2008-01-24 Zoltan Varga <vargaz@gmail.com>
[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/metadata-internals.h>
15 #include <mono/metadata/marshal.h>
16 #include <mono/metadata/tabledefs.h>
17 #include <mono/metadata/mono-debug.h>
18 #include <mono/metadata/mono-debug-debugger.h>
19 #include <mono/arch/x86/x86-codegen.h>
20
21 #ifdef HAVE_VALGRIND_MEMCHECK_H
22 #include <valgrind/memcheck.h>
23 #endif
24
25 #include "mini.h"
26 #include "mini-x86.h"
27
28 static guint8* nullified_class_init_trampoline;
29
30 /*
31  * mono_arch_get_unbox_trampoline:
32  * @m: method pointer
33  * @addr: pointer to native code for @m
34  *
35  * when value type methods are called through the vtable we need to unbox the
36  * this argument. This method returns a pointer to a trampoline which does
37  * unboxing before calling the method
38  */
39 gpointer
40 mono_arch_get_unbox_trampoline (MonoMethod *m, gpointer addr)
41 {
42         guint8 *code, *start;
43         int this_pos = 4;
44         MonoDomain *domain = mono_domain_get ();
45
46         if (!mono_method_signature (m)->ret->byref && MONO_TYPE_ISSTRUCT (mono_method_signature (m)->ret))
47                 this_pos = 8;
48             
49         mono_domain_lock (domain);
50         start = code = mono_code_manager_reserve (domain->code_mp, 16);
51         mono_domain_unlock (domain);
52
53         x86_alu_membase_imm (code, X86_ADD, X86_ESP, this_pos, sizeof (MonoObject));
54         x86_jump_code (code, addr);
55         g_assert ((code - start) < 16);
56
57         return start;
58 }
59
60 void
61 mono_arch_patch_callsite (guint8 *orig_code, guint8 *addr)
62 {
63         guint8 *code;
64         guint8 buf [8];
65         gboolean can_write = mono_breakpoint_clean_code (orig_code - 8, buf, sizeof (buf));
66
67         code = buf + 8;
68         if (mono_running_on_valgrind ())
69                 can_write = FALSE;
70
71         /* go to the start of the call instruction
72          *
73          * address_byte = (m << 6) | (o << 3) | reg
74          * call opcode: 0xff address_byte displacement
75          * 0xff m=1,o=2 imm8
76          * 0xff m=2,o=2 imm32
77          */
78         code -= 6;
79         orig_code -= 6;
80         if ((code [1] == 0xe8)) {
81                 if (can_write) {
82                         InterlockedExchange ((gint32*)(orig_code + 2), (guint)addr - ((guint)orig_code + 1) - 5);
83
84 #ifdef HAVE_VALGRIND_MEMCHECK_H
85                                 /* Tell valgrind to recompile the patched code */
86                                 //VALGRIND_DISCARD_TRANSLATIONS (code + 2, code + 6);
87 #endif
88                 }
89         } else if (code [1] == 0xe9) {
90                 /* A PLT entry: jmp <DISP> */
91                 if (can_write)
92                         InterlockedExchange ((gint32*)(orig_code + 2), (guint)addr - ((guint)orig_code + 1) - 5);
93         } else {
94                 printf ("Invalid trampoline sequence: %x %x %x %x %x %x %x\n", code [0], code [1], code [2], code [3],
95                                 code [4], code [5], code [6]);
96                 g_assert_not_reached ();
97         }
98 }
99
100 void
101 mono_arch_patch_plt_entry (guint8 *code, guint8 *addr)
102 {
103         /* A PLT entry: jmp <DISP> */
104         g_assert (code [0] == 0xe9);
105
106         if (!mono_running_on_valgrind ())
107                 InterlockedExchange ((gint32*)(code + 1), (guint)addr - (guint)code - 5);
108 }
109
110 void
111 mono_arch_nullify_class_init_trampoline (guint8 *code, gssize *regs)
112 {
113         code -= 5;
114         if (code [0] == 0xe8) {
115                 if (!mono_running_on_valgrind ()) {
116                         guint32 ops;
117                         /*
118                          * Thread safe code patching using the algorithm from the paper
119                          * 'Practicing JUDO: Java Under Dynamic Optimizations'
120                          */
121                         /* 
122                          * First atomically change the the first 2 bytes of the call to a
123                          * spinning jump.
124                          */
125                         ops = 0xfeeb;
126                         InterlockedExchange ((gint32*)code, ops);
127
128                         /* Then change the other bytes to a nop */
129                         code [2] = 0x90;
130                         code [3] = 0x90;
131                         code [4] = 0x90;
132
133                         /* Then atomically change the first 4 bytes to a nop as well */
134                         ops = 0x90909090;
135                         InterlockedExchange ((gint32*)code, ops);
136 #ifdef HAVE_VALGRIND_MEMCHECK_H
137                         /* FIXME: the calltree skin trips on the self modifying code above */
138
139                         /* Tell valgrind to recompile the patched code */
140                         //VALGRIND_DISCARD_TRANSLATIONS (code, code + 8);
141 #endif
142                 }
143         } else if (code [0] == 0x90 || code [0] == 0xeb) {
144                 /* Already changed by another thread */
145                 ;
146         } else if ((code [-1] == 0xff) && (x86_modrm_reg (code [0]) == 0x2)) {
147                 /* call *<OFFSET>(<REG>) -> Call made from AOT code */
148                 gpointer *vtable_slot;
149
150                 vtable_slot = mono_arch_get_vcall_slot_addr (code + 5, (gpointer*)regs);
151                 g_assert (vtable_slot);
152
153                 *vtable_slot = nullified_class_init_trampoline;
154         } else {
155                         printf ("Invalid trampoline sequence: %x %x %x %x %x %x %x\n", code [0], code [1], code [2], code [3],
156                                 code [4], code [5], code [6]);
157                         g_assert_not_reached ();
158                 }
159 }
160
161 void
162 mono_arch_nullify_plt_entry (guint8 *code)
163 {
164         if (!mono_running_on_valgrind ()) {
165                 guint32 ops;
166
167                 ops = 0xfeeb;
168                 InterlockedExchange ((gint32*)code, ops);
169
170                 /* Then change the other bytes to a nop */
171                 code [2] = 0x90;
172                 code [3] = 0x90;
173                 code [4] = 0x90;
174
175                 /* Change the first byte to a nop */
176                 ops = 0xc3;
177                 InterlockedExchange ((gint32*)code, ops);
178         }
179 }
180
181 guchar*
182 mono_arch_create_trampoline_code (MonoTrampolineType tramp_type)
183 {
184         guint8 *buf, *code, *tramp;
185         int pushed_args;
186
187         code = buf = mono_global_codeman_reserve (256);
188
189         /* Note that there is a single argument to the trampoline
190          * and it is stored at: esp + pushed_args * sizeof (gpointer)
191          * the ret address is at: esp + (pushed_args + 1) * sizeof (gpointer)
192          */
193
194         /* If this is a generic class init the argument is not on the
195          * stack yet but in MONO_ARCH_VTABLE_REG.
196          */
197         if (tramp_type == MONO_TRAMPOLINE_GENERIC_CLASS_INIT)
198                 x86_push_reg (buf, MONO_ARCH_VTABLE_REG);
199
200         /* Put all registers into an array on the stack
201          * If this code is changed, make sure to update the offset value in
202          * mono_arch_find_this_argument () in mini-x86.c.
203          */
204         x86_push_reg (buf, X86_EDI);
205         x86_push_reg (buf, X86_ESI);
206         x86_push_reg (buf, X86_EBP);
207         x86_push_reg (buf, X86_ESP);
208         x86_push_reg (buf, X86_EBX);
209         x86_push_reg (buf, X86_EDX);
210         x86_push_reg (buf, X86_ECX);
211         x86_push_reg (buf, X86_EAX);
212
213         pushed_args = 8;
214
215         /* Align stack on apple */
216         x86_alu_reg_imm (buf, X86_SUB, X86_ESP, 4);
217
218         pushed_args ++;
219
220         /* save LMF begin */
221
222         /* save the IP (caller ip) */
223         if (tramp_type == MONO_TRAMPOLINE_JUMP)
224                 x86_push_imm (buf, 0);
225         else
226                 x86_push_membase (buf, X86_ESP, (pushed_args + 1) * sizeof (gpointer));
227
228         pushed_args++;
229
230         x86_push_reg (buf, X86_EBP);
231         x86_push_reg (buf, X86_ESI);
232         x86_push_reg (buf, X86_EDI);
233         x86_push_reg (buf, X86_EBX);
234
235         pushed_args += 4;
236
237         /* save ESP */
238         x86_push_reg (buf, X86_ESP);
239         /* Adjust ESP so it points to the previous frame */
240         x86_alu_membase_imm (buf, X86_ADD, X86_ESP, 0, (pushed_args + 2) * 4);
241
242         pushed_args ++;
243
244         /* save method info */
245         if ((tramp_type == MONO_TRAMPOLINE_GENERIC) || (tramp_type == MONO_TRAMPOLINE_JUMP))
246                 x86_push_membase (buf, X86_ESP, pushed_args * sizeof (gpointer));
247         else
248                 x86_push_imm (buf, 0);
249
250         pushed_args++;
251
252         /* On apple, the stack is correctly aligned to 16 bytes because pushed_args is
253          * 16 and there is the extra trampoline arg + the return ip pushed by call
254          * FIXME: Note that if an exception happens while some args are pushed
255          * on the stack, the stack will be misaligned.
256          */
257         g_assert (pushed_args == 16);
258
259         /* get the address of lmf for the current thread */
260         x86_call_code (buf, mono_get_lmf_addr);
261         /* push lmf */
262         x86_push_reg (buf, X86_EAX); 
263         /* push *lfm (previous_lmf) */
264         x86_push_membase (buf, X86_EAX, 0);
265         /* Signal to mono_arch_find_jit_info () that this is a trampoline frame */
266         x86_alu_membase_imm (buf, X86_ADD, X86_ESP, 0, 1);
267         /* *(lmf) = ESP */
268         x86_mov_membase_reg (buf, X86_EAX, 0, X86_ESP, 4);
269         /* save LFM end */
270
271         pushed_args += 2;
272
273         /* starting the call sequence */
274
275         /* FIXME: Push the trampoline address */
276         x86_push_imm (buf, 0);
277
278         pushed_args++;
279
280         /* push the method info */
281         x86_push_membase (buf, X86_ESP, pushed_args * sizeof (gpointer));
282
283         pushed_args++;
284
285         /* push the return address onto the stack */
286         if (tramp_type == MONO_TRAMPOLINE_JUMP)
287                 x86_push_imm (buf, 0);
288         else
289                 x86_push_membase (buf, X86_ESP, (pushed_args + 1) * sizeof (gpointer));
290         pushed_args++;
291         /* push the address of the register array */
292         x86_lea_membase (buf, X86_EAX, X86_ESP, (pushed_args - 8) * sizeof (gpointer));
293         x86_push_reg (buf, X86_EAX);
294
295         pushed_args++;
296
297 #ifdef __APPLE__
298         /* check the stack is aligned after the ret ip is pushed */
299         /*x86_mov_reg_reg (buf, X86_EDX, X86_ESP, 4);
300         x86_alu_reg_imm (buf, X86_AND, X86_EDX, 15);
301         x86_alu_reg_imm (buf, X86_CMP, X86_EDX, 0);
302         x86_branch_disp (buf, X86_CC_Z, 3, FALSE);
303         x86_breakpoint (buf);*/
304 #endif
305
306         tramp = (guint8*)mono_get_trampoline_func (tramp_type);
307         x86_call_code (buf, tramp);
308
309         x86_alu_reg_imm (buf, X86_ADD, X86_ESP, 4*4);
310
311         /* Check for thread interruption */
312         /* This is not perf critical code so no need to check the interrupt flag */
313         x86_push_reg (buf, X86_EAX);
314         x86_call_code (buf, (guint8*)mono_thread_interruption_checkpoint);
315         x86_pop_reg (buf, X86_EAX);
316
317         /* Restore LMF */
318
319         /* ebx = previous_lmf */
320         x86_pop_reg (buf, X86_EBX);
321         x86_alu_reg_imm (buf, X86_SUB, X86_EBX, 1);
322         /* edi = lmf */
323         x86_pop_reg (buf, X86_EDI);
324         /* *(lmf) = previous_lmf */
325         x86_mov_membase_reg (buf, X86_EDI, 0, X86_EBX, 4);
326         /* discard method info */
327         x86_pop_reg (buf, X86_ESI);
328         /* discard ESP */
329         x86_pop_reg (buf, X86_ESI);
330         /* restore caller saved regs */
331         x86_pop_reg (buf, X86_EBX);
332         x86_pop_reg (buf, X86_EDI);
333         x86_pop_reg (buf, X86_ESI);
334         x86_pop_reg (buf, X86_EBP);
335
336         /* discard save IP */
337         x86_alu_reg_imm (buf, X86_ADD, X86_ESP, 4);             
338         /* restore LMF end */
339
340         /* Restore caller saved registers */
341         x86_mov_reg_membase (buf, X86_ECX, X86_ESP, 1 * 4, 4);
342         x86_mov_reg_membase (buf, X86_EDX, X86_ESP, 2 * 4, 4);
343
344         /* Pop saved reg array + stack align + method ptr */
345         x86_alu_reg_imm (buf, X86_ADD, X86_ESP, 10 * 4);
346
347         if (tramp_type == MONO_TRAMPOLINE_CLASS_INIT || tramp_type == MONO_TRAMPOLINE_GENERIC_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         if (tramp_type == MONO_TRAMPOLINE_CLASS_INIT) {
356                 /* Initialize the nullified class init trampoline used in the AOT case */
357                 nullified_class_init_trampoline = buf = mono_global_codeman_reserve (16);
358                 x86_ret (buf);
359         }
360
361         return code;
362 }
363
364 #define TRAMPOLINE_SIZE 10
365
366 gpointer
367 mono_arch_create_specific_trampoline (gpointer arg1, MonoTrampolineType tramp_type, MonoDomain *domain, guint32 *code_len)
368 {
369         guint8 *code, *buf, *tramp;
370         
371         tramp = mono_get_trampoline_code (tramp_type);
372
373         mono_domain_lock (domain);
374         code = buf = mono_code_manager_reserve_align (domain->code_mp, TRAMPOLINE_SIZE, 4);
375         mono_domain_unlock (domain);
376
377         x86_push_imm (buf, arg1);
378         x86_jump_code (buf, tramp);
379         g_assert ((buf - code) <= TRAMPOLINE_SIZE);
380
381         mono_arch_flush_icache (code, buf - code);
382
383         if (code_len)
384                 *code_len = buf - code;
385
386         return code;
387 }
388
389 void
390 mono_arch_invalidate_method (MonoJitInfo *ji, void *func, gpointer func_arg)
391 {
392         /* FIXME: This is not thread safe */
393         guint8 *code = ji->code_start;
394
395         x86_push_imm (code, func_arg);
396         x86_call_code (code, (guint8*)func);
397 }
398
399 /*
400  * This method is only called when running in the Mono Debugger.
401  */
402 gpointer
403 mono_debugger_create_notification_function (void)
404 {
405         guint8 *buf, *code;
406
407         code = buf = mono_global_codeman_reserve (2);
408         x86_breakpoint (buf);
409         x86_ret (buf);
410         return code;
411 }