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 <sys/nacl_syscalls.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)
54 MonoDomain *domain = mono_domain_get ();
56 this_reg = mono_arch_get_this_arg_reg (NULL);
58 start = code = mono_domain_code_reserve (domain, 20);
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) < 20);
66 nacl_domain_code_validate (domain, &start, 20, &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)
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 if ((((guint64)(addr)) >> 32) != 0) {
162 #ifdef MONO_ARCH_NOMAP32BIT
163 /* Print some diagnostics */
164 MonoJitInfo *ji = mono_jit_info_table_find (mono_domain_get (), (char*)orig_code);
166 fprintf (stderr, "At %s, offset 0x%zx\n", mono_method_full_name (ji->method, TRUE), (guint8*)orig_code - (guint8*)ji->code_start);
167 fprintf (stderr, "Addr: %p\n", addr);
168 ji = mono_jit_info_table_find (mono_domain_get (), (char*)addr);
170 fprintf (stderr, "Callee: %s\n", mono_method_full_name (ji->method, TRUE));
171 g_assert_not_reached ();
174 * This might happen when calling AOTed code. Create a thunk.
176 guint8 *thunk_start, *thunk_code;
178 thunk_start = thunk_code = mono_domain_code_reserve (mono_domain_get (), 32);
179 amd64_jump_membase (thunk_code, AMD64_RIP, 0);
180 *(guint64*)thunk_code = (guint64)addr;
182 g_assert ((((guint64)(addr)) >> 32) == 0);
183 mono_arch_flush_icache (thunk_start, thunk_code - thunk_start);
186 g_assert ((((guint64)(orig_code)) >> 32) == 0);
188 InterlockedExchange ((gint32*)(orig_code - 4), ((gint64)addr - (gint64)orig_code));
189 VALGRIND_DISCARD_TRANSLATIONS (orig_code - 5, 4);
193 else if ((code [-7] == 0x41) && (code [-6] == 0xff) && (code [-5] == 0x15)) {
194 /* call *<OFFSET>(%rip) */
195 gpointer *got_entry = (gpointer*)((guint8*)orig_code + (*(guint32*)(orig_code - 4)));
197 InterlockedExchangePointer (got_entry, addr);
198 VALGRIND_DISCARD_TRANSLATIONS (orig_code - 5, sizeof (gpointer));
201 #elif defined(__native_client__)
202 /* These are essentially the same 2 cases as above, modified for NaCl*/
204 /* Target must be bundle-aligned */
205 g_assert (((guint32)addr & kNaClAlignmentMask) == 0);
206 /* Return target must be bundle-aligned */
207 g_assert (((guint32)orig_code & kNaClAlignmentMask) == 0);
209 if (orig_code[-5] == 0xe8) {
212 gint32 offset = (gint32)addr - (gint32)orig_code;
213 guint8 buf[sizeof(gint32)];
214 *((gint32*)(buf)) = offset;
215 ret = nacl_dyncode_modify (orig_code - sizeof(gint32), buf, sizeof(gint32));
219 else if (is_nacl_call_reg_sequence (orig_code - 10) && orig_code[-16] == 0x41 && orig_code[-15] == 0xbb) {
221 guint8 buf[sizeof(gint32)];
222 *((gint32 *)(buf)) = addr;
223 /* orig_code[-14] is the start of the immediate. */
224 ret = nacl_dyncode_modify (orig_code - 14, buf, sizeof(gint32));
228 g_assert_not_reached ();
236 mono_arch_patch_plt_entry (guint8 *code, gpointer *got, mgreg_t *regs, guint8 *addr)
239 gpointer *plt_jump_table_entry;
241 #if defined(__default_codegen__)
242 /* A PLT entry: jmp *<DISP>(%rip) */
243 g_assert (code [0] == 0xff);
244 g_assert (code [1] == 0x25);
246 disp = *(gint32*)(code + 2);
248 plt_jump_table_entry = (gpointer*)(code + 6 + disp);
249 #elif defined(__native_client_codegen__)
251 /* mov <DISP>(%rip), %r11d */
254 /* Verify the 'mov' */
255 g_assert (code [0] == 0x45);
256 g_assert (code [1] == 0x8b);
257 g_assert (code [2] == 0x1d);
259 disp = *(gint32*)(code + 3);
261 /* 7 = 3 (mov opcode) + 4 (disp) */
262 /* This needs to resolve to the target of the RIP-relative offset */
263 plt_jump_table_entry = (gpointer*)(code + 7 + disp);
265 #endif /* __native_client_codegen__ */
268 InterlockedExchangePointer (plt_jump_table_entry, addr);
272 get_vcall_slot (guint8 *code, mgreg_t *regs, int *displacement)
276 MonoJitInfo *ji = NULL;
279 /* code - 9 might be before the start of the method */
280 /* FIXME: Avoid this expensive call somehow */
281 ji = mono_jit_info_table_find (mono_domain_get (), (char*)code);
284 mono_breakpoint_clean_code (ji ? ji->code_start : NULL, code, 9, buf, sizeof (buf));
291 if ((code [0] == 0x41) && (code [1] == 0xff) && (code [2] == 0x15)) {
292 /* call OFFSET(%rip) */
293 g_assert_not_reached ();
294 *displacement = *(guint32*)(code + 3);
295 return (gpointer*)(code + disp + 7);
297 g_assert_not_reached ();
303 get_vcall_slot_addr (guint8* code, mgreg_t *regs)
307 vt = get_vcall_slot (code, regs, &displacement);
310 return (gpointer*)((char*)vt + displacement);
314 mono_arch_nullify_class_init_trampoline (guint8 *code, mgreg_t *regs)
317 MonoJitInfo *ji = NULL;
321 /* code - 7 might be before the start of the method */
322 /* FIXME: Avoid this expensive call somehow */
323 ji = mono_jit_info_table_find (mono_domain_get (), (char*)code);
326 can_write = mono_breakpoint_clean_code (ji ? ji->code_start : NULL, code, 7, buf, sizeof (buf));
332 * A given byte sequence can match more than case here, so we have to be
333 * really careful about the ordering of the cases. Longer sequences
336 if ((buf [0] == 0x41) && (buf [1] == 0xff) && (buf [2] == 0x15)) {
337 gpointer *vtable_slot;
339 /* call *<OFFSET>(%rip) */
340 vtable_slot = get_vcall_slot_addr (code, regs);
341 g_assert (vtable_slot);
343 *vtable_slot = nullified_class_init_trampoline;
344 } else if (buf [2] == 0xe8) {
346 //guint8 *buf = code - 2;
349 * It would be better to replace the call with nops, but that doesn't seem
350 * to work on SMP machines even when the whole call is inside a cache line.
351 * Patching the call address seems to work.
361 mono_arch_patch_callsite (code - 5, code, nullified_class_init_trampoline);
362 } else if ((buf [5] == 0xff) && x86_modrm_mod (buf [6]) == 3 && x86_modrm_reg (buf [6]) == 2) {
364 /* Generated by the LLVM JIT or on platforms without MAP_32BIT set */
365 mono_arch_patch_callsite (code - 13, code, nullified_class_init_trampoline);
366 } else if (buf [4] == 0x90 || buf [5] == 0xeb || buf [6] == 0x66) {
367 /* Already changed by another thread */
370 printf ("Invalid trampoline sequence: %x %x %x %x %x %x %x\n", buf [0], buf [1], buf [2], buf [3],
371 buf [4], buf [5], buf [6]);
372 g_assert_not_reached ();
377 mono_arch_nullify_plt_entry (guint8 *code, mgreg_t *regs)
379 if (mono_aot_only && !nullified_class_init_trampoline)
380 nullified_class_init_trampoline = mono_aot_get_trampoline ("nullified_class_init_trampoline");
382 mono_arch_patch_plt_entry (code, NULL, regs, nullified_class_init_trampoline);
386 mono_arch_create_generic_trampoline (MonoTrampolineType tramp_type, MonoTrampInfo **info, gboolean aot)
388 guint8 *buf, *code, *tramp, *br [2], *r11_save_code, *after_r11_save_code;
389 int i, lmf_offset, offset, res_offset, arg_offset, rax_offset, tramp_offset, saved_regs_offset;
390 int saved_fpregs_offset, rbp_offset, framesize, orig_rsp_to_rbp_offset, cfa_offset;
392 GSList *unwind_ops = NULL;
393 MonoJumpInfo *ji = NULL;
394 const guint kMaxCodeSize = NACL_SIZE (548, 548*2);
396 #if defined(__native_client_codegen__)
397 const guint kNaClTrampOffset = 17;
400 if (tramp_type == MONO_TRAMPOLINE_JUMP)
405 code = buf = mono_global_codeman_reserve (kMaxCodeSize);
407 framesize = kMaxCodeSize + sizeof (MonoLMF);
408 framesize = (framesize + (MONO_ARCH_FRAME_ALIGNMENT - 1)) & ~ (MONO_ARCH_FRAME_ALIGNMENT - 1);
410 orig_rsp_to_rbp_offset = 0;
411 r11_save_code = code;
412 /* Reserve 5 bytes for the mov_membase_reg to save R11 */
414 after_r11_save_code = code;
416 // CFA = sp + 16 (the trampoline address is on the stack)
418 mono_add_unwind_op_def_cfa (unwind_ops, code, buf, AMD64_RSP, 16);
419 // IP saved at CFA - 8
420 mono_add_unwind_op_offset (unwind_ops, code, buf, AMD64_RIP, -8);
422 /* Pop the return address off the stack */
423 amd64_pop_reg (code, AMD64_R11);
424 orig_rsp_to_rbp_offset += sizeof(mgreg_t);
426 cfa_offset -= sizeof(mgreg_t);
427 mono_add_unwind_op_def_cfa_offset (unwind_ops, code, buf, cfa_offset);
430 * Allocate a new stack frame
432 amd64_push_reg (code, AMD64_RBP);
433 cfa_offset += sizeof(mgreg_t);
434 mono_add_unwind_op_def_cfa_offset (unwind_ops, code, buf, cfa_offset);
435 mono_add_unwind_op_offset (unwind_ops, code, buf, AMD64_RBP, - cfa_offset);
437 orig_rsp_to_rbp_offset -= sizeof(mgreg_t);
438 amd64_mov_reg_reg (code, AMD64_RBP, AMD64_RSP, sizeof(mgreg_t));
439 mono_add_unwind_op_def_cfa_reg (unwind_ops, code, buf, AMD64_RBP);
440 amd64_alu_reg_imm (code, X86_SUB, AMD64_RSP, framesize);
443 rbp_offset = - offset;
445 offset += sizeof(mgreg_t);
446 rax_offset = - offset;
448 offset += sizeof(mgreg_t);
449 tramp_offset = - offset;
451 offset += sizeof(gpointer);
452 arg_offset = - offset;
454 /* Compute the trampoline address from the return address */
456 #if defined(__default_codegen__)
457 /* 7 = length of call *<offset>(rip) */
458 amd64_alu_reg_imm (code, X86_SUB, AMD64_R11, 7);
459 #elif defined(__native_client_codegen__)
460 amd64_alu_reg_imm (code, X86_SUB, AMD64_R11, kNaClTrampOffset);
463 /* 5 = length of amd64_call_membase () */
464 amd64_alu_reg_imm (code, X86_SUB, AMD64_R11, 5);
466 amd64_mov_membase_reg (code, AMD64_RBP, tramp_offset, AMD64_R11, sizeof(gpointer));
468 offset += sizeof(mgreg_t);
469 res_offset = - offset;
471 /* Save all registers */
473 offset += AMD64_NREG * sizeof(mgreg_t);
474 saved_regs_offset = - offset;
475 for (i = 0; i < AMD64_NREG; ++i) {
476 if (i == AMD64_RBP) {
477 /* RAX is already saved */
478 amd64_mov_reg_membase (code, AMD64_RAX, AMD64_RBP, rbp_offset, sizeof(mgreg_t));
479 amd64_mov_membase_reg (code, AMD64_RBP, saved_regs_offset + (i * sizeof(mgreg_t)), AMD64_RAX, sizeof(mgreg_t));
480 } else if (i != AMD64_R11) {
481 amd64_mov_membase_reg (code, AMD64_RBP, saved_regs_offset + (i * sizeof(mgreg_t)), i, sizeof(mgreg_t));
483 /* We have to save R11 right at the start of
484 the trampoline code because it's used as a
486 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));
487 g_assert (r11_save_code == after_r11_save_code);
490 offset += 8 * sizeof(mgreg_t);
491 saved_fpregs_offset = - offset;
492 for (i = 0; i < 8; ++i)
493 amd64_movsd_membase_reg (code, AMD64_RBP, saved_fpregs_offset + (i * sizeof(mgreg_t)), i);
495 if (tramp_type != MONO_TRAMPOLINE_GENERIC_CLASS_INIT &&
496 tramp_type != MONO_TRAMPOLINE_MONITOR_ENTER &&
497 tramp_type != MONO_TRAMPOLINE_MONITOR_EXIT) {
498 /* Obtain the trampoline argument which is encoded in the instruction stream */
500 /* Load the GOT offset */
501 amd64_mov_reg_membase (code, AMD64_R11, AMD64_RBP, tramp_offset, sizeof(gpointer));
502 #if defined(__default_codegen__)
503 amd64_mov_reg_membase (code, AMD64_RAX, AMD64_R11, 7, 4);
504 #elif defined(__native_client_codegen__)
505 /* The arg is hidden in a "push imm32" instruction, */
506 /* add one to skip the opcode. */
507 amd64_mov_reg_membase (code, AMD64_RAX, AMD64_R11, kNaClTrampOffset+1, 4);
509 /* Compute the address of the GOT slot */
510 amd64_alu_reg_reg_size (code, X86_ADD, AMD64_R11, AMD64_RAX, sizeof(gpointer));
512 amd64_mov_reg_membase (code, AMD64_R11, AMD64_R11, 0, sizeof(gpointer));
514 amd64_mov_reg_membase (code, AMD64_R11, AMD64_RBP, tramp_offset, sizeof(gpointer));
515 #if defined(__default_codegen__)
516 amd64_mov_reg_membase (code, AMD64_RAX, AMD64_R11, 5, 1);
517 amd64_widen_reg (code, AMD64_RAX, AMD64_RAX, TRUE, FALSE);
518 amd64_alu_reg_imm_size (code, X86_CMP, AMD64_RAX, 4, 1);
520 x86_branch8 (code, X86_CC_NE, 6, FALSE);
521 /* 32 bit immediate */
522 amd64_mov_reg_membase (code, AMD64_R11, AMD64_R11, 6, 4);
524 x86_jump8 (code, 10);
525 /* 64 bit immediate */
526 mono_amd64_patch (br [0], code);
527 amd64_mov_reg_membase (code, AMD64_R11, AMD64_R11, 6, 8);
528 mono_amd64_patch (br [1], code);
529 #elif defined(__native_client_codegen__)
530 /* All args are 32-bit pointers in NaCl */
531 amd64_mov_reg_membase (code, AMD64_R11, AMD64_R11, 6, 4);
534 amd64_mov_membase_reg (code, AMD64_RBP, arg_offset, AMD64_R11, sizeof(gpointer));
536 amd64_mov_reg_membase (code, AMD64_R11, AMD64_RBP, saved_regs_offset + (MONO_AMD64_ARG_REG1 * sizeof(mgreg_t)), sizeof(mgreg_t));
537 amd64_mov_membase_reg (code, AMD64_RBP, arg_offset, AMD64_R11, sizeof(gpointer));
542 offset += sizeof (MonoLMF);
543 lmf_offset = - offset;
547 amd64_mov_reg_membase (code, AMD64_R11, AMD64_RBP, 8, sizeof(gpointer));
549 amd64_mov_reg_imm (code, AMD64_R11, 0);
550 amd64_mov_membase_reg (code, AMD64_RBP, lmf_offset + G_STRUCT_OFFSET (MonoLMF, rip), AMD64_R11, sizeof(mgreg_t));
552 amd64_mov_reg_membase (code, AMD64_R11, AMD64_RSP, framesize, sizeof(mgreg_t));
553 amd64_mov_membase_reg (code, AMD64_RBP, lmf_offset + G_STRUCT_OFFSET (MonoLMF, rbp), AMD64_R11, sizeof(mgreg_t));
555 amd64_mov_reg_reg (code, AMD64_R11, AMD64_RSP, sizeof(mgreg_t));
556 amd64_alu_reg_imm (code, X86_ADD, AMD64_R11, framesize + 16);
557 amd64_mov_membase_reg (code, AMD64_RBP, lmf_offset + G_STRUCT_OFFSET (MonoLMF, rsp), AMD64_R11, sizeof(mgreg_t));
559 if (tramp_type == MONO_TRAMPOLINE_JIT || tramp_type == MONO_TRAMPOLINE_JUMP) {
560 amd64_mov_reg_membase (code, AMD64_R11, AMD64_RBP, arg_offset, sizeof(gpointer));
561 amd64_mov_membase_reg (code, AMD64_RBP, lmf_offset + G_STRUCT_OFFSET (MonoLMF, method), AMD64_R11, sizeof(gpointer));
563 amd64_mov_membase_imm (code, AMD64_RBP, lmf_offset + G_STRUCT_OFFSET (MonoLMF, method), 0, sizeof(gpointer));
565 /* Save callee saved regs */
567 amd64_mov_membase_reg (code, AMD64_RBP, lmf_offset + G_STRUCT_OFFSET (MonoLMF, rdi), AMD64_RDI, sizeof(mgreg_t));
568 amd64_mov_membase_reg (code, AMD64_RBP, lmf_offset + G_STRUCT_OFFSET (MonoLMF, rsi), AMD64_RSI, sizeof(mgreg_t));
570 amd64_mov_membase_reg (code, AMD64_RBP, lmf_offset + G_STRUCT_OFFSET (MonoLMF, rbx), AMD64_RBX, sizeof(mgreg_t));
571 amd64_mov_membase_reg (code, AMD64_RBP, lmf_offset + G_STRUCT_OFFSET (MonoLMF, r12), AMD64_R12, sizeof(mgreg_t));
572 amd64_mov_membase_reg (code, AMD64_RBP, lmf_offset + G_STRUCT_OFFSET (MonoLMF, r13), AMD64_R13, sizeof(mgreg_t));
573 amd64_mov_membase_reg (code, AMD64_RBP, lmf_offset + G_STRUCT_OFFSET (MonoLMF, r14), AMD64_R14, sizeof(mgreg_t));
574 amd64_mov_membase_reg (code, AMD64_RBP, lmf_offset + G_STRUCT_OFFSET (MonoLMF, r15), AMD64_R15, sizeof(mgreg_t));
577 code = mono_arch_emit_load_aotconst (buf, code, &ji, MONO_PATCH_INFO_JIT_ICALL_ADDR, "mono_get_lmf_addr");
579 amd64_mov_reg_imm (code, AMD64_R11, mono_get_lmf_addr);
581 amd64_call_reg (code, AMD64_R11);
584 amd64_mov_membase_reg (code, AMD64_RBP, lmf_offset + G_STRUCT_OFFSET (MonoLMF, lmf_addr), AMD64_RAX, sizeof(gpointer));
585 /* Save previous_lmf */
586 /* Set the lowest bit to 1 to signal that this LMF has the ip field set */
587 amd64_mov_reg_membase (code, AMD64_R11, AMD64_RAX, 0, sizeof(gpointer));
588 amd64_alu_reg_imm_size (code, X86_ADD, AMD64_R11, 1, sizeof(gpointer));
589 amd64_mov_membase_reg (code, AMD64_RBP, lmf_offset + G_STRUCT_OFFSET (MonoLMF, previous_lmf), AMD64_R11, sizeof(gpointer));
591 amd64_lea_membase (code, AMD64_R11, AMD64_RBP, lmf_offset);
592 amd64_mov_membase_reg (code, AMD64_RAX, 0, AMD64_R11, sizeof(gpointer));
596 /* Arg1 is the pointer to the saved registers */
597 amd64_lea_membase (code, AMD64_ARG_REG1, AMD64_RBP, saved_regs_offset);
599 /* Arg2 is the address of the calling code */
601 amd64_mov_reg_membase (code, AMD64_ARG_REG2, AMD64_RBP, 8, sizeof(gpointer));
603 amd64_mov_reg_imm (code, AMD64_ARG_REG2, 0);
605 /* Arg3 is the method/vtable ptr */
606 amd64_mov_reg_membase (code, AMD64_ARG_REG3, AMD64_RBP, arg_offset, sizeof(gpointer));
608 /* Arg4 is the trampoline address */
609 amd64_mov_reg_membase (code, AMD64_ARG_REG4, AMD64_RBP, tramp_offset, sizeof(gpointer));
612 char *icall_name = g_strdup_printf ("trampoline_func_%d", tramp_type);
613 code = mono_arch_emit_load_aotconst (buf, code, &ji, MONO_PATCH_INFO_JIT_ICALL_ADDR, icall_name);
615 tramp = (guint8*)mono_get_trampoline_func (tramp_type);
616 amd64_mov_reg_imm (code, AMD64_R11, tramp);
618 amd64_call_reg (code, AMD64_R11);
620 /* Check for thread interruption */
621 /* This is not perf critical code so no need to check the interrupt flag */
623 * Have to call the _force_ variant, since there could be a protected wrapper on the top of the stack.
625 amd64_mov_membase_reg (code, AMD64_RBP, res_offset, AMD64_RAX, sizeof(mgreg_t));
627 code = mono_arch_emit_load_aotconst (buf, code, &ji, MONO_PATCH_INFO_JIT_ICALL_ADDR, "mono_thread_force_interruption_checkpoint");
629 amd64_mov_reg_imm (code, AMD64_R11, (guint8*)mono_thread_force_interruption_checkpoint);
631 amd64_call_reg (code, AMD64_R11);
633 amd64_mov_reg_membase (code, AMD64_RAX, AMD64_RBP, res_offset, sizeof(mgreg_t));
637 amd64_mov_reg_membase (code, AMD64_RCX, AMD64_RBP, lmf_offset + G_STRUCT_OFFSET (MonoLMF, previous_lmf), sizeof(gpointer));
638 amd64_alu_reg_imm_size (code, X86_SUB, AMD64_RCX, 1, sizeof(gpointer));
639 amd64_mov_reg_membase (code, AMD64_R11, AMD64_RBP, lmf_offset + G_STRUCT_OFFSET (MonoLMF, lmf_addr), sizeof(gpointer));
640 amd64_mov_membase_reg (code, AMD64_R11, 0, AMD64_RCX, sizeof(gpointer));
643 * Save rax to the stack, after the leave instruction, this will become part of
646 amd64_mov_membase_reg (code, AMD64_RBP, rax_offset, AMD64_RAX, sizeof(mgreg_t));
648 /* Restore argument registers, r10 (imt method/rgxtx)
649 and rax (needed for direct calls to C vararg functions). */
650 for (i = 0; i < AMD64_NREG; ++i)
651 if (AMD64_IS_ARGUMENT_REG (i) || i == AMD64_R10 || i == AMD64_RAX)
652 amd64_mov_reg_membase (code, i, AMD64_RBP, saved_regs_offset + (i * sizeof(mgreg_t)), sizeof(mgreg_t));
654 for (i = 0; i < 8; ++i)
655 amd64_movsd_reg_membase (code, i, AMD64_RBP, saved_fpregs_offset + (i * sizeof(mgreg_t)));
660 if (MONO_TRAMPOLINE_TYPE_MUST_RETURN (tramp_type)) {
662 amd64_mov_reg_membase (code, AMD64_RAX, AMD64_RSP, rax_offset - sizeof(mgreg_t), sizeof(mgreg_t));
665 /* call the compiled method using the saved rax */
666 amd64_jump_membase (code, AMD64_RSP, rax_offset - sizeof(mgreg_t));
669 g_assert ((code - buf) <= kMaxCodeSize);
671 nacl_global_codeman_validate (&buf, kMaxCodeSize, &code);
673 mono_arch_flush_icache (buf, code - buf);
675 if (tramp_type == MONO_TRAMPOLINE_CLASS_INIT) {
676 /* Initialize the nullified class init trampoline used in the AOT case */
677 nullified_class_init_trampoline = mono_arch_get_nullified_class_init_trampoline (NULL);
681 *info = mono_tramp_info_create (mono_get_generic_trampoline_name (tramp_type), buf, code - buf, ji, unwind_ops);
687 mono_arch_get_nullified_class_init_trampoline (MonoTrampInfo **info)
691 code = buf = mono_global_codeman_reserve (16);
694 nacl_global_codeman_validate(&buf, 16, &code);
696 mono_arch_flush_icache (buf, code - buf);
699 *info = mono_tramp_info_create (g_strdup_printf ("nullified_class_init_trampoline"), buf, code - buf, NULL, NULL);
701 if (mono_jit_map_is_enabled ())
702 mono_emit_jit_tramp (buf, code - buf, "nullified_class_init_trampoline");
708 mono_arch_create_specific_trampoline (gpointer arg1, MonoTrampolineType tramp_type, MonoDomain *domain, guint32 *code_len)
710 guint8 *code, *buf, *tramp;
712 gboolean far_addr = FALSE;
714 tramp = mono_get_trampoline_code (tramp_type);
716 #if defined(__default_codegen__)
717 if ((((guint64)arg1) >> 32) == 0)
722 code = buf = mono_domain_code_reserve_align (domain, size, 1);
724 if (((gint64)tramp - (gint64)code) >> 31 != 0 && ((gint64)tramp - (gint64)code) >> 31 != -1) {
725 #ifndef MONO_ARCH_NOMAP32BIT
726 g_assert_not_reached ();
730 code = buf = mono_domain_code_reserve_align (domain, size, 1);
732 #elif defined(__native_client_codegen__)
734 /* Aligning the call site below could */
735 /* add up to kNaClAlignment-1 bytes */
736 size += (kNaClAlignment-1);
737 buf = mono_domain_code_reserve_align (domain, size, kNaClAlignment);
742 amd64_mov_reg_imm (code, AMD64_R11, tramp);
743 amd64_call_reg (code, AMD64_R11);
745 amd64_call_code (code, tramp);
747 /* The trampoline code will obtain the argument from the instruction stream */
748 #if defined(__default_codegen__)
749 if ((((guint64)arg1) >> 32) == 0) {
751 *(guint32*)(code + 1) = (gint64)arg1;
755 *(guint64*)(code + 1) = (gint64)arg1;
758 #elif defined(__native_client_codegen__)
759 /* For NaCl, all tramp args are 32-bit because they're pointers */
760 *code = 0x68; /* push imm32 */
761 *(guint32*)(code + 1) = (gint32)arg1;
765 g_assert ((code - buf) <= size);
770 nacl_domain_code_validate(domain, &buf, size, &code);
772 mono_arch_flush_icache (buf, size);
778 mono_arch_create_rgctx_lazy_fetch_trampoline (guint32 slot, MonoTrampInfo **info, gboolean aot)
782 guint8 **rgctx_null_jumps;
787 MonoJumpInfo *ji = NULL;
788 GSList *unwind_ops = NULL;
790 mrgctx = MONO_RGCTX_SLOT_IS_MRGCTX (slot);
791 index = MONO_RGCTX_SLOT_INDEX (slot);
793 index += MONO_SIZEOF_METHOD_RUNTIME_GENERIC_CONTEXT / sizeof (gpointer);
794 for (depth = 0; ; ++depth) {
795 int size = mono_class_rgctx_get_array_size (depth, mrgctx);
797 if (index < size - 1)
802 tramp_size = NACL_SIZE (64 + 8 * depth, 128 + 8 * depth);
804 code = buf = mono_global_codeman_reserve (tramp_size);
806 unwind_ops = mono_arch_get_cie_program ();
808 rgctx_null_jumps = g_malloc (sizeof (guint8*) * (depth + 2));
812 amd64_mov_reg_reg (code, AMD64_RAX, AMD64_ARG_REG1, 8);
814 /* load rgctx ptr from vtable */
815 amd64_mov_reg_membase (code, AMD64_RAX, AMD64_ARG_REG1, G_STRUCT_OFFSET (MonoVTable, runtime_generic_context), sizeof(gpointer));
816 /* is the rgctx ptr null? */
817 amd64_test_reg_reg (code, AMD64_RAX, AMD64_RAX);
818 /* if yes, jump to actual trampoline */
819 rgctx_null_jumps [0] = code;
820 amd64_branch8 (code, X86_CC_Z, -1, 1);
823 for (i = 0; i < depth; ++i) {
824 /* load ptr to next array */
825 if (mrgctx && i == 0)
826 amd64_mov_reg_membase (code, AMD64_RAX, AMD64_RAX, MONO_SIZEOF_METHOD_RUNTIME_GENERIC_CONTEXT, sizeof(gpointer));
828 amd64_mov_reg_membase (code, AMD64_RAX, AMD64_RAX, 0, sizeof(gpointer));
829 /* is the ptr null? */
830 amd64_test_reg_reg (code, AMD64_RAX, AMD64_RAX);
831 /* if yes, jump to actual trampoline */
832 rgctx_null_jumps [i + 1] = code;
833 amd64_branch8 (code, X86_CC_Z, -1, 1);
837 amd64_mov_reg_membase (code, AMD64_RAX, AMD64_RAX, sizeof (gpointer) * (index + 1), sizeof(gpointer));
838 /* is the slot null? */
839 amd64_test_reg_reg (code, AMD64_RAX, AMD64_RAX);
840 /* if yes, jump to actual trampoline */
841 rgctx_null_jumps [depth + 1] = code;
842 amd64_branch8 (code, X86_CC_Z, -1, 1);
843 /* otherwise return */
846 for (i = mrgctx ? 1 : 0; i <= depth + 1; ++i)
847 mono_amd64_patch (rgctx_null_jumps [i], code);
849 g_free (rgctx_null_jumps);
851 /* move the rgctx pointer to the VTABLE register */
852 amd64_mov_reg_reg (code, MONO_ARCH_VTABLE_REG, AMD64_ARG_REG1, sizeof(gpointer));
855 code = mono_arch_emit_load_aotconst (buf, code, &ji, MONO_PATCH_INFO_JIT_ICALL_ADDR, g_strdup_printf ("specific_trampoline_lazy_fetch_%u", slot));
856 amd64_jump_reg (code, AMD64_R11);
858 tramp = mono_arch_create_specific_trampoline (GUINT_TO_POINTER (slot), MONO_TRAMPOLINE_RGCTX_LAZY_FETCH, mono_get_root_domain (), NULL);
860 /* jump to the actual trampoline */
861 amd64_jump_code (code, tramp);
864 nacl_global_codeman_validate (&buf, tramp_size, &code);
865 mono_arch_flush_icache (buf, code - buf);
867 g_assert (code - buf <= tramp_size);
870 *info = mono_tramp_info_create (mono_get_rgctx_fetch_trampoline_name (slot), buf, code - buf, ji, unwind_ops);
876 mono_arch_create_generic_class_init_trampoline (MonoTrampInfo **info, gboolean aot)
880 static int byte_offset = -1;
881 static guint8 bitmask;
884 GSList *unwind_ops = NULL;
885 MonoJumpInfo *ji = NULL;
889 code = buf = mono_global_codeman_reserve (tramp_size);
892 mono_marshal_find_bitfield_offset (MonoVTable, initialized, &byte_offset, &bitmask);
894 amd64_test_membase_imm_size (code, MONO_AMD64_ARG_REG1, byte_offset, bitmask, 1);
896 amd64_branch8 (code, X86_CC_Z, -1, 1);
900 x86_patch (jump, code);
903 code = mono_arch_emit_load_aotconst (buf, code, &ji, MONO_PATCH_INFO_JIT_ICALL_ADDR, "specific_trampoline_generic_class_init");
904 amd64_jump_reg (code, AMD64_R11);
906 tramp = mono_arch_create_specific_trampoline (NULL, MONO_TRAMPOLINE_GENERIC_CLASS_INIT, mono_get_root_domain (), NULL);
908 /* jump to the actual trampoline */
909 amd64_jump_code (code, tramp);
912 nacl_global_codeman_validate (&buf, tramp_size, &code);
914 mono_arch_flush_icache (buf, code - buf);
916 g_assert (code - buf <= tramp_size);
919 *info = mono_tramp_info_create (g_strdup_printf ("generic_class_init_trampoline"), buf, code - buf, ji, unwind_ops);
924 #ifdef MONO_ARCH_MONITOR_OBJECT_REG
927 mono_arch_create_monitor_enter_trampoline (MonoTrampInfo **info, gboolean aot)
931 guint8 *jump_obj_null, *jump_sync_null, *jump_cmpxchg_failed, *jump_other_owner, *jump_tid, *jump_sync_thin_hash = NULL;
933 int owner_offset, nest_offset, dummy;
934 MonoJumpInfo *ji = NULL;
935 GSList *unwind_ops = NULL;
937 g_assert (MONO_ARCH_MONITOR_OBJECT_REG == AMD64_RDI);
939 mono_monitor_threads_sync_members_offset (&owner_offset, &nest_offset, &dummy);
940 g_assert (MONO_THREADS_SYNC_MEMBER_SIZE (owner_offset) == sizeof (gpointer));
941 g_assert (MONO_THREADS_SYNC_MEMBER_SIZE (nest_offset) == sizeof (guint32));
942 owner_offset = MONO_THREADS_SYNC_MEMBER_OFFSET (owner_offset);
943 nest_offset = MONO_THREADS_SYNC_MEMBER_OFFSET (nest_offset);
947 code = buf = mono_global_codeman_reserve (tramp_size);
949 unwind_ops = mono_arch_get_cie_program ();
951 if (mono_thread_get_tls_offset () != -1) {
952 /* MonoObject* obj is in RDI */
954 amd64_test_reg_reg (code, AMD64_RDI, AMD64_RDI);
955 /* if yes, jump to actual trampoline */
956 jump_obj_null = code;
957 amd64_branch8 (code, X86_CC_Z, -1, 1);
959 /* load obj->synchronization to RCX */
960 amd64_mov_reg_membase (code, AMD64_RCX, AMD64_RDI, G_STRUCT_OFFSET (MonoObject, synchronisation), 8);
962 if (mono_gc_is_moving ()) {
963 /*if bit zero is set it's a thin hash*/
964 /*FIXME use testb encoding*/
965 amd64_test_reg_imm (code, AMD64_RCX, 0x01);
966 jump_sync_thin_hash = code;
967 amd64_branch8 (code, X86_CC_NE, -1, 1);
969 /*clear bits used by the gc*/
970 amd64_alu_reg_imm (code, X86_AND, AMD64_RCX, ~0x3);
973 /* is synchronization null? */
974 amd64_test_reg_reg (code, AMD64_RCX, AMD64_RCX);
975 /* if yes, jump to actual trampoline */
976 jump_sync_null = code;
977 amd64_branch8 (code, X86_CC_Z, -1, 1);
979 /* load MonoInternalThread* into RDX */
980 code = mono_amd64_emit_tls_get (code, AMD64_RDX, mono_thread_get_tls_offset ());
981 /* load TID into RDX */
982 amd64_mov_reg_membase (code, AMD64_RDX, AMD64_RDX, G_STRUCT_OFFSET (MonoInternalThread, tid), 8);
984 /* is synchronization->owner null? */
985 amd64_alu_membase_imm_size (code, X86_CMP, AMD64_RCX, owner_offset, 0, 8);
986 /* if not, jump to next case */
988 amd64_branch8 (code, X86_CC_NZ, -1, 1);
990 /* if yes, try a compare-exchange with the TID */
992 amd64_alu_reg_reg (code, X86_XOR, AMD64_RAX, AMD64_RAX);
993 /* compare and exchange */
994 amd64_prefix (code, X86_LOCK_PREFIX);
995 amd64_cmpxchg_membase_reg_size (code, AMD64_RCX, owner_offset, AMD64_RDX, 8);
996 /* if not successful, jump to actual trampoline */
997 jump_cmpxchg_failed = code;
998 amd64_branch8 (code, X86_CC_NZ, -1, 1);
999 /* if successful, return */
1002 /* next case: synchronization->owner is not null */
1003 x86_patch (jump_tid, code);
1004 /* is synchronization->owner == TID? */
1005 amd64_alu_membase_reg_size (code, X86_CMP, AMD64_RCX, owner_offset, AMD64_RDX, 8);
1006 /* if not, jump to actual trampoline */
1007 jump_other_owner = code;
1008 amd64_branch8 (code, X86_CC_NZ, -1, 1);
1009 /* if yes, increment nest */
1010 amd64_inc_membase_size (code, AMD64_RCX, nest_offset, 4);
1014 x86_patch (jump_obj_null, code);
1015 if (jump_sync_thin_hash)
1016 x86_patch (jump_sync_thin_hash, code);
1017 x86_patch (jump_sync_null, code);
1018 x86_patch (jump_cmpxchg_failed, code);
1019 x86_patch (jump_other_owner, code);
1022 /* jump to the actual trampoline */
1023 #if MONO_AMD64_ARG_REG1 != AMD64_RDI
1024 amd64_mov_reg_reg (code, MONO_AMD64_ARG_REG1, AMD64_RDI);
1028 code = mono_arch_emit_load_aotconst (buf, code, &ji, MONO_PATCH_INFO_JIT_ICALL_ADDR, "specific_trampoline_monitor_enter");
1029 amd64_jump_reg (code, AMD64_R11);
1031 tramp = mono_arch_create_specific_trampoline (NULL, MONO_TRAMPOLINE_MONITOR_ENTER, mono_get_root_domain (), NULL);
1033 /* jump to the actual trampoline */
1034 amd64_jump_code (code, tramp);
1037 nacl_global_codeman_validate (&buf, tramp_size, &code);
1039 mono_arch_flush_icache (code, code - buf);
1040 g_assert (code - buf <= tramp_size);
1043 *info = mono_tramp_info_create (g_strdup_printf ("monitor_enter_trampoline"), buf, code - buf, ji, unwind_ops);
1049 mono_arch_create_monitor_exit_trampoline (MonoTrampInfo **info, gboolean aot)
1053 guint8 *jump_obj_null, *jump_have_waiters, *jump_sync_null, *jump_not_owned, *jump_sync_thin_hash = NULL;
1056 int owner_offset, nest_offset, entry_count_offset;
1057 MonoJumpInfo *ji = NULL;
1058 GSList *unwind_ops = NULL;
1060 g_assert (MONO_ARCH_MONITOR_OBJECT_REG == AMD64_RDI);
1062 mono_monitor_threads_sync_members_offset (&owner_offset, &nest_offset, &entry_count_offset);
1063 g_assert (MONO_THREADS_SYNC_MEMBER_SIZE (owner_offset) == sizeof (gpointer));
1064 g_assert (MONO_THREADS_SYNC_MEMBER_SIZE (nest_offset) == sizeof (guint32));
1065 g_assert (MONO_THREADS_SYNC_MEMBER_SIZE (entry_count_offset) == sizeof (gint32));
1066 owner_offset = MONO_THREADS_SYNC_MEMBER_OFFSET (owner_offset);
1067 nest_offset = MONO_THREADS_SYNC_MEMBER_OFFSET (nest_offset);
1068 entry_count_offset = MONO_THREADS_SYNC_MEMBER_OFFSET (entry_count_offset);
1072 code = buf = mono_global_codeman_reserve (tramp_size);
1074 unwind_ops = mono_arch_get_cie_program ();
1076 if (mono_thread_get_tls_offset () != -1) {
1077 /* MonoObject* obj is in RDI */
1079 amd64_test_reg_reg (code, AMD64_RDI, AMD64_RDI);
1080 /* if yes, jump to actual trampoline */
1081 jump_obj_null = code;
1082 amd64_branch8 (code, X86_CC_Z, -1, 1);
1084 /* load obj->synchronization to RCX */
1085 amd64_mov_reg_membase (code, AMD64_RCX, AMD64_RDI, G_STRUCT_OFFSET (MonoObject, synchronisation), 8);
1087 if (mono_gc_is_moving ()) {
1088 /*if bit zero is set it's a thin hash*/
1089 /*FIXME use testb encoding*/
1090 amd64_test_reg_imm (code, AMD64_RCX, 0x01);
1091 jump_sync_thin_hash = code;
1092 amd64_branch8 (code, X86_CC_NE, -1, 1);
1094 /*clear bits used by the gc*/
1095 amd64_alu_reg_imm (code, X86_AND, AMD64_RCX, ~0x3);
1098 /* is synchronization null? */
1099 amd64_test_reg_reg (code, AMD64_RCX, AMD64_RCX);
1100 /* if yes, jump to actual trampoline */
1101 jump_sync_null = code;
1102 amd64_branch8 (code, X86_CC_Z, -1, 1);
1104 /* next case: synchronization is not null */
1105 /* load MonoInternalThread* into RDX */
1106 code = mono_amd64_emit_tls_get (code, AMD64_RDX, mono_thread_get_tls_offset ());
1107 /* load TID into RDX */
1108 amd64_mov_reg_membase (code, AMD64_RDX, AMD64_RDX, G_STRUCT_OFFSET (MonoInternalThread, tid), 8);
1109 /* is synchronization->owner == TID */
1110 amd64_alu_membase_reg_size (code, X86_CMP, AMD64_RCX, owner_offset, AMD64_RDX, 8);
1111 /* if no, jump to actual trampoline */
1112 jump_not_owned = code;
1113 amd64_branch8 (code, X86_CC_NZ, -1, 1);
1115 /* next case: synchronization->owner == TID */
1116 /* is synchronization->nest == 1 */
1117 amd64_alu_membase_imm_size (code, X86_CMP, AMD64_RCX, nest_offset, 1, 4);
1118 /* if not, jump to next case */
1120 amd64_branch8 (code, X86_CC_NZ, -1, 1);
1121 /* if yes, is synchronization->entry_count zero? */
1122 amd64_alu_membase_imm_size (code, X86_CMP, AMD64_RCX, entry_count_offset, 0, 4);
1123 /* if not, jump to actual trampoline */
1124 jump_have_waiters = code;
1125 amd64_branch8 (code, X86_CC_NZ, -1 , 1);
1126 /* if yes, set synchronization->owner to null and return */
1127 amd64_mov_membase_imm (code, AMD64_RCX, owner_offset, 0, 8);
1130 /* next case: synchronization->nest is not 1 */
1131 x86_patch (jump_next, code);
1132 /* decrease synchronization->nest and return */
1133 amd64_dec_membase_size (code, AMD64_RCX, nest_offset, 4);
1136 x86_patch (jump_obj_null, code);
1137 x86_patch (jump_have_waiters, code);
1138 x86_patch (jump_not_owned, code);
1139 x86_patch (jump_sync_null, code);
1142 /* jump to the actual trampoline */
1143 #if MONO_AMD64_ARG_REG1 != AMD64_RDI
1144 amd64_mov_reg_reg (code, MONO_AMD64_ARG_REG1, AMD64_RDI);
1148 code = mono_arch_emit_load_aotconst (buf, code, &ji, MONO_PATCH_INFO_JIT_ICALL_ADDR, "specific_trampoline_monitor_exit");
1149 amd64_jump_reg (code, AMD64_R11);
1151 tramp = mono_arch_create_specific_trampoline (NULL, MONO_TRAMPOLINE_MONITOR_EXIT, mono_get_root_domain (), NULL);
1152 amd64_jump_code (code, tramp);
1155 nacl_global_codeman_validate (&buf, tramp_size, &code);
1157 mono_arch_flush_icache (code, code - buf);
1158 g_assert (code - buf <= tramp_size);
1161 *info = mono_tramp_info_create (g_strdup_printf ("monitor_exit_trampoline"), buf, code - buf, ji, unwind_ops);
1168 mono_arch_invalidate_method (MonoJitInfo *ji, void *func, gpointer func_arg)
1170 /* FIXME: This is not thread safe */
1171 guint8 *code = ji->code_start;
1173 amd64_mov_reg_imm (code, AMD64_ARG_REG1, func_arg);
1174 amd64_mov_reg_imm (code, AMD64_R11, func);
1176 x86_push_imm (code, (guint64)func_arg);
1177 amd64_call_reg (code, AMD64_R11);
1182 handler_block_trampoline_helper (gpointer *ptr)
1184 MonoJitTlsData *jit_tls = mono_native_tls_get_value (mono_jit_tls_id);
1185 *ptr = jit_tls->handler_block_return_address;
1189 mono_arch_create_handler_block_trampoline (void)
1191 guint8 *tramp = mono_get_trampoline_code (MONO_TRAMPOLINE_HANDLER_BLOCK_GUARD);
1193 int tramp_size = 64;
1194 code = buf = mono_global_codeman_reserve (tramp_size);
1197 This trampoline restore the call chain of the handler block then jumps into the code that deals with it.
1200 if (mono_get_jit_tls_offset () != -1) {
1201 code = mono_amd64_emit_tls_get (code, AMD64_RDI, mono_get_jit_tls_offset ());
1203 amd64_mov_reg_membase (code, AMD64_RDI, AMD64_RDI, G_STRUCT_OFFSET (MonoJitTlsData, handler_block_return_address), 8);
1204 amd64_jump_code (code, tramp);
1206 /*Slow path uses a c helper*/
1207 amd64_mov_reg_reg (code, AMD64_RDI, AMD64_RSP, 8);
1208 amd64_mov_reg_imm (code, AMD64_RAX, tramp);
1209 amd64_push_reg (code, AMD64_RAX);
1210 amd64_jump_code (code, handler_block_trampoline_helper);
1213 mono_arch_flush_icache (buf, code - buf);
1214 g_assert (code - buf <= tramp_size);
1216 if (mono_jit_map_is_enabled ())
1217 mono_emit_jit_tramp (buf, code - buf, "handler_block_trampoline");
1223 * mono_arch_get_call_target:
1225 * Return the address called by the code before CODE if exists.
1228 mono_arch_get_call_target (guint8 *code)
1230 if (code [-5] == 0xe8) {
1231 guint32 disp = *(guint32*)(code - 4);
1232 guint8 *target = code + disp;
1241 * mono_arch_get_plt_info_offset:
1243 * Return the PLT info offset belonging to the plt entry PLT_ENTRY.
1246 mono_arch_get_plt_info_offset (guint8 *plt_entry, mgreg_t *regs, guint8 *code)
1248 #if defined(__native_client__) || defined(__native_client_codegen__)
1249 /* 18 = 3 (mov opcode) + 4 (disp) + 10 (nacljmp) + 1 (push opcode) */
1250 /* See aot-compiler.c arch_emit_plt_entry for details. */
1251 return *(guint32*)(plt_entry + 18);
1253 return *(guint32*)(plt_entry + 6);