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