2 * trampoline.c: JIT trampoline code
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);
126 * If the call was made from domain-neutral to domain-specific
127 * code, we can't patch the call site.
129 if (ji && target_ji && ! (ji->domain_neutral && !target_ji->domain_neutral)) {
130 *((guint32*)(code + 2)) = (guint)addr - ((guint)code + 1) - 5;
131 #ifdef HAVE_VALGRIND_MEMCHECK_H
132 /* Tell valgrind to recompile the patched code */
133 VALGRIND_DISCARD_TRANSLATIONS (code + 2, code + 6);
137 } else if ((code [4] == 0xff) && (((code [5] >> 6) & 0x3) == 0) && (((code [5] >> 3) & 0x7) == 2)) {
139 * This is a interface call: should check the above code can't catch it earlier
140 * 8b 40 30 mov 0x30(%eax),%eax
144 reg = code [5] & 0x07;
146 printf ("Invalid trampoline sequence: %x %x %x %x %x %x %x\n", code [0], code [1], code [2], code [3],
147 code [4], code [5], code [6]);
148 g_assert_not_reached ();
172 g_assert_not_reached ();
177 if (m->klass->valuetype)
178 addr = get_unbox_trampoline (m, addr);
180 *((gpointer *)o) = addr;
186 * x86_class_init_trampoline:
187 * @eax: saved x86 register
188 * @ecx: saved x86 register
189 * @edx: saved x86 register
190 * @esi: saved x86 register
191 * @edi: saved x86 register
192 * @ebx: saved x86 register
193 * @code: pointer into caller code
194 * @vtable: the type to initialize
196 * This method calls mono_runtime_class_init () to run the static constructor
197 * for the type, then patches the caller code so it is not called again.
200 x86_class_init_trampoline (int eax, int ecx, int edx, int esi, int edi,
201 int ebx, guint8 *code, MonoVTable *vtable)
205 mono_runtime_class_init (vtable);
208 if (code [0] == 0xe8) {
209 for (i = 0; i < 5; ++i)
211 #ifdef HAVE_VALGRIND_MEMCHECK_H
212 /* FIXME: the calltree skin trips on the self modifying code above */
214 /* Tell valgrind to recompile the patched code */
215 VALGRIND_DISCARD_TRANSLATIONS (code, code + 8);
219 g_assert_not_reached ();
223 create_trampoline_code (MonoTrampolineType tramp_type)
226 static guint8* generic_jump_trampoline = NULL;
227 static guint8 *generic_class_init_trampoline = NULL;
229 switch (tramp_type) {
230 case MONO_TRAMPOLINE_GENERIC:
231 if (mono_generic_trampoline_code)
232 return mono_generic_trampoline_code;
234 case MONO_TRAMPOLINE_JUMP:
235 if (generic_jump_trampoline)
236 return generic_jump_trampoline;
238 case MONO_TRAMPOLINE_CLASS_INIT:
239 if (generic_class_init_trampoline)
240 return generic_class_init_trampoline;
244 code = buf = g_malloc (256);
245 /* save caller save regs because we need to do a call */
246 x86_push_reg (buf, X86_EDX);
247 x86_push_reg (buf, X86_EAX);
248 x86_push_reg (buf, X86_ECX);
252 /* save the IP (caller ip) */
253 if (tramp_type == MONO_TRAMPOLINE_JUMP)
254 x86_push_imm (buf, 0);
256 x86_push_membase (buf, X86_ESP, 16);
258 x86_push_reg (buf, X86_EBX);
259 x86_push_reg (buf, X86_EDI);
260 x86_push_reg (buf, X86_ESI);
261 x86_push_reg (buf, X86_EBP);
263 /* save method info */
264 x86_push_membase (buf, X86_ESP, 32);
265 /* get the address of lmf for the current thread */
266 x86_call_code (buf, mono_get_lmf_addr);
268 x86_push_reg (buf, X86_EAX);
269 /* push *lfm (previous_lmf) */
270 x86_push_membase (buf, X86_EAX, 0);
272 x86_mov_membase_reg (buf, X86_EAX, 0, X86_ESP, 4);
275 /* push the method info */
276 x86_push_membase (buf, X86_ESP, 44);
277 /* push the return address onto the stack */
278 if (tramp_type == MONO_TRAMPOLINE_JUMP)
279 x86_push_imm (buf, 0);
281 x86_push_membase (buf, X86_ESP, 52);
283 /* save all register values */
284 x86_push_reg (buf, X86_EBX);
285 x86_push_reg (buf, X86_EDI);
286 x86_push_reg (buf, X86_ESI);
287 x86_push_membase (buf, X86_ESP, 64); /* EDX */
288 x86_push_membase (buf, X86_ESP, 64); /* ECX */
289 x86_push_membase (buf, X86_ESP, 64); /* EAX */
291 if (tramp_type == MONO_TRAMPOLINE_CLASS_INIT)
292 x86_call_code (buf, x86_class_init_trampoline);
294 x86_call_code (buf, x86_magic_trampoline);
295 x86_alu_reg_imm (buf, X86_ADD, X86_ESP, 8*4);
297 /* restore LMF start */
298 /* ebx = previous_lmf */
299 x86_pop_reg (buf, X86_EBX);
301 x86_pop_reg (buf, X86_EDI);
302 /* *(lmf) = previous_lmf */
303 x86_mov_membase_reg (buf, X86_EDI, 0, X86_EBX, 4);
304 /* discard method info */
305 x86_pop_reg (buf, X86_ESI);
306 /* restore caller saved regs */
307 x86_pop_reg (buf, X86_EBP);
308 x86_pop_reg (buf, X86_ESI);
309 x86_pop_reg (buf, X86_EDI);
310 x86_pop_reg (buf, X86_EBX);
311 /* discard save IP */
312 x86_alu_reg_imm (buf, X86_ADD, X86_ESP, 4);
313 /* restore LMF end */
315 x86_alu_reg_imm (buf, X86_ADD, X86_ESP, 16);
317 if (tramp_type == MONO_TRAMPOLINE_CLASS_INIT)
320 /* call the compiled method */
321 x86_jump_reg (buf, X86_EAX);
323 g_assert ((buf - code) <= 256);
325 switch (tramp_type) {
326 case MONO_TRAMPOLINE_GENERIC:
327 mono_generic_trampoline_code = code;
329 case MONO_TRAMPOLINE_JUMP:
330 generic_jump_trampoline = code;
332 case MONO_TRAMPOLINE_CLASS_INIT:
333 generic_class_init_trampoline = code;
340 #define TRAMPOLINE_SIZE 10
343 mono_arch_create_jump_trampoline (MonoMethod *method)
345 guint8 *code, *buf, *tramp;
347 if (method->iflags & METHOD_IMPL_ATTRIBUTE_SYNCHRONIZED)
348 return mono_arch_create_jump_trampoline (mono_marshal_get_synchronized_wrapper (method));
350 /* icalls use method->addr */
351 if ((method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL) ||
352 (method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL)) {
355 if (!method->addr && (method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL))
356 mono_lookup_pinvoke_call (method);
358 #ifdef MONO_USE_EXC_TABLES
359 if (mono_method_blittable (method)) {
363 nm = mono_marshal_get_native_wrapper (method);
364 return mono_compile_method (nm);
365 #ifdef MONO_USE_EXC_TABLES
370 tramp = create_trampoline_code (MONO_TRAMPOLINE_JUMP);
372 code = buf = g_malloc (TRAMPOLINE_SIZE);
373 x86_push_imm (buf, method);
374 x86_jump_code (buf, tramp);
375 g_assert ((buf - code) <= TRAMPOLINE_SIZE);
377 mono_jit_stats.method_trampolines++;
384 * mono_arch_create_jit_trampoline:
385 * @method: pointer to the method info
387 * Creates a trampoline function for virtual methods. If the created
388 * code is called it first starts JIT compilation of method,
389 * and then calls the newly created method. I also replaces the
390 * corresponding vtable entry (see x86_magic_trampoline).
392 * Returns: a pointer to the newly created code
395 mono_arch_create_jit_trampoline (MonoMethod *method)
397 guint8 *code, *buf, *tramp;
399 /* previously created trampoline code */
403 /* we immediately compile runtime provided functions */
404 if (method->iflags & METHOD_IMPL_ATTRIBUTE_RUNTIME) {
405 method->info = mono_compile_method (method);
409 /* icalls use method->addr */
410 if ((method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL) ||
411 (method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL)) {
414 if (!method->addr && (method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL))
415 mono_lookup_pinvoke_call (method);
417 #ifdef MONO_USE_EXC_TABLES
418 if (mono_method_blittable (method)) {
419 method->info = method->addr;
422 nm = mono_marshal_get_native_wrapper (method);
423 method->info = mono_compile_method (nm);
424 #ifdef MONO_USE_EXC_TABLES
430 if (method->iflags & METHOD_IMPL_ATTRIBUTE_SYNCHRONIZED)
431 return mono_arch_create_jit_trampoline (mono_marshal_get_synchronized_wrapper (method));
433 tramp = create_trampoline_code (MONO_TRAMPOLINE_GENERIC);
435 code = buf = g_malloc (TRAMPOLINE_SIZE);
436 x86_push_imm (buf, method);
437 x86_jump_code (buf, tramp);
438 g_assert ((buf - code) <= TRAMPOLINE_SIZE);
440 /* store trampoline address */
443 mono_jit_stats.method_trampolines++;
449 * mono_arch_create_class_init_trampoline:
450 * @vtable: the type to initialize
452 * Creates a trampoline function to run a type initializer.
453 * If the trampoline is called, it calls mono_runtime_class_init with the
454 * given vtable, then patches the caller code so it does not get called any
457 * Returns: a pointer to the newly created code
460 mono_arch_create_class_init_trampoline (MonoVTable *vtable)
462 guint8 *code, *buf, *tramp;
464 tramp = create_trampoline_code (MONO_TRAMPOLINE_CLASS_INIT);
466 code = buf = g_malloc (TRAMPOLINE_SIZE);
467 x86_push_imm (buf, vtable);
468 x86_jump_code (buf, tramp);
469 g_assert ((buf - code) <= TRAMPOLINE_SIZE);
471 mono_jit_stats.method_trampolines++;
477 * This method is only called when running in the Mono Debugger.
480 mono_debugger_create_notification_function (gpointer *notification_address)
484 ptr = buf = g_malloc0 (16);
485 x86_breakpoint (buf);
486 if (notification_address)
487 *notification_address = buf;