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