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