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