[runtime] Transition the trampoline code to use memory managers for memory allocation...
[mono-project.git] / mono / mini / exceptions-sparc.c
blob39f993dd1fe684dda1303a0d411542cb47a58763
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"
30 #include "mono/utils/mono-tls-inline.h"
32 #ifndef REG_SP
33 #define REG_SP REG_O6
34 #endif
36 #define MONO_SPARC_WINDOW_ADDR(sp) ((gpointer*)(((guint8*)(sp)) + MONO_SPARC_STACK_BIAS))
39 * mono_arch_get_restore_context:
41 * Returns a pointer to a method which restores a previously saved sigcontext.
43 gpointer
44 mono_arch_get_restore_context (MonoTrampInfo **info, gboolean aot)
46 static guint32 *start;
47 static int inited = 0;
48 guint32 *code;
50 g_assert (!aot);
51 if (info)
52 *info = NULL;
54 if (inited)
55 return start;
57 code = start = mono_global_codeman_reserve (32 * sizeof (guint32));
59 sparc_ldi_imm (code, sparc_o0, G_STRUCT_OFFSET (MonoContext, ip), sparc_i7);
60 sparc_ldi_imm (code, sparc_o0, G_STRUCT_OFFSET (MonoContext, sp), sparc_i6);
62 sparc_jmpl_imm (code, sparc_i7, 0, sparc_g0);
63 /* FIXME: This does not return to the correct window */
64 sparc_restore_imm (code, sparc_g0, 0, sparc_g0);
66 g_assert ((code - start) < 32);
68 mono_arch_flush_icache ((guint8*)start, (guint8*)code - (guint8*)start);
69 MONO_PROFILER_RAISE (jit_code_buffer, (start, code - start, MONO_PROFILER_CODE_BUFFER_EXCEPTION_HANDLING, NULL));
71 inited = 1;
73 return start;
77 * mono_arch_get_call_filter:
79 * Returns a pointer to a method which calls an exception filter. We
80 * also use this function to call finally handlers (we pass NULL as
81 * @exc object in this case).
83 * call_filter (MonoContext *ctx, gpointer ip)
85 gpointer
86 mono_arch_get_call_filter (MonoTrampInfo **info, gboolean aot)
88 static guint32 *start;
89 static int inited = 0;
90 guint32 *code;
91 int i;
93 g_assert (!aot);
94 if (info)
95 *info = NULL;
97 if (inited)
98 return start;
100 code = start = mono_global_codeman_reserve (64 * sizeof (guint32));
103 * There are two frames here:
104 * - the first frame is used by call_filter
105 * - the second frame is used to run the filter code
108 /* Create first frame */
109 sparc_save_imm (code, sparc_sp, -256, sparc_sp);
111 sparc_mov_reg_reg (code, sparc_i1, sparc_o0);
112 sparc_ldi_imm (code, sparc_i0, G_STRUCT_OFFSET (MonoContext, sp), sparc_o1);
114 /* Create second frame */
115 sparc_save_imm (code, sparc_sp, -256, sparc_sp);
117 sparc_mov_reg_reg (code, sparc_i0, sparc_o0);
118 sparc_mov_reg_reg (code, sparc_i1, sparc_o1);
121 * We need to change %fp to point to the stack frame of the method
122 * containing the filter. But changing %fp also changes the %sp of
123 * the parent frame (the first frame), so if the OS saves the first frame,
124 * it saves it to the stack frame of the method, which is not good.
125 * So flush all register windows to memory before changing %fp.
127 sparc_flushw (code);
129 sparc_mov_reg_reg (code, sparc_fp, sparc_o7);
132 * Modify the second frame so it is identical to the one used in the
133 * method containing the filter.
135 for (i = 0; i < 16; ++i)
136 sparc_ldi_imm (code, sparc_o1, MONO_SPARC_STACK_BIAS + i * sizeof (target_mgreg_t), sparc_l0 + i);
138 /* Save %fp to a location reserved in mono_arch_allocate_vars */
139 sparc_sti_imm (code, sparc_o7, sparc_fp, MONO_SPARC_STACK_BIAS - sizeof (target_mgreg_t));
141 /* Call the filter code, after this returns, %o0 will hold the result */
142 sparc_call_imm (code, sparc_o0, 0);
143 sparc_nop (code);
145 /* Restore original %fp */
146 sparc_ldi_imm (code, sparc_fp, MONO_SPARC_STACK_BIAS - sizeof (target_mgreg_t), sparc_fp);
148 sparc_mov_reg_reg (code, sparc_o0, sparc_i0);
150 /* Return to first frame */
151 sparc_restore (code, sparc_g0, sparc_g0, sparc_g0);
153 /* FIXME: Save locals to the stack */
155 /* Return to caller */
156 sparc_ret (code);
157 /* Return result in delay slot */
158 sparc_restore (code, sparc_o0, sparc_g0, sparc_o0);
160 g_assert ((code - start) < 64);
162 mono_arch_flush_icache ((guint8*)start, (guint8*)code - (guint8*)start);
163 MONO_PROFILER_RAISE (jit_code_buffer, (start, code - start, MONO_PROFILER_CODE_BUFFER_EXCEPTION_HANDLING, NULL));
165 inited = 1;
167 return start;
170 static void
171 throw_exception (MonoObject *exc, gpointer sp, gpointer ip, gboolean rethrow, gboolean preserve_ips)
173 ERROR_DECL (error);
174 MonoContext ctx;
175 static void (*restore_context) (MonoContext *);
176 gpointer *window;
178 if (!restore_context)
179 restore_context = mono_get_restore_context ();
181 window = MONO_SPARC_WINDOW_ADDR (sp);
182 ctx.sp = (gpointer*)sp;
183 ctx.ip = ip;
184 ctx.fp = (gpointer*)(MONO_SPARC_WINDOW_ADDR (sp) [sparc_i6 - 16]);
186 if (mono_object_isinst_checked (exc, mono_defaults.exception_class, error)) {
187 MonoException *mono_ex = (MonoException*)exc;
188 if (!rethrow && !mono_ex->caught_in_unmanaged) {
189 mono_ex->stack_trace = NULL;
190 mono_ex->trace_ips = NULL;
191 } else (preserve_ips) {
192 mono_ex->caught_in_unmanaged = NULL;
195 mono_error_assert_ok (error);
196 mono_handle_exception (&ctx, exc);
197 restore_context (&ctx);
199 g_assert_not_reached ();
202 static gpointer
203 get_throw_exception (gboolean rethrow, gboolean preserve_ips)
205 guint32 *start, *code;
207 code = start = mono_global_codeman_reserve (16 * sizeof (guint32));
209 sparc_save_imm (code, sparc_sp, -512, sparc_sp);
211 sparc_flushw (code);
212 sparc_mov_reg_reg (code, sparc_i0, sparc_o0);
213 sparc_mov_reg_reg (code, sparc_fp, sparc_o1);
214 sparc_mov_reg_reg (code, sparc_i7, sparc_o2);
215 sparc_set (code, rethrow, sparc_o3);
216 sparc_set (code, preserve_ips, sparc_o3);
217 sparc_set (code, throw_exception, sparc_o7);
218 sparc_jmpl (code, sparc_o7, sparc_g0, sparc_callsite);
219 sparc_nop (code);
221 g_assert ((code - start) <= 16);
223 mono_arch_flush_icache ((guint8*)start, (guint8*)code - (guint8*)start);
224 MONO_PROFILER_RAISE (jit_code_buffer, (start, code - start, MONO_PROFILER_CODE_BUFFER_EXCEPTION_HANDLING, NULL));
226 return start;
230 * mono_arch_get_throw_exception:
231 * \returns a function pointer which can be used to raise exceptions.
232 * The returned function has the following
233 * signature: void (*func) (MonoException *exc);
235 gpointer
236 mono_arch_get_throw_exception (MonoTrampInfo **info, gboolean aot)
238 static guint32* start;
239 static int inited = 0;
241 g_assert (!aot);
242 if (info)
243 *info = NULL;
245 if (inited)
246 return start;
248 inited = 1;
250 start = get_throw_exception (FALSE, FALSE);
252 return start;
255 gpointer
256 mono_arch_get_rethrow_exception (MonoTrampInfo **info, gboolean aot)
258 static guint32* start;
259 static int inited = 0;
261 g_assert (!aot);
262 if (info)
263 *info = NULL;
265 if (inited)
266 return start;
268 inited = 1;
270 start = get_throw_exception (TRUE, FALSE);
272 return start;
275 gpointer
276 mono_arch_get_rethrow_preserve_exception (MonoTrampInfo **info, gboolean aot)
278 static guint32* start;
279 static int inited = 0;
281 g_assert (!aot);
282 if (info)
283 *info = NULL;
285 if (inited)
286 return start;
288 inited = 1;
290 start = get_throw_exception (TRUE, TRUE);
292 return start;
296 * mono_arch_get_throw_corlib_exception:
297 * \returns a function pointer which can be used to raise
298 * corlib exceptions. The returned function has the following
299 * signature: void (*func) (guint32 ex_token, guint32 offset);
300 * Here, offset is the offset which needs to be substracted from the caller IP
301 * to get the IP of the throw. Passing the offset has the advantage that it
302 * needs no relocations in the caller.
304 gpointer
305 mono_arch_get_throw_corlib_exception (MonoTrampInfo **info, gboolean aot)
307 static guint32 *start;
308 static int inited = 0;
309 guint32 *code;
310 int reg;
312 g_assert (!aot);
313 if (info)
314 *info = NULL;
316 if (inited)
317 return start;
319 inited = 1;
320 code = start = mono_global_codeman_reserve (64 * sizeof (guint32));
322 #ifdef SPARCV9
323 reg = sparc_g4;
324 #else
325 reg = sparc_g1;
326 #endif
328 sparc_mov_reg_reg (code, sparc_o7, sparc_o2);
329 sparc_save_imm (code, sparc_sp, -160, sparc_sp);
331 sparc_set (code, MONO_TOKEN_TYPE_DEF, sparc_o7);
332 sparc_add (code, FALSE, sparc_i0, sparc_o7, sparc_o1);
333 sparc_set (code, m_class_get_image (mono_defaults.exception_class), sparc_o0);
334 sparc_set (code, mono_exception_from_token, sparc_o7);
335 sparc_jmpl (code, sparc_o7, sparc_g0, sparc_callsite);
336 sparc_nop (code);
338 /* Return to the caller, so exception handling does not see this frame */
339 sparc_restore (code, sparc_o0, sparc_g0, sparc_o0);
341 /* Compute throw ip */
342 sparc_sll_imm (code, sparc_o1, 2, sparc_o1);
343 sparc_sub (code, 0, sparc_o2, sparc_o1, sparc_o7);
345 sparc_set (code, mono_arch_get_throw_exception (NULL, FALSE), reg);
346 /* Use a jmp instead of a call so o7 is preserved */
347 sparc_jmpl_imm (code, reg, 0, sparc_g0);
348 sparc_nop (code);
350 g_assert ((code - start) < 32);
352 mono_arch_flush_icache ((guint8*)start, (guint8*)code - (guint8*)start);
353 MONO_PROFILER_RAISE (jit_code_buffer, (start, code - start, MONO_PROFILER_CODE_BUFFER_EXCEPTION_HANDLING, NULL));
355 return start;
358 /* mono_arch_unwind_frame:
360 * This function is used to gather information from @ctx. It return the
361 * MonoJitInfo of the corresponding function, unwinds one stack frame and
362 * stores the resulting context into @new_ctx. It also stores a string
363 * describing the stack location into @trace (if not NULL), and modifies
364 * the @lmf if necessary. @native_offset return the IP offset from the
365 * start of the function or -1 if that info is not available.
367 gboolean
368 mono_arch_unwind_frame (MonoDomain *domain, MonoJitTlsData *jit_tls,
369 MonoJitInfo *ji, MonoContext *ctx,
370 MonoContext *new_ctx, MonoLMF **lmf,
371 host_mgreg_t **save_locations,
372 StackFrameInfo *frame)
374 gpointer *window;
376 memset (frame, 0, sizeof (StackFrameInfo));
377 frame->ji = ji;
379 *new_ctx = *ctx;
381 if (ji != NULL) {
382 if (ji->is_trampoline)
383 frame->type = FRAME_TYPE_TRAMPOLINE;
384 else
385 frame->type = FRAME_TYPE_MANAGED;
387 /* Restore ip and sp from the saved register window */
388 window = MONO_SPARC_WINDOW_ADDR (ctx->sp);
389 new_ctx->ip = window [sparc_i7 - 16];
390 new_ctx->sp = (gpointer*)(window [sparc_i6 - 16]);
391 new_ctx->fp = (gpointer*)(MONO_SPARC_WINDOW_ADDR (new_ctx->sp) [sparc_i6 - 16]);
393 return TRUE;
395 else {
396 if (!(*lmf))
397 return FALSE;
399 if (!(*lmf)->method)
400 return FALSE;
402 ji = mini_jit_info_table_find (domain, (gpointer)(*lmf)->ip, NULL);
403 if (!ji)
404 return FALSE;
406 frame->ji = ji;
407 frame->type = FRAME_TYPE_MANAGED_TO_NATIVE;
409 new_ctx->ip = (*lmf)->ip;
410 new_ctx->sp = (*lmf)->sp;
411 new_ctx->fp = (*lmf)->ebp;
413 *lmf = (*lmf)->previous_lmf;
415 return TRUE;
419 #ifdef __linux__
421 gboolean
422 mono_arch_handle_exception (void *sigctx, gpointer obj)
424 MonoContext mctx;
425 struct sigcontext *sc = sigctx;
426 gpointer *window;
428 #ifdef SPARCV9
429 mctx.ip = (gpointer) sc->sigc_regs.tpc;
430 mctx.sp = (gpointer) sc->sigc_regs.u_regs[14];
431 #else
432 mctx.ip = (gpointer) sc->si_regs.pc;
433 mctx.sp = (gpointer) sc->si_regs.u_regs[14];
434 #endif
436 window = (gpointer*)(((guint8*)mctx.sp) + MONO_SPARC_STACK_BIAS);
437 mctx.fp = window [sparc_fp - 16];
439 mono_handle_exception (&mctx, obj);
441 #ifdef SPARCV9
442 sc->sigc_regs.tpc = (unsigned long) mctx.ip;
443 sc->sigc_regs.tnpc = (unsigned long) (mctx.ip + 4);
444 sc->sigc_regs.u_regs[14] = (unsigned long) mctx.sp;
445 #else
446 sc->si_regs.pc = (unsigned long) mctx.ip;
447 sc->si_regs.npc = (unsigned long) (mctx.ip + 4);
448 sc->si_regs.u_regs[14] = (unsigned long) mctx.sp;
449 #endif
451 window = (gpointer*)(((guint8*)mctx.sp) + MONO_SPARC_STACK_BIAS);
452 window [sparc_fp - 16] = mctx.fp;
454 return TRUE;
457 gpointer
458 mono_arch_ip_from_context (void *sigctx)
460 struct sigcontext *sc = sigctx;
461 gpointer *ret;
463 #ifdef SPARCV9
464 ret = (gpointer) sc->sigc_regs.tpc;
465 #else
466 ret = (gpointer) sc->si_regs.pc;
467 #endif
469 return ret;
472 #else /* !__linux__ */
474 gboolean
475 mono_arch_handle_exception (void *sigctx, gpointer obj)
477 MonoContext mctx;
478 ucontext_t *ctx = (ucontext_t*)sigctx;
479 gpointer *window;
482 * Access to the machine state using the ucontext_t parameter is somewhat
483 * under documented under solaris. The code below seems to work under
484 * Solaris 9.
486 g_assert (!ctx->uc_mcontext.gwins);
488 mctx.ip = ctx->uc_mcontext.gregs [REG_PC];
489 mctx.sp = ctx->uc_mcontext.gregs [REG_SP];
490 window = (gpointer*)(((guint8*)mctx.sp) + MONO_SPARC_STACK_BIAS);
491 mctx.fp = window [sparc_fp - 16];
493 mono_handle_exception (&mctx, obj);
495 /* We can't use restore_context to return from a signal handler */
496 ctx->uc_mcontext.gregs [REG_PC] = mctx.ip;
497 ctx->uc_mcontext.gregs [REG_nPC] = mctx.ip + 4;
498 ctx->uc_mcontext.gregs [REG_SP] = mctx.sp;
499 window = (gpointer*)(((guint8*)mctx.sp) + MONO_SPARC_STACK_BIAS);
500 window [sparc_fp - 16] = mctx.fp;
502 return TRUE;
505 gpointer
506 mono_arch_ip_from_context (void *sigctx)
508 ucontext_t *ctx = (ucontext_t*)sigctx;
509 return (gpointer)ctx->uc_mcontext.gregs [REG_PC];
512 #endif