Normalize line endings.
[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  */
10
11 #include <config.h>
12 #include <glib.h>
13
14 #include <mono/metadata/appdomain.h>
15 #include <mono/metadata/marshal.h>
16 #include <mono/metadata/tabledefs.h>
17 #include <mono/metadata/mono-debug-debugger.h>
18 #include <mono/metadata/monitor.h>
19 #include <mono/arch/amd64/amd64-codegen.h>
20
21 #include <mono/utils/memcheck.h>
22
23 #include "mini.h"
24 #include "mini-amd64.h"
25
26 #define IS_REX(inst) (((inst) >= 0x40) && ((inst) <= 0x4f))
27
28 static guint8* nullified_class_init_trampoline;
29
30 /*
31  * mono_arch_get_unbox_trampoline:
32  * @gsctx: the generic sharing context
33  * @m: method pointer
34  * @addr: pointer to native code for @m
35  *
36  * when value type methods are called through the vtable we need to unbox the
37  * this argument. This method returns a pointer to a trampoline which does
38  * unboxing before calling the method
39  */
40 gpointer
41 mono_arch_get_unbox_trampoline (MonoGenericSharingContext *gsctx, MonoMethod *m, gpointer addr)
42 {
43         guint8 *code, *start;
44         int this_reg;
45
46         MonoDomain *domain = mono_domain_get ();
47
48         this_reg = mono_arch_get_this_arg_reg (mono_method_signature (m), gsctx, NULL);
49
50         start = code = mono_domain_code_reserve (domain, 20);
51
52         amd64_alu_reg_imm (code, X86_ADD, this_reg, sizeof (MonoObject));
53         /* FIXME: Optimize this */
54         amd64_mov_reg_imm (code, AMD64_RAX, addr);
55         amd64_jump_reg (code, AMD64_RAX);
56         g_assert ((code - start) < 20);
57
58         mono_arch_flush_icache (start, code - start);
59
60         return start;
61 }
62
63 /*
64  * mono_arch_get_static_rgctx_trampoline:
65  *
66  *   Create a trampoline which sets RGCTX_REG to MRGCTX, then jumps to ADDR.
67  */
68 gpointer
69 mono_arch_get_static_rgctx_trampoline (MonoMethod *m, MonoMethodRuntimeGenericContext *mrgctx, gpointer addr)
70 {
71         guint8 *code, *start;
72         int buf_len;
73
74         MonoDomain *domain = mono_domain_get ();
75
76 #ifdef MONO_ARCH_NOMAP32BIT
77         buf_len = 32;
78 #else
79         /* AOTed code could still have a non-32 bit address */
80         if ((((guint64)addr) >> 32) == 0)
81                 buf_len = 16;
82         else
83                 buf_len = 30;
84 #endif
85
86         start = code = mono_domain_code_reserve (domain, buf_len);
87
88         amd64_mov_reg_imm (code, MONO_ARCH_RGCTX_REG, mrgctx);
89         amd64_jump_code (code, addr);
90         g_assert ((code - start) < buf_len);
91
92         mono_arch_flush_icache (start, code - start);
93
94         return start;
95 }
96
97 gpointer
98 mono_arch_get_llvm_imt_trampoline (MonoDomain *domain, MonoMethod *m, int vt_offset)
99 {
100         guint8 *code, *start;
101         int buf_len;
102         int this_reg;
103
104         buf_len = 32;
105
106         start = code = mono_domain_code_reserve (domain, buf_len);
107
108         this_reg = mono_arch_get_this_arg_reg (mono_method_signature (m), NULL, NULL);
109
110         /* Set imt arg */
111         amd64_mov_reg_imm (code, MONO_ARCH_IMT_REG, m);
112         /* Load vtable address */
113         amd64_mov_reg_membase (code, AMD64_RAX, this_reg, 0, 8);
114         amd64_jump_membase (code, AMD64_RAX, vt_offset);
115         amd64_ret (code);
116
117         g_assert ((code - start) < buf_len);
118
119         mono_arch_flush_icache (start, code - start);
120
121         return start;
122 }
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         if (((code [-13] == 0x49) && (code [-12] == 0xbb)) || (code [-5] == 0xe8)) {
140                 if (code [-5] != 0xe8) {
141                         if (can_write) {
142                                 InterlockedExchangePointer ((gpointer*)(orig_code - 11), addr);
143                                 VALGRIND_DISCARD_TRANSLATIONS (orig_code - 11, sizeof (gpointer));
144                         }
145                 } else {
146                         if ((((guint64)(addr)) >> 32) != 0) {
147 #ifdef MONO_ARCH_NOMAP32BIT
148                                 /* Print some diagnostics */
149                                 MonoJitInfo *ji = mono_jit_info_table_find (mono_domain_get (), (char*)orig_code);
150                                 if (ji)
151                                         fprintf (stderr, "At %s, offset 0x%zx\n", mono_method_full_name (ji->method, TRUE), (guint8*)orig_code - (guint8*)ji->code_start);
152                                 fprintf (stderr, "Addr: %p\n", addr);
153                                 ji = mono_jit_info_table_find (mono_domain_get (), (char*)addr);
154                                 if (ji)
155                                         fprintf (stderr, "Callee: %s\n", mono_method_full_name (ji->method, TRUE));
156                                 g_assert_not_reached ();
157 #else
158                                 /* 
159                                  * This might happen when calling AOTed code. Create a thunk.
160                                  */
161                                 guint8 *thunk_start, *thunk_code;
162
163                                 thunk_start = thunk_code = mono_domain_code_reserve (mono_domain_get (), 32);
164                                 amd64_jump_membase (thunk_code, AMD64_RIP, 0);
165                                 *(guint64*)thunk_code = (guint64)addr;
166                                 addr = thunk_start;
167                                 g_assert ((((guint64)(addr)) >> 32) == 0);
168                                 mono_arch_flush_icache (thunk_start, thunk_code - thunk_start);
169 #endif
170                         }
171                         g_assert ((((guint64)(orig_code)) >> 32) == 0);
172                         if (can_write) {
173                                 InterlockedExchange ((gint32*)(orig_code - 4), ((gint64)addr - (gint64)orig_code));
174                                 VALGRIND_DISCARD_TRANSLATIONS (orig_code - 5, 4);
175                         }
176                 }
177         }
178         else if ((code [-7] == 0x41) && (code [-6] == 0xff) && (code [-5] == 0x15)) {
179                 /* call *<OFFSET>(%rip) */
180                 gpointer *got_entry = (gpointer*)((guint8*)orig_code + (*(guint32*)(orig_code - 4)));
181                 if (can_write) {
182                         InterlockedExchangePointer (got_entry, addr);
183                         VALGRIND_DISCARD_TRANSLATIONS (orig_code - 5, sizeof (gpointer));
184                 }
185         }
186 }
187
188 void
189 mono_arch_patch_plt_entry (guint8 *code, gpointer *got, mgreg_t *regs, guint8 *addr)
190 {
191         gint32 disp;
192         gpointer *plt_jump_table_entry;
193
194         /* A PLT entry: jmp *<DISP>(%rip) */
195         g_assert (code [0] == 0xff);
196         g_assert (code [1] == 0x25);
197
198         disp = *(gint32*)(code + 2);
199
200         plt_jump_table_entry = (gpointer*)(code + 6 + disp);
201
202         InterlockedExchangePointer (plt_jump_table_entry, addr);
203 }
204
205 void
206 mono_arch_nullify_class_init_trampoline (guint8 *code, mgreg_t *regs)
207 {
208         guint8 buf [16];
209         MonoJitInfo *ji = NULL;
210         gboolean can_write;
211
212         if (mono_use_llvm) {
213                 /* code - 7 might be before the start of the method */
214                 /* FIXME: Avoid this expensive call somehow */
215                 ji = mono_jit_info_table_find (mono_domain_get (), (char*)code);
216         }
217
218         can_write = mono_breakpoint_clean_code (ji ? ji->code_start : NULL, code, 7, buf, sizeof (buf));
219
220         if (!can_write)
221                 return;
222
223         /* 
224          * A given byte sequence can match more than case here, so we have to be
225          * really careful about the ordering of the cases. Longer sequences
226          * come first.
227          */
228         if ((buf [0] == 0x41) && (buf [1] == 0xff) && (buf [2] == 0x15)) {
229                 gpointer *vtable_slot;
230
231                 /* call *<OFFSET>(%rip) */
232                 vtable_slot = mono_get_vcall_slot_addr (code, regs);
233                 g_assert (vtable_slot);
234
235                 *vtable_slot = nullified_class_init_trampoline;
236         } else if (buf [2] == 0xe8) {
237                 /* call <TARGET> */
238                 //guint8 *buf = code - 2;
239
240                 /* 
241                  * It would be better to replace the call with nops, but that doesn't seem
242                  * to work on SMP machines even when the whole call is inside a cache line.
243                  * Patching the call address seems to work.
244                  */
245                 /*
246                 buf [0] = 0x66;
247                 buf [1] = 0x66;
248                 buf [2] = 0x90;
249                 buf [3] = 0x66;
250                 buf [4] = 0x90;
251                 */
252
253                 mono_arch_patch_callsite (code - 5, code, nullified_class_init_trampoline);
254         } else if ((buf [5] == 0xff) && x86_modrm_mod (buf [6]) == 3 && x86_modrm_reg (buf [6]) == 2) {
255                 /* call *<reg> */
256                 /* Generated by the LLVM JIT or on platforms without MAP_32BIT set */
257                 mono_arch_patch_callsite (code - 13, code, nullified_class_init_trampoline);
258         } else if (buf [4] == 0x90 || buf [5] == 0xeb || buf [6] == 0x66) {
259                 /* Already changed by another thread */
260                 ;
261         } else {
262                 printf ("Invalid trampoline sequence: %x %x %x %x %x %x %x\n", buf [0], buf [1], buf [2], buf [3],
263                         buf [4], buf [5], buf [6]);
264                 g_assert_not_reached ();
265         }
266 }
267
268 void
269 mono_arch_nullify_plt_entry (guint8 *code, mgreg_t *regs)
270 {
271         if (mono_aot_only && !nullified_class_init_trampoline)
272                 nullified_class_init_trampoline = mono_aot_get_trampoline ("nullified_class_init_trampoline");
273
274         mono_arch_patch_plt_entry (code, NULL, regs, nullified_class_init_trampoline);
275 }
276
277 guchar*
278 mono_arch_create_generic_trampoline (MonoTrampolineType tramp_type, MonoTrampInfo **info, gboolean aot)
279 {
280         guint8 *buf, *code, *tramp, *br [2], *r11_save_code, *after_r11_save_code;
281         int i, lmf_offset, offset, res_offset, arg_offset, rax_offset, tramp_offset;
282         int buf_len, saved_regs_offset;
283         int saved_fpregs_offset, rbp_offset, framesize, orig_rsp_to_rbp_offset, cfa_offset;
284         gboolean has_caller;
285         GSList *unwind_ops = NULL;
286         MonoJumpInfo *ji = NULL;
287
288         if (tramp_type == MONO_TRAMPOLINE_JUMP)
289                 has_caller = FALSE;
290         else
291                 has_caller = TRUE;
292
293         buf_len = 548;
294         code = buf = mono_global_codeman_reserve (buf_len);
295
296         framesize = 538 + sizeof (MonoLMF);
297         framesize = (framesize + (MONO_ARCH_FRAME_ALIGNMENT - 1)) & ~ (MONO_ARCH_FRAME_ALIGNMENT - 1);
298
299         orig_rsp_to_rbp_offset = 0;
300         r11_save_code = code;
301         /* Reserve 5 bytes for the mov_membase_reg to save R11 */
302         code += 5;
303         after_r11_save_code = code;
304
305         // CFA = sp + 16 (the trampoline address is on the stack)
306         cfa_offset = 16;
307         mono_add_unwind_op_def_cfa (unwind_ops, code, buf, AMD64_RSP, 16);
308         // IP saved at CFA - 8
309         mono_add_unwind_op_offset (unwind_ops, code, buf, AMD64_RIP, -8);
310
311         /* Pop the return address off the stack */
312         amd64_pop_reg (code, AMD64_R11);
313         orig_rsp_to_rbp_offset += 8;
314
315         cfa_offset -= 8;
316         mono_add_unwind_op_def_cfa_offset (unwind_ops, code, buf, cfa_offset);
317
318         /* 
319          * Allocate a new stack frame
320          */
321         amd64_push_reg (code, AMD64_RBP);
322         cfa_offset += 8;
323         mono_add_unwind_op_def_cfa_offset (unwind_ops, code, buf, cfa_offset);
324         mono_add_unwind_op_offset (unwind_ops, code, buf, AMD64_RBP, - cfa_offset);
325
326         orig_rsp_to_rbp_offset -= 8;
327         amd64_mov_reg_reg (code, AMD64_RBP, AMD64_RSP, 8);
328         mono_add_unwind_op_def_cfa_reg (unwind_ops, code, buf, AMD64_RBP);
329         amd64_alu_reg_imm (code, X86_SUB, AMD64_RSP, framesize);
330
331         offset = 0;
332         rbp_offset = - offset;
333
334         offset += 8;
335         rax_offset = - offset;
336
337         offset += 8;
338         tramp_offset = - offset;
339
340         offset += 8;
341         arg_offset = - offset;
342
343         /* Compute the trampoline address from the return address */
344         if (aot) {
345                 /* 7 = length of call *<offset>(rip) */
346                 amd64_alu_reg_imm (code, X86_SUB, AMD64_R11, 7);
347         } else {
348                 /* 5 = length of amd64_call_membase () */
349                 amd64_alu_reg_imm (code, X86_SUB, AMD64_R11, 5);
350         }
351         amd64_mov_membase_reg (code, AMD64_RBP, tramp_offset, AMD64_R11, 8);
352
353         offset += 8;
354         res_offset = - offset;
355
356         /* Save all registers */
357
358         offset += AMD64_NREG * 8;
359         saved_regs_offset = - offset;
360         for (i = 0; i < AMD64_NREG; ++i) {
361                 if (i == AMD64_RBP) {
362                         /* RAX is already saved */
363                         amd64_mov_reg_membase (code, AMD64_RAX, AMD64_RBP, rbp_offset, 8);
364                         amd64_mov_membase_reg (code, AMD64_RBP, saved_regs_offset + (i * 8), AMD64_RAX, 8);
365                 } else if (i != AMD64_R11) {
366                         amd64_mov_membase_reg (code, AMD64_RBP, saved_regs_offset + (i * 8), i, 8);
367                 } else {
368                         /* We have to save R11 right at the start of
369                            the trampoline code because it's used as a
370                            scratch register */
371                         amd64_mov_membase_reg (r11_save_code, AMD64_RSP, saved_regs_offset + orig_rsp_to_rbp_offset + (i * 8), i, 8);
372                         g_assert (r11_save_code == after_r11_save_code);
373                 }
374         }
375         offset += 8 * 8;
376         saved_fpregs_offset = - offset;
377         for (i = 0; i < 8; ++i)
378                 amd64_movsd_membase_reg (code, AMD64_RBP, saved_fpregs_offset + (i * 8), i);
379
380         if (tramp_type != MONO_TRAMPOLINE_GENERIC_CLASS_INIT &&
381                         tramp_type != MONO_TRAMPOLINE_MONITOR_ENTER &&
382                         tramp_type != MONO_TRAMPOLINE_MONITOR_EXIT) {
383                 /* Obtain the trampoline argument which is encoded in the instruction stream */
384                 if (aot) {
385                         /* Load the GOT offset */
386                         amd64_mov_reg_membase (code, AMD64_R11, AMD64_RBP, tramp_offset, 8);
387                         amd64_mov_reg_membase (code, AMD64_RAX, AMD64_R11, 7, 4);
388                         /* Compute the address of the GOT slot */
389                         amd64_alu_reg_reg_size (code, X86_ADD, AMD64_R11, AMD64_RAX, 8);
390                         /* Load the value */
391                         amd64_mov_reg_membase (code, AMD64_R11, AMD64_R11, 0, 8);
392                 } else {                        
393                         amd64_mov_reg_membase (code, AMD64_R11, AMD64_RBP, tramp_offset, 8);
394                         amd64_mov_reg_membase (code, AMD64_RAX, AMD64_R11, 5, 1);
395                         amd64_widen_reg (code, AMD64_RAX, AMD64_RAX, TRUE, FALSE);
396                         amd64_alu_reg_imm_size (code, X86_CMP, AMD64_RAX, 4, 1);
397                         br [0] = code;
398                         x86_branch8 (code, X86_CC_NE, 6, FALSE);
399                         /* 32 bit immediate */
400                         amd64_mov_reg_membase (code, AMD64_R11, AMD64_R11, 6, 4);
401                         br [1] = code;
402                         x86_jump8 (code, 10);
403                         /* 64 bit immediate */
404                         mono_amd64_patch (br [0], code);
405                         amd64_mov_reg_membase (code, AMD64_R11, AMD64_R11, 6, 8);
406                         mono_amd64_patch (br [1], code);
407                 }
408                 amd64_mov_membase_reg (code, AMD64_RBP, arg_offset, AMD64_R11, 8);
409         } else {
410                 amd64_mov_reg_membase (code, AMD64_R11, AMD64_RBP, saved_regs_offset + (MONO_AMD64_ARG_REG1 * 8), 8);
411                 amd64_mov_membase_reg (code, AMD64_RBP, arg_offset, AMD64_R11, 8);
412         }
413
414         /* Save LMF begin */
415
416         offset += sizeof (MonoLMF);
417         lmf_offset = - offset;
418
419         /* Save ip */
420         if (has_caller)
421                 amd64_mov_reg_membase (code, AMD64_R11, AMD64_RBP, 8, 8);
422         else
423                 amd64_mov_reg_imm (code, AMD64_R11, 0);
424         amd64_mov_membase_reg (code, AMD64_RBP, lmf_offset + G_STRUCT_OFFSET (MonoLMF, rip), AMD64_R11, 8);
425         /* Save fp */
426         amd64_mov_reg_membase (code, AMD64_R11, AMD64_RSP, framesize, 8);
427         amd64_mov_membase_reg (code, AMD64_RBP, lmf_offset + G_STRUCT_OFFSET (MonoLMF, rbp), AMD64_R11, 8);
428         /* Save sp */
429         amd64_mov_reg_reg (code, AMD64_R11, AMD64_RSP, 8);
430         amd64_alu_reg_imm (code, X86_ADD, AMD64_R11, framesize + 16);
431         amd64_mov_membase_reg (code, AMD64_RBP, lmf_offset + G_STRUCT_OFFSET (MonoLMF, rsp), AMD64_R11, 8);
432         /* Save method */
433         if (tramp_type == MONO_TRAMPOLINE_JIT || tramp_type == MONO_TRAMPOLINE_JUMP) {
434                 amd64_mov_reg_membase (code, AMD64_R11, AMD64_RBP, arg_offset, 8);
435                 amd64_mov_membase_reg (code, AMD64_RBP, lmf_offset + G_STRUCT_OFFSET (MonoLMF, method), AMD64_R11, 8);
436         } else {
437                 amd64_mov_membase_imm (code, AMD64_RBP, lmf_offset + G_STRUCT_OFFSET (MonoLMF, method), 0, 8);
438         }
439         /* Save callee saved regs */
440 #ifdef TARGET_WIN32
441         amd64_mov_membase_reg (code, AMD64_RBP, lmf_offset + G_STRUCT_OFFSET (MonoLMF, rdi), AMD64_RDI, 8);
442         amd64_mov_membase_reg (code, AMD64_RBP, lmf_offset + G_STRUCT_OFFSET (MonoLMF, rsi), AMD64_RSI, 8);
443 #endif
444         amd64_mov_membase_reg (code, AMD64_RBP, lmf_offset + G_STRUCT_OFFSET (MonoLMF, rbx), AMD64_RBX, 8);
445         amd64_mov_membase_reg (code, AMD64_RBP, lmf_offset + G_STRUCT_OFFSET (MonoLMF, r12), AMD64_R12, 8);
446         amd64_mov_membase_reg (code, AMD64_RBP, lmf_offset + G_STRUCT_OFFSET (MonoLMF, r13), AMD64_R13, 8);
447         amd64_mov_membase_reg (code, AMD64_RBP, lmf_offset + G_STRUCT_OFFSET (MonoLMF, r14), AMD64_R14, 8);
448         amd64_mov_membase_reg (code, AMD64_RBP, lmf_offset + G_STRUCT_OFFSET (MonoLMF, r15), AMD64_R15, 8);
449
450         if (aot) {
451                 code = mono_arch_emit_load_aotconst (buf, code, &ji, MONO_PATCH_INFO_JIT_ICALL_ADDR, "mono_get_lmf_addr");
452         } else {
453                 amd64_mov_reg_imm (code, AMD64_R11, mono_get_lmf_addr);
454         }
455         amd64_call_reg (code, AMD64_R11);
456
457         /* Save lmf_addr */
458         amd64_mov_membase_reg (code, AMD64_RBP, lmf_offset + G_STRUCT_OFFSET (MonoLMF, lmf_addr), AMD64_RAX, 8);
459         /* Save previous_lmf */
460         /* Set the lowest bit to 1 to signal that this LMF has the ip field set */
461         amd64_mov_reg_membase (code, AMD64_R11, AMD64_RAX, 0, 8);
462         amd64_alu_reg_imm_size (code, X86_ADD, AMD64_R11, 1, 8);
463         amd64_mov_membase_reg (code, AMD64_RBP, lmf_offset + G_STRUCT_OFFSET (MonoLMF, previous_lmf), AMD64_R11, 8);
464         /* Set new lmf */
465         amd64_lea_membase (code, AMD64_R11, AMD64_RBP, lmf_offset);
466         amd64_mov_membase_reg (code, AMD64_RAX, 0, AMD64_R11, 8);
467
468         /* Save LMF end */
469
470         /* Arg1 is the pointer to the saved registers */
471         amd64_lea_membase (code, AMD64_ARG_REG1, AMD64_RBP, saved_regs_offset);
472
473         /* Arg2 is the address of the calling code */
474         if (has_caller)
475                 amd64_mov_reg_membase (code, AMD64_ARG_REG2, AMD64_RBP, 8, 8);
476         else
477                 amd64_mov_reg_imm (code, AMD64_ARG_REG2, 0);
478
479         /* Arg3 is the method/vtable ptr */
480         amd64_mov_reg_membase (code, AMD64_ARG_REG3, AMD64_RBP, arg_offset, 8);
481
482         /* Arg4 is the trampoline address */
483         amd64_mov_reg_membase (code, AMD64_ARG_REG4, AMD64_RBP, tramp_offset, 8);
484
485         if (aot) {
486                 char *icall_name = g_strdup_printf ("trampoline_func_%d", tramp_type);
487                 code = mono_arch_emit_load_aotconst (buf, code, &ji, MONO_PATCH_INFO_JIT_ICALL_ADDR, icall_name);
488         } else {
489                 tramp = (guint8*)mono_get_trampoline_func (tramp_type);
490                 amd64_mov_reg_imm (code, AMD64_R11, tramp);
491         }
492         amd64_call_reg (code, AMD64_R11);
493
494         /* Check for thread interruption */
495         /* This is not perf critical code so no need to check the interrupt flag */
496         /* 
497          * Have to call the _force_ variant, since there could be a protected wrapper on the top of the stack.
498          */
499         amd64_mov_membase_reg (code, AMD64_RBP, res_offset, AMD64_RAX, 8);
500         if (aot) {
501                 code = mono_arch_emit_load_aotconst (buf, code, &ji, MONO_PATCH_INFO_JIT_ICALL_ADDR, "mono_thread_force_interruption_checkpoint");
502         } else {
503                 amd64_mov_reg_imm (code, AMD64_R11, (guint8*)mono_thread_force_interruption_checkpoint);
504         }
505         amd64_call_reg (code, AMD64_R11);
506
507         amd64_mov_reg_membase (code, AMD64_RAX, AMD64_RBP, res_offset, 8);      
508
509         /* Restore LMF */
510
511         amd64_mov_reg_membase (code, AMD64_RCX, AMD64_RBP, lmf_offset + G_STRUCT_OFFSET (MonoLMF, previous_lmf), 8);
512         amd64_alu_reg_imm_size (code, X86_SUB, AMD64_RCX, 1, 8);
513         amd64_mov_reg_membase (code, AMD64_R11, AMD64_RBP, lmf_offset + G_STRUCT_OFFSET (MonoLMF, lmf_addr), 8);
514         amd64_mov_membase_reg (code, AMD64_R11, 0, AMD64_RCX, 8);
515
516         /* 
517          * Save rax to the stack, after the leave instruction, this will become part of
518          * the red zone.
519          */
520         amd64_mov_membase_reg (code, AMD64_RBP, rax_offset, AMD64_RAX, 8);
521
522         /* Restore argument registers, r10 (imt method/rgxtx)
523            and rax (needed for direct calls to C vararg functions). */
524         for (i = 0; i < AMD64_NREG; ++i)
525                 if (AMD64_IS_ARGUMENT_REG (i) || i == AMD64_R10 || i == AMD64_RAX)
526                         amd64_mov_reg_membase (code, i, AMD64_RBP, saved_regs_offset + (i * 8), 8);
527
528         for (i = 0; i < 8; ++i)
529                 amd64_movsd_reg_membase (code, i, AMD64_RBP, saved_fpregs_offset + (i * 8));
530
531         /* Restore stack */
532         amd64_leave (code);
533
534         if (MONO_TRAMPOLINE_TYPE_MUST_RETURN (tramp_type)) {
535                 /* Load result */
536                 amd64_mov_reg_membase (code, AMD64_RAX, AMD64_RSP, rax_offset - 0x8, 8);
537                 amd64_ret (code);
538         } else {
539                 /* call the compiled method using the saved rax */
540                 amd64_jump_membase (code, AMD64_RSP, rax_offset - 0x8);
541         }
542
543         g_assert ((code - buf) <= buf_len);
544
545         mono_arch_flush_icache (buf, code - buf);
546
547         if (tramp_type == MONO_TRAMPOLINE_CLASS_INIT) {
548                 /* Initialize the nullified class init trampoline used in the AOT case */
549                 nullified_class_init_trampoline = mono_arch_get_nullified_class_init_trampoline (NULL);
550         }
551
552         if (info)
553                 *info = mono_tramp_info_create (mono_get_generic_trampoline_name (tramp_type), buf, code - buf, ji, unwind_ops);
554
555         return buf;
556 }
557
558 gpointer
559 mono_arch_get_nullified_class_init_trampoline (MonoTrampInfo **info)
560 {
561         guint8 *code, *buf;
562
563         code = buf = mono_global_codeman_reserve (16);
564         amd64_ret (code);
565
566         mono_arch_flush_icache (buf, code - buf);
567
568         if (info)
569                 *info = mono_tramp_info_create (g_strdup_printf ("nullified_class_init_trampoline"), buf, code - buf, NULL, NULL);
570
571         return buf;
572 }
573
574 gpointer
575 mono_arch_create_specific_trampoline (gpointer arg1, MonoTrampolineType tramp_type, MonoDomain *domain, guint32 *code_len)
576 {
577         guint8 *code, *buf, *tramp;
578         int size;
579
580         tramp = mono_get_trampoline_code (tramp_type);
581
582         if ((((guint64)arg1) >> 32) == 0)
583                 size = 5 + 1 + 4;
584         else
585                 size = 5 + 1 + 8;
586
587         code = buf = mono_domain_code_reserve_align (domain, size, 1);
588
589         amd64_call_code (code, tramp);
590         /* The trampoline code will obtain the argument from the instruction stream */
591         if ((((guint64)arg1) >> 32) == 0) {
592                 *code = 0x4;
593                 *(guint32*)(code + 1) = (gint64)arg1;
594                 code += 5;
595         } else {
596                 *code = 0x8;
597                 *(guint64*)(code + 1) = (gint64)arg1;
598                 code += 9;
599         }
600
601         g_assert ((code - buf) <= size);
602
603         if (code_len)
604                 *code_len = size;
605
606         mono_arch_flush_icache (buf, size);
607
608         return buf;
609 }       
610
611 gpointer
612 mono_arch_create_rgctx_lazy_fetch_trampoline (guint32 slot, MonoTrampInfo **info, gboolean aot)
613 {
614         guint8 *tramp;
615         guint8 *code, *buf;
616         guint8 **rgctx_null_jumps;
617         int tramp_size;
618         int depth, index;
619         int i;
620         gboolean mrgctx;
621         MonoJumpInfo *ji = NULL;
622         GSList *unwind_ops = NULL;
623
624         mrgctx = MONO_RGCTX_SLOT_IS_MRGCTX (slot);
625         index = MONO_RGCTX_SLOT_INDEX (slot);
626         if (mrgctx)
627                 index += MONO_SIZEOF_METHOD_RUNTIME_GENERIC_CONTEXT / sizeof (gpointer);
628         for (depth = 0; ; ++depth) {
629                 int size = mono_class_rgctx_get_array_size (depth, mrgctx);
630
631                 if (index < size - 1)
632                         break;
633                 index -= size - 1;
634         }
635
636         tramp_size = 64 + 8 * depth;
637
638         code = buf = mono_global_codeman_reserve (tramp_size);
639
640         unwind_ops = mono_arch_get_cie_program ();
641
642         rgctx_null_jumps = g_malloc (sizeof (guint8*) * (depth + 2));
643
644         if (mrgctx) {
645                 /* get mrgctx ptr */
646                 amd64_mov_reg_reg (code, AMD64_RAX, AMD64_ARG_REG1, 8);
647         } else {
648                 /* load rgctx ptr from vtable */
649                 amd64_mov_reg_membase (code, AMD64_RAX, AMD64_ARG_REG1, G_STRUCT_OFFSET (MonoVTable, runtime_generic_context), 8);
650                 /* is the rgctx ptr null? */
651                 amd64_test_reg_reg (code, AMD64_RAX, AMD64_RAX);
652                 /* if yes, jump to actual trampoline */
653                 rgctx_null_jumps [0] = code;
654                 amd64_branch8 (code, X86_CC_Z, -1, 1);
655         }
656
657         for (i = 0; i < depth; ++i) {
658                 /* load ptr to next array */
659                 if (mrgctx && i == 0)
660                         amd64_mov_reg_membase (code, AMD64_RAX, AMD64_RAX, MONO_SIZEOF_METHOD_RUNTIME_GENERIC_CONTEXT, 8);
661                 else
662                         amd64_mov_reg_membase (code, AMD64_RAX, AMD64_RAX, 0, 8);
663                 /* is the ptr null? */
664                 amd64_test_reg_reg (code, AMD64_RAX, AMD64_RAX);
665                 /* if yes, jump to actual trampoline */
666                 rgctx_null_jumps [i + 1] = code;
667                 amd64_branch8 (code, X86_CC_Z, -1, 1);
668         }
669
670         /* fetch slot */
671         amd64_mov_reg_membase (code, AMD64_RAX, AMD64_RAX, sizeof (gpointer) * (index + 1), 8);
672         /* is the slot null? */
673         amd64_test_reg_reg (code, AMD64_RAX, AMD64_RAX);
674         /* if yes, jump to actual trampoline */
675         rgctx_null_jumps [depth + 1] = code;
676         amd64_branch8 (code, X86_CC_Z, -1, 1);
677         /* otherwise return */
678         amd64_ret (code);
679
680         for (i = mrgctx ? 1 : 0; i <= depth + 1; ++i)
681                 x86_patch (rgctx_null_jumps [i], code);
682
683         g_free (rgctx_null_jumps);
684
685         /* move the rgctx pointer to the VTABLE register */
686         amd64_mov_reg_reg (code, MONO_ARCH_VTABLE_REG, AMD64_ARG_REG1, 8);
687
688         if (aot) {
689                 code = mono_arch_emit_load_aotconst (buf, code, &ji, MONO_PATCH_INFO_JIT_ICALL_ADDR, g_strdup_printf ("specific_trampoline_lazy_fetch_%u", slot));
690                 amd64_jump_reg (code, AMD64_R11);
691         } else {
692                 tramp = mono_arch_create_specific_trampoline (GUINT_TO_POINTER (slot), MONO_TRAMPOLINE_RGCTX_LAZY_FETCH, mono_get_root_domain (), NULL);
693
694                 /* jump to the actual trampoline */
695                 amd64_jump_code (code, tramp);
696         }
697
698         mono_arch_flush_icache (buf, code - buf);
699
700         g_assert (code - buf <= tramp_size);
701
702         if (info)
703                 *info = mono_tramp_info_create (mono_get_rgctx_fetch_trampoline_name (slot), buf, code - buf, ji, unwind_ops);
704
705         return buf;
706 }
707
708 gpointer
709 mono_arch_create_generic_class_init_trampoline (MonoTrampInfo **info, gboolean aot)
710 {
711         guint8 *tramp;
712         guint8 *code, *buf;
713         static int byte_offset = -1;
714         static guint8 bitmask;
715         guint8 *jump;
716         int tramp_size;
717         GSList *unwind_ops = NULL;
718         MonoJumpInfo *ji = NULL;
719
720         tramp_size = 64;
721
722         code = buf = mono_global_codeman_reserve (tramp_size);
723
724         if (byte_offset < 0)
725                 mono_marshal_find_bitfield_offset (MonoVTable, initialized, &byte_offset, &bitmask);
726
727         amd64_test_membase_imm_size (code, MONO_AMD64_ARG_REG1, byte_offset, bitmask, 1);
728         jump = code;
729         amd64_branch8 (code, X86_CC_Z, -1, 1);
730
731         amd64_ret (code);
732
733         x86_patch (jump, code);
734
735         if (aot) {
736                 code = mono_arch_emit_load_aotconst (buf, code, &ji, MONO_PATCH_INFO_JIT_ICALL_ADDR, "specific_trampoline_generic_class_init");
737                 amd64_jump_reg (code, AMD64_R11);
738         } else {
739                 tramp = mono_arch_create_specific_trampoline (NULL, MONO_TRAMPOLINE_GENERIC_CLASS_INIT, mono_get_root_domain (), NULL);
740
741                 /* jump to the actual trampoline */
742                 amd64_jump_code (code, tramp);
743         }
744
745         mono_arch_flush_icache (buf, code - buf);
746
747         g_assert (code - buf <= tramp_size);
748
749         if (info)
750                 *info = mono_tramp_info_create (g_strdup_printf ("generic_class_init_trampoline"), buf, code - buf, ji, unwind_ops);
751
752         return buf;
753 }
754
755 #ifdef MONO_ARCH_MONITOR_OBJECT_REG
756
757 gpointer
758 mono_arch_create_monitor_enter_trampoline (MonoTrampInfo **info, gboolean aot)
759 {
760         guint8 *tramp;
761         guint8 *code, *buf;
762         guint8 *jump_obj_null, *jump_sync_null, *jump_cmpxchg_failed, *jump_other_owner, *jump_tid;
763         int tramp_size;
764         int owner_offset, nest_offset, dummy;
765         MonoJumpInfo *ji = NULL;
766         GSList *unwind_ops = NULL;
767
768         g_assert (MONO_ARCH_MONITOR_OBJECT_REG == AMD64_RDI);
769
770         mono_monitor_threads_sync_members_offset (&owner_offset, &nest_offset, &dummy);
771         g_assert (MONO_THREADS_SYNC_MEMBER_SIZE (owner_offset) == sizeof (gpointer));
772         g_assert (MONO_THREADS_SYNC_MEMBER_SIZE (nest_offset) == sizeof (guint32));
773         owner_offset = MONO_THREADS_SYNC_MEMBER_OFFSET (owner_offset);
774         nest_offset = MONO_THREADS_SYNC_MEMBER_OFFSET (nest_offset);
775
776         tramp_size = 96;
777
778         code = buf = mono_global_codeman_reserve (tramp_size);
779
780         if (mono_thread_get_tls_offset () != -1) {
781                 /* MonoObject* obj is in RDI */
782                 /* is obj null? */
783                 amd64_test_reg_reg (code, AMD64_RDI, AMD64_RDI);
784                 /* if yes, jump to actual trampoline */
785                 jump_obj_null = code;
786                 amd64_branch8 (code, X86_CC_Z, -1, 1);
787
788                 /* load obj->synchronization to RCX */
789                 amd64_mov_reg_membase (code, AMD64_RCX, AMD64_RDI, G_STRUCT_OFFSET (MonoObject, synchronisation), 8);
790                 /* is synchronization null? */
791                 amd64_test_reg_reg (code, AMD64_RCX, AMD64_RCX);
792                 /* if yes, jump to actual trampoline */
793                 jump_sync_null = code;
794                 amd64_branch8 (code, X86_CC_Z, -1, 1);
795
796                 /* load MonoInternalThread* into RDX */
797                 code = mono_amd64_emit_tls_get (code, AMD64_RDX, mono_thread_get_tls_offset ());
798                 /* load TID into RDX */
799                 amd64_mov_reg_membase (code, AMD64_RDX, AMD64_RDX, G_STRUCT_OFFSET (MonoInternalThread, tid), 8);
800
801                 /* is synchronization->owner null? */
802                 amd64_alu_membase_imm_size (code, X86_CMP, AMD64_RCX, owner_offset, 0, 8);
803                 /* if not, jump to next case */
804                 jump_tid = code;
805                 amd64_branch8 (code, X86_CC_NZ, -1, 1);
806
807                 /* if yes, try a compare-exchange with the TID */
808                 /* zero RAX */
809                 amd64_alu_reg_reg (code, X86_XOR, AMD64_RAX, AMD64_RAX);
810                 /* compare and exchange */
811                 amd64_prefix (code, X86_LOCK_PREFIX);
812                 amd64_cmpxchg_membase_reg_size (code, AMD64_RCX, owner_offset, AMD64_RDX, 8);
813                 /* if not successful, jump to actual trampoline */
814                 jump_cmpxchg_failed = code;
815                 amd64_branch8 (code, X86_CC_NZ, -1, 1);
816                 /* if successful, return */
817                 amd64_ret (code);
818
819                 /* next case: synchronization->owner is not null */
820                 x86_patch (jump_tid, code);
821                 /* is synchronization->owner == TID? */
822                 amd64_alu_membase_reg_size (code, X86_CMP, AMD64_RCX, owner_offset, AMD64_RDX, 8);
823                 /* if not, jump to actual trampoline */
824                 jump_other_owner = code;
825                 amd64_branch8 (code, X86_CC_NZ, -1, 1);
826                 /* if yes, increment nest */
827                 amd64_inc_membase_size (code, AMD64_RCX, nest_offset, 4);
828                 /* return */
829                 amd64_ret (code);
830
831                 x86_patch (jump_obj_null, code);
832                 x86_patch (jump_sync_null, code);
833                 x86_patch (jump_cmpxchg_failed, code);
834                 x86_patch (jump_other_owner, code);
835         }
836
837         /* jump to the actual trampoline */
838 #if MONO_AMD64_ARG_REG1 != AMD64_RDI
839         amd64_mov_reg_reg (code, MONO_AMD64_ARG_REG1, AMD64_RDI);
840 #endif
841
842         if (aot) {
843                 code = mono_arch_emit_load_aotconst (buf, code, &ji, MONO_PATCH_INFO_JIT_ICALL_ADDR, "specific_trampoline_monitor_enter");
844                 amd64_jump_reg (code, AMD64_R11);
845         } else {
846                 tramp = mono_arch_create_specific_trampoline (NULL, MONO_TRAMPOLINE_MONITOR_ENTER, mono_get_root_domain (), NULL);
847
848                 /* jump to the actual trampoline */
849                 amd64_jump_code (code, tramp);
850         }
851
852         mono_arch_flush_icache (code, code - buf);
853         g_assert (code - buf <= tramp_size);
854
855         if (info)
856                 *info = mono_tramp_info_create (g_strdup_printf ("monitor_enter_trampoline"), buf, code - buf, ji, unwind_ops);
857
858         return buf;
859 }
860
861 gpointer
862 mono_arch_create_monitor_exit_trampoline (MonoTrampInfo **info, gboolean aot)
863 {
864         guint8 *tramp;
865         guint8 *code, *buf;
866         guint8 *jump_obj_null, *jump_have_waiters, *jump_sync_null, *jump_not_owned;
867         guint8 *jump_next;
868         int tramp_size;
869         int owner_offset, nest_offset, entry_count_offset;
870         MonoJumpInfo *ji = NULL;
871         GSList *unwind_ops = NULL;
872
873         g_assert (MONO_ARCH_MONITOR_OBJECT_REG == AMD64_RDI);
874
875         mono_monitor_threads_sync_members_offset (&owner_offset, &nest_offset, &entry_count_offset);
876         g_assert (MONO_THREADS_SYNC_MEMBER_SIZE (owner_offset) == sizeof (gpointer));
877         g_assert (MONO_THREADS_SYNC_MEMBER_SIZE (nest_offset) == sizeof (guint32));
878         g_assert (MONO_THREADS_SYNC_MEMBER_SIZE (entry_count_offset) == sizeof (gint32));
879         owner_offset = MONO_THREADS_SYNC_MEMBER_OFFSET (owner_offset);
880         nest_offset = MONO_THREADS_SYNC_MEMBER_OFFSET (nest_offset);
881         entry_count_offset = MONO_THREADS_SYNC_MEMBER_OFFSET (entry_count_offset);
882
883         tramp_size = 94;
884
885         code = buf = mono_global_codeman_reserve (tramp_size);
886
887         if (mono_thread_get_tls_offset () != -1) {
888                 /* MonoObject* obj is in RDI */
889                 /* is obj null? */
890                 amd64_test_reg_reg (code, AMD64_RDI, AMD64_RDI);
891                 /* if yes, jump to actual trampoline */
892                 jump_obj_null = code;
893                 amd64_branch8 (code, X86_CC_Z, -1, 1);
894
895                 /* load obj->synchronization to RCX */
896                 amd64_mov_reg_membase (code, AMD64_RCX, AMD64_RDI, G_STRUCT_OFFSET (MonoObject, synchronisation), 8);
897                 /* is synchronization null? */
898                 amd64_test_reg_reg (code, AMD64_RCX, AMD64_RCX);
899                 /* if yes, jump to actual trampoline */
900                 jump_sync_null = code;
901                 amd64_branch8 (code, X86_CC_Z, -1, 1);
902
903                 /* next case: synchronization is not null */
904                 /* load MonoInternalThread* into RDX */
905                 code = mono_amd64_emit_tls_get (code, AMD64_RDX, mono_thread_get_tls_offset ());
906                 /* load TID into RDX */
907                 amd64_mov_reg_membase (code, AMD64_RDX, AMD64_RDX, G_STRUCT_OFFSET (MonoInternalThread, tid), 8);
908                 /* is synchronization->owner == TID */
909                 amd64_alu_membase_reg_size (code, X86_CMP, AMD64_RCX, owner_offset, AMD64_RDX, 8);
910                 /* if no, jump to actual trampoline */
911                 jump_not_owned = code;
912                 amd64_branch8 (code, X86_CC_NZ, -1, 1);
913
914                 /* next case: synchronization->owner == TID */
915                 /* is synchronization->nest == 1 */
916                 amd64_alu_membase_imm_size (code, X86_CMP, AMD64_RCX, nest_offset, 1, 4);
917                 /* if not, jump to next case */
918                 jump_next = code;
919                 amd64_branch8 (code, X86_CC_NZ, -1, 1);
920                 /* if yes, is synchronization->entry_count zero? */
921                 amd64_alu_membase_imm_size (code, X86_CMP, AMD64_RCX, entry_count_offset, 0, 4);
922                 /* if not, jump to actual trampoline */
923                 jump_have_waiters = code;
924                 amd64_branch8 (code, X86_CC_NZ, -1 , 1);
925                 /* if yes, set synchronization->owner to null and return */
926                 amd64_mov_membase_imm (code, AMD64_RCX, owner_offset, 0, 8);
927                 amd64_ret (code);
928
929                 /* next case: synchronization->nest is not 1 */
930                 x86_patch (jump_next, code);
931                 /* decrease synchronization->nest and return */
932                 amd64_dec_membase_size (code, AMD64_RCX, nest_offset, 4);
933                 amd64_ret (code);
934
935                 x86_patch (jump_obj_null, code);
936                 x86_patch (jump_have_waiters, code);
937                 x86_patch (jump_not_owned, code);
938                 x86_patch (jump_sync_null, code);
939         }
940
941         /* jump to the actual trampoline */
942 #if MONO_AMD64_ARG_REG1 != AMD64_RDI
943         amd64_mov_reg_reg (code, MONO_AMD64_ARG_REG1, AMD64_RDI);
944 #endif
945
946         if (aot) {
947                 code = mono_arch_emit_load_aotconst (buf, code, &ji, MONO_PATCH_INFO_JIT_ICALL_ADDR, "specific_trampoline_monitor_exit");
948                 amd64_jump_reg (code, AMD64_R11);
949         } else {
950                 tramp = mono_arch_create_specific_trampoline (NULL, MONO_TRAMPOLINE_MONITOR_EXIT, mono_get_root_domain (), NULL);
951                 amd64_jump_code (code, tramp);
952         }
953
954         mono_arch_flush_icache (code, code - buf);
955         g_assert (code - buf <= tramp_size);
956
957         if (info)
958                 *info = mono_tramp_info_create (g_strdup_printf ("monitor_exit_trampoline"), buf, code - buf, ji, unwind_ops);
959
960         return buf;
961 }
962 #endif
963
964 void
965 mono_arch_invalidate_method (MonoJitInfo *ji, void *func, gpointer func_arg)
966 {
967         /* FIXME: This is not thread safe */
968         guint8 *code = ji->code_start;
969
970         amd64_mov_reg_imm (code, AMD64_ARG_REG1, func_arg);
971         amd64_mov_reg_imm (code, AMD64_R11, func);
972
973         x86_push_imm (code, (guint64)func_arg);
974         amd64_call_reg (code, AMD64_R11);
975 }
976
977 /*
978  * mono_arch_get_call_target:
979  *
980  *   Return the address called by the code before CODE if exists.
981  */
982 guint8*
983 mono_arch_get_call_target (guint8 *code)
984 {
985         if (code [-5] == 0xe8) {
986                 guint32 disp = *(guint32*)(code - 4);
987                 guint8 *target = code + disp;
988
989                 return target;
990         } else {
991                 return NULL;
992         }
993 }
994
995 /*
996  * mono_arch_get_plt_info_offset:
997  *
998  *   Return the PLT info offset belonging to the plt entry PLT_ENTRY.
999  */
1000 guint32
1001 mono_arch_get_plt_info_offset (guint8 *plt_entry, mgreg_t *regs, guint8 *code)
1002 {
1003         return *(guint32*)(plt_entry + 6);
1004 }