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