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