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