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