3 * exception support for sparc
6 * Mark Crichton (crichton@gimp.org)
7 * Dietmar Maurer (dietmar@ximian.com)
9 * (C) 2003 Ximian, Inc.
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>
29 #include "mini-sparc.h"
30 #include "mono/utils/mono-tls-inline.h"
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.
44 mono_arch_get_restore_context (MonoTrampInfo
**info
, gboolean aot
)
46 static guint32
*start
;
47 static int inited
= 0;
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
));
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)
86 mono_arch_get_call_filter (MonoTrampInfo
**info
, gboolean aot
)
88 static guint32
*start
;
89 static int inited
= 0;
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.
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);
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 */
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
));
171 throw_exception (MonoObject
*exc
, gpointer sp
, gpointer ip
, gboolean rethrow
, gboolean preserve_ips
)
175 static void (*restore_context
) (MonoContext
*);
178 if (!restore_context
)
179 restore_context
= mono_get_restore_context ();
181 window
= MONO_SPARC_WINDOW_ADDR (sp
);
182 ctx
.sp
= (gpointer
*)sp
;
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 ();
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
);
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
);
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
));
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);
236 mono_arch_get_throw_exception (MonoTrampInfo
**info
, gboolean aot
)
238 static guint32
* start
;
239 static int inited
= 0;
250 start
= get_throw_exception (FALSE
, FALSE
);
256 mono_arch_get_rethrow_exception (MonoTrampInfo
**info
, gboolean aot
)
258 static guint32
* start
;
259 static int inited
= 0;
270 start
= get_throw_exception (TRUE
, FALSE
);
276 mono_arch_get_rethrow_preserve_exception (MonoTrampInfo
**info
, gboolean aot
)
278 static guint32
* start
;
279 static int inited
= 0;
290 start
= get_throw_exception (TRUE
, TRUE
);
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.
305 mono_arch_get_throw_corlib_exception (MonoTrampInfo
**info
, gboolean aot
)
307 static guint32
*start
;
308 static int inited
= 0;
320 code
= start
= mono_global_codeman_reserve (64 * sizeof (guint32
));
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
);
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
);
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
));
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.
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
)
376 memset (frame
, 0, sizeof (StackFrameInfo
));
382 if (ji
->is_trampoline
)
383 frame
->type
= FRAME_TYPE_TRAMPOLINE
;
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]);
402 ji
= mini_jit_info_table_find (domain
, (gpointer
)(*lmf
)->ip
, NULL
);
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
;
422 mono_arch_handle_exception (void *sigctx
, gpointer obj
)
425 struct sigcontext
*sc
= sigctx
;
429 mctx
.ip
= (gpointer
) sc
->sigc_regs
.tpc
;
430 mctx
.sp
= (gpointer
) sc
->sigc_regs
.u_regs
[14];
432 mctx
.ip
= (gpointer
) sc
->si_regs
.pc
;
433 mctx
.sp
= (gpointer
) sc
->si_regs
.u_regs
[14];
436 window
= (gpointer
*)(((guint8
*)mctx
.sp
) + MONO_SPARC_STACK_BIAS
);
437 mctx
.fp
= window
[sparc_fp
- 16];
439 mono_handle_exception (&mctx
, obj
);
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
;
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
;
451 window
= (gpointer
*)(((guint8
*)mctx
.sp
) + MONO_SPARC_STACK_BIAS
);
452 window
[sparc_fp
- 16] = mctx
.fp
;
458 mono_arch_ip_from_context (void *sigctx
)
460 struct sigcontext
*sc
= sigctx
;
464 ret
= (gpointer
) sc
->sigc_regs
.tpc
;
466 ret
= (gpointer
) sc
->si_regs
.pc
;
472 #else /* !__linux__ */
475 mono_arch_handle_exception (void *sigctx
, gpointer obj
)
478 ucontext_t
*ctx
= (ucontext_t
*)sigctx
;
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
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
;
506 mono_arch_ip_from_context (void *sigctx
)
508 ucontext_t
*ctx
= (ucontext_t
*)sigctx
;
509 return (gpointer
)ctx
->uc_mcontext
.gregs
[REG_PC
];