3 * exception support for ARM
6 * Dietmar Maurer (dietmar@ximian.com)
7 * Paolo Molaro (lupus@ximian.com)
9 * (C) 2001 Ximian, Inc.
17 #ifndef MONO_CROSS_COMPILE
19 #include <asm/sigcontext.h>
20 #endif /* def HOST_ANDROID */
23 #ifdef HAVE_UCONTEXT_H
25 #endif /* def HAVE_UCONTEXT_H */
27 #include <mono/arch/arm/arm-codegen.h>
28 #include <mono/arch/arm/arm-vfp-codegen.h>
29 #include <mono/metadata/abi-details.h>
30 #include <mono/metadata/appdomain.h>
31 #include <mono/metadata/tabledefs.h>
32 #include <mono/metadata/threads.h>
33 #include <mono/metadata/debug-helpers.h>
34 #include <mono/metadata/exception.h>
35 #include <mono/metadata/mono-debug.h>
39 #include "mono/utils/mono-sigcontext.h"
40 #include "mono/utils/mono-compiler.h"
43 * arch_get_restore_context:
45 * Returns a pointer to a method which restores a previously saved sigcontext.
46 * The first argument in r0 is the pointer to the context.
49 mono_arch_get_restore_context (MonoTrampInfo **info, gboolean aot)
54 MonoJumpInfo *ji = NULL;
55 GSList *unwind_ops = NULL;
57 start = code = mono_global_codeman_reserve (128);
60 * Move things to their proper place so we can restore all the registers with
66 if (!mono_arch_is_soft_float ()) {
67 ARM_ADD_REG_IMM8 (code, ARMREG_IP, ctx_reg, MONO_STRUCT_OFFSET (MonoContext, fregs));
68 ARM_FLDMD (code, ARM_VFP_D0, 16, ARMREG_IP);
72 ARM_LDR_IMM (code, ARMREG_IP, ctx_reg, MONO_STRUCT_OFFSET (MonoContext, pc));
73 ARM_STR_IMM (code, ARMREG_IP, ctx_reg, MONO_STRUCT_OFFSET (MonoContext, regs) + (ARMREG_PC * sizeof (mgreg_t)));
75 /* restore everything */
76 ARM_ADD_REG_IMM8 (code, ARMREG_IP, ctx_reg, MONO_STRUCT_OFFSET(MonoContext, regs));
77 ARM_LDM (code, ARMREG_IP, 0xffff);
82 g_assert ((code - start) < 128);
84 mono_arch_flush_icache (start, code - start);
87 *info = mono_tramp_info_create ("restore_context", start, code - start, ji, unwind_ops);
93 * arch_get_call_filter:
95 * Returns a pointer to a method which calls an exception filter. We
96 * also use this function to call finally handlers (we pass NULL as
97 * @exc object in this case).
100 mono_arch_get_call_filter (MonoTrampInfo **info, gboolean aot)
105 MonoJumpInfo *ji = NULL;
106 GSList *unwind_ops = NULL;
108 /* call_filter (MonoContext *ctx, unsigned long eip, gpointer exc) */
109 start = code = mono_global_codeman_reserve (320);
111 /* save all the regs on the stack */
112 ARM_MOV_REG_REG (code, ARMREG_IP, ARMREG_SP);
113 ARM_PUSH (code, MONO_ARM_REGSAVE_MASK);
115 ARM_SUB_REG_IMM8 (code, ARMREG_SP, ARMREG_SP, 8);
117 /* restore all the regs from ctx (in r0), but not sp, the stack pointer */
119 ARM_LDR_IMM (code, ARMREG_IP, ctx_reg, MONO_STRUCT_OFFSET (MonoContext, pc));
120 ARM_ADD_REG_IMM8 (code, ARMREG_LR, ctx_reg, MONO_STRUCT_OFFSET(MonoContext, regs) + (MONO_ARM_FIRST_SAVED_REG * sizeof (mgreg_t)));
121 ARM_LDM (code, ARMREG_LR, MONO_ARM_REGSAVE_MASK);
122 /* call handler at eip (r1) and set the first arg with the exception (r2) */
123 ARM_MOV_REG_REG (code, ARMREG_R0, ARMREG_R2);
124 ARM_MOV_REG_REG (code, ARMREG_LR, ARMREG_PC);
125 ARM_MOV_REG_REG (code, ARMREG_PC, ARMREG_R1);
127 ARM_ADD_REG_IMM8 (code, ARMREG_SP, ARMREG_SP, 8);
130 ARM_POP_NWB (code, 0xff0 | ((1 << ARMREG_SP) | (1 << ARMREG_PC)));
132 g_assert ((code - start) < 320);
134 mono_arch_flush_icache (start, code - start);
137 *info = mono_tramp_info_create ("call_filter", start, code - start, ji, unwind_ops);
143 mono_arm_throw_exception (MonoObject *exc, mgreg_t pc, mgreg_t sp, mgreg_t *int_regs, gdouble *fp_regs)
147 gboolean rethrow = sp & 1;
149 sp &= ~1; /* clear the optional rethrow bit */
150 pc &= ~1; /* clear the thumb bit */
151 /* adjust eip so that it point into the call instruction */
154 /*printf ("stack in throw: %p\n", esp);*/
155 MONO_CONTEXT_SET_BP (&ctx, int_regs [ARMREG_FP - 4]);
156 MONO_CONTEXT_SET_SP (&ctx, sp);
157 MONO_CONTEXT_SET_IP (&ctx, pc);
158 memcpy (((guint8*)&ctx.regs) + (ARMREG_R4 * sizeof (mgreg_t)), int_regs, 8 * sizeof (mgreg_t));
159 memcpy (&ctx.fregs, fp_regs, sizeof (double) * 16);
161 if (mono_object_isinst_checked (exc, mono_defaults.exception_class, &error)) {
162 MonoException *mono_ex = (MonoException*)exc;
164 mono_ex->stack_trace = NULL;
165 mono_ex->trace_ips = NULL;
168 mono_error_assert_ok (&error);
169 mono_handle_exception (&ctx, exc);
170 mono_restore_context (&ctx);
171 g_assert_not_reached ();
175 mono_arm_throw_exception_by_token (guint32 ex_token_index, mgreg_t pc, mgreg_t sp, mgreg_t *int_regs, gdouble *fp_regs)
177 guint32 ex_token = MONO_TOKEN_TYPE_DEF | ex_token_index;
178 /* Clear thumb bit */
181 mono_arm_throw_exception ((MonoObject*)mono_exception_from_token (mono_defaults.corlib, ex_token), pc, sp, int_regs, fp_regs);
185 mono_arm_resume_unwind (guint32 dummy1, mgreg_t pc, mgreg_t sp, mgreg_t *int_regs, gdouble *fp_regs)
189 pc &= ~1; /* clear the optional rethrow bit */
190 /* adjust eip so that it point into the call instruction */
193 MONO_CONTEXT_SET_BP (&ctx, int_regs [ARMREG_FP - 4]);
194 MONO_CONTEXT_SET_SP (&ctx, sp);
195 MONO_CONTEXT_SET_IP (&ctx, pc);
196 memcpy (((guint8*)&ctx.regs) + (ARMREG_R4 * sizeof (mgreg_t)), int_regs, 8 * sizeof (mgreg_t));
198 mono_resume_unwind (&ctx);
202 * get_throw_trampoline:
204 * Returns a function pointer which can be used to raise
205 * exceptions. The returned function has the following
206 * signature: void (*func) (MonoException *exc); or
207 * void (*func) (guint32 ex_token, guint8* ip);
211 get_throw_trampoline (int size, gboolean corlib, gboolean rethrow, gboolean llvm, gboolean resume_unwind, const char *tramp_name, MonoTrampInfo **info, gboolean aot)
215 MonoJumpInfo *ji = NULL;
216 GSList *unwind_ops = NULL;
219 code = start = mono_global_codeman_reserve (size);
221 mono_add_unwind_op_def_cfa (unwind_ops, code, start, ARMREG_SP, 0);
223 /* save all the regs on the stack */
224 ARM_MOV_REG_REG (code, ARMREG_IP, ARMREG_SP);
225 ARM_PUSH (code, MONO_ARM_REGSAVE_MASK);
227 cfa_offset = MONO_ARM_NUM_SAVED_REGS * sizeof (mgreg_t);
228 mono_add_unwind_op_def_cfa (unwind_ops, code, start, ARMREG_SP, cfa_offset);
229 mono_add_unwind_op_offset (unwind_ops, code, start, ARMREG_LR, - sizeof (mgreg_t));
232 if (!mono_arch_is_soft_float ()) {
233 ARM_SUB_REG_IMM8 (code, ARMREG_SP, ARMREG_SP, sizeof (double) * 16);
234 cfa_offset += sizeof (double) * 16;
235 mono_add_unwind_op_def_cfa_offset (unwind_ops, code, start, cfa_offset);
236 ARM_FSTMD (code, ARM_VFP_D0, 16, ARMREG_SP);
240 ARM_SUB_REG_IMM8 (code, ARMREG_SP, ARMREG_SP, 8);
242 mono_add_unwind_op_def_cfa_offset (unwind_ops, code, start, cfa_offset);
244 /* call throw_exception (exc, ip, sp, int_regs, fp_regs) */
246 ARM_ADD_REG_IMM8 (code, ARMREG_R2, ARMREG_SP, cfa_offset);
247 /* we encode rethrow in sp */
249 g_assert (!resume_unwind);
251 ARM_ORR_REG_IMM8 (code, ARMREG_R2, ARMREG_R2, rethrow);
253 /* exc is already in place in r0 */
255 /* The caller ip is already in R1 */
258 * The address passed by llvm might point to before the call,
259 * thus outside the eh range recorded by llvm. Use the return
261 * FIXME: Do this on more platforms.
263 ARM_MOV_REG_REG (code, ARMREG_R1, ARMREG_LR); /* caller ip */
266 ARM_MOV_REG_REG (code, ARMREG_R1, ARMREG_LR); /* caller ip */
269 ARM_ADD_REG_IMM8 (code, ARMREG_R3, ARMREG_SP, (cfa_offset - (MONO_ARM_NUM_SAVED_REGS * sizeof (mgreg_t))));
271 ARM_ADD_REG_IMM8 (code, ARMREG_LR, ARMREG_SP, 8);
272 ARM_STR_IMM (code, ARMREG_LR, ARMREG_SP, 0);
275 const char *icall_name;
278 icall_name = "mono_arm_resume_unwind";
280 icall_name = "mono_arm_throw_exception_by_token";
282 icall_name = "mono_arm_throw_exception";
284 ji = mono_patch_info_list_prepend (ji, code - start, MONO_PATCH_INFO_JIT_ICALL_ADDR, icall_name);
285 ARM_LDR_IMM (code, ARMREG_IP, ARMREG_PC, 0);
287 *(gpointer*)(gpointer)code = NULL;
289 ARM_LDR_REG_REG (code, ARMREG_IP, ARMREG_PC, ARMREG_IP);
291 code = mono_arm_emit_load_imm (code, ARMREG_IP, GPOINTER_TO_UINT (resume_unwind ? (gpointer)mono_arm_resume_unwind : (corlib ? (gpointer)mono_arm_throw_exception_by_token : (gpointer)mono_arm_throw_exception)));
293 ARM_MOV_REG_REG (code, ARMREG_LR, ARMREG_PC);
294 ARM_MOV_REG_REG (code, ARMREG_PC, ARMREG_IP);
295 /* we should never reach this breakpoint */
297 g_assert ((code - start) < size);
298 mono_arch_flush_icache (start, code - start);
301 *info = mono_tramp_info_create (tramp_name, start, code - start, ji, unwind_ops);
307 * arch_get_throw_exception:
309 * Returns a function pointer which can be used to raise
310 * exceptions. The returned function has the following
311 * signature: void (*func) (MonoException *exc);
312 * For example to raise an arithmetic exception you can use:
314 * x86_push_imm (code, mono_get_exception_arithmetic ());
315 * x86_call_code (code, arch_get_throw_exception ());
319 mono_arch_get_throw_exception (MonoTrampInfo **info, gboolean aot)
321 return get_throw_trampoline (132, FALSE, FALSE, FALSE, FALSE, "throw_exception", info, aot);
325 * mono_arch_get_rethrow_exception:
327 * Returns a function pointer which can be used to rethrow
328 * exceptions. The returned function has the following
329 * signature: void (*func) (MonoException *exc);
333 mono_arch_get_rethrow_exception (MonoTrampInfo **info, gboolean aot)
335 return get_throw_trampoline (132, FALSE, TRUE, FALSE, FALSE, "rethrow_exception", info, aot);
339 * mono_arch_get_throw_corlib_exception:
340 * \returns a function pointer which can be used to raise
341 * corlib exceptions. The returned function has the following
342 * signature: void (*func) (guint32 ex_token, guint32 offset);
343 * Here, \c offset is the offset which needs to be substracted from the caller IP
344 * to get the IP of the throw. Passing the offset has the advantage that it
345 * needs no relocations in the caller.
346 * On ARM, the ip is passed instead of an offset.
349 mono_arch_get_throw_corlib_exception (MonoTrampInfo **info, gboolean aot)
351 return get_throw_trampoline (168, TRUE, FALSE, FALSE, FALSE, "throw_corlib_exception", info, aot);
355 mono_arm_get_exception_trampolines (gboolean aot)
358 GSList *tramps = NULL;
360 /* LLVM uses the normal trampolines, but with a different name */
361 get_throw_trampoline (168, TRUE, FALSE, FALSE, FALSE, "llvm_throw_corlib_exception_trampoline", &info, aot);
362 tramps = g_slist_prepend (tramps, info);
364 get_throw_trampoline (168, TRUE, FALSE, TRUE, FALSE, "llvm_throw_corlib_exception_abs_trampoline", &info, aot);
365 tramps = g_slist_prepend (tramps, info);
367 get_throw_trampoline (168, FALSE, FALSE, FALSE, TRUE, "llvm_resume_unwind_trampoline", &info, aot);
368 tramps = g_slist_prepend (tramps, info);
374 mono_arch_exceptions_init (void)
380 tramp = mono_aot_get_trampoline ("llvm_throw_corlib_exception_trampoline");
381 mono_register_jit_icall (tramp, "llvm_throw_corlib_exception_trampoline", NULL, TRUE);
382 tramp = mono_aot_get_trampoline ("llvm_throw_corlib_exception_abs_trampoline");
383 mono_register_jit_icall (tramp, "llvm_throw_corlib_exception_abs_trampoline", NULL, TRUE);
384 tramp = mono_aot_get_trampoline ("llvm_resume_unwind_trampoline");
385 mono_register_jit_icall (tramp, "llvm_resume_unwind_trampoline", NULL, TRUE);
387 tramps = mono_arm_get_exception_trampolines (FALSE);
388 for (l = tramps; l; l = l->next) {
389 MonoTrampInfo *info = l->data;
391 mono_register_jit_icall (info->code, g_strdup (info->name), NULL, TRUE);
392 mono_tramp_info_register (info, NULL);
394 g_slist_free (tramps);
399 * mono_arch_unwind_frame:
401 * See exceptions-amd64.c for docs;
404 mono_arch_unwind_frame (MonoDomain *domain, MonoJitTlsData *jit_tls,
405 MonoJitInfo *ji, MonoContext *ctx,
406 MonoContext *new_ctx, MonoLMF **lmf,
407 mgreg_t **save_locations,
408 StackFrameInfo *frame)
410 gpointer ip = MONO_CONTEXT_GET_IP (ctx);
412 memset (frame, 0, sizeof (StackFrameInfo));
419 mono_unwind_reg_t regs [MONO_MAX_IREGS + 1 + 8];
421 guint32 unwind_info_len;
424 if (ji->is_trampoline)
425 frame->type = FRAME_TYPE_TRAMPOLINE;
427 frame->type = FRAME_TYPE_MANAGED;
429 unwind_info = mono_jinfo_get_unwind_info (ji, &unwind_info_len);
432 printf ("%s %p %p\n", ji->d.method->name, ji->code_start, ip);
433 mono_print_unwind_info (unwind_info, unwind_info_len);
436 for (i = 0; i < 16; ++i)
437 regs [i] = new_ctx->regs [i];
439 /* On IOS, d8..d15 are callee saved. They are mapped to 8..15 in unwind.c */
440 for (i = 0; i < 8; ++i)
441 regs [MONO_MAX_IREGS + i] = *(guint64*)&(new_ctx->fregs [8 + i]);
444 mono_unwind_frame (unwind_info, unwind_info_len, ji->code_start,
445 (guint8*)ji->code_start + ji->code_size,
446 ip, NULL, regs, MONO_MAX_IREGS + 8,
447 save_locations, MONO_MAX_IREGS, &cfa);
449 for (i = 0; i < 16; ++i)
450 new_ctx->regs [i] = regs [i];
451 new_ctx->pc = regs [ARMREG_LR];
452 new_ctx->regs [ARMREG_SP] = (gsize)cfa;
454 for (i = 0; i < 8; ++i)
455 new_ctx->fregs [8 + i] = *(double*)&(regs [MONO_MAX_IREGS + i]);
458 /* Clear thumb bit */
461 /* we substract 1, so that the IP points into the call instruction */
467 if (((gsize)(*lmf)->previous_lmf) & 2) {
468 MonoLMFExt *ext = (MonoLMFExt*)(*lmf);
470 if (ext->debugger_invoke) {
472 * This LMF entry is created by the soft debug code to mark transitions to
473 * managed code done during invokes.
475 frame->type = FRAME_TYPE_DEBUGGER_INVOKE;
476 memcpy (new_ctx, &ext->ctx, sizeof (MonoContext));
477 } else if (ext->interp_exit) {
478 frame->type = FRAME_TYPE_INTERP_TO_MANAGED;
479 frame->interp_exit_data = ext->interp_exit_data;
481 g_assert_not_reached ();
484 *lmf = (gpointer)(((gsize)(*lmf)->previous_lmf) & ~3);
489 frame->type = FRAME_TYPE_MANAGED_TO_NATIVE;
491 if ((ji = mini_jit_info_table_find (domain, (gpointer)(*lmf)->ip, NULL))) {
496 frame->method = (*lmf)->method;
500 * The LMF is saved at the start of the method using:
501 * ARM_MOV_REG_REG (code, ARMREG_IP, ARMREG_SP)
502 * ARM_PUSH (code, 0x5ff0);
503 * So it stores the register state as it existed at the caller. We need to
504 * produce the register state which existed at the time of the call which
505 * transitioned to native call, so we save the sp/fp/ip in the LMF.
507 memcpy (&new_ctx->regs [0], &(*lmf)->iregs [0], sizeof (mgreg_t) * 13);
508 new_ctx->pc = (*lmf)->ip;
509 new_ctx->regs [ARMREG_SP] = (*lmf)->sp;
510 new_ctx->regs [ARMREG_FP] = (*lmf)->fp;
512 /* Clear thumb bit */
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 works around a gcc 4.5 bug:
546 * https://bugs.launchpad.net/ubuntu/+source/gcc-4.5/+bug/721531
548 static MONO_NEVER_INLINE gpointer
549 get_handle_signal_exception_addr (void)
551 return handle_signal_exception;
555 * This is the function called from the signal handler
558 mono_arch_handle_exception (void *ctx, gpointer obj)
560 #if defined(MONO_CROSS_COMPILE) || !defined(MONO_ARCH_HAVE_SIGCTX_TO_MONOCTX)
561 g_assert_not_reached ();
562 #elif defined(MONO_ARCH_USE_SIGACTION)
563 arm_ucontext *sigctx = ctx;
565 * Handling the exception in the signal handler is problematic, since the original
566 * signal is disabled, and we could run arbitrary code though the debugger. So
567 * resume into the normal stack and do most work there if possible.
569 MonoJitTlsData *jit_tls = mono_tls_get_jit_tls ();
570 guint64 sp = UCONTEXT_REG_SP (sigctx);
572 /* Pass the ctx parameter in TLS */
573 mono_sigctx_to_monoctx (sigctx, &jit_tls->ex_ctx);
574 /* The others in registers */
575 UCONTEXT_REG_R0 (sigctx) = (gsize)obj;
577 /* Allocate a stack frame */
579 UCONTEXT_REG_SP (sigctx) = sp;
581 UCONTEXT_REG_PC (sigctx) = (gsize)get_handle_signal_exception_addr ();
582 #ifdef UCONTEXT_REG_CPSR
583 if ((gsize)UCONTEXT_REG_PC (sigctx) & 1)
584 /* Transition to thumb */
585 UCONTEXT_REG_CPSR (sigctx) |= (1 << 5);
587 /* Transition to ARM */
588 UCONTEXT_REG_CPSR (sigctx) &= ~(1 << 5);
596 mono_sigctx_to_monoctx (ctx, &mctx);
598 result = mono_handle_exception (&mctx, obj);
599 /* restore the context so that returning from the signal handler will invoke
602 mono_monoctx_to_sigctx (&mctx, ctx);
608 mono_arch_ip_from_context (void *sigctx)
610 #ifdef MONO_CROSS_COMPILE
611 g_assert_not_reached ();
613 arm_ucontext *my_uc = sigctx;
614 return (void*) UCONTEXT_REG_PC (my_uc);
619 mono_arch_setup_async_callback (MonoContext *ctx, void (*async_cb)(void *fun), gpointer user_data)
621 mgreg_t sp = (mgreg_t)MONO_CONTEXT_GET_SP (ctx);
624 g_assert (!user_data);
626 /* Allocate a stack frame */
628 MONO_CONTEXT_SET_SP (ctx, sp);
630 mono_arch_setup_resume_sighandler_ctx (ctx, async_cb);
634 * mono_arch_setup_resume_sighandler_ctx:
636 * Setup CTX so execution continues at FUNC.
639 mono_arch_setup_resume_sighandler_ctx (MonoContext *ctx, gpointer func)
641 MONO_CONTEXT_SET_IP (ctx,func);
642 if ((mgreg_t)MONO_CONTEXT_GET_IP (ctx) & 1)
643 /* Transition to thumb */
644 ctx->cpsr |= (1 << 5);
646 /* Transition to ARM */
647 ctx->cpsr &= ~(1 << 5);