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