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 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);
108 /* No need to save/restore fregs, since we don't currently use them */
110 /* Load regs from ctx */
111 code = mono_arm_emit_load_regarray (code, MONO_ARCH_CALLEE_SAVED_REGS, ARMREG_R0, MONO_STRUCT_OFFSET (MonoContext, regs));
113 arm_ldrx (code, ARMREG_FP, ARMREG_R0, MONO_STRUCT_OFFSET (MonoContext, regs) + (ARMREG_FP * 8));
116 arm_blrx (code, ARMREG_R1);
117 /* For filters, the result is in R0 */
120 arm_ldrx (code, ARMREG_FP, ARMREG_SP, gregs_offset + (ARMREG_FP * 8));
122 arm_ldrx (code, ARMREG_IP0, ARMREG_FP, ctx_offset);
123 /* Save registers back to ctx */
124 /* This isn't strictly neccessary since we don't allocate variables used in eh clauses to registers */
125 code = mono_arm_emit_store_regarray (code, MONO_ARCH_CALLEE_SAVED_REGS, ARMREG_IP0, MONO_STRUCT_OFFSET (MonoContext, regs));
128 code = mono_arm_emit_load_regarray (code, MONO_ARCH_CALLEE_SAVED_REGS, ARMREG_FP, gregs_offset);
130 code = mono_arm_emit_destroy_frame (code, frame_size, (1 << ARMREG_IP0));
131 arm_retx (code, ARMREG_LR);
133 g_assert ((code - start) < size);
134 mono_arch_flush_icache (start, code - start);
135 mono_profiler_code_buffer_new (start, code - start, MONO_PROFILER_CODE_BUFFER_EXCEPTION_HANDLING, NULL);
138 *info = mono_tramp_info_create ("call_filter", start, code - start, ji, unwind_ops);
144 get_throw_trampoline (int size, gboolean corlib, gboolean rethrow, gboolean llvm, gboolean resume_unwind, const char *tramp_name, MonoTrampInfo **info, gboolean aot)
146 guint8 *start, *code;
147 MonoJumpInfo *ji = NULL;
148 GSList *unwind_ops = NULL;
149 int i, offset, gregs_offset, fregs_offset, frame_size, num_fregs;
151 code = start = mono_global_codeman_reserve (size);
153 /* We are being called by JITted code, the exception object/type token is in R0 */
155 /* Compute stack frame size and offsets */
160 gregs_offset = offset;
164 fregs_offset = offset;
165 offset += num_fregs * 8;
166 frame_size = ALIGN_TO (offset, MONO_ARCH_FRAME_ALIGNMENT);
169 arm_stpx_pre (code, ARMREG_FP, ARMREG_LR, ARMREG_SP, -frame_size);
170 arm_movspx (code, ARMREG_FP, ARMREG_SP);
173 code = mono_arm_emit_store_regarray (code, 0xffffffff, ARMREG_FP, gregs_offset);
175 /* The real LR is in R1 */
176 arm_strx (code, ARMREG_R1, ARMREG_FP, gregs_offset + (ARMREG_LR * 8));
178 arm_ldrx (code, ARMREG_IP0, ARMREG_FP, 0);
179 arm_strx (code, ARMREG_IP0, ARMREG_FP, gregs_offset + (ARMREG_FP * 8));
180 arm_addx_imm (code, ARMREG_IP0, ARMREG_FP, frame_size);
181 arm_strx (code, ARMREG_IP0, ARMREG_FP, gregs_offset + (ARMREG_SP * 8));
183 for (i = 0; i < num_fregs; ++i)
184 arm_strfpx (code, ARMREG_D8 + i, ARMREG_FP, fregs_offset + (i * 8));
186 /* Call the C trampoline function */
187 /* Arg1 = exception object/type token */
188 arm_movx (code, ARMREG_R0, ARMREG_R0);
189 /* Arg2 = caller ip */
192 arm_ldrx (code, ARMREG_R1, ARMREG_FP, gregs_offset + (ARMREG_LR * 8));
194 arm_movx (code, ARMREG_R1, ARMREG_R1);
196 arm_ldrx (code, ARMREG_R1, ARMREG_FP, 8);
199 arm_addx_imm (code, ARMREG_R2, ARMREG_FP, gregs_offset);
201 arm_addx_imm (code, ARMREG_R3, ARMREG_FP, fregs_offset);
203 arm_movzx (code, ARMREG_R4, corlib ? 1 : 0, 0);
204 /* Arg 6 = rethrow */
205 arm_movzx (code, ARMREG_R5, rethrow ? 1 : 0, 0);
206 /* Call the function */
208 const char *icall_name;
211 icall_name = "mono_arm_resume_unwind";
213 icall_name = "mono_arm_throw_exception";
215 code = mono_arm_emit_aotconst (&ji, code, start, ARMREG_LR, MONO_PATCH_INFO_JIT_ICALL_ADDR, icall_name);
220 icall_func = mono_arm_resume_unwind;
222 icall_func = mono_arm_throw_exception;
224 code = mono_arm_emit_imm64 (code, ARMREG_LR, (guint64)icall_func);
226 arm_blrx (code, ARMREG_LR);
227 /* This shouldn't return */
230 g_assert ((code - start) < size);
231 mono_arch_flush_icache (start, code - start);
232 mono_profiler_code_buffer_new (start, code - start, MONO_PROFILER_CODE_BUFFER_EXCEPTION_HANDLING, NULL);
235 *info = mono_tramp_info_create (tramp_name, start, code - start, ji, unwind_ops);
241 mono_arch_get_throw_exception (MonoTrampInfo **info, gboolean aot)
243 return get_throw_trampoline (256, FALSE, FALSE, FALSE, FALSE, "throw_exception", info, aot);
247 mono_arch_get_rethrow_exception (MonoTrampInfo **info, gboolean aot)
249 return get_throw_trampoline (256, FALSE, TRUE, FALSE, FALSE, "rethrow_exception", info, aot);
253 mono_arch_get_throw_corlib_exception (MonoTrampInfo **info, gboolean aot)
255 return get_throw_trampoline (256, TRUE, FALSE, FALSE, FALSE, "throw_corlib_exception", info, aot);
259 mono_arm_get_exception_trampolines (gboolean aot)
262 GSList *tramps = NULL;
264 /* LLVM uses the normal trampolines, but with a different name */
265 get_throw_trampoline (256, TRUE, FALSE, FALSE, FALSE, "llvm_throw_corlib_exception_trampoline", &info, aot);
266 tramps = g_slist_prepend (tramps, info);
268 get_throw_trampoline (256, TRUE, FALSE, TRUE, FALSE, "llvm_throw_corlib_exception_abs_trampoline", &info, aot);
269 tramps = g_slist_prepend (tramps, info);
271 get_throw_trampoline (256, FALSE, FALSE, FALSE, TRUE, "llvm_resume_unwind_trampoline", &info, aot);
272 tramps = g_slist_prepend (tramps, info);
277 #else /* DISABLE_JIT */
280 mono_arch_get_restore_context (MonoTrampInfo **info, gboolean aot)
282 g_assert_not_reached ();
287 mono_arch_get_call_filter (MonoTrampInfo **info, gboolean aot)
289 g_assert_not_reached ();
294 mono_arch_get_throw_exception (MonoTrampInfo **info, gboolean aot)
296 g_assert_not_reached ();
301 mono_arch_get_rethrow_exception (MonoTrampInfo **info, gboolean aot)
303 g_assert_not_reached ();
308 mono_arch_get_throw_corlib_exception (MonoTrampInfo **info, gboolean aot)
310 g_assert_not_reached ();
315 mono_arm_get_exception_trampolines (gboolean aot)
317 g_assert_not_reached ();
321 #endif /* !DISABLE_JIT */
324 mono_arch_exceptions_init (void)
330 tramp = mono_aot_get_trampoline ("llvm_throw_corlib_exception_trampoline");
331 mono_register_jit_icall (tramp, "llvm_throw_corlib_exception_trampoline", NULL, TRUE);
332 tramp = mono_aot_get_trampoline ("llvm_throw_corlib_exception_abs_trampoline");
333 mono_register_jit_icall (tramp, "llvm_throw_corlib_exception_abs_trampoline", NULL, TRUE);
334 tramp = mono_aot_get_trampoline ("llvm_resume_unwind_trampoline");
335 mono_register_jit_icall (tramp, "llvm_resume_unwind_trampoline", NULL, TRUE);
337 tramps = mono_arm_get_exception_trampolines (FALSE);
338 for (l = tramps; l; l = l->next) {
339 MonoTrampInfo *info = l->data;
341 mono_register_jit_icall (info->code, g_strdup (info->name), NULL, TRUE);
342 mono_tramp_info_register (info, NULL);
344 g_slist_free (tramps);
349 * mono_arm_throw_exception:
351 * This function is called by the exception trampolines.
352 * FP_REGS points to the 8 callee saved fp regs.
355 mono_arm_throw_exception (gpointer arg, mgreg_t pc, mgreg_t *int_regs, gdouble *fp_regs, gboolean corlib, gboolean rethrow)
359 MonoObject *exc = NULL;
360 guint32 ex_token_index, ex_token;
365 ex_token_index = (guint64)arg;
366 ex_token = MONO_TOKEN_TYPE_DEF | ex_token_index;
367 exc = (MonoObject*)mono_exception_from_token (mono_defaults.corlib, ex_token);
370 /* Adjust pc so it points into the call instruction */
373 /* Initialize a ctx based on the arguments */
374 memset (&ctx, 0, sizeof (MonoContext));
375 memcpy (&(ctx.regs [0]), int_regs, sizeof (mgreg_t) * 32);
376 memcpy (&(ctx.fregs [ARMREG_D8]), fp_regs, sizeof (double) * 8);
379 if (mono_object_isinst_checked (exc, mono_defaults.exception_class, &error)) {
380 MonoException *mono_ex = (MonoException*)exc;
382 mono_ex->stack_trace = NULL;
383 mono_ex->trace_ips = NULL;
386 mono_error_assert_ok (&error);
388 mono_handle_exception (&ctx, exc);
390 mono_restore_context (&ctx);
394 mono_arm_resume_unwind (gpointer arg, mgreg_t pc, mgreg_t *int_regs, gdouble *fp_regs, gboolean corlib, gboolean rethrow)
398 /* Adjust pc so it points into the call instruction */
401 /* Initialize a ctx based on the arguments */
402 memset (&ctx, 0, sizeof (MonoContext));
403 memcpy (&(ctx.regs [0]), int_regs, sizeof (mgreg_t) * 32);
404 memcpy (&(ctx.fregs [ARMREG_D8]), fp_regs, sizeof (double) * 8);
407 mono_resume_unwind (&ctx);
411 * mono_arch_unwind_frame:
413 * See exceptions-amd64.c for docs;
416 mono_arch_unwind_frame (MonoDomain *domain, MonoJitTlsData *jit_tls,
417 MonoJitInfo *ji, MonoContext *ctx,
418 MonoContext *new_ctx, MonoLMF **lmf,
419 mgreg_t **save_locations,
420 StackFrameInfo *frame)
422 gpointer ip = MONO_CONTEXT_GET_IP (ctx);
424 memset (frame, 0, sizeof (StackFrameInfo));
430 mgreg_t regs [MONO_MAX_IREGS + 8 + 1];
432 guint32 unwind_info_len;
435 frame->type = FRAME_TYPE_MANAGED;
437 unwind_info = mono_jinfo_get_unwind_info (ji, &unwind_info_len);
439 memcpy (regs, &new_ctx->regs, sizeof (mgreg_t) * 32);
440 /* v8..v15 are callee saved */
441 memcpy (regs + MONO_MAX_IREGS, &(new_ctx->fregs [8]), sizeof (mgreg_t) * 8);
443 mono_unwind_frame (unwind_info, unwind_info_len, ji->code_start,
444 (guint8*)ji->code_start + ji->code_size,
445 ip, NULL, regs, MONO_MAX_IREGS + 8,
446 save_locations, MONO_MAX_IREGS, &cfa);
448 memcpy (&new_ctx->regs, regs, sizeof (mgreg_t) * 32);
449 memcpy (&(new_ctx->fregs [8]), regs + MONO_MAX_IREGS, sizeof (mgreg_t) * 8);
451 new_ctx->pc = regs [ARMREG_LR];
452 new_ctx->regs [ARMREG_SP] = (mgreg_t)cfa;
454 if (*lmf && (*lmf)->gregs [MONO_ARCH_LMF_REG_SP] && (MONO_CONTEXT_GET_SP (ctx) >= (gpointer)(*lmf)->gregs [MONO_ARCH_LMF_REG_SP])) {
455 /* remove any unused lmf */
456 *lmf = (gpointer)(((gsize)(*lmf)->previous_lmf) & ~3);
459 /* we substract 1, so that the IP points into the call instruction */
464 if (((gsize)(*lmf)->previous_lmf) & 2) {
466 * This LMF entry is created by the soft debug code to mark transitions to
467 * managed code done during invokes.
469 MonoLMFExt *ext = (MonoLMFExt*)(*lmf);
471 g_assert (ext->debugger_invoke);
473 memcpy (new_ctx, &ext->ctx, sizeof (MonoContext));
475 *lmf = (gpointer)(((gsize)(*lmf)->previous_lmf) & ~3);
477 frame->type = FRAME_TYPE_DEBUGGER_INVOKE;
482 frame->type = FRAME_TYPE_MANAGED_TO_NATIVE;
484 ji = mini_jit_info_table_find (domain, (gpointer)(*lmf)->pc, NULL);
488 g_assert (MONO_ARCH_LMF_REGS == ((0x3ff << 19) | (1 << ARMREG_FP) | (1 << ARMREG_SP)));
489 memcpy (&new_ctx->regs [ARMREG_R19], &(*lmf)->gregs [0], sizeof (mgreg_t) * 10);
490 new_ctx->regs [ARMREG_FP] = (*lmf)->gregs [MONO_ARCH_LMF_REG_FP];
491 new_ctx->regs [ARMREG_SP] = (*lmf)->gregs [MONO_ARCH_LMF_REG_SP];
492 new_ctx->pc = (*lmf)->pc;
494 /* we substract 1, so that the IP points into the call instruction */
497 *lmf = (gpointer)(((gsize)(*lmf)->previous_lmf) & ~3);
508 * Called by resuming from a signal handler.
511 handle_signal_exception (gpointer obj)
513 MonoJitTlsData *jit_tls = mono_native_tls_get_value (mono_jit_tls_id);
516 memcpy (&ctx, &jit_tls->ex_ctx, sizeof (MonoContext));
518 mono_handle_exception (&ctx, obj);
520 mono_restore_context (&ctx);
524 * This is the function called from the signal handler
527 mono_arch_handle_exception (void *ctx, gpointer obj)
529 #if defined(MONO_CROSS_COMPILE)
530 g_assert_not_reached ();
532 MonoJitTlsData *jit_tls;
536 * Resume into the normal stack and handle the exception there.
538 jit_tls = mono_native_tls_get_value (mono_jit_tls_id);
540 /* Pass the ctx parameter in TLS */
541 mono_sigctx_to_monoctx (sigctx, &jit_tls->ex_ctx);
542 /* The others in registers */
543 UCONTEXT_REG_R0 (sigctx) = (gsize)obj;
545 UCONTEXT_REG_PC (sigctx) = (gsize)handle_signal_exception;
546 UCONTEXT_REG_SP (sigctx) = UCONTEXT_REG_SP (sigctx) - MONO_ARCH_REDZONE_SIZE;
553 mono_arch_ip_from_context (void *sigctx)
555 #ifdef MONO_CROSS_COMPILE
556 g_assert_not_reached ();
559 return (gpointer)UCONTEXT_REG_PC (sigctx);
564 mono_arch_setup_async_callback (MonoContext *ctx, void (*async_cb)(void *fun), gpointer user_data)
566 mgreg_t sp = (mgreg_t)MONO_CONTEXT_GET_SP (ctx);
569 g_assert (!user_data);
571 /* Allocate a stack frame */
573 MONO_CONTEXT_SET_SP (ctx, sp);
575 mono_arch_setup_resume_sighandler_ctx (ctx, async_cb);
579 * mono_arch_setup_resume_sighandler_ctx:
581 * Setup CTX so execution continues at FUNC.
584 mono_arch_setup_resume_sighandler_ctx (MonoContext *ctx, gpointer func)
586 MONO_CONTEXT_SET_IP (ctx,func);