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