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