2 * tramp-arm.c: JIT trampoline code for ARM
5 * Paolo Molaro (lupus@ximian.com)
7 * (C) 2001-2003 Ximian, Inc.
8 * Copyright 2003-2011 Novell Inc
9 * Copyright 2011 Xamarin Inc
15 #include <mono/metadata/appdomain.h>
16 #include <mono/metadata/marshal.h>
17 #include <mono/metadata/tabledefs.h>
18 #include <mono/arch/arm/arm-codegen.h>
23 #define ALIGN_TO(val,align) ((((guint64)val) + ((align) - 1)) & ~((align) - 1))
25 #ifdef USE_JUMP_TABLES
28 decode_imm16 (guint32 insn)
30 return (((insn >> 16) & 0xf) << 12) | (insn & 0xfff);
33 #define INSN_MASK 0xff00000
34 #define MOVW_MASK ((3 << 24) | (0 << 20))
35 #define MOVT_MASK ((3 << 24) | (4 << 20))
38 mono_arch_jumptable_entry_from_code (guint8 *code)
40 guint32 insn1 = ((guint32*)code) [0];
41 guint32 insn2 = ((guint32*)code) [1];
43 if (((insn1 & INSN_MASK) == MOVW_MASK) &&
44 ((insn2 & INSN_MASK) == MOVT_MASK) ) {
45 guint32 imm_lo = decode_imm16 (insn1);
46 guint32 imm_hi = decode_imm16 (insn2);
47 return (gpointer*) GUINT_TO_POINTER (imm_lo | (imm_hi << 16));
49 g_assert_not_reached ();
59 mono_arch_patch_callsite (guint8 *method_start, guint8 *code_ptr, guint8 *addr)
63 * code_ptr is 4 instructions after MOVW/MOVT used to address
66 jte = mono_jumptable_get_entry (code_ptr - 16);
67 g_assert ( jte != NULL);
72 mono_arch_patch_callsite (guint8 *method_start, guint8 *code_ptr, guint8 *addr)
74 guint32 *code = (guint32*)code_ptr;
76 /* This is the 'bl' or the 'mov pc' instruction */
80 * Note that methods are called also with the bl opcode.
82 if ((((*code) >> 25) & 7) == 5) {
83 /*g_print ("direct patching\n");*/
84 arm_patch ((guint8*)code, addr);
85 mono_arch_flush_icache ((guint8*)code, 4);
89 if ((((*code) >> 20) & 0xFF) == 0x12) {
90 /*g_print ("patching bx\n");*/
91 arm_patch ((guint8*)code, addr);
92 mono_arch_flush_icache ((guint8*)(code - 2), 4);
96 g_assert_not_reached ();
101 mono_arch_patch_plt_entry (guint8 *code, gpointer *got, mgreg_t *regs, guint8 *addr)
105 /* Patch the jump table entry used by the plt entry */
106 if (*(guint32*)code == 0xe59fc000) {
107 /* ARM_LDR_IMM (code, ARMREG_IP, ARMREG_PC, 0); */
108 guint32 offset = ((guint32*)code)[2];
110 jump_entry = code + offset + 12;
111 } else if (*(guint16*)(code - 4) == 0xf8df) {
113 * Thumb PLT entry, begins with ldr.w ip, [pc, #8], code points to entry + 4, see
114 * mono_arm_get_thumb_plt_entry ().
119 offset = *(guint32*)(code + 12);
120 jump_entry = code + offset + 8;
122 g_assert_not_reached ();
125 *(guint8**)jump_entry = addr;
129 mono_arch_nullify_class_init_trampoline (guint8 *code, mgreg_t *regs)
131 mono_arch_patch_callsite (NULL, code, mini_get_nullified_class_init_trampoline ());
135 mono_arch_nullify_plt_entry (guint8 *code, mgreg_t *regs)
137 mono_arch_patch_plt_entry (code, NULL, regs, mini_get_nullified_class_init_trampoline ());
142 #define arm_is_imm12(v) ((int)(v) > -4096 && (int)(v) < 4096)
144 #ifndef USE_JUMP_TABLES
146 * Return the instruction to jump from code to target, 0 if not
147 * reachable with a single instruction
150 branch_for_target_reachable (guint8 *branch, guint8 *target)
152 gint diff = target - branch - 8;
153 g_assert ((diff & 3) == 0);
155 if (diff <= 33554431)
156 return (ARMCOND_AL << ARMCOND_SHIFT) | (ARM_BR_TAG) | (diff >> 2);
158 /* diff between 0 and -33554432 */
159 if (diff >= -33554432)
160 return (ARMCOND_AL << ARMCOND_SHIFT) | (ARM_BR_TAG) | ((diff >> 2) & ~0xff000000);
166 static inline guint8*
167 emit_bx (guint8* code, int reg)
169 if (mono_arm_thumb_supported ())
172 ARM_MOV_REG_REG (code, ARMREG_PC, reg);
176 /* Stack size for trampoline function
178 #define STACK ALIGN_TO (sizeof (MonoLMF), 8)
180 /* Method-specific trampoline code fragment size */
181 #define METHOD_TRAMPOLINE_SIZE 64
183 /* Jump-specific trampoline code fragment size */
184 #define JUMP_TRAMPOLINE_SIZE 64
187 mono_arch_create_generic_trampoline (MonoTrampolineType tramp_type, MonoTrampInfo **info, gboolean aot)
190 guint8 *buf, *code = NULL;
191 #ifdef USE_JUMP_TABLES
192 gpointer *load_get_lmf_addr = NULL, *load_trampoline = NULL;
194 guint8 *load_get_lmf_addr = NULL, *load_trampoline = NULL;
198 int cfa_offset, lmf_offset, regsave_size, lr_offset;
199 GSList *unwind_ops = NULL;
200 MonoJumpInfo *ji = NULL;
203 #ifdef USE_JUMP_TABLES
207 /* Now we'll create in 'buf' the ARM trampoline code. This
208 is the trampoline code common to all methods */
211 code = buf = mono_global_codeman_reserve (buf_len);
214 * At this point lr points to the specific arg and sp points to the saved
215 * regs on the stack (all but PC and SP). The original LR value has been
216 * saved as sp + LR_OFFSET by the push in the specific trampoline
219 /* The offset of lmf inside the stack frame */
220 lmf_offset = STACK - sizeof (MonoLMF);
221 /* The size of the area already allocated by the push in the specific trampoline */
222 regsave_size = 14 * sizeof (mgreg_t);
223 /* The offset where lr was saved inside the regsave area */
224 lr_offset = 13 * sizeof (mgreg_t);
226 // FIXME: Finish the unwind info, the current info allows us to unwind
227 // when the trampoline is not in the epilog
229 // CFA = SP + (num registers pushed) * 4
230 cfa_offset = 14 * sizeof (mgreg_t);
231 mono_add_unwind_op_def_cfa (unwind_ops, code, buf, ARMREG_SP, cfa_offset);
232 // PC saved at sp+LR_OFFSET
233 mono_add_unwind_op_offset (unwind_ops, code, buf, ARMREG_LR, -4);
235 if (aot && tramp_type != MONO_TRAMPOLINE_GENERIC_CLASS_INIT) {
237 * For page trampolines the data is in r1, so just move it, otherwise use the got slot as below.
238 * The trampoline contains a pc-relative offset to the got slot
239 * preceeding the got slot where the value is stored. The offset can be
243 ARM_MOV_REG_REG (code, ARMREG_V2, ARMREG_R1);
245 ARM_LDR_IMM (code, ARMREG_V2, ARMREG_LR, 0);
246 ARM_ADD_REG_IMM (code, ARMREG_V2, ARMREG_V2, 4, 0);
247 ARM_LDR_REG_REG (code, ARMREG_V2, ARMREG_V2, ARMREG_LR);
250 if (tramp_type != MONO_TRAMPOLINE_GENERIC_CLASS_INIT) {
251 ARM_LDR_IMM (code, ARMREG_V2, ARMREG_LR, 0);
254 ARM_MOV_REG_REG (code, ARMREG_V2, MONO_ARCH_VTABLE_REG);
256 ARM_LDR_IMM (code, ARMREG_V3, ARMREG_SP, lr_offset);
258 /* ok, now we can continue with the MonoLMF setup, mostly untouched
259 * from emit_prolog in mini-arm.c
260 * This is a synthetized call to mono_get_lmf_addr ()
263 ji = mono_patch_info_list_prepend (ji, code - buf, MONO_PATCH_INFO_JIT_ICALL_ADDR, "mono_get_lmf_addr");
264 ARM_LDR_IMM (code, ARMREG_R0, ARMREG_PC, 0);
266 *(gpointer*)code = NULL;
268 ARM_LDR_REG_REG (code, ARMREG_R0, ARMREG_PC, ARMREG_R0);
270 #ifdef USE_JUMP_TABLES
271 load_get_lmf_addr = mono_jumptable_add_entry ();
272 code = mono_arm_load_jumptable_entry (code, load_get_lmf_addr, ARMREG_R0);
274 load_get_lmf_addr = code;
278 ARM_MOV_REG_REG (code, ARMREG_LR, ARMREG_PC);
279 code = emit_bx (code, ARMREG_R0);
281 /* we build the MonoLMF structure on the stack - see mini-arm.h
282 * The pointer to the struct is put in r1.
283 * the iregs array is already allocated on the stack by push.
285 ARM_SUB_REG_IMM8 (code, ARMREG_SP, ARMREG_SP, STACK - regsave_size);
286 cfa_offset += STACK - regsave_size;
287 mono_add_unwind_op_def_cfa_offset (unwind_ops, code, buf, cfa_offset);
289 ARM_ADD_REG_IMM8 (code, ARMREG_V1, ARMREG_SP, STACK - sizeof (MonoLMF));
292 * The stack now looks like:
294 * v1 -> <rest of LMF>
298 /* r0 is the result from mono_get_lmf_addr () */
299 ARM_STR_IMM (code, ARMREG_R0, ARMREG_V1, G_STRUCT_OFFSET (MonoLMF, lmf_addr));
300 /* new_lmf->previous_lmf = *lmf_addr */
301 ARM_LDR_IMM (code, ARMREG_R2, ARMREG_R0, G_STRUCT_OFFSET (MonoLMF, previous_lmf));
302 ARM_STR_IMM (code, ARMREG_R2, ARMREG_V1, G_STRUCT_OFFSET (MonoLMF, previous_lmf));
303 /* *(lmf_addr) = r1 */
304 ARM_STR_IMM (code, ARMREG_V1, ARMREG_R0, G_STRUCT_OFFSET (MonoLMF, previous_lmf));
305 /* save method info (it's in v2) */
306 if ((tramp_type == MONO_TRAMPOLINE_JIT) || (tramp_type == MONO_TRAMPOLINE_JUMP))
307 ARM_STR_IMM (code, ARMREG_V2, ARMREG_V1, G_STRUCT_OFFSET (MonoLMF, method));
309 ARM_MOV_REG_IMM8 (code, ARMREG_R2, 0);
310 ARM_STR_IMM (code, ARMREG_R2, ARMREG_V1, G_STRUCT_OFFSET (MonoLMF, method));
313 ARM_ADD_REG_IMM8 (code, ARMREG_R2, ARMREG_SP, cfa_offset);
314 ARM_STR_IMM (code, ARMREG_R2, ARMREG_V1, G_STRUCT_OFFSET (MonoLMF, sp));
316 ARM_LDR_IMM (code, ARMREG_R2, ARMREG_V1, (G_STRUCT_OFFSET (MonoLMF, iregs) + ARMREG_FP*4));
317 ARM_STR_IMM (code, ARMREG_R2, ARMREG_V1, G_STRUCT_OFFSET (MonoLMF, fp));
318 /* save the IP (caller ip) */
319 if (tramp_type == MONO_TRAMPOLINE_JUMP) {
320 ARM_MOV_REG_IMM8 (code, ARMREG_R2, 0);
322 ARM_LDR_IMM (code, ARMREG_R2, ARMREG_V1, (G_STRUCT_OFFSET (MonoLMF, iregs) + 13*4));
324 ARM_STR_IMM (code, ARMREG_R2, ARMREG_V1, G_STRUCT_OFFSET (MonoLMF, ip));
327 * Now we're ready to call xxx_trampoline ().
329 /* Arg 1: the saved registers */
330 ARM_ADD_REG_IMM8 (code, ARMREG_R0, ARMREG_V1, G_STRUCT_OFFSET (MonoLMF, iregs));
332 /* Arg 2: code (next address to the instruction that called us) */
333 if (tramp_type == MONO_TRAMPOLINE_JUMP) {
334 ARM_MOV_REG_IMM8 (code, ARMREG_R1, 0);
336 ARM_MOV_REG_REG (code, ARMREG_R1, ARMREG_V3);
339 /* Arg 3: the specific argument, stored in v2
341 ARM_MOV_REG_REG (code, ARMREG_R2, ARMREG_V2);
344 char *icall_name = g_strdup_printf ("trampoline_func_%d", tramp_type);
345 ji = mono_patch_info_list_prepend (ji, code - buf, MONO_PATCH_INFO_JIT_ICALL_ADDR, icall_name);
346 ARM_LDR_IMM (code, ARMREG_IP, ARMREG_PC, 0);
348 *(gpointer*)code = NULL;
350 ARM_LDR_REG_REG (code, ARMREG_IP, ARMREG_PC, ARMREG_IP);
352 #ifdef USE_JUMP_TABLES
353 load_trampoline = mono_jumptable_add_entry ();
354 code = mono_arm_load_jumptable_entry (code, load_trampoline, ARMREG_IP);
356 load_trampoline = code;
361 ARM_MOV_REG_REG (code, ARMREG_LR, ARMREG_PC);
362 code = emit_bx (code, ARMREG_IP);
364 /* OK, code address is now on r0. Move it to the place on the stack
365 * where IP was saved (it is now no more useful to us and it can be
366 * clobbered). This way we can just restore all the regs in one inst
369 ARM_STR_IMM (code, ARMREG_R0, ARMREG_V1, G_STRUCT_OFFSET (MonoLMF, iregs) + (ARMREG_R12 * sizeof (mgreg_t)));
371 /* Check for thread interruption */
372 /* This is not perf critical code so no need to check the interrupt flag */
374 * Have to call the _force_ variant, since there could be a protected wrapper on the top of the stack.
377 ji = mono_patch_info_list_prepend (ji, code - buf, MONO_PATCH_INFO_JIT_ICALL_ADDR, "mono_thread_force_interruption_checkpoint");
378 ARM_LDR_IMM (code, ARMREG_IP, ARMREG_PC, 0);
380 *(gpointer*)code = NULL;
382 ARM_LDR_REG_REG (code, ARMREG_IP, ARMREG_PC, ARMREG_IP);
384 #ifdef USE_JUMP_TABLES
385 gpointer *jte = mono_jumptable_add_entry ();
386 code = mono_arm_load_jumptable_entry (code, jte, ARMREG_IP);
387 jte [0] = mono_thread_force_interruption_checkpoint;
389 ARM_LDR_IMM (code, ARMREG_IP, ARMREG_PC, 0);
391 *(gpointer*)code = mono_thread_force_interruption_checkpoint;
395 ARM_MOV_REG_REG (code, ARMREG_LR, ARMREG_PC);
396 code = emit_bx (code, ARMREG_IP);
399 * Now we restore the MonoLMF (see emit_epilogue in mini-arm.c)
400 * and the rest of the registers, so the method called will see
401 * the same state as before we executed.
403 /* ip = previous_lmf */
404 ARM_LDR_IMM (code, ARMREG_IP, ARMREG_V1, G_STRUCT_OFFSET (MonoLMF, previous_lmf));
406 ARM_LDR_IMM (code, ARMREG_LR, ARMREG_V1, G_STRUCT_OFFSET (MonoLMF, lmf_addr));
407 /* *(lmf_addr) = previous_lmf */
408 ARM_STR_IMM (code, ARMREG_IP, ARMREG_LR, G_STRUCT_OFFSET (MonoLMF, previous_lmf));
410 /* Non-standard function epilogue. Instead of doing a proper
411 * return, we just jump to the compiled code.
413 /* Restore the registers and jump to the code:
414 * Note that IP has been conveniently set to the method addr.
416 ARM_ADD_REG_IMM8 (code, ARMREG_SP, ARMREG_SP, STACK - regsave_size);
417 ARM_POP_NWB (code, 0x5fff);
418 if (tramp_type == MONO_TRAMPOLINE_RGCTX_LAZY_FETCH)
419 ARM_MOV_REG_REG (code, ARMREG_R0, ARMREG_IP);
420 ARM_ADD_REG_IMM8 (code, ARMREG_SP, ARMREG_SP, regsave_size);
421 if ((tramp_type == MONO_TRAMPOLINE_CLASS_INIT) || (tramp_type == MONO_TRAMPOLINE_GENERIC_CLASS_INIT) || (tramp_type == MONO_TRAMPOLINE_RGCTX_LAZY_FETCH))
422 code = emit_bx (code, ARMREG_LR);
424 code = emit_bx (code, ARMREG_IP);
426 #ifdef USE_JUMP_TABLES
427 load_get_lmf_addr [0] = mono_get_lmf_addr;
428 load_trampoline [0] = (gpointer)mono_get_trampoline_func (tramp_type);
430 constants = (gpointer*)code;
431 constants [0] = mono_get_lmf_addr;
432 constants [1] = (gpointer)mono_get_trampoline_func (tramp_type);
435 /* backpatch by emitting the missing instructions skipped above */
436 ARM_LDR_IMM (load_get_lmf_addr, ARMREG_R0, ARMREG_PC, (code - load_get_lmf_addr - 8));
437 ARM_LDR_IMM (load_trampoline, ARMREG_IP, ARMREG_PC, (code + 4 - load_trampoline - 8));
443 /* Flush instruction cache, since we've generated code */
444 mono_arch_flush_icache (buf, code - buf);
447 g_assert ((code - buf) <= buf_len);
450 tramp_name = mono_get_generic_trampoline_name (tramp_type);
451 *info = mono_tramp_info_create (tramp_name, buf, code - buf, ji, unwind_ops);
459 mono_arch_get_nullified_class_init_trampoline (MonoTrampInfo **info)
463 code = buf = mono_global_codeman_reserve (16);
465 code = emit_bx (code, ARMREG_LR);
467 mono_arch_flush_icache (buf, code - buf);
470 *info = mono_tramp_info_create ("nullified_class_init_trampoline", buf, code - buf, NULL, NULL);
475 #define SPEC_TRAMP_SIZE 24
478 mono_arch_create_specific_trampoline (gpointer arg1, MonoTrampolineType tramp_type, MonoDomain *domain, guint32 *code_len)
480 guint8 *code, *buf, *tramp;
482 #ifndef USE_JUMP_TABLES
483 guint32 short_branch;
485 guint32 size = SPEC_TRAMP_SIZE;
487 tramp = mono_get_trampoline_code (tramp_type);
489 mono_domain_lock (domain);
490 #ifdef USE_JUMP_TABLES
491 code = buf = mono_domain_code_reserve_align (domain, size, 4);
493 code = buf = mono_domain_code_reserve_align (domain, size, 4);
494 if ((short_branch = branch_for_target_reachable (code + 4, tramp))) {
496 mono_domain_code_commit (domain, code, SPEC_TRAMP_SIZE, size);
499 mono_domain_unlock (domain);
501 #ifdef USE_JUMP_TABLES
502 /* For jumptables case we always generate the same code for trampolines,
504 * push {r0, r1, r2, r3, r4, r5, r6, r7, r8, r9, r10, r11, r12, lr}
510 ARM_PUSH (code, 0x5fff);
511 constants = mono_jumptable_add_entries (2);
512 code = mono_arm_load_jumptable_entry_addr (code, constants, ARMREG_LR);
513 ARM_LDR_IMM (code, ARMREG_R1, ARMREG_LR, 4);
514 code = emit_bx (code, ARMREG_R1);
515 constants [0] = arg1;
516 constants [1] = tramp;
518 /* we could reduce this to 12 bytes if tramp is within reach:
522 * The called code can access method using the lr register
523 * A 20 byte sequence could be:
525 * ARM_MOV_REG_REG (lr, pc)
526 * ARM_LDR_IMM (pc, pc, 0)
530 /* We save all the registers, except PC and SP */
531 ARM_PUSH (code, 0x5fff);
533 constants = (gpointer*)code;
534 constants [0] = GUINT_TO_POINTER (short_branch | (1 << 24));
535 constants [1] = arg1;
538 ARM_LDR_IMM (code, ARMREG_R1, ARMREG_PC, 8); /* temp reg */
539 ARM_MOV_REG_REG (code, ARMREG_LR, ARMREG_PC);
540 code = emit_bx (code, ARMREG_R1);
542 constants = (gpointer*)code;
543 constants [0] = arg1;
544 constants [1] = tramp;
549 /* Flush instruction cache, since we've generated code */
550 mono_arch_flush_icache (buf, code - buf);
552 g_assert ((code - buf) <= size);
555 *code_len = code - buf;
561 * mono_arch_get_unbox_trampoline:
563 * @addr: pointer to native code for @m
565 * when value type methods are called through the vtable we need to unbox the
566 * this argument. This method returns a pointer to a trampoline which does
567 * unboxing before calling the method
570 mono_arch_get_unbox_trampoline (MonoMethod *m, gpointer addr)
572 guint8 *code, *start;
573 MonoDomain *domain = mono_domain_get ();
574 #ifdef USE_JUMP_TABLES
581 start = code = mono_domain_code_reserve (domain, size);
583 #ifdef USE_JUMP_TABLES
584 jte = mono_jumptable_add_entry ();
585 code = mono_arm_load_jumptable_entry (code, jte, ARMREG_IP);
586 ARM_ADD_REG_IMM8 (code, ARMREG_R0, ARMREG_R0, sizeof (MonoObject));
587 code = emit_bx (code, ARMREG_IP);
590 ARM_LDR_IMM (code, ARMREG_IP, ARMREG_PC, 4);
591 ARM_ADD_REG_IMM8 (code, ARMREG_R0, ARMREG_R0, sizeof (MonoObject));
592 code = emit_bx (code, ARMREG_IP);
593 *(guint32*)code = (guint32)addr;
596 mono_arch_flush_icache (start, code - start);
597 g_assert ((code - start) <= size);
598 /*g_print ("unbox trampoline at %d for %s:%s\n", this_pos, m->klass->name, m->name);
599 g_print ("unbox code is at %p for method at %p\n", start, addr);*/
605 mono_arch_get_static_rgctx_trampoline (MonoMethod *m, MonoMethodRuntimeGenericContext *mrgctx, gpointer addr)
607 guint8 *code, *start;
608 #ifdef USE_JUMP_TABLES
614 MonoDomain *domain = mono_domain_get ();
616 start = code = mono_domain_code_reserve (domain, buf_len);
618 #ifdef USE_JUMP_TABLES
619 jte = mono_jumptable_add_entries (2);
620 code = mono_arm_load_jumptable_entry_addr (code, jte, ARMREG_IP);
621 ARM_LDR_IMM (code, MONO_ARCH_RGCTX_REG, ARMREG_IP, 0);
622 ARM_LDR_IMM (code, ARMREG_IP, ARMREG_IP, 4);
623 ARM_BX (code, ARMREG_IP);
627 ARM_LDR_IMM (code, MONO_ARCH_RGCTX_REG, ARMREG_PC, 0);
628 ARM_LDR_IMM (code, ARMREG_PC, ARMREG_PC, 0);
629 *(guint32*)code = (guint32)mrgctx;
631 *(guint32*)code = (guint32)addr;
635 g_assert ((code - start) <= buf_len);
637 mono_arch_flush_icache (start, code - start);
643 mono_arch_create_rgctx_lazy_fetch_trampoline (guint32 slot, MonoTrampInfo **info, gboolean aot)
649 guint8 **rgctx_null_jumps;
653 MonoJumpInfo *ji = NULL;
654 GSList *unwind_ops = NULL;
655 #ifdef USE_JUMP_TABLES
659 mrgctx = MONO_RGCTX_SLOT_IS_MRGCTX (slot);
660 index = MONO_RGCTX_SLOT_INDEX (slot);
662 index += MONO_SIZEOF_METHOD_RUNTIME_GENERIC_CONTEXT / sizeof (gpointer);
663 for (depth = 0; ; ++depth) {
664 int size = mono_class_rgctx_get_array_size (depth, mrgctx);
666 if (index < size - 1)
671 tramp_size = 64 + 16 * depth;
673 code = buf = mono_global_codeman_reserve (tramp_size);
675 mono_add_unwind_op_def_cfa (unwind_ops, code, buf, ARMREG_SP, 0);
677 rgctx_null_jumps = g_malloc (sizeof (guint8*) * (depth + 2));
680 /* The vtable/mrgctx is in R0 */
681 g_assert (MONO_ARCH_VTABLE_REG == ARMREG_R0);
685 ARM_MOV_REG_REG (code, ARMREG_R1, ARMREG_R0);
687 /* load rgctx ptr from vtable */
688 g_assert (arm_is_imm12 (G_STRUCT_OFFSET (MonoVTable, runtime_generic_context)));
689 ARM_LDR_IMM (code, ARMREG_R1, ARMREG_R0, G_STRUCT_OFFSET (MonoVTable, runtime_generic_context));
690 /* is the rgctx ptr null? */
691 ARM_CMP_REG_IMM (code, ARMREG_R1, 0, 0);
692 /* if yes, jump to actual trampoline */
693 rgctx_null_jumps [njumps ++] = code;
694 ARM_B_COND (code, ARMCOND_EQ, 0);
697 for (i = 0; i < depth; ++i) {
698 /* load ptr to next array */
699 if (mrgctx && i == 0) {
700 g_assert (arm_is_imm12 (MONO_SIZEOF_METHOD_RUNTIME_GENERIC_CONTEXT));
701 ARM_LDR_IMM (code, ARMREG_R1, ARMREG_R1, MONO_SIZEOF_METHOD_RUNTIME_GENERIC_CONTEXT);
703 ARM_LDR_IMM (code, ARMREG_R1, ARMREG_R1, 0);
705 /* is the ptr null? */
706 ARM_CMP_REG_IMM (code, ARMREG_R1, 0, 0);
707 /* if yes, jump to actual trampoline */
708 rgctx_null_jumps [njumps ++] = code;
709 ARM_B_COND (code, ARMCOND_EQ, 0);
713 code = mono_arm_emit_load_imm (code, ARMREG_R2, sizeof (gpointer) * (index + 1));
714 ARM_LDR_REG_REG (code, ARMREG_R1, ARMREG_R1, ARMREG_R2);
715 /* is the slot null? */
716 ARM_CMP_REG_IMM (code, ARMREG_R1, 0, 0);
717 /* if yes, jump to actual trampoline */
718 rgctx_null_jumps [njumps ++] = code;
719 ARM_B_COND (code, ARMCOND_EQ, 0);
720 /* otherwise return, result is in R1 */
721 ARM_MOV_REG_REG (code, ARMREG_R0, ARMREG_R1);
722 code = emit_bx (code, ARMREG_LR);
724 g_assert (njumps <= depth + 2);
725 for (i = 0; i < njumps; ++i)
726 arm_patch (rgctx_null_jumps [i], code);
728 g_free (rgctx_null_jumps);
732 /* The vtable/mrgctx is still in R0 */
735 ji = mono_patch_info_list_prepend (ji, code - buf, MONO_PATCH_INFO_JIT_ICALL_ADDR, g_strdup_printf ("specific_trampoline_lazy_fetch_%u", slot));
736 ARM_LDR_IMM (code, ARMREG_R1, ARMREG_PC, 0);
738 *(gpointer*)code = NULL;
740 ARM_LDR_REG_REG (code, ARMREG_PC, ARMREG_PC, ARMREG_R1);
742 tramp = mono_arch_create_specific_trampoline (GUINT_TO_POINTER (slot), MONO_TRAMPOLINE_RGCTX_LAZY_FETCH, mono_get_root_domain (), &code_len);
744 /* Jump to the actual trampoline */
745 #ifdef USE_JUMP_TABLES
746 jte = mono_jumptable_add_entry ();
748 code = mono_arm_load_jumptable_entry (code, jte, ARMREG_R1);
749 code = emit_bx (code, ARMREG_R1);
751 ARM_LDR_IMM (code, ARMREG_R1, ARMREG_PC, 0); /* temp reg */
752 code = emit_bx (code, ARMREG_R1);
753 *(gpointer*)code = tramp;
758 mono_arch_flush_icache (buf, code - buf);
760 g_assert (code - buf <= tramp_size);
763 char *name = mono_get_rgctx_fetch_trampoline_name (slot);
764 *info = mono_tramp_info_create (name, buf, code - buf, ji, unwind_ops);
772 mono_arch_create_general_rgctx_lazy_fetch_trampoline (MonoTrampInfo **info, gboolean aot)
776 MonoJumpInfo *ji = NULL;
777 GSList *unwind_ops = NULL;
783 code = buf = mono_global_codeman_reserve (tramp_size);
785 mono_add_unwind_op_def_cfa (unwind_ops, code, buf, ARMREG_SP, 0);
787 // FIXME: Currently, we always go to the slow path.
788 /* Load trampoline addr */
789 ARM_LDR_IMM (code, ARMREG_R1, MONO_ARCH_RGCTX_REG, 4);
790 /* The vtable/mrgctx is in R0 */
791 g_assert (MONO_ARCH_VTABLE_REG == ARMREG_R0);
792 code = emit_bx (code, ARMREG_R1);
794 mono_arch_flush_icache (buf, code - buf);
796 g_assert (code - buf <= tramp_size);
799 *info = mono_tramp_info_create ("rgctx_fetch_trampoline_general", buf, code - buf, ji, unwind_ops);
804 #define arm_is_imm8(v) ((v) > -256 && (v) < 256)
807 mono_arch_create_generic_class_init_trampoline (MonoTrampInfo **info, gboolean aot)
811 static int byte_offset = -1;
812 static guint8 bitmask;
815 guint32 code_len, imm8;
817 GSList *unwind_ops = NULL;
818 MonoJumpInfo *ji = NULL;
822 code = buf = mono_global_codeman_reserve (tramp_size);
825 mono_marshal_find_bitfield_offset (MonoVTable, initialized, &byte_offset, &bitmask);
827 g_assert (arm_is_imm8 (byte_offset));
828 ARM_LDRSB_IMM (code, ARMREG_IP, MONO_ARCH_VTABLE_REG, byte_offset);
829 imm8 = mono_arm_is_rotated_imm8 (bitmask, &rot_amount);
830 g_assert (imm8 >= 0);
831 ARM_AND_REG_IMM (code, ARMREG_IP, ARMREG_IP, imm8, rot_amount);
832 ARM_CMP_REG_IMM (code, ARMREG_IP, 0, 0);
834 ARM_B_COND (code, ARMCOND_EQ, 0);
836 /* Initialized case */
837 ARM_MOV_REG_REG (code, ARMREG_PC, ARMREG_LR);
839 /* Uninitialized case */
840 arm_patch (jump, code);
843 ji = mono_patch_info_list_prepend (ji, code - buf, MONO_PATCH_INFO_JIT_ICALL_ADDR, "specific_trampoline_generic_class_init");
844 ARM_LDR_IMM (code, ARMREG_R1, ARMREG_PC, 0);
846 *(gpointer*)code = NULL;
848 ARM_LDR_REG_REG (code, ARMREG_PC, ARMREG_PC, ARMREG_R1);
850 #ifdef USE_JUMP_TABLES
851 gpointer *jte = mono_jumptable_add_entry ();
853 tramp = mono_arch_create_specific_trampoline (NULL, MONO_TRAMPOLINE_GENERIC_CLASS_INIT, mono_get_root_domain (), &code_len);
855 /* Jump to the actual trampoline */
856 #ifdef USE_JUMP_TABLES
857 code = mono_arm_load_jumptable_entry (code, jte, ARMREG_R1);
859 code = emit_bx (code, ARMREG_R1);
861 ARM_LDR_IMM (code, ARMREG_R1, ARMREG_PC, 0); /* temp reg */
862 code = emit_bx (code, ARMREG_R1);
863 *(gpointer*)code = tramp;
868 mono_arch_flush_icache (buf, code - buf);
870 g_assert (code - buf <= tramp_size);
873 *info = mono_tramp_info_create ("generic_class_init_trampoline", buf, code - buf, ji, unwind_ops);
881 mono_arch_create_generic_trampoline (MonoTrampolineType tramp_type, MonoTrampInfo **info, gboolean aot)
883 g_assert_not_reached ();
888 mono_arch_create_specific_trampoline (gpointer arg1, MonoTrampolineType tramp_type, MonoDomain *domain, guint32 *code_len)
890 g_assert_not_reached ();
895 mono_arch_get_unbox_trampoline (MonoMethod *m, gpointer addr)
897 g_assert_not_reached ();
902 mono_arch_get_static_rgctx_trampoline (MonoMethod *m, MonoMethodRuntimeGenericContext *mrgctx, gpointer addr)
904 g_assert_not_reached ();
909 mono_arch_create_rgctx_lazy_fetch_trampoline (guint32 slot, MonoTrampInfo **info, gboolean aot)
911 g_assert_not_reached ();
916 mono_arch_create_generic_class_init_trampoline (MonoTrampInfo **info, gboolean aot)
918 g_assert_not_reached ();
923 mono_arch_get_nullified_class_init_trampoline (MonoTrampInfo **info)
925 g_assert_not_reached ();
929 #endif /* DISABLE_JIT */
932 mono_arch_get_call_target (guint8 *code)
934 guint32 ins = ((guint32*)(gpointer)code) [-1];
937 /* Should be a 'bl' or a 'b' */
938 if (((ins >> 25) & 0x7) == 0x5) {
940 /* Should be a 'bl' */
941 if ((((ins >> 25) & 0x7) == 0x5) && (((ins >> 24) & 0x1) == 0x1)) {
943 gint32 disp = ((gint32)ins) & 0xffffff;
944 guint8 *target = code - 4 + 8 + (disp * 4);
953 mono_arch_get_plt_info_offset (guint8 *plt_entry, mgreg_t *regs, guint8 *code)
955 /* The offset is stored as the 4th word of the plt entry */
956 return ((guint32*)plt_entry) [3];
960 * Return the address of the PLT entry called by the thumb code CODE.
963 mono_arm_get_thumb_plt_entry (guint8 *code)
965 int s, j1, j2, imm10, imm11, i1, i2, imm32;
970 /* code should be right after a BL */
971 code = (guint8*)((mgreg_t)code & ~1);
972 base = (guint8*)((mgreg_t)code & ~3);
974 t1 = ((guint16*)bl) [0];
975 t2 = ((guint16*)bl) [1];
977 g_assert ((t1 >> 11) == 0x1e);
979 s = (t1 >> 10) & 0x1;
980 imm10 = (t1 >> 0) & 0x3ff;
981 j1 = (t2 >> 13) & 0x1;
982 j2 = (t2 >> 11) & 0x1;
985 i1 = (s ^ j1) ? 0 : 1;
986 i2 = (s ^ j2) ? 0 : 1;
988 imm32 = (imm11 << 1) | (imm10 << 12) | (i2 << 22) | (i1 << 23);
992 target = code + imm32;
994 /* target now points to the thumb plt entry */
995 /* ldr.w r12, [pc, #8] */
996 g_assert (((guint16*)target) [0] == 0xf8df);
997 g_assert (((guint16*)target) [1] == 0xc008);
1000 * The PLT info offset is at offset 16, but mono_arch_get_plt_entry_offset () returns
1001 * the 3rd word, so compensate by returning a different value.
1011 * mono_arch_get_gsharedvt_arg_trampoline:
1013 * See tramp-x86.c for documentation.
1016 mono_arch_get_gsharedvt_arg_trampoline (MonoDomain *domain, gpointer arg, gpointer addr)
1018 guint8 *code, *start;
1020 gpointer *constants;
1024 start = code = mono_domain_code_reserve (domain, buf_len);
1026 /* Similar to the specialized trampoline code */
1027 ARM_PUSH (code, (1 << ARMREG_R0) | (1 << ARMREG_R1) | (1 << ARMREG_R2) | (1 << ARMREG_R3) | (1 << ARMREG_LR));
1028 ARM_LDR_IMM (code, ARMREG_IP, ARMREG_PC, 8);
1029 /* arg is passed in LR */
1030 ARM_LDR_IMM (code, ARMREG_LR, ARMREG_PC, 0);
1031 code = emit_bx (code, ARMREG_IP);
1032 constants = (gpointer*)code;
1033 constants [0] = arg;
1034 constants [1] = addr;
1037 g_assert ((code - start) <= buf_len);
1039 nacl_domain_code_validate (domain, &start, buf_len, &code);
1040 mono_arch_flush_icache (start, code - start);
1048 mono_arch_get_gsharedvt_arg_trampoline (MonoDomain *domain, gpointer arg, gpointer addr)
1050 g_assert_not_reached ();
1056 #if defined(MONOTOUCH) || defined(MONO_EXTENSIONS)
1058 #include "../../../mono-extensions/mono/mini/tramp-arm-gsharedvt.c"
1063 mono_arm_start_gsharedvt_call (GSharedVtCallInfo *info, gpointer *caller, gpointer *callee, gpointer mrgctx_reg)
1065 g_assert_not_reached ();
1070 mono_arch_get_gsharedvt_trampoline (MonoTrampInfo **info, gboolean aot)
1077 #endif /* !MONOTOUCH */