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>
30 #include <mono/utils/mono-state.h>
34 #include "mini-runtime.h"
35 #include "aot-runtime.h"
36 #include "mono/utils/mono-tls-inline.h"
41 int sc_onstack; // sigstack state to restore
42 int sc_mask; // signal mask to restore
44 int sc_psw; // processor status word
45 int sc_sp; // stack pointer if sc_regs == NULL
46 void *sc_regs; // (kernel private) saved state
51 sigset_t uc_sigmask; // signal mask used by this context
52 stack_t uc_stack; // stack used by this context
53 struct ucontext *uc_link; // pointer to resuming context
54 size_t uc_mcsize; // size of the machine context passed in
55 mcontext_t uc_mcontext; // machine specific context
58 typedef struct ppc_exception_state {
59 unsigned long dar; // Fault registers for coredump
61 unsigned long exception;// number of powerpc exception taken
62 unsigned long pad0; // align to 16 bytes
64 unsigned long pad1[4]; // space in PCB "just in case"
65 } ppc_exception_state_t;
67 typedef struct ppc_vector_state {
68 unsigned long save_vr[32][4];
69 unsigned long save_vscr[4];
70 unsigned int save_pad5[4];
71 unsigned int save_vrvalid; // VRs that have been saved
72 unsigned int save_pad6[7];
75 typedef struct ppc_float_state {
78 unsigned int fpscr_pad; // fpscr is 64 bits, 32 bits of rubbish
79 unsigned int fpscr; // floating point status register
82 typedef struct ppc_thread_state {
83 unsigned int srr0; // Instruction address register (PC)
84 unsigned int srr1; // Machine state register (supervisor)
90 unsigned int cr; // Condition register
91 unsigned int xer; // User's integer exception register
92 unsigned int lr; // Link register
93 unsigned int ctr; // Count register
94 unsigned int mq; // MQ register (601 only)
96 unsigned int vrsave; // Vector Save Register
100 ppc_exception_state_t es;
101 ppc_thread_state_t ss;
102 ppc_float_state_t fs;
103 ppc_vector_state_t vs;
106 typedef struct mcontext * mcontext_t;
108 Linux/PPC instead has:
110 unsigned long _unused[4];
112 unsigned long handler;
113 unsigned long oldmask;
114 struct pt_regs *regs;
117 unsigned long gpr[32];
120 unsigned long orig_gpr3; // Used for restarting system calls
125 unsigned long mq; // 601 only (not used at present)
126 // Used on APUS to hold IPL value.
127 unsigned long trap; // Reason for being here
128 // N.B. for critical exceptions on 4xx, the dar and dsisr
129 // fields are overloaded to hold srr0 and srr1.
130 unsigned long dar; // Fault registers
131 unsigned long dsisr; // on 4xx/Book-E used for ESR
132 unsigned long result; // Result of a system call
135 elf_gregset_t mc_gregs;
136 elf_fpregset_t mc_fregs;
137 unsigned long mc_pad[2];
138 elf_vrregset_t mc_vregs __attribute__((__aligned__(16)));
142 unsigned long uc_flags;
143 struct ucontext *uc_link;
146 struct mcontext *uc_regs; // points to uc_mcontext field
148 // glibc has 1024-bit signal masks, ours are 64-bit
151 struct mcontext uc_mcontext;
154 #define ELF_NGREG 48 // includes nip, msr, lr, etc.
155 #define ELF_NFPREG 33 // includes fpscr
158 typedef unsigned long elf_greg_t;
159 typedef elf_greg_t elf_gregset_t[ELF_NGREG];
161 // Floating point registers
162 typedef double elf_fpreg_t;
163 typedef elf_fpreg_t elf_fpregset_t[ELF_NFPREG];
169 #define restore_regs_from_context(ctx_reg,ip_reg,tmp_reg) do { \
171 ppc_ldptr (code, ip_reg, G_STRUCT_OFFSET (MonoContext, sc_ir), ctx_reg); \
172 ppc_load_multiple_regs (code, MONO_PPC_FIRST_SAVED_GREG, \
173 G_STRUCT_OFFSET (MonoContext, regs) + MONO_PPC_FIRST_SAVED_GREG * sizeof (target_mgreg_t), ctx_reg); \
174 for (reg = MONO_PPC_FIRST_SAVED_FREG; reg < MONO_MAX_FREGS; ++reg) { \
175 ppc_lfd (code, reg, \
176 G_STRUCT_OFFSET(MonoContext, fregs) + reg * sizeof (gdouble), ctx_reg); \
181 #define setup_context(ctx)
183 #ifdef PPC_USES_FUNCTION_DESCRIPTOR
185 mono_ppc_create_pre_code_ftnptr (guint8
*code
)
187 MonoPPCFunctionDescriptor
*ftnptr
= (MonoPPCFunctionDescriptor
*)code
;
189 code
+= sizeof (MonoPPCFunctionDescriptor
);
199 * arch_get_restore_context:
201 * Returns a pointer to a method which restores a previously saved sigcontext.
202 * The first argument in r3 is the pointer to the context.
205 mono_arch_get_restore_context (MonoTrampInfo
**info
, gboolean aot
)
207 guint8
*start
, *code
;
208 int size
= MONO_PPC_32_64_CASE (128, 172) + PPC_FTNPTR_SIZE
;
209 MonoJumpInfo
*ji
= NULL
;
210 GSList
*unwind_ops
= NULL
;
212 code
= start
= mono_global_codeman_reserve (size
);
214 code
= mono_ppc_create_pre_code_ftnptr (code
);
215 restore_regs_from_context (ppc_r3
, ppc_r4
, ppc_r5
);
216 /* restore also the stack pointer */
217 ppc_ldptr (code
, ppc_sp
, G_STRUCT_OFFSET (MonoContext
, sc_sp
), ppc_r3
);
219 /* jump to the saved IP */
220 ppc_mtctr (code
, ppc_r4
);
221 ppc_bcctr (code
, PPC_BR_ALWAYS
, 0);
225 g_assert ((code
- start
) <= size
);
226 mono_arch_flush_icache (start
, code
- start
);
227 MONO_PROFILER_RAISE (jit_code_buffer
, (start
, code
- start
, MONO_PROFILER_CODE_BUFFER_EXCEPTION_HANDLING
, NULL
));
230 *info
= mono_tramp_info_create ("restore_context", start
, code
- start
, ji
, unwind_ops
);
235 #define SAVED_REGS_LENGTH (sizeof (gdouble) * MONO_MAX_FREGS + sizeof (target_mgreg_t) * MONO_MAX_IREGS)
236 #define ALIGN_STACK_FRAME_SIZE(s) (((s) + MONO_ARCH_FRAME_ALIGNMENT - 1) & ~(MONO_ARCH_FRAME_ALIGNMENT - 1))
237 /* The 64 bytes here are for outgoing arguments and a bit of spare.
238 We don't use it all, but it doesn't hurt. */
239 #define REG_SAVE_STACK_FRAME_SIZE (ALIGN_STACK_FRAME_SIZE (SAVED_REGS_LENGTH + PPC_MINIMAL_STACK_SIZE + 64))
242 emit_save_saved_regs (guint8
*code
, int pos
)
246 for (i
= MONO_MAX_FREGS
- 1; i
>= MONO_PPC_FIRST_SAVED_FREG
; --i
) {
247 pos
-= sizeof (gdouble
);
248 ppc_stfd (code
, i
, pos
, ppc_sp
);
250 pos
-= (MONO_MAX_FREGS
- MONO_SAVED_FREGS
) * sizeof (gdouble
);
251 pos
-= sizeof (target_mgreg_t
) * MONO_SAVED_GREGS
;
252 ppc_store_multiple_regs (code
, MONO_PPC_FIRST_SAVED_GREG
, pos
, ppc_sp
);
258 * mono_arch_get_call_filter:
260 * Returns a pointer to a method which calls an exception filter. We
261 * also use this function to call finally handlers (we pass NULL as
262 * @exc object in this case).
265 mono_arch_get_call_filter (MonoTrampInfo
**info
, gboolean aot
)
267 guint8
*start
, *code
;
268 int alloc_size
, pos
, i
;
269 int size
= MONO_PPC_32_64_CASE (320, 500) + PPC_FTNPTR_SIZE
;
270 MonoJumpInfo
*ji
= NULL
;
271 GSList
*unwind_ops
= NULL
;
273 /* call_filter (MonoContext *ctx, unsigned long eip, gpointer exc) */
274 code
= start
= mono_global_codeman_reserve (size
);
276 code
= mono_ppc_create_pre_code_ftnptr (code
);
279 ppc_mflr (code
, ppc_r0
);
280 ppc_stptr (code
, ppc_r0
, PPC_RET_ADDR_OFFSET
, ppc_sp
);
282 alloc_size
= REG_SAVE_STACK_FRAME_SIZE
;
284 /* allocate stack frame and set link from sp in ctx */
285 g_assert ((alloc_size
& (MONO_ARCH_FRAME_ALIGNMENT
-1)) == 0);
286 ppc_ldptr (code
, ppc_r0
, G_STRUCT_OFFSET (MonoContext
, sc_sp
), ppc_r3
);
287 ppc_ldptr_indexed (code
, ppc_r0
, ppc_r0
, ppc_r0
);
288 ppc_stptr_update (code
, ppc_r0
, -alloc_size
, ppc_sp
);
290 code
= emit_save_saved_regs (code
, alloc_size
);
292 /* restore all the regs from ctx (in r3), but not r1, the stack pointer */
293 restore_regs_from_context (ppc_r3
, ppc_r6
, ppc_r7
);
294 /* call handler at eip (r4) and set the first arg with the exception (r5) */
295 ppc_mtctr (code
, ppc_r4
);
296 ppc_mr (code
, ppc_r3
, ppc_r5
);
297 ppc_bcctrl (code
, PPC_BR_ALWAYS
, 0);
300 ppc_ldptr (code
, ppc_r0
, alloc_size
+ PPC_RET_ADDR_OFFSET
, ppc_sp
);
301 ppc_mtlr (code
, ppc_r0
);
303 /* restore all the regs from the stack */
305 for (i
= MONO_MAX_FREGS
- 1; i
>= MONO_PPC_FIRST_SAVED_FREG
; --i
) {
306 pos
-= sizeof (gdouble
);
307 ppc_lfd (code
, i
, pos
, ppc_sp
);
309 pos
-= (MONO_MAX_FREGS
- MONO_SAVED_FREGS
) * sizeof (gdouble
);
310 pos
-= sizeof (target_mgreg_t
) * MONO_SAVED_GREGS
;
311 ppc_load_multiple_regs (code
, MONO_PPC_FIRST_SAVED_GREG
, pos
, ppc_sp
);
313 ppc_addic (code
, ppc_sp
, ppc_sp
, alloc_size
);
316 g_assert ((code
- start
) < size
);
317 mono_arch_flush_icache (start
, code
- start
);
318 MONO_PROFILER_RAISE (jit_code_buffer
, (start
, code
- start
, MONO_PROFILER_CODE_BUFFER_EXCEPTION_HANDLING
, NULL
));
321 *info
= mono_tramp_info_create ("call_filter", start
, code
- start
, ji
, unwind_ops
);
327 mono_ppc_throw_exception (MonoObject
*exc
, unsigned long eip
, unsigned long esp
, host_mgreg_t
*int_regs
, gdouble
*fp_regs
, gboolean rethrow
, gboolean preserve_ips
)
332 /* adjust eip so that it point into the call instruction */
335 setup_context (&ctx
);
337 /*printf ("stack in throw: %p\n", esp);*/
338 MONO_CONTEXT_SET_BP (&ctx
, esp
);
339 MONO_CONTEXT_SET_IP (&ctx
, eip
);
340 memcpy (&ctx
.regs
, int_regs
, sizeof (host_mgreg_t
) * MONO_MAX_IREGS
);
341 memcpy (&ctx
.fregs
, fp_regs
, sizeof (double) * MONO_MAX_FREGS
);
343 if (mono_object_isinst_checked (exc
, mono_defaults
.exception_class
, error
)) {
344 MonoException
*mono_ex
= (MonoException
*)exc
;
345 if (!rethrow
&& !mono_ex
->caught_in_unmanaged
) {
346 mono_ex
->stack_trace
= NULL
;
347 mono_ex
->trace_ips
= NULL
;
348 } else if (preserve_ips
) {
349 mono_ex
->caught_in_unmanaged
= TRUE
;
352 mono_error_assert_ok (error
);
353 mono_handle_exception (&ctx
, exc
);
354 mono_restore_context (&ctx
);
356 g_assert_not_reached ();
360 * arch_get_throw_exception_generic:
362 * Returns a function pointer which can be used to raise
363 * exceptions. The returned function has the following
364 * signature: void (*func) (MonoException *exc); or
365 * void (*func) (guint32 ex_token, gpointer ip)
369 mono_arch_get_throw_exception_generic (int size
, MonoTrampInfo
**info
, int corlib
, gboolean rethrow
, gboolean aot
, gboolean preserve_ips
)
371 guint8
*start
, *code
;
373 MonoJumpInfo
*ji
= NULL
;
374 GSList
*unwind_ops
= NULL
;
376 code
= start
= mono_global_codeman_reserve (size
);
378 code
= mono_ppc_create_pre_code_ftnptr (code
);
382 ppc_mr (code
, ppc_r0
, ppc_r4
);
384 ppc_mflr (code
, ppc_r0
);
385 ppc_stptr (code
, ppc_r0
, PPC_RET_ADDR_OFFSET
, ppc_sp
);
387 alloc_size
= REG_SAVE_STACK_FRAME_SIZE
;
389 g_assert ((alloc_size
& (MONO_ARCH_FRAME_ALIGNMENT
-1)) == 0);
390 ppc_stptr_update (code
, ppc_sp
, -alloc_size
, ppc_sp
);
392 code
= emit_save_saved_regs (code
, alloc_size
);
396 ppc_mr (code
, ppc_r4
, ppc_r3
);
399 code
= mono_arch_emit_load_aotconst (start
, code
, &ji
, MONO_PATCH_INFO_IMAGE
, mono_defaults
.corlib
);
400 ppc_mr (code
, ppc_r3
, ppc_r12
);
401 code
= mono_arch_emit_load_aotconst (start
, code
, &ji
, MONO_PATCH_INFO_JIT_ICALL_ADDR
, GUINT_TO_POINTER (MONO_JIT_ICALL_mono_exception_from_token
));
402 #ifdef PPC_USES_FUNCTION_DESCRIPTOR
403 ppc_ldptr (code
, ppc_r2
, sizeof (target_mgreg_t
), ppc_r12
);
404 ppc_ldptr (code
, ppc_r12
, 0, ppc_r12
);
406 ppc_mtctr (code
, ppc_r12
);
407 ppc_bcctrl (code
, PPC_BR_ALWAYS
, 0);
409 ppc_load (code
, ppc_r3
, (gulong
)mono_defaults
.corlib
);
410 ppc_load_func (code
, PPC_CALL_REG
, mono_exception_from_token
);
411 ppc_mtctr (code
, PPC_CALL_REG
);
412 ppc_bcctrl (code
, PPC_BR_ALWAYS
, 0);
416 /* call throw_exception (exc, ip, sp, int_regs, fp_regs) */
418 ppc_ldptr (code
, ppc_r5
, 0, ppc_sp
);
419 /* exc is already in place in r3 */
421 ppc_ldptr (code
, ppc_r4
, PPC_RET_ADDR_OFFSET
, ppc_r5
);
423 ppc_mr (code
, ppc_r4
, ppc_r0
); /* caller ip */
424 /* pointer to the saved fp regs */
425 pos
= alloc_size
- sizeof (gdouble
) * MONO_MAX_FREGS
;
426 ppc_addi (code
, ppc_r7
, ppc_sp
, pos
);
427 /* pointer to the saved int regs */
428 pos
-= sizeof (target_mgreg_t
) * MONO_MAX_IREGS
;
429 ppc_addi (code
, ppc_r6
, ppc_sp
, pos
);
430 ppc_li (code
, ppc_r8
, rethrow
);
431 ppc_li (code
, ppc_r9
, preserve_ips
);
434 // This can be called from runtime code, which can't guarantee that
435 // r30 contains the got address.
436 // So emit the got address loading code too
437 code
= mono_arch_emit_load_got_addr (start
, code
, NULL
, &ji
);
438 code
= mono_arch_emit_load_aotconst (start
, code
, &ji
, MONO_PATCH_INFO_JIT_ICALL_ADDR
, GUINT_TO_POINTER (MONO_JIT_ICALL_mono_ppc_throw_exception
));
439 #ifdef PPC_USES_FUNCTION_DESCRIPTOR
440 ppc_ldptr (code
, ppc_r2
, sizeof (target_mgreg_t
), ppc_r12
);
441 ppc_ldptr (code
, ppc_r12
, 0, ppc_r12
);
443 ppc_mtctr (code
, ppc_r12
);
444 ppc_bcctrl (code
, PPC_BR_ALWAYS
, 0);
446 ppc_load_func (code
, PPC_CALL_REG
, mono_ppc_throw_exception
);
447 ppc_mtctr (code
, PPC_CALL_REG
);
448 ppc_bcctrl (code
, PPC_BR_ALWAYS
, 0);
450 /* we should never reach this breakpoint */
452 g_assert ((code
- start
) <= size
);
453 mono_arch_flush_icache (start
, code
- start
);
454 MONO_PROFILER_RAISE (jit_code_buffer
, (start
, code
- start
, MONO_PROFILER_CODE_BUFFER_EXCEPTION_HANDLING
, NULL
));
457 *info
= mono_tramp_info_create (corlib
? "throw_corlib_exception" : (preserve_ips
? "rethrow_preserve_exception" : (rethrow
? "rethrow_exception" : "throw_exception")), start
, code
- start
, ji
, unwind_ops
);
463 * mono_arch_get_rethrow_preserve_exception:
464 * \returns a function pointer which can be used to rethrow
465 * exceptions and completely preserve trace_ips.
466 * The returned function has the following
467 * signature: void (*func) (MonoException *exc);
470 mono_arch_get_rethrow_preserve_exception (MonoTrampInfo
**info
, gboolean aot
)
472 int size
= MONO_PPC_32_64_CASE (132, 224) + PPC_FTNPTR_SIZE
;
476 return mono_arch_get_throw_exception_generic (size
, info
, FALSE
, TRUE
, aot
, TRUE
);
480 * mono_arch_get_rethrow_exception:
481 * \returns a function pointer which can be used to rethrow
482 * exceptions. The returned function has the following
483 * signature: void (*func) (MonoException *exc);
486 mono_arch_get_rethrow_exception (MonoTrampInfo
**info
, gboolean aot
)
488 int size
= MONO_PPC_32_64_CASE (132, 224) + PPC_FTNPTR_SIZE
;
492 return mono_arch_get_throw_exception_generic (size
, info
, FALSE
, TRUE
, aot
, FALSE
);
496 * arch_get_throw_exception:
498 * Returns a function pointer which can be used to raise
499 * exceptions. The returned function has the following
500 * signature: void (*func) (MonoException *exc);
501 * For example to raise an arithmetic exception you can use:
503 * x86_push_imm (code, mono_get_exception_arithmetic ());
504 * x86_call_code (code, arch_get_throw_exception ());
508 mono_arch_get_throw_exception (MonoTrampInfo
**info
, gboolean aot
)
510 int size
= MONO_PPC_32_64_CASE (132, 224) + PPC_FTNPTR_SIZE
;
514 return mono_arch_get_throw_exception_generic (size
, info
, FALSE
, FALSE
, aot
, FALSE
);
518 * mono_arch_get_throw_corlib_exception:
519 * \returns a function pointer which can be used to raise
520 * corlib exceptions. The returned function has the following
521 * signature: void (*func) (guint32 ex_token, guint32 offset);
522 * On PPC, we pass the ip instead of the offset
525 mono_arch_get_throw_corlib_exception (MonoTrampInfo
**info
, gboolean aot
)
527 int size
= MONO_PPC_32_64_CASE (168, 304) + PPC_FTNPTR_SIZE
;
531 return mono_arch_get_throw_exception_generic (size
, info
, TRUE
, FALSE
, aot
, FALSE
);
535 * mono_arch_unwind_frame:
537 * See exceptions-amd64.c for docs.
540 mono_arch_unwind_frame (MonoDomain
*domain
, MonoJitTlsData
*jit_tls
,
541 MonoJitInfo
*ji
, MonoContext
*ctx
,
542 MonoContext
*new_ctx
, MonoLMF
**lmf
,
543 host_mgreg_t
**save_locations
,
544 StackFrameInfo
*frame
)
546 gpointer ip
= MONO_CONTEXT_GET_IP (ctx
);
547 MonoPPCStackFrame
*sframe
;
549 memset (frame
, 0, sizeof (StackFrameInfo
));
553 setup_context (new_ctx
);
557 host_mgreg_t regs
[ppc_lr
+ 1];
559 guint32 unwind_info_len
;
562 if (ji
->is_trampoline
)
563 frame
->type
= FRAME_TYPE_TRAMPOLINE
;
565 frame
->type
= FRAME_TYPE_MANAGED
;
567 unwind_info
= mono_jinfo_get_unwind_info (ji
, &unwind_info_len
);
569 sframe
= (MonoPPCStackFrame
*)MONO_CONTEXT_GET_SP (ctx
);
570 MONO_CONTEXT_SET_BP (new_ctx
, sframe
->sp
);
571 if (!ji
->is_trampoline
&& jinfo_get_method (ji
)->save_lmf
) {
572 /* sframe->sp points just past the end of the LMF */
573 guint8
*lmf_addr
= (guint8
*)sframe
->sp
- sizeof (MonoLMF
);
574 memcpy (&new_ctx
->fregs
[MONO_PPC_FIRST_SAVED_FREG
], lmf_addr
+ G_STRUCT_OFFSET (MonoLMF
, fregs
), sizeof (double) * MONO_SAVED_FREGS
);
575 memcpy (&new_ctx
->regs
[MONO_PPC_FIRST_SAVED_GREG
], lmf_addr
+ G_STRUCT_OFFSET (MonoLMF
, iregs
), sizeof (host_mgreg_t
) * MONO_SAVED_GREGS
);
576 /* the calling IP is in the parent frame */
577 sframe
= (MonoPPCStackFrame
*)sframe
->sp
;
578 /* we substract 4, so that the IP points into the call instruction */
579 MONO_CONTEXT_SET_IP (new_ctx
, sframe
->lr
- 4);
581 regs
[ppc_lr
] = ctx
->sc_ir
;
582 regs
[ppc_sp
] = ctx
->sc_sp
;
583 for (i
= MONO_PPC_FIRST_SAVED_GREG
; i
< MONO_MAX_IREGS
; ++i
)
584 regs
[i
] = ctx
->regs
[i
];
586 gboolean success
= mono_unwind_frame (unwind_info
, unwind_info_len
, (guint8
*)ji
->code_start
,
587 (guint8
*)ji
->code_start
+ ji
->code_size
,
588 (guint8
*)ip
, NULL
, regs
, ppc_lr
+ 1,
589 save_locations
, MONO_MAX_IREGS
, &cfa
);
594 /* we substract 4, so that the IP points into the call instruction */
595 MONO_CONTEXT_SET_IP (new_ctx
, regs
[ppc_lr
] - 4);
596 MONO_CONTEXT_SET_BP (new_ctx
, cfa
);
598 for (i
= MONO_PPC_FIRST_SAVED_GREG
; i
< MONO_MAX_IREGS
; ++i
)
599 new_ctx
->regs
[i
] = regs
[i
];
605 if ((ji
= mini_jit_info_table_find (domain
, (gpointer
)(*lmf
)->eip
, NULL
))) {
610 /* Trampoline lmf frame */
611 frame
->method
= (*lmf
)->method
;
614 /*sframe = (MonoPPCStackFrame*)MONO_CONTEXT_GET_SP (ctx);
615 MONO_CONTEXT_SET_BP (new_ctx, sframe->sp);
616 MONO_CONTEXT_SET_IP (new_ctx, sframe->lr);*/
617 MONO_CONTEXT_SET_BP (new_ctx
, (*lmf
)->ebp
);
618 MONO_CONTEXT_SET_IP (new_ctx
, (*lmf
)->eip
);
619 memcpy (&new_ctx
->regs
[MONO_PPC_FIRST_SAVED_GREG
], (*lmf
)->iregs
, sizeof (host_mgreg_t
) * MONO_SAVED_GREGS
);
620 memcpy (&new_ctx
->fregs
[MONO_PPC_FIRST_SAVED_FREG
], (*lmf
)->fregs
, sizeof (double) * MONO_SAVED_FREGS
);
623 frame
->type
= FRAME_TYPE_MANAGED_TO_NATIVE
;
625 /* FIXME: what about trampoline LMF frames? see exceptions-x86.c */
627 *lmf
= (MonoLMF
*)(*lmf
)->previous_lmf
;
636 mono_arch_ip_from_context (void *sigctx
)
638 #ifdef MONO_CROSS_COMPILE
639 g_assert_not_reached ();
641 os_ucontext
*uc
= (os_ucontext
*)sigctx
;
642 return (gpointer
)UCONTEXT_REG_NIP(uc
);
647 altstack_handle_and_restore (MonoContext
*mctx
, gpointer obj
)
649 mono_handle_exception (mctx
, obj
);
650 mono_restore_context (mctx
);
654 mono_arch_handle_altstack_exception (void *sigctx
, MONO_SIG_HANDLER_INFO_TYPE
*siginfo
, gpointer fault_addr
, gboolean stack_ovf
)
656 #ifdef MONO_CROSS_COMPILE
657 g_assert_not_reached ();
659 #ifdef MONO_ARCH_USE_SIGACTION
660 os_ucontext
*uc
= (os_ucontext
*)sigctx
;
661 MonoContext
*uc_copy
;
662 MonoJitInfo
*ji
= mini_jit_info_table_find (mono_domain_get (), mono_arch_ip_from_context (sigctx
), NULL
);
668 /* we don't do much now, but we can warn the user with a useful message */
669 fprintf (stderr
, "Stack overflow: IP: %p, SP: %p\n", mono_arch_ip_from_context (sigctx
), (gpointer
)UCONTEXT_REG_Rn(uc
, 1));
670 if (ji
&& !ji
->is_trampoline
&& jinfo_get_method (ji
))
671 method
= mono_method_full_name (jinfo_get_method (ji
), TRUE
);
673 method
= "Unmanaged";
674 fprintf (stderr
, "At %s\n", method
);
678 if (mono_dump_start ())
679 mono_handle_native_crash ("SIGSEGV", (MonoContext
*)sigctx
, siginfo
);
680 /* setup a call frame on the real stack so that control is returned there
681 * and exception handling can continue.
682 * The frame looks like:
685 * 224 is the size of the red zone
687 frame_size
= sizeof (MonoContext
) + sizeof (gpointer
) * 16 + 224;
690 sp
= (void**)(UCONTEXT_REG_Rn(uc
, 1) & ~15);
691 sp
= (void**)((char*)sp
- frame_size
);
692 /* may need to adjust pointers in the new struct copy, depending on the OS */
693 uc_copy
= (MonoContext
*)(sp
+ 16);
694 mono_sigctx_to_monoctx (uc
, uc_copy
);
695 #if defined(__linux__) && !defined(__mono_ppc64__)
696 uc_copy
->uc_mcontext
.uc_regs
= (gpointer
)((char*)uc_copy
+ ((char*)uc
->uc_mcontext
.uc_regs
- (char*)uc
));
698 g_assert (mono_arch_ip_from_context (uc
) == mono_arch_ip_from_context (uc_copy
));
699 /* at the return form the signal handler execution starts in altstack_handle_and_restore() */
700 UCONTEXT_REG_LNK(uc
) = UCONTEXT_REG_NIP(uc
);
701 #ifdef PPC_USES_FUNCTION_DESCRIPTOR
703 MonoPPCFunctionDescriptor
*handler_ftnptr
= (MonoPPCFunctionDescriptor
*)altstack_handle_and_restore
;
705 UCONTEXT_REG_NIP(uc
) = (gulong
)handler_ftnptr
->code
;
706 UCONTEXT_REG_Rn(uc
, 2) = (gulong
)handler_ftnptr
->toc
;
709 UCONTEXT_REG_NIP(uc
) = (unsigned long)altstack_handle_and_restore
;
711 /* ELF v2 ABI calling convention requires to put the target address into
712 * r12 if we use the global entry point of a function. */
713 UCONTEXT_REG_Rn(uc
, 12) = (unsigned long) altstack_handle_and_restore
;
716 UCONTEXT_REG_Rn(uc
, 1) = (unsigned long)sp
;
717 UCONTEXT_REG_Rn(uc
, PPC_FIRST_ARG_REG
) = (unsigned long)uc_copy
;
718 UCONTEXT_REG_Rn(uc
, PPC_FIRST_ARG_REG
+ 1) = 0;
719 UCONTEXT_REG_Rn(uc
, PPC_FIRST_ARG_REG
+ 2) = 0;
722 #endif /* !MONO_CROSS_COMPILE */
728 * Called by resuming from a signal handler.
731 handle_signal_exception (gpointer obj
)
733 MonoJitTlsData
*jit_tls
= mono_tls_get_jit_tls ();
736 memcpy (&ctx
, &jit_tls
->ex_ctx
, sizeof (MonoContext
));
738 mono_handle_exception (&ctx
, obj
);
740 mono_restore_context (&ctx
);
744 setup_ucontext_return (void *uc
, gpointer func
)
746 #if !defined(MONO_CROSS_COMPILE)
747 UCONTEXT_REG_LNK(uc
) = UCONTEXT_REG_NIP(uc
);
748 #ifdef PPC_USES_FUNCTION_DESCRIPTOR
750 MonoPPCFunctionDescriptor
*handler_ftnptr
= (MonoPPCFunctionDescriptor
*)func
;
752 UCONTEXT_REG_NIP(uc
) = (gulong
)handler_ftnptr
->code
;
753 UCONTEXT_REG_Rn(uc
, 2) = (gulong
)handler_ftnptr
->toc
;
756 UCONTEXT_REG_NIP(uc
) = (unsigned long)func
;
758 /* ELF v2 ABI calling convention requires to put the target address into
759 * r12 if we use the global entry point of a function. */
760 UCONTEXT_REG_Rn(uc
, 12) = (unsigned long) func
;
767 mono_arch_handle_exception (void *ctx
, gpointer obj
)
769 #if defined(MONO_ARCH_USE_SIGACTION) && defined(UCONTEXT_REG_Rn)
771 * Handling the exception in the signal handler is problematic, since the original
772 * signal is disabled, and we could run arbitrary code though the debugger. So
773 * resume into the normal stack and do most work there if possible.
775 MonoJitTlsData
*jit_tls
= mono_tls_get_jit_tls ();
781 /* Pass the ctx parameter in TLS */
782 mono_sigctx_to_monoctx (sigctx
, &jit_tls
->ex_ctx
);
783 /* The others in registers */
784 UCONTEXT_REG_Rn (sigctx
, PPC_FIRST_ARG_REG
) = (gsize
)obj
;
786 /* Allocate a stack frame below the red zone */
787 /* Similar to mono_arch_handle_altstack_exception () */
791 sp
= (host_mgreg_t
)(UCONTEXT_REG_Rn(uc
, 1) & ~15);
792 sp
= (host_mgreg_t
)(sp
- frame_size
);
793 UCONTEXT_REG_Rn(uc
, 1) = (host_mgreg_t
)sp
;
794 setup_ucontext_return (uc
, (gpointer
)handle_signal_exception
);
801 mono_sigctx_to_monoctx (ctx
, &mctx
);
803 result
= mono_handle_exception (&mctx
, obj
);
804 /* restore the context so that returning from the signal handler will invoke
807 mono_monoctx_to_sigctx (&mctx
, ctx
);
813 // FIX ME: This is not complete
815 mono_arch_setup_async_callback (MonoContext
*ctx
, void (*async_cb
)(void *fun
), gpointer user_data
)
817 uintptr_t sp
= (uintptr_t) MONO_CONTEXT_GET_SP(ctx
);
818 ctx
->regs
[PPC_FIRST_ARG_REG
] = (host_mgreg_t
)user_data
;
819 sp
-= PPC_MINIMAL_STACK_SIZE
;
820 *(unsigned long *)sp
= (uintptr_t) MONO_CONTEXT_GET_SP(ctx
);
821 MONO_CONTEXT_SET_BP(ctx
, sp
);
822 mono_arch_setup_resume_sighandler_ctx(ctx
, (gpointer
) async_cb
);
826 mono_arch_setup_resume_sighandler_ctx (MonoContext
*ctx
, gpointer func
)
828 #ifdef PPC_USES_FUNCTION_DESCRIPTOR
829 MonoPPCFunctionDescriptor
*handler_ftnptr
= (MonoPPCFunctionDescriptor
*)func
;
830 MONO_CONTEXT_SET_IP(ctx
, (gulong
)handler_ftnptr
->code
);
831 ctx
->regs
[2] = (gulong
)handler_ftnptr
->toc
;
833 MONO_CONTEXT_SET_IP(ctx
, (unsigned long) func
);