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/metadata/monitor.h>
20 #include <mono/metadata/gc-internal.h>
21 #include <mono/arch/amd64/amd64-codegen.h>
23 #include <mono/utils/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:
35 * @addr: pointer to native code for @m
37 * when value type methods are called through the vtable we need to unbox the
38 * this argument. This method returns a pointer to a trampoline which does
39 * unboxing before calling the method
42 mono_arch_get_unbox_trampoline (MonoMethod *m, gpointer addr)
47 MonoDomain *domain = mono_domain_get ();
49 this_reg = mono_arch_get_this_arg_reg (NULL);
51 start = code = mono_domain_code_reserve (domain, 20);
53 amd64_alu_reg_imm (code, X86_ADD, this_reg, sizeof (MonoObject));
54 /* FIXME: Optimize this */
55 amd64_mov_reg_imm (code, AMD64_RAX, addr);
56 amd64_jump_reg (code, AMD64_RAX);
57 g_assert ((code - start) < 20);
59 mono_arch_flush_icache (start, code - start);
65 * mono_arch_get_static_rgctx_trampoline:
67 * Create a trampoline which sets RGCTX_REG to MRGCTX, then jumps to ADDR.
70 mono_arch_get_static_rgctx_trampoline (MonoMethod *m, MonoMethodRuntimeGenericContext *mrgctx, gpointer addr)
75 MonoDomain *domain = mono_domain_get ();
77 #ifdef MONO_ARCH_NOMAP32BIT
80 /* AOTed code could still have a non-32 bit address */
81 if ((((guint64)addr) >> 32) == 0)
87 start = code = mono_domain_code_reserve (domain, buf_len);
89 amd64_mov_reg_imm (code, MONO_ARCH_RGCTX_REG, mrgctx);
90 amd64_jump_code (code, addr);
91 g_assert ((code - start) < buf_len);
93 mono_arch_flush_icache (start, code - start);
99 mono_arch_get_llvm_imt_trampoline (MonoDomain *domain, MonoMethod *m, int vt_offset)
101 guint8 *code, *start;
107 start = code = mono_domain_code_reserve (domain, buf_len);
109 this_reg = mono_arch_get_this_arg_reg (NULL);
112 amd64_mov_reg_imm (code, MONO_ARCH_IMT_REG, m);
113 /* Load vtable address */
114 amd64_mov_reg_membase (code, AMD64_RAX, this_reg, 0, 8);
115 amd64_jump_membase (code, AMD64_RAX, vt_offset);
118 g_assert ((code - start) < buf_len);
120 mono_arch_flush_icache (start, code - start);
126 * mono_arch_patch_callsite:
128 * Patch the callsite whose address is given by ORIG_CODE so it calls ADDR. ORIG_CODE
129 * points to the pc right after the call.
132 mono_arch_patch_callsite (guint8 *method_start, guint8 *orig_code, guint8 *addr)
136 gboolean can_write = mono_breakpoint_clean_code (method_start, orig_code, 14, buf, sizeof (buf));
140 if (((code [-13] == 0x49) && (code [-12] == 0xbb)) || (code [-5] == 0xe8)) {
141 if (code [-5] != 0xe8) {
143 InterlockedExchangePointer ((gpointer*)(orig_code - 11), addr);
144 VALGRIND_DISCARD_TRANSLATIONS (orig_code - 11, sizeof (gpointer));
147 if ((((guint64)(addr)) >> 32) != 0) {
148 #ifdef MONO_ARCH_NOMAP32BIT
149 /* Print some diagnostics */
150 MonoJitInfo *ji = mono_jit_info_table_find (mono_domain_get (), (char*)orig_code);
152 fprintf (stderr, "At %s, offset 0x%zx\n", mono_method_full_name (ji->method, TRUE), (guint8*)orig_code - (guint8*)ji->code_start);
153 fprintf (stderr, "Addr: %p\n", addr);
154 ji = mono_jit_info_table_find (mono_domain_get (), (char*)addr);
156 fprintf (stderr, "Callee: %s\n", mono_method_full_name (ji->method, TRUE));
157 g_assert_not_reached ();
160 * This might happen when calling AOTed code. Create a thunk.
162 guint8 *thunk_start, *thunk_code;
164 thunk_start = thunk_code = mono_domain_code_reserve (mono_domain_get (), 32);
165 amd64_jump_membase (thunk_code, AMD64_RIP, 0);
166 *(guint64*)thunk_code = (guint64)addr;
168 g_assert ((((guint64)(addr)) >> 32) == 0);
169 mono_arch_flush_icache (thunk_start, thunk_code - thunk_start);
172 g_assert ((((guint64)(orig_code)) >> 32) == 0);
174 InterlockedExchange ((gint32*)(orig_code - 4), ((gint64)addr - (gint64)orig_code));
175 VALGRIND_DISCARD_TRANSLATIONS (orig_code - 5, 4);
179 else if ((code [-7] == 0x41) && (code [-6] == 0xff) && (code [-5] == 0x15)) {
180 /* call *<OFFSET>(%rip) */
181 gpointer *got_entry = (gpointer*)((guint8*)orig_code + (*(guint32*)(orig_code - 4)));
183 InterlockedExchangePointer (got_entry, addr);
184 VALGRIND_DISCARD_TRANSLATIONS (orig_code - 5, sizeof (gpointer));
190 mono_arch_patch_plt_entry (guint8 *code, gpointer *got, mgreg_t *regs, guint8 *addr)
193 gpointer *plt_jump_table_entry;
195 /* A PLT entry: jmp *<DISP>(%rip) */
196 g_assert (code [0] == 0xff);
197 g_assert (code [1] == 0x25);
199 disp = *(gint32*)(code + 2);
201 plt_jump_table_entry = (gpointer*)(code + 6 + disp);
203 InterlockedExchangePointer (plt_jump_table_entry, addr);
207 get_vcall_slot (guint8 *code, mgreg_t *regs, int *displacement)
211 MonoJitInfo *ji = NULL;
214 /* code - 9 might be before the start of the method */
215 /* FIXME: Avoid this expensive call somehow */
216 ji = mono_jit_info_table_find (mono_domain_get (), (char*)code);
219 mono_breakpoint_clean_code (ji ? ji->code_start : NULL, code, 9, buf, sizeof (buf));
226 if ((code [0] == 0x41) && (code [1] == 0xff) && (code [2] == 0x15)) {
227 /* call OFFSET(%rip) */
228 g_assert_not_reached ();
229 *displacement = *(guint32*)(code + 3);
230 return (gpointer*)(code + disp + 7);
232 g_assert_not_reached ();
238 get_vcall_slot_addr (guint8* code, mgreg_t *regs)
242 vt = get_vcall_slot (code, regs, &displacement);
245 return (gpointer*)((char*)vt + displacement);
249 mono_arch_nullify_class_init_trampoline (guint8 *code, mgreg_t *regs)
252 MonoJitInfo *ji = NULL;
256 /* code - 7 might be before the start of the method */
257 /* FIXME: Avoid this expensive call somehow */
258 ji = mono_jit_info_table_find (mono_domain_get (), (char*)code);
261 can_write = mono_breakpoint_clean_code (ji ? ji->code_start : NULL, code, 7, buf, sizeof (buf));
267 * A given byte sequence can match more than case here, so we have to be
268 * really careful about the ordering of the cases. Longer sequences
271 if ((buf [0] == 0x41) && (buf [1] == 0xff) && (buf [2] == 0x15)) {
272 gpointer *vtable_slot;
274 /* call *<OFFSET>(%rip) */
275 vtable_slot = get_vcall_slot_addr (code, regs);
276 g_assert (vtable_slot);
278 *vtable_slot = nullified_class_init_trampoline;
279 } else if (buf [2] == 0xe8) {
281 //guint8 *buf = code - 2;
284 * It would be better to replace the call with nops, but that doesn't seem
285 * to work on SMP machines even when the whole call is inside a cache line.
286 * Patching the call address seems to work.
296 mono_arch_patch_callsite (code - 5, code, nullified_class_init_trampoline);
297 } else if ((buf [5] == 0xff) && x86_modrm_mod (buf [6]) == 3 && x86_modrm_reg (buf [6]) == 2) {
299 /* Generated by the LLVM JIT or on platforms without MAP_32BIT set */
300 mono_arch_patch_callsite (code - 13, code, nullified_class_init_trampoline);
301 } else if (buf [4] == 0x90 || buf [5] == 0xeb || buf [6] == 0x66) {
302 /* Already changed by another thread */
305 printf ("Invalid trampoline sequence: %x %x %x %x %x %x %x\n", buf [0], buf [1], buf [2], buf [3],
306 buf [4], buf [5], buf [6]);
307 g_assert_not_reached ();
312 mono_arch_nullify_plt_entry (guint8 *code, mgreg_t *regs)
314 if (mono_aot_only && !nullified_class_init_trampoline)
315 nullified_class_init_trampoline = mono_aot_get_trampoline ("nullified_class_init_trampoline");
317 mono_arch_patch_plt_entry (code, NULL, regs, nullified_class_init_trampoline);
321 mono_arch_create_generic_trampoline (MonoTrampolineType tramp_type, MonoTrampInfo **info, gboolean aot)
323 guint8 *buf, *code, *tramp, *br [2], *r11_save_code, *after_r11_save_code;
324 int i, lmf_offset, offset, res_offset, arg_offset, rax_offset, tramp_offset;
325 int buf_len, saved_regs_offset;
326 int saved_fpregs_offset, rbp_offset, framesize, orig_rsp_to_rbp_offset, cfa_offset;
328 GSList *unwind_ops = NULL;
329 MonoJumpInfo *ji = NULL;
331 if (tramp_type == MONO_TRAMPOLINE_JUMP)
337 code = buf = mono_global_codeman_reserve (buf_len);
339 framesize = 538 + sizeof (MonoLMF);
340 framesize = (framesize + (MONO_ARCH_FRAME_ALIGNMENT - 1)) & ~ (MONO_ARCH_FRAME_ALIGNMENT - 1);
342 orig_rsp_to_rbp_offset = 0;
343 r11_save_code = code;
344 /* Reserve 5 bytes for the mov_membase_reg to save R11 */
346 after_r11_save_code = code;
348 // CFA = sp + 16 (the trampoline address is on the stack)
350 mono_add_unwind_op_def_cfa (unwind_ops, code, buf, AMD64_RSP, 16);
351 // IP saved at CFA - 8
352 mono_add_unwind_op_offset (unwind_ops, code, buf, AMD64_RIP, -8);
354 /* Pop the return address off the stack */
355 amd64_pop_reg (code, AMD64_R11);
356 orig_rsp_to_rbp_offset += 8;
359 mono_add_unwind_op_def_cfa_offset (unwind_ops, code, buf, cfa_offset);
362 * Allocate a new stack frame
364 amd64_push_reg (code, AMD64_RBP);
366 mono_add_unwind_op_def_cfa_offset (unwind_ops, code, buf, cfa_offset);
367 mono_add_unwind_op_offset (unwind_ops, code, buf, AMD64_RBP, - cfa_offset);
369 orig_rsp_to_rbp_offset -= 8;
370 amd64_mov_reg_reg (code, AMD64_RBP, AMD64_RSP, 8);
371 mono_add_unwind_op_def_cfa_reg (unwind_ops, code, buf, AMD64_RBP);
372 amd64_alu_reg_imm (code, X86_SUB, AMD64_RSP, framesize);
375 rbp_offset = - offset;
378 rax_offset = - offset;
381 tramp_offset = - offset;
384 arg_offset = - offset;
386 /* Compute the trampoline address from the return address */
388 /* 7 = length of call *<offset>(rip) */
389 amd64_alu_reg_imm (code, X86_SUB, AMD64_R11, 7);
391 /* 5 = length of amd64_call_membase () */
392 amd64_alu_reg_imm (code, X86_SUB, AMD64_R11, 5);
394 amd64_mov_membase_reg (code, AMD64_RBP, tramp_offset, AMD64_R11, 8);
397 res_offset = - offset;
399 /* Save all registers */
401 offset += AMD64_NREG * 8;
402 saved_regs_offset = - offset;
403 for (i = 0; i < AMD64_NREG; ++i) {
404 if (i == AMD64_RBP) {
405 /* RAX is already saved */
406 amd64_mov_reg_membase (code, AMD64_RAX, AMD64_RBP, rbp_offset, 8);
407 amd64_mov_membase_reg (code, AMD64_RBP, saved_regs_offset + (i * 8), AMD64_RAX, 8);
408 } else if (i != AMD64_R11) {
409 amd64_mov_membase_reg (code, AMD64_RBP, saved_regs_offset + (i * 8), i, 8);
411 /* We have to save R11 right at the start of
412 the trampoline code because it's used as a
414 amd64_mov_membase_reg (r11_save_code, AMD64_RSP, saved_regs_offset + orig_rsp_to_rbp_offset + (i * 8), i, 8);
415 g_assert (r11_save_code == after_r11_save_code);
419 saved_fpregs_offset = - offset;
420 for (i = 0; i < 8; ++i)
421 amd64_movsd_membase_reg (code, AMD64_RBP, saved_fpregs_offset + (i * 8), i);
423 if (tramp_type != MONO_TRAMPOLINE_GENERIC_CLASS_INIT &&
424 tramp_type != MONO_TRAMPOLINE_MONITOR_ENTER &&
425 tramp_type != MONO_TRAMPOLINE_MONITOR_EXIT) {
426 /* Obtain the trampoline argument which is encoded in the instruction stream */
428 /* Load the GOT offset */
429 amd64_mov_reg_membase (code, AMD64_R11, AMD64_RBP, tramp_offset, 8);
430 amd64_mov_reg_membase (code, AMD64_RAX, AMD64_R11, 7, 4);
431 /* Compute the address of the GOT slot */
432 amd64_alu_reg_reg_size (code, X86_ADD, AMD64_R11, AMD64_RAX, 8);
434 amd64_mov_reg_membase (code, AMD64_R11, AMD64_R11, 0, 8);
436 amd64_mov_reg_membase (code, AMD64_R11, AMD64_RBP, tramp_offset, 8);
437 amd64_mov_reg_membase (code, AMD64_RAX, AMD64_R11, 5, 1);
438 amd64_widen_reg (code, AMD64_RAX, AMD64_RAX, TRUE, FALSE);
439 amd64_alu_reg_imm_size (code, X86_CMP, AMD64_RAX, 4, 1);
441 x86_branch8 (code, X86_CC_NE, 6, FALSE);
442 /* 32 bit immediate */
443 amd64_mov_reg_membase (code, AMD64_R11, AMD64_R11, 6, 4);
445 x86_jump8 (code, 10);
446 /* 64 bit immediate */
447 mono_amd64_patch (br [0], code);
448 amd64_mov_reg_membase (code, AMD64_R11, AMD64_R11, 6, 8);
449 mono_amd64_patch (br [1], code);
451 amd64_mov_membase_reg (code, AMD64_RBP, arg_offset, AMD64_R11, 8);
453 amd64_mov_reg_membase (code, AMD64_R11, AMD64_RBP, saved_regs_offset + (MONO_AMD64_ARG_REG1 * 8), 8);
454 amd64_mov_membase_reg (code, AMD64_RBP, arg_offset, AMD64_R11, 8);
459 offset += sizeof (MonoLMF);
460 lmf_offset = - offset;
464 amd64_mov_reg_membase (code, AMD64_R11, AMD64_RBP, 8, 8);
466 amd64_mov_reg_imm (code, AMD64_R11, 0);
467 amd64_mov_membase_reg (code, AMD64_RBP, lmf_offset + G_STRUCT_OFFSET (MonoLMF, rip), AMD64_R11, 8);
469 amd64_mov_reg_membase (code, AMD64_R11, AMD64_RSP, framesize, 8);
470 amd64_mov_membase_reg (code, AMD64_RBP, lmf_offset + G_STRUCT_OFFSET (MonoLMF, rbp), AMD64_R11, 8);
472 amd64_mov_reg_reg (code, AMD64_R11, AMD64_RSP, 8);
473 amd64_alu_reg_imm (code, X86_ADD, AMD64_R11, framesize + 16);
474 amd64_mov_membase_reg (code, AMD64_RBP, lmf_offset + G_STRUCT_OFFSET (MonoLMF, rsp), AMD64_R11, 8);
476 if (tramp_type == MONO_TRAMPOLINE_JIT || tramp_type == MONO_TRAMPOLINE_JUMP) {
477 amd64_mov_reg_membase (code, AMD64_R11, AMD64_RBP, arg_offset, 8);
478 amd64_mov_membase_reg (code, AMD64_RBP, lmf_offset + G_STRUCT_OFFSET (MonoLMF, method), AMD64_R11, 8);
480 amd64_mov_membase_imm (code, AMD64_RBP, lmf_offset + G_STRUCT_OFFSET (MonoLMF, method), 0, 8);
482 /* Save callee saved regs */
484 amd64_mov_membase_reg (code, AMD64_RBP, lmf_offset + G_STRUCT_OFFSET (MonoLMF, rdi), AMD64_RDI, 8);
485 amd64_mov_membase_reg (code, AMD64_RBP, lmf_offset + G_STRUCT_OFFSET (MonoLMF, rsi), AMD64_RSI, 8);
487 amd64_mov_membase_reg (code, AMD64_RBP, lmf_offset + G_STRUCT_OFFSET (MonoLMF, rbx), AMD64_RBX, 8);
488 amd64_mov_membase_reg (code, AMD64_RBP, lmf_offset + G_STRUCT_OFFSET (MonoLMF, r12), AMD64_R12, 8);
489 amd64_mov_membase_reg (code, AMD64_RBP, lmf_offset + G_STRUCT_OFFSET (MonoLMF, r13), AMD64_R13, 8);
490 amd64_mov_membase_reg (code, AMD64_RBP, lmf_offset + G_STRUCT_OFFSET (MonoLMF, r14), AMD64_R14, 8);
491 amd64_mov_membase_reg (code, AMD64_RBP, lmf_offset + G_STRUCT_OFFSET (MonoLMF, r15), AMD64_R15, 8);
494 code = mono_arch_emit_load_aotconst (buf, code, &ji, MONO_PATCH_INFO_JIT_ICALL_ADDR, "mono_get_lmf_addr");
496 amd64_mov_reg_imm (code, AMD64_R11, mono_get_lmf_addr);
498 amd64_call_reg (code, AMD64_R11);
501 amd64_mov_membase_reg (code, AMD64_RBP, lmf_offset + G_STRUCT_OFFSET (MonoLMF, lmf_addr), AMD64_RAX, 8);
502 /* Save previous_lmf */
503 /* Set the lowest bit to 1 to signal that this LMF has the ip field set */
504 amd64_mov_reg_membase (code, AMD64_R11, AMD64_RAX, 0, 8);
505 amd64_alu_reg_imm_size (code, X86_ADD, AMD64_R11, 1, 8);
506 amd64_mov_membase_reg (code, AMD64_RBP, lmf_offset + G_STRUCT_OFFSET (MonoLMF, previous_lmf), AMD64_R11, 8);
508 amd64_lea_membase (code, AMD64_R11, AMD64_RBP, lmf_offset);
509 amd64_mov_membase_reg (code, AMD64_RAX, 0, AMD64_R11, 8);
513 /* Arg1 is the pointer to the saved registers */
514 amd64_lea_membase (code, AMD64_ARG_REG1, AMD64_RBP, saved_regs_offset);
516 /* Arg2 is the address of the calling code */
518 amd64_mov_reg_membase (code, AMD64_ARG_REG2, AMD64_RBP, 8, 8);
520 amd64_mov_reg_imm (code, AMD64_ARG_REG2, 0);
522 /* Arg3 is the method/vtable ptr */
523 amd64_mov_reg_membase (code, AMD64_ARG_REG3, AMD64_RBP, arg_offset, 8);
525 /* Arg4 is the trampoline address */
526 amd64_mov_reg_membase (code, AMD64_ARG_REG4, AMD64_RBP, tramp_offset, 8);
529 char *icall_name = g_strdup_printf ("trampoline_func_%d", tramp_type);
530 code = mono_arch_emit_load_aotconst (buf, code, &ji, MONO_PATCH_INFO_JIT_ICALL_ADDR, icall_name);
532 tramp = (guint8*)mono_get_trampoline_func (tramp_type);
533 amd64_mov_reg_imm (code, AMD64_R11, tramp);
535 amd64_call_reg (code, AMD64_R11);
537 /* Check for thread interruption */
538 /* This is not perf critical code so no need to check the interrupt flag */
540 * Have to call the _force_ variant, since there could be a protected wrapper on the top of the stack.
542 amd64_mov_membase_reg (code, AMD64_RBP, res_offset, AMD64_RAX, 8);
544 code = mono_arch_emit_load_aotconst (buf, code, &ji, MONO_PATCH_INFO_JIT_ICALL_ADDR, "mono_thread_force_interruption_checkpoint");
546 amd64_mov_reg_imm (code, AMD64_R11, (guint8*)mono_thread_force_interruption_checkpoint);
548 amd64_call_reg (code, AMD64_R11);
550 amd64_mov_reg_membase (code, AMD64_RAX, AMD64_RBP, res_offset, 8);
554 amd64_mov_reg_membase (code, AMD64_RCX, AMD64_RBP, lmf_offset + G_STRUCT_OFFSET (MonoLMF, previous_lmf), 8);
555 amd64_alu_reg_imm_size (code, X86_SUB, AMD64_RCX, 1, 8);
556 amd64_mov_reg_membase (code, AMD64_R11, AMD64_RBP, lmf_offset + G_STRUCT_OFFSET (MonoLMF, lmf_addr), 8);
557 amd64_mov_membase_reg (code, AMD64_R11, 0, AMD64_RCX, 8);
560 * Save rax to the stack, after the leave instruction, this will become part of
563 amd64_mov_membase_reg (code, AMD64_RBP, rax_offset, AMD64_RAX, 8);
565 /* Restore argument registers, r10 (imt method/rgxtx)
566 and rax (needed for direct calls to C vararg functions). */
567 for (i = 0; i < AMD64_NREG; ++i)
568 if (AMD64_IS_ARGUMENT_REG (i) || i == AMD64_R10 || i == AMD64_RAX)
569 amd64_mov_reg_membase (code, i, AMD64_RBP, saved_regs_offset + (i * 8), 8);
571 for (i = 0; i < 8; ++i)
572 amd64_movsd_reg_membase (code, i, AMD64_RBP, saved_fpregs_offset + (i * 8));
577 if (MONO_TRAMPOLINE_TYPE_MUST_RETURN (tramp_type)) {
579 amd64_mov_reg_membase (code, AMD64_RAX, AMD64_RSP, rax_offset - 0x8, 8);
582 /* call the compiled method using the saved rax */
583 amd64_jump_membase (code, AMD64_RSP, rax_offset - 0x8);
586 g_assert ((code - buf) <= buf_len);
588 mono_arch_flush_icache (buf, code - buf);
590 if (tramp_type == MONO_TRAMPOLINE_CLASS_INIT) {
591 /* Initialize the nullified class init trampoline used in the AOT case */
592 nullified_class_init_trampoline = mono_arch_get_nullified_class_init_trampoline (NULL);
596 *info = mono_tramp_info_create (mono_get_generic_trampoline_name (tramp_type), buf, code - buf, ji, unwind_ops);
602 mono_arch_get_nullified_class_init_trampoline (MonoTrampInfo **info)
606 code = buf = mono_global_codeman_reserve (16);
609 mono_arch_flush_icache (buf, code - buf);
612 *info = mono_tramp_info_create (g_strdup_printf ("nullified_class_init_trampoline"), buf, code - buf, NULL, NULL);
614 if (mono_jit_map_is_enabled ())
615 mono_emit_jit_tramp (buf, code - buf, "nullified_class_init_trampoline");
621 mono_arch_create_specific_trampoline (gpointer arg1, MonoTrampolineType tramp_type, MonoDomain *domain, guint32 *code_len)
623 guint8 *code, *buf, *tramp;
626 tramp = mono_get_trampoline_code (tramp_type);
628 if ((((guint64)arg1) >> 32) == 0)
633 code = buf = mono_domain_code_reserve_align (domain, size, 1);
635 amd64_call_code (code, tramp);
636 /* The trampoline code will obtain the argument from the instruction stream */
637 if ((((guint64)arg1) >> 32) == 0) {
639 *(guint32*)(code + 1) = (gint64)arg1;
643 *(guint64*)(code + 1) = (gint64)arg1;
647 g_assert ((code - buf) <= size);
652 mono_arch_flush_icache (buf, size);
658 mono_arch_create_rgctx_lazy_fetch_trampoline (guint32 slot, MonoTrampInfo **info, gboolean aot)
662 guint8 **rgctx_null_jumps;
667 MonoJumpInfo *ji = NULL;
668 GSList *unwind_ops = NULL;
670 mrgctx = MONO_RGCTX_SLOT_IS_MRGCTX (slot);
671 index = MONO_RGCTX_SLOT_INDEX (slot);
673 index += MONO_SIZEOF_METHOD_RUNTIME_GENERIC_CONTEXT / sizeof (gpointer);
674 for (depth = 0; ; ++depth) {
675 int size = mono_class_rgctx_get_array_size (depth, mrgctx);
677 if (index < size - 1)
682 tramp_size = 64 + 8 * depth;
684 code = buf = mono_global_codeman_reserve (tramp_size);
686 unwind_ops = mono_arch_get_cie_program ();
688 rgctx_null_jumps = g_malloc (sizeof (guint8*) * (depth + 2));
692 amd64_mov_reg_reg (code, AMD64_RAX, AMD64_ARG_REG1, 8);
694 /* load rgctx ptr from vtable */
695 amd64_mov_reg_membase (code, AMD64_RAX, AMD64_ARG_REG1, G_STRUCT_OFFSET (MonoVTable, runtime_generic_context), 8);
696 /* is the rgctx ptr null? */
697 amd64_test_reg_reg (code, AMD64_RAX, AMD64_RAX);
698 /* if yes, jump to actual trampoline */
699 rgctx_null_jumps [0] = code;
700 amd64_branch8 (code, X86_CC_Z, -1, 1);
703 for (i = 0; i < depth; ++i) {
704 /* load ptr to next array */
705 if (mrgctx && i == 0)
706 amd64_mov_reg_membase (code, AMD64_RAX, AMD64_RAX, MONO_SIZEOF_METHOD_RUNTIME_GENERIC_CONTEXT, 8);
708 amd64_mov_reg_membase (code, AMD64_RAX, AMD64_RAX, 0, 8);
709 /* is the ptr null? */
710 amd64_test_reg_reg (code, AMD64_RAX, AMD64_RAX);
711 /* if yes, jump to actual trampoline */
712 rgctx_null_jumps [i + 1] = code;
713 amd64_branch8 (code, X86_CC_Z, -1, 1);
717 amd64_mov_reg_membase (code, AMD64_RAX, AMD64_RAX, sizeof (gpointer) * (index + 1), 8);
718 /* is the slot null? */
719 amd64_test_reg_reg (code, AMD64_RAX, AMD64_RAX);
720 /* if yes, jump to actual trampoline */
721 rgctx_null_jumps [depth + 1] = code;
722 amd64_branch8 (code, X86_CC_Z, -1, 1);
723 /* otherwise return */
726 for (i = mrgctx ? 1 : 0; i <= depth + 1; ++i)
727 x86_patch (rgctx_null_jumps [i], code);
729 g_free (rgctx_null_jumps);
731 /* move the rgctx pointer to the VTABLE register */
732 amd64_mov_reg_reg (code, MONO_ARCH_VTABLE_REG, AMD64_ARG_REG1, 8);
735 code = mono_arch_emit_load_aotconst (buf, code, &ji, MONO_PATCH_INFO_JIT_ICALL_ADDR, g_strdup_printf ("specific_trampoline_lazy_fetch_%u", slot));
736 amd64_jump_reg (code, AMD64_R11);
738 tramp = mono_arch_create_specific_trampoline (GUINT_TO_POINTER (slot), MONO_TRAMPOLINE_RGCTX_LAZY_FETCH, mono_get_root_domain (), NULL);
740 /* jump to the actual trampoline */
741 amd64_jump_code (code, tramp);
744 mono_arch_flush_icache (buf, code - buf);
746 g_assert (code - buf <= tramp_size);
749 *info = mono_tramp_info_create (mono_get_rgctx_fetch_trampoline_name (slot), buf, code - buf, ji, unwind_ops);
755 mono_arch_create_generic_class_init_trampoline (MonoTrampInfo **info, gboolean aot)
759 static int byte_offset = -1;
760 static guint8 bitmask;
763 GSList *unwind_ops = NULL;
764 MonoJumpInfo *ji = NULL;
768 code = buf = mono_global_codeman_reserve (tramp_size);
771 mono_marshal_find_bitfield_offset (MonoVTable, initialized, &byte_offset, &bitmask);
773 amd64_test_membase_imm_size (code, MONO_AMD64_ARG_REG1, byte_offset, bitmask, 1);
775 amd64_branch8 (code, X86_CC_Z, -1, 1);
779 x86_patch (jump, code);
782 code = mono_arch_emit_load_aotconst (buf, code, &ji, MONO_PATCH_INFO_JIT_ICALL_ADDR, "specific_trampoline_generic_class_init");
783 amd64_jump_reg (code, AMD64_R11);
785 tramp = mono_arch_create_specific_trampoline (NULL, MONO_TRAMPOLINE_GENERIC_CLASS_INIT, mono_get_root_domain (), NULL);
787 /* jump to the actual trampoline */
788 amd64_jump_code (code, tramp);
791 mono_arch_flush_icache (buf, code - buf);
793 g_assert (code - buf <= tramp_size);
796 *info = mono_tramp_info_create (g_strdup_printf ("generic_class_init_trampoline"), buf, code - buf, ji, unwind_ops);
801 #ifdef MONO_ARCH_MONITOR_OBJECT_REG
804 mono_arch_create_monitor_enter_trampoline (MonoTrampInfo **info, gboolean aot)
808 guint8 *jump_obj_null, *jump_sync_null, *jump_cmpxchg_failed, *jump_other_owner, *jump_tid, *jump_sync_thin_hash = NULL;
810 int owner_offset, nest_offset, dummy;
811 MonoJumpInfo *ji = NULL;
812 GSList *unwind_ops = NULL;
814 g_assert (MONO_ARCH_MONITOR_OBJECT_REG == AMD64_RDI);
816 mono_monitor_threads_sync_members_offset (&owner_offset, &nest_offset, &dummy);
817 g_assert (MONO_THREADS_SYNC_MEMBER_SIZE (owner_offset) == sizeof (gpointer));
818 g_assert (MONO_THREADS_SYNC_MEMBER_SIZE (nest_offset) == sizeof (guint32));
819 owner_offset = MONO_THREADS_SYNC_MEMBER_OFFSET (owner_offset);
820 nest_offset = MONO_THREADS_SYNC_MEMBER_OFFSET (nest_offset);
824 code = buf = mono_global_codeman_reserve (tramp_size);
826 unwind_ops = mono_arch_get_cie_program ();
828 if (mono_thread_get_tls_offset () != -1) {
829 /* MonoObject* obj is in RDI */
831 amd64_test_reg_reg (code, AMD64_RDI, AMD64_RDI);
832 /* if yes, jump to actual trampoline */
833 jump_obj_null = code;
834 amd64_branch8 (code, X86_CC_Z, -1, 1);
836 /* load obj->synchronization to RCX */
837 amd64_mov_reg_membase (code, AMD64_RCX, AMD64_RDI, G_STRUCT_OFFSET (MonoObject, synchronisation), 8);
839 if (mono_gc_is_moving ()) {
840 /*if bit zero is set it's a thin hash*/
841 /*FIXME use testb encoding*/
842 amd64_test_reg_imm (code, AMD64_RCX, 0x01);
843 jump_sync_thin_hash = code;
844 amd64_branch8 (code, X86_CC_NE, -1, 1);
846 /*clear bits used by the gc*/
847 amd64_alu_reg_imm (code, X86_AND, AMD64_RCX, ~0x3);
850 /* is synchronization null? */
851 amd64_test_reg_reg (code, AMD64_RCX, AMD64_RCX);
852 /* if yes, jump to actual trampoline */
853 jump_sync_null = code;
854 amd64_branch8 (code, X86_CC_Z, -1, 1);
856 /* load MonoInternalThread* into RDX */
857 code = mono_amd64_emit_tls_get (code, AMD64_RDX, mono_thread_get_tls_offset ());
858 /* load TID into RDX */
859 amd64_mov_reg_membase (code, AMD64_RDX, AMD64_RDX, G_STRUCT_OFFSET (MonoInternalThread, tid), 8);
861 /* is synchronization->owner null? */
862 amd64_alu_membase_imm_size (code, X86_CMP, AMD64_RCX, owner_offset, 0, 8);
863 /* if not, jump to next case */
865 amd64_branch8 (code, X86_CC_NZ, -1, 1);
867 /* if yes, try a compare-exchange with the TID */
869 amd64_alu_reg_reg (code, X86_XOR, AMD64_RAX, AMD64_RAX);
870 /* compare and exchange */
871 amd64_prefix (code, X86_LOCK_PREFIX);
872 amd64_cmpxchg_membase_reg_size (code, AMD64_RCX, owner_offset, AMD64_RDX, 8);
873 /* if not successful, jump to actual trampoline */
874 jump_cmpxchg_failed = code;
875 amd64_branch8 (code, X86_CC_NZ, -1, 1);
876 /* if successful, return */
879 /* next case: synchronization->owner is not null */
880 x86_patch (jump_tid, code);
881 /* is synchronization->owner == TID? */
882 amd64_alu_membase_reg_size (code, X86_CMP, AMD64_RCX, owner_offset, AMD64_RDX, 8);
883 /* if not, jump to actual trampoline */
884 jump_other_owner = code;
885 amd64_branch8 (code, X86_CC_NZ, -1, 1);
886 /* if yes, increment nest */
887 amd64_inc_membase_size (code, AMD64_RCX, nest_offset, 4);
891 x86_patch (jump_obj_null, code);
892 if (jump_sync_thin_hash)
893 x86_patch (jump_sync_thin_hash, code);
894 x86_patch (jump_sync_null, code);
895 x86_patch (jump_cmpxchg_failed, code);
896 x86_patch (jump_other_owner, code);
899 /* jump to the actual trampoline */
900 #if MONO_AMD64_ARG_REG1 != AMD64_RDI
901 amd64_mov_reg_reg (code, MONO_AMD64_ARG_REG1, AMD64_RDI);
905 code = mono_arch_emit_load_aotconst (buf, code, &ji, MONO_PATCH_INFO_JIT_ICALL_ADDR, "specific_trampoline_monitor_enter");
906 amd64_jump_reg (code, AMD64_R11);
908 tramp = mono_arch_create_specific_trampoline (NULL, MONO_TRAMPOLINE_MONITOR_ENTER, mono_get_root_domain (), NULL);
910 /* jump to the actual trampoline */
911 amd64_jump_code (code, tramp);
914 mono_arch_flush_icache (code, code - buf);
915 g_assert (code - buf <= tramp_size);
918 *info = mono_tramp_info_create (g_strdup_printf ("monitor_enter_trampoline"), buf, code - buf, ji, unwind_ops);
924 mono_arch_create_monitor_exit_trampoline (MonoTrampInfo **info, gboolean aot)
928 guint8 *jump_obj_null, *jump_have_waiters, *jump_sync_null, *jump_not_owned, *jump_sync_thin_hash = NULL;
931 int owner_offset, nest_offset, entry_count_offset;
932 MonoJumpInfo *ji = NULL;
933 GSList *unwind_ops = NULL;
935 g_assert (MONO_ARCH_MONITOR_OBJECT_REG == AMD64_RDI);
937 mono_monitor_threads_sync_members_offset (&owner_offset, &nest_offset, &entry_count_offset);
938 g_assert (MONO_THREADS_SYNC_MEMBER_SIZE (owner_offset) == sizeof (gpointer));
939 g_assert (MONO_THREADS_SYNC_MEMBER_SIZE (nest_offset) == sizeof (guint32));
940 g_assert (MONO_THREADS_SYNC_MEMBER_SIZE (entry_count_offset) == sizeof (gint32));
941 owner_offset = MONO_THREADS_SYNC_MEMBER_OFFSET (owner_offset);
942 nest_offset = MONO_THREADS_SYNC_MEMBER_OFFSET (nest_offset);
943 entry_count_offset = MONO_THREADS_SYNC_MEMBER_OFFSET (entry_count_offset);
947 code = buf = mono_global_codeman_reserve (tramp_size);
949 unwind_ops = mono_arch_get_cie_program ();
951 if (mono_thread_get_tls_offset () != -1) {
952 /* MonoObject* obj is in RDI */
954 amd64_test_reg_reg (code, AMD64_RDI, AMD64_RDI);
955 /* if yes, jump to actual trampoline */
956 jump_obj_null = code;
957 amd64_branch8 (code, X86_CC_Z, -1, 1);
959 /* load obj->synchronization to RCX */
960 amd64_mov_reg_membase (code, AMD64_RCX, AMD64_RDI, G_STRUCT_OFFSET (MonoObject, synchronisation), 8);
962 if (mono_gc_is_moving ()) {
963 /*if bit zero is set it's a thin hash*/
964 /*FIXME use testb encoding*/
965 amd64_test_reg_imm (code, AMD64_RCX, 0x01);
966 jump_sync_thin_hash = code;
967 amd64_branch8 (code, X86_CC_NE, -1, 1);
969 /*clear bits used by the gc*/
970 amd64_alu_reg_imm (code, X86_AND, AMD64_RCX, ~0x3);
973 /* is synchronization null? */
974 amd64_test_reg_reg (code, AMD64_RCX, AMD64_RCX);
975 /* if yes, jump to actual trampoline */
976 jump_sync_null = code;
977 amd64_branch8 (code, X86_CC_Z, -1, 1);
979 /* next case: synchronization is not null */
980 /* load MonoInternalThread* into RDX */
981 code = mono_amd64_emit_tls_get (code, AMD64_RDX, mono_thread_get_tls_offset ());
982 /* load TID into RDX */
983 amd64_mov_reg_membase (code, AMD64_RDX, AMD64_RDX, G_STRUCT_OFFSET (MonoInternalThread, tid), 8);
984 /* is synchronization->owner == TID */
985 amd64_alu_membase_reg_size (code, X86_CMP, AMD64_RCX, owner_offset, AMD64_RDX, 8);
986 /* if no, jump to actual trampoline */
987 jump_not_owned = code;
988 amd64_branch8 (code, X86_CC_NZ, -1, 1);
990 /* next case: synchronization->owner == TID */
991 /* is synchronization->nest == 1 */
992 amd64_alu_membase_imm_size (code, X86_CMP, AMD64_RCX, nest_offset, 1, 4);
993 /* if not, jump to next case */
995 amd64_branch8 (code, X86_CC_NZ, -1, 1);
996 /* if yes, is synchronization->entry_count zero? */
997 amd64_alu_membase_imm_size (code, X86_CMP, AMD64_RCX, entry_count_offset, 0, 4);
998 /* if not, jump to actual trampoline */
999 jump_have_waiters = code;
1000 amd64_branch8 (code, X86_CC_NZ, -1 , 1);
1001 /* if yes, set synchronization->owner to null and return */
1002 amd64_mov_membase_imm (code, AMD64_RCX, owner_offset, 0, 8);
1005 /* next case: synchronization->nest is not 1 */
1006 x86_patch (jump_next, code);
1007 /* decrease synchronization->nest and return */
1008 amd64_dec_membase_size (code, AMD64_RCX, nest_offset, 4);
1011 x86_patch (jump_obj_null, code);
1012 x86_patch (jump_have_waiters, code);
1013 x86_patch (jump_not_owned, code);
1014 x86_patch (jump_sync_null, code);
1017 /* jump to the actual trampoline */
1018 #if MONO_AMD64_ARG_REG1 != AMD64_RDI
1019 amd64_mov_reg_reg (code, MONO_AMD64_ARG_REG1, AMD64_RDI);
1023 code = mono_arch_emit_load_aotconst (buf, code, &ji, MONO_PATCH_INFO_JIT_ICALL_ADDR, "specific_trampoline_monitor_exit");
1024 amd64_jump_reg (code, AMD64_R11);
1026 tramp = mono_arch_create_specific_trampoline (NULL, MONO_TRAMPOLINE_MONITOR_EXIT, mono_get_root_domain (), NULL);
1027 amd64_jump_code (code, tramp);
1030 mono_arch_flush_icache (code, code - buf);
1031 g_assert (code - buf <= tramp_size);
1034 *info = mono_tramp_info_create (g_strdup_printf ("monitor_exit_trampoline"), buf, code - buf, ji, unwind_ops);
1041 mono_arch_invalidate_method (MonoJitInfo *ji, void *func, gpointer func_arg)
1043 /* FIXME: This is not thread safe */
1044 guint8 *code = ji->code_start;
1046 amd64_mov_reg_imm (code, AMD64_ARG_REG1, func_arg);
1047 amd64_mov_reg_imm (code, AMD64_R11, func);
1049 x86_push_imm (code, (guint64)func_arg);
1050 amd64_call_reg (code, AMD64_R11);
1055 handler_block_trampoline_helper (gpointer *ptr)
1057 MonoJitTlsData *jit_tls = TlsGetValue (mono_jit_tls_id);
1058 *ptr = jit_tls->handler_block_return_address;
1062 mono_arch_create_handler_block_trampoline (void)
1064 guint8 *tramp = mono_get_trampoline_code (MONO_TRAMPOLINE_HANDLER_BLOCK_GUARD);
1066 int tramp_size = 64;
1067 code = buf = mono_global_codeman_reserve (tramp_size);
1070 This trampoline restore the call chain of the handler block then jumps into the code that deals with it.
1073 if (mono_get_jit_tls_offset () != -1) {
1074 code = mono_amd64_emit_tls_get (code, AMD64_RDI, mono_get_jit_tls_offset ());
1076 amd64_mov_reg_membase (code, AMD64_RDI, AMD64_RDI, G_STRUCT_OFFSET (MonoJitTlsData, handler_block_return_address), 8);
1077 amd64_jump_code (code, tramp);
1079 /*Slow path uses a c helper*/
1080 amd64_mov_reg_reg (code, AMD64_RDI, AMD64_RSP, 8);
1081 amd64_mov_reg_imm (code, AMD64_RAX, tramp);
1082 amd64_push_reg (code, AMD64_RAX);
1083 amd64_jump_code (code, handler_block_trampoline_helper);
1086 mono_arch_flush_icache (buf, code - buf);
1087 g_assert (code - buf <= tramp_size);
1089 if (mono_jit_map_is_enabled ())
1090 mono_emit_jit_tramp (buf, code - buf, "handler_block_trampoline");
1096 * mono_arch_get_call_target:
1098 * Return the address called by the code before CODE if exists.
1101 mono_arch_get_call_target (guint8 *code)
1103 if (code [-5] == 0xe8) {
1104 guint32 disp = *(guint32*)(code - 4);
1105 guint8 *target = code + disp;
1114 * mono_arch_get_plt_info_offset:
1116 * Return the PLT info offset belonging to the plt entry PLT_ENTRY.
1119 mono_arch_get_plt_info_offset (guint8 *plt_entry, mgreg_t *regs, guint8 *code)
1121 return *(guint32*)(plt_entry + 6);