2 * exceptions-ia64.c: exception support for IA64
5 * Zoltan Varga (vargaz@gmail.com)
7 * (C) 2001 Ximian, Inc.
11 * We implement exception handling with the help of the libuwind library:
13 * http://www.hpl.hp.com/research/linux/libunwind/
15 * Under IA64 all functions are assumed to have unwind info, we do not need to save
16 * the machine state in the LMF. But we have to generate unwind info for all
17 * dynamically generated code.
24 #include <sys/ucontext.h>
26 #include <mono/arch/ia64/ia64-codegen.h>
27 #include <mono/metadata/appdomain.h>
28 #include <mono/metadata/tabledefs.h>
29 #include <mono/metadata/threads.h>
30 #include <mono/metadata/debug-helpers.h>
31 #include <mono/metadata/exception.h>
32 #include <mono/metadata/gc-internal.h>
33 #include <mono/metadata/mono-debug.h>
36 #include "mini-ia64.h"
38 #define ALIGN_TO(val,align) (((val) + ((align) - 1)) & ~((align) - 1))
40 #define NOT_IMPLEMENTED g_assert_not_reached ()
42 #define GP_SCRATCH_REG 31
43 #define GP_SCRATCH_REG2 30
46 mono_create_ftnptr (gpointer ptr)
48 gpointer *desc = g_malloc (2 * sizeof (gpointer));
56 restore_context (MonoContext *ctx)
60 /* Set this to 0 to tell OP_START_HANDLER that it doesn't have to set the frame pointer */
61 res = unw_set_reg (&ctx->cursor, UNW_IA64_GR + 15, 0);
64 unw_resume (&ctx->cursor);
68 * mono_arch_get_restore_context:
70 * Returns a pointer to a method which restores a previously saved sigcontext.
73 mono_arch_get_restore_context (void)
75 return restore_context;
79 get_real_call_filter (void)
82 static gboolean inited = FALSE;
83 Ia64CodegenState code;
84 int i, in0, local0, out0, nout;
86 unw_dyn_region_info_t *r_pro;
91 start = mono_global_codeman_reserve (1024);
93 /* int call_filter (guint64 fp, guint64 ip) */
96 * We have to create a register+stack frame similar to the frame which contains
99 * - setting up a register stack frame
100 * These cannot be set up in this function, because the fp register is a stacked
101 * register which is different in each method. Also, the register stack frame is
102 * different in each method. So we pass the FP value in a a non-stacked
103 * register and the code generated by the OP_START_HANDLER opcode will copy it
104 * to the appropriate register after setting up the register stack frame.
105 * The stacked registers are not need to be set since variables used in
106 * handler registers are never allocated to registers.
114 ia64_codegen_init (code, start);
116 ia64_alloc (code, local0 + 0, local0 - in0, out0 - local0, nout, 0);
117 ia64_mov_from_br (code, local0 + 1, IA64_B0);
119 /* FIXME: This depends on the current instruction emitter */
121 r_pro = g_malloc0 (_U_dyn_region_info_size (2));
123 r_pro->insn_count = 6;
125 _U_dyn_op_save_reg (&r_pro->op[i++], _U_QP_TRUE, /* when=*/ 2,
126 /* reg=*/ UNW_IA64_AR_PFS, /* dst=*/ UNW_IA64_GR + local0 + 0);
127 _U_dyn_op_save_reg (&r_pro->op[i++], _U_QP_TRUE, /* when=*/ 5,
128 /* reg=*/ UNW_IA64_RP, /* dst=*/ UNW_IA64_GR + local0 + 1);
129 g_assert ((unsigned) i <= r_pro->op_count);
132 ia64_mov (code, IA64_R15, in0 + 0);
134 ia64_mov_to_br (code, IA64_B0, in0 + 1);
136 ia64_mov_from_ip (code, GP_SCRATCH_REG);
137 ia64_adds_imm (code, GP_SCRATCH_REG, 3 * 16, GP_SCRATCH_REG);
139 /* Call the filter */
140 ia64_br_cond_reg (code, IA64_B0);
142 /* R8 contains the result of the filter */
143 ia64_mov_to_ar_i (code, IA64_PFS, local0 + 0);
144 ia64_mov_ret_to_br (code, IA64_B0, local0 + 1);
145 ia64_br_ret_reg (code, IA64_B0);
147 ia64_codegen_close (code);
149 g_assert ((code.buf - start) <= 256);
151 mono_arch_flush_icache (start, code.buf - start);
153 di = g_malloc0 (sizeof (unw_dyn_info_t));
154 di->start_ip = (unw_word_t) start;
155 di->end_ip = (unw_word_t) code.buf;
157 di->format = UNW_INFO_FORMAT_DYNAMIC;
158 di->u.pi.name_ptr = (unw_word_t)"throw_trampoline";
159 di->u.pi.regions = r_pro;
161 _U_dyn_register (di);
163 return mono_create_ftnptr (start);
167 call_filter (MonoContext *ctx, gpointer ip)
169 static int (*filter) (MonoContext *, gpointer) = NULL;
170 gpointer fp = MONO_CONTEXT_GET_BP (ctx);
172 /* FIXME: thread safety */
174 filter = get_real_call_filter ();
176 return filter (fp, ip);
180 * mono_arch_get_call_filter:
182 * Returns a pointer to a method which calls an exception filter. We
183 * also use this function to call finally handlers (we pass NULL as
184 * @exc object in this case).
187 mono_arch_get_call_filter (void)
193 throw_exception (MonoObject *exc, guint64 rethrow)
195 unw_context_t unw_ctx;
201 if (mono_object_isinst (exc, mono_defaults.exception_class)) {
202 MonoException *mono_ex = (MonoException*)exc;
204 mono_ex->stack_trace = NULL;
207 res = unw_getcontext (&unw_ctx);
209 res = unw_init_local (&ctx.cursor, &unw_ctx);
213 * Unwind until the first managed frame. This is needed since
214 * mono_handle_exception expects the variables in the original context to
215 * correspond to the method returned by mono_find_jit_info.
218 res = unw_get_reg (&ctx.cursor, UNW_IA64_IP, &ip);
221 ji = mono_jit_info_table_find (mono_domain_get (), (gpointer)ip);
226 res = unw_step (&ctx.cursor);
230 * This means an unhandled exception during the compilation of a
231 * topmost method like Main
238 mono_handle_exception (&ctx, exc, (gpointer)(ip), FALSE);
239 restore_context (&ctx);
241 g_assert_not_reached ();
245 get_throw_trampoline (gboolean rethrow)
248 Ia64CodegenState code;
249 gpointer ptr = throw_exception;
250 int i, in0, local0, out0;
252 unw_dyn_region_info_t *r_pro;
254 start = mono_global_codeman_reserve (256);
260 ia64_codegen_init (code, start);
261 ia64_alloc (code, local0 + 0, local0 - in0, out0 - local0, 3, 0);
262 ia64_mov_from_br (code, local0 + 1, IA64_B0);
264 /* FIXME: This depends on the current instruction emitter */
266 r_pro = g_malloc0 (_U_dyn_region_info_size (2));
268 r_pro->insn_count = 6;
270 _U_dyn_op_save_reg (&r_pro->op[i++], _U_QP_TRUE, /* when=*/ 2,
271 /* reg=*/ UNW_IA64_AR_PFS, /* dst=*/ UNW_IA64_GR + local0 + 0);
272 _U_dyn_op_save_reg (&r_pro->op[i++], _U_QP_TRUE, /* when=*/ 5,
273 /* reg=*/ UNW_IA64_RP, /* dst=*/ UNW_IA64_GR + local0 + 1);
274 g_assert ((unsigned) i <= r_pro->op_count);
277 ia64_mov (code, out0 + 0, in0 + 0);
278 ia64_adds_imm (code, out0 + 1, rethrow, IA64_R0);
280 /* Call throw_exception */
281 ia64_movl (code, GP_SCRATCH_REG, ptr);
282 ia64_ld8_inc_imm (code, GP_SCRATCH_REG2, GP_SCRATCH_REG, 8);
283 ia64_mov_to_br (code, IA64_B6, GP_SCRATCH_REG2);
284 ia64_ld8 (code, IA64_GP, GP_SCRATCH_REG);
285 ia64_br_call_reg (code, IA64_B0, IA64_B6);
288 ia64_break_i (code, 1000);
289 ia64_codegen_close (code);
291 g_assert ((code.buf - start) <= 256);
293 mono_arch_flush_icache (start, code.buf - start);
295 di = g_malloc0 (sizeof (unw_dyn_info_t));
296 di->start_ip = (unw_word_t) start;
297 di->end_ip = (unw_word_t) code.buf;
299 di->format = UNW_INFO_FORMAT_DYNAMIC;
300 di->u.pi.name_ptr = (unw_word_t)"throw_trampoline";
301 di->u.pi.regions = r_pro;
303 _U_dyn_register (di);
305 return mono_create_ftnptr (start);
309 * mono_arch_get_throw_exception:
311 * Returns a function pointer which can be used to raise
312 * exceptions. The returned function has the following
313 * signature: void (*func) (MonoException *exc);
317 mono_arch_get_throw_exception (void)
319 static guint8* start;
320 static gboolean inited = FALSE;
325 start = get_throw_trampoline (FALSE);
333 mono_arch_get_rethrow_exception (void)
335 static guint8* start;
336 static gboolean inited = FALSE;
341 start = get_throw_trampoline (TRUE);
349 mono_arch_get_throw_exception_by_name (void)
352 Ia64CodegenState code;
354 start = mono_global_codeman_reserve (64);
356 /* Not used on ia64 */
357 ia64_codegen_init (code, start);
358 ia64_break_i (code, 1001);
359 ia64_codegen_close (code);
361 g_assert ((code.buf - start) <= 256);
363 mono_arch_flush_icache (start, code.buf - start);
369 * mono_arch_get_throw_corlib_exception:
371 * Returns a function pointer which can be used to raise
372 * corlib exceptions. The returned function has the following
373 * signature: void (*func) (guint32 ex_token, guint32 offset);
374 * Here, offset is the offset which needs to be substracted from the caller IP
375 * to get the IP of the throw. Passing the offset has the advantage that it
376 * needs no relocations in the caller.
379 mono_arch_get_throw_corlib_exception (void)
381 static guint8* start;
382 static gboolean inited = FALSE;
384 int i, in0, local0, out0, nout;
385 Ia64CodegenState code;
387 unw_dyn_region_info_t *r_pro;
392 start = mono_global_codeman_reserve (1024);
399 ia64_codegen_init (code, start);
400 ia64_alloc (code, local0 + 0, local0 - in0, out0 - local0, nout, 0);
401 ia64_mov_from_br (code, local0 + 1, IA64_RP);
403 r_pro = g_malloc0 (_U_dyn_region_info_size (2));
405 r_pro->insn_count = 6;
407 _U_dyn_op_save_reg (&r_pro->op[i++], _U_QP_TRUE, /* when=*/ 2,
408 /* reg=*/ UNW_IA64_AR_PFS, /* dst=*/ UNW_IA64_GR + local0 + 0);
409 _U_dyn_op_save_reg (&r_pro->op[i++], _U_QP_TRUE, /* when=*/ 5,
410 /* reg=*/ UNW_IA64_RP, /* dst=*/ UNW_IA64_GR + local0 + 1);
411 g_assert ((unsigned) i <= r_pro->op_count);
413 /* Call exception_from_token */
414 ia64_movl (code, out0 + 0, mono_defaults.exception_class->image);
415 ia64_mov (code, out0 + 1, in0 + 0);
416 ptr = mono_exception_from_token;
417 ia64_movl (code, GP_SCRATCH_REG, ptr);
418 ia64_ld8_inc_imm (code, GP_SCRATCH_REG2, GP_SCRATCH_REG, 8);
419 ia64_mov_to_br (code, IA64_B6, GP_SCRATCH_REG2);
420 ia64_ld8 (code, IA64_GP, GP_SCRATCH_REG);
421 ia64_br_call_reg (code, IA64_B0, IA64_B6);
422 ia64_mov (code, local0 + 3, IA64_R8);
424 /* Compute throw ip */
425 ia64_mov (code, local0 + 2, local0 + 1);
426 ia64_sub (code, local0 + 2, local0 + 2, in0 + 1);
428 /* Trick the unwind library into using throw_ip as the IP in the caller frame */
429 ia64_mov (code, local0 + 1, local0 + 2);
432 ia64_mov (code, out0 + 0, local0 + 3);
433 ia64_mov (code, out0 + 1, IA64_R0);
435 /* Call throw_exception */
436 ptr = throw_exception;
437 ia64_movl (code, GP_SCRATCH_REG, ptr);
438 ia64_ld8_inc_imm (code, GP_SCRATCH_REG2, GP_SCRATCH_REG, 8);
439 ia64_mov_to_br (code, IA64_B6, GP_SCRATCH_REG2);
440 ia64_ld8 (code, IA64_GP, GP_SCRATCH_REG);
441 ia64_br_call_reg (code, IA64_B0, IA64_B6);
443 ia64_break_i (code, 1002);
444 ia64_codegen_close (code);
446 g_assert ((code.buf - start) <= 1024);
448 di = g_malloc0 (sizeof (unw_dyn_info_t));
449 di->start_ip = (unw_word_t) start;
450 di->end_ip = (unw_word_t) code.buf;
452 di->format = UNW_INFO_FORMAT_DYNAMIC;
453 di->u.pi.name_ptr = (unw_word_t)"throw_corlib_exception_trampoline";
454 di->u.pi.regions = r_pro;
456 _U_dyn_register (di);
458 mono_arch_flush_icache (start, code.buf - start);
460 return mono_create_ftnptr (start);
463 /* mono_arch_find_jit_info:
465 * This function is used to gather information from @ctx. It return the
466 * MonoJitInfo of the corresponding function, unwinds one stack frame and
467 * stores the resulting context into @new_ctx. It also stores a string
468 * describing the stack location into @trace (if not NULL), and modifies
469 * the @lmf if necessary. @native_offset return the IP offset from the
470 * start of the function or -1 if that info is not available.
473 mono_arch_find_jit_info (MonoDomain *domain, MonoJitTlsData *jit_tls, MonoJitInfo *res, MonoJitInfo *prev_ji, MonoContext *ctx,
474 MonoContext *new_ctx, char **trace, MonoLMF **lmf, int *native_offset,
484 err = unw_get_reg (&new_ctx->cursor, UNW_IA64_IP, &ip);
487 /* Avoid costly table lookup during stack overflow */
488 if (prev_ji && ((guint8*)ip > (guint8*)prev_ji->code_start && ((guint8*)ip < ((guint8*)prev_ji->code_start) + prev_ji->code_size)))
491 ji = mono_jit_info_table_find (domain, (gpointer)ip);
501 unw_get_proc_name (&new_ctx->cursor, name, 256, &off);
502 printf ("F: %s\n", name);
508 if (!ji->method->wrapper_type)
512 * Some managed methods like pinvoke wrappers might have save_lmf set.
513 * In this case, register save/restore code is not generated by the
514 * JIT, so we have to restore callee saved registers from the lmf.
516 if (ji->method->save_lmf) {
521 if (*lmf && (MONO_CONTEXT_GET_BP (new_ctx) >= (gpointer)(*lmf)->ebp)) {
522 /* remove any unused lmf */
523 *lmf = (*lmf)->previous_lmf;
529 /* This is an unmanaged frame, so just unwind through it */
530 err = unw_step (&new_ctx->cursor);
538 err = unw_step (&new_ctx->cursor);
544 return (gpointer)(gssize)-1;
548 * mono_arch_handle_exception:
550 * @ctx: saved processor state
551 * @obj: the exception object
554 mono_arch_handle_exception (void *sigctx, gpointer obj, gboolean test_only)
556 /* libunwind takes care of this */
557 unw_context_t unw_ctx;
563 res = unw_getcontext (&unw_ctx);
565 res = unw_init_local (&ctx.cursor, &unw_ctx);
569 * Unwind until the first managed frame. This skips the signal handler frames
573 res = unw_get_reg (&ctx.cursor, UNW_IA64_IP, &ip);
576 ji = mono_jit_info_table_find (mono_domain_get (), (gpointer)ip);
581 res = unw_step (&ctx.cursor);
585 mono_handle_exception (&ctx, obj, (gpointer)ip, test_only);
587 restore_context (&ctx);
589 g_assert_not_reached ();
593 mono_arch_ip_from_context (void *sigctx)