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 7601 2007-03-28 23:02:50Z michi $
30 #define _GNU_SOURCE /* include REG_ defines from ucontext.h */
38 #if defined(ENABLE_GC_CACAO)
39 # include "mm/cacao-gc/gc.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"
49 #include "vm/jit/i386/codegen.h"
52 /* md_signal_handler_sigsegv ***************************************************
54 Signal handler for hardware exceptions.
56 *******************************************************************************/
58 void md_signal_handler_sigsegv(int sig, siginfo_t *siginfo, void *_p)
75 _uc = (ucontext_t *) _p;
76 _mc = &_uc->uc_mcontext;
78 pv = NULL; /* is resolved during stackframeinfo creation */
79 sp = (u1 *) _mc->gregs[REG_ESP];
80 xpc = (u1 *) _mc->gregs[REG_EIP];
81 ra = xpc; /* return address is equal to XPC */
83 /* get exception-throwing instruction */
85 opc = M_ALD_MEM_GET_OPC(xpc);
86 mod = M_ALD_MEM_GET_MOD(xpc);
87 rm = M_ALD_MEM_GET_RM(xpc);
89 /* for values see emit_mov_mem_reg and emit_mem */
91 if ((opc == 0x8b) && (mod == 0) && (rm == 5)) {
92 /* this was a hardware-exception */
94 d = M_ALD_MEM_GET_REG(xpc);
95 disp = M_ALD_MEM_GET_DISP(xpc);
97 /* we use the exception type as load displacement */
101 /* ATTENTION: The _mc->gregs layout is completely crazy! The
102 registers are reversed starting with number 4 for REG_EDI
103 (see /usr/include/sys/ucontext.h). We have to convert that
106 val = _mc->gregs[REG_EAX - d];
109 /* this was a normal NPE */
111 type = EXCEPTION_HARDWARE_NULLPOINTER;
114 /* generate appropriate exception */
116 e = exceptions_new_hardware_exception(pv, sp, ra, xpc, type, val);
120 _mc->gregs[REG_EAX] = (ptrint) e;
121 _mc->gregs[REG_ECX] = (ptrint) xpc; /* REG_ITMP2_XPC */
122 _mc->gregs[REG_EIP] = (ptrint) asm_handle_exception;
126 /* md_signal_handler_sigfpe ****************************************************
128 ArithmeticException signal handler for hardware divide by zero
131 *******************************************************************************/
133 void md_signal_handler_sigfpe(int sig, siginfo_t *siginfo, void *_p)
141 _uc = (ucontext_t *) _p;
142 _mc = &_uc->uc_mcontext;
144 sp = (u1 *) _mc->gregs[REG_ESP];
145 xpc = (u1 *) _mc->gregs[REG_EIP];
146 ra = xpc; /* return address is equal to xpc */
148 _mc->gregs[REG_EAX] =
149 (ptrint) stacktrace_hardware_arithmeticexception(NULL, sp, ra, xpc);
151 _mc->gregs[REG_ECX] = (ptrint) xpc; /* REG_ITMP2_XPC */
152 _mc->gregs[REG_EIP] = (ptrint) asm_handle_exception;
156 /* md_signal_handler_sigusr1 ***************************************************
158 Signal handler for suspending threads.
160 *******************************************************************************/
162 #if defined(ENABLE_THREADS) && defined(ENABLE_GC_CACAO)
163 void md_signal_handler_sigusr1(int sig, siginfo_t *siginfo, void *_p)
170 _uc = (ucontext_t *) _p;
171 _mc = &_uc->uc_mcontext;
173 /* assume there is a GC pending */
176 /* get the PC and SP for this thread */
177 pc = (u1 *) _mc->gregs[REG_EIP];
178 sp = (u1 *) _mc->gregs[REG_ESP];
180 /* now suspend the current thread */
181 threads_suspend_ack(pc, sp);
186 /* md_signal_handler_sigusr2 ***************************************************
188 Signal handler for profiling sampling.
190 *******************************************************************************/
192 #if defined(ENABLE_THREADS)
193 void md_signal_handler_sigusr2(int sig, siginfo_t *siginfo, void *_p)
202 _uc = (ucontext_t *) _p;
203 _mc = &_uc->uc_mcontext;
205 pc = (u1 *) _mc->gregs[REG_EIP];
212 #if defined(ENABLE_THREADS)
213 void thread_restartcriticalsection(ucontext_t *uc)
217 critical = critical_find_restart_point((void *) uc->uc_mcontext.gregs[REG_EIP]);
220 uc->uc_mcontext.gregs[REG_EIP] = (ptrint) critical;
226 * These are local overrides for various environment variables in Emacs.
227 * Please do not remove this and leave it at the end of the file, where
228 * Emacs will automagically detect them.
229 * ---------------------------------------------------------------------
232 * indent-tabs-mode: t