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>
33 #include "mini-runtime.h"
34 #include "aot-runtime.h"
39 int sc_onstack; // sigstack state to restore
40 int sc_mask; // signal mask to restore
42 int sc_psw; // processor status word
43 int sc_sp; // stack pointer if sc_regs == NULL
44 void *sc_regs; // (kernel private) saved state
49 sigset_t uc_sigmask; // signal mask used by this context
50 stack_t uc_stack; // stack used by this context
51 struct ucontext *uc_link; // pointer to resuming context
52 size_t uc_mcsize; // size of the machine context passed in
53 mcontext_t uc_mcontext; // machine specific context
56 typedef struct ppc_exception_state {
57 unsigned long dar; // Fault registers for coredump
59 unsigned long exception;// number of powerpc exception taken
60 unsigned long pad0; // align to 16 bytes
62 unsigned long pad1[4]; // space in PCB "just in case"
63 } ppc_exception_state_t;
65 typedef struct ppc_vector_state {
66 unsigned long save_vr[32][4];
67 unsigned long save_vscr[4];
68 unsigned int save_pad5[4];
69 unsigned int save_vrvalid; // VRs that have been saved
70 unsigned int save_pad6[7];
73 typedef struct ppc_float_state {
76 unsigned int fpscr_pad; // fpscr is 64 bits, 32 bits of rubbish
77 unsigned int fpscr; // floating point status register
80 typedef struct ppc_thread_state {
81 unsigned int srr0; // Instruction address register (PC)
82 unsigned int srr1; // Machine state register (supervisor)
88 unsigned int cr; // Condition register
89 unsigned int xer; // User's integer exception register
90 unsigned int lr; // Link register
91 unsigned int ctr; // Count register
92 unsigned int mq; // MQ register (601 only)
94 unsigned int vrsave; // Vector Save Register
98 ppc_exception_state_t es;
99 ppc_thread_state_t ss;
100 ppc_float_state_t fs;
101 ppc_vector_state_t vs;
104 typedef struct mcontext * mcontext_t;
106 Linux/PPC instead has:
108 unsigned long _unused[4];
110 unsigned long handler;
111 unsigned long oldmask;
112 struct pt_regs *regs;
115 unsigned long gpr[32];
118 unsigned long orig_gpr3; // Used for restarting system calls
123 unsigned long mq; // 601 only (not used at present)
124 // Used on APUS to hold IPL value.
125 unsigned long trap; // Reason for being here
126 // N.B. for critical exceptions on 4xx, the dar and dsisr
127 // fields are overloaded to hold srr0 and srr1.
128 unsigned long dar; // Fault registers
129 unsigned long dsisr; // on 4xx/Book-E used for ESR
130 unsigned long result; // Result of a system call
133 elf_gregset_t mc_gregs;
134 elf_fpregset_t mc_fregs;
135 unsigned long mc_pad[2];
136 elf_vrregset_t mc_vregs __attribute__((__aligned__(16)));
140 unsigned long uc_flags;
141 struct ucontext *uc_link;
144 struct mcontext *uc_regs; // points to uc_mcontext field
146 // glibc has 1024-bit signal masks, ours are 64-bit
149 struct mcontext uc_mcontext;
152 #define ELF_NGREG 48 // includes nip, msr, lr, etc.
153 #define ELF_NFPREG 33 // includes fpscr
156 typedef unsigned long elf_greg_t;
157 typedef elf_greg_t elf_gregset_t[ELF_NGREG];
159 // Floating point registers
160 typedef double elf_fpreg_t;
161 typedef elf_fpreg_t elf_fpregset_t[ELF_NFPREG];
167 #define restore_regs_from_context(ctx_reg,ip_reg,tmp_reg) do { \
169 ppc_ldptr (code, ip_reg, G_STRUCT_OFFSET (MonoContext, sc_ir), ctx_reg); \
170 ppc_load_multiple_regs (code, MONO_PPC_FIRST_SAVED_GREG, \
171 G_STRUCT_OFFSET (MonoContext, regs) + MONO_PPC_FIRST_SAVED_GREG * sizeof (target_mgreg_t), ctx_reg); \
172 for (reg = MONO_PPC_FIRST_SAVED_FREG; reg < MONO_MAX_FREGS; ++reg) { \
173 ppc_lfd (code, reg, \
174 G_STRUCT_OFFSET(MonoContext, fregs) + reg * sizeof (gdouble), ctx_reg); \
179 #define setup_context(ctx)
181 #ifdef PPC_USES_FUNCTION_DESCRIPTOR
183 mono_ppc_create_pre_code_ftnptr (guint8
*code
)
185 MonoPPCFunctionDescriptor
*ftnptr
= (MonoPPCFunctionDescriptor
*)code
;
187 code
+= sizeof (MonoPPCFunctionDescriptor
);
197 * arch_get_restore_context:
199 * Returns a pointer to a method which restores a previously saved sigcontext.
200 * The first argument in r3 is the pointer to the context.
203 mono_arch_get_restore_context (MonoTrampInfo
**info
, gboolean aot
)
205 guint8
*start
, *code
;
206 int size
= MONO_PPC_32_64_CASE (128, 172) + PPC_FTNPTR_SIZE
;
207 MonoJumpInfo
*ji
= NULL
;
208 GSList
*unwind_ops
= NULL
;
210 code
= start
= mono_global_codeman_reserve (size
);
212 code
= mono_ppc_create_pre_code_ftnptr (code
);
213 restore_regs_from_context (ppc_r3
, ppc_r4
, ppc_r5
);
214 /* restore also the stack pointer */
215 ppc_ldptr (code
, ppc_sp
, G_STRUCT_OFFSET (MonoContext
, sc_sp
), ppc_r3
);
217 /* jump to the saved IP */
218 ppc_mtctr (code
, ppc_r4
);
219 ppc_bcctr (code
, PPC_BR_ALWAYS
, 0);
223 g_assert ((code
- start
) <= size
);
224 mono_arch_flush_icache (start
, code
- start
);
225 MONO_PROFILER_RAISE (jit_code_buffer
, (start
, code
- start
, MONO_PROFILER_CODE_BUFFER_EXCEPTION_HANDLING
, NULL
));
228 *info
= mono_tramp_info_create ("restore_context", start
, code
- start
, ji
, unwind_ops
);
233 #define SAVED_REGS_LENGTH (sizeof (gdouble) * MONO_MAX_FREGS + sizeof (target_mgreg_t) * MONO_MAX_IREGS)
234 #define ALIGN_STACK_FRAME_SIZE(s) (((s) + MONO_ARCH_FRAME_ALIGNMENT - 1) & ~(MONO_ARCH_FRAME_ALIGNMENT - 1))
235 /* The 64 bytes here are for outgoing arguments and a bit of spare.
236 We don't use it all, but it doesn't hurt. */
237 #define REG_SAVE_STACK_FRAME_SIZE (ALIGN_STACK_FRAME_SIZE (SAVED_REGS_LENGTH + PPC_MINIMAL_STACK_SIZE + 64))
240 emit_save_saved_regs (guint8
*code
, int pos
)
244 for (i
= MONO_MAX_FREGS
- 1; i
>= MONO_PPC_FIRST_SAVED_FREG
; --i
) {
245 pos
-= sizeof (gdouble
);
246 ppc_stfd (code
, i
, pos
, ppc_sp
);
248 pos
-= (MONO_MAX_FREGS
- MONO_SAVED_FREGS
) * sizeof (gdouble
);
249 pos
-= sizeof (target_mgreg_t
) * MONO_SAVED_GREGS
;
250 ppc_store_multiple_regs (code
, MONO_PPC_FIRST_SAVED_GREG
, pos
, ppc_sp
);
256 * mono_arch_get_call_filter:
258 * Returns a pointer to a method which calls an exception filter. We
259 * also use this function to call finally handlers (we pass NULL as
260 * @exc object in this case).
263 mono_arch_get_call_filter (MonoTrampInfo
**info
, gboolean aot
)
265 guint8
*start
, *code
;
266 int alloc_size
, pos
, i
;
267 int size
= MONO_PPC_32_64_CASE (320, 500) + PPC_FTNPTR_SIZE
;
268 MonoJumpInfo
*ji
= NULL
;
269 GSList
*unwind_ops
= NULL
;
271 /* call_filter (MonoContext *ctx, unsigned long eip, gpointer exc) */
272 code
= start
= mono_global_codeman_reserve (size
);
274 code
= mono_ppc_create_pre_code_ftnptr (code
);
277 ppc_mflr (code
, ppc_r0
);
278 ppc_stptr (code
, ppc_r0
, PPC_RET_ADDR_OFFSET
, ppc_sp
);
280 alloc_size
= REG_SAVE_STACK_FRAME_SIZE
;
282 /* allocate stack frame and set link from sp in ctx */
283 g_assert ((alloc_size
& (MONO_ARCH_FRAME_ALIGNMENT
-1)) == 0);
284 ppc_ldptr (code
, ppc_r0
, G_STRUCT_OFFSET (MonoContext
, sc_sp
), ppc_r3
);
285 ppc_ldptr_indexed (code
, ppc_r0
, ppc_r0
, ppc_r0
);
286 ppc_stptr_update (code
, ppc_r0
, -alloc_size
, ppc_sp
);
288 code
= emit_save_saved_regs (code
, alloc_size
);
290 /* restore all the regs from ctx (in r3), but not r1, the stack pointer */
291 restore_regs_from_context (ppc_r3
, ppc_r6
, ppc_r7
);
292 /* call handler at eip (r4) and set the first arg with the exception (r5) */
293 ppc_mtctr (code
, ppc_r4
);
294 ppc_mr (code
, ppc_r3
, ppc_r5
);
295 ppc_bcctrl (code
, PPC_BR_ALWAYS
, 0);
298 ppc_ldptr (code
, ppc_r0
, alloc_size
+ PPC_RET_ADDR_OFFSET
, ppc_sp
);
299 ppc_mtlr (code
, ppc_r0
);
301 /* restore all the regs from the stack */
303 for (i
= MONO_MAX_FREGS
- 1; i
>= MONO_PPC_FIRST_SAVED_FREG
; --i
) {
304 pos
-= sizeof (gdouble
);
305 ppc_lfd (code
, i
, pos
, ppc_sp
);
307 pos
-= (MONO_MAX_FREGS
- MONO_SAVED_FREGS
) * sizeof (gdouble
);
308 pos
-= sizeof (target_mgreg_t
) * MONO_SAVED_GREGS
;
309 ppc_load_multiple_regs (code
, MONO_PPC_FIRST_SAVED_GREG
, pos
, ppc_sp
);
311 ppc_addic (code
, ppc_sp
, ppc_sp
, alloc_size
);
314 g_assert ((code
- start
) < size
);
315 mono_arch_flush_icache (start
, code
- start
);
316 MONO_PROFILER_RAISE (jit_code_buffer
, (start
, code
- start
, MONO_PROFILER_CODE_BUFFER_EXCEPTION_HANDLING
, NULL
));
319 *info
= mono_tramp_info_create ("call_filter", start
, code
- start
, ji
, unwind_ops
);
325 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
)
330 /* adjust eip so that it point into the call instruction */
333 setup_context (&ctx
);
335 /*printf ("stack in throw: %p\n", esp);*/
336 MONO_CONTEXT_SET_BP (&ctx
, esp
);
337 MONO_CONTEXT_SET_IP (&ctx
, eip
);
338 memcpy (&ctx
.regs
, int_regs
, sizeof (host_mgreg_t
) * MONO_MAX_IREGS
);
339 memcpy (&ctx
.fregs
, fp_regs
, sizeof (double) * MONO_MAX_FREGS
);
341 if (mono_object_isinst_checked (exc
, mono_defaults
.exception_class
, error
)) {
342 MonoException
*mono_ex
= (MonoException
*)exc
;
344 mono_ex
->stack_trace
= NULL
;
345 mono_ex
->trace_ips
= NULL
;
346 } else if (preserve_ips
) {
347 mono_ex
->caught_in_unmanaged
= TRUE
;
350 mono_error_assert_ok (error
);
351 mono_handle_exception (&ctx
, exc
);
352 mono_restore_context (&ctx
);
354 g_assert_not_reached ();
358 * arch_get_throw_exception_generic:
360 * Returns a function pointer which can be used to raise
361 * exceptions. The returned function has the following
362 * signature: void (*func) (MonoException *exc); or
363 * void (*func) (guint32 ex_token, gpointer ip)
367 mono_arch_get_throw_exception_generic (int size
, MonoTrampInfo
**info
, int corlib
, gboolean rethrow
, gboolean aot
, gboolean preserve_ips
)
369 guint8
*start
, *code
;
371 MonoJumpInfo
*ji
= NULL
;
372 GSList
*unwind_ops
= NULL
;
374 code
= start
= mono_global_codeman_reserve (size
);
376 code
= mono_ppc_create_pre_code_ftnptr (code
);
380 ppc_mr (code
, ppc_r0
, ppc_r4
);
382 ppc_mflr (code
, ppc_r0
);
383 ppc_stptr (code
, ppc_r0
, PPC_RET_ADDR_OFFSET
, ppc_sp
);
385 alloc_size
= REG_SAVE_STACK_FRAME_SIZE
;
387 g_assert ((alloc_size
& (MONO_ARCH_FRAME_ALIGNMENT
-1)) == 0);
388 ppc_stptr_update (code
, ppc_sp
, -alloc_size
, ppc_sp
);
390 code
= emit_save_saved_regs (code
, alloc_size
);
394 ppc_mr (code
, ppc_r4
, ppc_r3
);
397 code
= mono_arch_emit_load_aotconst (start
, code
, &ji
, MONO_PATCH_INFO_IMAGE
, mono_defaults
.corlib
);
398 ppc_mr (code
, ppc_r3
, ppc_r12
);
399 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
));
400 #ifdef PPC_USES_FUNCTION_DESCRIPTOR
401 ppc_ldptr (code
, ppc_r2
, sizeof (target_mgreg_t
), ppc_r12
);
402 ppc_ldptr (code
, ppc_r12
, 0, ppc_r12
);
404 ppc_mtctr (code
, ppc_r12
);
405 ppc_bcctrl (code
, PPC_BR_ALWAYS
, 0);
407 ppc_load (code
, ppc_r3
, (gulong
)mono_defaults
.corlib
);
408 ppc_load_func (code
, PPC_CALL_REG
, mono_exception_from_token
);
409 ppc_mtctr (code
, PPC_CALL_REG
);
410 ppc_bcctrl (code
, PPC_BR_ALWAYS
, 0);
414 /* call throw_exception (exc, ip, sp, int_regs, fp_regs) */
416 ppc_ldptr (code
, ppc_r5
, 0, ppc_sp
);
417 /* exc is already in place in r3 */
419 ppc_ldptr (code
, ppc_r4
, PPC_RET_ADDR_OFFSET
, ppc_r5
);
421 ppc_mr (code
, ppc_r4
, ppc_r0
); /* caller ip */
422 /* pointer to the saved fp regs */
423 pos
= alloc_size
- sizeof (gdouble
) * MONO_MAX_FREGS
;
424 ppc_addi (code
, ppc_r7
, ppc_sp
, pos
);
425 /* pointer to the saved int regs */
426 pos
-= sizeof (target_mgreg_t
) * MONO_MAX_IREGS
;
427 ppc_addi (code
, ppc_r6
, ppc_sp
, pos
);
428 ppc_li (code
, ppc_r8
, rethrow
);
429 ppc_li (code
, ppc_r9
, preserve_ips
);
432 // This can be called from runtime code, which can't guarantee that
433 // r30 contains the got address.
434 // So emit the got address loading code too
435 code
= mono_arch_emit_load_got_addr (start
, code
, NULL
, &ji
);
436 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
));
437 #ifdef PPC_USES_FUNCTION_DESCRIPTOR
438 ppc_ldptr (code
, ppc_r2
, sizeof (target_mgreg_t
), ppc_r12
);
439 ppc_ldptr (code
, ppc_r12
, 0, ppc_r12
);
441 ppc_mtctr (code
, ppc_r12
);
442 ppc_bcctrl (code
, PPC_BR_ALWAYS
, 0);
444 ppc_load_func (code
, PPC_CALL_REG
, mono_ppc_throw_exception
);
445 ppc_mtctr (code
, PPC_CALL_REG
);
446 ppc_bcctrl (code
, PPC_BR_ALWAYS
, 0);
448 /* we should never reach this breakpoint */
450 g_assert ((code
- start
) <= size
);
451 mono_arch_flush_icache (start
, code
- start
);
452 MONO_PROFILER_RAISE (jit_code_buffer
, (start
, code
- start
, MONO_PROFILER_CODE_BUFFER_EXCEPTION_HANDLING
, NULL
));
455 *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
);
461 * mono_arch_get_rethrow_preserve_exception:
462 * \returns a function pointer which can be used to rethrow
463 * exceptions and completely preserve trace_ips.
464 * The returned function has the following
465 * signature: void (*func) (MonoException *exc);
468 mono_arch_get_rethrow_preserve_exception (MonoTrampInfo
**info
, gboolean aot
)
470 int size
= MONO_PPC_32_64_CASE (132, 224) + PPC_FTNPTR_SIZE
;
474 return mono_arch_get_throw_exception_generic (size
, info
, FALSE
, TRUE
, aot
, TRUE
);
478 * mono_arch_get_rethrow_exception:
479 * \returns a function pointer which can be used to rethrow
480 * exceptions. The returned function has the following
481 * signature: void (*func) (MonoException *exc);
484 mono_arch_get_rethrow_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
, TRUE
, aot
, FALSE
);
494 * arch_get_throw_exception:
496 * Returns a function pointer which can be used to raise
497 * exceptions. The returned function has the following
498 * signature: void (*func) (MonoException *exc);
499 * For example to raise an arithmetic exception you can use:
501 * x86_push_imm (code, mono_get_exception_arithmetic ());
502 * x86_call_code (code, arch_get_throw_exception ());
506 mono_arch_get_throw_exception (MonoTrampInfo
**info
, gboolean aot
)
508 int size
= MONO_PPC_32_64_CASE (132, 224) + PPC_FTNPTR_SIZE
;
512 return mono_arch_get_throw_exception_generic (size
, info
, FALSE
, FALSE
, aot
, FALSE
);
516 * mono_arch_get_throw_corlib_exception:
517 * \returns a function pointer which can be used to raise
518 * corlib exceptions. The returned function has the following
519 * signature: void (*func) (guint32 ex_token, guint32 offset);
520 * On PPC, we pass the ip instead of the offset
523 mono_arch_get_throw_corlib_exception (MonoTrampInfo
**info
, gboolean aot
)
525 int size
= MONO_PPC_32_64_CASE (168, 304) + PPC_FTNPTR_SIZE
;
529 return mono_arch_get_throw_exception_generic (size
, info
, TRUE
, FALSE
, aot
, FALSE
);
533 * mono_arch_unwind_frame:
535 * See exceptions-amd64.c for docs.
538 mono_arch_unwind_frame (MonoDomain
*domain
, MonoJitTlsData
*jit_tls
,
539 MonoJitInfo
*ji
, MonoContext
*ctx
,
540 MonoContext
*new_ctx
, MonoLMF
**lmf
,
541 host_mgreg_t
**save_locations
,
542 StackFrameInfo
*frame
)
544 gpointer ip
= MONO_CONTEXT_GET_IP (ctx
);
545 MonoPPCStackFrame
*sframe
;
547 memset (frame
, 0, sizeof (StackFrameInfo
));
551 setup_context (new_ctx
);
555 host_mgreg_t regs
[ppc_lr
+ 1];
557 guint32 unwind_info_len
;
560 if (ji
->is_trampoline
)
561 frame
->type
= FRAME_TYPE_TRAMPOLINE
;
563 frame
->type
= FRAME_TYPE_MANAGED
;
565 unwind_info
= mono_jinfo_get_unwind_info (ji
, &unwind_info_len
);
567 sframe
= (MonoPPCStackFrame
*)MONO_CONTEXT_GET_SP (ctx
);
568 MONO_CONTEXT_SET_BP (new_ctx
, sframe
->sp
);
569 if (!ji
->is_trampoline
&& jinfo_get_method (ji
)->save_lmf
) {
570 /* sframe->sp points just past the end of the LMF */
571 guint8
*lmf_addr
= (guint8
*)sframe
->sp
- sizeof (MonoLMF
);
572 memcpy (&new_ctx
->fregs
[MONO_PPC_FIRST_SAVED_FREG
], lmf_addr
+ G_STRUCT_OFFSET (MonoLMF
, fregs
), sizeof (double) * MONO_SAVED_FREGS
);
573 memcpy (&new_ctx
->regs
[MONO_PPC_FIRST_SAVED_GREG
], lmf_addr
+ G_STRUCT_OFFSET (MonoLMF
, iregs
), sizeof (host_mgreg_t
) * MONO_SAVED_GREGS
);
574 /* the calling IP is in the parent frame */
575 sframe
= (MonoPPCStackFrame
*)sframe
->sp
;
576 /* we substract 4, so that the IP points into the call instruction */
577 MONO_CONTEXT_SET_IP (new_ctx
, sframe
->lr
- 4);
579 regs
[ppc_lr
] = ctx
->sc_ir
;
580 regs
[ppc_sp
] = ctx
->sc_sp
;
581 for (i
= MONO_PPC_FIRST_SAVED_GREG
; i
< MONO_MAX_IREGS
; ++i
)
582 regs
[i
] = ctx
->regs
[i
];
584 gboolean success
= mono_unwind_frame (unwind_info
, unwind_info_len
, ji
->code_start
,
585 (guint8
*)ji
->code_start
+ ji
->code_size
,
586 ip
, NULL
, regs
, ppc_lr
+ 1,
587 save_locations
, MONO_MAX_IREGS
, &cfa
);
592 /* we substract 4, so that the IP points into the call instruction */
593 MONO_CONTEXT_SET_IP (new_ctx
, regs
[ppc_lr
] - 4);
594 MONO_CONTEXT_SET_BP (new_ctx
, cfa
);
596 for (i
= MONO_PPC_FIRST_SAVED_GREG
; i
< MONO_MAX_IREGS
; ++i
)
597 new_ctx
->regs
[i
] = regs
[i
];
603 if ((ji
= mini_jit_info_table_find (domain
, (gpointer
)(*lmf
)->eip
, NULL
))) {
608 /* Trampoline lmf frame */
609 frame
->method
= (*lmf
)->method
;
612 /*sframe = (MonoPPCStackFrame*)MONO_CONTEXT_GET_SP (ctx);
613 MONO_CONTEXT_SET_BP (new_ctx, sframe->sp);
614 MONO_CONTEXT_SET_IP (new_ctx, sframe->lr);*/
615 MONO_CONTEXT_SET_BP (new_ctx
, (*lmf
)->ebp
);
616 MONO_CONTEXT_SET_IP (new_ctx
, (*lmf
)->eip
);
617 memcpy (&new_ctx
->regs
[MONO_PPC_FIRST_SAVED_GREG
], (*lmf
)->iregs
, sizeof (host_mgreg_t
) * MONO_SAVED_GREGS
);
618 memcpy (&new_ctx
->fregs
[MONO_PPC_FIRST_SAVED_FREG
], (*lmf
)->fregs
, sizeof (double) * MONO_SAVED_FREGS
);
621 frame
->type
= FRAME_TYPE_MANAGED_TO_NATIVE
;
623 /* FIXME: what about trampoline LMF frames? see exceptions-x86.c */
625 *lmf
= (*lmf
)->previous_lmf
;
634 mono_arch_ip_from_context (void *sigctx
)
636 #ifdef MONO_CROSS_COMPILE
637 g_assert_not_reached ();
639 os_ucontext
*uc
= sigctx
;
640 return (gpointer
)UCONTEXT_REG_NIP(uc
);
645 altstack_handle_and_restore (MonoContext
*mctx
, gpointer obj
)
647 mono_handle_exception (mctx
, obj
);
648 mono_restore_context (mctx
);
652 mono_arch_handle_altstack_exception (void *sigctx
, MONO_SIG_HANDLER_INFO_TYPE
*siginfo
, gpointer fault_addr
, gboolean stack_ovf
)
654 #ifdef MONO_CROSS_COMPILE
655 g_assert_not_reached ();
657 #ifdef MONO_ARCH_USE_SIGACTION
658 os_ucontext
*uc
= (os_ucontext
*)sigctx
;
659 MonoContext
*uc_copy
;
660 MonoJitInfo
*ji
= mini_jit_info_table_find (mono_domain_get (), mono_arch_ip_from_context (sigctx
), NULL
);
666 /* we don't do much now, but we can warn the user with a useful message */
667 fprintf (stderr
, "Stack overflow: IP: %p, SP: %p\n", mono_arch_ip_from_context (sigctx
), (gpointer
)UCONTEXT_REG_Rn(uc
, 1));
668 if (ji
&& !ji
->is_trampoline
&& jinfo_get_method (ji
))
669 method
= mono_method_full_name (jinfo_get_method (ji
), TRUE
);
671 method
= "Unmanaged";
672 fprintf (stderr
, "At %s\n", method
);
676 mono_handle_native_crash ("SIGSEGV", sigctx
, siginfo
);
677 /* setup a call frame on the real stack so that control is returned there
678 * and exception handling can continue.
679 * The frame looks like:
682 * 224 is the size of the red zone
684 frame_size
= sizeof (MonoContext
) + sizeof (gpointer
) * 16 + 224;
687 sp
= (gpointer
)(UCONTEXT_REG_Rn(uc
, 1) & ~15);
688 sp
= (gpointer
)((char*)sp
- frame_size
);
689 /* may need to adjust pointers in the new struct copy, depending on the OS */
690 uc_copy
= (MonoContext
*)(sp
+ 16);
691 mono_sigctx_to_monoctx (uc
, uc_copy
);
692 #if defined(__linux__) && !defined(__mono_ppc64__)
693 uc_copy
->uc_mcontext
.uc_regs
= (gpointer
)((char*)uc_copy
+ ((char*)uc
->uc_mcontext
.uc_regs
- (char*)uc
));
695 g_assert (mono_arch_ip_from_context (uc
) == mono_arch_ip_from_context (uc_copy
));
696 /* at the return form the signal handler execution starts in altstack_handle_and_restore() */
697 UCONTEXT_REG_LNK(uc
) = UCONTEXT_REG_NIP(uc
);
698 #ifdef PPC_USES_FUNCTION_DESCRIPTOR
700 MonoPPCFunctionDescriptor
*handler_ftnptr
= (MonoPPCFunctionDescriptor
*)altstack_handle_and_restore
;
702 UCONTEXT_REG_NIP(uc
) = (gulong
)handler_ftnptr
->code
;
703 UCONTEXT_REG_Rn(uc
, 2) = (gulong
)handler_ftnptr
->toc
;
706 UCONTEXT_REG_NIP(uc
) = (unsigned long)altstack_handle_and_restore
;
708 /* ELF v2 ABI calling convention requires to put the target address into
709 * r12 if we use the global entry point of a function. */
710 UCONTEXT_REG_Rn(uc
, 12) = (unsigned long) altstack_handle_and_restore
;
713 UCONTEXT_REG_Rn(uc
, 1) = (unsigned long)sp
;
714 UCONTEXT_REG_Rn(uc
, PPC_FIRST_ARG_REG
) = (unsigned long)uc_copy
;
715 UCONTEXT_REG_Rn(uc
, PPC_FIRST_ARG_REG
+ 1) = 0;
716 UCONTEXT_REG_Rn(uc
, PPC_FIRST_ARG_REG
+ 2) = 0;
719 #endif /* !MONO_CROSS_COMPILE */
725 * Called by resuming from a signal handler.
728 handle_signal_exception (gpointer obj
)
730 MonoJitTlsData
*jit_tls
= mono_tls_get_jit_tls ();
733 memcpy (&ctx
, &jit_tls
->ex_ctx
, sizeof (MonoContext
));
735 mono_handle_exception (&ctx
, obj
);
737 mono_restore_context (&ctx
);
741 setup_ucontext_return (void *uc
, gpointer func
)
743 #if !defined(MONO_CROSS_COMPILE)
744 UCONTEXT_REG_LNK(uc
) = UCONTEXT_REG_NIP(uc
);
745 #ifdef PPC_USES_FUNCTION_DESCRIPTOR
747 MonoPPCFunctionDescriptor
*handler_ftnptr
= (MonoPPCFunctionDescriptor
*)func
;
749 UCONTEXT_REG_NIP(uc
) = (gulong
)handler_ftnptr
->code
;
750 UCONTEXT_REG_Rn(uc
, 2) = (gulong
)handler_ftnptr
->toc
;
753 UCONTEXT_REG_NIP(uc
) = (unsigned long)func
;
755 /* ELF v2 ABI calling convention requires to put the target address into
756 * r12 if we use the global entry point of a function. */
757 UCONTEXT_REG_Rn(uc
, 12) = (unsigned long) func
;
764 mono_arch_handle_exception (void *ctx
, gpointer obj
)
766 #if defined(MONO_ARCH_USE_SIGACTION) && defined(UCONTEXT_REG_Rn)
768 * Handling the exception in the signal handler is problematic, since the original
769 * signal is disabled, and we could run arbitrary code though the debugger. So
770 * resume into the normal stack and do most work there if possible.
772 MonoJitTlsData
*jit_tls
= mono_tls_get_jit_tls ();
778 /* Pass the ctx parameter in TLS */
779 mono_sigctx_to_monoctx (sigctx
, &jit_tls
->ex_ctx
);
780 /* The others in registers */
781 UCONTEXT_REG_Rn (sigctx
, PPC_FIRST_ARG_REG
) = (gsize
)obj
;
783 /* Allocate a stack frame below the red zone */
784 /* Similar to mono_arch_handle_altstack_exception () */
788 sp
= (host_mgreg_t
)(UCONTEXT_REG_Rn(uc
, 1) & ~15);
789 sp
= (host_mgreg_t
)(sp
- frame_size
);
790 UCONTEXT_REG_Rn(uc
, 1) = (host_mgreg_t
)sp
;
791 setup_ucontext_return (uc
, handle_signal_exception
);
798 mono_sigctx_to_monoctx (ctx
, &mctx
);
800 result
= mono_handle_exception (&mctx
, obj
);
801 /* restore the context so that returning from the signal handler will invoke
804 mono_monoctx_to_sigctx (&mctx
, ctx
);
810 // FIX ME: This is not complete
812 mono_arch_setup_async_callback (MonoContext
*ctx
, void (*async_cb
)(void *fun
), gpointer user_data
)
814 uintptr_t sp
= (uintptr_t) MONO_CONTEXT_GET_SP(ctx
);
815 ctx
->regs
[PPC_FIRST_ARG_REG
] = user_data
;
816 sp
-= PPC_MINIMAL_STACK_SIZE
;
817 *(unsigned long *)sp
= MONO_CONTEXT_GET_SP(ctx
);
818 MONO_CONTEXT_SET_BP(ctx
, sp
);
819 mono_arch_setup_resume_sighandler_ctx(ctx
, (unsigned long) async_cb
);
823 mono_arch_setup_resume_sighandler_ctx (MonoContext
*ctx
, gpointer func
)
825 #ifdef PPC_USES_FUNCTION_DESCRIPTOR
826 MonoPPCFunctionDescriptor
*handler_ftnptr
= (MonoPPCFunctionDescriptor
*)func
;
827 MONO_CONTEXT_SET_IP(ctx
, (gulong
)handler_ftnptr
->code
);
828 ctx
->regs
[2] = (gulong
)handler_ftnptr
->toc
;
830 MONO_CONTEXT_SET_IP(ctx
, (unsigned long) func
);