[runtime] New profiler API.
[mono.git] / mono / mini / tramp-x86.c
1 /**
2  * \file
3  * JIT trampoline code for x86
4  *
5  * Authors:
6  *   Dietmar Maurer (dietmar@ximian.com)
7  *
8  * (C) 2001 Ximian, Inc.
9  */
10
11 #include <config.h>
12 #include <glib.h>
13
14 #include <mono/metadata/abi-details.h>
15 #include <mono/metadata/appdomain.h>
16 #include <mono/metadata/metadata-internals.h>
17 #include <mono/metadata/marshal.h>
18 #include <mono/metadata/tabledefs.h>
19 #include <mono/metadata/profiler-private.h>
20 #include <mono/metadata/gc-internals.h>
21 #include <mono/arch/x86/x86-codegen.h>
22
23 #include <mono/utils/memcheck.h>
24
25 #include "mini.h"
26 #include "mini-x86.h"
27 #include "debugger-agent.h"
28 #include "jit-icalls.h"
29
30 #define ALIGN_TO(val,align) ((((guint64)val) + ((align) - 1)) & ~((align) - 1))
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_pos = 4, size = 16;
46         MonoDomain *domain = mono_domain_get ();
47         GSList *unwind_ops;
48
49         start = code = mono_domain_code_reserve (domain, size);
50
51         unwind_ops = mono_arch_get_cie_program ();
52
53         x86_alu_membase_imm (code, X86_ADD, X86_ESP, this_pos, sizeof (MonoObject));
54         x86_jump_code (code, addr);
55         g_assert ((code - start) < size);
56
57         MONO_PROFILER_RAISE (jit_code_buffer, (start, code - start, MONO_PROFILER_CODE_BUFFER_UNBOX_TRAMPOLINE, m));
58
59         mono_tramp_info_register (mono_tramp_info_create (NULL, start, code - start, NULL, unwind_ops), domain);
60
61         return start;
62 }
63
64 gpointer
65 mono_arch_get_static_rgctx_trampoline (gpointer arg, gpointer addr)
66 {
67         guint8 *code, *start;
68         int buf_len;
69         GSList *unwind_ops;
70
71         MonoDomain *domain = mono_domain_get ();
72
73         buf_len = 10;
74
75         start = code = mono_domain_code_reserve (domain, buf_len);
76
77         unwind_ops = mono_arch_get_cie_program ();
78
79         x86_mov_reg_imm (code, MONO_ARCH_RGCTX_REG, arg);
80         x86_jump_code (code, addr);
81         g_assert ((code - start) <= buf_len);
82
83         mono_arch_flush_icache (start, code - start);
84         MONO_PROFILER_RAISE (jit_code_buffer, (start, code - start, MONO_PROFILER_CODE_BUFFER_GENERICS_TRAMPOLINE, NULL));
85
86         mono_tramp_info_register (mono_tramp_info_create (NULL, start, code - start, NULL, unwind_ops), domain);
87
88         return start;
89 }
90
91 void
92 mono_arch_patch_callsite (guint8 *method_start, guint8 *orig_code, guint8 *addr)
93 {
94         guint8 *code;
95         guint8 buf [8];
96         gboolean can_write = mono_breakpoint_clean_code (method_start, orig_code, 8, buf, sizeof (buf));
97
98         code = buf + 8;
99
100         /* go to the start of the call instruction
101          *
102          * address_byte = (m << 6) | (o << 3) | reg
103          * call opcode: 0xff address_byte displacement
104          * 0xff m=1,o=2 imm8
105          * 0xff m=2,o=2 imm32
106          */
107         code -= 6;
108         orig_code -= 6;
109         if (code [1] == 0xe8) {
110                 if (can_write) {
111                         InterlockedExchange ((gint32*)(orig_code + 2), (guint)addr - ((guint)orig_code + 1) - 5);
112
113                         /* Tell valgrind to recompile the patched code */
114                         VALGRIND_DISCARD_TRANSLATIONS (orig_code + 2, 4);
115                 }
116         } else if (code [1] == 0xe9) {
117                 /* A PLT entry: jmp <DISP> */
118                 if (can_write)
119                         InterlockedExchange ((gint32*)(orig_code + 2), (guint)addr - ((guint)orig_code + 1) - 5);
120         } else {
121                 printf ("Invalid trampoline sequence: %x %x %x %x %x %x %x\n", code [0], code [1], code [2], code [3],
122                                 code [4], code [5], code [6]);
123                 g_assert_not_reached ();
124         }
125 }
126
127 void
128 mono_arch_patch_plt_entry (guint8 *code, gpointer *got, mgreg_t *regs, guint8 *addr)
129 {
130         guint32 offset;
131
132         /* Patch the jump table entry used by the plt entry */
133
134         /* A PLT entry: jmp *<DISP>(%ebx) */
135         g_assert (code [0] == 0xff);
136         g_assert (code [1] == 0xa3);
137
138         offset = *(guint32*)(code + 2);
139         if (!got)
140                 got = (gpointer*)(gsize) regs [MONO_ARCH_GOT_REG];
141         *(guint8**)((guint8*)got + offset) = addr;
142 }
143
144 static gpointer
145 get_vcall_slot (guint8 *code, mgreg_t *regs, int *displacement)
146 {
147         const int kBufSize = 8;
148         guint8 buf [64];
149         guint8 reg = 0;
150         gint32 disp = 0;
151
152         mono_breakpoint_clean_code (NULL, code, kBufSize, buf, kBufSize);
153         code = buf + 8;
154
155         *displacement = 0;
156
157         if ((code [0] == 0xff) && ((code [1] & 0x18) == 0x10) && ((code [1] >> 6) == 2)) {
158                 reg = code [1] & 0x07;
159                 disp = *((gint32*)(code + 2));
160         } else {
161                 g_assert_not_reached ();
162                 return NULL;
163         }
164
165         *displacement = disp;
166         return (gpointer)regs [reg];
167 }
168
169 static gpointer*
170 get_vcall_slot_addr (guint8* code, mgreg_t *regs)
171 {
172         gpointer vt;
173         int displacement;
174         vt = get_vcall_slot (code, regs, &displacement);
175         if (!vt)
176                 return NULL;
177         return (gpointer*)((char*)vt + displacement);
178 }
179
180 guchar*
181 mono_arch_create_generic_trampoline (MonoTrampolineType tramp_type, MonoTrampInfo **info, gboolean aot)
182 {
183         char *tramp_name;
184         guint8 *buf, *code, *tramp, *br_ex_check;
185         GSList *unwind_ops = NULL;
186         MonoJumpInfo *ji = NULL;
187         int i, offset, frame_size, regarray_offset, lmf_offset, caller_ip_offset, arg_offset;
188         int cfa_offset; /* cfa = cfa_reg + cfa_offset */
189
190         code = buf = mono_global_codeman_reserve (256);
191
192         /* Note that there is a single argument to the trampoline
193          * and it is stored at: esp + pushed_args * sizeof (gpointer)
194          * the ret address is at: esp + (pushed_args + 1) * sizeof (gpointer)
195          */
196
197         /* Compute frame offsets relative to the frame pointer %ebp */
198         arg_offset = sizeof (mgreg_t);
199         caller_ip_offset = 2 * sizeof (mgreg_t);
200         offset = 0;
201         offset += sizeof (MonoLMF);
202         lmf_offset = -offset;
203         offset += X86_NREG * sizeof (mgreg_t);
204         regarray_offset = -offset;
205         /* Argument area */
206         offset += 4 * sizeof (mgreg_t);
207         frame_size = ALIGN_TO (offset, MONO_ARCH_FRAME_ALIGNMENT);
208
209         /* ret addr and arg are on the stack */
210         cfa_offset = 2 * sizeof (mgreg_t);
211         mono_add_unwind_op_def_cfa (unwind_ops, code, buf, X86_ESP, cfa_offset);
212         // IP saved at CFA - 4
213         mono_add_unwind_op_offset (unwind_ops, code, buf, X86_NREG, -4);
214
215         /* Allocate frame */
216         x86_push_reg (code, X86_EBP);
217         cfa_offset += sizeof (mgreg_t);
218         mono_add_unwind_op_def_cfa_offset (unwind_ops, code, buf, cfa_offset);
219         mono_add_unwind_op_offset (unwind_ops, code, buf, X86_EBP, -cfa_offset);
220
221         x86_mov_reg_reg (code, X86_EBP, X86_ESP, sizeof (mgreg_t));
222         mono_add_unwind_op_def_cfa_reg (unwind_ops, code, buf, X86_EBP);
223
224         /* There are three words on the stack, adding + 4 aligns the stack to 16, which is needed on osx */
225         x86_alu_reg_imm (code, X86_SUB, X86_ESP, frame_size + sizeof (mgreg_t));
226
227         /* Save all registers */
228         for (i = X86_EAX; i <= X86_EDI; ++i) {
229                 int reg = i;
230
231                 if (i == X86_EBP) {
232                         /* Save original ebp */
233                         /* EAX is already saved */
234                         x86_mov_reg_membase (code, X86_EAX, X86_EBP, 0, sizeof (mgreg_t));
235                         reg = X86_EAX;
236                 } else if (i == X86_ESP) {
237                         /* Save original esp */
238                         /* EAX is already saved */
239                         x86_mov_reg_reg (code, X86_EAX, X86_EBP, sizeof (mgreg_t));
240                         /* Saved ebp + trampoline arg + return addr */
241                         x86_alu_reg_imm (code, X86_ADD, X86_EAX, 3 * sizeof (mgreg_t));
242                         reg = X86_EAX;
243                 }
244                 x86_mov_membase_reg (code, X86_EBP, regarray_offset + (i * sizeof (mgreg_t)), reg, sizeof (mgreg_t));
245         }
246
247         /* Setup LMF */
248         /* eip */
249         if (tramp_type == MONO_TRAMPOLINE_JUMP) {
250                 x86_mov_membase_imm (code, X86_EBP, lmf_offset + G_STRUCT_OFFSET (MonoLMF, eip), 0, sizeof (mgreg_t));
251         } else {
252                 x86_mov_reg_membase (code, X86_EAX, X86_EBP, caller_ip_offset, sizeof (mgreg_t));
253                 x86_mov_membase_reg (code, X86_EBP, lmf_offset + G_STRUCT_OFFSET (MonoLMF, eip), X86_EAX, sizeof (mgreg_t));
254         }
255         /* method */
256         if ((tramp_type == MONO_TRAMPOLINE_JIT) || (tramp_type == MONO_TRAMPOLINE_JUMP)) {
257                 x86_mov_reg_membase (code, X86_EAX, X86_EBP, arg_offset, sizeof (mgreg_t));
258                 x86_mov_membase_reg (code, X86_EBP, lmf_offset + G_STRUCT_OFFSET (MonoLMF, method), X86_EAX, sizeof (mgreg_t));
259         } else {
260                 x86_mov_membase_imm (code, X86_EBP, lmf_offset + G_STRUCT_OFFSET (MonoLMF, method), 0, sizeof (mgreg_t));
261         }
262         /* esp */
263         x86_mov_reg_membase (code, X86_EAX, X86_EBP, regarray_offset + (X86_ESP * sizeof (mgreg_t)), sizeof (mgreg_t));
264         x86_mov_membase_reg (code, X86_EBP, lmf_offset + G_STRUCT_OFFSET (MonoLMF, esp), X86_EAX, sizeof (mgreg_t));
265         /* callee save registers */
266         x86_mov_reg_membase (code, X86_EAX, X86_EBP, regarray_offset + (X86_EBX * sizeof (mgreg_t)), sizeof (mgreg_t));
267         x86_mov_membase_reg (code, X86_EBP, lmf_offset + G_STRUCT_OFFSET (MonoLMF, ebx), X86_EAX, sizeof (mgreg_t));
268         x86_mov_reg_membase (code, X86_EAX, X86_EBP, regarray_offset + (X86_EDI * sizeof (mgreg_t)), sizeof (mgreg_t));
269         x86_mov_membase_reg (code, X86_EBP, lmf_offset + G_STRUCT_OFFSET (MonoLMF, edi), X86_EAX, sizeof (mgreg_t));
270         x86_mov_reg_membase (code, X86_EAX, X86_EBP, regarray_offset + (X86_ESI * sizeof (mgreg_t)), sizeof (mgreg_t));
271         x86_mov_membase_reg (code, X86_EBP, lmf_offset + G_STRUCT_OFFSET (MonoLMF, esi), X86_EAX, sizeof (mgreg_t));
272         x86_mov_reg_membase (code, X86_EAX, X86_EBP, regarray_offset + (X86_EBP * sizeof (mgreg_t)), sizeof (mgreg_t));
273         x86_mov_membase_reg (code, X86_EBP, lmf_offset + G_STRUCT_OFFSET (MonoLMF, ebp), X86_EAX, sizeof (mgreg_t));
274
275         /* Push LMF */
276         /* get the address of lmf for the current thread */
277         if (aot) {
278                 code = mono_arch_emit_load_aotconst (buf, code, &ji, MONO_PATCH_INFO_JIT_ICALL_ADDR, "mono_get_lmf_addr");
279                 x86_call_reg (code, X86_EAX);
280         } else {
281                 x86_call_code (code, mono_get_lmf_addr);
282         }
283         /* lmf->lmf_addr = lmf_addr (%eax) */
284         x86_mov_membase_reg (code, X86_EBP, lmf_offset + G_STRUCT_OFFSET (MonoLMF, lmf_addr), X86_EAX, sizeof (mgreg_t));
285         /* lmf->previous_lmf = *(lmf_addr) */
286         x86_mov_reg_membase (code, X86_ECX, X86_EAX, 0, sizeof (mgreg_t));
287         /* Signal to mono_arch_unwind_frame () that this is a trampoline frame */
288         x86_alu_reg_imm (code, X86_ADD, X86_ECX, 1);
289         x86_mov_membase_reg (code, X86_EBP, lmf_offset + G_STRUCT_OFFSET (MonoLMF, previous_lmf), X86_ECX, sizeof (mgreg_t));
290         /* *lmf_addr = lmf */
291         x86_lea_membase (code, X86_ECX, X86_EBP, lmf_offset);
292         x86_mov_membase_reg (code, X86_EAX, 0, X86_ECX, sizeof (mgreg_t));
293
294         /* Call trampoline function */
295         /* Arg 1 - registers */
296         x86_lea_membase (code, X86_EAX, X86_EBP, regarray_offset);
297         x86_mov_membase_reg (code, X86_ESP, (0 * sizeof (mgreg_t)), X86_EAX, sizeof (mgreg_t));
298         /* Arg2 - calling code */
299         if (tramp_type == MONO_TRAMPOLINE_JUMP) {
300                 x86_mov_membase_imm (code, X86_ESP, (1 * sizeof (mgreg_t)), 0, sizeof (mgreg_t));
301         } else {
302                 x86_mov_reg_membase (code, X86_EAX, X86_EBP, caller_ip_offset, sizeof (mgreg_t));
303                 x86_mov_membase_reg (code, X86_ESP, (1 * sizeof (mgreg_t)), X86_EAX, sizeof (mgreg_t));
304         }
305         /* Arg3 - trampoline argument */
306         x86_mov_reg_membase (code, X86_EAX, X86_EBP, arg_offset, sizeof (mgreg_t));
307         x86_mov_membase_reg (code, X86_ESP, (2 * sizeof (mgreg_t)), X86_EAX, sizeof (mgreg_t));
308         /* Arg4 - trampoline address */
309         // FIXME:
310         x86_mov_membase_imm (code, X86_ESP, (3 * sizeof (mgreg_t)), 0, sizeof (mgreg_t));
311
312 #ifdef __APPLE__
313         /* check the stack is aligned after the ret ip is pushed */
314         /*
315         x86_mov_reg_reg (code, X86_EDX, X86_ESP, 4);
316         x86_alu_reg_imm (code, X86_AND, X86_EDX, 15);
317         x86_alu_reg_imm (code, X86_CMP, X86_EDX, 0);
318         x86_branch_disp (code, X86_CC_Z, 3, FALSE);
319         x86_breakpoint (code);
320         */
321 #endif
322
323         if (aot) {
324                 char *icall_name = g_strdup_printf ("trampoline_func_%d", tramp_type);
325                 code = mono_arch_emit_load_aotconst (buf, code, &ji, MONO_PATCH_INFO_JIT_ICALL_ADDR, icall_name);
326                 x86_call_reg (code, X86_EAX);
327         } else {
328                 tramp = (guint8*)mono_get_trampoline_func (tramp_type);
329                 x86_call_code (code, tramp);
330         }
331
332         /*
333          * Overwrite the trampoline argument with the address we need to jump to,
334          * to free %eax.
335          */
336         x86_mov_membase_reg (code, X86_EBP, arg_offset, X86_EAX, 4);
337
338         /* Restore LMF */
339         x86_mov_reg_membase (code, X86_EAX, X86_EBP, lmf_offset + G_STRUCT_OFFSET (MonoLMF, lmf_addr), sizeof (mgreg_t));
340         x86_mov_reg_membase (code, X86_ECX, X86_EBP, lmf_offset + G_STRUCT_OFFSET (MonoLMF, previous_lmf), sizeof (mgreg_t));
341         x86_alu_reg_imm (code, X86_SUB, X86_ECX, 1);
342         x86_mov_membase_reg (code, X86_EAX, 0, X86_ECX, sizeof (mgreg_t));
343
344         /* Check for interruptions */
345         if (aot) {
346                 code = mono_arch_emit_load_aotconst (buf, code, &ji, MONO_PATCH_INFO_JIT_ICALL_ADDR, "mono_thread_force_interruption_checkpoint_noraise");
347                 x86_call_reg (code, X86_EAX);
348         } else {
349                 x86_call_code (code, (guint8*)mono_thread_force_interruption_checkpoint_noraise);
350         }
351
352         x86_test_reg_reg (code, X86_EAX, X86_EAX);
353         br_ex_check = code;
354         x86_branch8 (code, X86_CC_Z, -1, 1);
355
356         /*
357          * Exception case:
358          * We have an exception we want to throw in the caller's frame, so pop
359          * the trampoline frame and throw from the caller.
360          */
361         x86_leave (code);
362         /*
363          * The exception is in eax.
364          * We are calling the throw trampoline used by OP_THROW, so we have to setup the
365          * stack to look the same.
366          * The stack contains the ret addr, and the trampoline argument, the throw trampoline
367          * expects it to contain the ret addr and the exception. It also needs to be aligned
368          * after the exception is pushed.
369          */
370         /* Align stack */
371         x86_push_reg (code, X86_EAX);
372         /* Push the exception */
373         x86_push_reg (code, X86_EAX);
374         //x86_breakpoint (code);
375         /* Push the original return value */
376         x86_push_membase (code, X86_ESP, 3 * 4);
377         /*
378          * EH is initialized after trampolines, so get the address of the variable
379          * which contains throw_exception, and load it from there.
380          */
381         if (aot) {
382                 /* Not really a jit icall */
383                 code = mono_arch_emit_load_aotconst (buf, code, &ji, MONO_PATCH_INFO_JIT_ICALL_ADDR, "throw_exception_addr");
384         } else {
385                 x86_mov_reg_imm (code, X86_ECX, (guint8*)mono_get_throw_exception_addr ());
386         }
387         x86_mov_reg_membase (code, X86_ECX, X86_ECX, 0, sizeof(gpointer));
388         x86_jump_reg (code, X86_ECX);
389
390         /* Normal case */
391         mono_x86_patch (br_ex_check, code);
392
393         /* Restore registers */
394         for (i = X86_EAX; i <= X86_EDI; ++i) {
395                 if (i == X86_ESP || i == X86_EBP)
396                         continue;
397                 if (i == X86_EAX && !((tramp_type == MONO_TRAMPOLINE_RESTORE_STACK_PROT) || (tramp_type == MONO_TRAMPOLINE_AOT_PLT)))
398                         continue;
399                 x86_mov_reg_membase (code, i, X86_EBP, regarray_offset + (i * 4), 4);
400         }
401
402         /* Restore frame */
403         x86_leave (code);
404         cfa_offset -= sizeof (mgreg_t);
405         mono_add_unwind_op_def_cfa (unwind_ops, code, buf, X86_ESP, cfa_offset);
406         mono_add_unwind_op_same_value (unwind_ops, code, buf, X86_EBP);
407
408         if (MONO_TRAMPOLINE_TYPE_MUST_RETURN (tramp_type)) {
409                 /* Load the value returned by the trampoline */
410                 x86_mov_reg_membase (code, X86_EAX, X86_ESP, 0, 4);
411                 /* The trampoline returns normally, pop the trampoline argument */
412                 x86_alu_reg_imm (code, X86_ADD, X86_ESP, 4);
413                 cfa_offset -= sizeof (mgreg_t);
414                 mono_add_unwind_op_def_cfa_offset (unwind_ops, code, buf, cfa_offset);
415                 x86_ret (code);
416         } else {
417                 /* The trampoline argument is at the top of the stack, and it contains the address we need to branch to */
418                 if (tramp_type == MONO_TRAMPOLINE_HANDLER_BLOCK_GUARD) {
419                         x86_pop_reg (code, X86_EAX);
420                         cfa_offset -= sizeof (mgreg_t);
421                         mono_add_unwind_op_def_cfa_offset (unwind_ops, code, buf, cfa_offset);
422                         x86_alu_reg_imm (code, X86_ADD, X86_ESP, 0x8);
423                         x86_jump_reg (code, X86_EAX);
424                 } else {
425                         x86_ret (code);
426                 }
427         }
428
429         g_assert ((code - buf) <= 256);
430         MONO_PROFILER_RAISE (jit_code_buffer, (buf, code - buf, MONO_PROFILER_CODE_BUFFER_HELPER, NULL));
431
432         tramp_name = mono_get_generic_trampoline_name (tramp_type);
433         *info = mono_tramp_info_create (tramp_name, buf, code - buf, ji, unwind_ops);
434         g_free (tramp_name);
435
436         return buf;
437 }
438
439 #define TRAMPOLINE_SIZE 10
440
441 gpointer
442 mono_arch_create_specific_trampoline (gpointer arg1, MonoTrampolineType tramp_type, MonoDomain *domain, guint32 *code_len)
443 {
444         guint8 *code, *buf, *tramp;
445         
446         tramp = mono_get_trampoline_code (tramp_type);
447
448         code = buf = mono_domain_code_reserve_align (domain, TRAMPOLINE_SIZE, 4);
449
450         x86_push_imm (buf, arg1);
451         x86_jump_code (buf, tramp);
452         g_assert ((buf - code) <= TRAMPOLINE_SIZE);
453
454         mono_arch_flush_icache (code, buf - code);
455         MONO_PROFILER_RAISE (jit_code_buffer, (code, buf - code, MONO_PROFILER_CODE_BUFFER_SPECIFIC_TRAMPOLINE, mono_get_generic_trampoline_simple_name (tramp_type)));
456
457         if (code_len)
458                 *code_len = buf - code;
459
460         return code;
461 }
462
463 gpointer
464 mono_arch_create_rgctx_lazy_fetch_trampoline (guint32 slot, MonoTrampInfo **info, gboolean aot)
465 {
466         guint8 *tramp;
467         guint8 *code, *buf;
468         guint8 **rgctx_null_jumps;
469         int tramp_size;
470         int depth, index;
471         int i;
472         gboolean mrgctx;
473         MonoJumpInfo *ji = NULL;
474         GSList *unwind_ops = NULL;
475
476         unwind_ops = mono_arch_get_cie_program ();
477
478         mrgctx = MONO_RGCTX_SLOT_IS_MRGCTX (slot);
479         index = MONO_RGCTX_SLOT_INDEX (slot);
480         if (mrgctx)
481                 index += MONO_SIZEOF_METHOD_RUNTIME_GENERIC_CONTEXT / sizeof (gpointer);
482         for (depth = 0; ; ++depth) {
483                 int size = mono_class_rgctx_get_array_size (depth, mrgctx);
484
485                 if (index < size - 1)
486                         break;
487                 index -= size - 1;
488         }
489
490         tramp_size = (aot ? 64 : 36) + 6 * depth;
491
492         code = buf = mono_global_codeman_reserve (tramp_size);
493
494         rgctx_null_jumps = g_malloc (sizeof (guint8*) * (depth + 2));
495
496         /* load vtable/mrgctx ptr */
497         x86_mov_reg_membase (code, X86_EAX, X86_ESP, 4, 4);
498         if (!mrgctx) {
499                 /* load rgctx ptr from vtable */
500                 x86_mov_reg_membase (code, X86_EAX, X86_EAX, MONO_STRUCT_OFFSET (MonoVTable, runtime_generic_context), 4);
501                 /* is the rgctx ptr null? */
502                 x86_test_reg_reg (code, X86_EAX, X86_EAX);
503                 /* if yes, jump to actual trampoline */
504                 rgctx_null_jumps [0] = code;
505                 x86_branch8 (code, X86_CC_Z, -1, 1);
506         }
507
508         for (i = 0; i < depth; ++i) {
509                 /* load ptr to next array */
510                 if (mrgctx && i == 0)
511                         x86_mov_reg_membase (code, X86_EAX, X86_EAX, MONO_SIZEOF_METHOD_RUNTIME_GENERIC_CONTEXT, 4);
512                 else
513                         x86_mov_reg_membase (code, X86_EAX, X86_EAX, 0, 4);
514                 /* is the ptr null? */
515                 x86_test_reg_reg (code, X86_EAX, X86_EAX);
516                 /* if yes, jump to actual trampoline */
517                 rgctx_null_jumps [i + 1] = code;
518                 x86_branch8 (code, X86_CC_Z, -1, 1);
519         }
520
521         /* fetch slot */
522         x86_mov_reg_membase (code, X86_EAX, X86_EAX, sizeof (gpointer) * (index + 1), 4);
523         /* is the slot null? */
524         x86_test_reg_reg (code, X86_EAX, X86_EAX);
525         /* if yes, jump to actual trampoline */
526         rgctx_null_jumps [depth + 1] = code;
527         x86_branch8 (code, X86_CC_Z, -1, 1);
528         /* otherwise return */
529         x86_ret (code);
530
531         for (i = mrgctx ? 1 : 0; i <= depth + 1; ++i)
532                 x86_patch (rgctx_null_jumps [i], code);
533
534         g_free (rgctx_null_jumps);
535
536         x86_mov_reg_membase (code, MONO_ARCH_VTABLE_REG, X86_ESP, 4, 4);
537
538         if (aot) {
539                 code = mono_arch_emit_load_aotconst (buf, code, &ji, MONO_PATCH_INFO_JIT_ICALL_ADDR, g_strdup_printf ("specific_trampoline_lazy_fetch_%u", slot));
540                 x86_jump_reg (code, X86_EAX);
541         } else {
542                 tramp = mono_arch_create_specific_trampoline (GUINT_TO_POINTER (slot), MONO_TRAMPOLINE_RGCTX_LAZY_FETCH, mono_get_root_domain (), NULL);
543
544                 /* jump to the actual trampoline */
545                 x86_jump_code (code, tramp);
546         }
547
548         mono_arch_flush_icache (buf, code - buf);
549         MONO_PROFILER_RAISE (jit_code_buffer, (buf, code - buf, MONO_PROFILER_CODE_BUFFER_GENERICS_TRAMPOLINE, NULL));
550
551         g_assert (code - buf <= tramp_size);
552
553         char *name = mono_get_rgctx_fetch_trampoline_name (slot);
554         *info = mono_tramp_info_create (name, buf, code - buf, ji, unwind_ops);
555         g_free (name);
556
557         return buf;
558 }
559
560 /*
561  * mono_arch_create_general_rgctx_lazy_fetch_trampoline:
562  *
563  *   This is a general variant of the rgctx fetch trampolines. It receives a pointer to gpointer[2] in the rgctx reg. The first entry contains the slot, the second
564  * the trampoline to call if the slot is not filled.
565  */
566 gpointer
567 mono_arch_create_general_rgctx_lazy_fetch_trampoline (MonoTrampInfo **info, gboolean aot)
568 {
569         guint8 *code, *buf;
570         int tramp_size;
571         MonoJumpInfo *ji = NULL;
572         GSList *unwind_ops = NULL;
573
574         g_assert (aot);
575
576         unwind_ops = mono_arch_get_cie_program ();
577
578         tramp_size = 64;
579
580         code = buf = mono_global_codeman_reserve (tramp_size);
581
582         // FIXME: Currently, we always go to the slow path.
583         
584         /* Load trampoline addr */
585         x86_mov_reg_membase (code, X86_EAX, MONO_ARCH_RGCTX_REG, 4, 4);
586         /* Load mrgctx/vtable */
587         x86_mov_reg_membase (code, MONO_ARCH_VTABLE_REG, X86_ESP, 4, 4);
588
589         x86_jump_reg (code, X86_EAX);
590
591         mono_arch_flush_icache (buf, code - buf);
592         MONO_PROFILER_RAISE (jit_code_buffer, (buf, code - buf, MONO_PROFILER_CODE_BUFFER_GENERICS_TRAMPOLINE, NULL));
593
594         g_assert (code - buf <= tramp_size);
595
596         *info = mono_tramp_info_create ("rgctx_fetch_trampoline_general", buf, code - buf, ji, unwind_ops);
597
598         return buf;
599 }
600
601 void
602 mono_arch_invalidate_method (MonoJitInfo *ji, void *func, gpointer func_arg)
603 {
604         /* FIXME: This is not thread safe */
605         guint8 *code = ji->code_start;
606
607         x86_push_imm (code, func_arg);
608         x86_call_code (code, (guint8*)func);
609 }
610
611 static gpointer
612 handler_block_trampoline_helper (void)
613 {
614         MonoJitTlsData *jit_tls = mono_tls_get_jit_tls ();
615         return jit_tls->handler_block_return_address;
616 }
617
618 gpointer
619 mono_arch_create_handler_block_trampoline (MonoTrampInfo **info, gboolean aot)
620 {
621         guint8 *tramp = mono_get_trampoline_code (MONO_TRAMPOLINE_HANDLER_BLOCK_GUARD);
622         guint8 *code, *buf;
623         int tramp_size = 64;
624         MonoJumpInfo *ji = NULL;
625         int cfa_offset;
626         GSList *unwind_ops = NULL;
627
628         g_assert (!aot);
629
630         code = buf = mono_global_codeman_reserve (tramp_size);
631
632         unwind_ops = mono_arch_get_cie_program ();
633         cfa_offset = sizeof (mgreg_t);
634         /*
635         This trampoline restore the call chain of the handler block then jumps into the code that deals with it.
636         */
637
638         /*
639          * We are in a method frame after the call emitted by OP_CALL_HANDLER.
640          */
641
642         /*Slow path uses a c helper*/
643         x86_call_code (code, handler_block_trampoline_helper);
644         /* Simulate a call */
645         /*Fix stack alignment*/
646         x86_alu_reg_imm (code, X86_SUB, X86_ESP, 0x4);
647         cfa_offset += sizeof (mgreg_t);
648         mono_add_unwind_op_def_cfa_offset (unwind_ops, code, buf, cfa_offset);
649
650         /* This is the address the trampoline will return to */
651         x86_push_reg (code, X86_EAX);
652         cfa_offset += sizeof (mgreg_t);
653         mono_add_unwind_op_def_cfa_offset (unwind_ops, code, buf, cfa_offset);
654
655         /* Dummy trampoline argument, since we call the generic trampoline directly */
656         x86_push_imm (code, 0);
657         cfa_offset += sizeof (mgreg_t);
658         mono_add_unwind_op_def_cfa_offset (unwind_ops, code, buf, cfa_offset);
659         x86_jump_code (code, tramp);
660
661         mono_arch_flush_icache (buf, code - buf);
662         MONO_PROFILER_RAISE (jit_code_buffer, (buf, code - buf, MONO_PROFILER_CODE_BUFFER_HELPER, NULL));
663         g_assert (code - buf <= tramp_size);
664
665         *info = mono_tramp_info_create ("handler_block_trampoline", buf, code - buf, ji, unwind_ops);
666
667         return buf;
668 }
669
670 guint8*
671 mono_arch_get_call_target (guint8 *code)
672 {
673         if (code [-5] == 0xe8) {
674                 gint32 disp = *(gint32*)(code - 4);
675                 guint8 *target = code + disp;
676
677                 return target;
678         } else {
679                 return NULL;
680         }
681 }
682
683 guint32
684 mono_arch_get_plt_info_offset (guint8 *plt_entry, mgreg_t *regs, guint8 *code)
685 {
686         return *(guint32*)(plt_entry + 6);
687 }
688
689 /*
690  * mono_arch_get_gsharedvt_arg_trampoline:
691  *
692  *   Return a trampoline which passes ARG to the gsharedvt in/out trampoline ADDR.
693  */
694 gpointer
695 mono_arch_get_gsharedvt_arg_trampoline (MonoDomain *domain, gpointer arg, gpointer addr)
696 {
697         guint8 *code, *start;
698         int buf_len;
699         GSList *unwind_ops;
700
701
702         buf_len = 10;
703
704         start = code = mono_domain_code_reserve (domain, buf_len);
705
706         unwind_ops = mono_arch_get_cie_program ();
707
708         x86_mov_reg_imm (code, X86_EAX, arg);
709         x86_jump_code (code, addr);
710         g_assert ((code - start) <= buf_len);
711
712         mono_arch_flush_icache (start, code - start);
713         MONO_PROFILER_RAISE (jit_code_buffer, (start, code - start, MONO_PROFILER_CODE_BUFFER_GENERICS_TRAMPOLINE, NULL));
714
715         mono_tramp_info_register (mono_tramp_info_create (NULL, start, code - start, NULL, unwind_ops), domain);
716
717         return start;
718 }
719
720 /*
721  * mono_arch_create_sdb_trampoline:
722  *
723  *   Return a trampoline which captures the current context, passes it to
724  * debugger_agent_single_step_from_context ()/debugger_agent_breakpoint_from_context (),
725  * then restores the (potentially changed) context.
726  */
727 guint8*
728 mono_arch_create_sdb_trampoline (gboolean single_step, MonoTrampInfo **info, gboolean aot)
729 {
730         int tramp_size = 256;
731         int framesize, ctx_offset, cfa_offset;
732         guint8 *code, *buf;
733         GSList *unwind_ops = NULL;
734         MonoJumpInfo *ji = NULL;
735
736         code = buf = mono_global_codeman_reserve (tramp_size);
737
738         framesize = 0;
739
740         /* Argument area */
741         framesize += sizeof (mgreg_t);
742
743         framesize = ALIGN_TO (framesize, 8);
744         ctx_offset = framesize;
745         framesize += sizeof (MonoContext);
746
747         framesize = ALIGN_TO (framesize, MONO_ARCH_FRAME_ALIGNMENT);
748
749         // CFA = sp + 4
750         cfa_offset = 4;
751         mono_add_unwind_op_def_cfa (unwind_ops, code, buf, X86_ESP, 4);
752         // IP saved at CFA - 4
753         mono_add_unwind_op_offset (unwind_ops, code, buf, X86_NREG, -cfa_offset);
754
755         x86_push_reg (code, X86_EBP);
756         cfa_offset += sizeof(mgreg_t);
757         mono_add_unwind_op_def_cfa_offset (unwind_ops, code, buf, cfa_offset);
758         mono_add_unwind_op_offset (unwind_ops, code, buf, X86_EBP, - cfa_offset);
759
760         x86_mov_reg_reg (code, X86_EBP, X86_ESP, sizeof(mgreg_t));
761         mono_add_unwind_op_def_cfa_reg (unwind_ops, code, buf, X86_EBP);
762         /* The + 8 makes the stack aligned */
763         x86_alu_reg_imm (code, X86_SUB, X86_ESP, framesize + 8);
764
765         /* Initialize a MonoContext structure on the stack */
766         x86_mov_membase_reg (code, X86_ESP, ctx_offset + G_STRUCT_OFFSET (MonoContext, eax), X86_EAX, sizeof (mgreg_t));
767         x86_mov_membase_reg (code, X86_ESP, ctx_offset + G_STRUCT_OFFSET (MonoContext, ebx), X86_EBX, sizeof (mgreg_t));
768         x86_mov_membase_reg (code, X86_ESP, ctx_offset + G_STRUCT_OFFSET (MonoContext, ecx), X86_ECX, sizeof (mgreg_t));
769         x86_mov_membase_reg (code, X86_ESP, ctx_offset + G_STRUCT_OFFSET (MonoContext, edx), X86_EDX, sizeof (mgreg_t));
770         x86_mov_reg_membase (code, X86_EAX, X86_EBP, 0, sizeof (mgreg_t));
771         x86_mov_membase_reg (code, X86_ESP, ctx_offset + G_STRUCT_OFFSET (MonoContext, ebp), X86_EAX, sizeof (mgreg_t));
772         x86_mov_reg_reg (code, X86_EAX, X86_EBP, sizeof (mgreg_t));
773         x86_alu_reg_imm (code, X86_ADD, X86_EAX, cfa_offset);
774         x86_mov_membase_reg (code, X86_ESP, ctx_offset + G_STRUCT_OFFSET (MonoContext, esp), X86_ESP, sizeof (mgreg_t));
775         x86_mov_membase_reg (code, X86_ESP, ctx_offset + G_STRUCT_OFFSET (MonoContext, esi), X86_ESI, sizeof (mgreg_t));
776         x86_mov_membase_reg (code, X86_ESP, ctx_offset + G_STRUCT_OFFSET (MonoContext, edi), X86_EDI, sizeof (mgreg_t));
777         x86_mov_reg_membase (code, X86_EAX, X86_EBP, 4, sizeof (mgreg_t));
778         x86_mov_membase_reg (code, X86_ESP, ctx_offset + G_STRUCT_OFFSET (MonoContext, eip), X86_EAX, sizeof (mgreg_t));
779
780         /* Call the single step/breakpoint function in sdb */
781         x86_lea_membase (code, X86_EAX, X86_ESP, ctx_offset);
782         x86_mov_membase_reg (code, X86_ESP, 0, X86_EAX, sizeof (mgreg_t));
783
784         if (aot) {
785                 x86_breakpoint (code);
786         } else {
787                 if (single_step)
788                         x86_call_code (code, debugger_agent_single_step_from_context);
789                 else
790                         x86_call_code (code, debugger_agent_breakpoint_from_context);
791         }
792
793         /* Restore registers from ctx */
794         /* Overwrite the saved ebp */
795         x86_mov_reg_membase (code, X86_EAX, X86_ESP, ctx_offset + G_STRUCT_OFFSET (MonoContext, ebp), sizeof (mgreg_t));
796         x86_mov_membase_reg (code, X86_EBP, 0, X86_EAX, sizeof (mgreg_t));
797         /* Overwrite saved eip */
798         x86_mov_reg_membase (code, X86_EAX, X86_ESP, ctx_offset + G_STRUCT_OFFSET (MonoContext, eip), sizeof (mgreg_t));
799         x86_mov_membase_reg (code, X86_EBP, 4, X86_EAX, sizeof (mgreg_t));
800         x86_mov_reg_membase (code, X86_EAX, X86_ESP, ctx_offset + G_STRUCT_OFFSET (MonoContext, eax), sizeof (mgreg_t));
801         x86_mov_reg_membase (code, X86_EBX, X86_ESP, ctx_offset + G_STRUCT_OFFSET (MonoContext, ebx), sizeof (mgreg_t));
802         x86_mov_reg_membase (code, X86_ECX, X86_ESP, ctx_offset + G_STRUCT_OFFSET (MonoContext, ecx), sizeof (mgreg_t));
803         x86_mov_reg_membase (code, X86_EDX, X86_ESP, ctx_offset + G_STRUCT_OFFSET (MonoContext, edx), sizeof (mgreg_t));
804         x86_mov_reg_membase (code, X86_ESI, X86_ESP, ctx_offset + G_STRUCT_OFFSET (MonoContext, esi), sizeof (mgreg_t));
805         x86_mov_reg_membase (code, X86_EDI, X86_ESP, ctx_offset + G_STRUCT_OFFSET (MonoContext, edi), sizeof (mgreg_t));
806
807         x86_leave (code);
808         cfa_offset -= sizeof (mgreg_t);
809         mono_add_unwind_op_def_cfa (unwind_ops, code, buf, X86_ESP, cfa_offset);
810         x86_ret (code);
811
812         mono_arch_flush_icache (code, code - buf);
813         g_assert (code - buf <= tramp_size);
814
815         const char *tramp_name = single_step ? "sdb_single_step_trampoline" : "sdb_breakpoint_trampoline";
816         *info = mono_tramp_info_create (tramp_name, buf, code - buf, ji, unwind_ops);
817
818         return buf;
819 }
820
821 gpointer
822 mono_arch_get_enter_icall_trampoline (MonoTrampInfo **info)
823 {
824         g_assert_not_reached ();
825         return NULL;
826 }
827