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"
38 #include "vm/jit/i386/md.h"
40 #include "threads/threads-common.h"
42 #include "vm/builtin.h"
43 #include "vm/exceptions.h"
44 #include "vm/signallocal.h"
45 #include "vm/stringlocal.h"
47 #include "vm/jit/asmpart.h"
48 #include "vm/jit/stacktrace.h"
51 /* md_signal_handler_sigsegv ***************************************************
53 Signal handler for hardware exceptions.
55 *******************************************************************************/
57 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];
108 if (type == EXCEPTION_HARDWARE_COMPILER) {
109 /* The PV from the compiler stub is equal to the XPC. */
113 /* We use a framesize of zero here because the call pushed
114 the return addres onto the stack. */
116 ra = md_stacktrace_get_returnaddress(sp, 0);
118 /* Skip the RA on the stack. */
120 sp = sp + 1 * SIZEOF_VOID_P;
122 /* The XPC is the RA minus 2, because the RA points to the
123 instruction after the call. */
129 /* this was a normal NPE */
131 type = EXCEPTION_HARDWARE_NULLPOINTER;
135 /* Handle the type. */
137 p = signal_handle(type, val, pv, sp, ra, xpc, _p);
141 if (type == EXCEPTION_HARDWARE_COMPILER) {
143 o = builtin_retrieve_exception();
145 _mc->gregs[REG_ESP] = (uintptr_t) sp; /* Remove RA from stack. */
147 _mc->gregs[REG_EAX] = (uintptr_t) o;
148 _mc->gregs[REG_ECX] = (uintptr_t) xpc; /* REG_ITMP2_XPC */
149 _mc->gregs[REG_EIP] = (uintptr_t) asm_handle_exception;
152 _mc->gregs[REG_EIP] = (uintptr_t) p;
156 _mc->gregs[REG_EAX] = (intptr_t) p;
157 _mc->gregs[REG_ECX] = (intptr_t) xpc; /* REG_ITMP2_XPC */
158 _mc->gregs[REG_EIP] = (intptr_t) asm_handle_exception;
163 /* md_signal_handler_sigfpe ****************************************************
165 ArithmeticException signal handler for hardware divide by zero
168 *******************************************************************************/
170 void md_signal_handler_sigfpe(int sig, siginfo_t *siginfo, void *_p)
182 _uc = (ucontext_t *) _p;
183 _mc = &_uc->uc_mcontext;
185 pv = NULL; /* is resolved during stackframeinfo creation */
186 sp = (u1 *) _mc->gregs[REG_ESP];
187 xpc = (u1 *) _mc->gregs[REG_EIP];
188 ra = xpc; /* return address is equal to xpc */
190 /* this is an ArithmeticException */
192 type = EXCEPTION_HARDWARE_ARITHMETIC;
195 /* Handle the type. */
197 p = signal_handle(type, val, pv, sp, ra, xpc, _p);
201 _mc->gregs[REG_EAX] = (intptr_t) p;
202 _mc->gregs[REG_ECX] = (intptr_t) xpc; /* REG_ITMP2_XPC */
203 _mc->gregs[REG_EIP] = (intptr_t) asm_handle_exception;
207 /* md_signal_handler_sigill ****************************************************
209 Signal handler for hardware patcher traps (ud2).
211 *******************************************************************************/
213 void md_signal_handler_sigill(int sig, siginfo_t *siginfo, void *_p)
225 _uc = (ucontext_t *) _p;
226 _mc = &_uc->uc_mcontext;
228 pv = NULL; /* is resolved during stackframeinfo creation */
229 sp = (u1 *) _mc->gregs[REG_ESP];
230 xpc = (u1 *) _mc->gregs[REG_EIP];
231 ra = xpc; /* return address is equal to xpc */
233 /* this is an ArithmeticException */
235 type = EXCEPTION_HARDWARE_PATCHER;
238 /* generate appropriate exception */
240 p = signal_handle(type, val, pv, sp, ra, xpc, _p);
242 /* set registers (only if exception object ready) */
245 _mc->gregs[REG_EAX] = (ptrint) p;
246 _mc->gregs[REG_ECX] = (ptrint) xpc; /* REG_ITMP2_XPC */
247 _mc->gregs[REG_EIP] = (ptrint) asm_handle_exception;
252 /* md_signal_handler_sigusr1 ***************************************************
254 Signal handler for suspending threads.
256 *******************************************************************************/
258 #if defined(ENABLE_THREADS) && defined(ENABLE_GC_CACAO)
259 void md_signal_handler_sigusr1(int sig, siginfo_t *siginfo, void *_p)
266 _uc = (ucontext_t *) _p;
267 _mc = &_uc->uc_mcontext;
269 /* get the PC and SP for this thread */
270 pc = (u1 *) _mc->gregs[REG_EIP];
271 sp = (u1 *) _mc->gregs[REG_ESP];
273 /* now suspend the current thread */
274 threads_suspend_ack(pc, sp);
279 /* md_signal_handler_sigusr2 ***************************************************
281 Signal handler for profiling sampling.
283 *******************************************************************************/
285 #if defined(ENABLE_THREADS)
286 void md_signal_handler_sigusr2(int sig, siginfo_t *siginfo, void *_p)
295 _uc = (ucontext_t *) _p;
296 _mc = &_uc->uc_mcontext;
298 pc = (u1 *) _mc->gregs[REG_EIP];
305 /* md_replace_executionstate_read **********************************************
307 Read the given context into an executionstate for Replacement.
309 *******************************************************************************/
311 #if defined(ENABLE_REPLACEMENT)
312 void md_replace_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;
337 /* md_replace_executionstate_write *********************************************
339 Write the given executionstate back to the context for Replacement.
341 *******************************************************************************/
343 #if defined(ENABLE_REPLACEMENT)
344 void md_replace_executionstate_write(executionstate_t *es, void *context)
350 _uc = (ucontext_t *) context;
351 _mc = &_uc->uc_mcontext;
353 /* write integer registers */
354 for (i = 0; i < INT_REG_CNT; i++)
355 _mc->gregs[REG_EAX - i] = es->intregs[i];
357 /* write special registers */
358 _mc->gregs[REG_EIP] = (ptrint) es->pc;
359 _mc->gregs[REG_ESP] = (ptrint) es->sp;
364 /* md_critical_section_restart *************************************************
366 Search the critical sections tree for a matching section and set
367 the PC to the restart point, if necessary.
369 *******************************************************************************/
371 #if defined(ENABLE_THREADS)
372 void md_critical_section_restart(ucontext_t *_uc)
378 _mc = &_uc->uc_mcontext;
380 pc = (u1 *) _mc->gregs[REG_EIP];
382 npc = critical_find_restart_point(pc);
385 _mc->gregs[REG_EIP] = (ptrint) npc;
391 * These are local overrides for various environment variables in Emacs.
392 * Please do not remove this and leave it at the end of the file, where
393 * Emacs will automagically detect them.
394 * ---------------------------------------------------------------------
397 * indent-tabs-mode: t