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)
73 _uc = (ucontext_t *) _p;
74 _mc = &_uc->uc_mcontext;
76 pv = NULL; /* is resolved during stackframeinfo creation */
77 sp = (u1 *) _mc->gregs[REG_ESP];
78 xpc = (u1 *) _mc->gregs[REG_EIP];
79 ra = xpc; /* return address is equal to XPC */
81 /* get exception-throwing instruction */
83 opc = M_ALD_MEM_GET_OPC(xpc);
84 mod = M_ALD_MEM_GET_MOD(xpc);
85 rm = M_ALD_MEM_GET_RM(xpc);
87 /* for values see emit_mov_mem_reg and emit_mem */
89 if ((opc == 0x8b) && (mod == 0) && (rm == 5)) {
90 /* this was a hardware-exception */
92 d = M_ALD_MEM_GET_REG(xpc);
93 disp = M_ALD_MEM_GET_DISP(xpc);
95 /* we use the exception type as load displacement */
99 /* ATTENTION: The _mc->gregs layout is completely crazy! The
100 registers are reversed starting with number 4 for REG_EDI
101 (see /usr/include/sys/ucontext.h). We have to convert that
104 val = _mc->gregs[REG_EAX - d];
106 if (type == TRAP_COMPILER) {
107 /* The PV from the compiler stub is equal to the XPC. */
111 /* We use a framesize of zero here because the call pushed
112 the return addres onto the stack. */
114 ra = md_stacktrace_get_returnaddress(sp, 0);
116 /* Skip the RA on the stack. */
118 sp = sp + 1 * SIZEOF_VOID_P;
120 /* The XPC is the RA minus 2, because the RA points to the
121 instruction after the call. */
127 /* this was a normal NPE */
129 type = TRAP_NullPointerException;
133 /* Handle the trap. */
135 p = trap_handle(type, val, pv, sp, ra, xpc, _p);
139 if (type == TRAP_COMPILER) {
141 o = builtin_retrieve_exception();
143 _mc->gregs[REG_ESP] = (uintptr_t) sp; /* Remove RA from stack. */
145 _mc->gregs[REG_EAX] = (uintptr_t) o;
146 _mc->gregs[REG_ECX] = (uintptr_t) xpc; /* REG_ITMP2_XPC */
147 _mc->gregs[REG_EIP] = (uintptr_t) asm_handle_exception;
150 _mc->gregs[REG_EIP] = (uintptr_t) p;
153 #if defined(ENABLE_REPLACEMENT)
154 else if (type == TRAP_COUNTDOWN) {
155 /* context has been written by md_replace_executionstate_write */
159 _mc->gregs[REG_EAX] = (uintptr_t) p;
160 _mc->gregs[REG_ECX] = (uintptr_t) xpc; /* REG_ITMP2_XPC */
161 _mc->gregs[REG_EIP] = (uintptr_t) asm_handle_exception;
166 /* md_signal_handler_sigfpe ****************************************************
168 ArithmeticException signal handler for hardware divide by zero
171 *******************************************************************************/
173 void md_signal_handler_sigfpe(int sig, siginfo_t *siginfo, void *_p)
185 _uc = (ucontext_t *) _p;
186 _mc = &_uc->uc_mcontext;
188 pv = NULL; /* is resolved during stackframeinfo creation */
189 sp = (u1 *) _mc->gregs[REG_ESP];
190 xpc = (u1 *) _mc->gregs[REG_EIP];
191 ra = xpc; /* return address is equal to xpc */
193 /* This is an ArithmeticException. */
195 type = TRAP_ArithmeticException;
198 /* Handle the trap. */
200 p = trap_handle(type, val, pv, sp, ra, xpc, _p);
204 _mc->gregs[REG_EAX] = (uintptr_t) p;
205 _mc->gregs[REG_ECX] = (uintptr_t) xpc; /* REG_ITMP2_XPC */
206 _mc->gregs[REG_EIP] = (uintptr_t) asm_handle_exception;
210 /* md_signal_handler_sigill ****************************************************
212 Signal handler for hardware patcher traps (ud2).
214 *******************************************************************************/
216 void md_signal_handler_sigill(int sig, siginfo_t *siginfo, void *_p)
228 _uc = (ucontext_t *) _p;
229 _mc = &_uc->uc_mcontext;
231 pv = NULL; /* is resolved during stackframeinfo creation */
232 sp = (u1 *) _mc->gregs[REG_ESP];
233 xpc = (u1 *) _mc->gregs[REG_EIP];
234 ra = xpc; /* return address is equal to xpc */
239 /* Handle the trap. */
241 p = trap_handle(type, val, pv, sp, ra, xpc, _p);
246 _mc->gregs[REG_EAX] = (uintptr_t) p;
247 _mc->gregs[REG_ECX] = (uintptr_t) xpc; /* REG_ITMP2_XPC */
248 _mc->gregs[REG_EIP] = (uintptr_t) asm_handle_exception;
253 /* md_signal_handler_sigusr1 ***************************************************
255 Signal handler for suspending threads.
257 *******************************************************************************/
259 #if defined(ENABLE_THREADS) && defined(ENABLE_GC_CACAO)
260 void md_signal_handler_sigusr1(int sig, siginfo_t *siginfo, void *_p)
267 _uc = (ucontext_t *) _p;
268 _mc = &_uc->uc_mcontext;
270 /* get the PC and SP for this thread */
271 pc = (u1 *) _mc->gregs[REG_EIP];
272 sp = (u1 *) _mc->gregs[REG_ESP];
274 /* now suspend the current thread */
275 threads_suspend_ack(pc, sp);
280 /* md_signal_handler_sigusr2 ***************************************************
282 Signal handler for profiling sampling.
284 *******************************************************************************/
286 #if defined(ENABLE_THREADS)
287 void md_signal_handler_sigusr2(int sig, siginfo_t *siginfo, void *_p)
296 _uc = (ucontext_t *) _p;
297 _mc = &_uc->uc_mcontext;
299 pc = (u1 *) _mc->gregs[REG_EIP];
306 /* md_executionstate_read ******************************************************
308 Read the given context into an executionstate for Replacement.
310 *******************************************************************************/
312 void md_executionstate_read(executionstate_t *es, void *context)
318 _uc = (ucontext_t *) context;
319 _mc = &_uc->uc_mcontext;
321 /* read special registers */
322 es->pc = (u1 *) _mc->gregs[REG_EIP];
323 es->sp = (u1 *) _mc->gregs[REG_ESP];
324 es->pv = NULL; /* pv must be looked up via AVL tree */
326 /* read integer registers */
327 for (i = 0; i < INT_REG_CNT; i++)
328 es->intregs[i] = _mc->gregs[REG_EAX - i];
330 /* read float registers */
331 for (i = 0; i < FLT_REG_CNT; i++)
332 es->fltregs[i] = 0xdeadbeefdeadbeefULL;
336 /* md_executionstate_write *****************************************************
338 Write the given executionstate back to the context for Replacement.
340 *******************************************************************************/
342 void md_executionstate_write(executionstate_t *es, void *context)
348 _uc = (ucontext_t *) context;
349 _mc = &_uc->uc_mcontext;
351 /* write integer registers */
352 for (i = 0; i < INT_REG_CNT; i++)
353 _mc->gregs[REG_EAX - i] = es->intregs[i];
355 /* write special registers */
356 _mc->gregs[REG_EIP] = (ptrint) es->pc;
357 _mc->gregs[REG_ESP] = (ptrint) es->sp;
362 * These are local overrides for various environment variables in Emacs.
363 * Please do not remove this and leave it at the end of the file, where
364 * Emacs will automagically detect them.
365 * ---------------------------------------------------------------------
368 * indent-tabs-mode: t