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.hpp"
40 #include "vm/jit/builtin.hpp"
41 #include "vm/signallocal.h"
43 #include "vm/jit/asmpart.h"
44 #include "vm/jit/executionstate.h"
45 #include "vm/jit/stacktrace.hpp"
46 #include "vm/jit/trap.h"
49 /* md_signal_handler_sigsegv ***************************************************
51 Signal handler for hardware exceptions.
53 *******************************************************************************/
55 void md_signal_handler_sigsegv(int sig, siginfo_t *siginfo, void *_p)
72 _uc = (ucontext_t *) _p;
73 _mc = &_uc->uc_mcontext;
75 pv = NULL; /* is resolved during stackframeinfo creation */
76 sp = (u1 *) _mc->gregs[REG_ESP];
77 xpc = (u1 *) _mc->gregs[REG_EIP];
78 ra = xpc; /* return address is equal to XPC */
80 /* get exception-throwing instruction */
82 opc = M_ALD_MEM_GET_OPC(xpc);
83 mod = M_ALD_MEM_GET_MOD(xpc);
84 rm = M_ALD_MEM_GET_RM(xpc);
86 /* for values see emit_mov_mem_reg and emit_mem */
88 if ((opc == 0x8b) && (mod == 0) && (rm == 5)) {
89 /* this was a hardware-exception */
91 d = M_ALD_MEM_GET_REG(xpc);
92 disp = M_ALD_MEM_GET_DISP(xpc);
94 /* we use the exception type as load displacement */
98 /* ATTENTION: The _mc->gregs layout is completely crazy! The
99 registers are reversed starting with number 4 for REG_EDI
100 (see /usr/include/sys/ucontext.h). We have to convert that
103 val = _mc->gregs[REG_EAX - d];
105 if (type == TRAP_COMPILER) {
106 /* The PV from the compiler stub is equal to the XPC. */
110 /* We use a framesize of zero here because the call pushed
111 the return addres onto the stack. */
113 ra = md_stacktrace_get_returnaddress(sp, 0);
115 /* Skip the RA on the stack. */
117 sp = sp + 1 * SIZEOF_VOID_P;
119 /* The XPC is the RA minus 2, because the RA points to the
120 instruction after the call. */
126 /* this was a normal NPE */
128 type = TRAP_NullPointerException;
132 /* Handle the trap. */
134 p = trap_handle(type, val, pv, sp, ra, xpc, _p);
138 if (type == TRAP_COMPILER) {
140 _mc->gregs[REG_ESP] = (uintptr_t) sp; /* Remove RA from stack. */
146 /* md_signal_handler_sigfpe ****************************************************
148 ArithmeticException signal handler for hardware divide by zero
151 *******************************************************************************/
153 void md_signal_handler_sigfpe(int sig, siginfo_t *siginfo, void *_p)
164 _uc = (ucontext_t *) _p;
165 _mc = &_uc->uc_mcontext;
167 pv = NULL; /* is resolved during stackframeinfo creation */
168 sp = (u1 *) _mc->gregs[REG_ESP];
169 xpc = (u1 *) _mc->gregs[REG_EIP];
170 ra = xpc; /* return address is equal to xpc */
172 /* This is an ArithmeticException. */
174 type = TRAP_ArithmeticException;
177 /* Handle the trap. */
179 trap_handle(type, val, pv, sp, ra, xpc, _p);
183 /* md_signal_handler_sigill ****************************************************
185 Signal handler for hardware patcher traps (ud2).
187 *******************************************************************************/
189 void md_signal_handler_sigill(int sig, siginfo_t *siginfo, void *_p)
200 _uc = (ucontext_t *) _p;
201 _mc = &_uc->uc_mcontext;
203 pv = NULL; /* is resolved during stackframeinfo creation */
204 sp = (u1 *) _mc->gregs[REG_ESP];
205 xpc = (u1 *) _mc->gregs[REG_EIP];
206 ra = xpc; /* return address is equal to xpc */
211 /* Handle the trap. */
213 trap_handle(type, val, pv, sp, ra, xpc, _p);
217 /* md_signal_handler_sigusr1 ***************************************************
219 Signal handler for suspending threads.
221 *******************************************************************************/
223 #if defined(ENABLE_THREADS) && defined(ENABLE_GC_CACAO)
224 void md_signal_handler_sigusr1(int sig, siginfo_t *siginfo, void *_p)
231 _uc = (ucontext_t *) _p;
232 _mc = &_uc->uc_mcontext;
234 /* get the PC and SP for this thread */
235 pc = (u1 *) _mc->gregs[REG_EIP];
236 sp = (u1 *) _mc->gregs[REG_ESP];
238 /* now suspend the current thread */
239 threads_suspend_ack(pc, sp);
244 /* md_signal_handler_sigusr2 ***************************************************
246 Signal handler for profiling sampling.
248 *******************************************************************************/
250 #if defined(ENABLE_THREADS)
251 void md_signal_handler_sigusr2(int sig, siginfo_t *siginfo, void *_p)
260 _uc = (ucontext_t *) _p;
261 _mc = &_uc->uc_mcontext;
263 pc = (u1 *) _mc->gregs[REG_EIP];
270 /* md_executionstate_read ******************************************************
272 Read the given context into an executionstate for Replacement.
274 *******************************************************************************/
276 void md_executionstate_read(executionstate_t *es, void *context)
282 _uc = (ucontext_t *) context;
283 _mc = &_uc->uc_mcontext;
285 /* read special registers */
286 es->pc = (u1 *) _mc->gregs[REG_EIP];
287 es->sp = (u1 *) _mc->gregs[REG_ESP];
288 es->pv = NULL; /* pv must be looked up via AVL tree */
290 /* read integer registers */
291 for (i = 0; i < INT_REG_CNT; i++)
292 es->intregs[i] = _mc->gregs[REG_EAX - i];
294 /* read float registers */
295 for (i = 0; i < FLT_REG_CNT; i++)
296 es->fltregs[i] = 0xdeadbeefdeadbeefULL;
300 /* md_executionstate_write *****************************************************
302 Write the given executionstate back to the context for Replacement.
304 *******************************************************************************/
306 void md_executionstate_write(executionstate_t *es, void *context)
312 _uc = (ucontext_t *) context;
313 _mc = &_uc->uc_mcontext;
315 /* write integer registers */
316 for (i = 0; i < INT_REG_CNT; i++)
317 _mc->gregs[REG_EAX - i] = es->intregs[i];
319 /* write special registers */
320 _mc->gregs[REG_EIP] = (ptrint) es->pc;
321 _mc->gregs[REG_ESP] = (ptrint) es->sp;
326 * These are local overrides for various environment variables in Emacs.
327 * Please do not remove this and leave it at the end of the file, where
328 * Emacs will automagically detect them.
329 * ---------------------------------------------------------------------
332 * indent-tabs-mode: t