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.
17 #include <mono/arch/arm64/arm64-codegen.h>
18 #include <mono/metadata/abi-details.h>
20 #define ALIGN_TO(val,align) ((((guint64)val) + ((align) - 1)) & ~((align) - 1))
25 mono_arch_get_restore_context (MonoTrampInfo **info, gboolean aot)
28 MonoJumpInfo *ji = NULL;
29 GSList *unwind_ops = NULL;
33 code = start = mono_global_codeman_reserve (size);
35 arm_movx (code, ARMREG_IP0, ARMREG_R0);
38 for (i = 0; i < 32; ++i)
39 arm_ldrfpx (code, i, ctx_reg, MONO_STRUCT_OFFSET (MonoContext, fregs) + (i * 8));
41 // FIXME: Restore less registers
42 // FIXME: fp should be restored later
43 code = mono_arm_emit_load_regarray (code, 0xffffffff & ~(1 << ctx_reg) & ~(1 << ARMREG_SP), ctx_reg, MONO_STRUCT_OFFSET (MonoContext, regs));
44 /* ip0/ip1 doesn't need to be restored */
46 arm_ldrx (code, ARMREG_IP1, ctx_reg, MONO_STRUCT_OFFSET (MonoContext, pc));
48 arm_ldrx (code, ARMREG_IP0, ctx_reg, MONO_STRUCT_OFFSET (MonoContext, regs) + (ARMREG_SP * 8));
49 /* Restore sp, ctx is no longer valid */
50 arm_movspx (code, ARMREG_SP, ARMREG_IP0);
52 arm_brx (code, ARMREG_IP1);
56 g_assert ((code - start) < size);
57 mono_arch_flush_icache (start, code - start);
58 mono_profiler_code_buffer_new (start, code - start, MONO_PROFILER_CODE_BUFFER_EXCEPTION_HANDLING, NULL);
61 *info = mono_tramp_info_create ("restore_context", start, code - start, ji, unwind_ops);
67 mono_arch_get_call_filter (MonoTrampInfo **info, gboolean aot)
71 int size, offset, gregs_offset, fregs_offset, ctx_offset, num_fregs, frame_size;
72 MonoJumpInfo *ji = NULL;
73 GSList *unwind_ops = NULL;
76 start = code = mono_global_codeman_reserve (size);
78 /* Compute stack frame size and offsets */
83 gregs_offset = offset;
87 fregs_offset = offset;
88 offset += num_fregs * 8;
91 frame_size = ALIGN_TO (offset, MONO_ARCH_FRAME_ALIGNMENT);
94 * We are being called from C code, ctx is in r0, the address to call is in r1.
95 * We need to save state, restore ctx, make the call, then restore the previous state,
96 * returning the value returned by the call.
100 arm_stpx_pre (code, ARMREG_FP, ARMREG_LR, ARMREG_SP, -frame_size);
101 arm_movspx (code, ARMREG_FP, ARMREG_SP);
104 arm_strx (code, ARMREG_R0, ARMREG_FP, ctx_offset);
106 code = mono_arm_emit_store_regarray (code, MONO_ARCH_CALLEE_SAVED_REGS | (1 << ARMREG_FP), ARMREG_FP, gregs_offset);
107 /* No need to save/restore fregs, since we don't currently use them */
109 /* Load regs from ctx */
110 code = mono_arm_emit_load_regarray (code, MONO_ARCH_CALLEE_SAVED_REGS, ARMREG_R0, MONO_STRUCT_OFFSET (MonoContext, regs));
112 arm_ldrx (code, ARMREG_FP, ARMREG_R0, MONO_STRUCT_OFFSET (MonoContext, regs) + (ARMREG_FP * 8));
115 arm_blrx (code, ARMREG_R1);
116 /* For filters, the result is in R0 */
119 arm_ldrx (code, ARMREG_FP, ARMREG_SP, gregs_offset + (ARMREG_FP * 8));
121 arm_ldrx (code, ARMREG_IP0, ARMREG_FP, ctx_offset);
122 /* Save registers back to ctx */
123 /* This isn't strictly neccessary since we don't allocate variables used in eh clauses to registers */
124 code = mono_arm_emit_store_regarray (code, MONO_ARCH_CALLEE_SAVED_REGS, ARMREG_IP0, MONO_STRUCT_OFFSET (MonoContext, regs));
127 code = mono_arm_emit_load_regarray (code, MONO_ARCH_CALLEE_SAVED_REGS, ARMREG_FP, gregs_offset);
129 code = mono_arm_emit_destroy_frame (code, frame_size, (1 << ARMREG_IP0));
130 arm_retx (code, ARMREG_LR);
132 g_assert ((code - start) < size);
133 mono_arch_flush_icache (start, code - start);
134 mono_profiler_code_buffer_new (start, code - start, MONO_PROFILER_CODE_BUFFER_EXCEPTION_HANDLING, NULL);
137 *info = mono_tramp_info_create ("call_filter", start, code - start, ji, unwind_ops);
143 get_throw_trampoline (int size, gboolean corlib, gboolean rethrow, gboolean llvm, gboolean resume_unwind, const char *tramp_name, MonoTrampInfo **info, gboolean aot)
145 guint8 *start, *code;
146 MonoJumpInfo *ji = NULL;
147 GSList *unwind_ops = NULL;
148 int i, offset, gregs_offset, fregs_offset, frame_size, num_fregs;
150 code = start = mono_global_codeman_reserve (size);
152 /* We are being called by JITted code, the exception object/type token is in R0 */
154 /* Compute stack frame size and offsets */
159 gregs_offset = offset;
163 fregs_offset = offset;
164 offset += num_fregs * 8;
165 frame_size = ALIGN_TO (offset, MONO_ARCH_FRAME_ALIGNMENT);
168 arm_stpx_pre (code, ARMREG_FP, ARMREG_LR, ARMREG_SP, -frame_size);
169 arm_movspx (code, ARMREG_FP, ARMREG_SP);
172 code = mono_arm_emit_store_regarray (code, 0xffffffff, ARMREG_FP, gregs_offset);
174 /* The real LR is in R1 */
175 arm_strx (code, ARMREG_R1, ARMREG_FP, gregs_offset + (ARMREG_LR * 8));
177 arm_ldrx (code, ARMREG_IP0, ARMREG_FP, 0);
178 arm_strx (code, ARMREG_IP0, ARMREG_FP, gregs_offset + (ARMREG_FP * 8));
179 arm_addx_imm (code, ARMREG_IP0, ARMREG_FP, frame_size);
180 arm_strx (code, ARMREG_IP0, ARMREG_FP, gregs_offset + (ARMREG_SP * 8));
182 for (i = 0; i < num_fregs; ++i)
183 arm_strfpx (code, ARMREG_D8 + i, ARMREG_FP, fregs_offset + (i * 8));
185 /* Call the C trampoline function */
186 /* Arg1 = exception object/type token */
187 arm_movx (code, ARMREG_R0, ARMREG_R0);
188 /* Arg2 = caller ip */
191 arm_ldrx (code, ARMREG_R1, ARMREG_FP, gregs_offset + (ARMREG_LR * 8));
193 arm_movx (code, ARMREG_R1, ARMREG_R1);
195 arm_ldrx (code, ARMREG_R1, ARMREG_FP, 8);
198 arm_addx_imm (code, ARMREG_R2, ARMREG_FP, gregs_offset);
200 arm_addx_imm (code, ARMREG_R3, ARMREG_FP, fregs_offset);
202 arm_movzx (code, ARMREG_R4, corlib ? 1 : 0, 0);
203 /* Arg 6 = rethrow */
204 arm_movzx (code, ARMREG_R5, rethrow ? 1 : 0, 0);
205 /* Call the function */
207 const char *icall_name;
210 icall_name = "mono_arm_resume_unwind";
212 icall_name = "mono_arm_throw_exception";
214 code = mono_arm_emit_aotconst (&ji, code, start, ARMREG_LR, MONO_PATCH_INFO_JIT_ICALL_ADDR, icall_name);
219 icall_func = mono_arm_resume_unwind;
221 icall_func = mono_arm_throw_exception;
223 code = mono_arm_emit_imm64 (code, ARMREG_LR, (guint64)icall_func);
225 arm_blrx (code, ARMREG_LR);
226 /* This shouldn't return */
229 g_assert ((code - start) < size);
230 mono_arch_flush_icache (start, code - start);
231 mono_profiler_code_buffer_new (start, code - start, MONO_PROFILER_CODE_BUFFER_EXCEPTION_HANDLING, NULL);
234 *info = mono_tramp_info_create (tramp_name, start, code - start, ji, unwind_ops);
240 mono_arch_get_throw_exception (MonoTrampInfo **info, gboolean aot)
242 return get_throw_trampoline (256, FALSE, FALSE, FALSE, FALSE, "throw_exception", info, aot);
246 mono_arch_get_rethrow_exception (MonoTrampInfo **info, gboolean aot)
248 return get_throw_trampoline (256, FALSE, TRUE, FALSE, FALSE, "rethrow_exception", info, aot);
252 mono_arch_get_throw_corlib_exception (MonoTrampInfo **info, gboolean aot)
254 return get_throw_trampoline (256, TRUE, FALSE, FALSE, FALSE, "throw_corlib_exception", info, aot);
258 mono_arm_get_exception_trampolines (gboolean aot)
261 GSList *tramps = NULL;
263 /* LLVM uses the normal trampolines, but with a different name */
264 get_throw_trampoline (256, TRUE, FALSE, FALSE, FALSE, "llvm_throw_corlib_exception_trampoline", &info, aot);
265 tramps = g_slist_prepend (tramps, info);
267 get_throw_trampoline (256, TRUE, FALSE, TRUE, FALSE, "llvm_throw_corlib_exception_abs_trampoline", &info, aot);
268 tramps = g_slist_prepend (tramps, info);
270 get_throw_trampoline (256, FALSE, FALSE, FALSE, TRUE, "llvm_resume_unwind_trampoline", &info, aot);
271 tramps = g_slist_prepend (tramps, info);
276 #else /* DISABLE_JIT */
279 mono_arch_get_restore_context (MonoTrampInfo **info, gboolean aot)
281 g_assert_not_reached ();
286 mono_arch_get_call_filter (MonoTrampInfo **info, gboolean aot)
288 g_assert_not_reached ();
293 mono_arch_get_throw_exception (MonoTrampInfo **info, gboolean aot)
295 g_assert_not_reached ();
300 mono_arch_get_rethrow_exception (MonoTrampInfo **info, gboolean aot)
302 g_assert_not_reached ();
307 mono_arch_get_throw_corlib_exception (MonoTrampInfo **info, gboolean aot)
309 g_assert_not_reached ();
314 mono_arm_get_exception_trampolines (gboolean aot)
316 g_assert_not_reached ();
320 #endif /* !DISABLE_JIT */
323 mono_arch_exceptions_init (void)
329 tramp = mono_aot_get_trampoline ("llvm_throw_corlib_exception_trampoline");
330 mono_register_jit_icall (tramp, "llvm_throw_corlib_exception_trampoline", NULL, TRUE);
331 tramp = mono_aot_get_trampoline ("llvm_throw_corlib_exception_abs_trampoline");
332 mono_register_jit_icall (tramp, "llvm_throw_corlib_exception_abs_trampoline", NULL, TRUE);
333 tramp = mono_aot_get_trampoline ("llvm_resume_unwind_trampoline");
334 mono_register_jit_icall (tramp, "llvm_resume_unwind_trampoline", NULL, TRUE);
336 tramps = mono_arm_get_exception_trampolines (FALSE);
337 for (l = tramps; l; l = l->next) {
338 MonoTrampInfo *info = l->data;
340 mono_register_jit_icall (info->code, g_strdup (info->name), NULL, TRUE);
341 mono_tramp_info_register (info, NULL);
343 g_slist_free (tramps);
348 * mono_arm_throw_exception:
350 * This function is called by the exception trampolines.
351 * FP_REGS points to the 8 callee saved fp regs.
354 mono_arm_throw_exception (gpointer arg, mgreg_t pc, mgreg_t *int_regs, gdouble *fp_regs, gboolean corlib, gboolean rethrow)
357 MonoObject *exc = NULL;
358 guint32 ex_token_index, ex_token;
363 ex_token_index = (guint64)arg;
364 ex_token = MONO_TOKEN_TYPE_DEF | ex_token_index;
365 exc = (MonoObject*)mono_exception_from_token (mono_defaults.corlib, ex_token);
368 /* Adjust pc so it points into the call instruction */
371 /* Initialize a ctx based on the arguments */
372 memset (&ctx, 0, sizeof (MonoContext));
373 memcpy (&(ctx.regs [0]), int_regs, sizeof (mgreg_t) * 32);
374 memcpy (&(ctx.fregs [ARMREG_D8]), fp_regs, sizeof (double) * 8);
377 if (mono_object_isinst (exc, mono_defaults.exception_class)) {
378 MonoException *mono_ex = (MonoException*)exc;
380 mono_ex->stack_trace = NULL;
383 mono_handle_exception (&ctx, exc);
385 mono_restore_context (&ctx);
389 mono_arm_resume_unwind (gpointer arg, mgreg_t pc, mgreg_t *int_regs, gdouble *fp_regs, gboolean corlib, gboolean rethrow)
393 /* Adjust pc so it points into the call instruction */
396 /* Initialize a ctx based on the arguments */
397 memset (&ctx, 0, sizeof (MonoContext));
398 memcpy (&(ctx.regs [0]), int_regs, sizeof (mgreg_t) * 32);
399 memcpy (&(ctx.fregs [ARMREG_D8]), fp_regs, sizeof (double) * 8);
402 mono_resume_unwind (&ctx);
406 * mono_arch_unwind_frame:
408 * See exceptions-amd64.c for docs;
411 mono_arch_unwind_frame (MonoDomain *domain, MonoJitTlsData *jit_tls,
412 MonoJitInfo *ji, MonoContext *ctx,
413 MonoContext *new_ctx, MonoLMF **lmf,
414 mgreg_t **save_locations,
415 StackFrameInfo *frame)
417 gpointer ip = MONO_CONTEXT_GET_IP (ctx);
419 memset (frame, 0, sizeof (StackFrameInfo));
425 mgreg_t regs [MONO_MAX_IREGS + 8 + 1];
427 guint32 unwind_info_len;
430 frame->type = FRAME_TYPE_MANAGED;
432 unwind_info = mono_jinfo_get_unwind_info (ji, &unwind_info_len);
434 memcpy (regs, &new_ctx->regs, sizeof (mgreg_t) * 32);
435 /* v8..v15 are callee saved */
436 memcpy (regs + MONO_MAX_IREGS, &(new_ctx->fregs [8]), sizeof (mgreg_t) * 8);
438 mono_unwind_frame (unwind_info, unwind_info_len, ji->code_start,
439 (guint8*)ji->code_start + ji->code_size,
440 ip, NULL, regs, MONO_MAX_IREGS + 8,
441 save_locations, MONO_MAX_IREGS, &cfa);
443 memcpy (&new_ctx->regs, regs, sizeof (mgreg_t) * 32);
444 memcpy (&(new_ctx->fregs [8]), regs + MONO_MAX_IREGS, sizeof (mgreg_t) * 8);
446 new_ctx->pc = regs [ARMREG_LR];
447 new_ctx->regs [ARMREG_SP] = (mgreg_t)cfa;
449 if (*lmf && (*lmf)->gregs [MONO_ARCH_LMF_REG_SP] && (MONO_CONTEXT_GET_SP (ctx) >= (gpointer)(*lmf)->gregs [MONO_ARCH_LMF_REG_SP])) {
450 /* remove any unused lmf */
451 *lmf = (gpointer)(((gsize)(*lmf)->previous_lmf) & ~3);
454 /* we substract 1, so that the IP points into the call instruction */
459 if (((gsize)(*lmf)->previous_lmf) & 2) {
461 * This LMF entry is created by the soft debug code to mark transitions to
462 * managed code done during invokes.
464 MonoLMFExt *ext = (MonoLMFExt*)(*lmf);
466 g_assert (ext->debugger_invoke);
468 memcpy (new_ctx, &ext->ctx, sizeof (MonoContext));
470 *lmf = (gpointer)(((gsize)(*lmf)->previous_lmf) & ~3);
472 frame->type = FRAME_TYPE_DEBUGGER_INVOKE;
477 frame->type = FRAME_TYPE_MANAGED_TO_NATIVE;
479 ji = mini_jit_info_table_find (domain, (gpointer)(*lmf)->pc, NULL);
483 g_assert (MONO_ARCH_LMF_REGS == ((0x3ff << 19) | (1 << ARMREG_FP) | (1 << ARMREG_SP)));
484 memcpy (&new_ctx->regs [ARMREG_R19], &(*lmf)->gregs [0], sizeof (mgreg_t) * 10);
485 new_ctx->regs [ARMREG_FP] = (*lmf)->gregs [MONO_ARCH_LMF_REG_FP];
486 new_ctx->regs [ARMREG_SP] = (*lmf)->gregs [MONO_ARCH_LMF_REG_SP];
487 new_ctx->pc = (*lmf)->pc;
489 /* we substract 1, so that the IP points into the call instruction */
492 *lmf = (gpointer)(((gsize)(*lmf)->previous_lmf) & ~3);
501 mono_arch_sigctx_to_monoctx (void *sigctx, MonoContext *mctx)
503 mono_sigctx_to_monoctx (sigctx, mctx);
507 mono_arch_monoctx_to_sigctx (MonoContext *mctx, void *sigctx)
509 mono_monoctx_to_sigctx (mctx, sigctx);
515 * Called by resuming from a signal handler.
518 handle_signal_exception (gpointer obj)
520 MonoJitTlsData *jit_tls = mono_native_tls_get_value (mono_jit_tls_id);
523 memcpy (&ctx, &jit_tls->ex_ctx, sizeof (MonoContext));
525 mono_handle_exception (&ctx, obj);
527 mono_restore_context (&ctx);
531 * This is the function called from the signal handler
534 mono_arch_handle_exception (void *ctx, gpointer obj)
536 #if defined(MONO_CROSS_COMPILE)
537 g_assert_not_reached ();
539 MonoJitTlsData *jit_tls;
543 * Resume into the normal stack and handle the exception there.
545 jit_tls = mono_native_tls_get_value (mono_jit_tls_id);
547 /* Pass the ctx parameter in TLS */
548 mono_arch_sigctx_to_monoctx (sigctx, &jit_tls->ex_ctx);
549 /* The others in registers */
550 UCONTEXT_REG_R0 (sigctx) = (gsize)obj;
552 UCONTEXT_REG_PC (sigctx) = (gsize)handle_signal_exception;
553 UCONTEXT_REG_SP (sigctx) = UCONTEXT_REG_SP (sigctx) - MONO_ARCH_REDZONE_SIZE;
560 mono_arch_ip_from_context (void *sigctx)
562 #ifdef MONO_CROSS_COMPILE
563 g_assert_not_reached ();
566 return (gpointer)UCONTEXT_REG_PC (sigctx);
571 mono_arch_setup_async_callback (MonoContext *ctx, void (*async_cb)(void *fun), gpointer user_data)
573 mgreg_t sp = (mgreg_t)MONO_CONTEXT_GET_SP (ctx);
576 g_assert (!user_data);
578 /* Allocate a stack frame */
580 MONO_CONTEXT_SET_SP (ctx, sp);
582 mono_arch_setup_resume_sighandler_ctx (ctx, async_cb);
586 * mono_arch_setup_resume_sighandler_ctx:
588 * Setup CTX so execution continues at FUNC.
591 mono_arch_setup_resume_sighandler_ctx (MonoContext *ctx, gpointer func)
593 MONO_CONTEXT_SET_IP (ctx,func);