[2019-12] [jit] Avoid passing a vtable argument to DIM methods when making calls...
[mono-project.git] / mono / mini / exceptions-ppc.c
blob44d09bcaba1d3f91a171c4fe8927f8db40308988
1 /**
2 * \file
3 * exception support for PowerPC
5 * Authors:
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
14 #include <config.h>
15 #include <glib.h>
16 #include <signal.h>
17 #include <string.h>
18 #include <stddef.h>
19 #if HAVE_UCONTEXT_H
20 #include <ucontext.h>
21 #endif
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>
32 #include "mini.h"
33 #include "mini-ppc.h"
34 #include "mini-runtime.h"
35 #include "aot-runtime.h"
36 #include "mono/utils/mono-tls-inline.h"
40 struct sigcontext {
41 int sc_onstack; // sigstack state to restore
42 int sc_mask; // signal mask to restore
43 int sc_ir; // pc
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
49 struct ucontext {
50 int uc_onstack;
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
60 unsigned long dsisr;
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];
73 } ppc_vector_state_t;
75 typedef struct ppc_float_state {
76 double fpregs[32];
78 unsigned int fpscr_pad; // fpscr is 64 bits, 32 bits of rubbish
79 unsigned int fpscr; // floating point status register
80 } ppc_float_state_t;
82 typedef struct ppc_thread_state {
83 unsigned int srr0; // Instruction address register (PC)
84 unsigned int srr1; // Machine state register (supervisor)
85 unsigned int r0;
86 unsigned int r1;
87 unsigned int r2;
88 ...
89 unsigned int r31;
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
97 } ppc_thread_state_t;
99 struct mcontext {
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:
109 struct sigcontext {
110 unsigned long _unused[4];
111 int signal;
112 unsigned long handler;
113 unsigned long oldmask;
114 struct pt_regs *regs;
116 struct pt_regs {
117 unsigned long gpr[32];
118 unsigned long nip;
119 unsigned long msr;
120 unsigned long orig_gpr3; // Used for restarting system calls
121 unsigned long ctr;
122 unsigned long link;
123 unsigned long xer;
124 unsigned long ccr;
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
134 struct mcontext {
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)));
141 struct ucontext {
142 unsigned long uc_flags;
143 struct ucontext *uc_link;
144 stack_t uc_stack;
145 int uc_pad[7];
146 struct mcontext *uc_regs; // points to uc_mcontext field
147 sigset_t uc_sigmask;
148 // glibc has 1024-bit signal masks, ours are 64-bit
149 int uc_maskext[30];
150 int uc_pad2[3];
151 struct mcontext uc_mcontext;
154 #define ELF_NGREG 48 // includes nip, msr, lr, etc.
155 #define ELF_NFPREG 33 // includes fpscr
157 // General registers
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 { \
170 int reg; \
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); \
178 } while (0)
180 /* nothing to do */
181 #define setup_context(ctx)
183 #ifdef PPC_USES_FUNCTION_DESCRIPTOR
184 guint8*
185 mono_ppc_create_pre_code_ftnptr (guint8 *code)
187 MonoPPCFunctionDescriptor *ftnptr = (MonoPPCFunctionDescriptor*)code;
189 code += sizeof (MonoPPCFunctionDescriptor);
190 ftnptr->code = code;
191 ftnptr->toc = NULL;
192 ftnptr->env = NULL;
194 return code;
196 #endif
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.
204 gpointer
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);
213 if (!aot)
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);
218 //ppc_break (code);
219 /* jump to the saved IP */
220 ppc_mtctr (code, ppc_r4);
221 ppc_bcctr (code, PPC_BR_ALWAYS, 0);
222 /* never reached */
223 ppc_break (code);
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));
229 if (info)
230 *info = mono_tramp_info_create ("restore_context", start, code - start, ji, unwind_ops);
232 return start;
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))
241 static guint8*
242 emit_save_saved_regs (guint8 *code, int pos)
244 int i;
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);
254 return code;
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).
264 gpointer
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);
275 if (!aot)
276 code = mono_ppc_create_pre_code_ftnptr (code);
278 /* store ret addr */
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);
299 /* epilog */
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 */
304 pos = alloc_size;
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);
314 ppc_blr (code);
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));
320 if (info)
321 *info = mono_tramp_info_create ("call_filter", start, code - start, ji, unwind_ops);
323 return start;
326 void
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)
329 ERROR_DECL (error);
330 MonoContext ctx;
332 /* adjust eip so that it point into the call instruction */
333 eip -= 4;
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)
368 static gpointer
369 mono_arch_get_throw_exception_generic (int size, MonoTrampInfo **info, int corlib, gboolean rethrow, gboolean aot, gboolean preserve_ips)
371 guint8 *start, *code;
372 int alloc_size, pos;
373 MonoJumpInfo *ji = NULL;
374 GSList *unwind_ops = NULL;
376 code = start = mono_global_codeman_reserve (size);
377 if (!aot)
378 code = mono_ppc_create_pre_code_ftnptr (code);
380 /* store ret addr */
381 if (corlib)
382 ppc_mr (code, ppc_r0, ppc_r4);
383 else
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);
394 //ppc_break (code);
395 if (corlib) {
396 ppc_mr (code, ppc_r4, ppc_r3);
398 if (aot) {
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);
405 #endif
406 ppc_mtctr (code, ppc_r12);
407 ppc_bcctrl (code, PPC_BR_ALWAYS, 0);
408 } else {
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) */
417 /* caller sp */
418 ppc_ldptr (code, ppc_r5, 0, ppc_sp);
419 /* exc is already in place in r3 */
420 if (corlib)
421 ppc_ldptr (code, ppc_r4, PPC_RET_ADDR_OFFSET, ppc_r5);
422 else
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);
433 if (aot) {
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);
442 #endif
443 ppc_mtctr (code, ppc_r12);
444 ppc_bcctrl (code, PPC_BR_ALWAYS, 0);
445 } else {
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 */
451 ppc_break (code);
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));
456 if (info)
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);
459 return start;
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);
469 gpointer
470 mono_arch_get_rethrow_preserve_exception (MonoTrampInfo **info, gboolean aot)
472 int size = MONO_PPC_32_64_CASE (132, 224) + PPC_FTNPTR_SIZE;
474 if (aot)
475 size += 64;
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);
485 gpointer
486 mono_arch_get_rethrow_exception (MonoTrampInfo **info, gboolean aot)
488 int size = MONO_PPC_32_64_CASE (132, 224) + PPC_FTNPTR_SIZE;
490 if (aot)
491 size += 64;
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 ());
507 gpointer
508 mono_arch_get_throw_exception (MonoTrampInfo **info, gboolean aot)
510 int size = MONO_PPC_32_64_CASE (132, 224) + PPC_FTNPTR_SIZE;
512 if (aot)
513 size += 64;
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
524 gpointer
525 mono_arch_get_throw_corlib_exception (MonoTrampInfo **info, gboolean aot)
527 int size = MONO_PPC_32_64_CASE (168, 304) + PPC_FTNPTR_SIZE;
529 if (aot)
530 size += 64;
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.
539 gboolean
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));
550 frame->ji = ji;
552 *new_ctx = *ctx;
553 setup_context (new_ctx);
555 if (ji != NULL) {
556 int i;
557 host_mgreg_t regs [ppc_lr + 1];
558 guint8 *cfa;
559 guint32 unwind_info_len;
560 guint8 *unwind_info;
562 if (ji->is_trampoline)
563 frame->type = FRAME_TYPE_TRAMPOLINE;
564 else
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);
580 } else {
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);
591 if (!success)
592 return FALSE;
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];
602 return TRUE;
603 } else if (*lmf) {
605 if ((ji = mini_jit_info_table_find (domain, (gpointer)(*lmf)->eip, NULL))) {
606 } else {
607 if (!(*lmf)->method)
608 return FALSE;
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);
622 frame->ji = ji;
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;
629 return TRUE;
632 return FALSE;
635 gpointer
636 mono_arch_ip_from_context (void *sigctx)
638 #ifdef MONO_CROSS_COMPILE
639 g_assert_not_reached ();
640 #else
641 os_ucontext *uc = (os_ucontext*)sigctx;
642 return (gpointer)UCONTEXT_REG_NIP(uc);
643 #endif
646 static void
647 altstack_handle_and_restore (MonoContext *mctx, gpointer obj)
649 mono_handle_exception (mctx, obj);
650 mono_restore_context (mctx);
653 void
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 ();
658 #else
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);
663 gpointer *sp;
664 int frame_size;
666 if (stack_ovf) {
667 const char *method;
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);
672 else
673 method = "Unmanaged";
674 fprintf (stderr, "At %s\n", method);
675 abort ();
677 if (!ji)
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:
683 * ucontext struct
684 * ...
685 * 224 is the size of the red zone
687 frame_size = sizeof (MonoContext) + sizeof (gpointer) * 16 + 224;
688 frame_size += 15;
689 frame_size &= ~15;
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));
697 #endif
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;
708 #else
709 UCONTEXT_REG_NIP(uc) = (unsigned long)altstack_handle_and_restore;
710 #if _CALL_ELF == 2
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;
714 #endif
715 #endif
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;
720 #endif
722 #endif /* !MONO_CROSS_COMPILE */
726 * handle_exception:
728 * Called by resuming from a signal handler.
730 static void
731 handle_signal_exception (gpointer obj)
733 MonoJitTlsData *jit_tls = mono_tls_get_jit_tls ();
734 MonoContext ctx;
736 memcpy (&ctx, &jit_tls->ex_ctx, sizeof (MonoContext));
738 mono_handle_exception (&ctx, obj);
740 mono_restore_context (&ctx);
743 static void
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;
755 #else
756 UCONTEXT_REG_NIP(uc) = (unsigned long)func;
757 #if _CALL_ELF == 2
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;
761 #endif
762 #endif
763 #endif
766 gboolean
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 ();
776 host_mgreg_t sp;
777 void *sigctx = ctx;
778 int frame_size;
779 void *uc = sigctx;
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 () */
788 frame_size = 224;
789 frame_size += 15;
790 frame_size &= ~15;
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);
796 return TRUE;
797 #else
798 MonoContext mctx;
799 gboolean result;
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
805 * the catch clause
807 mono_monoctx_to_sigctx (&mctx, ctx);
808 return result;
809 #endif
813 // FIX ME: This is not complete
814 void
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);
825 void
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;
832 #else
833 MONO_CONTEXT_SET_IP(ctx, (unsigned long) func);
834 #endif