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/builtin.h"
46 #include "vm/exceptions.h"
47 #include "vm/signallocal.h"
49 #include "vm/jit/asmpart.h"
50 #include "vm/jit/stacktrace.h"
53 /* md_signal_handler_sigsegv ***************************************************
55 Signal handler for hardware exception.
57 *******************************************************************************/
59 void md_signal_handler_sigsegv(int sig, siginfo_t *siginfo, void *_p)
77 _uc = (ucontext_t *) _p;
78 _mc = &_uc->uc_mcontext;
80 /* ATTENTION: Don't use CACAO's internal REG_* defines as they are
81 different to the ones in <ucontext.h>. */
83 pv = NULL; /* is resolved during stackframeinfo creation */
84 sp = (u1 *) _mc->gregs[REG_RSP];
85 xpc = (u1 *) _mc->gregs[REG_RIP];
86 ra = xpc; /* return address is equal to XPC */
89 /* check for StackOverflowException */
91 threads_check_stackoverflow(sp);
94 /* get exception-throwing instruction */
96 opc = M_ALD_MEM_GET_OPC(xpc);
97 mod = M_ALD_MEM_GET_MOD(xpc);
98 rm = M_ALD_MEM_GET_RM(xpc);
100 /* for values see emit_mov_mem_reg and emit_mem */
102 if ((opc == 0x8b) && (mod == 0) && (rm == 4)) {
103 /* this was a hardware-exception */
105 d = M_ALD_MEM_GET_REG(xpc);
106 disp = M_ALD_MEM_GET_DISP(xpc);
108 /* we use the exception type as load displacement */
114 /* ATTENTION: The _mc->gregs layout is even worse than on
115 i386! See /usr/include/sys/ucontext.h. We need a
116 switch-case here... */
119 case 0: /* REG_RAX == 13 */
122 case 1: /* REG_RCX == 14 */
125 case 2: /* REG_RDX == 12 */
128 case 3: /* REG_RBX == 11 */
131 case 4: /* REG_RSP == 15 */
134 case 5: /* REG_RBP == 10 */
137 case 6: /* REG_RSI == 9 */
140 case 7: /* REG_RDI == 8 */
143 case 8: /* REG_R8 == 0 */
144 case 9: /* REG_R9 == 1 */
145 case 10: /* REG_R10 == 2 */
146 case 11: /* REG_R11 == 3 */
147 case 12: /* REG_R12 == 4 */
148 case 13: /* REG_R13 == 5 */
149 case 14: /* REG_R14 == 6 */
150 case 15: /* REG_R15 == 7 */
157 if (type == EXCEPTION_HARDWARE_COMPILER) {
158 /* The PV from the compiler stub is equal to the XPC. */
162 /* We use a framesize of zero here because the call pushed
163 the return addres onto the stack. */
165 ra = md_stacktrace_get_returnaddress(sp, 0);
167 /* Skip the RA on the stack. */
169 sp = sp + 1 * SIZEOF_VOID_P;
171 /* The XPC is the RA minus 1, because the RA points to the
172 instruction after the call. */
178 /* this was a normal NPE */
180 type = EXCEPTION_HARDWARE_NULLPOINTER;
184 /* Handle the type. */
186 p = signal_handle(type, val, pv, sp, ra, xpc, _p);
190 if (type == EXCEPTION_HARDWARE_COMPILER) {
192 o = builtin_retrieve_exception();
194 _mc->gregs[REG_RSP] = (uintptr_t) sp; /* Remove RA from stack. */
196 _mc->gregs[REG_RAX] = (uintptr_t) o;
197 _mc->gregs[REG_R10] = (uintptr_t) xpc; /* REG_ITMP2_XPC */
198 _mc->gregs[REG_RIP] = (uintptr_t) asm_handle_exception;
201 _mc->gregs[REG_RIP] = (uintptr_t) p;
205 _mc->gregs[REG_RAX] = (uintptr_t) p;
206 _mc->gregs[REG_R10] = (uintptr_t) xpc; /* REG_ITMP2_XPC */
207 _mc->gregs[REG_RIP] = (uintptr_t) asm_handle_exception;
212 /* md_signal_handler_sigfpe ****************************************************
214 ArithmeticException signal handler for hardware divide by zero
217 *******************************************************************************/
219 void md_signal_handler_sigfpe(int sig, siginfo_t *siginfo, void *_p)
231 _uc = (ucontext_t *) _p;
232 _mc = &_uc->uc_mcontext;
234 /* ATTENTION: Don't use CACAO's internal REG_* defines as they are
235 different to the ones in <ucontext.h>. */
238 sp = (u1 *) _mc->gregs[REG_RSP];
239 xpc = (u1 *) _mc->gregs[REG_RIP];
240 ra = xpc; /* return address is equal to xpc */
242 /* this is an ArithmeticException */
244 type = EXCEPTION_HARDWARE_ARITHMETIC;
247 /* Handle the type. */
249 p = signal_handle(type, val, pv, sp, ra, xpc, _p);
253 _mc->gregs[REG_RAX] = (intptr_t) p;
254 _mc->gregs[REG_R10] = (intptr_t) xpc; /* REG_ITMP2_XPC */
255 _mc->gregs[REG_RIP] = (intptr_t) asm_handle_exception;
259 /* md_signal_handler_sigill ****************************************************
261 Signal handler for patchers.
263 *******************************************************************************/
265 void md_signal_handler_sigill(int sig, siginfo_t *siginfo, void *_p)
277 _uc = (ucontext_t *) _p;
278 _mc = &_uc->uc_mcontext;
280 /* ATTENTION: Don't use CACAO's internal REG_* defines as they are
281 different to the ones in <ucontext.h>. */
284 sp = (u1 *) _mc->gregs[REG_RSP];
285 xpc = (u1 *) _mc->gregs[REG_RIP];
286 ra = xpc; /* return address is equal to xpc */
288 /* This is a patcher. */
290 type = EXCEPTION_HARDWARE_PATCHER;
293 /* Handle the type. */
295 p = signal_handle(type, val, pv, sp, ra, xpc, _p);
300 _mc->gregs[REG_RAX] = (intptr_t) p;
301 _mc->gregs[REG_R10] = (intptr_t) xpc; /* REG_ITMP2_XPC */
302 _mc->gregs[REG_RIP] = (intptr_t) asm_handle_exception;
307 /* md_signal_handler_sigusr1 ***************************************************
309 Signal handler for suspending threads.
311 *******************************************************************************/
313 #if defined(ENABLE_THREADS) && defined(ENABLE_GC_CACAO)
314 void md_signal_handler_sigusr1(int sig, siginfo_t *siginfo, void *_p)
321 _uc = (ucontext_t *) _p;
322 _mc = &_uc->uc_mcontext;
324 /* ATTENTION: Don't use CACAO's internal REG_* defines as they are
325 different to the ones in <ucontext.h>. */
327 /* get the PC and SP for this thread */
328 pc = (u1 *) _mc->gregs[REG_RIP];
329 sp = (u1 *) _mc->gregs[REG_RSP];
331 /* now suspend the current thread */
332 threads_suspend_ack(pc, sp);
337 /* md_signal_handler_sigusr2 ***************************************************
339 Signal handler for profiling sampling.
341 *******************************************************************************/
343 #if defined(ENABLE_THREADS)
344 void md_signal_handler_sigusr2(int sig, siginfo_t *siginfo, void *_p)
353 _uc = (ucontext_t *) _p;
354 _mc = &_uc->uc_mcontext;
356 /* ATTENTION: Don't use CACAO's internal REG_* defines as they are
357 different to the ones in <ucontext.h>. */
359 pc = (u1 *) _mc->gregs[REG_RIP];
366 /* md_replace_executionstate_read **********************************************
368 Read the given context into an executionstate for Replacement.
370 *******************************************************************************/
372 #if defined(ENABLE_REPLACEMENT)
373 void md_replace_executionstate_read(executionstate_t *es, void *context)
380 _uc = (ucontext_t *) context;
381 _mc = &_uc->uc_mcontext;
383 /* read special registers */
384 es->pc = (u1 *) _mc->gregs[REG_RSP];
385 es->sp = (u1 *) _mc->gregs[REG_RSP];
388 /* read integer registers */
389 for (i = 0; i < INT_REG_CNT; i++) {
393 case 0: /* REG_RAX == 13 */
396 case 1: /* REG_RCX == 14 */
399 case 2: /* REG_RDX == 12 */
402 case 3: /* REG_RBX == 11 */
405 case 4: /* REG_RSP == 15 */
408 case 5: /* REG_RBP == 10 */
411 case 6: /* REG_RSI == 9 */
414 case 7: /* REG_RDI == 8 */
417 case 8: /* REG_R8 == 0 */
418 case 9: /* REG_R9 == 1 */
419 case 10: /* REG_R10 == 2 */
420 case 11: /* REG_R11 == 3 */
421 case 12: /* REG_R12 == 4 */
422 case 13: /* REG_R13 == 5 */
423 case 14: /* REG_R14 == 6 */
424 case 15: /* REG_R15 == 7 */
429 es->intregs[i] = _mc->gregs[d];
432 /* read float registers */
433 for (i = 0; i < FLT_REG_CNT; i++)
434 es->fltregs[i] = 0xdeadbeefdeadbeefL;
439 /* md_replace_executionstate_write *********************************************
441 Write the given executionstate back to the context for Replacement.
443 *******************************************************************************/
445 #if defined(ENABLE_REPLACEMENT)
446 void md_replace_executionstate_write(executionstate_t *es, void *context)
453 _uc = (ucontext_t *) context;
454 _mc = &_uc->uc_mcontext;
456 /* write integer registers */
457 for (i = 0; i < INT_REG_CNT; i++) {
461 case 0: /* REG_RAX == 13 */
464 case 1: /* REG_RCX == 14 */
467 case 2: /* REG_RDX == 12 */
470 case 3: /* REG_RBX == 11 */
473 case 4: /* REG_RSP == 15 */
476 case 5: /* REG_RBP == 10 */
479 case 6: /* REG_RSI == 9 */
482 case 7: /* REG_RDI == 8 */
485 case 8: /* REG_R8 == 0 */
486 case 9: /* REG_R9 == 1 */
487 case 10: /* REG_R10 == 2 */
488 case 11: /* REG_R11 == 3 */
489 case 12: /* REG_R12 == 4 */
490 case 13: /* REG_R13 == 5 */
491 case 14: /* REG_R14 == 6 */
492 case 15: /* REG_R15 == 7 */
497 _mc->gregs[d] = es->intregs[i];
500 /* write special registers */
501 _mc->gregs[REG_RIP] = (ptrint) es->pc;
502 _mc->gregs[REG_RSP] = (ptrint) es->sp;
507 /* md_critical_section_restart *************************************************
509 Search the critical sections tree for a matching section and set
510 the PC to the restart point, if necessary.
512 *******************************************************************************/
514 #if defined(ENABLE_THREADS)
515 void md_critical_section_restart(ucontext_t *_uc)
521 _mc = &_uc->uc_mcontext;
523 /* ATTENTION: Don't use CACAO's internal REG_* defines as they are
524 different to the ones in <ucontext.h>. */
526 pc = (u1 *) _mc->gregs[REG_RIP];
528 npc = critical_find_restart_point(pc);
531 _mc->gregs[REG_RIP] = (ptrint) npc;
537 * These are local overrides for various environment variables in Emacs.
538 * Please do not remove this and leave it at the end of the file, where
539 * Emacs will automagically detect them.
540 * ---------------------------------------------------------------------
543 * indent-tabs-mode: t
547 * vim:noexpandtab:sw=4:ts=4: