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/metadata-internals.h>
15 #include <mono/metadata/marshal.h>
16 #include <mono/metadata/tabledefs.h>
17 #include <mono/arch/x86/x86-codegen.h>
18 #include <mono/metadata/mono-debug-debugger.h>
20 #ifdef HAVE_VALGRIND_MEMCHECK_H
21 #include <valgrind/memcheck.h>
28 * get_unbox_trampoline:
30 * @addr: pointer to native code for @m
32 * when value type methods are called through the vtable we need to unbox the
33 * this argument. This method returns a pointer to a trampoline which does
34 * unboxing before calling the method
37 get_unbox_trampoline (MonoMethod *m, gpointer addr)
41 MonoDomain *domain = mono_domain_get ();
43 if (!mono_method_signature (m)->ret->byref && MONO_TYPE_ISSTRUCT (mono_method_signature (m)->ret))
46 mono_domain_lock (domain);
47 start = code = mono_code_manager_reserve (domain->code_mp, 16);
48 mono_domain_unlock (domain);
50 x86_alu_membase_imm (code, X86_ADD, X86_ESP, this_pos, sizeof (MonoObject));
51 x86_jump_code (code, addr);
52 g_assert ((code - start) < 16);
58 * x86_magic_trampoline:
59 * @eax: saved x86 register
60 * @ecx: saved x86 register
61 * @edx: saved x86 register
62 * @esi: saved x86 register
63 * @edi: saved x86 register
64 * @ebx: saved x86 register
65 * @code: pointer into caller code
66 * @method: the method to translate
68 * This method is called by the trampoline functions for virtual
69 * methods. It inspects the caller code to find the address of the
70 * vtable slot, then calls the JIT compiler and writes the address
71 * of the compiled method back to the vtable. All virtual methods
72 * are called with: x86_call_membase (inst, basereg, disp). We always
73 * use 32 bit displacement to ensure that the length of the call
74 * instruction is 6 bytes. We need to get the value of the basereg
75 * and the constant displacement.
78 x86_magic_trampoline (int eax, int ecx, int edx, int esi, int edi,
79 int ebx, guint8 *code, MonoMethod *m)
82 gpointer *vtable_slot;
85 addr = mono_compile_method (m);
88 /* the method was jumped to */
99 vtable_slot = mono_arch_get_vcall_slot_addr (code, (gpointer*)regs);
101 /* go to the start of the call instruction
103 * address_byte = (m << 6) | (o << 3) | reg
104 * call opcode: 0xff address_byte displacement
109 if ((code [1] == 0xe8)) {
110 if (!mono_running_on_valgrind ()) {
112 mono_jit_info_table_find (mono_domain_get (), code);
113 MonoJitInfo *target_ji =
114 mono_jit_info_table_find (mono_domain_get (), addr);
116 if (mono_method_same_domain (ji, target_ji)) {
117 InterlockedExchange ((gint32*)(code + 2), (guint)addr - ((guint)code + 1) - 5);
119 #ifdef HAVE_VALGRIND_MEMCHECK_H
120 /* Tell valgrind to recompile the patched code */
121 //VALGRIND_DISCARD_TRANSLATIONS (code + 2, code + 6);
127 printf ("Invalid trampoline sequence: %x %x %x %x %x %x %x\n", code [0], code [1], code [2], code [3],
128 code [4], code [5], code [6]);
129 g_assert_not_reached ();
133 if (m->klass->valuetype && !mono_aot_is_got_entry (code, (guint8*)vtable_slot))
134 addr = get_unbox_trampoline (m, addr);
136 if (mono_aot_is_got_entry (code, (guint8*)vtable_slot) || mono_domain_owns_vtable_slot (mono_domain_get (), vtable_slot))
143 * x86_aot_trampoline:
145 * This trampoline handles calls made from AOT code. We try to bypass the
146 * normal JIT compilation logic to avoid loading the metadata for the method.
149 x86_aot_trampoline (int eax, int ecx, int edx, int esi, int edi,
150 int ebx, guint8 *code, guint8 *token_info)
156 gpointer *vtable_slot;
159 image = *(gpointer*)token_info;
160 token_info += sizeof (gpointer);
161 token = *(guint32*)token_info;
163 /* Later we could avoid allocating the MonoMethod */
164 method = mono_get_method (image, token, NULL);
167 if (method->iflags & METHOD_IMPL_ATTRIBUTE_SYNCHRONIZED)
168 method = mono_marshal_get_synchronized_wrapper (method);
170 addr = mono_compile_method (method);
173 regs [X86_EAX] = eax;
174 regs [X86_ECX] = ecx;
175 regs [X86_EDX] = edx;
176 regs [X86_ESI] = esi;
177 regs [X86_EDI] = edi;
178 regs [X86_EBX] = ebx;
180 vtable_slot = mono_arch_get_vcall_slot_addr (code, (gpointer*)regs);
181 g_assert (vtable_slot);
183 if (method->klass->valuetype)
184 addr = get_unbox_trampoline (method, addr);
186 if (mono_domain_owns_vtable_slot (mono_domain_get (), vtable_slot))
193 * x86_class_init_trampoline:
194 * @eax: saved x86 register
195 * @ecx: saved x86 register
196 * @edx: saved x86 register
197 * @esi: saved x86 register
198 * @edi: saved x86 register
199 * @ebx: saved x86 register
200 * @code: pointer into caller code
201 * @vtable: the type to initialize
203 * This method calls mono_runtime_class_init () to run the static constructor
204 * for the type, then patches the caller code so it is not called again.
207 x86_class_init_trampoline (int eax, int ecx, int edx, int esi, int edi,
208 int ebx, guint8 *code, MonoVTable *vtable)
210 mono_runtime_class_init (vtable);
213 if (code [0] == 0xe8) {
214 if (!mono_running_on_valgrind ()) {
217 * Thread safe code patching using the algorithm from the paper
218 * 'Practicing JUDO: Java Under Dynamic Optimizations'
221 * First atomically change the the first 2 bytes of the call to a
225 InterlockedExchange ((gint32*)code, ops);
227 /* Then change the other bytes to a nop */
232 /* Then atomically change the first 4 bytes to a nop as well */
234 InterlockedExchange ((guint32*)code, ops);
236 #ifdef HAVE_VALGRIND_MEMCHECK_H
237 /* FIXME: the calltree skin trips on the self modifying code above */
239 /* Tell valgrind to recompile the patched code */
240 //VALGRIND_DISCARD_TRANSLATIONS (code, code + 8);
243 } else if (code [0] == 0x90 || code [0] == 0xeb) {
244 /* Already changed by another thread */
246 } else if ((code [-1] == 0xff) && (x86_modrm_reg (code [0]) == 0x2)) {
247 /* call *<OFFSET>(<REG>) -> Call made from AOT code */
248 /* FIXME: Patch up the trampoline */
251 printf ("Invalid trampoline sequence: %x %x %x %x %x %x %x\n", code [0], code [1], code [2], code [3],
252 code [4], code [5], code [6]);
253 g_assert_not_reached ();
258 mono_arch_create_trampoline_code (MonoTrampolineType tramp_type)
262 code = buf = mono_global_codeman_reserve (256);
264 /* save caller save regs because we need to do a call */
265 x86_push_reg (buf, X86_EDX);
266 x86_push_reg (buf, X86_EAX);
267 x86_push_reg (buf, X86_ECX);
271 /* save the IP (caller ip) */
272 if (tramp_type == MONO_TRAMPOLINE_JUMP)
273 x86_push_imm (buf, 0);
275 x86_push_membase (buf, X86_ESP, 16);
277 x86_push_reg (buf, X86_EBP);
278 x86_push_reg (buf, X86_ESI);
279 x86_push_reg (buf, X86_EDI);
280 x86_push_reg (buf, X86_EBX);
282 /* save method info */
283 x86_push_membase (buf, X86_ESP, 32);
284 /* get the address of lmf for the current thread */
285 x86_call_code (buf, mono_get_lmf_addr);
287 x86_push_reg (buf, X86_EAX);
288 /* push *lfm (previous_lmf) */
289 x86_push_membase (buf, X86_EAX, 0);
291 x86_mov_membase_reg (buf, X86_EAX, 0, X86_ESP, 4);
294 /* push the method info */
295 x86_push_membase (buf, X86_ESP, 44);
296 /* push the return address onto the stack */
297 if (tramp_type == MONO_TRAMPOLINE_JUMP)
298 x86_push_imm (buf, 0);
300 x86_push_membase (buf, X86_ESP, 52);
302 /* save all register values */
303 x86_push_reg (buf, X86_EBX);
304 x86_push_reg (buf, X86_EDI);
305 x86_push_reg (buf, X86_ESI);
306 x86_push_membase (buf, X86_ESP, 64); /* EDX */
307 x86_push_membase (buf, X86_ESP, 64); /* ECX */
308 x86_push_membase (buf, X86_ESP, 64); /* EAX */
310 if (tramp_type == MONO_TRAMPOLINE_CLASS_INIT)
311 x86_call_code (buf, x86_class_init_trampoline);
312 else if (tramp_type == MONO_TRAMPOLINE_AOT)
313 x86_call_code (buf, x86_aot_trampoline);
315 x86_call_code (buf, x86_magic_trampoline);
316 x86_alu_reg_imm (buf, X86_ADD, X86_ESP, 8*4);
318 /* restore LMF start */
319 /* ebx = previous_lmf */
320 x86_pop_reg (buf, X86_EBX);
322 x86_pop_reg (buf, X86_EDI);
323 /* *(lmf) = previous_lmf */
324 x86_mov_membase_reg (buf, X86_EDI, 0, X86_EBX, 4);
325 /* discard method info */
326 x86_pop_reg (buf, X86_ESI);
327 /* restore caller saved regs */
328 x86_pop_reg (buf, X86_EBX);
329 x86_pop_reg (buf, X86_EDI);
330 x86_pop_reg (buf, X86_ESI);
331 x86_pop_reg (buf, X86_EBP);
333 /* discard save IP */
334 x86_alu_reg_imm (buf, X86_ADD, X86_ESP, 4);
335 /* restore LMF end */
337 x86_alu_reg_imm (buf, X86_ADD, X86_ESP, 16);
339 if (tramp_type == MONO_TRAMPOLINE_CLASS_INIT)
342 /* call the compiled method */
343 x86_jump_reg (buf, X86_EAX);
345 g_assert ((buf - code) <= 256);
350 #define TRAMPOLINE_SIZE 10
353 create_specific_trampoline (gpointer arg1, MonoTrampolineType tramp_type, MonoDomain *domain)
355 guint8 *code, *buf, *tramp;
358 tramp = mono_get_trampoline_code (tramp_type);
360 mono_domain_lock (domain);
361 code = buf = mono_code_manager_reserve (domain->code_mp, TRAMPOLINE_SIZE);
362 mono_domain_unlock (domain);
364 x86_push_imm (buf, arg1);
365 x86_jump_code (buf, tramp);
366 g_assert ((buf - code) <= TRAMPOLINE_SIZE);
368 ji = g_new0 (MonoJitInfo, 1);
369 ji->code_start = code;
370 ji->code_size = buf - code;
372 mono_arch_flush_icache (ji->code_start, ji->code_size);
374 mono_jit_stats.method_trampolines++;
380 mono_arch_create_jump_trampoline (MonoMethod *method)
382 MonoJitInfo *ji = create_specific_trampoline (method, MONO_TRAMPOLINE_JUMP, mono_domain_get ());
389 * mono_arch_create_jit_trampoline:
390 * @method: pointer to the method info
392 * Creates a trampoline function for virtual methods. If the created
393 * code is called it first starts JIT compilation of method,
394 * and then calls the newly created method. I also replaces the
395 * corresponding vtable entry (see x86_magic_trampoline).
397 * Returns: a pointer to the newly created code
400 mono_arch_create_jit_trampoline (MonoMethod *method)
405 ji = create_specific_trampoline (method, MONO_TRAMPOLINE_GENERIC, mono_domain_get ());
406 code_start = ji->code_start;
413 mono_arch_create_jit_trampoline_from_token (MonoImage *image, guint32 token)
415 MonoDomain *domain = mono_domain_get ();
420 mono_domain_lock (domain);
421 buf = start = mono_code_manager_reserve (domain->code_mp, 2 * sizeof (gpointer));
422 mono_domain_unlock (domain);
424 *(gpointer*)buf = image;
425 buf += sizeof (gpointer);
426 *(guint32*)buf = token;
428 ji = create_specific_trampoline (start, MONO_TRAMPOLINE_AOT, domain);
429 code_start = ji->code_start;
436 * mono_arch_create_class_init_trampoline:
437 * @vtable: the type to initialize
439 * Creates a trampoline function to run a type initializer.
440 * If the trampoline is called, it calls mono_runtime_class_init with the
441 * given vtable, then patches the caller code so it does not get called any
444 * Returns: a pointer to the newly created code
447 mono_arch_create_class_init_trampoline (MonoVTable *vtable)
452 ji = create_specific_trampoline (vtable, MONO_TRAMPOLINE_CLASS_INIT, vtable->domain);
453 code = ji->code_start;
460 mono_arch_invalidate_method (MonoJitInfo *ji, void *func, gpointer func_arg)
462 /* FIXME: This is not thread safe */
463 guint8 *code = ji->code_start;
465 x86_push_imm (code, func_arg);
466 x86_call_code (code, (guint8*)func);
470 * This method is only called when running in the Mono Debugger.
473 mono_debugger_create_notification_function (gpointer *notification_address)
477 ptr = buf = mono_global_codeman_reserve (16);
479 x86_breakpoint (buf);
480 if (notification_address)
481 *notification_address = buf;