1 /* src/vm/jit/x86_64/patcher.c - x86_64 code patching functions
3 Copyright (C) 1996-2005, 2006, 2007, 2008
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 // Patch class flags.
180 /* *datap = c->flags; */
181 *((int32_t*) (ra + 2)) = c->flags;
183 // Synchronize data cache.
184 /* md_dcacheflush(datap, sizeof(int32_t)); */
185 md_icacheflush(ra + 2, sizeof(int32_t));
187 // Patch back the original code.
188 patcher_patch_code(pr);
194 /* patcher_get_putstatic *******************************************************
198 <patched call position>
199 4d 8b 15 86 fe ff ff mov -378(%rip),%r10
200 49 8b 32 mov (%r10),%rsi
202 *******************************************************************************/
204 bool patcher_get_putstatic(patchref_t *pr)
206 unresolved_field* uf = (unresolved_field*) pr->ref;
207 uintptr_t* datap = (uintptr_t*) pr->datap;
209 // Resolve the field.
210 fieldinfo* fi = resolve_field_eager(uf);
215 // Check if the field's class is initialized/
216 if (!(fi->clazz->state & CLASS_INITIALIZED))
217 if (!initialize_class(fi->clazz))
220 // Patch the field value's address.
221 *datap = (uintptr_t) fi->value;
223 // Synchronize data cache.
224 md_dcacheflush((void*) pr->datap, SIZEOF_VOID_P);
226 // Patch back the original code.
227 patcher_patch_code(pr);
233 /* patcher_get_putfield ********************************************************
237 <patched call position>
238 45 8b 8f 00 00 00 00 mov 0x0(%r15),%r9d
240 *******************************************************************************/
242 bool patcher_get_putfield(patchref_t *pr)
244 uint8_t* pc = (uint8_t*) pr->mpc;
245 unresolved_field* uf = (unresolved_field*) pr->ref;
247 // Resolve the field.
248 fieldinfo* fi = resolve_field_eager(uf);
253 // Patch the field's offset: we check for the field type, because
254 // the instructions have different lengths.
255 if (IS_INT_LNG_TYPE(fi->type)) {
256 // Check for special case: %rsp or %r12 as base register.
258 *((int32_t*) (pc + 4)) = fi->offset;
260 *((int32_t*) (pc + 3)) = fi->offset;
263 // Check for special case: %rsp or %r12 as base register.
265 *((int32_t*) (pc + 6)) = fi->offset;
267 *((int32_t*) (pc + 5)) = fi->offset;
270 // Synchronize instruction cache.
271 md_icacheflush(pc, 6 + sizeof(int32_t));
273 // Patch back the original code.
274 patcher_patch_code(pr);
280 /* patcher_putfieldconst *******************************************************
284 <patched call position>
285 41 c7 85 00 00 00 00 7b 00 00 00 movl $0x7b,0x0(%r13)
287 *******************************************************************************/
289 bool patcher_putfieldconst(patchref_t *pr)
291 uint8_t* pc = (uint8_t*) pr->mpc;
292 unresolved_field* uf = (unresolved_field*) pr->ref;
294 // Resolve the field.
295 fieldinfo* fi = resolve_field_eager(uf);
300 // Patch the field's offset.
301 if (IS_2_WORD_TYPE(fi->type) || IS_ADR_TYPE(fi->type)) {
302 // Handle special case when the base register is %r12.
304 *((uint32_t*) (pc + 14)) = fi->offset;
306 *((uint32_t*) (pc + 13)) = fi->offset;
309 // Handle special case when the base register is %r12.
311 *((uint32_t*) (pc + 4)) = fi->offset;
313 *((uint32_t*) (pc + 3)) = fi->offset;
316 // Synchronize instruction cache.
317 md_icacheflush(pc, 14 + sizeof(int32_t));
319 // Patch back the original code.
320 patcher_patch_code(pr);
326 /* patcher_invokestatic_special ************************************************
330 <patched call position>
331 49 ba 00 00 00 00 00 00 00 00 mov $0x0,%r10
334 *******************************************************************************/
336 bool patcher_invokestatic_special(patchref_t *pr)
338 unresolved_method* um = (unresolved_method*) pr->ref;
339 uintptr_t* datap = (uintptr_t*) pr->datap;
341 // Resolve the method.
342 methodinfo* m = resolve_method_eager(um);
347 // Patch stubroutine.
348 *datap = (uintptr_t) m->stubroutine;
350 // Synchronize data cache.
351 md_dcacheflush((void*) pr->datap, SIZEOF_VOID_P);
353 // Patch back the original code.
354 patcher_patch_code(pr);
360 /* patcher_invokevirtual *******************************************************
364 <patched call position>
365 4c 8b 17 mov (%rdi),%r10
366 49 8b 82 00 00 00 00 mov 0x0(%r10),%rax
369 *******************************************************************************/
371 bool patcher_invokevirtual(patchref_t *pr)
373 uint8_t* pc = (uint8_t*) pr->mpc;
374 unresolved_method* um = (unresolved_method*) pr->ref;
376 // Resovlve the method.
377 methodinfo* m = resolve_method_eager(um);
382 // Patch vftbl index.
383 *((int32_t*) (pc + 3 + 3)) = (int32_t) (OFFSET(vftbl_t, table[0]) + sizeof(methodptr) * m->vftblindex);
385 // Synchronize instruction cache.
386 md_icacheflush(pc + 3 + 3, SIZEOF_VOID_P);
388 // Patch back the original code.
389 patcher_patch_code(pr);
395 /* patcher_invokeinterface *****************************************************
399 <patched call position>
400 4c 8b 17 mov (%rdi),%r10
401 4d 8b 92 00 00 00 00 mov 0x0(%r10),%r10
402 49 8b 82 00 00 00 00 mov 0x0(%r10),%rax
405 *******************************************************************************/
407 bool patcher_invokeinterface(patchref_t *pr)
409 uint8_t* pc = (uint8_t*) pr->mpc;
410 unresolved_method* um = (unresolved_method*) pr->ref;
412 // Resolve the method.
413 methodinfo* m = resolve_method_eager(um);
418 // Patch interfacetable index.
419 *((int32_t*) (pc + 3 + 3)) = (int32_t) (OFFSET(vftbl_t, interfacetable[0]) - sizeof(methodptr) * m->clazz->index);
421 // Patch method offset.
422 *((int32_t*) (pc + 3 + 7 + 3)) = (int32_t) (sizeof(methodptr) * (m - m->clazz->methods));
424 // Synchronize instruction cache.
425 md_icacheflush(pc + 3 + 3, SIZEOF_VOID_P + 3 + SIZEOF_VOID_P);
427 // Patch back the original code.
428 patcher_patch_code(pr);
434 /* patcher_checkcast_interface *************************************************
438 <patched call position>
439 45 8b 9a 1c 00 00 00 mov 0x1c(%r10),%r11d
440 41 81 fb 00 00 00 00 cmp $0x0,%r11d
441 0f 8f 08 00 00 00 jg 0x00002aaaaae511d5
442 48 8b 0c 25 03 00 00 00 mov 0x3,%rcx
443 4d 8b 9a 00 00 00 00 mov 0x0(%r10),%r11
445 *******************************************************************************/
447 bool patcher_checkcast_interface(patchref_t *pr)
449 uint8_t* pc = (uint8_t*) pr->mpc;
450 constant_classref* cr = (constant_classref*) pr->ref;
452 // Resolve the class.
453 classinfo* c = resolve_classref_eager(cr);
458 // Patch super class index.
459 *((int32_t*) (pc + 7 + 3)) = c->index;
461 *((int32_t*) (pc + 7 + 7 + 6 + 8 + 3)) = (int32_t) (OFFSET(vftbl_t, interfacetable[0]) - c->index * sizeof(methodptr*));
463 // Synchronize instruction cache.
464 md_icacheflush(pc + 7 + 3, sizeof(int32_t) + 6 + 8 + 3 + sizeof(int32_t));
466 // Patch back the original code.
467 patcher_patch_code(pr);
473 /* patcher_instanceof_interface ************************************************
477 <patched call position>
478 45 8b 9a 1c 00 00 00 mov 0x1c(%r10),%r11d
479 41 81 fb 00 00 00 00 cmp $0x0,%r11d
480 0f 8e 94 04 00 00 jle 0x00002aaaaab018f8
481 4d 8b 9a 00 00 00 00 mov 0x0(%r10),%r11
483 *******************************************************************************/
485 bool patcher_instanceof_interface(patchref_t *pr)
487 uint8_t* pc = (uint8_t*) pr->mpc;
488 constant_classref* cr = (constant_classref*) pr->ref;
490 // Resolve the class.
491 classinfo* c = resolve_classref_eager(cr);
496 // Patch super class index.
497 *((int32_t*) (pc + 7 + 3)) = c->index;
499 *((int32_t*) (pc + 7 + 7 + 6 + 3)) = (int32_t) (OFFSET(vftbl_t, interfacetable[0]) - c->index * sizeof(methodptr*));
501 // Synchronize instruction cache.
502 md_icacheflush(pc + 7 + 3, sizeof(int32_t) + 6 + 3 + sizeof(int32_t));
504 // Patch back the original code.
505 patcher_patch_code(pr);
512 * These are local overrides for various environment variables in Emacs.
513 * Please do not remove this and leave it at the end of the file, where
514 * Emacs will automagically detect them.
515 * ---------------------------------------------------------------------
518 * indent-tabs-mode: t
522 * vim:noexpandtab:sw=4:ts=4: