* src/vm/jit/replace.h (executionstate_t): Added 'ra' field for return
[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 R. Grafl, A. Krall, C. Kruegel,
4    C. Oates, R. Obermaisser, M. Platter, M. Probst, S. Ring,
5    E. Steiner, C. Thalinger, D. Thuernbeck, P. Tomsich, C. Ullrich,
6    J. Wenninger, Institut f. Computersprachen - TU Wien
7
8    This file is part of CACAO.
9
10    This program is free software; you can redistribute it and/or
11    modify it under the terms of the GNU General Public License as
12    published by the Free Software Foundation; either version 2, or (at
13    your option) any later version.
14
15    This program is distributed in the hope that it will be useful, but
16    WITHOUT ANY WARRANTY; without even the implied warranty of
17    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18    General Public License for more details.
19
20    You should have received a copy of the GNU General Public License
21    along with this program; if not, write to the Free Software
22    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
23    02110-1301, USA.
24
25 */
26
27
28 #include "config.h"
29
30 #include <assert.h>
31 #include <stdint.h>
32 #include <ucontext.h>
33
34 #include "vm/types.h"
35
36 #include "vm/jit/powerpc/codegen.h"
37 #include "vm/jit/powerpc/md.h"
38 #include "vm/jit/powerpc/linux/md-abi.h"
39
40 #if defined(ENABLE_THREADS)
41 # include "threads/native/threads.h"
42 #endif
43
44 #include "vm/builtin.h"
45 #include "vm/exceptions.h"
46 #include "vm/signallocal.h"
47 #include "vm/stringlocal.h"
48 #include "vm/jit/asmpart.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
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_replace_executionstate_read **********************************************
304
305    Read the given context into an executionstate for Replacement.
306
307 *******************************************************************************/
308
309 #if defined(ENABLE_REPLACEMENT)
310 void md_replace_executionstate_read(executionstate_t *es, void *context)
311 {
312         ucontext_t    *_uc;
313         mcontext_t    *_mc;
314         unsigned long *_gregs;
315         s4              i;
316
317         _uc = (ucontext_t *) context;
318
319 #if defined(__UCLIBC__)
320 #error Please port md_replace_executionstate_read to __UCLIBC__
321 #else
322         _mc    = _uc->uc_mcontext.uc_regs;
323         _gregs = _mc->gregs;
324 #endif
325
326         /* read special registers */
327         es->pc = (u1 *) _gregs[PT_NIP];
328         es->sp = (u1 *) _gregs[REG_SP];
329         es->pv = (u1 *) _gregs[REG_PV];
330         es->ra = (u1 *) _gregs[PT_LNK];
331
332         /* read integer registers */
333         for (i = 0; i < INT_REG_CNT; i++)
334                 es->intregs[i] = _gregs[i];
335
336         /* read float registers */
337         /* Do not use the assignment operator '=', as the type of
338          * the _mc->fpregs[i] can cause invalid conversions. */
339
340         assert(sizeof(_mc->fpregs.fpregs) == sizeof(es->fltregs));
341         memcpy(&es->fltregs, &_mc->fpregs.fpregs, sizeof(_mc->fpregs.fpregs));
342 }
343 #endif
344
345
346 /* md_replace_executionstate_write *********************************************
347
348    Write the given executionstate back to the context for Replacement.
349
350 *******************************************************************************/
351
352 #if defined(ENABLE_REPLACEMENT)
353 void md_replace_executionstate_write(executionstate_t *es, void *context)
354 {
355         ucontext_t    *_uc;
356         mcontext_t    *_mc;
357         unsigned long *_gregs;
358         s4              i;
359
360         _uc = (ucontext_t *) context;
361
362 #if defined(__UCLIBC__)
363 #error Please port md_replace_executionstate_read to __UCLIBC__
364 #else
365         _mc    = _uc->uc_mcontext.uc_regs;
366         _gregs = _mc->gregs;
367 #endif
368
369         /* write integer registers */
370         for (i = 0; i < INT_REG_CNT; i++)
371                 _gregs[i] = es->intregs[i];
372
373         /* write float registers */
374         /* Do not use the assignment operator '=', as the type of
375          * the _mc->fpregs[i] can cause invalid conversions. */
376
377         assert(sizeof(_mc->fpregs.fpregs) == sizeof(es->fltregs));
378         memcpy(&_mc->fpregs.fpregs, &es->fltregs, sizeof(_mc->fpregs.fpregs));
379
380         /* write special registers */
381         _gregs[PT_NIP] = (ptrint) es->pc;
382         _gregs[REG_SP] = (ptrint) es->sp;
383         _gregs[REG_PV] = (ptrint) es->pv;
384         _gregs[PT_LNK] = (ptrint) es->ra;
385 }
386 #endif
387
388
389 /* md_critical_section_restart *************************************************
390
391    Search the critical sections tree for a matching section and set
392    the PC to the restart point, if necessary.
393
394 *******************************************************************************/
395
396 #if defined(ENABLE_THREADS)
397 void md_critical_section_restart(ucontext_t *_uc)
398 {
399         mcontext_t    *_mc;
400         unsigned long *_gregs;
401         u1            *pc;
402         u1            *npc;
403
404 #if defined(__UCLIBC__)
405         _mc    = &(_uc->uc_mcontext);
406         _gregs = _mc->regs->gpr;
407 #else
408         _mc    = _uc->uc_mcontext.uc_regs;
409         _gregs = _mc->gregs;
410 #endif
411
412         pc = (u1 *) _gregs[PT_NIP];
413
414         npc = critical_find_restart_point(pc);
415
416         if (npc != NULL)
417                 _gregs[PT_NIP] = (ptrint) npc;
418 }
419 #endif
420
421
422 /*
423  * These are local overrides for various environment variables in Emacs.
424  * Please do not remove this and leave it at the end of the file, where
425  * Emacs will automagically detect them.
426  * ---------------------------------------------------------------------
427  * Local variables:
428  * mode: c
429  * indent-tabs-mode: t
430  * c-basic-offset: 4
431  * tab-width: 4
432  * End:
433  */