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