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.hpp"
44 #include "vm/jit/asmpart.h"
45 #include "vm/jit/executionstate.h"
46 #include "vm/jit/stacktrace.hpp"
47 #include "vm/jit/trap.hpp"
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;
76 _uc = (ucontext_t *) _p;
77 _mc = _uc->uc_mcontext;
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 */
85 /* get exception-throwing instruction */
87 opc = M_ALD_MEM_GET_OPC(xpc);
88 mod = M_ALD_MEM_GET_MOD(xpc);
89 rm = M_ALD_MEM_GET_RM(xpc);
91 /* for values see emit_mov_mem_reg and emit_mem */
93 if ((opc == 0x8b) && (mod == 0) && (rm == 5)) {
94 /* this was a hardware-exception */
96 d = M_ALD_MEM_GET_REG(xpc);
97 disp = M_ALD_MEM_GET_DISP(xpc);
99 /* we use the exception type as load displacement */
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))))));
111 if (type == TRAP_COMPILER) {
112 /* The PV from the compiler stub is equal to the XPC. */
116 /* We use a framesize of zero here because the call pushed
117 the return addres onto the stack. */
119 ra = md_stacktrace_get_returnaddress(sp, 0);
121 /* Skip the RA on the stack. */
123 sp = sp + 1 * SIZEOF_VOID_P;
125 /* The XPC is the RA minus 2, because the RA points to the
126 instruction after the call. */
132 /* this was a normal NPE */
134 type = TRAP_NullPointerException;
137 /* Handle the trap. */
139 p = trap_handle(type, val, pv, sp, ra, xpc, _p);
143 if (type == TRAP_COMPILER) {
145 _ss->__esp = (uintptr_t) sp; /* Remove RA from stack. */
151 /* md_signal_handler_sigfpe ****************************************************
153 ArithmeticException signal handler for hardware divide by zero
156 *******************************************************************************/
158 void md_signal_handler_sigfpe(int sig, siginfo_t *siginfo, void *_p)
163 i386_thread_state_t *_ss;
171 _uc = (ucontext_t *) _p;
172 _mc = _uc->uc_mcontext;
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 */
180 /* This is an ArithmeticException */
182 type = TRAP_ArithmeticException;
185 /* Handle the trap. */
187 trap_handle(type, val, pv, sp, ra, xpc, _p);
191 /* md_signal_handler_sigusr2 ***************************************************
193 Signal handler for profiling sampling.
195 *******************************************************************************/
197 void md_signal_handler_sigusr2(int sig, siginfo_t *siginfo, void *_p)
202 i386_thread_state_t *_ss;
207 _uc = (ucontext_t *) _p;
208 _mc = _uc->uc_mcontext;
211 pc = (u1 *) _ss->__eip;
217 /* md_signal_handler_sigill ****************************************************
219 Signal handler for hardware patcher traps (ud2).
221 *******************************************************************************/
223 void md_signal_handler_sigill(int sig, siginfo_t *siginfo, void *_p)
228 i386_thread_state_t *_ss;
236 _uc = (ucontext_t *) _p;
237 _mc = _uc->uc_mcontext;
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 */
248 /* Handle the trap. */
250 trap_handle(type, val, pv, sp, ra, xpc, _p);
253 /* md_executionstate_read ******************************************************
255 Read the given context into an executionstate.
257 *******************************************************************************/
259 void md_executionstate_read(executionstate_t *es, void *context)
263 i386_thread_state_t *_ss;
266 _uc = (ucontext_t *) context;
267 _mc = _uc->uc_mcontext;
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 */
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))))));
285 /* read float registers */
286 for (i = 0; i < FLT_REG_CNT; i++)
287 es->fltregs[i] = 0xdeadbeefdeadbeefULL;
291 /* md_executionstate_write *****************************************************
293 Write the given executionstate back to the context.
295 *******************************************************************************/
297 void md_executionstate_write(executionstate_t *es, void *context)
301 i386_thread_state_t* _ss;
304 _uc = (ucontext_t *) context;
305 _mc = _uc->uc_mcontext;
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];
318 /* write special registers */
319 _ss->__eip = (ptrint) es->pc;
320 _ss->__esp = (ptrint) es->sp;
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 * ---------------------------------------------------------------------
331 * indent-tabs-mode: t