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.
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"
32 #define GENERIC_EXCEPTION_SIZE 256
36 #define restore_regs_from_context(ctx_reg,ip_reg,tmp_reg) do { \
39 #define restore_regs_from_context(ctx_reg,pc,tmp_reg) do { \
41 ppc_lwz (code, pc, G_STRUCT_OFFSET (MonoContext, sc_pc), ctx_reg); \
42 ppc_lmw (code, ppc_r13, ctx_reg, G_STRUCT_OFFSET (MonoContext, sc_regs)); \
43 for (reg = 0; reg < MONO_SAVED_FREGS; ++reg) { \
44 ppc_lfd (code, (14 + reg), G_STRUCT_OFFSET(MonoLMF, sc_fpregs) + reg * sizeof (gdouble), ctx_reg); \
50 #define setup_context(ctx) do { \
51 memset ((ctx), 0, sizeof(*(ctx))); \
55 * mono_arch_get_restore_context:
57 * Returns a pointer to a method which restores a previously saved MonoContext.
58 * The first argument in a0 is the pointer to the MonoContext.
61 mono_arch_get_restore_context (void)
65 static guint8 start
[128];
66 static int inited
= 0;
67 guint32 iregs_to_restore
;
74 iregs_to_restore
= (MONO_ARCH_CALLEE_SAVED_REGS \
75 | (1 << mips_sp
) | (1 << mips_ra
));
76 for (i
= 0; i
< MONO_SAVED_GREGS
; ++i
) {
77 if (iregs_to_restore
& (1 << i
)) {
78 MIPS_LW (code
, i
, mips_a0
, G_STRUCT_OFFSET (MonoContext
, sc_regs
[i
]));
82 /* Get the address to return to */
83 mips_lw (code
, mips_t9
, mips_a0
, G_STRUCT_OFFSET (MonoContext
, sc_pc
));
85 /* jump to the saved IP */
86 mips_jr (code
, mips_t9
);
90 mips_break (code
, 0xff);
92 g_assert ((code
- start
) < sizeof(start
));
93 mono_arch_flush_icache (start
, code
- start
);
98 * mono_arch_get_call_filter:
100 * Returns a pointer to a method which calls an exception filter. We
101 * also use this function to call finally handlers (we pass NULL as
102 * @exc object in this case).
104 * This function is invoked as
105 * call_handler (MonoContext *ctx, handler)
107 * Where 'handler' is a function to be invoked as:
111 mono_arch_get_call_filter (void)
113 static guint8 start
[320];
114 static int inited
= 0;
126 g_assert ((alloc_size
& (MIPS_STACK_ALIGNMENT
-1)) == 0);
128 mips_addiu (code
, mips_sp
, mips_sp
, -alloc_size
);
129 mips_sw (code
, mips_ra
, mips_sp
, alloc_size
+ MIPS_RET_ADDR_OFFSET
);
131 /* Save global registers on stack (s0 - s7) */
133 MIPS_SW (code
, mips_s0
, mips_sp
, offset
); offset
+= IREG_SIZE
;
134 MIPS_SW (code
, mips_s1
, mips_sp
, offset
); offset
+= IREG_SIZE
;
135 MIPS_SW (code
, mips_s2
, mips_sp
, offset
); offset
+= IREG_SIZE
;
136 MIPS_SW (code
, mips_s3
, mips_sp
, offset
); offset
+= IREG_SIZE
;
137 MIPS_SW (code
, mips_s4
, mips_sp
, offset
); offset
+= IREG_SIZE
;
138 MIPS_SW (code
, mips_s5
, mips_sp
, offset
); offset
+= IREG_SIZE
;
139 MIPS_SW (code
, mips_s6
, mips_sp
, offset
); offset
+= IREG_SIZE
;
140 MIPS_SW (code
, mips_s7
, mips_sp
, offset
); offset
+= IREG_SIZE
;
141 MIPS_SW (code
, mips_fp
, mips_sp
, offset
); offset
+= IREG_SIZE
;
143 /* Restore global registers from MonoContext, including the frame pointer */
144 MIPS_LW (code
, mips_s0
, mips_a0
, G_STRUCT_OFFSET (MonoContext
, sc_regs
[mips_s0
]));
145 MIPS_LW (code
, mips_s1
, mips_a0
, G_STRUCT_OFFSET (MonoContext
, sc_regs
[mips_s1
]));
146 MIPS_LW (code
, mips_s2
, mips_a0
, G_STRUCT_OFFSET (MonoContext
, sc_regs
[mips_s2
]));
147 MIPS_LW (code
, mips_s3
, mips_a0
, G_STRUCT_OFFSET (MonoContext
, sc_regs
[mips_s3
]));
148 MIPS_LW (code
, mips_s4
, mips_a0
, G_STRUCT_OFFSET (MonoContext
, sc_regs
[mips_s4
]));
149 MIPS_LW (code
, mips_s5
, mips_a0
, G_STRUCT_OFFSET (MonoContext
, sc_regs
[mips_s5
]));
150 MIPS_LW (code
, mips_s6
, mips_a0
, G_STRUCT_OFFSET (MonoContext
, sc_regs
[mips_s6
]));
151 MIPS_LW (code
, mips_s7
, mips_a0
, G_STRUCT_OFFSET (MonoContext
, sc_regs
[mips_s7
]));
152 MIPS_LW (code
, mips_fp
, mips_a0
, G_STRUCT_OFFSET (MonoContext
, sc_regs
[mips_fp
]));
154 /* a1 is the handler to call */
155 mips_move (code
, mips_t9
, mips_a1
);
157 /* jump to the saved IP */
158 mips_jalr (code
, mips_t9
, mips_ra
);
161 /* restore all regs from the stack */
163 MIPS_LW (code
, mips_s0
, mips_sp
, offset
); offset
+= IREG_SIZE
;
164 MIPS_LW (code
, mips_s1
, mips_sp
, offset
); offset
+= IREG_SIZE
;
165 MIPS_LW (code
, mips_s2
, mips_sp
, offset
); offset
+= IREG_SIZE
;
166 MIPS_LW (code
, mips_s3
, mips_sp
, offset
); offset
+= IREG_SIZE
;
167 MIPS_LW (code
, mips_s4
, mips_sp
, offset
); offset
+= IREG_SIZE
;
168 MIPS_LW (code
, mips_s5
, mips_sp
, offset
); offset
+= IREG_SIZE
;
169 MIPS_LW (code
, mips_s6
, mips_sp
, offset
); offset
+= IREG_SIZE
;
170 MIPS_LW (code
, mips_s7
, mips_sp
, offset
); offset
+= IREG_SIZE
;
171 MIPS_LW (code
, mips_fp
, mips_sp
, offset
); offset
+= IREG_SIZE
;
174 mips_lw (code
, mips_ra
, mips_sp
, alloc_size
+ MIPS_RET_ADDR_OFFSET
);
175 mips_addiu (code
, mips_sp
, mips_sp
, alloc_size
);
176 mips_jr (code
, mips_ra
);
179 g_assert ((code
- start
) < sizeof(start
));
180 mono_arch_flush_icache (start
, code
- start
);
185 throw_exception (MonoObject
*exc
, unsigned long eip
, unsigned long esp
, gboolean rethrow
)
187 static void (*restore_context
) (MonoContext
*);
190 #ifdef DEBUG_EXCEPTIONS
191 g_print ("throw_exception: exc=%p eip=%p esp=%p rethrow=%d\n",
192 exc
, (void *)eip
, (void *) esp
, rethrow
);
195 if (!restore_context
)
196 restore_context
= mono_arch_get_restore_context ();
198 /* adjust eip so that it point into the call instruction */
201 setup_context (&ctx
);
203 /*g_print ("stack in throw: %p\n", esp);*/
204 memcpy (&ctx
.sc_regs
, (void *)(esp
+ MIPS_STACK_PARAM_OFFSET
),
205 sizeof (gulong
) * MONO_SAVED_GREGS
);
206 memset (&ctx
.sc_fpregs
, 0, sizeof (mips_freg
) * MONO_SAVED_FREGS
);
207 MONO_CONTEXT_SET_IP (&ctx
, eip
);
209 if (mono_object_isinst (exc
, mono_defaults
.exception_class
)) {
210 MonoException
*mono_ex
= (MonoException
*)exc
;
212 mono_ex
->stack_trace
= NULL
;
214 mono_handle_exception (&ctx
, exc
, (void *)eip
, FALSE
);
215 #ifdef DEBUG_EXCEPTIONS
216 g_print ("throw_exception: restore to pc=%p sp=%p fp=%p ctx=%p\n",
217 (void *) ctx
.sc_pc
, (void *) ctx
.sc_regs
[mips_sp
],
218 (void *) ctx
.sc_regs
[mips_fp
], &ctx
);
220 restore_context (&ctx
);
222 g_assert_not_reached ();
226 * arch_get_throw_exception_generic:
228 * Returns a function pointer which can be used to raise
229 * exceptions. The returned function has the following
230 * signature: void (*func) (MonoException *exc); or
231 * void (*func) (char *exc_name);
235 mono_arch_get_throw_exception_generic (guint8
*start
, int size
, int by_name
, gboolean rethrow
)
238 int alloc_size
, pos
, i
;
242 //g_print ("mono_arch_get_throw_exception_generic: code=%p\n", code);
245 /* XXX - save all the FP regs on the stack ? */
247 pos
+= MONO_MAX_IREGS
* sizeof(guint32
);
249 alloc_size
= MIPS_MINIMAL_STACK_SIZE
+ pos
+ 64;
250 // align to MIPS_STACK_ALIGNMENT bytes
251 alloc_size
+= MIPS_STACK_ALIGNMENT
- 1;
252 alloc_size
&= ~(MIPS_STACK_ALIGNMENT
- 1);
254 g_assert ((alloc_size
& (MIPS_STACK_ALIGNMENT
-1)) == 0);
255 mips_addiu (code
, mips_sp
, mips_sp
, -alloc_size
);
256 mips_sw (code
, mips_ra
, mips_sp
, alloc_size
+ MIPS_RET_ADDR_OFFSET
);
258 /* Save all the regs on the stack */
259 for (i
= 0; i
< MONO_MAX_IREGS
; i
++) {
261 MIPS_SW (code
, i
, mips_sp
, i
*IREG_SIZE
+ MIPS_STACK_PARAM_OFFSET
);
263 mips_addiu (code
, mips_at
, mips_sp
, alloc_size
);
264 MIPS_SW (code
, mips_at
, mips_sp
, i
*IREG_SIZE
+ MIPS_STACK_PARAM_OFFSET
);
269 mips_move (code
, mips_a2
, mips_a0
);
270 mips_load (code
, mips_a0
, mono_defaults
.corlib
);
271 mips_load (code
, mips_a1
, "System");
272 mips_load (code
, mips_t9
, mono_exception_from_name
);
273 mips_jalr (code
, mips_t9
, mips_ra
);
275 mips_move (code
, mips_a0
, mips_v0
);
277 /* call throw_exception (exc, ip, sp, rethrow) */
279 /* exc is already in place in a0 */
283 mips_lw (code
, mips_a1
, mips_sp
, alloc_size
+ MIPS_RET_ADDR_OFFSET
);
285 mips_move (code
, mips_a1
, mips_ra
);
287 /* current sp & rethrow */
288 mips_move (code
, mips_a2
, mips_sp
);
289 mips_addiu (code
, mips_a3
, mips_zero
, rethrow
);
291 mips_load (code
, mips_t9
, throw_exception
);
292 mips_jr (code
, mips_t9
);
294 /* we should never reach this breakpoint */
295 mips_break (code
, 0xfe);
297 g_assert ((code
- start
) < size
);
298 mono_arch_flush_icache (start
, code
- start
);
303 * mono_arch_get_rethrow_exception:
305 * Returns a function pointer which can be used to rethrow
306 * exceptions. The returned function has the following
307 * signature: void (*func) (MonoException *exc);
311 mono_arch_get_rethrow_exception (void)
313 static guint8 start
[GENERIC_EXCEPTION_SIZE
];
314 static int inited
= 0;
318 mono_arch_get_throw_exception_generic (start
, sizeof (start
), FALSE
, TRUE
);
324 * arch_get_throw_exception:
326 * Returns a function pointer which can be used to raise
327 * exceptions. The returned function has the following
328 * signature: void (*func) (MonoException *exc);
329 * For example to raise an arithmetic exception you can use:
331 * x86_push_imm (code, mono_get_exception_arithmetic ());
332 * x86_call_code (code, arch_get_throw_exception ());
336 mono_arch_get_throw_exception (void)
338 static guint8 start
[GENERIC_EXCEPTION_SIZE
];
339 static int inited
= 0;
343 mono_arch_get_throw_exception_generic (start
, sizeof (start
), FALSE
, FALSE
);
349 * arch_get_throw_exception_by_name:
351 * Returns a function pointer which can be used to raise
352 * corlib exceptions. The returned function has the following
353 * signature: void (*func) (char *exc_name);
354 * For example to raise an arithmetic exception you can use:
356 * x86_push_imm (code, "ArithmeticException");
357 * x86_call_code (code, arch_get_throw_exception_by_name ());
361 mono_arch_get_throw_exception_by_name (void)
363 static guint8 start
[GENERIC_EXCEPTION_SIZE
];
364 static int inited
= 0;
368 mono_arch_get_throw_exception_generic (start
, sizeof (start
), TRUE
, FALSE
);
374 glist_to_array (GList
*list
, MonoClass
*eclass
)
376 MonoDomain
*domain
= mono_domain_get ();
383 len
= g_list_length (list
);
384 res
= mono_array_new (domain
, eclass
, len
);
386 for (i
= 0; list
; list
= list
->next
, i
++)
387 mono_array_set (res
, gpointer
, i
, list
->data
);
392 /* mono_arch_find_jit_info:
394 * This function is used to gather information from @ctx. It returns the
395 * MonoJitInfo of the corresponding function, unwinds one stack frame and
396 * stores the resulting context into @new_ctx. It also stores a string
397 * describing the stack location into @trace (if not NULL), and modifies
398 * the @lmf if necessary. @native_offset return the IP offset from the
399 * start of the function or -1 if that info is not available.
402 mono_arch_find_jit_info (MonoDomain
*domain
, MonoJitTlsData
*jit_tls
,
403 MonoJitInfo
*res
, MonoJitInfo
*prev_ji
,
404 MonoContext
*ctx
, MonoContext
*new_ctx
,
405 MonoLMF
**lmf
, gboolean
*managed
)
408 gpointer ip
= MONO_CONTEXT_GET_IP (ctx
);
409 gpointer fp
= MONO_CONTEXT_GET_BP (ctx
);
412 /* Avoid costly table lookup during stack overflow */
413 if (prev_ji
&& (ip
> prev_ji
->code_start
&& ((guint8
*)ip
< ((guint8
*)prev_ji
->code_start
) + prev_ji
->code_size
)))
416 ji
= mono_jit_info_table_find (domain
, ip
);
421 memcpy (new_ctx
, ctx
, sizeof (MonoContext
));
428 if (*lmf
&& (MONO_CONTEXT_GET_BP (ctx
) >= (gpointer
)(*lmf
)->ebp
)) {
429 /* remove any unused lmf */
430 *lmf
= (*lmf
)->previous_lmf
;
433 address
= (char *)ip
- (char *)ji
->code_start
;
436 if (!ji
->method
->wrapper_type
)
440 fp
= MONO_CONTEXT_GET_BP (ctx
);
442 /* Compute the previous stack frame */
443 sp
= (guint32
)(fp
) - (short)(*(guint32
*)(ji
->code_start
));
445 /* Sanity check the frame */
446 if (!sp
|| (sp
== 0xffffffff)
447 || (sp
& 0x07) || (sp
< 64*1024)) {
448 #ifdef DEBUG_EXCEPTIONS
449 g_print ("mono_arch_find_jit_info: bad stack sp=%p\n", (void *) sp
);
454 if (ji
->method
->save_lmf
&& 0) {
455 /* only enable this when prologue stops emitting
456 * normal save of s-regs when save_lmf is true.
457 * Will have to sync with prologue code at that point.
459 memcpy (&new_ctx
->sc_fpregs
,
460 (char*)sp
- sizeof (float) * MONO_SAVED_FREGS
,
461 sizeof (float) * MONO_SAVED_FREGS
);
462 memcpy (&new_ctx
->sc_regs
,
463 (char*)sp
- sizeof (float) * MONO_SAVED_FREGS
- sizeof (gulong
) * MONO_SAVED_GREGS
,
464 sizeof (gulong
) * MONO_SAVED_GREGS
);
465 } else if (ji
->used_regs
) {
467 guint32 mask
= ji
->used_regs
;
469 /* these all happen before adjustment of fp */
470 /* Look for sw ??, ????(sp) */
471 insn
= ((guint32
*)ji
->code_start
) + 1;
472 while (!*insn
|| ((*insn
& 0xffe00000) == 0xafa00000) || ((*insn
& 0xffe00000) == 0xffa00000)) {
473 int reg
= (*insn
>> 16) & 0x1f;
474 guint32 addr
= (((guint32
)fp
) + (short)(*insn
& 0x0000ffff));
477 if ((*insn
& 0xffe00000) == 0xafa00000)
478 new_ctx
->sc_regs
[reg
] = *(guint32
*)addr
;
480 new_ctx
->sc_regs
[reg
] = *(guint64
*)addr
;
483 MONO_CONTEXT_SET_SP (new_ctx
, sp
);
484 MONO_CONTEXT_SET_BP (new_ctx
, sp
);
485 /* assert that we found all registers we were supposed to */
488 /* we substract 8, so that the IP points into the call instruction */
489 MONO_CONTEXT_SET_IP (new_ctx
, new_ctx
->sc_regs
[mips_ra
] - 8);
491 /* Sanity check -- we should have made progress here */
492 g_assert (new_ctx
->sc_pc
!= ctx
->sc_pc
);
495 if (!(*lmf
)->method
) {
496 #ifdef DEBUG_EXCEPTIONS
497 g_print ("mono_arch_find_jit_info: bad lmf @ %p\n", (void *) *lmf
);
501 g_assert (((*lmf
)->magic
== MIPS_LMF_MAGIC1
) || ((*lmf
)->magic
== MIPS_LMF_MAGIC2
));
503 if ((ji
= mono_jit_info_table_find (domain
, (gpointer
)(*lmf
)->eip
))) {
505 memset (res
, 0, sizeof (MonoJitInfo
));
506 res
->method
= (*lmf
)->method
;
508 memcpy (&new_ctx
->sc_regs
, (*lmf
)->iregs
, sizeof (gulong
) * MONO_SAVED_GREGS
);
509 memcpy (&new_ctx
->sc_fpregs
, (*lmf
)->fregs
, sizeof (float) * MONO_SAVED_FREGS
);
510 MONO_CONTEXT_SET_IP (new_ctx
, (*lmf
)->eip
);
511 /* ensure that we've made progress */
512 g_assert (new_ctx
->sc_pc
!= ctx
->sc_pc
);
513 *lmf
= (*lmf
)->previous_lmf
;
515 return ji
? ji
: res
;
522 mono_arch_sigctx_to_monoctx (void *sigctx
, MonoContext
*mctx
)
525 struct sigcontext
*ctx
= (struct sigcontext
*)sigctx
;
527 mctx
->sc_pc
= ctx
->sc_pc
;
528 for (i
= 0; i
< 32; ++i
) {
529 mctx
->sc_regs
[i
] = ctx
->sc_regs
[i
];
530 mctx
->sc_fpregs
[i
] = ctx
->sc_fpregs
[i
];
535 mono_arch_monoctx_to_sigctx (MonoContext
*mctx
, void *sigctx
)
538 struct sigcontext
*ctx
= (struct sigcontext
*)sigctx
;
540 ctx
->sc_pc
= mctx
->sc_pc
;
541 for (i
= 0; i
< 32; ++i
) {
542 ctx
->sc_regs
[i
] = mctx
->sc_regs
[i
];
543 ctx
->sc_fpregs
[i
] = mctx
->sc_fpregs
[i
];
548 mono_arch_ip_from_context (void *sigctx
)
550 struct sigcontext
*ctx
= (struct sigcontext
*)sigctx
;
551 return (gpointer
)(guint32
)ctx
->sc_pc
;
555 * This is the function called from the signal handler
558 mono_arch_handle_exception (void *ctx
, gpointer obj
, gboolean test_only
)
563 mono_arch_sigctx_to_monoctx (ctx
, &mctx
);
564 #ifdef DEBUG_EXCEPTIONS
565 g_print ("mono_arch_handle_exception: pc=%p\n", (void *) mctx
.sc_pc
);
567 mono_handle_exception (&mctx
, obj
, (gpointer
)mctx
.sc_pc
, test_only
);
570 #ifdef DEBUG_EXCEPTIONS
571 g_print ("mono_arch_handle_exception: restore pc=%p\n", (void *)mctx
.sc_pc
);
573 /* restore the context so that returning from the signal handler
574 * will invoke the catch clause
576 mono_arch_monoctx_to_sigctx (&mctx
, ctx
);
583 mono_arch_has_unwind_info (gconstpointer addr
)