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