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.h"
39 #include "vm/builtin.h"
42 #include "vm/initialize.h"
43 #include "vm/options.h"
44 #include "vm/references.h"
45 #include "vm/resolve.h"
47 #include "vm/jit/patcher-common.h"
50 #define PATCH_BACK_ORIGINAL_MCODE \
55 /* patcher_patch_code **********************************************************
57 Just patches back the original machine code.
59 *******************************************************************************/
61 void patcher_patch_code(patchref_t *pr)
63 *((uint16_t*) pr->mpc) = (uint16_t) pr->mcode;
64 md_icacheflush((void*) pr->mpc, PATCHER_CALL_SIZE);
68 /* patcher_resolve_classref_to_classinfo ***************************************
72 <patched call position>
73 48 bf a0 f0 92 00 00 00 00 00 mov $0x92f0a0,%rdi
77 <patched call position>
78 48 be 30 40 b2 00 00 00 00 00 mov $0xb24030,%rsi
79 48 89 e2 mov %rsp,%rdx
80 48 b8 7c 96 4b 00 00 00 00 00 mov $0x4b967c,%rax
85 <patched call position>
86 48 be b8 3f b2 00 00 00 00 00 mov $0xb23fb8,%rsi
87 48 b8 00 00 00 00 00 00 00 00 mov $0x0,%rax
90 *******************************************************************************/
92 bool patcher_resolve_classref_to_classinfo(patchref_t *pr)
94 constant_classref* cr = (constant_classref*) pr->ref;
95 uintptr_t* datap = (uintptr_t*) pr->datap;
98 classinfo* c = resolve_classref_eager(cr);
104 *datap = (uintptr_t) c;
106 // Synchronize data cache.
107 md_dcacheflush(pr->datap, SIZEOF_VOID_P);
109 // Patch back the original code.
110 patcher_patch_code(pr);
116 /* patcher_resolve_classref_to_vftbl *******************************************
121 <patched call position>
123 *******************************************************************************/
125 bool patcher_resolve_classref_to_vftbl(patchref_t *pr)
127 constant_classref* cr = (constant_classref*) pr->ref;
128 uintptr_t* datap = (uintptr_t*) pr->datap;
130 // Resolve the field.
131 classinfo* c = resolve_classref_eager(cr);
136 // Patch super class' vftbl.
137 *datap = (uintptr_t) c->vftbl;
139 // Synchronize data cache.
140 md_dcacheflush(pr->datap, SIZEOF_VOID_P);
142 // Patch back the original code.
143 patcher_patch_code(pr);
149 /* patcher_resolve_classref_to_flags *******************************************
151 CHECKCAST/INSTANCEOF:
153 <patched call position>
155 *******************************************************************************/
157 bool patcher_resolve_classref_to_flags(patchref_t *pr)
159 constant_classref* cr = (constant_classref*) pr->ref;
160 /* int32_t* datap = (int32_t*) pr->datap; */
161 uint8_t* ra = (uint8_t*) pr->mpc;
163 // Resolve the field.
164 classinfo* c = resolve_classref_eager(cr);
169 // Patch class flags.
170 /* *datap = c->flags; */
171 *((int32_t*) (ra + 2)) = c->flags;
173 // Synchronize data cache.
174 /* md_dcacheflush(datap, sizeof(int32_t)); */
175 md_icacheflush(ra + 2, sizeof(int32_t));
177 // Patch back the original code.
178 patcher_patch_code(pr);
184 /* patcher_get_putstatic *******************************************************
188 <patched call position>
189 4d 8b 15 86 fe ff ff mov -378(%rip),%r10
190 49 8b 32 mov (%r10),%rsi
192 *******************************************************************************/
194 bool patcher_get_putstatic(patchref_t *pr)
196 unresolved_field* uf = (unresolved_field*) pr->ref;
197 uintptr_t* datap = (uintptr_t*) pr->datap;
199 // Resolve the field.
200 fieldinfo* fi = resolve_field_eager(uf);
205 // Check if the field's class is initialized/
206 if (!(fi->clazz->state & CLASS_INITIALIZED))
207 if (!initialize_class(fi->clazz))
210 // Patch the field value's address.
211 *datap = (uintptr_t) fi->value;
213 // Synchronize data cache.
214 md_dcacheflush(pr->datap, SIZEOF_VOID_P);
216 // Patch back the original code.
217 patcher_patch_code(pr);
223 /* patcher_get_putfield ********************************************************
227 <patched call position>
228 45 8b 8f 00 00 00 00 mov 0x0(%r15),%r9d
230 *******************************************************************************/
232 bool patcher_get_putfield(patchref_t *pr)
234 uint8_t* pc = (uint8_t*) pr->mpc;
235 unresolved_field* uf = (unresolved_field*) pr->ref;
237 // Resolve the field.
238 fieldinfo* fi = resolve_field_eager(uf);
243 // Patch the field's offset: we check for the field type, because
244 // the instructions have different lengths.
245 if (IS_INT_LNG_TYPE(fi->type)) {
246 // Check for special case: %rsp or %r12 as base register.
248 *((int32_t*) (pc + 4)) = fi->offset;
250 *((int32_t*) (pc + 3)) = fi->offset;
253 // Check for special case: %rsp or %r12 as base register.
255 *((int32_t*) (pc + 6)) = fi->offset;
257 *((int32_t*) (pc + 5)) = fi->offset;
260 // Synchronize instruction cache.
261 md_icacheflush(pc, 6 + sizeof(int32_t));
263 // Patch back the original code.
264 patcher_patch_code(pr);
270 /* patcher_putfieldconst *******************************************************
274 <patched call position>
275 41 c7 85 00 00 00 00 7b 00 00 00 movl $0x7b,0x0(%r13)
277 *******************************************************************************/
279 bool patcher_putfieldconst(patchref_t *pr)
281 uint8_t* pc = (uint8_t*) pr->mpc;
282 unresolved_field* uf = (unresolved_field*) pr->ref;
284 // Resolve the field.
285 fieldinfo* fi = resolve_field_eager(uf);
290 // Patch the field's offset.
291 if (IS_2_WORD_TYPE(fi->type) || IS_ADR_TYPE(fi->type)) {
292 // Handle special case when the base register is %r12.
294 *((uint32_t*) (pc + 14)) = fi->offset;
296 *((uint32_t*) (pc + 13)) = fi->offset;
299 // Handle special case when the base register is %r12.
301 *((uint32_t*) (pc + 4)) = fi->offset;
303 *((uint32_t*) (pc + 3)) = fi->offset;
306 // Synchronize instruction cache.
307 md_icacheflush(pc, 14 + sizeof(int32_t));
309 // Patch back the original code.
310 patcher_patch_code(pr);
316 /* patcher_invokestatic_special ************************************************
320 <patched call position>
321 49 ba 00 00 00 00 00 00 00 00 mov $0x0,%r10
324 *******************************************************************************/
326 bool patcher_invokestatic_special(patchref_t *pr)
328 unresolved_method* um = (unresolved_method*) pr->ref;
329 uintptr_t* datap = (uintptr_t*) pr->datap;
331 // Resolve the method.
332 methodinfo* m = resolve_method_eager(um);
337 // Patch stubroutine.
338 *datap = (uintptr_t) m->stubroutine;
340 // Synchronize data cache.
341 md_dcacheflush(pr->datap, SIZEOF_VOID_P);
343 // Patch back the original code.
344 patcher_patch_code(pr);
350 /* patcher_invokevirtual *******************************************************
354 <patched call position>
355 4c 8b 17 mov (%rdi),%r10
356 49 8b 82 00 00 00 00 mov 0x0(%r10),%rax
359 *******************************************************************************/
361 bool patcher_invokevirtual(patchref_t *pr)
363 uint8_t* pc = (uint8_t*) pr->mpc;
364 unresolved_method* um = (unresolved_method*) pr->ref;
366 // Resovlve the method.
367 methodinfo* m = resolve_method_eager(um);
372 // Patch vftbl index.
373 *((int32_t*) (pc + 3 + 3)) = (int32_t) (OFFSET(vftbl_t, table[0]) + sizeof(methodptr) * m->vftblindex);
375 // Synchronize instruction cache.
376 md_icacheflush(pc + 3 + 3, SIZEOF_VOID_P);
378 // Patch back the original code.
379 patcher_patch_code(pr);
385 /* patcher_invokeinterface *****************************************************
389 <patched call position>
390 4c 8b 17 mov (%rdi),%r10
391 4d 8b 92 00 00 00 00 mov 0x0(%r10),%r10
392 49 8b 82 00 00 00 00 mov 0x0(%r10),%rax
395 *******************************************************************************/
397 bool patcher_invokeinterface(patchref_t *pr)
399 uint8_t* pc = (uint8_t*) pr->mpc;
400 unresolved_method* um = (unresolved_method*) pr->ref;
402 // Resolve the method.
403 methodinfo* m = resolve_method_eager(um);
408 // Patch interfacetable index.
409 *((int32_t*) (pc + 3 + 3)) = (int32_t) (OFFSET(vftbl_t, interfacetable[0]) - sizeof(methodptr) * m->clazz->index);
411 // Patch method offset.
412 *((int32_t*) (pc + 3 + 7 + 3)) = (int32_t) (sizeof(methodptr) * (m - m->clazz->methods));
414 // Synchronize instruction cache.
415 md_icacheflush(pc + 3 + 3, SIZEOF_VOID_P + 3 + SIZEOF_VOID_P);
417 // Patch back the original code.
418 patcher_patch_code(pr);
424 /* patcher_checkcast_interface *************************************************
428 <patched call position>
429 45 8b 9a 1c 00 00 00 mov 0x1c(%r10),%r11d
430 41 81 fb 00 00 00 00 cmp $0x0,%r11d
431 0f 8f 08 00 00 00 jg 0x00002aaaaae511d5
432 48 8b 0c 25 03 00 00 00 mov 0x3,%rcx
433 4d 8b 9a 00 00 00 00 mov 0x0(%r10),%r11
435 *******************************************************************************/
437 bool patcher_checkcast_interface(patchref_t *pr)
439 uint8_t* pc = (uint8_t*) pr->mpc;
440 constant_classref* cr = (constant_classref*) pr->ref;
442 // Resolve the class.
443 classinfo* c = resolve_classref_eager(cr);
448 // Patch super class index.
449 *((int32_t*) (pc + 7 + 3)) = c->index;
451 *((int32_t*) (pc + 7 + 7 + 6 + 8 + 3)) = (int32_t) (OFFSET(vftbl_t, interfacetable[0]) - c->index * sizeof(methodptr*));
453 // Synchronize instruction cache.
454 md_icacheflush(pc + 7 + 3, sizeof(int32_t) + 6 + 8 + 3 + sizeof(int32_t));
456 // Patch back the original code.
457 patcher_patch_code(pr);
463 /* patcher_instanceof_interface ************************************************
467 <patched call position>
468 45 8b 9a 1c 00 00 00 mov 0x1c(%r10),%r11d
469 41 81 fb 00 00 00 00 cmp $0x0,%r11d
470 0f 8e 94 04 00 00 jle 0x00002aaaaab018f8
471 4d 8b 9a 00 00 00 00 mov 0x0(%r10),%r11
473 *******************************************************************************/
475 bool patcher_instanceof_interface(patchref_t *pr)
477 uint8_t* pc = (uint8_t*) pr->mpc;
478 constant_classref* cr = (constant_classref*) pr->ref;
480 // Resolve the class.
481 classinfo* c = resolve_classref_eager(cr);
486 // Patch super class index.
487 *((int32_t*) (pc + 7 + 3)) = c->index;
489 *((int32_t*) (pc + 7 + 7 + 6 + 3)) = (int32_t) (OFFSET(vftbl_t, interfacetable[0]) - c->index * sizeof(methodptr*));
491 // Synchronize instruction cache.
492 md_icacheflush(pc + 7 + 3, sizeof(int32_t) + 6 + 3 + sizeof(int32_t));
494 // Patch back the original code.
495 patcher_patch_code(pr);
502 * These are local overrides for various environment variables in Emacs.
503 * Please do not remove this and leave it at the end of the file, where
504 * Emacs will automagically detect them.
505 * ---------------------------------------------------------------------
508 * indent-tabs-mode: t
512 * vim:noexpandtab:sw=4:ts=4: