1 /* src/vm/jit/x86_64/md.c - machine dependent x86_64 Linux functions
3 Copyright (C) 1996-2005, 2006 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 Contact: cacao@cacaojvm.org
27 Authors: Christian Thalinger
29 Changes: Edwin Steiner
31 $Id: md.c 4637 2006-03-16 15:36:49Z twisti $
43 #include "vm/jit/x86_64/md-abi.h"
45 #include "vm/exceptions.h"
46 #include "vm/signallocal.h"
47 #include "vm/jit/asmpart.h"
48 #include "vm/jit/stacktrace.h"
49 #include "vm/options.h" /* XXX debug */
50 #include "vm/jit/disass.h" /* XXX debug */
53 /* md_init *********************************************************************
55 Do some machine dependent initialization.
57 *******************************************************************************/
65 /* md_signal_handler_sigsegv ***************************************************
67 NullPointerException signal handler for hardware null pointer
70 *******************************************************************************/
72 void md_signal_handler_sigsegv(int sig, siginfo_t *siginfo, void *_p)
80 _uc = (ucontext_t *) _p;
81 _mc = &_uc->uc_mcontext;
83 /* ATTENTION: don't use CACAO internal REG_* defines as they are */
84 /* different to the ones in <ucontext.h> */
86 sp = (u1 *) _mc->gregs[REG_RSP];
87 xpc = (u1 *) _mc->gregs[REG_RIP];
88 ra = xpc; /* return address is equal to xpc */
91 (ptrint) stacktrace_hardware_nullpointerexception(NULL, sp, ra, xpc);
93 _mc->gregs[REG_R10] = (ptrint) xpc; /* REG_ITMP2_XPC */
94 _mc->gregs[REG_RIP] = (ptrint) asm_handle_exception;
98 /* md_signal_handler_sigfpe ****************************************************
100 ArithmeticException signal handler for hardware divide by zero
103 *******************************************************************************/
105 void md_signal_handler_sigfpe(int sig, siginfo_t *siginfo, void *_p)
113 _uc = (ucontext_t *) _p;
114 _mc = &_uc->uc_mcontext;
116 /* ATTENTION: don't use CACAO internal REG_* defines as they are */
117 /* different to the ones in <ucontext.h> */
119 sp = (u1 *) _mc->gregs[REG_RSP];
120 xpc = (u1 *) _mc->gregs[REG_RIP];
121 ra = xpc; /* return address is equal to xpc */
123 _mc->gregs[REG_RAX] =
124 (ptrint) stacktrace_hardware_arithmeticexception(NULL, sp, ra, xpc);
126 _mc->gregs[REG_R10] = (ptrint) xpc; /* REG_ITMP2_XPC */
127 _mc->gregs[REG_RIP] = (ptrint) asm_handle_exception;
131 #if defined(USE_THREADS) && defined(NATIVE_THREADS)
132 void thread_restartcriticalsection(ucontext_t *uc)
136 critical = thread_checkcritical((void *) uc->uc_mcontext.gregs[REG_RIP]);
139 uc->uc_mcontext.gregs[REG_RIP] = (ptrint) critical;
144 /* md_stacktrace_get_returnaddress *********************************************
146 Returns the return address of the current stackframe, specified by
147 the passed stack pointer and the stack frame size.
149 *******************************************************************************/
151 u1 *md_stacktrace_get_returnaddress(u1 *sp, u4 framesize)
155 /* on x86_64 the return address is above the current stack frame */
157 ra = *((u1 **) (sp + framesize));
163 /* md_codegen_findmethod *******************************************************
165 On this architecture just a wrapper function to codegen_findmethod.
167 *******************************************************************************/
169 u1 *md_codegen_findmethod(u1 *ra)
173 /* the the start address of the function which contains this
174 address from the method table */
176 pv = codegen_findmethod(ra);
182 /* md_cacheflush ***************************************************************
184 Calls the system's function to flush the instruction and data
187 *******************************************************************************/
189 void md_cacheflush(u1 *addr, s4 nbytes)
195 /* md_patch_replacement_point **************************************************
197 Patch the given replacement point.
199 *******************************************************************************/
201 void md_patch_replacement_point(rplpoint *rp)
205 /* XXX this is probably unsafe! */
207 /* save the current machine code */
208 mcode = *(u8*)rp->pc;
210 /* write spinning instruction */
211 *(u2*)(rp->pc) = 0xebfe;
214 rp->pc[4] = (rp->mcode >> 32);
216 /* write first word */
217 *(u4*)(rp->pc) = (u4) rp->mcode;
219 /* store saved mcode */
228 /* XXX if required asm_cacheflush(rp->pc,8); */
232 * These are local overrides for various environment variables in Emacs.
233 * Please do not remove this and leave it at the end of the file, where
234 * Emacs will automagically detect them.
235 * ---------------------------------------------------------------------
238 * indent-tabs-mode: t
242 * vim:noexpandtab:sw=4:ts=4: