2 * exceptions-sparc.c: exception support for 64 bit 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>
27 #include "mini-sparc.h"
30 * mono_arch_get_restore_context:
32 * Returns a pointer to a method which restores a previously saved sigcontext.
35 mono_arch_get_restore_context (void)
37 static guint32 start [32];
38 static int inited = 0;
46 sparc_ld_imm (code, sparc_o0, G_STRUCT_OFFSET (MonoContext, ip), sparc_i7);
47 sparc_ld_imm (code, sparc_o0, G_STRUCT_OFFSET (MonoContext, sp), sparc_i6);
49 sparc_jmpl_imm (code, sparc_i7, 0, sparc_g0);
50 /* FIXME: This does not return to the correct window */
51 sparc_restore_imm (code, sparc_g0, 0, sparc_g0);
53 g_assert ((code - start) < 32);
61 * mono_arch_get_call_filter:
63 * Returns a pointer to a method which calls an exception filter. We
64 * also use this function to call finally handlers (we pass NULL as
65 * @exc object in this case).
67 * call_filter (MonoContext *ctx, gpointer ip)
70 mono_arch_get_call_filter (void)
72 static guint32 start [64];
73 static int inited = 0;
83 * There are two frames here:
84 * - the first frame is used by call_filter
85 * - the second frame is used to run the filter code
88 /* Create first frame */
89 sparc_save_imm (code, sparc_sp, -160, sparc_sp);
91 sparc_mov_reg_reg (code, sparc_i1, sparc_o0);
92 sparc_ld_imm (code, sparc_i0, G_STRUCT_OFFSET (MonoContext, sp), sparc_o1);
94 /* Create second frame */
95 sparc_save_imm (code, sparc_sp, -160, sparc_sp);
97 sparc_mov_reg_reg (code, sparc_i0, sparc_o0);
98 sparc_mov_reg_reg (code, sparc_i1, sparc_o1);
101 * We need to change %fp to point to the stack frame of the method
102 * containing the filter. But changing %fp also changes the %sp of
103 * the parent frame (the first frame), so if the OS saves the first frame,
104 * it saves it to the stack frame of the method, which is not good.
105 * So flush all register windows to memory before changing %fp.
109 sparc_mov_reg_reg (code, sparc_fp, sparc_o7);
112 * Modify the second frame so it is identical to the one used in the
113 * method containing the filter.
115 for (i = 0; i < 16; ++i)
116 sparc_ld_imm (code, sparc_o1, i * 4, sparc_l0 + i);
118 /* Save %fp to a location reserved in mono_arch_allocate_vars */
119 sparc_st_imm (code, sparc_o7, sparc_fp, -4);
121 /* Call the filter code, after this returns, %o0 will hold the result */
122 sparc_call_imm (code, sparc_o0, 0);
125 /* Restore original %fp */
126 sparc_ld_imm (code, sparc_fp, -4, sparc_fp);
128 sparc_mov_reg_reg (code, sparc_o0, sparc_i0);
130 /* Return to first frame */
131 sparc_restore (code, sparc_g0, sparc_g0, sparc_g0);
133 /* FIXME: Save locals to the stack */
135 /* Return to caller */
137 /* Return result in delay slot */
138 sparc_restore (code, sparc_o0, sparc_g0, sparc_o0);
140 g_assert ((code - start) < 64);
148 throw_exception (MonoObject *ex, guint32 sp, guint32 ip)
151 static void (*restore_context) (MonoContext *);
153 if (!restore_context)
154 restore_context = mono_arch_get_restore_context ();
156 ctx.sp = (guint32*)sp;
158 ctx.fp = (guint32*)ctx.sp [sparc_i6 - 16];
160 mono_handle_exception (&ctx, ex, FALSE);
161 restore_context (&ctx);
163 g_assert_not_reached ();
167 * mono_arch_get_throw_exception_by_name:
169 * Returns a function pointer which can be used to raise exceptions.
170 * The returned function has the following
171 * signature: void (*func) (char *exc_name);
174 mono_arch_get_throw_exception (void)
176 static guint32 start [32];
177 static int inited = 0;
186 sparc_save_imm (code, sparc_sp, -160, sparc_sp);
189 sparc_mov_reg_reg (code, sparc_i0, sparc_o0);
190 sparc_mov_reg_reg (code, sparc_fp, sparc_o1);
191 sparc_mov_reg_reg (code, sparc_i7, sparc_o2);
192 sparc_set (code, throw_exception, sparc_o7);
193 sparc_jmpl (code, sparc_o7, sparc_g0, sparc_callsite);
196 g_assert ((code - start) < 32);
202 * mono_arch_get_throw_exception_by_name:
204 * Returns a function pointer which can be used to raise
205 * corlib exceptions. The returned function has the following
206 * signature: void (*func) (char *exc_name, gpointer ip);
209 mono_arch_get_throw_exception_by_name (void)
211 static guint32 start [32];
212 static int inited = 0;
221 sparc_save_imm (code, sparc_sp, -160, sparc_sp);
223 sparc_mov_reg_reg (code, sparc_i0, sparc_o2);
224 sparc_set (code, mono_defaults.corlib, sparc_o0);
225 sparc_set (code, "System", sparc_o1);
226 sparc_set (code, mono_exception_from_name, sparc_o7);
227 sparc_jmpl (code, sparc_o7, sparc_g0, sparc_callsite);
230 /* Return to the caller, so exception handling does not see this frame */
231 sparc_restore (code, sparc_o0, sparc_g0, sparc_o0);
233 /* Put original return address into %o7 */
234 sparc_mov_reg_reg (code, sparc_o1, sparc_o7);
235 sparc_set (code, mono_arch_get_throw_exception (), sparc_g1);
236 /* Use a jmp instead of a call so o7 is preserved */
237 sparc_jmpl_imm (code, sparc_g1, 0, sparc_g0);
240 g_assert ((code - start) < 32);
245 /* mono_arch_find_jit_info:
247 * This function is used to gather information from @ctx. It return the
248 * MonoJitInfo of the corresponding function, unwinds one stack frame and
249 * stores the resulting context into @new_ctx. It also stores a string
250 * describing the stack location into @trace (if not NULL), and modifies
251 * the @lmf if necessary. @native_offset return the IP offset from the
252 * start of the function or -1 if that info is not available.
255 mono_arch_find_jit_info (MonoDomain *domain, MonoJitTlsData *jit_tls, MonoJitInfo *res, MonoJitInfo *prev_ji, MonoContext *ctx,
256 MonoContext *new_ctx, char **trace, MonoLMF **lmf, int *native_offset,
260 gpointer ip = MONO_CONTEXT_GET_IP (ctx);
263 /* Avoid costly table lookup during stack overflow */
264 if (prev_ji && (ip > prev_ji->code_start && ((guint8*)ip < ((guint8*)prev_ji->code_start) + prev_ji->code_size)))
267 ji = mono_jit_info_table_find (domain, ip);
279 char *source_location, *tmpaddr, *fname;
280 gint32 address, iloffset;
284 address = (char *)ip - (char *)ji->code_start;
287 *native_offset = address;
290 if (!ji->method->wrapper_type)
294 source_location = mono_debug_source_location_from_address (ji->method, address, NULL, domain);
295 iloffset = mono_debug_il_offset_from_address (ji->method, address, domain);
298 tmpaddr = g_strdup_printf ("<0x%05x>", address);
300 tmpaddr = g_strdup_printf ("[0x%05x]", iloffset);
302 fname = mono_method_full_name (ji->method, TRUE);
305 *trace = g_strdup_printf ("in %s (at %s) %s", tmpaddr, source_location, fname);
307 *trace = g_strdup_printf ("in %s %s", tmpaddr, fname);
310 g_free (source_location);
314 if (*lmf && (MONO_CONTEXT_GET_BP (ctx) >= (gpointer)(*lmf)->ebp)) {
315 /* remove any unused lmf */
316 *lmf = (*lmf)->previous_lmf;
319 /* Restore ip and sp from the saved register window */
320 window = (guint32*)ctx->sp;
321 new_ctx->ip = window [sparc_i7 - 16];
322 new_ctx->sp = (guint32*)(window [sparc_i6 - 16]);
323 new_ctx->fp = (guint32*)(new_ctx->sp [sparc_i6 - 16]);
338 *trace = g_strdup_printf ("in (unmanaged) %s", mono_method_full_name ((*lmf)->method, TRUE));
340 if ((ji = mono_jit_info_table_find (domain, (gpointer)(*lmf)->ip))) {
343 memset (res, 0, sizeof (MonoJitInfo));
344 res->method = (*lmf)->method;
347 new_ctx->ip = (*lmf)->ip;
348 new_ctx->sp = (*lmf)->sp;
349 new_ctx->fp = (*lmf)->ebp;
351 *lmf = (*lmf)->previous_lmf;
358 mono_arch_has_unwind_info (gconstpointer addr)
364 mono_arch_handle_exception (void *sigctx, gpointer obj, gboolean test_only)
367 ucontext_t *ctx = (ucontext_t*)sigctx;
370 * Access to the machine state using the ucontext_t parameter is somewhat
371 * under documented under solaris. The code below seems to work under
374 g_assert (!ctx->uc_mcontext.gwins);
376 mctx.ip = ctx->uc_mcontext.gregs [REG_PC];
377 mctx.sp = ctx->uc_mcontext.gregs [REG_SP];
378 mctx.fp = mctx.sp [sparc_fp - 16];
380 mono_handle_exception (&mctx, obj, test_only);
382 /* We can't use restore_context to return from a signal handler */
383 ctx->uc_mcontext.gregs [REG_PC] = mctx.ip;
384 ctx->uc_mcontext.gregs [REG_nPC] = mctx.ip + 4;
385 ctx->uc_mcontext.gregs [REG_SP] = mctx.sp;
386 mctx.sp [sparc_fp - 16] = mctx.fp;