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