8beb3adf7954352d5d1578919ee1515543e58b73
[cacao.git] / src / vm / jit / mips / linux / md-os.c
1 /* src/vm/jit/mips/linux/md-os.c - machine dependent MIPS Linux functions
2
3    Copyright (C) 1996-2005, 2006, 2007 R. Grafl, A. Krall, C. Kruegel,
4    C. Oates, R. Obermaisser, M. Platter, M. Probst, S. Ring,
5    E. Steiner, C. Thalinger, D. Thuernbeck, P. Tomsich, C. Ullrich,
6    J. Wenninger, Institut f. Computersprachen - TU Wien
7
8    This file is part of CACAO.
9
10    This program is free software; you can redistribute it and/or
11    modify it under the terms of the GNU General Public License as
12    published by the Free Software Foundation; either version 2, or (at
13    your option) any later version.
14
15    This program is distributed in the hope that it will be useful, but
16    WITHOUT ANY WARRANTY; without even the implied warranty of
17    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18    General Public License for more details.
19
20    You should have received a copy of the GNU General Public License
21    along with this program; if not, write to the Free Software
22    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
23    02110-1301, USA.
24
25    $Id: md-os.c 7688 2007-04-12 09:05:12Z michi $
26
27 */
28
29
30 #include "config.h"
31
32 #include <assert.h>
33 #include <sgidefs.h> /* required for _MIPS_SIM_ABI* defines (before signal.h) */
34 #include <signal.h>
35 #include <ucontext.h>
36
37 #include "vm/types.h"
38
39 #include "vm/jit/mips/codegen.h"
40 #include "vm/jit/mips/md-abi.h"
41
42 #include "mm/gc-common.h"
43
44 #include "vm/exceptions.h"
45 #include "vm/stringlocal.h"
46
47 #include "vm/jit/asmpart.h"
48 #include "vm/jit/stacktrace.h"
49
50
51 /* md_init *********************************************************************
52
53    Do some machine dependent initialization.
54
55 *******************************************************************************/
56
57 void md_init(void)
58 {
59         /* The Boehm GC initialization blocks the SIGSEGV signal. So we do a      */
60         /* dummy allocation here to ensure that the GC is initialized.            */
61
62 #if defined(ENABLE_GC_BOEHM)
63         (void) GCNEW(u1);
64 #endif
65
66 #if 0
67         /* Turn off flush-to-zero */
68
69         {
70                 union fpc_csr n;
71                 n.fc_word = get_fpc_csr();
72                 n.fc_struct.flush = 0;
73                 set_fpc_csr(n.fc_word);
74         }
75 #endif
76 }
77
78
79 /* md_signal_handler_sigsegv ***************************************************
80
81    NullPointerException signal handler for hardware null pointer
82    check.
83
84 *******************************************************************************/
85
86 void md_signal_handler_sigsegv(int sig, siginfo_t *siginfo, void *_p)
87 {
88         ucontext_t        *_uc;
89         mcontext_t        *_mc;
90         greg_t            *_gregs;
91         u1                *pv;
92         u1                *sp;
93         u1                *ra;
94         u1                *xpc;
95         unsigned int       cause;
96         u4                 mcode;
97         s4                 d;
98         s4                 s1;
99         s4                 disp;
100         ptrint             val;
101         ptrint             addr;
102         s4                 type;
103         java_objectheader *o;
104
105         _uc    = (struct ucontext *) _p;
106         _mc    = &_uc->uc_mcontext;
107
108 #if defined(__UCLIBC__)
109         _gregs = _mc->gpregs;
110 #else   
111         _gregs = _mc->gregs;
112 #endif
113
114         /* In glibc's ucontext.h the registers are defined as long long,
115            even for MIPS32, so we cast them.  This is not the case for
116            uClibc. */
117
118         pv  = (u1 *) (ptrint) _gregs[REG_PV];
119         sp  = (u1 *) (ptrint) _gregs[REG_SP];
120         ra  = (u1 *) (ptrint) _gregs[REG_RA];        /* this is correct for leafs */
121
122 #if !defined(__UCLIBC__) && ((__GLIBC__ == 2) && (__GLIBC_MINOR__ < 5))
123         /* NOTE: We only need this for pre glibc-2.5. */
124
125         xpc = (u1 *) (ptrint) _mc->pc;
126
127         /* get the cause of this exception */
128
129         cause = _mc->cause;
130
131         /* check the cause to find the faulting instruction */
132
133         /* TODO: use defines for that stuff */
134
135         switch (cause & 0x0000003c) {
136         case 0x00000008:
137                 /* TLBL: XPC is ok */
138                 break;
139
140         case 0x00000010:
141                 /* AdEL: XPC is of the following instruction */
142                 xpc = xpc - 4;
143                 break;
144         }
145 #else
146         xpc = (u1 *) (ptrint) _gregs[CTX_EPC];
147 #endif
148
149         /* get exception-throwing instruction */
150
151         mcode = *((u4 *) xpc);
152
153         d    = M_ITYPE_GET_RT(mcode);
154         s1   = M_ITYPE_GET_RS(mcode);
155         disp = M_ITYPE_GET_IMM(mcode);
156
157         /* check for special-load */
158
159         if (s1 == REG_ZERO) {
160                 /* we use the exception type as load displacement */
161
162                 type = disp;
163                 val  = _gregs[d];
164         }
165         else {
166                 /* This is a normal NPE: addr must be NULL and the NPE-type
167                    define is 0. */
168
169                 addr = _gregs[s1];
170                 type = (s4) addr;
171                 val  = 0;
172         }
173
174         /* generate appropriate exception */
175
176         o = exceptions_new_hardware_exception(pv, sp, ra, xpc, type, val);
177
178         /* set registers */
179
180         _gregs[REG_ITMP1_XPTR] = (ptrint) o;
181         _gregs[REG_ITMP2_XPC]  = (ptrint) xpc;
182
183 #if defined(__UCLIBC__)
184         _gregs[CTX_EPC]        = (ptrint) asm_handle_exception;
185 #else
186         _mc->pc                = (ptrint) asm_handle_exception;
187 #endif
188 }
189
190
191 /* md_signal_handler_sigusr2 ***************************************************
192
193    DOCUMENT ME
194
195 *******************************************************************************/
196
197 void md_signal_handler_sigusr2(int sig, siginfo_t *siginfo, void *_p)
198 {
199 }
200
201
202 #if defined(ENABLE_THREADS)
203 void thread_restartcriticalsection(ucontext_t *_uc)
204 {
205         mcontext_t *_mc;
206         u1         *pc;
207         u1         *npc;
208
209         _mc = &_uc->uc_mcontext;
210
211 #if defined(__UCLIBC__)
212         pc = (u1 *) (ptrint) _mc->gpregs[CTX_EPC];
213 #else
214         pc = (u1 *) (ptrint) _mc->pc;
215 #endif
216
217         npc = critical_find_restart_point(pc);
218
219         if (npc != NULL) {
220 #if defined(__UCLIBC__)
221                 _mc->gpregs[CTX_EPC] = (ptrint) npc;
222 #else
223                 _mc->pc              = (ptrint) npc;
224 #endif
225         }
226 }
227 #endif
228
229
230 /*
231  * These are local overrides for various environment variables in Emacs.
232  * Please do not remove this and leave it at the end of the file, where
233  * Emacs will automagically detect them.
234  * ---------------------------------------------------------------------
235  * Local variables:
236  * mode: c
237  * indent-tabs-mode: t
238  * c-basic-offset: 4
239  * tab-width: 4
240  * End:
241  * vim:noexpandtab:sw=4:ts=4:
242  */