2 * exceptions-ppc.c: exception support for PowerPC
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
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>
36 int sc_onstack; // sigstack state to restore
37 int sc_mask; // signal mask to restore
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
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
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];
70 typedef struct ppc_float_state {
73 unsigned int fpscr_pad; // fpscr is 64 bits, 32 bits of rubbish
74 unsigned int fpscr; // floating point status register
77 typedef struct ppc_thread_state {
78 unsigned int srr0; // Instruction address register (PC)
79 unsigned int srr1; // Machine state register (supervisor)
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
95 ppc_exception_state_t es;
96 ppc_thread_state_t ss;
98 ppc_vector_state_t vs;
101 typedef struct mcontext * mcontext_t;
103 Linux/PPC instead has:
105 unsigned long _unused[4];
107 unsigned long handler;
108 unsigned long oldmask;
109 struct pt_regs *regs;
112 unsigned long gpr[32];
115 unsigned long orig_gpr3; // Used for restarting system calls
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
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)));
137 unsigned long uc_flags;
138 struct ucontext *uc_link;
141 struct mcontext *uc_regs; // points to uc_mcontext field
143 // glibc has 1024-bit signal masks, ours are 64-bit
146 struct mcontext uc_mcontext;
149 #define ELF_NGREG 48 // includes nip, msr, lr, etc.
150 #define ELF_NFPREG 33 // includes fpscr
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 { \
166 ppc_ldptr (code, ip_reg, G_STRUCT_OFFSET (MonoContext, sc_ir), ctx_reg); \
167 ppc_load_multiple_regs (code, MONO_PPC_FIRST_SAVED_GREG, \
168 G_STRUCT_OFFSET (MonoContext, regs) + MONO_PPC_FIRST_SAVED_GREG * sizeof (gpointer), ctx_reg); \
169 for (reg = MONO_PPC_FIRST_SAVED_FREG; reg < MONO_MAX_FREGS; ++reg) { \
170 ppc_lfd (code, reg, \
171 G_STRUCT_OFFSET(MonoContext, fregs) + reg * sizeof (gdouble), ctx_reg); \
176 #define setup_context(ctx)
178 #ifdef PPC_USES_FUNCTION_DESCRIPTOR
180 mono_ppc_create_pre_code_ftnptr (guint8 *code)
182 MonoPPCFunctionDescriptor *ftnptr = (MonoPPCFunctionDescriptor*)code;
184 code += sizeof (MonoPPCFunctionDescriptor);
194 * arch_get_restore_context:
196 * Returns a pointer to a method which restores a previously saved sigcontext.
197 * The first argument in r3 is the pointer to the context.
200 mono_arch_get_restore_context (MonoTrampInfo **info, gboolean aot)
202 guint8 *start, *code;
203 int size = MONO_PPC_32_64_CASE (128, 172) + PPC_FTNPTR_SIZE;
204 MonoJumpInfo *ji = NULL;
205 GSList *unwind_ops = NULL;
207 code = start = mono_global_codeman_reserve (size);
209 code = mono_ppc_create_pre_code_ftnptr (code);
210 restore_regs_from_context (ppc_r3, ppc_r4, ppc_r5);
211 /* restore also the stack pointer */
212 ppc_ldptr (code, ppc_sp, G_STRUCT_OFFSET (MonoContext, sc_sp), ppc_r3);
214 /* jump to the saved IP */
215 ppc_mtctr (code, ppc_r4);
216 ppc_bcctr (code, PPC_BR_ALWAYS, 0);
220 g_assert ((code - start) <= size);
221 mono_arch_flush_icache (start, code - start);
222 mono_profiler_code_buffer_new (start, code - start, MONO_PROFILER_CODE_BUFFER_EXCEPTION_HANDLING, NULL);
225 *info = mono_tramp_info_create ("restore_context", start, code - start, ji, unwind_ops);
230 #define SAVED_REGS_LENGTH (sizeof (gdouble) * MONO_MAX_FREGS + sizeof (gpointer) * MONO_MAX_IREGS)
231 #define ALIGN_STACK_FRAME_SIZE(s) (((s) + MONO_ARCH_FRAME_ALIGNMENT - 1) & ~(MONO_ARCH_FRAME_ALIGNMENT - 1))
232 /* The 64 bytes here are for outgoing arguments and a bit of spare.
233 We don't use it all, but it doesn't hurt. */
234 #define REG_SAVE_STACK_FRAME_SIZE (ALIGN_STACK_FRAME_SIZE (SAVED_REGS_LENGTH + PPC_MINIMAL_STACK_SIZE + 64))
237 emit_save_saved_regs (guint8 *code, int pos)
241 for (i = MONO_MAX_FREGS - 1; i >= MONO_PPC_FIRST_SAVED_FREG; --i) {
242 pos -= sizeof (gdouble);
243 ppc_stfd (code, i, pos, ppc_sp);
245 pos -= (MONO_MAX_FREGS - MONO_SAVED_FREGS) * sizeof (gdouble);
246 pos -= sizeof (gpointer) * MONO_SAVED_GREGS;
247 ppc_store_multiple_regs (code, MONO_PPC_FIRST_SAVED_GREG, pos, ppc_sp);
253 * mono_arch_get_call_filter:
255 * Returns a pointer to a method which calls an exception filter. We
256 * also use this function to call finally handlers (we pass NULL as
257 * @exc object in this case).
260 mono_arch_get_call_filter (MonoTrampInfo **info, gboolean aot)
262 guint8 *start, *code;
263 int alloc_size, pos, i;
264 int size = MONO_PPC_32_64_CASE (320, 500) + PPC_FTNPTR_SIZE;
265 MonoJumpInfo *ji = NULL;
266 GSList *unwind_ops = NULL;
268 /* call_filter (MonoContext *ctx, unsigned long eip, gpointer exc) */
269 code = start = mono_global_codeman_reserve (size);
271 code = mono_ppc_create_pre_code_ftnptr (code);
274 ppc_mflr (code, ppc_r0);
275 ppc_stptr (code, ppc_r0, PPC_RET_ADDR_OFFSET, ppc_sp);
277 alloc_size = REG_SAVE_STACK_FRAME_SIZE;
279 /* allocate stack frame and set link from sp in ctx */
280 g_assert ((alloc_size & (MONO_ARCH_FRAME_ALIGNMENT-1)) == 0);
281 ppc_ldptr (code, ppc_r0, G_STRUCT_OFFSET (MonoContext, sc_sp), ppc_r3);
282 ppc_ldptr_indexed (code, ppc_r0, ppc_r0, ppc_r0);
283 ppc_stptr_update (code, ppc_r0, -alloc_size, ppc_sp);
285 code = emit_save_saved_regs (code, alloc_size);
287 /* restore all the regs from ctx (in r3), but not r1, the stack pointer */
288 restore_regs_from_context (ppc_r3, ppc_r6, ppc_r7);
289 /* call handler at eip (r4) and set the first arg with the exception (r5) */
290 ppc_mtctr (code, ppc_r4);
291 ppc_mr (code, ppc_r3, ppc_r5);
292 ppc_bcctrl (code, PPC_BR_ALWAYS, 0);
295 ppc_ldptr (code, ppc_r0, alloc_size + PPC_RET_ADDR_OFFSET, ppc_sp);
296 ppc_mtlr (code, ppc_r0);
298 /* restore all the regs from the stack */
300 for (i = MONO_MAX_FREGS - 1; i >= MONO_PPC_FIRST_SAVED_FREG; --i) {
301 pos -= sizeof (gdouble);
302 ppc_lfd (code, i, pos, ppc_sp);
304 pos -= (MONO_MAX_FREGS - MONO_SAVED_FREGS) * sizeof (gdouble);
305 pos -= sizeof (gpointer) * MONO_SAVED_GREGS;
306 ppc_load_multiple_regs (code, MONO_PPC_FIRST_SAVED_GREG, pos, ppc_sp);
308 ppc_addic (code, ppc_sp, ppc_sp, alloc_size);
311 g_assert ((code - start) < size);
312 mono_arch_flush_icache (start, code - start);
313 mono_profiler_code_buffer_new (start, code - start, MONO_PROFILER_CODE_BUFFER_EXCEPTION_HANDLING, NULL);
316 *info = mono_tramp_info_create ("call_filter", start, code - start, ji, unwind_ops);
322 mono_ppc_throw_exception (MonoObject *exc, unsigned long eip, unsigned long esp, mgreg_t *int_regs, gdouble *fp_regs, gboolean rethrow)
327 /* adjust eip so that it point into the call instruction */
330 setup_context (&ctx);
332 /*printf ("stack in throw: %p\n", esp);*/
333 MONO_CONTEXT_SET_BP (&ctx, esp);
334 MONO_CONTEXT_SET_IP (&ctx, eip);
335 memcpy (&ctx.regs, int_regs, sizeof (mgreg_t) * MONO_MAX_IREGS);
336 memcpy (&ctx.fregs, fp_regs, sizeof (double) * MONO_MAX_FREGS);
338 if (mono_object_isinst_checked (exc, mono_defaults.exception_class, &error)) {
339 MonoException *mono_ex = (MonoException*)exc;
341 mono_ex->stack_trace = NULL;
342 mono_ex->trace_ips = NULL;
345 mono_error_assert_ok (&error);
346 mono_handle_exception (&ctx, exc);
347 mono_restore_context (&ctx);
349 g_assert_not_reached ();
353 * arch_get_throw_exception_generic:
355 * Returns a function pointer which can be used to raise
356 * exceptions. The returned function has the following
357 * signature: void (*func) (MonoException *exc); or
358 * void (*func) (guint32 ex_token, gpointer ip)
362 mono_arch_get_throw_exception_generic (int size, MonoTrampInfo **info, int corlib, gboolean rethrow, gboolean aot)
364 guint8 *start, *code;
366 MonoJumpInfo *ji = NULL;
367 GSList *unwind_ops = NULL;
369 code = start = mono_global_codeman_reserve (size);
371 code = mono_ppc_create_pre_code_ftnptr (code);
375 ppc_mr (code, ppc_r0, ppc_r4);
377 ppc_mflr (code, ppc_r0);
378 ppc_stptr (code, ppc_r0, PPC_RET_ADDR_OFFSET, ppc_sp);
380 alloc_size = REG_SAVE_STACK_FRAME_SIZE;
382 g_assert ((alloc_size & (MONO_ARCH_FRAME_ALIGNMENT-1)) == 0);
383 ppc_stptr_update (code, ppc_sp, -alloc_size, ppc_sp);
385 code = emit_save_saved_regs (code, alloc_size);
389 ppc_mr (code, ppc_r4, ppc_r3);
392 code = mono_arch_emit_load_aotconst (start, code, &ji, MONO_PATCH_INFO_IMAGE, mono_defaults.corlib);
393 ppc_mr (code, ppc_r3, ppc_r12);
394 code = mono_arch_emit_load_aotconst (start, code, &ji, MONO_PATCH_INFO_JIT_ICALL_ADDR, "mono_exception_from_token");
395 #ifdef PPC_USES_FUNCTION_DESCRIPTOR
396 ppc_ldptr (code, ppc_r2, sizeof (gpointer), ppc_r12);
397 ppc_ldptr (code, ppc_r12, 0, ppc_r12);
399 ppc_mtctr (code, ppc_r12);
400 ppc_bcctrl (code, PPC_BR_ALWAYS, 0);
402 ppc_load (code, ppc_r3, (gulong)mono_defaults.corlib);
403 ppc_load_func (code, PPC_CALL_REG, mono_exception_from_token);
404 ppc_mtctr (code, PPC_CALL_REG);
405 ppc_bcctrl (code, PPC_BR_ALWAYS, 0);
409 /* call throw_exception (exc, ip, sp, int_regs, fp_regs) */
411 ppc_ldptr (code, ppc_r5, 0, ppc_sp);
412 /* exc is already in place in r3 */
414 ppc_ldptr (code, ppc_r4, PPC_RET_ADDR_OFFSET, ppc_r5);
416 ppc_mr (code, ppc_r4, ppc_r0); /* caller ip */
417 /* pointer to the saved fp regs */
418 pos = alloc_size - sizeof (gdouble) * MONO_MAX_FREGS;
419 ppc_addi (code, ppc_r7, ppc_sp, pos);
420 /* pointer to the saved int regs */
421 pos -= sizeof (gpointer) * MONO_MAX_IREGS;
422 ppc_addi (code, ppc_r6, ppc_sp, pos);
423 ppc_li (code, ppc_r8, rethrow);
426 // This can be called from runtime code, which can't guarantee that
427 // r30 contains the got address.
428 // So emit the got address loading code too
429 code = mono_arch_emit_load_got_addr (start, code, NULL, &ji);
430 code = mono_arch_emit_load_aotconst (start, code, &ji, MONO_PATCH_INFO_JIT_ICALL_ADDR, "mono_ppc_throw_exception");
431 #ifdef PPC_USES_FUNCTION_DESCRIPTOR
432 ppc_ldptr (code, ppc_r2, sizeof (gpointer), ppc_r12);
433 ppc_ldptr (code, ppc_r12, 0, ppc_r12);
435 ppc_mtctr (code, ppc_r12);
436 ppc_bcctrl (code, PPC_BR_ALWAYS, 0);
438 ppc_load_func (code, PPC_CALL_REG, mono_ppc_throw_exception);
439 ppc_mtctr (code, PPC_CALL_REG);
440 ppc_bcctrl (code, PPC_BR_ALWAYS, 0);
442 /* we should never reach this breakpoint */
444 g_assert ((code - start) <= size);
445 mono_arch_flush_icache (start, code - start);
446 mono_profiler_code_buffer_new (start, code - start, MONO_PROFILER_CODE_BUFFER_EXCEPTION_HANDLING, NULL);
449 *info = mono_tramp_info_create (corlib ? "throw_corlib_exception" : (rethrow ? "rethrow_exception" : "throw_exception"), start, code - start, ji, unwind_ops);
455 * mono_arch_get_rethrow_exception:
457 * Returns a function pointer which can be used to rethrow
458 * exceptions. The returned function has the following
459 * signature: void (*func) (MonoException *exc);
463 mono_arch_get_rethrow_exception (MonoTrampInfo **info, gboolean aot)
465 int size = MONO_PPC_32_64_CASE (132, 224) + PPC_FTNPTR_SIZE;
469 return mono_arch_get_throw_exception_generic (size, info, FALSE, TRUE, aot);
473 * arch_get_throw_exception:
475 * Returns a function pointer which can be used to raise
476 * exceptions. The returned function has the following
477 * signature: void (*func) (MonoException *exc);
478 * For example to raise an arithmetic exception you can use:
480 * x86_push_imm (code, mono_get_exception_arithmetic ());
481 * x86_call_code (code, arch_get_throw_exception ());
485 mono_arch_get_throw_exception (MonoTrampInfo **info, gboolean aot)
487 int size = MONO_PPC_32_64_CASE (132, 224) + PPC_FTNPTR_SIZE;
491 return mono_arch_get_throw_exception_generic (size, info, FALSE, FALSE, aot);
495 * mono_arch_get_throw_corlib_exception:
497 * Returns a function pointer which can be used to raise
498 * corlib exceptions. The returned function has the following
499 * signature: void (*func) (guint32 ex_token, guint32 offset);
500 * On PPC, we pass the ip instead of the offset
503 mono_arch_get_throw_corlib_exception (MonoTrampInfo **info, gboolean aot)
505 int size = MONO_PPC_32_64_CASE (168, 304) + PPC_FTNPTR_SIZE;
509 return mono_arch_get_throw_exception_generic (size, info, TRUE, FALSE, aot);
513 * mono_arch_unwind_frame:
515 * See exceptions-amd64.c for docs.
518 mono_arch_unwind_frame (MonoDomain *domain, MonoJitTlsData *jit_tls,
519 MonoJitInfo *ji, MonoContext *ctx,
520 MonoContext *new_ctx, MonoLMF **lmf,
521 mgreg_t **save_locations,
522 StackFrameInfo *frame)
524 gpointer ip = MONO_CONTEXT_GET_IP (ctx);
525 MonoPPCStackFrame *sframe;
527 memset (frame, 0, sizeof (StackFrameInfo));
531 setup_context (new_ctx);
535 mgreg_t regs [ppc_lr + 1];
537 guint32 unwind_info_len;
540 if (ji->is_trampoline)
541 frame->type = FRAME_TYPE_TRAMPOLINE;
543 frame->type = FRAME_TYPE_MANAGED;
545 unwind_info = mono_jinfo_get_unwind_info (ji, &unwind_info_len);
547 sframe = (MonoPPCStackFrame*)MONO_CONTEXT_GET_SP (ctx);
548 MONO_CONTEXT_SET_BP (new_ctx, sframe->sp);
549 if (!ji->is_trampoline && jinfo_get_method (ji)->save_lmf) {
550 /* sframe->sp points just past the end of the LMF */
551 guint8 *lmf_addr = (guint8*)sframe->sp - sizeof (MonoLMF);
552 memcpy (&new_ctx->fregs [MONO_PPC_FIRST_SAVED_FREG], lmf_addr + G_STRUCT_OFFSET (MonoLMF, fregs), sizeof (double) * MONO_SAVED_FREGS);
553 memcpy (&new_ctx->regs [MONO_PPC_FIRST_SAVED_GREG], lmf_addr + G_STRUCT_OFFSET (MonoLMF, iregs), sizeof (mgreg_t) * MONO_SAVED_GREGS);
554 /* the calling IP is in the parent frame */
555 sframe = (MonoPPCStackFrame*)sframe->sp;
556 /* we substract 4, so that the IP points into the call instruction */
557 MONO_CONTEXT_SET_IP (new_ctx, sframe->lr - 4);
559 regs [ppc_lr] = ctx->sc_ir;
560 regs [ppc_sp] = ctx->sc_sp;
561 for (i = MONO_PPC_FIRST_SAVED_GREG; i < MONO_MAX_IREGS; ++i)
562 regs [i] = ctx->regs [i];
564 mono_unwind_frame (unwind_info, unwind_info_len, ji->code_start,
565 (guint8*)ji->code_start + ji->code_size,
566 ip, NULL, regs, ppc_lr + 1,
567 save_locations, MONO_MAX_IREGS, &cfa);
569 /* we substract 4, so that the IP points into the call instruction */
570 MONO_CONTEXT_SET_IP (new_ctx, regs [ppc_lr] - 4);
571 MONO_CONTEXT_SET_BP (new_ctx, cfa);
573 for (i = MONO_PPC_FIRST_SAVED_GREG; i < MONO_MAX_IREGS; ++i)
574 new_ctx->regs [i] = regs [i];
580 if ((ji = mini_jit_info_table_find (domain, (gpointer)(*lmf)->eip, NULL))) {
585 /* Trampoline lmf frame */
586 frame->method = (*lmf)->method;
589 /*sframe = (MonoPPCStackFrame*)MONO_CONTEXT_GET_SP (ctx);
590 MONO_CONTEXT_SET_BP (new_ctx, sframe->sp);
591 MONO_CONTEXT_SET_IP (new_ctx, sframe->lr);*/
592 MONO_CONTEXT_SET_BP (new_ctx, (*lmf)->ebp);
593 MONO_CONTEXT_SET_IP (new_ctx, (*lmf)->eip);
594 memcpy (&new_ctx->regs [MONO_PPC_FIRST_SAVED_GREG], (*lmf)->iregs, sizeof (mgreg_t) * MONO_SAVED_GREGS);
595 memcpy (&new_ctx->fregs [MONO_PPC_FIRST_SAVED_FREG], (*lmf)->fregs, sizeof (double) * MONO_SAVED_FREGS);
598 frame->type = FRAME_TYPE_MANAGED_TO_NATIVE;
600 /* FIXME: what about trampoline LMF frames? see exceptions-x86.c */
602 *lmf = (*lmf)->previous_lmf;
611 mono_arch_ip_from_context (void *sigctx)
613 #ifdef MONO_CROSS_COMPILE
614 g_assert_not_reached ();
616 os_ucontext *uc = sigctx;
617 return (gpointer)UCONTEXT_REG_NIP(uc);
622 mono_ppc_set_func_into_sigctx (void *sigctx, void *func)
624 #ifdef MONO_CROSS_COMPILE
625 g_assert_not_reached ();
626 #elif defined(PPC_USES_FUNCTION_DESCRIPTOR)
627 /* Have to set both the ip and the TOC reg */
628 os_ucontext *uc = sigctx;
630 UCONTEXT_REG_NIP(uc) = ((gsize*)func) [0];
631 UCONTEXT_REG_Rn (uc, 2) = ((gsize*)func)[1];
633 g_assert_not_reached ();
638 altstack_handle_and_restore (void *sigctx, gpointer obj)
642 mono_sigctx_to_monoctx (sigctx, &mctx);
643 mono_handle_exception (&mctx, obj);
644 mono_restore_context (&mctx);
648 mono_arch_handle_altstack_exception (void *sigctx, MONO_SIG_HANDLER_INFO_TYPE *siginfo, gpointer fault_addr, gboolean stack_ovf)
650 #ifdef MONO_CROSS_COMPILE
651 g_assert_not_reached ();
653 #ifdef MONO_ARCH_USE_SIGACTION
654 os_ucontext *uc = (ucontext_t*)sigctx;
655 os_ucontext *uc_copy;
656 MonoJitInfo *ji = mini_jit_info_table_find (mono_domain_get (), mono_arch_ip_from_context (sigctx), NULL);
662 /* we don't do much now, but we can warn the user with a useful message */
663 fprintf (stderr, "Stack overflow: IP: %p, SP: %p\n", mono_arch_ip_from_context (sigctx), (gpointer)UCONTEXT_REG_Rn(uc, 1));
664 if (ji && !ji->is_trampoline && jinfo_get_method (ji))
665 method = mono_method_full_name (jinfo_get_method (ji), TRUE);
667 method = "Unmanaged";
668 fprintf (stderr, "At %s\n", method);
672 mono_handle_native_crash ("SIGSEGV", sigctx, siginfo);
673 /* setup a call frame on the real stack so that control is returned there
674 * and exception handling can continue.
675 * The frame looks like:
678 * 224 is the size of the red zone
680 frame_size = sizeof (ucontext_t) + sizeof (gpointer) * 16 + 224;
683 sp = (gpointer)(UCONTEXT_REG_Rn(uc, 1) & ~15);
684 sp = (gpointer)((char*)sp - frame_size);
685 /* may need to adjust pointers in the new struct copy, depending on the OS */
686 uc_copy = (ucontext_t*)(sp + 16);
687 memcpy (uc_copy, uc, sizeof (os_ucontext));
688 #if defined(__linux__) && !defined(__mono_ppc64__)
689 uc_copy->uc_mcontext.uc_regs = (gpointer)((char*)uc_copy + ((char*)uc->uc_mcontext.uc_regs - (char*)uc));
691 g_assert (mono_arch_ip_from_context (uc) == mono_arch_ip_from_context (uc_copy));
692 /* at the return form the signal handler execution starts in altstack_handle_and_restore() */
693 UCONTEXT_REG_LNK(uc) = UCONTEXT_REG_NIP(uc);
694 #ifdef PPC_USES_FUNCTION_DESCRIPTOR
696 MonoPPCFunctionDescriptor *handler_ftnptr = (MonoPPCFunctionDescriptor*)altstack_handle_and_restore;
698 UCONTEXT_REG_NIP(uc) = (gulong)handler_ftnptr->code;
699 UCONTEXT_REG_Rn(uc, 2) = (gulong)handler_ftnptr->toc;
702 UCONTEXT_REG_NIP(uc) = (unsigned long)altstack_handle_and_restore;
704 /* ELF v2 ABI calling convention requires to put the target address into
705 * r12 if we use the global entry point of a function. */
706 UCONTEXT_REG_Rn(uc, 12) = (unsigned long) altstack_handle_and_restore;
709 UCONTEXT_REG_Rn(uc, 1) = (unsigned long)sp;
710 UCONTEXT_REG_Rn(uc, PPC_FIRST_ARG_REG) = (unsigned long)(sp + 16);
711 UCONTEXT_REG_Rn(uc, PPC_FIRST_ARG_REG + 1) = 0;
712 UCONTEXT_REG_Rn(uc, PPC_FIRST_ARG_REG + 2) = 0;
715 #endif /* !MONO_CROSS_COMPILE */
721 * Called by resuming from a signal handler.
724 handle_signal_exception (gpointer obj)
726 MonoJitTlsData *jit_tls = mono_tls_get_jit_tls ();
729 memcpy (&ctx, &jit_tls->ex_ctx, sizeof (MonoContext));
731 mono_handle_exception (&ctx, obj);
733 mono_restore_context (&ctx);
737 setup_ucontext_return (void *uc, gpointer func)
739 #if !defined(MONO_CROSS_COMPILE)
740 UCONTEXT_REG_LNK(uc) = UCONTEXT_REG_NIP(uc);
741 #ifdef PPC_USES_FUNCTION_DESCRIPTOR
743 MonoPPCFunctionDescriptor *handler_ftnptr = (MonoPPCFunctionDescriptor*)func;
745 UCONTEXT_REG_NIP(uc) = (gulong)handler_ftnptr->code;
746 UCONTEXT_REG_Rn(uc, 2) = (gulong)handler_ftnptr->toc;
749 UCONTEXT_REG_NIP(uc) = (unsigned long)func;
751 /* ELF v2 ABI calling convention requires to put the target address into
752 * r12 if we use the global entry point of a function. */
753 UCONTEXT_REG_Rn(uc, 12) = (unsigned long) func;
760 mono_arch_handle_exception (void *ctx, gpointer obj)
762 #if defined(MONO_ARCH_USE_SIGACTION) && defined(UCONTEXT_REG_Rn)
764 * Handling the exception in the signal handler is problematic, since the original
765 * signal is disabled, and we could run arbitrary code though the debugger. So
766 * resume into the normal stack and do most work there if possible.
768 MonoJitTlsData *jit_tls = mono_tls_get_jit_tls ();
774 /* Pass the ctx parameter in TLS */
775 mono_sigctx_to_monoctx (sigctx, &jit_tls->ex_ctx);
776 /* The others in registers */
777 UCONTEXT_REG_Rn (sigctx, PPC_FIRST_ARG_REG) = (gsize)obj;
779 /* Allocate a stack frame below the red zone */
780 /* Similar to mono_arch_handle_altstack_exception () */
784 sp = (mgreg_t)(UCONTEXT_REG_Rn(uc, 1) & ~15);
785 sp = (mgreg_t)(sp - frame_size);
786 UCONTEXT_REG_Rn(uc, 1) = (mgreg_t)sp;
787 setup_ucontext_return (uc, handle_signal_exception);
794 mono_sigctx_to_monoctx (ctx, &mctx);
796 result = mono_handle_exception (&mctx, obj);
797 /* restore the context so that returning from the signal handler will invoke
800 mono_monoctx_to_sigctx (&mctx, ctx);
806 // FIX ME: This is not complete
808 mono_arch_setup_async_callback (MonoContext *ctx, void (*async_cb)(void *fun), gpointer user_data)
810 uintptr_t sp = (uintptr_t) MONO_CONTEXT_GET_SP(ctx);
811 sp -= PPC_MINIMAL_STACK_SIZE;
812 *(unsigned long *)sp = MONO_CONTEXT_GET_SP(ctx);
813 MONO_CONTEXT_SET_BP(ctx, sp);
814 MONO_CONTEXT_SET_IP(ctx, (unsigned long) async_cb);