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