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 #include <mono/utils/memcheck.h>
24 #include "mini-amd64.h"
26 #define IS_REX(inst) (((inst) >= 0x40) && ((inst) <= 0x4f))
28 static guint8* nullified_class_init_trampoline;
31 * mono_arch_get_unbox_trampoline:
32 * @gsctx: the generic sharing context
34 * @addr: pointer to native code for @m
36 * when value type methods are called through the vtable we need to unbox the
37 * this argument. This method returns a pointer to a trampoline which does
38 * unboxing before calling the method
41 mono_arch_get_unbox_trampoline (MonoGenericSharingContext *gsctx, MonoMethod *m, gpointer addr)
46 MonoDomain *domain = mono_domain_get ();
48 this_reg = mono_arch_get_this_arg_reg (mono_method_signature (m), gsctx, NULL);
50 start = code = mono_domain_code_reserve (domain, 20);
52 amd64_alu_reg_imm (code, X86_ADD, this_reg, sizeof (MonoObject));
53 /* FIXME: Optimize this */
54 amd64_mov_reg_imm (code, AMD64_RAX, addr);
55 amd64_jump_reg (code, AMD64_RAX);
56 g_assert ((code - start) < 20);
58 mono_arch_flush_icache (start, code - start);
64 * mono_arch_get_static_rgctx_trampoline:
66 * Create a trampoline which sets RGCTX_REG to MRGCTX, then jumps to ADDR.
69 mono_arch_get_static_rgctx_trampoline (MonoMethod *m, MonoMethodRuntimeGenericContext *mrgctx, gpointer addr)
74 MonoDomain *domain = mono_domain_get ();
76 #ifdef MONO_ARCH_NOMAP32BIT
79 /* AOTed code could still have a non-32 bit address */
80 if ((((guint64)addr) >> 32) == 0)
86 start = code = mono_domain_code_reserve (domain, buf_len);
88 amd64_mov_reg_imm (code, MONO_ARCH_RGCTX_REG, mrgctx);
89 amd64_jump_code (code, addr);
90 g_assert ((code - start) < buf_len);
92 mono_arch_flush_icache (start, code - start);
98 mono_arch_get_llvm_imt_trampoline (MonoDomain *domain, MonoMethod *m, int vt_offset)
100 guint8 *code, *start;
106 start = code = mono_domain_code_reserve (domain, buf_len);
108 this_reg = mono_arch_get_this_arg_reg (mono_method_signature (m), NULL, NULL);
111 amd64_mov_reg_imm (code, MONO_ARCH_IMT_REG, m);
112 /* Load vtable address */
113 amd64_mov_reg_membase (code, AMD64_RAX, this_reg, 0, 8);
114 amd64_jump_membase (code, AMD64_RAX, vt_offset);
117 g_assert ((code - start) < buf_len);
119 mono_arch_flush_icache (start, code - start);
125 * mono_arch_patch_callsite:
127 * Patch the callsite whose address is given by ORIG_CODE so it calls ADDR. ORIG_CODE
128 * points to the pc right after the call.
131 mono_arch_patch_callsite (guint8 *method_start, guint8 *orig_code, guint8 *addr)
135 gboolean can_write = mono_breakpoint_clean_code (method_start, orig_code, 14, buf, sizeof (buf));
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 if ((((guint64)(addr)) >> 32) != 0) {
147 #ifdef MONO_ARCH_NOMAP32BIT
148 /* Print some diagnostics */
149 MonoJitInfo *ji = mono_jit_info_table_find (mono_domain_get (), (char*)orig_code);
151 fprintf (stderr, "At %s, offset 0x%zx\n", mono_method_full_name (ji->method, TRUE), (guint8*)orig_code - (guint8*)ji->code_start);
152 fprintf (stderr, "Addr: %p\n", addr);
153 ji = mono_jit_info_table_find (mono_domain_get (), (char*)addr);
155 fprintf (stderr, "Callee: %s\n", mono_method_full_name (ji->method, TRUE));
156 g_assert_not_reached ();
159 * This might happen when calling AOTed code. Create a thunk.
161 guint8 *thunk_start, *thunk_code;
163 thunk_start = thunk_code = mono_domain_code_reserve (mono_domain_get (), 32);
164 amd64_jump_membase (thunk_code, AMD64_RIP, 0);
165 *(guint64*)thunk_code = (guint64)addr;
167 g_assert ((((guint64)(addr)) >> 32) == 0);
168 mono_arch_flush_icache (thunk_start, thunk_code - thunk_start);
171 g_assert ((((guint64)(orig_code)) >> 32) == 0);
173 InterlockedExchange ((gint32*)(orig_code - 4), ((gint64)addr - (gint64)orig_code));
174 VALGRIND_DISCARD_TRANSLATIONS (orig_code - 5, 4);
178 else if ((code [-7] == 0x41) && (code [-6] == 0xff) && (code [-5] == 0x15)) {
179 /* call *<OFFSET>(%rip) */
180 gpointer *got_entry = (gpointer*)((guint8*)orig_code + (*(guint32*)(orig_code - 4)));
182 InterlockedExchangePointer (got_entry, addr);
183 VALGRIND_DISCARD_TRANSLATIONS (orig_code - 5, sizeof (gpointer));
189 mono_arch_patch_plt_entry (guint8 *code, gpointer *got, mgreg_t *regs, guint8 *addr)
192 gpointer *plt_jump_table_entry;
194 /* A PLT entry: jmp *<DISP>(%rip) */
195 g_assert (code [0] == 0xff);
196 g_assert (code [1] == 0x25);
198 disp = *(gint32*)(code + 2);
200 plt_jump_table_entry = (gpointer*)(code + 6 + disp);
202 InterlockedExchangePointer (plt_jump_table_entry, addr);
206 mono_arch_nullify_class_init_trampoline (guint8 *code, mgreg_t *regs)
209 MonoJitInfo *ji = NULL;
213 /* code - 7 might be before the start of the method */
214 /* FIXME: Avoid this expensive call somehow */
215 ji = mono_jit_info_table_find (mono_domain_get (), (char*)code);
218 can_write = mono_breakpoint_clean_code (ji ? ji->code_start : NULL, code, 7, buf, sizeof (buf));
224 * A given byte sequence can match more than case here, so we have to be
225 * really careful about the ordering of the cases. Longer sequences
228 if ((buf [0] == 0x41) && (buf [1] == 0xff) && (buf [2] == 0x15)) {
229 gpointer *vtable_slot;
231 /* call *<OFFSET>(%rip) */
232 vtable_slot = mono_get_vcall_slot_addr (code, regs);
233 g_assert (vtable_slot);
235 *vtable_slot = nullified_class_init_trampoline;
236 } else if (buf [2] == 0xe8) {
238 //guint8 *buf = code - 2;
241 * It would be better to replace the call with nops, but that doesn't seem
242 * to work on SMP machines even when the whole call is inside a cache line.
243 * Patching the call address seems to work.
253 mono_arch_patch_callsite (code - 5, code, nullified_class_init_trampoline);
254 } else if ((buf [5] == 0xff) && x86_modrm_mod (buf [6]) == 3 && x86_modrm_reg (buf [6]) == 2) {
256 /* Generated by the LLVM JIT or on platforms without MAP_32BIT set */
257 mono_arch_patch_callsite (code - 13, code, nullified_class_init_trampoline);
258 } else if (buf [4] == 0x90 || buf [5] == 0xeb || buf [6] == 0x66) {
259 /* Already changed by another thread */
262 printf ("Invalid trampoline sequence: %x %x %x %x %x %x %x\n", buf [0], buf [1], buf [2], buf [3],
263 buf [4], buf [5], buf [6]);
264 g_assert_not_reached ();
269 mono_arch_nullify_plt_entry (guint8 *code, mgreg_t *regs)
271 if (mono_aot_only && !nullified_class_init_trampoline)
272 nullified_class_init_trampoline = mono_aot_get_trampoline ("nullified_class_init_trampoline");
274 mono_arch_patch_plt_entry (code, NULL, regs, nullified_class_init_trampoline);
278 mono_arch_create_generic_trampoline (MonoTrampolineType tramp_type, MonoTrampInfo **info, gboolean aot)
280 guint8 *buf, *code, *tramp, *br [2], *r11_save_code, *after_r11_save_code;
281 int i, lmf_offset, offset, res_offset, arg_offset, rax_offset, tramp_offset;
282 int buf_len, saved_regs_offset;
283 int saved_fpregs_offset, rbp_offset, framesize, orig_rsp_to_rbp_offset, cfa_offset;
285 GSList *unwind_ops = NULL;
286 MonoJumpInfo *ji = NULL;
288 if (tramp_type == MONO_TRAMPOLINE_JUMP)
294 code = buf = mono_global_codeman_reserve (buf_len);
296 framesize = 538 + sizeof (MonoLMF);
297 framesize = (framesize + (MONO_ARCH_FRAME_ALIGNMENT - 1)) & ~ (MONO_ARCH_FRAME_ALIGNMENT - 1);
299 orig_rsp_to_rbp_offset = 0;
300 r11_save_code = code;
301 /* Reserve 5 bytes for the mov_membase_reg to save R11 */
303 after_r11_save_code = code;
305 // CFA = sp + 16 (the trampoline address is on the stack)
307 mono_add_unwind_op_def_cfa (unwind_ops, code, buf, AMD64_RSP, 16);
308 // IP saved at CFA - 8
309 mono_add_unwind_op_offset (unwind_ops, code, buf, AMD64_RIP, -8);
311 /* Pop the return address off the stack */
312 amd64_pop_reg (code, AMD64_R11);
313 orig_rsp_to_rbp_offset += 8;
316 mono_add_unwind_op_def_cfa_offset (unwind_ops, code, buf, cfa_offset);
319 * Allocate a new stack frame
321 amd64_push_reg (code, AMD64_RBP);
323 mono_add_unwind_op_def_cfa_offset (unwind_ops, code, buf, cfa_offset);
324 mono_add_unwind_op_offset (unwind_ops, code, buf, AMD64_RBP, - cfa_offset);
326 orig_rsp_to_rbp_offset -= 8;
327 amd64_mov_reg_reg (code, AMD64_RBP, AMD64_RSP, 8);
328 mono_add_unwind_op_def_cfa_reg (unwind_ops, code, buf, AMD64_RBP);
329 amd64_alu_reg_imm (code, X86_SUB, AMD64_RSP, framesize);
332 rbp_offset = - offset;
335 rax_offset = - offset;
338 tramp_offset = - offset;
341 arg_offset = - offset;
343 /* Compute the trampoline address from the return address */
345 /* 7 = length of call *<offset>(rip) */
346 amd64_alu_reg_imm (code, X86_SUB, AMD64_R11, 7);
348 /* 5 = length of amd64_call_membase () */
349 amd64_alu_reg_imm (code, X86_SUB, AMD64_R11, 5);
351 amd64_mov_membase_reg (code, AMD64_RBP, tramp_offset, AMD64_R11, 8);
354 res_offset = - offset;
356 /* Save all registers */
358 offset += AMD64_NREG * 8;
359 saved_regs_offset = - offset;
360 for (i = 0; i < AMD64_NREG; ++i) {
361 if (i == AMD64_RBP) {
362 /* RAX is already saved */
363 amd64_mov_reg_membase (code, AMD64_RAX, AMD64_RBP, rbp_offset, 8);
364 amd64_mov_membase_reg (code, AMD64_RBP, saved_regs_offset + (i * 8), AMD64_RAX, 8);
365 } else if (i != AMD64_R11) {
366 amd64_mov_membase_reg (code, AMD64_RBP, saved_regs_offset + (i * 8), i, 8);
368 /* We have to save R11 right at the start of
369 the trampoline code because it's used as a
371 amd64_mov_membase_reg (r11_save_code, AMD64_RSP, saved_regs_offset + orig_rsp_to_rbp_offset + (i * 8), i, 8);
372 g_assert (r11_save_code == after_r11_save_code);
376 saved_fpregs_offset = - offset;
377 for (i = 0; i < 8; ++i)
378 amd64_movsd_membase_reg (code, AMD64_RBP, saved_fpregs_offset + (i * 8), i);
380 if (tramp_type != MONO_TRAMPOLINE_GENERIC_CLASS_INIT &&
381 tramp_type != MONO_TRAMPOLINE_MONITOR_ENTER &&
382 tramp_type != MONO_TRAMPOLINE_MONITOR_EXIT) {
383 /* Obtain the trampoline argument which is encoded in the instruction stream */
385 /* Load the GOT offset */
386 amd64_mov_reg_membase (code, AMD64_R11, AMD64_RBP, tramp_offset, 8);
387 amd64_mov_reg_membase (code, AMD64_RAX, AMD64_R11, 7, 4);
388 /* Compute the address of the GOT slot */
389 amd64_alu_reg_reg_size (code, X86_ADD, AMD64_R11, AMD64_RAX, 8);
391 amd64_mov_reg_membase (code, AMD64_R11, AMD64_R11, 0, 8);
393 amd64_mov_reg_membase (code, AMD64_R11, AMD64_RBP, tramp_offset, 8);
394 amd64_mov_reg_membase (code, AMD64_RAX, AMD64_R11, 5, 1);
395 amd64_widen_reg (code, AMD64_RAX, AMD64_RAX, TRUE, FALSE);
396 amd64_alu_reg_imm_size (code, X86_CMP, AMD64_RAX, 4, 1);
398 x86_branch8 (code, X86_CC_NE, 6, FALSE);
399 /* 32 bit immediate */
400 amd64_mov_reg_membase (code, AMD64_R11, AMD64_R11, 6, 4);
402 x86_jump8 (code, 10);
403 /* 64 bit immediate */
404 mono_amd64_patch (br [0], code);
405 amd64_mov_reg_membase (code, AMD64_R11, AMD64_R11, 6, 8);
406 mono_amd64_patch (br [1], code);
408 amd64_mov_membase_reg (code, AMD64_RBP, arg_offset, AMD64_R11, 8);
410 amd64_mov_reg_membase (code, AMD64_R11, AMD64_RBP, saved_regs_offset + (MONO_AMD64_ARG_REG1 * 8), 8);
411 amd64_mov_membase_reg (code, AMD64_RBP, arg_offset, AMD64_R11, 8);
416 offset += sizeof (MonoLMF);
417 lmf_offset = - offset;
421 amd64_mov_reg_membase (code, AMD64_R11, AMD64_RBP, 8, 8);
423 amd64_mov_reg_imm (code, AMD64_R11, 0);
424 amd64_mov_membase_reg (code, AMD64_RBP, lmf_offset + G_STRUCT_OFFSET (MonoLMF, rip), AMD64_R11, 8);
426 amd64_mov_reg_membase (code, AMD64_R11, AMD64_RSP, framesize, 8);
427 amd64_mov_membase_reg (code, AMD64_RBP, lmf_offset + G_STRUCT_OFFSET (MonoLMF, rbp), AMD64_R11, 8);
429 amd64_mov_reg_reg (code, AMD64_R11, AMD64_RSP, 8);
430 amd64_alu_reg_imm (code, X86_ADD, AMD64_R11, framesize + 16);
431 amd64_mov_membase_reg (code, AMD64_RBP, lmf_offset + G_STRUCT_OFFSET (MonoLMF, rsp), AMD64_R11, 8);
433 if (tramp_type == MONO_TRAMPOLINE_JIT || tramp_type == MONO_TRAMPOLINE_JUMP) {
434 amd64_mov_reg_membase (code, AMD64_R11, AMD64_RBP, arg_offset, 8);
435 amd64_mov_membase_reg (code, AMD64_RBP, lmf_offset + G_STRUCT_OFFSET (MonoLMF, method), AMD64_R11, 8);
437 amd64_mov_membase_imm (code, AMD64_RBP, lmf_offset + G_STRUCT_OFFSET (MonoLMF, method), 0, 8);
439 /* Save callee saved regs */
441 amd64_mov_membase_reg (code, AMD64_RBP, lmf_offset + G_STRUCT_OFFSET (MonoLMF, rdi), AMD64_RDI, 8);
442 amd64_mov_membase_reg (code, AMD64_RBP, lmf_offset + G_STRUCT_OFFSET (MonoLMF, rsi), AMD64_RSI, 8);
444 amd64_mov_membase_reg (code, AMD64_RBP, lmf_offset + G_STRUCT_OFFSET (MonoLMF, rbx), AMD64_RBX, 8);
445 amd64_mov_membase_reg (code, AMD64_RBP, lmf_offset + G_STRUCT_OFFSET (MonoLMF, r12), AMD64_R12, 8);
446 amd64_mov_membase_reg (code, AMD64_RBP, lmf_offset + G_STRUCT_OFFSET (MonoLMF, r13), AMD64_R13, 8);
447 amd64_mov_membase_reg (code, AMD64_RBP, lmf_offset + G_STRUCT_OFFSET (MonoLMF, r14), AMD64_R14, 8);
448 amd64_mov_membase_reg (code, AMD64_RBP, lmf_offset + G_STRUCT_OFFSET (MonoLMF, r15), AMD64_R15, 8);
451 code = mono_arch_emit_load_aotconst (buf, code, &ji, MONO_PATCH_INFO_JIT_ICALL_ADDR, "mono_get_lmf_addr");
453 amd64_mov_reg_imm (code, AMD64_R11, mono_get_lmf_addr);
455 amd64_call_reg (code, AMD64_R11);
458 amd64_mov_membase_reg (code, AMD64_RBP, lmf_offset + G_STRUCT_OFFSET (MonoLMF, lmf_addr), AMD64_RAX, 8);
459 /* Save previous_lmf */
460 /* Set the lowest bit to 1 to signal that this LMF has the ip field set */
461 amd64_mov_reg_membase (code, AMD64_R11, AMD64_RAX, 0, 8);
462 amd64_alu_reg_imm_size (code, X86_ADD, AMD64_R11, 1, 8);
463 amd64_mov_membase_reg (code, AMD64_RBP, lmf_offset + G_STRUCT_OFFSET (MonoLMF, previous_lmf), AMD64_R11, 8);
465 amd64_lea_membase (code, AMD64_R11, AMD64_RBP, lmf_offset);
466 amd64_mov_membase_reg (code, AMD64_RAX, 0, AMD64_R11, 8);
470 /* Arg1 is the pointer to the saved registers */
471 amd64_lea_membase (code, AMD64_ARG_REG1, AMD64_RBP, saved_regs_offset);
473 /* Arg2 is the address of the calling code */
475 amd64_mov_reg_membase (code, AMD64_ARG_REG2, AMD64_RBP, 8, 8);
477 amd64_mov_reg_imm (code, AMD64_ARG_REG2, 0);
479 /* Arg3 is the method/vtable ptr */
480 amd64_mov_reg_membase (code, AMD64_ARG_REG3, AMD64_RBP, arg_offset, 8);
482 /* Arg4 is the trampoline address */
483 amd64_mov_reg_membase (code, AMD64_ARG_REG4, AMD64_RBP, tramp_offset, 8);
486 char *icall_name = g_strdup_printf ("trampoline_func_%d", tramp_type);
487 code = mono_arch_emit_load_aotconst (buf, code, &ji, MONO_PATCH_INFO_JIT_ICALL_ADDR, icall_name);
489 tramp = (guint8*)mono_get_trampoline_func (tramp_type);
490 amd64_mov_reg_imm (code, AMD64_R11, tramp);
492 amd64_call_reg (code, AMD64_R11);
494 /* Check for thread interruption */
495 /* This is not perf critical code so no need to check the interrupt flag */
497 * Have to call the _force_ variant, since there could be a protected wrapper on the top of the stack.
499 amd64_mov_membase_reg (code, AMD64_RBP, res_offset, AMD64_RAX, 8);
501 code = mono_arch_emit_load_aotconst (buf, code, &ji, MONO_PATCH_INFO_JIT_ICALL_ADDR, "mono_thread_force_interruption_checkpoint");
503 amd64_mov_reg_imm (code, AMD64_R11, (guint8*)mono_thread_force_interruption_checkpoint);
505 amd64_call_reg (code, AMD64_R11);
507 amd64_mov_reg_membase (code, AMD64_RAX, AMD64_RBP, res_offset, 8);
511 amd64_mov_reg_membase (code, AMD64_RCX, AMD64_RBP, lmf_offset + G_STRUCT_OFFSET (MonoLMF, previous_lmf), 8);
512 amd64_alu_reg_imm_size (code, X86_SUB, AMD64_RCX, 1, 8);
513 amd64_mov_reg_membase (code, AMD64_R11, AMD64_RBP, lmf_offset + G_STRUCT_OFFSET (MonoLMF, lmf_addr), 8);
514 amd64_mov_membase_reg (code, AMD64_R11, 0, AMD64_RCX, 8);
517 * Save rax to the stack, after the leave instruction, this will become part of
520 amd64_mov_membase_reg (code, AMD64_RBP, rax_offset, AMD64_RAX, 8);
522 /* Restore argument registers, r10 (needed to pass rgctx to
523 static shared generic methods), r11 (imt register for
524 interface calls), and rax (needed for direct calls to C vararg functions). */
525 for (i = 0; i < AMD64_NREG; ++i)
526 if (AMD64_IS_ARGUMENT_REG (i) || i == AMD64_R10 || i == AMD64_R11 || i == AMD64_RAX)
527 amd64_mov_reg_membase (code, i, AMD64_RBP, saved_regs_offset + (i * 8), 8);
529 for (i = 0; i < 8; ++i)
530 amd64_movsd_reg_membase (code, i, AMD64_RBP, saved_fpregs_offset + (i * 8));
535 if (MONO_TRAMPOLINE_TYPE_MUST_RETURN (tramp_type)) {
537 amd64_mov_reg_membase (code, AMD64_RAX, AMD64_RSP, rax_offset - 0x8, 8);
540 /* call the compiled method using the saved rax */
541 amd64_jump_membase (code, AMD64_RSP, rax_offset - 0x8);
544 g_assert ((code - buf) <= buf_len);
546 mono_arch_flush_icache (buf, code - buf);
548 if (tramp_type == MONO_TRAMPOLINE_CLASS_INIT) {
549 /* Initialize the nullified class init trampoline used in the AOT case */
550 nullified_class_init_trampoline = mono_arch_get_nullified_class_init_trampoline (NULL);
554 *info = mono_tramp_info_create (g_strdup_printf ("generic_trampoline_%d", tramp_type), buf, code - buf, ji, unwind_ops);
560 mono_arch_get_nullified_class_init_trampoline (MonoTrampInfo **info)
564 code = buf = mono_global_codeman_reserve (16);
567 mono_arch_flush_icache (buf, code - buf);
570 *info = mono_tramp_info_create (g_strdup_printf ("nullified_class_init_trampoline"), buf, code - buf, NULL, NULL);
576 mono_arch_create_specific_trampoline (gpointer arg1, MonoTrampolineType tramp_type, MonoDomain *domain, guint32 *code_len)
578 guint8 *code, *buf, *tramp;
581 tramp = mono_get_trampoline_code (tramp_type);
583 if ((((guint64)arg1) >> 32) == 0)
588 code = buf = mono_domain_code_reserve_align (domain, size, 1);
590 amd64_call_code (code, tramp);
591 /* The trampoline code will obtain the argument from the instruction stream */
592 if ((((guint64)arg1) >> 32) == 0) {
594 *(guint32*)(code + 1) = (gint64)arg1;
598 *(guint64*)(code + 1) = (gint64)arg1;
602 g_assert ((code - buf) <= size);
607 mono_arch_flush_icache (buf, size);
613 mono_arch_create_rgctx_lazy_fetch_trampoline (guint32 slot, MonoTrampInfo **info, gboolean aot)
617 guint8 **rgctx_null_jumps;
622 MonoJumpInfo *ji = NULL;
623 GSList *unwind_ops = NULL;
626 mrgctx = MONO_RGCTX_SLOT_IS_MRGCTX (slot);
627 index = MONO_RGCTX_SLOT_INDEX (slot);
629 index += MONO_SIZEOF_METHOD_RUNTIME_GENERIC_CONTEXT / sizeof (gpointer);
630 for (depth = 0; ; ++depth) {
631 int size = mono_class_rgctx_get_array_size (depth, mrgctx);
633 if (index < size - 1)
638 tramp_size = 64 + 8 * depth;
640 code = buf = mono_global_codeman_reserve (tramp_size);
642 unwind_ops = mono_arch_get_cie_program ();
644 rgctx_null_jumps = g_malloc (sizeof (guint8*) * (depth + 2));
648 amd64_mov_reg_reg (code, AMD64_RAX, AMD64_ARG_REG1, 8);
650 /* load rgctx ptr from vtable */
651 amd64_mov_reg_membase (code, AMD64_RAX, AMD64_ARG_REG1, G_STRUCT_OFFSET (MonoVTable, runtime_generic_context), 8);
652 /* is the rgctx ptr null? */
653 amd64_test_reg_reg (code, AMD64_RAX, AMD64_RAX);
654 /* if yes, jump to actual trampoline */
655 rgctx_null_jumps [0] = code;
656 amd64_branch8 (code, X86_CC_Z, -1, 1);
659 for (i = 0; i < depth; ++i) {
660 /* load ptr to next array */
661 if (mrgctx && i == 0)
662 amd64_mov_reg_membase (code, AMD64_RAX, AMD64_RAX, MONO_SIZEOF_METHOD_RUNTIME_GENERIC_CONTEXT, 8);
664 amd64_mov_reg_membase (code, AMD64_RAX, AMD64_RAX, 0, 8);
665 /* is the ptr null? */
666 amd64_test_reg_reg (code, AMD64_RAX, AMD64_RAX);
667 /* if yes, jump to actual trampoline */
668 rgctx_null_jumps [i + 1] = code;
669 amd64_branch8 (code, X86_CC_Z, -1, 1);
673 amd64_mov_reg_membase (code, AMD64_RAX, AMD64_RAX, sizeof (gpointer) * (index + 1), 8);
674 /* is the slot null? */
675 amd64_test_reg_reg (code, AMD64_RAX, AMD64_RAX);
676 /* if yes, jump to actual trampoline */
677 rgctx_null_jumps [depth + 1] = code;
678 amd64_branch8 (code, X86_CC_Z, -1, 1);
679 /* otherwise return */
682 for (i = mrgctx ? 1 : 0; i <= depth + 1; ++i)
683 x86_patch (rgctx_null_jumps [i], code);
685 g_free (rgctx_null_jumps);
687 /* move the rgctx pointer to the VTABLE register */
688 amd64_mov_reg_reg (code, MONO_ARCH_VTABLE_REG, AMD64_ARG_REG1, 8);
691 code = mono_arch_emit_load_aotconst (buf, code, &ji, MONO_PATCH_INFO_JIT_ICALL_ADDR, g_strdup_printf ("specific_trampoline_lazy_fetch_%u", slot));
692 amd64_jump_reg (code, AMD64_R11);
694 tramp = mono_arch_create_specific_trampoline (GUINT_TO_POINTER (slot), MONO_TRAMPOLINE_RGCTX_LAZY_FETCH, mono_get_root_domain (), NULL);
696 /* jump to the actual trampoline */
697 amd64_jump_code (code, tramp);
700 mono_arch_flush_icache (buf, code - buf);
702 g_assert (code - buf <= tramp_size);
704 name = g_strdup_printf ("rgctx_fetch_trampoline_%s_%d", mrgctx ? "mrgctx" : "rgctx", index);
705 mono_save_trampoline_xdebug_info (name, buf, code - buf, unwind_ops);
709 *info = mono_tramp_info_create (g_strdup_printf ("rgctx_fetch_trampoline_%u", slot), buf, code - buf, ji, unwind_ops);
715 mono_arch_create_generic_class_init_trampoline (MonoTrampInfo **info, gboolean aot)
719 static int byte_offset = -1;
720 static guint8 bitmask;
723 GSList *unwind_ops = NULL;
724 MonoJumpInfo *ji = NULL;
728 code = buf = mono_global_codeman_reserve (tramp_size);
731 mono_marshal_find_bitfield_offset (MonoVTable, initialized, &byte_offset, &bitmask);
733 amd64_test_membase_imm_size (code, MONO_AMD64_ARG_REG1, byte_offset, bitmask, 1);
735 amd64_branch8 (code, X86_CC_Z, -1, 1);
739 x86_patch (jump, code);
742 code = mono_arch_emit_load_aotconst (buf, code, &ji, MONO_PATCH_INFO_JIT_ICALL_ADDR, "specific_trampoline_generic_class_init");
743 amd64_jump_reg (code, AMD64_R11);
745 tramp = mono_arch_create_specific_trampoline (NULL, MONO_TRAMPOLINE_GENERIC_CLASS_INIT, mono_get_root_domain (), NULL);
747 /* jump to the actual trampoline */
748 amd64_jump_code (code, tramp);
751 mono_arch_flush_icache (buf, code - buf);
753 g_assert (code - buf <= tramp_size);
756 *info = mono_tramp_info_create (g_strdup_printf ("generic_class_init_trampoline"), buf, code - buf, ji, unwind_ops);
761 #ifdef MONO_ARCH_MONITOR_OBJECT_REG
764 mono_arch_create_monitor_enter_trampoline (MonoTrampInfo **info, gboolean aot)
768 guint8 *jump_obj_null, *jump_sync_null, *jump_cmpxchg_failed, *jump_other_owner, *jump_tid;
770 int owner_offset, nest_offset, dummy;
771 MonoJumpInfo *ji = NULL;
772 GSList *unwind_ops = NULL;
774 g_assert (MONO_ARCH_MONITOR_OBJECT_REG == AMD64_RDI);
776 mono_monitor_threads_sync_members_offset (&owner_offset, &nest_offset, &dummy);
777 g_assert (MONO_THREADS_SYNC_MEMBER_SIZE (owner_offset) == sizeof (gpointer));
778 g_assert (MONO_THREADS_SYNC_MEMBER_SIZE (nest_offset) == sizeof (guint32));
779 owner_offset = MONO_THREADS_SYNC_MEMBER_OFFSET (owner_offset);
780 nest_offset = MONO_THREADS_SYNC_MEMBER_OFFSET (nest_offset);
784 code = buf = mono_global_codeman_reserve (tramp_size);
786 if (mono_thread_get_tls_offset () != -1) {
787 /* MonoObject* obj is in RDI */
789 amd64_test_reg_reg (code, AMD64_RDI, AMD64_RDI);
790 /* if yes, jump to actual trampoline */
791 jump_obj_null = code;
792 amd64_branch8 (code, X86_CC_Z, -1, 1);
794 /* load obj->synchronization to RCX */
795 amd64_mov_reg_membase (code, AMD64_RCX, AMD64_RDI, G_STRUCT_OFFSET (MonoObject, synchronisation), 8);
796 /* is synchronization null? */
797 amd64_test_reg_reg (code, AMD64_RCX, AMD64_RCX);
798 /* if yes, jump to actual trampoline */
799 jump_sync_null = code;
800 amd64_branch8 (code, X86_CC_Z, -1, 1);
802 /* load MonoInternalThread* into RDX */
803 code = mono_amd64_emit_tls_get (code, AMD64_RDX, mono_thread_get_tls_offset ());
804 /* load TID into RDX */
805 amd64_mov_reg_membase (code, AMD64_RDX, AMD64_RDX, G_STRUCT_OFFSET (MonoInternalThread, tid), 8);
807 /* is synchronization->owner null? */
808 amd64_alu_membase_imm_size (code, X86_CMP, AMD64_RCX, owner_offset, 0, 8);
809 /* if not, jump to next case */
811 amd64_branch8 (code, X86_CC_NZ, -1, 1);
813 /* if yes, try a compare-exchange with the TID */
815 amd64_alu_reg_reg (code, X86_XOR, AMD64_RAX, AMD64_RAX);
816 /* compare and exchange */
817 amd64_prefix (code, X86_LOCK_PREFIX);
818 amd64_cmpxchg_membase_reg_size (code, AMD64_RCX, owner_offset, AMD64_RDX, 8);
819 /* if not successful, jump to actual trampoline */
820 jump_cmpxchg_failed = code;
821 amd64_branch8 (code, X86_CC_NZ, -1, 1);
822 /* if successful, return */
825 /* next case: synchronization->owner is not null */
826 x86_patch (jump_tid, code);
827 /* is synchronization->owner == TID? */
828 amd64_alu_membase_reg_size (code, X86_CMP, AMD64_RCX, owner_offset, AMD64_RDX, 8);
829 /* if not, jump to actual trampoline */
830 jump_other_owner = code;
831 amd64_branch8 (code, X86_CC_NZ, -1, 1);
832 /* if yes, increment nest */
833 amd64_inc_membase_size (code, AMD64_RCX, nest_offset, 4);
837 x86_patch (jump_obj_null, code);
838 x86_patch (jump_sync_null, code);
839 x86_patch (jump_cmpxchg_failed, code);
840 x86_patch (jump_other_owner, code);
843 /* jump to the actual trampoline */
844 #if MONO_AMD64_ARG_REG1 != AMD64_RDI
845 amd64_mov_reg_reg (code, MONO_AMD64_ARG_REG1, AMD64_RDI);
849 code = mono_arch_emit_load_aotconst (buf, code, &ji, MONO_PATCH_INFO_JIT_ICALL_ADDR, "specific_trampoline_monitor_enter");
850 amd64_jump_reg (code, AMD64_R11);
852 tramp = mono_arch_create_specific_trampoline (NULL, MONO_TRAMPOLINE_MONITOR_ENTER, mono_get_root_domain (), NULL);
854 /* jump to the actual trampoline */
855 amd64_jump_code (code, tramp);
858 mono_arch_flush_icache (code, code - buf);
859 g_assert (code - buf <= tramp_size);
862 *info = mono_tramp_info_create (g_strdup_printf ("monitor_enter_trampoline"), buf, code - buf, ji, unwind_ops);
868 mono_arch_create_monitor_exit_trampoline (MonoTrampInfo **info, gboolean aot)
872 guint8 *jump_obj_null, *jump_have_waiters, *jump_sync_null, *jump_not_owned;
875 int owner_offset, nest_offset, entry_count_offset;
876 MonoJumpInfo *ji = NULL;
877 GSList *unwind_ops = NULL;
879 g_assert (MONO_ARCH_MONITOR_OBJECT_REG == AMD64_RDI);
881 mono_monitor_threads_sync_members_offset (&owner_offset, &nest_offset, &entry_count_offset);
882 g_assert (MONO_THREADS_SYNC_MEMBER_SIZE (owner_offset) == sizeof (gpointer));
883 g_assert (MONO_THREADS_SYNC_MEMBER_SIZE (nest_offset) == sizeof (guint32));
884 g_assert (MONO_THREADS_SYNC_MEMBER_SIZE (entry_count_offset) == sizeof (gint32));
885 owner_offset = MONO_THREADS_SYNC_MEMBER_OFFSET (owner_offset);
886 nest_offset = MONO_THREADS_SYNC_MEMBER_OFFSET (nest_offset);
887 entry_count_offset = MONO_THREADS_SYNC_MEMBER_OFFSET (entry_count_offset);
891 code = buf = mono_global_codeman_reserve (tramp_size);
893 if (mono_thread_get_tls_offset () != -1) {
894 /* MonoObject* obj is in RDI */
896 amd64_test_reg_reg (code, AMD64_RDI, AMD64_RDI);
897 /* if yes, jump to actual trampoline */
898 jump_obj_null = code;
899 amd64_branch8 (code, X86_CC_Z, -1, 1);
901 /* load obj->synchronization to RCX */
902 amd64_mov_reg_membase (code, AMD64_RCX, AMD64_RDI, G_STRUCT_OFFSET (MonoObject, synchronisation), 8);
903 /* is synchronization null? */
904 amd64_test_reg_reg (code, AMD64_RCX, AMD64_RCX);
905 /* if yes, jump to actual trampoline */
906 jump_sync_null = code;
907 amd64_branch8 (code, X86_CC_Z, -1, 1);
909 /* next case: synchronization is not null */
910 /* load MonoInternalThread* into RDX */
911 code = mono_amd64_emit_tls_get (code, AMD64_RDX, mono_thread_get_tls_offset ());
912 /* load TID into RDX */
913 amd64_mov_reg_membase (code, AMD64_RDX, AMD64_RDX, G_STRUCT_OFFSET (MonoInternalThread, tid), 8);
914 /* is synchronization->owner == TID */
915 amd64_alu_membase_reg_size (code, X86_CMP, AMD64_RCX, owner_offset, AMD64_RDX, 8);
916 /* if no, jump to actual trampoline */
917 jump_not_owned = code;
918 amd64_branch8 (code, X86_CC_NZ, -1, 1);
920 /* next case: synchronization->owner == TID */
921 /* is synchronization->nest == 1 */
922 amd64_alu_membase_imm_size (code, X86_CMP, AMD64_RCX, nest_offset, 1, 4);
923 /* if not, jump to next case */
925 amd64_branch8 (code, X86_CC_NZ, -1, 1);
926 /* if yes, is synchronization->entry_count zero? */
927 amd64_alu_membase_imm_size (code, X86_CMP, AMD64_RCX, entry_count_offset, 0, 4);
928 /* if not, jump to actual trampoline */
929 jump_have_waiters = code;
930 amd64_branch8 (code, X86_CC_NZ, -1 , 1);
931 /* if yes, set synchronization->owner to null and return */
932 amd64_mov_membase_imm (code, AMD64_RCX, owner_offset, 0, 8);
935 /* next case: synchronization->nest is not 1 */
936 x86_patch (jump_next, code);
937 /* decrease synchronization->nest and return */
938 amd64_dec_membase_size (code, AMD64_RCX, nest_offset, 4);
941 x86_patch (jump_obj_null, code);
942 x86_patch (jump_have_waiters, code);
943 x86_patch (jump_not_owned, code);
944 x86_patch (jump_sync_null, code);
947 /* jump to the actual trampoline */
948 #if MONO_AMD64_ARG_REG1 != AMD64_RDI
949 amd64_mov_reg_reg (code, MONO_AMD64_ARG_REG1, AMD64_RDI);
953 code = mono_arch_emit_load_aotconst (buf, code, &ji, MONO_PATCH_INFO_JIT_ICALL_ADDR, "specific_trampoline_monitor_exit");
954 amd64_jump_reg (code, AMD64_R11);
956 tramp = mono_arch_create_specific_trampoline (NULL, MONO_TRAMPOLINE_MONITOR_EXIT, mono_get_root_domain (), NULL);
957 amd64_jump_code (code, tramp);
960 mono_arch_flush_icache (code, code - buf);
961 g_assert (code - buf <= tramp_size);
964 *info = mono_tramp_info_create (g_strdup_printf ("monitor_exit_trampoline"), buf, code - buf, ji, unwind_ops);
971 mono_arch_invalidate_method (MonoJitInfo *ji, void *func, gpointer func_arg)
973 /* FIXME: This is not thread safe */
974 guint8 *code = ji->code_start;
976 amd64_mov_reg_imm (code, AMD64_ARG_REG1, func_arg);
977 amd64_mov_reg_imm (code, AMD64_R11, func);
979 x86_push_imm (code, (guint64)func_arg);
980 amd64_call_reg (code, AMD64_R11);
984 * mono_arch_get_call_target:
986 * Return the address called by the code before CODE if exists.
989 mono_arch_get_call_target (guint8 *code)
991 if (code [-5] == 0xe8) {
992 guint32 disp = *(guint32*)(code - 4);
993 guint8 *target = code + disp;
1002 * mono_arch_get_plt_info_offset:
1004 * Return the PLT info offset belonging to the plt entry PLT_ENTRY.
1007 mono_arch_get_plt_info_offset (guint8 *plt_entry, mgreg_t *regs, guint8 *code)
1009 return *(guint32*)(plt_entry + 6);