* src/vm/jit/trap.cpp (trap_handle) [__MIPS__]: Use executionstate for trap
[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, 2008
4    CACAOVM - Verein zur Foerderung der freien virtuellen Maschine CACAO
5
6    This file is part of CACAO.
7
8    This program is free software; you can redistribute it and/or
9    modify it under the terms of the GNU General Public License as
10    published by the Free Software Foundation; either version 2, or (at
11    your option) any later version.
12
13    This program is distributed in the hope that it will be useful, but
14    WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16    General Public License for more details.
17
18    You should have received a copy of the GNU General Public License
19    along with this program; if not, write to the Free Software
20    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21    02110-1301, USA.
22
23 */
24
25
26 #include "config.h"
27
28 #include <assert.h>
29 #include <sgidefs.h> /* required for _MIPS_SIM_ABI* defines (before signal.h) */
30 #include <signal.h>
31 #include <stdint.h>
32 #include <ucontext.h>
33
34 #include "vm/types.h"
35
36 #include "vm/jit/mips/codegen.h"
37 #include "vm/jit/mips/md.h"
38 #include "vm/jit/mips/md-abi.h"
39
40 #include "mm/gc.hpp"
41 #include "mm/memory.hpp"
42
43 #include "vm/signallocal.hpp"
44 #include "vm/os.hpp"
45
46 #include "vm/jit/asmpart.h"
47 #include "vm/jit/executionstate.h"
48 #include "vm/jit/trap.hpp"
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
60            a dummy allocation here to ensure that the GC is
61            initialized. */
62
63 #if defined(ENABLE_GC_BOEHM)
64         (void) GCNEW(int);
65 #endif
66
67 #if 0
68         /* Turn off flush-to-zero */
69
70         {
71                 union fpc_csr n;
72                 n.fc_word = get_fpc_csr();
73                 n.fc_struct.flush = 0;
74                 set_fpc_csr(n.fc_word);
75         }
76 #endif
77 }
78
79
80 /* md_signal_handler_sigsegv ***************************************************
81
82    NullPointerException signal handler for hardware null pointer
83    check.
84
85 *******************************************************************************/
86
87 void md_signal_handler_sigsegv(int sig, siginfo_t *siginfo, void *_p)
88 {
89         ucontext_t     *_uc;
90         mcontext_t     *_mc;
91         greg_t         *_gregs;
92         u1             *pv;
93         u1             *sp;
94         u1             *ra;
95         u1             *xpc;
96         unsigned int    cause;
97         u4              mcode;
98         int             d;
99         int             s1;
100         int16_t         disp;
101         intptr_t        val;
102         intptr_t        addr;
103         int             type;
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__)
123 # if ((__GLIBC__ == 2) && (__GLIBC_MINOR__ < 5))
124         /* NOTE: We only need this for pre glibc-2.5. */
125
126         xpc = (u1 *) (ptrint) _mc->pc;
127
128         /* get the cause of this exception */
129
130         cause = _mc->cause;
131
132         /* check the cause to find the faulting instruction */
133
134         /* TODO: use defines for that stuff */
135
136         switch (cause & 0x0000003c) {
137         case 0x00000008:
138                 /* TLBL: XPC is ok */
139                 break;
140
141         case 0x00000010:
142                 /* AdEL: XPC is of the following instruction */
143                 xpc = xpc - 4;
144                 break;
145         }
146 # else
147         xpc = (u1 *) (ptrint) _mc->pc;
148 # endif
149 #else
150         xpc = (u1 *) (ptrint) _gregs[CTX_EPC];
151 #endif
152
153         /* get exception-throwing instruction */
154
155         mcode = *((u4 *) xpc);
156
157         d    = M_ITYPE_GET_RT(mcode);
158         s1   = M_ITYPE_GET_RS(mcode);
159         disp = M_ITYPE_GET_IMM(mcode);
160
161         /* check for special-load */
162
163         if (s1 == REG_ZERO) {
164                 /* we use the exception type as load displacement */
165
166                 type = disp;
167                 val  = _gregs[d];
168
169                 if (type == TRAP_COMPILER) {
170                         /* The XPC is the RA minus 4, because the RA points to the
171                            instruction after the call. */
172
173                         xpc = ra - 4;
174                 }
175         }
176         else {
177                 /* This is a normal NPE: addr must be NULL and the NPE-type
178                    define is 0. */
179
180                 addr = _gregs[s1];
181                 type = (int) addr;
182                 val  = 0;
183         }
184
185         /* Handle the trap. */
186
187         trap_handle(type, val, pv, sp, ra, xpc, _p);
188 }
189
190
191 /**
192  * Signal handler for patcher calls.
193  */
194 void md_signal_handler_sigill(int sig, siginfo_t* siginfo, void* _p)
195 {
196         ucontext_t* _uc = (struct ucontext *) _p;
197         mcontext_t* _mc = &_uc->uc_mcontext;
198         greg_t* _gregs;
199
200 #if defined(__UCLIBC__)
201         _gregs = _mc->gpregs;
202 #else   
203         _gregs = _mc->gregs;
204 #endif
205
206         // In glibc's ucontext.h the registers are defined as long long
207         // int, even for MIPS32, so we cast them.  This is not the case
208         // for uClibc.
209         void* pv  = (void*) (uintptr_t) _gregs[REG_PV];
210         void* sp  = (void*) (uintptr_t) _gregs[REG_SP];
211         void* ra  = (void*) (uintptr_t) _gregs[REG_RA]; // The RA is correct for leaf methods.
212
213 #if defined(__UCLIBC__)
214         void* xpc = (void*) (uintptr_t) _gregs[CTX_EPC];
215 #else
216         void* xpc = (void*) (uintptr_t) _mc->pc;
217 #endif
218
219         // This signal is always a patcher.
220         int      type = TRAP_PATCHER;
221         intptr_t val  = 0;
222
223         // Handle the trap.
224         void* p = trap_handle(type, val, pv, sp, ra, xpc, _p);
225
226         // Set registers
227         if (p == NULL) {
228                 // We set the PC again because the cause may have changed the
229                 // XPC.
230 #if defined(__UCLIBC__)
231                 _gregs[CTX_EPC] = (uintptr_t) xpc;
232 #else
233                 _mc->pc         = (uintptr_t) xpc;
234 #endif
235         }
236 }
237
238
239 /* md_signal_handler_sigusr2 ***************************************************
240
241    DOCUMENT ME
242
243 *******************************************************************************/
244
245 void md_signal_handler_sigusr2(int sig, siginfo_t *siginfo, void *_p)
246 {
247 }
248
249
250 /**
251  * Read the given context into an executionstate.
252  *
253  * @param es      execution state
254  * @param context machine context
255  */
256 void md_executionstate_read(executionstate_t* es, void* context)
257 {
258         ucontext_t* _uc;
259         mcontext_t* _mc;
260         greg_t*     _gregs;
261         int         i;
262
263         vm_abort("md_executionstate_read: PLEASE REVISE ME!");
264
265         _uc = (ucontext_t*) context;
266         _mc = &_uc->uc_mcontext;
267
268 #if defined(__UCLIBC__)
269         _gregs = _mc->gpregs;
270 #else   
271         _gregs = _mc->gregs;
272 #endif
273
274         /* Read special registers. */
275
276         /* In glibc's ucontext.h the registers are defined as long long,
277            even for MIPS32, so we cast them.  This is not the case for
278            uClibc. */
279
280 #if defined(__UCLIBC__)
281         es->pc = _gregs[CTX_EPC];
282 #else
283         es->pc = (void*) (uintptr_t) _mc->pc;
284 #endif
285
286         es->sp = (void*) (uintptr_t) _gregs[REG_SP];
287         es->pv = (void*) (uintptr_t) _gregs[REG_PV];
288         es->ra = (void*) (uintptr_t) _gregs[REG_RA];
289
290         /* Read integer registers. */
291
292         for (i = 0; i < INT_REG_CNT; i++)
293                 es->intregs[i] = _gregs[i];
294
295         /* Read float registers. */
296
297         /* Do not use the assignment operator '=', as the type of the
298            _mc->fpregs[i] can cause invalid conversions. */
299
300         assert(sizeof(_mc->fpregs.fp_r) == sizeof(es->fltregs));
301         os_memcpy(&es->fltregs, &_mc->fpregs.fp_r, sizeof(_mc->fpregs.fp_r));
302 }
303
304
305 /**
306  * Write the given executionstate back to the context.
307  *
308  * @param es      execution state
309  * @param context machine context
310  */
311 void md_executionstate_write(executionstate_t* es, void* context)
312 {
313         ucontext_t* _uc;
314         mcontext_t* _mc;
315         greg_t*     _gregs;
316         int         i;
317
318         vm_abort("md_executionstate_write: PLEASE REVISE ME!");
319
320         _uc = (ucontext_t *) context;
321         _mc = &_uc->uc_mcontext;
322
323         /* Write integer registers. */
324
325         for (i = 0; i < INT_REG_CNT; i++)
326                 _gregs[i] = es->intregs[i];
327
328         /* Write float registers. */
329
330         /* Do not use the assignment operator '=', as the type of the
331            _mc->fpregs[i] can cause invalid conversions. */
332
333         assert(sizeof(_mc->fpregs.fp_r) == sizeof(es->fltregs));
334         os_memcpy(&_mc->fpregs.fp_r, &es->fltregs, sizeof(_mc->fpregs.fp_r));
335
336         /* Write special registers. */
337
338 #if defined(__UCLIBC__)
339         _gregs[CTX_EPC] = es->pc;
340 #else
341         _mc->pc         = (uintptr_t) es->pc;
342 #endif
343
344         _gregs[REG_SP]  = (uintptr_t) es->sp;
345         _gregs[REG_PV]  = (uintptr_t) es->pv;
346         _gregs[REG_RA]  = (uintptr_t) es->ra;
347 }
348
349
350 /*
351  * These are local overrides for various environment variables in Emacs.
352  * Please do not remove this and leave it at the end of the file, where
353  * Emacs will automagically detect them.
354  * ---------------------------------------------------------------------
355  * Local variables:
356  * mode: c
357  * indent-tabs-mode: t
358  * c-basic-offset: 4
359  * tab-width: 4
360  * End:
361  * vim:noexpandtab:sw=4:ts=4:
362  */