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 $
40 #include "vm/jit/x86_64/codegen.h"
42 #if defined(ENABLE_THREADS)
43 # include "threads/native/threads.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)
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 sp = (u1 *) _mc->gregs[REG_RSP];
83 xpc = (u1 *) _mc->gregs[REG_RIP];
84 ra = xpc; /* return address is equal to xpc */
87 /* check for StackOverflowException */
89 threads_check_stackoverflow(sp);
92 /* get exception-throwing instruction */
94 opc = M_ALD_MEM_GET_OPC(xpc);
95 mod = M_ALD_MEM_GET_MOD(xpc);
96 rm = M_ALD_MEM_GET_RM(xpc);
98 /* for values see emit_mov_mem_reg and emit_mem */
100 if ((opc == 0x8b) && (mod == 0) && (rm == 4)) {
101 /* this was a hardware-exception */
103 d = M_ALD_MEM_GET_REG(xpc);
104 disp = M_ALD_MEM_GET_DISP(xpc);
106 /* we use the exception type as load displacement */
112 /* ATTENTION: The _mc->gregs layout is even worse than on
113 i386! See /usr/include/sys/ucontext.h. We need a
114 switch-case here... */
117 case 0: /* REG_RAX == 13 */
120 case 1: /* REG_RCX == 14 */
123 case 2: /* REG_RDX == 12 */
126 case 3: /* REG_RBX == 11 */
129 case 4: /* REG_RSP == 15 */
132 case 5: /* REG_RBP == 10 */
135 case 6: /* REG_RSI == 9 */
138 case 7: /* REG_RDI == 8 */
141 case 8: /* REG_R8 == 0 */
142 case 9: /* REG_R9 == 1 */
143 case 10: /* REG_R10 == 2 */
144 case 11: /* REG_R11 == 3 */
145 case 12: /* REG_R12 == 4 */
146 case 13: /* REG_R13 == 5 */
147 case 14: /* REG_R14 == 6 */
148 case 15: /* REG_R15 == 7 */
156 /* this was a normal NPE */
158 type = EXCEPTION_HARDWARE_NULLPOINTER;
162 /* generate appropriate exception */
164 o = exceptions_new_hardware_exception(NULL, sp, ra, xpc, type, vali, &sfi);
168 _mc->gregs[REG_RAX] = (ptrint) o;
169 _mc->gregs[REG_R10] = (ptrint) xpc; /* REG_ITMP2_XPC */
170 _mc->gregs[REG_RIP] = (ptrint) asm_handle_exception;
174 /* md_signal_handler_sigfpe ****************************************************
176 ArithmeticException signal handler for hardware divide by zero
179 *******************************************************************************/
181 void md_signal_handler_sigfpe(int sig, siginfo_t *siginfo, void *_p)
192 java_objectheader *o;
194 _uc = (ucontext_t *) _p;
195 _mc = &_uc->uc_mcontext;
197 /* ATTENTION: Don't use CACAO's internal REG_* defines as they are
198 different to the ones in <ucontext.h>. */
201 sp = (u1 *) _mc->gregs[REG_RSP];
202 xpc = (u1 *) _mc->gregs[REG_RIP];
203 ra = xpc; /* return address is equal to xpc */
205 /* this is an ArithmeticException */
207 type = EXCEPTION_HARDWARE_ARITHMETIC;
210 /* generate appropriate exception */
212 o = exceptions_new_hardware_exception(pv, sp, ra, xpc, type, val, &sfi);
216 _mc->gregs[REG_RAX] = (ptrint) o;
217 _mc->gregs[REG_R10] = (ptrint) xpc; /* REG_ITMP2_XPC */
218 _mc->gregs[REG_RIP] = (ptrint) asm_handle_exception;
222 /* md_signal_handler_sigusr1 ***************************************************
224 Signal handler for suspending threads.
226 *******************************************************************************/
228 #if defined(ENABLE_THREADS) && defined(ENABLE_GC_CACAO)
229 void md_signal_handler_sigusr1(int sig, siginfo_t *siginfo, void *_p)
236 _uc = (ucontext_t *) _p;
237 _mc = &_uc->uc_mcontext;
239 /* ATTENTION: Don't use CACAO's internal REG_* defines as they are
240 different to the ones in <ucontext.h>. */
242 /* get the PC and SP for this thread */
243 pc = (u1 *) _mc->gregs[REG_RIP];
244 sp = (u1 *) _mc->gregs[REG_RSP];
246 /* now suspend the current thread */
247 threads_suspend_ack(pc, sp);
252 /* md_signal_handler_sigusr2 ***************************************************
254 Signal handler for profiling sampling.
256 *******************************************************************************/
258 #if defined(ENABLE_THREADS)
259 void md_signal_handler_sigusr2(int sig, siginfo_t *siginfo, void *_p)
268 _uc = (ucontext_t *) _p;
269 _mc = &_uc->uc_mcontext;
271 /* ATTENTION: Don't use CACAO's internal REG_* defines as they are
272 different to the ones in <ucontext.h>. */
274 pc = (u1 *) _mc->gregs[REG_RIP];
281 /* md_critical_section_restart *************************************************
283 Search the critical sections tree for a matching section and set
284 the PC to the restart point, if necessary.
286 *******************************************************************************/
288 #if defined(ENABLE_THREADS)
289 void md_critical_section_restart(ucontext_t *_uc)
295 _mc = &_uc->uc_mcontext;
297 /* ATTENTION: Don't use CACAO's internal REG_* defines as they are
298 different to the ones in <ucontext.h>. */
300 pc = (u1 *) _mc->gregs[REG_RIP];
302 npc = critical_find_restart_point(pc);
305 _mc->gregs[REG_RIP] = (ptrint) npc;
311 * These are local overrides for various environment variables in Emacs.
312 * Please do not remove this and leave it at the end of the file, where
313 * Emacs will automagically detect them.
314 * ---------------------------------------------------------------------
317 * indent-tabs-mode: t
321 * vim:noexpandtab:sw=4:ts=4: