Merge pull request #787 from directhex/master
[mono.git] / mono / mini / tramp-arm.c
1 /*
2  * tramp-arm.c: JIT trampoline code for ARM
3  *
4  * Authors:
5  *   Paolo Molaro (lupus@ximian.com)
6  *
7  * (C) 2001-2003 Ximian, Inc.
8  * Copyright 2003-2011 Novell Inc
9  * Copyright 2011 Xamarin Inc
10  */
11
12 #include <config.h>
13 #include <glib.h>
14
15 #include <mono/metadata/appdomain.h>
16 #include <mono/metadata/marshal.h>
17 #include <mono/metadata/tabledefs.h>
18 #include <mono/arch/arm/arm-codegen.h>
19
20 #include "mini.h"
21 #include "mini-arm.h"
22
23 #define ALIGN_TO(val,align) ((((guint64)val) + ((align) - 1)) & ~((align) - 1))
24
25 #ifdef USE_JUMP_TABLES
26
27 static guint16
28 decode_imm16 (guint32 insn)
29 {
30         return (((insn >> 16) & 0xf) << 12) | (insn & 0xfff);
31 }
32
33 #define INSN_MASK 0xff00000
34 #define MOVW_MASK ((3 << 24) | (0 << 20))
35 #define MOVT_MASK ((3 << 24) | (4 << 20))
36
37 gpointer*
38 mono_arch_jumptable_entry_from_code (guint8 *code)
39 {
40         guint32 insn1 = ((guint32*)code) [0];
41         guint32 insn2 = ((guint32*)code) [1];
42
43         if (((insn1 & INSN_MASK) == MOVW_MASK) &&
44             ((insn2 & INSN_MASK) == MOVT_MASK) ) {
45                 guint32 imm_lo = decode_imm16 (insn1);
46                 guint32 imm_hi = decode_imm16 (insn2);
47                 return (gpointer*) GUINT_TO_POINTER (imm_lo | (imm_hi << 16));
48         } else {
49                 g_assert_not_reached ();
50                 return NULL;
51         }
52 }
53
54 #undef INSN_MASK
55 #undef MOVW_MASK
56 #undef MOVT_MASK
57
58 void
59 mono_arch_patch_callsite (guint8 *method_start, guint8 *code_ptr, guint8 *addr)
60 {
61         gpointer *jte;
62         /*
63          * code_ptr is 4 instructions after MOVW/MOVT used to address
64          * jumptable entry.
65          */
66         jte = mono_jumptable_get_entry (code_ptr - 16);
67         g_assert ( jte != NULL);
68         *jte = addr;
69 }
70 #else
71 void
72 mono_arch_patch_callsite (guint8 *method_start, guint8 *code_ptr, guint8 *addr)
73 {
74         guint32 *code = (guint32*)code_ptr;
75
76         /* This is the 'bl' or the 'mov pc' instruction */
77         --code;
78         
79         /*
80          * Note that methods are called also with the bl opcode.
81          */
82         if ((((*code) >> 25)  & 7) == 5) {
83                 /*g_print ("direct patching\n");*/
84                 arm_patch ((guint8*)code, addr);
85                 mono_arch_flush_icache ((guint8*)code, 4);
86                 return;
87         }
88
89         if ((((*code) >> 20) & 0xFF) == 0x12) {
90                 /*g_print ("patching bx\n");*/
91                 arm_patch ((guint8*)code, addr);
92                 mono_arch_flush_icache ((guint8*)(code - 2), 4);
93                 return;
94         }
95
96         g_assert_not_reached ();
97 }
98 #endif
99
100 void
101 mono_arch_patch_plt_entry (guint8 *code, gpointer *got, mgreg_t *regs, guint8 *addr)
102 {
103         guint8 *jump_entry;
104
105         /* Patch the jump table entry used by the plt entry */
106         if (*(guint32*)code == 0xe59fc000) {
107                 /* ARM_LDR_IMM (code, ARMREG_IP, ARMREG_PC, 0); */
108                 guint32 offset = ((guint32*)code)[2];
109                 
110                 jump_entry = code + offset + 12;
111         } else if (*(guint16*)(code - 4) == 0xf8df) {
112                 /* 
113                  * Thumb PLT entry, begins with ldr.w ip, [pc, #8], code points to entry + 4, see
114                  * mono_arm_get_thumb_plt_entry ().
115                  */
116                 guint32 offset;
117
118                 code -= 4;
119                 offset = *(guint32*)(code + 12);
120                 jump_entry = code + offset + 8;
121         } else {
122                 g_assert_not_reached ();
123         }
124
125         *(guint8**)jump_entry = addr;
126 }
127
128 void
129 mono_arch_nullify_class_init_trampoline (guint8 *code, mgreg_t *regs)
130 {
131         mono_arch_patch_callsite (NULL, code, mini_get_nullified_class_init_trampoline ());
132 }
133
134 void
135 mono_arch_nullify_plt_entry (guint8 *code, mgreg_t *regs)
136 {
137         mono_arch_patch_plt_entry (code, NULL, regs, mini_get_nullified_class_init_trampoline ());
138 }
139
140 #ifndef DISABLE_JIT
141
142 #define arm_is_imm12(v) ((int)(v) > -4096 && (int)(v) < 4096)
143
144 #ifndef USE_JUMP_TABLES
145 /*
146  * Return the instruction to jump from code to target, 0 if not
147  * reachable with a single instruction
148  */
149 static guint32
150 branch_for_target_reachable (guint8 *branch, guint8 *target)
151 {
152         gint diff = target - branch - 8;
153         g_assert ((diff & 3) == 0);
154         if (diff >= 0) {
155                 if (diff <= 33554431)
156                         return (ARMCOND_AL << ARMCOND_SHIFT) | (ARM_BR_TAG) | (diff >> 2);
157         } else {
158                 /* diff between 0 and -33554432 */
159                 if (diff >= -33554432)
160                         return (ARMCOND_AL << ARMCOND_SHIFT) | (ARM_BR_TAG) | ((diff >> 2) & ~0xff000000);
161         }
162         return 0;
163 }
164 #endif
165
166 static inline guint8*
167 emit_bx (guint8* code, int reg)
168 {
169         if (mono_arm_thumb_supported ())
170                 ARM_BX (code, reg);
171         else
172                 ARM_MOV_REG_REG (code, ARMREG_PC, reg);
173         return code;
174 }
175
176 /* Stack size for trampoline function 
177  */
178 #define STACK ALIGN_TO (sizeof (MonoLMF), 8)
179
180 /* Method-specific trampoline code fragment size */
181 #define METHOD_TRAMPOLINE_SIZE 64
182
183 /* Jump-specific trampoline code fragment size */
184 #define JUMP_TRAMPOLINE_SIZE   64
185
186 guchar*
187 mono_arch_create_generic_trampoline (MonoTrampolineType tramp_type, MonoTrampInfo **info, gboolean aot)
188 {
189         char *tramp_name;
190         guint8 *buf, *code = NULL;
191 #ifdef USE_JUMP_TABLES
192         gpointer *load_get_lmf_addr = NULL, *load_trampoline = NULL;
193 #else
194         guint8 *load_get_lmf_addr  = NULL, *load_trampoline  = NULL;
195         gpointer *constants;
196 #endif
197
198         int cfa_offset, lmf_offset, regsave_size, lr_offset;
199         GSList *unwind_ops = NULL;
200         MonoJumpInfo *ji = NULL;
201         int buf_len;
202
203 #ifdef USE_JUMP_TABLES
204         g_assert (!aot);
205 #endif
206
207         /* Now we'll create in 'buf' the ARM trampoline code. This
208          is the trampoline code common to all methods  */
209
210         buf_len = 212;
211         code = buf = mono_global_codeman_reserve (buf_len);
212
213         /*
214          * At this point lr points to the specific arg and sp points to the saved
215          * regs on the stack (all but PC and SP). The original LR value has been
216          * saved as sp + LR_OFFSET by the push in the specific trampoline
217          */
218
219         /* The offset of lmf inside the stack frame */
220         lmf_offset = STACK - sizeof (MonoLMF);
221         /* The size of the area already allocated by the push in the specific trampoline */
222         regsave_size = 14 * sizeof (mgreg_t);
223         /* The offset where lr was saved inside the regsave area */
224         lr_offset = 13 * sizeof (mgreg_t);
225
226         // FIXME: Finish the unwind info, the current info allows us to unwind
227         // when the trampoline is not in the epilog
228
229         // CFA = SP + (num registers pushed) * 4
230         cfa_offset = 14 * sizeof (mgreg_t);
231         mono_add_unwind_op_def_cfa (unwind_ops, code, buf, ARMREG_SP, cfa_offset);
232         // PC saved at sp+LR_OFFSET
233         mono_add_unwind_op_offset (unwind_ops, code, buf, ARMREG_LR, -4);
234
235         if (aot && tramp_type != MONO_TRAMPOLINE_GENERIC_CLASS_INIT) {
236                 /* 
237                  * For page trampolines the data is in r1, so just move it, otherwise use the got slot as below.
238                  * The trampoline contains a pc-relative offset to the got slot 
239                  * preceeding the got slot where the value is stored. The offset can be
240                  * found at [lr + 0].
241                  */
242                 if (aot == 2) {
243                         ARM_MOV_REG_REG (code, ARMREG_V2, ARMREG_R1);
244                 } else {
245                         ARM_LDR_IMM (code, ARMREG_V2, ARMREG_LR, 0);
246                         ARM_ADD_REG_IMM (code, ARMREG_V2, ARMREG_V2, 4, 0);
247                         ARM_LDR_REG_REG (code, ARMREG_V2, ARMREG_V2, ARMREG_LR);
248                 }
249         } else {
250                 if (tramp_type != MONO_TRAMPOLINE_GENERIC_CLASS_INIT) {
251                         ARM_LDR_IMM (code, ARMREG_V2, ARMREG_LR, 0);
252                 }
253                 else
254                         ARM_MOV_REG_REG (code, ARMREG_V2, MONO_ARCH_VTABLE_REG);
255         }
256         ARM_LDR_IMM (code, ARMREG_V3, ARMREG_SP, lr_offset);
257
258         /* ok, now we can continue with the MonoLMF setup, mostly untouched 
259          * from emit_prolog in mini-arm.c
260          * This is a synthetized call to mono_get_lmf_addr ()
261          */
262         if (aot) {
263                 ji = mono_patch_info_list_prepend (ji, code - buf, MONO_PATCH_INFO_JIT_ICALL_ADDR, "mono_get_lmf_addr");
264                 ARM_LDR_IMM (code, ARMREG_R0, ARMREG_PC, 0);
265                 ARM_B (code, 0);
266                 *(gpointer*)code = NULL;
267                 code += 4;
268                 ARM_LDR_REG_REG (code, ARMREG_R0, ARMREG_PC, ARMREG_R0);
269         } else {
270 #ifdef USE_JUMP_TABLES
271                 load_get_lmf_addr = mono_jumptable_add_entry ();
272                 code = mono_arm_load_jumptable_entry (code, load_get_lmf_addr, ARMREG_R0);
273 #else
274                 load_get_lmf_addr = code;
275                 code += 4;
276 #endif
277         }
278         ARM_MOV_REG_REG (code, ARMREG_LR, ARMREG_PC);
279         code = emit_bx (code, ARMREG_R0);
280
281         /* we build the MonoLMF structure on the stack - see mini-arm.h
282          * The pointer to the struct is put in r1.
283          * the iregs array is already allocated on the stack by push.
284          */
285         ARM_SUB_REG_IMM8 (code, ARMREG_SP, ARMREG_SP, STACK - regsave_size);
286         cfa_offset += STACK - regsave_size;
287         mono_add_unwind_op_def_cfa_offset (unwind_ops, code, buf, cfa_offset);
288         /* V1 == lmf */
289         ARM_ADD_REG_IMM8 (code, ARMREG_V1, ARMREG_SP, STACK - sizeof (MonoLMF));
290
291         /*
292          * The stack now looks like:
293          *       <saved regs>
294          * v1 -> <rest of LMF>
295          * sp -> <alignment>
296          */
297
298         /* r0 is the result from mono_get_lmf_addr () */
299         ARM_STR_IMM (code, ARMREG_R0, ARMREG_V1, G_STRUCT_OFFSET (MonoLMF, lmf_addr));
300         /* new_lmf->previous_lmf = *lmf_addr */
301         ARM_LDR_IMM (code, ARMREG_R2, ARMREG_R0, G_STRUCT_OFFSET (MonoLMF, previous_lmf));
302         ARM_STR_IMM (code, ARMREG_R2, ARMREG_V1, G_STRUCT_OFFSET (MonoLMF, previous_lmf));
303         /* *(lmf_addr) = r1 */
304         ARM_STR_IMM (code, ARMREG_V1, ARMREG_R0, G_STRUCT_OFFSET (MonoLMF, previous_lmf));
305         /* save method info (it's in v2) */
306         if ((tramp_type == MONO_TRAMPOLINE_JIT) || (tramp_type == MONO_TRAMPOLINE_JUMP))
307                 ARM_STR_IMM (code, ARMREG_V2, ARMREG_V1, G_STRUCT_OFFSET (MonoLMF, method));
308         else {
309                 ARM_MOV_REG_IMM8 (code, ARMREG_R2, 0);
310                 ARM_STR_IMM (code, ARMREG_R2, ARMREG_V1, G_STRUCT_OFFSET (MonoLMF, method));
311         }
312         /* save caller SP */
313         ARM_ADD_REG_IMM8 (code, ARMREG_R2, ARMREG_SP, cfa_offset);
314         ARM_STR_IMM (code, ARMREG_R2, ARMREG_V1, G_STRUCT_OFFSET (MonoLMF, sp));
315         /* save caller FP */
316         ARM_LDR_IMM (code, ARMREG_R2, ARMREG_V1, (G_STRUCT_OFFSET (MonoLMF, iregs) + ARMREG_FP*4));
317         ARM_STR_IMM (code, ARMREG_R2, ARMREG_V1, G_STRUCT_OFFSET (MonoLMF, fp));
318         /* save the IP (caller ip) */
319         if (tramp_type == MONO_TRAMPOLINE_JUMP) {
320                 ARM_MOV_REG_IMM8 (code, ARMREG_R2, 0);
321         } else {
322                 ARM_LDR_IMM (code, ARMREG_R2, ARMREG_V1, (G_STRUCT_OFFSET (MonoLMF, iregs) + 13*4));
323         }
324         ARM_STR_IMM (code, ARMREG_R2, ARMREG_V1, G_STRUCT_OFFSET (MonoLMF, ip));
325
326         /*
327          * Now we're ready to call xxx_trampoline ().
328          */
329         /* Arg 1: the saved registers */
330         ARM_ADD_REG_IMM8 (code, ARMREG_R0, ARMREG_V1, G_STRUCT_OFFSET (MonoLMF, iregs));
331
332         /* Arg 2: code (next address to the instruction that called us) */
333         if (tramp_type == MONO_TRAMPOLINE_JUMP) {
334                 ARM_MOV_REG_IMM8 (code, ARMREG_R1, 0);
335         } else {
336                 ARM_MOV_REG_REG (code, ARMREG_R1, ARMREG_V3);
337         }
338         
339         /* Arg 3: the specific argument, stored in v2
340          */
341         ARM_MOV_REG_REG (code, ARMREG_R2, ARMREG_V2);
342
343         if (aot) {
344                 char *icall_name = g_strdup_printf ("trampoline_func_%d", tramp_type);
345                 ji = mono_patch_info_list_prepend (ji, code - buf, MONO_PATCH_INFO_JIT_ICALL_ADDR, icall_name);
346                 ARM_LDR_IMM (code, ARMREG_IP, ARMREG_PC, 0);
347                 ARM_B (code, 0);
348                 *(gpointer*)code = NULL;
349                 code += 4;
350                 ARM_LDR_REG_REG (code, ARMREG_IP, ARMREG_PC, ARMREG_IP);
351         } else {
352 #ifdef USE_JUMP_TABLES
353                 load_trampoline = mono_jumptable_add_entry ();
354                 code = mono_arm_load_jumptable_entry (code, load_trampoline, ARMREG_IP);
355 #else
356                 load_trampoline = code;
357                 code += 4;
358 #endif
359         }
360
361         ARM_MOV_REG_REG (code, ARMREG_LR, ARMREG_PC);
362         code = emit_bx (code, ARMREG_IP);
363
364         /* OK, code address is now on r0. Move it to the place on the stack
365          * where IP was saved (it is now no more useful to us and it can be
366          * clobbered). This way we can just restore all the regs in one inst
367          * and branch to IP.
368          */
369         ARM_STR_IMM (code, ARMREG_R0, ARMREG_V1, G_STRUCT_OFFSET (MonoLMF, iregs) + (ARMREG_R12 * sizeof (mgreg_t)));
370
371         /* Check for thread interruption */
372         /* This is not perf critical code so no need to check the interrupt flag */
373         /* 
374          * Have to call the _force_ variant, since there could be a protected wrapper on the top of the stack.
375          */
376         if (aot) {
377                 ji = mono_patch_info_list_prepend (ji, code - buf, MONO_PATCH_INFO_JIT_ICALL_ADDR, "mono_thread_force_interruption_checkpoint");
378                 ARM_LDR_IMM (code, ARMREG_IP, ARMREG_PC, 0);
379                 ARM_B (code, 0);
380                 *(gpointer*)code = NULL;
381                 code += 4;
382                 ARM_LDR_REG_REG (code, ARMREG_IP, ARMREG_PC, ARMREG_IP);
383         } else {
384 #ifdef USE_JUMP_TABLES
385                 gpointer *jte = mono_jumptable_add_entry ();
386                 code = mono_arm_load_jumptable_entry (code, jte, ARMREG_IP);
387                 jte [0] = mono_thread_force_interruption_checkpoint;
388 #else
389                 ARM_LDR_IMM (code, ARMREG_IP, ARMREG_PC, 0);
390                 ARM_B (code, 0);
391                 *(gpointer*)code = mono_thread_force_interruption_checkpoint;
392                 code += 4;
393 #endif
394         }
395         ARM_MOV_REG_REG (code, ARMREG_LR, ARMREG_PC);
396         code = emit_bx (code, ARMREG_IP);
397
398         /*
399          * Now we restore the MonoLMF (see emit_epilogue in mini-arm.c)
400          * and the rest of the registers, so the method called will see
401          * the same state as before we executed.
402          */
403         /* ip = previous_lmf */
404         ARM_LDR_IMM (code, ARMREG_IP, ARMREG_V1, G_STRUCT_OFFSET (MonoLMF, previous_lmf));
405         /* lr = lmf_addr */
406         ARM_LDR_IMM (code, ARMREG_LR, ARMREG_V1, G_STRUCT_OFFSET (MonoLMF, lmf_addr));
407         /* *(lmf_addr) = previous_lmf */
408         ARM_STR_IMM (code, ARMREG_IP, ARMREG_LR, G_STRUCT_OFFSET (MonoLMF, previous_lmf));
409
410         /* Non-standard function epilogue. Instead of doing a proper
411          * return, we just jump to the compiled code.
412          */
413         /* Restore the registers and jump to the code:
414          * Note that IP has been conveniently set to the method addr.
415          */
416         ARM_ADD_REG_IMM8 (code, ARMREG_SP, ARMREG_SP, STACK - regsave_size);
417         ARM_POP_NWB (code, 0x5fff);
418         if (tramp_type == MONO_TRAMPOLINE_RGCTX_LAZY_FETCH)
419                 ARM_MOV_REG_REG (code, ARMREG_R0, ARMREG_IP);
420         ARM_ADD_REG_IMM8 (code, ARMREG_SP, ARMREG_SP, regsave_size);
421         if ((tramp_type == MONO_TRAMPOLINE_CLASS_INIT) || (tramp_type == MONO_TRAMPOLINE_GENERIC_CLASS_INIT) || (tramp_type == MONO_TRAMPOLINE_RGCTX_LAZY_FETCH))
422                 code = emit_bx (code, ARMREG_LR);
423         else
424                 code = emit_bx (code, ARMREG_IP);
425
426 #ifdef USE_JUMP_TABLES
427         load_get_lmf_addr [0] = mono_get_lmf_addr;
428         load_trampoline [0] = (gpointer)mono_get_trampoline_func (tramp_type);
429 #else
430         constants = (gpointer*)code;
431         constants [0] = mono_get_lmf_addr;
432         constants [1] = (gpointer)mono_get_trampoline_func (tramp_type);
433
434         if (!aot) {
435                 /* backpatch by emitting the missing instructions skipped above */
436                 ARM_LDR_IMM (load_get_lmf_addr, ARMREG_R0, ARMREG_PC, (code - load_get_lmf_addr - 8));
437                 ARM_LDR_IMM (load_trampoline, ARMREG_IP, ARMREG_PC, (code + 4 - load_trampoline - 8));
438         }
439
440         code += 8;
441 #endif
442
443         /* Flush instruction cache, since we've generated code */
444         mono_arch_flush_icache (buf, code - buf);
445
446         /* Sanity check */
447         g_assert ((code - buf) <= buf_len);
448
449         if (info) {
450                 tramp_name = mono_get_generic_trampoline_name (tramp_type);
451                 *info = mono_tramp_info_create (tramp_name, buf, code - buf, ji, unwind_ops);
452                 g_free (tramp_name);
453         }
454
455         return buf;
456 }
457
458 gpointer
459 mono_arch_get_nullified_class_init_trampoline (MonoTrampInfo **info)
460 {
461         guint8 *buf, *code;
462
463         code = buf = mono_global_codeman_reserve (16);
464
465         code = emit_bx (code, ARMREG_LR);
466
467         mono_arch_flush_icache (buf, code - buf);
468
469         if (info)
470                 *info = mono_tramp_info_create ("nullified_class_init_trampoline", buf, code - buf, NULL, NULL);
471
472         return buf;
473 }
474
475 #define SPEC_TRAMP_SIZE 24
476
477 gpointer
478 mono_arch_create_specific_trampoline (gpointer arg1, MonoTrampolineType tramp_type, MonoDomain *domain, guint32 *code_len)
479 {
480         guint8 *code, *buf, *tramp;
481         gpointer *constants;
482 #ifndef USE_JUMP_TABLES
483         guint32 short_branch;
484 #endif
485         guint32 size = SPEC_TRAMP_SIZE;
486
487         tramp = mono_get_trampoline_code (tramp_type);
488
489         mono_domain_lock (domain);
490 #ifdef USE_JUMP_TABLES
491         code = buf = mono_domain_code_reserve_align (domain, size, 4);
492 #else
493         code = buf = mono_domain_code_reserve_align (domain, size, 4);
494         if ((short_branch = branch_for_target_reachable (code + 4, tramp))) {
495                 size = 12;
496                 mono_domain_code_commit (domain, code, SPEC_TRAMP_SIZE, size);
497         }
498 #endif
499         mono_domain_unlock (domain);
500
501 #ifdef USE_JUMP_TABLES
502         /* For jumptables case we always generate the same code for trampolines,
503          * namely
504          *   push {r0, r1, r2, r3, r4, r5, r6, r7, r8, r9, r10, r11, r12, lr}
505          *   movw lr, lo(jte)
506          *   movt lr, hi(jte)
507          *   ldr r1, [lr + 4]
508          *   bx r1
509          */
510         ARM_PUSH (code, 0x5fff);
511         constants = mono_jumptable_add_entries (2);
512         code = mono_arm_load_jumptable_entry_addr (code, constants, ARMREG_LR);
513         ARM_LDR_IMM (code, ARMREG_R1, ARMREG_LR, 4);
514         code = emit_bx (code, ARMREG_R1);
515         constants [0] = arg1;
516         constants [1] = tramp;
517 #else
518         /* we could reduce this to 12 bytes if tramp is within reach:
519          * ARM_PUSH ()
520          * ARM_BL ()
521          * method-literal
522          * The called code can access method using the lr register
523          * A 20 byte sequence could be:
524          * ARM_PUSH ()
525          * ARM_MOV_REG_REG (lr, pc)
526          * ARM_LDR_IMM (pc, pc, 0)
527          * method-literal
528          * tramp-literal
529          */
530         /* We save all the registers, except PC and SP */
531         ARM_PUSH (code, 0x5fff);
532         if (short_branch) {
533                 constants = (gpointer*)code;
534                 constants [0] = GUINT_TO_POINTER (short_branch | (1 << 24));
535                 constants [1] = arg1;
536                 code += 8;
537         } else {
538                 ARM_LDR_IMM (code, ARMREG_R1, ARMREG_PC, 8); /* temp reg */
539                 ARM_MOV_REG_REG (code, ARMREG_LR, ARMREG_PC);
540                 code = emit_bx (code, ARMREG_R1);
541
542                 constants = (gpointer*)code;
543                 constants [0] = arg1;
544                 constants [1] = tramp;
545                 code += 8;
546         }
547 #endif
548
549         /* Flush instruction cache, since we've generated code */
550         mono_arch_flush_icache (buf, code - buf);
551
552         g_assert ((code - buf) <= size);
553
554         if (code_len)
555                 *code_len = code - buf;
556
557         return buf;
558 }
559
560 /*
561  * mono_arch_get_unbox_trampoline:
562  * @m: method pointer
563  * @addr: pointer to native code for @m
564  *
565  * when value type methods are called through the vtable we need to unbox the
566  * this argument. This method returns a pointer to a trampoline which does
567  * unboxing before calling the method
568  */
569 gpointer
570 mono_arch_get_unbox_trampoline (MonoMethod *m, gpointer addr)
571 {
572         guint8 *code, *start;
573         MonoDomain *domain = mono_domain_get ();
574 #ifdef USE_JUMP_TABLES
575         gpointer *jte;
576         guint32 size = 20;
577 #else
578         guint32 size = 16;
579 #endif
580
581         start = code = mono_domain_code_reserve (domain, size);
582
583 #ifdef USE_JUMP_TABLES
584         jte = mono_jumptable_add_entry ();
585         code = mono_arm_load_jumptable_entry (code, jte, ARMREG_IP);
586         ARM_ADD_REG_IMM8 (code, ARMREG_R0, ARMREG_R0, sizeof (MonoObject));
587         code = emit_bx (code, ARMREG_IP);
588         jte [0] = addr;
589 #else
590         ARM_LDR_IMM (code, ARMREG_IP, ARMREG_PC, 4);
591         ARM_ADD_REG_IMM8 (code, ARMREG_R0, ARMREG_R0, sizeof (MonoObject));
592         code = emit_bx (code, ARMREG_IP);
593         *(guint32*)code = (guint32)addr;
594         code += 4;
595 #endif
596         mono_arch_flush_icache (start, code - start);
597         g_assert ((code - start) <= size);
598         /*g_print ("unbox trampoline at %d for %s:%s\n", this_pos, m->klass->name, m->name);
599         g_print ("unbox code is at %p for method at %p\n", start, addr);*/
600
601         return start;
602 }
603
604 gpointer
605 mono_arch_get_static_rgctx_trampoline (MonoMethod *m, MonoMethodRuntimeGenericContext *mrgctx, gpointer addr)
606 {
607         guint8 *code, *start;
608 #ifdef USE_JUMP_TABLES
609         int buf_len = 20;
610         gpointer *jte;
611 #else
612         int buf_len = 16;
613 #endif
614         MonoDomain *domain = mono_domain_get ();
615
616         start = code = mono_domain_code_reserve (domain, buf_len);
617
618 #ifdef USE_JUMP_TABLES
619         jte = mono_jumptable_add_entries (2);
620         code = mono_arm_load_jumptable_entry_addr (code, jte, ARMREG_IP);
621         ARM_LDR_IMM (code, MONO_ARCH_RGCTX_REG, ARMREG_IP, 0);
622         ARM_LDR_IMM (code, ARMREG_IP, ARMREG_IP, 4);
623         ARM_BX (code, ARMREG_IP);
624         jte [0] = mrgctx;
625         jte [1] = addr;
626 #else
627         ARM_LDR_IMM (code, MONO_ARCH_RGCTX_REG, ARMREG_PC, 0);
628         ARM_LDR_IMM (code, ARMREG_PC, ARMREG_PC, 0);
629         *(guint32*)code = (guint32)mrgctx;
630         code += 4;
631         *(guint32*)code = (guint32)addr;
632         code += 4;
633 #endif
634
635         g_assert ((code - start) <= buf_len);
636
637         mono_arch_flush_icache (start, code - start);
638
639         return start;
640 }
641
642 gpointer
643 mono_arch_create_rgctx_lazy_fetch_trampoline (guint32 slot, MonoTrampInfo **info, gboolean aot)
644 {
645         guint8 *tramp;
646         guint8 *code, *buf;
647         int tramp_size;
648         guint32 code_len;
649         guint8 **rgctx_null_jumps;
650         int depth, index;
651         int i, njumps;
652         gboolean mrgctx;
653         MonoJumpInfo *ji = NULL;
654         GSList *unwind_ops = NULL;
655 #ifdef USE_JUMP_TABLES
656         gpointer *jte;
657 #endif
658
659         mrgctx = MONO_RGCTX_SLOT_IS_MRGCTX (slot);
660         index = MONO_RGCTX_SLOT_INDEX (slot);
661         if (mrgctx)
662                 index += MONO_SIZEOF_METHOD_RUNTIME_GENERIC_CONTEXT / sizeof (gpointer);
663         for (depth = 0; ; ++depth) {
664                 int size = mono_class_rgctx_get_array_size (depth, mrgctx);
665
666                 if (index < size - 1)
667                         break;
668                 index -= size - 1;
669         }
670
671         tramp_size = 64 + 16 * depth;
672
673         code = buf = mono_global_codeman_reserve (tramp_size);
674
675         mono_add_unwind_op_def_cfa (unwind_ops, code, buf, ARMREG_SP, 0);
676
677         rgctx_null_jumps = g_malloc (sizeof (guint8*) * (depth + 2));
678         njumps = 0;
679
680         /* The vtable/mrgctx is in R0 */
681         g_assert (MONO_ARCH_VTABLE_REG == ARMREG_R0);
682
683         if (mrgctx) {
684                 /* get mrgctx ptr */
685                 ARM_MOV_REG_REG (code, ARMREG_R1, ARMREG_R0);
686         } else {
687                 /* load rgctx ptr from vtable */
688                 g_assert (arm_is_imm12 (G_STRUCT_OFFSET (MonoVTable, runtime_generic_context)));
689                 ARM_LDR_IMM (code, ARMREG_R1, ARMREG_R0, G_STRUCT_OFFSET (MonoVTable, runtime_generic_context));
690                 /* is the rgctx ptr null? */
691                 ARM_CMP_REG_IMM (code, ARMREG_R1, 0, 0);
692                 /* if yes, jump to actual trampoline */
693                 rgctx_null_jumps [njumps ++] = code;
694                 ARM_B_COND (code, ARMCOND_EQ, 0);
695         }
696
697         for (i = 0; i < depth; ++i) {
698                 /* load ptr to next array */
699                 if (mrgctx && i == 0) {
700                         g_assert (arm_is_imm12 (MONO_SIZEOF_METHOD_RUNTIME_GENERIC_CONTEXT));
701                         ARM_LDR_IMM (code, ARMREG_R1, ARMREG_R1, MONO_SIZEOF_METHOD_RUNTIME_GENERIC_CONTEXT);
702                 } else {
703                         ARM_LDR_IMM (code, ARMREG_R1, ARMREG_R1, 0);
704                 }
705                 /* is the ptr null? */
706                 ARM_CMP_REG_IMM (code, ARMREG_R1, 0, 0);
707                 /* if yes, jump to actual trampoline */
708                 rgctx_null_jumps [njumps ++] = code;
709                 ARM_B_COND (code, ARMCOND_EQ, 0);
710         }
711
712         /* fetch slot */
713         code = mono_arm_emit_load_imm (code, ARMREG_R2, sizeof (gpointer) * (index + 1));
714         ARM_LDR_REG_REG (code, ARMREG_R1, ARMREG_R1, ARMREG_R2);
715         /* is the slot null? */
716         ARM_CMP_REG_IMM (code, ARMREG_R1, 0, 0);
717         /* if yes, jump to actual trampoline */
718         rgctx_null_jumps [njumps ++] = code;
719         ARM_B_COND (code, ARMCOND_EQ, 0);
720         /* otherwise return, result is in R1 */
721         ARM_MOV_REG_REG (code, ARMREG_R0, ARMREG_R1);
722         code = emit_bx (code, ARMREG_LR);
723
724         g_assert (njumps <= depth + 2);
725         for (i = 0; i < njumps; ++i)
726                 arm_patch (rgctx_null_jumps [i], code);
727
728         g_free (rgctx_null_jumps);
729
730         /* Slowpath */
731
732         /* The vtable/mrgctx is still in R0 */
733
734         if (aot) {
735                 ji = mono_patch_info_list_prepend (ji, code - buf, MONO_PATCH_INFO_JIT_ICALL_ADDR, g_strdup_printf ("specific_trampoline_lazy_fetch_%u", slot));
736                 ARM_LDR_IMM (code, ARMREG_R1, ARMREG_PC, 0);
737                 ARM_B (code, 0);
738                 *(gpointer*)code = NULL;
739                 code += 4;
740                 ARM_LDR_REG_REG (code, ARMREG_PC, ARMREG_PC, ARMREG_R1);
741         } else {
742                 tramp = mono_arch_create_specific_trampoline (GUINT_TO_POINTER (slot), MONO_TRAMPOLINE_RGCTX_LAZY_FETCH, mono_get_root_domain (), &code_len);
743
744                 /* Jump to the actual trampoline */
745 #ifdef USE_JUMP_TABLES
746                 jte = mono_jumptable_add_entry ();
747                 jte [0] = tramp;
748                 code = mono_arm_load_jumptable_entry (code, jte, ARMREG_R1);
749                 code = emit_bx (code, ARMREG_R1);
750 #else
751                 ARM_LDR_IMM (code, ARMREG_R1, ARMREG_PC, 0); /* temp reg */
752                 code = emit_bx (code, ARMREG_R1);
753                 *(gpointer*)code = tramp;
754                 code += 4;
755 #endif
756         }
757
758         mono_arch_flush_icache (buf, code - buf);
759
760         g_assert (code - buf <= tramp_size);
761
762         if (info) {
763                 char *name = mono_get_rgctx_fetch_trampoline_name (slot);
764                 *info = mono_tramp_info_create (name, buf, code - buf, ji, unwind_ops);
765                 g_free (name);
766         }
767
768         return buf;
769 }
770
771 gpointer
772 mono_arch_create_general_rgctx_lazy_fetch_trampoline (MonoTrampInfo **info, gboolean aot)
773 {
774         guint8 *code, *buf;
775         int tramp_size;
776         MonoJumpInfo *ji = NULL;
777         GSList *unwind_ops = NULL;
778
779         g_assert (aot);
780
781         tramp_size = 32;
782
783         code = buf = mono_global_codeman_reserve (tramp_size);
784
785         mono_add_unwind_op_def_cfa (unwind_ops, code, buf, ARMREG_SP, 0);
786
787         // FIXME: Currently, we always go to the slow path.
788         /* Load trampoline addr */
789         ARM_LDR_IMM (code, ARMREG_R1, MONO_ARCH_RGCTX_REG, 4);
790         /* The vtable/mrgctx is in R0 */
791         g_assert (MONO_ARCH_VTABLE_REG == ARMREG_R0);
792         code = emit_bx (code, ARMREG_R1);
793
794         mono_arch_flush_icache (buf, code - buf);
795
796         g_assert (code - buf <= tramp_size);
797
798         if (info)
799                 *info = mono_tramp_info_create ("rgctx_fetch_trampoline_general", buf, code - buf, ji, unwind_ops);
800
801         return buf;
802 }
803
804 #define arm_is_imm8(v) ((v) > -256 && (v) < 256)
805
806 gpointer
807 mono_arch_create_generic_class_init_trampoline (MonoTrampInfo **info, gboolean aot)
808 {
809         guint8 *tramp;
810         guint8 *code, *buf;
811         static int byte_offset = -1;
812         static guint8 bitmask;
813         guint8 *jump;
814         int tramp_size;
815         guint32 code_len, imm8;
816         gint rot_amount;
817         GSList *unwind_ops = NULL;
818         MonoJumpInfo *ji = NULL;
819
820         tramp_size = 64;
821
822         code = buf = mono_global_codeman_reserve (tramp_size);
823
824         if (byte_offset < 0)
825                 mono_marshal_find_bitfield_offset (MonoVTable, initialized, &byte_offset, &bitmask);
826
827         g_assert (arm_is_imm8 (byte_offset));
828         ARM_LDRSB_IMM (code, ARMREG_IP, MONO_ARCH_VTABLE_REG, byte_offset);
829         imm8 = mono_arm_is_rotated_imm8 (bitmask, &rot_amount);
830         g_assert (imm8 >= 0);
831         ARM_AND_REG_IMM (code, ARMREG_IP, ARMREG_IP, imm8, rot_amount);
832         ARM_CMP_REG_IMM (code, ARMREG_IP, 0, 0);
833         jump = code;
834         ARM_B_COND (code, ARMCOND_EQ, 0);
835
836         /* Initialized case */
837         ARM_MOV_REG_REG (code, ARMREG_PC, ARMREG_LR);   
838
839         /* Uninitialized case */
840         arm_patch (jump, code);
841
842         if (aot) {
843                 ji = mono_patch_info_list_prepend (ji, code - buf, MONO_PATCH_INFO_JIT_ICALL_ADDR, "specific_trampoline_generic_class_init");
844                 ARM_LDR_IMM (code, ARMREG_R1, ARMREG_PC, 0);
845                 ARM_B (code, 0);
846                 *(gpointer*)code = NULL;
847                 code += 4;
848                 ARM_LDR_REG_REG (code, ARMREG_PC, ARMREG_PC, ARMREG_R1);
849         } else {
850 #ifdef USE_JUMP_TABLES
851                 gpointer *jte = mono_jumptable_add_entry ();
852 #endif
853                 tramp = mono_arch_create_specific_trampoline (NULL, MONO_TRAMPOLINE_GENERIC_CLASS_INIT, mono_get_root_domain (), &code_len);
854
855                 /* Jump to the actual trampoline */
856 #ifdef USE_JUMP_TABLES
857                 code = mono_arm_load_jumptable_entry (code, jte, ARMREG_R1);
858                 jte [0] = tramp;
859                 code = emit_bx (code, ARMREG_R1);
860 #else
861                 ARM_LDR_IMM (code, ARMREG_R1, ARMREG_PC, 0); /* temp reg */
862                 code = emit_bx (code, ARMREG_R1);
863                 *(gpointer*)code = tramp;
864                 code += 4;
865 #endif
866         }
867
868         mono_arch_flush_icache (buf, code - buf);
869
870         g_assert (code - buf <= tramp_size);
871
872         if (info)
873                 *info = mono_tramp_info_create ("generic_class_init_trampoline", buf, code - buf, ji, unwind_ops);
874
875         return buf;
876 }
877
878 #else
879
880 guchar*
881 mono_arch_create_generic_trampoline (MonoTrampolineType tramp_type, MonoTrampInfo **info, gboolean aot)
882 {
883         g_assert_not_reached ();
884         return NULL;
885 }
886
887 gpointer
888 mono_arch_create_specific_trampoline (gpointer arg1, MonoTrampolineType tramp_type, MonoDomain *domain, guint32 *code_len)
889 {
890         g_assert_not_reached ();
891         return NULL;
892 }
893
894 gpointer
895 mono_arch_get_unbox_trampoline (MonoMethod *m, gpointer addr)
896 {
897         g_assert_not_reached ();
898         return NULL;
899 }
900
901 gpointer
902 mono_arch_get_static_rgctx_trampoline (MonoMethod *m, MonoMethodRuntimeGenericContext *mrgctx, gpointer addr)
903 {
904         g_assert_not_reached ();
905         return NULL;
906 }
907
908 gpointer
909 mono_arch_create_rgctx_lazy_fetch_trampoline (guint32 slot, MonoTrampInfo **info, gboolean aot)
910 {
911         g_assert_not_reached ();
912         return NULL;
913 }
914
915 gpointer
916 mono_arch_create_generic_class_init_trampoline (MonoTrampInfo **info, gboolean aot)
917 {
918         g_assert_not_reached ();
919         return NULL;
920 }
921
922 gpointer
923 mono_arch_get_nullified_class_init_trampoline (MonoTrampInfo **info)
924 {
925         g_assert_not_reached ();
926         return NULL;
927 }
928         
929 #endif /* DISABLE_JIT */
930
931 guint8*
932 mono_arch_get_call_target (guint8 *code)
933 {
934         guint32 ins = ((guint32*)(gpointer)code) [-1];
935
936 #if MONOTOUCH
937         /* Should be a 'bl' or a 'b' */
938         if (((ins >> 25) & 0x7) == 0x5) {
939 #else
940         /* Should be a 'bl' */
941         if ((((ins >> 25) & 0x7) == 0x5) && (((ins >> 24) & 0x1) == 0x1)) {
942 #endif
943                 gint32 disp = ((gint32)ins) & 0xffffff;
944                 guint8 *target = code - 4 + 8 + (disp * 4);
945
946                 return target;
947         } else {
948                 return NULL;
949         }
950 }
951
952 guint32
953 mono_arch_get_plt_info_offset (guint8 *plt_entry, mgreg_t *regs, guint8 *code)
954 {
955         /* The offset is stored as the 4th word of the plt entry */
956         return ((guint32*)plt_entry) [3];
957 }
958
959 /*
960  * Return the address of the PLT entry called by the thumb code CODE.
961  */
962 guint8*
963 mono_arm_get_thumb_plt_entry (guint8 *code)
964 {
965         int s, j1, j2, imm10, imm11, i1, i2, imm32;
966         guint8 *bl, *base;
967         guint16 t1, t2;
968         guint8 *target;
969
970         /* code should be right after a BL */
971         code = (guint8*)((mgreg_t)code & ~1);
972         base = (guint8*)((mgreg_t)code & ~3);
973         bl = code - 4;
974         t1 = ((guint16*)bl) [0];
975         t2 = ((guint16*)bl) [1];
976
977         g_assert ((t1 >> 11) == 0x1e);
978
979         s = (t1 >> 10) & 0x1;
980         imm10 = (t1 >> 0) & 0x3ff;
981         j1 = (t2 >> 13) & 0x1;
982         j2 = (t2 >> 11) & 0x1;
983         imm11 = t2 & 0x7ff;
984
985         i1 = (s ^ j1) ? 0 : 1;
986         i2 = (s ^ j2) ? 0 : 1;
987
988         imm32 = (imm11 << 1) | (imm10 << 12) | (i2 << 22) | (i1 << 23);
989         // FIXME:
990         g_assert (s == 0);
991
992         target = code + imm32;
993
994         /* target now points to the thumb plt entry */
995         /* ldr.w r12, [pc, #8] */
996         g_assert (((guint16*)target) [0] == 0xf8df);
997         g_assert (((guint16*)target) [1] == 0xc008);
998
999         /* 
1000          * The PLT info offset is at offset 16, but mono_arch_get_plt_entry_offset () returns
1001          * the 3rd word, so compensate by returning a different value.
1002          */
1003         target += 4;
1004
1005         return target;
1006 }
1007
1008 #ifndef DISABLE_JIT
1009
1010 /*
1011  * mono_arch_get_gsharedvt_arg_trampoline:
1012  *
1013  *   See tramp-x86.c for documentation.
1014  */
1015 gpointer
1016 mono_arch_get_gsharedvt_arg_trampoline (MonoDomain *domain, gpointer arg, gpointer addr)
1017 {
1018         guint8 *code, *start;
1019         int buf_len;
1020         gpointer *constants;
1021
1022         buf_len = 24;
1023
1024         start = code = mono_domain_code_reserve (domain, buf_len);
1025
1026         /* Similar to the specialized trampoline code */
1027         ARM_PUSH (code, (1 << ARMREG_R0) | (1 << ARMREG_R1) | (1 << ARMREG_R2) | (1 << ARMREG_R3) | (1 << ARMREG_LR));
1028         ARM_LDR_IMM (code, ARMREG_IP, ARMREG_PC, 8);
1029         /* arg is passed in LR */
1030         ARM_LDR_IMM (code, ARMREG_LR, ARMREG_PC, 0);
1031         code = emit_bx (code, ARMREG_IP);
1032         constants = (gpointer*)code;
1033         constants [0] = arg;
1034         constants [1] = addr;
1035         code += 8;
1036
1037         g_assert ((code - start) <= buf_len);
1038
1039         nacl_domain_code_validate (domain, &start, buf_len, &code);
1040         mono_arch_flush_icache (start, code - start);
1041
1042         return start;
1043 }
1044
1045 #else
1046
1047 gpointer
1048 mono_arch_get_gsharedvt_arg_trampoline (MonoDomain *domain, gpointer arg, gpointer addr)
1049 {
1050         g_assert_not_reached ();
1051         return NULL;
1052 }
1053
1054 #endif
1055
1056 #if defined(MONOTOUCH) || defined(MONO_EXTENSIONS)
1057
1058 #include "../../../mono-extensions/mono/mini/tramp-arm-gsharedvt.c"
1059
1060 #else
1061
1062 gpointer
1063 mono_arm_start_gsharedvt_call (GSharedVtCallInfo *info, gpointer *caller, gpointer *callee, gpointer mrgctx_reg)
1064 {
1065         g_assert_not_reached ();
1066         return NULL;
1067 }
1068
1069 gpointer
1070 mono_arch_get_gsharedvt_trampoline (MonoTrampInfo **info, gboolean aot)
1071 {
1072         if (info)
1073                 *info = NULL;
1074         return NULL;
1075 }
1076
1077 #endif /* !MONOTOUCH */