2 * tramp-arm.c: JIT trampoline code for ARM
5 * Paolo Molaro (lupus@ximian.com)
7 * (C) 2001 Ximian, Inc.
13 #include <mono/metadata/appdomain.h>
14 #include <mono/metadata/marshal.h>
15 #include <mono/metadata/tabledefs.h>
16 #include <mono/arch/arm/arm-codegen.h>
22 * Return the instruction to jump from code to target, 0 if not
23 * reachable with a single instruction
26 branch_for_target_reachable (guint8 *branch, guint8 *target)
28 gint diff = target - branch - 8;
29 g_assert ((diff & 3) == 0);
32 return (ARMCOND_AL << ARMCOND_SHIFT) | (ARM_BR_TAG) | (diff >> 2);
34 /* diff between 0 and -33554432 */
35 if (diff >= -33554432)
36 return (ARMCOND_AL << ARMCOND_SHIFT) | (ARM_BR_TAG) | ((diff >> 2) & ~0xff000000);
42 * mono_arch_get_unbox_trampoline:
44 * @addr: pointer to native code for @m
46 * when value type methods are called through the vtable we need to unbox the
47 * this argument. This method returns a pointer to a trampoline which does
48 * unboxing before calling the method
51 mono_arch_get_unbox_trampoline (MonoMethod *m, gpointer addr)
55 MonoDomain *domain = mono_domain_get ();
57 if (!mono_method_signature (m)->ret->byref && MONO_TYPE_ISSTRUCT (mono_method_signature (m)->ret))
60 mono_domain_lock (domain);
61 start = code = mono_code_manager_reserve (domain->code_mp, 16);
62 mono_domain_unlock (domain);
64 ARM_LDR_IMM (code, ARMREG_IP, ARMREG_PC, 4);
65 ARM_ADD_REG_IMM8 (code, this_pos, this_pos, sizeof (MonoObject));
66 ARM_MOV_REG_REG (code, ARMREG_PC, ARMREG_IP);
67 *(guint32*)code = (guint32)addr;
69 mono_arch_flush_icache (start, code - start);
70 g_assert ((code - start) <= 16);
71 /*g_print ("unbox trampoline at %d for %s:%s\n", this_pos, m->klass->name, m->name);
72 g_print ("unbox code is at %p for method at %p\n", start, addr);*/
78 mono_arch_patch_callsite (guint8 *code_ptr, guint8 *addr)
80 guint32 *code = (guint32*)code_ptr;
82 /* This is the 'bl' or the 'mov pc' instruction */
86 * Note that methods are called also with the bl opcode.
88 if ((((*code) >> 25) & 7) == 5) {
89 /*g_print ("direct patching\n");*/
90 arm_patch ((char*)code, addr);
91 mono_arch_flush_icache ((char*)code, 4);
95 if ((((*code) >> 20) & 0xFF) == 0x12) {
96 /*g_print ("patching bx\n");*/
97 arm_patch ((char*)code, addr);
98 mono_arch_flush_icache ((char*)(code - 2), 4);
102 g_assert_not_reached ();
106 mono_arch_patch_plt_entry (guint8 *code, guint8 *addr)
108 guint32 ins = branch_for_target_reachable (code, addr);
111 /* Patch the branch */
112 ((guint32*)code) [0] = ins;
114 /* Patch the jump address */
115 ((guint32*)code) [1] = addr;
116 mono_arch_flush_icache ((char*)code, 4);
120 mono_arch_nullify_class_init_trampoline (guint8 *code, gssize *regs)
126 mono_arch_nullify_plt_entry (guint8 *code)
132 ARM_MOV_REG_REG (p, ARMREG_PC, ARMREG_LR);
134 ((guint32*)code) [0] = ((guint32*)buf) [0];
135 mono_arch_flush_icache ((char*)code, 4);
139 /* Stack size for trampoline function
141 #define STACK (sizeof (MonoLMF))
143 /* Method-specific trampoline code fragment size */
144 #define METHOD_TRAMPOLINE_SIZE 64
146 /* Jump-specific trampoline code fragment size */
147 #define JUMP_TRAMPOLINE_SIZE 64
149 #define GEN_TRAMP_SIZE 148
152 * Stack frame description when the generic trampoline is called.
154 * ------------------- old sp
156 * ------------------- sp
159 mono_arch_create_trampoline_code (MonoTrampolineType tramp_type)
161 guint8 *buf, *code = NULL;
163 guint8 *load_get_lmf_addr, *load_trampoline;
166 /* Now we'll create in 'buf' the ARM trampoline code. This
167 is the trampoline code common to all methods */
169 code = buf = mono_global_codeman_reserve (GEN_TRAMP_SIZE);
172 * At this point lr points to the specific arg and sp points to the saved
173 * regs on the stack (all but PC and SP). The original LR value has been
174 * saved as sp + LR_OFFSET by the push in the specific trampoline
176 #define LR_OFFSET (sizeof (gpointer) * 13)
177 ARM_MOV_REG_REG (buf, ARMREG_V1, ARMREG_SP);
178 ARM_LDR_IMM (buf, ARMREG_V2, ARMREG_LR, 0);
179 ARM_LDR_IMM (buf, ARMREG_V3, ARMREG_SP, LR_OFFSET);
181 /* ok, now we can continue with the MonoLMF setup, mostly untouched
182 * from emit_prolog in mini-arm.c
183 * This is a sinthetized call to mono_get_lmf_addr ()
185 load_get_lmf_addr = buf;
187 ARM_MOV_REG_REG (buf, ARMREG_LR, ARMREG_PC);
188 ARM_MOV_REG_REG (buf, ARMREG_PC, ARMREG_R0);
190 /* we build the MonoLMF structure on the stack - see mini-arm.h
191 * The pointer to the struct is put in r1.
192 * the iregs array is already allocated on the stack by push.
194 ARM_SUB_REG_IMM8 (buf, ARMREG_SP, ARMREG_SP, sizeof (MonoLMF) - sizeof (guint) * 14);
195 ARM_ADD_REG_IMM8 (buf, ARMREG_R1, ARMREG_SP, STACK - sizeof (MonoLMF));
196 /* r0 is the result from mono_get_lmf_addr () */
197 ARM_STR_IMM (buf, ARMREG_R0, ARMREG_R1, G_STRUCT_OFFSET (MonoLMF, lmf_addr));
198 /* new_lmf->previous_lmf = *lmf_addr */
199 ARM_LDR_IMM (buf, ARMREG_R2, ARMREG_R0, G_STRUCT_OFFSET (MonoLMF, previous_lmf));
200 ARM_STR_IMM (buf, ARMREG_R2, ARMREG_R1, G_STRUCT_OFFSET (MonoLMF, previous_lmf));
201 /* *(lmf_addr) = r1 */
202 ARM_STR_IMM (buf, ARMREG_R1, ARMREG_R0, G_STRUCT_OFFSET (MonoLMF, previous_lmf));
203 /* save method info (it's in v2) */
204 if ((tramp_type == MONO_TRAMPOLINE_GENERIC) || (tramp_type == MONO_TRAMPOLINE_JUMP))
205 ARM_STR_IMM (buf, ARMREG_V2, ARMREG_R1, G_STRUCT_OFFSET (MonoLMF, method));
206 ARM_STR_IMM (buf, ARMREG_SP, ARMREG_R1, G_STRUCT_OFFSET (MonoLMF, ebp));
207 /* save the IP (caller ip) */
208 if (tramp_type == MONO_TRAMPOLINE_JUMP) {
209 ARM_MOV_REG_IMM8 (buf, ARMREG_R2, 0);
211 /* assumes STACK == sizeof (MonoLMF) */
212 ARM_LDR_IMM (buf, ARMREG_R2, ARMREG_SP, (G_STRUCT_OFFSET (MonoLMF, iregs) + 13*4));
214 ARM_STR_IMM (buf, ARMREG_R2, ARMREG_R1, G_STRUCT_OFFSET (MonoLMF, eip));
217 * Now we're ready to call xxx_trampoline ().
219 /* Arg 1: the saved registers. It was put in v1 */
220 ARM_MOV_REG_REG (buf, ARMREG_R0, ARMREG_V1);
222 /* Arg 2: code (next address to the instruction that called us) */
223 if (tramp_type == MONO_TRAMPOLINE_JUMP) {
224 ARM_MOV_REG_IMM8 (buf, ARMREG_R1, 0);
226 ARM_MOV_REG_REG (buf, ARMREG_R1, ARMREG_V3);
229 /* Arg 3: the specific argument, stored in v2
231 ARM_MOV_REG_REG (buf, ARMREG_R2, ARMREG_V2);
233 load_trampoline = buf;
236 ARM_MOV_REG_REG (buf, ARMREG_LR, ARMREG_PC);
237 ARM_MOV_REG_REG (buf, ARMREG_PC, ARMREG_IP);
239 /* OK, code address is now on r0. Move it to the place on the stack
240 * where IP was saved (it is now no more useful to us and it can be
241 * clobbered). This way we can just restore all the regs in one inst
244 ARM_STR_IMM (buf, ARMREG_R0, ARMREG_V1, (ARMREG_R12 * 4));
247 * Now we restore the MonoLMF (see emit_epilogue in mini-arm.c)
248 * and the rest of the registers, so the method called will see
249 * the same state as before we executed.
250 * The pointer to MonoLMF is in r2.
252 ARM_MOV_REG_REG (buf, ARMREG_R2, ARMREG_SP);
253 /* ip = previous_lmf */
254 ARM_LDR_IMM (buf, ARMREG_IP, ARMREG_R2, G_STRUCT_OFFSET (MonoLMF, previous_lmf));
256 ARM_LDR_IMM (buf, ARMREG_LR, ARMREG_R2, G_STRUCT_OFFSET (MonoLMF, lmf_addr));
257 /* *(lmf_addr) = previous_lmf */
258 ARM_STR_IMM (buf, ARMREG_IP, ARMREG_LR, G_STRUCT_OFFSET (MonoLMF, previous_lmf));
260 /* Non-standard function epilogue. Instead of doing a proper
261 * return, we just jump to the compiled code.
263 /* Restore the registers and jump to the code:
264 * Note that IP has been conveniently set to the method addr.
266 ARM_ADD_REG_IMM8 (buf, ARMREG_SP, ARMREG_SP, sizeof (MonoLMF) - sizeof (guint) * 14);
267 ARM_POP_NWB (buf, 0x5fff);
268 /* do we need to set sp? */
269 ARM_ADD_REG_IMM8 (buf, ARMREG_SP, ARMREG_SP, (14 * 4));
270 if (tramp_type == MONO_TRAMPOLINE_CLASS_INIT)
271 ARM_MOV_REG_REG (buf, ARMREG_PC, ARMREG_LR);
273 ARM_MOV_REG_REG (buf, ARMREG_PC, ARMREG_IP);
275 constants = (gpointer*)buf;
276 constants [0] = mono_get_lmf_addr;
277 constants [1] = mono_get_trampoline_func (tramp_type);
279 /* backpatch by emitting the missing instructions skipped above */
280 ARM_LDR_IMM (load_get_lmf_addr, ARMREG_R0, ARMREG_PC, (buf - load_get_lmf_addr - 8));
281 ARM_LDR_IMM (load_trampoline, ARMREG_IP, ARMREG_PC, (buf + 4 - load_trampoline - 8));
285 /* Flush instruction cache, since we've generated code */
286 mono_arch_flush_icache (code, buf - code);
289 g_assert ((buf - code) <= GEN_TRAMP_SIZE);
294 #define SPEC_TRAMP_SIZE 24
297 mono_arch_create_specific_trampoline (gpointer arg1, MonoTrampolineType tramp_type, MonoDomain *domain, guint32 *code_len)
299 guint8 *code, *buf, *tramp;
301 guint32 short_branch, size = SPEC_TRAMP_SIZE;
303 tramp = mono_get_trampoline_code (tramp_type);
305 mono_domain_lock (domain);
306 code = buf = mono_code_manager_reserve_align (domain->code_mp, size, 4);
307 if ((short_branch = branch_for_target_reachable (code + 8, tramp))) {
309 mono_code_manager_commit (domain->code_mp, code, SPEC_TRAMP_SIZE, size);
311 mono_domain_unlock (domain);
313 /* we could reduce this to 12 bytes if tramp is within reach:
317 * The called code can access method using the lr register
318 * A 20 byte sequence could be:
320 * ARM_MOV_REG_REG (lr, pc)
321 * ARM_LDR_IMM (pc, pc, 0)
325 /* We save all the registers, except PC and SP */
326 ARM_PUSH (buf, 0x5fff);
328 constants = (gpointer*)buf;
329 constants [0] = GUINT_TO_POINTER (short_branch | (1 << 24));
330 constants [1] = arg1;
333 ARM_LDR_IMM (buf, ARMREG_R1, ARMREG_PC, 8); /* temp reg */
334 ARM_MOV_REG_REG (buf, ARMREG_LR, ARMREG_PC);
335 ARM_MOV_REG_REG (buf, ARMREG_PC, ARMREG_R1);
337 constants = (gpointer*)buf;
338 constants [0] = arg1;
339 constants [1] = tramp;
343 /* Flush instruction cache, since we've generated code */
344 mono_arch_flush_icache (code, buf - code);
346 g_assert ((buf - code) <= size);
349 *code_len = buf - code;
355 * This method is only called when running in the Mono Debugger.
358 mono_debugger_create_notification_function (void)
362 ptr = buf = mono_global_codeman_reserve (8);
363 //FIXME: ARM_SWI (buf, 0x9F0001);
364 ARM_MOV_REG_REG (buf, ARMREG_PC, ARMREG_LR);
365 mono_arch_flush_icache (ptr, buf - ptr);
371 mono_arch_create_rgctx_lazy_fetch_trampoline (guint32 encoded_offset)
373 /* FIXME: implement! */
374 g_assert_not_reached ();
379 mono_arch_get_rgctx_lazy_fetch_offset (gpointer *regs)
381 /* FIXME: implement! */
382 g_assert_not_reached ();