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"
41 #if defined(ENABLE_THREADS)
42 # include "threads/native/threads.h"
45 #include "vm/exceptions.h"
46 #include "vm/signallocal.h"
48 #include "vm/jit/asmpart.h"
49 #include "vm/jit/stacktrace.h"
52 /* md_signal_handler_sigsegv ***************************************************
54 Signal handler for hardware exception.
56 *******************************************************************************/
58 void md_signal_handler_sigsegv(int sig, siginfo_t *siginfo, void *_p)
76 _uc = (ucontext_t *) _p;
77 _mc = &_uc->uc_mcontext;
79 /* ATTENTION: Don't use CACAO's internal REG_* defines as they are
80 different to the ones in <ucontext.h>. */
82 pv = NULL; /* is resolved during stackframeinfo creation */
83 sp = (u1 *) _mc->gregs[REG_RSP];
84 xpc = (u1 *) _mc->gregs[REG_RIP];
85 ra = xpc; /* return address is equal to XPC */
88 /* check for StackOverflowException */
90 threads_check_stackoverflow(sp);
93 /* get exception-throwing instruction */
95 opc = M_ALD_MEM_GET_OPC(xpc);
96 mod = M_ALD_MEM_GET_MOD(xpc);
97 rm = M_ALD_MEM_GET_RM(xpc);
99 /* for values see emit_mov_mem_reg and emit_mem */
101 if ((opc == 0x8b) && (mod == 0) && (rm == 4)) {
102 /* this was a hardware-exception */
104 d = M_ALD_MEM_GET_REG(xpc);
105 disp = M_ALD_MEM_GET_DISP(xpc);
107 /* we use the exception type as load displacement */
113 /* ATTENTION: The _mc->gregs layout is even worse than on
114 i386! See /usr/include/sys/ucontext.h. We need a
115 switch-case here... */
118 case 0: /* REG_RAX == 13 */
121 case 1: /* REG_RCX == 14 */
124 case 2: /* REG_RDX == 12 */
127 case 3: /* REG_RBX == 11 */
130 case 4: /* REG_RSP == 15 */
133 case 5: /* REG_RBP == 10 */
136 case 6: /* REG_RSI == 9 */
139 case 7: /* REG_RDI == 8 */
142 case 8: /* REG_R8 == 0 */
143 case 9: /* REG_R9 == 1 */
144 case 10: /* REG_R10 == 2 */
145 case 11: /* REG_R11 == 3 */
146 case 12: /* REG_R12 == 4 */
147 case 13: /* REG_R13 == 5 */
148 case 14: /* REG_R14 == 6 */
149 case 15: /* REG_R15 == 7 */
156 if (type == EXCEPTION_HARDWARE_COMPILER) {
157 /* The PV from the compiler stub is equal to the XPC. */
161 /* We use a framesize of zero here because the call pushed
162 the return addres onto the stack. */
164 ra = md_stacktrace_get_returnaddress(sp, 0);
166 /* Skip the RA on the stack. */
168 sp = sp + 1 * SIZEOF_VOID_P;
170 /* The XPC is the RA minus 1, because the RA points to the
171 instruction after the call. */
177 /* this was a normal NPE */
179 type = EXCEPTION_HARDWARE_NULLPOINTER;
183 /* Handle the type. */
185 p = signal_handle(type, val, pv, sp, ra, xpc, _p);
189 if (type == EXCEPTION_HARDWARE_COMPILER) {
191 o = exceptions_get_and_clear_exception();
193 _mc->gregs[REG_RSP] = (uintptr_t) sp; /* Remove RA from stack. */
195 _mc->gregs[REG_RAX] = (uintptr_t) o;
196 _mc->gregs[REG_R10] = (uintptr_t) xpc; /* REG_ITMP2_XPC */
197 _mc->gregs[REG_RIP] = (uintptr_t) asm_handle_exception;
200 _mc->gregs[REG_RIP] = (uintptr_t) p;
204 _mc->gregs[REG_RAX] = (uintptr_t) p;
205 _mc->gregs[REG_R10] = (uintptr_t) xpc; /* REG_ITMP2_XPC */
206 _mc->gregs[REG_RIP] = (uintptr_t) asm_handle_exception;
211 /* md_signal_handler_sigfpe ****************************************************
213 ArithmeticException signal handler for hardware divide by zero
216 *******************************************************************************/
218 void md_signal_handler_sigfpe(int sig, siginfo_t *siginfo, void *_p)
230 _uc = (ucontext_t *) _p;
231 _mc = &_uc->uc_mcontext;
233 /* ATTENTION: Don't use CACAO's internal REG_* defines as they are
234 different to the ones in <ucontext.h>. */
237 sp = (u1 *) _mc->gregs[REG_RSP];
238 xpc = (u1 *) _mc->gregs[REG_RIP];
239 ra = xpc; /* return address is equal to xpc */
241 /* this is an ArithmeticException */
243 type = EXCEPTION_HARDWARE_ARITHMETIC;
246 /* Handle the type. */
248 p = signal_handle(type, val, pv, sp, ra, xpc, _p);
252 _mc->gregs[REG_RAX] = (intptr_t) p;
253 _mc->gregs[REG_R10] = (intptr_t) xpc; /* REG_ITMP2_XPC */
254 _mc->gregs[REG_RIP] = (intptr_t) asm_handle_exception;
258 /* md_signal_handler_sigill ****************************************************
260 Signal handler for patchers.
262 *******************************************************************************/
264 void md_signal_handler_sigill(int sig, siginfo_t *siginfo, void *_p)
276 _uc = (ucontext_t *) _p;
277 _mc = &_uc->uc_mcontext;
279 /* ATTENTION: Don't use CACAO's internal REG_* defines as they are
280 different to the ones in <ucontext.h>. */
283 sp = (u1 *) _mc->gregs[REG_RSP];
284 xpc = (u1 *) _mc->gregs[REG_RIP];
285 ra = xpc; /* return address is equal to xpc */
287 /* This is a patcher. */
289 type = EXCEPTION_HARDWARE_PATCHER;
292 /* Handle the type. */
294 p = signal_handle(type, val, pv, sp, ra, xpc, _p);
299 _mc->gregs[REG_RAX] = (intptr_t) p;
300 _mc->gregs[REG_R10] = (intptr_t) xpc; /* REG_ITMP2_XPC */
301 _mc->gregs[REG_RIP] = (intptr_t) asm_handle_exception;
306 /* md_signal_handler_sigusr1 ***************************************************
308 Signal handler for suspending threads.
310 *******************************************************************************/
312 #if defined(ENABLE_THREADS) && defined(ENABLE_GC_CACAO)
313 void md_signal_handler_sigusr1(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 /* get the PC and SP for this thread */
327 pc = (u1 *) _mc->gregs[REG_RIP];
328 sp = (u1 *) _mc->gregs[REG_RSP];
330 /* now suspend the current thread */
331 threads_suspend_ack(pc, sp);
336 /* md_signal_handler_sigusr2 ***************************************************
338 Signal handler for profiling sampling.
340 *******************************************************************************/
342 #if defined(ENABLE_THREADS)
343 void md_signal_handler_sigusr2(int sig, siginfo_t *siginfo, void *_p)
352 _uc = (ucontext_t *) _p;
353 _mc = &_uc->uc_mcontext;
355 /* ATTENTION: Don't use CACAO's internal REG_* defines as they are
356 different to the ones in <ucontext.h>. */
358 pc = (u1 *) _mc->gregs[REG_RIP];
365 /* md_replace_executionstate_read **********************************************
367 Read the given context into an executionstate for Replacement.
369 *******************************************************************************/
371 #if defined(ENABLE_REPLACEMENT)
372 void md_replace_executionstate_read(executionstate_t *es, void *context)
379 _uc = (ucontext_t *) context;
380 _mc = &_uc->uc_mcontext;
382 /* read special registers */
383 es->pc = (u1 *) _mc->gregs[REG_RSP];
384 es->sp = (u1 *) _mc->gregs[REG_RSP];
387 /* read integer registers */
388 for (i = 0; i < INT_REG_CNT; i++) {
392 case 0: /* REG_RAX == 13 */
395 case 1: /* REG_RCX == 14 */
398 case 2: /* REG_RDX == 12 */
401 case 3: /* REG_RBX == 11 */
404 case 4: /* REG_RSP == 15 */
407 case 5: /* REG_RBP == 10 */
410 case 6: /* REG_RSI == 9 */
413 case 7: /* REG_RDI == 8 */
416 case 8: /* REG_R8 == 0 */
417 case 9: /* REG_R9 == 1 */
418 case 10: /* REG_R10 == 2 */
419 case 11: /* REG_R11 == 3 */
420 case 12: /* REG_R12 == 4 */
421 case 13: /* REG_R13 == 5 */
422 case 14: /* REG_R14 == 6 */
423 case 15: /* REG_R15 == 7 */
428 es->intregs[i] = _mc->gregs[d];
431 /* read float registers */
432 for (i = 0; i < FLT_REG_CNT; i++)
433 es->fltregs[i] = 0xdeadbeefdeadbeefL;
438 /* md_replace_executionstate_write *********************************************
440 Write the given executionstate back to the context for Replacement.
442 *******************************************************************************/
444 #if defined(ENABLE_REPLACEMENT)
445 void md_replace_executionstate_write(executionstate_t *es, void *context)
452 _uc = (ucontext_t *) context;
453 _mc = &_uc->uc_mcontext;
455 /* write integer registers */
456 for (i = 0; i < INT_REG_CNT; i++) {
460 case 0: /* REG_RAX == 13 */
463 case 1: /* REG_RCX == 14 */
466 case 2: /* REG_RDX == 12 */
469 case 3: /* REG_RBX == 11 */
472 case 4: /* REG_RSP == 15 */
475 case 5: /* REG_RBP == 10 */
478 case 6: /* REG_RSI == 9 */
481 case 7: /* REG_RDI == 8 */
484 case 8: /* REG_R8 == 0 */
485 case 9: /* REG_R9 == 1 */
486 case 10: /* REG_R10 == 2 */
487 case 11: /* REG_R11 == 3 */
488 case 12: /* REG_R12 == 4 */
489 case 13: /* REG_R13 == 5 */
490 case 14: /* REG_R14 == 6 */
491 case 15: /* REG_R15 == 7 */
496 _mc->gregs[d] = es->intregs[i];
499 /* write special registers */
500 _mc->gregs[REG_RIP] = (ptrint) es->pc;
501 _mc->gregs[REG_RSP] = (ptrint) es->sp;
506 /* md_critical_section_restart *************************************************
508 Search the critical sections tree for a matching section and set
509 the PC to the restart point, if necessary.
511 *******************************************************************************/
513 #if defined(ENABLE_THREADS)
514 void md_critical_section_restart(ucontext_t *_uc)
520 _mc = &_uc->uc_mcontext;
522 /* ATTENTION: Don't use CACAO's internal REG_* defines as they are
523 different to the ones in <ucontext.h>. */
525 pc = (u1 *) _mc->gregs[REG_RIP];
527 npc = critical_find_restart_point(pc);
530 _mc->gregs[REG_RIP] = (ptrint) npc;
536 * These are local overrides for various environment variables in Emacs.
537 * Please do not remove this and leave it at the end of the file, where
538 * Emacs will automagically detect them.
539 * ---------------------------------------------------------------------
542 * indent-tabs-mode: t
546 * vim:noexpandtab:sw=4:ts=4: