* src/vm/jit/trap.c: Moved to C++
[cacao.git] / src / vm / jit / i386 / darwin / md-os.c
1 /* src/vm/jit/i386/darwin/md-os.c - machine dependent i386 Darwin 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 <signal.h>
30 #include <stdint.h>
31 #include <ucontext.h>
32
33 #include "vm/types.h"
34
35 #include "vm/jit/i386/codegen.h"
36 #include "vm/jit/i386/md.h"
37
38 #include "threads/thread.hpp"
39
40 #include "vm/jit/builtin.hpp"
41 #include "vm/global.h"
42 #include "vm/signallocal.hpp"
43
44 #include "vm/jit/asmpart.h"
45 #include "vm/jit/executionstate.h"
46 #include "vm/jit/stacktrace.hpp"
47 #include "vm/jit/trap.hpp"
48
49 #include "vm/jit/i386/codegen.h"
50
51
52 /* md_signal_handler_sigsegv ***************************************************
53
54    Signal handler for hardware exceptions.
55
56 *******************************************************************************/
57
58 void md_signal_handler_sigsegv(int sig, siginfo_t *siginfo, void *_p)
59 {
60         ucontext_t          *_uc;
61         mcontext_t           _mc;
62         u1                  *pv;
63         i386_thread_state_t *_ss;
64         u1                  *sp;
65         u1                  *ra;
66         u1                  *xpc;
67         u1                   opc;
68         u1                   mod;
69         u1                   rm;
70         int                  d;
71         int32_t              disp;
72         intptr_t             val;
73         int                  type;
74         void                *p;
75
76         _uc = (ucontext_t *) _p;
77         _mc = _uc->uc_mcontext;
78         _ss = &_mc->__ss;
79
80         pv  = NULL;                 /* is resolved during stackframeinfo creation */
81         sp  = (u1 *) _ss->__esp;
82         xpc = (u1 *) _ss->__eip;
83         ra  = xpc;                              /* return address is equal to XPC */
84
85         /* get exception-throwing instruction */
86
87         opc = M_ALD_MEM_GET_OPC(xpc);
88         mod = M_ALD_MEM_GET_MOD(xpc);
89         rm  = M_ALD_MEM_GET_RM(xpc);
90
91         /* for values see emit_mov_mem_reg and emit_mem */
92
93         if ((opc == 0x8b) && (mod == 0) && (rm == 5)) {
94                 /* this was a hardware-exception */
95
96                 d    = M_ALD_MEM_GET_REG(xpc);
97                 disp = M_ALD_MEM_GET_DISP(xpc);
98
99                 /* we use the exception type as load displacement */
100
101                 type = disp;
102
103                 val = (d == 0) ? _ss->__eax :
104                         ((d == 1) ? _ss->__ecx :
105                         ((d == 2) ? _ss->__edx :
106                         ((d == 3) ? _ss->__ebx :
107                         ((d == 4) ? _ss->__esp :
108                         ((d == 5) ? _ss->__ebp :
109                         ((d == 6) ? _ss->__esi : _ss->__edi))))));
110
111                 if (type == TRAP_COMPILER) {
112                         /* The PV from the compiler stub is equal to the XPC. */
113
114                         pv = xpc;
115
116                         /* We use a framesize of zero here because the call pushed
117                            the return addres onto the stack. */
118
119                         ra = md_stacktrace_get_returnaddress(sp, 0);
120
121                         /* Skip the RA on the stack. */
122
123                         sp = sp + 1 * SIZEOF_VOID_P;
124
125                         /* The XPC is the RA minus 2, because the RA points to the
126                            instruction after the call. */
127
128                         xpc = ra - 2;
129                 } 
130         }
131         else {
132                 /* this was a normal NPE */
133
134                 type = TRAP_NullPointerException;
135         }
136
137         /* Handle the trap. */
138
139         p = trap_handle(type, val, pv, sp, ra, xpc, _p);
140
141         /* Set registers. */
142
143         if (type == TRAP_COMPILER) {
144                 if (p == NULL) {
145                         _ss->__esp = (uintptr_t) sp;  /* Remove RA from stack. */
146                 }
147         }
148 }
149
150
151 /* md_signal_handler_sigfpe ****************************************************
152
153    ArithmeticException signal handler for hardware divide by zero
154    check.
155
156 *******************************************************************************/
157
158 void md_signal_handler_sigfpe(int sig, siginfo_t *siginfo, void *_p)
159 {
160         ucontext_t          *_uc;
161         mcontext_t           _mc;
162         u1                  *pv;
163         i386_thread_state_t *_ss;
164         u1                  *sp;
165         u1                  *ra;
166         u1                  *xpc;
167         int                  type;
168         intptr_t             val;
169
170
171         _uc = (ucontext_t *) _p;
172         _mc = _uc->uc_mcontext;
173         _ss = &_mc->__ss;
174
175         pv  = NULL;                 /* is resolved during stackframeinfo creation */
176         sp  = (u1 *) _ss->__esp;
177         xpc = (u1 *) _ss->__eip;
178         ra  = xpc;                          /* return address is equal to xpc     */
179
180         /* This is an ArithmeticException */
181
182         type = TRAP_ArithmeticException;
183         val  = 0;
184
185         /* Handle the trap. */
186
187         trap_handle(type, val, pv, sp, ra, xpc, _p);
188 }
189
190
191 /* md_signal_handler_sigusr2 ***************************************************
192
193    Signal handler for profiling sampling.
194
195 *******************************************************************************/
196
197 void md_signal_handler_sigusr2(int sig, siginfo_t *siginfo, void *_p)
198 {
199         threadobject        *t;
200         ucontext_t          *_uc;
201         mcontext_t           _mc;
202         i386_thread_state_t *_ss;
203         u1                  *pc;
204
205         t = THREADOBJECT;
206
207         _uc = (ucontext_t *) _p;
208         _mc = _uc->uc_mcontext;
209         _ss = &_mc->__ss;
210
211         pc = (u1 *) _ss->__eip;
212
213         t->pc = pc;
214 }
215
216
217 /* md_signal_handler_sigill ****************************************************
218
219    Signal handler for hardware patcher traps (ud2).
220
221 *******************************************************************************/
222
223 void md_signal_handler_sigill(int sig, siginfo_t *siginfo, void *_p)
224 {
225         ucontext_t          *_uc;
226         mcontext_t           _mc;
227         u1                  *pv;
228         i386_thread_state_t *_ss;
229         u1                  *sp;
230         u1                  *ra;
231         u1                  *xpc;
232         int                  type;
233         intptr_t             val;
234
235
236         _uc = (ucontext_t *) _p;
237         _mc = _uc->uc_mcontext;
238         _ss = &_mc->__ss;
239
240         pv  = NULL;                 /* is resolved during stackframeinfo creation */
241         sp  = (u1 *) _ss->__esp;
242         xpc = (u1 *) _ss->__eip;
243         ra  = xpc;                          /* return address is equal to xpc     */
244
245         type = TRAP_PATCHER;
246         val  = 0;
247
248         /* Handle the trap. */
249
250         trap_handle(type, val, pv, sp, ra, xpc, _p);
251 }
252
253 /* md_executionstate_read ******************************************************
254
255    Read the given context into an executionstate.
256
257 *******************************************************************************/
258
259 void md_executionstate_read(executionstate_t *es, void *context)
260 {
261         ucontext_t          *_uc;
262         mcontext_t           _mc; 
263         i386_thread_state_t *_ss;
264         int                  i;
265
266         _uc = (ucontext_t *) context;
267         _mc = _uc->uc_mcontext;
268         _ss = &_mc->__ss;
269
270         /* read special registers */
271         es->pc = (u1 *) _ss->__eip;
272         es->sp = (u1 *) _ss->__esp;
273         es->pv = NULL;                   /* pv must be looked up via AVL tree */
274
275         /* read integer registers */
276         for (i = 0; i < INT_REG_CNT; i++)
277                 es->intregs[i] = (i == 0) ? _ss->__eax :
278                         ((i == 1) ? _ss->__ecx :
279                         ((i == 2) ? _ss->__edx :
280                         ((i == 3) ? _ss->__ebx :
281                         ((i == 4) ? _ss->__esp :
282                         ((i == 5) ? _ss->__ebp :
283                         ((i == 6) ? _ss->__esi : _ss->__edi))))));
284
285         /* read float registers */
286         for (i = 0; i < FLT_REG_CNT; i++)
287                 es->fltregs[i] = 0xdeadbeefdeadbeefULL;
288 }
289
290
291 /* md_executionstate_write *****************************************************
292
293    Write the given executionstate back to the context.
294
295 *******************************************************************************/
296
297 void md_executionstate_write(executionstate_t *es, void *context)
298 {
299         ucontext_t*          _uc;
300         mcontext_t           _mc;
301         i386_thread_state_t* _ss;
302         int                  i;
303
304         _uc = (ucontext_t *) context;
305         _mc = _uc->uc_mcontext;
306         _ss = &_mc->__ss;
307
308         /* write integer registers */
309         for (i = 0; i < INT_REG_CNT; i++)
310                 *((i == 0) ? &_ss->__eax :
311                  ((i == 1) ? &_ss->__ecx :
312                  ((i == 2) ? &_ss->__edx :
313                  ((i == 3) ? &_ss->__ebx :
314                  ((i == 4) ? &_ss->__esp :
315                  ((i == 5) ? &_ss->__ebp :
316                  ((i == 6) ? &_ss->__esi : &_ss->__edi))))))) = es->intregs[i];
317
318         /* write special registers */
319         _ss->__eip = (ptrint) es->pc;
320         _ss->__esp = (ptrint) es->sp;
321 }
322
323
324 /*
325  * These are local overrides for various environment variables in Emacs.
326  * Please do not remove this and leave it at the end of the file, where
327  * Emacs will automagically detect them.
328  * ---------------------------------------------------------------------
329  * Local variables:
330  * mode: c
331  * indent-tabs-mode: t
332  * c-basic-offset: 4
333  * tab-width: 4
334  * End:
335  */