2 * exceptions-ppc.c: exception support for PowerPC
5 * Dietmar Maurer (dietmar@ximian.com)
6 * Paolo Molaro (lupus@ximian.com)
8 * (C) 2001 Ximian, Inc.
17 #include <mono/arch/ppc/ppc-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 int sc_onstack; // sigstack state to restore
34 int sc_mask; // signal mask to restore
36 int sc_psw; // processor status word
37 int sc_sp; // stack pointer if sc_regs == NULL
38 void *sc_regs; // (kernel private) saved state
43 sigset_t uc_sigmask; // signal mask used by this context
44 stack_t uc_stack; // stack used by this context
45 struct ucontext *uc_link; // pointer to resuming context
46 size_t uc_mcsize; // size of the machine context passed in
47 mcontext_t uc_mcontext; // machine specific context
50 typedef struct ppc_exception_state {
51 unsigned long dar; // Fault registers for coredump
53 unsigned long exception;// number of powerpc exception taken
54 unsigned long pad0; // align to 16 bytes
56 unsigned long pad1[4]; // space in PCB "just in case"
57 } ppc_exception_state_t;
59 typedef struct ppc_vector_state {
60 unsigned long save_vr[32][4];
61 unsigned long save_vscr[4];
62 unsigned int save_pad5[4];
63 unsigned int save_vrvalid; // VRs that have been saved
64 unsigned int save_pad6[7];
67 typedef struct ppc_float_state {
70 unsigned int fpscr_pad; // fpscr is 64 bits, 32 bits of rubbish
71 unsigned int fpscr; // floating point status register
74 typedef struct ppc_thread_state {
75 unsigned int srr0; // Instruction address register (PC)
76 unsigned int srr1; // Machine state register (supervisor)
82 unsigned int cr; // Condition register
83 unsigned int xer; // User's integer exception register
84 unsigned int lr; // Link register
85 unsigned int ctr; // Count register
86 unsigned int mq; // MQ register (601 only)
88 unsigned int vrsave; // Vector Save Register
92 ppc_exception_state_t es;
93 ppc_thread_state_t ss;
95 ppc_vector_state_t vs;
98 typedef struct mcontext * mcontext_t;
100 Linux/PPC instead has:
102 unsigned long _unused[4];
104 unsigned long handler;
105 unsigned long oldmask;
106 struct pt_regs *regs;
109 unsigned long gpr[32];
112 unsigned long orig_gpr3; // Used for restarting system calls
117 unsigned long mq; // 601 only (not used at present)
118 // Used on APUS to hold IPL value.
119 unsigned long trap; // Reason for being here
120 // N.B. for critical exceptions on 4xx, the dar and dsisr
121 // fields are overloaded to hold srr0 and srr1.
122 unsigned long dar; // Fault registers
123 unsigned long dsisr; // on 4xx/Book-E used for ESR
124 unsigned long result; // Result of a system call
127 elf_gregset_t mc_gregs;
128 elf_fpregset_t mc_fregs;
129 unsigned long mc_pad[2];
130 elf_vrregset_t mc_vregs __attribute__((__aligned__(16)));
134 unsigned long uc_flags;
135 struct ucontext *uc_link;
138 struct mcontext *uc_regs; // points to uc_mcontext field
140 // glibc has 1024-bit signal masks, ours are 64-bit
143 struct mcontext uc_mcontext;
146 #define ELF_NGREG 48 // includes nip, msr, lr, etc.
147 #define ELF_NFPREG 33 // includes fpscr
150 typedef unsigned long elf_greg_t;
151 typedef elf_greg_t elf_gregset_t[ELF_NGREG];
153 // Floating point registers
154 typedef double elf_fpreg_t;
155 typedef elf_fpreg_t elf_fpregset_t[ELF_NFPREG];
161 #define restore_regs_from_context(ctx_reg,ip_reg,tmp_reg) do { \
163 ppc_lwz (code, ip_reg, G_STRUCT_OFFSET (MonoContext, sc_ir), ctx_reg); \
164 ppc_lmw (code, ppc_r13, ctx_reg, G_STRUCT_OFFSET (MonoContext, regs)); \
165 for (reg = 0; reg < MONO_SAVED_FREGS; ++reg) { \
166 ppc_lfd (code, (14 + reg), G_STRUCT_OFFSET(MonoLMF, fregs) + reg * sizeof (gdouble), ctx_reg); \
171 #define setup_context(ctx)
174 * arch_get_restore_context:
176 * Returns a pointer to a method which restores a previously saved sigcontext.
177 * The first argument in r3 is the pointer to the context.
180 arch_get_restore_context (void)
183 static guint8 start [128];
184 static int inited = 0;
191 restore_regs_from_context (ppc_r3, ppc_r4, ppc_r5);
192 /* restore also the stack pointer */
193 ppc_lwz (code, ppc_sp, G_STRUCT_OFFSET (MonoContext, sc_sp), ppc_r3);
195 /* jump to the saved IP */
196 ppc_mtctr (code, ppc_r4);
197 ppc_bcctr (code, PPC_BR_ALWAYS, 0);
201 g_assert ((code - start) < sizeof(start));
202 mono_arch_flush_icache (start, code - start);
207 * arch_get_call_filter:
209 * Returns a pointer to a method which calls an exception filter. We
210 * also use this function to call finally handlers (we pass NULL as
211 * @exc object in this case).
214 arch_get_call_filter (void)
216 static guint8 start [320];
217 static int inited = 0;
219 int alloc_size, pos, i;
225 /* call_filter (MonoContext *ctx, unsigned long eip, gpointer exc) */
228 /* save all the regs on the stack */
230 for (i = 31; i >= 14; --i) {
231 pos += sizeof (gdouble);
232 ppc_stfd (code, i, -pos, ppc_sp);
234 pos += sizeof (gulong) * MONO_SAVED_GREGS;
235 ppc_stmw (code, ppc_r13, ppc_sp, -pos);
237 ppc_mflr (code, ppc_r0);
238 ppc_stw (code, ppc_r0, PPC_RET_ADDR_OFFSET, ppc_sp);
240 alloc_size = PPC_MINIMAL_STACK_SIZE + pos + 64;
241 // align to PPC_STACK_ALIGNMENT bytes
242 alloc_size += PPC_STACK_ALIGNMENT - 1;
243 alloc_size &= ~(PPC_STACK_ALIGNMENT - 1);
245 g_assert ((alloc_size & (PPC_STACK_ALIGNMENT-1)) == 0);
246 ppc_stwu (code, ppc_sp, -alloc_size, ppc_sp);
248 /* restore all the regs from ctx (in r3), but not r1, the stack pointer */
249 restore_regs_from_context (ppc_r3, ppc_r6, ppc_r7);
250 /* call handler at eip (r4) and set the first arg with the exception (r5) */
251 ppc_mtctr (code, ppc_r4);
252 ppc_mr (code, ppc_r3, ppc_r5);
253 ppc_bcctrl (code, PPC_BR_ALWAYS, 0);
256 ppc_lwz (code, ppc_r0, alloc_size + PPC_RET_ADDR_OFFSET, ppc_sp);
257 ppc_mtlr (code, ppc_r0);
258 ppc_addic (code, ppc_sp, ppc_sp, alloc_size);
260 /* restore all the regs from the stack */
262 for (i = 31; i >= 14; --i) {
263 pos += sizeof (double);
264 ppc_lfd (code, i, -pos, ppc_sp);
266 pos += sizeof (gulong) * MONO_SAVED_GREGS;
267 ppc_lmw (code, ppc_r13, ppc_sp, -pos);
271 g_assert ((code - start) < sizeof(start));
272 mono_arch_flush_icache (start, code - start);
277 throw_exception (MonoObject *exc, unsigned long eip, unsigned long esp, gulong *int_regs, gdouble *fp_regs, gboolean rethrow)
279 static void (*restore_context) (MonoContext *);
282 if (!restore_context)
283 restore_context = arch_get_restore_context ();
285 /* adjust eip so that it point into the call instruction */
288 setup_context (&ctx);
290 /*printf ("stack in throw: %p\n", esp);*/
291 MONO_CONTEXT_SET_BP (&ctx, esp);
292 MONO_CONTEXT_SET_IP (&ctx, eip);
293 memcpy (&ctx.regs, int_regs, sizeof (gulong) * MONO_SAVED_GREGS);
294 memcpy (&ctx.fregs, fp_regs, sizeof (double) * MONO_SAVED_FREGS);
296 if (mono_object_isinst (exc, mono_defaults.exception_class)) {
297 MonoException *mono_ex = (MonoException*)exc;
299 mono_ex->stack_trace = NULL;
301 arch_handle_exception (&ctx, exc, FALSE);
302 restore_context (&ctx);
304 g_assert_not_reached ();
308 * arch_get_throw_exception_generic:
310 * Returns a function pointer which can be used to raise
311 * exceptions. The returned function has the following
312 * signature: void (*func) (MonoException *exc); or
313 * void (*func) (char *exc_name);
317 mono_arch_get_throw_exception_generic (guint8 *start, int size, int by_name, gboolean rethrow)
320 int alloc_size, pos, i;
324 /* save all the regs on the stack */
326 for (i = 31; i >= 14; --i) {
327 pos += sizeof (gdouble);
328 ppc_stfd (code, i, -pos, ppc_sp);
330 pos += sizeof (gulong) * MONO_SAVED_GREGS;
331 ppc_stmw (code, ppc_r13, ppc_sp, -pos);
333 ppc_mflr (code, ppc_r0);
334 ppc_stw (code, ppc_r0, PPC_RET_ADDR_OFFSET, ppc_sp);
336 alloc_size = PPC_MINIMAL_STACK_SIZE + pos + 64;
337 // align to PPC_STACK_ALIGNMENT bytes
338 alloc_size += PPC_STACK_ALIGNMENT - 1;
339 alloc_size &= ~(PPC_STACK_ALIGNMENT - 1);
341 g_assert ((alloc_size & (PPC_STACK_ALIGNMENT-1)) == 0);
342 ppc_stwu (code, ppc_sp, -alloc_size, ppc_sp);
346 ppc_mr (code, ppc_r5, ppc_r3);
347 ppc_load (code, ppc_r3, mono_defaults.corlib);
348 ppc_load (code, ppc_r4, "System");
349 ppc_load (code, ppc_r0, mono_exception_from_name);
350 ppc_mtctr (code, ppc_r0);
351 ppc_bcctrl (code, PPC_BR_ALWAYS, 0);
354 /* call throw_exception (exc, ip, sp, int_regs, fp_regs) */
356 ppc_lwz (code, ppc_r5, 0, ppc_sp);
357 /* exc is already in place in r3 */
359 ppc_lwz (code, ppc_r4, PPC_RET_ADDR_OFFSET, ppc_r5);
361 ppc_mr (code, ppc_r4, ppc_r0); /* caller ip */
362 /* pointer to the saved fp regs */
363 pos = alloc_size - sizeof (double) * MONO_SAVED_FREGS;
364 ppc_addi (code, ppc_r7, ppc_sp, pos);
365 /* pointer to the saved int regs */
366 pos -= sizeof (gulong) * MONO_SAVED_GREGS;
367 ppc_addi (code, ppc_r6, ppc_sp, pos);
368 ppc_li (code, ppc_r8, rethrow);
370 ppc_load (code, ppc_r0, throw_exception);
371 ppc_mtctr (code, ppc_r0);
372 ppc_bcctrl (code, PPC_BR_ALWAYS, 0);
373 /* we should never reach this breakpoint */
375 g_assert ((code - start) < size);
376 mono_arch_flush_icache (start, code - start);
381 * mono_arch_get_rethrow_exception:
383 * Returns a function pointer which can be used to rethrow
384 * exceptions. The returned function has the following
385 * signature: void (*func) (MonoException *exc);
389 mono_arch_get_rethrow_exception (void)
391 static guint8 start [132];
392 static int inited = 0;
396 mono_arch_get_throw_exception_generic (start, sizeof (start), FALSE, TRUE);
401 * arch_get_throw_exception:
403 * Returns a function pointer which can be used to raise
404 * exceptions. The returned function has the following
405 * signature: void (*func) (MonoException *exc);
406 * For example to raise an arithmetic exception you can use:
408 * x86_push_imm (code, mono_get_exception_arithmetic ());
409 * x86_call_code (code, arch_get_throw_exception ());
413 mono_arch_get_throw_exception (void)
415 static guint8 start [132];
416 static int inited = 0;
420 mono_arch_get_throw_exception_generic (start, sizeof (start), FALSE, FALSE);
426 * arch_get_throw_exception_by_name:
428 * Returns a function pointer which can be used to raise
429 * corlib exceptions. The returned function has the following
430 * signature: void (*func) (char *exc_name);
431 * For example to raise an arithmetic exception you can use:
433 * x86_push_imm (code, "ArithmeticException");
434 * x86_call_code (code, arch_get_throw_exception_by_name ());
438 mono_arch_get_throw_exception_by_name (void)
440 static guint8 start [168];
441 static int inited = 0;
445 mono_arch_get_throw_exception_generic (start, sizeof (start), TRUE, FALSE);
451 glist_to_array (GList *list, MonoClass *eclass)
453 MonoDomain *domain = mono_domain_get ();
460 len = g_list_length (list);
461 res = mono_array_new (domain, eclass, len);
463 for (i = 0; list; list = list->next, i++)
464 mono_array_set (res, gpointer, i, list->data);
469 /* mono_arch_find_jit_info:
471 * This function is used to gather information from @ctx. It return the
472 * MonoJitInfo of the corresponding function, unwinds one stack frame and
473 * stores the resulting context into @new_ctx. It also stores a string
474 * describing the stack location into @trace (if not NULL), and modifies
475 * the @lmf if necessary. @native_offset return the IP offset from the
476 * start of the function or -1 if that info is not available.
479 mono_arch_find_jit_info (MonoDomain *domain, MonoJitTlsData *jit_tls, MonoJitInfo *res, MonoJitInfo *prev_ji,
480 MonoContext *ctx, MonoContext *new_ctx, char **trace, MonoLMF **lmf,
481 int *native_offset, gboolean *managed)
484 gpointer ip = MONO_CONTEXT_GET_IP (ctx);
487 MonoPPCStackFrame *sframe;
489 /* Avoid costly table lookup during stack overflow */
490 if (prev_ji && (ip > prev_ji->code_start && ((guint8*)ip < ((guint8*)prev_ji->code_start) + prev_ji->code_size)))
493 ji = mono_jit_info_table_find (domain, ip);
510 setup_context (new_ctx);
512 if (*lmf && (MONO_CONTEXT_GET_BP (ctx) >= (gpointer)(*lmf)->ebp)) {
513 /* remove any unused lmf */
514 *lmf = (*lmf)->previous_lmf;
517 address = (char *)ip - (char *)ji->code_start;
520 *native_offset = address;
523 if (!ji->method->wrapper_type)
527 *trace = mono_debug_print_stack_frame (ji->method, offset, domain);
529 sframe = (MonoPPCStackFrame*)MONO_CONTEXT_GET_BP (ctx);
530 MONO_CONTEXT_SET_BP (new_ctx, sframe->sp);
531 if (ji->method->save_lmf) {
532 memcpy (&new_ctx->fregs, (char*)sframe->sp - sizeof (double) * MONO_SAVED_FREGS, sizeof (double) * MONO_SAVED_FREGS);
533 memcpy (&new_ctx->regs, (char*)sframe->sp - sizeof (double) * MONO_SAVED_FREGS - sizeof (gulong) * MONO_SAVED_GREGS, sizeof (gulong) * MONO_SAVED_GREGS);
534 } else if (ji->used_regs) {
535 /* keep updated with emit_prolog in mini-ppc.c */
537 /* FIXME handle floating point args
538 for (i = 31; i >= 14; --i) {
539 if (ji->used_fregs & (1 << i)) {
540 offset += sizeof (double);
541 new_ctx->fregs [i - 14] = *(gulong*)((char*)sframe->sp - offset);
544 for (i = 31; i >= 13; --i) {
545 if (ji->used_regs & (1 << i)) {
546 offset += sizeof (gulong);
547 new_ctx->regs [i - 13] = *(gulong*)((char*)sframe->sp - offset);
551 /* the calling IP is in the parent frame */
552 sframe = (MonoPPCStackFrame*)sframe->sp;
553 /* we substract 4, so that the IP points into the call instruction */
554 MONO_CONTEXT_SET_IP (new_ctx, sframe->lr - 4);
560 setup_context (new_ctx);
566 char *fname = mono_method_full_name ((*lmf)->method, TRUE);
567 *trace = g_strdup_printf ("in (unmanaged) %s", fname);
571 if ((ji = mono_jit_info_table_find (domain, (gpointer)(*lmf)->eip))) {
573 memset (res, 0, sizeof (MonoJitInfo));
574 res->method = (*lmf)->method;
577 /*sframe = (MonoPPCStackFrame*)MONO_CONTEXT_GET_BP (ctx);
578 MONO_CONTEXT_SET_BP (new_ctx, sframe->sp);
579 MONO_CONTEXT_SET_IP (new_ctx, sframe->lr);*/
580 MONO_CONTEXT_SET_BP (new_ctx, (*lmf)->ebp);
581 MONO_CONTEXT_SET_IP (new_ctx, (*lmf)->eip);
582 memcpy (&new_ctx->regs, (*lmf)->iregs, sizeof (gulong) * MONO_SAVED_GREGS);
583 memcpy (&new_ctx->fregs, (*lmf)->fregs, sizeof (double) * MONO_SAVED_FREGS);
584 *lmf = (*lmf)->previous_lmf;
586 return ji ? ji : res;
593 mono_jit_walk_stack (MonoStackWalk func, gboolean do_il_offset, gpointer user_data) {
594 MonoDomain *domain = mono_domain_get ();
595 MonoJitTlsData *jit_tls = TlsGetValue (mono_jit_tls_id);
596 MonoLMF *lmf = jit_tls->lmf;
597 MonoJitInfo *ji, rji;
598 gint native_offset, il_offset;
600 MonoPPCStackFrame *sframe;
602 MonoContext ctx, new_ctx;
604 setup_context (&ctx);
605 setup_context (&new_ctx);
608 __asm__ volatile("lwz %0,0(r1)" : "=r" (sframe));
610 __asm__ volatile("lwz %0,0(1)" : "=r" (sframe));
612 //MONO_CONTEXT_SET_IP (&ctx, sframe->lr);
613 MONO_CONTEXT_SET_BP (&ctx, sframe->sp);
614 sframe = (MonoPPCStackFrame*)sframe->sp;
615 MONO_CONTEXT_SET_IP (&ctx, sframe->lr);
617 while (MONO_CONTEXT_GET_BP (&ctx) < jit_tls->end_of_stack) {
619 ji = mono_arch_find_jit_info (domain, jit_tls, &rji, NULL, &ctx, &new_ctx, NULL, &lmf, &native_offset, &managed);
622 if (ji == (gpointer)-1)
626 MonoDebugSourceLocation *source;
628 source = mono_debug_lookup_source_location (ji->method, native_offset, domain);
629 il_offset = source ? source->il_offset : -1;
630 mono_debug_free_source_location (source);
634 if (func (ji->method, native_offset, il_offset, managed, user_data))
638 setup_context (&ctx);
643 ves_icall_get_frame_info (gint32 skip, MonoBoolean need_file_info,
644 MonoReflectionMethod **method,
645 gint32 *iloffset, gint32 *native_offset,
646 MonoString **file, gint32 *line, gint32 *column)
648 MonoDomain *domain = mono_domain_get ();
649 MonoJitTlsData *jit_tls = TlsGetValue (mono_jit_tls_id);
650 MonoLMF *lmf = jit_tls->lmf;
651 MonoJitInfo *ji, rji;
652 MonoContext ctx, new_ctx;
653 MonoPPCStackFrame *sframe;
654 MonoDebugSourceLocation *location;
657 __asm__ volatile("lwz %0,0(r1)" : "=r" (sframe));
659 __asm__ volatile("lwz %0,0(1)" : "=r" (sframe));
661 MONO_CONTEXT_SET_BP (&ctx, sframe->sp);
662 sframe = (MonoPPCStackFrame*)sframe->sp;
663 MONO_CONTEXT_SET_IP (&ctx, sframe->lr);
664 /*MONO_CONTEXT_SET_IP (&ctx, ves_icall_get_frame_info);
665 MONO_CONTEXT_SET_BP (&ctx, __builtin_frame_address (0));*/
670 ji = mono_arch_find_jit_info (domain, jit_tls, &rji, NULL, &ctx, &new_ctx, NULL, &lmf, native_offset, NULL);
674 if (!ji || ji == (gpointer)-1 || MONO_CONTEXT_GET_BP (&ctx) >= jit_tls->end_of_stack)
677 /* skip all wrappers ??*/
678 if (ji->method->wrapper_type == MONO_WRAPPER_RUNTIME_INVOKE ||
679 ji->method->wrapper_type == MONO_WRAPPER_REMOTING_INVOKE_WITH_CHECK ||
680 ji->method->wrapper_type == MONO_WRAPPER_XDOMAIN_INVOKE ||
681 ji->method->wrapper_type == MONO_WRAPPER_XDOMAIN_DISPATCH ||
682 ji->method->wrapper_type == MONO_WRAPPER_REMOTING_INVOKE)
689 *method = mono_method_get_object (domain, ji->method, NULL);
691 location = mono_debug_lookup_source_location (ji->method, *native_offset, domain);
693 *iloffset = location->il_offset;
697 if (need_file_info) {
699 *file = mono_string_new (domain, location->source_file);
700 *line = location->row;
701 *column = location->column;
708 mono_debug_free_source_location (location);
714 * This is the function called from the signal handler
718 mono_arch_handle_exception (void *ctx, gpointer obj, gboolean test_only)
720 struct ucontext *uc = ctx;
724 mctx.sc_ir = uc->uc_mcontext->ss.srr0;
725 mctx.sc_sp = uc->uc_mcontext->ss.r1;
726 memcpy (&mctx.regs, &uc->uc_mcontext->ss.r13, sizeof (gulong) * MONO_SAVED_GREGS);
727 memcpy (&mctx.fregs, &uc->uc_mcontext->fs.fpregs [14], sizeof (double) * MONO_SAVED_FREGS);
729 result = arch_handle_exception (&mctx, obj, test_only);
730 /* restore the context so that returning from the signal handler will invoke
733 uc->uc_mcontext->ss.srr0 = mctx.sc_ir;
734 uc->uc_mcontext->ss.r1 = mctx.sc_sp;
735 memcpy (&uc->uc_mcontext->ss.r13, &mctx.regs, sizeof (gulong) * MONO_SAVED_GREGS);
736 memcpy (&uc->uc_mcontext->fs.fpregs [14], &mctx.fregs, sizeof (double) * MONO_SAVED_FREGS);
741 mono_arch_ip_from_context (void *sigctx)
743 struct ucontext *uc = sigctx;
744 return (gpointer)uc->uc_mcontext->ss.srr0;
750 mono_arch_handle_exception (void *ctx, gpointer obj, gboolean test_only)
752 struct ucontext *uc = ctx;
756 mctx.sc_ir = uc->uc_mcontext.uc_regs->gregs [PT_NIP];
757 mctx.sc_sp = uc->uc_mcontext.uc_regs->gregs [PT_R1];
758 memcpy (&mctx.regs, &uc->uc_mcontext.uc_regs->gregs [PT_R13], sizeof (gulong) * MONO_SAVED_GREGS);
759 memcpy (&mctx.fregs, &uc->uc_mcontext.uc_regs->fpregs.fpregs [14], sizeof (double) * MONO_SAVED_FREGS);
761 result = arch_handle_exception (&mctx, obj, test_only);
762 /* restore the context so that returning from the signal handler will invoke
765 uc->uc_mcontext.uc_regs->gregs [PT_NIP] = mctx.sc_ir;
766 uc->uc_mcontext.uc_regs->gregs [PT_R1] = mctx.sc_sp;
767 memcpy (&uc->uc_mcontext.uc_regs->gregs [PT_R13], &mctx.regs, sizeof (gulong) * MONO_SAVED_GREGS);
768 memcpy (&uc->uc_mcontext.uc_regs->fpregs.fpregs [14], &mctx.fregs, sizeof (double) * MONO_SAVED_FREGS);
773 mono_arch_ip_from_context (void *sigctx)
775 struct ucontext *uc = sigctx;
776 return (gpointer)uc->uc_mcontext.uc_regs->gregs [PT_NIP];
782 * arch_handle_exception:
783 * @ctx: saved processor state
784 * @obj: the exception object
785 * @test_only: only test if the exception is caught, but dont call handlers
790 arch_handle_exception (MonoContext *ctx, gpointer obj, gboolean test_only)
792 MonoDomain *domain = mono_domain_get ();
793 MonoJitInfo *ji, rji;
794 static int (*call_filter) (MonoContext *, gpointer, gpointer) = NULL;
795 MonoJitTlsData *jit_tls = TlsGetValue (mono_jit_tls_id);
796 MonoLMF *lmf = jit_tls->lmf;
797 GList *trace_ips = NULL;
798 MonoException *mono_ex;
799 MonoArray *initial_trace_ips = NULL;
801 gboolean has_dynamic_methods = FALSE;
803 g_assert (ctx != NULL);
805 MonoException *ex = mono_get_exception_null_reference ();
806 ex->message = mono_string_new (domain,
807 "Object reference not set to an instance of an object");
808 obj = (MonoObject *)ex;
811 if (mono_object_isinst (obj, mono_defaults.exception_class)) {
812 mono_ex = (MonoException*)obj;
813 initial_trace_ips = mono_ex->trace_ips;
820 call_filter = arch_get_call_filter ();
822 g_assert (jit_tls->end_of_stack);
823 g_assert (jit_tls->abort_func);
826 MonoContext ctx_cp = *ctx;
827 setup_context (&ctx_cp);
828 if (mono_jit_trace_calls != NULL)
829 g_print ("EXCEPTION handling: %s\n", mono_object_class (obj)->name);
830 if (!arch_handle_exception (&ctx_cp, obj, TRUE)) {
831 if (mono_break_on_exc)
833 mono_unhandled_exception (obj);
837 memset (&rji, 0, sizeof (rji));
842 setup_context (&new_ctx);
843 ji = mono_arch_find_jit_info (domain, jit_tls, &rji, &rji, ctx, &new_ctx,
844 NULL, &lmf, NULL, NULL);
846 g_warning ("Exception inside function without unwind info");
847 g_assert_not_reached ();
850 if (ji != (gpointer)-1) {
853 if (test_only && ji->method->wrapper_type != MONO_WRAPPER_RUNTIME_INVOKE && mono_ex) {
855 * Avoid overwriting the stack trace if the exception is
856 * rethrown. Also avoid giant stack traces during a stack
859 if (!initial_trace_ips && (frame_count < 1000)) {
860 trace_ips = g_list_prepend (trace_ips, MONO_CONTEXT_GET_IP (ctx));
865 if (ji->method->dynamic)
866 has_dynamic_methods = TRUE;
868 if (ji->num_clauses) {
871 g_assert (ji->clauses);
873 for (i = 0; i < ji->num_clauses; i++) {
874 MonoJitExceptionInfo *ei = &ji->clauses [i];
875 gboolean filtered = FALSE;
877 if (ei->try_start <= MONO_CONTEXT_GET_IP (ctx) &&
878 MONO_CONTEXT_GET_IP (ctx) <= ei->try_end) {
881 if ((ei->flags == MONO_EXCEPTION_CLAUSE_NONE) || (ei->flags == MONO_EXCEPTION_CLAUSE_FILTER)) {
882 /* store the exception object int cfg->excvar */
883 g_assert (ei->exvar_offset);
884 /* need to use the frame pointer (ppc_r31), not r1 (regs start from register r13): methods with clauses always have r31 */
885 *((gpointer *)((char *)(ctx->regs [ppc_r31-13]) + ei->exvar_offset)) = obj;
888 if (ei->flags == MONO_EXCEPTION_CLAUSE_FILTER)
889 filtered = call_filter (ctx, ei->data.filter, mono_ex);
891 if ((ei->flags == MONO_EXCEPTION_CLAUSE_NONE &&
892 mono_object_isinst (obj, ei->data.catch_class)) || filtered) {
894 if (mono_ex && !initial_trace_ips) {
895 trace_ips = g_list_reverse (trace_ips);
896 mono_ex->trace_ips = glist_to_array (trace_ips, mono_defaults.int_class);
897 if (has_dynamic_methods)
898 /* These methods could go away anytime, so compute the stack trace now */
899 mono_ex->stack_trace = ves_icall_System_Exception_get_trace (mono_ex);
901 g_list_free (trace_ips);
904 if (mono_jit_trace_calls != NULL)
905 g_print ("EXCEPTION: catch found at clause %d of %s\n", i, mono_method_full_name (ji->method, TRUE));
906 /*printf ("stack for catch: %p\n", MONO_CONTEXT_GET_BP (ctx));*/
907 MONO_CONTEXT_SET_IP (ctx, ei->handler_start);
911 if (!test_only && ei->try_start <= MONO_CONTEXT_GET_IP (ctx) &&
912 MONO_CONTEXT_GET_IP (ctx) < ei->try_end &&
913 (ei->flags & MONO_EXCEPTION_CLAUSE_FINALLY)) {
914 if (mono_jit_trace_calls != NULL)
915 g_print ("EXCEPTION: finally clause %d of %s\n", i, mono_method_full_name (ji->method, TRUE));
916 call_filter (ctx, ei->handler_start, NULL);
927 if ((ji == (gpointer)-1) || MONO_CONTEXT_GET_BP (ctx) >= jit_tls->end_of_stack) {
930 jit_tls->abort_func (obj);
931 g_assert_not_reached ();
933 if (mono_ex && !initial_trace_ips) {
934 trace_ips = g_list_reverse (trace_ips);
935 mono_ex->trace_ips = glist_to_array (trace_ips, mono_defaults.int_class);
936 if (has_dynamic_methods)
937 /* These methods could go away anytime, so compute the stack trace now */
938 mono_ex->stack_trace = ves_icall_System_Exception_get_trace (mono_ex);
940 g_list_free (trace_ips);
946 g_assert_not_reached ();
950 mono_arch_has_unwind_info (gconstpointer addr)