Copy
[mono-project.git] / mono / mini / exceptions-ppc.c
blob36bc44c3c4305eb7f55daa43a7d61b88ec27c52a
1 /*
2 * exceptions-ppc.c: exception support for PowerPC
4 * Authors:
5 * Dietmar Maurer (dietmar@ximian.com)
6 * Paolo Molaro (lupus@ximian.com)
7 * Andreas Faerber <andreas.faerber@web.de>
9 * (C) 2001 Ximian, Inc.
10 * (C) 2007-2008 Andreas Faerber
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
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>
30 #include "mini.h"
31 #include "mini-ppc.h"
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
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
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
59 unsigned long pad1[4]; // space in PCB "just in case"
60 } ppc_exception_state_t;
62 typedef struct ppc_vector_state {
63 unsigned long save_vr[32][4];
64 unsigned long save_vscr[4];
65 unsigned int save_pad5[4];
66 unsigned int save_vrvalid; // VRs that have been saved
67 unsigned int save_pad6[7];
68 } ppc_vector_state_t;
70 typedef struct ppc_float_state {
71 double fpregs[32];
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;
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)
91 unsigned int vrsave; // Vector Save Register
92 } ppc_thread_state_t;
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;
101 typedef struct mcontext * mcontext_t;
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;
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
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)));
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;
149 #define ELF_NGREG 48 // includes nip, msr, lr, etc.
150 #define ELF_NFPREG 33 // includes fpscr
152 // General registers
153 typedef unsigned long elf_greg_t;
154 typedef elf_greg_t elf_gregset_t[ELF_NGREG];
156 // Floating point registers
157 typedef double elf_fpreg_t;
158 typedef elf_fpreg_t elf_fpregset_t[ELF_NFPREG];
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, 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); \
172 } while (0)
174 /* nothing to do */
175 #define setup_context(ctx)
177 #ifdef PPC_USES_FUNCTION_DESCRIPTOR
178 guint8*
179 mono_ppc_create_pre_code_ftnptr (guint8 *code)
181 MonoPPCFunctionDescriptor *ftnptr = (MonoPPCFunctionDescriptor*)code;
183 code += sizeof (MonoPPCFunctionDescriptor);
184 ftnptr->code = code;
185 ftnptr->toc = NULL;
186 ftnptr->env = NULL;
188 return code;
190 #endif
193 * arch_get_restore_context:
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.
198 gpointer
199 mono_arch_get_restore_context (void)
201 guint8 *start, *code;
202 int size = MONO_PPC_32_64_CASE (128, 172) + PPC_FTNPTR_SIZE;
204 code = start = mono_global_codeman_reserve (size);
205 code = mono_ppc_create_pre_code_ftnptr (code);
206 restore_regs_from_context (ppc_r3, ppc_r4, ppc_r5);
207 /* restore also the stack pointer */
208 ppc_load_reg (code, ppc_sp, G_STRUCT_OFFSET (MonoContext, sc_sp), ppc_r3);
209 //ppc_break (code);
210 /* jump to the saved IP */
211 ppc_mtctr (code, ppc_r4);
212 ppc_bcctr (code, PPC_BR_ALWAYS, 0);
213 /* never reached */
214 ppc_break (code);
216 g_assert ((code - start) <= size);
217 mono_arch_flush_icache (start, code - start);
218 return start;
221 #define SAVED_REGS_LENGTH (sizeof (gdouble) * MONO_SAVED_FREGS + sizeof (gpointer) * MONO_SAVED_GREGS)
222 #define ALIGN_STACK_FRAME_SIZE(s) (((s) + MONO_ARCH_FRAME_ALIGNMENT - 1) & ~(MONO_ARCH_FRAME_ALIGNMENT - 1))
223 /* The 64 bytes here are for outgoing arguments and a bit of spare.
224 We don't use it all, but it doesn't hurt. */
225 #define REG_SAVE_STACK_FRAME_SIZE (ALIGN_STACK_FRAME_SIZE (SAVED_REGS_LENGTH + PPC_MINIMAL_STACK_SIZE + 64))
227 static guint8*
228 emit_save_saved_regs (guint8 *code, int pos)
230 int i;
232 for (i = 31; i >= 14; --i) {
233 pos -= sizeof (gdouble);
234 ppc_stfd (code, i, pos, ppc_sp);
236 pos -= sizeof (gpointer) * MONO_SAVED_GREGS;
237 ppc_store_multiple_regs (code, ppc_r13, pos, ppc_sp);
239 return code;
243 * mono_arch_get_call_filter:
245 * Returns a pointer to a method which calls an exception filter. We
246 * also use this function to call finally handlers (we pass NULL as
247 * @exc object in this case).
249 gpointer
250 mono_arch_get_call_filter (void)
252 guint8 *start, *code;
253 int alloc_size, pos, i;
254 int size = MONO_PPC_32_64_CASE (320, 500) + PPC_FTNPTR_SIZE;
256 /* call_filter (MonoContext *ctx, unsigned long eip, gpointer exc) */
257 code = start = mono_global_codeman_reserve (size);
258 code = mono_ppc_create_pre_code_ftnptr (code);
260 /* store ret addr */
261 ppc_mflr (code, ppc_r0);
262 ppc_store_reg (code, ppc_r0, PPC_RET_ADDR_OFFSET, ppc_sp);
264 alloc_size = REG_SAVE_STACK_FRAME_SIZE;
266 /* allocate stack frame and set link from sp in ctx */
267 g_assert ((alloc_size & (MONO_ARCH_FRAME_ALIGNMENT-1)) == 0);
268 ppc_load_reg (code, ppc_r0, G_STRUCT_OFFSET (MonoContext, sc_sp), ppc_r3);
269 ppc_load_reg_indexed (code, ppc_r0, ppc_r0, ppc_r0);
270 ppc_store_reg_update (code, ppc_r0, -alloc_size, ppc_sp);
272 code = emit_save_saved_regs (code, alloc_size);
274 /* restore all the regs from ctx (in r3), but not r1, the stack pointer */
275 restore_regs_from_context (ppc_r3, ppc_r6, ppc_r7);
276 /* call handler at eip (r4) and set the first arg with the exception (r5) */
277 ppc_mtctr (code, ppc_r4);
278 ppc_mr (code, ppc_r3, ppc_r5);
279 ppc_bcctrl (code, PPC_BR_ALWAYS, 0);
281 /* epilog */
282 ppc_load_reg (code, ppc_r0, alloc_size + PPC_RET_ADDR_OFFSET, ppc_sp);
283 ppc_mtlr (code, ppc_r0);
285 /* restore all the regs from the stack */
286 pos = alloc_size;
287 for (i = 31; i >= 14; --i) {
288 pos -= sizeof (gdouble);
289 ppc_lfd (code, i, pos, ppc_sp);
291 pos -= sizeof (gpointer) * MONO_SAVED_GREGS;
292 ppc_load_multiple_regs (code, ppc_r13, pos, ppc_sp);
294 ppc_addic (code, ppc_sp, ppc_sp, alloc_size);
295 ppc_blr (code);
297 g_assert ((code - start) < size);
298 mono_arch_flush_icache (start, code - start);
299 return start;
302 static void
303 throw_exception (MonoObject *exc, unsigned long eip, unsigned long esp, gulong *int_regs, gdouble *fp_regs, gboolean rethrow)
305 static void (*restore_context) (MonoContext *);
306 MonoContext ctx;
308 if (!restore_context)
309 restore_context = mono_get_restore_context ();
311 /* adjust eip so that it point into the call instruction */
312 eip -= 4;
314 setup_context (&ctx);
316 /*printf ("stack in throw: %p\n", esp);*/
317 MONO_CONTEXT_SET_BP (&ctx, esp);
318 MONO_CONTEXT_SET_IP (&ctx, eip);
319 memcpy (&ctx.regs, int_regs, sizeof (gulong) * MONO_SAVED_GREGS);
320 memcpy (&ctx.fregs, fp_regs, sizeof (double) * MONO_SAVED_FREGS);
322 if (mono_object_isinst (exc, mono_defaults.exception_class)) {
323 MonoException *mono_ex = (MonoException*)exc;
324 if (!rethrow)
325 mono_ex->stack_trace = NULL;
327 mono_handle_exception (&ctx, exc, (gpointer)eip, FALSE);
328 restore_context (&ctx);
330 g_assert_not_reached ();
334 * arch_get_throw_exception_generic:
336 * Returns a function pointer which can be used to raise
337 * exceptions. The returned function has the following
338 * signature: void (*func) (MonoException *exc); or
339 * void (*func) (guint32 ex_token, gpointer ip)
342 static gpointer
343 mono_arch_get_throw_exception_generic (int size, int corlib, gboolean rethrow)
345 guint8 *start, *code;
346 int alloc_size, pos;
348 start = mono_global_codeman_reserve (size);
350 code = mono_ppc_create_pre_code_ftnptr (start);
352 /* store ret addr */
353 if (corlib)
354 ppc_mr (code, ppc_r0, ppc_r4);
355 else
356 ppc_mflr (code, ppc_r0);
357 ppc_store_reg (code, ppc_r0, PPC_RET_ADDR_OFFSET, ppc_sp);
359 alloc_size = REG_SAVE_STACK_FRAME_SIZE;
361 g_assert ((alloc_size & (MONO_ARCH_FRAME_ALIGNMENT-1)) == 0);
362 ppc_store_reg_update (code, ppc_sp, -alloc_size, ppc_sp);
364 code = emit_save_saved_regs (code, alloc_size);
366 //ppc_break (code);
367 if (corlib) {
368 ppc_mr (code, ppc_r4, ppc_r3);
369 ppc_load (code, ppc_r3, (gulong)mono_defaults.corlib);
370 ppc_load_func (code, ppc_r0, mono_exception_from_token);
371 ppc_mtctr (code, ppc_r0);
372 ppc_bcctrl (code, PPC_BR_ALWAYS, 0);
375 /* call throw_exception (exc, ip, sp, int_regs, fp_regs) */
376 /* caller sp */
377 ppc_load_reg (code, ppc_r5, 0, ppc_sp);
378 /* exc is already in place in r3 */
379 if (corlib)
380 ppc_load_reg (code, ppc_r4, PPC_RET_ADDR_OFFSET, ppc_r5);
381 else
382 ppc_mr (code, ppc_r4, ppc_r0); /* caller ip */
383 /* pointer to the saved fp regs */
384 pos = alloc_size - sizeof (gdouble) * MONO_SAVED_FREGS;
385 ppc_addi (code, ppc_r7, ppc_sp, pos);
386 /* pointer to the saved int regs */
387 pos -= sizeof (gpointer) * MONO_SAVED_GREGS;
388 ppc_addi (code, ppc_r6, ppc_sp, pos);
389 ppc_li (code, ppc_r8, rethrow);
391 ppc_load_func (code, ppc_r0, throw_exception);
392 ppc_mtctr (code, ppc_r0);
393 ppc_bcctrl (code, PPC_BR_ALWAYS, 0);
394 /* we should never reach this breakpoint */
395 ppc_break (code);
396 g_assert ((code - start) <= size);
397 mono_arch_flush_icache (start, code - start);
398 return start;
402 * mono_arch_get_rethrow_exception:
404 * Returns a function pointer which can be used to rethrow
405 * exceptions. The returned function has the following
406 * signature: void (*func) (MonoException *exc);
409 gpointer
410 mono_arch_get_rethrow_exception (void)
412 int size = MONO_PPC_32_64_CASE (132, 224) + PPC_FTNPTR_SIZE;
414 return mono_arch_get_throw_exception_generic (size, FALSE, TRUE);
417 * arch_get_throw_exception:
419 * Returns a function pointer which can be used to raise
420 * exceptions. The returned function has the following
421 * signature: void (*func) (MonoException *exc);
422 * For example to raise an arithmetic exception you can use:
424 * x86_push_imm (code, mono_get_exception_arithmetic ());
425 * x86_call_code (code, arch_get_throw_exception ());
428 gpointer
429 mono_arch_get_throw_exception (void)
431 int size = MONO_PPC_32_64_CASE (132, 224) + PPC_FTNPTR_SIZE;
433 return mono_arch_get_throw_exception_generic (size, FALSE, FALSE);
437 * arch_get_throw_exception_by_name:
439 * Returns a function pointer which can be used to raise
440 * corlib exceptions. The returned function has the following
441 * signature: void (*func) (char *exc_name);
442 * For example to raise an arithmetic exception you can use:
444 * x86_push_imm (code, "ArithmeticException");
445 * x86_call_code (code, arch_get_throw_exception_by_name ());
448 gpointer
449 mono_arch_get_throw_exception_by_name (void)
451 guint8 *start, *code;
452 int size = 64;
454 /* Not used on PPC */
455 start = code = mono_global_codeman_reserve (size);
456 ppc_break (code);
457 mono_arch_flush_icache (start, code - start);
458 return start;
462 * mono_arch_get_throw_corlib_exception:
464 * Returns a function pointer which can be used to raise
465 * corlib exceptions. The returned function has the following
466 * signature: void (*func) (guint32 ex_token, guint32 offset);
467 * On PPC, we pass the ip instead of the offset
469 gpointer
470 mono_arch_get_throw_corlib_exception (void)
472 int size = MONO_PPC_32_64_CASE (168, 304) + PPC_FTNPTR_SIZE;
474 return mono_arch_get_throw_exception_generic (size, TRUE, FALSE);
477 /* mono_arch_find_jit_info:
479 * This function is used to gather information from @ctx. It return the
480 * MonoJitInfo of the corresponding function, unwinds one stack frame and
481 * stores the resulting context into @new_ctx. It also stores a string
482 * describing the stack location into @trace (if not NULL), and modifies
483 * the @lmf if necessary. @native_offset return the IP offset from the
484 * start of the function or -1 if that info is not available.
486 MonoJitInfo *
487 mono_arch_find_jit_info (MonoDomain *domain, MonoJitTlsData *jit_tls, MonoJitInfo *res, MonoJitInfo *prev_ji, MonoContext *ctx,
488 MonoContext *new_ctx, MonoLMF **lmf, gboolean *managed)
490 MonoJitInfo *ji;
491 gpointer ip = MONO_CONTEXT_GET_IP (ctx);
492 MonoPPCStackFrame *sframe;
494 /* Avoid costly table lookup during stack overflow */
495 if (prev_ji && (ip > prev_ji->code_start && ((guint8*)ip < ((guint8*)prev_ji->code_start) + prev_ji->code_size)))
496 ji = prev_ji;
497 else
498 ji = mono_jit_info_table_find (domain, ip);
500 if (managed)
501 *managed = FALSE;
503 if (ji != NULL) {
504 gint32 address;
505 int offset, i;
507 *new_ctx = *ctx;
508 setup_context (new_ctx);
510 if (*lmf && (MONO_CONTEXT_GET_SP (ctx) >= (gpointer)(*lmf)->ebp)) {
511 /* remove any unused lmf */
512 *lmf = (*lmf)->previous_lmf;
515 address = (char *)ip - (char *)ji->code_start;
517 if (managed)
518 if (!ji->method->wrapper_type)
519 *managed = TRUE;
521 sframe = (MonoPPCStackFrame*)MONO_CONTEXT_GET_SP (ctx);
522 MONO_CONTEXT_SET_BP (new_ctx, sframe->sp);
523 if (ji->method->save_lmf) {
524 memcpy (&new_ctx->fregs, (char*)sframe->sp - sizeof (double) * MONO_SAVED_FREGS, sizeof (double) * MONO_SAVED_FREGS);
525 memcpy (&new_ctx->regs, (char*)sframe->sp - sizeof (double) * MONO_SAVED_FREGS - sizeof (gulong) * MONO_SAVED_GREGS, sizeof (gulong) * MONO_SAVED_GREGS);
526 } else if (ji->used_regs) {
527 /* keep updated with emit_prolog in mini-ppc.c */
528 offset = 0;
529 /* FIXME handle floating point args
530 for (i = 31; i >= 14; --i) {
531 if (ji->used_fregs & (1 << i)) {
532 offset += sizeof (double);
533 new_ctx->fregs [i - 14] = *(gulong*)((char*)sframe->sp - offset);
536 for (i = 31; i >= 13; --i) {
537 if (ji->used_regs & (1 << i)) {
538 offset += sizeof (gulong);
539 new_ctx->regs [i - 13] = *(gulong*)((char*)sframe->sp - offset);
543 /* the calling IP is in the parent frame */
544 sframe = (MonoPPCStackFrame*)sframe->sp;
545 /* we substract 4, so that the IP points into the call instruction */
546 MONO_CONTEXT_SET_IP (new_ctx, sframe->lr - 4);
548 return ji;
549 } else if (*lmf) {
551 *new_ctx = *ctx;
552 setup_context (new_ctx);
554 if ((ji = mono_jit_info_table_find (domain, (gpointer)(*lmf)->eip))) {
555 } else {
556 if (!(*lmf)->method)
557 return (gpointer)-1;
559 memset (res, 0, sizeof (MonoJitInfo));
560 res->method = (*lmf)->method;
563 /*sframe = (MonoPPCStackFrame*)MONO_CONTEXT_GET_SP (ctx);
564 MONO_CONTEXT_SET_BP (new_ctx, sframe->sp);
565 MONO_CONTEXT_SET_IP (new_ctx, sframe->lr);*/
566 MONO_CONTEXT_SET_BP (new_ctx, (*lmf)->ebp);
567 MONO_CONTEXT_SET_IP (new_ctx, (*lmf)->eip);
568 memcpy (&new_ctx->regs, (*lmf)->iregs, sizeof (gulong) * MONO_SAVED_GREGS);
569 memcpy (&new_ctx->fregs, (*lmf)->fregs, sizeof (double) * MONO_SAVED_FREGS);
571 /* FIXME: what about trampoline LMF frames? see exceptions-x86.c */
573 *lmf = (*lmf)->previous_lmf;
575 return ji ? ji : res;
578 return NULL;
582 * This is the function called from the signal handler
584 void
585 mono_arch_sigctx_to_monoctx (void *ctx, MonoContext *mctx)
587 os_ucontext *uc = ctx;
589 mctx->sc_ir = UCONTEXT_REG_NIP(uc);
590 mctx->sc_sp = UCONTEXT_REG_Rn(uc, 1);
591 memcpy (&mctx->regs, &UCONTEXT_REG_Rn(uc, 13), sizeof (gulong) * MONO_SAVED_GREGS);
592 memcpy (&mctx->fregs, &UCONTEXT_REG_FPRn(uc, 14), sizeof (double) * MONO_SAVED_FREGS);
595 void
596 mono_arch_monoctx_to_sigctx (MonoContext *mctx, void *ctx)
598 os_ucontext *uc = ctx;
600 UCONTEXT_REG_NIP(uc) = mctx->sc_ir;
601 UCONTEXT_REG_Rn(uc, 1) = mctx->sc_sp;
602 memcpy (&UCONTEXT_REG_Rn(uc, 13), &mctx->regs, sizeof (gulong) * MONO_SAVED_GREGS);
603 memcpy (&UCONTEXT_REG_FPRn(uc, 14), &mctx->fregs, sizeof (double) * MONO_SAVED_FREGS);
606 gpointer
607 mono_arch_ip_from_context (void *sigctx)
609 os_ucontext *uc = sigctx;
610 return (gpointer)UCONTEXT_REG_NIP(uc);
613 static void
614 altstack_handle_and_restore (void *sigctx, gpointer obj, gboolean test_only)
616 void (*restore_context) (MonoContext *);
617 MonoContext mctx;
619 restore_context = mono_get_restore_context ();
620 mono_arch_sigctx_to_monoctx (sigctx, &mctx);
621 mono_handle_exception (&mctx, obj, (gpointer)mctx.sc_ir, test_only);
622 restore_context (&mctx);
625 void
626 mono_arch_handle_altstack_exception (void *sigctx, gpointer fault_addr, gboolean stack_ovf)
628 #ifdef MONO_ARCH_USE_SIGACTION
629 os_ucontext *uc = (ucontext_t*)sigctx;
630 os_ucontext *uc_copy;
631 MonoJitInfo *ji = mono_jit_info_table_find (mono_domain_get (), mono_arch_ip_from_context (sigctx));
632 gpointer *sp;
633 int frame_size;
635 if (stack_ovf) {
636 const char *method;
637 /* we don't do much now, but we can warn the user with a useful message */
638 fprintf (stderr, "Stack overflow: IP: %p, SP: %p\n", mono_arch_ip_from_context (sigctx), (gpointer)UCONTEXT_REG_Rn(uc, 1));
639 if (ji && ji->method)
640 method = mono_method_full_name (ji->method, TRUE);
641 else
642 method = "Unmanaged";
643 fprintf (stderr, "At %s\n", method);
644 abort ();
646 if (!ji)
647 mono_handle_native_sigsegv (SIGSEGV, sigctx);
648 /* setup a call frame on the real stack so that control is returned there
649 * and exception handling can continue.
650 * The frame looks like:
651 * ucontext struct
652 * ...
653 * 224 is the size of the red zone
655 frame_size = sizeof (ucontext_t) + sizeof (gpointer) * 16 + 224;
656 frame_size += 15;
657 frame_size &= ~15;
658 sp = (gpointer)(UCONTEXT_REG_Rn(uc, 1) & ~15);
659 sp = (gpointer)((char*)sp - frame_size);
660 /* may need to adjust pointers in the new struct copy, depending on the OS */
661 uc_copy = (ucontext_t*)(sp + 16);
662 memcpy (uc_copy, uc, sizeof (os_ucontext));
663 #if defined(__linux__) && !defined(__mono_ppc64__)
664 uc_copy->uc_mcontext.uc_regs = (gpointer)((char*)uc_copy + ((char*)uc->uc_mcontext.uc_regs - (char*)uc));
665 #endif
666 g_assert (mono_arch_ip_from_context (uc) == mono_arch_ip_from_context (uc_copy));
667 /* at the return form the signal handler execution starts in altstack_handle_and_restore() */
668 UCONTEXT_REG_LNK(uc) = UCONTEXT_REG_NIP(uc);
669 #ifdef PPC_USES_FUNCTION_DESCRIPTOR
671 MonoPPCFunctionDescriptor *handler_ftnptr = (MonoPPCFunctionDescriptor*)altstack_handle_and_restore;
673 UCONTEXT_REG_NIP(uc) = (gulong)handler_ftnptr->code;
674 UCONTEXT_REG_Rn(uc, 2) = (gulong)handler_ftnptr->toc;
676 #else
677 UCONTEXT_REG_NIP(uc) = (unsigned long)altstack_handle_and_restore;
678 #endif
679 UCONTEXT_REG_Rn(uc, 1) = (unsigned long)sp;
680 UCONTEXT_REG_Rn(uc, PPC_FIRST_ARG_REG) = (unsigned long)(sp + 16);
681 UCONTEXT_REG_Rn(uc, PPC_FIRST_ARG_REG + 1) = 0;
682 UCONTEXT_REG_Rn(uc, PPC_FIRST_ARG_REG + 2) = 0;
683 #endif
686 gboolean
687 mono_arch_handle_exception (void *ctx, gpointer obj, gboolean test_only)
689 MonoContext mctx;
690 gboolean result;
692 mono_arch_sigctx_to_monoctx (ctx, &mctx);
694 result = mono_handle_exception (&mctx, obj, (gpointer)mctx.sc_ir, test_only);
695 /* restore the context so that returning from the signal handler will invoke
696 * the catch clause
698 mono_arch_monoctx_to_sigctx (&mctx, ctx);
699 return result;
702 gboolean
703 mono_arch_has_unwind_info (gconstpointer addr)
705 return FALSE;