1 /* src/vm/jit/x86_64/md.c - machine dependent x86_64 functions
3 Copyright (C) 1996-2005, 2006, 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
33 #include "vm/jit/x86_64/md-abi.h"
35 #if defined(ENABLE_THREADS)
36 # include "threads/native/threads.h"
39 #include "vm/jit/asmpart.h"
40 #include "vm/jit/codegen-common.h"
41 #include "vm/jit/stacktrace.h"
43 #if defined(ENABLE_REPLACEMENT)
44 # include "vm/exceptions.h"
47 #if !defined(NDEBUG) && defined(ENABLE_DISASSEMBLER)
48 #include "vmcore/options.h" /* XXX debug */
49 #include "vm/jit/disass.h" /* XXX debug */
53 /* md_init *********************************************************************
55 Do some machine dependent initialization.
57 *******************************************************************************/
65 /* md_stacktrace_get_returnaddress *********************************************
67 Returns the return address of the current stackframe, specified by
68 the passed stack pointer and the stack frame size.
70 *******************************************************************************/
72 u1 *md_stacktrace_get_returnaddress(u1 *sp, u4 framesize)
76 /* on x86_64 the return address is above the current stack frame */
78 ra = *((u1 **) (sp + framesize));
84 /* md_get_method_patch_address *************************************************
86 Gets the patch address of the currently compiled method. The offset
87 is extracted from the load instruction(s) before the jump and added
88 to the right base address (PV or REG_METHODPTR).
92 4d 8b 15 e2 fe ff ff mov -286(%rip),%r10
93 49 ff d2 rex64Z callq *%r10
97 4c 8b 17 mov (%rdi),%r10
98 49 8b 82 00 00 00 00 mov 0x0(%r10),%rax
99 48 ff d3 rex64 callq *%rax
103 4c 8b 17 mov (%rdi),%r10
104 4d 8b 92 00 00 00 00 mov 0x0(%r10),%r10
105 49 8b 82 00 00 00 00 mov 0x0(%r10),%rax
106 48 ff d3 rex64 callq *%r11
108 *******************************************************************************/
110 u1 *md_get_method_patch_address(u1 *ra, stackframeinfo *sfi, u1 *mptr)
114 u1 *pa; /* patch address */
116 /* go back to the actual call instruction (3-bytes) */
120 /* get the last byte of the call */
124 /* check for the different calls */
127 /* INVOKESTATIC/SPECIAL */
129 /* Get the offset from the instruction (the offset address is
130 4-bytes before the call instruction). */
132 offset = *((s4 *) (ra - 4));
134 /* add the offset to the return address (IP-relative addressing) */
138 else if (mcode == 0xd3) {
139 /* INVOKEVIRTUAL/INTERFACE */
141 /* return NULL if no mptr was specified (used for replacement) */
146 /* Get the offset from the instruction (the offset address is
147 4-bytes before the call instruction). */
149 offset = *((s4 *) (ra - 4));
151 /* add the offset to the method pointer */
156 /* catch any problems */
158 vm_abort("md_get_method_patch_address: unknown instruction %x", mcode);
160 /* keep compiler happy */
169 /* md_codegen_get_pv_from_pc ***************************************************
171 On this architecture just a wrapper function to
172 codegen_get_pv_from_pc.
174 *******************************************************************************/
176 u1 *md_codegen_get_pv_from_pc(u1 *ra)
180 /* Get the start address of the function which contains this
181 address from the method table. */
183 pv = codegen_get_pv_from_pc(ra);
189 /* md_cacheflush ***************************************************************
191 Calls the system's function to flush the instruction and data
194 *******************************************************************************/
196 void md_cacheflush(u1 *addr, s4 nbytes)
202 /* md_icacheflush **************************************************************
204 Calls the system's function to flush the instruction cache.
206 *******************************************************************************/
208 void md_icacheflush(u1 *addr, s4 nbytes)
214 /* md_dcacheflush **************************************************************
216 Calls the system's function to flush the data cache.
218 *******************************************************************************/
220 void md_dcacheflush(u1 *addr, s4 nbytes)
226 /* md_patch_replacement_point **************************************************
228 Patch the given replacement point.
230 *******************************************************************************/
232 #if defined(ENABLE_REPLACEMENT)
233 void md_patch_replacement_point(codeinfo *code, s4 index, rplpoint *rp, u1 *savedmcode)
237 /* XXX this is probably unsafe! */
240 /* write spinning instruction */
241 *(u2*)(rp->pc) = 0xebfe;
244 rp->pc[4] = savedmcode[4];
246 /* write first word */
247 *(u4*)(rp->pc) = *(u4*)(savedmcode);
250 /* save the current machine code */
251 *(u4*)(savedmcode) = *(u4*)(rp->pc);
252 savedmcode[4] = rp->pc[4];
254 /* build the machine code for the patch */
255 assert(0); /* XXX build trap instruction below */
258 /* write spinning instruction */
259 *(u2*)(rp->pc) = 0xebfe;
262 rp->pc[4] = (mcode >> 32);
264 /* write first word */
265 *(u4*)(rp->pc) = (u4) mcode;
268 #if !defined(NDEBUG) && defined(ENABLE_DISASSEMBLER) && 0
276 /* XXX if required asm_cacheflush(rp->pc,8); */
278 #endif /* defined(ENABLE_REPLACEMENT) */
281 * These are local overrides for various environment variables in Emacs.
282 * Please do not remove this and leave it at the end of the file, where
283 * Emacs will automagically detect them.
284 * ---------------------------------------------------------------------
287 * indent-tabs-mode: t
291 * vim:noexpandtab:sw=4:ts=4: