1 /* src/vm/jit/i386/darwin/md-os.c - machine dependent i386 Darwin functions
3 Copyright (C) 1996-2005, 2006, 2007, 2008
4 CACAOVM - Verein zur Foerderung der freien virtuellen Maschine CACAO
6 This file is part of CACAO.
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.
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.
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
35 #include "vm/jit/i386/codegen.h"
36 #include "vm/jit/i386/md.h"
38 #include "threads/thread.hpp"
40 #include "vm/jit/builtin.hpp"
41 #include "vm/global.h"
42 #include "vm/signallocal.h"
44 #include "vm/jit/asmpart.h"
45 #include "vm/jit/executionstate.h"
46 #include "vm/jit/stacktrace.hpp"
47 #include "vm/jit/trap.h"
49 #include "vm/jit/i386/codegen.h"
52 /* md_signal_handler_sigsegv ***************************************************
54 Signal handler for hardware exceptions.
56 *******************************************************************************/
58 void md_signal_handler_sigsegv(int sig, siginfo_t *siginfo, void *_p)
63 i386_thread_state_t *_ss;
77 _uc = (ucontext_t *) _p;
78 _mc = _uc->uc_mcontext;
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 */
86 /* get exception-throwing instruction */
88 opc = M_ALD_MEM_GET_OPC(xpc);
89 mod = M_ALD_MEM_GET_MOD(xpc);
90 rm = M_ALD_MEM_GET_RM(xpc);
92 /* for values see emit_mov_mem_reg and emit_mem */
94 if ((opc == 0x8b) && (mod == 0) && (rm == 5)) {
95 /* this was a hardware-exception */
97 d = M_ALD_MEM_GET_REG(xpc);
98 disp = M_ALD_MEM_GET_DISP(xpc);
100 /* we use the exception type as load displacement */
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))))));
112 if (type == TRAP_COMPILER) {
113 /* The PV from the compiler stub is equal to the XPC. */
117 /* We use a framesize of zero here because the call pushed
118 the return addres onto the stack. */
120 ra = md_stacktrace_get_returnaddress(sp, 0);
122 /* Skip the RA on the stack. */
124 sp = sp + 1 * SIZEOF_VOID_P;
126 /* The XPC is the RA minus 2, because the RA points to the
127 instruction after the call. */
133 /* this was a normal NPE */
135 type = TRAP_NullPointerException;
138 /* Handle the trap. */
140 p = trap_handle(type, val, pv, sp, ra, xpc, _p);
144 if (type == TRAP_COMPILER) {
146 o = builtin_retrieve_exception();
148 _ss->__esp = (uintptr_t) sp; /* Remove RA from stack. */
150 _ss->__eax = (uintptr_t) o;
151 _ss->__ecx = (uintptr_t) xpc; /* REG_ITMP2_XPC */
152 _ss->__eip = (uintptr_t) asm_handle_exception;
155 _ss->__eip = (uintptr_t) p;
159 _ss->__eax = (uintptr_t) p;
160 _ss->__ecx = (uintptr_t) xpc; /* REG_ITMP2_XPC */
161 _ss->__eip = (uintptr_t) asm_handle_exception;
166 /* md_signal_handler_sigfpe ****************************************************
168 ArithmeticException signal handler for hardware divide by zero
171 *******************************************************************************/
173 void md_signal_handler_sigfpe(int sig, siginfo_t *siginfo, void *_p)
178 i386_thread_state_t *_ss;
187 _uc = (ucontext_t *) _p;
188 _mc = _uc->uc_mcontext;
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 */
196 /* This is an ArithmeticException */
198 type = TRAP_ArithmeticException;
201 /* Handle the trap. */
203 p = trap_handle(type, val, pv, sp, ra, xpc, _p);
207 _ss->__eax = (uintptr_t) p;
208 _ss->__ecx = (uintptr_t) xpc; /* REG_ITMP2_XPC */
209 _ss->__eip = (uintptr_t) asm_handle_exception;
213 /* md_signal_handler_sigusr2 ***************************************************
215 Signal handler for profiling sampling.
217 *******************************************************************************/
219 void md_signal_handler_sigusr2(int sig, siginfo_t *siginfo, void *_p)
224 i386_thread_state_t *_ss;
229 _uc = (ucontext_t *) _p;
230 _mc = _uc->uc_mcontext;
233 pc = (u1 *) _ss->__eip;
239 /* md_signal_handler_sigill ****************************************************
241 Signal handler for hardware patcher traps (ud2).
243 *******************************************************************************/
245 void md_signal_handler_sigill(int sig, siginfo_t *siginfo, void *_p)
250 i386_thread_state_t *_ss;
259 _uc = (ucontext_t *) _p;
260 _mc = _uc->uc_mcontext;
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 */
271 /* Handle the trap. */
273 p = trap_handle(type, val, pv, sp, ra, xpc, _p);
278 _ss->__eax = (uintptr_t) p;
279 _ss->__ecx = (uintptr_t) xpc; /* REG_ITMP2_XPC */
280 _ss->__eip = (uintptr_t) asm_handle_exception;
284 /* md_executionstate_read ******************************************************
286 Read the given context into an executionstate.
288 *******************************************************************************/
290 void md_executionstate_read(executionstate_t *es, void *context)
294 i386_thread_state_t *_ss;
297 _uc = (ucontext_t *) context;
298 _mc = _uc->uc_mcontext;
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 */
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))))));
316 /* read float registers */
317 for (i = 0; i < FLT_REG_CNT; i++)
318 es->fltregs[i] = 0xdeadbeefdeadbeefULL;
322 /* md_executionstate_write *****************************************************
324 Write the given executionstate back to the context.
326 *******************************************************************************/
328 void md_executionstate_write(executionstate_t *es, void *context)
332 i386_thread_state_t* _ss;
335 _uc = (ucontext_t *) context;
336 _mc = _uc->uc_mcontext;
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];
349 /* write special registers */
350 _ss->__eip = (ptrint) es->pc;
351 _ss->__esp = (ptrint) es->sp;
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 * ---------------------------------------------------------------------
362 * indent-tabs-mode: t