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>
26 /* adapt to mini later... */
27 #define mono_jit_share_code (1)
30 * Address of the x86 trampoline code. This is used by the debugger to check
31 * whether a method is a trampoline.
33 guint8 *mono_generic_trampoline_code = NULL;
36 * get_unbox_trampoline:
38 * @addr: pointer to native code for @m
40 * when value type methods are called through the vtable we need to unbox the
41 * this argument. This method returns a pointer to a trampoline which does
42 * unboxing before calling the method
45 get_unbox_trampoline (MonoMethod *m, gpointer addr)
50 if (!m->signature->ret->byref && MONO_TYPE_ISSTRUCT (m->signature->ret))
53 start = code = g_malloc (16);
55 x86_alu_membase_imm (code, X86_ADD, X86_ESP, this_pos, sizeof (MonoObject));
56 x86_jump_code (code, addr);
57 g_assert ((code - start) < 16);
63 * x86_magic_trampoline:
64 * @eax: saved x86 register
65 * @ecx: saved x86 register
66 * @edx: saved x86 register
67 * @esi: saved x86 register
68 * @edi: saved x86 register
69 * @ebx: saved x86 register
70 * @code: pointer into caller code
71 * @method: the method to translate
73 * This method is called by the trampoline functions for virtual
74 * methods. It inspects the caller code to find the address of the
75 * vtable slot, then calls the JIT compiler and writes the address
76 * of the compiled method back to the vtable. All virtual methods
77 * are called with: x86_call_membase (inst, basereg, disp). We always
78 * use 32 bit displacement to ensure that the length of the call
79 * instruction is 6 bytes. We need to get the value of the basereg
80 * and the constant displacement.
83 x86_magic_trampoline (int eax, int ecx, int edx, int esi, int edi,
84 int ebx, guint8 *code, MonoMethod *m)
91 addr = mono_compile_method (m);
94 /* the method was jumped to */
98 /* go to the start of the call instruction
100 * address_byte = (m << 6) | (o << 3) | reg
101 * call opcode: 0xff address_byte displacement
106 if ((code [1] != 0xe8) && (code [3] == 0xff) && ((code [4] & 0x18) == 0x10) && ((code [4] >> 6) == 1)) {
107 reg = code [4] & 0x07;
108 disp = (signed char)code [5];
110 if ((code [0] == 0xff) && ((code [1] & 0x18) == 0x10) && ((code [1] >> 6) == 2)) {
111 reg = code [1] & 0x07;
112 disp = *((gint32*)(code + 2));
113 } else if ((code [1] == 0xe8)) {
115 mono_jit_info_table_find (mono_domain_get (), code);
116 MonoJitInfo *target_ji =
117 mono_jit_info_table_find (mono_domain_get (), addr);
120 * If the call was made from domain-neutral to domain-specific
121 * code, we can't patch the call site.
123 if (ji && target_ji && ! (ji->domain_neutral && !target_ji->domain_neutral)) {
124 *((guint32*)(code + 2)) = (guint)addr - ((guint)code + 1) - 5;
125 #ifdef HAVE_VALGRIND_MEMCHECK_H
126 /* Tell valgrind to recompile the patched code */
127 VALGRIND_DISCARD_TRANSLATIONS (code, code + 16);
131 } else if ((code [4] == 0xff) && (((code [5] >> 6) & 0x3) == 0) && (((code [5] >> 3) & 0x7) == 2)) {
133 * This is a interface call: should check the above code can't catch it earlier
134 * 8b 40 30 mov 0x30(%eax),%eax
138 reg = code [5] & 0x07;
140 printf ("Invalid trampoline sequence: %x %x %x %x %x %x %x\n", code [0], code [1], code [2], code [3],
141 code [4], code [5], code [6]);
142 g_assert_not_reached ();
166 g_assert_not_reached ();
171 if (m->klass->valuetype)
172 addr = get_unbox_trampoline (m, addr);
174 *((gpointer *)o) = addr;
180 create_trampoline_code (int is_jump)
183 static guint8* generic_jump_trampoline = NULL;
186 if (generic_jump_trampoline)
187 return generic_jump_trampoline;
189 if (mono_generic_trampoline_code)
190 return mono_generic_trampoline_code;
193 code = buf = g_malloc (256);
194 /* save caller save regs because we need to do a call */
195 x86_push_reg (buf, X86_EDX);
196 x86_push_reg (buf, X86_EAX);
197 x86_push_reg (buf, X86_ECX);
201 /* save the IP (caller ip) */
203 x86_push_imm (buf, 0);
205 x86_push_membase (buf, X86_ESP, 16);
207 x86_push_reg (buf, X86_EBX);
208 x86_push_reg (buf, X86_EDI);
209 x86_push_reg (buf, X86_ESI);
210 x86_push_reg (buf, X86_EBP);
212 /* save method info */
213 x86_push_membase (buf, X86_ESP, 32);
214 /* get the address of lmf for the current thread */
215 x86_call_code (buf, mono_get_lmf_addr);
217 x86_push_reg (buf, X86_EAX);
218 /* push *lfm (previous_lmf) */
219 x86_push_membase (buf, X86_EAX, 0);
221 x86_mov_membase_reg (buf, X86_EAX, 0, X86_ESP, 4);
224 /* push the method info */
225 x86_push_membase (buf, X86_ESP, 44);
226 /* push the return address onto the stack */
228 x86_push_imm (buf, 0);
230 x86_push_membase (buf, X86_ESP, 52);
232 /* save all register values */
233 x86_push_reg (buf, X86_EBX);
234 x86_push_reg (buf, X86_EDI);
235 x86_push_reg (buf, X86_ESI);
236 x86_push_membase (buf, X86_ESP, 64); /* EDX */
237 x86_push_membase (buf, X86_ESP, 64); /* ECX */
238 x86_push_membase (buf, X86_ESP, 64); /* EAX */
240 x86_call_code (buf, x86_magic_trampoline);
241 x86_alu_reg_imm (buf, X86_ADD, X86_ESP, 8*4);
243 /* restore LMF start */
244 /* ebx = previous_lmf */
245 x86_pop_reg (buf, X86_EBX);
247 x86_pop_reg (buf, X86_EDI);
248 /* *(lmf) = previous_lmf */
249 x86_mov_membase_reg (buf, X86_EDI, 0, X86_EBX, 4);
250 /* discard method info */
251 x86_pop_reg (buf, X86_ESI);
252 /* restore caller saved regs */
253 x86_pop_reg (buf, X86_EBP);
254 x86_pop_reg (buf, X86_ESI);
255 x86_pop_reg (buf, X86_EDI);
256 x86_pop_reg (buf, X86_EBX);
257 /* discard save IP */
258 x86_alu_reg_imm (buf, X86_ADD, X86_ESP, 4);
259 /* restore LMF end */
261 x86_alu_reg_imm (buf, X86_ADD, X86_ESP, 16);
263 /* call the compiled method */
264 x86_jump_reg (buf, X86_EAX);
266 g_assert ((buf - code) <= 256);
269 return generic_jump_trampoline = code;
271 return mono_generic_trampoline_code = code;
275 #define TRAMPOLINE_SIZE 10
278 mono_arch_create_jump_trampoline (MonoMethod *method)
280 guint8 *code, *buf, *tramp;
282 if (method->iflags & METHOD_IMPL_ATTRIBUTE_SYNCHRONIZED)
283 return mono_arch_create_jump_trampoline (mono_marshal_get_synchronized_wrapper (method));
285 /* icalls use method->addr */
286 if ((method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL) ||
287 (method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL)) {
290 if (!method->addr && (method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL))
291 mono_lookup_pinvoke_call (method);
293 #ifdef MONO_USE_EXC_TABLES
294 if (mono_method_blittable (method)) {
298 nm = mono_marshal_get_native_wrapper (method);
299 return mono_compile_method (nm);
300 #ifdef MONO_USE_EXC_TABLES
305 tramp = create_trampoline_code (TRUE);
307 code = buf = g_malloc (TRAMPOLINE_SIZE);
308 x86_push_imm (buf, method);
309 x86_jump_code (buf, tramp);
310 g_assert ((buf - code) <= TRAMPOLINE_SIZE);
312 mono_jit_stats.method_trampolines++;
319 * mono_arch_create_jit_trampoline:
320 * @method: pointer to the method info
322 * Creates a trampoline function for virtual methods. If the created
323 * code is called it first starts JIT compilation of method,
324 * and then calls the newly created method. I also replaces the
325 * corresponding vtable entry (see x86_magic_trampoline).
327 * Returns: a pointer to the newly created code
330 mono_arch_create_jit_trampoline (MonoMethod *method)
334 /* previously created trampoline code */
338 /* we immediately compile runtime provided functions */
339 if (method->iflags & METHOD_IMPL_ATTRIBUTE_RUNTIME) {
340 method->info = mono_compile_method (method);
344 /* icalls use method->addr */
345 if ((method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL) ||
346 (method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL)) {
349 if (!method->addr && (method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL))
350 mono_lookup_pinvoke_call (method);
352 #ifdef MONO_USE_EXC_TABLES
353 if (mono_method_blittable (method)) {
354 method->info = method->addr;
357 nm = mono_marshal_get_native_wrapper (method);
358 method->info = mono_compile_method (nm);
359 #ifdef MONO_USE_EXC_TABLES
365 if (method->iflags & METHOD_IMPL_ATTRIBUTE_SYNCHRONIZED)
366 return mono_arch_create_jit_trampoline (mono_marshal_get_synchronized_wrapper (method));
368 create_trampoline_code (FALSE);
370 code = buf = g_malloc (TRAMPOLINE_SIZE);
371 x86_push_imm (buf, method);
372 x86_jump_code (buf, mono_generic_trampoline_code);
373 g_assert ((buf - code) <= TRAMPOLINE_SIZE);
375 /* store trampoline address */
378 mono_jit_stats.method_trampolines++;
384 * This method is only called when running in the Mono Debugger.
387 mono_debugger_create_notification_function (gpointer *notification_address)
391 ptr = buf = g_malloc0 (16);
392 x86_breakpoint (buf);
393 if (notification_address)
394 *notification_address = buf;