1 /* src/vm/jit/x86_64/patcher.c - x86_64 code patching functions
3 Copyright (C) 1996-2005, 2006, 2007 R. Grafl, A. Krall, C. Kruegel,
4 C. Oates, R. Obermaisser, M. Platter, M. Probst, S. Ring,
5 E. Steiner, C. Thalinger, D. Thuernbeck, P. Tomsich, C. Ullrich,
6 J. Wenninger, Institut f. Computersprachen - TU Wien
8 This file is part of CACAO.
10 This program is free software; you can redistribute it and/or
11 modify it under the terms of the GNU General Public License as
12 published by the Free Software Foundation; either version 2, or (at
13 your option) any later version.
15 This program is distributed in the hope that it will be useful, but
16 WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
34 #include "vm/jit/x86_64/codegen.h"
36 #include "mm/memory.h"
38 #include "native/native.h"
40 #include "vm/builtin.h"
41 #include "vm/exceptions.h"
42 #include "vm/initialize.h"
44 #include "vm/jit/patcher-common.h"
45 #include "vm/jit/stacktrace.h"
47 #include "vmcore/class.h"
48 #include "vmcore/field.h"
49 #include "vmcore/options.h"
50 #include "vmcore/references.h"
51 #include "vm/resolve.h"
54 #define PATCH_BACK_ORIGINAL_MCODE \
56 *((uint16_t *) pr->mpc) = (uint16_t) pr->mcode; \
60 /* patcher_patch_code **********************************************************
62 Just patches back the original machine code.
64 *******************************************************************************/
66 void patcher_patch_code(patchref_t *pr)
68 PATCH_BACK_ORIGINAL_MCODE;
72 /* patcher_resolve_classref_to_classinfo ***************************************
76 <patched call position>
77 48 bf a0 f0 92 00 00 00 00 00 mov $0x92f0a0,%rdi
81 <patched call position>
82 48 be 30 40 b2 00 00 00 00 00 mov $0xb24030,%rsi
83 48 89 e2 mov %rsp,%rdx
84 48 b8 7c 96 4b 00 00 00 00 00 mov $0x4b967c,%rax
89 <patched call position>
90 48 be b8 3f b2 00 00 00 00 00 mov $0xb23fb8,%rsi
91 48 b8 00 00 00 00 00 00 00 00 mov $0x0,%rax
94 *******************************************************************************/
96 bool patcher_resolve_classref_to_classinfo(patchref_t *pr)
98 constant_classref *cr;
102 cr = (constant_classref *) pr->ref;
103 datap = (intptr_t *) pr->datap;
105 /* get the classinfo */
107 if (!(c = resolve_classref_eager(cr)))
110 PATCH_BACK_ORIGINAL_MCODE;
112 /* patch the classinfo pointer */
114 *datap = (intptr_t) c;
120 /* patcher_resolve_classref_to_vftbl *******************************************
125 <patched call position>
127 *******************************************************************************/
129 bool patcher_resolve_classref_to_vftbl(patchref_t *pr)
131 constant_classref *cr;
135 /* get stuff from the stack */
137 cr = (constant_classref *) pr->ref;
138 datap = (intptr_t *) pr->datap;
140 /* get the fieldinfo */
142 if (!(c = resolve_classref_eager(cr)))
145 PATCH_BACK_ORIGINAL_MCODE;
147 /* patch super class' vftbl */
149 *datap = (intptr_t) c->vftbl;
155 /* patcher_resolve_classref_to_flags *******************************************
157 CHECKCAST/INSTANCEOF:
159 <patched call position>
161 *******************************************************************************/
163 bool patcher_resolve_classref_to_flags(patchref_t *pr)
165 constant_classref *cr;
170 cr = (constant_classref *) pr->ref;
171 datap = (int32_t *) pr->datap;
172 ra = (uint8_t *) pr->mpc;
174 /* get the fieldinfo */
176 if (!(c = resolve_classref_eager(cr)))
179 PATCH_BACK_ORIGINAL_MCODE;
181 /* if we show disassembly, we have to skip the nop's */
184 ra = ra + PATCHER_CALL_SIZE;
186 /* patch class flags */
188 /* *datap = c->flags; */
189 *((int32_t *) (ra + 2)) = c->flags;
195 /* patcher_get_putstatic *******************************************************
199 <patched call position>
200 4d 8b 15 86 fe ff ff mov -378(%rip),%r10
201 49 8b 32 mov (%r10),%rsi
203 *******************************************************************************/
205 bool patcher_get_putstatic(patchref_t *pr)
207 unresolved_field *uf;
211 uf = (unresolved_field *) pr->ref;
212 datap = (intptr_t *) pr->datap;
214 /* get the fieldinfo */
216 if (!(fi = resolve_field_eager(uf)))
219 /* check if the field's class is initialized */
221 if (!(fi->class->state & CLASS_INITIALIZED))
222 if (!initialize_class(fi->class))
225 PATCH_BACK_ORIGINAL_MCODE;
227 /* patch the field value's address */
229 *datap = (intptr_t) fi->value;
235 /* patcher_get_putfield ********************************************************
239 <patched call position>
240 45 8b 8f 00 00 00 00 mov 0x0(%r15),%r9d
242 *******************************************************************************/
244 bool patcher_get_putfield(patchref_t *pr)
247 unresolved_field *uf;
251 ra = (uint8_t *) pr->mpc;
252 uf = (unresolved_field *) pr->ref;
254 /* get the fieldinfo */
256 if (!(fi = resolve_field_eager(uf)))
259 PATCH_BACK_ORIGINAL_MCODE;
261 /* if we show disassembly, we have to skip the nop's */
264 ra = ra + PATCHER_CALL_SIZE;
266 /* Patch the field's offset: we check for the field type, because
267 the instructions have different lengths. */
269 if (IS_INT_LNG_TYPE(fi->type)) {
270 /* Check for special case: %rsp or %r12 as base register. */
275 *((int32_t *) (ra + 4)) = fi->offset;
277 *((int32_t *) (ra + 3)) = fi->offset;
280 /* Check for special case: %rsp or %r12 as base register. */
285 *((int32_t *) (ra + 6)) = fi->offset;
287 *((int32_t *) (ra + 5)) = fi->offset;
294 /* patcher_putfieldconst *******************************************************
298 <patched call position>
299 41 c7 85 00 00 00 00 7b 00 00 00 movl $0x7b,0x0(%r13)
301 *******************************************************************************/
303 bool patcher_putfieldconst(patchref_t *pr)
306 unresolved_field *uf;
310 ra = (uint8_t *) pr->mpc;
311 uf = (unresolved_field *) pr->ref;
313 /* get the fieldinfo */
315 if (!(fi = resolve_field_eager(uf)))
318 PATCH_BACK_ORIGINAL_MCODE;
320 /* if we show disassembly, we have to skip the nop's */
323 ra = ra + PATCHER_CALL_SIZE;
325 /* patch the field's offset */
327 if (IS_2_WORD_TYPE(fi->type) || IS_ADR_TYPE(fi->type)) {
328 /* handle special case when the base register is %r12 */
333 *((uint32_t *) (ra + 4)) = fi->offset;
334 *((uint32_t *) (ra + 12 + 4)) = fi->offset + 4;
337 *((uint32_t *) (ra + 3)) = fi->offset;
338 *((uint32_t *) (ra + 11 + 3)) = fi->offset + 4;
342 /* handle special case when the base register is %r12 */
347 *((uint32_t *) (ra + 4)) = fi->offset;
349 *((uint32_t *) (ra + 3)) = fi->offset;
356 /* patcher_invokestatic_special ************************************************
360 <patched call position>
361 49 ba 00 00 00 00 00 00 00 00 mov $0x0,%r10
364 *******************************************************************************/
366 bool patcher_invokestatic_special(patchref_t *pr)
368 unresolved_method *um;
372 /* get stuff from the stack */
374 um = (unresolved_method *) pr->ref;
375 datap = (intptr_t *) pr->datap;
377 /* get the fieldinfo */
379 if (!(m = resolve_method_eager(um)))
382 PATCH_BACK_ORIGINAL_MCODE;
384 /* patch stubroutine */
386 *datap = (intptr_t) m->stubroutine;
392 /* patcher_invokevirtual *******************************************************
396 <patched call position>
397 4c 8b 17 mov (%rdi),%r10
398 49 8b 82 00 00 00 00 mov 0x0(%r10),%rax
401 *******************************************************************************/
403 bool patcher_invokevirtual(patchref_t *pr)
406 unresolved_method *um;
409 ra = (uint8_t *) pr->mpc;
410 um = (unresolved_method *) pr->ref;
412 /* get the methodinfo */
414 if (!(m = resolve_method_eager(um)))
417 PATCH_BACK_ORIGINAL_MCODE;
419 /* if we show disassembly, we have to skip the nop's */
422 ra = ra + PATCHER_CALL_SIZE;
424 /* patch vftbl index */
426 *((int32_t *) (ra + 3 + 3)) =
427 (int32_t) (OFFSET(vftbl_t, table[0]) +
428 sizeof(methodptr) * m->vftblindex);
434 /* patcher_invokeinterface *****************************************************
438 <patched call position>
439 4c 8b 17 mov (%rdi),%r10
440 4d 8b 92 00 00 00 00 mov 0x0(%r10),%r10
441 49 8b 82 00 00 00 00 mov 0x0(%r10),%rax
444 *******************************************************************************/
446 bool patcher_invokeinterface(patchref_t *pr)
449 unresolved_method *um;
452 /* get stuff from the stack */
454 ra = (uint8_t *) pr->mpc;
455 um = (unresolved_method *) pr->ref;
457 /* get the fieldinfo */
459 if (!(m = resolve_method_eager(um)))
462 PATCH_BACK_ORIGINAL_MCODE;
464 /* if we show disassembly, we have to skip the nop's */
467 ra = ra + PATCHER_CALL_SIZE;
469 /* patch interfacetable index */
471 *((int32_t *) (ra + 3 + 3)) =
472 (int32_t) (OFFSET(vftbl_t, interfacetable[0]) -
473 sizeof(methodptr) * m->class->index);
475 /* patch method offset */
477 *((int32_t *) (ra + 3 + 7 + 3)) =
478 (int32_t) (sizeof(methodptr) * (m - m->class->methods));
484 /* patcher_checkcast_interface *************************************************
488 <patched call position>
489 45 8b 9a 1c 00 00 00 mov 0x1c(%r10),%r11d
490 41 81 fb 00 00 00 00 cmp $0x0,%r11d
491 0f 8f 08 00 00 00 jg 0x00002aaaaae511d5
492 48 8b 0c 25 03 00 00 00 mov 0x3,%rcx
493 4d 8b 9a 00 00 00 00 mov 0x0(%r10),%r11
495 *******************************************************************************/
497 bool patcher_checkcast_interface(patchref_t *pr)
500 constant_classref *cr;
503 ra = (uint8_t *) pr->mpc;
504 cr = (constant_classref *) pr->ref;
506 /* get the fieldinfo */
508 if (!(c = resolve_classref_eager(cr)))
511 PATCH_BACK_ORIGINAL_MCODE;
513 /* if we show disassembly, we have to skip the nop's */
516 ra = ra + PATCHER_CALL_SIZE;
518 /* patch super class index */
520 *((int32_t *) (ra + 7 + 3)) = c->index;
522 *((int32_t *) (ra + 7 + 7 + 6 + 8 + 3)) =
523 (int32_t) (OFFSET(vftbl_t, interfacetable[0]) -
524 c->index * sizeof(methodptr*));
530 /* patcher_instanceof_interface ************************************************
534 <patched call position>
535 45 8b 9a 1c 00 00 00 mov 0x1c(%r10),%r11d
536 41 81 fb 00 00 00 00 cmp $0x0,%r11d
537 0f 8e 94 04 00 00 jle 0x00002aaaaab018f8
538 4d 8b 9a 00 00 00 00 mov 0x0(%r10),%r11
540 *******************************************************************************/
542 bool patcher_instanceof_interface(patchref_t *pr)
545 constant_classref *cr;
548 ra = (uint8_t *) pr->mpc;
549 cr = (constant_classref *) pr->ref;
551 /* get the fieldinfo */
553 if (!(c = resolve_classref_eager(cr)))
556 PATCH_BACK_ORIGINAL_MCODE;
558 /* if we show disassembly, we have to skip the nop's */
561 ra = ra + PATCHER_CALL_SIZE;
563 /* patch super class index */
565 *((int32_t *) (ra + 7 + 3)) = c->index;
567 *((int32_t *) (ra + 7 + 7 + 6 + 3)) =
568 (int32_t) (OFFSET(vftbl_t, interfacetable[0]) -
569 c->index * sizeof(methodptr*));
576 * These are local overrides for various environment variables in Emacs.
577 * Please do not remove this and leave it at the end of the file, where
578 * Emacs will automagically detect them.
579 * ---------------------------------------------------------------------
582 * indent-tabs-mode: t
586 * vim:noexpandtab:sw=4:ts=4: