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/tabledefs.h>
15 #include <mono/arch/x86/x86-codegen.h>
22 * Address of the x86 trampoline code. This is used by the debugger to check
23 * whether a method is a trampoline.
25 guint8 *mono_generic_trampoline_code = NULL;
28 * Address of a special breakpoint trampoline code for the debugger.
30 guint8 *mono_breakpoint_trampoline_code = NULL;
33 * get_unbox_trampoline:
35 * @addr: pointer to native code for @m
37 * when value type methods are called through the vtable we need to unbox the
38 * this argument. This method returns a pointer to a trampoline which does
39 * unboxing before calling the method
42 get_unbox_trampoline (MonoMethod *m, gpointer addr)
47 if (!m->signature->ret->byref && m->signature->ret->type == MONO_TYPE_VALUETYPE)
50 start = code = g_malloc (16);
52 x86_alu_membase_imm (code, X86_ADD, X86_ESP, this_pos, sizeof (MonoObject));
53 x86_jump_code (code, addr);
54 g_assert ((code - start) < 16);
60 * get_breakpoint_trampoline:
62 * @addr: pointer to native code for @m
64 * creates a special trampoline for the debugger which is used to get
65 * a breakpoint after compiling a method.
68 get_breakpoint_trampoline (MonoMethod *m, guint32 breakpoint_id, gpointer addr)
70 guint8 *code, *start, *buf;
72 if (!mono_breakpoint_trampoline_code) {
73 mono_breakpoint_trampoline_code = buf = g_malloc (8);
76 x86_alu_reg_imm (buf, X86_ADD, X86_ESP, 8);
79 g_assert ((buf - mono_breakpoint_trampoline_code) <= 8);
82 start = code = g_malloc (22);
83 x86_push_imm (code, addr);
84 x86_push_imm (code, breakpoint_id);
85 x86_push_imm (code, m);
86 x86_jump_code (code, mono_breakpoint_trampoline_code);
87 g_assert ((code - start) <= 22);
93 * x86_magic_trampoline:
94 * @eax: saved x86 register
95 * @ecx: saved x86 register
96 * @edx: saved x86 register
97 * @esi: saved x86 register
98 * @edi: saved x86 register
99 * @ebx: saved x86 register
100 * @code: pointer into caller code
101 * @method: the method to translate
103 * This method is called by the trampoline functions for virtual
104 * methods. It inspects the caller code to find the address of the
105 * vtable slot, then calls the JIT compiler and writes the address
106 * of the compiled method back to the vtable. All virtual methods
107 * are called with: x86_call_membase (inst, basereg, disp). We always
108 * use 32 bit displacement to ensure that the length of the call
109 * instruction is 6 bytes. We need to get the value of the basereg
110 * and the constant displacement.
113 x86_magic_trampoline (int eax, int ecx, int edx, int esi, int edi,
114 int ebx, guint8 *code, MonoMethod *m)
119 guint32 breakpoint_id;
120 gpointer addr, trampoline;
122 EnterCriticalSection (metadata_section);
123 addr = mono_compile_method (m);
124 LeaveCriticalSection (metadata_section);
127 /* go to the start of the call instruction
129 * address_byte = (m << 6) | (o << 3) | reg
130 * call opcode: 0xff address_byte displacement
135 if ((code [1] != 0xe8) && (code [3] == 0xff) && ((code [4] & 0x18) == 0x10) && ((code [4] >> 6) == 1)) {
136 reg = code [4] & 0x07;
137 disp = (signed char)code [5];
139 if ((code [0] == 0xff) && ((code [1] & 0x18) == 0x10) && ((code [1] >> 6) == 2)) {
140 reg = code [1] & 0x07;
141 disp = *((gint32*)(code + 2));
142 } else if ((code [1] == 0xe8)) {
143 breakpoint_id = mono_method_has_breakpoint (m, TRUE);
145 mono_remove_breakpoint (breakpoint_id);
146 trampoline = get_breakpoint_trampoline (m, breakpoint_id, addr);
149 *((guint32*)(code + 2)) = (guint)addr - ((guint)code + 1) - 5;
152 printf ("Invalid trampoline sequence: %x %x %x %x %x %x %x\n", code [0], code [1], code [2], code [3],
153 code [4], code [5], code [6]);
154 g_assert_not_reached ();
178 g_assert_not_reached ();
183 if (m->klass->valuetype) {
184 trampoline = *((gpointer *)o) = get_unbox_trampoline (m, addr);
186 trampoline = *((gpointer *)o) = addr;
189 breakpoint_id = mono_method_has_breakpoint (m, TRUE);
191 mono_remove_breakpoint (breakpoint_id);
192 return get_breakpoint_trampoline (m, breakpoint_id, trampoline);
199 * arch_create_jit_trampoline:
200 * @method: pointer to the method info
202 * Creates a trampoline function for virtual methods. If the created
203 * code is called it first starts JIT compilation of method,
204 * and then calls the newly created method. I also replaces the
205 * corresponding vtable entry (see x86_magic_trampoline).
207 * Returns: a pointer to the newly created code
210 arch_create_jit_trampoline (MonoMethod *method)
212 MonoDomain *domain = mono_domain_get ();
214 GHashTable *jit_code_hash;
216 /* previously created trampoline code */
220 /* we immediately compile runtime provided functions */
221 if (method->iflags & METHOD_IMPL_ATTRIBUTE_RUNTIME) {
222 method->info = mono_compile_method (method);
226 /* icalls use method->addr */
227 if ((method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL) ||
228 (method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL)) {
231 if (!method->addr && (method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL))
232 mono_lookup_pinvoke_call (method);
234 #ifdef MONO_USE_EXC_TABLES
235 if (mono_method_blittable (method)) {
236 method->info = method->addr;
239 nm = mono_marshal_get_native_wrapper (method);
240 method->info = mono_compile_method (nm);
241 #ifdef MONO_USE_EXC_TABLES
247 /* check if we already have JITed code */
248 if (mono_jit_share_code)
249 jit_code_hash = mono_root_domain->jit_code_hash;
251 jit_code_hash = domain->jit_code_hash;
253 if ((code = g_hash_table_lookup (jit_code_hash, method))) {
254 mono_jit_stats.methods_lookups++;
258 if (!mono_generic_trampoline_code) {
259 mono_generic_trampoline_code = buf = g_malloc (256);
260 /* save caller save regs because we need to do a call */
261 x86_push_reg (buf, X86_EDX);
262 x86_push_reg (buf, X86_EAX);
263 x86_push_reg (buf, X86_ECX);
267 /* save the IP (caller ip) */
268 x86_push_membase (buf, X86_ESP, 16);
270 x86_push_reg (buf, X86_EBX);
271 x86_push_reg (buf, X86_EDI);
272 x86_push_reg (buf, X86_ESI);
273 x86_push_reg (buf, X86_EBP);
275 /* save method info */
276 x86_push_membase (buf, X86_ESP, 32);
277 /* get the address of lmf for the current thread */
278 x86_call_code (buf, mono_get_lmf_addr);
280 x86_push_reg (buf, X86_EAX);
281 /* push *lfm (previous_lmf) */
282 x86_push_membase (buf, X86_EAX, 0);
284 x86_mov_membase_reg (buf, X86_EAX, 0, X86_ESP, 4);
287 /* push the method info */
288 x86_push_membase (buf, X86_ESP, 44);
289 /* push the return address onto the stack */
290 x86_push_membase (buf, X86_ESP, 52);
292 /* save all register values */
293 x86_push_reg (buf, X86_EBX);
294 x86_push_reg (buf, X86_EDI);
295 x86_push_reg (buf, X86_ESI);
296 x86_push_membase (buf, X86_ESP, 64); /* EDX */
297 x86_push_membase (buf, X86_ESP, 64); /* ECX */
298 x86_push_membase (buf, X86_ESP, 64); /* EAX */
300 x86_call_code (buf, x86_magic_trampoline);
301 x86_alu_reg_imm (buf, X86_ADD, X86_ESP, 8*4);
303 /* restore LMF start */
304 /* ebx = previous_lmf */
305 x86_pop_reg (buf, X86_EBX);
307 x86_pop_reg (buf, X86_EDI);
308 /* *(lmf) = previous_lmf */
309 x86_mov_membase_reg (buf, X86_EDI, 0, X86_EBX, 4);
310 /* discard method info */
311 x86_pop_reg (buf, X86_ESI);
312 /* restore caller saved regs */
313 x86_pop_reg (buf, X86_EBP);
314 x86_pop_reg (buf, X86_ESI);
315 x86_pop_reg (buf, X86_EDI);
316 x86_pop_reg (buf, X86_EBX);
317 /* discard save IP */
318 x86_alu_reg_imm (buf, X86_ADD, X86_ESP, 4);
319 /* restore LMF end */
321 x86_alu_reg_imm (buf, X86_ADD, X86_ESP, 16);
323 /* call the compiled method */
324 x86_jump_reg (buf, X86_EAX);
326 g_assert ((buf - mono_generic_trampoline_code) <= 256);
329 code = buf = g_malloc (16);
330 x86_push_imm (buf, method);
331 x86_jump_code (buf, mono_generic_trampoline_code);
332 g_assert ((buf - code) <= 16);
334 /* store trampoline address */
337 mono_jit_stats.method_trampolines++;