Merge pull request #5714 from alexischr/update_bockbuild
[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                 x86_ret (code);
418         }
419
420         g_assert ((code - buf) <= 256);
421         MONO_PROFILER_RAISE (jit_code_buffer, (buf, code - buf, MONO_PROFILER_CODE_BUFFER_HELPER, NULL));
422
423         tramp_name = mono_get_generic_trampoline_name (tramp_type);
424         *info = mono_tramp_info_create (tramp_name, buf, code - buf, ji, unwind_ops);
425         g_free (tramp_name);
426
427         return buf;
428 }
429
430 #define TRAMPOLINE_SIZE 10
431
432 gpointer
433 mono_arch_create_specific_trampoline (gpointer arg1, MonoTrampolineType tramp_type, MonoDomain *domain, guint32 *code_len)
434 {
435         guint8 *code, *buf, *tramp;
436         
437         tramp = mono_get_trampoline_code (tramp_type);
438
439         code = buf = mono_domain_code_reserve_align (domain, TRAMPOLINE_SIZE, 4);
440
441         x86_push_imm (buf, arg1);
442         x86_jump_code (buf, tramp);
443         g_assert ((buf - code) <= TRAMPOLINE_SIZE);
444
445         mono_arch_flush_icache (code, buf - code);
446         MONO_PROFILER_RAISE (jit_code_buffer, (code, buf - code, MONO_PROFILER_CODE_BUFFER_SPECIFIC_TRAMPOLINE, mono_get_generic_trampoline_simple_name (tramp_type)));
447
448         if (code_len)
449                 *code_len = buf - code;
450
451         return code;
452 }
453
454 gpointer
455 mono_arch_create_rgctx_lazy_fetch_trampoline (guint32 slot, MonoTrampInfo **info, gboolean aot)
456 {
457         guint8 *tramp;
458         guint8 *code, *buf;
459         guint8 **rgctx_null_jumps;
460         int tramp_size;
461         int depth, index;
462         int i;
463         gboolean mrgctx;
464         MonoJumpInfo *ji = NULL;
465         GSList *unwind_ops = NULL;
466
467         unwind_ops = mono_arch_get_cie_program ();
468
469         mrgctx = MONO_RGCTX_SLOT_IS_MRGCTX (slot);
470         index = MONO_RGCTX_SLOT_INDEX (slot);
471         if (mrgctx)
472                 index += MONO_SIZEOF_METHOD_RUNTIME_GENERIC_CONTEXT / sizeof (gpointer);
473         for (depth = 0; ; ++depth) {
474                 int size = mono_class_rgctx_get_array_size (depth, mrgctx);
475
476                 if (index < size - 1)
477                         break;
478                 index -= size - 1;
479         }
480
481         tramp_size = (aot ? 64 : 36) + 6 * depth;
482
483         code = buf = mono_global_codeman_reserve (tramp_size);
484
485         rgctx_null_jumps = g_malloc (sizeof (guint8*) * (depth + 2));
486
487         /* load vtable/mrgctx ptr */
488         x86_mov_reg_membase (code, X86_EAX, X86_ESP, 4, 4);
489         if (!mrgctx) {
490                 /* load rgctx ptr from vtable */
491                 x86_mov_reg_membase (code, X86_EAX, X86_EAX, MONO_STRUCT_OFFSET (MonoVTable, runtime_generic_context), 4);
492                 /* is the rgctx ptr null? */
493                 x86_test_reg_reg (code, X86_EAX, X86_EAX);
494                 /* if yes, jump to actual trampoline */
495                 rgctx_null_jumps [0] = code;
496                 x86_branch8 (code, X86_CC_Z, -1, 1);
497         }
498
499         for (i = 0; i < depth; ++i) {
500                 /* load ptr to next array */
501                 if (mrgctx && i == 0)
502                         x86_mov_reg_membase (code, X86_EAX, X86_EAX, MONO_SIZEOF_METHOD_RUNTIME_GENERIC_CONTEXT, 4);
503                 else
504                         x86_mov_reg_membase (code, X86_EAX, X86_EAX, 0, 4);
505                 /* is the ptr null? */
506                 x86_test_reg_reg (code, X86_EAX, X86_EAX);
507                 /* if yes, jump to actual trampoline */
508                 rgctx_null_jumps [i + 1] = code;
509                 x86_branch8 (code, X86_CC_Z, -1, 1);
510         }
511
512         /* fetch slot */
513         x86_mov_reg_membase (code, X86_EAX, X86_EAX, sizeof (gpointer) * (index + 1), 4);
514         /* is the slot null? */
515         x86_test_reg_reg (code, X86_EAX, X86_EAX);
516         /* if yes, jump to actual trampoline */
517         rgctx_null_jumps [depth + 1] = code;
518         x86_branch8 (code, X86_CC_Z, -1, 1);
519         /* otherwise return */
520         x86_ret (code);
521
522         for (i = mrgctx ? 1 : 0; i <= depth + 1; ++i)
523                 x86_patch (rgctx_null_jumps [i], code);
524
525         g_free (rgctx_null_jumps);
526
527         x86_mov_reg_membase (code, MONO_ARCH_VTABLE_REG, X86_ESP, 4, 4);
528
529         if (aot) {
530                 code = mono_arch_emit_load_aotconst (buf, code, &ji, MONO_PATCH_INFO_JIT_ICALL_ADDR, g_strdup_printf ("specific_trampoline_lazy_fetch_%u", slot));
531                 x86_jump_reg (code, X86_EAX);
532         } else {
533                 tramp = mono_arch_create_specific_trampoline (GUINT_TO_POINTER (slot), MONO_TRAMPOLINE_RGCTX_LAZY_FETCH, mono_get_root_domain (), NULL);
534
535                 /* jump to the actual trampoline */
536                 x86_jump_code (code, tramp);
537         }
538
539         mono_arch_flush_icache (buf, code - buf);
540         MONO_PROFILER_RAISE (jit_code_buffer, (buf, code - buf, MONO_PROFILER_CODE_BUFFER_GENERICS_TRAMPOLINE, NULL));
541
542         g_assert (code - buf <= tramp_size);
543
544         char *name = mono_get_rgctx_fetch_trampoline_name (slot);
545         *info = mono_tramp_info_create (name, buf, code - buf, ji, unwind_ops);
546         g_free (name);
547
548         return buf;
549 }
550
551 /*
552  * mono_arch_create_general_rgctx_lazy_fetch_trampoline:
553  *
554  *   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
555  * the trampoline to call if the slot is not filled.
556  */
557 gpointer
558 mono_arch_create_general_rgctx_lazy_fetch_trampoline (MonoTrampInfo **info, gboolean aot)
559 {
560         guint8 *code, *buf;
561         int tramp_size;
562         MonoJumpInfo *ji = NULL;
563         GSList *unwind_ops = NULL;
564
565         g_assert (aot);
566
567         unwind_ops = mono_arch_get_cie_program ();
568
569         tramp_size = 64;
570
571         code = buf = mono_global_codeman_reserve (tramp_size);
572
573         // FIXME: Currently, we always go to the slow path.
574         
575         /* Load trampoline addr */
576         x86_mov_reg_membase (code, X86_EAX, MONO_ARCH_RGCTX_REG, 4, 4);
577         /* Load mrgctx/vtable */
578         x86_mov_reg_membase (code, MONO_ARCH_VTABLE_REG, X86_ESP, 4, 4);
579
580         x86_jump_reg (code, X86_EAX);
581
582         mono_arch_flush_icache (buf, code - buf);
583         MONO_PROFILER_RAISE (jit_code_buffer, (buf, code - buf, MONO_PROFILER_CODE_BUFFER_GENERICS_TRAMPOLINE, NULL));
584
585         g_assert (code - buf <= tramp_size);
586
587         *info = mono_tramp_info_create ("rgctx_fetch_trampoline_general", buf, code - buf, ji, unwind_ops);
588
589         return buf;
590 }
591
592 void
593 mono_arch_invalidate_method (MonoJitInfo *ji, void *func, gpointer func_arg)
594 {
595         /* FIXME: This is not thread safe */
596         guint8 *code = ji->code_start;
597
598         x86_push_imm (code, func_arg);
599         x86_call_code (code, (guint8*)func);
600 }
601
602 guint8*
603 mono_arch_get_call_target (guint8 *code)
604 {
605         if (code [-5] == 0xe8) {
606                 gint32 disp = *(gint32*)(code - 4);
607                 guint8 *target = code + disp;
608
609                 return target;
610         } else {
611                 return NULL;
612         }
613 }
614
615 guint32
616 mono_arch_get_plt_info_offset (guint8 *plt_entry, mgreg_t *regs, guint8 *code)
617 {
618         return *(guint32*)(plt_entry + 6);
619 }
620
621 /*
622  * mono_arch_get_gsharedvt_arg_trampoline:
623  *
624  *   Return a trampoline which passes ARG to the gsharedvt in/out trampoline ADDR.
625  */
626 gpointer
627 mono_arch_get_gsharedvt_arg_trampoline (MonoDomain *domain, gpointer arg, gpointer addr)
628 {
629         guint8 *code, *start;
630         int buf_len;
631         GSList *unwind_ops;
632
633
634         buf_len = 10;
635
636         start = code = mono_domain_code_reserve (domain, buf_len);
637
638         unwind_ops = mono_arch_get_cie_program ();
639
640         x86_mov_reg_imm (code, X86_EAX, arg);
641         x86_jump_code (code, addr);
642         g_assert ((code - start) <= buf_len);
643
644         mono_arch_flush_icache (start, code - start);
645         MONO_PROFILER_RAISE (jit_code_buffer, (start, code - start, MONO_PROFILER_CODE_BUFFER_GENERICS_TRAMPOLINE, NULL));
646
647         mono_tramp_info_register (mono_tramp_info_create (NULL, start, code - start, NULL, unwind_ops), domain);
648
649         return start;
650 }
651
652 /*
653  * mono_arch_create_sdb_trampoline:
654  *
655  *   Return a trampoline which captures the current context, passes it to
656  * debugger_agent_single_step_from_context ()/debugger_agent_breakpoint_from_context (),
657  * then restores the (potentially changed) context.
658  */
659 guint8*
660 mono_arch_create_sdb_trampoline (gboolean single_step, MonoTrampInfo **info, gboolean aot)
661 {
662         int tramp_size = 256;
663         int framesize, ctx_offset, cfa_offset;
664         guint8 *code, *buf;
665         GSList *unwind_ops = NULL;
666         MonoJumpInfo *ji = NULL;
667
668         code = buf = mono_global_codeman_reserve (tramp_size);
669
670         framesize = 0;
671
672         /* Argument area */
673         framesize += sizeof (mgreg_t);
674
675         framesize = ALIGN_TO (framesize, 8);
676         ctx_offset = framesize;
677         framesize += sizeof (MonoContext);
678
679         framesize = ALIGN_TO (framesize, MONO_ARCH_FRAME_ALIGNMENT);
680
681         // CFA = sp + 4
682         cfa_offset = 4;
683         mono_add_unwind_op_def_cfa (unwind_ops, code, buf, X86_ESP, 4);
684         // IP saved at CFA - 4
685         mono_add_unwind_op_offset (unwind_ops, code, buf, X86_NREG, -cfa_offset);
686
687         x86_push_reg (code, X86_EBP);
688         cfa_offset += sizeof(mgreg_t);
689         mono_add_unwind_op_def_cfa_offset (unwind_ops, code, buf, cfa_offset);
690         mono_add_unwind_op_offset (unwind_ops, code, buf, X86_EBP, - cfa_offset);
691
692         x86_mov_reg_reg (code, X86_EBP, X86_ESP, sizeof(mgreg_t));
693         mono_add_unwind_op_def_cfa_reg (unwind_ops, code, buf, X86_EBP);
694         /* The + 8 makes the stack aligned */
695         x86_alu_reg_imm (code, X86_SUB, X86_ESP, framesize + 8);
696
697         /* Initialize a MonoContext structure on the stack */
698         x86_mov_membase_reg (code, X86_ESP, ctx_offset + G_STRUCT_OFFSET (MonoContext, eax), X86_EAX, sizeof (mgreg_t));
699         x86_mov_membase_reg (code, X86_ESP, ctx_offset + G_STRUCT_OFFSET (MonoContext, ebx), X86_EBX, sizeof (mgreg_t));
700         x86_mov_membase_reg (code, X86_ESP, ctx_offset + G_STRUCT_OFFSET (MonoContext, ecx), X86_ECX, sizeof (mgreg_t));
701         x86_mov_membase_reg (code, X86_ESP, ctx_offset + G_STRUCT_OFFSET (MonoContext, edx), X86_EDX, sizeof (mgreg_t));
702         x86_mov_reg_membase (code, X86_EAX, X86_EBP, 0, sizeof (mgreg_t));
703         x86_mov_membase_reg (code, X86_ESP, ctx_offset + G_STRUCT_OFFSET (MonoContext, ebp), X86_EAX, sizeof (mgreg_t));
704         x86_mov_reg_reg (code, X86_EAX, X86_EBP, sizeof (mgreg_t));
705         x86_alu_reg_imm (code, X86_ADD, X86_EAX, cfa_offset);
706         x86_mov_membase_reg (code, X86_ESP, ctx_offset + G_STRUCT_OFFSET (MonoContext, esp), X86_ESP, sizeof (mgreg_t));
707         x86_mov_membase_reg (code, X86_ESP, ctx_offset + G_STRUCT_OFFSET (MonoContext, esi), X86_ESI, sizeof (mgreg_t));
708         x86_mov_membase_reg (code, X86_ESP, ctx_offset + G_STRUCT_OFFSET (MonoContext, edi), X86_EDI, sizeof (mgreg_t));
709         x86_mov_reg_membase (code, X86_EAX, X86_EBP, 4, sizeof (mgreg_t));
710         x86_mov_membase_reg (code, X86_ESP, ctx_offset + G_STRUCT_OFFSET (MonoContext, eip), X86_EAX, sizeof (mgreg_t));
711
712         /* Call the single step/breakpoint function in sdb */
713         x86_lea_membase (code, X86_EAX, X86_ESP, ctx_offset);
714         x86_mov_membase_reg (code, X86_ESP, 0, X86_EAX, sizeof (mgreg_t));
715
716         if (aot) {
717                 x86_breakpoint (code);
718         } else {
719                 if (single_step)
720                         x86_call_code (code, debugger_agent_single_step_from_context);
721                 else
722                         x86_call_code (code, debugger_agent_breakpoint_from_context);
723         }
724
725         /* Restore registers from ctx */
726         /* Overwrite the saved ebp */
727         x86_mov_reg_membase (code, X86_EAX, X86_ESP, ctx_offset + G_STRUCT_OFFSET (MonoContext, ebp), sizeof (mgreg_t));
728         x86_mov_membase_reg (code, X86_EBP, 0, X86_EAX, sizeof (mgreg_t));
729         /* Overwrite saved eip */
730         x86_mov_reg_membase (code, X86_EAX, X86_ESP, ctx_offset + G_STRUCT_OFFSET (MonoContext, eip), sizeof (mgreg_t));
731         x86_mov_membase_reg (code, X86_EBP, 4, X86_EAX, sizeof (mgreg_t));
732         x86_mov_reg_membase (code, X86_EAX, X86_ESP, ctx_offset + G_STRUCT_OFFSET (MonoContext, eax), sizeof (mgreg_t));
733         x86_mov_reg_membase (code, X86_EBX, X86_ESP, ctx_offset + G_STRUCT_OFFSET (MonoContext, ebx), sizeof (mgreg_t));
734         x86_mov_reg_membase (code, X86_ECX, X86_ESP, ctx_offset + G_STRUCT_OFFSET (MonoContext, ecx), sizeof (mgreg_t));
735         x86_mov_reg_membase (code, X86_EDX, X86_ESP, ctx_offset + G_STRUCT_OFFSET (MonoContext, edx), sizeof (mgreg_t));
736         x86_mov_reg_membase (code, X86_ESI, X86_ESP, ctx_offset + G_STRUCT_OFFSET (MonoContext, esi), sizeof (mgreg_t));
737         x86_mov_reg_membase (code, X86_EDI, X86_ESP, ctx_offset + G_STRUCT_OFFSET (MonoContext, edi), sizeof (mgreg_t));
738
739         x86_leave (code);
740         cfa_offset -= sizeof (mgreg_t);
741         mono_add_unwind_op_def_cfa (unwind_ops, code, buf, X86_ESP, cfa_offset);
742         x86_ret (code);
743
744         mono_arch_flush_icache (code, code - buf);
745         g_assert (code - buf <= tramp_size);
746
747         const char *tramp_name = single_step ? "sdb_single_step_trampoline" : "sdb_breakpoint_trampoline";
748         *info = mono_tramp_info_create (tramp_name, buf, code - buf, ji, unwind_ops);
749
750         return buf;
751 }
752
753 gpointer
754 mono_arch_get_enter_icall_trampoline (MonoTrampInfo **info)
755 {
756         g_assert_not_reached ();
757         return NULL;
758 }
759