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.
16 #include <mono/arch/ppc/ppc-codegen.h>
17 #include <mono/metadata/appdomain.h>
18 #include <mono/metadata/tabledefs.h>
19 #include <mono/metadata/threads.h>
20 #include <mono/metadata/debug-helpers.h>
21 #include <mono/metadata/exception.h>
22 #include <mono/metadata/mono-debug.h>
28 typedef struct sigcontext MonoContext;
30 typedef struct ucontext MonoContext;
36 int sc_onstack; // sigstack state to restore
37 int sc_mask; // signal mask to restore
39 int sc_psw; // processor status word
40 int sc_sp; // stack pointer if sc_regs == NULL
41 void *sc_regs; // (kernel private) saved state
46 sigset_t uc_sigmask; // signal mask used by this context
47 stack_t uc_stack; // stack used by this context
48 struct ucontext *uc_link; // pointer to resuming context
49 size_t uc_mcsize; // size of the machine context passed in
50 mcontext_t uc_mcontext; // machine specific context
53 typedef struct ppc_exception_state {
54 unsigned long dar; // Fault registers for coredump
56 unsigned long exception;// number of powerpc exception taken
57 unsigned long pad0; // align to 16 bytes
59 unsigned long pad1[4]; // space in PCB "just in case"
60 } ppc_exception_state_t;
62 typedef struct ppc_vector_state {
63 unsigned long save_vr[32][4];
64 unsigned long save_vscr[4];
65 unsigned int save_pad5[4];
66 unsigned int save_vrvalid; // VRs that have been saved
67 unsigned int save_pad6[7];
70 typedef struct ppc_float_state {
73 unsigned int fpscr_pad; // fpscr is 64 bits, 32 bits of rubbish
74 unsigned int fpscr; // floating point status register
77 typedef struct ppc_thread_state {
78 unsigned int srr0; // Instruction address register (PC)
79 unsigned int srr1; // Machine state register (supervisor)
85 unsigned int cr; // Condition register
86 unsigned int xer; // User's integer exception register
87 unsigned int lr; // Link register
88 unsigned int ctr; // Count register
89 unsigned int mq; // MQ register (601 only)
91 unsigned int vrsave; // Vector Save Register
95 ppc_exception_state_t es;
96 ppc_thread_state_t ss;
98 ppc_vector_state_t vs;
101 typedef struct mcontext * mcontext_t;
103 Linux/PPC instead has:
105 unsigned long _unused[4];
107 unsigned long handler;
108 unsigned long oldmask;
109 struct pt_regs *regs;
112 unsigned long gpr[32];
115 unsigned long orig_gpr3; // Used for restarting system calls
120 unsigned long mq; // 601 only (not used at present)
121 // Used on APUS to hold IPL value.
122 unsigned long trap; // Reason for being here
123 // N.B. for critical exceptions on 4xx, the dar and dsisr
124 // fields are overloaded to hold srr0 and srr1.
125 unsigned long dar; // Fault registers
126 unsigned long dsisr; // on 4xx/Book-E used for ESR
127 unsigned long result; // Result of a system call
130 elf_gregset_t mc_gregs;
131 elf_fpregset_t mc_fregs;
132 unsigned long mc_pad[2];
133 elf_vrregset_t mc_vregs __attribute__((__aligned__(16)));
137 unsigned long uc_flags;
138 struct ucontext *uc_link;
141 struct mcontext *uc_regs; // points to uc_mcontext field
143 // glibc has 1024-bit signal masks, ours are 64-bit
146 struct mcontext uc_mcontext;
149 #define ELF_NGREG 48 // includes nip, msr, lr, etc.
150 #define ELF_NFPREG 33 // includes fpscr
153 typedef unsigned long elf_greg_t;
154 typedef elf_greg_t elf_gregset_t[ELF_NGREG];
156 // Floating point registers
157 typedef double elf_fpreg_t;
158 typedef elf_fpreg_t elf_fpregset_t[ELF_NFPREG];
165 /* we have the stack pointer, not the base pointer in sigcontext */
166 #define MONO_CONTEXT_SET_IP(ctx,ip) do { (ctx)->sc_ir = (int)ip; } while (0);
167 #define MONO_CONTEXT_SET_BP(ctx,bp) do { (ctx)->sc_sp = (int)bp; } while (0);
169 #define MONO_CONTEXT_GET_IP(ctx) ((gpointer)((ctx)->sc_ir))
170 #define MONO_CONTEXT_GET_BP(ctx) ((gpointer)((ctx)->sc_sp))
174 unsigned long unused1;
178 #else /* on linux/ppc we use ucontext. See /usr/include/asm/ptrace.h. */
180 #define MONO_CONTEXT_SET_IP(ctx,ip) do { (ctx)->uc_mcontext.uc_regs->gregs [PT_NIP] = (unsigned long)ip; } while (0);
181 #define MONO_CONTEXT_SET_BP(ctx,bp) do { (ctx)->uc_mcontext.uc_regs->gregs [PT_R1] = (unsigned long)bp; } while (0);
183 #define MONO_CONTEXT_GET_IP(ctx) ((gpointer)((ctx)->uc_mcontext.uc_regs->gregs [PT_NIP]))
184 #define MONO_CONTEXT_GET_BP(ctx) ((gpointer)((ctx)->uc_mcontext.uc_regs->gregs [PT_R1]))
193 /* disbale this for now */
194 #undef MONO_USE_EXC_TABLES
196 #ifdef MONO_USE_EXC_TABLES
198 /*************************************/
199 /* STACK UNWINDING STUFF */
200 /*************************************/
202 /* These definitions are from unwind-dw2.c in glibc 2.2.5 */
205 #define DWARF_FRAME_REGISTERS 17
207 typedef struct frame_state
213 long reg_or_offset[DWARF_FRAME_REGISTERS+1];
214 unsigned short cfa_reg;
215 unsigned short retaddr_column;
216 char saved[DWARF_FRAME_REGISTERS+1];
222 get_sigcontext_reg (struct sigcontext *ctx, int dwarf_regnum)
224 switch (dwarf_regnum) {
242 g_assert_not_reached ();
249 set_sigcontext_reg (struct sigcontext *ctx, int dwarf_regnum, long value)
251 switch (dwarf_regnum) {
280 g_assert_not_reached ();
284 typedef struct frame_state * (*framesf) (void *, struct frame_state *);
286 static framesf frame_state_for = NULL;
288 static gboolean inited = FALSE;
290 typedef char ** (*get_backtrace_symbols_type) (void *__const *__array, int __size);
292 static get_backtrace_symbols_type get_backtrace_symbols = NULL;
295 init_frame_state_for (void)
300 * There are two versions of __frame_state_for: one in libgcc.a and the
301 * other in glibc.so. We need the version from glibc.
302 * For more info, see this:
303 * http://gcc.gnu.org/ml/gcc/2002-08/msg00192.html
305 if ((module = g_module_open ("libc.so.6", G_MODULE_BIND_LAZY))) {
307 if (!g_module_symbol (module, "__frame_state_for", (gpointer*)&frame_state_for))
308 frame_state_for = NULL;
310 if (!g_module_symbol (module, "backtrace_symbols", (gpointer*)&get_backtrace_symbols)) {
311 get_backtrace_symbols = NULL;
312 frame_state_for = NULL;
315 g_module_close (module);
323 gboolean mono_arch_handle_exception (MonoContext *ctx, gpointer obj, gboolean test_only);
325 /* mono_arch_has_unwind_info:
327 * Tests if a function has an DWARF exception table able to restore
328 * all caller saved registers.
331 mono_arch_has_unwind_info (MonoMethod *method)
334 struct frame_state state_in;
335 struct frame_state *res;
338 init_frame_state_for ();
340 if (!frame_state_for)
343 g_assert (method->addr);
345 memset (&state_in, 0, sizeof (state_in));
347 /* offset 10 is just a guess, but it works for all methods tested */
348 if ((res = frame_state_for ((char *)method->addr + 10, &state_in))) {
350 if (res->saved [X86_EBX] != 1 ||
351 res->saved [X86_EDI] != 1 ||
352 res->saved [X86_EBP] != 1 ||
353 res->saved [X86_ESI] != 1) {
368 void *return_address;
372 ppc_unwind_native_frame (MonoDomain *domain, MonoJitTlsData *jit_tls, MonoContext *ctx,
373 MonoContext *new_ctx, MonoLMF *lmf, char **trace)
376 struct stack_frame *frame;
379 struct frame_state state_in;
380 struct frame_state *res;
386 init_frame_state_for ();
388 if (!frame_state_for)
391 frame = MONO_CONTEXT_GET_BP (ctx);
393 max_stack = lmf && lmf->method ? lmf : jit_tls->end_of_stack;
397 memset (&state_in, 0, sizeof (state_in));
399 while ((gpointer)frame->next < (gpointer)max_stack) {
400 gpointer ip, addr = frame->return_address;
402 char *tmp, **symbols;
405 ip = MONO_CONTEXT_GET_IP (new_ctx);
406 symbols = get_backtrace_symbols (&ip, 1);
408 tmp = g_strdup_printf ("%s\nin (unmanaged) %s", *trace, symbols [0]);
410 tmp = g_strdup_printf ("in (unmanaged) %s", symbols [0]);
417 if ((res = frame_state_for (addr, &state_in))) {
420 cfa = (gint8*) (get_sigcontext_reg (new_ctx, res->cfa_reg) + res->cfa_offset);
421 frame = (struct stack_frame *)((gint8*)cfa - 8);
422 for (i = 0; i < DWARF_FRAME_REGISTERS + 1; i++) {
423 int how = res->saved[i];
425 g_assert ((how == 0) || (how == 1));
428 val = * (long*) ((gint8*)cfa + res->reg_or_offset[i]);
429 set_sigcontext_reg (new_ctx, i, val);
432 new_ctx->SC_ESP = (long)cfa;
434 if (res->saved [X86_EBX] == 1 &&
435 res->saved [X86_EDI] == 1 &&
436 res->saved [X86_EBP] == 1 &&
437 res->saved [X86_ESI] == 1 &&
438 (ji = mono_jit_info_table_find (domain, frame->return_address))) {
439 //printf ("FRAME CFA %s\n", mono_method_full_name (ji->method, TRUE));
444 //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)));
446 MONO_CONTEXT_SET_IP (new_ctx, frame->return_address);
448 MONO_CONTEXT_SET_BP (new_ctx, frame);
450 /* stop if !frame or when we detect an unexpected managed frame */
451 if (!frame || mono_jit_info_table_find (domain, frame->return_address)) {
462 //g_assert_not_reached ();
475 /* FIXME: restore the rest of the registers */
476 #define restore_regs_from_context(ctx_reg,ip_reg,tmp_reg) do { \
477 ppc_lwz (code, ip_reg, G_STRUCT_OFFSET (struct sigcontext, sc_ir), ctx_reg); \
478 ppc_lwz (code, ppc_sp, G_STRUCT_OFFSET (struct sigcontext, sc_sp), ctx_reg); \
482 #define setup_context(ctx)
486 * The registers can be at different offsets in the ucontext,
487 * so we need to reference them from uctx->uc_mcontext.uc_regs
489 /* return the offset within uc_regs of register regnum (PT_*) */
490 #define regoffset(regnum) (G_STRUCT_OFFSET (mcontext_t, gregs) + (regnum) * sizeof (unsigned long))
492 /* FIXME: restore the rest of the registers */
493 #define restore_regs_from_context(ctx_reg,ip_reg,tmp_reg) do { \
494 ppc_lwz (code, tmp_reg, G_STRUCT_OFFSET (struct ucontext, uc_mcontext.uc_regs), ctx_reg); \
495 ppc_lwz (code, ip_reg, regoffset (PT_NIP), tmp_reg); \
496 ppc_lwz (code, ppc_sp, regoffset (PT_R1), tmp_reg); \
497 ppc_lmw (code, ppc_r13, tmp_reg, regoffset (PT_R13)); \
500 /* yes, very ugly, but we need to setup the pointer, since
501 * the mcontext could be at different offsets in the ucontext
502 * generated by the kernel.
504 #define setup_context(ctx) do { \
505 (ctx)->uc_mcontext.uc_regs = (mcontext_t*)&(ctx)->uc_reg_space; \
510 * arch_get_restore_context:
512 * Returns a pointer to a method which restores a previously saved sigcontext.
513 * The first argument in r3 is the pointer to the context.
516 arch_get_restore_context (void)
519 static guint8 start [64];
520 static int inited = 0;
527 restore_regs_from_context (ppc_r3, ppc_r4, ppc_r5);
529 /* jump to the saved IP */
530 ppc_mtctr (code, ppc_r4);
531 ppc_bcctr (code, PPC_BR_ALWAYS, 0);
535 g_assert ((code - start) < sizeof(start));
540 * arch_get_call_filter:
542 * Returns a pointer to a method which calls an exception filter. We
543 * also use this function to call finally handlers (we pass NULL as
544 * @exc object in this case).
547 arch_get_call_filter (void)
549 static guint8 start [256];
550 static int inited = 0;
552 int alloc_size, pos, i;
558 /* call_filter (MonoContext *ctx, unsigned long eip, gpointer exc) */
561 ppc_mflr (code, ppc_r0);
562 ppc_stw (code, ppc_r0, PPC_RET_ADDR_OFFSET, ppc_sp);
563 alloc_size = 32 + PPC_MINIMAL_STACK_SIZE + (sizeof (gulong) * 19 + sizeof (gdouble) * 18);
564 // align to PPC_STACK_ALIGNMENT bytes
565 if (alloc_size & (PPC_STACK_ALIGNMENT - 1))
566 alloc_size += PPC_STACK_ALIGNMENT - (alloc_size & (PPC_STACK_ALIGNMENT - 1));
567 g_assert ((alloc_size & (PPC_STACK_ALIGNMENT-1)) == 0);
568 ppc_stwu (code, ppc_sp, -alloc_size, ppc_sp);
569 /* save all the regs on the stack */
570 pos = 32 + PPC_MINIMAL_STACK_SIZE;
571 ppc_stmw (code, ppc_r13, ppc_sp, pos);
572 pos += sizeof (gulong) * 19;
573 for (i = 14; i < 32; ++i) {
574 ppc_stfd (code, i, pos, ppc_sp);
575 pos += sizeof (gdouble);
577 /* restore all the regs from ctx (in r3) */
578 /* FIXME: calling the filter code must not restore the stack pointer */
579 restore_regs_from_context (ppc_r3, ppc_r6, ppc_r7);
580 /* call handler at eip (r4) and set the first arg with the exception (r5) */
581 ppc_mtctr (code, ppc_r4);
582 ppc_mr (code, ppc_r3, ppc_r5);
583 ppc_bcctrl (code, PPC_BR_ALWAYS, 0);
584 /* restore all the regs from the stack */
585 pos = 32 + PPC_MINIMAL_STACK_SIZE;
586 ppc_lmw (code, ppc_r13, ppc_sp, pos);
587 pos += sizeof (gulong) * 19;
588 for (i = 14; i < 32; ++i) {
589 ppc_lfd (code, i, pos, ppc_sp);
590 pos += sizeof (gdouble);
592 ppc_lwz (code, ppc_r0, alloc_size + PPC_RET_ADDR_OFFSET, ppc_sp);
593 ppc_mtlr (code, ppc_r0);
594 ppc_addic (code, ppc_sp, ppc_sp, alloc_size);
596 g_assert ((code - start) < sizeof(start));
601 throw_exception (MonoObject *exc, unsigned long eip, unsigned long esp, gulong *int_regs, gdouble *fp_regs)
603 static void (*restore_context) (MonoContext *);
606 if (!restore_context)
607 restore_context = arch_get_restore_context ();
609 /* adjust eip so that it point into the call instruction */
612 setup_context (&ctx);
614 /*printf ("stack in throw: %p\n", esp);*/
615 MONO_CONTEXT_SET_BP (&ctx, esp);
616 MONO_CONTEXT_SET_IP (&ctx, eip);
620 memcpy (((char*)ctx.uc_mcontext.uc_regs) + regoffset (PT_R13), int_regs, sizeof (gulong) * 19);
623 mono_arch_handle_exception (&ctx, exc, FALSE);
624 restore_context (&ctx);
626 g_assert_not_reached ();
630 * arch_get_throw_exception_generic:
632 * Returns a function pointer which can be used to raise
633 * exceptions. The returned function has the following
634 * signature: void (*func) (MonoException *exc); or
635 * void (*func) (char *exc_name);
639 mono_arch_get_throw_exception_generic (guint8 *start, int size, int by_name)
642 int alloc_size, pos, i;
646 ppc_mflr (code, ppc_r0);
647 ppc_stw (code, ppc_r0, PPC_RET_ADDR_OFFSET, ppc_sp);
648 alloc_size = 32 + PPC_MINIMAL_STACK_SIZE + (sizeof (gulong) * 19 + sizeof (gdouble) * 18);
649 // align to PPC_STACK_ALIGNMENT bytes
650 if (alloc_size & (PPC_STACK_ALIGNMENT - 1)) {
651 alloc_size += PPC_STACK_ALIGNMENT - 1;
652 alloc_size &= ~(PPC_STACK_ALIGNMENT - 1);
654 /*g_print ("alloc size = %d\n", alloc_size);*/
655 g_assert ((alloc_size & (PPC_STACK_ALIGNMENT-1)) == 0);
656 ppc_stwu (code, ppc_sp, -alloc_size, ppc_sp);
659 ppc_mr (code, ppc_r5, ppc_r3);
660 ppc_load (code, ppc_r3, mono_defaults.corlib);
661 ppc_load (code, ppc_r4, "System");
663 ppc_patch (code - 4, mono_exception_from_name);
665 /* save all the regs on the stack */
666 pos = 32 + PPC_MINIMAL_STACK_SIZE;
667 ppc_stmw (code, ppc_r13, ppc_sp, pos);
668 pos += sizeof (gulong) * 19;
669 /* align for doubles */
672 for (i = 14; i < 32; ++i) {
673 ppc_stfd (code, i, pos, ppc_sp);
674 pos += sizeof (gdouble);
676 /* call throw_exception (exc, ip, sp, int_regs, fp_regs) */
678 ppc_lwz (code, ppc_r5, 0, ppc_sp);
679 /* exc is already in place in r3 */
681 ppc_lwz (code, ppc_r4, PPC_RET_ADDR_OFFSET, ppc_r5);
683 ppc_mr (code, ppc_r4, ppc_r0); /* caller ip */
684 /* pointer to the saved int regs */
685 pos = 32 + PPC_MINIMAL_STACK_SIZE;
686 ppc_addi (code, ppc_r6, ppc_sp, pos);
687 /* pointer to the saved fp regs */
688 pos += sizeof (gulong) * 19;
689 ppc_addi (code, ppc_r7, ppc_sp, pos);
691 ppc_patch (code - 4, throw_exception);
692 /* we should never reach this breakpoint */
694 g_assert ((code - start) < size);
699 * arch_get_throw_exception:
701 * Returns a function pointer which can be used to raise
702 * exceptions. The returned function has the following
703 * signature: void (*func) (MonoException *exc);
704 * For example to raise an arithmetic exception you can use:
706 * x86_push_imm (code, mono_get_exception_arithmetic ());
707 * x86_call_code (code, arch_get_throw_exception ());
711 mono_arch_get_throw_exception (void)
713 static guint8 start [128];
714 static int inited = 0;
718 mono_arch_get_throw_exception_generic (start, sizeof (start), FALSE);
724 * arch_get_throw_exception_by_name:
726 * Returns a function pointer which can be used to raise
727 * corlib exceptions. The returned function has the following
728 * signature: void (*func) (char *exc_name);
729 * For example to raise an arithmetic exception you can use:
731 * x86_push_imm (code, "ArithmeticException");
732 * x86_call_code (code, arch_get_throw_exception_by_name ());
736 mono_arch_get_throw_exception_by_name (void)
738 static guint8 start [160];
739 static int inited = 0;
743 mono_arch_get_throw_exception_generic (start, sizeof (start), TRUE);
749 glist_to_array (GList *list)
751 MonoDomain *domain = mono_domain_get ();
758 len = g_list_length (list);
759 res = mono_array_new (domain, mono_defaults.int_class, len);
761 for (i = 0; list; list = list->next, i++)
762 mono_array_set (res, gpointer, i, list->data);
767 /* mono_arch_find_jit_info:
769 * This function is used to gather information from @ctx. It return the
770 * MonoJitInfo of the corresponding function, unwinds one stack frame and
771 * stores the resulting context into @new_ctx. It also stores a string
772 * describing the stack location into @trace (if not NULL), and modifies
773 * the @lmf if necessary. @native_offset return the IP offset from the
774 * start of the function or -1 if that info is not available.
777 mono_arch_find_jit_info (MonoDomain *domain, MonoJitTlsData *jit_tls, MonoJitInfo *res, MonoContext *ctx,
778 MonoContext *new_ctx, char **trace, MonoLMF **lmf, int *native_offset,
782 gpointer ip = MONO_CONTEXT_GET_IP (ctx);
785 MonoPPCStackFrame *sframe;
787 ji = mono_jit_info_table_find (domain, ip);
799 char *source_location, *tmpaddr, *fname;
800 gint32 address, iloffset;
805 setup_context (new_ctx);
807 if (*lmf && (MONO_CONTEXT_GET_BP (ctx) >= (gpointer)(*lmf)->ebp)) {
808 /* remove any unused lmf */
809 *lmf = (*lmf)->previous_lmf;
812 address = (char *)ip - (char *)ji->code_start;
815 *native_offset = address;
818 if (!ji->method->wrapper_type)
822 source_location = mono_debug_source_location_from_address (ji->method, address, NULL, domain);
823 iloffset = mono_debug_il_offset_from_address (ji->method, address, domain);
826 tmpaddr = g_strdup_printf ("<0x%05x>", address);
828 tmpaddr = g_strdup_printf ("[0x%05x]", iloffset);
830 fname = mono_method_full_name (ji->method, TRUE);
833 *trace = g_strdup_printf ("in %s (at %s) %s", tmpaddr, source_location, fname);
835 *trace = g_strdup_printf ("in %s %s", tmpaddr, fname);
838 g_free (source_location);
843 /* restore caller saved registers */
844 if (ji->used_regs & X86_EBX_MASK) {
845 new_ctx->SC_EBX = *((int *)ctx->SC_EBP + offset);
848 if (ji->used_regs & X86_EDI_MASK) {
849 new_ctx->SC_EDI = *((int *)ctx->SC_EBP + offset);
852 if (ji->used_regs & X86_ESI_MASK) {
853 new_ctx->SC_ESI = *((int *)ctx->SC_EBP + offset);
856 new_ctx->SC_ESP = ctx->SC_EBP;
857 /* we substract 1, so that the IP points into the call instruction */
858 new_ctx->SC_EIP = *((int *)ctx->SC_EBP + 1) - 1;
859 new_ctx->SC_EBP = *((int *)ctx->SC_EBP);
861 sframe = (MonoPPCStackFrame*)MONO_CONTEXT_GET_BP (ctx);
862 MONO_CONTEXT_SET_BP (new_ctx, sframe->sp);
864 /* keep updated with emit_prolog in mini-ppc.c */
866 for (i = 13; i < 32; ++i) {
867 if (ji->used_regs & (1 << i)) {
868 offset += sizeof (gulong);
872 *(gulong*)(((char*)new_ctx->uc_mcontext.uc_regs) + regoffset (PT_R0 + i)) = *(gulong*)((char*)sframe->sp - offset);
877 /* the calling IP is in the parent frame */
878 sframe = (MonoPPCStackFrame*)sframe->sp;
879 /* we substract 4, so that the IP points into the call instruction */
880 MONO_CONTEXT_SET_IP (new_ctx, sframe->lr - 4);
883 #ifdef MONO_USE_EXC_TABLES
884 } else if ((ji = ppc_unwind_native_frame (domain, jit_tls, ctx, new_ctx, *lmf, trace))) {
891 setup_context (new_ctx);
897 *trace = g_strdup_printf ("in (unmanaged) %s", mono_method_full_name ((*lmf)->method, TRUE));
899 if ((ji = mono_jit_info_table_find (domain, (gpointer)(*lmf)->eip))) {
902 memset (res, 0, sizeof (MonoJitInfo));
903 res->method = (*lmf)->method;
907 new_ctx->SC_ESI = (*lmf)->esi;
908 new_ctx->SC_EDI = (*lmf)->edi;
909 new_ctx->SC_EBX = (*lmf)->ebx;
910 new_ctx->SC_EBP = (*lmf)->ebp;
911 new_ctx->SC_EIP = (*lmf)->eip;
912 /* the lmf is always stored on the stack, so the following
913 * expression points to a stack location which can be used as ESP */
914 new_ctx->SC_ESP = (unsigned long)&((*lmf)->eip);
916 /*sframe = (MonoPPCStackFrame*)MONO_CONTEXT_GET_BP (ctx);
917 MONO_CONTEXT_SET_BP (new_ctx, sframe->sp);
918 MONO_CONTEXT_SET_IP (new_ctx, sframe->lr);*/
919 MONO_CONTEXT_SET_BP (new_ctx, (*lmf)->ebp);
920 MONO_CONTEXT_SET_IP (new_ctx, (*lmf)->eip);
924 memcpy (((char*)new_ctx->uc_mcontext.uc_regs) + regoffset (PT_R13), (*lmf)->iregs, sizeof (gulong) * 19);
926 *lmf = (*lmf)->previous_lmf;
936 ves_icall_get_trace (MonoException *exc, gint32 skip, MonoBoolean need_file_info)
938 MonoDomain *domain = mono_domain_get ();
940 MonoArray *ta = exc->trace_ips;
943 len = mono_array_length (ta);
945 res = mono_array_new (domain, mono_defaults.stack_frame_class, len > skip ? len - skip : 0);
947 for (i = skip; i < len; i++) {
949 MonoStackFrame *sf = (MonoStackFrame *)mono_object_new (domain, mono_defaults.stack_frame_class);
950 gpointer ip = mono_array_get (ta, gpointer, i);
952 ji = mono_jit_info_table_find (domain, ip);
953 g_assert (ji != NULL);
955 sf->method = mono_method_get_object (domain, ji->method, NULL);
956 sf->native_offset = (char *)ip - (char *)ji->code_start;
958 sf->il_offset = mono_debug_il_offset_from_address (ji->method, sf->native_offset, domain);
960 if (need_file_info) {
963 filename = mono_debug_source_location_from_address (ji->method, sf->native_offset, &sf->line, domain);
965 sf->filename = filename? mono_string_new (domain, filename): NULL;
971 mono_array_set (res, gpointer, i, sf);
978 mono_jit_walk_stack (MonoStackWalk func, gpointer user_data) {
979 MonoDomain *domain = mono_domain_get ();
980 MonoJitTlsData *jit_tls = TlsGetValue (mono_jit_tls_id);
981 MonoLMF *lmf = jit_tls->lmf;
982 MonoJitInfo *ji, rji;
983 gint native_offset, il_offset;
985 MonoPPCStackFrame *sframe;
987 MonoContext ctx, new_ctx;
989 setup_context (&ctx);
990 setup_context (&new_ctx);
993 __asm__ volatile("lwz %0,0(r1)" : "=r" (sframe));
995 __asm__ volatile("lwz %0,0(1)" : "=r" (sframe));
997 MONO_CONTEXT_SET_IP (&ctx, sframe->lr);
998 MONO_CONTEXT_SET_BP (&ctx, sframe->sp);
1000 while (MONO_CONTEXT_GET_BP (&ctx) < jit_tls->end_of_stack) {
1002 ji = mono_arch_find_jit_info (domain, jit_tls, &rji, &ctx, &new_ctx, NULL, &lmf, &native_offset, &managed);
1005 if (ji == (gpointer)-1)
1008 il_offset = mono_debug_il_offset_from_address (ji->method, native_offset, domain);
1010 if (func (ji->method, native_offset, il_offset, managed, user_data))
1014 setup_context (&ctx);
1019 ves_icall_get_frame_info (gint32 skip, MonoBoolean need_file_info,
1020 MonoReflectionMethod **method,
1021 gint32 *iloffset, gint32 *native_offset,
1022 MonoString **file, gint32 *line, gint32 *column)
1024 MonoDomain *domain = mono_domain_get ();
1025 MonoJitTlsData *jit_tls = TlsGetValue (mono_jit_tls_id);
1026 MonoLMF *lmf = jit_tls->lmf;
1027 MonoJitInfo *ji, rji;
1028 MonoContext ctx, new_ctx;
1030 MONO_CONTEXT_SET_IP (&ctx, ves_icall_get_frame_info);
1031 MONO_CONTEXT_SET_BP (&ctx, __builtin_frame_address (0));
1036 ji = mono_arch_find_jit_info (domain, jit_tls, &rji, &ctx, &new_ctx, NULL, &lmf, native_offset, NULL);
1040 if (!ji || ji == (gpointer)-1 || MONO_CONTEXT_GET_BP (&ctx) >= jit_tls->end_of_stack)
1043 /* skip all wrappers ??*/
1044 if (ji->method->wrapper_type == MONO_WRAPPER_RUNTIME_INVOKE ||
1045 ji->method->wrapper_type == MONO_WRAPPER_REMOTING_INVOKE_WITH_CHECK ||
1046 ji->method->wrapper_type == MONO_WRAPPER_REMOTING_INVOKE)
1051 } while (skip >= 0);
1053 *method = mono_method_get_object (domain, ji->method, NULL);
1054 *iloffset = mono_debug_il_offset_from_address (ji->method, *native_offset, domain);
1056 if (need_file_info) {
1059 filename = mono_debug_source_location_from_address (ji->method, *native_offset, line, domain);
1061 *file = filename? mono_string_new (domain, filename): NULL;
1071 * arch_handle_exception:
1072 * @ctx: saved processor state
1073 * @obj: the exception object
1074 * @test_only: only test if the exception is caught, but dont call handlers
1079 mono_arch_handle_exception (MonoContext *ctx, gpointer obj, gboolean test_only)
1081 MonoDomain *domain = mono_domain_get ();
1082 MonoJitInfo *ji, rji;
1083 static int (*call_filter) (MonoContext *, gpointer, gpointer) = NULL;
1084 MonoJitTlsData *jit_tls = TlsGetValue (mono_jit_tls_id);
1085 MonoLMF *lmf = jit_tls->lmf;
1086 GList *trace_ips = NULL;
1087 MonoException *mono_ex;
1088 MonoString *initial_stack_trace;
1090 g_assert (ctx != NULL);
1092 MonoException *ex = mono_get_exception_null_reference ();
1093 ex->message = mono_string_new (domain,
1094 "Object reference not set to an instance of an object");
1095 obj = (MonoObject *)ex;
1098 if (mono_object_isinst (obj, mono_defaults.exception_class)) {
1099 mono_ex = (MonoException*)obj;
1100 initial_stack_trace = mono_ex->stack_trace;
1107 call_filter = arch_get_call_filter ();
1109 g_assert (jit_tls->end_of_stack);
1110 g_assert (jit_tls->abort_func);
1113 MonoContext ctx_cp = *ctx;
1114 setup_context (&ctx_cp);
1115 if (mono_jit_trace_calls != NULL)
1116 g_print ("EXCEPTION handling: %s\n", mono_object_class (obj)->name);
1117 if (!mono_arch_handle_exception (&ctx_cp, obj, TRUE)) {
1118 if (mono_break_on_exc)
1120 mono_unhandled_exception (obj);
1125 MonoContext new_ctx;
1128 setup_context (&new_ctx);
1129 ji = mono_arch_find_jit_info (domain, jit_tls, &rji, ctx, &new_ctx,
1130 test_only ? &trace : NULL, &lmf, NULL, NULL);
1132 g_warning ("Exception inside function without unwind info");
1133 g_assert_not_reached ();
1136 if (ji != (gpointer)-1) {
1138 if (test_only && ji->method->wrapper_type != MONO_WRAPPER_RUNTIME_INVOKE && mono_ex) {
1141 if (!initial_stack_trace) {
1142 trace_ips = g_list_append (trace_ips, MONO_CONTEXT_GET_IP (ctx));
1144 if (!mono_ex->stack_trace)
1145 strace = g_strdup ("");
1147 strace = mono_string_to_utf8 (mono_ex->stack_trace);
1149 tmp = g_strdup_printf ("%s%s\n", strace, trace);
1152 mono_ex->stack_trace = mono_string_new (domain, tmp);
1158 if (ji->num_clauses) {
1161 g_assert (ji->clauses);
1163 for (i = 0; i < ji->num_clauses; i++) {
1164 MonoJitExceptionInfo *ei = &ji->clauses [i];
1166 if (ei->try_start <= MONO_CONTEXT_GET_IP (ctx) &&
1167 MONO_CONTEXT_GET_IP (ctx) <= ei->try_end) {
1169 if ((ei->flags == MONO_EXCEPTION_CLAUSE_NONE &&
1170 mono_object_isinst (obj, mono_class_get (ji->method->klass->image, ei->data.token))) ||
1171 ((ei->flags == MONO_EXCEPTION_CLAUSE_FILTER &&
1172 call_filter (ctx, ei->data.filter, obj)))) {
1175 mono_ex->trace_ips = glist_to_array (trace_ips);
1176 g_list_free (trace_ips);
1180 if (mono_jit_trace_calls != NULL)
1181 g_print ("EXCEPTION: catch found at clause %d of %s\n", i, mono_method_full_name (ji->method, TRUE));
1182 /*printf ("stack for catch: %p\n", MONO_CONTEXT_GET_BP (ctx));*/
1183 MONO_CONTEXT_SET_IP (ctx, ei->handler_start);
1184 *((gpointer *)((char *)MONO_CONTEXT_GET_BP (ctx) + ji->exvar_offset)) = obj;
1189 if (!test_only && ei->try_start <= MONO_CONTEXT_GET_IP (ctx) &&
1190 MONO_CONTEXT_GET_IP (ctx) < ei->try_end &&
1191 (ei->flags & MONO_EXCEPTION_CLAUSE_FINALLY)) {
1192 if (mono_jit_trace_calls != NULL)
1193 g_print ("EXCEPTION: finally clause %d of %s\n", i, mono_method_full_name (ji->method, TRUE));
1194 call_filter (ctx, ei->handler_start, NULL);
1205 setup_context (ctx);
1207 if ((ji == (gpointer)-1) || MONO_CONTEXT_GET_BP (ctx) >= jit_tls->end_of_stack) {
1210 jit_tls->abort_func (obj);
1211 g_assert_not_reached ();
1214 mono_ex->trace_ips = glist_to_array (trace_ips);
1215 g_list_free (trace_ips);
1221 g_assert_not_reached ();
1225 mono_arch_has_unwind_info (gconstpointer addr)