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/metadata/gc-internal.h>
21 #include <mono/arch/x86/x86-codegen.h>
23 #include <mono/utils/memcheck.h>
29 * mono_arch_get_unbox_trampoline:
31 * @addr: pointer to native code for @m
33 * when value type methods are called through the vtable we need to unbox the
34 * this argument. This method returns a pointer to a trampoline which does
35 * unboxing before calling the method
38 mono_arch_get_unbox_trampoline (MonoMethod *m, gpointer addr)
41 int this_pos = 4, size = NACL_SIZE(16, 32);
42 MonoDomain *domain = mono_domain_get ();
44 start = code = mono_domain_code_reserve (domain, size);
46 x86_alu_membase_imm (code, X86_ADD, X86_ESP, this_pos, sizeof (MonoObject));
47 x86_jump_code (code, addr);
48 g_assert ((code - start) < size);
50 nacl_domain_code_validate (domain, &start, size, &code);
56 mono_arch_get_static_rgctx_trampoline (MonoMethod *m, MonoMethodRuntimeGenericContext *mrgctx, gpointer addr)
61 MonoDomain *domain = mono_domain_get ();
63 buf_len = NACL_SIZE (10, 32);
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 nacl_domain_code_validate (domain, &start, buf_len, &code);
72 mono_arch_flush_icache (start, code - start);
78 mono_arch_get_llvm_imt_trampoline (MonoDomain *domain, MonoMethod *m, int vt_offset)
86 start = code = mono_domain_code_reserve (domain, buf_len);
88 this_offset = mono_x86_get_this_arg_offset (NULL, mono_method_signature (m));
91 x86_mov_reg_imm (code, MONO_ARCH_IMT_REG, m);
93 x86_mov_reg_membase (code, X86_EAX, X86_ESP, this_offset + 4, 4);
94 /* Load vtable address */
95 x86_mov_reg_membase (code, X86_EAX, X86_EAX, 0, 4);
96 x86_jump_membase (code, X86_EAX, vt_offset);
98 g_assert ((code - start) < buf_len);
100 nacl_domain_code_validate (domain, &start, buf_len, &code);
102 mono_arch_flush_icache (start, code - start);
108 mono_arch_patch_callsite (guint8 *method_start, guint8 *orig_code, guint8 *addr)
110 #if defined(__default_codegen__)
113 gboolean can_write = mono_breakpoint_clean_code (method_start, orig_code, 8, buf, sizeof (buf));
117 /* go to the start of the call instruction
119 * address_byte = (m << 6) | (o << 3) | reg
120 * call opcode: 0xff address_byte displacement
126 if (code [1] == 0xe8) {
128 InterlockedExchange ((gint32*)(orig_code + 2), (guint)addr - ((guint)orig_code + 1) - 5);
130 /* Tell valgrind to recompile the patched code */
131 VALGRIND_DISCARD_TRANSLATIONS (orig_code + 2, 4);
133 } else if (code [1] == 0xe9) {
134 /* A PLT entry: jmp <DISP> */
136 InterlockedExchange ((gint32*)(orig_code + 2), (guint)addr - ((guint)orig_code + 1) - 5);
138 printf ("Invalid trampoline sequence: %x %x %x %x %x %x %x\n", code [0], code [1], code [2], code [3],
139 code [4], code [5], code [6]);
140 g_assert_not_reached ();
142 #elif defined(__native_client__)
143 /* Target must be bundle-aligned */
144 g_assert (((guint32)addr & kNaClAlignmentMask) == 0);
146 /* 0xe8 = call <DISP>, 0xe9 = jump <DISP> */
147 if ((orig_code [-5] == 0xe8) || orig_code [-6] == 0xe9) {
149 gint32 offset = (gint32)addr - (gint32)orig_code;
150 guint8 buf[sizeof(gint32)];
151 *((gint32*)(buf)) = offset;
152 ret = nacl_dyncode_modify (orig_code - sizeof(gint32), buf, sizeof(gint32));
155 printf ("Invalid trampoline sequence %p: %02x %02x %02x %02x %02x\n", orig_code, orig_code [-5], orig_code [-4], orig_code [-3], orig_code [-2], orig_code[-1]);
156 g_assert_not_reached ();
162 mono_arch_patch_plt_entry (guint8 *code, gpointer *got, mgreg_t *regs, guint8 *addr)
166 /* Patch the jump table entry used by the plt entry */
168 #if defined(__native_client_codegen__) || defined(__native_client__)
169 /* for both compiler and runtime */
171 /* mov <DISP>(%ebx), %ecx */
172 /* and 0xffffffe0, %ecx */
174 g_assert (code [0] == 0x8b);
175 g_assert (code [1] == 0x8b);
177 offset = *(guint32*)(code + 2);
178 #elif defined(__default_codegen__)
179 /* A PLT entry: jmp *<DISP>(%ebx) */
180 g_assert (code [0] == 0xff);
181 g_assert (code [1] == 0xa3);
183 offset = *(guint32*)(code + 2);
184 #endif /* __native_client_codegen__ */
186 got = (gpointer*)(gsize) regs [MONO_ARCH_GOT_REG];
187 *(guint8**)((guint8*)got + offset) = addr;
191 get_vcall_slot (guint8 *code, mgreg_t *regs, int *displacement)
193 const int kBufSize = NACL_SIZE (8, 16);
198 mono_breakpoint_clean_code (NULL, code, kBufSize, buf, kBufSize);
203 if ((code [0] == 0xff) && ((code [1] & 0x18) == 0x10) && ((code [1] >> 6) == 2)) {
204 reg = code [1] & 0x07;
205 disp = *((gint32*)(code + 2));
206 #if defined(__native_client_codegen__) || defined(__native_client__)
207 } else if ((code[1] == 0x83) && (code[2] == 0xe1) && (code[4] == 0xff) &&
208 (code[5] == 0xd1) && (code[-5] == 0x8b)) {
209 disp = *((gint32*)(code - 3));
210 reg = code[-4] & 0x07;
211 } else if ((code[-2] == 0x8b) && (code[1] == 0x83) && (code[4] == 0xff)) {
212 reg = code[-1] & 0x07;
213 disp = (signed char)code[0];
216 g_assert_not_reached ();
220 *displacement = disp;
221 return (gpointer)regs [reg];
225 get_vcall_slot_addr (guint8* code, mgreg_t *regs)
229 vt = get_vcall_slot (code, regs, &displacement);
232 return (gpointer*)((char*)vt + displacement);
236 mono_arch_nullify_class_init_trampoline (guint8 *code, mgreg_t *regs)
239 gboolean can_write = mono_breakpoint_clean_code (NULL, code, 6, buf, sizeof (buf));
240 gpointer tramp = mini_get_nullified_class_init_trampoline ();
246 if (code [0] == 0xe8) {
247 #if defined(__default_codegen__)
248 if (!mono_running_on_valgrind ()) {
251 * Thread safe code patching using the algorithm from the paper
252 * 'Practicing JUDO: Java Under Dynamic Optimizations'
255 * First atomically change the the first 2 bytes of the call to a
259 InterlockedExchange ((gint32*)code, ops);
261 /* Then change the other bytes to a nop */
266 /* Then atomically change the first 4 bytes to a nop as well */
268 InterlockedExchange ((gint32*)code, ops);
269 /* FIXME: the calltree skin trips on the self modifying code above */
271 /* Tell valgrind to recompile the patched code */
272 //VALGRIND_DISCARD_TRANSLATIONS (code, 8);
274 #elif defined(__native_client_codegen__)
275 mono_arch_patch_callsite (code, code + 5, tramp);
277 } else if (code [0] == 0x90 || code [0] == 0xeb) {
278 /* Already changed by another thread */
280 } else if ((code [-1] == 0xff) && (x86_modrm_reg (code [0]) == 0x2)) {
281 /* call *<OFFSET>(<REG>) -> Call made from AOT code */
282 gpointer *vtable_slot;
284 vtable_slot = get_vcall_slot_addr (code + 5, regs);
285 g_assert (vtable_slot);
287 *vtable_slot = tramp;
289 printf ("Invalid trampoline sequence: %x %x %x %x %x %x %x\n", code [0], code [1], code [2], code [3],
290 code [4], code [5], code [6]);
291 g_assert_not_reached ();
296 mono_arch_nullify_plt_entry (guint8 *code, mgreg_t *regs)
298 mono_arch_patch_plt_entry (code, NULL, regs, mini_get_nullified_class_init_trampoline ());
302 mono_arch_create_generic_trampoline (MonoTrampolineType tramp_type, MonoTrampInfo **info, gboolean aot)
305 guint8 *buf, *code, *tramp;
306 int pushed_args, pushed_args_caller_saved;
307 GSList *unwind_ops = NULL;
308 MonoJumpInfo *ji = NULL;
310 unwind_ops = mono_arch_get_cie_program ();
312 code = buf = mono_global_codeman_reserve (256);
314 /* Note that there is a single argument to the trampoline
315 * and it is stored at: esp + pushed_args * sizeof (gpointer)
316 * the ret address is at: esp + (pushed_args + 1) * sizeof (gpointer)
319 /* Put all registers into an array on the stack
320 * If this code is changed, make sure to update the offset value in
321 * mono_arch_get_this_arg_from_call () in mini-x86.c.
323 x86_push_reg (code, X86_EDI);
324 x86_push_reg (code, X86_ESI);
325 x86_push_reg (code, X86_EBP);
326 x86_push_reg (code, X86_ESP);
327 x86_push_reg (code, X86_EBX);
328 x86_push_reg (code, X86_EDX);
329 x86_push_reg (code, X86_ECX);
330 x86_push_reg (code, X86_EAX);
332 pushed_args_caller_saved = pushed_args = 8;
334 /* Align stack on apple */
335 x86_alu_reg_imm (code, X86_SUB, X86_ESP, 4);
341 /* save the IP (caller ip) */
342 if (tramp_type == MONO_TRAMPOLINE_JUMP)
343 x86_push_imm (code, 0);
345 x86_push_membase (code, X86_ESP, (pushed_args + 1) * sizeof (gpointer));
349 x86_push_reg (code, X86_EBP);
350 x86_push_reg (code, X86_ESI);
351 x86_push_reg (code, X86_EDI);
352 x86_push_reg (code, X86_EBX);
357 x86_push_reg (code, X86_ESP);
358 /* Adjust ESP so it points to the previous frame */
359 x86_alu_membase_imm (code, X86_ADD, X86_ESP, 0, (pushed_args + 2) * 4);
363 /* save method info */
364 if ((tramp_type == MONO_TRAMPOLINE_JIT) || (tramp_type == MONO_TRAMPOLINE_JUMP))
365 x86_push_membase (code, X86_ESP, pushed_args * sizeof (gpointer));
367 x86_push_imm (code, 0);
371 /* On apple, the stack is correctly aligned to 16 bytes because pushed_args is
372 * 16 and there is the extra trampoline arg + the return ip pushed by call
373 * FIXME: Note that if an exception happens while some args are pushed
374 * on the stack, the stack will be misaligned.
376 g_assert (pushed_args == 16);
378 /* get the address of lmf for the current thread */
380 code = mono_arch_emit_load_aotconst (buf, code, &ji, MONO_PATCH_INFO_JIT_ICALL_ADDR, "mono_get_lmf_addr");
381 x86_call_reg (code, X86_EAX);
383 x86_call_code (code, mono_get_lmf_addr);
386 x86_push_reg (code, X86_EAX);
387 /* push *lfm (previous_lmf) */
388 x86_push_membase (code, X86_EAX, 0);
389 /* Signal to mono_arch_find_jit_info () that this is a trampoline frame */
390 x86_alu_membase_imm (code, X86_ADD, X86_ESP, 0, 1);
392 x86_mov_membase_reg (code, X86_EAX, 0, X86_ESP, 4);
397 /* starting the call sequence */
399 /* FIXME: Push the trampoline address */
400 x86_push_imm (code, 0);
404 /* push the method info */
405 x86_push_membase (code, X86_ESP, pushed_args * sizeof (gpointer));
409 /* push the return address onto the stack */
410 if (tramp_type == MONO_TRAMPOLINE_JUMP)
411 x86_push_imm (code, 0);
413 x86_push_membase (code, X86_ESP, (pushed_args + 1) * sizeof (gpointer));
415 /* push the address of the register array */
416 x86_lea_membase (code, X86_EAX, X86_ESP, (pushed_args - 8) * sizeof (gpointer));
417 x86_push_reg (code, X86_EAX);
422 /* check the stack is aligned after the ret ip is pushed */
423 /*x86_mov_reg_reg (buf, X86_EDX, X86_ESP, 4);
424 x86_alu_reg_imm (buf, X86_AND, X86_EDX, 15);
425 x86_alu_reg_imm (buf, X86_CMP, X86_EDX, 0);
426 x86_branch_disp (buf, X86_CC_Z, 3, FALSE);
427 x86_breakpoint (buf);*/
430 mono_add_unwind_op_def_cfa (unwind_ops, code, buf, X86_ESP, ((pushed_args + 2) * 4));
433 char *icall_name = g_strdup_printf ("trampoline_func_%d", tramp_type);
434 code = mono_arch_emit_load_aotconst (buf, code, &ji, MONO_PATCH_INFO_JIT_ICALL_ADDR, icall_name);
435 x86_call_reg (code, X86_EAX);
437 tramp = (guint8*)mono_get_trampoline_func (tramp_type);
438 x86_call_code (code, tramp);
441 x86_alu_reg_imm (code, X86_ADD, X86_ESP, 4*4);
445 /* Check for thread interruption */
446 /* This is not perf critical code so no need to check the interrupt flag */
447 /* Align the stack on osx */
448 x86_alu_reg_imm (code, X86_SUB, X86_ESP, 3 * 4);
449 x86_push_reg (code, X86_EAX);
451 code = mono_arch_emit_load_aotconst (buf, code, &ji, MONO_PATCH_INFO_JIT_ICALL_ADDR, "mono_thread_force_interruption_checkpoint");
452 x86_call_reg (code, X86_EAX);
454 x86_call_code (code, (guint8*)mono_thread_force_interruption_checkpoint);
456 x86_pop_reg (code, X86_EAX);
457 x86_alu_reg_imm (code, X86_ADD, X86_ESP, 3 * 4);
461 /* ebx = previous_lmf */
462 x86_pop_reg (code, X86_EBX);
464 x86_alu_reg_imm (code, X86_SUB, X86_EBX, 1);
467 x86_pop_reg (code, X86_EDI);
470 /* *(lmf) = previous_lmf */
471 x86_mov_membase_reg (code, X86_EDI, 0, X86_EBX, 4);
473 /* discard method info */
474 x86_pop_reg (code, X86_ESI);
478 x86_pop_reg (code, X86_ESI);
481 /* restore caller saved regs */
482 x86_pop_reg (code, X86_EBX);
483 x86_pop_reg (code, X86_EDI);
484 x86_pop_reg (code, X86_ESI);
485 x86_pop_reg (code, X86_EBP);
489 /* discard save IP */
490 x86_alu_reg_imm (code, X86_ADD, X86_ESP, 4);
493 /* restore LMF end */
495 if (!MONO_TRAMPOLINE_TYPE_MUST_RETURN (tramp_type)) {
497 * Overwrite the method ptr with the address we need to jump to,
500 x86_mov_membase_reg (code, X86_ESP, pushed_args * sizeof (gpointer), X86_EAX, 4);
503 /* Restore caller saved registers */
504 x86_mov_reg_membase (code, X86_ECX, X86_ESP, (pushed_args - pushed_args_caller_saved + X86_ECX) * 4, 4);
505 x86_mov_reg_membase (code, X86_EDX, X86_ESP, (pushed_args - pushed_args_caller_saved + X86_EDX) * 4, 4);
506 if ((tramp_type == MONO_TRAMPOLINE_RESTORE_STACK_PROT) || (tramp_type == MONO_TRAMPOLINE_AOT_PLT))
507 x86_mov_reg_membase (code, X86_EAX, X86_ESP, (pushed_args - pushed_args_caller_saved + X86_EAX) * 4, 4);
509 if (!MONO_TRAMPOLINE_TYPE_MUST_RETURN (tramp_type)) {
510 /* Pop saved reg array + stack align */
511 x86_alu_reg_imm (code, X86_ADD, X86_ESP, 9 * 4);
513 g_assert (pushed_args == 0);
515 /* Pop saved reg array + stack align + method ptr */
516 x86_alu_reg_imm (code, X86_ADD, X86_ESP, 10 * 4);
519 /* We've popped one more stack item than we've pushed (the
520 method ptr argument), so we must end up at -1. */
521 g_assert (pushed_args == -1);
524 /*block guard trampolines are called with the stack aligned but must exit with the stack unaligned. */
525 if (tramp_type == MONO_TRAMPOLINE_HANDLER_BLOCK_GUARD) {
526 x86_pop_reg (code, X86_EAX);
527 x86_alu_reg_imm (code, X86_ADD, X86_ESP, 0x8);
528 x86_jump_reg (code, X86_EAX);
533 nacl_global_codeman_validate (&buf, 256, &code);
534 g_assert ((code - buf) <= 256);
537 tramp_name = mono_get_generic_trampoline_name (tramp_type);
538 *info = mono_tramp_info_create (tramp_name, buf, code - buf, ji, unwind_ops);
546 mono_arch_get_nullified_class_init_trampoline (MonoTrampInfo **info)
549 int tramp_size = NACL_SIZE (16, kNaClAlignment);
551 code = buf = mono_global_codeman_reserve (tramp_size);
554 nacl_global_codeman_validate (&buf, tramp_size, &code);
556 mono_arch_flush_icache (buf, code - buf);
559 *info = mono_tramp_info_create ("nullified_class_init_trampoline", buf, code - buf, NULL, NULL);
564 #define TRAMPOLINE_SIZE 10
567 mono_arch_create_specific_trampoline (gpointer arg1, MonoTrampolineType tramp_type, MonoDomain *domain, guint32 *code_len)
569 guint8 *code, *buf, *tramp;
571 tramp = mono_get_trampoline_code (tramp_type);
573 code = buf = mono_domain_code_reserve_align (domain, TRAMPOLINE_SIZE, NACL_SIZE (4, kNaClAlignment));
575 x86_push_imm (buf, arg1);
576 x86_jump_code (buf, tramp);
577 g_assert ((buf - code) <= TRAMPOLINE_SIZE);
579 nacl_domain_code_validate (domain, &code, NACL_SIZE (4, kNaClAlignment), &buf);
581 mono_arch_flush_icache (code, buf - code);
584 *code_len = buf - code;
590 mono_arch_create_rgctx_lazy_fetch_trampoline (guint32 slot, MonoTrampInfo **info, gboolean aot)
594 guint8 **rgctx_null_jumps;
599 MonoJumpInfo *ji = NULL;
600 GSList *unwind_ops = NULL;
602 unwind_ops = mono_arch_get_cie_program ();
604 mrgctx = MONO_RGCTX_SLOT_IS_MRGCTX (slot);
605 index = MONO_RGCTX_SLOT_INDEX (slot);
607 index += MONO_SIZEOF_METHOD_RUNTIME_GENERIC_CONTEXT / sizeof (gpointer);
608 for (depth = 0; ; ++depth) {
609 int size = mono_class_rgctx_get_array_size (depth, mrgctx);
611 if (index < size - 1)
616 #if defined(__default_codegen__)
617 tramp_size = (aot ? 64 : 36) + 6 * depth;
618 #elif defined(__native_client_codegen__)
619 tramp_size = (aot ? 64 : 36) + 2 * kNaClAlignment +
620 6 * (depth + kNaClAlignment);
623 code = buf = mono_global_codeman_reserve (tramp_size);
625 rgctx_null_jumps = g_malloc (sizeof (guint8*) * (depth + 2));
627 /* load vtable/mrgctx ptr */
628 x86_mov_reg_membase (code, X86_EAX, X86_ESP, 4, 4);
630 /* load rgctx ptr from vtable */
631 x86_mov_reg_membase (code, X86_EAX, X86_EAX, G_STRUCT_OFFSET (MonoVTable, runtime_generic_context), 4);
632 /* is the rgctx ptr null? */
633 x86_test_reg_reg (code, X86_EAX, X86_EAX);
634 /* if yes, jump to actual trampoline */
635 rgctx_null_jumps [0] = code;
636 x86_branch8 (code, X86_CC_Z, -1, 1);
639 for (i = 0; i < depth; ++i) {
640 /* load ptr to next array */
641 if (mrgctx && i == 0)
642 x86_mov_reg_membase (code, X86_EAX, X86_EAX, MONO_SIZEOF_METHOD_RUNTIME_GENERIC_CONTEXT, 4);
644 x86_mov_reg_membase (code, X86_EAX, X86_EAX, 0, 4);
645 /* is the ptr null? */
646 x86_test_reg_reg (code, X86_EAX, X86_EAX);
647 /* if yes, jump to actual trampoline */
648 rgctx_null_jumps [i + 1] = code;
649 x86_branch8 (code, X86_CC_Z, -1, 1);
653 x86_mov_reg_membase (code, X86_EAX, X86_EAX, sizeof (gpointer) * (index + 1), 4);
654 /* is the slot null? */
655 x86_test_reg_reg (code, X86_EAX, X86_EAX);
656 /* if yes, jump to actual trampoline */
657 rgctx_null_jumps [depth + 1] = code;
658 x86_branch8 (code, X86_CC_Z, -1, 1);
659 /* otherwise return */
662 for (i = mrgctx ? 1 : 0; i <= depth + 1; ++i)
663 x86_patch (rgctx_null_jumps [i], code);
665 g_free (rgctx_null_jumps);
667 x86_mov_reg_membase (code, MONO_ARCH_VTABLE_REG, X86_ESP, 4, 4);
670 code = mono_arch_emit_load_aotconst (buf, code, &ji, MONO_PATCH_INFO_JIT_ICALL_ADDR, g_strdup_printf ("specific_trampoline_lazy_fetch_%u", slot));
671 x86_jump_reg (code, X86_EAX);
673 tramp = mono_arch_create_specific_trampoline (GUINT_TO_POINTER (slot), MONO_TRAMPOLINE_RGCTX_LAZY_FETCH, mono_get_root_domain (), NULL);
675 /* jump to the actual trampoline */
676 x86_jump_code (code, tramp);
679 nacl_global_codeman_validate (&buf, tramp_size, &code);
680 mono_arch_flush_icache (buf, code - buf);
682 g_assert (code - buf <= tramp_size);
685 char *name = mono_get_rgctx_fetch_trampoline_name (slot);
686 *info = mono_tramp_info_create (name, buf, code - buf, ji, unwind_ops);
694 * mono_arch_create_general_rgctx_lazy_fetch_trampoline:
696 * This is a general variant of the rgctx fetch trampolines. It receives a pointer to gpointer[2] in the rgctx reg. The first entry contains the slot, the second
697 * the trampoline to call if the slot is not filled.
700 mono_arch_create_general_rgctx_lazy_fetch_trampoline (MonoTrampInfo **info, gboolean aot)
704 MonoJumpInfo *ji = NULL;
705 GSList *unwind_ops = NULL;
709 unwind_ops = mono_arch_get_cie_program ();
713 code = buf = mono_global_codeman_reserve (tramp_size);
715 // FIXME: Currently, we always go to the slow path.
717 /* Load trampoline addr */
718 x86_mov_reg_membase (code, X86_EAX, MONO_ARCH_RGCTX_REG, 4, 4);
719 /* Load mrgctx/vtable */
720 x86_mov_reg_membase (code, MONO_ARCH_VTABLE_REG, X86_ESP, 4, 4);
722 x86_jump_reg (code, X86_EAX);
724 nacl_global_codeman_validate (&buf, tramp_size, &code);
725 mono_arch_flush_icache (buf, code - buf);
727 g_assert (code - buf <= tramp_size);
730 *info = mono_tramp_info_create ("rgctx_fetch_trampoline_general", buf, code - buf, ji, unwind_ops);
736 mono_arch_create_generic_class_init_trampoline (MonoTrampInfo **info, gboolean aot)
740 static int byte_offset = -1;
741 static guint8 bitmask;
744 GSList *unwind_ops = NULL;
745 MonoJumpInfo *ji = NULL;
749 code = buf = mono_global_codeman_reserve (tramp_size);
751 unwind_ops = mono_arch_get_cie_program ();
754 mono_marshal_find_bitfield_offset (MonoVTable, initialized, &byte_offset, &bitmask);
756 x86_test_membase_imm (code, MONO_ARCH_VTABLE_REG, byte_offset, bitmask);
758 x86_branch8 (code, X86_CC_Z, -1, 1);
762 x86_patch (jump, code);
764 /* Push the vtable so the stack is the same as in a specific trampoline */
765 x86_push_reg (code, MONO_ARCH_VTABLE_REG);
768 code = mono_arch_emit_load_aotconst (buf, code, &ji, MONO_PATCH_INFO_JIT_ICALL_ADDR, "generic_trampoline_generic_class_init");
769 x86_jump_reg (code, X86_EAX);
771 tramp = mono_get_trampoline_code (MONO_TRAMPOLINE_GENERIC_CLASS_INIT);
773 /* jump to the actual trampoline */
774 x86_jump_code (code, tramp);
777 mono_arch_flush_icache (code, code - buf);
779 g_assert (code - buf <= tramp_size);
780 #ifdef __native_client_codegen__
781 g_assert (code - buf <= kNaClAlignment);
784 nacl_global_codeman_validate (&buf, tramp_size, &code);
787 *info = mono_tramp_info_create ("generic_class_init_trampoline", buf, code - buf, ji, unwind_ops);
792 #ifdef MONO_ARCH_MONITOR_OBJECT_REG
794 * The code produced by this trampoline is equivalent to this:
797 * if (obj->synchronisation) {
798 * if (obj->synchronisation->owner == 0) {
799 * if (cmpxch (&obj->synchronisation->owner, TID, 0) == 0)
802 * if (obj->synchronisation->owner == TID) {
803 * ++obj->synchronisation->nest;
808 * return full_monitor_enter ();
812 mono_arch_create_monitor_enter_trampoline (MonoTrampInfo **info, gboolean aot)
814 guint8 *tramp = mono_get_trampoline_code (MONO_TRAMPOLINE_MONITOR_ENTER);
816 guint8 *jump_obj_null, *jump_sync_null, *jump_other_owner, *jump_cmpxchg_failed, *jump_tid, *jump_sync_thin_hash = NULL;
818 int owner_offset, nest_offset, dummy;
819 MonoJumpInfo *ji = NULL;
820 GSList *unwind_ops = NULL;
822 g_assert (MONO_ARCH_MONITOR_OBJECT_REG == X86_EAX);
824 mono_monitor_threads_sync_members_offset (&owner_offset, &nest_offset, &dummy);
825 g_assert (MONO_THREADS_SYNC_MEMBER_SIZE (owner_offset) == sizeof (gpointer));
826 g_assert (MONO_THREADS_SYNC_MEMBER_SIZE (nest_offset) == sizeof (guint32));
827 owner_offset = MONO_THREADS_SYNC_MEMBER_OFFSET (owner_offset);
828 nest_offset = MONO_THREADS_SYNC_MEMBER_OFFSET (nest_offset);
830 tramp_size = NACL_SIZE (96, 128);
832 code = buf = mono_global_codeman_reserve (tramp_size);
834 if (mono_thread_get_tls_offset () != -1) {
835 /* MonoObject* obj is in EAX */
837 x86_test_reg_reg (code, X86_EAX, X86_EAX);
838 /* if yes, jump to actual trampoline */
839 jump_obj_null = code;
840 x86_branch8 (code, X86_CC_Z, -1, 1);
842 /* load obj->synchronization to ECX */
843 x86_mov_reg_membase (code, X86_ECX, X86_EAX, G_STRUCT_OFFSET (MonoObject, synchronisation), 4);
845 if (mono_gc_is_moving ()) {
846 /*if bit zero is set it's a thin hash*/
847 /*FIXME use testb encoding*/
848 x86_test_reg_imm (code, X86_ECX, 0x01);
849 jump_sync_thin_hash = code;
850 x86_branch8 (code, X86_CC_NE, -1, 1);
852 /*clear bits used by the gc*/
853 x86_alu_reg_imm (code, X86_AND, X86_ECX, ~0x3);
856 /* is synchronization null? */
857 x86_test_reg_reg (code, X86_ECX, X86_ECX);
859 /* if yes, jump to actual trampoline */
860 jump_sync_null = code;
861 x86_branch8 (code, X86_CC_Z, -1, 1);
863 /* load MonoInternalThread* into EDX */
865 /* load_aotconst () puts the result into EAX */
866 x86_mov_reg_reg (code, X86_EDX, X86_EAX, sizeof (mgreg_t));
867 code = mono_arch_emit_load_aotconst (buf, code, &ji, MONO_PATCH_INFO_TLS_OFFSET, GINT_TO_POINTER (TLS_KEY_THREAD));
868 code = mono_x86_emit_tls_get_reg (code, X86_EAX, X86_EAX);
869 x86_xchg_reg_reg (code, X86_EAX, X86_EDX, sizeof (mgreg_t));
871 code = mono_x86_emit_tls_get (code, X86_EDX, mono_thread_get_tls_offset ());
873 /* load TID into EDX */
874 x86_mov_reg_membase (code, X86_EDX, X86_EDX, G_STRUCT_OFFSET (MonoInternalThread, tid), 4);
876 /* is synchronization->owner null? */
877 x86_alu_membase_imm (code, X86_CMP, X86_ECX, owner_offset, 0);
878 /* if not, jump to next case */
880 x86_branch8 (code, X86_CC_NZ, -1, 1);
882 /* if yes, try a compare-exchange with the TID */
883 /* free up register EAX, needed for the zero */
884 x86_push_reg (code, X86_EAX);
886 x86_alu_reg_reg (code, X86_XOR, X86_EAX, X86_EAX);
887 /* compare and exchange */
888 x86_prefix (code, X86_LOCK_PREFIX);
889 x86_cmpxchg_membase_reg (code, X86_ECX, owner_offset, X86_EDX);
890 /* if not successful, jump to actual trampoline */
891 jump_cmpxchg_failed = code;
892 x86_branch8 (code, X86_CC_NZ, -1, 1);
893 /* if successful, pop and return */
894 x86_pop_reg (code, X86_EAX);
897 /* next case: synchronization->owner is not null */
898 x86_patch (jump_tid, code);
899 /* is synchronization->owner == TID? */
900 x86_alu_membase_reg (code, X86_CMP, X86_ECX, owner_offset, X86_EDX);
901 /* if not, jump to actual trampoline */
902 jump_other_owner = code;
903 x86_branch8 (code, X86_CC_NZ, -1, 1);
904 /* if yes, increment nest */
905 x86_inc_membase (code, X86_ECX, nest_offset);
910 x86_patch (jump_obj_null, code);
911 if (jump_sync_thin_hash)
912 x86_patch (jump_sync_thin_hash, code);
913 x86_patch (jump_sync_null, code);
914 x86_patch (jump_other_owner, code);
915 x86_push_reg (code, X86_EAX);
916 /* jump to the actual trampoline */
917 x86_patch (jump_cmpxchg_failed, code);
919 /* We are calling the generic trampoline directly, the argument is pushed
920 * on the stack just like a specific trampoline.
922 code = mono_arch_emit_load_aotconst (buf, code, &ji, MONO_PATCH_INFO_JIT_ICALL_ADDR, "generic_trampoline_monitor_enter");
923 x86_jump_reg (code, X86_EAX);
925 x86_jump_code (code, tramp);
928 /* push obj and jump to the actual trampoline */
929 x86_push_reg (code, X86_EAX);
931 code = mono_arch_emit_load_aotconst (buf, code, &ji, MONO_PATCH_INFO_JIT_ICALL_ADDR, "generic_trampoline_monitor_enter");
932 x86_jump_reg (code, X86_EAX);
934 x86_jump_code (code, tramp);
938 mono_arch_flush_icache (buf, code - buf);
939 g_assert (code - buf <= tramp_size);
941 nacl_global_codeman_validate (&buf, tramp_size, &code);
944 *info = mono_tramp_info_create ("monitor_enter_trampoline", buf, code - buf, ji, unwind_ops);
950 mono_arch_create_monitor_exit_trampoline (MonoTrampInfo **info, gboolean aot)
952 guint8 *tramp = mono_get_trampoline_code (MONO_TRAMPOLINE_MONITOR_EXIT);
954 guint8 *jump_obj_null, *jump_have_waiters, *jump_sync_null, *jump_not_owned, *jump_sync_thin_hash = NULL;
957 int owner_offset, nest_offset, entry_count_offset;
958 MonoJumpInfo *ji = NULL;
959 GSList *unwind_ops = NULL;
961 g_assert (MONO_ARCH_MONITOR_OBJECT_REG == X86_EAX);
963 mono_monitor_threads_sync_members_offset (&owner_offset, &nest_offset, &entry_count_offset);
964 g_assert (MONO_THREADS_SYNC_MEMBER_SIZE (owner_offset) == sizeof (gpointer));
965 g_assert (MONO_THREADS_SYNC_MEMBER_SIZE (nest_offset) == sizeof (guint32));
966 g_assert (MONO_THREADS_SYNC_MEMBER_SIZE (entry_count_offset) == sizeof (gint32));
967 owner_offset = MONO_THREADS_SYNC_MEMBER_OFFSET (owner_offset);
968 nest_offset = MONO_THREADS_SYNC_MEMBER_OFFSET (nest_offset);
969 entry_count_offset = MONO_THREADS_SYNC_MEMBER_OFFSET (entry_count_offset);
971 tramp_size = NACL_SIZE (96, 128);
973 code = buf = mono_global_codeman_reserve (tramp_size);
975 if (mono_thread_get_tls_offset () != -1) {
976 /* MonoObject* obj is in EAX */
978 x86_test_reg_reg (code, X86_EAX, X86_EAX);
979 /* if yes, jump to actual trampoline */
980 jump_obj_null = code;
981 x86_branch8 (code, X86_CC_Z, -1, 1);
983 /* load obj->synchronization to ECX */
984 x86_mov_reg_membase (code, X86_ECX, X86_EAX, G_STRUCT_OFFSET (MonoObject, synchronisation), 4);
986 if (mono_gc_is_moving ()) {
987 /*if bit zero is set it's a thin hash*/
988 /*FIXME use testb encoding*/
989 x86_test_reg_imm (code, X86_ECX, 0x01);
990 jump_sync_thin_hash = code;
991 x86_branch8 (code, X86_CC_NE, -1, 1);
993 /*clear bits used by the gc*/
994 x86_alu_reg_imm (code, X86_AND, X86_ECX, ~0x3);
997 /* is synchronization null? */
998 x86_test_reg_reg (code, X86_ECX, X86_ECX);
999 /* if yes, jump to actual trampoline */
1000 jump_sync_null = code;
1001 x86_branch8 (code, X86_CC_Z, -1, 1);
1003 /* next case: synchronization is not null */
1004 /* load MonoInternalThread* into EDX */
1006 /* load_aotconst () puts the result into EAX */
1007 x86_mov_reg_reg (code, X86_EDX, X86_EAX, sizeof (mgreg_t));
1008 code = mono_arch_emit_load_aotconst (buf, code, &ji, MONO_PATCH_INFO_TLS_OFFSET, GINT_TO_POINTER (TLS_KEY_THREAD));
1009 code = mono_x86_emit_tls_get_reg (code, X86_EAX, X86_EAX);
1010 x86_xchg_reg_reg (code, X86_EAX, X86_EDX, sizeof (mgreg_t));
1012 code = mono_x86_emit_tls_get (code, X86_EDX, mono_thread_get_tls_offset ());
1014 /* load TID into EDX */
1015 x86_mov_reg_membase (code, X86_EDX, X86_EDX, G_STRUCT_OFFSET (MonoInternalThread, tid), 4);
1016 /* is synchronization->owner == TID */
1017 x86_alu_membase_reg (code, X86_CMP, X86_ECX, owner_offset, X86_EDX);
1018 /* if no, jump to actual trampoline */
1019 jump_not_owned = code;
1020 x86_branch8 (code, X86_CC_NZ, -1, 1);
1022 /* next case: synchronization->owner == TID */
1023 /* is synchronization->nest == 1 */
1024 x86_alu_membase_imm (code, X86_CMP, X86_ECX, nest_offset, 1);
1025 /* if not, jump to next case */
1027 x86_branch8 (code, X86_CC_NZ, -1, 1);
1028 /* if yes, is synchronization->entry_count zero? */
1029 x86_alu_membase_imm (code, X86_CMP, X86_ECX, entry_count_offset, 0);
1030 /* if not, jump to actual trampoline */
1031 jump_have_waiters = code;
1032 x86_branch8 (code, X86_CC_NZ, -1 , 1);
1033 /* if yes, set synchronization->owner to null and return */
1034 x86_mov_membase_imm (code, X86_ECX, owner_offset, 0, 4);
1037 /* next case: synchronization->nest is not 1 */
1038 x86_patch (jump_next, code);
1039 /* decrease synchronization->nest and return */
1040 x86_dec_membase (code, X86_ECX, nest_offset);
1043 /* push obj and jump to the actual trampoline */
1044 x86_patch (jump_obj_null, code);
1045 if (jump_sync_thin_hash)
1046 x86_patch (jump_sync_thin_hash, code);
1047 x86_patch (jump_have_waiters, code);
1048 x86_patch (jump_not_owned, code);
1049 x86_patch (jump_sync_null, code);
1052 /* push obj and jump to the actual trampoline */
1053 x86_push_reg (code, X86_EAX);
1055 code = mono_arch_emit_load_aotconst (buf, code, &ji, MONO_PATCH_INFO_JIT_ICALL_ADDR, "generic_trampoline_monitor_exit");
1056 x86_jump_reg (code, X86_EAX);
1058 x86_jump_code (code, tramp);
1061 nacl_global_codeman_validate (&buf, tramp_size, &code);
1063 mono_arch_flush_icache (buf, code - buf);
1064 g_assert (code - buf <= tramp_size);
1067 *info = mono_tramp_info_create ("monitor_exit_trampoline", buf, code - buf, ji, unwind_ops);
1075 mono_arch_create_monitor_enter_trampoline (MonoTrampInfo **info, gboolean aot)
1077 g_assert_not_reached ();
1082 mono_arch_create_monitor_exit_trampoline (MonoTrampInfo **info, gboolean aot)
1084 g_assert_not_reached ();
1091 mono_arch_invalidate_method (MonoJitInfo *ji, void *func, gpointer func_arg)
1093 /* FIXME: This is not thread safe */
1094 guint8 *code = ji->code_start;
1096 x86_push_imm (code, func_arg);
1097 x86_call_code (code, (guint8*)func);
1101 handler_block_trampoline_helper (void)
1103 MonoJitTlsData *jit_tls = mono_native_tls_get_value (mono_jit_tls_id);
1104 return jit_tls->handler_block_return_address;
1108 mono_arch_create_handler_block_trampoline (MonoTrampInfo **info, gboolean aot)
1110 guint8 *tramp = mono_get_trampoline_code (MONO_TRAMPOLINE_HANDLER_BLOCK_GUARD);
1112 int tramp_size = 64;
1113 MonoJumpInfo *ji = NULL;
1114 GSList *unwind_ops = NULL;
1118 code = buf = mono_global_codeman_reserve (tramp_size);
1121 This trampoline restore the call chain of the handler block then jumps into the code that deals with it.
1125 * We are in a method frame after the call emitted by OP_CALL_HANDLER.
1128 if (mono_get_jit_tls_offset () != -1) {
1129 code = mono_x86_emit_tls_get (code, X86_EAX, mono_get_jit_tls_offset ());
1130 x86_mov_reg_membase (code, X86_EAX, X86_EAX, G_STRUCT_OFFSET (MonoJitTlsData, handler_block_return_address), 4);
1132 /*Slow path uses a c helper*/
1133 x86_call_code (code, handler_block_trampoline_helper);
1135 /* Simulate a call */
1136 /*Fix stack alignment*/
1137 x86_alu_reg_imm (code, X86_SUB, X86_ESP, 0x4);
1138 /* This is the address the trampoline will return to */
1139 x86_push_reg (code, X86_EAX);
1140 /* Dummy trampoline argument, since we call the generic trampoline directly */
1141 x86_push_imm (code, 0);
1142 x86_jump_code (code, tramp);
1144 nacl_global_codeman_validate (&buf, tramp_size, &code);
1146 mono_arch_flush_icache (buf, code - buf);
1147 g_assert (code - buf <= tramp_size);
1150 *info = mono_tramp_info_create ("handler_block_trampoline", buf, code - buf, ji, unwind_ops);
1156 mono_arch_get_call_target (guint8 *code)
1158 if (code [-5] == 0xe8) {
1159 guint32 disp = *(guint32*)(code - 4);
1160 guint8 *target = code + disp;
1169 mono_arch_get_plt_info_offset (guint8 *plt_entry, mgreg_t *regs, guint8 *code)
1171 return *(guint32*)(plt_entry + NACL_SIZE (6, 12));
1175 * mono_arch_get_gsharedvt_arg_trampoline:
1177 * Return a trampoline which passes ARG to the gsharedvt in/out trampoline ADDR.
1180 mono_arch_get_gsharedvt_arg_trampoline (MonoDomain *domain, gpointer arg, gpointer addr)
1182 guint8 *code, *start;
1187 start = code = mono_domain_code_reserve (domain, buf_len);
1189 x86_mov_reg_imm (code, X86_EAX, arg);
1190 x86_jump_code (code, addr);
1191 g_assert ((code - start) <= buf_len);
1193 nacl_domain_code_validate (domain, &start, buf_len, &code);
1194 mono_arch_flush_icache (start, code - start);
1199 #if defined(ENABLE_GSHAREDVT)
1201 #include "../../../mono-extensions/mono/mini/tramp-x86-gsharedvt.c"
1206 mono_arch_get_gsharedvt_trampoline (MonoTrampInfo **info, gboolean aot)
1213 #endif /* !MONOTOUCH */