* src/vm/jit/mips/arch.h (JIT_COMPILER_VIA_SIGNAL): Temporarily added.
[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 */
26
27
28 #include "config.h"
29
30 #include <assert.h>
31 #include <sgidefs.h> /* required for _MIPS_SIM_ABI* defines (before signal.h) */
32 #include <signal.h>
33 #include <stdint.h>
34 #include <ucontext.h>
35
36 #include "vm/types.h"
37
38 #include "vm/jit/mips/codegen.h"
39 #include "vm/jit/mips/md-abi.h"
40
41 #include "mm/gc-common.h"
42 #include "mm/memory.h"
43
44 #include "vm/exceptions.h"
45 #include "vm/signallocal.h"
46
47 #include "vm/jit/asmpart.h"
48 #include "vm/jit/md.h"
49 #include "vm/jit/stacktrace.h"
50
51
52 /* md_init *********************************************************************
53
54    Do some machine dependent initialization.
55
56 *******************************************************************************/
57
58 void md_init(void)
59 {
60         /* The Boehm GC initialization blocks the SIGSEGV signal. So we do
61            a dummy allocation here to ensure that the GC is
62            initialized. */
63
64 #if defined(ENABLE_GC_BOEHM)
65         (void) GCNEW(int);
66 #endif
67
68 #if 0
69         /* Turn off flush-to-zero */
70
71         {
72                 union fpc_csr n;
73                 n.fc_word = get_fpc_csr();
74                 n.fc_struct.flush = 0;
75                 set_fpc_csr(n.fc_word);
76         }
77 #endif
78 }
79
80
81 /* md_signal_handler_sigsegv ***************************************************
82
83    NullPointerException signal handler for hardware null pointer
84    check.
85
86 *******************************************************************************/
87
88 void md_signal_handler_sigsegv(int sig, siginfo_t *siginfo, void *_p)
89 {
90         ucontext_t     *_uc;
91         mcontext_t     *_mc;
92         greg_t         *_gregs;
93         u1             *pv;
94         u1             *sp;
95         u1             *ra;
96         u1             *xpc;
97         unsigned int    cause;
98         u4              mcode;
99         int             d;
100         int             s1;
101         int16_t         disp;
102         intptr_t        val;
103         intptr_t        addr;
104         int             type;
105         void           *p;
106
107         _uc    = (struct ucontext *) _p;
108         _mc    = &_uc->uc_mcontext;
109
110 #if defined(__UCLIBC__)
111         _gregs = _mc->gpregs;
112 #else   
113         _gregs = _mc->gregs;
114 #endif
115
116         /* In glibc's ucontext.h the registers are defined as long long,
117            even for MIPS32, so we cast them.  This is not the case for
118            uClibc. */
119
120         pv  = (u1 *) (ptrint) _gregs[REG_PV];
121         sp  = (u1 *) (ptrint) _gregs[REG_SP];
122         ra  = (u1 *) (ptrint) _gregs[REG_RA];        /* this is correct for leafs */
123
124 #if !defined(__UCLIBC__) && ((__GLIBC__ == 2) && (__GLIBC_MINOR__ < 5))
125         /* NOTE: We only need this for pre glibc-2.5. */
126
127         xpc = (u1 *) (ptrint) _mc->pc;
128
129         /* get the cause of this exception */
130
131         cause = _mc->cause;
132
133         /* check the cause to find the faulting instruction */
134
135         /* TODO: use defines for that stuff */
136
137         switch (cause & 0x0000003c) {
138         case 0x00000008:
139                 /* TLBL: XPC is ok */
140                 break;
141
142         case 0x00000010:
143                 /* AdEL: XPC is of the following instruction */
144                 xpc = xpc - 4;
145                 break;
146         }
147 #else
148         xpc = (u1 *) (ptrint) _gregs[CTX_EPC];
149 #endif
150
151         /* get exception-throwing instruction */
152
153         mcode = *((u4 *) xpc);
154
155         d    = M_ITYPE_GET_RT(mcode);
156         s1   = M_ITYPE_GET_RS(mcode);
157         disp = M_ITYPE_GET_IMM(mcode);
158
159         /* check for special-load */
160
161         if (s1 == REG_ZERO) {
162                 /* we use the exception type as load displacement */
163
164                 type = disp;
165                 val  = _gregs[d];
166
167                 if (type == EXCEPTION_HARDWARE_COMPILER) {
168                         /* The XPC is the RA minus 4, because the RA points to the
169                            instruction after the call. */
170
171                         xpc = ra - 4;
172                 }
173         }
174         else {
175                 /* This is a normal NPE: addr must be NULL and the NPE-type
176                    define is 0. */
177
178                 addr = _gregs[s1];
179                 type = (s4) addr;
180                 val  = 0;
181         }
182
183         /* Handle the type. */
184
185         p = signal_handle(type, val, pv, sp, ra, xpc, _p);
186
187 #if 0
188         /* set registers (only if exception object ready) */
189
190         if (p != NULL) {
191                 _gregs[REG_ITMP1_XPTR] = (intptr_t) p;
192                 _gregs[REG_ITMP2_XPC]  = (intptr_t) xpc;
193
194 #if defined(__UCLIBC__)
195                 _gregs[CTX_EPC]        = (intptr_t) asm_handle_exception;
196 #else
197                 _mc->pc                = (intptr_t) asm_handle_exception;
198 #endif
199         }
200         else {
201 #if defined(__UCLIBC__)
202                 _gregs[CTX_EPC]        = (intptr_t) xpc;
203 #else
204                 _mc->pc                = (intptr_t) xpc;
205 #endif
206         }
207 #endif
208
209         /* Set registers. */
210
211         switch (type) {
212         case EXCEPTION_HARDWARE_COMPILER:
213                 if (p != NULL) {
214                         _gregs[REG_PV]  = (uintptr_t) p;
215 #if defined(__UCLIBC__)
216                         _gregs[CTX_EPC] = (uintptr_t) p;
217 #else
218                         _mc->pc         = (uintptr_t) p;
219 #endif
220                         break;
221                 }
222
223                 /* Get and set the PV from the parent Java method. */
224
225                 pv = md_codegen_get_pv_from_pc(ra);
226
227                 _gregs[REG_PV] = (uintptr_t) pv;
228
229                 /* Get the exception object. */
230
231                 p = builtin_retrieve_exception();
232
233                 assert(p != NULL);
234
235                 /* fall-through */
236
237         case EXCEPTION_HARDWARE_PATCHER:
238                 if (p == NULL) {
239                         /* We set the PC again because the cause may have changed
240                            the XPC. */
241
242 #if defined(__UCLIBC__)
243                         _gregs[CTX_EPC] = (uintptr_t) xpc;
244 #else
245                         _mc->pc         = (uintptr_t) xpc;
246 #endif
247                         break;
248                 }
249
250                 /* fall-through */
251                 
252         default:
253                 _gregs[REG_ITMP1_XPTR] = (uintptr_t) p;
254                 _gregs[REG_ITMP2_XPC]  = (uintptr_t) xpc;
255 #if defined(__UCLIBC__)
256                 _gregs[CTX_EPC]        = (uintptr_t) asm_handle_exception;
257 #else
258                 _mc->pc                = (uintptr_t) asm_handle_exception;
259 #endif
260         }
261 }
262
263
264 /* md_signal_handler_sigusr2 ***************************************************
265
266    DOCUMENT ME
267
268 *******************************************************************************/
269
270 void md_signal_handler_sigusr2(int sig, siginfo_t *siginfo, void *_p)
271 {
272 }
273
274
275 /* md_critical_section_restart *************************************************
276
277    Search the critical sections tree for a matching section and set
278    the PC to the restart point, if necessary.
279
280 *******************************************************************************/
281
282 #if defined(ENABLE_THREADS)
283 void md_critical_section_restart(ucontext_t *_uc)
284 {
285         mcontext_t *_mc;
286         u1         *pc;
287         u1         *npc;
288
289         _mc = &_uc->uc_mcontext;
290
291 #if defined(__UCLIBC__)
292         pc = (u1 *) (ptrint) _mc->gpregs[CTX_EPC];
293 #else
294         pc = (u1 *) (ptrint) _mc->pc;
295 #endif
296
297         npc = critical_find_restart_point(pc);
298
299         if (npc != NULL) {
300 #if defined(__UCLIBC__)
301                 _mc->gpregs[CTX_EPC] = (ptrint) npc;
302 #else
303                 _mc->pc              = (ptrint) npc;
304 #endif
305         }
306 }
307 #endif
308
309
310 /*
311  * These are local overrides for various environment variables in Emacs.
312  * Please do not remove this and leave it at the end of the file, where
313  * Emacs will automagically detect them.
314  * ---------------------------------------------------------------------
315  * Local variables:
316  * mode: c
317  * indent-tabs-mode: t
318  * c-basic-offset: 4
319  * tab-width: 4
320  * End:
321  * vim:noexpandtab:sw=4:ts=4:
322  */