Merge remote-tracking branch 'mfoliveira/ppc64el-v2'
[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 (MonoTrampInfo **info, gboolean aot)
200 {
201         guint8 *start, *code;
202         int size = MONO_PPC_32_64_CASE (128, 172) + PPC_FTNPTR_SIZE;
203         MonoJumpInfo *ji = NULL;
204         GSList *unwind_ops = NULL;
205
206         code = start = mono_global_codeman_reserve (size);
207         if (!aot)
208                 code = mono_ppc_create_pre_code_ftnptr (code);
209         restore_regs_from_context (ppc_r3, ppc_r4, ppc_r5);
210         /* restore also the stack pointer */
211         ppc_ldptr (code, ppc_sp, G_STRUCT_OFFSET (MonoContext, sc_sp), ppc_r3);
212         //ppc_break (code);
213         /* jump to the saved IP */
214         ppc_mtctr (code, ppc_r4);
215         ppc_bcctr (code, PPC_BR_ALWAYS, 0);
216         /* never reached */
217         ppc_break (code);
218
219         g_assert ((code - start) <= size);
220         mono_arch_flush_icache (start, code - start);
221
222         if (info)
223                 *info = mono_tramp_info_create ("restore_context", start, code - start, ji, unwind_ops);
224
225         return start;
226 }
227
228 #define SAVED_REGS_LENGTH               (sizeof (gdouble) * MONO_SAVED_FREGS + sizeof (gpointer) * MONO_SAVED_GREGS)
229 #define ALIGN_STACK_FRAME_SIZE(s)       (((s) + MONO_ARCH_FRAME_ALIGNMENT - 1) & ~(MONO_ARCH_FRAME_ALIGNMENT - 1))
230 /* The 64 bytes here are for outgoing arguments and a bit of spare.
231    We don't use it all, but it doesn't hurt. */
232 #define REG_SAVE_STACK_FRAME_SIZE       (ALIGN_STACK_FRAME_SIZE (SAVED_REGS_LENGTH + PPC_MINIMAL_STACK_SIZE + 64))
233
234 static guint8*
235 emit_save_saved_regs (guint8 *code, int pos)
236 {
237         int i;
238
239         for (i = 31; i >= 14; --i) {
240                 pos -= sizeof (gdouble);
241                 ppc_stfd (code, i, pos, ppc_sp);
242         }
243         pos -= sizeof (gpointer) * MONO_SAVED_GREGS;
244         ppc_store_multiple_regs (code, ppc_r13, pos, ppc_sp);
245
246         return code;
247 }
248
249 /*
250  * mono_arch_get_call_filter:
251  *
252  * Returns a pointer to a method which calls an exception filter. We
253  * also use this function to call finally handlers (we pass NULL as 
254  * @exc object in this case).
255  */
256 gpointer
257 mono_arch_get_call_filter (MonoTrampInfo **info, gboolean aot)
258 {
259         guint8 *start, *code;
260         int alloc_size, pos, i;
261         int size = MONO_PPC_32_64_CASE (320, 500) + PPC_FTNPTR_SIZE;
262         MonoJumpInfo *ji = NULL;
263         GSList *unwind_ops = NULL;
264
265         /* call_filter (MonoContext *ctx, unsigned long eip, gpointer exc) */
266         code = start = mono_global_codeman_reserve (size);
267         if (!aot)
268                 code = mono_ppc_create_pre_code_ftnptr (code);
269
270         /* store ret addr */
271         ppc_mflr (code, ppc_r0);
272         ppc_stptr (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_ldptr (code, ppc_r0, G_STRUCT_OFFSET (MonoContext, sc_sp), ppc_r3);
279         ppc_ldptr_indexed (code, ppc_r0, ppc_r0, ppc_r0);
280         ppc_stptr_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_ldptr (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, pos, ppc_sp);
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
310         if (info)
311                 *info = mono_tramp_info_create ("call_filter", start, code - start, ji, unwind_ops);
312
313         return start;
314 }
315
316 void
317 mono_ppc_throw_exception (MonoObject *exc, unsigned long eip, unsigned long esp, mgreg_t *int_regs, gdouble *fp_regs, gboolean rethrow)
318 {
319         MonoContext ctx;
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 (mgreg_t) * 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);
338         mono_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) (guint32 ex_token, gpointer ip)
350  *
351  */
352 static gpointer
353 mono_arch_get_throw_exception_generic (int size, MonoTrampInfo **info, int corlib, gboolean rethrow, gboolean aot)
354 {
355         guint8 *start, *code;
356         int alloc_size, pos;
357         MonoJumpInfo *ji = NULL;
358         GSList *unwind_ops = NULL;
359
360         code = start = mono_global_codeman_reserve (size);
361         if (!aot)
362                 code = mono_ppc_create_pre_code_ftnptr (code);
363
364         /* store ret addr */
365         if (corlib)
366                 ppc_mr (code, ppc_r0, ppc_r4);
367         else
368                 ppc_mflr (code, ppc_r0);
369         ppc_stptr (code, ppc_r0, PPC_RET_ADDR_OFFSET, ppc_sp);
370
371         alloc_size = REG_SAVE_STACK_FRAME_SIZE;
372
373         g_assert ((alloc_size & (MONO_ARCH_FRAME_ALIGNMENT-1)) == 0);
374         ppc_stptr_update (code, ppc_sp, -alloc_size, ppc_sp);
375
376         code = emit_save_saved_regs (code, alloc_size);
377
378         //ppc_break (code);
379         if (corlib) {
380                 ppc_mr (code, ppc_r4, ppc_r3);
381
382                 if (aot) {
383                         code = mono_arch_emit_load_aotconst (start, code, &ji, MONO_PATCH_INFO_IMAGE, mono_defaults.corlib);
384                         ppc_mr (code, ppc_r3, ppc_r12);
385                         code = mono_arch_emit_load_aotconst (start, code, &ji, MONO_PATCH_INFO_JIT_ICALL_ADDR, "mono_exception_from_token");
386 #ifdef PPC_USES_FUNCTION_DESCRIPTOR
387                         ppc_ldptr (code, ppc_r2, sizeof (gpointer), ppc_r12);
388                         ppc_ldptr (code, ppc_r12, 0, ppc_r12);
389 #endif
390                         ppc_mtctr (code, ppc_r12);
391                         ppc_bcctrl (code, PPC_BR_ALWAYS, 0);
392                 } else {
393                         ppc_load (code, ppc_r3, (gulong)mono_defaults.corlib);
394                         ppc_load_func (code, PPC_CALL_REG, mono_exception_from_token);
395                         ppc_mtctr (code, PPC_CALL_REG);
396                         ppc_bcctrl (code, PPC_BR_ALWAYS, 0);
397                 }
398         }
399
400         /* call throw_exception (exc, ip, sp, int_regs, fp_regs) */
401         /* caller sp */
402         ppc_ldptr (code, ppc_r5, 0, ppc_sp);
403         /* exc is already in place in r3 */
404         if (corlib)
405                 ppc_ldptr (code, ppc_r4, PPC_RET_ADDR_OFFSET, ppc_r5);
406         else
407                 ppc_mr (code, ppc_r4, ppc_r0); /* caller ip */
408         /* pointer to the saved fp regs */
409         pos = alloc_size - sizeof (gdouble) * MONO_SAVED_FREGS;
410         ppc_addi (code, ppc_r7, ppc_sp, pos);
411         /* pointer to the saved int regs */
412         pos -= sizeof (gpointer) * MONO_SAVED_GREGS;
413         ppc_addi (code, ppc_r6, ppc_sp, pos);
414         ppc_li (code, ppc_r8, rethrow);
415
416         if (aot) {
417                 // This can be called from runtime code, which can't guarantee that
418                 // r30 contains the got address.
419                 // So emit the got address loading code too
420                 code = mono_arch_emit_load_got_addr (start, code, NULL, &ji);
421                 code = mono_arch_emit_load_aotconst (start, code, &ji, MONO_PATCH_INFO_JIT_ICALL_ADDR, "mono_ppc_throw_exception");
422 #ifdef PPC_USES_FUNCTION_DESCRIPTOR
423                 ppc_ldptr (code, ppc_r2, sizeof (gpointer), ppc_r12);
424                 ppc_ldptr (code, ppc_r12, 0, ppc_r12);
425 #endif
426                 ppc_mtctr (code, ppc_r12);
427                 ppc_bcctrl (code, PPC_BR_ALWAYS, 0);
428         } else {
429                 ppc_load_func (code, PPC_CALL_REG, mono_ppc_throw_exception);
430                 ppc_mtctr (code, PPC_CALL_REG);
431                 ppc_bcctrl (code, PPC_BR_ALWAYS, 0);
432         }
433         /* we should never reach this breakpoint */
434         ppc_break (code);
435         g_assert ((code - start) <= size);
436         mono_arch_flush_icache (start, code - start);
437
438         if (info)
439                 *info = mono_tramp_info_create (corlib ? "throw_corlib_exception" : (rethrow ? "rethrow_exception" : "throw_exception"), start, code - start, ji, unwind_ops);
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 (MonoTrampInfo **info, 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, info, 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 (MonoTrampInfo **info, 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, info, FALSE, FALSE, aot);
482 }
483
484 /**
485  * mono_arch_get_throw_corlib_exception:
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) (guint32 ex_token, guint32 offset); 
490  * On PPC, we pass the ip instead of the offset
491  */
492 gpointer
493 mono_arch_get_throw_corlib_exception (MonoTrampInfo **info, gboolean aot)
494 {
495         int size = MONO_PPC_32_64_CASE (168, 304) + PPC_FTNPTR_SIZE;
496
497         if (aot)
498                 size += 64;
499         return mono_arch_get_throw_exception_generic (size, info, TRUE, FALSE, aot);
500 }
501
502 /*
503  * mono_arch_find_jit_info:
504  *
505  * See exceptions-amd64.c for docs.
506  */
507 gboolean
508 mono_arch_find_jit_info (MonoDomain *domain, MonoJitTlsData *jit_tls, 
509                                                          MonoJitInfo *ji, MonoContext *ctx, 
510                                                          MonoContext *new_ctx, MonoLMF **lmf,
511                                                          mgreg_t **save_locations,
512                                                          StackFrameInfo *frame)
513 {
514         gpointer ip = MONO_CONTEXT_GET_IP (ctx);
515         MonoPPCStackFrame *sframe;
516
517         memset (frame, 0, sizeof (StackFrameInfo));
518         frame->ji = ji;
519
520         *new_ctx = *ctx;
521         setup_context (new_ctx);
522
523         if (ji != NULL) {
524                 int i;
525                 mgreg_t regs [ppc_lr + 1];
526                 guint8 *cfa;
527                 guint32 unwind_info_len;
528                 guint8 *unwind_info;
529
530                 frame->type = FRAME_TYPE_MANAGED;
531
532                 unwind_info = mono_jinfo_get_unwind_info (ji, &unwind_info_len);
533
534                 sframe = (MonoPPCStackFrame*)MONO_CONTEXT_GET_SP (ctx);
535                 MONO_CONTEXT_SET_BP (new_ctx, sframe->sp);
536                 if (jinfo_get_method (ji)->save_lmf) {
537                         /* sframe->sp points just past the end of the LMF */
538                         guint8 *lmf_addr = (guint8*)sframe->sp - sizeof (MonoLMF);
539                         memcpy (&new_ctx->fregs, lmf_addr + G_STRUCT_OFFSET (MonoLMF, fregs), sizeof (double) * MONO_SAVED_FREGS);
540                         memcpy (&new_ctx->regs, lmf_addr + G_STRUCT_OFFSET (MonoLMF, iregs), sizeof (mgreg_t) * MONO_SAVED_GREGS);
541                         /* the calling IP is in the parent frame */
542                         sframe = (MonoPPCStackFrame*)sframe->sp;
543                         /* we substract 4, so that the IP points into the call instruction */
544                         MONO_CONTEXT_SET_IP (new_ctx, sframe->lr - 4);
545                 } else {
546                         regs [ppc_lr] = ctx->sc_ir;
547                         regs [ppc_sp] = ctx->sc_sp;
548                         for (i = 0; i < MONO_SAVED_GREGS; ++i)
549                                 regs [ppc_r13 + i] = ctx->regs [i];
550
551                         mono_unwind_frame (unwind_info, unwind_info_len, ji->code_start, 
552                                                            (guint8*)ji->code_start + ji->code_size,
553                                                            ip, NULL, regs, ppc_lr + 1,
554                                                            save_locations, MONO_MAX_IREGS, &cfa);
555
556                         /* we substract 4, so that the IP points into the call instruction */
557                         MONO_CONTEXT_SET_IP (new_ctx, regs [ppc_lr] - 4);
558                         MONO_CONTEXT_SET_BP (new_ctx, cfa);
559
560                         for (i = 0; i < MONO_SAVED_GREGS; ++i)
561                                 new_ctx->regs [i] = regs [ppc_r13 + i];
562                 }
563
564                 return TRUE;
565         } else if (*lmf) {
566                 
567                 if ((ji = mini_jit_info_table_find (domain, (gpointer)(*lmf)->eip, NULL))) {
568                 } else {
569                         if (!(*lmf)->method)
570                                 return FALSE;
571
572                         /* Trampoline lmf frame */
573                         frame->method = (*lmf)->method;
574                 }
575
576                 /*sframe = (MonoPPCStackFrame*)MONO_CONTEXT_GET_SP (ctx);
577                 MONO_CONTEXT_SET_BP (new_ctx, sframe->sp);
578                 MONO_CONTEXT_SET_IP (new_ctx, sframe->lr);*/
579                 MONO_CONTEXT_SET_BP (new_ctx, (*lmf)->ebp);
580                 MONO_CONTEXT_SET_IP (new_ctx, (*lmf)->eip);
581                 memcpy (&new_ctx->regs, (*lmf)->iregs, sizeof (mgreg_t) * MONO_SAVED_GREGS);
582                 memcpy (&new_ctx->fregs, (*lmf)->fregs, sizeof (double) * MONO_SAVED_FREGS);
583
584                 frame->ji = ji;
585                 frame->type = FRAME_TYPE_MANAGED_TO_NATIVE;
586
587                 /* FIXME: what about trampoline LMF frames?  see exceptions-x86.c */
588
589                 *lmf = (*lmf)->previous_lmf;
590
591                 return TRUE;
592         }
593
594         return FALSE;
595 }
596
597 gpointer
598 mono_arch_ip_from_context (void *sigctx)
599 {
600 #ifdef MONO_CROSS_COMPILE
601         g_assert_not_reached ();
602 #else
603         os_ucontext *uc = sigctx;
604         return (gpointer)UCONTEXT_REG_NIP(uc);
605 #endif
606 }
607
608 void
609 mono_ppc_set_func_into_sigctx (void *sigctx, void *func)
610 {
611 #ifdef MONO_CROSS_COMPILE
612         g_assert_not_reached ();
613 #elif defined(PPC_USES_FUNCTION_DESCRIPTOR)
614         /* Have to set both the ip and the TOC reg */
615         os_ucontext *uc = sigctx;
616
617         UCONTEXT_REG_NIP(uc) = ((gsize*)func) [0];
618         UCONTEXT_REG_Rn (uc, 2) = ((gsize*)func)[1];
619 #else
620         g_assert_not_reached ();
621 #endif
622 }
623
624 static void
625 altstack_handle_and_restore (void *sigctx, gpointer obj)
626 {
627         MonoContext mctx;
628
629         mono_sigctx_to_monoctx (sigctx, &mctx);
630         mono_handle_exception (&mctx, obj);
631         mono_restore_context (&mctx);
632 }
633
634 void
635 mono_arch_handle_altstack_exception (void *sigctx, gpointer fault_addr, gboolean stack_ovf)
636 {
637 #ifdef MONO_CROSS_COMPILE
638         g_assert_not_reached ();
639 #else
640 #ifdef MONO_ARCH_USE_SIGACTION
641         os_ucontext *uc = (ucontext_t*)sigctx;
642         os_ucontext *uc_copy;
643         MonoJitInfo *ji = mini_jit_info_table_find (mono_domain_get (), mono_arch_ip_from_context (sigctx), NULL);
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 && jinfo_get_method (ji))
652                         method = mono_method_full_name (jinfo_get_method (ji), 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 #endif /* !MONO_CROSS_COMPILE */
698 }
699
700 /*
701  * handle_exception:
702  *
703  *   Called by resuming from a signal handler.
704  */
705 static void
706 handle_signal_exception (gpointer obj)
707 {
708         MonoJitTlsData *jit_tls = mono_native_tls_get_value (mono_jit_tls_id);
709         MonoContext ctx;
710
711         memcpy (&ctx, &jit_tls->ex_ctx, sizeof (MonoContext));
712
713         mono_handle_exception (&ctx, obj);
714
715         mono_restore_context (&ctx);
716 }
717
718 static void
719 setup_ucontext_return (void *uc, gpointer func)
720 {
721 #if !defined(MONO_CROSS_COMPILE)
722         UCONTEXT_REG_LNK(uc) = UCONTEXT_REG_NIP(uc);
723 #ifdef PPC_USES_FUNCTION_DESCRIPTOR
724         {
725                 MonoPPCFunctionDescriptor *handler_ftnptr = (MonoPPCFunctionDescriptor*)func;
726
727                 UCONTEXT_REG_NIP(uc) = (gulong)handler_ftnptr->code;
728                 UCONTEXT_REG_Rn(uc, 2) = (gulong)handler_ftnptr->toc;
729         }
730 #else
731         UCONTEXT_REG_NIP(uc) = (unsigned long)func;
732 #endif
733 #endif
734 }
735
736 gboolean
737 mono_arch_handle_exception (void *ctx, gpointer obj)
738 {
739 #if defined(MONO_ARCH_USE_SIGACTION) && defined(UCONTEXT_REG_Rn)
740         /*
741          * Handling the exception in the signal handler is problematic, since the original
742          * signal is disabled, and we could run arbitrary code though the debugger. So
743          * resume into the normal stack and do most work there if possible.
744          */
745         MonoJitTlsData *jit_tls = mono_native_tls_get_value (mono_jit_tls_id);
746         mgreg_t sp;
747         void *sigctx = ctx;
748         int frame_size;
749         void *uc = sigctx;
750
751         /* Pass the ctx parameter in TLS */
752         mono_sigctx_to_monoctx (sigctx, &jit_tls->ex_ctx);
753         /* The others in registers */
754         UCONTEXT_REG_Rn (sigctx, PPC_FIRST_ARG_REG) = (gsize)obj;
755
756         /* Allocate a stack frame below the red zone */
757         /* Similar to mono_arch_handle_altstack_exception () */
758         frame_size = 224;
759         frame_size += 15;
760         frame_size &= ~15;
761         sp = (mgreg_t)(UCONTEXT_REG_Rn(uc, 1) & ~15);
762         sp = (mgreg_t)(sp - frame_size);
763         UCONTEXT_REG_Rn(uc, 1) = (mgreg_t)sp;
764         setup_ucontext_return (uc, handle_signal_exception);
765
766         return TRUE;
767 #else
768         MonoContext mctx;
769         gboolean result;
770
771         mono_sigctx_to_monoctx (ctx, &mctx);
772
773         result = mono_handle_exception (&mctx, obj);
774         /* restore the context so that returning from the signal handler will invoke
775          * the catch clause 
776          */
777         mono_monoctx_to_sigctx (&mctx, ctx);
778         return result;
779 #endif
780 }