* src/vm/builtin.c: Moved to .cpp.
[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.h"
43
44 #include "vm/jit/asmpart.h"
45 #include "vm/jit/executionstate.h"
46 #include "vm/jit/stacktrace.hpp"
47 #include "vm/jit/trap.h"
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         java_object_t       *o;
76
77         _uc = (ucontext_t *) _p;
78         _mc = _uc->uc_mcontext;
79         _ss = &_mc->__ss;
80
81         pv  = NULL;                 /* is resolved during stackframeinfo creation */
82         sp  = (u1 *) _ss->__esp;
83         xpc = (u1 *) _ss->__eip;
84         ra  = xpc;                              /* return address is equal to XPC */
85
86         /* get exception-throwing instruction */
87
88         opc = M_ALD_MEM_GET_OPC(xpc);
89         mod = M_ALD_MEM_GET_MOD(xpc);
90         rm  = M_ALD_MEM_GET_RM(xpc);
91
92         /* for values see emit_mov_mem_reg and emit_mem */
93
94         if ((opc == 0x8b) && (mod == 0) && (rm == 5)) {
95                 /* this was a hardware-exception */
96
97                 d    = M_ALD_MEM_GET_REG(xpc);
98                 disp = M_ALD_MEM_GET_DISP(xpc);
99
100                 /* we use the exception type as load displacement */
101
102                 type = disp;
103
104                 val = (d == 0) ? _ss->__eax :
105                         ((d == 1) ? _ss->__ecx :
106                         ((d == 2) ? _ss->__edx :
107                         ((d == 3) ? _ss->__ebx :
108                         ((d == 4) ? _ss->__esp :
109                         ((d == 5) ? _ss->__ebp :
110                         ((d == 6) ? _ss->__esi : _ss->__edi))))));
111
112                 if (type == TRAP_COMPILER) {
113                         /* The PV from the compiler stub is equal to the XPC. */
114
115                         pv = xpc;
116
117                         /* We use a framesize of zero here because the call pushed
118                            the return addres onto the stack. */
119
120                         ra = md_stacktrace_get_returnaddress(sp, 0);
121
122                         /* Skip the RA on the stack. */
123
124                         sp = sp + 1 * SIZEOF_VOID_P;
125
126                         /* The XPC is the RA minus 2, because the RA points to the
127                            instruction after the call. */
128
129                         xpc = ra - 2;
130                 } 
131         }
132         else {
133                 /* this was a normal NPE */
134
135                 type = TRAP_NullPointerException;
136         }
137
138         /* Handle the trap. */
139
140         p = trap_handle(type, val, pv, sp, ra, xpc, _p);
141
142         /* Set registers. */
143
144         if (type == TRAP_COMPILER) {
145                 if (p == NULL) { 
146                         o = builtin_retrieve_exception();
147
148                         _ss->__esp = (uintptr_t) sp;  /* Remove RA from stack. */
149
150                         _ss->__eax = (uintptr_t) o;
151                         _ss->__ecx = (uintptr_t) xpc;           /* REG_ITMP2_XPC */
152                         _ss->__eip = (uintptr_t) asm_handle_exception;
153                 }
154                 else {
155                         _ss->__eip = (uintptr_t) p;
156                 }
157         }
158         else {
159                 _ss->__eax = (uintptr_t) p;
160                 _ss->__ecx = (uintptr_t) xpc;            /* REG_ITMP2_XPC */
161                 _ss->__eip = (uintptr_t) asm_handle_exception;
162         }
163 }
164
165
166 /* md_signal_handler_sigfpe ****************************************************
167
168    ArithmeticException signal handler for hardware divide by zero
169    check.
170
171 *******************************************************************************/
172
173 void md_signal_handler_sigfpe(int sig, siginfo_t *siginfo, void *_p)
174 {
175         ucontext_t          *_uc;
176         mcontext_t           _mc;
177         u1                  *pv;
178         i386_thread_state_t *_ss;
179         u1                  *sp;
180         u1                  *ra;
181         u1                  *xpc;
182         int                  type;
183         intptr_t             val;
184         void                *p;
185
186
187         _uc = (ucontext_t *) _p;
188         _mc = _uc->uc_mcontext;
189         _ss = &_mc->__ss;
190
191         pv  = NULL;                 /* is resolved during stackframeinfo creation */
192         sp  = (u1 *) _ss->__esp;
193         xpc = (u1 *) _ss->__eip;
194         ra  = xpc;                          /* return address is equal to xpc     */
195
196         /* This is an ArithmeticException */
197
198         type = TRAP_ArithmeticException;
199         val  = 0;
200
201         /* Handle the trap. */
202
203         p = trap_handle(type, val, pv, sp, ra, xpc, _p);
204
205         /* Set registers. */
206
207         _ss->__eax = (uintptr_t) p;
208         _ss->__ecx = (uintptr_t) xpc;            /* REG_ITMP2_XPC */
209         _ss->__eip = (uintptr_t) asm_handle_exception;
210 }
211
212
213 /* md_signal_handler_sigusr2 ***************************************************
214
215    Signal handler for profiling sampling.
216
217 *******************************************************************************/
218
219 void md_signal_handler_sigusr2(int sig, siginfo_t *siginfo, void *_p)
220 {
221         threadobject        *t;
222         ucontext_t          *_uc;
223         mcontext_t           _mc;
224         i386_thread_state_t *_ss;
225         u1                  *pc;
226
227         t = THREADOBJECT;
228
229         _uc = (ucontext_t *) _p;
230         _mc = _uc->uc_mcontext;
231         _ss = &_mc->__ss;
232
233         pc = (u1 *) _ss->__eip;
234
235         t->pc = pc;
236 }
237
238
239 /* md_signal_handler_sigill ****************************************************
240
241    Signal handler for hardware patcher traps (ud2).
242
243 *******************************************************************************/
244
245 void md_signal_handler_sigill(int sig, siginfo_t *siginfo, void *_p)
246 {
247         ucontext_t          *_uc;
248         mcontext_t           _mc;
249         u1                  *pv;
250         i386_thread_state_t *_ss;
251         u1                  *sp;
252         u1                  *ra;
253         u1                  *xpc;
254         int                  type;
255         intptr_t             val;
256         void                *p;
257
258
259         _uc = (ucontext_t *) _p;
260         _mc = _uc->uc_mcontext;
261         _ss = &_mc->__ss;
262
263         pv  = NULL;                 /* is resolved during stackframeinfo creation */
264         sp  = (u1 *) _ss->__esp;
265         xpc = (u1 *) _ss->__eip;
266         ra  = xpc;                          /* return address is equal to xpc     */
267
268         type = TRAP_PATCHER;
269         val  = 0;
270
271         /* Handle the trap. */
272
273         p = trap_handle(type, val, pv, sp, ra, xpc, _p);
274
275         /* Set registers. */
276
277         if (p != NULL) {
278                 _ss->__eax = (uintptr_t) p;
279                 _ss->__ecx = (uintptr_t) xpc;            /* REG_ITMP2_XPC */
280                 _ss->__eip = (uintptr_t) asm_handle_exception;
281         }
282 }
283
284 /* md_executionstate_read ******************************************************
285
286    Read the given context into an executionstate.
287
288 *******************************************************************************/
289
290 void md_executionstate_read(executionstate_t *es, void *context)
291 {
292         ucontext_t          *_uc;
293         mcontext_t           _mc; 
294         i386_thread_state_t *_ss;
295         int                  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
321
322 /* md_executionstate_write *****************************************************
323
324    Write the given executionstate back to the context.
325
326 *******************************************************************************/
327
328 void md_executionstate_write(executionstate_t *es, void *context)
329 {
330         ucontext_t*          _uc;
331         mcontext_t           _mc;
332         i386_thread_state_t* _ss;
333         int                  i;
334
335         _uc = (ucontext_t *) context;
336         _mc = _uc->uc_mcontext;
337         _ss = &_mc->__ss;
338
339         /* write integer registers */
340         for (i = 0; i < INT_REG_CNT; i++)
341                 *((i == 0) ? &_ss->__eax :
342                  ((i == 1) ? &_ss->__ecx :
343                  ((i == 2) ? &_ss->__edx :
344                  ((i == 3) ? &_ss->__ebx :
345                  ((i == 4) ? &_ss->__esp :
346                  ((i == 5) ? &_ss->__ebp :
347                  ((i == 6) ? &_ss->__esi : &_ss->__edi))))))) = es->intregs[i];
348
349         /* write special registers */
350         _ss->__eip = (ptrint) es->pc;
351         _ss->__esp = (ptrint) es->sp;
352 }
353
354
355 /*
356  * These are local overrides for various environment variables in Emacs.
357  * Please do not remove this and leave it at the end of the file, where
358  * Emacs will automagically detect them.
359  * ---------------------------------------------------------------------
360  * Local variables:
361  * mode: c
362  * indent-tabs-mode: t
363  * c-basic-offset: 4
364  * tab-width: 4
365  * End:
366  */