2 * tramp-amd64.c: JIT trampoline code for amd64
5 * Dietmar Maurer (dietmar@ximian.com)
6 * Zoltan Varga (vargaz@gmail.com)
8 * (C) 2001 Ximian, Inc.
9 * Copyright 2003-2011 Novell, Inc (http://www.novell.com)
10 * Copyright 2011 Xamarin, Inc (http://www.xamarin.com)
16 #include <mono/metadata/appdomain.h>
17 #include <mono/metadata/marshal.h>
18 #include <mono/metadata/tabledefs.h>
19 #include <mono/metadata/mono-debug-debugger.h>
20 #include <mono/metadata/monitor.h>
21 #include <mono/metadata/monitor.h>
22 #include <mono/metadata/gc-internal.h>
23 #include <mono/arch/amd64/amd64-codegen.h>
25 #include <mono/utils/memcheck.h>
28 #include "mini-amd64.h"
30 #if defined(__native_client_codegen__) && defined(__native_client__)
32 #include <nacl/nacl_dyncode.h>
35 #define IS_REX(inst) (((inst) >= 0x40) && ((inst) <= 0x4f))
37 static guint8* nullified_class_init_trampoline;
40 * mono_arch_get_unbox_trampoline:
42 * @addr: pointer to native code for @m
44 * when value type methods are called through the vtable we need to unbox the
45 * this argument. This method returns a pointer to a trampoline which does
46 * unboxing before calling the method
49 mono_arch_get_unbox_trampoline (MonoMethod *m, gpointer addr)
52 int this_reg, size = NACL_SIZE (20, 32);
54 MonoDomain *domain = mono_domain_get ();
56 this_reg = mono_arch_get_this_arg_reg (NULL);
58 start = code = mono_domain_code_reserve (domain, size);
60 amd64_alu_reg_imm (code, X86_ADD, this_reg, sizeof (MonoObject));
61 /* FIXME: Optimize this */
62 amd64_mov_reg_imm (code, AMD64_RAX, addr);
63 amd64_jump_reg (code, AMD64_RAX);
64 g_assert ((code - start) < size);
66 nacl_domain_code_validate (domain, &start, size, &code);
68 mono_arch_flush_icache (start, code - start);
74 * mono_arch_get_static_rgctx_trampoline:
76 * Create a trampoline which sets RGCTX_REG to MRGCTX, then jumps to ADDR.
79 mono_arch_get_static_rgctx_trampoline (MonoMethod *m, MonoMethodRuntimeGenericContext *mrgctx, gpointer addr)
84 MonoDomain *domain = mono_domain_get ();
86 #ifdef MONO_ARCH_NOMAP32BIT
89 /* AOTed code could still have a non-32 bit address */
90 if ((((guint64)addr) >> 32) == 0)
91 buf_len = NACL_SIZE (16, 32);
93 buf_len = NACL_SIZE (30, 32);
96 start = code = mono_domain_code_reserve (domain, buf_len);
98 amd64_mov_reg_imm (code, MONO_ARCH_RGCTX_REG, mrgctx);
99 amd64_jump_code (code, addr);
100 g_assert ((code - start) < buf_len);
102 nacl_domain_code_validate (domain, &start, buf_len, &code);
103 mono_arch_flush_icache (start, code - start);
109 mono_arch_get_llvm_imt_trampoline (MonoDomain *domain, MonoMethod *m, int vt_offset)
111 guint8 *code, *start;
117 start = code = mono_domain_code_reserve (domain, buf_len);
119 this_reg = mono_arch_get_this_arg_reg (NULL);
122 amd64_mov_reg_imm (code, MONO_ARCH_IMT_REG, m);
123 /* Load vtable address */
124 amd64_mov_reg_membase (code, AMD64_RAX, this_reg, 0, 8);
125 amd64_jump_membase (code, AMD64_RAX, vt_offset);
128 g_assert ((code - start) < buf_len);
130 nacl_domain_code_validate (domain, &start, buf_len, &code);
132 mono_arch_flush_icache (start, code - start);
138 * mono_arch_patch_callsite:
140 * Patch the callsite whose address is given by ORIG_CODE so it calls ADDR. ORIG_CODE
141 * points to the pc right after the call.
144 mono_arch_patch_callsite (guint8 *method_start, guint8 *orig_code, guint8 *addr)
146 #if defined(__default_codegen__)
149 gboolean can_write = mono_breakpoint_clean_code (method_start, orig_code, 14, buf, sizeof (buf));
153 /* mov 64-bit imm into r11 (followed by call reg?) or direct call*/
154 if (((code [-13] == 0x49) && (code [-12] == 0xbb)) || (code [-5] == 0xe8)) {
155 if (code [-5] != 0xe8) {
157 InterlockedExchangePointer ((gpointer*)(orig_code - 11), addr);
158 VALGRIND_DISCARD_TRANSLATIONS (orig_code - 11, sizeof (gpointer));
161 gboolean disp_32bit = ((((gint64)addr - (gint64)orig_code)) < (1 << 30)) && ((((gint64)addr - (gint64)orig_code)) > -(1 << 30));
163 if ((((guint64)(addr)) >> 32) != 0 && !disp_32bit) {
164 #ifdef MONO_ARCH_NOMAP32BIT
165 /* Print some diagnostics */
166 MonoJitInfo *ji = mono_jit_info_table_find (mono_domain_get (), (char*)orig_code);
168 fprintf (stderr, "At %s, offset 0x%zx\n", mono_method_full_name (ji->method, TRUE), (guint8*)orig_code - (guint8*)ji->code_start);
169 fprintf (stderr, "Addr: %p\n", addr);
170 ji = mono_jit_info_table_find (mono_domain_get (), (char*)addr);
172 fprintf (stderr, "Callee: %s\n", mono_method_full_name (ji->method, TRUE));
173 g_assert_not_reached ();
176 * This might happen when calling AOTed code. Create a thunk.
178 guint8 *thunk_start, *thunk_code;
180 thunk_start = thunk_code = mono_domain_code_reserve (mono_domain_get (), 32);
181 amd64_jump_membase (thunk_code, AMD64_RIP, 0);
182 *(guint64*)thunk_code = (guint64)addr;
184 g_assert ((((guint64)(addr)) >> 32) == 0);
185 mono_arch_flush_icache (thunk_start, thunk_code - thunk_start);
189 InterlockedExchange ((gint32*)(orig_code - 4), ((gint64)addr - (gint64)orig_code));
190 VALGRIND_DISCARD_TRANSLATIONS (orig_code - 5, 4);
194 else if ((code [-7] == 0x41) && (code [-6] == 0xff) && (code [-5] == 0x15)) {
195 /* call *<OFFSET>(%rip) */
196 gpointer *got_entry = (gpointer*)((guint8*)orig_code + (*(guint32*)(orig_code - 4)));
198 InterlockedExchangePointer (got_entry, addr);
199 VALGRIND_DISCARD_TRANSLATIONS (orig_code - 5, sizeof (gpointer));
202 #elif defined(__native_client__)
203 /* These are essentially the same 2 cases as above, modified for NaCl*/
205 /* Target must be bundle-aligned */
206 g_assert (((guint32)addr & kNaClAlignmentMask) == 0);
207 /* Return target must be bundle-aligned */
208 g_assert (((guint32)orig_code & kNaClAlignmentMask) == 0);
210 if (orig_code[-5] == 0xe8) {
213 gint32 offset = (gint32)addr - (gint32)orig_code;
214 guint8 buf[sizeof(gint32)];
215 *((gint32*)(buf)) = offset;
216 ret = nacl_dyncode_modify (orig_code - sizeof(gint32), buf, sizeof(gint32));
220 else if (is_nacl_call_reg_sequence (orig_code - 10) && orig_code[-16] == 0x41 && orig_code[-15] == 0xbb) {
222 guint8 buf[sizeof(gint32)];
223 *((gint32 *)(buf)) = addr;
224 /* orig_code[-14] is the start of the immediate. */
225 ret = nacl_dyncode_modify (orig_code - 14, buf, sizeof(gint32));
229 g_assert_not_reached ();
237 mono_arch_create_llvm_native_thunk (MonoDomain *domain, guint8 *addr)
240 * The caller is LLVM code and the call displacement might exceed 32 bits. We can't determine the caller address, so
241 * we add a thunk every time.
242 * Since the caller is also allocated using the domain code manager, hopefully the displacement will fit into 32 bits.
243 * FIXME: Avoid this if possible if !MONO_ARCH_NOMAP32BIT and ADDR is 32 bits.
245 guint8 *thunk_start, *thunk_code;
247 thunk_start = thunk_code = mono_domain_code_reserve (mono_domain_get (), 32);
248 amd64_jump_membase (thunk_code, AMD64_RIP, 0);
249 *(guint64*)thunk_code = (guint64)addr;
251 mono_arch_flush_icache (thunk_start, thunk_code - thunk_start);
256 mono_arch_patch_plt_entry (guint8 *code, gpointer *got, mgreg_t *regs, guint8 *addr)
259 gpointer *plt_jump_table_entry;
261 #if defined(__default_codegen__)
262 /* A PLT entry: jmp *<DISP>(%rip) */
263 g_assert (code [0] == 0xff);
264 g_assert (code [1] == 0x25);
266 disp = *(gint32*)(code + 2);
268 plt_jump_table_entry = (gpointer*)(code + 6 + disp);
269 #elif defined(__native_client_codegen__)
271 /* mov <DISP>(%rip), %r11d */
274 /* Verify the 'mov' */
275 g_assert (code [0] == 0x45);
276 g_assert (code [1] == 0x8b);
277 g_assert (code [2] == 0x1d);
279 disp = *(gint32*)(code + 3);
281 /* 7 = 3 (mov opcode) + 4 (disp) */
282 /* This needs to resolve to the target of the RIP-relative offset */
283 plt_jump_table_entry = (gpointer*)(code + 7 + disp);
285 #endif /* __native_client_codegen__ */
288 InterlockedExchangePointer (plt_jump_table_entry, addr);
292 get_vcall_slot (guint8 *code, mgreg_t *regs, int *displacement)
296 MonoJitInfo *ji = NULL;
299 /* code - 9 might be before the start of the method */
300 /* FIXME: Avoid this expensive call somehow */
301 ji = mono_jit_info_table_find (mono_domain_get (), (char*)code);
304 mono_breakpoint_clean_code (ji ? ji->code_start : NULL, code, 9, buf, sizeof (buf));
311 if ((code [0] == 0x41) && (code [1] == 0xff) && (code [2] == 0x15)) {
312 /* call OFFSET(%rip) */
313 g_assert_not_reached ();
314 *displacement = *(guint32*)(code + 3);
315 return (gpointer*)(code + disp + 7);
317 g_assert_not_reached ();
323 get_vcall_slot_addr (guint8* code, mgreg_t *regs)
327 vt = get_vcall_slot (code, regs, &displacement);
330 return (gpointer*)((char*)vt + displacement);
334 mono_arch_nullify_class_init_trampoline (guint8 *code, mgreg_t *regs)
337 MonoJitInfo *ji = NULL;
341 /* code - 7 might be before the start of the method */
342 /* FIXME: Avoid this expensive call somehow */
343 ji = mono_jit_info_table_find (mono_domain_get (), (char*)code);
346 can_write = mono_breakpoint_clean_code (ji ? ji->code_start : NULL, code, 7, buf, sizeof (buf));
352 * A given byte sequence can match more than case here, so we have to be
353 * really careful about the ordering of the cases. Longer sequences
356 if ((buf [0] == 0x41) && (buf [1] == 0xff) && (buf [2] == 0x15)) {
357 gpointer *vtable_slot;
359 /* call *<OFFSET>(%rip) */
360 vtable_slot = get_vcall_slot_addr (code, regs);
361 g_assert (vtable_slot);
363 *vtable_slot = nullified_class_init_trampoline;
364 } else if (buf [2] == 0xe8) {
366 //guint8 *buf = code - 2;
369 * It would be better to replace the call with nops, but that doesn't seem
370 * to work on SMP machines even when the whole call is inside a cache line.
371 * Patching the call address seems to work.
381 mono_arch_patch_callsite (code - 5, code, nullified_class_init_trampoline);
382 } else if ((buf [5] == 0xff) && x86_modrm_mod (buf [6]) == 3 && x86_modrm_reg (buf [6]) == 2) {
384 /* Generated by the LLVM JIT or on platforms without MAP_32BIT set */
385 mono_arch_patch_callsite (code - 13, code, nullified_class_init_trampoline);
386 } else if (buf [4] == 0x90 || buf [5] == 0xeb || buf [6] == 0x66) {
387 /* Already changed by another thread */
390 printf ("Invalid trampoline sequence: %x %x %x %x %x %x %x\n", buf [0], buf [1], buf [2], buf [3],
391 buf [4], buf [5], buf [6]);
392 g_assert_not_reached ();
397 mono_arch_nullify_plt_entry (guint8 *code, mgreg_t *regs)
399 if (mono_aot_only && !nullified_class_init_trampoline)
400 nullified_class_init_trampoline = mono_aot_get_trampoline ("nullified_class_init_trampoline");
402 mono_arch_patch_plt_entry (code, NULL, regs, nullified_class_init_trampoline);
406 stack_unaligned (MonoTrampolineType tramp_type)
408 printf ("%d\n", tramp_type);
409 g_assert_not_reached ();
413 mono_arch_create_generic_trampoline (MonoTrampolineType tramp_type, MonoTrampInfo **info, gboolean aot)
416 guint8 *buf, *code, *tramp, *br [2], *r11_save_code, *after_r11_save_code;
417 int i, lmf_offset, offset, res_offset, arg_offset, rax_offset, tramp_offset, saved_regs_offset;
418 int saved_fpregs_offset, rbp_offset, framesize, orig_rsp_to_rbp_offset, cfa_offset;
420 GSList *unwind_ops = NULL;
421 MonoJumpInfo *ji = NULL;
422 const guint kMaxCodeSize = NACL_SIZE (600, 600*2);
424 #if defined(__native_client_codegen__)
425 const guint kNaClTrampOffset = 17;
428 if (tramp_type == MONO_TRAMPOLINE_JUMP || tramp_type == MONO_TRAMPOLINE_HANDLER_BLOCK_GUARD)
433 code = buf = mono_global_codeman_reserve (kMaxCodeSize);
435 framesize = kMaxCodeSize + sizeof (MonoLMF);
436 framesize = (framesize + (MONO_ARCH_FRAME_ALIGNMENT - 1)) & ~ (MONO_ARCH_FRAME_ALIGNMENT - 1);
438 orig_rsp_to_rbp_offset = 0;
439 r11_save_code = code;
440 /* Reserve 5 bytes for the mov_membase_reg to save R11 */
442 after_r11_save_code = code;
444 // CFA = sp + 16 (the trampoline address is on the stack)
446 mono_add_unwind_op_def_cfa (unwind_ops, code, buf, AMD64_RSP, 16);
447 // IP saved at CFA - 8
448 mono_add_unwind_op_offset (unwind_ops, code, buf, AMD64_RIP, -8);
450 /* Pop the return address off the stack */
451 amd64_pop_reg (code, AMD64_R11);
452 orig_rsp_to_rbp_offset += sizeof(mgreg_t);
454 cfa_offset -= sizeof(mgreg_t);
455 mono_add_unwind_op_def_cfa_offset (unwind_ops, code, buf, cfa_offset);
458 * Allocate a new stack frame
460 amd64_push_reg (code, AMD64_RBP);
461 cfa_offset += sizeof(mgreg_t);
462 mono_add_unwind_op_def_cfa_offset (unwind_ops, code, buf, cfa_offset);
463 mono_add_unwind_op_offset (unwind_ops, code, buf, AMD64_RBP, - cfa_offset);
465 orig_rsp_to_rbp_offset -= sizeof(mgreg_t);
466 amd64_mov_reg_reg (code, AMD64_RBP, AMD64_RSP, sizeof(mgreg_t));
467 mono_add_unwind_op_def_cfa_reg (unwind_ops, code, buf, AMD64_RBP);
468 amd64_alu_reg_imm (code, X86_SUB, AMD64_RSP, framesize);
471 rbp_offset = - offset;
473 offset += sizeof(mgreg_t);
474 rax_offset = - offset;
476 offset += sizeof(mgreg_t);
477 tramp_offset = - offset;
479 offset += sizeof(gpointer);
480 arg_offset = - offset;
482 /* Compute the trampoline address from the return address */
484 #if defined(__default_codegen__)
485 /* 7 = length of call *<offset>(rip) */
486 amd64_alu_reg_imm (code, X86_SUB, AMD64_R11, 7);
487 #elif defined(__native_client_codegen__)
488 amd64_alu_reg_imm (code, X86_SUB, AMD64_R11, kNaClTrampOffset);
491 /* 5 = length of amd64_call_membase () */
492 amd64_alu_reg_imm (code, X86_SUB, AMD64_R11, 5);
494 amd64_mov_membase_reg (code, AMD64_RBP, tramp_offset, AMD64_R11, sizeof(gpointer));
496 offset += sizeof(mgreg_t);
497 res_offset = - offset;
499 /* Save all registers */
501 offset += AMD64_NREG * sizeof(mgreg_t);
502 saved_regs_offset = - offset;
503 for (i = 0; i < AMD64_NREG; ++i) {
504 if (i == AMD64_RBP) {
505 /* RAX is already saved */
506 amd64_mov_reg_membase (code, AMD64_RAX, AMD64_RBP, rbp_offset, sizeof(mgreg_t));
507 amd64_mov_membase_reg (code, AMD64_RBP, saved_regs_offset + (i * sizeof(mgreg_t)), AMD64_RAX, sizeof(mgreg_t));
508 } else if (i != AMD64_R11) {
509 amd64_mov_membase_reg (code, AMD64_RBP, saved_regs_offset + (i * sizeof(mgreg_t)), i, sizeof(mgreg_t));
511 /* We have to save R11 right at the start of
512 the trampoline code because it's used as a
514 amd64_mov_membase_reg (r11_save_code, AMD64_RSP, saved_regs_offset + orig_rsp_to_rbp_offset + (i * sizeof(mgreg_t)), i, sizeof(mgreg_t));
515 g_assert (r11_save_code == after_r11_save_code);
518 offset += 8 * sizeof(mgreg_t);
519 saved_fpregs_offset = - offset;
520 for (i = 0; i < 8; ++i)
521 amd64_movsd_membase_reg (code, AMD64_RBP, saved_fpregs_offset + (i * sizeof(mgreg_t)), i);
523 /* Check that the stack is aligned */
524 #if defined(__default_codegen__)
525 amd64_mov_reg_reg (code, AMD64_R11, AMD64_RSP, sizeof (mgreg_t));
526 amd64_alu_reg_imm (code, X86_AND, AMD64_R11, 15);
527 amd64_alu_reg_imm (code, X86_CMP, AMD64_R11, 0);
529 amd64_branch_disp (code, X86_CC_Z, 0, FALSE);
531 amd64_mov_reg_imm (code, AMD64_R11, 0);
532 amd64_mov_reg_membase (code, AMD64_R11, AMD64_R11, 0, 8);
534 amd64_mov_reg_imm (code, AMD64_RDI, tramp_type);
535 amd64_mov_reg_imm (code, AMD64_R11, stack_unaligned);
536 amd64_call_reg (code, AMD64_R11);
538 mono_amd64_patch (br [0], code);
539 //amd64_breakpoint (code);
542 if (tramp_type != MONO_TRAMPOLINE_GENERIC_CLASS_INIT &&
543 tramp_type != MONO_TRAMPOLINE_MONITOR_ENTER &&
544 tramp_type != MONO_TRAMPOLINE_MONITOR_EXIT &&
545 tramp_type != MONO_TRAMPOLINE_HANDLER_BLOCK_GUARD) {
546 /* Obtain the trampoline argument which is encoded in the instruction stream */
548 /* Load the GOT offset */
549 amd64_mov_reg_membase (code, AMD64_R11, AMD64_RBP, tramp_offset, sizeof(gpointer));
550 #if defined(__default_codegen__)
551 amd64_mov_reg_membase (code, AMD64_RAX, AMD64_R11, 7, 4);
552 #elif defined(__native_client_codegen__)
553 /* The arg is hidden in a "push imm32" instruction, */
554 /* add one to skip the opcode. */
555 amd64_mov_reg_membase (code, AMD64_RAX, AMD64_R11, kNaClTrampOffset+1, 4);
557 /* Compute the address of the GOT slot */
558 amd64_alu_reg_reg_size (code, X86_ADD, AMD64_R11, AMD64_RAX, sizeof(gpointer));
560 amd64_mov_reg_membase (code, AMD64_R11, AMD64_R11, 0, sizeof(gpointer));
562 amd64_mov_reg_membase (code, AMD64_R11, AMD64_RBP, tramp_offset, sizeof(gpointer));
563 #if defined(__default_codegen__)
564 amd64_mov_reg_membase (code, AMD64_RAX, AMD64_R11, 5, 1);
565 amd64_widen_reg (code, AMD64_RAX, AMD64_RAX, TRUE, FALSE);
566 amd64_alu_reg_imm_size (code, X86_CMP, AMD64_RAX, 4, 1);
568 x86_branch8 (code, X86_CC_NE, 6, FALSE);
569 /* 32 bit immediate */
570 amd64_mov_reg_membase (code, AMD64_R11, AMD64_R11, 6, 4);
572 x86_jump8 (code, 10);
573 /* 64 bit immediate */
574 mono_amd64_patch (br [0], code);
575 amd64_mov_reg_membase (code, AMD64_R11, AMD64_R11, 6, 8);
576 mono_amd64_patch (br [1], code);
577 #elif defined(__native_client_codegen__)
578 /* All args are 32-bit pointers in NaCl */
579 amd64_mov_reg_membase (code, AMD64_R11, AMD64_R11, 6, 4);
582 amd64_mov_membase_reg (code, AMD64_RBP, arg_offset, AMD64_R11, sizeof(gpointer));
584 amd64_mov_reg_membase (code, AMD64_R11, AMD64_RBP, saved_regs_offset + (MONO_AMD64_ARG_REG1 * sizeof(mgreg_t)), sizeof(mgreg_t));
585 amd64_mov_membase_reg (code, AMD64_RBP, arg_offset, AMD64_R11, sizeof(gpointer));
590 offset += sizeof (MonoLMF);
591 lmf_offset = - offset;
595 amd64_mov_reg_membase (code, AMD64_R11, AMD64_RBP, 8, sizeof(gpointer));
597 amd64_mov_reg_imm (code, AMD64_R11, 0);
598 amd64_mov_membase_reg (code, AMD64_RBP, lmf_offset + G_STRUCT_OFFSET (MonoLMF, rip), AMD64_R11, sizeof(mgreg_t));
600 amd64_mov_reg_membase (code, AMD64_R11, AMD64_RSP, framesize, sizeof(mgreg_t));
601 amd64_mov_membase_reg (code, AMD64_RBP, lmf_offset + G_STRUCT_OFFSET (MonoLMF, rbp), AMD64_R11, sizeof(mgreg_t));
603 amd64_mov_reg_reg (code, AMD64_R11, AMD64_RSP, sizeof(mgreg_t));
604 amd64_alu_reg_imm (code, X86_ADD, AMD64_R11, framesize + 16);
605 amd64_mov_membase_reg (code, AMD64_RBP, lmf_offset + G_STRUCT_OFFSET (MonoLMF, rsp), AMD64_R11, sizeof(mgreg_t));
607 if (tramp_type == MONO_TRAMPOLINE_JIT || tramp_type == MONO_TRAMPOLINE_JUMP) {
608 amd64_mov_reg_membase (code, AMD64_R11, AMD64_RBP, arg_offset, sizeof(gpointer));
609 amd64_mov_membase_reg (code, AMD64_RBP, lmf_offset + G_STRUCT_OFFSET (MonoLMF, method), AMD64_R11, sizeof(gpointer));
611 amd64_mov_membase_imm (code, AMD64_RBP, lmf_offset + G_STRUCT_OFFSET (MonoLMF, method), 0, sizeof(gpointer));
613 /* Save callee saved regs */
615 amd64_mov_membase_reg (code, AMD64_RBP, lmf_offset + G_STRUCT_OFFSET (MonoLMF, rdi), AMD64_RDI, sizeof(mgreg_t));
616 amd64_mov_membase_reg (code, AMD64_RBP, lmf_offset + G_STRUCT_OFFSET (MonoLMF, rsi), AMD64_RSI, sizeof(mgreg_t));
618 amd64_mov_membase_reg (code, AMD64_RBP, lmf_offset + G_STRUCT_OFFSET (MonoLMF, rbx), AMD64_RBX, sizeof(mgreg_t));
619 amd64_mov_membase_reg (code, AMD64_RBP, lmf_offset + G_STRUCT_OFFSET (MonoLMF, r12), AMD64_R12, sizeof(mgreg_t));
620 amd64_mov_membase_reg (code, AMD64_RBP, lmf_offset + G_STRUCT_OFFSET (MonoLMF, r13), AMD64_R13, sizeof(mgreg_t));
621 amd64_mov_membase_reg (code, AMD64_RBP, lmf_offset + G_STRUCT_OFFSET (MonoLMF, r14), AMD64_R14, sizeof(mgreg_t));
622 amd64_mov_membase_reg (code, AMD64_RBP, lmf_offset + G_STRUCT_OFFSET (MonoLMF, r15), AMD64_R15, sizeof(mgreg_t));
625 code = mono_arch_emit_load_aotconst (buf, code, &ji, MONO_PATCH_INFO_JIT_ICALL_ADDR, "mono_get_lmf_addr");
627 amd64_mov_reg_imm (code, AMD64_R11, mono_get_lmf_addr);
629 amd64_call_reg (code, AMD64_R11);
632 amd64_mov_membase_reg (code, AMD64_RBP, lmf_offset + G_STRUCT_OFFSET (MonoLMF, lmf_addr), AMD64_RAX, sizeof(gpointer));
633 /* Save previous_lmf */
634 /* Set the lowest bit to 1 to signal that this LMF has the ip field set */
635 amd64_mov_reg_membase (code, AMD64_R11, AMD64_RAX, 0, sizeof(gpointer));
636 amd64_alu_reg_imm_size (code, X86_ADD, AMD64_R11, 1, sizeof(gpointer));
637 amd64_mov_membase_reg (code, AMD64_RBP, lmf_offset + G_STRUCT_OFFSET (MonoLMF, previous_lmf), AMD64_R11, sizeof(gpointer));
639 amd64_lea_membase (code, AMD64_R11, AMD64_RBP, lmf_offset);
640 amd64_mov_membase_reg (code, AMD64_RAX, 0, AMD64_R11, sizeof(gpointer));
644 /* Arg1 is the pointer to the saved registers */
645 amd64_lea_membase (code, AMD64_ARG_REG1, AMD64_RBP, saved_regs_offset);
647 /* Arg2 is the address of the calling code */
649 amd64_mov_reg_membase (code, AMD64_ARG_REG2, AMD64_RBP, 8, sizeof(gpointer));
651 amd64_mov_reg_imm (code, AMD64_ARG_REG2, 0);
653 /* Arg3 is the method/vtable ptr */
654 amd64_mov_reg_membase (code, AMD64_ARG_REG3, AMD64_RBP, arg_offset, sizeof(gpointer));
656 /* Arg4 is the trampoline address */
657 amd64_mov_reg_membase (code, AMD64_ARG_REG4, AMD64_RBP, tramp_offset, sizeof(gpointer));
660 char *icall_name = g_strdup_printf ("trampoline_func_%d", tramp_type);
661 code = mono_arch_emit_load_aotconst (buf, code, &ji, MONO_PATCH_INFO_JIT_ICALL_ADDR, icall_name);
663 tramp = (guint8*)mono_get_trampoline_func (tramp_type);
664 amd64_mov_reg_imm (code, AMD64_R11, tramp);
666 amd64_call_reg (code, AMD64_R11);
668 /* Check for thread interruption */
669 /* This is not perf critical code so no need to check the interrupt flag */
671 * Have to call the _force_ variant, since there could be a protected wrapper on the top of the stack.
673 amd64_mov_membase_reg (code, AMD64_RBP, res_offset, AMD64_RAX, sizeof(mgreg_t));
675 code = mono_arch_emit_load_aotconst (buf, code, &ji, MONO_PATCH_INFO_JIT_ICALL_ADDR, "mono_thread_force_interruption_checkpoint");
677 amd64_mov_reg_imm (code, AMD64_R11, (guint8*)mono_thread_force_interruption_checkpoint);
679 amd64_call_reg (code, AMD64_R11);
681 amd64_mov_reg_membase (code, AMD64_RAX, AMD64_RBP, res_offset, sizeof(mgreg_t));
685 amd64_mov_reg_membase (code, AMD64_RCX, AMD64_RBP, lmf_offset + G_STRUCT_OFFSET (MonoLMF, previous_lmf), sizeof(gpointer));
686 amd64_alu_reg_imm_size (code, X86_SUB, AMD64_RCX, 1, sizeof(gpointer));
687 amd64_mov_reg_membase (code, AMD64_R11, AMD64_RBP, lmf_offset + G_STRUCT_OFFSET (MonoLMF, lmf_addr), sizeof(gpointer));
688 amd64_mov_membase_reg (code, AMD64_R11, 0, AMD64_RCX, sizeof(gpointer));
691 * Save rax to the stack, after the leave instruction, this will become part of
694 amd64_mov_membase_reg (code, AMD64_RBP, rax_offset, AMD64_RAX, sizeof(mgreg_t));
696 /* Restore argument registers, r10 (imt method/rgxtx)
697 and rax (needed for direct calls to C vararg functions). */
698 for (i = 0; i < AMD64_NREG; ++i)
699 if (AMD64_IS_ARGUMENT_REG (i) || i == AMD64_R10 || i == AMD64_RAX)
700 amd64_mov_reg_membase (code, i, AMD64_RBP, saved_regs_offset + (i * sizeof(mgreg_t)), sizeof(mgreg_t));
702 for (i = 0; i < 8; ++i)
703 amd64_movsd_reg_membase (code, i, AMD64_RBP, saved_fpregs_offset + (i * sizeof(mgreg_t)));
708 if (MONO_TRAMPOLINE_TYPE_MUST_RETURN (tramp_type)) {
710 amd64_mov_reg_membase (code, AMD64_RAX, AMD64_RSP, rax_offset - sizeof(mgreg_t), sizeof(mgreg_t));
713 /* call the compiled method using the saved rax */
714 amd64_jump_membase (code, AMD64_RSP, rax_offset - sizeof(mgreg_t));
717 g_assert ((code - buf) <= kMaxCodeSize);
719 nacl_global_codeman_validate (&buf, kMaxCodeSize, &code);
721 mono_arch_flush_icache (buf, code - buf);
723 if (tramp_type == MONO_TRAMPOLINE_CLASS_INIT) {
724 /* Initialize the nullified class init trampoline used in the AOT case */
725 nullified_class_init_trampoline = mono_arch_get_nullified_class_init_trampoline (NULL);
729 tramp_name = mono_get_generic_trampoline_name (tramp_type);
730 *info = mono_tramp_info_create (tramp_name, buf, code - buf, ji, unwind_ops);
738 mono_arch_get_nullified_class_init_trampoline (MonoTrampInfo **info)
741 int size = NACL_SIZE (16, 32);
743 code = buf = mono_global_codeman_reserve (size);
746 nacl_global_codeman_validate(&buf, size, &code);
748 mono_arch_flush_icache (buf, code - buf);
751 *info = mono_tramp_info_create ("nullified_class_init_trampoline", buf, code - buf, NULL, NULL);
753 if (mono_jit_map_is_enabled ())
754 mono_emit_jit_tramp (buf, code - buf, "nullified_class_init_trampoline");
760 mono_arch_create_specific_trampoline (gpointer arg1, MonoTrampolineType tramp_type, MonoDomain *domain, guint32 *code_len)
762 guint8 *code, *buf, *tramp;
764 gboolean far_addr = FALSE;
766 tramp = mono_get_trampoline_code (tramp_type);
768 #if defined(__default_codegen__)
769 if ((((guint64)arg1) >> 32) == 0)
774 code = buf = mono_domain_code_reserve_align (domain, size, 1);
776 if (((gint64)tramp - (gint64)code) >> 31 != 0 && ((gint64)tramp - (gint64)code) >> 31 != -1) {
777 #ifndef MONO_ARCH_NOMAP32BIT
778 g_assert_not_reached ();
782 code = buf = mono_domain_code_reserve_align (domain, size, 1);
784 #elif defined(__native_client_codegen__)
786 /* Aligning the call site below could */
787 /* add up to kNaClAlignment-1 bytes */
788 size += (kNaClAlignment-1);
789 size = NACL_BUNDLE_ALIGN_UP (size);
790 buf = mono_domain_code_reserve_align (domain, size, kNaClAlignment);
795 amd64_mov_reg_imm (code, AMD64_R11, tramp);
796 amd64_call_reg (code, AMD64_R11);
798 amd64_call_code (code, tramp);
800 /* The trampoline code will obtain the argument from the instruction stream */
801 #if defined(__default_codegen__)
802 if ((((guint64)arg1) >> 32) == 0) {
804 *(guint32*)(code + 1) = (gint64)arg1;
808 *(guint64*)(code + 1) = (gint64)arg1;
811 #elif defined(__native_client_codegen__)
812 /* For NaCl, all tramp args are 32-bit because they're pointers */
813 *code = 0x68; /* push imm32 */
814 *(guint32*)(code + 1) = (gint32)arg1;
818 g_assert ((code - buf) <= size);
823 nacl_domain_code_validate(domain, &buf, size, &code);
825 mono_arch_flush_icache (buf, size);
831 mono_arch_create_rgctx_lazy_fetch_trampoline (guint32 slot, MonoTrampInfo **info, gboolean aot)
835 guint8 **rgctx_null_jumps;
840 MonoJumpInfo *ji = NULL;
841 GSList *unwind_ops = NULL;
843 mrgctx = MONO_RGCTX_SLOT_IS_MRGCTX (slot);
844 index = MONO_RGCTX_SLOT_INDEX (slot);
846 index += MONO_SIZEOF_METHOD_RUNTIME_GENERIC_CONTEXT / sizeof (gpointer);
847 for (depth = 0; ; ++depth) {
848 int size = mono_class_rgctx_get_array_size (depth, mrgctx);
850 if (index < size - 1)
855 tramp_size = NACL_SIZE (64 + 8 * depth, 128 + 8 * depth);
857 code = buf = mono_global_codeman_reserve (tramp_size);
859 unwind_ops = mono_arch_get_cie_program ();
861 rgctx_null_jumps = g_malloc (sizeof (guint8*) * (depth + 2));
865 amd64_mov_reg_reg (code, AMD64_RAX, AMD64_ARG_REG1, 8);
867 /* load rgctx ptr from vtable */
868 amd64_mov_reg_membase (code, AMD64_RAX, AMD64_ARG_REG1, G_STRUCT_OFFSET (MonoVTable, runtime_generic_context), sizeof(gpointer));
869 /* is the rgctx ptr null? */
870 amd64_test_reg_reg (code, AMD64_RAX, AMD64_RAX);
871 /* if yes, jump to actual trampoline */
872 rgctx_null_jumps [0] = code;
873 amd64_branch8 (code, X86_CC_Z, -1, 1);
876 for (i = 0; i < depth; ++i) {
877 /* load ptr to next array */
878 if (mrgctx && i == 0)
879 amd64_mov_reg_membase (code, AMD64_RAX, AMD64_RAX, MONO_SIZEOF_METHOD_RUNTIME_GENERIC_CONTEXT, sizeof(gpointer));
881 amd64_mov_reg_membase (code, AMD64_RAX, AMD64_RAX, 0, sizeof(gpointer));
882 /* is the ptr null? */
883 amd64_test_reg_reg (code, AMD64_RAX, AMD64_RAX);
884 /* if yes, jump to actual trampoline */
885 rgctx_null_jumps [i + 1] = code;
886 amd64_branch8 (code, X86_CC_Z, -1, 1);
890 amd64_mov_reg_membase (code, AMD64_RAX, AMD64_RAX, sizeof (gpointer) * (index + 1), sizeof(gpointer));
891 /* is the slot null? */
892 amd64_test_reg_reg (code, AMD64_RAX, AMD64_RAX);
893 /* if yes, jump to actual trampoline */
894 rgctx_null_jumps [depth + 1] = code;
895 amd64_branch8 (code, X86_CC_Z, -1, 1);
896 /* otherwise return */
899 for (i = mrgctx ? 1 : 0; i <= depth + 1; ++i)
900 mono_amd64_patch (rgctx_null_jumps [i], code);
902 g_free (rgctx_null_jumps);
904 /* move the rgctx pointer to the VTABLE register */
905 amd64_mov_reg_reg (code, MONO_ARCH_VTABLE_REG, AMD64_ARG_REG1, sizeof(gpointer));
908 code = mono_arch_emit_load_aotconst (buf, code, &ji, MONO_PATCH_INFO_JIT_ICALL_ADDR, g_strdup_printf ("specific_trampoline_lazy_fetch_%u", slot));
909 amd64_jump_reg (code, AMD64_R11);
911 tramp = mono_arch_create_specific_trampoline (GUINT_TO_POINTER (slot), MONO_TRAMPOLINE_RGCTX_LAZY_FETCH, mono_get_root_domain (), NULL);
913 /* jump to the actual trampoline */
914 amd64_jump_code (code, tramp);
917 nacl_global_codeman_validate (&buf, tramp_size, &code);
918 mono_arch_flush_icache (buf, code - buf);
920 g_assert (code - buf <= tramp_size);
923 char *name = mono_get_rgctx_fetch_trampoline_name (slot);
924 *info = mono_tramp_info_create (name, buf, code - buf, ji, unwind_ops);
932 mono_arch_create_generic_class_init_trampoline (MonoTrampInfo **info, gboolean aot)
936 static int byte_offset = -1;
937 static guint8 bitmask;
940 GSList *unwind_ops = NULL;
941 MonoJumpInfo *ji = NULL;
945 code = buf = mono_global_codeman_reserve (tramp_size);
948 mono_marshal_find_bitfield_offset (MonoVTable, initialized, &byte_offset, &bitmask);
950 amd64_test_membase_imm_size (code, MONO_AMD64_ARG_REG1, byte_offset, bitmask, 1);
952 amd64_branch8 (code, X86_CC_Z, -1, 1);
956 x86_patch (jump, code);
959 code = mono_arch_emit_load_aotconst (buf, code, &ji, MONO_PATCH_INFO_JIT_ICALL_ADDR, "specific_trampoline_generic_class_init");
960 amd64_jump_reg (code, AMD64_R11);
962 tramp = mono_arch_create_specific_trampoline (NULL, MONO_TRAMPOLINE_GENERIC_CLASS_INIT, mono_get_root_domain (), NULL);
964 /* jump to the actual trampoline */
965 amd64_jump_code (code, tramp);
968 nacl_global_codeman_validate (&buf, tramp_size, &code);
970 mono_arch_flush_icache (buf, code - buf);
972 g_assert (code - buf <= tramp_size);
975 *info = mono_tramp_info_create ("generic_class_init_trampoline", buf, code - buf, ji, unwind_ops);
980 #ifdef MONO_ARCH_MONITOR_OBJECT_REG
983 mono_arch_create_monitor_enter_trampoline (MonoTrampInfo **info, gboolean aot)
987 guint8 *jump_obj_null, *jump_sync_null, *jump_cmpxchg_failed, *jump_other_owner, *jump_tid, *jump_sync_thin_hash = NULL;
989 int owner_offset, nest_offset, dummy;
990 MonoJumpInfo *ji = NULL;
991 GSList *unwind_ops = NULL;
993 g_assert (MONO_ARCH_MONITOR_OBJECT_REG == AMD64_RDI);
995 mono_monitor_threads_sync_members_offset (&owner_offset, &nest_offset, &dummy);
996 g_assert (MONO_THREADS_SYNC_MEMBER_SIZE (owner_offset) == sizeof (gpointer));
997 g_assert (MONO_THREADS_SYNC_MEMBER_SIZE (nest_offset) == sizeof (guint32));
998 owner_offset = MONO_THREADS_SYNC_MEMBER_OFFSET (owner_offset);
999 nest_offset = MONO_THREADS_SYNC_MEMBER_OFFSET (nest_offset);
1003 code = buf = mono_global_codeman_reserve (tramp_size);
1005 unwind_ops = mono_arch_get_cie_program ();
1007 if (mono_thread_get_tls_offset () != -1) {
1008 /* MonoObject* obj is in RDI */
1010 amd64_test_reg_reg (code, AMD64_RDI, AMD64_RDI);
1011 /* if yes, jump to actual trampoline */
1012 jump_obj_null = code;
1013 amd64_branch8 (code, X86_CC_Z, -1, 1);
1015 /* load obj->synchronization to RCX */
1016 amd64_mov_reg_membase (code, AMD64_RCX, AMD64_RDI, G_STRUCT_OFFSET (MonoObject, synchronisation), 8);
1018 if (mono_gc_is_moving ()) {
1019 /*if bit zero is set it's a thin hash*/
1020 /*FIXME use testb encoding*/
1021 amd64_test_reg_imm (code, AMD64_RCX, 0x01);
1022 jump_sync_thin_hash = code;
1023 amd64_branch8 (code, X86_CC_NE, -1, 1);
1025 /*clear bits used by the gc*/
1026 amd64_alu_reg_imm (code, X86_AND, AMD64_RCX, ~0x3);
1029 /* is synchronization null? */
1030 amd64_test_reg_reg (code, AMD64_RCX, AMD64_RCX);
1031 /* if yes, jump to actual trampoline */
1032 jump_sync_null = code;
1033 amd64_branch8 (code, X86_CC_Z, -1, 1);
1035 /* load MonoInternalThread* into RDX */
1036 code = mono_amd64_emit_tls_get (code, AMD64_RDX, mono_thread_get_tls_offset ());
1037 /* load TID into RDX */
1038 amd64_mov_reg_membase (code, AMD64_RDX, AMD64_RDX, G_STRUCT_OFFSET (MonoInternalThread, tid), 8);
1040 /* is synchronization->owner null? */
1041 amd64_alu_membase_imm_size (code, X86_CMP, AMD64_RCX, owner_offset, 0, 8);
1042 /* if not, jump to next case */
1044 amd64_branch8 (code, X86_CC_NZ, -1, 1);
1046 /* if yes, try a compare-exchange with the TID */
1048 amd64_alu_reg_reg (code, X86_XOR, AMD64_RAX, AMD64_RAX);
1049 /* compare and exchange */
1050 amd64_prefix (code, X86_LOCK_PREFIX);
1051 amd64_cmpxchg_membase_reg_size (code, AMD64_RCX, owner_offset, AMD64_RDX, 8);
1052 /* if not successful, jump to actual trampoline */
1053 jump_cmpxchg_failed = code;
1054 amd64_branch8 (code, X86_CC_NZ, -1, 1);
1055 /* if successful, return */
1058 /* next case: synchronization->owner is not null */
1059 x86_patch (jump_tid, code);
1060 /* is synchronization->owner == TID? */
1061 amd64_alu_membase_reg_size (code, X86_CMP, AMD64_RCX, owner_offset, AMD64_RDX, 8);
1062 /* if not, jump to actual trampoline */
1063 jump_other_owner = code;
1064 amd64_branch8 (code, X86_CC_NZ, -1, 1);
1065 /* if yes, increment nest */
1066 amd64_inc_membase_size (code, AMD64_RCX, nest_offset, 4);
1070 x86_patch (jump_obj_null, code);
1071 if (jump_sync_thin_hash)
1072 x86_patch (jump_sync_thin_hash, code);
1073 x86_patch (jump_sync_null, code);
1074 x86_patch (jump_cmpxchg_failed, code);
1075 x86_patch (jump_other_owner, code);
1078 /* jump to the actual trampoline */
1079 #if MONO_AMD64_ARG_REG1 != AMD64_RDI
1080 amd64_mov_reg_reg (code, MONO_AMD64_ARG_REG1, AMD64_RDI);
1084 code = mono_arch_emit_load_aotconst (buf, code, &ji, MONO_PATCH_INFO_JIT_ICALL_ADDR, "specific_trampoline_monitor_enter");
1085 amd64_jump_reg (code, AMD64_R11);
1087 tramp = mono_arch_create_specific_trampoline (NULL, MONO_TRAMPOLINE_MONITOR_ENTER, mono_get_root_domain (), NULL);
1089 /* jump to the actual trampoline */
1090 amd64_jump_code (code, tramp);
1093 nacl_global_codeman_validate (&buf, tramp_size, &code);
1095 mono_arch_flush_icache (code, code - buf);
1096 g_assert (code - buf <= tramp_size);
1099 *info = mono_tramp_info_create ("monitor_enter_trampoline", buf, code - buf, ji, unwind_ops);
1105 mono_arch_create_monitor_exit_trampoline (MonoTrampInfo **info, gboolean aot)
1109 guint8 *jump_obj_null, *jump_have_waiters, *jump_sync_null, *jump_not_owned, *jump_sync_thin_hash = NULL;
1112 int owner_offset, nest_offset, entry_count_offset;
1113 MonoJumpInfo *ji = NULL;
1114 GSList *unwind_ops = NULL;
1116 g_assert (MONO_ARCH_MONITOR_OBJECT_REG == AMD64_RDI);
1118 mono_monitor_threads_sync_members_offset (&owner_offset, &nest_offset, &entry_count_offset);
1119 g_assert (MONO_THREADS_SYNC_MEMBER_SIZE (owner_offset) == sizeof (gpointer));
1120 g_assert (MONO_THREADS_SYNC_MEMBER_SIZE (nest_offset) == sizeof (guint32));
1121 g_assert (MONO_THREADS_SYNC_MEMBER_SIZE (entry_count_offset) == sizeof (gint32));
1122 owner_offset = MONO_THREADS_SYNC_MEMBER_OFFSET (owner_offset);
1123 nest_offset = MONO_THREADS_SYNC_MEMBER_OFFSET (nest_offset);
1124 entry_count_offset = MONO_THREADS_SYNC_MEMBER_OFFSET (entry_count_offset);
1128 code = buf = mono_global_codeman_reserve (tramp_size);
1130 unwind_ops = mono_arch_get_cie_program ();
1132 if (mono_thread_get_tls_offset () != -1) {
1133 /* MonoObject* obj is in RDI */
1135 amd64_test_reg_reg (code, AMD64_RDI, AMD64_RDI);
1136 /* if yes, jump to actual trampoline */
1137 jump_obj_null = code;
1138 amd64_branch8 (code, X86_CC_Z, -1, 1);
1140 /* load obj->synchronization to RCX */
1141 amd64_mov_reg_membase (code, AMD64_RCX, AMD64_RDI, G_STRUCT_OFFSET (MonoObject, synchronisation), 8);
1143 if (mono_gc_is_moving ()) {
1144 /*if bit zero is set it's a thin hash*/
1145 /*FIXME use testb encoding*/
1146 amd64_test_reg_imm (code, AMD64_RCX, 0x01);
1147 jump_sync_thin_hash = code;
1148 amd64_branch8 (code, X86_CC_NE, -1, 1);
1150 /*clear bits used by the gc*/
1151 amd64_alu_reg_imm (code, X86_AND, AMD64_RCX, ~0x3);
1154 /* is synchronization null? */
1155 amd64_test_reg_reg (code, AMD64_RCX, AMD64_RCX);
1156 /* if yes, jump to actual trampoline */
1157 jump_sync_null = code;
1158 amd64_branch8 (code, X86_CC_Z, -1, 1);
1160 /* next case: synchronization is not null */
1161 /* load MonoInternalThread* into RDX */
1162 code = mono_amd64_emit_tls_get (code, AMD64_RDX, mono_thread_get_tls_offset ());
1163 /* load TID into RDX */
1164 amd64_mov_reg_membase (code, AMD64_RDX, AMD64_RDX, G_STRUCT_OFFSET (MonoInternalThread, tid), 8);
1165 /* is synchronization->owner == TID */
1166 amd64_alu_membase_reg_size (code, X86_CMP, AMD64_RCX, owner_offset, AMD64_RDX, 8);
1167 /* if no, jump to actual trampoline */
1168 jump_not_owned = code;
1169 amd64_branch8 (code, X86_CC_NZ, -1, 1);
1171 /* next case: synchronization->owner == TID */
1172 /* is synchronization->nest == 1 */
1173 amd64_alu_membase_imm_size (code, X86_CMP, AMD64_RCX, nest_offset, 1, 4);
1174 /* if not, jump to next case */
1176 amd64_branch8 (code, X86_CC_NZ, -1, 1);
1177 /* if yes, is synchronization->entry_count zero? */
1178 amd64_alu_membase_imm_size (code, X86_CMP, AMD64_RCX, entry_count_offset, 0, 4);
1179 /* if not, jump to actual trampoline */
1180 jump_have_waiters = code;
1181 amd64_branch8 (code, X86_CC_NZ, -1 , 1);
1182 /* if yes, set synchronization->owner to null and return */
1183 amd64_mov_membase_imm (code, AMD64_RCX, owner_offset, 0, 8);
1186 /* next case: synchronization->nest is not 1 */
1187 x86_patch (jump_next, code);
1188 /* decrease synchronization->nest and return */
1189 amd64_dec_membase_size (code, AMD64_RCX, nest_offset, 4);
1192 x86_patch (jump_obj_null, code);
1193 x86_patch (jump_have_waiters, code);
1194 x86_patch (jump_not_owned, code);
1195 x86_patch (jump_sync_null, code);
1198 /* jump to the actual trampoline */
1199 #if MONO_AMD64_ARG_REG1 != AMD64_RDI
1200 amd64_mov_reg_reg (code, MONO_AMD64_ARG_REG1, AMD64_RDI);
1204 code = mono_arch_emit_load_aotconst (buf, code, &ji, MONO_PATCH_INFO_JIT_ICALL_ADDR, "specific_trampoline_monitor_exit");
1205 amd64_jump_reg (code, AMD64_R11);
1207 tramp = mono_arch_create_specific_trampoline (NULL, MONO_TRAMPOLINE_MONITOR_EXIT, mono_get_root_domain (), NULL);
1208 amd64_jump_code (code, tramp);
1211 nacl_global_codeman_validate (&buf, tramp_size, &code);
1213 mono_arch_flush_icache (code, code - buf);
1214 g_assert (code - buf <= tramp_size);
1217 *info = mono_tramp_info_create ("monitor_exit_trampoline", buf, code - buf, ji, unwind_ops);
1224 mono_arch_invalidate_method (MonoJitInfo *ji, void *func, gpointer func_arg)
1226 /* FIXME: This is not thread safe */
1227 guint8 *code = ji->code_start;
1229 amd64_mov_reg_imm (code, AMD64_ARG_REG1, func_arg);
1230 amd64_mov_reg_imm (code, AMD64_R11, func);
1232 x86_push_imm (code, (guint64)func_arg);
1233 amd64_call_reg (code, AMD64_R11);
1238 handler_block_trampoline_helper (gpointer *ptr)
1240 MonoJitTlsData *jit_tls = mono_native_tls_get_value (mono_jit_tls_id);
1241 *ptr = jit_tls->handler_block_return_address;
1245 mono_arch_create_handler_block_trampoline (void)
1247 guint8 *tramp = mono_get_trampoline_code (MONO_TRAMPOLINE_HANDLER_BLOCK_GUARD);
1249 int tramp_size = 64;
1250 code = buf = mono_global_codeman_reserve (tramp_size);
1253 This trampoline restore the call chain of the handler block then jumps into the code that deals with it.
1256 if (mono_get_jit_tls_offset () != -1) {
1257 code = mono_amd64_emit_tls_get (code, AMD64_RDI, mono_get_jit_tls_offset ());
1258 amd64_mov_reg_membase (code, AMD64_RDI, AMD64_RDI, G_STRUCT_OFFSET (MonoJitTlsData, handler_block_return_address), 8);
1259 /* Simulate a call */
1260 amd64_push_reg (code, AMD64_RAX);
1261 amd64_jump_code (code, tramp);
1263 /*Slow path uses a c helper*/
1264 amd64_mov_reg_reg (code, AMD64_RDI, AMD64_RSP, 8);
1265 amd64_mov_reg_imm (code, AMD64_RAX, tramp);
1266 amd64_push_reg (code, AMD64_RAX);
1267 amd64_jump_code (code, handler_block_trampoline_helper);
1270 mono_arch_flush_icache (buf, code - buf);
1271 g_assert (code - buf <= tramp_size);
1273 if (mono_jit_map_is_enabled ())
1274 mono_emit_jit_tramp (buf, code - buf, "handler_block_trampoline");
1280 * mono_arch_get_call_target:
1282 * Return the address called by the code before CODE if exists.
1285 mono_arch_get_call_target (guint8 *code)
1287 if (code [-5] == 0xe8) {
1288 guint32 disp = *(guint32*)(code - 4);
1289 guint8 *target = code + disp;
1298 * mono_arch_get_plt_info_offset:
1300 * Return the PLT info offset belonging to the plt entry PLT_ENTRY.
1303 mono_arch_get_plt_info_offset (guint8 *plt_entry, mgreg_t *regs, guint8 *code)
1305 #if defined(__native_client__) || defined(__native_client_codegen__)
1306 /* 18 = 3 (mov opcode) + 4 (disp) + 10 (nacljmp) + 1 (push opcode) */
1307 /* See aot-compiler.c arch_emit_plt_entry for details. */
1308 return *(guint32*)(plt_entry + 18);
1310 return *(guint32*)(plt_entry + 6);