e707d6ee2ec446aa96a1b707175c5c27c8c30cac
[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.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
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         void           *p;
105
106         _uc    = (struct ucontext *) _p;
107         _mc    = &_uc->uc_mcontext;
108
109 #if defined(__UCLIBC__)
110         _gregs = _mc->gpregs;
111 #else   
112         _gregs = _mc->gregs;
113 #endif
114
115         /* In glibc's ucontext.h the registers are defined as long long,
116            even for MIPS32, so we cast them.  This is not the case for
117            uClibc. */
118
119         pv  = (u1 *) (ptrint) _gregs[REG_PV];
120         sp  = (u1 *) (ptrint) _gregs[REG_SP];
121         ra  = (u1 *) (ptrint) _gregs[REG_RA];        /* this is correct for leafs */
122
123 #if !defined(__UCLIBC__)
124 # if ((__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) _mc->pc;
149 # endif
150 #else
151         xpc = (u1 *) (ptrint) _gregs[CTX_EPC];
152 #endif
153
154         /* get exception-throwing instruction */
155
156         mcode = *((u4 *) xpc);
157
158         d    = M_ITYPE_GET_RT(mcode);
159         s1   = M_ITYPE_GET_RS(mcode);
160         disp = M_ITYPE_GET_IMM(mcode);
161
162         /* check for special-load */
163
164         if (s1 == REG_ZERO) {
165                 /* we use the exception type as load displacement */
166
167                 type = disp;
168                 val  = _gregs[d];
169
170                 if (type == TRAP_COMPILER) {
171                         /* The XPC is the RA minus 4, because the RA points to the
172                            instruction after the call. */
173
174                         xpc = ra - 4;
175                 }
176         }
177         else {
178                 /* This is a normal NPE: addr must be NULL and the NPE-type
179                    define is 0. */
180
181                 addr = _gregs[s1];
182                 type = (int) addr;
183                 val  = 0;
184         }
185
186         /* Handle the trap. */
187
188         p = trap_handle(type, val, pv, sp, ra, xpc, _p);
189
190         /* Set registers. */
191
192         switch (type) {
193         case TRAP_COMPILER:
194                 if (p != NULL) {
195                         _gregs[REG_PV]  = (uintptr_t) p;
196 #if defined(__UCLIBC__)
197                         _gregs[CTX_EPC] = (uintptr_t) p;
198 #else
199                         _mc->pc         = (uintptr_t) p;
200 #endif
201                         break;
202                 }
203
204                 /* Get and set the PV from the parent Java method. */
205
206                 pv = md_codegen_get_pv_from_pc(ra);
207
208                 _gregs[REG_PV] = (uintptr_t) pv;
209
210                 /* Get the exception object. */
211
212                 p = builtin_retrieve_exception();
213
214                 assert(p != NULL);
215
216                 /* fall-through */
217
218         default:
219                 _gregs[REG_ITMP1_XPTR] = (uintptr_t) p;
220                 _gregs[REG_ITMP2_XPC]  = (uintptr_t) xpc;
221 #if defined(__UCLIBC__)
222                 _gregs[CTX_EPC]        = (uintptr_t) asm_handle_exception;
223 #else
224                 _mc->pc                = (uintptr_t) asm_handle_exception;
225 #endif
226         }
227 }
228
229
230 /**
231  * Signal handler for patcher calls.
232  */
233 void md_signal_handler_sigill(int sig, siginfo_t* siginfo, void* _p)
234 {
235         ucontext_t* _uc = (struct ucontext *) _p;
236         mcontext_t* _mc = &_uc->uc_mcontext;
237         greg_t* _gregs;
238
239 #if defined(__UCLIBC__)
240         _gregs = _mc->gpregs;
241 #else   
242         _gregs = _mc->gregs;
243 #endif
244
245         // In glibc's ucontext.h the registers are defined as long long
246         // int, even for MIPS32, so we cast them.  This is not the case
247         // for uClibc.
248         void* pv  = (void*) (uintptr_t) _gregs[REG_PV];
249         void* sp  = (void*) (uintptr_t) _gregs[REG_SP];
250         void* ra  = (void*) (uintptr_t) _gregs[REG_RA]; // The RA is correct for leaf methods.
251
252 #if defined(__UCLIBC__)
253         void* xpc = (void*) (uintptr_t) _gregs[CTX_EPC];
254 #else
255         void* xpc = (void*) (uintptr_t) _mc->pc;
256 #endif
257
258         // This signal is always a patcher.
259         int      type = TRAP_PATCHER;
260         intptr_t val  = 0;
261
262         // Handle the trap.
263         void* p = trap_handle(type, val, pv, sp, ra, xpc, _p);
264
265         // Set registers if we have an exception, continue execution
266         // otherwise.
267         if (p != NULL) {
268                 _gregs[REG_ITMP1_XPTR] = (uintptr_t) p;
269                 _gregs[REG_ITMP2_XPC]  = (uintptr_t) xpc;
270 #if defined(__UCLIBC__)
271                 _gregs[CTX_EPC]        = (uintptr_t) asm_handle_exception;
272 #else
273                 _mc->pc                = (uintptr_t) asm_handle_exception;
274 #endif
275         }
276         else {
277                 // We set the PC again because the cause may have changed the
278                 // XPC.
279 #if defined(__UCLIBC__)
280                 _gregs[CTX_EPC] = (uintptr_t) xpc;
281 #else
282                 _mc->pc         = (uintptr_t) xpc;
283 #endif
284         }
285 }
286
287
288 /* md_signal_handler_sigusr2 ***************************************************
289
290    DOCUMENT ME
291
292 *******************************************************************************/
293
294 void md_signal_handler_sigusr2(int sig, siginfo_t *siginfo, void *_p)
295 {
296 }
297
298
299 /**
300  * Read the given context into an executionstate.
301  *
302  * @param es      execution state
303  * @param context machine context
304  */
305 void md_executionstate_read(executionstate_t* es, void* context)
306 {
307         ucontext_t* _uc;
308         mcontext_t* _mc;
309         greg_t*     _gregs;
310         int         i;
311
312         vm_abort("md_executionstate_read: PLEASE REVISE ME!");
313
314         _uc = (ucontext_t*) context;
315         _mc = &_uc->uc_mcontext;
316
317 #if defined(__UCLIBC__)
318         _gregs = _mc->gpregs;
319 #else   
320         _gregs = _mc->gregs;
321 #endif
322
323         /* Read special registers. */
324
325         /* In glibc's ucontext.h the registers are defined as long long,
326            even for MIPS32, so we cast them.  This is not the case for
327            uClibc. */
328
329 #if defined(__UCLIBC__)
330         es->pc = _gregs[CTX_EPC];
331 #else
332         es->pc = (void*) (uintptr_t) _mc->pc;
333 #endif
334
335         es->sp = (void*) (uintptr_t) _gregs[REG_SP];
336         es->pv = (void*) (uintptr_t) _gregs[REG_PV];
337         es->ra = (void*) (uintptr_t) _gregs[REG_RA];
338
339         /* Read integer registers. */
340
341         for (i = 0; i < INT_REG_CNT; i++)
342                 es->intregs[i] = _gregs[i];
343
344         /* Read float registers. */
345
346         /* Do not use the assignment operator '=', as the type of the
347            _mc->fpregs[i] can cause invalid conversions. */
348
349         assert(sizeof(_mc->fpregs.fp_r) == sizeof(es->fltregs));
350         os_memcpy(&es->fltregs, &_mc->fpregs.fp_r, sizeof(_mc->fpregs.fp_r));
351 }
352
353
354 /**
355  * Write the given executionstate back to the context.
356  *
357  * @param es      execution state
358  * @param context machine context
359  */
360 void md_executionstate_write(executionstate_t* es, void* context)
361 {
362         ucontext_t* _uc;
363         mcontext_t* _mc;
364         greg_t*     _gregs;
365         int         i;
366
367         vm_abort("md_executionstate_write: PLEASE REVISE ME!");
368
369         _uc = (ucontext_t *) context;
370         _mc = &_uc->uc_mcontext;
371
372         /* Write integer registers. */
373
374         for (i = 0; i < INT_REG_CNT; i++)
375                 _gregs[i] = es->intregs[i];
376
377         /* Write float registers. */
378
379         /* Do not use the assignment operator '=', as the type of the
380            _mc->fpregs[i] can cause invalid conversions. */
381
382         assert(sizeof(_mc->fpregs.fp_r) == sizeof(es->fltregs));
383         os_memcpy(&_mc->fpregs.fp_r, &es->fltregs, sizeof(_mc->fpregs.fp_r));
384
385         /* Write special registers. */
386
387 #if defined(__UCLIBC__)
388         _gregs[CTX_EPC] = es->pc;
389 #else
390         _mc->pc         = (uintptr_t) es->pc;
391 #endif
392
393         _gregs[REG_SP]  = (uintptr_t) es->sp;
394         _gregs[REG_PV]  = (uintptr_t) es->pv;
395         _gregs[REG_RA]  = (uintptr_t) es->ra;
396 }
397
398
399 /*
400  * These are local overrides for various environment variables in Emacs.
401  * Please do not remove this and leave it at the end of the file, where
402  * Emacs will automagically detect them.
403  * ---------------------------------------------------------------------
404  * Local variables:
405  * mode: c
406  * indent-tabs-mode: t
407  * c-basic-offset: 4
408  * tab-width: 4
409  * End:
410  * vim:noexpandtab:sw=4:ts=4:
411  */