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 4698 2006-03-28 14:31:53Z 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"
50 #if !defined(NDEBUG) && defined(ENABLE_DISASSEMBLER)
51 #include "vm/options.h" /* XXX debug */
52 #include "vm/jit/disass.h" /* XXX debug */
56 /* md_init *********************************************************************
58 Do some machine dependent initialization.
60 *******************************************************************************/
68 /* md_signal_handler_sigsegv ***************************************************
70 NullPointerException signal handler for hardware null pointer
73 *******************************************************************************/
75 void md_signal_handler_sigsegv(int sig, siginfo_t *siginfo, void *_p)
83 _uc = (ucontext_t *) _p;
84 _mc = &_uc->uc_mcontext;
86 /* ATTENTION: Don't use CACAO's internal REG_* defines as they are
87 different to the ones in <ucontext.h>. */
89 sp = (u1 *) _mc->gregs[REG_RSP];
90 xpc = (u1 *) _mc->gregs[REG_RIP];
91 ra = xpc; /* return address is equal to xpc */
94 (ptrint) stacktrace_hardware_nullpointerexception(NULL, sp, ra, xpc);
96 _mc->gregs[REG_R10] = (ptrint) xpc; /* REG_ITMP2_XPC */
97 _mc->gregs[REG_RIP] = (ptrint) asm_handle_exception;
101 /* md_signal_handler_sigfpe ****************************************************
103 ArithmeticException signal handler for hardware divide by zero
106 *******************************************************************************/
108 void md_signal_handler_sigfpe(int sig, siginfo_t *siginfo, void *_p)
116 _uc = (ucontext_t *) _p;
117 _mc = &_uc->uc_mcontext;
119 /* ATTENTION: Don't use CACAO's internal REG_* defines as they are
120 different to the ones in <ucontext.h>. */
122 sp = (u1 *) _mc->gregs[REG_RSP];
123 xpc = (u1 *) _mc->gregs[REG_RIP];
124 ra = xpc; /* return address is equal to xpc */
126 _mc->gregs[REG_RAX] =
127 (ptrint) stacktrace_hardware_arithmeticexception(NULL, sp, ra, xpc);
129 _mc->gregs[REG_R10] = (ptrint) xpc; /* REG_ITMP2_XPC */
130 _mc->gregs[REG_RIP] = (ptrint) asm_handle_exception;
134 #if defined(USE_THREADS) && defined(NATIVE_THREADS)
135 void thread_restartcriticalsection(ucontext_t *uc)
139 critical = thread_checkcritical((void *) uc->uc_mcontext.gregs[REG_RIP]);
142 uc->uc_mcontext.gregs[REG_RIP] = (ptrint) critical;
147 /* md_stacktrace_get_returnaddress *********************************************
149 Returns the return address of the current stackframe, specified by
150 the passed stack pointer and the stack frame size.
152 *******************************************************************************/
154 u1 *md_stacktrace_get_returnaddress(u1 *sp, u4 framesize)
158 /* on x86_64 the return address is above the current stack frame */
160 ra = *((u1 **) (sp + framesize));
166 /* md_codegen_findmethod *******************************************************
168 On this architecture just a wrapper function to codegen_findmethod.
170 *******************************************************************************/
172 u1 *md_codegen_findmethod(u1 *ra)
176 /* the the start address of the function which contains this
177 address from the method table */
179 pv = codegen_findmethod(ra);
185 /* md_cacheflush ***************************************************************
187 Calls the system's function to flush the instruction and data
190 *******************************************************************************/
192 void md_cacheflush(u1 *addr, s4 nbytes)
198 /* md_patch_replacement_point **************************************************
200 Patch the given replacement point.
202 *******************************************************************************/
204 void md_patch_replacement_point(rplpoint *rp)
208 /* XXX this is probably unsafe! */
210 /* save the current machine code */
211 mcode = *(u8*)rp->pc;
213 /* write spinning instruction */
214 *(u2*)(rp->pc) = 0xebfe;
217 rp->pc[4] = (rp->mcode >> 32);
219 /* write first word */
220 *(u4*)(rp->pc) = (u4) rp->mcode;
222 /* store saved mcode */
225 #if !defined(NDEBUG) && defined(ENABLE_DISASSEMBLER)
233 /* XXX if required asm_cacheflush(rp->pc,8); */
237 * These are local overrides for various environment variables in Emacs.
238 * Please do not remove this and leave it at the end of the file, where
239 * Emacs will automagically detect them.
240 * ---------------------------------------------------------------------
243 * indent-tabs-mode: t
247 * vim:noexpandtab:sw=4:ts=4: