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