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