2009-09-06 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         /* Align the stack on osx */
337         x86_alu_reg_imm (buf, X86_SUB, X86_ESP, 3 * 4);
338         x86_push_reg (buf, X86_EAX);
339         x86_call_code (buf, (guint8*)mono_thread_force_interruption_checkpoint);
340         x86_pop_reg (buf, X86_EAX);
341         x86_alu_reg_imm (buf, X86_ADD, X86_ESP, 3 * 4);
342
343         /* Restore LMF */
344
345         /* ebx = previous_lmf */
346         x86_pop_reg (buf, X86_EBX);
347         pushed_args--;
348         x86_alu_reg_imm (buf, X86_SUB, X86_EBX, 1);
349
350         /* edi = lmf */
351         x86_pop_reg (buf, X86_EDI);
352         pushed_args--;
353
354         /* *(lmf) = previous_lmf */
355         x86_mov_membase_reg (buf, X86_EDI, 0, X86_EBX, 4);
356
357         /* discard method info */
358         x86_pop_reg (buf, X86_ESI);
359         pushed_args--;
360
361         /* discard ESP */
362         x86_pop_reg (buf, X86_ESI);
363         pushed_args--;
364
365         /* restore caller saved regs */
366         x86_pop_reg (buf, X86_EBX);
367         x86_pop_reg (buf, X86_EDI);
368         x86_pop_reg (buf, X86_ESI);
369         x86_pop_reg (buf, X86_EBP);
370
371         pushed_args -= 4;
372
373         /* discard save IP */
374         x86_alu_reg_imm (buf, X86_ADD, X86_ESP, 4);
375         pushed_args--;
376
377         /* restore LMF end */
378
379         if (!MONO_TRAMPOLINE_TYPE_MUST_RETURN (tramp_type)) {
380                 /* 
381                  * Overwrite the method ptr with the address we need to jump to,
382                  * to free %eax.
383                  */
384                 x86_mov_membase_reg (buf, X86_ESP, pushed_args * sizeof (gpointer), X86_EAX, 4);
385         }
386
387         /* Restore caller saved registers */
388         x86_mov_reg_membase (buf, X86_ECX, X86_ESP, (pushed_args - pushed_args_caller_saved + X86_ECX) * 4, 4);
389         x86_mov_reg_membase (buf, X86_EDX, X86_ESP, (pushed_args - pushed_args_caller_saved + X86_EDX) * 4, 4);
390         if ((tramp_type == MONO_TRAMPOLINE_RESTORE_STACK_PROT) || (tramp_type == MONO_TRAMPOLINE_AOT_PLT))
391                 x86_mov_reg_membase (buf, X86_EAX, X86_ESP, (pushed_args - pushed_args_caller_saved + X86_EAX) * 4, 4);
392
393         if (!MONO_TRAMPOLINE_TYPE_MUST_RETURN (tramp_type)) {
394                 /* Pop saved reg array + stack align */
395                 x86_alu_reg_imm (buf, X86_ADD, X86_ESP, 9 * 4);
396                 pushed_args -= 9;
397                 g_assert (pushed_args == 0);
398         } else {
399                 /* Pop saved reg array + stack align + method ptr */
400                 x86_alu_reg_imm (buf, X86_ADD, X86_ESP, 10 * 4);
401                 pushed_args -= 10;
402
403                 /* We've popped one more stack item than we've pushed (the
404                    method ptr argument), so we must end up at -1. */
405                 g_assert (pushed_args == -1);
406         }
407
408         x86_ret (buf);
409
410         g_assert ((buf - code) <= 256);
411
412         if (tramp_type == MONO_TRAMPOLINE_CLASS_INIT) {
413                 /* Initialize the nullified class init trampoline used in the AOT case */
414                 nullified_class_init_trampoline = buf = mono_global_codeman_reserve (16);
415                 x86_ret (buf);
416         }
417
418         return code;
419 }
420
421 #define TRAMPOLINE_SIZE 10
422
423 gpointer
424 mono_arch_create_specific_trampoline (gpointer arg1, MonoTrampolineType tramp_type, MonoDomain *domain, guint32 *code_len)
425 {
426         guint8 *code, *buf, *tramp;
427         
428         tramp = mono_get_trampoline_code (tramp_type);
429
430         code = buf = mono_domain_code_reserve_align (domain, TRAMPOLINE_SIZE, 4);
431
432         x86_push_imm (buf, arg1);
433         x86_jump_code (buf, tramp);
434         g_assert ((buf - code) <= TRAMPOLINE_SIZE);
435
436         mono_arch_flush_icache (code, buf - code);
437
438         if (code_len)
439                 *code_len = buf - code;
440
441         return code;
442 }
443
444 gpointer
445 mono_arch_create_rgctx_lazy_fetch_trampoline (guint32 slot)
446 {
447         guint8 *tramp;
448         guint8 *code, *buf;
449         guint8 **rgctx_null_jumps;
450         int tramp_size;
451         int depth, index;
452         int i;
453         gboolean mrgctx;
454
455         mrgctx = MONO_RGCTX_SLOT_IS_MRGCTX (slot);
456         index = MONO_RGCTX_SLOT_INDEX (slot);
457         if (mrgctx)
458                 index += MONO_SIZEOF_METHOD_RUNTIME_GENERIC_CONTEXT / sizeof (gpointer);
459         for (depth = 0; ; ++depth) {
460                 int size = mono_class_rgctx_get_array_size (depth, mrgctx);
461
462                 if (index < size - 1)
463                         break;
464                 index -= size - 1;
465         }
466
467         tramp_size = 36 + 6 * depth;
468
469         code = buf = mono_global_codeman_reserve (tramp_size);
470
471         rgctx_null_jumps = g_malloc (sizeof (guint8*) * (depth + 2));
472
473         /* load vtable/mrgctx ptr */
474         x86_mov_reg_membase (buf, X86_EAX, X86_ESP, 4, 4);
475         if (!mrgctx) {
476                 /* load rgctx ptr from vtable */
477                 x86_mov_reg_membase (buf, X86_EAX, X86_EAX, G_STRUCT_OFFSET (MonoVTable, runtime_generic_context), 4);
478                 /* is the rgctx ptr null? */
479                 x86_test_reg_reg (buf, X86_EAX, X86_EAX);
480                 /* if yes, jump to actual trampoline */
481                 rgctx_null_jumps [0] = buf;
482                 x86_branch8 (buf, X86_CC_Z, -1, 1);
483         }
484
485         for (i = 0; i < depth; ++i) {
486                 /* load ptr to next array */
487                 if (mrgctx && i == 0)
488                         x86_mov_reg_membase (buf, X86_EAX, X86_EAX, MONO_SIZEOF_METHOD_RUNTIME_GENERIC_CONTEXT, 4);
489                 else
490                         x86_mov_reg_membase (buf, X86_EAX, X86_EAX, 0, 4);
491                 /* is the ptr null? */
492                 x86_test_reg_reg (buf, X86_EAX, X86_EAX);
493                 /* if yes, jump to actual trampoline */
494                 rgctx_null_jumps [i + 1] = buf;
495                 x86_branch8 (buf, X86_CC_Z, -1, 1);
496         }
497
498         /* fetch slot */
499         x86_mov_reg_membase (buf, X86_EAX, X86_EAX, sizeof (gpointer) * (index + 1), 4);
500         /* is the slot null? */
501         x86_test_reg_reg (buf, X86_EAX, X86_EAX);
502         /* if yes, jump to actual trampoline */
503         rgctx_null_jumps [depth + 1] = buf;
504         x86_branch8 (buf, X86_CC_Z, -1, 1);
505         /* otherwise return */
506         x86_ret (buf);
507
508         for (i = mrgctx ? 1 : 0; i <= depth + 1; ++i)
509                 x86_patch (rgctx_null_jumps [i], buf);
510
511         g_free (rgctx_null_jumps);
512
513         x86_mov_reg_membase (buf, MONO_ARCH_VTABLE_REG, X86_ESP, 4, 4);
514
515         tramp = mono_arch_create_specific_trampoline (GUINT_TO_POINTER (slot), MONO_TRAMPOLINE_RGCTX_LAZY_FETCH, mono_get_root_domain (), NULL);
516
517         /* jump to the actual trampoline */
518         x86_jump_code (buf, tramp);
519
520         mono_arch_flush_icache (code, buf - code);
521
522         g_assert (buf - code <= tramp_size);
523
524         return code;
525 }
526
527 gpointer
528 mono_arch_create_generic_class_init_trampoline (void)
529 {
530         guint8 *tramp;
531         guint8 *code, *buf;
532         static int byte_offset = -1;
533         static guint8 bitmask;
534         guint8 *jump;
535         int tramp_size;
536
537         tramp_size = 64;
538
539         code = buf = mono_global_codeman_reserve (tramp_size);
540
541         if (byte_offset < 0)
542                 mono_marshal_find_bitfield_offset (MonoVTable, initialized, &byte_offset, &bitmask);
543
544         x86_test_membase_imm (code, MONO_ARCH_VTABLE_REG, byte_offset, bitmask);
545         jump = code;
546         x86_branch8 (code, X86_CC_Z, -1, 1);
547
548         x86_ret (code);
549
550         x86_patch (jump, code);
551
552         /* Push the vtable so the stack is the same as in a specific trampoline */
553         x86_push_reg (code, MONO_ARCH_VTABLE_REG);
554
555         tramp = mono_get_trampoline_code (MONO_TRAMPOLINE_GENERIC_CLASS_INIT);
556
557         /* jump to the actual trampoline */
558         x86_jump_code (code, tramp);
559
560         mono_arch_flush_icache (code, code - buf);
561
562         g_assert (code - buf <= tramp_size);
563
564         return buf;
565 }
566
567 #ifdef MONO_ARCH_MONITOR_OBJECT_REG
568 /*
569  * The code produced by this trampoline is equivalent to this:
570  *
571  * if (obj) {
572  *      if (obj->synchronisation) {
573  *              if (obj->synchronisation->owner == 0) {
574  *                      if (cmpxch (&obj->synchronisation->owner, TID, 0) == 0)
575  *                              return;
576  *              }
577  *              if (obj->synchronisation->owner == TID) {
578  *                      ++obj->synchronisation->nest;
579  *                      return;
580  *              }
581  *      }
582  * }
583  * return full_monitor_enter ();
584  *
585  */
586 gpointer
587 mono_arch_create_monitor_enter_trampoline (void)
588 {
589         guint32 code_size;
590         MonoJumpInfo *ji;
591
592         return mono_arch_create_monitor_enter_trampoline_full (&code_size, &ji, FALSE);
593 }
594
595 gpointer
596 mono_arch_create_monitor_enter_trampoline_full (guint32 *code_size, MonoJumpInfo **ji, gboolean aot)
597 {
598         guint8 *tramp = mono_get_trampoline_code (MONO_TRAMPOLINE_MONITOR_ENTER);
599         guint8 *code, *buf;
600         guint8 *jump_obj_null, *jump_sync_null, *jump_other_owner, *jump_cmpxchg_failed, *jump_tid;
601         int tramp_size;
602         int owner_offset, nest_offset, dummy;
603
604         g_assert (MONO_ARCH_MONITOR_OBJECT_REG == X86_EAX);
605
606         mono_monitor_threads_sync_members_offset (&owner_offset, &nest_offset, &dummy);
607         g_assert (MONO_THREADS_SYNC_MEMBER_SIZE (owner_offset) == sizeof (gpointer));
608         g_assert (MONO_THREADS_SYNC_MEMBER_SIZE (nest_offset) == sizeof (guint32));
609         owner_offset = MONO_THREADS_SYNC_MEMBER_OFFSET (owner_offset);
610         nest_offset = MONO_THREADS_SYNC_MEMBER_OFFSET (nest_offset);
611
612         tramp_size = 64;
613
614         code = buf = mono_global_codeman_reserve (tramp_size);
615
616         if (mono_thread_get_tls_offset () != -1) {
617                 /* MonoObject* obj is in EAX */
618                 /* is obj null? */
619                 x86_test_reg_reg (buf, X86_EAX, X86_EAX);
620                 /* if yes, jump to actual trampoline */
621                 jump_obj_null = buf;
622                 x86_branch8 (buf, X86_CC_Z, -1, 1);
623
624                 /* load obj->synchronization to ECX */
625                 x86_mov_reg_membase (buf, X86_ECX, X86_EAX, G_STRUCT_OFFSET (MonoObject, synchronisation), 4);
626                 /* is synchronization null? */
627                 x86_test_reg_reg (buf, X86_ECX, X86_ECX);
628                 /* if yes, jump to actual trampoline */
629                 jump_sync_null = buf;
630                 x86_branch8 (buf, X86_CC_Z, -1, 1);
631
632                 /* load MonoThread* into EDX */
633                 buf = mono_x86_emit_tls_get (buf, X86_EDX, mono_thread_get_tls_offset ());
634                 /* load TID into EDX */
635                 x86_mov_reg_membase (buf, X86_EDX, X86_EDX, G_STRUCT_OFFSET (MonoThread, tid), 4);
636
637                 /* is synchronization->owner null? */
638                 x86_alu_membase_imm (buf, X86_CMP, X86_ECX, owner_offset, 0);
639                 /* if not, jump to next case */
640                 jump_tid = buf;
641                 x86_branch8 (buf, X86_CC_NZ, -1, 1);
642
643                 /* if yes, try a compare-exchange with the TID */
644                 /* free up register EAX, needed for the zero */
645                 x86_push_reg (buf, X86_EAX);
646                 /* zero EAX */
647                 x86_alu_reg_reg (buf, X86_XOR, X86_EAX, X86_EAX);
648                 /* compare and exchange */
649                 x86_prefix (buf, X86_LOCK_PREFIX);
650                 x86_cmpxchg_membase_reg (buf, X86_ECX, owner_offset, X86_EDX);
651                 /* if not successful, jump to actual trampoline */
652                 jump_cmpxchg_failed = buf;
653                 x86_branch8 (buf, X86_CC_NZ, -1, 1);
654                 /* if successful, pop and return */
655                 x86_pop_reg (buf, X86_EAX);
656                 x86_ret (buf);
657
658                 /* next case: synchronization->owner is not null */
659                 x86_patch (jump_tid, buf);
660                 /* is synchronization->owner == TID? */
661                 x86_alu_membase_reg (buf, X86_CMP, X86_ECX, owner_offset, X86_EDX);
662                 /* if not, jump to actual trampoline */
663                 jump_other_owner = buf;
664                 x86_branch8 (buf, X86_CC_NZ, -1, 1);
665                 /* if yes, increment nest */
666                 x86_inc_membase (buf, X86_ECX, nest_offset);
667                 /* return */
668                 x86_ret (buf);
669
670                 /* push obj */
671                 x86_patch (jump_obj_null, buf);
672                 x86_patch (jump_sync_null, buf);
673                 x86_patch (jump_other_owner, buf);
674                 x86_push_reg (buf, X86_EAX);
675                 /* jump to the actual trampoline */
676                 x86_patch (jump_cmpxchg_failed, buf);
677                 x86_jump_code (buf, tramp);
678         } else {
679                 /* push obj and jump to the actual trampoline */
680                 x86_push_reg (buf, X86_EAX);
681                 x86_jump_code (buf, tramp);
682         }
683
684         mono_arch_flush_icache (buf, buf - code);
685         g_assert (buf - code <= tramp_size);
686
687         return code;
688 }
689
690 gpointer
691 mono_arch_create_monitor_exit_trampoline (void)
692 {
693         guint32 code_size;
694         MonoJumpInfo *ji;
695
696         return mono_arch_create_monitor_exit_trampoline_full (&code_size, &ji, FALSE);
697 }
698
699 gpointer
700 mono_arch_create_monitor_exit_trampoline_full (guint32 *code_size, MonoJumpInfo **ji, gboolean aot)
701 {
702         guint8 *tramp = mono_get_trampoline_code (MONO_TRAMPOLINE_MONITOR_EXIT);
703         guint8 *code, *buf;
704         guint8 *jump_obj_null, *jump_have_waiters;
705         guint8 *jump_next;
706         int tramp_size;
707         int owner_offset, nest_offset, entry_count_offset;
708
709         g_assert (MONO_ARCH_MONITOR_OBJECT_REG == X86_EAX);
710
711         mono_monitor_threads_sync_members_offset (&owner_offset, &nest_offset, &entry_count_offset);
712         g_assert (MONO_THREADS_SYNC_MEMBER_SIZE (owner_offset) == sizeof (gpointer));
713         g_assert (MONO_THREADS_SYNC_MEMBER_SIZE (nest_offset) == sizeof (guint32));
714         g_assert (MONO_THREADS_SYNC_MEMBER_SIZE (entry_count_offset) == sizeof (gint32));
715         owner_offset = MONO_THREADS_SYNC_MEMBER_OFFSET (owner_offset);
716         nest_offset = MONO_THREADS_SYNC_MEMBER_OFFSET (nest_offset);
717         entry_count_offset = MONO_THREADS_SYNC_MEMBER_OFFSET (entry_count_offset);
718
719         tramp_size = 64;
720
721         code = buf = mono_global_codeman_reserve (tramp_size);
722
723         if (mono_thread_get_tls_offset () != -1) {
724                 /* MonoObject* obj is in EAX */
725                 /* is obj null? */
726                 x86_test_reg_reg (buf, X86_EAX, X86_EAX);
727                 /* if yes, jump to actual trampoline */
728                 jump_obj_null = buf;
729                 x86_branch8 (buf, X86_CC_Z, -1, 1);
730
731                 /* load obj->synchronization to ECX */
732                 x86_mov_reg_membase (buf, X86_ECX, X86_EAX, G_STRUCT_OFFSET (MonoObject, synchronisation), 4);
733                 /* is synchronization null? */
734                 x86_test_reg_reg (buf, X86_ECX, X86_ECX);
735                 /* if not, jump to next case */
736                 jump_next = buf;
737                 x86_branch8 (buf, X86_CC_NZ, -1, 1);
738                 /* if yes, just return */
739                 x86_ret (buf);
740
741                 /* next case: synchronization is not null */
742                 x86_patch (jump_next, buf);
743                 /* load MonoThread* into EDX */
744                 buf = mono_x86_emit_tls_get (buf, X86_EDX, mono_thread_get_tls_offset ());
745                 /* load TID into EDX */
746                 x86_mov_reg_membase (buf, X86_EDX, X86_EDX, G_STRUCT_OFFSET (MonoThread, tid), 4);
747                 /* is synchronization->owner == TID */
748                 x86_alu_membase_reg (buf, X86_CMP, X86_ECX, owner_offset, X86_EDX);
749                 /* if yes, jump to next case */
750                 jump_next = buf;
751                 x86_branch8 (buf, X86_CC_Z, -1, 1);
752                 /* if not, just return */
753                 x86_ret (buf);
754
755                 /* next case: synchronization->owner == TID */
756                 x86_patch (jump_next, buf);
757                 /* is synchronization->nest == 1 */
758                 x86_alu_membase_imm (buf, X86_CMP, X86_ECX, nest_offset, 1);
759                 /* if not, jump to next case */
760                 jump_next = buf;
761                 x86_branch8 (buf, X86_CC_NZ, -1, 1);
762                 /* if yes, is synchronization->entry_count zero? */
763                 x86_alu_membase_imm (buf, X86_CMP, X86_ECX, entry_count_offset, 0);
764                 /* if not, jump to actual trampoline */
765                 jump_have_waiters = buf;
766                 x86_branch8 (buf, X86_CC_NZ, -1 , 1);
767                 /* if yes, set synchronization->owner to null and return */
768                 x86_mov_membase_imm (buf, X86_ECX, owner_offset, 0, 4);
769                 x86_ret (buf);
770
771                 /* next case: synchronization->nest is not 1 */
772                 x86_patch (jump_next, buf);
773                 /* decrease synchronization->nest and return */
774                 x86_dec_membase (buf, X86_ECX, nest_offset);
775                 x86_ret (buf);
776
777                 /* push obj and jump to the actual trampoline */
778                 x86_patch (jump_obj_null, buf);
779                 x86_patch (jump_have_waiters, buf);
780                 x86_push_reg (buf, X86_EAX);
781                 x86_jump_code (buf, tramp);
782         } else {
783                 /* push obj and jump to the actual trampoline */
784                 x86_push_reg (buf, X86_EAX);
785                 x86_jump_code (buf, tramp);
786         }
787
788         mono_arch_flush_icache (buf, buf - code);
789         g_assert (buf - code <= tramp_size);
790
791         return code;
792 }
793 #else
794 gpointer
795 mono_arch_create_monitor_enter_trampoline (void)
796 {
797         g_assert_not_reached ();
798         return NULL;
799 }
800
801 gpointer
802 mono_arch_create_monitor_exit_trampoline (void)
803 {
804         g_assert_not_reached ();
805         return NULL;
806 }
807 #endif
808
809 void
810 mono_arch_invalidate_method (MonoJitInfo *ji, void *func, gpointer func_arg)
811 {
812         /* FIXME: This is not thread safe */
813         guint8 *code = ji->code_start;
814
815         x86_push_imm (code, func_arg);
816         x86_call_code (code, (guint8*)func);
817 }