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
25 $Id: md-os.c 8243 2007-07-31 08:57:54Z michi $
30 #define _GNU_SOURCE /* include REG_ defines from ucontext.h */
38 #include "vm/jit/i386/codegen.h"
40 #include "threads/threads-common.h"
42 #include "vm/exceptions.h"
43 #include "vm/signallocal.h"
44 #include "vm/stringlocal.h"
46 #include "vm/jit/asmpart.h"
47 #include "vm/jit/stacktrace.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];
108 /* this was a normal NPE */
110 type = EXCEPTION_HARDWARE_NULLPOINTER;
114 /* create stackframeinfo */
116 stacktrace_create_extern_stackframeinfo(&sfi, pv, sp, ra, xpc);
118 /* generate appropriate exception */
120 e = exceptions_new_hardware_exception(xpc, type, val);
122 /* remove stackframeinfo */
124 stacktrace_remove_stackframeinfo(&sfi);
128 _mc->gregs[REG_EAX] = (ptrint) e;
129 _mc->gregs[REG_ECX] = (ptrint) xpc; /* REG_ITMP2_XPC */
130 _mc->gregs[REG_EIP] = (ptrint) asm_handle_exception;
134 /* md_signal_handler_sigfpe ****************************************************
136 ArithmeticException signal handler for hardware divide by zero
139 *******************************************************************************/
141 void md_signal_handler_sigfpe(int sig, siginfo_t *siginfo, void *_p)
152 java_objectheader *e;
154 _uc = (ucontext_t *) _p;
155 _mc = &_uc->uc_mcontext;
157 pv = NULL; /* is resolved during stackframeinfo creation */
158 sp = (u1 *) _mc->gregs[REG_ESP];
159 xpc = (u1 *) _mc->gregs[REG_EIP];
160 ra = xpc; /* return address is equal to xpc */
162 /* this is an ArithmeticException */
164 type = EXCEPTION_HARDWARE_ARITHMETIC;
167 /* create stackframeinfo */
169 stacktrace_create_extern_stackframeinfo(&sfi, pv, sp, ra, xpc);
171 /* generate appropriate exception */
173 e = exceptions_new_hardware_exception(xpc, type, val);
175 /* remove stackframeinfo */
177 stacktrace_remove_stackframeinfo(&sfi);
179 _mc->gregs[REG_EAX] = (ptrint) e;
180 _mc->gregs[REG_ECX] = (ptrint) xpc; /* REG_ITMP2_XPC */
181 _mc->gregs[REG_EIP] = (ptrint) asm_handle_exception;
185 /* md_signal_handler_sigusr2 ***************************************************
187 Signal handler for profiling sampling.
189 *******************************************************************************/
191 #if defined(ENABLE_THREADS)
192 void md_signal_handler_sigusr2(int sig, siginfo_t *siginfo, void *_p)
201 _uc = (ucontext_t *) _p;
202 _mc = &_uc->uc_mcontext;
204 pc = (u1 *) _mc->gregs[REG_EIP];
211 /* md_critical_section_restart *************************************************
213 Search the critical sections tree for a matching section and set
214 the PC to the restart point, if necessary.
216 *******************************************************************************/
218 #if defined(ENABLE_THREADS)
219 void md_critical_section_restart(ucontext_t *_uc)
225 _mc = &_uc->uc_mcontext;
227 pc = (u1 *) _mc->gregs[REG_EIP];
229 npc = critical_find_restart_point(pc);
232 _mc->gregs[REG_EIP] = (ptrint) npc;
238 * These are local overrides for various environment variables in Emacs.
239 * Please do not remove this and leave it at the end of the file, where
240 * Emacs will automagically detect them.
241 * ---------------------------------------------------------------------
244 * indent-tabs-mode: t