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