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.hpp"
43 #include "vm/jit/asmpart.h"
44 #include "vm/jit/executionstate.h"
45 #include "vm/jit/stacktrace.hpp"
46 #include "vm/jit/trap.hpp"
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 */
208 // Check if the trap instruction is valid.
209 // TODO Move this into patcher_handler.
210 if (patcher_is_valid_trap_instruction_at(xpc) == false) {
211 // Check if the PC has been patched during our way to this
212 // signal handler (see PR85).
213 if (patcher_is_patched_at(xpc) == true)
216 // We have a problem...
217 log_println("md_signal_handler_sigill: Unknown illegal instruction at 0x%lx", xpc);
218 #if defined(ENABLE_DISASSEMBLER)
219 (void) disassinstr(xpc);
221 vm_abort("Aborting...");
227 /* Handle the trap. */
229 trap_handle(type, val, pv, sp, ra, xpc, _p);
233 /* md_signal_handler_sigusr1 ***************************************************
235 Signal handler for suspending threads.
237 *******************************************************************************/
239 #if defined(ENABLE_THREADS) && defined(ENABLE_GC_CACAO)
240 void md_signal_handler_sigusr1(int sig, siginfo_t *siginfo, void *_p)
247 _uc = (ucontext_t *) _p;
248 _mc = &_uc->uc_mcontext;
250 /* get the PC and SP for this thread */
251 pc = (u1 *) _mc->gregs[REG_EIP];
252 sp = (u1 *) _mc->gregs[REG_ESP];
254 /* now suspend the current thread */
255 threads_suspend_ack(pc, sp);
260 /* md_signal_handler_sigusr2 ***************************************************
262 Signal handler for profiling sampling.
264 *******************************************************************************/
266 #if defined(ENABLE_THREADS)
267 void md_signal_handler_sigusr2(int sig, siginfo_t *siginfo, void *_p)
276 _uc = (ucontext_t *) _p;
277 _mc = &_uc->uc_mcontext;
279 pc = (u1 *) _mc->gregs[REG_EIP];
286 /* md_executionstate_read ******************************************************
288 Read the given context into an executionstate for Replacement.
290 *******************************************************************************/
292 void md_executionstate_read(executionstate_t *es, void *context)
298 _uc = (ucontext_t *) context;
299 _mc = &_uc->uc_mcontext;
301 /* read special registers */
302 es->pc = (u1 *) _mc->gregs[REG_EIP];
303 es->sp = (u1 *) _mc->gregs[REG_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] = _mc->gregs[REG_EAX - i];
310 /* read float registers */
311 for (i = 0; i < FLT_REG_CNT; i++)
312 es->fltregs[i] = 0xdeadbeefdeadbeefULL;
316 /* md_executionstate_write *****************************************************
318 Write the given executionstate back to the context for Replacement.
320 *******************************************************************************/
322 void md_executionstate_write(executionstate_t *es, void *context)
328 _uc = (ucontext_t *) context;
329 _mc = &_uc->uc_mcontext;
331 /* write integer registers */
332 for (i = 0; i < INT_REG_CNT; i++)
333 _mc->gregs[REG_EAX - i] = es->intregs[i];
335 /* write special registers */
336 _mc->gregs[REG_EIP] = (ptrint) es->pc;
337 _mc->gregs[REG_ESP] = (ptrint) es->sp;
342 * These are local overrides for various environment variables in Emacs.
343 * Please do not remove this and leave it at the end of the file, where
344 * Emacs will automagically detect them.
345 * ---------------------------------------------------------------------
348 * indent-tabs-mode: t