2009-08-15 Zoltan Varga <vargaz@gmail.com>
[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  *   Andreas Faerber <andreas.faerber@web.de>
8  *
9  * (C) 2001 Ximian, Inc.
10  * (C) 2007-2008 Andreas Faerber
11  */
12
13 #include <config.h>
14 #include <glib.h>
15 #include <signal.h>
16 #include <string.h>
17 #include <stddef.h>
18 #if HAVE_UCONTEXT_H
19 #include <ucontext.h>
20 #endif
21
22 #include <mono/arch/ppc/ppc-codegen.h>
23 #include <mono/metadata/appdomain.h>
24 #include <mono/metadata/tabledefs.h>
25 #include <mono/metadata/threads.h>
26 #include <mono/metadata/debug-helpers.h>
27 #include <mono/metadata/exception.h>
28 #include <mono/metadata/mono-debug.h>
29
30 #include "mini.h"
31 #include "mini-ppc.h"
32
33 /*
34
35 struct sigcontext {
36     int      sc_onstack;     // sigstack state to restore 
37     int      sc_mask;        // signal mask to restore 
38     int      sc_ir;          // pc 
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 
42 };
43
44 struct ucontext {
45         int             uc_onstack;
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 
51 };
52
53 typedef struct ppc_exception_state {
54         unsigned long dar;      // Fault registers for coredump 
55         unsigned long dsisr;
56         unsigned long exception;// number of powerpc exception taken 
57         unsigned long pad0;     // align to 16 bytes 
58
59         unsigned long pad1[4];  // space in PCB "just in case" 
60 } ppc_exception_state_t;
61
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];
68 } ppc_vector_state_t;
69
70 typedef struct ppc_float_state {
71         double  fpregs[32];
72
73         unsigned int fpscr_pad; // fpscr is 64 bits, 32 bits of rubbish 
74         unsigned int fpscr;     // floating point status register 
75 } ppc_float_state_t;
76
77 typedef struct ppc_thread_state {
78         unsigned int srr0;      // Instruction address register (PC) 
79         unsigned int srr1;      // Machine state register (supervisor) 
80         unsigned int r0;
81         unsigned int r1;
82         unsigned int r2;
83         ... 
84         unsigned int r31;
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) 
90
91         unsigned int vrsave;    // Vector Save Register 
92 } ppc_thread_state_t;
93
94 struct mcontext {
95         ppc_exception_state_t   es;
96         ppc_thread_state_t      ss;
97         ppc_float_state_t       fs;
98         ppc_vector_state_t      vs;
99 };
100
101 typedef struct mcontext  * mcontext_t;
102
103 Linux/PPC instead has:
104 struct sigcontext {
105         unsigned long   _unused[4];
106         int             signal;
107         unsigned long   handler;
108         unsigned long   oldmask;
109         struct pt_regs  *regs;
110 };
111 struct pt_regs {
112         unsigned long gpr[32];
113         unsigned long nip;
114         unsigned long msr;
115         unsigned long orig_gpr3;        // Used for restarting system calls 
116         unsigned long ctr;
117         unsigned long link;
118         unsigned long xer;
119         unsigned long ccr;
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 
128 };
129 struct mcontext {
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)));
134 };
135
136 struct ucontext {
137         unsigned long    uc_flags;
138         struct ucontext *uc_link;
139         stack_t          uc_stack;
140         int              uc_pad[7];
141         struct mcontext *uc_regs;       // points to uc_mcontext field 
142         sigset_t         uc_sigmask;
143         // glibc has 1024-bit signal masks, ours are 64-bit 
144         int              uc_maskext[30];
145         int              uc_pad2[3];
146         struct mcontext  uc_mcontext;
147 };
148
149 #define ELF_NGREG       48      // includes nip, msr, lr, etc. 
150 #define ELF_NFPREG      33      // includes fpscr 
151
152 // General registers 
153 typedef unsigned long elf_greg_t;
154 typedef elf_greg_t elf_gregset_t[ELF_NGREG];
155
156 // Floating point registers 
157 typedef double elf_fpreg_t;
158 typedef elf_fpreg_t elf_fpregset_t[ELF_NFPREG];
159
160
161 */
162
163
164 #define restore_regs_from_context(ctx_reg,ip_reg,tmp_reg) do {  \
165                 int reg;        \
166                 ppc_ldptr (code, ip_reg, G_STRUCT_OFFSET (MonoContext, sc_ir), ctx_reg);        \
167                 ppc_load_multiple_regs (code, ppc_r13, G_STRUCT_OFFSET (MonoContext, regs), ctx_reg);   \
168                 for (reg = 0; reg < MONO_SAVED_FREGS; ++reg) {  \
169                         ppc_lfd (code, (14 + reg),      \
170                                 G_STRUCT_OFFSET(MonoContext, fregs) + reg * sizeof (gdouble), ctx_reg); \
171                 }       \
172         } while (0)
173
174 /* nothing to do */
175 #define setup_context(ctx)
176
177 #ifdef PPC_USES_FUNCTION_DESCRIPTOR
178 guint8*
179 mono_ppc_create_pre_code_ftnptr (guint8 *code)
180 {
181         MonoPPCFunctionDescriptor *ftnptr = (MonoPPCFunctionDescriptor*)code;
182
183         code += sizeof (MonoPPCFunctionDescriptor);
184         ftnptr->code = code;
185         ftnptr->toc = NULL;
186         ftnptr->env = NULL;
187
188         return code;
189 }
190 #endif
191
192 /*
193  * arch_get_restore_context:
194  *
195  * Returns a pointer to a method which restores a previously saved sigcontext.
196  * The first argument in r3 is the pointer to the context.
197  */
198 gpointer
199 mono_arch_get_restore_context_full (guint32 *code_size, MonoJumpInfo **ji, gboolean aot)
200 {
201         guint8 *start, *code;
202         int size = MONO_PPC_32_64_CASE (128, 172) + PPC_FTNPTR_SIZE;
203
204         code = start = mono_global_codeman_reserve (size);
205         *ji = NULL;
206         if (!aot)
207                 code = mono_ppc_create_pre_code_ftnptr (code);
208         restore_regs_from_context (ppc_r3, ppc_r4, ppc_r5);
209         /* restore also the stack pointer */
210         ppc_ldptr (code, ppc_sp, G_STRUCT_OFFSET (MonoContext, sc_sp), ppc_r3);
211         //ppc_break (code);
212         /* jump to the saved IP */
213         ppc_mtctr (code, ppc_r4);
214         ppc_bcctr (code, PPC_BR_ALWAYS, 0);
215         /* never reached */
216         ppc_break (code);
217
218         g_assert ((code - start) <= size);
219         mono_arch_flush_icache (start, code - start);
220
221         *code_size = code - start;
222
223         return start;
224 }
225
226 #define SAVED_REGS_LENGTH               (sizeof (gdouble) * MONO_SAVED_FREGS + sizeof (gpointer) * MONO_SAVED_GREGS)
227 #define ALIGN_STACK_FRAME_SIZE(s)       (((s) + MONO_ARCH_FRAME_ALIGNMENT - 1) & ~(MONO_ARCH_FRAME_ALIGNMENT - 1))
228 /* The 64 bytes here are for outgoing arguments and a bit of spare.
229    We don't use it all, but it doesn't hurt. */
230 #define REG_SAVE_STACK_FRAME_SIZE       (ALIGN_STACK_FRAME_SIZE (SAVED_REGS_LENGTH + PPC_MINIMAL_STACK_SIZE + 64))
231
232 static guint8*
233 emit_save_saved_regs (guint8 *code, int pos)
234 {
235         int i;
236
237         for (i = 31; i >= 14; --i) {
238                 pos -= sizeof (gdouble);
239                 ppc_stfd (code, i, pos, ppc_sp);
240         }
241         pos -= sizeof (gpointer) * MONO_SAVED_GREGS;
242         ppc_store_multiple_regs (code, ppc_r13, pos, ppc_sp);
243
244         return code;
245 }
246
247 /*
248  * mono_arch_get_call_filter:
249  *
250  * Returns a pointer to a method which calls an exception filter. We
251  * also use this function to call finally handlers (we pass NULL as 
252  * @exc object in this case).
253  */
254 gpointer
255 mono_arch_get_call_filter_full (guint32 *code_size, MonoJumpInfo **ji, gboolean aot)
256 {
257         guint8 *start, *code;
258         int alloc_size, pos, i;
259         int size = MONO_PPC_32_64_CASE (320, 500) + PPC_FTNPTR_SIZE;
260
261         *ji = NULL;
262
263         /* call_filter (MonoContext *ctx, unsigned long eip, gpointer exc) */
264         code = start = mono_global_codeman_reserve (size);
265         if (!aot)
266                 code = mono_ppc_create_pre_code_ftnptr (code);
267
268         /* store ret addr */
269         ppc_mflr (code, ppc_r0);
270         ppc_stptr (code, ppc_r0, PPC_RET_ADDR_OFFSET, ppc_sp);
271
272         alloc_size = REG_SAVE_STACK_FRAME_SIZE;
273
274         /* allocate stack frame and set link from sp in ctx */
275         g_assert ((alloc_size & (MONO_ARCH_FRAME_ALIGNMENT-1)) == 0);
276         ppc_ldptr (code, ppc_r0, G_STRUCT_OFFSET (MonoContext, sc_sp), ppc_r3);
277         ppc_ldptr_indexed (code, ppc_r0, ppc_r0, ppc_r0);
278         ppc_stptr_update (code, ppc_r0, -alloc_size, ppc_sp);
279
280         code = emit_save_saved_regs (code, alloc_size);
281
282         /* restore all the regs from ctx (in r3), but not r1, the stack pointer */
283         restore_regs_from_context (ppc_r3, ppc_r6, ppc_r7);
284         /* call handler at eip (r4) and set the first arg with the exception (r5) */
285         ppc_mtctr (code, ppc_r4);
286         ppc_mr (code, ppc_r3, ppc_r5);
287         ppc_bcctrl (code, PPC_BR_ALWAYS, 0);
288
289         /* epilog */
290         ppc_ldptr (code, ppc_r0, alloc_size + PPC_RET_ADDR_OFFSET, ppc_sp);
291         ppc_mtlr (code, ppc_r0);
292
293         /* restore all the regs from the stack */
294         pos = alloc_size;
295         for (i = 31; i >= 14; --i) {
296                 pos -= sizeof (gdouble);
297                 ppc_lfd (code, i, pos, ppc_sp);
298         }
299         pos -= sizeof (gpointer) * MONO_SAVED_GREGS;
300         ppc_load_multiple_regs (code, ppc_r13, pos, ppc_sp);
301
302         ppc_addic (code, ppc_sp, ppc_sp, alloc_size);
303         ppc_blr (code);
304
305         g_assert ((code - start) < size);
306         mono_arch_flush_icache (start, code - start);
307
308         *code_size = code - start;
309
310         return start;
311 }
312
313 void
314 mono_ppc_throw_exception (MonoObject *exc, unsigned long eip, unsigned long esp, gulong *int_regs, gdouble *fp_regs, gboolean rethrow)
315 {
316         static void (*restore_context) (MonoContext *);
317         MonoContext ctx;
318
319         if (!restore_context)
320                 restore_context = mono_get_restore_context ();
321
322         /* adjust eip so that it point into the call instruction */
323         eip -= 4;
324
325         setup_context (&ctx);
326
327         /*printf ("stack in throw: %p\n", esp);*/
328         MONO_CONTEXT_SET_BP (&ctx, esp);
329         MONO_CONTEXT_SET_IP (&ctx, eip);
330         memcpy (&ctx.regs, int_regs, sizeof (gulong) * MONO_SAVED_GREGS);
331         memcpy (&ctx.fregs, fp_regs, sizeof (double) * MONO_SAVED_FREGS);
332
333         if (mono_object_isinst (exc, mono_defaults.exception_class)) {
334                 MonoException *mono_ex = (MonoException*)exc;
335                 if (!rethrow)
336                         mono_ex->stack_trace = NULL;
337         }
338         mono_handle_exception (&ctx, exc, (gpointer)eip, FALSE);
339         restore_context (&ctx);
340
341         g_assert_not_reached ();
342 }
343
344 /**
345  * arch_get_throw_exception_generic:
346  *
347  * Returns a function pointer which can be used to raise 
348  * exceptions. The returned function has the following 
349  * signature: void (*func) (MonoException *exc); or
350  * void (*func) (guint32 ex_token, gpointer ip)
351  *
352  */
353 static gpointer
354 mono_arch_get_throw_exception_generic (int size, guint32 *code_size, MonoJumpInfo **ji, int corlib, gboolean rethrow, gboolean aot)
355 {
356         guint8 *start, *code;
357         int alloc_size, pos;
358
359         *ji = NULL;
360
361         code = start = mono_global_codeman_reserve (size);
362         if (!aot)
363                 code = mono_ppc_create_pre_code_ftnptr (code);
364
365         /* store ret addr */
366         if (corlib)
367                 ppc_mr (code, ppc_r0, ppc_r4);
368         else
369                 ppc_mflr (code, ppc_r0);
370         ppc_stptr (code, ppc_r0, PPC_RET_ADDR_OFFSET, ppc_sp);
371
372         alloc_size = REG_SAVE_STACK_FRAME_SIZE;
373
374         g_assert ((alloc_size & (MONO_ARCH_FRAME_ALIGNMENT-1)) == 0);
375         ppc_stptr_update (code, ppc_sp, -alloc_size, ppc_sp);
376
377         code = emit_save_saved_regs (code, alloc_size);
378
379         //ppc_break (code);
380         if (corlib) {
381                 ppc_mr (code, ppc_r4, ppc_r3);
382
383                 if (aot) {
384                         code = mono_arch_emit_load_aotconst (start, code, ji, MONO_PATCH_INFO_IMAGE, mono_defaults.corlib);
385                         ppc_mr (code, ppc_r3, ppc_r11);
386                         code = mono_arch_emit_load_aotconst (start, code, ji, MONO_PATCH_INFO_JIT_ICALL_ADDR, "mono_exception_from_token");
387 #ifdef PPC_USES_FUNCTION_DESCRIPTOR
388                         ppc_ldptr (code, ppc_r2, sizeof (gpointer), ppc_r11);
389                         ppc_ldptr (code, ppc_r11, 0, ppc_r11);
390 #endif
391                         ppc_mtctr (code, ppc_r11);
392                         ppc_bcctrl (code, PPC_BR_ALWAYS, 0);
393                 } else {
394                         ppc_load (code, ppc_r3, (gulong)mono_defaults.corlib);
395                         ppc_load_func (code, ppc_r0, mono_exception_from_token);
396                         ppc_mtctr (code, ppc_r0);
397                         ppc_bcctrl (code, PPC_BR_ALWAYS, 0);
398                 }
399         }
400
401         /* call throw_exception (exc, ip, sp, int_regs, fp_regs) */
402         /* caller sp */
403         ppc_ldptr (code, ppc_r5, 0, ppc_sp);
404         /* exc is already in place in r3 */
405         if (corlib)
406                 ppc_ldptr (code, ppc_r4, PPC_RET_ADDR_OFFSET, ppc_r5);
407         else
408                 ppc_mr (code, ppc_r4, ppc_r0); /* caller ip */
409         /* pointer to the saved fp regs */
410         pos = alloc_size - sizeof (gdouble) * MONO_SAVED_FREGS;
411         ppc_addi (code, ppc_r7, ppc_sp, pos);
412         /* pointer to the saved int regs */
413         pos -= sizeof (gpointer) * MONO_SAVED_GREGS;
414         ppc_addi (code, ppc_r6, ppc_sp, pos);
415         ppc_li (code, ppc_r8, rethrow);
416
417         if (aot) {
418                 // This can be called from runtime code, which can't guarantee that
419                 // r30 contains the got address.
420                 // So emit the got address loading code too
421                 code = mono_arch_emit_load_got_addr (start, code, NULL, ji);
422                 code = mono_arch_emit_load_aotconst (start, code, ji, MONO_PATCH_INFO_JIT_ICALL_ADDR, "mono_ppc_throw_exception");
423 #ifdef PPC_USES_FUNCTION_DESCRIPTOR
424                 ppc_ldptr (code, ppc_r2, sizeof (gpointer), ppc_r11);
425                 ppc_ldptr (code, ppc_r11, 0, ppc_r11);
426 #endif
427                 ppc_mtctr (code, ppc_r11);
428                 ppc_bcctrl (code, PPC_BR_ALWAYS, 0);
429         } else {
430                 ppc_load_func (code, ppc_r0, mono_ppc_throw_exception);
431                 ppc_mtctr (code, ppc_r0);
432                 ppc_bcctrl (code, PPC_BR_ALWAYS, 0);
433         }
434         /* we should never reach this breakpoint */
435         ppc_break (code);
436         g_assert ((code - start) <= size);
437         mono_arch_flush_icache (start, code - start);
438
439         *code_size = code - start;
440
441         return start;
442 }
443
444 /**
445  * mono_arch_get_rethrow_exception:
446  *
447  * Returns a function pointer which can be used to rethrow 
448  * exceptions. The returned function has the following 
449  * signature: void (*func) (MonoException *exc); 
450  *
451  */
452 gpointer
453 mono_arch_get_rethrow_exception_full (guint32 *code_size, MonoJumpInfo **ji, gboolean aot)
454 {
455         int size = MONO_PPC_32_64_CASE (132, 224) + PPC_FTNPTR_SIZE;
456
457         if (aot)
458                 size += 64;
459         return mono_arch_get_throw_exception_generic (size, code_size, ji, FALSE, TRUE, aot);
460 }
461
462 /**
463  * arch_get_throw_exception:
464  *
465  * Returns a function pointer which can be used to raise 
466  * exceptions. The returned function has the following 
467  * signature: void (*func) (MonoException *exc); 
468  * For example to raise an arithmetic exception you can use:
469  *
470  * x86_push_imm (code, mono_get_exception_arithmetic ()); 
471  * x86_call_code (code, arch_get_throw_exception ()); 
472  *
473  */
474 gpointer
475 mono_arch_get_throw_exception_full (guint32 *code_size, MonoJumpInfo **ji, gboolean aot)
476 {
477         int size = MONO_PPC_32_64_CASE (132, 224) + PPC_FTNPTR_SIZE;
478
479         if (aot)
480                 size += 64;
481         return mono_arch_get_throw_exception_generic (size, code_size, ji, FALSE, FALSE, aot);
482 }
483
484 /**
485  * arch_get_throw_exception_by_name:
486  *
487  * Returns a function pointer which can be used to raise 
488  * corlib exceptions. The returned function has the following 
489  * signature: void (*func) (char *exc_name); 
490  * For example to raise an arithmetic exception you can use:
491  *
492  * x86_push_imm (code, "ArithmeticException"); 
493  * x86_call_code (code, arch_get_throw_exception_by_name ()); 
494  *
495  */
496 gpointer
497 mono_arch_get_throw_exception_by_name_full (guint32 *code_size, MonoJumpInfo **ji, gboolean aot)
498 {
499         guint8 *start, *code;
500         int size = 64;
501
502         *ji = NULL;
503
504         /* Not used on PPC */   
505         start = code = mono_global_codeman_reserve (size);
506         ppc_break (code);
507         mono_arch_flush_icache (start, code - start);
508         *code_size = code - start;
509         return start;
510 }
511
512 /**
513  * mono_arch_get_throw_corlib_exception:
514  *
515  * Returns a function pointer which can be used to raise 
516  * corlib exceptions. The returned function has the following 
517  * signature: void (*func) (guint32 ex_token, guint32 offset); 
518  * On PPC, we pass the ip instead of the offset
519  */
520 gpointer
521 mono_arch_get_throw_corlib_exception_full (guint32 *code_size, MonoJumpInfo **ji, gboolean aot)
522 {
523         int size = MONO_PPC_32_64_CASE (168, 304) + PPC_FTNPTR_SIZE;
524
525         if (aot)
526                 size += 64;
527         return mono_arch_get_throw_exception_generic (size, code_size, ji, TRUE, FALSE, aot);
528 }
529
530 /* mono_arch_find_jit_info:
531  *
532  * This function is used to gather information from @ctx. It return the 
533  * MonoJitInfo of the corresponding function, unwinds one stack frame and
534  * stores the resulting context into @new_ctx. It also stores a string 
535  * describing the stack location into @trace (if not NULL), and modifies
536  * the @lmf if necessary. @native_offset return the IP offset from the 
537  * start of the function or -1 if that info is not available.
538  */
539 MonoJitInfo *
540 mono_arch_find_jit_info (MonoDomain *domain, MonoJitTlsData *jit_tls, MonoJitInfo *res, MonoJitInfo *prev_ji, MonoContext *ctx,
541                                                  MonoContext *new_ctx, MonoLMF **lmf, gboolean *managed)
542 {
543         MonoJitInfo *ji;
544         gpointer ip = MONO_CONTEXT_GET_IP (ctx);
545         MonoPPCStackFrame *sframe;
546
547         /* Avoid costly table lookup during stack overflow */
548         if (prev_ji && (ip > prev_ji->code_start && ((guint8*)ip < ((guint8*)prev_ji->code_start) + prev_ji->code_size)))
549                 ji = prev_ji;
550         else
551                 ji = mono_jit_info_table_find (domain, ip);
552
553         if (managed)
554                 *managed = FALSE;
555
556         if (ji != NULL) {
557                 gint32 address;
558                 int offset, i;
559
560                 *new_ctx = *ctx;
561                 setup_context (new_ctx);
562
563                 if (*lmf && (MONO_CONTEXT_GET_SP (ctx) >= (gpointer)(*lmf)->ebp)) {
564                         /* remove any unused lmf */
565                         *lmf = (*lmf)->previous_lmf;
566                 }
567
568                 address = (char *)ip - (char *)ji->code_start;
569
570                 if (managed)
571                         if (!ji->method->wrapper_type)
572                                 *managed = TRUE;
573
574                 sframe = (MonoPPCStackFrame*)MONO_CONTEXT_GET_SP (ctx);
575                 MONO_CONTEXT_SET_BP (new_ctx, sframe->sp);
576                 if (ji->method->save_lmf) {
577                         memcpy (&new_ctx->fregs, (char*)sframe->sp - sizeof (double) * MONO_SAVED_FREGS, sizeof (double) * MONO_SAVED_FREGS);
578                         memcpy (&new_ctx->regs, (char*)sframe->sp - sizeof (double) * MONO_SAVED_FREGS - sizeof (gulong) * MONO_SAVED_GREGS, sizeof (gulong) * MONO_SAVED_GREGS);
579                 } else if (ji->used_regs) {
580                         /* keep updated with emit_prolog in mini-ppc.c */
581                         offset = 0;
582                         /* FIXME handle floating point args 
583                         for (i = 31; i >= 14; --i) {
584                                 if (ji->used_fregs & (1 << i)) {
585                                         offset += sizeof (double);
586                                         new_ctx->fregs [i - 14] = *(gulong*)((char*)sframe->sp - offset);
587                                 }
588                         }*/
589                         for (i = 31; i >= 13; --i) {
590                                 if (ji->used_regs & (1 << i)) {
591                                         offset += sizeof (gulong);
592                                         new_ctx->regs [i - 13] = *(gulong*)((char*)sframe->sp - offset);
593                                 }
594                         }
595                 }
596                 /* the calling IP is in the parent frame */
597                 sframe = (MonoPPCStackFrame*)sframe->sp;
598                 /* we substract 4, so that the IP points into the call instruction */
599                 MONO_CONTEXT_SET_IP (new_ctx, sframe->lr - 4);
600
601                 return ji;
602         } else if (*lmf) {
603                 
604                 *new_ctx = *ctx;
605                 setup_context (new_ctx);
606
607                 if ((ji = mono_jit_info_table_find (domain, (gpointer)(*lmf)->eip))) {
608                 } else {
609                         if (!(*lmf)->method)
610                                 return (gpointer)-1;
611
612                         memset (res, 0, MONO_SIZEOF_JIT_INFO);
613                         res->method = (*lmf)->method;
614                 }
615
616                 /*sframe = (MonoPPCStackFrame*)MONO_CONTEXT_GET_SP (ctx);
617                 MONO_CONTEXT_SET_BP (new_ctx, sframe->sp);
618                 MONO_CONTEXT_SET_IP (new_ctx, sframe->lr);*/
619                 MONO_CONTEXT_SET_BP (new_ctx, (*lmf)->ebp);
620                 MONO_CONTEXT_SET_IP (new_ctx, (*lmf)->eip);
621                 memcpy (&new_ctx->regs, (*lmf)->iregs, sizeof (gulong) * MONO_SAVED_GREGS);
622                 memcpy (&new_ctx->fregs, (*lmf)->fregs, sizeof (double) * MONO_SAVED_FREGS);
623
624                 /* FIXME: what about trampoline LMF frames?  see exceptions-x86.c */
625
626                 *lmf = (*lmf)->previous_lmf;
627
628                 return ji ? ji : res;
629         }
630
631         return NULL;
632 }
633
634 /*
635  * This is the function called from the signal handler
636  */
637 void
638 mono_arch_sigctx_to_monoctx (void *ctx, MonoContext *mctx)
639 {
640 #ifdef MONO_CROSS_COMPILE
641         g_assert_not_reached ();
642 #else
643         os_ucontext *uc = ctx;
644
645         mctx->sc_ir = UCONTEXT_REG_NIP(uc);
646         mctx->sc_sp = UCONTEXT_REG_Rn(uc, 1);
647         memcpy (&mctx->regs, &UCONTEXT_REG_Rn(uc, 13), sizeof (gulong) * MONO_SAVED_GREGS);
648         memcpy (&mctx->fregs, &UCONTEXT_REG_FPRn(uc, 14), sizeof (double) * MONO_SAVED_FREGS);
649 #endif
650 }
651
652 void
653 mono_arch_monoctx_to_sigctx (MonoContext *mctx, void *ctx)
654 {
655 #ifdef MONO_CROSS_COMPILE
656         g_assert_not_reached ();
657 #else
658         os_ucontext *uc = ctx;
659
660         UCONTEXT_REG_NIP(uc) = mctx->sc_ir;
661         UCONTEXT_REG_Rn(uc, 1) = mctx->sc_sp;
662         memcpy (&UCONTEXT_REG_Rn(uc, 13), &mctx->regs, sizeof (gulong) * MONO_SAVED_GREGS);
663         memcpy (&UCONTEXT_REG_FPRn(uc, 14), &mctx->fregs, sizeof (double) * MONO_SAVED_FREGS);
664 #endif
665 }
666
667 gpointer
668 mono_arch_ip_from_context (void *sigctx)
669 {
670 #ifdef MONO_CROSS_COMPILE
671         g_assert_not_reached ();
672 #else
673         os_ucontext *uc = sigctx;
674         return (gpointer)UCONTEXT_REG_NIP(uc);
675 #endif
676 }
677
678 static void
679 altstack_handle_and_restore (void *sigctx, gpointer obj, gboolean test_only)
680 {
681         void (*restore_context) (MonoContext *);
682         MonoContext mctx;
683
684         restore_context = mono_get_restore_context ();
685         mono_arch_sigctx_to_monoctx (sigctx, &mctx);
686         mono_handle_exception (&mctx, obj, (gpointer)mctx.sc_ir, test_only);
687         restore_context (&mctx);
688 }
689
690 void
691 mono_arch_handle_altstack_exception (void *sigctx, gpointer fault_addr, gboolean stack_ovf)
692 {
693 #ifdef MONO_CROSS_COMPILE
694         g_assert_not_reached ();
695 #else
696 #ifdef MONO_ARCH_USE_SIGACTION
697         os_ucontext *uc = (ucontext_t*)sigctx;
698         os_ucontext *uc_copy;
699         MonoJitInfo *ji = mono_jit_info_table_find (mono_domain_get (), mono_arch_ip_from_context (sigctx));
700         gpointer *sp;
701         int frame_size;
702
703         if (stack_ovf) {
704                 const char *method;
705                 /* we don't do much now, but we can warn the user with a useful message */
706                 fprintf (stderr, "Stack overflow: IP: %p, SP: %p\n", mono_arch_ip_from_context (sigctx), (gpointer)UCONTEXT_REG_Rn(uc, 1));
707                 if (ji && ji->method)
708                         method = mono_method_full_name (ji->method, TRUE);
709                 else
710                         method = "Unmanaged";
711                 fprintf (stderr, "At %s\n", method);
712                 abort ();
713         }
714         if (!ji)
715                 mono_handle_native_sigsegv (SIGSEGV, sigctx);
716         /* setup a call frame on the real stack so that control is returned there
717          * and exception handling can continue.
718          * The frame looks like:
719          *   ucontext struct
720          *   ...
721          * 224 is the size of the red zone
722          */
723         frame_size = sizeof (ucontext_t) + sizeof (gpointer) * 16 + 224;
724         frame_size += 15;
725         frame_size &= ~15;
726         sp = (gpointer)(UCONTEXT_REG_Rn(uc, 1) & ~15);
727         sp = (gpointer)((char*)sp - frame_size);
728         /* may need to adjust pointers in the new struct copy, depending on the OS */
729         uc_copy = (ucontext_t*)(sp + 16);
730         memcpy (uc_copy, uc, sizeof (os_ucontext));
731 #if defined(__linux__) && !defined(__mono_ppc64__)
732         uc_copy->uc_mcontext.uc_regs = (gpointer)((char*)uc_copy + ((char*)uc->uc_mcontext.uc_regs - (char*)uc));
733 #endif
734         g_assert (mono_arch_ip_from_context (uc) == mono_arch_ip_from_context (uc_copy));
735         /* at the return form the signal handler execution starts in altstack_handle_and_restore() */
736         UCONTEXT_REG_LNK(uc) = UCONTEXT_REG_NIP(uc);
737 #ifdef PPC_USES_FUNCTION_DESCRIPTOR
738         {
739                 MonoPPCFunctionDescriptor *handler_ftnptr = (MonoPPCFunctionDescriptor*)altstack_handle_and_restore;
740
741                 UCONTEXT_REG_NIP(uc) = (gulong)handler_ftnptr->code;
742                 UCONTEXT_REG_Rn(uc, 2) = (gulong)handler_ftnptr->toc;
743         }
744 #else
745         UCONTEXT_REG_NIP(uc) = (unsigned long)altstack_handle_and_restore;
746 #endif
747         UCONTEXT_REG_Rn(uc, 1) = (unsigned long)sp;
748         UCONTEXT_REG_Rn(uc, PPC_FIRST_ARG_REG) = (unsigned long)(sp + 16);
749         UCONTEXT_REG_Rn(uc, PPC_FIRST_ARG_REG + 1) = 0;
750         UCONTEXT_REG_Rn(uc, PPC_FIRST_ARG_REG + 2) = 0;
751 #endif
752
753 #endif /* !MONO_CROSS_COMPILE */
754 }
755
756 gboolean
757 mono_arch_handle_exception (void *ctx, gpointer obj, gboolean test_only)
758 {
759         MonoContext mctx;
760         gboolean result;
761
762         mono_arch_sigctx_to_monoctx (ctx, &mctx);
763
764         result = mono_handle_exception (&mctx, obj, (gpointer)mctx.sc_ir, test_only);
765         /* restore the context so that returning from the signal handler will invoke
766          * the catch clause 
767          */
768         mono_arch_monoctx_to_sigctx (&mctx, ctx);
769         return result;
770 }
771
772 gboolean
773 mono_arch_has_unwind_info (gconstpointer addr)
774 {
775         return FALSE;
776 }
777