1 /* src/vm/jit/x86_64/linux/md-os.c - machine dependent x86_64 Linux functions
3 Copyright (C) 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
37 #include "vm/jit/x86_64/codegen.h"
38 #include "vm/jit/x86_64/md.h"
40 #include "threads/thread.hpp"
42 #include "vm/jit/builtin.hpp"
43 #include "vm/signallocal.hpp"
45 #include "vm/jit/asmpart.h"
46 #include "vm/jit/executionstate.h"
47 #include "vm/jit/trap.hpp"
48 #include "vm/jit/stacktrace.hpp"
51 /* md_signal_handler_sigsegv ***************************************************
53 Signal handler for hardware exception.
55 *******************************************************************************/
57 void md_signal_handler_sigsegv(int sig, siginfo_t *siginfo, void *_p)
74 _uc = (ucontext_t *) _p;
75 _mc = &_uc->uc_mcontext;
77 /* ATTENTION: Don't use CACAO's internal REG_* defines as they are
78 different to the ones in <ucontext.h>. */
80 pv = NULL; /* is resolved during stackframeinfo creation */
81 sp = (u1 *) _mc->gregs[REG_RSP];
82 xpc = (u1 *) _mc->gregs[REG_RIP];
83 ra = xpc; /* return address is equal to XPC */
86 /* check for StackOverflowException */
88 threads_check_stackoverflow(sp);
91 /* get exception-throwing instruction */
93 opc = M_ALD_MEM_GET_OPC(xpc);
94 mod = M_ALD_MEM_GET_MOD(xpc);
95 rm = M_ALD_MEM_GET_RM(xpc);
97 /* for values see emit_mov_mem_reg and emit_mem */
99 if ((opc == 0x8b) && (mod == 0) && (rm == 4)) {
100 /* this was a hardware-exception */
102 d = M_ALD_MEM_GET_REG(xpc);
103 disp = M_ALD_MEM_GET_DISP(xpc);
105 /* we use the exception type as load displacement */
111 /* ATTENTION: The _mc->gregs layout is even worse than on
112 i386! See /usr/include/sys/ucontext.h. We need a
113 switch-case here... */
116 case 0: /* REG_RAX == 13 */
119 case 1: /* REG_RCX == 14 */
122 case 2: /* REG_RDX == 12 */
125 case 3: /* REG_RBX == 11 */
128 case 4: /* REG_RSP == 15 */
131 case 5: /* REG_RBP == 10 */
134 case 6: /* REG_RSI == 9 */
137 case 7: /* REG_RDI == 8 */
140 case 8: /* REG_R8 == 0 */
141 case 9: /* REG_R9 == 1 */
142 case 10: /* REG_R10 == 2 */
143 case 11: /* REG_R11 == 3 */
144 case 12: /* REG_R12 == 4 */
145 case 13: /* REG_R13 == 5 */
146 case 14: /* REG_R14 == 6 */
147 case 15: /* REG_R15 == 7 */
154 if (type == TRAP_COMPILER) {
155 /* The PV from the compiler stub is equal to the XPC. */
159 /* We use a framesize of zero here because the call pushed
160 the return addres onto the stack. */
162 ra = md_stacktrace_get_returnaddress(sp, 0);
164 /* Skip the RA on the stack. */
166 sp = sp + 1 * SIZEOF_VOID_P;
168 /* The XPC is the RA minus 1, because the RA points to the
169 instruction after the call. */
175 /* this was a normal NPE */
177 type = TRAP_NullPointerException;
181 /* Handle the trap. */
183 p = trap_handle(type, val, pv, sp, ra, xpc, _p);
187 if (type == TRAP_COMPILER) {
189 _mc->gregs[REG_RSP] = (uintptr_t) sp; /* Remove RA from stack. */
195 /* md_signal_handler_sigfpe ****************************************************
197 ArithmeticException signal handler for hardware divide by zero
200 *******************************************************************************/
202 void md_signal_handler_sigfpe(int sig, siginfo_t *siginfo, void *_p)
213 _uc = (ucontext_t *) _p;
214 _mc = &_uc->uc_mcontext;
216 /* ATTENTION: Don't use CACAO's internal REG_* defines as they are
217 different to the ones in <ucontext.h>. */
220 sp = (u1 *) _mc->gregs[REG_RSP];
221 xpc = (u1 *) _mc->gregs[REG_RIP];
222 ra = xpc; /* return address is equal to xpc */
224 /* This is an ArithmeticException. */
226 type = TRAP_ArithmeticException;
229 /* Handle the trap. */
231 trap_handle(type, val, pv, sp, ra, xpc, _p);
235 /* md_signal_handler_sigill ****************************************************
237 Signal handler for patchers.
239 *******************************************************************************/
241 void md_signal_handler_sigill(int sig, siginfo_t *siginfo, void *_p)
252 _uc = (ucontext_t *) _p;
253 _mc = &_uc->uc_mcontext;
255 /* ATTENTION: Don't use CACAO's internal REG_* defines as they are
256 different to the ones in <ucontext.h>. */
259 sp = (u1 *) _mc->gregs[REG_RSP];
260 xpc = (u1 *) _mc->gregs[REG_RIP];
261 ra = xpc; /* return address is equal to xpc */
263 // Check if the trap instruction is valid.
264 // TODO Move this into patcher_handler.
265 if (patcher_is_valid_trap_instruction_at(xpc) == false) {
266 // Check if the PC has been patched during our way to this
267 // signal handler (see PR85).
268 if (patcher_is_patched_at(xpc) == true)
271 // We have a problem...
272 log_println("md_signal_handler_sigill: Unknown illegal instruction at 0x%lx", xpc);
273 #if defined(ENABLE_DISASSEMBLER)
274 (void) disassinstr(xpc);
276 vm_abort("Aborting...");
279 /* This is a patcher. */
284 /* Handle the trap. */
286 trap_handle(type, val, pv, sp, ra, xpc, _p);
290 /* md_signal_handler_sigusr1 ***************************************************
292 Signal handler for suspending threads.
294 *******************************************************************************/
296 #if defined(ENABLE_THREADS) && defined(ENABLE_GC_CACAO)
297 void md_signal_handler_sigusr1(int sig, siginfo_t *siginfo, void *_p)
304 _uc = (ucontext_t *) _p;
305 _mc = &_uc->uc_mcontext;
307 /* ATTENTION: Don't use CACAO's internal REG_* defines as they are
308 different to the ones in <ucontext.h>. */
310 /* get the PC and SP for this thread */
311 pc = (u1 *) _mc->gregs[REG_RIP];
312 sp = (u1 *) _mc->gregs[REG_RSP];
314 /* now suspend the current thread */
315 threads_suspend_ack(pc, sp);
320 /* md_signal_handler_sigusr2 ***************************************************
322 Signal handler for profiling sampling.
324 *******************************************************************************/
326 #if defined(ENABLE_THREADS)
327 void md_signal_handler_sigusr2(int sig, siginfo_t *siginfo, void *_p)
336 _uc = (ucontext_t *) _p;
337 _mc = &_uc->uc_mcontext;
339 /* ATTENTION: Don't use CACAO's internal REG_* defines as they are
340 different to the ones in <ucontext.h>. */
342 pc = (u1 *) _mc->gregs[REG_RIP];
349 /* md_executionstate_read ******************************************************
351 Read the given context into an executionstate.
353 *******************************************************************************/
355 void md_executionstate_read(executionstate_t *es, void *context)
362 _uc = (ucontext_t *) context;
363 _mc = &_uc->uc_mcontext;
365 /* read special registers */
366 es->pc = (u1 *) _mc->gregs[REG_RIP];
367 es->sp = (u1 *) _mc->gregs[REG_RSP];
370 /* read integer registers */
371 for (i = 0; i < INT_REG_CNT; i++) {
375 case 0: /* REG_RAX == 13 */
378 case 1: /* REG_RCX == 14 */
381 case 2: /* REG_RDX == 12 */
384 case 3: /* REG_RBX == 11 */
387 case 4: /* REG_RSP == 15 */
390 case 5: /* REG_RBP == 10 */
393 case 6: /* REG_RSI == 9 */
396 case 7: /* REG_RDI == 8 */
399 case 8: /* REG_R8 == 0 */
400 case 9: /* REG_R9 == 1 */
401 case 10: /* REG_R10 == 2 */
402 case 11: /* REG_R11 == 3 */
403 case 12: /* REG_R12 == 4 */
404 case 13: /* REG_R13 == 5 */
405 case 14: /* REG_R14 == 6 */
406 case 15: /* REG_R15 == 7 */
411 es->intregs[i] = _mc->gregs[d];
414 /* read float registers */
415 for (i = 0; i < FLT_REG_CNT; i++)
416 es->fltregs[i] = 0xdeadbeefdeadbeefL;
420 /* md_executionstate_write *****************************************************
422 Write the given executionstate back to the context.
424 *******************************************************************************/
426 void md_executionstate_write(executionstate_t *es, void *context)
433 _uc = (ucontext_t *) context;
434 _mc = &_uc->uc_mcontext;
436 /* write integer registers */
437 for (i = 0; i < INT_REG_CNT; i++) {
441 case 0: /* REG_RAX == 13 */
444 case 1: /* REG_RCX == 14 */
447 case 2: /* REG_RDX == 12 */
450 case 3: /* REG_RBX == 11 */
453 case 4: /* REG_RSP == 15 */
456 case 5: /* REG_RBP == 10 */
459 case 6: /* REG_RSI == 9 */
462 case 7: /* REG_RDI == 8 */
465 case 8: /* REG_R8 == 0 */
466 case 9: /* REG_R9 == 1 */
467 case 10: /* REG_R10 == 2 */
468 case 11: /* REG_R11 == 3 */
469 case 12: /* REG_R12 == 4 */
470 case 13: /* REG_R13 == 5 */
471 case 14: /* REG_R14 == 6 */
472 case 15: /* REG_R15 == 7 */
477 _mc->gregs[d] = es->intregs[i];
480 /* write special registers */
481 _mc->gregs[REG_RIP] = (ptrint) es->pc;
482 _mc->gregs[REG_RSP] = (ptrint) es->sp;
487 * These are local overrides for various environment variables in Emacs.
488 * Please do not remove this and leave it at the end of the file, where
489 * Emacs will automagically detect them.
490 * ---------------------------------------------------------------------
493 * indent-tabs-mode: t
497 * vim:noexpandtab:sw=4:ts=4: