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/executionstate.h"
49 #include "vm/jit/stacktrace.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)
76 _uc = (ucontext_t *) _p;
77 _mc = &_uc->uc_mcontext;
79 pv = NULL; /* is resolved during stackframeinfo creation */
80 sp = (u1 *) _mc->gregs[REG_ESP];
81 xpc = (u1 *) _mc->gregs[REG_EIP];
82 ra = xpc; /* return address is equal to XPC */
84 /* get exception-throwing instruction */
86 opc = M_ALD_MEM_GET_OPC(xpc);
87 mod = M_ALD_MEM_GET_MOD(xpc);
88 rm = M_ALD_MEM_GET_RM(xpc);
90 /* for values see emit_mov_mem_reg and emit_mem */
92 if ((opc == 0x8b) && (mod == 0) && (rm == 5)) {
93 /* this was a hardware-exception */
95 d = M_ALD_MEM_GET_REG(xpc);
96 disp = M_ALD_MEM_GET_DISP(xpc);
98 /* we use the exception type as load displacement */
102 /* ATTENTION: The _mc->gregs layout is completely crazy! The
103 registers are reversed starting with number 4 for REG_EDI
104 (see /usr/include/sys/ucontext.h). We have to convert that
107 val = _mc->gregs[REG_EAX - d];
109 if (type == EXCEPTION_HARDWARE_COMPILER) {
110 /* The PV from the compiler stub is equal to the XPC. */
114 /* We use a framesize of zero here because the call pushed
115 the return addres onto the stack. */
117 ra = md_stacktrace_get_returnaddress(sp, 0);
119 /* Skip the RA on the stack. */
121 sp = sp + 1 * SIZEOF_VOID_P;
123 /* The XPC is the RA minus 2, because the RA points to the
124 instruction after the call. */
130 /* this was a normal NPE */
132 type = EXCEPTION_HARDWARE_NULLPOINTER;
136 /* Handle the type. */
138 p = signal_handle(type, val, pv, sp, ra, xpc, _p);
142 if (type == EXCEPTION_HARDWARE_COMPILER) {
144 o = builtin_retrieve_exception();
146 _mc->gregs[REG_ESP] = (uintptr_t) sp; /* Remove RA from stack. */
148 _mc->gregs[REG_EAX] = (uintptr_t) o;
149 _mc->gregs[REG_ECX] = (uintptr_t) xpc; /* REG_ITMP2_XPC */
150 _mc->gregs[REG_EIP] = (uintptr_t) asm_handle_exception;
153 _mc->gregs[REG_EIP] = (uintptr_t) p;
157 _mc->gregs[REG_EAX] = (intptr_t) p;
158 _mc->gregs[REG_ECX] = (intptr_t) xpc; /* REG_ITMP2_XPC */
159 _mc->gregs[REG_EIP] = (intptr_t) asm_handle_exception;
164 /* md_signal_handler_sigfpe ****************************************************
166 ArithmeticException signal handler for hardware divide by zero
169 *******************************************************************************/
171 void md_signal_handler_sigfpe(int sig, siginfo_t *siginfo, void *_p)
183 _uc = (ucontext_t *) _p;
184 _mc = &_uc->uc_mcontext;
186 pv = NULL; /* is resolved during stackframeinfo creation */
187 sp = (u1 *) _mc->gregs[REG_ESP];
188 xpc = (u1 *) _mc->gregs[REG_EIP];
189 ra = xpc; /* return address is equal to xpc */
191 /* this is an ArithmeticException */
193 type = EXCEPTION_HARDWARE_ARITHMETIC;
196 /* Handle the type. */
198 p = signal_handle(type, val, pv, sp, ra, xpc, _p);
202 _mc->gregs[REG_EAX] = (intptr_t) p;
203 _mc->gregs[REG_ECX] = (intptr_t) xpc; /* REG_ITMP2_XPC */
204 _mc->gregs[REG_EIP] = (intptr_t) asm_handle_exception;
208 /* md_signal_handler_sigill ****************************************************
210 Signal handler for hardware patcher traps (ud2).
212 *******************************************************************************/
214 void md_signal_handler_sigill(int sig, siginfo_t *siginfo, void *_p)
226 _uc = (ucontext_t *) _p;
227 _mc = &_uc->uc_mcontext;
229 pv = NULL; /* is resolved during stackframeinfo creation */
230 sp = (u1 *) _mc->gregs[REG_ESP];
231 xpc = (u1 *) _mc->gregs[REG_EIP];
232 ra = xpc; /* return address is equal to xpc */
234 /* this is an ArithmeticException */
236 type = EXCEPTION_HARDWARE_PATCHER;
239 /* generate appropriate exception */
241 p = signal_handle(type, val, pv, sp, ra, xpc, _p);
243 /* set registers (only if exception object ready) */
246 _mc->gregs[REG_EAX] = (ptrint) p;
247 _mc->gregs[REG_ECX] = (ptrint) xpc; /* REG_ITMP2_XPC */
248 _mc->gregs[REG_EIP] = (ptrint) 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;
361 /* md_critical_section_restart *************************************************
363 Search the critical sections tree for a matching section and set
364 the PC to the restart point, if necessary.
366 *******************************************************************************/
368 #if defined(ENABLE_THREADS)
369 void md_critical_section_restart(ucontext_t *_uc)
375 _mc = &_uc->uc_mcontext;
377 pc = (u1 *) _mc->gregs[REG_EIP];
379 npc = critical_find_restart_point(pc);
382 _mc->gregs[REG_EIP] = (ptrint) npc;
388 * These are local overrides for various environment variables in Emacs.
389 * Please do not remove this and leave it at the end of the file, where
390 * Emacs will automagically detect them.
391 * ---------------------------------------------------------------------
394 * indent-tabs-mode: t