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
25 $Id: md.c 7249 2007-01-29 19:32:52Z twisti $
41 #include "vm/jit/x86_64/codegen.h"
43 #if defined(ENABLE_THREADS)
44 # include "threads/native/threads.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)
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 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 */
157 /* this was a normal NPE */
159 type = EXCEPTION_HARDWARE_NULLPOINTER;
163 /* create stackframeinfo */
165 stacktrace_create_extern_stackframeinfo(&sfi, NULL, sp, ra, xpc);
167 /* Handle the type. */
169 p = signal_handle(xpc, type, val);
171 /* remove stackframeinfo */
173 stacktrace_remove_stackframeinfo(&sfi);
177 _mc->gregs[REG_RAX] = (intptr_t) p;
178 _mc->gregs[REG_R10] = (intptr_t) xpc; /* REG_ITMP2_XPC */
179 _mc->gregs[REG_RIP] = (intptr_t) asm_handle_exception;
183 /* md_signal_handler_sigfpe ****************************************************
185 ArithmeticException signal handler for hardware divide by zero
188 *******************************************************************************/
190 void md_signal_handler_sigfpe(int sig, siginfo_t *siginfo, void *_p)
203 _uc = (ucontext_t *) _p;
204 _mc = &_uc->uc_mcontext;
206 /* ATTENTION: Don't use CACAO's internal REG_* defines as they are
207 different to the ones in <ucontext.h>. */
210 sp = (u1 *) _mc->gregs[REG_RSP];
211 xpc = (u1 *) _mc->gregs[REG_RIP];
212 ra = xpc; /* return address is equal to xpc */
214 /* this is an ArithmeticException */
216 type = EXCEPTION_HARDWARE_ARITHMETIC;
219 /* create stackframeinfo */
221 stacktrace_create_extern_stackframeinfo(&sfi, pv, sp, ra, xpc);
223 /* Handle the type. */
225 p = signal_handle(xpc, type, val);
227 /* remove stackframeinfo */
229 stacktrace_remove_stackframeinfo(&sfi);
233 _mc->gregs[REG_RAX] = (intptr_t) p;
234 _mc->gregs[REG_R10] = (intptr_t) xpc; /* REG_ITMP2_XPC */
235 _mc->gregs[REG_RIP] = (intptr_t) asm_handle_exception;
239 /* md_signal_handler_sigusr2 ***************************************************
241 Signal handler for profiling sampling.
243 *******************************************************************************/
245 #if defined(ENABLE_THREADS)
246 void md_signal_handler_sigusr2(int sig, siginfo_t *siginfo, void *_p)
255 _uc = (ucontext_t *) _p;
256 _mc = &_uc->uc_mcontext;
258 /* ATTENTION: Don't use CACAO's internal REG_* defines as they are
259 different to the ones in <ucontext.h>. */
261 pc = (u1 *) _mc->gregs[REG_RIP];
268 /* md_critical_section_restart *************************************************
270 Search the critical sections tree for a matching section and set
271 the PC to the restart point, if necessary.
273 *******************************************************************************/
275 #if defined(ENABLE_THREADS)
276 void md_critical_section_restart(ucontext_t *_uc)
282 _mc = &_uc->uc_mcontext;
284 /* ATTENTION: Don't use CACAO's internal REG_* defines as they are
285 different to the ones in <ucontext.h>. */
287 pc = (u1 *) _mc->gregs[REG_RIP];
289 npc = critical_find_restart_point(pc);
292 _mc->gregs[REG_RIP] = (ptrint) npc;
298 * These are local overrides for various environment variables in Emacs.
299 * Please do not remove this and leave it at the end of the file, where
300 * Emacs will automagically detect them.
301 * ---------------------------------------------------------------------
304 * indent-tabs-mode: t
308 * vim:noexpandtab:sw=4:ts=4: