3 * JIT trampoline code for ARM
6 * Paolo Molaro (lupus@ximian.com)
8 * (C) 2001-2003 Ximian, Inc.
9 * Copyright 2003-2011 Novell Inc
10 * Copyright 2011 Xamarin Inc
11 * Licensed under the MIT license. See LICENSE file in the project root for full license information.
17 #include <mono/metadata/abi-details.h>
18 #include <mono/metadata/appdomain.h>
19 #include <mono/metadata/marshal.h>
20 #include <mono/metadata/tabledefs.h>
21 #include <mono/metadata/profiler-private.h>
22 #include <mono/arch/arm/arm-codegen.h>
23 #include <mono/arch/arm/arm-vfp-codegen.h>
27 #include "debugger-agent.h"
28 #include "jit-icalls.h"
30 #ifdef ENABLE_INTERPRETER
31 #include "interp/interp.h"
34 #define ALIGN_TO(val,align) ((((guint64)val) + ((align) - 1)) & ~((align) - 1))
37 mono_arch_patch_callsite (guint8 *method_start, guint8 *code_ptr, guint8 *addr)
39 guint32 *code = (guint32*)code_ptr;
41 /* This is the 'bl' or the 'mov pc' instruction */
45 * Note that methods are called also with the bl opcode.
47 if ((((*code) >> 25) & 7) == 5) {
48 /*g_print ("direct patching\n");*/
49 arm_patch ((guint8*)code, addr);
50 mono_arch_flush_icache ((guint8*)code, 4);
54 if ((((*code) >> 20) & 0xFF) == 0x12) {
55 /*g_print ("patching bx\n");*/
56 arm_patch ((guint8*)code, addr);
57 mono_arch_flush_icache ((guint8*)(code - 2), 4);
61 g_assert_not_reached ();
65 mono_arch_patch_plt_entry (guint8 *code, gpointer *got, mgreg_t *regs, guint8 *addr)
69 /* Patch the jump table entry used by the plt entry */
70 if (*(guint32*)code == 0xe59fc000) {
71 /* ARM_LDR_IMM (code, ARMREG_IP, ARMREG_PC, 0); */
72 guint32 offset = ((guint32*)code)[2];
74 jump_entry = code + offset + 12;
75 } else if (*(guint16*)(code - 4) == 0xf8df) {
77 * Thumb PLT entry, begins with ldr.w ip, [pc, #8], code points to entry + 4, see
78 * mono_arm_get_thumb_plt_entry ().
83 offset = *(guint32*)(code + 12);
84 jump_entry = code + offset + 8;
86 g_assert_not_reached ();
89 *(guint8**)jump_entry = addr;
93 mono_arm_handler_block_trampoline_helper (gpointer *ptr)
95 MonoJitTlsData *jit_tls = mono_tls_get_jit_tls ();
96 return jit_tls->handler_block_return_address;
101 #define arm_is_imm12(v) ((int)(v) > -4096 && (int)(v) < 4096)
104 * Return the instruction to jump from code to target, 0 if not
105 * reachable with a single instruction
108 branch_for_target_reachable (guint8 *branch, guint8 *target)
110 gint diff = target - branch - 8;
111 g_assert ((diff & 3) == 0);
113 if (diff <= 33554431)
114 return (ARMCOND_AL << ARMCOND_SHIFT) | (ARM_BR_TAG) | (diff >> 2);
116 /* diff between 0 and -33554432 */
117 if (diff >= -33554432)
118 return (ARMCOND_AL << ARMCOND_SHIFT) | (ARM_BR_TAG) | ((diff >> 2) & ~0xff000000);
123 static inline guint8*
124 emit_bx (guint8* code, int reg)
126 if (mono_arm_thumb_supported ())
129 ARM_MOV_REG_REG (code, ARMREG_PC, reg);
133 /* Stack size for trampoline function
135 #define STACK ALIGN_TO (sizeof (MonoLMF), MONO_ARCH_FRAME_ALIGNMENT)
137 /* Method-specific trampoline code fragment size */
138 #define METHOD_TRAMPOLINE_SIZE 64
140 /* Jump-specific trampoline code fragment size */
141 #define JUMP_TRAMPOLINE_SIZE 64
144 mono_arch_create_generic_trampoline (MonoTrampolineType tramp_type, MonoTrampInfo **info, gboolean aot)
147 guint8 *buf, *code = NULL;
148 guint8 *load_get_lmf_addr = NULL, *load_trampoline = NULL;
150 int i, cfa_offset, regsave_size, lr_offset;
151 GSList *unwind_ops = NULL;
152 MonoJumpInfo *ji = NULL;
155 /* Now we'll create in 'buf' the ARM trampoline code. This
156 is the trampoline code common to all methods */
160 /* Add space for saving/restoring VFP regs. */
161 if (mono_arm_is_hard_float ())
164 code = buf = mono_global_codeman_reserve (buf_len);
167 * At this point lr points to the specific arg and sp points to the saved
168 * regs on the stack (all but PC and SP). The original LR value has been
169 * saved as sp + LR_OFFSET by the push in the specific trampoline
172 /* The size of the area already allocated by the push in the specific trampoline */
173 regsave_size = 14 * sizeof (mgreg_t);
174 /* The offset where lr was saved inside the regsave area */
175 lr_offset = 13 * sizeof (mgreg_t);
177 // CFA = SP + (num registers pushed) * 4
178 cfa_offset = 14 * sizeof (mgreg_t);
179 mono_add_unwind_op_def_cfa (unwind_ops, code, buf, ARMREG_SP, cfa_offset);
180 // PC saved at sp+LR_OFFSET
181 mono_add_unwind_op_offset (unwind_ops, code, buf, ARMREG_LR, -4);
182 /* Callee saved regs */
183 for (i = 0; i < 8; ++i)
184 mono_add_unwind_op_offset (unwind_ops, code, buf, ARMREG_R4 + i, -regsave_size + ((4 + i) * 4));
188 * For page trampolines the data is in r1, so just move it, otherwise use the got slot as below.
189 * The trampoline contains a pc-relative offset to the got slot
190 * preceeding the got slot where the value is stored. The offset can be
193 /* See if emit_trampolines () in aot-compiler.c for the '2' */
195 ARM_MOV_REG_REG (code, ARMREG_V2, ARMREG_R1);
197 ARM_LDR_IMM (code, ARMREG_V2, ARMREG_LR, 0);
198 ARM_ADD_REG_IMM (code, ARMREG_V2, ARMREG_V2, 4, 0);
199 ARM_LDR_REG_REG (code, ARMREG_V2, ARMREG_V2, ARMREG_LR);
202 ARM_LDR_IMM (code, ARMREG_V2, ARMREG_LR, 0);
204 ARM_LDR_IMM (code, ARMREG_V3, ARMREG_SP, lr_offset);
206 /* we build the MonoLMF structure on the stack - see mini-arm.h
207 * The pointer to the struct is put in r1.
208 * the iregs array is already allocated on the stack by push.
210 code = mono_arm_emit_load_imm (code, ARMREG_R2, STACK - regsave_size);
211 ARM_SUB_REG_REG (code, ARMREG_SP, ARMREG_SP, ARMREG_R2);
212 cfa_offset += STACK - regsave_size;
213 mono_add_unwind_op_def_cfa_offset (unwind_ops, code, buf, cfa_offset);
215 code = mono_arm_emit_load_imm (code, ARMREG_R2, STACK - sizeof (MonoLMF));
216 ARM_ADD_REG_REG (code, ARMREG_V1, ARMREG_SP, ARMREG_R2);
218 /* ok, now we can continue with the MonoLMF setup, mostly untouched
219 * from emit_prolog in mini-arm.c
220 * This is a synthetized call to mono_get_lmf_addr ()
223 ji = mono_patch_info_list_prepend (ji, code - buf, MONO_PATCH_INFO_JIT_ICALL_ADDR, "mono_get_lmf_addr");
224 ARM_LDR_IMM (code, ARMREG_R0, ARMREG_PC, 0);
226 *(gpointer*)code = NULL;
228 ARM_LDR_REG_REG (code, ARMREG_R0, ARMREG_PC, ARMREG_R0);
230 load_get_lmf_addr = code;
233 ARM_MOV_REG_REG (code, ARMREG_LR, ARMREG_PC);
234 code = emit_bx (code, ARMREG_R0);
237 * The stack now looks like:
239 * v1 -> <rest of LMF>
243 /* r0 is the result from mono_get_lmf_addr () */
244 ARM_STR_IMM (code, ARMREG_R0, ARMREG_V1, MONO_STRUCT_OFFSET (MonoLMF, lmf_addr));
245 /* new_lmf->previous_lmf = *lmf_addr */
246 ARM_LDR_IMM (code, ARMREG_R2, ARMREG_R0, MONO_STRUCT_OFFSET (MonoLMF, previous_lmf));
247 ARM_STR_IMM (code, ARMREG_R2, ARMREG_V1, MONO_STRUCT_OFFSET (MonoLMF, previous_lmf));
248 /* *(lmf_addr) = r1 */
249 ARM_STR_IMM (code, ARMREG_V1, ARMREG_R0, MONO_STRUCT_OFFSET (MonoLMF, previous_lmf));
250 /* save method info (it's in v2) */
251 if ((tramp_type == MONO_TRAMPOLINE_JIT) || (tramp_type == MONO_TRAMPOLINE_JUMP))
252 ARM_STR_IMM (code, ARMREG_V2, ARMREG_V1, MONO_STRUCT_OFFSET (MonoLMF, method));
254 ARM_MOV_REG_IMM8 (code, ARMREG_R2, 0);
255 ARM_STR_IMM (code, ARMREG_R2, ARMREG_V1, MONO_STRUCT_OFFSET (MonoLMF, method));
258 code = mono_arm_emit_load_imm (code, ARMREG_R2, cfa_offset);
259 ARM_ADD_REG_REG (code, ARMREG_R2, ARMREG_SP, ARMREG_R2);
260 ARM_STR_IMM (code, ARMREG_R2, ARMREG_V1, MONO_STRUCT_OFFSET (MonoLMF, sp));
262 ARM_LDR_IMM (code, ARMREG_R2, ARMREG_V1, (MONO_STRUCT_OFFSET (MonoLMF, iregs) + ARMREG_FP*4));
263 ARM_STR_IMM (code, ARMREG_R2, ARMREG_V1, MONO_STRUCT_OFFSET (MonoLMF, fp));
264 /* save the IP (caller ip) */
265 if (tramp_type == MONO_TRAMPOLINE_JUMP) {
266 ARM_MOV_REG_IMM8 (code, ARMREG_R2, 0);
268 ARM_LDR_IMM (code, ARMREG_R2, ARMREG_V1, (MONO_STRUCT_OFFSET (MonoLMF, iregs) + 13*4));
270 ARM_STR_IMM (code, ARMREG_R2, ARMREG_V1, MONO_STRUCT_OFFSET (MonoLMF, ip));
272 /* Save VFP registers. */
273 if (mono_arm_is_hard_float ()) {
275 * Strictly speaking, we don't have to save d0-d7 in the LMF, but
276 * it's easier than attempting to store them on the stack since
277 * this trampoline code is pretty messy.
279 ARM_ADD_REG_IMM8 (code, ARMREG_R0, ARMREG_V1, MONO_STRUCT_OFFSET (MonoLMF, fregs));
280 ARM_FSTMD (code, ARM_VFP_D0, 8, ARMREG_R0);
284 * Now we're ready to call xxx_trampoline ().
286 /* Arg 1: the saved registers */
287 ARM_ADD_REG_IMM (code, ARMREG_R0, ARMREG_V1, MONO_STRUCT_OFFSET (MonoLMF, iregs), 0);
289 /* Arg 2: code (next address to the instruction that called us) */
290 if (tramp_type == MONO_TRAMPOLINE_JUMP) {
291 ARM_MOV_REG_IMM8 (code, ARMREG_R1, 0);
293 ARM_MOV_REG_REG (code, ARMREG_R1, ARMREG_V3);
296 /* Arg 3: the specific argument, stored in v2
298 ARM_MOV_REG_REG (code, ARMREG_R2, ARMREG_V2);
301 char *icall_name = g_strdup_printf ("trampoline_func_%d", tramp_type);
302 ji = mono_patch_info_list_prepend (ji, code - buf, MONO_PATCH_INFO_JIT_ICALL_ADDR, icall_name);
303 ARM_LDR_IMM (code, ARMREG_IP, ARMREG_PC, 0);
305 *(gpointer*)code = NULL;
307 ARM_LDR_REG_REG (code, ARMREG_IP, ARMREG_PC, ARMREG_IP);
309 load_trampoline = code;
313 ARM_MOV_REG_REG (code, ARMREG_LR, ARMREG_PC);
314 code = emit_bx (code, ARMREG_IP);
316 /* OK, code address is now on r0. Move it to the place on the stack
317 * where IP was saved (it is now no more useful to us and it can be
318 * clobbered). This way we can just restore all the regs in one inst
321 ARM_STR_IMM (code, ARMREG_R0, ARMREG_V1, MONO_STRUCT_OFFSET (MonoLMF, iregs) + (ARMREG_R12 * sizeof (mgreg_t)));
323 /* Check for thread interruption */
324 /* This is not perf critical code so no need to check the interrupt flag */
326 * Have to call the _force_ variant, since there could be a protected wrapper on the top of the stack.
329 ji = mono_patch_info_list_prepend (ji, code - buf, MONO_PATCH_INFO_JIT_ICALL_ADDR, "mono_interruption_checkpoint_from_trampoline");
330 ARM_LDR_IMM (code, ARMREG_IP, ARMREG_PC, 0);
332 *(gpointer*)code = NULL;
334 ARM_LDR_REG_REG (code, ARMREG_IP, ARMREG_PC, ARMREG_IP);
336 ARM_LDR_IMM (code, ARMREG_IP, ARMREG_PC, 0);
338 *(gpointer*)code = mono_interruption_checkpoint_from_trampoline;
341 ARM_MOV_REG_REG (code, ARMREG_LR, ARMREG_PC);
342 code = emit_bx (code, ARMREG_IP);
345 * Now we restore the MonoLMF (see emit_epilogue in mini-arm.c)
346 * and the rest of the registers, so the method called will see
347 * the same state as before we executed.
349 /* ip = previous_lmf */
350 ARM_LDR_IMM (code, ARMREG_IP, ARMREG_V1, MONO_STRUCT_OFFSET (MonoLMF, previous_lmf));
352 ARM_LDR_IMM (code, ARMREG_LR, ARMREG_V1, MONO_STRUCT_OFFSET (MonoLMF, lmf_addr));
353 /* *(lmf_addr) = previous_lmf */
354 ARM_STR_IMM (code, ARMREG_IP, ARMREG_LR, MONO_STRUCT_OFFSET (MonoLMF, previous_lmf));
356 /* Restore VFP registers. */
357 if (mono_arm_is_hard_float ()) {
358 ARM_ADD_REG_IMM8 (code, ARMREG_R0, ARMREG_V1, MONO_STRUCT_OFFSET (MonoLMF, fregs));
359 ARM_FLDMD (code, ARM_VFP_D0, 8, ARMREG_R0);
362 /* Non-standard function epilogue. Instead of doing a proper
363 * return, we just jump to the compiled code.
365 /* Restore the registers and jump to the code:
366 * Note that IP has been conveniently set to the method addr.
368 ARM_ADD_REG_IMM8 (code, ARMREG_SP, ARMREG_SP, STACK - regsave_size);
369 cfa_offset -= STACK - regsave_size;
370 mono_add_unwind_op_def_cfa_offset (unwind_ops, code, buf, cfa_offset);
371 ARM_POP_NWB (code, 0x5fff);
372 mono_add_unwind_op_same_value (unwind_ops, code, buf, ARMREG_LR);
373 if (tramp_type == MONO_TRAMPOLINE_RGCTX_LAZY_FETCH)
374 ARM_MOV_REG_REG (code, ARMREG_R0, ARMREG_IP);
375 ARM_ADD_REG_IMM8 (code, ARMREG_SP, ARMREG_SP, regsave_size);
376 cfa_offset -= regsave_size;
377 g_assert (cfa_offset == 0);
378 mono_add_unwind_op_def_cfa_offset (unwind_ops, code, buf, cfa_offset);
379 if (MONO_TRAMPOLINE_TYPE_MUST_RETURN (tramp_type))
380 code = emit_bx (code, ARMREG_LR);
382 code = emit_bx (code, ARMREG_IP);
384 constants = (gpointer*)code;
385 constants [0] = mono_get_lmf_addr;
386 constants [1] = (gpointer)mono_get_trampoline_func (tramp_type);
389 /* backpatch by emitting the missing instructions skipped above */
390 ARM_LDR_IMM (load_get_lmf_addr, ARMREG_R0, ARMREG_PC, (code - load_get_lmf_addr - 8));
391 ARM_LDR_IMM (load_trampoline, ARMREG_IP, ARMREG_PC, (code + 4 - load_trampoline - 8));
396 /* Flush instruction cache, since we've generated code */
397 mono_arch_flush_icache (buf, code - buf);
398 mono_profiler_code_buffer_new (buf, code - buf, MONO_PROFILER_CODE_BUFFER_HELPER, NULL);
401 g_assert ((code - buf) <= buf_len);
404 tramp_name = mono_get_generic_trampoline_name (tramp_type);
405 *info = mono_tramp_info_create (tramp_name, buf, code - buf, ji, unwind_ops);
411 #define SPEC_TRAMP_SIZE 24
414 mono_arch_create_specific_trampoline (gpointer arg1, MonoTrampolineType tramp_type, MonoDomain *domain, guint32 *code_len)
416 guint8 *code, *buf, *tramp;
418 guint32 short_branch = FALSE;
419 guint32 size = SPEC_TRAMP_SIZE;
421 tramp = mono_get_trampoline_code (tramp_type);
424 mono_domain_lock (domain);
425 code = buf = mono_domain_code_reserve_align (domain, size, 4);
426 if ((short_branch = branch_for_target_reachable (code + 4, tramp))) {
428 mono_domain_code_commit (domain, code, SPEC_TRAMP_SIZE, size);
430 mono_domain_unlock (domain);
432 code = buf = mono_global_codeman_reserve (size);
433 short_branch = FALSE;
436 /* we could reduce this to 12 bytes if tramp is within reach:
440 * The called code can access method using the lr register
441 * A 20 byte sequence could be:
443 * ARM_MOV_REG_REG (lr, pc)
444 * ARM_LDR_IMM (pc, pc, 0)
448 /* We save all the registers, except PC and SP */
449 ARM_PUSH (code, 0x5fff);
451 constants = (gpointer*)code;
452 constants [0] = GUINT_TO_POINTER (short_branch | (1 << 24));
453 constants [1] = arg1;
456 ARM_LDR_IMM (code, ARMREG_R1, ARMREG_PC, 8); /* temp reg */
457 ARM_MOV_REG_REG (code, ARMREG_LR, ARMREG_PC);
458 code = emit_bx (code, ARMREG_R1);
460 constants = (gpointer*)code;
461 constants [0] = arg1;
462 constants [1] = tramp;
466 /* Flush instruction cache, since we've generated code */
467 mono_arch_flush_icache (buf, code - buf);
468 mono_profiler_code_buffer_new (buf, code - buf, MONO_PROFILER_CODE_BUFFER_SPECIFIC_TRAMPOLINE, mono_get_generic_trampoline_simple_name (tramp_type));
470 g_assert ((code - buf) <= size);
473 *code_len = code - buf;
479 * mono_arch_get_unbox_trampoline:
481 * @addr: pointer to native code for @m
483 * when value type methods are called through the vtable we need to unbox the
484 * this argument. This method returns a pointer to a trampoline which does
485 * unboxing before calling the method
488 mono_arch_get_unbox_trampoline (MonoMethod *m, gpointer addr)
490 guint8 *code, *start;
491 MonoDomain *domain = mono_domain_get ();
495 start = code = mono_domain_code_reserve (domain, size);
497 unwind_ops = mono_arch_get_cie_program ();
499 ARM_LDR_IMM (code, ARMREG_IP, ARMREG_PC, 4);
500 ARM_ADD_REG_IMM8 (code, ARMREG_R0, ARMREG_R0, sizeof (MonoObject));
501 code = emit_bx (code, ARMREG_IP);
502 *(guint32*)code = (guint32)addr;
504 mono_arch_flush_icache (start, code - start);
505 mono_profiler_code_buffer_new (start, code - start, MONO_PROFILER_CODE_BUFFER_UNBOX_TRAMPOLINE, m);
506 g_assert ((code - start) <= size);
507 /*g_print ("unbox trampoline at %d for %s:%s\n", this_pos, m->klass->name, m->name);
508 g_print ("unbox code is at %p for method at %p\n", start, addr);*/
510 mono_tramp_info_register (mono_tramp_info_create (NULL, start, code - start, NULL, unwind_ops), domain);
516 mono_arch_get_static_rgctx_trampoline (gpointer arg, gpointer addr)
518 guint8 *code, *start;
521 MonoDomain *domain = mono_domain_get ();
523 start = code = mono_domain_code_reserve (domain, buf_len);
525 unwind_ops = mono_arch_get_cie_program ();
527 ARM_LDR_IMM (code, MONO_ARCH_RGCTX_REG, ARMREG_PC, 0);
528 ARM_LDR_IMM (code, ARMREG_PC, ARMREG_PC, 0);
529 *(guint32*)code = (guint32)arg;
531 *(guint32*)code = (guint32)addr;
534 g_assert ((code - start) <= buf_len);
536 mono_arch_flush_icache (start, code - start);
537 mono_profiler_code_buffer_new (start, code - start, MONO_PROFILER_CODE_BUFFER_GENERICS_TRAMPOLINE, NULL);
539 mono_tramp_info_register (mono_tramp_info_create (NULL, start, code - start, NULL, unwind_ops), domain);
545 mono_arch_create_rgctx_lazy_fetch_trampoline (guint32 slot, MonoTrampInfo **info, gboolean aot)
551 guint8 **rgctx_null_jumps;
555 MonoJumpInfo *ji = NULL;
556 GSList *unwind_ops = NULL;
558 mrgctx = MONO_RGCTX_SLOT_IS_MRGCTX (slot);
559 index = MONO_RGCTX_SLOT_INDEX (slot);
561 index += MONO_SIZEOF_METHOD_RUNTIME_GENERIC_CONTEXT / sizeof (gpointer);
562 for (depth = 0; ; ++depth) {
563 int size = mono_class_rgctx_get_array_size (depth, mrgctx);
565 if (index < size - 1)
570 tramp_size = 64 + 16 * depth;
572 code = buf = mono_global_codeman_reserve (tramp_size);
574 unwind_ops = mono_arch_get_cie_program ();
576 rgctx_null_jumps = g_malloc (sizeof (guint8*) * (depth + 2));
579 /* The vtable/mrgctx is in R0 */
580 g_assert (MONO_ARCH_VTABLE_REG == ARMREG_R0);
584 ARM_MOV_REG_REG (code, ARMREG_R1, ARMREG_R0);
586 /* load rgctx ptr from vtable */
587 g_assert (arm_is_imm12 (MONO_STRUCT_OFFSET (MonoVTable, runtime_generic_context)));
588 ARM_LDR_IMM (code, ARMREG_R1, ARMREG_R0, MONO_STRUCT_OFFSET (MonoVTable, runtime_generic_context));
589 /* is the rgctx ptr null? */
590 ARM_CMP_REG_IMM (code, ARMREG_R1, 0, 0);
591 /* if yes, jump to actual trampoline */
592 rgctx_null_jumps [njumps ++] = code;
593 ARM_B_COND (code, ARMCOND_EQ, 0);
596 for (i = 0; i < depth; ++i) {
597 /* load ptr to next array */
598 if (mrgctx && i == 0) {
599 g_assert (arm_is_imm12 (MONO_SIZEOF_METHOD_RUNTIME_GENERIC_CONTEXT));
600 ARM_LDR_IMM (code, ARMREG_R1, ARMREG_R1, MONO_SIZEOF_METHOD_RUNTIME_GENERIC_CONTEXT);
602 ARM_LDR_IMM (code, ARMREG_R1, ARMREG_R1, 0);
604 /* is the ptr null? */
605 ARM_CMP_REG_IMM (code, ARMREG_R1, 0, 0);
606 /* if yes, jump to actual trampoline */
607 rgctx_null_jumps [njumps ++] = code;
608 ARM_B_COND (code, ARMCOND_EQ, 0);
612 code = mono_arm_emit_load_imm (code, ARMREG_R2, sizeof (gpointer) * (index + 1));
613 ARM_LDR_REG_REG (code, ARMREG_R1, ARMREG_R1, ARMREG_R2);
614 /* is the slot null? */
615 ARM_CMP_REG_IMM (code, ARMREG_R1, 0, 0);
616 /* if yes, jump to actual trampoline */
617 rgctx_null_jumps [njumps ++] = code;
618 ARM_B_COND (code, ARMCOND_EQ, 0);
619 /* otherwise return, result is in R1 */
620 ARM_MOV_REG_REG (code, ARMREG_R0, ARMREG_R1);
621 code = emit_bx (code, ARMREG_LR);
623 g_assert (njumps <= depth + 2);
624 for (i = 0; i < njumps; ++i)
625 arm_patch (rgctx_null_jumps [i], code);
627 g_free (rgctx_null_jumps);
631 /* The vtable/mrgctx is still in R0 */
634 ji = mono_patch_info_list_prepend (ji, code - buf, MONO_PATCH_INFO_JIT_ICALL_ADDR, g_strdup_printf ("specific_trampoline_lazy_fetch_%u", slot));
635 ARM_LDR_IMM (code, ARMREG_R1, ARMREG_PC, 0);
637 *(gpointer*)code = NULL;
639 ARM_LDR_REG_REG (code, ARMREG_PC, ARMREG_PC, ARMREG_R1);
641 tramp = mono_arch_create_specific_trampoline (GUINT_TO_POINTER (slot), MONO_TRAMPOLINE_RGCTX_LAZY_FETCH, mono_get_root_domain (), &code_len);
643 /* Jump to the actual trampoline */
644 ARM_LDR_IMM (code, ARMREG_R1, ARMREG_PC, 0); /* temp reg */
645 code = emit_bx (code, ARMREG_R1);
646 *(gpointer*)code = tramp;
650 mono_arch_flush_icache (buf, code - buf);
651 mono_profiler_code_buffer_new (buf, code - buf, MONO_PROFILER_CODE_BUFFER_GENERICS_TRAMPOLINE, NULL);
653 g_assert (code - buf <= tramp_size);
655 char *name = mono_get_rgctx_fetch_trampoline_name (slot);
656 *info = mono_tramp_info_create (name, buf, code - buf, ji, unwind_ops);
663 mono_arch_create_general_rgctx_lazy_fetch_trampoline (MonoTrampInfo **info, gboolean aot)
667 MonoJumpInfo *ji = NULL;
668 GSList *unwind_ops = NULL;
674 code = buf = mono_global_codeman_reserve (tramp_size);
676 unwind_ops = mono_arch_get_cie_program ();
678 // FIXME: Currently, we always go to the slow path.
679 /* Load trampoline addr */
680 ARM_LDR_IMM (code, ARMREG_R1, MONO_ARCH_RGCTX_REG, 4);
681 /* The vtable/mrgctx is in R0 */
682 g_assert (MONO_ARCH_VTABLE_REG == ARMREG_R0);
683 code = emit_bx (code, ARMREG_R1);
685 mono_arch_flush_icache (buf, code - buf);
686 mono_profiler_code_buffer_new (buf, code - buf, MONO_PROFILER_CODE_BUFFER_GENERICS_TRAMPOLINE, NULL);
688 g_assert (code - buf <= tramp_size);
690 *info = mono_tramp_info_create ("rgctx_fetch_trampoline_general", buf, code - buf, ji, unwind_ops);
696 mono_arch_create_handler_block_trampoline (MonoTrampInfo **info, gboolean aot)
701 MonoJumpInfo *ji = NULL;
702 GSList *unwind_ops = NULL;
704 code = buf = mono_global_codeman_reserve (tramp_size);
706 unwind_ops = mono_arch_get_cie_program ();
708 tramp = mono_arch_create_specific_trampoline (NULL, MONO_TRAMPOLINE_HANDLER_BLOCK_GUARD, NULL, NULL);
711 This trampoline restore the call chain of the handler block then jumps into the code that deals with it.
715 * We are in a method frame after the call emitted by OP_CALL_HANDLER.
717 /* Obtain jit_tls->handler_block_return_address */
719 code = mono_arm_emit_aotconst (&ji, code, buf, ARMREG_R0, MONO_PATCH_INFO_JIT_ICALL_ADDR, "mono_arm_handler_block_trampoline_helper");
722 ARM_LDR_IMM (code, ARMREG_R0, ARMREG_PC, 0);
724 *(gpointer*)code = mono_arm_handler_block_trampoline_helper;
728 /* Set it as the return address so the trampoline will return to it */
729 ARM_MOV_REG_REG (code, ARMREG_LR, ARMREG_R0);
731 /* Call the trampoline */
733 char *name = g_strdup_printf ("trampoline_func_%d", MONO_TRAMPOLINE_HANDLER_BLOCK_GUARD);
734 code = mono_arm_emit_aotconst (&ji, code, buf, ARMREG_R0, MONO_PATCH_INFO_JIT_ICALL_ADDR, name);
735 code = emit_bx (code, ARMREG_R0);
737 ARM_LDR_IMM (code, ARMREG_R0, ARMREG_PC, 0);
738 code = emit_bx (code, ARMREG_R0);
739 *(gpointer*)code = tramp;
743 mono_arch_flush_icache (buf, code - buf);
744 mono_profiler_code_buffer_new (buf, code - buf, MONO_PROFILER_CODE_BUFFER_HELPER, NULL);
745 g_assert (code - buf <= tramp_size);
747 *info = mono_tramp_info_create ("handler_block_trampoline", buf, code - buf, ji, unwind_ops);
753 mono_arch_create_sdb_trampoline (gboolean single_step, MonoTrampInfo **info, gboolean aot)
756 GSList *unwind_ops = NULL;
757 MonoJumpInfo *ji = NULL;
760 buf = code = mono_global_codeman_reserve (96);
763 * Construct the MonoContext structure on the stack.
766 frame_size = sizeof (MonoContext);
767 frame_size = ALIGN_TO (frame_size, MONO_ARCH_FRAME_ALIGNMENT);
768 ARM_SUB_REG_IMM8 (code, ARMREG_SP, ARMREG_SP, frame_size);
770 /* save ip, lr and pc into their correspodings ctx.regs slots. */
771 ARM_STR_IMM (code, ARMREG_IP, ARMREG_SP, MONO_STRUCT_OFFSET (MonoContext, regs) + sizeof (mgreg_t) * ARMREG_IP);
772 ARM_STR_IMM (code, ARMREG_LR, ARMREG_SP, MONO_STRUCT_OFFSET (MonoContext, regs) + 4 * ARMREG_LR);
773 ARM_STR_IMM (code, ARMREG_LR, ARMREG_SP, MONO_STRUCT_OFFSET (MonoContext, regs) + 4 * ARMREG_PC);
775 /* save r0..r10 and fp */
776 ARM_ADD_REG_IMM8 (code, ARMREG_IP, ARMREG_SP, MONO_STRUCT_OFFSET (MonoContext, regs));
777 ARM_STM (code, ARMREG_IP, 0x0fff);
779 /* now we can update fp. */
780 ARM_MOV_REG_REG (code, ARMREG_FP, ARMREG_SP);
782 /* make ctx.esp hold the actual value of sp at the beginning of this method. */
783 ARM_ADD_REG_IMM8 (code, ARMREG_R0, ARMREG_FP, frame_size);
784 ARM_STR_IMM (code, ARMREG_R0, ARMREG_IP, 4 * ARMREG_SP);
785 ARM_STR_IMM (code, ARMREG_R0, ARMREG_FP, MONO_STRUCT_OFFSET (MonoContext, regs) + 4 * ARMREG_SP);
787 /* make ctx.eip hold the address of the call. */
788 //ARM_SUB_REG_IMM8 (code, ARMREG_LR, ARMREG_LR, 4);
789 ARM_STR_IMM (code, ARMREG_LR, ARMREG_FP, MONO_STRUCT_OFFSET (MonoContext, pc));
791 /* r0 now points to the MonoContext */
792 ARM_MOV_REG_REG (code, ARMREG_R0, ARMREG_FP);
797 ji = mono_patch_info_list_prepend (ji, code - buf, MONO_PATCH_INFO_JIT_ICALL_ADDR, "debugger_agent_single_step_from_context");
799 ji = mono_patch_info_list_prepend (ji, code - buf, MONO_PATCH_INFO_JIT_ICALL_ADDR, "debugger_agent_breakpoint_from_context");
800 ARM_LDR_IMM (code, ARMREG_IP, ARMREG_PC, 0);
802 *(gpointer*)code = NULL;
804 ARM_LDR_REG_REG (code, ARMREG_IP, ARMREG_PC, ARMREG_IP);
805 ARM_BLX_REG (code, ARMREG_IP);
807 ARM_LDR_IMM (code, ARMREG_IP, ARMREG_PC, 0);
810 *(gpointer*)code = debugger_agent_single_step_from_context;
812 *(gpointer*)code = debugger_agent_breakpoint_from_context;
814 ARM_BLX_REG (code, ARMREG_IP);
817 /* we're back; save ctx.eip and ctx.esp into the corresponding regs slots. */
818 ARM_LDR_IMM (code, ARMREG_R0, ARMREG_FP, MONO_STRUCT_OFFSET (MonoContext, pc));
819 ARM_STR_IMM (code, ARMREG_R0, ARMREG_FP, MONO_STRUCT_OFFSET (MonoContext, regs) + 4 * ARMREG_LR);
820 ARM_STR_IMM (code, ARMREG_R0, ARMREG_FP, MONO_STRUCT_OFFSET (MonoContext, regs) + 4 * ARMREG_PC);
822 /* make ip point to the regs array, then restore everything, including pc. */
823 ARM_ADD_REG_IMM8 (code, ARMREG_IP, ARMREG_FP, MONO_STRUCT_OFFSET (MonoContext, regs));
824 ARM_LDM (code, ARMREG_IP, 0xffff);
826 mono_arch_flush_icache (buf, code - buf);
827 mono_profiler_code_buffer_new (buf, code - buf, MONO_PROFILER_CODE_BUFFER_HELPER, NULL);
829 const char *tramp_name = single_step ? "sdb_single_step_trampoline" : "sdb_breakpoint_trampoline";
830 *info = mono_tramp_info_create (tramp_name, buf, code - buf, ji, unwind_ops);
836 * mono_arch_get_enter_icall_trampoline:
838 * See tramp-amd64.c for documentation.
841 mono_arch_get_enter_icall_trampoline (MonoTrampInfo **info)
843 #ifdef ENABLE_INTERPRETER
844 const int gregs_num = INTERP_ICALL_TRAMP_IARGS;
845 const int fregs_num = INTERP_ICALL_TRAMP_FARGS;
847 guint8 *start = NULL, *code, *label_gexits [gregs_num], *label_fexits [fregs_num], *label_leave_tramp [3], *label_is_float_ret;
848 MonoJumpInfo *ji = NULL;
849 GSList *unwind_ops = NULL;
850 int buf_len, i, framesize = 0, off_methodargs, off_targetaddr;
851 const int fp_reg = ARMREG_R7;
853 buf_len = 512 + 1024;
854 start = code = (guint8 *) mono_global_codeman_reserve (buf_len);
856 off_methodargs = framesize;
857 framesize += sizeof (mgreg_t);
859 off_targetaddr = framesize;
860 framesize += sizeof (mgreg_t);
862 framesize = ALIGN_TO (framesize, MONO_ARCH_FRAME_ALIGNMENT);
864 /* allocate space on stack for argument passing */
865 const int stack_space = ALIGN_TO (((gregs_num - ARMREG_R3) * sizeof (mgreg_t)), MONO_ARCH_FRAME_ALIGNMENT);
867 /* use r4, r5 and r6 as scratch registers */
868 ARM_PUSH (code, (1 << fp_reg) | (1 << ARMREG_LR) | (1 << ARMREG_R4) | (1 << ARMREG_R5) | (1 << ARMREG_R6));
869 ARM_MOV_REG_REG (code, fp_reg, ARMREG_SP);
870 ARM_SUB_REG_IMM8 (code, ARMREG_SP, ARMREG_SP, stack_space + framesize);
872 /* save InterpMethodArguments* onto stack */
873 ARM_STR_IMM (code, ARMREG_R1, fp_reg, off_methodargs);
875 /* save target address onto stack */
876 ARM_STR_IMM (code, ARMREG_R0, fp_reg, off_targetaddr);
878 /* load pointer to InterpMethodArguments* into r4 */
879 ARM_MOV_REG_REG (code, ARMREG_R4, ARMREG_R1);
881 /* move flen into r5 */
882 ARM_LDR_IMM (code, ARMREG_R5, ARMREG_R4, MONO_STRUCT_OFFSET (InterpMethodArguments, flen));
883 /* load pointer to fargs into r6 */
884 ARM_LDR_IMM (code, ARMREG_R6, ARMREG_R4, MONO_STRUCT_OFFSET (InterpMethodArguments, fargs));
886 for (i = 0; i < fregs_num; ++i) {
887 ARM_CMP_REG_IMM (code, ARMREG_R5, 0, 0);
888 label_fexits [i] = code;
889 ARM_B_COND (code, ARMCOND_EQ, 0);
891 g_assert (i <= ARM_VFP_D7); /* otherwise, need to pass args on stack */
892 ARM_FLDD (code, i, ARMREG_R6, i * sizeof (double));
893 ARM_SUB_REG_IMM8 (code, ARMREG_R5, ARMREG_R5, 1);
896 for (i = 0; i < fregs_num; i++)
897 arm_patch (label_fexits [i], code);
899 /* move ilen into r5 */
900 ARM_LDR_IMM (code, ARMREG_R5, ARMREG_R4, MONO_STRUCT_OFFSET (InterpMethodArguments, ilen));
901 /* load pointer to iargs into r6 */
902 ARM_LDR_IMM (code, ARMREG_R6, ARMREG_R4, MONO_STRUCT_OFFSET (InterpMethodArguments, iargs));
904 int stack_offset = 0;
905 for (i = 0; i < gregs_num; i++) {
906 ARM_CMP_REG_IMM (code, ARMREG_R5, 0, 0);
907 label_gexits [i] = code;
908 ARM_B_COND (code, ARMCOND_EQ, 0);
910 if (i <= ARMREG_R3) {
911 ARM_LDR_IMM (code, i, ARMREG_R6, i * sizeof (mgreg_t));
913 ARM_LDR_IMM (code, ARMREG_R4, ARMREG_R6, i * sizeof (mgreg_t));
914 ARM_STR_IMM (code, ARMREG_R4, ARMREG_SP, stack_offset);
915 stack_offset += sizeof (mgreg_t);
917 ARM_SUB_REG_IMM8 (code, ARMREG_R5, ARMREG_R5, 1);
920 for (i = 0; i < gregs_num; i++)
921 arm_patch (label_gexits [i], code);
923 /* load target addr */
924 ARM_LDR_IMM (code, ARMREG_R4, fp_reg, off_targetaddr);
926 /* call into native function */
927 ARM_BLX_REG (code, ARMREG_R4);
929 /* load InterpMethodArguments */
930 ARM_LDR_IMM (code, ARMREG_R4, fp_reg, off_methodargs);
932 /* load is_float_ret */
933 ARM_LDR_IMM (code, ARMREG_R5, ARMREG_R4, MONO_STRUCT_OFFSET (InterpMethodArguments, is_float_ret));
935 /* check if a float return value is expected */
936 ARM_CMP_REG_IMM (code, ARMREG_R5, 0, 0);
937 label_is_float_ret = code;
938 ARM_B_COND (code, ARMCOND_NE, 0);
942 ARM_LDR_IMM (code, ARMREG_R5, ARMREG_R4, MONO_STRUCT_OFFSET (InterpMethodArguments, retval));
944 ARM_CMP_REG_IMM (code, ARMREG_R5, 0, 0);
945 label_leave_tramp [0] = code;
946 ARM_B_COND (code, ARMCOND_EQ, 0);
948 /* store greg result, always write back 64bit */
949 ARM_STR_IMM (code, ARMREG_R0, ARMREG_R5, 0);
950 ARM_STR_IMM (code, ARMREG_R1, ARMREG_R5, 4);
952 label_leave_tramp [1] = code;
953 ARM_B_COND (code, ARMCOND_AL, 0);
956 arm_patch (label_is_float_ret, code);
958 ARM_LDR_IMM (code, ARMREG_R5, ARMREG_R4, MONO_STRUCT_OFFSET (InterpMethodArguments, retval));
960 ARM_CMP_REG_IMM (code, ARMREG_R5, 0, 0);
961 label_leave_tramp [2] = code;
962 ARM_B_COND (code, ARMCOND_EQ, 0);
964 /* store freg result */
965 ARM_FSTD (code, ARM_VFP_F0, ARMREG_R5, 0);
967 for (i = 0; i < 3; i++)
968 arm_patch (label_leave_tramp [i], code);
970 ARM_MOV_REG_REG (code, ARMREG_SP, fp_reg);
971 ARM_POP (code, (1 << fp_reg) | (1 << ARMREG_PC) | (1 << ARMREG_R4) | (1 << ARMREG_R5) | (1 << ARMREG_R6));
973 g_assert (code - start < buf_len);
975 mono_arch_flush_icache (start, code - start);
976 mono_profiler_code_buffer_new (start, code - start, MONO_PROFILER_CODE_BUFFER_EXCEPTION_HANDLING, NULL);
979 *info = mono_tramp_info_create ("enter_icall_trampoline", start, code - start, ji, unwind_ops);
983 g_assert_not_reached ();
985 #endif /* ENABLE_INTERPRETER */
991 mono_arch_create_generic_trampoline (MonoTrampolineType tramp_type, MonoTrampInfo **info, gboolean aot)
993 g_assert_not_reached ();
998 mono_arch_create_specific_trampoline (gpointer arg1, MonoTrampolineType tramp_type, MonoDomain *domain, guint32 *code_len)
1000 g_assert_not_reached ();
1005 mono_arch_get_unbox_trampoline (MonoMethod *m, gpointer addr)
1007 g_assert_not_reached ();
1012 mono_arch_get_static_rgctx_trampoline (gpointer arg, gpointer addr)
1014 g_assert_not_reached ();
1019 mono_arch_create_rgctx_lazy_fetch_trampoline (guint32 slot, MonoTrampInfo **info, gboolean aot)
1021 g_assert_not_reached ();
1026 mono_arch_create_handler_block_trampoline (MonoTrampInfo **info, gboolean aot)
1028 g_assert_not_reached ();
1033 mono_arch_create_sdb_trampoline (gboolean single_step, MonoTrampInfo **info, gboolean aot)
1035 g_assert_not_reached ();
1039 #endif /* DISABLE_JIT */
1042 mono_arch_get_call_target (guint8 *code)
1044 guint32 ins = ((guint32*)(gpointer)code) [-1];
1046 /* Should be a 'bl' or a 'b' */
1047 if (((ins >> 25) & 0x7) == 0x5) {
1048 gint32 disp = ((((gint32)ins) & 0xffffff) << 8) >> 8;
1049 guint8 *target = code - 4 + 8 + (disp * 4);
1058 mono_arch_get_plt_info_offset (guint8 *plt_entry, mgreg_t *regs, guint8 *code)
1060 /* The offset is stored as the 4th word of the plt entry */
1061 return ((guint32*)plt_entry) [3];
1065 * Return the address of the PLT entry called by the thumb code CODE.
1068 mono_arm_get_thumb_plt_entry (guint8 *code)
1070 int s, j1, j2, imm10, imm11, i1, i2, imm32;
1075 /* code should be right after a BL */
1076 code = (guint8*)((mgreg_t)code & ~1);
1077 base = (guint8*)((mgreg_t)code & ~3);
1079 t1 = ((guint16*)bl) [0];
1080 t2 = ((guint16*)bl) [1];
1082 g_assert ((t1 >> 11) == 0x1e);
1084 s = (t1 >> 10) & 0x1;
1085 imm10 = (t1 >> 0) & 0x3ff;
1086 j1 = (t2 >> 13) & 0x1;
1087 j2 = (t2 >> 11) & 0x1;
1090 i1 = (s ^ j1) ? 0 : 1;
1091 i2 = (s ^ j2) ? 0 : 1;
1093 imm32 = (imm11 << 1) | (imm10 << 12) | (i2 << 22) | (i1 << 23);
1095 /* Sign extend from 24 bits to 32 bits */
1096 imm32 = ((gint32)imm32 << 8) >> 8;
1098 target = code + imm32;
1100 /* target now points to the thumb plt entry */
1101 /* ldr.w r12, [pc, #8] */
1102 g_assert (((guint16*)target) [0] == 0xf8df);
1103 g_assert (((guint16*)target) [1] == 0xc008);
1106 * The PLT info offset is at offset 16, but mono_arch_get_plt_entry_offset () returns
1107 * the 3rd word, so compensate by returning a different value.
1117 * mono_arch_get_gsharedvt_arg_trampoline:
1119 * See tramp-x86.c for documentation.
1122 mono_arch_get_gsharedvt_arg_trampoline (MonoDomain *domain, gpointer arg, gpointer addr)
1126 gpointer *constants;
1130 buf = code = mono_domain_code_reserve (domain, buf_len);
1132 /* Similar to the specialized trampoline code */
1133 ARM_PUSH (code, (1 << ARMREG_R0) | (1 << ARMREG_R1) | (1 << ARMREG_R2) | (1 << ARMREG_R3) | (1 << ARMREG_LR));
1134 ARM_LDR_IMM (code, ARMREG_IP, ARMREG_PC, 8);
1135 /* arg is passed in LR */
1136 ARM_LDR_IMM (code, ARMREG_LR, ARMREG_PC, 0);
1137 code = emit_bx (code, ARMREG_IP);
1138 constants = (gpointer*)code;
1139 constants [0] = arg;
1140 constants [1] = addr;
1143 g_assert ((code - buf) <= buf_len);
1145 mono_arch_flush_icache (buf, code - buf);
1146 mono_profiler_code_buffer_new (buf, code - buf, MONO_PROFILER_CODE_BUFFER_GENERICS_TRAMPOLINE, NULL);
1148 mono_tramp_info_register (mono_tramp_info_create (NULL, buf, code - buf, NULL, NULL), domain);
1156 mono_arch_get_gsharedvt_arg_trampoline (MonoDomain *domain, gpointer arg, gpointer addr)
1158 g_assert_not_reached ();