[interp] Fix interp logging (#17636)
[mono-project.git] / mono / mini / exceptions-mips.c
blob15a89d657d28eecf6a2dc4bffa5ecc67cd507f3b
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"
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.
43 gpointer
44 mono_arch_get_restore_context (MonoTrampInfo **info, gboolean aot)
46 int i;
47 guint8 *code;
48 static guint8 start [512];
49 static int inited = 0;
50 guint32 iregs_to_restore;
52 g_assert (!aot);
53 if (info)
54 *info = NULL;
56 if (inited)
57 return start;
58 inited = 1;
59 code = start;
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);
77 mips_nop (code);
79 /* never reached */
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));
85 return start;
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:
99 * handler (void)
101 gpointer
102 mono_arch_get_call_filter (MonoTrampInfo **info, gboolean aot)
104 static guint8 start [320];
105 static int inited = 0;
106 guint8 *code;
107 int alloc_size;
108 int offset;
110 g_assert (!aot);
111 if (info)
112 *info = NULL;
114 if (inited)
115 return start;
117 inited = 1;
118 code = start;
120 alloc_size = 64;
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) */
127 offset = 16;
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);
154 mips_nop (code);
156 /* restore all regs from the stack */
157 offset = 16;
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;
168 /* epilog */
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);
172 mips_nop (code);
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));
177 return start;
180 static void
181 throw_exception (MonoObject *exc, unsigned long eip, unsigned long esp, gboolean rethrow, gboolean preserve_ips)
183 ERROR_DECL (error);
184 MonoContext ctx;
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);
189 #endif
191 /* adjust eip so that it point into the call instruction */
192 eip -= 8;
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);
217 #endif
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);
232 static gpointer
233 mono_arch_get_throw_exception_generic (guint8 *start, int size, int corlib, gboolean rethrow, gboolean preserve_ips)
235 guint8 *code;
236 int alloc_size, pos, i;
238 code = start;
240 //g_print ("mono_arch_get_throw_exception_generic: code=%p\n", code);
242 pos = 0;
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++) {
258 if (i != mips_sp)
259 MIPS_SW (code, i, mips_sp, i*IREG_SIZE + MIPS_STACK_PARAM_OFFSET);
260 else {
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);
266 if (corlib) {
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);
271 mips_nop (code);
272 mips_move (code, mips_a0, mips_v0);
274 /* call throw_exception (exc, ip, sp, rethrow) */
276 /* exc is already in place in a0 */
278 /* pointer to ip */
279 if (corlib)
280 mips_lw (code, mips_a1, mips_sp, alloc_size + MIPS_RET_ADDR_OFFSET);
281 else
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);
290 mips_nop (code);
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));
297 return start;
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);
306 gpointer
307 mono_arch_get_rethrow_exception (MonoTrampInfo **info, gboolean aot)
309 static guint8 start [GENERIC_EXCEPTION_SIZE];
310 static int inited = 0;
312 g_assert (!aot);
313 if (info)
314 *info = NULL;
316 if (inited)
317 return start;
318 mono_arch_get_throw_exception_generic (start, sizeof (start), FALSE, TRUE, FALSE);
319 inited = 1;
320 return start;
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);
330 gpointer
331 mono_arch_get_rethrow_preserve_exception (MonoTrampInfo **info, gboolean aot)
333 static guint8 start [GENERIC_EXCEPTION_SIZE];
334 static int inited = 0;
336 g_assert (!aot);
337 if (info)
338 *info = NULL;
340 if (inited)
341 return start;
342 mono_arch_get_throw_exception_generic (start, sizeof (start), FALSE, TRUE, TRUE);
343 inited = 1;
344 return start;
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 ());
359 gpointer
360 mono_arch_get_throw_exception (MonoTrampInfo **info, gboolean aot)
362 static guint8 start [GENERIC_EXCEPTION_SIZE];
363 static int inited = 0;
365 g_assert (!aot);
366 if (info)
367 *info = NULL;
369 if (inited)
370 return start;
371 mono_arch_get_throw_exception_generic (start, sizeof (start), FALSE, FALSE, FALSE);
372 inited = 1;
373 return start;
376 gpointer
377 mono_arch_get_throw_exception_by_name (void)
379 guint8 *start, *code;
380 int size = 64;
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));
387 return start;
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.
397 gpointer
398 mono_arch_get_throw_corlib_exception (MonoTrampInfo **info, gboolean aot)
400 static guint8 start [GENERIC_EXCEPTION_SIZE];
401 static int inited = 0;
403 g_assert (!aot);
404 if (info)
405 *info = NULL;
407 if (inited)
408 return start;
409 mono_arch_get_throw_exception_generic (start, sizeof (start), TRUE, FALSE, FALSE);
410 inited = 1;
411 return start;
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.
422 gboolean
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));
430 frame->ji = ji;
432 *new_ctx = *ctx;
434 if (ji != NULL) {
435 int i;
436 gpointer ip = MONO_CONTEXT_GET_IP (ctx);
437 host_mgreg_t regs [MONO_MAX_IREGS + 1];
438 guint8 *cfa;
439 guint32 unwind_info_len;
440 guint8 *unwind_info;
442 if (ji->is_trampoline)
443 frame->type = FRAME_TYPE_TRAMPOLINE;
444 else
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);
457 if (!success)
458 return FALSE;
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));
470 return TRUE;
471 } else if (*lmf) {
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);
477 #endif
478 return FALSE;
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);
483 if (!ji) {
484 // FIXME: This can happen with multiple appdomains (bug #444383)
485 return FALSE;
488 frame->ji = ji;
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);
499 return TRUE;
502 return FALSE;
505 gpointer
506 mono_arch_ip_from_context (void *sigctx)
508 return (gpointer)(gsize)UCONTEXT_REG_PC (sigctx);
512 * handle_exception:
514 * Called by resuming from a signal handler.
516 static void
517 handle_signal_exception (gpointer obj)
519 MonoJitTlsData *jit_tls = mono_tls_get_jit_tls ();
520 MonoContext ctx;
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
532 gboolean
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)
538 void *sigctx = ctx;
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 */
554 sp -= 256;
555 UCONTEXT_GREGS (sigctx)[mips_sp] = sp;
557 UCONTEXT_REG_PC (sigctx) = (gsize)handle_signal_exception;
559 return TRUE;
560 #else
561 MonoContext mctx;
562 gboolean result;
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
568 * the catch clause
570 mono_monoctx_to_sigctx (&mctx, ctx);
571 return result;
572 #endif
576 * mono_arch_setup_resume_sighandler_ctx:
578 * Setup CTX so execution continues at FUNC.
580 void
581 mono_arch_setup_resume_sighandler_ctx (MonoContext *ctx, gpointer func)
583 MONO_CONTEXT_SET_IP (ctx,func);