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