1 /* src/vm/jit/x86_64/linux/md-os.c - machine dependent x86_64 Linux functions
3 Copyright (C) 2007 R. Grafl, A. Krall, C. Kruegel,
4 C. Oates, R. Obermaisser, M. Platter, M. Probst, S. Ring,
5 E. Steiner, C. Thalinger, D. Thuernbeck, P. Tomsich, C. Ullrich,
6 J. Wenninger, Institut f. Computersprachen - TU Wien
8 This file is part of CACAO.
10 This program is free software; you can redistribute it and/or
11 modify it under the terms of the GNU General Public License as
12 published by the Free Software Foundation; either version 2, or (at
13 your option) any later version.
15 This program is distributed in the hope that it will be useful, but
16 WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
39 #include "vm/jit/x86_64/codegen.h"
40 #include "vm/jit/x86_64/md.h"
42 #if defined(ENABLE_THREADS)
43 # include "threads/native/threads.h"
46 #include "vm/builtin.h"
47 #include "vm/exceptions.h"
48 #include "vm/signallocal.h"
50 #include "vm/jit/asmpart.h"
51 #include "vm/jit/stacktrace.h"
54 /* md_signal_handler_sigsegv ***************************************************
56 Signal handler for hardware exception.
58 *******************************************************************************/
60 void md_signal_handler_sigsegv(int sig, siginfo_t *siginfo, void *_p)
78 _uc = (ucontext_t *) _p;
79 _mc = &_uc->uc_mcontext;
81 /* ATTENTION: Don't use CACAO's internal REG_* defines as they are
82 different to the ones in <ucontext.h>. */
84 pv = NULL; /* is resolved during stackframeinfo creation */
85 sp = (u1 *) _mc->gregs[REG_RSP];
86 xpc = (u1 *) _mc->gregs[REG_RIP];
87 ra = xpc; /* return address is equal to XPC */
90 /* check for StackOverflowException */
92 threads_check_stackoverflow(sp);
95 /* get exception-throwing instruction */
97 opc = M_ALD_MEM_GET_OPC(xpc);
98 mod = M_ALD_MEM_GET_MOD(xpc);
99 rm = M_ALD_MEM_GET_RM(xpc);
101 /* for values see emit_mov_mem_reg and emit_mem */
103 if ((opc == 0x8b) && (mod == 0) && (rm == 4)) {
104 /* this was a hardware-exception */
106 d = M_ALD_MEM_GET_REG(xpc);
107 disp = M_ALD_MEM_GET_DISP(xpc);
109 /* we use the exception type as load displacement */
115 /* ATTENTION: The _mc->gregs layout is even worse than on
116 i386! See /usr/include/sys/ucontext.h. We need a
117 switch-case here... */
120 case 0: /* REG_RAX == 13 */
123 case 1: /* REG_RCX == 14 */
126 case 2: /* REG_RDX == 12 */
129 case 3: /* REG_RBX == 11 */
132 case 4: /* REG_RSP == 15 */
135 case 5: /* REG_RBP == 10 */
138 case 6: /* REG_RSI == 9 */
141 case 7: /* REG_RDI == 8 */
144 case 8: /* REG_R8 == 0 */
145 case 9: /* REG_R9 == 1 */
146 case 10: /* REG_R10 == 2 */
147 case 11: /* REG_R11 == 3 */
148 case 12: /* REG_R12 == 4 */
149 case 13: /* REG_R13 == 5 */
150 case 14: /* REG_R14 == 6 */
151 case 15: /* REG_R15 == 7 */
158 if (type == EXCEPTION_HARDWARE_COMPILER) {
159 /* The PV from the compiler stub is equal to the XPC. */
163 /* We use a framesize of zero here because the call pushed
164 the return addres onto the stack. */
166 ra = md_stacktrace_get_returnaddress(sp, 0);
168 /* Skip the RA on the stack. */
170 sp = sp + 1 * SIZEOF_VOID_P;
172 /* The XPC is the RA minus 1, because the RA points to the
173 instruction after the call. */
179 /* this was a normal NPE */
181 type = EXCEPTION_HARDWARE_NULLPOINTER;
185 /* Handle the type. */
187 p = signal_handle(type, val, pv, sp, ra, xpc, _p);
191 if (type == EXCEPTION_HARDWARE_COMPILER) {
193 o = builtin_retrieve_exception();
195 _mc->gregs[REG_RSP] = (uintptr_t) sp; /* Remove RA from stack. */
197 _mc->gregs[REG_RAX] = (uintptr_t) o;
198 _mc->gregs[REG_R10] = (uintptr_t) xpc; /* REG_ITMP2_XPC */
199 _mc->gregs[REG_RIP] = (uintptr_t) asm_handle_exception;
202 _mc->gregs[REG_RIP] = (uintptr_t) p;
206 _mc->gregs[REG_RAX] = (uintptr_t) p;
207 _mc->gregs[REG_R10] = (uintptr_t) xpc; /* REG_ITMP2_XPC */
208 _mc->gregs[REG_RIP] = (uintptr_t) asm_handle_exception;
213 /* md_signal_handler_sigfpe ****************************************************
215 ArithmeticException signal handler for hardware divide by zero
218 *******************************************************************************/
220 void md_signal_handler_sigfpe(int sig, siginfo_t *siginfo, void *_p)
232 _uc = (ucontext_t *) _p;
233 _mc = &_uc->uc_mcontext;
235 /* ATTENTION: Don't use CACAO's internal REG_* defines as they are
236 different to the ones in <ucontext.h>. */
239 sp = (u1 *) _mc->gregs[REG_RSP];
240 xpc = (u1 *) _mc->gregs[REG_RIP];
241 ra = xpc; /* return address is equal to xpc */
243 /* this is an ArithmeticException */
245 type = EXCEPTION_HARDWARE_ARITHMETIC;
248 /* Handle the type. */
250 p = signal_handle(type, val, pv, sp, ra, xpc, _p);
254 _mc->gregs[REG_RAX] = (intptr_t) p;
255 _mc->gregs[REG_R10] = (intptr_t) xpc; /* REG_ITMP2_XPC */
256 _mc->gregs[REG_RIP] = (intptr_t) asm_handle_exception;
260 /* md_signal_handler_sigill ****************************************************
262 Signal handler for patchers.
264 *******************************************************************************/
266 void md_signal_handler_sigill(int sig, siginfo_t *siginfo, void *_p)
278 _uc = (ucontext_t *) _p;
279 _mc = &_uc->uc_mcontext;
281 /* ATTENTION: Don't use CACAO's internal REG_* defines as they are
282 different to the ones in <ucontext.h>. */
285 sp = (u1 *) _mc->gregs[REG_RSP];
286 xpc = (u1 *) _mc->gregs[REG_RIP];
287 ra = xpc; /* return address is equal to xpc */
289 /* This is a patcher. */
291 type = EXCEPTION_HARDWARE_PATCHER;
294 /* Handle the type. */
296 p = signal_handle(type, val, pv, sp, ra, xpc, _p);
301 _mc->gregs[REG_RAX] = (intptr_t) p;
302 _mc->gregs[REG_R10] = (intptr_t) xpc; /* REG_ITMP2_XPC */
303 _mc->gregs[REG_RIP] = (intptr_t) asm_handle_exception;
308 /* md_signal_handler_sigusr1 ***************************************************
310 Signal handler for suspending threads.
312 *******************************************************************************/
314 #if defined(ENABLE_THREADS) && defined(ENABLE_GC_CACAO)
315 void md_signal_handler_sigusr1(int sig, siginfo_t *siginfo, void *_p)
322 _uc = (ucontext_t *) _p;
323 _mc = &_uc->uc_mcontext;
325 /* ATTENTION: Don't use CACAO's internal REG_* defines as they are
326 different to the ones in <ucontext.h>. */
328 /* get the PC and SP for this thread */
329 pc = (u1 *) _mc->gregs[REG_RIP];
330 sp = (u1 *) _mc->gregs[REG_RSP];
332 /* now suspend the current thread */
333 threads_suspend_ack(pc, sp);
338 /* md_signal_handler_sigusr2 ***************************************************
340 Signal handler for profiling sampling.
342 *******************************************************************************/
344 #if defined(ENABLE_THREADS)
345 void md_signal_handler_sigusr2(int sig, siginfo_t *siginfo, void *_p)
354 _uc = (ucontext_t *) _p;
355 _mc = &_uc->uc_mcontext;
357 /* ATTENTION: Don't use CACAO's internal REG_* defines as they are
358 different to the ones in <ucontext.h>. */
360 pc = (u1 *) _mc->gregs[REG_RIP];
367 /* md_replace_executionstate_read **********************************************
369 Read the given context into an executionstate for Replacement.
371 *******************************************************************************/
373 #if defined(ENABLE_REPLACEMENT)
374 void md_replace_executionstate_read(executionstate_t *es, void *context)
381 _uc = (ucontext_t *) context;
382 _mc = &_uc->uc_mcontext;
384 /* read special registers */
385 es->pc = (u1 *) _mc->gregs[REG_RIP];
386 es->sp = (u1 *) _mc->gregs[REG_RSP];
389 /* read integer registers */
390 for (i = 0; i < INT_REG_CNT; i++) {
394 case 0: /* REG_RAX == 13 */
397 case 1: /* REG_RCX == 14 */
400 case 2: /* REG_RDX == 12 */
403 case 3: /* REG_RBX == 11 */
406 case 4: /* REG_RSP == 15 */
409 case 5: /* REG_RBP == 10 */
412 case 6: /* REG_RSI == 9 */
415 case 7: /* REG_RDI == 8 */
418 case 8: /* REG_R8 == 0 */
419 case 9: /* REG_R9 == 1 */
420 case 10: /* REG_R10 == 2 */
421 case 11: /* REG_R11 == 3 */
422 case 12: /* REG_R12 == 4 */
423 case 13: /* REG_R13 == 5 */
424 case 14: /* REG_R14 == 6 */
425 case 15: /* REG_R15 == 7 */
430 es->intregs[i] = _mc->gregs[d];
433 /* read float registers */
434 for (i = 0; i < FLT_REG_CNT; i++)
435 es->fltregs[i] = 0xdeadbeefdeadbeefL;
440 /* md_replace_executionstate_write *********************************************
442 Write the given executionstate back to the context for Replacement.
444 *******************************************************************************/
446 #if defined(ENABLE_REPLACEMENT)
447 void md_replace_executionstate_write(executionstate_t *es, void *context)
454 _uc = (ucontext_t *) context;
455 _mc = &_uc->uc_mcontext;
457 /* write integer registers */
458 for (i = 0; i < INT_REG_CNT; i++) {
462 case 0: /* REG_RAX == 13 */
465 case 1: /* REG_RCX == 14 */
468 case 2: /* REG_RDX == 12 */
471 case 3: /* REG_RBX == 11 */
474 case 4: /* REG_RSP == 15 */
477 case 5: /* REG_RBP == 10 */
480 case 6: /* REG_RSI == 9 */
483 case 7: /* REG_RDI == 8 */
486 case 8: /* REG_R8 == 0 */
487 case 9: /* REG_R9 == 1 */
488 case 10: /* REG_R10 == 2 */
489 case 11: /* REG_R11 == 3 */
490 case 12: /* REG_R12 == 4 */
491 case 13: /* REG_R13 == 5 */
492 case 14: /* REG_R14 == 6 */
493 case 15: /* REG_R15 == 7 */
498 _mc->gregs[d] = es->intregs[i];
501 /* write special registers */
502 _mc->gregs[REG_RIP] = (ptrint) es->pc;
503 _mc->gregs[REG_RSP] = (ptrint) es->sp;
508 /* md_critical_section_restart *************************************************
510 Search the critical sections tree for a matching section and set
511 the PC to the restart point, if necessary.
513 *******************************************************************************/
515 #if defined(ENABLE_THREADS)
516 void md_critical_section_restart(ucontext_t *_uc)
522 _mc = &_uc->uc_mcontext;
524 /* ATTENTION: Don't use CACAO's internal REG_* defines as they are
525 different to the ones in <ucontext.h>. */
527 pc = (u1 *) _mc->gregs[REG_RIP];
529 npc = critical_find_restart_point(pc);
532 _mc->gregs[REG_RIP] = (ptrint) npc;
538 * These are local overrides for various environment variables in Emacs.
539 * Please do not remove this and leave it at the end of the file, where
540 * Emacs will automagically detect them.
541 * ---------------------------------------------------------------------
544 * indent-tabs-mode: t
548 * vim:noexpandtab:sw=4:ts=4: