2 * tramp-arm.c: JIT trampoline code for ARM
5 * Paolo Molaro (lupus@ximian.com)
7 * (C) 2001 Ximian, Inc.
13 #include <mono/metadata/appdomain.h>
14 #include <mono/metadata/marshal.h>
15 #include <mono/metadata/tabledefs.h>
16 #include <mono/arch/arm/arm-codegen.h>
21 #define ALIGN_TO(val,align) ((((guint64)val) + ((align) - 1)) & ~((align) - 1))
23 static guint8* nullified_class_init_trampoline;
26 mono_arch_patch_callsite (guint8 *method_start, guint8 *code_ptr, guint8 *addr)
28 guint32 *code = (guint32*)code_ptr;
30 /* This is the 'bl' or the 'mov pc' instruction */
34 * Note that methods are called also with the bl opcode.
36 if ((((*code) >> 25) & 7) == 5) {
37 /*g_print ("direct patching\n");*/
38 arm_patch ((guint8*)code, addr);
39 mono_arch_flush_icache ((guint8*)code, 4);
43 if ((((*code) >> 20) & 0xFF) == 0x12) {
44 /*g_print ("patching bx\n");*/
45 arm_patch ((guint8*)code, addr);
46 mono_arch_flush_icache ((guint8*)(code - 2), 4);
50 g_assert_not_reached ();
54 mono_arch_patch_plt_entry (guint8 *code, gpointer *got, mgreg_t *regs, guint8 *addr)
58 /* Patch the jump table entry used by the plt entry */
59 if (*(guint32*)code == 0xe59fc000) {
60 /* ARM_LDR_IMM (code, ARMREG_IP, ARMREG_PC, 0); */
61 guint32 offset = ((guint32*)code)[2];
63 jump_entry = code + offset + 12;
64 } else if (*(guint16*)(code - 4) == 0xf8df) {
66 * Thumb PLT entry, begins with ldr.w ip, [pc, #8], code points to entry + 4, see
67 * mono_arm_get_thumb_plt_entry ().
72 offset = *(guint32*)(code + 12);
73 jump_entry = code + offset + 8;
75 g_assert_not_reached ();
78 *(guint8**)jump_entry = addr;
82 mono_arch_nullify_class_init_trampoline (guint8 *code, mgreg_t *regs)
84 mono_arch_patch_callsite (NULL, code, nullified_class_init_trampoline);
88 mono_arch_nullify_plt_entry (guint8 *code, mgreg_t *regs)
90 if (mono_aot_only && !nullified_class_init_trampoline)
91 nullified_class_init_trampoline = mono_aot_get_trampoline ("nullified_class_init_trampoline");
93 mono_arch_patch_plt_entry (code, NULL, regs, nullified_class_init_trampoline);
98 #define arm_is_imm12(v) ((int)(v) > -4096 && (int)(v) < 4096)
101 * Return the instruction to jump from code to target, 0 if not
102 * reachable with a single instruction
105 branch_for_target_reachable (guint8 *branch, guint8 *target)
107 gint diff = target - branch - 8;
108 g_assert ((diff & 3) == 0);
110 if (diff <= 33554431)
111 return (ARMCOND_AL << ARMCOND_SHIFT) | (ARM_BR_TAG) | (diff >> 2);
113 /* diff between 0 and -33554432 */
114 if (diff >= -33554432)
115 return (ARMCOND_AL << ARMCOND_SHIFT) | (ARM_BR_TAG) | ((diff >> 2) & ~0xff000000);
120 static inline guint8*
121 emit_bx (guint8* code, int reg)
123 if (mono_arm_thumb_supported ())
126 ARM_MOV_REG_REG (code, ARMREG_PC, reg);
130 /* Stack size for trampoline function
132 #define STACK ALIGN_TO (sizeof (MonoLMF), 8)
134 /* Method-specific trampoline code fragment size */
135 #define METHOD_TRAMPOLINE_SIZE 64
137 /* Jump-specific trampoline code fragment size */
138 #define JUMP_TRAMPOLINE_SIZE 64
141 mono_arch_create_generic_trampoline (MonoTrampolineType tramp_type, MonoTrampInfo **info, gboolean aot)
143 guint8 *buf, *code = NULL;
144 guint8 *load_get_lmf_addr, *load_trampoline;
146 int cfa_offset, lmf_offset, regsave_size, lr_offset;
147 GSList *unwind_ops = NULL;
148 MonoJumpInfo *ji = NULL;
151 /* Now we'll create in 'buf' the ARM trampoline code. This
152 is the trampoline code common to all methods */
155 code = buf = mono_global_codeman_reserve (buf_len);
158 * At this point lr points to the specific arg and sp points to the saved
159 * regs on the stack (all but PC and SP). The original LR value has been
160 * saved as sp + LR_OFFSET by the push in the specific trampoline
163 /* The offset of lmf inside the stack frame */
164 lmf_offset = STACK - sizeof (MonoLMF);
165 /* The size of the area already allocated by the push in the specific trampoline */
166 regsave_size = 14 * sizeof (mgreg_t);
167 /* The offset where lr was saved inside the regsave area */
168 lr_offset = 13 * sizeof (mgreg_t);
170 // FIXME: Finish the unwind info, the current info allows us to unwind
171 // when the trampoline is not in the epilog
173 // CFA = SP + (num registers pushed) * 4
174 cfa_offset = 14 * sizeof (mgreg_t);
175 mono_add_unwind_op_def_cfa (unwind_ops, code, buf, ARMREG_SP, cfa_offset);
176 // PC saved at sp+LR_OFFSET
177 mono_add_unwind_op_offset (unwind_ops, code, buf, ARMREG_LR, -4);
179 if (aot && tramp_type != MONO_TRAMPOLINE_GENERIC_CLASS_INIT) {
181 * The trampoline contains a pc-relative offset to the got slot
182 * preceeding the got slot where the value is stored. The offset can be
185 ARM_LDR_IMM (code, ARMREG_V2, ARMREG_LR, 0);
186 ARM_ADD_REG_IMM (code, ARMREG_V2, ARMREG_V2, 4, 0);
187 ARM_LDR_REG_REG (code, ARMREG_V2, ARMREG_V2, ARMREG_LR);
189 if (tramp_type != MONO_TRAMPOLINE_GENERIC_CLASS_INIT)
190 ARM_LDR_IMM (code, ARMREG_V2, ARMREG_LR, 0);
192 ARM_MOV_REG_REG (code, ARMREG_V2, MONO_ARCH_VTABLE_REG);
194 ARM_LDR_IMM (code, ARMREG_V3, ARMREG_SP, lr_offset);
196 /* ok, now we can continue with the MonoLMF setup, mostly untouched
197 * from emit_prolog in mini-arm.c
198 * This is a synthetized call to mono_get_lmf_addr ()
201 ji = mono_patch_info_list_prepend (ji, code - buf, MONO_PATCH_INFO_JIT_ICALL_ADDR, "mono_get_lmf_addr");
202 ARM_LDR_IMM (code, ARMREG_R0, ARMREG_PC, 0);
204 *(gpointer*)code = NULL;
206 ARM_LDR_REG_REG (code, ARMREG_R0, ARMREG_PC, ARMREG_R0);
208 load_get_lmf_addr = code;
211 ARM_MOV_REG_REG (code, ARMREG_LR, ARMREG_PC);
212 code = emit_bx (code, ARMREG_R0);
214 /* we build the MonoLMF structure on the stack - see mini-arm.h
215 * The pointer to the struct is put in r1.
216 * the iregs array is already allocated on the stack by push.
218 ARM_SUB_REG_IMM8 (code, ARMREG_SP, ARMREG_SP, STACK - regsave_size);
219 cfa_offset += STACK - regsave_size;
220 mono_add_unwind_op_def_cfa_offset (unwind_ops, code, buf, cfa_offset);
222 ARM_ADD_REG_IMM8 (code, ARMREG_V1, ARMREG_SP, STACK - sizeof (MonoLMF));
225 * The stack now looks like:
227 * v1 -> <rest of LMF>
231 /* r0 is the result from mono_get_lmf_addr () */
232 ARM_STR_IMM (code, ARMREG_R0, ARMREG_V1, G_STRUCT_OFFSET (MonoLMF, lmf_addr));
233 /* new_lmf->previous_lmf = *lmf_addr */
234 ARM_LDR_IMM (code, ARMREG_R2, ARMREG_R0, G_STRUCT_OFFSET (MonoLMF, previous_lmf));
235 ARM_STR_IMM (code, ARMREG_R2, ARMREG_V1, G_STRUCT_OFFSET (MonoLMF, previous_lmf));
236 /* *(lmf_addr) = r1 */
237 ARM_STR_IMM (code, ARMREG_V1, ARMREG_R0, G_STRUCT_OFFSET (MonoLMF, previous_lmf));
238 /* save method info (it's in v2) */
239 if ((tramp_type == MONO_TRAMPOLINE_JIT) || (tramp_type == MONO_TRAMPOLINE_JUMP))
240 ARM_STR_IMM (code, ARMREG_V2, ARMREG_V1, G_STRUCT_OFFSET (MonoLMF, method));
242 ARM_MOV_REG_IMM8 (code, ARMREG_R2, 0);
243 ARM_STR_IMM (code, ARMREG_R2, ARMREG_V1, G_STRUCT_OFFSET (MonoLMF, method));
245 /* Save sp into lmf->iregs, the eh code expects it to be at IP */
246 ARM_ADD_REG_IMM8 (code, ARMREG_R2, ARMREG_SP, cfa_offset);
247 ARM_STR_IMM (code, ARMREG_R2, ARMREG_V1, G_STRUCT_OFFSET (MonoLMF, iregs) + (ARMREG_IP * sizeof (mgreg_t)));
248 ARM_STR_IMM (code, ARMREG_SP, ARMREG_V1, G_STRUCT_OFFSET (MonoLMF, esp));
249 /* save the IP (caller ip) */
250 if (tramp_type == MONO_TRAMPOLINE_JUMP) {
251 ARM_MOV_REG_IMM8 (code, ARMREG_R2, 0);
253 ARM_LDR_IMM (code, ARMREG_R2, ARMREG_V1, (G_STRUCT_OFFSET (MonoLMF, iregs) + 13*4));
255 ARM_STR_IMM (code, ARMREG_R2, ARMREG_V1, G_STRUCT_OFFSET (MonoLMF, eip));
258 * Now we're ready to call xxx_trampoline ().
260 /* Arg 1: the saved registers */
261 ARM_ADD_REG_IMM8 (code, ARMREG_R0, ARMREG_V1, G_STRUCT_OFFSET (MonoLMF, iregs));
263 /* Arg 2: code (next address to the instruction that called us) */
264 if (tramp_type == MONO_TRAMPOLINE_JUMP) {
265 ARM_MOV_REG_IMM8 (code, ARMREG_R1, 0);
267 ARM_MOV_REG_REG (code, ARMREG_R1, ARMREG_V3);
270 /* Arg 3: the specific argument, stored in v2
272 ARM_MOV_REG_REG (code, ARMREG_R2, ARMREG_V2);
275 char *icall_name = g_strdup_printf ("trampoline_func_%d", tramp_type);
276 ji = mono_patch_info_list_prepend (ji, code - buf, MONO_PATCH_INFO_JIT_ICALL_ADDR, icall_name);
277 ARM_LDR_IMM (code, ARMREG_IP, ARMREG_PC, 0);
279 *(gpointer*)code = NULL;
281 ARM_LDR_REG_REG (code, ARMREG_IP, ARMREG_PC, ARMREG_IP);
283 load_trampoline = code;
287 ARM_MOV_REG_REG (code, ARMREG_LR, ARMREG_PC);
288 code = emit_bx (code, ARMREG_IP);
290 /* OK, code address is now on r0. Move it to the place on the stack
291 * where IP was saved (it is now no more useful to us and it can be
292 * clobbered). This way we can just restore all the regs in one inst
295 ARM_STR_IMM (code, ARMREG_R0, ARMREG_V1, G_STRUCT_OFFSET (MonoLMF, iregs) + (ARMREG_R12 * sizeof (mgreg_t)));
297 /* Check for thread interruption */
298 /* This is not perf critical code so no need to check the interrupt flag */
300 * Have to call the _force_ variant, since there could be a protected wrapper on the top of the stack.
303 ji = mono_patch_info_list_prepend (ji, code - buf, MONO_PATCH_INFO_JIT_ICALL_ADDR, "mono_thread_force_interruption_checkpoint");
304 ARM_LDR_IMM (code, ARMREG_IP, ARMREG_PC, 0);
306 *(gpointer*)code = NULL;
308 ARM_LDR_REG_REG (code, ARMREG_IP, ARMREG_PC, ARMREG_IP);
310 ARM_LDR_IMM (code, ARMREG_IP, ARMREG_PC, 0);
312 *(gpointer*)code = mono_thread_force_interruption_checkpoint;
315 ARM_MOV_REG_REG (code, ARMREG_LR, ARMREG_PC);
316 code = emit_bx (code, ARMREG_IP);
319 * Now we restore the MonoLMF (see emit_epilogue in mini-arm.c)
320 * and the rest of the registers, so the method called will see
321 * the same state as before we executed.
323 /* ip = previous_lmf */
324 ARM_LDR_IMM (code, ARMREG_IP, ARMREG_V1, G_STRUCT_OFFSET (MonoLMF, previous_lmf));
326 ARM_LDR_IMM (code, ARMREG_LR, ARMREG_V1, G_STRUCT_OFFSET (MonoLMF, lmf_addr));
327 /* *(lmf_addr) = previous_lmf */
328 ARM_STR_IMM (code, ARMREG_IP, ARMREG_LR, G_STRUCT_OFFSET (MonoLMF, previous_lmf));
330 /* Non-standard function epilogue. Instead of doing a proper
331 * return, we just jump to the compiled code.
333 /* Restore the registers and jump to the code:
334 * Note that IP has been conveniently set to the method addr.
336 ARM_ADD_REG_IMM8 (code, ARMREG_SP, ARMREG_SP, STACK - regsave_size);
337 ARM_POP_NWB (code, 0x5fff);
338 if (tramp_type == MONO_TRAMPOLINE_RGCTX_LAZY_FETCH)
339 ARM_MOV_REG_REG (code, ARMREG_R0, ARMREG_IP);
340 ARM_ADD_REG_IMM8 (code, ARMREG_SP, ARMREG_SP, regsave_size);
341 if ((tramp_type == MONO_TRAMPOLINE_CLASS_INIT) || (tramp_type == MONO_TRAMPOLINE_GENERIC_CLASS_INIT) || (tramp_type == MONO_TRAMPOLINE_RGCTX_LAZY_FETCH))
342 code = emit_bx (code, ARMREG_LR);
344 code = emit_bx (code, ARMREG_IP);
346 constants = (gpointer*)code;
347 constants [0] = mono_get_lmf_addr;
348 constants [1] = (gpointer)mono_get_trampoline_func (tramp_type);
351 /* backpatch by emitting the missing instructions skipped above */
352 ARM_LDR_IMM (load_get_lmf_addr, ARMREG_R0, ARMREG_PC, (code - load_get_lmf_addr - 8));
353 ARM_LDR_IMM (load_trampoline, ARMREG_IP, ARMREG_PC, (code + 4 - load_trampoline - 8));
358 /* Flush instruction cache, since we've generated code */
359 mono_arch_flush_icache (buf, code - buf);
362 g_assert ((code - buf) <= buf_len);
364 if (tramp_type == MONO_TRAMPOLINE_CLASS_INIT)
365 /* Initialize the nullified class init trampoline used in the AOT case */
366 nullified_class_init_trampoline = mono_arch_get_nullified_class_init_trampoline (NULL);
369 *info = mono_tramp_info_create (mono_get_generic_trampoline_name (tramp_type), buf, code - buf, ji, unwind_ops);
375 mono_arch_get_nullified_class_init_trampoline (MonoTrampInfo **info)
379 code = buf = mono_global_codeman_reserve (16);
381 code = emit_bx (code, ARMREG_LR);
383 mono_arch_flush_icache (buf, code - buf);
386 *info = mono_tramp_info_create (g_strdup_printf ("nullified_class_init_trampoline"), buf, code - buf, NULL, NULL);
391 #define SPEC_TRAMP_SIZE 24
394 mono_arch_create_specific_trampoline (gpointer arg1, MonoTrampolineType tramp_type, MonoDomain *domain, guint32 *code_len)
396 guint8 *code, *buf, *tramp;
398 guint32 short_branch, size = SPEC_TRAMP_SIZE;
400 tramp = mono_get_trampoline_code (tramp_type);
402 mono_domain_lock (domain);
403 code = buf = mono_domain_code_reserve_align (domain, size, 4);
404 if ((short_branch = branch_for_target_reachable (code + 4, tramp))) {
406 mono_domain_code_commit (domain, code, SPEC_TRAMP_SIZE, size);
408 mono_domain_unlock (domain);
410 /* we could reduce this to 12 bytes if tramp is within reach:
414 * The called code can access method using the lr register
415 * A 20 byte sequence could be:
417 * ARM_MOV_REG_REG (lr, pc)
418 * ARM_LDR_IMM (pc, pc, 0)
422 /* We save all the registers, except PC and SP */
423 ARM_PUSH (code, 0x5fff);
425 constants = (gpointer*)code;
426 constants [0] = GUINT_TO_POINTER (short_branch | (1 << 24));
427 constants [1] = arg1;
430 ARM_LDR_IMM (code, ARMREG_R1, ARMREG_PC, 8); /* temp reg */
431 ARM_MOV_REG_REG (code, ARMREG_LR, ARMREG_PC);
432 code = emit_bx (code, ARMREG_R1);
434 constants = (gpointer*)code;
435 constants [0] = arg1;
436 constants [1] = tramp;
440 /* Flush instruction cache, since we've generated code */
441 mono_arch_flush_icache (buf, code - buf);
443 g_assert ((code - buf) <= size);
446 *code_len = code - buf;
452 * mono_arch_get_unbox_trampoline:
454 * @addr: pointer to native code for @m
456 * when value type methods are called through the vtable we need to unbox the
457 * this argument. This method returns a pointer to a trampoline which does
458 * unboxing before calling the method
461 mono_arch_get_unbox_trampoline (MonoMethod *m, gpointer addr)
463 guint8 *code, *start;
464 MonoDomain *domain = mono_domain_get ();
466 start = code = mono_domain_code_reserve (domain, 16);
468 ARM_LDR_IMM (code, ARMREG_IP, ARMREG_PC, 4);
469 ARM_ADD_REG_IMM8 (code, ARMREG_R0, ARMREG_R0, sizeof (MonoObject));
470 code = emit_bx (code, ARMREG_IP);
471 *(guint32*)code = (guint32)addr;
473 mono_arch_flush_icache (start, code - start);
474 g_assert ((code - start) <= 16);
475 /*g_print ("unbox trampoline at %d for %s:%s\n", this_pos, m->klass->name, m->name);
476 g_print ("unbox code is at %p for method at %p\n", start, addr);*/
482 mono_arch_get_static_rgctx_trampoline (MonoMethod *m, MonoMethodRuntimeGenericContext *mrgctx, gpointer addr)
484 guint8 *code, *start;
487 MonoDomain *domain = mono_domain_get ();
491 start = code = mono_domain_code_reserve (domain, buf_len);
493 ARM_LDR_IMM (code, MONO_ARCH_RGCTX_REG, ARMREG_PC, 0);
494 ARM_LDR_IMM (code, ARMREG_PC, ARMREG_PC, 0);
495 *(guint32*)code = (guint32)mrgctx;
497 *(guint32*)code = (guint32)addr;
500 g_assert ((code - start) <= buf_len);
502 mono_arch_flush_icache (start, code - start);
508 mono_arch_create_rgctx_lazy_fetch_trampoline (guint32 slot, MonoTrampInfo **info, gboolean aot)
514 guint8 **rgctx_null_jumps;
518 MonoJumpInfo *ji = NULL;
519 GSList *unwind_ops = NULL;
521 mrgctx = MONO_RGCTX_SLOT_IS_MRGCTX (slot);
522 index = MONO_RGCTX_SLOT_INDEX (slot);
524 index += MONO_SIZEOF_METHOD_RUNTIME_GENERIC_CONTEXT / sizeof (gpointer);
525 for (depth = 0; ; ++depth) {
526 int size = mono_class_rgctx_get_array_size (depth, mrgctx);
528 if (index < size - 1)
533 tramp_size = 64 + 16 * depth;
535 code = buf = mono_global_codeman_reserve (tramp_size);
537 mono_add_unwind_op_def_cfa (unwind_ops, code, buf, ARMREG_SP, 0);
539 rgctx_null_jumps = g_malloc (sizeof (guint8*) * (depth + 2));
542 /* The vtable/mrgctx is in R0 */
543 g_assert (MONO_ARCH_VTABLE_REG == ARMREG_R0);
547 ARM_MOV_REG_REG (code, ARMREG_R1, ARMREG_R0);
549 /* load rgctx ptr from vtable */
550 g_assert (arm_is_imm12 (G_STRUCT_OFFSET (MonoVTable, runtime_generic_context)));
551 ARM_LDR_IMM (code, ARMREG_R1, ARMREG_R0, G_STRUCT_OFFSET (MonoVTable, runtime_generic_context));
552 /* is the rgctx ptr null? */
553 ARM_CMP_REG_IMM (code, ARMREG_R1, 0, 0);
554 /* if yes, jump to actual trampoline */
555 rgctx_null_jumps [njumps ++] = code;
556 ARM_B_COND (code, ARMCOND_EQ, 0);
559 for (i = 0; i < depth; ++i) {
560 /* load ptr to next array */
561 if (mrgctx && i == 0) {
562 g_assert (arm_is_imm12 (MONO_SIZEOF_METHOD_RUNTIME_GENERIC_CONTEXT));
563 ARM_LDR_IMM (code, ARMREG_R1, ARMREG_R1, MONO_SIZEOF_METHOD_RUNTIME_GENERIC_CONTEXT);
565 ARM_LDR_IMM (code, ARMREG_R1, ARMREG_R1, 0);
567 /* is the ptr null? */
568 ARM_CMP_REG_IMM (code, ARMREG_R1, 0, 0);
569 /* if yes, jump to actual trampoline */
570 rgctx_null_jumps [njumps ++] = code;
571 ARM_B_COND (code, ARMCOND_EQ, 0);
575 code = mono_arm_emit_load_imm (code, ARMREG_R2, sizeof (gpointer) * (index + 1));
576 ARM_LDR_REG_REG (code, ARMREG_R1, ARMREG_R1, ARMREG_R2);
577 /* is the slot null? */
578 ARM_CMP_REG_IMM (code, ARMREG_R1, 0, 0);
579 /* if yes, jump to actual trampoline */
580 rgctx_null_jumps [njumps ++] = code;
581 ARM_B_COND (code, ARMCOND_EQ, 0);
582 /* otherwise return, result is in R1 */
583 ARM_MOV_REG_REG (code, ARMREG_R0, ARMREG_R1);
584 code = emit_bx (code, ARMREG_LR);
586 g_assert (njumps <= depth + 2);
587 for (i = 0; i < njumps; ++i)
588 arm_patch (rgctx_null_jumps [i], code);
590 g_free (rgctx_null_jumps);
594 /* The vtable/mrgctx is still in R0 */
597 ji = mono_patch_info_list_prepend (ji, code - buf, MONO_PATCH_INFO_JIT_ICALL_ADDR, g_strdup_printf ("specific_trampoline_lazy_fetch_%u", slot));
598 ARM_LDR_IMM (code, ARMREG_R1, ARMREG_PC, 0);
600 *(gpointer*)code = NULL;
602 ARM_LDR_REG_REG (code, ARMREG_PC, ARMREG_PC, ARMREG_R1);
604 tramp = mono_arch_create_specific_trampoline (GUINT_TO_POINTER (slot), MONO_TRAMPOLINE_RGCTX_LAZY_FETCH, mono_get_root_domain (), &code_len);
606 /* Jump to the actual trampoline */
607 ARM_LDR_IMM (code, ARMREG_R1, ARMREG_PC, 0); /* temp reg */
608 code = emit_bx (code, ARMREG_R1);
609 *(gpointer*)code = tramp;
613 mono_arch_flush_icache (buf, code - buf);
615 g_assert (code - buf <= tramp_size);
618 *info = mono_tramp_info_create (mono_get_rgctx_fetch_trampoline_name (slot), buf, code - buf, ji, unwind_ops);
623 #define arm_is_imm8(v) ((v) > -256 && (v) < 256)
626 mono_arch_create_generic_class_init_trampoline (MonoTrampInfo **info, gboolean aot)
630 static int byte_offset = -1;
631 static guint8 bitmask;
634 guint32 code_len, imm8;
636 GSList *unwind_ops = NULL;
637 MonoJumpInfo *ji = NULL;
641 code = buf = mono_global_codeman_reserve (tramp_size);
644 mono_marshal_find_bitfield_offset (MonoVTable, initialized, &byte_offset, &bitmask);
646 g_assert (arm_is_imm8 (byte_offset));
647 ARM_LDRSB_IMM (code, ARMREG_IP, MONO_ARCH_VTABLE_REG, byte_offset);
648 imm8 = mono_arm_is_rotated_imm8 (bitmask, &rot_amount);
649 g_assert (imm8 >= 0);
650 ARM_AND_REG_IMM (code, ARMREG_IP, ARMREG_IP, imm8, rot_amount);
651 ARM_CMP_REG_IMM (code, ARMREG_IP, 0, 0);
653 ARM_B_COND (code, ARMCOND_EQ, 0);
655 /* Initialized case */
656 ARM_MOV_REG_REG (code, ARMREG_PC, ARMREG_LR);
658 /* Uninitialized case */
659 arm_patch (jump, code);
662 ji = mono_patch_info_list_prepend (ji, code - buf, MONO_PATCH_INFO_JIT_ICALL_ADDR, "specific_trampoline_generic_class_init");
663 ARM_LDR_IMM (code, ARMREG_R1, ARMREG_PC, 0);
665 *(gpointer*)code = NULL;
667 ARM_LDR_REG_REG (code, ARMREG_PC, ARMREG_PC, ARMREG_R1);
669 tramp = mono_arch_create_specific_trampoline (NULL, MONO_TRAMPOLINE_GENERIC_CLASS_INIT, mono_get_root_domain (), &code_len);
671 /* Jump to the actual trampoline */
672 ARM_LDR_IMM (code, ARMREG_R1, ARMREG_PC, 0); /* temp reg */
673 code = emit_bx (code, ARMREG_R1);
674 *(gpointer*)code = tramp;
678 mono_arch_flush_icache (buf, code - buf);
680 g_assert (code - buf <= tramp_size);
683 *info = mono_tramp_info_create (g_strdup_printf ("generic_class_init_trampoline"), buf, code - buf, ji, unwind_ops);
691 mono_arch_create_generic_trampoline (MonoTrampolineType tramp_type, MonoTrampInfo **info, gboolean aot)
693 g_assert_not_reached ();
698 mono_arch_create_specific_trampoline (gpointer arg1, MonoTrampolineType tramp_type, MonoDomain *domain, guint32 *code_len)
700 g_assert_not_reached ();
705 mono_arch_get_unbox_trampoline (MonoMethod *m, gpointer addr)
707 g_assert_not_reached ();
712 mono_arch_get_static_rgctx_trampoline (MonoMethod *m, MonoMethodRuntimeGenericContext *mrgctx, gpointer addr)
714 g_assert_not_reached ();
719 mono_arch_create_rgctx_lazy_fetch_trampoline (guint32 slot, MonoTrampInfo **info, gboolean aot)
721 g_assert_not_reached ();
726 mono_arch_create_generic_class_init_trampoline (MonoTrampInfo **info, gboolean aot)
728 g_assert_not_reached ();
732 #endif /* DISABLE_JIT */
735 mono_arch_get_call_target (guint8 *code)
737 guint32 ins = ((guint32*)(gpointer)code) [-1];
739 /* Should be a 'bl' */
740 if ((((ins >> 25) & 0x7) == 0x5) && (((ins >> 24) & 0x1) == 0x1)) {
741 gint32 disp = ((gint32)ins) & 0xffffff;
742 guint8 *target = code - 4 + 8 + (disp * 4);
751 mono_arch_get_plt_info_offset (guint8 *plt_entry, mgreg_t *regs, guint8 *code)
753 /* The offset is stored as the 4th word of the plt entry */
754 return ((guint32*)plt_entry) [3];
758 * Return the address of the PLT entry called by the thumb code CODE.
761 mono_arm_get_thumb_plt_entry (guint8 *code)
763 int s, j1, j2, imm10, imm11, i1, i2, imm32;
768 /* code should be right after a BL */
769 code = (guint8*)((mgreg_t)code & ~1);
770 base = (guint8*)((mgreg_t)code & ~3);
772 t1 = ((guint16*)bl) [0];
773 t2 = ((guint16*)bl) [1];
775 g_assert ((t1 >> 11) == 0x1e);
777 s = (t1 >> 10) & 0x1;
778 imm10 = (t1 >> 0) & 0x3ff;
779 j1 = (t2 >> 13) & 0x1;
780 j2 = (t2 >> 11) & 0x1;
783 i1 = (s ^ j1) ? 0 : 1;
784 i2 = (s ^ j2) ? 0 : 1;
786 imm32 = (imm11 << 1) | (imm10 << 12) | (i2 << 22) | (i1 << 23);
790 target = code + imm32;
792 /* target now points to the thumb plt entry */
793 /* ldr.w r12, [pc, #8] */
794 g_assert (((guint16*)target) [0] == 0xf8df);
795 g_assert (((guint16*)target) [1] == 0xc008);
798 * The PLT info offset is at offset 16, but mono_arch_get_plt_entry_offset () returns
799 * the 3rd word, so compensate by returning a different value.