1 /* src/vm/jit/i386/linux/md-os.c - machine dependent i386 Linux 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
26 #define _GNU_SOURCE /* include REG_ defines from ucontext.h */
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/signallocal.h"
42 #include "vm/stringlocal.h"
44 #include "vm/jit/asmpart.h"
45 #include "vm/jit/executionstate.h"
46 #include "vm/jit/stacktrace.h"
47 #include "vm/jit/trap.h"
50 /* md_signal_handler_sigsegv ***************************************************
52 Signal handler for hardware exceptions.
54 *******************************************************************************/
56 void md_signal_handler_sigsegv(int sig, siginfo_t *siginfo, void *_p)
74 _uc = (ucontext_t *) _p;
75 _mc = &_uc->uc_mcontext;
77 pv = NULL; /* is resolved during stackframeinfo creation */
78 sp = (u1 *) _mc->gregs[REG_ESP];
79 xpc = (u1 *) _mc->gregs[REG_EIP];
80 ra = xpc; /* return address is equal to XPC */
82 /* get exception-throwing instruction */
84 opc = M_ALD_MEM_GET_OPC(xpc);
85 mod = M_ALD_MEM_GET_MOD(xpc);
86 rm = M_ALD_MEM_GET_RM(xpc);
88 /* for values see emit_mov_mem_reg and emit_mem */
90 if ((opc == 0x8b) && (mod == 0) && (rm == 5)) {
91 /* this was a hardware-exception */
93 d = M_ALD_MEM_GET_REG(xpc);
94 disp = M_ALD_MEM_GET_DISP(xpc);
96 /* we use the exception type as load displacement */
100 /* ATTENTION: The _mc->gregs layout is completely crazy! The
101 registers are reversed starting with number 4 for REG_EDI
102 (see /usr/include/sys/ucontext.h). We have to convert that
105 val = _mc->gregs[REG_EAX - d];
107 if (type == TRAP_COMPILER) {
108 /* The PV from the compiler stub is equal to the XPC. */
112 /* We use a framesize of zero here because the call pushed
113 the return addres onto the stack. */
115 ra = md_stacktrace_get_returnaddress(sp, 0);
117 /* Skip the RA on the stack. */
119 sp = sp + 1 * SIZEOF_VOID_P;
121 /* The XPC is the RA minus 2, because the RA points to the
122 instruction after the call. */
128 /* this was a normal NPE */
130 type = TRAP_NullPointerException;
134 /* Handle the trap. */
136 p = trap_handle(type, val, pv, sp, ra, xpc, _p);
140 if (type == TRAP_COMPILER) {
142 o = builtin_retrieve_exception();
144 _mc->gregs[REG_ESP] = (uintptr_t) sp; /* Remove RA from stack. */
146 _mc->gregs[REG_EAX] = (uintptr_t) o;
147 _mc->gregs[REG_ECX] = (uintptr_t) xpc; /* REG_ITMP2_XPC */
148 _mc->gregs[REG_EIP] = (uintptr_t) asm_handle_exception;
151 _mc->gregs[REG_EIP] = (uintptr_t) p;
154 #if defined(ENABLE_REPLACEMENT)
155 else if (type == TRAP_COUNTDOWN) {
156 /* context has been written by md_replace_executionstate_write */
160 _mc->gregs[REG_EAX] = (uintptr_t) p;
161 _mc->gregs[REG_ECX] = (uintptr_t) xpc; /* REG_ITMP2_XPC */
162 _mc->gregs[REG_EIP] = (uintptr_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)
186 _uc = (ucontext_t *) _p;
187 _mc = &_uc->uc_mcontext;
189 pv = NULL; /* is resolved during stackframeinfo creation */
190 sp = (u1 *) _mc->gregs[REG_ESP];
191 xpc = (u1 *) _mc->gregs[REG_EIP];
192 ra = xpc; /* return address is equal to xpc */
194 /* This is an ArithmeticException. */
196 type = TRAP_ArithmeticException;
199 /* Handle the trap. */
201 p = trap_handle(type, val, pv, sp, ra, xpc, _p);
205 _mc->gregs[REG_EAX] = (uintptr_t) p;
206 _mc->gregs[REG_ECX] = (uintptr_t) xpc; /* REG_ITMP2_XPC */
207 _mc->gregs[REG_EIP] = (uintptr_t) asm_handle_exception;
211 /* md_signal_handler_sigill ****************************************************
213 Signal handler for hardware patcher traps (ud2).
215 *******************************************************************************/
217 void md_signal_handler_sigill(int sig, siginfo_t *siginfo, void *_p)
229 _uc = (ucontext_t *) _p;
230 _mc = &_uc->uc_mcontext;
232 pv = NULL; /* is resolved during stackframeinfo creation */
233 sp = (u1 *) _mc->gregs[REG_ESP];
234 xpc = (u1 *) _mc->gregs[REG_EIP];
235 ra = xpc; /* return address is equal to xpc */
240 /* Handle the trap. */
242 p = trap_handle(type, val, pv, sp, ra, xpc, _p);
247 _mc->gregs[REG_EAX] = (uintptr_t) p;
248 _mc->gregs[REG_ECX] = (uintptr_t) xpc; /* REG_ITMP2_XPC */
249 _mc->gregs[REG_EIP] = (uintptr_t) asm_handle_exception;
254 /* md_signal_handler_sigusr1 ***************************************************
256 Signal handler for suspending threads.
258 *******************************************************************************/
260 #if defined(ENABLE_THREADS) && defined(ENABLE_GC_CACAO)
261 void md_signal_handler_sigusr1(int sig, siginfo_t *siginfo, void *_p)
268 _uc = (ucontext_t *) _p;
269 _mc = &_uc->uc_mcontext;
271 /* get the PC and SP for this thread */
272 pc = (u1 *) _mc->gregs[REG_EIP];
273 sp = (u1 *) _mc->gregs[REG_ESP];
275 /* now suspend the current thread */
276 threads_suspend_ack(pc, sp);
281 /* md_signal_handler_sigusr2 ***************************************************
283 Signal handler for profiling sampling.
285 *******************************************************************************/
287 #if defined(ENABLE_THREADS)
288 void md_signal_handler_sigusr2(int sig, siginfo_t *siginfo, void *_p)
297 _uc = (ucontext_t *) _p;
298 _mc = &_uc->uc_mcontext;
300 pc = (u1 *) _mc->gregs[REG_EIP];
307 /* md_executionstate_read ******************************************************
309 Read the given context into an executionstate for Replacement.
311 *******************************************************************************/
313 void md_executionstate_read(executionstate_t *es, void *context)
319 _uc = (ucontext_t *) context;
320 _mc = &_uc->uc_mcontext;
322 /* read special registers */
323 es->pc = (u1 *) _mc->gregs[REG_EIP];
324 es->sp = (u1 *) _mc->gregs[REG_ESP];
325 es->pv = NULL; /* pv must be looked up via AVL tree */
327 /* read integer registers */
328 for (i = 0; i < INT_REG_CNT; i++)
329 es->intregs[i] = _mc->gregs[REG_EAX - i];
331 /* read float registers */
332 for (i = 0; i < FLT_REG_CNT; i++)
333 es->fltregs[i] = 0xdeadbeefdeadbeefULL;
337 /* md_executionstate_write *****************************************************
339 Write the given executionstate back to the context for Replacement.
341 *******************************************************************************/
343 void md_executionstate_write(executionstate_t *es, void *context)
349 _uc = (ucontext_t *) context;
350 _mc = &_uc->uc_mcontext;
352 /* write integer registers */
353 for (i = 0; i < INT_REG_CNT; i++)
354 _mc->gregs[REG_EAX - i] = es->intregs[i];
356 /* write special registers */
357 _mc->gregs[REG_EIP] = (ptrint) es->pc;
358 _mc->gregs[REG_ESP] = (ptrint) es->sp;
363 * These are local overrides for various environment variables in Emacs.
364 * Please do not remove this and leave it at the end of the file, where
365 * Emacs will automagically detect them.
366 * ---------------------------------------------------------------------
369 * indent-tabs-mode: t