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/abi-details.h>
17 #include <mono/metadata/appdomain.h>
18 #include <mono/metadata/marshal.h>
19 #include <mono/metadata/tabledefs.h>
20 #include <mono/metadata/mono-debug-debugger.h>
21 #include <mono/metadata/profiler-private.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"
29 #include "debugger-agent.h"
31 #if defined(__native_client_codegen__) && defined(__native_client__)
33 #include <nacl/nacl_dyncode.h>
36 #define ALIGN_TO(val,align) ((((guint64)val) + ((align) - 1)) & ~((align) - 1))
38 #define IS_REX(inst) (((inst) >= 0x40) && ((inst) <= 0x4f))
41 * mono_arch_get_unbox_trampoline:
43 * @addr: pointer to native code for @m
45 * when value type methods are called through the vtable we need to unbox the
46 * this argument. This method returns a pointer to a trampoline which does
47 * unboxing before calling the method
50 mono_arch_get_unbox_trampoline (MonoMethod *m, gpointer addr)
54 int this_reg, size = NACL_SIZE (20, 32);
56 MonoDomain *domain = mono_domain_get ();
58 this_reg = mono_arch_get_this_arg_reg (NULL);
60 start = code = mono_domain_code_reserve (domain, size);
62 unwind_ops = mono_arch_get_cie_program ();
64 amd64_alu_reg_imm (code, X86_ADD, this_reg, sizeof (MonoObject));
65 /* FIXME: Optimize this */
66 amd64_mov_reg_imm (code, AMD64_RAX, addr);
67 amd64_jump_reg (code, AMD64_RAX);
68 g_assert ((code - start) < size);
70 nacl_domain_code_validate (domain, &start, size, &code);
72 mono_arch_flush_icache (start, code - start);
73 mono_profiler_code_buffer_new (start, code - start, MONO_PROFILER_CODE_BUFFER_UNBOX_TRAMPOLINE, m);
75 mono_tramp_info_register (mono_tramp_info_create (NULL, start, code - start, NULL, unwind_ops), domain);
81 * mono_arch_get_static_rgctx_trampoline:
83 * Create a trampoline which sets RGCTX_REG to MRGCTX, then jumps to ADDR.
86 mono_arch_get_static_rgctx_trampoline (MonoMethod *m, MonoMethodRuntimeGenericContext *mrgctx, gpointer addr)
92 MonoDomain *domain = mono_domain_get ();
94 #ifdef MONO_ARCH_NOMAP32BIT
97 /* AOTed code could still have a non-32 bit address */
98 if ((((guint64)addr) >> 32) == 0)
99 buf_len = NACL_SIZE (16, 32);
101 buf_len = NACL_SIZE (30, 32);
104 start = code = mono_domain_code_reserve (domain, buf_len);
106 unwind_ops = mono_arch_get_cie_program ();
108 amd64_mov_reg_imm (code, MONO_ARCH_RGCTX_REG, mrgctx);
109 amd64_jump_code (code, addr);
110 g_assert ((code - start) < buf_len);
112 nacl_domain_code_validate (domain, &start, buf_len, &code);
113 mono_arch_flush_icache (start, code - start);
114 mono_profiler_code_buffer_new (start, code - start, MONO_PROFILER_CODE_BUFFER_GENERICS_TRAMPOLINE, NULL);
116 mono_tramp_info_register (mono_tramp_info_create (NULL, start, code - start, NULL, unwind_ops), domain);
122 mono_arch_get_llvm_imt_trampoline (MonoDomain *domain, MonoMethod *m, int vt_offset)
124 guint8 *code, *start;
130 start = code = mono_domain_code_reserve (domain, buf_len);
132 this_reg = mono_arch_get_this_arg_reg (NULL);
135 amd64_mov_reg_imm (code, MONO_ARCH_IMT_REG, m);
136 /* Load vtable address */
137 amd64_mov_reg_membase (code, AMD64_RAX, this_reg, 0, 8);
138 amd64_jump_membase (code, AMD64_RAX, vt_offset);
141 g_assert ((code - start) < buf_len);
143 nacl_domain_code_validate (domain, &start, buf_len, &code);
145 mono_arch_flush_icache (start, code - start);
146 mono_profiler_code_buffer_new (start, code - start, MONO_PROFILER_CODE_BUFFER_IMT_TRAMPOLINE, NULL);
152 // Workaround lack of Valgrind support for 64-bit Windows
153 #define VALGRIND_DISCARD_TRANSLATIONS(...)
157 * mono_arch_patch_callsite:
159 * Patch the callsite whose address is given by ORIG_CODE so it calls ADDR. ORIG_CODE
160 * points to the pc right after the call.
163 mono_arch_patch_callsite (guint8 *method_start, guint8 *orig_code, guint8 *addr)
165 #if defined(__default_codegen__)
168 gboolean can_write = mono_breakpoint_clean_code (method_start, orig_code, 14, buf, sizeof (buf));
172 /* mov 64-bit imm into r11 (followed by call reg?) or direct call*/
173 if (((code [-13] == 0x49) && (code [-12] == 0xbb)) || (code [-5] == 0xe8)) {
174 if (code [-5] != 0xe8) {
176 InterlockedExchangePointer ((gpointer*)(orig_code - 11), addr);
177 VALGRIND_DISCARD_TRANSLATIONS (orig_code - 11, sizeof (gpointer));
180 gboolean disp_32bit = ((((gint64)addr - (gint64)orig_code)) < (1 << 30)) && ((((gint64)addr - (gint64)orig_code)) > -(1 << 30));
182 if ((((guint64)(addr)) >> 32) != 0 && !disp_32bit) {
184 * This might happen with LLVM or when calling AOTed code. Create a thunk.
186 guint8 *thunk_start, *thunk_code;
188 thunk_start = thunk_code = mono_domain_code_reserve (mono_domain_get (), 32);
189 amd64_jump_membase (thunk_code, AMD64_RIP, 0);
190 *(guint64*)thunk_code = (guint64)addr;
192 g_assert ((((guint64)(addr)) >> 32) == 0);
193 mono_arch_flush_icache (thunk_start, thunk_code - thunk_start);
194 mono_profiler_code_buffer_new (thunk_start, thunk_code - thunk_start, MONO_PROFILER_CODE_BUFFER_HELPER, NULL);
197 InterlockedExchange ((gint32*)(orig_code - 4), ((gint64)addr - (gint64)orig_code));
198 VALGRIND_DISCARD_TRANSLATIONS (orig_code - 5, 4);
202 else if ((code [-7] == 0x41) && (code [-6] == 0xff) && (code [-5] == 0x15)) {
203 /* call *<OFFSET>(%rip) */
204 gpointer *got_entry = (gpointer*)((guint8*)orig_code + (*(guint32*)(orig_code - 4)));
206 InterlockedExchangePointer (got_entry, addr);
207 VALGRIND_DISCARD_TRANSLATIONS (orig_code - 5, sizeof (gpointer));
210 #elif defined(__native_client__)
211 /* These are essentially the same 2 cases as above, modified for NaCl*/
213 /* Target must be bundle-aligned */
214 g_assert (((guint32)addr & kNaClAlignmentMask) == 0);
215 /* Return target must be bundle-aligned */
216 g_assert (((guint32)orig_code & kNaClAlignmentMask) == 0);
218 if (orig_code[-5] == 0xe8) {
221 gint32 offset = (gint32)addr - (gint32)orig_code;
222 guint8 buf[sizeof(gint32)];
223 *((gint32*)(buf)) = offset;
224 ret = nacl_dyncode_modify (orig_code - sizeof(gint32), buf, sizeof(gint32));
228 else if (is_nacl_call_reg_sequence (orig_code - 10) && orig_code[-16] == 0x41 && orig_code[-15] == 0xbb) {
230 guint8 buf[sizeof(gint32)];
231 *((gint32 *)(buf)) = addr;
232 /* orig_code[-14] is the start of the immediate. */
233 ret = nacl_dyncode_modify (orig_code - 14, buf, sizeof(gint32));
237 g_assert_not_reached ();
245 mono_arch_create_llvm_native_thunk (MonoDomain *domain, guint8 *addr)
248 * The caller is LLVM code and the call displacement might exceed 32 bits. We can't determine the caller address, so
249 * we add a thunk every time.
250 * Since the caller is also allocated using the domain code manager, hopefully the displacement will fit into 32 bits.
251 * FIXME: Avoid this if possible if !MONO_ARCH_NOMAP32BIT and ADDR is 32 bits.
253 guint8 *thunk_start, *thunk_code;
255 thunk_start = thunk_code = mono_domain_code_reserve (mono_domain_get (), 32);
256 amd64_jump_membase (thunk_code, AMD64_RIP, 0);
257 *(guint64*)thunk_code = (guint64)addr;
259 mono_arch_flush_icache (thunk_start, thunk_code - thunk_start);
260 mono_profiler_code_buffer_new (thunk_start, thunk_code - thunk_start, MONO_PROFILER_CODE_BUFFER_HELPER, NULL);
265 mono_arch_patch_plt_entry (guint8 *code, gpointer *got, mgreg_t *regs, guint8 *addr)
268 gpointer *plt_jump_table_entry;
270 #if defined(__default_codegen__)
271 /* A PLT entry: jmp *<DISP>(%rip) */
272 g_assert (code [0] == 0xff);
273 g_assert (code [1] == 0x25);
275 disp = *(gint32*)(code + 2);
277 plt_jump_table_entry = (gpointer*)(code + 6 + disp);
278 #elif defined(__native_client_codegen__)
280 /* mov <DISP>(%rip), %r11d */
283 /* Verify the 'mov' */
284 g_assert (code [0] == 0x45);
285 g_assert (code [1] == 0x8b);
286 g_assert (code [2] == 0x1d);
288 disp = *(gint32*)(code + 3);
290 /* 7 = 3 (mov opcode) + 4 (disp) */
291 /* This needs to resolve to the target of the RIP-relative offset */
292 plt_jump_table_entry = (gpointer*)(code + 7 + disp);
294 #endif /* __native_client_codegen__ */
296 InterlockedExchangePointer (plt_jump_table_entry, addr);
300 stack_unaligned (MonoTrampolineType tramp_type)
302 printf ("%d\n", tramp_type);
303 g_assert_not_reached ();
307 mono_arch_create_generic_trampoline (MonoTrampolineType tramp_type, MonoTrampInfo **info, gboolean aot)
310 guint8 *buf, *code, *tramp, *br [2], *r11_save_code, *after_r11_save_code;
311 int i, lmf_offset, offset, res_offset, arg_offset, rax_offset, tramp_offset, ctx_offset, saved_regs_offset;
312 int r11_save_offset, saved_fpregs_offset, rbp_offset, framesize, orig_rsp_to_rbp_offset, cfa_offset;
314 GSList *unwind_ops = NULL;
315 MonoJumpInfo *ji = NULL;
316 const guint kMaxCodeSize = NACL_SIZE (600, 600*2);
318 #if defined(__native_client_codegen__)
319 const guint kNaClTrampOffset = 17;
322 if (tramp_type == MONO_TRAMPOLINE_JUMP || tramp_type == MONO_TRAMPOLINE_HANDLER_BLOCK_GUARD)
327 code = buf = mono_global_codeman_reserve (kMaxCodeSize);
329 /* Compute stack frame size and offsets */
331 rbp_offset = -offset;
333 offset += sizeof(mgreg_t);
334 rax_offset = -offset;
336 offset += sizeof(mgreg_t);
337 r11_save_offset = -offset;
339 offset += sizeof(mgreg_t);
340 tramp_offset = -offset;
342 offset += sizeof(gpointer);
343 arg_offset = -offset;
345 offset += sizeof(mgreg_t);
346 res_offset = -offset;
348 offset += sizeof (MonoContext);
349 ctx_offset = -offset;
350 saved_regs_offset = ctx_offset + MONO_STRUCT_OFFSET (MonoContext, gregs);
351 saved_fpregs_offset = ctx_offset + MONO_STRUCT_OFFSET (MonoContext, fregs);
353 offset += sizeof (MonoLMFTramp);
354 lmf_offset = -offset;
357 /* Reserve space where the callee can save the argument registers */
358 offset += 4 * sizeof (mgreg_t);
361 framesize = ALIGN_TO (offset, MONO_ARCH_FRAME_ALIGNMENT);
363 // CFA = sp + 16 (the trampoline address is on the stack)
365 mono_add_unwind_op_def_cfa (unwind_ops, code, buf, AMD64_RSP, 16);
366 // IP saved at CFA - 8
367 mono_add_unwind_op_offset (unwind_ops, code, buf, AMD64_RIP, -8);
369 orig_rsp_to_rbp_offset = 0;
370 r11_save_code = code;
371 /* Reserve space for the mov_membase_reg to save R11 */
373 after_r11_save_code = code;
375 /* Pop the return address off the stack */
376 amd64_pop_reg (code, AMD64_R11);
377 orig_rsp_to_rbp_offset += sizeof(mgreg_t);
379 cfa_offset -= sizeof(mgreg_t);
380 mono_add_unwind_op_def_cfa_offset (unwind_ops, code, buf, cfa_offset);
383 * Allocate a new stack frame
385 amd64_push_reg (code, AMD64_RBP);
386 cfa_offset += sizeof(mgreg_t);
387 mono_add_unwind_op_def_cfa_offset (unwind_ops, code, buf, cfa_offset);
388 mono_add_unwind_op_offset (unwind_ops, code, buf, AMD64_RBP, - cfa_offset);
390 orig_rsp_to_rbp_offset -= sizeof(mgreg_t);
391 amd64_mov_reg_reg (code, AMD64_RBP, AMD64_RSP, sizeof(mgreg_t));
392 mono_add_unwind_op_def_cfa_reg (unwind_ops, code, buf, AMD64_RBP);
393 amd64_alu_reg_imm (code, X86_SUB, AMD64_RSP, framesize);
395 /* Compute the trampoline address from the return address */
397 #if defined(__default_codegen__)
398 /* 7 = length of call *<offset>(rip) */
399 amd64_alu_reg_imm (code, X86_SUB, AMD64_R11, 7);
400 #elif defined(__native_client_codegen__)
401 amd64_alu_reg_imm (code, X86_SUB, AMD64_R11, kNaClTrampOffset);
404 /* 5 = length of amd64_call_membase () */
405 amd64_alu_reg_imm (code, X86_SUB, AMD64_R11, 5);
407 amd64_mov_membase_reg (code, AMD64_RBP, tramp_offset, AMD64_R11, sizeof(gpointer));
409 /* Save all registers */
410 for (i = 0; i < AMD64_NREG; ++i) {
411 if (i == AMD64_RBP) {
412 /* RAX is already saved */
413 amd64_mov_reg_membase (code, AMD64_RAX, AMD64_RBP, rbp_offset, sizeof(mgreg_t));
414 amd64_mov_membase_reg (code, AMD64_RBP, saved_regs_offset + (i * sizeof(mgreg_t)), AMD64_RAX, sizeof(mgreg_t));
415 } else if (i == AMD64_RIP) {
417 amd64_mov_reg_membase (code, AMD64_R11, AMD64_RBP, 8, sizeof(gpointer));
419 amd64_mov_reg_imm (code, AMD64_R11, 0);
420 amd64_mov_membase_reg (code, AMD64_RBP, saved_regs_offset + (i * sizeof(mgreg_t)), AMD64_R11, sizeof(mgreg_t));
421 } else if (i == AMD64_RSP) {
422 amd64_mov_reg_reg (code, AMD64_R11, AMD64_RSP, sizeof(mgreg_t));
423 amd64_alu_reg_imm (code, X86_ADD, AMD64_R11, framesize + 16);
424 amd64_mov_membase_reg (code, AMD64_RBP, saved_regs_offset + (i * sizeof(mgreg_t)), AMD64_R11, sizeof(mgreg_t));
425 } else if (i != AMD64_R11) {
426 amd64_mov_membase_reg (code, AMD64_RBP, saved_regs_offset + (i * sizeof(mgreg_t)), i, sizeof(mgreg_t));
428 /* We have to save R11 right at the start of
429 the trampoline code because it's used as a
431 /* This happens before the frame is set up, so it goes into the redzone */
432 amd64_mov_membase_reg (r11_save_code, AMD64_RSP, r11_save_offset + orig_rsp_to_rbp_offset, i, sizeof(mgreg_t));
433 g_assert (r11_save_code == after_r11_save_code);
435 /* Copy from the save slot into the register array slot */
436 amd64_mov_reg_membase (code, i, AMD64_RSP, r11_save_offset + orig_rsp_to_rbp_offset, sizeof(mgreg_t));
437 amd64_mov_membase_reg (code, AMD64_RBP, saved_regs_offset + (i * sizeof(mgreg_t)), i, sizeof(mgreg_t));
440 for (i = 0; i < 8; ++i)
441 amd64_movsd_membase_reg (code, AMD64_RBP, saved_fpregs_offset + (i * sizeof(mgreg_t)), i);
443 /* Check that the stack is aligned */
444 #if defined(__default_codegen__)
445 amd64_mov_reg_reg (code, AMD64_R11, AMD64_RSP, sizeof (mgreg_t));
446 amd64_alu_reg_imm (code, X86_AND, AMD64_R11, 15);
447 amd64_alu_reg_imm (code, X86_CMP, AMD64_R11, 0);
449 amd64_branch_disp (code, X86_CC_Z, 0, FALSE);
451 amd64_mov_reg_imm (code, AMD64_R11, 0);
452 amd64_mov_reg_membase (code, AMD64_R11, AMD64_R11, 0, 8);
454 amd64_mov_reg_imm (code, MONO_AMD64_ARG_REG1, tramp_type);
455 amd64_mov_reg_imm (code, AMD64_R11, stack_unaligned);
456 amd64_call_reg (code, AMD64_R11);
458 mono_amd64_patch (br [0], code);
459 //amd64_breakpoint (code);
462 if (tramp_type != MONO_TRAMPOLINE_HANDLER_BLOCK_GUARD) {
463 /* Obtain the trampoline argument which is encoded in the instruction stream */
465 /* Load the GOT offset */
466 amd64_mov_reg_membase (code, AMD64_R11, AMD64_RBP, tramp_offset, sizeof(gpointer));
467 #if defined(__default_codegen__)
469 * r11 points to a call *<offset>(%rip) instruction, load the
470 * pc-relative offset from the instruction itself.
472 amd64_mov_reg_membase (code, AMD64_RAX, AMD64_R11, 3, 4);
473 /* 7 is the length of the call, 8 is the offset to the next got slot */
474 amd64_alu_reg_imm_size (code, X86_ADD, AMD64_RAX, 7 + sizeof (gpointer), sizeof(gpointer));
475 #elif defined(__native_client_codegen__)
476 /* The arg is hidden in a "push imm32" instruction, */
477 /* add one to skip the opcode. */
478 amd64_mov_reg_membase (code, AMD64_RAX, AMD64_R11, kNaClTrampOffset+1, 4);
480 /* Compute the address of the GOT slot */
481 amd64_alu_reg_reg_size (code, X86_ADD, AMD64_R11, AMD64_RAX, sizeof(gpointer));
483 amd64_mov_reg_membase (code, AMD64_R11, AMD64_R11, 0, sizeof(gpointer));
485 amd64_mov_reg_membase (code, AMD64_R11, AMD64_RBP, tramp_offset, sizeof(gpointer));
486 #if defined(__default_codegen__)
487 amd64_mov_reg_membase (code, AMD64_RAX, AMD64_R11, 5, 1);
488 amd64_widen_reg (code, AMD64_RAX, AMD64_RAX, TRUE, FALSE);
489 amd64_alu_reg_imm_size (code, X86_CMP, AMD64_RAX, 4, 1);
491 x86_branch8 (code, X86_CC_NE, 6, FALSE);
492 /* 32 bit immediate */
493 amd64_mov_reg_membase (code, AMD64_R11, AMD64_R11, 6, 4);
495 x86_jump8 (code, 10);
496 /* 64 bit immediate */
497 mono_amd64_patch (br [0], code);
498 amd64_mov_reg_membase (code, AMD64_R11, AMD64_R11, 6, 8);
499 mono_amd64_patch (br [1], code);
500 #elif defined(__native_client_codegen__)
501 /* All args are 32-bit pointers in NaCl */
502 amd64_mov_reg_membase (code, AMD64_R11, AMD64_R11, 6, 4);
505 amd64_mov_membase_reg (code, AMD64_RBP, arg_offset, AMD64_R11, sizeof(gpointer));
507 amd64_mov_reg_membase (code, AMD64_R11, AMD64_RBP, saved_regs_offset + (MONO_AMD64_ARG_REG1 * sizeof(mgreg_t)), sizeof(mgreg_t));
508 amd64_mov_membase_reg (code, AMD64_RBP, arg_offset, AMD64_R11, sizeof(gpointer));
515 amd64_mov_reg_membase (code, AMD64_R11, AMD64_RBP, 8, sizeof(gpointer));
517 amd64_mov_reg_imm (code, AMD64_R11, 0);
518 amd64_mov_membase_reg (code, AMD64_RBP, lmf_offset + MONO_STRUCT_OFFSET (MonoLMF, rip), AMD64_R11, sizeof(mgreg_t));
520 amd64_mov_reg_reg (code, AMD64_R11, AMD64_RSP, sizeof(mgreg_t));
521 amd64_alu_reg_imm (code, X86_ADD, AMD64_R11, framesize + 16);
522 amd64_mov_membase_reg (code, AMD64_RBP, lmf_offset + MONO_STRUCT_OFFSET (MonoLMF, rsp), AMD64_R11, sizeof(mgreg_t));
523 /* Save pointer to context */
524 amd64_lea_membase (code, AMD64_R11, AMD64_RBP, ctx_offset);
525 amd64_mov_membase_reg (code, AMD64_RBP, lmf_offset + MONO_STRUCT_OFFSET (MonoLMFTramp, ctx), AMD64_R11, sizeof(mgreg_t));
528 code = mono_arch_emit_load_aotconst (buf, code, &ji, MONO_PATCH_INFO_JIT_ICALL_ADDR, "mono_get_lmf_addr");
530 amd64_mov_reg_imm (code, AMD64_R11, mono_get_lmf_addr);
532 amd64_call_reg (code, AMD64_R11);
535 amd64_mov_membase_reg (code, AMD64_RBP, lmf_offset + MONO_STRUCT_OFFSET (MonoLMFTramp, lmf_addr), AMD64_RAX, sizeof(gpointer));
536 /* Save previous_lmf */
537 /* Set the lowest bit to signal that this LMF has the ip field set */
538 /* Set the third lowest bit to signal that this is a MonoLMFTramp structure */
539 amd64_mov_reg_membase (code, AMD64_R11, AMD64_RAX, 0, sizeof(gpointer));
540 amd64_alu_reg_imm_size (code, X86_ADD, AMD64_R11, 0x5, sizeof(gpointer));
541 amd64_mov_membase_reg (code, AMD64_RBP, lmf_offset + MONO_STRUCT_OFFSET (MonoLMF, previous_lmf), AMD64_R11, sizeof(gpointer));
543 amd64_lea_membase (code, AMD64_R11, AMD64_RBP, lmf_offset);
544 amd64_mov_membase_reg (code, AMD64_RAX, 0, AMD64_R11, sizeof(gpointer));
548 /* Arg1 is the pointer to the saved registers */
549 amd64_lea_membase (code, AMD64_ARG_REG1, AMD64_RBP, saved_regs_offset);
551 /* Arg2 is the address of the calling code */
553 amd64_mov_reg_membase (code, AMD64_ARG_REG2, AMD64_RBP, 8, sizeof(gpointer));
555 amd64_mov_reg_imm (code, AMD64_ARG_REG2, 0);
557 /* Arg3 is the method/vtable ptr */
558 amd64_mov_reg_membase (code, AMD64_ARG_REG3, AMD64_RBP, arg_offset, sizeof(gpointer));
560 /* Arg4 is the trampoline address */
561 amd64_mov_reg_membase (code, AMD64_ARG_REG4, AMD64_RBP, tramp_offset, sizeof(gpointer));
564 char *icall_name = g_strdup_printf ("trampoline_func_%d", tramp_type);
565 code = mono_arch_emit_load_aotconst (buf, code, &ji, MONO_PATCH_INFO_JIT_ICALL_ADDR, icall_name);
567 tramp = (guint8*)mono_get_trampoline_func (tramp_type);
568 amd64_mov_reg_imm (code, AMD64_R11, tramp);
570 amd64_call_reg (code, AMD64_R11);
572 /* Check for thread interruption */
573 /* This is not perf critical code so no need to check the interrupt flag */
575 * Have to call the _force_ variant, since there could be a protected wrapper on the top of the stack.
577 amd64_mov_membase_reg (code, AMD64_RBP, res_offset, AMD64_RAX, sizeof(mgreg_t));
579 code = mono_arch_emit_load_aotconst (buf, code, &ji, MONO_PATCH_INFO_JIT_ICALL_ADDR, "mono_thread_force_interruption_checkpoint");
581 amd64_mov_reg_imm (code, AMD64_R11, (guint8*)mono_thread_force_interruption_checkpoint);
583 amd64_call_reg (code, AMD64_R11);
585 amd64_mov_reg_membase (code, AMD64_RAX, AMD64_RBP, res_offset, sizeof(mgreg_t));
588 amd64_mov_reg_membase (code, AMD64_RCX, AMD64_RBP, lmf_offset + MONO_STRUCT_OFFSET (MonoLMF, previous_lmf), sizeof(gpointer));
589 amd64_alu_reg_imm_size (code, X86_SUB, AMD64_RCX, 0x5, sizeof(gpointer));
590 amd64_mov_reg_membase (code, AMD64_R11, AMD64_RBP, lmf_offset + MONO_STRUCT_OFFSET (MonoLMFTramp, lmf_addr), sizeof(gpointer));
591 amd64_mov_membase_reg (code, AMD64_R11, 0, AMD64_RCX, sizeof(gpointer));
594 * Save rax to the stack, after the leave instruction, this will become part of
597 amd64_mov_membase_reg (code, AMD64_RBP, rax_offset, AMD64_RAX, sizeof(mgreg_t));
599 /* Restore argument registers, r10 (imt method/rgxtx)
600 and rax (needed for direct calls to C vararg functions). */
601 for (i = 0; i < AMD64_NREG; ++i)
602 if (AMD64_IS_ARGUMENT_REG (i) || i == AMD64_R10 || i == AMD64_RAX)
603 amd64_mov_reg_membase (code, i, AMD64_RBP, saved_regs_offset + (i * sizeof(mgreg_t)), sizeof(mgreg_t));
604 for (i = 0; i < 8; ++i)
605 amd64_movsd_reg_membase (code, i, AMD64_RBP, saved_fpregs_offset + (i * sizeof(mgreg_t)));
609 cfa_offset -= sizeof (mgreg_t);
610 mono_add_unwind_op_def_cfa (unwind_ops, code, buf, AMD64_RSP, cfa_offset);
613 if (MONO_TRAMPOLINE_TYPE_MUST_RETURN (tramp_type)) {
615 amd64_mov_reg_membase (code, AMD64_RAX, AMD64_RSP, rax_offset - sizeof(mgreg_t), sizeof(mgreg_t));
618 /* call the compiled method using the saved rax */
619 amd64_jump_membase (code, AMD64_RSP, rax_offset - sizeof(mgreg_t));
622 g_assert ((code - buf) <= kMaxCodeSize);
624 nacl_global_codeman_validate (&buf, kMaxCodeSize, &code);
626 mono_arch_flush_icache (buf, code - buf);
627 mono_profiler_code_buffer_new (buf, code - buf, MONO_PROFILER_CODE_BUFFER_HELPER, NULL);
629 tramp_name = mono_get_generic_trampoline_name (tramp_type);
630 *info = mono_tramp_info_create (tramp_name, buf, code - buf, ji, unwind_ops);
637 mono_arch_create_specific_trampoline (gpointer arg1, MonoTrampolineType tramp_type, MonoDomain *domain, guint32 *code_len)
639 guint8 *code, *buf, *tramp;
641 gboolean far_addr = FALSE;
643 tramp = mono_get_trampoline_code (tramp_type);
645 #if defined(__default_codegen__)
646 if ((((guint64)arg1) >> 32) == 0)
651 code = buf = mono_domain_code_reserve_align (domain, size, 1);
653 if (((gint64)tramp - (gint64)code) >> 31 != 0 && ((gint64)tramp - (gint64)code) >> 31 != -1) {
654 #ifndef MONO_ARCH_NOMAP32BIT
655 g_assert_not_reached ();
659 code = buf = mono_domain_code_reserve_align (domain, size, 1);
661 #elif defined(__native_client_codegen__)
663 /* Aligning the call site below could */
664 /* add up to kNaClAlignment-1 bytes */
665 size += (kNaClAlignment-1);
666 size = NACL_BUNDLE_ALIGN_UP (size);
667 buf = mono_domain_code_reserve_align (domain, size, kNaClAlignment);
672 amd64_mov_reg_imm (code, AMD64_R11, tramp);
673 amd64_call_reg (code, AMD64_R11);
675 amd64_call_code (code, tramp);
677 /* The trampoline code will obtain the argument from the instruction stream */
678 #if defined(__default_codegen__)
679 if ((((guint64)arg1) >> 32) == 0) {
681 *(guint32*)(code + 1) = (gint64)arg1;
685 *(guint64*)(code + 1) = (gint64)arg1;
688 #elif defined(__native_client_codegen__)
689 /* For NaCl, all tramp args are 32-bit because they're pointers */
690 *code = 0x68; /* push imm32 */
691 *(guint32*)(code + 1) = (gint32)arg1;
695 g_assert ((code - buf) <= size);
700 nacl_domain_code_validate(domain, &buf, size, &code);
702 mono_arch_flush_icache (buf, size);
703 mono_profiler_code_buffer_new (buf, code - buf, MONO_PROFILER_CODE_BUFFER_SPECIFIC_TRAMPOLINE, mono_get_generic_trampoline_simple_name (tramp_type));
709 mono_arch_create_rgctx_lazy_fetch_trampoline (guint32 slot, MonoTrampInfo **info, gboolean aot)
713 guint8 **rgctx_null_jumps;
718 MonoJumpInfo *ji = NULL;
721 mrgctx = MONO_RGCTX_SLOT_IS_MRGCTX (slot);
722 index = MONO_RGCTX_SLOT_INDEX (slot);
724 index += MONO_SIZEOF_METHOD_RUNTIME_GENERIC_CONTEXT / sizeof (gpointer);
725 for (depth = 0; ; ++depth) {
726 int size = mono_class_rgctx_get_array_size (depth, mrgctx);
728 if (index < size - 1)
733 tramp_size = NACL_SIZE (64 + 8 * depth, 128 + 8 * depth);
735 code = buf = mono_global_codeman_reserve (tramp_size);
737 unwind_ops = mono_arch_get_cie_program ();
739 rgctx_null_jumps = g_malloc (sizeof (guint8*) * (depth + 2));
743 amd64_mov_reg_reg (code, AMD64_RAX, AMD64_ARG_REG1, 8);
745 /* load rgctx ptr from vtable */
746 amd64_mov_reg_membase (code, AMD64_RAX, AMD64_ARG_REG1, MONO_STRUCT_OFFSET (MonoVTable, runtime_generic_context), sizeof(gpointer));
747 /* is the rgctx ptr null? */
748 amd64_test_reg_reg (code, AMD64_RAX, AMD64_RAX);
749 /* if yes, jump to actual trampoline */
750 rgctx_null_jumps [0] = code;
751 amd64_branch8 (code, X86_CC_Z, -1, 1);
754 for (i = 0; i < depth; ++i) {
755 /* load ptr to next array */
756 if (mrgctx && i == 0)
757 amd64_mov_reg_membase (code, AMD64_RAX, AMD64_RAX, MONO_SIZEOF_METHOD_RUNTIME_GENERIC_CONTEXT, sizeof(gpointer));
759 amd64_mov_reg_membase (code, AMD64_RAX, AMD64_RAX, 0, sizeof(gpointer));
760 /* is the ptr null? */
761 amd64_test_reg_reg (code, AMD64_RAX, AMD64_RAX);
762 /* if yes, jump to actual trampoline */
763 rgctx_null_jumps [i + 1] = code;
764 amd64_branch8 (code, X86_CC_Z, -1, 1);
768 amd64_mov_reg_membase (code, AMD64_RAX, AMD64_RAX, sizeof (gpointer) * (index + 1), sizeof(gpointer));
769 /* is the slot null? */
770 amd64_test_reg_reg (code, AMD64_RAX, AMD64_RAX);
771 /* if yes, jump to actual trampoline */
772 rgctx_null_jumps [depth + 1] = code;
773 amd64_branch8 (code, X86_CC_Z, -1, 1);
774 /* otherwise return */
777 for (i = mrgctx ? 1 : 0; i <= depth + 1; ++i)
778 mono_amd64_patch (rgctx_null_jumps [i], code);
780 g_free (rgctx_null_jumps);
782 /* move the rgctx pointer to the VTABLE register */
783 amd64_mov_reg_reg (code, MONO_ARCH_VTABLE_REG, AMD64_ARG_REG1, sizeof(gpointer));
786 code = mono_arch_emit_load_aotconst (buf, code, &ji, MONO_PATCH_INFO_JIT_ICALL_ADDR, g_strdup_printf ("specific_trampoline_lazy_fetch_%u", slot));
787 amd64_jump_reg (code, AMD64_R11);
789 tramp = mono_arch_create_specific_trampoline (GUINT_TO_POINTER (slot), MONO_TRAMPOLINE_RGCTX_LAZY_FETCH, mono_get_root_domain (), NULL);
791 /* jump to the actual trampoline */
792 amd64_jump_code (code, tramp);
795 nacl_global_codeman_validate (&buf, tramp_size, &code);
796 mono_arch_flush_icache (buf, code - buf);
797 mono_profiler_code_buffer_new (buf, code - buf, MONO_PROFILER_CODE_BUFFER_GENERICS_TRAMPOLINE, NULL);
799 g_assert (code - buf <= tramp_size);
801 char *name = mono_get_rgctx_fetch_trampoline_name (slot);
802 *info = mono_tramp_info_create (name, buf, code - buf, ji, unwind_ops);
809 mono_arch_invalidate_method (MonoJitInfo *ji, void *func, gpointer func_arg)
811 /* FIXME: This is not thread safe */
812 guint8 *code = ji->code_start;
814 amd64_mov_reg_imm (code, AMD64_ARG_REG1, func_arg);
815 amd64_mov_reg_imm (code, AMD64_R11, func);
817 x86_push_imm (code, (guint64)func_arg);
818 amd64_call_reg (code, AMD64_R11);
823 handler_block_trampoline_helper (gpointer *ptr)
825 MonoJitTlsData *jit_tls = mono_native_tls_get_value (mono_jit_tls_id);
826 *ptr = jit_tls->handler_block_return_address;
830 mono_arch_create_handler_block_trampoline (MonoTrampInfo **info, gboolean aot)
832 guint8 *tramp = mono_get_trampoline_code (MONO_TRAMPOLINE_HANDLER_BLOCK_GUARD);
835 MonoJumpInfo *ji = NULL;
840 code = buf = mono_global_codeman_reserve (tramp_size);
842 unwind_ops = mono_arch_get_cie_program ();
845 This trampoline restore the call chain of the handler block then jumps into the code that deals with it.
847 if (mono_get_jit_tls_offset () != -1) {
848 code = mono_amd64_emit_tls_get (code, MONO_AMD64_ARG_REG1, mono_get_jit_tls_offset ());
849 amd64_mov_reg_membase (code, MONO_AMD64_ARG_REG1, MONO_AMD64_ARG_REG1, MONO_STRUCT_OFFSET (MonoJitTlsData, handler_block_return_address), 8);
850 /* Simulate a call */
851 amd64_push_reg (code, AMD64_RAX);
852 mono_add_unwind_op_def_cfa_offset (unwind_ops, code, buf, 16);
853 amd64_jump_code (code, tramp);
855 /*Slow path uses a c helper*/
856 amd64_mov_reg_reg (code, MONO_AMD64_ARG_REG1, AMD64_RSP, 8);
857 amd64_mov_reg_imm (code, AMD64_RAX, tramp);
858 amd64_push_reg (code, AMD64_RAX);
859 mono_add_unwind_op_def_cfa_offset (unwind_ops, code, buf, 16);
860 amd64_push_reg (code, AMD64_RAX);
861 mono_add_unwind_op_def_cfa_offset (unwind_ops, code, buf, 24);
862 amd64_jump_code (code, handler_block_trampoline_helper);
865 mono_arch_flush_icache (buf, code - buf);
866 mono_profiler_code_buffer_new (buf, code - buf, MONO_PROFILER_CODE_BUFFER_HELPER, NULL);
867 g_assert (code - buf <= tramp_size);
869 *info = mono_tramp_info_create ("handler_block_trampoline", buf, code - buf, ji, unwind_ops);
875 * mono_arch_get_call_target:
877 * Return the address called by the code before CODE if exists.
880 mono_arch_get_call_target (guint8 *code)
882 if (code [-5] == 0xe8) {
883 gint32 disp = *(gint32*)(code - 4);
884 guint8 *target = code + disp;
893 * mono_arch_get_plt_info_offset:
895 * Return the PLT info offset belonging to the plt entry PLT_ENTRY.
898 mono_arch_get_plt_info_offset (guint8 *plt_entry, mgreg_t *regs, guint8 *code)
900 #if defined(__native_client__) || defined(__native_client_codegen__)
901 /* 18 = 3 (mov opcode) + 4 (disp) + 10 (nacljmp) + 1 (push opcode) */
902 /* See aot-compiler.c arch_emit_plt_entry for details. */
903 return *(guint32*)(plt_entry + 18);
905 return *(guint32*)(plt_entry + 6);
910 * mono_arch_create_sdb_trampoline:
912 * Return a trampoline which captures the current context, passes it to
913 * debugger_agent_single_step_from_context ()/debugger_agent_breakpoint_from_context (),
914 * then restores the (potentially changed) context.
917 mono_arch_create_sdb_trampoline (gboolean single_step, MonoTrampInfo **info, gboolean aot)
919 int tramp_size = 256;
920 int i, framesize, ctx_offset, cfa_offset, gregs_offset;
922 GSList *unwind_ops = NULL;
923 MonoJumpInfo *ji = NULL;
925 code = buf = mono_global_codeman_reserve (tramp_size);
929 /* Reserve space where the callee can save the argument registers */
930 framesize += 4 * sizeof (mgreg_t);
933 ctx_offset = framesize;
934 framesize += sizeof (MonoContext);
936 framesize = ALIGN_TO (framesize, MONO_ARCH_FRAME_ALIGNMENT);
940 mono_add_unwind_op_def_cfa (unwind_ops, code, buf, AMD64_RSP, 8);
941 // IP saved at CFA - 8
942 mono_add_unwind_op_offset (unwind_ops, code, buf, AMD64_RIP, -cfa_offset);
944 amd64_push_reg (code, AMD64_RBP);
945 cfa_offset += sizeof(mgreg_t);
946 mono_add_unwind_op_def_cfa_offset (unwind_ops, code, buf, cfa_offset);
947 mono_add_unwind_op_offset (unwind_ops, code, buf, AMD64_RBP, - cfa_offset);
949 amd64_mov_reg_reg (code, AMD64_RBP, AMD64_RSP, sizeof(mgreg_t));
950 mono_add_unwind_op_def_cfa_reg (unwind_ops, code, buf, AMD64_RBP);
951 amd64_alu_reg_imm (code, X86_SUB, AMD64_RSP, framesize);
953 gregs_offset = ctx_offset + MONO_STRUCT_OFFSET (MonoContext, gregs);
955 /* Initialize a MonoContext structure on the stack */
956 for (i = 0; i < AMD64_NREG; ++i) {
957 if (i != AMD64_RIP && i != AMD64_RSP && i != AMD64_RBP)
958 amd64_mov_membase_reg (code, AMD64_RSP, gregs_offset + (i * sizeof (mgreg_t)), i, sizeof (mgreg_t));
960 amd64_mov_reg_membase (code, AMD64_R11, AMD64_RBP, 0, sizeof (mgreg_t));
961 amd64_mov_membase_reg (code, AMD64_RSP, gregs_offset + (AMD64_RBP * sizeof (mgreg_t)), AMD64_R11, sizeof (mgreg_t));
962 amd64_lea_membase (code, AMD64_R11, AMD64_RBP, 2 * sizeof (mgreg_t));
963 amd64_mov_membase_reg (code, AMD64_RSP, gregs_offset + (AMD64_RSP * sizeof (mgreg_t)), AMD64_R11, sizeof (mgreg_t));
964 amd64_mov_reg_membase (code, AMD64_R11, AMD64_RBP, sizeof (mgreg_t), sizeof (mgreg_t));
965 amd64_mov_membase_reg (code, AMD64_RSP, gregs_offset + (AMD64_RIP * sizeof (mgreg_t)), AMD64_R11, sizeof (mgreg_t));
967 /* Call the single step/breakpoint function in sdb */
968 amd64_lea_membase (code, AMD64_ARG_REG1, AMD64_RSP, ctx_offset);
972 code = mono_arch_emit_load_aotconst (buf, code, &ji, MONO_PATCH_INFO_JIT_ICALL_ADDR, "debugger_agent_single_step_from_context");
974 code = mono_arch_emit_load_aotconst (buf, code, &ji, MONO_PATCH_INFO_JIT_ICALL_ADDR, "debugger_agent_breakpoint_from_context");
977 amd64_mov_reg_imm (code, AMD64_R11, debugger_agent_single_step_from_context);
979 amd64_mov_reg_imm (code, AMD64_R11, debugger_agent_breakpoint_from_context);
981 amd64_call_reg (code, AMD64_R11);
983 /* Restore registers from ctx */
984 for (i = 0; i < AMD64_NREG; ++i) {
985 if (i != AMD64_RIP && i != AMD64_RSP && i != AMD64_RBP)
986 amd64_mov_reg_membase (code, i, AMD64_RSP, gregs_offset + (i * sizeof (mgreg_t)), sizeof (mgreg_t));
988 amd64_mov_reg_membase (code, AMD64_R11, AMD64_RSP, gregs_offset + (AMD64_RBP * sizeof (mgreg_t)), sizeof (mgreg_t));
989 amd64_mov_membase_reg (code, AMD64_RBP, 0, AMD64_R11, sizeof (mgreg_t));
990 amd64_mov_reg_membase (code, AMD64_R11, AMD64_RSP, gregs_offset + (AMD64_RIP * sizeof (mgreg_t)), sizeof (mgreg_t));
991 amd64_mov_membase_reg (code, AMD64_RBP, sizeof (mgreg_t), AMD64_R11, sizeof (mgreg_t));
994 cfa_offset -= sizeof (mgreg_t);
995 mono_add_unwind_op_def_cfa (unwind_ops, code, buf, AMD64_RSP, cfa_offset);
998 mono_arch_flush_icache (code, code - buf);
999 g_assert (code - buf <= tramp_size);
1001 const char *tramp_name = single_step ? "sdb_single_step_trampoline" : "sdb_breakpoint_trampoline";
1002 *info = mono_tramp_info_create (tramp_name, buf, code - buf, ji, unwind_ops);
1007 #if defined(ENABLE_GSHAREDVT)
1009 #include "../../../mono-extensions/mono/mini/tramp-amd64-gsharedvt.c"
1011 #endif /* !ENABLE_GSHAREDVT */