[arm64] Remove an assert which is hit when mono_arch_get_call_target () is called...
[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 #ifdef ENABLE_INTERPRETER
25 #include "interp/interp.h"
26 #endif
27
28
29 #define ALIGN_TO(val,align) ((((guint64)val) + ((align) - 1)) & ~((align) - 1))
30
31 void
32 mono_arch_patch_callsite (guint8 *method_start, guint8 *code_ptr, guint8 *addr)
33 {
34         mono_arm_patch (code_ptr - 4, addr, MONO_R_ARM64_BL);
35         mono_arch_flush_icache (code_ptr - 4, 4);
36 }
37
38 void
39 mono_arch_patch_plt_entry (guint8 *code, gpointer *got, mgreg_t *regs, guint8 *addr)
40 {
41         guint32 ins;
42         guint64 slot_addr;
43         int disp;
44
45         /* 
46          * Decode the address loaded by the PLT entry emitted by arch_emit_plt_entry () in
47          * aot-compiler.c
48          */
49
50         /* adrp */
51         ins = ((guint32*)code) [0];
52         g_assert (((ins >> 24) & 0x1f) == 0x10);
53         disp = (((ins >> 5) & 0x7ffff) << 2) | ((ins >> 29) & 0x3);
54         /* FIXME: disp is signed */
55         g_assert ((disp >> 20) == 0);
56
57         slot_addr = ((guint64)code + (disp << 12)) & ~0xfff;
58
59         /* add x16, x16, :lo12:got */
60         ins = ((guint32*)code) [1];
61         g_assert (((ins >> 22) & 0x3) == 0);
62         slot_addr += (ins >> 10) & 0xfff;
63
64         /* ldr x16, [x16, <offset>] */
65         ins = ((guint32*)code) [2];
66         g_assert (((ins >> 24) & 0x3f) == 0x39);
67         slot_addr += ((ins >> 10) & 0xfff) * 8;
68
69         g_assert (*(guint64*)slot_addr);
70         *(gpointer*)slot_addr = addr;
71 }
72
73 guint8*
74 mono_arch_get_call_target (guint8 *code)
75 {
76         guint32 imm;
77         int disp;
78
79         code -= 4;
80
81         imm = *(guint32*)code;
82         /* Should be a b/bl */
83         g_assert (((imm >> 26) & 0x7) == 0x5);
84
85         disp = (imm & 0x3ffffff);
86         if ((disp >> 25) != 0)
87                 /* Negative, sing extend to 32 bits */
88                 disp = disp | 0xfc000000;
89
90         return code + (disp * 4);
91 }
92
93 guint32
94 mono_arch_get_plt_info_offset (guint8 *plt_entry, mgreg_t *regs, guint8 *code)
95 {
96         /* The offset is stored as the 5th word of the plt entry */
97         return ((guint32*)plt_entry) [4];
98 }
99
100 gpointer
101 mono_arm_handler_block_trampoline_helper (gpointer *ptr)
102 {
103         MonoJitTlsData *jit_tls = mono_tls_get_jit_tls ();
104         return jit_tls->handler_block_return_address;
105 }
106
107 #ifndef DISABLE_JIT
108
109 guchar*
110 mono_arch_create_generic_trampoline (MonoTrampolineType tramp_type, MonoTrampInfo **info, gboolean aot)
111 {
112         guint8 *code, *buf, *tramp, *labels [16];
113         int i, buf_len, imm;
114         int frame_size, offset, gregs_offset, num_fregs, fregs_offset, arg_offset, lmf_offset, res_offset;
115         guint64 gregs_regset;
116         GSList *unwind_ops = NULL;
117         MonoJumpInfo *ji = NULL;
118         char *tramp_name;
119
120         buf_len = 768;
121         buf = code = mono_global_codeman_reserve (buf_len);
122
123         /*
124          * We are getting called by a specific trampoline, ip1 contains the trampoline argument.
125          */
126
127         /* Compute stack frame size and offsets */
128         offset = 0;
129         /* frame block */
130         offset += 2 * 8;
131         /* gregs */
132         gregs_offset = offset;
133         offset += 32 * 8;
134         /* fregs */
135         // FIXME: Save 128 bits
136         /* Only have to save the argument regs */
137         num_fregs = 8;
138         fregs_offset = offset;
139         offset += num_fregs * 8;
140         /* arg */
141         arg_offset = offset;
142         offset += 8;
143         /* result */
144         res_offset = offset;
145         offset += 8;
146         /* LMF */
147         lmf_offset = offset;
148         offset += sizeof (MonoLMF);
149         //offset += 22 * 8;
150         frame_size = ALIGN_TO (offset, MONO_ARCH_FRAME_ALIGNMENT);
151
152         /* Setup stack frame */
153         imm = frame_size;
154         while (imm > 256) {
155                 arm_subx_imm (code, ARMREG_SP, ARMREG_SP, 256);
156                 imm -= 256;
157         }
158         arm_subx_imm (code, ARMREG_SP, ARMREG_SP, imm);
159         arm_stpx (code, ARMREG_FP, ARMREG_LR, ARMREG_SP, 0);
160         arm_movspx (code, ARMREG_FP, ARMREG_SP);
161
162         /* Save gregs */
163         // FIXME: Optimize this
164         gregs_regset = ~((1 << ARMREG_FP) | (1 << ARMREG_SP));
165         code = mono_arm_emit_store_regarray (code, gregs_regset, ARMREG_FP, gregs_offset);
166         /* Save fregs */
167         for (i = 0; i < num_fregs; ++i)
168                 arm_strfpx (code, i, ARMREG_FP, fregs_offset + (i * 8));
169         /* Save trampoline arg */
170         arm_strx (code, ARMREG_IP1, ARMREG_FP, arg_offset);
171
172         /* Setup LMF */
173         arm_addx_imm (code, ARMREG_IP0, ARMREG_FP, lmf_offset);
174         code = mono_arm_emit_store_regset (code, MONO_ARCH_LMF_REGS, ARMREG_IP0, MONO_STRUCT_OFFSET (MonoLMF, gregs));
175         /* Save caller fp */
176         arm_ldrx (code, ARMREG_IP1, ARMREG_FP, 0);
177         arm_strx (code, ARMREG_IP1, ARMREG_IP0, MONO_STRUCT_OFFSET (MonoLMF, gregs) + (MONO_ARCH_LMF_REG_FP * 8));
178         /* Save caller sp */
179         arm_movx (code, ARMREG_IP1, ARMREG_FP);
180         imm = frame_size;
181         while (imm > 256) {
182                 arm_addx_imm (code, ARMREG_IP1, ARMREG_IP1, 256);
183                 imm -= 256;
184         }
185         arm_addx_imm (code, ARMREG_IP1, ARMREG_IP1, imm);
186         arm_strx (code, ARMREG_IP1, ARMREG_IP0, MONO_STRUCT_OFFSET (MonoLMF, gregs) + (MONO_ARCH_LMF_REG_SP * 8));
187         /* Save caller pc */
188         if (tramp_type == MONO_TRAMPOLINE_JUMP)
189                 arm_movx (code, ARMREG_LR, ARMREG_RZR);
190         else
191                 arm_ldrx (code, ARMREG_LR, ARMREG_FP, 8);
192         arm_strx (code, ARMREG_LR, ARMREG_IP0, MONO_STRUCT_OFFSET (MonoLMF, pc));
193
194         /* Save LMF */
195         /* Similar to emit_save_lmf () */
196         if (aot) {
197                 code = mono_arm_emit_aotconst (&ji, code, buf, ARMREG_IP0, MONO_PATCH_INFO_JIT_ICALL_ADDR, "mono_get_lmf_addr");
198         } else {
199                 tramp = (guint8*)mono_get_lmf_addr;
200                 code = mono_arm_emit_imm64 (code, ARMREG_IP0, (guint64)tramp);
201         }
202         arm_blrx (code, ARMREG_IP0);
203         /* r0 contains the address of the tls slot holding the current lmf */
204         /* ip0 = lmf */
205         arm_addx_imm (code, ARMREG_IP0, ARMREG_FP, lmf_offset);
206         /* lmf->lmf_addr = lmf_addr */
207         arm_strx (code, ARMREG_R0, ARMREG_IP0, MONO_STRUCT_OFFSET (MonoLMF, lmf_addr));
208         /* lmf->previous_lmf = *lmf_addr */
209         arm_ldrx (code, ARMREG_IP1, ARMREG_R0, 0);
210         arm_strx (code, ARMREG_IP1, ARMREG_IP0, MONO_STRUCT_OFFSET (MonoLMF, previous_lmf));
211         /* *lmf_addr = lmf */
212         arm_strx (code, ARMREG_IP0, ARMREG_R0, 0);
213
214         /* Call the C trampoline function */
215         /* Arg 1 = gregs */
216         arm_addx_imm (code, ARMREG_R0, ARMREG_FP, gregs_offset);
217         /* Arg 2 = caller */
218         if (tramp_type == MONO_TRAMPOLINE_JUMP)
219                 arm_movx (code, ARMREG_R1, ARMREG_RZR);
220         else
221                 arm_ldrx (code, ARMREG_R1, ARMREG_FP, gregs_offset + (ARMREG_LR * 8));
222         /* Arg 3 = arg */
223         if (MONO_TRAMPOLINE_TYPE_HAS_ARG (tramp_type))
224                 /* Passed in r0 */
225                 arm_ldrx (code, ARMREG_R2, ARMREG_FP, gregs_offset + (ARMREG_R0 * 8));
226         else
227                 arm_ldrx (code, ARMREG_R2, ARMREG_FP, arg_offset);
228         /* Arg 4 = trampoline addr */
229         arm_movx (code, ARMREG_R3, ARMREG_RZR);
230
231         if (aot) {
232                 char *icall_name = g_strdup_printf ("trampoline_func_%d", tramp_type);
233                 code = mono_arm_emit_aotconst (&ji, code, buf, ARMREG_IP0, MONO_PATCH_INFO_JIT_ICALL_ADDR, icall_name);
234         } else {
235                 tramp = (guint8*)mono_get_trampoline_func (tramp_type);
236                 code = mono_arm_emit_imm64 (code, ARMREG_IP0, (guint64)tramp);
237         }
238         arm_blrx (code, ARMREG_IP0);
239
240         /* Save the result */
241         arm_strx (code, ARMREG_R0, ARMREG_FP, res_offset);
242
243         /* Restore LMF */
244         /* Similar to emit_restore_lmf () */
245         /* Clobbers ip0/ip1 */
246         /* ip0 = lmf */
247         arm_addx_imm (code, ARMREG_IP0, ARMREG_FP, lmf_offset);
248         /* ip1 = lmf->previous_lmf */
249         arm_ldrx (code, ARMREG_IP1, ARMREG_IP0, MONO_STRUCT_OFFSET (MonoLMF, previous_lmf));
250         /* ip0 = lmf->lmf_addr */
251         arm_ldrx (code, ARMREG_IP0, ARMREG_IP0, MONO_STRUCT_OFFSET (MonoLMF, lmf_addr));
252         /* *lmf_addr = previous_lmf */
253         arm_strx (code, ARMREG_IP1, ARMREG_IP0, 0);
254
255         /* Check for thread interruption */
256         /* This is not perf critical code so no need to check the interrupt flag */
257         if (aot) {
258                 code = mono_arm_emit_aotconst (&ji, code, buf, ARMREG_IP0, MONO_PATCH_INFO_JIT_ICALL_ADDR, "mono_thread_force_interruption_checkpoint_noraise");
259         } else {
260                 code = mono_arm_emit_imm64 (code, ARMREG_IP0, (guint64)mono_thread_force_interruption_checkpoint_noraise);
261         }
262         arm_blrx (code, ARMREG_IP0);
263         /* Check whenever there is an exception to be thrown */
264         labels [0] = code;
265         arm_cbnzx (code, ARMREG_R0, 0);
266
267         /* Normal case */
268
269         /* Restore gregs */
270         /* Only have to load the argument regs (r0..r8) and the rgctx reg */
271         code = mono_arm_emit_load_regarray (code, 0x1ff | (1 << ARMREG_LR) | (1 << MONO_ARCH_RGCTX_REG), ARMREG_FP, gregs_offset);
272         /* Restore fregs */
273         for (i = 0; i < num_fregs; ++i)
274                 arm_ldrfpx (code, i, ARMREG_FP, fregs_offset + (i * 8));
275
276         /* Load the result */
277         arm_ldrx (code, ARMREG_IP1, ARMREG_FP, res_offset);
278
279         /* These trampolines return a value */
280         if (tramp_type == MONO_TRAMPOLINE_RGCTX_LAZY_FETCH)
281                 arm_movx (code, ARMREG_R0, ARMREG_IP1);
282
283         /* Cleanup frame */
284         code = mono_arm_emit_destroy_frame (code, frame_size, ((1 << ARMREG_IP0)));
285
286         if (tramp_type == MONO_TRAMPOLINE_RGCTX_LAZY_FETCH)
287                 arm_retx (code, ARMREG_LR);
288         else
289                 arm_brx (code, ARMREG_IP1);
290
291         /* Exception case */
292         mono_arm_patch (labels [0], code, MONO_R_ARM64_CBZ);
293
294         /*
295          * We have an exception we want to throw in the caller's frame, so pop
296          * the trampoline frame and throw from the caller.
297          */
298         code = mono_arm_emit_destroy_frame (code, frame_size, ((1 << ARMREG_IP0)));
299         /* We are in the parent frame, the exception is in x0 */
300         /*
301          * EH is initialized after trampolines, so get the address of the variable
302          * which contains throw_exception, and load it from there.
303          */
304         if (aot) {
305                 /* Not really a jit icall */
306                 code = mono_arm_emit_aotconst (&ji, code, buf, ARMREG_IP0, MONO_PATCH_INFO_JIT_ICALL_ADDR, "throw_exception_addr");
307         } else {
308                 code = mono_arm_emit_imm64 (code, ARMREG_IP0, (guint64)mono_get_throw_exception_addr ());
309         }
310         arm_ldrx (code, ARMREG_IP0, ARMREG_IP0, 0);
311         /* lr contains the return address, the trampoline will use it as the throw site */
312         arm_brx (code, ARMREG_IP0);
313
314         g_assert ((code - buf) < buf_len);
315         mono_arch_flush_icache (buf, code - buf);
316
317         if (info) {
318                 tramp_name = mono_get_generic_trampoline_name (tramp_type);
319                 *info = mono_tramp_info_create (tramp_name, buf, code - buf, ji, unwind_ops);
320                 g_free (tramp_name);
321         }
322
323         return buf;
324 }
325
326 gpointer
327 mono_arch_create_specific_trampoline (gpointer arg1, MonoTrampolineType tramp_type, MonoDomain *domain, guint32 *code_len)
328 {
329         guint8 *code, *buf, *tramp;
330         int buf_len = 64;
331
332         /*
333          * Return a trampoline which calls generic trampoline TRAMP_TYPE passing in ARG1.
334          * Pass the argument in ip1, clobbering ip0.
335          */
336         tramp = mono_get_trampoline_code (tramp_type);
337
338         buf = code = mono_global_codeman_reserve (buf_len);
339
340         code = mono_arm_emit_imm64 (code, ARMREG_IP1, (guint64)arg1);
341         code = mono_arm_emit_imm64 (code, ARMREG_IP0, (guint64)tramp);
342
343         arm_brx (code, ARMREG_IP0);
344
345         g_assert ((code - buf) < buf_len);
346         mono_arch_flush_icache (buf, code - buf);
347         if (code_len)
348                 *code_len = code - buf;
349
350         return buf;
351 }
352
353 gpointer
354 mono_arch_get_unbox_trampoline (MonoMethod *m, gpointer addr)
355 {
356         guint8 *code, *start;
357         guint32 size = 32;
358         MonoDomain *domain = mono_domain_get ();
359
360         start = code = mono_domain_code_reserve (domain, size);
361         code = mono_arm_emit_imm64 (code, ARMREG_IP0, (guint64)addr);
362         arm_addx_imm (code, ARMREG_R0, ARMREG_R0, sizeof (MonoObject));
363         arm_brx (code, ARMREG_IP0);
364
365         g_assert ((code - start) <= size);
366         mono_arch_flush_icache (start, code - start);
367         return start;
368 }
369
370 gpointer
371 mono_arch_get_static_rgctx_trampoline (gpointer arg, gpointer addr)
372 {
373         guint8 *code, *start;
374         guint32 buf_len = 32;
375         MonoDomain *domain = mono_domain_get ();
376
377         start = code = mono_domain_code_reserve (domain, buf_len);
378         code = mono_arm_emit_imm64 (code, MONO_ARCH_RGCTX_REG, (guint64)arg);
379         code = mono_arm_emit_imm64 (code, ARMREG_IP0, (guint64)addr);
380         arm_brx (code, ARMREG_IP0);
381
382         g_assert ((code - start) <= buf_len);
383
384         mono_arch_flush_icache (start, code - start);
385
386         return start;
387 }
388
389 gpointer
390 mono_arch_create_rgctx_lazy_fetch_trampoline (guint32 slot, MonoTrampInfo **info, gboolean aot)
391 {
392         guint8 *code, *buf;
393         int buf_size;
394         int i, depth, index, njumps;
395         gboolean is_mrgctx;
396         guint8 **rgctx_null_jumps;
397         MonoJumpInfo *ji = NULL;
398         GSList *unwind_ops = NULL;
399         guint8 *tramp;
400         guint32 code_len;
401
402         is_mrgctx = MONO_RGCTX_SLOT_IS_MRGCTX (slot);
403         index = MONO_RGCTX_SLOT_INDEX (slot);
404         if (is_mrgctx)
405                 index += MONO_SIZEOF_METHOD_RUNTIME_GENERIC_CONTEXT / sizeof (gpointer);
406         for (depth = 0; ; ++depth) {
407                 int size = mono_class_rgctx_get_array_size (depth, is_mrgctx);
408
409                 if (index < size - 1)
410                         break;
411                 index -= size - 1;
412         }
413
414         buf_size = 64 + 16 * depth;
415         code = buf = mono_global_codeman_reserve (buf_size);
416
417         rgctx_null_jumps = g_malloc0 (sizeof (guint8*) * (depth + 2));
418         njumps = 0;
419
420         /* The vtable/mrgtx is in R0 */
421         g_assert (MONO_ARCH_VTABLE_REG == ARMREG_R0);
422
423         if (is_mrgctx) {
424                 /* get mrgctx ptr */
425                 arm_movx (code, ARMREG_IP1, ARMREG_R0);
426         } else {
427                 /* load rgctx ptr from vtable */
428                 code = mono_arm_emit_ldrx (code, ARMREG_IP1, ARMREG_R0, MONO_STRUCT_OFFSET (MonoVTable, runtime_generic_context));
429                 /* is the rgctx ptr null? */
430                 /* if yes, jump to actual trampoline */
431                 rgctx_null_jumps [njumps ++] = code;
432                 arm_cbzx (code, ARMREG_IP1, 0);
433         }
434
435         for (i = 0; i < depth; ++i) {
436                 /* load ptr to next array */
437                 if (is_mrgctx && i == 0) {
438                         code = mono_arm_emit_ldrx (code, ARMREG_IP1, ARMREG_IP1, MONO_SIZEOF_METHOD_RUNTIME_GENERIC_CONTEXT);
439                 } else {
440                         code = mono_arm_emit_ldrx (code, ARMREG_IP1, ARMREG_IP1, 0);
441                 }
442                 /* is the ptr null? */
443                 /* if yes, jump to actual trampoline */
444                 rgctx_null_jumps [njumps ++] = code;
445                 arm_cbzx (code, ARMREG_IP1, 0);
446         }
447
448         /* fetch slot */
449         code = mono_arm_emit_ldrx (code, ARMREG_IP1, ARMREG_IP1, sizeof (gpointer) * (index + 1));
450         /* is the slot null? */
451         /* if yes, jump to actual trampoline */
452         rgctx_null_jumps [njumps ++] = code;
453         arm_cbzx (code, ARMREG_IP1, 0);
454         /* otherwise return, result is in IP1 */
455         arm_movx (code, ARMREG_R0, ARMREG_IP1);
456         arm_brx (code, ARMREG_LR);
457
458         g_assert (njumps <= depth + 2);
459         for (i = 0; i < njumps; ++i)
460                 mono_arm_patch (rgctx_null_jumps [i], code, MONO_R_ARM64_CBZ);
461
462         g_free (rgctx_null_jumps);
463
464         /* Slowpath */
465
466         /* Call mono_rgctx_lazy_fetch_trampoline (), passing in the slot as argument */
467         /* The vtable/mrgctx is still in R0 */
468         if (aot) {
469                 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));
470         } else {
471                 tramp = mono_arch_create_specific_trampoline (GUINT_TO_POINTER (slot), MONO_TRAMPOLINE_RGCTX_LAZY_FETCH, mono_get_root_domain (), &code_len);
472                 code = mono_arm_emit_imm64 (code, ARMREG_IP0, (guint64)tramp);
473         }
474         arm_brx (code, ARMREG_IP0);
475
476         mono_arch_flush_icache (buf, code - buf);
477
478         g_assert (code - buf <= buf_size);
479
480         if (info) {
481                 char *name = mono_get_rgctx_fetch_trampoline_name (slot);
482                 *info = mono_tramp_info_create (name, buf, code - buf, ji, unwind_ops);
483                 g_free (name);
484         }
485
486         return buf;
487 }
488
489 gpointer
490 mono_arch_create_general_rgctx_lazy_fetch_trampoline (MonoTrampInfo **info, gboolean aot)
491 {
492         guint8 *code, *buf;
493         int tramp_size;
494         MonoJumpInfo *ji = NULL;
495         GSList *unwind_ops = NULL;
496
497         g_assert (aot);
498
499         tramp_size = 32;
500
501         code = buf = mono_global_codeman_reserve (tramp_size);
502
503         mono_add_unwind_op_def_cfa (unwind_ops, code, buf, ARMREG_SP, 0);
504
505         // FIXME: Currently, we always go to the slow path.
506         /* Load trampoline addr */
507         arm_ldrx (code, ARMREG_IP0, MONO_ARCH_RGCTX_REG, 8);
508         /* The vtable/mrgctx is in R0 */
509         g_assert (MONO_ARCH_VTABLE_REG == ARMREG_R0);
510         arm_brx (code, ARMREG_IP0);
511
512         mono_arch_flush_icache (buf, code - buf);
513
514         g_assert (code - buf <= tramp_size);
515
516         if (info)
517                 *info = mono_tramp_info_create ("rgctx_fetch_trampoline_general", buf, code - buf, ji, unwind_ops);
518
519         return buf;
520 }
521
522 gpointer
523 mono_arch_create_handler_block_trampoline (MonoTrampInfo **info, gboolean aot)
524 {
525         guint8 *tramp;
526         guint8 *code, *buf;
527         int tramp_size = 64;
528         MonoJumpInfo *ji = NULL;
529         GSList *unwind_ops = NULL;
530
531         code = buf = mono_global_codeman_reserve (tramp_size);
532
533         unwind_ops = NULL;
534
535         /*
536         This trampoline restore the call chain of the handler block then jumps into the code that deals with it.
537         */
538
539         /*
540          * We are in a method frame after the call emitted by OP_CALL_HANDLER.
541          */
542         if (aot)
543                 code = mono_arm_emit_aotconst (&ji, code, buf, ARMREG_IP0, MONO_PATCH_INFO_JIT_ICALL_ADDR, "mono_arm_handler_block_trampoline_helper");
544         else
545                 code = mono_arm_emit_imm64 (code, ARMREG_IP0, (guint64)mono_arm_handler_block_trampoline_helper);
546         /* Set it as the return address so the trampoline will return to it */
547         arm_movx (code, ARMREG_LR, ARMREG_IP0);
548
549         /* Call the trampoline */
550         if (aot) {
551                 char *name = g_strdup_printf ("trampoline_func_%d", MONO_TRAMPOLINE_HANDLER_BLOCK_GUARD);
552                 code = mono_arm_emit_aotconst (&ji, code, buf, ARMREG_IP0, MONO_PATCH_INFO_JIT_ICALL_ADDR, name);
553         } else {
554                 tramp = mono_arch_create_specific_trampoline (NULL, MONO_TRAMPOLINE_HANDLER_BLOCK_GUARD, NULL, NULL);
555                 code = mono_arm_emit_imm64 (code, ARMREG_IP0, (guint64)tramp);
556         }
557         arm_brx (code, ARMREG_IP0);
558
559         mono_arch_flush_icache (buf, code - buf);
560         mono_profiler_code_buffer_new (buf, code - buf, MONO_PROFILER_CODE_BUFFER_HELPER, NULL);
561         g_assert (code - buf <= tramp_size);
562
563         *info = mono_tramp_info_create ("handler_block_trampoline", buf, code - buf, ji, unwind_ops);
564
565         return buf;
566 }
567
568 /*
569  * mono_arch_create_sdb_trampoline:
570  *
571  *   Return a trampoline which captures the current context, passes it to
572  * debugger_agent_single_step_from_context ()/debugger_agent_breakpoint_from_context (),
573  * then restores the (potentially changed) context.
574  */
575 guint8*
576 mono_arch_create_sdb_trampoline (gboolean single_step, MonoTrampInfo **info, gboolean aot)
577 {
578         int tramp_size = 512;
579         int offset, imm, frame_size, ctx_offset;
580         guint64 gregs_regset;
581         guint8 *code, *buf;
582         GSList *unwind_ops = NULL;
583         MonoJumpInfo *ji = NULL;
584
585         code = buf = mono_global_codeman_reserve (tramp_size);
586
587         /* Compute stack frame size and offsets */
588         offset = 0;
589         /* frame block */
590         offset += 2 * 8;
591         /* MonoContext */
592         ctx_offset = offset;
593         offset += sizeof (MonoContext);
594         offset = ALIGN_TO (offset, MONO_ARCH_FRAME_ALIGNMENT);
595         frame_size = offset;
596
597         // FIXME: Unwind info
598
599         /* Setup stack frame */
600         imm = frame_size;
601         while (imm > 256) {
602                 arm_subx_imm (code, ARMREG_SP, ARMREG_SP, 256);
603                 imm -= 256;
604         }
605         arm_subx_imm (code, ARMREG_SP, ARMREG_SP, imm);
606         arm_stpx (code, ARMREG_FP, ARMREG_LR, ARMREG_SP, 0);
607         arm_movspx (code, ARMREG_FP, ARMREG_SP);
608
609         /* Initialize a MonoContext structure on the stack */
610         /* No need to save fregs */
611         gregs_regset = ~((1 << ARMREG_FP) | (1 << ARMREG_SP));
612         code = mono_arm_emit_store_regarray (code, gregs_regset, ARMREG_FP, ctx_offset + G_STRUCT_OFFSET (MonoContext, regs));
613         /* Save caller fp */
614         arm_ldrx (code, ARMREG_IP1, ARMREG_FP, 0);
615         arm_strx (code, ARMREG_IP1, ARMREG_FP, ctx_offset + G_STRUCT_OFFSET (MonoContext, regs) + (ARMREG_FP * 8));
616         /* Save caller sp */
617         arm_movx (code, ARMREG_IP1, ARMREG_FP);
618         imm = frame_size;
619         while (imm > 256) {
620                 arm_addx_imm (code, ARMREG_IP1, ARMREG_IP1, 256);
621                 imm -= 256;
622         }
623         arm_addx_imm (code, ARMREG_IP1, ARMREG_IP1, imm);
624         arm_strx (code, ARMREG_IP1, ARMREG_FP, ctx_offset + G_STRUCT_OFFSET (MonoContext, regs) + (ARMREG_SP * 8));
625         /* Save caller ip */
626         arm_ldrx (code, ARMREG_IP1, ARMREG_FP, 8);
627         arm_strx (code, ARMREG_IP1, ARMREG_FP, ctx_offset + G_STRUCT_OFFSET (MonoContext, pc));
628
629         /* Call the single step/breakpoint function in sdb */
630         /* Arg1 = ctx */
631         arm_addx_imm (code, ARMREG_R0, ARMREG_FP, ctx_offset);
632         if (aot) {
633                 if (single_step)
634                         code = mono_arm_emit_aotconst (&ji, code, buf, ARMREG_IP0, MONO_PATCH_INFO_JIT_ICALL_ADDR, "debugger_agent_single_step_from_context");
635                 else
636                         code = mono_arm_emit_aotconst (&ji, code, buf, ARMREG_IP0, MONO_PATCH_INFO_JIT_ICALL_ADDR, "debugger_agent_breakpoint_from_context");
637         } else {
638                 gpointer addr = single_step ? debugger_agent_single_step_from_context : debugger_agent_breakpoint_from_context;
639
640                 code = mono_arm_emit_imm64 (code, ARMREG_IP0, (guint64)addr);
641         }
642         arm_blrx (code, ARMREG_IP0);
643
644         /* Restore ctx */
645         /* Save fp/pc into the frame block */
646         arm_ldrx (code, ARMREG_IP0, ARMREG_FP, ctx_offset + G_STRUCT_OFFSET (MonoContext, regs) + (ARMREG_FP * 8));
647         arm_strx (code, ARMREG_IP0, ARMREG_FP, 0);
648         arm_ldrx (code, ARMREG_IP0, ARMREG_FP, ctx_offset + G_STRUCT_OFFSET (MonoContext, pc));
649         arm_strx (code, ARMREG_IP0, ARMREG_FP, 8);
650         gregs_regset = ~((1 << ARMREG_FP) | (1 << ARMREG_SP));
651         code = mono_arm_emit_load_regarray (code, gregs_regset, ARMREG_FP, ctx_offset + G_STRUCT_OFFSET (MonoContext, regs));
652
653         code = mono_arm_emit_destroy_frame (code, frame_size, ((1 << ARMREG_IP0) | (1 << ARMREG_IP1)));
654
655         arm_retx (code, ARMREG_LR);
656
657         mono_arch_flush_icache (code, code - buf);
658         g_assert (code - buf <= tramp_size);
659
660         const char *tramp_name = single_step ? "sdb_single_step_trampoline" : "sdb_breakpoint_trampoline";
661         *info = mono_tramp_info_create (tramp_name, buf, code - buf, ji, unwind_ops);
662
663         return buf;
664 }
665
666 /*
667  * mono_arch_get_enter_icall_trampoline:
668  *
669  *   See tramp-amd64.c for documentation.
670  */
671 gpointer
672 mono_arch_get_enter_icall_trampoline (MonoTrampInfo **info)
673 {
674 #ifdef ENABLE_INTERPRETER
675         const int gregs_num = INTERP_ICALL_TRAMP_IARGS;
676         const int fregs_num = INTERP_ICALL_TRAMP_FARGS;
677
678         guint8 *start = NULL, *code, *label_gexits [gregs_num], *label_fexits [fregs_num], *label_leave_tramp [3], *label_is_float_ret;
679         MonoJumpInfo *ji = NULL;
680         GSList *unwind_ops = NULL;
681         int buf_len, i, framesize = 0, off_methodargs, off_targetaddr;
682
683         buf_len = 512 + 1024;
684         start = code = (guint8 *) mono_global_codeman_reserve (buf_len);
685
686         /* save FP and LR */
687         framesize += 2 * sizeof (mgreg_t);
688
689         off_methodargs = framesize;
690         framesize += sizeof (mgreg_t);
691
692         off_targetaddr = framesize;
693         framesize += sizeof (mgreg_t);
694
695         framesize = ALIGN_TO (framesize, MONO_ARCH_FRAME_ALIGNMENT);
696
697         /* allocate space on stack for argument passing */
698         const int stack_space = ALIGN_TO (((gregs_num - ARMREG_R7) * sizeof (mgreg_t)), MONO_ARCH_FRAME_ALIGNMENT);
699
700         arm_subx_imm (code, ARMREG_SP, ARMREG_SP, stack_space + framesize);
701         arm_stpx (code, ARMREG_FP, ARMREG_LR, ARMREG_SP, stack_space);
702         arm_addx_imm (code, ARMREG_FP, ARMREG_SP, stack_space);
703
704         /* save InterpMethodArguments* onto stack */
705         arm_strx (code, ARMREG_R1, ARMREG_FP, off_methodargs);
706
707         /* save target address onto stack */
708         arm_strx (code, ARMREG_R0, ARMREG_FP, off_targetaddr);
709
710         /* load pointer to InterpMethodArguments* into IP0 */
711         arm_movx (code, ARMREG_IP0, ARMREG_R1);
712
713         /* move flen into R9 */
714         arm_ldrx (code, ARMREG_R9, ARMREG_IP0, MONO_STRUCT_OFFSET (InterpMethodArguments, flen));
715         /* load pointer to fargs into R10 */
716         arm_ldrx (code, ARMREG_R10, ARMREG_IP0, MONO_STRUCT_OFFSET (InterpMethodArguments, fargs));
717
718         for (i = 0; i < fregs_num; ++i) {
719                 arm_cmpx_imm (code, ARMREG_R9, 0);
720                 label_fexits [i] = code;
721                 arm_bcc (code, ARMCOND_EQ, 0);
722
723                 g_assert (i <= ARMREG_D7); /* otherwise, need to pass args on stack */
724                 arm_ldrfpx (code, i, ARMREG_R10, i * sizeof (double));
725                 arm_subx_imm (code, ARMREG_R9, ARMREG_R9, 1);
726         }
727
728         for (i = 0; i < fregs_num; i++)
729                 mono_arm_patch (label_fexits [i], code, MONO_R_ARM64_BCC);
730
731         /* move ilen into R9 */
732         arm_ldrx (code, ARMREG_R9, ARMREG_IP0, MONO_STRUCT_OFFSET (InterpMethodArguments, ilen));
733         /* load pointer to iargs into R10 */
734         arm_ldrx (code, ARMREG_R10, ARMREG_IP0, MONO_STRUCT_OFFSET (InterpMethodArguments, iargs));
735
736         int stack_offset = 0;
737         for (i = 0; i < gregs_num; i++) {
738                 arm_cmpx_imm (code, ARMREG_R9, 0);
739                 label_gexits [i] = code;
740                 arm_bcc (code, ARMCOND_EQ, 0);
741
742                 if (i <= ARMREG_R7) {
743                         arm_ldrx (code, i, ARMREG_R10, i * sizeof (mgreg_t));
744                 } else {
745                         arm_ldrx (code, ARMREG_R11, ARMREG_R10, i * sizeof (mgreg_t));
746                         arm_strx (code, ARMREG_R11, ARMREG_SP, stack_offset);
747                         stack_offset += sizeof (mgreg_t);
748                 }
749                 arm_subx_imm (code, ARMREG_R9, ARMREG_R9, 1);
750         }
751
752         for (i = 0; i < gregs_num; i++)
753                 mono_arm_patch (label_gexits [i], code, MONO_R_ARM64_BCC);
754
755         /* load target addr */
756         arm_ldrx (code, ARMREG_R11, ARMREG_FP, off_targetaddr);
757
758         /* call into native function */
759         arm_blrx (code, ARMREG_R11);
760
761         /* load InterpMethodArguments */
762         arm_ldrx (code, ARMREG_IP0, ARMREG_FP, off_methodargs);
763
764         /* load is_float_ret */
765         arm_ldrx (code, ARMREG_R11, ARMREG_IP0, MONO_STRUCT_OFFSET (InterpMethodArguments, is_float_ret));
766
767         /* check if a float return value is expected */
768         arm_cmpx_imm (code, ARMREG_R11, 0);
769         label_is_float_ret = code;
770         arm_bcc (code, ARMCOND_NE, 0);
771
772         /* greg return */
773         /* load retval */
774         arm_ldrx (code, ARMREG_R11, ARMREG_IP0, MONO_STRUCT_OFFSET (InterpMethodArguments, retval));
775
776         arm_cmpx_imm (code, ARMREG_R11, 0);
777         label_leave_tramp [0] = code;
778         arm_bcc (code, ARMCOND_EQ, 0);
779
780         /* store greg result */
781         arm_strx (code, ARMREG_R0, ARMREG_R11, 0);
782
783         label_leave_tramp [1] = code;
784         arm_bcc (code, ARMCOND_AL, 0);
785
786         /* freg return */
787         mono_arm_patch (label_is_float_ret, code, MONO_R_ARM64_BCC);
788         /* load retval */
789         arm_ldrx (code, ARMREG_R11, ARMREG_IP0, MONO_STRUCT_OFFSET (InterpMethodArguments, retval));
790
791         arm_cmpx_imm (code, ARMREG_R11, 0);
792         label_leave_tramp [2] = code;
793         arm_bcc (code, ARMCOND_EQ, 0);
794
795         /* store freg result */
796         arm_strfpx (code, ARMREG_D0, ARMREG_R11, 0);
797
798         for (i = 0; i < 3; i++)
799                 mono_arm_patch (label_leave_tramp [i], code, MONO_R_ARM64_BCC);
800
801         arm_movspx (code, ARMREG_SP, ARMREG_FP);
802         arm_ldpx (code, ARMREG_FP, ARMREG_LR, ARMREG_SP, 0);
803         arm_addx_imm (code, ARMREG_SP, ARMREG_SP, framesize);
804         arm_retx (code, ARMREG_LR);
805
806         g_assert (code - start < buf_len);
807
808         mono_arch_flush_icache (start, code - start);
809         mono_profiler_code_buffer_new (start, code - start, MONO_PROFILER_CODE_BUFFER_EXCEPTION_HANDLING, NULL);
810
811         if (info)
812                 *info = mono_tramp_info_create ("enter_icall_trampoline", start, code - start, ji, unwind_ops);
813
814         return start;
815 #else
816         g_assert_not_reached ();
817         return NULL;
818 #endif /* ENABLE_INTERPRETER */
819 }
820
821 #else /* DISABLE_JIT */
822
823 guchar*
824 mono_arch_create_generic_trampoline (MonoTrampolineType tramp_type, MonoTrampInfo **info, gboolean aot)
825 {
826         g_assert_not_reached ();
827         return NULL;
828 }
829
830 gpointer
831 mono_arch_create_specific_trampoline (gpointer arg1, MonoTrampolineType tramp_type, MonoDomain *domain, guint32 *code_len)
832 {
833         g_assert_not_reached ();
834         return NULL;
835 }
836
837 gpointer
838 mono_arch_create_handler_block_trampoline (MonoTrampInfo **info, gboolean aot)
839 {
840         g_assert_not_reached ();
841         return NULL;
842 }
843
844 gpointer
845 mono_arch_get_unbox_trampoline (MonoMethod *m, gpointer addr)
846 {
847         g_assert_not_reached ();
848         return NULL;
849 }
850
851 gpointer
852 mono_arch_get_static_rgctx_trampoline (gpointer arg, gpointer addr)
853 {
854         g_assert_not_reached ();
855         return NULL;
856 }
857
858 gpointer
859 mono_arch_create_rgctx_lazy_fetch_trampoline (guint32 slot, MonoTrampInfo **info, gboolean aot)
860 {
861         g_assert_not_reached ();
862         return NULL;
863 }
864
865 gpointer
866 mono_arch_get_nullified_class_init_trampoline (MonoTrampInfo **info)
867 {
868         g_assert_not_reached ();
869         return NULL;
870 }
871
872 guint8*
873 mono_arch_create_sdb_trampoline (gboolean single_step, MonoTrampInfo **info, gboolean aot)
874 {
875         g_assert_not_reached ();
876         return NULL;
877 }
878
879 gpointer
880 mono_arch_get_enter_icall_trampoline (MonoTrampInfo **info)
881 {
882         g_assert_not_reached ();
883         return NULL;
884 }
885 #endif /* !DISABLE_JIT */