Fix CounterGroup to use built-in types
[mono-project.git] / mono / mini / exceptions-sparc.c
blobc84d0fc59519bbf25ba9f90dbedefa334316514e
1 /**
2 * \file
3 * exception support for sparc
5 * Authors:
6 * Mark Crichton (crichton@gimp.org)
7 * Dietmar Maurer (dietmar@ximian.com)
9 * (C) 2003 Ximian, Inc.
12 #include <config.h>
13 #include <glib.h>
14 #include <signal.h>
15 #include <string.h>
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>
28 #include "mini.h"
29 #include "mini-sparc.h"
31 #ifndef REG_SP
32 #define REG_SP REG_O6
33 #endif
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.
42 gpointer
43 mono_arch_get_restore_context (MonoTrampInfo **info, gboolean aot)
45 static guint32 *start;
46 static int inited = 0;
47 guint32 *code;
49 g_assert (!aot);
50 if (info)
51 *info = NULL;
53 if (inited)
54 return start;
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);
68 MONO_PROFILER_RAISE (jit_code_buffer, (start, code - start, MONO_PROFILER_CODE_BUFFER_EXCEPTION_HANDLING, NULL));
70 inited = 1;
72 return start;
76 * mono_arch_get_call_filter:
78 * Returns a pointer to a method which calls an exception filter. We
79 * also use this function to call finally handlers (we pass NULL as
80 * @exc object in this case).
82 * call_filter (MonoContext *ctx, gpointer ip)
84 gpointer
85 mono_arch_get_call_filter (MonoTrampInfo **info, gboolean aot)
87 static guint32 *start;
88 static int inited = 0;
89 guint32 *code;
90 int i;
92 g_assert (!aot);
93 if (info)
94 *info = NULL;
96 if (inited)
97 return start;
99 code = start = mono_global_codeman_reserve (64 * sizeof (guint32));
102 * There are two frames here:
103 * - the first frame is used by call_filter
104 * - the second frame is used to run the filter code
107 /* Create first frame */
108 sparc_save_imm (code, sparc_sp, -256, sparc_sp);
110 sparc_mov_reg_reg (code, sparc_i1, sparc_o0);
111 sparc_ldi_imm (code, sparc_i0, G_STRUCT_OFFSET (MonoContext, sp), sparc_o1);
113 /* Create second frame */
114 sparc_save_imm (code, sparc_sp, -256, sparc_sp);
116 sparc_mov_reg_reg (code, sparc_i0, sparc_o0);
117 sparc_mov_reg_reg (code, sparc_i1, sparc_o1);
120 * We need to change %fp to point to the stack frame of the method
121 * containing the filter. But changing %fp also changes the %sp of
122 * the parent frame (the first frame), so if the OS saves the first frame,
123 * it saves it to the stack frame of the method, which is not good.
124 * So flush all register windows to memory before changing %fp.
126 sparc_flushw (code);
128 sparc_mov_reg_reg (code, sparc_fp, sparc_o7);
131 * Modify the second frame so it is identical to the one used in the
132 * method containing the filter.
134 for (i = 0; i < 16; ++i)
135 sparc_ldi_imm (code, sparc_o1, MONO_SPARC_STACK_BIAS + i * sizeof (target_mgreg_t), sparc_l0 + i);
137 /* Save %fp to a location reserved in mono_arch_allocate_vars */
138 sparc_sti_imm (code, sparc_o7, sparc_fp, MONO_SPARC_STACK_BIAS - sizeof (target_mgreg_t));
140 /* Call the filter code, after this returns, %o0 will hold the result */
141 sparc_call_imm (code, sparc_o0, 0);
142 sparc_nop (code);
144 /* Restore original %fp */
145 sparc_ldi_imm (code, sparc_fp, MONO_SPARC_STACK_BIAS - sizeof (target_mgreg_t), sparc_fp);
147 sparc_mov_reg_reg (code, sparc_o0, sparc_i0);
149 /* Return to first frame */
150 sparc_restore (code, sparc_g0, sparc_g0, sparc_g0);
152 /* FIXME: Save locals to the stack */
154 /* Return to caller */
155 sparc_ret (code);
156 /* Return result in delay slot */
157 sparc_restore (code, sparc_o0, sparc_g0, sparc_o0);
159 g_assert ((code - start) < 64);
161 mono_arch_flush_icache ((guint8*)start, (guint8*)code - (guint8*)start);
162 MONO_PROFILER_RAISE (jit_code_buffer, (start, code - start, MONO_PROFILER_CODE_BUFFER_EXCEPTION_HANDLING, NULL));
164 inited = 1;
166 return start;
169 static void
170 throw_exception (MonoObject *exc, gpointer sp, gpointer ip, gboolean rethrow, gboolean preserve_ips)
172 ERROR_DECL (error);
173 MonoContext ctx;
174 static void (*restore_context) (MonoContext *);
175 gpointer *window;
177 if (!restore_context)
178 restore_context = mono_get_restore_context ();
180 window = MONO_SPARC_WINDOW_ADDR (sp);
181 ctx.sp = (gpointer*)sp;
182 ctx.ip = ip;
183 ctx.fp = (gpointer*)(MONO_SPARC_WINDOW_ADDR (sp) [sparc_i6 - 16]);
185 if (mono_object_isinst_checked (exc, mono_defaults.exception_class, error)) {
186 MonoException *mono_ex = (MonoException*)exc;
187 if (!rethrow) {
188 mono_ex->stack_trace = NULL;
189 mono_ex->trace_ips = NULL;
190 } else (preserve_ips) {
191 mono_ex->caught_in_unmanaged = NULL;
194 mono_error_assert_ok (error);
195 mono_handle_exception (&ctx, exc);
196 restore_context (&ctx);
198 g_assert_not_reached ();
201 static gpointer
202 get_throw_exception (gboolean rethrow, gboolean preserve_ips)
204 guint32 *start, *code;
206 code = start = mono_global_codeman_reserve (16 * sizeof (guint32));
208 sparc_save_imm (code, sparc_sp, -512, sparc_sp);
210 sparc_flushw (code);
211 sparc_mov_reg_reg (code, sparc_i0, sparc_o0);
212 sparc_mov_reg_reg (code, sparc_fp, sparc_o1);
213 sparc_mov_reg_reg (code, sparc_i7, sparc_o2);
214 sparc_set (code, rethrow, sparc_o3);
215 sparc_set (code, preserve_ips, sparc_o3);
216 sparc_set (code, throw_exception, sparc_o7);
217 sparc_jmpl (code, sparc_o7, sparc_g0, sparc_callsite);
218 sparc_nop (code);
220 g_assert ((code - start) <= 16);
222 mono_arch_flush_icache ((guint8*)start, (guint8*)code - (guint8*)start);
223 MONO_PROFILER_RAISE (jit_code_buffer, (start, code - start, MONO_PROFILER_CODE_BUFFER_EXCEPTION_HANDLING, NULL));
225 return start;
229 * mono_arch_get_throw_exception:
230 * \returns a function pointer which can be used to raise exceptions.
231 * The returned function has the following
232 * signature: void (*func) (MonoException *exc);
234 gpointer
235 mono_arch_get_throw_exception (MonoTrampInfo **info, gboolean aot)
237 static guint32* start;
238 static int inited = 0;
240 g_assert (!aot);
241 if (info)
242 *info = NULL;
244 if (inited)
245 return start;
247 inited = 1;
249 start = get_throw_exception (FALSE, FALSE);
251 return start;
254 gpointer
255 mono_arch_get_rethrow_exception (MonoTrampInfo **info, gboolean aot)
257 static guint32* start;
258 static int inited = 0;
260 g_assert (!aot);
261 if (info)
262 *info = NULL;
264 if (inited)
265 return start;
267 inited = 1;
269 start = get_throw_exception (TRUE, FALSE);
271 return start;
274 gpointer
275 mono_arch_get_rethrow_preserve_exception (MonoTrampInfo **info, gboolean aot)
277 static guint32* start;
278 static int inited = 0;
280 g_assert (!aot);
281 if (info)
282 *info = NULL;
284 if (inited)
285 return start;
287 inited = 1;
289 start = get_throw_exception (TRUE, TRUE);
291 return start;
295 * mono_arch_get_throw_corlib_exception:
296 * \returns a function pointer which can be used to raise
297 * corlib exceptions. The returned function has the following
298 * signature: void (*func) (guint32 ex_token, guint32 offset);
299 * Here, offset is the offset which needs to be substracted from the caller IP
300 * to get the IP of the throw. Passing the offset has the advantage that it
301 * needs no relocations in the caller.
303 gpointer
304 mono_arch_get_throw_corlib_exception (MonoTrampInfo **info, gboolean aot)
306 static guint32 *start;
307 static int inited = 0;
308 guint32 *code;
309 int reg;
311 g_assert (!aot);
312 if (info)
313 *info = NULL;
315 if (inited)
316 return start;
318 inited = 1;
319 code = start = mono_global_codeman_reserve (64 * sizeof (guint32));
321 #ifdef SPARCV9
322 reg = sparc_g4;
323 #else
324 reg = sparc_g1;
325 #endif
327 sparc_mov_reg_reg (code, sparc_o7, sparc_o2);
328 sparc_save_imm (code, sparc_sp, -160, sparc_sp);
330 sparc_set (code, MONO_TOKEN_TYPE_DEF, sparc_o7);
331 sparc_add (code, FALSE, sparc_i0, sparc_o7, sparc_o1);
332 sparc_set (code, m_class_get_image (mono_defaults.exception_class), sparc_o0);
333 sparc_set (code, mono_exception_from_token, sparc_o7);
334 sparc_jmpl (code, sparc_o7, sparc_g0, sparc_callsite);
335 sparc_nop (code);
337 /* Return to the caller, so exception handling does not see this frame */
338 sparc_restore (code, sparc_o0, sparc_g0, sparc_o0);
340 /* Compute throw ip */
341 sparc_sll_imm (code, sparc_o1, 2, sparc_o1);
342 sparc_sub (code, 0, sparc_o2, sparc_o1, sparc_o7);
344 sparc_set (code, mono_arch_get_throw_exception (NULL, FALSE), reg);
345 /* Use a jmp instead of a call so o7 is preserved */
346 sparc_jmpl_imm (code, reg, 0, sparc_g0);
347 sparc_nop (code);
349 g_assert ((code - start) < 32);
351 mono_arch_flush_icache ((guint8*)start, (guint8*)code - (guint8*)start);
352 MONO_PROFILER_RAISE (jit_code_buffer, (start, code - start, MONO_PROFILER_CODE_BUFFER_EXCEPTION_HANDLING, NULL));
354 return start;
357 /* mono_arch_unwind_frame:
359 * This function is used to gather information from @ctx. It return the
360 * MonoJitInfo of the corresponding function, unwinds one stack frame and
361 * stores the resulting context into @new_ctx. It also stores a string
362 * describing the stack location into @trace (if not NULL), and modifies
363 * the @lmf if necessary. @native_offset return the IP offset from the
364 * start of the function or -1 if that info is not available.
366 gboolean
367 mono_arch_unwind_frame (MonoDomain *domain, MonoJitTlsData *jit_tls,
368 MonoJitInfo *ji, MonoContext *ctx,
369 MonoContext *new_ctx, MonoLMF **lmf,
370 host_mgreg_t **save_locations,
371 StackFrameInfo *frame)
373 gpointer *window;
375 memset (frame, 0, sizeof (StackFrameInfo));
376 frame->ji = ji;
378 *new_ctx = *ctx;
380 if (ji != NULL) {
381 if (ji->is_trampoline)
382 frame->type = FRAME_TYPE_TRAMPOLINE;
383 else
384 frame->type = FRAME_TYPE_MANAGED;
386 /* Restore ip and sp from the saved register window */
387 window = MONO_SPARC_WINDOW_ADDR (ctx->sp);
388 new_ctx->ip = window [sparc_i7 - 16];
389 new_ctx->sp = (gpointer*)(window [sparc_i6 - 16]);
390 new_ctx->fp = (gpointer*)(MONO_SPARC_WINDOW_ADDR (new_ctx->sp) [sparc_i6 - 16]);
392 return TRUE;
394 else {
395 if (!(*lmf))
396 return FALSE;
398 if (!(*lmf)->method)
399 return FALSE;
401 ji = mini_jit_info_table_find (domain, (gpointer)(*lmf)->ip, NULL);
402 if (!ji)
403 return FALSE;
405 frame->ji = ji;
406 frame->type = FRAME_TYPE_MANAGED_TO_NATIVE;
408 new_ctx->ip = (*lmf)->ip;
409 new_ctx->sp = (*lmf)->sp;
410 new_ctx->fp = (*lmf)->ebp;
412 *lmf = (*lmf)->previous_lmf;
414 return TRUE;
418 #ifdef __linux__
420 gboolean
421 mono_arch_handle_exception (void *sigctx, gpointer obj)
423 MonoContext mctx;
424 struct sigcontext *sc = sigctx;
425 gpointer *window;
427 #ifdef SPARCV9
428 mctx.ip = (gpointer) sc->sigc_regs.tpc;
429 mctx.sp = (gpointer) sc->sigc_regs.u_regs[14];
430 #else
431 mctx.ip = (gpointer) sc->si_regs.pc;
432 mctx.sp = (gpointer) sc->si_regs.u_regs[14];
433 #endif
435 window = (gpointer*)(((guint8*)mctx.sp) + MONO_SPARC_STACK_BIAS);
436 mctx.fp = window [sparc_fp - 16];
438 mono_handle_exception (&mctx, obj);
440 #ifdef SPARCV9
441 sc->sigc_regs.tpc = (unsigned long) mctx.ip;
442 sc->sigc_regs.tnpc = (unsigned long) (mctx.ip + 4);
443 sc->sigc_regs.u_regs[14] = (unsigned long) mctx.sp;
444 #else
445 sc->si_regs.pc = (unsigned long) mctx.ip;
446 sc->si_regs.npc = (unsigned long) (mctx.ip + 4);
447 sc->si_regs.u_regs[14] = (unsigned long) mctx.sp;
448 #endif
450 window = (gpointer*)(((guint8*)mctx.sp) + MONO_SPARC_STACK_BIAS);
451 window [sparc_fp - 16] = mctx.fp;
453 return TRUE;
456 gpointer
457 mono_arch_ip_from_context (void *sigctx)
459 struct sigcontext *sc = sigctx;
460 gpointer *ret;
462 #ifdef SPARCV9
463 ret = (gpointer) sc->sigc_regs.tpc;
464 #else
465 ret = (gpointer) sc->si_regs.pc;
466 #endif
468 return ret;
471 #else /* !__linux__ */
473 gboolean
474 mono_arch_handle_exception (void *sigctx, gpointer obj)
476 MonoContext mctx;
477 ucontext_t *ctx = (ucontext_t*)sigctx;
478 gpointer *window;
481 * Access to the machine state using the ucontext_t parameter is somewhat
482 * under documented under solaris. The code below seems to work under
483 * Solaris 9.
485 g_assert (!ctx->uc_mcontext.gwins);
487 mctx.ip = ctx->uc_mcontext.gregs [REG_PC];
488 mctx.sp = ctx->uc_mcontext.gregs [REG_SP];
489 window = (gpointer*)(((guint8*)mctx.sp) + MONO_SPARC_STACK_BIAS);
490 mctx.fp = window [sparc_fp - 16];
492 mono_handle_exception (&mctx, obj);
494 /* We can't use restore_context to return from a signal handler */
495 ctx->uc_mcontext.gregs [REG_PC] = mctx.ip;
496 ctx->uc_mcontext.gregs [REG_nPC] = mctx.ip + 4;
497 ctx->uc_mcontext.gregs [REG_SP] = mctx.sp;
498 window = (gpointer*)(((guint8*)mctx.sp) + MONO_SPARC_STACK_BIAS);
499 window [sparc_fp - 16] = mctx.fp;
501 return TRUE;
504 gpointer
505 mono_arch_ip_from_context (void *sigctx)
507 ucontext_t *ctx = (ucontext_t*)sigctx;
508 return (gpointer)ctx->uc_mcontext.gregs [REG_PC];
511 #endif