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;
35 code = start = mono_global_codeman_reserve (size);
37 arm_movx (code, ARMREG_IP0, ARMREG_R0);
41 arm_ldrx (code, ARMREG_IP1, ctx_reg, MONO_STRUCT_OFFSET (MonoContext, has_fregs));
43 arm_cbzx (code, ARMREG_IP1, 0);
44 for (i = 0; i < 32; ++i)
45 arm_ldrfpx (code, i, ctx_reg, MONO_STRUCT_OFFSET (MonoContext, fregs) + (i * 8));
46 mono_arm_patch (labels [0], code, MONO_R_ARM64_CBZ);
48 // FIXME: Restore less registers
49 // FIXME: fp should be restored later
50 code = mono_arm_emit_load_regarray (code, 0xffffffff & ~(1 << ctx_reg) & ~(1 << ARMREG_SP), ctx_reg, MONO_STRUCT_OFFSET (MonoContext, regs));
51 /* ip0/ip1 doesn't need to be restored */
53 arm_ldrx (code, ARMREG_IP1, ctx_reg, MONO_STRUCT_OFFSET (MonoContext, pc));
55 arm_ldrx (code, ARMREG_IP0, ctx_reg, MONO_STRUCT_OFFSET (MonoContext, regs) + (ARMREG_SP * 8));
56 /* Restore sp, ctx is no longer valid */
57 arm_movspx (code, ARMREG_SP, ARMREG_IP0);
59 arm_brx (code, ARMREG_IP1);
63 g_assert ((code - start) < size);
64 mono_arch_flush_icache (start, code - start);
65 mono_profiler_code_buffer_new (start, code - start, MONO_PROFILER_CODE_BUFFER_EXCEPTION_HANDLING, NULL);
68 *info = mono_tramp_info_create ("restore_context", start, code - start, ji, unwind_ops);
74 mono_arch_get_call_filter (MonoTrampInfo **info, gboolean aot)
78 int i, size, offset, gregs_offset, fregs_offset, ctx_offset, num_fregs, frame_size;
79 MonoJumpInfo *ji = NULL;
80 GSList *unwind_ops = NULL;
84 start = code = mono_global_codeman_reserve (size);
86 /* Compute stack frame size and offsets */
91 gregs_offset = offset;
95 fregs_offset = offset;
96 offset += num_fregs * 8;
99 frame_size = ALIGN_TO (offset, MONO_ARCH_FRAME_ALIGNMENT);
102 * We are being called from C code, ctx is in r0, the address to call is in r1.
103 * We need to save state, restore ctx, make the call, then restore the previous state,
104 * returning the value returned by the call.
108 arm_stpx_pre (code, ARMREG_FP, ARMREG_LR, ARMREG_SP, -frame_size);
109 arm_movspx (code, ARMREG_FP, ARMREG_SP);
112 arm_strx (code, ARMREG_R0, ARMREG_FP, ctx_offset);
114 code = mono_arm_emit_store_regarray (code, MONO_ARCH_CALLEE_SAVED_REGS | (1 << ARMREG_FP), ARMREG_FP, gregs_offset);
116 for (i = 0; i < num_fregs; ++i)
117 arm_strfpx (code, ARMREG_D8 + i, ARMREG_FP, fregs_offset + (i * 8));
119 /* Load regs from ctx */
120 code = mono_arm_emit_load_regarray (code, MONO_ARCH_CALLEE_SAVED_REGS, ARMREG_R0, MONO_STRUCT_OFFSET (MonoContext, regs));
122 arm_ldrx (code, ARMREG_IP0, ARMREG_R0, MONO_STRUCT_OFFSET (MonoContext, has_fregs));
124 arm_cbzx (code, ARMREG_IP0, 0);
125 for (i = 0; i < num_fregs; ++i)
126 arm_ldrfpx (code, ARMREG_D8 + i, ARMREG_R0, MONO_STRUCT_OFFSET (MonoContext, fregs) + (i * 8));
127 mono_arm_patch (labels [0], code, MONO_R_ARM64_CBZ);
129 arm_ldrx (code, ARMREG_FP, ARMREG_R0, MONO_STRUCT_OFFSET (MonoContext, regs) + (ARMREG_FP * 8));
132 arm_blrx (code, ARMREG_R1);
133 /* For filters, the result is in R0 */
136 arm_ldrx (code, ARMREG_FP, ARMREG_SP, gregs_offset + (ARMREG_FP * 8));
138 arm_ldrx (code, ARMREG_IP0, ARMREG_FP, ctx_offset);
139 /* Save registers back to ctx */
140 /* This isn't strictly neccessary since we don't allocate variables used in eh clauses to registers */
141 code = mono_arm_emit_store_regarray (code, MONO_ARCH_CALLEE_SAVED_REGS, ARMREG_IP0, MONO_STRUCT_OFFSET (MonoContext, regs));
144 code = mono_arm_emit_load_regarray (code, MONO_ARCH_CALLEE_SAVED_REGS, ARMREG_FP, gregs_offset);
146 for (i = 0; i < num_fregs; ++i)
147 arm_ldrfpx (code, ARMREG_D8 + i, ARMREG_FP, fregs_offset + (i * 8));
149 code = mono_arm_emit_destroy_frame (code, frame_size, (1 << ARMREG_IP0));
150 arm_retx (code, ARMREG_LR);
152 g_assert ((code - start) < size);
153 mono_arch_flush_icache (start, code - start);
154 mono_profiler_code_buffer_new (start, code - start, MONO_PROFILER_CODE_BUFFER_EXCEPTION_HANDLING, NULL);
157 *info = mono_tramp_info_create ("call_filter", start, code - start, ji, unwind_ops);
163 get_throw_trampoline (int size, gboolean corlib, gboolean rethrow, gboolean llvm, gboolean resume_unwind, const char *tramp_name, MonoTrampInfo **info, gboolean aot)
165 guint8 *start, *code;
166 MonoJumpInfo *ji = NULL;
167 GSList *unwind_ops = NULL;
168 int i, offset, gregs_offset, fregs_offset, frame_size, num_fregs;
170 code = start = mono_global_codeman_reserve (size);
172 /* We are being called by JITted code, the exception object/type token is in R0 */
174 /* Compute stack frame size and offsets */
179 gregs_offset = offset;
183 fregs_offset = offset;
184 offset += num_fregs * 8;
185 frame_size = ALIGN_TO (offset, MONO_ARCH_FRAME_ALIGNMENT);
188 arm_stpx_pre (code, ARMREG_FP, ARMREG_LR, ARMREG_SP, -frame_size);
189 arm_movspx (code, ARMREG_FP, ARMREG_SP);
192 code = mono_arm_emit_store_regarray (code, 0xffffffff, ARMREG_FP, gregs_offset);
194 /* The real LR is in R1 */
195 arm_strx (code, ARMREG_R1, ARMREG_FP, gregs_offset + (ARMREG_LR * 8));
197 arm_ldrx (code, ARMREG_IP0, ARMREG_FP, 0);
198 arm_strx (code, ARMREG_IP0, ARMREG_FP, gregs_offset + (ARMREG_FP * 8));
199 arm_addx_imm (code, ARMREG_IP0, ARMREG_FP, frame_size);
200 arm_strx (code, ARMREG_IP0, ARMREG_FP, gregs_offset + (ARMREG_SP * 8));
202 for (i = 0; i < num_fregs; ++i)
203 arm_strfpx (code, ARMREG_D8 + i, ARMREG_FP, fregs_offset + (i * 8));
205 /* Call the C trampoline function */
206 /* Arg1 = exception object/type token */
207 arm_movx (code, ARMREG_R0, ARMREG_R0);
208 /* Arg2 = caller ip */
211 arm_ldrx (code, ARMREG_R1, ARMREG_FP, gregs_offset + (ARMREG_LR * 8));
213 arm_movx (code, ARMREG_R1, ARMREG_R1);
215 arm_ldrx (code, ARMREG_R1, ARMREG_FP, 8);
218 arm_addx_imm (code, ARMREG_R2, ARMREG_FP, gregs_offset);
220 arm_addx_imm (code, ARMREG_R3, ARMREG_FP, fregs_offset);
222 arm_movzx (code, ARMREG_R4, corlib ? 1 : 0, 0);
223 /* Arg 6 = rethrow */
224 arm_movzx (code, ARMREG_R5, rethrow ? 1 : 0, 0);
225 /* Call the function */
227 const char *icall_name;
230 icall_name = "mono_arm_resume_unwind";
232 icall_name = "mono_arm_throw_exception";
234 code = mono_arm_emit_aotconst (&ji, code, start, ARMREG_LR, MONO_PATCH_INFO_JIT_ICALL_ADDR, icall_name);
239 icall_func = mono_arm_resume_unwind;
241 icall_func = mono_arm_throw_exception;
243 code = mono_arm_emit_imm64 (code, ARMREG_LR, (guint64)icall_func);
245 arm_blrx (code, ARMREG_LR);
246 /* This shouldn't return */
249 g_assert ((code - start) < size);
250 mono_arch_flush_icache (start, code - start);
251 mono_profiler_code_buffer_new (start, code - start, MONO_PROFILER_CODE_BUFFER_EXCEPTION_HANDLING, NULL);
254 *info = mono_tramp_info_create (tramp_name, start, code - start, ji, unwind_ops);
260 mono_arch_get_throw_exception (MonoTrampInfo **info, gboolean aot)
262 return get_throw_trampoline (256, FALSE, FALSE, FALSE, FALSE, "throw_exception", info, aot);
266 mono_arch_get_rethrow_exception (MonoTrampInfo **info, gboolean aot)
268 return get_throw_trampoline (256, FALSE, TRUE, FALSE, FALSE, "rethrow_exception", info, aot);
272 mono_arch_get_throw_corlib_exception (MonoTrampInfo **info, gboolean aot)
274 return get_throw_trampoline (256, TRUE, FALSE, FALSE, FALSE, "throw_corlib_exception", info, aot);
278 mono_arm_get_exception_trampolines (gboolean aot)
281 GSList *tramps = NULL;
283 /* LLVM uses the normal trampolines, but with a different name */
284 get_throw_trampoline (256, TRUE, FALSE, FALSE, FALSE, "llvm_throw_corlib_exception_trampoline", &info, aot);
285 tramps = g_slist_prepend (tramps, info);
287 get_throw_trampoline (256, TRUE, FALSE, TRUE, FALSE, "llvm_throw_corlib_exception_abs_trampoline", &info, aot);
288 tramps = g_slist_prepend (tramps, info);
290 get_throw_trampoline (256, FALSE, FALSE, FALSE, TRUE, "llvm_resume_unwind_trampoline", &info, aot);
291 tramps = g_slist_prepend (tramps, info);
296 #else /* DISABLE_JIT */
299 mono_arch_get_restore_context (MonoTrampInfo **info, gboolean aot)
301 g_assert_not_reached ();
306 mono_arch_get_call_filter (MonoTrampInfo **info, gboolean aot)
308 g_assert_not_reached ();
313 mono_arch_get_throw_exception (MonoTrampInfo **info, gboolean aot)
315 g_assert_not_reached ();
320 mono_arch_get_rethrow_exception (MonoTrampInfo **info, gboolean aot)
322 g_assert_not_reached ();
327 mono_arch_get_throw_corlib_exception (MonoTrampInfo **info, gboolean aot)
329 g_assert_not_reached ();
334 mono_arm_get_exception_trampolines (gboolean aot)
336 g_assert_not_reached ();
340 #endif /* !DISABLE_JIT */
343 mono_arch_exceptions_init (void)
349 tramp = mono_aot_get_trampoline ("llvm_throw_corlib_exception_trampoline");
350 mono_register_jit_icall (tramp, "llvm_throw_corlib_exception_trampoline", NULL, TRUE);
351 tramp = mono_aot_get_trampoline ("llvm_throw_corlib_exception_abs_trampoline");
352 mono_register_jit_icall (tramp, "llvm_throw_corlib_exception_abs_trampoline", NULL, TRUE);
353 tramp = mono_aot_get_trampoline ("llvm_resume_unwind_trampoline");
354 mono_register_jit_icall (tramp, "llvm_resume_unwind_trampoline", NULL, TRUE);
356 tramps = mono_arm_get_exception_trampolines (FALSE);
357 for (l = tramps; l; l = l->next) {
358 MonoTrampInfo *info = l->data;
360 mono_register_jit_icall (info->code, g_strdup (info->name), NULL, TRUE);
361 mono_tramp_info_register (info, NULL);
363 g_slist_free (tramps);
368 * mono_arm_throw_exception:
370 * This function is called by the exception trampolines.
371 * FP_REGS points to the 8 callee saved fp regs.
374 mono_arm_throw_exception (gpointer arg, mgreg_t pc, mgreg_t *int_regs, gdouble *fp_regs, gboolean corlib, gboolean rethrow)
378 MonoObject *exc = NULL;
379 guint32 ex_token_index, ex_token;
384 ex_token_index = (guint64)arg;
385 ex_token = MONO_TOKEN_TYPE_DEF | ex_token_index;
386 exc = (MonoObject*)mono_exception_from_token (mono_defaults.corlib, ex_token);
389 /* Adjust pc so it points into the call instruction */
392 /* Initialize a ctx based on the arguments */
393 memset (&ctx, 0, sizeof (MonoContext));
394 memcpy (&(ctx.regs [0]), int_regs, sizeof (mgreg_t) * 32);
395 memcpy (&(ctx.fregs [ARMREG_D8]), fp_regs, sizeof (double) * 8);
399 if (mono_object_isinst_checked (exc, mono_defaults.exception_class, &error)) {
400 MonoException *mono_ex = (MonoException*)exc;
402 mono_ex->stack_trace = NULL;
403 mono_ex->trace_ips = NULL;
406 mono_error_assert_ok (&error);
408 mono_handle_exception (&ctx, exc);
410 mono_restore_context (&ctx);
414 mono_arm_resume_unwind (gpointer arg, mgreg_t pc, mgreg_t *int_regs, gdouble *fp_regs, gboolean corlib, gboolean rethrow)
418 /* Adjust pc so it points into the call instruction */
421 /* Initialize a ctx based on the arguments */
422 memset (&ctx, 0, sizeof (MonoContext));
423 memcpy (&(ctx.regs [0]), int_regs, sizeof (mgreg_t) * 32);
424 memcpy (&(ctx.fregs [ARMREG_D8]), fp_regs, sizeof (double) * 8);
428 mono_resume_unwind (&ctx);
432 * mono_arch_unwind_frame:
434 * See exceptions-amd64.c for docs;
437 mono_arch_unwind_frame (MonoDomain *domain, MonoJitTlsData *jit_tls,
438 MonoJitInfo *ji, MonoContext *ctx,
439 MonoContext *new_ctx, MonoLMF **lmf,
440 mgreg_t **save_locations,
441 StackFrameInfo *frame)
443 gpointer ip = MONO_CONTEXT_GET_IP (ctx);
445 memset (frame, 0, sizeof (StackFrameInfo));
451 mgreg_t regs [MONO_MAX_IREGS + 8 + 1];
453 guint32 unwind_info_len;
456 frame->type = FRAME_TYPE_MANAGED;
458 unwind_info = mono_jinfo_get_unwind_info (ji, &unwind_info_len);
460 memcpy (regs, &new_ctx->regs, sizeof (mgreg_t) * 32);
461 /* v8..v15 are callee saved */
462 memcpy (regs + MONO_MAX_IREGS, &(new_ctx->fregs [8]), sizeof (mgreg_t) * 8);
464 mono_unwind_frame (unwind_info, unwind_info_len, ji->code_start,
465 (guint8*)ji->code_start + ji->code_size,
466 ip, NULL, regs, MONO_MAX_IREGS + 8,
467 save_locations, MONO_MAX_IREGS, &cfa);
469 memcpy (&new_ctx->regs, regs, sizeof (mgreg_t) * 32);
470 memcpy (&(new_ctx->fregs [8]), regs + MONO_MAX_IREGS, sizeof (mgreg_t) * 8);
472 new_ctx->pc = regs [ARMREG_LR];
473 new_ctx->regs [ARMREG_SP] = (mgreg_t)cfa;
475 if (*lmf && (*lmf)->gregs [MONO_ARCH_LMF_REG_SP] && (MONO_CONTEXT_GET_SP (ctx) >= (gpointer)(*lmf)->gregs [MONO_ARCH_LMF_REG_SP])) {
476 /* remove any unused lmf */
477 *lmf = (gpointer)(((gsize)(*lmf)->previous_lmf) & ~3);
480 /* we substract 1, so that the IP points into the call instruction */
485 if (((gsize)(*lmf)->previous_lmf) & 2) {
487 * This LMF entry is created by the soft debug code to mark transitions to
488 * managed code done during invokes.
490 MonoLMFExt *ext = (MonoLMFExt*)(*lmf);
492 g_assert (ext->debugger_invoke);
494 memcpy (new_ctx, &ext->ctx, sizeof (MonoContext));
496 *lmf = (gpointer)(((gsize)(*lmf)->previous_lmf) & ~3);
498 frame->type = FRAME_TYPE_DEBUGGER_INVOKE;
503 frame->type = FRAME_TYPE_MANAGED_TO_NATIVE;
505 ji = mini_jit_info_table_find (domain, (gpointer)(*lmf)->pc, NULL);
509 g_assert (MONO_ARCH_LMF_REGS == ((0x3ff << 19) | (1 << ARMREG_FP) | (1 << ARMREG_SP)));
510 memcpy (&new_ctx->regs [ARMREG_R19], &(*lmf)->gregs [0], sizeof (mgreg_t) * 10);
511 new_ctx->regs [ARMREG_FP] = (*lmf)->gregs [MONO_ARCH_LMF_REG_FP];
512 new_ctx->regs [ARMREG_SP] = (*lmf)->gregs [MONO_ARCH_LMF_REG_SP];
513 new_ctx->pc = (*lmf)->pc;
515 /* we substract 1, so that the IP points into the call instruction */
518 *lmf = (gpointer)(((gsize)(*lmf)->previous_lmf) & ~3);
529 * Called by resuming from a signal handler.
532 handle_signal_exception (gpointer obj)
534 MonoJitTlsData *jit_tls = mono_tls_get_jit_tls ();
537 memcpy (&ctx, &jit_tls->ex_ctx, sizeof (MonoContext));
539 mono_handle_exception (&ctx, obj);
541 mono_restore_context (&ctx);
545 * This is the function called from the signal handler
548 mono_arch_handle_exception (void *ctx, gpointer obj)
550 #if defined(MONO_CROSS_COMPILE)
551 g_assert_not_reached ();
553 MonoJitTlsData *jit_tls;
557 * Resume into the normal stack and handle the exception there.
559 jit_tls = mono_tls_get_jit_tls ();
561 /* Pass the ctx parameter in TLS */
562 mono_sigctx_to_monoctx (sigctx, &jit_tls->ex_ctx);
563 /* The others in registers */
564 UCONTEXT_REG_R0 (sigctx) = (gsize)obj;
566 UCONTEXT_REG_PC (sigctx) = (gsize)handle_signal_exception;
567 UCONTEXT_REG_SP (sigctx) = UCONTEXT_REG_SP (sigctx) - MONO_ARCH_REDZONE_SIZE;
574 mono_arch_ip_from_context (void *sigctx)
576 #ifdef MONO_CROSS_COMPILE
577 g_assert_not_reached ();
580 return (gpointer)UCONTEXT_REG_PC (sigctx);
585 mono_arch_setup_async_callback (MonoContext *ctx, void (*async_cb)(void *fun), gpointer user_data)
587 mgreg_t sp = (mgreg_t)MONO_CONTEXT_GET_SP (ctx);
590 g_assert (!user_data);
592 /* Allocate a stack frame */
594 MONO_CONTEXT_SET_SP (ctx, sp);
596 mono_arch_setup_resume_sighandler_ctx (ctx, async_cb);
600 * mono_arch_setup_resume_sighandler_ctx:
602 * Setup CTX so execution continues at FUNC.
605 mono_arch_setup_resume_sighandler_ctx (MonoContext *ctx, gpointer func)
607 MONO_CONTEXT_SET_IP (ctx,func);