1 /* src/vm/jit/i386/linux/md-os.c - machine dependent i386 Linux functions
3 Copyright (C) 1996-2005, 2006, 2007, 2008
4 CACAOVM - Verein zur Foerderung der freien virtuellen Maschine CACAO
6 This file is part of CACAO.
8 This program is free software; you can redistribute it and/or
9 modify it under the terms of the GNU General Public License as
10 published by the Free Software Foundation; either version 2, or (at
11 your option) any later version.
13 This program is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
26 #define _GNU_SOURCE /* include REG_ defines from ucontext.h */
35 #include "vm/jit/i386/codegen.h"
36 #include "vm/jit/i386/md.h"
38 #include "threads/thread.h"
40 #include "vm/builtin.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/executionstate.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];
107 if (type == EXCEPTION_HARDWARE_COMPILER) {
108 /* The PV from the compiler stub is equal to the XPC. */
112 /* We use a framesize of zero here because the call pushed
113 the return addres onto the stack. */
115 ra = md_stacktrace_get_returnaddress(sp, 0);
117 /* Skip the RA on the stack. */
119 sp = sp + 1 * SIZEOF_VOID_P;
121 /* The XPC is the RA minus 2, because the RA points to the
122 instruction after the call. */
128 /* this was a normal NPE */
130 type = EXCEPTION_HARDWARE_NULLPOINTER;
134 /* Handle the type. */
136 p = signal_handle(type, val, pv, sp, ra, xpc, _p);
140 if (type == EXCEPTION_HARDWARE_COMPILER) {
142 o = builtin_retrieve_exception();
144 _mc->gregs[REG_ESP] = (uintptr_t) sp; /* Remove RA from stack. */
146 _mc->gregs[REG_EAX] = (uintptr_t) o;
147 _mc->gregs[REG_ECX] = (uintptr_t) xpc; /* REG_ITMP2_XPC */
148 _mc->gregs[REG_EIP] = (uintptr_t) asm_handle_exception;
151 _mc->gregs[REG_EIP] = (uintptr_t) p;
155 _mc->gregs[REG_EAX] = (intptr_t) p;
156 _mc->gregs[REG_ECX] = (intptr_t) xpc; /* REG_ITMP2_XPC */
157 _mc->gregs[REG_EIP] = (intptr_t) asm_handle_exception;
162 /* md_signal_handler_sigfpe ****************************************************
164 ArithmeticException signal handler for hardware divide by zero
167 *******************************************************************************/
169 void md_signal_handler_sigfpe(int sig, siginfo_t *siginfo, void *_p)
181 _uc = (ucontext_t *) _p;
182 _mc = &_uc->uc_mcontext;
184 pv = NULL; /* is resolved during stackframeinfo creation */
185 sp = (u1 *) _mc->gregs[REG_ESP];
186 xpc = (u1 *) _mc->gregs[REG_EIP];
187 ra = xpc; /* return address is equal to xpc */
189 /* this is an ArithmeticException */
191 type = EXCEPTION_HARDWARE_ARITHMETIC;
194 /* Handle the type. */
196 p = signal_handle(type, val, pv, sp, ra, xpc, _p);
200 _mc->gregs[REG_EAX] = (intptr_t) p;
201 _mc->gregs[REG_ECX] = (intptr_t) xpc; /* REG_ITMP2_XPC */
202 _mc->gregs[REG_EIP] = (intptr_t) asm_handle_exception;
206 /* md_signal_handler_sigill ****************************************************
208 Signal handler for hardware patcher traps (ud2).
210 *******************************************************************************/
212 void md_signal_handler_sigill(int sig, siginfo_t *siginfo, void *_p)
224 _uc = (ucontext_t *) _p;
225 _mc = &_uc->uc_mcontext;
227 pv = NULL; /* is resolved during stackframeinfo creation */
228 sp = (u1 *) _mc->gregs[REG_ESP];
229 xpc = (u1 *) _mc->gregs[REG_EIP];
230 ra = xpc; /* return address is equal to xpc */
232 /* this is an ArithmeticException */
234 type = EXCEPTION_HARDWARE_PATCHER;
237 /* generate appropriate exception */
239 p = signal_handle(type, val, pv, sp, ra, xpc, _p);
241 /* set registers (only if exception object ready) */
244 _mc->gregs[REG_EAX] = (ptrint) p;
245 _mc->gregs[REG_ECX] = (ptrint) xpc; /* REG_ITMP2_XPC */
246 _mc->gregs[REG_EIP] = (ptrint) asm_handle_exception;
251 /* md_signal_handler_sigusr1 ***************************************************
253 Signal handler for suspending threads.
255 *******************************************************************************/
257 #if defined(ENABLE_THREADS) && defined(ENABLE_GC_CACAO)
258 void md_signal_handler_sigusr1(int sig, siginfo_t *siginfo, void *_p)
265 _uc = (ucontext_t *) _p;
266 _mc = &_uc->uc_mcontext;
268 /* get the PC and SP for this thread */
269 pc = (u1 *) _mc->gregs[REG_EIP];
270 sp = (u1 *) _mc->gregs[REG_ESP];
272 /* now suspend the current thread */
273 threads_suspend_ack(pc, sp);
278 /* md_signal_handler_sigusr2 ***************************************************
280 Signal handler for profiling sampling.
282 *******************************************************************************/
284 #if defined(ENABLE_THREADS)
285 void md_signal_handler_sigusr2(int sig, siginfo_t *siginfo, void *_p)
294 _uc = (ucontext_t *) _p;
295 _mc = &_uc->uc_mcontext;
297 pc = (u1 *) _mc->gregs[REG_EIP];
304 /* md_executionstate_read ******************************************************
306 Read the given context into an executionstate for Replacement.
308 *******************************************************************************/
310 void md_executionstate_read(executionstate_t *es, void *context)
316 _uc = (ucontext_t *) context;
317 _mc = &_uc->uc_mcontext;
319 /* read special registers */
320 es->pc = (u1 *) _mc->gregs[REG_EIP];
321 es->sp = (u1 *) _mc->gregs[REG_ESP];
322 es->pv = NULL; /* pv must be looked up via AVL tree */
324 /* read integer registers */
325 for (i = 0; i < INT_REG_CNT; i++)
326 es->intregs[i] = _mc->gregs[REG_EAX - i];
328 /* read float registers */
329 for (i = 0; i < FLT_REG_CNT; i++)
330 es->fltregs[i] = 0xdeadbeefdeadbeefULL;
334 /* md_executionstate_write *****************************************************
336 Write the given executionstate back to the context for Replacement.
338 *******************************************************************************/
340 void md_executionstate_write(executionstate_t *es, void *context)
346 _uc = (ucontext_t *) context;
347 _mc = &_uc->uc_mcontext;
349 /* write integer registers */
350 for (i = 0; i < INT_REG_CNT; i++)
351 _mc->gregs[REG_EAX - i] = es->intregs[i];
353 /* write special registers */
354 _mc->gregs[REG_EIP] = (ptrint) es->pc;
355 _mc->gregs[REG_ESP] = (ptrint) es->sp;
359 /* md_critical_section_restart *************************************************
361 Search the critical sections tree for a matching section and set
362 the PC to the restart point, if necessary.
364 *******************************************************************************/
366 #if defined(ENABLE_THREADS)
367 void md_critical_section_restart(ucontext_t *_uc)
373 _mc = &_uc->uc_mcontext;
375 pc = (u1 *) _mc->gregs[REG_EIP];
377 npc = critical_find_restart_point(pc);
380 _mc->gregs[REG_EIP] = (ptrint) npc;
386 * These are local overrides for various environment variables in Emacs.
387 * Please do not remove this and leave it at the end of the file, where
388 * Emacs will automagically detect them.
389 * ---------------------------------------------------------------------
392 * indent-tabs-mode: t