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