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);
83 mono_arch_get_llvm_imt_trampoline (MonoDomain *domain, MonoMethod *m, int vt_offset)
91 start = code = mono_domain_code_reserve (domain, buf_len);
93 this_reg = mono_arch_get_this_arg_reg (mono_method_signature (m), NULL, NULL);
96 x86_mov_reg_imm (code, MONO_ARCH_IMT_REG, m);
97 /* Load vtable address */
98 x86_mov_reg_membase (code, X86_EAX, this_reg, 0, 8);
99 x86_jump_membase (code, X86_EAX, vt_offset);
102 g_assert ((code - start) < buf_len);
104 mono_arch_flush_icache (start, code - start);
111 mono_arch_patch_callsite (guint8 *method_start, guint8 *orig_code, guint8 *addr)
115 gboolean can_write = mono_breakpoint_clean_code (method_start, orig_code, 8, buf, sizeof (buf));
118 if (mono_running_on_valgrind ())
121 /* go to the start of the call instruction
123 * address_byte = (m << 6) | (o << 3) | reg
124 * call opcode: 0xff address_byte displacement
130 if ((code [1] == 0xe8)) {
132 InterlockedExchange ((gint32*)(orig_code + 2), (guint)addr - ((guint)orig_code + 1) - 5);
134 #ifdef HAVE_VALGRIND_MEMCHECK_H
135 /* Tell valgrind to recompile the patched code */
136 //VALGRIND_DISCARD_TRANSLATIONS (code + 2, code + 6);
139 } else if (code [1] == 0xe9) {
140 /* A PLT entry: jmp <DISP> */
142 InterlockedExchange ((gint32*)(orig_code + 2), (guint)addr - ((guint)orig_code + 1) - 5);
144 printf ("Invalid trampoline sequence: %x %x %x %x %x %x %x\n", code [0], code [1], code [2], code [3],
145 code [4], code [5], code [6]);
146 g_assert_not_reached ();
151 mono_arch_patch_plt_entry (guint8 *code, gpointer *got, mgreg_t *regs, guint8 *addr)
153 /* A PLT entry: jmp <DISP> */
154 g_assert (code [0] == 0xe9);
156 if (!mono_running_on_valgrind ())
157 InterlockedExchange ((gint32*)(code + 1), (guint)addr - (guint)code - 5);
161 mono_arch_nullify_class_init_trampoline (guint8 *code, mgreg_t *regs)
164 gboolean can_write = mono_breakpoint_clean_code (NULL, code, 6, buf, sizeof (buf));
170 if (code [0] == 0xe8) {
171 if (!mono_running_on_valgrind ()) {
174 * Thread safe code patching using the algorithm from the paper
175 * 'Practicing JUDO: Java Under Dynamic Optimizations'
178 * First atomically change the the first 2 bytes of the call to a
182 InterlockedExchange ((gint32*)code, ops);
184 /* Then change the other bytes to a nop */
189 /* Then atomically change the first 4 bytes to a nop as well */
191 InterlockedExchange ((gint32*)code, ops);
192 #ifdef HAVE_VALGRIND_MEMCHECK_H
193 /* FIXME: the calltree skin trips on the self modifying code above */
195 /* Tell valgrind to recompile the patched code */
196 //VALGRIND_DISCARD_TRANSLATIONS (code, code + 8);
199 } else if (code [0] == 0x90 || code [0] == 0xeb) {
200 /* Already changed by another thread */
202 } else if ((code [-1] == 0xff) && (x86_modrm_reg (code [0]) == 0x2)) {
203 /* call *<OFFSET>(<REG>) -> Call made from AOT code */
204 gpointer *vtable_slot;
206 vtable_slot = mono_get_vcall_slot_addr (code + 5, regs);
207 g_assert (vtable_slot);
209 *vtable_slot = nullified_class_init_trampoline;
211 printf ("Invalid trampoline sequence: %x %x %x %x %x %x %x\n", code [0], code [1], code [2], code [3],
212 code [4], code [5], code [6]);
213 g_assert_not_reached ();
218 mono_arch_nullify_plt_entry (guint8 *code, mgreg_t *regs)
220 if (!mono_running_on_valgrind ()) {
224 InterlockedExchange ((gint32*)code, ops);
226 /* Then change the other bytes to a nop */
231 /* Change the first byte to a nop */
233 InterlockedExchange ((gint32*)code, ops);
238 mono_arch_create_trampoline_code (MonoTrampolineType tramp_type)
240 guint8 *buf, *code, *tramp;
241 int pushed_args, pushed_args_caller_saved;
243 code = buf = mono_global_codeman_reserve (256);
245 /* Note that there is a single argument to the trampoline
246 * and it is stored at: esp + pushed_args * sizeof (gpointer)
247 * the ret address is at: esp + (pushed_args + 1) * sizeof (gpointer)
250 /* Put all registers into an array on the stack
251 * If this code is changed, make sure to update the offset value in
252 * mono_arch_find_this_argument () in mini-x86.c.
254 x86_push_reg (buf, X86_EDI);
255 x86_push_reg (buf, X86_ESI);
256 x86_push_reg (buf, X86_EBP);
257 x86_push_reg (buf, X86_ESP);
258 x86_push_reg (buf, X86_EBX);
259 x86_push_reg (buf, X86_EDX);
260 x86_push_reg (buf, X86_ECX);
261 x86_push_reg (buf, X86_EAX);
263 pushed_args_caller_saved = pushed_args = 8;
265 /* Align stack on apple */
266 x86_alu_reg_imm (buf, X86_SUB, X86_ESP, 4);
272 /* save the IP (caller ip) */
273 if (tramp_type == MONO_TRAMPOLINE_JUMP)
274 x86_push_imm (buf, 0);
276 x86_push_membase (buf, X86_ESP, (pushed_args + 1) * sizeof (gpointer));
280 x86_push_reg (buf, X86_EBP);
281 x86_push_reg (buf, X86_ESI);
282 x86_push_reg (buf, X86_EDI);
283 x86_push_reg (buf, X86_EBX);
288 x86_push_reg (buf, X86_ESP);
289 /* Adjust ESP so it points to the previous frame */
290 x86_alu_membase_imm (buf, X86_ADD, X86_ESP, 0, (pushed_args + 2) * 4);
294 /* save method info */
295 if ((tramp_type == MONO_TRAMPOLINE_JIT) || (tramp_type == MONO_TRAMPOLINE_JUMP))
296 x86_push_membase (buf, X86_ESP, pushed_args * sizeof (gpointer));
298 x86_push_imm (buf, 0);
302 /* On apple, the stack is correctly aligned to 16 bytes because pushed_args is
303 * 16 and there is the extra trampoline arg + the return ip pushed by call
304 * FIXME: Note that if an exception happens while some args are pushed
305 * on the stack, the stack will be misaligned.
307 g_assert (pushed_args == 16);
309 /* get the address of lmf for the current thread */
310 x86_call_code (buf, mono_get_lmf_addr);
312 x86_push_reg (buf, X86_EAX);
313 /* push *lfm (previous_lmf) */
314 x86_push_membase (buf, X86_EAX, 0);
315 /* Signal to mono_arch_find_jit_info () that this is a trampoline frame */
316 x86_alu_membase_imm (buf, X86_ADD, X86_ESP, 0, 1);
318 x86_mov_membase_reg (buf, X86_EAX, 0, X86_ESP, 4);
323 /* starting the call sequence */
325 /* FIXME: Push the trampoline address */
326 x86_push_imm (buf, 0);
330 /* push the method info */
331 x86_push_membase (buf, X86_ESP, pushed_args * sizeof (gpointer));
335 /* push the return address onto the stack */
336 if (tramp_type == MONO_TRAMPOLINE_JUMP)
337 x86_push_imm (buf, 0);
339 x86_push_membase (buf, X86_ESP, (pushed_args + 1) * sizeof (gpointer));
341 /* push the address of the register array */
342 x86_lea_membase (buf, X86_EAX, X86_ESP, (pushed_args - 8) * sizeof (gpointer));
343 x86_push_reg (buf, X86_EAX);
348 /* check the stack is aligned after the ret ip is pushed */
349 /*x86_mov_reg_reg (buf, X86_EDX, X86_ESP, 4);
350 x86_alu_reg_imm (buf, X86_AND, X86_EDX, 15);
351 x86_alu_reg_imm (buf, X86_CMP, X86_EDX, 0);
352 x86_branch_disp (buf, X86_CC_Z, 3, FALSE);
353 x86_breakpoint (buf);*/
356 tramp = (guint8*)mono_get_trampoline_func (tramp_type);
357 x86_call_code (buf, tramp);
359 x86_alu_reg_imm (buf, X86_ADD, X86_ESP, 4*4);
363 /* Check for thread interruption */
364 /* This is not perf critical code so no need to check the interrupt flag */
365 /* Align the stack on osx */
366 x86_alu_reg_imm (buf, X86_SUB, X86_ESP, 3 * 4);
367 x86_push_reg (buf, X86_EAX);
368 x86_call_code (buf, (guint8*)mono_thread_force_interruption_checkpoint);
369 x86_pop_reg (buf, X86_EAX);
370 x86_alu_reg_imm (buf, X86_ADD, X86_ESP, 3 * 4);
374 /* ebx = previous_lmf */
375 x86_pop_reg (buf, X86_EBX);
377 x86_alu_reg_imm (buf, X86_SUB, X86_EBX, 1);
380 x86_pop_reg (buf, X86_EDI);
383 /* *(lmf) = previous_lmf */
384 x86_mov_membase_reg (buf, X86_EDI, 0, X86_EBX, 4);
386 /* discard method info */
387 x86_pop_reg (buf, X86_ESI);
391 x86_pop_reg (buf, X86_ESI);
394 /* restore caller saved regs */
395 x86_pop_reg (buf, X86_EBX);
396 x86_pop_reg (buf, X86_EDI);
397 x86_pop_reg (buf, X86_ESI);
398 x86_pop_reg (buf, X86_EBP);
402 /* discard save IP */
403 x86_alu_reg_imm (buf, X86_ADD, X86_ESP, 4);
406 /* restore LMF end */
408 if (!MONO_TRAMPOLINE_TYPE_MUST_RETURN (tramp_type)) {
410 * Overwrite the method ptr with the address we need to jump to,
413 x86_mov_membase_reg (buf, X86_ESP, pushed_args * sizeof (gpointer), X86_EAX, 4);
416 /* Restore caller saved registers */
417 x86_mov_reg_membase (buf, X86_ECX, X86_ESP, (pushed_args - pushed_args_caller_saved + X86_ECX) * 4, 4);
418 x86_mov_reg_membase (buf, X86_EDX, X86_ESP, (pushed_args - pushed_args_caller_saved + X86_EDX) * 4, 4);
419 if ((tramp_type == MONO_TRAMPOLINE_RESTORE_STACK_PROT) || (tramp_type == MONO_TRAMPOLINE_AOT_PLT))
420 x86_mov_reg_membase (buf, X86_EAX, X86_ESP, (pushed_args - pushed_args_caller_saved + X86_EAX) * 4, 4);
422 if (!MONO_TRAMPOLINE_TYPE_MUST_RETURN (tramp_type)) {
423 /* Pop saved reg array + stack align */
424 x86_alu_reg_imm (buf, X86_ADD, X86_ESP, 9 * 4);
426 g_assert (pushed_args == 0);
428 /* Pop saved reg array + stack align + method ptr */
429 x86_alu_reg_imm (buf, X86_ADD, X86_ESP, 10 * 4);
432 /* We've popped one more stack item than we've pushed (the
433 method ptr argument), so we must end up at -1. */
434 g_assert (pushed_args == -1);
439 g_assert ((buf - code) <= 256);
441 if (tramp_type == MONO_TRAMPOLINE_CLASS_INIT) {
442 /* Initialize the nullified class init trampoline used in the AOT case */
443 nullified_class_init_trampoline = buf = mono_global_codeman_reserve (16);
450 #define TRAMPOLINE_SIZE 10
453 mono_arch_create_specific_trampoline (gpointer arg1, MonoTrampolineType tramp_type, MonoDomain *domain, guint32 *code_len)
455 guint8 *code, *buf, *tramp;
457 tramp = mono_get_trampoline_code (tramp_type);
459 code = buf = mono_domain_code_reserve_align (domain, TRAMPOLINE_SIZE, 4);
461 x86_push_imm (buf, arg1);
462 x86_jump_code (buf, tramp);
463 g_assert ((buf - code) <= TRAMPOLINE_SIZE);
465 mono_arch_flush_icache (code, buf - code);
468 *code_len = buf - code;
474 mono_arch_create_rgctx_lazy_fetch_trampoline (guint32 slot)
478 guint8 **rgctx_null_jumps;
484 mrgctx = MONO_RGCTX_SLOT_IS_MRGCTX (slot);
485 index = MONO_RGCTX_SLOT_INDEX (slot);
487 index += MONO_SIZEOF_METHOD_RUNTIME_GENERIC_CONTEXT / sizeof (gpointer);
488 for (depth = 0; ; ++depth) {
489 int size = mono_class_rgctx_get_array_size (depth, mrgctx);
491 if (index < size - 1)
496 tramp_size = 36 + 6 * depth;
498 code = buf = mono_global_codeman_reserve (tramp_size);
500 rgctx_null_jumps = g_malloc (sizeof (guint8*) * (depth + 2));
502 /* load vtable/mrgctx ptr */
503 x86_mov_reg_membase (buf, X86_EAX, X86_ESP, 4, 4);
505 /* load rgctx ptr from vtable */
506 x86_mov_reg_membase (buf, X86_EAX, X86_EAX, G_STRUCT_OFFSET (MonoVTable, runtime_generic_context), 4);
507 /* is the rgctx ptr null? */
508 x86_test_reg_reg (buf, X86_EAX, X86_EAX);
509 /* if yes, jump to actual trampoline */
510 rgctx_null_jumps [0] = buf;
511 x86_branch8 (buf, X86_CC_Z, -1, 1);
514 for (i = 0; i < depth; ++i) {
515 /* load ptr to next array */
516 if (mrgctx && i == 0)
517 x86_mov_reg_membase (buf, X86_EAX, X86_EAX, MONO_SIZEOF_METHOD_RUNTIME_GENERIC_CONTEXT, 4);
519 x86_mov_reg_membase (buf, X86_EAX, X86_EAX, 0, 4);
520 /* is the ptr null? */
521 x86_test_reg_reg (buf, X86_EAX, X86_EAX);
522 /* if yes, jump to actual trampoline */
523 rgctx_null_jumps [i + 1] = buf;
524 x86_branch8 (buf, X86_CC_Z, -1, 1);
528 x86_mov_reg_membase (buf, X86_EAX, X86_EAX, sizeof (gpointer) * (index + 1), 4);
529 /* is the slot null? */
530 x86_test_reg_reg (buf, X86_EAX, X86_EAX);
531 /* if yes, jump to actual trampoline */
532 rgctx_null_jumps [depth + 1] = buf;
533 x86_branch8 (buf, X86_CC_Z, -1, 1);
534 /* otherwise return */
537 for (i = mrgctx ? 1 : 0; i <= depth + 1; ++i)
538 x86_patch (rgctx_null_jumps [i], buf);
540 g_free (rgctx_null_jumps);
542 x86_mov_reg_membase (buf, MONO_ARCH_VTABLE_REG, X86_ESP, 4, 4);
544 tramp = mono_arch_create_specific_trampoline (GUINT_TO_POINTER (slot), MONO_TRAMPOLINE_RGCTX_LAZY_FETCH, mono_get_root_domain (), NULL);
546 /* jump to the actual trampoline */
547 x86_jump_code (buf, tramp);
549 mono_arch_flush_icache (code, buf - code);
551 g_assert (buf - code <= tramp_size);
557 mono_arch_create_generic_class_init_trampoline (void)
561 static int byte_offset = -1;
562 static guint8 bitmask;
568 code = buf = mono_global_codeman_reserve (tramp_size);
571 mono_marshal_find_bitfield_offset (MonoVTable, initialized, &byte_offset, &bitmask);
573 x86_test_membase_imm (code, MONO_ARCH_VTABLE_REG, byte_offset, bitmask);
575 x86_branch8 (code, X86_CC_Z, -1, 1);
579 x86_patch (jump, code);
581 /* Push the vtable so the stack is the same as in a specific trampoline */
582 x86_push_reg (code, MONO_ARCH_VTABLE_REG);
584 tramp = mono_get_trampoline_code (MONO_TRAMPOLINE_GENERIC_CLASS_INIT);
586 /* jump to the actual trampoline */
587 x86_jump_code (code, tramp);
589 mono_arch_flush_icache (code, code - buf);
591 g_assert (code - buf <= tramp_size);
596 #ifdef MONO_ARCH_MONITOR_OBJECT_REG
598 * The code produced by this trampoline is equivalent to this:
601 * if (obj->synchronisation) {
602 * if (obj->synchronisation->owner == 0) {
603 * if (cmpxch (&obj->synchronisation->owner, TID, 0) == 0)
606 * if (obj->synchronisation->owner == TID) {
607 * ++obj->synchronisation->nest;
612 * return full_monitor_enter ();
616 mono_arch_create_monitor_enter_trampoline (void)
621 return mono_arch_create_monitor_enter_trampoline_full (&code_size, &ji, FALSE);
625 mono_arch_create_monitor_enter_trampoline_full (guint32 *code_size, MonoJumpInfo **ji, gboolean aot)
627 guint8 *tramp = mono_get_trampoline_code (MONO_TRAMPOLINE_MONITOR_ENTER);
629 guint8 *jump_obj_null, *jump_sync_null, *jump_other_owner, *jump_cmpxchg_failed, *jump_tid;
631 int owner_offset, nest_offset, dummy;
633 g_assert (MONO_ARCH_MONITOR_OBJECT_REG == X86_EAX);
635 mono_monitor_threads_sync_members_offset (&owner_offset, &nest_offset, &dummy);
636 g_assert (MONO_THREADS_SYNC_MEMBER_SIZE (owner_offset) == sizeof (gpointer));
637 g_assert (MONO_THREADS_SYNC_MEMBER_SIZE (nest_offset) == sizeof (guint32));
638 owner_offset = MONO_THREADS_SYNC_MEMBER_OFFSET (owner_offset);
639 nest_offset = MONO_THREADS_SYNC_MEMBER_OFFSET (nest_offset);
643 code = buf = mono_global_codeman_reserve (tramp_size);
645 if (mono_thread_get_tls_offset () != -1) {
646 /* MonoObject* obj is in EAX */
648 x86_test_reg_reg (buf, X86_EAX, X86_EAX);
649 /* if yes, jump to actual trampoline */
651 x86_branch8 (buf, X86_CC_Z, -1, 1);
653 /* load obj->synchronization to ECX */
654 x86_mov_reg_membase (buf, X86_ECX, X86_EAX, G_STRUCT_OFFSET (MonoObject, synchronisation), 4);
655 /* is synchronization null? */
656 x86_test_reg_reg (buf, X86_ECX, X86_ECX);
657 /* if yes, jump to actual trampoline */
658 jump_sync_null = buf;
659 x86_branch8 (buf, X86_CC_Z, -1, 1);
661 /* load MonoInternalThread* into EDX */
662 buf = mono_x86_emit_tls_get (buf, X86_EDX, mono_thread_get_tls_offset ());
663 /* load TID into EDX */
664 x86_mov_reg_membase (buf, X86_EDX, X86_EDX, G_STRUCT_OFFSET (MonoInternalThread, tid), 4);
666 /* is synchronization->owner null? */
667 x86_alu_membase_imm (buf, X86_CMP, X86_ECX, owner_offset, 0);
668 /* if not, jump to next case */
670 x86_branch8 (buf, X86_CC_NZ, -1, 1);
672 /* if yes, try a compare-exchange with the TID */
673 /* free up register EAX, needed for the zero */
674 x86_push_reg (buf, X86_EAX);
676 x86_alu_reg_reg (buf, X86_XOR, X86_EAX, X86_EAX);
677 /* compare and exchange */
678 x86_prefix (buf, X86_LOCK_PREFIX);
679 x86_cmpxchg_membase_reg (buf, X86_ECX, owner_offset, X86_EDX);
680 /* if not successful, jump to actual trampoline */
681 jump_cmpxchg_failed = buf;
682 x86_branch8 (buf, X86_CC_NZ, -1, 1);
683 /* if successful, pop and return */
684 x86_pop_reg (buf, X86_EAX);
687 /* next case: synchronization->owner is not null */
688 x86_patch (jump_tid, buf);
689 /* is synchronization->owner == TID? */
690 x86_alu_membase_reg (buf, X86_CMP, X86_ECX, owner_offset, X86_EDX);
691 /* if not, jump to actual trampoline */
692 jump_other_owner = buf;
693 x86_branch8 (buf, X86_CC_NZ, -1, 1);
694 /* if yes, increment nest */
695 x86_inc_membase (buf, X86_ECX, nest_offset);
700 x86_patch (jump_obj_null, buf);
701 x86_patch (jump_sync_null, buf);
702 x86_patch (jump_other_owner, buf);
703 x86_push_reg (buf, X86_EAX);
704 /* jump to the actual trampoline */
705 x86_patch (jump_cmpxchg_failed, buf);
706 x86_jump_code (buf, tramp);
708 /* push obj and jump to the actual trampoline */
709 x86_push_reg (buf, X86_EAX);
710 x86_jump_code (buf, tramp);
713 mono_arch_flush_icache (buf, buf - code);
714 g_assert (buf - code <= tramp_size);
720 mono_arch_create_monitor_exit_trampoline (void)
725 return mono_arch_create_monitor_exit_trampoline_full (&code_size, &ji, FALSE);
729 mono_arch_create_monitor_exit_trampoline_full (guint32 *code_size, MonoJumpInfo **ji, gboolean aot)
731 guint8 *tramp = mono_get_trampoline_code (MONO_TRAMPOLINE_MONITOR_EXIT);
733 guint8 *jump_obj_null, *jump_have_waiters;
736 int owner_offset, nest_offset, entry_count_offset;
738 g_assert (MONO_ARCH_MONITOR_OBJECT_REG == X86_EAX);
740 mono_monitor_threads_sync_members_offset (&owner_offset, &nest_offset, &entry_count_offset);
741 g_assert (MONO_THREADS_SYNC_MEMBER_SIZE (owner_offset) == sizeof (gpointer));
742 g_assert (MONO_THREADS_SYNC_MEMBER_SIZE (nest_offset) == sizeof (guint32));
743 g_assert (MONO_THREADS_SYNC_MEMBER_SIZE (entry_count_offset) == sizeof (gint32));
744 owner_offset = MONO_THREADS_SYNC_MEMBER_OFFSET (owner_offset);
745 nest_offset = MONO_THREADS_SYNC_MEMBER_OFFSET (nest_offset);
746 entry_count_offset = MONO_THREADS_SYNC_MEMBER_OFFSET (entry_count_offset);
750 code = buf = mono_global_codeman_reserve (tramp_size);
752 if (mono_thread_get_tls_offset () != -1) {
753 /* MonoObject* obj is in EAX */
755 x86_test_reg_reg (buf, X86_EAX, X86_EAX);
756 /* if yes, jump to actual trampoline */
758 x86_branch8 (buf, X86_CC_Z, -1, 1);
760 /* load obj->synchronization to ECX */
761 x86_mov_reg_membase (buf, X86_ECX, X86_EAX, G_STRUCT_OFFSET (MonoObject, synchronisation), 4);
762 /* is synchronization null? */
763 x86_test_reg_reg (buf, X86_ECX, X86_ECX);
764 /* if not, jump to next case */
766 x86_branch8 (buf, X86_CC_NZ, -1, 1);
767 /* if yes, just return */
770 /* next case: synchronization is not null */
771 x86_patch (jump_next, buf);
772 /* load MonoInternalThread* into EDX */
773 buf = mono_x86_emit_tls_get (buf, X86_EDX, mono_thread_get_tls_offset ());
774 /* load TID into EDX */
775 x86_mov_reg_membase (buf, X86_EDX, X86_EDX, G_STRUCT_OFFSET (MonoInternalThread, tid), 4);
776 /* is synchronization->owner == TID */
777 x86_alu_membase_reg (buf, X86_CMP, X86_ECX, owner_offset, X86_EDX);
778 /* if yes, jump to next case */
780 x86_branch8 (buf, X86_CC_Z, -1, 1);
781 /* if not, just return */
784 /* next case: synchronization->owner == TID */
785 x86_patch (jump_next, buf);
786 /* is synchronization->nest == 1 */
787 x86_alu_membase_imm (buf, X86_CMP, X86_ECX, nest_offset, 1);
788 /* if not, jump to next case */
790 x86_branch8 (buf, X86_CC_NZ, -1, 1);
791 /* if yes, is synchronization->entry_count zero? */
792 x86_alu_membase_imm (buf, X86_CMP, X86_ECX, entry_count_offset, 0);
793 /* if not, jump to actual trampoline */
794 jump_have_waiters = buf;
795 x86_branch8 (buf, X86_CC_NZ, -1 , 1);
796 /* if yes, set synchronization->owner to null and return */
797 x86_mov_membase_imm (buf, X86_ECX, owner_offset, 0, 4);
800 /* next case: synchronization->nest is not 1 */
801 x86_patch (jump_next, buf);
802 /* decrease synchronization->nest and return */
803 x86_dec_membase (buf, X86_ECX, nest_offset);
806 /* push obj and jump to the actual trampoline */
807 x86_patch (jump_obj_null, buf);
808 x86_patch (jump_have_waiters, buf);
809 x86_push_reg (buf, X86_EAX);
810 x86_jump_code (buf, tramp);
812 /* push obj and jump to the actual trampoline */
813 x86_push_reg (buf, X86_EAX);
814 x86_jump_code (buf, tramp);
817 mono_arch_flush_icache (buf, buf - code);
818 g_assert (buf - code <= tramp_size);
824 mono_arch_create_monitor_enter_trampoline (void)
826 g_assert_not_reached ();
831 mono_arch_create_monitor_exit_trampoline (void)
833 g_assert_not_reached ();
839 mono_arch_invalidate_method (MonoJitInfo *ji, void *func, gpointer func_arg)
841 /* FIXME: This is not thread safe */
842 guint8 *code = ji->code_start;
844 x86_push_imm (code, func_arg);
845 x86_call_code (code, (guint8*)func);