* Merged executionstate branch.
[cacao.git] / src / vm / jit / powerpc / linux / md-os.c
1 /* src/vm/jit/powerpc/linux/md-os.c - machine dependent PowerPC 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 <stdint.h>
30 #include <ucontext.h>
31
32 #include "vm/types.h"
33
34 #include "vm/jit/powerpc/codegen.h"
35 #include "vm/jit/powerpc/md.h"
36 #include "vm/jit/powerpc/linux/md-abi.h"
37
38 #include "threads/thread.h"
39
40 #include "vm/builtin.h"
41 #include "vm/exceptions.h"
42 #include "vm/signallocal.h"
43 #include "vm/stringlocal.h"
44
45 #include "vm/jit/asmpart.h"
46 #include "vm/jit/executionstate.h"
47
48 #if defined(ENABLE_PROFILING)
49 # include "vm/jit/optimizing/profile.h"
50 #endif
51
52 #include "vm/jit/stacktrace.h"
53
54 #include "vmcore/system.h"
55
56
57 /* md_signal_handler_sigsegv ***************************************************
58
59    Signal handler for hardware-exceptions.
60
61 *******************************************************************************/
62
63 void md_signal_handler_sigsegv(int sig, siginfo_t *siginfo, void *_p)
64 {
65         ucontext_t     *_uc;
66         mcontext_t     *_mc;
67         unsigned long  *_gregs;
68         u1             *pv;
69         u1             *sp;
70         u1             *ra;
71         u1             *xpc;
72         u4              mcode;
73         int             s1;
74         int16_t         disp;
75         int             d;
76         intptr_t        addr;
77         intptr_t        val;
78         int             type;
79         void           *p;
80
81         _uc = (ucontext_t *) _p;
82
83 #if defined(__UCLIBC__)
84         _mc    = &(_uc->uc_mcontext);
85         _gregs = _mc->regs->gpr;
86 #else
87         _mc    = _uc->uc_mcontext.uc_regs;
88         _gregs = _mc->gregs;
89 #endif
90
91         pv  = (u1 *) _gregs[REG_PV];
92         sp  = (u1 *) _gregs[REG_SP];
93         ra  = (u1 *) _gregs[PT_LNK];                 /* this is correct for leafs */
94         xpc = (u1 *) _gregs[PT_NIP];
95
96         /* get exception-throwing instruction */
97
98         mcode = *((u4 *) xpc);
99
100         s1   = M_INSTR_OP2_IMM_A(mcode);
101         disp = M_INSTR_OP2_IMM_I(mcode);
102         d    = M_INSTR_OP2_IMM_D(mcode);
103
104         val  = _gregs[d];
105
106         /* check for special-load */
107
108         if (s1 == REG_ZERO) {
109                 /* we use the exception type as load displacement */
110
111                 type = disp;
112
113                 if (type == EXCEPTION_HARDWARE_COMPILER) {
114                         /* The XPC is the RA minus 4, because the RA points to the
115                            instruction after the call. */
116
117                         xpc = ra - 4;
118                 }
119         }
120         else {
121                 /* This is a normal NPE: addr must be NULL and the NPE-type
122                    define is 0. */
123
124                 addr = _gregs[s1];
125                 type = EXCEPTION_HARDWARE_NULLPOINTER;
126
127                 if (addr != 0)
128                         vm_abort("md_signal_handler_sigsegv: faulting address is not NULL: addr=%p", addr);
129         }
130
131         /* Handle the type. */
132
133         p = signal_handle(type, val, pv, sp, ra, xpc, _p);
134
135         /* Set registers. */
136
137         switch (type) {
138         case EXCEPTION_HARDWARE_COMPILER:
139                 if (p != NULL) {
140                         _gregs[REG_PV] = (uintptr_t) p;
141                         _gregs[PT_NIP] = (uintptr_t) p;
142                         break;
143                 }
144
145                 /* Get and set the PV from the parent Java method. */
146
147                 pv = md_codegen_get_pv_from_pc(ra);
148
149                 _gregs[REG_PV] = (uintptr_t) pv;
150
151                 /* Get the exception object. */
152
153                 p = builtin_retrieve_exception();
154
155                 assert(p != NULL);
156
157                 /* fall-through */
158
159         case EXCEPTION_HARDWARE_PATCHER:
160                 if (p == NULL)
161                         break;
162
163                 /* fall-through */
164                 
165         default:
166                 _gregs[REG_ITMP1_XPTR] = (uintptr_t) p;
167                 _gregs[REG_ITMP2_XPC]  = (uintptr_t) xpc;
168                 _gregs[PT_NIP]         = (uintptr_t) asm_handle_exception;
169         }
170 }
171
172
173 /* md_signal_handler_sigtrap ***************************************************
174
175    Signal handler for hardware-traps.
176
177 *******************************************************************************/
178
179 void md_signal_handler_sigtrap(int sig, siginfo_t *siginfo, void *_p)
180 {
181         ucontext_t     *_uc;
182         mcontext_t     *_mc;
183         unsigned long  *_gregs;
184         u1             *pv;
185         u1             *sp;
186         u1             *ra;
187         u1             *xpc;
188         u4              mcode;
189         int             s1;
190         intptr_t        val;
191         int             type;
192         void           *p;
193
194         _uc = (ucontext_t *) _p;
195
196 #if defined(__UCLIBC__)
197         _mc    = &(_uc->uc_mcontext);
198         _gregs = _mc->regs->gpr;
199 #else
200         _mc    = _uc->uc_mcontext.uc_regs;
201         _gregs = _mc->gregs;
202 #endif
203
204         pv  = (u1 *) _gregs[REG_PV];
205         sp  = (u1 *) _gregs[REG_SP];
206         ra  = (u1 *) _gregs[PT_LNK];                 /* this is correct for leafs */
207         xpc = (u1 *) _gregs[PT_NIP];
208
209         /* get exception-throwing instruction */
210
211         mcode = *((u4 *) xpc);
212
213         s1 = M_OP3_GET_A(mcode);
214
215         /* for now we only handle ArrayIndexOutOfBoundsException */
216
217         type = EXCEPTION_HARDWARE_ARRAYINDEXOUTOFBOUNDS;
218         val  = _gregs[s1];
219
220         /* Handle the type. */
221
222         p = signal_handle(type, val, pv, sp, ra, xpc, _p);
223
224         /* set registers */
225
226         _gregs[REG_ITMP1_XPTR] = (intptr_t) p;
227         _gregs[REG_ITMP2_XPC]  = (intptr_t) xpc;
228         _gregs[PT_NIP]         = (intptr_t) asm_handle_exception;
229 }
230
231
232 /* md_signal_handler_sigusr1 ***************************************************
233
234    Signal handler for suspending threads.
235
236 *******************************************************************************/
237
238 #if defined(ENABLE_THREADS) && defined(ENABLE_GC_CACAO)
239 void md_signal_handler_sigusr1(int sig, siginfo_t *siginfo, void *_p)
240 {
241         ucontext_t    *_uc;
242         mcontext_t    *_mc;
243         unsigned long *_gregs;
244         u1            *pc;
245         u1            *sp;
246
247         _uc = (ucontext_t *) _p;
248
249 #if defined(__UCLIBC__)
250         _mc    = &(_uc->uc_mcontext);
251         _gregs = _mc->regs->gpr;
252 #else
253         _mc    = _uc->uc_mcontext.uc_regs;
254         _gregs = _mc->gregs;
255 #endif
256
257         /* get the PC and SP for this thread */
258
259         pc = (u1 *) _gregs[PT_NIP];
260         sp = (u1 *) _gregs[REG_SP];
261
262         /* now suspend the current thread */
263
264         threads_suspend_ack(pc, sp);
265 }
266 #endif
267
268
269 /* md_signal_handler_sigusr2 ***************************************************
270
271    Signal handler for profiling sampling.
272
273 *******************************************************************************/
274
275 #if defined(ENABLE_THREADS)
276 void md_signal_handler_sigusr2(int sig, siginfo_t *siginfo, void *_p)
277 {
278         threadobject  *tobj;
279         ucontext_t    *_uc;
280         mcontext_t    *_mc;
281         unsigned long *_gregs;
282         u1            *pc;
283
284         tobj = THREADOBJECT;
285
286         _uc = (ucontext_t *) _p;
287
288 #if defined(__UCLIBC__)
289         _mc    = &(_uc->uc_mcontext);
290         _gregs = _mc->regs->gpr;
291 #else
292         _mc    = _uc->uc_mcontext.uc_regs;
293         _gregs = _mc->gregs;
294 #endif
295
296         pc = (u1 *) _gregs[PT_NIP];
297
298         tobj->pc = pc;
299 }
300 #endif
301
302
303 /* md_executionstate_read ******************************************************
304
305    Read the given context into an executionstate.
306
307 *******************************************************************************/
308
309 void md_executionstate_read(executionstate_t *es, void *context)
310 {
311         ucontext_t    *_uc;
312         mcontext_t    *_mc;
313         unsigned long *_gregs;
314         s4              i;
315
316         _uc = (ucontext_t *) context;
317
318 #if defined(__UCLIBC__)
319 #error Please port md_executionstate_read to __UCLIBC__
320 #else
321         _mc    = _uc->uc_mcontext.uc_regs;
322         _gregs = _mc->gregs;
323 #endif
324
325         /* read special registers */
326         es->pc = (u1 *) _gregs[PT_NIP];
327         es->sp = (u1 *) _gregs[REG_SP];
328         es->pv = (u1 *) _gregs[REG_PV];
329         es->ra = (u1 *) _gregs[PT_LNK];
330
331         /* read integer registers */
332         for (i = 0; i < INT_REG_CNT; i++)
333                 es->intregs[i] = _gregs[i];
334
335         /* read float registers */
336         /* Do not use the assignment operator '=', as the type of
337          * the _mc->fpregs[i] can cause invalid conversions. */
338
339         assert(sizeof(_mc->fpregs.fpregs) == sizeof(es->fltregs));
340         system_memcpy(&es->fltregs, &_mc->fpregs.fpregs, sizeof(_mc->fpregs.fpregs));
341 }
342
343
344 /* md_executionstate_write *****************************************************
345
346    Write the given executionstate back to the context.
347
348 *******************************************************************************/
349
350 void md_executionstate_write(executionstate_t *es, void *context)
351 {
352         ucontext_t    *_uc;
353         mcontext_t    *_mc;
354         unsigned long *_gregs;
355         s4              i;
356
357         _uc = (ucontext_t *) context;
358
359 #if defined(__UCLIBC__)
360 #error Please port md_executionstate_write to __UCLIBC__
361 #else
362         _mc    = _uc->uc_mcontext.uc_regs;
363         _gregs = _mc->gregs;
364 #endif
365
366         /* write integer registers */
367         for (i = 0; i < INT_REG_CNT; i++)
368                 _gregs[i] = es->intregs[i];
369
370         /* write float registers */
371         /* Do not use the assignment operator '=', as the type of
372          * the _mc->fpregs[i] can cause invalid conversions. */
373
374         assert(sizeof(_mc->fpregs.fpregs) == sizeof(es->fltregs));
375         system_memcpy(&_mc->fpregs.fpregs, &es->fltregs, sizeof(_mc->fpregs.fpregs));
376
377         /* write special registers */
378         _gregs[PT_NIP] = (ptrint) es->pc;
379         _gregs[REG_SP] = (ptrint) es->sp;
380         _gregs[REG_PV] = (ptrint) es->pv;
381         _gregs[PT_LNK] = (ptrint) es->ra;
382 }
383
384
385 /* md_critical_section_restart *************************************************
386
387    Search the critical sections tree for a matching section and set
388    the PC to the restart point, if necessary.
389
390 *******************************************************************************/
391
392 #if defined(ENABLE_THREADS)
393 void md_critical_section_restart(ucontext_t *_uc)
394 {
395         mcontext_t    *_mc;
396         unsigned long *_gregs;
397         u1            *pc;
398         u1            *npc;
399
400 #if defined(__UCLIBC__)
401         _mc    = &(_uc->uc_mcontext);
402         _gregs = _mc->regs->gpr;
403 #else
404         _mc    = _uc->uc_mcontext.uc_regs;
405         _gregs = _mc->gregs;
406 #endif
407
408         pc = (u1 *) _gregs[PT_NIP];
409
410         npc = critical_find_restart_point(pc);
411
412         if (npc != NULL)
413                 _gregs[PT_NIP] = (ptrint) npc;
414 }
415 #endif
416
417
418 /*
419  * These are local overrides for various environment variables in Emacs.
420  * Please do not remove this and leave it at the end of the file, where
421  * Emacs will automagically detect them.
422  * ---------------------------------------------------------------------
423  * Local variables:
424  * mode: c
425  * indent-tabs-mode: t
426  * c-basic-offset: 4
427  * tab-width: 4
428  * End:
429  */