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