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)
196 guint8 *buf, *code = NULL;
197 #ifdef USE_JUMP_TABLES
198 gpointer *load_get_lmf_addr = NULL, *load_trampoline = NULL;
200 guint8 *load_get_lmf_addr = NULL, *load_trampoline = NULL;
204 int cfa_offset, lmf_offset, regsave_size, lr_offset;
205 GSList *unwind_ops = NULL;
206 MonoJumpInfo *ji = NULL;
209 #ifdef USE_JUMP_TABLES
213 /* Now we'll create in 'buf' the ARM trampoline code. This
214 is the trampoline code common to all methods */
217 code = buf = mono_global_codeman_reserve (buf_len);
220 * At this point lr points to the specific arg and sp points to the saved
221 * regs on the stack (all but PC and SP). The original LR value has been
222 * saved as sp + LR_OFFSET by the push in the specific trampoline
225 /* The offset of lmf inside the stack frame */
226 lmf_offset = STACK - sizeof (MonoLMF);
227 /* The size of the area already allocated by the push in the specific trampoline */
228 regsave_size = 14 * sizeof (mgreg_t);
229 /* The offset where lr was saved inside the regsave area */
230 lr_offset = 13 * sizeof (mgreg_t);
232 // FIXME: Finish the unwind info, the current info allows us to unwind
233 // when the trampoline is not in the epilog
235 // CFA = SP + (num registers pushed) * 4
236 cfa_offset = 14 * sizeof (mgreg_t);
237 mono_add_unwind_op_def_cfa (unwind_ops, code, buf, ARMREG_SP, cfa_offset);
238 // PC saved at sp+LR_OFFSET
239 mono_add_unwind_op_offset (unwind_ops, code, buf, ARMREG_LR, -4);
241 if (aot && tramp_type != MONO_TRAMPOLINE_GENERIC_CLASS_INIT) {
243 * For page trampolines the data is in r1, so just move it, otherwise use the got slot as below.
244 * The trampoline contains a pc-relative offset to the got slot
245 * preceeding the got slot where the value is stored. The offset can be
249 ARM_MOV_REG_REG (code, ARMREG_V2, ARMREG_R1);
251 ARM_LDR_IMM (code, ARMREG_V2, ARMREG_LR, 0);
252 ARM_ADD_REG_IMM (code, ARMREG_V2, ARMREG_V2, 4, 0);
253 ARM_LDR_REG_REG (code, ARMREG_V2, ARMREG_V2, ARMREG_LR);
256 if (tramp_type != MONO_TRAMPOLINE_GENERIC_CLASS_INIT) {
257 ARM_LDR_IMM (code, ARMREG_V2, ARMREG_LR, 0);
260 ARM_MOV_REG_REG (code, ARMREG_V2, MONO_ARCH_VTABLE_REG);
262 ARM_LDR_IMM (code, ARMREG_V3, ARMREG_SP, lr_offset);
264 /* ok, now we can continue with the MonoLMF setup, mostly untouched
265 * from emit_prolog in mini-arm.c
266 * This is a synthetized call to mono_get_lmf_addr ()
269 ji = mono_patch_info_list_prepend (ji, code - buf, MONO_PATCH_INFO_JIT_ICALL_ADDR, "mono_get_lmf_addr");
270 ARM_LDR_IMM (code, ARMREG_R0, ARMREG_PC, 0);
272 *(gpointer*)code = NULL;
274 ARM_LDR_REG_REG (code, ARMREG_R0, ARMREG_PC, ARMREG_R0);
276 #ifdef USE_JUMP_TABLES
277 load_get_lmf_addr = mono_jumptable_add_entry ();
278 code = mono_arm_load_jumptable_entry (code, load_get_lmf_addr, ARMREG_R0);
280 load_get_lmf_addr = code;
284 ARM_MOV_REG_REG (code, ARMREG_LR, ARMREG_PC);
285 code = emit_bx (code, ARMREG_R0);
287 /* we build the MonoLMF structure on the stack - see mini-arm.h
288 * The pointer to the struct is put in r1.
289 * the iregs array is already allocated on the stack by push.
291 ARM_SUB_REG_IMM8 (code, ARMREG_SP, ARMREG_SP, STACK - regsave_size);
292 cfa_offset += STACK - regsave_size;
293 mono_add_unwind_op_def_cfa_offset (unwind_ops, code, buf, cfa_offset);
295 ARM_ADD_REG_IMM8 (code, ARMREG_V1, ARMREG_SP, STACK - sizeof (MonoLMF));
298 * The stack now looks like:
300 * v1 -> <rest of LMF>
304 /* r0 is the result from mono_get_lmf_addr () */
305 ARM_STR_IMM (code, ARMREG_R0, ARMREG_V1, G_STRUCT_OFFSET (MonoLMF, lmf_addr));
306 /* new_lmf->previous_lmf = *lmf_addr */
307 ARM_LDR_IMM (code, ARMREG_R2, ARMREG_R0, G_STRUCT_OFFSET (MonoLMF, previous_lmf));
308 ARM_STR_IMM (code, ARMREG_R2, ARMREG_V1, G_STRUCT_OFFSET (MonoLMF, previous_lmf));
309 /* *(lmf_addr) = r1 */
310 ARM_STR_IMM (code, ARMREG_V1, ARMREG_R0, G_STRUCT_OFFSET (MonoLMF, previous_lmf));
311 /* save method info (it's in v2) */
312 if ((tramp_type == MONO_TRAMPOLINE_JIT) || (tramp_type == MONO_TRAMPOLINE_JUMP))
313 ARM_STR_IMM (code, ARMREG_V2, ARMREG_V1, G_STRUCT_OFFSET (MonoLMF, method));
315 ARM_MOV_REG_IMM8 (code, ARMREG_R2, 0);
316 ARM_STR_IMM (code, ARMREG_R2, ARMREG_V1, G_STRUCT_OFFSET (MonoLMF, method));
319 ARM_ADD_REG_IMM8 (code, ARMREG_R2, ARMREG_SP, cfa_offset);
320 ARM_STR_IMM (code, ARMREG_R2, ARMREG_V1, G_STRUCT_OFFSET (MonoLMF, sp));
322 ARM_LDR_IMM (code, ARMREG_R2, ARMREG_V1, (G_STRUCT_OFFSET (MonoLMF, iregs) + ARMREG_FP*4));
323 ARM_STR_IMM (code, ARMREG_R2, ARMREG_V1, G_STRUCT_OFFSET (MonoLMF, fp));
324 /* save the IP (caller ip) */
325 if (tramp_type == MONO_TRAMPOLINE_JUMP) {
326 ARM_MOV_REG_IMM8 (code, ARMREG_R2, 0);
328 ARM_LDR_IMM (code, ARMREG_R2, ARMREG_V1, (G_STRUCT_OFFSET (MonoLMF, iregs) + 13*4));
330 ARM_STR_IMM (code, ARMREG_R2, ARMREG_V1, G_STRUCT_OFFSET (MonoLMF, ip));
333 * Now we're ready to call xxx_trampoline ().
335 /* Arg 1: the saved registers */
336 ARM_ADD_REG_IMM8 (code, ARMREG_R0, ARMREG_V1, G_STRUCT_OFFSET (MonoLMF, iregs));
338 /* Arg 2: code (next address to the instruction that called us) */
339 if (tramp_type == MONO_TRAMPOLINE_JUMP) {
340 ARM_MOV_REG_IMM8 (code, ARMREG_R1, 0);
342 ARM_MOV_REG_REG (code, ARMREG_R1, ARMREG_V3);
345 /* Arg 3: the specific argument, stored in v2
347 ARM_MOV_REG_REG (code, ARMREG_R2, ARMREG_V2);
350 char *icall_name = g_strdup_printf ("trampoline_func_%d", tramp_type);
351 ji = mono_patch_info_list_prepend (ji, code - buf, MONO_PATCH_INFO_JIT_ICALL_ADDR, icall_name);
352 ARM_LDR_IMM (code, ARMREG_IP, ARMREG_PC, 0);
354 *(gpointer*)code = NULL;
356 ARM_LDR_REG_REG (code, ARMREG_IP, ARMREG_PC, ARMREG_IP);
358 #ifdef USE_JUMP_TABLES
359 load_trampoline = mono_jumptable_add_entry ();
360 code = mono_arm_load_jumptable_entry (code, load_trampoline, ARMREG_IP);
362 load_trampoline = code;
367 ARM_MOV_REG_REG (code, ARMREG_LR, ARMREG_PC);
368 code = emit_bx (code, ARMREG_IP);
370 /* OK, code address is now on r0. Move it to the place on the stack
371 * where IP was saved (it is now no more useful to us and it can be
372 * clobbered). This way we can just restore all the regs in one inst
375 ARM_STR_IMM (code, ARMREG_R0, ARMREG_V1, G_STRUCT_OFFSET (MonoLMF, iregs) + (ARMREG_R12 * sizeof (mgreg_t)));
377 /* Check for thread interruption */
378 /* This is not perf critical code so no need to check the interrupt flag */
380 * Have to call the _force_ variant, since there could be a protected wrapper on the top of the stack.
383 ji = mono_patch_info_list_prepend (ji, code - buf, MONO_PATCH_INFO_JIT_ICALL_ADDR, "mono_thread_force_interruption_checkpoint");
384 ARM_LDR_IMM (code, ARMREG_IP, ARMREG_PC, 0);
386 *(gpointer*)code = NULL;
388 ARM_LDR_REG_REG (code, ARMREG_IP, ARMREG_PC, ARMREG_IP);
390 #ifdef USE_JUMP_TABLES
391 gpointer *jte = mono_jumptable_add_entry ();
392 code = mono_arm_load_jumptable_entry (code, jte, ARMREG_IP);
393 jte [0] = mono_thread_force_interruption_checkpoint;
395 ARM_LDR_IMM (code, ARMREG_IP, ARMREG_PC, 0);
397 *(gpointer*)code = mono_thread_force_interruption_checkpoint;
401 ARM_MOV_REG_REG (code, ARMREG_LR, ARMREG_PC);
402 code = emit_bx (code, ARMREG_IP);
405 * Now we restore the MonoLMF (see emit_epilogue in mini-arm.c)
406 * and the rest of the registers, so the method called will see
407 * the same state as before we executed.
409 /* ip = previous_lmf */
410 ARM_LDR_IMM (code, ARMREG_IP, ARMREG_V1, G_STRUCT_OFFSET (MonoLMF, previous_lmf));
412 ARM_LDR_IMM (code, ARMREG_LR, ARMREG_V1, G_STRUCT_OFFSET (MonoLMF, lmf_addr));
413 /* *(lmf_addr) = previous_lmf */
414 ARM_STR_IMM (code, ARMREG_IP, ARMREG_LR, G_STRUCT_OFFSET (MonoLMF, previous_lmf));
416 /* Non-standard function epilogue. Instead of doing a proper
417 * return, we just jump to the compiled code.
419 /* Restore the registers and jump to the code:
420 * Note that IP has been conveniently set to the method addr.
422 ARM_ADD_REG_IMM8 (code, ARMREG_SP, ARMREG_SP, STACK - regsave_size);
423 ARM_POP_NWB (code, 0x5fff);
424 if (tramp_type == MONO_TRAMPOLINE_RGCTX_LAZY_FETCH)
425 ARM_MOV_REG_REG (code, ARMREG_R0, ARMREG_IP);
426 ARM_ADD_REG_IMM8 (code, ARMREG_SP, ARMREG_SP, regsave_size);
427 if ((tramp_type == MONO_TRAMPOLINE_CLASS_INIT) || (tramp_type == MONO_TRAMPOLINE_GENERIC_CLASS_INIT) || (tramp_type == MONO_TRAMPOLINE_RGCTX_LAZY_FETCH))
428 code = emit_bx (code, ARMREG_LR);
430 code = emit_bx (code, ARMREG_IP);
432 #ifdef USE_JUMP_TABLES
433 load_get_lmf_addr [0] = mono_get_lmf_addr;
434 load_trampoline [0] = (gpointer)mono_get_trampoline_func (tramp_type);
436 constants = (gpointer*)code;
437 constants [0] = mono_get_lmf_addr;
438 constants [1] = (gpointer)mono_get_trampoline_func (tramp_type);
441 /* backpatch by emitting the missing instructions skipped above */
442 ARM_LDR_IMM (load_get_lmf_addr, ARMREG_R0, ARMREG_PC, (code - load_get_lmf_addr - 8));
443 ARM_LDR_IMM (load_trampoline, ARMREG_IP, ARMREG_PC, (code + 4 - load_trampoline - 8));
449 /* Flush instruction cache, since we've generated code */
450 mono_arch_flush_icache (buf, code - buf);
453 g_assert ((code - buf) <= buf_len);
455 if (tramp_type == MONO_TRAMPOLINE_CLASS_INIT)
456 /* Initialize the nullified class init trampoline used in the AOT case */
457 nullified_class_init_trampoline = mono_arch_get_nullified_class_init_trampoline (NULL);
460 tramp_name = mono_get_generic_trampoline_name (tramp_type);
461 *info = mono_tramp_info_create (tramp_name, buf, code - buf, ji, unwind_ops);
469 mono_arch_get_nullified_class_init_trampoline (MonoTrampInfo **info)
473 code = buf = mono_global_codeman_reserve (16);
475 code = emit_bx (code, ARMREG_LR);
477 mono_arch_flush_icache (buf, code - buf);
480 *info = mono_tramp_info_create ("nullified_class_init_trampoline", buf, code - buf, NULL, NULL);
485 #define SPEC_TRAMP_SIZE 24
488 mono_arch_create_specific_trampoline (gpointer arg1, MonoTrampolineType tramp_type, MonoDomain *domain, guint32 *code_len)
490 guint8 *code, *buf, *tramp;
492 #ifndef USE_JUMP_TABLES
493 guint32 short_branch;
495 guint32 size = SPEC_TRAMP_SIZE;
497 tramp = mono_get_trampoline_code (tramp_type);
499 mono_domain_lock (domain);
500 #ifdef USE_JUMP_TABLES
501 code = buf = mono_domain_code_reserve_align (domain, size, 4);
503 code = buf = mono_domain_code_reserve_align (domain, size, 4);
504 if ((short_branch = branch_for_target_reachable (code + 4, tramp))) {
506 mono_domain_code_commit (domain, code, SPEC_TRAMP_SIZE, size);
509 mono_domain_unlock (domain);
511 #ifdef USE_JUMP_TABLES
512 /* For jumptables case we always generate the same code for trampolines,
514 * push {r0, r1, r2, r3, r4, r5, r6, r7, r8, r9, r10, r11, r12, lr}
520 ARM_PUSH (code, 0x5fff);
521 constants = mono_jumptable_add_entries (2);
522 code = mono_arm_load_jumptable_entry_addr (code, constants, ARMREG_LR);
523 ARM_LDR_IMM (code, ARMREG_R1, ARMREG_LR, 4);
524 code = emit_bx (code, ARMREG_R1);
525 constants [0] = arg1;
526 constants [1] = tramp;
528 /* we could reduce this to 12 bytes if tramp is within reach:
532 * The called code can access method using the lr register
533 * A 20 byte sequence could be:
535 * ARM_MOV_REG_REG (lr, pc)
536 * ARM_LDR_IMM (pc, pc, 0)
540 /* We save all the registers, except PC and SP */
541 ARM_PUSH (code, 0x5fff);
543 constants = (gpointer*)code;
544 constants [0] = GUINT_TO_POINTER (short_branch | (1 << 24));
545 constants [1] = arg1;
548 ARM_LDR_IMM (code, ARMREG_R1, ARMREG_PC, 8); /* temp reg */
549 ARM_MOV_REG_REG (code, ARMREG_LR, ARMREG_PC);
550 code = emit_bx (code, ARMREG_R1);
552 constants = (gpointer*)code;
553 constants [0] = arg1;
554 constants [1] = tramp;
559 /* Flush instruction cache, since we've generated code */
560 mono_arch_flush_icache (buf, code - buf);
562 g_assert ((code - buf) <= size);
565 *code_len = code - buf;
571 * mono_arch_get_unbox_trampoline:
573 * @addr: pointer to native code for @m
575 * when value type methods are called through the vtable we need to unbox the
576 * this argument. This method returns a pointer to a trampoline which does
577 * unboxing before calling the method
580 mono_arch_get_unbox_trampoline (MonoMethod *m, gpointer addr)
582 guint8 *code, *start;
583 MonoDomain *domain = mono_domain_get ();
584 #ifdef USE_JUMP_TABLES
591 start = code = mono_domain_code_reserve (domain, size);
593 #ifdef USE_JUMP_TABLES
594 jte = mono_jumptable_add_entry ();
595 code = mono_arm_load_jumptable_entry (code, jte, ARMREG_IP);
596 ARM_ADD_REG_IMM8 (code, ARMREG_R0, ARMREG_R0, sizeof (MonoObject));
597 code = emit_bx (code, ARMREG_IP);
600 ARM_LDR_IMM (code, ARMREG_IP, ARMREG_PC, 4);
601 ARM_ADD_REG_IMM8 (code, ARMREG_R0, ARMREG_R0, sizeof (MonoObject));
602 code = emit_bx (code, ARMREG_IP);
603 *(guint32*)code = (guint32)addr;
606 mono_arch_flush_icache (start, code - start);
607 g_assert ((code - start) <= size);
608 /*g_print ("unbox trampoline at %d for %s:%s\n", this_pos, m->klass->name, m->name);
609 g_print ("unbox code is at %p for method at %p\n", start, addr);*/
615 mono_arch_get_static_rgctx_trampoline (MonoMethod *m, MonoMethodRuntimeGenericContext *mrgctx, gpointer addr)
617 guint8 *code, *start;
618 #ifdef USE_JUMP_TABLES
624 MonoDomain *domain = mono_domain_get ();
626 start = code = mono_domain_code_reserve (domain, buf_len);
628 #ifdef USE_JUMP_TABLES
629 jte = mono_jumptable_add_entries (2);
630 code = mono_arm_load_jumptable_entry_addr (code, jte, ARMREG_IP);
631 ARM_LDR_IMM (code, MONO_ARCH_RGCTX_REG, ARMREG_IP, 0);
632 ARM_LDR_IMM (code, ARMREG_IP, ARMREG_IP, 4);
633 ARM_BX (code, ARMREG_IP);
637 ARM_LDR_IMM (code, MONO_ARCH_RGCTX_REG, ARMREG_PC, 0);
638 ARM_LDR_IMM (code, ARMREG_PC, ARMREG_PC, 0);
639 *(guint32*)code = (guint32)mrgctx;
641 *(guint32*)code = (guint32)addr;
645 g_assert ((code - start) <= buf_len);
647 mono_arch_flush_icache (start, code - start);
653 mono_arch_create_rgctx_lazy_fetch_trampoline (guint32 slot, MonoTrampInfo **info, gboolean aot)
659 guint8 **rgctx_null_jumps;
663 MonoJumpInfo *ji = NULL;
664 GSList *unwind_ops = NULL;
665 #ifdef USE_JUMP_TABLES
669 mrgctx = MONO_RGCTX_SLOT_IS_MRGCTX (slot);
670 index = MONO_RGCTX_SLOT_INDEX (slot);
672 index += MONO_SIZEOF_METHOD_RUNTIME_GENERIC_CONTEXT / sizeof (gpointer);
673 for (depth = 0; ; ++depth) {
674 int size = mono_class_rgctx_get_array_size (depth, mrgctx);
676 if (index < size - 1)
681 tramp_size = 64 + 16 * depth;
683 code = buf = mono_global_codeman_reserve (tramp_size);
685 mono_add_unwind_op_def_cfa (unwind_ops, code, buf, ARMREG_SP, 0);
687 rgctx_null_jumps = g_malloc (sizeof (guint8*) * (depth + 2));
690 /* The vtable/mrgctx is in R0 */
691 g_assert (MONO_ARCH_VTABLE_REG == ARMREG_R0);
695 ARM_MOV_REG_REG (code, ARMREG_R1, ARMREG_R0);
697 /* load rgctx ptr from vtable */
698 g_assert (arm_is_imm12 (G_STRUCT_OFFSET (MonoVTable, runtime_generic_context)));
699 ARM_LDR_IMM (code, ARMREG_R1, ARMREG_R0, G_STRUCT_OFFSET (MonoVTable, runtime_generic_context));
700 /* is the rgctx ptr null? */
701 ARM_CMP_REG_IMM (code, ARMREG_R1, 0, 0);
702 /* if yes, jump to actual trampoline */
703 rgctx_null_jumps [njumps ++] = code;
704 ARM_B_COND (code, ARMCOND_EQ, 0);
707 for (i = 0; i < depth; ++i) {
708 /* load ptr to next array */
709 if (mrgctx && i == 0) {
710 g_assert (arm_is_imm12 (MONO_SIZEOF_METHOD_RUNTIME_GENERIC_CONTEXT));
711 ARM_LDR_IMM (code, ARMREG_R1, ARMREG_R1, MONO_SIZEOF_METHOD_RUNTIME_GENERIC_CONTEXT);
713 ARM_LDR_IMM (code, ARMREG_R1, ARMREG_R1, 0);
715 /* is the ptr 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);
723 code = mono_arm_emit_load_imm (code, ARMREG_R2, sizeof (gpointer) * (index + 1));
724 ARM_LDR_REG_REG (code, ARMREG_R1, ARMREG_R1, ARMREG_R2);
725 /* is the slot null? */
726 ARM_CMP_REG_IMM (code, ARMREG_R1, 0, 0);
727 /* if yes, jump to actual trampoline */
728 rgctx_null_jumps [njumps ++] = code;
729 ARM_B_COND (code, ARMCOND_EQ, 0);
730 /* otherwise return, result is in R1 */
731 ARM_MOV_REG_REG (code, ARMREG_R0, ARMREG_R1);
732 code = emit_bx (code, ARMREG_LR);
734 g_assert (njumps <= depth + 2);
735 for (i = 0; i < njumps; ++i)
736 arm_patch (rgctx_null_jumps [i], code);
738 g_free (rgctx_null_jumps);
742 /* The vtable/mrgctx is still in R0 */
745 ji = mono_patch_info_list_prepend (ji, code - buf, MONO_PATCH_INFO_JIT_ICALL_ADDR, g_strdup_printf ("specific_trampoline_lazy_fetch_%u", slot));
746 ARM_LDR_IMM (code, ARMREG_R1, ARMREG_PC, 0);
748 *(gpointer*)code = NULL;
750 ARM_LDR_REG_REG (code, ARMREG_PC, ARMREG_PC, ARMREG_R1);
752 tramp = mono_arch_create_specific_trampoline (GUINT_TO_POINTER (slot), MONO_TRAMPOLINE_RGCTX_LAZY_FETCH, mono_get_root_domain (), &code_len);
754 /* Jump to the actual trampoline */
755 #ifdef USE_JUMP_TABLES
756 jte = mono_jumptable_add_entry ();
758 code = mono_arm_load_jumptable_entry (code, jte, ARMREG_R1);
759 code = emit_bx (code, ARMREG_R1);
761 ARM_LDR_IMM (code, ARMREG_R1, ARMREG_PC, 0); /* temp reg */
762 code = emit_bx (code, ARMREG_R1);
763 *(gpointer*)code = tramp;
768 mono_arch_flush_icache (buf, code - buf);
770 g_assert (code - buf <= tramp_size);
773 char *name = mono_get_rgctx_fetch_trampoline_name (slot);
774 *info = mono_tramp_info_create (name, buf, code - buf, ji, unwind_ops);
782 mono_arch_create_general_rgctx_lazy_fetch_trampoline (MonoTrampInfo **info, gboolean aot)
786 MonoJumpInfo *ji = NULL;
787 GSList *unwind_ops = NULL;
793 code = buf = mono_global_codeman_reserve (tramp_size);
795 mono_add_unwind_op_def_cfa (unwind_ops, code, buf, ARMREG_SP, 0);
797 // FIXME: Currently, we always go to the slow path.
798 /* Load trampoline addr */
799 ARM_LDR_IMM (code, ARMREG_R1, MONO_ARCH_RGCTX_REG, 4);
800 /* The vtable/mrgctx is in R0 */
801 g_assert (MONO_ARCH_VTABLE_REG == ARMREG_R0);
802 code = emit_bx (code, ARMREG_R1);
804 mono_arch_flush_icache (buf, code - buf);
806 g_assert (code - buf <= tramp_size);
809 *info = mono_tramp_info_create ("rgctx_fetch_trampoline_general", buf, code - buf, ji, unwind_ops);
814 #define arm_is_imm8(v) ((v) > -256 && (v) < 256)
817 mono_arch_create_generic_class_init_trampoline (MonoTrampInfo **info, gboolean aot)
821 static int byte_offset = -1;
822 static guint8 bitmask;
825 guint32 code_len, imm8;
827 GSList *unwind_ops = NULL;
828 MonoJumpInfo *ji = NULL;
832 code = buf = mono_global_codeman_reserve (tramp_size);
835 mono_marshal_find_bitfield_offset (MonoVTable, initialized, &byte_offset, &bitmask);
837 g_assert (arm_is_imm8 (byte_offset));
838 ARM_LDRSB_IMM (code, ARMREG_IP, MONO_ARCH_VTABLE_REG, byte_offset);
839 imm8 = mono_arm_is_rotated_imm8 (bitmask, &rot_amount);
840 g_assert (imm8 >= 0);
841 ARM_AND_REG_IMM (code, ARMREG_IP, ARMREG_IP, imm8, rot_amount);
842 ARM_CMP_REG_IMM (code, ARMREG_IP, 0, 0);
844 ARM_B_COND (code, ARMCOND_EQ, 0);
846 /* Initialized case */
847 ARM_MOV_REG_REG (code, ARMREG_PC, ARMREG_LR);
849 /* Uninitialized case */
850 arm_patch (jump, code);
853 ji = mono_patch_info_list_prepend (ji, code - buf, MONO_PATCH_INFO_JIT_ICALL_ADDR, "specific_trampoline_generic_class_init");
854 ARM_LDR_IMM (code, ARMREG_R1, ARMREG_PC, 0);
856 *(gpointer*)code = NULL;
858 ARM_LDR_REG_REG (code, ARMREG_PC, ARMREG_PC, ARMREG_R1);
860 #ifdef USE_JUMP_TABLES
861 gpointer *jte = mono_jumptable_add_entry ();
863 tramp = mono_arch_create_specific_trampoline (NULL, MONO_TRAMPOLINE_GENERIC_CLASS_INIT, mono_get_root_domain (), &code_len);
865 /* Jump to the actual trampoline */
866 #ifdef USE_JUMP_TABLES
867 code = mono_arm_load_jumptable_entry (code, jte, ARMREG_R1);
869 code = emit_bx (code, ARMREG_R1);
871 ARM_LDR_IMM (code, ARMREG_R1, ARMREG_PC, 0); /* temp reg */
872 code = emit_bx (code, ARMREG_R1);
873 *(gpointer*)code = tramp;
878 mono_arch_flush_icache (buf, code - buf);
880 g_assert (code - buf <= tramp_size);
883 *info = mono_tramp_info_create ("generic_class_init_trampoline", buf, code - buf, ji, unwind_ops);
891 mono_arch_create_generic_trampoline (MonoTrampolineType tramp_type, MonoTrampInfo **info, gboolean aot)
893 g_assert_not_reached ();
898 mono_arch_create_specific_trampoline (gpointer arg1, MonoTrampolineType tramp_type, MonoDomain *domain, guint32 *code_len)
900 g_assert_not_reached ();
905 mono_arch_get_unbox_trampoline (MonoMethod *m, gpointer addr)
907 g_assert_not_reached ();
912 mono_arch_get_static_rgctx_trampoline (MonoMethod *m, MonoMethodRuntimeGenericContext *mrgctx, gpointer addr)
914 g_assert_not_reached ();
919 mono_arch_create_rgctx_lazy_fetch_trampoline (guint32 slot, MonoTrampInfo **info, gboolean aot)
921 g_assert_not_reached ();
926 mono_arch_create_generic_class_init_trampoline (MonoTrampInfo **info, gboolean aot)
928 g_assert_not_reached ();
932 #endif /* DISABLE_JIT */
935 mono_arch_get_call_target (guint8 *code)
937 guint32 ins = ((guint32*)(gpointer)code) [-1];
940 /* Should be a 'bl' or a 'b' */
941 if (((ins >> 25) & 0x7) == 0x5) {
943 /* Should be a 'bl' */
944 if ((((ins >> 25) & 0x7) == 0x5) && (((ins >> 24) & 0x1) == 0x1)) {
946 gint32 disp = ((gint32)ins) & 0xffffff;
947 guint8 *target = code - 4 + 8 + (disp * 4);
956 mono_arch_get_plt_info_offset (guint8 *plt_entry, mgreg_t *regs, guint8 *code)
958 /* The offset is stored as the 4th word of the plt entry */
959 return ((guint32*)plt_entry) [3];
963 * Return the address of the PLT entry called by the thumb code CODE.
966 mono_arm_get_thumb_plt_entry (guint8 *code)
968 int s, j1, j2, imm10, imm11, i1, i2, imm32;
973 /* code should be right after a BL */
974 code = (guint8*)((mgreg_t)code & ~1);
975 base = (guint8*)((mgreg_t)code & ~3);
977 t1 = ((guint16*)bl) [0];
978 t2 = ((guint16*)bl) [1];
980 g_assert ((t1 >> 11) == 0x1e);
982 s = (t1 >> 10) & 0x1;
983 imm10 = (t1 >> 0) & 0x3ff;
984 j1 = (t2 >> 13) & 0x1;
985 j2 = (t2 >> 11) & 0x1;
988 i1 = (s ^ j1) ? 0 : 1;
989 i2 = (s ^ j2) ? 0 : 1;
991 imm32 = (imm11 << 1) | (imm10 << 12) | (i2 << 22) | (i1 << 23);
995 target = code + imm32;
997 /* target now points to the thumb plt entry */
998 /* ldr.w r12, [pc, #8] */
999 g_assert (((guint16*)target) [0] == 0xf8df);
1000 g_assert (((guint16*)target) [1] == 0xc008);
1003 * The PLT info offset is at offset 16, but mono_arch_get_plt_entry_offset () returns
1004 * the 3rd word, so compensate by returning a different value.
1014 * mono_arch_get_gsharedvt_arg_trampoline:
1016 * See tramp-x86.c for documentation.
1019 mono_arch_get_gsharedvt_arg_trampoline (MonoDomain *domain, gpointer arg, gpointer addr)
1021 guint8 *code, *start;
1023 gpointer *constants;
1027 start = code = mono_domain_code_reserve (domain, buf_len);
1029 /* Similar to the specialized trampoline code */
1030 ARM_PUSH (code, (1 << ARMREG_R0) | (1 << ARMREG_R1) | (1 << ARMREG_R2) | (1 << ARMREG_R3) | (1 << ARMREG_LR));
1031 ARM_LDR_IMM (code, ARMREG_IP, ARMREG_PC, 8);
1032 /* arg is passed in LR */
1033 ARM_LDR_IMM (code, ARMREG_LR, ARMREG_PC, 0);
1034 code = emit_bx (code, ARMREG_IP);
1035 constants = (gpointer*)code;
1036 constants [0] = arg;
1037 constants [1] = addr;
1040 g_assert ((code - start) <= buf_len);
1042 nacl_domain_code_validate (domain, &start, buf_len, &code);
1043 mono_arch_flush_icache (start, code - start);
1051 mono_arch_get_gsharedvt_arg_trampoline (MonoDomain *domain, gpointer arg, gpointer addr)
1053 g_assert_not_reached ();
1059 #if defined(MONOTOUCH) || defined(MONO_EXTENSIONS)
1061 #include "../../../mono-extensions/mono/mini/tramp-arm-gsharedvt.c"
1066 mono_arm_start_gsharedvt_call (GSharedVtCallInfo *info, gpointer *caller, gpointer *callee, gpointer mrgctx_reg)
1068 g_assert_not_reached ();
1073 mono_arch_get_gsharedvt_trampoline (MonoTrampInfo **info, gboolean aot)
1080 #endif /* !MONOTOUCH */