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 #define ALIGN_TO(val,align) ((((guint64)val) + ((align) - 1)) & ~((align) - 1))
33 mono_arch_patch_callsite (guint8 *method_start, guint8 *code_ptr, guint8 *addr)
35 guint32 *code = (guint32*)code_ptr;
37 /* This is the 'bl' or the 'mov pc' instruction */
41 * Note that methods are called also with the bl opcode.
43 if ((((*code) >> 25) & 7) == 5) {
44 /*g_print ("direct patching\n");*/
45 arm_patch ((guint8*)code, addr);
46 mono_arch_flush_icache ((guint8*)code, 4);
50 if ((((*code) >> 20) & 0xFF) == 0x12) {
51 /*g_print ("patching bx\n");*/
52 arm_patch ((guint8*)code, addr);
53 mono_arch_flush_icache ((guint8*)(code - 2), 4);
57 g_assert_not_reached ();
61 mono_arch_patch_plt_entry (guint8 *code, gpointer *got, mgreg_t *regs, guint8 *addr)
65 /* Patch the jump table entry used by the plt entry */
66 if (*(guint32*)code == 0xe59fc000) {
67 /* ARM_LDR_IMM (code, ARMREG_IP, ARMREG_PC, 0); */
68 guint32 offset = ((guint32*)code)[2];
70 jump_entry = code + offset + 12;
71 } else if (*(guint16*)(code - 4) == 0xf8df) {
73 * Thumb PLT entry, begins with ldr.w ip, [pc, #8], code points to entry + 4, see
74 * mono_arm_get_thumb_plt_entry ().
79 offset = *(guint32*)(code + 12);
80 jump_entry = code + offset + 8;
82 g_assert_not_reached ();
85 *(guint8**)jump_entry = addr;
89 mono_arm_handler_block_trampoline_helper (gpointer *ptr)
91 MonoJitTlsData *jit_tls = mono_tls_get_jit_tls ();
92 return jit_tls->handler_block_return_address;
97 #define arm_is_imm12(v) ((int)(v) > -4096 && (int)(v) < 4096)
100 * Return the instruction to jump from code to target, 0 if not
101 * reachable with a single instruction
104 branch_for_target_reachable (guint8 *branch, guint8 *target)
106 gint diff = target - branch - 8;
107 g_assert ((diff & 3) == 0);
109 if (diff <= 33554431)
110 return (ARMCOND_AL << ARMCOND_SHIFT) | (ARM_BR_TAG) | (diff >> 2);
112 /* diff between 0 and -33554432 */
113 if (diff >= -33554432)
114 return (ARMCOND_AL << ARMCOND_SHIFT) | (ARM_BR_TAG) | ((diff >> 2) & ~0xff000000);
119 static inline guint8*
120 emit_bx (guint8* code, int reg)
122 if (mono_arm_thumb_supported ())
125 ARM_MOV_REG_REG (code, ARMREG_PC, reg);
129 /* Stack size for trampoline function
131 #define STACK ALIGN_TO (sizeof (MonoLMF), MONO_ARCH_FRAME_ALIGNMENT)
133 /* Method-specific trampoline code fragment size */
134 #define METHOD_TRAMPOLINE_SIZE 64
136 /* Jump-specific trampoline code fragment size */
137 #define JUMP_TRAMPOLINE_SIZE 64
140 mono_arch_create_generic_trampoline (MonoTrampolineType tramp_type, MonoTrampInfo **info, gboolean aot)
143 guint8 *buf, *code = NULL;
144 guint8 *load_get_lmf_addr = NULL, *load_trampoline = NULL;
146 int i, cfa_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 */
156 /* Add space for saving/restoring VFP regs. */
157 if (mono_arm_is_hard_float ())
160 code = buf = mono_global_codeman_reserve (buf_len);
163 * At this point lr points to the specific arg and sp points to the saved
164 * regs on the stack (all but PC and SP). The original LR value has been
165 * saved as sp + LR_OFFSET by the push in the specific trampoline
168 /* The size of the area already allocated by the push in the specific trampoline */
169 regsave_size = 14 * sizeof (mgreg_t);
170 /* The offset where lr was saved inside the regsave area */
171 lr_offset = 13 * sizeof (mgreg_t);
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);
178 /* Callee saved regs */
179 for (i = 0; i < 8; ++i)
180 mono_add_unwind_op_offset (unwind_ops, code, buf, ARMREG_R4 + i, -regsave_size + ((4 + i) * 4));
184 * For page trampolines the data is in r1, so just move it, otherwise use the got slot as below.
185 * The trampoline contains a pc-relative offset to the got slot
186 * preceeding the got slot where the value is stored. The offset can be
189 /* See if emit_trampolines () in aot-compiler.c for the '2' */
191 ARM_MOV_REG_REG (code, ARMREG_V2, ARMREG_R1);
193 ARM_LDR_IMM (code, ARMREG_V2, ARMREG_LR, 0);
194 ARM_ADD_REG_IMM (code, ARMREG_V2, ARMREG_V2, 4, 0);
195 ARM_LDR_REG_REG (code, ARMREG_V2, ARMREG_V2, ARMREG_LR);
198 ARM_LDR_IMM (code, ARMREG_V2, ARMREG_LR, 0);
200 ARM_LDR_IMM (code, ARMREG_V3, ARMREG_SP, lr_offset);
202 /* we build the MonoLMF structure on the stack - see mini-arm.h
203 * The pointer to the struct is put in r1.
204 * the iregs array is already allocated on the stack by push.
206 code = mono_arm_emit_load_imm (code, ARMREG_R2, STACK - regsave_size);
207 ARM_SUB_REG_REG (code, ARMREG_SP, ARMREG_SP, ARMREG_R2);
208 cfa_offset += STACK - regsave_size;
209 mono_add_unwind_op_def_cfa_offset (unwind_ops, code, buf, cfa_offset);
211 code = mono_arm_emit_load_imm (code, ARMREG_R2, STACK - sizeof (MonoLMF));
212 ARM_ADD_REG_REG (code, ARMREG_V1, ARMREG_SP, ARMREG_R2);
214 /* ok, now we can continue with the MonoLMF setup, mostly untouched
215 * from emit_prolog in mini-arm.c
216 * This is a synthetized call to mono_get_lmf_addr ()
219 ji = mono_patch_info_list_prepend (ji, code - buf, MONO_PATCH_INFO_JIT_ICALL_ADDR, "mono_get_lmf_addr");
220 ARM_LDR_IMM (code, ARMREG_R0, ARMREG_PC, 0);
222 *(gpointer*)code = NULL;
224 ARM_LDR_REG_REG (code, ARMREG_R0, ARMREG_PC, ARMREG_R0);
226 load_get_lmf_addr = code;
229 ARM_MOV_REG_REG (code, ARMREG_LR, ARMREG_PC);
230 code = emit_bx (code, ARMREG_R0);
233 * The stack now looks like:
235 * v1 -> <rest of LMF>
239 /* r0 is the result from mono_get_lmf_addr () */
240 ARM_STR_IMM (code, ARMREG_R0, ARMREG_V1, MONO_STRUCT_OFFSET (MonoLMF, lmf_addr));
241 /* new_lmf->previous_lmf = *lmf_addr */
242 ARM_LDR_IMM (code, ARMREG_R2, ARMREG_R0, MONO_STRUCT_OFFSET (MonoLMF, previous_lmf));
243 ARM_STR_IMM (code, ARMREG_R2, ARMREG_V1, MONO_STRUCT_OFFSET (MonoLMF, previous_lmf));
244 /* *(lmf_addr) = r1 */
245 ARM_STR_IMM (code, ARMREG_V1, ARMREG_R0, MONO_STRUCT_OFFSET (MonoLMF, previous_lmf));
246 /* save method info (it's in v2) */
247 if ((tramp_type == MONO_TRAMPOLINE_JIT) || (tramp_type == MONO_TRAMPOLINE_JUMP))
248 ARM_STR_IMM (code, ARMREG_V2, ARMREG_V1, MONO_STRUCT_OFFSET (MonoLMF, method));
250 ARM_MOV_REG_IMM8 (code, ARMREG_R2, 0);
251 ARM_STR_IMM (code, ARMREG_R2, ARMREG_V1, MONO_STRUCT_OFFSET (MonoLMF, method));
254 code = mono_arm_emit_load_imm (code, ARMREG_R2, cfa_offset);
255 ARM_ADD_REG_REG (code, ARMREG_R2, ARMREG_SP, ARMREG_R2);
256 ARM_STR_IMM (code, ARMREG_R2, ARMREG_V1, MONO_STRUCT_OFFSET (MonoLMF, sp));
258 ARM_LDR_IMM (code, ARMREG_R2, ARMREG_V1, (MONO_STRUCT_OFFSET (MonoLMF, iregs) + ARMREG_FP*4));
259 ARM_STR_IMM (code, ARMREG_R2, ARMREG_V1, MONO_STRUCT_OFFSET (MonoLMF, fp));
260 /* save the IP (caller ip) */
261 if (tramp_type == MONO_TRAMPOLINE_JUMP) {
262 ARM_MOV_REG_IMM8 (code, ARMREG_R2, 0);
264 ARM_LDR_IMM (code, ARMREG_R2, ARMREG_V1, (MONO_STRUCT_OFFSET (MonoLMF, iregs) + 13*4));
266 ARM_STR_IMM (code, ARMREG_R2, ARMREG_V1, MONO_STRUCT_OFFSET (MonoLMF, ip));
268 /* Save VFP registers. */
269 if (mono_arm_is_hard_float ()) {
271 * Strictly speaking, we don't have to save d0-d7 in the LMF, but
272 * it's easier than attempting to store them on the stack since
273 * this trampoline code is pretty messy.
275 ARM_ADD_REG_IMM8 (code, ARMREG_R0, ARMREG_V1, MONO_STRUCT_OFFSET (MonoLMF, fregs));
276 ARM_FSTMD (code, ARM_VFP_D0, 8, ARMREG_R0);
280 * Now we're ready to call xxx_trampoline ().
282 /* Arg 1: the saved registers */
283 ARM_ADD_REG_IMM (code, ARMREG_R0, ARMREG_V1, MONO_STRUCT_OFFSET (MonoLMF, iregs), 0);
285 /* Arg 2: code (next address to the instruction that called us) */
286 if (tramp_type == MONO_TRAMPOLINE_JUMP) {
287 ARM_MOV_REG_IMM8 (code, ARMREG_R1, 0);
289 ARM_MOV_REG_REG (code, ARMREG_R1, ARMREG_V3);
292 /* Arg 3: the specific argument, stored in v2
294 ARM_MOV_REG_REG (code, ARMREG_R2, ARMREG_V2);
297 char *icall_name = g_strdup_printf ("trampoline_func_%d", tramp_type);
298 ji = mono_patch_info_list_prepend (ji, code - buf, MONO_PATCH_INFO_JIT_ICALL_ADDR, icall_name);
299 ARM_LDR_IMM (code, ARMREG_IP, ARMREG_PC, 0);
301 *(gpointer*)code = NULL;
303 ARM_LDR_REG_REG (code, ARMREG_IP, ARMREG_PC, ARMREG_IP);
305 load_trampoline = code;
309 ARM_MOV_REG_REG (code, ARMREG_LR, ARMREG_PC);
310 code = emit_bx (code, ARMREG_IP);
312 /* OK, code address is now on r0. Move it to the place on the stack
313 * where IP was saved (it is now no more useful to us and it can be
314 * clobbered). This way we can just restore all the regs in one inst
317 ARM_STR_IMM (code, ARMREG_R0, ARMREG_V1, MONO_STRUCT_OFFSET (MonoLMF, iregs) + (ARMREG_R12 * sizeof (mgreg_t)));
319 /* Check for thread interruption */
320 /* This is not perf critical code so no need to check the interrupt flag */
322 * Have to call the _force_ variant, since there could be a protected wrapper on the top of the stack.
325 ji = mono_patch_info_list_prepend (ji, code - buf, MONO_PATCH_INFO_JIT_ICALL_ADDR, "mono_interruption_checkpoint_from_trampoline");
326 ARM_LDR_IMM (code, ARMREG_IP, ARMREG_PC, 0);
328 *(gpointer*)code = NULL;
330 ARM_LDR_REG_REG (code, ARMREG_IP, ARMREG_PC, ARMREG_IP);
332 ARM_LDR_IMM (code, ARMREG_IP, ARMREG_PC, 0);
334 *(gpointer*)code = mono_interruption_checkpoint_from_trampoline;
337 ARM_MOV_REG_REG (code, ARMREG_LR, ARMREG_PC);
338 code = emit_bx (code, ARMREG_IP);
341 * Now we restore the MonoLMF (see emit_epilogue in mini-arm.c)
342 * and the rest of the registers, so the method called will see
343 * the same state as before we executed.
345 /* ip = previous_lmf */
346 ARM_LDR_IMM (code, ARMREG_IP, ARMREG_V1, MONO_STRUCT_OFFSET (MonoLMF, previous_lmf));
348 ARM_LDR_IMM (code, ARMREG_LR, ARMREG_V1, MONO_STRUCT_OFFSET (MonoLMF, lmf_addr));
349 /* *(lmf_addr) = previous_lmf */
350 ARM_STR_IMM (code, ARMREG_IP, ARMREG_LR, MONO_STRUCT_OFFSET (MonoLMF, previous_lmf));
352 /* Restore VFP registers. */
353 if (mono_arm_is_hard_float ()) {
354 ARM_ADD_REG_IMM8 (code, ARMREG_R0, ARMREG_V1, MONO_STRUCT_OFFSET (MonoLMF, fregs));
355 ARM_FLDMD (code, ARM_VFP_D0, 8, ARMREG_R0);
358 /* Non-standard function epilogue. Instead of doing a proper
359 * return, we just jump to the compiled code.
361 /* Restore the registers and jump to the code:
362 * Note that IP has been conveniently set to the method addr.
364 ARM_ADD_REG_IMM8 (code, ARMREG_SP, ARMREG_SP, STACK - regsave_size);
365 cfa_offset -= STACK - regsave_size;
366 mono_add_unwind_op_def_cfa_offset (unwind_ops, code, buf, cfa_offset);
367 ARM_POP_NWB (code, 0x5fff);
368 mono_add_unwind_op_same_value (unwind_ops, code, buf, ARMREG_LR);
369 if (tramp_type == MONO_TRAMPOLINE_RGCTX_LAZY_FETCH)
370 ARM_MOV_REG_REG (code, ARMREG_R0, ARMREG_IP);
371 ARM_ADD_REG_IMM8 (code, ARMREG_SP, ARMREG_SP, regsave_size);
372 cfa_offset -= regsave_size;
373 g_assert (cfa_offset == 0);
374 mono_add_unwind_op_def_cfa_offset (unwind_ops, code, buf, cfa_offset);
375 if (MONO_TRAMPOLINE_TYPE_MUST_RETURN (tramp_type))
376 code = emit_bx (code, ARMREG_LR);
378 code = emit_bx (code, ARMREG_IP);
380 constants = (gpointer*)code;
381 constants [0] = mono_get_lmf_addr;
382 constants [1] = (gpointer)mono_get_trampoline_func (tramp_type);
385 /* backpatch by emitting the missing instructions skipped above */
386 ARM_LDR_IMM (load_get_lmf_addr, ARMREG_R0, ARMREG_PC, (code - load_get_lmf_addr - 8));
387 ARM_LDR_IMM (load_trampoline, ARMREG_IP, ARMREG_PC, (code + 4 - load_trampoline - 8));
392 /* Flush instruction cache, since we've generated code */
393 mono_arch_flush_icache (buf, code - buf);
394 mono_profiler_code_buffer_new (buf, code - buf, MONO_PROFILER_CODE_BUFFER_HELPER, NULL);
397 g_assert ((code - buf) <= buf_len);
400 tramp_name = mono_get_generic_trampoline_name (tramp_type);
401 *info = mono_tramp_info_create (tramp_name, buf, code - buf, ji, unwind_ops);
407 #define SPEC_TRAMP_SIZE 24
410 mono_arch_create_specific_trampoline (gpointer arg1, MonoTrampolineType tramp_type, MonoDomain *domain, guint32 *code_len)
412 guint8 *code, *buf, *tramp;
414 guint32 short_branch = FALSE;
415 guint32 size = SPEC_TRAMP_SIZE;
417 tramp = mono_get_trampoline_code (tramp_type);
420 mono_domain_lock (domain);
421 code = buf = mono_domain_code_reserve_align (domain, size, 4);
422 if ((short_branch = branch_for_target_reachable (code + 4, tramp))) {
424 mono_domain_code_commit (domain, code, SPEC_TRAMP_SIZE, size);
426 mono_domain_unlock (domain);
428 code = buf = mono_global_codeman_reserve (size);
429 short_branch = FALSE;
432 /* we could reduce this to 12 bytes if tramp is within reach:
436 * The called code can access method using the lr register
437 * A 20 byte sequence could be:
439 * ARM_MOV_REG_REG (lr, pc)
440 * ARM_LDR_IMM (pc, pc, 0)
444 /* We save all the registers, except PC and SP */
445 ARM_PUSH (code, 0x5fff);
447 constants = (gpointer*)code;
448 constants [0] = GUINT_TO_POINTER (short_branch | (1 << 24));
449 constants [1] = arg1;
452 ARM_LDR_IMM (code, ARMREG_R1, ARMREG_PC, 8); /* temp reg */
453 ARM_MOV_REG_REG (code, ARMREG_LR, ARMREG_PC);
454 code = emit_bx (code, ARMREG_R1);
456 constants = (gpointer*)code;
457 constants [0] = arg1;
458 constants [1] = tramp;
462 /* Flush instruction cache, since we've generated code */
463 mono_arch_flush_icache (buf, code - buf);
464 mono_profiler_code_buffer_new (buf, code - buf, MONO_PROFILER_CODE_BUFFER_SPECIFIC_TRAMPOLINE, mono_get_generic_trampoline_simple_name (tramp_type));
466 g_assert ((code - buf) <= size);
469 *code_len = code - buf;
475 * mono_arch_get_unbox_trampoline:
477 * @addr: pointer to native code for @m
479 * when value type methods are called through the vtable we need to unbox the
480 * this argument. This method returns a pointer to a trampoline which does
481 * unboxing before calling the method
484 mono_arch_get_unbox_trampoline (MonoMethod *m, gpointer addr)
486 guint8 *code, *start;
487 MonoDomain *domain = mono_domain_get ();
491 start = code = mono_domain_code_reserve (domain, size);
493 unwind_ops = mono_arch_get_cie_program ();
495 ARM_LDR_IMM (code, ARMREG_IP, ARMREG_PC, 4);
496 ARM_ADD_REG_IMM8 (code, ARMREG_R0, ARMREG_R0, sizeof (MonoObject));
497 code = emit_bx (code, ARMREG_IP);
498 *(guint32*)code = (guint32)addr;
500 mono_arch_flush_icache (start, code - start);
501 mono_profiler_code_buffer_new (start, code - start, MONO_PROFILER_CODE_BUFFER_UNBOX_TRAMPOLINE, m);
502 g_assert ((code - start) <= size);
503 /*g_print ("unbox trampoline at %d for %s:%s\n", this_pos, m->klass->name, m->name);
504 g_print ("unbox code is at %p for method at %p\n", start, addr);*/
506 mono_tramp_info_register (mono_tramp_info_create (NULL, start, code - start, NULL, unwind_ops), domain);
512 mono_arch_get_static_rgctx_trampoline (gpointer arg, gpointer addr)
514 guint8 *code, *start;
517 MonoDomain *domain = mono_domain_get ();
519 start = code = mono_domain_code_reserve (domain, buf_len);
521 unwind_ops = mono_arch_get_cie_program ();
523 ARM_LDR_IMM (code, MONO_ARCH_RGCTX_REG, ARMREG_PC, 0);
524 ARM_LDR_IMM (code, ARMREG_PC, ARMREG_PC, 0);
525 *(guint32*)code = (guint32)arg;
527 *(guint32*)code = (guint32)addr;
530 g_assert ((code - start) <= buf_len);
532 mono_arch_flush_icache (start, code - start);
533 mono_profiler_code_buffer_new (start, code - start, MONO_PROFILER_CODE_BUFFER_GENERICS_TRAMPOLINE, NULL);
535 mono_tramp_info_register (mono_tramp_info_create (NULL, start, code - start, NULL, unwind_ops), domain);
541 mono_arch_create_rgctx_lazy_fetch_trampoline (guint32 slot, MonoTrampInfo **info, gboolean aot)
547 guint8 **rgctx_null_jumps;
551 MonoJumpInfo *ji = NULL;
552 GSList *unwind_ops = NULL;
554 mrgctx = MONO_RGCTX_SLOT_IS_MRGCTX (slot);
555 index = MONO_RGCTX_SLOT_INDEX (slot);
557 index += MONO_SIZEOF_METHOD_RUNTIME_GENERIC_CONTEXT / sizeof (gpointer);
558 for (depth = 0; ; ++depth) {
559 int size = mono_class_rgctx_get_array_size (depth, mrgctx);
561 if (index < size - 1)
566 tramp_size = 64 + 16 * depth;
568 code = buf = mono_global_codeman_reserve (tramp_size);
570 unwind_ops = mono_arch_get_cie_program ();
572 rgctx_null_jumps = g_malloc (sizeof (guint8*) * (depth + 2));
575 /* The vtable/mrgctx is in R0 */
576 g_assert (MONO_ARCH_VTABLE_REG == ARMREG_R0);
580 ARM_MOV_REG_REG (code, ARMREG_R1, ARMREG_R0);
582 /* load rgctx ptr from vtable */
583 g_assert (arm_is_imm12 (MONO_STRUCT_OFFSET (MonoVTable, runtime_generic_context)));
584 ARM_LDR_IMM (code, ARMREG_R1, ARMREG_R0, MONO_STRUCT_OFFSET (MonoVTable, runtime_generic_context));
585 /* is the rgctx ptr null? */
586 ARM_CMP_REG_IMM (code, ARMREG_R1, 0, 0);
587 /* if yes, jump to actual trampoline */
588 rgctx_null_jumps [njumps ++] = code;
589 ARM_B_COND (code, ARMCOND_EQ, 0);
592 for (i = 0; i < depth; ++i) {
593 /* load ptr to next array */
594 if (mrgctx && i == 0) {
595 g_assert (arm_is_imm12 (MONO_SIZEOF_METHOD_RUNTIME_GENERIC_CONTEXT));
596 ARM_LDR_IMM (code, ARMREG_R1, ARMREG_R1, MONO_SIZEOF_METHOD_RUNTIME_GENERIC_CONTEXT);
598 ARM_LDR_IMM (code, ARMREG_R1, ARMREG_R1, 0);
600 /* is the ptr null? */
601 ARM_CMP_REG_IMM (code, ARMREG_R1, 0, 0);
602 /* if yes, jump to actual trampoline */
603 rgctx_null_jumps [njumps ++] = code;
604 ARM_B_COND (code, ARMCOND_EQ, 0);
608 code = mono_arm_emit_load_imm (code, ARMREG_R2, sizeof (gpointer) * (index + 1));
609 ARM_LDR_REG_REG (code, ARMREG_R1, ARMREG_R1, ARMREG_R2);
610 /* is the slot null? */
611 ARM_CMP_REG_IMM (code, ARMREG_R1, 0, 0);
612 /* if yes, jump to actual trampoline */
613 rgctx_null_jumps [njumps ++] = code;
614 ARM_B_COND (code, ARMCOND_EQ, 0);
615 /* otherwise return, result is in R1 */
616 ARM_MOV_REG_REG (code, ARMREG_R0, ARMREG_R1);
617 code = emit_bx (code, ARMREG_LR);
619 g_assert (njumps <= depth + 2);
620 for (i = 0; i < njumps; ++i)
621 arm_patch (rgctx_null_jumps [i], code);
623 g_free (rgctx_null_jumps);
627 /* The vtable/mrgctx is still in R0 */
630 ji = mono_patch_info_list_prepend (ji, code - buf, MONO_PATCH_INFO_JIT_ICALL_ADDR, g_strdup_printf ("specific_trampoline_lazy_fetch_%u", slot));
631 ARM_LDR_IMM (code, ARMREG_R1, ARMREG_PC, 0);
633 *(gpointer*)code = NULL;
635 ARM_LDR_REG_REG (code, ARMREG_PC, ARMREG_PC, ARMREG_R1);
637 tramp = mono_arch_create_specific_trampoline (GUINT_TO_POINTER (slot), MONO_TRAMPOLINE_RGCTX_LAZY_FETCH, mono_get_root_domain (), &code_len);
639 /* Jump to the actual trampoline */
640 ARM_LDR_IMM (code, ARMREG_R1, ARMREG_PC, 0); /* temp reg */
641 code = emit_bx (code, ARMREG_R1);
642 *(gpointer*)code = tramp;
646 mono_arch_flush_icache (buf, code - buf);
647 mono_profiler_code_buffer_new (buf, code - buf, MONO_PROFILER_CODE_BUFFER_GENERICS_TRAMPOLINE, NULL);
649 g_assert (code - buf <= tramp_size);
651 char *name = mono_get_rgctx_fetch_trampoline_name (slot);
652 *info = mono_tramp_info_create (name, buf, code - buf, ji, unwind_ops);
659 mono_arch_create_general_rgctx_lazy_fetch_trampoline (MonoTrampInfo **info, gboolean aot)
663 MonoJumpInfo *ji = NULL;
664 GSList *unwind_ops = NULL;
670 code = buf = mono_global_codeman_reserve (tramp_size);
672 unwind_ops = mono_arch_get_cie_program ();
674 // FIXME: Currently, we always go to the slow path.
675 /* Load trampoline addr */
676 ARM_LDR_IMM (code, ARMREG_R1, MONO_ARCH_RGCTX_REG, 4);
677 /* The vtable/mrgctx is in R0 */
678 g_assert (MONO_ARCH_VTABLE_REG == ARMREG_R0);
679 code = emit_bx (code, ARMREG_R1);
681 mono_arch_flush_icache (buf, code - buf);
682 mono_profiler_code_buffer_new (buf, code - buf, MONO_PROFILER_CODE_BUFFER_GENERICS_TRAMPOLINE, NULL);
684 g_assert (code - buf <= tramp_size);
686 *info = mono_tramp_info_create ("rgctx_fetch_trampoline_general", buf, code - buf, ji, unwind_ops);
692 mono_arch_create_handler_block_trampoline (MonoTrampInfo **info, gboolean aot)
697 MonoJumpInfo *ji = NULL;
698 GSList *unwind_ops = NULL;
700 code = buf = mono_global_codeman_reserve (tramp_size);
702 unwind_ops = mono_arch_get_cie_program ();
704 tramp = mono_arch_create_specific_trampoline (NULL, MONO_TRAMPOLINE_HANDLER_BLOCK_GUARD, NULL, NULL);
707 This trampoline restore the call chain of the handler block then jumps into the code that deals with it.
711 * We are in a method frame after the call emitted by OP_CALL_HANDLER.
713 /* Obtain jit_tls->handler_block_return_address */
715 code = mono_arm_emit_aotconst (&ji, code, buf, ARMREG_R0, MONO_PATCH_INFO_JIT_ICALL_ADDR, "mono_arm_handler_block_trampoline_helper");
718 ARM_LDR_IMM (code, ARMREG_R0, ARMREG_PC, 0);
720 *(gpointer*)code = mono_arm_handler_block_trampoline_helper;
724 /* Set it as the return address so the trampoline will return to it */
725 ARM_MOV_REG_REG (code, ARMREG_LR, ARMREG_R0);
727 /* Call the trampoline */
729 char *name = g_strdup_printf ("trampoline_func_%d", MONO_TRAMPOLINE_HANDLER_BLOCK_GUARD);
730 code = mono_arm_emit_aotconst (&ji, code, buf, ARMREG_R0, MONO_PATCH_INFO_JIT_ICALL_ADDR, name);
731 code = emit_bx (code, ARMREG_R0);
733 ARM_LDR_IMM (code, ARMREG_R0, ARMREG_PC, 0);
734 code = emit_bx (code, ARMREG_R0);
735 *(gpointer*)code = tramp;
739 mono_arch_flush_icache (buf, code - buf);
740 mono_profiler_code_buffer_new (buf, code - buf, MONO_PROFILER_CODE_BUFFER_HELPER, NULL);
741 g_assert (code - buf <= tramp_size);
743 *info = mono_tramp_info_create ("handler_block_trampoline", buf, code - buf, ji, unwind_ops);
749 mono_arch_create_sdb_trampoline (gboolean single_step, MonoTrampInfo **info, gboolean aot)
752 GSList *unwind_ops = NULL;
753 MonoJumpInfo *ji = NULL;
756 buf = code = mono_global_codeman_reserve (96);
759 * Construct the MonoContext structure on the stack.
762 frame_size = sizeof (MonoContext);
763 frame_size = ALIGN_TO (frame_size, MONO_ARCH_FRAME_ALIGNMENT);
764 ARM_SUB_REG_IMM8 (code, ARMREG_SP, ARMREG_SP, frame_size);
766 /* save ip, lr and pc into their correspodings ctx.regs slots. */
767 ARM_STR_IMM (code, ARMREG_IP, ARMREG_SP, MONO_STRUCT_OFFSET (MonoContext, regs) + sizeof (mgreg_t) * ARMREG_IP);
768 ARM_STR_IMM (code, ARMREG_LR, ARMREG_SP, MONO_STRUCT_OFFSET (MonoContext, regs) + 4 * ARMREG_LR);
769 ARM_STR_IMM (code, ARMREG_LR, ARMREG_SP, MONO_STRUCT_OFFSET (MonoContext, regs) + 4 * ARMREG_PC);
771 /* save r0..r10 and fp */
772 ARM_ADD_REG_IMM8 (code, ARMREG_IP, ARMREG_SP, MONO_STRUCT_OFFSET (MonoContext, regs));
773 ARM_STM (code, ARMREG_IP, 0x0fff);
775 /* now we can update fp. */
776 ARM_MOV_REG_REG (code, ARMREG_FP, ARMREG_SP);
778 /* make ctx.esp hold the actual value of sp at the beginning of this method. */
779 ARM_ADD_REG_IMM8 (code, ARMREG_R0, ARMREG_FP, frame_size);
780 ARM_STR_IMM (code, ARMREG_R0, ARMREG_IP, 4 * ARMREG_SP);
781 ARM_STR_IMM (code, ARMREG_R0, ARMREG_FP, MONO_STRUCT_OFFSET (MonoContext, regs) + 4 * ARMREG_SP);
783 /* make ctx.eip hold the address of the call. */
784 //ARM_SUB_REG_IMM8 (code, ARMREG_LR, ARMREG_LR, 4);
785 ARM_STR_IMM (code, ARMREG_LR, ARMREG_FP, MONO_STRUCT_OFFSET (MonoContext, pc));
787 /* r0 now points to the MonoContext */
788 ARM_MOV_REG_REG (code, ARMREG_R0, ARMREG_FP);
793 ji = mono_patch_info_list_prepend (ji, code - buf, MONO_PATCH_INFO_JIT_ICALL_ADDR, "debugger_agent_single_step_from_context");
795 ji = mono_patch_info_list_prepend (ji, code - buf, MONO_PATCH_INFO_JIT_ICALL_ADDR, "debugger_agent_breakpoint_from_context");
796 ARM_LDR_IMM (code, ARMREG_IP, ARMREG_PC, 0);
798 *(gpointer*)code = NULL;
800 ARM_LDR_REG_REG (code, ARMREG_IP, ARMREG_PC, ARMREG_IP);
801 ARM_BLX_REG (code, ARMREG_IP);
803 ARM_LDR_IMM (code, ARMREG_IP, ARMREG_PC, 0);
806 *(gpointer*)code = debugger_agent_single_step_from_context;
808 *(gpointer*)code = debugger_agent_breakpoint_from_context;
810 ARM_BLX_REG (code, ARMREG_IP);
813 /* we're back; save ctx.eip and ctx.esp into the corresponding regs slots. */
814 ARM_LDR_IMM (code, ARMREG_R0, ARMREG_FP, MONO_STRUCT_OFFSET (MonoContext, pc));
815 ARM_STR_IMM (code, ARMREG_R0, ARMREG_FP, MONO_STRUCT_OFFSET (MonoContext, regs) + 4 * ARMREG_LR);
816 ARM_STR_IMM (code, ARMREG_R0, ARMREG_FP, MONO_STRUCT_OFFSET (MonoContext, regs) + 4 * ARMREG_PC);
818 /* make ip point to the regs array, then restore everything, including pc. */
819 ARM_ADD_REG_IMM8 (code, ARMREG_IP, ARMREG_FP, MONO_STRUCT_OFFSET (MonoContext, regs));
820 ARM_LDM (code, ARMREG_IP, 0xffff);
822 mono_arch_flush_icache (buf, code - buf);
823 mono_profiler_code_buffer_new (buf, code - buf, MONO_PROFILER_CODE_BUFFER_HELPER, NULL);
825 const char *tramp_name = single_step ? "sdb_single_step_trampoline" : "sdb_breakpoint_trampoline";
826 *info = mono_tramp_info_create (tramp_name, buf, code - buf, ji, unwind_ops);
834 mono_arch_create_generic_trampoline (MonoTrampolineType tramp_type, MonoTrampInfo **info, gboolean aot)
836 g_assert_not_reached ();
841 mono_arch_create_specific_trampoline (gpointer arg1, MonoTrampolineType tramp_type, MonoDomain *domain, guint32 *code_len)
843 g_assert_not_reached ();
848 mono_arch_get_unbox_trampoline (MonoMethod *m, gpointer addr)
850 g_assert_not_reached ();
855 mono_arch_get_static_rgctx_trampoline (gpointer arg, gpointer addr)
857 g_assert_not_reached ();
862 mono_arch_create_rgctx_lazy_fetch_trampoline (guint32 slot, MonoTrampInfo **info, gboolean aot)
864 g_assert_not_reached ();
869 mono_arch_create_handler_block_trampoline (MonoTrampInfo **info, gboolean aot)
871 g_assert_not_reached ();
876 mono_arch_create_sdb_trampoline (gboolean single_step, MonoTrampInfo **info, gboolean aot)
878 g_assert_not_reached ();
882 #endif /* DISABLE_JIT */
885 mono_arch_get_call_target (guint8 *code)
887 guint32 ins = ((guint32*)(gpointer)code) [-1];
889 /* Should be a 'bl' or a 'b' */
890 if (((ins >> 25) & 0x7) == 0x5) {
891 gint32 disp = ((((gint32)ins) & 0xffffff) << 8) >> 8;
892 guint8 *target = code - 4 + 8 + (disp * 4);
901 mono_arch_get_plt_info_offset (guint8 *plt_entry, mgreg_t *regs, guint8 *code)
903 /* The offset is stored as the 4th word of the plt entry */
904 return ((guint32*)plt_entry) [3];
908 * Return the address of the PLT entry called by the thumb code CODE.
911 mono_arm_get_thumb_plt_entry (guint8 *code)
913 int s, j1, j2, imm10, imm11, i1, i2, imm32;
918 /* code should be right after a BL */
919 code = (guint8*)((mgreg_t)code & ~1);
920 base = (guint8*)((mgreg_t)code & ~3);
922 t1 = ((guint16*)bl) [0];
923 t2 = ((guint16*)bl) [1];
925 g_assert ((t1 >> 11) == 0x1e);
927 s = (t1 >> 10) & 0x1;
928 imm10 = (t1 >> 0) & 0x3ff;
929 j1 = (t2 >> 13) & 0x1;
930 j2 = (t2 >> 11) & 0x1;
933 i1 = (s ^ j1) ? 0 : 1;
934 i2 = (s ^ j2) ? 0 : 1;
936 imm32 = (imm11 << 1) | (imm10 << 12) | (i2 << 22) | (i1 << 23);
938 /* Sign extend from 24 bits to 32 bits */
939 imm32 = ((gint32)imm32 << 8) >> 8;
941 target = code + imm32;
943 /* target now points to the thumb plt entry */
944 /* ldr.w r12, [pc, #8] */
945 g_assert (((guint16*)target) [0] == 0xf8df);
946 g_assert (((guint16*)target) [1] == 0xc008);
949 * The PLT info offset is at offset 16, but mono_arch_get_plt_entry_offset () returns
950 * the 3rd word, so compensate by returning a different value.
960 * mono_arch_get_gsharedvt_arg_trampoline:
962 * See tramp-x86.c for documentation.
965 mono_arch_get_gsharedvt_arg_trampoline (MonoDomain *domain, gpointer arg, gpointer addr)
973 buf = code = mono_domain_code_reserve (domain, buf_len);
975 /* Similar to the specialized trampoline code */
976 ARM_PUSH (code, (1 << ARMREG_R0) | (1 << ARMREG_R1) | (1 << ARMREG_R2) | (1 << ARMREG_R3) | (1 << ARMREG_LR));
977 ARM_LDR_IMM (code, ARMREG_IP, ARMREG_PC, 8);
978 /* arg is passed in LR */
979 ARM_LDR_IMM (code, ARMREG_LR, ARMREG_PC, 0);
980 code = emit_bx (code, ARMREG_IP);
981 constants = (gpointer*)code;
983 constants [1] = addr;
986 g_assert ((code - buf) <= buf_len);
988 mono_arch_flush_icache (buf, code - buf);
989 mono_profiler_code_buffer_new (buf, code - buf, MONO_PROFILER_CODE_BUFFER_GENERICS_TRAMPOLINE, NULL);
991 mono_tramp_info_register (mono_tramp_info_create (NULL, buf, code - buf, NULL, NULL), domain);
999 mono_arch_get_gsharedvt_arg_trampoline (MonoDomain *domain, gpointer arg, gpointer addr)
1001 g_assert_not_reached ();