Merge pull request #4540 from kumpera/android-changes-part1
[mono.git] / mono / mini / tramp-arm64.c
1 /**
2  * \file
3  * JIT trampoline code for ARM64
4  *
5  * Copyright 2013 Xamarin Inc
6  *
7  * Based on tramp-arm.c:
8  * 
9  * Authors:
10  *   Paolo Molaro (lupus@ximian.com)
11  *
12  * (C) 2001-2003 Ximian, Inc.
13  * Copyright 2003-2011 Novell Inc
14  * Copyright 2011 Xamarin Inc
15  * Licensed under the MIT license. See LICENSE file in the project root for full license information.
16  */
17
18 #include "mini.h"
19 #include "debugger-agent.h"
20
21 #include <mono/arch/arm64/arm64-codegen.h>
22 #include <mono/metadata/abi-details.h>
23
24 #define ALIGN_TO(val,align) ((((guint64)val) + ((align) - 1)) & ~((align) - 1))
25
26 void
27 mono_arch_patch_callsite (guint8 *method_start, guint8 *code_ptr, guint8 *addr)
28 {
29         mono_arm_patch (code_ptr - 4, addr, MONO_R_ARM64_BL);
30         mono_arch_flush_icache (code_ptr - 4, 4);
31 }
32
33 void
34 mono_arch_patch_plt_entry (guint8 *code, gpointer *got, mgreg_t *regs, guint8 *addr)
35 {
36         guint32 ins;
37         guint64 slot_addr;
38         int disp;
39
40         /* 
41          * Decode the address loaded by the PLT entry emitted by arch_emit_plt_entry () in
42          * aot-compiler.c
43          */
44
45         /* adrp */
46         ins = ((guint32*)code) [0];
47         g_assert (((ins >> 24) & 0x1f) == 0x10);
48         disp = (((ins >> 5) & 0x7ffff) << 2) | ((ins >> 29) & 0x3);
49         /* FIXME: disp is signed */
50         g_assert ((disp >> 20) == 0);
51
52         slot_addr = ((guint64)code + (disp << 12)) & ~0xfff;
53
54         /* add x16, x16, :lo12:got */
55         ins = ((guint32*)code) [1];
56         g_assert (((ins >> 22) & 0x3) == 0);
57         slot_addr += (ins >> 10) & 0xfff;
58
59         /* ldr x16, [x16, <offset>] */
60         ins = ((guint32*)code) [2];
61         g_assert (((ins >> 24) & 0x3f) == 0x39);
62         slot_addr += ((ins >> 10) & 0xfff) * 8;
63
64         g_assert (*(guint64*)slot_addr);
65         *(gpointer*)slot_addr = addr;
66 }
67
68 guint8*
69 mono_arch_get_call_target (guint8 *code)
70 {
71         guint32 imm;
72         int disp;
73
74         code -= 4;
75
76         imm = *(guint32*)code;
77         /* Should be a bl */
78         g_assert (((imm >> 31) & 0x1) == 0x1);
79         g_assert (((imm >> 26) & 0x7) == 0x5);
80
81         disp = (imm & 0x3ffffff);
82         if ((disp >> 25) != 0)
83                 /* Negative, sing extend to 32 bits */
84                 disp = disp | 0xfc000000;
85
86         return code + (disp * 4);
87 }
88
89 guint32
90 mono_arch_get_plt_info_offset (guint8 *plt_entry, mgreg_t *regs, guint8 *code)
91 {
92         /* The offset is stored as the 5th word of the plt entry */
93         return ((guint32*)plt_entry) [4];
94 }
95
96 gpointer
97 mono_arm_handler_block_trampoline_helper (gpointer *ptr)
98 {
99         MonoJitTlsData *jit_tls = mono_tls_get_jit_tls ();
100         return jit_tls->handler_block_return_address;
101 }
102
103 #ifndef DISABLE_JIT
104
105 guchar*
106 mono_arch_create_generic_trampoline (MonoTrampolineType tramp_type, MonoTrampInfo **info, gboolean aot)
107 {
108         guint8 *code, *buf, *tramp, *labels [16];
109         int i, buf_len, imm;
110         int frame_size, offset, gregs_offset, num_fregs, fregs_offset, arg_offset, lmf_offset, res_offset;
111         guint64 gregs_regset;
112         GSList *unwind_ops = NULL;
113         MonoJumpInfo *ji = NULL;
114         char *tramp_name;
115
116         buf_len = 768;
117         buf = code = mono_global_codeman_reserve (buf_len);
118
119         /*
120          * We are getting called by a specific trampoline, ip1 contains the trampoline argument.
121          */
122
123         /* Compute stack frame size and offsets */
124         offset = 0;
125         /* frame block */
126         offset += 2 * 8;
127         /* gregs */
128         gregs_offset = offset;
129         offset += 32 * 8;
130         /* fregs */
131         // FIXME: Save 128 bits
132         /* Only have to save the argument regs */
133         num_fregs = 8;
134         fregs_offset = offset;
135         offset += num_fregs * 8;
136         /* arg */
137         arg_offset = offset;
138         offset += 8;
139         /* result */
140         res_offset = offset;
141         offset += 8;
142         /* LMF */
143         lmf_offset = offset;
144         offset += sizeof (MonoLMF);
145         //offset += 22 * 8;
146         frame_size = ALIGN_TO (offset, MONO_ARCH_FRAME_ALIGNMENT);
147
148         /* Setup stack frame */
149         imm = frame_size;
150         while (imm > 256) {
151                 arm_subx_imm (code, ARMREG_SP, ARMREG_SP, 256);
152                 imm -= 256;
153         }
154         arm_subx_imm (code, ARMREG_SP, ARMREG_SP, imm);
155         arm_stpx (code, ARMREG_FP, ARMREG_LR, ARMREG_SP, 0);
156         arm_movspx (code, ARMREG_FP, ARMREG_SP);
157
158         /* Save gregs */
159         // FIXME: Optimize this
160         gregs_regset = ~((1 << ARMREG_FP) | (1 << ARMREG_SP));
161         code = mono_arm_emit_store_regarray (code, gregs_regset, ARMREG_FP, gregs_offset);
162         /* Save fregs */
163         for (i = 0; i < num_fregs; ++i)
164                 arm_strfpx (code, i, ARMREG_FP, fregs_offset + (i * 8));
165         /* Save trampoline arg */
166         arm_strx (code, ARMREG_IP1, ARMREG_FP, arg_offset);
167
168         /* Setup LMF */
169         arm_addx_imm (code, ARMREG_IP0, ARMREG_FP, lmf_offset);
170         code = mono_arm_emit_store_regset (code, MONO_ARCH_LMF_REGS, ARMREG_IP0, MONO_STRUCT_OFFSET (MonoLMF, gregs));
171         /* Save caller fp */
172         arm_ldrx (code, ARMREG_IP1, ARMREG_FP, 0);
173         arm_strx (code, ARMREG_IP1, ARMREG_IP0, MONO_STRUCT_OFFSET (MonoLMF, gregs) + (MONO_ARCH_LMF_REG_FP * 8));
174         /* Save caller sp */
175         arm_movx (code, ARMREG_IP1, ARMREG_FP);
176         imm = frame_size;
177         while (imm > 256) {
178                 arm_addx_imm (code, ARMREG_IP1, ARMREG_IP1, 256);
179                 imm -= 256;
180         }
181         arm_addx_imm (code, ARMREG_IP1, ARMREG_IP1, imm);
182         arm_strx (code, ARMREG_IP1, ARMREG_IP0, MONO_STRUCT_OFFSET (MonoLMF, gregs) + (MONO_ARCH_LMF_REG_SP * 8));
183         /* Save caller pc */
184         if (tramp_type == MONO_TRAMPOLINE_JUMP)
185                 arm_movx (code, ARMREG_LR, ARMREG_RZR);
186         else
187                 arm_ldrx (code, ARMREG_LR, ARMREG_FP, 8);
188         arm_strx (code, ARMREG_LR, ARMREG_IP0, MONO_STRUCT_OFFSET (MonoLMF, pc));
189
190         /* Save LMF */
191         /* Similar to emit_save_lmf () */
192         if (aot) {
193                 code = mono_arm_emit_aotconst (&ji, code, buf, ARMREG_IP0, MONO_PATCH_INFO_JIT_ICALL_ADDR, "mono_get_lmf_addr");
194         } else {
195                 tramp = (guint8*)mono_get_lmf_addr;
196                 code = mono_arm_emit_imm64 (code, ARMREG_IP0, (guint64)tramp);
197         }
198         arm_blrx (code, ARMREG_IP0);
199         /* r0 contains the address of the tls slot holding the current lmf */
200         /* ip0 = lmf */
201         arm_addx_imm (code, ARMREG_IP0, ARMREG_FP, lmf_offset);
202         /* lmf->lmf_addr = lmf_addr */
203         arm_strx (code, ARMREG_R0, ARMREG_IP0, MONO_STRUCT_OFFSET (MonoLMF, lmf_addr));
204         /* lmf->previous_lmf = *lmf_addr */
205         arm_ldrx (code, ARMREG_IP1, ARMREG_R0, 0);
206         arm_strx (code, ARMREG_IP1, ARMREG_IP0, MONO_STRUCT_OFFSET (MonoLMF, previous_lmf));
207         /* *lmf_addr = lmf */
208         arm_strx (code, ARMREG_IP0, ARMREG_R0, 0);
209
210         /* Call the C trampoline function */
211         /* Arg 1 = gregs */
212         arm_addx_imm (code, ARMREG_R0, ARMREG_FP, gregs_offset);
213         /* Arg 2 = caller */
214         if (tramp_type == MONO_TRAMPOLINE_JUMP)
215                 arm_movx (code, ARMREG_R1, ARMREG_RZR);
216         else
217                 arm_ldrx (code, ARMREG_R1, ARMREG_FP, gregs_offset + (ARMREG_LR * 8));
218         /* Arg 3 = arg */
219         if (MONO_TRAMPOLINE_TYPE_HAS_ARG (tramp_type))
220                 /* Passed in r0 */
221                 arm_ldrx (code, ARMREG_R2, ARMREG_FP, gregs_offset + (ARMREG_R0 * 8));
222         else
223                 arm_ldrx (code, ARMREG_R2, ARMREG_FP, arg_offset);
224         /* Arg 4 = trampoline addr */
225         arm_movx (code, ARMREG_R3, ARMREG_RZR);
226
227         if (aot) {
228                 char *icall_name = g_strdup_printf ("trampoline_func_%d", tramp_type);
229                 code = mono_arm_emit_aotconst (&ji, code, buf, ARMREG_IP0, MONO_PATCH_INFO_JIT_ICALL_ADDR, icall_name);
230         } else {
231                 tramp = (guint8*)mono_get_trampoline_func (tramp_type);
232                 code = mono_arm_emit_imm64 (code, ARMREG_IP0, (guint64)tramp);
233         }
234         arm_blrx (code, ARMREG_IP0);
235
236         /* Save the result */
237         arm_strx (code, ARMREG_R0, ARMREG_FP, res_offset);
238
239         /* Restore LMF */
240         /* Similar to emit_restore_lmf () */
241         /* Clobbers ip0/ip1 */
242         /* ip0 = lmf */
243         arm_addx_imm (code, ARMREG_IP0, ARMREG_FP, lmf_offset);
244         /* ip1 = lmf->previous_lmf */
245         arm_ldrx (code, ARMREG_IP1, ARMREG_IP0, MONO_STRUCT_OFFSET (MonoLMF, previous_lmf));
246         /* ip0 = lmf->lmf_addr */
247         arm_ldrx (code, ARMREG_IP0, ARMREG_IP0, MONO_STRUCT_OFFSET (MonoLMF, lmf_addr));
248         /* *lmf_addr = previous_lmf */
249         arm_strx (code, ARMREG_IP1, ARMREG_IP0, 0);
250
251         /* Check for thread interruption */
252         /* This is not perf critical code so no need to check the interrupt flag */
253         if (aot) {
254                 code = mono_arm_emit_aotconst (&ji, code, buf, ARMREG_IP0, MONO_PATCH_INFO_JIT_ICALL_ADDR, "mono_thread_force_interruption_checkpoint_noraise");
255         } else {
256                 code = mono_arm_emit_imm64 (code, ARMREG_IP0, (guint64)mono_thread_force_interruption_checkpoint_noraise);
257         }
258         arm_blrx (code, ARMREG_IP0);
259         /* Check whenever there is an exception to be thrown */
260         labels [0] = code;
261         arm_cbnzx (code, ARMREG_R0, 0);
262
263         /* Normal case */
264
265         /* Restore gregs */
266         /* Only have to load the argument regs (r0..r8) and the rgctx reg */
267         code = mono_arm_emit_load_regarray (code, 0x1ff | (1 << ARMREG_LR) | (1 << MONO_ARCH_RGCTX_REG), ARMREG_FP, gregs_offset);
268         /* Restore fregs */
269         for (i = 0; i < num_fregs; ++i)
270                 arm_ldrfpx (code, i, ARMREG_FP, fregs_offset + (i * 8));
271
272         /* Load the result */
273         arm_ldrx (code, ARMREG_IP1, ARMREG_FP, res_offset);
274
275         /* These trampolines return a value */
276         if (tramp_type == MONO_TRAMPOLINE_RGCTX_LAZY_FETCH)
277                 arm_movx (code, ARMREG_R0, ARMREG_IP1);
278
279         /* Cleanup frame */
280         code = mono_arm_emit_destroy_frame (code, frame_size, ((1 << ARMREG_IP0)));
281
282         if (tramp_type == MONO_TRAMPOLINE_RGCTX_LAZY_FETCH)
283                 arm_retx (code, ARMREG_LR);
284         else
285                 arm_brx (code, ARMREG_IP1);
286
287         /* Exception case */
288         mono_arm_patch (labels [0], code, MONO_R_ARM64_CBZ);
289
290         /*
291          * We have an exception we want to throw in the caller's frame, so pop
292          * the trampoline frame and throw from the caller.
293          */
294         code = mono_arm_emit_destroy_frame (code, frame_size, ((1 << ARMREG_IP0)));
295         /* We are in the parent frame, the exception is in x0 */
296         /*
297          * EH is initialized after trampolines, so get the address of the variable
298          * which contains throw_exception, and load it from there.
299          */
300         if (aot) {
301                 /* Not really a jit icall */
302                 code = mono_arm_emit_aotconst (&ji, code, buf, ARMREG_IP0, MONO_PATCH_INFO_JIT_ICALL_ADDR, "throw_exception_addr");
303         } else {
304                 code = mono_arm_emit_imm64 (code, ARMREG_IP0, (guint64)mono_get_throw_exception_addr ());
305         }
306         arm_ldrx (code, ARMREG_IP0, ARMREG_IP0, 0);
307         /* lr contains the return address, the trampoline will use it as the throw site */
308         arm_brx (code, ARMREG_IP0);
309
310         g_assert ((code - buf) < buf_len);
311         mono_arch_flush_icache (buf, code - buf);
312
313         if (info) {
314                 tramp_name = mono_get_generic_trampoline_name (tramp_type);
315                 *info = mono_tramp_info_create (tramp_name, buf, code - buf, ji, unwind_ops);
316                 g_free (tramp_name);
317         }
318
319         return buf;
320 }
321
322 gpointer
323 mono_arch_create_specific_trampoline (gpointer arg1, MonoTrampolineType tramp_type, MonoDomain *domain, guint32 *code_len)
324 {
325         guint8 *code, *buf, *tramp;
326         int buf_len = 64;
327
328         /*
329          * Return a trampoline which calls generic trampoline TRAMP_TYPE passing in ARG1.
330          * Pass the argument in ip1, clobbering ip0.
331          */
332         tramp = mono_get_trampoline_code (tramp_type);
333
334         buf = code = mono_global_codeman_reserve (buf_len);
335
336         code = mono_arm_emit_imm64 (code, ARMREG_IP1, (guint64)arg1);
337         code = mono_arm_emit_imm64 (code, ARMREG_IP0, (guint64)tramp);
338
339         arm_brx (code, ARMREG_IP0);
340
341         g_assert ((code - buf) < buf_len);
342         mono_arch_flush_icache (buf, code - buf);
343         if (code_len)
344                 *code_len = code - buf;
345
346         return buf;
347 }
348
349 gpointer
350 mono_arch_get_unbox_trampoline (MonoMethod *m, gpointer addr)
351 {
352         guint8 *code, *start;
353         guint32 size = 32;
354         MonoDomain *domain = mono_domain_get ();
355
356         start = code = mono_domain_code_reserve (domain, size);
357         code = mono_arm_emit_imm64 (code, ARMREG_IP0, (guint64)addr);
358         arm_addx_imm (code, ARMREG_R0, ARMREG_R0, sizeof (MonoObject));
359         arm_brx (code, ARMREG_IP0);
360
361         g_assert ((code - start) <= size);
362         mono_arch_flush_icache (start, code - start);
363         return start;
364 }
365
366 gpointer
367 mono_arch_get_static_rgctx_trampoline (gpointer arg, gpointer addr)
368 {
369         guint8 *code, *start;
370         guint32 buf_len = 32;
371         MonoDomain *domain = mono_domain_get ();
372
373         start = code = mono_domain_code_reserve (domain, buf_len);
374         code = mono_arm_emit_imm64 (code, MONO_ARCH_RGCTX_REG, (guint64)arg);
375         code = mono_arm_emit_imm64 (code, ARMREG_IP0, (guint64)addr);
376         arm_brx (code, ARMREG_IP0);
377
378         g_assert ((code - start) <= buf_len);
379
380         mono_arch_flush_icache (start, code - start);
381
382         return start;
383 }
384
385 gpointer
386 mono_arch_create_rgctx_lazy_fetch_trampoline (guint32 slot, MonoTrampInfo **info, gboolean aot)
387 {
388         guint8 *code, *buf;
389         int buf_size;
390         int i, depth, index, njumps;
391         gboolean is_mrgctx;
392         guint8 **rgctx_null_jumps;
393         MonoJumpInfo *ji = NULL;
394         GSList *unwind_ops = NULL;
395         guint8 *tramp;
396         guint32 code_len;
397
398         is_mrgctx = MONO_RGCTX_SLOT_IS_MRGCTX (slot);
399         index = MONO_RGCTX_SLOT_INDEX (slot);
400         if (is_mrgctx)
401                 index += MONO_SIZEOF_METHOD_RUNTIME_GENERIC_CONTEXT / sizeof (gpointer);
402         for (depth = 0; ; ++depth) {
403                 int size = mono_class_rgctx_get_array_size (depth, is_mrgctx);
404
405                 if (index < size - 1)
406                         break;
407                 index -= size - 1;
408         }
409
410         buf_size = 64 + 16 * depth;
411         code = buf = mono_global_codeman_reserve (buf_size);
412
413         rgctx_null_jumps = g_malloc0 (sizeof (guint8*) * (depth + 2));
414         njumps = 0;
415
416         /* The vtable/mrgtx is in R0 */
417         g_assert (MONO_ARCH_VTABLE_REG == ARMREG_R0);
418
419         if (is_mrgctx) {
420                 /* get mrgctx ptr */
421                 arm_movx (code, ARMREG_IP1, ARMREG_R0);
422         } else {
423                 /* load rgctx ptr from vtable */
424                 code = mono_arm_emit_ldrx (code, ARMREG_IP1, ARMREG_R0, MONO_STRUCT_OFFSET (MonoVTable, runtime_generic_context));
425                 /* is the rgctx ptr null? */
426                 /* if yes, jump to actual trampoline */
427                 rgctx_null_jumps [njumps ++] = code;
428                 arm_cbzx (code, ARMREG_IP1, 0);
429         }
430
431         for (i = 0; i < depth; ++i) {
432                 /* load ptr to next array */
433                 if (is_mrgctx && i == 0) {
434                         code = mono_arm_emit_ldrx (code, ARMREG_IP1, ARMREG_IP1, MONO_SIZEOF_METHOD_RUNTIME_GENERIC_CONTEXT);
435                 } else {
436                         code = mono_arm_emit_ldrx (code, ARMREG_IP1, ARMREG_IP1, 0);
437                 }
438                 /* is the ptr null? */
439                 /* if yes, jump to actual trampoline */
440                 rgctx_null_jumps [njumps ++] = code;
441                 arm_cbzx (code, ARMREG_IP1, 0);
442         }
443
444         /* fetch slot */
445         code = mono_arm_emit_ldrx (code, ARMREG_IP1, ARMREG_IP1, sizeof (gpointer) * (index + 1));
446         /* is the slot null? */
447         /* if yes, jump to actual trampoline */
448         rgctx_null_jumps [njumps ++] = code;
449         arm_cbzx (code, ARMREG_IP1, 0);
450         /* otherwise return, result is in IP1 */
451         arm_movx (code, ARMREG_R0, ARMREG_IP1);
452         arm_brx (code, ARMREG_LR);
453
454         g_assert (njumps <= depth + 2);
455         for (i = 0; i < njumps; ++i)
456                 mono_arm_patch (rgctx_null_jumps [i], code, MONO_R_ARM64_CBZ);
457
458         g_free (rgctx_null_jumps);
459
460         /* Slowpath */
461
462         /* Call mono_rgctx_lazy_fetch_trampoline (), passing in the slot as argument */
463         /* The vtable/mrgctx is still in R0 */
464         if (aot) {
465                 code = mono_arm_emit_aotconst (&ji, code, buf, ARMREG_IP0, MONO_PATCH_INFO_JIT_ICALL_ADDR, g_strdup_printf ("specific_trampoline_lazy_fetch_%u", slot));
466         } else {
467                 tramp = mono_arch_create_specific_trampoline (GUINT_TO_POINTER (slot), MONO_TRAMPOLINE_RGCTX_LAZY_FETCH, mono_get_root_domain (), &code_len);
468                 code = mono_arm_emit_imm64 (code, ARMREG_IP0, (guint64)tramp);
469         }
470         arm_brx (code, ARMREG_IP0);
471
472         mono_arch_flush_icache (buf, code - buf);
473
474         g_assert (code - buf <= buf_size);
475
476         if (info) {
477                 char *name = mono_get_rgctx_fetch_trampoline_name (slot);
478                 *info = mono_tramp_info_create (name, buf, code - buf, ji, unwind_ops);
479                 g_free (name);
480         }
481
482         return buf;
483 }
484
485 gpointer
486 mono_arch_create_general_rgctx_lazy_fetch_trampoline (MonoTrampInfo **info, gboolean aot)
487 {
488         guint8 *code, *buf;
489         int tramp_size;
490         MonoJumpInfo *ji = NULL;
491         GSList *unwind_ops = NULL;
492
493         g_assert (aot);
494
495         tramp_size = 32;
496
497         code = buf = mono_global_codeman_reserve (tramp_size);
498
499         mono_add_unwind_op_def_cfa (unwind_ops, code, buf, ARMREG_SP, 0);
500
501         // FIXME: Currently, we always go to the slow path.
502         /* Load trampoline addr */
503         arm_ldrx (code, ARMREG_IP0, MONO_ARCH_RGCTX_REG, 8);
504         /* The vtable/mrgctx is in R0 */
505         g_assert (MONO_ARCH_VTABLE_REG == ARMREG_R0);
506         arm_brx (code, ARMREG_IP0);
507
508         mono_arch_flush_icache (buf, code - buf);
509
510         g_assert (code - buf <= tramp_size);
511
512         if (info)
513                 *info = mono_tramp_info_create ("rgctx_fetch_trampoline_general", buf, code - buf, ji, unwind_ops);
514
515         return buf;
516 }
517
518 gpointer
519 mono_arch_create_handler_block_trampoline (MonoTrampInfo **info, gboolean aot)
520 {
521         guint8 *tramp;
522         guint8 *code, *buf;
523         int tramp_size = 64;
524         MonoJumpInfo *ji = NULL;
525         GSList *unwind_ops = NULL;
526
527         code = buf = mono_global_codeman_reserve (tramp_size);
528
529         unwind_ops = NULL;
530
531         /*
532         This trampoline restore the call chain of the handler block then jumps into the code that deals with it.
533         */
534
535         /*
536          * We are in a method frame after the call emitted by OP_CALL_HANDLER.
537          */
538         if (aot)
539                 code = mono_arm_emit_aotconst (&ji, code, buf, ARMREG_IP0, MONO_PATCH_INFO_JIT_ICALL_ADDR, "mono_arm_handler_block_trampoline_helper");
540         else
541                 code = mono_arm_emit_imm64 (code, ARMREG_IP0, (guint64)mono_arm_handler_block_trampoline_helper);
542         /* Set it as the return address so the trampoline will return to it */
543         arm_movx (code, ARMREG_LR, ARMREG_IP0);
544
545         /* Call the trampoline */
546         if (aot) {
547                 char *name = g_strdup_printf ("trampoline_func_%d", MONO_TRAMPOLINE_HANDLER_BLOCK_GUARD);
548                 code = mono_arm_emit_aotconst (&ji, code, buf, ARMREG_IP0, MONO_PATCH_INFO_JIT_ICALL_ADDR, name);
549         } else {
550                 tramp = mono_arch_create_specific_trampoline (NULL, MONO_TRAMPOLINE_HANDLER_BLOCK_GUARD, NULL, NULL);
551                 code = mono_arm_emit_imm64 (code, ARMREG_IP0, (guint64)tramp);
552         }
553         arm_brx (code, ARMREG_IP0);
554
555         mono_arch_flush_icache (buf, code - buf);
556         mono_profiler_code_buffer_new (buf, code - buf, MONO_PROFILER_CODE_BUFFER_HELPER, NULL);
557         g_assert (code - buf <= tramp_size);
558
559         *info = mono_tramp_info_create ("handler_block_trampoline", buf, code - buf, ji, unwind_ops);
560
561         return buf;
562 }
563
564 /*
565  * mono_arch_create_sdb_trampoline:
566  *
567  *   Return a trampoline which captures the current context, passes it to
568  * debugger_agent_single_step_from_context ()/debugger_agent_breakpoint_from_context (),
569  * then restores the (potentially changed) context.
570  */
571 guint8*
572 mono_arch_create_sdb_trampoline (gboolean single_step, MonoTrampInfo **info, gboolean aot)
573 {
574         int tramp_size = 512;
575         int offset, imm, frame_size, ctx_offset;
576         guint64 gregs_regset;
577         guint8 *code, *buf;
578         GSList *unwind_ops = NULL;
579         MonoJumpInfo *ji = NULL;
580
581         code = buf = mono_global_codeman_reserve (tramp_size);
582
583         /* Compute stack frame size and offsets */
584         offset = 0;
585         /* frame block */
586         offset += 2 * 8;
587         /* MonoContext */
588         ctx_offset = offset;
589         offset += sizeof (MonoContext);
590         offset = ALIGN_TO (offset, MONO_ARCH_FRAME_ALIGNMENT);
591         frame_size = offset;
592
593         // FIXME: Unwind info
594
595         /* Setup stack frame */
596         imm = frame_size;
597         while (imm > 256) {
598                 arm_subx_imm (code, ARMREG_SP, ARMREG_SP, 256);
599                 imm -= 256;
600         }
601         arm_subx_imm (code, ARMREG_SP, ARMREG_SP, imm);
602         arm_stpx (code, ARMREG_FP, ARMREG_LR, ARMREG_SP, 0);
603         arm_movspx (code, ARMREG_FP, ARMREG_SP);
604
605         /* Initialize a MonoContext structure on the stack */
606         /* No need to save fregs */
607         gregs_regset = ~((1 << ARMREG_FP) | (1 << ARMREG_SP));
608         code = mono_arm_emit_store_regarray (code, gregs_regset, ARMREG_FP, ctx_offset + G_STRUCT_OFFSET (MonoContext, regs));
609         /* Save caller fp */
610         arm_ldrx (code, ARMREG_IP1, ARMREG_FP, 0);
611         arm_strx (code, ARMREG_IP1, ARMREG_FP, ctx_offset + G_STRUCT_OFFSET (MonoContext, regs) + (ARMREG_FP * 8));
612         /* Save caller sp */
613         arm_movx (code, ARMREG_IP1, ARMREG_FP);
614         imm = frame_size;
615         while (imm > 256) {
616                 arm_addx_imm (code, ARMREG_IP1, ARMREG_IP1, 256);
617                 imm -= 256;
618         }
619         arm_addx_imm (code, ARMREG_IP1, ARMREG_IP1, imm);
620         arm_strx (code, ARMREG_IP1, ARMREG_FP, ctx_offset + G_STRUCT_OFFSET (MonoContext, regs) + (ARMREG_SP * 8));
621         /* Save caller ip */
622         arm_ldrx (code, ARMREG_IP1, ARMREG_FP, 8);
623         arm_strx (code, ARMREG_IP1, ARMREG_FP, ctx_offset + G_STRUCT_OFFSET (MonoContext, pc));
624
625         /* Call the single step/breakpoint function in sdb */
626         /* Arg1 = ctx */
627         arm_addx_imm (code, ARMREG_R0, ARMREG_FP, ctx_offset);
628         if (aot) {
629                 if (single_step)
630                         code = mono_arm_emit_aotconst (&ji, code, buf, ARMREG_IP0, MONO_PATCH_INFO_JIT_ICALL_ADDR, "debugger_agent_single_step_from_context");
631                 else
632                         code = mono_arm_emit_aotconst (&ji, code, buf, ARMREG_IP0, MONO_PATCH_INFO_JIT_ICALL_ADDR, "debugger_agent_breakpoint_from_context");
633         } else {
634                 gpointer addr = single_step ? debugger_agent_single_step_from_context : debugger_agent_breakpoint_from_context;
635
636                 code = mono_arm_emit_imm64 (code, ARMREG_IP0, (guint64)addr);
637         }
638         arm_blrx (code, ARMREG_IP0);
639
640         /* Restore ctx */
641         /* Save fp/pc into the frame block */
642         arm_ldrx (code, ARMREG_IP0, ARMREG_FP, ctx_offset + G_STRUCT_OFFSET (MonoContext, regs) + (ARMREG_FP * 8));
643         arm_strx (code, ARMREG_IP0, ARMREG_FP, 0);
644         arm_ldrx (code, ARMREG_IP0, ARMREG_FP, ctx_offset + G_STRUCT_OFFSET (MonoContext, pc));
645         arm_strx (code, ARMREG_IP0, ARMREG_FP, 8);
646         gregs_regset = ~((1 << ARMREG_FP) | (1 << ARMREG_SP));
647         code = mono_arm_emit_load_regarray (code, gregs_regset, ARMREG_FP, ctx_offset + G_STRUCT_OFFSET (MonoContext, regs));
648
649         code = mono_arm_emit_destroy_frame (code, frame_size, ((1 << ARMREG_IP0) | (1 << ARMREG_IP1)));
650
651         arm_retx (code, ARMREG_LR);
652
653         mono_arch_flush_icache (code, code - buf);
654         g_assert (code - buf <= tramp_size);
655
656         const char *tramp_name = single_step ? "sdb_single_step_trampoline" : "sdb_breakpoint_trampoline";
657         *info = mono_tramp_info_create (tramp_name, buf, code - buf, ji, unwind_ops);
658
659         return buf;
660 }
661
662 #else /* DISABLE_JIT */
663
664 guchar*
665 mono_arch_create_generic_trampoline (MonoTrampolineType tramp_type, MonoTrampInfo **info, gboolean aot)
666 {
667         g_assert_not_reached ();
668         return NULL;
669 }
670
671 gpointer
672 mono_arch_create_specific_trampoline (gpointer arg1, MonoTrampolineType tramp_type, MonoDomain *domain, guint32 *code_len)
673 {
674         g_assert_not_reached ();
675         return NULL;
676 }
677
678 gpointer
679 mono_arch_create_handler_block_trampoline (MonoTrampInfo **info, gboolean aot)
680 {
681         g_assert_not_reached ();
682         return NULL;
683 }
684
685 gpointer
686 mono_arch_get_unbox_trampoline (MonoMethod *m, gpointer addr)
687 {
688         g_assert_not_reached ();
689         return NULL;
690 }
691
692 gpointer
693 mono_arch_get_static_rgctx_trampoline (gpointer arg, gpointer addr)
694 {
695         g_assert_not_reached ();
696         return NULL;
697 }
698
699 gpointer
700 mono_arch_create_rgctx_lazy_fetch_trampoline (guint32 slot, MonoTrampInfo **info, gboolean aot)
701 {
702         g_assert_not_reached ();
703         return NULL;
704 }
705
706 gpointer
707 mono_arch_get_nullified_class_init_trampoline (MonoTrampInfo **info)
708 {
709         g_assert_not_reached ();
710         return NULL;
711 }
712
713 guint8*
714 mono_arch_create_sdb_trampoline (gboolean single_step, MonoTrampInfo **info, gboolean aot)
715 {
716         g_assert_not_reached ();
717         return NULL;
718 }
719
720 #endif /* !DISABLE_JIT */