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/x86/x86-codegen.h>
17 #include <mono/metadata/mono-debug-debugger.h>
19 #ifdef HAVE_VALGRIND_MEMCHECK_H
20 #include <valgrind/memcheck.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 x86 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)
56 if (!m->signature->ret->byref && MONO_TYPE_ISSTRUCT (m->signature->ret))
59 start = code = g_malloc (16);
61 x86_alu_membase_imm (code, X86_ADD, X86_ESP, this_pos, sizeof (MonoObject));
62 x86_jump_code (code, addr);
63 g_assert ((code - start) < 16);
69 * x86_magic_trampoline:
70 * @eax: saved x86 register
71 * @ecx: saved x86 register
72 * @edx: saved x86 register
73 * @esi: saved x86 register
74 * @edi: saved x86 register
75 * @ebx: saved x86 register
76 * @code: pointer into caller code
77 * @method: the method to translate
79 * This method is called by the trampoline functions for virtual
80 * methods. It inspects the caller code to find the address of the
81 * vtable slot, then calls the JIT compiler and writes the address
82 * of the compiled method back to the vtable. All virtual methods
83 * are called with: x86_call_membase (inst, basereg, disp). We always
84 * use 32 bit displacement to ensure that the length of the call
85 * instruction is 6 bytes. We need to get the value of the basereg
86 * and the constant displacement.
89 x86_magic_trampoline (int eax, int ecx, int edx, int esi, int edi,
90 int ebx, guint8 *code, MonoMethod *m)
97 addr = mono_compile_method (m);
100 /* the method was jumped to */
104 /* go to the start of the call instruction
106 * address_byte = (m << 6) | (o << 3) | reg
107 * call opcode: 0xff address_byte displacement
112 if ((code [1] != 0xe8) && (code [3] == 0xff) && ((code [4] & 0x18) == 0x10) && ((code [4] >> 6) == 1)) {
113 reg = code [4] & 0x07;
114 disp = (signed char)code [5];
116 if ((code [0] == 0xff) && ((code [1] & 0x18) == 0x10) && ((code [1] >> 6) == 2)) {
117 reg = code [1] & 0x07;
118 disp = *((gint32*)(code + 2));
119 } else if ((code [1] == 0xe8)) {
121 mono_jit_info_table_find (mono_domain_get (), code);
122 MonoJitInfo *target_ji =
123 mono_jit_info_table_find (mono_domain_get (), addr);
125 if (mono_method_same_domain (ji, target_ji)) {
126 if (!mono_running_on_valgrind ()) {
127 InterlockedExchange ((gint32*)(code + 2), (guint)addr - ((guint)code + 1) - 5);
129 #ifdef HAVE_VALGRIND_MEMCHECK_H
130 /* Tell valgrind to recompile the patched code */
131 //VALGRIND_DISCARD_TRANSLATIONS (code + 2, code + 6);
136 } else if ((code [4] == 0xff) && (((code [5] >> 6) & 0x3) == 0) && (((code [5] >> 3) & 0x7) == 2)) {
138 * This is a interface call: should check the above code can't catch it earlier
139 * 8b 40 30 mov 0x30(%eax),%eax
143 reg = code [5] & 0x07;
145 printf ("Invalid trampoline sequence: %x %x %x %x %x %x %x\n", code [0], code [1], code [2], code [3],
146 code [4], code [5], code [6]);
147 g_assert_not_reached ();
171 g_assert_not_reached ();
176 if (m->klass->valuetype && !mono_aot_is_got_entry (code, o))
177 addr = get_unbox_trampoline (m, addr);
179 *((gpointer *)o) = addr;
185 * x86_class_init_trampoline:
186 * @eax: saved x86 register
187 * @ecx: saved x86 register
188 * @edx: saved x86 register
189 * @esi: saved x86 register
190 * @edi: saved x86 register
191 * @ebx: saved x86 register
192 * @code: pointer into caller code
193 * @vtable: the type to initialize
195 * This method calls mono_runtime_class_init () to run the static constructor
196 * for the type, then patches the caller code so it is not called again.
199 x86_class_init_trampoline (int eax, int ecx, int edx, int esi, int edi,
200 int ebx, guint8 *code, MonoVTable *vtable)
202 mono_runtime_class_init (vtable);
205 if (code [0] == 0xe8) {
206 if (!mono_running_on_valgrind ()) {
209 * Thread safe code patching using the algorithm from the paper
210 * 'Practicing JUDO: Java Under Dynamic Optimizations'
213 * First atomically change the the first 2 bytes of the call to a
217 InterlockedExchange ((gint32*)code, ops);
219 /* Then change the other bytes to a nop */
224 /* Then atomically change the first 4 bytes to a nop as well */
226 InterlockedExchange ((guint32*)code, ops);
228 #ifdef HAVE_VALGRIND_MEMCHECK_H
229 /* FIXME: the calltree skin trips on the self modifying code above */
231 /* Tell valgrind to recompile the patched code */
232 //VALGRIND_DISCARD_TRANSLATIONS (code, code + 8);
235 } else if (code [0] == 0x90 || code [0] == 0xeb) {
236 /* Already changed by another thread */
238 } else if ((code [-1] == 0xff) && (x86_modrm_reg (code [0]) == 0x2)) {
239 /* call *<OFFSET>(<REG>) -> Call made from AOT code */
240 /* FIXME: Patch up the trampoline */
243 printf ("Invalid trampoline sequence: %x %x %x %x %x %x %x\n", code [0], code [1], code [2], code [3],
244 code [4], code [5], code [6]);
245 g_assert_not_reached ();
250 create_trampoline_code (MonoTrampolineType tramp_type)
253 static guint8* generic_jump_trampoline = NULL;
254 static guint8 *generic_class_init_trampoline = NULL;
256 switch (tramp_type) {
257 case MONO_TRAMPOLINE_GENERIC:
258 if (mono_generic_trampoline_code)
259 return mono_generic_trampoline_code;
261 case MONO_TRAMPOLINE_JUMP:
262 if (generic_jump_trampoline)
263 return generic_jump_trampoline;
265 case MONO_TRAMPOLINE_CLASS_INIT:
266 if (generic_class_init_trampoline)
267 return generic_class_init_trampoline;
271 code = buf = g_malloc (256);
272 /* save caller save regs because we need to do a call */
273 x86_push_reg (buf, X86_EDX);
274 x86_push_reg (buf, X86_EAX);
275 x86_push_reg (buf, X86_ECX);
279 /* save the IP (caller ip) */
280 if (tramp_type == MONO_TRAMPOLINE_JUMP)
281 x86_push_imm (buf, 0);
283 x86_push_membase (buf, X86_ESP, 16);
285 x86_push_reg (buf, X86_EBP);
286 x86_push_reg (buf, X86_ESI);
287 x86_push_reg (buf, X86_EDI);
288 x86_push_reg (buf, X86_EBX);
290 /* save method info */
291 x86_push_membase (buf, X86_ESP, 32);
292 /* get the address of lmf for the current thread */
293 x86_call_code (buf, mono_get_lmf_addr);
295 x86_push_reg (buf, X86_EAX);
296 /* push *lfm (previous_lmf) */
297 x86_push_membase (buf, X86_EAX, 0);
299 x86_mov_membase_reg (buf, X86_EAX, 0, X86_ESP, 4);
302 /* push the method info */
303 x86_push_membase (buf, X86_ESP, 44);
304 /* push the return address onto the stack */
305 if (tramp_type == MONO_TRAMPOLINE_JUMP)
306 x86_push_imm (buf, 0);
308 x86_push_membase (buf, X86_ESP, 52);
310 /* save all register values */
311 x86_push_reg (buf, X86_EBX);
312 x86_push_reg (buf, X86_EDI);
313 x86_push_reg (buf, X86_ESI);
314 x86_push_membase (buf, X86_ESP, 64); /* EDX */
315 x86_push_membase (buf, X86_ESP, 64); /* ECX */
316 x86_push_membase (buf, X86_ESP, 64); /* EAX */
318 if (tramp_type == MONO_TRAMPOLINE_CLASS_INIT)
319 x86_call_code (buf, x86_class_init_trampoline);
321 x86_call_code (buf, x86_magic_trampoline);
322 x86_alu_reg_imm (buf, X86_ADD, X86_ESP, 8*4);
324 /* restore LMF start */
325 /* ebx = previous_lmf */
326 x86_pop_reg (buf, X86_EBX);
328 x86_pop_reg (buf, X86_EDI);
329 /* *(lmf) = previous_lmf */
330 x86_mov_membase_reg (buf, X86_EDI, 0, X86_EBX, 4);
331 /* discard method info */
332 x86_pop_reg (buf, X86_ESI);
333 /* restore caller saved regs */
334 x86_pop_reg (buf, X86_EBX);
335 x86_pop_reg (buf, X86_EDI);
336 x86_pop_reg (buf, X86_ESI);
337 x86_pop_reg (buf, X86_EBP);
339 /* discard save IP */
340 x86_alu_reg_imm (buf, X86_ADD, X86_ESP, 4);
341 /* restore LMF end */
343 x86_alu_reg_imm (buf, X86_ADD, X86_ESP, 16);
345 if (tramp_type == MONO_TRAMPOLINE_CLASS_INIT)
348 /* call the compiled method */
349 x86_jump_reg (buf, X86_EAX);
351 g_assert ((buf - code) <= 256);
353 switch (tramp_type) {
354 case MONO_TRAMPOLINE_GENERIC:
355 mono_generic_trampoline_code = code;
357 case MONO_TRAMPOLINE_JUMP:
358 generic_jump_trampoline = code;
360 case MONO_TRAMPOLINE_CLASS_INIT:
361 generic_class_init_trampoline = code;
368 #define TRAMPOLINE_SIZE 10
371 mono_arch_create_jump_trampoline (MonoMethod *method)
373 guint8 *code, *buf, *tramp;
376 tramp = create_trampoline_code (MONO_TRAMPOLINE_JUMP);
378 code = buf = g_malloc (TRAMPOLINE_SIZE);
379 x86_push_imm (buf, method);
380 x86_jump_code (buf, tramp);
381 g_assert ((buf - code) <= TRAMPOLINE_SIZE);
383 ji = g_new0 (MonoJitInfo, 1);
385 ji->code_start = code;
386 ji->code_size = buf - code;
388 mono_arch_flush_icache (ji->code_start, ji->code_size);
390 mono_jit_stats.method_trampolines++;
397 * mono_arch_create_jit_trampoline:
398 * @method: pointer to the method info
400 * Creates a trampoline function for virtual methods. If the created
401 * code is called it first starts JIT compilation of method,
402 * and then calls the newly created method. I also replaces the
403 * corresponding vtable entry (see x86_magic_trampoline).
405 * Returns: a pointer to the newly created code
408 mono_arch_create_jit_trampoline (MonoMethod *method)
410 guint8 *code, *buf, *tramp;
412 tramp = create_trampoline_code (MONO_TRAMPOLINE_GENERIC);
414 code = buf = g_malloc (TRAMPOLINE_SIZE);
415 x86_push_imm (buf, method);
416 x86_jump_code (buf, tramp);
417 g_assert ((buf - code) <= TRAMPOLINE_SIZE);
423 * mono_arch_create_class_init_trampoline:
424 * @vtable: the type to initialize
426 * Creates a trampoline function to run a type initializer.
427 * If the trampoline is called, it calls mono_runtime_class_init with the
428 * given vtable, then patches the caller code so it does not get called any
431 * Returns: a pointer to the newly created code
434 mono_arch_create_class_init_trampoline (MonoVTable *vtable)
436 guint8 *code, *buf, *tramp;
438 tramp = create_trampoline_code (MONO_TRAMPOLINE_CLASS_INIT);
440 code = buf = g_malloc (TRAMPOLINE_SIZE);
441 x86_push_imm (buf, vtable);
442 x86_jump_code (buf, tramp);
443 g_assert ((buf - code) <= TRAMPOLINE_SIZE);
445 mono_jit_stats.method_trampolines++;
451 mono_arch_invalidate_method (MonoJitInfo *ji, void *func, gpointer func_arg)
453 /* FIXME: This is not thread safe */
454 guint8 *code = ji->code_start;
456 x86_push_imm (code, func_arg);
457 x86_call_code (code, (guint8*)func);
461 * This method is only called when running in the Mono Debugger.
464 mono_debugger_create_notification_function (gpointer *notification_address)
468 ptr = buf = g_malloc0 (16);
469 x86_breakpoint (buf);
470 if (notification_address)
471 *notification_address = buf;