1 /* src/vm/jit/arm/patcher.c - ARM 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
33 #include "vm/jit/arm/md.h"
35 #include "mm/memory.h"
37 #include "native/native.h"
39 #include "vm/builtin.h"
40 #include "vm/exceptions.h"
41 #include "vm/initialize.h"
43 #include "vm/jit/asmpart.h"
44 #include "vm/jit/patcher-common.h"
46 #include "vmcore/field.h"
47 #include "vmcore/options.h"
48 #include "vmcore/references.h"
49 #include "vm/resolve.h"
52 #define PATCH_BACK_ORIGINAL_MCODE \
53 *((u4 *) pr->mpc) = (u4) pr->mcode; \
54 md_icacheflush((u1 *) pr->mpc, 1 * 4);
56 #define gen_resolveload(inst,offset) \
57 assert((offset) >= -0x0fff && (offset) <= 0x0fff); \
58 assert(!((inst) & 0x0fff)); \
60 (inst) = ((inst) & 0xff7ff000) | ((-(offset)) & 0x0fff); \
61 /*(inst) &= ~(1 << 23);*/ \
63 (inst) = ((inst) & 0xfffff000) | ((offset) & 0x0fff); \
64 /*(inst) |= (1 << 23);*/ \
68 /* patcher_patch_code **********************************************************
70 Just patches back the original machine code.
72 *******************************************************************************/
74 void patcher_patch_code(patchref_t *pr)
76 PATCH_BACK_ORIGINAL_MCODE;
80 /* patcher_get_putstatic *******************************************************
84 <patched call position>
85 e51c103c ldr r1, [ip, #-60]
87 *******************************************************************************/
89 bool patcher_get_putstatic(patchref_t *pr)
95 /* get stuff from the stack */
97 uf = (unresolved_field *) pr->ref;
98 datap = (u1 *) pr->datap;
100 /* get the fieldinfo */
102 if (!(fi = resolve_field_eager(uf)))
105 /* check if the field's class is initialized */
107 if (!(fi->class->state & CLASS_INITIALIZED))
108 if (!initialize_class(fi->class))
111 PATCH_BACK_ORIGINAL_MCODE;
113 /* patch the field value's address */
115 *((intptr_t *) datap) = (intptr_t) fi->value;
121 /* patcher_get_putfield ********************************************************
125 <patched call position>
126 e58a8000 str r8, [sl, #__]
128 *******************************************************************************/
130 bool patcher_get_putfield(patchref_t *pr)
134 unresolved_field *uf;
137 /* get stuff from the stack */
140 uf = (unresolved_field*) pr->ref;
142 /* get the fieldinfo */
144 if (!(fi = resolve_field_eager(uf)))
147 PATCH_BACK_ORIGINAL_MCODE;
149 /* if we show disassembly, we have to skip the nop */
154 /* patch the field's offset into the instruction */
159 #if defined(ENABLE_SOFTFLOAT)
162 assert(fi->offset <= 0x0fff);
163 *((u4 *) (ra + 0 * 4)) |= (fi->offset & 0x0fff);
167 #if defined(ENABLE_SOFTFLOAT)
170 assert((fi->offset + 4) <= 0x0fff);
171 *((u4 *) (ra + 0 * 4)) |= ((fi->offset + 0) & 0x0fff);
172 *((u4 *) (ra + 1 * 4)) &= 0xfffff000;
173 *((u4 *) (ra + 1 * 4)) |= ((fi->offset + 4) & 0x0fff);
176 #if !defined(ENABLE_SOFTFLOAT)
179 assert(fi->offset <= 0x03ff);
180 *((u4 *) (ra + 0 * 4)) |= ((fi->offset >> 2) & 0x00ff);
185 /* synchronize instruction cache */
187 md_icacheflush(ra, 2 * 4);
193 /* patcher_resolve_classref_to_classinfo ***************************************
195 ACONST - Machine code:
197 <patched call postition>
198 e51cc030 ldr r0, [ip, #-48]
200 MULTIANEWARRAY - Machine code:
202 <patched call position>
203 e3a00002 mov r0, #2 ; 0x2
204 e51c1064 ldr r1, [ip, #-100]
207 e51cf068 ldr pc, [ip, #-104]
209 ARRAYCHECKCAST - Machine code:
211 <patched call position>
212 e51c1120 ldr r1, [ip, #-288]
214 e51cf124 ldr pc, [ip, #-292]
216 *******************************************************************************/
218 bool patcher_resolve_classref_to_classinfo(patchref_t *pr)
220 constant_classref *cr;
224 /* get stuff from the stack */
226 cr = (constant_classref *) pr->ref;
227 datap = (u1 *) pr->datap;
229 /* get the classinfo */
231 if (!(c = resolve_classref_eager(cr)))
234 PATCH_BACK_ORIGINAL_MCODE;
236 /* patch the classinfo pointer */
238 *((ptrint *) datap) = (ptrint) c;
244 /* patcher_invokestatic_special ************************************************
248 <patched call position>
249 e51cc02c ldr ip, [ip, #-44]
253 ******************************************************************************/
255 bool patcher_invokestatic_special(patchref_t *pr)
257 unresolved_method *um;
261 /* get stuff from the stack */
263 um = (unresolved_method*) pr->ref;
264 datap = (u1 *) pr->datap;
266 /* get the methodinfo */
268 if (!(m = resolve_method_eager(um)))
271 PATCH_BACK_ORIGINAL_MCODE;
273 /* patch stubroutine */
275 *((ptrint *) datap) = (ptrint) m->stubroutine;
281 /* patcher_invokevirtual *******************************************************
285 <patched call position>
286 e590b000 ldr fp, [r0]
287 e59bc000 ldr ip, [fp, #__]
291 *******************************************************************************/
293 bool patcher_invokevirtual(patchref_t *pr)
296 unresolved_method *um;
299 /* get stuff from the stack */
302 um = (unresolved_method *) pr->ref;
304 /* get the methodinfo */
306 if (!(m = resolve_method_eager(um)))
309 PATCH_BACK_ORIGINAL_MCODE;
311 /* if we show disassembly, we have to skip the nop */
316 /* patch vftbl index */
318 gen_resolveload(*((s4 *) (ra + 1 * 4)), (s4) (OFFSET(vftbl_t, table[0]) + sizeof(methodptr) * m->vftblindex));
320 /* synchronize instruction cache */
322 md_icacheflush(ra + 1 * 4, 1 * 4);
328 /* patcher_invokeinterface *****************************************************
332 <patched call position>
333 e590b000 ldr fp, [r0]
334 e59bb000 ldr fp, [fp, #__]
335 e59bc000 ldr ip, [fp, #__]
340 *******************************************************************************/
342 bool patcher_invokeinterface(patchref_t *pr)
345 unresolved_method *um;
348 /* get stuff from the stack */
351 um = (unresolved_method *) pr->ref;
353 /* get the methodinfo */
355 if (!(m = resolve_method_eager(um)))
358 PATCH_BACK_ORIGINAL_MCODE;
360 /* if we show disassembly, we have to skip the nop */
365 /* patch interfacetable index */
367 gen_resolveload(*((s4 *) (ra + 1 * 4)), (s4) (OFFSET(vftbl_t, interfacetable[0]) - sizeof(methodptr*) * m->clazz->index));
369 /* patch method offset */
371 gen_resolveload(*((s4 *) (ra + 2 * 4)), (s4) (sizeof(methodptr) * (m - m->clazz->methods)));
373 /* synchronize instruction cache */
375 md_icacheflush(ra + 1 * 4, 2 * 4);
381 /* patcher_checkcast_instanceof_flags ******************************************
385 <patched call position>
387 *******************************************************************************/
389 bool patcher_resolve_classref_to_flags(patchref_t *pr)
391 constant_classref *cr;
395 /* get stuff from the stack */
397 cr = (constant_classref *) pr->ref;
398 datap = (u1 *) pr->datap;
400 /* get the classinfo */
402 if (!(c = resolve_classref_eager(cr)))
405 PATCH_BACK_ORIGINAL_MCODE;
407 /* patch class flags */
409 *((s4 *) datap) = (s4) c->flags;
415 /* patcher_resolve_classref_to_index *******************************************
419 <patched call position>
421 *******************************************************************************/
423 bool patcher_resolve_classref_to_index(patchref_t *pr)
425 constant_classref *cr;
429 /* get stuff from the stack */
431 cr = (constant_classref *) pr->ref;
432 datap = (u1 *) pr->datap;
434 /* get the classinfo */
436 if (!(c = resolve_classref_eager(cr)))
439 PATCH_BACK_ORIGINAL_MCODE;
441 /* patch super class index */
443 *((s4 *) datap) = (s4) c->index;
449 /* patcher_resolve_classref_to_vftbl *******************************************
453 <patched call position>
455 *******************************************************************************/
457 bool patcher_resolve_classref_to_vftbl(patchref_t *pr)
459 constant_classref *cr;
463 /* get stuff from the stack */
465 cr = (constant_classref *) pr->ref;
466 datap = (u1 *) pr->datap;
468 /* get the classinfo */
470 if (!(c = resolve_classref_eager(cr)))
473 PATCH_BACK_ORIGINAL_MCODE;
475 /* patch super class' vftbl */
477 *((ptrint *) datap) = (ptrint) c->vftbl;
484 * These are local overrides for various environment variables in Emacs.
485 * Please do not remove this and leave it at the end of the file, where
486 * Emacs will automagically detect them.
487 * ---------------------------------------------------------------------
490 * indent-tabs-mode: t
494 * vim:noexpandtab:sw=4:ts=4: