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"
45 #include "vm/jit/asmpart.h"
46 #include "vm/jit/stacktrace.h"
48 #include "vm/jit/i386/codegen.h"
51 /* md_signal_handler_sigsegv ***************************************************
53 Signal handler for hardware exceptions.
55 *******************************************************************************/
57 void md_signal_handler_sigsegv(int sig, siginfo_t *siginfo, void *_p)
62 i386_thread_state_t *_ss;
76 _uc = (ucontext_t *) _p;
77 _mc = _uc->uc_mcontext;
80 pv = NULL; /* is resolved during stackframeinfo creation */
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 == EXCEPTION_HARDWARE_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 = EXCEPTION_HARDWARE_NULLPOINTER;
137 /* Handle the type. */
139 p = signal_handle(type, val, pv, sp, ra, xpc, _p);
143 if (type == EXCEPTION_HARDWARE_COMPILER) {
145 o = builtin_retrieve_exception();
147 _ss->esp = (uintptr_t) sp; /* Remove RA from stack. */
149 _ss->eax = (uintptr_t) o;
150 _ss->ecx = (uintptr_t) xpc; /* REG_ITMP2_XPC */
151 _ss->eip = (uintptr_t) asm_handle_exception;
154 _ss->eip = (uintptr_t) p;
158 _ss->eax = (intptr_t) p;
159 _ss->ecx = (intptr_t) xpc;
160 _ss->eip = (intptr_t) asm_handle_exception;
165 /* md_signal_handler_sigfpe ****************************************************
167 ArithmeticException signal handler for hardware divide by zero
170 *******************************************************************************/
172 void md_signal_handler_sigfpe(int sig, siginfo_t *siginfo, void *_p)
177 i386_thread_state_t *_ss;
186 _uc = (ucontext_t *) _p;
187 _mc = _uc->uc_mcontext;
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 */
195 /* this is an ArithmeticException */
197 type = EXCEPTION_HARDWARE_ARITHMETIC;
200 /* Handle the type. */
202 p = signal_handle(type, val, pv, sp, ra, xpc, _p);
204 _ss->eax = (intptr_t) p;
205 _ss->ecx = (intptr_t) xpc;
206 _ss->eip = (intptr_t) asm_handle_exception;
210 /* md_signal_handler_sigusr2 ***************************************************
212 Signal handler for profiling sampling.
214 *******************************************************************************/
216 void md_signal_handler_sigusr2(int sig, siginfo_t *siginfo, void *_p)
221 i386_thread_state_t *_ss;
226 _uc = (ucontext_t *) _p;
227 _mc = _uc->uc_mcontext;
230 pc = (u1 *) _ss->eip;
236 /* md_signal_handler_sigill ****************************************************
238 Signal handler for hardware patcher traps (ud2).
240 *******************************************************************************/
242 void md_signal_handler_sigill(int sig, siginfo_t *siginfo, void *_p)
247 i386_thread_state_t *_ss;
256 _uc = (ucontext_t *) _p;
257 _mc = _uc->uc_mcontext;
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 */
265 /* this is an ArithmeticException */
267 type = EXCEPTION_HARDWARE_PATCHER;
270 /* generate appropriate exception */
272 p = signal_handle(type, val, pv, sp, ra, xpc, _p);
274 /* set registers (only if exception object ready) */
277 _ss->eax = (intptr_t) p;
278 _ss->ecx = (intptr_t) xpc;
279 _ss->eip = (intptr_t) asm_handle_exception;
283 /* md_replace_executionstate_read **********************************************
285 Read the given context into an executionstate for Replacement.
287 *******************************************************************************/
289 #if defined(ENABLE_REPLACEMENT)
290 void md_replace_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;
323 /* md_replace_executionstate_write *********************************************
325 Write the given executionstate back to the context for Replacement.
327 *******************************************************************************/
329 #if defined(ENABLE_REPLACEMENT)
330 void md_replace_executionstate_write(executionstate_t *es, void *context)
334 i386_thread_state_t *_ss;
337 _uc = (ucontext_t *) context;
338 _mc = &_uc->uc_mcontext;
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];
351 /* write special registers */
352 _ss->eip = (ptrint) es->pc;
353 _ss->esp = (ptrint) es->sp;
358 /* md_critical_section_restart *************************************************
360 Search the critical sections tree for a matching section and set
361 the PC to the restart point, if necessary.
363 *******************************************************************************/
365 #if defined(ENABLE_THREADS)
366 void thread_restartcriticalsection(ucontext_t *_uc)
369 i386_thread_state_t *_ss;
373 _mc = _uc->uc_mcontext;
376 pc = (u1 *) _ss->eip;
378 rpc = critical_find_restart_point(pc);
381 _ss->eip = (ptrint) rpc;
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 * ---------------------------------------------------------------------
393 * indent-tabs-mode: t