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