2 * tramp-amd64.c: JIT trampoline code for amd64
5 * Dietmar Maurer (dietmar@ximian.com)
6 * Zoltan Varga (vargaz@gmail.com)
8 * (C) 2001 Ximian, Inc.
14 #include <mono/metadata/appdomain.h>
15 #include <mono/metadata/marshal.h>
16 #include <mono/metadata/tabledefs.h>
17 #include <mono/metadata/mono-debug-debugger.h>
18 #include <mono/metadata/monitor.h>
19 #include <mono/arch/amd64/amd64-codegen.h>
21 #ifdef HAVE_VALGRIND_MEMCHECK_H
22 #include <valgrind/memcheck.h>
26 #include "mini-amd64.h"
28 #define IS_REX(inst) (((inst) >= 0x40) && ((inst) <= 0x4f))
30 static guint8* nullified_class_init_trampoline;
33 * mono_arch_get_unbox_trampoline:
34 * @gsctx: the generic sharing context
36 * @addr: pointer to native code for @m
38 * when value type methods are called through the vtable we need to unbox the
39 * this argument. This method returns a pointer to a trampoline which does
40 * unboxing before calling the method
43 mono_arch_get_unbox_trampoline (MonoGenericSharingContext *gsctx, MonoMethod *m, gpointer addr)
48 MonoDomain *domain = mono_domain_get ();
50 this_reg = mono_arch_get_this_arg_reg (mono_method_signature (m), gsctx, NULL);
52 start = code = mono_domain_code_reserve (domain, 20);
54 amd64_alu_reg_imm (code, X86_ADD, this_reg, sizeof (MonoObject));
55 /* FIXME: Optimize this */
56 amd64_mov_reg_imm (code, AMD64_RAX, addr);
57 amd64_jump_reg (code, AMD64_RAX);
58 g_assert ((code - start) < 20);
60 mono_arch_flush_icache (start, code - start);
66 * mono_arch_get_static_rgctx_trampoline:
68 * Create a trampoline which sets RGCTX_REG to MRGCTX, then jumps to ADDR.
71 mono_arch_get_static_rgctx_trampoline (MonoMethod *m, MonoMethodRuntimeGenericContext *mrgctx, gpointer addr)
76 MonoDomain *domain = mono_domain_get ();
78 #ifdef MONO_ARCH_NOMAP32BIT
81 /* AOTed code could still have a non-32 bit address */
82 if ((((guint64)addr) >> 32) == 0)
88 start = code = mono_domain_code_reserve (domain, buf_len);
90 amd64_mov_reg_imm (code, MONO_ARCH_RGCTX_REG, mrgctx);
91 amd64_jump_code (code, addr);
92 g_assert ((code - start) < buf_len);
94 mono_arch_flush_icache (start, code - start);
100 mono_arch_get_llvm_imt_trampoline (MonoDomain *domain, MonoMethod *m, int vt_offset)
102 guint8 *code, *start;
108 start = code = mono_domain_code_reserve (domain, buf_len);
110 this_reg = mono_arch_get_this_arg_reg (mono_method_signature (m), NULL, NULL);
113 amd64_mov_reg_imm (code, MONO_ARCH_IMT_REG, m);
114 /* Load vtable address */
115 amd64_mov_reg_membase (code, AMD64_RAX, this_reg, 0, 8);
116 amd64_jump_membase (code, AMD64_RAX, vt_offset);
119 g_assert ((code - start) < buf_len);
121 mono_arch_flush_icache (start, code - start);
127 * mono_arch_patch_callsite:
129 * Patch the callsite whose address is given by ORIG_CODE so it calls ADDR. ORIG_CODE
130 * points to the pc right after the call.
133 mono_arch_patch_callsite (guint8 *method_start, guint8 *orig_code, guint8 *addr)
137 gboolean can_write = mono_breakpoint_clean_code (method_start, orig_code, 14, buf, sizeof (buf));
141 if (((code [-13] == 0x49) && (code [-12] == 0xbb)) || (code [-5] == 0xe8)) {
142 if (code [-5] != 0xe8) {
144 InterlockedExchangePointer ((gpointer*)(orig_code - 11), addr);
145 #ifdef HAVE_VALGRIND_MEMCHECK_H
146 VALGRIND_DISCARD_TRANSLATIONS (orig_code - 11, sizeof (gpointer));
150 if ((((guint64)(addr)) >> 32) != 0) {
151 #ifdef MONO_ARCH_NOMAP32BIT
152 /* Print some diagnostics */
153 MonoJitInfo *ji = mono_jit_info_table_find (mono_domain_get (), (char*)orig_code);
155 fprintf (stderr, "At %s, offset 0x%zx\n", mono_method_full_name (ji->method, TRUE), (guint8*)orig_code - (guint8*)ji->code_start);
156 fprintf (stderr, "Addr: %p\n", addr);
157 ji = mono_jit_info_table_find (mono_domain_get (), (char*)addr);
159 fprintf (stderr, "Callee: %s\n", mono_method_full_name (ji->method, TRUE));
160 g_assert_not_reached ();
163 * This might happen when calling AOTed code. Create a thunk.
165 guint8 *thunk_start, *thunk_code;
167 thunk_start = thunk_code = mono_domain_code_reserve (mono_domain_get (), 32);
168 amd64_jump_membase (thunk_code, AMD64_RIP, 0);
169 *(guint64*)thunk_code = (guint64)addr;
171 g_assert ((((guint64)(addr)) >> 32) == 0);
172 mono_arch_flush_icache (thunk_start, thunk_code - thunk_start);
175 g_assert ((((guint64)(orig_code)) >> 32) == 0);
177 InterlockedExchange ((gint32*)(orig_code - 4), ((gint64)addr - (gint64)orig_code));
178 #ifdef HAVE_VALGRIND_MEMCHECK_H
179 VALGRIND_DISCARD_TRANSLATIONS (orig_code - 5, 4);
184 else if ((code [-7] == 0x41) && (code [-6] == 0xff) && (code [-5] == 0x15)) {
185 /* call *<OFFSET>(%rip) */
186 gpointer *got_entry = (gpointer*)((guint8*)orig_code + (*(guint32*)(orig_code - 4)));
188 InterlockedExchangePointer (got_entry, addr);
189 #ifdef HAVE_VALGRIND_MEMCHECK_H
190 VALGRIND_DISCARD_TRANSLATIONS (orig_code - 5, sizeof (gpointer));
197 mono_arch_patch_plt_entry (guint8 *code, gpointer *got, mgreg_t *regs, guint8 *addr)
200 gpointer *plt_jump_table_entry;
202 /* A PLT entry: jmp *<DISP>(%rip) */
203 g_assert (code [0] == 0xff);
204 g_assert (code [1] == 0x25);
206 disp = *(gint32*)(code + 2);
208 plt_jump_table_entry = (gpointer*)(code + 6 + disp);
210 InterlockedExchangePointer (plt_jump_table_entry, addr);
214 mono_arch_nullify_class_init_trampoline (guint8 *code, mgreg_t *regs)
217 MonoJitInfo *ji = NULL;
221 /* code - 7 might be before the start of the method */
222 /* FIXME: Avoid this expensive call somehow */
223 ji = mono_jit_info_table_find (mono_domain_get (), (char*)code);
226 can_write = mono_breakpoint_clean_code (ji ? ji->code_start : NULL, code, 7, buf, sizeof (buf));
234 * A given byte sequence can match more than case here, so we have to be
235 * really careful about the ordering of the cases. Longer sequences
238 if ((buf [0] == 0x41) && (buf [1] == 0xff) && (buf [2] == 0x15)) {
239 gpointer *vtable_slot;
241 /* call *<OFFSET>(%rip) */
242 vtable_slot = mono_get_vcall_slot_addr (code + 3, regs);
243 g_assert (vtable_slot);
245 *vtable_slot = nullified_class_init_trampoline;
246 } else if (buf [2] == 0xe8) {
248 //guint8 *buf = code - 2;
251 * It would be better to replace the call with nops, but that doesn't seem
252 * to work on SMP machines even when the whole call is inside a cache line.
253 * Patching the call address seems to work.
263 mono_arch_patch_callsite (code - 2, code - 2 + 5, nullified_class_init_trampoline);
264 } else if ((buf [4] == 0x41) && (buf [5] == 0xff)) {
266 /* happens on machines without MAP_32BIT like freebsd */
267 /* amd64_set_reg_template is 10 bytes long */
268 guint8* buf = code - 10;
270 /* FIXME: Make this thread safe */
271 /* Padding code suggested by the AMD64 Opt Manual */
285 } else if ((buf [5] == 0xff) && (buf [6] == 0xd0)) {
287 /* Used by the LLVM JIT */
288 /* FIXME: Not thread safe */
291 } else if (buf [4] == 0x90 || buf [5] == 0xeb || buf [6] == 0x66) {
292 /* Already changed by another thread */
295 printf ("Invalid trampoline sequence: %x %x %x %x %x %x %x\n", buf [0], buf [1], buf [2], buf [3],
296 buf [4], buf [5], buf [6]);
297 g_assert_not_reached ();
302 mono_arch_nullify_plt_entry (guint8 *code, mgreg_t *regs)
304 if (mono_aot_only && !nullified_class_init_trampoline)
305 nullified_class_init_trampoline = mono_aot_get_named_code ("nullified_class_init_trampoline");
307 mono_arch_patch_plt_entry (code, NULL, regs, nullified_class_init_trampoline);
311 mono_arch_create_trampoline_code (MonoTrampolineType tramp_type)
316 GSList *unwind_ops, *l;
318 code = mono_arch_create_trampoline_code_full (tramp_type, &code_size, &ji, &unwind_ops, FALSE);
320 mono_save_trampoline_xdebug_info ("<generic_trampoline>", code, code_size, unwind_ops);
322 for (l = unwind_ops; l; l = l->next)
324 g_slist_free (unwind_ops);
330 mono_arch_create_trampoline_code_full (MonoTrampolineType tramp_type, guint32 *code_size, MonoJumpInfo **ji, GSList **out_unwind_ops, gboolean aot)
332 guint8 *buf, *code, *tramp, *br [2], *r11_save_code, *after_r11_save_code;
333 int i, lmf_offset, offset, res_offset, arg_offset, rax_offset, tramp_offset, saved_regs_offset;
334 int saved_fpregs_offset, rbp_offset, framesize, orig_rsp_to_rbp_offset, cfa_offset;
336 GSList *unwind_ops = NULL;
338 if (tramp_type == MONO_TRAMPOLINE_JUMP)
343 code = buf = mono_global_codeman_reserve (538);
347 framesize = 538 + sizeof (MonoLMF);
348 framesize = (framesize + (MONO_ARCH_FRAME_ALIGNMENT - 1)) & ~ (MONO_ARCH_FRAME_ALIGNMENT - 1);
350 orig_rsp_to_rbp_offset = 0;
351 r11_save_code = code;
352 /* Reserve 5 bytes for the mov_membase_reg to save R11 */
354 after_r11_save_code = code;
356 // CFA = sp + 16 (the trampoline address is on the stack)
358 mono_add_unwind_op_def_cfa (unwind_ops, code, buf, AMD64_RSP, 16);
359 // IP saved at CFA - 8
360 mono_add_unwind_op_offset (unwind_ops, code, buf, AMD64_RIP, -8);
362 /* Pop the return address off the stack */
363 amd64_pop_reg (code, AMD64_R11);
364 orig_rsp_to_rbp_offset += 8;
367 mono_add_unwind_op_def_cfa_offset (unwind_ops, code, buf, cfa_offset);
370 * Allocate a new stack frame
372 amd64_push_reg (code, AMD64_RBP);
374 mono_add_unwind_op_def_cfa_offset (unwind_ops, code, buf, cfa_offset);
375 mono_add_unwind_op_offset (unwind_ops, code, buf, AMD64_RBP, - cfa_offset);
377 orig_rsp_to_rbp_offset -= 8;
378 amd64_mov_reg_reg (code, AMD64_RBP, AMD64_RSP, 8);
379 mono_add_unwind_op_def_cfa_reg (unwind_ops, code, buf, AMD64_RBP);
380 amd64_alu_reg_imm (code, X86_SUB, AMD64_RSP, framesize);
383 rbp_offset = - offset;
386 rax_offset = - offset;
389 tramp_offset = - offset;
392 arg_offset = - offset;
394 /* Compute the trampoline address from the return address */
396 /* 7 = length of call *<offset>(rip) */
397 amd64_alu_reg_imm (code, X86_SUB, AMD64_R11, 7);
399 /* 5 = length of amd64_call_membase () */
400 amd64_alu_reg_imm (code, X86_SUB, AMD64_R11, 5);
402 amd64_mov_membase_reg (code, AMD64_RBP, tramp_offset, AMD64_R11, 8);
405 res_offset = - offset;
407 /* Save all registers */
409 offset += AMD64_NREG * 8;
410 saved_regs_offset = - offset;
411 for (i = 0; i < AMD64_NREG; ++i) {
412 if (i == AMD64_RBP) {
413 /* RAX is already saved */
414 amd64_mov_reg_membase (code, AMD64_RAX, AMD64_RBP, rbp_offset, 8);
415 amd64_mov_membase_reg (code, AMD64_RBP, saved_regs_offset + (i * 8), AMD64_RAX, 8);
416 } else if (i != AMD64_R11) {
417 amd64_mov_membase_reg (code, AMD64_RBP, saved_regs_offset + (i * 8), i, 8);
419 /* We have to save R11 right at the start of
420 the trampoline code because it's used as a
422 amd64_mov_membase_reg (r11_save_code, AMD64_RSP, saved_regs_offset + orig_rsp_to_rbp_offset + (i * 8), i, 8);
423 g_assert (r11_save_code == after_r11_save_code);
427 saved_fpregs_offset = - offset;
428 for (i = 0; i < 8; ++i)
429 amd64_movsd_membase_reg (code, AMD64_RBP, saved_fpregs_offset + (i * 8), i);
431 if (tramp_type != MONO_TRAMPOLINE_GENERIC_CLASS_INIT &&
432 tramp_type != MONO_TRAMPOLINE_MONITOR_ENTER &&
433 tramp_type != MONO_TRAMPOLINE_MONITOR_EXIT) {
434 /* Obtain the trampoline argument which is encoded in the instruction stream */
436 /* Load the GOT offset */
437 amd64_mov_reg_membase (code, AMD64_R11, AMD64_RBP, tramp_offset, 8);
438 amd64_mov_reg_membase (code, AMD64_RAX, AMD64_R11, 7, 4);
439 /* Compute the address of the GOT slot */
440 amd64_alu_reg_reg_size (code, X86_ADD, AMD64_R11, AMD64_RAX, 8);
442 amd64_mov_reg_membase (code, AMD64_R11, AMD64_R11, 0, 8);
444 amd64_mov_reg_membase (code, AMD64_R11, AMD64_RBP, tramp_offset, 8);
445 amd64_mov_reg_membase (code, AMD64_RAX, AMD64_R11, 5, 1);
446 amd64_widen_reg (code, AMD64_RAX, AMD64_RAX, TRUE, FALSE);
447 amd64_alu_reg_imm_size (code, X86_CMP, AMD64_RAX, 4, 1);
449 x86_branch8 (code, X86_CC_NE, 6, FALSE);
450 /* 32 bit immediate */
451 amd64_mov_reg_membase (code, AMD64_R11, AMD64_R11, 6, 4);
453 x86_jump8 (code, 10);
454 /* 64 bit immediate */
455 mono_amd64_patch (br [0], code);
456 amd64_mov_reg_membase (code, AMD64_R11, AMD64_R11, 6, 8);
457 mono_amd64_patch (br [1], code);
459 amd64_mov_membase_reg (code, AMD64_RBP, arg_offset, AMD64_R11, 8);
461 amd64_mov_reg_membase (code, AMD64_R11, AMD64_RBP, saved_regs_offset + (MONO_AMD64_ARG_REG1 * 8), 8);
462 amd64_mov_membase_reg (code, AMD64_RBP, arg_offset, AMD64_R11, 8);
467 offset += sizeof (MonoLMF);
468 lmf_offset = - offset;
472 amd64_mov_reg_membase (code, AMD64_R11, AMD64_RBP, 8, 8);
474 amd64_mov_reg_imm (code, AMD64_R11, 0);
475 amd64_mov_membase_reg (code, AMD64_RBP, lmf_offset + G_STRUCT_OFFSET (MonoLMF, rip), AMD64_R11, 8);
477 amd64_mov_reg_membase (code, AMD64_R11, AMD64_RSP, framesize, 8);
478 amd64_mov_membase_reg (code, AMD64_RBP, lmf_offset + G_STRUCT_OFFSET (MonoLMF, rbp), AMD64_R11, 8);
480 amd64_mov_reg_reg (code, AMD64_R11, AMD64_RSP, 8);
481 amd64_alu_reg_imm (code, X86_ADD, AMD64_R11, framesize + 16);
482 amd64_mov_membase_reg (code, AMD64_RBP, lmf_offset + G_STRUCT_OFFSET (MonoLMF, rsp), AMD64_R11, 8);
484 if (tramp_type == MONO_TRAMPOLINE_JIT || tramp_type == MONO_TRAMPOLINE_JUMP) {
485 amd64_mov_reg_membase (code, AMD64_R11, AMD64_RBP, arg_offset, 8);
486 amd64_mov_membase_reg (code, AMD64_RBP, lmf_offset + G_STRUCT_OFFSET (MonoLMF, method), AMD64_R11, 8);
488 amd64_mov_membase_imm (code, AMD64_RBP, lmf_offset + G_STRUCT_OFFSET (MonoLMF, method), 0, 8);
490 /* Save callee saved regs */
492 amd64_mov_membase_reg (code, AMD64_RBP, lmf_offset + G_STRUCT_OFFSET (MonoLMF, rdi), AMD64_RDI, 8);
493 amd64_mov_membase_reg (code, AMD64_RBP, lmf_offset + G_STRUCT_OFFSET (MonoLMF, rsi), AMD64_RSI, 8);
495 amd64_mov_membase_reg (code, AMD64_RBP, lmf_offset + G_STRUCT_OFFSET (MonoLMF, rbx), AMD64_RBX, 8);
496 amd64_mov_membase_reg (code, AMD64_RBP, lmf_offset + G_STRUCT_OFFSET (MonoLMF, r12), AMD64_R12, 8);
497 amd64_mov_membase_reg (code, AMD64_RBP, lmf_offset + G_STRUCT_OFFSET (MonoLMF, r13), AMD64_R13, 8);
498 amd64_mov_membase_reg (code, AMD64_RBP, lmf_offset + G_STRUCT_OFFSET (MonoLMF, r14), AMD64_R14, 8);
499 amd64_mov_membase_reg (code, AMD64_RBP, lmf_offset + G_STRUCT_OFFSET (MonoLMF, r15), AMD64_R15, 8);
502 *ji = mono_patch_info_list_prepend (*ji, code - buf, MONO_PATCH_INFO_JIT_ICALL_ADDR, "mono_get_lmf_addr");
503 amd64_mov_reg_membase (code, AMD64_R11, AMD64_RIP, 0, 8);
505 amd64_mov_reg_imm (code, AMD64_R11, mono_get_lmf_addr);
507 amd64_call_reg (code, AMD64_R11);
510 amd64_mov_membase_reg (code, AMD64_RBP, lmf_offset + G_STRUCT_OFFSET (MonoLMF, lmf_addr), AMD64_RAX, 8);
511 /* Save previous_lmf */
512 /* Set the lowest bit to 1 to signal that this LMF has the ip field set */
513 amd64_mov_reg_membase (code, AMD64_R11, AMD64_RAX, 0, 8);
514 amd64_alu_reg_imm_size (code, X86_ADD, AMD64_R11, 1, 8);
515 amd64_mov_membase_reg (code, AMD64_RBP, lmf_offset + G_STRUCT_OFFSET (MonoLMF, previous_lmf), AMD64_R11, 8);
517 amd64_lea_membase (code, AMD64_R11, AMD64_RBP, lmf_offset);
518 amd64_mov_membase_reg (code, AMD64_RAX, 0, AMD64_R11, 8);
522 /* Arg1 is the pointer to the saved registers */
523 amd64_lea_membase (code, AMD64_ARG_REG1, AMD64_RBP, saved_regs_offset);
525 /* Arg2 is the address of the calling code */
527 amd64_mov_reg_membase (code, AMD64_ARG_REG2, AMD64_RBP, 8, 8);
529 amd64_mov_reg_imm (code, AMD64_ARG_REG2, 0);
531 /* Arg3 is the method/vtable ptr */
532 amd64_mov_reg_membase (code, AMD64_ARG_REG3, AMD64_RBP, arg_offset, 8);
534 /* Arg4 is the trampoline address */
535 amd64_mov_reg_membase (code, AMD64_ARG_REG4, AMD64_RBP, tramp_offset, 8);
538 char *icall_name = g_strdup_printf ("trampoline_func_%d", tramp_type);
539 *ji = mono_patch_info_list_prepend (*ji, code - buf, MONO_PATCH_INFO_JIT_ICALL_ADDR, icall_name);
540 amd64_mov_reg_membase (code, AMD64_RAX, AMD64_RIP, 0, 8);
542 tramp = (guint8*)mono_get_trampoline_func (tramp_type);
543 amd64_mov_reg_imm (code, AMD64_RAX, tramp);
545 amd64_call_reg (code, AMD64_RAX);
547 /* Check for thread interruption */
548 /* This is not perf critical code so no need to check the interrupt flag */
550 * Have to call the _force_ variant, since there could be a protected wrapper on the top of the stack.
552 amd64_mov_membase_reg (code, AMD64_RBP, res_offset, AMD64_RAX, 8);
554 *ji = mono_patch_info_list_prepend (*ji, code - buf, MONO_PATCH_INFO_JIT_ICALL_ADDR, "mono_thread_force_interruption_checkpoint");
555 amd64_mov_reg_membase (code, AMD64_RAX, AMD64_RIP, 0, 8);
557 amd64_mov_reg_imm (code, AMD64_RAX, (guint8*)mono_thread_force_interruption_checkpoint);
559 amd64_call_reg (code, AMD64_RAX);
560 amd64_mov_reg_membase (code, AMD64_RAX, AMD64_RBP, res_offset, 8);
564 amd64_mov_reg_membase (code, AMD64_RCX, AMD64_RBP, lmf_offset + G_STRUCT_OFFSET (MonoLMF, previous_lmf), 8);
565 amd64_alu_reg_imm_size (code, X86_SUB, AMD64_RCX, 1, 8);
566 amd64_mov_reg_membase (code, AMD64_R11, AMD64_RBP, lmf_offset + G_STRUCT_OFFSET (MonoLMF, lmf_addr), 8);
567 amd64_mov_membase_reg (code, AMD64_R11, 0, AMD64_RCX, 8);
570 * Save rax to the stack, after the leave instruction, this will become part of
573 amd64_mov_membase_reg (code, AMD64_RBP, rax_offset, AMD64_RAX, 8);
575 /* Restore argument registers, r10 (needed to pass rgctx to
576 static shared generic methods), r11 (imt register for
577 interface calls), and rax (needed for direct calls to C vararg functions). */
578 for (i = 0; i < AMD64_NREG; ++i)
579 if (AMD64_IS_ARGUMENT_REG (i) || i == AMD64_R10 || i == AMD64_R11 || i == AMD64_RAX)
580 amd64_mov_reg_membase (code, i, AMD64_RBP, saved_regs_offset + (i * 8), 8);
582 for (i = 0; i < 8; ++i)
583 amd64_movsd_reg_membase (code, i, AMD64_RBP, saved_fpregs_offset + (i * 8));
588 if (MONO_TRAMPOLINE_TYPE_MUST_RETURN (tramp_type)) {
590 amd64_mov_reg_membase (code, AMD64_RAX, AMD64_RSP, rax_offset - 0x8, 8);
593 /* call the compiled method using the saved rax */
594 amd64_jump_membase (code, AMD64_RSP, rax_offset - 0x8);
597 g_assert ((code - buf) <= 538);
599 mono_arch_flush_icache (buf, code - buf);
601 *code_size = code - buf;
603 if (tramp_type == MONO_TRAMPOLINE_CLASS_INIT) {
606 /* Initialize the nullified class init trampoline used in the AOT case */
607 nullified_class_init_trampoline = mono_arch_get_nullified_class_init_trampoline (&code_len);
610 *out_unwind_ops = unwind_ops;
616 mono_arch_get_nullified_class_init_trampoline (guint32 *code_len)
620 code = buf = mono_global_codeman_reserve (16);
623 mono_arch_flush_icache (buf, code - buf);
625 *code_len = code - buf;
631 mono_arch_create_specific_trampoline (gpointer arg1, MonoTrampolineType tramp_type, MonoDomain *domain, guint32 *code_len)
633 guint8 *code, *buf, *tramp;
636 tramp = mono_get_trampoline_code (tramp_type);
638 if ((((guint64)arg1) >> 32) == 0)
643 code = buf = mono_domain_code_reserve_align (domain, size, 1);
645 amd64_call_code (code, tramp);
646 /* The trampoline code will obtain the argument from the instruction stream */
647 if ((((guint64)arg1) >> 32) == 0) {
649 *(guint32*)(code + 1) = (gint64)arg1;
653 *(guint64*)(code + 1) = (gint64)arg1;
657 g_assert ((code - buf) <= size);
662 mono_arch_flush_icache (buf, size);
668 mono_arch_create_rgctx_lazy_fetch_trampoline (guint32 slot)
673 return mono_arch_create_rgctx_lazy_fetch_trampoline_full (slot, &code_size, &ji, FALSE);
677 mono_arch_create_rgctx_lazy_fetch_trampoline_full (guint32 slot, guint32 *code_size, MonoJumpInfo **ji, gboolean aot)
681 guint8 **rgctx_null_jumps;
689 mrgctx = MONO_RGCTX_SLOT_IS_MRGCTX (slot);
690 index = MONO_RGCTX_SLOT_INDEX (slot);
692 index += MONO_SIZEOF_METHOD_RUNTIME_GENERIC_CONTEXT / sizeof (gpointer);
693 for (depth = 0; ; ++depth) {
694 int size = mono_class_rgctx_get_array_size (depth, mrgctx);
696 if (index < size - 1)
701 tramp_size = 64 + 8 * depth;
703 code = buf = mono_global_codeman_reserve (tramp_size);
705 rgctx_null_jumps = g_malloc (sizeof (guint8*) * (depth + 2));
709 amd64_mov_reg_reg (code, AMD64_RAX, AMD64_ARG_REG1, 8);
711 /* load rgctx ptr from vtable */
712 amd64_mov_reg_membase (code, AMD64_RAX, AMD64_ARG_REG1, G_STRUCT_OFFSET (MonoVTable, runtime_generic_context), 8);
713 /* is the rgctx ptr null? */
714 amd64_test_reg_reg (code, AMD64_RAX, AMD64_RAX);
715 /* if yes, jump to actual trampoline */
716 rgctx_null_jumps [0] = code;
717 amd64_branch8 (code, X86_CC_Z, -1, 1);
720 for (i = 0; i < depth; ++i) {
721 /* load ptr to next array */
722 if (mrgctx && i == 0)
723 amd64_mov_reg_membase (code, AMD64_RAX, AMD64_RAX, MONO_SIZEOF_METHOD_RUNTIME_GENERIC_CONTEXT, 8);
725 amd64_mov_reg_membase (code, AMD64_RAX, AMD64_RAX, 0, 8);
726 /* is the ptr null? */
727 amd64_test_reg_reg (code, AMD64_RAX, AMD64_RAX);
728 /* if yes, jump to actual trampoline */
729 rgctx_null_jumps [i + 1] = code;
730 amd64_branch8 (code, X86_CC_Z, -1, 1);
734 amd64_mov_reg_membase (code, AMD64_RAX, AMD64_RAX, sizeof (gpointer) * (index + 1), 8);
735 /* is the slot null? */
736 amd64_test_reg_reg (code, AMD64_RAX, AMD64_RAX);
737 /* if yes, jump to actual trampoline */
738 rgctx_null_jumps [depth + 1] = code;
739 amd64_branch8 (code, X86_CC_Z, -1, 1);
740 /* otherwise return */
743 for (i = mrgctx ? 1 : 0; i <= depth + 1; ++i)
744 x86_patch (rgctx_null_jumps [i], code);
746 g_free (rgctx_null_jumps);
748 /* move the rgctx pointer to the VTABLE register */
749 amd64_mov_reg_reg (code, MONO_ARCH_VTABLE_REG, AMD64_ARG_REG1, 8);
752 *ji = mono_patch_info_list_prepend (*ji, code - buf, MONO_PATCH_INFO_JIT_ICALL_ADDR, g_strdup_printf ("specific_trampoline_lazy_fetch_%u", slot));
753 amd64_mov_reg_membase (code, AMD64_R11, AMD64_RIP, 0, 8);
754 amd64_jump_reg (code, AMD64_R11);
756 tramp = mono_arch_create_specific_trampoline (GUINT_TO_POINTER (slot), MONO_TRAMPOLINE_RGCTX_LAZY_FETCH, mono_get_root_domain (), NULL);
758 /* jump to the actual trampoline */
759 amd64_jump_code (code, tramp);
762 mono_arch_flush_icache (buf, code - buf);
764 g_assert (code - buf <= tramp_size);
766 *code_size = code - buf;
772 mono_arch_create_generic_class_init_trampoline (void)
777 return mono_arch_create_generic_class_init_trampoline_full (&code_size, &ji, FALSE);
781 mono_arch_create_generic_class_init_trampoline_full (guint32 *code_size, MonoJumpInfo **ji, gboolean aot)
785 static int byte_offset = -1;
786 static guint8 bitmask;
794 code = buf = mono_global_codeman_reserve (tramp_size);
797 mono_marshal_find_bitfield_offset (MonoVTable, initialized, &byte_offset, &bitmask);
799 amd64_test_membase_imm_size (code, MONO_AMD64_ARG_REG1, byte_offset, bitmask, 1);
801 amd64_branch8 (code, X86_CC_Z, -1, 1);
805 x86_patch (jump, code);
808 *ji = mono_patch_info_list_prepend (*ji, code - buf, MONO_PATCH_INFO_JIT_ICALL_ADDR, "specific_trampoline_generic_class_init");
809 amd64_mov_reg_membase (code, AMD64_R11, AMD64_RIP, 0, 8);
810 amd64_jump_reg (code, AMD64_R11);
812 tramp = mono_arch_create_specific_trampoline (NULL, MONO_TRAMPOLINE_GENERIC_CLASS_INIT, mono_get_root_domain (), NULL);
814 /* jump to the actual trampoline */
815 amd64_jump_code (code, tramp);
818 mono_arch_flush_icache (buf, code - buf);
820 g_assert (code - buf <= tramp_size);
822 *code_size = code - buf;
827 #ifdef MONO_ARCH_MONITOR_OBJECT_REG
830 mono_arch_create_monitor_enter_trampoline (void)
835 return mono_arch_create_monitor_enter_trampoline_full (&code_size, &ji, FALSE);
839 mono_arch_create_monitor_enter_trampoline_full (guint32 *code_size, MonoJumpInfo **ji, gboolean aot)
844 guint8 *jump_obj_null, *jump_sync_null, *jump_cmpxchg_failed, *jump_other_owner, *jump_tid;
846 int owner_offset, nest_offset, dummy;
850 g_assert (MONO_ARCH_MONITOR_OBJECT_REG == AMD64_RDI);
852 mono_monitor_threads_sync_members_offset (&owner_offset, &nest_offset, &dummy);
853 g_assert (MONO_THREADS_SYNC_MEMBER_SIZE (owner_offset) == sizeof (gpointer));
854 g_assert (MONO_THREADS_SYNC_MEMBER_SIZE (nest_offset) == sizeof (guint32));
855 owner_offset = MONO_THREADS_SYNC_MEMBER_OFFSET (owner_offset);
856 nest_offset = MONO_THREADS_SYNC_MEMBER_OFFSET (nest_offset);
860 code = buf = mono_global_codeman_reserve (tramp_size);
862 if (mono_thread_get_tls_offset () != -1) {
863 /* MonoObject* obj is in RDI */
865 amd64_test_reg_reg (code, AMD64_RDI, AMD64_RDI);
866 /* if yes, jump to actual trampoline */
867 jump_obj_null = code;
868 amd64_branch8 (code, X86_CC_Z, -1, 1);
870 /* load obj->synchronization to RCX */
871 amd64_mov_reg_membase (code, AMD64_RCX, AMD64_RDI, G_STRUCT_OFFSET (MonoObject, synchronisation), 8);
872 /* is synchronization null? */
873 amd64_test_reg_reg (code, AMD64_RCX, AMD64_RCX);
874 /* if yes, jump to actual trampoline */
875 jump_sync_null = code;
876 amd64_branch8 (code, X86_CC_Z, -1, 1);
878 /* load MonoInternalThread* into RDX */
879 code = mono_amd64_emit_tls_get (code, AMD64_RDX, mono_thread_get_tls_offset ());
880 /* load TID into RDX */
881 amd64_mov_reg_membase (code, AMD64_RDX, AMD64_RDX, G_STRUCT_OFFSET (MonoInternalThread, tid), 8);
883 /* is synchronization->owner null? */
884 amd64_alu_membase_imm_size (code, X86_CMP, AMD64_RCX, owner_offset, 0, 8);
885 /* if not, jump to next case */
887 amd64_branch8 (code, X86_CC_NZ, -1, 1);
889 /* if yes, try a compare-exchange with the TID */
891 amd64_alu_reg_reg (code, X86_XOR, AMD64_RAX, AMD64_RAX);
892 /* compare and exchange */
893 amd64_prefix (code, X86_LOCK_PREFIX);
894 amd64_cmpxchg_membase_reg_size (code, AMD64_RCX, owner_offset, AMD64_RDX, 8);
895 /* if not successful, jump to actual trampoline */
896 jump_cmpxchg_failed = code;
897 amd64_branch8 (code, X86_CC_NZ, -1, 1);
898 /* if successful, return */
901 /* next case: synchronization->owner is not null */
902 x86_patch (jump_tid, code);
903 /* is synchronization->owner == TID? */
904 amd64_alu_membase_reg_size (code, X86_CMP, AMD64_RCX, owner_offset, AMD64_RDX, 8);
905 /* if not, jump to actual trampoline */
906 jump_other_owner = code;
907 amd64_branch8 (code, X86_CC_NZ, -1, 1);
908 /* if yes, increment nest */
909 amd64_inc_membase_size (code, AMD64_RCX, nest_offset, 4);
913 x86_patch (jump_obj_null, code);
914 x86_patch (jump_sync_null, code);
915 x86_patch (jump_cmpxchg_failed, code);
916 x86_patch (jump_other_owner, code);
919 /* jump to the actual trampoline */
920 #if MONO_AMD64_ARG_REG1 != AMD64_RDI
921 amd64_mov_reg_reg (code, MONO_AMD64_ARG_REG1, AMD64_RDI);
925 *ji = mono_patch_info_list_prepend (*ji, code - buf, MONO_PATCH_INFO_JIT_ICALL_ADDR, "specific_trampoline_monitor_enter");
926 amd64_mov_reg_membase (code, AMD64_R11, AMD64_RIP, 0, 8);
927 amd64_jump_reg (code, AMD64_R11);
929 tramp = mono_arch_create_specific_trampoline (NULL, MONO_TRAMPOLINE_MONITOR_ENTER, mono_get_root_domain (), NULL);
931 /* jump to the actual trampoline */
932 amd64_jump_code (code, tramp);
935 mono_arch_flush_icache (code, code - buf);
936 g_assert (code - buf <= tramp_size);
938 *code_size = code - buf;
944 mono_arch_create_monitor_exit_trampoline (void)
949 return mono_arch_create_monitor_exit_trampoline_full (&code_size, &ji, FALSE);
953 mono_arch_create_monitor_exit_trampoline_full (guint32 *code_size, MonoJumpInfo **ji, gboolean aot)
957 guint8 *jump_obj_null, *jump_have_waiters;
960 int owner_offset, nest_offset, entry_count_offset;
964 g_assert (MONO_ARCH_MONITOR_OBJECT_REG == AMD64_RDI);
966 mono_monitor_threads_sync_members_offset (&owner_offset, &nest_offset, &entry_count_offset);
967 g_assert (MONO_THREADS_SYNC_MEMBER_SIZE (owner_offset) == sizeof (gpointer));
968 g_assert (MONO_THREADS_SYNC_MEMBER_SIZE (nest_offset) == sizeof (guint32));
969 g_assert (MONO_THREADS_SYNC_MEMBER_SIZE (entry_count_offset) == sizeof (gint32));
970 owner_offset = MONO_THREADS_SYNC_MEMBER_OFFSET (owner_offset);
971 nest_offset = MONO_THREADS_SYNC_MEMBER_OFFSET (nest_offset);
972 entry_count_offset = MONO_THREADS_SYNC_MEMBER_OFFSET (entry_count_offset);
976 code = buf = mono_global_codeman_reserve (tramp_size);
978 if (mono_thread_get_tls_offset () != -1) {
979 /* MonoObject* obj is in RDI */
981 amd64_test_reg_reg (code, AMD64_RDI, AMD64_RDI);
982 /* if yes, jump to actual trampoline */
983 jump_obj_null = code;
984 amd64_branch8 (code, X86_CC_Z, -1, 1);
986 /* load obj->synchronization to RCX */
987 amd64_mov_reg_membase (code, AMD64_RCX, AMD64_RDI, G_STRUCT_OFFSET (MonoObject, synchronisation), 8);
988 /* is synchronization null? */
989 amd64_test_reg_reg (code, AMD64_RCX, AMD64_RCX);
990 /* if not, jump to next case */
992 amd64_branch8 (code, X86_CC_NZ, -1, 1);
993 /* if yes, just return */
996 /* next case: synchronization is not null */
997 x86_patch (jump_next, code);
998 /* load MonoInternalThread* into RDX */
999 code = mono_amd64_emit_tls_get (code, AMD64_RDX, mono_thread_get_tls_offset ());
1000 /* load TID into RDX */
1001 amd64_mov_reg_membase (code, AMD64_RDX, AMD64_RDX, G_STRUCT_OFFSET (MonoInternalThread, tid), 8);
1002 /* is synchronization->owner == TID */
1003 amd64_alu_membase_reg_size (code, X86_CMP, AMD64_RCX, owner_offset, AMD64_RDX, 8);
1004 /* if yes, jump to next case */
1006 amd64_branch8 (code, X86_CC_Z, -1, 1);
1007 /* if not, just return */
1010 /* next case: synchronization->owner == TID */
1011 x86_patch (jump_next, code);
1012 /* is synchronization->nest == 1 */
1013 amd64_alu_membase_imm_size (code, X86_CMP, AMD64_RCX, nest_offset, 1, 4);
1014 /* if not, jump to next case */
1016 amd64_branch8 (code, X86_CC_NZ, -1, 1);
1017 /* if yes, is synchronization->entry_count zero? */
1018 amd64_alu_membase_imm_size (code, X86_CMP, AMD64_RCX, entry_count_offset, 0, 4);
1019 /* if not, jump to actual trampoline */
1020 jump_have_waiters = code;
1021 amd64_branch8 (code, X86_CC_NZ, -1 , 1);
1022 /* if yes, set synchronization->owner to null and return */
1023 amd64_mov_membase_imm (code, AMD64_RCX, owner_offset, 0, 8);
1026 /* next case: synchronization->nest is not 1 */
1027 x86_patch (jump_next, code);
1028 /* decrease synchronization->nest and return */
1029 amd64_dec_membase_size (code, AMD64_RCX, nest_offset, 4);
1032 x86_patch (jump_obj_null, code);
1033 x86_patch (jump_have_waiters, code);
1036 /* jump to the actual trampoline */
1037 #if MONO_AMD64_ARG_REG1 != AMD64_RDI
1038 amd64_mov_reg_reg (code, MONO_AMD64_ARG_REG1, AMD64_RDI);
1042 *ji = mono_patch_info_list_prepend (*ji, code - buf, MONO_PATCH_INFO_JIT_ICALL_ADDR, "specific_trampoline_monitor_exit");
1043 amd64_mov_reg_membase (code, AMD64_R11, AMD64_RIP, 0, 8);
1044 amd64_jump_reg (code, AMD64_R11);
1046 tramp = mono_arch_create_specific_trampoline (NULL, MONO_TRAMPOLINE_MONITOR_EXIT, mono_get_root_domain (), NULL);
1047 amd64_jump_code (code, tramp);
1050 mono_arch_flush_icache (code, code - buf);
1051 g_assert (code - buf <= tramp_size);
1053 *code_size = code - buf;
1060 mono_arch_invalidate_method (MonoJitInfo *ji, void *func, gpointer func_arg)
1062 /* FIXME: This is not thread safe */
1063 guint8 *code = ji->code_start;
1065 amd64_mov_reg_imm (code, AMD64_ARG_REG1, func_arg);
1066 amd64_mov_reg_imm (code, AMD64_R11, func);
1068 x86_push_imm (code, (guint64)func_arg);
1069 amd64_call_reg (code, AMD64_R11);