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;
184 /* disbale this for now */
185 #undef MONO_USE_EXC_TABLES
187 #ifdef MONO_USE_EXC_TABLES
189 /*************************************/
190 /* STACK UNWINDING STUFF */
191 /*************************************/
193 /* These definitions are from unwind-dw2.c in glibc 2.2.5 */
196 #define DWARF_FRAME_REGISTERS 17
198 typedef struct frame_state
204 long reg_or_offset[DWARF_FRAME_REGISTERS+1];
205 unsigned short cfa_reg;
206 unsigned short retaddr_column;
207 char saved[DWARF_FRAME_REGISTERS+1];
213 get_sigcontext_reg (struct sigcontext *ctx, int dwarf_regnum)
215 switch (dwarf_regnum) {
233 g_assert_not_reached ();
240 set_sigcontext_reg (struct sigcontext *ctx, int dwarf_regnum, long value)
242 switch (dwarf_regnum) {
271 g_assert_not_reached ();
275 typedef struct frame_state * (*framesf) (void *, struct frame_state *);
277 static framesf frame_state_for = NULL;
279 static gboolean inited = FALSE;
281 typedef char ** (*get_backtrace_symbols_type) (void *__const *__array, int __size);
283 static get_backtrace_symbols_type get_backtrace_symbols = NULL;
286 init_frame_state_for (void)
291 * There are two versions of __frame_state_for: one in libgcc.a and the
292 * other in glibc.so. We need the version from glibc.
293 * For more info, see this:
294 * http://gcc.gnu.org/ml/gcc/2002-08/msg00192.html
296 if ((module = g_module_open ("libc.so.6", G_MODULE_BIND_LAZY))) {
298 if (!g_module_symbol (module, "__frame_state_for", (gpointer*)&frame_state_for))
299 frame_state_for = NULL;
301 if (!g_module_symbol (module, "backtrace_symbols", (gpointer*)&get_backtrace_symbols)) {
302 get_backtrace_symbols = NULL;
303 frame_state_for = NULL;
306 g_module_close (module);
314 /* mono_arch_has_unwind_info:
316 * Tests if a function has an DWARF exception table able to restore
317 * all caller saved registers.
320 mono_arch_has_unwind_info (MonoMethod *method)
323 struct frame_state state_in;
324 struct frame_state *res;
327 init_frame_state_for ();
329 if (!frame_state_for)
332 g_assert (method->addr);
334 memset (&state_in, 0, sizeof (state_in));
336 /* offset 10 is just a guess, but it works for all methods tested */
337 if ((res = frame_state_for ((char *)method->addr + 10, &state_in))) {
339 if (res->saved [X86_EBX] != 1 ||
340 res->saved [X86_EDI] != 1 ||
341 res->saved [X86_EBP] != 1 ||
342 res->saved [X86_ESI] != 1) {
357 void *return_address;
361 ppc_unwind_native_frame (MonoDomain *domain, MonoJitTlsData *jit_tls, MonoContext *ctx,
362 MonoContext *new_ctx, MonoLMF *lmf, char **trace)
365 struct stack_frame *frame;
368 struct frame_state state_in;
369 struct frame_state *res;
375 init_frame_state_for ();
377 if (!frame_state_for)
380 frame = MONO_CONTEXT_GET_BP (ctx);
382 max_stack = lmf && lmf->method ? lmf : jit_tls->end_of_stack;
386 memset (&state_in, 0, sizeof (state_in));
388 while ((gpointer)frame->next < (gpointer)max_stack) {
389 gpointer ip, addr = frame->return_address;
391 char *tmp, **symbols;
394 ip = MONO_CONTEXT_GET_IP (new_ctx);
395 symbols = get_backtrace_symbols (&ip, 1);
397 tmp = g_strdup_printf ("%s\nin (unmanaged) %s", *trace, symbols [0]);
399 tmp = g_strdup_printf ("in (unmanaged) %s", symbols [0]);
406 if ((res = frame_state_for (addr, &state_in))) {
409 cfa = (gint8*) (get_sigcontext_reg (new_ctx, res->cfa_reg) + res->cfa_offset);
410 frame = (struct stack_frame *)((gint8*)cfa - 8);
411 for (i = 0; i < DWARF_FRAME_REGISTERS + 1; i++) {
412 int how = res->saved[i];
414 g_assert ((how == 0) || (how == 1));
417 val = * (long*) ((gint8*)cfa + res->reg_or_offset[i]);
418 set_sigcontext_reg (new_ctx, i, val);
421 new_ctx->SC_ESP = (long)cfa;
423 if (res->saved [X86_EBX] == 1 &&
424 res->saved [X86_EDI] == 1 &&
425 res->saved [X86_EBP] == 1 &&
426 res->saved [X86_ESI] == 1 &&
427 (ji = mono_jit_info_table_find (domain, frame->return_address))) {
428 //printf ("FRAME CFA %s\n", mono_method_full_name (ji->method, TRUE));
433 //printf ("FRAME %p %p %p\n", frame, MONO_CONTEXT_GET_IP (new_ctx), mono_jit_info_table_find (domain, MONO_CONTEXT_GET_IP (new_ctx)));
435 MONO_CONTEXT_SET_IP (new_ctx, frame->return_address);
437 MONO_CONTEXT_SET_BP (new_ctx, frame);
439 /* stop if !frame or when we detect an unexpected managed frame */
440 if (!frame || mono_jit_info_table_find (domain, frame->return_address)) {
451 //g_assert_not_reached ();
463 #define restore_regs_from_context(ctx_reg,ip_reg,tmp_reg) do { \
465 ppc_lwz (code, ip_reg, G_STRUCT_OFFSET (MonoContext, sc_ir), ctx_reg); \
466 ppc_lmw (code, ppc_r13, ctx_reg, G_STRUCT_OFFSET (MonoContext, regs)); \
467 for (reg = 0; reg < MONO_SAVED_FREGS; ++reg) { \
468 ppc_lfd (code, (14 + reg), G_STRUCT_OFFSET(MonoLMF, fregs) + reg * sizeof (gdouble), ctx_reg); \
473 #define setup_context(ctx)
476 * arch_get_restore_context:
478 * Returns a pointer to a method which restores a previously saved sigcontext.
479 * The first argument in r3 is the pointer to the context.
482 arch_get_restore_context (void)
485 static guint8 start [128];
486 static int inited = 0;
493 restore_regs_from_context (ppc_r3, ppc_r4, ppc_r5);
494 /* restore also the stack pointer */
495 ppc_lwz (code, ppc_sp, G_STRUCT_OFFSET (MonoContext, sc_sp), ppc_r3);
497 /* jump to the saved IP */
498 ppc_mtctr (code, ppc_r4);
499 ppc_bcctr (code, PPC_BR_ALWAYS, 0);
503 g_assert ((code - start) < sizeof(start));
508 * arch_get_call_filter:
510 * Returns a pointer to a method which calls an exception filter. We
511 * also use this function to call finally handlers (we pass NULL as
512 * @exc object in this case).
515 arch_get_call_filter (void)
517 static guint8 start [320];
518 static int inited = 0;
520 int alloc_size, pos, i;
526 /* call_filter (MonoContext *ctx, unsigned long eip, gpointer exc) */
529 /* save all the regs on the stack */
531 for (i = 31; i >= 14; --i) {
532 pos += sizeof (gdouble);
533 ppc_stfd (code, i, -pos, ppc_sp);
535 pos += sizeof (gulong) * MONO_SAVED_GREGS;
536 ppc_stmw (code, ppc_r13, ppc_sp, -pos);
538 ppc_mflr (code, ppc_r0);
539 ppc_stw (code, ppc_r0, PPC_RET_ADDR_OFFSET, ppc_sp);
541 alloc_size = PPC_MINIMAL_STACK_SIZE + pos + 64;
542 // align to PPC_STACK_ALIGNMENT bytes
543 alloc_size += PPC_STACK_ALIGNMENT - 1;
544 alloc_size &= ~(PPC_STACK_ALIGNMENT - 1);
546 g_assert ((alloc_size & (PPC_STACK_ALIGNMENT-1)) == 0);
547 ppc_stwu (code, ppc_sp, -alloc_size, ppc_sp);
549 /* restore all the regs from ctx (in r3), but not r1, the stack pointer */
550 restore_regs_from_context (ppc_r3, ppc_r6, ppc_r7);
551 /* call handler at eip (r4) and set the first arg with the exception (r5) */
552 ppc_mtctr (code, ppc_r4);
553 ppc_mr (code, ppc_r3, ppc_r5);
554 ppc_bcctrl (code, PPC_BR_ALWAYS, 0);
557 ppc_lwz (code, ppc_r0, alloc_size + PPC_RET_ADDR_OFFSET, ppc_sp);
558 ppc_mtlr (code, ppc_r0);
559 ppc_addic (code, ppc_sp, ppc_sp, alloc_size);
561 /* restore all the regs from the stack */
563 for (i = 31; i >= 14; --i) {
564 pos += sizeof (double);
565 ppc_lfd (code, i, -pos, ppc_sp);
567 pos += sizeof (gulong) * MONO_SAVED_GREGS;
568 ppc_lmw (code, ppc_r13, ppc_sp, -pos);
572 g_assert ((code - start) < sizeof(start));
577 throw_exception (MonoObject *exc, unsigned long eip, unsigned long esp, gulong *int_regs, gdouble *fp_regs)
579 static void (*restore_context) (MonoContext *);
582 if (!restore_context)
583 restore_context = arch_get_restore_context ();
585 /* adjust eip so that it point into the call instruction */
588 setup_context (&ctx);
590 /*printf ("stack in throw: %p\n", esp);*/
591 MONO_CONTEXT_SET_BP (&ctx, esp);
592 MONO_CONTEXT_SET_IP (&ctx, eip);
593 memcpy (&ctx.regs, int_regs, sizeof (gulong) * MONO_SAVED_GREGS);
594 memcpy (&ctx.fregs, fp_regs, sizeof (double) * MONO_SAVED_FREGS);
596 arch_handle_exception (&ctx, exc, FALSE);
597 restore_context (&ctx);
599 g_assert_not_reached ();
603 * arch_get_throw_exception_generic:
605 * Returns a function pointer which can be used to raise
606 * exceptions. The returned function has the following
607 * signature: void (*func) (MonoException *exc); or
608 * void (*func) (char *exc_name);
612 mono_arch_get_throw_exception_generic (guint8 *start, int size, int by_name)
615 int alloc_size, pos, i;
619 /* save all the regs on the stack */
621 for (i = 31; i >= 14; --i) {
622 pos += sizeof (gdouble);
623 ppc_stfd (code, i, -pos, ppc_sp);
625 pos += sizeof (gulong) * MONO_SAVED_GREGS;
626 ppc_stmw (code, ppc_r13, ppc_sp, -pos);
628 ppc_mflr (code, ppc_r0);
629 ppc_stw (code, ppc_r0, PPC_RET_ADDR_OFFSET, ppc_sp);
631 alloc_size = PPC_MINIMAL_STACK_SIZE + pos + 64;
632 // align to PPC_STACK_ALIGNMENT bytes
633 alloc_size += PPC_STACK_ALIGNMENT - 1;
634 alloc_size &= ~(PPC_STACK_ALIGNMENT - 1);
636 g_assert ((alloc_size & (PPC_STACK_ALIGNMENT-1)) == 0);
637 ppc_stwu (code, ppc_sp, -alloc_size, ppc_sp);
641 ppc_mr (code, ppc_r5, ppc_r3);
642 ppc_load (code, ppc_r3, mono_defaults.corlib);
643 ppc_load (code, ppc_r4, "System");
645 ppc_patch (code - 4, mono_exception_from_name);
648 /* call throw_exception (exc, ip, sp, int_regs, fp_regs) */
650 ppc_lwz (code, ppc_r5, 0, ppc_sp);
651 /* exc is already in place in r3 */
653 ppc_lwz (code, ppc_r4, PPC_RET_ADDR_OFFSET, ppc_r5);
655 ppc_mr (code, ppc_r4, ppc_r0); /* caller ip */
656 /* pointer to the saved fp regs */
657 pos = alloc_size - sizeof (double) * MONO_SAVED_FREGS;
658 ppc_addi (code, ppc_r7, ppc_sp, pos);
659 /* pointer to the saved int regs */
660 pos -= sizeof (gulong) * MONO_SAVED_GREGS;
661 ppc_addi (code, ppc_r6, ppc_sp, pos);
664 ppc_patch (code - 4, throw_exception);
665 /* we should never reach this breakpoint */
667 g_assert ((code - start) < size);
672 * arch_get_throw_exception:
674 * Returns a function pointer which can be used to raise
675 * exceptions. The returned function has the following
676 * signature: void (*func) (MonoException *exc);
677 * For example to raise an arithmetic exception you can use:
679 * x86_push_imm (code, mono_get_exception_arithmetic ());
680 * x86_call_code (code, arch_get_throw_exception ());
684 mono_arch_get_throw_exception (void)
686 static guint8 start [128];
687 static int inited = 0;
691 mono_arch_get_throw_exception_generic (start, sizeof (start), FALSE);
697 * arch_get_throw_exception_by_name:
699 * Returns a function pointer which can be used to raise
700 * corlib exceptions. The returned function has the following
701 * signature: void (*func) (char *exc_name);
702 * For example to raise an arithmetic exception you can use:
704 * x86_push_imm (code, "ArithmeticException");
705 * x86_call_code (code, arch_get_throw_exception_by_name ());
709 mono_arch_get_throw_exception_by_name (void)
711 static guint8 start [160];
712 static int inited = 0;
716 mono_arch_get_throw_exception_generic (start, sizeof (start), TRUE);
722 glist_to_array (GList *list)
724 MonoDomain *domain = mono_domain_get ();
731 len = g_list_length (list);
732 res = mono_array_new (domain, mono_defaults.int_class, len);
734 for (i = 0; list; list = list->next, i++)
735 mono_array_set (res, gpointer, i, list->data);
740 /* mono_arch_find_jit_info:
742 * This function is used to gather information from @ctx. It return the
743 * MonoJitInfo of the corresponding function, unwinds one stack frame and
744 * stores the resulting context into @new_ctx. It also stores a string
745 * describing the stack location into @trace (if not NULL), and modifies
746 * the @lmf if necessary. @native_offset return the IP offset from the
747 * start of the function or -1 if that info is not available.
750 mono_arch_find_jit_info (MonoDomain *domain, MonoJitTlsData *jit_tls, MonoJitInfo *res, MonoJitInfo *prev_ji,
751 MonoContext *ctx, MonoContext *new_ctx, char **trace, MonoLMF **lmf,
752 int *native_offset, gboolean *managed)
755 gpointer ip = MONO_CONTEXT_GET_IP (ctx);
758 MonoPPCStackFrame *sframe;
760 /* Avoid costly table lookup during stack overflow */
761 if (prev_ji && (ip > prev_ji->code_start && ((guint8*)ip < ((guint8*)prev_ji->code_start) + prev_ji->code_size)))
764 ji = mono_jit_info_table_find (domain, ip);
776 char *source_location, *tmpaddr, *fname;
777 gint32 address, iloffset;
782 setup_context (new_ctx);
784 if (*lmf && (MONO_CONTEXT_GET_BP (ctx) >= (gpointer)(*lmf)->ebp)) {
785 /* remove any unused lmf */
786 *lmf = (*lmf)->previous_lmf;
789 address = (char *)ip - (char *)ji->code_start;
792 *native_offset = address;
795 if (!ji->method->wrapper_type)
799 source_location = mono_debug_source_location_from_address (ji->method, address, NULL, domain);
800 iloffset = mono_debug_il_offset_from_address (ji->method, address, domain);
803 tmpaddr = g_strdup_printf ("<0x%05x>", address);
805 tmpaddr = g_strdup_printf ("[0x%05x]", iloffset);
807 fname = mono_method_full_name (ji->method, TRUE);
810 *trace = g_strdup_printf ("in %s (at %s) %s", tmpaddr, source_location, fname);
812 *trace = g_strdup_printf ("in %s %s", tmpaddr, fname);
815 g_free (source_location);
820 /* restore caller saved registers */
821 if (ji->used_regs & X86_EBX_MASK) {
822 new_ctx->SC_EBX = *((int *)ctx->SC_EBP + offset);
825 if (ji->used_regs & X86_EDI_MASK) {
826 new_ctx->SC_EDI = *((int *)ctx->SC_EBP + offset);
829 if (ji->used_regs & X86_ESI_MASK) {
830 new_ctx->SC_ESI = *((int *)ctx->SC_EBP + offset);
833 new_ctx->SC_ESP = ctx->SC_EBP;
834 /* we substract 1, so that the IP points into the call instruction */
835 new_ctx->SC_EIP = *((int *)ctx->SC_EBP + 1) - 1;
836 new_ctx->SC_EBP = *((int *)ctx->SC_EBP);
838 sframe = (MonoPPCStackFrame*)MONO_CONTEXT_GET_BP (ctx);
839 MONO_CONTEXT_SET_BP (new_ctx, sframe->sp);
841 /* keep updated with emit_prolog in mini-ppc.c */
843 /* FIXME handle floating point args
844 for (i = 31; i >= 14; --i) {
845 if (ji->used_fregs & (1 << i)) {
846 offset += sizeof (double);
847 new_ctx->fregs [i - 14] = *(gulong*)((char*)sframe->sp - offset);
850 for (i = 31; i >= 13; --i) {
851 if (ji->used_regs & (1 << i)) {
852 offset += sizeof (gulong);
853 new_ctx->regs [i - 13] = *(gulong*)((char*)sframe->sp - offset);
857 /* the calling IP is in the parent frame */
858 sframe = (MonoPPCStackFrame*)sframe->sp;
859 /* we substract 4, so that the IP points into the call instruction */
860 MONO_CONTEXT_SET_IP (new_ctx, sframe->lr - 4);
863 #ifdef MONO_USE_EXC_TABLES
864 } else if ((ji = ppc_unwind_native_frame (domain, jit_tls, ctx, new_ctx, *lmf, trace))) {
871 setup_context (new_ctx);
877 *trace = g_strdup_printf ("in (unmanaged) %s", mono_method_full_name ((*lmf)->method, TRUE));
879 if ((ji = mono_jit_info_table_find (domain, (gpointer)(*lmf)->eip))) {
882 memset (res, 0, sizeof (MonoJitInfo));
883 res->method = (*lmf)->method;
887 new_ctx->SC_ESI = (*lmf)->esi;
888 new_ctx->SC_EDI = (*lmf)->edi;
889 new_ctx->SC_EBX = (*lmf)->ebx;
890 new_ctx->SC_EBP = (*lmf)->ebp;
891 new_ctx->SC_EIP = (*lmf)->eip;
892 /* the lmf is always stored on the stack, so the following
893 * expression points to a stack location which can be used as ESP */
894 new_ctx->SC_ESP = (unsigned long)&((*lmf)->eip);
896 /*sframe = (MonoPPCStackFrame*)MONO_CONTEXT_GET_BP (ctx);
897 MONO_CONTEXT_SET_BP (new_ctx, sframe->sp);
898 MONO_CONTEXT_SET_IP (new_ctx, sframe->lr);*/
899 MONO_CONTEXT_SET_BP (new_ctx, (*lmf)->ebp);
900 MONO_CONTEXT_SET_IP (new_ctx, (*lmf)->eip);
901 memcpy (&new_ctx->regs, (*lmf)->iregs, sizeof (gulong) * MONO_SAVED_GREGS);
902 memcpy (&new_ctx->fregs, (*lmf)->fregs, sizeof (double) * MONO_SAVED_FREGS);
903 *lmf = (*lmf)->previous_lmf;
913 ves_icall_get_trace (MonoException *exc, gint32 skip, MonoBoolean need_file_info)
915 MonoDomain *domain = mono_domain_get ();
917 MonoArray *ta = exc->trace_ips;
920 len = mono_array_length (ta);
922 res = mono_array_new (domain, mono_defaults.stack_frame_class, len > skip ? len - skip : 0);
924 for (i = skip; i < len; i++) {
926 MonoStackFrame *sf = (MonoStackFrame *)mono_object_new (domain, mono_defaults.stack_frame_class);
927 gpointer ip = mono_array_get (ta, gpointer, i);
929 ji = mono_jit_info_table_find (domain, ip);
930 g_assert (ji != NULL);
932 sf->method = mono_method_get_object (domain, ji->method, NULL);
933 sf->native_offset = (char *)ip - (char *)ji->code_start;
935 sf->il_offset = mono_debug_il_offset_from_address (ji->method, sf->native_offset, domain);
937 if (need_file_info) {
940 filename = mono_debug_source_location_from_address (ji->method, sf->native_offset, &sf->line, domain);
942 sf->filename = filename? mono_string_new (domain, filename): NULL;
948 mono_array_set (res, gpointer, i, sf);
955 mono_jit_walk_stack (MonoStackWalk func, gpointer user_data) {
956 MonoDomain *domain = mono_domain_get ();
957 MonoJitTlsData *jit_tls = TlsGetValue (mono_jit_tls_id);
958 MonoLMF *lmf = jit_tls->lmf;
959 MonoJitInfo *ji, rji;
960 gint native_offset, il_offset;
962 MonoPPCStackFrame *sframe;
964 MonoContext ctx, new_ctx;
966 setup_context (&ctx);
967 setup_context (&new_ctx);
970 __asm__ volatile("lwz %0,0(r1)" : "=r" (sframe));
972 __asm__ volatile("lwz %0,0(1)" : "=r" (sframe));
974 MONO_CONTEXT_SET_IP (&ctx, sframe->lr);
975 MONO_CONTEXT_SET_BP (&ctx, sframe->sp);
977 while (MONO_CONTEXT_GET_BP (&ctx) < jit_tls->end_of_stack) {
979 ji = mono_arch_find_jit_info (domain, jit_tls, &rji, NULL, &ctx, &new_ctx, NULL, &lmf, &native_offset, &managed);
982 if (ji == (gpointer)-1)
985 il_offset = mono_debug_il_offset_from_address (ji->method, native_offset, domain);
987 if (func (ji->method, native_offset, il_offset, managed, user_data))
991 setup_context (&ctx);
996 ves_icall_get_frame_info (gint32 skip, MonoBoolean need_file_info,
997 MonoReflectionMethod **method,
998 gint32 *iloffset, gint32 *native_offset,
999 MonoString **file, gint32 *line, gint32 *column)
1001 MonoDomain *domain = mono_domain_get ();
1002 MonoJitTlsData *jit_tls = TlsGetValue (mono_jit_tls_id);
1003 MonoLMF *lmf = jit_tls->lmf;
1004 MonoJitInfo *ji, rji;
1005 MonoContext ctx, new_ctx;
1007 MONO_CONTEXT_SET_IP (&ctx, ves_icall_get_frame_info);
1008 MONO_CONTEXT_SET_BP (&ctx, __builtin_frame_address (0));
1013 ji = mono_arch_find_jit_info (domain, jit_tls, &rji, NULL, &ctx, &new_ctx, NULL, &lmf, native_offset, NULL);
1017 if (!ji || ji == (gpointer)-1 || MONO_CONTEXT_GET_BP (&ctx) >= jit_tls->end_of_stack)
1020 /* skip all wrappers ??*/
1021 if (ji->method->wrapper_type == MONO_WRAPPER_RUNTIME_INVOKE ||
1022 ji->method->wrapper_type == MONO_WRAPPER_REMOTING_INVOKE_WITH_CHECK ||
1023 ji->method->wrapper_type == MONO_WRAPPER_REMOTING_INVOKE)
1028 } while (skip >= 0);
1030 *method = mono_method_get_object (domain, ji->method, NULL);
1031 *iloffset = mono_debug_il_offset_from_address (ji->method, *native_offset, domain);
1033 if (need_file_info) {
1036 filename = mono_debug_source_location_from_address (ji->method, *native_offset, line, domain);
1038 *file = filename? mono_string_new (domain, filename): NULL;
1048 * This is the function called from the signal handler
1052 mono_arch_handle_exception (void *ctx, gpointer obj, gboolean test_only)
1054 struct ucontext *uc = ctx;
1058 mctx.sc_ir = uc->uc_mcontext->ss.srr0;
1059 mctx.sc_sp = uc->uc_mcontext->ss.r1;
1060 memcpy (&mctx.regs, &uc->uc_mcontext->ss.r13, sizeof (gulong) * MONO_SAVED_GREGS);
1061 memcpy (&mctx.fregs, &uc->uc_mcontext->fs.fpregs [14], sizeof (double) * MONO_SAVED_FREGS);
1063 result = arch_handle_exception (&mctx, obj, test_only);
1064 /* restore the context so that returning from the signal handler will invoke
1067 uc->uc_mcontext->ss.srr0 = mctx.sc_ir;
1068 uc->uc_mcontext->ss.r1 = mctx.sc_sp;
1069 memcpy (&uc->uc_mcontext->ss.r13, &mctx.regs, sizeof (gulong) * MONO_SAVED_GREGS);
1070 memcpy (&uc->uc_mcontext->fs.fpregs [14], &mctx.fregs, sizeof (double) * MONO_SAVED_FREGS);
1075 mono_arch_ip_from_context (void *sigctx)
1077 struct ucontext *uc = sigctx;
1078 return (gpointer)uc->uc_mcontext->ss.srr0;
1084 mono_arch_handle_exception (void *ctx, gpointer obj, gboolean test_only)
1086 struct ucontext *uc = ctx;
1090 mctx.sc_ir = uc->uc_mcontext.uc_regs->gregs [PT_NIP];
1091 mctx.sc_sp = uc->uc_mcontext.uc_regs->gregs [PT_R1];
1092 memcpy (&mctx.regs, &uc->uc_mcontext.uc_regs->gregs [PT_R13], sizeof (gulong) * MONO_SAVED_GREGS);
1093 memcpy (&mctx.fregs, &uc->uc_mcontext.uc_regs->fpregs.fpregs [14], sizeof (double) * MONO_SAVED_FREGS);
1095 result = arch_handle_exception (&mctx, obj, test_only);
1096 /* restore the context so that returning from the signal handler will invoke
1099 uc->uc_mcontext.uc_regs->gregs [PT_NIP] = mctx.sc_ir;
1100 uc->uc_mcontext.uc_regs->gregs [PT_R1] = mctx.sc_sp;
1101 memcpy (&uc->uc_mcontext.uc_regs->gregs [PT_R13], &mctx.regs, sizeof (gulong) * MONO_SAVED_GREGS);
1102 memcpy (&uc->uc_mcontext.uc_regs->fpregs.fpregs [14], &mctx.fregs, sizeof (double) * MONO_SAVED_FREGS);
1107 mono_arch_ip_from_context (void *sigctx)
1109 struct ucontext *uc = sigctx;
1110 return (gpointer)uc->uc_mcontext.uc_regs->gregs [PT_NIP];
1116 * arch_handle_exception:
1117 * @ctx: saved processor state
1118 * @obj: the exception object
1119 * @test_only: only test if the exception is caught, but dont call handlers
1124 arch_handle_exception (MonoContext *ctx, gpointer obj, gboolean test_only)
1126 MonoDomain *domain = mono_domain_get ();
1127 MonoJitInfo *ji, rji;
1128 static int (*call_filter) (MonoContext *, gpointer, gpointer) = NULL;
1129 MonoJitTlsData *jit_tls = TlsGetValue (mono_jit_tls_id);
1130 MonoLMF *lmf = jit_tls->lmf;
1131 GList *trace_ips = NULL;
1132 MonoException *mono_ex;
1133 MonoString *initial_stack_trace;
1135 g_assert (ctx != NULL);
1137 MonoException *ex = mono_get_exception_null_reference ();
1138 ex->message = mono_string_new (domain,
1139 "Object reference not set to an instance of an object");
1140 obj = (MonoObject *)ex;
1143 if (mono_object_isinst (obj, mono_defaults.exception_class)) {
1144 mono_ex = (MonoException*)obj;
1145 initial_stack_trace = mono_ex->stack_trace;
1152 call_filter = arch_get_call_filter ();
1154 g_assert (jit_tls->end_of_stack);
1155 g_assert (jit_tls->abort_func);
1158 MonoContext ctx_cp = *ctx;
1159 setup_context (&ctx_cp);
1160 if (mono_jit_trace_calls != NULL)
1161 g_print ("EXCEPTION handling: %s\n", mono_object_class (obj)->name);
1162 if (!arch_handle_exception (&ctx_cp, obj, TRUE)) {
1163 if (mono_break_on_exc)
1165 mono_unhandled_exception (obj);
1169 memset (&rji, 0, sizeof (rji));
1172 MonoContext new_ctx;
1175 setup_context (&new_ctx);
1176 ji = mono_arch_find_jit_info (domain, jit_tls, &rji, &rji, ctx, &new_ctx,
1177 test_only ? &trace : NULL, &lmf, NULL, NULL);
1179 g_warning ("Exception inside function without unwind info");
1180 g_assert_not_reached ();
1183 if (ji != (gpointer)-1) {
1185 if (test_only && ji->method->wrapper_type != MONO_WRAPPER_RUNTIME_INVOKE && mono_ex) {
1188 if (!initial_stack_trace) {
1189 trace_ips = g_list_append (trace_ips, MONO_CONTEXT_GET_IP (ctx));
1191 if (!mono_ex->stack_trace)
1192 strace = g_strdup ("");
1194 strace = mono_string_to_utf8 (mono_ex->stack_trace);
1196 tmp = g_strdup_printf ("%s%s\n", strace, trace);
1199 mono_ex->stack_trace = mono_string_new (domain, tmp);
1205 if (ji->num_clauses) {
1208 g_assert (ji->clauses);
1210 for (i = 0; i < ji->num_clauses; i++) {
1211 MonoJitExceptionInfo *ei = &ji->clauses [i];
1213 if (ei->try_start <= MONO_CONTEXT_GET_IP (ctx) &&
1214 MONO_CONTEXT_GET_IP (ctx) <= ei->try_end) {
1216 if ((ei->flags == MONO_EXCEPTION_CLAUSE_NONE &&
1217 mono_object_isinst (obj, mono_class_get (ji->method->klass->image, ei->data.token))) ||
1218 ((ei->flags == MONO_EXCEPTION_CLAUSE_FILTER &&
1219 call_filter (ctx, ei->data.filter, obj)))) {
1222 mono_ex->trace_ips = glist_to_array (trace_ips);
1223 g_list_free (trace_ips);
1227 if (mono_jit_trace_calls != NULL)
1228 g_print ("EXCEPTION: catch found at clause %d of %s\n", i, mono_method_full_name (ji->method, TRUE));
1229 /*printf ("stack for catch: %p\n", MONO_CONTEXT_GET_BP (ctx));*/
1230 MONO_CONTEXT_SET_IP (ctx, ei->handler_start);
1231 /* need to use the frame pointer (ppc_r31), not r1 (regs start from register r13): methods with clauses always have r31 */
1232 *((gpointer *)((char *)(ctx->regs [ppc_r31-13]) + ji->exvar_offset)) = obj;
1237 if (!test_only && ei->try_start <= MONO_CONTEXT_GET_IP (ctx) &&
1238 MONO_CONTEXT_GET_IP (ctx) < ei->try_end &&
1239 (ei->flags & MONO_EXCEPTION_CLAUSE_FINALLY)) {
1240 if (mono_jit_trace_calls != NULL)
1241 g_print ("EXCEPTION: finally clause %d of %s\n", i, mono_method_full_name (ji->method, TRUE));
1242 call_filter (ctx, ei->handler_start, NULL);
1253 setup_context (ctx);
1255 if ((ji == (gpointer)-1) || MONO_CONTEXT_GET_BP (ctx) >= jit_tls->end_of_stack) {
1258 jit_tls->abort_func (obj);
1259 g_assert_not_reached ();
1262 mono_ex->trace_ips = glist_to_array (trace_ips);
1263 g_list_free (trace_ips);
1269 g_assert_not_reached ();
1273 mono_arch_has_unwind_info (gconstpointer addr)