3 * exception support for PowerPC
6 * Dietmar Maurer (dietmar@ximian.com)
7 * Paolo Molaro (lupus@ximian.com)
8 * Andreas Faerber <andreas.faerber@web.de>
10 * (C) 2001 Ximian, Inc.
11 * (C) 2007-2008 Andreas Faerber
23 #include <mono/arch/ppc/ppc-codegen.h>
24 #include <mono/metadata/appdomain.h>
25 #include <mono/metadata/tabledefs.h>
26 #include <mono/metadata/threads.h>
27 #include <mono/metadata/debug-helpers.h>
28 #include <mono/metadata/exception.h>
29 #include <mono/metadata/mono-debug.h>
37 int sc_onstack; // sigstack state to restore
38 int sc_mask; // signal mask to restore
40 int sc_psw; // processor status word
41 int sc_sp; // stack pointer if sc_regs == NULL
42 void *sc_regs; // (kernel private) saved state
47 sigset_t uc_sigmask; // signal mask used by this context
48 stack_t uc_stack; // stack used by this context
49 struct ucontext *uc_link; // pointer to resuming context
50 size_t uc_mcsize; // size of the machine context passed in
51 mcontext_t uc_mcontext; // machine specific context
54 typedef struct ppc_exception_state {
55 unsigned long dar; // Fault registers for coredump
57 unsigned long exception;// number of powerpc exception taken
58 unsigned long pad0; // align to 16 bytes
60 unsigned long pad1[4]; // space in PCB "just in case"
61 } ppc_exception_state_t;
63 typedef struct ppc_vector_state {
64 unsigned long save_vr[32][4];
65 unsigned long save_vscr[4];
66 unsigned int save_pad5[4];
67 unsigned int save_vrvalid; // VRs that have been saved
68 unsigned int save_pad6[7];
71 typedef struct ppc_float_state {
74 unsigned int fpscr_pad; // fpscr is 64 bits, 32 bits of rubbish
75 unsigned int fpscr; // floating point status register
78 typedef struct ppc_thread_state {
79 unsigned int srr0; // Instruction address register (PC)
80 unsigned int srr1; // Machine state register (supervisor)
86 unsigned int cr; // Condition register
87 unsigned int xer; // User's integer exception register
88 unsigned int lr; // Link register
89 unsigned int ctr; // Count register
90 unsigned int mq; // MQ register (601 only)
92 unsigned int vrsave; // Vector Save Register
96 ppc_exception_state_t es;
97 ppc_thread_state_t ss;
99 ppc_vector_state_t vs;
102 typedef struct mcontext * mcontext_t;
104 Linux/PPC instead has:
106 unsigned long _unused[4];
108 unsigned long handler;
109 unsigned long oldmask;
110 struct pt_regs *regs;
113 unsigned long gpr[32];
116 unsigned long orig_gpr3; // Used for restarting system calls
121 unsigned long mq; // 601 only (not used at present)
122 // Used on APUS to hold IPL value.
123 unsigned long trap; // Reason for being here
124 // N.B. for critical exceptions on 4xx, the dar and dsisr
125 // fields are overloaded to hold srr0 and srr1.
126 unsigned long dar; // Fault registers
127 unsigned long dsisr; // on 4xx/Book-E used for ESR
128 unsigned long result; // Result of a system call
131 elf_gregset_t mc_gregs;
132 elf_fpregset_t mc_fregs;
133 unsigned long mc_pad[2];
134 elf_vrregset_t mc_vregs __attribute__((__aligned__(16)));
138 unsigned long uc_flags;
139 struct ucontext *uc_link;
142 struct mcontext *uc_regs; // points to uc_mcontext field
144 // glibc has 1024-bit signal masks, ours are 64-bit
147 struct mcontext uc_mcontext;
150 #define ELF_NGREG 48 // includes nip, msr, lr, etc.
151 #define ELF_NFPREG 33 // includes fpscr
154 typedef unsigned long elf_greg_t;
155 typedef elf_greg_t elf_gregset_t[ELF_NGREG];
157 // Floating point registers
158 typedef double elf_fpreg_t;
159 typedef elf_fpreg_t elf_fpregset_t[ELF_NFPREG];
165 #define restore_regs_from_context(ctx_reg,ip_reg,tmp_reg) do { \
167 ppc_ldptr (code, ip_reg, G_STRUCT_OFFSET (MonoContext, sc_ir), ctx_reg); \
168 ppc_load_multiple_regs (code, MONO_PPC_FIRST_SAVED_GREG, \
169 G_STRUCT_OFFSET (MonoContext, regs) + MONO_PPC_FIRST_SAVED_GREG * sizeof (gpointer), ctx_reg); \
170 for (reg = MONO_PPC_FIRST_SAVED_FREG; reg < MONO_MAX_FREGS; ++reg) { \
171 ppc_lfd (code, reg, \
172 G_STRUCT_OFFSET(MonoContext, fregs) + reg * sizeof (gdouble), ctx_reg); \
177 #define setup_context(ctx)
179 #ifdef PPC_USES_FUNCTION_DESCRIPTOR
181 mono_ppc_create_pre_code_ftnptr (guint8 *code)
183 MonoPPCFunctionDescriptor *ftnptr = (MonoPPCFunctionDescriptor*)code;
185 code += sizeof (MonoPPCFunctionDescriptor);
195 * arch_get_restore_context:
197 * Returns a pointer to a method which restores a previously saved sigcontext.
198 * The first argument in r3 is the pointer to the context.
201 mono_arch_get_restore_context (MonoTrampInfo **info, gboolean aot)
203 guint8 *start, *code;
204 int size = MONO_PPC_32_64_CASE (128, 172) + PPC_FTNPTR_SIZE;
205 MonoJumpInfo *ji = NULL;
206 GSList *unwind_ops = NULL;
208 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_ldptr (code, ppc_sp, G_STRUCT_OFFSET (MonoContext, sc_sp), ppc_r3);
215 /* jump to the saved IP */
216 ppc_mtctr (code, ppc_r4);
217 ppc_bcctr (code, PPC_BR_ALWAYS, 0);
221 g_assert ((code - start) <= size);
222 mono_arch_flush_icache (start, code - start);
223 MONO_PROFILER_RAISE (jit_code_buffer, (start, code - start, MONO_PROFILER_CODE_BUFFER_EXCEPTION_HANDLING, NULL));
226 *info = mono_tramp_info_create ("restore_context", start, code - start, ji, unwind_ops);
231 #define SAVED_REGS_LENGTH (sizeof (gdouble) * MONO_MAX_FREGS + sizeof (gpointer) * MONO_MAX_IREGS)
232 #define ALIGN_STACK_FRAME_SIZE(s) (((s) + MONO_ARCH_FRAME_ALIGNMENT - 1) & ~(MONO_ARCH_FRAME_ALIGNMENT - 1))
233 /* The 64 bytes here are for outgoing arguments and a bit of spare.
234 We don't use it all, but it doesn't hurt. */
235 #define REG_SAVE_STACK_FRAME_SIZE (ALIGN_STACK_FRAME_SIZE (SAVED_REGS_LENGTH + PPC_MINIMAL_STACK_SIZE + 64))
238 emit_save_saved_regs (guint8 *code, int pos)
242 for (i = MONO_MAX_FREGS - 1; i >= MONO_PPC_FIRST_SAVED_FREG; --i) {
243 pos -= sizeof (gdouble);
244 ppc_stfd (code, i, pos, ppc_sp);
246 pos -= (MONO_MAX_FREGS - MONO_SAVED_FREGS) * sizeof (gdouble);
247 pos -= sizeof (gpointer) * MONO_SAVED_GREGS;
248 ppc_store_multiple_regs (code, MONO_PPC_FIRST_SAVED_GREG, pos, ppc_sp);
254 * mono_arch_get_call_filter:
256 * Returns a pointer to a method which calls an exception filter. We
257 * also use this function to call finally handlers (we pass NULL as
258 * @exc object in this case).
261 mono_arch_get_call_filter (MonoTrampInfo **info, gboolean aot)
263 guint8 *start, *code;
264 int alloc_size, pos, i;
265 int size = MONO_PPC_32_64_CASE (320, 500) + PPC_FTNPTR_SIZE;
266 MonoJumpInfo *ji = NULL;
267 GSList *unwind_ops = NULL;
269 /* call_filter (MonoContext *ctx, unsigned long eip, gpointer exc) */
270 code = start = mono_global_codeman_reserve (size);
272 code = mono_ppc_create_pre_code_ftnptr (code);
275 ppc_mflr (code, ppc_r0);
276 ppc_stptr (code, ppc_r0, PPC_RET_ADDR_OFFSET, ppc_sp);
278 alloc_size = REG_SAVE_STACK_FRAME_SIZE;
280 /* allocate stack frame and set link from sp in ctx */
281 g_assert ((alloc_size & (MONO_ARCH_FRAME_ALIGNMENT-1)) == 0);
282 ppc_ldptr (code, ppc_r0, G_STRUCT_OFFSET (MonoContext, sc_sp), ppc_r3);
283 ppc_ldptr_indexed (code, ppc_r0, ppc_r0, ppc_r0);
284 ppc_stptr_update (code, ppc_r0, -alloc_size, ppc_sp);
286 code = emit_save_saved_regs (code, alloc_size);
288 /* restore all the regs from ctx (in r3), but not r1, the stack pointer */
289 restore_regs_from_context (ppc_r3, ppc_r6, ppc_r7);
290 /* call handler at eip (r4) and set the first arg with the exception (r5) */
291 ppc_mtctr (code, ppc_r4);
292 ppc_mr (code, ppc_r3, ppc_r5);
293 ppc_bcctrl (code, PPC_BR_ALWAYS, 0);
296 ppc_ldptr (code, ppc_r0, alloc_size + PPC_RET_ADDR_OFFSET, ppc_sp);
297 ppc_mtlr (code, ppc_r0);
299 /* restore all the regs from the stack */
301 for (i = MONO_MAX_FREGS - 1; i >= MONO_PPC_FIRST_SAVED_FREG; --i) {
302 pos -= sizeof (gdouble);
303 ppc_lfd (code, i, pos, ppc_sp);
305 pos -= (MONO_MAX_FREGS - MONO_SAVED_FREGS) * sizeof (gdouble);
306 pos -= sizeof (gpointer) * MONO_SAVED_GREGS;
307 ppc_load_multiple_regs (code, MONO_PPC_FIRST_SAVED_GREG, pos, ppc_sp);
309 ppc_addic (code, ppc_sp, ppc_sp, alloc_size);
312 g_assert ((code - start) < size);
313 mono_arch_flush_icache (start, code - start);
314 MONO_PROFILER_RAISE (jit_code_buffer, (start, code - start, MONO_PROFILER_CODE_BUFFER_EXCEPTION_HANDLING, NULL));
317 *info = mono_tramp_info_create ("call_filter", start, code - start, ji, unwind_ops);
323 mono_ppc_throw_exception (MonoObject *exc, unsigned long eip, unsigned long esp, mgreg_t *int_regs, gdouble *fp_regs, gboolean rethrow)
328 /* adjust eip so that it point into the call instruction */
331 setup_context (&ctx);
333 /*printf ("stack in throw: %p\n", esp);*/
334 MONO_CONTEXT_SET_BP (&ctx, esp);
335 MONO_CONTEXT_SET_IP (&ctx, eip);
336 memcpy (&ctx.regs, int_regs, sizeof (mgreg_t) * MONO_MAX_IREGS);
337 memcpy (&ctx.fregs, fp_regs, sizeof (double) * MONO_MAX_FREGS);
339 if (mono_object_isinst_checked (exc, mono_defaults.exception_class, &error)) {
340 MonoException *mono_ex = (MonoException*)exc;
342 mono_ex->stack_trace = NULL;
343 mono_ex->trace_ips = NULL;
346 mono_error_assert_ok (&error);
347 mono_handle_exception (&ctx, exc);
348 mono_restore_context (&ctx);
350 g_assert_not_reached ();
354 * arch_get_throw_exception_generic:
356 * Returns a function pointer which can be used to raise
357 * exceptions. The returned function has the following
358 * signature: void (*func) (MonoException *exc); or
359 * void (*func) (guint32 ex_token, gpointer ip)
363 mono_arch_get_throw_exception_generic (int size, MonoTrampInfo **info, int corlib, gboolean rethrow, gboolean aot)
365 guint8 *start, *code;
367 MonoJumpInfo *ji = NULL;
368 GSList *unwind_ops = NULL;
370 code = start = mono_global_codeman_reserve (size);
372 code = mono_ppc_create_pre_code_ftnptr (code);
376 ppc_mr (code, ppc_r0, ppc_r4);
378 ppc_mflr (code, ppc_r0);
379 ppc_stptr (code, ppc_r0, PPC_RET_ADDR_OFFSET, ppc_sp);
381 alloc_size = REG_SAVE_STACK_FRAME_SIZE;
383 g_assert ((alloc_size & (MONO_ARCH_FRAME_ALIGNMENT-1)) == 0);
384 ppc_stptr_update (code, ppc_sp, -alloc_size, ppc_sp);
386 code = emit_save_saved_regs (code, alloc_size);
390 ppc_mr (code, ppc_r4, ppc_r3);
393 code = mono_arch_emit_load_aotconst (start, code, &ji, MONO_PATCH_INFO_IMAGE, mono_defaults.corlib);
394 ppc_mr (code, ppc_r3, ppc_r12);
395 code = mono_arch_emit_load_aotconst (start, code, &ji, MONO_PATCH_INFO_JIT_ICALL_ADDR, "mono_exception_from_token");
396 #ifdef PPC_USES_FUNCTION_DESCRIPTOR
397 ppc_ldptr (code, ppc_r2, sizeof (gpointer), ppc_r12);
398 ppc_ldptr (code, ppc_r12, 0, ppc_r12);
400 ppc_mtctr (code, ppc_r12);
401 ppc_bcctrl (code, PPC_BR_ALWAYS, 0);
403 ppc_load (code, ppc_r3, (gulong)mono_defaults.corlib);
404 ppc_load_func (code, PPC_CALL_REG, mono_exception_from_token);
405 ppc_mtctr (code, PPC_CALL_REG);
406 ppc_bcctrl (code, PPC_BR_ALWAYS, 0);
410 /* call throw_exception (exc, ip, sp, int_regs, fp_regs) */
412 ppc_ldptr (code, ppc_r5, 0, ppc_sp);
413 /* exc is already in place in r3 */
415 ppc_ldptr (code, ppc_r4, PPC_RET_ADDR_OFFSET, ppc_r5);
417 ppc_mr (code, ppc_r4, ppc_r0); /* caller ip */
418 /* pointer to the saved fp regs */
419 pos = alloc_size - sizeof (gdouble) * MONO_MAX_FREGS;
420 ppc_addi (code, ppc_r7, ppc_sp, pos);
421 /* pointer to the saved int regs */
422 pos -= sizeof (gpointer) * MONO_MAX_IREGS;
423 ppc_addi (code, ppc_r6, ppc_sp, pos);
424 ppc_li (code, ppc_r8, rethrow);
427 // This can be called from runtime code, which can't guarantee that
428 // r30 contains the got address.
429 // So emit the got address loading code too
430 code = mono_arch_emit_load_got_addr (start, code, NULL, &ji);
431 code = mono_arch_emit_load_aotconst (start, code, &ji, MONO_PATCH_INFO_JIT_ICALL_ADDR, "mono_ppc_throw_exception");
432 #ifdef PPC_USES_FUNCTION_DESCRIPTOR
433 ppc_ldptr (code, ppc_r2, sizeof (gpointer), ppc_r12);
434 ppc_ldptr (code, ppc_r12, 0, ppc_r12);
436 ppc_mtctr (code, ppc_r12);
437 ppc_bcctrl (code, PPC_BR_ALWAYS, 0);
439 ppc_load_func (code, PPC_CALL_REG, mono_ppc_throw_exception);
440 ppc_mtctr (code, PPC_CALL_REG);
441 ppc_bcctrl (code, PPC_BR_ALWAYS, 0);
443 /* we should never reach this breakpoint */
445 g_assert ((code - start) <= size);
446 mono_arch_flush_icache (start, code - start);
447 MONO_PROFILER_RAISE (jit_code_buffer, (start, code - start, MONO_PROFILER_CODE_BUFFER_EXCEPTION_HANDLING, NULL));
450 *info = mono_tramp_info_create (corlib ? "throw_corlib_exception" : (rethrow ? "rethrow_exception" : "throw_exception"), start, code - start, ji, unwind_ops);
456 * 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);
462 mono_arch_get_rethrow_exception (MonoTrampInfo **info, gboolean aot)
464 int size = MONO_PPC_32_64_CASE (132, 224) + PPC_FTNPTR_SIZE;
468 return mono_arch_get_throw_exception_generic (size, info, FALSE, TRUE, aot);
472 * arch_get_throw_exception:
474 * Returns a function pointer which can be used to raise
475 * exceptions. The returned function has the following
476 * signature: void (*func) (MonoException *exc);
477 * For example to raise an arithmetic exception you can use:
479 * x86_push_imm (code, mono_get_exception_arithmetic ());
480 * x86_call_code (code, arch_get_throw_exception ());
484 mono_arch_get_throw_exception (MonoTrampInfo **info, gboolean aot)
486 int size = MONO_PPC_32_64_CASE (132, 224) + PPC_FTNPTR_SIZE;
490 return mono_arch_get_throw_exception_generic (size, info, FALSE, FALSE, aot);
494 * mono_arch_get_throw_corlib_exception:
495 * \returns a function pointer which can be used to raise
496 * corlib exceptions. The returned function has the following
497 * signature: void (*func) (guint32 ex_token, guint32 offset);
498 * On PPC, we pass the ip instead of the offset
501 mono_arch_get_throw_corlib_exception (MonoTrampInfo **info, gboolean aot)
503 int size = MONO_PPC_32_64_CASE (168, 304) + PPC_FTNPTR_SIZE;
507 return mono_arch_get_throw_exception_generic (size, info, TRUE, FALSE, aot);
511 * mono_arch_unwind_frame:
513 * See exceptions-amd64.c for docs.
516 mono_arch_unwind_frame (MonoDomain *domain, MonoJitTlsData *jit_tls,
517 MonoJitInfo *ji, MonoContext *ctx,
518 MonoContext *new_ctx, MonoLMF **lmf,
519 mgreg_t **save_locations,
520 StackFrameInfo *frame)
522 gpointer ip = MONO_CONTEXT_GET_IP (ctx);
523 MonoPPCStackFrame *sframe;
525 memset (frame, 0, sizeof (StackFrameInfo));
529 setup_context (new_ctx);
533 mgreg_t regs [ppc_lr + 1];
535 guint32 unwind_info_len;
538 if (ji->is_trampoline)
539 frame->type = FRAME_TYPE_TRAMPOLINE;
541 frame->type = FRAME_TYPE_MANAGED;
543 unwind_info = mono_jinfo_get_unwind_info (ji, &unwind_info_len);
545 sframe = (MonoPPCStackFrame*)MONO_CONTEXT_GET_SP (ctx);
546 MONO_CONTEXT_SET_BP (new_ctx, sframe->sp);
547 if (!ji->is_trampoline && jinfo_get_method (ji)->save_lmf) {
548 /* sframe->sp points just past the end of the LMF */
549 guint8 *lmf_addr = (guint8*)sframe->sp - sizeof (MonoLMF);
550 memcpy (&new_ctx->fregs [MONO_PPC_FIRST_SAVED_FREG], lmf_addr + G_STRUCT_OFFSET (MonoLMF, fregs), sizeof (double) * MONO_SAVED_FREGS);
551 memcpy (&new_ctx->regs [MONO_PPC_FIRST_SAVED_GREG], lmf_addr + G_STRUCT_OFFSET (MonoLMF, iregs), sizeof (mgreg_t) * MONO_SAVED_GREGS);
552 /* the calling IP is in the parent frame */
553 sframe = (MonoPPCStackFrame*)sframe->sp;
554 /* we substract 4, so that the IP points into the call instruction */
555 MONO_CONTEXT_SET_IP (new_ctx, sframe->lr - 4);
557 regs [ppc_lr] = ctx->sc_ir;
558 regs [ppc_sp] = ctx->sc_sp;
559 for (i = MONO_PPC_FIRST_SAVED_GREG; i < MONO_MAX_IREGS; ++i)
560 regs [i] = ctx->regs [i];
562 mono_unwind_frame (unwind_info, unwind_info_len, ji->code_start,
563 (guint8*)ji->code_start + ji->code_size,
564 ip, NULL, regs, ppc_lr + 1,
565 save_locations, MONO_MAX_IREGS, &cfa);
567 /* we substract 4, so that the IP points into the call instruction */
568 MONO_CONTEXT_SET_IP (new_ctx, regs [ppc_lr] - 4);
569 MONO_CONTEXT_SET_BP (new_ctx, cfa);
571 for (i = MONO_PPC_FIRST_SAVED_GREG; i < MONO_MAX_IREGS; ++i)
572 new_ctx->regs [i] = regs [i];
578 if ((ji = mini_jit_info_table_find (domain, (gpointer)(*lmf)->eip, NULL))) {
583 /* Trampoline lmf frame */
584 frame->method = (*lmf)->method;
587 /*sframe = (MonoPPCStackFrame*)MONO_CONTEXT_GET_SP (ctx);
588 MONO_CONTEXT_SET_BP (new_ctx, sframe->sp);
589 MONO_CONTEXT_SET_IP (new_ctx, sframe->lr);*/
590 MONO_CONTEXT_SET_BP (new_ctx, (*lmf)->ebp);
591 MONO_CONTEXT_SET_IP (new_ctx, (*lmf)->eip);
592 memcpy (&new_ctx->regs [MONO_PPC_FIRST_SAVED_GREG], (*lmf)->iregs, sizeof (mgreg_t) * MONO_SAVED_GREGS);
593 memcpy (&new_ctx->fregs [MONO_PPC_FIRST_SAVED_FREG], (*lmf)->fregs, sizeof (double) * MONO_SAVED_FREGS);
596 frame->type = FRAME_TYPE_MANAGED_TO_NATIVE;
598 /* FIXME: what about trampoline LMF frames? see exceptions-x86.c */
600 *lmf = (*lmf)->previous_lmf;
609 mono_arch_ip_from_context (void *sigctx)
611 #ifdef MONO_CROSS_COMPILE
612 g_assert_not_reached ();
614 os_ucontext *uc = sigctx;
615 return (gpointer)UCONTEXT_REG_NIP(uc);
620 mono_ppc_set_func_into_sigctx (void *sigctx, void *func)
622 #ifdef MONO_CROSS_COMPILE
623 g_assert_not_reached ();
624 #elif defined(PPC_USES_FUNCTION_DESCRIPTOR)
625 /* Have to set both the ip and the TOC reg */
626 os_ucontext *uc = sigctx;
628 UCONTEXT_REG_NIP(uc) = ((gsize*)func) [0];
629 UCONTEXT_REG_Rn (uc, 2) = ((gsize*)func)[1];
631 g_assert_not_reached ();
636 altstack_handle_and_restore (void *sigctx, gpointer obj)
640 mono_sigctx_to_monoctx (sigctx, &mctx);
641 mono_handle_exception (&mctx, obj);
642 mono_restore_context (&mctx);
646 mono_arch_handle_altstack_exception (void *sigctx, MONO_SIG_HANDLER_INFO_TYPE *siginfo, gpointer fault_addr, gboolean stack_ovf)
648 #ifdef MONO_CROSS_COMPILE
649 g_assert_not_reached ();
651 #ifdef MONO_ARCH_USE_SIGACTION
652 os_ucontext *uc = (ucontext_t*)sigctx;
653 os_ucontext *uc_copy;
654 MonoJitInfo *ji = mini_jit_info_table_find (mono_domain_get (), mono_arch_ip_from_context (sigctx), NULL);
660 /* we don't do much now, but we can warn the user with a useful message */
661 fprintf (stderr, "Stack overflow: IP: %p, SP: %p\n", mono_arch_ip_from_context (sigctx), (gpointer)UCONTEXT_REG_Rn(uc, 1));
662 if (ji && !ji->is_trampoline && jinfo_get_method (ji))
663 method = mono_method_full_name (jinfo_get_method (ji), TRUE);
665 method = "Unmanaged";
666 fprintf (stderr, "At %s\n", method);
670 mono_handle_native_crash ("SIGSEGV", sigctx, siginfo);
671 /* setup a call frame on the real stack so that control is returned there
672 * and exception handling can continue.
673 * The frame looks like:
676 * 224 is the size of the red zone
678 frame_size = sizeof (ucontext_t) + sizeof (gpointer) * 16 + 224;
681 sp = (gpointer)(UCONTEXT_REG_Rn(uc, 1) & ~15);
682 sp = (gpointer)((char*)sp - frame_size);
683 /* may need to adjust pointers in the new struct copy, depending on the OS */
684 uc_copy = (ucontext_t*)(sp + 16);
685 memcpy (uc_copy, uc, sizeof (os_ucontext));
686 #if defined(__linux__) && !defined(__mono_ppc64__)
687 uc_copy->uc_mcontext.uc_regs = (gpointer)((char*)uc_copy + ((char*)uc->uc_mcontext.uc_regs - (char*)uc));
689 g_assert (mono_arch_ip_from_context (uc) == mono_arch_ip_from_context (uc_copy));
690 /* at the return form the signal handler execution starts in altstack_handle_and_restore() */
691 UCONTEXT_REG_LNK(uc) = UCONTEXT_REG_NIP(uc);
692 #ifdef PPC_USES_FUNCTION_DESCRIPTOR
694 MonoPPCFunctionDescriptor *handler_ftnptr = (MonoPPCFunctionDescriptor*)altstack_handle_and_restore;
696 UCONTEXT_REG_NIP(uc) = (gulong)handler_ftnptr->code;
697 UCONTEXT_REG_Rn(uc, 2) = (gulong)handler_ftnptr->toc;
700 UCONTEXT_REG_NIP(uc) = (unsigned long)altstack_handle_and_restore;
702 /* ELF v2 ABI calling convention requires to put the target address into
703 * r12 if we use the global entry point of a function. */
704 UCONTEXT_REG_Rn(uc, 12) = (unsigned long) altstack_handle_and_restore;
707 UCONTEXT_REG_Rn(uc, 1) = (unsigned long)sp;
708 UCONTEXT_REG_Rn(uc, PPC_FIRST_ARG_REG) = (unsigned long)(sp + 16);
709 UCONTEXT_REG_Rn(uc, PPC_FIRST_ARG_REG + 1) = 0;
710 UCONTEXT_REG_Rn(uc, PPC_FIRST_ARG_REG + 2) = 0;
713 #endif /* !MONO_CROSS_COMPILE */
719 * Called by resuming from a signal handler.
722 handle_signal_exception (gpointer obj)
724 MonoJitTlsData *jit_tls = mono_tls_get_jit_tls ();
727 memcpy (&ctx, &jit_tls->ex_ctx, sizeof (MonoContext));
729 mono_handle_exception (&ctx, obj);
731 mono_restore_context (&ctx);
735 setup_ucontext_return (void *uc, gpointer func)
737 #if !defined(MONO_CROSS_COMPILE)
738 UCONTEXT_REG_LNK(uc) = UCONTEXT_REG_NIP(uc);
739 #ifdef PPC_USES_FUNCTION_DESCRIPTOR
741 MonoPPCFunctionDescriptor *handler_ftnptr = (MonoPPCFunctionDescriptor*)func;
743 UCONTEXT_REG_NIP(uc) = (gulong)handler_ftnptr->code;
744 UCONTEXT_REG_Rn(uc, 2) = (gulong)handler_ftnptr->toc;
747 UCONTEXT_REG_NIP(uc) = (unsigned long)func;
749 /* ELF v2 ABI calling convention requires to put the target address into
750 * r12 if we use the global entry point of a function. */
751 UCONTEXT_REG_Rn(uc, 12) = (unsigned long) func;
758 mono_arch_handle_exception (void *ctx, gpointer obj)
760 #if defined(MONO_ARCH_USE_SIGACTION) && defined(UCONTEXT_REG_Rn)
762 * Handling the exception in the signal handler is problematic, since the original
763 * signal is disabled, and we could run arbitrary code though the debugger. So
764 * resume into the normal stack and do most work there if possible.
766 MonoJitTlsData *jit_tls = mono_tls_get_jit_tls ();
772 /* Pass the ctx parameter in TLS */
773 mono_sigctx_to_monoctx (sigctx, &jit_tls->ex_ctx);
774 /* The others in registers */
775 UCONTEXT_REG_Rn (sigctx, PPC_FIRST_ARG_REG) = (gsize)obj;
777 /* Allocate a stack frame below the red zone */
778 /* Similar to mono_arch_handle_altstack_exception () */
782 sp = (mgreg_t)(UCONTEXT_REG_Rn(uc, 1) & ~15);
783 sp = (mgreg_t)(sp - frame_size);
784 UCONTEXT_REG_Rn(uc, 1) = (mgreg_t)sp;
785 setup_ucontext_return (uc, handle_signal_exception);
792 mono_sigctx_to_monoctx (ctx, &mctx);
794 result = mono_handle_exception (&mctx, obj);
795 /* restore the context so that returning from the signal handler will invoke
798 mono_monoctx_to_sigctx (&mctx, ctx);
804 // FIX ME: This is not complete
806 mono_arch_setup_async_callback (MonoContext *ctx, void (*async_cb)(void *fun), gpointer user_data)
808 uintptr_t sp = (uintptr_t) MONO_CONTEXT_GET_SP(ctx);
809 sp -= PPC_MINIMAL_STACK_SIZE;
810 *(unsigned long *)sp = MONO_CONTEXT_GET_SP(ctx);
811 MONO_CONTEXT_SET_BP(ctx, sp);
812 MONO_CONTEXT_SET_IP(ctx, (unsigned long) async_cb);