2010-04-27 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/metadata/monitor.h>
20 #include <mono/arch/x86/x86-codegen.h>
21
22 #include <mono/utils/memcheck.h>
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  * @gsctx: the generic sharing context
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 (MonoGenericSharingContext *gsctx, MonoMethod *m, gpointer addr)
41 {
42         guint8 *code, *start;
43         int this_pos = 4;
44         MonoDomain *domain = mono_domain_get ();
45
46         if (MONO_TYPE_ISSTRUCT (mono_method_signature (m)->ret))
47                 this_pos = 8;
48             
49         start = code = mono_domain_code_reserve (domain, 16);
50
51         x86_alu_membase_imm (code, X86_ADD, X86_ESP, this_pos, sizeof (MonoObject));
52         x86_jump_code (code, addr);
53         g_assert ((code - start) < 16);
54
55         return start;
56 }
57
58 gpointer
59 mono_arch_get_static_rgctx_trampoline (MonoMethod *m, MonoMethodRuntimeGenericContext *mrgctx, gpointer addr)
60 {
61         guint8 *code, *start;
62         int buf_len;
63
64         MonoDomain *domain = mono_domain_get ();
65
66         buf_len = 10;
67
68         start = code = mono_domain_code_reserve (domain, buf_len);
69
70         x86_mov_reg_imm (code, MONO_ARCH_RGCTX_REG, mrgctx);
71         x86_jump_code (code, addr);
72         g_assert ((code - start) <= buf_len);
73
74         mono_arch_flush_icache (start, code - start);
75
76         return start;
77 }
78
79 gpointer
80 mono_arch_get_llvm_imt_trampoline (MonoDomain *domain, MonoMethod *m, int vt_offset)
81 {
82         guint8 *code, *start;
83         int buf_len;
84         int this_offset;
85
86         buf_len = 32;
87
88         start = code = mono_domain_code_reserve (domain, buf_len);
89
90         this_offset = mono_x86_get_this_arg_offset (NULL, mono_method_signature (m));
91
92         /* Set imt arg */
93         x86_mov_reg_imm (code, MONO_ARCH_IMT_REG, m);
94         /* Load this */
95         x86_mov_reg_membase (code, X86_EAX, X86_ESP, this_offset + 4, 4);
96         /* Load vtable address */
97         x86_mov_reg_membase (code, X86_EAX, X86_EAX, 0, 4);
98         x86_jump_membase (code, X86_EAX, vt_offset);
99
100         g_assert ((code - start) < buf_len);
101
102         mono_arch_flush_icache (start, code - start);
103
104         return start;
105 }
106
107 void
108 mono_arch_patch_callsite (guint8 *method_start, guint8 *orig_code, guint8 *addr)
109 {
110         guint8 *code;
111         guint8 buf [8];
112         gboolean can_write = mono_breakpoint_clean_code (method_start, orig_code, 8, buf, sizeof (buf));
113
114         code = buf + 8;
115
116         /* go to the start of the call instruction
117          *
118          * address_byte = (m << 6) | (o << 3) | reg
119          * call opcode: 0xff address_byte displacement
120          * 0xff m=1,o=2 imm8
121          * 0xff m=2,o=2 imm32
122          */
123         code -= 6;
124         orig_code -= 6;
125         if ((code [1] == 0xe8)) {
126                 if (can_write) {
127                         InterlockedExchange ((gint32*)(orig_code + 2), (guint)addr - ((guint)orig_code + 1) - 5);
128
129                         /* Tell valgrind to recompile the patched code */
130                         VALGRIND_DISCARD_TRANSLATIONS (orig_code + 2, 4);
131                 }
132         } else if (code [1] == 0xe9) {
133                 /* A PLT entry: jmp <DISP> */
134                 if (can_write)
135                         InterlockedExchange ((gint32*)(orig_code + 2), (guint)addr - ((guint)orig_code + 1) - 5);
136         } else {
137                 printf ("Invalid trampoline sequence: %x %x %x %x %x %x %x\n", code [0], code [1], code [2], code [3],
138                                 code [4], code [5], code [6]);
139                 g_assert_not_reached ();
140         }
141 }
142
143 void
144 mono_arch_patch_plt_entry (guint8 *code, gpointer *got, mgreg_t *regs, guint8 *addr)
145 {
146         guint32 offset;
147
148         /* Patch the jump table entry used by the plt entry */
149
150         /* A PLT entry: jmp *<DISP>(%ebx) */
151         g_assert (code [0] == 0xff);
152         g_assert (code [1] == 0xa3);
153
154         offset = *(guint32*)(code + 2);
155
156         if (!got)
157                 got = (gpointer*)(gsize) regs [MONO_ARCH_GOT_REG];
158         *(guint8**)((guint8*)got + offset) = addr;
159 }
160
161 void
162 mono_arch_nullify_class_init_trampoline (guint8 *code, mgreg_t *regs)
163 {
164         guint8 buf [16];
165         gboolean can_write = mono_breakpoint_clean_code (NULL, code, 6, buf, sizeof (buf));
166
167         if (!can_write)
168                 return;
169
170         code -= 5;
171         if (code [0] == 0xe8) {
172                 if (!mono_running_on_valgrind ()) {
173                         guint32 ops;
174                         /*
175                          * Thread safe code patching using the algorithm from the paper
176                          * 'Practicing JUDO: Java Under Dynamic Optimizations'
177                          */
178                         /* 
179                          * First atomically change the the first 2 bytes of the call to a
180                          * spinning jump.
181                          */
182                         ops = 0xfeeb;
183                         InterlockedExchange ((gint32*)code, ops);
184
185                         /* Then change the other bytes to a nop */
186                         code [2] = 0x90;
187                         code [3] = 0x90;
188                         code [4] = 0x90;
189
190                         /* Then atomically change the first 4 bytes to a nop as well */
191                         ops = 0x90909090;
192                         InterlockedExchange ((gint32*)code, ops);
193                         /* FIXME: the calltree skin trips on the self modifying code above */
194
195                         /* Tell valgrind to recompile the patched code */
196                         //VALGRIND_DISCARD_TRANSLATIONS (code, 8);
197                 }
198         } else if (code [0] == 0x90 || code [0] == 0xeb) {
199                 /* Already changed by another thread */
200                 ;
201         } else if ((code [-1] == 0xff) && (x86_modrm_reg (code [0]) == 0x2)) {
202                 /* call *<OFFSET>(<REG>) -> Call made from AOT code */
203                 gpointer *vtable_slot;
204
205                 vtable_slot = mono_get_vcall_slot_addr (code + 5, regs);
206                 g_assert (vtable_slot);
207
208                 *vtable_slot = nullified_class_init_trampoline;
209         } else {
210                         printf ("Invalid trampoline sequence: %x %x %x %x %x %x %x\n", code [0], code [1], code [2], code [3],
211                                 code [4], code [5], code [6]);
212                         g_assert_not_reached ();
213                 }
214 }
215
216 void
217 mono_arch_nullify_plt_entry (guint8 *code, mgreg_t *regs)
218 {
219         if (mono_aot_only && !nullified_class_init_trampoline)
220                 nullified_class_init_trampoline = mono_aot_get_named_code ("nullified_class_init_trampoline");
221
222         mono_arch_patch_plt_entry (code, NULL, regs, nullified_class_init_trampoline);
223 }
224
225 guchar*
226 mono_arch_create_trampoline_code (MonoTrampolineType tramp_type)
227 {
228         MonoJumpInfo *ji;
229         guint32 code_size;
230         guchar *code;
231         GSList *unwind_ops, *l;
232
233         code = mono_arch_create_trampoline_code_full (tramp_type, &code_size, &ji, &unwind_ops, FALSE);
234
235         mono_save_trampoline_xdebug_info ("<generic_trampoline>", code, code_size, unwind_ops);
236
237         for (l = unwind_ops; l; l = l->next)
238                 g_free (l->data);
239         g_slist_free (unwind_ops);
240
241         return code;
242 }
243
244 guchar*
245 mono_arch_create_trampoline_code_full (MonoTrampolineType tramp_type, guint32 *code_size, MonoJumpInfo **ji, GSList **out_unwind_ops, gboolean aot)
246 {
247         guint8 *buf, *code, *tramp;
248         int pushed_args, pushed_args_caller_saved;
249         GSList *unwind_ops = NULL;
250
251         code = buf = mono_global_codeman_reserve (256);
252
253         *ji = NULL;
254
255         /* Note that there is a single argument to the trampoline
256          * and it is stored at: esp + pushed_args * sizeof (gpointer)
257          * the ret address is at: esp + (pushed_args + 1) * sizeof (gpointer)
258          */
259
260         /* Put all registers into an array on the stack
261          * If this code is changed, make sure to update the offset value in
262          * mono_arch_get_this_arg_from_call () in mini-x86.c.
263          */
264         x86_push_reg (code, X86_EDI);
265         x86_push_reg (code, X86_ESI);
266         x86_push_reg (code, X86_EBP);
267         x86_push_reg (code, X86_ESP);
268         x86_push_reg (code, X86_EBX);
269         x86_push_reg (code, X86_EDX);
270         x86_push_reg (code, X86_ECX);
271         x86_push_reg (code, X86_EAX);
272
273         pushed_args_caller_saved = pushed_args = 8;
274
275         /* Align stack on apple */
276         x86_alu_reg_imm (code, X86_SUB, X86_ESP, 4);
277
278         pushed_args ++;
279
280         /* save LMF begin */
281
282         /* save the IP (caller ip) */
283         if (tramp_type == MONO_TRAMPOLINE_JUMP)
284                 x86_push_imm (code, 0);
285         else
286                 x86_push_membase (code, X86_ESP, (pushed_args + 1) * sizeof (gpointer));
287
288         pushed_args++;
289
290         x86_push_reg (code, X86_EBP);
291         x86_push_reg (code, X86_ESI);
292         x86_push_reg (code, X86_EDI);
293         x86_push_reg (code, X86_EBX);
294
295         pushed_args += 4;
296
297         /* save ESP */
298         x86_push_reg (code, X86_ESP);
299         /* Adjust ESP so it points to the previous frame */
300         x86_alu_membase_imm (code, X86_ADD, X86_ESP, 0, (pushed_args + 2) * 4);
301
302         pushed_args ++;
303
304         /* save method info */
305         if ((tramp_type == MONO_TRAMPOLINE_JIT) || (tramp_type == MONO_TRAMPOLINE_JUMP))
306                 x86_push_membase (code, X86_ESP, pushed_args * sizeof (gpointer));
307         else
308                 x86_push_imm (code, 0);
309
310         pushed_args++;
311
312         /* On apple, the stack is correctly aligned to 16 bytes because pushed_args is
313          * 16 and there is the extra trampoline arg + the return ip pushed by call
314          * FIXME: Note that if an exception happens while some args are pushed
315          * on the stack, the stack will be misaligned.
316          */
317         g_assert (pushed_args == 16);
318
319         /* get the address of lmf for the current thread */
320         if (aot) {
321                 code = mono_arch_emit_load_aotconst (buf, code, ji, MONO_PATCH_INFO_JIT_ICALL_ADDR, "mono_get_lmf_addr");
322                 x86_call_reg (code, X86_EAX);
323         } else {
324                 x86_call_code (code, mono_get_lmf_addr);
325         }
326         /* push lmf */
327         x86_push_reg (code, X86_EAX); 
328         /* push *lfm (previous_lmf) */
329         x86_push_membase (code, X86_EAX, 0);
330         /* Signal to mono_arch_find_jit_info () that this is a trampoline frame */
331         x86_alu_membase_imm (code, X86_ADD, X86_ESP, 0, 1);
332         /* *(lmf) = ESP */
333         x86_mov_membase_reg (code, X86_EAX, 0, X86_ESP, 4);
334         /* save LFM end */
335
336         pushed_args += 2;
337
338         /* starting the call sequence */
339
340         /* FIXME: Push the trampoline address */
341         x86_push_imm (code, 0);
342
343         pushed_args++;
344
345         /* push the method info */
346         x86_push_membase (code, X86_ESP, pushed_args * sizeof (gpointer));
347
348         pushed_args++;
349
350         /* push the return address onto the stack */
351         if (tramp_type == MONO_TRAMPOLINE_JUMP)
352                 x86_push_imm (code, 0);
353         else
354                 x86_push_membase (code, X86_ESP, (pushed_args + 1) * sizeof (gpointer));
355         pushed_args++;
356         /* push the address of the register array */
357         x86_lea_membase (code, X86_EAX, X86_ESP, (pushed_args - 8) * sizeof (gpointer));
358         x86_push_reg (code, X86_EAX);
359
360         pushed_args++;
361
362 #ifdef __APPLE__
363         /* check the stack is aligned after the ret ip is pushed */
364         /*x86_mov_reg_reg (buf, X86_EDX, X86_ESP, 4);
365         x86_alu_reg_imm (buf, X86_AND, X86_EDX, 15);
366         x86_alu_reg_imm (buf, X86_CMP, X86_EDX, 0);
367         x86_branch_disp (buf, X86_CC_Z, 3, FALSE);
368         x86_breakpoint (buf);*/
369 #endif
370
371         if (aot) {
372                 char *icall_name = g_strdup_printf ("trampoline_func_%d", tramp_type);
373                 code = mono_arch_emit_load_aotconst (buf, code, ji, MONO_PATCH_INFO_JIT_ICALL_ADDR, icall_name);
374                 x86_call_reg (code, X86_EAX);
375         } else {
376                 tramp = (guint8*)mono_get_trampoline_func (tramp_type);
377                 x86_call_code (code, tramp);
378         }
379
380         x86_alu_reg_imm (code, X86_ADD, X86_ESP, 4*4);
381
382         pushed_args -= 4;
383
384         /* Check for thread interruption */
385         /* This is not perf critical code so no need to check the interrupt flag */
386         /* Align the stack on osx */
387         x86_alu_reg_imm (code, X86_SUB, X86_ESP, 3 * 4);
388         x86_push_reg (code, X86_EAX);
389         if (aot) {
390                 code = mono_arch_emit_load_aotconst (buf, code, ji, MONO_PATCH_INFO_JIT_ICALL_ADDR, "mono_thread_force_interruption_checkpoint");
391                 x86_call_reg (code, X86_EAX);
392         } else {
393                 x86_call_code (code, (guint8*)mono_thread_force_interruption_checkpoint);
394         }
395         x86_pop_reg (code, X86_EAX);
396         x86_alu_reg_imm (code, X86_ADD, X86_ESP, 3 * 4);
397
398         /* Restore LMF */
399
400         /* ebx = previous_lmf */
401         x86_pop_reg (code, X86_EBX);
402         pushed_args--;
403         x86_alu_reg_imm (code, X86_SUB, X86_EBX, 1);
404
405         /* edi = lmf */
406         x86_pop_reg (code, X86_EDI);
407         pushed_args--;
408
409         /* *(lmf) = previous_lmf */
410         x86_mov_membase_reg (code, X86_EDI, 0, X86_EBX, 4);
411
412         /* discard method info */
413         x86_pop_reg (code, X86_ESI);
414         pushed_args--;
415
416         /* discard ESP */
417         x86_pop_reg (code, X86_ESI);
418         pushed_args--;
419
420         /* restore caller saved regs */
421         x86_pop_reg (code, X86_EBX);
422         x86_pop_reg (code, X86_EDI);
423         x86_pop_reg (code, X86_ESI);
424         x86_pop_reg (code, X86_EBP);
425
426         pushed_args -= 4;
427
428         /* discard save IP */
429         x86_alu_reg_imm (code, X86_ADD, X86_ESP, 4);
430         pushed_args--;
431
432         /* restore LMF end */
433
434         if (!MONO_TRAMPOLINE_TYPE_MUST_RETURN (tramp_type)) {
435                 /* 
436                  * Overwrite the method ptr with the address we need to jump to,
437                  * to free %eax.
438                  */
439                 x86_mov_membase_reg (code, X86_ESP, pushed_args * sizeof (gpointer), X86_EAX, 4);
440         }
441
442         /* Restore caller saved registers */
443         x86_mov_reg_membase (code, X86_ECX, X86_ESP, (pushed_args - pushed_args_caller_saved + X86_ECX) * 4, 4);
444         x86_mov_reg_membase (code, X86_EDX, X86_ESP, (pushed_args - pushed_args_caller_saved + X86_EDX) * 4, 4);
445         if ((tramp_type == MONO_TRAMPOLINE_RESTORE_STACK_PROT) || (tramp_type == MONO_TRAMPOLINE_AOT_PLT))
446                 x86_mov_reg_membase (code, X86_EAX, X86_ESP, (pushed_args - pushed_args_caller_saved + X86_EAX) * 4, 4);
447
448         if (!MONO_TRAMPOLINE_TYPE_MUST_RETURN (tramp_type)) {
449                 /* Pop saved reg array + stack align */
450                 x86_alu_reg_imm (code, X86_ADD, X86_ESP, 9 * 4);
451                 pushed_args -= 9;
452                 g_assert (pushed_args == 0);
453         } else {
454                 /* Pop saved reg array + stack align + method ptr */
455                 x86_alu_reg_imm (code, X86_ADD, X86_ESP, 10 * 4);
456                 pushed_args -= 10;
457
458                 /* We've popped one more stack item than we've pushed (the
459                    method ptr argument), so we must end up at -1. */
460                 g_assert (pushed_args == -1);
461         }
462
463         x86_ret (code);
464
465         g_assert ((code - buf) <= 256);
466
467         *code_size = code - buf;
468
469         if (tramp_type == MONO_TRAMPOLINE_CLASS_INIT) {
470                 /* Initialize the nullified class init trampoline used in the AOT case */
471                 nullified_class_init_trampoline = code = mono_global_codeman_reserve (16);
472                 x86_ret (code);
473         }
474
475         *out_unwind_ops = unwind_ops;
476
477         return buf;
478 }
479
480 gpointer
481 mono_arch_get_nullified_class_init_trampoline (guint32 *code_len)
482 {
483         guint8 *code, *buf;
484
485         code = buf = mono_global_codeman_reserve (16);
486         x86_ret (code);
487
488         mono_arch_flush_icache (buf, code - buf);
489
490         *code_len = code - buf;
491
492         return buf;
493 }
494
495 #define TRAMPOLINE_SIZE 10
496
497 gpointer
498 mono_arch_create_specific_trampoline (gpointer arg1, MonoTrampolineType tramp_type, MonoDomain *domain, guint32 *code_len)
499 {
500         guint8 *code, *buf, *tramp;
501         
502         tramp = mono_get_trampoline_code (tramp_type);
503
504         code = buf = mono_domain_code_reserve_align (domain, TRAMPOLINE_SIZE, 4);
505
506         x86_push_imm (buf, arg1);
507         x86_jump_code (buf, tramp);
508         g_assert ((buf - code) <= TRAMPOLINE_SIZE);
509
510         mono_arch_flush_icache (code, buf - code);
511
512         if (code_len)
513                 *code_len = buf - code;
514
515         return code;
516 }
517
518 gpointer
519 mono_arch_create_rgctx_lazy_fetch_trampoline (guint32 slot)
520 {
521         guint32 code_size;
522         MonoJumpInfo *ji;
523
524         return mono_arch_create_rgctx_lazy_fetch_trampoline_full (slot, &code_size, &ji, FALSE);
525 }
526
527 gpointer
528 mono_arch_create_rgctx_lazy_fetch_trampoline_full (guint32 slot, guint32 *code_size, MonoJumpInfo **ji, gboolean aot)
529 {
530         guint8 *tramp;
531         guint8 *code, *buf;
532         guint8 **rgctx_null_jumps;
533         int tramp_size;
534         int depth, index;
535         int i;
536         gboolean mrgctx;
537
538         *ji = NULL;
539
540         mrgctx = MONO_RGCTX_SLOT_IS_MRGCTX (slot);
541         index = MONO_RGCTX_SLOT_INDEX (slot);
542         if (mrgctx)
543                 index += MONO_SIZEOF_METHOD_RUNTIME_GENERIC_CONTEXT / sizeof (gpointer);
544         for (depth = 0; ; ++depth) {
545                 int size = mono_class_rgctx_get_array_size (depth, mrgctx);
546
547                 if (index < size - 1)
548                         break;
549                 index -= size - 1;
550         }
551
552         tramp_size = (aot ? 64 : 36) + 6 * depth;
553
554         code = buf = mono_global_codeman_reserve (tramp_size);
555
556         rgctx_null_jumps = g_malloc (sizeof (guint8*) * (depth + 2));
557
558         /* load vtable/mrgctx ptr */
559         x86_mov_reg_membase (code, X86_EAX, X86_ESP, 4, 4);
560         if (!mrgctx) {
561                 /* load rgctx ptr from vtable */
562                 x86_mov_reg_membase (code, X86_EAX, X86_EAX, G_STRUCT_OFFSET (MonoVTable, runtime_generic_context), 4);
563                 /* is the rgctx ptr null? */
564                 x86_test_reg_reg (code, X86_EAX, X86_EAX);
565                 /* if yes, jump to actual trampoline */
566                 rgctx_null_jumps [0] = code;
567                 x86_branch8 (code, X86_CC_Z, -1, 1);
568         }
569
570         for (i = 0; i < depth; ++i) {
571                 /* load ptr to next array */
572                 if (mrgctx && i == 0)
573                         x86_mov_reg_membase (code, X86_EAX, X86_EAX, MONO_SIZEOF_METHOD_RUNTIME_GENERIC_CONTEXT, 4);
574                 else
575                         x86_mov_reg_membase (code, X86_EAX, X86_EAX, 0, 4);
576                 /* is the ptr null? */
577                 x86_test_reg_reg (code, X86_EAX, X86_EAX);
578                 /* if yes, jump to actual trampoline */
579                 rgctx_null_jumps [i + 1] = code;
580                 x86_branch8 (code, X86_CC_Z, -1, 1);
581         }
582
583         /* fetch slot */
584         x86_mov_reg_membase (code, X86_EAX, X86_EAX, sizeof (gpointer) * (index + 1), 4);
585         /* is the slot null? */
586         x86_test_reg_reg (code, X86_EAX, X86_EAX);
587         /* if yes, jump to actual trampoline */
588         rgctx_null_jumps [depth + 1] = code;
589         x86_branch8 (code, X86_CC_Z, -1, 1);
590         /* otherwise return */
591         x86_ret (code);
592
593         for (i = mrgctx ? 1 : 0; i <= depth + 1; ++i)
594                 x86_patch (rgctx_null_jumps [i], code);
595
596         g_free (rgctx_null_jumps);
597
598         x86_mov_reg_membase (code, MONO_ARCH_VTABLE_REG, X86_ESP, 4, 4);
599
600         if (aot) {
601                 code = mono_arch_emit_load_aotconst (buf, code, ji, MONO_PATCH_INFO_JIT_ICALL_ADDR, g_strdup_printf ("specific_trampoline_lazy_fetch_%u", slot));
602                 x86_jump_reg (code, X86_EAX);
603         } else {
604                 tramp = mono_arch_create_specific_trampoline (GUINT_TO_POINTER (slot), MONO_TRAMPOLINE_RGCTX_LAZY_FETCH, mono_get_root_domain (), NULL);
605
606                 /* jump to the actual trampoline */
607                 x86_jump_code (code, tramp);
608         }
609
610         mono_arch_flush_icache (buf, code - buf);
611
612         g_assert (code - buf <= tramp_size);
613
614         *code_size = code - buf;
615
616         return buf;
617 }
618
619 gpointer
620 mono_arch_create_generic_class_init_trampoline (void)
621 {
622         guint32 code_size;
623         MonoJumpInfo *ji;
624
625         return mono_arch_create_generic_class_init_trampoline_full (&code_size, &ji, FALSE);
626 }
627
628 gpointer
629 mono_arch_create_generic_class_init_trampoline_full (guint32 *code_size, MonoJumpInfo **ji, gboolean aot)
630 {
631         guint8 *tramp;
632         guint8 *code, *buf;
633         static int byte_offset = -1;
634         static guint8 bitmask;
635         guint8 *jump;
636         int tramp_size;
637
638         tramp_size = 64;
639
640         code = buf = mono_global_codeman_reserve (tramp_size);
641
642         *ji = NULL;
643
644         if (byte_offset < 0)
645                 mono_marshal_find_bitfield_offset (MonoVTable, initialized, &byte_offset, &bitmask);
646
647         x86_test_membase_imm (code, MONO_ARCH_VTABLE_REG, byte_offset, bitmask);
648         jump = code;
649         x86_branch8 (code, X86_CC_Z, -1, 1);
650
651         x86_ret (code);
652
653         x86_patch (jump, code);
654
655         /* Push the vtable so the stack is the same as in a specific trampoline */
656         x86_push_reg (code, MONO_ARCH_VTABLE_REG);
657
658         if (aot) {
659                 code = mono_arch_emit_load_aotconst (buf, code, ji, MONO_PATCH_INFO_JIT_ICALL_ADDR, "generic_trampoline_generic_class_init");
660                 x86_jump_reg (code, X86_EAX);
661         } else {
662                 tramp = mono_get_trampoline_code (MONO_TRAMPOLINE_GENERIC_CLASS_INIT);
663
664                 /* jump to the actual trampoline */
665                 x86_jump_code (code, tramp);
666         }
667
668         mono_arch_flush_icache (code, code - buf);
669
670         g_assert (code - buf <= tramp_size);
671
672         return buf;
673 }
674
675 #ifdef MONO_ARCH_MONITOR_OBJECT_REG
676 /*
677  * The code produced by this trampoline is equivalent to this:
678  *
679  * if (obj) {
680  *      if (obj->synchronisation) {
681  *              if (obj->synchronisation->owner == 0) {
682  *                      if (cmpxch (&obj->synchronisation->owner, TID, 0) == 0)
683  *                              return;
684  *              }
685  *              if (obj->synchronisation->owner == TID) {
686  *                      ++obj->synchronisation->nest;
687  *                      return;
688  *              }
689  *      }
690  * }
691  * return full_monitor_enter ();
692  *
693  */
694 gpointer
695 mono_arch_create_monitor_enter_trampoline (void)
696 {
697         guint32 code_size;
698         MonoJumpInfo *ji;
699
700         return mono_arch_create_monitor_enter_trampoline_full (&code_size, &ji, FALSE);
701 }
702
703 gpointer
704 mono_arch_create_monitor_enter_trampoline_full (guint32 *code_size, MonoJumpInfo **ji, gboolean aot)
705 {
706         guint8 *tramp = mono_get_trampoline_code (MONO_TRAMPOLINE_MONITOR_ENTER);
707         guint8 *code, *buf;
708         guint8 *jump_obj_null, *jump_sync_null, *jump_other_owner, *jump_cmpxchg_failed, *jump_tid;
709         int tramp_size;
710         int owner_offset, nest_offset, dummy;
711
712         *ji = NULL;
713
714         g_assert (MONO_ARCH_MONITOR_OBJECT_REG == X86_EAX);
715
716         mono_monitor_threads_sync_members_offset (&owner_offset, &nest_offset, &dummy);
717         g_assert (MONO_THREADS_SYNC_MEMBER_SIZE (owner_offset) == sizeof (gpointer));
718         g_assert (MONO_THREADS_SYNC_MEMBER_SIZE (nest_offset) == sizeof (guint32));
719         owner_offset = MONO_THREADS_SYNC_MEMBER_OFFSET (owner_offset);
720         nest_offset = MONO_THREADS_SYNC_MEMBER_OFFSET (nest_offset);
721
722         tramp_size = 64;
723
724         code = buf = mono_global_codeman_reserve (tramp_size);
725
726         if (mono_thread_get_tls_offset () != -1) {
727                 /* MonoObject* obj is in EAX */
728                 /* is obj null? */
729                 x86_test_reg_reg (code, X86_EAX, X86_EAX);
730                 /* if yes, jump to actual trampoline */
731                 jump_obj_null = code;
732                 x86_branch8 (code, X86_CC_Z, -1, 1);
733
734                 /* load obj->synchronization to ECX */
735                 x86_mov_reg_membase (code, X86_ECX, X86_EAX, G_STRUCT_OFFSET (MonoObject, synchronisation), 4);
736                 /* is synchronization null? */
737                 x86_test_reg_reg (code, X86_ECX, X86_ECX);
738                 /* if yes, jump to actual trampoline */
739                 jump_sync_null = code;
740                 x86_branch8 (code, X86_CC_Z, -1, 1);
741
742                 /* load MonoInternalThread* into EDX */
743                 code = mono_x86_emit_tls_get (code, X86_EDX, mono_thread_get_tls_offset ());
744                 /* load TID into EDX */
745                 x86_mov_reg_membase (code, X86_EDX, X86_EDX, G_STRUCT_OFFSET (MonoInternalThread, tid), 4);
746
747                 /* is synchronization->owner null? */
748                 x86_alu_membase_imm (code, X86_CMP, X86_ECX, owner_offset, 0);
749                 /* if not, jump to next case */
750                 jump_tid = code;
751                 x86_branch8 (code, X86_CC_NZ, -1, 1);
752
753                 /* if yes, try a compare-exchange with the TID */
754                 /* free up register EAX, needed for the zero */
755                 x86_push_reg (code, X86_EAX);
756                 /* zero EAX */
757                 x86_alu_reg_reg (code, X86_XOR, X86_EAX, X86_EAX);
758                 /* compare and exchange */
759                 x86_prefix (code, X86_LOCK_PREFIX);
760                 x86_cmpxchg_membase_reg (code, X86_ECX, owner_offset, X86_EDX);
761                 /* if not successful, jump to actual trampoline */
762                 jump_cmpxchg_failed = code;
763                 x86_branch8 (code, X86_CC_NZ, -1, 1);
764                 /* if successful, pop and return */
765                 x86_pop_reg (code, X86_EAX);
766                 x86_ret (code);
767
768                 /* next case: synchronization->owner is not null */
769                 x86_patch (jump_tid, code);
770                 /* is synchronization->owner == TID? */
771                 x86_alu_membase_reg (code, X86_CMP, X86_ECX, owner_offset, X86_EDX);
772                 /* if not, jump to actual trampoline */
773                 jump_other_owner = code;
774                 x86_branch8 (code, X86_CC_NZ, -1, 1);
775                 /* if yes, increment nest */
776                 x86_inc_membase (code, X86_ECX, nest_offset);
777                 /* return */
778                 x86_ret (code);
779
780                 /* push obj */
781                 x86_patch (jump_obj_null, code);
782                 x86_patch (jump_sync_null, code);
783                 x86_patch (jump_other_owner, code);
784                 x86_push_reg (code, X86_EAX);
785                 /* jump to the actual trampoline */
786                 x86_patch (jump_cmpxchg_failed, code);
787                 if (aot) {
788                         /* We are calling the generic trampoline directly, the argument is pushed
789                          * on the stack just like a specific trampoline.
790                          */
791                         code = mono_arch_emit_load_aotconst (buf, code, ji, MONO_PATCH_INFO_JIT_ICALL_ADDR, "generic_trampoline_monitor_enter");
792                         x86_jump_reg (code, X86_EAX);
793                 } else {
794                         x86_jump_code (code, tramp);
795                 }
796         } else {
797                 /* push obj and jump to the actual trampoline */
798                 x86_push_reg (code, X86_EAX);
799                 if (aot) {
800                         code = mono_arch_emit_load_aotconst (buf, code, ji, MONO_PATCH_INFO_JIT_ICALL_ADDR, "generic_trampoline_monitor_enter");
801                         x86_jump_reg (code, X86_EAX);
802                 } else {
803                         x86_jump_code (code, tramp);
804                 }
805         }
806
807         mono_arch_flush_icache (buf, code - buf);
808         g_assert (code - buf <= tramp_size);
809
810         *code_size = code - buf;
811
812         return buf;
813 }
814
815 gpointer
816 mono_arch_create_monitor_exit_trampoline (void)
817 {
818         guint32 code_size;
819         MonoJumpInfo *ji;
820
821         return mono_arch_create_monitor_exit_trampoline_full (&code_size, &ji, FALSE);
822 }
823
824 gpointer
825 mono_arch_create_monitor_exit_trampoline_full (guint32 *code_size, MonoJumpInfo **ji, gboolean aot)
826 {
827         guint8 *tramp = mono_get_trampoline_code (MONO_TRAMPOLINE_MONITOR_EXIT);
828         guint8 *code, *buf;
829         guint8 *jump_obj_null, *jump_have_waiters, *jump_sync_null, *jump_not_owned;
830         guint8 *jump_next;
831         int tramp_size;
832         int owner_offset, nest_offset, entry_count_offset;
833
834         *ji = NULL;
835
836         g_assert (MONO_ARCH_MONITOR_OBJECT_REG == X86_EAX);
837
838         mono_monitor_threads_sync_members_offset (&owner_offset, &nest_offset, &entry_count_offset);
839         g_assert (MONO_THREADS_SYNC_MEMBER_SIZE (owner_offset) == sizeof (gpointer));
840         g_assert (MONO_THREADS_SYNC_MEMBER_SIZE (nest_offset) == sizeof (guint32));
841         g_assert (MONO_THREADS_SYNC_MEMBER_SIZE (entry_count_offset) == sizeof (gint32));
842         owner_offset = MONO_THREADS_SYNC_MEMBER_OFFSET (owner_offset);
843         nest_offset = MONO_THREADS_SYNC_MEMBER_OFFSET (nest_offset);
844         entry_count_offset = MONO_THREADS_SYNC_MEMBER_OFFSET (entry_count_offset);
845
846         tramp_size = 64;
847
848         code = buf = mono_global_codeman_reserve (tramp_size);
849
850         if (mono_thread_get_tls_offset () != -1) {
851                 /* MonoObject* obj is in EAX */
852                 /* is obj null? */
853                 x86_test_reg_reg (code, X86_EAX, X86_EAX);
854                 /* if yes, jump to actual trampoline */
855                 jump_obj_null = code;
856                 x86_branch8 (code, X86_CC_Z, -1, 1);
857
858                 /* load obj->synchronization to ECX */
859                 x86_mov_reg_membase (code, X86_ECX, X86_EAX, G_STRUCT_OFFSET (MonoObject, synchronisation), 4);
860                 /* is synchronization null? */
861                 x86_test_reg_reg (code, X86_ECX, X86_ECX);
862                 /* if yes, jump to actual trampoline */
863                 jump_sync_null = code;
864                 x86_branch8 (code, X86_CC_Z, -1, 1);
865
866                 /* next case: synchronization is not null */
867                 /* load MonoInternalThread* into EDX */
868                 code = mono_x86_emit_tls_get (code, X86_EDX, mono_thread_get_tls_offset ());
869                 /* load TID into EDX */
870                 x86_mov_reg_membase (code, X86_EDX, X86_EDX, G_STRUCT_OFFSET (MonoInternalThread, tid), 4);
871                 /* is synchronization->owner == TID */
872                 x86_alu_membase_reg (code, X86_CMP, X86_ECX, owner_offset, X86_EDX);
873                 /* if no, jump to actual trampoline */
874                 jump_not_owned = code;
875                 x86_branch8 (code, X86_CC_NZ, -1, 1);
876
877                 /* next case: synchronization->owner == TID */
878                 /* is synchronization->nest == 1 */
879                 x86_alu_membase_imm (code, X86_CMP, X86_ECX, nest_offset, 1);
880                 /* if not, jump to next case */
881                 jump_next = code;
882                 x86_branch8 (code, X86_CC_NZ, -1, 1);
883                 /* if yes, is synchronization->entry_count zero? */
884                 x86_alu_membase_imm (code, X86_CMP, X86_ECX, entry_count_offset, 0);
885                 /* if not, jump to actual trampoline */
886                 jump_have_waiters = code;
887                 x86_branch8 (code, X86_CC_NZ, -1 , 1);
888                 /* if yes, set synchronization->owner to null and return */
889                 x86_mov_membase_imm (code, X86_ECX, owner_offset, 0, 4);
890                 x86_ret (code);
891
892                 /* next case: synchronization->nest is not 1 */
893                 x86_patch (jump_next, code);
894                 /* decrease synchronization->nest and return */
895                 x86_dec_membase (code, X86_ECX, nest_offset);
896                 x86_ret (code);
897
898                 /* push obj and jump to the actual trampoline */
899                 x86_patch (jump_obj_null, code);
900                 x86_patch (jump_have_waiters, code);
901                 x86_patch (jump_not_owned, code);
902                 x86_patch (jump_sync_null, code);
903         }
904
905         /* push obj and jump to the actual trampoline */
906         x86_push_reg (code, X86_EAX);
907         if (aot) {
908                 code = mono_arch_emit_load_aotconst (buf, code, ji, MONO_PATCH_INFO_JIT_ICALL_ADDR, "generic_trampoline_monitor_exit");
909                 x86_jump_reg (code, X86_EAX);
910         } else {
911                 x86_jump_code (code, tramp);
912         }
913
914         mono_arch_flush_icache (buf, code - buf);
915         g_assert (code - buf <= tramp_size);
916
917         *code_size = code - buf;
918
919         return buf;
920 }
921 #else
922 gpointer
923 mono_arch_create_monitor_enter_trampoline (void)
924 {
925         g_assert_not_reached ();
926         return NULL;
927 }
928
929 gpointer
930 mono_arch_create_monitor_exit_trampoline (void)
931 {
932         g_assert_not_reached ();
933         return NULL;
934 }
935 #endif
936
937 void
938 mono_arch_invalidate_method (MonoJitInfo *ji, void *func, gpointer func_arg)
939 {
940         /* FIXME: This is not thread safe */
941         guint8 *code = ji->code_start;
942
943         x86_push_imm (code, func_arg);
944         x86_call_code (code, (guint8*)func);
945 }
946
947 static void
948 handler_block_trampoline_helper (gpointer *ptr)
949 {
950         MonoJitTlsData *jit_tls = TlsGetValue (mono_jit_tls_id);
951         *ptr = jit_tls->handler_block_return_address;
952 }
953
954 gpointer
955 mono_arch_create_handler_block_trampoline (void)
956 {
957         guint8 *tramp = mono_get_trampoline_code (MONO_TRAMPOLINE_HANDLER_BLOCK_GUARD);
958         guint8 *code, *buf;
959         int tramp_size = 64;
960         code = buf = mono_global_codeman_reserve (tramp_size);
961
962         /*
963         This trampoline restore the call chain of the handler block then jumps into the code that deals with it.
964         */
965
966         if (mono_get_jit_tls_offset () != -1) {
967                 code = mono_x86_emit_tls_get (code, X86_EAX, mono_get_jit_tls_offset ());
968                 x86_mov_reg_membase (code, X86_EAX, X86_EAX, G_STRUCT_OFFSET (MonoJitTlsData, handler_block_return_address), 4);
969                 /*simulate a call*/
970                 x86_push_reg (code, X86_EAX);
971                 x86_jump_code (code, tramp);
972         } else {
973                 /*Slow path uses a c helper*/
974                 x86_push_reg (code, X86_ESP);
975                 x86_push_imm (code, tramp);
976                 x86_jump_code (code, handler_block_trampoline_helper);
977         }
978
979         mono_arch_flush_icache (buf, code - buf);
980         g_assert (code - buf <= tramp_size);
981
982         return buf;
983 }