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