Moved ProviderCollectionTest.cs from System assembly to System.Configuration.
[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/arch/amd64/amd64-codegen.h>
19
20 #ifdef HAVE_VALGRIND_MEMCHECK_H
21 #include <valgrind/memcheck.h>
22 #endif
23
24 #include "mini.h"
25 #include "mini-amd64.h"
26
27 #define IS_REX(inst) (((inst) >= 0x40) && ((inst) <= 0x4f))
28
29 static guint8* nullified_class_init_trampoline;
30
31 /*
32  * mono_arch_get_unbox_trampoline:
33  * @gsctx: the generic sharing context
34  * @m: method pointer
35  * @addr: pointer to native code for @m
36  *
37  * when value type methods are called through the vtable we need to unbox the
38  * this argument. This method returns a pointer to a trampoline which does
39  * unboxing before calling the method
40  */
41 gpointer
42 mono_arch_get_unbox_trampoline (MonoGenericSharingContext *gsctx, MonoMethod *m, gpointer addr)
43 {
44         guint8 *code, *start;
45         int this_reg;
46
47         MonoDomain *domain = mono_domain_get ();
48
49         this_reg = mono_arch_get_this_arg_reg (mono_method_signature (m), gsctx, NULL);
50
51         mono_domain_lock (domain);
52         start = code = mono_code_manager_reserve (domain->code_mp, 20);
53         mono_domain_unlock (domain);
54
55         amd64_alu_reg_imm (code, X86_ADD, this_reg, sizeof (MonoObject));
56         /* FIXME: Optimize this */
57         amd64_mov_reg_imm (code, AMD64_RAX, addr);
58         amd64_jump_reg (code, AMD64_RAX);
59         g_assert ((code - start) < 20);
60
61         mono_arch_flush_icache (start, code - start);
62
63         return start;
64 }
65
66 /*
67  * mono_arch_patch_callsite:
68  *
69  *   Patch the callsite whose address is given by ORIG_CODE so it calls ADDR. ORIG_CODE
70  * points to the pc right after the call.
71  */
72 void
73 mono_arch_patch_callsite (guint8 *method_start, guint8 *orig_code, guint8 *addr)
74 {
75         guint8 *code;
76         guint8 buf [16];
77         gboolean can_write = mono_breakpoint_clean_code (method_start, orig_code, 14, buf, sizeof (buf));
78
79         code = buf + 14;
80
81         if (((code [-13] == 0x49) && (code [-12] == 0xbb)) || (code [-5] == 0xe8)) {
82                 if (code [-5] != 0xe8) {
83                         if (can_write) {
84                                 InterlockedExchangePointer ((gpointer*)(orig_code - 11), addr);
85 #ifdef HAVE_VALGRIND_MEMCHECK_H
86                                 VALGRIND_DISCARD_TRANSLATIONS (orig_code - 11, sizeof (gpointer));
87 #endif
88                         }
89                 } else {
90                         if ((((guint64)(addr)) >> 32) != 0) {
91                                 /* Print some diagnostics */
92                                 MonoJitInfo *ji = mono_jit_info_table_find (mono_domain_get (), (char*)orig_code);
93                                 if (ji)
94                                         fprintf (stderr, "At %s, offset 0x%zx\n", mono_method_full_name (ji->method, TRUE), (guint8*)orig_code - (guint8*)ji->code_start);
95                                 fprintf (stderr, "Addr: %p\n", addr);
96                                 ji = mono_jit_info_table_find (mono_domain_get (), (char*)addr);
97                                 if (ji)
98                                         fprintf (stderr, "Callee: %s\n", mono_method_full_name (ji->method, TRUE));
99                                 g_assert_not_reached ();
100                         }
101                         g_assert ((((guint64)(orig_code)) >> 32) == 0);
102                         if (can_write) {
103                                 InterlockedExchange ((gint32*)(orig_code - 4), ((gint64)addr - (gint64)orig_code));
104 #ifdef HAVE_VALGRIND_MEMCHECK_H
105                                 VALGRIND_DISCARD_TRANSLATIONS (orig_code - 5, 4);
106 #endif
107                         }
108                 }
109         }
110         else if ((code [-7] == 0x41) && (code [-6] == 0xff) && (code [-5] == 0x15)) {
111                 /* call *<OFFSET>(%rip) */
112                 gpointer *got_entry = (gpointer*)((guint8*)orig_code + (*(guint32*)(orig_code - 4)));
113                 if (can_write) {
114                         InterlockedExchangePointer (got_entry, addr);
115 #ifdef HAVE_VALGRIND_MEMCHECK_H
116                         VALGRIND_DISCARD_TRANSLATIONS (orig_code - 5, sizeof (gpointer));
117 #endif
118                 }
119         }
120 }
121
122 void
123 mono_arch_patch_plt_entry (guint8 *code, guint8 *addr)
124 {
125         gint32 disp;
126         gpointer *plt_jump_table_entry;
127
128         /* A PLT entry: jmp *<DISP>(%rip) */
129         g_assert (code [0] == 0xff);
130         g_assert (code [1] == 0x25);
131
132         disp = *(gint32*)(code + 2);
133
134         plt_jump_table_entry = (gpointer*)(code + 6 + disp);
135
136         InterlockedExchangePointer (plt_jump_table_entry, addr);
137 }
138
139 void
140 mono_arch_nullify_class_init_trampoline (guint8 *code, gssize *regs)
141 {
142         guint8 buf [16];
143         gboolean can_write = mono_breakpoint_clean_code (NULL, code, 7, buf, sizeof (buf));
144
145         if (!can_write)
146                 return;
147
148         code -= 3;
149
150         /* 
151          * A given byte sequence can match more than case here, so we have to be
152          * really careful about the ordering of the cases. Longer sequences
153          * come first.
154          */
155         if ((code [-4] == 0x41) && (code [-3] == 0xff) && (code [-2] == 0x15)) {
156                 gpointer *vtable_slot;
157
158                 /* call *<OFFSET>(%rip) */
159                 vtable_slot = mono_arch_get_vcall_slot_addr (code + 3, (gpointer*)regs);
160                 g_assert (vtable_slot);
161
162                 *vtable_slot = nullified_class_init_trampoline;
163         } else if (code [-2] == 0xe8) {
164                 /* call <TARGET> */
165                 guint8 *buf = code - 2;
166
167                 buf [0] = 0x66;
168                 buf [1] = 0x66;
169                 buf [2] = 0x90;
170                 buf [3] = 0x66;
171                 buf [4] = 0x90;
172         } else if ((code [0] == 0x41) && (code [1] == 0xff)) {
173                 /* call <REG> */
174                 /* happens on machines without MAP_32BIT like freebsd */
175                 /* amd64_set_reg_template is 10 bytes long */
176                 guint8* buf = code - 10;
177
178                 /* FIXME: Make this thread safe */
179                 /* Padding code suggested by the AMD64 Opt Manual */
180                 buf [0] = 0x66;
181                 buf [1] = 0x66;
182                 buf [2] = 0x66;
183                 buf [3] = 0x90;
184                 buf [4] = 0x66;
185                 buf [5] = 0x66;
186                 buf [6] = 0x66;
187                 buf [7] = 0x90;
188                 buf [8] = 0x66;
189                 buf [9] = 0x66;
190                 buf [10] = 0x90;
191                 buf [11] = 0x66;
192                 buf [12] = 0x90;
193         } else if (code [0] == 0x90 || code [0] == 0xeb || code [0] == 0x66) {
194                 /* Already changed by another thread */
195                 ;
196         } else {
197                 printf ("Invalid trampoline sequence: %x %x %x %x %x %x %x\n", code [0], code [1], code [2], code [3],
198                         code [4], code [5], code [6]);
199                 g_assert_not_reached ();
200         }
201 }
202
203 void
204 mono_arch_nullify_plt_entry (guint8 *code)
205 {
206         if (mono_aot_only && !nullified_class_init_trampoline)
207                 nullified_class_init_trampoline = mono_aot_get_named_code ("nullified_class_init_trampoline");
208
209         mono_arch_patch_plt_entry (code, nullified_class_init_trampoline);
210 }
211
212 guchar*
213 mono_arch_create_trampoline_code (MonoTrampolineType tramp_type)
214 {
215         MonoJumpInfo *ji;
216         guint32 code_size;
217
218         return mono_arch_create_trampoline_code_full (tramp_type, &code_size, &ji, FALSE);
219 }
220
221 guchar*
222 mono_arch_create_trampoline_code_full (MonoTrampolineType tramp_type, guint32 *code_size, MonoJumpInfo **ji, gboolean aot)
223 {
224         guint8 *buf, *code, *tramp, *br [2], *r11_save_code, *after_r11_save_code;
225         int i, lmf_offset, offset, res_offset, arg_offset, tramp_offset, saved_regs_offset;
226         int saved_fpregs_offset, rbp_offset, framesize, orig_rsp_to_rbp_offset;
227         gboolean has_caller;
228
229         if (tramp_type == MONO_TRAMPOLINE_JUMP)
230                 has_caller = FALSE;
231         else
232                 has_caller = TRUE;
233
234         code = buf = mono_global_codeman_reserve (524);
235
236         *ji = NULL;
237
238         framesize = 524 + sizeof (MonoLMF);
239         framesize = (framesize + (MONO_ARCH_FRAME_ALIGNMENT - 1)) & ~ (MONO_ARCH_FRAME_ALIGNMENT - 1);
240
241         orig_rsp_to_rbp_offset = 0;
242         r11_save_code = code;
243         /* Reserve 5 bytes for the mov_membase_reg to save R11 */
244         code += 5;
245         after_r11_save_code = code;
246
247         /* Pop the return address off the stack */
248         amd64_pop_reg (code, AMD64_R11);
249         orig_rsp_to_rbp_offset += 8;
250
251         /* 
252          * Allocate a new stack frame
253          */
254         amd64_push_reg (code, AMD64_RBP);
255         orig_rsp_to_rbp_offset -= 8;
256         amd64_mov_reg_reg (code, AMD64_RBP, AMD64_RSP, 8);
257         amd64_alu_reg_imm (code, X86_SUB, AMD64_RSP, framesize);
258
259         offset = 0;
260         rbp_offset = - offset;
261
262         offset += 8;
263         tramp_offset = - offset;
264
265         offset += 8;
266         arg_offset = - offset;
267
268         /* Compute the trampoline address from the return address */
269         if (aot) {
270                 /* 7 = length of call *<offset>(rip) */
271                 amd64_alu_reg_imm (code, X86_SUB, AMD64_R11, 7);
272         } else {
273                 /* 5 = length of amd64_call_membase () */
274                 amd64_alu_reg_imm (code, X86_SUB, AMD64_R11, 5);
275         }
276         amd64_mov_membase_reg (code, AMD64_RBP, tramp_offset, AMD64_R11, 8);
277
278         offset += 8;
279         res_offset = - offset;
280
281         /* Save all registers */
282
283         offset += AMD64_NREG * 8;
284         saved_regs_offset = - offset;
285         for (i = 0; i < AMD64_NREG; ++i) {
286                 if (i == AMD64_RBP) {
287                         /* RAX is already saved */
288                         amd64_mov_reg_membase (code, AMD64_RAX, AMD64_RBP, rbp_offset, 8);
289                         amd64_mov_membase_reg (code, AMD64_RBP, saved_regs_offset + (i * 8), AMD64_RAX, 8);
290                 } else if (i != AMD64_R11) {
291                         amd64_mov_membase_reg (code, AMD64_RBP, saved_regs_offset + (i * 8), i, 8);
292                 } else {
293                         /* We have to save R11 right at the start of
294                            the trampoline code because it's used as a
295                            scratch register */
296                         amd64_mov_membase_reg (r11_save_code, AMD64_RSP, saved_regs_offset + orig_rsp_to_rbp_offset + (i * 8), i, 8);
297                         g_assert (r11_save_code == after_r11_save_code);
298                 }
299         }
300         offset += 8 * 8;
301         saved_fpregs_offset = - offset;
302         for (i = 0; i < 8; ++i)
303                 amd64_movsd_membase_reg (code, AMD64_RBP, saved_fpregs_offset + (i * 8), i);
304
305         if (tramp_type != MONO_TRAMPOLINE_GENERIC_CLASS_INIT) {
306                 /* Obtain the trampoline argument which is encoded in the instruction stream */
307                 if (aot) {
308                         /* Load the GOT offset */
309                         amd64_mov_reg_membase (code, AMD64_R11, AMD64_RBP, tramp_offset, 8);
310                         amd64_mov_reg_membase (code, AMD64_RAX, AMD64_R11, 7, 4);
311                         /* Compute the address of the GOT slot */
312                         amd64_alu_reg_reg_size (code, X86_ADD, AMD64_R11, AMD64_RAX, 8);
313                         /* Load the value */
314                         amd64_mov_reg_membase (code, AMD64_R11, AMD64_R11, 0, 8);
315                 } else {                        
316                         amd64_mov_reg_membase (code, AMD64_R11, AMD64_RBP, tramp_offset, 8);
317                         amd64_mov_reg_membase (code, AMD64_RAX, AMD64_R11, 5, 1);
318                         amd64_widen_reg (code, AMD64_RAX, AMD64_RAX, TRUE, FALSE);
319                         amd64_alu_reg_imm_size (code, X86_CMP, AMD64_RAX, 4, 1);
320                         br [0] = code;
321                         x86_branch8 (code, X86_CC_NE, 6, FALSE);
322                         /* 32 bit immediate */
323                         amd64_mov_reg_membase (code, AMD64_R11, AMD64_R11, 6, 4);
324                         br [1] = code;
325                         x86_jump8 (code, 10);
326                         /* 64 bit immediate */
327                         mono_amd64_patch (br [0], code);
328                         amd64_mov_reg_membase (code, AMD64_R11, AMD64_R11, 6, 8);
329                         mono_amd64_patch (br [1], code);
330                 }
331                 amd64_mov_membase_reg (code, AMD64_RBP, arg_offset, AMD64_R11, 8);
332         } else {
333                 amd64_mov_reg_membase (code, AMD64_R11, AMD64_RBP, saved_regs_offset + (MONO_AMD64_ARG_REG1 * 8), 8);
334                 amd64_mov_membase_reg (code, AMD64_RBP, arg_offset, AMD64_R11, 8);
335         }
336
337         /* Save LMF begin */
338
339         offset += sizeof (MonoLMF);
340         lmf_offset = - offset;
341
342         /* Save ip */
343         if (has_caller)
344                 amd64_mov_reg_membase (code, AMD64_R11, AMD64_RBP, 8, 8);
345         else
346                 amd64_mov_reg_imm (code, AMD64_R11, 0);
347         amd64_mov_membase_reg (code, AMD64_RBP, lmf_offset + G_STRUCT_OFFSET (MonoLMF, rip), AMD64_R11, 8);
348         /* Save fp */
349         amd64_mov_reg_membase (code, AMD64_R11, AMD64_RSP, framesize, 8);
350         amd64_mov_membase_reg (code, AMD64_RBP, lmf_offset + G_STRUCT_OFFSET (MonoLMF, rbp), AMD64_R11, 8);
351         /* Save sp */
352         amd64_mov_reg_reg (code, AMD64_R11, AMD64_RSP, 8);
353         amd64_alu_reg_imm (code, X86_ADD, AMD64_R11, framesize + 16);
354         amd64_mov_membase_reg (code, AMD64_RBP, lmf_offset + G_STRUCT_OFFSET (MonoLMF, rsp), AMD64_R11, 8);
355         /* Save method */
356         if (tramp_type == MONO_TRAMPOLINE_JIT || tramp_type == MONO_TRAMPOLINE_JUMP) {
357                 amd64_mov_reg_membase (code, AMD64_R11, AMD64_RBP, arg_offset, 8);
358                 amd64_mov_membase_reg (code, AMD64_RBP, lmf_offset + G_STRUCT_OFFSET (MonoLMF, method), AMD64_R11, 8);
359         } else {
360                 amd64_mov_membase_imm (code, AMD64_RBP, lmf_offset + G_STRUCT_OFFSET (MonoLMF, method), 0, 8);
361         }
362         /* Save callee saved regs */
363 #ifdef PLATFORM_WIN32
364         amd64_mov_membase_reg (code, AMD64_RBP, lmf_offset + G_STRUCT_OFFSET (MonoLMF, rdi), AMD64_RDI, 8);
365         amd64_mov_membase_reg (code, AMD64_RBP, lmf_offset + G_STRUCT_OFFSET (MonoLMF, rsi), AMD64_RSI, 8);
366 #endif
367         amd64_mov_membase_reg (code, AMD64_RBP, lmf_offset + G_STRUCT_OFFSET (MonoLMF, rbx), AMD64_RBX, 8);
368         amd64_mov_membase_reg (code, AMD64_RBP, lmf_offset + G_STRUCT_OFFSET (MonoLMF, r12), AMD64_R12, 8);
369         amd64_mov_membase_reg (code, AMD64_RBP, lmf_offset + G_STRUCT_OFFSET (MonoLMF, r13), AMD64_R13, 8);
370         amd64_mov_membase_reg (code, AMD64_RBP, lmf_offset + G_STRUCT_OFFSET (MonoLMF, r14), AMD64_R14, 8);
371         amd64_mov_membase_reg (code, AMD64_RBP, lmf_offset + G_STRUCT_OFFSET (MonoLMF, r15), AMD64_R15, 8);
372
373         if (aot) {
374                 *ji = mono_patch_info_list_prepend (*ji, code - buf, MONO_PATCH_INFO_JIT_ICALL_ADDR, "mono_get_lmf_addr");
375                 amd64_mov_reg_membase (code, AMD64_R11, AMD64_RIP, 0, 8);
376         } else {
377                 amd64_mov_reg_imm (code, AMD64_R11, mono_get_lmf_addr);
378         }
379         amd64_call_reg (code, AMD64_R11);
380
381         /* Save lmf_addr */
382         amd64_mov_membase_reg (code, AMD64_RBP, lmf_offset + G_STRUCT_OFFSET (MonoLMF, lmf_addr), AMD64_RAX, 8);
383         /* Save previous_lmf */
384         /* Set the lowest bit to 1 to signal that this LMF has the ip field set */
385         amd64_mov_reg_membase (code, AMD64_R11, AMD64_RAX, 0, 8);
386         amd64_alu_reg_imm_size (code, X86_ADD, AMD64_R11, 1, 8);
387         amd64_mov_membase_reg (code, AMD64_RBP, lmf_offset + G_STRUCT_OFFSET (MonoLMF, previous_lmf), AMD64_R11, 8);
388         /* Set new lmf */
389         amd64_lea_membase (code, AMD64_R11, AMD64_RBP, lmf_offset);
390         amd64_mov_membase_reg (code, AMD64_RAX, 0, AMD64_R11, 8);
391
392         /* Save LMF end */
393
394         /* Arg1 is the pointer to the saved registers */
395         amd64_lea_membase (code, AMD64_ARG_REG1, AMD64_RBP, saved_regs_offset);
396
397         /* Arg2 is the address of the calling code */
398         if (has_caller)
399                 amd64_mov_reg_membase (code, AMD64_ARG_REG2, AMD64_RBP, 8, 8);
400         else
401                 amd64_mov_reg_imm (code, AMD64_ARG_REG2, 0);
402
403         /* Arg3 is the method/vtable ptr */
404         amd64_mov_reg_membase (code, AMD64_ARG_REG3, AMD64_RBP, arg_offset, 8);
405
406         /* Arg4 is the trampoline address */
407         amd64_mov_reg_membase (code, AMD64_ARG_REG4, AMD64_RBP, tramp_offset, 8);
408
409         if (aot) {
410                 char *icall_name = g_strdup_printf ("trampoline_func_%d", tramp_type);
411                 *ji = mono_patch_info_list_prepend (*ji, code - buf, MONO_PATCH_INFO_JIT_ICALL_ADDR, icall_name);
412                 amd64_mov_reg_membase (code, AMD64_RAX, AMD64_RIP, 0, 8);
413         } else {
414                 tramp = (guint8*)mono_get_trampoline_func (tramp_type);
415                 amd64_mov_reg_imm (code, AMD64_RAX, tramp);
416         }
417         amd64_call_reg (code, AMD64_RAX);
418
419         /* Check for thread interruption */
420         /* This is not perf critical code so no need to check the interrupt flag */
421         /* 
422          * Have to call the _force_ variant, since there could be a protected wrapper on the top of the stack.
423          */
424         amd64_mov_membase_reg (code, AMD64_RBP, res_offset, AMD64_RAX, 8);
425         if (aot) {
426                 *ji = mono_patch_info_list_prepend (*ji, code - buf, MONO_PATCH_INFO_JIT_ICALL_ADDR, "mono_thread_force_interruption_checkpoint");
427                 amd64_mov_reg_membase (code, AMD64_RAX, AMD64_RIP, 0, 8);
428         } else {
429                 amd64_mov_reg_imm (code, AMD64_RAX, (guint8*)mono_thread_force_interruption_checkpoint);
430         }
431         amd64_call_reg (code, AMD64_RAX);
432         amd64_mov_reg_membase (code, AMD64_RAX, AMD64_RBP, res_offset, 8);      
433
434         /* Restore LMF */
435
436         amd64_mov_reg_membase (code, AMD64_RCX, AMD64_RBP, lmf_offset + G_STRUCT_OFFSET (MonoLMF, previous_lmf), 8);
437         amd64_alu_reg_imm_size (code, X86_SUB, AMD64_RCX, 1, 8);
438         amd64_mov_reg_membase (code, AMD64_R11, AMD64_RBP, lmf_offset + G_STRUCT_OFFSET (MonoLMF, lmf_addr), 8);
439         amd64_mov_membase_reg (code, AMD64_R11, 0, AMD64_RCX, 8);
440
441         /* Restore argument registers, r10 (needed to pass rgctx to
442            static shared generic methods) and r11 (imt register for
443            interface calls). */
444         for (i = 0; i < AMD64_NREG; ++i)
445                 if (AMD64_IS_ARGUMENT_REG (i) || i == AMD64_R10 || i == AMD64_R11)
446                         amd64_mov_reg_membase (code, i, AMD64_RBP, saved_regs_offset + (i * 8), 8);
447
448         /* 
449          * FIXME: When using aot-only, the called code might be a C vararg function 
450          * which uses %rax as well.
451          * We could restore it, but we would have to use another register to store the
452          * target address, and we don't have any left.
453          * Also, the default AOT plt trampolines overwrite 'rax'.
454          */
455
456         for (i = 0; i < 8; ++i)
457                 amd64_movsd_reg_membase (code, i, AMD64_RBP, saved_fpregs_offset + (i * 8));
458
459         /* Restore stack */
460         amd64_leave (code);
461
462         if (tramp_type == MONO_TRAMPOLINE_CLASS_INIT ||
463                         tramp_type == MONO_TRAMPOLINE_GENERIC_CLASS_INIT ||
464                         tramp_type == MONO_TRAMPOLINE_RGCTX_LAZY_FETCH)
465                 amd64_ret (code);
466         else {
467                 /* call the compiled method */
468                 amd64_jump_reg (code, AMD64_RAX);
469         }
470
471         g_assert ((code - buf) <= 524);
472
473         mono_arch_flush_icache (buf, code - buf);
474
475         *code_size = code - buf;
476
477         if (tramp_type == MONO_TRAMPOLINE_CLASS_INIT) {
478                 guint32 code_len;
479
480                 /* Initialize the nullified class init trampoline used in the AOT case */
481                 nullified_class_init_trampoline = mono_arch_get_nullified_class_init_trampoline (&code_len);
482         }
483
484         return buf;
485 }
486
487 gpointer
488 mono_arch_get_nullified_class_init_trampoline (guint32 *code_len)
489 {
490         guint8 *code, *buf;
491
492         code = buf = mono_global_codeman_reserve (16);
493         amd64_ret (code);
494
495         mono_arch_flush_icache (buf, code - buf);
496
497         *code_len = code - buf;
498
499         return buf;
500 }
501
502 gpointer
503 mono_arch_create_specific_trampoline (gpointer arg1, MonoTrampolineType tramp_type, MonoDomain *domain, guint32 *code_len)
504 {
505         guint8 *code, *buf, *tramp;
506         int size;
507
508         tramp = mono_get_trampoline_code (tramp_type);
509
510         if ((((guint64)arg1) >> 32) == 0)
511                 size = 5 + 1 + 4;
512         else
513                 size = 5 + 1 + 8;
514
515         mono_domain_lock (domain);
516         code = buf = mono_code_manager_reserve_align (domain->code_mp, size, 1);
517         mono_domain_unlock (domain);
518
519         amd64_call_code (code, tramp);
520         /* The trampoline code will obtain the argument from the instruction stream */
521         if ((((guint64)arg1) >> 32) == 0) {
522                 *code = 0x4;
523                 *(guint32*)(code + 1) = (gint64)arg1;
524                 code += 5;
525         } else {
526                 *code = 0x8;
527                 *(guint64*)(code + 1) = (gint64)arg1;
528                 code += 9;
529         }
530
531         g_assert ((code - buf) <= size);
532
533         if (code_len)
534                 *code_len = size;
535
536         mono_arch_flush_icache (buf, size);
537
538         return buf;
539 }       
540
541 gpointer
542 mono_arch_create_rgctx_lazy_fetch_trampoline (guint32 slot)
543 {
544         guint32 code_size;
545         MonoJumpInfo *ji;
546
547         return mono_arch_create_rgctx_lazy_fetch_trampoline_full (slot, &code_size, &ji, FALSE);
548 }
549
550 gpointer
551 mono_arch_create_rgctx_lazy_fetch_trampoline_full (guint32 slot, guint32 *code_size, MonoJumpInfo **ji, gboolean aot)
552 {
553         guint8 *tramp;
554         guint8 *code, *buf;
555         guint8 **rgctx_null_jumps;
556         int tramp_size;
557         int depth, index;
558         int i;
559         gboolean mrgctx;
560
561         *ji = NULL;
562
563         mrgctx = MONO_RGCTX_SLOT_IS_MRGCTX (slot);
564         index = MONO_RGCTX_SLOT_INDEX (slot);
565         if (mrgctx)
566                 index += sizeof (MonoMethodRuntimeGenericContext) / sizeof (gpointer);
567         for (depth = 0; ; ++depth) {
568                 int size = mono_class_rgctx_get_array_size (depth, mrgctx);
569
570                 if (index < size - 1)
571                         break;
572                 index -= size - 1;
573         }
574
575         tramp_size = 36 + 8 * depth;
576
577         code = buf = mono_global_codeman_reserve (tramp_size);
578
579         rgctx_null_jumps = g_malloc (sizeof (guint8*) * (depth + 2));
580
581         if (mrgctx) {
582                 /* get mrgctx ptr */
583                 amd64_mov_reg_reg (code, AMD64_RAX, AMD64_ARG_REG1, 8);
584         } else {
585                 /* load rgctx ptr from vtable */
586                 amd64_mov_reg_membase (code, AMD64_RAX, AMD64_ARG_REG1, G_STRUCT_OFFSET (MonoVTable, runtime_generic_context), 8);
587                 /* is the rgctx ptr null? */
588                 amd64_test_reg_reg (code, AMD64_RAX, AMD64_RAX);
589                 /* if yes, jump to actual trampoline */
590                 rgctx_null_jumps [0] = code;
591                 amd64_branch8 (code, X86_CC_Z, -1, 1);
592         }
593
594         for (i = 0; i < depth; ++i) {
595                 /* load ptr to next array */
596                 if (mrgctx && i == 0)
597                         amd64_mov_reg_membase (code, AMD64_RAX, AMD64_RAX, sizeof (MonoMethodRuntimeGenericContext), 8);
598                 else
599                         amd64_mov_reg_membase (code, AMD64_RAX, AMD64_RAX, 0, 8);
600                 /* is the ptr null? */
601                 amd64_test_reg_reg (code, AMD64_RAX, AMD64_RAX);
602                 /* if yes, jump to actual trampoline */
603                 rgctx_null_jumps [i + 1] = code;
604                 amd64_branch8 (code, X86_CC_Z, -1, 1);
605         }
606
607         /* fetch slot */
608         amd64_mov_reg_membase (code, AMD64_RAX, AMD64_RAX, sizeof (gpointer) * (index + 1), 8);
609         /* is the slot null? */
610         amd64_test_reg_reg (code, AMD64_RAX, AMD64_RAX);
611         /* if yes, jump to actual trampoline */
612         rgctx_null_jumps [depth + 1] = code;
613         amd64_branch8 (code, X86_CC_Z, -1, 1);
614         /* otherwise return */
615         amd64_ret (code);
616
617         for (i = mrgctx ? 1 : 0; i <= depth + 1; ++i)
618                 x86_patch (rgctx_null_jumps [i], code);
619
620         g_free (rgctx_null_jumps);
621
622         /* move the rgctx pointer to the VTABLE register */
623         amd64_mov_reg_reg (code, MONO_ARCH_VTABLE_REG, AMD64_ARG_REG1, 8);
624
625         if (aot) {
626                 *ji = mono_patch_info_list_prepend (*ji, code - buf, MONO_PATCH_INFO_JIT_ICALL_ADDR, g_strdup_printf ("specific_trampoline_lazy_fetch_%u", slot));
627                 amd64_mov_reg_membase (code, AMD64_R11, AMD64_RIP, 0, 8);
628                 amd64_jump_reg (code, AMD64_R11);
629         } else {
630                 tramp = mono_arch_create_specific_trampoline (GUINT_TO_POINTER (slot), MONO_TRAMPOLINE_RGCTX_LAZY_FETCH, mono_get_root_domain (), NULL);
631
632                 /* jump to the actual trampoline */
633                 amd64_jump_code (code, tramp);
634         }
635
636         mono_arch_flush_icache (buf, code - buf);
637
638         g_assert (code - buf <= tramp_size);
639
640         return buf;
641 }
642
643 gpointer
644 mono_arch_create_generic_class_init_trampoline (void)
645 {
646         guint8 *tramp;
647         guint8 *code, *buf;
648         static int byte_offset = -1;
649         static guint8 bitmask;
650         guint8 *jump;
651         int tramp_size;
652
653         tramp_size = 64;
654
655         code = buf = mono_global_codeman_reserve (tramp_size);
656
657         if (byte_offset < 0)
658                 mono_marshal_find_bitfield_offset (MonoVTable, initialized, &byte_offset, &bitmask);
659
660         amd64_test_membase_imm_size (code, MONO_AMD64_ARG_REG1, byte_offset, bitmask, 1);
661         jump = code;
662         amd64_branch8 (code, X86_CC_Z, -1, 1);
663
664         amd64_ret (code);
665
666         x86_patch (jump, code);
667
668         tramp = mono_arch_create_specific_trampoline (NULL, MONO_TRAMPOLINE_GENERIC_CLASS_INIT, mono_get_root_domain (), NULL);
669
670         /* jump to the actual trampoline */
671         amd64_jump_code (code, tramp);
672
673         mono_arch_flush_icache (buf, code - buf);
674
675         g_assert (code - buf <= tramp_size);
676
677         return buf;
678 }
679
680 void
681 mono_arch_invalidate_method (MonoJitInfo *ji, void *func, gpointer func_arg)
682 {
683         /* FIXME: This is not thread safe */
684         guint8 *code = ji->code_start;
685
686         amd64_mov_reg_imm (code, AMD64_ARG_REG1, func_arg);
687         amd64_mov_reg_imm (code, AMD64_R11, func);
688
689         x86_push_imm (code, (guint64)func_arg);
690         amd64_call_reg (code, AMD64_R11);
691 }