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/arch/amd64/amd64-codegen.h>
17 #include <mono/metadata/mono-debug-debugger.h>
19 #ifdef HAVE_VALGRIND_MEMCHECK_H
20 #include <valgrind/memcheck.h>
24 #include "mini-amd64.h"
27 * get_unbox_trampoline:
29 * @addr: pointer to native code for @m
31 * when value type methods are called through the vtable we need to unbox the
32 * this argument. This method returns a pointer to a trampoline which does
33 * unboxing before calling the method
36 get_unbox_trampoline (MonoMethod *m, gpointer addr)
39 int this_reg = AMD64_RDI;
40 MonoDomain *domain = mono_domain_get ();
42 if (!mono_method_signature (m)->ret->byref && MONO_TYPE_ISSTRUCT (mono_method_signature (m)->ret))
45 mono_domain_lock (domain);
46 start = code = mono_code_manager_reserve (domain->code_mp, 20);
47 mono_domain_unlock (domain);
49 amd64_alu_reg_imm (code, X86_ADD, this_reg, sizeof (MonoObject));
50 /* FIXME: Optimize this */
51 amd64_mov_reg_imm (code, AMD64_RAX, addr);
52 amd64_jump_reg (code, AMD64_RAX);
53 g_assert ((code - start) < 20);
55 mono_arch_flush_icache (start, code - start);
61 * amd64_magic_trampoline:
64 amd64_magic_trampoline (long *regs, guint8 *code, MonoMethod *m, guint8* tramp)
67 gpointer *vtable_slot;
69 addr = mono_compile_method (m);
72 //printf ("ENTER: %s\n", mono_method_full_name (m, TRUE));
74 /* the method was jumped to */
76 /* FIXME: Optimize the case when the call is from a delegate wrapper */
79 vtable_slot = mono_arch_get_vcall_slot_addr (code, (gpointer*)regs);
82 if (m->klass->valuetype)
83 addr = get_unbox_trampoline (m, addr);
85 g_assert (*vtable_slot);
87 if (mono_aot_is_got_entry (code, (guint8*)vtable_slot) || mono_domain_owns_vtable_slot (mono_domain_get (), vtable_slot))
91 /* Patch calling code */
93 if (((code [-13] == 0x49) && (code [-12] == 0xbb)) ||
94 (code [-5] == 0xe8)) {
96 mono_jit_info_table_find (mono_domain_get (), code);
97 MonoJitInfo *target_ji =
98 mono_jit_info_table_find (mono_domain_get (), addr);
100 if (mono_method_same_domain (ji, target_ji)) {
101 if (code [-5] != 0xe8)
102 InterlockedExchangePointer ((gpointer*)(code - 11), addr);
104 g_assert ((((guint64)(addr)) >> 32) == 0);
105 g_assert ((((guint64)(code)) >> 32) == 0);
106 InterlockedExchange ((guint32*)(code - 4), ((gint64)addr - (gint64)code));
110 else if ((code [-7] == 0x41) && (code [-6] == 0xff) && (code [-5] == 0x15)) {
111 /* call *<OFFSET>(%rip) */
113 mono_jit_info_table_find (mono_domain_get (), code);
114 MonoJitInfo *target_ji =
115 mono_jit_info_table_find (mono_domain_get (), addr);
117 if (mono_method_same_domain (ji, target_ji)) {
118 gpointer *got_entry = (gpointer*)((guint8*)code + (*(guint32*)(code - 4)));
119 InterlockedExchangePointer (got_entry, addr);
128 * amd64_aot_trampoline:
130 * This trampoline handles calls made from AOT code. We try to bypass the
131 * normal JIT compilation logic to avoid loading the metadata for the method.
134 amd64_aot_trampoline (long *regs, guint8 *code, guint8 *token_info,
141 gpointer *vtable_slot;
143 image = *(gpointer*)token_info;
144 token_info += sizeof (gpointer);
145 token = *(guint32*)token_info;
147 /* Later we could avoid allocating the MonoMethod */
148 method = mono_get_method (image, token, NULL);
151 if (method->iflags & METHOD_IMPL_ATTRIBUTE_SYNCHRONIZED)
152 method = mono_marshal_get_synchronized_wrapper (method);
154 addr = mono_compile_method (method);
157 vtable_slot = mono_arch_get_vcall_slot_addr (code, (gpointer*)regs);
158 g_assert (vtable_slot);
160 if (method->klass->valuetype)
161 addr = get_unbox_trampoline (method, addr);
163 if (mono_domain_owns_vtable_slot (mono_domain_get (), vtable_slot))
170 * amd64_class_init_trampoline:
172 * This method calls mono_runtime_class_init () to run the static constructor
173 * for the type, then patches the caller code so it is not called again.
176 amd64_class_init_trampoline (long *regs, guint8 *code, MonoVTable *vtable, guint8 *tramp)
178 mono_runtime_class_init (vtable);
182 if ((code [0] == 0x49) && (code [1] == 0xff)) {
183 if (!mono_running_on_valgrind ()) {
184 /* amd64_set_reg_template is 10 bytes long */
185 guint8* buf = code - 10;
187 /* FIXME: Make this thread safe */
188 /* Padding code suggested by the AMD64 Opt Manual */
203 } else if (code [-2] == 0xe8) {
204 guint8 *buf = code - 2;
211 } else if (code [0] == 0x90 || code [0] == 0xeb || code [0] == 0x66)
212 /* Already changed by another thread */
214 else if ((code [-4] == 0x41) && (code [-3] == 0xff) && (code [-2] == 0x15))
215 /* call *<OFFSET>(%rip) */
218 printf ("Invalid trampoline sequence: %x %x %x %x %x %x %x\n", code [0], code [1], code [2], code [3],
219 code [4], code [5], code [6]);
220 g_assert_not_reached ();
225 mono_arch_create_trampoline_code (MonoTrampolineType tramp_type)
227 guint8 *buf, *code, *tramp;
228 int i, lmf_offset, offset, method_offset, tramp_offset, saved_regs_offset, saved_fpregs_offset, framesize;
231 if (tramp_type == MONO_TRAMPOLINE_JUMP)
236 code = buf = mono_global_codeman_reserve (512);
238 framesize = 512 + sizeof (MonoLMF);
239 framesize = (framesize + (MONO_ARCH_FRAME_ALIGNMENT - 1)) & ~ (MONO_ARCH_FRAME_ALIGNMENT - 1);
244 * Allocate a new stack frame and transfer the two arguments received on
245 * the stack to our frame.
247 amd64_alu_reg_imm (code, X86_ADD, AMD64_RSP, 8);
248 amd64_pop_reg (code, AMD64_R11);
250 amd64_push_reg (code, AMD64_RBP);
251 amd64_mov_reg_reg (code, AMD64_RBP, AMD64_RSP, 8);
252 amd64_alu_reg_imm (code, X86_SUB, AMD64_RSP, framesize);
255 * The method is at offset -8 from the new RBP, so no need to
259 method_offset = - offset;
262 tramp_offset = - offset;
263 amd64_mov_membase_reg (code, AMD64_RBP, tramp_offset, AMD64_R11, 8);
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);
278 offset += sizeof (MonoLMF);
279 lmf_offset = - offset;
283 amd64_mov_reg_membase (code, AMD64_R11, AMD64_RBP, 8, 8);
285 amd64_mov_reg_imm (code, AMD64_R11, 0);
286 amd64_mov_membase_reg (code, AMD64_RBP, lmf_offset + G_STRUCT_OFFSET (MonoLMF, rip), AMD64_R11, 8);
288 amd64_mov_membase_reg (code, AMD64_RBP, lmf_offset + G_STRUCT_OFFSET (MonoLMF, ebp), AMD64_RBP, 8);
290 if (tramp_type == MONO_TRAMPOLINE_GENERIC)
291 amd64_mov_reg_membase (code, AMD64_R11, AMD64_RBP, method_offset, 8);
293 amd64_mov_reg_imm (code, AMD64_R11, 0);
294 amd64_mov_membase_reg (code, AMD64_RBP, lmf_offset + G_STRUCT_OFFSET (MonoLMF, method), AMD64_R11, 8);
295 /* Save callee saved regs */
296 amd64_mov_membase_reg (code, AMD64_RBP, lmf_offset + G_STRUCT_OFFSET (MonoLMF, rbx), AMD64_RBX, 8);
297 amd64_mov_membase_reg (code, AMD64_RBP, lmf_offset + G_STRUCT_OFFSET (MonoLMF, r12), AMD64_R12, 8);
298 amd64_mov_membase_reg (code, AMD64_RBP, lmf_offset + G_STRUCT_OFFSET (MonoLMF, r13), AMD64_R13, 8);
299 amd64_mov_membase_reg (code, AMD64_RBP, lmf_offset + G_STRUCT_OFFSET (MonoLMF, r14), AMD64_R14, 8);
300 amd64_mov_membase_reg (code, AMD64_RBP, lmf_offset + G_STRUCT_OFFSET (MonoLMF, r15), AMD64_R15, 8);
302 amd64_mov_reg_imm (code, AMD64_R11, mono_get_lmf_addr);
303 amd64_call_reg (code, AMD64_R11);
306 amd64_mov_membase_reg (code, AMD64_RBP, lmf_offset + G_STRUCT_OFFSET (MonoLMF, lmf_addr), AMD64_RAX, 8);
307 /* Save previous_lmf */
308 amd64_mov_reg_membase (code, AMD64_R11, AMD64_RAX, 0, 8);
309 amd64_mov_membase_reg (code, AMD64_RBP, lmf_offset + G_STRUCT_OFFSET (MonoLMF, previous_lmf), AMD64_R11, 8);
311 amd64_lea_membase (code, AMD64_R11, AMD64_RBP, lmf_offset);
312 amd64_mov_membase_reg (code, AMD64_RAX, 0, AMD64_R11, 8);
316 /* Arg1 is the pointer to the saved registers */
317 amd64_lea_membase (code, AMD64_RDI, AMD64_RBP, saved_regs_offset);
319 /* Arg2 is the address of the calling code */
321 amd64_mov_reg_membase (code, AMD64_RSI, AMD64_RBP, 8, 8);
323 amd64_mov_reg_imm (code, AMD64_RSI, 0);
325 /* Arg3 is the method/vtable ptr */
326 amd64_mov_reg_membase (code, AMD64_RDX, AMD64_RBP, method_offset, 8);
328 /* Arg4 is the trampoline address */
329 amd64_mov_reg_membase (code, AMD64_RCX, AMD64_RBP, tramp_offset, 8);
331 if (tramp_type == MONO_TRAMPOLINE_CLASS_INIT)
332 tramp = (guint8*)amd64_class_init_trampoline;
333 else if (tramp_type == MONO_TRAMPOLINE_AOT)
334 tramp = (guint8*)amd64_aot_trampoline;
336 tramp = (guint8*)amd64_magic_trampoline;
338 amd64_mov_reg_imm (code, AMD64_RAX, tramp);
339 amd64_call_reg (code, AMD64_RAX);
343 amd64_mov_reg_membase (code, AMD64_RCX, AMD64_RBP, lmf_offset + G_STRUCT_OFFSET (MonoLMF, previous_lmf), 8);
344 amd64_mov_reg_membase (code, AMD64_R11, AMD64_RBP, lmf_offset + G_STRUCT_OFFSET (MonoLMF, lmf_addr), 8);
345 amd64_mov_membase_reg (code, AMD64_R11, 0, AMD64_RCX, 8);
347 /* Restore argument registers */
348 for (i = 0; i < AMD64_NREG; ++i)
349 if (AMD64_IS_ARGUMENT_REG (i))
350 amd64_mov_reg_membase (code, i, AMD64_RBP, saved_regs_offset + (i * 8), 8);
352 for (i = 0; i < 8; ++i)
353 amd64_movsd_reg_membase (code, i, AMD64_RBP, saved_fpregs_offset + (i * 8));
358 if (tramp_type == MONO_TRAMPOLINE_CLASS_INIT)
361 /* call the compiled method */
362 amd64_jump_reg (code, X86_EAX);
364 g_assert ((code - buf) <= 512);
366 mono_arch_flush_icache (buf, code - buf);
371 #define TRAMPOLINE_SIZE 34
374 create_specific_trampoline (gpointer arg1, MonoTrampolineType tramp_type, MonoDomain *domain)
377 guint8 *code, *buf, *tramp;
380 tramp = mono_get_trampoline_code (tramp_type);
382 code = buf = g_alloca (TRAMPOLINE_SIZE);
384 /* push trampoline address */
385 amd64_lea_membase (code, AMD64_R11, AMD64_RIP, -7);
386 amd64_push_reg (code, AMD64_R11);
389 if (amd64_is_imm32 ((gint64)arg1))
390 amd64_push_imm (code, (gint64)arg1);
392 amd64_mov_reg_imm (code, AMD64_R11, arg1);
393 amd64_push_reg (code, AMD64_R11);
396 jump_offset = code - buf;
397 amd64_jump_disp (code, 0xffffffff);
399 g_assert ((code - buf) <= TRAMPOLINE_SIZE);
401 ji = g_new0 (MonoJitInfo, 1);
403 mono_domain_lock (domain);
405 * FIXME: Changing the size to code - buf causes strange crashes during
408 ji->code_start = mono_code_manager_reserve (domain->code_mp, TRAMPOLINE_SIZE);
409 ji->code_size = code - buf;
410 mono_domain_unlock (domain);
412 memcpy (ji->code_start, buf, ji->code_size);
415 g_assert ((((gint64)tramp) >> 32) == 0);
416 code = (guint8*)ji->code_start + jump_offset;
417 amd64_jump_disp (code, tramp - code);
419 mono_jit_stats.method_trampolines++;
421 mono_arch_flush_icache (ji->code_start, ji->code_size);
427 mono_arch_create_jump_trampoline (MonoMethod *method)
429 MonoJitInfo *ji = create_specific_trampoline (method, MONO_TRAMPOLINE_JUMP, mono_domain_get ());
436 * mono_arch_create_jit_trampoline:
437 * @method: pointer to the method info
439 * Creates a trampoline function for virtual methods. If the created
440 * code is called it first starts JIT compilation of method,
441 * and then calls the newly created method. I also replaces the
442 * corresponding vtable entry (see amd64_magic_trampoline).
444 * Returns: a pointer to the newly created code
447 mono_arch_create_jit_trampoline (MonoMethod *method)
452 ji = create_specific_trampoline (method, MONO_TRAMPOLINE_GENERIC, mono_domain_get ());
453 code_start = ji->code_start;
460 mono_arch_create_jit_trampoline_from_token (MonoImage *image, guint32 token)
462 MonoDomain *domain = mono_domain_get ();
467 mono_domain_lock (domain);
468 buf = start = mono_code_manager_reserve (domain->code_mp, 2 * sizeof (gpointer));
469 mono_domain_unlock (domain);
471 *(gpointer*)buf = image;
472 buf += sizeof (gpointer);
473 *(guint32*)buf = token;
475 ji = create_specific_trampoline (start, MONO_TRAMPOLINE_AOT, domain);
476 code_start = ji->code_start;
483 * mono_arch_create_class_init_trampoline:
484 * @vtable: the type to initialize
486 * Creates a trampoline function to run a type initializer.
487 * If the trampoline is called, it calls mono_runtime_class_init with the
488 * given vtable, then patches the caller code so it does not get called any
491 * Returns: a pointer to the newly created code
494 mono_arch_create_class_init_trampoline (MonoVTable *vtable)
499 ji = create_specific_trampoline (vtable, MONO_TRAMPOLINE_CLASS_INIT, vtable->domain);
500 code = ji->code_start;
507 mono_arch_invalidate_method (MonoJitInfo *ji, void *func, gpointer func_arg)
509 /* FIXME: This is not thread safe */
510 guint8 *code = ji->code_start;
512 amd64_mov_reg_imm (code, AMD64_RDI, func_arg);
513 amd64_mov_reg_imm (code, AMD64_R11, func);
515 x86_push_imm (code, (guint64)func_arg);
516 amd64_call_reg (code, AMD64_R11);
520 * This method is only called when running in the Mono Debugger.
523 mono_debugger_create_notification_function (gpointer *notification_address)
527 ptr = buf = mono_global_codeman_reserve (16);
529 x86_breakpoint (buf);
530 if (notification_address)
531 *notification_address = buf;