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 \
52 *((uint16_t*) pr->mpc) = (uint16_t) pr->mcode; \
53 md_icacheflush((void*) pr->mpc, PATCHER_CALL_SIZE); \
57 /* patcher_patch_code **********************************************************
59 Just patches back the original machine code.
61 *******************************************************************************/
63 void patcher_patch_code(patchref_t *pr)
65 PATCH_BACK_ORIGINAL_MCODE;
69 /* patcher_resolve_classref_to_classinfo ***************************************
73 <patched call position>
74 48 bf a0 f0 92 00 00 00 00 00 mov $0x92f0a0,%rdi
78 <patched call position>
79 48 be 30 40 b2 00 00 00 00 00 mov $0xb24030,%rsi
80 48 89 e2 mov %rsp,%rdx
81 48 b8 7c 96 4b 00 00 00 00 00 mov $0x4b967c,%rax
86 <patched call position>
87 48 be b8 3f b2 00 00 00 00 00 mov $0xb23fb8,%rsi
88 48 b8 00 00 00 00 00 00 00 00 mov $0x0,%rax
91 *******************************************************************************/
93 bool patcher_resolve_classref_to_classinfo(patchref_t *pr)
95 constant_classref *cr;
99 cr = (constant_classref *) pr->ref;
100 datap = (intptr_t *) pr->datap;
102 /* get the classinfo */
104 if (!(c = resolve_classref_eager(cr)))
107 PATCH_BACK_ORIGINAL_MCODE;
109 /* patch the classinfo pointer */
111 *datap = (intptr_t) c;
117 /* patcher_resolve_classref_to_vftbl *******************************************
122 <patched call position>
124 *******************************************************************************/
126 bool patcher_resolve_classref_to_vftbl(patchref_t *pr)
128 constant_classref *cr;
132 /* get stuff from the stack */
134 cr = (constant_classref *) pr->ref;
135 datap = (intptr_t *) pr->datap;
137 /* get the fieldinfo */
139 if (!(c = resolve_classref_eager(cr)))
142 PATCH_BACK_ORIGINAL_MCODE;
144 /* patch super class' vftbl */
146 *datap = (intptr_t) c->vftbl;
152 /* patcher_resolve_classref_to_flags *******************************************
154 CHECKCAST/INSTANCEOF:
156 <patched call position>
158 *******************************************************************************/
160 bool patcher_resolve_classref_to_flags(patchref_t *pr)
162 constant_classref *cr;
167 cr = (constant_classref *) pr->ref;
168 datap = (int32_t *) pr->datap;
169 ra = (uint8_t *) pr->mpc;
171 /* get the fieldinfo */
173 if (!(c = resolve_classref_eager(cr)))
176 PATCH_BACK_ORIGINAL_MCODE;
178 /* patch class flags */
180 /* *datap = c->flags; */
181 *((int32_t *) (ra + 2)) = c->flags;
187 /* patcher_get_putstatic *******************************************************
191 <patched call position>
192 4d 8b 15 86 fe ff ff mov -378(%rip),%r10
193 49 8b 32 mov (%r10),%rsi
195 *******************************************************************************/
197 bool patcher_get_putstatic(patchref_t *pr)
199 unresolved_field *uf;
203 uf = (unresolved_field *) pr->ref;
204 datap = (intptr_t *) pr->datap;
206 /* get the fieldinfo */
208 if (!(fi = resolve_field_eager(uf)))
211 /* check if the field's class is initialized */
213 if (!(fi->clazz->state & CLASS_INITIALIZED))
214 if (!initialize_class(fi->clazz))
217 PATCH_BACK_ORIGINAL_MCODE;
219 /* patch the field value's address */
221 *datap = (intptr_t) fi->value;
227 /* patcher_get_putfield ********************************************************
231 <patched call position>
232 45 8b 8f 00 00 00 00 mov 0x0(%r15),%r9d
234 *******************************************************************************/
236 bool patcher_get_putfield(patchref_t *pr)
239 unresolved_field *uf;
243 ra = (uint8_t *) pr->mpc;
244 uf = (unresolved_field *) pr->ref;
246 /* get the fieldinfo */
248 if (!(fi = resolve_field_eager(uf)))
251 PATCH_BACK_ORIGINAL_MCODE;
253 /* Patch the field's offset: we check for the field type, because
254 the instructions have different lengths. */
256 if (IS_INT_LNG_TYPE(fi->type)) {
257 /* Check for special case: %rsp or %r12 as base register. */
262 *((int32_t *) (ra + 4)) = fi->offset;
264 *((int32_t *) (ra + 3)) = fi->offset;
267 /* Check for special case: %rsp or %r12 as base register. */
272 *((int32_t *) (ra + 6)) = fi->offset;
274 *((int32_t *) (ra + 5)) = fi->offset;
281 /* patcher_putfieldconst *******************************************************
285 <patched call position>
286 41 c7 85 00 00 00 00 7b 00 00 00 movl $0x7b,0x0(%r13)
288 *******************************************************************************/
290 bool patcher_putfieldconst(patchref_t *pr)
293 unresolved_field *uf;
297 ra = (uint8_t *) pr->mpc;
298 uf = (unresolved_field *) pr->ref;
300 /* get the fieldinfo */
302 if (!(fi = resolve_field_eager(uf)))
305 PATCH_BACK_ORIGINAL_MCODE;
307 /* patch the field's offset */
309 if (IS_2_WORD_TYPE(fi->type) || IS_ADR_TYPE(fi->type)) {
310 /* handle special case when the base register is %r12 */
315 *((uint32_t *) (ra + 14)) = fi->offset;
318 *((uint32_t *) (ra + 13)) = fi->offset;
322 /* handle special case when the base register is %r12 */
327 *((uint32_t *) (ra + 4)) = fi->offset;
329 *((uint32_t *) (ra + 3)) = fi->offset;
336 /* patcher_invokestatic_special ************************************************
340 <patched call position>
341 49 ba 00 00 00 00 00 00 00 00 mov $0x0,%r10
344 *******************************************************************************/
346 bool patcher_invokestatic_special(patchref_t *pr)
348 unresolved_method *um;
352 /* get stuff from the stack */
354 um = (unresolved_method *) pr->ref;
355 datap = (intptr_t *) pr->datap;
357 /* get the fieldinfo */
359 if (!(m = resolve_method_eager(um)))
362 PATCH_BACK_ORIGINAL_MCODE;
364 /* patch stubroutine */
366 *datap = (intptr_t) m->stubroutine;
372 /* patcher_invokevirtual *******************************************************
376 <patched call position>
377 4c 8b 17 mov (%rdi),%r10
378 49 8b 82 00 00 00 00 mov 0x0(%r10),%rax
381 *******************************************************************************/
383 bool patcher_invokevirtual(patchref_t *pr)
386 unresolved_method *um;
389 ra = (uint8_t *) pr->mpc;
390 um = (unresolved_method *) pr->ref;
392 /* get the methodinfo */
394 if (!(m = resolve_method_eager(um)))
397 PATCH_BACK_ORIGINAL_MCODE;
399 /* patch vftbl index */
401 *((int32_t *) (ra + 3 + 3)) =
402 (int32_t) (OFFSET(vftbl_t, table[0]) +
403 sizeof(methodptr) * m->vftblindex);
409 /* patcher_invokeinterface *****************************************************
413 <patched call position>
414 4c 8b 17 mov (%rdi),%r10
415 4d 8b 92 00 00 00 00 mov 0x0(%r10),%r10
416 49 8b 82 00 00 00 00 mov 0x0(%r10),%rax
419 *******************************************************************************/
421 bool patcher_invokeinterface(patchref_t *pr)
424 unresolved_method *um;
427 /* get stuff from the stack */
429 ra = (uint8_t *) pr->mpc;
430 um = (unresolved_method *) pr->ref;
432 /* get the fieldinfo */
434 if (!(m = resolve_method_eager(um)))
437 PATCH_BACK_ORIGINAL_MCODE;
439 /* patch interfacetable index */
441 *((int32_t *) (ra + 3 + 3)) =
442 (int32_t) (OFFSET(vftbl_t, interfacetable[0]) -
443 sizeof(methodptr) * m->clazz->index);
445 /* patch method offset */
447 *((int32_t *) (ra + 3 + 7 + 3)) =
448 (int32_t) (sizeof(methodptr) * (m - m->clazz->methods));
454 /* patcher_checkcast_interface *************************************************
458 <patched call position>
459 45 8b 9a 1c 00 00 00 mov 0x1c(%r10),%r11d
460 41 81 fb 00 00 00 00 cmp $0x0,%r11d
461 0f 8f 08 00 00 00 jg 0x00002aaaaae511d5
462 48 8b 0c 25 03 00 00 00 mov 0x3,%rcx
463 4d 8b 9a 00 00 00 00 mov 0x0(%r10),%r11
465 *******************************************************************************/
467 bool patcher_checkcast_interface(patchref_t *pr)
470 constant_classref *cr;
473 ra = (uint8_t *) pr->mpc;
474 cr = (constant_classref *) pr->ref;
476 /* get the fieldinfo */
478 if (!(c = resolve_classref_eager(cr)))
481 PATCH_BACK_ORIGINAL_MCODE;
483 /* patch super class index */
485 *((int32_t *) (ra + 7 + 3)) = c->index;
487 *((int32_t *) (ra + 7 + 7 + 6 + 8 + 3)) =
488 (int32_t) (OFFSET(vftbl_t, interfacetable[0]) -
489 c->index * sizeof(methodptr*));
495 /* patcher_instanceof_interface ************************************************
499 <patched call position>
500 45 8b 9a 1c 00 00 00 mov 0x1c(%r10),%r11d
501 41 81 fb 00 00 00 00 cmp $0x0,%r11d
502 0f 8e 94 04 00 00 jle 0x00002aaaaab018f8
503 4d 8b 9a 00 00 00 00 mov 0x0(%r10),%r11
505 *******************************************************************************/
507 bool patcher_instanceof_interface(patchref_t *pr)
510 constant_classref *cr;
513 ra = (uint8_t *) pr->mpc;
514 cr = (constant_classref *) pr->ref;
516 /* get the fieldinfo */
518 if (!(c = resolve_classref_eager(cr)))
521 PATCH_BACK_ORIGINAL_MCODE;
523 /* patch super class index */
525 *((int32_t *) (ra + 7 + 3)) = c->index;
527 *((int32_t *) (ra + 7 + 7 + 6 + 3)) =
528 (int32_t) (OFFSET(vftbl_t, interfacetable[0]) -
529 c->index * sizeof(methodptr*));
536 * These are local overrides for various environment variables in Emacs.
537 * Please do not remove this and leave it at the end of the file, where
538 * Emacs will automagically detect them.
539 * ---------------------------------------------------------------------
542 * indent-tabs-mode: t
546 * vim:noexpandtab:sw=4:ts=4: