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