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.
17 #include <mono/arch/arm/arm-codegen.h>
18 #include <mono/metadata/appdomain.h>
19 #include <mono/metadata/tabledefs.h>
20 #include <mono/metadata/threads.h>
21 #include <mono/metadata/debug-helpers.h>
22 #include <mono/metadata/exception.h>
23 #include <mono/metadata/mono-debug.h>
31 unsigned long trap_no;
32 unsigned long error_code;
33 unsigned long oldmask;
44 unsigned long arm_r10;
50 unsigned long arm_cpsr;
51 unsigned long fault_address;
54 gregs below is this struct
56 unsigned long int uregs[18];
59 the companion user_fpregs has just 8 double registers
60 (it's valid for FPA mode, will need changes for VFP)
67 typedef struct ucontext {
68 unsigned long int uc_flags;
69 struct ucontext *uc_link;
70 __sigset_t uc_sigmask;
72 mcontext_t uc_mcontext;
73 long int uc_filler[5];
79 * So, it turns out that the ucontext struct defined by libc is incorrect.
80 * We define our own version here and use it instead.
84 #define my_ucontext ucontext_t
86 typedef struct my_ucontext {
87 unsigned long uc_flags;
88 struct my_ucontext *uc_link;
94 struct sigcontext sig_ctx;
95 /* some 2.6.x kernel has fp data here after a few other fields
96 * we don't use them for now...
102 * arch_get_restore_context:
104 * Returns a pointer to a method which restores a previously saved sigcontext.
105 * The first argument in r0 is the pointer to the context.
108 mono_arch_get_restore_context_full (guint32 *code_size, MonoJumpInfo **ji, gboolean aot)
116 start = code = mono_global_codeman_reserve (128);
119 * Move things to their proper place so we can restore all the registers with
126 ARM_LDR_IMM (code, ARMREG_IP, ctx_reg, G_STRUCT_OFFSET (MonoContext, eip));
127 ARM_STR_IMM (code, ARMREG_IP, ctx_reg, G_STRUCT_OFFSET (MonoContext, regs) + (ARMREG_PC * 4));
129 ARM_LDR_IMM (code, ARMREG_IP, ctx_reg, G_STRUCT_OFFSET (MonoContext, esp));
130 ARM_STR_IMM (code, ARMREG_IP, ctx_reg, G_STRUCT_OFFSET (MonoContext, regs) + (ARMREG_SP * 4));
132 ARM_LDR_IMM (code, ARMREG_IP, ctx_reg, G_STRUCT_OFFSET (MonoContext, ebp));
133 ARM_STR_IMM (code, ARMREG_IP, ctx_reg, G_STRUCT_OFFSET (MonoContext, regs) + (ARMREG_FP * 4));
135 /* restore everything */
136 ARM_ADD_REG_IMM8 (code, ARMREG_IP, ctx_reg, G_STRUCT_OFFSET(MonoContext, regs));
137 ARM_LDM (code, ARMREG_IP, 0xffff);
142 g_assert ((code - start) < 128);
144 mono_arch_flush_icache (start, code - start);
146 *code_size = code - start;
152 * arch_get_call_filter:
154 * Returns a pointer to a method which calls an exception filter. We
155 * also use this function to call finally handlers (we pass NULL as
156 * @exc object in this case).
159 mono_arch_get_call_filter_full (guint32 *code_size, MonoJumpInfo **ji, gboolean aot)
167 /* call_filter (MonoContext *ctx, unsigned long eip, gpointer exc) */
168 start = code = mono_global_codeman_reserve (320);
170 /* save all the regs on the stack */
171 ARM_MOV_REG_REG (code, ARMREG_IP, ARMREG_SP);
172 ARM_PUSH (code, MONO_ARM_REGSAVE_MASK);
174 /* restore all the regs from ctx (in r0), but not sp, the stack pointer */
176 ARM_LDR_IMM (code, ARMREG_IP, ctx_reg, G_STRUCT_OFFSET (MonoContext, eip));
177 ARM_ADD_REG_IMM8 (code, ARMREG_LR, ctx_reg, G_STRUCT_OFFSET(MonoContext, regs) + (4 * 4));
178 ARM_LDM (code, ARMREG_LR, MONO_ARM_REGSAVE_MASK);
179 /* call handler at eip (r1) and set the first arg with the exception (r2) */
180 ARM_MOV_REG_REG (code, ARMREG_R0, ARMREG_R2);
181 ARM_MOV_REG_REG (code, ARMREG_LR, ARMREG_PC);
182 ARM_MOV_REG_REG (code, ARMREG_PC, ARMREG_R1);
185 ARM_POP_NWB (code, 0xff0 | ((1 << ARMREG_SP) | (1 << ARMREG_PC)));
187 g_assert ((code - start) < 320);
189 mono_arch_flush_icache (start, code - start);
191 *code_size = code - start;
197 mono_arm_throw_exception (MonoObject *exc, unsigned long eip, unsigned long esp, gulong *int_regs, gdouble *fp_regs)
199 static void (*restore_context) (MonoContext *);
201 gboolean rethrow = eip & 1;
203 if (!restore_context)
204 restore_context = mono_get_restore_context ();
206 eip &= ~1; /* clear the optional rethrow bit */
207 /* adjust eip so that it point into the call instruction */
210 /*printf ("stack in throw: %p\n", esp);*/
211 MONO_CONTEXT_SET_BP (&ctx, int_regs [ARMREG_FP - 4]);
212 MONO_CONTEXT_SET_SP (&ctx, esp);
213 MONO_CONTEXT_SET_IP (&ctx, eip);
214 memcpy (((guint8*)&ctx.regs) + (4 * 4), int_regs, sizeof (gulong) * 8);
215 /* memcpy (&ctx.fregs, fp_regs, sizeof (double) * MONO_SAVED_FREGS); */
217 if (mono_object_isinst (exc, mono_defaults.exception_class)) {
218 MonoException *mono_ex = (MonoException*)exc;
220 mono_ex->stack_trace = NULL;
222 mono_handle_exception (&ctx, exc, (gpointer)(eip + 4), FALSE);
223 restore_context (&ctx);
224 g_assert_not_reached ();
228 mono_arm_throw_exception_by_token (guint32 type_token, unsigned long eip, unsigned long esp, gulong *int_regs, gdouble *fp_regs)
230 mono_arm_throw_exception ((MonoObject*)mono_exception_from_token (mono_defaults.corlib, type_token), eip, esp, int_regs, fp_regs);
234 * arch_get_throw_exception_generic:
236 * Returns a function pointer which can be used to raise
237 * exceptions. The returned function has the following
238 * signature: void (*func) (MonoException *exc); or
239 * void (*func) (guint32 ex_token, guint8* ip);
243 mono_arch_get_throw_exception_generic (int size, int by_token, gboolean rethrow, guint32 *code_size, MonoJumpInfo **ji, gboolean aot)
250 code = start = mono_global_codeman_reserve (size);
252 /* save all the regs on the stack */
253 ARM_MOV_REG_REG (code, ARMREG_IP, ARMREG_SP);
254 ARM_PUSH (code, MONO_ARM_REGSAVE_MASK);
256 /* call throw_exception (exc, ip, sp, int_regs, fp_regs) */
258 ARM_ADD_REG_IMM8 (code, ARMREG_R2, ARMREG_SP, 10 * 4); /* 10 saved regs */
259 /* exc is already in place in r0 */
261 /* The caller ip is already in R1 */
263 ARM_MOV_REG_REG (code, ARMREG_R1, ARMREG_LR); /* caller ip */
265 /* FIXME: pointer to the saved fp regs */
266 /*pos = alloc_size - sizeof (double) * MONO_SAVED_FREGS;
267 ppc_addi (code, ppc_r7, ppc_sp, pos);*/
268 /* pointer to the saved int regs */
269 ARM_MOV_REG_REG (code, ARMREG_R3, ARMREG_SP); /* the pushed regs */
270 /* we encode rethrow in the ip, so we avoid args on the stack */
271 ARM_ORR_REG_IMM8 (code, ARMREG_R1, ARMREG_R1, rethrow);
274 *ji = mono_patch_info_list_prepend (*ji, code - start, MONO_PATCH_INFO_JIT_ICALL_ADDR, by_token ? "mono_arm_throw_exception_by_token" : "mono_arm_throw_exception");
275 ARM_LDR_IMM (code, ARMREG_IP, ARMREG_PC, 0);
277 *(gpointer*)(gpointer)code = NULL;
279 ARM_LDR_REG_REG (code, ARMREG_IP, ARMREG_PC, ARMREG_IP);
281 code = mono_arm_emit_load_imm (code, ARMREG_IP, GPOINTER_TO_UINT (by_token ? (gpointer)mono_arm_throw_exception_by_token : (gpointer)mono_arm_throw_exception));
283 ARM_MOV_REG_REG (code, ARMREG_LR, ARMREG_PC);
284 ARM_MOV_REG_REG (code, ARMREG_PC, ARMREG_IP);
285 /* we should never reach this breakpoint */
287 g_assert ((code - start) < size);
288 mono_arch_flush_icache (start, code - start);
290 *code_size = code - start;
296 * mono_arch_get_rethrow_exception:
298 * Returns a function pointer which can be used to rethrow
299 * exceptions. The returned function has the following
300 * signature: void (*func) (MonoException *exc);
304 mono_arch_get_rethrow_exception_full (guint32 *code_size, MonoJumpInfo **ji, gboolean aot)
306 return mono_arch_get_throw_exception_generic (132, FALSE, TRUE, code_size, ji, aot);
310 * arch_get_throw_exception:
312 * Returns a function pointer which can be used to raise
313 * exceptions. The returned function has the following
314 * signature: void (*func) (MonoException *exc);
315 * For example to raise an arithmetic exception you can use:
317 * x86_push_imm (code, mono_get_exception_arithmetic ());
318 * x86_call_code (code, arch_get_throw_exception ());
322 mono_arch_get_throw_exception_full (guint32 *code_size, MonoJumpInfo **ji, gboolean aot)
324 return mono_arch_get_throw_exception_generic (132, FALSE, FALSE, code_size, ji, aot);
328 * mono_arch_get_throw_corlib_exception:
330 * Returns a function pointer which can be used to raise
331 * corlib exceptions. The returned function has the following
332 * signature: void (*func) (guint32 ex_token, guint32 offset);
333 * Here, offset is the offset which needs to be substracted from the caller IP
334 * to get the IP of the throw. Passing the offset has the advantage that it
335 * needs no relocations in the caller.
336 * On ARM, the ip is passed instead of an offset.
339 mono_arch_get_throw_corlib_exception_full (guint32 *code_size, MonoJumpInfo **ji, gboolean aot)
341 return mono_arch_get_throw_exception_generic (168, TRUE, FALSE, code_size, ji, aot);
345 * mono_arch_find_jit_info_ext:
347 * See exceptions-amd64.c for docs;
350 mono_arch_find_jit_info_ext (MonoDomain *domain, MonoJitTlsData *jit_tls,
351 MonoJitInfo *ji, MonoContext *ctx,
352 MonoContext *new_ctx, MonoLMF **lmf,
353 StackFrameInfo *frame)
355 gpointer ip = MONO_CONTEXT_GET_IP (ctx);
357 memset (frame, 0, sizeof (StackFrameInfo));
359 frame->managed = FALSE;
365 gssize regs [MONO_MAX_IREGS + 1];
367 guint32 unwind_info_len;
370 frame->type = FRAME_TYPE_MANAGED;
372 if (!ji->method->wrapper_type || ji->method->wrapper_type == MONO_WRAPPER_DYNAMIC_METHOD)
373 frame->managed = TRUE;
376 unwind_info = mono_aot_get_unwind_info (ji, &unwind_info_len);
378 unwind_info = mono_get_cached_unwind_info (ji->used_regs, &unwind_info_len);
380 for (i = 0; i < 16; ++i)
381 regs [i] = new_ctx->regs [i];
382 regs [ARMREG_SP] = new_ctx->esp;
384 mono_unwind_frame (unwind_info, unwind_info_len, ji->code_start,
385 (guint8*)ji->code_start + ji->code_size,
386 ip, regs, MONO_MAX_IREGS, &cfa);
388 for (i = 0; i < 16; ++i)
389 new_ctx->regs [i] = regs [i];
390 new_ctx->eip = regs [ARMREG_LR];
391 new_ctx->esp = (gsize)cfa;
392 new_ctx->ebp = new_ctx->esp;
394 if (*lmf && (MONO_CONTEXT_GET_BP (ctx) >= (gpointer)(*lmf)->ebp)) {
395 /* remove any unused lmf */
396 *lmf = (gpointer)(((gsize)(*lmf)->previous_lmf) & ~3);
399 /* we substract 1, so that the IP points into the call instruction */
405 if (((gsize)(*lmf)->previous_lmf) & 2) {
407 * This LMF entry is created by the soft debug code to mark transitions to
408 * managed code done during invokes.
410 MonoLMFExt *ext = (MonoLMFExt*)(*lmf);
412 g_assert (ext->debugger_invoke);
414 memcpy (new_ctx, &ext->ctx, sizeof (MonoContext));
416 *lmf = (gpointer)(((gsize)(*lmf)->previous_lmf) & ~3);
418 frame->type = FRAME_TYPE_DEBUGGER_INVOKE;
423 frame->type = FRAME_TYPE_MANAGED_TO_NATIVE;
425 if ((ji = mini_jit_info_table_find (domain, (gpointer)(*lmf)->eip, NULL))) {
430 frame->method = (*lmf)->method;
433 memcpy (&new_ctx->regs [0], &(*lmf)->iregs [0], sizeof (gulong) * 13);
435 new_ctx->regs [ARMREG_LR] = (*lmf)->iregs [ARMREG_LR - 1];
436 /* This is the sp for the current frame */
437 new_ctx->esp = (*lmf)->iregs [ARMREG_FP];
438 new_ctx->eip = (*lmf)->iregs [13];
439 new_ctx->ebp = new_ctx->esp;
441 *lmf = (gpointer)(((gsize)(*lmf)->previous_lmf) & ~3);
450 mono_arch_sigctx_to_monoctx (void *sigctx, MonoContext *mctx)
453 g_assert_not_reached ();
455 my_ucontext *my_uc = sigctx;
457 mctx->eip = UCONTEXT_REG_PC (my_uc);
458 mctx->esp = UCONTEXT_REG_SP (my_uc);
459 memcpy (&mctx->regs, &UCONTEXT_REG_R0 (my_uc), sizeof (gulong) * 16);
461 mctx->ebp = mctx->regs [ARMREG_FP];
465 mono_arch_monoctx_to_sigctx (MonoContext *mctx, void *ctx)
468 g_assert_not_reached ();
470 my_ucontext *my_uc = ctx;
472 UCONTEXT_REG_PC (my_uc) = mctx->eip;
473 UCONTEXT_REG_SP (my_uc) = mctx->ebp;
474 /* The upper registers are not guaranteed to be valid */
475 memcpy (&UCONTEXT_REG_R0 (my_uc), &mctx->regs, sizeof (gulong) * 12);
480 * This is the function called from the signal handler
483 mono_arch_handle_exception (void *ctx, gpointer obj, gboolean test_only)
488 mono_arch_sigctx_to_monoctx (ctx, &mctx);
490 result = mono_handle_exception (&mctx, obj, (gpointer)mctx.eip, test_only);
491 /* restore the context so that returning from the signal handler will invoke
494 mono_arch_monoctx_to_sigctx (&mctx, ctx);
499 mono_arch_ip_from_context (void *sigctx)
502 g_assert_not_reached ();
504 my_ucontext *my_uc = sigctx;
505 return (void*) UCONTEXT_REG_PC (my_uc);
510 mono_arch_has_unwind_info (gconstpointer addr)