3 * exception support for IA64
6 * Zoltan Varga (vargaz@gmail.com)
8 * (C) 2001 Ximian, Inc.
12 * We implement exception handling with the help of the libuwind library:
14 * http://www.hpl.hp.com/research/linux/libunwind/
16 * Under IA64 all functions are assumed to have unwind info, we do not need to save
17 * the machine state in the LMF. But we have to generate unwind info for all
18 * dynamically generated code.
25 #include <sys/ucontext.h>
27 #include <mono/arch/ia64/ia64-codegen.h>
28 #include <mono/metadata/appdomain.h>
29 #include <mono/metadata/tabledefs.h>
30 #include <mono/metadata/threads.h>
31 #include <mono/metadata/debug-helpers.h>
32 #include <mono/metadata/exception.h>
33 #include <mono/metadata/gc-internals.h>
34 #include <mono/metadata/mono-debug.h>
37 #include "mini-ia64.h"
39 #define ALIGN_TO(val,align) (((val) + ((align) - 1)) & ~((align) - 1))
41 #define GP_SCRATCH_REG 31
42 #define GP_SCRATCH_REG2 30
44 G_GNUC_UNUSED static void
45 print_ctx (MonoContext *ctx)
48 unw_word_t off, ip, sp;
52 unw_get_proc_name (&ctx->cursor, name, 256, &off);
53 unw_get_proc_info(&ctx->cursor, &pi);
54 res = unw_get_reg (&ctx->cursor, UNW_IA64_IP, &ip);
56 res = unw_get_reg (&ctx->cursor, UNW_IA64_SP, &sp);
59 printf ("%s:%lx [%lx-%lx] SP: %lx\n", name, ip - pi.start_ip, pi.start_ip, pi.end_ip, sp);
63 ia64_create_ftnptr (gpointer ptr)
65 gpointer *desc = mono_global_codeman_reserve (2 * sizeof (gpointer));
73 restore_context (MonoContext *ctx)
78 res = unw_get_reg (&ctx->cursor, UNW_IA64_IP, &ip);
81 /* Set this to 0 to tell OP_START_HANDLER that it doesn't have to set the frame pointer */
82 res = unw_set_reg (&ctx->cursor, UNW_IA64_GR + 15, 0);
85 unw_resume (&ctx->cursor);
89 * mono_arch_get_restore_context:
91 * Returns a pointer to a method which restores a previously saved sigcontext.
94 mono_arch_get_restore_context (MonoTrampInfo **info, gboolean aot)
100 return restore_context;
104 get_real_call_filter (void)
106 static gpointer filter;
107 static gboolean inited = FALSE;
109 Ia64CodegenState code;
110 int in0, local0, out0, nout;
112 unw_dyn_region_info_t *r_pro, *r_body, *r_epilog;
117 start = mono_global_codeman_reserve (1024);
119 /* int call_filter (guint64 fp, guint64 ip) */
122 * We have to create a register+stack frame similar to the frame which
123 * contains the filter.
125 * - setting up a register stack frame
126 * These cannot be set up in this function, because the fp register is a
127 * stacked register which is different in each method. Also, the register
128 * stack frame is different in each method. So we pass the FP value in a a
129 * non-stacked register and the code generated by the OP_START_HANDLER
130 * opcode will copy it to the appropriate register after setting up the
131 * register stack frame.
132 * The stacked registers are not need to be set since variables used in
133 * handler regions are never allocated to registers.
141 ia64_codegen_init (code, start);
143 ia64_codegen_set_one_ins_per_bundle (code, TRUE);
145 ia64_unw_save_reg (code, UNW_IA64_AR_PFS, UNW_IA64_GR + local0 + 0);
146 ia64_alloc (code, local0 + 0, local0 - in0, out0 - local0, nout, 0);
147 ia64_unw_save_reg (code, UNW_IA64_RP, UNW_IA64_GR + local0 + 1);
148 ia64_mov_from_br (code, local0 + 1, IA64_B0);
150 ia64_begin_bundle (code);
152 r_pro = mono_ia64_create_unwind_region (&code);
155 ia64_mov (code, IA64_R15, in0 + 0);
157 ia64_mov_to_br (code, IA64_B6, in0 + 1);
159 /* Call the filter */
160 ia64_br_call_reg (code, IA64_B0, IA64_B6);
162 /* R8 contains the result of the filter */
164 /* FIXME: Add unwind info for this */
166 ia64_begin_bundle (code);
168 r_body = mono_ia64_create_unwind_region (&code);
169 r_pro->next = r_body;
171 ia64_mov_to_ar_i (code, IA64_PFS, local0 + 0);
172 ia64_mov_ret_to_br (code, IA64_B0, local0 + 1);
173 ia64_br_ret_reg (code, IA64_B0);
175 ia64_begin_bundle (code);
177 r_epilog = mono_ia64_create_unwind_region (&code);
178 r_body->next = r_epilog;
180 ia64_codegen_set_one_ins_per_bundle (code, FALSE);
182 ia64_codegen_close (code);
184 g_assert ((code.buf - start) <= 256);
186 mono_arch_flush_icache (start, code.buf - start);
188 di = g_malloc0 (sizeof (unw_dyn_info_t));
189 di->start_ip = (unw_word_t) start;
190 di->end_ip = (unw_word_t) code.buf;
192 di->format = UNW_INFO_FORMAT_DYNAMIC;
193 di->u.pi.name_ptr = (unw_word_t)"throw_trampoline";
194 di->u.pi.regions = r_body;
196 _U_dyn_register (di);
198 filter = ia64_create_ftnptr (start);
206 call_filter (MonoContext *ctx, gpointer ip)
208 int (*filter) (MonoContext *, gpointer);
209 gpointer fp = MONO_CONTEXT_GET_BP (ctx);
211 filter = get_real_call_filter ();
213 return filter (fp, ip);
217 * mono_arch_get_call_filter:
219 * Returns a pointer to a method which calls an exception filter. We
220 * also use this function to call finally handlers (we pass NULL as
221 * @exc object in this case).
224 mono_arch_get_call_filter (MonoTrampInfo **info, gboolean aot)
230 /* Initialize the real filter non-lazily */
231 get_real_call_filter ();
237 throw_exception (MonoObject *exc, guint64 rethrow)
239 unw_context_t unw_ctx;
246 if (mono_object_isinst_checked (exc, mono_defaults.exception_class, &error)) {
247 MonoException *mono_ex = (MonoException*)exc;
249 mono_ex->stack_trace = NULL;
250 mono_ex->trace_ips = NULL;
253 mono_error_assert_ok (&error);
255 res = unw_getcontext (&unw_ctx);
257 res = unw_init_local (&ctx.cursor, &unw_ctx);
261 * Unwind until the first managed frame. This is needed since
262 * mono_handle_exception expects the variables in the original context to
263 * correspond to the method returned by mono_find_jit_info.
266 res = unw_get_reg (&ctx.cursor, UNW_IA64_IP, &ip);
269 res = unw_get_reg (&ctx.cursor, UNW_IA64_SP, &sp);
272 ji = mini_jit_info_table_find (mono_domain_get (), (gpointer)ip, NULL);
274 //printf ("UN: %s %lx %lx\n", ji ? jinfo_get_method (ji)->name : "", ip, sp);
279 res = unw_step (&ctx.cursor);
283 * This means an unhandled exception during the compilation of a
284 * topmost method like Main
290 ctx.precise_ip = FALSE;
292 mono_handle_exception (&ctx, exc);
293 restore_context (&ctx);
295 g_assert_not_reached ();
299 get_throw_trampoline (gboolean rethrow)
302 Ia64CodegenState code;
303 gpointer ptr = throw_exception;
304 int i, in0, local0, out0;
306 unw_dyn_region_info_t *r_pro;
308 start = mono_global_codeman_reserve (256);
314 ia64_codegen_init (code, start);
315 ia64_alloc (code, local0 + 0, local0 - in0, out0 - local0, 3, 0);
316 ia64_mov_from_br (code, local0 + 1, IA64_B0);
318 /* FIXME: This depends on the current instruction emitter */
320 r_pro = g_malloc0 (_U_dyn_region_info_size (2));
322 r_pro->insn_count = 6;
324 _U_dyn_op_save_reg (&r_pro->op[i++], _U_QP_TRUE, /* when=*/ 2,
325 /* reg=*/ UNW_IA64_AR_PFS, /* dst=*/ UNW_IA64_GR + local0 + 0);
326 _U_dyn_op_save_reg (&r_pro->op[i++], _U_QP_TRUE, /* when=*/ 5,
327 /* reg=*/ UNW_IA64_RP, /* dst=*/ UNW_IA64_GR + local0 + 1);
328 g_assert ((unsigned) i <= r_pro->op_count);
331 ia64_mov (code, out0 + 0, in0 + 0);
332 ia64_adds_imm (code, out0 + 1, rethrow, IA64_R0);
334 /* Call throw_exception */
335 ia64_movl (code, GP_SCRATCH_REG, ptr);
336 ia64_ld8_inc_imm (code, GP_SCRATCH_REG2, GP_SCRATCH_REG, 8);
337 ia64_mov_to_br (code, IA64_B6, GP_SCRATCH_REG2);
338 ia64_ld8 (code, IA64_GP, GP_SCRATCH_REG);
339 ia64_br_call_reg (code, IA64_B0, IA64_B6);
342 ia64_break_i (code, 1000);
343 ia64_codegen_close (code);
345 g_assert ((code.buf - start) <= 256);
347 mono_arch_flush_icache (start, code.buf - start);
349 di = g_malloc0 (sizeof (unw_dyn_info_t));
350 di->start_ip = (unw_word_t) start;
351 di->end_ip = (unw_word_t) code.buf;
353 di->format = UNW_INFO_FORMAT_DYNAMIC;
354 di->u.pi.name_ptr = (unw_word_t)"throw_trampoline";
355 di->u.pi.regions = r_pro;
357 _U_dyn_register (di);
359 return ia64_create_ftnptr (start);
363 * mono_arch_get_throw_exception:
364 * \returns a function pointer which can be used to raise
365 * exceptions. The returned function has the following
366 * signature: void (*func) (MonoException *exc);
369 mono_arch_get_throw_exception (MonoTrampInfo **info, gboolean aot)
375 return get_throw_trampoline (FALSE);
379 mono_arch_get_rethrow_exception (MonoTrampInfo **info, gboolean aot)
385 return get_throw_trampoline (TRUE);
389 * mono_arch_get_throw_corlib_exception:
390 * \returns a function pointer which can be used to raise
391 * corlib exceptions. The returned function has the following
392 * signature: void (*func) (guint32 ex_token_index, guint32 offset);
393 * Here, offset is the offset which needs to be substracted from the caller IP
394 * to get the IP of the throw. Passing the offset has the advantage that it
395 * needs no relocations in the caller.
398 mono_arch_get_throw_corlib_exception (MonoTrampInfo **info, gboolean aot)
401 static gboolean inited = FALSE;
404 int i, in0, local0, out0, nout;
405 Ia64CodegenState code;
407 unw_dyn_region_info_t *r_pro;
416 start = mono_global_codeman_reserve (1024);
423 ia64_codegen_init (code, start);
424 ia64_alloc (code, local0 + 0, local0 - in0, out0 - local0, nout, 0);
425 ia64_mov_from_br (code, local0 + 1, IA64_RP);
427 r_pro = g_malloc0 (_U_dyn_region_info_size (2));
429 r_pro->insn_count = 6;
431 _U_dyn_op_save_reg (&r_pro->op[i++], _U_QP_TRUE, /* when=*/ 2,
432 /* reg=*/ UNW_IA64_AR_PFS, /* dst=*/ UNW_IA64_GR + local0 + 0);
433 _U_dyn_op_save_reg (&r_pro->op[i++], _U_QP_TRUE, /* when=*/ 5,
434 /* reg=*/ UNW_IA64_RP, /* dst=*/ UNW_IA64_GR + local0 + 1);
435 g_assert ((unsigned) i <= r_pro->op_count);
437 /* Call exception_from_token */
438 ia64_movl (code, out0 + 0, mono_defaults.exception_class->image);
439 ia64_mov (code, out0 + 1, in0 + 0);
440 ia64_movl (code, GP_SCRATCH_REG, MONO_TOKEN_TYPE_DEF);
441 ia64_add (code, out0 + 1, in0 + 0, GP_SCRATCH_REG);
442 ptr = mono_exception_from_token;
443 ia64_movl (code, GP_SCRATCH_REG, ptr);
444 ia64_ld8_inc_imm (code, GP_SCRATCH_REG2, GP_SCRATCH_REG, 8);
445 ia64_mov_to_br (code, IA64_B6, GP_SCRATCH_REG2);
446 ia64_ld8 (code, IA64_GP, GP_SCRATCH_REG);
447 ia64_br_call_reg (code, IA64_B0, IA64_B6);
448 ia64_mov (code, local0 + 3, IA64_R8);
450 /* Compute throw ip */
451 ia64_mov (code, local0 + 2, local0 + 1);
452 ia64_sub (code, local0 + 2, local0 + 2, in0 + 1);
454 /* Trick the unwind library into using throw_ip as the IP in the caller frame */
455 ia64_mov (code, local0 + 1, local0 + 2);
458 ia64_mov (code, out0 + 0, local0 + 3);
459 ia64_mov (code, out0 + 1, IA64_R0);
461 /* Call throw_exception */
462 ptr = throw_exception;
463 ia64_movl (code, GP_SCRATCH_REG, ptr);
464 ia64_ld8_inc_imm (code, GP_SCRATCH_REG2, GP_SCRATCH_REG, 8);
465 ia64_mov_to_br (code, IA64_B6, GP_SCRATCH_REG2);
466 ia64_ld8 (code, IA64_GP, GP_SCRATCH_REG);
467 ia64_br_call_reg (code, IA64_B0, IA64_B6);
469 ia64_break_i (code, 1002);
470 ia64_codegen_close (code);
472 g_assert ((code.buf - start) <= 1024);
474 di = g_malloc0 (sizeof (unw_dyn_info_t));
475 di->start_ip = (unw_word_t) start;
476 di->end_ip = (unw_word_t) code.buf;
478 di->format = UNW_INFO_FORMAT_DYNAMIC;
479 di->u.pi.name_ptr = (unw_word_t)"throw_corlib_exception_trampoline";
480 di->u.pi.regions = r_pro;
482 _U_dyn_register (di);
484 mono_arch_flush_icache (start, code.buf - start);
486 res = ia64_create_ftnptr (start);
493 * mono_arch_unwind_frame:
495 * This function is used to gather information from @ctx, and store it in @frame_info.
496 * It unwinds one stack frame, and stores the resulting context into @new_ctx. @lmf
497 * is modified if needed.
498 * Returns TRUE on success, FALSE otherwise.
501 mono_arch_unwind_frame (MonoDomain *domain, MonoJitTlsData *jit_tls,
502 MonoJitInfo *ji, MonoContext *ctx,
503 MonoContext *new_ctx, MonoLMF **lmf,
504 mgreg_t **save_locations,
505 StackFrameInfo *frame)
510 memset (frame, 0, sizeof (StackFrameInfo));
514 new_ctx->precise_ip = FALSE;
518 err = unw_get_reg (&new_ctx->cursor, UNW_IA64_IP, &ip);
521 ji = mini_jit_info_table_find (domain, (gpointer)ip, NULL);
528 unw_get_proc_name (&new_ctx->cursor, name, 256, &off);
529 printf ("F: %s\n", name);
536 /* This is an unmanaged frame, so just unwind through it */
537 /* FIXME: This returns -3 for the __clone2 frame in libc */
538 err = unw_step (&new_ctx->cursor);
548 if (ji->is_trampoline)
549 frame->type = FRAME_TYPE_TRAMPOLINE;
551 frame->type = FRAME_TYPE_MANAGED;
554 //print_ctx (new_ctx);
556 err = unw_step (&new_ctx->cursor);
559 //print_ctx (new_ctx);
568 * mono_arch_handle_exception:
569 * \param ctx saved processor state
570 * \param obj the exception object
573 mono_arch_handle_exception (void *sigctx, gpointer obj)
575 /* libunwind takes care of this */
576 unw_context_t unw_ctx;
582 res = unw_getcontext (&unw_ctx);
584 res = unw_init_local (&ctx.cursor, &unw_ctx);
588 * Unwind until the first managed frame. This skips the signal handler frames
592 res = unw_get_reg (&ctx.cursor, UNW_IA64_IP, &ip);
595 ji = mini_jit_info_table_find (mono_domain_get (), (gpointer)ip, NULL);
600 res = unw_step (&ctx.cursor);
603 ctx.precise_ip = TRUE;
605 mono_handle_exception (&ctx, obj);
607 restore_context (&ctx);
609 g_assert_not_reached ();
613 mono_arch_ip_from_context (void *sigctx)
615 ucontext_t *ctx = (ucontext_t*)sigctx;
617 return (gpointer)ctx->uc_mcontext.sc_ip;