2 * tramp-amd64.c: JIT trampoline code for amd64
5 * Dietmar Maurer (dietmar@ximian.com)
6 * Zoltan Varga (vargaz@gmail.com)
7 * Johan Lorensson (lateralusx.github@gmail.com)
9 * (C) 2001 Ximian, Inc.
10 * Copyright 2003-2011 Novell, Inc (http://www.novell.com)
11 * Copyright 2011 Xamarin, Inc (http://www.xamarin.com)
12 * Licensed under the MIT license. See LICENSE file in the project root for full license information.
18 #include <mono/metadata/abi-details.h>
19 #include <mono/metadata/appdomain.h>
20 #include <mono/metadata/marshal.h>
21 #include <mono/metadata/tabledefs.h>
22 #include <mono/metadata/profiler-private.h>
23 #include <mono/metadata/gc-internals.h>
24 #include <mono/arch/amd64/amd64-codegen.h>
26 #include <mono/utils/memcheck.h>
29 #include "mini-amd64.h"
30 #include "debugger-agent.h"
32 #define ALIGN_TO(val,align) ((((guint64)val) + ((align) - 1)) & ~((align) - 1))
34 #define IS_REX(inst) (((inst) >= 0x40) && ((inst) <= 0x4f))
38 * mono_arch_get_unbox_trampoline:
40 * @addr: pointer to native code for @m
42 * when value type methods are called through the vtable we need to unbox the
43 * this argument. This method returns a pointer to a trampoline which does
44 * unboxing before calling the method
47 mono_arch_get_unbox_trampoline (MonoMethod *m, gpointer addr)
51 int this_reg, size = 20;
53 MonoDomain *domain = mono_domain_get ();
55 this_reg = mono_arch_get_this_arg_reg (NULL);
57 start = code = (guint8 *)mono_domain_code_reserve (domain, size + MONO_TRAMPOLINE_UNWINDINFO_SIZE(0));
59 unwind_ops = mono_arch_get_cie_program ();
61 amd64_alu_reg_imm (code, X86_ADD, this_reg, sizeof (MonoObject));
62 /* FIXME: Optimize this */
63 amd64_mov_reg_imm (code, AMD64_RAX, addr);
64 amd64_jump_reg (code, AMD64_RAX);
65 g_assert ((code - start) < size);
66 g_assert_checked (mono_arch_unwindinfo_validate_size (unwind_ops, MONO_TRAMPOLINE_UNWINDINFO_SIZE(0)));
68 mono_arch_flush_icache (start, code - start);
69 mono_profiler_code_buffer_new (start, code - start, MONO_PROFILER_CODE_BUFFER_UNBOX_TRAMPOLINE, m);
71 mono_tramp_info_register (mono_tramp_info_create (NULL, start, code - start, NULL, unwind_ops), domain);
77 * mono_arch_get_static_rgctx_trampoline:
79 * Create a trampoline which sets RGCTX_REG to MRGCTX, then jumps to ADDR.
82 mono_arch_get_static_rgctx_trampoline (MonoMethod *m, MonoMethodRuntimeGenericContext *mrgctx, gpointer addr)
88 MonoDomain *domain = mono_domain_get ();
90 #ifdef MONO_ARCH_NOMAP32BIT
93 /* AOTed code could still have a non-32 bit address */
94 if ((((guint64)addr) >> 32) == 0)
100 start = code = (guint8 *)mono_domain_code_reserve (domain, buf_len + MONO_TRAMPOLINE_UNWINDINFO_SIZE(0));
102 unwind_ops = mono_arch_get_cie_program ();
104 amd64_mov_reg_imm (code, MONO_ARCH_RGCTX_REG, mrgctx);
105 amd64_jump_code (code, addr);
106 g_assert ((code - start) < buf_len);
107 g_assert_checked (mono_arch_unwindinfo_validate_size (unwind_ops, MONO_TRAMPOLINE_UNWINDINFO_SIZE(0)));
109 mono_arch_flush_icache (start, code - start);
110 mono_profiler_code_buffer_new (start, code - start, MONO_PROFILER_CODE_BUFFER_GENERICS_TRAMPOLINE, NULL);
112 mono_tramp_info_register (mono_tramp_info_create (NULL, start, code - start, NULL, unwind_ops), domain);
116 #endif /* !DISABLE_JIT */
119 // Workaround lack of Valgrind support for 64-bit Windows
120 #define VALGRIND_DISCARD_TRANSLATIONS(...)
124 * mono_arch_patch_callsite:
126 * Patch the callsite whose address is given by ORIG_CODE so it calls ADDR. ORIG_CODE
127 * points to the pc right after the call.
130 mono_arch_patch_callsite (guint8 *method_start, guint8 *orig_code, guint8 *addr)
134 gboolean can_write = mono_breakpoint_clean_code (method_start, orig_code, 14, buf, sizeof (buf));
138 /* mov 64-bit imm into r11 (followed by call reg?) or direct call*/
139 if (((code [-13] == 0x49) && (code [-12] == 0xbb)) || (code [-5] == 0xe8)) {
140 if (code [-5] != 0xe8) {
142 InterlockedExchangePointer ((gpointer*)(orig_code - 11), addr);
143 VALGRIND_DISCARD_TRANSLATIONS (orig_code - 11, sizeof (gpointer));
146 gboolean disp_32bit = ((((gint64)addr - (gint64)orig_code)) < (1 << 30)) && ((((gint64)addr - (gint64)orig_code)) > -(1 << 30));
148 if ((((guint64)(addr)) >> 32) != 0 && !disp_32bit) {
150 * This might happen with LLVM or when calling AOTed code. Create a thunk.
152 guint8 *thunk_start, *thunk_code;
154 thunk_start = thunk_code = (guint8 *)mono_domain_code_reserve (mono_domain_get (), 32);
155 amd64_jump_membase (thunk_code, AMD64_RIP, 0);
156 *(guint64*)thunk_code = (guint64)addr;
158 g_assert ((((guint64)(addr)) >> 32) == 0);
159 mono_arch_flush_icache (thunk_start, thunk_code - thunk_start);
160 mono_profiler_code_buffer_new (thunk_start, thunk_code - thunk_start, MONO_PROFILER_CODE_BUFFER_HELPER, NULL);
163 InterlockedExchange ((gint32*)(orig_code - 4), ((gint64)addr - (gint64)orig_code));
164 VALGRIND_DISCARD_TRANSLATIONS (orig_code - 5, 4);
168 else if ((code [-7] == 0x41) && (code [-6] == 0xff) && (code [-5] == 0x15)) {
169 /* call *<OFFSET>(%rip) */
170 gpointer *got_entry = (gpointer*)((guint8*)orig_code + (*(guint32*)(orig_code - 4)));
172 InterlockedExchangePointer (got_entry, addr);
173 VALGRIND_DISCARD_TRANSLATIONS (orig_code - 5, sizeof (gpointer));
180 mono_arch_create_llvm_native_thunk (MonoDomain *domain, guint8 *addr)
183 * The caller is LLVM code and the call displacement might exceed 32 bits. We can't determine the caller address, so
184 * we add a thunk every time.
185 * Since the caller is also allocated using the domain code manager, hopefully the displacement will fit into 32 bits.
186 * FIXME: Avoid this if possible if !MONO_ARCH_NOMAP32BIT and ADDR is 32 bits.
188 guint8 *thunk_start, *thunk_code;
190 thunk_start = thunk_code = (guint8 *)mono_domain_code_reserve (mono_domain_get (), 32);
191 amd64_jump_membase (thunk_code, AMD64_RIP, 0);
192 *(guint64*)thunk_code = (guint64)addr;
194 mono_arch_flush_icache (thunk_start, thunk_code - thunk_start);
195 mono_profiler_code_buffer_new (thunk_start, thunk_code - thunk_start, MONO_PROFILER_CODE_BUFFER_HELPER, NULL);
198 #endif /* !DISABLE_JIT */
201 mono_arch_patch_plt_entry (guint8 *code, gpointer *got, mgreg_t *regs, guint8 *addr)
204 gpointer *plt_jump_table_entry;
206 /* A PLT entry: jmp *<DISP>(%rip) */
207 g_assert (code [0] == 0xff);
208 g_assert (code [1] == 0x25);
210 disp = *(gint32*)(code + 2);
212 plt_jump_table_entry = (gpointer*)(code + 6 + disp);
214 InterlockedExchangePointer (plt_jump_table_entry, addr);
219 stack_unaligned (MonoTrampolineType tramp_type)
221 printf ("%d\n", tramp_type);
222 g_assert_not_reached ();
226 mono_arch_create_generic_trampoline (MonoTrampolineType tramp_type, MonoTrampInfo **info, gboolean aot)
229 guint8 *buf, *code, *tramp, *br [2], *r11_save_code, *after_r11_save_code, *br_ex_check;
230 int i, lmf_offset, offset, res_offset, arg_offset, rax_offset, ex_offset, tramp_offset, ctx_offset, saved_regs_offset;
231 int r11_save_offset, saved_fpregs_offset, rbp_offset, framesize, orig_rsp_to_rbp_offset, cfa_offset;
233 GSList *unwind_ops = NULL;
234 MonoJumpInfo *ji = NULL;
235 const guint kMaxCodeSize = 630;
237 if (tramp_type == MONO_TRAMPOLINE_JUMP || tramp_type == MONO_TRAMPOLINE_HANDLER_BLOCK_GUARD)
242 code = buf = (guint8 *)mono_global_codeman_reserve (kMaxCodeSize + MONO_MAX_TRAMPOLINE_UNWINDINFO_SIZE);
244 /* Compute stack frame size and offsets */
246 rbp_offset = -offset;
248 offset += sizeof(mgreg_t);
249 rax_offset = -offset;
251 offset += sizeof(mgreg_t);
254 offset += sizeof(mgreg_t);
255 r11_save_offset = -offset;
257 offset += sizeof(mgreg_t);
258 tramp_offset = -offset;
260 offset += sizeof(gpointer);
261 arg_offset = -offset;
263 offset += sizeof(mgreg_t);
264 res_offset = -offset;
266 offset += sizeof (MonoContext);
267 ctx_offset = -offset;
268 saved_regs_offset = ctx_offset + MONO_STRUCT_OFFSET (MonoContext, gregs);
269 saved_fpregs_offset = ctx_offset + MONO_STRUCT_OFFSET (MonoContext, fregs);
271 offset += sizeof (MonoLMFTramp);
272 lmf_offset = -offset;
275 /* Reserve space where the callee can save the argument registers */
276 offset += 4 * sizeof (mgreg_t);
279 framesize = ALIGN_TO (offset, MONO_ARCH_FRAME_ALIGNMENT);
281 // CFA = sp + 16 (the trampoline address is on the stack)
283 mono_add_unwind_op_def_cfa (unwind_ops, code, buf, AMD64_RSP, 16);
284 // IP saved at CFA - 8
285 mono_add_unwind_op_offset (unwind_ops, code, buf, AMD64_RIP, -8);
287 orig_rsp_to_rbp_offset = 0;
288 r11_save_code = code;
289 /* Reserve space for the mov_membase_reg to save R11 */
291 after_r11_save_code = code;
293 /* Pop the return address off the stack */
294 amd64_pop_reg (code, AMD64_R11);
295 orig_rsp_to_rbp_offset += sizeof(mgreg_t);
297 cfa_offset -= sizeof(mgreg_t);
298 mono_add_unwind_op_def_cfa_offset (unwind_ops, code, buf, cfa_offset);
301 * Allocate a new stack frame
303 amd64_push_reg (code, AMD64_RBP);
304 cfa_offset += sizeof(mgreg_t);
305 mono_add_unwind_op_def_cfa_offset (unwind_ops, code, buf, cfa_offset);
306 mono_add_unwind_op_offset (unwind_ops, code, buf, AMD64_RBP, - cfa_offset);
308 orig_rsp_to_rbp_offset -= sizeof(mgreg_t);
309 amd64_mov_reg_reg (code, AMD64_RBP, AMD64_RSP, sizeof(mgreg_t));
310 mono_add_unwind_op_def_cfa_reg (unwind_ops, code, buf, AMD64_RBP);
311 mono_add_unwind_op_fp_alloc (unwind_ops, code, buf, AMD64_RBP, 0);
312 amd64_alu_reg_imm (code, X86_SUB, AMD64_RSP, framesize);
314 /* Compute the trampoline address from the return address */
316 /* 7 = length of call *<offset>(rip) */
317 amd64_alu_reg_imm (code, X86_SUB, AMD64_R11, 7);
319 /* 5 = length of amd64_call_membase () */
320 amd64_alu_reg_imm (code, X86_SUB, AMD64_R11, 5);
322 amd64_mov_membase_reg (code, AMD64_RBP, tramp_offset, AMD64_R11, sizeof(gpointer));
324 /* Save all registers */
325 for (i = 0; i < AMD64_NREG; ++i) {
326 if (i == AMD64_RBP) {
327 /* RAX is already saved */
328 amd64_mov_reg_membase (code, AMD64_RAX, AMD64_RBP, rbp_offset, sizeof(mgreg_t));
329 amd64_mov_membase_reg (code, AMD64_RBP, saved_regs_offset + (i * sizeof(mgreg_t)), AMD64_RAX, sizeof(mgreg_t));
330 } else if (i == AMD64_RIP) {
332 amd64_mov_reg_membase (code, AMD64_R11, AMD64_RBP, 8, sizeof(gpointer));
334 amd64_mov_reg_imm (code, AMD64_R11, 0);
335 amd64_mov_membase_reg (code, AMD64_RBP, saved_regs_offset + (i * sizeof(mgreg_t)), AMD64_R11, sizeof(mgreg_t));
336 } else if (i == AMD64_RSP) {
337 amd64_mov_reg_reg (code, AMD64_R11, AMD64_RSP, sizeof(mgreg_t));
338 amd64_alu_reg_imm (code, X86_ADD, AMD64_R11, framesize + 16);
339 amd64_mov_membase_reg (code, AMD64_RBP, saved_regs_offset + (i * sizeof(mgreg_t)), AMD64_R11, sizeof(mgreg_t));
340 } else if (i != AMD64_R11) {
341 amd64_mov_membase_reg (code, AMD64_RBP, saved_regs_offset + (i * sizeof(mgreg_t)), i, sizeof(mgreg_t));
343 /* We have to save R11 right at the start of
344 the trampoline code because it's used as a
346 /* This happens before the frame is set up, so it goes into the redzone */
347 amd64_mov_membase_reg (r11_save_code, AMD64_RSP, r11_save_offset + orig_rsp_to_rbp_offset, i, sizeof(mgreg_t));
348 g_assert (r11_save_code == after_r11_save_code);
350 /* Copy from the save slot into the register array slot */
351 amd64_mov_reg_membase (code, i, AMD64_RSP, r11_save_offset + orig_rsp_to_rbp_offset, sizeof(mgreg_t));
352 amd64_mov_membase_reg (code, AMD64_RBP, saved_regs_offset + (i * sizeof(mgreg_t)), i, sizeof(mgreg_t));
354 /* cfa = rbp + cfa_offset */
355 mono_add_unwind_op_offset (unwind_ops, code, buf, i, - cfa_offset + saved_regs_offset + (i * sizeof (mgreg_t)));
357 for (i = 0; i < 8; ++i)
358 amd64_movsd_membase_reg (code, AMD64_RBP, saved_fpregs_offset + (i * sizeof(mgreg_t)), i);
360 /* Check that the stack is aligned */
361 amd64_mov_reg_reg (code, AMD64_R11, AMD64_RSP, sizeof (mgreg_t));
362 amd64_alu_reg_imm (code, X86_AND, AMD64_R11, 15);
363 amd64_alu_reg_imm (code, X86_CMP, AMD64_R11, 0);
365 amd64_branch_disp (code, X86_CC_Z, 0, FALSE);
367 amd64_mov_reg_imm (code, AMD64_R11, 0);
368 amd64_mov_reg_membase (code, AMD64_R11, AMD64_R11, 0, 8);
370 amd64_mov_reg_imm (code, MONO_AMD64_ARG_REG1, tramp_type);
371 amd64_mov_reg_imm (code, AMD64_R11, stack_unaligned);
372 amd64_call_reg (code, AMD64_R11);
374 mono_amd64_patch (br [0], code);
375 //amd64_breakpoint (code);
377 if (tramp_type != MONO_TRAMPOLINE_HANDLER_BLOCK_GUARD) {
378 /* Obtain the trampoline argument which is encoded in the instruction stream */
380 /* Load the GOT offset */
381 amd64_mov_reg_membase (code, AMD64_R11, AMD64_RBP, tramp_offset, sizeof(gpointer));
383 * r11 points to a call *<offset>(%rip) instruction, load the
384 * pc-relative offset from the instruction itself.
386 amd64_mov_reg_membase (code, AMD64_RAX, AMD64_R11, 3, 4);
387 /* 7 is the length of the call, 8 is the offset to the next got slot */
388 amd64_alu_reg_imm_size (code, X86_ADD, AMD64_RAX, 7 + sizeof (gpointer), sizeof(gpointer));
389 /* Compute the address of the GOT slot */
390 amd64_alu_reg_reg_size (code, X86_ADD, AMD64_R11, AMD64_RAX, sizeof(gpointer));
392 amd64_mov_reg_membase (code, AMD64_R11, AMD64_R11, 0, sizeof(gpointer));
394 amd64_mov_reg_membase (code, AMD64_R11, AMD64_RBP, tramp_offset, sizeof(gpointer));
395 amd64_mov_reg_membase (code, AMD64_RAX, AMD64_R11, 5, 1);
396 amd64_widen_reg (code, AMD64_RAX, AMD64_RAX, TRUE, FALSE);
397 amd64_alu_reg_imm_size (code, X86_CMP, AMD64_RAX, 4, 1);
399 x86_branch8 (code, X86_CC_NE, 6, FALSE);
400 /* 32 bit immediate */
401 amd64_mov_reg_membase (code, AMD64_R11, AMD64_R11, 6, 4);
403 x86_jump8 (code, 10);
404 /* 64 bit immediate */
405 mono_amd64_patch (br [0], code);
406 amd64_mov_reg_membase (code, AMD64_R11, AMD64_R11, 6, 8);
407 mono_amd64_patch (br [1], code);
409 amd64_mov_membase_reg (code, AMD64_RBP, arg_offset, AMD64_R11, sizeof(gpointer));
411 amd64_mov_reg_membase (code, AMD64_R11, AMD64_RBP, saved_regs_offset + (MONO_AMD64_ARG_REG1 * sizeof(mgreg_t)), sizeof(mgreg_t));
412 amd64_mov_membase_reg (code, AMD64_RBP, arg_offset, AMD64_R11, sizeof(gpointer));
419 amd64_mov_reg_membase (code, AMD64_R11, AMD64_RBP, 8, sizeof(gpointer));
421 amd64_mov_reg_imm (code, AMD64_R11, 0);
422 amd64_mov_membase_reg (code, AMD64_RBP, lmf_offset + MONO_STRUCT_OFFSET (MonoLMF, rip), AMD64_R11, sizeof(mgreg_t));
424 amd64_mov_reg_reg (code, AMD64_R11, AMD64_RSP, sizeof(mgreg_t));
425 amd64_alu_reg_imm (code, X86_ADD, AMD64_R11, framesize + 16);
426 amd64_mov_membase_reg (code, AMD64_RBP, lmf_offset + MONO_STRUCT_OFFSET (MonoLMF, rsp), AMD64_R11, sizeof(mgreg_t));
427 /* Save pointer to context */
428 amd64_lea_membase (code, AMD64_R11, AMD64_RBP, ctx_offset);
429 amd64_mov_membase_reg (code, AMD64_RBP, lmf_offset + MONO_STRUCT_OFFSET (MonoLMFTramp, ctx), AMD64_R11, sizeof(mgreg_t));
432 code = mono_arch_emit_load_aotconst (buf, code, &ji, MONO_PATCH_INFO_JIT_ICALL_ADDR, "mono_get_lmf_addr");
434 amd64_mov_reg_imm (code, AMD64_R11, mono_get_lmf_addr);
436 amd64_call_reg (code, AMD64_R11);
439 amd64_mov_membase_reg (code, AMD64_RBP, lmf_offset + MONO_STRUCT_OFFSET (MonoLMFTramp, lmf_addr), AMD64_RAX, sizeof(gpointer));
440 /* Save previous_lmf */
441 /* Set the lowest bit to signal that this LMF has the ip field set */
442 /* Set the third lowest bit to signal that this is a MonoLMFTramp structure */
443 amd64_mov_reg_membase (code, AMD64_R11, AMD64_RAX, 0, sizeof(gpointer));
444 amd64_alu_reg_imm_size (code, X86_ADD, AMD64_R11, 0x5, sizeof(gpointer));
445 amd64_mov_membase_reg (code, AMD64_RBP, lmf_offset + MONO_STRUCT_OFFSET (MonoLMF, previous_lmf), AMD64_R11, sizeof(gpointer));
447 amd64_lea_membase (code, AMD64_R11, AMD64_RBP, lmf_offset);
448 amd64_mov_membase_reg (code, AMD64_RAX, 0, AMD64_R11, sizeof(gpointer));
452 /* Arg1 is the pointer to the saved registers */
453 amd64_lea_membase (code, AMD64_ARG_REG1, AMD64_RBP, saved_regs_offset);
455 /* Arg2 is the address of the calling code */
457 amd64_mov_reg_membase (code, AMD64_ARG_REG2, AMD64_RBP, 8, sizeof(gpointer));
459 amd64_mov_reg_imm (code, AMD64_ARG_REG2, 0);
461 /* Arg3 is the method/vtable ptr */
462 amd64_mov_reg_membase (code, AMD64_ARG_REG3, AMD64_RBP, arg_offset, sizeof(gpointer));
464 /* Arg4 is the trampoline address */
465 amd64_mov_reg_membase (code, AMD64_ARG_REG4, AMD64_RBP, tramp_offset, sizeof(gpointer));
468 char *icall_name = g_strdup_printf ("trampoline_func_%d", tramp_type);
469 code = mono_arch_emit_load_aotconst (buf, code, &ji, MONO_PATCH_INFO_JIT_ICALL_ADDR, icall_name);
471 tramp = (guint8*)mono_get_trampoline_func (tramp_type);
472 amd64_mov_reg_imm (code, AMD64_R11, tramp);
474 amd64_call_reg (code, AMD64_R11);
475 amd64_mov_membase_reg (code, AMD64_RBP, res_offset, AMD64_RAX, sizeof(mgreg_t));
478 amd64_mov_reg_membase (code, AMD64_RCX, AMD64_RBP, lmf_offset + MONO_STRUCT_OFFSET (MonoLMF, previous_lmf), sizeof(gpointer));
479 amd64_alu_reg_imm_size (code, X86_SUB, AMD64_RCX, 0x5, sizeof(gpointer));
480 amd64_mov_reg_membase (code, AMD64_R11, AMD64_RBP, lmf_offset + MONO_STRUCT_OFFSET (MonoLMFTramp, lmf_addr), sizeof(gpointer));
481 amd64_mov_membase_reg (code, AMD64_R11, 0, AMD64_RCX, sizeof(gpointer));
484 * Save rax to the stack, after the leave instruction, this will become part of
487 amd64_mov_reg_membase (code, AMD64_RAX, AMD64_RBP, res_offset, sizeof(mgreg_t));
488 amd64_mov_membase_reg (code, AMD64_RBP, rax_offset, AMD64_RAX, sizeof(mgreg_t));
490 /* Check for thread interruption */
491 /* This is not perf critical code so no need to check the interrupt flag */
493 * Have to call the _force_ variant, since there could be a protected wrapper on the top of the stack.
496 code = mono_arch_emit_load_aotconst (buf, code, &ji, MONO_PATCH_INFO_JIT_ICALL_ADDR, "mono_thread_force_interruption_checkpoint_noraise");
498 amd64_mov_reg_imm (code, AMD64_R11, (guint8*)mono_thread_force_interruption_checkpoint_noraise);
500 amd64_call_reg (code, AMD64_R11);
502 amd64_test_reg_reg (code, AMD64_RAX, AMD64_RAX);
504 amd64_branch8 (code, X86_CC_Z, -1, 1);
508 * We have an exception we want to throw in the caller's frame, so pop
509 * the trampoline frame and throw from the caller.
512 amd64_lea_membase (code, AMD64_RSP, AMD64_RBP, 0);
513 amd64_pop_reg (code, AMD64_RBP);
514 mono_add_unwind_op_same_value (unwind_ops, code, buf, AMD64_RBP);
518 /* We are in the parent frame, the exception is in rax */
520 * EH is initialized after trampolines, so get the address of the variable
521 * which contains throw_exception, and load it from there.
524 /* Not really a jit icall */
525 code = mono_arch_emit_load_aotconst (buf, code, &ji, MONO_PATCH_INFO_JIT_ICALL_ADDR, "throw_exception_addr");
527 amd64_mov_reg_imm (code, AMD64_R11, (guint8*)mono_get_throw_exception_addr ());
529 amd64_mov_reg_membase (code, AMD64_R11, AMD64_R11, 0, sizeof(gpointer));
530 amd64_mov_reg_reg (code, AMD64_ARG_REG1, AMD64_RAX, sizeof(mgreg_t));
532 * We still have the original return value on the top of the stack, so the
533 * throw trampoline will use that as the throw site.
535 amd64_jump_reg (code, AMD64_R11);
538 mono_amd64_patch (br_ex_check, code);
540 /* Restore argument registers, r10 (imt method/rgxtx)
541 and rax (needed for direct calls to C vararg functions). */
542 for (i = 0; i < AMD64_NREG; ++i)
543 if (AMD64_IS_ARGUMENT_REG (i) || i == AMD64_R10 || i == AMD64_RAX)
544 amd64_mov_reg_membase (code, i, AMD64_RBP, saved_regs_offset + (i * sizeof(mgreg_t)), sizeof(mgreg_t));
545 for (i = 0; i < 8; ++i)
546 amd64_movsd_reg_membase (code, i, AMD64_RBP, saved_fpregs_offset + (i * sizeof(mgreg_t)));
550 amd64_lea_membase (code, AMD64_RSP, AMD64_RBP, 0);
551 amd64_pop_reg (code, AMD64_RBP);
552 mono_add_unwind_op_same_value (unwind_ops, code, buf, AMD64_RBP);
556 cfa_offset -= sizeof (mgreg_t);
557 mono_add_unwind_op_def_cfa (unwind_ops, code, buf, AMD64_RSP, cfa_offset);
559 if (MONO_TRAMPOLINE_TYPE_MUST_RETURN (tramp_type)) {
561 amd64_mov_reg_membase (code, AMD64_RAX, AMD64_RSP, rax_offset - sizeof(mgreg_t), sizeof(mgreg_t));
564 /* call the compiled method using the saved rax */
565 amd64_jump_membase (code, AMD64_RSP, rax_offset - sizeof(mgreg_t));
568 g_assert ((code - buf) <= kMaxCodeSize);
569 g_assert_checked (mono_arch_unwindinfo_validate_size (unwind_ops, MONO_MAX_TRAMPOLINE_UNWINDINFO_SIZE));
571 mono_arch_flush_icache (buf, code - buf);
572 mono_profiler_code_buffer_new (buf, code - buf, MONO_PROFILER_CODE_BUFFER_HELPER, NULL);
574 tramp_name = mono_get_generic_trampoline_name (tramp_type);
575 *info = mono_tramp_info_create (tramp_name, buf, code - buf, ji, unwind_ops);
582 mono_arch_create_specific_trampoline (gpointer arg1, MonoTrampolineType tramp_type, MonoDomain *domain, guint32 *code_len)
584 guint8 *code, *buf, *tramp;
586 gboolean far_addr = FALSE;
588 tramp = mono_get_trampoline_code (tramp_type);
590 if ((((guint64)arg1) >> 32) == 0)
595 code = buf = (guint8 *)mono_domain_code_reserve_align (domain, size, 1);
597 if (((gint64)tramp - (gint64)code) >> 31 != 0 && ((gint64)tramp - (gint64)code) >> 31 != -1) {
598 #ifndef MONO_ARCH_NOMAP32BIT
599 g_assert_not_reached ();
603 code = buf = (guint8 *)mono_domain_code_reserve_align (domain, size, 1);
607 amd64_mov_reg_imm (code, AMD64_R11, tramp);
608 amd64_call_reg (code, AMD64_R11);
610 amd64_call_code (code, tramp);
612 /* The trampoline code will obtain the argument from the instruction stream */
613 if ((((guint64)arg1) >> 32) == 0) {
615 *(guint32*)(code + 1) = (gint64)arg1;
619 *(guint64*)(code + 1) = (gint64)arg1;
623 g_assert ((code - buf) <= size);
628 mono_arch_flush_icache (buf, size);
629 mono_profiler_code_buffer_new (buf, code - buf, MONO_PROFILER_CODE_BUFFER_SPECIFIC_TRAMPOLINE, mono_get_generic_trampoline_simple_name (tramp_type));
635 mono_arch_create_rgctx_lazy_fetch_trampoline (guint32 slot, MonoTrampInfo **info, gboolean aot)
639 guint8 **rgctx_null_jumps;
644 MonoJumpInfo *ji = NULL;
647 mrgctx = MONO_RGCTX_SLOT_IS_MRGCTX (slot);
648 index = MONO_RGCTX_SLOT_INDEX (slot);
650 index += MONO_SIZEOF_METHOD_RUNTIME_GENERIC_CONTEXT / sizeof (gpointer);
651 for (depth = 0; ; ++depth) {
652 int size = mono_class_rgctx_get_array_size (depth, mrgctx);
654 if (index < size - 1)
659 tramp_size = 64 + 8 * depth;
661 code = buf = (guint8 *)mono_global_codeman_reserve (tramp_size + MONO_TRAMPOLINE_UNWINDINFO_SIZE(0));
663 unwind_ops = mono_arch_get_cie_program ();
665 rgctx_null_jumps = (guint8 **)g_malloc (sizeof (guint8*) * (depth + 2));
669 amd64_mov_reg_reg (code, AMD64_RAX, AMD64_ARG_REG1, 8);
671 /* load rgctx ptr from vtable */
672 amd64_mov_reg_membase (code, AMD64_RAX, AMD64_ARG_REG1, MONO_STRUCT_OFFSET (MonoVTable, runtime_generic_context), sizeof(gpointer));
673 /* is the rgctx ptr null? */
674 amd64_test_reg_reg (code, AMD64_RAX, AMD64_RAX);
675 /* if yes, jump to actual trampoline */
676 rgctx_null_jumps [0] = code;
677 amd64_branch8 (code, X86_CC_Z, -1, 1);
680 for (i = 0; i < depth; ++i) {
681 /* load ptr to next array */
682 if (mrgctx && i == 0)
683 amd64_mov_reg_membase (code, AMD64_RAX, AMD64_RAX, MONO_SIZEOF_METHOD_RUNTIME_GENERIC_CONTEXT, sizeof(gpointer));
685 amd64_mov_reg_membase (code, AMD64_RAX, AMD64_RAX, 0, sizeof(gpointer));
686 /* is the ptr null? */
687 amd64_test_reg_reg (code, AMD64_RAX, AMD64_RAX);
688 /* if yes, jump to actual trampoline */
689 rgctx_null_jumps [i + 1] = code;
690 amd64_branch8 (code, X86_CC_Z, -1, 1);
694 amd64_mov_reg_membase (code, AMD64_RAX, AMD64_RAX, sizeof (gpointer) * (index + 1), sizeof(gpointer));
695 /* is the slot null? */
696 amd64_test_reg_reg (code, AMD64_RAX, AMD64_RAX);
697 /* if yes, jump to actual trampoline */
698 rgctx_null_jumps [depth + 1] = code;
699 amd64_branch8 (code, X86_CC_Z, -1, 1);
700 /* otherwise return */
703 for (i = mrgctx ? 1 : 0; i <= depth + 1; ++i)
704 mono_amd64_patch (rgctx_null_jumps [i], code);
706 g_free (rgctx_null_jumps);
708 /* move the rgctx pointer to the VTABLE register */
709 amd64_mov_reg_reg (code, MONO_ARCH_VTABLE_REG, AMD64_ARG_REG1, sizeof(gpointer));
712 code = mono_arch_emit_load_aotconst (buf, code, &ji, MONO_PATCH_INFO_JIT_ICALL_ADDR, g_strdup_printf ("specific_trampoline_lazy_fetch_%u", slot));
713 amd64_jump_reg (code, AMD64_R11);
715 tramp = (guint8 *)mono_arch_create_specific_trampoline (GUINT_TO_POINTER (slot), MONO_TRAMPOLINE_RGCTX_LAZY_FETCH, mono_get_root_domain (), NULL);
717 /* jump to the actual trampoline */
718 amd64_jump_code (code, tramp);
721 mono_arch_flush_icache (buf, code - buf);
722 mono_profiler_code_buffer_new (buf, code - buf, MONO_PROFILER_CODE_BUFFER_GENERICS_TRAMPOLINE, NULL);
724 g_assert (code - buf <= tramp_size);
725 g_assert_checked (mono_arch_unwindinfo_validate_size (unwind_ops, MONO_TRAMPOLINE_UNWINDINFO_SIZE(0)));
727 char *name = mono_get_rgctx_fetch_trampoline_name (slot);
728 *info = mono_tramp_info_create (name, buf, code - buf, ji, unwind_ops);
735 mono_arch_create_general_rgctx_lazy_fetch_trampoline (MonoTrampInfo **info, gboolean aot)
739 MonoJumpInfo *ji = NULL;
745 code = buf = (guint8 *)mono_global_codeman_reserve (tramp_size + MONO_TRAMPOLINE_UNWINDINFO_SIZE(0));
747 unwind_ops = mono_arch_get_cie_program ();
749 // FIXME: Currently, we always go to the slow path.
750 /* This receives a <slot, trampoline> in the rgctx arg reg. */
751 /* Load trampoline addr */
752 amd64_mov_reg_membase (code, AMD64_R11, MONO_ARCH_RGCTX_REG, 8, 8);
753 /* move the rgctx pointer to the VTABLE register */
754 amd64_mov_reg_reg (code, MONO_ARCH_VTABLE_REG, AMD64_ARG_REG1, sizeof(gpointer));
755 /* Jump to the trampoline */
756 amd64_jump_reg (code, AMD64_R11);
758 mono_arch_flush_icache (buf, code - buf);
759 mono_profiler_code_buffer_new (buf, code - buf, MONO_PROFILER_CODE_BUFFER_GENERICS_TRAMPOLINE, NULL);
761 g_assert (code - buf <= tramp_size);
762 g_assert_checked (mono_arch_unwindinfo_validate_size (unwind_ops, MONO_TRAMPOLINE_UNWINDINFO_SIZE(0)));
765 *info = mono_tramp_info_create ("rgctx_fetch_trampoline_general", buf, code - buf, ji, unwind_ops);
771 mono_arch_invalidate_method (MonoJitInfo *ji, void *func, gpointer func_arg)
773 /* FIXME: This is not thread safe */
774 guint8 *code = (guint8 *)ji->code_start;
776 amd64_mov_reg_imm (code, AMD64_ARG_REG1, func_arg);
777 amd64_mov_reg_imm (code, AMD64_R11, func);
779 x86_push_imm (code, (guint64)func_arg);
780 amd64_call_reg (code, AMD64_R11);
782 #endif /* !DISABLE_JIT */
785 mono_amd64_handler_block_trampoline_helper (void)
787 MonoJitTlsData *jit_tls = (MonoJitTlsData *)mono_tls_get_jit_tls ();
788 return jit_tls->handler_block_return_address;
793 mono_arch_create_handler_block_trampoline (MonoTrampInfo **info, gboolean aot)
797 MonoJumpInfo *ji = NULL;
800 code = buf = (guint8 *)mono_global_codeman_reserve (tramp_size + MONO_TRAMPOLINE_UNWINDINFO_SIZE(0));
802 unwind_ops = mono_arch_get_cie_program ();
805 * This trampoline restore the call chain of the handler block then jumps into the code that deals with it.
806 * We get here from the ret emitted by CEE_ENDFINALLY.
807 * The stack is misaligned.
809 /* Align the stack before the call to mono_amd64_handler_block_trampoline_helper() */
811 /* Also make room for the "register parameter stack area" as specified by the Windows x64 ABI (4 64-bit registers) */
812 amd64_alu_reg_imm (code, X86_SUB, AMD64_RSP, 8 + 4 * 8);
814 amd64_alu_reg_imm (code, X86_SUB, AMD64_RSP, 8);
817 code = mono_arch_emit_load_aotconst (buf, code, &ji, MONO_PATCH_INFO_JIT_ICALL_ADDR, "mono_amd64_handler_block_trampoline_helper");
818 amd64_call_reg (code, AMD64_R11);
820 amd64_mov_reg_imm (code, AMD64_RAX, mono_amd64_handler_block_trampoline_helper);
821 amd64_call_reg (code, AMD64_RAX);
823 /* Undo stack alignment */
825 amd64_alu_reg_imm (code, X86_ADD, AMD64_RSP, 8 + 4 * 8);
827 amd64_alu_reg_imm (code, X86_ADD, AMD64_RSP, 8);
829 /* Save the result to the stack */
830 amd64_push_reg (code, AMD64_RAX);
832 /* Make room for the "register parameter stack area" as specified by the Windows x64 ABI (4 64-bit registers) */
833 amd64_alu_reg_imm (code, X86_SUB, AMD64_RSP, 4 * 8);
836 char *name = g_strdup_printf ("trampoline_func_%d", MONO_TRAMPOLINE_HANDLER_BLOCK_GUARD);
837 code = mono_arch_emit_load_aotconst (buf, code, &ji, MONO_PATCH_INFO_JIT_ICALL_ADDR, name);
838 amd64_mov_reg_reg (code, AMD64_RAX, AMD64_R11, 8);
840 amd64_mov_reg_imm (code, AMD64_RAX, mono_get_trampoline_func (MONO_TRAMPOLINE_HANDLER_BLOCK_GUARD));
842 /* The stack is aligned */
843 amd64_call_reg (code, AMD64_RAX);
845 amd64_alu_reg_imm (code, X86_ADD, AMD64_RSP, 4 * 8);
847 /* Load return address */
848 amd64_pop_reg (code, AMD64_RAX);
849 /* The stack is misaligned, thats what the code we branch to expects */
850 amd64_jump_reg (code, AMD64_RAX);
852 mono_arch_flush_icache (buf, code - buf);
853 mono_profiler_code_buffer_new (buf, code - buf, MONO_PROFILER_CODE_BUFFER_HELPER, NULL);
854 g_assert (code - buf <= tramp_size);
855 g_assert_checked (mono_arch_unwindinfo_validate_size (unwind_ops, MONO_TRAMPOLINE_UNWINDINFO_SIZE(0)));
857 *info = mono_tramp_info_create ("handler_block_trampoline", buf, code - buf, ji, unwind_ops);
861 #endif /* !DISABLE_JIT */
864 * mono_arch_get_call_target:
866 * Return the address called by the code before CODE if exists.
869 mono_arch_get_call_target (guint8 *code)
871 if (code [-5] == 0xe8) {
872 gint32 disp = *(gint32*)(code - 4);
873 guint8 *target = code + disp;
882 * mono_arch_get_plt_info_offset:
884 * Return the PLT info offset belonging to the plt entry PLT_ENTRY.
887 mono_arch_get_plt_info_offset (guint8 *plt_entry, mgreg_t *regs, guint8 *code)
889 return *(guint32*)(plt_entry + 6);
894 * mono_arch_create_sdb_trampoline:
896 * Return a trampoline which captures the current context, passes it to
897 * debugger_agent_single_step_from_context ()/debugger_agent_breakpoint_from_context (),
898 * then restores the (potentially changed) context.
901 mono_arch_create_sdb_trampoline (gboolean single_step, MonoTrampInfo **info, gboolean aot)
903 int tramp_size = 512;
904 int i, framesize, ctx_offset, cfa_offset, gregs_offset;
906 GSList *unwind_ops = NULL;
907 MonoJumpInfo *ji = NULL;
909 code = buf = (guint8 *)mono_global_codeman_reserve (tramp_size + MONO_MAX_TRAMPOLINE_UNWINDINFO_SIZE);
913 /* Reserve space where the callee can save the argument registers */
914 framesize += 4 * sizeof (mgreg_t);
917 ctx_offset = framesize;
918 framesize += sizeof (MonoContext);
920 framesize = ALIGN_TO (framesize, MONO_ARCH_FRAME_ALIGNMENT);
924 mono_add_unwind_op_def_cfa (unwind_ops, code, buf, AMD64_RSP, 8);
925 // IP saved at CFA - 8
926 mono_add_unwind_op_offset (unwind_ops, code, buf, AMD64_RIP, -cfa_offset);
928 amd64_push_reg (code, AMD64_RBP);
929 cfa_offset += sizeof(mgreg_t);
930 mono_add_unwind_op_def_cfa_offset (unwind_ops, code, buf, cfa_offset);
931 mono_add_unwind_op_offset (unwind_ops, code, buf, AMD64_RBP, - cfa_offset);
933 amd64_mov_reg_reg (code, AMD64_RBP, AMD64_RSP, sizeof(mgreg_t));
934 mono_add_unwind_op_def_cfa_reg (unwind_ops, code, buf, AMD64_RBP);
935 mono_add_unwind_op_fp_alloc (unwind_ops, code, buf, AMD64_RBP, 0);
936 amd64_alu_reg_imm (code, X86_SUB, AMD64_RSP, framesize);
938 gregs_offset = ctx_offset + MONO_STRUCT_OFFSET (MonoContext, gregs);
940 /* Initialize a MonoContext structure on the stack */
941 for (i = 0; i < AMD64_NREG; ++i) {
942 if (i != AMD64_RIP && i != AMD64_RSP && i != AMD64_RBP)
943 amd64_mov_membase_reg (code, AMD64_RSP, gregs_offset + (i * sizeof (mgreg_t)), i, sizeof (mgreg_t));
945 amd64_mov_reg_membase (code, AMD64_R11, AMD64_RBP, 0, sizeof (mgreg_t));
946 amd64_mov_membase_reg (code, AMD64_RSP, gregs_offset + (AMD64_RBP * sizeof (mgreg_t)), AMD64_R11, sizeof (mgreg_t));
947 amd64_lea_membase (code, AMD64_R11, AMD64_RBP, 2 * sizeof (mgreg_t));
948 amd64_mov_membase_reg (code, AMD64_RSP, gregs_offset + (AMD64_RSP * sizeof (mgreg_t)), AMD64_R11, sizeof (mgreg_t));
949 amd64_mov_reg_membase (code, AMD64_R11, AMD64_RBP, sizeof (mgreg_t), sizeof (mgreg_t));
950 amd64_mov_membase_reg (code, AMD64_RSP, gregs_offset + (AMD64_RIP * sizeof (mgreg_t)), AMD64_R11, sizeof (mgreg_t));
952 /* Call the single step/breakpoint function in sdb */
953 amd64_lea_membase (code, AMD64_ARG_REG1, AMD64_RSP, ctx_offset);
957 code = mono_arch_emit_load_aotconst (buf, code, &ji, MONO_PATCH_INFO_JIT_ICALL_ADDR, "debugger_agent_single_step_from_context");
959 code = mono_arch_emit_load_aotconst (buf, code, &ji, MONO_PATCH_INFO_JIT_ICALL_ADDR, "debugger_agent_breakpoint_from_context");
962 amd64_mov_reg_imm (code, AMD64_R11, debugger_agent_single_step_from_context);
964 amd64_mov_reg_imm (code, AMD64_R11, debugger_agent_breakpoint_from_context);
966 amd64_call_reg (code, AMD64_R11);
968 /* Restore registers from ctx */
969 for (i = 0; i < AMD64_NREG; ++i) {
970 if (i != AMD64_RIP && i != AMD64_RSP && i != AMD64_RBP)
971 amd64_mov_reg_membase (code, i, AMD64_RSP, gregs_offset + (i * sizeof (mgreg_t)), sizeof (mgreg_t));
973 amd64_mov_reg_membase (code, AMD64_R11, AMD64_RSP, gregs_offset + (AMD64_RBP * sizeof (mgreg_t)), sizeof (mgreg_t));
974 amd64_mov_membase_reg (code, AMD64_RBP, 0, AMD64_R11, sizeof (mgreg_t));
975 amd64_mov_reg_membase (code, AMD64_R11, AMD64_RSP, gregs_offset + (AMD64_RIP * sizeof (mgreg_t)), sizeof (mgreg_t));
976 amd64_mov_membase_reg (code, AMD64_RBP, sizeof (mgreg_t), AMD64_R11, sizeof (mgreg_t));
979 amd64_lea_membase (code, AMD64_RSP, AMD64_RBP, 0);
980 amd64_pop_reg (code, AMD64_RBP);
981 mono_add_unwind_op_same_value (unwind_ops, code, buf, AMD64_RBP);
985 cfa_offset -= sizeof (mgreg_t);
986 mono_add_unwind_op_def_cfa (unwind_ops, code, buf, AMD64_RSP, cfa_offset);
989 mono_arch_flush_icache (code, code - buf);
990 mono_profiler_code_buffer_new (buf, code - buf, MONO_PROFILER_CODE_BUFFER_HELPER, NULL);
991 g_assert (code - buf <= tramp_size);
992 g_assert_checked (mono_arch_unwindinfo_validate_size (unwind_ops, MONO_MAX_TRAMPOLINE_UNWINDINFO_SIZE));
994 const char *tramp_name = single_step ? "sdb_single_step_trampoline" : "sdb_breakpoint_trampoline";
995 *info = mono_tramp_info_create (tramp_name, buf, code - buf, ji, unwind_ops);
1001 * mono_arch_get_enter_icall_trampoline:
1003 * A trampoline that handles the transition from interpreter into native world.
1004 * It requiers to set up a descriptor (MethodArguments) that describes the
1005 * required arguments passed to the callee.
1008 mono_arch_get_enter_icall_trampoline (MonoTrampInfo **info)
1010 #ifdef ENABLE_INTERPRETER
1011 const int gregs_num = 8;
1012 const int fregs_num = 3;
1013 guint8 *start = NULL, *code, *label_gexits [gregs_num], *label_fexits [fregs_num], *label_leave_tramp [3], *label_is_float_ret;
1014 MonoJumpInfo *ji = NULL;
1015 GSList *unwind_ops = NULL;
1016 static int farg_regs[] = {AMD64_XMM0, AMD64_XMM1, AMD64_XMM2};
1017 int i, framesize = 0, off_rbp, off_methodargs, off_targetaddr;
1019 start = code = (guint8 *) mono_global_codeman_reserve (256 + MONO_TRAMPOLINE_UNWINDINFO_SIZE(0));
1021 off_rbp = -framesize;
1023 framesize += sizeof (mgreg_t);
1024 off_methodargs = -framesize;
1026 framesize += sizeof (mgreg_t);
1027 off_targetaddr = -framesize;
1029 framesize += (gregs_num - PARAM_REGS) * sizeof (mgreg_t);
1031 amd64_push_reg (code, AMD64_RBP);
1032 amd64_mov_reg_reg (code, AMD64_RBP, AMD64_RSP, sizeof (mgreg_t));
1033 amd64_alu_reg_imm (code, X86_SUB, AMD64_RSP, ALIGN_TO (framesize, MONO_ARCH_FRAME_ALIGNMENT));
1035 /* save MethodArguments* onto stack */
1036 amd64_mov_membase_reg (code, AMD64_RBP, off_methodargs, AMD64_ARG_REG2, sizeof (mgreg_t));
1038 /* save target address on stack */
1039 amd64_mov_membase_reg (code, AMD64_RBP, off_targetaddr, AMD64_ARG_REG1, sizeof (mgreg_t));
1041 /* load pointer to MethodArguments* into R11 */
1042 amd64_mov_reg_reg (code, AMD64_R11, AMD64_ARG_REG2, 8);
1044 /* move flen into RAX */ // TODO: struct offset
1045 amd64_mov_reg_membase (code, AMD64_RAX, AMD64_R11, 16, sizeof (mgreg_t));
1046 /* load pointer to fregs into R11 */ // TODO: struct offset
1047 amd64_mov_reg_membase (code, AMD64_R11, AMD64_R11, 24, sizeof (mgreg_t));
1049 for (i = 0; i < fregs_num; ++i) {
1050 amd64_test_reg_reg (code, AMD64_RAX, AMD64_RAX);
1051 label_fexits [i] = code;
1052 x86_branch8 (code, X86_CC_Z, 0, FALSE);
1054 amd64_sse_movsd_reg_membase (code, farg_regs [i], AMD64_R11, i * sizeof (double));
1055 amd64_dec_reg_size (code, AMD64_RAX, 1);
1058 for (i = 0; i < fregs_num; i++) {
1059 x86_patch (label_fexits [i], code);
1062 /* load pointer to MethodArguments* into R11 */
1063 amd64_mov_reg_reg (code, AMD64_R11, AMD64_ARG_REG2, sizeof (mgreg_t));
1064 /* move ilen into RAX */ // TODO: struct offset
1065 amd64_mov_reg_membase (code, AMD64_RAX, AMD64_R11, 0, sizeof (mgreg_t));
1067 int stack_offset = 0;
1068 for (i = 0; i < gregs_num; i++) {
1069 amd64_test_reg_reg (code, AMD64_RAX, AMD64_RAX);
1070 label_gexits [i] = code;
1071 x86_branch32 (code, X86_CC_Z, 0, FALSE);
1073 /* load pointer to MethodArguments* into R11 */
1074 amd64_mov_reg_membase (code, AMD64_R11, AMD64_RBP, off_methodargs, sizeof (mgreg_t));
1075 /* load pointer to iregs into R11 */ // TODO: struct offset
1076 amd64_mov_reg_membase (code, AMD64_R11, AMD64_R11, 8, sizeof (mgreg_t));
1078 if (i < PARAM_REGS) {
1079 amd64_mov_reg_membase (code, param_regs [i], AMD64_R11, i * sizeof (mgreg_t), sizeof (mgreg_t));
1081 amd64_mov_reg_membase (code, AMD64_R11, AMD64_R11, i * sizeof (mgreg_t), sizeof (mgreg_t));
1082 amd64_mov_membase_reg (code, AMD64_RSP, stack_offset, AMD64_R11, sizeof (mgreg_t));
1083 stack_offset += sizeof (mgreg_t);
1085 amd64_dec_reg_size (code, AMD64_RAX, 1);
1088 for (i = 0; i < gregs_num; i++) {
1089 x86_patch (label_gexits [i], code);
1092 /* load target addr */
1093 amd64_mov_reg_membase (code, AMD64_R11, AMD64_RBP, off_targetaddr, sizeof (mgreg_t));
1095 /* call into native function */
1096 amd64_call_reg (code, AMD64_R11);
1098 /* load MethodArguments */
1099 amd64_mov_reg_membase (code, AMD64_R11, AMD64_RBP, off_methodargs, sizeof (mgreg_t));
1101 /* load is_float_ret */ // TODO: struct offset
1102 amd64_mov_reg_membase (code, AMD64_R11, AMD64_R11, 0x28, sizeof (mgreg_t));
1104 /* check if a float return value is expected */
1105 amd64_test_reg_reg (code, AMD64_R11, AMD64_R11);
1107 label_is_float_ret = code;
1108 x86_branch8 (code, X86_CC_NZ, 0, FALSE);
1113 /* load MethodArguments */
1114 amd64_mov_reg_membase (code, AMD64_R11, AMD64_RBP, off_methodargs, sizeof (mgreg_t));
1115 /* load retval */ // TODO: struct offset
1116 amd64_mov_reg_membase (code, AMD64_R11, AMD64_R11, 0x20, sizeof (mgreg_t));
1118 amd64_test_reg_reg (code, AMD64_R11, AMD64_R11);
1119 label_leave_tramp [0] = code;
1120 x86_branch8 (code, X86_CC_Z, 0, FALSE);
1122 amd64_mov_membase_reg (code, AMD64_R11, 0, AMD64_RAX, sizeof (mgreg_t));
1124 label_leave_tramp [1] = code;
1125 x86_jump8 (code, 0);
1130 x86_patch (label_is_float_ret, code);
1131 /* load MethodArguments */
1132 amd64_mov_reg_membase (code, AMD64_R11, AMD64_RBP, off_methodargs, sizeof (mgreg_t));
1133 /* load retval */ // TODO: struct offset
1134 amd64_mov_reg_membase (code, AMD64_R11, AMD64_R11, 0x20, sizeof (mgreg_t));
1136 amd64_test_reg_reg (code, AMD64_R11, AMD64_R11);
1137 label_leave_tramp [2] = code;
1138 x86_branch8 (code, X86_CC_Z, 0, FALSE);
1140 amd64_sse_movsd_membase_reg (code, AMD64_R11, 0, AMD64_XMM0);
1142 for (i = 0; i < 3; i++)
1143 x86_patch (label_leave_tramp [i], code);
1145 amd64_alu_reg_imm (code, X86_ADD, AMD64_RSP, ALIGN_TO (framesize, MONO_ARCH_FRAME_ALIGNMENT));
1146 amd64_pop_reg (code, AMD64_RBP);
1149 mono_arch_flush_icache (start, code - start);
1150 mono_profiler_code_buffer_new (start, code - start, MONO_PROFILER_CODE_BUFFER_EXCEPTION_HANDLING, NULL);
1153 *info = mono_tramp_info_create ("enter_icall_trampoline", start, code - start, ji, unwind_ops);
1157 g_assert_not_reached ();
1159 #endif /* ENABLE_INTERPRETER */
1161 #endif /* !DISABLE_JIT */
1165 mono_arch_get_unbox_trampoline (MonoMethod *m, gpointer addr)
1167 g_assert_not_reached ();
1172 mono_arch_get_static_rgctx_trampoline (MonoMethod *m, MonoMethodRuntimeGenericContext *mrgctx, gpointer addr)
1174 g_assert_not_reached ();
1179 mono_arch_create_rgctx_lazy_fetch_trampoline (guint32 slot, MonoTrampInfo **info, gboolean aot)
1181 g_assert_not_reached ();
1186 mono_arch_create_generic_trampoline (MonoTrampolineType tramp_type, MonoTrampInfo **info, gboolean aot)
1188 g_assert_not_reached ();
1193 mono_arch_create_specific_trampoline (gpointer arg1, MonoTrampolineType tramp_type, MonoDomain *domain, guint32 *code_len)
1195 g_assert_not_reached ();
1200 mono_arch_create_general_rgctx_lazy_fetch_trampoline (MonoTrampInfo **info, gboolean aot)
1202 g_assert_not_reached ();
1207 mono_arch_create_handler_block_trampoline (MonoTrampInfo **info, gboolean aot)
1209 g_assert_not_reached ();
1214 mono_arch_invalidate_method (MonoJitInfo *ji, void *func, gpointer func_arg)
1216 g_assert_not_reached ();
1221 mono_arch_create_sdb_trampoline (gboolean single_step, MonoTrampInfo **info, gboolean aot)
1223 g_assert_not_reached ();
1228 mono_arch_get_enter_icall_trampoline (MonoTrampInfo **info)
1230 g_assert_not_reached ();
1233 #endif /* DISABLE_JIT */