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
84 start = code = mono_domain_code_reserve (domain, buf_len);
86 amd64_mov_reg_imm (code, MONO_ARCH_RGCTX_REG, mrgctx);
87 amd64_jump_code (code, addr);
88 g_assert ((code - start) < buf_len);
90 mono_arch_flush_icache (start, code - start);
96 * mono_arch_patch_callsite:
98 * Patch the callsite whose address is given by ORIG_CODE so it calls ADDR. ORIG_CODE
99 * points to the pc right after the call.
102 mono_arch_patch_callsite (guint8 *method_start, guint8 *orig_code, guint8 *addr)
106 gboolean can_write = mono_breakpoint_clean_code (method_start, orig_code, 14, buf, sizeof (buf));
110 if (((code [-13] == 0x49) && (code [-12] == 0xbb)) || (code [-5] == 0xe8)) {
111 if (code [-5] != 0xe8) {
113 InterlockedExchangePointer ((gpointer*)(orig_code - 11), addr);
114 #ifdef HAVE_VALGRIND_MEMCHECK_H
115 VALGRIND_DISCARD_TRANSLATIONS (orig_code - 11, sizeof (gpointer));
119 if ((((guint64)(addr)) >> 32) != 0) {
120 /* Print some diagnostics */
121 MonoJitInfo *ji = mono_jit_info_table_find (mono_domain_get (), (char*)orig_code);
123 fprintf (stderr, "At %s, offset 0x%zx\n", mono_method_full_name (ji->method, TRUE), (guint8*)orig_code - (guint8*)ji->code_start);
124 fprintf (stderr, "Addr: %p\n", addr);
125 ji = mono_jit_info_table_find (mono_domain_get (), (char*)addr);
127 fprintf (stderr, "Callee: %s\n", mono_method_full_name (ji->method, TRUE));
128 g_assert_not_reached ();
130 g_assert ((((guint64)(orig_code)) >> 32) == 0);
132 InterlockedExchange ((gint32*)(orig_code - 4), ((gint64)addr - (gint64)orig_code));
133 #ifdef HAVE_VALGRIND_MEMCHECK_H
134 VALGRIND_DISCARD_TRANSLATIONS (orig_code - 5, 4);
139 else if ((code [-7] == 0x41) && (code [-6] == 0xff) && (code [-5] == 0x15)) {
140 /* call *<OFFSET>(%rip) */
141 gpointer *got_entry = (gpointer*)((guint8*)orig_code + (*(guint32*)(orig_code - 4)));
143 InterlockedExchangePointer (got_entry, addr);
144 #ifdef HAVE_VALGRIND_MEMCHECK_H
145 VALGRIND_DISCARD_TRANSLATIONS (orig_code - 5, sizeof (gpointer));
152 mono_arch_patch_plt_entry (guint8 *code, guint8 *addr)
155 gpointer *plt_jump_table_entry;
157 /* A PLT entry: jmp *<DISP>(%rip) */
158 g_assert (code [0] == 0xff);
159 g_assert (code [1] == 0x25);
161 disp = *(gint32*)(code + 2);
163 plt_jump_table_entry = (gpointer*)(code + 6 + disp);
165 InterlockedExchangePointer (plt_jump_table_entry, addr);
169 mono_arch_nullify_class_init_trampoline (guint8 *code, gssize *regs)
172 gboolean can_write = mono_breakpoint_clean_code (NULL, code, 7, buf, sizeof (buf));
180 * A given byte sequence can match more than case here, so we have to be
181 * really careful about the ordering of the cases. Longer sequences
184 if ((code [-4] == 0x41) && (code [-3] == 0xff) && (code [-2] == 0x15)) {
185 gpointer *vtable_slot;
187 /* call *<OFFSET>(%rip) */
188 vtable_slot = mono_arch_get_vcall_slot_addr (code + 3, (gpointer*)regs);
189 g_assert (vtable_slot);
191 *vtable_slot = nullified_class_init_trampoline;
192 } else if (code [-2] == 0xe8) {
194 //guint8 *buf = code - 2;
197 * It would be better to replace the call with nops, but that doesn't seem
198 * to work on SMP machines even when the whole call is inside a cache line.
199 * Patching the call address seems to work.
209 mono_arch_patch_callsite (code - 2, code - 2 + 5, nullified_class_init_trampoline);
210 } else if ((code [0] == 0x41) && (code [1] == 0xff)) {
212 /* happens on machines without MAP_32BIT like freebsd */
213 /* amd64_set_reg_template is 10 bytes long */
214 guint8* buf = code - 10;
216 /* FIXME: Make this thread safe */
217 /* Padding code suggested by the AMD64 Opt Manual */
231 } else if (code [0] == 0x90 || code [0] == 0xeb || code [0] == 0x66) {
232 /* Already changed by another thread */
235 printf ("Invalid trampoline sequence: %x %x %x %x %x %x %x\n", code [0], code [1], code [2], code [3],
236 code [4], code [5], code [6]);
237 g_assert_not_reached ();
242 mono_arch_nullify_plt_entry (guint8 *code)
244 if (mono_aot_only && !nullified_class_init_trampoline)
245 nullified_class_init_trampoline = mono_aot_get_named_code ("nullified_class_init_trampoline");
247 mono_arch_patch_plt_entry (code, nullified_class_init_trampoline);
251 mono_arch_create_trampoline_code (MonoTrampolineType tramp_type)
256 GSList *unwind_ops, *l;
258 code = mono_arch_create_trampoline_code_full (tramp_type, &code_size, &ji, &unwind_ops, FALSE);
260 mono_save_trampoline_xdebug_info ("<generic_trampoline>", code, code_size, unwind_ops);
262 for (l = unwind_ops; l; l = l->next)
264 g_slist_free (unwind_ops);
270 mono_arch_create_trampoline_code_full (MonoTrampolineType tramp_type, guint32 *code_size, MonoJumpInfo **ji, GSList **out_unwind_ops, gboolean aot)
272 guint8 *buf, *code, *tramp, *br [2], *r11_save_code, *after_r11_save_code;
273 int i, lmf_offset, offset, res_offset, arg_offset, rax_offset, tramp_offset, saved_regs_offset;
274 int saved_fpregs_offset, rbp_offset, framesize, orig_rsp_to_rbp_offset, cfa_offset;
276 GSList *unwind_ops = NULL;
278 if (tramp_type == MONO_TRAMPOLINE_JUMP)
283 code = buf = mono_global_codeman_reserve (538);
287 framesize = 538 + sizeof (MonoLMF);
288 framesize = (framesize + (MONO_ARCH_FRAME_ALIGNMENT - 1)) & ~ (MONO_ARCH_FRAME_ALIGNMENT - 1);
290 orig_rsp_to_rbp_offset = 0;
291 r11_save_code = code;
292 /* Reserve 5 bytes for the mov_membase_reg to save R11 */
294 after_r11_save_code = code;
296 // CFA = sp + 16 (the trampoline address is on the stack)
298 mono_add_unwind_op_def_cfa (unwind_ops, code, buf, AMD64_RSP, 16);
299 // IP saved at CFA - 8
300 mono_add_unwind_op_offset (unwind_ops, code, buf, AMD64_RIP, -8);
302 /* Pop the return address off the stack */
303 amd64_pop_reg (code, AMD64_R11);
304 orig_rsp_to_rbp_offset += 8;
307 mono_add_unwind_op_def_cfa_offset (unwind_ops, code, buf, cfa_offset);
310 * Allocate a new stack frame
312 amd64_push_reg (code, AMD64_RBP);
314 mono_add_unwind_op_def_cfa_offset (unwind_ops, code, buf, cfa_offset);
315 mono_add_unwind_op_offset (unwind_ops, code, buf, AMD64_RBP, - cfa_offset);
317 orig_rsp_to_rbp_offset -= 8;
318 amd64_mov_reg_reg (code, AMD64_RBP, AMD64_RSP, 8);
319 mono_add_unwind_op_def_cfa_reg (unwind_ops, code, buf, AMD64_RBP);
320 amd64_alu_reg_imm (code, X86_SUB, AMD64_RSP, framesize);
323 rbp_offset = - offset;
326 rax_offset = - offset;
329 tramp_offset = - offset;
332 arg_offset = - offset;
334 /* Compute the trampoline address from the return address */
336 /* 7 = length of call *<offset>(rip) */
337 amd64_alu_reg_imm (code, X86_SUB, AMD64_R11, 7);
339 /* 5 = length of amd64_call_membase () */
340 amd64_alu_reg_imm (code, X86_SUB, AMD64_R11, 5);
342 amd64_mov_membase_reg (code, AMD64_RBP, tramp_offset, AMD64_R11, 8);
345 res_offset = - offset;
347 /* Save all registers */
349 offset += AMD64_NREG * 8;
350 saved_regs_offset = - offset;
351 for (i = 0; i < AMD64_NREG; ++i) {
352 if (i == AMD64_RBP) {
353 /* RAX is already saved */
354 amd64_mov_reg_membase (code, AMD64_RAX, AMD64_RBP, rbp_offset, 8);
355 amd64_mov_membase_reg (code, AMD64_RBP, saved_regs_offset + (i * 8), AMD64_RAX, 8);
356 } else if (i != AMD64_R11) {
357 amd64_mov_membase_reg (code, AMD64_RBP, saved_regs_offset + (i * 8), i, 8);
359 /* We have to save R11 right at the start of
360 the trampoline code because it's used as a
362 amd64_mov_membase_reg (r11_save_code, AMD64_RSP, saved_regs_offset + orig_rsp_to_rbp_offset + (i * 8), i, 8);
363 g_assert (r11_save_code == after_r11_save_code);
367 saved_fpregs_offset = - offset;
368 for (i = 0; i < 8; ++i)
369 amd64_movsd_membase_reg (code, AMD64_RBP, saved_fpregs_offset + (i * 8), i);
371 if (tramp_type != MONO_TRAMPOLINE_GENERIC_CLASS_INIT &&
372 tramp_type != MONO_TRAMPOLINE_MONITOR_ENTER &&
373 tramp_type != MONO_TRAMPOLINE_MONITOR_EXIT) {
374 /* Obtain the trampoline argument which is encoded in the instruction stream */
376 /* Load the GOT offset */
377 amd64_mov_reg_membase (code, AMD64_R11, AMD64_RBP, tramp_offset, 8);
378 amd64_mov_reg_membase (code, AMD64_RAX, AMD64_R11, 7, 4);
379 /* Compute the address of the GOT slot */
380 amd64_alu_reg_reg_size (code, X86_ADD, AMD64_R11, AMD64_RAX, 8);
382 amd64_mov_reg_membase (code, AMD64_R11, AMD64_R11, 0, 8);
384 amd64_mov_reg_membase (code, AMD64_R11, AMD64_RBP, tramp_offset, 8);
385 amd64_mov_reg_membase (code, AMD64_RAX, AMD64_R11, 5, 1);
386 amd64_widen_reg (code, AMD64_RAX, AMD64_RAX, TRUE, FALSE);
387 amd64_alu_reg_imm_size (code, X86_CMP, AMD64_RAX, 4, 1);
389 x86_branch8 (code, X86_CC_NE, 6, FALSE);
390 /* 32 bit immediate */
391 amd64_mov_reg_membase (code, AMD64_R11, AMD64_R11, 6, 4);
393 x86_jump8 (code, 10);
394 /* 64 bit immediate */
395 mono_amd64_patch (br [0], code);
396 amd64_mov_reg_membase (code, AMD64_R11, AMD64_R11, 6, 8);
397 mono_amd64_patch (br [1], code);
399 amd64_mov_membase_reg (code, AMD64_RBP, arg_offset, AMD64_R11, 8);
401 amd64_mov_reg_membase (code, AMD64_R11, AMD64_RBP, saved_regs_offset + (MONO_AMD64_ARG_REG1 * 8), 8);
402 amd64_mov_membase_reg (code, AMD64_RBP, arg_offset, AMD64_R11, 8);
407 offset += sizeof (MonoLMF);
408 lmf_offset = - offset;
412 amd64_mov_reg_membase (code, AMD64_R11, AMD64_RBP, 8, 8);
414 amd64_mov_reg_imm (code, AMD64_R11, 0);
415 amd64_mov_membase_reg (code, AMD64_RBP, lmf_offset + G_STRUCT_OFFSET (MonoLMF, rip), AMD64_R11, 8);
417 amd64_mov_reg_membase (code, AMD64_R11, AMD64_RSP, framesize, 8);
418 amd64_mov_membase_reg (code, AMD64_RBP, lmf_offset + G_STRUCT_OFFSET (MonoLMF, rbp), AMD64_R11, 8);
420 amd64_mov_reg_reg (code, AMD64_R11, AMD64_RSP, 8);
421 amd64_alu_reg_imm (code, X86_ADD, AMD64_R11, framesize + 16);
422 amd64_mov_membase_reg (code, AMD64_RBP, lmf_offset + G_STRUCT_OFFSET (MonoLMF, rsp), AMD64_R11, 8);
424 if (tramp_type == MONO_TRAMPOLINE_JIT || tramp_type == MONO_TRAMPOLINE_JUMP) {
425 amd64_mov_reg_membase (code, AMD64_R11, AMD64_RBP, arg_offset, 8);
426 amd64_mov_membase_reg (code, AMD64_RBP, lmf_offset + G_STRUCT_OFFSET (MonoLMF, method), AMD64_R11, 8);
428 amd64_mov_membase_imm (code, AMD64_RBP, lmf_offset + G_STRUCT_OFFSET (MonoLMF, method), 0, 8);
430 /* Save callee saved regs */
431 #ifdef PLATFORM_WIN32
432 amd64_mov_membase_reg (code, AMD64_RBP, lmf_offset + G_STRUCT_OFFSET (MonoLMF, rdi), AMD64_RDI, 8);
433 amd64_mov_membase_reg (code, AMD64_RBP, lmf_offset + G_STRUCT_OFFSET (MonoLMF, rsi), AMD64_RSI, 8);
435 amd64_mov_membase_reg (code, AMD64_RBP, lmf_offset + G_STRUCT_OFFSET (MonoLMF, rbx), AMD64_RBX, 8);
436 amd64_mov_membase_reg (code, AMD64_RBP, lmf_offset + G_STRUCT_OFFSET (MonoLMF, r12), AMD64_R12, 8);
437 amd64_mov_membase_reg (code, AMD64_RBP, lmf_offset + G_STRUCT_OFFSET (MonoLMF, r13), AMD64_R13, 8);
438 amd64_mov_membase_reg (code, AMD64_RBP, lmf_offset + G_STRUCT_OFFSET (MonoLMF, r14), AMD64_R14, 8);
439 amd64_mov_membase_reg (code, AMD64_RBP, lmf_offset + G_STRUCT_OFFSET (MonoLMF, r15), AMD64_R15, 8);
442 *ji = mono_patch_info_list_prepend (*ji, code - buf, MONO_PATCH_INFO_JIT_ICALL_ADDR, "mono_get_lmf_addr");
443 amd64_mov_reg_membase (code, AMD64_R11, AMD64_RIP, 0, 8);
445 amd64_mov_reg_imm (code, AMD64_R11, mono_get_lmf_addr);
447 amd64_call_reg (code, AMD64_R11);
450 amd64_mov_membase_reg (code, AMD64_RBP, lmf_offset + G_STRUCT_OFFSET (MonoLMF, lmf_addr), AMD64_RAX, 8);
451 /* Save previous_lmf */
452 /* Set the lowest bit to 1 to signal that this LMF has the ip field set */
453 amd64_mov_reg_membase (code, AMD64_R11, AMD64_RAX, 0, 8);
454 amd64_alu_reg_imm_size (code, X86_ADD, AMD64_R11, 1, 8);
455 amd64_mov_membase_reg (code, AMD64_RBP, lmf_offset + G_STRUCT_OFFSET (MonoLMF, previous_lmf), AMD64_R11, 8);
457 amd64_lea_membase (code, AMD64_R11, AMD64_RBP, lmf_offset);
458 amd64_mov_membase_reg (code, AMD64_RAX, 0, AMD64_R11, 8);
462 /* Arg1 is the pointer to the saved registers */
463 amd64_lea_membase (code, AMD64_ARG_REG1, AMD64_RBP, saved_regs_offset);
465 /* Arg2 is the address of the calling code */
467 amd64_mov_reg_membase (code, AMD64_ARG_REG2, AMD64_RBP, 8, 8);
469 amd64_mov_reg_imm (code, AMD64_ARG_REG2, 0);
471 /* Arg3 is the method/vtable ptr */
472 amd64_mov_reg_membase (code, AMD64_ARG_REG3, AMD64_RBP, arg_offset, 8);
474 /* Arg4 is the trampoline address */
475 amd64_mov_reg_membase (code, AMD64_ARG_REG4, AMD64_RBP, tramp_offset, 8);
478 char *icall_name = g_strdup_printf ("trampoline_func_%d", tramp_type);
479 *ji = mono_patch_info_list_prepend (*ji, code - buf, MONO_PATCH_INFO_JIT_ICALL_ADDR, icall_name);
480 amd64_mov_reg_membase (code, AMD64_RAX, AMD64_RIP, 0, 8);
482 tramp = (guint8*)mono_get_trampoline_func (tramp_type);
483 amd64_mov_reg_imm (code, AMD64_RAX, tramp);
485 amd64_call_reg (code, AMD64_RAX);
487 /* Check for thread interruption */
488 /* This is not perf critical code so no need to check the interrupt flag */
490 * Have to call the _force_ variant, since there could be a protected wrapper on the top of the stack.
492 amd64_mov_membase_reg (code, AMD64_RBP, res_offset, AMD64_RAX, 8);
494 *ji = mono_patch_info_list_prepend (*ji, code - buf, MONO_PATCH_INFO_JIT_ICALL_ADDR, "mono_thread_force_interruption_checkpoint");
495 amd64_mov_reg_membase (code, AMD64_RAX, AMD64_RIP, 0, 8);
497 amd64_mov_reg_imm (code, AMD64_RAX, (guint8*)mono_thread_force_interruption_checkpoint);
499 amd64_call_reg (code, AMD64_RAX);
500 amd64_mov_reg_membase (code, AMD64_RAX, AMD64_RBP, res_offset, 8);
504 amd64_mov_reg_membase (code, AMD64_RCX, AMD64_RBP, lmf_offset + G_STRUCT_OFFSET (MonoLMF, previous_lmf), 8);
505 amd64_alu_reg_imm_size (code, X86_SUB, AMD64_RCX, 1, 8);
506 amd64_mov_reg_membase (code, AMD64_R11, AMD64_RBP, lmf_offset + G_STRUCT_OFFSET (MonoLMF, lmf_addr), 8);
507 amd64_mov_membase_reg (code, AMD64_R11, 0, AMD64_RCX, 8);
510 * Save rax to the stack, after the leave instruction, this will become part of
513 amd64_mov_membase_reg (code, AMD64_RBP, rax_offset, AMD64_RAX, 8);
515 /* Restore argument registers, r10 (needed to pass rgctx to
516 static shared generic methods), r11 (imt register for
517 interface calls), and rax (needed for direct calls to C vararg functions). */
518 for (i = 0; i < AMD64_NREG; ++i)
519 if (AMD64_IS_ARGUMENT_REG (i) || i == AMD64_R10 || i == AMD64_R11 || i == AMD64_RAX)
520 amd64_mov_reg_membase (code, i, AMD64_RBP, saved_regs_offset + (i * 8), 8);
522 for (i = 0; i < 8; ++i)
523 amd64_movsd_reg_membase (code, i, AMD64_RBP, saved_fpregs_offset + (i * 8));
528 if (MONO_TRAMPOLINE_TYPE_MUST_RETURN (tramp_type)) {
530 amd64_mov_reg_membase (code, AMD64_RAX, AMD64_RSP, rax_offset - 0x8, 8);
533 /* call the compiled method using the saved rax */
534 amd64_jump_membase (code, AMD64_RSP, rax_offset - 0x8);
537 g_assert ((code - buf) <= 538);
539 mono_arch_flush_icache (buf, code - buf);
541 *code_size = code - buf;
543 if (tramp_type == MONO_TRAMPOLINE_CLASS_INIT) {
546 /* Initialize the nullified class init trampoline used in the AOT case */
547 nullified_class_init_trampoline = mono_arch_get_nullified_class_init_trampoline (&code_len);
550 *out_unwind_ops = unwind_ops;
556 mono_arch_get_nullified_class_init_trampoline (guint32 *code_len)
560 code = buf = mono_global_codeman_reserve (16);
563 mono_arch_flush_icache (buf, code - buf);
565 *code_len = code - buf;
571 mono_arch_create_specific_trampoline (gpointer arg1, MonoTrampolineType tramp_type, MonoDomain *domain, guint32 *code_len)
573 guint8 *code, *buf, *tramp;
576 tramp = mono_get_trampoline_code (tramp_type);
578 if ((((guint64)arg1) >> 32) == 0)
583 code = buf = mono_domain_code_reserve_align (domain, size, 1);
585 amd64_call_code (code, tramp);
586 /* The trampoline code will obtain the argument from the instruction stream */
587 if ((((guint64)arg1) >> 32) == 0) {
589 *(guint32*)(code + 1) = (gint64)arg1;
593 *(guint64*)(code + 1) = (gint64)arg1;
597 g_assert ((code - buf) <= size);
602 mono_arch_flush_icache (buf, size);
608 mono_arch_create_rgctx_lazy_fetch_trampoline (guint32 slot)
613 return mono_arch_create_rgctx_lazy_fetch_trampoline_full (slot, &code_size, &ji, FALSE);
617 mono_arch_create_rgctx_lazy_fetch_trampoline_full (guint32 slot, guint32 *code_size, MonoJumpInfo **ji, gboolean aot)
621 guint8 **rgctx_null_jumps;
629 mrgctx = MONO_RGCTX_SLOT_IS_MRGCTX (slot);
630 index = MONO_RGCTX_SLOT_INDEX (slot);
632 index += sizeof (MonoMethodRuntimeGenericContext) / sizeof (gpointer);
633 for (depth = 0; ; ++depth) {
634 int size = mono_class_rgctx_get_array_size (depth, mrgctx);
636 if (index < size - 1)
641 tramp_size = 64 + 8 * depth;
643 code = buf = mono_global_codeman_reserve (tramp_size);
645 rgctx_null_jumps = g_malloc (sizeof (guint8*) * (depth + 2));
649 amd64_mov_reg_reg (code, AMD64_RAX, AMD64_ARG_REG1, 8);
651 /* load rgctx ptr from vtable */
652 amd64_mov_reg_membase (code, AMD64_RAX, AMD64_ARG_REG1, G_STRUCT_OFFSET (MonoVTable, runtime_generic_context), 8);
653 /* is the rgctx ptr null? */
654 amd64_test_reg_reg (code, AMD64_RAX, AMD64_RAX);
655 /* if yes, jump to actual trampoline */
656 rgctx_null_jumps [0] = code;
657 amd64_branch8 (code, X86_CC_Z, -1, 1);
660 for (i = 0; i < depth; ++i) {
661 /* load ptr to next array */
662 if (mrgctx && i == 0)
663 amd64_mov_reg_membase (code, AMD64_RAX, AMD64_RAX, sizeof (MonoMethodRuntimeGenericContext), 8);
665 amd64_mov_reg_membase (code, AMD64_RAX, AMD64_RAX, 0, 8);
666 /* is the ptr null? */
667 amd64_test_reg_reg (code, AMD64_RAX, AMD64_RAX);
668 /* if yes, jump to actual trampoline */
669 rgctx_null_jumps [i + 1] = code;
670 amd64_branch8 (code, X86_CC_Z, -1, 1);
674 amd64_mov_reg_membase (code, AMD64_RAX, AMD64_RAX, sizeof (gpointer) * (index + 1), 8);
675 /* is the slot null? */
676 amd64_test_reg_reg (code, AMD64_RAX, AMD64_RAX);
677 /* if yes, jump to actual trampoline */
678 rgctx_null_jumps [depth + 1] = code;
679 amd64_branch8 (code, X86_CC_Z, -1, 1);
680 /* otherwise return */
683 for (i = mrgctx ? 1 : 0; i <= depth + 1; ++i)
684 x86_patch (rgctx_null_jumps [i], code);
686 g_free (rgctx_null_jumps);
688 /* move the rgctx pointer to the VTABLE register */
689 amd64_mov_reg_reg (code, MONO_ARCH_VTABLE_REG, AMD64_ARG_REG1, 8);
692 *ji = mono_patch_info_list_prepend (*ji, code - buf, MONO_PATCH_INFO_JIT_ICALL_ADDR, g_strdup_printf ("specific_trampoline_lazy_fetch_%u", slot));
693 amd64_mov_reg_membase (code, AMD64_R11, AMD64_RIP, 0, 8);
694 amd64_jump_reg (code, AMD64_R11);
696 tramp = mono_arch_create_specific_trampoline (GUINT_TO_POINTER (slot), MONO_TRAMPOLINE_RGCTX_LAZY_FETCH, mono_get_root_domain (), NULL);
698 /* jump to the actual trampoline */
699 amd64_jump_code (code, tramp);
702 mono_arch_flush_icache (buf, code - buf);
704 g_assert (code - buf <= tramp_size);
706 *code_size = code - buf;
712 mono_arch_create_generic_class_init_trampoline (void)
717 return mono_arch_create_generic_class_init_trampoline_full (&code_size, &ji, FALSE);
721 mono_arch_create_generic_class_init_trampoline_full (guint32 *code_size, MonoJumpInfo **ji, gboolean aot)
725 static int byte_offset = -1;
726 static guint8 bitmask;
734 code = buf = mono_global_codeman_reserve (tramp_size);
737 mono_marshal_find_bitfield_offset (MonoVTable, initialized, &byte_offset, &bitmask);
739 amd64_test_membase_imm_size (code, MONO_AMD64_ARG_REG1, byte_offset, bitmask, 1);
741 amd64_branch8 (code, X86_CC_Z, -1, 1);
745 x86_patch (jump, code);
748 *ji = mono_patch_info_list_prepend (*ji, code - buf, MONO_PATCH_INFO_JIT_ICALL_ADDR, "specific_trampoline_generic_class_init");
749 amd64_mov_reg_membase (code, AMD64_R11, AMD64_RIP, 0, 8);
750 amd64_jump_reg (code, AMD64_R11);
752 tramp = mono_arch_create_specific_trampoline (NULL, MONO_TRAMPOLINE_GENERIC_CLASS_INIT, mono_get_root_domain (), NULL);
754 /* jump to the actual trampoline */
755 amd64_jump_code (code, tramp);
758 mono_arch_flush_icache (buf, code - buf);
760 g_assert (code - buf <= tramp_size);
762 *code_size = code - buf;
767 #ifdef MONO_ARCH_MONITOR_OBJECT_REG
770 mono_arch_create_monitor_enter_trampoline (void)
775 return mono_arch_create_monitor_enter_trampoline_full (&code_size, &ji, FALSE);
779 mono_arch_create_monitor_enter_trampoline_full (guint32 *code_size, MonoJumpInfo **ji, gboolean aot)
784 guint8 *jump_obj_null, *jump_sync_null, *jump_cmpxchg_failed, *jump_other_owner, *jump_tid;
786 int owner_offset, nest_offset, dummy;
790 g_assert (MONO_ARCH_MONITOR_OBJECT_REG == AMD64_RDI);
792 mono_monitor_threads_sync_members_offset (&owner_offset, &nest_offset, &dummy);
793 g_assert (MONO_THREADS_SYNC_MEMBER_SIZE (owner_offset) == sizeof (gpointer));
794 g_assert (MONO_THREADS_SYNC_MEMBER_SIZE (nest_offset) == sizeof (guint32));
795 owner_offset = MONO_THREADS_SYNC_MEMBER_OFFSET (owner_offset);
796 nest_offset = MONO_THREADS_SYNC_MEMBER_OFFSET (nest_offset);
800 code = buf = mono_global_codeman_reserve (tramp_size);
802 if (mono_thread_get_tls_offset () != -1) {
803 /* MonoObject* obj is in RDI */
805 amd64_test_reg_reg (code, AMD64_RDI, AMD64_RDI);
806 /* if yes, jump to actual trampoline */
807 jump_obj_null = code;
808 amd64_branch8 (code, X86_CC_Z, -1, 1);
810 /* load obj->synchronization to RCX */
811 amd64_mov_reg_membase (code, AMD64_RCX, AMD64_RDI, G_STRUCT_OFFSET (MonoObject, synchronisation), 8);
812 /* is synchronization null? */
813 amd64_test_reg_reg (code, AMD64_RCX, AMD64_RCX);
814 /* if yes, jump to actual trampoline */
815 jump_sync_null = code;
816 amd64_branch8 (code, X86_CC_Z, -1, 1);
818 /* load MonoThread* into RDX */
819 code = mono_amd64_emit_tls_get (code, AMD64_RDX, mono_thread_get_tls_offset ());
820 /* load TID into RDX */
821 amd64_mov_reg_membase (code, AMD64_RDX, AMD64_RDX, G_STRUCT_OFFSET (MonoThread, tid), 8);
823 /* is synchronization->owner null? */
824 amd64_alu_membase_imm_size (code, X86_CMP, AMD64_RCX, owner_offset, 0, 8);
825 /* if not, jump to next case */
827 amd64_branch8 (code, X86_CC_NZ, -1, 1);
829 /* if yes, try a compare-exchange with the TID */
831 amd64_alu_reg_reg (code, X86_XOR, AMD64_RAX, AMD64_RAX);
832 /* compare and exchange */
833 amd64_prefix (code, X86_LOCK_PREFIX);
834 amd64_cmpxchg_membase_reg_size (code, AMD64_RCX, owner_offset, AMD64_RDX, 8);
835 /* if not successful, jump to actual trampoline */
836 jump_cmpxchg_failed = code;
837 amd64_branch8 (code, X86_CC_NZ, -1, 1);
838 /* if successful, return */
841 /* next case: synchronization->owner is not null */
842 x86_patch (jump_tid, code);
843 /* is synchronization->owner == TID? */
844 amd64_alu_membase_reg_size (code, X86_CMP, AMD64_RCX, owner_offset, AMD64_RDX, 8);
845 /* if not, jump to actual trampoline */
846 jump_other_owner = code;
847 amd64_branch8 (code, X86_CC_NZ, -1, 1);
848 /* if yes, increment nest */
849 amd64_inc_membase_size (code, AMD64_RCX, nest_offset, 4);
853 x86_patch (jump_obj_null, code);
854 x86_patch (jump_sync_null, code);
855 x86_patch (jump_cmpxchg_failed, code);
856 x86_patch (jump_other_owner, code);
859 /* jump to the actual trampoline */
860 #if MONO_AMD64_ARG_REG1 != AMD64_RDI
861 amd64_mov_reg_reg (code, MONO_AMD64_ARG_REG1, AMD64_RDI);
865 *ji = mono_patch_info_list_prepend (*ji, code - buf, MONO_PATCH_INFO_JIT_ICALL_ADDR, "specific_trampoline_monitor_enter");
866 amd64_mov_reg_membase (code, AMD64_R11, AMD64_RIP, 0, 8);
867 amd64_jump_reg (code, AMD64_R11);
869 tramp = mono_arch_create_specific_trampoline (NULL, MONO_TRAMPOLINE_MONITOR_ENTER, mono_get_root_domain (), NULL);
871 /* jump to the actual trampoline */
872 amd64_jump_code (code, tramp);
875 mono_arch_flush_icache (code, code - buf);
876 g_assert (code - buf <= tramp_size);
878 *code_size = code - buf;
884 mono_arch_create_monitor_exit_trampoline (void)
889 return mono_arch_create_monitor_exit_trampoline_full (&code_size, &ji, FALSE);
893 mono_arch_create_monitor_exit_trampoline_full (guint32 *code_size, MonoJumpInfo **ji, gboolean aot)
897 guint8 *jump_obj_null, *jump_have_waiters;
900 int owner_offset, nest_offset, entry_count_offset;
904 g_assert (MONO_ARCH_MONITOR_OBJECT_REG == AMD64_RDI);
906 mono_monitor_threads_sync_members_offset (&owner_offset, &nest_offset, &entry_count_offset);
907 g_assert (MONO_THREADS_SYNC_MEMBER_SIZE (owner_offset) == sizeof (gpointer));
908 g_assert (MONO_THREADS_SYNC_MEMBER_SIZE (nest_offset) == sizeof (guint32));
909 g_assert (MONO_THREADS_SYNC_MEMBER_SIZE (entry_count_offset) == sizeof (gint32));
910 owner_offset = MONO_THREADS_SYNC_MEMBER_OFFSET (owner_offset);
911 nest_offset = MONO_THREADS_SYNC_MEMBER_OFFSET (nest_offset);
912 entry_count_offset = MONO_THREADS_SYNC_MEMBER_OFFSET (entry_count_offset);
916 code = buf = mono_global_codeman_reserve (tramp_size);
918 if (mono_thread_get_tls_offset () != -1) {
919 /* MonoObject* obj is in RDI */
921 amd64_test_reg_reg (code, AMD64_RDI, AMD64_RDI);
922 /* if yes, jump to actual trampoline */
923 jump_obj_null = code;
924 amd64_branch8 (code, X86_CC_Z, -1, 1);
926 /* load obj->synchronization to RCX */
927 amd64_mov_reg_membase (code, AMD64_RCX, AMD64_RDI, G_STRUCT_OFFSET (MonoObject, synchronisation), 8);
928 /* is synchronization null? */
929 amd64_test_reg_reg (code, AMD64_RCX, AMD64_RCX);
930 /* if not, jump to next case */
932 amd64_branch8 (code, X86_CC_NZ, -1, 1);
933 /* if yes, just return */
936 /* next case: synchronization is not null */
937 x86_patch (jump_next, code);
938 /* load MonoThread* into RDX */
939 code = mono_amd64_emit_tls_get (code, AMD64_RDX, mono_thread_get_tls_offset ());
940 /* load TID into RDX */
941 amd64_mov_reg_membase (code, AMD64_RDX, AMD64_RDX, G_STRUCT_OFFSET (MonoThread, tid), 8);
942 /* is synchronization->owner == TID */
943 amd64_alu_membase_reg_size (code, X86_CMP, AMD64_RCX, owner_offset, AMD64_RDX, 8);
944 /* if yes, jump to next case */
946 amd64_branch8 (code, X86_CC_Z, -1, 1);
947 /* if not, just return */
950 /* next case: synchronization->owner == TID */
951 x86_patch (jump_next, code);
952 /* is synchronization->nest == 1 */
953 amd64_alu_membase_imm_size (code, X86_CMP, AMD64_RCX, nest_offset, 1, 4);
954 /* if not, jump to next case */
956 amd64_branch8 (code, X86_CC_NZ, -1, 1);
957 /* if yes, is synchronization->entry_count zero? */
958 amd64_alu_membase_imm_size (code, X86_CMP, AMD64_RCX, entry_count_offset, 0, 4);
959 /* if not, jump to actual trampoline */
960 jump_have_waiters = code;
961 amd64_branch8 (code, X86_CC_NZ, -1 , 1);
962 /* if yes, set synchronization->owner to null and return */
963 amd64_mov_membase_imm (code, AMD64_RCX, owner_offset, 0, 8);
966 /* next case: synchronization->nest is not 1 */
967 x86_patch (jump_next, code);
968 /* decrease synchronization->nest and return */
969 amd64_dec_membase_size (code, AMD64_RCX, nest_offset, 4);
972 x86_patch (jump_obj_null, code);
973 x86_patch (jump_have_waiters, code);
976 /* jump to the actual trampoline */
977 #if MONO_AMD64_ARG_REG1 != AMD64_RDI
978 amd64_mov_reg_reg (code, MONO_AMD64_ARG_REG1, AMD64_RDI);
982 *ji = mono_patch_info_list_prepend (*ji, code - buf, MONO_PATCH_INFO_JIT_ICALL_ADDR, "specific_trampoline_monitor_exit");
983 amd64_mov_reg_membase (code, AMD64_R11, AMD64_RIP, 0, 8);
984 amd64_jump_reg (code, AMD64_R11);
986 tramp = mono_arch_create_specific_trampoline (NULL, MONO_TRAMPOLINE_MONITOR_EXIT, mono_get_root_domain (), NULL);
987 amd64_jump_code (code, tramp);
990 mono_arch_flush_icache (code, code - buf);
991 g_assert (code - buf <= tramp_size);
993 *code_size = code - buf;
1000 mono_arch_invalidate_method (MonoJitInfo *ji, void *func, gpointer func_arg)
1002 /* FIXME: This is not thread safe */
1003 guint8 *code = ji->code_start;
1005 amd64_mov_reg_imm (code, AMD64_ARG_REG1, func_arg);
1006 amd64_mov_reg_imm (code, AMD64_R11, func);
1008 x86_push_imm (code, (guint64)func_arg);
1009 amd64_call_reg (code, AMD64_R11);