b2bc5c433f05c0e7b981d11400c52e7cd3840791
[cacao.git] / src / vm / jit / powerpc / darwin / md-os.c
1 /* src/vm/jit/powerpc/darwin/md-os.c - machine dependent PowerPC Darwin 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 <signal.h>
34 #include <stdint.h>
35 #include <ucontext.h>
36
37 #include "vm/types.h"
38
39 #include "vm/jit/powerpc/codegen.h"
40 #include "vm/jit/powerpc/darwin/md-abi.h"
41
42 #if defined(ENABLE_THREADS)
43 # include "threads/native/threads.h"
44 #endif
45
46 #include "vm/exceptions.h"
47 #include "vm/global.h"
48 #include "vm/signallocal.h"
49 #include "vm/stringlocal.h"
50
51 #include "vm/jit/asmpart.h"
52 #include "vm/jit/stacktrace.h"
53
54
55 /* md_signal_handler_sigsegv ***************************************************
56
57    NullPointerException signal handler for hardware null pointer
58    check.
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         ppc_thread_state_t *_ss;
68         ptrint             *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         _mc = _uc->uc_mcontext;
84         _ss = &_mc->ss;
85
86         /* immitate a gregs array */
87
88         gregs = &_ss->r0;
89
90         /* get register values */
91
92         pv  = (u1 *) _ss->r13;
93         sp  = (u1 *) _ss->r1;
94         ra  = (u1 *) _ss->lr;                    /* this is correct for leafs */
95         xpc = (u1 *) _ss->srr0;
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                 _ss->r11  = (intptr_t) p;
141                 _ss->r12  = (intptr_t) xpc;
142                 _ss->srr0 = (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         ppc_thread_state_t *_ss;
159         ptrint             *gregs;
160         u1                 *pv;
161         u1                 *sp;
162         u1                 *ra;
163         u1                 *xpc;
164         u4                  mcode;
165         int                 s1;
166         intptr_t            val;
167         int                 type;
168         void               *p;
169
170         _uc = (ucontext_t *) _p;
171         _mc = _uc->uc_mcontext;
172         _ss = &_mc->ss;
173
174         /* immitate a gregs array */
175
176         gregs = &_ss->r0;
177
178         /* get register values */
179
180         pv  = (u1 *) _ss->r13;
181         sp  = (u1 *) _ss->r1;
182         ra  = (u1 *) _ss->lr;                    /* this is correct for leafs */
183         xpc = (u1 *) _ss->srr0;
184
185         /* get exception-throwing instruction */
186
187         mcode = *((u4 *) xpc);
188
189         s1 = M_OP3_GET_A(mcode);
190
191         /* for now we only handle ArrayIndexOutOfBoundsException */
192
193         type = EXCEPTION_HARDWARE_ARRAYINDEXOUTOFBOUNDS;
194         val  = gregs[s1];
195
196         /* create stackframeinfo */
197
198         stacktrace_create_extern_stackframeinfo(&sfi, pv, sp, ra, xpc);
199
200         /* Handle the type. */
201
202         p = signal_handle(xpc, type, val);
203
204         /* remove stackframeinfo */
205
206         stacktrace_remove_stackframeinfo(&sfi);
207
208         /* set registers */
209
210         _ss->r11  = (intptr_t) p;
211         _ss->r12  = (intptr_t) xpc;
212         _ss->srr0 = (intptr_t) asm_handle_exception;
213 }
214
215
216 /* md_signal_handler_sigusr2 ***************************************************
217
218    Signal handler for profiling sampling.
219
220 *******************************************************************************/
221
222 void md_signal_handler_sigusr2(int sig, siginfo_t *siginfo, void *_p)
223 {
224         threadobject       *t;
225         ucontext_t         *_uc;
226         mcontext_t          _mc;
227         ppc_thread_state_t *_ss;
228         u1                 *pc;
229
230         t = THREADOBJECT;
231
232         _uc = (ucontext_t *) _p;
233         _mc = _uc->uc_mcontext;
234         _ss = &_mc->ss;
235
236         pc = (u1 *) _ss->srr0;
237
238         t->pc = pc;
239 }
240
241
242 /* md_critical_section_restart *************************************************
243
244    Search the critical sections tree for a matching section and set
245    the PC to the restart point, if necessary.
246
247 *******************************************************************************/
248
249 #if defined(ENABLE_THREADS)
250 void md_critical_section_restart(ucontext_t *_uc)
251 {
252         mcontext_t          _mc;
253         ppc_thread_state_t *_ss;
254         u1                 *pc;
255         u1                 *npc;
256
257         _mc = _uc->uc_mcontext;
258         _ss = &_mc->ss;
259
260         pc = (u1 *) _ss->srr0;
261
262         npc = critical_find_restart_point(pc);
263
264         if (npc != NULL)
265                 _ss->srr0 = (ptrint) npc;
266 }
267 #endif
268
269
270 /*
271  * These are local overrides for various environment variables in Emacs.
272  * Please do not remove this and leave it at the end of the file, where
273  * Emacs will automagically detect them.
274  * ---------------------------------------------------------------------
275  * Local variables:
276  * mode: c
277  * indent-tabs-mode: t
278  * c-basic-offset: 4
279  * tab-width: 4
280  * End:
281  */