2 * tramp-x86.c: JIT trampoline code for x86
5 * Dietmar Maurer (dietmar@ximian.com)
7 * (C) 2001 Ximian, Inc.
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>
22 #ifdef HAVE_VALGRIND_MEMCHECK_H
23 #include <valgrind/memcheck.h>
29 static guint8* nullified_class_init_trampoline;
32 * mono_arch_get_unbox_trampoline:
33 * @gsctx: the generic sharing context
35 * @addr: pointer to native code for @m
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
42 mono_arch_get_unbox_trampoline (MonoGenericSharingContext *gsctx, MonoMethod *m, gpointer addr)
46 MonoDomain *domain = mono_domain_get ();
48 if (MONO_TYPE_ISSTRUCT (mono_method_signature (m)->ret))
51 start = code = mono_domain_code_reserve (domain, 16);
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);
61 mono_arch_get_static_rgctx_trampoline (MonoMethod *m, MonoMethodRuntimeGenericContext *mrgctx, gpointer addr)
66 MonoDomain *domain = mono_domain_get ();
70 start = code = mono_domain_code_reserve (domain, buf_len);
72 x86_mov_reg_imm (code, MONO_ARCH_RGCTX_REG, mrgctx);
73 x86_jump_code (code, addr);
74 g_assert ((code - start) <= buf_len);
76 mono_arch_flush_icache (start, code - start);
82 mono_arch_patch_callsite (guint8 *method_start, guint8 *orig_code, guint8 *addr)
86 gboolean can_write = mono_breakpoint_clean_code (method_start, orig_code, 8, buf, sizeof (buf));
89 if (mono_running_on_valgrind ())
92 /* go to the start of the call instruction
94 * address_byte = (m << 6) | (o << 3) | reg
95 * call opcode: 0xff address_byte displacement
101 if ((code [1] == 0xe8)) {
103 InterlockedExchange ((gint32*)(orig_code + 2), (guint)addr - ((guint)orig_code + 1) - 5);
105 #ifdef HAVE_VALGRIND_MEMCHECK_H
106 /* Tell valgrind to recompile the patched code */
107 //VALGRIND_DISCARD_TRANSLATIONS (code + 2, code + 6);
110 } else if (code [1] == 0xe9) {
111 /* A PLT entry: jmp <DISP> */
113 InterlockedExchange ((gint32*)(orig_code + 2), (guint)addr - ((guint)orig_code + 1) - 5);
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 ();
122 mono_arch_patch_plt_entry (guint8 *code, gpointer *got, mgreg_t *regs, guint8 *addr)
124 /* A PLT entry: jmp <DISP> */
125 g_assert (code [0] == 0xe9);
127 if (!mono_running_on_valgrind ())
128 InterlockedExchange ((gint32*)(code + 1), (guint)addr - (guint)code - 5);
132 mono_arch_nullify_class_init_trampoline (guint8 *code, mgreg_t *regs)
135 gboolean can_write = mono_breakpoint_clean_code (NULL, code, 6, buf, sizeof (buf));
141 if (code [0] == 0xe8) {
142 if (!mono_running_on_valgrind ()) {
145 * Thread safe code patching using the algorithm from the paper
146 * 'Practicing JUDO: Java Under Dynamic Optimizations'
149 * First atomically change the the first 2 bytes of the call to a
153 InterlockedExchange ((gint32*)code, ops);
155 /* Then change the other bytes to a nop */
160 /* Then atomically change the first 4 bytes to a nop as well */
162 InterlockedExchange ((gint32*)code, ops);
163 #ifdef HAVE_VALGRIND_MEMCHECK_H
164 /* FIXME: the calltree skin trips on the self modifying code above */
166 /* Tell valgrind to recompile the patched code */
167 //VALGRIND_DISCARD_TRANSLATIONS (code, code + 8);
170 } else if (code [0] == 0x90 || code [0] == 0xeb) {
171 /* Already changed by another thread */
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;
177 vtable_slot = mono_get_vcall_slot_addr (code + 5, regs);
178 g_assert (vtable_slot);
180 *vtable_slot = nullified_class_init_trampoline;
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 ();
189 mono_arch_nullify_plt_entry (guint8 *code, mgreg_t *regs)
191 if (!mono_running_on_valgrind ()) {
195 InterlockedExchange ((gint32*)code, ops);
197 /* Then change the other bytes to a nop */
202 /* Change the first byte to a nop */
204 InterlockedExchange ((gint32*)code, ops);
209 mono_arch_create_trampoline_code (MonoTrampolineType tramp_type)
211 guint8 *buf, *code, *tramp;
212 int pushed_args, pushed_args_caller_saved;
214 code = buf = mono_global_codeman_reserve (256);
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)
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.
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);
234 pushed_args_caller_saved = pushed_args = 8;
236 /* Align stack on apple */
237 x86_alu_reg_imm (buf, X86_SUB, X86_ESP, 4);
243 /* save the IP (caller ip) */
244 if (tramp_type == MONO_TRAMPOLINE_JUMP)
245 x86_push_imm (buf, 0);
247 x86_push_membase (buf, X86_ESP, (pushed_args + 1) * sizeof (gpointer));
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);
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);
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));
269 x86_push_imm (buf, 0);
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.
278 g_assert (pushed_args == 16);
280 /* get the address of lmf for the current thread */
281 x86_call_code (buf, mono_get_lmf_addr);
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);
289 x86_mov_membase_reg (buf, X86_EAX, 0, X86_ESP, 4);
294 /* starting the call sequence */
296 /* FIXME: Push the trampoline address */
297 x86_push_imm (buf, 0);
301 /* push the method info */
302 x86_push_membase (buf, X86_ESP, pushed_args * sizeof (gpointer));
306 /* push the return address onto the stack */
307 if (tramp_type == MONO_TRAMPOLINE_JUMP)
308 x86_push_imm (buf, 0);
310 x86_push_membase (buf, X86_ESP, (pushed_args + 1) * sizeof (gpointer));
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);
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);*/
327 tramp = (guint8*)mono_get_trampoline_func (tramp_type);
328 x86_call_code (buf, tramp);
330 x86_alu_reg_imm (buf, X86_ADD, X86_ESP, 4*4);
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);
342 /* ebx = previous_lmf */
343 x86_pop_reg (buf, X86_EBX);
345 x86_alu_reg_imm (buf, X86_SUB, X86_EBX, 1);
348 x86_pop_reg (buf, X86_EDI);
351 /* *(lmf) = previous_lmf */
352 x86_mov_membase_reg (buf, X86_EDI, 0, X86_EBX, 4);
354 /* discard method info */
355 x86_pop_reg (buf, X86_ESI);
359 x86_pop_reg (buf, X86_ESI);
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);
370 /* discard save IP */
371 x86_alu_reg_imm (buf, X86_ADD, X86_ESP, 4);
374 /* restore LMF end */
376 if (!MONO_TRAMPOLINE_TYPE_MUST_RETURN (tramp_type)) {
378 * Overwrite the method ptr with the address we need to jump to,
381 x86_mov_membase_reg (buf, X86_ESP, pushed_args * sizeof (gpointer), X86_EAX, 4);
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);
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);
394 g_assert (pushed_args == 0);
396 /* Pop saved reg array + stack align + method ptr */
397 x86_alu_reg_imm (buf, X86_ADD, X86_ESP, 10 * 4);
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);
407 g_assert ((buf - code) <= 256);
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);
418 #define TRAMPOLINE_SIZE 10
421 mono_arch_create_specific_trampoline (gpointer arg1, MonoTrampolineType tramp_type, MonoDomain *domain, guint32 *code_len)
423 guint8 *code, *buf, *tramp;
425 tramp = mono_get_trampoline_code (tramp_type);
427 code = buf = mono_domain_code_reserve_align (domain, TRAMPOLINE_SIZE, 4);
429 x86_push_imm (buf, arg1);
430 x86_jump_code (buf, tramp);
431 g_assert ((buf - code) <= TRAMPOLINE_SIZE);
433 mono_arch_flush_icache (code, buf - code);
436 *code_len = buf - code;
442 mono_arch_create_rgctx_lazy_fetch_trampoline (guint32 slot)
446 guint8 **rgctx_null_jumps;
452 mrgctx = MONO_RGCTX_SLOT_IS_MRGCTX (slot);
453 index = MONO_RGCTX_SLOT_INDEX (slot);
455 index += MONO_SIZEOF_METHOD_RUNTIME_GENERIC_CONTEXT / sizeof (gpointer);
456 for (depth = 0; ; ++depth) {
457 int size = mono_class_rgctx_get_array_size (depth, mrgctx);
459 if (index < size - 1)
464 tramp_size = 36 + 6 * depth;
466 code = buf = mono_global_codeman_reserve (tramp_size);
468 rgctx_null_jumps = g_malloc (sizeof (guint8*) * (depth + 2));
470 /* load vtable/mrgctx ptr */
471 x86_mov_reg_membase (buf, X86_EAX, X86_ESP, 4, 4);
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);
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, MONO_SIZEOF_METHOD_RUNTIME_GENERIC_CONTEXT, 4);
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);
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 */
505 for (i = mrgctx ? 1 : 0; i <= depth + 1; ++i)
506 x86_patch (rgctx_null_jumps [i], buf);
508 g_free (rgctx_null_jumps);
510 x86_mov_reg_membase (buf, MONO_ARCH_VTABLE_REG, X86_ESP, 4, 4);
512 tramp = mono_arch_create_specific_trampoline (GUINT_TO_POINTER (slot), MONO_TRAMPOLINE_RGCTX_LAZY_FETCH, mono_get_root_domain (), NULL);
514 /* jump to the actual trampoline */
515 x86_jump_code (buf, tramp);
517 mono_arch_flush_icache (code, buf - code);
519 g_assert (buf - code <= tramp_size);
525 mono_arch_create_generic_class_init_trampoline (void)
529 static int byte_offset = -1;
530 static guint8 bitmask;
536 code = buf = mono_global_codeman_reserve (tramp_size);
539 mono_marshal_find_bitfield_offset (MonoVTable, initialized, &byte_offset, &bitmask);
541 x86_test_membase_imm (code, MONO_ARCH_VTABLE_REG, byte_offset, bitmask);
543 x86_branch8 (code, X86_CC_Z, -1, 1);
547 x86_patch (jump, code);
549 /* Push the vtable so the stack is the same as in a specific trampoline */
550 x86_push_reg (code, MONO_ARCH_VTABLE_REG);
552 tramp = mono_get_trampoline_code (MONO_TRAMPOLINE_GENERIC_CLASS_INIT);
554 /* jump to the actual trampoline */
555 x86_jump_code (code, tramp);
557 mono_arch_flush_icache (code, code - buf);
559 g_assert (code - buf <= tramp_size);
564 #ifdef MONO_ARCH_MONITOR_OBJECT_REG
566 * The code produced by this trampoline is equivalent to this:
569 * if (obj->synchronisation) {
570 * if (obj->synchronisation->owner == 0) {
571 * if (cmpxch (&obj->synchronisation->owner, TID, 0) == 0)
574 * if (obj->synchronisation->owner == TID) {
575 * ++obj->synchronisation->nest;
580 * return full_monitor_enter ();
584 mono_arch_create_monitor_enter_trampoline (void)
589 return mono_arch_create_monitor_enter_trampoline_full (&code_size, &ji, FALSE);
593 mono_arch_create_monitor_enter_trampoline_full (guint32 *code_size, MonoJumpInfo **ji, gboolean aot)
595 guint8 *tramp = mono_get_trampoline_code (MONO_TRAMPOLINE_MONITOR_ENTER);
597 guint8 *jump_obj_null, *jump_sync_null, *jump_other_owner, *jump_cmpxchg_failed, *jump_tid;
599 int owner_offset, nest_offset, dummy;
601 g_assert (MONO_ARCH_MONITOR_OBJECT_REG == X86_EAX);
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);
611 code = buf = mono_global_codeman_reserve (tramp_size);
613 if (mono_thread_get_tls_offset () != -1) {
614 /* MonoObject* obj is in EAX */
616 x86_test_reg_reg (buf, X86_EAX, X86_EAX);
617 /* if yes, jump to actual trampoline */
619 x86_branch8 (buf, X86_CC_Z, -1, 1);
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);
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);
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 */
638 x86_branch8 (buf, X86_CC_NZ, -1, 1);
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);
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);
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);
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);
676 /* push obj and jump to the actual trampoline */
677 x86_push_reg (buf, X86_EAX);
678 x86_jump_code (buf, tramp);
681 mono_arch_flush_icache (buf, buf - code);
682 g_assert (buf - code <= tramp_size);
688 mono_arch_create_monitor_exit_trampoline (void)
693 return mono_arch_create_monitor_exit_trampoline_full (&code_size, &ji, FALSE);
697 mono_arch_create_monitor_exit_trampoline_full (guint32 *code_size, MonoJumpInfo **ji, gboolean aot)
699 guint8 *tramp = mono_get_trampoline_code (MONO_TRAMPOLINE_MONITOR_EXIT);
701 guint8 *jump_obj_null, *jump_have_waiters;
704 int owner_offset, nest_offset, entry_count_offset;
706 g_assert (MONO_ARCH_MONITOR_OBJECT_REG == X86_EAX);
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);
718 code = buf = mono_global_codeman_reserve (tramp_size);
720 if (mono_thread_get_tls_offset () != -1) {
721 /* MonoObject* obj is in EAX */
723 x86_test_reg_reg (buf, X86_EAX, X86_EAX);
724 /* if yes, jump to actual trampoline */
726 x86_branch8 (buf, X86_CC_Z, -1, 1);
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 */
734 x86_branch8 (buf, X86_CC_NZ, -1, 1);
735 /* if yes, just return */
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 */
748 x86_branch8 (buf, X86_CC_Z, -1, 1);
749 /* if not, just return */
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 */
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);
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);
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);
780 /* push obj and jump to the actual trampoline */
781 x86_push_reg (buf, X86_EAX);
782 x86_jump_code (buf, tramp);
785 mono_arch_flush_icache (buf, buf - code);
786 g_assert (buf - code <= tramp_size);
792 mono_arch_create_monitor_enter_trampoline (void)
794 g_assert_not_reached ();
799 mono_arch_create_monitor_exit_trampoline (void)
801 g_assert_not_reached ();
807 mono_arch_invalidate_method (MonoJitInfo *ji, void *func, gpointer func_arg)
809 /* FIXME: This is not thread safe */
810 guint8 *code = ji->code_start;
812 x86_push_imm (code, func_arg);
813 x86_call_code (code, (guint8*)func);