* Removed all Id tags.
[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 */
26
27
28 #include "config.h"
29
30 #include <assert.h>
31 #include <signal.h>
32 #include <stdint.h>
33 #include <ucontext.h>
34
35 #include "vm/types.h"
36
37 #include "vm/jit/powerpc/codegen.h"
38 #include "vm/jit/powerpc/darwin/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/global.h"
46 #include "vm/signallocal.h"
47 #include "vm/stringlocal.h"
48
49 #include "vm/jit/asmpart.h"
50 #include "vm/jit/stacktrace.h"
51
52
53 /* md_signal_handler_sigsegv ***************************************************
54
55    NullPointerException signal handler for hardware null pointer
56    check.
57
58 *******************************************************************************/
59
60 void md_signal_handler_sigsegv(int sig, siginfo_t *siginfo, void *_p)
61 {
62         stackframeinfo      sfi;
63         ucontext_t         *_uc;
64         mcontext_t          _mc;
65         ppc_thread_state_t *_ss;
66         ptrint             *gregs;
67         u1                 *pv;
68         u1                 *sp;
69         u1                 *ra;
70         u1                 *xpc;
71         u4                  mcode;
72         int                 s1;
73         int16_t             disp;
74         int                 d;
75         intptr_t            addr;
76         intptr_t            val;
77         int                 type;
78         void               *p;
79
80         _uc = (ucontext_t *) _p;
81         _mc = _uc->uc_mcontext;
82         _ss = &_mc->ss;
83
84         /* immitate a gregs array */
85
86         gregs = &_ss->r0;
87
88         /* get register values */
89
90         pv  = (u1 *) _ss->r13;
91         sp  = (u1 *) _ss->r1;
92         ra  = (u1 *) _ss->lr;                    /* this is correct for leafs */
93         xpc = (u1 *) _ss->srr0;
94
95         /* get exception-throwing instruction */
96
97         mcode = *((u4 *) xpc);
98
99         s1   = M_INSTR_OP2_IMM_A(mcode);
100         disp = M_INSTR_OP2_IMM_I(mcode);
101         d    = M_INSTR_OP2_IMM_D(mcode);
102
103         val  = gregs[d];
104
105         /* check for special-load */
106
107         if (s1 == REG_ZERO) {
108                 /* we use the exception type as load displacement */
109
110                 type = disp;
111         }
112         else {
113                 /* This is a normal NPE: addr must be NULL and the NPE-type
114                    define is 0. */
115
116                 addr = gregs[s1];
117                 type = EXCEPTION_HARDWARE_NULLPOINTER;
118
119                 if (addr != 0)
120                         vm_abort("md_signal_handler_sigsegv: faulting address is not NULL: addr=%p", addr);
121         }
122
123         /* create stackframeinfo */
124
125         stacktrace_create_extern_stackframeinfo(&sfi, pv, sp, ra, xpc);
126
127         /* Handle the type. */
128
129         p = signal_handle(xpc, type, val);
130
131         /* remove stackframeinfo */
132
133         stacktrace_remove_stackframeinfo(&sfi);
134
135         /* set registers (only if exception object ready) */
136
137         if (p != NULL) {
138                 _ss->r11  = (intptr_t) p;
139                 _ss->r12  = (intptr_t) xpc;
140                 _ss->srr0 = (intptr_t) asm_handle_exception;
141         }
142 }
143
144
145 /* md_signal_handler_sigtrap ***************************************************
146
147    Signal handler for hardware-traps.
148
149 *******************************************************************************/
150
151 void md_signal_handler_sigtrap(int sig, siginfo_t *siginfo, void *_p)
152 {
153         stackframeinfo      sfi;
154         ucontext_t         *_uc;
155         mcontext_t          _mc;
156         ppc_thread_state_t *_ss;
157         ptrint             *gregs;
158         u1                 *pv;
159         u1                 *sp;
160         u1                 *ra;
161         u1                 *xpc;
162         u4                  mcode;
163         int                 s1;
164         intptr_t            val;
165         int                 type;
166         void               *p;
167
168         _uc = (ucontext_t *) _p;
169         _mc = _uc->uc_mcontext;
170         _ss = &_mc->ss;
171
172         /* immitate a gregs array */
173
174         gregs = &_ss->r0;
175
176         /* get register values */
177
178         pv  = (u1 *) _ss->r13;
179         sp  = (u1 *) _ss->r1;
180         ra  = (u1 *) _ss->lr;                    /* this is correct for leafs */
181         xpc = (u1 *) _ss->srr0;
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         /* Handle the type. */
199
200         p = signal_handle(xpc, type, val);
201
202         /* remove stackframeinfo */
203
204         stacktrace_remove_stackframeinfo(&sfi);
205
206         /* set registers */
207
208         _ss->r11  = (intptr_t) p;
209         _ss->r12  = (intptr_t) xpc;
210         _ss->srr0 = (intptr_t) asm_handle_exception;
211 }
212
213
214 /* md_signal_handler_sigusr2 ***************************************************
215
216    Signal handler for profiling sampling.
217
218 *******************************************************************************/
219
220 void md_signal_handler_sigusr2(int sig, siginfo_t *siginfo, void *_p)
221 {
222         threadobject       *t;
223         ucontext_t         *_uc;
224         mcontext_t          _mc;
225         ppc_thread_state_t *_ss;
226         u1                 *pc;
227
228         t = THREADOBJECT;
229
230         _uc = (ucontext_t *) _p;
231         _mc = _uc->uc_mcontext;
232         _ss = &_mc->ss;
233
234         pc = (u1 *) _ss->srr0;
235
236         t->pc = pc;
237 }
238
239
240 /* md_critical_section_restart *************************************************
241
242    Search the critical sections tree for a matching section and set
243    the PC to the restart point, if necessary.
244
245 *******************************************************************************/
246
247 #if defined(ENABLE_THREADS)
248 void md_critical_section_restart(ucontext_t *_uc)
249 {
250         mcontext_t          _mc;
251         ppc_thread_state_t *_ss;
252         u1                 *pc;
253         u1                 *npc;
254
255         _mc = _uc->uc_mcontext;
256         _ss = &_mc->ss;
257
258         pc = (u1 *) _ss->srr0;
259
260         npc = critical_find_restart_point(pc);
261
262         if (npc != NULL)
263                 _ss->srr0 = (ptrint) npc;
264 }
265 #endif
266
267
268 /*
269  * These are local overrides for various environment variables in Emacs.
270  * Please do not remove this and leave it at the end of the file, where
271  * Emacs will automagically detect them.
272  * ---------------------------------------------------------------------
273  * Local variables:
274  * mode: c
275  * indent-tabs-mode: t
276  * c-basic-offset: 4
277  * tab-width: 4
278  * End:
279  */