[jit] Don't emit a redundant move in lazy fetch trampolines on AMD64. (#5346)
[mono.git] / mono / mini / tramp-amd64.c
1 /**
2  * \file
3  * JIT trampoline code for amd64
4  *
5  * Authors:
6  *   Dietmar Maurer (dietmar@ximian.com)
7  *   Zoltan Varga (vargaz@gmail.com)
8  *   Johan Lorensson (lateralusx.github@gmail.com)
9  *
10  * (C) 2001 Ximian, Inc.
11  * Copyright 2003-2011 Novell, Inc (http://www.novell.com)
12  * Copyright 2011 Xamarin, Inc (http://www.xamarin.com)
13  * Licensed under the MIT license. See LICENSE file in the project root for full license information.
14  */
15
16 #include <config.h>
17 #include <glib.h>
18
19 #include <mono/metadata/abi-details.h>
20 #include <mono/metadata/appdomain.h>
21 #include <mono/metadata/marshal.h>
22 #include <mono/metadata/tabledefs.h>
23 #include <mono/metadata/profiler-private.h>
24 #include <mono/metadata/gc-internals.h>
25 #include <mono/arch/amd64/amd64-codegen.h>
26
27 #include <mono/utils/memcheck.h>
28
29 #include "mini.h"
30 #include "mini-amd64.h"
31 #include "debugger-agent.h"
32
33 #ifdef ENABLE_INTERPRETER
34 #include "interp/interp.h"
35 #endif
36
37 #define ALIGN_TO(val,align) ((((guint64)val) + ((align) - 1)) & ~((align) - 1))
38
39 #define IS_REX(inst) (((inst) >= 0x40) && ((inst) <= 0x4f))
40
41 #ifndef DISABLE_JIT
42 /*
43  * mono_arch_get_unbox_trampoline:
44  * @m: method pointer
45  * @addr: pointer to native code for @m
46  *
47  * when value type methods are called through the vtable we need to unbox the
48  * this argument. This method returns a pointer to a trampoline which does
49  * unboxing before calling the method
50  */
51 gpointer
52 mono_arch_get_unbox_trampoline (MonoMethod *m, gpointer addr)
53 {
54         guint8 *code, *start;
55         GSList *unwind_ops;
56         int this_reg, size = 20;
57
58         MonoDomain *domain = mono_domain_get ();
59
60         this_reg = mono_arch_get_this_arg_reg (NULL);
61
62         start = code = (guint8 *)mono_domain_code_reserve (domain, size + MONO_TRAMPOLINE_UNWINDINFO_SIZE(0));
63
64         unwind_ops = mono_arch_get_cie_program ();
65
66         amd64_alu_reg_imm (code, X86_ADD, this_reg, sizeof (MonoObject));
67         /* FIXME: Optimize this */
68         amd64_mov_reg_imm (code, AMD64_RAX, addr);
69         amd64_jump_reg (code, AMD64_RAX);
70         g_assert ((code - start) < size);
71         g_assert_checked (mono_arch_unwindinfo_validate_size (unwind_ops, MONO_TRAMPOLINE_UNWINDINFO_SIZE(0)));
72
73         mono_arch_flush_icache (start, code - start);
74         MONO_PROFILER_RAISE (jit_code_buffer, (start, code - start, MONO_PROFILER_CODE_BUFFER_UNBOX_TRAMPOLINE, m));
75
76         mono_tramp_info_register (mono_tramp_info_create (NULL, start, code - start, NULL, unwind_ops), domain);
77
78         return start;
79 }
80
81 /*
82  * mono_arch_get_static_rgctx_trampoline:
83  *
84  *   Create a trampoline which sets RGCTX_REG to ARG, then jumps to ADDR.
85  */
86 gpointer
87 mono_arch_get_static_rgctx_trampoline (gpointer arg, gpointer addr)
88 {
89         guint8 *code, *start;
90         GSList *unwind_ops;
91         int buf_len;
92
93         MonoDomain *domain = mono_domain_get ();
94
95 #ifdef MONO_ARCH_NOMAP32BIT
96         buf_len = 32;
97 #else
98         /* AOTed code could still have a non-32 bit address */
99         if ((((guint64)addr) >> 32) == 0)
100                 buf_len = 16;
101         else
102                 buf_len = 30;
103 #endif
104
105         start = code = (guint8 *)mono_domain_code_reserve (domain, buf_len + MONO_TRAMPOLINE_UNWINDINFO_SIZE(0));
106
107         unwind_ops = mono_arch_get_cie_program ();
108
109         amd64_mov_reg_imm (code, MONO_ARCH_RGCTX_REG, arg);
110         amd64_jump_code (code, addr);
111         g_assert ((code - start) < buf_len);
112         g_assert_checked (mono_arch_unwindinfo_validate_size (unwind_ops, MONO_TRAMPOLINE_UNWINDINFO_SIZE(0)));
113
114         mono_arch_flush_icache (start, code - start);
115         MONO_PROFILER_RAISE (jit_code_buffer, (start, code - start, MONO_PROFILER_CODE_BUFFER_GENERICS_TRAMPOLINE, NULL));
116
117         mono_tramp_info_register (mono_tramp_info_create (NULL, start, code - start, NULL, unwind_ops), domain);
118
119         return start;
120 }
121 #endif /* !DISABLE_JIT */
122
123 #ifdef _WIN64
124 // Workaround lack of Valgrind support for 64-bit Windows
125 #define VALGRIND_DISCARD_TRANSLATIONS(...)
126 #endif
127
128 /*
129  * mono_arch_patch_callsite:
130  *
131  *   Patch the callsite whose address is given by ORIG_CODE so it calls ADDR. ORIG_CODE
132  * points to the pc right after the call.
133  */
134 void
135 mono_arch_patch_callsite (guint8 *method_start, guint8 *orig_code, guint8 *addr)
136 {
137         guint8 *code;
138         guint8 buf [16];
139         gboolean can_write = mono_breakpoint_clean_code (method_start, orig_code, 14, buf, sizeof (buf));
140
141         code = buf + 14;
142
143         /* mov 64-bit imm into r11 (followed by call reg?)  or direct call*/
144         if (((code [-13] == 0x49) && (code [-12] == 0xbb)) || (code [-5] == 0xe8)) {
145                 if (code [-5] != 0xe8) {
146                         if (can_write) {
147                                 InterlockedExchangePointer ((gpointer*)(orig_code - 11), addr);
148                                 VALGRIND_DISCARD_TRANSLATIONS (orig_code - 11, sizeof (gpointer));
149                         }
150                 } else {
151                         gboolean disp_32bit = ((((gint64)addr - (gint64)orig_code)) < (1 << 30)) && ((((gint64)addr - (gint64)orig_code)) > -(1 << 30));
152
153                         if ((((guint64)(addr)) >> 32) != 0 && !disp_32bit) {
154                                 /* 
155                                  * This might happen with LLVM or when calling AOTed code. Create a thunk.
156                                  */
157                                 guint8 *thunk_start, *thunk_code;
158
159                                 thunk_start = thunk_code = (guint8 *)mono_domain_code_reserve (mono_domain_get (), 32);
160                                 amd64_jump_membase (thunk_code, AMD64_RIP, 0);
161                                 *(guint64*)thunk_code = (guint64)addr;
162                                 addr = thunk_start;
163                                 g_assert ((((guint64)(addr)) >> 32) == 0);
164                                 mono_arch_flush_icache (thunk_start, thunk_code - thunk_start);
165                                 MONO_PROFILER_RAISE (jit_code_buffer, (thunk_start, thunk_code - thunk_start, MONO_PROFILER_CODE_BUFFER_HELPER, NULL));
166                         }
167                         if (can_write) {
168                                 InterlockedExchange ((gint32*)(orig_code - 4), ((gint64)addr - (gint64)orig_code));
169                                 VALGRIND_DISCARD_TRANSLATIONS (orig_code - 5, 4);
170                         }
171                 }
172         }
173         else if ((code [-7] == 0x41) && (code [-6] == 0xff) && (code [-5] == 0x15)) {
174                 /* call *<OFFSET>(%rip) */
175                 gpointer *got_entry = (gpointer*)((guint8*)orig_code + (*(guint32*)(orig_code - 4)));
176                 if (can_write) {
177                         InterlockedExchangePointer (got_entry, addr);
178                         VALGRIND_DISCARD_TRANSLATIONS (orig_code - 5, sizeof (gpointer));
179                 }
180         }
181 }
182
183 #ifndef DISABLE_JIT
184 guint8*
185 mono_arch_create_llvm_native_thunk (MonoDomain *domain, guint8 *addr)
186 {
187         /*
188          * The caller is LLVM code and the call displacement might exceed 32 bits. We can't determine the caller address, so
189          * we add a thunk every time.
190          * Since the caller is also allocated using the domain code manager, hopefully the displacement will fit into 32 bits.
191          * FIXME: Avoid this if possible if !MONO_ARCH_NOMAP32BIT and ADDR is 32 bits.
192          */
193         guint8 *thunk_start, *thunk_code;
194
195         thunk_start = thunk_code = (guint8 *)mono_domain_code_reserve (mono_domain_get (), 32);
196         amd64_jump_membase (thunk_code, AMD64_RIP, 0);
197         *(guint64*)thunk_code = (guint64)addr;
198         addr = thunk_start;
199         mono_arch_flush_icache (thunk_start, thunk_code - thunk_start);
200         MONO_PROFILER_RAISE (jit_code_buffer, (thunk_start, thunk_code - thunk_start, MONO_PROFILER_CODE_BUFFER_HELPER, NULL));
201         return addr;
202 }
203 #endif /* !DISABLE_JIT */
204
205 void
206 mono_arch_patch_plt_entry (guint8 *code, gpointer *got, mgreg_t *regs, guint8 *addr)
207 {
208         gint32 disp;
209         gpointer *plt_jump_table_entry;
210
211         /* A PLT entry: jmp *<DISP>(%rip) */
212         g_assert (code [0] == 0xff);
213         g_assert (code [1] == 0x25);
214
215         disp = *(gint32*)(code + 2);
216
217         plt_jump_table_entry = (gpointer*)(code + 6 + disp);
218
219         InterlockedExchangePointer (plt_jump_table_entry, addr);
220 }
221
222 #ifndef DISABLE_JIT
223 static void
224 stack_unaligned (MonoTrampolineType tramp_type)
225 {
226         printf ("%d\n", tramp_type);
227         g_assert_not_reached ();
228 }
229
230 guchar*
231 mono_arch_create_generic_trampoline (MonoTrampolineType tramp_type, MonoTrampInfo **info, gboolean aot)
232 {
233         char *tramp_name;
234         guint8 *buf, *code, *tramp, *br [2], *r11_save_code, *after_r11_save_code, *br_ex_check;
235         int i, lmf_offset, offset, res_offset, arg_offset, rax_offset, ex_offset, tramp_offset, ctx_offset, saved_regs_offset;
236         int r11_save_offset, saved_fpregs_offset, rbp_offset, framesize, orig_rsp_to_rbp_offset, cfa_offset;
237         gboolean has_caller;
238         GSList *unwind_ops = NULL;
239         MonoJumpInfo *ji = NULL;
240         const guint kMaxCodeSize = 630;
241
242         if (tramp_type == MONO_TRAMPOLINE_JUMP)
243                 has_caller = FALSE;
244         else
245                 has_caller = TRUE;
246
247         code = buf = (guint8 *)mono_global_codeman_reserve (kMaxCodeSize + MONO_MAX_TRAMPOLINE_UNWINDINFO_SIZE);
248
249         /* Compute stack frame size and offsets */
250         offset = 0;
251         rbp_offset = -offset;
252
253         offset += sizeof(mgreg_t);
254         rax_offset = -offset;
255
256         offset += sizeof(mgreg_t);
257         ex_offset = -offset;
258
259         offset += sizeof(mgreg_t);
260         r11_save_offset = -offset;
261
262         offset += sizeof(mgreg_t);
263         tramp_offset = -offset;
264
265         offset += sizeof(gpointer);
266         arg_offset = -offset;
267
268         offset += sizeof(mgreg_t);
269         res_offset = -offset;
270
271         offset += sizeof (MonoContext);
272         ctx_offset = -offset;
273         saved_regs_offset = ctx_offset + MONO_STRUCT_OFFSET (MonoContext, gregs);
274         saved_fpregs_offset = ctx_offset + MONO_STRUCT_OFFSET (MonoContext, fregs);
275
276         offset += sizeof (MonoLMFTramp);
277         lmf_offset = -offset;
278
279 #ifdef TARGET_WIN32
280         /* Reserve space where the callee can save the argument registers */
281         offset += 4 * sizeof (mgreg_t);
282 #endif
283
284         framesize = ALIGN_TO (offset, MONO_ARCH_FRAME_ALIGNMENT);
285
286         // CFA = sp + 16 (the trampoline address is on the stack)
287         cfa_offset = 16;
288         mono_add_unwind_op_def_cfa (unwind_ops, code, buf, AMD64_RSP, 16);
289         // IP saved at CFA - 8
290         mono_add_unwind_op_offset (unwind_ops, code, buf, AMD64_RIP, -8);
291
292         orig_rsp_to_rbp_offset = 0;
293         r11_save_code = code;
294         /* Reserve space for the mov_membase_reg to save R11 */
295         code += 5;
296         after_r11_save_code = code;
297
298         /* Pop the return address off the stack */
299         amd64_pop_reg (code, AMD64_R11);
300         orig_rsp_to_rbp_offset += sizeof(mgreg_t);
301
302         cfa_offset -= sizeof(mgreg_t);
303         mono_add_unwind_op_def_cfa_offset (unwind_ops, code, buf, cfa_offset);
304
305         /* 
306          * Allocate a new stack frame
307          */
308         amd64_push_reg (code, AMD64_RBP);
309         cfa_offset += sizeof(mgreg_t);
310         mono_add_unwind_op_def_cfa_offset (unwind_ops, code, buf, cfa_offset);
311         mono_add_unwind_op_offset (unwind_ops, code, buf, AMD64_RBP, - cfa_offset);
312
313         orig_rsp_to_rbp_offset -= sizeof(mgreg_t);
314         amd64_mov_reg_reg (code, AMD64_RBP, AMD64_RSP, sizeof(mgreg_t));
315         mono_add_unwind_op_def_cfa_reg (unwind_ops, code, buf, AMD64_RBP);
316         mono_add_unwind_op_fp_alloc (unwind_ops, code, buf, AMD64_RBP, 0);
317         amd64_alu_reg_imm (code, X86_SUB, AMD64_RSP, framesize);
318
319         /* Compute the trampoline address from the return address */
320         if (aot) {
321                 /* 7 = length of call *<offset>(rip) */
322                 amd64_alu_reg_imm (code, X86_SUB, AMD64_R11, 7);
323         } else {
324                 /* 5 = length of amd64_call_membase () */
325                 amd64_alu_reg_imm (code, X86_SUB, AMD64_R11, 5);
326         }
327         amd64_mov_membase_reg (code, AMD64_RBP, tramp_offset, AMD64_R11, sizeof(gpointer));
328
329         /* Save all registers */
330         for (i = 0; i < AMD64_NREG; ++i) {
331                 if (i == AMD64_RBP) {
332                         /* RAX is already saved */
333                         amd64_mov_reg_membase (code, AMD64_RAX, AMD64_RBP, rbp_offset, sizeof(mgreg_t));
334                         amd64_mov_membase_reg (code, AMD64_RBP, saved_regs_offset + (i * sizeof(mgreg_t)), AMD64_RAX, sizeof(mgreg_t));
335                 } else if (i == AMD64_RIP) {
336                         if (has_caller)
337                                 amd64_mov_reg_membase (code, AMD64_R11, AMD64_RBP, 8, sizeof(gpointer));
338                         else
339                                 amd64_mov_reg_imm (code, AMD64_R11, 0);
340                         amd64_mov_membase_reg (code, AMD64_RBP, saved_regs_offset + (i * sizeof(mgreg_t)), AMD64_R11, sizeof(mgreg_t));
341                 } else if (i == AMD64_RSP) {
342                         amd64_mov_reg_reg (code, AMD64_R11, AMD64_RSP, sizeof(mgreg_t));
343                         amd64_alu_reg_imm (code, X86_ADD, AMD64_R11, framesize + 16);
344                         amd64_mov_membase_reg (code, AMD64_RBP, saved_regs_offset + (i * sizeof(mgreg_t)), AMD64_R11, sizeof(mgreg_t));
345                 } else if (i != AMD64_R11) {
346                         amd64_mov_membase_reg (code, AMD64_RBP, saved_regs_offset + (i * sizeof(mgreg_t)), i, sizeof(mgreg_t));
347                 } else {
348                         /* We have to save R11 right at the start of
349                            the trampoline code because it's used as a
350                            scratch register */
351                         /* This happens before the frame is set up, so it goes into the redzone */
352                         amd64_mov_membase_reg (r11_save_code, AMD64_RSP, r11_save_offset + orig_rsp_to_rbp_offset, i, sizeof(mgreg_t));
353                         g_assert (r11_save_code == after_r11_save_code);
354
355                         /* Copy from the save slot into the register array slot */
356                         amd64_mov_reg_membase (code, i, AMD64_RSP, r11_save_offset + orig_rsp_to_rbp_offset, sizeof(mgreg_t));
357                         amd64_mov_membase_reg (code, AMD64_RBP, saved_regs_offset + (i * sizeof(mgreg_t)), i, sizeof(mgreg_t));
358                 }
359                 /* cfa = rbp + cfa_offset */
360                 mono_add_unwind_op_offset (unwind_ops, code, buf, i, - cfa_offset + saved_regs_offset + (i * sizeof (mgreg_t)));
361         }
362         for (i = 0; i < 8; ++i)
363                 amd64_movsd_membase_reg (code, AMD64_RBP, saved_fpregs_offset + (i * sizeof(mgreg_t)), i);
364
365         /* Check that the stack is aligned */
366         amd64_mov_reg_reg (code, AMD64_R11, AMD64_RSP, sizeof (mgreg_t));
367         amd64_alu_reg_imm (code, X86_AND, AMD64_R11, 15);
368         amd64_alu_reg_imm (code, X86_CMP, AMD64_R11, 0);
369         br [0] = code;
370         amd64_branch_disp (code, X86_CC_Z, 0, FALSE);
371         if (aot) {
372                 amd64_mov_reg_imm (code, AMD64_R11, 0);
373                 amd64_mov_reg_membase (code, AMD64_R11, AMD64_R11, 0, 8);
374         } else {
375                 amd64_mov_reg_imm (code, MONO_AMD64_ARG_REG1, tramp_type);
376                 amd64_mov_reg_imm (code, AMD64_R11, stack_unaligned);
377                 amd64_call_reg (code, AMD64_R11);
378         }
379         mono_amd64_patch (br [0], code);
380         //amd64_breakpoint (code);
381
382         /* Obtain the trampoline argument which is encoded in the instruction stream */
383         if (aot) {
384                 /* Load the GOT offset */
385                 amd64_mov_reg_membase (code, AMD64_R11, AMD64_RBP, tramp_offset, sizeof(gpointer));
386                 /*
387                  * r11 points to a call *<offset>(%rip) instruction, load the
388                  * pc-relative offset from the instruction itself.
389                  */
390                 amd64_mov_reg_membase (code, AMD64_RAX, AMD64_R11, 3, 4);
391                 /* 7 is the length of the call, 8 is the offset to the next got slot */
392                 amd64_alu_reg_imm_size (code, X86_ADD, AMD64_RAX, 7 + sizeof (gpointer), sizeof(gpointer));
393                 /* Compute the address of the GOT slot */
394                 amd64_alu_reg_reg_size (code, X86_ADD, AMD64_R11, AMD64_RAX, sizeof(gpointer));
395                 /* Load the value */
396                 amd64_mov_reg_membase (code, AMD64_R11, AMD64_R11, 0, sizeof(gpointer));
397         } else {
398                 amd64_mov_reg_membase (code, AMD64_R11, AMD64_RBP, tramp_offset, sizeof(gpointer));
399                 amd64_mov_reg_membase (code, AMD64_RAX, AMD64_R11, 5, 1);
400                 amd64_widen_reg (code, AMD64_RAX, AMD64_RAX, TRUE, FALSE);
401                 amd64_alu_reg_imm_size (code, X86_CMP, AMD64_RAX, 4, 1);
402                 br [0] = code;
403                 x86_branch8 (code, X86_CC_NE, 6, FALSE);
404                 /* 32 bit immediate */
405                 amd64_mov_reg_membase (code, AMD64_R11, AMD64_R11, 6, 4);
406                 br [1] = code;
407                 x86_jump8 (code, 10);
408                 /* 64 bit immediate */
409                 mono_amd64_patch (br [0], code);
410                 amd64_mov_reg_membase (code, AMD64_R11, AMD64_R11, 6, 8);
411                 mono_amd64_patch (br [1], code);
412         }
413         amd64_mov_membase_reg (code, AMD64_RBP, arg_offset, AMD64_R11, sizeof(gpointer));
414
415         /* Save LMF begin */
416
417         /* Save ip */
418         if (has_caller)
419                 amd64_mov_reg_membase (code, AMD64_R11, AMD64_RBP, 8, sizeof(gpointer));
420         else
421                 amd64_mov_reg_imm (code, AMD64_R11, 0);
422         amd64_mov_membase_reg (code, AMD64_RBP, lmf_offset + MONO_STRUCT_OFFSET (MonoLMF, rip), AMD64_R11, sizeof(mgreg_t));
423         /* Save sp */
424         amd64_mov_reg_reg (code, AMD64_R11, AMD64_RSP, sizeof(mgreg_t));
425         amd64_alu_reg_imm (code, X86_ADD, AMD64_R11, framesize + 16);
426         amd64_mov_membase_reg (code, AMD64_RBP, lmf_offset + MONO_STRUCT_OFFSET (MonoLMF, rsp), AMD64_R11, sizeof(mgreg_t));
427         /* Save pointer to context */
428         amd64_lea_membase (code, AMD64_R11, AMD64_RBP, ctx_offset);
429         amd64_mov_membase_reg (code, AMD64_RBP, lmf_offset + MONO_STRUCT_OFFSET (MonoLMFTramp, ctx), AMD64_R11, sizeof(mgreg_t));
430
431         if (aot) {
432                 code = mono_arch_emit_load_aotconst (buf, code, &ji, MONO_PATCH_INFO_JIT_ICALL_ADDR, "mono_get_lmf_addr");
433         } else {
434                 amd64_mov_reg_imm (code, AMD64_R11, mono_get_lmf_addr);
435         }
436         amd64_call_reg (code, AMD64_R11);
437
438         /* Save lmf_addr */
439         amd64_mov_membase_reg (code, AMD64_RBP, lmf_offset + MONO_STRUCT_OFFSET (MonoLMFTramp, lmf_addr), AMD64_RAX, sizeof(gpointer));
440         /* Save previous_lmf */
441         /* Set the lowest bit to signal that this LMF has the ip field set */
442         /* Set the third lowest bit to signal that this is a MonoLMFTramp structure */
443         amd64_mov_reg_membase (code, AMD64_R11, AMD64_RAX, 0, sizeof(gpointer));
444         amd64_alu_reg_imm_size (code, X86_ADD, AMD64_R11, 0x5, sizeof(gpointer));
445         amd64_mov_membase_reg (code, AMD64_RBP, lmf_offset + MONO_STRUCT_OFFSET (MonoLMF, previous_lmf), AMD64_R11, sizeof(gpointer));
446         /* Set new lmf */
447         amd64_lea_membase (code, AMD64_R11, AMD64_RBP, lmf_offset);
448         amd64_mov_membase_reg (code, AMD64_RAX, 0, AMD64_R11, sizeof(gpointer));
449
450         /* Save LMF end */
451
452         /* Arg1 is the pointer to the saved registers */
453         amd64_lea_membase (code, AMD64_ARG_REG1, AMD64_RBP, saved_regs_offset);
454
455         /* Arg2 is the address of the calling code */
456         if (has_caller)
457                 amd64_mov_reg_membase (code, AMD64_ARG_REG2, AMD64_RBP, 8, sizeof(gpointer));
458         else
459                 amd64_mov_reg_imm (code, AMD64_ARG_REG2, 0);
460
461         /* Arg3 is the method/vtable ptr */
462         amd64_mov_reg_membase (code, AMD64_ARG_REG3, AMD64_RBP, arg_offset, sizeof(gpointer));
463
464         /* Arg4 is the trampoline address */
465         amd64_mov_reg_membase (code, AMD64_ARG_REG4, AMD64_RBP, tramp_offset, sizeof(gpointer));
466
467         if (aot) {
468                 char *icall_name = g_strdup_printf ("trampoline_func_%d", tramp_type);
469                 code = mono_arch_emit_load_aotconst (buf, code, &ji, MONO_PATCH_INFO_JIT_ICALL_ADDR, icall_name);
470         } else {
471                 tramp = (guint8*)mono_get_trampoline_func (tramp_type);
472                 amd64_mov_reg_imm (code, AMD64_R11, tramp);
473         }
474         amd64_call_reg (code, AMD64_R11);
475         amd64_mov_membase_reg (code, AMD64_RBP, res_offset, AMD64_RAX, sizeof(mgreg_t));
476
477         /* Restore LMF */
478         amd64_mov_reg_membase (code, AMD64_RCX, AMD64_RBP, lmf_offset + MONO_STRUCT_OFFSET (MonoLMF, previous_lmf), sizeof(gpointer));
479         amd64_alu_reg_imm_size (code, X86_SUB, AMD64_RCX, 0x5, sizeof(gpointer));
480         amd64_mov_reg_membase (code, AMD64_R11, AMD64_RBP, lmf_offset + MONO_STRUCT_OFFSET (MonoLMFTramp, lmf_addr), sizeof(gpointer));
481         amd64_mov_membase_reg (code, AMD64_R11, 0, AMD64_RCX, sizeof(gpointer));
482
483         /* 
484          * Save rax to the stack, after the leave instruction, this will become part of
485          * the red zone.
486          */
487         amd64_mov_reg_membase (code, AMD64_RAX, AMD64_RBP, res_offset, sizeof(mgreg_t));
488         amd64_mov_membase_reg (code, AMD64_RBP, rax_offset, AMD64_RAX, sizeof(mgreg_t));
489
490         /* Check for thread interruption */
491         /* This is not perf critical code so no need to check the interrupt flag */
492         /* 
493          * Have to call the _force_ variant, since there could be a protected wrapper on the top of the stack.
494          */
495         if (aot) {
496                 code = mono_arch_emit_load_aotconst (buf, code, &ji, MONO_PATCH_INFO_JIT_ICALL_ADDR, "mono_thread_force_interruption_checkpoint_noraise");
497         } else {
498                 amd64_mov_reg_imm (code, AMD64_R11, (guint8*)mono_thread_force_interruption_checkpoint_noraise);
499         }
500         amd64_call_reg (code, AMD64_R11);
501
502         amd64_test_reg_reg (code, AMD64_RAX, AMD64_RAX);
503         br_ex_check = code;
504         amd64_branch8 (code, X86_CC_Z, -1, 1);
505
506         /*
507          * Exception case:
508          * We have an exception we want to throw in the caller's frame, so pop
509          * the trampoline frame and throw from the caller.
510          */
511 #if TARGET_WIN32
512         amd64_lea_membase (code, AMD64_RSP, AMD64_RBP, 0);
513         amd64_pop_reg (code, AMD64_RBP);
514         mono_add_unwind_op_same_value (unwind_ops, code, buf, AMD64_RBP);
515 #else
516         amd64_leave (code);
517 #endif
518         /* We are in the parent frame, the exception is in rax */
519         /*
520          * EH is initialized after trampolines, so get the address of the variable
521          * which contains throw_exception, and load it from there.
522          */
523         if (aot) {
524                 /* Not really a jit icall */
525                 code = mono_arch_emit_load_aotconst (buf, code, &ji, MONO_PATCH_INFO_JIT_ICALL_ADDR, "throw_exception_addr");
526         } else {
527                 amd64_mov_reg_imm (code, AMD64_R11, (guint8*)mono_get_throw_exception_addr ());
528         }
529         amd64_mov_reg_membase (code, AMD64_R11, AMD64_R11, 0, sizeof(gpointer));
530         amd64_mov_reg_reg (code, AMD64_ARG_REG1, AMD64_RAX, sizeof(mgreg_t));
531         /*
532          * We still have the original return value on the top of the stack, so the
533          * throw trampoline will use that as the throw site.
534          */
535         amd64_jump_reg (code, AMD64_R11);
536
537         /* Normal case */
538         mono_amd64_patch (br_ex_check, code);
539
540         /* Restore argument registers, r10 (imt method/rgxtx)
541            and rax (needed for direct calls to C vararg functions). */
542         for (i = 0; i < AMD64_NREG; ++i)
543                 if (AMD64_IS_ARGUMENT_REG (i) || i == AMD64_R10 || i == AMD64_RAX)
544                         amd64_mov_reg_membase (code, i, AMD64_RBP, saved_regs_offset + (i * sizeof(mgreg_t)), sizeof(mgreg_t));
545         for (i = 0; i < 8; ++i)
546                 amd64_movsd_reg_membase (code, i, AMD64_RBP, saved_fpregs_offset + (i * sizeof(mgreg_t)));
547
548         /* Restore stack */
549 #if TARGET_WIN32
550         amd64_lea_membase (code, AMD64_RSP, AMD64_RBP, 0);
551         amd64_pop_reg (code, AMD64_RBP);
552         mono_add_unwind_op_same_value (unwind_ops, code, buf, AMD64_RBP);
553 #else
554         amd64_leave (code);
555 #endif
556         cfa_offset -= sizeof (mgreg_t);
557         mono_add_unwind_op_def_cfa (unwind_ops, code, buf, AMD64_RSP, cfa_offset);
558
559         if (MONO_TRAMPOLINE_TYPE_MUST_RETURN (tramp_type)) {
560                 /* Load result */
561                 amd64_mov_reg_membase (code, AMD64_RAX, AMD64_RSP, rax_offset - sizeof(mgreg_t), sizeof(mgreg_t));
562                 amd64_ret (code);
563         } else {
564                 /* call the compiled method using the saved rax */
565                 amd64_jump_membase (code, AMD64_RSP, rax_offset - sizeof(mgreg_t));
566         }
567
568         g_assert ((code - buf) <= kMaxCodeSize);
569         g_assert_checked (mono_arch_unwindinfo_validate_size (unwind_ops, MONO_MAX_TRAMPOLINE_UNWINDINFO_SIZE));
570
571         mono_arch_flush_icache (buf, code - buf);
572         MONO_PROFILER_RAISE (jit_code_buffer, (buf, code - buf, MONO_PROFILER_CODE_BUFFER_HELPER, NULL));
573
574         tramp_name = mono_get_generic_trampoline_name (tramp_type);
575         *info = mono_tramp_info_create (tramp_name, buf, code - buf, ji, unwind_ops);
576         g_free (tramp_name);
577
578         return buf;
579 }
580
581 gpointer
582 mono_arch_create_specific_trampoline (gpointer arg1, MonoTrampolineType tramp_type, MonoDomain *domain, guint32 *code_len)
583 {
584         guint8 *code, *buf, *tramp;
585         int size;
586         gboolean far_addr = FALSE;
587
588         tramp = mono_get_trampoline_code (tramp_type);
589
590         if ((((guint64)arg1) >> 32) == 0)
591                 size = 5 + 1 + 4;
592         else
593                 size = 5 + 1 + 8;
594
595         code = buf = (guint8 *)mono_domain_code_reserve_align (domain, size, 1);
596
597         if (((gint64)tramp - (gint64)code) >> 31 != 0 && ((gint64)tramp - (gint64)code) >> 31 != -1) {
598 #ifndef MONO_ARCH_NOMAP32BIT
599                 g_assert_not_reached ();
600 #endif
601                 far_addr = TRUE;
602                 size += 16;
603                 code = buf = (guint8 *)mono_domain_code_reserve_align (domain, size, 1);
604         }
605
606         if (far_addr) {
607                 amd64_mov_reg_imm (code, AMD64_R11, tramp);
608                 amd64_call_reg (code, AMD64_R11);
609         } else {
610                 amd64_call_code (code, tramp);
611         }
612         /* The trampoline code will obtain the argument from the instruction stream */
613         if ((((guint64)arg1) >> 32) == 0) {
614                 *code = 0x4;
615                 *(guint32*)(code + 1) = (gint64)arg1;
616                 code += 5;
617         } else {
618                 *code = 0x8;
619                 *(guint64*)(code + 1) = (gint64)arg1;
620                 code += 9;
621         }
622
623         g_assert ((code - buf) <= size);
624
625         if (code_len)
626                 *code_len = size;
627
628         mono_arch_flush_icache (buf, size);
629         MONO_PROFILER_RAISE (jit_code_buffer, (buf, code - buf, MONO_PROFILER_CODE_BUFFER_SPECIFIC_TRAMPOLINE, mono_get_generic_trampoline_simple_name (tramp_type)));
630
631         return buf;
632 }       
633
634 gpointer
635 mono_arch_create_rgctx_lazy_fetch_trampoline (guint32 slot, MonoTrampInfo **info, gboolean aot)
636 {
637         guint8 *tramp;
638         guint8 *code, *buf;
639         guint8 **rgctx_null_jumps;
640         int tramp_size;
641         int depth, index;
642         int i;
643         gboolean mrgctx;
644         MonoJumpInfo *ji = NULL;
645         GSList *unwind_ops;
646
647         mrgctx = MONO_RGCTX_SLOT_IS_MRGCTX (slot);
648         index = MONO_RGCTX_SLOT_INDEX (slot);
649         if (mrgctx)
650                 index += MONO_SIZEOF_METHOD_RUNTIME_GENERIC_CONTEXT / sizeof (gpointer);
651         for (depth = 0; ; ++depth) {
652                 int size = mono_class_rgctx_get_array_size (depth, mrgctx);
653
654                 if (index < size - 1)
655                         break;
656                 index -= size - 1;
657         }
658
659         tramp_size = 64 + 8 * depth;
660
661         code = buf = (guint8 *)mono_global_codeman_reserve (tramp_size + MONO_TRAMPOLINE_UNWINDINFO_SIZE(0));
662
663         unwind_ops = mono_arch_get_cie_program ();
664
665         rgctx_null_jumps = (guint8 **)g_malloc (sizeof (guint8*) * (depth + 2));
666
667         if (mrgctx) {
668                 /* get mrgctx ptr */
669                 amd64_mov_reg_reg (code, AMD64_RAX, AMD64_ARG_REG1, 8);
670         } else {
671                 /* load rgctx ptr from vtable */
672                 amd64_mov_reg_membase (code, AMD64_RAX, AMD64_ARG_REG1, MONO_STRUCT_OFFSET (MonoVTable, runtime_generic_context), sizeof(gpointer));
673                 /* is the rgctx ptr null? */
674                 amd64_test_reg_reg (code, AMD64_RAX, AMD64_RAX);
675                 /* if yes, jump to actual trampoline */
676                 rgctx_null_jumps [0] = code;
677                 amd64_branch8 (code, X86_CC_Z, -1, 1);
678         }
679
680         for (i = 0; i < depth; ++i) {
681                 /* load ptr to next array */
682                 if (mrgctx && i == 0)
683                         amd64_mov_reg_membase (code, AMD64_RAX, AMD64_RAX, MONO_SIZEOF_METHOD_RUNTIME_GENERIC_CONTEXT, sizeof(gpointer));
684                 else
685                         amd64_mov_reg_membase (code, AMD64_RAX, AMD64_RAX, 0, sizeof(gpointer));
686                 /* is the ptr null? */
687                 amd64_test_reg_reg (code, AMD64_RAX, AMD64_RAX);
688                 /* if yes, jump to actual trampoline */
689                 rgctx_null_jumps [i + 1] = code;
690                 amd64_branch8 (code, X86_CC_Z, -1, 1);
691         }
692
693         /* fetch slot */
694         amd64_mov_reg_membase (code, AMD64_RAX, AMD64_RAX, sizeof (gpointer) * (index + 1), sizeof(gpointer));
695         /* is the slot null? */
696         amd64_test_reg_reg (code, AMD64_RAX, AMD64_RAX);
697         /* if yes, jump to actual trampoline */
698         rgctx_null_jumps [depth + 1] = code;
699         amd64_branch8 (code, X86_CC_Z, -1, 1);
700         /* otherwise return */
701         amd64_ret (code);
702
703         for (i = mrgctx ? 1 : 0; i <= depth + 1; ++i)
704                 mono_amd64_patch (rgctx_null_jumps [i], code);
705
706         g_free (rgctx_null_jumps);
707
708         if (MONO_ARCH_VTABLE_REG != AMD64_ARG_REG1) {
709                 /* move the rgctx pointer to the VTABLE register */
710                 amd64_mov_reg_reg (code, MONO_ARCH_VTABLE_REG, AMD64_ARG_REG1, sizeof(gpointer));
711         }
712
713         if (aot) {
714                 code = mono_arch_emit_load_aotconst (buf, code, &ji, MONO_PATCH_INFO_JIT_ICALL_ADDR, g_strdup_printf ("specific_trampoline_lazy_fetch_%u", slot));
715                 amd64_jump_reg (code, AMD64_R11);
716         } else {
717                 tramp = (guint8 *)mono_arch_create_specific_trampoline (GUINT_TO_POINTER (slot), MONO_TRAMPOLINE_RGCTX_LAZY_FETCH, mono_get_root_domain (), NULL);
718
719                 /* jump to the actual trampoline */
720                 amd64_jump_code (code, tramp);
721         }
722
723         mono_arch_flush_icache (buf, code - buf);
724         MONO_PROFILER_RAISE (jit_code_buffer, (buf, code - buf, MONO_PROFILER_CODE_BUFFER_GENERICS_TRAMPOLINE, NULL));
725
726         g_assert (code - buf <= tramp_size);
727         g_assert_checked (mono_arch_unwindinfo_validate_size (unwind_ops, MONO_TRAMPOLINE_UNWINDINFO_SIZE(0)));
728
729         char *name = mono_get_rgctx_fetch_trampoline_name (slot);
730         *info = mono_tramp_info_create (name, buf, code - buf, ji, unwind_ops);
731         g_free (name);
732
733         return buf;
734 }
735
736 gpointer
737 mono_arch_create_general_rgctx_lazy_fetch_trampoline (MonoTrampInfo **info, gboolean aot)
738 {
739         guint8 *code, *buf;
740         int tramp_size;
741         MonoJumpInfo *ji = NULL;
742         GSList *unwind_ops;
743
744         g_assert (aot);
745         tramp_size = 64;
746
747         code = buf = (guint8 *)mono_global_codeman_reserve (tramp_size + MONO_TRAMPOLINE_UNWINDINFO_SIZE(0));
748
749         unwind_ops = mono_arch_get_cie_program ();
750
751         // FIXME: Currently, we always go to the slow path.
752         /* This receives a <slot, trampoline> in the rgctx arg reg. */
753         /* Load trampoline addr */
754         amd64_mov_reg_membase (code, AMD64_R11, MONO_ARCH_RGCTX_REG, 8, 8);
755         /* move the rgctx pointer to the VTABLE register */
756         amd64_mov_reg_reg (code, MONO_ARCH_VTABLE_REG, AMD64_ARG_REG1, sizeof(gpointer));
757         /* Jump to the trampoline */
758         amd64_jump_reg (code, AMD64_R11);
759
760         mono_arch_flush_icache (buf, code - buf);
761         MONO_PROFILER_RAISE (jit_code_buffer, (buf, code - buf, MONO_PROFILER_CODE_BUFFER_GENERICS_TRAMPOLINE, NULL));
762
763         g_assert (code - buf <= tramp_size);
764         g_assert_checked (mono_arch_unwindinfo_validate_size (unwind_ops, MONO_TRAMPOLINE_UNWINDINFO_SIZE(0)));
765
766         if (info)
767                 *info = mono_tramp_info_create ("rgctx_fetch_trampoline_general", buf, code - buf, ji, unwind_ops);
768
769         return buf;
770 }
771
772 void
773 mono_arch_invalidate_method (MonoJitInfo *ji, void *func, gpointer func_arg)
774 {
775         /* FIXME: This is not thread safe */
776         guint8 *code = (guint8 *)ji->code_start;
777
778         amd64_mov_reg_imm (code, AMD64_ARG_REG1, func_arg);
779         amd64_mov_reg_imm (code, AMD64_R11, func);
780
781         x86_push_imm (code, (guint64)func_arg);
782         amd64_call_reg (code, AMD64_R11);
783 }
784 #endif /* !DISABLE_JIT */
785
786 /*
787  * mono_arch_get_call_target:
788  *
789  *   Return the address called by the code before CODE if exists.
790  */
791 guint8*
792 mono_arch_get_call_target (guint8 *code)
793 {
794         if (code [-5] == 0xe8) {
795                 gint32 disp = *(gint32*)(code - 4);
796                 guint8 *target = code + disp;
797
798                 return target;
799         } else {
800                 return NULL;
801         }
802 }
803
804 /*
805  * mono_arch_get_plt_info_offset:
806  *
807  *   Return the PLT info offset belonging to the plt entry PLT_ENTRY.
808  */
809 guint32
810 mono_arch_get_plt_info_offset (guint8 *plt_entry, mgreg_t *regs, guint8 *code)
811 {
812         return *(guint32*)(plt_entry + 6);
813 }
814
815 #ifndef DISABLE_JIT
816 /*
817  * mono_arch_create_sdb_trampoline:
818  *
819  *   Return a trampoline which captures the current context, passes it to
820  * debugger_agent_single_step_from_context ()/debugger_agent_breakpoint_from_context (),
821  * then restores the (potentially changed) context.
822  */
823 guint8*
824 mono_arch_create_sdb_trampoline (gboolean single_step, MonoTrampInfo **info, gboolean aot)
825 {
826         int tramp_size = 512;
827         int i, framesize, ctx_offset, cfa_offset, gregs_offset;
828         guint8 *code, *buf;
829         GSList *unwind_ops = NULL;
830         MonoJumpInfo *ji = NULL;
831
832         code = buf = (guint8 *)mono_global_codeman_reserve (tramp_size + MONO_MAX_TRAMPOLINE_UNWINDINFO_SIZE);
833
834         framesize = 0;
835 #ifdef TARGET_WIN32
836         /* Reserve space where the callee can save the argument registers */
837         framesize += 4 * sizeof (mgreg_t);
838 #endif
839
840         ctx_offset = framesize;
841         framesize += sizeof (MonoContext);
842
843         framesize = ALIGN_TO (framesize, MONO_ARCH_FRAME_ALIGNMENT);
844
845         // CFA = sp + 8
846         cfa_offset = 8;
847         mono_add_unwind_op_def_cfa (unwind_ops, code, buf, AMD64_RSP, 8);
848         // IP saved at CFA - 8
849         mono_add_unwind_op_offset (unwind_ops, code, buf, AMD64_RIP, -cfa_offset);
850
851         amd64_push_reg (code, AMD64_RBP);
852         cfa_offset += sizeof(mgreg_t);
853         mono_add_unwind_op_def_cfa_offset (unwind_ops, code, buf, cfa_offset);
854         mono_add_unwind_op_offset (unwind_ops, code, buf, AMD64_RBP, - cfa_offset);
855
856         amd64_mov_reg_reg (code, AMD64_RBP, AMD64_RSP, sizeof(mgreg_t));
857         mono_add_unwind_op_def_cfa_reg (unwind_ops, code, buf, AMD64_RBP);
858         mono_add_unwind_op_fp_alloc (unwind_ops, code, buf, AMD64_RBP, 0);
859         amd64_alu_reg_imm (code, X86_SUB, AMD64_RSP, framesize);
860
861         gregs_offset = ctx_offset + MONO_STRUCT_OFFSET (MonoContext, gregs);
862
863         /* Initialize a MonoContext structure on the stack */
864         for (i = 0; i < AMD64_NREG; ++i) {
865                 if (i != AMD64_RIP && i != AMD64_RSP && i != AMD64_RBP)
866                         amd64_mov_membase_reg (code, AMD64_RSP, gregs_offset + (i * sizeof (mgreg_t)), i, sizeof (mgreg_t));
867         }
868         amd64_mov_reg_membase (code, AMD64_R11, AMD64_RBP, 0, sizeof (mgreg_t));
869         amd64_mov_membase_reg (code, AMD64_RSP, gregs_offset + (AMD64_RBP * sizeof (mgreg_t)), AMD64_R11, sizeof (mgreg_t));
870         amd64_lea_membase (code, AMD64_R11, AMD64_RBP, 2 * sizeof (mgreg_t));
871         amd64_mov_membase_reg (code, AMD64_RSP, gregs_offset + (AMD64_RSP * sizeof (mgreg_t)), AMD64_R11, sizeof (mgreg_t));
872         amd64_mov_reg_membase (code, AMD64_R11, AMD64_RBP, sizeof (mgreg_t), sizeof (mgreg_t));
873         amd64_mov_membase_reg (code, AMD64_RSP, gregs_offset + (AMD64_RIP * sizeof (mgreg_t)), AMD64_R11, sizeof (mgreg_t));
874
875         /* Call the single step/breakpoint function in sdb */
876         amd64_lea_membase (code, AMD64_ARG_REG1, AMD64_RSP, ctx_offset);
877
878         if (aot) {
879                 if (single_step)
880                         code = mono_arch_emit_load_aotconst (buf, code, &ji, MONO_PATCH_INFO_JIT_ICALL_ADDR, "debugger_agent_single_step_from_context");
881                 else
882                         code = mono_arch_emit_load_aotconst (buf, code, &ji, MONO_PATCH_INFO_JIT_ICALL_ADDR, "debugger_agent_breakpoint_from_context");
883         } else {
884                 if (single_step)
885                         amd64_mov_reg_imm (code, AMD64_R11, debugger_agent_single_step_from_context);
886                 else
887                         amd64_mov_reg_imm (code, AMD64_R11, debugger_agent_breakpoint_from_context);
888         }       
889         amd64_call_reg (code, AMD64_R11);
890
891         /* Restore registers from ctx */
892         for (i = 0; i < AMD64_NREG; ++i) {
893                 if (i != AMD64_RIP && i != AMD64_RSP && i != AMD64_RBP)
894                         amd64_mov_reg_membase (code, i, AMD64_RSP, gregs_offset + (i * sizeof (mgreg_t)), sizeof (mgreg_t));
895         }
896         amd64_mov_reg_membase (code, AMD64_R11, AMD64_RSP, gregs_offset + (AMD64_RBP * sizeof (mgreg_t)), sizeof (mgreg_t));
897         amd64_mov_membase_reg (code, AMD64_RBP, 0, AMD64_R11, sizeof (mgreg_t));
898         amd64_mov_reg_membase (code, AMD64_R11, AMD64_RSP, gregs_offset + (AMD64_RIP * sizeof (mgreg_t)), sizeof (mgreg_t));
899         amd64_mov_membase_reg (code, AMD64_RBP, sizeof (mgreg_t), AMD64_R11, sizeof (mgreg_t));
900
901 #if TARGET_WIN32
902         amd64_lea_membase (code, AMD64_RSP, AMD64_RBP, 0);
903         amd64_pop_reg (code, AMD64_RBP);
904         mono_add_unwind_op_same_value (unwind_ops, code, buf, AMD64_RBP);
905 #else
906         amd64_leave (code);
907 #endif
908         cfa_offset -= sizeof (mgreg_t);
909         mono_add_unwind_op_def_cfa (unwind_ops, code, buf, AMD64_RSP, cfa_offset);
910         amd64_ret (code);
911
912         mono_arch_flush_icache (code, code - buf);
913         MONO_PROFILER_RAISE (jit_code_buffer, (buf, code - buf, MONO_PROFILER_CODE_BUFFER_HELPER, NULL));
914         g_assert (code - buf <= tramp_size);
915         g_assert_checked (mono_arch_unwindinfo_validate_size (unwind_ops, MONO_MAX_TRAMPOLINE_UNWINDINFO_SIZE));
916
917         const char *tramp_name = single_step ? "sdb_single_step_trampoline" : "sdb_breakpoint_trampoline";
918         *info = mono_tramp_info_create (tramp_name, buf, code - buf, ji, unwind_ops);
919
920         return buf;
921 }
922
923 /*
924  * mono_arch_get_enter_icall_trampoline:
925  *
926  *   A trampoline that handles the transition from interpreter into native
927  *   world. It requiers to set up a descriptor (InterpMethodArguments), so the
928  *   trampoline can translate the arguments into the native calling convention.
929  *
930  *   See also `build_args_from_sig ()` in interp.c.
931  */
932 gpointer
933 mono_arch_get_enter_icall_trampoline (MonoTrampInfo **info)
934 {
935 #ifdef ENABLE_INTERPRETER
936         const int gregs_num = INTERP_ICALL_TRAMP_IARGS;
937         const int fregs_num = INTERP_ICALL_TRAMP_FARGS;
938         guint8 *start = NULL, *code, *label_gexits [gregs_num], *label_fexits [fregs_num], *label_leave_tramp [3], *label_is_float_ret;
939         MonoJumpInfo *ji = NULL;
940         GSList *unwind_ops = NULL;
941         static int farg_regs[] = {AMD64_XMM0, AMD64_XMM1, AMD64_XMM2};
942         int buf_len, i, framesize = 0, off_rbp, off_methodargs, off_targetaddr;
943
944         buf_len = 512 + MONO_TRAMPOLINE_UNWINDINFO_SIZE(0);
945         start = code = (guint8 *) mono_global_codeman_reserve (buf_len);
946
947         off_rbp = -framesize;
948
949         framesize += sizeof (mgreg_t);
950         off_methodargs = -framesize;
951
952         framesize += sizeof (mgreg_t);
953         off_targetaddr = -framesize;
954
955         framesize += (gregs_num - PARAM_REGS) * sizeof (mgreg_t);
956
957         amd64_push_reg (code, AMD64_RBP);
958         amd64_mov_reg_reg (code, AMD64_RBP, AMD64_RSP, sizeof (mgreg_t));
959         amd64_alu_reg_imm (code, X86_SUB, AMD64_RSP, ALIGN_TO (framesize, MONO_ARCH_FRAME_ALIGNMENT));
960
961         /* save InterpMethodArguments* onto stack */
962         amd64_mov_membase_reg (code, AMD64_RBP, off_methodargs, AMD64_ARG_REG2, sizeof (mgreg_t));
963
964         /* save target address on stack */
965         amd64_mov_membase_reg (code, AMD64_RBP, off_targetaddr, AMD64_ARG_REG1, sizeof (mgreg_t));
966
967         /* load pointer to InterpMethodArguments* into R11 */
968         amd64_mov_reg_reg (code, AMD64_R11, AMD64_ARG_REG2, 8);
969         
970         /* move flen into RAX */
971         amd64_mov_reg_membase (code, AMD64_RAX, AMD64_R11, MONO_STRUCT_OFFSET (InterpMethodArguments, flen), sizeof (mgreg_t));
972         /* load pointer to fargs into R11 */
973         amd64_mov_reg_membase (code, AMD64_R11, AMD64_R11, MONO_STRUCT_OFFSET (InterpMethodArguments, fargs), sizeof (mgreg_t));
974
975         for (i = 0; i < fregs_num; ++i) {
976                 amd64_test_reg_reg (code, AMD64_RAX, AMD64_RAX);
977                 label_fexits [i] = code;
978                 x86_branch8 (code, X86_CC_Z, 0, FALSE);
979
980                 amd64_sse_movsd_reg_membase (code, farg_regs [i], AMD64_R11, i * sizeof (double));
981                 amd64_dec_reg_size (code, AMD64_RAX, 1);
982         }
983
984         for (i = 0; i < fregs_num; i++)
985                 x86_patch (label_fexits [i], code);
986
987         /* load pointer to InterpMethodArguments* into R11 */
988         amd64_mov_reg_reg (code, AMD64_R11, AMD64_ARG_REG2, sizeof (mgreg_t));
989         /* move ilen into RAX */
990         amd64_mov_reg_membase (code, AMD64_RAX, AMD64_R11, MONO_STRUCT_OFFSET (InterpMethodArguments, ilen), sizeof (mgreg_t));
991
992         int stack_offset = 0;
993         for (i = 0; i < gregs_num; i++) {
994                 amd64_test_reg_reg (code, AMD64_RAX, AMD64_RAX);
995                 label_gexits [i] = code;
996                 x86_branch32 (code, X86_CC_Z, 0, FALSE);
997
998                 /* load pointer to InterpMethodArguments* into R11 */
999                 amd64_mov_reg_membase (code, AMD64_R11, AMD64_RBP, off_methodargs, sizeof (mgreg_t));
1000                 /* load pointer to iargs into R11 */
1001                 amd64_mov_reg_membase (code, AMD64_R11, AMD64_R11, MONO_STRUCT_OFFSET (InterpMethodArguments, iargs), sizeof (mgreg_t));
1002
1003                 if (i < PARAM_REGS) {
1004                         amd64_mov_reg_membase (code, param_regs [i], AMD64_R11, i * sizeof (mgreg_t), sizeof (mgreg_t));
1005                 } else {
1006                         amd64_mov_reg_membase (code, AMD64_R11, AMD64_R11, i * sizeof (mgreg_t), sizeof (mgreg_t));
1007                         amd64_mov_membase_reg (code, AMD64_RSP, stack_offset, AMD64_R11, sizeof (mgreg_t));
1008                         stack_offset += sizeof (mgreg_t);
1009                 }
1010                 amd64_dec_reg_size (code, AMD64_RAX, 1);
1011         }
1012
1013         for (i = 0; i < gregs_num; i++)
1014                 x86_patch (label_gexits [i], code);
1015
1016         /* load target addr */
1017         amd64_mov_reg_membase (code, AMD64_R11, AMD64_RBP, off_targetaddr, sizeof (mgreg_t));
1018
1019         /* call into native function */
1020         amd64_call_reg (code, AMD64_R11);
1021
1022         /* load InterpMethodArguments */
1023         amd64_mov_reg_membase (code, AMD64_R11, AMD64_RBP, off_methodargs, sizeof (mgreg_t));
1024
1025         /* load is_float_ret */
1026         amd64_mov_reg_membase (code, AMD64_R11, AMD64_R11, MONO_STRUCT_OFFSET (InterpMethodArguments, is_float_ret), sizeof (mgreg_t));
1027
1028         /* check if a float return value is expected */
1029         amd64_test_reg_reg (code, AMD64_R11, AMD64_R11);
1030
1031         label_is_float_ret = code;
1032         x86_branch8 (code, X86_CC_NZ, 0, FALSE);
1033
1034         /* greg return */
1035         /* load InterpMethodArguments */
1036         amd64_mov_reg_membase (code, AMD64_R11, AMD64_RBP, off_methodargs, sizeof (mgreg_t));
1037         /* load retval */
1038         amd64_mov_reg_membase (code, AMD64_R11, AMD64_R11, MONO_STRUCT_OFFSET (InterpMethodArguments, retval), sizeof (mgreg_t));
1039
1040         amd64_test_reg_reg (code, AMD64_R11, AMD64_R11);
1041         label_leave_tramp [0] = code;
1042         x86_branch8 (code, X86_CC_Z, 0, FALSE);
1043
1044         amd64_mov_membase_reg (code, AMD64_R11, 0, AMD64_RAX, sizeof (mgreg_t));
1045
1046         label_leave_tramp [1] = code;
1047         x86_jump8 (code, 0);
1048
1049         /* freg return */
1050         x86_patch (label_is_float_ret, code);
1051         /* load InterpMethodArguments */
1052         amd64_mov_reg_membase (code, AMD64_R11, AMD64_RBP, off_methodargs, sizeof (mgreg_t));
1053         /* load retval */
1054         amd64_mov_reg_membase (code, AMD64_R11, AMD64_R11, MONO_STRUCT_OFFSET (InterpMethodArguments, retval), sizeof (mgreg_t));
1055
1056         amd64_test_reg_reg (code, AMD64_R11, AMD64_R11);
1057         label_leave_tramp [2] = code;
1058         x86_branch8 (code, X86_CC_Z, 0, FALSE);
1059
1060         amd64_sse_movsd_membase_reg (code, AMD64_R11, 0, AMD64_XMM0);
1061
1062         for (i = 0; i < 3; i++)
1063                 x86_patch (label_leave_tramp [i], code);
1064
1065         amd64_alu_reg_imm (code, X86_ADD, AMD64_RSP, ALIGN_TO (framesize, MONO_ARCH_FRAME_ALIGNMENT));
1066         amd64_pop_reg (code, AMD64_RBP);
1067         amd64_ret (code);
1068
1069         g_assert (code - start < buf_len);
1070
1071         mono_arch_flush_icache (start, code - start);
1072         MONO_PROFILER_RAISE (jit_code_buffer, (start, code - start, MONO_PROFILER_CODE_BUFFER_EXCEPTION_HANDLING, NULL));
1073
1074         if (info)
1075                 *info = mono_tramp_info_create ("enter_icall_trampoline", start, code - start, ji, unwind_ops);
1076
1077         return start;
1078 #else
1079         g_assert_not_reached ();
1080         return NULL;
1081 #endif /* ENABLE_INTERPRETER */
1082 }
1083 #endif /* !DISABLE_JIT */
1084
1085 #ifdef DISABLE_JIT
1086 gpointer
1087 mono_arch_get_unbox_trampoline (MonoMethod *m, gpointer addr)
1088 {
1089         g_assert_not_reached ();
1090         return NULL;
1091 }
1092
1093 gpointer
1094 mono_arch_get_static_rgctx_trampoline (gpointer arg, gpointer addr)
1095 {
1096         g_assert_not_reached ();
1097         return NULL;
1098 }
1099
1100 gpointer
1101 mono_arch_create_rgctx_lazy_fetch_trampoline (guint32 slot, MonoTrampInfo **info, gboolean aot)
1102 {
1103         g_assert_not_reached ();
1104         return NULL;
1105 }
1106
1107 guchar*
1108 mono_arch_create_generic_trampoline (MonoTrampolineType tramp_type, MonoTrampInfo **info, gboolean aot)
1109 {
1110         g_assert_not_reached ();
1111         return NULL;
1112 }
1113
1114 gpointer
1115 mono_arch_create_specific_trampoline (gpointer arg1, MonoTrampolineType tramp_type, MonoDomain *domain, guint32 *code_len)
1116 {
1117         g_assert_not_reached ();
1118         return NULL;
1119 }
1120
1121 gpointer
1122 mono_arch_create_general_rgctx_lazy_fetch_trampoline (MonoTrampInfo **info, gboolean aot)
1123 {
1124         g_assert_not_reached ();
1125         return NULL;
1126 }
1127
1128 void
1129 mono_arch_invalidate_method (MonoJitInfo *ji, void *func, gpointer func_arg)
1130 {
1131         g_assert_not_reached ();
1132         return;
1133 }
1134
1135 guint8*
1136 mono_arch_create_sdb_trampoline (gboolean single_step, MonoTrampInfo **info, gboolean aot)
1137 {
1138         g_assert_not_reached ();
1139         return NULL;
1140 }
1141
1142 gpointer
1143 mono_arch_get_enter_icall_trampoline (MonoTrampInfo **info)
1144 {
1145         g_assert_not_reached ();
1146         return NULL;
1147 }
1148 #endif /* DISABLE_JIT */