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"
33 #define MONO_SPARC_WINDOW_ADDR(sp) ((gpointer*)(((guint8*)(sp)) + MONO_SPARC_STACK_BIAS))
36 * mono_arch_get_restore_context:
38 * Returns a pointer to a method which restores a previously saved sigcontext.
41 mono_arch_get_restore_context (void)
43 static guint32 start [32];
44 static int inited = 0;
52 sparc_ldi_imm (code, sparc_o0, G_STRUCT_OFFSET (MonoContext, ip), sparc_i7);
53 sparc_ldi_imm (code, sparc_o0, G_STRUCT_OFFSET (MonoContext, sp), sparc_i6);
55 sparc_jmpl_imm (code, sparc_i7, 0, sparc_g0);
56 /* FIXME: This does not return to the correct window */
57 sparc_restore_imm (code, sparc_g0, 0, sparc_g0);
59 g_assert ((code - start) < 32);
67 * mono_arch_get_call_filter:
69 * Returns a pointer to a method which calls an exception filter. We
70 * also use this function to call finally handlers (we pass NULL as
71 * @exc object in this case).
73 * call_filter (MonoContext *ctx, gpointer ip)
76 mono_arch_get_call_filter (void)
78 static guint32 start [64];
79 static int inited = 0;
89 * There are two frames here:
90 * - the first frame is used by call_filter
91 * - the second frame is used to run the filter code
94 /* Create first frame */
95 sparc_save_imm (code, sparc_sp, -256, sparc_sp);
97 sparc_mov_reg_reg (code, sparc_i1, sparc_o0);
98 sparc_ldi_imm (code, sparc_i0, G_STRUCT_OFFSET (MonoContext, sp), sparc_o1);
100 /* Create second frame */
101 sparc_save_imm (code, sparc_sp, -256, sparc_sp);
103 sparc_mov_reg_reg (code, sparc_i0, sparc_o0);
104 sparc_mov_reg_reg (code, sparc_i1, sparc_o1);
107 * We need to change %fp to point to the stack frame of the method
108 * containing the filter. But changing %fp also changes the %sp of
109 * the parent frame (the first frame), so if the OS saves the first frame,
110 * it saves it to the stack frame of the method, which is not good.
111 * So flush all register windows to memory before changing %fp.
115 sparc_mov_reg_reg (code, sparc_fp, sparc_o7);
118 * Modify the second frame so it is identical to the one used in the
119 * method containing the filter.
121 for (i = 0; i < 16; ++i)
122 sparc_ldi_imm (code, sparc_o1, MONO_SPARC_STACK_BIAS + i * sizeof (gpointer), sparc_l0 + i);
124 /* Save %fp to a location reserved in mono_arch_allocate_vars */
125 sparc_sti_imm (code, sparc_o7, sparc_fp, MONO_SPARC_STACK_BIAS - sizeof (gpointer));
127 /* Call the filter code, after this returns, %o0 will hold the result */
128 sparc_call_imm (code, sparc_o0, 0);
131 /* Restore original %fp */
132 sparc_ldi_imm (code, sparc_fp, MONO_SPARC_STACK_BIAS - sizeof (gpointer), sparc_fp);
134 sparc_mov_reg_reg (code, sparc_o0, sparc_i0);
136 /* Return to first frame */
137 sparc_restore (code, sparc_g0, sparc_g0, sparc_g0);
139 /* FIXME: Save locals to the stack */
141 /* Return to caller */
143 /* Return result in delay slot */
144 sparc_restore (code, sparc_o0, sparc_g0, sparc_o0);
146 g_assert ((code - start) < 64);
154 throw_exception (MonoObject *exc, gpointer sp, gpointer ip)
157 static void (*restore_context) (MonoContext *);
160 if (!restore_context)
161 restore_context = mono_arch_get_restore_context ();
163 window = MONO_SPARC_WINDOW_ADDR (sp);
164 ctx.sp = (gpointer*)sp;
166 ctx.fp = (gpointer*)(MONO_SPARC_WINDOW_ADDR (sp) [sparc_i6 - 16]);
168 if (mono_object_isinst (exc, mono_defaults.exception_class)) {
169 MonoException *mono_ex = (MonoException*)exc;
170 mono_ex->stack_trace = NULL;
172 mono_handle_exception (&ctx, exc, ip, FALSE);
173 restore_context (&ctx);
175 g_assert_not_reached ();
179 * mono_arch_get_throw_exception_by_name:
181 * Returns a function pointer which can be used to raise exceptions.
182 * The returned function has the following
183 * signature: void (*func) (char *exc_name);
186 mono_arch_get_throw_exception (void)
188 static guint32 start [32];
189 static int inited = 0;
198 sparc_save_imm (code, sparc_sp, -512, sparc_sp);
201 sparc_mov_reg_reg (code, sparc_i0, sparc_o0);
202 sparc_mov_reg_reg (code, sparc_fp, sparc_o1);
203 sparc_mov_reg_reg (code, sparc_i7, sparc_o2);
204 sparc_set (code, throw_exception, sparc_o7);
205 sparc_jmpl (code, sparc_o7, sparc_g0, sparc_callsite);
208 g_assert ((code - start) < 32);
214 * mono_arch_get_throw_exception_by_name:
216 * Returns a function pointer which can be used to raise
217 * corlib exceptions. The returned function has the following
218 * signature: void (*func) (char *exc_name, gpointer ip);
221 mono_arch_get_throw_exception_by_name (void)
223 static guint32 start [64];
224 static int inited = 0;
240 sparc_save_imm (code, sparc_sp, -160, sparc_sp);
242 sparc_mov_reg_reg (code, sparc_i0, sparc_o2);
243 sparc_set (code, mono_defaults.corlib, sparc_o0);
244 sparc_set (code, "System", sparc_o1);
245 sparc_set (code, mono_exception_from_name, sparc_o7);
246 sparc_jmpl (code, sparc_o7, sparc_g0, sparc_callsite);
249 /* Return to the caller, so exception handling does not see this frame */
250 sparc_restore (code, sparc_o0, sparc_g0, sparc_o0);
252 /* Put original return address into %o7 */
253 sparc_mov_reg_reg (code, sparc_o1, sparc_o7);
254 sparc_set (code, mono_arch_get_throw_exception (), reg);
255 /* Use a jmp instead of a call so o7 is preserved */
256 sparc_jmpl_imm (code, reg, 0, sparc_g0);
259 g_assert ((code - start) < 32);
264 /* mono_arch_find_jit_info:
266 * This function is used to gather information from @ctx. It return the
267 * MonoJitInfo of the corresponding function, unwinds one stack frame and
268 * stores the resulting context into @new_ctx. It also stores a string
269 * describing the stack location into @trace (if not NULL), and modifies
270 * the @lmf if necessary. @native_offset return the IP offset from the
271 * start of the function or -1 if that info is not available.
274 mono_arch_find_jit_info (MonoDomain *domain, MonoJitTlsData *jit_tls, MonoJitInfo *res, MonoJitInfo *prev_ji, MonoContext *ctx,
275 MonoContext *new_ctx, char **trace, MonoLMF **lmf, int *native_offset,
279 gpointer ip = MONO_CONTEXT_GET_IP (ctx);
282 /* Avoid costly table lookup during stack overflow */
283 if (prev_ji && (ip > prev_ji->code_start && ((guint8*)ip < ((guint8*)prev_ji->code_start) + prev_ji->code_size)))
286 ji = mono_jit_info_table_find (domain, ip);
298 char *source_location, *tmpaddr, *fname;
299 gint32 address, iloffset;
303 address = (char *)ip - (char *)ji->code_start;
306 *native_offset = address;
309 if (!ji->method->wrapper_type)
313 source_location = mono_debug_source_location_from_address (ji->method, address, NULL, domain);
314 iloffset = mono_debug_il_offset_from_address (ji->method, address, domain);
317 tmpaddr = g_strdup_printf ("<0x%05x>", address);
319 tmpaddr = g_strdup_printf ("[0x%05x]", iloffset);
321 fname = mono_method_full_name (ji->method, TRUE);
324 *trace = g_strdup_printf ("in %s (at %s) %s", tmpaddr, source_location, fname);
326 *trace = g_strdup_printf ("in %s %s", tmpaddr, fname);
329 g_free (source_location);
333 if (*lmf && (MONO_CONTEXT_GET_BP (ctx) >= (gpointer)(*lmf)->ebp)) {
334 /* remove any unused lmf */
335 *lmf = (*lmf)->previous_lmf;
338 /* Restore ip and sp from the saved register window */
339 window = MONO_SPARC_WINDOW_ADDR (ctx->sp);
340 new_ctx->ip = window [sparc_i7 - 16];
341 new_ctx->sp = (gpointer*)(window [sparc_i6 - 16]);
342 new_ctx->fp = (gpointer*)(MONO_SPARC_WINDOW_ADDR (new_ctx->sp) [sparc_i6 - 16]);
357 *trace = g_strdup_printf ("in (unmanaged) %s", mono_method_full_name ((*lmf)->method, TRUE));
359 if ((ji = mono_jit_info_table_find (domain, (gpointer)(*lmf)->ip))) {
362 memset (res, 0, sizeof (MonoJitInfo));
363 res->method = (*lmf)->method;
366 new_ctx->ip = (*lmf)->ip;
367 new_ctx->sp = (*lmf)->sp;
368 new_ctx->fp = (*lmf)->ebp;
370 *lmf = (*lmf)->previous_lmf;
377 mono_arch_has_unwind_info (gconstpointer addr)
383 mono_arch_handle_exception (void *sigctx, gpointer obj, gboolean test_only)
386 ucontext_t *ctx = (ucontext_t*)sigctx;
390 * Access to the machine state using the ucontext_t parameter is somewhat
391 * under documented under solaris. The code below seems to work under
395 g_assert (!ctx->uc_mcontext.gwins);
397 /* better, but doesn't work all the time. need to rethink! */
398 g_assert (!ctx->uc_mcontext.gregs);
401 mctx.ip = ctx->uc_mcontext.gregs [REG_PC];
402 mctx.sp = ctx->uc_mcontext.gregs [REG_SP];
403 window = (gpointer*)(((guint8*)mctx.sp) + MONO_SPARC_STACK_BIAS);
404 mctx.fp = window [sparc_fp - 16];
406 mono_handle_exception (&mctx, obj, mctx.ip, test_only);
408 /* We can't use restore_context to return from a signal handler */
409 ctx->uc_mcontext.gregs [REG_PC] = mctx.ip;
410 ctx->uc_mcontext.gregs [REG_nPC] = mctx.ip + 4;
411 ctx->uc_mcontext.gregs [REG_SP] = mctx.sp;
412 window = (gpointer*)(((guint8*)mctx.sp) + MONO_SPARC_STACK_BIAS);
413 window [sparc_fp - 16] = mctx.fp;
419 mono_arch_ip_from_context (void *sigctx)
421 ucontext_t *ctx = (ucontext_t*)sigctx;
422 return (gpointer)ctx->uc_mcontext.gregs [REG_PC];