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)
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 /* This is a patcher. */
268 /* Handle the trap. */
270 trap_handle(type, val, pv, sp, ra, xpc, _p);
274 /* md_signal_handler_sigusr1 ***************************************************
276 Signal handler for suspending threads.
278 *******************************************************************************/
280 #if defined(ENABLE_THREADS) && defined(ENABLE_GC_CACAO)
281 void md_signal_handler_sigusr1(int sig, siginfo_t *siginfo, void *_p)
288 _uc = (ucontext_t *) _p;
289 _mc = &_uc->uc_mcontext;
291 /* ATTENTION: Don't use CACAO's internal REG_* defines as they are
292 different to the ones in <ucontext.h>. */
294 /* get the PC and SP for this thread */
295 pc = (u1 *) _mc->gregs[REG_RIP];
296 sp = (u1 *) _mc->gregs[REG_RSP];
298 /* now suspend the current thread */
299 threads_suspend_ack(pc, sp);
304 /* md_signal_handler_sigusr2 ***************************************************
306 Signal handler for profiling sampling.
308 *******************************************************************************/
310 #if defined(ENABLE_THREADS)
311 void md_signal_handler_sigusr2(int sig, siginfo_t *siginfo, void *_p)
320 _uc = (ucontext_t *) _p;
321 _mc = &_uc->uc_mcontext;
323 /* ATTENTION: Don't use CACAO's internal REG_* defines as they are
324 different to the ones in <ucontext.h>. */
326 pc = (u1 *) _mc->gregs[REG_RIP];
333 /* md_executionstate_read ******************************************************
335 Read the given context into an executionstate.
337 *******************************************************************************/
339 void md_executionstate_read(executionstate_t *es, void *context)
346 _uc = (ucontext_t *) context;
347 _mc = &_uc->uc_mcontext;
349 /* read special registers */
350 es->pc = (u1 *) _mc->gregs[REG_RIP];
351 es->sp = (u1 *) _mc->gregs[REG_RSP];
354 /* read integer registers */
355 for (i = 0; i < INT_REG_CNT; i++) {
359 case 0: /* REG_RAX == 13 */
362 case 1: /* REG_RCX == 14 */
365 case 2: /* REG_RDX == 12 */
368 case 3: /* REG_RBX == 11 */
371 case 4: /* REG_RSP == 15 */
374 case 5: /* REG_RBP == 10 */
377 case 6: /* REG_RSI == 9 */
380 case 7: /* REG_RDI == 8 */
383 case 8: /* REG_R8 == 0 */
384 case 9: /* REG_R9 == 1 */
385 case 10: /* REG_R10 == 2 */
386 case 11: /* REG_R11 == 3 */
387 case 12: /* REG_R12 == 4 */
388 case 13: /* REG_R13 == 5 */
389 case 14: /* REG_R14 == 6 */
390 case 15: /* REG_R15 == 7 */
395 es->intregs[i] = _mc->gregs[d];
398 /* read float registers */
399 for (i = 0; i < FLT_REG_CNT; i++)
400 es->fltregs[i] = 0xdeadbeefdeadbeefL;
404 /* md_executionstate_write *****************************************************
406 Write the given executionstate back to the context.
408 *******************************************************************************/
410 void md_executionstate_write(executionstate_t *es, void *context)
417 _uc = (ucontext_t *) context;
418 _mc = &_uc->uc_mcontext;
420 /* write integer registers */
421 for (i = 0; i < INT_REG_CNT; i++) {
425 case 0: /* REG_RAX == 13 */
428 case 1: /* REG_RCX == 14 */
431 case 2: /* REG_RDX == 12 */
434 case 3: /* REG_RBX == 11 */
437 case 4: /* REG_RSP == 15 */
440 case 5: /* REG_RBP == 10 */
443 case 6: /* REG_RSI == 9 */
446 case 7: /* REG_RDI == 8 */
449 case 8: /* REG_R8 == 0 */
450 case 9: /* REG_R9 == 1 */
451 case 10: /* REG_R10 == 2 */
452 case 11: /* REG_R11 == 3 */
453 case 12: /* REG_R12 == 4 */
454 case 13: /* REG_R13 == 5 */
455 case 14: /* REG_R14 == 6 */
456 case 15: /* REG_R15 == 7 */
461 _mc->gregs[d] = es->intregs[i];
464 /* write special registers */
465 _mc->gregs[REG_RIP] = (ptrint) es->pc;
466 _mc->gregs[REG_RSP] = (ptrint) es->sp;
471 * These are local overrides for various environment variables in Emacs.
472 * Please do not remove this and leave it at the end of the file, where
473 * Emacs will automagically detect them.
474 * ---------------------------------------------------------------------
477 * indent-tabs-mode: t
481 * vim:noexpandtab:sw=4:ts=4: