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>
19 #include <mono/arch/arm/arm-vfp-codegen.h>
24 #define ALIGN_TO(val,align) ((((guint64)val) + ((align) - 1)) & ~((align) - 1))
26 #ifdef USE_JUMP_TABLES
29 decode_imm16 (guint32 insn)
31 return (((insn >> 16) & 0xf) << 12) | (insn & 0xfff);
34 #define INSN_MASK 0xff00000
35 #define MOVW_MASK ((3 << 24) | (0 << 20))
36 #define MOVT_MASK ((3 << 24) | (4 << 20))
39 mono_arch_jumptable_entry_from_code (guint8 *code)
41 guint32 insn1 = ((guint32*)code) [0];
42 guint32 insn2 = ((guint32*)code) [1];
44 if (((insn1 & INSN_MASK) == MOVW_MASK) &&
45 ((insn2 & INSN_MASK) == MOVT_MASK) ) {
46 guint32 imm_lo = decode_imm16 (insn1);
47 guint32 imm_hi = decode_imm16 (insn2);
48 return (gpointer*) GUINT_TO_POINTER (imm_lo | (imm_hi << 16));
50 g_assert_not_reached ();
60 mono_arch_patch_callsite (guint8 *method_start, guint8 *code_ptr, guint8 *addr)
64 * code_ptr is 4 instructions after MOVW/MOVT used to address
67 jte = mono_jumptable_get_entry (code_ptr - 16);
68 g_assert ( jte != NULL);
73 mono_arch_patch_callsite (guint8 *method_start, guint8 *code_ptr, guint8 *addr)
75 guint32 *code = (guint32*)code_ptr;
77 /* This is the 'bl' or the 'mov pc' instruction */
81 * Note that methods are called also with the bl opcode.
83 if ((((*code) >> 25) & 7) == 5) {
84 /*g_print ("direct patching\n");*/
85 arm_patch ((guint8*)code, addr);
86 mono_arch_flush_icache ((guint8*)code, 4);
90 if ((((*code) >> 20) & 0xFF) == 0x12) {
91 /*g_print ("patching bx\n");*/
92 arm_patch ((guint8*)code, addr);
93 mono_arch_flush_icache ((guint8*)(code - 2), 4);
97 g_assert_not_reached ();
102 mono_arch_patch_plt_entry (guint8 *code, gpointer *got, mgreg_t *regs, guint8 *addr)
106 /* Patch the jump table entry used by the plt entry */
107 if (*(guint32*)code == 0xe59fc000) {
108 /* ARM_LDR_IMM (code, ARMREG_IP, ARMREG_PC, 0); */
109 guint32 offset = ((guint32*)code)[2];
111 jump_entry = code + offset + 12;
112 } else if (*(guint16*)(code - 4) == 0xf8df) {
114 * Thumb PLT entry, begins with ldr.w ip, [pc, #8], code points to entry + 4, see
115 * mono_arm_get_thumb_plt_entry ().
120 offset = *(guint32*)(code + 12);
121 jump_entry = code + offset + 8;
123 g_assert_not_reached ();
126 *(guint8**)jump_entry = addr;
130 mono_arch_nullify_class_init_trampoline (guint8 *code, mgreg_t *regs)
132 mono_arch_patch_callsite (NULL, code, mini_get_nullified_class_init_trampoline ());
137 #define arm_is_imm12(v) ((int)(v) > -4096 && (int)(v) < 4096)
139 #ifndef USE_JUMP_TABLES
141 * Return the instruction to jump from code to target, 0 if not
142 * reachable with a single instruction
145 branch_for_target_reachable (guint8 *branch, guint8 *target)
147 gint diff = target - branch - 8;
148 g_assert ((diff & 3) == 0);
150 if (diff <= 33554431)
151 return (ARMCOND_AL << ARMCOND_SHIFT) | (ARM_BR_TAG) | (diff >> 2);
153 /* diff between 0 and -33554432 */
154 if (diff >= -33554432)
155 return (ARMCOND_AL << ARMCOND_SHIFT) | (ARM_BR_TAG) | ((diff >> 2) & ~0xff000000);
161 static inline guint8*
162 emit_bx (guint8* code, int reg)
164 if (mono_arm_thumb_supported ())
167 ARM_MOV_REG_REG (code, ARMREG_PC, reg);
171 /* Stack size for trampoline function
173 #define STACK ALIGN_TO (sizeof (MonoLMF), 8)
175 /* Method-specific trampoline code fragment size */
176 #define METHOD_TRAMPOLINE_SIZE 64
178 /* Jump-specific trampoline code fragment size */
179 #define JUMP_TRAMPOLINE_SIZE 64
182 mono_arch_create_generic_trampoline (MonoTrampolineType tramp_type, MonoTrampInfo **info, gboolean aot)
185 guint8 *buf, *code = NULL;
186 #ifdef USE_JUMP_TABLES
187 gpointer *load_get_lmf_addr = NULL, *load_trampoline = NULL;
189 guint8 *load_get_lmf_addr = NULL, *load_trampoline = NULL;
193 int cfa_offset, regsave_size, lr_offset;
194 GSList *unwind_ops = NULL;
195 MonoJumpInfo *ji = NULL;
198 #ifdef USE_JUMP_TABLES
202 /* Now we'll create in 'buf' the ARM trampoline code. This
203 is the trampoline code common to all methods */
207 /* Add space for saving/restoring VFP regs. */
208 if (mono_arm_is_hard_float ())
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 size of the area already allocated by the push in the specific trampoline */
220 regsave_size = 14 * sizeof (mgreg_t);
221 /* The offset where lr was saved inside the regsave area */
222 lr_offset = 13 * sizeof (mgreg_t);
224 // FIXME: Finish the unwind info, the current info allows us to unwind
225 // when the trampoline is not in the epilog
227 // CFA = SP + (num registers pushed) * 4
228 cfa_offset = 14 * sizeof (mgreg_t);
229 mono_add_unwind_op_def_cfa (unwind_ops, code, buf, ARMREG_SP, cfa_offset);
230 // PC saved at sp+LR_OFFSET
231 mono_add_unwind_op_offset (unwind_ops, code, buf, ARMREG_LR, -4);
233 if (aot && tramp_type != MONO_TRAMPOLINE_GENERIC_CLASS_INIT) {
235 * For page trampolines the data is in r1, so just move it, otherwise use the got slot as below.
236 * The trampoline contains a pc-relative offset to the got slot
237 * preceeding the got slot where the value is stored. The offset can be
241 ARM_MOV_REG_REG (code, ARMREG_V2, ARMREG_R1);
243 ARM_LDR_IMM (code, ARMREG_V2, ARMREG_LR, 0);
244 ARM_ADD_REG_IMM (code, ARMREG_V2, ARMREG_V2, 4, 0);
245 ARM_LDR_REG_REG (code, ARMREG_V2, ARMREG_V2, ARMREG_LR);
248 if (tramp_type != MONO_TRAMPOLINE_GENERIC_CLASS_INIT) {
249 ARM_LDR_IMM (code, ARMREG_V2, ARMREG_LR, 0);
252 ARM_MOV_REG_REG (code, ARMREG_V2, MONO_ARCH_VTABLE_REG);
254 ARM_LDR_IMM (code, ARMREG_V3, ARMREG_SP, lr_offset);
256 /* ok, now we can continue with the MonoLMF setup, mostly untouched
257 * from emit_prolog in mini-arm.c
258 * This is a synthetized call to mono_get_lmf_addr ()
261 ji = mono_patch_info_list_prepend (ji, code - buf, MONO_PATCH_INFO_JIT_ICALL_ADDR, "mono_get_lmf_addr");
262 ARM_LDR_IMM (code, ARMREG_R0, ARMREG_PC, 0);
264 *(gpointer*)code = NULL;
266 ARM_LDR_REG_REG (code, ARMREG_R0, ARMREG_PC, ARMREG_R0);
268 #ifdef USE_JUMP_TABLES
269 load_get_lmf_addr = mono_jumptable_add_entry ();
270 code = mono_arm_load_jumptable_entry (code, load_get_lmf_addr, ARMREG_R0);
272 load_get_lmf_addr = code;
276 ARM_MOV_REG_REG (code, ARMREG_LR, ARMREG_PC);
277 code = emit_bx (code, ARMREG_R0);
279 /* we build the MonoLMF structure on the stack - see mini-arm.h
280 * The pointer to the struct is put in r1.
281 * the iregs array is already allocated on the stack by push.
283 code = mono_arm_emit_load_imm (code, ARMREG_R2, STACK - regsave_size);
284 ARM_SUB_REG_REG (code, ARMREG_SP, ARMREG_SP, ARMREG_R2);
285 cfa_offset += STACK - regsave_size;
286 mono_add_unwind_op_def_cfa_offset (unwind_ops, code, buf, cfa_offset);
288 code = mono_arm_emit_load_imm (code, ARMREG_R2, STACK - sizeof (MonoLMF));
289 ARM_ADD_REG_REG (code, ARMREG_V1, ARMREG_SP, ARMREG_R2);
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 code = mono_arm_emit_load_imm (code, ARMREG_R2, cfa_offset);
314 ARM_ADD_REG_REG (code, ARMREG_R2, ARMREG_SP, ARMREG_R2);
315 ARM_STR_IMM (code, ARMREG_R2, ARMREG_V1, G_STRUCT_OFFSET (MonoLMF, sp));
317 ARM_LDR_IMM (code, ARMREG_R2, ARMREG_V1, (G_STRUCT_OFFSET (MonoLMF, iregs) + ARMREG_FP*4));
318 ARM_STR_IMM (code, ARMREG_R2, ARMREG_V1, G_STRUCT_OFFSET (MonoLMF, fp));
319 /* save the IP (caller ip) */
320 if (tramp_type == MONO_TRAMPOLINE_JUMP) {
321 ARM_MOV_REG_IMM8 (code, ARMREG_R2, 0);
323 ARM_LDR_IMM (code, ARMREG_R2, ARMREG_V1, (G_STRUCT_OFFSET (MonoLMF, iregs) + 13*4));
325 ARM_STR_IMM (code, ARMREG_R2, ARMREG_V1, G_STRUCT_OFFSET (MonoLMF, ip));
327 /* Save VFP registers. */
328 if (mono_arm_is_hard_float ()) {
330 * Strictly speaking, we don't have to save d0-d7 in the LMF, but
331 * it's easier than attempting to store them on the stack since
332 * this trampoline code is pretty messy.
334 ARM_ADD_REG_IMM8 (code, ARMREG_R0, ARMREG_V1, G_STRUCT_OFFSET (MonoLMF, fregs));
335 ARM_FSTMD (code, ARM_VFP_D0, 8, ARMREG_R0);
339 * Now we're ready to call xxx_trampoline ().
341 /* Arg 1: the saved registers */
342 ARM_ADD_REG_IMM (code, ARMREG_R0, ARMREG_V1, G_STRUCT_OFFSET (MonoLMF, iregs), 0);
344 /* Arg 2: code (next address to the instruction that called us) */
345 if (tramp_type == MONO_TRAMPOLINE_JUMP) {
346 ARM_MOV_REG_IMM8 (code, ARMREG_R1, 0);
348 ARM_MOV_REG_REG (code, ARMREG_R1, ARMREG_V3);
351 /* Arg 3: the specific argument, stored in v2
353 ARM_MOV_REG_REG (code, ARMREG_R2, ARMREG_V2);
356 char *icall_name = g_strdup_printf ("trampoline_func_%d", tramp_type);
357 ji = mono_patch_info_list_prepend (ji, code - buf, MONO_PATCH_INFO_JIT_ICALL_ADDR, icall_name);
358 ARM_LDR_IMM (code, ARMREG_IP, ARMREG_PC, 0);
360 *(gpointer*)code = NULL;
362 ARM_LDR_REG_REG (code, ARMREG_IP, ARMREG_PC, ARMREG_IP);
364 #ifdef USE_JUMP_TABLES
365 load_trampoline = mono_jumptable_add_entry ();
366 code = mono_arm_load_jumptable_entry (code, load_trampoline, ARMREG_IP);
368 load_trampoline = code;
373 ARM_MOV_REG_REG (code, ARMREG_LR, ARMREG_PC);
374 code = emit_bx (code, ARMREG_IP);
376 /* OK, code address is now on r0. Move it to the place on the stack
377 * where IP was saved (it is now no more useful to us and it can be
378 * clobbered). This way we can just restore all the regs in one inst
381 ARM_STR_IMM (code, ARMREG_R0, ARMREG_V1, G_STRUCT_OFFSET (MonoLMF, iregs) + (ARMREG_R12 * sizeof (mgreg_t)));
383 /* Check for thread interruption */
384 /* This is not perf critical code so no need to check the interrupt flag */
386 * Have to call the _force_ variant, since there could be a protected wrapper on the top of the stack.
389 ji = mono_patch_info_list_prepend (ji, code - buf, MONO_PATCH_INFO_JIT_ICALL_ADDR, "mono_thread_force_interruption_checkpoint");
390 ARM_LDR_IMM (code, ARMREG_IP, ARMREG_PC, 0);
392 *(gpointer*)code = NULL;
394 ARM_LDR_REG_REG (code, ARMREG_IP, ARMREG_PC, ARMREG_IP);
396 #ifdef USE_JUMP_TABLES
397 gpointer *jte = mono_jumptable_add_entry ();
398 code = mono_arm_load_jumptable_entry (code, jte, ARMREG_IP);
399 jte [0] = mono_thread_force_interruption_checkpoint;
401 ARM_LDR_IMM (code, ARMREG_IP, ARMREG_PC, 0);
403 *(gpointer*)code = mono_thread_force_interruption_checkpoint;
407 ARM_MOV_REG_REG (code, ARMREG_LR, ARMREG_PC);
408 code = emit_bx (code, ARMREG_IP);
411 * Now we restore the MonoLMF (see emit_epilogue in mini-arm.c)
412 * and the rest of the registers, so the method called will see
413 * the same state as before we executed.
415 /* ip = previous_lmf */
416 ARM_LDR_IMM (code, ARMREG_IP, ARMREG_V1, G_STRUCT_OFFSET (MonoLMF, previous_lmf));
418 ARM_LDR_IMM (code, ARMREG_LR, ARMREG_V1, G_STRUCT_OFFSET (MonoLMF, lmf_addr));
419 /* *(lmf_addr) = previous_lmf */
420 ARM_STR_IMM (code, ARMREG_IP, ARMREG_LR, G_STRUCT_OFFSET (MonoLMF, previous_lmf));
422 /* Restore VFP registers. */
423 if (mono_arm_is_hard_float ()) {
424 ARM_ADD_REG_IMM8 (code, ARMREG_R0, ARMREG_V1, G_STRUCT_OFFSET (MonoLMF, fregs));
425 ARM_FLDMD (code, ARM_VFP_D0, 8, ARMREG_R0);
428 /* Non-standard function epilogue. Instead of doing a proper
429 * return, we just jump to the compiled code.
431 /* Restore the registers and jump to the code:
432 * Note that IP has been conveniently set to the method addr.
434 ARM_ADD_REG_IMM8 (code, ARMREG_SP, ARMREG_SP, STACK - regsave_size);
435 ARM_POP_NWB (code, 0x5fff);
436 if (tramp_type == MONO_TRAMPOLINE_RGCTX_LAZY_FETCH)
437 ARM_MOV_REG_REG (code, ARMREG_R0, ARMREG_IP);
438 ARM_ADD_REG_IMM8 (code, ARMREG_SP, ARMREG_SP, regsave_size);
439 if ((tramp_type == MONO_TRAMPOLINE_CLASS_INIT) || (tramp_type == MONO_TRAMPOLINE_GENERIC_CLASS_INIT) || (tramp_type == MONO_TRAMPOLINE_RGCTX_LAZY_FETCH))
440 code = emit_bx (code, ARMREG_LR);
442 code = emit_bx (code, ARMREG_IP);
444 #ifdef USE_JUMP_TABLES
445 load_get_lmf_addr [0] = mono_get_lmf_addr;
446 load_trampoline [0] = (gpointer)mono_get_trampoline_func (tramp_type);
448 constants = (gpointer*)code;
449 constants [0] = mono_get_lmf_addr;
450 constants [1] = (gpointer)mono_get_trampoline_func (tramp_type);
453 /* backpatch by emitting the missing instructions skipped above */
454 ARM_LDR_IMM (load_get_lmf_addr, ARMREG_R0, ARMREG_PC, (code - load_get_lmf_addr - 8));
455 ARM_LDR_IMM (load_trampoline, ARMREG_IP, ARMREG_PC, (code + 4 - load_trampoline - 8));
461 /* Flush instruction cache, since we've generated code */
462 mono_arch_flush_icache (buf, code - buf);
465 g_assert ((code - buf) <= buf_len);
468 tramp_name = mono_get_generic_trampoline_name (tramp_type);
469 *info = mono_tramp_info_create (tramp_name, buf, code - buf, ji, unwind_ops);
477 mono_arch_get_nullified_class_init_trampoline (MonoTrampInfo **info)
481 code = buf = mono_global_codeman_reserve (16);
483 code = emit_bx (code, ARMREG_LR);
485 mono_arch_flush_icache (buf, code - buf);
488 *info = mono_tramp_info_create ("nullified_class_init_trampoline", buf, code - buf, NULL, NULL);
493 #define SPEC_TRAMP_SIZE 24
496 mono_arch_create_specific_trampoline (gpointer arg1, MonoTrampolineType tramp_type, MonoDomain *domain, guint32 *code_len)
498 guint8 *code, *buf, *tramp;
500 #ifndef USE_JUMP_TABLES
501 guint32 short_branch;
503 guint32 size = SPEC_TRAMP_SIZE;
505 tramp = mono_get_trampoline_code (tramp_type);
507 mono_domain_lock (domain);
508 #ifdef USE_JUMP_TABLES
509 code = buf = mono_domain_code_reserve_align (domain, size, 4);
511 code = buf = mono_domain_code_reserve_align (domain, size, 4);
512 if ((short_branch = branch_for_target_reachable (code + 4, tramp))) {
514 mono_domain_code_commit (domain, code, SPEC_TRAMP_SIZE, size);
517 mono_domain_unlock (domain);
519 #ifdef USE_JUMP_TABLES
520 /* For jumptables case we always generate the same code for trampolines,
522 * push {r0, r1, r2, r3, r4, r5, r6, r7, r8, r9, r10, r11, r12, lr}
528 ARM_PUSH (code, 0x5fff);
529 constants = mono_jumptable_add_entries (2);
530 code = mono_arm_load_jumptable_entry_addr (code, constants, ARMREG_LR);
531 ARM_LDR_IMM (code, ARMREG_R1, ARMREG_LR, 4);
532 code = emit_bx (code, ARMREG_R1);
533 constants [0] = arg1;
534 constants [1] = tramp;
536 /* we could reduce this to 12 bytes if tramp is within reach:
540 * The called code can access method using the lr register
541 * A 20 byte sequence could be:
543 * ARM_MOV_REG_REG (lr, pc)
544 * ARM_LDR_IMM (pc, pc, 0)
548 /* We save all the registers, except PC and SP */
549 ARM_PUSH (code, 0x5fff);
551 constants = (gpointer*)code;
552 constants [0] = GUINT_TO_POINTER (short_branch | (1 << 24));
553 constants [1] = arg1;
556 ARM_LDR_IMM (code, ARMREG_R1, ARMREG_PC, 8); /* temp reg */
557 ARM_MOV_REG_REG (code, ARMREG_LR, ARMREG_PC);
558 code = emit_bx (code, ARMREG_R1);
560 constants = (gpointer*)code;
561 constants [0] = arg1;
562 constants [1] = tramp;
567 /* Flush instruction cache, since we've generated code */
568 mono_arch_flush_icache (buf, code - buf);
570 g_assert ((code - buf) <= size);
573 *code_len = code - buf;
579 * mono_arch_get_unbox_trampoline:
581 * @addr: pointer to native code for @m
583 * when value type methods are called through the vtable we need to unbox the
584 * this argument. This method returns a pointer to a trampoline which does
585 * unboxing before calling the method
588 mono_arch_get_unbox_trampoline (MonoMethod *m, gpointer addr)
590 guint8 *code, *start;
591 MonoDomain *domain = mono_domain_get ();
592 #ifdef USE_JUMP_TABLES
599 start = code = mono_domain_code_reserve (domain, size);
601 #ifdef USE_JUMP_TABLES
602 jte = mono_jumptable_add_entry ();
603 code = mono_arm_load_jumptable_entry (code, jte, ARMREG_IP);
604 ARM_ADD_REG_IMM8 (code, ARMREG_R0, ARMREG_R0, sizeof (MonoObject));
605 code = emit_bx (code, ARMREG_IP);
608 ARM_LDR_IMM (code, ARMREG_IP, ARMREG_PC, 4);
609 ARM_ADD_REG_IMM8 (code, ARMREG_R0, ARMREG_R0, sizeof (MonoObject));
610 code = emit_bx (code, ARMREG_IP);
611 *(guint32*)code = (guint32)addr;
614 mono_arch_flush_icache (start, code - start);
615 g_assert ((code - start) <= size);
616 /*g_print ("unbox trampoline at %d for %s:%s\n", this_pos, m->klass->name, m->name);
617 g_print ("unbox code is at %p for method at %p\n", start, addr);*/
623 mono_arch_get_static_rgctx_trampoline (MonoMethod *m, MonoMethodRuntimeGenericContext *mrgctx, gpointer addr)
625 guint8 *code, *start;
626 #ifdef USE_JUMP_TABLES
632 MonoDomain *domain = mono_domain_get ();
634 start = code = mono_domain_code_reserve (domain, buf_len);
636 #ifdef USE_JUMP_TABLES
637 jte = mono_jumptable_add_entries (2);
638 code = mono_arm_load_jumptable_entry_addr (code, jte, ARMREG_IP);
639 ARM_LDR_IMM (code, MONO_ARCH_RGCTX_REG, ARMREG_IP, 0);
640 ARM_LDR_IMM (code, ARMREG_IP, ARMREG_IP, 4);
641 ARM_BX (code, ARMREG_IP);
645 ARM_LDR_IMM (code, MONO_ARCH_RGCTX_REG, ARMREG_PC, 0);
646 ARM_LDR_IMM (code, ARMREG_PC, ARMREG_PC, 0);
647 *(guint32*)code = (guint32)mrgctx;
649 *(guint32*)code = (guint32)addr;
653 g_assert ((code - start) <= buf_len);
655 mono_arch_flush_icache (start, code - start);
661 mono_arch_create_rgctx_lazy_fetch_trampoline (guint32 slot, MonoTrampInfo **info, gboolean aot)
667 guint8 **rgctx_null_jumps;
671 MonoJumpInfo *ji = NULL;
672 GSList *unwind_ops = NULL;
673 #ifdef USE_JUMP_TABLES
677 mrgctx = MONO_RGCTX_SLOT_IS_MRGCTX (slot);
678 index = MONO_RGCTX_SLOT_INDEX (slot);
680 index += MONO_SIZEOF_METHOD_RUNTIME_GENERIC_CONTEXT / sizeof (gpointer);
681 for (depth = 0; ; ++depth) {
682 int size = mono_class_rgctx_get_array_size (depth, mrgctx);
684 if (index < size - 1)
689 tramp_size = 64 + 16 * depth;
691 code = buf = mono_global_codeman_reserve (tramp_size);
693 mono_add_unwind_op_def_cfa (unwind_ops, code, buf, ARMREG_SP, 0);
695 rgctx_null_jumps = g_malloc (sizeof (guint8*) * (depth + 2));
698 /* The vtable/mrgctx is in R0 */
699 g_assert (MONO_ARCH_VTABLE_REG == ARMREG_R0);
703 ARM_MOV_REG_REG (code, ARMREG_R1, ARMREG_R0);
705 /* load rgctx ptr from vtable */
706 g_assert (arm_is_imm12 (G_STRUCT_OFFSET (MonoVTable, runtime_generic_context)));
707 ARM_LDR_IMM (code, ARMREG_R1, ARMREG_R0, G_STRUCT_OFFSET (MonoVTable, runtime_generic_context));
708 /* is the rgctx ptr null? */
709 ARM_CMP_REG_IMM (code, ARMREG_R1, 0, 0);
710 /* if yes, jump to actual trampoline */
711 rgctx_null_jumps [njumps ++] = code;
712 ARM_B_COND (code, ARMCOND_EQ, 0);
715 for (i = 0; i < depth; ++i) {
716 /* load ptr to next array */
717 if (mrgctx && i == 0) {
718 g_assert (arm_is_imm12 (MONO_SIZEOF_METHOD_RUNTIME_GENERIC_CONTEXT));
719 ARM_LDR_IMM (code, ARMREG_R1, ARMREG_R1, MONO_SIZEOF_METHOD_RUNTIME_GENERIC_CONTEXT);
721 ARM_LDR_IMM (code, ARMREG_R1, ARMREG_R1, 0);
723 /* is the ptr null? */
724 ARM_CMP_REG_IMM (code, ARMREG_R1, 0, 0);
725 /* if yes, jump to actual trampoline */
726 rgctx_null_jumps [njumps ++] = code;
727 ARM_B_COND (code, ARMCOND_EQ, 0);
731 code = mono_arm_emit_load_imm (code, ARMREG_R2, sizeof (gpointer) * (index + 1));
732 ARM_LDR_REG_REG (code, ARMREG_R1, ARMREG_R1, ARMREG_R2);
733 /* is the slot null? */
734 ARM_CMP_REG_IMM (code, ARMREG_R1, 0, 0);
735 /* if yes, jump to actual trampoline */
736 rgctx_null_jumps [njumps ++] = code;
737 ARM_B_COND (code, ARMCOND_EQ, 0);
738 /* otherwise return, result is in R1 */
739 ARM_MOV_REG_REG (code, ARMREG_R0, ARMREG_R1);
740 code = emit_bx (code, ARMREG_LR);
742 g_assert (njumps <= depth + 2);
743 for (i = 0; i < njumps; ++i)
744 arm_patch (rgctx_null_jumps [i], code);
746 g_free (rgctx_null_jumps);
750 /* The vtable/mrgctx is still in R0 */
753 ji = mono_patch_info_list_prepend (ji, code - buf, MONO_PATCH_INFO_JIT_ICALL_ADDR, g_strdup_printf ("specific_trampoline_lazy_fetch_%u", slot));
754 ARM_LDR_IMM (code, ARMREG_R1, ARMREG_PC, 0);
756 *(gpointer*)code = NULL;
758 ARM_LDR_REG_REG (code, ARMREG_PC, ARMREG_PC, ARMREG_R1);
760 tramp = mono_arch_create_specific_trampoline (GUINT_TO_POINTER (slot), MONO_TRAMPOLINE_RGCTX_LAZY_FETCH, mono_get_root_domain (), &code_len);
762 /* Jump to the actual trampoline */
763 #ifdef USE_JUMP_TABLES
764 jte = mono_jumptable_add_entry ();
766 code = mono_arm_load_jumptable_entry (code, jte, ARMREG_R1);
767 code = emit_bx (code, ARMREG_R1);
769 ARM_LDR_IMM (code, ARMREG_R1, ARMREG_PC, 0); /* temp reg */
770 code = emit_bx (code, ARMREG_R1);
771 *(gpointer*)code = tramp;
776 mono_arch_flush_icache (buf, code - buf);
778 g_assert (code - buf <= tramp_size);
781 char *name = mono_get_rgctx_fetch_trampoline_name (slot);
782 *info = mono_tramp_info_create (name, buf, code - buf, ji, unwind_ops);
790 mono_arch_create_general_rgctx_lazy_fetch_trampoline (MonoTrampInfo **info, gboolean aot)
794 MonoJumpInfo *ji = NULL;
795 GSList *unwind_ops = NULL;
801 code = buf = mono_global_codeman_reserve (tramp_size);
803 mono_add_unwind_op_def_cfa (unwind_ops, code, buf, ARMREG_SP, 0);
805 // FIXME: Currently, we always go to the slow path.
806 /* Load trampoline addr */
807 ARM_LDR_IMM (code, ARMREG_R1, MONO_ARCH_RGCTX_REG, 4);
808 /* The vtable/mrgctx is in R0 */
809 g_assert (MONO_ARCH_VTABLE_REG == ARMREG_R0);
810 code = emit_bx (code, ARMREG_R1);
812 mono_arch_flush_icache (buf, code - buf);
814 g_assert (code - buf <= tramp_size);
817 *info = mono_tramp_info_create ("rgctx_fetch_trampoline_general", buf, code - buf, ji, unwind_ops);
822 #define arm_is_imm8(v) ((v) > -256 && (v) < 256)
825 mono_arch_create_generic_class_init_trampoline (MonoTrampInfo **info, gboolean aot)
829 static int byte_offset = -1;
830 static guint8 bitmask;
833 guint32 code_len, imm8;
835 GSList *unwind_ops = NULL;
836 MonoJumpInfo *ji = NULL;
840 code = buf = mono_global_codeman_reserve (tramp_size);
843 mono_marshal_find_bitfield_offset (MonoVTable, initialized, &byte_offset, &bitmask);
845 g_assert (arm_is_imm8 (byte_offset));
846 ARM_LDRSB_IMM (code, ARMREG_IP, MONO_ARCH_VTABLE_REG, byte_offset);
847 imm8 = mono_arm_is_rotated_imm8 (bitmask, &rot_amount);
848 g_assert (imm8 >= 0);
849 ARM_AND_REG_IMM (code, ARMREG_IP, ARMREG_IP, imm8, rot_amount);
850 ARM_CMP_REG_IMM (code, ARMREG_IP, 0, 0);
852 ARM_B_COND (code, ARMCOND_EQ, 0);
854 /* Initialized case */
855 ARM_MOV_REG_REG (code, ARMREG_PC, ARMREG_LR);
857 /* Uninitialized case */
858 arm_patch (jump, code);
861 ji = mono_patch_info_list_prepend (ji, code - buf, MONO_PATCH_INFO_JIT_ICALL_ADDR, "specific_trampoline_generic_class_init");
862 ARM_LDR_IMM (code, ARMREG_R1, ARMREG_PC, 0);
864 *(gpointer*)code = NULL;
866 ARM_LDR_REG_REG (code, ARMREG_PC, ARMREG_PC, ARMREG_R1);
868 #ifdef USE_JUMP_TABLES
869 gpointer *jte = mono_jumptable_add_entry ();
871 tramp = mono_arch_create_specific_trampoline (NULL, MONO_TRAMPOLINE_GENERIC_CLASS_INIT, mono_get_root_domain (), &code_len);
873 /* Jump to the actual trampoline */
874 #ifdef USE_JUMP_TABLES
875 code = mono_arm_load_jumptable_entry (code, jte, ARMREG_R1);
877 code = emit_bx (code, ARMREG_R1);
879 ARM_LDR_IMM (code, ARMREG_R1, ARMREG_PC, 0); /* temp reg */
880 code = emit_bx (code, ARMREG_R1);
881 *(gpointer*)code = tramp;
886 mono_arch_flush_icache (buf, code - buf);
888 g_assert (code - buf <= tramp_size);
891 *info = mono_tramp_info_create ("generic_class_init_trampoline", buf, code - buf, ji, unwind_ops);
899 mono_arch_create_generic_trampoline (MonoTrampolineType tramp_type, MonoTrampInfo **info, gboolean aot)
901 g_assert_not_reached ();
906 mono_arch_create_specific_trampoline (gpointer arg1, MonoTrampolineType tramp_type, MonoDomain *domain, guint32 *code_len)
908 g_assert_not_reached ();
913 mono_arch_get_unbox_trampoline (MonoMethod *m, gpointer addr)
915 g_assert_not_reached ();
920 mono_arch_get_static_rgctx_trampoline (MonoMethod *m, MonoMethodRuntimeGenericContext *mrgctx, gpointer addr)
922 g_assert_not_reached ();
927 mono_arch_create_rgctx_lazy_fetch_trampoline (guint32 slot, MonoTrampInfo **info, gboolean aot)
929 g_assert_not_reached ();
934 mono_arch_create_generic_class_init_trampoline (MonoTrampInfo **info, gboolean aot)
936 g_assert_not_reached ();
941 mono_arch_get_nullified_class_init_trampoline (MonoTrampInfo **info)
943 g_assert_not_reached ();
947 #endif /* DISABLE_JIT */
950 mono_arch_get_call_target (guint8 *code)
952 guint32 ins = ((guint32*)(gpointer)code) [-1];
955 /* Should be a 'bl' or a 'b' */
956 if (((ins >> 25) & 0x7) == 0x5) {
958 /* Should be a 'bl' */
959 if ((((ins >> 25) & 0x7) == 0x5) && (((ins >> 24) & 0x1) == 0x1)) {
961 gint32 disp = ((gint32)ins) & 0xffffff;
962 guint8 *target = code - 4 + 8 + (disp * 4);
971 mono_arch_get_plt_info_offset (guint8 *plt_entry, mgreg_t *regs, guint8 *code)
973 /* The offset is stored as the 4th word of the plt entry */
974 return ((guint32*)plt_entry) [3];
978 * Return the address of the PLT entry called by the thumb code CODE.
981 mono_arm_get_thumb_plt_entry (guint8 *code)
983 int s, j1, j2, imm10, imm11, i1, i2, imm32;
988 /* code should be right after a BL */
989 code = (guint8*)((mgreg_t)code & ~1);
990 base = (guint8*)((mgreg_t)code & ~3);
992 t1 = ((guint16*)bl) [0];
993 t2 = ((guint16*)bl) [1];
995 g_assert ((t1 >> 11) == 0x1e);
997 s = (t1 >> 10) & 0x1;
998 imm10 = (t1 >> 0) & 0x3ff;
999 j1 = (t2 >> 13) & 0x1;
1000 j2 = (t2 >> 11) & 0x1;
1003 i1 = (s ^ j1) ? 0 : 1;
1004 i2 = (s ^ j2) ? 0 : 1;
1006 imm32 = (imm11 << 1) | (imm10 << 12) | (i2 << 22) | (i1 << 23);
1010 target = code + imm32;
1012 /* target now points to the thumb plt entry */
1013 /* ldr.w r12, [pc, #8] */
1014 g_assert (((guint16*)target) [0] == 0xf8df);
1015 g_assert (((guint16*)target) [1] == 0xc008);
1018 * The PLT info offset is at offset 16, but mono_arch_get_plt_entry_offset () returns
1019 * the 3rd word, so compensate by returning a different value.
1029 * mono_arch_get_gsharedvt_arg_trampoline:
1031 * See tramp-x86.c for documentation.
1034 mono_arch_get_gsharedvt_arg_trampoline (MonoDomain *domain, gpointer arg, gpointer addr)
1036 guint8 *code, *start;
1038 gpointer *constants;
1042 start = code = mono_domain_code_reserve (domain, buf_len);
1044 /* Similar to the specialized trampoline code */
1045 ARM_PUSH (code, (1 << ARMREG_R0) | (1 << ARMREG_R1) | (1 << ARMREG_R2) | (1 << ARMREG_R3) | (1 << ARMREG_LR));
1046 ARM_LDR_IMM (code, ARMREG_IP, ARMREG_PC, 8);
1047 /* arg is passed in LR */
1048 ARM_LDR_IMM (code, ARMREG_LR, ARMREG_PC, 0);
1049 code = emit_bx (code, ARMREG_IP);
1050 constants = (gpointer*)code;
1051 constants [0] = arg;
1052 constants [1] = addr;
1055 g_assert ((code - start) <= buf_len);
1057 nacl_domain_code_validate (domain, &start, buf_len, &code);
1058 mono_arch_flush_icache (start, code - start);
1066 mono_arch_get_gsharedvt_arg_trampoline (MonoDomain *domain, gpointer arg, gpointer addr)
1068 g_assert_not_reached ();
1074 #if defined(ENABLE_GSHAREDVT)
1076 #include "../../../mono-extensions/mono/mini/tramp-arm-gsharedvt.c"
1081 mono_arm_start_gsharedvt_call (GSharedVtCallInfo *info, gpointer *caller, gpointer *callee, gpointer mrgctx_reg)
1083 g_assert_not_reached ();
1088 mono_arch_get_gsharedvt_trampoline (MonoTrampInfo **info, gboolean aot)
1095 #endif /* !MONOTOUCH */