1 /* src/vm/jit/x86_64/md.c - machine dependent x86_64 functions
3 Copyright (C) 1996-2005, 2006, 2007, 2008
4 CACAOVM - Verein zur Foerderung der freien virtuellen Maschine CACAO
5 Copyright (C) 2009 Theobroma Systems Ltd.
7 This file is part of CACAO.
9 This program is free software; you can redistribute it and/or
10 modify it under the terms of the GNU General Public License as
11 published by the Free Software Foundation; either version 2, or (at
12 your option) any later version.
14 This program is distributed in the hope that it will be useful, but
15 WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
33 #include "vm/jit/x86_64/codegen.h"
34 #include "vm/jit/x86_64/md-abi.h"
38 #include "vm/jit/codegen-common.hpp"
39 #include "vm/jit/executionstate.h"
40 #include "vm/jit/jit.hpp"
41 #include "vm/jit/trap.hpp"
44 /* md_init *********************************************************************
46 Do some machine dependent initialization.
48 *******************************************************************************/
56 /* md_jit_method_patch_address *************************************************
58 Gets the patch address of the currently compiled method. The offset
59 is extracted from the load instruction(s) before the jump and added
60 to the right base address (PV or REG_METHODPTR).
64 4d 8b 15 e2 fe ff ff mov -286(%rip),%r10
65 49 ff d2 rex64Z callq *%r10
69 4c 8b 17 mov (%rdi),%r10
70 49 8b 82 00 00 00 00 mov 0x0(%r10),%rax
71 48 ff d3 rex64 callq *%rax
75 4c 8b 17 mov (%rdi),%r10
76 4d 8b 92 00 00 00 00 mov 0x0(%r10),%r10
77 49 8b 82 00 00 00 00 mov 0x0(%r10),%rax
78 48 ff d3 rex64 callq *%r11
80 *******************************************************************************/
82 void *md_jit_method_patch_address(void *pv, void *ra, void *mptr)
87 void *pa; /* patch address */
89 /* go back to the actual call instruction (3-bytes) */
91 pc = ((uint8_t *) ra) - 3;
93 /* get the last byte of the call */
97 /* check for the different calls */
100 /* INVOKESTATIC/SPECIAL */
102 /* Get the offset from the instruction (the offset address is
103 4-bytes before the call instruction). */
105 offset = *((int32_t *) (pc - 4));
107 /* add the offset to the return address (IP-relative addressing) */
111 else if (mcode == 0xd3) {
112 /* INVOKEVIRTUAL/INTERFACE */
114 /* return NULL if no mptr was specified (used for replacement) */
119 /* Get the offset from the instruction (the offset address is
120 4-bytes before the call instruction). */
122 offset = *((int32_t *) (pc - 4));
124 /* add the offset to the method pointer */
126 pa = ((uint8_t *) mptr) + offset;
129 /* catch any problems */
131 vm_abort("md_jit_method_patch_address: unknown instruction %x", mcode);
133 /* keep compiler happy */
143 * Decode the trap instruction at the given PC.
145 * @param trp information about trap to be filled
146 * @param sig signal number
147 * @param xpc exception PC
148 * @param es execution state of the machine
149 * @return true if trap was decoded successfully, false otherwise.
151 bool md_trap_decode(trapinfo_t* trp, int sig, void* _xpc, executionstate_t* es)
153 uint8_t* xpc = (uint8_t*) _xpc;
156 // Check for StackOverflowException.
157 threads_check_stackoverflow(sp);
162 // Check for ArithmeticException.
163 trp->type = TRAP_ArithmeticException;
168 // Check for valid trap instruction.
169 if (patcher_is_valid_trap_instruction_at(xpc)) {
170 trp->type = TRAP_PATCHER;
178 // Get exception-throwing instruction.
179 uint8_t opc = M_ALD_MEM_GET_OPC(xpc);
180 uint8_t mod = M_ALD_MEM_GET_MOD(xpc);
181 uint8_t rm = M_ALD_MEM_GET_RM(xpc);
183 // Check for hardware exception, for values
184 // see emit_mov_mem_reg and emit_mem.
185 if ((opc == 0x8b) && (mod == 0) && (rm == 4)) {
186 int32_t d = M_ALD_MEM_GET_REG(xpc);
187 int32_t disp = M_ALD_MEM_GET_DISP(xpc);
189 // We use the exception type as load displacement.
191 trp->value = es->intregs[d];
195 // Default case is a normal NullPointerException.
197 trp->type = TRAP_NullPointerException;
209 /* md_patch_replacement_point **************************************************
211 Patch the given replacement point.
213 *******************************************************************************/
215 #if defined(ENABLE_REPLACEMENT)
216 void md_patch_replacement_point(u1 *pc, u1 *savedmcode, bool revert)
221 /* write saved machine code */
222 *(u2*)(pc) = *(u2*)(savedmcode);
225 /* save the current machine code */
226 *(u2*)(savedmcode) = *(u2*)(pc);
228 /* build the machine code for the patch */
231 /* write new machine code */
232 *(u2*)(pc) = (u2) mcode;
235 /* XXX if required asm_cacheflush(pc,8); */
237 #endif /* defined(ENABLE_REPLACEMENT) */
241 * These are local overrides for various environment variables in Emacs.
242 * Please do not remove this and leave it at the end of the file, where
243 * Emacs will automagically detect them.
244 * ---------------------------------------------------------------------
247 * indent-tabs-mode: t
251 * vim:noexpandtab:sw=4:ts=4: