2 * exceptions-sparc.c: exception support for sparc
5 * Mark Crichton (crichton@gimp.org)
6 * Dietmar Maurer (dietmar@ximian.com)
8 * (C) 2003 Ximian, Inc.
15 #include <sys/ucontext.h>
17 #include <mono/arch/sparc/sparc-codegen.h>
18 #include <mono/metadata/appdomain.h>
19 #include <mono/metadata/tabledefs.h>
20 #include <mono/metadata/threads.h>
21 #include <mono/metadata/debug-helpers.h>
22 #include <mono/metadata/exception.h>
23 #include <mono/metadata/mono-debug.h>
24 #include <mono/metadata/gc-internal.h>
25 #include <mono/metadata/tokentype.h>
28 #include "mini-sparc.h"
34 #define MONO_SPARC_WINDOW_ADDR(sp) ((gpointer*)(((guint8*)(sp)) + MONO_SPARC_STACK_BIAS))
37 * mono_arch_get_restore_context:
39 * Returns a pointer to a method which restores a previously saved sigcontext.
42 mono_arch_get_restore_context (MonoTrampInfo **info, gboolean aot)
44 static guint32 *start;
45 static int inited = 0;
55 code = start = mono_global_codeman_reserve (32 * sizeof (guint32));
57 sparc_ldi_imm (code, sparc_o0, G_STRUCT_OFFSET (MonoContext, ip), sparc_i7);
58 sparc_ldi_imm (code, sparc_o0, G_STRUCT_OFFSET (MonoContext, sp), sparc_i6);
60 sparc_jmpl_imm (code, sparc_i7, 0, sparc_g0);
61 /* FIXME: This does not return to the correct window */
62 sparc_restore_imm (code, sparc_g0, 0, sparc_g0);
64 g_assert ((code - start) < 32);
66 mono_arch_flush_icache ((guint8*)start, (guint8*)code - (guint8*)start);
74 * mono_arch_get_call_filter:
76 * Returns a pointer to a method which calls an exception filter. We
77 * also use this function to call finally handlers (we pass NULL as
78 * @exc object in this case).
80 * call_filter (MonoContext *ctx, gpointer ip)
83 mono_arch_get_call_filter (MonoTrampInfo **info, gboolean aot)
85 static guint32 *start;
86 static int inited = 0;
97 code = start = mono_global_codeman_reserve (64 * sizeof (guint32));
100 * There are two frames here:
101 * - the first frame is used by call_filter
102 * - the second frame is used to run the filter code
105 /* Create first frame */
106 sparc_save_imm (code, sparc_sp, -256, sparc_sp);
108 sparc_mov_reg_reg (code, sparc_i1, sparc_o0);
109 sparc_ldi_imm (code, sparc_i0, G_STRUCT_OFFSET (MonoContext, sp), sparc_o1);
111 /* Create second frame */
112 sparc_save_imm (code, sparc_sp, -256, sparc_sp);
114 sparc_mov_reg_reg (code, sparc_i0, sparc_o0);
115 sparc_mov_reg_reg (code, sparc_i1, sparc_o1);
118 * We need to change %fp to point to the stack frame of the method
119 * containing the filter. But changing %fp also changes the %sp of
120 * the parent frame (the first frame), so if the OS saves the first frame,
121 * it saves it to the stack frame of the method, which is not good.
122 * So flush all register windows to memory before changing %fp.
126 sparc_mov_reg_reg (code, sparc_fp, sparc_o7);
129 * Modify the second frame so it is identical to the one used in the
130 * method containing the filter.
132 for (i = 0; i < 16; ++i)
133 sparc_ldi_imm (code, sparc_o1, MONO_SPARC_STACK_BIAS + i * sizeof (gpointer), sparc_l0 + i);
135 /* Save %fp to a location reserved in mono_arch_allocate_vars */
136 sparc_sti_imm (code, sparc_o7, sparc_fp, MONO_SPARC_STACK_BIAS - sizeof (gpointer));
138 /* Call the filter code, after this returns, %o0 will hold the result */
139 sparc_call_imm (code, sparc_o0, 0);
142 /* Restore original %fp */
143 sparc_ldi_imm (code, sparc_fp, MONO_SPARC_STACK_BIAS - sizeof (gpointer), sparc_fp);
145 sparc_mov_reg_reg (code, sparc_o0, sparc_i0);
147 /* Return to first frame */
148 sparc_restore (code, sparc_g0, sparc_g0, sparc_g0);
150 /* FIXME: Save locals to the stack */
152 /* Return to caller */
154 /* Return result in delay slot */
155 sparc_restore (code, sparc_o0, sparc_g0, sparc_o0);
157 g_assert ((code - start) < 64);
159 mono_arch_flush_icache ((guint8*)start, (guint8*)code - (guint8*)start);
167 throw_exception (MonoObject *exc, gpointer sp, gpointer ip, gboolean rethrow)
170 static void (*restore_context) (MonoContext *);
173 if (!restore_context)
174 restore_context = mono_get_restore_context ();
176 window = MONO_SPARC_WINDOW_ADDR (sp);
177 ctx.sp = (gpointer*)sp;
179 ctx.fp = (gpointer*)(MONO_SPARC_WINDOW_ADDR (sp) [sparc_i6 - 16]);
181 if (mono_object_isinst (exc, mono_defaults.exception_class)) {
182 MonoException *mono_ex = (MonoException*)exc;
184 mono_ex->stack_trace = NULL;
186 mono_handle_exception (&ctx, exc, ip, FALSE);
187 restore_context (&ctx);
189 g_assert_not_reached ();
193 get_throw_exception (gboolean rethrow)
195 guint32 *start, *code;
197 code = start = mono_global_codeman_reserve (16 * sizeof (guint32));
199 sparc_save_imm (code, sparc_sp, -512, sparc_sp);
202 sparc_mov_reg_reg (code, sparc_i0, sparc_o0);
203 sparc_mov_reg_reg (code, sparc_fp, sparc_o1);
204 sparc_mov_reg_reg (code, sparc_i7, sparc_o2);
205 sparc_set (code, rethrow, sparc_o3);
206 sparc_set (code, throw_exception, sparc_o7);
207 sparc_jmpl (code, sparc_o7, sparc_g0, sparc_callsite);
210 g_assert ((code - start) <= 16);
212 mono_arch_flush_icache ((guint8*)start, (guint8*)code - (guint8*)start);
218 * mono_arch_get_throw_exception:
220 * Returns a function pointer which can be used to raise exceptions.
221 * The returned function has the following
222 * signature: void (*func) (MonoException *exc);
225 mono_arch_get_throw_exception (MonoTrampInfo **info, gboolean aot)
227 static guint32* start;
228 static int inited = 0;
239 start = get_throw_exception (FALSE);
245 mono_arch_get_rethrow_exception (MonoTrampInfo **info, gboolean aot)
247 static guint32* start;
248 static int inited = 0;
259 start = get_throw_exception (TRUE);
265 * mono_arch_get_throw_corlib_exception:
267 * Returns a function pointer which can be used to raise
268 * corlib exceptions. The returned function has the following
269 * signature: void (*func) (guint32 ex_token, guint32 offset);
270 * Here, offset is the offset which needs to be substracted from the caller IP
271 * to get the IP of the throw. Passing the offset has the advantage that it
272 * needs no relocations in the caller.
275 mono_arch_get_throw_corlib_exception (MonoTrampInfo **info, gboolean aot)
277 static guint32 *start;
278 static int inited = 0;
290 code = start = mono_global_codeman_reserve (64 * sizeof (guint32));
298 sparc_mov_reg_reg (code, sparc_o7, sparc_o2);
299 sparc_save_imm (code, sparc_sp, -160, sparc_sp);
301 sparc_set (code, MONO_TOKEN_TYPE_DEF, sparc_o7);
302 sparc_add (code, FALSE, sparc_i0, sparc_o7, sparc_o1);
303 sparc_set (code, mono_defaults.exception_class->image, sparc_o0);
304 sparc_set (code, mono_exception_from_token, sparc_o7);
305 sparc_jmpl (code, sparc_o7, sparc_g0, sparc_callsite);
308 /* Return to the caller, so exception handling does not see this frame */
309 sparc_restore (code, sparc_o0, sparc_g0, sparc_o0);
311 /* Compute throw ip */
312 sparc_sll_imm (code, sparc_o1, 2, sparc_o1);
313 sparc_sub (code, 0, sparc_o2, sparc_o1, sparc_o7);
315 sparc_set (code, mono_arch_get_throw_exception (NULL, FALSE), reg);
316 /* Use a jmp instead of a call so o7 is preserved */
317 sparc_jmpl_imm (code, reg, 0, sparc_g0);
320 g_assert ((code - start) < 32);
322 mono_arch_flush_icache ((guint8*)start, (guint8*)code - (guint8*)start);
327 /* mono_arch_find_jit_info:
329 * This function is used to gather information from @ctx. It return the
330 * MonoJitInfo of the corresponding function, unwinds one stack frame and
331 * stores the resulting context into @new_ctx. It also stores a string
332 * describing the stack location into @trace (if not NULL), and modifies
333 * the @lmf if necessary. @native_offset return the IP offset from the
334 * start of the function or -1 if that info is not available.
337 mono_arch_find_jit_info (MonoDomain *domain, MonoJitTlsData *jit_tls, MonoJitInfo *res, MonoJitInfo *prev_ji, MonoContext *ctx,
338 MonoContext *new_ctx, MonoLMF **lmf, gboolean *managed)
341 gpointer ip = MONO_CONTEXT_GET_IP (ctx);
344 /* Avoid costly table lookup during stack overflow */
345 if (prev_ji && (ip > prev_ji->code_start && ((guint8*)ip < ((guint8*)prev_ji->code_start) + prev_ji->code_size)))
348 ji = mini_jit_info_table_find (domain, ip, NULL);
357 if (!ji->method->wrapper_type)
360 if (*lmf && (MONO_CONTEXT_GET_BP (ctx) >= (gpointer)(*lmf)->ebp)) {
361 /* remove any unused lmf */
362 *lmf = (*lmf)->previous_lmf;
365 /* Restore ip and sp from the saved register window */
366 window = MONO_SPARC_WINDOW_ADDR (ctx->sp);
367 new_ctx->ip = window [sparc_i7 - 16];
368 new_ctx->sp = (gpointer*)(window [sparc_i6 - 16]);
369 new_ctx->fp = (gpointer*)(MONO_SPARC_WINDOW_ADDR (new_ctx->sp) [sparc_i6 - 16]);
382 if ((ji = mini_jit_info_table_find (domain, (gpointer)(*lmf)->ip, NULL))) {
384 memset (res, 0, MONO_SIZEOF_JIT_INFO);
385 res->method = (*lmf)->method;
388 new_ctx->ip = (*lmf)->ip;
389 new_ctx->sp = (*lmf)->sp;
390 new_ctx->fp = (*lmf)->ebp;
392 *lmf = (*lmf)->previous_lmf;
394 return ji ? ji : res;
399 mono_arch_has_unwind_info (gconstpointer addr)
407 mono_arch_handle_exception (void *sigctx, gpointer obj, gboolean test_only)
410 struct sigcontext *sc = sigctx;
414 mctx.ip = (gpointer) sc->sigc_regs.tpc;
415 mctx.sp = (gpointer) sc->sigc_regs.u_regs[14];
417 mctx.ip = (gpointer) sc->si_regs.pc;
418 mctx.sp = (gpointer) sc->si_regs.u_regs[14];
421 window = (gpointer*)(((guint8*)mctx.sp) + MONO_SPARC_STACK_BIAS);
422 mctx.fp = window [sparc_fp - 16];
424 mono_handle_exception (&mctx, obj, mctx.ip, test_only);
427 sc->sigc_regs.tpc = (unsigned long) mctx.ip;
428 sc->sigc_regs.tnpc = (unsigned long) (mctx.ip + 4);
429 sc->sigc_regs.u_regs[14] = (unsigned long) mctx.sp;
431 sc->si_regs.pc = (unsigned long) mctx.ip;
432 sc->si_regs.npc = (unsigned long) (mctx.ip + 4);
433 sc->si_regs.u_regs[14] = (unsigned long) mctx.sp;
436 window = (gpointer*)(((guint8*)mctx.sp) + MONO_SPARC_STACK_BIAS);
437 window [sparc_fp - 16] = mctx.fp;
443 mono_arch_ip_from_context (void *sigctx)
445 struct sigcontext *sc = sigctx;
449 ret = (gpointer) sc->sigc_regs.tpc;
451 ret = (gpointer) sc->si_regs.pc;
457 #else /* !__linux__ */
460 mono_arch_handle_exception (void *sigctx, gpointer obj, gboolean test_only)
463 ucontext_t *ctx = (ucontext_t*)sigctx;
467 * Access to the machine state using the ucontext_t parameter is somewhat
468 * under documented under solaris. The code below seems to work under
471 g_assert (!ctx->uc_mcontext.gwins);
473 mctx.ip = ctx->uc_mcontext.gregs [REG_PC];
474 mctx.sp = ctx->uc_mcontext.gregs [REG_SP];
475 window = (gpointer*)(((guint8*)mctx.sp) + MONO_SPARC_STACK_BIAS);
476 mctx.fp = window [sparc_fp - 16];
478 mono_handle_exception (&mctx, obj, mctx.ip, test_only);
480 /* We can't use restore_context to return from a signal handler */
481 ctx->uc_mcontext.gregs [REG_PC] = mctx.ip;
482 ctx->uc_mcontext.gregs [REG_nPC] = mctx.ip + 4;
483 ctx->uc_mcontext.gregs [REG_SP] = mctx.sp;
484 window = (gpointer*)(((guint8*)mctx.sp) + MONO_SPARC_STACK_BIAS);
485 window [sparc_fp - 16] = mctx.fp;
491 mono_arch_ip_from_context (void *sigctx)
493 ucontext_t *ctx = (ucontext_t*)sigctx;
494 return (gpointer)ctx->uc_mcontext.gregs [REG_PC];