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.h"
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"
46 #include "vm/jit/asmpart.h"
47 #include "vm/jit/executionstate.h"
48 #include "vm/jit/stacktrace.h"
50 #include "vm/jit/i386/codegen.h"
53 /* md_signal_handler_sigsegv ***************************************************
55 Signal handler for hardware exceptions.
57 *******************************************************************************/
59 void md_signal_handler_sigsegv(int sig, siginfo_t *siginfo, void *_p)
64 i386_thread_state_t *_ss;
78 _uc = (ucontext_t *) _p;
79 _mc = _uc->uc_mcontext;
82 pv = NULL; /* is resolved during stackframeinfo creation */
84 xpc = (u1 *) _ss->eip;
85 ra = xpc; /* return address is equal to XPC */
87 /* get exception-throwing instruction */
89 opc = M_ALD_MEM_GET_OPC(xpc);
90 mod = M_ALD_MEM_GET_MOD(xpc);
91 rm = M_ALD_MEM_GET_RM(xpc);
93 /* for values see emit_mov_mem_reg and emit_mem */
95 if ((opc == 0x8b) && (mod == 0) && (rm == 5)) {
96 /* this was a hardware-exception */
98 d = M_ALD_MEM_GET_REG(xpc);
99 disp = M_ALD_MEM_GET_DISP(xpc);
101 /* we use the exception type as load displacement */
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))))));
113 if (type == EXCEPTION_HARDWARE_COMPILER) {
114 /* The PV from the compiler stub is equal to the XPC. */
118 /* We use a framesize of zero here because the call pushed
119 the return addres onto the stack. */
121 ra = md_stacktrace_get_returnaddress(sp, 0);
123 /* Skip the RA on the stack. */
125 sp = sp + 1 * SIZEOF_VOID_P;
127 /* The XPC is the RA minus 2, because the RA points to the
128 instruction after the call. */
134 /* this was a normal NPE */
136 type = EXCEPTION_HARDWARE_NULLPOINTER;
139 /* Handle the type. */
141 p = signal_handle(type, val, pv, sp, ra, xpc, _p);
145 if (type == EXCEPTION_HARDWARE_COMPILER) {
147 o = builtin_retrieve_exception();
149 _ss->esp = (uintptr_t) sp; /* Remove RA from stack. */
151 _ss->eax = (uintptr_t) o;
152 _ss->ecx = (uintptr_t) xpc; /* REG_ITMP2_XPC */
153 _ss->eip = (uintptr_t) asm_handle_exception;
156 _ss->eip = (uintptr_t) p;
160 _ss->eax = (intptr_t) p;
161 _ss->ecx = (intptr_t) xpc;
162 _ss->eip = (intptr_t) asm_handle_exception;
167 /* md_signal_handler_sigfpe ****************************************************
169 ArithmeticException signal handler for hardware divide by zero
172 *******************************************************************************/
174 void md_signal_handler_sigfpe(int sig, siginfo_t *siginfo, void *_p)
179 i386_thread_state_t *_ss;
188 _uc = (ucontext_t *) _p;
189 _mc = _uc->uc_mcontext;
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 */
197 /* this is an ArithmeticException */
199 type = EXCEPTION_HARDWARE_ARITHMETIC;
202 /* Handle the type. */
204 p = signal_handle(type, val, pv, sp, ra, xpc, _p);
206 _ss->eax = (intptr_t) p;
207 _ss->ecx = (intptr_t) xpc;
208 _ss->eip = (intptr_t) asm_handle_exception;
212 /* md_signal_handler_sigusr2 ***************************************************
214 Signal handler for profiling sampling.
216 *******************************************************************************/
218 void md_signal_handler_sigusr2(int sig, siginfo_t *siginfo, void *_p)
223 i386_thread_state_t *_ss;
228 _uc = (ucontext_t *) _p;
229 _mc = _uc->uc_mcontext;
232 pc = (u1 *) _ss->eip;
238 /* md_signal_handler_sigill ****************************************************
240 Signal handler for hardware patcher traps (ud2).
242 *******************************************************************************/
244 void md_signal_handler_sigill(int sig, siginfo_t *siginfo, void *_p)
249 i386_thread_state_t *_ss;
258 _uc = (ucontext_t *) _p;
259 _mc = _uc->uc_mcontext;
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 */
267 /* this is an ArithmeticException */
269 type = EXCEPTION_HARDWARE_PATCHER;
272 /* generate appropriate exception */
274 p = signal_handle(type, val, pv, sp, ra, xpc, _p);
276 /* set registers (only if exception object ready) */
279 _ss->eax = (intptr_t) p;
280 _ss->ecx = (intptr_t) xpc;
281 _ss->eip = (intptr_t) asm_handle_exception;
285 /* md_executionstate_read ******************************************************
287 Read the given context into an executionstate.
289 *******************************************************************************/
291 void md_executionstate_read(executionstate_t *es, void *context)
295 i386_thread_state_t *_ss;
298 _uc = (ucontext_t *) context;
299 _mc = _uc->uc_mcontext;
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 */
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))))));
317 /* read float registers */
318 for (i = 0; i < FLT_REG_CNT; i++)
319 es->fltregs[i] = 0xdeadbeefdeadbeefULL;
323 /* md_executionstate_write *****************************************************
325 Write the given executionstate back to the context.
327 *******************************************************************************/
329 void md_executionstate_write(executionstate_t *es, void *context)
333 i386_thread_state_t* _ss;
336 _uc = (ucontext_t *) context;
337 _mc = _uc->uc_mcontext;
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];
350 /* write special registers */
351 _ss->eip = (ptrint) es->pc;
352 _ss->esp = (ptrint) es->sp;
356 /* md_critical_section_restart *************************************************
358 Search the critical sections tree for a matching section and set
359 the PC to the restart point, if necessary.
361 *******************************************************************************/
363 #if defined(ENABLE_THREADS)
364 void thread_restartcriticalsection(ucontext_t *_uc)
367 i386_thread_state_t *_ss;
371 _mc = _uc->uc_mcontext;
374 pc = (u1 *) _ss->eip;
376 rpc = critical_find_restart_point(pc);
379 _ss->eip = (ptrint) rpc;
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 * ---------------------------------------------------------------------
391 * indent-tabs-mode: t