3 * exception support for ARM64
5 * Copyright 2013 Xamarin Inc
7 * Based on exceptions-arm.c:
10 * Dietmar Maurer (dietmar@ximian.com)
11 * Paolo Molaro (lupus@ximian.com)
13 * (C) 2001 Ximian, Inc.
14 * Licensed under the MIT license. See LICENSE file in the project root for full license information.
19 #include <mono/arch/arm64/arm64-codegen.h>
20 #include <mono/metadata/abi-details.h>
22 #define ALIGN_TO(val,align) ((((guint64)val) + ((align) - 1)) & ~((align) - 1))
27 mono_arch_get_restore_context (MonoTrampInfo **info, gboolean aot)
30 MonoJumpInfo *ji = NULL;
31 GSList *unwind_ops = NULL;
36 code = start = mono_global_codeman_reserve (size);
38 arm_movx (code, ARMREG_IP0, ARMREG_R0);
42 arm_ldrx (code, ARMREG_IP1, ctx_reg, MONO_STRUCT_OFFSET (MonoContext, has_fregs));
44 arm_cbzx (code, ARMREG_IP1, 0);
45 for (i = 0; i < 32; ++i)
46 arm_ldrfpx (code, i, ctx_reg, MONO_STRUCT_OFFSET (MonoContext, fregs) + (i * 8));
47 mono_arm_patch (labels [0], code, MONO_R_ARM64_CBZ);
49 // FIXME: Restore less registers
50 // FIXME: fp should be restored later
51 code = mono_arm_emit_load_regarray (code, 0xffffffff & ~(1 << ctx_reg) & ~(1 << ARMREG_SP), ctx_reg, MONO_STRUCT_OFFSET (MonoContext, regs));
52 /* ip0/ip1 doesn't need to be restored */
54 arm_ldrx (code, ARMREG_IP1, ctx_reg, MONO_STRUCT_OFFSET (MonoContext, pc));
56 arm_ldrx (code, ARMREG_IP0, ctx_reg, MONO_STRUCT_OFFSET (MonoContext, regs) + (ARMREG_SP * 8));
57 /* Restore sp, ctx is no longer valid */
58 arm_movspx (code, ARMREG_SP, ARMREG_IP0);
60 arm_brx (code, ARMREG_IP1);
64 g_assert ((code - start) < size);
65 mono_arch_flush_icache (start, code - start);
66 MONO_PROFILER_RAISE (jit_code_buffer, (start, code - start, MONO_PROFILER_CODE_BUFFER_EXCEPTION_HANDLING, NULL));
69 *info = mono_tramp_info_create ("restore_context", start, code - start, ji, unwind_ops);
75 mono_arch_get_call_filter (MonoTrampInfo **info, gboolean aot)
79 int i, size, offset, gregs_offset, fregs_offset, ctx_offset, num_fregs, frame_size;
80 MonoJumpInfo *ji = NULL;
81 GSList *unwind_ops = NULL;
85 start = code = mono_global_codeman_reserve (size);
87 /* Compute stack frame size and offsets */
92 gregs_offset = offset;
96 fregs_offset = offset;
97 offset += num_fregs * 8;
100 frame_size = ALIGN_TO (offset, MONO_ARCH_FRAME_ALIGNMENT);
103 * We are being called from C code, ctx is in r0, the address to call is in r1.
104 * We need to save state, restore ctx, make the call, then restore the previous state,
105 * returning the value returned by the call.
109 arm_stpx_pre (code, ARMREG_FP, ARMREG_LR, ARMREG_SP, -frame_size);
110 arm_movspx (code, ARMREG_FP, ARMREG_SP);
113 arm_strx (code, ARMREG_R0, ARMREG_FP, ctx_offset);
115 code = mono_arm_emit_store_regarray (code, MONO_ARCH_CALLEE_SAVED_REGS | (1 << ARMREG_FP), ARMREG_FP, gregs_offset);
117 for (i = 0; i < num_fregs; ++i)
118 arm_strfpx (code, ARMREG_D8 + i, ARMREG_FP, fregs_offset + (i * 8));
120 /* Load regs from ctx */
121 code = mono_arm_emit_load_regarray (code, MONO_ARCH_CALLEE_SAVED_REGS, ARMREG_R0, MONO_STRUCT_OFFSET (MonoContext, regs));
123 arm_ldrx (code, ARMREG_IP0, ARMREG_R0, MONO_STRUCT_OFFSET (MonoContext, has_fregs));
125 arm_cbzx (code, ARMREG_IP0, 0);
126 for (i = 0; i < num_fregs; ++i)
127 arm_ldrfpx (code, ARMREG_D8 + i, ARMREG_R0, MONO_STRUCT_OFFSET (MonoContext, fregs) + ((i + 8) * 8));
128 mono_arm_patch (labels [0], code, MONO_R_ARM64_CBZ);
130 arm_ldrx (code, ARMREG_FP, ARMREG_R0, MONO_STRUCT_OFFSET (MonoContext, regs) + (ARMREG_FP * 8));
133 arm_blrx (code, ARMREG_R1);
134 /* For filters, the result is in R0 */
137 arm_ldrx (code, ARMREG_FP, ARMREG_SP, gregs_offset + (ARMREG_FP * 8));
139 arm_ldrx (code, ARMREG_IP0, ARMREG_FP, ctx_offset);
140 /* Save registers back to ctx */
141 /* This isn't strictly neccessary since we don't allocate variables used in eh clauses to registers */
142 code = mono_arm_emit_store_regarray (code, MONO_ARCH_CALLEE_SAVED_REGS, ARMREG_IP0, MONO_STRUCT_OFFSET (MonoContext, regs));
145 code = mono_arm_emit_load_regarray (code, MONO_ARCH_CALLEE_SAVED_REGS, ARMREG_FP, gregs_offset);
147 for (i = 0; i < num_fregs; ++i)
148 arm_ldrfpx (code, ARMREG_D8 + i, ARMREG_FP, fregs_offset + (i * 8));
150 code = mono_arm_emit_destroy_frame (code, frame_size, (1 << ARMREG_IP0));
151 arm_retx (code, ARMREG_LR);
153 g_assert ((code - start) < size);
154 mono_arch_flush_icache (start, code - start);
155 MONO_PROFILER_RAISE (jit_code_buffer, (start, code - start, MONO_PROFILER_CODE_BUFFER_EXCEPTION_HANDLING, NULL));
158 *info = mono_tramp_info_create ("call_filter", start, code - start, ji, unwind_ops);
164 get_throw_trampoline (int size, gboolean corlib, gboolean rethrow, gboolean llvm, gboolean resume_unwind, const char *tramp_name, MonoTrampInfo **info, gboolean aot)
166 guint8 *start, *code;
167 MonoJumpInfo *ji = NULL;
168 GSList *unwind_ops = NULL;
169 int i, offset, gregs_offset, fregs_offset, frame_size, num_fregs;
171 code = start = mono_global_codeman_reserve (size);
173 /* We are being called by JITted code, the exception object/type token is in R0 */
175 /* Compute stack frame size and offsets */
180 gregs_offset = offset;
184 fregs_offset = offset;
185 offset += num_fregs * 8;
186 frame_size = ALIGN_TO (offset, MONO_ARCH_FRAME_ALIGNMENT);
189 arm_stpx_pre (code, ARMREG_FP, ARMREG_LR, ARMREG_SP, -frame_size);
190 arm_movspx (code, ARMREG_FP, ARMREG_SP);
193 code = mono_arm_emit_store_regarray (code, 0xffffffff, ARMREG_FP, gregs_offset);
195 /* The real LR is in R1 */
196 arm_strx (code, ARMREG_R1, ARMREG_FP, gregs_offset + (ARMREG_LR * 8));
198 arm_ldrx (code, ARMREG_IP0, ARMREG_FP, 0);
199 arm_strx (code, ARMREG_IP0, ARMREG_FP, gregs_offset + (ARMREG_FP * 8));
200 arm_addx_imm (code, ARMREG_IP0, ARMREG_FP, frame_size);
201 arm_strx (code, ARMREG_IP0, ARMREG_FP, gregs_offset + (ARMREG_SP * 8));
203 for (i = 0; i < num_fregs; ++i)
204 arm_strfpx (code, ARMREG_D8 + i, ARMREG_FP, fregs_offset + (i * 8));
206 /* Call the C trampoline function */
207 /* Arg1 = exception object/type token */
208 arm_movx (code, ARMREG_R0, ARMREG_R0);
209 /* Arg2 = caller ip */
212 arm_ldrx (code, ARMREG_R1, ARMREG_FP, gregs_offset + (ARMREG_LR * 8));
214 arm_movx (code, ARMREG_R1, ARMREG_R1);
216 arm_ldrx (code, ARMREG_R1, ARMREG_FP, 8);
219 arm_addx_imm (code, ARMREG_R2, ARMREG_FP, gregs_offset);
221 arm_addx_imm (code, ARMREG_R3, ARMREG_FP, fregs_offset);
223 arm_movzx (code, ARMREG_R4, corlib ? 1 : 0, 0);
224 /* Arg 6 = rethrow */
225 arm_movzx (code, ARMREG_R5, rethrow ? 1 : 0, 0);
226 /* Call the function */
228 const char *icall_name;
231 icall_name = "mono_arm_resume_unwind";
233 icall_name = "mono_arm_throw_exception";
235 code = mono_arm_emit_aotconst (&ji, code, start, ARMREG_LR, MONO_PATCH_INFO_JIT_ICALL_ADDR, icall_name);
240 icall_func = mono_arm_resume_unwind;
242 icall_func = mono_arm_throw_exception;
244 code = mono_arm_emit_imm64 (code, ARMREG_LR, (guint64)icall_func);
246 arm_blrx (code, ARMREG_LR);
247 /* This shouldn't return */
250 g_assert ((code - start) < size);
251 mono_arch_flush_icache (start, code - start);
252 MONO_PROFILER_RAISE (jit_code_buffer, (start, code - start, MONO_PROFILER_CODE_BUFFER_EXCEPTION_HANDLING, NULL));
255 *info = mono_tramp_info_create (tramp_name, start, code - start, ji, unwind_ops);
261 mono_arch_get_throw_exception (MonoTrampInfo **info, gboolean aot)
263 return get_throw_trampoline (256, FALSE, FALSE, FALSE, FALSE, "throw_exception", info, aot);
267 mono_arch_get_rethrow_exception (MonoTrampInfo **info, gboolean aot)
269 return get_throw_trampoline (256, FALSE, TRUE, FALSE, FALSE, "rethrow_exception", info, aot);
273 mono_arch_get_throw_corlib_exception (MonoTrampInfo **info, gboolean aot)
275 return get_throw_trampoline (256, TRUE, FALSE, FALSE, FALSE, "throw_corlib_exception", info, aot);
279 mono_arm_get_exception_trampolines (gboolean aot)
282 GSList *tramps = NULL;
284 /* LLVM uses the normal trampolines, but with a different name */
285 get_throw_trampoline (256, TRUE, FALSE, FALSE, FALSE, "llvm_throw_corlib_exception_trampoline", &info, aot);
286 tramps = g_slist_prepend (tramps, info);
288 get_throw_trampoline (256, TRUE, FALSE, TRUE, FALSE, "llvm_throw_corlib_exception_abs_trampoline", &info, aot);
289 tramps = g_slist_prepend (tramps, info);
291 get_throw_trampoline (256, FALSE, FALSE, FALSE, TRUE, "llvm_resume_unwind_trampoline", &info, aot);
292 tramps = g_slist_prepend (tramps, info);
297 #else /* DISABLE_JIT */
300 mono_arch_get_restore_context (MonoTrampInfo **info, gboolean aot)
302 g_assert_not_reached ();
307 mono_arch_get_call_filter (MonoTrampInfo **info, gboolean aot)
309 g_assert_not_reached ();
314 mono_arch_get_throw_exception (MonoTrampInfo **info, gboolean aot)
316 g_assert_not_reached ();
321 mono_arch_get_rethrow_exception (MonoTrampInfo **info, gboolean aot)
323 g_assert_not_reached ();
328 mono_arch_get_throw_corlib_exception (MonoTrampInfo **info, gboolean aot)
330 g_assert_not_reached ();
335 mono_arm_get_exception_trampolines (gboolean aot)
337 g_assert_not_reached ();
341 #endif /* !DISABLE_JIT */
344 mono_arch_exceptions_init (void)
350 tramp = mono_aot_get_trampoline ("llvm_throw_corlib_exception_trampoline");
351 mono_register_jit_icall (tramp, "llvm_throw_corlib_exception_trampoline", NULL, TRUE);
352 tramp = mono_aot_get_trampoline ("llvm_throw_corlib_exception_abs_trampoline");
353 mono_register_jit_icall (tramp, "llvm_throw_corlib_exception_abs_trampoline", NULL, TRUE);
354 tramp = mono_aot_get_trampoline ("llvm_resume_unwind_trampoline");
355 mono_register_jit_icall (tramp, "llvm_resume_unwind_trampoline", NULL, TRUE);
357 tramps = mono_arm_get_exception_trampolines (FALSE);
358 for (l = tramps; l; l = l->next) {
359 MonoTrampInfo *info = l->data;
361 mono_register_jit_icall (info->code, g_strdup (info->name), NULL, TRUE);
362 mono_tramp_info_register (info, NULL);
364 g_slist_free (tramps);
369 * mono_arm_throw_exception:
371 * This function is called by the exception trampolines.
372 * FP_REGS points to the 8 callee saved fp regs.
375 mono_arm_throw_exception (gpointer arg, mgreg_t pc, mgreg_t *int_regs, gdouble *fp_regs, gboolean corlib, gboolean rethrow)
379 MonoObject *exc = NULL;
380 guint32 ex_token_index, ex_token;
385 ex_token_index = (guint64)arg;
386 ex_token = MONO_TOKEN_TYPE_DEF | ex_token_index;
387 exc = (MonoObject*)mono_exception_from_token (mono_defaults.corlib, ex_token);
390 /* Adjust pc so it points into the call instruction */
393 /* Initialize a ctx based on the arguments */
394 memset (&ctx, 0, sizeof (MonoContext));
395 memcpy (&(ctx.regs [0]), int_regs, sizeof (mgreg_t) * 32);
396 memcpy (&(ctx.fregs [ARMREG_D8]), fp_regs, sizeof (double) * 8);
400 if (mono_object_isinst_checked (exc, mono_defaults.exception_class, &error)) {
401 MonoException *mono_ex = (MonoException*)exc;
403 mono_ex->stack_trace = NULL;
404 mono_ex->trace_ips = NULL;
407 mono_error_assert_ok (&error);
409 mono_handle_exception (&ctx, exc);
411 mono_restore_context (&ctx);
415 mono_arm_resume_unwind (gpointer arg, mgreg_t pc, mgreg_t *int_regs, gdouble *fp_regs, gboolean corlib, gboolean rethrow)
419 /* Adjust pc so it points into the call instruction */
422 /* Initialize a ctx based on the arguments */
423 memset (&ctx, 0, sizeof (MonoContext));
424 memcpy (&(ctx.regs [0]), int_regs, sizeof (mgreg_t) * 32);
425 memcpy (&(ctx.fregs [ARMREG_D8]), fp_regs, sizeof (double) * 8);
429 mono_resume_unwind (&ctx);
433 * mono_arch_unwind_frame:
435 * See exceptions-amd64.c for docs;
438 mono_arch_unwind_frame (MonoDomain *domain, MonoJitTlsData *jit_tls,
439 MonoJitInfo *ji, MonoContext *ctx,
440 MonoContext *new_ctx, MonoLMF **lmf,
441 mgreg_t **save_locations,
442 StackFrameInfo *frame)
444 gpointer ip = MONO_CONTEXT_GET_IP (ctx);
446 memset (frame, 0, sizeof (StackFrameInfo));
452 mgreg_t regs [MONO_MAX_IREGS + 8 + 1];
454 guint32 unwind_info_len;
457 frame->type = FRAME_TYPE_MANAGED;
459 unwind_info = mono_jinfo_get_unwind_info (ji, &unwind_info_len);
461 memcpy (regs, &new_ctx->regs, sizeof (mgreg_t) * 32);
462 /* v8..v15 are callee saved */
463 memcpy (regs + MONO_MAX_IREGS, &(new_ctx->fregs [8]), sizeof (mgreg_t) * 8);
465 mono_unwind_frame (unwind_info, unwind_info_len, ji->code_start,
466 (guint8*)ji->code_start + ji->code_size,
467 ip, NULL, regs, MONO_MAX_IREGS + 8,
468 save_locations, MONO_MAX_IREGS, &cfa);
470 memcpy (&new_ctx->regs, regs, sizeof (mgreg_t) * 32);
471 memcpy (&(new_ctx->fregs [8]), regs + MONO_MAX_IREGS, sizeof (mgreg_t) * 8);
473 new_ctx->pc = regs [ARMREG_LR];
474 new_ctx->regs [ARMREG_SP] = (mgreg_t)cfa;
476 if (*lmf && (*lmf)->gregs [MONO_ARCH_LMF_REG_SP] && (MONO_CONTEXT_GET_SP (ctx) >= (gpointer)(*lmf)->gregs [MONO_ARCH_LMF_REG_SP])) {
477 /* remove any unused lmf */
478 *lmf = (gpointer)(((gsize)(*lmf)->previous_lmf) & ~3);
481 /* we substract 1, so that the IP points into the call instruction */
486 if (((gsize)(*lmf)->previous_lmf) & 2) {
487 MonoLMFExt *ext = (MonoLMFExt*)(*lmf);
489 if (ext->debugger_invoke) {
491 * This LMF entry is created by the soft debug code to mark transitions to
492 * managed code done during invokes.
494 frame->type = FRAME_TYPE_DEBUGGER_INVOKE;
495 memcpy (new_ctx, &ext->ctx, sizeof (MonoContext));
496 } else if (ext->interp_exit) {
497 frame->type = FRAME_TYPE_INTERP_TO_MANAGED;
498 frame->interp_exit_data = ext->interp_exit_data;
500 g_assert_not_reached ();
503 *lmf = (gpointer)(((gsize)(*lmf)->previous_lmf) & ~3);
508 frame->type = FRAME_TYPE_MANAGED_TO_NATIVE;
510 ji = mini_jit_info_table_find (domain, (gpointer)(*lmf)->pc, NULL);
514 g_assert (MONO_ARCH_LMF_REGS == ((0x3ff << 19) | (1 << ARMREG_FP) | (1 << ARMREG_SP)));
515 memcpy (&new_ctx->regs [ARMREG_R19], &(*lmf)->gregs [0], sizeof (mgreg_t) * 10);
516 new_ctx->regs [ARMREG_FP] = (*lmf)->gregs [MONO_ARCH_LMF_REG_FP];
517 new_ctx->regs [ARMREG_SP] = (*lmf)->gregs [MONO_ARCH_LMF_REG_SP];
518 new_ctx->pc = (*lmf)->pc;
520 /* we substract 1, so that the IP points into the call instruction */
523 *lmf = (gpointer)(((gsize)(*lmf)->previous_lmf) & ~3);
534 * Called by resuming from a signal handler.
537 handle_signal_exception (gpointer obj)
539 MonoJitTlsData *jit_tls = mono_tls_get_jit_tls ();
542 memcpy (&ctx, &jit_tls->ex_ctx, sizeof (MonoContext));
544 mono_handle_exception (&ctx, obj);
546 mono_restore_context (&ctx);
550 * This is the function called from the signal handler
553 mono_arch_handle_exception (void *ctx, gpointer obj)
555 #if defined(MONO_CROSS_COMPILE)
556 g_assert_not_reached ();
558 MonoJitTlsData *jit_tls;
562 * Resume into the normal stack and handle the exception there.
564 jit_tls = mono_tls_get_jit_tls ();
566 /* Pass the ctx parameter in TLS */
567 mono_sigctx_to_monoctx (sigctx, &jit_tls->ex_ctx);
568 /* The others in registers */
569 UCONTEXT_REG_R0 (sigctx) = (gsize)obj;
571 UCONTEXT_REG_PC (sigctx) = (gsize)handle_signal_exception;
572 UCONTEXT_REG_SP (sigctx) = UCONTEXT_REG_SP (sigctx) - MONO_ARCH_REDZONE_SIZE;
579 mono_arch_ip_from_context (void *sigctx)
581 #ifdef MONO_CROSS_COMPILE
582 g_assert_not_reached ();
585 return (gpointer)UCONTEXT_REG_PC (sigctx);
590 mono_arch_setup_async_callback (MonoContext *ctx, void (*async_cb)(void *fun), gpointer user_data)
592 mgreg_t sp = (mgreg_t)MONO_CONTEXT_GET_SP (ctx);
595 g_assert (!user_data);
597 /* Allocate a stack frame */
599 MONO_CONTEXT_SET_SP (ctx, sp);
601 mono_arch_setup_resume_sighandler_ctx (ctx, async_cb);
605 * mono_arch_setup_resume_sighandler_ctx:
607 * Setup CTX so execution continues at FUNC.
610 mono_arch_setup_resume_sighandler_ctx (MonoContext *ctx, gpointer func)
612 MONO_CONTEXT_SET_IP (ctx,func);