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.h"
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 /* patcher_resolve_classref_to_classinfo ***************************************
67 <patched call position>
68 48 bf a0 f0 92 00 00 00 00 00 mov $0x92f0a0,%rdi
72 <patched call position>
73 48 be 30 40 b2 00 00 00 00 00 mov $0xb24030,%rsi
74 48 89 e2 mov %rsp,%rdx
75 48 b8 7c 96 4b 00 00 00 00 00 mov $0x4b967c,%rax
80 <patched call position>
81 48 be b8 3f b2 00 00 00 00 00 mov $0xb23fb8,%rsi
82 48 b8 00 00 00 00 00 00 00 00 mov $0x0,%rax
85 *******************************************************************************/
87 bool patcher_resolve_classref_to_classinfo(patchref_t *pr)
89 constant_classref* cr = (constant_classref*) pr->ref;
90 uintptr_t* datap = (uintptr_t*) pr->datap;
93 classinfo* c = resolve_classref_eager(cr);
99 *datap = (uintptr_t) c;
101 // Synchronize data cache.
102 md_dcacheflush((void*) pr->datap, SIZEOF_VOID_P);
104 // Patch back the original code.
105 patcher_patch_code(pr);
111 /* patcher_resolve_classref_to_vftbl *******************************************
116 <patched call position>
118 *******************************************************************************/
120 bool patcher_resolve_classref_to_vftbl(patchref_t *pr)
122 constant_classref* cr = (constant_classref*) pr->ref;
123 uintptr_t* datap = (uintptr_t*) pr->datap;
125 // Resolve the field.
126 classinfo* c = resolve_classref_eager(cr);
131 // Patch super class' vftbl.
132 *datap = (uintptr_t) c->vftbl;
134 // Synchronize data cache.
135 md_dcacheflush((void*) pr->datap, SIZEOF_VOID_P);
137 // Patch back the original code.
138 patcher_patch_code(pr);
144 /* patcher_resolve_classref_to_flags *******************************************
146 CHECKCAST/INSTANCEOF:
148 <patched call position>
150 *******************************************************************************/
152 bool patcher_resolve_classref_to_flags(patchref_t *pr)
154 constant_classref* cr = (constant_classref*) pr->ref;
155 /* int32_t* datap = (int32_t*) pr->datap; */
156 uint8_t* ra = (uint8_t*) pr->mpc;
158 // Resolve the field.
159 classinfo* c = resolve_classref_eager(cr);
164 // Patch class flags.
165 /* *datap = c->flags; */
166 *((int32_t*) (ra + 2)) = c->flags;
168 // Synchronize data cache.
169 /* md_dcacheflush(datap, sizeof(int32_t)); */
170 md_icacheflush(ra + 2, sizeof(int32_t));
172 // Patch back the original code.
173 patcher_patch_code(pr);
179 /* patcher_get_putstatic *******************************************************
183 <patched call position>
184 4d 8b 15 86 fe ff ff mov -378(%rip),%r10
185 49 8b 32 mov (%r10),%rsi
187 *******************************************************************************/
189 bool patcher_get_putstatic(patchref_t *pr)
191 unresolved_field* uf = (unresolved_field*) pr->ref;
192 uintptr_t* datap = (uintptr_t*) pr->datap;
194 // Resolve the field.
195 fieldinfo* fi = resolve_field_eager(uf);
200 // Check if the field's class is initialized/
201 if (!(fi->clazz->state & CLASS_INITIALIZED))
202 if (!initialize_class(fi->clazz))
205 // Patch the field value's address.
206 *datap = (uintptr_t) fi->value;
208 // Synchronize data cache.
209 md_dcacheflush((void*) pr->datap, SIZEOF_VOID_P);
211 // Patch back the original code.
212 patcher_patch_code(pr);
218 /* patcher_get_putfield ********************************************************
222 <patched call position>
223 45 8b 8f 00 00 00 00 mov 0x0(%r15),%r9d
225 *******************************************************************************/
227 bool patcher_get_putfield(patchref_t *pr)
229 uint8_t* pc = (uint8_t*) pr->mpc;
230 unresolved_field* uf = (unresolved_field*) pr->ref;
232 // Resolve the field.
233 fieldinfo* fi = resolve_field_eager(uf);
238 // Patch the field's offset: we check for the field type, because
239 // the instructions have different lengths.
240 if (IS_INT_LNG_TYPE(fi->type)) {
241 // Check for special case: %rsp or %r12 as base register.
243 *((int32_t*) (pc + 4)) = fi->offset;
245 *((int32_t*) (pc + 3)) = fi->offset;
248 // Check for special case: %rsp or %r12 as base register.
250 *((int32_t*) (pc + 6)) = fi->offset;
252 *((int32_t*) (pc + 5)) = fi->offset;
255 // Synchronize instruction cache.
256 md_icacheflush(pc, 6 + sizeof(int32_t));
258 // Patch back the original code.
259 patcher_patch_code(pr);
265 /* patcher_putfieldconst *******************************************************
269 <patched call position>
270 41 c7 85 00 00 00 00 7b 00 00 00 movl $0x7b,0x0(%r13)
272 *******************************************************************************/
274 bool patcher_putfieldconst(patchref_t *pr)
276 uint8_t* pc = (uint8_t*) pr->mpc;
277 unresolved_field* uf = (unresolved_field*) pr->ref;
279 // Resolve the field.
280 fieldinfo* fi = resolve_field_eager(uf);
285 // Patch the field's offset.
286 if (IS_2_WORD_TYPE(fi->type) || IS_ADR_TYPE(fi->type)) {
287 // Handle special case when the base register is %r12.
289 *((uint32_t*) (pc + 14)) = fi->offset;
291 *((uint32_t*) (pc + 13)) = fi->offset;
294 // Handle special case when the base register is %r12.
296 *((uint32_t*) (pc + 4)) = fi->offset;
298 *((uint32_t*) (pc + 3)) = fi->offset;
301 // Synchronize instruction cache.
302 md_icacheflush(pc, 14 + sizeof(int32_t));
304 // Patch back the original code.
305 patcher_patch_code(pr);
311 /* patcher_invokestatic_special ************************************************
315 <patched call position>
316 49 ba 00 00 00 00 00 00 00 00 mov $0x0,%r10
319 *******************************************************************************/
321 bool patcher_invokestatic_special(patchref_t *pr)
323 unresolved_method* um = (unresolved_method*) pr->ref;
324 uintptr_t* datap = (uintptr_t*) pr->datap;
326 // Resolve the method.
327 methodinfo* m = resolve_method_eager(um);
332 // Patch stubroutine.
333 *datap = (uintptr_t) m->stubroutine;
335 // Synchronize data cache.
336 md_dcacheflush((void*) pr->datap, SIZEOF_VOID_P);
338 // Patch back the original code.
339 patcher_patch_code(pr);
345 /* patcher_invokevirtual *******************************************************
349 <patched call position>
350 4c 8b 17 mov (%rdi),%r10
351 49 8b 82 00 00 00 00 mov 0x0(%r10),%rax
354 *******************************************************************************/
356 bool patcher_invokevirtual(patchref_t *pr)
358 uint8_t* pc = (uint8_t*) pr->mpc;
359 unresolved_method* um = (unresolved_method*) pr->ref;
361 // Resovlve the method.
362 methodinfo* m = resolve_method_eager(um);
367 // Patch vftbl index.
368 *((int32_t*) (pc + 3 + 3)) = (int32_t) (OFFSET(vftbl_t, table[0]) + sizeof(methodptr) * m->vftblindex);
370 // Synchronize instruction cache.
371 md_icacheflush(pc + 3 + 3, SIZEOF_VOID_P);
373 // Patch back the original code.
374 patcher_patch_code(pr);
380 /* patcher_invokeinterface *****************************************************
384 <patched call position>
385 4c 8b 17 mov (%rdi),%r10
386 4d 8b 92 00 00 00 00 mov 0x0(%r10),%r10
387 49 8b 82 00 00 00 00 mov 0x0(%r10),%rax
390 *******************************************************************************/
392 bool patcher_invokeinterface(patchref_t *pr)
394 uint8_t* pc = (uint8_t*) pr->mpc;
395 unresolved_method* um = (unresolved_method*) pr->ref;
397 // Resolve the method.
398 methodinfo* m = resolve_method_eager(um);
403 // Patch interfacetable index.
404 *((int32_t*) (pc + 3 + 3)) = (int32_t) (OFFSET(vftbl_t, interfacetable[0]) - sizeof(methodptr) * m->clazz->index);
406 // Patch method offset.
407 *((int32_t*) (pc + 3 + 7 + 3)) = (int32_t) (sizeof(methodptr) * (m - m->clazz->methods));
409 // Synchronize instruction cache.
410 md_icacheflush(pc + 3 + 3, SIZEOF_VOID_P + 3 + SIZEOF_VOID_P);
412 // Patch back the original code.
413 patcher_patch_code(pr);
419 /* patcher_checkcast_interface *************************************************
423 <patched call position>
424 45 8b 9a 1c 00 00 00 mov 0x1c(%r10),%r11d
425 41 81 fb 00 00 00 00 cmp $0x0,%r11d
426 0f 8f 08 00 00 00 jg 0x00002aaaaae511d5
427 48 8b 0c 25 03 00 00 00 mov 0x3,%rcx
428 4d 8b 9a 00 00 00 00 mov 0x0(%r10),%r11
430 *******************************************************************************/
432 bool patcher_checkcast_interface(patchref_t *pr)
434 uint8_t* pc = (uint8_t*) pr->mpc;
435 constant_classref* cr = (constant_classref*) pr->ref;
437 // Resolve the class.
438 classinfo* c = resolve_classref_eager(cr);
443 // Patch super class index.
444 *((int32_t*) (pc + 7 + 3)) = c->index;
446 *((int32_t*) (pc + 7 + 7 + 6 + 8 + 3)) = (int32_t) (OFFSET(vftbl_t, interfacetable[0]) - c->index * sizeof(methodptr*));
448 // Synchronize instruction cache.
449 md_icacheflush(pc + 7 + 3, sizeof(int32_t) + 6 + 8 + 3 + sizeof(int32_t));
451 // Patch back the original code.
452 patcher_patch_code(pr);
458 /* patcher_instanceof_interface ************************************************
462 <patched call position>
463 45 8b 9a 1c 00 00 00 mov 0x1c(%r10),%r11d
464 41 81 fb 00 00 00 00 cmp $0x0,%r11d
465 0f 8e 94 04 00 00 jle 0x00002aaaaab018f8
466 4d 8b 9a 00 00 00 00 mov 0x0(%r10),%r11
468 *******************************************************************************/
470 bool patcher_instanceof_interface(patchref_t *pr)
472 uint8_t* pc = (uint8_t*) pr->mpc;
473 constant_classref* cr = (constant_classref*) pr->ref;
475 // Resolve the class.
476 classinfo* c = resolve_classref_eager(cr);
481 // Patch super class index.
482 *((int32_t*) (pc + 7 + 3)) = c->index;
484 *((int32_t*) (pc + 7 + 7 + 6 + 3)) = (int32_t) (OFFSET(vftbl_t, interfacetable[0]) - c->index * sizeof(methodptr*));
486 // Synchronize instruction cache.
487 md_icacheflush(pc + 7 + 3, sizeof(int32_t) + 6 + 3 + sizeof(int32_t));
489 // Patch back the original code.
490 patcher_patch_code(pr);
497 * These are local overrides for various environment variables in Emacs.
498 * Please do not remove this and leave it at the end of the file, where
499 * Emacs will automagically detect them.
500 * ---------------------------------------------------------------------
503 * indent-tabs-mode: t
507 * vim:noexpandtab:sw=4:ts=4: