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];
160 /* we have the stack pointer, not the base pointer in sigcontext */
161 #define MONO_CONTEXT_SET_IP(ctx,ip) do { (ctx)->sc_ir = (int)ip; } while (0);
162 #define MONO_CONTEXT_SET_BP(ctx,bp) do { (ctx)->sc_sp = (int)bp; } while (0);
164 #define MONO_CONTEXT_GET_IP(ctx) ((gpointer)((ctx)->sc_ir))
165 #define MONO_CONTEXT_GET_BP(ctx) ((gpointer)((ctx)->sc_sp))
171 unsigned long unused1;
185 #define restore_regs_from_context(ctx_reg,ip_reg,tmp_reg) do { \
187 ppc_lwz (code, ip_reg, G_STRUCT_OFFSET (MonoContext, sc_ir), ctx_reg); \
188 ppc_lmw (code, ppc_r13, ctx_reg, G_STRUCT_OFFSET (MonoContext, regs)); \
189 for (reg = 0; reg < MONO_SAVED_FREGS; ++reg) { \
190 ppc_lfd (code, (14 + reg), G_STRUCT_OFFSET(MonoLMF, fregs) + reg * sizeof (gdouble), ctx_reg); \
195 #define setup_context(ctx)
198 * arch_get_restore_context:
200 * Returns a pointer to a method which restores a previously saved sigcontext.
201 * The first argument in r3 is the pointer to the context.
204 arch_get_restore_context (void)
207 static guint8 start [128];
208 static int inited = 0;
215 restore_regs_from_context (ppc_r3, ppc_r4, ppc_r5);
216 /* restore also the stack pointer */
217 ppc_lwz (code, ppc_sp, G_STRUCT_OFFSET (MonoContext, sc_sp), ppc_r3);
219 /* jump to the saved IP */
220 ppc_mtctr (code, ppc_r4);
221 ppc_bcctr (code, PPC_BR_ALWAYS, 0);
225 g_assert ((code - start) < sizeof(start));
230 * arch_get_call_filter:
232 * Returns a pointer to a method which calls an exception filter. We
233 * also use this function to call finally handlers (we pass NULL as
234 * @exc object in this case).
237 arch_get_call_filter (void)
239 static guint8 start [320];
240 static int inited = 0;
242 int alloc_size, pos, i;
248 /* call_filter (MonoContext *ctx, unsigned long eip, gpointer exc) */
251 /* save all the regs on the stack */
253 for (i = 31; i >= 14; --i) {
254 pos += sizeof (gdouble);
255 ppc_stfd (code, i, -pos, ppc_sp);
257 pos += sizeof (gulong) * MONO_SAVED_GREGS;
258 ppc_stmw (code, ppc_r13, ppc_sp, -pos);
260 ppc_mflr (code, ppc_r0);
261 ppc_stw (code, ppc_r0, PPC_RET_ADDR_OFFSET, ppc_sp);
263 alloc_size = PPC_MINIMAL_STACK_SIZE + pos + 64;
264 // align to PPC_STACK_ALIGNMENT bytes
265 alloc_size += PPC_STACK_ALIGNMENT - 1;
266 alloc_size &= ~(PPC_STACK_ALIGNMENT - 1);
268 g_assert ((alloc_size & (PPC_STACK_ALIGNMENT-1)) == 0);
269 ppc_stwu (code, ppc_sp, -alloc_size, ppc_sp);
271 /* restore all the regs from ctx (in r3), but not r1, the stack pointer */
272 restore_regs_from_context (ppc_r3, ppc_r6, ppc_r7);
273 /* call handler at eip (r4) and set the first arg with the exception (r5) */
274 ppc_mtctr (code, ppc_r4);
275 ppc_mr (code, ppc_r3, ppc_r5);
276 ppc_bcctrl (code, PPC_BR_ALWAYS, 0);
279 ppc_lwz (code, ppc_r0, alloc_size + PPC_RET_ADDR_OFFSET, ppc_sp);
280 ppc_mtlr (code, ppc_r0);
281 ppc_addic (code, ppc_sp, ppc_sp, alloc_size);
283 /* restore all the regs from the stack */
285 for (i = 31; i >= 14; --i) {
286 pos += sizeof (double);
287 ppc_lfd (code, i, -pos, ppc_sp);
289 pos += sizeof (gulong) * MONO_SAVED_GREGS;
290 ppc_lmw (code, ppc_r13, ppc_sp, -pos);
294 g_assert ((code - start) < sizeof(start));
299 throw_exception (MonoObject *exc, unsigned long eip, unsigned long esp, gulong *int_regs, gdouble *fp_regs)
301 static void (*restore_context) (MonoContext *);
304 if (!restore_context)
305 restore_context = arch_get_restore_context ();
307 /* adjust eip so that it point into the call instruction */
310 setup_context (&ctx);
312 /*printf ("stack in throw: %p\n", esp);*/
313 MONO_CONTEXT_SET_BP (&ctx, esp);
314 MONO_CONTEXT_SET_IP (&ctx, eip);
315 memcpy (&ctx.regs, int_regs, sizeof (gulong) * MONO_SAVED_GREGS);
316 memcpy (&ctx.fregs, fp_regs, sizeof (double) * MONO_SAVED_FREGS);
318 if (mono_object_isinst (exc, mono_defaults.exception_class)) {
319 MonoException *mono_ex = (MonoException*)exc;
320 mono_ex->stack_trace = NULL;
322 arch_handle_exception (&ctx, exc, FALSE);
323 restore_context (&ctx);
325 g_assert_not_reached ();
329 * arch_get_throw_exception_generic:
331 * Returns a function pointer which can be used to raise
332 * exceptions. The returned function has the following
333 * signature: void (*func) (MonoException *exc); or
334 * void (*func) (char *exc_name);
338 mono_arch_get_throw_exception_generic (guint8 *start, int size, int by_name)
341 int alloc_size, pos, i;
345 /* save all the regs on the stack */
347 for (i = 31; i >= 14; --i) {
348 pos += sizeof (gdouble);
349 ppc_stfd (code, i, -pos, ppc_sp);
351 pos += sizeof (gulong) * MONO_SAVED_GREGS;
352 ppc_stmw (code, ppc_r13, ppc_sp, -pos);
354 ppc_mflr (code, ppc_r0);
355 ppc_stw (code, ppc_r0, PPC_RET_ADDR_OFFSET, ppc_sp);
357 alloc_size = PPC_MINIMAL_STACK_SIZE + pos + 64;
358 // align to PPC_STACK_ALIGNMENT bytes
359 alloc_size += PPC_STACK_ALIGNMENT - 1;
360 alloc_size &= ~(PPC_STACK_ALIGNMENT - 1);
362 g_assert ((alloc_size & (PPC_STACK_ALIGNMENT-1)) == 0);
363 ppc_stwu (code, ppc_sp, -alloc_size, ppc_sp);
367 ppc_mr (code, ppc_r5, ppc_r3);
368 ppc_load (code, ppc_r3, mono_defaults.corlib);
369 ppc_load (code, ppc_r4, "System");
371 ppc_patch (code - 4, mono_exception_from_name);
374 /* call throw_exception (exc, ip, sp, int_regs, fp_regs) */
376 ppc_lwz (code, ppc_r5, 0, ppc_sp);
377 /* exc is already in place in r3 */
379 ppc_lwz (code, ppc_r4, PPC_RET_ADDR_OFFSET, ppc_r5);
381 ppc_mr (code, ppc_r4, ppc_r0); /* caller ip */
382 /* pointer to the saved fp regs */
383 pos = alloc_size - sizeof (double) * MONO_SAVED_FREGS;
384 ppc_addi (code, ppc_r7, ppc_sp, pos);
385 /* pointer to the saved int regs */
386 pos -= sizeof (gulong) * MONO_SAVED_GREGS;
387 ppc_addi (code, ppc_r6, ppc_sp, pos);
390 ppc_patch (code - 4, throw_exception);
391 /* we should never reach this breakpoint */
393 g_assert ((code - start) < size);
398 * arch_get_throw_exception:
400 * Returns a function pointer which can be used to raise
401 * exceptions. The returned function has the following
402 * signature: void (*func) (MonoException *exc);
403 * For example to raise an arithmetic exception you can use:
405 * x86_push_imm (code, mono_get_exception_arithmetic ());
406 * x86_call_code (code, arch_get_throw_exception ());
410 mono_arch_get_throw_exception (void)
412 static guint8 start [128];
413 static int inited = 0;
417 mono_arch_get_throw_exception_generic (start, sizeof (start), FALSE);
423 * arch_get_throw_exception_by_name:
425 * Returns a function pointer which can be used to raise
426 * corlib exceptions. The returned function has the following
427 * signature: void (*func) (char *exc_name);
428 * For example to raise an arithmetic exception you can use:
430 * x86_push_imm (code, "ArithmeticException");
431 * x86_call_code (code, arch_get_throw_exception_by_name ());
435 mono_arch_get_throw_exception_by_name (void)
437 static guint8 start [160];
438 static int inited = 0;
442 mono_arch_get_throw_exception_generic (start, sizeof (start), TRUE);
448 glist_to_array (GList *list)
450 MonoDomain *domain = mono_domain_get ();
457 len = g_list_length (list);
458 res = mono_array_new (domain, mono_defaults.int_class, len);
460 for (i = 0; list; list = list->next, i++)
461 mono_array_set (res, gpointer, i, list->data);
466 /* mono_arch_find_jit_info:
468 * This function is used to gather information from @ctx. It return the
469 * MonoJitInfo of the corresponding function, unwinds one stack frame and
470 * stores the resulting context into @new_ctx. It also stores a string
471 * describing the stack location into @trace (if not NULL), and modifies
472 * the @lmf if necessary. @native_offset return the IP offset from the
473 * start of the function or -1 if that info is not available.
476 mono_arch_find_jit_info (MonoDomain *domain, MonoJitTlsData *jit_tls, MonoJitInfo *res, MonoJitInfo *prev_ji,
477 MonoContext *ctx, MonoContext *new_ctx, char **trace, MonoLMF **lmf,
478 int *native_offset, gboolean *managed)
481 gpointer ip = MONO_CONTEXT_GET_IP (ctx);
484 MonoPPCStackFrame *sframe;
486 /* Avoid costly table lookup during stack overflow */
487 if (prev_ji && (ip > prev_ji->code_start && ((guint8*)ip < ((guint8*)prev_ji->code_start) + prev_ji->code_size)))
490 ji = mono_jit_info_table_find (domain, ip);
502 char *source_location, *tmpaddr, *fname;
503 gint32 address, iloffset;
508 setup_context (new_ctx);
510 if (*lmf && (MONO_CONTEXT_GET_BP (ctx) >= (gpointer)(*lmf)->ebp)) {
511 /* remove any unused lmf */
512 *lmf = (*lmf)->previous_lmf;
515 address = (char *)ip - (char *)ji->code_start;
518 *native_offset = address;
521 if (!ji->method->wrapper_type)
525 source_location = mono_debug_source_location_from_address (ji->method, address, NULL, domain);
526 iloffset = mono_debug_il_offset_from_address (ji->method, address, domain);
529 tmpaddr = g_strdup_printf ("<0x%05x>", address);
531 tmpaddr = g_strdup_printf ("[0x%05x]", iloffset);
533 fname = mono_method_full_name (ji->method, TRUE);
536 *trace = g_strdup_printf ("in %s (at %s) %s", tmpaddr, source_location, fname);
538 *trace = g_strdup_printf ("in %s %s", tmpaddr, fname);
541 g_free (source_location);
544 sframe = (MonoPPCStackFrame*)MONO_CONTEXT_GET_BP (ctx);
545 MONO_CONTEXT_SET_BP (new_ctx, sframe->sp);
546 if (ji->method->save_lmf) {
547 memcpy (&new_ctx->fregs, (char*)sframe->sp - sizeof (double) * MONO_SAVED_FREGS, sizeof (double) * MONO_SAVED_FREGS);
548 memcpy (&new_ctx->regs, (char*)sframe->sp - sizeof (double) * MONO_SAVED_FREGS - sizeof (gulong) * MONO_SAVED_GREGS, sizeof (gulong) * MONO_SAVED_GREGS);
549 } else if (ji->used_regs) {
550 /* keep updated with emit_prolog in mini-ppc.c */
552 /* FIXME handle floating point args
553 for (i = 31; i >= 14; --i) {
554 if (ji->used_fregs & (1 << i)) {
555 offset += sizeof (double);
556 new_ctx->fregs [i - 14] = *(gulong*)((char*)sframe->sp - offset);
559 for (i = 31; i >= 13; --i) {
560 if (ji->used_regs & (1 << i)) {
561 offset += sizeof (gulong);
562 new_ctx->regs [i - 13] = *(gulong*)((char*)sframe->sp - offset);
566 /* the calling IP is in the parent frame */
567 sframe = (MonoPPCStackFrame*)sframe->sp;
568 /* we substract 4, so that the IP points into the call instruction */
569 MONO_CONTEXT_SET_IP (new_ctx, sframe->lr - 4);
576 setup_context (new_ctx);
582 *trace = g_strdup_printf ("in (unmanaged) %s", mono_method_full_name ((*lmf)->method, TRUE));
584 if ((ji = mono_jit_info_table_find (domain, (gpointer)(*lmf)->eip))) {
587 memset (res, 0, sizeof (MonoJitInfo));
588 res->method = (*lmf)->method;
591 /*sframe = (MonoPPCStackFrame*)MONO_CONTEXT_GET_BP (ctx);
592 MONO_CONTEXT_SET_BP (new_ctx, sframe->sp);
593 MONO_CONTEXT_SET_IP (new_ctx, sframe->lr);*/
594 MONO_CONTEXT_SET_BP (new_ctx, (*lmf)->ebp);
595 MONO_CONTEXT_SET_IP (new_ctx, (*lmf)->eip);
596 memcpy (&new_ctx->regs, (*lmf)->iregs, sizeof (gulong) * MONO_SAVED_GREGS);
597 memcpy (&new_ctx->fregs, (*lmf)->fregs, sizeof (double) * MONO_SAVED_FREGS);
598 *lmf = (*lmf)->previous_lmf;
608 ves_icall_get_trace (MonoException *exc, gint32 skip, MonoBoolean need_file_info)
610 MonoDomain *domain = mono_domain_get ();
612 MonoArray *ta = exc->trace_ips;
615 len = mono_array_length (ta);
617 res = mono_array_new (domain, mono_defaults.stack_frame_class, len > skip ? len - skip : 0);
619 for (i = skip; i < len; i++) {
621 MonoStackFrame *sf = (MonoStackFrame *)mono_object_new (domain, mono_defaults.stack_frame_class);
622 gpointer ip = mono_array_get (ta, gpointer, i);
624 ji = mono_jit_info_table_find (domain, ip);
626 sf->method = mono_method_get_object (domain, mono_defaults.object_class->methods [0], NULL);
627 mono_array_set (res, gpointer, i, sf);
630 //g_assert (ji != NULL);
632 sf->method = mono_method_get_object (domain, ji->method, NULL);
633 sf->native_offset = (char *)ip - (char *)ji->code_start;
635 sf->il_offset = mono_debug_il_offset_from_address (ji->method, sf->native_offset, domain);
637 if (need_file_info) {
640 filename = mono_debug_source_location_from_address (ji->method, sf->native_offset, &sf->line, domain);
642 sf->filename = filename? mono_string_new (domain, filename): NULL;
648 mono_array_set (res, gpointer, i, sf);
655 mono_jit_walk_stack (MonoStackWalk func, gpointer user_data) {
656 MonoDomain *domain = mono_domain_get ();
657 MonoJitTlsData *jit_tls = TlsGetValue (mono_jit_tls_id);
658 MonoLMF *lmf = jit_tls->lmf;
659 MonoJitInfo *ji, rji;
660 gint native_offset, il_offset;
662 MonoPPCStackFrame *sframe;
664 MonoContext ctx, new_ctx;
666 setup_context (&ctx);
667 setup_context (&new_ctx);
670 __asm__ volatile("lwz %0,0(r1)" : "=r" (sframe));
672 __asm__ volatile("lwz %0,0(1)" : "=r" (sframe));
674 //MONO_CONTEXT_SET_IP (&ctx, sframe->lr);
675 MONO_CONTEXT_SET_BP (&ctx, sframe->sp);
676 sframe = (MonoPPCStackFrame*)sframe->sp;
677 MONO_CONTEXT_SET_IP (&ctx, sframe->lr);
679 while (MONO_CONTEXT_GET_BP (&ctx) < jit_tls->end_of_stack) {
681 ji = mono_arch_find_jit_info (domain, jit_tls, &rji, NULL, &ctx, &new_ctx, NULL, &lmf, &native_offset, &managed);
684 if (ji == (gpointer)-1)
687 il_offset = mono_debug_il_offset_from_address (ji->method, native_offset, domain);
689 if (func (ji->method, native_offset, il_offset, managed, user_data))
693 setup_context (&ctx);
698 ves_icall_get_frame_info (gint32 skip, MonoBoolean need_file_info,
699 MonoReflectionMethod **method,
700 gint32 *iloffset, gint32 *native_offset,
701 MonoString **file, gint32 *line, gint32 *column)
703 MonoDomain *domain = mono_domain_get ();
704 MonoJitTlsData *jit_tls = TlsGetValue (mono_jit_tls_id);
705 MonoLMF *lmf = jit_tls->lmf;
706 MonoJitInfo *ji, rji;
707 MonoContext ctx, new_ctx;
708 MonoPPCStackFrame *sframe;
711 __asm__ volatile("lwz %0,0(r1)" : "=r" (sframe));
713 __asm__ volatile("lwz %0,0(1)" : "=r" (sframe));
715 MONO_CONTEXT_SET_BP (&ctx, sframe->sp);
716 sframe = (MonoPPCStackFrame*)sframe->sp;
717 MONO_CONTEXT_SET_IP (&ctx, sframe->lr);
718 /*MONO_CONTEXT_SET_IP (&ctx, ves_icall_get_frame_info);
719 MONO_CONTEXT_SET_BP (&ctx, __builtin_frame_address (0));*/
724 ji = mono_arch_find_jit_info (domain, jit_tls, &rji, NULL, &ctx, &new_ctx, NULL, &lmf, native_offset, NULL);
728 if (!ji || ji == (gpointer)-1 || MONO_CONTEXT_GET_BP (&ctx) >= jit_tls->end_of_stack)
731 /* skip all wrappers ??*/
732 if (ji->method->wrapper_type == MONO_WRAPPER_RUNTIME_INVOKE ||
733 ji->method->wrapper_type == MONO_WRAPPER_REMOTING_INVOKE_WITH_CHECK ||
734 ji->method->wrapper_type == MONO_WRAPPER_REMOTING_INVOKE)
741 *method = mono_method_get_object (domain, ji->method, NULL);
742 *iloffset = mono_debug_il_offset_from_address (ji->method, *native_offset, domain);
744 if (need_file_info) {
747 filename = mono_debug_source_location_from_address (ji->method, *native_offset, line, domain);
749 *file = filename? mono_string_new (domain, filename): NULL;
759 * This is the function called from the signal handler
763 mono_arch_handle_exception (void *ctx, gpointer obj, gboolean test_only)
765 struct ucontext *uc = ctx;
769 mctx.sc_ir = uc->uc_mcontext->ss.srr0;
770 mctx.sc_sp = uc->uc_mcontext->ss.r1;
771 memcpy (&mctx.regs, &uc->uc_mcontext->ss.r13, sizeof (gulong) * MONO_SAVED_GREGS);
772 memcpy (&mctx.fregs, &uc->uc_mcontext->fs.fpregs [14], sizeof (double) * MONO_SAVED_FREGS);
774 result = arch_handle_exception (&mctx, obj, test_only);
775 /* restore the context so that returning from the signal handler will invoke
778 uc->uc_mcontext->ss.srr0 = mctx.sc_ir;
779 uc->uc_mcontext->ss.r1 = mctx.sc_sp;
780 memcpy (&uc->uc_mcontext->ss.r13, &mctx.regs, sizeof (gulong) * MONO_SAVED_GREGS);
781 memcpy (&uc->uc_mcontext->fs.fpregs [14], &mctx.fregs, sizeof (double) * MONO_SAVED_FREGS);
786 mono_arch_ip_from_context (void *sigctx)
788 struct ucontext *uc = sigctx;
789 return (gpointer)uc->uc_mcontext->ss.srr0;
795 mono_arch_handle_exception (void *ctx, gpointer obj, gboolean test_only)
797 struct ucontext *uc = ctx;
801 mctx.sc_ir = uc->uc_mcontext.uc_regs->gregs [PT_NIP];
802 mctx.sc_sp = uc->uc_mcontext.uc_regs->gregs [PT_R1];
803 memcpy (&mctx.regs, &uc->uc_mcontext.uc_regs->gregs [PT_R13], sizeof (gulong) * MONO_SAVED_GREGS);
804 memcpy (&mctx.fregs, &uc->uc_mcontext.uc_regs->fpregs.fpregs [14], sizeof (double) * MONO_SAVED_FREGS);
806 result = arch_handle_exception (&mctx, obj, test_only);
807 /* restore the context so that returning from the signal handler will invoke
810 uc->uc_mcontext.uc_regs->gregs [PT_NIP] = mctx.sc_ir;
811 uc->uc_mcontext.uc_regs->gregs [PT_R1] = mctx.sc_sp;
812 memcpy (&uc->uc_mcontext.uc_regs->gregs [PT_R13], &mctx.regs, sizeof (gulong) * MONO_SAVED_GREGS);
813 memcpy (&uc->uc_mcontext.uc_regs->fpregs.fpregs [14], &mctx.fregs, sizeof (double) * MONO_SAVED_FREGS);
818 mono_arch_ip_from_context (void *sigctx)
820 struct ucontext *uc = sigctx;
821 return (gpointer)uc->uc_mcontext.uc_regs->gregs [PT_NIP];
827 * arch_handle_exception:
828 * @ctx: saved processor state
829 * @obj: the exception object
830 * @test_only: only test if the exception is caught, but dont call handlers
835 arch_handle_exception (MonoContext *ctx, gpointer obj, gboolean test_only)
837 MonoDomain *domain = mono_domain_get ();
838 MonoJitInfo *ji, rji;
839 static int (*call_filter) (MonoContext *, gpointer, gpointer) = NULL;
840 MonoJitTlsData *jit_tls = TlsGetValue (mono_jit_tls_id);
841 MonoLMF *lmf = jit_tls->lmf;
842 GList *trace_ips = NULL;
843 MonoException *mono_ex;
844 MonoString *initial_stack_trace = NULL;
845 GString *trace_str = NULL;
848 g_assert (ctx != NULL);
850 MonoException *ex = mono_get_exception_null_reference ();
851 ex->message = mono_string_new (domain,
852 "Object reference not set to an instance of an object");
853 obj = (MonoObject *)ex;
856 if (mono_object_isinst (obj, mono_defaults.exception_class)) {
857 mono_ex = (MonoException*)obj;
858 initial_stack_trace = mono_ex->stack_trace;
865 call_filter = arch_get_call_filter ();
867 g_assert (jit_tls->end_of_stack);
868 g_assert (jit_tls->abort_func);
871 MonoContext ctx_cp = *ctx;
872 setup_context (&ctx_cp);
873 if (mono_jit_trace_calls != NULL)
874 g_print ("EXCEPTION handling: %s\n", mono_object_class (obj)->name);
875 if (!arch_handle_exception (&ctx_cp, obj, TRUE)) {
876 if (mono_break_on_exc)
878 mono_unhandled_exception (obj);
882 memset (&rji, 0, sizeof (rji));
887 gboolean need_trace = FALSE;
889 if (test_only && (frame_count < 1000)) {
892 trace_str = g_string_new ("");
894 setup_context (&new_ctx);
895 ji = mono_arch_find_jit_info (domain, jit_tls, &rji, &rji, ctx, &new_ctx,
896 need_trace ? &trace : NULL, &lmf, NULL, NULL);
898 g_warning ("Exception inside function without unwind info");
899 g_assert_not_reached ();
902 if (ji != (gpointer)-1) {
905 if (test_only && ji->method->wrapper_type != MONO_WRAPPER_RUNTIME_INVOKE && mono_ex) {
906 if (!initial_stack_trace && (frame_count < 1000)) {
907 trace_ips = g_list_prepend (trace_ips, MONO_CONTEXT_GET_IP (ctx));
909 g_string_append (trace_str, trace);
910 g_string_append_c (trace_str, '\n');
914 if (ji->num_clauses) {
917 g_assert (ji->clauses);
919 for (i = 0; i < ji->num_clauses; i++) {
920 MonoJitExceptionInfo *ei = &ji->clauses [i];
921 gboolean filtered = FALSE;
923 if (ei->try_start <= MONO_CONTEXT_GET_IP (ctx) &&
924 MONO_CONTEXT_GET_IP (ctx) <= ei->try_end) {
927 if ((ei->flags == MONO_EXCEPTION_CLAUSE_NONE) || (ei->flags == MONO_EXCEPTION_CLAUSE_FILTER)) {
928 /* store the exception object int cfg->excvar */
929 g_assert (ji->exvar_offset);
930 /* need to use the frame pointer (ppc_r31), not r1 (regs start from register r13): methods with clauses always have r31 */
931 *((gpointer *)((char *)(ctx->regs [ppc_r31-13]) + ji->exvar_offset)) = obj;
932 if (!initial_stack_trace && trace_str) {
933 mono_ex->stack_trace = mono_string_new (domain, trace_str->str);
937 if (ei->flags == MONO_EXCEPTION_CLAUSE_FILTER)
938 filtered = call_filter (ctx, ei->data.filter, mono_ex);
940 if ((ei->flags == MONO_EXCEPTION_CLAUSE_NONE &&
941 mono_object_isinst (obj, mono_class_get (ji->method->klass->image, ei->data.token))) || filtered) {
944 trace_ips = g_list_reverse (trace_ips);
945 mono_ex->trace_ips = glist_to_array (trace_ips);
947 g_list_free (trace_ips);
950 g_string_free (trace_str, TRUE);
953 if (mono_jit_trace_calls != NULL)
954 g_print ("EXCEPTION: catch found at clause %d of %s\n", i, mono_method_full_name (ji->method, TRUE));
955 /*printf ("stack for catch: %p\n", MONO_CONTEXT_GET_BP (ctx));*/
956 MONO_CONTEXT_SET_IP (ctx, ei->handler_start);
960 g_string_free (trace_str, TRUE);
963 if (!test_only && ei->try_start <= MONO_CONTEXT_GET_IP (ctx) &&
964 MONO_CONTEXT_GET_IP (ctx) < ei->try_end &&
965 (ei->flags & MONO_EXCEPTION_CLAUSE_FINALLY)) {
966 if (mono_jit_trace_calls != NULL)
967 g_print ("EXCEPTION: finally clause %d of %s\n", i, mono_method_full_name (ji->method, TRUE));
968 call_filter (ctx, ei->handler_start, NULL);
981 if ((ji == (gpointer)-1) || MONO_CONTEXT_GET_BP (ctx) >= jit_tls->end_of_stack) {
984 jit_tls->abort_func (obj);
985 g_assert_not_reached ();
988 trace_ips = g_list_reverse (trace_ips);
989 mono_ex->trace_ips = glist_to_array (trace_ips);
991 g_list_free (trace_ips);
993 g_string_free (trace_str, TRUE);
999 g_assert_not_reached ();
1003 mono_arch_has_unwind_info (gconstpointer addr)