[LoongArch64] Part-5:add loongarch support in some files for LoongArch64. (#21769)
[mono-project.git] / mono / mini / tramp-arm64.c
blob555229028011c982f751d632e0cb38bb55bfc7db
1 /**
2 * \file
3 * JIT trampoline code for ARM64
5 * Copyright 2013 Xamarin Inc
7 * Based on tramp-arm.c:
8 *
9 * Authors:
10 * Paolo Molaro (lupus@ximian.com)
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.
18 #include "mini.h"
19 #include "mini-runtime.h"
20 #include "debugger-agent.h"
22 #include <mono/arch/arm64/arm64-codegen.h>
23 #include <mono/metadata/abi-details.h>
25 #ifndef DISABLE_INTERPRETER
26 #include "interp/interp.h"
27 #endif
28 #include "mono/utils/mono-tls-inline.h"
30 void
31 mono_arch_patch_callsite (guint8 *method_start, guint8 *code_ptr, guint8 *addr)
33 MINI_BEGIN_CODEGEN ();
34 mono_arm_patch (code_ptr - 4, addr, MONO_R_ARM64_BL);
35 MINI_END_CODEGEN (code_ptr - 4, 4, -1, NULL);
38 void
39 mono_arch_patch_plt_entry (guint8 *code, gpointer *got, host_mgreg_t *regs, guint8 *addr)
41 guint32 ins;
42 guint64 slot_addr;
43 int disp;
45 /*
46 * Decode the address loaded by the PLT entry emitted by arch_emit_plt_entry () in
47 * aot-compiler.c
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);
57 slot_addr = ((guint64)code + (disp << 12)) & ~0xfff;
59 /* add x16, x16, :lo12:got */
60 ins = ((guint32*)code) [1];
61 g_assert (((ins >> 22) & 0x3) == 0);
62 slot_addr += (ins >> 10) & 0xfff;
64 /* ldr x16, [x16, <offset>] */
65 ins = ((guint32*)code) [2];
66 g_assert (((ins >> 24) & 0x3f) == 0x39);
67 slot_addr += ((ins >> 10) & 0xfff) * 8;
69 g_assert (*(guint64*)slot_addr);
70 *(gpointer*)slot_addr = addr;
73 guint8*
74 mono_arch_get_call_target (guint8 *code)
76 code -= 4;
77 guint32 ins = *(guint32 *)code;
78 /* Should be a b/bl */
79 if (((ins >> 26) & 0x1f) != 0x5)
80 return NULL;
81 gint32 disp = ((gint32)((ins & 0x3ffffff) << 6)) >> 6;
82 return code + (disp * 4);
85 guint32
86 mono_arch_get_plt_info_offset (guint8 *plt_entry, host_mgreg_t *regs, guint8 *code)
88 /* The offset is stored as the 5th word of the plt entry */
89 return ((guint32*)plt_entry) [4];
92 #ifndef DISABLE_JIT
94 guchar*
95 mono_arch_create_generic_trampoline (MonoTrampolineType tramp_type, MonoTrampInfo **info, gboolean aot)
97 guint8 *code, *buf, *tramp, *labels [16];
98 int i, buf_len, imm;
99 int frame_size, offset, gregs_offset, num_fregs, fregs_offset, arg_offset, lmf_offset, res_offset;
100 guint64 gregs_regset;
101 GSList *unwind_ops = NULL;
102 MonoJumpInfo *ji = NULL;
103 const char *tramp_name;
105 buf_len = 768;
106 buf = code = mono_global_codeman_reserve (buf_len);
109 * We are getting called by a specific trampoline, ip1 contains the trampoline argument.
112 /* Compute stack frame size and offsets */
113 offset = 0;
114 /* frame block */
115 offset += 2 * 8;
116 /* gregs */
117 gregs_offset = offset;
118 offset += 32 * 8;
119 /* fregs */
120 // FIXME: Save 128 bits
121 /* Only have to save the argument regs */
122 num_fregs = 8;
123 fregs_offset = offset;
124 offset += num_fregs * 8;
125 /* arg */
126 arg_offset = offset;
127 offset += 8;
128 /* result */
129 res_offset = offset;
130 offset += 8;
131 /* LMF */
132 lmf_offset = offset;
133 offset += sizeof (MonoLMF);
134 //offset += 22 * 8;
135 frame_size = ALIGN_TO (offset, MONO_ARCH_FRAME_ALIGNMENT);
137 MINI_BEGIN_CODEGEN ();
139 /* Setup stack frame */
140 imm = frame_size;
141 mono_add_unwind_op_def_cfa (unwind_ops, code, buf, ARMREG_SP, 0);
142 while (imm > 256) {
143 arm_subx_imm (code, ARMREG_SP, ARMREG_SP, 256);
144 imm -= 256;
145 mono_add_unwind_op_def_cfa_offset (unwind_ops, code, buf, frame_size - imm);
147 arm_subx_imm (code, ARMREG_SP, ARMREG_SP, imm);
148 mono_add_unwind_op_def_cfa_offset (unwind_ops, code, buf, frame_size);
150 arm_stpx (code, ARMREG_FP, ARMREG_LR, ARMREG_SP, 0);
151 mono_add_unwind_op_offset (unwind_ops, code, buf, ARMREG_LR, -frame_size + 8);
152 mono_add_unwind_op_offset (unwind_ops, code, buf, ARMREG_FP, -frame_size);
154 arm_movspx (code, ARMREG_FP, ARMREG_SP);
155 mono_add_unwind_op_def_cfa_reg (unwind_ops, code, buf, ARMREG_FP);
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);
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;
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));
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, GUINT_TO_POINTER (MONO_JIT_ICALL_mono_get_lmf_addr));
193 } else {
194 tramp = (guint8*)mono_get_lmf_addr;
195 code = mono_arm_emit_imm64 (code, ARMREG_IP0, (guint64)tramp);
197 code = mono_arm_emit_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_strp (code, ARMREG_R0, ARMREG_IP0, MONO_STRUCT_OFFSET (MonoLMF, lmf_addr));
203 /* lmf->previous_lmf = *lmf_addr */
204 arm_ldrp (code, ARMREG_IP1, ARMREG_R0, 0);
205 arm_strp (code, ARMREG_IP1, ARMREG_IP0, MONO_STRUCT_OFFSET (MonoLMF, previous_lmf));
206 /* *lmf_addr = lmf */
207 arm_strp (code, ARMREG_IP0, ARMREG_R0, 0);
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);
226 if (aot) {
227 code = mono_arm_emit_aotconst (&ji, code, buf, ARMREG_IP0, MONO_PATCH_INFO_JIT_ICALL_ADDR, GINT_TO_POINTER (mono_trampoline_type_to_jit_icall_id (tramp_type)));
228 } else {
229 tramp = (guint8*)mono_get_trampoline_func (tramp_type);
230 code = mono_arm_emit_imm64 (code, ARMREG_IP0, (guint64)tramp);
232 code = mono_arm_emit_blrx (code, ARMREG_IP0);
234 /* Save the result */
235 arm_strx (code, ARMREG_R0, ARMREG_FP, res_offset);
237 /* Restore LMF */
238 /* Similar to emit_restore_lmf () */
239 /* Clobbers ip0/ip1 */
240 /* ip0 = lmf */
241 arm_addx_imm (code, ARMREG_IP0, ARMREG_FP, lmf_offset);
242 /* ip1 = lmf->previous_lmf */
243 arm_ldrp (code, ARMREG_IP1, ARMREG_IP0, MONO_STRUCT_OFFSET (MonoLMF, previous_lmf));
244 /* ip0 = lmf->lmf_addr */
245 arm_ldrp (code, ARMREG_IP0, ARMREG_IP0, MONO_STRUCT_OFFSET (MonoLMF, lmf_addr));
246 /* *lmf_addr = previous_lmf */
247 arm_strp (code, ARMREG_IP1, ARMREG_IP0, 0);
249 /* Check for thread interruption */
250 /* This is not perf critical code so no need to check the interrupt flag */
251 if (aot) {
252 code = mono_arm_emit_aotconst (&ji, code, buf, ARMREG_IP0, MONO_PATCH_INFO_JIT_ICALL_ADDR, GUINT_TO_POINTER (MONO_JIT_ICALL_mono_thread_force_interruption_checkpoint_noraise));
253 } else {
254 code = mono_arm_emit_imm64 (code, ARMREG_IP0, (guint64)mono_thread_force_interruption_checkpoint_noraise);
256 code = mono_arm_emit_blrx (code, ARMREG_IP0);
257 /* Check whenever there is an exception to be thrown */
258 labels [0] = code;
259 arm_cbnzx (code, ARMREG_R0, 0);
261 /* Normal case */
263 /* Restore gregs */
264 /* Only have to load the argument regs (r0..r8) and the rgctx reg */
265 code = mono_arm_emit_load_regarray (code, 0x1ff | (1 << ARMREG_LR) | (1 << MONO_ARCH_RGCTX_REG), ARMREG_FP, gregs_offset);
266 /* Restore fregs */
267 for (i = 0; i < num_fregs; ++i)
268 arm_ldrfpx (code, i, ARMREG_FP, fregs_offset + (i * 8));
270 /* Load the result */
271 arm_ldrx (code, ARMREG_IP1, ARMREG_FP, res_offset);
273 /* These trampolines return a value */
274 if (tramp_type == MONO_TRAMPOLINE_RGCTX_LAZY_FETCH)
275 arm_movx (code, ARMREG_R0, ARMREG_IP1);
277 /* Cleanup frame */
278 code = mono_arm_emit_destroy_frame (code, frame_size, ((1 << ARMREG_IP0)));
280 if (tramp_type == MONO_TRAMPOLINE_RGCTX_LAZY_FETCH) {
281 arm_retx (code, ARMREG_LR);
282 } else {
283 code = mono_arm_emit_brx (code, ARMREG_IP1);
286 /* Exception case */
287 mono_arm_patch (labels [0], code, MONO_R_ARM64_CBZ);
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.
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 */
296 * EH is initialized after trampolines, so get the address of the variable
297 * which contains throw_exception, and load it from there.
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, GUINT_TO_POINTER (MONO_JIT_ICALL_mono_rethrow_preserve_exception));
302 } else {
303 code = mono_arm_emit_imm64 (code, ARMREG_IP0, (guint64)mono_get_rethrow_preserve_exception_addr ());
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 code = mono_arm_emit_brx (code, ARMREG_IP0);
309 g_assert ((code - buf) < buf_len);
311 MINI_END_CODEGEN (buf, code - buf, MONO_PROFILER_CODE_BUFFER_HELPER, NULL);
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);
318 return (guchar*)MINI_ADDR_TO_FTNPTR (buf);
321 gpointer
322 mono_arch_create_specific_trampoline (gpointer arg1, MonoTrampolineType tramp_type, MonoMemoryManager *mem_manager, guint32 *code_len)
324 guint8 *code, *buf, *tramp;
325 int buf_len = 64;
328 * Return a trampoline which calls generic trampoline TRAMP_TYPE passing in ARG1.
329 * Pass the argument in ip1, clobbering ip0.
331 tramp = mono_get_trampoline_code (tramp_type);
333 buf = code = mono_mem_manager_code_reserve (mem_manager, buf_len);
335 MINI_BEGIN_CODEGEN ();
337 code = mono_arm_emit_imm64 (code, ARMREG_IP1, (guint64)arg1);
338 code = mono_arm_emit_imm64 (code, ARMREG_IP0, (guint64)tramp);
340 code = mono_arm_emit_brx (code, ARMREG_IP0);
342 g_assert ((code - buf) < buf_len);
344 MINI_END_CODEGEN (buf, code - buf, MONO_PROFILER_CODE_BUFFER_SPECIFIC_TRAMPOLINE, mono_get_generic_trampoline_simple_name (tramp_type));
346 if (code_len)
347 *code_len = code - buf;
349 return (gpointer)MINI_ADDR_TO_FTNPTR (buf);
352 gpointer
353 mono_arch_get_unbox_trampoline (MonoMethod *m, gpointer addr)
355 guint8 *code, *start;
356 guint32 size = 32;
357 MonoDomain *domain = mono_domain_get ();
358 MonoMemoryManager *mem_manager = m_method_get_mem_manager (domain, m);
360 start = code = mono_mem_manager_code_reserve (mem_manager, size);
362 MINI_BEGIN_CODEGEN ();
364 // FIXME: Maybe make a normal non-ptrauth call ?
366 code = mono_arm_emit_imm64 (code, ARMREG_IP0, (guint64)addr);
367 arm_addx_imm (code, ARMREG_R0, ARMREG_R0, MONO_ABI_SIZEOF (MonoObject));
368 code = mono_arm_emit_brx (code, ARMREG_IP0);
370 g_assert ((code - start) <= size);
372 MINI_END_CODEGEN (start, code - start, MONO_PROFILER_CODE_BUFFER_UNBOX_TRAMPOLINE, m);
374 return (gpointer)MINI_ADDR_TO_FTNPTR (start);
377 gpointer
378 mono_arch_get_static_rgctx_trampoline (MonoMemoryManager *mem_manager, gpointer arg, gpointer addr)
380 guint8 *code, *start;
381 guint32 buf_len = 32;
383 start = code = mono_mem_manager_code_reserve (mem_manager, buf_len);
385 MINI_BEGIN_CODEGEN ();
387 code = mono_arm_emit_imm64 (code, MONO_ARCH_RGCTX_REG, (guint64)arg);
388 code = mono_arm_emit_imm64 (code, ARMREG_IP0, (guint64)addr);
389 code = mono_arm_emit_brx (code, ARMREG_IP0);
391 MINI_END_CODEGEN (start, code - start, MONO_PROFILER_CODE_BUFFER_GENERICS_TRAMPOLINE, NULL);
393 g_assert ((code - start) <= buf_len);
395 return (gpointer)MINI_ADDR_TO_FTNPTR (start);
398 gpointer
399 mono_arch_create_rgctx_lazy_fetch_trampoline (guint32 slot, MonoTrampInfo **info, gboolean aot)
401 guint8 *code, *buf;
402 int buf_size;
403 int i, depth, index, njumps;
404 gboolean is_mrgctx;
405 guint8 **rgctx_null_jumps;
406 MonoJumpInfo *ji = NULL;
407 GSList *unwind_ops = NULL;
408 guint8 *tramp;
409 guint32 code_len;
411 is_mrgctx = MONO_RGCTX_SLOT_IS_MRGCTX (slot);
412 index = MONO_RGCTX_SLOT_INDEX (slot);
413 if (is_mrgctx)
414 index += MONO_SIZEOF_METHOD_RUNTIME_GENERIC_CONTEXT / sizeof (target_mgreg_t);
415 for (depth = 0; ; ++depth) {
416 int size = mono_class_rgctx_get_array_size (depth, is_mrgctx);
418 if (index < size - 1)
419 break;
420 index -= size - 1;
423 buf_size = 64 + 16 * depth;
424 code = buf = mono_global_codeman_reserve (buf_size);
426 rgctx_null_jumps = g_malloc0 (sizeof (guint8*) * (depth + 2));
427 njumps = 0;
429 /* The vtable/mrgtx is in R0 */
430 g_assert (MONO_ARCH_VTABLE_REG == ARMREG_R0);
432 MINI_BEGIN_CODEGEN ();
434 if (is_mrgctx) {
435 /* get mrgctx ptr */
436 arm_movx (code, ARMREG_IP1, ARMREG_R0);
437 } else {
438 /* load rgctx ptr from vtable */
439 code = mono_arm_emit_ldrx (code, ARMREG_IP1, ARMREG_R0, MONO_STRUCT_OFFSET (MonoVTable, runtime_generic_context));
440 /* is the rgctx ptr null? */
441 /* if yes, jump to actual trampoline */
442 rgctx_null_jumps [njumps ++] = code;
443 arm_cbzx (code, ARMREG_IP1, 0);
446 for (i = 0; i < depth; ++i) {
447 /* load ptr to next array */
448 if (is_mrgctx && i == 0) {
449 code = mono_arm_emit_ldrx (code, ARMREG_IP1, ARMREG_IP1, MONO_SIZEOF_METHOD_RUNTIME_GENERIC_CONTEXT);
450 } else {
451 code = mono_arm_emit_ldrx (code, ARMREG_IP1, ARMREG_IP1, 0);
453 /* is the ptr null? */
454 /* if yes, jump to actual trampoline */
455 rgctx_null_jumps [njumps ++] = code;
456 arm_cbzx (code, ARMREG_IP1, 0);
459 /* fetch slot */
460 code = mono_arm_emit_ldrx (code, ARMREG_IP1, ARMREG_IP1, sizeof (target_mgreg_t) * (index + 1));
461 /* is the slot null? */
462 /* if yes, jump to actual trampoline */
463 rgctx_null_jumps [njumps ++] = code;
464 arm_cbzx (code, ARMREG_IP1, 0);
465 /* otherwise return, result is in IP1 */
466 arm_movx (code, ARMREG_R0, ARMREG_IP1);
467 arm_brx (code, ARMREG_LR);
469 g_assert (njumps <= depth + 2);
470 for (i = 0; i < njumps; ++i)
471 mono_arm_patch (rgctx_null_jumps [i], code, MONO_R_ARM64_CBZ);
473 g_free (rgctx_null_jumps);
475 /* Slowpath */
477 /* Call mono_rgctx_lazy_fetch_trampoline (), passing in the slot as argument */
478 /* The vtable/mrgctx is still in R0 */
479 if (aot) {
480 code = mono_arm_emit_aotconst (&ji, code, buf, ARMREG_IP0, MONO_PATCH_INFO_SPECIFIC_TRAMPOLINE_LAZY_FETCH_ADDR, GUINT_TO_POINTER (slot));
481 } else {
482 MonoMemoryManager *mem_manager = mono_domain_ambient_memory_manager (mono_get_root_domain ());
483 tramp = (guint8*)mono_arch_create_specific_trampoline (GUINT_TO_POINTER (slot), MONO_TRAMPOLINE_RGCTX_LAZY_FETCH, mem_manager, &code_len);
484 code = mono_arm_emit_imm64 (code, ARMREG_IP0, (guint64)tramp);
486 code = mono_arm_emit_brx (code, ARMREG_IP0);
488 g_assert (code - buf <= buf_size);
490 MINI_END_CODEGEN (buf, code - buf, MONO_PROFILER_CODE_BUFFER_GENERICS_TRAMPOLINE, NULL);
492 if (info) {
493 char *name = mono_get_rgctx_fetch_trampoline_name (slot);
494 *info = mono_tramp_info_create (name, buf, code - buf, ji, unwind_ops);
495 g_free (name);
498 return (gpointer)MINI_ADDR_TO_FTNPTR (buf);
501 gpointer
502 mono_arch_create_general_rgctx_lazy_fetch_trampoline (MonoTrampInfo **info, gboolean aot)
504 guint8 *code, *buf;
505 int tramp_size;
506 MonoJumpInfo *ji = NULL;
507 GSList *unwind_ops = NULL;
509 g_assert (aot);
511 tramp_size = 32;
513 code = buf = mono_global_codeman_reserve (tramp_size);
515 mono_add_unwind_op_def_cfa (unwind_ops, code, buf, ARMREG_SP, 0);
517 MINI_BEGIN_CODEGEN ();
519 // FIXME: Currently, we always go to the slow path.
520 /* Load trampoline addr */
521 arm_ldrx (code, ARMREG_IP0, MONO_ARCH_RGCTX_REG, 8);
522 /* The vtable/mrgctx is in R0 */
523 g_assert (MONO_ARCH_VTABLE_REG == ARMREG_R0);
524 code = mono_arm_emit_brx (code, ARMREG_IP0);
526 g_assert (code - buf <= tramp_size);
528 MINI_END_CODEGEN (buf, code - buf, MONO_PROFILER_CODE_BUFFER_GENERICS_TRAMPOLINE, NULL);
530 if (info)
531 *info = mono_tramp_info_create ("rgctx_fetch_trampoline_general", buf, code - buf, ji, unwind_ops);
533 return (gpointer)MINI_ADDR_TO_FTNPTR (buf);
537 * mono_arch_create_sdb_trampoline:
539 * Return a trampoline which captures the current context, passes it to
540 * mini_get_dbg_callbacks ()->single_step_from_context ()/mini_get_dbg_callbacks ()->breakpoint_from_context (),
541 * then restores the (potentially changed) context.
543 guint8*
544 mono_arch_create_sdb_trampoline (gboolean single_step, MonoTrampInfo **info, gboolean aot)
546 int tramp_size = 512;
547 int offset, imm, frame_size, ctx_offset;
548 guint64 gregs_regset;
549 guint8 *code, *buf;
550 GSList *unwind_ops = NULL;
551 MonoJumpInfo *ji = NULL;
553 code = buf = mono_global_codeman_reserve (tramp_size);
555 /* Compute stack frame size and offsets */
556 offset = 0;
557 /* frame block */
558 offset += 2 * 8;
559 /* MonoContext */
560 ctx_offset = offset;
561 offset += sizeof (MonoContext);
562 offset = ALIGN_TO (offset, MONO_ARCH_FRAME_ALIGNMENT);
563 frame_size = offset;
565 // FIXME: Unwind info
567 MINI_BEGIN_CODEGEN ();
569 /* Setup stack frame */
570 imm = frame_size;
571 while (imm > 256) {
572 arm_subx_imm (code, ARMREG_SP, ARMREG_SP, 256);
573 imm -= 256;
575 arm_subx_imm (code, ARMREG_SP, ARMREG_SP, imm);
576 arm_stpx (code, ARMREG_FP, ARMREG_LR, ARMREG_SP, 0);
577 arm_movspx (code, ARMREG_FP, ARMREG_SP);
579 /* Initialize a MonoContext structure on the stack */
580 /* No need to save fregs */
581 gregs_regset = ~((1 << ARMREG_FP) | (1 << ARMREG_SP));
582 code = mono_arm_emit_store_regarray (code, gregs_regset, ARMREG_FP, ctx_offset + G_STRUCT_OFFSET (MonoContext, regs));
583 /* Save caller fp */
584 arm_ldrx (code, ARMREG_IP1, ARMREG_FP, 0);
585 arm_strx (code, ARMREG_IP1, ARMREG_FP, ctx_offset + G_STRUCT_OFFSET (MonoContext, regs) + (ARMREG_FP * 8));
586 /* Save caller sp */
587 arm_movx (code, ARMREG_IP1, ARMREG_FP);
588 imm = frame_size;
589 while (imm > 256) {
590 arm_addx_imm (code, ARMREG_IP1, ARMREG_IP1, 256);
591 imm -= 256;
593 arm_addx_imm (code, ARMREG_IP1, ARMREG_IP1, imm);
594 arm_strx (code, ARMREG_IP1, ARMREG_FP, ctx_offset + G_STRUCT_OFFSET (MonoContext, regs) + (ARMREG_SP * 8));
595 /* Save caller ip */
596 arm_ldrx (code, ARMREG_IP1, ARMREG_FP, 8);
597 arm_strx (code, ARMREG_IP1, ARMREG_FP, ctx_offset + G_STRUCT_OFFSET (MonoContext, pc));
599 /* Call the single step/breakpoint function in sdb */
600 /* Arg1 = ctx */
601 arm_addx_imm (code, ARMREG_R0, ARMREG_FP, ctx_offset);
602 if (aot) {
603 if (single_step)
604 code = mono_arm_emit_aotconst (&ji, code, buf, ARMREG_IP0, MONO_PATCH_INFO_JIT_ICALL_ADDR, GUINT_TO_POINTER (MONO_JIT_ICALL_mono_debugger_agent_single_step_from_context));
605 else
606 code = mono_arm_emit_aotconst (&ji, code, buf, ARMREG_IP0, MONO_PATCH_INFO_JIT_ICALL_ADDR, GUINT_TO_POINTER (MONO_JIT_ICALL_mono_debugger_agent_breakpoint_from_context));
607 } else {
608 void (*addr) (MonoContext *ctx) = single_step ? mini_get_dbg_callbacks ()->single_step_from_context : mini_get_dbg_callbacks ()->breakpoint_from_context;
610 code = mono_arm_emit_imm64 (code, ARMREG_IP0, (guint64)addr);
612 code = mono_arm_emit_blrx (code, ARMREG_IP0);
614 /* Restore ctx */
615 /* Save fp/pc into the frame block */
616 arm_ldrx (code, ARMREG_IP0, ARMREG_FP, ctx_offset + G_STRUCT_OFFSET (MonoContext, regs) + (ARMREG_FP * 8));
617 arm_strx (code, ARMREG_IP0, ARMREG_FP, 0);
618 arm_ldrx (code, ARMREG_IP0, ARMREG_FP, ctx_offset + G_STRUCT_OFFSET (MonoContext, pc));
619 arm_strx (code, ARMREG_IP0, ARMREG_FP, 8);
620 gregs_regset = ~((1 << ARMREG_FP) | (1 << ARMREG_SP));
621 code = mono_arm_emit_load_regarray (code, gregs_regset, ARMREG_FP, ctx_offset + G_STRUCT_OFFSET (MonoContext, regs));
623 code = mono_arm_emit_destroy_frame (code, frame_size, ((1 << ARMREG_IP0) | (1 << ARMREG_IP1)));
625 arm_retx (code, ARMREG_LR);
627 g_assert (code - buf <= tramp_size);
629 MINI_END_CODEGEN (buf, code - buf, MONO_PROFILER_CODE_BUFFER_HELPER, NULL);
631 const char *tramp_name = single_step ? "sdb_single_step_trampoline" : "sdb_breakpoint_trampoline";
632 *info = mono_tramp_info_create (tramp_name, buf, code - buf, ji, unwind_ops);
634 return (guint8*)MINI_ADDR_TO_FTNPTR (buf);
638 * mono_arch_get_interp_to_native_trampoline:
640 * See tramp-amd64.c for documentation.
642 gpointer
643 mono_arch_get_interp_to_native_trampoline (MonoTrampInfo **info)
645 #ifndef DISABLE_INTERPRETER
646 guint8 *start = NULL, *code;
647 guint8 *label_start_copy, *label_exit_copy;
648 MonoJumpInfo *ji = NULL;
649 GSList *unwind_ops = NULL;
650 int buf_len, i, framesize = 0, off_methodargs, off_targetaddr;
652 buf_len = 512 + 1024;
653 start = code = (guint8 *) mono_global_codeman_reserve (buf_len);
655 /* allocate frame */
656 framesize += 2 * sizeof (host_mgreg_t);
658 off_methodargs = framesize;
659 framesize += sizeof (host_mgreg_t);
661 off_targetaddr = framesize;
662 framesize += sizeof (host_mgreg_t);
664 framesize = ALIGN_TO (framesize, MONO_ARCH_FRAME_ALIGNMENT);
666 MINI_BEGIN_CODEGEN ();
668 arm_subx_imm (code, ARMREG_SP, ARMREG_SP, framesize);
669 arm_stpx (code, ARMREG_FP, ARMREG_LR, ARMREG_SP, 0);
670 arm_movspx (code, ARMREG_FP, ARMREG_SP);
672 /* save CallContext* onto stack */
673 arm_strx (code, ARMREG_R1, ARMREG_FP, off_methodargs);
675 /* save target address onto stack */
676 arm_strx (code, ARMREG_R0, ARMREG_FP, off_targetaddr);
678 /* allocate the stack space necessary for the call */
679 arm_ldrw (code, ARMREG_R0, ARMREG_R1, MONO_STRUCT_OFFSET (CallContext, stack_size));
680 arm_movspx (code, ARMREG_IP0, ARMREG_SP);
681 arm_subx (code, ARMREG_IP0, ARMREG_IP0, ARMREG_R0);
682 arm_movspx (code, ARMREG_SP, ARMREG_IP0);
684 /* copy stack from the CallContext, IP0 = dest, IP1 = source */
685 arm_movspx (code, ARMREG_IP0, ARMREG_SP);
686 arm_ldrp (code, ARMREG_IP1, ARMREG_R1, MONO_STRUCT_OFFSET (CallContext, stack));
688 label_start_copy = code;
690 arm_cmpx_imm (code, ARMREG_R0, 0);
691 label_exit_copy = code;
692 arm_bcc (code, ARMCOND_EQ, 0);
693 arm_ldrx (code, ARMREG_R2, ARMREG_IP1, 0);
694 arm_strx (code, ARMREG_R2, ARMREG_IP0, 0);
695 arm_addx_imm (code, ARMREG_IP0, ARMREG_IP0, sizeof (host_mgreg_t));
696 arm_addx_imm (code, ARMREG_IP1, ARMREG_IP1, sizeof (host_mgreg_t));
697 arm_subx_imm (code, ARMREG_R0, ARMREG_R0, sizeof (host_mgreg_t));
698 arm_b (code, label_start_copy);
699 mono_arm_patch (label_exit_copy, code, MONO_R_ARM64_BCC);
701 /* Load CallContext* into IP0 */
702 arm_ldrx (code, ARMREG_IP0, ARMREG_FP, off_methodargs);
704 /* set all general purpose registers from CallContext */
705 for (i = 0; i < PARAM_REGS + 1; i++)
706 arm_ldrx (code, i, ARMREG_IP0, MONO_STRUCT_OFFSET (CallContext, gregs) + i * sizeof (host_mgreg_t));
708 /* set all floating registers from CallContext */
709 for (i = 0; i < FP_PARAM_REGS; i++)
710 arm_ldrfpx (code, i, ARMREG_IP0, MONO_STRUCT_OFFSET (CallContext, fregs) + i * sizeof (double));
712 /* load target addr */
713 arm_ldrx (code, ARMREG_IP0, ARMREG_FP, off_targetaddr);
715 /* call into native function */
716 code = mono_arm_emit_blrx (code, ARMREG_IP0);
718 /* load CallContext* */
719 arm_ldrx (code, ARMREG_IP0, ARMREG_FP, off_methodargs);
721 /* set all general purpose registers to CallContext */
722 for (i = 0; i < PARAM_REGS; i++)
723 arm_strx (code, i, ARMREG_IP0, MONO_STRUCT_OFFSET (CallContext, gregs) + i * sizeof (host_mgreg_t));
725 /* set all floating registers to CallContext */
726 for (i = 0; i < FP_PARAM_REGS; i++)
727 arm_strfpx (code, i, ARMREG_IP0, MONO_STRUCT_OFFSET (CallContext, fregs) + i * sizeof (double));
729 arm_movspx (code, ARMREG_SP, ARMREG_FP);
730 arm_ldpx (code, ARMREG_FP, ARMREG_LR, ARMREG_SP, 0);
731 arm_addx_imm (code, ARMREG_SP, ARMREG_SP, framesize);
732 arm_retx (code, ARMREG_LR);
734 g_assert (code - start < buf_len);
736 MINI_END_CODEGEN (start, code - start, MONO_PROFILER_CODE_BUFFER_HELPER, NULL);
738 if (info)
739 *info = mono_tramp_info_create ("interp_to_native_trampoline", start, code - start, ji, unwind_ops);
741 return (guint8*)MINI_ADDR_TO_FTNPTR (start);
742 #else
743 g_assert_not_reached ();
744 return NULL;
745 #endif /* DISABLE_INTERPRETER */
748 gpointer
749 mono_arch_get_native_to_interp_trampoline (MonoTrampInfo **info)
751 #ifndef DISABLE_INTERPRETER
752 guint8 *start = NULL, *code;
753 MonoJumpInfo *ji = NULL;
754 GSList *unwind_ops = NULL;
755 int buf_len, i;
756 int framesize, offset, ccontext_offset;
758 buf_len = 512;
759 start = code = (guint8 *) mono_global_codeman_reserve (buf_len);
761 /* Allocate frame (FP + LR + CallContext) */
762 offset = 2 * sizeof (host_mgreg_t);
763 ccontext_offset = offset;
764 offset += sizeof (CallContext);
765 framesize = ALIGN_TO (offset, MONO_ARCH_FRAME_ALIGNMENT);
767 MINI_BEGIN_CODEGEN ();
769 mono_add_unwind_op_def_cfa (unwind_ops, code, start, ARMREG_SP, 0);
771 arm_subx_imm (code, ARMREG_SP, ARMREG_SP, framesize);
772 mono_add_unwind_op_def_cfa_offset (unwind_ops, code, start, framesize);
774 arm_stpx (code, ARMREG_FP, ARMREG_LR, ARMREG_SP, 0);
775 mono_add_unwind_op_offset (unwind_ops, code, start, ARMREG_LR, -framesize + 8);
776 mono_add_unwind_op_offset (unwind_ops, code, start, ARMREG_FP, -framesize);
778 arm_movspx (code, ARMREG_FP, ARMREG_SP);
779 mono_add_unwind_op_def_cfa_reg (unwind_ops, code, start, ARMREG_FP);
781 /* save all general purpose registers into the CallContext */
782 for (i = 0; i < PARAM_REGS + 1; i++)
783 arm_strx (code, i, ARMREG_FP, ccontext_offset + MONO_STRUCT_OFFSET (CallContext, gregs) + i * sizeof (host_mgreg_t));
785 /* save all floating registers into the CallContext */
786 for (i = 0; i < FP_PARAM_REGS; i++)
787 arm_strfpx (code, i, ARMREG_FP, ccontext_offset + MONO_STRUCT_OFFSET (CallContext, fregs) + i * sizeof (double));
789 /* set the stack pointer to the value at call site */
790 arm_addx_imm (code, ARMREG_R0, ARMREG_FP, framesize);
791 arm_strp (code, ARMREG_R0, ARMREG_FP, ccontext_offset + MONO_STRUCT_OFFSET (CallContext, stack));
793 /* call interp_entry with the ccontext and rmethod as arguments */
794 arm_addx_imm (code, ARMREG_R0, ARMREG_FP, ccontext_offset);
795 arm_ldrp (code, ARMREG_R1, MONO_ARCH_RGCTX_REG, MONO_STRUCT_OFFSET (MonoFtnDesc, arg));
796 arm_ldrp (code, ARMREG_IP0, MONO_ARCH_RGCTX_REG, MONO_STRUCT_OFFSET (MonoFtnDesc, addr));
797 code = mono_arm_emit_blrx (code, ARMREG_IP0);
799 /* load the return values from the context */
800 for (i = 0; i < PARAM_REGS; i++)
801 arm_ldrx (code, i, ARMREG_FP, ccontext_offset + MONO_STRUCT_OFFSET (CallContext, gregs) + i * sizeof (host_mgreg_t));
803 for (i = 0; i < FP_PARAM_REGS; i++)
804 arm_ldrfpx (code, i, ARMREG_FP, ccontext_offset + MONO_STRUCT_OFFSET (CallContext, fregs) + i * sizeof (double));
806 /* reset stack and return */
807 arm_ldpx (code, ARMREG_FP, ARMREG_LR, ARMREG_SP, 0);
808 arm_addx_imm (code, ARMREG_SP, ARMREG_SP, framesize);
809 arm_retx (code, ARMREG_LR);
811 g_assert (code - start < buf_len);
813 MINI_END_CODEGEN (start, code - start, MONO_PROFILER_CODE_BUFFER_EXCEPTION_HANDLING, NULL);
815 if (info)
816 *info = mono_tramp_info_create ("native_to_interp_trampoline", start, code - start, ji, unwind_ops);
818 return (guint8*)MINI_ADDR_TO_FTNPTR (start);
819 #else
820 g_assert_not_reached ();
821 return NULL;
822 #endif /* DISABLE_INTERPRETER */
825 #else /* DISABLE_JIT */
827 guchar*
828 mono_arch_create_generic_trampoline (MonoTrampolineType tramp_type, MonoTrampInfo **info, gboolean aot)
830 g_assert_not_reached ();
831 return NULL;
834 gpointer
835 mono_arch_create_specific_trampoline (gpointer arg1, MonoTrampolineType tramp_type, MonoMemoryManager *mem_manager, guint32 *code_len)
837 g_assert_not_reached ();
838 return NULL;
841 gpointer
842 mono_arch_get_unbox_trampoline (MonoMethod *m, gpointer addr)
844 g_assert_not_reached ();
845 return NULL;
848 gpointer
849 mono_arch_get_static_rgctx_trampoline (MonoMemoryManager *mem_manager, gpointer arg, gpointer addr)
851 g_assert_not_reached ();
852 return NULL;
855 gpointer
856 mono_arch_create_rgctx_lazy_fetch_trampoline (guint32 slot, MonoTrampInfo **info, gboolean aot)
858 g_assert_not_reached ();
859 return NULL;
862 guint8*
863 mono_arch_create_sdb_trampoline (gboolean single_step, MonoTrampInfo **info, gboolean aot)
865 g_assert_not_reached ();
866 return NULL;
869 gpointer
870 mono_arch_get_interp_to_native_trampoline (MonoTrampInfo **info)
872 g_assert_not_reached ();
873 return NULL;
876 gpointer
877 mono_arch_get_native_to_interp_trampoline (MonoTrampInfo **info)
879 g_assert_not_reached ();
880 return NULL;
883 #endif /* !DISABLE_JIT */