Sat Dec 13 17:49:10 CET 2003 Paolo Molaro <lupus@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 #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                                 InterlockedExchange ((gint32*)(code + 2), (guint)addr - ((guint)code + 1) - 5);
128 #ifdef HAVE_VALGRIND_MEMCHECK_H
129                                 /* Tell valgrind to recompile the patched code */
130                                 VALGRIND_DISCARD_TRANSLATIONS (code + 2, code + 6);
131 #endif
132                         }
133                         return addr;
134                 } else if ((code [4] == 0xff) && (((code [5] >> 6) & 0x3) == 0) && (((code [5] >> 3) & 0x7) == 2)) {
135                         /*
136                          * This is a interface call: should check the above code can't catch it earlier 
137                          * 8b 40 30   mov    0x30(%eax),%eax
138                          * ff 10      call   *(%eax)
139                          */
140                         disp = 0;
141                         reg = code [5] & 0x07;
142                 } else {
143                         printf ("Invalid trampoline sequence: %x %x %x %x %x %x %x\n", code [0], code [1], code [2], code [3],
144                                 code [4], code [5], code [6]);
145                         g_assert_not_reached ();
146                 }
147         }
148
149         switch (reg) {
150         case X86_EAX:
151                 o = (gpointer)eax;
152                 break;
153         case X86_EDX:
154                 o = (gpointer)edx;
155                 break;
156         case X86_ECX:
157                 o = (gpointer)ecx;
158                 break;
159         case X86_ESI:
160                 o = (gpointer)esi;
161                 break;
162         case X86_EDI:
163                 o = (gpointer)edi;
164                 break;
165         case X86_EBX:
166                 o = (gpointer)ebx;
167                 break;
168         default:
169                 g_assert_not_reached ();
170         }
171
172         o += disp;
173
174         if (m->klass->valuetype)
175                 addr = get_unbox_trampoline (m, addr);
176
177         *((gpointer *)o) = addr;
178
179         return addr;
180 }
181
182 /**
183  * x86_class_init_trampoline:
184  * @eax: saved x86 register 
185  * @ecx: saved x86 register 
186  * @edx: saved x86 register 
187  * @esi: saved x86 register 
188  * @edi: saved x86 register 
189  * @ebx: saved x86 register
190  * @code: pointer into caller code
191  * @vtable: the type to initialize
192  *
193  * This method calls mono_runtime_class_init () to run the static constructor
194  * for the type, then patches the caller code so it is not called again.
195  */
196 static void
197 x86_class_init_trampoline (int eax, int ecx, int edx, int esi, int edi, 
198                                                    int ebx, guint8 *code, MonoVTable *vtable)
199 {
200         mono_runtime_class_init (vtable);
201
202         code -= 5;
203         if (code [0] == 0xe8) {
204                 gboolean do_patch = TRUE;
205
206 #ifdef HAVE_VALGRIND_MEMCHECK_H
207                 if (RUNNING_ON_VALGRIND)
208                         do_patch = FALSE;
209 #endif
210
211                 if (do_patch) {
212                         guint32 ops;
213                         /*
214                          * Thread safe code patching using the algorithm from the paper
215                          * 'Practicing JUDO: Java Under Dynamic Optimizations'
216                          */
217                         /* 
218                          * First atomically change the the first 2 bytes of the call to a
219                          * spinning jump.
220                          */
221                         ops = 0xfeeb;
222                         InterlockedExchange ((gint32*)code, ops);
223
224                         /* Then change the other bytes to a nop */
225                         code [2] = 0x90;
226                         code [3] = 0x90;
227                         code [4] = 0x90;
228
229                         /* Then atomically change the first 4 bytes to a nop as well */
230                         ops = 0x90909090;
231                         InterlockedExchange ((guint32*)code, ops);
232
233 #ifdef HAVE_VALGRIND_MEMCHECK_H
234                         /* FIXME: the calltree skin trips on the self modifying code above */
235
236                         /* Tell valgrind to recompile the patched code */
237                         //VALGRIND_DISCARD_TRANSLATIONS (code, code + 8);
238 #endif
239                 }
240         }
241         else
242                 if (code [0] == 0x90 || code [0] == 0xeb)
243                         /* Already changed by another thread */
244                         ;
245                 else {
246                         printf ("Invalid trampoline sequence: %x %x %x %x %x %x %x\n", code [0], code [1], code [2], code [3],
247                                 code [4], code [5], code [6]);
248                         g_assert_not_reached ();
249                 }
250 }
251
252 static guchar*
253 create_trampoline_code (MonoTrampolineType tramp_type)
254 {
255         guint8 *buf, *code;
256         static guint8* generic_jump_trampoline = NULL;
257         static guint8 *generic_class_init_trampoline = NULL;
258
259         switch (tramp_type) {
260         case MONO_TRAMPOLINE_GENERIC:
261                 if (mono_generic_trampoline_code)
262                         return mono_generic_trampoline_code;
263                 break;
264         case MONO_TRAMPOLINE_JUMP:
265                 if (generic_jump_trampoline)
266                         return generic_jump_trampoline;
267                 break;
268         case MONO_TRAMPOLINE_CLASS_INIT:
269                 if (generic_class_init_trampoline)
270                         return generic_class_init_trampoline;
271                 break;
272         }
273
274         code = buf = g_malloc (256);
275         /* save caller save regs because we need to do a call */ 
276         x86_push_reg (buf, X86_EDX);
277         x86_push_reg (buf, X86_EAX);
278         x86_push_reg (buf, X86_ECX);
279
280         /* save LMF begin */
281
282         /* save the IP (caller ip) */
283         if (tramp_type == MONO_TRAMPOLINE_JUMP)
284                 x86_push_imm (buf, 0);
285         else
286                 x86_push_membase (buf, X86_ESP, 16);
287
288         x86_push_reg (buf, X86_EBX);
289         x86_push_reg (buf, X86_EDI);
290         x86_push_reg (buf, X86_ESI);
291         x86_push_reg (buf, X86_EBP);
292
293         /* save method info */
294         x86_push_membase (buf, X86_ESP, 32);
295         /* get the address of lmf for the current thread */
296         x86_call_code (buf, mono_get_lmf_addr);
297         /* push lmf */
298         x86_push_reg (buf, X86_EAX); 
299         /* push *lfm (previous_lmf) */
300         x86_push_membase (buf, X86_EAX, 0);
301         /* *(lmf) = ESP */
302         x86_mov_membase_reg (buf, X86_EAX, 0, X86_ESP, 4);
303         /* save LFM end */
304
305         /* push the method info */
306         x86_push_membase (buf, X86_ESP, 44);
307         /* push the return address onto the stack */
308         if (tramp_type == MONO_TRAMPOLINE_JUMP)
309                 x86_push_imm (buf, 0);
310         else
311                 x86_push_membase (buf, X86_ESP, 52);
312
313         /* save all register values */
314         x86_push_reg (buf, X86_EBX);
315         x86_push_reg (buf, X86_EDI);
316         x86_push_reg (buf, X86_ESI);
317         x86_push_membase (buf, X86_ESP, 64); /* EDX */
318         x86_push_membase (buf, X86_ESP, 64); /* ECX */
319         x86_push_membase (buf, X86_ESP, 64); /* EAX */
320
321         if (tramp_type == MONO_TRAMPOLINE_CLASS_INIT)
322                 x86_call_code (buf, x86_class_init_trampoline);
323         else
324                 x86_call_code (buf, x86_magic_trampoline);
325         x86_alu_reg_imm (buf, X86_ADD, X86_ESP, 8*4);
326
327         /* restore LMF start */
328         /* ebx = previous_lmf */
329         x86_pop_reg (buf, X86_EBX);
330         /* edi = lmf */
331         x86_pop_reg (buf, X86_EDI);
332         /* *(lmf) = previous_lmf */
333         x86_mov_membase_reg (buf, X86_EDI, 0, X86_EBX, 4);
334         /* discard method info */
335         x86_pop_reg (buf, X86_ESI);
336         /* restore caller saved regs */
337         x86_pop_reg (buf, X86_EBP);
338         x86_pop_reg (buf, X86_ESI);
339         x86_pop_reg (buf, X86_EDI);
340         x86_pop_reg (buf, X86_EBX);
341         /* discard save IP */
342         x86_alu_reg_imm (buf, X86_ADD, X86_ESP, 4);             
343         /* restore LMF end */
344
345         x86_alu_reg_imm (buf, X86_ADD, X86_ESP, 16);
346
347         if (tramp_type == MONO_TRAMPOLINE_CLASS_INIT)
348                 x86_ret (buf);
349         else
350                 /* call the compiled method */
351                 x86_jump_reg (buf, X86_EAX);
352
353         g_assert ((buf - code) <= 256);
354
355         switch (tramp_type) {
356         case MONO_TRAMPOLINE_GENERIC:
357                 mono_generic_trampoline_code = code;
358                 break;
359         case MONO_TRAMPOLINE_JUMP:
360                 generic_jump_trampoline = code;
361                 break;
362         case MONO_TRAMPOLINE_CLASS_INIT:
363                 generic_class_init_trampoline = code;
364                 break;
365         }
366
367         return code;
368 }
369
370 #define TRAMPOLINE_SIZE 10
371
372 gpointer
373 mono_arch_create_jump_trampoline (MonoMethod *method)
374 {
375         guint8 *code, *buf, *tramp;
376
377         if (method->iflags & METHOD_IMPL_ATTRIBUTE_SYNCHRONIZED)
378                 return mono_arch_create_jump_trampoline (mono_marshal_get_synchronized_wrapper (method));
379
380         /* icalls use method->addr */
381         if ((method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL) ||
382             (method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL)) {
383                 MonoMethod *nm;
384                 
385                 if (!method->addr) {
386                         if (method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL)
387                                 method->addr = mono_lookup_internal_call (method);
388                         if (method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL)
389                                 mono_lookup_pinvoke_call (method);
390                 }
391 #ifdef MONO_USE_EXC_TABLES
392                 if (mono_method_blittable (method)) {
393                         return method->addr;
394                 } else {
395 #endif
396                         nm = mono_marshal_get_native_wrapper (method);
397                         return mono_compile_method (nm);
398 #ifdef MONO_USE_EXC_TABLES
399                 }
400 #endif
401         }
402         
403         tramp = create_trampoline_code (MONO_TRAMPOLINE_JUMP);
404
405         code = buf = g_malloc (TRAMPOLINE_SIZE);
406         x86_push_imm (buf, method);
407         x86_jump_code (buf, tramp);
408         g_assert ((buf - code) <= TRAMPOLINE_SIZE);
409
410         mono_jit_stats.method_trampolines++;
411
412         return code;
413
414 }
415
416 /**
417  * mono_arch_create_jit_trampoline:
418  * @method: pointer to the method info
419  *
420  * Creates a trampoline function for virtual methods. If the created
421  * code is called it first starts JIT compilation of method,
422  * and then calls the newly created method. I also replaces the
423  * corresponding vtable entry (see x86_magic_trampoline).
424  * 
425  * Returns: a pointer to the newly created code 
426  */
427 gpointer
428 mono_arch_create_jit_trampoline (MonoMethod *method)
429 {
430         guint8 *code, *buf, *tramp;
431
432         /* previously created trampoline code */
433         if (method->info)
434                 return method->info;
435
436         if (method->iflags & METHOD_IMPL_ATTRIBUTE_SYNCHRONIZED)
437                 return mono_arch_create_jit_trampoline (mono_marshal_get_synchronized_wrapper (method));
438
439         tramp = create_trampoline_code (MONO_TRAMPOLINE_GENERIC);
440
441         code = buf = g_malloc (TRAMPOLINE_SIZE);
442         x86_push_imm (buf, method);
443         x86_jump_code (buf, tramp);
444         g_assert ((buf - code) <= TRAMPOLINE_SIZE);
445
446         /* store trampoline address */
447         method->info = code;
448
449         mono_jit_stats.method_trampolines++;
450
451         return code;
452 }
453
454 /**
455  * mono_arch_create_class_init_trampoline:
456  *  @vtable: the type to initialize
457  *
458  * Creates a trampoline function to run a type initializer. 
459  * If the trampoline is called, it calls mono_runtime_class_init with the
460  * given vtable, then patches the caller code so it does not get called any
461  * more.
462  * 
463  * Returns: a pointer to the newly created code 
464  */
465 gpointer
466 mono_arch_create_class_init_trampoline (MonoVTable *vtable)
467 {
468         guint8 *code, *buf, *tramp;
469
470         tramp = create_trampoline_code (MONO_TRAMPOLINE_CLASS_INIT);
471
472         code = buf = g_malloc (TRAMPOLINE_SIZE);
473         x86_push_imm (buf, vtable);
474         x86_jump_code (buf, tramp);
475         g_assert ((buf - code) <= TRAMPOLINE_SIZE);
476
477         mono_jit_stats.method_trampolines++;
478
479         return code;
480 }
481
482 /*
483  * This method is only called when running in the Mono Debugger.
484  */
485 gpointer
486 mono_debugger_create_notification_function (gpointer *notification_address)
487 {
488         guint8 *ptr, *buf;
489
490         ptr = buf = g_malloc0 (16);
491         x86_breakpoint (buf);
492         if (notification_address)
493                 *notification_address = buf;
494         x86_ret (buf);
495
496         return ptr;
497 }
498