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