1 /* src/vm/jit/x86_64/patcher.c - x86_64 code patching functions
3 Copyright (C) 1996-2005, 2006, 2007, 2008, 2009
4 CACAOVM - Verein zur Foerderung der freien virtuellen Maschine CACAO
6 This file is part of CACAO.
8 This program is free software; you can redistribute it and/or
9 modify it under the terms of the GNU General Public License as
10 published by the Free Software Foundation; either version 2, or (at
11 your option) any later version.
13 This program is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
32 #include "vm/jit/x86_64/codegen.h"
33 #include "vm/jit/x86_64/md.h"
35 #include "mm/memory.hpp"
37 #include "native/native.hpp"
39 #include "vm/jit/builtin.hpp"
40 #include "vm/class.hpp"
41 #include "vm/field.hpp"
42 #include "vm/initialize.hpp"
43 #include "vm/options.h"
44 #include "vm/references.h"
45 #include "vm/resolve.hpp"
47 #include "vm/jit/patcher-common.hpp"
50 /* patcher_patch_code **********************************************************
52 Just patches back the original machine code.
54 *******************************************************************************/
56 void patcher_patch_code(patchref_t *pr)
58 *((uint16_t*) pr->mpc) = (uint16_t) pr->mcode;
59 md_icacheflush((void*) pr->mpc, PATCHER_CALL_SIZE);
63 * Check if the trap instruction at the given PC is valid.
65 * @param pc Program counter.
67 * @return true if valid, false otherwise.
69 bool patcher_is_valid_trap_instruction_at(void* pc)
71 uint16_t mcode = *((uint16_t*) pc);
73 // Check for the undefined instruction we use.
74 return (mcode == 0x0b0f);
78 /* patcher_resolve_classref_to_classinfo ***************************************
82 <patched call position>
83 48 bf a0 f0 92 00 00 00 00 00 mov $0x92f0a0,%rdi
87 <patched call position>
88 48 be 30 40 b2 00 00 00 00 00 mov $0xb24030,%rsi
89 48 89 e2 mov %rsp,%rdx
90 48 b8 7c 96 4b 00 00 00 00 00 mov $0x4b967c,%rax
95 <patched call position>
96 48 be b8 3f b2 00 00 00 00 00 mov $0xb23fb8,%rsi
97 48 b8 00 00 00 00 00 00 00 00 mov $0x0,%rax
100 *******************************************************************************/
102 bool patcher_resolve_classref_to_classinfo(patchref_t *pr)
104 constant_classref* cr = (constant_classref*) pr->ref;
105 uintptr_t* datap = (uintptr_t*) pr->datap;
107 // Resolve the class.
108 classinfo* c = resolve_classref_eager(cr);
114 *datap = (uintptr_t) c;
116 // Synchronize data cache.
117 md_dcacheflush((void*) pr->datap, SIZEOF_VOID_P);
119 // Patch back the original code.
120 patcher_patch_code(pr);
126 /* patcher_resolve_classref_to_vftbl *******************************************
131 <patched call position>
133 *******************************************************************************/
135 bool patcher_resolve_classref_to_vftbl(patchref_t *pr)
137 constant_classref* cr = (constant_classref*) pr->ref;
138 uintptr_t* datap = (uintptr_t*) pr->datap;
140 // Resolve the field.
141 classinfo* c = resolve_classref_eager(cr);
146 // Patch super class' vftbl.
147 *datap = (uintptr_t) c->vftbl;
149 // Synchronize data cache.
150 md_dcacheflush((void*) pr->datap, SIZEOF_VOID_P);
152 // Patch back the original code.
153 patcher_patch_code(pr);
159 /* patcher_resolve_classref_to_flags *******************************************
161 CHECKCAST/INSTANCEOF:
163 <patched call position>
165 *******************************************************************************/
167 bool patcher_resolve_classref_to_flags(patchref_t *pr)
169 constant_classref* cr = (constant_classref*) pr->ref;
170 /* int32_t* datap = (int32_t*) pr->datap; */
171 uint8_t* ra = (uint8_t*) pr->mpc;
173 // Resolve the field.
174 classinfo* c = resolve_classref_eager(cr);
179 ra += PATCHER_CALL_SIZE;
181 // Patch class flags.
182 /* *datap = c->flags; */
183 *((int32_t*) (ra + 2)) = c->flags;
185 // Synchronize data cache.
186 /* md_dcacheflush(datap, sizeof(int32_t)); */
187 md_icacheflush(ra + 2, sizeof(int32_t));
189 // Patch back the original code.
190 patcher_patch_code(pr);
196 /* patcher_get_putstatic *******************************************************
200 <patched call position>
201 4d 8b 15 86 fe ff ff mov -378(%rip),%r10
202 49 8b 32 mov (%r10),%rsi
204 *******************************************************************************/
206 bool patcher_get_putstatic(patchref_t *pr)
208 unresolved_field* uf = (unresolved_field*) pr->ref;
209 uintptr_t* datap = (uintptr_t*) pr->datap;
210 uint8_t* ra = (uint8_t*) pr->mpc;
212 // Resolve the field.
213 fieldinfo* fi = resolve_field_eager(uf);
218 ra += PATCHER_CALL_SIZE;
220 // Check if the field's class is initialized/
221 if (!(fi->clazz->state & CLASS_INITIALIZED))
222 if (!initialize_class(fi->clazz))
225 // Patch the field value's address.
226 *datap = (uintptr_t) fi->value;
228 // Synchronize data cache.
229 md_dcacheflush((void*) pr->datap, SIZEOF_VOID_P);
231 // Patch back the original code.
232 patcher_patch_code(pr);
238 /* patcher_get_putfield ********************************************************
242 <patched call position>
243 45 8b 8f 00 00 00 00 mov 0x0(%r15),%r9d
245 *******************************************************************************/
247 bool patcher_get_putfield(patchref_t *pr)
249 uint8_t* pc = (uint8_t*) pr->mpc;
250 unresolved_field* uf = (unresolved_field*) pr->ref;
252 // Resolve the field.
253 fieldinfo* fi = resolve_field_eager(uf);
258 pc += PATCHER_CALL_SIZE;
260 // Patch the field's offset: we check for the field type, because
261 // the instructions have different lengths.
262 if (IS_INT_LNG_TYPE(fi->type)) {
263 // Check for special case: %rsp or %r12 as base register.
265 *((int32_t*) (pc + 4)) = fi->offset;
267 *((int32_t*) (pc + 3)) = fi->offset;
270 // Check for special case: %rsp or %r12 as base register.
272 *((int32_t*) (pc + 6)) = fi->offset;
274 *((int32_t*) (pc + 5)) = fi->offset;
277 // Synchronize instruction cache.
278 md_icacheflush(pc, 6 + sizeof(int32_t));
280 // Patch back the original code.
281 patcher_patch_code(pr);
287 /* patcher_putfieldconst *******************************************************
291 <patched call position>
292 41 c7 85 00 00 00 00 7b 00 00 00 movl $0x7b,0x0(%r13)
294 *******************************************************************************/
296 bool patcher_putfieldconst(patchref_t *pr)
298 uint8_t* pc = (uint8_t*) pr->mpc;
299 unresolved_field* uf = (unresolved_field*) pr->ref;
301 // Resolve the field.
302 fieldinfo* fi = resolve_field_eager(uf);
307 pc += PATCHER_CALL_SIZE;
309 // Patch the field's offset.
310 if (IS_2_WORD_TYPE(fi->type) || IS_ADR_TYPE(fi->type)) {
311 // Handle special case when the base register is %r12.
313 *((uint32_t*) (pc + 14)) = fi->offset;
315 *((uint32_t*) (pc + 13)) = fi->offset;
318 // Handle special case when the base register is %r12.
320 *((uint32_t*) (pc + 4)) = fi->offset;
322 *((uint32_t*) (pc + 3)) = fi->offset;
325 // Synchronize instruction cache.
326 md_icacheflush(pc, 14 + sizeof(int32_t));
328 // Patch back the original code.
329 patcher_patch_code(pr);
335 /* patcher_invokestatic_special ************************************************
339 <patched call position>
340 49 ba 00 00 00 00 00 00 00 00 mov $0x0,%r10
343 *******************************************************************************/
345 bool patcher_invokestatic_special(patchref_t *pr)
347 unresolved_method* um = (unresolved_method*) pr->ref;
348 uintptr_t* datap = (uintptr_t*) pr->datap;
350 // Resolve the method.
351 methodinfo* m = resolve_method_eager(um);
356 // Patch stubroutine.
357 *datap = (uintptr_t) m->stubroutine;
359 // Synchronize data cache.
360 md_dcacheflush((void*) pr->datap, SIZEOF_VOID_P);
362 // Patch back the original code.
363 patcher_patch_code(pr);
369 /* patcher_invokevirtual *******************************************************
373 <patched call position>
374 4c 8b 17 mov (%rdi),%r10
375 49 8b 82 00 00 00 00 mov 0x0(%r10),%rax
378 *******************************************************************************/
380 bool patcher_invokevirtual(patchref_t *pr)
382 uint8_t* pc = (uint8_t*) pr->mpc;
383 unresolved_method* um = (unresolved_method*) pr->ref;
385 // Resovlve the method.
386 methodinfo* m = resolve_method_eager(um);
391 pc += PATCHER_CALL_SIZE;
393 // Patch vftbl index.
394 *((int32_t*) (pc + 3 + 3)) = (int32_t) (OFFSET(vftbl_t, table[0]) + sizeof(methodptr) * m->vftblindex);
396 // Synchronize instruction cache.
397 md_icacheflush(pc + 3 + 3, SIZEOF_VOID_P);
399 // Patch back the original code.
400 patcher_patch_code(pr);
406 /* patcher_invokeinterface *****************************************************
410 <patched call position>
411 4c 8b 17 mov (%rdi),%r10
412 4d 8b 92 00 00 00 00 mov 0x0(%r10),%r10
413 49 8b 82 00 00 00 00 mov 0x0(%r10),%rax
416 *******************************************************************************/
418 bool patcher_invokeinterface(patchref_t *pr)
420 uint8_t* pc = (uint8_t*) pr->mpc;
421 unresolved_method* um = (unresolved_method*) pr->ref;
423 // Resolve the method.
424 methodinfo* m = resolve_method_eager(um);
429 pc += PATCHER_CALL_SIZE;
431 // Patch interfacetable index.
432 *((int32_t*) (pc + 3 + 3)) = (int32_t) (OFFSET(vftbl_t, interfacetable[0]) - sizeof(methodptr) * m->clazz->index);
434 // Patch method offset.
435 *((int32_t*) (pc + 3 + 7 + 3)) = (int32_t) (sizeof(methodptr) * (m - m->clazz->methods));
437 // Synchronize instruction cache.
438 md_icacheflush(pc + 3 + 3, SIZEOF_VOID_P + 3 + SIZEOF_VOID_P);
440 // Patch back the original code.
441 patcher_patch_code(pr);
447 /* patcher_checkcast_interface *************************************************
451 <patched call position>
452 45 8b 9a 1c 00 00 00 mov 0x1c(%r10),%r11d
453 41 81 fb 00 00 00 00 cmp $0x0,%r11d
454 0f 8f 08 00 00 00 jg 0x00002aaaaae511d5
455 48 8b 0c 25 03 00 00 00 mov 0x3,%rcx
456 4d 8b 9a 00 00 00 00 mov 0x0(%r10),%r11
458 *******************************************************************************/
460 bool patcher_checkcast_interface(patchref_t *pr)
462 uint8_t* pc = (uint8_t*) pr->mpc;
463 constant_classref* cr = (constant_classref*) pr->ref;
465 // Resolve the class.
466 classinfo* c = resolve_classref_eager(cr);
471 pc += PATCHER_CALL_SIZE;
473 // Patch super class index.
474 *((int32_t*) (pc + 7 + 3)) = c->index;
476 *((int32_t*) (pc + 7 + 7 + 6 + 8 + 3)) = (int32_t) (OFFSET(vftbl_t, interfacetable[0]) - c->index * sizeof(methodptr*));
478 // Synchronize instruction cache.
479 md_icacheflush(pc + 7 + 3, sizeof(int32_t) + 6 + 8 + 3 + sizeof(int32_t));
481 // Patch back the original code.
482 patcher_patch_code(pr);
488 /* patcher_instanceof_interface ************************************************
492 <patched call position>
493 45 8b 9a 1c 00 00 00 mov 0x1c(%r10),%r11d
494 41 81 fb 00 00 00 00 cmp $0x0,%r11d
495 0f 8e 94 04 00 00 jle 0x00002aaaaab018f8
496 4d 8b 9a 00 00 00 00 mov 0x0(%r10),%r11
498 *******************************************************************************/
500 bool patcher_instanceof_interface(patchref_t *pr)
502 uint8_t* pc = (uint8_t*) pr->mpc;
503 constant_classref* cr = (constant_classref*) pr->ref;
505 // Resolve the class.
506 classinfo* c = resolve_classref_eager(cr);
511 pc += PATCHER_CALL_SIZE;
513 // Patch super class index.
514 *((int32_t*) (pc + 7 + 3)) = c->index;
516 *((int32_t*) (pc + 7 + 7 + 6 + 3)) = (int32_t) (OFFSET(vftbl_t, interfacetable[0]) - c->index * sizeof(methodptr*));
518 // Synchronize instruction cache.
519 md_icacheflush(pc + 7 + 3, sizeof(int32_t) + 6 + 3 + sizeof(int32_t));
521 // Patch back the original code.
522 patcher_patch_code(pr);
529 * These are local overrides for various environment variables in Emacs.
530 * Please do not remove this and leave it at the end of the file, where
531 * Emacs will automagically detect them.
532 * ---------------------------------------------------------------------
535 * indent-tabs-mode: t
539 * vim:noexpandtab:sw=4:ts=4: