Get rid of mono_arch_get_vcall_slot ().
[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/appdomain.h>
14 #include <mono/metadata/metadata-internals.h>
15 #include <mono/metadata/marshal.h>
16 #include <mono/metadata/tabledefs.h>
17 #include <mono/metadata/mono-debug.h>
18 #include <mono/metadata/mono-debug-debugger.h>
19 #include <mono/metadata/monitor.h>
20 #include <mono/arch/x86/x86-codegen.h>
21
22 #include <mono/utils/memcheck.h>
23
24 #include "mini.h"
25 #include "mini-x86.h"
26
27 static guint8* nullified_class_init_trampoline;
28
29 /*
30  * mono_arch_get_unbox_trampoline:
31  * @gsctx: the generic sharing context
32  * @m: method pointer
33  * @addr: pointer to native code for @m
34  *
35  * when value type methods are called through the vtable we need to unbox the
36  * this argument. This method returns a pointer to a trampoline which does
37  * unboxing before calling the method
38  */
39 gpointer
40 mono_arch_get_unbox_trampoline (MonoGenericSharingContext *gsctx, MonoMethod *m, gpointer addr)
41 {
42         guint8 *code, *start;
43         int this_pos = 4;
44         MonoDomain *domain = mono_domain_get ();
45
46         start = code = mono_domain_code_reserve (domain, 16);
47
48         x86_alu_membase_imm (code, X86_ADD, X86_ESP, this_pos, sizeof (MonoObject));
49         x86_jump_code (code, addr);
50         g_assert ((code - start) < 16);
51
52         return start;
53 }
54
55 gpointer
56 mono_arch_get_static_rgctx_trampoline (MonoMethod *m, MonoMethodRuntimeGenericContext *mrgctx, gpointer addr)
57 {
58         guint8 *code, *start;
59         int buf_len;
60
61         MonoDomain *domain = mono_domain_get ();
62
63         buf_len = 10;
64
65         start = code = mono_domain_code_reserve (domain, buf_len);
66
67         x86_mov_reg_imm (code, MONO_ARCH_RGCTX_REG, mrgctx);
68         x86_jump_code (code, addr);
69         g_assert ((code - start) <= buf_len);
70
71         mono_arch_flush_icache (start, code - start);
72
73         return start;
74 }
75
76 gpointer
77 mono_arch_get_llvm_imt_trampoline (MonoDomain *domain, MonoMethod *m, int vt_offset)
78 {
79         guint8 *code, *start;
80         int buf_len;
81         int this_offset;
82
83         buf_len = 32;
84
85         start = code = mono_domain_code_reserve (domain, buf_len);
86
87         this_offset = mono_x86_get_this_arg_offset (NULL, mono_method_signature (m));
88
89         /* Set imt arg */
90         x86_mov_reg_imm (code, MONO_ARCH_IMT_REG, m);
91         /* Load this */
92         x86_mov_reg_membase (code, X86_EAX, X86_ESP, this_offset + 4, 4);
93         /* Load vtable address */
94         x86_mov_reg_membase (code, X86_EAX, X86_EAX, 0, 4);
95         x86_jump_membase (code, X86_EAX, vt_offset);
96
97         g_assert ((code - start) < buf_len);
98
99         mono_arch_flush_icache (start, code - start);
100
101         return start;
102 }
103
104 void
105 mono_arch_patch_callsite (guint8 *method_start, guint8 *orig_code, guint8 *addr)
106 {
107         guint8 *code;
108         guint8 buf [8];
109         gboolean can_write = mono_breakpoint_clean_code (method_start, orig_code, 8, buf, sizeof (buf));
110
111         code = buf + 8;
112
113         /* go to the start of the call instruction
114          *
115          * address_byte = (m << 6) | (o << 3) | reg
116          * call opcode: 0xff address_byte displacement
117          * 0xff m=1,o=2 imm8
118          * 0xff m=2,o=2 imm32
119          */
120         code -= 6;
121         orig_code -= 6;
122         if ((code [1] == 0xe8)) {
123                 if (can_write) {
124                         InterlockedExchange ((gint32*)(orig_code + 2), (guint)addr - ((guint)orig_code + 1) - 5);
125
126                         /* Tell valgrind to recompile the patched code */
127                         VALGRIND_DISCARD_TRANSLATIONS (orig_code + 2, 4);
128                 }
129         } else if (code [1] == 0xe9) {
130                 /* A PLT entry: jmp <DISP> */
131                 if (can_write)
132                         InterlockedExchange ((gint32*)(orig_code + 2), (guint)addr - ((guint)orig_code + 1) - 5);
133         } else {
134                 printf ("Invalid trampoline sequence: %x %x %x %x %x %x %x\n", code [0], code [1], code [2], code [3],
135                                 code [4], code [5], code [6]);
136                 g_assert_not_reached ();
137         }
138 }
139
140 void
141 mono_arch_patch_plt_entry (guint8 *code, gpointer *got, mgreg_t *regs, guint8 *addr)
142 {
143         guint32 offset;
144
145         /* Patch the jump table entry used by the plt entry */
146
147         /* A PLT entry: jmp *<DISP>(%ebx) */
148         g_assert (code [0] == 0xff);
149         g_assert (code [1] == 0xa3);
150
151         offset = *(guint32*)(code + 2);
152
153         if (!got)
154                 got = (gpointer*)(gsize) regs [MONO_ARCH_GOT_REG];
155         *(guint8**)((guint8*)got + offset) = addr;
156 }
157
158 static gpointer
159 get_vcall_slot (guint8 *code, mgreg_t *regs, int *displacement)
160 {
161         guint8 buf [8];
162         guint8 reg = 0;
163         gint32 disp = 0;
164
165         mono_breakpoint_clean_code (NULL, code, 8, buf, sizeof (buf));
166         code = buf + 8;
167
168         *displacement = 0;
169
170         code -= 6;
171
172         if ((code [0] == 0xff) && ((code [1] & 0x18) == 0x10) && ((code [1] >> 6) == 2)) {
173                 reg = code [1] & 0x07;
174                 disp = *((gint32*)(code + 2));
175         } else {
176                 g_assert_not_reached ();
177                 return NULL;
178         }
179
180         *displacement = disp;
181         return (gpointer)regs [reg];
182 }
183
184 static gpointer*
185 get_vcall_slot_addr (guint8* code, mgreg_t *regs)
186 {
187         gpointer vt;
188         int displacement;
189         vt = get_vcall_slot (code, regs, &displacement);
190         if (!vt)
191                 return NULL;
192         return (gpointer*)((char*)vt + displacement);
193 }
194
195 void
196 mono_arch_nullify_class_init_trampoline (guint8 *code, mgreg_t *regs)
197 {
198         guint8 buf [16];
199         gboolean can_write = mono_breakpoint_clean_code (NULL, code, 6, buf, sizeof (buf));
200
201         if (!can_write)
202                 return;
203
204         code -= 5;
205         if (code [0] == 0xe8) {
206                 if (!mono_running_on_valgrind ()) {
207                         guint32 ops;
208                         /*
209                          * Thread safe code patching using the algorithm from the paper
210                          * 'Practicing JUDO: Java Under Dynamic Optimizations'
211                          */
212                         /* 
213                          * First atomically change the the first 2 bytes of the call to a
214                          * spinning jump.
215                          */
216                         ops = 0xfeeb;
217                         InterlockedExchange ((gint32*)code, ops);
218
219                         /* Then change the other bytes to a nop */
220                         code [2] = 0x90;
221                         code [3] = 0x90;
222                         code [4] = 0x90;
223
224                         /* Then atomically change the first 4 bytes to a nop as well */
225                         ops = 0x90909090;
226                         InterlockedExchange ((gint32*)code, ops);
227                         /* FIXME: the calltree skin trips on the self modifying code above */
228
229                         /* Tell valgrind to recompile the patched code */
230                         //VALGRIND_DISCARD_TRANSLATIONS (code, 8);
231                 }
232         } else if (code [0] == 0x90 || code [0] == 0xeb) {
233                 /* Already changed by another thread */
234                 ;
235         } else if ((code [-1] == 0xff) && (x86_modrm_reg (code [0]) == 0x2)) {
236                 /* call *<OFFSET>(<REG>) -> Call made from AOT code */
237                 gpointer *vtable_slot;
238
239                 vtable_slot = get_vcall_slot_addr (code + 5, regs);
240                 g_assert (vtable_slot);
241
242                 *vtable_slot = nullified_class_init_trampoline;
243         } else {
244                         printf ("Invalid trampoline sequence: %x %x %x %x %x %x %x\n", code [0], code [1], code [2], code [3],
245                                 code [4], code [5], code [6]);
246                         g_assert_not_reached ();
247                 }
248 }
249
250 void
251 mono_arch_nullify_plt_entry (guint8 *code, mgreg_t *regs)
252 {
253         if (mono_aot_only && !nullified_class_init_trampoline)
254                 nullified_class_init_trampoline = mono_aot_get_trampoline ("nullified_class_init_trampoline");
255
256         mono_arch_patch_plt_entry (code, NULL, regs, nullified_class_init_trampoline);
257 }
258
259 guchar*
260 mono_arch_create_generic_trampoline (MonoTrampolineType tramp_type, MonoTrampInfo **info, gboolean aot)
261 {
262         guint8 *buf, *code, *tramp;
263         int pushed_args, pushed_args_caller_saved;
264         GSList *unwind_ops = NULL;
265         MonoJumpInfo *ji = NULL;
266
267         unwind_ops = mono_arch_get_cie_program ();
268
269         code = buf = mono_global_codeman_reserve (256);
270
271         /* Note that there is a single argument to the trampoline
272          * and it is stored at: esp + pushed_args * sizeof (gpointer)
273          * the ret address is at: esp + (pushed_args + 1) * sizeof (gpointer)
274          */
275
276         /* Put all registers into an array on the stack
277          * If this code is changed, make sure to update the offset value in
278          * mono_arch_get_this_arg_from_call () in mini-x86.c.
279          */
280         x86_push_reg (code, X86_EDI);
281         x86_push_reg (code, X86_ESI);
282         x86_push_reg (code, X86_EBP);
283         x86_push_reg (code, X86_ESP);
284         x86_push_reg (code, X86_EBX);
285         x86_push_reg (code, X86_EDX);
286         x86_push_reg (code, X86_ECX);
287         x86_push_reg (code, X86_EAX);
288
289         pushed_args_caller_saved = pushed_args = 8;
290
291         /* Align stack on apple */
292         x86_alu_reg_imm (code, X86_SUB, X86_ESP, 4);
293
294         pushed_args ++;
295
296         /* save LMF begin */
297
298         /* save the IP (caller ip) */
299         if (tramp_type == MONO_TRAMPOLINE_JUMP)
300                 x86_push_imm (code, 0);
301         else
302                 x86_push_membase (code, X86_ESP, (pushed_args + 1) * sizeof (gpointer));
303
304         pushed_args++;
305
306         x86_push_reg (code, X86_EBP);
307         x86_push_reg (code, X86_ESI);
308         x86_push_reg (code, X86_EDI);
309         x86_push_reg (code, X86_EBX);
310
311         pushed_args += 4;
312
313         /* save ESP */
314         x86_push_reg (code, X86_ESP);
315         /* Adjust ESP so it points to the previous frame */
316         x86_alu_membase_imm (code, X86_ADD, X86_ESP, 0, (pushed_args + 2) * 4);
317
318         pushed_args ++;
319
320         /* save method info */
321         if ((tramp_type == MONO_TRAMPOLINE_JIT) || (tramp_type == MONO_TRAMPOLINE_JUMP))
322                 x86_push_membase (code, X86_ESP, pushed_args * sizeof (gpointer));
323         else
324                 x86_push_imm (code, 0);
325
326         pushed_args++;
327
328         /* On apple, the stack is correctly aligned to 16 bytes because pushed_args is
329          * 16 and there is the extra trampoline arg + the return ip pushed by call
330          * FIXME: Note that if an exception happens while some args are pushed
331          * on the stack, the stack will be misaligned.
332          */
333         g_assert (pushed_args == 16);
334
335         /* get the address of lmf for the current thread */
336         if (aot) {
337                 code = mono_arch_emit_load_aotconst (buf, code, &ji, MONO_PATCH_INFO_JIT_ICALL_ADDR, "mono_get_lmf_addr");
338                 x86_call_reg (code, X86_EAX);
339         } else {
340                 x86_call_code (code, mono_get_lmf_addr);
341         }
342         /* push lmf */
343         x86_push_reg (code, X86_EAX); 
344         /* push *lfm (previous_lmf) */
345         x86_push_membase (code, X86_EAX, 0);
346         /* Signal to mono_arch_find_jit_info () that this is a trampoline frame */
347         x86_alu_membase_imm (code, X86_ADD, X86_ESP, 0, 1);
348         /* *(lmf) = ESP */
349         x86_mov_membase_reg (code, X86_EAX, 0, X86_ESP, 4);
350         /* save LFM end */
351
352         pushed_args += 2;
353
354         /* starting the call sequence */
355
356         /* FIXME: Push the trampoline address */
357         x86_push_imm (code, 0);
358
359         pushed_args++;
360
361         /* push the method info */
362         x86_push_membase (code, X86_ESP, pushed_args * sizeof (gpointer));
363
364         pushed_args++;
365
366         /* push the return address onto the stack */
367         if (tramp_type == MONO_TRAMPOLINE_JUMP)
368                 x86_push_imm (code, 0);
369         else
370                 x86_push_membase (code, X86_ESP, (pushed_args + 1) * sizeof (gpointer));
371         pushed_args++;
372         /* push the address of the register array */
373         x86_lea_membase (code, X86_EAX, X86_ESP, (pushed_args - 8) * sizeof (gpointer));
374         x86_push_reg (code, X86_EAX);
375
376         pushed_args++;
377
378 #ifdef __APPLE__
379         /* check the stack is aligned after the ret ip is pushed */
380         /*x86_mov_reg_reg (buf, X86_EDX, X86_ESP, 4);
381         x86_alu_reg_imm (buf, X86_AND, X86_EDX, 15);
382         x86_alu_reg_imm (buf, X86_CMP, X86_EDX, 0);
383         x86_branch_disp (buf, X86_CC_Z, 3, FALSE);
384         x86_breakpoint (buf);*/
385 #endif
386
387         mono_add_unwind_op_def_cfa (unwind_ops, code, buf, X86_ESP, ((pushed_args + 2) * 4));
388
389         if (aot) {
390                 char *icall_name = g_strdup_printf ("trampoline_func_%d", tramp_type);
391                 code = mono_arch_emit_load_aotconst (buf, code, &ji, MONO_PATCH_INFO_JIT_ICALL_ADDR, icall_name);
392                 x86_call_reg (code, X86_EAX);
393         } else {
394                 tramp = (guint8*)mono_get_trampoline_func (tramp_type);
395                 x86_call_code (code, tramp);
396         }
397
398         x86_alu_reg_imm (code, X86_ADD, X86_ESP, 4*4);
399
400         pushed_args -= 4;
401
402         /* Check for thread interruption */
403         /* This is not perf critical code so no need to check the interrupt flag */
404         /* Align the stack on osx */
405         x86_alu_reg_imm (code, X86_SUB, X86_ESP, 3 * 4);
406         x86_push_reg (code, X86_EAX);
407         if (aot) {
408                 code = mono_arch_emit_load_aotconst (buf, code, &ji, MONO_PATCH_INFO_JIT_ICALL_ADDR, "mono_thread_force_interruption_checkpoint");
409                 x86_call_reg (code, X86_EAX);
410         } else {
411                 x86_call_code (code, (guint8*)mono_thread_force_interruption_checkpoint);
412         }
413         x86_pop_reg (code, X86_EAX);
414         x86_alu_reg_imm (code, X86_ADD, X86_ESP, 3 * 4);
415
416         /* Restore LMF */
417
418         /* ebx = previous_lmf */
419         x86_pop_reg (code, X86_EBX);
420         pushed_args--;
421         x86_alu_reg_imm (code, X86_SUB, X86_EBX, 1);
422
423         /* edi = lmf */
424         x86_pop_reg (code, X86_EDI);
425         pushed_args--;
426
427         /* *(lmf) = previous_lmf */
428         x86_mov_membase_reg (code, X86_EDI, 0, X86_EBX, 4);
429
430         /* discard method info */
431         x86_pop_reg (code, X86_ESI);
432         pushed_args--;
433
434         /* discard ESP */
435         x86_pop_reg (code, X86_ESI);
436         pushed_args--;
437
438         /* restore caller saved regs */
439         x86_pop_reg (code, X86_EBX);
440         x86_pop_reg (code, X86_EDI);
441         x86_pop_reg (code, X86_ESI);
442         x86_pop_reg (code, X86_EBP);
443
444         pushed_args -= 4;
445
446         /* discard save IP */
447         x86_alu_reg_imm (code, X86_ADD, X86_ESP, 4);
448         pushed_args--;
449
450         /* restore LMF end */
451
452         if (!MONO_TRAMPOLINE_TYPE_MUST_RETURN (tramp_type)) {
453                 /* 
454                  * Overwrite the method ptr with the address we need to jump to,
455                  * to free %eax.
456                  */
457                 x86_mov_membase_reg (code, X86_ESP, pushed_args * sizeof (gpointer), X86_EAX, 4);
458         }
459
460         /* Restore caller saved registers */
461         x86_mov_reg_membase (code, X86_ECX, X86_ESP, (pushed_args - pushed_args_caller_saved + X86_ECX) * 4, 4);
462         x86_mov_reg_membase (code, X86_EDX, X86_ESP, (pushed_args - pushed_args_caller_saved + X86_EDX) * 4, 4);
463         if ((tramp_type == MONO_TRAMPOLINE_RESTORE_STACK_PROT) || (tramp_type == MONO_TRAMPOLINE_AOT_PLT))
464                 x86_mov_reg_membase (code, X86_EAX, X86_ESP, (pushed_args - pushed_args_caller_saved + X86_EAX) * 4, 4);
465
466         if (!MONO_TRAMPOLINE_TYPE_MUST_RETURN (tramp_type)) {
467                 /* Pop saved reg array + stack align */
468                 x86_alu_reg_imm (code, X86_ADD, X86_ESP, 9 * 4);
469                 pushed_args -= 9;
470                 g_assert (pushed_args == 0);
471         } else {
472                 /* Pop saved reg array + stack align + method ptr */
473                 x86_alu_reg_imm (code, X86_ADD, X86_ESP, 10 * 4);
474                 pushed_args -= 10;
475
476                 /* We've popped one more stack item than we've pushed (the
477                    method ptr argument), so we must end up at -1. */
478                 g_assert (pushed_args == -1);
479         }
480
481         x86_ret (code);
482
483         g_assert ((code - buf) <= 256);
484
485         if (info)
486                 *info = mono_tramp_info_create (mono_get_generic_trampoline_name (tramp_type), buf, code - buf, ji, unwind_ops);
487
488         if (tramp_type == MONO_TRAMPOLINE_CLASS_INIT) {
489                 /* Initialize the nullified class init trampoline used in the AOT case */
490                 nullified_class_init_trampoline = mono_arch_get_nullified_class_init_trampoline (NULL);
491         }
492
493         return buf;
494 }
495
496 gpointer
497 mono_arch_get_nullified_class_init_trampoline (MonoTrampInfo **info)
498 {
499         guint8 *code, *buf;
500
501         code = buf = mono_global_codeman_reserve (16);
502         x86_ret (code);
503
504         mono_arch_flush_icache (buf, code - buf);
505
506         if (info)
507                 *info = mono_tramp_info_create (g_strdup_printf ("nullified_class_init_trampoline"), buf, code - buf, NULL, NULL);
508
509         return buf;
510 }
511
512 #define TRAMPOLINE_SIZE 10
513
514 gpointer
515 mono_arch_create_specific_trampoline (gpointer arg1, MonoTrampolineType tramp_type, MonoDomain *domain, guint32 *code_len)
516 {
517         guint8 *code, *buf, *tramp;
518         
519         tramp = mono_get_trampoline_code (tramp_type);
520
521         code = buf = mono_domain_code_reserve_align (domain, TRAMPOLINE_SIZE, 4);
522
523         x86_push_imm (buf, arg1);
524         x86_jump_code (buf, tramp);
525         g_assert ((buf - code) <= TRAMPOLINE_SIZE);
526
527         mono_arch_flush_icache (code, buf - code);
528
529         if (code_len)
530                 *code_len = buf - code;
531
532         return code;
533 }
534
535 gpointer
536 mono_arch_create_rgctx_lazy_fetch_trampoline (guint32 slot, MonoTrampInfo **info, gboolean aot)
537 {
538         guint8 *tramp;
539         guint8 *code, *buf;
540         guint8 **rgctx_null_jumps;
541         int tramp_size;
542         int depth, index;
543         int i;
544         gboolean mrgctx;
545         MonoJumpInfo *ji = NULL;
546         GSList *unwind_ops = NULL;
547
548         unwind_ops = mono_arch_get_cie_program ();
549
550         mrgctx = MONO_RGCTX_SLOT_IS_MRGCTX (slot);
551         index = MONO_RGCTX_SLOT_INDEX (slot);
552         if (mrgctx)
553                 index += MONO_SIZEOF_METHOD_RUNTIME_GENERIC_CONTEXT / sizeof (gpointer);
554         for (depth = 0; ; ++depth) {
555                 int size = mono_class_rgctx_get_array_size (depth, mrgctx);
556
557                 if (index < size - 1)
558                         break;
559                 index -= size - 1;
560         }
561
562         tramp_size = (aot ? 64 : 36) + 6 * depth;
563
564         code = buf = mono_global_codeman_reserve (tramp_size);
565
566         rgctx_null_jumps = g_malloc (sizeof (guint8*) * (depth + 2));
567
568         /* load vtable/mrgctx ptr */
569         x86_mov_reg_membase (code, X86_EAX, X86_ESP, 4, 4);
570         if (!mrgctx) {
571                 /* load rgctx ptr from vtable */
572                 x86_mov_reg_membase (code, X86_EAX, X86_EAX, G_STRUCT_OFFSET (MonoVTable, runtime_generic_context), 4);
573                 /* is the rgctx ptr null? */
574                 x86_test_reg_reg (code, X86_EAX, X86_EAX);
575                 /* if yes, jump to actual trampoline */
576                 rgctx_null_jumps [0] = code;
577                 x86_branch8 (code, X86_CC_Z, -1, 1);
578         }
579
580         for (i = 0; i < depth; ++i) {
581                 /* load ptr to next array */
582                 if (mrgctx && i == 0)
583                         x86_mov_reg_membase (code, X86_EAX, X86_EAX, MONO_SIZEOF_METHOD_RUNTIME_GENERIC_CONTEXT, 4);
584                 else
585                         x86_mov_reg_membase (code, X86_EAX, X86_EAX, 0, 4);
586                 /* is the ptr null? */
587                 x86_test_reg_reg (code, X86_EAX, X86_EAX);
588                 /* if yes, jump to actual trampoline */
589                 rgctx_null_jumps [i + 1] = code;
590                 x86_branch8 (code, X86_CC_Z, -1, 1);
591         }
592
593         /* fetch slot */
594         x86_mov_reg_membase (code, X86_EAX, X86_EAX, sizeof (gpointer) * (index + 1), 4);
595         /* is the slot null? */
596         x86_test_reg_reg (code, X86_EAX, X86_EAX);
597         /* if yes, jump to actual trampoline */
598         rgctx_null_jumps [depth + 1] = code;
599         x86_branch8 (code, X86_CC_Z, -1, 1);
600         /* otherwise return */
601         x86_ret (code);
602
603         for (i = mrgctx ? 1 : 0; i <= depth + 1; ++i)
604                 x86_patch (rgctx_null_jumps [i], code);
605
606         g_free (rgctx_null_jumps);
607
608         x86_mov_reg_membase (code, MONO_ARCH_VTABLE_REG, X86_ESP, 4, 4);
609
610         if (aot) {
611                 code = mono_arch_emit_load_aotconst (buf, code, &ji, MONO_PATCH_INFO_JIT_ICALL_ADDR, g_strdup_printf ("specific_trampoline_lazy_fetch_%u", slot));
612                 x86_jump_reg (code, X86_EAX);
613         } else {
614                 tramp = mono_arch_create_specific_trampoline (GUINT_TO_POINTER (slot), MONO_TRAMPOLINE_RGCTX_LAZY_FETCH, mono_get_root_domain (), NULL);
615
616                 /* jump to the actual trampoline */
617                 x86_jump_code (code, tramp);
618         }
619
620         mono_arch_flush_icache (buf, code - buf);
621
622         g_assert (code - buf <= tramp_size);
623
624         if (info)
625                 *info = mono_tramp_info_create (mono_get_rgctx_fetch_trampoline_name (slot), buf, code - buf, ji, unwind_ops);
626
627         return buf;
628 }
629
630 gpointer
631 mono_arch_create_generic_class_init_trampoline (MonoTrampInfo **info, gboolean aot)
632 {
633         guint8 *tramp;
634         guint8 *code, *buf;
635         static int byte_offset = -1;
636         static guint8 bitmask;
637         guint8 *jump;
638         int tramp_size;
639         GSList *unwind_ops = NULL;
640         MonoJumpInfo *ji = NULL;
641
642         tramp_size = 64;
643
644         code = buf = mono_global_codeman_reserve (tramp_size);
645
646         unwind_ops = mono_arch_get_cie_program ();
647
648         if (byte_offset < 0)
649                 mono_marshal_find_bitfield_offset (MonoVTable, initialized, &byte_offset, &bitmask);
650
651         x86_test_membase_imm (code, MONO_ARCH_VTABLE_REG, byte_offset, bitmask);
652         jump = code;
653         x86_branch8 (code, X86_CC_Z, -1, 1);
654
655         x86_ret (code);
656
657         x86_patch (jump, code);
658
659         /* Push the vtable so the stack is the same as in a specific trampoline */
660         x86_push_reg (code, MONO_ARCH_VTABLE_REG);
661
662         if (aot) {
663                 code = mono_arch_emit_load_aotconst (buf, code, &ji, MONO_PATCH_INFO_JIT_ICALL_ADDR, "generic_trampoline_generic_class_init");
664                 x86_jump_reg (code, X86_EAX);
665         } else {
666                 tramp = mono_get_trampoline_code (MONO_TRAMPOLINE_GENERIC_CLASS_INIT);
667
668                 /* jump to the actual trampoline */
669                 x86_jump_code (code, tramp);
670         }
671
672         mono_arch_flush_icache (code, code - buf);
673
674         g_assert (code - buf <= tramp_size);
675
676         if (info)
677                 *info = mono_tramp_info_create (g_strdup_printf ("generic_class_init_trampoline"), buf, code - buf, ji, unwind_ops);
678
679         return buf;
680 }
681
682 #ifdef MONO_ARCH_MONITOR_OBJECT_REG
683 /*
684  * The code produced by this trampoline is equivalent to this:
685  *
686  * if (obj) {
687  *      if (obj->synchronisation) {
688  *              if (obj->synchronisation->owner == 0) {
689  *                      if (cmpxch (&obj->synchronisation->owner, TID, 0) == 0)
690  *                              return;
691  *              }
692  *              if (obj->synchronisation->owner == TID) {
693  *                      ++obj->synchronisation->nest;
694  *                      return;
695  *              }
696  *      }
697  * }
698  * return full_monitor_enter ();
699  *
700  */
701 gpointer
702 mono_arch_create_monitor_enter_trampoline (MonoTrampInfo **info, gboolean aot)
703 {
704         guint8 *tramp = mono_get_trampoline_code (MONO_TRAMPOLINE_MONITOR_ENTER);
705         guint8 *code, *buf;
706         guint8 *jump_obj_null, *jump_sync_null, *jump_other_owner, *jump_cmpxchg_failed, *jump_tid;
707         int tramp_size;
708         int owner_offset, nest_offset, dummy;
709         MonoJumpInfo *ji = NULL;
710         GSList *unwind_ops = NULL;
711
712         g_assert (MONO_ARCH_MONITOR_OBJECT_REG == X86_EAX);
713
714         mono_monitor_threads_sync_members_offset (&owner_offset, &nest_offset, &dummy);
715         g_assert (MONO_THREADS_SYNC_MEMBER_SIZE (owner_offset) == sizeof (gpointer));
716         g_assert (MONO_THREADS_SYNC_MEMBER_SIZE (nest_offset) == sizeof (guint32));
717         owner_offset = MONO_THREADS_SYNC_MEMBER_OFFSET (owner_offset);
718         nest_offset = MONO_THREADS_SYNC_MEMBER_OFFSET (nest_offset);
719
720         tramp_size = 64;
721
722         code = buf = mono_global_codeman_reserve (tramp_size);
723
724         if (mono_thread_get_tls_offset () != -1) {
725                 /* MonoObject* obj is in EAX */
726                 /* is obj null? */
727                 x86_test_reg_reg (code, X86_EAX, X86_EAX);
728                 /* if yes, jump to actual trampoline */
729                 jump_obj_null = code;
730                 x86_branch8 (code, X86_CC_Z, -1, 1);
731
732                 /* load obj->synchronization to ECX */
733                 x86_mov_reg_membase (code, X86_ECX, X86_EAX, G_STRUCT_OFFSET (MonoObject, synchronisation), 4);
734                 /* is synchronization null? */
735                 x86_test_reg_reg (code, X86_ECX, X86_ECX);
736                 /* if yes, jump to actual trampoline */
737                 jump_sync_null = code;
738                 x86_branch8 (code, X86_CC_Z, -1, 1);
739
740                 /* load MonoInternalThread* into EDX */
741                 code = mono_x86_emit_tls_get (code, X86_EDX, mono_thread_get_tls_offset ());
742                 /* load TID into EDX */
743                 x86_mov_reg_membase (code, X86_EDX, X86_EDX, G_STRUCT_OFFSET (MonoInternalThread, tid), 4);
744
745                 /* is synchronization->owner null? */
746                 x86_alu_membase_imm (code, X86_CMP, X86_ECX, owner_offset, 0);
747                 /* if not, jump to next case */
748                 jump_tid = code;
749                 x86_branch8 (code, X86_CC_NZ, -1, 1);
750
751                 /* if yes, try a compare-exchange with the TID */
752                 /* free up register EAX, needed for the zero */
753                 x86_push_reg (code, X86_EAX);
754                 /* zero EAX */
755                 x86_alu_reg_reg (code, X86_XOR, X86_EAX, X86_EAX);
756                 /* compare and exchange */
757                 x86_prefix (code, X86_LOCK_PREFIX);
758                 x86_cmpxchg_membase_reg (code, X86_ECX, owner_offset, X86_EDX);
759                 /* if not successful, jump to actual trampoline */
760                 jump_cmpxchg_failed = code;
761                 x86_branch8 (code, X86_CC_NZ, -1, 1);
762                 /* if successful, pop and return */
763                 x86_pop_reg (code, X86_EAX);
764                 x86_ret (code);
765
766                 /* next case: synchronization->owner is not null */
767                 x86_patch (jump_tid, code);
768                 /* is synchronization->owner == TID? */
769                 x86_alu_membase_reg (code, X86_CMP, X86_ECX, owner_offset, X86_EDX);
770                 /* if not, jump to actual trampoline */
771                 jump_other_owner = code;
772                 x86_branch8 (code, X86_CC_NZ, -1, 1);
773                 /* if yes, increment nest */
774                 x86_inc_membase (code, X86_ECX, nest_offset);
775                 /* return */
776                 x86_ret (code);
777
778                 /* push obj */
779                 x86_patch (jump_obj_null, code);
780                 x86_patch (jump_sync_null, code);
781                 x86_patch (jump_other_owner, code);
782                 x86_push_reg (code, X86_EAX);
783                 /* jump to the actual trampoline */
784                 x86_patch (jump_cmpxchg_failed, code);
785                 if (aot) {
786                         /* We are calling the generic trampoline directly, the argument is pushed
787                          * on the stack just like a specific trampoline.
788                          */
789                         code = mono_arch_emit_load_aotconst (buf, code, &ji, MONO_PATCH_INFO_JIT_ICALL_ADDR, "generic_trampoline_monitor_enter");
790                         x86_jump_reg (code, X86_EAX);
791                 } else {
792                         x86_jump_code (code, tramp);
793                 }
794         } else {
795                 /* push obj and jump to the actual trampoline */
796                 x86_push_reg (code, X86_EAX);
797                 if (aot) {
798                         code = mono_arch_emit_load_aotconst (buf, code, &ji, MONO_PATCH_INFO_JIT_ICALL_ADDR, "generic_trampoline_monitor_enter");
799                         x86_jump_reg (code, X86_EAX);
800                 } else {
801                         x86_jump_code (code, tramp);
802                 }
803         }
804
805         mono_arch_flush_icache (buf, code - buf);
806         g_assert (code - buf <= tramp_size);
807
808         if (info)
809                 *info = mono_tramp_info_create (g_strdup_printf ("monitor_enter_trampoline"), buf, code - buf, ji, unwind_ops);
810
811         return buf;
812 }
813
814 gpointer
815 mono_arch_create_monitor_exit_trampoline (MonoTrampInfo **info, gboolean aot)
816 {
817         guint8 *tramp = mono_get_trampoline_code (MONO_TRAMPOLINE_MONITOR_EXIT);
818         guint8 *code, *buf;
819         guint8 *jump_obj_null, *jump_have_waiters, *jump_sync_null, *jump_not_owned;
820         guint8 *jump_next;
821         int tramp_size;
822         int owner_offset, nest_offset, entry_count_offset;
823         MonoJumpInfo *ji = NULL;
824         GSList *unwind_ops = NULL;
825
826         g_assert (MONO_ARCH_MONITOR_OBJECT_REG == X86_EAX);
827
828         mono_monitor_threads_sync_members_offset (&owner_offset, &nest_offset, &entry_count_offset);
829         g_assert (MONO_THREADS_SYNC_MEMBER_SIZE (owner_offset) == sizeof (gpointer));
830         g_assert (MONO_THREADS_SYNC_MEMBER_SIZE (nest_offset) == sizeof (guint32));
831         g_assert (MONO_THREADS_SYNC_MEMBER_SIZE (entry_count_offset) == sizeof (gint32));
832         owner_offset = MONO_THREADS_SYNC_MEMBER_OFFSET (owner_offset);
833         nest_offset = MONO_THREADS_SYNC_MEMBER_OFFSET (nest_offset);
834         entry_count_offset = MONO_THREADS_SYNC_MEMBER_OFFSET (entry_count_offset);
835
836         tramp_size = 64;
837
838         code = buf = mono_global_codeman_reserve (tramp_size);
839
840         if (mono_thread_get_tls_offset () != -1) {
841                 /* MonoObject* obj is in EAX */
842                 /* is obj null? */
843                 x86_test_reg_reg (code, X86_EAX, X86_EAX);
844                 /* if yes, jump to actual trampoline */
845                 jump_obj_null = code;
846                 x86_branch8 (code, X86_CC_Z, -1, 1);
847
848                 /* load obj->synchronization to ECX */
849                 x86_mov_reg_membase (code, X86_ECX, X86_EAX, G_STRUCT_OFFSET (MonoObject, synchronisation), 4);
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                 code = mono_x86_emit_tls_get (code, X86_EDX, mono_thread_get_tls_offset ());
859                 /* load TID into EDX */
860                 x86_mov_reg_membase (code, X86_EDX, X86_EDX, G_STRUCT_OFFSET (MonoInternalThread, tid), 4);
861                 /* is synchronization->owner == TID */
862                 x86_alu_membase_reg (code, X86_CMP, X86_ECX, owner_offset, X86_EDX);
863                 /* if no, jump to actual trampoline */
864                 jump_not_owned = code;
865                 x86_branch8 (code, X86_CC_NZ, -1, 1);
866
867                 /* next case: synchronization->owner == TID */
868                 /* is synchronization->nest == 1 */
869                 x86_alu_membase_imm (code, X86_CMP, X86_ECX, nest_offset, 1);
870                 /* if not, jump to next case */
871                 jump_next = code;
872                 x86_branch8 (code, X86_CC_NZ, -1, 1);
873                 /* if yes, is synchronization->entry_count zero? */
874                 x86_alu_membase_imm (code, X86_CMP, X86_ECX, entry_count_offset, 0);
875                 /* if not, jump to actual trampoline */
876                 jump_have_waiters = code;
877                 x86_branch8 (code, X86_CC_NZ, -1 , 1);
878                 /* if yes, set synchronization->owner to null and return */
879                 x86_mov_membase_imm (code, X86_ECX, owner_offset, 0, 4);
880                 x86_ret (code);
881
882                 /* next case: synchronization->nest is not 1 */
883                 x86_patch (jump_next, code);
884                 /* decrease synchronization->nest and return */
885                 x86_dec_membase (code, X86_ECX, nest_offset);
886                 x86_ret (code);
887
888                 /* push obj and jump to the actual trampoline */
889                 x86_patch (jump_obj_null, code);
890                 x86_patch (jump_have_waiters, code);
891                 x86_patch (jump_not_owned, code);
892                 x86_patch (jump_sync_null, code);
893         }
894
895         /* push obj and jump to the actual trampoline */
896         x86_push_reg (code, X86_EAX);
897         if (aot) {
898                 code = mono_arch_emit_load_aotconst (buf, code, &ji, MONO_PATCH_INFO_JIT_ICALL_ADDR, "generic_trampoline_monitor_exit");
899                 x86_jump_reg (code, X86_EAX);
900         } else {
901                 x86_jump_code (code, tramp);
902         }
903
904         mono_arch_flush_icache (buf, code - buf);
905         g_assert (code - buf <= tramp_size);
906
907         if (info)
908                 *info = mono_tramp_info_create (g_strdup_printf ("monitor_exit_trampoline"), buf, code - buf, ji, unwind_ops);
909
910         return buf;
911 }
912
913 #else
914
915 gpointer
916 mono_arch_create_monitor_enter_trampoline (MonoTrampInfo **info, gboolean aot)
917 {
918         g_assert_not_reached ();
919         return NULL;
920 }
921
922 gpointer
923 mono_arch_create_monitor_exit_trampoline (MonoTrampInfo **info, gboolean aot)
924 {
925         g_assert_not_reached ();
926         return NULL;
927 }
928
929 #endif
930
931 void
932 mono_arch_invalidate_method (MonoJitInfo *ji, void *func, gpointer func_arg)
933 {
934         /* FIXME: This is not thread safe */
935         guint8 *code = ji->code_start;
936
937         x86_push_imm (code, func_arg);
938         x86_call_code (code, (guint8*)func);
939 }
940
941 static void
942 handler_block_trampoline_helper (gpointer *ptr)
943 {
944         MonoJitTlsData *jit_tls = TlsGetValue (mono_jit_tls_id);
945         *ptr = jit_tls->handler_block_return_address;
946 }
947
948 gpointer
949 mono_arch_create_handler_block_trampoline (void)
950 {
951         guint8 *tramp = mono_get_trampoline_code (MONO_TRAMPOLINE_HANDLER_BLOCK_GUARD);
952         guint8 *code, *buf;
953         int tramp_size = 64;
954         code = buf = mono_global_codeman_reserve (tramp_size);
955
956         /*
957         This trampoline restore the call chain of the handler block then jumps into the code that deals with it.
958         */
959
960         if (mono_get_jit_tls_offset () != -1) {
961                 code = mono_x86_emit_tls_get (code, X86_EAX, mono_get_jit_tls_offset ());
962                 x86_mov_reg_membase (code, X86_EAX, X86_EAX, G_STRUCT_OFFSET (MonoJitTlsData, handler_block_return_address), 4);
963                 /*simulate a call*/
964                 x86_push_reg (code, X86_EAX);
965                 x86_jump_code (code, tramp);
966         } else {
967                 /*Slow path uses a c helper*/
968                 x86_push_reg (code, X86_ESP);
969                 x86_push_imm (code, tramp);
970                 x86_jump_code (code, handler_block_trampoline_helper);
971         }
972
973         mono_arch_flush_icache (buf, code - buf);
974         g_assert (code - buf <= tramp_size);
975
976         return buf;
977 }
978
979 guint8*
980 mono_arch_get_call_target (guint8 *code)
981 {
982         if (code [-5] == 0xe8) {
983                 guint32 disp = *(guint32*)(code - 4);
984                 guint8 *target = code + disp;
985
986                 return target;
987         } else {
988                 return NULL;
989         }
990 }
991
992 guint32
993 mono_arch_get_plt_info_offset (guint8 *plt_entry, mgreg_t *regs, guint8 *code)
994 {
995         return *(guint32*)(plt_entry + 6);
996 }