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...
101 #define restore_regs_from_context(ctx_reg,ip_reg,tmp_reg) do { \
102 ARM_LDR_IMM (code, ip_reg, ctx_reg, G_STRUCT_OFFSET (MonoContext, eip)); \
103 ARM_ADD_REG_IMM8 (code, tmp_reg, ctx_reg, G_STRUCT_OFFSET(MonoContext, regs)); \
104 ARM_LDMIA (code, tmp_reg, MONO_ARM_REGSAVE_MASK); \
108 #define setup_context(ctx)
111 * arch_get_restore_context:
113 * Returns a pointer to a method which restores a previously saved sigcontext.
114 * The first argument in r0 is the pointer to the context.
117 mono_arch_get_restore_context_full (guint32 *code_size, MonoJumpInfo **ji, gboolean aot)
124 start = code = mono_global_codeman_reserve (128);
126 restore_regs_from_context (ARMREG_R0, ARMREG_R1, ARMREG_R2);
127 /* restore also the stack pointer, FIXME: handle sp != fp */
128 ARM_LDR_IMM (code, ARMREG_SP, ARMREG_R0, G_STRUCT_OFFSET (MonoContext, ebp));
129 ARM_LDR_IMM (code, ARMREG_FP, ARMREG_R0, G_STRUCT_OFFSET (MonoContext, esp));
131 /* jump to the saved IP */
132 ARM_MOV_REG_REG (code, ARMREG_PC, ARMREG_R1);
136 g_assert ((code - start) < 128);
138 mono_arch_flush_icache (start, code - start);
140 *code_size = code - start;
146 * arch_get_call_filter:
148 * Returns a pointer to a method which calls an exception filter. We
149 * also use this function to call finally handlers (we pass NULL as
150 * @exc object in this case).
153 mono_arch_get_call_filter_full (guint32 *code_size, MonoJumpInfo **ji, gboolean aot)
160 /* call_filter (MonoContext *ctx, unsigned long eip, gpointer exc) */
161 start = code = mono_global_codeman_reserve (320);
163 /* save all the regs on the stack */
164 ARM_MOV_REG_REG (code, ARMREG_IP, ARMREG_SP);
165 ARM_PUSH (code, MONO_ARM_REGSAVE_MASK);
167 /* restore all the regs from ctx (in r0), but not sp, the stack pointer */
168 restore_regs_from_context (ARMREG_R0, ARMREG_IP, ARMREG_LR);
169 /* call handler at eip (r1) and set the first arg with the exception (r2) */
170 ARM_MOV_REG_REG (code, ARMREG_R0, ARMREG_R2);
171 ARM_MOV_REG_REG (code, ARMREG_LR, ARMREG_PC);
172 ARM_MOV_REG_REG (code, ARMREG_PC, ARMREG_R1);
175 ARM_POP_NWB (code, 0xff0 | ((1 << ARMREG_SP) | (1 << ARMREG_PC)));
177 g_assert ((code - start) < 320);
179 mono_arch_flush_icache (start, code - start);
181 *code_size = code - start;
187 mono_arm_throw_exception (MonoObject *exc, unsigned long eip, unsigned long esp, gulong *int_regs, gdouble *fp_regs)
189 static void (*restore_context) (MonoContext *);
191 gboolean rethrow = eip & 1;
193 if (!restore_context)
194 restore_context = mono_get_restore_context ();
196 eip &= ~1; /* clear the optional rethrow bit */
197 /* adjust eip so that it point into the call instruction */
200 setup_context (&ctx);
202 /*printf ("stack in throw: %p\n", esp);*/
203 MONO_CONTEXT_SET_BP (&ctx, esp);
204 MONO_CONTEXT_SET_SP (&ctx, esp);
205 MONO_CONTEXT_SET_IP (&ctx, eip);
206 memcpy (&ctx.regs, int_regs, sizeof (gulong) * 8);
207 /* memcpy (&ctx.fregs, fp_regs, sizeof (double) * MONO_SAVED_FREGS); */
209 if (mono_object_isinst (exc, mono_defaults.exception_class)) {
210 MonoException *mono_ex = (MonoException*)exc;
212 mono_ex->stack_trace = NULL;
214 mono_handle_exception (&ctx, exc, (gpointer)(eip + 4), FALSE);
215 restore_context (&ctx);
216 g_assert_not_reached ();
220 * arch_get_throw_exception_generic:
222 * Returns a function pointer which can be used to raise
223 * exceptions. The returned function has the following
224 * signature: void (*func) (MonoException *exc); or
225 * void (*func) (guint32 ex_token, guint8* ip);
229 mono_arch_get_throw_exception_generic (int size, int by_token, gboolean rethrow, guint32 *code_size, MonoJumpInfo **ji, gboolean aot)
236 code = start = mono_global_codeman_reserve (size);
238 /* save all the regs on the stack */
239 ARM_MOV_REG_REG (code, ARMREG_IP, ARMREG_SP);
240 ARM_PUSH (code, MONO_ARM_REGSAVE_MASK);
243 /* r0 has the type token of the exception: get the object */
244 ARM_PUSH1 (code, ARMREG_R1);
245 ARM_MOV_REG_REG (code, ARMREG_R1, ARMREG_R0);
246 code = mono_arm_emit_load_imm (code, ARMREG_R0, GPOINTER_TO_UINT (mono_defaults.corlib));
247 code = mono_arm_emit_load_imm (code, ARMREG_IP, GPOINTER_TO_UINT (mono_exception_from_token));
248 ARM_MOV_REG_REG (code, ARMREG_LR, ARMREG_PC);
249 ARM_MOV_REG_REG (code, ARMREG_PC, ARMREG_IP);
250 ARM_POP1 (code, ARMREG_R1);
253 /* call throw_exception (exc, ip, sp, int_regs, fp_regs) */
255 ARM_ADD_REG_IMM8 (code, ARMREG_R2, ARMREG_SP, 10 * 4); /* 10 saved regs */
256 /* exc is already in place in r0 */
258 /* The caller ip is already in R1 */
260 ARM_MOV_REG_REG (code, ARMREG_R1, ARMREG_LR); /* caller ip */
262 /* FIXME: pointer to the saved fp regs */
263 /*pos = alloc_size - sizeof (double) * MONO_SAVED_FREGS;
264 ppc_addi (code, ppc_r7, ppc_sp, pos);*/
265 /* pointer to the saved int regs */
266 ARM_MOV_REG_REG (code, ARMREG_R3, ARMREG_SP); /* the pushed regs */
267 /* we encode rethrow in the ip, so we avoid args on the stack */
268 ARM_ORR_REG_IMM8 (code, ARMREG_R1, ARMREG_R1, rethrow);
271 *ji = mono_patch_info_list_prepend (*ji, code - start, MONO_PATCH_INFO_JIT_ICALL_ADDR, "mono_arm_throw_exception");
272 ARM_LDR_IMM (code, ARMREG_IP, ARMREG_PC, 0);
274 *(gpointer*)(gpointer)code = NULL;
276 ARM_LDR_REG_REG (code, ARMREG_IP, ARMREG_PC, ARMREG_IP);
278 code = mono_arm_emit_load_imm (code, ARMREG_IP, GPOINTER_TO_UINT (mono_arm_throw_exception));
280 ARM_MOV_REG_REG (code, ARMREG_LR, ARMREG_PC);
281 ARM_MOV_REG_REG (code, ARMREG_PC, ARMREG_IP);
282 /* we should never reach this breakpoint */
284 g_assert ((code - start) < size);
285 mono_arch_flush_icache (start, code - start);
287 *code_size = code - start;
293 * mono_arch_get_rethrow_exception:
295 * Returns a function pointer which can be used to rethrow
296 * exceptions. The returned function has the following
297 * signature: void (*func) (MonoException *exc);
301 mono_arch_get_rethrow_exception_full (guint32 *code_size, MonoJumpInfo **ji, gboolean aot)
303 return mono_arch_get_throw_exception_generic (132, FALSE, TRUE, code_size, ji, aot);
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_full (guint32 *code_size, MonoJumpInfo **ji, gboolean aot)
321 return mono_arch_get_throw_exception_generic (132, FALSE, FALSE, code_size, ji, aot);
325 mono_arch_get_throw_exception_by_name_full (guint32 *code_size, MonoJumpInfo **ji, gboolean aot)
332 start = code = mono_global_codeman_reserve (64);
334 /* Not used on ARM */
337 *code_size = code - start;
343 * mono_arch_get_throw_corlib_exception:
345 * Returns a function pointer which can be used to raise
346 * corlib exceptions. The returned function has the following
347 * signature: void (*func) (guint32 ex_token, guint32 offset);
348 * Here, offset is the offset which needs to be substracted from the caller IP
349 * to get the IP of the throw. Passing the offset has the advantage that it
350 * needs no relocations in the caller.
351 * On ARM, the ip is passed instead of an offset.
354 mono_arch_get_throw_corlib_exception_full (guint32 *code_size, MonoJumpInfo **ji, gboolean aot)
356 return mono_arch_get_throw_exception_generic (168, TRUE, FALSE, code_size, ji, aot);
359 /* mono_arch_find_jit_info:
361 * This function is used to gather information from @ctx. It return the
362 * MonoJitInfo of the corresponding function, unwinds one stack frame and
363 * stores the resulting context into @new_ctx. It also stores a string
364 * describing the stack location into @trace (if not NULL), and modifies
365 * the @lmf if necessary. @native_offset return the IP offset from the
366 * start of the function or -1 if that info is not available.
369 mono_arch_find_jit_info (MonoDomain *domain, MonoJitTlsData *jit_tls, MonoJitInfo *res, MonoJitInfo *prev_ji,
370 MonoContext *ctx, MonoContext *new_ctx, MonoLMF **lmf, gboolean *managed)
373 gpointer ip = MONO_CONTEXT_GET_IP (ctx);
375 /* Avoid costly table lookup during stack overflow */
376 if (prev_ji && (ip > prev_ji->code_start && ((guint8*)ip < ((guint8*)prev_ji->code_start) + prev_ji->code_size)))
379 ji = mono_jit_info_table_find (domain, ip);
390 if (!ji->method->wrapper_type)
394 * Some managed methods like pinvoke wrappers might have save_lmf set.
395 * In this case, register save/restore code is not generated by the
396 * JIT, so we have to restore callee saved registers from the lmf.
398 if (ji->method->save_lmf) {
400 * We only need to do this if the exception was raised in managed
401 * code, since otherwise the lmf was already popped of the stack.
403 if (*lmf && (MONO_CONTEXT_GET_BP (ctx) >= (gpointer)(*lmf)->ebp)) {
404 memcpy (&new_ctx->regs [0], &(*lmf)->iregs [4], sizeof (gulong) * MONO_SAVED_GREGS);
406 new_ctx->esp = (*lmf)->iregs [12];
407 new_ctx->eip = (*lmf)->iregs [13];
408 new_ctx->ebp = new_ctx->esp;
412 offset = ji->used_regs >> 16;
414 /* the saved regs are at sp + offset */
415 /* restore caller saved registers */
416 sp = (char*)ctx->ebp;
418 for (i = 4; i < 16; ++i) {
419 if (ji->used_regs & (1 << i)) {
420 new_ctx->regs [i - 4] = *(gulong*)sp;
421 sp += sizeof (gulong);
425 new_ctx->esp = *(gulong*)sp;
426 sp += sizeof (gulong);
427 new_ctx->eip = *(gulong*)sp;
428 sp += sizeof (gulong);
429 new_ctx->ebp = new_ctx->esp;
430 /* Needed by get_exception_catch_class () */
431 new_ctx->regs [ARMREG_R11] = new_ctx->esp;
434 if (*lmf && (MONO_CONTEXT_GET_BP (ctx) >= (gpointer)(*lmf)->ebp)) {
435 /* remove any unused lmf */
436 *lmf = (*lmf)->previous_lmf;
439 /* we substract 1, so that the IP points into the call instruction */
447 if ((ji = mono_jit_info_table_find (domain, (gpointer)(*lmf)->eip))) {
451 memset (res, 0, sizeof (MonoJitInfo));
452 res->method = (*lmf)->method;
455 memcpy (&new_ctx->regs [0], &(*lmf)->iregs [4], sizeof (gulong) * MONO_SAVED_GREGS);
456 new_ctx->esp = (*lmf)->iregs [12];
457 new_ctx->eip = (*lmf)->iregs [13];
458 new_ctx->ebp = new_ctx->esp;
460 *lmf = (*lmf)->previous_lmf;
462 return ji ? ji : res;
469 mono_arch_sigctx_to_monoctx (void *sigctx, MonoContext *mctx)
472 struct ucontext *uc = sigctx;
474 mctx->eip = uc->uc_mcontext.gregs [ARMREG_PC];
475 mctx->ebp = uc->uc_mcontext.gregs [ARMREG_SP];
476 memcpy (&mctx->regs, &uc->uc_mcontext.gregs [ARMREG_R4], sizeof (gulong) * 8);
477 /* memcpy (&mctx->fregs, &uc->uc_mcontext.uc_regs->fpregs.fpregs [14], sizeof (double) * MONO_SAVED_FREGS);*/
479 my_ucontext *my_uc = sigctx;
481 mctx->eip = UCONTEXT_REG_PC (my_uc);
482 mctx->ebp = UCONTEXT_REG_SP (my_uc);
483 memcpy (&mctx->regs, &UCONTEXT_REG_R4 (my_uc), sizeof (gulong) * 8);
488 mono_arch_monoctx_to_sigctx (MonoContext *mctx, void *ctx)
491 struct ucontext *uc = ctx;
493 uc->uc_mcontext.gregs [ARMREG_PC] = mctx->eip;
494 uc->uc_mcontext.gregs [ARMREG_SP] = mctx->ebp;
495 memcpy (&uc->uc_mcontext.gregs [ARMREG_R4], &mctx->regs, sizeof (gulong) * 8);
496 /* memcpy (&uc->uc_mcontext.uc_regs->fpregs.fpregs [14], &mctx->fregs, sizeof (double) * MONO_SAVED_FREGS);*/
498 my_ucontext *my_uc = ctx;
500 UCONTEXT_REG_PC (my_uc) = mctx->eip;
501 UCONTEXT_REG_SP (my_uc) = mctx->ebp;
502 memcpy (&UCONTEXT_REG_R4 (my_uc), &mctx->regs, sizeof (gulong) * 8);
507 * This is the function called from the signal handler
510 mono_arch_handle_exception (void *ctx, gpointer obj, gboolean test_only)
515 mono_arch_sigctx_to_monoctx (ctx, &mctx);
517 result = mono_handle_exception (&mctx, obj, (gpointer)mctx.eip, test_only);
518 /* restore the context so that returning from the signal handler will invoke
521 mono_arch_monoctx_to_sigctx (&mctx, ctx);
526 mono_arch_ip_from_context (void *sigctx)
529 struct ucontext *uc = sigctx;
530 return (gpointer)uc->uc_mcontext.gregs [ARMREG_PC];
532 my_ucontext *my_uc = sigctx;
533 return (void*) UCONTEXT_REG_PC (my_uc);
538 mono_arch_has_unwind_info (gconstpointer addr)