3 * JIT trampoline code for ARM64
5 * Copyright 2013 Xamarin Inc
7 * Based on tramp-arm.c:
10 * Paolo Molaro (lupus@ximian.com)
12 * (C) 2001-2003 Ximian, Inc.
13 * Copyright 2003-2011 Novell Inc
14 * Copyright 2011 Xamarin Inc
15 * Licensed under the MIT license. See LICENSE file in the project root for full license information.
19 #include "debugger-agent.h"
21 #include <mono/arch/arm64/arm64-codegen.h>
22 #include <mono/metadata/abi-details.h>
24 #ifdef ENABLE_INTERPRETER
25 #include "interp/interp.h"
29 #define ALIGN_TO(val,align) ((((guint64)val) + ((align) - 1)) & ~((align) - 1))
32 mono_arch_patch_callsite (guint8 *method_start, guint8 *code_ptr, guint8 *addr)
34 mono_arm_patch (code_ptr - 4, addr, MONO_R_ARM64_BL);
35 mono_arch_flush_icache (code_ptr - 4, 4);
39 mono_arch_patch_plt_entry (guint8 *code, gpointer *got, mgreg_t *regs, guint8 *addr)
46 * Decode the address loaded by the PLT entry emitted by arch_emit_plt_entry () in
51 ins = ((guint32*)code) [0];
52 g_assert (((ins >> 24) & 0x1f) == 0x10);
53 disp = (((ins >> 5) & 0x7ffff) << 2) | ((ins >> 29) & 0x3);
54 /* FIXME: disp is signed */
55 g_assert ((disp >> 20) == 0);
57 slot_addr = ((guint64)code + (disp << 12)) & ~0xfff;
59 /* add x16, x16, :lo12:got */
60 ins = ((guint32*)code) [1];
61 g_assert (((ins >> 22) & 0x3) == 0);
62 slot_addr += (ins >> 10) & 0xfff;
64 /* ldr x16, [x16, <offset>] */
65 ins = ((guint32*)code) [2];
66 g_assert (((ins >> 24) & 0x3f) == 0x39);
67 slot_addr += ((ins >> 10) & 0xfff) * 8;
69 g_assert (*(guint64*)slot_addr);
70 *(gpointer*)slot_addr = addr;
74 mono_arch_get_call_target (guint8 *code)
81 imm = *(guint32*)code;
83 g_assert (((imm >> 31) & 0x1) == 0x1);
84 g_assert (((imm >> 26) & 0x7) == 0x5);
86 disp = (imm & 0x3ffffff);
87 if ((disp >> 25) != 0)
88 /* Negative, sing extend to 32 bits */
89 disp = disp | 0xfc000000;
91 return code + (disp * 4);
95 mono_arch_get_plt_info_offset (guint8 *plt_entry, mgreg_t *regs, guint8 *code)
97 /* The offset is stored as the 5th word of the plt entry */
98 return ((guint32*)plt_entry) [4];
102 mono_arm_handler_block_trampoline_helper (gpointer *ptr)
104 MonoJitTlsData *jit_tls = mono_tls_get_jit_tls ();
105 return jit_tls->handler_block_return_address;
111 mono_arch_create_generic_trampoline (MonoTrampolineType tramp_type, MonoTrampInfo **info, gboolean aot)
113 guint8 *code, *buf, *tramp, *labels [16];
115 int frame_size, offset, gregs_offset, num_fregs, fregs_offset, arg_offset, lmf_offset, res_offset;
116 guint64 gregs_regset;
117 GSList *unwind_ops = NULL;
118 MonoJumpInfo *ji = NULL;
122 buf = code = mono_global_codeman_reserve (buf_len);
125 * We are getting called by a specific trampoline, ip1 contains the trampoline argument.
128 /* Compute stack frame size and offsets */
133 gregs_offset = offset;
136 // FIXME: Save 128 bits
137 /* Only have to save the argument regs */
139 fregs_offset = offset;
140 offset += num_fregs * 8;
149 offset += sizeof (MonoLMF);
151 frame_size = ALIGN_TO (offset, MONO_ARCH_FRAME_ALIGNMENT);
153 /* Setup stack frame */
156 arm_subx_imm (code, ARMREG_SP, ARMREG_SP, 256);
159 arm_subx_imm (code, ARMREG_SP, ARMREG_SP, imm);
160 arm_stpx (code, ARMREG_FP, ARMREG_LR, ARMREG_SP, 0);
161 arm_movspx (code, ARMREG_FP, ARMREG_SP);
164 // FIXME: Optimize this
165 gregs_regset = ~((1 << ARMREG_FP) | (1 << ARMREG_SP));
166 code = mono_arm_emit_store_regarray (code, gregs_regset, ARMREG_FP, gregs_offset);
168 for (i = 0; i < num_fregs; ++i)
169 arm_strfpx (code, i, ARMREG_FP, fregs_offset + (i * 8));
170 /* Save trampoline arg */
171 arm_strx (code, ARMREG_IP1, ARMREG_FP, arg_offset);
174 arm_addx_imm (code, ARMREG_IP0, ARMREG_FP, lmf_offset);
175 code = mono_arm_emit_store_regset (code, MONO_ARCH_LMF_REGS, ARMREG_IP0, MONO_STRUCT_OFFSET (MonoLMF, gregs));
177 arm_ldrx (code, ARMREG_IP1, ARMREG_FP, 0);
178 arm_strx (code, ARMREG_IP1, ARMREG_IP0, MONO_STRUCT_OFFSET (MonoLMF, gregs) + (MONO_ARCH_LMF_REG_FP * 8));
180 arm_movx (code, ARMREG_IP1, ARMREG_FP);
183 arm_addx_imm (code, ARMREG_IP1, ARMREG_IP1, 256);
186 arm_addx_imm (code, ARMREG_IP1, ARMREG_IP1, imm);
187 arm_strx (code, ARMREG_IP1, ARMREG_IP0, MONO_STRUCT_OFFSET (MonoLMF, gregs) + (MONO_ARCH_LMF_REG_SP * 8));
189 if (tramp_type == MONO_TRAMPOLINE_JUMP)
190 arm_movx (code, ARMREG_LR, ARMREG_RZR);
192 arm_ldrx (code, ARMREG_LR, ARMREG_FP, 8);
193 arm_strx (code, ARMREG_LR, ARMREG_IP0, MONO_STRUCT_OFFSET (MonoLMF, pc));
196 /* Similar to emit_save_lmf () */
198 code = mono_arm_emit_aotconst (&ji, code, buf, ARMREG_IP0, MONO_PATCH_INFO_JIT_ICALL_ADDR, "mono_get_lmf_addr");
200 tramp = (guint8*)mono_get_lmf_addr;
201 code = mono_arm_emit_imm64 (code, ARMREG_IP0, (guint64)tramp);
203 arm_blrx (code, ARMREG_IP0);
204 /* r0 contains the address of the tls slot holding the current lmf */
206 arm_addx_imm (code, ARMREG_IP0, ARMREG_FP, lmf_offset);
207 /* lmf->lmf_addr = lmf_addr */
208 arm_strx (code, ARMREG_R0, ARMREG_IP0, MONO_STRUCT_OFFSET (MonoLMF, lmf_addr));
209 /* lmf->previous_lmf = *lmf_addr */
210 arm_ldrx (code, ARMREG_IP1, ARMREG_R0, 0);
211 arm_strx (code, ARMREG_IP1, ARMREG_IP0, MONO_STRUCT_OFFSET (MonoLMF, previous_lmf));
212 /* *lmf_addr = lmf */
213 arm_strx (code, ARMREG_IP0, ARMREG_R0, 0);
215 /* Call the C trampoline function */
217 arm_addx_imm (code, ARMREG_R0, ARMREG_FP, gregs_offset);
219 if (tramp_type == MONO_TRAMPOLINE_JUMP)
220 arm_movx (code, ARMREG_R1, ARMREG_RZR);
222 arm_ldrx (code, ARMREG_R1, ARMREG_FP, gregs_offset + (ARMREG_LR * 8));
224 if (MONO_TRAMPOLINE_TYPE_HAS_ARG (tramp_type))
226 arm_ldrx (code, ARMREG_R2, ARMREG_FP, gregs_offset + (ARMREG_R0 * 8));
228 arm_ldrx (code, ARMREG_R2, ARMREG_FP, arg_offset);
229 /* Arg 4 = trampoline addr */
230 arm_movx (code, ARMREG_R3, ARMREG_RZR);
233 char *icall_name = g_strdup_printf ("trampoline_func_%d", tramp_type);
234 code = mono_arm_emit_aotconst (&ji, code, buf, ARMREG_IP0, MONO_PATCH_INFO_JIT_ICALL_ADDR, icall_name);
236 tramp = (guint8*)mono_get_trampoline_func (tramp_type);
237 code = mono_arm_emit_imm64 (code, ARMREG_IP0, (guint64)tramp);
239 arm_blrx (code, ARMREG_IP0);
241 /* Save the result */
242 arm_strx (code, ARMREG_R0, ARMREG_FP, res_offset);
245 /* Similar to emit_restore_lmf () */
246 /* Clobbers ip0/ip1 */
248 arm_addx_imm (code, ARMREG_IP0, ARMREG_FP, lmf_offset);
249 /* ip1 = lmf->previous_lmf */
250 arm_ldrx (code, ARMREG_IP1, ARMREG_IP0, MONO_STRUCT_OFFSET (MonoLMF, previous_lmf));
251 /* ip0 = lmf->lmf_addr */
252 arm_ldrx (code, ARMREG_IP0, ARMREG_IP0, MONO_STRUCT_OFFSET (MonoLMF, lmf_addr));
253 /* *lmf_addr = previous_lmf */
254 arm_strx (code, ARMREG_IP1, ARMREG_IP0, 0);
256 /* Check for thread interruption */
257 /* This is not perf critical code so no need to check the interrupt flag */
259 code = mono_arm_emit_aotconst (&ji, code, buf, ARMREG_IP0, MONO_PATCH_INFO_JIT_ICALL_ADDR, "mono_thread_force_interruption_checkpoint_noraise");
261 code = mono_arm_emit_imm64 (code, ARMREG_IP0, (guint64)mono_thread_force_interruption_checkpoint_noraise);
263 arm_blrx (code, ARMREG_IP0);
264 /* Check whenever there is an exception to be thrown */
266 arm_cbnzx (code, ARMREG_R0, 0);
271 /* Only have to load the argument regs (r0..r8) and the rgctx reg */
272 code = mono_arm_emit_load_regarray (code, 0x1ff | (1 << ARMREG_LR) | (1 << MONO_ARCH_RGCTX_REG), ARMREG_FP, gregs_offset);
274 for (i = 0; i < num_fregs; ++i)
275 arm_ldrfpx (code, i, ARMREG_FP, fregs_offset + (i * 8));
277 /* Load the result */
278 arm_ldrx (code, ARMREG_IP1, ARMREG_FP, res_offset);
280 /* These trampolines return a value */
281 if (tramp_type == MONO_TRAMPOLINE_RGCTX_LAZY_FETCH)
282 arm_movx (code, ARMREG_R0, ARMREG_IP1);
285 code = mono_arm_emit_destroy_frame (code, frame_size, ((1 << ARMREG_IP0)));
287 if (tramp_type == MONO_TRAMPOLINE_RGCTX_LAZY_FETCH)
288 arm_retx (code, ARMREG_LR);
290 arm_brx (code, ARMREG_IP1);
293 mono_arm_patch (labels [0], code, MONO_R_ARM64_CBZ);
296 * We have an exception we want to throw in the caller's frame, so pop
297 * the trampoline frame and throw from the caller.
299 code = mono_arm_emit_destroy_frame (code, frame_size, ((1 << ARMREG_IP0)));
300 /* We are in the parent frame, the exception is in x0 */
302 * EH is initialized after trampolines, so get the address of the variable
303 * which contains throw_exception, and load it from there.
306 /* Not really a jit icall */
307 code = mono_arm_emit_aotconst (&ji, code, buf, ARMREG_IP0, MONO_PATCH_INFO_JIT_ICALL_ADDR, "throw_exception_addr");
309 code = mono_arm_emit_imm64 (code, ARMREG_IP0, (guint64)mono_get_throw_exception_addr ());
311 arm_ldrx (code, ARMREG_IP0, ARMREG_IP0, 0);
312 /* lr contains the return address, the trampoline will use it as the throw site */
313 arm_brx (code, ARMREG_IP0);
315 g_assert ((code - buf) < buf_len);
316 mono_arch_flush_icache (buf, code - buf);
319 tramp_name = mono_get_generic_trampoline_name (tramp_type);
320 *info = mono_tramp_info_create (tramp_name, buf, code - buf, ji, unwind_ops);
328 mono_arch_create_specific_trampoline (gpointer arg1, MonoTrampolineType tramp_type, MonoDomain *domain, guint32 *code_len)
330 guint8 *code, *buf, *tramp;
334 * Return a trampoline which calls generic trampoline TRAMP_TYPE passing in ARG1.
335 * Pass the argument in ip1, clobbering ip0.
337 tramp = mono_get_trampoline_code (tramp_type);
339 buf = code = mono_global_codeman_reserve (buf_len);
341 code = mono_arm_emit_imm64 (code, ARMREG_IP1, (guint64)arg1);
342 code = mono_arm_emit_imm64 (code, ARMREG_IP0, (guint64)tramp);
344 arm_brx (code, ARMREG_IP0);
346 g_assert ((code - buf) < buf_len);
347 mono_arch_flush_icache (buf, code - buf);
349 *code_len = code - buf;
355 mono_arch_get_unbox_trampoline (MonoMethod *m, gpointer addr)
357 guint8 *code, *start;
359 MonoDomain *domain = mono_domain_get ();
361 start = code = mono_domain_code_reserve (domain, size);
362 code = mono_arm_emit_imm64 (code, ARMREG_IP0, (guint64)addr);
363 arm_addx_imm (code, ARMREG_R0, ARMREG_R0, sizeof (MonoObject));
364 arm_brx (code, ARMREG_IP0);
366 g_assert ((code - start) <= size);
367 mono_arch_flush_icache (start, code - start);
372 mono_arch_get_static_rgctx_trampoline (gpointer arg, gpointer addr)
374 guint8 *code, *start;
375 guint32 buf_len = 32;
376 MonoDomain *domain = mono_domain_get ();
378 start = code = mono_domain_code_reserve (domain, buf_len);
379 code = mono_arm_emit_imm64 (code, MONO_ARCH_RGCTX_REG, (guint64)arg);
380 code = mono_arm_emit_imm64 (code, ARMREG_IP0, (guint64)addr);
381 arm_brx (code, ARMREG_IP0);
383 g_assert ((code - start) <= buf_len);
385 mono_arch_flush_icache (start, code - start);
391 mono_arch_create_rgctx_lazy_fetch_trampoline (guint32 slot, MonoTrampInfo **info, gboolean aot)
395 int i, depth, index, njumps;
397 guint8 **rgctx_null_jumps;
398 MonoJumpInfo *ji = NULL;
399 GSList *unwind_ops = NULL;
403 is_mrgctx = MONO_RGCTX_SLOT_IS_MRGCTX (slot);
404 index = MONO_RGCTX_SLOT_INDEX (slot);
406 index += MONO_SIZEOF_METHOD_RUNTIME_GENERIC_CONTEXT / sizeof (gpointer);
407 for (depth = 0; ; ++depth) {
408 int size = mono_class_rgctx_get_array_size (depth, is_mrgctx);
410 if (index < size - 1)
415 buf_size = 64 + 16 * depth;
416 code = buf = mono_global_codeman_reserve (buf_size);
418 rgctx_null_jumps = g_malloc0 (sizeof (guint8*) * (depth + 2));
421 /* The vtable/mrgtx is in R0 */
422 g_assert (MONO_ARCH_VTABLE_REG == ARMREG_R0);
426 arm_movx (code, ARMREG_IP1, ARMREG_R0);
428 /* load rgctx ptr from vtable */
429 code = mono_arm_emit_ldrx (code, ARMREG_IP1, ARMREG_R0, MONO_STRUCT_OFFSET (MonoVTable, runtime_generic_context));
430 /* is the rgctx ptr null? */
431 /* if yes, jump to actual trampoline */
432 rgctx_null_jumps [njumps ++] = code;
433 arm_cbzx (code, ARMREG_IP1, 0);
436 for (i = 0; i < depth; ++i) {
437 /* load ptr to next array */
438 if (is_mrgctx && i == 0) {
439 code = mono_arm_emit_ldrx (code, ARMREG_IP1, ARMREG_IP1, MONO_SIZEOF_METHOD_RUNTIME_GENERIC_CONTEXT);
441 code = mono_arm_emit_ldrx (code, ARMREG_IP1, ARMREG_IP1, 0);
443 /* is the ptr null? */
444 /* if yes, jump to actual trampoline */
445 rgctx_null_jumps [njumps ++] = code;
446 arm_cbzx (code, ARMREG_IP1, 0);
450 code = mono_arm_emit_ldrx (code, ARMREG_IP1, ARMREG_IP1, sizeof (gpointer) * (index + 1));
451 /* is the slot null? */
452 /* if yes, jump to actual trampoline */
453 rgctx_null_jumps [njumps ++] = code;
454 arm_cbzx (code, ARMREG_IP1, 0);
455 /* otherwise return, result is in IP1 */
456 arm_movx (code, ARMREG_R0, ARMREG_IP1);
457 arm_brx (code, ARMREG_LR);
459 g_assert (njumps <= depth + 2);
460 for (i = 0; i < njumps; ++i)
461 mono_arm_patch (rgctx_null_jumps [i], code, MONO_R_ARM64_CBZ);
463 g_free (rgctx_null_jumps);
467 /* Call mono_rgctx_lazy_fetch_trampoline (), passing in the slot as argument */
468 /* The vtable/mrgctx is still in R0 */
470 code = mono_arm_emit_aotconst (&ji, code, buf, ARMREG_IP0, MONO_PATCH_INFO_JIT_ICALL_ADDR, g_strdup_printf ("specific_trampoline_lazy_fetch_%u", slot));
472 tramp = mono_arch_create_specific_trampoline (GUINT_TO_POINTER (slot), MONO_TRAMPOLINE_RGCTX_LAZY_FETCH, mono_get_root_domain (), &code_len);
473 code = mono_arm_emit_imm64 (code, ARMREG_IP0, (guint64)tramp);
475 arm_brx (code, ARMREG_IP0);
477 mono_arch_flush_icache (buf, code - buf);
479 g_assert (code - buf <= buf_size);
482 char *name = mono_get_rgctx_fetch_trampoline_name (slot);
483 *info = mono_tramp_info_create (name, buf, code - buf, ji, unwind_ops);
491 mono_arch_create_general_rgctx_lazy_fetch_trampoline (MonoTrampInfo **info, gboolean aot)
495 MonoJumpInfo *ji = NULL;
496 GSList *unwind_ops = NULL;
502 code = buf = mono_global_codeman_reserve (tramp_size);
504 mono_add_unwind_op_def_cfa (unwind_ops, code, buf, ARMREG_SP, 0);
506 // FIXME: Currently, we always go to the slow path.
507 /* Load trampoline addr */
508 arm_ldrx (code, ARMREG_IP0, MONO_ARCH_RGCTX_REG, 8);
509 /* The vtable/mrgctx is in R0 */
510 g_assert (MONO_ARCH_VTABLE_REG == ARMREG_R0);
511 arm_brx (code, ARMREG_IP0);
513 mono_arch_flush_icache (buf, code - buf);
515 g_assert (code - buf <= tramp_size);
518 *info = mono_tramp_info_create ("rgctx_fetch_trampoline_general", buf, code - buf, ji, unwind_ops);
524 mono_arch_create_handler_block_trampoline (MonoTrampInfo **info, gboolean aot)
529 MonoJumpInfo *ji = NULL;
530 GSList *unwind_ops = NULL;
532 code = buf = mono_global_codeman_reserve (tramp_size);
537 This trampoline restore the call chain of the handler block then jumps into the code that deals with it.
541 * We are in a method frame after the call emitted by OP_CALL_HANDLER.
544 code = mono_arm_emit_aotconst (&ji, code, buf, ARMREG_IP0, MONO_PATCH_INFO_JIT_ICALL_ADDR, "mono_arm_handler_block_trampoline_helper");
546 code = mono_arm_emit_imm64 (code, ARMREG_IP0, (guint64)mono_arm_handler_block_trampoline_helper);
547 /* Set it as the return address so the trampoline will return to it */
548 arm_movx (code, ARMREG_LR, ARMREG_IP0);
550 /* Call the trampoline */
552 char *name = g_strdup_printf ("trampoline_func_%d", MONO_TRAMPOLINE_HANDLER_BLOCK_GUARD);
553 code = mono_arm_emit_aotconst (&ji, code, buf, ARMREG_IP0, MONO_PATCH_INFO_JIT_ICALL_ADDR, name);
555 tramp = mono_arch_create_specific_trampoline (NULL, MONO_TRAMPOLINE_HANDLER_BLOCK_GUARD, NULL, NULL);
556 code = mono_arm_emit_imm64 (code, ARMREG_IP0, (guint64)tramp);
558 arm_brx (code, ARMREG_IP0);
560 mono_arch_flush_icache (buf, code - buf);
561 mono_profiler_code_buffer_new (buf, code - buf, MONO_PROFILER_CODE_BUFFER_HELPER, NULL);
562 g_assert (code - buf <= tramp_size);
564 *info = mono_tramp_info_create ("handler_block_trampoline", buf, code - buf, ji, unwind_ops);
570 * mono_arch_create_sdb_trampoline:
572 * Return a trampoline which captures the current context, passes it to
573 * debugger_agent_single_step_from_context ()/debugger_agent_breakpoint_from_context (),
574 * then restores the (potentially changed) context.
577 mono_arch_create_sdb_trampoline (gboolean single_step, MonoTrampInfo **info, gboolean aot)
579 int tramp_size = 512;
580 int offset, imm, frame_size, ctx_offset;
581 guint64 gregs_regset;
583 GSList *unwind_ops = NULL;
584 MonoJumpInfo *ji = NULL;
586 code = buf = mono_global_codeman_reserve (tramp_size);
588 /* Compute stack frame size and offsets */
594 offset += sizeof (MonoContext);
595 offset = ALIGN_TO (offset, MONO_ARCH_FRAME_ALIGNMENT);
598 // FIXME: Unwind info
600 /* Setup stack frame */
603 arm_subx_imm (code, ARMREG_SP, ARMREG_SP, 256);
606 arm_subx_imm (code, ARMREG_SP, ARMREG_SP, imm);
607 arm_stpx (code, ARMREG_FP, ARMREG_LR, ARMREG_SP, 0);
608 arm_movspx (code, ARMREG_FP, ARMREG_SP);
610 /* Initialize a MonoContext structure on the stack */
611 /* No need to save fregs */
612 gregs_regset = ~((1 << ARMREG_FP) | (1 << ARMREG_SP));
613 code = mono_arm_emit_store_regarray (code, gregs_regset, ARMREG_FP, ctx_offset + G_STRUCT_OFFSET (MonoContext, regs));
615 arm_ldrx (code, ARMREG_IP1, ARMREG_FP, 0);
616 arm_strx (code, ARMREG_IP1, ARMREG_FP, ctx_offset + G_STRUCT_OFFSET (MonoContext, regs) + (ARMREG_FP * 8));
618 arm_movx (code, ARMREG_IP1, ARMREG_FP);
621 arm_addx_imm (code, ARMREG_IP1, ARMREG_IP1, 256);
624 arm_addx_imm (code, ARMREG_IP1, ARMREG_IP1, imm);
625 arm_strx (code, ARMREG_IP1, ARMREG_FP, ctx_offset + G_STRUCT_OFFSET (MonoContext, regs) + (ARMREG_SP * 8));
627 arm_ldrx (code, ARMREG_IP1, ARMREG_FP, 8);
628 arm_strx (code, ARMREG_IP1, ARMREG_FP, ctx_offset + G_STRUCT_OFFSET (MonoContext, pc));
630 /* Call the single step/breakpoint function in sdb */
632 arm_addx_imm (code, ARMREG_R0, ARMREG_FP, ctx_offset);
635 code = mono_arm_emit_aotconst (&ji, code, buf, ARMREG_IP0, MONO_PATCH_INFO_JIT_ICALL_ADDR, "debugger_agent_single_step_from_context");
637 code = mono_arm_emit_aotconst (&ji, code, buf, ARMREG_IP0, MONO_PATCH_INFO_JIT_ICALL_ADDR, "debugger_agent_breakpoint_from_context");
639 gpointer addr = single_step ? debugger_agent_single_step_from_context : debugger_agent_breakpoint_from_context;
641 code = mono_arm_emit_imm64 (code, ARMREG_IP0, (guint64)addr);
643 arm_blrx (code, ARMREG_IP0);
646 /* Save fp/pc into the frame block */
647 arm_ldrx (code, ARMREG_IP0, ARMREG_FP, ctx_offset + G_STRUCT_OFFSET (MonoContext, regs) + (ARMREG_FP * 8));
648 arm_strx (code, ARMREG_IP0, ARMREG_FP, 0);
649 arm_ldrx (code, ARMREG_IP0, ARMREG_FP, ctx_offset + G_STRUCT_OFFSET (MonoContext, pc));
650 arm_strx (code, ARMREG_IP0, ARMREG_FP, 8);
651 gregs_regset = ~((1 << ARMREG_FP) | (1 << ARMREG_SP));
652 code = mono_arm_emit_load_regarray (code, gregs_regset, ARMREG_FP, ctx_offset + G_STRUCT_OFFSET (MonoContext, regs));
654 code = mono_arm_emit_destroy_frame (code, frame_size, ((1 << ARMREG_IP0) | (1 << ARMREG_IP1)));
656 arm_retx (code, ARMREG_LR);
658 mono_arch_flush_icache (code, code - buf);
659 g_assert (code - buf <= tramp_size);
661 const char *tramp_name = single_step ? "sdb_single_step_trampoline" : "sdb_breakpoint_trampoline";
662 *info = mono_tramp_info_create (tramp_name, buf, code - buf, ji, unwind_ops);
668 * mono_arch_get_enter_icall_trampoline:
670 * See tramp-amd64.c for documentation.
673 mono_arch_get_enter_icall_trampoline (MonoTrampInfo **info)
675 #ifdef ENABLE_INTERPRETER
676 const int gregs_num = INTERP_ICALL_TRAMP_IARGS;
677 const int fregs_num = INTERP_ICALL_TRAMP_FARGS;
679 guint8 *start = NULL, *code, *label_gexits [gregs_num], *label_fexits [fregs_num], *label_leave_tramp [3], *label_is_float_ret;
680 MonoJumpInfo *ji = NULL;
681 GSList *unwind_ops = NULL;
682 int buf_len, i, framesize = 0, off_methodargs, off_targetaddr;
684 buf_len = 512 + 1024;
685 start = code = (guint8 *) mono_global_codeman_reserve (buf_len);
688 framesize += 2 * sizeof (mgreg_t);
690 off_methodargs = framesize;
691 framesize += sizeof (mgreg_t);
693 off_targetaddr = framesize;
694 framesize += sizeof (mgreg_t);
696 framesize = ALIGN_TO (framesize, MONO_ARCH_FRAME_ALIGNMENT);
698 /* allocate space on stack for argument passing */
699 const int stack_space = ALIGN_TO (((gregs_num - ARMREG_R7) * sizeof (mgreg_t)), MONO_ARCH_FRAME_ALIGNMENT);
701 arm_subx_imm (code, ARMREG_SP, ARMREG_SP, stack_space + framesize);
702 arm_stpx (code, ARMREG_FP, ARMREG_LR, ARMREG_SP, stack_space);
703 arm_addx_imm (code, ARMREG_FP, ARMREG_SP, stack_space);
705 /* save InterpMethodArguments* onto stack */
706 arm_strx (code, ARMREG_R1, ARMREG_FP, off_methodargs);
708 /* save target address onto stack */
709 arm_strx (code, ARMREG_R0, ARMREG_FP, off_targetaddr);
711 /* load pointer to InterpMethodArguments* into IP0 */
712 arm_movx (code, ARMREG_IP0, ARMREG_R1);
714 /* move flen into R9 */
715 arm_ldrx (code, ARMREG_R9, ARMREG_IP0, MONO_STRUCT_OFFSET (InterpMethodArguments, flen));
716 /* load pointer to fargs into R10 */
717 arm_ldrx (code, ARMREG_R10, ARMREG_IP0, MONO_STRUCT_OFFSET (InterpMethodArguments, fargs));
719 for (i = 0; i < fregs_num; ++i) {
720 arm_cmpx_imm (code, ARMREG_R9, 0);
721 label_fexits [i] = code;
722 arm_bcc (code, ARMCOND_EQ, 0);
724 g_assert (i <= ARMREG_D7); /* otherwise, need to pass args on stack */
725 arm_ldrfpx (code, i, ARMREG_R10, i * sizeof (double));
726 arm_subx_imm (code, ARMREG_R9, ARMREG_R9, 1);
729 for (i = 0; i < fregs_num; i++)
730 mono_arm_patch (label_fexits [i], code, MONO_R_ARM64_BCC);
732 /* move ilen into R9 */
733 arm_ldrx (code, ARMREG_R9, ARMREG_IP0, MONO_STRUCT_OFFSET (InterpMethodArguments, ilen));
734 /* load pointer to iargs into R10 */
735 arm_ldrx (code, ARMREG_R10, ARMREG_IP0, MONO_STRUCT_OFFSET (InterpMethodArguments, iargs));
737 int stack_offset = 0;
738 for (i = 0; i < gregs_num; i++) {
739 arm_cmpx_imm (code, ARMREG_R9, 0);
740 label_gexits [i] = code;
741 arm_bcc (code, ARMCOND_EQ, 0);
743 if (i <= ARMREG_R7) {
744 arm_ldrx (code, i, ARMREG_R10, i * sizeof (mgreg_t));
746 arm_ldrx (code, ARMREG_R11, ARMREG_R10, i * sizeof (mgreg_t));
747 arm_strx (code, ARMREG_R11, ARMREG_SP, stack_offset);
748 stack_offset += sizeof (mgreg_t);
750 arm_subx_imm (code, ARMREG_R9, ARMREG_R9, 1);
753 for (i = 0; i < gregs_num; i++)
754 mono_arm_patch (label_gexits [i], code, MONO_R_ARM64_BCC);
756 /* load target addr */
757 arm_ldrx (code, ARMREG_R11, ARMREG_FP, off_targetaddr);
759 /* call into native function */
760 arm_blrx (code, ARMREG_R11);
762 /* load InterpMethodArguments */
763 arm_ldrx (code, ARMREG_IP0, ARMREG_FP, off_methodargs);
765 /* load is_float_ret */
766 arm_ldrx (code, ARMREG_R11, ARMREG_IP0, MONO_STRUCT_OFFSET (InterpMethodArguments, is_float_ret));
768 /* check if a float return value is expected */
769 arm_cmpx_imm (code, ARMREG_R11, 0);
770 label_is_float_ret = code;
771 arm_bcc (code, ARMCOND_NE, 0);
775 arm_ldrx (code, ARMREG_R11, ARMREG_IP0, MONO_STRUCT_OFFSET (InterpMethodArguments, retval));
777 arm_cmpx_imm (code, ARMREG_R11, 0);
778 label_leave_tramp [0] = code;
779 arm_bcc (code, ARMCOND_EQ, 0);
781 /* store greg result */
782 arm_strx (code, ARMREG_R0, ARMREG_R11, 0);
784 label_leave_tramp [1] = code;
785 arm_bcc (code, ARMCOND_AL, 0);
788 mono_arm_patch (label_is_float_ret, code, MONO_R_ARM64_BCC);
790 arm_ldrx (code, ARMREG_R11, ARMREG_IP0, MONO_STRUCT_OFFSET (InterpMethodArguments, retval));
792 arm_cmpx_imm (code, ARMREG_R11, 0);
793 label_leave_tramp [2] = code;
794 arm_bcc (code, ARMCOND_EQ, 0);
796 /* store freg result */
797 arm_strfpx (code, ARMREG_D0, ARMREG_R11, 0);
799 for (i = 0; i < 3; i++)
800 mono_arm_patch (label_leave_tramp [i], code, MONO_R_ARM64_BCC);
802 arm_movspx (code, ARMREG_SP, ARMREG_FP);
803 arm_ldpx (code, ARMREG_FP, ARMREG_LR, ARMREG_SP, 0);
804 arm_addx_imm (code, ARMREG_SP, ARMREG_SP, framesize);
805 arm_retx (code, ARMREG_LR);
807 g_assert (code - start < buf_len);
809 mono_arch_flush_icache (start, code - start);
810 mono_profiler_code_buffer_new (start, code - start, MONO_PROFILER_CODE_BUFFER_EXCEPTION_HANDLING, NULL);
813 *info = mono_tramp_info_create ("enter_icall_trampoline", start, code - start, ji, unwind_ops);
817 g_assert_not_reached ();
819 #endif /* ENABLE_INTERPRETER */
822 #else /* DISABLE_JIT */
825 mono_arch_create_generic_trampoline (MonoTrampolineType tramp_type, MonoTrampInfo **info, gboolean aot)
827 g_assert_not_reached ();
832 mono_arch_create_specific_trampoline (gpointer arg1, MonoTrampolineType tramp_type, MonoDomain *domain, guint32 *code_len)
834 g_assert_not_reached ();
839 mono_arch_create_handler_block_trampoline (MonoTrampInfo **info, gboolean aot)
841 g_assert_not_reached ();
846 mono_arch_get_unbox_trampoline (MonoMethod *m, gpointer addr)
848 g_assert_not_reached ();
853 mono_arch_get_static_rgctx_trampoline (gpointer arg, gpointer addr)
855 g_assert_not_reached ();
860 mono_arch_create_rgctx_lazy_fetch_trampoline (guint32 slot, MonoTrampInfo **info, gboolean aot)
862 g_assert_not_reached ();
867 mono_arch_get_nullified_class_init_trampoline (MonoTrampInfo **info)
869 g_assert_not_reached ();
874 mono_arch_create_sdb_trampoline (gboolean single_step, MonoTrampInfo **info, gboolean aot)
876 g_assert_not_reached ();
880 #endif /* !DISABLE_JIT */