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 static guint8* nullified_class_init_trampoline;
28 #ifdef USE_JUMP_TABLES
31 decode_imm16 (guint32 insn)
33 return (((insn >> 16) & 0xf) << 12) | (insn & 0xfff);
36 #define INSN_MASK 0xff00000
37 #define MOVW_MASK ((3 << 24) | (0 << 20))
38 #define MOVT_MASK ((3 << 24) | (4 << 20))
41 mono_arch_jumptable_entry_from_code (guint8 *code)
43 guint32 insn1 = ((guint32*)code) [0];
44 guint32 insn2 = ((guint32*)code) [1];
46 if (((insn1 & INSN_MASK) == MOVW_MASK) &&
47 ((insn2 & INSN_MASK) == MOVT_MASK) ) {
48 guint32 imm_lo = decode_imm16 (insn1);
49 guint32 imm_hi = decode_imm16 (insn2);
50 return (gpointer*) GUINT_TO_POINTER (imm_lo | (imm_hi << 16));
52 g_assert_not_reached ();
62 mono_arch_patch_callsite (guint8 *method_start, guint8 *code_ptr, guint8 *addr)
66 * code_ptr is 4 instructions after MOVW/MOVT used to address
69 jte = mono_jumptable_get_entry (code_ptr - 16);
70 g_assert ( jte != NULL);
75 mono_arch_patch_callsite (guint8 *method_start, guint8 *code_ptr, guint8 *addr)
77 guint32 *code = (guint32*)code_ptr;
79 /* This is the 'bl' or the 'mov pc' instruction */
83 * Note that methods are called also with the bl opcode.
85 if ((((*code) >> 25) & 7) == 5) {
86 /*g_print ("direct patching\n");*/
87 arm_patch ((guint8*)code, addr);
88 mono_arch_flush_icache ((guint8*)code, 4);
92 if ((((*code) >> 20) & 0xFF) == 0x12) {
93 /*g_print ("patching bx\n");*/
94 arm_patch ((guint8*)code, addr);
95 mono_arch_flush_icache ((guint8*)(code - 2), 4);
99 g_assert_not_reached ();
104 mono_arch_patch_plt_entry (guint8 *code, gpointer *got, mgreg_t *regs, guint8 *addr)
108 /* Patch the jump table entry used by the plt entry */
109 if (*(guint32*)code == 0xe59fc000) {
110 /* ARM_LDR_IMM (code, ARMREG_IP, ARMREG_PC, 0); */
111 guint32 offset = ((guint32*)code)[2];
113 jump_entry = code + offset + 12;
114 } else if (*(guint16*)(code - 4) == 0xf8df) {
116 * Thumb PLT entry, begins with ldr.w ip, [pc, #8], code points to entry + 4, see
117 * mono_arm_get_thumb_plt_entry ().
122 offset = *(guint32*)(code + 12);
123 jump_entry = code + offset + 8;
125 g_assert_not_reached ();
128 *(guint8**)jump_entry = addr;
132 mono_arch_nullify_class_init_trampoline (guint8 *code, mgreg_t *regs)
134 mono_arch_patch_callsite (NULL, code, nullified_class_init_trampoline);
138 mono_arch_nullify_plt_entry (guint8 *code, mgreg_t *regs)
140 if (mono_aot_only && !nullified_class_init_trampoline)
141 nullified_class_init_trampoline = mono_aot_get_trampoline ("nullified_class_init_trampoline");
143 mono_arch_patch_plt_entry (code, NULL, regs, nullified_class_init_trampoline);
148 #define arm_is_imm12(v) ((int)(v) > -4096 && (int)(v) < 4096)
150 #ifndef USE_JUMP_TABLES
152 * Return the instruction to jump from code to target, 0 if not
153 * reachable with a single instruction
156 branch_for_target_reachable (guint8 *branch, guint8 *target)
158 gint diff = target - branch - 8;
159 g_assert ((diff & 3) == 0);
161 if (diff <= 33554431)
162 return (ARMCOND_AL << ARMCOND_SHIFT) | (ARM_BR_TAG) | (diff >> 2);
164 /* diff between 0 and -33554432 */
165 if (diff >= -33554432)
166 return (ARMCOND_AL << ARMCOND_SHIFT) | (ARM_BR_TAG) | ((diff >> 2) & ~0xff000000);
172 static inline guint8*
173 emit_bx (guint8* code, int reg)
175 if (mono_arm_thumb_supported ())
178 ARM_MOV_REG_REG (code, ARMREG_PC, reg);
182 /* Stack size for trampoline function
184 #define STACK ALIGN_TO (sizeof (MonoLMF), 8)
186 /* Method-specific trampoline code fragment size */
187 #define METHOD_TRAMPOLINE_SIZE 64
189 /* Jump-specific trampoline code fragment size */
190 #define JUMP_TRAMPOLINE_SIZE 64
193 mono_arch_create_generic_trampoline (MonoTrampolineType tramp_type, MonoTrampInfo **info, gboolean aot)
195 guint8 *buf, *code = NULL;
196 #ifdef USE_JUMP_TABLES
197 gpointer *load_get_lmf_addr = NULL, *load_trampoline = NULL;
199 guint8 *load_get_lmf_addr = NULL, *load_trampoline = NULL;
203 int cfa_offset, lmf_offset, regsave_size, lr_offset;
204 GSList *unwind_ops = NULL;
205 MonoJumpInfo *ji = NULL;
208 #ifdef USE_JUMP_TABLES
212 /* Now we'll create in 'buf' the ARM trampoline code. This
213 is the trampoline code common to all methods */
216 code = buf = mono_global_codeman_reserve (buf_len);
219 * At this point lr points to the specific arg and sp points to the saved
220 * regs on the stack (all but PC and SP). The original LR value has been
221 * saved as sp + LR_OFFSET by the push in the specific trampoline
224 /* The offset of lmf inside the stack frame */
225 lmf_offset = STACK - sizeof (MonoLMF);
226 /* The size of the area already allocated by the push in the specific trampoline */
227 regsave_size = 14 * sizeof (mgreg_t);
228 /* The offset where lr was saved inside the regsave area */
229 lr_offset = 13 * sizeof (mgreg_t);
231 // FIXME: Finish the unwind info, the current info allows us to unwind
232 // when the trampoline is not in the epilog
234 // CFA = SP + (num registers pushed) * 4
235 cfa_offset = 14 * sizeof (mgreg_t);
236 mono_add_unwind_op_def_cfa (unwind_ops, code, buf, ARMREG_SP, cfa_offset);
237 // PC saved at sp+LR_OFFSET
238 mono_add_unwind_op_offset (unwind_ops, code, buf, ARMREG_LR, -4);
240 if (aot && tramp_type != MONO_TRAMPOLINE_GENERIC_CLASS_INIT) {
242 * For page trampolines the data is in r1, so just move it, otherwise use the got slot as below.
243 * The trampoline contains a pc-relative offset to the got slot
244 * preceeding the got slot where the value is stored. The offset can be
248 ARM_MOV_REG_REG (code, ARMREG_V2, ARMREG_R1);
250 ARM_LDR_IMM (code, ARMREG_V2, ARMREG_LR, 0);
251 ARM_ADD_REG_IMM (code, ARMREG_V2, ARMREG_V2, 4, 0);
252 ARM_LDR_REG_REG (code, ARMREG_V2, ARMREG_V2, ARMREG_LR);
255 if (tramp_type != MONO_TRAMPOLINE_GENERIC_CLASS_INIT) {
256 ARM_LDR_IMM (code, ARMREG_V2, ARMREG_LR, 0);
259 ARM_MOV_REG_REG (code, ARMREG_V2, MONO_ARCH_VTABLE_REG);
261 ARM_LDR_IMM (code, ARMREG_V3, ARMREG_SP, lr_offset);
263 /* ok, now we can continue with the MonoLMF setup, mostly untouched
264 * from emit_prolog in mini-arm.c
265 * This is a synthetized call to mono_get_lmf_addr ()
268 ji = mono_patch_info_list_prepend (ji, code - buf, MONO_PATCH_INFO_JIT_ICALL_ADDR, "mono_get_lmf_addr");
269 ARM_LDR_IMM (code, ARMREG_R0, ARMREG_PC, 0);
271 *(gpointer*)code = NULL;
273 ARM_LDR_REG_REG (code, ARMREG_R0, ARMREG_PC, ARMREG_R0);
275 #ifdef USE_JUMP_TABLES
276 load_get_lmf_addr = mono_jumptable_add_entry ();
277 code = mono_arm_load_jumptable_entry (code, load_get_lmf_addr, ARMREG_R0);
279 load_get_lmf_addr = code;
283 ARM_MOV_REG_REG (code, ARMREG_LR, ARMREG_PC);
284 code = emit_bx (code, ARMREG_R0);
286 /* we build the MonoLMF structure on the stack - see mini-arm.h
287 * The pointer to the struct is put in r1.
288 * the iregs array is already allocated on the stack by push.
290 ARM_SUB_REG_IMM8 (code, ARMREG_SP, ARMREG_SP, STACK - regsave_size);
291 cfa_offset += STACK - regsave_size;
292 mono_add_unwind_op_def_cfa_offset (unwind_ops, code, buf, cfa_offset);
294 ARM_ADD_REG_IMM8 (code, ARMREG_V1, ARMREG_SP, STACK - sizeof (MonoLMF));
297 * The stack now looks like:
299 * v1 -> <rest of LMF>
303 /* r0 is the result from mono_get_lmf_addr () */
304 ARM_STR_IMM (code, ARMREG_R0, ARMREG_V1, G_STRUCT_OFFSET (MonoLMF, lmf_addr));
305 /* new_lmf->previous_lmf = *lmf_addr */
306 ARM_LDR_IMM (code, ARMREG_R2, ARMREG_R0, G_STRUCT_OFFSET (MonoLMF, previous_lmf));
307 ARM_STR_IMM (code, ARMREG_R2, ARMREG_V1, G_STRUCT_OFFSET (MonoLMF, previous_lmf));
308 /* *(lmf_addr) = r1 */
309 ARM_STR_IMM (code, ARMREG_V1, ARMREG_R0, G_STRUCT_OFFSET (MonoLMF, previous_lmf));
310 /* save method info (it's in v2) */
311 if ((tramp_type == MONO_TRAMPOLINE_JIT) || (tramp_type == MONO_TRAMPOLINE_JUMP))
312 ARM_STR_IMM (code, ARMREG_V2, ARMREG_V1, G_STRUCT_OFFSET (MonoLMF, method));
314 ARM_MOV_REG_IMM8 (code, ARMREG_R2, 0);
315 ARM_STR_IMM (code, ARMREG_R2, ARMREG_V1, G_STRUCT_OFFSET (MonoLMF, method));
318 ARM_ADD_REG_IMM8 (code, ARMREG_R2, ARMREG_SP, cfa_offset);
319 ARM_STR_IMM (code, ARMREG_R2, ARMREG_V1, G_STRUCT_OFFSET (MonoLMF, sp));
321 ARM_LDR_IMM (code, ARMREG_R2, ARMREG_V1, (G_STRUCT_OFFSET (MonoLMF, iregs) + ARMREG_FP*4));
322 ARM_STR_IMM (code, ARMREG_R2, ARMREG_V1, G_STRUCT_OFFSET (MonoLMF, fp));
323 /* save the IP (caller ip) */
324 if (tramp_type == MONO_TRAMPOLINE_JUMP) {
325 ARM_MOV_REG_IMM8 (code, ARMREG_R2, 0);
327 ARM_LDR_IMM (code, ARMREG_R2, ARMREG_V1, (G_STRUCT_OFFSET (MonoLMF, iregs) + 13*4));
329 ARM_STR_IMM (code, ARMREG_R2, ARMREG_V1, G_STRUCT_OFFSET (MonoLMF, ip));
332 * Now we're ready to call xxx_trampoline ().
334 /* Arg 1: the saved registers */
335 ARM_ADD_REG_IMM8 (code, ARMREG_R0, ARMREG_V1, G_STRUCT_OFFSET (MonoLMF, iregs));
337 /* Arg 2: code (next address to the instruction that called us) */
338 if (tramp_type == MONO_TRAMPOLINE_JUMP) {
339 ARM_MOV_REG_IMM8 (code, ARMREG_R1, 0);
341 ARM_MOV_REG_REG (code, ARMREG_R1, ARMREG_V3);
344 /* Arg 3: the specific argument, stored in v2
346 ARM_MOV_REG_REG (code, ARMREG_R2, ARMREG_V2);
349 char *icall_name = g_strdup_printf ("trampoline_func_%d", tramp_type);
350 ji = mono_patch_info_list_prepend (ji, code - buf, MONO_PATCH_INFO_JIT_ICALL_ADDR, icall_name);
351 ARM_LDR_IMM (code, ARMREG_IP, ARMREG_PC, 0);
353 *(gpointer*)code = NULL;
355 ARM_LDR_REG_REG (code, ARMREG_IP, ARMREG_PC, ARMREG_IP);
357 #ifdef USE_JUMP_TABLES
358 load_trampoline = mono_jumptable_add_entry ();
359 code = mono_arm_load_jumptable_entry (code, load_trampoline, ARMREG_IP);
361 load_trampoline = code;
366 ARM_MOV_REG_REG (code, ARMREG_LR, ARMREG_PC);
367 code = emit_bx (code, ARMREG_IP);
369 /* OK, code address is now on r0. Move it to the place on the stack
370 * where IP was saved (it is now no more useful to us and it can be
371 * clobbered). This way we can just restore all the regs in one inst
374 ARM_STR_IMM (code, ARMREG_R0, ARMREG_V1, G_STRUCT_OFFSET (MonoLMF, iregs) + (ARMREG_R12 * sizeof (mgreg_t)));
376 /* Check for thread interruption */
377 /* This is not perf critical code so no need to check the interrupt flag */
379 * Have to call the _force_ variant, since there could be a protected wrapper on the top of the stack.
382 ji = mono_patch_info_list_prepend (ji, code - buf, MONO_PATCH_INFO_JIT_ICALL_ADDR, "mono_thread_force_interruption_checkpoint");
383 ARM_LDR_IMM (code, ARMREG_IP, ARMREG_PC, 0);
385 *(gpointer*)code = NULL;
387 ARM_LDR_REG_REG (code, ARMREG_IP, ARMREG_PC, ARMREG_IP);
389 #ifdef USE_JUMP_TABLES
390 gpointer *jte = mono_jumptable_add_entry ();
391 code = mono_arm_load_jumptable_entry (code, jte, ARMREG_IP);
392 jte [0] = mono_thread_force_interruption_checkpoint;
394 ARM_LDR_IMM (code, ARMREG_IP, ARMREG_PC, 0);
396 *(gpointer*)code = mono_thread_force_interruption_checkpoint;
400 ARM_MOV_REG_REG (code, ARMREG_LR, ARMREG_PC);
401 code = emit_bx (code, ARMREG_IP);
404 * Now we restore the MonoLMF (see emit_epilogue in mini-arm.c)
405 * and the rest of the registers, so the method called will see
406 * the same state as before we executed.
408 /* ip = previous_lmf */
409 ARM_LDR_IMM (code, ARMREG_IP, ARMREG_V1, G_STRUCT_OFFSET (MonoLMF, previous_lmf));
411 ARM_LDR_IMM (code, ARMREG_LR, ARMREG_V1, G_STRUCT_OFFSET (MonoLMF, lmf_addr));
412 /* *(lmf_addr) = previous_lmf */
413 ARM_STR_IMM (code, ARMREG_IP, ARMREG_LR, G_STRUCT_OFFSET (MonoLMF, previous_lmf));
415 /* Non-standard function epilogue. Instead of doing a proper
416 * return, we just jump to the compiled code.
418 /* Restore the registers and jump to the code:
419 * Note that IP has been conveniently set to the method addr.
421 ARM_ADD_REG_IMM8 (code, ARMREG_SP, ARMREG_SP, STACK - regsave_size);
422 ARM_POP_NWB (code, 0x5fff);
423 if (tramp_type == MONO_TRAMPOLINE_RGCTX_LAZY_FETCH)
424 ARM_MOV_REG_REG (code, ARMREG_R0, ARMREG_IP);
425 ARM_ADD_REG_IMM8 (code, ARMREG_SP, ARMREG_SP, regsave_size);
426 if ((tramp_type == MONO_TRAMPOLINE_CLASS_INIT) || (tramp_type == MONO_TRAMPOLINE_GENERIC_CLASS_INIT) || (tramp_type == MONO_TRAMPOLINE_RGCTX_LAZY_FETCH))
427 code = emit_bx (code, ARMREG_LR);
429 code = emit_bx (code, ARMREG_IP);
431 #ifdef USE_JUMP_TABLES
432 load_get_lmf_addr [0] = mono_get_lmf_addr;
433 load_trampoline [0] = (gpointer)mono_get_trampoline_func (tramp_type);
435 constants = (gpointer*)code;
436 constants [0] = mono_get_lmf_addr;
437 constants [1] = (gpointer)mono_get_trampoline_func (tramp_type);
440 /* backpatch by emitting the missing instructions skipped above */
441 ARM_LDR_IMM (load_get_lmf_addr, ARMREG_R0, ARMREG_PC, (code - load_get_lmf_addr - 8));
442 ARM_LDR_IMM (load_trampoline, ARMREG_IP, ARMREG_PC, (code + 4 - load_trampoline - 8));
448 /* Flush instruction cache, since we've generated code */
449 mono_arch_flush_icache (buf, code - buf);
452 g_assert ((code - buf) <= buf_len);
454 if (tramp_type == MONO_TRAMPOLINE_CLASS_INIT)
455 /* Initialize the nullified class init trampoline used in the AOT case */
456 nullified_class_init_trampoline = mono_arch_get_nullified_class_init_trampoline (NULL);
459 *info = mono_tramp_info_create (mono_get_generic_trampoline_name (tramp_type), buf, code - buf, ji, unwind_ops);
465 mono_arch_get_nullified_class_init_trampoline (MonoTrampInfo **info)
469 code = buf = mono_global_codeman_reserve (16);
471 code = emit_bx (code, ARMREG_LR);
473 mono_arch_flush_icache (buf, code - buf);
476 *info = mono_tramp_info_create ("nullified_class_init_trampoline", buf, code - buf, NULL, NULL);
481 #define SPEC_TRAMP_SIZE 24
484 mono_arch_create_specific_trampoline (gpointer arg1, MonoTrampolineType tramp_type, MonoDomain *domain, guint32 *code_len)
486 guint8 *code, *buf, *tramp;
488 #ifndef USE_JUMP_TABLES
489 guint32 short_branch;
491 guint32 size = SPEC_TRAMP_SIZE;
493 tramp = mono_get_trampoline_code (tramp_type);
495 mono_domain_lock (domain);
496 #ifdef USE_JUMP_TABLES
497 code = buf = mono_domain_code_reserve_align (domain, size, 4);
499 code = buf = mono_domain_code_reserve_align (domain, size, 4);
500 if ((short_branch = branch_for_target_reachable (code + 4, tramp))) {
502 mono_domain_code_commit (domain, code, SPEC_TRAMP_SIZE, size);
505 mono_domain_unlock (domain);
507 #ifdef USE_JUMP_TABLES
508 /* For jumptables case we always generate the same code for trampolines,
510 * push {r0, r1, r2, r3, r4, r5, r6, r7, r8, r9, r10, r11, r12, lr}
516 ARM_PUSH (code, 0x5fff);
517 constants = mono_jumptable_add_entries (2);
518 code = mono_arm_load_jumptable_entry_addr (code, constants, ARMREG_LR);
519 ARM_LDR_IMM (code, ARMREG_R1, ARMREG_LR, 4);
520 code = emit_bx (code, ARMREG_R1);
521 constants [0] = arg1;
522 constants [1] = tramp;
524 /* we could reduce this to 12 bytes if tramp is within reach:
528 * The called code can access method using the lr register
529 * A 20 byte sequence could be:
531 * ARM_MOV_REG_REG (lr, pc)
532 * ARM_LDR_IMM (pc, pc, 0)
536 /* We save all the registers, except PC and SP */
537 ARM_PUSH (code, 0x5fff);
539 constants = (gpointer*)code;
540 constants [0] = GUINT_TO_POINTER (short_branch | (1 << 24));
541 constants [1] = arg1;
544 ARM_LDR_IMM (code, ARMREG_R1, ARMREG_PC, 8); /* temp reg */
545 ARM_MOV_REG_REG (code, ARMREG_LR, ARMREG_PC);
546 code = emit_bx (code, ARMREG_R1);
548 constants = (gpointer*)code;
549 constants [0] = arg1;
550 constants [1] = tramp;
555 /* Flush instruction cache, since we've generated code */
556 mono_arch_flush_icache (buf, code - buf);
558 g_assert ((code - buf) <= size);
561 *code_len = code - buf;
567 * mono_arch_get_unbox_trampoline:
569 * @addr: pointer to native code for @m
571 * when value type methods are called through the vtable we need to unbox the
572 * this argument. This method returns a pointer to a trampoline which does
573 * unboxing before calling the method
576 mono_arch_get_unbox_trampoline (MonoMethod *m, gpointer addr)
578 guint8 *code, *start;
579 MonoDomain *domain = mono_domain_get ();
580 #ifdef USE_JUMP_TABLES
587 start = code = mono_domain_code_reserve (domain, size);
589 #ifdef USE_JUMP_TABLES
590 jte = mono_jumptable_add_entry ();
591 code = mono_arm_load_jumptable_entry (code, jte, ARMREG_IP);
592 ARM_ADD_REG_IMM8 (code, ARMREG_R0, ARMREG_R0, sizeof (MonoObject));
593 code = emit_bx (code, ARMREG_IP);
596 ARM_LDR_IMM (code, ARMREG_IP, ARMREG_PC, 4);
597 ARM_ADD_REG_IMM8 (code, ARMREG_R0, ARMREG_R0, sizeof (MonoObject));
598 code = emit_bx (code, ARMREG_IP);
599 *(guint32*)code = (guint32)addr;
602 mono_arch_flush_icache (start, code - start);
603 g_assert ((code - start) <= size);
604 /*g_print ("unbox trampoline at %d for %s:%s\n", this_pos, m->klass->name, m->name);
605 g_print ("unbox code is at %p for method at %p\n", start, addr);*/
611 mono_arch_get_static_rgctx_trampoline (MonoMethod *m, MonoMethodRuntimeGenericContext *mrgctx, gpointer addr)
613 guint8 *code, *start;
614 #ifdef USE_JUMP_TABLES
620 MonoDomain *domain = mono_domain_get ();
622 start = code = mono_domain_code_reserve (domain, buf_len);
624 #ifdef USE_JUMP_TABLES
625 jte = mono_jumptable_add_entries (2);
626 code = mono_arm_load_jumptable_entry_addr (code, jte, ARMREG_IP);
627 ARM_LDR_IMM (code, MONO_ARCH_RGCTX_REG, ARMREG_IP, 0);
628 ARM_LDR_IMM (code, ARMREG_IP, ARMREG_IP, 4);
629 ARM_BX (code, ARMREG_IP);
633 ARM_LDR_IMM (code, MONO_ARCH_RGCTX_REG, ARMREG_PC, 0);
634 ARM_LDR_IMM (code, ARMREG_PC, ARMREG_PC, 0);
635 *(guint32*)code = (guint32)mrgctx;
637 *(guint32*)code = (guint32)addr;
641 g_assert ((code - start) <= buf_len);
643 mono_arch_flush_icache (start, code - start);
649 mono_arch_create_rgctx_lazy_fetch_trampoline (guint32 slot, MonoTrampInfo **info, gboolean aot)
655 guint8 **rgctx_null_jumps;
659 MonoJumpInfo *ji = NULL;
660 GSList *unwind_ops = NULL;
661 #ifdef USE_JUMP_TABLES
665 mrgctx = MONO_RGCTX_SLOT_IS_MRGCTX (slot);
666 index = MONO_RGCTX_SLOT_INDEX (slot);
668 index += MONO_SIZEOF_METHOD_RUNTIME_GENERIC_CONTEXT / sizeof (gpointer);
669 for (depth = 0; ; ++depth) {
670 int size = mono_class_rgctx_get_array_size (depth, mrgctx);
672 if (index < size - 1)
677 tramp_size = 64 + 16 * depth;
679 code = buf = mono_global_codeman_reserve (tramp_size);
681 mono_add_unwind_op_def_cfa (unwind_ops, code, buf, ARMREG_SP, 0);
683 rgctx_null_jumps = g_malloc (sizeof (guint8*) * (depth + 2));
686 /* The vtable/mrgctx is in R0 */
687 g_assert (MONO_ARCH_VTABLE_REG == ARMREG_R0);
691 ARM_MOV_REG_REG (code, ARMREG_R1, ARMREG_R0);
693 /* load rgctx ptr from vtable */
694 g_assert (arm_is_imm12 (G_STRUCT_OFFSET (MonoVTable, runtime_generic_context)));
695 ARM_LDR_IMM (code, ARMREG_R1, ARMREG_R0, G_STRUCT_OFFSET (MonoVTable, runtime_generic_context));
696 /* is the rgctx ptr null? */
697 ARM_CMP_REG_IMM (code, ARMREG_R1, 0, 0);
698 /* if yes, jump to actual trampoline */
699 rgctx_null_jumps [njumps ++] = code;
700 ARM_B_COND (code, ARMCOND_EQ, 0);
703 for (i = 0; i < depth; ++i) {
704 /* load ptr to next array */
705 if (mrgctx && i == 0) {
706 g_assert (arm_is_imm12 (MONO_SIZEOF_METHOD_RUNTIME_GENERIC_CONTEXT));
707 ARM_LDR_IMM (code, ARMREG_R1, ARMREG_R1, MONO_SIZEOF_METHOD_RUNTIME_GENERIC_CONTEXT);
709 ARM_LDR_IMM (code, ARMREG_R1, ARMREG_R1, 0);
711 /* is the ptr null? */
712 ARM_CMP_REG_IMM (code, ARMREG_R1, 0, 0);
713 /* if yes, jump to actual trampoline */
714 rgctx_null_jumps [njumps ++] = code;
715 ARM_B_COND (code, ARMCOND_EQ, 0);
719 code = mono_arm_emit_load_imm (code, ARMREG_R2, sizeof (gpointer) * (index + 1));
720 ARM_LDR_REG_REG (code, ARMREG_R1, ARMREG_R1, ARMREG_R2);
721 /* is the slot null? */
722 ARM_CMP_REG_IMM (code, ARMREG_R1, 0, 0);
723 /* if yes, jump to actual trampoline */
724 rgctx_null_jumps [njumps ++] = code;
725 ARM_B_COND (code, ARMCOND_EQ, 0);
726 /* otherwise return, result is in R1 */
727 ARM_MOV_REG_REG (code, ARMREG_R0, ARMREG_R1);
728 code = emit_bx (code, ARMREG_LR);
730 g_assert (njumps <= depth + 2);
731 for (i = 0; i < njumps; ++i)
732 arm_patch (rgctx_null_jumps [i], code);
734 g_free (rgctx_null_jumps);
738 /* The vtable/mrgctx is still in R0 */
741 ji = mono_patch_info_list_prepend (ji, code - buf, MONO_PATCH_INFO_JIT_ICALL_ADDR, g_strdup_printf ("specific_trampoline_lazy_fetch_%u", slot));
742 ARM_LDR_IMM (code, ARMREG_R1, ARMREG_PC, 0);
744 *(gpointer*)code = NULL;
746 ARM_LDR_REG_REG (code, ARMREG_PC, ARMREG_PC, ARMREG_R1);
748 tramp = mono_arch_create_specific_trampoline (GUINT_TO_POINTER (slot), MONO_TRAMPOLINE_RGCTX_LAZY_FETCH, mono_get_root_domain (), &code_len);
750 /* Jump to the actual trampoline */
751 #ifdef USE_JUMP_TABLES
752 jte = mono_jumptable_add_entry ();
754 code = mono_arm_load_jumptable_entry (code, jte, ARMREG_R1);
755 code = emit_bx (code, ARMREG_R1);
757 ARM_LDR_IMM (code, ARMREG_R1, ARMREG_PC, 0); /* temp reg */
758 code = emit_bx (code, ARMREG_R1);
759 *(gpointer*)code = tramp;
764 mono_arch_flush_icache (buf, code - buf);
766 g_assert (code - buf <= tramp_size);
769 char *name = mono_get_rgctx_fetch_trampoline_name (slot);
770 *info = mono_tramp_info_create (name, buf, code - buf, ji, unwind_ops);
778 mono_arch_create_general_rgctx_lazy_fetch_trampoline (MonoTrampInfo **info, gboolean aot)
782 MonoJumpInfo *ji = NULL;
783 GSList *unwind_ops = NULL;
789 code = buf = mono_global_codeman_reserve (tramp_size);
791 mono_add_unwind_op_def_cfa (unwind_ops, code, buf, ARMREG_SP, 0);
793 // FIXME: Currently, we always go to the slow path.
794 /* Load trampoline addr */
795 ARM_LDR_IMM (code, ARMREG_R1, MONO_ARCH_RGCTX_REG, 4);
796 /* The vtable/mrgctx is in R0 */
797 g_assert (MONO_ARCH_VTABLE_REG == ARMREG_R0);
798 code = emit_bx (code, ARMREG_R1);
800 mono_arch_flush_icache (buf, code - buf);
802 g_assert (code - buf <= tramp_size);
805 *info = mono_tramp_info_create ("rgctx_fetch_trampoline_general", buf, code - buf, ji, unwind_ops);
810 #define arm_is_imm8(v) ((v) > -256 && (v) < 256)
813 mono_arch_create_generic_class_init_trampoline (MonoTrampInfo **info, gboolean aot)
817 static int byte_offset = -1;
818 static guint8 bitmask;
821 guint32 code_len, imm8;
823 GSList *unwind_ops = NULL;
824 MonoJumpInfo *ji = NULL;
828 code = buf = mono_global_codeman_reserve (tramp_size);
831 mono_marshal_find_bitfield_offset (MonoVTable, initialized, &byte_offset, &bitmask);
833 g_assert (arm_is_imm8 (byte_offset));
834 ARM_LDRSB_IMM (code, ARMREG_IP, MONO_ARCH_VTABLE_REG, byte_offset);
835 imm8 = mono_arm_is_rotated_imm8 (bitmask, &rot_amount);
836 g_assert (imm8 >= 0);
837 ARM_AND_REG_IMM (code, ARMREG_IP, ARMREG_IP, imm8, rot_amount);
838 ARM_CMP_REG_IMM (code, ARMREG_IP, 0, 0);
840 ARM_B_COND (code, ARMCOND_EQ, 0);
842 /* Initialized case */
843 ARM_MOV_REG_REG (code, ARMREG_PC, ARMREG_LR);
845 /* Uninitialized case */
846 arm_patch (jump, code);
849 ji = mono_patch_info_list_prepend (ji, code - buf, MONO_PATCH_INFO_JIT_ICALL_ADDR, "specific_trampoline_generic_class_init");
850 ARM_LDR_IMM (code, ARMREG_R1, ARMREG_PC, 0);
852 *(gpointer*)code = NULL;
854 ARM_LDR_REG_REG (code, ARMREG_PC, ARMREG_PC, ARMREG_R1);
856 #ifdef USE_JUMP_TABLES
857 gpointer *jte = mono_jumptable_add_entry ();
859 tramp = mono_arch_create_specific_trampoline (NULL, MONO_TRAMPOLINE_GENERIC_CLASS_INIT, mono_get_root_domain (), &code_len);
861 /* Jump to the actual trampoline */
862 #ifdef USE_JUMP_TABLES
863 code = mono_arm_load_jumptable_entry (code, jte, ARMREG_R1);
865 code = emit_bx (code, ARMREG_R1);
867 ARM_LDR_IMM (code, ARMREG_R1, ARMREG_PC, 0); /* temp reg */
868 code = emit_bx (code, ARMREG_R1);
869 *(gpointer*)code = tramp;
874 mono_arch_flush_icache (buf, code - buf);
876 g_assert (code - buf <= tramp_size);
879 *info = mono_tramp_info_create ("generic_class_init_trampoline", buf, code - buf, ji, unwind_ops);
887 mono_arch_create_generic_trampoline (MonoTrampolineType tramp_type, MonoTrampInfo **info, gboolean aot)
889 g_assert_not_reached ();
894 mono_arch_create_specific_trampoline (gpointer arg1, MonoTrampolineType tramp_type, MonoDomain *domain, guint32 *code_len)
896 g_assert_not_reached ();
901 mono_arch_get_unbox_trampoline (MonoMethod *m, gpointer addr)
903 g_assert_not_reached ();
908 mono_arch_get_static_rgctx_trampoline (MonoMethod *m, MonoMethodRuntimeGenericContext *mrgctx, gpointer addr)
910 g_assert_not_reached ();
915 mono_arch_create_rgctx_lazy_fetch_trampoline (guint32 slot, MonoTrampInfo **info, gboolean aot)
917 g_assert_not_reached ();
922 mono_arch_create_generic_class_init_trampoline (MonoTrampInfo **info, gboolean aot)
924 g_assert_not_reached ();
928 #endif /* DISABLE_JIT */
931 mono_arch_get_call_target (guint8 *code)
933 guint32 ins = ((guint32*)(gpointer)code) [-1];
936 /* Should be a 'bl' or a 'b' */
937 if (((ins >> 25) & 0x7) == 0x5) {
939 /* Should be a 'bl' */
940 if ((((ins >> 25) & 0x7) == 0x5) && (((ins >> 24) & 0x1) == 0x1)) {
942 gint32 disp = ((gint32)ins) & 0xffffff;
943 guint8 *target = code - 4 + 8 + (disp * 4);
952 mono_arch_get_plt_info_offset (guint8 *plt_entry, mgreg_t *regs, guint8 *code)
954 /* The offset is stored as the 4th word of the plt entry */
955 return ((guint32*)plt_entry) [3];
959 * Return the address of the PLT entry called by the thumb code CODE.
962 mono_arm_get_thumb_plt_entry (guint8 *code)
964 int s, j1, j2, imm10, imm11, i1, i2, imm32;
969 /* code should be right after a BL */
970 code = (guint8*)((mgreg_t)code & ~1);
971 base = (guint8*)((mgreg_t)code & ~3);
973 t1 = ((guint16*)bl) [0];
974 t2 = ((guint16*)bl) [1];
976 g_assert ((t1 >> 11) == 0x1e);
978 s = (t1 >> 10) & 0x1;
979 imm10 = (t1 >> 0) & 0x3ff;
980 j1 = (t2 >> 13) & 0x1;
981 j2 = (t2 >> 11) & 0x1;
984 i1 = (s ^ j1) ? 0 : 1;
985 i2 = (s ^ j2) ? 0 : 1;
987 imm32 = (imm11 << 1) | (imm10 << 12) | (i2 << 22) | (i1 << 23);
991 target = code + imm32;
993 /* target now points to the thumb plt entry */
994 /* ldr.w r12, [pc, #8] */
995 g_assert (((guint16*)target) [0] == 0xf8df);
996 g_assert (((guint16*)target) [1] == 0xc008);
999 * The PLT info offset is at offset 16, but mono_arch_get_plt_entry_offset () returns
1000 * the 3rd word, so compensate by returning a different value.
1010 * mono_arch_get_gsharedvt_arg_trampoline:
1012 * See tramp-x86.c for documentation.
1015 mono_arch_get_gsharedvt_arg_trampoline (MonoDomain *domain, gpointer arg, gpointer addr)
1017 guint8 *code, *start;
1019 gpointer *constants;
1023 start = code = mono_domain_code_reserve (domain, buf_len);
1025 /* Similar to the specialized trampoline code */
1026 ARM_PUSH (code, (1 << ARMREG_R0) | (1 << ARMREG_R1) | (1 << ARMREG_R2) | (1 << ARMREG_R3) | (1 << ARMREG_LR));
1027 ARM_LDR_IMM (code, ARMREG_IP, ARMREG_PC, 8);
1028 /* arg is passed in LR */
1029 ARM_LDR_IMM (code, ARMREG_LR, ARMREG_PC, 0);
1030 code = emit_bx (code, ARMREG_IP);
1031 constants = (gpointer*)code;
1032 constants [0] = arg;
1033 constants [1] = addr;
1036 g_assert ((code - start) <= buf_len);
1038 nacl_domain_code_validate (domain, &start, buf_len, &code);
1039 mono_arch_flush_icache (start, code - start);
1047 mono_arch_get_gsharedvt_arg_trampoline (MonoDomain *domain, gpointer arg, gpointer addr)
1049 g_assert_not_reached ();
1055 #if defined(MONOTOUCH) || defined(MONO_EXTENSIONS)
1057 #include "../../../mono-extensions/mono/mini/tramp-arm-gsharedvt.c"
1062 mono_arm_start_gsharedvt_call (GSharedVtCallInfo *info, gpointer *caller, gpointer *callee, gpointer mrgctx_reg)
1064 g_assert_not_reached ();
1069 mono_arch_get_gsharedvt_trampoline (MonoTrampInfo **info, gboolean aot)
1076 #endif /* !MONOTOUCH */