56a8cab42054f2af459793763562c304bd27aa5d
[mono.git] / mono / mini / exceptions-ppc.c
1 /*
2  * exceptions-ppc.c: exception support for PowerPC
3  *
4  * Authors:
5  *   Dietmar Maurer (dietmar@ximian.com)
6  *   Paolo Molaro (lupus@ximian.com)
7  *
8  * (C) 2001 Ximian, Inc.
9  */
10
11 #include <config.h>
12 #include <glib.h>
13 #include <signal.h>
14 #include <string.h>
15 #include <ucontext.h>
16
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>
24
25 #include "mini.h"
26 #include "mini-ppc.h"
27
28 static gboolean arch_handle_exception (MonoContext *ctx, gpointer obj, gboolean test_only);
29
30 /*
31
32 struct sigcontext {
33     int      sc_onstack;     // sigstack state to restore 
34     int      sc_mask;        // signal mask to restore 
35     int      sc_ir;          // pc 
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 
39 };
40
41 struct ucontext {
42         int             uc_onstack;
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 
48 };
49
50 typedef struct ppc_exception_state {
51         unsigned long dar;      // Fault registers for coredump 
52         unsigned long dsisr;
53         unsigned long exception;// number of powerpc exception taken 
54         unsigned long pad0;     // align to 16 bytes 
55
56         unsigned long pad1[4];  // space in PCB "just in case" 
57 } ppc_exception_state_t;
58
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];
65 } ppc_vector_state_t;
66
67 typedef struct ppc_float_state {
68         double  fpregs[32];
69
70         unsigned int fpscr_pad; // fpscr is 64 bits, 32 bits of rubbish 
71         unsigned int fpscr;     // floating point status register 
72 } ppc_float_state_t;
73
74 typedef struct ppc_thread_state {
75         unsigned int srr0;      // Instruction address register (PC) 
76         unsigned int srr1;      // Machine state register (supervisor) 
77         unsigned int r0;
78         unsigned int r1;
79         unsigned int r2;
80         ... 
81         unsigned int r31;
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) 
87
88         unsigned int vrsave;    // Vector Save Register 
89 } ppc_thread_state_t;
90
91 struct mcontext {
92         ppc_exception_state_t   es;
93         ppc_thread_state_t      ss;
94         ppc_float_state_t       fs;
95         ppc_vector_state_t      vs;
96 };
97
98 typedef struct mcontext  * mcontext_t;
99
100 Linux/PPC instead has:
101 struct sigcontext {
102         unsigned long   _unused[4];
103         int             signal;
104         unsigned long   handler;
105         unsigned long   oldmask;
106         struct pt_regs  *regs;
107 };
108 struct pt_regs {
109         unsigned long gpr[32];
110         unsigned long nip;
111         unsigned long msr;
112         unsigned long orig_gpr3;        // Used for restarting system calls 
113         unsigned long ctr;
114         unsigned long link;
115         unsigned long xer;
116         unsigned long ccr;
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 
125 };
126 struct mcontext {
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)));
131 };
132
133 struct ucontext {
134         unsigned long    uc_flags;
135         struct ucontext *uc_link;
136         stack_t          uc_stack;
137         int              uc_pad[7];
138         struct mcontext *uc_regs;       // points to uc_mcontext field 
139         sigset_t         uc_sigmask;
140         // glibc has 1024-bit signal masks, ours are 64-bit 
141         int              uc_maskext[30];
142         int              uc_pad2[3];
143         struct mcontext  uc_mcontext;
144 };
145
146 #define ELF_NGREG       48      // includes nip, msr, lr, etc. 
147 #define ELF_NFPREG      33      // includes fpscr 
148
149 // General registers 
150 typedef unsigned long elf_greg_t;
151 typedef elf_greg_t elf_gregset_t[ELF_NGREG];
152
153 // Floating point registers 
154 typedef double elf_fpreg_t;
155 typedef elf_fpreg_t elf_fpregset_t[ELF_NFPREG];
156
157
158 */
159
160
161 #define restore_regs_from_context(ctx_reg,ip_reg,tmp_reg) do {  \
162                 int reg;        \
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);  \
167                 }       \
168         } while (0)
169
170 /* nothing to do */
171 #define setup_context(ctx)
172
173 /*
174  * arch_get_restore_context:
175  *
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.
178  */
179 static gpointer
180 arch_get_restore_context (void)
181 {
182         guint8 *code;
183         static guint8 start [128];
184         static int inited = 0;
185
186         if (inited)
187                 return start;
188         inited = 1;
189
190         code = start;
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);
194         //ppc_break (code);
195         /* jump to the saved IP */
196         ppc_mtctr (code, ppc_r4);
197         ppc_bcctr (code, PPC_BR_ALWAYS, 0);
198         /* never reached */
199         ppc_break (code);
200
201         g_assert ((code - start) < sizeof(start));
202         return start;
203 }
204
205 /*
206  * arch_get_call_filter:
207  *
208  * Returns a pointer to a method which calls an exception filter. We
209  * also use this function to call finally handlers (we pass NULL as 
210  * @exc object in this case).
211  */
212 static gpointer
213 arch_get_call_filter (void)
214 {
215         static guint8 start [320];
216         static int inited = 0;
217         guint8 *code;
218         int alloc_size, pos, i;
219
220         if (inited)
221                 return start;
222
223         inited = 1;
224         /* call_filter (MonoContext *ctx, unsigned long eip, gpointer exc) */
225         code = start;
226
227         /* save all the regs on the stack */
228         pos = 0;
229         for (i = 31; i >= 14; --i) {
230                 pos += sizeof (gdouble);
231                 ppc_stfd (code, i, -pos, ppc_sp);
232         }
233         pos += sizeof (gulong) * MONO_SAVED_GREGS;
234         ppc_stmw (code, ppc_r13, ppc_sp, -pos);
235
236         ppc_mflr (code, ppc_r0);
237         ppc_stw (code, ppc_r0, PPC_RET_ADDR_OFFSET, ppc_sp);
238
239         alloc_size = PPC_MINIMAL_STACK_SIZE + pos + 64;
240         // align to PPC_STACK_ALIGNMENT bytes
241         alloc_size += PPC_STACK_ALIGNMENT - 1;
242         alloc_size &= ~(PPC_STACK_ALIGNMENT - 1);
243
244         g_assert ((alloc_size & (PPC_STACK_ALIGNMENT-1)) == 0);
245         ppc_stwu (code, ppc_sp, -alloc_size, ppc_sp);
246
247         /* restore all the regs from ctx (in r3), but not r1, the stack pointer */
248         restore_regs_from_context (ppc_r3, ppc_r6, ppc_r7);
249         /* call handler at eip (r4) and set the first arg with the exception (r5) */
250         ppc_mtctr (code, ppc_r4);
251         ppc_mr (code, ppc_r3, ppc_r5);
252         ppc_bcctrl (code, PPC_BR_ALWAYS, 0);
253
254         /* epilog */
255         ppc_lwz (code, ppc_r0, alloc_size + PPC_RET_ADDR_OFFSET, ppc_sp);
256         ppc_mtlr (code, ppc_r0);
257         ppc_addic (code, ppc_sp, ppc_sp, alloc_size);
258         
259         /* restore all the regs from the stack */
260         pos = 0;
261         for (i = 31; i >= 14; --i) {
262                 pos += sizeof (double);
263                 ppc_lfd (code, i, -pos, ppc_sp);
264         }
265         pos += sizeof (gulong) * MONO_SAVED_GREGS;
266         ppc_lmw (code, ppc_r13, ppc_sp, -pos);
267
268         ppc_blr (code);
269
270         g_assert ((code - start) < sizeof(start));
271         return start;
272 }
273
274 static void
275 throw_exception (MonoObject *exc, unsigned long eip, unsigned long esp, gulong *int_regs, gdouble *fp_regs, gboolean rethrow)
276 {
277         static void (*restore_context) (MonoContext *);
278         MonoContext ctx;
279
280         if (!restore_context)
281                 restore_context = arch_get_restore_context ();
282
283         /* adjust eip so that it point into the call instruction */
284         eip -= 4;
285
286         setup_context (&ctx);
287
288         /*printf ("stack in throw: %p\n", esp);*/
289         MONO_CONTEXT_SET_BP (&ctx, esp);
290         MONO_CONTEXT_SET_IP (&ctx, eip);
291         memcpy (&ctx.regs, int_regs, sizeof (gulong) * MONO_SAVED_GREGS);
292         memcpy (&ctx.fregs, fp_regs, sizeof (double) * MONO_SAVED_FREGS);
293
294         if (mono_object_isinst (exc, mono_defaults.exception_class)) {
295                 MonoException *mono_ex = (MonoException*)exc;
296                 if (!rethrow)
297                         mono_ex->stack_trace = NULL;
298         }
299         arch_handle_exception (&ctx, exc, FALSE);
300         restore_context (&ctx);
301
302         g_assert_not_reached ();
303 }
304
305 /**
306  * arch_get_throw_exception_generic:
307  *
308  * Returns a function pointer which can be used to raise 
309  * exceptions. The returned function has the following 
310  * signature: void (*func) (MonoException *exc); or
311  * void (*func) (char *exc_name);
312  *
313  */
314 static gpointer 
315 mono_arch_get_throw_exception_generic (guint8 *start, int size, int by_name, gboolean rethrow)
316 {
317         guint8 *code;
318         int alloc_size, pos, i;
319
320         code = start;
321
322         /* save all the regs on the stack */
323         pos = 0;
324         for (i = 31; i >= 14; --i) {
325                 pos += sizeof (gdouble);
326                 ppc_stfd (code, i, -pos, ppc_sp);
327         }
328         pos += sizeof (gulong) * MONO_SAVED_GREGS;
329         ppc_stmw (code, ppc_r13, ppc_sp, -pos);
330
331         ppc_mflr (code, ppc_r0);
332         ppc_stw (code, ppc_r0, PPC_RET_ADDR_OFFSET, ppc_sp);
333
334         alloc_size = PPC_MINIMAL_STACK_SIZE + pos + 64;
335         // align to PPC_STACK_ALIGNMENT bytes
336         alloc_size += PPC_STACK_ALIGNMENT - 1;
337         alloc_size &= ~(PPC_STACK_ALIGNMENT - 1);
338
339         g_assert ((alloc_size & (PPC_STACK_ALIGNMENT-1)) == 0);
340         ppc_stwu (code, ppc_sp, -alloc_size, ppc_sp);
341
342         //ppc_break (code);
343         if (by_name) {
344                 ppc_mr (code, ppc_r5, ppc_r3);
345                 ppc_load (code, ppc_r3, mono_defaults.corlib);
346                 ppc_load (code, ppc_r4, "System");
347                 ppc_load (code, ppc_r0, mono_exception_from_name);
348                 ppc_mtctr (code, ppc_r0);
349                 ppc_bcctrl (code, PPC_BR_ALWAYS, 0);
350         }
351
352         /* call throw_exception (exc, ip, sp, int_regs, fp_regs) */
353         /* caller sp */
354         ppc_lwz (code, ppc_r5, 0, ppc_sp); 
355         /* exc is already in place in r3 */
356         if (by_name)
357                 ppc_lwz (code, ppc_r4, PPC_RET_ADDR_OFFSET, ppc_r5); 
358         else
359                 ppc_mr (code, ppc_r4, ppc_r0); /* caller ip */
360         /* pointer to the saved fp regs */
361         pos = alloc_size - sizeof (double) * MONO_SAVED_FREGS;
362         ppc_addi (code, ppc_r7, ppc_sp, pos);
363         /* pointer to the saved int regs */
364         pos -= sizeof (gulong) * MONO_SAVED_GREGS;
365         ppc_addi (code, ppc_r6, ppc_sp, pos);
366         ppc_li (code, ppc_r8, rethrow);
367
368         ppc_load (code, ppc_r0, throw_exception);
369         ppc_mtctr (code, ppc_r0);
370         ppc_bcctrl (code, PPC_BR_ALWAYS, 0);
371         /* we should never reach this breakpoint */
372         ppc_break (code);
373         g_assert ((code - start) < size);
374         return start;
375 }
376
377 /**
378  * mono_arch_get_rethrow_exception:
379  *
380  * Returns a function pointer which can be used to rethrow 
381  * exceptions. The returned function has the following 
382  * signature: void (*func) (MonoException *exc); 
383  *
384  */
385 gpointer
386 mono_arch_get_rethrow_exception (void)
387 {
388         static guint8 start [132];
389         static int inited = 0;
390
391         if (inited)
392                 return start;
393         mono_arch_get_throw_exception_generic (start, sizeof (start), FALSE, TRUE);
394         inited = 1;
395         return start;
396 }
397 /**
398  * arch_get_throw_exception:
399  *
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:
404  *
405  * x86_push_imm (code, mono_get_exception_arithmetic ()); 
406  * x86_call_code (code, arch_get_throw_exception ()); 
407  *
408  */
409 gpointer 
410 mono_arch_get_throw_exception (void)
411 {
412         static guint8 start [132];
413         static int inited = 0;
414
415         if (inited)
416                 return start;
417         mono_arch_get_throw_exception_generic (start, sizeof (start), FALSE, FALSE);
418         inited = 1;
419         return start;
420 }
421
422 /**
423  * arch_get_throw_exception_by_name:
424  *
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:
429  *
430  * x86_push_imm (code, "ArithmeticException"); 
431  * x86_call_code (code, arch_get_throw_exception_by_name ()); 
432  *
433  */
434 gpointer 
435 mono_arch_get_throw_exception_by_name (void)
436 {
437         static guint8 start [168];
438         static int inited = 0;
439
440         if (inited)
441                 return start;
442         mono_arch_get_throw_exception_generic (start, sizeof (start), TRUE, FALSE);
443         inited = 1;
444         return start;
445 }       
446
447 static MonoArray *
448 glist_to_array (GList *list) 
449 {
450         MonoDomain *domain = mono_domain_get ();
451         MonoArray *res;
452         int len, i;
453
454         if (!list)
455                 return NULL;
456
457         len = g_list_length (list);
458         res = mono_array_new (domain, mono_defaults.int_class, len);
459
460         for (i = 0; list; list = list->next, i++)
461                 mono_array_set (res, gpointer, i, list->data);
462
463         return res;
464 }
465
466 /* mono_arch_find_jit_info:
467  *
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.
474  */
475 MonoJitInfo *
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)
479 {
480         MonoJitInfo *ji;
481         gpointer ip = MONO_CONTEXT_GET_IP (ctx);
482         unsigned long *ptr;
483         char *p;
484         MonoPPCStackFrame *sframe;
485
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)))
488                 ji = prev_ji;
489         else
490                 ji = mono_jit_info_table_find (domain, ip);
491
492         if (trace)
493                 *trace = NULL;
494
495         if (native_offset)
496                 *native_offset = -1;
497
498         if (managed)
499                 *managed = FALSE;
500
501         if (ji != NULL) {
502                 char *source_location, *tmpaddr, *fname;
503                 gint32 address, iloffset;
504                 int offset, i;
505                 gulong *ctx_regs;
506
507                 *new_ctx = *ctx;
508                 setup_context (new_ctx);
509
510                 if (*lmf && (MONO_CONTEXT_GET_BP (ctx) >= (gpointer)(*lmf)->ebp)) {
511                         /* remove any unused lmf */
512                         *lmf = (*lmf)->previous_lmf;
513                 }
514
515                 address = (char *)ip - (char *)ji->code_start;
516
517                 if (native_offset)
518                         *native_offset = address;
519
520                 if (managed)
521                         if (!ji->method->wrapper_type)
522                                 *managed = TRUE;
523
524                 if (trace) {
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);
527
528                         if (iloffset < 0)
529                                 tmpaddr = g_strdup_printf ("<0x%05x>", address);
530                         else
531                                 tmpaddr = g_strdup_printf ("[0x%05x]", iloffset);
532                 
533                         fname = mono_method_full_name (ji->method, TRUE);
534
535                         if (source_location)
536                                 *trace = g_strdup_printf ("in %s (at %s) %s", tmpaddr, source_location, fname);
537                         else
538                                 *trace = g_strdup_printf ("in %s %s", tmpaddr, fname);
539
540                         g_free (fname);
541                         g_free (source_location);
542                         g_free (tmpaddr);
543                 }
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 */
551                         offset = 0;
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);
557                                 }
558                         }*/
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);
563                                 }
564                         }
565                 }
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);
570
571                 return ji;
572         } else if (*lmf) {
573                 
574                 *new_ctx = *ctx;
575                 setup_context (new_ctx);
576
577                 if (!(*lmf)->method)
578                         return (gpointer)-1;
579
580                 if (trace) {
581                         char *fname = mono_method_full_name ((*lmf)->method, TRUE);
582                         *trace = g_strdup_printf ("in (unmanaged) %s", fname);
583                         g_free (fname);
584                 }
585                 
586                 if ((ji = mono_jit_info_table_find (domain, (gpointer)(*lmf)->eip))) {
587                 } else {
588                         memset (res, 0, sizeof (MonoJitInfo));
589                         res->method = (*lmf)->method;
590                 }
591
592                 /*sframe = (MonoPPCStackFrame*)MONO_CONTEXT_GET_BP (ctx);
593                 MONO_CONTEXT_SET_BP (new_ctx, sframe->sp);
594                 MONO_CONTEXT_SET_IP (new_ctx, sframe->lr);*/
595                 MONO_CONTEXT_SET_BP (new_ctx, (*lmf)->ebp);
596                 MONO_CONTEXT_SET_IP (new_ctx, (*lmf)->eip);
597                 memcpy (&new_ctx->regs, (*lmf)->iregs, sizeof (gulong) * MONO_SAVED_GREGS);
598                 memcpy (&new_ctx->fregs, (*lmf)->fregs, sizeof (double) * MONO_SAVED_FREGS);
599                 *lmf = (*lmf)->previous_lmf;
600
601                 return ji ? ji : res;
602         }
603
604         return NULL;
605 }
606
607 void
608 mono_jit_walk_stack (MonoStackWalk func, gboolean do_il_offset, gpointer user_data) {
609         MonoDomain *domain = mono_domain_get ();
610         MonoJitTlsData *jit_tls = TlsGetValue (mono_jit_tls_id);
611         MonoLMF *lmf = jit_tls->lmf;
612         MonoJitInfo *ji, rji;
613         gint native_offset, il_offset;
614         gboolean managed;
615         MonoPPCStackFrame *sframe;
616
617         MonoContext ctx, new_ctx;
618
619         setup_context (&ctx);
620         setup_context (&new_ctx);
621
622 #ifdef __APPLE__
623         __asm__ volatile("lwz   %0,0(r1)" : "=r" (sframe));
624 #else
625         __asm__ volatile("lwz   %0,0(1)" : "=r" (sframe));
626 #endif
627         //MONO_CONTEXT_SET_IP (&ctx, sframe->lr);
628         MONO_CONTEXT_SET_BP (&ctx, sframe->sp);
629         sframe = (MonoPPCStackFrame*)sframe->sp;
630         MONO_CONTEXT_SET_IP (&ctx, sframe->lr);
631
632         while (MONO_CONTEXT_GET_BP (&ctx) < jit_tls->end_of_stack) {
633                 
634                 ji = mono_arch_find_jit_info (domain, jit_tls, &rji, NULL, &ctx, &new_ctx, NULL, &lmf, &native_offset, &managed);
635                 g_assert (ji);
636
637                 if (ji == (gpointer)-1)
638                         return;
639
640                 il_offset = do_il_offset ? mono_debug_il_offset_from_address (ji->method, native_offset, domain): -1;
641
642                 if (func (ji->method, native_offset, il_offset, managed, user_data))
643                         return;
644                 
645                 ctx = new_ctx;
646                 setup_context (&ctx);
647         }
648 }
649
650 MonoBoolean
651 ves_icall_get_frame_info (gint32 skip, MonoBoolean need_file_info, 
652                           MonoReflectionMethod **method, 
653                           gint32 *iloffset, gint32 *native_offset,
654                           MonoString **file, gint32 *line, gint32 *column)
655 {
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         MonoContext ctx, new_ctx;
661         MonoPPCStackFrame *sframe;
662
663 #ifdef __APPLE__
664         __asm__ volatile("lwz   %0,0(r1)" : "=r" (sframe));
665 #else
666         __asm__ volatile("lwz   %0,0(1)" : "=r" (sframe));
667 #endif
668         MONO_CONTEXT_SET_BP (&ctx, sframe->sp);
669         sframe = (MonoPPCStackFrame*)sframe->sp;
670         MONO_CONTEXT_SET_IP (&ctx, sframe->lr);
671         /*MONO_CONTEXT_SET_IP (&ctx, ves_icall_get_frame_info);
672         MONO_CONTEXT_SET_BP (&ctx, __builtin_frame_address (0));*/
673
674         skip++;
675
676         do {
677                 ji = mono_arch_find_jit_info (domain, jit_tls, &rji, NULL, &ctx, &new_ctx, NULL, &lmf, native_offset, NULL);
678
679                 ctx = new_ctx;
680                 
681                 if (!ji || ji == (gpointer)-1 || MONO_CONTEXT_GET_BP (&ctx) >= jit_tls->end_of_stack)
682                         return FALSE;
683
684                 /* skip all wrappers ??*/
685                 if (ji->method->wrapper_type == MONO_WRAPPER_RUNTIME_INVOKE ||
686                     ji->method->wrapper_type == MONO_WRAPPER_REMOTING_INVOKE_WITH_CHECK ||
687                     ji->method->wrapper_type == MONO_WRAPPER_XDOMAIN_INVOKE ||
688                     ji->method->wrapper_type == MONO_WRAPPER_XDOMAIN_DISPATCH ||
689                     ji->method->wrapper_type == MONO_WRAPPER_REMOTING_INVOKE)
690                         continue;
691
692                 skip--;
693
694         } while (skip >= 0);
695
696         *method = mono_method_get_object (domain, ji->method, NULL);
697         *iloffset = mono_debug_il_offset_from_address (ji->method, *native_offset, domain);
698
699         if (need_file_info) {
700                 gchar *filename;
701
702                 filename = mono_debug_source_location_from_address (ji->method, *native_offset, line, domain);
703
704                 *file = filename? mono_string_new (domain, filename): NULL;
705                 *column = 0;
706
707                 g_free (filename);
708         }
709
710         return TRUE;
711 }
712
713 /*
714  * This is the function called from the signal handler
715  */
716 #ifdef __APPLE__
717 gboolean
718 mono_arch_handle_exception (void *ctx, gpointer obj, gboolean test_only)
719 {
720         struct ucontext *uc = ctx;
721         MonoContext mctx;
722         gboolean result;
723         
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);
728
729         result = arch_handle_exception (&mctx, obj, test_only);
730         /* restore the context so that returning from the signal handler will invoke
731          * the catch clause 
732          */
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);
737         return result;
738 }
739
740 gpointer
741 mono_arch_ip_from_context (void *sigctx)
742 {
743         struct ucontext *uc = sigctx;
744         return (gpointer)uc->uc_mcontext->ss.srr0;
745 }
746
747 #else
748 /* Linux */
749 gboolean
750 mono_arch_handle_exception (void *ctx, gpointer obj, gboolean test_only)
751 {
752         struct ucontext *uc = ctx;
753         MonoContext mctx;
754         gboolean result;
755         
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);
760
761         result = arch_handle_exception (&mctx, obj, test_only);
762         /* restore the context so that returning from the signal handler will invoke
763          * the catch clause 
764          */
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);
769         return result;
770 }
771
772 gpointer
773 mono_arch_ip_from_context (void *sigctx)
774 {
775         struct ucontext *uc = sigctx;
776         return (gpointer)uc->uc_mcontext.uc_regs->gregs [PT_NIP];
777 }
778
779 #endif
780
781 /**
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
786  *
787  *
788  */
789 static gboolean
790 arch_handle_exception (MonoContext *ctx, gpointer obj, gboolean test_only)
791 {
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         MonoString *initial_stack_trace = NULL;
800         GString *trace_str = NULL;
801         int frame_count = 0;
802
803         g_assert (ctx != NULL);
804         if (!obj) {
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;
809         } 
810
811         if (mono_object_isinst (obj, mono_defaults.exception_class)) {
812                 mono_ex = (MonoException*)obj;
813                 initial_stack_trace = mono_ex->stack_trace;
814         } else {
815                 mono_ex = NULL;
816         }
817
818
819         if (!call_filter)
820                 call_filter = arch_get_call_filter ();
821
822         g_assert (jit_tls->end_of_stack);
823         g_assert (jit_tls->abort_func);
824
825         if (!test_only) {
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)
832                                 G_BREAKPOINT ();
833                         mono_unhandled_exception (obj);
834                 }
835         }
836
837         memset (&rji, 0, sizeof (rji));
838
839         while (1) {
840                 MonoContext new_ctx;
841                 char *trace = NULL;
842                 gboolean need_trace = FALSE;
843                 
844                 if (test_only && (frame_count < 1000)) {
845                         need_trace = TRUE;
846                         if (!trace_str)
847                                 trace_str = g_string_new ("");
848                 }
849                 setup_context (&new_ctx);
850                 ji = mono_arch_find_jit_info (domain, jit_tls, &rji, &rji, ctx, &new_ctx, 
851                                               need_trace ? &trace : NULL, &lmf, NULL, NULL);
852                 if (!ji) {
853                         g_warning ("Exception inside function without unwind info");
854                         g_assert_not_reached ();
855                 }
856
857                 if (ji != (gpointer)-1) {
858                         frame_count ++;
859                         
860                         if (test_only && ji->method->wrapper_type != MONO_WRAPPER_RUNTIME_INVOKE && mono_ex) {
861                                 if (!initial_stack_trace && (frame_count < 1000)) {
862                                         trace_ips = g_list_prepend (trace_ips, MONO_CONTEXT_GET_IP (ctx));
863
864                                         g_string_append (trace_str, trace);
865                                         g_string_append_c (trace_str, '\n');
866                                 }
867                         }
868
869                         if (ji->num_clauses) {
870                                 int i;
871                                 
872                                 g_assert (ji->clauses);
873                         
874                                 for (i = 0; i < ji->num_clauses; i++) {
875                                         MonoJitExceptionInfo *ei = &ji->clauses [i];
876                                         gboolean filtered = FALSE;
877
878                                         if (ei->try_start <= MONO_CONTEXT_GET_IP (ctx) && 
879                                             MONO_CONTEXT_GET_IP (ctx) <= ei->try_end) { 
880                                                 /* catch block */
881
882                                                 if ((ei->flags == MONO_EXCEPTION_CLAUSE_NONE) || (ei->flags == MONO_EXCEPTION_CLAUSE_FILTER)) {
883                                                         /* store the exception object int cfg->excvar */
884                                                         g_assert (ei->exvar_offset);
885                                                         /* need to use the frame pointer (ppc_r31), not r1 (regs start from register r13): methods with clauses always have r31 */
886                                                         *((gpointer *)((char *)(ctx->regs [ppc_r31-13]) + ei->exvar_offset)) = obj;
887                                                         if (!initial_stack_trace && trace_str) {
888                                                                 mono_ex->stack_trace = mono_string_new (domain, trace_str->str);
889                                                         }
890                                                 }
891
892                                                 if (ei->flags == MONO_EXCEPTION_CLAUSE_FILTER)
893                                                         filtered = call_filter (ctx, ei->data.filter, mono_ex);
894
895                                                 if ((ei->flags == MONO_EXCEPTION_CLAUSE_NONE && 
896                                                      mono_object_isinst (obj, ei->data.catch_class)) || filtered) {
897                                                         if (test_only) {
898                                                                 if (mono_ex) {
899                                                                         trace_ips = g_list_reverse (trace_ips);
900                                                                         mono_ex->trace_ips = glist_to_array (trace_ips);
901                                                                 }
902                                                                 g_list_free (trace_ips);
903                                                                 g_free (trace);
904                                                                 if (trace_str)
905                                                                         g_string_free (trace_str, TRUE);
906                                                                 return TRUE;
907                                                         }
908                                                         if (mono_jit_trace_calls != NULL)
909                                                                 g_print ("EXCEPTION: catch found at clause %d of %s\n", i, mono_method_full_name (ji->method, TRUE));
910                                                         /*printf ("stack for catch: %p\n", MONO_CONTEXT_GET_BP (ctx));*/
911                                                         MONO_CONTEXT_SET_IP (ctx, ei->handler_start);
912                                                         jit_tls->lmf = lmf;
913                                                         g_free (trace);
914                                                         if (trace_str)
915                                                                 g_string_free (trace_str, TRUE);
916                                                         return 0;
917                                                 }
918                                                 if (!test_only && ei->try_start <= MONO_CONTEXT_GET_IP (ctx) && 
919                                                     MONO_CONTEXT_GET_IP (ctx) < ei->try_end &&
920                                                     (ei->flags & MONO_EXCEPTION_CLAUSE_FINALLY)) {
921                                                         if (mono_jit_trace_calls != NULL)
922                                                                 g_print ("EXCEPTION: finally clause %d of %s\n", i, mono_method_full_name (ji->method, TRUE));
923                                                         call_filter (ctx, ei->handler_start, NULL);
924                                                 }
925                                                 
926                                         }
927                                 }
928                         }
929                 }
930
931                 g_free (trace);
932                         
933                 *ctx = new_ctx;
934                 setup_context (ctx);
935
936                 if ((ji == (gpointer)-1) || MONO_CONTEXT_GET_BP (ctx) >= jit_tls->end_of_stack) {
937                         if (!test_only) {
938                                 jit_tls->lmf = lmf;
939                                 jit_tls->abort_func (obj);
940                                 g_assert_not_reached ();
941                         } else {
942                                 if (mono_ex) {
943                                         trace_ips = g_list_reverse (trace_ips);
944                                         mono_ex->trace_ips = glist_to_array (trace_ips);
945                                 }
946                                 g_list_free (trace_ips);
947                                 if (trace_str)
948                                         g_string_free (trace_str, TRUE);
949                                 return FALSE;
950                         }
951                 }
952         }
953
954         g_assert_not_reached ();
955 }
956
957 gboolean
958 mono_arch_has_unwind_info (gconstpointer addr)
959 {
960         return FALSE;
961 }
962