[wasm] Add a --runtime-arg= argument to runtime-tests.js to allow setting runtime...
[mono-project.git] / mono / mini / exceptions-mips.c
blobd0a325d7367cf73876b75df5210128a49fa29f83
1 /**
2 * \file
3 * exception support for MIPS
5 * Authors:
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)
12 * (C) 2006 Broadcom
13 * (C) 2001 Ximian, Inc.
16 #include <config.h>
17 #include <glib.h>
18 #include <signal.h>
19 #include <string.h>
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>
29 #include "mini.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.
42 gpointer
43 mono_arch_get_restore_context (MonoTrampInfo **info, gboolean aot)
45 int i;
46 guint8 *code;
47 static guint8 start [512];
48 static int inited = 0;
49 guint32 iregs_to_restore;
51 g_assert (!aot);
52 if (info)
53 *info = NULL;
55 if (inited)
56 return start;
57 inited = 1;
58 code = start;
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);
76 mips_nop (code);
78 /* never reached */
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));
84 return start;
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:
98 * handler (void)
100 gpointer
101 mono_arch_get_call_filter (MonoTrampInfo **info, gboolean aot)
103 static guint8 start [320];
104 static int inited = 0;
105 guint8 *code;
106 int alloc_size;
107 int offset;
109 g_assert (!aot);
110 if (info)
111 *info = NULL;
113 if (inited)
114 return start;
116 inited = 1;
117 code = start;
119 alloc_size = 64;
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) */
126 offset = 16;
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);
153 mips_nop (code);
155 /* restore all regs from the stack */
156 offset = 16;
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;
167 /* epilog */
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);
171 mips_nop (code);
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));
176 return start;
179 static void
180 throw_exception (MonoObject *exc, unsigned long eip, unsigned long esp, gboolean rethrow, gboolean preserve_ips)
182 ERROR_DECL (error);
183 MonoContext ctx;
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);
188 #endif
190 /* adjust eip so that it point into the call instruction */
191 eip -= 8;
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;
203 if (!rethrow) {
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);
216 #endif
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);
231 static gpointer
232 mono_arch_get_throw_exception_generic (guint8 *start, int size, int corlib, gboolean rethrow, gboolean preserve_ips)
234 guint8 *code;
235 int alloc_size, pos, i;
237 code = start;
239 //g_print ("mono_arch_get_throw_exception_generic: code=%p\n", code);
241 pos = 0;
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++) {
257 if (i != mips_sp)
258 MIPS_SW (code, i, mips_sp, i*IREG_SIZE + MIPS_STACK_PARAM_OFFSET);
259 else {
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);
265 if (corlib) {
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);
270 mips_nop (code);
271 mips_move (code, mips_a0, mips_v0);
273 /* call throw_exception (exc, ip, sp, rethrow) */
275 /* exc is already in place in a0 */
277 /* pointer to ip */
278 if (corlib)
279 mips_lw (code, mips_a1, mips_sp, alloc_size + MIPS_RET_ADDR_OFFSET);
280 else
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);
289 mips_nop (code);
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));
296 return start;
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);
305 gpointer
306 mono_arch_get_rethrow_exception (MonoTrampInfo **info, gboolean aot)
308 static guint8 start [GENERIC_EXCEPTION_SIZE];
309 static int inited = 0;
311 g_assert (!aot);
312 if (info)
313 *info = NULL;
315 if (inited)
316 return start;
317 mono_arch_get_throw_exception_generic (start, sizeof (start), FALSE, TRUE, FALSE);
318 inited = 1;
319 return start;
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);
329 gpointer
330 mono_arch_get_rethrow_preserve_exception (MonoTrampInfo **info, gboolean aot)
332 static guint8 start [GENERIC_EXCEPTION_SIZE];
333 static int inited = 0;
335 g_assert (!aot);
336 if (info)
337 *info = NULL;
339 if (inited)
340 return start;
341 mono_arch_get_throw_exception_generic (start, sizeof (start), FALSE, TRUE, TRUE);
342 inited = 1;
343 return start;
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 ());
358 gpointer
359 mono_arch_get_throw_exception (MonoTrampInfo **info, gboolean aot)
361 static guint8 start [GENERIC_EXCEPTION_SIZE];
362 static int inited = 0;
364 g_assert (!aot);
365 if (info)
366 *info = NULL;
368 if (inited)
369 return start;
370 mono_arch_get_throw_exception_generic (start, sizeof (start), FALSE, FALSE, FALSE);
371 inited = 1;
372 return start;
375 gpointer
376 mono_arch_get_throw_exception_by_name (void)
378 guint8 *start, *code;
379 int size = 64;
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));
386 return start;
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.
396 gpointer
397 mono_arch_get_throw_corlib_exception (MonoTrampInfo **info, gboolean aot)
399 static guint8 start [GENERIC_EXCEPTION_SIZE];
400 static int inited = 0;
402 g_assert (!aot);
403 if (info)
404 *info = NULL;
406 if (inited)
407 return start;
408 mono_arch_get_throw_exception_generic (start, sizeof (start), TRUE, FALSE, FALSE);
409 inited = 1;
410 return start;
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.
421 gboolean
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));
429 frame->ji = ji;
431 *new_ctx = *ctx;
433 if (ji != NULL) {
434 int i;
435 gpointer ip = MONO_CONTEXT_GET_IP (ctx);
436 host_mgreg_t regs [MONO_MAX_IREGS + 1];
437 guint8 *cfa;
438 guint32 unwind_info_len;
439 guint8 *unwind_info;
441 if (ji->is_trampoline)
442 frame->type = FRAME_TYPE_TRAMPOLINE;
443 else
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);
456 if (!success)
457 return FALSE;
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));
469 return TRUE;
470 } else if (*lmf) {
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);
476 #endif
477 return FALSE;
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);
482 if (!ji) {
483 // FIXME: This can happen with multiple appdomains (bug #444383)
484 return FALSE;
487 frame->ji = ji;
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);
498 return TRUE;
501 return FALSE;
504 gpointer
505 mono_arch_ip_from_context (void *sigctx)
507 return (gpointer)(gsize)UCONTEXT_REG_PC (sigctx);
511 * handle_exception:
513 * Called by resuming from a signal handler.
515 static void
516 handle_signal_exception (gpointer obj)
518 MonoJitTlsData *jit_tls = mono_tls_get_jit_tls ();
519 MonoContext ctx;
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
531 gboolean
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)
537 void *sigctx = ctx;
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 */
553 sp -= 256;
554 UCONTEXT_GREGS (sigctx)[mips_sp] = sp;
556 UCONTEXT_REG_PC (sigctx) = (gsize)handle_signal_exception;
558 return TRUE;
559 #else
560 MonoContext mctx;
561 gboolean result;
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
567 * the catch clause
569 mono_monoctx_to_sigctx (&mctx, ctx);
570 return result;
571 #endif
575 * mono_arch_setup_resume_sighandler_ctx:
577 * Setup CTX so execution continues at FUNC.
579 void
580 mono_arch_setup_resume_sighandler_ctx (MonoContext *ctx, gpointer func)
582 MONO_CONTEXT_SET_IP (ctx,func);