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 MONO_TRAMPOLINE_GENERIC,
29 MONO_TRAMPOLINE_CLASS_INIT
32 /* adapt to mini later... */
33 #define mono_jit_share_code (1)
36 * Address of the trampoline code. This is used by the debugger to check
37 * whether a method is a trampoline.
39 guint8 *mono_generic_trampoline_code = NULL;
42 * get_unbox_trampoline:
44 * @addr: pointer to native code for @m
46 * when value type methods are called through the vtable we need to unbox the
47 * this argument. This method returns a pointer to a trampoline which does
48 * unboxing before calling the method
51 get_unbox_trampoline (MonoMethod *m, gpointer addr)
54 int this_reg = AMD64_RDI;
56 if (!m->signature->ret->byref && MONO_TYPE_ISSTRUCT (m->signature->ret))
59 start = code = g_malloc (20);
61 amd64_alu_reg_imm (code, X86_ADD, this_reg, sizeof (MonoObject));
62 /* FIXME: Optimize this */
63 amd64_mov_reg_imm (code, AMD64_RAX, addr);
64 amd64_jump_reg (code, AMD64_RAX);
65 g_assert ((code - start) < 20);
67 mono_arch_flush_icache (start, code - start);
73 * amd64_magic_trampoline:
76 amd64_magic_trampoline (long *regs, guint8 *code, MonoMethod *m, guint8* tramp)
79 gpointer *vtable_slot;
81 addr = mono_compile_method (m);
84 //printf ("ENTER: %s\n", mono_method_full_name (m, TRUE));
86 /* the method was jumped to */
90 vtable_slot = mono_amd64_get_vcall_slot_addr (code, regs);
93 if (m->klass->valuetype)
94 addr = get_unbox_trampoline (m, addr);
96 g_assert (*vtable_slot);
101 /* Patch calling code */
103 if ((code [-13] == 0x49) && (code [-12] == 0xbb)) {
105 mono_jit_info_table_find (mono_domain_get (), code);
106 MonoJitInfo *target_ji =
107 mono_jit_info_table_find (mono_domain_get (), addr);
109 /* The first part of the condition means an icall without a wrapper */
110 if ((!target_ji && m->addr) || mono_method_same_domain (ji, target_ji)) {
111 InterlockedExchangePointer ((gpointer*)(code - 11), addr);
115 /* FIXME: handle more cases */
117 /* Patch trampoline in case the calling code can't be patched */
118 /* FIXME: Make this thread safe */
120 /* FIXME: This causes nunit-console to fail */
121 amd64_mov_reg_imm (tramp, AMD64_R11, addr);
122 amd64_jump_reg (tramp, AMD64_R11);
131 * amd64_class_init_trampoline:
133 * This method calls mono_runtime_class_init () to run the static constructor
134 * for the type, then patches the caller code so it is not called again.
137 amd64_class_init_trampoline (long *regs, guint8 *code, MonoVTable *vtable)
139 mono_runtime_class_init (vtable);
142 if ((code [0] == 0x49) && (code [1] == 0xff)) {
143 if (!mono_running_on_valgrind ()) {
144 /* amd64_set_reg_template is 10 bytes long */
145 guint8* buf = code - 10;
147 /* FIXME: Make this thread safe */
148 /* Padding code suggested by the AMD64 Opt Manual */
165 if (code [0] == 0x90 || code [0] == 0xeb || code [0] == 0x66)
166 /* Already changed by another thread */
169 printf ("Invalid trampoline sequence: %x %x %x %x %x %x %x\n", code [0], code [1], code [2], code [3],
170 code [4], code [5], code [6]);
171 g_assert_not_reached ();
176 create_trampoline_code (MonoTrampolineType tramp_type)
178 guint8 *buf, *code, *tramp;
179 int i, lmf_offset, offset, method_offset, tramp_offset, saved_regs_offset, saved_fpregs_offset, framesize;
180 static guint8* generic_jump_trampoline = NULL;
181 static guint8 *generic_class_init_trampoline = NULL;
183 switch (tramp_type) {
184 case MONO_TRAMPOLINE_GENERIC:
185 if (mono_generic_trampoline_code)
186 return mono_generic_trampoline_code;
188 case MONO_TRAMPOLINE_JUMP:
189 if (generic_jump_trampoline)
190 return generic_jump_trampoline;
192 case MONO_TRAMPOLINE_CLASS_INIT:
193 if (generic_class_init_trampoline)
194 return generic_class_init_trampoline;
198 code = buf = g_malloc (512);
200 framesize = 512 + sizeof (MonoLMF);
201 framesize = (framesize + (MONO_ARCH_FRAME_ALIGNMENT - 1)) & ~ (MONO_ARCH_FRAME_ALIGNMENT - 1);
206 * Allocate a new stack frame and transfer the two arguments received on
207 * the stack to our frame.
209 amd64_alu_reg_imm (code, X86_ADD, AMD64_RSP, 8);
210 amd64_pop_reg (code, AMD64_R11);
212 amd64_push_reg (code, AMD64_RBP);
213 amd64_mov_reg_reg (code, AMD64_RBP, AMD64_RSP, 8);
214 amd64_alu_reg_imm (code, X86_SUB, AMD64_RSP, framesize);
217 * The method is at offset -8 from the new RBP, so no need to
221 method_offset = - offset;
224 tramp_offset = - offset;
225 amd64_mov_membase_reg (code, AMD64_RBP, tramp_offset, AMD64_R11, 8);
227 /* Save all registers */
229 offset += AMD64_NREG * 8;
230 saved_regs_offset = - offset;
231 for (i = 0; i < AMD64_NREG; ++i)
232 amd64_mov_membase_reg (code, AMD64_RBP, saved_regs_offset + (i * 8), i, 8);
234 saved_fpregs_offset = - offset;
235 for (i = 0; i < 8; ++i)
236 amd64_movsd_membase_reg (code, AMD64_RBP, saved_fpregs_offset + (i * 8), i);
240 offset += sizeof (MonoLMF);
241 lmf_offset = - offset;
244 if (tramp_type == MONO_TRAMPOLINE_JUMP)
245 amd64_mov_reg_imm (code, AMD64_R11, 0);
247 amd64_mov_reg_membase (code, AMD64_R11, AMD64_RBP, 8, 8);
248 amd64_mov_membase_reg (code, AMD64_RBP, lmf_offset + G_STRUCT_OFFSET (MonoLMF, rip), AMD64_R11, 8);
250 amd64_mov_membase_reg (code, AMD64_RBP, lmf_offset + G_STRUCT_OFFSET (MonoLMF, ebp), AMD64_RBP, 8);
252 if (tramp_type == MONO_TRAMPOLINE_GENERIC)
253 amd64_mov_reg_membase (code, AMD64_R11, AMD64_RBP, method_offset, 8);
255 amd64_mov_reg_imm (code, AMD64_R11, 0);
256 amd64_mov_membase_reg (code, AMD64_RBP, lmf_offset + G_STRUCT_OFFSET (MonoLMF, method), AMD64_R11, 8);
257 /* Save callee saved regs */
258 amd64_mov_membase_reg (code, AMD64_RBP, lmf_offset + G_STRUCT_OFFSET (MonoLMF, rbx), AMD64_RBX, 8);
259 amd64_mov_membase_reg (code, AMD64_RBP, lmf_offset + G_STRUCT_OFFSET (MonoLMF, r12), AMD64_R12, 8);
260 amd64_mov_membase_reg (code, AMD64_RBP, lmf_offset + G_STRUCT_OFFSET (MonoLMF, r13), AMD64_R13, 8);
261 amd64_mov_membase_reg (code, AMD64_RBP, lmf_offset + G_STRUCT_OFFSET (MonoLMF, r14), AMD64_R14, 8);
262 amd64_mov_membase_reg (code, AMD64_RBP, lmf_offset + G_STRUCT_OFFSET (MonoLMF, r15), AMD64_R15, 8);
264 amd64_mov_reg_imm (code, AMD64_R11, mono_get_lmf_addr);
265 amd64_call_reg (code, AMD64_R11);
268 amd64_mov_membase_reg (code, AMD64_RBP, lmf_offset + G_STRUCT_OFFSET (MonoLMF, lmf_addr), AMD64_RAX, 8);
269 /* Save previous_lmf */
270 amd64_mov_reg_membase (code, AMD64_R11, AMD64_RAX, 0, 8);
271 amd64_mov_membase_reg (code, AMD64_RBP, lmf_offset + G_STRUCT_OFFSET (MonoLMF, previous_lmf), AMD64_R11, 8);
273 amd64_lea_membase (code, AMD64_R11, AMD64_RBP, lmf_offset);
274 amd64_mov_membase_reg (code, AMD64_RAX, 0, AMD64_R11, 8);
278 /* Arg1 is the pointer to the saved registers */
279 amd64_lea_membase (code, AMD64_RDI, AMD64_RBP, saved_regs_offset);
281 /* Arg2 is the address of the calling code */
282 amd64_mov_reg_membase (code, AMD64_RSI, AMD64_RBP, 8, 8);
284 /* Arg3 is the method/vtable ptr */
285 amd64_mov_reg_membase (code, AMD64_RDX, AMD64_RBP, method_offset, 8);
287 /* Arg4 is the trampoline address */
288 amd64_mov_reg_membase (code, AMD64_RCX, AMD64_RBP, tramp_offset, 8);
290 if (tramp_type == MONO_TRAMPOLINE_CLASS_INIT)
291 tramp = (guint8*)amd64_class_init_trampoline;
293 tramp = (guint8*)amd64_magic_trampoline;
295 amd64_mov_reg_imm (code, AMD64_RAX, tramp);
296 amd64_call_reg (code, AMD64_RAX);
300 amd64_mov_reg_membase (code, AMD64_RCX, AMD64_RBP, lmf_offset + G_STRUCT_OFFSET (MonoLMF, previous_lmf), 8);
301 amd64_mov_reg_membase (code, AMD64_R11, AMD64_RBP, lmf_offset + G_STRUCT_OFFSET (MonoLMF, lmf_addr), 8);
302 amd64_mov_membase_reg (code, AMD64_R11, 0, AMD64_RCX, 8);
304 /* Restore argument registers */
305 for (i = 0; i < AMD64_NREG; ++i)
306 if (AMD64_IS_ARGUMENT_REG (i))
307 amd64_mov_reg_membase (code, i, AMD64_RBP, saved_regs_offset + (i * 8), 8);
309 for (i = 0; i < 8; ++i)
310 amd64_movsd_reg_membase (code, i, AMD64_RBP, saved_fpregs_offset + (i * 8));
315 if (tramp_type == MONO_TRAMPOLINE_CLASS_INIT)
318 /* call the compiled method */
319 amd64_jump_reg (code, X86_EAX);
321 g_assert ((code - buf) <= 512);
323 switch (tramp_type) {
324 case MONO_TRAMPOLINE_GENERIC:
325 mono_generic_trampoline_code = buf;
327 case MONO_TRAMPOLINE_JUMP:
328 generic_jump_trampoline = buf;
330 case MONO_TRAMPOLINE_CLASS_INIT:
331 generic_class_init_trampoline = buf;
338 #define TRAMPOLINE_SIZE 30
341 create_specific_trampoline (gpointer arg1, MonoTrampolineType tramp_type, MonoDomain *domain)
344 guint8 *code, *buf, *tramp;
346 tramp = create_trampoline_code (tramp_type);
348 mono_domain_lock (domain);
349 code = buf = mono_code_manager_reserve (domain->code_mp, TRAMPOLINE_SIZE);
350 mono_domain_unlock (domain);
352 /* push trampoline address */
353 amd64_lea_membase (code, AMD64_R11, AMD64_RIP, -7);
354 amd64_push_reg (code, AMD64_R11);
357 amd64_mov_reg_imm (code, AMD64_R11, arg1);
358 amd64_push_reg (code, AMD64_R11);
360 /* FIXME: Optimize this */
361 amd64_mov_reg_imm (code, AMD64_R11, tramp);
362 amd64_jump_reg (code, AMD64_R11);
364 g_assert ((code - buf) <= TRAMPOLINE_SIZE);
366 ji = g_new0 (MonoJitInfo, 1);
367 ji->code_start = buf;
368 ji->code_size = code - buf;
370 mono_jit_stats.method_trampolines++;
372 mono_arch_flush_icache (ji->code_start, ji->code_size);
378 mono_arch_create_jump_trampoline (MonoMethod *method)
380 MonoJitInfo *ji = create_specific_trampoline (method, MONO_TRAMPOLINE_JUMP, mono_domain_get ());
387 * mono_arch_create_jit_trampoline:
388 * @method: pointer to the method info
390 * Creates a trampoline function for virtual methods. If the created
391 * code is called it first starts JIT compilation of method,
392 * and then calls the newly created method. I also replaces the
393 * corresponding vtable entry (see amd64_magic_trampoline).
395 * Returns: a pointer to the newly created code
398 mono_arch_create_jit_trampoline (MonoMethod *method)
401 MonoDomain *domain = mono_domain_get ();
403 /* Trampoline are arch specific, so cache only the one used in the root domain */
404 if ((domain == mono_get_root_domain ()) && method->info)
407 if (method->iflags & METHOD_IMPL_ATTRIBUTE_SYNCHRONIZED)
408 return mono_arch_create_jit_trampoline (mono_marshal_get_synchronized_wrapper (method));
410 ji = create_specific_trampoline (method, MONO_TRAMPOLINE_GENERIC, domain);
411 if (domain == mono_get_root_domain ())
412 method->info = ji->code_start;
415 return ji->code_start;
419 * mono_arch_create_class_init_trampoline:
420 * @vtable: the type to initialize
422 * Creates a trampoline function to run a type initializer.
423 * If the trampoline is called, it calls mono_runtime_class_init with the
424 * given vtable, then patches the caller code so it does not get called any
427 * Returns: a pointer to the newly created code
430 mono_arch_create_class_init_trampoline (MonoVTable *vtable)
435 ji = create_specific_trampoline (vtable, MONO_TRAMPOLINE_CLASS_INIT, vtable->domain);
436 code = ji->code_start;
443 * This method is only called when running in the Mono Debugger.
446 mono_debugger_create_notification_function (gpointer *notification_address)
450 ptr = buf = g_malloc0 (16);
451 x86_breakpoint (buf);
452 if (notification_address)
453 *notification_address = buf;