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