3 * exception support for MIPS
6 * Mark Mason (mason@broadcom.com)
8 * Based on exceptions-ppc.c by:
9 * Dietmar Maurer (dietmar@ximian.com)
10 * Paolo Molaro (lupus@ximian.com)
13 * (C) 2001 Ximian, Inc.
21 #include <mono/arch/mips/mips-codegen.h>
22 #include <mono/metadata/appdomain.h>
23 #include <mono/metadata/tabledefs.h>
24 #include <mono/metadata/threads.h>
25 #include <mono/metadata/debug-helpers.h>
26 #include <mono/metadata/exception.h>
27 #include <mono/metadata/mono-debug.h>
30 #include "mini-mips.h"
31 #include "mini-runtime.h"
32 #include "aot-runtime.h"
33 #include "mono/utils/mono-tls-inline.h"
35 #define GENERIC_EXCEPTION_SIZE 256
38 * mono_arch_get_restore_context:
40 * Returns a pointer to a method which restores a previously saved MonoContext.
41 * The first argument in a0 is the pointer to the MonoContext.
44 mono_arch_get_restore_context (MonoTrampInfo
**info
, gboolean aot
)
48 static guint8 start
[512];
49 static int inited
= 0;
50 guint32 iregs_to_restore
;
61 mips_move (code
, mips_at
, mips_a0
);
63 iregs_to_restore
= (MONO_ARCH_CALLEE_SAVED_REGS \
64 | (1 << mips_sp
) | (1 << mips_ra
));
65 for (i
= 0; i
< MONO_SAVED_GREGS
; ++i
) {
66 //if (iregs_to_restore & (1 << i)) {
67 if (i
!= mips_zero
&& i
!= mips_at
) {
68 MIPS_LW (code
, i
, mips_at
, G_STRUCT_OFFSET (MonoContext
, sc_regs
[i
]));
72 /* Get the address to return to */
73 mips_lw (code
, mips_t9
, mips_at
, G_STRUCT_OFFSET (MonoContext
, sc_pc
));
75 /* jump to the saved IP */
76 mips_jr (code
, mips_t9
);
80 mips_break (code
, 0xff);
82 g_assert ((code
- start
) < sizeof(start
));
83 mono_arch_flush_icache (start
, code
- start
);
84 MONO_PROFILER_RAISE (jit_code_buffer
, (start
, code
- start
, MONO_PROFILER_CODE_BUFFER_EXCEPTION_HANDLING
, NULL
));
89 * mono_arch_get_call_filter:
91 * Returns a pointer to a method which calls an exception filter. We
92 * also use this function to call finally handlers (we pass NULL as
93 * @exc object in this case).
95 * This function is invoked as
96 * call_handler (MonoContext *ctx, handler)
98 * Where 'handler' is a function to be invoked as:
102 mono_arch_get_call_filter (MonoTrampInfo
**info
, gboolean aot
)
104 static guint8 start
[320];
105 static int inited
= 0;
121 g_assert ((alloc_size
& (MIPS_STACK_ALIGNMENT
-1)) == 0);
123 mips_addiu (code
, mips_sp
, mips_sp
, -alloc_size
);
124 mips_sw (code
, mips_ra
, mips_sp
, alloc_size
+ MIPS_RET_ADDR_OFFSET
);
126 /* Save global registers on stack (s0 - s7) */
128 MIPS_SW (code
, mips_s0
, mips_sp
, offset
); offset
+= IREG_SIZE
;
129 MIPS_SW (code
, mips_s1
, mips_sp
, offset
); offset
+= IREG_SIZE
;
130 MIPS_SW (code
, mips_s2
, mips_sp
, offset
); offset
+= IREG_SIZE
;
131 MIPS_SW (code
, mips_s3
, mips_sp
, offset
); offset
+= IREG_SIZE
;
132 MIPS_SW (code
, mips_s4
, mips_sp
, offset
); offset
+= IREG_SIZE
;
133 MIPS_SW (code
, mips_s5
, mips_sp
, offset
); offset
+= IREG_SIZE
;
134 MIPS_SW (code
, mips_s6
, mips_sp
, offset
); offset
+= IREG_SIZE
;
135 MIPS_SW (code
, mips_s7
, mips_sp
, offset
); offset
+= IREG_SIZE
;
136 MIPS_SW (code
, mips_fp
, mips_sp
, offset
); offset
+= IREG_SIZE
;
138 /* Restore global registers from MonoContext, including the frame pointer */
139 MIPS_LW (code
, mips_s0
, mips_a0
, G_STRUCT_OFFSET (MonoContext
, sc_regs
[mips_s0
]));
140 MIPS_LW (code
, mips_s1
, mips_a0
, G_STRUCT_OFFSET (MonoContext
, sc_regs
[mips_s1
]));
141 MIPS_LW (code
, mips_s2
, mips_a0
, G_STRUCT_OFFSET (MonoContext
, sc_regs
[mips_s2
]));
142 MIPS_LW (code
, mips_s3
, mips_a0
, G_STRUCT_OFFSET (MonoContext
, sc_regs
[mips_s3
]));
143 MIPS_LW (code
, mips_s4
, mips_a0
, G_STRUCT_OFFSET (MonoContext
, sc_regs
[mips_s4
]));
144 MIPS_LW (code
, mips_s5
, mips_a0
, G_STRUCT_OFFSET (MonoContext
, sc_regs
[mips_s5
]));
145 MIPS_LW (code
, mips_s6
, mips_a0
, G_STRUCT_OFFSET (MonoContext
, sc_regs
[mips_s6
]));
146 MIPS_LW (code
, mips_s7
, mips_a0
, G_STRUCT_OFFSET (MonoContext
, sc_regs
[mips_s7
]));
147 MIPS_LW (code
, mips_fp
, mips_a0
, G_STRUCT_OFFSET (MonoContext
, sc_regs
[mips_fp
]));
149 /* a1 is the handler to call */
150 mips_move (code
, mips_t9
, mips_a1
);
152 /* jump to the saved IP */
153 mips_jalr (code
, mips_t9
, mips_ra
);
156 /* restore all regs from the stack */
158 MIPS_LW (code
, mips_s0
, mips_sp
, offset
); offset
+= IREG_SIZE
;
159 MIPS_LW (code
, mips_s1
, mips_sp
, offset
); offset
+= IREG_SIZE
;
160 MIPS_LW (code
, mips_s2
, mips_sp
, offset
); offset
+= IREG_SIZE
;
161 MIPS_LW (code
, mips_s3
, mips_sp
, offset
); offset
+= IREG_SIZE
;
162 MIPS_LW (code
, mips_s4
, mips_sp
, offset
); offset
+= IREG_SIZE
;
163 MIPS_LW (code
, mips_s5
, mips_sp
, offset
); offset
+= IREG_SIZE
;
164 MIPS_LW (code
, mips_s6
, mips_sp
, offset
); offset
+= IREG_SIZE
;
165 MIPS_LW (code
, mips_s7
, mips_sp
, offset
); offset
+= IREG_SIZE
;
166 MIPS_LW (code
, mips_fp
, mips_sp
, offset
); offset
+= IREG_SIZE
;
169 mips_lw (code
, mips_ra
, mips_sp
, alloc_size
+ MIPS_RET_ADDR_OFFSET
);
170 mips_addiu (code
, mips_sp
, mips_sp
, alloc_size
);
171 mips_jr (code
, mips_ra
);
174 g_assert ((code
- start
) < sizeof(start
));
175 mono_arch_flush_icache (start
, code
- start
);
176 MONO_PROFILER_RAISE (jit_code_buffer
, (start
, code
- start
, MONO_PROFILER_CODE_BUFFER_EXCEPTION_HANDLING
, NULL
));
181 throw_exception (MonoObject
*exc
, unsigned long eip
, unsigned long esp
, gboolean rethrow
, gboolean preserve_ips
)
186 #ifdef DEBUG_EXCEPTIONS
187 g_print ("throw_exception: exc=%p eip=%p esp=%p rethrow=%d\n",
188 exc
, (void *)eip
, (void *) esp
, rethrow
);
191 /* adjust eip so that it point into the call instruction */
194 memset (&ctx
, 0, sizeof (MonoContext
));
196 /*g_print ("stack in throw: %p\n", esp);*/
197 memcpy (&ctx
.sc_regs
, (void *)(esp
+ MIPS_STACK_PARAM_OFFSET
),
198 sizeof (gulong
) * MONO_SAVED_GREGS
);
199 memset (&ctx
.sc_fpregs
, 0, sizeof (mips_freg
) * MONO_SAVED_FREGS
);
200 MONO_CONTEXT_SET_IP (&ctx
, eip
);
202 if (mono_object_isinst_checked (exc
, mono_defaults
.exception_class
, error
)) {
203 MonoException
*mono_ex
= (MonoException
*)exc
;
204 if (!rethrow
&& !mono_ex
->caught_in_unmanaged
) {
205 mono_ex
->stack_trace
= NULL
;
206 mono_ex
->trace_ips
= NULL
;
207 } if (preserve_ips
) {
208 mono_ex
->caught_in_unmanaged
= TRUE
;
211 mono_error_assert_ok (error
);
212 mono_handle_exception (&ctx
, exc
);
213 #ifdef DEBUG_EXCEPTIONS
214 g_print ("throw_exception: restore to pc=%p sp=%p fp=%p ctx=%p\n",
215 (void *) ctx
.sc_pc
, (void *) ctx
.sc_regs
[mips_sp
],
216 (void *) ctx
.sc_regs
[mips_fp
], &ctx
);
218 mono_restore_context (&ctx
);
220 g_assert_not_reached ();
224 * arch_get_throw_exception_generic:
226 * Returns a function pointer which can be used to raise
227 * exceptions. The returned function has the following
228 * signature: void (*func) (MonoException *exc); or
229 * void (*func) (char *exc_name);
233 mono_arch_get_throw_exception_generic (guint8
*start
, int size
, int corlib
, gboolean rethrow
, gboolean preserve_ips
)
236 int alloc_size
, pos
, i
;
240 //g_print ("mono_arch_get_throw_exception_generic: code=%p\n", code);
243 /* XXX - save all the FP regs on the stack ? */
245 pos
+= MONO_MAX_IREGS
* sizeof(guint32
);
247 alloc_size
= MIPS_MINIMAL_STACK_SIZE
+ pos
+ 64;
248 // align to MIPS_STACK_ALIGNMENT bytes
249 alloc_size
+= MIPS_STACK_ALIGNMENT
- 1;
250 alloc_size
&= ~(MIPS_STACK_ALIGNMENT
- 1);
252 g_assert ((alloc_size
& (MIPS_STACK_ALIGNMENT
-1)) == 0);
253 mips_addiu (code
, mips_sp
, mips_sp
, -alloc_size
);
254 mips_sw (code
, mips_ra
, mips_sp
, alloc_size
+ MIPS_RET_ADDR_OFFSET
);
256 /* Save all the regs on the stack */
257 for (i
= 0; i
< MONO_MAX_IREGS
; i
++) {
259 MIPS_SW (code
, i
, mips_sp
, i
*IREG_SIZE
+ MIPS_STACK_PARAM_OFFSET
);
261 mips_addiu (code
, mips_at
, mips_sp
, alloc_size
);
262 MIPS_SW (code
, mips_at
, mips_sp
, i
*IREG_SIZE
+ MIPS_STACK_PARAM_OFFSET
);
267 mips_move (code
, mips_a1
, mips_a0
);
268 mips_load (code
, mips_a0
, mono_defaults
.corlib
);
269 mips_load (code
, mips_t9
, mono_exception_from_token
);
270 mips_jalr (code
, mips_t9
, mips_ra
);
272 mips_move (code
, mips_a0
, mips_v0
);
274 /* call throw_exception (exc, ip, sp, rethrow) */
276 /* exc is already in place in a0 */
280 mips_lw (code
, mips_a1
, mips_sp
, alloc_size
+ MIPS_RET_ADDR_OFFSET
);
282 mips_move (code
, mips_a1
, mips_ra
);
284 /* current sp & rethrow */
285 mips_move (code
, mips_a2
, mips_sp
);
286 mips_addiu (code
, mips_a3
, mips_zero
, rethrow
);
288 mips_load (code
, mips_t9
, throw_exception
);
289 mips_jr (code
, mips_t9
);
291 /* we should never reach this breakpoint */
292 mips_break (code
, 0xfe);
294 g_assert ((code
- start
) < size
);
295 mono_arch_flush_icache (start
, code
- start
);
296 MONO_PROFILER_RAISE (jit_code_buffer
, (start
, code
- start
, MONO_PROFILER_CODE_BUFFER_EXCEPTION_HANDLING
, NULL
));
301 * mono_arch_get_rethrow_exception:
302 * \returns a function pointer which can be used to rethrow
303 * exceptions. The returned function has the following
304 * signature: void (*func) (MonoException *exc);
307 mono_arch_get_rethrow_exception (MonoTrampInfo
**info
, gboolean aot
)
309 static guint8 start
[GENERIC_EXCEPTION_SIZE
];
310 static int inited
= 0;
318 mono_arch_get_throw_exception_generic (start
, sizeof (start
), FALSE
, TRUE
, FALSE
);
324 * mono_arch_get_rethrow_preserve_exception:
325 * \returns a function pointer which can be used to rethrow
326 * exceptions while avoiding modification of saved trace_ips.
327 * The returned function has the following
328 * signature: void (*func) (MonoException *exc);
331 mono_arch_get_rethrow_preserve_exception (MonoTrampInfo
**info
, gboolean aot
)
333 static guint8 start
[GENERIC_EXCEPTION_SIZE
];
334 static int inited
= 0;
342 mono_arch_get_throw_exception_generic (start
, sizeof (start
), FALSE
, TRUE
, TRUE
);
348 * arch_get_throw_exception:
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);
353 * For example to raise an arithmetic exception you can use:
355 * x86_push_imm (code, mono_get_exception_arithmetic ());
356 * x86_call_code (code, arch_get_throw_exception ());
360 mono_arch_get_throw_exception (MonoTrampInfo
**info
, gboolean aot
)
362 static guint8 start
[GENERIC_EXCEPTION_SIZE
];
363 static int inited
= 0;
371 mono_arch_get_throw_exception_generic (start
, sizeof (start
), FALSE
, FALSE
, FALSE
);
377 mono_arch_get_throw_exception_by_name (void)
379 guint8
*start
, *code
;
382 /* Not used on MIPS */
383 start
= code
= mono_global_codeman_reserve (size
);
384 mips_break (code
, 0xfd);
385 mono_arch_flush_icache (start
, code
- start
);
386 MONO_PROFILER_RAISE (jit_code_buffer
, (start
, code
- start
, MONO_PROFILER_CODE_BUFFER_EXCEPTION_HANDLING
, NULL
));
391 * mono_arch_get_throw_corlib_exception:
392 * \returns a function pointer which can be used to raise
393 * corlib exceptions. The returned function has the following
394 * signature: void (*func) (guint32 ex_token, guint32 offset);
395 * On MIPS, the offset argument is missing.
398 mono_arch_get_throw_corlib_exception (MonoTrampInfo
**info
, gboolean aot
)
400 static guint8 start
[GENERIC_EXCEPTION_SIZE
];
401 static int inited
= 0;
409 mono_arch_get_throw_exception_generic (start
, sizeof (start
), TRUE
, FALSE
, FALSE
);
415 * mono_arch_unwind_frame:
417 * This function is used to gather information from @ctx, and store it in @frame_info.
418 * It unwinds one stack frame, and stores the resulting context into @new_ctx. @lmf
419 * is modified if needed.
420 * Returns TRUE on success, FALSE otherwise.
423 mono_arch_unwind_frame (MonoDomain
*domain
, MonoJitTlsData
*jit_tls
,
424 MonoJitInfo
*ji
, MonoContext
*ctx
,
425 MonoContext
*new_ctx
, MonoLMF
**lmf
,
426 host_mgreg_t
**save_locations
,
427 StackFrameInfo
*frame
)
429 memset (frame
, 0, sizeof (StackFrameInfo
));
436 gpointer ip
= MONO_CONTEXT_GET_IP (ctx
);
437 host_mgreg_t regs
[MONO_MAX_IREGS
+ 1];
439 guint32 unwind_info_len
;
442 if (ji
->is_trampoline
)
443 frame
->type
= FRAME_TYPE_TRAMPOLINE
;
445 frame
->type
= FRAME_TYPE_MANAGED
;
447 unwind_info
= mono_jinfo_get_unwind_info (ji
, &unwind_info_len
);
449 for (i
= 0; i
< MONO_MAX_IREGS
; ++i
)
450 regs
[i
] = new_ctx
->sc_regs
[i
];
452 gboolean success
= mono_unwind_frame (unwind_info
, unwind_info_len
, ji
->code_start
,
453 (guint8
*)ji
->code_start
+ ji
->code_size
,
454 ip
, NULL
, regs
, MONO_MAX_IREGS
,
455 save_locations
, MONO_MAX_IREGS
, &cfa
);
460 for (i
= 0; i
< MONO_MAX_IREGS
; ++i
)
461 new_ctx
->sc_regs
[i
] = regs
[i
];
462 new_ctx
->sc_pc
= regs
[mips_ra
];
463 new_ctx
->sc_regs
[mips_sp
] = (host_mgreg_t
)(gsize
)cfa
;
465 /* we substract 8, so that the IP points into the call instruction */
466 MONO_CONTEXT_SET_IP (new_ctx
, new_ctx
->sc_pc
- 8);
468 /* Sanity check -- we should have made progress here */
469 g_assert (MONO_CONTEXT_GET_SP (new_ctx
) != MONO_CONTEXT_GET_SP (ctx
));
472 g_assert ((((guint64
)(*lmf
)->previous_lmf
) & 2) == 0);
474 if (!(*lmf
)->method
) {
475 #ifdef DEBUG_EXCEPTIONS
476 g_print ("mono_arch_unwind_frame: bad lmf @ %p\n", (void *) *lmf
);
480 g_assert (((*lmf
)->magic
== MIPS_LMF_MAGIC1
) || ((*lmf
)->magic
== MIPS_LMF_MAGIC2
));
482 ji
= mini_jit_info_table_find (domain
, (gpointer
)(*lmf
)->eip
, NULL
);
484 // FIXME: This can happen with multiple appdomains (bug #444383)
489 frame
->type
= FRAME_TYPE_MANAGED_TO_NATIVE
;
491 memcpy (&new_ctx
->sc_regs
, (*lmf
)->iregs
, sizeof (gulong
) * MONO_SAVED_GREGS
);
492 memcpy (&new_ctx
->sc_fpregs
, (*lmf
)->fregs
, sizeof (float) * MONO_SAVED_FREGS
);
493 MONO_CONTEXT_SET_IP (new_ctx
, (*lmf
)->eip
);
494 /* ensure that we've made progress */
495 g_assert (new_ctx
->sc_pc
!= ctx
->sc_pc
);
497 *lmf
= (gpointer
)(((gsize
)(*lmf
)->previous_lmf
) & ~3);
506 mono_arch_ip_from_context (void *sigctx
)
508 return (gpointer
)(gsize
)UCONTEXT_REG_PC (sigctx
);
514 * Called by resuming from a signal handler.
517 handle_signal_exception (gpointer obj
)
519 MonoJitTlsData
*jit_tls
= mono_tls_get_jit_tls ();
522 memcpy (&ctx
, &jit_tls
->ex_ctx
, sizeof (MonoContext
));
524 mono_handle_exception (&ctx
, obj
);
526 mono_restore_context (&ctx
);
530 * This is the function called from the signal handler
533 mono_arch_handle_exception (void *ctx
, gpointer obj
)
535 #if defined(MONO_CROSS_COMPILE)
536 g_assert_not_reached ();
537 #elif defined(MONO_ARCH_USE_SIGACTION)
541 * Handling the exception in the signal handler is problematic, since the original
542 * signal is disabled, and we could run arbitrary code though the debugger. So
543 * resume into the normal stack and do most work there if possible.
545 MonoJitTlsData
*jit_tls
= mono_tls_get_jit_tls ();
546 guint64 sp
= UCONTEXT_GREGS (sigctx
) [mips_sp
];
548 /* Pass the ctx parameter in TLS */
549 mono_sigctx_to_monoctx (sigctx
, &jit_tls
->ex_ctx
);
550 /* The others in registers */
551 UCONTEXT_GREGS (sigctx
)[mips_a0
] = (gsize
)obj
;
553 /* Allocate a stack frame */
555 UCONTEXT_GREGS (sigctx
)[mips_sp
] = sp
;
557 UCONTEXT_REG_PC (sigctx
) = (gsize
)handle_signal_exception
;
564 mono_sigctx_to_monoctx (ctx
, &mctx
);
566 result
= mono_handle_exception (&mctx
, obj
);
567 /* restore the context so that returning from the signal handler will invoke
570 mono_monoctx_to_sigctx (&mctx
, ctx
);
576 * mono_arch_setup_resume_sighandler_ctx:
578 * Setup CTX so execution continues at FUNC.
581 mono_arch_setup_resume_sighandler_ctx (MonoContext
*ctx
, gpointer func
)
583 MONO_CONTEXT_SET_IP (ctx
,func
);