[Facades] Use the Open.snk key for the System.ValueTuple facade (#4173)
[mono-project.git] / mono / mini / tramp-arm.c
blob4e2a23ce6a049d3c992869e213a178e76b3c39b2
1 /*
2 * tramp-arm.c: JIT trampoline code for ARM
4 * Authors:
5 * Paolo Molaro (lupus@ximian.com)
7 * (C) 2001-2003 Ximian, Inc.
8 * Copyright 2003-2011 Novell Inc
9 * Copyright 2011 Xamarin Inc
10 * Licensed under the MIT license. See LICENSE file in the project root for full license information.
13 #include <config.h>
14 #include <glib.h>
16 #include <mono/metadata/abi-details.h>
17 #include <mono/metadata/appdomain.h>
18 #include <mono/metadata/marshal.h>
19 #include <mono/metadata/tabledefs.h>
20 #include <mono/metadata/profiler-private.h>
21 #include <mono/arch/arm/arm-codegen.h>
22 #include <mono/arch/arm/arm-vfp-codegen.h>
24 #include "mini.h"
25 #include "mini-arm.h"
26 #include "debugger-agent.h"
27 #include "jit-icalls.h"
29 #define ALIGN_TO(val,align) ((((guint64)val) + ((align) - 1)) & ~((align) - 1))
31 void
32 mono_arch_patch_callsite (guint8 *method_start, guint8 *code_ptr, guint8 *addr)
34 guint32 *code = (guint32*)code_ptr;
36 /* This is the 'bl' or the 'mov pc' instruction */
37 --code;
40 * Note that methods are called also with the bl opcode.
42 if ((((*code) >> 25) & 7) == 5) {
43 /*g_print ("direct patching\n");*/
44 arm_patch ((guint8*)code, addr);
45 mono_arch_flush_icache ((guint8*)code, 4);
46 return;
49 if ((((*code) >> 20) & 0xFF) == 0x12) {
50 /*g_print ("patching bx\n");*/
51 arm_patch ((guint8*)code, addr);
52 mono_arch_flush_icache ((guint8*)(code - 2), 4);
53 return;
56 g_assert_not_reached ();
59 void
60 mono_arch_patch_plt_entry (guint8 *code, gpointer *got, mgreg_t *regs, guint8 *addr)
62 guint8 *jump_entry;
64 /* Patch the jump table entry used by the plt entry */
65 if (*(guint32*)code == 0xe59fc000) {
66 /* ARM_LDR_IMM (code, ARMREG_IP, ARMREG_PC, 0); */
67 guint32 offset = ((guint32*)code)[2];
69 jump_entry = code + offset + 12;
70 } else if (*(guint16*)(code - 4) == 0xf8df) {
71 /*
72 * Thumb PLT entry, begins with ldr.w ip, [pc, #8], code points to entry + 4, see
73 * mono_arm_get_thumb_plt_entry ().
75 guint32 offset;
77 code -= 4;
78 offset = *(guint32*)(code + 12);
79 jump_entry = code + offset + 8;
80 } else {
81 g_assert_not_reached ();
84 *(guint8**)jump_entry = addr;
87 #ifndef DISABLE_JIT
89 #define arm_is_imm12(v) ((int)(v) > -4096 && (int)(v) < 4096)
92 * Return the instruction to jump from code to target, 0 if not
93 * reachable with a single instruction
95 static guint32
96 branch_for_target_reachable (guint8 *branch, guint8 *target)
98 gint diff = target - branch - 8;
99 g_assert ((diff & 3) == 0);
100 if (diff >= 0) {
101 if (diff <= 33554431)
102 return (ARMCOND_AL << ARMCOND_SHIFT) | (ARM_BR_TAG) | (diff >> 2);
103 } else {
104 /* diff between 0 and -33554432 */
105 if (diff >= -33554432)
106 return (ARMCOND_AL << ARMCOND_SHIFT) | (ARM_BR_TAG) | ((diff >> 2) & ~0xff000000);
108 return 0;
111 static inline guint8*
112 emit_bx (guint8* code, int reg)
114 if (mono_arm_thumb_supported ())
115 ARM_BX (code, reg);
116 else
117 ARM_MOV_REG_REG (code, ARMREG_PC, reg);
118 return code;
121 /* Stack size for trampoline function
123 #define STACK ALIGN_TO (sizeof (MonoLMF), MONO_ARCH_FRAME_ALIGNMENT)
125 /* Method-specific trampoline code fragment size */
126 #define METHOD_TRAMPOLINE_SIZE 64
128 /* Jump-specific trampoline code fragment size */
129 #define JUMP_TRAMPOLINE_SIZE 64
131 guchar*
132 mono_arch_create_generic_trampoline (MonoTrampolineType tramp_type, MonoTrampInfo **info, gboolean aot)
134 char *tramp_name;
135 guint8 *buf, *code = NULL;
136 guint8 *load_get_lmf_addr = NULL, *load_trampoline = NULL;
137 gpointer *constants;
138 int i, cfa_offset, regsave_size, lr_offset;
139 GSList *unwind_ops = NULL;
140 MonoJumpInfo *ji = NULL;
141 int buf_len;
143 /* Now we'll create in 'buf' the ARM trampoline code. This
144 is the trampoline code common to all methods */
146 buf_len = 272;
148 /* Add space for saving/restoring VFP regs. */
149 if (mono_arm_is_hard_float ())
150 buf_len += 8 * 2;
152 code = buf = mono_global_codeman_reserve (buf_len);
155 * At this point lr points to the specific arg and sp points to the saved
156 * regs on the stack (all but PC and SP). The original LR value has been
157 * saved as sp + LR_OFFSET by the push in the specific trampoline
160 /* The size of the area already allocated by the push in the specific trampoline */
161 regsave_size = 14 * sizeof (mgreg_t);
162 /* The offset where lr was saved inside the regsave area */
163 lr_offset = 13 * sizeof (mgreg_t);
165 // CFA = SP + (num registers pushed) * 4
166 cfa_offset = 14 * sizeof (mgreg_t);
167 mono_add_unwind_op_def_cfa (unwind_ops, code, buf, ARMREG_SP, cfa_offset);
168 // PC saved at sp+LR_OFFSET
169 mono_add_unwind_op_offset (unwind_ops, code, buf, ARMREG_LR, -4);
170 /* Callee saved regs */
171 for (i = 0; i < 8; ++i)
172 mono_add_unwind_op_offset (unwind_ops, code, buf, ARMREG_R4 + i, -regsave_size + ((4 + i) * 4));
174 if (aot) {
176 * For page trampolines the data is in r1, so just move it, otherwise use the got slot as below.
177 * The trampoline contains a pc-relative offset to the got slot
178 * preceeding the got slot where the value is stored. The offset can be
179 * found at [lr + 0].
181 /* See if emit_trampolines () in aot-compiler.c for the '2' */
182 if (aot == 2) {
183 ARM_MOV_REG_REG (code, ARMREG_V2, ARMREG_R1);
184 } else {
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 } else {
190 ARM_LDR_IMM (code, ARMREG_V2, ARMREG_LR, 0);
192 ARM_LDR_IMM (code, ARMREG_V3, ARMREG_SP, lr_offset);
194 /* we build the MonoLMF structure on the stack - see mini-arm.h
195 * The pointer to the struct is put in r1.
196 * the iregs array is already allocated on the stack by push.
198 code = mono_arm_emit_load_imm (code, ARMREG_R2, STACK - regsave_size);
199 ARM_SUB_REG_REG (code, ARMREG_SP, ARMREG_SP, ARMREG_R2);
200 cfa_offset += STACK - regsave_size;
201 mono_add_unwind_op_def_cfa_offset (unwind_ops, code, buf, cfa_offset);
202 /* V1 == lmf */
203 code = mono_arm_emit_load_imm (code, ARMREG_R2, STACK - sizeof (MonoLMF));
204 ARM_ADD_REG_REG (code, ARMREG_V1, ARMREG_SP, ARMREG_R2);
206 /* ok, now we can continue with the MonoLMF setup, mostly untouched
207 * from emit_prolog in mini-arm.c
208 * This is a synthetized call to mono_get_lmf_addr ()
210 if (aot) {
211 ji = mono_patch_info_list_prepend (ji, code - buf, MONO_PATCH_INFO_JIT_ICALL_ADDR, "mono_get_lmf_addr");
212 ARM_LDR_IMM (code, ARMREG_R0, ARMREG_PC, 0);
213 ARM_B (code, 0);
214 *(gpointer*)code = NULL;
215 code += 4;
216 ARM_LDR_REG_REG (code, ARMREG_R0, ARMREG_PC, ARMREG_R0);
217 } else {
218 load_get_lmf_addr = code;
219 code += 4;
221 ARM_MOV_REG_REG (code, ARMREG_LR, ARMREG_PC);
222 code = emit_bx (code, ARMREG_R0);
225 * The stack now looks like:
226 * <saved regs>
227 * v1 -> <rest of LMF>
228 * sp -> <alignment>
231 /* r0 is the result from mono_get_lmf_addr () */
232 ARM_STR_IMM (code, ARMREG_R0, ARMREG_V1, MONO_STRUCT_OFFSET (MonoLMF, lmf_addr));
233 /* new_lmf->previous_lmf = *lmf_addr */
234 ARM_LDR_IMM (code, ARMREG_R2, ARMREG_R0, MONO_STRUCT_OFFSET (MonoLMF, previous_lmf));
235 ARM_STR_IMM (code, ARMREG_R2, ARMREG_V1, MONO_STRUCT_OFFSET (MonoLMF, previous_lmf));
236 /* *(lmf_addr) = r1 */
237 ARM_STR_IMM (code, ARMREG_V1, ARMREG_R0, MONO_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, MONO_STRUCT_OFFSET (MonoLMF, method));
241 else {
242 ARM_MOV_REG_IMM8 (code, ARMREG_R2, 0);
243 ARM_STR_IMM (code, ARMREG_R2, ARMREG_V1, MONO_STRUCT_OFFSET (MonoLMF, method));
245 /* save caller SP */
246 code = mono_arm_emit_load_imm (code, ARMREG_R2, cfa_offset);
247 ARM_ADD_REG_REG (code, ARMREG_R2, ARMREG_SP, ARMREG_R2);
248 ARM_STR_IMM (code, ARMREG_R2, ARMREG_V1, MONO_STRUCT_OFFSET (MonoLMF, sp));
249 /* save caller FP */
250 ARM_LDR_IMM (code, ARMREG_R2, ARMREG_V1, (MONO_STRUCT_OFFSET (MonoLMF, iregs) + ARMREG_FP*4));
251 ARM_STR_IMM (code, ARMREG_R2, ARMREG_V1, MONO_STRUCT_OFFSET (MonoLMF, fp));
252 /* save the IP (caller ip) */
253 if (tramp_type == MONO_TRAMPOLINE_JUMP) {
254 ARM_MOV_REG_IMM8 (code, ARMREG_R2, 0);
255 } else {
256 ARM_LDR_IMM (code, ARMREG_R2, ARMREG_V1, (MONO_STRUCT_OFFSET (MonoLMF, iregs) + 13*4));
258 ARM_STR_IMM (code, ARMREG_R2, ARMREG_V1, MONO_STRUCT_OFFSET (MonoLMF, ip));
260 /* Save VFP registers. */
261 if (mono_arm_is_hard_float ()) {
263 * Strictly speaking, we don't have to save d0-d7 in the LMF, but
264 * it's easier than attempting to store them on the stack since
265 * this trampoline code is pretty messy.
267 ARM_ADD_REG_IMM8 (code, ARMREG_R0, ARMREG_V1, MONO_STRUCT_OFFSET (MonoLMF, fregs));
268 ARM_FSTMD (code, ARM_VFP_D0, 8, ARMREG_R0);
272 * Now we're ready to call xxx_trampoline ().
274 /* Arg 1: the saved registers */
275 ARM_ADD_REG_IMM (code, ARMREG_R0, ARMREG_V1, MONO_STRUCT_OFFSET (MonoLMF, iregs), 0);
277 /* Arg 2: code (next address to the instruction that called us) */
278 if (tramp_type == MONO_TRAMPOLINE_JUMP) {
279 ARM_MOV_REG_IMM8 (code, ARMREG_R1, 0);
280 } else {
281 ARM_MOV_REG_REG (code, ARMREG_R1, ARMREG_V3);
284 /* Arg 3: the specific argument, stored in v2
286 ARM_MOV_REG_REG (code, ARMREG_R2, ARMREG_V2);
288 if (aot) {
289 char *icall_name = g_strdup_printf ("trampoline_func_%d", tramp_type);
290 ji = mono_patch_info_list_prepend (ji, code - buf, MONO_PATCH_INFO_JIT_ICALL_ADDR, icall_name);
291 ARM_LDR_IMM (code, ARMREG_IP, ARMREG_PC, 0);
292 ARM_B (code, 0);
293 *(gpointer*)code = NULL;
294 code += 4;
295 ARM_LDR_REG_REG (code, ARMREG_IP, ARMREG_PC, ARMREG_IP);
296 } else {
297 load_trampoline = code;
298 code += 4;
301 ARM_MOV_REG_REG (code, ARMREG_LR, ARMREG_PC);
302 code = emit_bx (code, ARMREG_IP);
304 /* OK, code address is now on r0. Move it to the place on the stack
305 * where IP was saved (it is now no more useful to us and it can be
306 * clobbered). This way we can just restore all the regs in one inst
307 * and branch to IP.
309 ARM_STR_IMM (code, ARMREG_R0, ARMREG_V1, MONO_STRUCT_OFFSET (MonoLMF, iregs) + (ARMREG_R12 * sizeof (mgreg_t)));
311 /* Check for thread interruption */
312 /* This is not perf critical code so no need to check the interrupt flag */
314 * Have to call the _force_ variant, since there could be a protected wrapper on the top of the stack.
316 if (aot) {
317 ji = mono_patch_info_list_prepend (ji, code - buf, MONO_PATCH_INFO_JIT_ICALL_ADDR, "mono_interruption_checkpoint_from_trampoline");
318 ARM_LDR_IMM (code, ARMREG_IP, ARMREG_PC, 0);
319 ARM_B (code, 0);
320 *(gpointer*)code = NULL;
321 code += 4;
322 ARM_LDR_REG_REG (code, ARMREG_IP, ARMREG_PC, ARMREG_IP);
323 } else {
324 ARM_LDR_IMM (code, ARMREG_IP, ARMREG_PC, 0);
325 ARM_B (code, 0);
326 *(gpointer*)code = mono_interruption_checkpoint_from_trampoline;
327 code += 4;
329 ARM_MOV_REG_REG (code, ARMREG_LR, ARMREG_PC);
330 code = emit_bx (code, ARMREG_IP);
333 * Now we restore the MonoLMF (see emit_epilogue in mini-arm.c)
334 * and the rest of the registers, so the method called will see
335 * the same state as before we executed.
337 /* ip = previous_lmf */
338 ARM_LDR_IMM (code, ARMREG_IP, ARMREG_V1, MONO_STRUCT_OFFSET (MonoLMF, previous_lmf));
339 /* lr = lmf_addr */
340 ARM_LDR_IMM (code, ARMREG_LR, ARMREG_V1, MONO_STRUCT_OFFSET (MonoLMF, lmf_addr));
341 /* *(lmf_addr) = previous_lmf */
342 ARM_STR_IMM (code, ARMREG_IP, ARMREG_LR, MONO_STRUCT_OFFSET (MonoLMF, previous_lmf));
344 /* Restore VFP registers. */
345 if (mono_arm_is_hard_float ()) {
346 ARM_ADD_REG_IMM8 (code, ARMREG_R0, ARMREG_V1, MONO_STRUCT_OFFSET (MonoLMF, fregs));
347 ARM_FLDMD (code, ARM_VFP_D0, 8, ARMREG_R0);
350 /* Non-standard function epilogue. Instead of doing a proper
351 * return, we just jump to the compiled code.
353 /* Restore the registers and jump to the code:
354 * Note that IP has been conveniently set to the method addr.
356 ARM_ADD_REG_IMM8 (code, ARMREG_SP, ARMREG_SP, STACK - regsave_size);
357 cfa_offset -= STACK - regsave_size;
358 mono_add_unwind_op_def_cfa_offset (unwind_ops, code, buf, cfa_offset);
359 ARM_POP_NWB (code, 0x5fff);
360 mono_add_unwind_op_same_value (unwind_ops, code, buf, ARMREG_LR);
361 if (tramp_type == MONO_TRAMPOLINE_RGCTX_LAZY_FETCH)
362 ARM_MOV_REG_REG (code, ARMREG_R0, ARMREG_IP);
363 ARM_ADD_REG_IMM8 (code, ARMREG_SP, ARMREG_SP, regsave_size);
364 cfa_offset -= regsave_size;
365 g_assert (cfa_offset == 0);
366 mono_add_unwind_op_def_cfa_offset (unwind_ops, code, buf, cfa_offset);
367 if (MONO_TRAMPOLINE_TYPE_MUST_RETURN (tramp_type))
368 code = emit_bx (code, ARMREG_LR);
369 else
370 code = emit_bx (code, ARMREG_IP);
372 constants = (gpointer*)code;
373 constants [0] = mono_get_lmf_addr;
374 constants [1] = (gpointer)mono_get_trampoline_func (tramp_type);
376 if (!aot) {
377 /* backpatch by emitting the missing instructions skipped above */
378 ARM_LDR_IMM (load_get_lmf_addr, ARMREG_R0, ARMREG_PC, (code - load_get_lmf_addr - 8));
379 ARM_LDR_IMM (load_trampoline, ARMREG_IP, ARMREG_PC, (code + 4 - load_trampoline - 8));
382 code += 8;
384 /* Flush instruction cache, since we've generated code */
385 mono_arch_flush_icache (buf, code - buf);
386 mono_profiler_code_buffer_new (buf, code - buf, MONO_PROFILER_CODE_BUFFER_HELPER, NULL);
388 /* Sanity check */
389 g_assert ((code - buf) <= buf_len);
391 g_assert (info);
392 tramp_name = mono_get_generic_trampoline_name (tramp_type);
393 *info = mono_tramp_info_create (tramp_name, buf, code - buf, ji, unwind_ops);
394 g_free (tramp_name);
396 return buf;
399 #define SPEC_TRAMP_SIZE 24
401 gpointer
402 mono_arch_create_specific_trampoline (gpointer arg1, MonoTrampolineType tramp_type, MonoDomain *domain, guint32 *code_len)
404 guint8 *code, *buf, *tramp;
405 gpointer *constants;
406 guint32 short_branch = FALSE;
407 guint32 size = SPEC_TRAMP_SIZE;
409 tramp = mono_get_trampoline_code (tramp_type);
411 if (domain) {
412 mono_domain_lock (domain);
413 code = buf = mono_domain_code_reserve_align (domain, size, 4);
414 if ((short_branch = branch_for_target_reachable (code + 4, tramp))) {
415 size = 12;
416 mono_domain_code_commit (domain, code, SPEC_TRAMP_SIZE, size);
418 mono_domain_unlock (domain);
419 } else {
420 code = buf = mono_global_codeman_reserve (size);
421 short_branch = FALSE;
424 /* we could reduce this to 12 bytes if tramp is within reach:
425 * ARM_PUSH ()
426 * ARM_BL ()
427 * method-literal
428 * The called code can access method using the lr register
429 * A 20 byte sequence could be:
430 * ARM_PUSH ()
431 * ARM_MOV_REG_REG (lr, pc)
432 * ARM_LDR_IMM (pc, pc, 0)
433 * method-literal
434 * tramp-literal
436 /* We save all the registers, except PC and SP */
437 ARM_PUSH (code, 0x5fff);
438 if (short_branch) {
439 constants = (gpointer*)code;
440 constants [0] = GUINT_TO_POINTER (short_branch | (1 << 24));
441 constants [1] = arg1;
442 code += 8;
443 } else {
444 ARM_LDR_IMM (code, ARMREG_R1, ARMREG_PC, 8); /* temp reg */
445 ARM_MOV_REG_REG (code, ARMREG_LR, ARMREG_PC);
446 code = emit_bx (code, ARMREG_R1);
448 constants = (gpointer*)code;
449 constants [0] = arg1;
450 constants [1] = tramp;
451 code += 8;
454 /* Flush instruction cache, since we've generated code */
455 mono_arch_flush_icache (buf, code - buf);
456 mono_profiler_code_buffer_new (buf, code - buf, MONO_PROFILER_CODE_BUFFER_SPECIFIC_TRAMPOLINE, mono_get_generic_trampoline_simple_name (tramp_type));
458 g_assert ((code - buf) <= size);
460 if (code_len)
461 *code_len = code - buf;
463 return buf;
467 * mono_arch_get_unbox_trampoline:
468 * @m: method pointer
469 * @addr: pointer to native code for @m
471 * when value type methods are called through the vtable we need to unbox the
472 * this argument. This method returns a pointer to a trampoline which does
473 * unboxing before calling the method
475 gpointer
476 mono_arch_get_unbox_trampoline (MonoMethod *m, gpointer addr)
478 guint8 *code, *start;
479 MonoDomain *domain = mono_domain_get ();
480 GSList *unwind_ops;
481 guint32 size = 16;
483 start = code = mono_domain_code_reserve (domain, size);
485 unwind_ops = mono_arch_get_cie_program ();
487 ARM_LDR_IMM (code, ARMREG_IP, ARMREG_PC, 4);
488 ARM_ADD_REG_IMM8 (code, ARMREG_R0, ARMREG_R0, sizeof (MonoObject));
489 code = emit_bx (code, ARMREG_IP);
490 *(guint32*)code = (guint32)addr;
491 code += 4;
492 mono_arch_flush_icache (start, code - start);
493 mono_profiler_code_buffer_new (start, code - start, MONO_PROFILER_CODE_BUFFER_UNBOX_TRAMPOLINE, m);
494 g_assert ((code - start) <= size);
495 /*g_print ("unbox trampoline at %d for %s:%s\n", this_pos, m->klass->name, m->name);
496 g_print ("unbox code is at %p for method at %p\n", start, addr);*/
498 mono_tramp_info_register (mono_tramp_info_create (NULL, start, code - start, NULL, unwind_ops), domain);
500 return start;
503 gpointer
504 mono_arch_get_static_rgctx_trampoline (MonoMethod *m, MonoMethodRuntimeGenericContext *mrgctx, gpointer addr)
506 guint8 *code, *start;
507 GSList *unwind_ops;
508 int buf_len = 16;
509 MonoDomain *domain = mono_domain_get ();
511 start = code = mono_domain_code_reserve (domain, buf_len);
513 unwind_ops = mono_arch_get_cie_program ();
515 ARM_LDR_IMM (code, MONO_ARCH_RGCTX_REG, ARMREG_PC, 0);
516 ARM_LDR_IMM (code, ARMREG_PC, ARMREG_PC, 0);
517 *(guint32*)code = (guint32)mrgctx;
518 code += 4;
519 *(guint32*)code = (guint32)addr;
520 code += 4;
522 g_assert ((code - start) <= buf_len);
524 mono_arch_flush_icache (start, code - start);
525 mono_profiler_code_buffer_new (start, code - start, MONO_PROFILER_CODE_BUFFER_GENERICS_TRAMPOLINE, NULL);
527 mono_tramp_info_register (mono_tramp_info_create (NULL, start, code - start, NULL, unwind_ops), domain);
529 return start;
532 gpointer
533 mono_arch_create_rgctx_lazy_fetch_trampoline (guint32 slot, MonoTrampInfo **info, gboolean aot)
535 guint8 *tramp;
536 guint8 *code, *buf;
537 int tramp_size;
538 guint32 code_len;
539 guint8 **rgctx_null_jumps;
540 int depth, index;
541 int i, njumps;
542 gboolean mrgctx;
543 MonoJumpInfo *ji = NULL;
544 GSList *unwind_ops = NULL;
546 mrgctx = MONO_RGCTX_SLOT_IS_MRGCTX (slot);
547 index = MONO_RGCTX_SLOT_INDEX (slot);
548 if (mrgctx)
549 index += MONO_SIZEOF_METHOD_RUNTIME_GENERIC_CONTEXT / sizeof (gpointer);
550 for (depth = 0; ; ++depth) {
551 int size = mono_class_rgctx_get_array_size (depth, mrgctx);
553 if (index < size - 1)
554 break;
555 index -= size - 1;
558 tramp_size = 64 + 16 * depth;
560 code = buf = mono_global_codeman_reserve (tramp_size);
562 unwind_ops = mono_arch_get_cie_program ();
564 rgctx_null_jumps = g_malloc (sizeof (guint8*) * (depth + 2));
565 njumps = 0;
567 /* The vtable/mrgctx is in R0 */
568 g_assert (MONO_ARCH_VTABLE_REG == ARMREG_R0);
570 if (mrgctx) {
571 /* get mrgctx ptr */
572 ARM_MOV_REG_REG (code, ARMREG_R1, ARMREG_R0);
573 } else {
574 /* load rgctx ptr from vtable */
575 g_assert (arm_is_imm12 (MONO_STRUCT_OFFSET (MonoVTable, runtime_generic_context)));
576 ARM_LDR_IMM (code, ARMREG_R1, ARMREG_R0, MONO_STRUCT_OFFSET (MonoVTable, runtime_generic_context));
577 /* is the rgctx ptr 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);
584 for (i = 0; i < depth; ++i) {
585 /* load ptr to next array */
586 if (mrgctx && i == 0) {
587 g_assert (arm_is_imm12 (MONO_SIZEOF_METHOD_RUNTIME_GENERIC_CONTEXT));
588 ARM_LDR_IMM (code, ARMREG_R1, ARMREG_R1, MONO_SIZEOF_METHOD_RUNTIME_GENERIC_CONTEXT);
589 } else {
590 ARM_LDR_IMM (code, ARMREG_R1, ARMREG_R1, 0);
592 /* is the ptr null? */
593 ARM_CMP_REG_IMM (code, ARMREG_R1, 0, 0);
594 /* if yes, jump to actual trampoline */
595 rgctx_null_jumps [njumps ++] = code;
596 ARM_B_COND (code, ARMCOND_EQ, 0);
599 /* fetch slot */
600 code = mono_arm_emit_load_imm (code, ARMREG_R2, sizeof (gpointer) * (index + 1));
601 ARM_LDR_REG_REG (code, ARMREG_R1, ARMREG_R1, ARMREG_R2);
602 /* is the slot null? */
603 ARM_CMP_REG_IMM (code, ARMREG_R1, 0, 0);
604 /* if yes, jump to actual trampoline */
605 rgctx_null_jumps [njumps ++] = code;
606 ARM_B_COND (code, ARMCOND_EQ, 0);
607 /* otherwise return, result is in R1 */
608 ARM_MOV_REG_REG (code, ARMREG_R0, ARMREG_R1);
609 code = emit_bx (code, ARMREG_LR);
611 g_assert (njumps <= depth + 2);
612 for (i = 0; i < njumps; ++i)
613 arm_patch (rgctx_null_jumps [i], code);
615 g_free (rgctx_null_jumps);
617 /* Slowpath */
619 /* The vtable/mrgctx is still in R0 */
621 if (aot) {
622 ji = mono_patch_info_list_prepend (ji, code - buf, MONO_PATCH_INFO_JIT_ICALL_ADDR, g_strdup_printf ("specific_trampoline_lazy_fetch_%u", slot));
623 ARM_LDR_IMM (code, ARMREG_R1, ARMREG_PC, 0);
624 ARM_B (code, 0);
625 *(gpointer*)code = NULL;
626 code += 4;
627 ARM_LDR_REG_REG (code, ARMREG_PC, ARMREG_PC, ARMREG_R1);
628 } else {
629 tramp = mono_arch_create_specific_trampoline (GUINT_TO_POINTER (slot), MONO_TRAMPOLINE_RGCTX_LAZY_FETCH, mono_get_root_domain (), &code_len);
631 /* Jump to the actual trampoline */
632 ARM_LDR_IMM (code, ARMREG_R1, ARMREG_PC, 0); /* temp reg */
633 code = emit_bx (code, ARMREG_R1);
634 *(gpointer*)code = tramp;
635 code += 4;
638 mono_arch_flush_icache (buf, code - buf);
639 mono_profiler_code_buffer_new (buf, code - buf, MONO_PROFILER_CODE_BUFFER_GENERICS_TRAMPOLINE, NULL);
641 g_assert (code - buf <= tramp_size);
643 char *name = mono_get_rgctx_fetch_trampoline_name (slot);
644 *info = mono_tramp_info_create (name, buf, code - buf, ji, unwind_ops);
645 g_free (name);
647 return buf;
650 gpointer
651 mono_arch_create_general_rgctx_lazy_fetch_trampoline (MonoTrampInfo **info, gboolean aot)
653 guint8 *code, *buf;
654 int tramp_size;
655 MonoJumpInfo *ji = NULL;
656 GSList *unwind_ops = NULL;
658 g_assert (aot);
660 tramp_size = 32;
662 code = buf = mono_global_codeman_reserve (tramp_size);
664 unwind_ops = mono_arch_get_cie_program ();
666 // FIXME: Currently, we always go to the slow path.
667 /* Load trampoline addr */
668 ARM_LDR_IMM (code, ARMREG_R1, MONO_ARCH_RGCTX_REG, 4);
669 /* The vtable/mrgctx is in R0 */
670 g_assert (MONO_ARCH_VTABLE_REG == ARMREG_R0);
671 code = emit_bx (code, ARMREG_R1);
673 mono_arch_flush_icache (buf, code - buf);
674 mono_profiler_code_buffer_new (buf, code - buf, MONO_PROFILER_CODE_BUFFER_GENERICS_TRAMPOLINE, NULL);
676 g_assert (code - buf <= tramp_size);
678 *info = mono_tramp_info_create ("rgctx_fetch_trampoline_general", buf, code - buf, ji, unwind_ops);
680 return buf;
683 static gpointer
684 handler_block_trampoline_helper (gpointer *ptr)
686 MonoJitTlsData *jit_tls = mono_tls_get_jit_tls ();
687 return jit_tls->handler_block_return_address;
690 gpointer
691 mono_arch_create_handler_block_trampoline (MonoTrampInfo **info, gboolean aot)
693 guint8 *tramp;
694 guint8 *code, *buf;
695 int tramp_size = 64;
696 MonoJumpInfo *ji = NULL;
697 GSList *unwind_ops = NULL;
699 g_assert (!aot);
701 code = buf = mono_global_codeman_reserve (tramp_size);
703 unwind_ops = mono_arch_get_cie_program ();
705 tramp = mono_arch_create_specific_trampoline (NULL, MONO_TRAMPOLINE_HANDLER_BLOCK_GUARD, NULL, NULL);
708 This trampoline restore the call chain of the handler block then jumps into the code that deals with it.
712 * We are in a method frame after the call emitted by OP_CALL_HANDLER.
714 /* Obtain jit_tls->handler_block_return_address */
715 ARM_LDR_IMM (code, ARMREG_R0, ARMREG_PC, 0);
716 ARM_B (code, 0);
717 *(gpointer*)code = handler_block_trampoline_helper;
718 code += 4;
720 /* Set it as the return address so the trampoline will return to it */
721 ARM_MOV_REG_REG (code, ARMREG_LR, ARMREG_R0);
723 /* Call the trampoline */
724 ARM_LDR_IMM (code, ARMREG_R0, ARMREG_PC, 0);
725 code = emit_bx (code, ARMREG_R0);
726 *(gpointer*)code = tramp;
727 code += 4;
729 mono_arch_flush_icache (buf, code - buf);
730 mono_profiler_code_buffer_new (buf, code - buf, MONO_PROFILER_CODE_BUFFER_HELPER, NULL);
731 g_assert (code - buf <= tramp_size);
733 *info = mono_tramp_info_create ("handler_block_trampoline", buf, code - buf, ji, unwind_ops);
735 return buf;
738 guint8*
739 mono_arch_create_sdb_trampoline (gboolean single_step, MonoTrampInfo **info, gboolean aot)
741 guint8 *buf, *code;
742 GSList *unwind_ops = NULL;
743 MonoJumpInfo *ji = NULL;
744 int frame_size;
746 buf = code = mono_global_codeman_reserve (96);
749 * Construct the MonoContext structure on the stack.
752 frame_size = sizeof (MonoContext);
753 frame_size = ALIGN_TO (frame_size, MONO_ARCH_FRAME_ALIGNMENT);
754 ARM_SUB_REG_IMM8 (code, ARMREG_SP, ARMREG_SP, frame_size);
756 /* save ip, lr and pc into their correspodings ctx.regs slots. */
757 ARM_STR_IMM (code, ARMREG_IP, ARMREG_SP, MONO_STRUCT_OFFSET (MonoContext, regs) + sizeof (mgreg_t) * ARMREG_IP);
758 ARM_STR_IMM (code, ARMREG_LR, ARMREG_SP, MONO_STRUCT_OFFSET (MonoContext, regs) + 4 * ARMREG_LR);
759 ARM_STR_IMM (code, ARMREG_LR, ARMREG_SP, MONO_STRUCT_OFFSET (MonoContext, regs) + 4 * ARMREG_PC);
761 /* save r0..r10 and fp */
762 ARM_ADD_REG_IMM8 (code, ARMREG_IP, ARMREG_SP, MONO_STRUCT_OFFSET (MonoContext, regs));
763 ARM_STM (code, ARMREG_IP, 0x0fff);
765 /* now we can update fp. */
766 ARM_MOV_REG_REG (code, ARMREG_FP, ARMREG_SP);
768 /* make ctx.esp hold the actual value of sp at the beginning of this method. */
769 ARM_ADD_REG_IMM8 (code, ARMREG_R0, ARMREG_FP, frame_size);
770 ARM_STR_IMM (code, ARMREG_R0, ARMREG_IP, 4 * ARMREG_SP);
771 ARM_STR_IMM (code, ARMREG_R0, ARMREG_FP, MONO_STRUCT_OFFSET (MonoContext, regs) + 4 * ARMREG_SP);
773 /* make ctx.eip hold the address of the call. */
774 //ARM_SUB_REG_IMM8 (code, ARMREG_LR, ARMREG_LR, 4);
775 ARM_STR_IMM (code, ARMREG_LR, ARMREG_FP, MONO_STRUCT_OFFSET (MonoContext, pc));
777 /* r0 now points to the MonoContext */
778 ARM_MOV_REG_REG (code, ARMREG_R0, ARMREG_FP);
780 /* call */
781 if (aot) {
782 if (single_step)
783 ji = mono_patch_info_list_prepend (ji, code - buf, MONO_PATCH_INFO_JIT_ICALL_ADDR, "debugger_agent_single_step_from_context");
784 else
785 ji = mono_patch_info_list_prepend (ji, code - buf, MONO_PATCH_INFO_JIT_ICALL_ADDR, "debugger_agent_breakpoint_from_context");
786 ARM_LDR_IMM (code, ARMREG_IP, ARMREG_PC, 0);
787 ARM_B (code, 0);
788 *(gpointer*)code = NULL;
789 code += 4;
790 ARM_LDR_REG_REG (code, ARMREG_IP, ARMREG_PC, ARMREG_IP);
791 ARM_BLX_REG (code, ARMREG_IP);
792 } else {
793 ARM_LDR_IMM (code, ARMREG_IP, ARMREG_PC, 0);
794 ARM_B (code, 0);
795 if (single_step)
796 *(gpointer*)code = debugger_agent_single_step_from_context;
797 else
798 *(gpointer*)code = debugger_agent_breakpoint_from_context;
799 code += 4;
800 ARM_BLX_REG (code, ARMREG_IP);
803 /* we're back; save ctx.eip and ctx.esp into the corresponding regs slots. */
804 ARM_LDR_IMM (code, ARMREG_R0, ARMREG_FP, MONO_STRUCT_OFFSET (MonoContext, pc));
805 ARM_STR_IMM (code, ARMREG_R0, ARMREG_FP, MONO_STRUCT_OFFSET (MonoContext, regs) + 4 * ARMREG_LR);
806 ARM_STR_IMM (code, ARMREG_R0, ARMREG_FP, MONO_STRUCT_OFFSET (MonoContext, regs) + 4 * ARMREG_PC);
808 /* make ip point to the regs array, then restore everything, including pc. */
809 ARM_ADD_REG_IMM8 (code, ARMREG_IP, ARMREG_FP, MONO_STRUCT_OFFSET (MonoContext, regs));
810 ARM_LDM (code, ARMREG_IP, 0xffff);
812 mono_arch_flush_icache (buf, code - buf);
813 mono_profiler_code_buffer_new (buf, code - buf, MONO_PROFILER_CODE_BUFFER_HELPER, NULL);
815 const char *tramp_name = single_step ? "sdb_single_step_trampoline" : "sdb_breakpoint_trampoline";
816 *info = mono_tramp_info_create (tramp_name, buf, code - buf, ji, unwind_ops);
818 return buf;
821 #else
823 guchar*
824 mono_arch_create_generic_trampoline (MonoTrampolineType tramp_type, MonoTrampInfo **info, gboolean aot)
826 g_assert_not_reached ();
827 return NULL;
830 gpointer
831 mono_arch_create_specific_trampoline (gpointer arg1, MonoTrampolineType tramp_type, MonoDomain *domain, guint32 *code_len)
833 g_assert_not_reached ();
834 return NULL;
837 gpointer
838 mono_arch_get_unbox_trampoline (MonoMethod *m, gpointer addr)
840 g_assert_not_reached ();
841 return NULL;
844 gpointer
845 mono_arch_get_static_rgctx_trampoline (MonoMethod *m, MonoMethodRuntimeGenericContext *mrgctx, gpointer addr)
847 g_assert_not_reached ();
848 return NULL;
851 gpointer
852 mono_arch_create_rgctx_lazy_fetch_trampoline (guint32 slot, MonoTrampInfo **info, gboolean aot)
854 g_assert_not_reached ();
855 return NULL;
858 gpointer
859 mono_arch_create_handler_block_trampoline (MonoTrampInfo **info, gboolean aot)
861 g_assert_not_reached ();
862 return NULL;
865 guint8*
866 mono_arch_create_sdb_trampoline (gboolean single_step, MonoTrampInfo **info, gboolean aot)
868 g_assert_not_reached ();
869 return NULL;
872 #endif /* DISABLE_JIT */
874 guint8*
875 mono_arch_get_call_target (guint8 *code)
877 guint32 ins = ((guint32*)(gpointer)code) [-1];
879 /* Should be a 'bl' or a 'b' */
880 if (((ins >> 25) & 0x7) == 0x5) {
881 gint32 disp = ((((gint32)ins) & 0xffffff) << 8) >> 8;
882 guint8 *target = code - 4 + 8 + (disp * 4);
884 return target;
885 } else {
886 return NULL;
890 guint32
891 mono_arch_get_plt_info_offset (guint8 *plt_entry, mgreg_t *regs, guint8 *code)
893 /* The offset is stored as the 4th word of the plt entry */
894 return ((guint32*)plt_entry) [3];
898 * Return the address of the PLT entry called by the thumb code CODE.
900 guint8*
901 mono_arm_get_thumb_plt_entry (guint8 *code)
903 int s, j1, j2, imm10, imm11, i1, i2, imm32;
904 guint8 *bl, *base;
905 guint16 t1, t2;
906 guint8 *target;
908 /* code should be right after a BL */
909 code = (guint8*)((mgreg_t)code & ~1);
910 base = (guint8*)((mgreg_t)code & ~3);
911 bl = code - 4;
912 t1 = ((guint16*)bl) [0];
913 t2 = ((guint16*)bl) [1];
915 g_assert ((t1 >> 11) == 0x1e);
917 s = (t1 >> 10) & 0x1;
918 imm10 = (t1 >> 0) & 0x3ff;
919 j1 = (t2 >> 13) & 0x1;
920 j2 = (t2 >> 11) & 0x1;
921 imm11 = t2 & 0x7ff;
923 i1 = (s ^ j1) ? 0 : 1;
924 i2 = (s ^ j2) ? 0 : 1;
926 imm32 = (imm11 << 1) | (imm10 << 12) | (i2 << 22) | (i1 << 23);
927 if (s)
928 /* Sign extend from 24 bits to 32 bits */
929 imm32 = ((gint32)imm32 << 8) >> 8;
931 target = code + imm32;
933 /* target now points to the thumb plt entry */
934 /* ldr.w r12, [pc, #8] */
935 g_assert (((guint16*)target) [0] == 0xf8df);
936 g_assert (((guint16*)target) [1] == 0xc008);
939 * The PLT info offset is at offset 16, but mono_arch_get_plt_entry_offset () returns
940 * the 3rd word, so compensate by returning a different value.
942 target += 4;
944 return target;
947 #ifndef DISABLE_JIT
950 * mono_arch_get_gsharedvt_arg_trampoline:
952 * See tramp-x86.c for documentation.
954 gpointer
955 mono_arch_get_gsharedvt_arg_trampoline (MonoDomain *domain, gpointer arg, gpointer addr)
957 guint8 *code, *buf;
958 int buf_len;
959 gpointer *constants;
961 buf_len = 24;
963 buf = code = mono_domain_code_reserve (domain, buf_len);
965 /* Similar to the specialized trampoline code */
966 ARM_PUSH (code, (1 << ARMREG_R0) | (1 << ARMREG_R1) | (1 << ARMREG_R2) | (1 << ARMREG_R3) | (1 << ARMREG_LR));
967 ARM_LDR_IMM (code, ARMREG_IP, ARMREG_PC, 8);
968 /* arg is passed in LR */
969 ARM_LDR_IMM (code, ARMREG_LR, ARMREG_PC, 0);
970 code = emit_bx (code, ARMREG_IP);
971 constants = (gpointer*)code;
972 constants [0] = arg;
973 constants [1] = addr;
974 code += 8;
976 g_assert ((code - buf) <= buf_len);
978 mono_arch_flush_icache (buf, code - buf);
979 mono_profiler_code_buffer_new (buf, code - buf, MONO_PROFILER_CODE_BUFFER_GENERICS_TRAMPOLINE, NULL);
981 mono_tramp_info_register (mono_tramp_info_create (NULL, buf, code - buf, NULL, NULL), domain);
983 return buf;
986 #else
988 gpointer
989 mono_arch_get_gsharedvt_arg_trampoline (MonoDomain *domain, gpointer arg, gpointer addr)
991 g_assert_not_reached ();
992 return NULL;
995 #endif