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