1 /* src/vm/jit/i386/linux/md-os.c - machine dependent i386 Linux functions
3 Copyright (C) 1996-2005, 2006, 2007 R. Grafl, A. Krall, C. Kruegel,
4 C. Oates, R. Obermaisser, M. Platter, M. Probst, S. Ring,
5 E. Steiner, C. Thalinger, D. Thuernbeck, P. Tomsich, C. Ullrich,
6 J. Wenninger, Institut f. Computersprachen - TU Wien
8 This file is part of CACAO.
10 This program is free software; you can redistribute it and/or
11 modify it under the terms of the GNU General Public License as
12 published by the Free Software Foundation; either version 2, or (at
13 your option) any later version.
15 This program is distributed in the hope that it will be useful, but
16 WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
28 #define _GNU_SOURCE /* include REG_ defines from ucontext.h */
37 #include "vm/jit/i386/codegen.h"
39 #include "threads/threads-common.h"
41 #include "vm/exceptions.h"
42 #include "vm/signallocal.h"
43 #include "vm/stringlocal.h"
45 #include "vm/jit/asmpart.h"
46 #include "vm/jit/stacktrace.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 == EXCEPTION_HARDWARE_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 = EXCEPTION_HARDWARE_NULLPOINTER;
133 /* Handle the type. */
135 p = signal_handle(type, val, pv, sp, ra, xpc, _p);
139 if (type == EXCEPTION_HARDWARE_COMPILER) {
141 o = exceptions_get_and_clear_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;
154 _mc->gregs[REG_EAX] = (intptr_t) p;
155 _mc->gregs[REG_ECX] = (intptr_t) xpc; /* REG_ITMP2_XPC */
156 _mc->gregs[REG_EIP] = (intptr_t) asm_handle_exception;
161 /* md_signal_handler_sigfpe ****************************************************
163 ArithmeticException signal handler for hardware divide by zero
166 *******************************************************************************/
168 void md_signal_handler_sigfpe(int sig, siginfo_t *siginfo, void *_p)
180 _uc = (ucontext_t *) _p;
181 _mc = &_uc->uc_mcontext;
183 pv = NULL; /* is resolved during stackframeinfo creation */
184 sp = (u1 *) _mc->gregs[REG_ESP];
185 xpc = (u1 *) _mc->gregs[REG_EIP];
186 ra = xpc; /* return address is equal to xpc */
188 /* this is an ArithmeticException */
190 type = EXCEPTION_HARDWARE_ARITHMETIC;
193 /* Handle the type. */
195 p = signal_handle(type, val, pv, sp, ra, xpc, _p);
199 _mc->gregs[REG_EAX] = (intptr_t) p;
200 _mc->gregs[REG_ECX] = (intptr_t) xpc; /* REG_ITMP2_XPC */
201 _mc->gregs[REG_EIP] = (intptr_t) asm_handle_exception;
205 /* md_signal_handler_sigill ****************************************************
207 Signal handler for hardware patcher traps (ud2).
209 *******************************************************************************/
211 void md_signal_handler_sigill(int sig, siginfo_t *siginfo, void *_p)
223 _uc = (ucontext_t *) _p;
224 _mc = &_uc->uc_mcontext;
226 pv = NULL; /* is resolved during stackframeinfo creation */
227 sp = (u1 *) _mc->gregs[REG_ESP];
228 xpc = (u1 *) _mc->gregs[REG_EIP];
229 ra = xpc; /* return address is equal to xpc */
231 /* this is an ArithmeticException */
233 type = EXCEPTION_HARDWARE_PATCHER;
236 /* generate appropriate exception */
238 p = signal_handle(type, val, pv, sp, ra, xpc, _p);
240 /* set registers (only if exception object ready) */
243 _mc->gregs[REG_EAX] = (ptrint) p;
244 _mc->gregs[REG_ECX] = (ptrint) xpc; /* REG_ITMP2_XPC */
245 _mc->gregs[REG_EIP] = (ptrint) asm_handle_exception;
250 /* md_signal_handler_sigusr1 ***************************************************
252 Signal handler for suspending threads.
254 *******************************************************************************/
256 #if defined(ENABLE_THREADS) && defined(ENABLE_GC_CACAO)
257 void md_signal_handler_sigusr1(int sig, siginfo_t *siginfo, void *_p)
264 _uc = (ucontext_t *) _p;
265 _mc = &_uc->uc_mcontext;
267 /* get the PC and SP for this thread */
268 pc = (u1 *) _mc->gregs[REG_EIP];
269 sp = (u1 *) _mc->gregs[REG_ESP];
271 /* now suspend the current thread */
272 threads_suspend_ack(pc, sp);
277 /* md_signal_handler_sigusr2 ***************************************************
279 Signal handler for profiling sampling.
281 *******************************************************************************/
283 #if defined(ENABLE_THREADS)
284 void md_signal_handler_sigusr2(int sig, siginfo_t *siginfo, void *_p)
293 _uc = (ucontext_t *) _p;
294 _mc = &_uc->uc_mcontext;
296 pc = (u1 *) _mc->gregs[REG_EIP];
303 /* md_critical_section_restart *************************************************
305 Search the critical sections tree for a matching section and set
306 the PC to the restart point, if necessary.
308 *******************************************************************************/
310 #if defined(ENABLE_THREADS)
311 void md_critical_section_restart(ucontext_t *_uc)
317 _mc = &_uc->uc_mcontext;
319 pc = (u1 *) _mc->gregs[REG_EIP];
321 npc = critical_find_restart_point(pc);
324 _mc->gregs[REG_EIP] = (ptrint) npc;
330 * These are local overrides for various environment variables in Emacs.
331 * Please do not remove this and leave it at the end of the file, where
332 * Emacs will automagically detect them.
333 * ---------------------------------------------------------------------
336 * indent-tabs-mode: t