* Merged executionstate branch.
[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.h"
39
40 #include "vm/builtin.h"
41 #include "vm/exceptions.h"
42 #include "vm/global.h"
43 #include "vm/signallocal.h"
44 #include "vm/stringlocal.h"
45
46 #include "vm/jit/asmpart.h"
47 #include "vm/jit/executionstate.h"
48 #include "vm/jit/stacktrace.h"
49
50 #include "vm/jit/i386/codegen.h"
51
52
53 /* md_signal_handler_sigsegv ***************************************************
54
55    Signal handler for hardware exceptions.
56
57 *******************************************************************************/
58
59 void md_signal_handler_sigsegv(int sig, siginfo_t *siginfo, void *_p)
60 {
61         ucontext_t          *_uc;
62         mcontext_t           _mc;
63         u1                  *pv;
64         i386_thread_state_t *_ss;
65         u1                  *sp;
66         u1                  *ra;
67         u1                  *xpc;
68     u1                   opc;
69     u1                   mod;
70     u1                   rm;
71     int                  d;
72     int32_t              disp;
73         intptr_t             val;
74     int                  type;
75     void                *p;
76         java_object_t       *o;
77
78         _uc = (ucontext_t *) _p;
79         _mc = _uc->uc_mcontext;
80         _ss = &_mc->ss;
81
82     pv  = NULL;                 /* is resolved during stackframeinfo creation */
83         sp  = (u1 *) _ss->esp;
84         xpc = (u1 *) _ss->eip;
85     ra  = xpc;                              /* return address is equal to XPC */
86
87     /* get exception-throwing instruction */
88
89     opc = M_ALD_MEM_GET_OPC(xpc);
90     mod = M_ALD_MEM_GET_MOD(xpc);
91     rm  = M_ALD_MEM_GET_RM(xpc);
92
93     /* for values see emit_mov_mem_reg and emit_mem */
94
95     if ((opc == 0x8b) && (mod == 0) && (rm == 5)) {
96         /* this was a hardware-exception */
97
98         d    = M_ALD_MEM_GET_REG(xpc);
99         disp = M_ALD_MEM_GET_DISP(xpc);
100
101         /* we use the exception type as load displacement */
102
103         type = disp;
104
105         val = (d == 0) ? _ss->eax :
106             ((d == 1) ? _ss->ecx :
107             ((d == 2) ? _ss->edx :
108             ((d == 3) ? _ss->ebx :
109             ((d == 4) ? _ss->esp :
110             ((d == 5) ? _ss->ebp :
111             ((d == 6) ? _ss->esi : _ss->edi))))));
112
113                 if (type == EXCEPTION_HARDWARE_COMPILER) {
114                         /* The PV from the compiler stub is equal to the XPC. */
115
116                         pv = xpc;
117
118                         /* We use a framesize of zero here because the call pushed
119                            the return addres onto the stack. */
120
121                         ra = md_stacktrace_get_returnaddress(sp, 0);
122
123                         /* Skip the RA on the stack. */
124
125                         sp = sp + 1 * SIZEOF_VOID_P;
126
127                         /* The XPC is the RA minus 2, because the RA points to the
128                            instruction after the call. */
129
130                         xpc = ra - 2;
131                 } 
132     }
133     else {
134         /* this was a normal NPE */
135
136         type = EXCEPTION_HARDWARE_NULLPOINTER;
137     }
138
139         /* Handle the type. */
140
141         p = signal_handle(type, val, pv, sp, ra, xpc, _p);
142
143         /* Set registers. */
144
145         if (type == EXCEPTION_HARDWARE_COMPILER) {
146                 if (p == NULL) { 
147                         o = builtin_retrieve_exception();
148
149                         _ss->esp = (uintptr_t) sp;    /* Remove RA from stack. */
150
151                         _ss->eax = (uintptr_t) o;
152                         _ss->ecx = (uintptr_t) xpc;            /* REG_ITMP2_XPC */
153                         _ss->eip = (uintptr_t) asm_handle_exception;
154                 }
155                 else {
156                         _ss->eip = (uintptr_t) p;
157                 }
158         }
159         else {
160                 _ss->eax = (intptr_t) p;
161                 _ss->ecx = (intptr_t) xpc;
162                 _ss->eip = (intptr_t) asm_handle_exception;
163         }
164 }
165
166
167 /* md_signal_handler_sigfpe ****************************************************
168
169    ArithmeticException signal handler for hardware divide by zero
170    check.
171
172 *******************************************************************************/
173
174 void md_signal_handler_sigfpe(int sig, siginfo_t *siginfo, void *_p)
175 {
176         ucontext_t          *_uc;
177         mcontext_t           _mc;
178     u1                  *pv;
179         i386_thread_state_t *_ss;
180         u1                  *sp;
181         u1                  *ra;
182         u1                  *xpc;
183     int                  type;
184     intptr_t             val;
185     void                *p;
186
187
188         _uc = (ucontext_t *) _p;
189         _mc = _uc->uc_mcontext;
190         _ss = &_mc->ss;
191
192     pv  = NULL;                 /* is resolved during stackframeinfo creation */
193         sp  = (u1 *) _ss->esp;
194         xpc = (u1 *) _ss->eip;
195         ra  = xpc;                          /* return address is equal to xpc     */
196
197     /* this is an ArithmeticException */
198
199     type = EXCEPTION_HARDWARE_ARITHMETIC;
200     val  = 0;
201
202         /* Handle the type. */
203
204         p = signal_handle(type, val, pv, sp, ra, xpc, _p);
205
206     _ss->eax = (intptr_t) p;
207         _ss->ecx = (intptr_t) xpc;
208         _ss->eip = (intptr_t) asm_handle_exception;
209 }
210
211
212 /* md_signal_handler_sigusr2 ***************************************************
213
214    Signal handler for profiling sampling.
215
216 *******************************************************************************/
217
218 void md_signal_handler_sigusr2(int sig, siginfo_t *siginfo, void *_p)
219 {
220         threadobject        *t;
221         ucontext_t          *_uc;
222         mcontext_t           _mc;
223         i386_thread_state_t *_ss;
224         u1                  *pc;
225
226         t = THREADOBJECT;
227
228         _uc = (ucontext_t *) _p;
229         _mc = _uc->uc_mcontext;
230         _ss = &_mc->ss;
231
232         pc = (u1 *) _ss->eip;
233
234         t->pc = pc;
235 }
236
237
238 /* md_signal_handler_sigill ****************************************************
239
240    Signal handler for hardware patcher traps (ud2).
241
242 *******************************************************************************/
243
244 void md_signal_handler_sigill(int sig, siginfo_t *siginfo, void *_p)
245 {
246         ucontext_t          *_uc;
247         mcontext_t           _mc;
248         u1                  *pv;
249         i386_thread_state_t *_ss;
250         u1                  *sp;
251         u1                  *ra;
252         u1                  *xpc;
253         int                  type;
254         intptr_t             val;
255         void                *p;
256
257
258         _uc = (ucontext_t *) _p;
259         _mc = _uc->uc_mcontext;
260         _ss = &_mc->ss;
261
262         pv  = NULL;                 /* is resolved during stackframeinfo creation */
263         sp  = (u1 *) _ss->esp;
264         xpc = (u1 *) _ss->eip;
265         ra  = xpc;                          /* return address is equal to xpc     */
266
267         /* this is an ArithmeticException */
268
269         type = EXCEPTION_HARDWARE_PATCHER;
270         val  = 0;
271
272         /* generate appropriate exception */
273
274         p = signal_handle(type, val, pv, sp, ra, xpc, _p);
275
276         /* set registers (only if exception object ready) */
277
278         if (p != NULL) {
279                 _ss->eax = (intptr_t) p;
280                 _ss->ecx = (intptr_t) xpc;
281                 _ss->eip = (intptr_t) asm_handle_exception;
282         }
283 }
284
285 /* md_executionstate_read ******************************************************
286
287    Read the given context into an executionstate.
288
289 *******************************************************************************/
290
291 void md_executionstate_read(executionstate_t *es, void *context)
292 {
293         ucontext_t          *_uc;
294         mcontext_t           _mc; 
295         i386_thread_state_t *_ss;
296         int                  i;
297
298         _uc = (ucontext_t *) context;
299         _mc = _uc->uc_mcontext;
300         _ss = &_mc->ss;
301
302         /* read special registers */
303         es->pc = (u1 *) _ss->eip;
304         es->sp = (u1 *) _ss->esp;
305         es->pv = NULL;                   /* pv must be looked up via AVL tree */
306
307         /* read integer registers */
308         for (i = 0; i < INT_REG_CNT; i++)
309                 es->intregs[i] = (i == 0) ? _ss->eax :
310                         ((i == 1) ? _ss->ecx :
311                         ((i == 2) ? _ss->edx :
312                         ((i == 3) ? _ss->ebx :
313                         ((i == 4) ? _ss->esp :
314                         ((i == 5) ? _ss->ebp :
315                         ((i == 6) ? _ss->esi : _ss->edi))))));
316
317         /* read float registers */
318         for (i = 0; i < FLT_REG_CNT; i++)
319                 es->fltregs[i] = 0xdeadbeefdeadbeefULL;
320 }
321
322
323 /* md_executionstate_write *****************************************************
324
325    Write the given executionstate back to the context.
326
327 *******************************************************************************/
328
329 void md_executionstate_write(executionstate_t *es, void *context)
330 {
331         ucontext_t*          _uc;
332         mcontext_t           _mc;
333         i386_thread_state_t* _ss;
334         int                  i;
335
336         _uc = (ucontext_t *) context;
337         _mc = _uc->uc_mcontext;
338         _ss = &_mc->ss;
339
340         /* write integer registers */
341         for (i = 0; i < INT_REG_CNT; i++)
342                 *((i == 0) ? &_ss->eax :
343                   ((i == 1) ? &_ss->ecx :
344                   ((i == 2) ? &_ss->edx :
345                   ((i == 3) ? &_ss->ebx :
346                   ((i == 4) ? &_ss->esp :
347                   ((i == 5) ? &_ss->ebp :
348                   ((i == 6) ? &_ss->esi : &_ss->edi))))))) = es->intregs[i];
349
350         /* write special registers */
351         _ss->eip = (ptrint) es->pc;
352         _ss->esp = (ptrint) es->sp;
353 }
354
355
356 /* md_critical_section_restart *************************************************
357
358    Search the critical sections tree for a matching section and set
359    the PC to the restart point, if necessary.
360
361 *******************************************************************************/
362
363 #if defined(ENABLE_THREADS)
364 void thread_restartcriticalsection(ucontext_t *_uc)
365 {
366         mcontext_t           _mc;
367         i386_thread_state_t *_ss;
368         u1                  *pc;
369         void                *rpc;
370
371         _mc = _uc->uc_mcontext;
372         _ss = &_mc->ss;
373
374         pc = (u1 *) _ss->eip;
375
376         rpc = critical_find_restart_point(pc);
377
378         if (rpc != NULL)
379                 _ss->eip = (ptrint) rpc;
380 }
381 #endif
382
383
384 /*
385  * These are local overrides for various environment variables in Emacs.
386  * Please do not remove this and leave it at the end of the file, where
387  * Emacs will automagically detect them.
388  * ---------------------------------------------------------------------
389  * Local variables:
390  * mode: c
391  * indent-tabs-mode: t
392  * c-basic-offset: 4
393  * tab-width: 4
394  * End:
395  */