2003-09-08 Zoltan Varga <vargaz@freemail.hu>
[mono.git] / mono / mini / tramp-x86.c
1 /*
2  * trampoline.c: JIT trampoline code
3  *
4  * Authors:
5  *   Dietmar Maurer (dietmar@ximian.com)
6  *
7  * (C) 2001 Ximian, Inc.
8  */
9
10 #include <config.h>
11 #include <glib.h>
12
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>
18
19 #ifdef HAVE_VALGRIND_MEMCHECK_H
20 #include <valgrind/memcheck.h>
21 #endif
22
23 #include "mini.h"
24 #include "mini-x86.h"
25
26 /* adapt to mini later... */
27 #define mono_jit_share_code (1)
28
29 /*
30  * Address of the x86 trampoline code.  This is used by the debugger to check
31  * whether a method is a trampoline.
32  */
33 guint8 *mono_generic_trampoline_code = NULL;
34
35 /*
36  * get_unbox_trampoline:
37  * @m: method pointer
38  * @addr: pointer to native code for @m
39  *
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
43  */
44 static gpointer
45 get_unbox_trampoline (MonoMethod *m, gpointer addr)
46 {
47         guint8 *code, *start;
48         int this_pos = 4;
49
50         if (!m->signature->ret->byref && MONO_TYPE_ISSTRUCT (m->signature->ret))
51                 this_pos = 8;
52             
53         start = code = g_malloc (16);
54
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);
58
59         return start;
60 }
61
62 /**
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
72  *
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.
81  */
82 static gpointer
83 x86_magic_trampoline (int eax, int ecx, int edx, int esi, int edi, 
84                       int ebx, guint8 *code, MonoMethod *m)
85 {
86         guint8 reg;
87         gint32 disp;
88         char *o;
89         gpointer addr;
90
91         addr = mono_compile_method (m);
92         g_assert (addr);
93
94         /* the method was jumped to */
95         if (!code)
96                 return addr;
97
98         /* go to the start of the call instruction
99          *
100          * address_byte = (m << 6) | (o << 3) | reg
101          * call opcode: 0xff address_byte displacement
102          * 0xff m=1,o=2 imm8
103          * 0xff m=2,o=2 imm32
104          */
105         code -= 6;
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];
109         } else {
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)) {
114                         MonoJitInfo *ji = 
115                                 mono_jit_info_table_find (mono_domain_get (), code);
116                         MonoJitInfo *target_ji = 
117                                 mono_jit_info_table_find (mono_domain_get (), addr);
118
119                         /*
120                          * If the call was made from domain-neutral to domain-specific 
121                          * code, we can't patch the call site.
122                          */
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);
128 #endif
129                         }
130                         return addr;
131                 } else if ((code [4] == 0xff) && (((code [5] >> 6) & 0x3) == 0) && (((code [5] >> 3) & 0x7) == 2)) {
132                         /*
133                          * This is a interface call: should check the above code can't catch it earlier 
134                          * 8b 40 30   mov    0x30(%eax),%eax
135                          * ff 10      call   *(%eax)
136                          */
137                         disp = 0;
138                         reg = code [5] & 0x07;
139                 } else {
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 ();
143                 }
144         }
145
146         switch (reg) {
147         case X86_EAX:
148                 o = (gpointer)eax;
149                 break;
150         case X86_EDX:
151                 o = (gpointer)edx;
152                 break;
153         case X86_ECX:
154                 o = (gpointer)ecx;
155                 break;
156         case X86_ESI:
157                 o = (gpointer)esi;
158                 break;
159         case X86_EDI:
160                 o = (gpointer)edi;
161                 break;
162         case X86_EBX:
163                 o = (gpointer)ebx;
164                 break;
165         default:
166                 g_assert_not_reached ();
167         }
168
169         o += disp;
170
171         if (m->klass->valuetype)
172                 addr = get_unbox_trampoline (m, addr);
173
174         *((gpointer *)o) = addr;
175
176         return addr;
177 }
178
179 static guchar*
180 create_trampoline_code (int is_jump)
181 {
182         guint8 *buf, *code;
183         static guint8* generic_jump_trampoline = NULL;
184         
185         if (is_jump) {
186                 if (generic_jump_trampoline)
187                         return generic_jump_trampoline;
188         } else {
189                 if (mono_generic_trampoline_code)
190                         return mono_generic_trampoline_code;
191         }
192         
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);
198
199         /* save LMF begin */
200
201         /* save the IP (caller ip) */
202         if (is_jump)
203                 x86_push_imm (buf, 0);
204         else
205                 x86_push_membase (buf, X86_ESP, 16);
206
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);
211
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);
216         /* push lmf */
217         x86_push_reg (buf, X86_EAX); 
218         /* push *lfm (previous_lmf) */
219         x86_push_membase (buf, X86_EAX, 0);
220         /* *(lmf) = ESP */
221         x86_mov_membase_reg (buf, X86_EAX, 0, X86_ESP, 4);
222         /* save LFM end */
223
224         /* push the method info */
225         x86_push_membase (buf, X86_ESP, 44);
226         /* push the return address onto the stack */
227         if (is_jump)
228                 x86_push_imm (buf, 0);
229         else
230                 x86_push_membase (buf, X86_ESP, 52);
231
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 */
239
240         x86_call_code (buf, x86_magic_trampoline);
241         x86_alu_reg_imm (buf, X86_ADD, X86_ESP, 8*4);
242
243         /* restore LMF start */
244         /* ebx = previous_lmf */
245         x86_pop_reg (buf, X86_EBX);
246         /* edi = lmf */
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 */
260
261         x86_alu_reg_imm (buf, X86_ADD, X86_ESP, 16);
262
263         /* call the compiled method */
264         x86_jump_reg (buf, X86_EAX);
265
266         g_assert ((buf - code) <= 256);
267
268         if (is_jump) {
269                 return generic_jump_trampoline = code;
270         } else {
271                 return mono_generic_trampoline_code = code;
272         }
273 }
274
275 #define TRAMPOLINE_SIZE 10
276
277 gpointer
278 mono_arch_create_jump_trampoline (MonoMethod *method)
279 {
280         guint8 *code, *buf, *tramp;
281
282         if (method->iflags & METHOD_IMPL_ATTRIBUTE_SYNCHRONIZED)
283                 return mono_arch_create_jump_trampoline (mono_marshal_get_synchronized_wrapper (method));
284
285         /* icalls use method->addr */
286         if ((method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL) ||
287             (method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL)) {
288                 MonoMethod *nm;
289                 
290                 if (!method->addr && (method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL))
291                         mono_lookup_pinvoke_call (method);
292
293 #ifdef MONO_USE_EXC_TABLES
294                 if (mono_method_blittable (method)) {
295                         return method->addr;
296                 } else {
297 #endif
298                         nm = mono_marshal_get_native_wrapper (method);
299                         return mono_compile_method (nm);
300 #ifdef MONO_USE_EXC_TABLES
301                 }
302 #endif
303         }
304         
305         tramp = create_trampoline_code (TRUE);
306
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);
311
312         mono_jit_stats.method_trampolines++;
313
314         return code;
315
316 }
317
318 /**
319  * mono_arch_create_jit_trampoline:
320  * @method: pointer to the method info
321  *
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).
326  * 
327  * Returns: a pointer to the newly created code 
328  */
329 gpointer
330 mono_arch_create_jit_trampoline (MonoMethod *method)
331 {
332         guint8 *code, *buf;
333
334         /* previously created trampoline code */
335         if (method->info)
336                 return method->info;
337
338         /* we immediately compile runtime provided functions */
339         if (method->iflags & METHOD_IMPL_ATTRIBUTE_RUNTIME) {
340                 method->info = mono_compile_method (method);
341                 return method->info;
342         }
343
344         /* icalls use method->addr */
345         if ((method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL) ||
346             (method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL)) {
347                 MonoMethod *nm;
348                 
349                 if (!method->addr && (method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL))
350                         mono_lookup_pinvoke_call (method);
351
352 #ifdef MONO_USE_EXC_TABLES
353                 if (mono_method_blittable (method)) {
354                         method->info = method->addr;
355                 } else {
356 #endif
357                         nm = mono_marshal_get_native_wrapper (method);
358                         method->info = mono_compile_method (nm);
359 #ifdef MONO_USE_EXC_TABLES
360                 }
361 #endif
362                 return method->info;
363         }
364         
365         if (method->iflags & METHOD_IMPL_ATTRIBUTE_SYNCHRONIZED)
366                 return mono_arch_create_jit_trampoline (mono_marshal_get_synchronized_wrapper (method));
367
368         create_trampoline_code (FALSE);
369
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);
374
375         /* store trampoline address */
376         method->info = code;
377
378         mono_jit_stats.method_trampolines++;
379
380         return code;
381 }
382
383 /*
384  * This method is only called when running in the Mono Debugger.
385  */
386 gpointer
387 mono_debugger_create_notification_function (gpointer *notification_address)
388 {
389         guint8 *ptr, *buf;
390
391         ptr = buf = g_malloc0 (16);
392         x86_breakpoint (buf);
393         if (notification_address)
394                 *notification_address = buf;
395         x86_ret (buf);
396
397         return ptr;
398 }
399