2 * exceptions-arm64.c: exception support for ARM64
4 * Copyright 2013 Xamarin Inc
6 * Based on exceptions-arm.c:
9 * Dietmar Maurer (dietmar@ximian.com)
10 * Paolo Molaro (lupus@ximian.com)
12 * (C) 2001 Ximian, Inc.
13 * Licensed under the MIT license. See LICENSE file in the project root for full license information.
18 #include <mono/arch/arm64/arm64-codegen.h>
19 #include <mono/metadata/abi-details.h>
21 #define ALIGN_TO(val,align) ((((guint64)val) + ((align) - 1)) & ~((align) - 1))
26 mono_arch_get_restore_context (MonoTrampInfo **info, gboolean aot)
29 MonoJumpInfo *ji = NULL;
30 GSList *unwind_ops = NULL;
34 code = start = mono_global_codeman_reserve (size);
36 arm_movx (code, ARMREG_IP0, ARMREG_R0);
39 for (i = 0; i < 32; ++i)
40 arm_ldrfpx (code, i, ctx_reg, MONO_STRUCT_OFFSET (MonoContext, fregs) + (i * 8));
42 // FIXME: Restore less registers
43 // FIXME: fp should be restored later
44 code = mono_arm_emit_load_regarray (code, 0xffffffff & ~(1 << ctx_reg) & ~(1 << ARMREG_SP), ctx_reg, MONO_STRUCT_OFFSET (MonoContext, regs));
45 /* ip0/ip1 doesn't need to be restored */
47 arm_ldrx (code, ARMREG_IP1, ctx_reg, MONO_STRUCT_OFFSET (MonoContext, pc));
49 arm_ldrx (code, ARMREG_IP0, ctx_reg, MONO_STRUCT_OFFSET (MonoContext, regs) + (ARMREG_SP * 8));
50 /* Restore sp, ctx is no longer valid */
51 arm_movspx (code, ARMREG_SP, ARMREG_IP0);
53 arm_brx (code, ARMREG_IP1);
57 g_assert ((code - start) < size);
58 mono_arch_flush_icache (start, code - start);
59 mono_profiler_code_buffer_new (start, code - start, MONO_PROFILER_CODE_BUFFER_EXCEPTION_HANDLING, NULL);
62 *info = mono_tramp_info_create ("restore_context", start, code - start, ji, unwind_ops);
68 mono_arch_get_call_filter (MonoTrampInfo **info, gboolean aot)
72 int i, size, offset, gregs_offset, fregs_offset, ctx_offset, num_fregs, frame_size;
73 MonoJumpInfo *ji = NULL;
74 GSList *unwind_ops = NULL;
77 start = code = mono_global_codeman_reserve (size);
79 /* Compute stack frame size and offsets */
84 gregs_offset = offset;
88 fregs_offset = offset;
89 offset += num_fregs * 8;
92 frame_size = ALIGN_TO (offset, MONO_ARCH_FRAME_ALIGNMENT);
95 * We are being called from C code, ctx is in r0, the address to call is in r1.
96 * We need to save state, restore ctx, make the call, then restore the previous state,
97 * returning the value returned by the call.
101 arm_stpx_pre (code, ARMREG_FP, ARMREG_LR, ARMREG_SP, -frame_size);
102 arm_movspx (code, ARMREG_FP, ARMREG_SP);
105 arm_strx (code, ARMREG_R0, ARMREG_FP, ctx_offset);
107 code = mono_arm_emit_store_regarray (code, MONO_ARCH_CALLEE_SAVED_REGS | (1 << ARMREG_FP), ARMREG_FP, gregs_offset);
109 for (i = 0; i < num_fregs; ++i)
110 arm_strfpx (code, ARMREG_D8 + i, ARMREG_FP, fregs_offset + (i * 8));
112 /* No need to save/restore fregs, since we don't currently use them */
114 /* Load regs from ctx */
115 code = mono_arm_emit_load_regarray (code, MONO_ARCH_CALLEE_SAVED_REGS, ARMREG_R0, MONO_STRUCT_OFFSET (MonoContext, regs));
117 for (i = 0; i < num_fregs; ++i)
118 arm_ldrfpx (code, ARMREG_D8 + i, ARMREG_R0, MONO_STRUCT_OFFSET (MonoContext, fregs) + (i * 8));
120 arm_ldrx (code, ARMREG_FP, ARMREG_R0, MONO_STRUCT_OFFSET (MonoContext, regs) + (ARMREG_FP * 8));
123 arm_blrx (code, ARMREG_R1);
124 /* For filters, the result is in R0 */
127 arm_ldrx (code, ARMREG_FP, ARMREG_SP, gregs_offset + (ARMREG_FP * 8));
129 arm_ldrx (code, ARMREG_IP0, ARMREG_FP, ctx_offset);
130 /* Save registers back to ctx */
131 /* This isn't strictly neccessary since we don't allocate variables used in eh clauses to registers */
132 code = mono_arm_emit_store_regarray (code, MONO_ARCH_CALLEE_SAVED_REGS, ARMREG_IP0, MONO_STRUCT_OFFSET (MonoContext, regs));
135 code = mono_arm_emit_load_regarray (code, MONO_ARCH_CALLEE_SAVED_REGS, ARMREG_FP, gregs_offset);
137 for (i = 0; i < num_fregs; ++i)
138 arm_ldrfpx (code, ARMREG_D8 + i, ARMREG_FP, fregs_offset + (i * 8));
140 code = mono_arm_emit_destroy_frame (code, frame_size, (1 << ARMREG_IP0));
141 arm_retx (code, ARMREG_LR);
143 g_assert ((code - start) < size);
144 mono_arch_flush_icache (start, code - start);
145 mono_profiler_code_buffer_new (start, code - start, MONO_PROFILER_CODE_BUFFER_EXCEPTION_HANDLING, NULL);
148 *info = mono_tramp_info_create ("call_filter", start, code - start, ji, unwind_ops);
154 get_throw_trampoline (int size, gboolean corlib, gboolean rethrow, gboolean llvm, gboolean resume_unwind, const char *tramp_name, MonoTrampInfo **info, gboolean aot)
156 guint8 *start, *code;
157 MonoJumpInfo *ji = NULL;
158 GSList *unwind_ops = NULL;
159 int i, offset, gregs_offset, fregs_offset, frame_size, num_fregs;
161 code = start = mono_global_codeman_reserve (size);
163 /* We are being called by JITted code, the exception object/type token is in R0 */
165 /* Compute stack frame size and offsets */
170 gregs_offset = offset;
174 fregs_offset = offset;
175 offset += num_fregs * 8;
176 frame_size = ALIGN_TO (offset, MONO_ARCH_FRAME_ALIGNMENT);
179 arm_stpx_pre (code, ARMREG_FP, ARMREG_LR, ARMREG_SP, -frame_size);
180 arm_movspx (code, ARMREG_FP, ARMREG_SP);
183 code = mono_arm_emit_store_regarray (code, 0xffffffff, ARMREG_FP, gregs_offset);
185 /* The real LR is in R1 */
186 arm_strx (code, ARMREG_R1, ARMREG_FP, gregs_offset + (ARMREG_LR * 8));
188 arm_ldrx (code, ARMREG_IP0, ARMREG_FP, 0);
189 arm_strx (code, ARMREG_IP0, ARMREG_FP, gregs_offset + (ARMREG_FP * 8));
190 arm_addx_imm (code, ARMREG_IP0, ARMREG_FP, frame_size);
191 arm_strx (code, ARMREG_IP0, ARMREG_FP, gregs_offset + (ARMREG_SP * 8));
193 for (i = 0; i < num_fregs; ++i)
194 arm_strfpx (code, ARMREG_D8 + i, ARMREG_FP, fregs_offset + (i * 8));
196 /* Call the C trampoline function */
197 /* Arg1 = exception object/type token */
198 arm_movx (code, ARMREG_R0, ARMREG_R0);
199 /* Arg2 = caller ip */
202 arm_ldrx (code, ARMREG_R1, ARMREG_FP, gregs_offset + (ARMREG_LR * 8));
204 arm_movx (code, ARMREG_R1, ARMREG_R1);
206 arm_ldrx (code, ARMREG_R1, ARMREG_FP, 8);
209 arm_addx_imm (code, ARMREG_R2, ARMREG_FP, gregs_offset);
211 arm_addx_imm (code, ARMREG_R3, ARMREG_FP, fregs_offset);
213 arm_movzx (code, ARMREG_R4, corlib ? 1 : 0, 0);
214 /* Arg 6 = rethrow */
215 arm_movzx (code, ARMREG_R5, rethrow ? 1 : 0, 0);
216 /* Call the function */
218 const char *icall_name;
221 icall_name = "mono_arm_resume_unwind";
223 icall_name = "mono_arm_throw_exception";
225 code = mono_arm_emit_aotconst (&ji, code, start, ARMREG_LR, MONO_PATCH_INFO_JIT_ICALL_ADDR, icall_name);
230 icall_func = mono_arm_resume_unwind;
232 icall_func = mono_arm_throw_exception;
234 code = mono_arm_emit_imm64 (code, ARMREG_LR, (guint64)icall_func);
236 arm_blrx (code, ARMREG_LR);
237 /* This shouldn't return */
240 g_assert ((code - start) < size);
241 mono_arch_flush_icache (start, code - start);
242 mono_profiler_code_buffer_new (start, code - start, MONO_PROFILER_CODE_BUFFER_EXCEPTION_HANDLING, NULL);
245 *info = mono_tramp_info_create (tramp_name, start, code - start, ji, unwind_ops);
251 mono_arch_get_throw_exception (MonoTrampInfo **info, gboolean aot)
253 return get_throw_trampoline (256, FALSE, FALSE, FALSE, FALSE, "throw_exception", info, aot);
257 mono_arch_get_rethrow_exception (MonoTrampInfo **info, gboolean aot)
259 return get_throw_trampoline (256, FALSE, TRUE, FALSE, FALSE, "rethrow_exception", info, aot);
263 mono_arch_get_throw_corlib_exception (MonoTrampInfo **info, gboolean aot)
265 return get_throw_trampoline (256, TRUE, FALSE, FALSE, FALSE, "throw_corlib_exception", info, aot);
269 mono_arm_get_exception_trampolines (gboolean aot)
272 GSList *tramps = NULL;
274 /* LLVM uses the normal trampolines, but with a different name */
275 get_throw_trampoline (256, TRUE, FALSE, FALSE, FALSE, "llvm_throw_corlib_exception_trampoline", &info, aot);
276 tramps = g_slist_prepend (tramps, info);
278 get_throw_trampoline (256, TRUE, FALSE, TRUE, FALSE, "llvm_throw_corlib_exception_abs_trampoline", &info, aot);
279 tramps = g_slist_prepend (tramps, info);
281 get_throw_trampoline (256, FALSE, FALSE, FALSE, TRUE, "llvm_resume_unwind_trampoline", &info, aot);
282 tramps = g_slist_prepend (tramps, info);
287 #else /* DISABLE_JIT */
290 mono_arch_get_restore_context (MonoTrampInfo **info, gboolean aot)
292 g_assert_not_reached ();
297 mono_arch_get_call_filter (MonoTrampInfo **info, gboolean aot)
299 g_assert_not_reached ();
304 mono_arch_get_throw_exception (MonoTrampInfo **info, gboolean aot)
306 g_assert_not_reached ();
311 mono_arch_get_rethrow_exception (MonoTrampInfo **info, gboolean aot)
313 g_assert_not_reached ();
318 mono_arch_get_throw_corlib_exception (MonoTrampInfo **info, gboolean aot)
320 g_assert_not_reached ();
325 mono_arm_get_exception_trampolines (gboolean aot)
327 g_assert_not_reached ();
331 #endif /* !DISABLE_JIT */
334 mono_arch_exceptions_init (void)
340 tramp = mono_aot_get_trampoline ("llvm_throw_corlib_exception_trampoline");
341 mono_register_jit_icall (tramp, "llvm_throw_corlib_exception_trampoline", NULL, TRUE);
342 tramp = mono_aot_get_trampoline ("llvm_throw_corlib_exception_abs_trampoline");
343 mono_register_jit_icall (tramp, "llvm_throw_corlib_exception_abs_trampoline", NULL, TRUE);
344 tramp = mono_aot_get_trampoline ("llvm_resume_unwind_trampoline");
345 mono_register_jit_icall (tramp, "llvm_resume_unwind_trampoline", NULL, TRUE);
347 tramps = mono_arm_get_exception_trampolines (FALSE);
348 for (l = tramps; l; l = l->next) {
349 MonoTrampInfo *info = l->data;
351 mono_register_jit_icall (info->code, g_strdup (info->name), NULL, TRUE);
352 mono_tramp_info_register (info, NULL);
354 g_slist_free (tramps);
359 * mono_arm_throw_exception:
361 * This function is called by the exception trampolines.
362 * FP_REGS points to the 8 callee saved fp regs.
365 mono_arm_throw_exception (gpointer arg, mgreg_t pc, mgreg_t *int_regs, gdouble *fp_regs, gboolean corlib, gboolean rethrow)
369 MonoObject *exc = NULL;
370 guint32 ex_token_index, ex_token;
375 ex_token_index = (guint64)arg;
376 ex_token = MONO_TOKEN_TYPE_DEF | ex_token_index;
377 exc = (MonoObject*)mono_exception_from_token (mono_defaults.corlib, ex_token);
380 /* Adjust pc so it points into the call instruction */
383 /* Initialize a ctx based on the arguments */
384 memset (&ctx, 0, sizeof (MonoContext));
385 memcpy (&(ctx.regs [0]), int_regs, sizeof (mgreg_t) * 32);
386 memcpy (&(ctx.fregs [ARMREG_D8]), fp_regs, sizeof (double) * 8);
389 if (mono_object_isinst_checked (exc, mono_defaults.exception_class, &error)) {
390 MonoException *mono_ex = (MonoException*)exc;
392 mono_ex->stack_trace = NULL;
393 mono_ex->trace_ips = NULL;
396 mono_error_assert_ok (&error);
398 mono_handle_exception (&ctx, exc);
400 mono_restore_context (&ctx);
404 mono_arm_resume_unwind (gpointer arg, mgreg_t pc, mgreg_t *int_regs, gdouble *fp_regs, gboolean corlib, gboolean rethrow)
408 /* Adjust pc so it points into the call instruction */
411 /* Initialize a ctx based on the arguments */
412 memset (&ctx, 0, sizeof (MonoContext));
413 memcpy (&(ctx.regs [0]), int_regs, sizeof (mgreg_t) * 32);
414 memcpy (&(ctx.fregs [ARMREG_D8]), fp_regs, sizeof (double) * 8);
417 mono_resume_unwind (&ctx);
421 * mono_arch_unwind_frame:
423 * See exceptions-amd64.c for docs;
426 mono_arch_unwind_frame (MonoDomain *domain, MonoJitTlsData *jit_tls,
427 MonoJitInfo *ji, MonoContext *ctx,
428 MonoContext *new_ctx, MonoLMF **lmf,
429 mgreg_t **save_locations,
430 StackFrameInfo *frame)
432 gpointer ip = MONO_CONTEXT_GET_IP (ctx);
434 memset (frame, 0, sizeof (StackFrameInfo));
440 mgreg_t regs [MONO_MAX_IREGS + 8 + 1];
442 guint32 unwind_info_len;
445 frame->type = FRAME_TYPE_MANAGED;
447 unwind_info = mono_jinfo_get_unwind_info (ji, &unwind_info_len);
449 memcpy (regs, &new_ctx->regs, sizeof (mgreg_t) * 32);
450 /* v8..v15 are callee saved */
451 memcpy (regs + MONO_MAX_IREGS, &(new_ctx->fregs [8]), sizeof (mgreg_t) * 8);
453 mono_unwind_frame (unwind_info, unwind_info_len, ji->code_start,
454 (guint8*)ji->code_start + ji->code_size,
455 ip, NULL, regs, MONO_MAX_IREGS + 8,
456 save_locations, MONO_MAX_IREGS, &cfa);
458 memcpy (&new_ctx->regs, regs, sizeof (mgreg_t) * 32);
459 memcpy (&(new_ctx->fregs [8]), regs + MONO_MAX_IREGS, sizeof (mgreg_t) * 8);
461 new_ctx->pc = regs [ARMREG_LR];
462 new_ctx->regs [ARMREG_SP] = (mgreg_t)cfa;
464 if (*lmf && (*lmf)->gregs [MONO_ARCH_LMF_REG_SP] && (MONO_CONTEXT_GET_SP (ctx) >= (gpointer)(*lmf)->gregs [MONO_ARCH_LMF_REG_SP])) {
465 /* remove any unused lmf */
466 *lmf = (gpointer)(((gsize)(*lmf)->previous_lmf) & ~3);
469 /* we substract 1, so that the IP points into the call instruction */
474 if (((gsize)(*lmf)->previous_lmf) & 2) {
476 * This LMF entry is created by the soft debug code to mark transitions to
477 * managed code done during invokes.
479 MonoLMFExt *ext = (MonoLMFExt*)(*lmf);
481 g_assert (ext->debugger_invoke);
483 memcpy (new_ctx, &ext->ctx, sizeof (MonoContext));
485 *lmf = (gpointer)(((gsize)(*lmf)->previous_lmf) & ~3);
487 frame->type = FRAME_TYPE_DEBUGGER_INVOKE;
492 frame->type = FRAME_TYPE_MANAGED_TO_NATIVE;
494 ji = mini_jit_info_table_find (domain, (gpointer)(*lmf)->pc, NULL);
498 g_assert (MONO_ARCH_LMF_REGS == ((0x3ff << 19) | (1 << ARMREG_FP) | (1 << ARMREG_SP)));
499 memcpy (&new_ctx->regs [ARMREG_R19], &(*lmf)->gregs [0], sizeof (mgreg_t) * 10);
500 new_ctx->regs [ARMREG_FP] = (*lmf)->gregs [MONO_ARCH_LMF_REG_FP];
501 new_ctx->regs [ARMREG_SP] = (*lmf)->gregs [MONO_ARCH_LMF_REG_SP];
502 new_ctx->pc = (*lmf)->pc;
504 /* we substract 1, so that the IP points into the call instruction */
507 *lmf = (gpointer)(((gsize)(*lmf)->previous_lmf) & ~3);
518 * Called by resuming from a signal handler.
521 handle_signal_exception (gpointer obj)
523 MonoJitTlsData *jit_tls = mono_native_tls_get_value (mono_jit_tls_id);
526 memcpy (&ctx, &jit_tls->ex_ctx, sizeof (MonoContext));
528 mono_handle_exception (&ctx, obj);
530 mono_restore_context (&ctx);
534 * This is the function called from the signal handler
537 mono_arch_handle_exception (void *ctx, gpointer obj)
539 #if defined(MONO_CROSS_COMPILE)
540 g_assert_not_reached ();
542 MonoJitTlsData *jit_tls;
546 * Resume into the normal stack and handle the exception there.
548 jit_tls = mono_native_tls_get_value (mono_jit_tls_id);
550 /* Pass the ctx parameter in TLS */
551 mono_sigctx_to_monoctx (sigctx, &jit_tls->ex_ctx);
552 /* The others in registers */
553 UCONTEXT_REG_R0 (sigctx) = (gsize)obj;
555 UCONTEXT_REG_PC (sigctx) = (gsize)handle_signal_exception;
556 UCONTEXT_REG_SP (sigctx) = UCONTEXT_REG_SP (sigctx) - MONO_ARCH_REDZONE_SIZE;
563 mono_arch_ip_from_context (void *sigctx)
565 #ifdef MONO_CROSS_COMPILE
566 g_assert_not_reached ();
569 return (gpointer)UCONTEXT_REG_PC (sigctx);
574 mono_arch_setup_async_callback (MonoContext *ctx, void (*async_cb)(void *fun), gpointer user_data)
576 mgreg_t sp = (mgreg_t)MONO_CONTEXT_GET_SP (ctx);
579 g_assert (!user_data);
581 /* Allocate a stack frame */
583 MONO_CONTEXT_SET_SP (ctx, sp);
585 mono_arch_setup_resume_sighandler_ctx (ctx, async_cb);
589 * mono_arch_setup_resume_sighandler_ctx:
591 * Setup CTX so execution continues at FUNC.
594 mono_arch_setup_resume_sighandler_ctx (MonoContext *ctx, gpointer func)
596 MONO_CONTEXT_SET_IP (ctx,func);