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
5 Copyright (C) 2008 Theobroma Systems Ltd.
7 This file is part of CACAO.
9 This program is free software; you can redistribute it and/or
10 modify it under the terms of the GNU General Public License as
11 published by the Free Software Foundation; either version 2, or (at
12 your option) any later version.
14 This program is distributed in the hope that it will be useful, but
15 WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
34 #include "vm/jit/arm/md.h"
36 #include "mm/memory.h"
38 #include "native/native.hpp"
40 #include "vm/jit/builtin.hpp"
41 #include "vm/class.hpp"
42 #include "vm/field.hpp"
43 #include "vm/initialize.hpp"
44 #include "vm/options.h"
45 #include "vm/references.h"
46 #include "vm/resolve.hpp"
48 #include "vm/jit/asmpart.h"
49 #include "vm/jit/patcher-common.hpp"
52 #define gen_resolveload(inst,offset) \
53 assert((offset) >= -0x0fff && (offset) <= 0x0fff); \
54 assert(!((inst) & 0x0fff)); \
56 (inst) = ((inst) & 0xff7ff000) | ((-(offset)) & 0x0fff); \
57 /*(inst) &= ~(1 << 23);*/ \
59 (inst) = ((inst) & 0xfffff000) | ((offset) & 0x0fff); \
60 /*(inst) |= (1 << 23);*/ \
64 /* patcher_patch_code **********************************************************
66 Just patches back the original machine code.
68 *******************************************************************************/
70 void patcher_patch_code(patchref_t *pr)
72 *((uint32_t*) pr->mpc) = (uint32_t) pr->mcode;
73 md_icacheflush((void*) pr->mpc, 1 * 4);
78 * Check if the trap instruction at the given PC is valid.
80 * @param pc Program counter.
82 * @return true if valid, false otherwise.
84 bool patcher_is_valid_trap_instruction_at(void* pc)
86 uint32_t mcode = *((uint32_t*) pc);
88 // Check for the undefined instruction we use.
89 if ((mcode & 0x0ff000f0) != 0x07f000f0) {
97 /* patcher_get_putstatic *******************************************************
101 <patched call position>
102 e51c103c ldr r1, [ip, #-60]
104 *******************************************************************************/
106 bool patcher_get_putstatic(patchref_t *pr)
108 unresolved_field* uf = (unresolved_field *) pr->ref;
109 uintptr_t* datap = (uintptr_t*) pr->datap;
111 // Resolve the field.
112 fieldinfo* fi = resolve_field_eager(uf);
117 // Check if the field's class is initialized.
118 if (!(fi->clazz->state & CLASS_INITIALIZED))
119 if (!initialize_class(fi->clazz))
122 // Patch the field value's address.
123 *datap = (uintptr_t) fi->value;
125 // Patch back the original code.
126 patcher_patch_code(pr);
132 /* patcher_get_putfield ********************************************************
136 <patched call position>
137 e58a8000 str r8, [sl, #__]
139 *******************************************************************************/
141 bool patcher_get_putfield(patchref_t *pr)
143 uint32_t* pc = (uint32_t*) pr->mpc;
144 unresolved_field* uf = (unresolved_field*) pr->ref;
146 // Resolve the field.
147 fieldinfo* fi = resolve_field_eager(uf);
152 // Patch the field's offset into the instruction.
157 #if defined(ENABLE_SOFTFLOAT)
160 assert(fi->offset <= 0x0fff);
161 pr->mcode |= (fi->offset & 0x0fff);
165 #if defined(ENABLE_SOFTFLOAT)
168 assert((fi->offset + 4) <= 0x0fff);
169 pr->mcode |= ((fi->offset + 0) & 0x0fff);
171 pc[1] |= ((fi->offset + 4) & 0x0fff);
174 #if !defined(ENABLE_SOFTFLOAT)
177 assert(fi->offset <= 0x03ff);
178 pr->mcode |= ((fi->offset >> 2) & 0x00ff);
183 // Synchronize instruction cache.
184 md_icacheflush(pc, 2 * 4);
186 // Patch back the original code.
187 patcher_patch_code(pr);
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 = (constant_classref *) pr->ref;
221 uintptr_t* datap = (uintptr_t*) pr->datap;
223 // Resolve the class.
224 classinfo* c = resolve_classref_eager(cr);
229 // Patch the classinfo pointer.
230 *datap = (uintptr_t) c;
232 // Patch back the original code.
233 patcher_patch_code(pr);
239 /* patcher_invokestatic_special ************************************************
243 <patched call position>
244 e51cc02c ldr ip, [ip, #-44]
248 ******************************************************************************/
250 bool patcher_invokestatic_special(patchref_t *pr)
252 unresolved_method* um = (unresolved_method*) pr->ref;
253 uintptr_t* datap = (uintptr_t*) pr->datap;
255 // Reolve the method.
256 methodinfo* m = resolve_method_eager(um);
261 // Patch stubroutine.
262 *datap = (uintptr_t) m->stubroutine;
264 // Patch back the original code.
265 patcher_patch_code(pr);
271 /* patcher_invokevirtual *******************************************************
275 <patched call position>
276 e590b000 ldr fp, [r0]
277 e59bc000 ldr ip, [fp, #__]
281 *******************************************************************************/
283 bool patcher_invokevirtual(patchref_t *pr)
285 uint32_t* pc = (uint32_t*) pr->mpc;
286 unresolved_method* um = (unresolved_method *) pr->ref;
288 // Resolve the method.
289 methodinfo* m = resolve_method_eager(um);
294 // Patch vftbl index.
295 gen_resolveload(pc[1], (int32_t) (OFFSET(vftbl_t, table[0]) + sizeof(methodptr) * m->vftblindex));
297 // Synchronize instruction cache.
298 md_icacheflush(pc + 1, 1 * 4);
300 // Patch back the original code.
301 patcher_patch_code(pr);
307 /* patcher_invokeinterface *****************************************************
311 <patched call position>
312 e590b000 ldr fp, [r0]
313 e59bb000 ldr fp, [fp, #__]
314 e59bc000 ldr ip, [fp, #__]
319 *******************************************************************************/
321 bool patcher_invokeinterface(patchref_t *pr)
323 uint32_t* pc = (uint32_t*) pr->mpc;
324 unresolved_method* um = (unresolved_method *) pr->ref;
326 // Resolve the method.
327 methodinfo* m = resolve_method_eager(um);
332 // Patch interfacetable index.
333 gen_resolveload(pc[1], (int32_t) (OFFSET(vftbl_t, interfacetable[0]) - sizeof(methodptr*) * m->clazz->index));
335 // Patch method offset.
336 gen_resolveload(pc[2], (int32_t) (sizeof(methodptr) * (m - m->clazz->methods)));
338 // Synchronize instruction cache.
339 md_icacheflush(pc + 1, 2 * 4);
341 // Patch back the original code.
342 patcher_patch_code(pr);
348 /* patcher_checkcast_instanceof_flags ******************************************
352 <patched call position>
354 *******************************************************************************/
356 bool patcher_resolve_classref_to_flags(patchref_t *pr)
358 constant_classref* cr = (constant_classref*) pr->ref;
359 int32_t* datap = (int32_t*) pr->datap;
361 // Resolve the class.
362 classinfo* c = resolve_classref_eager(cr);
367 // Patch class flags.
368 *datap = (int32_t) c->flags;
370 // Patch back the original code.
371 patcher_patch_code(pr);
377 /* patcher_resolve_classref_to_index *******************************************
381 <patched call position>
383 *******************************************************************************/
385 bool patcher_resolve_classref_to_index(patchref_t *pr)
387 constant_classref* cr = (constant_classref*) pr->ref;
388 int32_t* datap = (int32_t*) pr->datap;
390 // Resolve the class.
391 classinfo* c = resolve_classref_eager(cr);
396 // Patch super class index.
397 *datap = (int32_t) c->index;
399 // Patch back the original code.
400 patcher_patch_code(pr);
406 /* patcher_resolve_classref_to_vftbl *******************************************
410 <patched call position>
412 *******************************************************************************/
414 bool patcher_resolve_classref_to_vftbl(patchref_t *pr)
416 constant_classref* cr = (constant_classref*) pr->ref;
417 uintptr_t* datap = (uintptr_t*) pr->datap;
419 // Resolve the class.
421 classinfo* c = resolve_classref_eager(cr);
426 // Patch super class' vftbl.
427 *datap = (uintptr_t) c->vftbl;
429 // Patch back the original code.
430 patcher_patch_code(pr);
437 * These are local overrides for various environment variables in Emacs.
438 * Please do not remove this and leave it at the end of the file, where
439 * Emacs will automagically detect them.
440 * ---------------------------------------------------------------------
443 * indent-tabs-mode: t
447 * vim:noexpandtab:sw=4:ts=4: