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_get_llvm_imt_trampoline (MonoDomain *domain, MonoMethod *m, int vt_offset)
90 start = code = mono_domain_code_reserve (domain, buf_len);
92 this_offset = mono_x86_get_this_arg_offset (NULL, mono_method_signature (m));
95 x86_mov_reg_imm (code, MONO_ARCH_IMT_REG, m);
97 x86_mov_reg_membase (code, X86_EAX, X86_ESP, this_offset + 4, 4);
98 /* Load vtable address */
99 x86_mov_reg_membase (code, X86_EAX, X86_EAX, 0, 4);
100 x86_jump_membase (code, X86_EAX, vt_offset);
102 g_assert ((code - start) < buf_len);
104 mono_arch_flush_icache (start, code - start);
110 mono_arch_patch_callsite (guint8 *method_start, guint8 *orig_code, guint8 *addr)
114 gboolean can_write = mono_breakpoint_clean_code (method_start, orig_code, 8, buf, sizeof (buf));
117 if (mono_running_on_valgrind ())
120 /* go to the start of the call instruction
122 * address_byte = (m << 6) | (o << 3) | reg
123 * call opcode: 0xff address_byte displacement
129 if ((code [1] == 0xe8)) {
131 InterlockedExchange ((gint32*)(orig_code + 2), (guint)addr - ((guint)orig_code + 1) - 5);
133 #ifdef HAVE_VALGRIND_MEMCHECK_H
134 /* Tell valgrind to recompile the patched code */
135 //VALGRIND_DISCARD_TRANSLATIONS (code + 2, code + 6);
138 } else if (code [1] == 0xe9) {
139 /* A PLT entry: jmp <DISP> */
141 InterlockedExchange ((gint32*)(orig_code + 2), (guint)addr - ((guint)orig_code + 1) - 5);
143 printf ("Invalid trampoline sequence: %x %x %x %x %x %x %x\n", code [0], code [1], code [2], code [3],
144 code [4], code [5], code [6]);
145 g_assert_not_reached ();
150 mono_arch_patch_plt_entry (guint8 *code, gpointer *got, mgreg_t *regs, guint8 *addr)
152 /* A PLT entry: jmp <DISP> */
153 g_assert (code [0] == 0xe9);
155 if (!mono_running_on_valgrind ())
156 InterlockedExchange ((gint32*)(code + 1), (guint)addr - (guint)code - 5);
160 mono_arch_nullify_class_init_trampoline (guint8 *code, mgreg_t *regs)
163 gboolean can_write = mono_breakpoint_clean_code (NULL, code, 6, buf, sizeof (buf));
169 if (code [0] == 0xe8) {
170 if (!mono_running_on_valgrind ()) {
173 * Thread safe code patching using the algorithm from the paper
174 * 'Practicing JUDO: Java Under Dynamic Optimizations'
177 * First atomically change the the first 2 bytes of the call to a
181 InterlockedExchange ((gint32*)code, ops);
183 /* Then change the other bytes to a nop */
188 /* Then atomically change the first 4 bytes to a nop as well */
190 InterlockedExchange ((gint32*)code, ops);
191 #ifdef HAVE_VALGRIND_MEMCHECK_H
192 /* FIXME: the calltree skin trips on the self modifying code above */
194 /* Tell valgrind to recompile the patched code */
195 //VALGRIND_DISCARD_TRANSLATIONS (code, code + 8);
198 } else if (code [0] == 0x90 || code [0] == 0xeb) {
199 /* Already changed by another thread */
201 } else if ((code [-1] == 0xff) && (x86_modrm_reg (code [0]) == 0x2)) {
202 /* call *<OFFSET>(<REG>) -> Call made from AOT code */
203 gpointer *vtable_slot;
205 vtable_slot = mono_get_vcall_slot_addr (code + 5, regs);
206 g_assert (vtable_slot);
208 *vtable_slot = nullified_class_init_trampoline;
210 printf ("Invalid trampoline sequence: %x %x %x %x %x %x %x\n", code [0], code [1], code [2], code [3],
211 code [4], code [5], code [6]);
212 g_assert_not_reached ();
217 mono_arch_nullify_plt_entry (guint8 *code, mgreg_t *regs)
219 if (!mono_running_on_valgrind ()) {
223 InterlockedExchange ((gint32*)code, ops);
225 /* Then change the other bytes to a nop */
230 /* Change the first byte to a nop */
232 InterlockedExchange ((gint32*)code, ops);
237 mono_arch_create_trampoline_code (MonoTrampolineType tramp_type)
239 guint8 *buf, *code, *tramp;
240 int pushed_args, pushed_args_caller_saved;
242 code = buf = mono_global_codeman_reserve (256);
244 /* Note that there is a single argument to the trampoline
245 * and it is stored at: esp + pushed_args * sizeof (gpointer)
246 * the ret address is at: esp + (pushed_args + 1) * sizeof (gpointer)
249 /* Put all registers into an array on the stack
250 * If this code is changed, make sure to update the offset value in
251 * mono_arch_find_this_argument () in mini-x86.c.
253 x86_push_reg (buf, X86_EDI);
254 x86_push_reg (buf, X86_ESI);
255 x86_push_reg (buf, X86_EBP);
256 x86_push_reg (buf, X86_ESP);
257 x86_push_reg (buf, X86_EBX);
258 x86_push_reg (buf, X86_EDX);
259 x86_push_reg (buf, X86_ECX);
260 x86_push_reg (buf, X86_EAX);
262 pushed_args_caller_saved = pushed_args = 8;
264 /* Align stack on apple */
265 x86_alu_reg_imm (buf, X86_SUB, X86_ESP, 4);
271 /* save the IP (caller ip) */
272 if (tramp_type == MONO_TRAMPOLINE_JUMP)
273 x86_push_imm (buf, 0);
275 x86_push_membase (buf, X86_ESP, (pushed_args + 1) * sizeof (gpointer));
279 x86_push_reg (buf, X86_EBP);
280 x86_push_reg (buf, X86_ESI);
281 x86_push_reg (buf, X86_EDI);
282 x86_push_reg (buf, X86_EBX);
287 x86_push_reg (buf, X86_ESP);
288 /* Adjust ESP so it points to the previous frame */
289 x86_alu_membase_imm (buf, X86_ADD, X86_ESP, 0, (pushed_args + 2) * 4);
293 /* save method info */
294 if ((tramp_type == MONO_TRAMPOLINE_JIT) || (tramp_type == MONO_TRAMPOLINE_JUMP))
295 x86_push_membase (buf, X86_ESP, pushed_args * sizeof (gpointer));
297 x86_push_imm (buf, 0);
301 /* On apple, the stack is correctly aligned to 16 bytes because pushed_args is
302 * 16 and there is the extra trampoline arg + the return ip pushed by call
303 * FIXME: Note that if an exception happens while some args are pushed
304 * on the stack, the stack will be misaligned.
306 g_assert (pushed_args == 16);
308 /* get the address of lmf for the current thread */
309 x86_call_code (buf, mono_get_lmf_addr);
311 x86_push_reg (buf, X86_EAX);
312 /* push *lfm (previous_lmf) */
313 x86_push_membase (buf, X86_EAX, 0);
314 /* Signal to mono_arch_find_jit_info () that this is a trampoline frame */
315 x86_alu_membase_imm (buf, X86_ADD, X86_ESP, 0, 1);
317 x86_mov_membase_reg (buf, X86_EAX, 0, X86_ESP, 4);
322 /* starting the call sequence */
324 /* FIXME: Push the trampoline address */
325 x86_push_imm (buf, 0);
329 /* push the method info */
330 x86_push_membase (buf, X86_ESP, pushed_args * sizeof (gpointer));
334 /* push the return address onto the stack */
335 if (tramp_type == MONO_TRAMPOLINE_JUMP)
336 x86_push_imm (buf, 0);
338 x86_push_membase (buf, X86_ESP, (pushed_args + 1) * sizeof (gpointer));
340 /* push the address of the register array */
341 x86_lea_membase (buf, X86_EAX, X86_ESP, (pushed_args - 8) * sizeof (gpointer));
342 x86_push_reg (buf, X86_EAX);
347 /* check the stack is aligned after the ret ip is pushed */
348 /*x86_mov_reg_reg (buf, X86_EDX, X86_ESP, 4);
349 x86_alu_reg_imm (buf, X86_AND, X86_EDX, 15);
350 x86_alu_reg_imm (buf, X86_CMP, X86_EDX, 0);
351 x86_branch_disp (buf, X86_CC_Z, 3, FALSE);
352 x86_breakpoint (buf);*/
355 tramp = (guint8*)mono_get_trampoline_func (tramp_type);
356 x86_call_code (buf, tramp);
358 x86_alu_reg_imm (buf, X86_ADD, X86_ESP, 4*4);
362 /* Check for thread interruption */
363 /* This is not perf critical code so no need to check the interrupt flag */
364 /* Align the stack on osx */
365 x86_alu_reg_imm (buf, X86_SUB, X86_ESP, 3 * 4);
366 x86_push_reg (buf, X86_EAX);
367 x86_call_code (buf, (guint8*)mono_thread_force_interruption_checkpoint);
368 x86_pop_reg (buf, X86_EAX);
369 x86_alu_reg_imm (buf, X86_ADD, X86_ESP, 3 * 4);
373 /* ebx = previous_lmf */
374 x86_pop_reg (buf, X86_EBX);
376 x86_alu_reg_imm (buf, X86_SUB, X86_EBX, 1);
379 x86_pop_reg (buf, X86_EDI);
382 /* *(lmf) = previous_lmf */
383 x86_mov_membase_reg (buf, X86_EDI, 0, X86_EBX, 4);
385 /* discard method info */
386 x86_pop_reg (buf, X86_ESI);
390 x86_pop_reg (buf, X86_ESI);
393 /* restore caller saved regs */
394 x86_pop_reg (buf, X86_EBX);
395 x86_pop_reg (buf, X86_EDI);
396 x86_pop_reg (buf, X86_ESI);
397 x86_pop_reg (buf, X86_EBP);
401 /* discard save IP */
402 x86_alu_reg_imm (buf, X86_ADD, X86_ESP, 4);
405 /* restore LMF end */
407 if (!MONO_TRAMPOLINE_TYPE_MUST_RETURN (tramp_type)) {
409 * Overwrite the method ptr with the address we need to jump to,
412 x86_mov_membase_reg (buf, X86_ESP, pushed_args * sizeof (gpointer), X86_EAX, 4);
415 /* Restore caller saved registers */
416 x86_mov_reg_membase (buf, X86_ECX, X86_ESP, (pushed_args - pushed_args_caller_saved + X86_ECX) * 4, 4);
417 x86_mov_reg_membase (buf, X86_EDX, X86_ESP, (pushed_args - pushed_args_caller_saved + X86_EDX) * 4, 4);
418 if ((tramp_type == MONO_TRAMPOLINE_RESTORE_STACK_PROT) || (tramp_type == MONO_TRAMPOLINE_AOT_PLT))
419 x86_mov_reg_membase (buf, X86_EAX, X86_ESP, (pushed_args - pushed_args_caller_saved + X86_EAX) * 4, 4);
421 if (!MONO_TRAMPOLINE_TYPE_MUST_RETURN (tramp_type)) {
422 /* Pop saved reg array + stack align */
423 x86_alu_reg_imm (buf, X86_ADD, X86_ESP, 9 * 4);
425 g_assert (pushed_args == 0);
427 /* Pop saved reg array + stack align + method ptr */
428 x86_alu_reg_imm (buf, X86_ADD, X86_ESP, 10 * 4);
431 /* We've popped one more stack item than we've pushed (the
432 method ptr argument), so we must end up at -1. */
433 g_assert (pushed_args == -1);
438 g_assert ((buf - code) <= 256);
440 if (tramp_type == MONO_TRAMPOLINE_CLASS_INIT) {
441 /* Initialize the nullified class init trampoline used in the AOT case */
442 nullified_class_init_trampoline = buf = mono_global_codeman_reserve (16);
449 #define TRAMPOLINE_SIZE 10
452 mono_arch_create_specific_trampoline (gpointer arg1, MonoTrampolineType tramp_type, MonoDomain *domain, guint32 *code_len)
454 guint8 *code, *buf, *tramp;
456 tramp = mono_get_trampoline_code (tramp_type);
458 code = buf = mono_domain_code_reserve_align (domain, TRAMPOLINE_SIZE, 4);
460 x86_push_imm (buf, arg1);
461 x86_jump_code (buf, tramp);
462 g_assert ((buf - code) <= TRAMPOLINE_SIZE);
464 mono_arch_flush_icache (code, buf - code);
467 *code_len = buf - code;
473 mono_arch_create_rgctx_lazy_fetch_trampoline (guint32 slot)
477 guint8 **rgctx_null_jumps;
483 mrgctx = MONO_RGCTX_SLOT_IS_MRGCTX (slot);
484 index = MONO_RGCTX_SLOT_INDEX (slot);
486 index += MONO_SIZEOF_METHOD_RUNTIME_GENERIC_CONTEXT / sizeof (gpointer);
487 for (depth = 0; ; ++depth) {
488 int size = mono_class_rgctx_get_array_size (depth, mrgctx);
490 if (index < size - 1)
495 tramp_size = 36 + 6 * depth;
497 code = buf = mono_global_codeman_reserve (tramp_size);
499 rgctx_null_jumps = g_malloc (sizeof (guint8*) * (depth + 2));
501 /* load vtable/mrgctx ptr */
502 x86_mov_reg_membase (buf, X86_EAX, X86_ESP, 4, 4);
504 /* load rgctx ptr from vtable */
505 x86_mov_reg_membase (buf, X86_EAX, X86_EAX, G_STRUCT_OFFSET (MonoVTable, runtime_generic_context), 4);
506 /* is the rgctx ptr null? */
507 x86_test_reg_reg (buf, X86_EAX, X86_EAX);
508 /* if yes, jump to actual trampoline */
509 rgctx_null_jumps [0] = buf;
510 x86_branch8 (buf, X86_CC_Z, -1, 1);
513 for (i = 0; i < depth; ++i) {
514 /* load ptr to next array */
515 if (mrgctx && i == 0)
516 x86_mov_reg_membase (buf, X86_EAX, X86_EAX, MONO_SIZEOF_METHOD_RUNTIME_GENERIC_CONTEXT, 4);
518 x86_mov_reg_membase (buf, X86_EAX, X86_EAX, 0, 4);
519 /* is the ptr null? */
520 x86_test_reg_reg (buf, X86_EAX, X86_EAX);
521 /* if yes, jump to actual trampoline */
522 rgctx_null_jumps [i + 1] = buf;
523 x86_branch8 (buf, X86_CC_Z, -1, 1);
527 x86_mov_reg_membase (buf, X86_EAX, X86_EAX, sizeof (gpointer) * (index + 1), 4);
528 /* is the slot null? */
529 x86_test_reg_reg (buf, X86_EAX, X86_EAX);
530 /* if yes, jump to actual trampoline */
531 rgctx_null_jumps [depth + 1] = buf;
532 x86_branch8 (buf, X86_CC_Z, -1, 1);
533 /* otherwise return */
536 for (i = mrgctx ? 1 : 0; i <= depth + 1; ++i)
537 x86_patch (rgctx_null_jumps [i], buf);
539 g_free (rgctx_null_jumps);
541 x86_mov_reg_membase (buf, MONO_ARCH_VTABLE_REG, X86_ESP, 4, 4);
543 tramp = mono_arch_create_specific_trampoline (GUINT_TO_POINTER (slot), MONO_TRAMPOLINE_RGCTX_LAZY_FETCH, mono_get_root_domain (), NULL);
545 /* jump to the actual trampoline */
546 x86_jump_code (buf, tramp);
548 mono_arch_flush_icache (code, buf - code);
550 g_assert (buf - code <= tramp_size);
556 mono_arch_create_generic_class_init_trampoline (void)
560 static int byte_offset = -1;
561 static guint8 bitmask;
567 code = buf = mono_global_codeman_reserve (tramp_size);
570 mono_marshal_find_bitfield_offset (MonoVTable, initialized, &byte_offset, &bitmask);
572 x86_test_membase_imm (code, MONO_ARCH_VTABLE_REG, byte_offset, bitmask);
574 x86_branch8 (code, X86_CC_Z, -1, 1);
578 x86_patch (jump, code);
580 /* Push the vtable so the stack is the same as in a specific trampoline */
581 x86_push_reg (code, MONO_ARCH_VTABLE_REG);
583 tramp = mono_get_trampoline_code (MONO_TRAMPOLINE_GENERIC_CLASS_INIT);
585 /* jump to the actual trampoline */
586 x86_jump_code (code, tramp);
588 mono_arch_flush_icache (code, code - buf);
590 g_assert (code - buf <= tramp_size);
595 #ifdef MONO_ARCH_MONITOR_OBJECT_REG
597 * The code produced by this trampoline is equivalent to this:
600 * if (obj->synchronisation) {
601 * if (obj->synchronisation->owner == 0) {
602 * if (cmpxch (&obj->synchronisation->owner, TID, 0) == 0)
605 * if (obj->synchronisation->owner == TID) {
606 * ++obj->synchronisation->nest;
611 * return full_monitor_enter ();
615 mono_arch_create_monitor_enter_trampoline (void)
620 return mono_arch_create_monitor_enter_trampoline_full (&code_size, &ji, FALSE);
624 mono_arch_create_monitor_enter_trampoline_full (guint32 *code_size, MonoJumpInfo **ji, gboolean aot)
626 guint8 *tramp = mono_get_trampoline_code (MONO_TRAMPOLINE_MONITOR_ENTER);
628 guint8 *jump_obj_null, *jump_sync_null, *jump_other_owner, *jump_cmpxchg_failed, *jump_tid;
630 int owner_offset, nest_offset, dummy;
632 g_assert (MONO_ARCH_MONITOR_OBJECT_REG == X86_EAX);
634 mono_monitor_threads_sync_members_offset (&owner_offset, &nest_offset, &dummy);
635 g_assert (MONO_THREADS_SYNC_MEMBER_SIZE (owner_offset) == sizeof (gpointer));
636 g_assert (MONO_THREADS_SYNC_MEMBER_SIZE (nest_offset) == sizeof (guint32));
637 owner_offset = MONO_THREADS_SYNC_MEMBER_OFFSET (owner_offset);
638 nest_offset = MONO_THREADS_SYNC_MEMBER_OFFSET (nest_offset);
642 code = buf = mono_global_codeman_reserve (tramp_size);
644 if (mono_thread_get_tls_offset () != -1) {
645 /* MonoObject* obj is in EAX */
647 x86_test_reg_reg (buf, X86_EAX, X86_EAX);
648 /* if yes, jump to actual trampoline */
650 x86_branch8 (buf, X86_CC_Z, -1, 1);
652 /* load obj->synchronization to ECX */
653 x86_mov_reg_membase (buf, X86_ECX, X86_EAX, G_STRUCT_OFFSET (MonoObject, synchronisation), 4);
654 /* is synchronization null? */
655 x86_test_reg_reg (buf, X86_ECX, X86_ECX);
656 /* if yes, jump to actual trampoline */
657 jump_sync_null = buf;
658 x86_branch8 (buf, X86_CC_Z, -1, 1);
660 /* load MonoInternalThread* into EDX */
661 buf = mono_x86_emit_tls_get (buf, X86_EDX, mono_thread_get_tls_offset ());
662 /* load TID into EDX */
663 x86_mov_reg_membase (buf, X86_EDX, X86_EDX, G_STRUCT_OFFSET (MonoInternalThread, tid), 4);
665 /* is synchronization->owner null? */
666 x86_alu_membase_imm (buf, X86_CMP, X86_ECX, owner_offset, 0);
667 /* if not, jump to next case */
669 x86_branch8 (buf, X86_CC_NZ, -1, 1);
671 /* if yes, try a compare-exchange with the TID */
672 /* free up register EAX, needed for the zero */
673 x86_push_reg (buf, X86_EAX);
675 x86_alu_reg_reg (buf, X86_XOR, X86_EAX, X86_EAX);
676 /* compare and exchange */
677 x86_prefix (buf, X86_LOCK_PREFIX);
678 x86_cmpxchg_membase_reg (buf, X86_ECX, owner_offset, X86_EDX);
679 /* if not successful, jump to actual trampoline */
680 jump_cmpxchg_failed = buf;
681 x86_branch8 (buf, X86_CC_NZ, -1, 1);
682 /* if successful, pop and return */
683 x86_pop_reg (buf, X86_EAX);
686 /* next case: synchronization->owner is not null */
687 x86_patch (jump_tid, buf);
688 /* is synchronization->owner == TID? */
689 x86_alu_membase_reg (buf, X86_CMP, X86_ECX, owner_offset, X86_EDX);
690 /* if not, jump to actual trampoline */
691 jump_other_owner = buf;
692 x86_branch8 (buf, X86_CC_NZ, -1, 1);
693 /* if yes, increment nest */
694 x86_inc_membase (buf, X86_ECX, nest_offset);
699 x86_patch (jump_obj_null, buf);
700 x86_patch (jump_sync_null, buf);
701 x86_patch (jump_other_owner, buf);
702 x86_push_reg (buf, X86_EAX);
703 /* jump to the actual trampoline */
704 x86_patch (jump_cmpxchg_failed, buf);
705 x86_jump_code (buf, tramp);
707 /* push obj and jump to the actual trampoline */
708 x86_push_reg (buf, X86_EAX);
709 x86_jump_code (buf, tramp);
712 mono_arch_flush_icache (buf, buf - code);
713 g_assert (buf - code <= tramp_size);
719 mono_arch_create_monitor_exit_trampoline (void)
724 return mono_arch_create_monitor_exit_trampoline_full (&code_size, &ji, FALSE);
728 mono_arch_create_monitor_exit_trampoline_full (guint32 *code_size, MonoJumpInfo **ji, gboolean aot)
730 guint8 *tramp = mono_get_trampoline_code (MONO_TRAMPOLINE_MONITOR_EXIT);
732 guint8 *jump_obj_null, *jump_have_waiters;
735 int owner_offset, nest_offset, entry_count_offset;
737 g_assert (MONO_ARCH_MONITOR_OBJECT_REG == X86_EAX);
739 mono_monitor_threads_sync_members_offset (&owner_offset, &nest_offset, &entry_count_offset);
740 g_assert (MONO_THREADS_SYNC_MEMBER_SIZE (owner_offset) == sizeof (gpointer));
741 g_assert (MONO_THREADS_SYNC_MEMBER_SIZE (nest_offset) == sizeof (guint32));
742 g_assert (MONO_THREADS_SYNC_MEMBER_SIZE (entry_count_offset) == sizeof (gint32));
743 owner_offset = MONO_THREADS_SYNC_MEMBER_OFFSET (owner_offset);
744 nest_offset = MONO_THREADS_SYNC_MEMBER_OFFSET (nest_offset);
745 entry_count_offset = MONO_THREADS_SYNC_MEMBER_OFFSET (entry_count_offset);
749 code = buf = mono_global_codeman_reserve (tramp_size);
751 if (mono_thread_get_tls_offset () != -1) {
752 /* MonoObject* obj is in EAX */
754 x86_test_reg_reg (buf, X86_EAX, X86_EAX);
755 /* if yes, jump to actual trampoline */
757 x86_branch8 (buf, X86_CC_Z, -1, 1);
759 /* load obj->synchronization to ECX */
760 x86_mov_reg_membase (buf, X86_ECX, X86_EAX, G_STRUCT_OFFSET (MonoObject, synchronisation), 4);
761 /* is synchronization null? */
762 x86_test_reg_reg (buf, X86_ECX, X86_ECX);
763 /* if not, jump to next case */
765 x86_branch8 (buf, X86_CC_NZ, -1, 1);
766 /* if yes, just return */
769 /* next case: synchronization is not null */
770 x86_patch (jump_next, buf);
771 /* load MonoInternalThread* into EDX */
772 buf = mono_x86_emit_tls_get (buf, X86_EDX, mono_thread_get_tls_offset ());
773 /* load TID into EDX */
774 x86_mov_reg_membase (buf, X86_EDX, X86_EDX, G_STRUCT_OFFSET (MonoInternalThread, tid), 4);
775 /* is synchronization->owner == TID */
776 x86_alu_membase_reg (buf, X86_CMP, X86_ECX, owner_offset, X86_EDX);
777 /* if yes, jump to next case */
779 x86_branch8 (buf, X86_CC_Z, -1, 1);
780 /* if not, just return */
783 /* next case: synchronization->owner == TID */
784 x86_patch (jump_next, buf);
785 /* is synchronization->nest == 1 */
786 x86_alu_membase_imm (buf, X86_CMP, X86_ECX, nest_offset, 1);
787 /* if not, jump to next case */
789 x86_branch8 (buf, X86_CC_NZ, -1, 1);
790 /* if yes, is synchronization->entry_count zero? */
791 x86_alu_membase_imm (buf, X86_CMP, X86_ECX, entry_count_offset, 0);
792 /* if not, jump to actual trampoline */
793 jump_have_waiters = buf;
794 x86_branch8 (buf, X86_CC_NZ, -1 , 1);
795 /* if yes, set synchronization->owner to null and return */
796 x86_mov_membase_imm (buf, X86_ECX, owner_offset, 0, 4);
799 /* next case: synchronization->nest is not 1 */
800 x86_patch (jump_next, buf);
801 /* decrease synchronization->nest and return */
802 x86_dec_membase (buf, X86_ECX, nest_offset);
805 /* push obj and jump to the actual trampoline */
806 x86_patch (jump_obj_null, buf);
807 x86_patch (jump_have_waiters, buf);
808 x86_push_reg (buf, X86_EAX);
809 x86_jump_code (buf, tramp);
811 /* push obj and jump to the actual trampoline */
812 x86_push_reg (buf, X86_EAX);
813 x86_jump_code (buf, tramp);
816 mono_arch_flush_icache (buf, buf - code);
817 g_assert (buf - code <= tramp_size);
823 mono_arch_create_monitor_enter_trampoline (void)
825 g_assert_not_reached ();
830 mono_arch_create_monitor_exit_trampoline (void)
832 g_assert_not_reached ();
838 mono_arch_invalidate_method (MonoJitInfo *ji, void *func, gpointer func_arg)
840 /* FIXME: This is not thread safe */
841 guint8 *code = ji->code_start;
843 x86_push_imm (code, func_arg);
844 x86_call_code (code, (guint8*)func);