2005-12-11 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/arch/x86/x86-codegen.h>
18 #include <mono/metadata/mono-debug-debugger.h>
19
20 #ifdef HAVE_VALGRIND_MEMCHECK_H
21 #include <valgrind/memcheck.h>
22 #endif
23
24 #include "mini.h"
25 #include "mini-x86.h"
26
27 static guint8* nullified_class_init_trampoline;
28
29 /*
30  * mono_arch_get_unbox_trampoline:
31  * @m: method pointer
32  * @addr: pointer to native code for @m
33  *
34  * when value type methods are called through the vtable we need to unbox the
35  * this argument. This method returns a pointer to a trampoline which does
36  * unboxing before calling the method
37  */
38 gpointer
39 mono_arch_get_unbox_trampoline (MonoMethod *m, gpointer addr)
40 {
41         guint8 *code, *start;
42         int this_pos = 4;
43         MonoDomain *domain = mono_domain_get ();
44
45         if (!mono_method_signature (m)->ret->byref && MONO_TYPE_ISSTRUCT (mono_method_signature (m)->ret))
46                 this_pos = 8;
47             
48         mono_domain_lock (domain);
49         start = code = mono_code_manager_reserve (domain->code_mp, 16);
50         mono_domain_unlock (domain);
51
52         x86_alu_membase_imm (code, X86_ADD, X86_ESP, this_pos, sizeof (MonoObject));
53         x86_jump_code (code, addr);
54         g_assert ((code - start) < 16);
55
56         return start;
57 }
58
59 void
60 mono_arch_patch_callsite (guint8 *code, guint8 *addr)
61 {
62         /* go to the start of the call instruction
63          *
64          * address_byte = (m << 6) | (o << 3) | reg
65          * call opcode: 0xff address_byte displacement
66          * 0xff m=1,o=2 imm8
67          * 0xff m=2,o=2 imm32
68          */
69         code -= 6;
70         if ((code [1] == 0xe8)) {
71                 if (!mono_running_on_valgrind ()) {
72                         InterlockedExchange ((gint32*)(code + 2), (guint)addr - ((guint)code + 1) - 5);
73
74 #ifdef HAVE_VALGRIND_MEMCHECK_H
75                                 /* Tell valgrind to recompile the patched code */
76                                 //VALGRIND_DISCARD_TRANSLATIONS (code + 2, code + 6);
77 #endif
78                 }
79         } else {
80                 printf ("Invalid trampoline sequence: %x %x %x %x %x %x %x\n", code [0], code [1], code [2], code [3],
81                                 code [4], code [5], code [6]);
82                 g_assert_not_reached ();
83         }
84 }
85
86 void
87 mono_arch_nullify_class_init_trampoline (guint8 *code, gssize *regs)
88 {
89         code -= 5;
90         if (code [0] == 0xe8) {
91                 if (!mono_running_on_valgrind ()) {
92                         guint32 ops;
93                         /*
94                          * Thread safe code patching using the algorithm from the paper
95                          * 'Practicing JUDO: Java Under Dynamic Optimizations'
96                          */
97                         /* 
98                          * First atomically change the the first 2 bytes of the call to a
99                          * spinning jump.
100                          */
101                         ops = 0xfeeb;
102                         InterlockedExchange ((gint32*)code, ops);
103
104                         /* Then change the other bytes to a nop */
105                         code [2] = 0x90;
106                         code [3] = 0x90;
107                         code [4] = 0x90;
108
109                         /* Then atomically change the first 4 bytes to a nop as well */
110                         ops = 0x90909090;
111                         InterlockedExchange ((gint32*)code, ops);
112 #ifdef HAVE_VALGRIND_MEMCHECK_H
113                         /* FIXME: the calltree skin trips on the self modifying code above */
114
115                         /* Tell valgrind to recompile the patched code */
116                         //VALGRIND_DISCARD_TRANSLATIONS (code, code + 8);
117 #endif
118                 }
119         } else if (code [0] == 0x90 || code [0] == 0xeb) {
120                 /* Already changed by another thread */
121                 ;
122         } else if ((code [-1] == 0xff) && (x86_modrm_reg (code [0]) == 0x2)) {
123                 /* call *<OFFSET>(<REG>) -> Call made from AOT code */
124                 gpointer *vtable_slot;
125
126                 vtable_slot = mono_arch_get_vcall_slot_addr (code + 5, (gpointer*)regs);
127                 g_assert (vtable_slot);
128
129                 *vtable_slot = nullified_class_init_trampoline;
130         } else {
131                         printf ("Invalid trampoline sequence: %x %x %x %x %x %x %x\n", code [0], code [1], code [2], code [3],
132                                 code [4], code [5], code [6]);
133                         g_assert_not_reached ();
134                 }
135 }
136
137 void
138 mono_arch_patch_delegate_trampoline (guint8 *code, guint8 *tramp, gssize *regs, guint8 *addr)
139 {
140         guint32 reg;
141         guint32 disp;
142
143         if ((code [-3] == 0xff) && (x86_modrm_reg (code [-2]) == 0x2) && (x86_modrm_mod (code [-2]) == 0x1)) {
144                 /* call *[reg+disp8] */
145                 reg = x86_modrm_rm (code [-2]);
146                 disp = *(guint8*)(code - 1);
147                 //printf ("B: [%%r%d+0x%x]\n", reg, disp);
148         }
149         else {
150                 int i;
151
152                 for (i = -16; i < 0; ++i)
153                         printf ("%d ", code [i]);
154                 printf ("\n");
155                 g_assert_not_reached ();
156         }
157
158         *(gpointer*)(((guint32)(regs [reg])) + disp) = addr;
159 }
160
161 guchar*
162 mono_arch_create_trampoline_code (MonoTrampolineType tramp_type)
163 {
164         guint8 *buf, *code;
165
166         code = buf = mono_global_codeman_reserve (256);
167
168         /* Put all registers into an array on the stack */
169         x86_push_reg (buf, X86_EDI);
170         x86_push_reg (buf, X86_ESI);
171         x86_push_reg (buf, X86_EBP);
172         x86_push_reg (buf, X86_ESP);
173         x86_push_reg (buf, X86_EBX);
174         x86_push_reg (buf, X86_EDX);
175         x86_push_reg (buf, X86_ECX);
176         x86_push_reg (buf, X86_EAX);
177
178         /* save LMF begin */
179
180         /* save the IP (caller ip) */
181         if (tramp_type == MONO_TRAMPOLINE_JUMP)
182                 x86_push_imm (buf, 0);
183         else
184                 x86_push_membase (buf, X86_ESP, 8 * 4 + 4);
185
186         x86_push_reg (buf, X86_EBP);
187         x86_push_reg (buf, X86_ESI);
188         x86_push_reg (buf, X86_EDI);
189         x86_push_reg (buf, X86_EBX);
190
191         /* save method info */
192         x86_push_membase (buf, X86_ESP, 13 * 4);
193         /* get the address of lmf for the current thread */
194         x86_call_code (buf, mono_get_lmf_addr);
195         /* push lmf */
196         x86_push_reg (buf, X86_EAX); 
197         /* push *lfm (previous_lmf) */
198         x86_push_membase (buf, X86_EAX, 0);
199         /* *(lmf) = ESP */
200         x86_mov_membase_reg (buf, X86_EAX, 0, X86_ESP, 4);
201         /* save LFM end */
202
203         /* FIXME: Push the trampoline address */
204         x86_push_imm (buf, 0);
205
206         /* push the method info */
207         x86_push_membase (buf, X86_ESP, 17 * 4);
208         /* push the return address onto the stack */
209         if (tramp_type == MONO_TRAMPOLINE_JUMP)
210                 x86_push_imm (buf, 0);
211         else
212                 x86_push_membase (buf, X86_ESP, 18 * 4 + 4);
213         /* push the address of the register array */
214         x86_lea_membase (buf, X86_EAX, X86_ESP, 11 * 4);
215         x86_push_reg (buf, X86_EAX);
216
217         if (tramp_type == MONO_TRAMPOLINE_CLASS_INIT)
218                 x86_call_code (buf, mono_class_init_trampoline);
219         else if (tramp_type == MONO_TRAMPOLINE_AOT)
220                 x86_call_code (buf, mono_aot_trampoline);
221         else if (tramp_type == MONO_TRAMPOLINE_DELEGATE)
222                 x86_call_code (buf, mono_delegate_trampoline);
223         else
224                 x86_call_code (buf, mono_magic_trampoline);
225         x86_alu_reg_imm (buf, X86_ADD, X86_ESP, 4*4);
226
227         /* restore LMF start */
228         /* ebx = previous_lmf */
229         x86_pop_reg (buf, X86_EBX);
230         /* edi = lmf */
231         x86_pop_reg (buf, X86_EDI);
232         /* *(lmf) = previous_lmf */
233         x86_mov_membase_reg (buf, X86_EDI, 0, X86_EBX, 4);
234         /* discard method info */
235         x86_pop_reg (buf, X86_ESI);
236         /* restore caller saved regs */
237         x86_pop_reg (buf, X86_EBX);
238         x86_pop_reg (buf, X86_EDI);
239         x86_pop_reg (buf, X86_ESI);
240         x86_pop_reg (buf, X86_EBP);
241
242         /* discard save IP */
243         x86_alu_reg_imm (buf, X86_ADD, X86_ESP, 4);             
244         /* restore LMF end */
245
246         /* Restore caller saved registers */
247         x86_mov_reg_membase (buf, X86_ECX, X86_ESP, 1 * 4, 4);
248         x86_mov_reg_membase (buf, X86_EDX, X86_ESP, 2 * 4, 4);
249
250         /* Pop saved reg array + method ptr */
251         x86_alu_reg_imm (buf, X86_ADD, X86_ESP, 9 * 4);
252
253         if (tramp_type == MONO_TRAMPOLINE_CLASS_INIT)
254                 x86_ret (buf);
255         else
256                 /* call the compiled method */
257                 x86_jump_reg (buf, X86_EAX);
258
259         g_assert ((buf - code) <= 256);
260
261         if (tramp_type == MONO_TRAMPOLINE_CLASS_INIT) {
262                 /* Initialize the nullified class init trampoline used in the AOT case */
263                 nullified_class_init_trampoline = buf = mono_global_codeman_reserve (16);
264                 x86_ret (buf);
265         }
266
267         return code;
268 }
269
270 #define TRAMPOLINE_SIZE 10
271
272 gpointer
273 mono_arch_create_specific_trampoline (gpointer arg1, MonoTrampolineType tramp_type, MonoDomain *domain, guint32 *code_len)
274 {
275         guint8 *code, *buf, *tramp;
276         
277         tramp = mono_get_trampoline_code (tramp_type);
278
279         mono_domain_lock (domain);
280         code = buf = mono_code_manager_reserve (domain->code_mp, TRAMPOLINE_SIZE);
281         mono_domain_unlock (domain);
282
283         x86_push_imm (buf, arg1);
284         x86_jump_code (buf, tramp);
285         g_assert ((buf - code) <= TRAMPOLINE_SIZE);
286
287         mono_arch_flush_icache (code, buf - code);
288
289         mono_jit_stats.method_trampolines++;
290
291         if (code_len)
292                 *code_len = buf - code;
293
294         return code;
295 }
296
297 void
298 mono_arch_invalidate_method (MonoJitInfo *ji, void *func, gpointer func_arg)
299 {
300         /* FIXME: This is not thread safe */
301         guint8 *code = ji->code_start;
302
303         x86_push_imm (code, func_arg);
304         x86_call_code (code, (guint8*)func);
305 }
306
307 /*
308  * This method is only called when running in the Mono Debugger.
309  */
310 gpointer
311 mono_debugger_create_notification_function (gpointer *notification_address)
312 {
313         guint8 *ptr, *buf;
314
315         ptr = buf = mono_global_codeman_reserve (16);
316
317         x86_breakpoint (buf);
318         if (notification_address)
319                 *notification_address = buf;
320         x86_ret (buf);
321
322         return ptr;
323 }