Merge pull request #4444 from lateralusX/jlorenss/windows-unwind-info
[mono.git] / mono / mini / tramp-amd64.c
1 /*
2  * tramp-amd64.c: JIT trampoline code for amd64
3  *
4  * Authors:
5  *   Dietmar Maurer (dietmar@ximian.com)
6  *   Zoltan Varga (vargaz@gmail.com)
7  *   Johan Lorensson (lateralusx.github@gmail.com)
8  *
9  * (C) 2001 Ximian, Inc.
10  * Copyright 2003-2011 Novell, Inc (http://www.novell.com)
11  * Copyright 2011 Xamarin, Inc (http://www.xamarin.com)
12  * Licensed under the MIT license. See LICENSE file in the project root for full license information.
13  */
14
15 #include <config.h>
16 #include <glib.h>
17
18 #include <mono/metadata/abi-details.h>
19 #include <mono/metadata/appdomain.h>
20 #include <mono/metadata/marshal.h>
21 #include <mono/metadata/tabledefs.h>
22 #include <mono/metadata/profiler-private.h>
23 #include <mono/metadata/gc-internals.h>
24 #include <mono/arch/amd64/amd64-codegen.h>
25
26 #include <mono/utils/memcheck.h>
27
28 #include "mini.h"
29 #include "mini-amd64.h"
30 #include "debugger-agent.h"
31
32 #define ALIGN_TO(val,align) ((((guint64)val) + ((align) - 1)) & ~((align) - 1))
33
34 #define IS_REX(inst) (((inst) >= 0x40) && ((inst) <= 0x4f))
35
36 #ifndef DISABLE_JIT
37 /*
38  * mono_arch_get_unbox_trampoline:
39  * @m: method pointer
40  * @addr: pointer to native code for @m
41  *
42  * when value type methods are called through the vtable we need to unbox the
43  * this argument. This method returns a pointer to a trampoline which does
44  * unboxing before calling the method
45  */
46 gpointer
47 mono_arch_get_unbox_trampoline (MonoMethod *m, gpointer addr)
48 {
49         guint8 *code, *start;
50         GSList *unwind_ops;
51         int this_reg, size = 20;
52
53         MonoDomain *domain = mono_domain_get ();
54
55         this_reg = mono_arch_get_this_arg_reg (NULL);
56
57         start = code = (guint8 *)mono_domain_code_reserve (domain, size + MONO_TRAMPOLINE_UNWINDINFO_SIZE(0));
58
59         unwind_ops = mono_arch_get_cie_program ();
60
61         amd64_alu_reg_imm (code, X86_ADD, this_reg, sizeof (MonoObject));
62         /* FIXME: Optimize this */
63         amd64_mov_reg_imm (code, AMD64_RAX, addr);
64         amd64_jump_reg (code, AMD64_RAX);
65         g_assert ((code - start) < size);
66         g_assert_checked (mono_arch_unwindinfo_validate_size (unwind_ops, MONO_TRAMPOLINE_UNWINDINFO_SIZE(0)));
67
68         mono_arch_flush_icache (start, code - start);
69         mono_profiler_code_buffer_new (start, code - start, MONO_PROFILER_CODE_BUFFER_UNBOX_TRAMPOLINE, m);
70
71         mono_tramp_info_register (mono_tramp_info_create (NULL, start, code - start, NULL, unwind_ops), domain);
72
73         return start;
74 }
75
76 /*
77  * mono_arch_get_static_rgctx_trampoline:
78  *
79  *   Create a trampoline which sets RGCTX_REG to MRGCTX, then jumps to ADDR.
80  */
81 gpointer
82 mono_arch_get_static_rgctx_trampoline (MonoMethod *m, MonoMethodRuntimeGenericContext *mrgctx, gpointer addr)
83 {
84         guint8 *code, *start;
85         GSList *unwind_ops;
86         int buf_len;
87
88         MonoDomain *domain = mono_domain_get ();
89
90 #ifdef MONO_ARCH_NOMAP32BIT
91         buf_len = 32;
92 #else
93         /* AOTed code could still have a non-32 bit address */
94         if ((((guint64)addr) >> 32) == 0)
95                 buf_len = 16;
96         else
97                 buf_len = 30;
98 #endif
99
100         start = code = (guint8 *)mono_domain_code_reserve (domain, buf_len + MONO_TRAMPOLINE_UNWINDINFO_SIZE(0));
101
102         unwind_ops = mono_arch_get_cie_program ();
103
104         amd64_mov_reg_imm (code, MONO_ARCH_RGCTX_REG, mrgctx);
105         amd64_jump_code (code, addr);
106         g_assert ((code - start) < buf_len);
107         g_assert_checked (mono_arch_unwindinfo_validate_size (unwind_ops, MONO_TRAMPOLINE_UNWINDINFO_SIZE(0)));
108
109         mono_arch_flush_icache (start, code - start);
110         mono_profiler_code_buffer_new (start, code - start, MONO_PROFILER_CODE_BUFFER_GENERICS_TRAMPOLINE, NULL);
111
112         mono_tramp_info_register (mono_tramp_info_create (NULL, start, code - start, NULL, unwind_ops), domain);
113
114         return start;
115 }
116 #endif /* !DISABLE_JIT */
117
118 #ifdef _WIN64
119 // Workaround lack of Valgrind support for 64-bit Windows
120 #define VALGRIND_DISCARD_TRANSLATIONS(...)
121 #endif
122
123 /*
124  * mono_arch_patch_callsite:
125  *
126  *   Patch the callsite whose address is given by ORIG_CODE so it calls ADDR. ORIG_CODE
127  * points to the pc right after the call.
128  */
129 void
130 mono_arch_patch_callsite (guint8 *method_start, guint8 *orig_code, guint8 *addr)
131 {
132         guint8 *code;
133         guint8 buf [16];
134         gboolean can_write = mono_breakpoint_clean_code (method_start, orig_code, 14, buf, sizeof (buf));
135
136         code = buf + 14;
137
138         /* mov 64-bit imm into r11 (followed by call reg?)  or direct call*/
139         if (((code [-13] == 0x49) && (code [-12] == 0xbb)) || (code [-5] == 0xe8)) {
140                 if (code [-5] != 0xe8) {
141                         if (can_write) {
142                                 InterlockedExchangePointer ((gpointer*)(orig_code - 11), addr);
143                                 VALGRIND_DISCARD_TRANSLATIONS (orig_code - 11, sizeof (gpointer));
144                         }
145                 } else {
146                         gboolean disp_32bit = ((((gint64)addr - (gint64)orig_code)) < (1 << 30)) && ((((gint64)addr - (gint64)orig_code)) > -(1 << 30));
147
148                         if ((((guint64)(addr)) >> 32) != 0 && !disp_32bit) {
149                                 /* 
150                                  * This might happen with LLVM or when calling AOTed code. Create a thunk.
151                                  */
152                                 guint8 *thunk_start, *thunk_code;
153
154                                 thunk_start = thunk_code = (guint8 *)mono_domain_code_reserve (mono_domain_get (), 32);
155                                 amd64_jump_membase (thunk_code, AMD64_RIP, 0);
156                                 *(guint64*)thunk_code = (guint64)addr;
157                                 addr = thunk_start;
158                                 g_assert ((((guint64)(addr)) >> 32) == 0);
159                                 mono_arch_flush_icache (thunk_start, thunk_code - thunk_start);
160                                 mono_profiler_code_buffer_new (thunk_start, thunk_code - thunk_start, MONO_PROFILER_CODE_BUFFER_HELPER, NULL);
161                         }
162                         if (can_write) {
163                                 InterlockedExchange ((gint32*)(orig_code - 4), ((gint64)addr - (gint64)orig_code));
164                                 VALGRIND_DISCARD_TRANSLATIONS (orig_code - 5, 4);
165                         }
166                 }
167         }
168         else if ((code [-7] == 0x41) && (code [-6] == 0xff) && (code [-5] == 0x15)) {
169                 /* call *<OFFSET>(%rip) */
170                 gpointer *got_entry = (gpointer*)((guint8*)orig_code + (*(guint32*)(orig_code - 4)));
171                 if (can_write) {
172                         InterlockedExchangePointer (got_entry, addr);
173                         VALGRIND_DISCARD_TRANSLATIONS (orig_code - 5, sizeof (gpointer));
174                 }
175         }
176 }
177
178 #ifndef DISABLE_JIT
179 guint8*
180 mono_arch_create_llvm_native_thunk (MonoDomain *domain, guint8 *addr)
181 {
182         /*
183          * The caller is LLVM code and the call displacement might exceed 32 bits. We can't determine the caller address, so
184          * we add a thunk every time.
185          * Since the caller is also allocated using the domain code manager, hopefully the displacement will fit into 32 bits.
186          * FIXME: Avoid this if possible if !MONO_ARCH_NOMAP32BIT and ADDR is 32 bits.
187          */
188         guint8 *thunk_start, *thunk_code;
189
190         thunk_start = thunk_code = (guint8 *)mono_domain_code_reserve (mono_domain_get (), 32);
191         amd64_jump_membase (thunk_code, AMD64_RIP, 0);
192         *(guint64*)thunk_code = (guint64)addr;
193         addr = thunk_start;
194         mono_arch_flush_icache (thunk_start, thunk_code - thunk_start);
195         mono_profiler_code_buffer_new (thunk_start, thunk_code - thunk_start, MONO_PROFILER_CODE_BUFFER_HELPER, NULL);
196         return addr;
197 }
198 #endif /* !DISABLE_JIT */
199
200 void
201 mono_arch_patch_plt_entry (guint8 *code, gpointer *got, mgreg_t *regs, guint8 *addr)
202 {
203         gint32 disp;
204         gpointer *plt_jump_table_entry;
205
206         /* A PLT entry: jmp *<DISP>(%rip) */
207         g_assert (code [0] == 0xff);
208         g_assert (code [1] == 0x25);
209
210         disp = *(gint32*)(code + 2);
211
212         plt_jump_table_entry = (gpointer*)(code + 6 + disp);
213
214         InterlockedExchangePointer (plt_jump_table_entry, addr);
215 }
216
217 #ifndef DISABLE_JIT
218 static void
219 stack_unaligned (MonoTrampolineType tramp_type)
220 {
221         printf ("%d\n", tramp_type);
222         g_assert_not_reached ();
223 }
224
225 guchar*
226 mono_arch_create_generic_trampoline (MonoTrampolineType tramp_type, MonoTrampInfo **info, gboolean aot)
227 {
228         char *tramp_name;
229         guint8 *buf, *code, *tramp, *br [2], *r11_save_code, *after_r11_save_code, *br_ex_check;
230         int i, lmf_offset, offset, res_offset, arg_offset, rax_offset, ex_offset, tramp_offset, ctx_offset, saved_regs_offset;
231         int r11_save_offset, saved_fpregs_offset, rbp_offset, framesize, orig_rsp_to_rbp_offset, cfa_offset;
232         gboolean has_caller;
233         GSList *unwind_ops = NULL;
234         MonoJumpInfo *ji = NULL;
235         const guint kMaxCodeSize = 630;
236
237         if (tramp_type == MONO_TRAMPOLINE_JUMP || tramp_type == MONO_TRAMPOLINE_HANDLER_BLOCK_GUARD)
238                 has_caller = FALSE;
239         else
240                 has_caller = TRUE;
241
242         code = buf = (guint8 *)mono_global_codeman_reserve (kMaxCodeSize + MONO_MAX_TRAMPOLINE_UNWINDINFO_SIZE);
243
244         /* Compute stack frame size and offsets */
245         offset = 0;
246         rbp_offset = -offset;
247
248         offset += sizeof(mgreg_t);
249         rax_offset = -offset;
250
251         offset += sizeof(mgreg_t);
252         ex_offset = -offset;
253
254         offset += sizeof(mgreg_t);
255         r11_save_offset = -offset;
256
257         offset += sizeof(mgreg_t);
258         tramp_offset = -offset;
259
260         offset += sizeof(gpointer);
261         arg_offset = -offset;
262
263         offset += sizeof(mgreg_t);
264         res_offset = -offset;
265
266         offset += sizeof (MonoContext);
267         ctx_offset = -offset;
268         saved_regs_offset = ctx_offset + MONO_STRUCT_OFFSET (MonoContext, gregs);
269         saved_fpregs_offset = ctx_offset + MONO_STRUCT_OFFSET (MonoContext, fregs);
270
271         offset += sizeof (MonoLMFTramp);
272         lmf_offset = -offset;
273
274 #ifdef TARGET_WIN32
275         /* Reserve space where the callee can save the argument registers */
276         offset += 4 * sizeof (mgreg_t);
277 #endif
278
279         framesize = ALIGN_TO (offset, MONO_ARCH_FRAME_ALIGNMENT);
280
281         // CFA = sp + 16 (the trampoline address is on the stack)
282         cfa_offset = 16;
283         mono_add_unwind_op_def_cfa (unwind_ops, code, buf, AMD64_RSP, 16);
284         // IP saved at CFA - 8
285         mono_add_unwind_op_offset (unwind_ops, code, buf, AMD64_RIP, -8);
286
287         orig_rsp_to_rbp_offset = 0;
288         r11_save_code = code;
289         /* Reserve space for the mov_membase_reg to save R11 */
290         code += 5;
291         after_r11_save_code = code;
292
293         /* Pop the return address off the stack */
294         amd64_pop_reg (code, AMD64_R11);
295         orig_rsp_to_rbp_offset += sizeof(mgreg_t);
296
297         cfa_offset -= sizeof(mgreg_t);
298         mono_add_unwind_op_def_cfa_offset (unwind_ops, code, buf, cfa_offset);
299
300         /* 
301          * Allocate a new stack frame
302          */
303         amd64_push_reg (code, AMD64_RBP);
304         cfa_offset += sizeof(mgreg_t);
305         mono_add_unwind_op_def_cfa_offset (unwind_ops, code, buf, cfa_offset);
306         mono_add_unwind_op_offset (unwind_ops, code, buf, AMD64_RBP, - cfa_offset);
307
308         orig_rsp_to_rbp_offset -= sizeof(mgreg_t);
309         amd64_mov_reg_reg (code, AMD64_RBP, AMD64_RSP, sizeof(mgreg_t));
310         mono_add_unwind_op_def_cfa_reg (unwind_ops, code, buf, AMD64_RBP);
311         mono_add_unwind_op_fp_alloc (unwind_ops, code, buf, AMD64_RBP, 0);
312         amd64_alu_reg_imm (code, X86_SUB, AMD64_RSP, framesize);
313
314         /* Compute the trampoline address from the return address */
315         if (aot) {
316                 /* 7 = length of call *<offset>(rip) */
317                 amd64_alu_reg_imm (code, X86_SUB, AMD64_R11, 7);
318         } else {
319                 /* 5 = length of amd64_call_membase () */
320                 amd64_alu_reg_imm (code, X86_SUB, AMD64_R11, 5);
321         }
322         amd64_mov_membase_reg (code, AMD64_RBP, tramp_offset, AMD64_R11, sizeof(gpointer));
323
324         /* Save all registers */
325         for (i = 0; i < AMD64_NREG; ++i) {
326                 if (i == AMD64_RBP) {
327                         /* RAX is already saved */
328                         amd64_mov_reg_membase (code, AMD64_RAX, AMD64_RBP, rbp_offset, sizeof(mgreg_t));
329                         amd64_mov_membase_reg (code, AMD64_RBP, saved_regs_offset + (i * sizeof(mgreg_t)), AMD64_RAX, sizeof(mgreg_t));
330                 } else if (i == AMD64_RIP) {
331                         if (has_caller)
332                                 amd64_mov_reg_membase (code, AMD64_R11, AMD64_RBP, 8, sizeof(gpointer));
333                         else
334                                 amd64_mov_reg_imm (code, AMD64_R11, 0);
335                         amd64_mov_membase_reg (code, AMD64_RBP, saved_regs_offset + (i * sizeof(mgreg_t)), AMD64_R11, sizeof(mgreg_t));
336                 } else if (i == AMD64_RSP) {
337                         amd64_mov_reg_reg (code, AMD64_R11, AMD64_RSP, sizeof(mgreg_t));
338                         amd64_alu_reg_imm (code, X86_ADD, AMD64_R11, framesize + 16);
339                         amd64_mov_membase_reg (code, AMD64_RBP, saved_regs_offset + (i * sizeof(mgreg_t)), AMD64_R11, sizeof(mgreg_t));
340                 } else if (i != AMD64_R11) {
341                         amd64_mov_membase_reg (code, AMD64_RBP, saved_regs_offset + (i * sizeof(mgreg_t)), i, sizeof(mgreg_t));
342                 } else {
343                         /* We have to save R11 right at the start of
344                            the trampoline code because it's used as a
345                            scratch register */
346                         /* This happens before the frame is set up, so it goes into the redzone */
347                         amd64_mov_membase_reg (r11_save_code, AMD64_RSP, r11_save_offset + orig_rsp_to_rbp_offset, i, sizeof(mgreg_t));
348                         g_assert (r11_save_code == after_r11_save_code);
349
350                         /* Copy from the save slot into the register array slot */
351                         amd64_mov_reg_membase (code, i, AMD64_RSP, r11_save_offset + orig_rsp_to_rbp_offset, sizeof(mgreg_t));
352                         amd64_mov_membase_reg (code, AMD64_RBP, saved_regs_offset + (i * sizeof(mgreg_t)), i, sizeof(mgreg_t));
353                 }
354                 /* cfa = rbp + cfa_offset */
355                 mono_add_unwind_op_offset (unwind_ops, code, buf, i, - cfa_offset + saved_regs_offset + (i * sizeof (mgreg_t)));
356         }
357         for (i = 0; i < 8; ++i)
358                 amd64_movsd_membase_reg (code, AMD64_RBP, saved_fpregs_offset + (i * sizeof(mgreg_t)), i);
359
360         /* Check that the stack is aligned */
361         amd64_mov_reg_reg (code, AMD64_R11, AMD64_RSP, sizeof (mgreg_t));
362         amd64_alu_reg_imm (code, X86_AND, AMD64_R11, 15);
363         amd64_alu_reg_imm (code, X86_CMP, AMD64_R11, 0);
364         br [0] = code;
365         amd64_branch_disp (code, X86_CC_Z, 0, FALSE);
366         if (aot) {
367                 amd64_mov_reg_imm (code, AMD64_R11, 0);
368                 amd64_mov_reg_membase (code, AMD64_R11, AMD64_R11, 0, 8);
369         } else {
370                 amd64_mov_reg_imm (code, MONO_AMD64_ARG_REG1, tramp_type);
371                 amd64_mov_reg_imm (code, AMD64_R11, stack_unaligned);
372                 amd64_call_reg (code, AMD64_R11);
373         }
374         mono_amd64_patch (br [0], code);
375         //amd64_breakpoint (code);
376
377         if (tramp_type != MONO_TRAMPOLINE_HANDLER_BLOCK_GUARD) {
378                 /* Obtain the trampoline argument which is encoded in the instruction stream */
379                 if (aot) {
380                         /* Load the GOT offset */
381                         amd64_mov_reg_membase (code, AMD64_R11, AMD64_RBP, tramp_offset, sizeof(gpointer));
382                         /*
383                          * r11 points to a call *<offset>(%rip) instruction, load the
384                          * pc-relative offset from the instruction itself.
385                          */
386                         amd64_mov_reg_membase (code, AMD64_RAX, AMD64_R11, 3, 4);
387                         /* 7 is the length of the call, 8 is the offset to the next got slot */
388                         amd64_alu_reg_imm_size (code, X86_ADD, AMD64_RAX, 7 + sizeof (gpointer), sizeof(gpointer));
389                         /* Compute the address of the GOT slot */
390                         amd64_alu_reg_reg_size (code, X86_ADD, AMD64_R11, AMD64_RAX, sizeof(gpointer));
391                         /* Load the value */
392                         amd64_mov_reg_membase (code, AMD64_R11, AMD64_R11, 0, sizeof(gpointer));
393                 } else {                        
394                         amd64_mov_reg_membase (code, AMD64_R11, AMD64_RBP, tramp_offset, sizeof(gpointer));
395                         amd64_mov_reg_membase (code, AMD64_RAX, AMD64_R11, 5, 1);
396                         amd64_widen_reg (code, AMD64_RAX, AMD64_RAX, TRUE, FALSE);
397                         amd64_alu_reg_imm_size (code, X86_CMP, AMD64_RAX, 4, 1);
398                         br [0] = code;
399                         x86_branch8 (code, X86_CC_NE, 6, FALSE);
400                         /* 32 bit immediate */
401                         amd64_mov_reg_membase (code, AMD64_R11, AMD64_R11, 6, 4);
402                         br [1] = code;
403                         x86_jump8 (code, 10);
404                         /* 64 bit immediate */
405                         mono_amd64_patch (br [0], code);
406                         amd64_mov_reg_membase (code, AMD64_R11, AMD64_R11, 6, 8);
407                         mono_amd64_patch (br [1], code);
408                 }
409                 amd64_mov_membase_reg (code, AMD64_RBP, arg_offset, AMD64_R11, sizeof(gpointer));
410         } else {
411                 amd64_mov_reg_membase (code, AMD64_R11, AMD64_RBP, saved_regs_offset + (MONO_AMD64_ARG_REG1 * sizeof(mgreg_t)), sizeof(mgreg_t));
412                 amd64_mov_membase_reg (code, AMD64_RBP, arg_offset, AMD64_R11, sizeof(gpointer));
413         }
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_code_buffer_new (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_code_buffer_new (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         /* move the rgctx pointer to the VTABLE register */
709         amd64_mov_reg_reg (code, MONO_ARCH_VTABLE_REG, AMD64_ARG_REG1, sizeof(gpointer));
710
711         if (aot) {
712                 code = mono_arch_emit_load_aotconst (buf, code, &ji, MONO_PATCH_INFO_JIT_ICALL_ADDR, g_strdup_printf ("specific_trampoline_lazy_fetch_%u", slot));
713                 amd64_jump_reg (code, AMD64_R11);
714         } else {
715                 tramp = (guint8 *)mono_arch_create_specific_trampoline (GUINT_TO_POINTER (slot), MONO_TRAMPOLINE_RGCTX_LAZY_FETCH, mono_get_root_domain (), NULL);
716
717                 /* jump to the actual trampoline */
718                 amd64_jump_code (code, tramp);
719         }
720
721         mono_arch_flush_icache (buf, code - buf);
722         mono_profiler_code_buffer_new (buf, code - buf, MONO_PROFILER_CODE_BUFFER_GENERICS_TRAMPOLINE, NULL);
723
724         g_assert (code - buf <= tramp_size);
725         g_assert_checked (mono_arch_unwindinfo_validate_size (unwind_ops, MONO_TRAMPOLINE_UNWINDINFO_SIZE(0)));
726
727         char *name = mono_get_rgctx_fetch_trampoline_name (slot);
728         *info = mono_tramp_info_create (name, buf, code - buf, ji, unwind_ops);
729         g_free (name);
730
731         return buf;
732 }
733
734 gpointer
735 mono_arch_create_general_rgctx_lazy_fetch_trampoline (MonoTrampInfo **info, gboolean aot)
736 {
737         guint8 *code, *buf;
738         int tramp_size;
739         MonoJumpInfo *ji = NULL;
740         GSList *unwind_ops;
741
742         g_assert (aot);
743         tramp_size = 64;
744
745         code = buf = (guint8 *)mono_global_codeman_reserve (tramp_size + MONO_TRAMPOLINE_UNWINDINFO_SIZE(0));
746
747         unwind_ops = mono_arch_get_cie_program ();
748
749         // FIXME: Currently, we always go to the slow path.
750         /* This receives a <slot, trampoline> in the rgctx arg reg. */
751         /* Load trampoline addr */
752         amd64_mov_reg_membase (code, AMD64_R11, MONO_ARCH_RGCTX_REG, 8, 8);
753         /* move the rgctx pointer to the VTABLE register */
754         amd64_mov_reg_reg (code, MONO_ARCH_VTABLE_REG, AMD64_ARG_REG1, sizeof(gpointer));
755         /* Jump to the trampoline */
756         amd64_jump_reg (code, AMD64_R11);
757
758         mono_arch_flush_icache (buf, code - buf);
759         mono_profiler_code_buffer_new (buf, code - buf, MONO_PROFILER_CODE_BUFFER_GENERICS_TRAMPOLINE, NULL);
760
761         g_assert (code - buf <= tramp_size);
762         g_assert_checked (mono_arch_unwindinfo_validate_size (unwind_ops, MONO_TRAMPOLINE_UNWINDINFO_SIZE(0)));
763
764         if (info)
765                 *info = mono_tramp_info_create ("rgctx_fetch_trampoline_general", buf, code - buf, ji, unwind_ops);
766
767         return buf;
768 }
769
770 void
771 mono_arch_invalidate_method (MonoJitInfo *ji, void *func, gpointer func_arg)
772 {
773         /* FIXME: This is not thread safe */
774         guint8 *code = (guint8 *)ji->code_start;
775
776         amd64_mov_reg_imm (code, AMD64_ARG_REG1, func_arg);
777         amd64_mov_reg_imm (code, AMD64_R11, func);
778
779         x86_push_imm (code, (guint64)func_arg);
780         amd64_call_reg (code, AMD64_R11);
781 }
782 #endif /* !DISABLE_JIT */
783
784 gpointer
785 mono_amd64_handler_block_trampoline_helper (void)
786 {
787         MonoJitTlsData *jit_tls = (MonoJitTlsData *)mono_tls_get_jit_tls ();
788         return jit_tls->handler_block_return_address;
789 }
790
791 #ifndef DISABLE_JIT
792 gpointer
793 mono_arch_create_handler_block_trampoline (MonoTrampInfo **info, gboolean aot)
794 {
795         guint8 *code, *buf;
796         int tramp_size = 64;
797         MonoJumpInfo *ji = NULL;
798         GSList *unwind_ops;
799
800         code = buf = (guint8 *)mono_global_codeman_reserve (tramp_size + MONO_TRAMPOLINE_UNWINDINFO_SIZE(0));
801
802         unwind_ops = mono_arch_get_cie_program ();
803
804         /*
805          * This trampoline restore the call chain of the handler block then jumps into the code that deals with it.
806          * We get here from the ret emitted by CEE_ENDFINALLY.
807          * The stack is misaligned.
808          */
809         /* Align the stack before the call to mono_amd64_handler_block_trampoline_helper() */
810 #ifdef TARGET_WIN32
811         /* Also make room for the "register parameter stack area" as specified by the Windows x64 ABI (4 64-bit registers) */
812         amd64_alu_reg_imm (code, X86_SUB, AMD64_RSP, 8 + 4 * 8);
813 #else
814         amd64_alu_reg_imm (code, X86_SUB, AMD64_RSP, 8);
815 #endif
816         if (aot) {
817                 code = mono_arch_emit_load_aotconst (buf, code, &ji, MONO_PATCH_INFO_JIT_ICALL_ADDR, "mono_amd64_handler_block_trampoline_helper");
818                 amd64_call_reg (code, AMD64_R11);
819         } else {
820                 amd64_mov_reg_imm (code, AMD64_RAX, mono_amd64_handler_block_trampoline_helper);
821                 amd64_call_reg (code, AMD64_RAX);
822         }
823         /* Undo stack alignment */
824 #ifdef TARGET_WIN32
825         amd64_alu_reg_imm (code, X86_ADD, AMD64_RSP, 8 + 4 * 8);
826 #else
827         amd64_alu_reg_imm (code, X86_ADD, AMD64_RSP, 8);
828 #endif
829         /* Save the result to the stack */
830         amd64_push_reg (code, AMD64_RAX);
831 #ifdef TARGET_WIN32
832         /* Make room for the "register parameter stack area" as specified by the Windows x64 ABI (4 64-bit registers) */
833         amd64_alu_reg_imm (code, X86_SUB, AMD64_RSP, 4 * 8);
834 #endif
835         if (aot) {
836                 char *name = g_strdup_printf ("trampoline_func_%d", MONO_TRAMPOLINE_HANDLER_BLOCK_GUARD);
837                 code = mono_arch_emit_load_aotconst (buf, code, &ji, MONO_PATCH_INFO_JIT_ICALL_ADDR, name);
838                 amd64_mov_reg_reg (code, AMD64_RAX, AMD64_R11, 8);
839         } else {
840                 amd64_mov_reg_imm (code, AMD64_RAX, mono_get_trampoline_func (MONO_TRAMPOLINE_HANDLER_BLOCK_GUARD));
841         }
842         /* The stack is aligned */
843         amd64_call_reg (code, AMD64_RAX);
844 #ifdef TARGET_WIN32
845         amd64_alu_reg_imm (code, X86_ADD, AMD64_RSP, 4 * 8);
846 #endif
847         /* Load return address */
848         amd64_pop_reg (code, AMD64_RAX);
849         /* The stack is misaligned, thats what the code we branch to expects */
850         amd64_jump_reg (code, AMD64_RAX);
851
852         mono_arch_flush_icache (buf, code - buf);
853         mono_profiler_code_buffer_new (buf, code - buf, MONO_PROFILER_CODE_BUFFER_HELPER, NULL);
854         g_assert (code - buf <= tramp_size);
855         g_assert_checked (mono_arch_unwindinfo_validate_size (unwind_ops, MONO_TRAMPOLINE_UNWINDINFO_SIZE(0)));
856
857         *info = mono_tramp_info_create ("handler_block_trampoline", buf, code - buf, ji, unwind_ops);
858
859         return buf;
860 }
861 #endif /* !DISABLE_JIT */
862
863 /*
864  * mono_arch_get_call_target:
865  *
866  *   Return the address called by the code before CODE if exists.
867  */
868 guint8*
869 mono_arch_get_call_target (guint8 *code)
870 {
871         if (code [-5] == 0xe8) {
872                 gint32 disp = *(gint32*)(code - 4);
873                 guint8 *target = code + disp;
874
875                 return target;
876         } else {
877                 return NULL;
878         }
879 }
880
881 /*
882  * mono_arch_get_plt_info_offset:
883  *
884  *   Return the PLT info offset belonging to the plt entry PLT_ENTRY.
885  */
886 guint32
887 mono_arch_get_plt_info_offset (guint8 *plt_entry, mgreg_t *regs, guint8 *code)
888 {
889         return *(guint32*)(plt_entry + 6);
890 }
891
892 #ifndef DISABLE_JIT
893 /*
894  * mono_arch_create_sdb_trampoline:
895  *
896  *   Return a trampoline which captures the current context, passes it to
897  * debugger_agent_single_step_from_context ()/debugger_agent_breakpoint_from_context (),
898  * then restores the (potentially changed) context.
899  */
900 guint8*
901 mono_arch_create_sdb_trampoline (gboolean single_step, MonoTrampInfo **info, gboolean aot)
902 {
903         int tramp_size = 512;
904         int i, framesize, ctx_offset, cfa_offset, gregs_offset;
905         guint8 *code, *buf;
906         GSList *unwind_ops = NULL;
907         MonoJumpInfo *ji = NULL;
908
909         code = buf = (guint8 *)mono_global_codeman_reserve (tramp_size + MONO_MAX_TRAMPOLINE_UNWINDINFO_SIZE);
910
911         framesize = 0;
912 #ifdef TARGET_WIN32
913         /* Reserve space where the callee can save the argument registers */
914         framesize += 4 * sizeof (mgreg_t);
915 #endif
916
917         ctx_offset = framesize;
918         framesize += sizeof (MonoContext);
919
920         framesize = ALIGN_TO (framesize, MONO_ARCH_FRAME_ALIGNMENT);
921
922         // CFA = sp + 8
923         cfa_offset = 8;
924         mono_add_unwind_op_def_cfa (unwind_ops, code, buf, AMD64_RSP, 8);
925         // IP saved at CFA - 8
926         mono_add_unwind_op_offset (unwind_ops, code, buf, AMD64_RIP, -cfa_offset);
927
928         amd64_push_reg (code, AMD64_RBP);
929         cfa_offset += sizeof(mgreg_t);
930         mono_add_unwind_op_def_cfa_offset (unwind_ops, code, buf, cfa_offset);
931         mono_add_unwind_op_offset (unwind_ops, code, buf, AMD64_RBP, - cfa_offset);
932
933         amd64_mov_reg_reg (code, AMD64_RBP, AMD64_RSP, sizeof(mgreg_t));
934         mono_add_unwind_op_def_cfa_reg (unwind_ops, code, buf, AMD64_RBP);
935         mono_add_unwind_op_fp_alloc (unwind_ops, code, buf, AMD64_RBP, 0);
936         amd64_alu_reg_imm (code, X86_SUB, AMD64_RSP, framesize);
937
938         gregs_offset = ctx_offset + MONO_STRUCT_OFFSET (MonoContext, gregs);
939
940         /* Initialize a MonoContext structure on the stack */
941         for (i = 0; i < AMD64_NREG; ++i) {
942                 if (i != AMD64_RIP && i != AMD64_RSP && i != AMD64_RBP)
943                         amd64_mov_membase_reg (code, AMD64_RSP, gregs_offset + (i * sizeof (mgreg_t)), i, sizeof (mgreg_t));
944         }
945         amd64_mov_reg_membase (code, AMD64_R11, AMD64_RBP, 0, sizeof (mgreg_t));
946         amd64_mov_membase_reg (code, AMD64_RSP, gregs_offset + (AMD64_RBP * sizeof (mgreg_t)), AMD64_R11, sizeof (mgreg_t));
947         amd64_lea_membase (code, AMD64_R11, AMD64_RBP, 2 * sizeof (mgreg_t));
948         amd64_mov_membase_reg (code, AMD64_RSP, gregs_offset + (AMD64_RSP * sizeof (mgreg_t)), AMD64_R11, sizeof (mgreg_t));
949         amd64_mov_reg_membase (code, AMD64_R11, AMD64_RBP, sizeof (mgreg_t), sizeof (mgreg_t));
950         amd64_mov_membase_reg (code, AMD64_RSP, gregs_offset + (AMD64_RIP * sizeof (mgreg_t)), AMD64_R11, sizeof (mgreg_t));
951
952         /* Call the single step/breakpoint function in sdb */
953         amd64_lea_membase (code, AMD64_ARG_REG1, AMD64_RSP, ctx_offset);
954
955         if (aot) {
956                 if (single_step)
957                         code = mono_arch_emit_load_aotconst (buf, code, &ji, MONO_PATCH_INFO_JIT_ICALL_ADDR, "debugger_agent_single_step_from_context");
958                 else
959                         code = mono_arch_emit_load_aotconst (buf, code, &ji, MONO_PATCH_INFO_JIT_ICALL_ADDR, "debugger_agent_breakpoint_from_context");
960         } else {
961                 if (single_step)
962                         amd64_mov_reg_imm (code, AMD64_R11, debugger_agent_single_step_from_context);
963                 else
964                         amd64_mov_reg_imm (code, AMD64_R11, debugger_agent_breakpoint_from_context);
965         }       
966         amd64_call_reg (code, AMD64_R11);
967
968         /* Restore registers from ctx */
969         for (i = 0; i < AMD64_NREG; ++i) {
970                 if (i != AMD64_RIP && i != AMD64_RSP && i != AMD64_RBP)
971                         amd64_mov_reg_membase (code, i, AMD64_RSP, gregs_offset + (i * sizeof (mgreg_t)), sizeof (mgreg_t));
972         }
973         amd64_mov_reg_membase (code, AMD64_R11, AMD64_RSP, gregs_offset + (AMD64_RBP * sizeof (mgreg_t)), sizeof (mgreg_t));
974         amd64_mov_membase_reg (code, AMD64_RBP, 0, AMD64_R11, sizeof (mgreg_t));
975         amd64_mov_reg_membase (code, AMD64_R11, AMD64_RSP, gregs_offset + (AMD64_RIP * sizeof (mgreg_t)), sizeof (mgreg_t));
976         amd64_mov_membase_reg (code, AMD64_RBP, sizeof (mgreg_t), AMD64_R11, sizeof (mgreg_t));
977
978 #if TARGET_WIN32
979         amd64_lea_membase (code, AMD64_RSP, AMD64_RBP, 0);
980         amd64_pop_reg (code, AMD64_RBP);
981         mono_add_unwind_op_same_value (unwind_ops, code, buf, AMD64_RBP);
982 #else
983         amd64_leave (code);
984 #endif
985         cfa_offset -= sizeof (mgreg_t);
986         mono_add_unwind_op_def_cfa (unwind_ops, code, buf, AMD64_RSP, cfa_offset);
987         amd64_ret (code);
988
989         mono_arch_flush_icache (code, code - buf);
990         mono_profiler_code_buffer_new (buf, code - buf, MONO_PROFILER_CODE_BUFFER_HELPER, NULL);
991         g_assert (code - buf <= tramp_size);
992         g_assert_checked (mono_arch_unwindinfo_validate_size (unwind_ops, MONO_MAX_TRAMPOLINE_UNWINDINFO_SIZE));
993
994         const char *tramp_name = single_step ? "sdb_single_step_trampoline" : "sdb_breakpoint_trampoline";
995         *info = mono_tramp_info_create (tramp_name, buf, code - buf, ji, unwind_ops);
996
997         return buf;
998 }
999
1000 /*
1001  * mono_arch_get_enter_icall_trampoline:
1002  *
1003  *   A trampoline that handles the transition from interpreter into native world.
1004  *   It requiers to set up a descriptor (MethodArguments) that describes the
1005  *   required arguments passed to the callee.
1006  */
1007 gpointer
1008 mono_arch_get_enter_icall_trampoline (MonoTrampInfo **info)
1009 {
1010 #ifdef ENABLE_INTERPRETER
1011         const int gregs_num = 8;
1012         const int fregs_num = 3;
1013         guint8 *start = NULL, *code, *label_gexits [gregs_num], *label_fexits [fregs_num], *label_leave_tramp [3], *label_is_float_ret;
1014         MonoJumpInfo *ji = NULL;
1015         GSList *unwind_ops = NULL;
1016         static int farg_regs[] = {AMD64_XMM0, AMD64_XMM1, AMD64_XMM2};
1017         int i, framesize = 0, off_rbp, off_methodargs, off_targetaddr;
1018
1019         start = code = (guint8 *) mono_global_codeman_reserve (256 + MONO_TRAMPOLINE_UNWINDINFO_SIZE(0));
1020
1021         off_rbp = -framesize;
1022
1023         framesize += sizeof (mgreg_t);
1024         off_methodargs = -framesize;
1025
1026         framesize += sizeof (mgreg_t);
1027         off_targetaddr = -framesize;
1028
1029         framesize += (gregs_num - PARAM_REGS) * sizeof (mgreg_t);
1030
1031         amd64_push_reg (code, AMD64_RBP);
1032         amd64_mov_reg_reg (code, AMD64_RBP, AMD64_RSP, sizeof (mgreg_t));
1033         amd64_alu_reg_imm (code, X86_SUB, AMD64_RSP, ALIGN_TO (framesize, MONO_ARCH_FRAME_ALIGNMENT));
1034
1035         /* save MethodArguments* onto stack */
1036         amd64_mov_membase_reg (code, AMD64_RBP, off_methodargs, AMD64_ARG_REG2, sizeof (mgreg_t));
1037
1038         /* save target address on stack */
1039         amd64_mov_membase_reg (code, AMD64_RBP, off_targetaddr, AMD64_ARG_REG1, sizeof (mgreg_t));
1040
1041         /* load pointer to MethodArguments* into R11 */
1042         amd64_mov_reg_reg (code, AMD64_R11, AMD64_ARG_REG2, 8);
1043         
1044         /* move flen into RAX */ // TODO: struct offset
1045         amd64_mov_reg_membase (code, AMD64_RAX, AMD64_R11, 16, sizeof (mgreg_t));
1046         /* load pointer to fregs into R11 */ // TODO: struct offset
1047         amd64_mov_reg_membase (code, AMD64_R11, AMD64_R11, 24, sizeof (mgreg_t));
1048
1049         for (i = 0; i < fregs_num; ++i) {
1050                 amd64_test_reg_reg (code, AMD64_RAX, AMD64_RAX);
1051                 label_fexits [i] = code;
1052                 x86_branch8 (code, X86_CC_Z, 0, FALSE);
1053
1054                 amd64_sse_movsd_reg_membase (code, farg_regs [i], AMD64_R11, i * sizeof (double));
1055                 amd64_dec_reg_size (code, AMD64_RAX, 1);
1056         }
1057
1058         for (i = 0; i < fregs_num; i++) {
1059                 x86_patch (label_fexits [i], code);
1060         }
1061
1062         /* load pointer to MethodArguments* into R11 */
1063         amd64_mov_reg_reg (code, AMD64_R11, AMD64_ARG_REG2, sizeof (mgreg_t));
1064         /* move ilen into RAX */ // TODO: struct offset
1065         amd64_mov_reg_membase (code, AMD64_RAX, AMD64_R11, 0, sizeof (mgreg_t));
1066
1067         int stack_offset = 0;
1068         for (i = 0; i < gregs_num; i++) {
1069                 amd64_test_reg_reg (code, AMD64_RAX, AMD64_RAX);
1070                 label_gexits [i] = code;
1071                 x86_branch32 (code, X86_CC_Z, 0, FALSE);
1072
1073                 /* load pointer to MethodArguments* into R11 */
1074                 amd64_mov_reg_membase (code, AMD64_R11, AMD64_RBP, off_methodargs, sizeof (mgreg_t));
1075                 /* load pointer to iregs into R11 */ // TODO: struct offset
1076                 amd64_mov_reg_membase (code, AMD64_R11, AMD64_R11, 8, sizeof (mgreg_t));
1077
1078                 if (i < PARAM_REGS) {
1079                         amd64_mov_reg_membase (code, param_regs [i], AMD64_R11, i * sizeof (mgreg_t), sizeof (mgreg_t));
1080                 } else {
1081                         amd64_mov_reg_membase (code, AMD64_R11, AMD64_R11, i * sizeof (mgreg_t), sizeof (mgreg_t));
1082                         amd64_mov_membase_reg (code, AMD64_RSP, stack_offset, AMD64_R11, sizeof (mgreg_t));
1083                         stack_offset += sizeof (mgreg_t);
1084                 }
1085                 amd64_dec_reg_size (code, AMD64_RAX, 1);
1086         }
1087
1088         for (i = 0; i < gregs_num; i++) {
1089                 x86_patch (label_gexits [i], code);
1090         }
1091
1092         /* load target addr */
1093         amd64_mov_reg_membase (code, AMD64_R11, AMD64_RBP, off_targetaddr, sizeof (mgreg_t));
1094
1095         /* call into native function */
1096         amd64_call_reg (code, AMD64_R11);
1097
1098         /* load MethodArguments */
1099         amd64_mov_reg_membase (code, AMD64_R11, AMD64_RBP, off_methodargs, sizeof (mgreg_t));
1100
1101         /* load is_float_ret */ // TODO: struct offset
1102         amd64_mov_reg_membase (code, AMD64_R11, AMD64_R11, 0x28, sizeof (mgreg_t));
1103
1104         /* check if a float return value is expected */
1105         amd64_test_reg_reg (code, AMD64_R11, AMD64_R11);
1106
1107         label_is_float_ret = code;
1108         x86_branch8 (code, X86_CC_NZ, 0, FALSE);
1109
1110
1111
1112         /* greg return */
1113         /* load MethodArguments */
1114         amd64_mov_reg_membase (code, AMD64_R11, AMD64_RBP, off_methodargs, sizeof (mgreg_t));
1115         /* load retval */ // TODO: struct offset
1116         amd64_mov_reg_membase (code, AMD64_R11, AMD64_R11, 0x20, sizeof (mgreg_t));
1117
1118         amd64_test_reg_reg (code, AMD64_R11, AMD64_R11);
1119         label_leave_tramp [0] = code;
1120         x86_branch8 (code, X86_CC_Z, 0, FALSE);
1121
1122         amd64_mov_membase_reg (code, AMD64_R11, 0, AMD64_RAX, sizeof (mgreg_t));
1123
1124         label_leave_tramp [1] = code;
1125         x86_jump8 (code, 0);
1126
1127
1128
1129         /* freg return */
1130         x86_patch (label_is_float_ret, code);
1131         /* load MethodArguments */
1132         amd64_mov_reg_membase (code, AMD64_R11, AMD64_RBP, off_methodargs, sizeof (mgreg_t));
1133         /* load retval */ // TODO: struct offset
1134         amd64_mov_reg_membase (code, AMD64_R11, AMD64_R11, 0x20, sizeof (mgreg_t));
1135
1136         amd64_test_reg_reg (code, AMD64_R11, AMD64_R11);
1137         label_leave_tramp [2] = code;
1138         x86_branch8 (code, X86_CC_Z, 0, FALSE);
1139
1140         amd64_sse_movsd_membase_reg (code, AMD64_R11, 0, AMD64_XMM0);
1141
1142         for (i = 0; i < 3; i++)
1143                 x86_patch (label_leave_tramp [i], code);
1144
1145         amd64_alu_reg_imm (code, X86_ADD, AMD64_RSP, ALIGN_TO (framesize, MONO_ARCH_FRAME_ALIGNMENT));
1146         amd64_pop_reg (code, AMD64_RBP);
1147         amd64_ret (code);
1148
1149         mono_arch_flush_icache (start, code - start);
1150         mono_profiler_code_buffer_new (start, code - start, MONO_PROFILER_CODE_BUFFER_EXCEPTION_HANDLING, NULL);
1151
1152         if (info)
1153                 *info = mono_tramp_info_create ("enter_icall_trampoline", start, code - start, ji, unwind_ops);
1154
1155         return start;
1156 #else
1157         g_assert_not_reached ();
1158         return NULL;
1159 #endif /* ENABLE_INTERPRETER */
1160 }
1161 #endif /* !DISABLE_JIT */
1162
1163 #ifdef DISABLE_JIT
1164 gpointer
1165 mono_arch_get_unbox_trampoline (MonoMethod *m, gpointer addr)
1166 {
1167         g_assert_not_reached ();
1168         return NULL;
1169 }
1170
1171 gpointer
1172 mono_arch_get_static_rgctx_trampoline (MonoMethod *m, MonoMethodRuntimeGenericContext *mrgctx, gpointer addr)
1173 {
1174         g_assert_not_reached ();
1175         return NULL;
1176 }
1177
1178 gpointer
1179 mono_arch_create_rgctx_lazy_fetch_trampoline (guint32 slot, MonoTrampInfo **info, gboolean aot)
1180 {
1181         g_assert_not_reached ();
1182         return NULL;
1183 }
1184
1185 guchar*
1186 mono_arch_create_generic_trampoline (MonoTrampolineType tramp_type, MonoTrampInfo **info, gboolean aot)
1187 {
1188         g_assert_not_reached ();
1189         return NULL;
1190 }
1191
1192 gpointer
1193 mono_arch_create_specific_trampoline (gpointer arg1, MonoTrampolineType tramp_type, MonoDomain *domain, guint32 *code_len)
1194 {
1195         g_assert_not_reached ();
1196         return NULL;
1197 }
1198
1199 gpointer
1200 mono_arch_create_general_rgctx_lazy_fetch_trampoline (MonoTrampInfo **info, gboolean aot)
1201 {
1202         g_assert_not_reached ();
1203         return NULL;
1204 }
1205
1206 gpointer
1207 mono_arch_create_handler_block_trampoline (MonoTrampInfo **info, gboolean aot)
1208 {
1209         g_assert_not_reached ();
1210         return NULL;
1211 }
1212
1213 void
1214 mono_arch_invalidate_method (MonoJitInfo *ji, void *func, gpointer func_arg)
1215 {
1216         g_assert_not_reached ();
1217         return;
1218 }
1219
1220 guint8*
1221 mono_arch_create_sdb_trampoline (gboolean single_step, MonoTrampInfo **info, gboolean aot)
1222 {
1223         g_assert_not_reached ();
1224         return NULL;
1225 }
1226
1227 gpointer
1228 mono_arch_get_enter_icall_trampoline (MonoTrampInfo **info)
1229 {
1230         g_assert_not_reached ();
1231         return NULL;
1232 }
1233 #endif /* DISABLE_JIT */