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>
28 static gboolean arch_handle_exception (MonoContext *ctx, gpointer obj, gboolean test_only);
33 unsigned long trap_no;
34 unsigned long error_code;
35 unsigned long oldmask;
46 unsigned long arm_r10;
52 unsigned long arm_cpsr;
53 unsigned long fault_address;
56 gregs below is this struct
58 unsigned long int uregs[18];
61 the companion user_fpregs has just 8 double registers
62 (it's valid for FPA mode, will need changes for VFP)
69 typedef struct ucontext {
70 unsigned long int uc_flags;
71 struct ucontext *uc_link;
72 __sigset_t uc_sigmask;
74 mcontext_t uc_mcontext;
75 long int uc_filler[5];
81 * So, it turns out that the ucontext struct defined by libc is incorrect.
82 * We define our own version here and use it instead.
85 typedef struct my_ucontext {
86 unsigned long uc_flags;
87 struct my_ucontext *uc_link;
93 struct sigcontext sig_ctx;
94 /* some 2.6.x kernel has fp data here after a few other fields
95 * we don't use them for now...
99 #define restore_regs_from_context(ctx_reg,ip_reg,tmp_reg) do { \
101 ARM_LDR_IMM (code, ip_reg, ctx_reg, G_STRUCT_OFFSET (MonoContext, eip)); \
102 ARM_ADD_REG_IMM8 (code, tmp_reg, ctx_reg, G_STRUCT_OFFSET(MonoContext, regs)); \
103 ARM_LDMIA (code, tmp_reg, MONO_ARM_REGSAVE_MASK); \
107 #define setup_context(ctx)
110 * arch_get_restore_context:
112 * Returns a pointer to a method which restores a previously saved sigcontext.
113 * The first argument in r0 is the pointer to the context.
116 mono_arch_get_restore_context (void)
119 static guint8 start [128];
120 static int inited = 0;
127 restore_regs_from_context (ARMREG_R0, ARMREG_R1, ARMREG_R2);
128 /* restore also the stack pointer, FIXME: handle sp != fp */
129 ARM_LDR_IMM (code, ARMREG_SP, ARMREG_R0, G_STRUCT_OFFSET (MonoContext, ebp));
130 /* jump to the saved IP */
131 ARM_MOV_REG_REG (code, ARMREG_PC, ARMREG_R1);
135 g_assert ((code - start) < sizeof(start));
136 mono_arch_flush_icache (start, code - start);
141 * arch_get_call_filter:
143 * Returns a pointer to a method which calls an exception filter. We
144 * also use this function to call finally handlers (we pass NULL as
145 * @exc object in this case).
148 mono_arch_get_call_filter (void)
150 static guint8 start [320];
151 static int inited = 0;
153 int alloc_size, pos, i;
159 /* call_filter (MonoContext *ctx, unsigned long eip, gpointer exc) */
162 /* save all the regs on the stack */
163 ARM_MOV_REG_REG (code, ARMREG_IP, ARMREG_SP);
164 ARM_PUSH (code, MONO_ARM_REGSAVE_MASK);
166 /* restore all the regs from ctx (in r0), but not sp, the stack pointer */
167 restore_regs_from_context (ARMREG_R0, ARMREG_IP, ARMREG_LR);
168 /* call handler at eip (r1) and set the first arg with the exception (r2) */
169 ARM_MOV_REG_REG (code, ARMREG_R0, ARMREG_R2);
170 ARM_MOV_REG_REG (code, ARMREG_LR, ARMREG_PC);
171 ARM_MOV_REG_REG (code, ARMREG_PC, ARMREG_R1);
174 ARM_POP_NWB (code, 0xff0 | ((1 << ARMREG_SP) | (1 << ARMREG_PC)));
176 g_assert ((code - start) < sizeof(start));
177 mono_arch_flush_icache (start, code - start);
182 throw_exception (MonoObject *exc, unsigned long eip, unsigned long esp, gulong *int_regs, gdouble *fp_regs)
184 static void (*restore_context) (MonoContext *);
186 gboolean rethrow = eip & 1;
188 if (!restore_context)
189 restore_context = mono_arch_get_restore_context ();
191 eip &= ~1; /* clear the optional rethrow bit */
192 /* adjust eip so that it point into the call instruction */
195 setup_context (&ctx);
197 /*printf ("stack in throw: %p\n", esp);*/
198 MONO_CONTEXT_SET_BP (&ctx, esp);
199 MONO_CONTEXT_SET_IP (&ctx, eip);
200 memcpy (&ctx.regs, int_regs, sizeof (gulong) * 8);
201 /* memcpy (&ctx.fregs, fp_regs, sizeof (double) * MONO_SAVED_FREGS); */
203 if (mono_object_isinst (exc, mono_defaults.exception_class)) {
204 MonoException *mono_ex = (MonoException*)exc;
206 mono_ex->stack_trace = NULL;
208 mono_handle_exception (&ctx, exc, (gpointer)(eip + 4), FALSE);
209 restore_context (&ctx);
210 g_assert_not_reached ();
214 * arch_get_throw_exception_generic:
216 * Returns a function pointer which can be used to raise
217 * exceptions. The returned function has the following
218 * signature: void (*func) (MonoException *exc); or
219 * void (*func) (char *exc_name);
223 mono_arch_get_throw_exception_generic (guint8 *start, int size, int by_name, gboolean rethrow)
226 int alloc_size, pos, i;
230 /* save all the regs on the stack */
231 ARM_MOV_REG_REG (code, ARMREG_IP, ARMREG_SP);
232 ARM_PUSH (code, MONO_ARM_REGSAVE_MASK);
235 /* r0 has the name of the exception: get the object */
236 ARM_MOV_REG_REG (code, ARMREG_R2, ARMREG_R0);
237 code = mono_arm_emit_load_imm (code, ARMREG_R0, GPOINTER_TO_UINT (mono_defaults.corlib));
238 code = mono_arm_emit_load_imm (code, ARMREG_R1, GPOINTER_TO_UINT ("System"));
239 code = mono_arm_emit_load_imm (code, ARMREG_IP, GPOINTER_TO_UINT (mono_exception_from_name));
240 ARM_MOV_REG_REG (code, ARMREG_LR, ARMREG_PC);
241 ARM_MOV_REG_REG (code, ARMREG_PC, ARMREG_IP);
244 /* call throw_exception (exc, ip, sp, int_regs, fp_regs) */
246 ARM_ADD_REG_IMM8 (code, ARMREG_R2, ARMREG_SP, 10 * 4); /* 10 saved regs */
247 /* exc is already in place in r0 */
249 ARM_LDR_IMM (code, ARMREG_R1, ARMREG_SP, 9 * 4); /* pos on the stack were lr was saved */
251 ARM_MOV_REG_REG (code, ARMREG_R1, ARMREG_LR); /* caller ip */
252 /* FIXME: pointer to the saved fp regs */
253 /*pos = alloc_size - sizeof (double) * MONO_SAVED_FREGS;
254 ppc_addi (code, ppc_r7, ppc_sp, pos);*/
255 /* pointer to the saved int regs */
256 ARM_MOV_REG_REG (code, ARMREG_R3, ARMREG_SP); /* the pushed regs */
257 /* we encode rethrow in the ip, so we avoid args on the stack */
258 ARM_ORR_REG_IMM8 (code, ARMREG_R1, ARMREG_R1, rethrow);
260 code = mono_arm_emit_load_imm (code, ARMREG_IP, GPOINTER_TO_UINT (throw_exception));
261 ARM_MOV_REG_REG (code, ARMREG_LR, ARMREG_PC);
262 ARM_MOV_REG_REG (code, ARMREG_PC, ARMREG_IP);
263 /* we should never reach this breakpoint */
265 g_assert ((code - start) < size);
266 mono_arch_flush_icache (start, code - start);
271 * mono_arch_get_rethrow_exception:
273 * Returns a function pointer which can be used to rethrow
274 * exceptions. The returned function has the following
275 * signature: void (*func) (MonoException *exc);
279 mono_arch_get_rethrow_exception (void)
281 static guint8 start [132];
282 static int inited = 0;
286 mono_arch_get_throw_exception_generic (start, sizeof (start), FALSE, TRUE);
291 * arch_get_throw_exception:
293 * Returns a function pointer which can be used to raise
294 * exceptions. The returned function has the following
295 * signature: void (*func) (MonoException *exc);
296 * For example to raise an arithmetic exception you can use:
298 * x86_push_imm (code, mono_get_exception_arithmetic ());
299 * x86_call_code (code, arch_get_throw_exception ());
303 mono_arch_get_throw_exception (void)
305 static guint8 start [132];
306 static int inited = 0;
310 mono_arch_get_throw_exception_generic (start, sizeof (start), FALSE, FALSE);
316 * arch_get_throw_exception_by_name:
318 * Returns a function pointer which can be used to raise
319 * corlib exceptions. The returned function has the following
320 * signature: void (*func) (char *exc_name);
321 * For example to raise an arithmetic exception you can use:
323 * x86_push_imm (code, "ArithmeticException");
324 * x86_call_code (code, arch_get_throw_exception_by_name ());
328 mono_arch_get_throw_exception_by_name (void)
330 static guint8 start [168];
331 static int inited = 0;
335 mono_arch_get_throw_exception_generic (start, sizeof (start), TRUE, FALSE);
341 glist_to_array (GList *list, MonoClass *eclass)
343 MonoDomain *domain = mono_domain_get ();
350 len = g_list_length (list);
351 res = mono_array_new (domain, eclass, len);
353 for (i = 0; list; list = list->next, i++)
354 mono_array_set (res, gpointer, i, list->data);
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, char **trace, MonoLMF **lmf,
371 int *native_offset, gboolean *managed)
374 gpointer ip = MONO_CONTEXT_GET_IP (ctx);
376 /* Avoid costly table lookup during stack overflow */
377 if (prev_ji && (ip > prev_ji->code_start && ((guint8*)ip < ((guint8*)prev_ji->code_start) + prev_ji->code_size)))
380 ji = mono_jit_info_table_find (domain, ip);
391 if (!ji->method->wrapper_type)
395 * Some managed methods like pinvoke wrappers might have save_lmf set.
396 * In this case, register save/restore code is not generated by the
397 * JIT, so we have to restore callee saved registers from the lmf.
399 if (ji->method->save_lmf) {
401 * We only need to do this if the exception was raised in managed
402 * code, since otherwise the lmf was already popped of the stack.
404 if (*lmf && (MONO_CONTEXT_GET_BP (ctx) >= (gpointer)(*lmf)->ebp)) {
405 memcpy (&new_ctx->regs [0], &(*lmf)->iregs [4], sizeof (gulong) * MONO_SAVED_GREGS);
407 new_ctx->esp = (*lmf)->iregs [12];
408 new_ctx->eip = (*lmf)->iregs [13];
409 new_ctx->ebp = new_ctx->esp;
413 offset = ji->used_regs >> 16;
415 /* the saved regs are at sp + offset */
416 /* restore caller saved registers */
417 sp = (char*)ctx->ebp;
419 for (i = 4; i < 16; ++i) {
420 if (ji->used_regs & (1 << i)) {
421 new_ctx->regs [i - 4] = *(gulong*)sp;
422 sp += sizeof (gulong);
426 new_ctx->esp = *(gulong*)sp;
427 sp += sizeof (gulong);
428 new_ctx->eip = *(gulong*)sp;
429 sp += sizeof (gulong);
430 new_ctx->ebp = new_ctx->esp;
433 if (*lmf && (MONO_CONTEXT_GET_BP (ctx) >= (gpointer)(*lmf)->ebp)) {
434 /* remove any unused lmf */
435 *lmf = (*lmf)->previous_lmf;
438 /* we substract 1, so that the IP points into the call instruction */
449 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 * This is the function called from the signal handler
472 mono_arch_handle_exception (void *ctx, gpointer obj, gboolean test_only)
475 struct ucontext *uc = ctx;
477 my_ucontext *my_uc = ctx;
483 mctx.eip = uc->uc_mcontext.gregs [ARMREG_PC];
484 mctx.ebp = uc->uc_mcontext.gregs [ARMREG_SP];
485 memcpy (&mctx.regs, &uc->uc_mcontext.gregs [ARMREG_R4], sizeof (gulong) * 8);
486 /* memcpy (&mctx.fregs, &uc->uc_mcontext.uc_regs->fpregs.fpregs [14], sizeof (double) * MONO_SAVED_FREGS);*/
488 mctx.eip = my_uc->sig_ctx.arm_pc;
489 mctx.ebp = my_uc->sig_ctx.arm_sp;
490 memcpy (&mctx.regs, &my_uc->sig_ctx.arm_r4, sizeof (gulong) * 8);
493 result = mono_handle_exception (&mctx, obj, (gpointer)mctx.eip, test_only);
494 /* restore the context so that returning from the signal handler will invoke
498 uc->uc_mcontext.gregs [ARMREG_PC] = mctx.eip;
499 uc->uc_mcontext.gregs [ARMREG_SP] = mctx.ebp;
500 memcpy (&uc->uc_mcontext.gregs [ARMREG_R4], &mctx.regs, sizeof (gulong) * 8);
501 /* memcpy (&uc->uc_mcontext.uc_regs->fpregs.fpregs [14], &mctx.fregs, sizeof (double) * MONO_SAVED_FREGS);*/
503 my_uc->sig_ctx.arm_pc = mctx.eip;
504 my_uc->sig_ctx.arm_sp = mctx.ebp;
505 memcpy (&my_uc->sig_ctx.arm_r4, &mctx.regs, sizeof (gulong) * 8);
511 mono_arch_ip_from_context (void *sigctx)
514 struct ucontext *uc = sigctx;
515 return (gpointer)uc->uc_mcontext.gregs [ARMREG_PC];
517 my_ucontext *my_uc = sigctx;
518 return (void*)my_uc->sig_ctx.arm_pc;
523 mono_arch_has_unwind_info (gconstpointer addr)