1 /* src/vm/jit/arm/patcher.c - ARM 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
35 #include "vm/jit/arm/md.h"
37 #include "mm/memory.h"
39 #include "native/native.h"
41 #include "vm/builtin.h"
42 #include "vm/exceptions.h"
43 #include "vm/initialize.h"
45 #include "vm/jit/asmpart.h"
46 #include "vm/jit/patcher-common.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 \
55 *((u4 *) pr->mpc) = (u4) pr->mcode; \
56 md_icacheflush((u1 *) pr->mpc, 1 * 4);
58 #define gen_resolveload(inst,offset) \
59 assert((offset) >= -0x0fff && (offset) <= 0x0fff); \
60 assert(!((inst) & 0x0fff)); \
62 (inst) = ((inst) & 0xff7ff000) | ((-(offset)) & 0x0fff); \
63 /*(inst) &= ~(1 << 23);*/ \
65 (inst) = ((inst) & 0xfffff000) | ((offset) & 0x0fff); \
66 /*(inst) |= (1 << 23);*/ \
70 /* patcher_patch_code **********************************************************
72 Just patches back the original machine code.
74 *******************************************************************************/
76 void patcher_patch_code(patchref_t *pr)
78 PATCH_BACK_ORIGINAL_MCODE;
82 /* patcher_get_putstatic *******************************************************
86 <patched call position>
87 e51c103c ldr r1, [ip, #-60]
89 *******************************************************************************/
91 bool patcher_get_putstatic(patchref_t *pr)
97 /* get stuff from the stack */
99 uf = (unresolved_field *) pr->ref;
100 datap = (u1 *) pr->datap;
102 /* get the fieldinfo */
104 if (!(fi = resolve_field_eager(uf)))
107 /* check if the field's class is initialized */
109 if (!(fi->class->state & CLASS_INITIALIZED))
110 if (!initialize_class(fi->class))
113 PATCH_BACK_ORIGINAL_MCODE;
115 /* patch the field value's address */
117 *((intptr_t *) datap) = (intptr_t) fi->value;
123 /* patcher_get_putfield ********************************************************
127 <patched call position>
128 e58a8000 str r8, [sl, #__]
130 *******************************************************************************/
132 bool patcher_get_putfield(patchref_t *pr)
136 unresolved_field *uf;
139 /* get stuff from the stack */
142 uf = (unresolved_field*) pr->ref;
144 /* get the fieldinfo */
146 if (!(fi = resolve_field_eager(uf)))
149 PATCH_BACK_ORIGINAL_MCODE;
151 /* if we show disassembly, we have to skip the nop */
156 /* patch the field's offset into the instruction */
161 #if defined(ENABLE_SOFTFLOAT)
164 assert(fi->offset <= 0x0fff);
165 *((u4 *) (ra + 0 * 4)) |= (fi->offset & 0x0fff);
169 #if defined(ENABLE_SOFTFLOAT)
172 assert((fi->offset + 4) <= 0x0fff);
173 *((u4 *) (ra + 0 * 4)) |= ((fi->offset + 0) & 0x0fff);
174 *((u4 *) (ra + 1 * 4)) &= 0xfffff000;
175 *((u4 *) (ra + 1 * 4)) |= ((fi->offset + 4) & 0x0fff);
178 #if !defined(ENABLE_SOFTFLOAT)
181 assert(fi->offset <= 0x03ff);
182 *((u4 *) (ra + 0 * 4)) |= ((fi->offset >> 2) & 0x00ff);
187 /* synchronize instruction cache */
189 md_icacheflush(ra, 2 * 4);
195 /* patcher_resolve_classref_to_classinfo ***************************************
197 ACONST - Machine code:
199 <patched call postition>
200 e51cc030 ldr r0, [ip, #-48]
202 MULTIANEWARRAY - Machine code:
204 <patched call position>
205 e3a00002 mov r0, #2 ; 0x2
206 e51c1064 ldr r1, [ip, #-100]
209 e51cf068 ldr pc, [ip, #-104]
211 ARRAYCHECKCAST - Machine code:
213 <patched call position>
214 e51c1120 ldr r1, [ip, #-288]
216 e51cf124 ldr pc, [ip, #-292]
218 *******************************************************************************/
220 bool patcher_resolve_classref_to_classinfo(patchref_t *pr)
222 constant_classref *cr;
226 /* get stuff from the stack */
228 cr = (constant_classref *) pr->ref;
229 datap = (u1 *) pr->datap;
231 /* get the classinfo */
233 if (!(c = resolve_classref_eager(cr)))
236 PATCH_BACK_ORIGINAL_MCODE;
238 /* patch the classinfo pointer */
240 *((ptrint *) datap) = (ptrint) c;
246 /* patcher_invokestatic_special ************************************************
250 <patched call position>
251 e51cc02c ldr ip, [ip, #-44]
255 ******************************************************************************/
257 bool patcher_invokestatic_special(patchref_t *pr)
259 unresolved_method *um;
263 /* get stuff from the stack */
265 um = (unresolved_method*) pr->ref;
266 datap = (u1 *) pr->datap;
268 /* get the methodinfo */
270 if (!(m = resolve_method_eager(um)))
273 PATCH_BACK_ORIGINAL_MCODE;
275 /* patch stubroutine */
277 *((ptrint *) datap) = (ptrint) m->stubroutine;
283 /* patcher_invokevirtual *******************************************************
287 <patched call position>
288 e590b000 ldr fp, [r0]
289 e59bc000 ldr ip, [fp, #__]
293 *******************************************************************************/
295 bool patcher_invokevirtual(patchref_t *pr)
298 unresolved_method *um;
301 /* get stuff from the stack */
304 um = (unresolved_method *) pr->ref;
306 /* get the methodinfo */
308 if (!(m = resolve_method_eager(um)))
311 PATCH_BACK_ORIGINAL_MCODE;
313 /* if we show disassembly, we have to skip the nop */
318 /* patch vftbl index */
320 gen_resolveload(*((s4 *) (ra + 1 * 4)), (s4) (OFFSET(vftbl_t, table[0]) + sizeof(methodptr) * m->vftblindex));
322 /* synchronize instruction cache */
324 md_icacheflush(ra + 1 * 4, 1 * 4);
330 /* patcher_invokeinterface *****************************************************
334 <patched call position>
335 e590b000 ldr fp, [r0]
336 e59bb000 ldr fp, [fp, #__]
337 e59bc000 ldr ip, [fp, #__]
342 *******************************************************************************/
344 bool patcher_invokeinterface(patchref_t *pr)
347 unresolved_method *um;
350 /* get stuff from the stack */
353 um = (unresolved_method *) pr->ref;
355 /* get the methodinfo */
357 if (!(m = resolve_method_eager(um)))
360 PATCH_BACK_ORIGINAL_MCODE;
362 /* if we show disassembly, we have to skip the nop */
367 /* patch interfacetable index */
369 gen_resolveload(*((s4 *) (ra + 1 * 4)), (s4) (OFFSET(vftbl_t, interfacetable[0]) - sizeof(methodptr*) * m->class->index));
371 /* patch method offset */
373 gen_resolveload(*((s4 *) (ra + 2 * 4)), (s4) (sizeof(methodptr) * (m - m->class->methods)));
375 /* synchronize instruction cache */
377 md_icacheflush(ra + 1 * 4, 2 * 4);
383 /* patcher_checkcast_instanceof_flags ******************************************
387 <patched call position>
389 *******************************************************************************/
391 bool patcher_resolve_classref_to_flags(patchref_t *pr)
393 constant_classref *cr;
397 /* get stuff from the stack */
399 cr = (constant_classref *) pr->ref;
400 datap = (u1 *) pr->datap;
402 /* get the classinfo */
404 if (!(c = resolve_classref_eager(cr)))
407 PATCH_BACK_ORIGINAL_MCODE;
409 /* patch class flags */
411 *((s4 *) datap) = (s4) c->flags;
417 /* patcher_resolve_classref_to_index *******************************************
421 <patched call position>
423 *******************************************************************************/
425 bool patcher_resolve_classref_to_index(patchref_t *pr)
427 constant_classref *cr;
431 /* get stuff from the stack */
433 cr = (constant_classref *) pr->ref;
434 datap = (u1 *) pr->datap;
436 /* get the classinfo */
438 if (!(c = resolve_classref_eager(cr)))
441 PATCH_BACK_ORIGINAL_MCODE;
443 /* patch super class index */
445 *((s4 *) datap) = (s4) c->index;
451 /* patcher_resolve_classref_to_vftbl *******************************************
455 <patched call position>
457 *******************************************************************************/
459 bool patcher_resolve_classref_to_vftbl(patchref_t *pr)
461 constant_classref *cr;
465 /* get stuff from the stack */
467 cr = (constant_classref *) pr->ref;
468 datap = (u1 *) pr->datap;
470 /* get the classinfo */
472 if (!(c = resolve_classref_eager(cr)))
475 PATCH_BACK_ORIGINAL_MCODE;
477 /* patch super class' vftbl */
479 *((ptrint *) datap) = (ptrint) c->vftbl;
486 * These are local overrides for various environment variables in Emacs.
487 * Please do not remove this and leave it at the end of the file, where
488 * Emacs will automagically detect them.
489 * ---------------------------------------------------------------------
492 * indent-tabs-mode: t
496 * vim:noexpandtab:sw=4:ts=4: