2 * tramp-x86.c: JIT trampoline code for x86
5 * Dietmar Maurer (dietmar@ximian.com)
7 * (C) 2001 Ximian, Inc.
13 #include <mono/metadata/appdomain.h>
14 #include <mono/metadata/marshal.h>
15 #include <mono/metadata/tabledefs.h>
16 #include <mono/metadata/mono-debug-debugger.h>
17 #include <mono/arch/amd64/amd64-codegen.h>
19 #ifdef HAVE_VALGRIND_MEMCHECK_H
20 #include <valgrind/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:
33 * @addr: pointer to native code for @m
35 * when value type methods are called through the vtable we need to unbox the
36 * this argument. This method returns a pointer to a trampoline which does
37 * unboxing before calling the method
40 mono_arch_get_unbox_trampoline (MonoMethod *m, gpointer addr)
43 int this_reg = AMD64_ARG_REG1;
45 MonoDomain *domain = mono_domain_get ();
47 if (!mono_method_signature (m)->ret->byref && MONO_TYPE_ISSTRUCT (mono_method_signature (m)->ret))
48 this_reg = AMD64_ARG_REG2;
50 mono_domain_lock (domain);
51 start = code = mono_code_manager_reserve (domain->code_mp, 20);
52 mono_domain_unlock (domain);
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_patch_callsite:
68 * Patch the callsite whose address is given by ORIG_CODE so it calls ADDR. ORIG_CODE
69 * points to the pc right after the call.
72 mono_arch_patch_callsite (guint8 *orig_code, guint8 *addr)
76 gboolean can_write = mono_breakpoint_clean_code (orig_code - 14, buf, sizeof (buf));
80 if (((code [-13] == 0x49) && (code [-12] == 0xbb)) || (code [-5] == 0xe8)) {
81 if (code [-5] != 0xe8) {
83 InterlockedExchangePointer ((gpointer*)(orig_code - 11), addr);
85 if ((((guint64)(addr)) >> 32) != 0) {
86 /* Print some diagnostics */
87 MonoJitInfo *ji = mono_jit_info_table_find (mono_domain_get (), (char*)orig_code);
89 fprintf (stderr, "At %s, offset 0x%zx\n", mono_method_full_name (ji->method, TRUE), (guint8*)orig_code - (guint8*)ji->code_start);
90 fprintf (stderr, "Addr: %p\n", addr);
91 ji = mono_jit_info_table_find (mono_domain_get (), (char*)addr);
93 fprintf (stderr, "Callee: %s\n", mono_method_full_name (ji->method, TRUE));
94 g_assert_not_reached ();
96 g_assert ((((guint64)(orig_code)) >> 32) == 0);
98 InterlockedExchange ((gint32*)(orig_code - 4), ((gint64)addr - (gint64)orig_code));
101 else if ((code [-7] == 0x41) && (code [-6] == 0xff) && (code [-5] == 0x15)) {
102 /* call *<OFFSET>(%rip) */
103 gpointer *got_entry = (gpointer*)((guint8*)orig_code + (*(guint32*)(orig_code - 4)));
105 InterlockedExchangePointer (got_entry, addr);
110 mono_arch_patch_plt_entry (guint8 *code, guint8 *addr)
113 gpointer *plt_jump_table_entry;
115 /* A PLT entry: jmp *<DISP>(%rip) */
116 g_assert (code [0] == 0xff);
117 g_assert (code [1] == 0x25);
119 disp = *(gint32*)(code + 2);
121 plt_jump_table_entry = (gpointer*)(code + 6 + disp);
123 InterlockedExchangePointer (plt_jump_table_entry, addr);
127 mono_arch_nullify_class_init_trampoline (guint8 *code, gssize *regs)
132 * A given byte sequence can match more than case here, so we have to be
133 * really careful about the ordering of the cases. Longer sequences
136 if ((code [-4] == 0x41) && (code [-3] == 0xff) && (code [-2] == 0x15)) {
137 gpointer *vtable_slot;
139 /* call *<OFFSET>(%rip) */
140 vtable_slot = mono_arch_get_vcall_slot_addr (code + 3, (gpointer*)regs);
141 g_assert (vtable_slot);
143 *vtable_slot = nullified_class_init_trampoline;
144 } else if (code [-2] == 0xe8) {
146 guint8 *buf = code - 2;
153 } else if ((code [0] == 0x41) && (code [1] == 0xff)) {
155 /* happens on machines without MAP_32BIT like freebsd */
156 /* amd64_set_reg_template is 10 bytes long */
157 guint8* buf = code - 10;
159 /* FIXME: Make this thread safe */
160 /* Padding code suggested by the AMD64 Opt Manual */
174 } else if (code [0] == 0x90 || code [0] == 0xeb || code [0] == 0x66) {
175 /* Already changed by another thread */
178 printf ("Invalid trampoline sequence: %x %x %x %x %x %x %x\n", code [0], code [1], code [2], code [3],
179 code [4], code [5], code [6]);
180 g_assert_not_reached ();
185 mono_arch_nullify_plt_entry (guint8 *code)
187 mono_arch_patch_plt_entry (code, nullified_class_init_trampoline);
191 mono_arch_create_trampoline_code (MonoTrampolineType tramp_type)
193 guint8 *buf, *code, *tramp, *br [2];
194 int i, lmf_offset, offset, res_offset, arg_offset, tramp_offset, saved_regs_offset, saved_fpregs_offset, framesize;
197 if (tramp_type == MONO_TRAMPOLINE_JUMP)
202 code = buf = mono_global_codeman_reserve (512);
204 framesize = 512 + sizeof (MonoLMF);
205 framesize = (framesize + (MONO_ARCH_FRAME_ALIGNMENT - 1)) & ~ (MONO_ARCH_FRAME_ALIGNMENT - 1);
209 if (tramp_type == MONO_TRAMPOLINE_GENERIC_CLASS_INIT) {
210 static int byte_offset = -1;
211 static guint8 bitmask;
216 mono_marshal_find_bitfield_offset (MonoVTable, initialized, &byte_offset, &bitmask);
218 amd64_test_membase_imm_size (code, MONO_ARCH_VTABLE_REG, byte_offset, bitmask, 1);
220 amd64_branch8 (code, X86_CC_Z, -1, 1);
224 x86_patch (jump, code);
228 * The generic class init trampoline is called directly by
229 * JITted code, there is no specific trampoline. The lazy
230 * fetch trampolines behave like generic class init
233 if (tramp_type != MONO_TRAMPOLINE_GENERIC_CLASS_INIT &&
234 tramp_type != MONO_TRAMPOLINE_RGCTX_LAZY_FETCH) {
235 /* Pop the return address off the stack */
236 amd64_pop_reg (code, AMD64_R11);
240 * Allocate a new stack frame
242 amd64_push_reg (code, AMD64_RBP);
243 amd64_mov_reg_reg (code, AMD64_RBP, AMD64_RSP, 8);
244 amd64_alu_reg_imm (code, X86_SUB, AMD64_RSP, framesize);
247 tramp_offset = - offset;
250 arg_offset = - offset;
252 if (tramp_type != MONO_TRAMPOLINE_GENERIC_CLASS_INIT &&
253 tramp_type != MONO_TRAMPOLINE_RGCTX_LAZY_FETCH) {
254 /* Compute the trampoline address from the return address */
255 /* 5 = length of amd64_call_membase () */
256 amd64_alu_reg_imm (code, X86_SUB, AMD64_R11, 5);
257 amd64_mov_membase_reg (code, AMD64_RBP, tramp_offset, AMD64_R11, 8);
259 amd64_mov_membase_imm (code, AMD64_RBP, tramp_offset, 0, 8);
263 res_offset = - offset;
265 /* Save all registers */
267 offset += AMD64_NREG * 8;
268 saved_regs_offset = - offset;
269 for (i = 0; i < AMD64_NREG; ++i)
270 amd64_mov_membase_reg (code, AMD64_RBP, saved_regs_offset + (i * 8), i, 8);
272 saved_fpregs_offset = - offset;
273 for (i = 0; i < 8; ++i)
274 amd64_movsd_membase_reg (code, AMD64_RBP, saved_fpregs_offset + (i * 8), i);
276 if (tramp_type != MONO_TRAMPOLINE_GENERIC_CLASS_INIT &&
277 tramp_type != MONO_TRAMPOLINE_RGCTX_LAZY_FETCH) {
278 /* Obtain the trampoline argument which is encoded in the instruction stream */
279 amd64_mov_reg_membase (code, AMD64_R11, AMD64_RBP, tramp_offset, 8);
280 amd64_mov_reg_membase (code, AMD64_RAX, AMD64_R11, 5, 1);
281 amd64_widen_reg (code, AMD64_RAX, AMD64_RAX, TRUE, FALSE);
282 amd64_alu_reg_imm_size (code, X86_CMP, AMD64_RAX, 4, 1);
284 x86_branch8 (code, X86_CC_NE, 6, FALSE);
285 /* 32 bit immediate */
286 amd64_mov_reg_membase (code, AMD64_R11, AMD64_R11, 6, 4);
288 x86_jump8 (code, 10);
289 /* 64 bit immediate */
290 mono_amd64_patch (br [0], code);
291 amd64_mov_reg_membase (code, AMD64_R11, AMD64_R11, 6, 8);
292 mono_amd64_patch (br [1], code);
293 amd64_mov_membase_reg (code, AMD64_RBP, arg_offset, AMD64_R11, 8);
295 amd64_mov_membase_reg (code, AMD64_RBP, arg_offset, MONO_ARCH_VTABLE_REG, 8);
300 offset += sizeof (MonoLMF);
301 lmf_offset = - offset;
305 amd64_mov_reg_membase (code, AMD64_R11, AMD64_RBP, 8, 8);
307 amd64_mov_reg_imm (code, AMD64_R11, 0);
308 amd64_mov_membase_reg (code, AMD64_RBP, lmf_offset + G_STRUCT_OFFSET (MonoLMF, rip), AMD64_R11, 8);
310 amd64_mov_reg_membase (code, AMD64_R11, AMD64_RSP, framesize, 8);
311 amd64_mov_membase_reg (code, AMD64_RBP, lmf_offset + G_STRUCT_OFFSET (MonoLMF, rbp), AMD64_R11, 8);
313 amd64_mov_reg_reg (code, AMD64_R11, AMD64_RSP, 8);
314 amd64_alu_reg_imm (code, X86_ADD, AMD64_R11, framesize + 16);
315 amd64_mov_membase_reg (code, AMD64_RBP, lmf_offset + G_STRUCT_OFFSET (MonoLMF, rsp), AMD64_R11, 8);
317 if (tramp_type == MONO_TRAMPOLINE_GENERIC || tramp_type == MONO_TRAMPOLINE_JUMP) {
318 amd64_mov_reg_membase (code, AMD64_R11, AMD64_RBP, arg_offset, 8);
319 amd64_mov_membase_reg (code, AMD64_RBP, lmf_offset + G_STRUCT_OFFSET (MonoLMF, method), AMD64_R11, 8);
321 amd64_mov_membase_imm (code, AMD64_RBP, lmf_offset + G_STRUCT_OFFSET (MonoLMF, method), 0, 8);
323 /* Save callee saved regs */
324 #ifdef PLATFORM_WIN32
325 amd64_mov_membase_reg (code, AMD64_RBP, lmf_offset + G_STRUCT_OFFSET (MonoLMF, rdi), AMD64_RDI, 8);
326 amd64_mov_membase_reg (code, AMD64_RBP, lmf_offset + G_STRUCT_OFFSET (MonoLMF, rsi), AMD64_RSI, 8);
328 amd64_mov_membase_reg (code, AMD64_RBP, lmf_offset + G_STRUCT_OFFSET (MonoLMF, rbx), AMD64_RBX, 8);
329 amd64_mov_membase_reg (code, AMD64_RBP, lmf_offset + G_STRUCT_OFFSET (MonoLMF, r12), AMD64_R12, 8);
330 amd64_mov_membase_reg (code, AMD64_RBP, lmf_offset + G_STRUCT_OFFSET (MonoLMF, r13), AMD64_R13, 8);
331 amd64_mov_membase_reg (code, AMD64_RBP, lmf_offset + G_STRUCT_OFFSET (MonoLMF, r14), AMD64_R14, 8);
332 amd64_mov_membase_reg (code, AMD64_RBP, lmf_offset + G_STRUCT_OFFSET (MonoLMF, r15), AMD64_R15, 8);
334 amd64_mov_reg_imm (code, AMD64_R11, mono_get_lmf_addr);
335 amd64_call_reg (code, AMD64_R11);
338 amd64_mov_membase_reg (code, AMD64_RBP, lmf_offset + G_STRUCT_OFFSET (MonoLMF, lmf_addr), AMD64_RAX, 8);
339 /* Save previous_lmf */
340 /* Set the lowest bit to 1 to signal that this LMF has the ip field set */
341 amd64_mov_reg_membase (code, AMD64_R11, AMD64_RAX, 0, 8);
342 amd64_alu_reg_imm_size (code, X86_ADD, AMD64_R11, 1, 8);
343 amd64_mov_membase_reg (code, AMD64_RBP, lmf_offset + G_STRUCT_OFFSET (MonoLMF, previous_lmf), AMD64_R11, 8);
345 amd64_lea_membase (code, AMD64_R11, AMD64_RBP, lmf_offset);
346 amd64_mov_membase_reg (code, AMD64_RAX, 0, AMD64_R11, 8);
350 /* Arg1 is the pointer to the saved registers */
351 amd64_lea_membase (code, AMD64_ARG_REG1, AMD64_RBP, saved_regs_offset);
353 /* Arg2 is the address of the calling code */
355 amd64_mov_reg_membase (code, AMD64_ARG_REG2, AMD64_RBP, 8, 8);
357 amd64_mov_reg_imm (code, AMD64_ARG_REG2, 0);
359 /* Arg3 is the method/vtable ptr */
360 amd64_mov_reg_membase (code, AMD64_ARG_REG3, AMD64_RBP, arg_offset, 8);
362 /* Arg4 is the trampoline address */
363 amd64_mov_reg_membase (code, AMD64_ARG_REG4, AMD64_RBP, tramp_offset, 8);
365 tramp = (guint8*)mono_get_trampoline_func (tramp_type);
366 amd64_mov_reg_imm (code, AMD64_RAX, tramp);
367 amd64_call_reg (code, AMD64_RAX);
369 /* Check for thread interruption */
370 /* This is not perf critical code so no need to check the interrupt flag */
371 amd64_mov_membase_reg (code, AMD64_RBP, res_offset, AMD64_RAX, 8);
372 amd64_mov_reg_imm (code, AMD64_RAX, (guint8*)mono_thread_interruption_checkpoint);
373 amd64_call_reg (code, AMD64_RAX);
374 amd64_mov_reg_membase (code, AMD64_RAX, AMD64_RBP, res_offset, 8);
378 amd64_mov_reg_membase (code, AMD64_RCX, AMD64_RBP, lmf_offset + G_STRUCT_OFFSET (MonoLMF, previous_lmf), 8);
379 amd64_alu_reg_imm_size (code, X86_SUB, AMD64_RCX, 1, 8);
380 amd64_mov_reg_membase (code, AMD64_R11, AMD64_RBP, lmf_offset + G_STRUCT_OFFSET (MonoLMF, lmf_addr), 8);
381 amd64_mov_membase_reg (code, AMD64_R11, 0, AMD64_RCX, 8);
383 /* Restore argument registers */
384 for (i = 0; i < AMD64_NREG; ++i)
385 if (AMD64_IS_ARGUMENT_REG (i))
386 amd64_mov_reg_membase (code, i, AMD64_RBP, saved_regs_offset + (i * 8), 8);
388 for (i = 0; i < 8; ++i)
389 amd64_movsd_reg_membase (code, i, AMD64_RBP, saved_fpregs_offset + (i * 8));
394 if (tramp_type == MONO_TRAMPOLINE_CLASS_INIT ||
395 tramp_type == MONO_TRAMPOLINE_GENERIC_CLASS_INIT ||
396 tramp_type == MONO_TRAMPOLINE_RGCTX_LAZY_FETCH)
399 /* call the compiled method */
400 amd64_jump_reg (code, X86_EAX);
402 g_assert ((code - buf) <= 512);
404 mono_arch_flush_icache (buf, code - buf);
406 if (tramp_type == MONO_TRAMPOLINE_CLASS_INIT) {
407 /* Initialize the nullified class init trampoline used in the AOT case */
408 nullified_class_init_trampoline = code = mono_global_codeman_reserve (16);
416 mono_arch_create_specific_trampoline (gpointer arg1, MonoTrampolineType tramp_type, MonoDomain *domain, guint32 *code_len)
418 guint8 *code, *buf, *tramp;
421 tramp = mono_get_trampoline_code (tramp_type);
423 if ((((guint64)arg1) >> 32) == 0)
428 mono_domain_lock (domain);
429 code = buf = mono_code_manager_reserve_align (domain->code_mp, size, 1);
430 mono_domain_unlock (domain);
432 amd64_call_code (code, tramp);
433 /* The trampoline code will obtain the argument from the instruction stream */
434 if ((((guint64)arg1) >> 32) == 0) {
436 *(guint32*)(code + 1) = (gint64)arg1;
440 *(guint64*)(code + 1) = (gint64)arg1;
444 g_assert ((code - buf) <= size);
449 mono_arch_flush_icache (buf, size);
455 mono_arch_create_rgctx_lazy_fetch_trampoline (guint32 encoded_offset)
457 guint8 *tramp = mono_get_trampoline_code (MONO_TRAMPOLINE_RGCTX_LAZY_FETCH);
458 gboolean indirect = MONO_RGCTX_OFFSET_IS_INDIRECT (encoded_offset);
459 int offset = indirect ? MONO_RGCTX_OFFSET_INDIRECT_OFFSET (encoded_offset) :
460 MONO_RGCTX_OFFSET_DIRECT_OFFSET (encoded_offset);
461 guint8 *code, *buf, *jump;
467 g_assert (MONO_RGCTX_OFFSET_INDIRECT_SLOT (encoded_offset) == 0);
469 code = buf = mono_global_codeman_reserve (32);
473 /* if indirect, load extra_other_infos ptr */
474 amd64_mov_reg_membase (buf, AMD64_RAX, AMD64_ARG_REG1, G_STRUCT_OFFSET (MonoRuntimeGenericContext, extra_other_infos), 8);
475 slots_reg = AMD64_RAX;
477 slots_reg = AMD64_ARG_REG1;
480 amd64_mov_reg_membase (buf, AMD64_RAX, slots_reg, offset, 8);
485 amd64_test_reg_reg (buf, AMD64_RAX, AMD64_RAX);
487 /* if yes, jump to actual trampoline */
488 amd64_branch8 (buf, X86_CC_Z, -1, 1);
490 /* if no, just return */
493 x86_patch (jump, buf);
494 /* move the rgctx pointer to the VTABLE register */
495 amd64_mov_reg_reg (buf, MONO_ARCH_VTABLE_REG, AMD64_ARG_REG1, 8);
496 /* store the offset in RAX */
497 amd64_mov_reg_imm (buf, AMD64_RAX, encoded_offset);
498 /* jump to the actual trampoline */
499 amd64_jump_code (buf, tramp);
501 mono_arch_flush_icache (code, buf - code);
503 g_assert (buf - code <= 32);
509 mono_arch_get_rgctx_lazy_fetch_offset (gpointer *regs)
511 return (guint32)(gulong)(regs [AMD64_RAX]);
515 mono_arch_invalidate_method (MonoJitInfo *ji, void *func, gpointer func_arg)
517 /* FIXME: This is not thread safe */
518 guint8 *code = ji->code_start;
520 amd64_mov_reg_imm (code, AMD64_ARG_REG1, func_arg);
521 amd64_mov_reg_imm (code, AMD64_R11, func);
523 x86_push_imm (code, (guint64)func_arg);
524 amd64_call_reg (code, AMD64_R11);
528 * This method is only called when running in the Mono Debugger.
531 mono_debugger_create_notification_function (void)
535 code = buf = mono_global_codeman_reserve (2);
536 x86_breakpoint (buf);