Treat CEE_BREAK the same as Debugger:Break (), i.e. route it through sdb.
[mono-project/dkf.git] / mono / mini / exceptions-ppc.c
blob6f860587da1958613e5222819f3ef8315159eb29
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_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); \
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 (MonoTrampInfo **info, gboolean aot)
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;
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);
219 g_assert ((code - start) <= size);
220 mono_arch_flush_icache (start, code - start);
222 if (info)
223 *info = mono_tramp_info_create (g_strdup_printf ("restore_context"), start, code - start, ji, unwind_ops);
225 return start;
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))
234 static guint8*
235 emit_save_saved_regs (guint8 *code, int pos)
237 int i;
239 for (i = 31; i >= 14; --i) {
240 pos -= sizeof (gdouble);
241 ppc_stfd (code, i, pos, ppc_sp);
243 pos -= sizeof (gpointer) * MONO_SAVED_GREGS;
244 ppc_store_multiple_regs (code, ppc_r13, pos, ppc_sp);
246 return code;
250 * mono_arch_get_call_filter:
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).
256 gpointer
257 mono_arch_get_call_filter (MonoTrampInfo **info, gboolean aot)
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;
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);
270 /* store ret addr */
271 ppc_mflr (code, ppc_r0);
272 ppc_stptr (code, ppc_r0, PPC_RET_ADDR_OFFSET, ppc_sp);
274 alloc_size = REG_SAVE_STACK_FRAME_SIZE;
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);
282 code = emit_save_saved_regs (code, alloc_size);
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);
291 /* epilog */
292 ppc_ldptr (code, ppc_r0, alloc_size + PPC_RET_ADDR_OFFSET, ppc_sp);
293 ppc_mtlr (code, ppc_r0);
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);
301 pos -= sizeof (gpointer) * MONO_SAVED_GREGS;
302 ppc_load_multiple_regs (code, ppc_r13, pos, ppc_sp);
304 ppc_addic (code, ppc_sp, ppc_sp, alloc_size);
305 ppc_blr (code);
307 g_assert ((code - start) < size);
308 mono_arch_flush_icache (start, code - start);
310 if (info)
311 *info = mono_tramp_info_create (g_strdup_printf ("call_filter"), start, code - start, ji, unwind_ops);
313 return start;
316 void
317 mono_ppc_throw_exception (MonoObject *exc, unsigned long eip, unsigned long esp, mgreg_t *int_regs, gdouble *fp_regs, gboolean rethrow)
319 static void (*restore_context) (MonoContext *);
320 MonoContext ctx;
322 if (!restore_context)
323 restore_context = mono_get_restore_context ();
325 /* adjust eip so that it point into the call instruction */
326 eip -= 4;
328 setup_context (&ctx);
330 /*printf ("stack in throw: %p\n", esp);*/
331 MONO_CONTEXT_SET_BP (&ctx, esp);
332 MONO_CONTEXT_SET_IP (&ctx, eip);
333 memcpy (&ctx.regs, int_regs, sizeof (mgreg_t) * MONO_SAVED_GREGS);
334 memcpy (&ctx.fregs, fp_regs, sizeof (double) * MONO_SAVED_FREGS);
336 if (mono_object_isinst (exc, mono_defaults.exception_class)) {
337 MonoException *mono_ex = (MonoException*)exc;
338 if (!rethrow)
339 mono_ex->stack_trace = NULL;
341 mono_handle_exception (&ctx, exc, (gpointer)eip, FALSE);
342 restore_context (&ctx);
344 g_assert_not_reached ();
348 * arch_get_throw_exception_generic:
350 * Returns a function pointer which can be used to raise
351 * exceptions. The returned function has the following
352 * signature: void (*func) (MonoException *exc); or
353 * void (*func) (guint32 ex_token, gpointer ip)
356 static gpointer
357 mono_arch_get_throw_exception_generic (int size, MonoTrampInfo **info, int corlib, gboolean rethrow, gboolean aot)
359 guint8 *start, *code;
360 int alloc_size, pos;
361 MonoJumpInfo *ji = NULL;
362 GSList *unwind_ops = NULL;
364 code = start = mono_global_codeman_reserve (size);
365 if (!aot)
366 code = mono_ppc_create_pre_code_ftnptr (code);
368 /* store ret addr */
369 if (corlib)
370 ppc_mr (code, ppc_r0, ppc_r4);
371 else
372 ppc_mflr (code, ppc_r0);
373 ppc_stptr (code, ppc_r0, PPC_RET_ADDR_OFFSET, ppc_sp);
375 alloc_size = REG_SAVE_STACK_FRAME_SIZE;
377 g_assert ((alloc_size & (MONO_ARCH_FRAME_ALIGNMENT-1)) == 0);
378 ppc_stptr_update (code, ppc_sp, -alloc_size, ppc_sp);
380 code = emit_save_saved_regs (code, alloc_size);
382 //ppc_break (code);
383 if (corlib) {
384 ppc_mr (code, ppc_r4, ppc_r3);
386 if (aot) {
387 code = mono_arch_emit_load_aotconst (start, code, &ji, MONO_PATCH_INFO_IMAGE, mono_defaults.corlib);
388 ppc_mr (code, ppc_r3, ppc_r11);
389 code = mono_arch_emit_load_aotconst (start, code, &ji, MONO_PATCH_INFO_JIT_ICALL_ADDR, "mono_exception_from_token");
390 #ifdef PPC_USES_FUNCTION_DESCRIPTOR
391 ppc_ldptr (code, ppc_r2, sizeof (gpointer), ppc_r11);
392 ppc_ldptr (code, ppc_r11, 0, ppc_r11);
393 #endif
394 ppc_mtctr (code, ppc_r11);
395 ppc_bcctrl (code, PPC_BR_ALWAYS, 0);
396 } else {
397 ppc_load (code, ppc_r3, (gulong)mono_defaults.corlib);
398 ppc_load_func (code, ppc_r0, mono_exception_from_token);
399 ppc_mtctr (code, ppc_r0);
400 ppc_bcctrl (code, PPC_BR_ALWAYS, 0);
404 /* call throw_exception (exc, ip, sp, int_regs, fp_regs) */
405 /* caller sp */
406 ppc_ldptr (code, ppc_r5, 0, ppc_sp);
407 /* exc is already in place in r3 */
408 if (corlib)
409 ppc_ldptr (code, ppc_r4, PPC_RET_ADDR_OFFSET, ppc_r5);
410 else
411 ppc_mr (code, ppc_r4, ppc_r0); /* caller ip */
412 /* pointer to the saved fp regs */
413 pos = alloc_size - sizeof (gdouble) * MONO_SAVED_FREGS;
414 ppc_addi (code, ppc_r7, ppc_sp, pos);
415 /* pointer to the saved int regs */
416 pos -= sizeof (gpointer) * MONO_SAVED_GREGS;
417 ppc_addi (code, ppc_r6, ppc_sp, pos);
418 ppc_li (code, ppc_r8, rethrow);
420 if (aot) {
421 // This can be called from runtime code, which can't guarantee that
422 // r30 contains the got address.
423 // So emit the got address loading code too
424 code = mono_arch_emit_load_got_addr (start, code, NULL, &ji);
425 code = mono_arch_emit_load_aotconst (start, code, &ji, MONO_PATCH_INFO_JIT_ICALL_ADDR, "mono_ppc_throw_exception");
426 #ifdef PPC_USES_FUNCTION_DESCRIPTOR
427 ppc_ldptr (code, ppc_r2, sizeof (gpointer), ppc_r11);
428 ppc_ldptr (code, ppc_r11, 0, ppc_r11);
429 #endif
430 ppc_mtctr (code, ppc_r11);
431 ppc_bcctrl (code, PPC_BR_ALWAYS, 0);
432 } else {
433 ppc_load_func (code, ppc_r0, mono_ppc_throw_exception);
434 ppc_mtctr (code, ppc_r0);
435 ppc_bcctrl (code, PPC_BR_ALWAYS, 0);
437 /* we should never reach this breakpoint */
438 ppc_break (code);
439 g_assert ((code - start) <= size);
440 mono_arch_flush_icache (start, code - start);
442 if (info)
443 *info = mono_tramp_info_create (g_strdup_printf (corlib ? "throw_corlib_exception" : (rethrow ? "rethrow_exception" : "throw_exception")), start, code - start, ji, unwind_ops);
445 return start;
449 * mono_arch_get_rethrow_exception:
451 * Returns a function pointer which can be used to rethrow
452 * exceptions. The returned function has the following
453 * signature: void (*func) (MonoException *exc);
456 gpointer
457 mono_arch_get_rethrow_exception (MonoTrampInfo **info, gboolean aot)
459 int size = MONO_PPC_32_64_CASE (132, 224) + PPC_FTNPTR_SIZE;
461 if (aot)
462 size += 64;
463 return mono_arch_get_throw_exception_generic (size, info, FALSE, TRUE, aot);
467 * arch_get_throw_exception:
469 * Returns a function pointer which can be used to raise
470 * exceptions. The returned function has the following
471 * signature: void (*func) (MonoException *exc);
472 * For example to raise an arithmetic exception you can use:
474 * x86_push_imm (code, mono_get_exception_arithmetic ());
475 * x86_call_code (code, arch_get_throw_exception ());
478 gpointer
479 mono_arch_get_throw_exception (MonoTrampInfo **info, gboolean aot)
481 int size = MONO_PPC_32_64_CASE (132, 224) + PPC_FTNPTR_SIZE;
483 if (aot)
484 size += 64;
485 return mono_arch_get_throw_exception_generic (size, info, FALSE, FALSE, aot);
489 * mono_arch_get_throw_corlib_exception:
491 * Returns a function pointer which can be used to raise
492 * corlib exceptions. The returned function has the following
493 * signature: void (*func) (guint32 ex_token, guint32 offset);
494 * On PPC, we pass the ip instead of the offset
496 gpointer
497 mono_arch_get_throw_corlib_exception (MonoTrampInfo **info, gboolean aot)
499 int size = MONO_PPC_32_64_CASE (168, 304) + PPC_FTNPTR_SIZE;
501 if (aot)
502 size += 64;
503 return mono_arch_get_throw_exception_generic (size, info, TRUE, FALSE, aot);
507 * mono_arch_find_jit_info:
509 * See exceptions-amd64.c for docs.
511 gboolean
512 mono_arch_find_jit_info (MonoDomain *domain, MonoJitTlsData *jit_tls,
513 MonoJitInfo *ji, MonoContext *ctx,
514 MonoContext *new_ctx, MonoLMF **lmf,
515 mgreg_t **save_locations,
516 StackFrameInfo *frame)
518 gpointer ip = MONO_CONTEXT_GET_IP (ctx);
519 MonoPPCStackFrame *sframe;
521 memset (frame, 0, sizeof (StackFrameInfo));
522 frame->ji = ji;
524 *new_ctx = *ctx;
525 setup_context (new_ctx);
527 if (ji != NULL) {
528 int i;
529 mgreg_t regs [ppc_lr + 1];
530 guint8 *cfa;
531 guint32 unwind_info_len;
532 guint8 *unwind_info;
534 frame->type = FRAME_TYPE_MANAGED;
536 if (ji->from_aot)
537 unwind_info = mono_aot_get_unwind_info (ji, &unwind_info_len);
538 else
539 unwind_info = mono_get_cached_unwind_info (ji->used_regs, &unwind_info_len);
541 sframe = (MonoPPCStackFrame*)MONO_CONTEXT_GET_SP (ctx);
542 MONO_CONTEXT_SET_BP (new_ctx, sframe->sp);
543 if (ji->method->save_lmf) {
544 /* sframe->sp points just past the end of the LMF */
545 guint8 *lmf_addr = (guint8*)sframe->sp - sizeof (MonoLMF);
546 memcpy (&new_ctx->fregs, lmf_addr + G_STRUCT_OFFSET (MonoLMF, fregs), sizeof (double) * MONO_SAVED_FREGS);
547 memcpy (&new_ctx->regs, lmf_addr + G_STRUCT_OFFSET (MonoLMF, iregs), sizeof (mgreg_t) * MONO_SAVED_GREGS);
548 /* the calling IP is in the parent frame */
549 sframe = (MonoPPCStackFrame*)sframe->sp;
550 /* we substract 4, so that the IP points into the call instruction */
551 MONO_CONTEXT_SET_IP (new_ctx, sframe->lr - 4);
552 } else {
553 regs [ppc_lr] = ctx->sc_ir;
554 regs [ppc_sp] = ctx->sc_sp;
555 for (i = 0; i < MONO_SAVED_GREGS; ++i)
556 regs [ppc_r13 + i] = ctx->regs [i];
558 mono_unwind_frame (unwind_info, unwind_info_len, ji->code_start,
559 (guint8*)ji->code_start + ji->code_size,
560 ip, regs, ppc_lr + 1,
561 save_locations, MONO_MAX_IREGS, &cfa);
563 /* we substract 4, so that the IP points into the call instruction */
564 MONO_CONTEXT_SET_IP (new_ctx, regs [ppc_lr] - 4);
565 MONO_CONTEXT_SET_BP (new_ctx, cfa);
567 for (i = 0; i < MONO_SAVED_GREGS; ++i)
568 new_ctx->regs [i] = regs [ppc_r13 + i];
571 if (*lmf && (MONO_CONTEXT_GET_SP (ctx) >= (gpointer)(*lmf)->ebp)) {
572 /* remove any unused lmf */
573 *lmf = (*lmf)->previous_lmf;
576 return TRUE;
577 } else if (*lmf) {
579 if ((ji = mini_jit_info_table_find (domain, (gpointer)(*lmf)->eip, NULL))) {
580 } else {
581 if (!(*lmf)->method)
582 return FALSE;
584 /* Trampoline lmf frame */
585 frame->method = (*lmf)->method;
588 /*sframe = (MonoPPCStackFrame*)MONO_CONTEXT_GET_SP (ctx);
589 MONO_CONTEXT_SET_BP (new_ctx, sframe->sp);
590 MONO_CONTEXT_SET_IP (new_ctx, sframe->lr);*/
591 MONO_CONTEXT_SET_BP (new_ctx, (*lmf)->ebp);
592 MONO_CONTEXT_SET_IP (new_ctx, (*lmf)->eip);
593 memcpy (&new_ctx->regs, (*lmf)->iregs, sizeof (mgreg_t) * MONO_SAVED_GREGS);
594 memcpy (&new_ctx->fregs, (*lmf)->fregs, sizeof (double) * MONO_SAVED_FREGS);
596 frame->ji = ji;
597 frame->type = FRAME_TYPE_MANAGED_TO_NATIVE;
599 /* FIXME: what about trampoline LMF frames? see exceptions-x86.c */
601 *lmf = (*lmf)->previous_lmf;
603 return TRUE;
606 return FALSE;
610 * This is the function called from the signal handler
612 void
613 mono_arch_sigctx_to_monoctx (void *ctx, MonoContext *mctx)
615 #ifdef MONO_CROSS_COMPILE
616 g_assert_not_reached ();
617 #else
618 os_ucontext *uc = ctx;
620 mctx->sc_ir = UCONTEXT_REG_NIP(uc);
621 mctx->sc_sp = UCONTEXT_REG_Rn(uc, 1);
622 memcpy (&mctx->regs, &UCONTEXT_REG_Rn(uc, 13), sizeof (mgreg_t) * MONO_SAVED_GREGS);
623 memcpy (&mctx->fregs, &UCONTEXT_REG_FPRn(uc, 14), sizeof (double) * MONO_SAVED_FREGS);
624 #endif
627 void
628 mono_arch_monoctx_to_sigctx (MonoContext *mctx, void *ctx)
630 #ifdef MONO_CROSS_COMPILE
631 g_assert_not_reached ();
632 #else
633 os_ucontext *uc = ctx;
635 UCONTEXT_REG_NIP(uc) = mctx->sc_ir;
636 UCONTEXT_REG_Rn(uc, 1) = mctx->sc_sp;
637 memcpy (&UCONTEXT_REG_Rn(uc, 13), &mctx->regs, sizeof (mgreg_t) * MONO_SAVED_GREGS);
638 memcpy (&UCONTEXT_REG_FPRn(uc, 14), &mctx->fregs, sizeof (double) * MONO_SAVED_FREGS);
639 #endif
642 gpointer
643 mono_arch_ip_from_context (void *sigctx)
645 #ifdef MONO_CROSS_COMPILE
646 g_assert_not_reached ();
647 #else
648 os_ucontext *uc = sigctx;
649 return (gpointer)UCONTEXT_REG_NIP(uc);
650 #endif
653 void
654 mono_ppc_set_func_into_sigctx (void *sigctx, void *func)
656 #ifdef MONO_CROSS_COMPILE
657 g_assert_not_reached ();
658 #elif defined(PPC_USES_FUNCTION_DESCRIPTOR)
659 /* Have to set both the ip and the TOC reg */
660 os_ucontext *uc = sigctx;
662 UCONTEXT_REG_NIP(uc) = ((gsize*)func) [0];
663 UCONTEXT_REG_Rn (uc, 2) = ((gsize*)func)[1];
664 #else
665 g_assert_not_reached ();
666 #endif
669 static void
670 altstack_handle_and_restore (void *sigctx, gpointer obj, gboolean test_only)
672 void (*restore_context) (MonoContext *);
673 MonoContext mctx;
675 restore_context = mono_get_restore_context ();
676 mono_arch_sigctx_to_monoctx (sigctx, &mctx);
677 mono_handle_exception (&mctx, obj, (gpointer)mctx.sc_ir, test_only);
678 restore_context (&mctx);
681 void
682 mono_arch_handle_altstack_exception (void *sigctx, gpointer fault_addr, gboolean stack_ovf)
684 #ifdef MONO_CROSS_COMPILE
685 g_assert_not_reached ();
686 #else
687 #ifdef MONO_ARCH_USE_SIGACTION
688 os_ucontext *uc = (ucontext_t*)sigctx;
689 os_ucontext *uc_copy;
690 MonoJitInfo *ji = mini_jit_info_table_find (mono_domain_get (), mono_arch_ip_from_context (sigctx), NULL);
691 gpointer *sp;
692 int frame_size;
694 if (stack_ovf) {
695 const char *method;
696 /* we don't do much now, but we can warn the user with a useful message */
697 fprintf (stderr, "Stack overflow: IP: %p, SP: %p\n", mono_arch_ip_from_context (sigctx), (gpointer)UCONTEXT_REG_Rn(uc, 1));
698 if (ji && ji->method)
699 method = mono_method_full_name (ji->method, TRUE);
700 else
701 method = "Unmanaged";
702 fprintf (stderr, "At %s\n", method);
703 abort ();
705 if (!ji)
706 mono_handle_native_sigsegv (SIGSEGV, sigctx);
707 /* setup a call frame on the real stack so that control is returned there
708 * and exception handling can continue.
709 * The frame looks like:
710 * ucontext struct
711 * ...
712 * 224 is the size of the red zone
714 frame_size = sizeof (ucontext_t) + sizeof (gpointer) * 16 + 224;
715 frame_size += 15;
716 frame_size &= ~15;
717 sp = (gpointer)(UCONTEXT_REG_Rn(uc, 1) & ~15);
718 sp = (gpointer)((char*)sp - frame_size);
719 /* may need to adjust pointers in the new struct copy, depending on the OS */
720 uc_copy = (ucontext_t*)(sp + 16);
721 memcpy (uc_copy, uc, sizeof (os_ucontext));
722 #if defined(__linux__) && !defined(__mono_ppc64__)
723 uc_copy->uc_mcontext.uc_regs = (gpointer)((char*)uc_copy + ((char*)uc->uc_mcontext.uc_regs - (char*)uc));
724 #endif
725 g_assert (mono_arch_ip_from_context (uc) == mono_arch_ip_from_context (uc_copy));
726 /* at the return form the signal handler execution starts in altstack_handle_and_restore() */
727 UCONTEXT_REG_LNK(uc) = UCONTEXT_REG_NIP(uc);
728 #ifdef PPC_USES_FUNCTION_DESCRIPTOR
730 MonoPPCFunctionDescriptor *handler_ftnptr = (MonoPPCFunctionDescriptor*)altstack_handle_and_restore;
732 UCONTEXT_REG_NIP(uc) = (gulong)handler_ftnptr->code;
733 UCONTEXT_REG_Rn(uc, 2) = (gulong)handler_ftnptr->toc;
735 #else
736 UCONTEXT_REG_NIP(uc) = (unsigned long)altstack_handle_and_restore;
737 #endif
738 UCONTEXT_REG_Rn(uc, 1) = (unsigned long)sp;
739 UCONTEXT_REG_Rn(uc, PPC_FIRST_ARG_REG) = (unsigned long)(sp + 16);
740 UCONTEXT_REG_Rn(uc, PPC_FIRST_ARG_REG + 1) = 0;
741 UCONTEXT_REG_Rn(uc, PPC_FIRST_ARG_REG + 2) = 0;
742 #endif
744 #endif /* !MONO_CROSS_COMPILE */
748 * handle_exception:
750 * Called by resuming from a signal handler.
752 static void
753 handle_signal_exception (gpointer obj, gboolean test_only)
755 MonoJitTlsData *jit_tls = TlsGetValue (mono_jit_tls_id);
756 MonoContext ctx;
757 static void (*restore_context) (MonoContext *);
759 if (!restore_context)
760 restore_context = mono_get_restore_context ();
762 memcpy (&ctx, &jit_tls->ex_ctx, sizeof (MonoContext));
764 mono_handle_exception (&ctx, obj, MONO_CONTEXT_GET_IP (&ctx), test_only);
766 restore_context (&ctx);
769 static void
770 setup_ucontext_return (void *uc, gpointer func)
772 #if !defined(MONO_CROSS_COMPILE)
773 UCONTEXT_REG_LNK(uc) = UCONTEXT_REG_NIP(uc);
774 #ifdef PPC_USES_FUNCTION_DESCRIPTOR
776 MonoPPCFunctionDescriptor *handler_ftnptr = (MonoPPCFunctionDescriptor*)func;
778 UCONTEXT_REG_NIP(uc) = (gulong)handler_ftnptr->code;
779 UCONTEXT_REG_Rn(uc, 2) = (gulong)handler_ftnptr->toc;
781 #else
782 UCONTEXT_REG_NIP(uc) = (unsigned long)func;
783 #endif
784 #endif
787 gboolean
788 mono_arch_handle_exception (void *ctx, gpointer obj, gboolean test_only)
790 #if defined(MONO_ARCH_USE_SIGACTION) && defined(UCONTEXT_REG_Rn)
792 * Handling the exception in the signal handler is problematic, since the original
793 * signal is disabled, and we could run arbitrary code though the debugger. So
794 * resume into the normal stack and do most work there if possible.
796 MonoJitTlsData *jit_tls = TlsGetValue (mono_jit_tls_id);
797 mgreg_t sp;
798 void *sigctx = ctx;
799 int frame_size;
800 void *uc = sigctx;
802 /* Pass the ctx parameter in TLS */
803 mono_arch_sigctx_to_monoctx (sigctx, &jit_tls->ex_ctx);
804 /* The others in registers */
805 UCONTEXT_REG_Rn (sigctx, PPC_FIRST_ARG_REG) = (gsize)obj;
806 UCONTEXT_REG_Rn (sigctx, PPC_FIRST_ARG_REG + 1) = test_only;
808 /* Allocate a stack frame below the red zone */
809 /* Similar to mono_arch_handle_altstack_exception () */
810 frame_size = 224;
811 frame_size += 15;
812 frame_size &= ~15;
813 sp = (mgreg_t)(UCONTEXT_REG_Rn(uc, 1) & ~15);
814 sp = (mgreg_t)(sp - frame_size);
815 UCONTEXT_REG_Rn(uc, 1) = (mgreg_t)sp;
816 setup_ucontext_return (uc, handle_signal_exception);
818 return TRUE;
819 #else
820 MonoContext mctx;
821 gboolean result;
823 mono_arch_sigctx_to_monoctx (ctx, &mctx);
825 result = mono_handle_exception (&mctx, obj, (gpointer)mctx.sc_ir, test_only);
826 /* restore the context so that returning from the signal handler will invoke
827 * the catch clause
829 mono_arch_monoctx_to_sigctx (&mctx, ctx);
830 return result;
831 #endif