1 /* src/vm/jit/i386/freebsd/md-os.c - machine dependent i386 FreeBSD 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
33 #include "vm/jit/i386/codegen.h"
34 #include "vm/jit/i386/md.h"
36 #include "threads/thread.hpp"
38 #include "vm/jit/builtin.hpp"
39 #include "vm/signallocal.hpp"
41 #include "vm/jit/asmpart.h"
42 #include "vm/jit/executionstate.h"
43 #include "vm/jit/stacktrace.hpp"
44 #include "vm/jit/trap.hpp"
47 /* md_signal_handler_sigsegv ***************************************************
49 Signal handler for hardware exceptions.
51 *******************************************************************************/
53 void md_signal_handler_sigsegv(int sig, siginfo_t *siginfo, void *_p)
70 _uc = (ucontext_t *) _p;
71 _mc = &_uc->uc_mcontext;
73 pv = NULL; /* is resolved during stackframeinfo creation */
74 sp = (u1 *) _mc->mc_esp;
75 xpc = (u1 *) _mc->mc_eip;
76 ra = xpc; /* return address is equal to XPC */
78 /* get exception-throwing instruction */
80 opc = M_ALD_MEM_GET_OPC(xpc);
81 mod = M_ALD_MEM_GET_MOD(xpc);
82 rm = M_ALD_MEM_GET_RM(xpc);
84 /* for values see emit_mov_mem_reg and emit_mem */
86 if ((opc == 0x8b) && (mod == 0) && (rm == 5)) {
87 /* this was a hardware-exception */
89 d = M_ALD_MEM_GET_REG(xpc);
90 disp = M_ALD_MEM_GET_DISP(xpc);
92 /* we use the exception type as load displacement */
122 vm_abort("md_signal_handler_sigsegv: Unkown register %d", d);
125 if (type == TRAP_COMPILER) {
126 /* The PV from the compiler stub is equal to the XPC. */
130 /* We use a framesize of zero here because the call pushed
131 the return addres onto the stack. */
133 ra = md_stacktrace_get_returnaddress(sp, 0);
135 /* Skip the RA on the stack. */
137 sp = sp + 1 * SIZEOF_VOID_P;
139 /* The XPC is the RA minus 2, because the RA points to the
140 instruction after the call. */
146 /* this was a normal NPE */
148 type = TRAP_NullPointerException;
152 /* Handle the trap. */
154 p = trap_handle(type, val, pv, sp, ra, xpc, _p);
158 if (type == TRAP_COMPILER) {
160 _mc->mc_esp = (uintptr_t) sp; // Remove RA from stack.
166 /* md_signal_handler_sigfpe ****************************************************
168 ArithmeticException signal handler for hardware divide by zero
171 *******************************************************************************/
173 void md_signal_handler_sigfpe(int sig, siginfo_t *siginfo, void *_p)
184 _uc = (ucontext_t *) _p;
185 _mc = &_uc->uc_mcontext;
187 pv = NULL; /* is resolved during stackframeinfo creation */
188 sp = (u1 *) _mc->mc_esp;
189 xpc = (u1 *) _mc->mc_eip;
190 ra = xpc; /* return address is equal to xpc */
192 /* This is an ArithmeticException. */
194 type = TRAP_ArithmeticException;
197 /* Handle the trap. */
199 trap_handle(type, val, pv, sp, ra, xpc, _p);
203 /* md_signal_handler_sigill ****************************************************
205 Signal handler for hardware patcher traps (ud2).
207 *******************************************************************************/
209 void md_signal_handler_sigill(int sig, siginfo_t *siginfo, void *_p)
220 _uc = (ucontext_t *) _p;
221 _mc = &_uc->uc_mcontext;
223 pv = NULL; /* is resolved during stackframeinfo creation */
224 sp = (u1 *) _mc->mc_esp;
225 xpc = (u1 *) _mc->mc_eip;
226 ra = xpc; /* return address is equal to xpc */
228 // Check if the trap instruction is valid.
229 // TODO Move this into patcher_handler.
230 if (patcher_is_valid_trap_instruction_at(xpc) == false) {
231 // Check if the PC has been patched during our way to this
232 // signal handler (see PR85).
233 if (patcher_is_patched_at(xpc) == true)
236 // We have a problem...
237 log_println("md_signal_handler_sigill: Unknown illegal instruction at 0x%lx", xpc);
238 #if defined(ENABLE_DISASSEMBLER)
239 (void) disassinstr(xpc);
241 vm_abort("Aborting...");
247 /* Handle the trap. */
249 trap_handle(type, val, pv, sp, ra, xpc, _p);
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->mc_eip;
272 sp = (u1 *) _mc->mc_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->mc_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)
314 ucontext_t* _uc = (ucontext_t*) context;
315 mcontext_t* _mc = &_uc->uc_mcontext;
317 // Read special registers.
318 es->pc = (u1 *) _mc->mc_eip;
319 es->sp = (u1 *) _mc->mc_esp;
320 es->pv = NULL; /* pv must be looked up via AVL tree */
322 // Read integer registers.
323 es->intregs[EAX] = _mc->mc_eax;
324 es->intregs[ECX] = _mc->mc_ecx;
325 es->intregs[EDX] = _mc->mc_edx;
326 es->intregs[EBX] = _mc->mc_ebx;
327 es->intregs[ESP] = _mc->mc_esp;
328 es->intregs[EBP] = _mc->mc_ebp;
329 es->intregs[ESI] = _mc->mc_esi;
330 es->intregs[EDI] = _mc->mc_edi;
332 // Read float registers.
333 for (int i = 0; i < FLT_REG_CNT; i++)
334 es->fltregs[i] = 0xdeadbeefdeadbeefULL;
338 /* md_executionstate_write *****************************************************
340 Write the given executionstate back to the context for Replacement.
342 *******************************************************************************/
344 void md_executionstate_write(executionstate_t* es, void* context)
346 ucontext_t* _uc = (ucontext_t*) context;
347 mcontext_t* _mc = &_uc->uc_mcontext;
349 // Write integer registers.
350 _mc->mc_eax = es->intregs[EAX];
351 _mc->mc_ecx = es->intregs[ECX];
352 _mc->mc_edx = es->intregs[EDX];
353 _mc->mc_ebx = es->intregs[EBX];
354 _mc->mc_esp = es->intregs[ESP];
355 _mc->mc_ebp = es->intregs[EBP];
356 _mc->mc_esi = es->intregs[ESI];
357 _mc->mc_edi = es->intregs[EDI];
359 // Write special registers.
360 _mc->mc_eip = (uintptr_t) es->pc;
361 _mc->mc_esp = (uintptr_t) es->sp;
366 * These are local overrides for various environment variables in Emacs.
367 * Please do not remove this and leave it at the end of the file, where
368 * Emacs will automagically detect them.
369 * ---------------------------------------------------------------------
372 * indent-tabs-mode: t