2003-08-26 Martin Baulig <martin@ximian.com>
[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 #include "mini.h"
20 #include "mini-x86.h"
21
22 /* adapt to mini later... */
23 #define mono_jit_share_code (1)
24
25 /*
26  * Address of the x86 trampoline code.  This is used by the debugger to check
27  * whether a method is a trampoline.
28  */
29 guint8 *mono_generic_trampoline_code = NULL;
30
31 /*
32  * get_unbox_trampoline:
33  * @m: method pointer
34  * @addr: pointer to native code for @m
35  *
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
39  */
40 static gpointer
41 get_unbox_trampoline (MonoMethod *m, gpointer addr)
42 {
43         guint8 *code, *start;
44         int this_pos = 4;
45
46         if (!m->signature->ret->byref && MONO_TYPE_ISSTRUCT (m->signature->ret))
47                 this_pos = 8;
48             
49         start = code = g_malloc (16);
50
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);
54
55         return start;
56 }
57
58 /**
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
68  *
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.
77  */
78 static gpointer
79 x86_magic_trampoline (int eax, int ecx, int edx, int esi, int edi, 
80                       int ebx, guint8 *code, MonoMethod *m)
81 {
82         guint8 reg;
83         gint32 disp;
84         char *o;
85         gpointer addr;
86
87         EnterCriticalSection (metadata_section);
88         addr = mono_compile_method (m);
89         LeaveCriticalSection (metadata_section);
90         g_assert (addr);
91
92         /* the method was jumped to */
93         if (!code)
94                 return addr;
95
96         /* go to the start of the call instruction
97          *
98          * address_byte = (m << 6) | (o << 3) | reg
99          * call opcode: 0xff address_byte displacement
100          * 0xff m=1,o=2 imm8
101          * 0xff m=2,o=2 imm32
102          */
103         code -= 6;
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];
107         } else {
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; 
113                         return addr;
114                 } else if ((code [4] == 0xff) && (((code [5] >> 6) & 0x3) == 0) && (((code [5] >> 3) & 0x7) == 2)) {
115                         /*
116                          * This is a interface call: should check the above code can't catch it earlier 
117                          * 8b 40 30   mov    0x30(%eax),%eax
118                          * ff 10      call   *(%eax)
119                          */
120                         disp = 0;
121                         reg = code [5] & 0x07;
122                 } else {
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 ();
126                 }
127         }
128
129         switch (reg) {
130         case X86_EAX:
131                 o = (gpointer)eax;
132                 break;
133         case X86_EDX:
134                 o = (gpointer)edx;
135                 break;
136         case X86_ECX:
137                 o = (gpointer)ecx;
138                 break;
139         case X86_ESI:
140                 o = (gpointer)esi;
141                 break;
142         case X86_EDI:
143                 o = (gpointer)edi;
144                 break;
145         case X86_EBX:
146                 o = (gpointer)ebx;
147                 break;
148         default:
149                 g_assert_not_reached ();
150         }
151
152         o += disp;
153
154         if (m->klass->valuetype) {
155                 return *((gpointer *)o) = get_unbox_trampoline (m, addr);
156         } else {
157                 return *((gpointer *)o) = addr;
158         }
159 }
160
161 static guchar*
162 create_trampoline_code (int is_jump)
163 {
164         guint8 *buf, *code;
165         static guint8* generic_jump_trampoline = NULL;
166         
167         if (is_jump) {
168                 if (generic_jump_trampoline)
169                         return generic_jump_trampoline;
170         } else {
171                 if (mono_generic_trampoline_code)
172                         return mono_generic_trampoline_code;
173         }
174         
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);
180
181         /* save LMF begin */
182
183         /* save the IP (caller ip) */
184         if (is_jump)
185                 x86_push_imm (buf, 0);
186         else
187                 x86_push_membase (buf, X86_ESP, 16);
188
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);
193
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);
198         /* push lmf */
199         x86_push_reg (buf, X86_EAX); 
200         /* push *lfm (previous_lmf) */
201         x86_push_membase (buf, X86_EAX, 0);
202         /* *(lmf) = ESP */
203         x86_mov_membase_reg (buf, X86_EAX, 0, X86_ESP, 4);
204         /* save LFM end */
205
206         /* push the method info */
207         x86_push_membase (buf, X86_ESP, 44);
208         /* push the return address onto the stack */
209         if (is_jump)
210                 x86_push_imm (buf, 0);
211         else
212                 x86_push_membase (buf, X86_ESP, 52);
213
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 */
221
222         x86_call_code (buf, x86_magic_trampoline);
223         x86_alu_reg_imm (buf, X86_ADD, X86_ESP, 8*4);
224
225         /* restore LMF start */
226         /* ebx = previous_lmf */
227         x86_pop_reg (buf, X86_EBX);
228         /* edi = lmf */
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 */
242
243         x86_alu_reg_imm (buf, X86_ADD, X86_ESP, 16);
244
245         /* call the compiled method */
246         x86_jump_reg (buf, X86_EAX);
247
248         g_assert ((buf - code) <= 256);
249
250         if (is_jump) {
251                 return generic_jump_trampoline = code;
252         } else {
253                 return mono_generic_trampoline_code = code;
254         }
255 }
256
257 #define TRAMPOLINE_SIZE 10
258
259 gpointer
260 mono_arch_create_jump_trampoline (MonoMethod *method)
261 {
262         guint8 *code, *buf, *tramp;
263
264         if (method->iflags & METHOD_IMPL_ATTRIBUTE_SYNCHRONIZED)
265                 return mono_arch_create_jump_trampoline (mono_marshal_get_synchronized_wrapper (method));
266
267         /* icalls use method->addr */
268         if ((method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL) ||
269             (method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL)) {
270                 MonoMethod *nm;
271                 
272                 if (!method->addr && (method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL))
273                         mono_lookup_pinvoke_call (method);
274
275 #ifdef MONO_USE_EXC_TABLES
276                 if (mono_method_blittable (method)) {
277                         return method->addr;
278                 } else {
279 #endif
280                         nm = mono_marshal_get_native_wrapper (method);
281                         return mono_compile_method (nm);
282 #ifdef MONO_USE_EXC_TABLES
283                 }
284 #endif
285         }
286         
287         tramp = create_trampoline_code (TRUE);
288
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);
293
294         mono_jit_stats.method_trampolines++;
295
296         return code;
297
298 }
299
300 /**
301  * mono_arch_create_jit_trampoline:
302  * @method: pointer to the method info
303  *
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).
308  * 
309  * Returns: a pointer to the newly created code 
310  */
311 gpointer
312 mono_arch_create_jit_trampoline (MonoMethod *method)
313 {
314         guint8 *code, *buf;
315
316         /* previously created trampoline code */
317         if (method->info)
318                 return method->info;
319
320         /* we immediately compile runtime provided functions */
321         if (method->iflags & METHOD_IMPL_ATTRIBUTE_RUNTIME) {
322                 method->info = mono_compile_method (method);
323                 return method->info;
324         }
325
326         /* icalls use method->addr */
327         if ((method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL) ||
328             (method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL)) {
329                 MonoMethod *nm;
330                 
331                 if (!method->addr && (method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL))
332                         mono_lookup_pinvoke_call (method);
333
334 #ifdef MONO_USE_EXC_TABLES
335                 if (mono_method_blittable (method)) {
336                         method->info = method->addr;
337                 } else {
338 #endif
339                         nm = mono_marshal_get_native_wrapper (method);
340                         method->info = mono_compile_method (nm);
341 #ifdef MONO_USE_EXC_TABLES
342                 }
343 #endif
344                 return method->info;
345         }
346         
347         if (method->iflags & METHOD_IMPL_ATTRIBUTE_SYNCHRONIZED)
348                 return mono_arch_create_jit_trampoline (mono_marshal_get_synchronized_wrapper (method));
349
350         create_trampoline_code (FALSE);
351
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);
356
357         /* store trampoline address */
358         method->info = code;
359
360         mono_jit_stats.method_trampolines++;
361
362         return code;
363 }
364
365 /*
366  * This method is only called when running in the Mono Debugger.
367  */
368 gpointer
369 mono_debugger_create_notification_function (gpointer *notification_address)
370 {
371         guint8 *ptr, *buf;
372
373         ptr = buf = g_malloc0 (16);
374         x86_breakpoint (buf);
375         if (notification_address)
376                 *notification_address = buf;
377         x86_ret (buf);
378
379         return ptr;
380 }
381