Fri Jan 16 15:21:21 CET 2009 Paolo Molaro <lupus@ximian.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_load_reg (code, ip_reg, G_STRUCT_OFFSET (MonoContext, sc_ir), ctx_reg);     \
167                 ppc_load_multiple_regs (code, ppc_r13, ctx_reg, G_STRUCT_OFFSET (MonoContext, regs));   \
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 (void)
200 {
201         static guint8 *start = NULL;
202
203         guint8 *code;
204         int size = MONO_PPC_32_64_CASE (128, 172) + PPC_FTNPTR_SIZE;
205
206         if (start)
207                 return start;
208
209         code = start = mono_global_codeman_reserve (size);
210         code = mono_ppc_create_pre_code_ftnptr (code);
211         restore_regs_from_context (ppc_r3, ppc_r4, ppc_r5);
212         /* restore also the stack pointer */
213         ppc_load_reg (code, ppc_sp, G_STRUCT_OFFSET (MonoContext, sc_sp), ppc_r3);
214         //ppc_break (code);
215         /* jump to the saved IP */
216         ppc_mtctr (code, ppc_r4);
217         ppc_bcctr (code, PPC_BR_ALWAYS, 0);
218         /* never reached */
219         ppc_break (code);
220
221         g_assert ((code - start) < size);
222         mono_arch_flush_icache (start, code - start);
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, ppc_sp, pos);
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 (void)
256 {
257         static guint8 *start = NULL;
258
259         guint8 *code;
260         int alloc_size, pos, i;
261         int size = MONO_PPC_32_64_CASE (320, 500) + PPC_FTNPTR_SIZE;
262
263         if (start)
264                 return start;
265
266         /* call_filter (MonoContext *ctx, unsigned long eip, gpointer exc) */
267         code = start = mono_global_codeman_reserve (size);
268         code = mono_ppc_create_pre_code_ftnptr (code);
269
270         /* store ret addr */
271         ppc_mflr (code, ppc_r0);
272         ppc_store_reg (code, ppc_r0, PPC_RET_ADDR_OFFSET, ppc_sp);
273
274         alloc_size = REG_SAVE_STACK_FRAME_SIZE;
275
276         /* allocate stack frame and set link from sp in ctx */
277         g_assert ((alloc_size & (MONO_ARCH_FRAME_ALIGNMENT-1)) == 0);
278         ppc_load_reg (code, ppc_r0, G_STRUCT_OFFSET (MonoContext, sc_sp), ppc_r3);
279         ppc_load_reg_indexed (code, ppc_r0, ppc_r0, ppc_r0);
280         ppc_store_reg_update (code, ppc_r0, -alloc_size, ppc_sp);
281
282         code = emit_save_saved_regs (code, alloc_size);
283
284         /* restore all the regs from ctx (in r3), but not r1, the stack pointer */
285         restore_regs_from_context (ppc_r3, ppc_r6, ppc_r7);
286         /* call handler at eip (r4) and set the first arg with the exception (r5) */
287         ppc_mtctr (code, ppc_r4);
288         ppc_mr (code, ppc_r3, ppc_r5);
289         ppc_bcctrl (code, PPC_BR_ALWAYS, 0);
290
291         /* epilog */
292         ppc_load_reg (code, ppc_r0, alloc_size + PPC_RET_ADDR_OFFSET, ppc_sp);
293         ppc_mtlr (code, ppc_r0);
294
295         /* restore all the regs from the stack */
296         pos = alloc_size;
297         for (i = 31; i >= 14; --i) {
298                 pos -= sizeof (gdouble);
299                 ppc_lfd (code, i, pos, ppc_sp);
300         }
301         pos -= sizeof (gpointer) * MONO_SAVED_GREGS;
302         ppc_load_multiple_regs (code, ppc_r13, ppc_sp, pos);
303
304         ppc_addic (code, ppc_sp, ppc_sp, alloc_size);
305         ppc_blr (code);
306
307         g_assert ((code - start) < size);
308         mono_arch_flush_icache (start, code - start);
309         return start;
310 }
311
312 static void
313 throw_exception (MonoObject *exc, unsigned long eip, unsigned long esp, gulong *int_regs, gdouble *fp_regs, gboolean rethrow)
314 {
315         static void (*restore_context) (MonoContext *);
316         MonoContext ctx;
317
318         if (!restore_context)
319                 restore_context = mono_arch_get_restore_context ();
320
321         /* adjust eip so that it point into the call instruction */
322         eip -= 4;
323
324         setup_context (&ctx);
325
326         /*printf ("stack in throw: %p\n", esp);*/
327         MONO_CONTEXT_SET_BP (&ctx, esp);
328         MONO_CONTEXT_SET_IP (&ctx, eip);
329         memcpy (&ctx.regs, int_regs, sizeof (gulong) * MONO_SAVED_GREGS);
330         memcpy (&ctx.fregs, fp_regs, sizeof (double) * MONO_SAVED_FREGS);
331
332         if (mono_object_isinst (exc, mono_defaults.exception_class)) {
333                 MonoException *mono_ex = (MonoException*)exc;
334                 if (!rethrow)
335                         mono_ex->stack_trace = NULL;
336         }
337         mono_handle_exception (&ctx, exc, (gpointer)eip, FALSE);
338         restore_context (&ctx);
339
340         g_assert_not_reached ();
341 }
342
343 /**
344  * arch_get_throw_exception_generic:
345  *
346  * Returns a function pointer which can be used to raise 
347  * exceptions. The returned function has the following 
348  * signature: void (*func) (MonoException *exc); or
349  * void (*func) (char *exc_name);
350  *
351  */
352 static gpointer 
353 mono_arch_get_throw_exception_generic (guint8 *start, int size, int by_name, gboolean rethrow)
354 {
355         guint8 *code;
356         int alloc_size, pos;
357
358         code = mono_ppc_create_pre_code_ftnptr (start);
359
360         /* store ret addr */
361         ppc_mflr (code, ppc_r0);
362         ppc_store_reg (code, ppc_r0, PPC_RET_ADDR_OFFSET, ppc_sp);
363
364         alloc_size = REG_SAVE_STACK_FRAME_SIZE;
365
366         g_assert ((alloc_size & (MONO_ARCH_FRAME_ALIGNMENT-1)) == 0);
367         ppc_store_reg_update (code, ppc_sp, -alloc_size, ppc_sp);
368
369         code = emit_save_saved_regs (code, alloc_size);
370
371         //ppc_break (code);
372         if (by_name) {
373                 ppc_mr (code, ppc_r5, ppc_r3);
374                 ppc_load (code, ppc_r3, (gulong)mono_defaults.corlib);
375                 ppc_load (code, ppc_r4, "System");
376                 ppc_load_func (code, ppc_r0, mono_exception_from_name);
377                 ppc_mtctr (code, ppc_r0);
378                 ppc_bcctrl (code, PPC_BR_ALWAYS, 0);
379         }
380
381         /* call throw_exception (exc, ip, sp, int_regs, fp_regs) */
382         /* caller sp */
383         ppc_load_reg (code, ppc_r5, 0, ppc_sp);
384         /* exc is already in place in r3 */
385         if (by_name)
386                 ppc_load_reg (code, ppc_r4, PPC_RET_ADDR_OFFSET, ppc_r5);
387         else
388                 ppc_mr (code, ppc_r4, ppc_r0); /* caller ip */
389         /* pointer to the saved fp regs */
390         pos = alloc_size - sizeof (gdouble) * MONO_SAVED_FREGS;
391         ppc_addi (code, ppc_r7, ppc_sp, pos);
392         /* pointer to the saved int regs */
393         pos -= sizeof (gpointer) * MONO_SAVED_GREGS;
394         ppc_addi (code, ppc_r6, ppc_sp, pos);
395         ppc_li (code, ppc_r8, rethrow);
396
397         ppc_load_func (code, ppc_r0, throw_exception);
398         ppc_mtctr (code, ppc_r0);
399         ppc_bcctrl (code, PPC_BR_ALWAYS, 0);
400         /* we should never reach this breakpoint */
401         ppc_break (code);
402         g_assert ((code - start) <= size);
403         mono_arch_flush_icache (start, code - start);
404         return start;
405 }
406
407 /**
408  * mono_arch_get_rethrow_exception:
409  *
410  * Returns a function pointer which can be used to rethrow 
411  * exceptions. The returned function has the following 
412  * signature: void (*func) (MonoException *exc); 
413  *
414  */
415 gpointer
416 mono_arch_get_rethrow_exception (void)
417 {
418         static guint8 *start = NULL;
419         static int inited = 0;
420
421         guint8 *code;
422         int size = MONO_PPC_32_64_CASE (132, 224) + PPC_FTNPTR_SIZE;
423
424         if (inited)
425                 return start;
426         code = mono_global_codeman_reserve (size);
427         start = mono_arch_get_throw_exception_generic (code, size, FALSE, TRUE);
428         inited = 1;
429         return start;
430 }
431 /**
432  * arch_get_throw_exception:
433  *
434  * Returns a function pointer which can be used to raise 
435  * exceptions. The returned function has the following 
436  * signature: void (*func) (MonoException *exc); 
437  * For example to raise an arithmetic exception you can use:
438  *
439  * x86_push_imm (code, mono_get_exception_arithmetic ()); 
440  * x86_call_code (code, arch_get_throw_exception ()); 
441  *
442  */
443 gpointer 
444 mono_arch_get_throw_exception (void)
445 {
446         static guint8 *start = NULL;
447         static int inited = 0;
448
449         guint8 *code;
450         int size = MONO_PPC_32_64_CASE (132, 224) + PPC_FTNPTR_SIZE;
451
452         if (inited)
453                 return start;
454         code = mono_global_codeman_reserve (size);
455         start = mono_arch_get_throw_exception_generic (code, size, FALSE, FALSE);
456         inited = 1;
457         return start;
458 }
459
460 /**
461  * arch_get_throw_exception_by_name:
462  *
463  * Returns a function pointer which can be used to raise 
464  * corlib exceptions. The returned function has the following 
465  * signature: void (*func) (char *exc_name); 
466  * For example to raise an arithmetic exception you can use:
467  *
468  * x86_push_imm (code, "ArithmeticException"); 
469  * x86_call_code (code, arch_get_throw_exception_by_name ()); 
470  *
471  */
472 gpointer 
473 mono_arch_get_throw_exception_by_name (void)
474 {
475         static guint8 *start = NULL;
476         static int inited = 0;
477
478         guint8 *code;
479         int size = MONO_PPC_32_64_CASE (168, 304) + PPC_FTNPTR_SIZE;
480
481         if (inited)
482                 return start;
483         code = mono_global_codeman_reserve (size);
484         start = mono_arch_get_throw_exception_generic (code, size, TRUE, FALSE);
485         inited = 1;
486         return start;
487 }
488
489 /* mono_arch_find_jit_info:
490  *
491  * This function is used to gather information from @ctx. It return the 
492  * MonoJitInfo of the corresponding function, unwinds one stack frame and
493  * stores the resulting context into @new_ctx. It also stores a string 
494  * describing the stack location into @trace (if not NULL), and modifies
495  * the @lmf if necessary. @native_offset return the IP offset from the 
496  * start of the function or -1 if that info is not available.
497  */
498 MonoJitInfo *
499 mono_arch_find_jit_info (MonoDomain *domain, MonoJitTlsData *jit_tls, MonoJitInfo *res, MonoJitInfo *prev_ji, MonoContext *ctx,
500                                                  MonoContext *new_ctx, MonoLMF **lmf, gboolean *managed)
501 {
502         MonoJitInfo *ji;
503         gpointer ip = MONO_CONTEXT_GET_IP (ctx);
504         MonoPPCStackFrame *sframe;
505
506         /* Avoid costly table lookup during stack overflow */
507         if (prev_ji && (ip > prev_ji->code_start && ((guint8*)ip < ((guint8*)prev_ji->code_start) + prev_ji->code_size)))
508                 ji = prev_ji;
509         else
510                 ji = mono_jit_info_table_find (domain, ip);
511
512         if (managed)
513                 *managed = FALSE;
514
515         if (ji != NULL) {
516                 gint32 address;
517                 int offset, i;
518
519                 *new_ctx = *ctx;
520                 setup_context (new_ctx);
521
522                 if (*lmf && (MONO_CONTEXT_GET_SP (ctx) >= (gpointer)(*lmf)->ebp)) {
523                         /* remove any unused lmf */
524                         *lmf = (*lmf)->previous_lmf;
525                 }
526
527                 address = (char *)ip - (char *)ji->code_start;
528
529                 if (managed)
530                         if (!ji->method->wrapper_type)
531                                 *managed = TRUE;
532
533                 sframe = (MonoPPCStackFrame*)MONO_CONTEXT_GET_SP (ctx);
534                 MONO_CONTEXT_SET_BP (new_ctx, sframe->sp);
535                 if (ji->method->save_lmf) {
536                         memcpy (&new_ctx->fregs, (char*)sframe->sp - sizeof (double) * MONO_SAVED_FREGS, sizeof (double) * MONO_SAVED_FREGS);
537                         memcpy (&new_ctx->regs, (char*)sframe->sp - sizeof (double) * MONO_SAVED_FREGS - sizeof (gulong) * MONO_SAVED_GREGS, sizeof (gulong) * MONO_SAVED_GREGS);
538                 } else if (ji->used_regs) {
539                         /* keep updated with emit_prolog in mini-ppc.c */
540                         offset = 0;
541                         /* FIXME handle floating point args 
542                         for (i = 31; i >= 14; --i) {
543                                 if (ji->used_fregs & (1 << i)) {
544                                         offset += sizeof (double);
545                                         new_ctx->fregs [i - 14] = *(gulong*)((char*)sframe->sp - offset);
546                                 }
547                         }*/
548                         for (i = 31; i >= 13; --i) {
549                                 if (ji->used_regs & (1 << i)) {
550                                         offset += sizeof (gulong);
551                                         new_ctx->regs [i - 13] = *(gulong*)((char*)sframe->sp - offset);
552                                 }
553                         }
554                 }
555                 /* the calling IP is in the parent frame */
556                 sframe = (MonoPPCStackFrame*)sframe->sp;
557                 /* we substract 4, so that the IP points into the call instruction */
558                 MONO_CONTEXT_SET_IP (new_ctx, sframe->lr - 4);
559
560                 return ji;
561         } else if (*lmf) {
562                 
563                 *new_ctx = *ctx;
564                 setup_context (new_ctx);
565
566                 if ((ji = mono_jit_info_table_find (domain, (gpointer)(*lmf)->eip))) {
567                 } else {
568                         if (!(*lmf)->method)
569                                 return (gpointer)-1;
570
571                         memset (res, 0, sizeof (MonoJitInfo));
572                         res->method = (*lmf)->method;
573                 }
574
575                 /*sframe = (MonoPPCStackFrame*)MONO_CONTEXT_GET_SP (ctx);
576                 MONO_CONTEXT_SET_BP (new_ctx, sframe->sp);
577                 MONO_CONTEXT_SET_IP (new_ctx, sframe->lr);*/
578                 MONO_CONTEXT_SET_BP (new_ctx, (*lmf)->ebp);
579                 MONO_CONTEXT_SET_IP (new_ctx, (*lmf)->eip);
580                 memcpy (&new_ctx->regs, (*lmf)->iregs, sizeof (gulong) * MONO_SAVED_GREGS);
581                 memcpy (&new_ctx->fregs, (*lmf)->fregs, sizeof (double) * MONO_SAVED_FREGS);
582
583                 /* FIXME: what about trampoline LMF frames?  see exceptions-x86.c */
584
585                 *lmf = (*lmf)->previous_lmf;
586
587                 return ji ? ji : res;
588         }
589
590         return NULL;
591 }
592
593 /*
594  * This is the function called from the signal handler
595  */
596 void
597 mono_arch_sigctx_to_monoctx (void *ctx, MonoContext *mctx)
598 {
599         os_ucontext *uc = ctx;
600
601         mctx->sc_ir = UCONTEXT_REG_NIP(uc);
602         mctx->sc_sp = UCONTEXT_REG_Rn(uc, 1);
603         memcpy (&mctx->regs, &UCONTEXT_REG_Rn(uc, 13), sizeof (gulong) * MONO_SAVED_GREGS);
604         memcpy (&mctx->fregs, &UCONTEXT_REG_FPRn(uc, 14), sizeof (double) * MONO_SAVED_FREGS);
605 }
606
607 void
608 mono_arch_monoctx_to_sigctx (MonoContext *mctx, void *ctx)
609 {
610         os_ucontext *uc = ctx;
611
612         UCONTEXT_REG_NIP(uc) = mctx->sc_ir;
613         UCONTEXT_REG_Rn(uc, 1) = mctx->sc_sp;
614         memcpy (&UCONTEXT_REG_Rn(uc, 13), &mctx->regs, sizeof (gulong) * MONO_SAVED_GREGS);
615         memcpy (&UCONTEXT_REG_FPRn(uc, 14), &mctx->fregs, sizeof (double) * MONO_SAVED_FREGS);
616 }
617
618 gpointer
619 mono_arch_ip_from_context (void *sigctx)
620 {
621         os_ucontext *uc = sigctx;
622         return (gpointer)UCONTEXT_REG_NIP(uc);
623 }
624
625 static void
626 altstack_handle_and_restore (void *sigctx, gpointer obj, gboolean test_only)
627 {
628         void (*restore_context) (MonoContext *);
629         MonoContext mctx;
630
631         restore_context = mono_arch_get_restore_context ();
632         mono_arch_sigctx_to_monoctx (sigctx, &mctx);
633         mono_handle_exception (&mctx, obj, (gpointer)mctx.sc_ir, test_only);
634         restore_context (&mctx);
635 }
636
637 void
638 mono_arch_handle_altstack_exception (void *sigctx, gpointer fault_addr, gboolean stack_ovf)
639 {
640 #ifdef MONO_ARCH_USE_SIGACTION
641         os_ucontext *uc = (ucontext_t*)sigctx;
642         os_ucontext *uc_copy;
643         MonoJitInfo *ji = mono_jit_info_table_find (mono_domain_get (), mono_arch_ip_from_context (sigctx));
644         gpointer *sp;
645         int frame_size;
646
647         if (stack_ovf) {
648                 const char *method;
649                 /* we don't do much now, but we can warn the user with a useful message */
650                 fprintf (stderr, "Stack overflow: IP: %p, SP: %p\n", mono_arch_ip_from_context (sigctx), (gpointer)UCONTEXT_REG_Rn(uc, 1));
651                 if (ji && ji->method)
652                         method = mono_method_full_name (ji->method, TRUE);
653                 else
654                         method = "Unmanaged";
655                 fprintf (stderr, "At %s\n", method);
656                 abort ();
657         }
658         if (!ji)
659                 mono_handle_native_sigsegv (SIGSEGV, sigctx);
660         /* setup a call frame on the real stack so that control is returned there
661          * and exception handling can continue.
662          * The frame looks like:
663          *   ucontext struct
664          *   ...
665          * 224 is the size of the red zone
666          */
667         frame_size = sizeof (ucontext_t) + sizeof (gpointer) * 16 + 224;
668         frame_size += 15;
669         frame_size &= ~15;
670         sp = (gpointer)(UCONTEXT_REG_Rn(uc, 1) & ~15);
671         sp = (gpointer)((char*)sp - frame_size);
672         /* may need to adjust pointers in the new struct copy, depending on the OS */
673         uc_copy = (ucontext_t*)(sp + 16);
674         memcpy (uc_copy, uc, sizeof (os_ucontext));
675 #if defined(__linux__) && !defined(__mono_ppc64__)
676         uc_copy->uc_mcontext.uc_regs = (gpointer)((char*)uc_copy + ((char*)uc->uc_mcontext.uc_regs - (char*)uc));
677 #endif
678         g_assert (mono_arch_ip_from_context (uc) == mono_arch_ip_from_context (uc_copy));
679         /* at the return form the signal handler execution starts in altstack_handle_and_restore() */
680         UCONTEXT_REG_LNK(uc) = UCONTEXT_REG_NIP(uc);
681 #ifdef PPC_USES_FUNCTION_DESCRIPTOR
682         {
683                 MonoPPCFunctionDescriptor *handler_ftnptr = (MonoPPCFunctionDescriptor*)altstack_handle_and_restore;
684
685                 UCONTEXT_REG_NIP(uc) = (gulong)handler_ftnptr->code;
686                 UCONTEXT_REG_Rn(uc, 2) = (gulong)handler_ftnptr->toc;
687         }
688 #else
689         UCONTEXT_REG_NIP(uc) = (unsigned long)altstack_handle_and_restore;
690 #endif
691         UCONTEXT_REG_Rn(uc, 1) = (unsigned long)sp;
692         UCONTEXT_REG_Rn(uc, PPC_FIRST_ARG_REG) = (unsigned long)(sp + 16);
693         UCONTEXT_REG_Rn(uc, PPC_FIRST_ARG_REG + 1) = 0;
694         UCONTEXT_REG_Rn(uc, PPC_FIRST_ARG_REG + 2) = 0;
695 #endif
696 }
697
698 gboolean
699 mono_arch_handle_exception (void *ctx, gpointer obj, gboolean test_only)
700 {
701         MonoContext mctx;
702         gboolean result;
703
704         mono_arch_sigctx_to_monoctx (ctx, &mctx);
705
706         result = mono_handle_exception (&mctx, obj, (gpointer)mctx.sc_ir, test_only);
707         /* restore the context so that returning from the signal handler will invoke
708          * the catch clause 
709          */
710         mono_arch_monoctx_to_sigctx (&mctx, ctx);
711         return result;
712 }
713
714 gboolean
715 mono_arch_has_unwind_info (gconstpointer addr)
716 {
717         return FALSE;
718 }
719