[runtime] Register static rgctx trampolines
[mono.git] / mono / mini / tramp-x86.c
1 /*
2  * tramp-x86.c: JIT trampoline code for x86
3  *
4  * Authors:
5  *   Dietmar Maurer (dietmar@ximian.com)
6  *
7  * (C) 2001 Ximian, Inc.
8  */
9
10 #include <config.h>
11 #include <glib.h>
12
13 #include <mono/metadata/abi-details.h>
14 #include <mono/metadata/appdomain.h>
15 #include <mono/metadata/metadata-internals.h>
16 #include <mono/metadata/marshal.h>
17 #include <mono/metadata/tabledefs.h>
18 #include <mono/metadata/mono-debug.h>
19 #include <mono/metadata/mono-debug-debugger.h>
20 #include <mono/metadata/monitor.h>
21 #include <mono/metadata/profiler-private.h>
22 #include <mono/metadata/gc-internal.h>
23 #include <mono/arch/x86/x86-codegen.h>
24
25 #include <mono/utils/memcheck.h>
26
27 #include "mini.h"
28 #include "mini-x86.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 = NACL_SIZE(16, 32);
46         MonoDomain *domain = mono_domain_get ();
47
48         start = code = mono_domain_code_reserve (domain, size);
49
50         x86_alu_membase_imm (code, X86_ADD, X86_ESP, this_pos, sizeof (MonoObject));
51         x86_jump_code (code, addr);
52         g_assert ((code - start) < size);
53
54         nacl_domain_code_validate (domain, &start, size, &code);
55         mono_profiler_code_buffer_new (start, code - start, MONO_PROFILER_CODE_BUFFER_UNBOX_TRAMPOLINE, m);
56
57         return start;
58 }
59
60 gpointer
61 mono_arch_get_static_rgctx_trampoline (MonoMethod *m, MonoMethodRuntimeGenericContext *mrgctx, gpointer addr)
62 {
63         guint8 *code, *start;
64         int buf_len;
65
66         MonoDomain *domain = mono_domain_get ();
67
68         buf_len = NACL_SIZE (10, 32);
69
70         start = code = mono_domain_code_reserve (domain, buf_len);
71
72         x86_mov_reg_imm (code, MONO_ARCH_RGCTX_REG, mrgctx);
73         x86_jump_code (code, addr);
74         g_assert ((code - start) <= buf_len);
75
76         nacl_domain_code_validate (domain, &start, buf_len, &code);
77         mono_arch_flush_icache (start, code - start);
78         mono_profiler_code_buffer_new (start, code - start, MONO_PROFILER_CODE_BUFFER_GENERICS_TRAMPOLINE, NULL);
79
80         mono_tramp_info_register (mono_tramp_info_create (NULL, start, code - start, NULL, NULL));
81
82         return start;
83 }
84
85 gpointer
86 mono_arch_get_llvm_imt_trampoline (MonoDomain *domain, MonoMethod *m, int vt_offset)
87 {
88         guint8 *code, *start;
89         int buf_len;
90         int this_offset;
91
92         buf_len = 32;
93
94         start = code = mono_domain_code_reserve (domain, buf_len);
95
96         this_offset = mono_x86_get_this_arg_offset (mono_method_signature (m));
97
98         /* Set imt arg */
99         x86_mov_reg_imm (code, MONO_ARCH_IMT_REG, m);
100         /* Load this */
101         x86_mov_reg_membase (code, X86_EAX, X86_ESP, this_offset + 4, 4);
102         /* Load vtable address */
103         x86_mov_reg_membase (code, X86_EAX, X86_EAX, 0, 4);
104         x86_jump_membase (code, X86_EAX, vt_offset);
105
106         g_assert ((code - start) < buf_len);
107
108         nacl_domain_code_validate (domain, &start, buf_len, &code);
109
110         mono_arch_flush_icache (start, code - start);
111         mono_profiler_code_buffer_new (start, code - start, MONO_PROFILER_CODE_BUFFER_IMT_TRAMPOLINE, NULL);
112
113         return start;
114 }
115
116 void
117 mono_arch_patch_callsite (guint8 *method_start, guint8 *orig_code, guint8 *addr)
118 {
119 #if defined(__default_codegen__)
120         guint8 *code;
121         guint8 buf [8];
122         gboolean can_write = mono_breakpoint_clean_code (method_start, orig_code, 8, buf, sizeof (buf));
123
124         code = buf + 8;
125
126         /* go to the start of the call instruction
127          *
128          * address_byte = (m << 6) | (o << 3) | reg
129          * call opcode: 0xff address_byte displacement
130          * 0xff m=1,o=2 imm8
131          * 0xff m=2,o=2 imm32
132          */
133         code -= 6;
134         orig_code -= 6;
135         if (code [1] == 0xe8) {
136                 if (can_write) {
137                         InterlockedExchange ((gint32*)(orig_code + 2), (guint)addr - ((guint)orig_code + 1) - 5);
138
139                         /* Tell valgrind to recompile the patched code */
140                         VALGRIND_DISCARD_TRANSLATIONS (orig_code + 2, 4);
141                 }
142         } else if (code [1] == 0xe9) {
143                 /* A PLT entry: jmp <DISP> */
144                 if (can_write)
145                         InterlockedExchange ((gint32*)(orig_code + 2), (guint)addr - ((guint)orig_code + 1) - 5);
146         } else {
147                 printf ("Invalid trampoline sequence: %x %x %x %x %x %x %x\n", code [0], code [1], code [2], code [3],
148                                 code [4], code [5], code [6]);
149                 g_assert_not_reached ();
150         }
151 #elif defined(__native_client__)
152         /* Target must be bundle-aligned */
153         g_assert (((guint32)addr & kNaClAlignmentMask) == 0);
154
155         /* 0xe8 = call <DISP>, 0xe9 = jump <DISP> */
156         if ((orig_code [-5] == 0xe8) || orig_code [-6] == 0xe9) {
157                 int ret;
158                 gint32 offset = (gint32)addr - (gint32)orig_code;
159                 guint8 buf[sizeof(gint32)];
160                 *((gint32*)(buf)) = offset;
161                 ret = nacl_dyncode_modify (orig_code - sizeof(gint32), buf, sizeof(gint32));
162                 g_assert (ret == 0);
163         } else {
164                 printf ("Invalid trampoline sequence %p: %02x %02x %02x %02x %02x\n", orig_code, orig_code [-5], orig_code [-4], orig_code [-3], orig_code [-2], orig_code[-1]);
165                 g_assert_not_reached ();
166         }
167 #endif
168 }
169
170 void
171 mono_arch_patch_plt_entry (guint8 *code, gpointer *got, mgreg_t *regs, guint8 *addr)
172 {
173         guint32 offset;
174
175         /* Patch the jump table entry used by the plt entry */
176
177 #if defined(__native_client_codegen__) || defined(__native_client__)
178         /* for both compiler and runtime      */
179         /* A PLT entry:                       */
180         /*        mov <DISP>(%ebx), %ecx      */
181         /*        and 0xffffffe0, %ecx        */
182         /*        jmp *%ecx                   */
183         g_assert (code [0] == 0x8b);
184         g_assert (code [1] == 0x8b);
185
186         offset = *(guint32*)(code + 2);
187 #elif defined(__default_codegen__)
188         /* A PLT entry: jmp *<DISP>(%ebx) */
189         g_assert (code [0] == 0xff);
190         g_assert (code [1] == 0xa3);
191
192         offset = *(guint32*)(code + 2);
193 #endif  /* __native_client_codegen__ */
194         if (!got)
195                 got = (gpointer*)(gsize) regs [MONO_ARCH_GOT_REG];
196         *(guint8**)((guint8*)got + offset) = addr;
197 }
198
199 static gpointer
200 get_vcall_slot (guint8 *code, mgreg_t *regs, int *displacement)
201 {
202         const int kBufSize = NACL_SIZE (8, 16);
203         guint8 buf [64];
204         guint8 reg = 0;
205         gint32 disp = 0;
206
207         mono_breakpoint_clean_code (NULL, code, kBufSize, buf, kBufSize);
208         code = buf + 8;
209
210         *displacement = 0;
211
212         if ((code [0] == 0xff) && ((code [1] & 0x18) == 0x10) && ((code [1] >> 6) == 2)) {
213                 reg = code [1] & 0x07;
214                 disp = *((gint32*)(code + 2));
215 #if defined(__native_client_codegen__) || defined(__native_client__)
216         } else if ((code[1] == 0x83) && (code[2] == 0xe1) && (code[4] == 0xff) &&
217                            (code[5] == 0xd1) && (code[-5] == 0x8b)) {
218                 disp = *((gint32*)(code - 3));
219                 reg = code[-4] & 0x07;
220         } else if ((code[-2] == 0x8b) && (code[1] == 0x83) && (code[4] == 0xff)) {
221                 reg = code[-1] & 0x07;
222                 disp = (signed char)code[0];
223 #endif
224         } else {
225                 g_assert_not_reached ();
226                 return NULL;
227         }
228
229         *displacement = disp;
230         return (gpointer)regs [reg];
231 }
232
233 static gpointer*
234 get_vcall_slot_addr (guint8* code, mgreg_t *regs)
235 {
236         gpointer vt;
237         int displacement;
238         vt = get_vcall_slot (code, regs, &displacement);
239         if (!vt)
240                 return NULL;
241         return (gpointer*)((char*)vt + displacement);
242 }
243
244 guchar*
245 mono_arch_create_generic_trampoline (MonoTrampolineType tramp_type, MonoTrampInfo **info, gboolean aot)
246 {
247         char *tramp_name;
248         guint8 *buf, *code, *tramp;
249         GSList *unwind_ops = NULL;
250         MonoJumpInfo *ji = NULL;
251         int i, offset, frame_size, regarray_offset, lmf_offset, caller_ip_offset, arg_offset;
252
253         unwind_ops = mono_arch_get_cie_program ();
254
255         code = buf = mono_global_codeman_reserve (256);
256
257         /* Note that there is a single argument to the trampoline
258          * and it is stored at: esp + pushed_args * sizeof (gpointer)
259          * the ret address is at: esp + (pushed_args + 1) * sizeof (gpointer)
260          */
261
262         // FIXME: Unwind info
263
264         /* Compute frame offsets relative to the frame pointer %ebp */
265         arg_offset = sizeof (mgreg_t);
266         caller_ip_offset = 2 * sizeof (mgreg_t);
267         offset = 0;
268         offset += sizeof (MonoLMF);
269         lmf_offset = -offset;
270         offset += X86_NREG * sizeof (mgreg_t);
271         regarray_offset = -offset;
272         /* Argument area */
273         offset += 4 * sizeof (mgreg_t);
274         frame_size = ALIGN_TO (offset, MONO_ARCH_FRAME_ALIGNMENT);
275
276         /* Allocate frame */
277         x86_push_reg (code, X86_EBP);
278         x86_mov_reg_reg (code, X86_EBP, X86_ESP, sizeof (mgreg_t));
279         /* There are three words on the stack, adding + 4 aligns the stack to 16, which is needed on osx */
280         x86_alu_reg_imm (code, X86_SUB, X86_ESP, frame_size + sizeof (mgreg_t));
281
282         /* Save all registers */
283         for (i = X86_EAX; i <= X86_EDI; ++i) {
284                 int reg = i;
285
286                 if (i == X86_EBP) {
287                         /* Save original ebp */
288                         /* EAX is already saved */
289                         x86_mov_reg_membase (code, X86_EAX, X86_EBP, 0, sizeof (mgreg_t));
290                         reg = X86_EAX;
291                 } else if (i == X86_ESP) {
292                         /* Save original esp */
293                         /* EAX is already saved */
294                         x86_mov_reg_reg (code, X86_EAX, X86_EBP, sizeof (mgreg_t));
295                         /* Saved ebp + trampoline arg + return addr */
296                         x86_alu_reg_imm (code, X86_ADD, X86_EAX, 3 * sizeof (mgreg_t));
297                         reg = X86_EAX;
298                 }
299                 x86_mov_membase_reg (code, X86_EBP, regarray_offset + (i * sizeof (mgreg_t)), reg, sizeof (mgreg_t));
300         }
301
302         /* Setup LMF */
303         /* eip */
304         if (tramp_type == MONO_TRAMPOLINE_JUMP) {
305                 x86_mov_membase_imm (code, X86_EBP, lmf_offset + G_STRUCT_OFFSET (MonoLMF, eip), 0, sizeof (mgreg_t));
306         } else {
307                 x86_mov_reg_membase (code, X86_EAX, X86_EBP, caller_ip_offset, sizeof (mgreg_t));
308                 x86_mov_membase_reg (code, X86_EBP, lmf_offset + G_STRUCT_OFFSET (MonoLMF, eip), X86_EAX, sizeof (mgreg_t));
309         }
310         /* method */
311         if ((tramp_type == MONO_TRAMPOLINE_JIT) || (tramp_type == MONO_TRAMPOLINE_JUMP)) {
312                 x86_mov_reg_membase (code, X86_EAX, X86_EBP, arg_offset, sizeof (mgreg_t));
313                 x86_mov_membase_reg (code, X86_EBP, lmf_offset + G_STRUCT_OFFSET (MonoLMF, method), X86_EAX, sizeof (mgreg_t));
314         } else {
315                 x86_mov_membase_imm (code, X86_EBP, lmf_offset + G_STRUCT_OFFSET (MonoLMF, method), 0, sizeof (mgreg_t));
316         }
317         /* esp */
318         x86_mov_reg_membase (code, X86_EAX, X86_EBP, regarray_offset + (X86_ESP * sizeof (mgreg_t)), sizeof (mgreg_t));
319         x86_mov_membase_reg (code, X86_EBP, lmf_offset + G_STRUCT_OFFSET (MonoLMF, esp), X86_EAX, sizeof (mgreg_t));
320         /* callee save registers */
321         x86_mov_reg_membase (code, X86_EAX, X86_EBP, regarray_offset + (X86_EBX * sizeof (mgreg_t)), sizeof (mgreg_t));
322         x86_mov_membase_reg (code, X86_EBP, lmf_offset + G_STRUCT_OFFSET (MonoLMF, ebx), X86_EAX, sizeof (mgreg_t));
323         x86_mov_reg_membase (code, X86_EAX, X86_EBP, regarray_offset + (X86_EDI * sizeof (mgreg_t)), sizeof (mgreg_t));
324         x86_mov_membase_reg (code, X86_EBP, lmf_offset + G_STRUCT_OFFSET (MonoLMF, edi), X86_EAX, sizeof (mgreg_t));
325         x86_mov_reg_membase (code, X86_EAX, X86_EBP, regarray_offset + (X86_ESI * sizeof (mgreg_t)), sizeof (mgreg_t));
326         x86_mov_membase_reg (code, X86_EBP, lmf_offset + G_STRUCT_OFFSET (MonoLMF, esi), X86_EAX, sizeof (mgreg_t));
327         x86_mov_reg_membase (code, X86_EAX, X86_EBP, regarray_offset + (X86_EBP * sizeof (mgreg_t)), sizeof (mgreg_t));
328         x86_mov_membase_reg (code, X86_EBP, lmf_offset + G_STRUCT_OFFSET (MonoLMF, ebp), X86_EAX, sizeof (mgreg_t));
329
330         /* Push LMF */
331         /* get the address of lmf for the current thread */
332         if (aot) {
333                 code = mono_arch_emit_load_aotconst (buf, code, &ji, MONO_PATCH_INFO_JIT_ICALL_ADDR, "mono_get_lmf_addr");
334                 x86_call_reg (code, X86_EAX);
335         } else {
336                 x86_call_code (code, mono_get_lmf_addr);
337         }
338         /* lmf->lmf_addr = lmf_addr (%eax) */
339         x86_mov_membase_reg (code, X86_EBP, lmf_offset + G_STRUCT_OFFSET (MonoLMF, lmf_addr), X86_EAX, sizeof (mgreg_t));
340         /* lmf->previous_lmf = *(lmf_addr) */
341         x86_mov_reg_membase (code, X86_ECX, X86_EAX, 0, sizeof (mgreg_t));
342         /* Signal to mono_arch_unwind_frame () that this is a trampoline frame */
343         x86_alu_reg_imm (code, X86_ADD, X86_ECX, 1);
344         x86_mov_membase_reg (code, X86_EBP, lmf_offset + G_STRUCT_OFFSET (MonoLMF, previous_lmf), X86_ECX, sizeof (mgreg_t));
345         /* *lmf_addr = lmf */
346         x86_lea_membase (code, X86_ECX, X86_EBP, lmf_offset);
347         x86_mov_membase_reg (code, X86_EAX, 0, X86_ECX, sizeof (mgreg_t));
348
349         /* Call trampoline function */
350         /* Arg 1 - registers */
351         x86_lea_membase (code, X86_EAX, X86_EBP, regarray_offset);
352         x86_mov_membase_reg (code, X86_ESP, (0 * sizeof (mgreg_t)), X86_EAX, sizeof (mgreg_t));
353         /* Arg2 - calling code */
354         if (tramp_type == MONO_TRAMPOLINE_JUMP) {
355                 x86_mov_membase_imm (code, X86_ESP, (1 * sizeof (mgreg_t)), 0, sizeof (mgreg_t));
356         } else {
357                 x86_mov_reg_membase (code, X86_EAX, X86_EBP, caller_ip_offset, sizeof (mgreg_t));
358                 x86_mov_membase_reg (code, X86_ESP, (1 * sizeof (mgreg_t)), X86_EAX, sizeof (mgreg_t));
359         }
360         /* Arg3 - trampoline argument */
361         x86_mov_reg_membase (code, X86_EAX, X86_EBP, arg_offset, sizeof (mgreg_t));
362         x86_mov_membase_reg (code, X86_ESP, (2 * sizeof (mgreg_t)), X86_EAX, sizeof (mgreg_t));
363         /* Arg4 - trampoline address */
364         // FIXME:
365         x86_mov_membase_imm (code, X86_ESP, (3 * sizeof (mgreg_t)), 0, sizeof (mgreg_t));
366
367 #ifdef __APPLE__
368         /* check the stack is aligned after the ret ip is pushed */
369         /*
370         x86_mov_reg_reg (code, X86_EDX, X86_ESP, 4);
371         x86_alu_reg_imm (code, X86_AND, X86_EDX, 15);
372         x86_alu_reg_imm (code, X86_CMP, X86_EDX, 0);
373         x86_branch_disp (code, X86_CC_Z, 3, FALSE);
374         x86_breakpoint (code);
375         */
376 #endif
377
378         if (aot) {
379                 char *icall_name = g_strdup_printf ("trampoline_func_%d", tramp_type);
380                 code = mono_arch_emit_load_aotconst (buf, code, &ji, MONO_PATCH_INFO_JIT_ICALL_ADDR, icall_name);
381                 x86_call_reg (code, X86_EAX);
382         } else {
383                 tramp = (guint8*)mono_get_trampoline_func (tramp_type);
384                 x86_call_code (code, tramp);
385         }
386
387         /*
388          * Overwrite the trampoline argument with the address we need to jump to,
389          * to free %eax.
390          */
391         x86_mov_membase_reg (code, X86_EBP, arg_offset, X86_EAX, 4);
392
393         /* Check for interruptions */
394         if (aot) {
395                 code = mono_arch_emit_load_aotconst (buf, code, &ji, MONO_PATCH_INFO_JIT_ICALL_ADDR, "mono_thread_force_interruption_checkpoint");
396                 x86_call_reg (code, X86_EAX);
397         } else {
398                 x86_call_code (code, (guint8*)mono_thread_force_interruption_checkpoint);
399         }
400
401         /* Restore LMF */
402         x86_mov_reg_membase (code, X86_EAX, X86_EBP, lmf_offset + G_STRUCT_OFFSET (MonoLMF, lmf_addr), sizeof (mgreg_t));
403         x86_mov_reg_membase (code, X86_ECX, X86_EBP, lmf_offset + G_STRUCT_OFFSET (MonoLMF, previous_lmf), sizeof (mgreg_t));
404         x86_alu_reg_imm (code, X86_SUB, X86_ECX, 1);
405         x86_mov_membase_reg (code, X86_EAX, 0, X86_ECX, sizeof (mgreg_t));
406
407         /* Restore registers */
408         for (i = X86_EAX; i <= X86_EDI; ++i) {
409                 if (i == X86_ESP || i == X86_EBP)
410                         continue;
411                 if (i == X86_EAX && !((tramp_type == MONO_TRAMPOLINE_RESTORE_STACK_PROT) || (tramp_type == MONO_TRAMPOLINE_AOT_PLT)))
412                         continue;
413                 x86_mov_reg_membase (code, i, X86_EBP, regarray_offset + (i * 4), 4);
414         }
415
416         /* Restore frame */
417         x86_leave (code);
418
419         if (MONO_TRAMPOLINE_TYPE_MUST_RETURN (tramp_type)) {
420                 /* Load the value returned by the trampoline */
421                 x86_mov_reg_membase (code, X86_EAX, X86_ESP, 0, 4);
422                 /* The trampoline returns normally, pop the trampoline argument */
423                 x86_alu_reg_imm (code, X86_ADD, X86_ESP, 4);
424                 x86_ret (code);
425         } else {
426                 /* The trampoline argument is at the top of the stack, and it contains the address we need to branch to */
427                 if (tramp_type == MONO_TRAMPOLINE_HANDLER_BLOCK_GUARD) {
428                         x86_pop_reg (code, X86_EAX);
429                         x86_alu_reg_imm (code, X86_ADD, X86_ESP, 0x8);
430                         x86_jump_reg (code, X86_EAX);
431                 } else {
432                         x86_ret (code);
433                 }
434         }
435
436         nacl_global_codeman_validate (&buf, 256, &code);
437         g_assert ((code - buf) <= 256);
438         mono_profiler_code_buffer_new (buf, code - buf, MONO_PROFILER_CODE_BUFFER_HELPER, NULL);
439
440         tramp_name = mono_get_generic_trampoline_name (tramp_type);
441         *info = mono_tramp_info_create (tramp_name, buf, code - buf, ji, unwind_ops);
442         g_free (tramp_name);
443
444         return buf;
445 }
446
447 #define TRAMPOLINE_SIZE 10
448
449 gpointer
450 mono_arch_create_specific_trampoline (gpointer arg1, MonoTrampolineType tramp_type, MonoDomain *domain, guint32 *code_len)
451 {
452         guint8 *code, *buf, *tramp;
453         
454         tramp = mono_get_trampoline_code (tramp_type);
455
456         code = buf = mono_domain_code_reserve_align (domain, TRAMPOLINE_SIZE, NACL_SIZE (4, kNaClAlignment));
457
458         x86_push_imm (buf, arg1);
459         x86_jump_code (buf, tramp);
460         g_assert ((buf - code) <= TRAMPOLINE_SIZE);
461
462         nacl_domain_code_validate (domain, &code, NACL_SIZE (4, kNaClAlignment), &buf);
463
464         mono_arch_flush_icache (code, buf - code);
465         mono_profiler_code_buffer_new (code, buf - code, MONO_PROFILER_CODE_BUFFER_SPECIFIC_TRAMPOLINE, mono_get_generic_trampoline_simple_name (tramp_type));
466
467         if (code_len)
468                 *code_len = buf - code;
469
470         return code;
471 }
472
473 gpointer
474 mono_arch_create_rgctx_lazy_fetch_trampoline (guint32 slot, MonoTrampInfo **info, gboolean aot)
475 {
476         guint8 *tramp;
477         guint8 *code, *buf;
478         guint8 **rgctx_null_jumps;
479         int tramp_size;
480         int depth, index;
481         int i;
482         gboolean mrgctx;
483         MonoJumpInfo *ji = NULL;
484         GSList *unwind_ops = NULL;
485
486         unwind_ops = mono_arch_get_cie_program ();
487
488         mrgctx = MONO_RGCTX_SLOT_IS_MRGCTX (slot);
489         index = MONO_RGCTX_SLOT_INDEX (slot);
490         if (mrgctx)
491                 index += MONO_SIZEOF_METHOD_RUNTIME_GENERIC_CONTEXT / sizeof (gpointer);
492         for (depth = 0; ; ++depth) {
493                 int size = mono_class_rgctx_get_array_size (depth, mrgctx);
494
495                 if (index < size - 1)
496                         break;
497                 index -= size - 1;
498         }
499
500 #if defined(__default_codegen__)
501         tramp_size = (aot ? 64 : 36) + 6 * depth;
502 #elif defined(__native_client_codegen__)
503         tramp_size = (aot ? 64 : 36) + 2 * kNaClAlignment +
504           6 * (depth + kNaClAlignment);
505 #endif
506
507         code = buf = mono_global_codeman_reserve (tramp_size);
508
509         rgctx_null_jumps = g_malloc (sizeof (guint8*) * (depth + 2));
510
511         /* load vtable/mrgctx ptr */
512         x86_mov_reg_membase (code, X86_EAX, X86_ESP, 4, 4);
513         if (!mrgctx) {
514                 /* load rgctx ptr from vtable */
515                 x86_mov_reg_membase (code, X86_EAX, X86_EAX, MONO_STRUCT_OFFSET (MonoVTable, runtime_generic_context), 4);
516                 /* is the rgctx ptr null? */
517                 x86_test_reg_reg (code, X86_EAX, X86_EAX);
518                 /* if yes, jump to actual trampoline */
519                 rgctx_null_jumps [0] = code;
520                 x86_branch8 (code, X86_CC_Z, -1, 1);
521         }
522
523         for (i = 0; i < depth; ++i) {
524                 /* load ptr to next array */
525                 if (mrgctx && i == 0)
526                         x86_mov_reg_membase (code, X86_EAX, X86_EAX, MONO_SIZEOF_METHOD_RUNTIME_GENERIC_CONTEXT, 4);
527                 else
528                         x86_mov_reg_membase (code, X86_EAX, X86_EAX, 0, 4);
529                 /* is the ptr null? */
530                 x86_test_reg_reg (code, X86_EAX, X86_EAX);
531                 /* if yes, jump to actual trampoline */
532                 rgctx_null_jumps [i + 1] = code;
533                 x86_branch8 (code, X86_CC_Z, -1, 1);
534         }
535
536         /* fetch slot */
537         x86_mov_reg_membase (code, X86_EAX, X86_EAX, sizeof (gpointer) * (index + 1), 4);
538         /* is the slot null? */
539         x86_test_reg_reg (code, X86_EAX, X86_EAX);
540         /* if yes, jump to actual trampoline */
541         rgctx_null_jumps [depth + 1] = code;
542         x86_branch8 (code, X86_CC_Z, -1, 1);
543         /* otherwise return */
544         x86_ret (code);
545
546         for (i = mrgctx ? 1 : 0; i <= depth + 1; ++i)
547                 x86_patch (rgctx_null_jumps [i], code);
548
549         g_free (rgctx_null_jumps);
550
551         x86_mov_reg_membase (code, MONO_ARCH_VTABLE_REG, X86_ESP, 4, 4);
552
553         if (aot) {
554                 code = mono_arch_emit_load_aotconst (buf, code, &ji, MONO_PATCH_INFO_JIT_ICALL_ADDR, g_strdup_printf ("specific_trampoline_lazy_fetch_%u", slot));
555                 x86_jump_reg (code, X86_EAX);
556         } else {
557                 tramp = mono_arch_create_specific_trampoline (GUINT_TO_POINTER (slot), MONO_TRAMPOLINE_RGCTX_LAZY_FETCH, mono_get_root_domain (), NULL);
558
559                 /* jump to the actual trampoline */
560                 x86_jump_code (code, tramp);
561         }
562
563         nacl_global_codeman_validate (&buf, tramp_size, &code);
564         mono_arch_flush_icache (buf, code - buf);
565         mono_profiler_code_buffer_new (buf, code - buf, MONO_PROFILER_CODE_BUFFER_GENERICS_TRAMPOLINE, NULL);
566
567         g_assert (code - buf <= tramp_size);
568
569         char *name = mono_get_rgctx_fetch_trampoline_name (slot);
570         *info = mono_tramp_info_create (name, buf, code - buf, ji, unwind_ops);
571         g_free (name);
572
573         return buf;
574 }
575
576 /*
577  * mono_arch_create_general_rgctx_lazy_fetch_trampoline:
578  *
579  *   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
580  * the trampoline to call if the slot is not filled.
581  */
582 gpointer
583 mono_arch_create_general_rgctx_lazy_fetch_trampoline (MonoTrampInfo **info, gboolean aot)
584 {
585         guint8 *code, *buf;
586         int tramp_size;
587         MonoJumpInfo *ji = NULL;
588         GSList *unwind_ops = NULL;
589
590         g_assert (aot);
591
592         unwind_ops = mono_arch_get_cie_program ();
593
594         tramp_size = 64;
595
596         code = buf = mono_global_codeman_reserve (tramp_size);
597
598         // FIXME: Currently, we always go to the slow path.
599         
600         /* Load trampoline addr */
601         x86_mov_reg_membase (code, X86_EAX, MONO_ARCH_RGCTX_REG, 4, 4);
602         /* Load mrgctx/vtable */
603         x86_mov_reg_membase (code, MONO_ARCH_VTABLE_REG, X86_ESP, 4, 4);
604
605         x86_jump_reg (code, X86_EAX);
606
607         nacl_global_codeman_validate (&buf, tramp_size, &code);
608         mono_arch_flush_icache (buf, code - buf);
609         mono_profiler_code_buffer_new (buf, code - buf, MONO_PROFILER_CODE_BUFFER_GENERICS_TRAMPOLINE, NULL);
610
611         g_assert (code - buf <= tramp_size);
612
613         *info = mono_tramp_info_create ("rgctx_fetch_trampoline_general", buf, code - buf, ji, unwind_ops);
614
615         return buf;
616 }
617
618 #ifdef MONO_ARCH_MONITOR_OBJECT_REG
619 /*
620  * The code produced by this trampoline is equivalent to this:
621  *
622  * if (obj) {
623  *      if (obj->synchronisation) {
624  *              if (obj->synchronisation->owner == 0) {
625  *                      if (cmpxch (&obj->synchronisation->owner, TID, 0) == 0)
626  *                              return;
627  *              }
628  *              if (obj->synchronisation->owner == TID) {
629  *                      ++obj->synchronisation->nest;
630  *                      return;
631  *              }
632  *      }
633  * }
634  * return full_monitor_enter ();
635  *
636  */
637 gpointer
638 mono_arch_create_monitor_enter_trampoline (MonoTrampInfo **info, gboolean is_v4, gboolean aot)
639 {
640         guint8 *code, *buf;
641         guint8 *jump_obj_null, *jump_sync_null, *jump_other_owner, *jump_cmpxchg_failed, *jump_tid, *jump_sync_thin_hash = NULL;
642         guint8 *jump_lock_taken_true = NULL;
643         int tramp_size;
644         int status_offset, nest_offset;
645         MonoJumpInfo *ji = NULL;
646         GSList *unwind_ops = NULL;
647
648         g_assert (MONO_ARCH_MONITOR_OBJECT_REG == X86_EAX);
649 #ifdef MONO_ARCH_MONITOR_LOCK_TAKEN_REG
650         g_assert (MONO_ARCH_MONITOR_LOCK_TAKEN_REG == X86_EDX);
651 #else
652         g_assert (!is_v4);
653 #endif
654
655         mono_monitor_threads_sync_members_offset (&status_offset, &nest_offset);
656         g_assert (MONO_THREADS_SYNC_MEMBER_SIZE (status_offset) == sizeof (guint32));
657         g_assert (MONO_THREADS_SYNC_MEMBER_SIZE (nest_offset) == sizeof (guint32));
658         status_offset = MONO_THREADS_SYNC_MEMBER_OFFSET (status_offset);
659         nest_offset = MONO_THREADS_SYNC_MEMBER_OFFSET (nest_offset);
660
661         tramp_size = NACL_SIZE (128, 192);
662
663         code = buf = mono_global_codeman_reserve (tramp_size);
664
665         x86_push_reg (code, X86_EAX);
666         if (mono_thread_get_tls_offset () != -1) {
667                 if (is_v4) {
668                         x86_test_membase_imm (code, X86_EDX, 0, 1);
669                         /* if *lock_taken is 1, jump to actual trampoline */
670                         jump_lock_taken_true = code;
671                         x86_branch8 (code, X86_CC_NZ, -1, 1);
672                         x86_push_reg (code, X86_EDX);
673                 }
674                 /* MonoObject* obj is in EAX */
675                 /* is obj null? */
676                 x86_test_reg_reg (code, X86_EAX, X86_EAX);
677                 /* if yes, jump to actual trampoline */
678                 jump_obj_null = code;
679                 x86_branch8 (code, X86_CC_Z, -1, 1);
680
681                 /* load obj->synchronization to ECX */
682                 x86_mov_reg_membase (code, X86_ECX, X86_EAX, MONO_STRUCT_OFFSET (MonoObject, synchronisation), 4);
683
684                 if (mono_gc_is_moving ()) {
685                         /*if bit zero is set it's a thin hash*/
686                         /*FIXME use testb encoding*/
687                         x86_test_reg_imm (code, X86_ECX, 0x01);
688                         jump_sync_thin_hash = code;
689                         x86_branch8 (code, X86_CC_NE, -1, 1);
690
691                         /*clear bits used by the gc*/
692                         x86_alu_reg_imm (code, X86_AND, X86_ECX, ~0x3);
693                 }
694
695                 /* is synchronization null? */
696                 x86_test_reg_reg (code, X86_ECX, X86_ECX);
697
698                 /* if yes, jump to actual trampoline */
699                 jump_sync_null = code;
700                 x86_branch8 (code, X86_CC_Z, -1, 1);
701
702                 /* load MonoInternalThread* into EDX */
703                 if (aot) {
704                         /* load_aotconst () puts the result into EAX */
705                         x86_mov_reg_reg (code, X86_EDX, X86_EAX, sizeof (mgreg_t));
706                         code = mono_arch_emit_load_aotconst (buf, code, &ji, MONO_PATCH_INFO_TLS_OFFSET, GINT_TO_POINTER (TLS_KEY_THREAD));
707                         code = mono_x86_emit_tls_get_reg (code, X86_EAX, X86_EAX);
708                         x86_xchg_reg_reg (code, X86_EAX, X86_EDX, sizeof (mgreg_t));
709                 } else {
710                         code = mono_x86_emit_tls_get (code, X86_EDX, mono_thread_get_tls_offset ());
711                 }
712                 /* load TID into EDX */
713                 x86_mov_reg_membase (code, X86_EDX, X86_EDX, MONO_STRUCT_OFFSET (MonoInternalThread, small_id), 4);
714
715                 /* is synchronization->owner free */
716                 x86_mov_reg_membase (code, X86_EAX, X86_ECX, status_offset, 4);
717                 x86_test_reg_imm (code, X86_EAX, OWNER_MASK);
718                 /* if not, jump to next case */
719                 jump_tid = code;
720                 x86_branch8 (code, X86_CC_NZ, -1, 1);
721
722                 /* if yes, try a compare-exchange with the TID */
723                 /* Form new status */
724                 x86_alu_reg_reg (code, X86_OR, X86_EDX, X86_EAX);
725                 /* compare and exchange */
726                 x86_prefix (code, X86_LOCK_PREFIX);
727                 x86_cmpxchg_membase_reg (code, X86_ECX, status_offset, X86_EDX);
728                 /* if not successful, jump to actual trampoline */
729                 jump_cmpxchg_failed = code;
730                 x86_branch8 (code, X86_CC_NZ, -1, 1);
731                 /* if successful, pop and return */
732                 if (is_v4) {
733                         x86_pop_reg (code, X86_EDX);
734                         x86_mov_membase_imm (code, X86_EDX, 0, 1, 1);
735                 }
736                 x86_pop_reg (code, X86_EAX);
737                 x86_ret (code);
738
739                 /* next case: synchronization->owner is not null */
740                 x86_patch (jump_tid, code);
741                 /* is synchronization->owner == TID? */
742                 x86_alu_reg_imm (code, X86_AND, X86_EAX, OWNER_MASK);
743                 x86_alu_reg_reg (code, X86_CMP, X86_EAX, X86_EDX);
744                 /* if not, jump to actual trampoline */
745                 jump_other_owner = code;
746                 x86_branch8 (code, X86_CC_NZ, -1, 1);
747                 /* if yes, increment nest */
748                 x86_inc_membase (code, X86_ECX, nest_offset);
749                 if (is_v4) {
750                         x86_pop_reg (code, X86_EDX);
751                         x86_mov_membase_imm (code, X86_EDX, 0, 1, 1);
752                 }
753                 x86_pop_reg (code, X86_EAX);
754                 /* return */
755                 x86_ret (code);
756
757                 /* obj is pushed, jump to the actual trampoline */
758                 x86_patch (jump_obj_null, code);
759                 if (jump_sync_thin_hash)
760                         x86_patch (jump_sync_thin_hash, code);
761                 x86_patch (jump_sync_null, code);
762                 x86_patch (jump_other_owner, code);
763                 x86_patch (jump_cmpxchg_failed, code);
764
765                 if (is_v4) {
766                         x86_pop_reg (code, X86_EDX);
767                         x86_patch (jump_lock_taken_true, code);
768                 }
769         }
770
771         if (aot) {
772                 /* We are calling the generic trampoline directly, the argument is pushed
773                  * on the stack just like a specific trampoline.
774                  */
775                 if (is_v4)
776                         code = mono_arch_emit_load_aotconst (buf, code, &ji, MONO_PATCH_INFO_JIT_ICALL_ADDR, "generic_trampoline_monitor_enter_v4");
777                 else
778                         code = mono_arch_emit_load_aotconst (buf, code, &ji, MONO_PATCH_INFO_JIT_ICALL_ADDR, "generic_trampoline_monitor_enter");
779                 x86_jump_reg (code, X86_EAX);
780         } else {
781                 if (is_v4)
782                         x86_jump_code (code, mono_get_trampoline_code (MONO_TRAMPOLINE_MONITOR_ENTER_V4));
783                 else
784                         x86_jump_code (code, mono_get_trampoline_code (MONO_TRAMPOLINE_MONITOR_ENTER));
785         }
786
787         mono_arch_flush_icache (buf, code - buf);
788         g_assert (code - buf <= tramp_size);
789
790         nacl_global_codeman_validate (&buf, tramp_size, &code);
791         mono_profiler_code_buffer_new (buf, code - buf, MONO_PROFILER_CODE_BUFFER_MONITOR, NULL);
792
793         if (is_v4)
794                 *info = mono_tramp_info_create ("monitor_enter_v4_trampoline", buf, code - buf, ji, unwind_ops);
795         else
796                 *info = mono_tramp_info_create ("monitor_enter_trampoline", buf, code - buf, ji, unwind_ops);
797
798         return buf;
799 }
800
801 gpointer
802 mono_arch_create_monitor_exit_trampoline (MonoTrampInfo **info, gboolean aot)
803 {
804         guint8 *tramp = mono_get_trampoline_code (MONO_TRAMPOLINE_MONITOR_EXIT);
805         guint8 *code, *buf;
806         guint8 *jump_obj_null, *jump_have_waiters, *jump_sync_null, *jump_not_owned, *jump_sync_thin_hash = NULL;
807         guint8 *jump_next, *jump_cmpxchg_failed;
808         int tramp_size;
809         int status_offset, nest_offset;
810         MonoJumpInfo *ji = NULL;
811         GSList *unwind_ops = NULL;
812
813         g_assert (MONO_ARCH_MONITOR_OBJECT_REG == X86_EAX);
814
815         mono_monitor_threads_sync_members_offset (&status_offset, &nest_offset);
816         g_assert (MONO_THREADS_SYNC_MEMBER_SIZE (status_offset) == sizeof (guint32));
817         g_assert (MONO_THREADS_SYNC_MEMBER_SIZE (nest_offset) == sizeof (guint32));
818         status_offset = MONO_THREADS_SYNC_MEMBER_OFFSET (status_offset);
819         nest_offset = MONO_THREADS_SYNC_MEMBER_OFFSET (nest_offset);
820
821         tramp_size = NACL_SIZE (128, 192);
822
823         code = buf = mono_global_codeman_reserve (tramp_size);
824
825         x86_push_reg (code, X86_EAX);
826         if (mono_thread_get_tls_offset () != -1) {
827                 /* MonoObject* obj is in EAX */
828                 /* is obj null? */
829                 x86_test_reg_reg (code, X86_EAX, X86_EAX);
830                 /* if yes, jump to actual trampoline */
831                 jump_obj_null = code;
832                 x86_branch8 (code, X86_CC_Z, -1, 1);
833
834                 /* load obj->synchronization to ECX */
835                 x86_mov_reg_membase (code, X86_ECX, X86_EAX, MONO_STRUCT_OFFSET (MonoObject, synchronisation), 4);
836
837                 if (mono_gc_is_moving ()) {
838                         /*if bit zero is set it's a thin hash*/
839                         /*FIXME use testb encoding*/
840                         x86_test_reg_imm (code, X86_ECX, 0x01);
841                         jump_sync_thin_hash = code;
842                         x86_branch8 (code, X86_CC_NE, -1, 1);
843
844                         /*clear bits used by the gc*/
845                         x86_alu_reg_imm (code, X86_AND, X86_ECX, ~0x3);
846                 }
847
848                 /* is synchronization null? */
849                 x86_test_reg_reg (code, X86_ECX, X86_ECX);
850                 /* if yes, jump to actual trampoline */
851                 jump_sync_null = code;
852                 x86_branch8 (code, X86_CC_Z, -1, 1);
853
854                 /* next case: synchronization is not null */
855                 /* load MonoInternalThread* into EDX */
856                 if (aot) {
857                         /* load_aotconst () puts the result into EAX */
858                         x86_mov_reg_reg (code, X86_EDX, X86_EAX, sizeof (mgreg_t));
859                         code = mono_arch_emit_load_aotconst (buf, code, &ji, MONO_PATCH_INFO_TLS_OFFSET, GINT_TO_POINTER (TLS_KEY_THREAD));
860                         code = mono_x86_emit_tls_get_reg (code, X86_EAX, X86_EAX);
861                         x86_xchg_reg_reg (code, X86_EAX, X86_EDX, sizeof (mgreg_t));
862                 } else {
863                         code = mono_x86_emit_tls_get (code, X86_EDX, mono_thread_get_tls_offset ());
864                 }
865                 /* load TID into EDX */
866                 x86_mov_reg_membase (code, X86_EDX, X86_EDX, MONO_STRUCT_OFFSET (MonoInternalThread, small_id), 4);
867                 /* is synchronization->owner == TID */
868                 x86_mov_reg_membase (code, X86_EAX, X86_ECX, status_offset, 4);
869                 x86_alu_reg_reg (code, X86_XOR, X86_EDX, X86_EAX);
870                 x86_test_reg_imm (code, X86_EDX, OWNER_MASK);
871                 /* if no, jump to actual trampoline */
872                 jump_not_owned = code;
873                 x86_branch8 (code, X86_CC_NZ, -1, 1);
874
875                 /* next case: synchronization->owner == TID */
876                 /* is synchronization->nest == 1 */
877                 x86_alu_membase_imm (code, X86_CMP, X86_ECX, nest_offset, 1);
878                 /* if not, jump to next case */
879                 jump_next = code;
880                 x86_branch8 (code, X86_CC_NZ, -1, 1);
881                 /* if yes, is synchronization->entry_count greater than zero? */
882                 x86_test_reg_imm (code, X86_EAX, ENTRY_COUNT_WAITERS);
883                 /* if yes, jump to actual trampoline */
884                 jump_have_waiters = code;
885                 x86_branch8 (code, X86_CC_NZ, -1 , 1);
886                 /* if not, try to set synchronization->owner to null and return */
887                 x86_mov_reg_reg (code, X86_EDX, X86_EAX, 4);
888                 x86_alu_reg_imm (code, X86_AND, X86_EDX, ENTRY_COUNT_MASK); 
889                 /* compare and exchange */
890                 x86_prefix (code, X86_LOCK_PREFIX);
891                 /* EAX contains the previous status */
892                 x86_cmpxchg_membase_reg (code, X86_ECX, status_offset, X86_EDX);
893                 /* if not successful, jump to actual trampoline */
894                 jump_cmpxchg_failed = code;
895                 x86_branch8 (code, X86_CC_NZ, -1, 1);
896
897                 x86_pop_reg (code, X86_EAX);
898                 x86_ret (code);
899
900                 /* next case: synchronization->nest is not 1 */
901                 x86_patch (jump_next, code);
902                 /* decrease synchronization->nest and return */
903                 x86_dec_membase (code, X86_ECX, nest_offset);
904                 x86_pop_reg (code, X86_EAX);
905                 x86_ret (code);
906
907                 /* push obj and jump to the actual trampoline */
908                 x86_patch (jump_obj_null, code);
909                 if (jump_sync_thin_hash)
910                         x86_patch (jump_sync_thin_hash, code);
911                 x86_patch (jump_have_waiters, code);
912                 x86_patch (jump_cmpxchg_failed, code);
913                 x86_patch (jump_not_owned, code);
914                 x86_patch (jump_sync_null, code);
915         }
916
917         /* obj is pushed, jump to the actual trampoline */
918         if (aot) {
919                 code = mono_arch_emit_load_aotconst (buf, code, &ji, MONO_PATCH_INFO_JIT_ICALL_ADDR, "generic_trampoline_monitor_exit");
920                 x86_jump_reg (code, X86_EAX);
921         } else {
922                 x86_jump_code (code, tramp);
923         }
924
925         nacl_global_codeman_validate (&buf, tramp_size, &code);
926
927         mono_arch_flush_icache (buf, code - buf);
928         g_assert (code - buf <= tramp_size);
929         mono_profiler_code_buffer_new (buf, code - buf, MONO_PROFILER_CODE_BUFFER_MONITOR, NULL);
930
931         *info = mono_tramp_info_create ("monitor_exit_trampoline", buf, code - buf, ji, unwind_ops);
932
933         return buf;
934 }
935
936 #else
937
938 gpointer
939 mono_arch_create_monitor_enter_trampoline (MonoTrampInfo **info, gboolean is_v4, gboolean aot)
940 {
941         g_assert_not_reached ();
942         return NULL;
943 }
944
945 gpointer
946 mono_arch_create_monitor_exit_trampoline (MonoTrampInfo **info, gboolean aot)
947 {
948         g_assert_not_reached ();
949         return NULL;
950 }
951
952 #endif
953
954 void
955 mono_arch_invalidate_method (MonoJitInfo *ji, void *func, gpointer func_arg)
956 {
957         /* FIXME: This is not thread safe */
958         guint8 *code = ji->code_start;
959
960         x86_push_imm (code, func_arg);
961         x86_call_code (code, (guint8*)func);
962 }
963
964 static gpointer
965 handler_block_trampoline_helper (void)
966 {
967         MonoJitTlsData *jit_tls = mono_native_tls_get_value (mono_jit_tls_id);
968         return jit_tls->handler_block_return_address;
969 }
970
971 gpointer
972 mono_arch_create_handler_block_trampoline (MonoTrampInfo **info, gboolean aot)
973 {
974         guint8 *tramp = mono_get_trampoline_code (MONO_TRAMPOLINE_HANDLER_BLOCK_GUARD);
975         guint8 *code, *buf;
976         int tramp_size = 64;
977         MonoJumpInfo *ji = NULL;
978         GSList *unwind_ops = NULL;
979
980         g_assert (!aot);
981
982         code = buf = mono_global_codeman_reserve (tramp_size);
983
984         /*
985         This trampoline restore the call chain of the handler block then jumps into the code that deals with it.
986         */
987
988         /*
989          * We are in a method frame after the call emitted by OP_CALL_HANDLER.
990          */
991
992         if (mono_get_jit_tls_offset () != -1) {
993                 code = mono_x86_emit_tls_get (code, X86_EAX, mono_get_jit_tls_offset ());
994                 x86_mov_reg_membase (code, X86_EAX, X86_EAX, MONO_STRUCT_OFFSET (MonoJitTlsData, handler_block_return_address), 4);
995         } else {
996                 /*Slow path uses a c helper*/
997                 x86_call_code (code, handler_block_trampoline_helper);
998         }
999         /* Simulate a call */
1000         /*Fix stack alignment*/
1001         x86_alu_reg_imm (code, X86_SUB, X86_ESP, 0x4);
1002         /* This is the address the trampoline will return to */
1003         x86_push_reg (code, X86_EAX);
1004         /* Dummy trampoline argument, since we call the generic trampoline directly */
1005         x86_push_imm (code, 0);
1006         x86_jump_code (code, tramp);
1007
1008         nacl_global_codeman_validate (&buf, tramp_size, &code);
1009
1010         mono_arch_flush_icache (buf, code - buf);
1011         mono_profiler_code_buffer_new (buf, code - buf, MONO_PROFILER_CODE_BUFFER_HELPER, NULL);
1012         g_assert (code - buf <= tramp_size);
1013
1014         *info = mono_tramp_info_create ("handler_block_trampoline", buf, code - buf, ji, unwind_ops);
1015
1016         return buf;
1017 }
1018
1019 guint8*
1020 mono_arch_get_call_target (guint8 *code)
1021 {
1022         if (code [-5] == 0xe8) {
1023                 gint32 disp = *(gint32*)(code - 4);
1024                 guint8 *target = code + disp;
1025
1026                 return target;
1027         } else {
1028                 return NULL;
1029         }
1030 }
1031
1032 guint32
1033 mono_arch_get_plt_info_offset (guint8 *plt_entry, mgreg_t *regs, guint8 *code)
1034 {
1035         return *(guint32*)(plt_entry + NACL_SIZE (6, 12));
1036 }
1037
1038 /*
1039  * mono_arch_get_gsharedvt_arg_trampoline:
1040  *
1041  *   Return a trampoline which passes ARG to the gsharedvt in/out trampoline ADDR.
1042  */
1043 gpointer
1044 mono_arch_get_gsharedvt_arg_trampoline (MonoDomain *domain, gpointer arg, gpointer addr)
1045 {
1046         guint8 *code, *start;
1047         int buf_len;
1048
1049         buf_len = 10;
1050
1051         start = code = mono_domain_code_reserve (domain, buf_len);
1052
1053         x86_mov_reg_imm (code, X86_EAX, arg);
1054         x86_jump_code (code, addr);
1055         g_assert ((code - start) <= buf_len);
1056
1057         nacl_domain_code_validate (domain, &start, buf_len, &code);
1058         mono_arch_flush_icache (start, code - start);
1059         mono_profiler_code_buffer_new (start, code - start, MONO_PROFILER_CODE_BUFFER_GENERICS_TRAMPOLINE, NULL);
1060
1061         return start;
1062 }
1063
1064 #if defined(ENABLE_GSHAREDVT)
1065
1066 #include "../../../mono-extensions/mono/mini/tramp-x86-gsharedvt.c"
1067
1068 #else
1069
1070 gpointer
1071 mono_arch_get_gsharedvt_trampoline (MonoTrampInfo **info, gboolean aot)
1072 {
1073         *info = NULL;
1074         return NULL;
1075 }
1076
1077 #endif /* !MONOTOUCH */