Proper x86_64 mnemonics
[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-common.h"
41 #include "mm/memory.h"
42
43 #include "vm/exceptions.h"
44 #include "vm/signallocal.h"
45
46 #include "vm/jit/asmpart.h"
47 #include "vm/jit/executionstate.h"
48 #include "vm/jit/stacktrace.h"
49 #include "vm/jit/trap.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__)
125 # if ((__GLIBC__ == 2) && (__GLIBC_MINOR__ < 5))
126         /* NOTE: We only need this for pre glibc-2.5. */
127
128         xpc = (u1 *) (ptrint) _mc->pc;
129
130         /* get the cause of this exception */
131
132         cause = _mc->cause;
133
134         /* check the cause to find the faulting instruction */
135
136         /* TODO: use defines for that stuff */
137
138         switch (cause & 0x0000003c) {
139         case 0x00000008:
140                 /* TLBL: XPC is ok */
141                 break;
142
143         case 0x00000010:
144                 /* AdEL: XPC is of the following instruction */
145                 xpc = xpc - 4;
146                 break;
147         }
148 # else
149         xpc = (u1 *) (ptrint) _mc->pc;
150 # endif
151 #else
152         xpc = (u1 *) (ptrint) _gregs[CTX_EPC];
153 #endif
154
155         /* get exception-throwing instruction */
156
157         mcode = *((u4 *) xpc);
158
159         d    = M_ITYPE_GET_RT(mcode);
160         s1   = M_ITYPE_GET_RS(mcode);
161         disp = M_ITYPE_GET_IMM(mcode);
162
163         /* check for special-load */
164
165         if (s1 == REG_ZERO) {
166                 /* we use the exception type as load displacement */
167
168                 type = disp;
169                 val  = _gregs[d];
170
171                 if (type == TRAP_COMPILER) {
172                         /* The XPC is the RA minus 4, because the RA points to the
173                            instruction after the call. */
174
175                         xpc = ra - 4;
176                 }
177         }
178         else {
179                 /* This is a normal NPE: addr must be NULL and the NPE-type
180                    define is 0. */
181
182                 addr = _gregs[s1];
183                 type = (int) addr;
184                 val  = 0;
185         }
186
187         /* Handle the trap. */
188
189         p = trap_handle(type, val, pv, sp, ra, xpc, _p);
190
191         /* Set registers. */
192
193         switch (type) {
194         case TRAP_COMPILER:
195                 if (p != NULL) {
196                         _gregs[REG_PV]  = (uintptr_t) p;
197 #if defined(__UCLIBC__)
198                         _gregs[CTX_EPC] = (uintptr_t) p;
199 #else
200                         _mc->pc         = (uintptr_t) p;
201 #endif
202                         break;
203                 }
204
205                 /* Get and set the PV from the parent Java method. */
206
207                 pv = md_codegen_get_pv_from_pc(ra);
208
209                 _gregs[REG_PV] = (uintptr_t) pv;
210
211                 /* Get the exception object. */
212
213                 p = builtin_retrieve_exception();
214
215                 assert(p != NULL);
216
217                 /* fall-through */
218
219         case TRAP_PATCHER:
220                 if (p == NULL) {
221                         /* We set the PC again because the cause may have changed
222                            the XPC. */
223
224 #if defined(__UCLIBC__)
225                         _gregs[CTX_EPC] = (uintptr_t) xpc;
226 #else
227                         _mc->pc         = (uintptr_t) xpc;
228 #endif
229                         break;
230                 }
231
232                 /* fall-through */
233                 
234         default:
235                 _gregs[REG_ITMP1_XPTR] = (uintptr_t) p;
236                 _gregs[REG_ITMP2_XPC]  = (uintptr_t) xpc;
237 #if defined(__UCLIBC__)
238                 _gregs[CTX_EPC]        = (uintptr_t) asm_handle_exception;
239 #else
240                 _mc->pc                = (uintptr_t) asm_handle_exception;
241 #endif
242         }
243 }
244
245
246 /* md_signal_handler_sigusr2 ***************************************************
247
248    DOCUMENT ME
249
250 *******************************************************************************/
251
252 void md_signal_handler_sigusr2(int sig, siginfo_t *siginfo, void *_p)
253 {
254 }
255
256
257 /**
258  * Read the given context into an executionstate.
259  *
260  * @param es      execution state
261  * @param context machine context
262  */
263 void md_executionstate_read(executionstate_t* es, void* context)
264 {
265         ucontext_t* _uc;
266         mcontext_t* _mc;
267         greg_t*     _gregs;
268         int         i;
269
270         vm_abort("md_executionstate_read: PLEASE REVISE ME!");
271
272         _uc = (ucontext_t*) context;
273         _mc = &_uc->uc_mcontext;
274
275 #if defined(__UCLIBC__)
276         _gregs = _mc->gpregs;
277 #else   
278         _gregs = _mc->gregs;
279 #endif
280
281         /* Read special registers. */
282
283         /* In glibc's ucontext.h the registers are defined as long long,
284            even for MIPS32, so we cast them.  This is not the case for
285            uClibc. */
286
287 #if defined(__UCLIBC__)
288         es->pc = _gregs[CTX_EPC];
289 #else
290         es->pc = (void*) (uintptr_t) _mc->pc;
291 #endif
292
293         es->sp = (void*) (uintptr_t) _gregs[REG_SP];
294         es->pv = (void*) (uintptr_t) _gregs[REG_PV];
295         es->ra = (void*) (uintptr_t) _gregs[REG_RA];
296
297         /* Read integer registers. */
298
299         for (i = 0; i < INT_REG_CNT; i++)
300                 es->intregs[i] = _gregs[i];
301
302         /* Read float registers. */
303
304         /* Do not use the assignment operator '=', as the type of the
305            _mc->fpregs[i] can cause invalid conversions. */
306
307         assert(sizeof(_mc->fpregs.fp_r) == sizeof(es->fltregs));
308         system_memcpy(&es->fltregs, &_mc->fpregs.fp_r, sizeof(_mc->fpregs.fp_r));
309 }
310
311
312 /**
313  * Write the given executionstate back to the context.
314  *
315  * @param es      execution state
316  * @param context machine context
317  */
318 void md_executionstate_write(executionstate_t* es, void* context)
319 {
320         ucontext_t* _uc;
321         mcontext_t* _mc;
322         greg_t*     _gregs;
323         int         i;
324
325         vm_abort("md_executionstate_write: PLEASE REVISE ME!");
326
327         _uc = (ucontext_t *) context;
328         _mc = &_uc->uc_mcontext;
329
330         /* Write integer registers. */
331
332         for (i = 0; i < INT_REG_CNT; i++)
333                 _gregs[i] = es->intregs[i];
334
335         /* Write float registers. */
336
337         /* Do not use the assignment operator '=', as the type of the
338            _mc->fpregs[i] can cause invalid conversions. */
339
340         assert(sizeof(_mc->fpregs.fp_r) == sizeof(es->fltregs));
341         system_memcpy(&_mc->fpregs.fp_r, &es->fltregs, sizeof(_mc->fpregs.fp_r));
342
343         /* Write special registers. */
344
345 #if defined(__UCLIBC__)
346         _gregs[CTX_EPC] = es->pc;
347 #else
348         _mc->pc         = (uintptr_t) es->pc;
349 #endif
350
351         _gregs[REG_SP]  = (uintptr_t) es->sp;
352         _gregs[REG_PV]  = (uintptr_t) es->pv;
353         _gregs[REG_RA]  = (uintptr_t) es->ra;
354 }
355
356
357 /* md_critical_section_restart *************************************************
358
359    Search the critical sections tree for a matching section and set
360    the PC to the restart point, if necessary.
361
362 *******************************************************************************/
363
364 #if defined(ENABLE_THREADS)
365 void md_critical_section_restart(ucontext_t *_uc)
366 {
367         mcontext_t *_mc;
368         u1         *pc;
369         u1         *npc;
370
371         _mc = &_uc->uc_mcontext;
372
373 #if defined(__UCLIBC__)
374         pc = (u1 *) (ptrint) _mc->gpregs[CTX_EPC];
375 #else
376         pc = (u1 *) (ptrint) _mc->pc;
377 #endif
378
379         npc = critical_find_restart_point(pc);
380
381         if (npc != NULL) {
382 #if defined(__UCLIBC__)
383                 _mc->gpregs[CTX_EPC] = (ptrint) npc;
384 #else
385                 _mc->pc              = (ptrint) npc;
386 #endif
387         }
388 }
389 #endif
390
391
392 /*
393  * These are local overrides for various environment variables in Emacs.
394  * Please do not remove this and leave it at the end of the file, where
395  * Emacs will automagically detect them.
396  * ---------------------------------------------------------------------
397  * Local variables:
398  * mode: c
399  * indent-tabs-mode: t
400  * c-basic-offset: 4
401  * tab-width: 4
402  * End:
403  * vim:noexpandtab:sw=4:ts=4:
404  */