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 #if defined(__native_client_codegen__) || defined(__native_client__)
148 /* for both compiler and runtime */
150 /* mov <DISP>(%ebx), %ecx */
151 /* and 0xffffffe0, %ecx */
153 g_assert (code [0] == 0x8b);
154 g_assert (code [1] == 0x8b);
156 offset = *(guint32*)(code + 2);
158 /* A PLT entry: jmp *<DISP>(%ebx) */
159 g_assert (code [0] == 0xff);
160 g_assert (code [1] == 0xa3);
162 offset = *(guint32*)(code + 2);
163 #endif /* __native_client_codegen__ */
165 got = (gpointer*)(gsize) regs [MONO_ARCH_GOT_REG];
166 *(guint8**)((guint8*)got + offset) = addr;
170 get_vcall_slot (guint8 *code, mgreg_t *regs, int *displacement)
172 const int kBufSize = NACL_SIZE (8, 16);
177 mono_breakpoint_clean_code (NULL, code, kBufSize, buf, kBufSize);
182 if ((code [0] == 0xff) && ((code [1] & 0x18) == 0x10) && ((code [1] >> 6) == 2)) {
183 reg = code [1] & 0x07;
184 disp = *((gint32*)(code + 2));
185 #if defined(__native_client_codegen__) || defined(__native_client__)
186 } else if ((code[1] == 0x83) && (code[2] == 0xe1) && (code[4] == 0xff) &&
187 (code[5] == 0xd1) && (code[-5] == 0x8b)) {
188 disp = *((gint32*)(code - 3));
189 reg = code[-4] & 0x07;
190 } else if ((code[-2] == 0x8b) && (code[1] == 0x83) && (code[4] == 0xff)) {
191 reg = code[-1] & 0x07;
192 disp = (signed char)code[0];
195 g_assert_not_reached ();
199 *displacement = disp;
200 return (gpointer)regs [reg];
204 get_vcall_slot_addr (guint8* code, mgreg_t *regs)
208 vt = get_vcall_slot (code, regs, &displacement);
211 return (gpointer*)((char*)vt + displacement);
215 mono_arch_nullify_class_init_trampoline (guint8 *code, mgreg_t *regs)
218 gboolean can_write = mono_breakpoint_clean_code (NULL, code, 6, buf, sizeof (buf));
224 if (code [0] == 0xe8) {
225 if (!mono_running_on_valgrind ()) {
228 * Thread safe code patching using the algorithm from the paper
229 * 'Practicing JUDO: Java Under Dynamic Optimizations'
232 * First atomically change the the first 2 bytes of the call to a
236 InterlockedExchange ((gint32*)code, ops);
238 /* Then change the other bytes to a nop */
243 /* Then atomically change the first 4 bytes to a nop as well */
245 InterlockedExchange ((gint32*)code, ops);
246 /* FIXME: the calltree skin trips on the self modifying code above */
248 /* Tell valgrind to recompile the patched code */
249 //VALGRIND_DISCARD_TRANSLATIONS (code, 8);
251 } else if (code [0] == 0x90 || code [0] == 0xeb) {
252 /* Already changed by another thread */
254 } else if ((code [-1] == 0xff) && (x86_modrm_reg (code [0]) == 0x2)) {
255 /* call *<OFFSET>(<REG>) -> Call made from AOT code */
256 gpointer *vtable_slot;
258 vtable_slot = get_vcall_slot_addr (code + 5, regs);
259 g_assert (vtable_slot);
261 *vtable_slot = nullified_class_init_trampoline;
263 printf ("Invalid trampoline sequence: %x %x %x %x %x %x %x\n", code [0], code [1], code [2], code [3],
264 code [4], code [5], code [6]);
265 g_assert_not_reached ();
270 mono_arch_nullify_plt_entry (guint8 *code, mgreg_t *regs)
272 if (mono_aot_only && !nullified_class_init_trampoline)
273 nullified_class_init_trampoline = mono_aot_get_trampoline ("nullified_class_init_trampoline");
275 mono_arch_patch_plt_entry (code, NULL, regs, nullified_class_init_trampoline);
279 mono_arch_create_generic_trampoline (MonoTrampolineType tramp_type, MonoTrampInfo **info, gboolean aot)
281 guint8 *buf, *code, *tramp;
282 int pushed_args, pushed_args_caller_saved;
283 GSList *unwind_ops = NULL;
284 MonoJumpInfo *ji = NULL;
286 unwind_ops = mono_arch_get_cie_program ();
288 code = buf = mono_global_codeman_reserve (256);
290 /* Note that there is a single argument to the trampoline
291 * and it is stored at: esp + pushed_args * sizeof (gpointer)
292 * the ret address is at: esp + (pushed_args + 1) * sizeof (gpointer)
295 /* Put all registers into an array on the stack
296 * If this code is changed, make sure to update the offset value in
297 * mono_arch_get_this_arg_from_call () in mini-x86.c.
299 x86_push_reg (code, X86_EDI);
300 x86_push_reg (code, X86_ESI);
301 x86_push_reg (code, X86_EBP);
302 x86_push_reg (code, X86_ESP);
303 x86_push_reg (code, X86_EBX);
304 x86_push_reg (code, X86_EDX);
305 x86_push_reg (code, X86_ECX);
306 x86_push_reg (code, X86_EAX);
308 pushed_args_caller_saved = pushed_args = 8;
310 /* Align stack on apple */
311 x86_alu_reg_imm (code, X86_SUB, X86_ESP, 4);
317 /* save the IP (caller ip) */
318 if (tramp_type == MONO_TRAMPOLINE_JUMP)
319 x86_push_imm (code, 0);
321 x86_push_membase (code, X86_ESP, (pushed_args + 1) * sizeof (gpointer));
325 x86_push_reg (code, X86_EBP);
326 x86_push_reg (code, X86_ESI);
327 x86_push_reg (code, X86_EDI);
328 x86_push_reg (code, X86_EBX);
333 x86_push_reg (code, X86_ESP);
334 /* Adjust ESP so it points to the previous frame */
335 x86_alu_membase_imm (code, X86_ADD, X86_ESP, 0, (pushed_args + 2) * 4);
339 /* save method info */
340 if ((tramp_type == MONO_TRAMPOLINE_JIT) || (tramp_type == MONO_TRAMPOLINE_JUMP))
341 x86_push_membase (code, X86_ESP, pushed_args * sizeof (gpointer));
343 x86_push_imm (code, 0);
347 /* On apple, the stack is correctly aligned to 16 bytes because pushed_args is
348 * 16 and there is the extra trampoline arg + the return ip pushed by call
349 * FIXME: Note that if an exception happens while some args are pushed
350 * on the stack, the stack will be misaligned.
352 g_assert (pushed_args == 16);
354 /* get the address of lmf for the current thread */
356 code = mono_arch_emit_load_aotconst (buf, code, &ji, MONO_PATCH_INFO_JIT_ICALL_ADDR, "mono_get_lmf_addr");
357 x86_call_reg (code, X86_EAX);
359 x86_call_code (code, mono_get_lmf_addr);
362 x86_push_reg (code, X86_EAX);
363 /* push *lfm (previous_lmf) */
364 x86_push_membase (code, X86_EAX, 0);
365 /* Signal to mono_arch_find_jit_info () that this is a trampoline frame */
366 x86_alu_membase_imm (code, X86_ADD, X86_ESP, 0, 1);
368 x86_mov_membase_reg (code, X86_EAX, 0, X86_ESP, 4);
373 /* starting the call sequence */
375 /* FIXME: Push the trampoline address */
376 x86_push_imm (code, 0);
380 /* push the method info */
381 x86_push_membase (code, X86_ESP, pushed_args * sizeof (gpointer));
385 /* push the return address onto the stack */
386 if (tramp_type == MONO_TRAMPOLINE_JUMP)
387 x86_push_imm (code, 0);
389 x86_push_membase (code, X86_ESP, (pushed_args + 1) * sizeof (gpointer));
391 /* push the address of the register array */
392 x86_lea_membase (code, X86_EAX, X86_ESP, (pushed_args - 8) * sizeof (gpointer));
393 x86_push_reg (code, X86_EAX);
398 /* check the stack is aligned after the ret ip is pushed */
399 /*x86_mov_reg_reg (buf, X86_EDX, X86_ESP, 4);
400 x86_alu_reg_imm (buf, X86_AND, X86_EDX, 15);
401 x86_alu_reg_imm (buf, X86_CMP, X86_EDX, 0);
402 x86_branch_disp (buf, X86_CC_Z, 3, FALSE);
403 x86_breakpoint (buf);*/
406 mono_add_unwind_op_def_cfa (unwind_ops, code, buf, X86_ESP, ((pushed_args + 2) * 4));
409 char *icall_name = g_strdup_printf ("trampoline_func_%d", tramp_type);
410 code = mono_arch_emit_load_aotconst (buf, code, &ji, MONO_PATCH_INFO_JIT_ICALL_ADDR, icall_name);
411 x86_call_reg (code, X86_EAX);
413 tramp = (guint8*)mono_get_trampoline_func (tramp_type);
414 x86_call_code (code, tramp);
417 x86_alu_reg_imm (code, X86_ADD, X86_ESP, 4*4);
421 /* Check for thread interruption */
422 /* This is not perf critical code so no need to check the interrupt flag */
423 /* Align the stack on osx */
424 x86_alu_reg_imm (code, X86_SUB, X86_ESP, 3 * 4);
425 x86_push_reg (code, X86_EAX);
427 code = mono_arch_emit_load_aotconst (buf, code, &ji, MONO_PATCH_INFO_JIT_ICALL_ADDR, "mono_thread_force_interruption_checkpoint");
428 x86_call_reg (code, X86_EAX);
430 x86_call_code (code, (guint8*)mono_thread_force_interruption_checkpoint);
432 x86_pop_reg (code, X86_EAX);
433 x86_alu_reg_imm (code, X86_ADD, X86_ESP, 3 * 4);
437 /* ebx = previous_lmf */
438 x86_pop_reg (code, X86_EBX);
440 x86_alu_reg_imm (code, X86_SUB, X86_EBX, 1);
443 x86_pop_reg (code, X86_EDI);
446 /* *(lmf) = previous_lmf */
447 x86_mov_membase_reg (code, X86_EDI, 0, X86_EBX, 4);
449 /* discard method info */
450 x86_pop_reg (code, X86_ESI);
454 x86_pop_reg (code, X86_ESI);
457 /* restore caller saved regs */
458 x86_pop_reg (code, X86_EBX);
459 x86_pop_reg (code, X86_EDI);
460 x86_pop_reg (code, X86_ESI);
461 x86_pop_reg (code, X86_EBP);
465 /* discard save IP */
466 x86_alu_reg_imm (code, X86_ADD, X86_ESP, 4);
469 /* restore LMF end */
471 if (!MONO_TRAMPOLINE_TYPE_MUST_RETURN (tramp_type)) {
473 * Overwrite the method ptr with the address we need to jump to,
476 x86_mov_membase_reg (code, X86_ESP, pushed_args * sizeof (gpointer), X86_EAX, 4);
479 /* Restore caller saved registers */
480 x86_mov_reg_membase (code, X86_ECX, X86_ESP, (pushed_args - pushed_args_caller_saved + X86_ECX) * 4, 4);
481 x86_mov_reg_membase (code, X86_EDX, X86_ESP, (pushed_args - pushed_args_caller_saved + X86_EDX) * 4, 4);
482 if ((tramp_type == MONO_TRAMPOLINE_RESTORE_STACK_PROT) || (tramp_type == MONO_TRAMPOLINE_AOT_PLT))
483 x86_mov_reg_membase (code, X86_EAX, X86_ESP, (pushed_args - pushed_args_caller_saved + X86_EAX) * 4, 4);
485 if (!MONO_TRAMPOLINE_TYPE_MUST_RETURN (tramp_type)) {
486 /* Pop saved reg array + stack align */
487 x86_alu_reg_imm (code, X86_ADD, X86_ESP, 9 * 4);
489 g_assert (pushed_args == 0);
491 /* Pop saved reg array + stack align + method ptr */
492 x86_alu_reg_imm (code, X86_ADD, X86_ESP, 10 * 4);
495 /* We've popped one more stack item than we've pushed (the
496 method ptr argument), so we must end up at -1. */
497 g_assert (pushed_args == -1);
502 g_assert ((code - buf) <= 256);
505 *info = mono_tramp_info_create (mono_get_generic_trampoline_name (tramp_type), buf, code - buf, ji, unwind_ops);
507 if (tramp_type == MONO_TRAMPOLINE_CLASS_INIT) {
508 /* Initialize the nullified class init trampoline used in the AOT case */
509 nullified_class_init_trampoline = mono_arch_get_nullified_class_init_trampoline (NULL);
512 if (mono_jit_map_is_enabled ()) {
514 buff = mono_get_generic_trampoline_name (tramp_type);
515 mono_emit_jit_tramp (buf, code - buf, buff);
523 mono_arch_get_nullified_class_init_trampoline (MonoTrampInfo **info)
527 code = buf = mono_global_codeman_reserve (16);
530 mono_arch_flush_icache (buf, code - buf);
533 *info = mono_tramp_info_create (g_strdup_printf ("nullified_class_init_trampoline"), buf, code - buf, NULL, NULL);
535 if (mono_jit_map_is_enabled ())
536 mono_emit_jit_tramp (buf, code - buf, "nullified_class_init_trampoline");
541 #define TRAMPOLINE_SIZE 10
544 mono_arch_create_specific_trampoline (gpointer arg1, MonoTrampolineType tramp_type, MonoDomain *domain, guint32 *code_len)
546 guint8 *code, *buf, *tramp;
548 tramp = mono_get_trampoline_code (tramp_type);
550 code = buf = mono_domain_code_reserve_align (domain, TRAMPOLINE_SIZE, NACL_SIZE (4, kNaClAlignment));
552 x86_push_imm (buf, arg1);
553 x86_jump_code (buf, tramp);
554 g_assert ((buf - code) <= TRAMPOLINE_SIZE);
556 mono_arch_flush_icache (code, buf - code);
559 *code_len = buf - code;
565 mono_arch_create_rgctx_lazy_fetch_trampoline (guint32 slot, MonoTrampInfo **info, gboolean aot)
569 guint8 **rgctx_null_jumps;
574 MonoJumpInfo *ji = NULL;
575 GSList *unwind_ops = NULL;
577 unwind_ops = mono_arch_get_cie_program ();
579 mrgctx = MONO_RGCTX_SLOT_IS_MRGCTX (slot);
580 index = MONO_RGCTX_SLOT_INDEX (slot);
582 index += MONO_SIZEOF_METHOD_RUNTIME_GENERIC_CONTEXT / sizeof (gpointer);
583 for (depth = 0; ; ++depth) {
584 int size = mono_class_rgctx_get_array_size (depth, mrgctx);
586 if (index < size - 1)
591 #ifdef __native_client_codegen__
592 /* TODO: align for Native Client */
593 tramp_size = (aot ? 64 : 36) + 2 * kNaClAlignment +
594 6 * (depth + kNaClAlignment);
596 tramp_size = (aot ? 64 : 36) + 6 * depth;
597 #endif /* __native_client_codegen__ */
599 code = buf = mono_global_codeman_reserve (tramp_size);
601 rgctx_null_jumps = g_malloc (sizeof (guint8*) * (depth + 2));
603 /* load vtable/mrgctx ptr */
604 x86_mov_reg_membase (code, X86_EAX, X86_ESP, 4, 4);
606 /* load rgctx ptr from vtable */
607 x86_mov_reg_membase (code, X86_EAX, X86_EAX, G_STRUCT_OFFSET (MonoVTable, runtime_generic_context), 4);
608 /* is the rgctx ptr null? */
609 x86_test_reg_reg (code, X86_EAX, X86_EAX);
610 /* if yes, jump to actual trampoline */
611 rgctx_null_jumps [0] = code;
612 x86_branch8 (code, X86_CC_Z, -1, 1);
615 for (i = 0; i < depth; ++i) {
616 /* load ptr to next array */
617 if (mrgctx && i == 0)
618 x86_mov_reg_membase (code, X86_EAX, X86_EAX, MONO_SIZEOF_METHOD_RUNTIME_GENERIC_CONTEXT, 4);
620 x86_mov_reg_membase (code, X86_EAX, X86_EAX, 0, 4);
621 /* is the ptr null? */
622 x86_test_reg_reg (code, X86_EAX, X86_EAX);
623 /* if yes, jump to actual trampoline */
624 rgctx_null_jumps [i + 1] = code;
625 x86_branch8 (code, X86_CC_Z, -1, 1);
629 x86_mov_reg_membase (code, X86_EAX, X86_EAX, sizeof (gpointer) * (index + 1), 4);
630 /* is the slot null? */
631 x86_test_reg_reg (code, X86_EAX, X86_EAX);
632 /* if yes, jump to actual trampoline */
633 rgctx_null_jumps [depth + 1] = code;
634 x86_branch8 (code, X86_CC_Z, -1, 1);
635 /* otherwise return */
638 for (i = mrgctx ? 1 : 0; i <= depth + 1; ++i)
639 x86_patch (rgctx_null_jumps [i], code);
641 g_free (rgctx_null_jumps);
643 x86_mov_reg_membase (code, MONO_ARCH_VTABLE_REG, X86_ESP, 4, 4);
646 code = mono_arch_emit_load_aotconst (buf, code, &ji, MONO_PATCH_INFO_JIT_ICALL_ADDR, g_strdup_printf ("specific_trampoline_lazy_fetch_%u", slot));
647 x86_jump_reg (code, X86_EAX);
649 tramp = mono_arch_create_specific_trampoline (GUINT_TO_POINTER (slot), MONO_TRAMPOLINE_RGCTX_LAZY_FETCH, mono_get_root_domain (), NULL);
651 /* jump to the actual trampoline */
652 x86_jump_code (code, tramp);
655 mono_arch_flush_icache (buf, code - buf);
657 g_assert (code - buf <= tramp_size);
660 *info = mono_tramp_info_create (mono_get_rgctx_fetch_trampoline_name (slot), buf, code - buf, ji, unwind_ops);
662 if (mono_jit_map_is_enabled ()) {
663 char *buff = mono_get_rgctx_fetch_trampoline_name (slot);
664 mono_emit_jit_tramp (buf, code - buf, buff);
672 mono_arch_create_generic_class_init_trampoline (MonoTrampInfo **info, gboolean aot)
676 static int byte_offset = -1;
677 static guint8 bitmask;
680 GSList *unwind_ops = NULL;
681 MonoJumpInfo *ji = NULL;
685 code = buf = mono_global_codeman_reserve (tramp_size);
687 unwind_ops = mono_arch_get_cie_program ();
690 mono_marshal_find_bitfield_offset (MonoVTable, initialized, &byte_offset, &bitmask);
692 x86_test_membase_imm (code, MONO_ARCH_VTABLE_REG, byte_offset, bitmask);
694 x86_branch8 (code, X86_CC_Z, -1, 1);
698 x86_patch (jump, code);
700 /* Push the vtable so the stack is the same as in a specific trampoline */
701 x86_push_reg (code, MONO_ARCH_VTABLE_REG);
704 code = mono_arch_emit_load_aotconst (buf, code, &ji, MONO_PATCH_INFO_JIT_ICALL_ADDR, "generic_trampoline_generic_class_init");
705 x86_jump_reg (code, X86_EAX);
707 tramp = mono_get_trampoline_code (MONO_TRAMPOLINE_GENERIC_CLASS_INIT);
709 /* jump to the actual trampoline */
710 x86_jump_code (code, tramp);
713 mono_arch_flush_icache (code, code - buf);
715 g_assert (code - buf <= tramp_size);
716 #ifdef __native_client_codegen__
717 g_assert (code - buf <= kNaClAlignment);
720 *info = mono_tramp_info_create (g_strdup_printf ("generic_class_init_trampoline"), buf, code - buf, ji, unwind_ops);
722 if (mono_jit_map_is_enabled ())
723 mono_emit_jit_tramp (buf, code - buf, "generic_class_init_trampoline");
728 #ifdef MONO_ARCH_MONITOR_OBJECT_REG
730 * The code produced by this trampoline is equivalent to this:
733 * if (obj->synchronisation) {
734 * if (obj->synchronisation->owner == 0) {
735 * if (cmpxch (&obj->synchronisation->owner, TID, 0) == 0)
738 * if (obj->synchronisation->owner == TID) {
739 * ++obj->synchronisation->nest;
744 * return full_monitor_enter ();
748 mono_arch_create_monitor_enter_trampoline (MonoTrampInfo **info, gboolean aot)
750 guint8 *tramp = mono_get_trampoline_code (MONO_TRAMPOLINE_MONITOR_ENTER);
752 guint8 *jump_obj_null, *jump_sync_null, *jump_other_owner, *jump_cmpxchg_failed, *jump_tid;
754 int owner_offset, nest_offset, dummy;
755 MonoJumpInfo *ji = NULL;
756 GSList *unwind_ops = NULL;
758 g_assert (MONO_ARCH_MONITOR_OBJECT_REG == X86_EAX);
760 mono_monitor_threads_sync_members_offset (&owner_offset, &nest_offset, &dummy);
761 g_assert (MONO_THREADS_SYNC_MEMBER_SIZE (owner_offset) == sizeof (gpointer));
762 g_assert (MONO_THREADS_SYNC_MEMBER_SIZE (nest_offset) == sizeof (guint32));
763 owner_offset = MONO_THREADS_SYNC_MEMBER_OFFSET (owner_offset);
764 nest_offset = MONO_THREADS_SYNC_MEMBER_OFFSET (nest_offset);
766 tramp_size = NACL_SIZE (64, 128);
768 code = buf = mono_global_codeman_reserve (tramp_size);
770 if (mono_thread_get_tls_offset () != -1) {
771 /* MonoObject* obj is in EAX */
773 x86_test_reg_reg (code, X86_EAX, X86_EAX);
774 /* if yes, jump to actual trampoline */
775 jump_obj_null = code;
776 x86_branch8 (code, X86_CC_Z, -1, 1);
778 /* load obj->synchronization to ECX */
779 x86_mov_reg_membase (code, X86_ECX, X86_EAX, G_STRUCT_OFFSET (MonoObject, synchronisation), 4);
780 /* is synchronization null? */
781 x86_test_reg_reg (code, X86_ECX, X86_ECX);
782 /* if yes, jump to actual trampoline */
783 jump_sync_null = code;
784 x86_branch8 (code, X86_CC_Z, -1, 1);
786 /* load MonoInternalThread* into EDX */
787 code = mono_x86_emit_tls_get (code, X86_EDX, mono_thread_get_tls_offset ());
788 /* load TID into EDX */
789 x86_mov_reg_membase (code, X86_EDX, X86_EDX, G_STRUCT_OFFSET (MonoInternalThread, tid), 4);
791 /* is synchronization->owner null? */
792 x86_alu_membase_imm (code, X86_CMP, X86_ECX, owner_offset, 0);
793 /* if not, jump to next case */
795 x86_branch8 (code, X86_CC_NZ, -1, 1);
797 /* if yes, try a compare-exchange with the TID */
798 /* free up register EAX, needed for the zero */
799 x86_push_reg (code, X86_EAX);
801 x86_alu_reg_reg (code, X86_XOR, X86_EAX, X86_EAX);
802 /* compare and exchange */
803 x86_prefix (code, X86_LOCK_PREFIX);
804 x86_cmpxchg_membase_reg (code, X86_ECX, owner_offset, X86_EDX);
805 /* if not successful, jump to actual trampoline */
806 jump_cmpxchg_failed = code;
807 x86_branch8 (code, X86_CC_NZ, -1, 1);
808 /* if successful, pop and return */
809 x86_pop_reg (code, X86_EAX);
812 /* next case: synchronization->owner is not null */
813 x86_patch (jump_tid, code);
814 /* is synchronization->owner == TID? */
815 x86_alu_membase_reg (code, X86_CMP, X86_ECX, owner_offset, X86_EDX);
816 /* if not, jump to actual trampoline */
817 jump_other_owner = code;
818 x86_branch8 (code, X86_CC_NZ, -1, 1);
819 /* if yes, increment nest */
820 x86_inc_membase (code, X86_ECX, nest_offset);
825 x86_patch (jump_obj_null, code);
826 x86_patch (jump_sync_null, code);
827 x86_patch (jump_other_owner, code);
828 x86_push_reg (code, X86_EAX);
829 /* jump to the actual trampoline */
830 x86_patch (jump_cmpxchg_failed, code);
832 /* We are calling the generic trampoline directly, the argument is pushed
833 * on the stack just like a specific trampoline.
835 code = mono_arch_emit_load_aotconst (buf, code, &ji, MONO_PATCH_INFO_JIT_ICALL_ADDR, "generic_trampoline_monitor_enter");
836 x86_jump_reg (code, X86_EAX);
838 x86_jump_code (code, tramp);
841 /* push obj and jump to the actual trampoline */
842 x86_push_reg (code, X86_EAX);
844 code = mono_arch_emit_load_aotconst (buf, code, &ji, MONO_PATCH_INFO_JIT_ICALL_ADDR, "generic_trampoline_monitor_enter");
845 x86_jump_reg (code, X86_EAX);
847 x86_jump_code (code, tramp);
851 mono_arch_flush_icache (buf, code - buf);
852 g_assert (code - buf <= tramp_size);
855 *info = mono_tramp_info_create (g_strdup_printf ("monitor_enter_trampoline"), buf, code - buf, ji, unwind_ops);
857 if (mono_jit_map_is_enabled ())
858 mono_emit_jit_tramp (buf, code - buf, "monitor_enter_trampoline");
864 mono_arch_create_monitor_exit_trampoline (MonoTrampInfo **info, gboolean aot)
866 guint8 *tramp = mono_get_trampoline_code (MONO_TRAMPOLINE_MONITOR_EXIT);
868 guint8 *jump_obj_null, *jump_have_waiters, *jump_sync_null, *jump_not_owned;
871 int owner_offset, nest_offset, entry_count_offset;
872 MonoJumpInfo *ji = NULL;
873 GSList *unwind_ops = NULL;
875 g_assert (MONO_ARCH_MONITOR_OBJECT_REG == X86_EAX);
877 mono_monitor_threads_sync_members_offset (&owner_offset, &nest_offset, &entry_count_offset);
878 g_assert (MONO_THREADS_SYNC_MEMBER_SIZE (owner_offset) == sizeof (gpointer));
879 g_assert (MONO_THREADS_SYNC_MEMBER_SIZE (nest_offset) == sizeof (guint32));
880 g_assert (MONO_THREADS_SYNC_MEMBER_SIZE (entry_count_offset) == sizeof (gint32));
881 owner_offset = MONO_THREADS_SYNC_MEMBER_OFFSET (owner_offset);
882 nest_offset = MONO_THREADS_SYNC_MEMBER_OFFSET (nest_offset);
883 entry_count_offset = MONO_THREADS_SYNC_MEMBER_OFFSET (entry_count_offset);
885 tramp_size = NACL_SIZE (64, 128);
887 code = buf = mono_global_codeman_reserve (tramp_size);
889 if (mono_thread_get_tls_offset () != -1) {
890 /* MonoObject* obj is in EAX */
892 x86_test_reg_reg (code, X86_EAX, X86_EAX);
893 /* if yes, jump to actual trampoline */
894 jump_obj_null = code;
895 x86_branch8 (code, X86_CC_Z, -1, 1);
897 /* load obj->synchronization to ECX */
898 x86_mov_reg_membase (code, X86_ECX, X86_EAX, G_STRUCT_OFFSET (MonoObject, synchronisation), 4);
899 /* is synchronization null? */
900 x86_test_reg_reg (code, X86_ECX, X86_ECX);
901 /* if yes, jump to actual trampoline */
902 jump_sync_null = code;
903 x86_branch8 (code, X86_CC_Z, -1, 1);
905 /* next case: synchronization is not null */
906 /* load MonoInternalThread* into EDX */
907 code = mono_x86_emit_tls_get (code, X86_EDX, mono_thread_get_tls_offset ());
908 /* load TID into EDX */
909 x86_mov_reg_membase (code, X86_EDX, X86_EDX, G_STRUCT_OFFSET (MonoInternalThread, tid), 4);
910 /* is synchronization->owner == TID */
911 x86_alu_membase_reg (code, X86_CMP, X86_ECX, owner_offset, X86_EDX);
912 /* if no, jump to actual trampoline */
913 jump_not_owned = code;
914 x86_branch8 (code, X86_CC_NZ, -1, 1);
916 /* next case: synchronization->owner == TID */
917 /* is synchronization->nest == 1 */
918 x86_alu_membase_imm (code, X86_CMP, X86_ECX, nest_offset, 1);
919 /* if not, jump to next case */
921 x86_branch8 (code, X86_CC_NZ, -1, 1);
922 /* if yes, is synchronization->entry_count zero? */
923 x86_alu_membase_imm (code, X86_CMP, X86_ECX, entry_count_offset, 0);
924 /* if not, jump to actual trampoline */
925 jump_have_waiters = code;
926 x86_branch8 (code, X86_CC_NZ, -1 , 1);
927 /* if yes, set synchronization->owner to null and return */
928 x86_mov_membase_imm (code, X86_ECX, owner_offset, 0, 4);
931 /* next case: synchronization->nest is not 1 */
932 x86_patch (jump_next, code);
933 /* decrease synchronization->nest and return */
934 x86_dec_membase (code, X86_ECX, nest_offset);
937 /* push obj and jump to the actual trampoline */
938 x86_patch (jump_obj_null, code);
939 x86_patch (jump_have_waiters, code);
940 x86_patch (jump_not_owned, code);
941 x86_patch (jump_sync_null, code);
944 /* push obj and jump to the actual trampoline */
945 x86_push_reg (code, X86_EAX);
947 code = mono_arch_emit_load_aotconst (buf, code, &ji, MONO_PATCH_INFO_JIT_ICALL_ADDR, "generic_trampoline_monitor_exit");
948 x86_jump_reg (code, X86_EAX);
950 x86_jump_code (code, tramp);
953 mono_arch_flush_icache (buf, code - buf);
954 g_assert (code - buf <= tramp_size);
957 *info = mono_tramp_info_create (g_strdup_printf ("monitor_exit_trampoline"), buf, code - buf, ji, unwind_ops);
959 if (mono_jit_map_is_enabled ())
960 mono_emit_jit_tramp (buf, code - buf, "monitor_exit_trampoline");
968 mono_arch_create_monitor_enter_trampoline (MonoTrampInfo **info, gboolean aot)
970 g_assert_not_reached ();
975 mono_arch_create_monitor_exit_trampoline (MonoTrampInfo **info, gboolean aot)
977 g_assert_not_reached ();
984 mono_arch_invalidate_method (MonoJitInfo *ji, void *func, gpointer func_arg)
986 /* FIXME: This is not thread safe */
987 guint8 *code = ji->code_start;
989 x86_push_imm (code, func_arg);
990 x86_call_code (code, (guint8*)func);
994 handler_block_trampoline_helper (gpointer *ptr)
996 MonoJitTlsData *jit_tls = TlsGetValue (mono_jit_tls_id);
997 *ptr = jit_tls->handler_block_return_address;
1001 mono_arch_create_handler_block_trampoline (void)
1003 guint8 *tramp = mono_get_trampoline_code (MONO_TRAMPOLINE_HANDLER_BLOCK_GUARD);
1005 int tramp_size = 64;
1006 code = buf = mono_global_codeman_reserve (tramp_size);
1009 This trampoline restore the call chain of the handler block then jumps into the code that deals with it.
1012 if (mono_get_jit_tls_offset () != -1) {
1013 code = mono_x86_emit_tls_get (code, X86_EAX, mono_get_jit_tls_offset ());
1014 x86_mov_reg_membase (code, X86_EAX, X86_EAX, G_STRUCT_OFFSET (MonoJitTlsData, handler_block_return_address), 4);
1016 x86_push_reg (code, X86_EAX);
1017 x86_jump_code (code, tramp);
1019 /*Slow path uses a c helper*/
1020 x86_push_reg (code, X86_ESP);
1021 x86_push_imm (code, tramp);
1022 x86_jump_code (code, handler_block_trampoline_helper);
1025 mono_arch_flush_icache (buf, code - buf);
1026 g_assert (code - buf <= tramp_size);
1028 if (mono_jit_map_is_enabled ())
1029 mono_emit_jit_tramp (buf, code - buf, "handler_block_trampoline");
1035 mono_arch_get_call_target (guint8 *code)
1037 if (code [-5] == 0xe8) {
1038 guint32 disp = *(guint32*)(code - 4);
1039 guint8 *target = code + disp;
1048 mono_arch_get_plt_info_offset (guint8 *plt_entry, mgreg_t *regs, guint8 *code)
1050 return *(guint32*)(plt_entry + NACL_SIZE (6, 12));