* src/vm/exceptions.c (exceptions_new_hardware_exception): Do not create sfi.
[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    $Id: md-os.c 8243 2007-07-31 08:57:54Z michi $
26
27 */
28
29
30 #include "config.h"
31
32 #include <assert.h>
33 #include <ucontext.h>
34
35 #include "vm/types.h"
36
37 #include "vm/jit/powerpc/codegen.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/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         stackframeinfo     sfi;
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         s4                 s1;
74         s4                 disp;
75         s4                 d;
76         ptrint             addr;
77         ptrint             val;
78         s4                 type;
79         java_objectheader *e;
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         else {
114                 /* This is a normal NPE: addr must be NULL and the NPE-type
115                    define is 0. */
116
117                 addr = _gregs[s1];
118                 type = EXCEPTION_HARDWARE_NULLPOINTER;
119
120                 if (addr != 0)
121                         vm_abort("md_signal_handler_sigsegv: faulting address is not NULL: addr=%p", addr);
122         }
123
124         /* create stackframeinfo */
125
126         stacktrace_create_extern_stackframeinfo(&sfi, pv, sp, ra, xpc);
127
128         /* generate appropriate exception */
129
130         e = exceptions_new_hardware_exception(xpc, type, val);
131
132         /* remove stackframeinfo */
133
134         stacktrace_remove_stackframeinfo(&sfi);
135
136         /* set registers (only if exception object ready) */
137
138         if (e != NULL) {
139                 _gregs[REG_ITMP1_XPTR] = (ptrint) e;
140                 _gregs[REG_ITMP2_XPC]  = (ptrint) xpc;
141                 _gregs[PT_NIP]         = (ptrint) asm_handle_exception;
142         }
143 }
144
145
146 /* md_signal_handler_sigtrap ***************************************************
147
148    Signal handler for hardware-traps.
149
150 *******************************************************************************/
151
152 void md_signal_handler_sigtrap(int sig, siginfo_t *siginfo, void *_p)
153 {
154         stackframeinfo     sfi;
155         ucontext_t        *_uc;
156         mcontext_t        *_mc;
157         unsigned long     *_gregs;
158         u1                *pv;
159         u1                *sp;
160         u1                *ra;
161         u1                *xpc;
162         u4                 mcode;
163         s4                 s1;
164         ptrint             val;
165         s4                 type;
166         java_objectheader *e;
167
168         _uc = (ucontext_t *) _p;
169
170 #if defined(__UCLIBC__)
171         _mc    = &(_uc->uc_mcontext);
172         _gregs = _mc->regs->gpr;
173 #else
174         _mc    = _uc->uc_mcontext.uc_regs;
175         _gregs = _mc->gregs;
176 #endif
177
178         pv  = (u1 *) _gregs[REG_PV];
179         sp  = (u1 *) _gregs[REG_SP];
180         ra  = (u1 *) _gregs[PT_LNK];                 /* this is correct for leafs */
181         xpc = (u1 *) _gregs[PT_NIP];
182
183         /* get exception-throwing instruction */
184
185         mcode = *((u4 *) xpc);
186
187         s1 = M_OP3_GET_A(mcode);
188
189         /* for now we only handle ArrayIndexOutOfBoundsException */
190
191         type = EXCEPTION_HARDWARE_ARRAYINDEXOUTOFBOUNDS;
192         val  = _gregs[s1];
193
194         /* create stackframeinfo */
195
196         stacktrace_create_extern_stackframeinfo(&sfi, pv, sp, ra, xpc);
197
198         /* generate appropriate exception */
199
200         e = exceptions_new_hardware_exception(xpc, type, val);
201
202         /* remove stackframeinfo */
203
204         stacktrace_remove_stackframeinfo(&sfi);
205
206         /* set registers */
207
208         _gregs[REG_ITMP1_XPTR] = (ptrint) e;
209         _gregs[REG_ITMP2_XPC]  = (ptrint) xpc;
210         _gregs[PT_NIP]         = (ptrint) asm_handle_exception;
211 }
212
213
214 /* md_signal_handler_sigusr2 ***************************************************
215
216    Signal handler for profiling sampling.
217
218 *******************************************************************************/
219
220 #if defined(ENABLE_THREADS)
221 void md_signal_handler_sigusr2(int sig, siginfo_t *siginfo, void *_p)
222 {
223         threadobject  *tobj;
224         ucontext_t    *_uc;
225         mcontext_t    *_mc;
226         unsigned long *_gregs;
227         u1            *pc;
228
229         tobj = THREADOBJECT;
230
231         _uc = (ucontext_t *) _p;
232
233 #if defined(__UCLIBC__)
234         _mc    = &(_uc->uc_mcontext);
235         _gregs = _mc->regs->gpr;
236 #else
237         _mc    = _uc->uc_mcontext.uc_regs;
238         _gregs = _mc->gregs;
239 #endif
240
241         pc = (u1 *) _gregs[PT_NIP];
242
243         tobj->pc = pc;
244 }
245 #endif
246
247
248 /* md_critical_section_restart *************************************************
249
250    Search the critical sections tree for a matching section and set
251    the PC to the restart point, if necessary.
252
253 *******************************************************************************/
254
255 #if defined(ENABLE_THREADS)
256 void md_critical_section_restart(ucontext_t *_uc)
257 {
258         mcontext_t    *_mc;
259         unsigned long *_gregs;
260         u1            *pc;
261         u1            *npc;
262
263 #if defined(__UCLIBC__)
264         _mc    = &(_uc->uc_mcontext);
265         _gregs = _mc->regs->gpr;
266 #else
267         _mc    = _uc->uc_mcontext.uc_regs;
268         _gregs = _mc->gregs;
269 #endif
270
271         pc = (u1 *) _gregs[PT_NIP];
272
273         npc = critical_find_restart_point(pc);
274
275         if (npc != NULL)
276                 _gregs[PT_NIP] = (ptrint) npc;
277 }
278 #endif
279
280
281 /*
282  * These are local overrides for various environment variables in Emacs.
283  * Please do not remove this and leave it at the end of the file, where
284  * Emacs will automagically detect them.
285  * ---------------------------------------------------------------------
286  * Local variables:
287  * mode: c
288  * indent-tabs-mode: t
289  * c-basic-offset: 4
290  * tab-width: 4
291  * End:
292  */