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