2 * exceptions-arm.c: exception support for ARM
5 * Dietmar Maurer (dietmar@ximian.com)
6 * Paolo Molaro (lupus@ximian.com)
8 * (C) 2001 Ximian, Inc.
16 #ifndef MONO_CROSS_COMPILE
17 #ifdef HAVE_ASM_SIGCONTEXT_H
18 #include <asm/sigcontext.h>
19 #endif /* def HAVE_ASM_SIGCONTEXT_H */
22 #ifdef HAVE_UCONTEXT_H
24 #endif /* def HAVE_UCONTEXT_H */
26 #include <mono/arch/arm/arm-codegen.h>
27 #include <mono/arch/arm/arm-vfp-codegen.h>
28 #include <mono/metadata/abi-details.h>
29 #include <mono/metadata/appdomain.h>
30 #include <mono/metadata/tabledefs.h>
31 #include <mono/metadata/threads.h>
32 #include <mono/metadata/debug-helpers.h>
33 #include <mono/metadata/exception.h>
34 #include <mono/metadata/mono-debug.h>
38 #include "mono/utils/mono-sigcontext.h"
39 #include "mono/utils/mono-compiler.h"
42 * arch_get_restore_context:
44 * Returns a pointer to a method which restores a previously saved sigcontext.
45 * The first argument in r0 is the pointer to the context.
48 mono_arch_get_restore_context (MonoTrampInfo **info, gboolean aot)
53 MonoJumpInfo *ji = NULL;
54 GSList *unwind_ops = NULL;
56 start = code = mono_global_codeman_reserve (128);
59 * Move things to their proper place so we can restore all the registers with
65 if (!mono_arch_is_soft_float ()) {
66 ARM_ADD_REG_IMM8 (code, ARMREG_IP, ctx_reg, MONO_STRUCT_OFFSET (MonoContext, fregs));
67 ARM_FLDMD (code, ARM_VFP_D0, 16, ARMREG_IP);
71 ARM_LDR_IMM (code, ARMREG_IP, ctx_reg, MONO_STRUCT_OFFSET (MonoContext, pc));
72 ARM_STR_IMM (code, ARMREG_IP, ctx_reg, MONO_STRUCT_OFFSET (MonoContext, regs) + (ARMREG_PC * sizeof (mgreg_t)));
74 /* restore everything */
75 ARM_ADD_REG_IMM8 (code, ARMREG_IP, ctx_reg, MONO_STRUCT_OFFSET(MonoContext, regs));
76 ARM_LDM (code, ARMREG_IP, 0xffff);
81 g_assert ((code - start) < 128);
83 mono_arch_flush_icache (start, code - start);
86 *info = mono_tramp_info_create ("restore_context", start, code - start, ji, unwind_ops);
92 * arch_get_call_filter:
94 * Returns a pointer to a method which calls an exception filter. We
95 * also use this function to call finally handlers (we pass NULL as
96 * @exc object in this case).
99 mono_arch_get_call_filter (MonoTrampInfo **info, gboolean aot)
104 MonoJumpInfo *ji = NULL;
105 GSList *unwind_ops = NULL;
107 /* call_filter (MonoContext *ctx, unsigned long eip, gpointer exc) */
108 start = code = mono_global_codeman_reserve (320);
110 /* save all the regs on the stack */
111 ARM_MOV_REG_REG (code, ARMREG_IP, ARMREG_SP);
112 ARM_PUSH (code, MONO_ARM_REGSAVE_MASK);
114 /* restore all the regs from ctx (in r0), but not sp, the stack pointer */
116 ARM_LDR_IMM (code, ARMREG_IP, ctx_reg, MONO_STRUCT_OFFSET (MonoContext, pc));
117 ARM_ADD_REG_IMM8 (code, ARMREG_LR, ctx_reg, MONO_STRUCT_OFFSET(MonoContext, regs) + (MONO_ARM_FIRST_SAVED_REG * sizeof (mgreg_t)));
118 ARM_LDM (code, ARMREG_LR, MONO_ARM_REGSAVE_MASK);
119 /* call handler at eip (r1) and set the first arg with the exception (r2) */
120 ARM_MOV_REG_REG (code, ARMREG_R0, ARMREG_R2);
121 ARM_MOV_REG_REG (code, ARMREG_LR, ARMREG_PC);
122 ARM_MOV_REG_REG (code, ARMREG_PC, ARMREG_R1);
125 ARM_POP_NWB (code, 0xff0 | ((1 << ARMREG_SP) | (1 << ARMREG_PC)));
127 g_assert ((code - start) < 320);
129 mono_arch_flush_icache (start, code - start);
132 *info = mono_tramp_info_create ("call_filter", start, code - start, ji, unwind_ops);
138 mono_arm_throw_exception (MonoObject *exc, mgreg_t pc, mgreg_t sp, mgreg_t *int_regs, gdouble *fp_regs)
142 gboolean rethrow = sp & 1;
144 sp &= ~1; /* clear the optional rethrow bit */
145 pc &= ~1; /* clear the thumb bit */
146 /* adjust eip so that it point into the call instruction */
149 /*printf ("stack in throw: %p\n", esp);*/
150 MONO_CONTEXT_SET_BP (&ctx, int_regs [ARMREG_FP - 4]);
151 MONO_CONTEXT_SET_SP (&ctx, sp);
152 MONO_CONTEXT_SET_IP (&ctx, pc);
153 memcpy (((guint8*)&ctx.regs) + (ARMREG_R4 * sizeof (mgreg_t)), int_regs, 8 * sizeof (mgreg_t));
154 memcpy (&ctx.fregs, fp_regs, sizeof (double) * 16);
156 if (mono_object_isinst_checked (exc, mono_defaults.exception_class, &error)) {
157 MonoException *mono_ex = (MonoException*)exc;
159 mono_ex->stack_trace = NULL;
160 mono_ex->trace_ips = NULL;
163 mono_error_assert_ok (&error);
164 mono_handle_exception (&ctx, exc);
165 mono_restore_context (&ctx);
166 g_assert_not_reached ();
170 mono_arm_throw_exception_by_token (guint32 ex_token_index, mgreg_t pc, mgreg_t sp, mgreg_t *int_regs, gdouble *fp_regs)
172 guint32 ex_token = MONO_TOKEN_TYPE_DEF | ex_token_index;
173 /* Clear thumb bit */
176 mono_arm_throw_exception ((MonoObject*)mono_exception_from_token (mono_defaults.corlib, ex_token), pc, sp, int_regs, fp_regs);
180 mono_arm_resume_unwind (guint32 dummy1, mgreg_t pc, mgreg_t sp, mgreg_t *int_regs, gdouble *fp_regs)
184 pc &= ~1; /* clear the optional rethrow bit */
185 /* adjust eip so that it point into the call instruction */
188 MONO_CONTEXT_SET_BP (&ctx, int_regs [ARMREG_FP - 4]);
189 MONO_CONTEXT_SET_SP (&ctx, sp);
190 MONO_CONTEXT_SET_IP (&ctx, pc);
191 memcpy (((guint8*)&ctx.regs) + (ARMREG_R4 * sizeof (mgreg_t)), int_regs, 8 * sizeof (mgreg_t));
193 mono_resume_unwind (&ctx);
197 * get_throw_trampoline:
199 * Returns a function pointer which can be used to raise
200 * exceptions. The returned function has the following
201 * signature: void (*func) (MonoException *exc); or
202 * void (*func) (guint32 ex_token, guint8* ip);
206 get_throw_trampoline (int size, gboolean corlib, gboolean rethrow, gboolean llvm, gboolean resume_unwind, const char *tramp_name, MonoTrampInfo **info, gboolean aot)
210 MonoJumpInfo *ji = NULL;
211 GSList *unwind_ops = NULL;
214 code = start = mono_global_codeman_reserve (size);
216 mono_add_unwind_op_def_cfa (unwind_ops, code, start, ARMREG_SP, 0);
218 /* save all the regs on the stack */
219 ARM_MOV_REG_REG (code, ARMREG_IP, ARMREG_SP);
220 ARM_PUSH (code, MONO_ARM_REGSAVE_MASK);
222 cfa_offset = MONO_ARM_NUM_SAVED_REGS * sizeof (mgreg_t);
223 mono_add_unwind_op_def_cfa (unwind_ops, code, start, ARMREG_SP, cfa_offset);
224 mono_add_unwind_op_offset (unwind_ops, code, start, ARMREG_LR, - sizeof (mgreg_t));
227 if (!mono_arch_is_soft_float ()) {
228 ARM_SUB_REG_IMM8 (code, ARMREG_SP, ARMREG_SP, sizeof (double) * 16);
229 cfa_offset += sizeof (double) * 16;
230 mono_add_unwind_op_def_cfa_offset (unwind_ops, code, start, cfa_offset);
231 ARM_FSTMD (code, ARM_VFP_D0, 16, ARMREG_SP);
235 ARM_SUB_REG_IMM8 (code, ARMREG_SP, ARMREG_SP, 8);
237 mono_add_unwind_op_def_cfa_offset (unwind_ops, code, start, cfa_offset);
239 /* call throw_exception (exc, ip, sp, int_regs, fp_regs) */
241 ARM_ADD_REG_IMM8 (code, ARMREG_R2, ARMREG_SP, cfa_offset);
242 /* we encode rethrow in sp */
244 g_assert (!resume_unwind);
246 ARM_ORR_REG_IMM8 (code, ARMREG_R2, ARMREG_R2, rethrow);
248 /* exc is already in place in r0 */
250 /* The caller ip is already in R1 */
253 * The address passed by llvm might point to before the call,
254 * thus outside the eh range recorded by llvm. Use the return
256 * FIXME: Do this on more platforms.
258 ARM_MOV_REG_REG (code, ARMREG_R1, ARMREG_LR); /* caller ip */
261 ARM_MOV_REG_REG (code, ARMREG_R1, ARMREG_LR); /* caller ip */
264 ARM_ADD_REG_IMM8 (code, ARMREG_R3, ARMREG_SP, (cfa_offset - (MONO_ARM_NUM_SAVED_REGS * sizeof (mgreg_t))));
266 ARM_ADD_REG_IMM8 (code, ARMREG_LR, ARMREG_SP, 8);
267 ARM_STR_IMM (code, ARMREG_LR, ARMREG_SP, 0);
270 const char *icall_name;
273 icall_name = "mono_arm_resume_unwind";
275 icall_name = "mono_arm_throw_exception_by_token";
277 icall_name = "mono_arm_throw_exception";
279 ji = mono_patch_info_list_prepend (ji, code - start, MONO_PATCH_INFO_JIT_ICALL_ADDR, icall_name);
280 ARM_LDR_IMM (code, ARMREG_IP, ARMREG_PC, 0);
282 *(gpointer*)(gpointer)code = NULL;
284 ARM_LDR_REG_REG (code, ARMREG_IP, ARMREG_PC, ARMREG_IP);
286 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)));
288 ARM_MOV_REG_REG (code, ARMREG_LR, ARMREG_PC);
289 ARM_MOV_REG_REG (code, ARMREG_PC, ARMREG_IP);
290 /* we should never reach this breakpoint */
292 g_assert ((code - start) < size);
293 mono_arch_flush_icache (start, code - start);
296 *info = mono_tramp_info_create (tramp_name, start, code - start, ji, unwind_ops);
302 * arch_get_throw_exception:
304 * Returns a function pointer which can be used to raise
305 * exceptions. The returned function has the following
306 * signature: void (*func) (MonoException *exc);
307 * For example to raise an arithmetic exception you can use:
309 * x86_push_imm (code, mono_get_exception_arithmetic ());
310 * x86_call_code (code, arch_get_throw_exception ());
314 mono_arch_get_throw_exception (MonoTrampInfo **info, gboolean aot)
316 return get_throw_trampoline (132, FALSE, FALSE, FALSE, FALSE, "throw_exception", info, aot);
320 * mono_arch_get_rethrow_exception:
322 * Returns a function pointer which can be used to rethrow
323 * exceptions. The returned function has the following
324 * signature: void (*func) (MonoException *exc);
328 mono_arch_get_rethrow_exception (MonoTrampInfo **info, gboolean aot)
330 return get_throw_trampoline (132, FALSE, TRUE, FALSE, FALSE, "rethrow_exception", info, aot);
334 * mono_arch_get_throw_corlib_exception:
336 * Returns a function pointer which can be used to raise
337 * corlib exceptions. The returned function has the following
338 * signature: void (*func) (guint32 ex_token, guint32 offset);
339 * Here, offset is the offset which needs to be substracted from the caller IP
340 * to get the IP of the throw. Passing the offset has the advantage that it
341 * needs no relocations in the caller.
342 * On ARM, the ip is passed instead of an offset.
345 mono_arch_get_throw_corlib_exception (MonoTrampInfo **info, gboolean aot)
347 return get_throw_trampoline (168, TRUE, FALSE, FALSE, FALSE, "throw_corlib_exception", info, aot);
351 mono_arm_get_exception_trampolines (gboolean aot)
354 GSList *tramps = NULL;
356 /* LLVM uses the normal trampolines, but with a different name */
357 get_throw_trampoline (168, TRUE, FALSE, FALSE, FALSE, "llvm_throw_corlib_exception_trampoline", &info, aot);
358 tramps = g_slist_prepend (tramps, info);
360 get_throw_trampoline (168, TRUE, FALSE, TRUE, FALSE, "llvm_throw_corlib_exception_abs_trampoline", &info, aot);
361 tramps = g_slist_prepend (tramps, info);
363 get_throw_trampoline (168, FALSE, FALSE, FALSE, TRUE, "llvm_resume_unwind_trampoline", &info, aot);
364 tramps = g_slist_prepend (tramps, info);
370 mono_arch_exceptions_init (void)
376 tramp = mono_aot_get_trampoline ("llvm_throw_corlib_exception_trampoline");
377 mono_register_jit_icall (tramp, "llvm_throw_corlib_exception_trampoline", NULL, TRUE);
378 tramp = mono_aot_get_trampoline ("llvm_throw_corlib_exception_abs_trampoline");
379 mono_register_jit_icall (tramp, "llvm_throw_corlib_exception_abs_trampoline", NULL, TRUE);
380 tramp = mono_aot_get_trampoline ("llvm_resume_unwind_trampoline");
381 mono_register_jit_icall (tramp, "llvm_resume_unwind_trampoline", NULL, TRUE);
383 tramps = mono_arm_get_exception_trampolines (FALSE);
384 for (l = tramps; l; l = l->next) {
385 MonoTrampInfo *info = l->data;
387 mono_register_jit_icall (info->code, g_strdup (info->name), NULL, TRUE);
388 mono_tramp_info_register (info, NULL);
390 g_slist_free (tramps);
395 * mono_arch_unwind_frame:
397 * See exceptions-amd64.c for docs;
400 mono_arch_unwind_frame (MonoDomain *domain, MonoJitTlsData *jit_tls,
401 MonoJitInfo *ji, MonoContext *ctx,
402 MonoContext *new_ctx, MonoLMF **lmf,
403 mgreg_t **save_locations,
404 StackFrameInfo *frame)
406 gpointer ip = MONO_CONTEXT_GET_IP (ctx);
408 memset (frame, 0, sizeof (StackFrameInfo));
415 mono_unwind_reg_t regs [MONO_MAX_IREGS + 1 + 8];
417 guint32 unwind_info_len;
420 if (ji->is_trampoline)
421 frame->type = FRAME_TYPE_TRAMPOLINE;
423 frame->type = FRAME_TYPE_MANAGED;
425 unwind_info = mono_jinfo_get_unwind_info (ji, &unwind_info_len);
428 printf ("%s %p %p\n", ji->d.method->name, ji->code_start, ip);
429 mono_print_unwind_info (unwind_info, unwind_info_len);
432 for (i = 0; i < 16; ++i)
433 regs [i] = new_ctx->regs [i];
435 /* On IOS, d8..d15 are callee saved. They are mapped to 8..15 in unwind.c */
436 for (i = 0; i < 8; ++i)
437 regs [MONO_MAX_IREGS + i] = *(guint64*)&(new_ctx->fregs [8 + i]);
440 mono_unwind_frame (unwind_info, unwind_info_len, ji->code_start,
441 (guint8*)ji->code_start + ji->code_size,
442 ip, NULL, regs, MONO_MAX_IREGS + 8,
443 save_locations, MONO_MAX_IREGS, &cfa);
445 for (i = 0; i < 16; ++i)
446 new_ctx->regs [i] = regs [i];
447 new_ctx->pc = regs [ARMREG_LR];
448 new_ctx->regs [ARMREG_SP] = (gsize)cfa;
450 for (i = 0; i < 8; ++i)
451 new_ctx->fregs [8 + i] = *(double*)&(regs [MONO_MAX_IREGS + i]);
454 /* Clear thumb bit */
457 /* we substract 1, so that the IP points into the call instruction */
463 if (((gsize)(*lmf)->previous_lmf) & 2) {
465 * This LMF entry is created by the soft debug code to mark transitions to
466 * managed code done during invokes.
468 MonoLMFExt *ext = (MonoLMFExt*)(*lmf);
470 g_assert (ext->debugger_invoke);
472 memcpy (new_ctx, &ext->ctx, sizeof (MonoContext));
474 *lmf = (gpointer)(((gsize)(*lmf)->previous_lmf) & ~3);
476 frame->type = FRAME_TYPE_DEBUGGER_INVOKE;
481 frame->type = FRAME_TYPE_MANAGED_TO_NATIVE;
483 if ((ji = mini_jit_info_table_find (domain, (gpointer)(*lmf)->ip, NULL))) {
488 frame->method = (*lmf)->method;
492 * The LMF is saved at the start of the method using:
493 * ARM_MOV_REG_REG (code, ARMREG_IP, ARMREG_SP)
494 * ARM_PUSH (code, 0x5ff0);
495 * So it stores the register state as it existed at the caller. We need to
496 * produce the register state which existed at the time of the call which
497 * transitioned to native call, so we save the sp/fp/ip in the LMF.
499 memcpy (&new_ctx->regs [0], &(*lmf)->iregs [0], sizeof (mgreg_t) * 13);
500 new_ctx->pc = (*lmf)->ip;
501 new_ctx->regs [ARMREG_SP] = (*lmf)->sp;
502 new_ctx->regs [ARMREG_FP] = (*lmf)->fp;
504 /* Clear thumb bit */
507 /* we substract 1, so that the IP points into the call instruction */
510 *lmf = (gpointer)(((gsize)(*lmf)->previous_lmf) & ~3);
521 * Called by resuming from a signal handler.
524 handle_signal_exception (gpointer obj)
526 MonoJitTlsData *jit_tls = mono_native_tls_get_value (mono_jit_tls_id);
529 memcpy (&ctx, &jit_tls->ex_ctx, sizeof (MonoContext));
531 mono_handle_exception (&ctx, obj);
533 mono_restore_context (&ctx);
537 * This works around a gcc 4.5 bug:
538 * https://bugs.launchpad.net/ubuntu/+source/gcc-4.5/+bug/721531
540 static MONO_NEVER_INLINE gpointer
541 get_handle_signal_exception_addr (void)
543 return handle_signal_exception;
547 * This is the function called from the signal handler
550 mono_arch_handle_exception (void *ctx, gpointer obj)
552 #if defined(MONO_CROSS_COMPILE) || !defined(MONO_ARCH_HAVE_SIGCTX_TO_MONOCTX)
553 g_assert_not_reached ();
554 #elif defined(MONO_ARCH_USE_SIGACTION)
555 arm_ucontext *sigctx = ctx;
557 * Handling the exception in the signal handler is problematic, since the original
558 * signal is disabled, and we could run arbitrary code though the debugger. So
559 * resume into the normal stack and do most work there if possible.
561 MonoJitTlsData *jit_tls = mono_native_tls_get_value (mono_jit_tls_id);
562 guint64 sp = UCONTEXT_REG_SP (sigctx);
564 /* Pass the ctx parameter in TLS */
565 mono_sigctx_to_monoctx (sigctx, &jit_tls->ex_ctx);
566 /* The others in registers */
567 UCONTEXT_REG_R0 (sigctx) = (gsize)obj;
569 /* Allocate a stack frame */
571 UCONTEXT_REG_SP (sigctx) = sp;
573 UCONTEXT_REG_PC (sigctx) = (gsize)get_handle_signal_exception_addr ();
574 #ifdef UCONTEXT_REG_CPSR
575 if ((gsize)UCONTEXT_REG_PC (sigctx) & 1)
576 /* Transition to thumb */
577 UCONTEXT_REG_CPSR (sigctx) |= (1 << 5);
579 /* Transition to ARM */
580 UCONTEXT_REG_CPSR (sigctx) &= ~(1 << 5);
588 mono_sigctx_to_monoctx (ctx, &mctx);
590 result = mono_handle_exception (&mctx, obj);
591 /* restore the context so that returning from the signal handler will invoke
594 mono_monoctx_to_sigctx (&mctx, ctx);
600 mono_arch_ip_from_context (void *sigctx)
602 #ifdef MONO_CROSS_COMPILE
603 g_assert_not_reached ();
604 #elif defined(__native_client__)
605 g_assert_not_reached ();
607 arm_ucontext *my_uc = sigctx;
608 return (void*) UCONTEXT_REG_PC (my_uc);
613 mono_arch_setup_async_callback (MonoContext *ctx, void (*async_cb)(void *fun), gpointer user_data)
615 mgreg_t sp = (mgreg_t)MONO_CONTEXT_GET_SP (ctx);
618 g_assert (!user_data);
620 /* Allocate a stack frame */
622 MONO_CONTEXT_SET_SP (ctx, sp);
624 mono_arch_setup_resume_sighandler_ctx (ctx, async_cb);
628 * mono_arch_setup_resume_sighandler_ctx:
630 * Setup CTX so execution continues at FUNC.
633 mono_arch_setup_resume_sighandler_ctx (MonoContext *ctx, gpointer func)
635 MONO_CONTEXT_SET_IP (ctx,func);
636 if ((mgreg_t)MONO_CONTEXT_GET_IP (ctx) & 1)
637 /* Transition to thumb */
638 ctx->cpsr |= (1 << 5);
640 /* Transition to ARM */
641 ctx->cpsr &= ~(1 << 5);