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.h"
45 #include "vm/jit/asmpart.h"
46 #include "vm/jit/executionstate.h"
47 #include "vm/jit/trap.h"
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)
75 _uc = (ucontext_t *) _p;
76 _mc = &_uc->uc_mcontext;
78 /* ATTENTION: Don't use CACAO's internal REG_* defines as they are
79 different to the ones in <ucontext.h>. */
81 pv = NULL; /* is resolved during stackframeinfo creation */
82 sp = (u1 *) _mc->gregs[REG_RSP];
83 xpc = (u1 *) _mc->gregs[REG_RIP];
84 ra = xpc; /* return address is equal to XPC */
87 /* check for StackOverflowException */
89 threads_check_stackoverflow(sp);
92 /* get exception-throwing instruction */
94 opc = M_ALD_MEM_GET_OPC(xpc);
95 mod = M_ALD_MEM_GET_MOD(xpc);
96 rm = M_ALD_MEM_GET_RM(xpc);
98 /* for values see emit_mov_mem_reg and emit_mem */
100 if ((opc == 0x8b) && (mod == 0) && (rm == 4)) {
101 /* this was a hardware-exception */
103 d = M_ALD_MEM_GET_REG(xpc);
104 disp = M_ALD_MEM_GET_DISP(xpc);
106 /* we use the exception type as load displacement */
112 /* ATTENTION: The _mc->gregs layout is even worse than on
113 i386! See /usr/include/sys/ucontext.h. We need a
114 switch-case here... */
117 case 0: /* REG_RAX == 13 */
120 case 1: /* REG_RCX == 14 */
123 case 2: /* REG_RDX == 12 */
126 case 3: /* REG_RBX == 11 */
129 case 4: /* REG_RSP == 15 */
132 case 5: /* REG_RBP == 10 */
135 case 6: /* REG_RSI == 9 */
138 case 7: /* REG_RDI == 8 */
141 case 8: /* REG_R8 == 0 */
142 case 9: /* REG_R9 == 1 */
143 case 10: /* REG_R10 == 2 */
144 case 11: /* REG_R11 == 3 */
145 case 12: /* REG_R12 == 4 */
146 case 13: /* REG_R13 == 5 */
147 case 14: /* REG_R14 == 6 */
148 case 15: /* REG_R15 == 7 */
155 if (type == TRAP_COMPILER) {
156 /* The PV from the compiler stub is equal to the XPC. */
160 /* We use a framesize of zero here because the call pushed
161 the return addres onto the stack. */
163 ra = md_stacktrace_get_returnaddress(sp, 0);
165 /* Skip the RA on the stack. */
167 sp = sp + 1 * SIZEOF_VOID_P;
169 /* The XPC is the RA minus 1, because the RA points to the
170 instruction after the call. */
176 /* this was a normal NPE */
178 type = TRAP_NullPointerException;
182 /* Handle the trap. */
184 p = trap_handle(type, val, pv, sp, ra, xpc, _p);
188 if (type == TRAP_COMPILER) {
190 o = builtin_retrieve_exception();
192 _mc->gregs[REG_RSP] = (uintptr_t) sp; /* Remove RA from stack. */
194 _mc->gregs[REG_RAX] = (uintptr_t) o;
195 _mc->gregs[REG_R10] = (uintptr_t) xpc; /* REG_ITMP2_XPC */
196 _mc->gregs[REG_RIP] = (uintptr_t) asm_handle_exception;
199 _mc->gregs[REG_RIP] = (uintptr_t) p;
203 _mc->gregs[REG_RAX] = (uintptr_t) p;
204 _mc->gregs[REG_R10] = (uintptr_t) xpc; /* REG_ITMP2_XPC */
205 _mc->gregs[REG_RIP] = (uintptr_t) asm_handle_exception;
210 /* md_signal_handler_sigfpe ****************************************************
212 ArithmeticException signal handler for hardware divide by zero
215 *******************************************************************************/
217 void md_signal_handler_sigfpe(int sig, siginfo_t *siginfo, void *_p)
229 _uc = (ucontext_t *) _p;
230 _mc = &_uc->uc_mcontext;
232 /* ATTENTION: Don't use CACAO's internal REG_* defines as they are
233 different to the ones in <ucontext.h>. */
236 sp = (u1 *) _mc->gregs[REG_RSP];
237 xpc = (u1 *) _mc->gregs[REG_RIP];
238 ra = xpc; /* return address is equal to xpc */
240 /* This is an ArithmeticException. */
242 type = TRAP_ArithmeticException;
245 /* Handle the trap. */
247 p = trap_handle(type, val, pv, sp, ra, xpc, _p);
251 _mc->gregs[REG_RAX] = (intptr_t) p;
252 _mc->gregs[REG_R10] = (intptr_t) xpc; /* REG_ITMP2_XPC */
253 _mc->gregs[REG_RIP] = (intptr_t) asm_handle_exception;
257 /* md_signal_handler_sigill ****************************************************
259 Signal handler for patchers.
261 *******************************************************************************/
263 void md_signal_handler_sigill(int sig, siginfo_t *siginfo, void *_p)
275 _uc = (ucontext_t *) _p;
276 _mc = &_uc->uc_mcontext;
278 /* ATTENTION: Don't use CACAO's internal REG_* defines as they are
279 different to the ones in <ucontext.h>. */
282 sp = (u1 *) _mc->gregs[REG_RSP];
283 xpc = (u1 *) _mc->gregs[REG_RIP];
284 ra = xpc; /* return address is equal to xpc */
286 /* This is a patcher. */
291 /* Handle the trap. */
293 p = trap_handle(type, val, pv, sp, ra, xpc, _p);
298 _mc->gregs[REG_RAX] = (intptr_t) p;
299 _mc->gregs[REG_R10] = (intptr_t) xpc; /* REG_ITMP2_XPC */
300 _mc->gregs[REG_RIP] = (intptr_t) asm_handle_exception;
305 /* md_signal_handler_sigusr1 ***************************************************
307 Signal handler for suspending threads.
309 *******************************************************************************/
311 #if defined(ENABLE_THREADS) && defined(ENABLE_GC_CACAO)
312 void md_signal_handler_sigusr1(int sig, siginfo_t *siginfo, void *_p)
319 _uc = (ucontext_t *) _p;
320 _mc = &_uc->uc_mcontext;
322 /* ATTENTION: Don't use CACAO's internal REG_* defines as they are
323 different to the ones in <ucontext.h>. */
325 /* get the PC and SP for this thread */
326 pc = (u1 *) _mc->gregs[REG_RIP];
327 sp = (u1 *) _mc->gregs[REG_RSP];
329 /* now suspend the current thread */
330 threads_suspend_ack(pc, sp);
335 /* md_signal_handler_sigusr2 ***************************************************
337 Signal handler for profiling sampling.
339 *******************************************************************************/
341 #if defined(ENABLE_THREADS)
342 void md_signal_handler_sigusr2(int sig, siginfo_t *siginfo, void *_p)
351 _uc = (ucontext_t *) _p;
352 _mc = &_uc->uc_mcontext;
354 /* ATTENTION: Don't use CACAO's internal REG_* defines as they are
355 different to the ones in <ucontext.h>. */
357 pc = (u1 *) _mc->gregs[REG_RIP];
364 /* md_executionstate_read ******************************************************
366 Read the given context into an executionstate.
368 *******************************************************************************/
370 void md_executionstate_read(executionstate_t *es, void *context)
377 _uc = (ucontext_t *) context;
378 _mc = &_uc->uc_mcontext;
380 /* read special registers */
381 es->pc = (u1 *) _mc->gregs[REG_RIP];
382 es->sp = (u1 *) _mc->gregs[REG_RSP];
385 /* read integer registers */
386 for (i = 0; i < INT_REG_CNT; i++) {
390 case 0: /* REG_RAX == 13 */
393 case 1: /* REG_RCX == 14 */
396 case 2: /* REG_RDX == 12 */
399 case 3: /* REG_RBX == 11 */
402 case 4: /* REG_RSP == 15 */
405 case 5: /* REG_RBP == 10 */
408 case 6: /* REG_RSI == 9 */
411 case 7: /* REG_RDI == 8 */
414 case 8: /* REG_R8 == 0 */
415 case 9: /* REG_R9 == 1 */
416 case 10: /* REG_R10 == 2 */
417 case 11: /* REG_R11 == 3 */
418 case 12: /* REG_R12 == 4 */
419 case 13: /* REG_R13 == 5 */
420 case 14: /* REG_R14 == 6 */
421 case 15: /* REG_R15 == 7 */
426 es->intregs[i] = _mc->gregs[d];
429 /* read float registers */
430 for (i = 0; i < FLT_REG_CNT; i++)
431 es->fltregs[i] = 0xdeadbeefdeadbeefL;
435 /* md_executionstate_write *****************************************************
437 Write the given executionstate back to the context.
439 *******************************************************************************/
441 void md_executionstate_write(executionstate_t *es, void *context)
448 _uc = (ucontext_t *) context;
449 _mc = &_uc->uc_mcontext;
451 /* write integer registers */
452 for (i = 0; i < INT_REG_CNT; i++) {
456 case 0: /* REG_RAX == 13 */
459 case 1: /* REG_RCX == 14 */
462 case 2: /* REG_RDX == 12 */
465 case 3: /* REG_RBX == 11 */
468 case 4: /* REG_RSP == 15 */
471 case 5: /* REG_RBP == 10 */
474 case 6: /* REG_RSI == 9 */
477 case 7: /* REG_RDI == 8 */
480 case 8: /* REG_R8 == 0 */
481 case 9: /* REG_R9 == 1 */
482 case 10: /* REG_R10 == 2 */
483 case 11: /* REG_R11 == 3 */
484 case 12: /* REG_R12 == 4 */
485 case 13: /* REG_R13 == 5 */
486 case 14: /* REG_R14 == 6 */
487 case 15: /* REG_R15 == 7 */
492 _mc->gregs[d] = es->intregs[i];
495 /* write special registers */
496 _mc->gregs[REG_RIP] = (ptrint) es->pc;
497 _mc->gregs[REG_RSP] = (ptrint) es->sp;
502 * These are local overrides for various environment variables in Emacs.
503 * Please do not remove this and leave it at the end of the file, where
504 * Emacs will automagically detect them.
505 * ---------------------------------------------------------------------
508 * indent-tabs-mode: t
512 * vim:noexpandtab:sw=4:ts=4: