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