This commit was manufactured by cvs2svn to create branch 'mono-1-0'.
[mono.git] / mono / mini / tramp-x86.c
1 /*
2  * tramp-x86.c: JIT trampoline code for x86
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 typedef enum {
27         MONO_TRAMPOLINE_GENERIC,
28         MONO_TRAMPOLINE_JUMP,
29         MONO_TRAMPOLINE_CLASS_INIT
30 } MonoTrampolineType;
31
32 /* adapt to mini later... */
33 #define mono_jit_share_code (1)
34
35 /*
36  * Address of the x86 trampoline code.  This is used by the debugger to check
37  * whether a method is a trampoline.
38  */
39 guint8 *mono_generic_trampoline_code = NULL;
40
41 /*
42  * get_unbox_trampoline:
43  * @m: method pointer
44  * @addr: pointer to native code for @m
45  *
46  * when value type methods are called through the vtable we need to unbox the
47  * this argument. This method returns a pointer to a trampoline which does
48  * unboxing before calling the method
49  */
50 static gpointer
51 get_unbox_trampoline (MonoMethod *m, gpointer addr)
52 {
53         guint8 *code, *start;
54         int this_pos = 4;
55
56         if (!m->signature->ret->byref && MONO_TYPE_ISSTRUCT (m->signature->ret))
57                 this_pos = 8;
58             
59         start = code = g_malloc (16);
60
61         x86_alu_membase_imm (code, X86_ADD, X86_ESP, this_pos, sizeof (MonoObject));
62         x86_jump_code (code, addr);
63         g_assert ((code - start) < 16);
64
65         return start;
66 }
67
68 /**
69  * x86_magic_trampoline:
70  * @eax: saved x86 register 
71  * @ecx: saved x86 register 
72  * @edx: saved x86 register 
73  * @esi: saved x86 register 
74  * @edi: saved x86 register 
75  * @ebx: saved x86 register
76  * @code: pointer into caller code
77  * @method: the method to translate
78  *
79  * This method is called by the trampoline functions for virtual
80  * methods. It inspects the caller code to find the address of the
81  * vtable slot, then calls the JIT compiler and writes the address
82  * of the compiled method back to the vtable. All virtual methods 
83  * are called with: x86_call_membase (inst, basereg, disp). We always
84  * use 32 bit displacement to ensure that the length of the call 
85  * instruction is 6 bytes. We need to get the value of the basereg 
86  * and the constant displacement.
87  */
88 static gpointer
89 x86_magic_trampoline (int eax, int ecx, int edx, int esi, int edi, 
90                       int ebx, guint8 *code, MonoMethod *m)
91 {
92         guint8 reg;
93         gint32 disp;
94         char *o;
95         gpointer addr;
96
97         addr = mono_compile_method (m);
98         g_assert (addr);
99
100         /* the method was jumped to */
101         if (!code)
102                 return addr;
103
104         /* go to the start of the call instruction
105          *
106          * address_byte = (m << 6) | (o << 3) | reg
107          * call opcode: 0xff address_byte displacement
108          * 0xff m=1,o=2 imm8
109          * 0xff m=2,o=2 imm32
110          */
111         code -= 6;
112         if ((code [1] != 0xe8) && (code [3] == 0xff) && ((code [4] & 0x18) == 0x10) && ((code [4] >> 6) == 1)) {
113                 reg = code [4] & 0x07;
114                 disp = (signed char)code [5];
115         } else {
116                 if ((code [0] == 0xff) && ((code [1] & 0x18) == 0x10) && ((code [1] >> 6) == 2)) {
117                         reg = code [1] & 0x07;
118                         disp = *((gint32*)(code + 2));
119                 } else if ((code [1] == 0xe8)) {
120                         MonoJitInfo *ji = 
121                                 mono_jit_info_table_find (mono_domain_get (), code);
122                         MonoJitInfo *target_ji = 
123                                 mono_jit_info_table_find (mono_domain_get (), addr);
124
125                         /* The first part of the condition means an icall without a wrapper */
126                         if ((!target_ji && m->addr) || mono_method_same_domain (ji, target_ji)) {
127                                 if (!mono_running_on_valgrind ()) {
128                                         InterlockedExchange ((gint32*)(code + 2), (guint)addr - ((guint)code + 1) - 5);
129
130 #ifdef HAVE_VALGRIND_MEMCHECK_H
131                                         /* Tell valgrind to recompile the patched code */
132                                         //VALGRIND_DISCARD_TRANSLATIONS (code + 2, code + 6);
133 #endif
134                                 }
135                         }
136                         return addr;
137                 } else if ((code [4] == 0xff) && (((code [5] >> 6) & 0x3) == 0) && (((code [5] >> 3) & 0x7) == 2)) {
138                         /*
139                          * This is a interface call: should check the above code can't catch it earlier 
140                          * 8b 40 30   mov    0x30(%eax),%eax
141                          * ff 10      call   *(%eax)
142                          */
143                         disp = 0;
144                         reg = code [5] & 0x07;
145                 } else {
146                         printf ("Invalid trampoline sequence: %x %x %x %x %x %x %x\n", code [0], code [1], code [2], code [3],
147                                 code [4], code [5], code [6]);
148                         g_assert_not_reached ();
149                 }
150         }
151
152         switch (reg) {
153         case X86_EAX:
154                 o = (gpointer)eax;
155                 break;
156         case X86_EDX:
157                 o = (gpointer)edx;
158                 break;
159         case X86_ECX:
160                 o = (gpointer)ecx;
161                 break;
162         case X86_ESI:
163                 o = (gpointer)esi;
164                 break;
165         case X86_EDI:
166                 o = (gpointer)edi;
167                 break;
168         case X86_EBX:
169                 o = (gpointer)ebx;
170                 break;
171         default:
172                 g_assert_not_reached ();
173         }
174
175         o += disp;
176
177         if (m->klass->valuetype)
178                 addr = get_unbox_trampoline (m, addr);
179
180         *((gpointer *)o) = addr;
181
182         return addr;
183 }
184
185 /**
186  * x86_class_init_trampoline:
187  * @eax: saved x86 register 
188  * @ecx: saved x86 register 
189  * @edx: saved x86 register 
190  * @esi: saved x86 register 
191  * @edi: saved x86 register 
192  * @ebx: saved x86 register
193  * @code: pointer into caller code
194  * @vtable: the type to initialize
195  *
196  * This method calls mono_runtime_class_init () to run the static constructor
197  * for the type, then patches the caller code so it is not called again.
198  */
199 static void
200 x86_class_init_trampoline (int eax, int ecx, int edx, int esi, int edi, 
201                                                    int ebx, guint8 *code, MonoVTable *vtable)
202 {
203         mono_runtime_class_init (vtable);
204
205         code -= 5;
206         if (code [0] == 0xe8) {
207                 if (!mono_running_on_valgrind ()) {
208                         guint32 ops;
209                         /*
210                          * Thread safe code patching using the algorithm from the paper
211                          * 'Practicing JUDO: Java Under Dynamic Optimizations'
212                          */
213                         /* 
214                          * First atomically change the the first 2 bytes of the call to a
215                          * spinning jump.
216                          */
217                         ops = 0xfeeb;
218                         InterlockedExchange ((gint32*)code, ops);
219
220                         /* Then change the other bytes to a nop */
221                         code [2] = 0x90;
222                         code [3] = 0x90;
223                         code [4] = 0x90;
224
225                         /* Then atomically change the first 4 bytes to a nop as well */
226                         ops = 0x90909090;
227                         InterlockedExchange ((guint32*)code, ops);
228
229 #ifdef HAVE_VALGRIND_MEMCHECK_H
230                         /* FIXME: the calltree skin trips on the self modifying code above */
231
232                         /* Tell valgrind to recompile the patched code */
233                         //VALGRIND_DISCARD_TRANSLATIONS (code, code + 8);
234 #endif
235                 }
236         }
237         else
238                 if (code [0] == 0x90 || code [0] == 0xeb)
239                         /* Already changed by another thread */
240                         ;
241                 else {
242                         printf ("Invalid trampoline sequence: %x %x %x %x %x %x %x\n", code [0], code [1], code [2], code [3],
243                                 code [4], code [5], code [6]);
244                         g_assert_not_reached ();
245                 }
246 }
247
248 static guchar*
249 create_trampoline_code (MonoTrampolineType tramp_type)
250 {
251         guint8 *buf, *code;
252         static guint8* generic_jump_trampoline = NULL;
253         static guint8 *generic_class_init_trampoline = NULL;
254
255         switch (tramp_type) {
256         case MONO_TRAMPOLINE_GENERIC:
257                 if (mono_generic_trampoline_code)
258                         return mono_generic_trampoline_code;
259                 break;
260         case MONO_TRAMPOLINE_JUMP:
261                 if (generic_jump_trampoline)
262                         return generic_jump_trampoline;
263                 break;
264         case MONO_TRAMPOLINE_CLASS_INIT:
265                 if (generic_class_init_trampoline)
266                         return generic_class_init_trampoline;
267                 break;
268         }
269
270         code = buf = g_malloc (256);
271         /* save caller save regs because we need to do a call */ 
272         x86_push_reg (buf, X86_EDX);
273         x86_push_reg (buf, X86_EAX);
274         x86_push_reg (buf, X86_ECX);
275
276         /* save LMF begin */
277
278         /* save the IP (caller ip) */
279         if (tramp_type == MONO_TRAMPOLINE_JUMP)
280                 x86_push_imm (buf, 0);
281         else
282                 x86_push_membase (buf, X86_ESP, 16);
283
284         x86_push_reg (buf, X86_EBP);
285         x86_push_reg (buf, X86_ESI);
286         x86_push_reg (buf, X86_EDI);
287         x86_push_reg (buf, X86_EBX);
288
289         /* save method info */
290         x86_push_membase (buf, X86_ESP, 32);
291         /* get the address of lmf for the current thread */
292         x86_call_code (buf, mono_get_lmf_addr);
293         /* push lmf */
294         x86_push_reg (buf, X86_EAX); 
295         /* push *lfm (previous_lmf) */
296         x86_push_membase (buf, X86_EAX, 0);
297         /* *(lmf) = ESP */
298         x86_mov_membase_reg (buf, X86_EAX, 0, X86_ESP, 4);
299         /* save LFM end */
300
301         /* push the method info */
302         x86_push_membase (buf, X86_ESP, 44);
303         /* push the return address onto the stack */
304         if (tramp_type == MONO_TRAMPOLINE_JUMP)
305                 x86_push_imm (buf, 0);
306         else
307                 x86_push_membase (buf, X86_ESP, 52);
308
309         /* save all register values */
310         x86_push_reg (buf, X86_EBX);
311         x86_push_reg (buf, X86_EDI);
312         x86_push_reg (buf, X86_ESI);
313         x86_push_membase (buf, X86_ESP, 64); /* EDX */
314         x86_push_membase (buf, X86_ESP, 64); /* ECX */
315         x86_push_membase (buf, X86_ESP, 64); /* EAX */
316
317         if (tramp_type == MONO_TRAMPOLINE_CLASS_INIT)
318                 x86_call_code (buf, x86_class_init_trampoline);
319         else
320                 x86_call_code (buf, x86_magic_trampoline);
321         x86_alu_reg_imm (buf, X86_ADD, X86_ESP, 8*4);
322
323         /* restore LMF start */
324         /* ebx = previous_lmf */
325         x86_pop_reg (buf, X86_EBX);
326         /* edi = lmf */
327         x86_pop_reg (buf, X86_EDI);
328         /* *(lmf) = previous_lmf */
329         x86_mov_membase_reg (buf, X86_EDI, 0, X86_EBX, 4);
330         /* discard method info */
331         x86_pop_reg (buf, X86_ESI);
332         /* restore caller saved regs */
333         x86_pop_reg (buf, X86_EBX);
334         x86_pop_reg (buf, X86_EDI);
335         x86_pop_reg (buf, X86_ESI);
336         x86_pop_reg (buf, X86_EBP);
337
338         /* discard save IP */
339         x86_alu_reg_imm (buf, X86_ADD, X86_ESP, 4);             
340         /* restore LMF end */
341
342         x86_alu_reg_imm (buf, X86_ADD, X86_ESP, 16);
343
344         if (tramp_type == MONO_TRAMPOLINE_CLASS_INIT)
345                 x86_ret (buf);
346         else
347                 /* call the compiled method */
348                 x86_jump_reg (buf, X86_EAX);
349
350         g_assert ((buf - code) <= 256);
351
352         switch (tramp_type) {
353         case MONO_TRAMPOLINE_GENERIC:
354                 mono_generic_trampoline_code = code;
355                 break;
356         case MONO_TRAMPOLINE_JUMP:
357                 generic_jump_trampoline = code;
358                 break;
359         case MONO_TRAMPOLINE_CLASS_INIT:
360                 generic_class_init_trampoline = code;
361                 break;
362         }
363
364         return code;
365 }
366
367 #define TRAMPOLINE_SIZE 10
368
369 MonoJitInfo*
370 mono_arch_create_jump_trampoline (MonoMethod *method)
371 {
372         guint8 *code, *buf, *tramp;
373         MonoJitInfo *ji;
374         
375         tramp = create_trampoline_code (MONO_TRAMPOLINE_JUMP);
376
377         code = buf = g_malloc (TRAMPOLINE_SIZE);
378         x86_push_imm (buf, method);
379         x86_jump_code (buf, tramp);
380         g_assert ((buf - code) <= TRAMPOLINE_SIZE);
381
382         ji = g_new0 (MonoJitInfo, 1);
383         ji->method = method;
384         ji->code_start = code;
385         ji->code_size = buf - code;
386
387         mono_arch_flush_icache (ji->code_start, ji->code_size);
388
389         mono_jit_stats.method_trampolines++;
390
391         return ji;
392
393 }
394
395 /**
396  * mono_arch_create_jit_trampoline:
397  * @method: pointer to the method info
398  *
399  * Creates a trampoline function for virtual methods. If the created
400  * code is called it first starts JIT compilation of method,
401  * and then calls the newly created method. I also replaces the
402  * corresponding vtable entry (see x86_magic_trampoline).
403  * 
404  * Returns: a pointer to the newly created code 
405  */
406 gpointer
407 mono_arch_create_jit_trampoline (MonoMethod *method)
408 {
409         guint8 *code, *buf, *tramp;
410
411         /* previously created trampoline code */
412         if (method->info)
413                 return method->info;
414
415         if (method->iflags & METHOD_IMPL_ATTRIBUTE_SYNCHRONIZED)
416                 return mono_arch_create_jit_trampoline (mono_marshal_get_synchronized_wrapper (method));
417
418         tramp = create_trampoline_code (MONO_TRAMPOLINE_GENERIC);
419
420         code = buf = g_malloc (TRAMPOLINE_SIZE);
421         x86_push_imm (buf, method);
422         x86_jump_code (buf, tramp);
423         g_assert ((buf - code) <= TRAMPOLINE_SIZE);
424
425         /* store trampoline address */
426         method->info = code;
427
428         mono_jit_stats.method_trampolines++;
429
430         return code;
431 }
432
433 /**
434  * mono_arch_create_class_init_trampoline:
435  *  @vtable: the type to initialize
436  *
437  * Creates a trampoline function to run a type initializer. 
438  * If the trampoline is called, it calls mono_runtime_class_init with the
439  * given vtable, then patches the caller code so it does not get called any
440  * more.
441  * 
442  * Returns: a pointer to the newly created code 
443  */
444 gpointer
445 mono_arch_create_class_init_trampoline (MonoVTable *vtable)
446 {
447         guint8 *code, *buf, *tramp;
448
449         tramp = create_trampoline_code (MONO_TRAMPOLINE_CLASS_INIT);
450
451         code = buf = g_malloc (TRAMPOLINE_SIZE);
452         x86_push_imm (buf, vtable);
453         x86_jump_code (buf, tramp);
454         g_assert ((buf - code) <= TRAMPOLINE_SIZE);
455
456         mono_jit_stats.method_trampolines++;
457
458         return code;
459 }
460
461 /*
462  * This method is only called when running in the Mono Debugger.
463  */
464 gpointer
465 mono_debugger_create_notification_function (gpointer *notification_address)
466 {
467         guint8 *ptr, *buf;
468
469         ptr = buf = g_malloc0 (16);
470         x86_breakpoint (buf);
471         if (notification_address)
472                 *notification_address = buf;
473         x86_ret (buf);
474
475         return ptr;
476 }
477