Merge pull request #1861 from saper/home-override
[mono-project.git] / mono / mini / exceptions-sparc.c
blob52a93ac1f13f925b20c3bb1961fe255c2e8071b3
1 /*
2 * exceptions-sparc.c: exception support for sparc
4 * Authors:
5 * Mark Crichton (crichton@gimp.org)
6 * Dietmar Maurer (dietmar@ximian.com)
8 * (C) 2003 Ximian, Inc.
9 */
11 #include <config.h>
12 #include <glib.h>
13 #include <signal.h>
14 #include <string.h>
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>
27 #include "mini.h"
28 #include "mini-sparc.h"
30 #ifndef REG_SP
31 #define REG_SP REG_O6
32 #endif
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.
41 gpointer
42 mono_arch_get_restore_context (MonoTrampInfo **info, gboolean aot)
44 static guint32 *start;
45 static int inited = 0;
46 guint32 *code;
48 g_assert (!aot);
49 if (info)
50 *info = NULL;
52 if (inited)
53 return start;
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);
68 inited = 1;
70 return 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)
82 gpointer
83 mono_arch_get_call_filter (MonoTrampInfo **info, gboolean aot)
85 static guint32 *start;
86 static int inited = 0;
87 guint32 *code;
88 int i;
90 g_assert (!aot);
91 if (info)
92 *info = NULL;
94 if (inited)
95 return start;
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.
124 sparc_flushw (code);
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);
140 sparc_nop (code);
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 */
153 sparc_ret (code);
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);
161 inited = 1;
163 return start;
166 static void
167 throw_exception (MonoObject *exc, gpointer sp, gpointer ip, gboolean rethrow)
169 MonoContext ctx;
170 static void (*restore_context) (MonoContext *);
171 gpointer *window;
173 if (!restore_context)
174 restore_context = mono_get_restore_context ();
176 window = MONO_SPARC_WINDOW_ADDR (sp);
177 ctx.sp = (gpointer*)sp;
178 ctx.ip = ip;
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;
183 if (!rethrow) {
184 mono_ex->stack_trace = NULL;
185 mono_ex->trace_ips = NULL;
188 mono_handle_exception (&ctx, exc);
189 restore_context (&ctx);
191 g_assert_not_reached ();
194 static gpointer
195 get_throw_exception (gboolean rethrow)
197 guint32 *start, *code;
199 code = start = mono_global_codeman_reserve (16 * sizeof (guint32));
201 sparc_save_imm (code, sparc_sp, -512, sparc_sp);
203 sparc_flushw (code);
204 sparc_mov_reg_reg (code, sparc_i0, sparc_o0);
205 sparc_mov_reg_reg (code, sparc_fp, sparc_o1);
206 sparc_mov_reg_reg (code, sparc_i7, sparc_o2);
207 sparc_set (code, rethrow, sparc_o3);
208 sparc_set (code, throw_exception, sparc_o7);
209 sparc_jmpl (code, sparc_o7, sparc_g0, sparc_callsite);
210 sparc_nop (code);
212 g_assert ((code - start) <= 16);
214 mono_arch_flush_icache ((guint8*)start, (guint8*)code - (guint8*)start);
216 return start;
220 * mono_arch_get_throw_exception:
222 * Returns a function pointer which can be used to raise exceptions.
223 * The returned function has the following
224 * signature: void (*func) (MonoException *exc);
226 gpointer
227 mono_arch_get_throw_exception (MonoTrampInfo **info, gboolean aot)
229 static guint32* start;
230 static int inited = 0;
232 g_assert (!aot);
233 if (info)
234 *info = NULL;
236 if (inited)
237 return start;
239 inited = 1;
241 start = get_throw_exception (FALSE);
243 return start;
246 gpointer
247 mono_arch_get_rethrow_exception (MonoTrampInfo **info, gboolean aot)
249 static guint32* start;
250 static int inited = 0;
252 g_assert (!aot);
253 if (info)
254 *info = NULL;
256 if (inited)
257 return start;
259 inited = 1;
261 start = get_throw_exception (TRUE);
263 return start;
267 * mono_arch_get_throw_corlib_exception:
269 * Returns a function pointer which can be used to raise
270 * corlib exceptions. The returned function has the following
271 * signature: void (*func) (guint32 ex_token, guint32 offset);
272 * Here, offset is the offset which needs to be substracted from the caller IP
273 * to get the IP of the throw. Passing the offset has the advantage that it
274 * needs no relocations in the caller.
276 gpointer
277 mono_arch_get_throw_corlib_exception (MonoTrampInfo **info, gboolean aot)
279 static guint32 *start;
280 static int inited = 0;
281 guint32 *code;
282 int reg;
284 g_assert (!aot);
285 if (info)
286 *info = NULL;
288 if (inited)
289 return start;
291 inited = 1;
292 code = start = mono_global_codeman_reserve (64 * sizeof (guint32));
294 #ifdef SPARCV9
295 reg = sparc_g4;
296 #else
297 reg = sparc_g1;
298 #endif
300 sparc_mov_reg_reg (code, sparc_o7, sparc_o2);
301 sparc_save_imm (code, sparc_sp, -160, sparc_sp);
303 sparc_set (code, MONO_TOKEN_TYPE_DEF, sparc_o7);
304 sparc_add (code, FALSE, sparc_i0, sparc_o7, sparc_o1);
305 sparc_set (code, mono_defaults.exception_class->image, sparc_o0);
306 sparc_set (code, mono_exception_from_token, sparc_o7);
307 sparc_jmpl (code, sparc_o7, sparc_g0, sparc_callsite);
308 sparc_nop (code);
310 /* Return to the caller, so exception handling does not see this frame */
311 sparc_restore (code, sparc_o0, sparc_g0, sparc_o0);
313 /* Compute throw ip */
314 sparc_sll_imm (code, sparc_o1, 2, sparc_o1);
315 sparc_sub (code, 0, sparc_o2, sparc_o1, sparc_o7);
317 sparc_set (code, mono_arch_get_throw_exception (NULL, FALSE), reg);
318 /* Use a jmp instead of a call so o7 is preserved */
319 sparc_jmpl_imm (code, reg, 0, sparc_g0);
320 sparc_nop (code);
322 g_assert ((code - start) < 32);
324 mono_arch_flush_icache ((guint8*)start, (guint8*)code - (guint8*)start);
326 return start;
329 /* mono_arch_find_jit_info:
331 * This function is used to gather information from @ctx. It return the
332 * MonoJitInfo of the corresponding function, unwinds one stack frame and
333 * stores the resulting context into @new_ctx. It also stores a string
334 * describing the stack location into @trace (if not NULL), and modifies
335 * the @lmf if necessary. @native_offset return the IP offset from the
336 * start of the function or -1 if that info is not available.
338 gboolean
339 mono_arch_find_jit_info (MonoDomain *domain, MonoJitTlsData *jit_tls,
340 MonoJitInfo *ji, MonoContext *ctx,
341 MonoContext *new_ctx, MonoLMF **lmf,
342 mgreg_t **save_locations,
343 StackFrameInfo *frame)
345 gpointer *window;
347 memset (frame, 0, sizeof (StackFrameInfo));
348 frame->ji = ji;
350 *new_ctx = *ctx;
352 if (ji != NULL) {
353 frame->type = FRAME_TYPE_MANAGED;
355 /* Restore ip and sp from the saved register window */
356 window = MONO_SPARC_WINDOW_ADDR (ctx->sp);
357 new_ctx->ip = window [sparc_i7 - 16];
358 new_ctx->sp = (gpointer*)(window [sparc_i6 - 16]);
359 new_ctx->fp = (gpointer*)(MONO_SPARC_WINDOW_ADDR (new_ctx->sp) [sparc_i6 - 16]);
361 return TRUE;
363 else {
364 if (!(*lmf))
365 return FALSE;
367 if (!(*lmf)->method)
368 return FALSE;
370 ji = mini_jit_info_table_find (domain, (gpointer)(*lmf)->ip, NULL);
371 if (!ji)
372 return FALSE;
374 frame->ji = ji;
375 frame->type = FRAME_TYPE_MANAGED_TO_NATIVE;
377 new_ctx->ip = (*lmf)->ip;
378 new_ctx->sp = (*lmf)->sp;
379 new_ctx->fp = (*lmf)->ebp;
381 *lmf = (*lmf)->previous_lmf;
383 return TRUE;
387 #ifdef __linux__
389 gboolean
390 mono_arch_handle_exception (void *sigctx, gpointer obj)
392 MonoContext mctx;
393 struct sigcontext *sc = sigctx;
394 gpointer *window;
396 #ifdef SPARCV9
397 mctx.ip = (gpointer) sc->sigc_regs.tpc;
398 mctx.sp = (gpointer) sc->sigc_regs.u_regs[14];
399 #else
400 mctx.ip = (gpointer) sc->si_regs.pc;
401 mctx.sp = (gpointer) sc->si_regs.u_regs[14];
402 #endif
404 window = (gpointer*)(((guint8*)mctx.sp) + MONO_SPARC_STACK_BIAS);
405 mctx.fp = window [sparc_fp - 16];
407 mono_handle_exception (&mctx, obj);
409 #ifdef SPARCV9
410 sc->sigc_regs.tpc = (unsigned long) mctx.ip;
411 sc->sigc_regs.tnpc = (unsigned long) (mctx.ip + 4);
412 sc->sigc_regs.u_regs[14] = (unsigned long) mctx.sp;
413 #else
414 sc->si_regs.pc = (unsigned long) mctx.ip;
415 sc->si_regs.npc = (unsigned long) (mctx.ip + 4);
416 sc->si_regs.u_regs[14] = (unsigned long) mctx.sp;
417 #endif
419 window = (gpointer*)(((guint8*)mctx.sp) + MONO_SPARC_STACK_BIAS);
420 window [sparc_fp - 16] = mctx.fp;
422 return TRUE;
425 gpointer
426 mono_arch_ip_from_context (void *sigctx)
428 struct sigcontext *sc = sigctx;
429 gpointer *ret;
431 #ifdef SPARCV9
432 ret = (gpointer) sc->sigc_regs.tpc;
433 #else
434 ret = (gpointer) sc->si_regs.pc;
435 #endif
437 return ret;
440 #else /* !__linux__ */
442 gboolean
443 mono_arch_handle_exception (void *sigctx, gpointer obj)
445 MonoContext mctx;
446 ucontext_t *ctx = (ucontext_t*)sigctx;
447 gpointer *window;
450 * Access to the machine state using the ucontext_t parameter is somewhat
451 * under documented under solaris. The code below seems to work under
452 * Solaris 9.
454 g_assert (!ctx->uc_mcontext.gwins);
456 mctx.ip = ctx->uc_mcontext.gregs [REG_PC];
457 mctx.sp = ctx->uc_mcontext.gregs [REG_SP];
458 window = (gpointer*)(((guint8*)mctx.sp) + MONO_SPARC_STACK_BIAS);
459 mctx.fp = window [sparc_fp - 16];
461 mono_handle_exception (&mctx, obj);
463 /* We can't use restore_context to return from a signal handler */
464 ctx->uc_mcontext.gregs [REG_PC] = mctx.ip;
465 ctx->uc_mcontext.gregs [REG_nPC] = mctx.ip + 4;
466 ctx->uc_mcontext.gregs [REG_SP] = mctx.sp;
467 window = (gpointer*)(((guint8*)mctx.sp) + MONO_SPARC_STACK_BIAS);
468 window [sparc_fp - 16] = mctx.fp;
470 return TRUE;
473 gpointer
474 mono_arch_ip_from_context (void *sigctx)
476 ucontext_t *ctx = (ucontext_t*)sigctx;
477 return (gpointer)ctx->uc_mcontext.gregs [REG_PC];
480 #endif