2 * exceptions-mips.c: exception support for MIPS
5 * Mark Mason (mason@broadcom.com)
7 * Based on exceptions-ppc.c by:
8 * Dietmar Maurer (dietmar@ximian.com)
9 * Paolo Molaro (lupus@ximian.com)
12 * (C) 2001 Ximian, Inc.
20 #include <mono/arch/mips/mips-codegen.h>
21 #include <mono/metadata/appdomain.h>
22 #include <mono/metadata/tabledefs.h>
23 #include <mono/metadata/threads.h>
24 #include <mono/metadata/debug-helpers.h>
25 #include <mono/metadata/exception.h>
26 #include <mono/metadata/mono-debug.h>
29 #include "mini-mips.h"
31 #define GENERIC_EXCEPTION_SIZE 256
34 * mono_arch_get_restore_context:
36 * Returns a pointer to a method which restores a previously saved MonoContext.
37 * The first argument in a0 is the pointer to the MonoContext.
40 mono_arch_get_restore_context (MonoTrampInfo **info, gboolean aot)
44 static guint8 start [512];
45 static int inited = 0;
46 guint32 iregs_to_restore;
57 mips_move (code, mips_at, mips_a0);
59 iregs_to_restore = (MONO_ARCH_CALLEE_SAVED_REGS \
60 | (1 << mips_sp) | (1 << mips_ra));
61 for (i = 0; i < MONO_SAVED_GREGS; ++i) {
62 //if (iregs_to_restore & (1 << i)) {
63 if (i != mips_zero && i != mips_at) {
64 MIPS_LW (code, i, mips_at, G_STRUCT_OFFSET (MonoContext, sc_regs[i]));
68 /* Get the address to return to */
69 mips_lw (code, mips_t9, mips_at, G_STRUCT_OFFSET (MonoContext, sc_pc));
71 /* jump to the saved IP */
72 mips_jr (code, mips_t9);
76 mips_break (code, 0xff);
78 g_assert ((code - start) < sizeof(start));
79 mono_arch_flush_icache (start, code - start);
80 mono_profiler_code_buffer_new (start, code - start, MONO_PROFILER_CODE_BUFFER_EXCEPTION_HANDLING, NULL);
85 * mono_arch_get_call_filter:
87 * Returns a pointer to a method which calls an exception filter. We
88 * also use this function to call finally handlers (we pass NULL as
89 * @exc object in this case).
91 * This function is invoked as
92 * call_handler (MonoContext *ctx, handler)
94 * Where 'handler' is a function to be invoked as:
98 mono_arch_get_call_filter (MonoTrampInfo **info, gboolean aot)
100 static guint8 start [320];
101 static int inited = 0;
117 g_assert ((alloc_size & (MIPS_STACK_ALIGNMENT-1)) == 0);
119 mips_addiu (code, mips_sp, mips_sp, -alloc_size);
120 mips_sw (code, mips_ra, mips_sp, alloc_size + MIPS_RET_ADDR_OFFSET);
122 /* Save global registers on stack (s0 - s7) */
124 MIPS_SW (code, mips_s0, mips_sp, offset); offset += IREG_SIZE;
125 MIPS_SW (code, mips_s1, mips_sp, offset); offset += IREG_SIZE;
126 MIPS_SW (code, mips_s2, mips_sp, offset); offset += IREG_SIZE;
127 MIPS_SW (code, mips_s3, mips_sp, offset); offset += IREG_SIZE;
128 MIPS_SW (code, mips_s4, mips_sp, offset); offset += IREG_SIZE;
129 MIPS_SW (code, mips_s5, mips_sp, offset); offset += IREG_SIZE;
130 MIPS_SW (code, mips_s6, mips_sp, offset); offset += IREG_SIZE;
131 MIPS_SW (code, mips_s7, mips_sp, offset); offset += IREG_SIZE;
132 MIPS_SW (code, mips_fp, mips_sp, offset); offset += IREG_SIZE;
134 /* Restore global registers from MonoContext, including the frame pointer */
135 MIPS_LW (code, mips_s0, mips_a0, G_STRUCT_OFFSET (MonoContext, sc_regs[mips_s0]));
136 MIPS_LW (code, mips_s1, mips_a0, G_STRUCT_OFFSET (MonoContext, sc_regs[mips_s1]));
137 MIPS_LW (code, mips_s2, mips_a0, G_STRUCT_OFFSET (MonoContext, sc_regs[mips_s2]));
138 MIPS_LW (code, mips_s3, mips_a0, G_STRUCT_OFFSET (MonoContext, sc_regs[mips_s3]));
139 MIPS_LW (code, mips_s4, mips_a0, G_STRUCT_OFFSET (MonoContext, sc_regs[mips_s4]));
140 MIPS_LW (code, mips_s5, mips_a0, G_STRUCT_OFFSET (MonoContext, sc_regs[mips_s5]));
141 MIPS_LW (code, mips_s6, mips_a0, G_STRUCT_OFFSET (MonoContext, sc_regs[mips_s6]));
142 MIPS_LW (code, mips_s7, mips_a0, G_STRUCT_OFFSET (MonoContext, sc_regs[mips_s7]));
143 MIPS_LW (code, mips_fp, mips_a0, G_STRUCT_OFFSET (MonoContext, sc_regs[mips_fp]));
145 /* a1 is the handler to call */
146 mips_move (code, mips_t9, mips_a1);
148 /* jump to the saved IP */
149 mips_jalr (code, mips_t9, mips_ra);
152 /* restore all regs from the stack */
154 MIPS_LW (code, mips_s0, mips_sp, offset); offset += IREG_SIZE;
155 MIPS_LW (code, mips_s1, mips_sp, offset); offset += IREG_SIZE;
156 MIPS_LW (code, mips_s2, mips_sp, offset); offset += IREG_SIZE;
157 MIPS_LW (code, mips_s3, mips_sp, offset); offset += IREG_SIZE;
158 MIPS_LW (code, mips_s4, mips_sp, offset); offset += IREG_SIZE;
159 MIPS_LW (code, mips_s5, mips_sp, offset); offset += IREG_SIZE;
160 MIPS_LW (code, mips_s6, mips_sp, offset); offset += IREG_SIZE;
161 MIPS_LW (code, mips_s7, mips_sp, offset); offset += IREG_SIZE;
162 MIPS_LW (code, mips_fp, mips_sp, offset); offset += IREG_SIZE;
165 mips_lw (code, mips_ra, mips_sp, alloc_size + MIPS_RET_ADDR_OFFSET);
166 mips_addiu (code, mips_sp, mips_sp, alloc_size);
167 mips_jr (code, mips_ra);
170 g_assert ((code - start) < sizeof(start));
171 mono_arch_flush_icache (start, code - start);
172 mono_profiler_code_buffer_new (start, code - start, MONO_PROFILER_CODE_BUFFER_EXCEPTION_HANDLING, NULL);
177 throw_exception (MonoObject *exc, unsigned long eip, unsigned long esp, gboolean rethrow)
181 #ifdef DEBUG_EXCEPTIONS
182 g_print ("throw_exception: exc=%p eip=%p esp=%p rethrow=%d\n",
183 exc, (void *)eip, (void *) esp, rethrow);
186 /* adjust eip so that it point into the call instruction */
189 memset (&ctx, 0, sizeof (MonoContext));
191 /*g_print ("stack in throw: %p\n", esp);*/
192 memcpy (&ctx.sc_regs, (void *)(esp + MIPS_STACK_PARAM_OFFSET),
193 sizeof (gulong) * MONO_SAVED_GREGS);
194 memset (&ctx.sc_fpregs, 0, sizeof (mips_freg) * MONO_SAVED_FREGS);
195 MONO_CONTEXT_SET_IP (&ctx, eip);
197 if (mono_object_isinst (exc, mono_defaults.exception_class)) {
198 MonoException *mono_ex = (MonoException*)exc;
200 mono_ex->stack_trace = NULL;
201 mono_ex->trace_ips = NULL;
204 mono_handle_exception (&ctx, exc);
205 #ifdef DEBUG_EXCEPTIONS
206 g_print ("throw_exception: restore to pc=%p sp=%p fp=%p ctx=%p\n",
207 (void *) ctx.sc_pc, (void *) ctx.sc_regs[mips_sp],
208 (void *) ctx.sc_regs[mips_fp], &ctx);
210 mono_restore_context (&ctx);
212 g_assert_not_reached ();
216 * arch_get_throw_exception_generic:
218 * Returns a function pointer which can be used to raise
219 * exceptions. The returned function has the following
220 * signature: void (*func) (MonoException *exc); or
221 * void (*func) (char *exc_name);
225 mono_arch_get_throw_exception_generic (guint8 *start, int size, int corlib, gboolean rethrow)
228 int alloc_size, pos, i;
232 //g_print ("mono_arch_get_throw_exception_generic: code=%p\n", code);
235 /* XXX - save all the FP regs on the stack ? */
237 pos += MONO_MAX_IREGS * sizeof(guint32);
239 alloc_size = MIPS_MINIMAL_STACK_SIZE + pos + 64;
240 // align to MIPS_STACK_ALIGNMENT bytes
241 alloc_size += MIPS_STACK_ALIGNMENT - 1;
242 alloc_size &= ~(MIPS_STACK_ALIGNMENT - 1);
244 g_assert ((alloc_size & (MIPS_STACK_ALIGNMENT-1)) == 0);
245 mips_addiu (code, mips_sp, mips_sp, -alloc_size);
246 mips_sw (code, mips_ra, mips_sp, alloc_size + MIPS_RET_ADDR_OFFSET);
248 /* Save all the regs on the stack */
249 for (i = 0; i < MONO_MAX_IREGS; i++) {
251 MIPS_SW (code, i, mips_sp, i*IREG_SIZE + MIPS_STACK_PARAM_OFFSET);
253 mips_addiu (code, mips_at, mips_sp, alloc_size);
254 MIPS_SW (code, mips_at, mips_sp, i*IREG_SIZE + MIPS_STACK_PARAM_OFFSET);
259 mips_move (code, mips_a1, mips_a0);
260 mips_load (code, mips_a0, mono_defaults.corlib);
261 mips_load (code, mips_t9, mono_exception_from_token);
262 mips_jalr (code, mips_t9, mips_ra);
264 mips_move (code, mips_a0, mips_v0);
266 /* call throw_exception (exc, ip, sp, rethrow) */
268 /* exc is already in place in a0 */
272 mips_lw (code, mips_a1, mips_sp, alloc_size + MIPS_RET_ADDR_OFFSET);
274 mips_move (code, mips_a1, mips_ra);
276 /* current sp & rethrow */
277 mips_move (code, mips_a2, mips_sp);
278 mips_addiu (code, mips_a3, mips_zero, rethrow);
280 mips_load (code, mips_t9, throw_exception);
281 mips_jr (code, mips_t9);
283 /* we should never reach this breakpoint */
284 mips_break (code, 0xfe);
286 g_assert ((code - start) < size);
287 mono_arch_flush_icache (start, code - start);
288 mono_profiler_code_buffer_new (start, code - start, MONO_PROFILER_CODE_BUFFER_EXCEPTION_HANDLING, NULL);
293 * mono_arch_get_rethrow_exception:
295 * Returns a function pointer which can be used to rethrow
296 * exceptions. The returned function has the following
297 * signature: void (*func) (MonoException *exc);
301 mono_arch_get_rethrow_exception (MonoTrampInfo **info, gboolean aot)
303 static guint8 start [GENERIC_EXCEPTION_SIZE];
304 static int inited = 0;
312 mono_arch_get_throw_exception_generic (start, sizeof (start), FALSE, TRUE);
318 * arch_get_throw_exception:
320 * Returns a function pointer which can be used to raise
321 * exceptions. The returned function has the following
322 * signature: void (*func) (MonoException *exc);
323 * For example to raise an arithmetic exception you can use:
325 * x86_push_imm (code, mono_get_exception_arithmetic ());
326 * x86_call_code (code, arch_get_throw_exception ());
330 mono_arch_get_throw_exception (MonoTrampInfo **info, gboolean aot)
332 static guint8 start [GENERIC_EXCEPTION_SIZE];
333 static int inited = 0;
341 mono_arch_get_throw_exception_generic (start, sizeof (start), FALSE, FALSE);
347 mono_arch_get_throw_exception_by_name (void)
349 guint8 *start, *code;
352 /* Not used on MIPS */
353 start = code = mono_global_codeman_reserve (size);
354 mips_break (code, 0xfd);
355 mono_arch_flush_icache (start, code - start);
356 mono_profiler_code_buffer_new (start, code - start, MONO_PROFILER_CODE_BUFFER_EXCEPTION_HANDLING, NULL);
361 * mono_arch_get_throw_corlib_exception:
363 * Returns a function pointer which can be used to raise
364 * corlib exceptions. The returned function has the following
365 * signature: void (*func) (guint32 ex_token, guint32 offset);
366 * On MIPS, the offset argument is missing.
369 mono_arch_get_throw_corlib_exception (MonoTrampInfo **info, gboolean aot)
371 static guint8 start [GENERIC_EXCEPTION_SIZE];
372 static int inited = 0;
380 mono_arch_get_throw_exception_generic (start, sizeof (start), TRUE, FALSE);
386 * mono_arch_unwind_frame:
388 * This function is used to gather information from @ctx, and store it in @frame_info.
389 * It unwinds one stack frame, and stores the resulting context into @new_ctx. @lmf
390 * is modified if needed.
391 * Returns TRUE on success, FALSE otherwise.
394 mono_arch_unwind_frame (MonoDomain *domain, MonoJitTlsData *jit_tls,
395 MonoJitInfo *ji, MonoContext *ctx,
396 MonoContext *new_ctx, MonoLMF **lmf,
397 mgreg_t **save_locations,
398 StackFrameInfo *frame)
400 memset (frame, 0, sizeof (StackFrameInfo));
407 gpointer ip = MONO_CONTEXT_GET_IP (ctx);
408 mgreg_t regs [MONO_MAX_IREGS + 1];
410 guint32 unwind_info_len;
413 if (ji->is_trampoline)
414 frame->type = FRAME_TYPE_TRAMPOLINE;
416 frame->type = FRAME_TYPE_MANAGED;
418 unwind_info = mono_jinfo_get_unwind_info (ji, &unwind_info_len);
420 for (i = 0; i < MONO_MAX_IREGS; ++i)
421 regs [i] = new_ctx->sc_regs [i];
423 mono_unwind_frame (unwind_info, unwind_info_len, ji->code_start,
424 (guint8*)ji->code_start + ji->code_size,
425 ip, NULL, regs, MONO_MAX_IREGS,
426 save_locations, MONO_MAX_IREGS, &cfa);
428 for (i = 0; i < MONO_MAX_IREGS; ++i)
429 new_ctx->sc_regs [i] = regs [i];
430 new_ctx->sc_pc = regs [mips_ra];
431 new_ctx->sc_regs [mips_sp] = (mgreg_t)cfa;
433 /* we substract 8, so that the IP points into the call instruction */
434 MONO_CONTEXT_SET_IP (new_ctx, new_ctx->sc_pc - 8);
436 /* Sanity check -- we should have made progress here */
437 g_assert (MONO_CONTEXT_GET_SP (new_ctx) != MONO_CONTEXT_GET_SP (ctx));
441 if (((mgreg_t)(*lmf)->previous_lmf) & 2) {
443 * This LMF entry is created by the soft debug code to mark transitions to
444 * managed code done during invokes.
446 MonoLMFExt *ext = (MonoLMFExt*)(*lmf);
448 g_assert (ext->debugger_invoke);
450 memcpy (new_ctx, &ext->ctx, sizeof (MonoContext));
452 *lmf = (gpointer)(((gsize)(*lmf)->previous_lmf) & ~3);
454 frame->type = FRAME_TYPE_DEBUGGER_INVOKE;
459 if (!(*lmf)->method) {
460 #ifdef DEBUG_EXCEPTIONS
461 g_print ("mono_arch_unwind_frame: bad lmf @ %p\n", (void *) *lmf);
465 g_assert (((*lmf)->magic == MIPS_LMF_MAGIC1) || ((*lmf)->magic == MIPS_LMF_MAGIC2));
467 ji = mini_jit_info_table_find (domain, (gpointer)(*lmf)->eip, NULL);
469 // FIXME: This can happen with multiple appdomains (bug #444383)
474 frame->type = FRAME_TYPE_MANAGED_TO_NATIVE;
476 memcpy (&new_ctx->sc_regs, (*lmf)->iregs, sizeof (gulong) * MONO_SAVED_GREGS);
477 memcpy (&new_ctx->sc_fpregs, (*lmf)->fregs, sizeof (float) * MONO_SAVED_FREGS);
478 MONO_CONTEXT_SET_IP (new_ctx, (*lmf)->eip);
479 /* ensure that we've made progress */
480 g_assert (new_ctx->sc_pc != ctx->sc_pc);
482 *lmf = (gpointer)(((gsize)(*lmf)->previous_lmf) & ~3);
491 mono_arch_ip_from_context (void *sigctx)
493 return (gpointer)(gsize)UCONTEXT_REG_PC (sigctx);
499 * Called by resuming from a signal handler.
502 handle_signal_exception (gpointer obj)
504 MonoJitTlsData *jit_tls = mono_native_tls_get_value (mono_jit_tls_id);
507 memcpy (&ctx, &jit_tls->ex_ctx, sizeof (MonoContext));
509 mono_handle_exception (&ctx, obj);
511 mono_restore_context (&ctx);
515 * This is the function called from the signal handler
518 mono_arch_handle_exception (void *ctx, gpointer obj)
520 #if defined(MONO_CROSS_COMPILE)
521 g_assert_not_reached ();
522 #elif defined(MONO_ARCH_USE_SIGACTION)
526 * Handling the exception in the signal handler is problematic, since the original
527 * signal is disabled, and we could run arbitrary code though the debugger. So
528 * resume into the normal stack and do most work there if possible.
530 MonoJitTlsData *jit_tls = mono_native_tls_get_value (mono_jit_tls_id);
531 guint64 sp = UCONTEXT_GREGS (sigctx) [mips_sp];
533 /* Pass the ctx parameter in TLS */
534 mono_sigctx_to_monoctx (sigctx, &jit_tls->ex_ctx);
535 /* The others in registers */
536 UCONTEXT_GREGS (sigctx)[mips_a0] = (gsize)obj;
538 /* Allocate a stack frame */
540 UCONTEXT_GREGS (sigctx)[mips_sp] = sp;
542 UCONTEXT_REG_PC (sigctx) = (gsize)handle_signal_exception;
549 mono_sigctx_to_monoctx (ctx, &mctx);
551 result = mono_handle_exception (&mctx, obj);
552 /* restore the context so that returning from the signal handler will invoke
555 mono_monoctx_to_sigctx (&mctx, ctx);
561 * mono_arch_setup_resume_sighandler_ctx:
563 * Setup CTX so execution continues at FUNC.
566 mono_arch_setup_resume_sighandler_ctx (MonoContext *ctx, gpointer func)
568 MONO_CONTEXT_SET_IP (ctx,func);