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/appdomain.h>
29 #include <mono/metadata/tabledefs.h>
30 #include <mono/metadata/threads.h>
31 #include <mono/metadata/debug-helpers.h>
32 #include <mono/metadata/exception.h>
33 #include <mono/metadata/mono-debug.h>
37 #include "mono/utils/mono-sigcontext.h"
40 * arch_get_restore_context:
42 * Returns a pointer to a method which restores a previously saved sigcontext.
43 * The first argument in r0 is the pointer to the context.
46 mono_arch_get_restore_context (MonoTrampInfo **info, gboolean aot)
51 MonoJumpInfo *ji = NULL;
52 GSList *unwind_ops = NULL;
54 start = code = mono_global_codeman_reserve (128);
57 * Move things to their proper place so we can restore all the registers with
63 #if defined(ARM_FPU_VFP)
64 ARM_ADD_REG_IMM8 (code, ARMREG_IP, ctx_reg, G_STRUCT_OFFSET (MonoContext, fregs));
65 ARM_FLDMD (code, ARM_VFP_D0, 16, ARMREG_IP);
69 ARM_LDR_IMM (code, ARMREG_IP, ctx_reg, G_STRUCT_OFFSET (MonoContext, pc));
70 ARM_STR_IMM (code, ARMREG_IP, ctx_reg, G_STRUCT_OFFSET (MonoContext, regs) + (ARMREG_PC * sizeof (mgreg_t)));
72 /* restore everything */
73 ARM_ADD_REG_IMM8 (code, ARMREG_IP, ctx_reg, G_STRUCT_OFFSET(MonoContext, regs));
74 ARM_LDM (code, ARMREG_IP, 0xffff);
79 g_assert ((code - start) < 128);
81 mono_arch_flush_icache (start, code - start);
84 *info = mono_tramp_info_create (g_strdup_printf ("restore_context"), start, code - start, ji, unwind_ops);
90 * arch_get_call_filter:
92 * Returns a pointer to a method which calls an exception filter. We
93 * also use this function to call finally handlers (we pass NULL as
94 * @exc object in this case).
97 mono_arch_get_call_filter (MonoTrampInfo **info, gboolean aot)
102 MonoJumpInfo *ji = NULL;
103 GSList *unwind_ops = NULL;
105 /* call_filter (MonoContext *ctx, unsigned long eip, gpointer exc) */
106 start = code = mono_global_codeman_reserve (320);
108 /* save all the regs on the stack */
109 ARM_MOV_REG_REG (code, ARMREG_IP, ARMREG_SP);
110 ARM_PUSH (code, MONO_ARM_REGSAVE_MASK);
112 /* restore all the regs from ctx (in r0), but not sp, the stack pointer */
114 ARM_LDR_IMM (code, ARMREG_IP, ctx_reg, G_STRUCT_OFFSET (MonoContext, pc));
115 ARM_ADD_REG_IMM8 (code, ARMREG_LR, ctx_reg, G_STRUCT_OFFSET(MonoContext, regs) + (MONO_ARM_FIRST_SAVED_REG * sizeof (mgreg_t)));
116 ARM_LDM (code, ARMREG_LR, MONO_ARM_REGSAVE_MASK);
117 /* call handler at eip (r1) and set the first arg with the exception (r2) */
118 ARM_MOV_REG_REG (code, ARMREG_R0, ARMREG_R2);
119 ARM_MOV_REG_REG (code, ARMREG_LR, ARMREG_PC);
120 ARM_MOV_REG_REG (code, ARMREG_PC, ARMREG_R1);
123 ARM_POP_NWB (code, 0xff0 | ((1 << ARMREG_SP) | (1 << ARMREG_PC)));
125 g_assert ((code - start) < 320);
127 mono_arch_flush_icache (start, code - start);
130 *info = mono_tramp_info_create (g_strdup_printf ("call_filter"), start, code - start, ji, unwind_ops);
136 mono_arm_throw_exception (MonoObject *exc, mgreg_t pc, mgreg_t sp, mgreg_t *int_regs, gdouble *fp_regs)
138 static void (*restore_context) (MonoContext *);
140 gboolean rethrow = pc & 1;
142 if (!restore_context)
143 restore_context = mono_get_restore_context ();
145 pc &= ~1; /* clear the optional rethrow 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 (exc, mono_defaults.exception_class)) {
157 MonoException *mono_ex = (MonoException*)exc;
159 mono_ex->stack_trace = NULL;
161 mono_handle_exception (&ctx, exc);
162 restore_context (&ctx);
163 g_assert_not_reached ();
167 mono_arm_throw_exception_by_token (guint32 type_token, mgreg_t pc, mgreg_t sp, mgreg_t *int_regs, gdouble *fp_regs)
169 /* Clear thumb bit */
172 mono_arm_throw_exception ((MonoObject*)mono_exception_from_token (mono_defaults.corlib, type_token), pc, sp, int_regs, fp_regs);
176 mono_arm_resume_unwind (guint32 dummy1, mgreg_t pc, mgreg_t sp, mgreg_t *int_regs, gdouble *fp_regs)
180 pc &= ~1; /* clear the optional rethrow bit */
181 /* adjust eip so that it point into the call instruction */
184 MONO_CONTEXT_SET_BP (&ctx, int_regs [ARMREG_FP - 4]);
185 MONO_CONTEXT_SET_SP (&ctx, sp);
186 MONO_CONTEXT_SET_IP (&ctx, pc);
187 memcpy (((guint8*)&ctx.regs) + (ARMREG_R4 * sizeof (mgreg_t)), int_regs, 8 * sizeof (mgreg_t));
189 mono_resume_unwind (&ctx);
193 * get_throw_trampoline:
195 * Returns a function pointer which can be used to raise
196 * exceptions. The returned function has the following
197 * signature: void (*func) (MonoException *exc); or
198 * void (*func) (guint32 ex_token, guint8* ip);
202 get_throw_trampoline (int size, gboolean corlib, gboolean rethrow, gboolean llvm, gboolean resume_unwind, const char *tramp_name, MonoTrampInfo **info, gboolean aot)
206 MonoJumpInfo *ji = NULL;
207 GSList *unwind_ops = NULL;
210 code = start = mono_global_codeman_reserve (size);
212 mono_add_unwind_op_def_cfa (unwind_ops, code, start, ARMREG_SP, 0);
214 /* save all the regs on the stack */
215 ARM_MOV_REG_REG (code, ARMREG_IP, ARMREG_SP);
216 ARM_PUSH (code, MONO_ARM_REGSAVE_MASK);
218 cfa_offset = MONO_ARM_NUM_SAVED_REGS * sizeof (mgreg_t);
219 mono_add_unwind_op_def_cfa (unwind_ops, code, start, ARMREG_SP, cfa_offset);
220 mono_add_unwind_op_offset (unwind_ops, code, start, ARMREG_LR, - sizeof (mgreg_t));
223 ARM_SUB_REG_IMM8 (code, ARMREG_SP, ARMREG_SP, sizeof (double) * 16);
224 cfa_offset += sizeof (double) * 16;
225 mono_add_unwind_op_def_cfa_offset (unwind_ops, code, start, cfa_offset);
226 #if defined(ARM_FPU_VFP)
227 ARM_FSTMD (code, ARM_VFP_D0, 16, ARMREG_SP);
231 ARM_SUB_REG_IMM8 (code, ARMREG_SP, ARMREG_SP, 8);
233 mono_add_unwind_op_def_cfa_offset (unwind_ops, code, start, cfa_offset);
235 /* call throw_exception (exc, ip, sp, int_regs, fp_regs) */
237 ARM_ADD_REG_IMM8 (code, ARMREG_R2, ARMREG_SP, cfa_offset);
238 /* exc is already in place in r0 */
240 /* The caller ip is already in R1 */
242 /* Negate the ip adjustment done in mono_arm_throw_exception */
243 ARM_ADD_REG_IMM8 (code, ARMREG_R1, ARMREG_R1, 4);
245 ARM_MOV_REG_REG (code, ARMREG_R1, ARMREG_LR); /* caller ip */
248 ARM_ADD_REG_IMM8 (code, ARMREG_R3, ARMREG_SP, (cfa_offset - (MONO_ARM_NUM_SAVED_REGS * sizeof (mgreg_t))));
249 /* we encode rethrow in the ip */
250 ARM_ORR_REG_IMM8 (code, ARMREG_R1, ARMREG_R1, rethrow);
252 ARM_ADD_REG_IMM8 (code, ARMREG_LR, ARMREG_SP, 8);
253 ARM_STR_IMM (code, ARMREG_LR, ARMREG_SP, 0);
256 const char *icall_name;
259 icall_name = "mono_arm_resume_unwind";
261 icall_name = "mono_arm_throw_exception_by_token";
263 icall_name = "mono_arm_throw_exception";
265 ji = mono_patch_info_list_prepend (ji, code - start, MONO_PATCH_INFO_JIT_ICALL_ADDR, icall_name);
266 ARM_LDR_IMM (code, ARMREG_IP, ARMREG_PC, 0);
268 *(gpointer*)(gpointer)code = NULL;
270 ARM_LDR_REG_REG (code, ARMREG_IP, ARMREG_PC, ARMREG_IP);
272 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)));
274 ARM_MOV_REG_REG (code, ARMREG_LR, ARMREG_PC);
275 ARM_MOV_REG_REG (code, ARMREG_PC, ARMREG_IP);
276 /* we should never reach this breakpoint */
278 g_assert ((code - start) < size);
279 mono_arch_flush_icache (start, code - start);
282 *info = mono_tramp_info_create (g_strdup_printf (tramp_name), start, code - start, ji, unwind_ops);
288 * arch_get_throw_exception:
290 * Returns a function pointer which can be used to raise
291 * exceptions. The returned function has the following
292 * signature: void (*func) (MonoException *exc);
293 * For example to raise an arithmetic exception you can use:
295 * x86_push_imm (code, mono_get_exception_arithmetic ());
296 * x86_call_code (code, arch_get_throw_exception ());
300 mono_arch_get_throw_exception (MonoTrampInfo **info, gboolean aot)
302 return get_throw_trampoline (132, FALSE, FALSE, FALSE, FALSE, "throw_exception", info, aot);
306 * mono_arch_get_rethrow_exception:
308 * Returns a function pointer which can be used to rethrow
309 * exceptions. The returned function has the following
310 * signature: void (*func) (MonoException *exc);
314 mono_arch_get_rethrow_exception (MonoTrampInfo **info, gboolean aot)
316 return get_throw_trampoline (132, FALSE, TRUE, FALSE, FALSE, "rethrow_exception", info, aot);
320 * mono_arch_get_throw_corlib_exception:
322 * Returns a function pointer which can be used to raise
323 * corlib exceptions. The returned function has the following
324 * signature: void (*func) (guint32 ex_token, guint32 offset);
325 * Here, offset is the offset which needs to be substracted from the caller IP
326 * to get the IP of the throw. Passing the offset has the advantage that it
327 * needs no relocations in the caller.
328 * On ARM, the ip is passed instead of an offset.
331 mono_arch_get_throw_corlib_exception (MonoTrampInfo **info, gboolean aot)
333 return get_throw_trampoline (168, TRUE, FALSE, FALSE, FALSE, "throw_corlib_exception", info, aot);
337 mono_arm_get_exception_trampolines (gboolean aot)
340 GSList *tramps = NULL;
342 /* LLVM uses the normal trampolines, but with a different name */
343 get_throw_trampoline (168, TRUE, FALSE, FALSE, FALSE, "llvm_throw_corlib_exception_trampoline", &info, aot);
344 tramps = g_slist_prepend (tramps, info);
346 get_throw_trampoline (168, TRUE, FALSE, TRUE, FALSE, "llvm_throw_corlib_exception_abs_trampoline", &info, aot);
347 tramps = g_slist_prepend (tramps, info);
349 get_throw_trampoline (168, FALSE, FALSE, FALSE, TRUE, "llvm_resume_unwind_trampoline", &info, aot);
350 tramps = g_slist_prepend (tramps, info);
356 mono_arch_exceptions_init (void)
362 tramp = mono_aot_get_trampoline ("llvm_throw_corlib_exception_trampoline");
363 mono_register_jit_icall (tramp, "llvm_throw_corlib_exception_trampoline", NULL, TRUE);
364 tramp = mono_aot_get_trampoline ("llvm_throw_corlib_exception_abs_trampoline");
365 mono_register_jit_icall (tramp, "llvm_throw_corlib_exception_abs_trampoline", NULL, TRUE);
366 tramp = mono_aot_get_trampoline ("llvm_resume_unwind_trampoline");
367 mono_register_jit_icall (tramp, "llvm_resume_unwind_trampoline", NULL, TRUE);
369 tramps = mono_arm_get_exception_trampolines (FALSE);
370 for (l = tramps; l; l = l->next) {
371 MonoTrampInfo *info = l->data;
373 mono_register_jit_icall (info->code, g_strdup (info->name), NULL, TRUE);
374 mono_save_trampoline_xdebug_info (info);
375 mono_tramp_info_free (info);
377 g_slist_free (tramps);
382 * mono_arch_find_jit_info:
384 * See exceptions-amd64.c for docs;
387 mono_arch_find_jit_info (MonoDomain *domain, MonoJitTlsData *jit_tls,
388 MonoJitInfo *ji, MonoContext *ctx,
389 MonoContext *new_ctx, MonoLMF **lmf,
390 mgreg_t **save_locations,
391 StackFrameInfo *frame)
393 gpointer ip = MONO_CONTEXT_GET_IP (ctx);
395 memset (frame, 0, sizeof (StackFrameInfo));
402 gssize regs [MONO_MAX_IREGS + 1];
404 guint32 unwind_info_len;
407 frame->type = FRAME_TYPE_MANAGED;
410 unwind_info = mono_aot_get_unwind_info (ji, &unwind_info_len);
412 unwind_info = mono_get_cached_unwind_info (ji->used_regs, &unwind_info_len);
414 for (i = 0; i < 16; ++i)
415 regs [i] = new_ctx->regs [i];
417 mono_unwind_frame (unwind_info, unwind_info_len, ji->code_start,
418 (guint8*)ji->code_start + ji->code_size,
419 ip, regs, MONO_MAX_IREGS,
420 save_locations, MONO_MAX_IREGS, &cfa);
422 for (i = 0; i < 16; ++i)
423 new_ctx->regs [i] = regs [i];
424 new_ctx->pc = regs [ARMREG_LR];
425 new_ctx->regs [ARMREG_SP] = (gsize)cfa;
427 if (*lmf && (MONO_CONTEXT_GET_SP (ctx) >= (gpointer)(*lmf)->sp)) {
428 /* remove any unused lmf */
429 *lmf = (gpointer)(((gsize)(*lmf)->previous_lmf) & ~3);
432 /* Clear thumb bit */
435 /* we substract 1, so that the IP points into the call instruction */
441 if (((gsize)(*lmf)->previous_lmf) & 2) {
443 * This LMF entry is created by the soft debug code to mark transitions to
444 * managed code done during invokes.
446 MonoLMFExt *ext = (MonoLMFExt*)(*lmf);
448 g_assert (ext->debugger_invoke);
450 memcpy (new_ctx, &ext->ctx, sizeof (MonoContext));
452 *lmf = (gpointer)(((gsize)(*lmf)->previous_lmf) & ~3);
454 frame->type = FRAME_TYPE_DEBUGGER_INVOKE;
459 frame->type = FRAME_TYPE_MANAGED_TO_NATIVE;
461 if ((ji = mini_jit_info_table_find (domain, (gpointer)(*lmf)->ip, NULL))) {
466 frame->method = (*lmf)->method;
470 * The LMF is saved at the start of the method using:
471 * ARM_MOV_REG_REG (code, ARMREG_IP, ARMREG_SP)
472 * ARM_PUSH (code, 0x5ff0);
473 * So it stores the register state as it existed at the caller. We need to
474 * produce the register state which existed at the time of the call which
475 * transitioned to native call, so we save the sp/fp/ip in the LMF.
477 memcpy (&new_ctx->regs [0], &(*lmf)->iregs [0], sizeof (mgreg_t) * 13);
478 new_ctx->pc = (*lmf)->ip;
479 new_ctx->regs [ARMREG_SP] = (*lmf)->sp;
480 new_ctx->regs [ARMREG_FP] = (*lmf)->fp;
482 /* Clear thumb bit */
485 /* we substract 1, so that the IP points into the call instruction */
488 *lmf = (gpointer)(((gsize)(*lmf)->previous_lmf) & ~3);
497 mono_arch_sigctx_to_monoctx (void *sigctx, MonoContext *mctx)
499 mono_sigctx_to_monoctx (sigctx, mctx);
503 mono_arch_monoctx_to_sigctx (MonoContext *mctx, void *ctx)
505 mono_monoctx_to_sigctx (mctx, ctx);
511 * Called by resuming from a signal handler.
514 handle_signal_exception (gpointer obj)
516 MonoJitTlsData *jit_tls = mono_native_tls_get_value (mono_jit_tls_id);
518 static void (*restore_context) (MonoContext *);
520 if (!restore_context)
521 restore_context = mono_get_restore_context ();
523 memcpy (&ctx, &jit_tls->ex_ctx, sizeof (MonoContext));
525 mono_handle_exception (&ctx, obj);
527 restore_context (&ctx);
531 * This works around a gcc 4.5 bug:
532 * https://bugs.launchpad.net/ubuntu/+source/gcc-4.5/+bug/721531
534 #if defined(__GNUC__)
535 __attribute__((noinline))
538 get_handle_signal_exception_addr (void)
540 return handle_signal_exception;
544 * This is the function called from the signal handler
547 mono_arch_handle_exception (void *ctx, gpointer obj)
549 #if defined(MONO_CROSS_COMPILE)
550 g_assert_not_reached ();
551 #elif defined(MONO_ARCH_USE_SIGACTION)
552 arm_ucontext *sigctx = ctx;
554 * Handling the exception in the signal handler is problematic, since the original
555 * signal is disabled, and we could run arbitrary code though the debugger. So
556 * resume into the normal stack and do most work there if possible.
558 MonoJitTlsData *jit_tls = mono_native_tls_get_value (mono_jit_tls_id);
559 guint64 sp = UCONTEXT_REG_SP (sigctx);
561 /* Pass the ctx parameter in TLS */
562 mono_arch_sigctx_to_monoctx (sigctx, &jit_tls->ex_ctx);
563 /* The others in registers */
564 UCONTEXT_REG_R0 (sigctx) = (gsize)obj;
566 /* Allocate a stack frame */
568 UCONTEXT_REG_SP (sigctx) = sp;
570 UCONTEXT_REG_PC (sigctx) = (gsize)get_handle_signal_exception_addr ();
571 #ifdef UCONTEXT_REG_CPSR
572 if ((gsize)UCONTEXT_REG_PC (sigctx) & 1)
573 /* Transition to thumb */
574 UCONTEXT_REG_CPSR (sigctx) |= (1 << 5);
576 /* Transition to ARM */
577 UCONTEXT_REG_CPSR (sigctx) &= ~(1 << 5);
585 mono_arch_sigctx_to_monoctx (ctx, &mctx);
587 result = mono_handle_exception (&mctx, obj);
588 /* restore the context so that returning from the signal handler will invoke
591 mono_arch_monoctx_to_sigctx (&mctx, ctx);
597 mono_arch_ip_from_context (void *sigctx)
599 #ifdef MONO_CROSS_COMPILE
600 g_assert_not_reached ();
602 arm_ucontext *my_uc = sigctx;
603 return (void*) UCONTEXT_REG_PC (my_uc);
608 mono_arch_setup_async_callback (MonoContext *ctx, void (*async_cb)(void *fun), gpointer user_data)
610 mgreg_t sp = (mgreg_t)MONO_CONTEXT_GET_SP (ctx);
613 g_assert (!user_data);
615 /* Allocate a stack frame */
617 MONO_CONTEXT_SET_SP (ctx, sp);
618 MONO_CONTEXT_SET_IP (ctx, async_cb);
624 * mono_arch_setup_resume_sighandler_ctx:
626 * Setup CTX so execution continues at FUNC.
629 mono_arch_setup_resume_sighandler_ctx (MonoContext *ctx, gpointer func)
631 MONO_CONTEXT_SET_IP (ctx,func);
632 if ((mgreg_t)MONO_CONTEXT_GET_IP (ctx) & 1)
633 /* Transition to thumb */
634 ctx->cpsr |= (1 << 5);
636 /* Transition to ARM */
637 ctx->cpsr &= ~(1 << 5);