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"
34 #define GENERIC_EXCEPTION_SIZE 256
37 * mono_arch_get_restore_context:
39 * Returns a pointer to a method which restores a previously saved MonoContext.
40 * The first argument in a0 is the pointer to the MonoContext.
43 mono_arch_get_restore_context (MonoTrampInfo
**info
, gboolean aot
)
47 static guint8 start
[512];
48 static int inited
= 0;
49 guint32 iregs_to_restore
;
60 mips_move (code
, mips_at
, mips_a0
);
62 iregs_to_restore
= (MONO_ARCH_CALLEE_SAVED_REGS \
63 | (1 << mips_sp
) | (1 << mips_ra
));
64 for (i
= 0; i
< MONO_SAVED_GREGS
; ++i
) {
65 //if (iregs_to_restore & (1 << i)) {
66 if (i
!= mips_zero
&& i
!= mips_at
) {
67 MIPS_LW (code
, i
, mips_at
, G_STRUCT_OFFSET (MonoContext
, sc_regs
[i
]));
71 /* Get the address to return to */
72 mips_lw (code
, mips_t9
, mips_at
, G_STRUCT_OFFSET (MonoContext
, sc_pc
));
74 /* jump to the saved IP */
75 mips_jr (code
, mips_t9
);
79 mips_break (code
, 0xff);
81 g_assert ((code
- start
) < sizeof(start
));
82 mono_arch_flush_icache (start
, code
- start
);
83 MONO_PROFILER_RAISE (jit_code_buffer
, (start
, code
- start
, MONO_PROFILER_CODE_BUFFER_EXCEPTION_HANDLING
, NULL
));
88 * mono_arch_get_call_filter:
90 * Returns a pointer to a method which calls an exception filter. We
91 * also use this function to call finally handlers (we pass NULL as
92 * @exc object in this case).
94 * This function is invoked as
95 * call_handler (MonoContext *ctx, handler)
97 * Where 'handler' is a function to be invoked as:
101 mono_arch_get_call_filter (MonoTrampInfo
**info
, gboolean aot
)
103 static guint8 start
[320];
104 static int inited
= 0;
120 g_assert ((alloc_size
& (MIPS_STACK_ALIGNMENT
-1)) == 0);
122 mips_addiu (code
, mips_sp
, mips_sp
, -alloc_size
);
123 mips_sw (code
, mips_ra
, mips_sp
, alloc_size
+ MIPS_RET_ADDR_OFFSET
);
125 /* Save global registers on stack (s0 - s7) */
127 MIPS_SW (code
, mips_s0
, mips_sp
, offset
); offset
+= IREG_SIZE
;
128 MIPS_SW (code
, mips_s1
, mips_sp
, offset
); offset
+= IREG_SIZE
;
129 MIPS_SW (code
, mips_s2
, mips_sp
, offset
); offset
+= IREG_SIZE
;
130 MIPS_SW (code
, mips_s3
, mips_sp
, offset
); offset
+= IREG_SIZE
;
131 MIPS_SW (code
, mips_s4
, mips_sp
, offset
); offset
+= IREG_SIZE
;
132 MIPS_SW (code
, mips_s5
, mips_sp
, offset
); offset
+= IREG_SIZE
;
133 MIPS_SW (code
, mips_s6
, mips_sp
, offset
); offset
+= IREG_SIZE
;
134 MIPS_SW (code
, mips_s7
, mips_sp
, offset
); offset
+= IREG_SIZE
;
135 MIPS_SW (code
, mips_fp
, mips_sp
, offset
); offset
+= IREG_SIZE
;
137 /* Restore global registers from MonoContext, including the frame pointer */
138 MIPS_LW (code
, mips_s0
, mips_a0
, G_STRUCT_OFFSET (MonoContext
, sc_regs
[mips_s0
]));
139 MIPS_LW (code
, mips_s1
, mips_a0
, G_STRUCT_OFFSET (MonoContext
, sc_regs
[mips_s1
]));
140 MIPS_LW (code
, mips_s2
, mips_a0
, G_STRUCT_OFFSET (MonoContext
, sc_regs
[mips_s2
]));
141 MIPS_LW (code
, mips_s3
, mips_a0
, G_STRUCT_OFFSET (MonoContext
, sc_regs
[mips_s3
]));
142 MIPS_LW (code
, mips_s4
, mips_a0
, G_STRUCT_OFFSET (MonoContext
, sc_regs
[mips_s4
]));
143 MIPS_LW (code
, mips_s5
, mips_a0
, G_STRUCT_OFFSET (MonoContext
, sc_regs
[mips_s5
]));
144 MIPS_LW (code
, mips_s6
, mips_a0
, G_STRUCT_OFFSET (MonoContext
, sc_regs
[mips_s6
]));
145 MIPS_LW (code
, mips_s7
, mips_a0
, G_STRUCT_OFFSET (MonoContext
, sc_regs
[mips_s7
]));
146 MIPS_LW (code
, mips_fp
, mips_a0
, G_STRUCT_OFFSET (MonoContext
, sc_regs
[mips_fp
]));
148 /* a1 is the handler to call */
149 mips_move (code
, mips_t9
, mips_a1
);
151 /* jump to the saved IP */
152 mips_jalr (code
, mips_t9
, mips_ra
);
155 /* restore all regs from the stack */
157 MIPS_LW (code
, mips_s0
, mips_sp
, offset
); offset
+= IREG_SIZE
;
158 MIPS_LW (code
, mips_s1
, mips_sp
, offset
); offset
+= IREG_SIZE
;
159 MIPS_LW (code
, mips_s2
, mips_sp
, offset
); offset
+= IREG_SIZE
;
160 MIPS_LW (code
, mips_s3
, mips_sp
, offset
); offset
+= IREG_SIZE
;
161 MIPS_LW (code
, mips_s4
, mips_sp
, offset
); offset
+= IREG_SIZE
;
162 MIPS_LW (code
, mips_s5
, mips_sp
, offset
); offset
+= IREG_SIZE
;
163 MIPS_LW (code
, mips_s6
, mips_sp
, offset
); offset
+= IREG_SIZE
;
164 MIPS_LW (code
, mips_s7
, mips_sp
, offset
); offset
+= IREG_SIZE
;
165 MIPS_LW (code
, mips_fp
, mips_sp
, offset
); offset
+= IREG_SIZE
;
168 mips_lw (code
, mips_ra
, mips_sp
, alloc_size
+ MIPS_RET_ADDR_OFFSET
);
169 mips_addiu (code
, mips_sp
, mips_sp
, alloc_size
);
170 mips_jr (code
, mips_ra
);
173 g_assert ((code
- start
) < sizeof(start
));
174 mono_arch_flush_icache (start
, code
- start
);
175 MONO_PROFILER_RAISE (jit_code_buffer
, (start
, code
- start
, MONO_PROFILER_CODE_BUFFER_EXCEPTION_HANDLING
, NULL
));
180 throw_exception (MonoObject
*exc
, unsigned long eip
, unsigned long esp
, gboolean rethrow
, gboolean preserve_ips
)
185 #ifdef DEBUG_EXCEPTIONS
186 g_print ("throw_exception: exc=%p eip=%p esp=%p rethrow=%d\n",
187 exc
, (void *)eip
, (void *) esp
, rethrow
);
190 /* adjust eip so that it point into the call instruction */
193 memset (&ctx
, 0, sizeof (MonoContext
));
195 /*g_print ("stack in throw: %p\n", esp);*/
196 memcpy (&ctx
.sc_regs
, (void *)(esp
+ MIPS_STACK_PARAM_OFFSET
),
197 sizeof (gulong
) * MONO_SAVED_GREGS
);
198 memset (&ctx
.sc_fpregs
, 0, sizeof (mips_freg
) * MONO_SAVED_FREGS
);
199 MONO_CONTEXT_SET_IP (&ctx
, eip
);
201 if (mono_object_isinst_checked (exc
, mono_defaults
.exception_class
, error
)) {
202 MonoException
*mono_ex
= (MonoException
*)exc
;
204 mono_ex
->stack_trace
= NULL
;
205 mono_ex
->trace_ips
= NULL
;
206 } if (preserve_ips
) {
207 mono_ex
->caught_in_unmanaged
= TRUE
;
210 mono_error_assert_ok (error
);
211 mono_handle_exception (&ctx
, exc
);
212 #ifdef DEBUG_EXCEPTIONS
213 g_print ("throw_exception: restore to pc=%p sp=%p fp=%p ctx=%p\n",
214 (void *) ctx
.sc_pc
, (void *) ctx
.sc_regs
[mips_sp
],
215 (void *) ctx
.sc_regs
[mips_fp
], &ctx
);
217 mono_restore_context (&ctx
);
219 g_assert_not_reached ();
223 * arch_get_throw_exception_generic:
225 * Returns a function pointer which can be used to raise
226 * exceptions. The returned function has the following
227 * signature: void (*func) (MonoException *exc); or
228 * void (*func) (char *exc_name);
232 mono_arch_get_throw_exception_generic (guint8
*start
, int size
, int corlib
, gboolean rethrow
, gboolean preserve_ips
)
235 int alloc_size
, pos
, i
;
239 //g_print ("mono_arch_get_throw_exception_generic: code=%p\n", code);
242 /* XXX - save all the FP regs on the stack ? */
244 pos
+= MONO_MAX_IREGS
* sizeof(guint32
);
246 alloc_size
= MIPS_MINIMAL_STACK_SIZE
+ pos
+ 64;
247 // align to MIPS_STACK_ALIGNMENT bytes
248 alloc_size
+= MIPS_STACK_ALIGNMENT
- 1;
249 alloc_size
&= ~(MIPS_STACK_ALIGNMENT
- 1);
251 g_assert ((alloc_size
& (MIPS_STACK_ALIGNMENT
-1)) == 0);
252 mips_addiu (code
, mips_sp
, mips_sp
, -alloc_size
);
253 mips_sw (code
, mips_ra
, mips_sp
, alloc_size
+ MIPS_RET_ADDR_OFFSET
);
255 /* Save all the regs on the stack */
256 for (i
= 0; i
< MONO_MAX_IREGS
; i
++) {
258 MIPS_SW (code
, i
, mips_sp
, i
*IREG_SIZE
+ MIPS_STACK_PARAM_OFFSET
);
260 mips_addiu (code
, mips_at
, mips_sp
, alloc_size
);
261 MIPS_SW (code
, mips_at
, mips_sp
, i
*IREG_SIZE
+ MIPS_STACK_PARAM_OFFSET
);
266 mips_move (code
, mips_a1
, mips_a0
);
267 mips_load (code
, mips_a0
, mono_defaults
.corlib
);
268 mips_load (code
, mips_t9
, mono_exception_from_token
);
269 mips_jalr (code
, mips_t9
, mips_ra
);
271 mips_move (code
, mips_a0
, mips_v0
);
273 /* call throw_exception (exc, ip, sp, rethrow) */
275 /* exc is already in place in a0 */
279 mips_lw (code
, mips_a1
, mips_sp
, alloc_size
+ MIPS_RET_ADDR_OFFSET
);
281 mips_move (code
, mips_a1
, mips_ra
);
283 /* current sp & rethrow */
284 mips_move (code
, mips_a2
, mips_sp
);
285 mips_addiu (code
, mips_a3
, mips_zero
, rethrow
);
287 mips_load (code
, mips_t9
, throw_exception
);
288 mips_jr (code
, mips_t9
);
290 /* we should never reach this breakpoint */
291 mips_break (code
, 0xfe);
293 g_assert ((code
- start
) < size
);
294 mono_arch_flush_icache (start
, code
- start
);
295 MONO_PROFILER_RAISE (jit_code_buffer
, (start
, code
- start
, MONO_PROFILER_CODE_BUFFER_EXCEPTION_HANDLING
, NULL
));
300 * mono_arch_get_rethrow_exception:
301 * \returns a function pointer which can be used to rethrow
302 * exceptions. The returned function has the following
303 * signature: void (*func) (MonoException *exc);
306 mono_arch_get_rethrow_exception (MonoTrampInfo
**info
, gboolean aot
)
308 static guint8 start
[GENERIC_EXCEPTION_SIZE
];
309 static int inited
= 0;
317 mono_arch_get_throw_exception_generic (start
, sizeof (start
), FALSE
, TRUE
, FALSE
);
323 * mono_arch_get_rethrow_preserve_exception:
324 * \returns a function pointer which can be used to rethrow
325 * exceptions while avoiding modification of saved trace_ips.
326 * The returned function has the following
327 * signature: void (*func) (MonoException *exc);
330 mono_arch_get_rethrow_preserve_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
, TRUE
, TRUE
);
347 * arch_get_throw_exception:
349 * Returns a function pointer which can be used to raise
350 * exceptions. The returned function has the following
351 * signature: void (*func) (MonoException *exc);
352 * For example to raise an arithmetic exception you can use:
354 * x86_push_imm (code, mono_get_exception_arithmetic ());
355 * x86_call_code (code, arch_get_throw_exception ());
359 mono_arch_get_throw_exception (MonoTrampInfo
**info
, gboolean aot
)
361 static guint8 start
[GENERIC_EXCEPTION_SIZE
];
362 static int inited
= 0;
370 mono_arch_get_throw_exception_generic (start
, sizeof (start
), FALSE
, FALSE
, FALSE
);
376 mono_arch_get_throw_exception_by_name (void)
378 guint8
*start
, *code
;
381 /* Not used on MIPS */
382 start
= code
= mono_global_codeman_reserve (size
);
383 mips_break (code
, 0xfd);
384 mono_arch_flush_icache (start
, code
- start
);
385 MONO_PROFILER_RAISE (jit_code_buffer
, (start
, code
- start
, MONO_PROFILER_CODE_BUFFER_EXCEPTION_HANDLING
, NULL
));
390 * mono_arch_get_throw_corlib_exception:
391 * \returns a function pointer which can be used to raise
392 * corlib exceptions. The returned function has the following
393 * signature: void (*func) (guint32 ex_token, guint32 offset);
394 * On MIPS, the offset argument is missing.
397 mono_arch_get_throw_corlib_exception (MonoTrampInfo
**info
, gboolean aot
)
399 static guint8 start
[GENERIC_EXCEPTION_SIZE
];
400 static int inited
= 0;
408 mono_arch_get_throw_exception_generic (start
, sizeof (start
), TRUE
, FALSE
, FALSE
);
414 * mono_arch_unwind_frame:
416 * This function is used to gather information from @ctx, and store it in @frame_info.
417 * It unwinds one stack frame, and stores the resulting context into @new_ctx. @lmf
418 * is modified if needed.
419 * Returns TRUE on success, FALSE otherwise.
422 mono_arch_unwind_frame (MonoDomain
*domain
, MonoJitTlsData
*jit_tls
,
423 MonoJitInfo
*ji
, MonoContext
*ctx
,
424 MonoContext
*new_ctx
, MonoLMF
**lmf
,
425 host_mgreg_t
**save_locations
,
426 StackFrameInfo
*frame
)
428 memset (frame
, 0, sizeof (StackFrameInfo
));
435 gpointer ip
= MONO_CONTEXT_GET_IP (ctx
);
436 host_mgreg_t regs
[MONO_MAX_IREGS
+ 1];
438 guint32 unwind_info_len
;
441 if (ji
->is_trampoline
)
442 frame
->type
= FRAME_TYPE_TRAMPOLINE
;
444 frame
->type
= FRAME_TYPE_MANAGED
;
446 unwind_info
= mono_jinfo_get_unwind_info (ji
, &unwind_info_len
);
448 for (i
= 0; i
< MONO_MAX_IREGS
; ++i
)
449 regs
[i
] = new_ctx
->sc_regs
[i
];
451 gboolean success
= mono_unwind_frame (unwind_info
, unwind_info_len
, ji
->code_start
,
452 (guint8
*)ji
->code_start
+ ji
->code_size
,
453 ip
, NULL
, regs
, MONO_MAX_IREGS
,
454 save_locations
, MONO_MAX_IREGS
, &cfa
);
459 for (i
= 0; i
< MONO_MAX_IREGS
; ++i
)
460 new_ctx
->sc_regs
[i
] = regs
[i
];
461 new_ctx
->sc_pc
= regs
[mips_ra
];
462 new_ctx
->sc_regs
[mips_sp
] = (host_mgreg_t
)(gsize
)cfa
;
464 /* we substract 8, so that the IP points into the call instruction */
465 MONO_CONTEXT_SET_IP (new_ctx
, new_ctx
->sc_pc
- 8);
467 /* Sanity check -- we should have made progress here */
468 g_assert (MONO_CONTEXT_GET_SP (new_ctx
) != MONO_CONTEXT_GET_SP (ctx
));
471 g_assert ((((guint64
)(*lmf
)->previous_lmf
) & 2) == 0);
473 if (!(*lmf
)->method
) {
474 #ifdef DEBUG_EXCEPTIONS
475 g_print ("mono_arch_unwind_frame: bad lmf @ %p\n", (void *) *lmf
);
479 g_assert (((*lmf
)->magic
== MIPS_LMF_MAGIC1
) || ((*lmf
)->magic
== MIPS_LMF_MAGIC2
));
481 ji
= mini_jit_info_table_find (domain
, (gpointer
)(*lmf
)->eip
, NULL
);
483 // FIXME: This can happen with multiple appdomains (bug #444383)
488 frame
->type
= FRAME_TYPE_MANAGED_TO_NATIVE
;
490 memcpy (&new_ctx
->sc_regs
, (*lmf
)->iregs
, sizeof (gulong
) * MONO_SAVED_GREGS
);
491 memcpy (&new_ctx
->sc_fpregs
, (*lmf
)->fregs
, sizeof (float) * MONO_SAVED_FREGS
);
492 MONO_CONTEXT_SET_IP (new_ctx
, (*lmf
)->eip
);
493 /* ensure that we've made progress */
494 g_assert (new_ctx
->sc_pc
!= ctx
->sc_pc
);
496 *lmf
= (gpointer
)(((gsize
)(*lmf
)->previous_lmf
) & ~3);
505 mono_arch_ip_from_context (void *sigctx
)
507 return (gpointer
)(gsize
)UCONTEXT_REG_PC (sigctx
);
513 * Called by resuming from a signal handler.
516 handle_signal_exception (gpointer obj
)
518 MonoJitTlsData
*jit_tls
= mono_tls_get_jit_tls ();
521 memcpy (&ctx
, &jit_tls
->ex_ctx
, sizeof (MonoContext
));
523 mono_handle_exception (&ctx
, obj
);
525 mono_restore_context (&ctx
);
529 * This is the function called from the signal handler
532 mono_arch_handle_exception (void *ctx
, gpointer obj
)
534 #if defined(MONO_CROSS_COMPILE)
535 g_assert_not_reached ();
536 #elif defined(MONO_ARCH_USE_SIGACTION)
540 * Handling the exception in the signal handler is problematic, since the original
541 * signal is disabled, and we could run arbitrary code though the debugger. So
542 * resume into the normal stack and do most work there if possible.
544 MonoJitTlsData
*jit_tls
= mono_tls_get_jit_tls ();
545 guint64 sp
= UCONTEXT_GREGS (sigctx
) [mips_sp
];
547 /* Pass the ctx parameter in TLS */
548 mono_sigctx_to_monoctx (sigctx
, &jit_tls
->ex_ctx
);
549 /* The others in registers */
550 UCONTEXT_GREGS (sigctx
)[mips_a0
] = (gsize
)obj
;
552 /* Allocate a stack frame */
554 UCONTEXT_GREGS (sigctx
)[mips_sp
] = sp
;
556 UCONTEXT_REG_PC (sigctx
) = (gsize
)handle_signal_exception
;
563 mono_sigctx_to_monoctx (ctx
, &mctx
);
565 result
= mono_handle_exception (&mctx
, obj
);
566 /* restore the context so that returning from the signal handler will invoke
569 mono_monoctx_to_sigctx (&mctx
, ctx
);
575 * mono_arch_setup_resume_sighandler_ctx:
577 * Setup CTX so execution continues at FUNC.
580 mono_arch_setup_resume_sighandler_ctx (MonoContext
*ctx
, gpointer func
)
582 MONO_CONTEXT_SET_IP (ctx
,func
);