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