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>
22 /* adapt to mini later... */
23 #define mono_jit_share_code (1)
26 * Address of the x86 trampoline code. This is used by the debugger to check
27 * whether a method is a trampoline.
29 guint8 *mono_generic_trampoline_code = NULL;
32 * get_unbox_trampoline:
34 * @addr: pointer to native code for @m
36 * when value type methods are called through the vtable we need to unbox the
37 * this argument. This method returns a pointer to a trampoline which does
38 * unboxing before calling the method
41 get_unbox_trampoline (MonoMethod *m, gpointer addr)
46 if (!m->signature->ret->byref && MONO_TYPE_ISSTRUCT (m->signature->ret))
49 start = code = g_malloc (16);
51 x86_alu_membase_imm (code, X86_ADD, X86_ESP, this_pos, sizeof (MonoObject));
52 x86_jump_code (code, addr);
53 g_assert ((code - start) < 16);
59 * x86_magic_trampoline:
60 * @eax: saved x86 register
61 * @ecx: saved x86 register
62 * @edx: saved x86 register
63 * @esi: saved x86 register
64 * @edi: saved x86 register
65 * @ebx: saved x86 register
66 * @code: pointer into caller code
67 * @method: the method to translate
69 * This method is called by the trampoline functions for virtual
70 * methods. It inspects the caller code to find the address of the
71 * vtable slot, then calls the JIT compiler and writes the address
72 * of the compiled method back to the vtable. All virtual methods
73 * are called with: x86_call_membase (inst, basereg, disp). We always
74 * use 32 bit displacement to ensure that the length of the call
75 * instruction is 6 bytes. We need to get the value of the basereg
76 * and the constant displacement.
79 x86_magic_trampoline (int eax, int ecx, int edx, int esi, int edi,
80 int ebx, guint8 *code, MonoMethod *m)
87 EnterCriticalSection (metadata_section);
88 addr = mono_compile_method (m);
89 LeaveCriticalSection (metadata_section);
92 /* the method was jumped to */
96 /* go to the start of the call instruction
98 * address_byte = (m << 6) | (o << 3) | reg
99 * call opcode: 0xff address_byte displacement
104 if ((code [1] != 0xe8) && (code [3] == 0xff) && ((code [4] & 0x18) == 0x10) && ((code [4] >> 6) == 1)) {
105 reg = code [4] & 0x07;
106 disp = (signed char)code [5];
108 if ((code [0] == 0xff) && ((code [1] & 0x18) == 0x10) && ((code [1] >> 6) == 2)) {
109 reg = code [1] & 0x07;
110 disp = *((gint32*)(code + 2));
111 } else if ((code [1] == 0xe8)) {
112 *((guint32*)(code + 2)) = (guint)addr - ((guint)code + 1) - 5;
114 } else if ((code [4] == 0xff) && (((code [5] >> 6) & 0x3) == 0) && (((code [5] >> 3) & 0x7) == 2)) {
116 * This is a interface call: should check the above code can't catch it earlier
117 * 8b 40 30 mov 0x30(%eax),%eax
121 reg = code [5] & 0x07;
123 printf ("Invalid trampoline sequence: %x %x %x %x %x %x %x\n", code [0], code [1], code [2], code [3],
124 code [4], code [5], code [6]);
125 g_assert_not_reached ();
149 g_assert_not_reached ();
154 if (m->klass->valuetype) {
155 return *((gpointer *)o) = get_unbox_trampoline (m, addr);
157 return *((gpointer *)o) = addr;
162 create_trampoline_code (int is_jump)
165 static guint8* generic_jump_trampoline = NULL;
168 if (generic_jump_trampoline)
169 return generic_jump_trampoline;
171 if (mono_generic_trampoline_code)
172 return mono_generic_trampoline_code;
175 code = buf = g_malloc (256);
176 /* save caller save regs because we need to do a call */
177 x86_push_reg (buf, X86_EDX);
178 x86_push_reg (buf, X86_EAX);
179 x86_push_reg (buf, X86_ECX);
183 /* save the IP (caller ip) */
185 x86_push_imm (buf, 0);
187 x86_push_membase (buf, X86_ESP, 16);
189 x86_push_reg (buf, X86_EBX);
190 x86_push_reg (buf, X86_EDI);
191 x86_push_reg (buf, X86_ESI);
192 x86_push_reg (buf, X86_EBP);
194 /* save method info */
195 x86_push_membase (buf, X86_ESP, 32);
196 /* get the address of lmf for the current thread */
197 x86_call_code (buf, mono_get_lmf_addr);
199 x86_push_reg (buf, X86_EAX);
200 /* push *lfm (previous_lmf) */
201 x86_push_membase (buf, X86_EAX, 0);
203 x86_mov_membase_reg (buf, X86_EAX, 0, X86_ESP, 4);
206 /* push the method info */
207 x86_push_membase (buf, X86_ESP, 44);
208 /* push the return address onto the stack */
210 x86_push_imm (buf, 0);
212 x86_push_membase (buf, X86_ESP, 52);
214 /* save all register values */
215 x86_push_reg (buf, X86_EBX);
216 x86_push_reg (buf, X86_EDI);
217 x86_push_reg (buf, X86_ESI);
218 x86_push_membase (buf, X86_ESP, 64); /* EDX */
219 x86_push_membase (buf, X86_ESP, 64); /* ECX */
220 x86_push_membase (buf, X86_ESP, 64); /* EAX */
222 x86_call_code (buf, x86_magic_trampoline);
223 x86_alu_reg_imm (buf, X86_ADD, X86_ESP, 8*4);
225 /* restore LMF start */
226 /* ebx = previous_lmf */
227 x86_pop_reg (buf, X86_EBX);
229 x86_pop_reg (buf, X86_EDI);
230 /* *(lmf) = previous_lmf */
231 x86_mov_membase_reg (buf, X86_EDI, 0, X86_EBX, 4);
232 /* discard method info */
233 x86_pop_reg (buf, X86_ESI);
234 /* restore caller saved regs */
235 x86_pop_reg (buf, X86_EBP);
236 x86_pop_reg (buf, X86_ESI);
237 x86_pop_reg (buf, X86_EDI);
238 x86_pop_reg (buf, X86_EBX);
239 /* discard save IP */
240 x86_alu_reg_imm (buf, X86_ADD, X86_ESP, 4);
241 /* restore LMF end */
243 x86_alu_reg_imm (buf, X86_ADD, X86_ESP, 16);
245 /* call the compiled method */
246 x86_jump_reg (buf, X86_EAX);
248 g_assert ((buf - code) <= 256);
251 return generic_jump_trampoline = code;
253 return mono_generic_trampoline_code = code;
257 #define TRAMPOLINE_SIZE 10
260 mono_arch_create_jump_trampoline (MonoMethod *method)
262 guint8 *code, *buf, *tramp;
264 if (method->iflags & METHOD_IMPL_ATTRIBUTE_SYNCHRONIZED)
265 return mono_arch_create_jump_trampoline (mono_marshal_get_synchronized_wrapper (method));
267 /* icalls use method->addr */
268 if ((method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL) ||
269 (method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL)) {
272 if (!method->addr && (method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL))
273 mono_lookup_pinvoke_call (method);
275 #ifdef MONO_USE_EXC_TABLES
276 if (mono_method_blittable (method)) {
280 nm = mono_marshal_get_native_wrapper (method);
281 return mono_compile_method (nm);
282 #ifdef MONO_USE_EXC_TABLES
287 tramp = create_trampoline_code (TRUE);
289 code = buf = g_malloc (TRAMPOLINE_SIZE);
290 x86_push_imm (buf, method);
291 x86_jump_code (buf, tramp);
292 g_assert ((buf - code) <= TRAMPOLINE_SIZE);
294 mono_jit_stats.method_trampolines++;
301 * mono_arch_create_jit_trampoline:
302 * @method: pointer to the method info
304 * Creates a trampoline function for virtual methods. If the created
305 * code is called it first starts JIT compilation of method,
306 * and then calls the newly created method. I also replaces the
307 * corresponding vtable entry (see x86_magic_trampoline).
309 * Returns: a pointer to the newly created code
312 mono_arch_create_jit_trampoline (MonoMethod *method)
316 /* previously created trampoline code */
320 /* we immediately compile runtime provided functions */
321 if (method->iflags & METHOD_IMPL_ATTRIBUTE_RUNTIME) {
322 method->info = mono_compile_method (method);
326 /* icalls use method->addr */
327 if ((method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL) ||
328 (method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL)) {
331 if (!method->addr && (method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL))
332 mono_lookup_pinvoke_call (method);
334 #ifdef MONO_USE_EXC_TABLES
335 if (mono_method_blittable (method)) {
336 method->info = method->addr;
339 nm = mono_marshal_get_native_wrapper (method);
340 method->info = mono_compile_method (nm);
341 #ifdef MONO_USE_EXC_TABLES
347 if (method->iflags & METHOD_IMPL_ATTRIBUTE_SYNCHRONIZED)
348 return mono_arch_create_jit_trampoline (mono_marshal_get_synchronized_wrapper (method));
350 create_trampoline_code (FALSE);
352 code = buf = g_malloc (TRAMPOLINE_SIZE);
353 x86_push_imm (buf, method);
354 x86_jump_code (buf, mono_generic_trampoline_code);
355 g_assert ((buf - code) <= TRAMPOLINE_SIZE);
357 /* store trampoline address */
360 mono_jit_stats.method_trampolines++;
366 * This method is only called when running in the Mono Debugger.
369 mono_debugger_create_notification_function (gpointer *notification_address)
373 ptr = buf = g_malloc0 (16);
374 x86_breakpoint (buf);
375 if (notification_address)
376 *notification_address = buf;