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