3 * exception support for sparc
6 * Mark Crichton (crichton@gimp.org)
7 * Dietmar Maurer (dietmar@ximian.com)
9 * (C) 2003 Ximian, Inc.
16 #include <sys/ucontext.h>
18 #include <mono/arch/sparc/sparc-codegen.h>
19 #include <mono/metadata/appdomain.h>
20 #include <mono/metadata/tabledefs.h>
21 #include <mono/metadata/threads.h>
22 #include <mono/metadata/debug-helpers.h>
23 #include <mono/metadata/exception.h>
24 #include <mono/metadata/mono-debug.h>
25 #include <mono/metadata/gc-internals.h>
26 #include <mono/metadata/tokentype.h>
29 #include "mini-sparc.h"
35 #define MONO_SPARC_WINDOW_ADDR(sp) ((gpointer*)(((guint8*)(sp)) + MONO_SPARC_STACK_BIAS))
38 * mono_arch_get_restore_context:
40 * Returns a pointer to a method which restores a previously saved sigcontext.
43 mono_arch_get_restore_context (MonoTrampInfo **info, gboolean aot)
45 static guint32 *start;
46 static int inited = 0;
56 code = start = mono_global_codeman_reserve (32 * sizeof (guint32));
58 sparc_ldi_imm (code, sparc_o0, G_STRUCT_OFFSET (MonoContext, ip), sparc_i7);
59 sparc_ldi_imm (code, sparc_o0, G_STRUCT_OFFSET (MonoContext, sp), sparc_i6);
61 sparc_jmpl_imm (code, sparc_i7, 0, sparc_g0);
62 /* FIXME: This does not return to the correct window */
63 sparc_restore_imm (code, sparc_g0, 0, sparc_g0);
65 g_assert ((code - start) < 32);
67 mono_arch_flush_icache ((guint8*)start, (guint8*)code - (guint8*)start);
75 * mono_arch_get_call_filter:
77 * Returns a pointer to a method which calls an exception filter. We
78 * also use this function to call finally handlers (we pass NULL as
79 * @exc object in this case).
81 * call_filter (MonoContext *ctx, gpointer ip)
84 mono_arch_get_call_filter (MonoTrampInfo **info, gboolean aot)
86 static guint32 *start;
87 static int inited = 0;
98 code = start = mono_global_codeman_reserve (64 * sizeof (guint32));
101 * There are two frames here:
102 * - the first frame is used by call_filter
103 * - the second frame is used to run the filter code
106 /* Create first frame */
107 sparc_save_imm (code, sparc_sp, -256, sparc_sp);
109 sparc_mov_reg_reg (code, sparc_i1, sparc_o0);
110 sparc_ldi_imm (code, sparc_i0, G_STRUCT_OFFSET (MonoContext, sp), sparc_o1);
112 /* Create second frame */
113 sparc_save_imm (code, sparc_sp, -256, sparc_sp);
115 sparc_mov_reg_reg (code, sparc_i0, sparc_o0);
116 sparc_mov_reg_reg (code, sparc_i1, sparc_o1);
119 * We need to change %fp to point to the stack frame of the method
120 * containing the filter. But changing %fp also changes the %sp of
121 * the parent frame (the first frame), so if the OS saves the first frame,
122 * it saves it to the stack frame of the method, which is not good.
123 * So flush all register windows to memory before changing %fp.
127 sparc_mov_reg_reg (code, sparc_fp, sparc_o7);
130 * Modify the second frame so it is identical to the one used in the
131 * method containing the filter.
133 for (i = 0; i < 16; ++i)
134 sparc_ldi_imm (code, sparc_o1, MONO_SPARC_STACK_BIAS + i * sizeof (gpointer), sparc_l0 + i);
136 /* Save %fp to a location reserved in mono_arch_allocate_vars */
137 sparc_sti_imm (code, sparc_o7, sparc_fp, MONO_SPARC_STACK_BIAS - sizeof (gpointer));
139 /* Call the filter code, after this returns, %o0 will hold the result */
140 sparc_call_imm (code, sparc_o0, 0);
143 /* Restore original %fp */
144 sparc_ldi_imm (code, sparc_fp, MONO_SPARC_STACK_BIAS - sizeof (gpointer), sparc_fp);
146 sparc_mov_reg_reg (code, sparc_o0, sparc_i0);
148 /* Return to first frame */
149 sparc_restore (code, sparc_g0, sparc_g0, sparc_g0);
151 /* FIXME: Save locals to the stack */
153 /* Return to caller */
155 /* Return result in delay slot */
156 sparc_restore (code, sparc_o0, sparc_g0, sparc_o0);
158 g_assert ((code - start) < 64);
160 mono_arch_flush_icache ((guint8*)start, (guint8*)code - (guint8*)start);
168 throw_exception (MonoObject *exc, gpointer sp, gpointer ip, gboolean rethrow)
172 static void (*restore_context) (MonoContext *);
175 if (!restore_context)
176 restore_context = mono_get_restore_context ();
178 window = MONO_SPARC_WINDOW_ADDR (sp);
179 ctx.sp = (gpointer*)sp;
181 ctx.fp = (gpointer*)(MONO_SPARC_WINDOW_ADDR (sp) [sparc_i6 - 16]);
183 if (mono_object_isinst_checked (exc, mono_defaults.exception_class, &error)) {
184 MonoException *mono_ex = (MonoException*)exc;
186 mono_ex->stack_trace = NULL;
187 mono_ex->trace_ips = NULL;
190 mono_error_assert_ok (&error);
191 mono_handle_exception (&ctx, exc);
192 restore_context (&ctx);
194 g_assert_not_reached ();
198 get_throw_exception (gboolean rethrow)
200 guint32 *start, *code;
202 code = start = mono_global_codeman_reserve (16 * sizeof (guint32));
204 sparc_save_imm (code, sparc_sp, -512, sparc_sp);
207 sparc_mov_reg_reg (code, sparc_i0, sparc_o0);
208 sparc_mov_reg_reg (code, sparc_fp, sparc_o1);
209 sparc_mov_reg_reg (code, sparc_i7, sparc_o2);
210 sparc_set (code, rethrow, sparc_o3);
211 sparc_set (code, throw_exception, sparc_o7);
212 sparc_jmpl (code, sparc_o7, sparc_g0, sparc_callsite);
215 g_assert ((code - start) <= 16);
217 mono_arch_flush_icache ((guint8*)start, (guint8*)code - (guint8*)start);
223 * mono_arch_get_throw_exception:
225 * Returns a function pointer which can be used to raise exceptions.
226 * The returned function has the following
227 * signature: void (*func) (MonoException *exc);
230 mono_arch_get_throw_exception (MonoTrampInfo **info, gboolean aot)
232 static guint32* start;
233 static int inited = 0;
244 start = get_throw_exception (FALSE);
250 mono_arch_get_rethrow_exception (MonoTrampInfo **info, gboolean aot)
252 static guint32* start;
253 static int inited = 0;
264 start = get_throw_exception (TRUE);
270 * mono_arch_get_throw_corlib_exception:
272 * Returns a function pointer which can be used to raise
273 * corlib exceptions. The returned function has the following
274 * signature: void (*func) (guint32 ex_token, guint32 offset);
275 * Here, offset is the offset which needs to be substracted from the caller IP
276 * to get the IP of the throw. Passing the offset has the advantage that it
277 * needs no relocations in the caller.
280 mono_arch_get_throw_corlib_exception (MonoTrampInfo **info, gboolean aot)
282 static guint32 *start;
283 static int inited = 0;
295 code = start = mono_global_codeman_reserve (64 * sizeof (guint32));
303 sparc_mov_reg_reg (code, sparc_o7, sparc_o2);
304 sparc_save_imm (code, sparc_sp, -160, sparc_sp);
306 sparc_set (code, MONO_TOKEN_TYPE_DEF, sparc_o7);
307 sparc_add (code, FALSE, sparc_i0, sparc_o7, sparc_o1);
308 sparc_set (code, mono_defaults.exception_class->image, sparc_o0);
309 sparc_set (code, mono_exception_from_token, sparc_o7);
310 sparc_jmpl (code, sparc_o7, sparc_g0, sparc_callsite);
313 /* Return to the caller, so exception handling does not see this frame */
314 sparc_restore (code, sparc_o0, sparc_g0, sparc_o0);
316 /* Compute throw ip */
317 sparc_sll_imm (code, sparc_o1, 2, sparc_o1);
318 sparc_sub (code, 0, sparc_o2, sparc_o1, sparc_o7);
320 sparc_set (code, mono_arch_get_throw_exception (NULL, FALSE), reg);
321 /* Use a jmp instead of a call so o7 is preserved */
322 sparc_jmpl_imm (code, reg, 0, sparc_g0);
325 g_assert ((code - start) < 32);
327 mono_arch_flush_icache ((guint8*)start, (guint8*)code - (guint8*)start);
332 /* mono_arch_unwind_frame:
334 * This function is used to gather information from @ctx. It return the
335 * MonoJitInfo of the corresponding function, unwinds one stack frame and
336 * stores the resulting context into @new_ctx. It also stores a string
337 * describing the stack location into @trace (if not NULL), and modifies
338 * the @lmf if necessary. @native_offset return the IP offset from the
339 * start of the function or -1 if that info is not available.
342 mono_arch_unwind_frame (MonoDomain *domain, MonoJitTlsData *jit_tls,
343 MonoJitInfo *ji, MonoContext *ctx,
344 MonoContext *new_ctx, MonoLMF **lmf,
345 mgreg_t **save_locations,
346 StackFrameInfo *frame)
350 memset (frame, 0, sizeof (StackFrameInfo));
356 if (ji->is_trampoline)
357 frame->type = FRAME_TYPE_TRAMPOLINE;
359 frame->type = FRAME_TYPE_MANAGED;
361 /* Restore ip and sp from the saved register window */
362 window = MONO_SPARC_WINDOW_ADDR (ctx->sp);
363 new_ctx->ip = window [sparc_i7 - 16];
364 new_ctx->sp = (gpointer*)(window [sparc_i6 - 16]);
365 new_ctx->fp = (gpointer*)(MONO_SPARC_WINDOW_ADDR (new_ctx->sp) [sparc_i6 - 16]);
376 ji = mini_jit_info_table_find (domain, (gpointer)(*lmf)->ip, NULL);
381 frame->type = FRAME_TYPE_MANAGED_TO_NATIVE;
383 new_ctx->ip = (*lmf)->ip;
384 new_ctx->sp = (*lmf)->sp;
385 new_ctx->fp = (*lmf)->ebp;
387 *lmf = (*lmf)->previous_lmf;
396 mono_arch_handle_exception (void *sigctx, gpointer obj)
399 struct sigcontext *sc = sigctx;
403 mctx.ip = (gpointer) sc->sigc_regs.tpc;
404 mctx.sp = (gpointer) sc->sigc_regs.u_regs[14];
406 mctx.ip = (gpointer) sc->si_regs.pc;
407 mctx.sp = (gpointer) sc->si_regs.u_regs[14];
410 window = (gpointer*)(((guint8*)mctx.sp) + MONO_SPARC_STACK_BIAS);
411 mctx.fp = window [sparc_fp - 16];
413 mono_handle_exception (&mctx, obj);
416 sc->sigc_regs.tpc = (unsigned long) mctx.ip;
417 sc->sigc_regs.tnpc = (unsigned long) (mctx.ip + 4);
418 sc->sigc_regs.u_regs[14] = (unsigned long) mctx.sp;
420 sc->si_regs.pc = (unsigned long) mctx.ip;
421 sc->si_regs.npc = (unsigned long) (mctx.ip + 4);
422 sc->si_regs.u_regs[14] = (unsigned long) mctx.sp;
425 window = (gpointer*)(((guint8*)mctx.sp) + MONO_SPARC_STACK_BIAS);
426 window [sparc_fp - 16] = mctx.fp;
432 mono_arch_ip_from_context (void *sigctx)
434 struct sigcontext *sc = sigctx;
438 ret = (gpointer) sc->sigc_regs.tpc;
440 ret = (gpointer) sc->si_regs.pc;
446 #else /* !__linux__ */
449 mono_arch_handle_exception (void *sigctx, gpointer obj)
452 ucontext_t *ctx = (ucontext_t*)sigctx;
456 * Access to the machine state using the ucontext_t parameter is somewhat
457 * under documented under solaris. The code below seems to work under
460 g_assert (!ctx->uc_mcontext.gwins);
462 mctx.ip = ctx->uc_mcontext.gregs [REG_PC];
463 mctx.sp = ctx->uc_mcontext.gregs [REG_SP];
464 window = (gpointer*)(((guint8*)mctx.sp) + MONO_SPARC_STACK_BIAS);
465 mctx.fp = window [sparc_fp - 16];
467 mono_handle_exception (&mctx, obj);
469 /* We can't use restore_context to return from a signal handler */
470 ctx->uc_mcontext.gregs [REG_PC] = mctx.ip;
471 ctx->uc_mcontext.gregs [REG_nPC] = mctx.ip + 4;
472 ctx->uc_mcontext.gregs [REG_SP] = mctx.sp;
473 window = (gpointer*)(((guint8*)mctx.sp) + MONO_SPARC_STACK_BIAS);
474 window [sparc_fp - 16] = mctx.fp;
480 mono_arch_ip_from_context (void *sigctx)
482 ucontext_t *ctx = (ucontext_t*)sigctx;
483 return (gpointer)ctx->uc_mcontext.gregs [REG_PC];