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