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 #include <mono/utils/memcheck.h>
27 static guint8* nullified_class_init_trampoline;
30 * mono_arch_get_unbox_trampoline:
31 * @gsctx: the generic sharing context
33 * @addr: pointer to native code for @m
35 * when value type methods are called through the vtable we need to unbox the
36 * this argument. This method returns a pointer to a trampoline which does
37 * unboxing before calling the method
40 mono_arch_get_unbox_trampoline (MonoGenericSharingContext *gsctx, MonoMethod *m, gpointer addr)
44 MonoDomain *domain = mono_domain_get ();
46 start = code = mono_domain_code_reserve (domain, 16);
48 x86_alu_membase_imm (code, X86_ADD, X86_ESP, this_pos, sizeof (MonoObject));
49 x86_jump_code (code, addr);
50 g_assert ((code - start) < 16);
56 mono_arch_get_static_rgctx_trampoline (MonoMethod *m, MonoMethodRuntimeGenericContext *mrgctx, gpointer addr)
61 MonoDomain *domain = mono_domain_get ();
65 start = code = mono_domain_code_reserve (domain, buf_len);
67 x86_mov_reg_imm (code, MONO_ARCH_RGCTX_REG, mrgctx);
68 x86_jump_code (code, addr);
69 g_assert ((code - start) <= buf_len);
71 mono_arch_flush_icache (start, code - start);
77 mono_arch_get_llvm_imt_trampoline (MonoDomain *domain, MonoMethod *m, int vt_offset)
85 start = code = mono_domain_code_reserve (domain, buf_len);
87 this_offset = mono_x86_get_this_arg_offset (NULL, mono_method_signature (m));
90 x86_mov_reg_imm (code, MONO_ARCH_IMT_REG, m);
92 x86_mov_reg_membase (code, X86_EAX, X86_ESP, this_offset + 4, 4);
93 /* Load vtable address */
94 x86_mov_reg_membase (code, X86_EAX, X86_EAX, 0, 4);
95 x86_jump_membase (code, X86_EAX, vt_offset);
97 g_assert ((code - start) < buf_len);
99 mono_arch_flush_icache (start, code - start);
105 mono_arch_patch_callsite (guint8 *method_start, guint8 *orig_code, guint8 *addr)
109 gboolean can_write = mono_breakpoint_clean_code (method_start, orig_code, 8, buf, sizeof (buf));
113 /* go to the start of the call instruction
115 * address_byte = (m << 6) | (o << 3) | reg
116 * call opcode: 0xff address_byte displacement
122 if ((code [1] == 0xe8)) {
124 InterlockedExchange ((gint32*)(orig_code + 2), (guint)addr - ((guint)orig_code + 1) - 5);
126 /* Tell valgrind to recompile the patched code */
127 VALGRIND_DISCARD_TRANSLATIONS (orig_code + 2, 4);
129 } else if (code [1] == 0xe9) {
130 /* A PLT entry: jmp <DISP> */
132 InterlockedExchange ((gint32*)(orig_code + 2), (guint)addr - ((guint)orig_code + 1) - 5);
134 printf ("Invalid trampoline sequence: %x %x %x %x %x %x %x\n", code [0], code [1], code [2], code [3],
135 code [4], code [5], code [6]);
136 g_assert_not_reached ();
141 mono_arch_patch_plt_entry (guint8 *code, gpointer *got, mgreg_t *regs, guint8 *addr)
145 /* Patch the jump table entry used by the plt entry */
147 /* A PLT entry: jmp *<DISP>(%ebx) */
148 g_assert (code [0] == 0xff);
149 g_assert (code [1] == 0xa3);
151 offset = *(guint32*)(code + 2);
154 got = (gpointer*)(gsize) regs [MONO_ARCH_GOT_REG];
155 *(guint8**)((guint8*)got + offset) = addr;
159 mono_arch_nullify_class_init_trampoline (guint8 *code, mgreg_t *regs)
162 gboolean can_write = mono_breakpoint_clean_code (NULL, code, 6, buf, sizeof (buf));
168 if (code [0] == 0xe8) {
169 if (!mono_running_on_valgrind ()) {
172 * Thread safe code patching using the algorithm from the paper
173 * 'Practicing JUDO: Java Under Dynamic Optimizations'
176 * First atomically change the the first 2 bytes of the call to a
180 InterlockedExchange ((gint32*)code, ops);
182 /* Then change the other bytes to a nop */
187 /* Then atomically change the first 4 bytes to a nop as well */
189 InterlockedExchange ((gint32*)code, ops);
190 /* FIXME: the calltree skin trips on the self modifying code above */
192 /* Tell valgrind to recompile the patched code */
193 //VALGRIND_DISCARD_TRANSLATIONS (code, 8);
195 } else if (code [0] == 0x90 || code [0] == 0xeb) {
196 /* Already changed by another thread */
198 } else if ((code [-1] == 0xff) && (x86_modrm_reg (code [0]) == 0x2)) {
199 /* call *<OFFSET>(<REG>) -> Call made from AOT code */
200 gpointer *vtable_slot;
202 vtable_slot = mono_get_vcall_slot_addr (code + 5, regs);
203 g_assert (vtable_slot);
205 *vtable_slot = nullified_class_init_trampoline;
207 printf ("Invalid trampoline sequence: %x %x %x %x %x %x %x\n", code [0], code [1], code [2], code [3],
208 code [4], code [5], code [6]);
209 g_assert_not_reached ();
214 mono_arch_nullify_plt_entry (guint8 *code, mgreg_t *regs)
216 if (mono_aot_only && !nullified_class_init_trampoline)
217 nullified_class_init_trampoline = mono_aot_get_trampoline ("nullified_class_init_trampoline");
219 mono_arch_patch_plt_entry (code, NULL, regs, nullified_class_init_trampoline);
223 mono_arch_create_generic_trampoline (MonoTrampolineType tramp_type, MonoTrampInfo **info, gboolean aot)
225 guint8 *buf, *code, *tramp;
226 int pushed_args, pushed_args_caller_saved;
227 GSList *unwind_ops = NULL;
228 MonoJumpInfo *ji = NULL;
230 unwind_ops = mono_arch_get_cie_program ();
232 code = buf = mono_global_codeman_reserve (256);
234 /* Note that there is a single argument to the trampoline
235 * and it is stored at: esp + pushed_args * sizeof (gpointer)
236 * the ret address is at: esp + (pushed_args + 1) * sizeof (gpointer)
239 /* Put all registers into an array on the stack
240 * If this code is changed, make sure to update the offset value in
241 * mono_arch_get_this_arg_from_call () in mini-x86.c.
243 x86_push_reg (code, X86_EDI);
244 x86_push_reg (code, X86_ESI);
245 x86_push_reg (code, X86_EBP);
246 x86_push_reg (code, X86_ESP);
247 x86_push_reg (code, X86_EBX);
248 x86_push_reg (code, X86_EDX);
249 x86_push_reg (code, X86_ECX);
250 x86_push_reg (code, X86_EAX);
252 pushed_args_caller_saved = pushed_args = 8;
254 /* Align stack on apple */
255 x86_alu_reg_imm (code, X86_SUB, X86_ESP, 4);
261 /* save the IP (caller ip) */
262 if (tramp_type == MONO_TRAMPOLINE_JUMP)
263 x86_push_imm (code, 0);
265 x86_push_membase (code, X86_ESP, (pushed_args + 1) * sizeof (gpointer));
269 x86_push_reg (code, X86_EBP);
270 x86_push_reg (code, X86_ESI);
271 x86_push_reg (code, X86_EDI);
272 x86_push_reg (code, X86_EBX);
277 x86_push_reg (code, X86_ESP);
278 /* Adjust ESP so it points to the previous frame */
279 x86_alu_membase_imm (code, X86_ADD, X86_ESP, 0, (pushed_args + 2) * 4);
283 /* save method info */
284 if ((tramp_type == MONO_TRAMPOLINE_JIT) || (tramp_type == MONO_TRAMPOLINE_JUMP))
285 x86_push_membase (code, X86_ESP, pushed_args * sizeof (gpointer));
287 x86_push_imm (code, 0);
291 /* On apple, the stack is correctly aligned to 16 bytes because pushed_args is
292 * 16 and there is the extra trampoline arg + the return ip pushed by call
293 * FIXME: Note that if an exception happens while some args are pushed
294 * on the stack, the stack will be misaligned.
296 g_assert (pushed_args == 16);
298 /* get the address of lmf for the current thread */
300 code = mono_arch_emit_load_aotconst (buf, code, &ji, MONO_PATCH_INFO_JIT_ICALL_ADDR, "mono_get_lmf_addr");
301 x86_call_reg (code, X86_EAX);
303 x86_call_code (code, mono_get_lmf_addr);
306 x86_push_reg (code, X86_EAX);
307 /* push *lfm (previous_lmf) */
308 x86_push_membase (code, X86_EAX, 0);
309 /* Signal to mono_arch_find_jit_info () that this is a trampoline frame */
310 x86_alu_membase_imm (code, X86_ADD, X86_ESP, 0, 1);
312 x86_mov_membase_reg (code, X86_EAX, 0, X86_ESP, 4);
317 /* starting the call sequence */
319 /* FIXME: Push the trampoline address */
320 x86_push_imm (code, 0);
324 /* push the method info */
325 x86_push_membase (code, X86_ESP, pushed_args * sizeof (gpointer));
329 /* push the return address onto the stack */
330 if (tramp_type == MONO_TRAMPOLINE_JUMP)
331 x86_push_imm (code, 0);
333 x86_push_membase (code, X86_ESP, (pushed_args + 1) * sizeof (gpointer));
335 /* push the address of the register array */
336 x86_lea_membase (code, X86_EAX, X86_ESP, (pushed_args - 8) * sizeof (gpointer));
337 x86_push_reg (code, X86_EAX);
342 /* check the stack is aligned after the ret ip is pushed */
343 /*x86_mov_reg_reg (buf, X86_EDX, X86_ESP, 4);
344 x86_alu_reg_imm (buf, X86_AND, X86_EDX, 15);
345 x86_alu_reg_imm (buf, X86_CMP, X86_EDX, 0);
346 x86_branch_disp (buf, X86_CC_Z, 3, FALSE);
347 x86_breakpoint (buf);*/
350 mono_add_unwind_op_def_cfa (unwind_ops, code, buf, X86_ESP, ((pushed_args + 2) * 4));
353 char *icall_name = g_strdup_printf ("trampoline_func_%d", tramp_type);
354 code = mono_arch_emit_load_aotconst (buf, code, &ji, MONO_PATCH_INFO_JIT_ICALL_ADDR, icall_name);
355 x86_call_reg (code, X86_EAX);
357 tramp = (guint8*)mono_get_trampoline_func (tramp_type);
358 x86_call_code (code, tramp);
361 x86_alu_reg_imm (code, X86_ADD, X86_ESP, 4*4);
365 /* Check for thread interruption */
366 /* This is not perf critical code so no need to check the interrupt flag */
367 /* Align the stack on osx */
368 x86_alu_reg_imm (code, X86_SUB, X86_ESP, 3 * 4);
369 x86_push_reg (code, X86_EAX);
371 code = mono_arch_emit_load_aotconst (buf, code, &ji, MONO_PATCH_INFO_JIT_ICALL_ADDR, "mono_thread_force_interruption_checkpoint");
372 x86_call_reg (code, X86_EAX);
374 x86_call_code (code, (guint8*)mono_thread_force_interruption_checkpoint);
376 x86_pop_reg (code, X86_EAX);
377 x86_alu_reg_imm (code, X86_ADD, X86_ESP, 3 * 4);
381 /* ebx = previous_lmf */
382 x86_pop_reg (code, X86_EBX);
384 x86_alu_reg_imm (code, X86_SUB, X86_EBX, 1);
387 x86_pop_reg (code, X86_EDI);
390 /* *(lmf) = previous_lmf */
391 x86_mov_membase_reg (code, X86_EDI, 0, X86_EBX, 4);
393 /* discard method info */
394 x86_pop_reg (code, X86_ESI);
398 x86_pop_reg (code, X86_ESI);
401 /* restore caller saved regs */
402 x86_pop_reg (code, X86_EBX);
403 x86_pop_reg (code, X86_EDI);
404 x86_pop_reg (code, X86_ESI);
405 x86_pop_reg (code, X86_EBP);
409 /* discard save IP */
410 x86_alu_reg_imm (code, X86_ADD, X86_ESP, 4);
413 /* restore LMF end */
415 if (!MONO_TRAMPOLINE_TYPE_MUST_RETURN (tramp_type)) {
417 * Overwrite the method ptr with the address we need to jump to,
420 x86_mov_membase_reg (code, X86_ESP, pushed_args * sizeof (gpointer), X86_EAX, 4);
423 /* Restore caller saved registers */
424 x86_mov_reg_membase (code, X86_ECX, X86_ESP, (pushed_args - pushed_args_caller_saved + X86_ECX) * 4, 4);
425 x86_mov_reg_membase (code, X86_EDX, X86_ESP, (pushed_args - pushed_args_caller_saved + X86_EDX) * 4, 4);
426 if ((tramp_type == MONO_TRAMPOLINE_RESTORE_STACK_PROT) || (tramp_type == MONO_TRAMPOLINE_AOT_PLT))
427 x86_mov_reg_membase (code, X86_EAX, X86_ESP, (pushed_args - pushed_args_caller_saved + X86_EAX) * 4, 4);
429 if (!MONO_TRAMPOLINE_TYPE_MUST_RETURN (tramp_type)) {
430 /* Pop saved reg array + stack align */
431 x86_alu_reg_imm (code, X86_ADD, X86_ESP, 9 * 4);
433 g_assert (pushed_args == 0);
435 /* Pop saved reg array + stack align + method ptr */
436 x86_alu_reg_imm (code, X86_ADD, X86_ESP, 10 * 4);
439 /* We've popped one more stack item than we've pushed (the
440 method ptr argument), so we must end up at -1. */
441 g_assert (pushed_args == -1);
446 g_assert ((code - buf) <= 256);
449 *info = mono_tramp_info_create (g_strdup_printf ("generic_trampoline_%d", tramp_type), buf, code - buf, ji, unwind_ops);
451 if (tramp_type == MONO_TRAMPOLINE_CLASS_INIT) {
452 /* Initialize the nullified class init trampoline used in the AOT case */
453 nullified_class_init_trampoline = mono_arch_get_nullified_class_init_trampoline (NULL);
460 mono_arch_get_nullified_class_init_trampoline (MonoTrampInfo **info)
464 code = buf = mono_global_codeman_reserve (16);
467 mono_arch_flush_icache (buf, code - buf);
470 *info = mono_tramp_info_create (g_strdup_printf ("nullified_class_init_trampoline"), buf, code - buf, NULL, NULL);
475 #define TRAMPOLINE_SIZE 10
478 mono_arch_create_specific_trampoline (gpointer arg1, MonoTrampolineType tramp_type, MonoDomain *domain, guint32 *code_len)
480 guint8 *code, *buf, *tramp;
482 tramp = mono_get_trampoline_code (tramp_type);
484 code = buf = mono_domain_code_reserve_align (domain, TRAMPOLINE_SIZE, 4);
486 x86_push_imm (buf, arg1);
487 x86_jump_code (buf, tramp);
488 g_assert ((buf - code) <= TRAMPOLINE_SIZE);
490 mono_arch_flush_icache (code, buf - code);
493 *code_len = buf - code;
499 mono_arch_create_rgctx_lazy_fetch_trampoline (guint32 slot, MonoTrampInfo **info, gboolean aot)
503 guint8 **rgctx_null_jumps;
508 MonoJumpInfo *ji = NULL;
509 GSList *unwind_ops = NULL;
511 mrgctx = MONO_RGCTX_SLOT_IS_MRGCTX (slot);
512 index = MONO_RGCTX_SLOT_INDEX (slot);
514 index += MONO_SIZEOF_METHOD_RUNTIME_GENERIC_CONTEXT / sizeof (gpointer);
515 for (depth = 0; ; ++depth) {
516 int size = mono_class_rgctx_get_array_size (depth, mrgctx);
518 if (index < size - 1)
523 tramp_size = (aot ? 64 : 36) + 6 * depth;
525 code = buf = mono_global_codeman_reserve (tramp_size);
527 rgctx_null_jumps = g_malloc (sizeof (guint8*) * (depth + 2));
529 /* load vtable/mrgctx ptr */
530 x86_mov_reg_membase (code, X86_EAX, X86_ESP, 4, 4);
532 /* load rgctx ptr from vtable */
533 x86_mov_reg_membase (code, X86_EAX, X86_EAX, G_STRUCT_OFFSET (MonoVTable, runtime_generic_context), 4);
534 /* is the rgctx ptr null? */
535 x86_test_reg_reg (code, X86_EAX, X86_EAX);
536 /* if yes, jump to actual trampoline */
537 rgctx_null_jumps [0] = code;
538 x86_branch8 (code, X86_CC_Z, -1, 1);
541 for (i = 0; i < depth; ++i) {
542 /* load ptr to next array */
543 if (mrgctx && i == 0)
544 x86_mov_reg_membase (code, X86_EAX, X86_EAX, MONO_SIZEOF_METHOD_RUNTIME_GENERIC_CONTEXT, 4);
546 x86_mov_reg_membase (code, X86_EAX, X86_EAX, 0, 4);
547 /* is the ptr null? */
548 x86_test_reg_reg (code, X86_EAX, X86_EAX);
549 /* if yes, jump to actual trampoline */
550 rgctx_null_jumps [i + 1] = code;
551 x86_branch8 (code, X86_CC_Z, -1, 1);
555 x86_mov_reg_membase (code, X86_EAX, X86_EAX, sizeof (gpointer) * (index + 1), 4);
556 /* is the slot null? */
557 x86_test_reg_reg (code, X86_EAX, X86_EAX);
558 /* if yes, jump to actual trampoline */
559 rgctx_null_jumps [depth + 1] = code;
560 x86_branch8 (code, X86_CC_Z, -1, 1);
561 /* otherwise return */
564 for (i = mrgctx ? 1 : 0; i <= depth + 1; ++i)
565 x86_patch (rgctx_null_jumps [i], code);
567 g_free (rgctx_null_jumps);
569 x86_mov_reg_membase (code, MONO_ARCH_VTABLE_REG, X86_ESP, 4, 4);
572 code = mono_arch_emit_load_aotconst (buf, code, &ji, MONO_PATCH_INFO_JIT_ICALL_ADDR, g_strdup_printf ("specific_trampoline_lazy_fetch_%u", slot));
573 x86_jump_reg (code, X86_EAX);
575 tramp = mono_arch_create_specific_trampoline (GUINT_TO_POINTER (slot), MONO_TRAMPOLINE_RGCTX_LAZY_FETCH, mono_get_root_domain (), NULL);
577 /* jump to the actual trampoline */
578 x86_jump_code (code, tramp);
581 mono_arch_flush_icache (buf, code - buf);
583 g_assert (code - buf <= tramp_size);
586 *info = mono_tramp_info_create (g_strdup_printf ("rgctx_fetch_trampoline_%u", slot), buf, code - buf, ji, unwind_ops);
592 mono_arch_create_generic_class_init_trampoline (MonoTrampInfo **info, gboolean aot)
596 static int byte_offset = -1;
597 static guint8 bitmask;
600 GSList *unwind_ops = NULL;
601 MonoJumpInfo *ji = NULL;
605 code = buf = mono_global_codeman_reserve (tramp_size);
608 mono_marshal_find_bitfield_offset (MonoVTable, initialized, &byte_offset, &bitmask);
610 x86_test_membase_imm (code, MONO_ARCH_VTABLE_REG, byte_offset, bitmask);
612 x86_branch8 (code, X86_CC_Z, -1, 1);
616 x86_patch (jump, code);
618 /* Push the vtable so the stack is the same as in a specific trampoline */
619 x86_push_reg (code, MONO_ARCH_VTABLE_REG);
622 code = mono_arch_emit_load_aotconst (buf, code, &ji, MONO_PATCH_INFO_JIT_ICALL_ADDR, "generic_trampoline_generic_class_init");
623 x86_jump_reg (code, X86_EAX);
625 tramp = mono_get_trampoline_code (MONO_TRAMPOLINE_GENERIC_CLASS_INIT);
627 /* jump to the actual trampoline */
628 x86_jump_code (code, tramp);
631 mono_arch_flush_icache (code, code - buf);
633 g_assert (code - buf <= tramp_size);
636 *info = mono_tramp_info_create (g_strdup_printf ("generic_class_init_trampoline"), buf, code - buf, ji, unwind_ops);
641 #ifdef MONO_ARCH_MONITOR_OBJECT_REG
643 * The code produced by this trampoline is equivalent to this:
646 * if (obj->synchronisation) {
647 * if (obj->synchronisation->owner == 0) {
648 * if (cmpxch (&obj->synchronisation->owner, TID, 0) == 0)
651 * if (obj->synchronisation->owner == TID) {
652 * ++obj->synchronisation->nest;
657 * return full_monitor_enter ();
661 mono_arch_create_monitor_enter_trampoline (MonoTrampInfo **info, gboolean aot)
663 guint8 *tramp = mono_get_trampoline_code (MONO_TRAMPOLINE_MONITOR_ENTER);
665 guint8 *jump_obj_null, *jump_sync_null, *jump_other_owner, *jump_cmpxchg_failed, *jump_tid;
667 int owner_offset, nest_offset, dummy;
668 MonoJumpInfo *ji = NULL;
669 GSList *unwind_ops = NULL;
671 g_assert (MONO_ARCH_MONITOR_OBJECT_REG == X86_EAX);
673 mono_monitor_threads_sync_members_offset (&owner_offset, &nest_offset, &dummy);
674 g_assert (MONO_THREADS_SYNC_MEMBER_SIZE (owner_offset) == sizeof (gpointer));
675 g_assert (MONO_THREADS_SYNC_MEMBER_SIZE (nest_offset) == sizeof (guint32));
676 owner_offset = MONO_THREADS_SYNC_MEMBER_OFFSET (owner_offset);
677 nest_offset = MONO_THREADS_SYNC_MEMBER_OFFSET (nest_offset);
681 code = buf = mono_global_codeman_reserve (tramp_size);
683 if (mono_thread_get_tls_offset () != -1) {
684 /* MonoObject* obj is in EAX */
686 x86_test_reg_reg (code, X86_EAX, X86_EAX);
687 /* if yes, jump to actual trampoline */
688 jump_obj_null = code;
689 x86_branch8 (code, X86_CC_Z, -1, 1);
691 /* load obj->synchronization to ECX */
692 x86_mov_reg_membase (code, X86_ECX, X86_EAX, G_STRUCT_OFFSET (MonoObject, synchronisation), 4);
693 /* is synchronization null? */
694 x86_test_reg_reg (code, X86_ECX, X86_ECX);
695 /* if yes, jump to actual trampoline */
696 jump_sync_null = code;
697 x86_branch8 (code, X86_CC_Z, -1, 1);
699 /* load MonoInternalThread* into EDX */
700 code = mono_x86_emit_tls_get (code, X86_EDX, mono_thread_get_tls_offset ());
701 /* load TID into EDX */
702 x86_mov_reg_membase (code, X86_EDX, X86_EDX, G_STRUCT_OFFSET (MonoInternalThread, tid), 4);
704 /* is synchronization->owner null? */
705 x86_alu_membase_imm (code, X86_CMP, X86_ECX, owner_offset, 0);
706 /* if not, jump to next case */
708 x86_branch8 (code, X86_CC_NZ, -1, 1);
710 /* if yes, try a compare-exchange with the TID */
711 /* free up register EAX, needed for the zero */
712 x86_push_reg (code, X86_EAX);
714 x86_alu_reg_reg (code, X86_XOR, X86_EAX, X86_EAX);
715 /* compare and exchange */
716 x86_prefix (code, X86_LOCK_PREFIX);
717 x86_cmpxchg_membase_reg (code, X86_ECX, owner_offset, X86_EDX);
718 /* if not successful, jump to actual trampoline */
719 jump_cmpxchg_failed = code;
720 x86_branch8 (code, X86_CC_NZ, -1, 1);
721 /* if successful, pop and return */
722 x86_pop_reg (code, X86_EAX);
725 /* next case: synchronization->owner is not null */
726 x86_patch (jump_tid, code);
727 /* is synchronization->owner == TID? */
728 x86_alu_membase_reg (code, X86_CMP, X86_ECX, owner_offset, X86_EDX);
729 /* if not, jump to actual trampoline */
730 jump_other_owner = code;
731 x86_branch8 (code, X86_CC_NZ, -1, 1);
732 /* if yes, increment nest */
733 x86_inc_membase (code, X86_ECX, nest_offset);
738 x86_patch (jump_obj_null, code);
739 x86_patch (jump_sync_null, code);
740 x86_patch (jump_other_owner, code);
741 x86_push_reg (code, X86_EAX);
742 /* jump to the actual trampoline */
743 x86_patch (jump_cmpxchg_failed, code);
745 /* We are calling the generic trampoline directly, the argument is pushed
746 * on the stack just like a specific trampoline.
748 code = mono_arch_emit_load_aotconst (buf, code, &ji, MONO_PATCH_INFO_JIT_ICALL_ADDR, "generic_trampoline_monitor_enter");
749 x86_jump_reg (code, X86_EAX);
751 x86_jump_code (code, tramp);
754 /* push obj and jump to the actual trampoline */
755 x86_push_reg (code, X86_EAX);
757 code = mono_arch_emit_load_aotconst (buf, code, &ji, MONO_PATCH_INFO_JIT_ICALL_ADDR, "generic_trampoline_monitor_enter");
758 x86_jump_reg (code, X86_EAX);
760 x86_jump_code (code, tramp);
764 mono_arch_flush_icache (buf, code - buf);
765 g_assert (code - buf <= tramp_size);
768 *info = mono_tramp_info_create (g_strdup_printf ("monitor_enter_trampoline"), buf, code - buf, ji, unwind_ops);
774 mono_arch_create_monitor_exit_trampoline (MonoTrampInfo **info, gboolean aot)
776 guint8 *tramp = mono_get_trampoline_code (MONO_TRAMPOLINE_MONITOR_EXIT);
778 guint8 *jump_obj_null, *jump_have_waiters, *jump_sync_null, *jump_not_owned;
781 int owner_offset, nest_offset, entry_count_offset;
782 MonoJumpInfo *ji = NULL;
783 GSList *unwind_ops = NULL;
785 g_assert (MONO_ARCH_MONITOR_OBJECT_REG == X86_EAX);
787 mono_monitor_threads_sync_members_offset (&owner_offset, &nest_offset, &entry_count_offset);
788 g_assert (MONO_THREADS_SYNC_MEMBER_SIZE (owner_offset) == sizeof (gpointer));
789 g_assert (MONO_THREADS_SYNC_MEMBER_SIZE (nest_offset) == sizeof (guint32));
790 g_assert (MONO_THREADS_SYNC_MEMBER_SIZE (entry_count_offset) == sizeof (gint32));
791 owner_offset = MONO_THREADS_SYNC_MEMBER_OFFSET (owner_offset);
792 nest_offset = MONO_THREADS_SYNC_MEMBER_OFFSET (nest_offset);
793 entry_count_offset = MONO_THREADS_SYNC_MEMBER_OFFSET (entry_count_offset);
797 code = buf = mono_global_codeman_reserve (tramp_size);
799 if (mono_thread_get_tls_offset () != -1) {
800 /* MonoObject* obj is in EAX */
802 x86_test_reg_reg (code, X86_EAX, X86_EAX);
803 /* if yes, jump to actual trampoline */
804 jump_obj_null = code;
805 x86_branch8 (code, X86_CC_Z, -1, 1);
807 /* load obj->synchronization to ECX */
808 x86_mov_reg_membase (code, X86_ECX, X86_EAX, G_STRUCT_OFFSET (MonoObject, synchronisation), 4);
809 /* is synchronization null? */
810 x86_test_reg_reg (code, X86_ECX, X86_ECX);
811 /* if yes, jump to actual trampoline */
812 jump_sync_null = code;
813 x86_branch8 (code, X86_CC_Z, -1, 1);
815 /* next case: synchronization is not null */
816 /* load MonoInternalThread* into EDX */
817 code = mono_x86_emit_tls_get (code, X86_EDX, mono_thread_get_tls_offset ());
818 /* load TID into EDX */
819 x86_mov_reg_membase (code, X86_EDX, X86_EDX, G_STRUCT_OFFSET (MonoInternalThread, tid), 4);
820 /* is synchronization->owner == TID */
821 x86_alu_membase_reg (code, X86_CMP, X86_ECX, owner_offset, X86_EDX);
822 /* if no, jump to actual trampoline */
823 jump_not_owned = code;
824 x86_branch8 (code, X86_CC_NZ, -1, 1);
826 /* next case: synchronization->owner == TID */
827 /* is synchronization->nest == 1 */
828 x86_alu_membase_imm (code, X86_CMP, X86_ECX, nest_offset, 1);
829 /* if not, jump to next case */
831 x86_branch8 (code, X86_CC_NZ, -1, 1);
832 /* if yes, is synchronization->entry_count zero? */
833 x86_alu_membase_imm (code, X86_CMP, X86_ECX, entry_count_offset, 0);
834 /* if not, jump to actual trampoline */
835 jump_have_waiters = code;
836 x86_branch8 (code, X86_CC_NZ, -1 , 1);
837 /* if yes, set synchronization->owner to null and return */
838 x86_mov_membase_imm (code, X86_ECX, owner_offset, 0, 4);
841 /* next case: synchronization->nest is not 1 */
842 x86_patch (jump_next, code);
843 /* decrease synchronization->nest and return */
844 x86_dec_membase (code, X86_ECX, nest_offset);
847 /* push obj and jump to the actual trampoline */
848 x86_patch (jump_obj_null, code);
849 x86_patch (jump_have_waiters, code);
850 x86_patch (jump_not_owned, code);
851 x86_patch (jump_sync_null, code);
854 /* push obj and jump to the actual trampoline */
855 x86_push_reg (code, X86_EAX);
857 code = mono_arch_emit_load_aotconst (buf, code, &ji, MONO_PATCH_INFO_JIT_ICALL_ADDR, "generic_trampoline_monitor_exit");
858 x86_jump_reg (code, X86_EAX);
860 x86_jump_code (code, tramp);
863 mono_arch_flush_icache (buf, code - buf);
864 g_assert (code - buf <= tramp_size);
867 *info = mono_tramp_info_create (g_strdup_printf ("monitor_exit_trampoline"), buf, code - buf, ji, unwind_ops);
875 mono_arch_create_monitor_enter_trampoline (MonoTrampInfo **info, gboolean aot)
877 g_assert_not_reached ();
882 mono_arch_create_monitor_exit_trampoline (MonoTrampInfo **info, gboolean aot)
884 g_assert_not_reached ();
891 mono_arch_invalidate_method (MonoJitInfo *ji, void *func, gpointer func_arg)
893 /* FIXME: This is not thread safe */
894 guint8 *code = ji->code_start;
896 x86_push_imm (code, func_arg);
897 x86_call_code (code, (guint8*)func);
901 handler_block_trampoline_helper (gpointer *ptr)
903 MonoJitTlsData *jit_tls = TlsGetValue (mono_jit_tls_id);
904 *ptr = jit_tls->handler_block_return_address;
908 mono_arch_create_handler_block_trampoline (void)
910 guint8 *tramp = mono_get_trampoline_code (MONO_TRAMPOLINE_HANDLER_BLOCK_GUARD);
913 code = buf = mono_global_codeman_reserve (tramp_size);
916 This trampoline restore the call chain of the handler block then jumps into the code that deals with it.
919 if (mono_get_jit_tls_offset () != -1) {
920 code = mono_x86_emit_tls_get (code, X86_EAX, mono_get_jit_tls_offset ());
921 x86_mov_reg_membase (code, X86_EAX, X86_EAX, G_STRUCT_OFFSET (MonoJitTlsData, handler_block_return_address), 4);
923 x86_push_reg (code, X86_EAX);
924 x86_jump_code (code, tramp);
926 /*Slow path uses a c helper*/
927 x86_push_reg (code, X86_ESP);
928 x86_push_imm (code, tramp);
929 x86_jump_code (code, handler_block_trampoline_helper);
932 mono_arch_flush_icache (buf, code - buf);
933 g_assert (code - buf <= tramp_size);
939 mono_arch_get_call_target (guint8 *code)
941 if (code [-5] == 0xe8) {
942 guint32 disp = *(guint32*)(code - 4);
943 guint8 *target = code + disp;
952 mono_arch_get_plt_info_offset (guint8 *plt_entry, mgreg_t *regs, guint8 *code)
954 return *(guint32*)(plt_entry + 6);