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"
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.
43 mono_arch_get_restore_context (MonoTrampInfo
**info
, gboolean aot
)
45 static guint32
*start
;
46 static int inited
= 0;
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
));
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)
85 mono_arch_get_call_filter (MonoTrampInfo
**info
, gboolean aot
)
87 static guint32
*start
;
88 static int inited
= 0;
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.
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);
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 */
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
));
170 throw_exception (MonoObject
*exc
, gpointer sp
, gpointer ip
, gboolean rethrow
, gboolean preserve_ips
)
174 static void (*restore_context
) (MonoContext
*);
177 if (!restore_context
)
178 restore_context
= mono_get_restore_context ();
180 window
= MONO_SPARC_WINDOW_ADDR (sp
);
181 ctx
.sp
= (gpointer
*)sp
;
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
;
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 ();
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
);
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
);
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
));
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);
235 mono_arch_get_throw_exception (MonoTrampInfo
**info
, gboolean aot
)
237 static guint32
* start
;
238 static int inited
= 0;
249 start
= get_throw_exception (FALSE
, FALSE
);
255 mono_arch_get_rethrow_exception (MonoTrampInfo
**info
, gboolean aot
)
257 static guint32
* start
;
258 static int inited
= 0;
269 start
= get_throw_exception (TRUE
, FALSE
);
275 mono_arch_get_rethrow_preserve_exception (MonoTrampInfo
**info
, gboolean aot
)
277 static guint32
* start
;
278 static int inited
= 0;
289 start
= get_throw_exception (TRUE
, TRUE
);
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.
304 mono_arch_get_throw_corlib_exception (MonoTrampInfo
**info
, gboolean aot
)
306 static guint32
*start
;
307 static int inited
= 0;
319 code
= start
= mono_global_codeman_reserve (64 * sizeof (guint32
));
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
);
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
);
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
));
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.
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
)
375 memset (frame
, 0, sizeof (StackFrameInfo
));
381 if (ji
->is_trampoline
)
382 frame
->type
= FRAME_TYPE_TRAMPOLINE
;
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]);
401 ji
= mini_jit_info_table_find (domain
, (gpointer
)(*lmf
)->ip
, NULL
);
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
;
421 mono_arch_handle_exception (void *sigctx
, gpointer obj
)
424 struct sigcontext
*sc
= sigctx
;
428 mctx
.ip
= (gpointer
) sc
->sigc_regs
.tpc
;
429 mctx
.sp
= (gpointer
) sc
->sigc_regs
.u_regs
[14];
431 mctx
.ip
= (gpointer
) sc
->si_regs
.pc
;
432 mctx
.sp
= (gpointer
) sc
->si_regs
.u_regs
[14];
435 window
= (gpointer
*)(((guint8
*)mctx
.sp
) + MONO_SPARC_STACK_BIAS
);
436 mctx
.fp
= window
[sparc_fp
- 16];
438 mono_handle_exception (&mctx
, obj
);
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
;
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
;
450 window
= (gpointer
*)(((guint8
*)mctx
.sp
) + MONO_SPARC_STACK_BIAS
);
451 window
[sparc_fp
- 16] = mctx
.fp
;
457 mono_arch_ip_from_context (void *sigctx
)
459 struct sigcontext
*sc
= sigctx
;
463 ret
= (gpointer
) sc
->sigc_regs
.tpc
;
465 ret
= (gpointer
) sc
->si_regs
.pc
;
471 #else /* !__linux__ */
474 mono_arch_handle_exception (void *sigctx
, gpointer obj
)
477 ucontext_t
*ctx
= (ucontext_t
*)sigctx
;
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
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
;
505 mono_arch_ip_from_context (void *sigctx
)
507 ucontext_t
*ctx
= (ucontext_t
*)sigctx
;
508 return (gpointer
)ctx
->uc_mcontext
.gregs
[REG_PC
];