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