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