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);
91 fprintf (stderr, "Addr: %p\n", addr);
92 g_assert_not_reached ();
94 g_assert ((((guint64)(orig_code)) >> 32) == 0);
96 InterlockedExchange ((gint32*)(orig_code - 4), ((gint64)addr - (gint64)orig_code));
99 else if ((code [-7] == 0x41) && (code [-6] == 0xff) && (code [-5] == 0x15)) {
100 /* call *<OFFSET>(%rip) */
101 gpointer *got_entry = (gpointer*)((guint8*)orig_code + (*(guint32*)(orig_code - 4)));
103 InterlockedExchangePointer (got_entry, addr);
108 mono_arch_patch_plt_entry (guint8 *code, guint8 *addr)
111 gpointer *plt_jump_table_entry;
113 /* A PLT entry: jmp *<DISP>(%rip) */
114 g_assert (code [0] == 0xff);
115 g_assert (code [1] == 0x25);
117 disp = *(gint32*)(code + 2);
119 plt_jump_table_entry = (gpointer*)(code + 6 + disp);
121 InterlockedExchangePointer (plt_jump_table_entry, addr);
125 mono_arch_nullify_class_init_trampoline (guint8 *code, gssize *regs)
130 * A given byte sequence can match more than case here, so we have to be
131 * really careful about the ordering of the cases. Longer sequences
134 if ((code [-4] == 0x41) && (code [-3] == 0xff) && (code [-2] == 0x15)) {
135 gpointer *vtable_slot;
137 /* call *<OFFSET>(%rip) */
138 vtable_slot = mono_arch_get_vcall_slot_addr (code + 3, (gpointer*)regs);
139 g_assert (vtable_slot);
141 *vtable_slot = nullified_class_init_trampoline;
142 } else if (code [-2] == 0xe8) {
144 guint8 *buf = code - 2;
151 } else if ((code [0] == 0x41) && (code [1] == 0xff)) {
153 /* happens on machines without MAP_32BIT like freebsd */
154 /* amd64_set_reg_template is 10 bytes long */
155 guint8* buf = code - 10;
157 /* FIXME: Make this thread safe */
158 /* Padding code suggested by the AMD64 Opt Manual */
172 } else if (code [0] == 0x90 || code [0] == 0xeb || code [0] == 0x66) {
173 /* Already changed by another thread */
176 printf ("Invalid trampoline sequence: %x %x %x %x %x %x %x\n", code [0], code [1], code [2], code [3],
177 code [4], code [5], code [6]);
178 g_assert_not_reached ();
183 mono_arch_nullify_plt_entry (guint8 *code)
185 mono_arch_patch_plt_entry (code, nullified_class_init_trampoline);
189 mono_arch_create_trampoline_code (MonoTrampolineType tramp_type)
191 guint8 *buf, *code, *tramp, *br [2];
192 int i, lmf_offset, offset, res_offset, arg_offset, tramp_offset, saved_regs_offset, saved_fpregs_offset, framesize;
195 if (tramp_type == MONO_TRAMPOLINE_JUMP)
200 code = buf = mono_global_codeman_reserve (512);
202 framesize = 512 + sizeof (MonoLMF);
203 framesize = (framesize + (MONO_ARCH_FRAME_ALIGNMENT - 1)) & ~ (MONO_ARCH_FRAME_ALIGNMENT - 1);
208 * The generic class init trampoline is called directly by JITted code, there is no
209 * specific trampoline.
211 if (tramp_type != MONO_TRAMPOLINE_GENERIC_CLASS_INIT) {
212 /* Pop the return address off the stack */
213 amd64_pop_reg (code, AMD64_R11);
217 * Allocate a new stack frame
219 amd64_push_reg (code, AMD64_RBP);
220 amd64_mov_reg_reg (code, AMD64_RBP, AMD64_RSP, 8);
221 amd64_alu_reg_imm (code, X86_SUB, AMD64_RSP, framesize);
224 tramp_offset = - offset;
227 arg_offset = - offset;
229 if (tramp_type != MONO_TRAMPOLINE_GENERIC_CLASS_INIT) {
230 /* Compute the trampoline address from the return address */
231 /* 5 = length of amd64_call_membase () */
232 amd64_alu_reg_imm (code, X86_SUB, AMD64_R11, 5);
233 amd64_mov_membase_reg (code, AMD64_RBP, tramp_offset, AMD64_R11, 8);
235 amd64_mov_membase_imm (code, AMD64_RBP, tramp_offset, 0, 8);
239 res_offset = - offset;
241 /* Save all registers */
243 offset += AMD64_NREG * 8;
244 saved_regs_offset = - offset;
245 for (i = 0; i < AMD64_NREG; ++i)
246 amd64_mov_membase_reg (code, AMD64_RBP, saved_regs_offset + (i * 8), i, 8);
248 saved_fpregs_offset = - offset;
249 for (i = 0; i < 8; ++i)
250 amd64_movsd_membase_reg (code, AMD64_RBP, saved_fpregs_offset + (i * 8), i);
252 if (tramp_type != MONO_TRAMPOLINE_GENERIC_CLASS_INIT) {
253 /* Obtain the trampoline argument which is encoded in the instruction stream */
254 amd64_mov_reg_membase (code, AMD64_R11, AMD64_RBP, tramp_offset, 8);
255 amd64_mov_reg_membase (code, AMD64_RAX, AMD64_R11, 5, 1);
256 amd64_widen_reg (code, AMD64_RAX, AMD64_RAX, TRUE, FALSE);
257 amd64_alu_reg_imm_size (code, X86_CMP, AMD64_RAX, 4, 1);
259 x86_branch8 (code, X86_CC_NE, 6, FALSE);
260 /* 32 bit immediate */
261 amd64_mov_reg_membase (code, AMD64_R11, AMD64_R11, 6, 4);
263 x86_jump8 (code, 10);
264 /* 64 bit immediate */
265 mono_amd64_patch (br [0], code);
266 amd64_mov_reg_membase (code, AMD64_R11, AMD64_R11, 6, 8);
267 mono_amd64_patch (br [1], code);
268 amd64_mov_membase_reg (code, AMD64_RBP, arg_offset, AMD64_R11, 8);
270 amd64_mov_membase_reg (code, AMD64_RBP, arg_offset, MONO_ARCH_VTABLE_REG, 8);
275 offset += sizeof (MonoLMF);
276 lmf_offset = - offset;
280 amd64_mov_reg_membase (code, AMD64_R11, AMD64_RBP, 8, 8);
282 amd64_mov_reg_imm (code, AMD64_R11, 0);
283 amd64_mov_membase_reg (code, AMD64_RBP, lmf_offset + G_STRUCT_OFFSET (MonoLMF, rip), AMD64_R11, 8);
285 amd64_mov_reg_membase (code, AMD64_R11, AMD64_RSP, framesize, 8);
286 amd64_mov_membase_reg (code, AMD64_RBP, lmf_offset + G_STRUCT_OFFSET (MonoLMF, rbp), AMD64_R11, 8);
288 amd64_mov_reg_reg (code, AMD64_R11, AMD64_RSP, 8);
289 amd64_alu_reg_imm (code, X86_ADD, AMD64_R11, framesize + 16);
290 amd64_mov_membase_reg (code, AMD64_RBP, lmf_offset + G_STRUCT_OFFSET (MonoLMF, rsp), AMD64_R11, 8);
292 if (tramp_type == MONO_TRAMPOLINE_GENERIC || tramp_type == MONO_TRAMPOLINE_JUMP) {
293 amd64_mov_reg_membase (code, AMD64_R11, AMD64_RBP, arg_offset, 8);
294 amd64_mov_membase_reg (code, AMD64_RBP, lmf_offset + G_STRUCT_OFFSET (MonoLMF, method), AMD64_R11, 8);
296 amd64_mov_membase_imm (code, AMD64_RBP, lmf_offset + G_STRUCT_OFFSET (MonoLMF, method), 0, 8);
298 /* Save callee saved regs */
299 #ifdef PLATFORM_WIN32
300 amd64_mov_membase_reg (code, AMD64_RBP, lmf_offset + G_STRUCT_OFFSET (MonoLMF, rdi), AMD64_RDI, 8);
301 amd64_mov_membase_reg (code, AMD64_RBP, lmf_offset + G_STRUCT_OFFSET (MonoLMF, rsi), AMD64_RSI, 8);
303 amd64_mov_membase_reg (code, AMD64_RBP, lmf_offset + G_STRUCT_OFFSET (MonoLMF, rbx), AMD64_RBX, 8);
304 amd64_mov_membase_reg (code, AMD64_RBP, lmf_offset + G_STRUCT_OFFSET (MonoLMF, r12), AMD64_R12, 8);
305 amd64_mov_membase_reg (code, AMD64_RBP, lmf_offset + G_STRUCT_OFFSET (MonoLMF, r13), AMD64_R13, 8);
306 amd64_mov_membase_reg (code, AMD64_RBP, lmf_offset + G_STRUCT_OFFSET (MonoLMF, r14), AMD64_R14, 8);
307 amd64_mov_membase_reg (code, AMD64_RBP, lmf_offset + G_STRUCT_OFFSET (MonoLMF, r15), AMD64_R15, 8);
309 amd64_mov_reg_imm (code, AMD64_R11, mono_get_lmf_addr);
310 amd64_call_reg (code, AMD64_R11);
313 amd64_mov_membase_reg (code, AMD64_RBP, lmf_offset + G_STRUCT_OFFSET (MonoLMF, lmf_addr), AMD64_RAX, 8);
314 /* Save previous_lmf */
315 /* Set the lowest bit to 1 to signal that this LMF has the ip field set */
316 amd64_mov_reg_membase (code, AMD64_R11, AMD64_RAX, 0, 8);
317 amd64_alu_reg_imm_size (code, X86_ADD, AMD64_R11, 1, 8);
318 amd64_mov_membase_reg (code, AMD64_RBP, lmf_offset + G_STRUCT_OFFSET (MonoLMF, previous_lmf), AMD64_R11, 8);
320 amd64_lea_membase (code, AMD64_R11, AMD64_RBP, lmf_offset);
321 amd64_mov_membase_reg (code, AMD64_RAX, 0, AMD64_R11, 8);
325 /* Arg1 is the pointer to the saved registers */
326 amd64_lea_membase (code, AMD64_ARG_REG1, AMD64_RBP, saved_regs_offset);
328 /* Arg2 is the address of the calling code */
330 amd64_mov_reg_membase (code, AMD64_ARG_REG2, AMD64_RBP, 8, 8);
332 amd64_mov_reg_imm (code, AMD64_ARG_REG2, 0);
334 /* Arg3 is the method/vtable ptr */
335 amd64_mov_reg_membase (code, AMD64_ARG_REG3, AMD64_RBP, arg_offset, 8);
337 /* Arg4 is the trampoline address */
338 amd64_mov_reg_membase (code, AMD64_ARG_REG4, AMD64_RBP, tramp_offset, 8);
340 tramp = (guint8*)mono_get_trampoline_func (tramp_type);
341 amd64_mov_reg_imm (code, AMD64_RAX, tramp);
342 amd64_call_reg (code, AMD64_RAX);
344 /* Check for thread interruption */
345 /* This is not perf critical code so no need to check the interrupt flag */
346 amd64_mov_membase_reg (code, AMD64_RBP, res_offset, AMD64_RAX, 8);
347 amd64_mov_reg_imm (code, AMD64_RAX, (guint8*)mono_thread_interruption_checkpoint);
348 amd64_call_reg (code, AMD64_RAX);
349 amd64_mov_reg_membase (code, AMD64_RAX, AMD64_RBP, res_offset, 8);
353 amd64_mov_reg_membase (code, AMD64_RCX, AMD64_RBP, lmf_offset + G_STRUCT_OFFSET (MonoLMF, previous_lmf), 8);
354 amd64_alu_reg_imm_size (code, X86_SUB, AMD64_RCX, 1, 8);
355 amd64_mov_reg_membase (code, AMD64_R11, AMD64_RBP, lmf_offset + G_STRUCT_OFFSET (MonoLMF, lmf_addr), 8);
356 amd64_mov_membase_reg (code, AMD64_R11, 0, AMD64_RCX, 8);
358 /* Restore argument registers */
359 for (i = 0; i < AMD64_NREG; ++i)
360 if (AMD64_IS_ARGUMENT_REG (i))
361 amd64_mov_reg_membase (code, i, AMD64_RBP, saved_regs_offset + (i * 8), 8);
363 for (i = 0; i < 8; ++i)
364 amd64_movsd_reg_membase (code, i, AMD64_RBP, saved_fpregs_offset + (i * 8));
369 if (tramp_type == MONO_TRAMPOLINE_CLASS_INIT || tramp_type == MONO_TRAMPOLINE_GENERIC_CLASS_INIT)
372 /* call the compiled method */
373 amd64_jump_reg (code, X86_EAX);
375 g_assert ((code - buf) <= 512);
377 mono_arch_flush_icache (buf, code - buf);
379 if (tramp_type == MONO_TRAMPOLINE_CLASS_INIT) {
380 /* Initialize the nullified class init trampoline used in the AOT case */
381 nullified_class_init_trampoline = code = mono_global_codeman_reserve (16);
389 mono_arch_create_specific_trampoline (gpointer arg1, MonoTrampolineType tramp_type, MonoDomain *domain, guint32 *code_len)
391 guint8 *code, *buf, *tramp;
394 tramp = mono_get_trampoline_code (tramp_type);
396 if ((((guint64)arg1) >> 32) == 0)
401 mono_domain_lock (domain);
402 code = buf = mono_code_manager_reserve_align (domain->code_mp, size, 1);
403 mono_domain_unlock (domain);
405 amd64_call_code (code, tramp);
406 /* The trampoline code will obtain the argument from the instruction stream */
407 if ((((guint64)arg1) >> 32) == 0) {
409 *(guint32*)(code + 1) = (gint64)arg1;
413 *(guint64*)(code + 1) = (gint64)arg1;
417 g_assert ((code - buf) <= size);
422 mono_arch_flush_icache (buf, size);
428 mono_arch_invalidate_method (MonoJitInfo *ji, void *func, gpointer func_arg)
430 /* FIXME: This is not thread safe */
431 guint8 *code = ji->code_start;
433 amd64_mov_reg_imm (code, AMD64_ARG_REG1, func_arg);
434 amd64_mov_reg_imm (code, AMD64_R11, func);
436 x86_push_imm (code, (guint64)func_arg);
437 amd64_call_reg (code, AMD64_R11);
441 * This method is only called when running in the Mono Debugger.
444 mono_debugger_create_notification_function (void)
448 code = buf = mono_global_codeman_reserve (2);
449 x86_breakpoint (buf);