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 GP_SCRATCH_REG 31
41 #define GP_SCRATCH_REG2 30
43 G_GNUC_UNUSED static void
44 print_ctx (MonoContext *ctx)
47 unw_word_t off, ip, sp;
51 unw_get_proc_name (&ctx->cursor, name, 256, &off);
52 unw_get_proc_info(&ctx->cursor, &pi);
53 res = unw_get_reg (&ctx->cursor, UNW_IA64_IP, &ip);
55 res = unw_get_reg (&ctx->cursor, UNW_IA64_SP, &sp);
58 printf ("%s:%lx [%lx-%lx] SP: %lx\n", name, ip - pi.start_ip, pi.start_ip, pi.end_ip, sp);
62 ia64_create_ftnptr (gpointer ptr)
64 gpointer *desc = mono_global_codeman_reserve (2 * sizeof (gpointer));
72 restore_context (MonoContext *ctx)
77 res = unw_get_reg (&ctx->cursor, UNW_IA64_IP, &ip);
80 /* Set this to 0 to tell OP_START_HANDLER that it doesn't have to set the frame pointer */
81 res = unw_set_reg (&ctx->cursor, UNW_IA64_GR + 15, 0);
84 unw_resume (&ctx->cursor);
88 * mono_arch_get_restore_context:
90 * Returns a pointer to a method which restores a previously saved sigcontext.
93 mono_arch_get_restore_context (void)
95 return restore_context;
99 get_real_call_filter (void)
101 static gpointer filter;
102 static gboolean inited = FALSE;
104 Ia64CodegenState code;
105 int in0, local0, out0, nout;
107 unw_dyn_region_info_t *r_pro, *r_body, *r_epilog;
112 start = mono_global_codeman_reserve (1024);
114 /* int call_filter (guint64 fp, guint64 ip) */
117 * We have to create a register+stack frame similar to the frame which
118 * contains the filter.
120 * - setting up a register stack frame
121 * These cannot be set up in this function, because the fp register is a
122 * stacked register which is different in each method. Also, the register
123 * stack frame is different in each method. So we pass the FP value in a a
124 * non-stacked register and the code generated by the OP_START_HANDLER
125 * opcode will copy it to the appropriate register after setting up the
126 * register stack frame.
127 * The stacked registers are not need to be set since variables used in
128 * handler regions are never allocated to registers.
136 ia64_codegen_init (code, start);
138 ia64_codegen_set_one_ins_per_bundle (code, TRUE);
140 ia64_unw_save_reg (code, UNW_IA64_AR_PFS, UNW_IA64_GR + local0 + 0);
141 ia64_alloc (code, local0 + 0, local0 - in0, out0 - local0, nout, 0);
142 ia64_unw_save_reg (code, UNW_IA64_RP, UNW_IA64_GR + local0 + 1);
143 ia64_mov_from_br (code, local0 + 1, IA64_B0);
145 ia64_begin_bundle (code);
147 r_pro = mono_ia64_create_unwind_region (&code);
150 ia64_mov (code, IA64_R15, in0 + 0);
152 ia64_mov_to_br (code, IA64_B6, in0 + 1);
154 /* Call the filter */
155 ia64_br_call_reg (code, IA64_B0, IA64_B6);
157 /* R8 contains the result of the filter */
159 /* FIXME: Add unwind info for this */
161 ia64_begin_bundle (code);
163 r_body = mono_ia64_create_unwind_region (&code);
164 r_pro->next = r_body;
166 ia64_mov_to_ar_i (code, IA64_PFS, local0 + 0);
167 ia64_mov_ret_to_br (code, IA64_B0, local0 + 1);
168 ia64_br_ret_reg (code, IA64_B0);
170 ia64_begin_bundle (code);
172 r_epilog = mono_ia64_create_unwind_region (&code);
173 r_body->next = r_epilog;
175 ia64_codegen_set_one_ins_per_bundle (code, FALSE);
177 ia64_codegen_close (code);
179 g_assert ((code.buf - start) <= 256);
181 mono_arch_flush_icache (start, code.buf - start);
183 di = g_malloc0 (sizeof (unw_dyn_info_t));
184 di->start_ip = (unw_word_t) start;
185 di->end_ip = (unw_word_t) code.buf;
187 di->format = UNW_INFO_FORMAT_DYNAMIC;
188 di->u.pi.name_ptr = (unw_word_t)"throw_trampoline";
189 di->u.pi.regions = r_body;
191 _U_dyn_register (di);
193 filter = ia64_create_ftnptr (start);
201 call_filter (MonoContext *ctx, gpointer ip)
203 int (*filter) (MonoContext *, gpointer);
204 gpointer fp = MONO_CONTEXT_GET_BP (ctx);
206 filter = get_real_call_filter ();
208 return filter (fp, ip);
212 * mono_arch_get_call_filter:
214 * Returns a pointer to a method which calls an exception filter. We
215 * also use this function to call finally handlers (we pass NULL as
216 * @exc object in this case).
219 mono_arch_get_call_filter (void)
221 /* Initialize the real filter non-lazily */
222 get_real_call_filter ();
228 throw_exception (MonoObject *exc, guint64 rethrow)
230 unw_context_t unw_ctx;
236 if (mono_object_isinst (exc, mono_defaults.exception_class)) {
237 MonoException *mono_ex = (MonoException*)exc;
239 mono_ex->stack_trace = NULL;
242 res = unw_getcontext (&unw_ctx);
244 res = unw_init_local (&ctx.cursor, &unw_ctx);
248 * Unwind until the first managed frame. This is needed since
249 * mono_handle_exception expects the variables in the original context to
250 * correspond to the method returned by mono_find_jit_info.
253 res = unw_get_reg (&ctx.cursor, UNW_IA64_IP, &ip);
256 res = unw_get_reg (&ctx.cursor, UNW_IA64_SP, &sp);
259 ji = mono_jit_info_table_find (mono_domain_get (), (gpointer)ip);
261 //printf ("UN: %s %lx %lx\n", ji ? ji->method->name : "", ip, sp);
266 res = unw_step (&ctx.cursor);
270 * This means an unhandled exception during the compilation of a
271 * topmost method like Main
278 mono_handle_exception (&ctx, exc, (gpointer)(ip), FALSE);
279 restore_context (&ctx);
281 g_assert_not_reached ();
285 get_throw_trampoline (gboolean rethrow)
288 Ia64CodegenState code;
289 gpointer ptr = throw_exception;
290 int i, in0, local0, out0;
292 unw_dyn_region_info_t *r_pro;
294 start = mono_global_codeman_reserve (256);
300 ia64_codegen_init (code, start);
301 ia64_alloc (code, local0 + 0, local0 - in0, out0 - local0, 3, 0);
302 ia64_mov_from_br (code, local0 + 1, IA64_B0);
304 /* FIXME: This depends on the current instruction emitter */
306 r_pro = g_malloc0 (_U_dyn_region_info_size (2));
308 r_pro->insn_count = 6;
310 _U_dyn_op_save_reg (&r_pro->op[i++], _U_QP_TRUE, /* when=*/ 2,
311 /* reg=*/ UNW_IA64_AR_PFS, /* dst=*/ UNW_IA64_GR + local0 + 0);
312 _U_dyn_op_save_reg (&r_pro->op[i++], _U_QP_TRUE, /* when=*/ 5,
313 /* reg=*/ UNW_IA64_RP, /* dst=*/ UNW_IA64_GR + local0 + 1);
314 g_assert ((unsigned) i <= r_pro->op_count);
317 ia64_mov (code, out0 + 0, in0 + 0);
318 ia64_adds_imm (code, out0 + 1, rethrow, IA64_R0);
320 /* Call throw_exception */
321 ia64_movl (code, GP_SCRATCH_REG, ptr);
322 ia64_ld8_inc_imm (code, GP_SCRATCH_REG2, GP_SCRATCH_REG, 8);
323 ia64_mov_to_br (code, IA64_B6, GP_SCRATCH_REG2);
324 ia64_ld8 (code, IA64_GP, GP_SCRATCH_REG);
325 ia64_br_call_reg (code, IA64_B0, IA64_B6);
328 ia64_break_i (code, 1000);
329 ia64_codegen_close (code);
331 g_assert ((code.buf - start) <= 256);
333 mono_arch_flush_icache (start, code.buf - start);
335 di = g_malloc0 (sizeof (unw_dyn_info_t));
336 di->start_ip = (unw_word_t) start;
337 di->end_ip = (unw_word_t) code.buf;
339 di->format = UNW_INFO_FORMAT_DYNAMIC;
340 di->u.pi.name_ptr = (unw_word_t)"throw_trampoline";
341 di->u.pi.regions = r_pro;
343 _U_dyn_register (di);
345 return ia64_create_ftnptr (start);
349 * mono_arch_get_throw_exception:
351 * Returns a function pointer which can be used to raise
352 * exceptions. The returned function has the following
353 * signature: void (*func) (MonoException *exc);
357 mono_arch_get_throw_exception (void)
359 static guint8* start;
360 static gboolean inited = FALSE;
365 start = get_throw_trampoline (FALSE);
373 mono_arch_get_rethrow_exception (void)
375 static guint8* start;
376 static gboolean inited = FALSE;
381 start = get_throw_trampoline (TRUE);
389 mono_arch_get_throw_exception_by_name (void)
392 Ia64CodegenState code;
394 start = mono_global_codeman_reserve (64);
396 /* Not used on ia64 */
397 ia64_codegen_init (code, start);
398 ia64_break_i (code, 1001);
399 ia64_codegen_close (code);
401 g_assert ((code.buf - start) <= 256);
403 mono_arch_flush_icache (start, code.buf - start);
409 * mono_arch_get_throw_corlib_exception:
411 * Returns a function pointer which can be used to raise
412 * corlib exceptions. The returned function has the following
413 * signature: void (*func) (guint32 ex_token_index, guint32 offset);
414 * Here, offset is the offset which needs to be substracted from the caller IP
415 * to get the IP of the throw. Passing the offset has the advantage that it
416 * needs no relocations in the caller.
419 mono_arch_get_throw_corlib_exception (void)
422 static gboolean inited = FALSE;
425 int i, in0, local0, out0, nout;
426 Ia64CodegenState code;
428 unw_dyn_region_info_t *r_pro;
433 start = mono_global_codeman_reserve (1024);
440 ia64_codegen_init (code, start);
441 ia64_alloc (code, local0 + 0, local0 - in0, out0 - local0, nout, 0);
442 ia64_mov_from_br (code, local0 + 1, IA64_RP);
444 r_pro = g_malloc0 (_U_dyn_region_info_size (2));
446 r_pro->insn_count = 6;
448 _U_dyn_op_save_reg (&r_pro->op[i++], _U_QP_TRUE, /* when=*/ 2,
449 /* reg=*/ UNW_IA64_AR_PFS, /* dst=*/ UNW_IA64_GR + local0 + 0);
450 _U_dyn_op_save_reg (&r_pro->op[i++], _U_QP_TRUE, /* when=*/ 5,
451 /* reg=*/ UNW_IA64_RP, /* dst=*/ UNW_IA64_GR + local0 + 1);
452 g_assert ((unsigned) i <= r_pro->op_count);
454 /* Call exception_from_token */
455 ia64_movl (code, out0 + 0, mono_defaults.exception_class->image);
456 ia64_mov (code, out0 + 1, in0 + 0);
457 ia64_movl (code, GP_SCRATCH_REG, MONO_TOKEN_TYPE_DEF);
458 ia64_add (code, out0 + 1, in0 + 0, GP_SCRATCH_REG);
459 ptr = mono_exception_from_token;
460 ia64_movl (code, GP_SCRATCH_REG, ptr);
461 ia64_ld8_inc_imm (code, GP_SCRATCH_REG2, GP_SCRATCH_REG, 8);
462 ia64_mov_to_br (code, IA64_B6, GP_SCRATCH_REG2);
463 ia64_ld8 (code, IA64_GP, GP_SCRATCH_REG);
464 ia64_br_call_reg (code, IA64_B0, IA64_B6);
465 ia64_mov (code, local0 + 3, IA64_R8);
467 /* Compute throw ip */
468 ia64_mov (code, local0 + 2, local0 + 1);
469 ia64_sub (code, local0 + 2, local0 + 2, in0 + 1);
471 /* Trick the unwind library into using throw_ip as the IP in the caller frame */
472 ia64_mov (code, local0 + 1, local0 + 2);
475 ia64_mov (code, out0 + 0, local0 + 3);
476 ia64_mov (code, out0 + 1, IA64_R0);
478 /* Call throw_exception */
479 ptr = throw_exception;
480 ia64_movl (code, GP_SCRATCH_REG, ptr);
481 ia64_ld8_inc_imm (code, GP_SCRATCH_REG2, GP_SCRATCH_REG, 8);
482 ia64_mov_to_br (code, IA64_B6, GP_SCRATCH_REG2);
483 ia64_ld8 (code, IA64_GP, GP_SCRATCH_REG);
484 ia64_br_call_reg (code, IA64_B0, IA64_B6);
486 ia64_break_i (code, 1002);
487 ia64_codegen_close (code);
489 g_assert ((code.buf - start) <= 1024);
491 di = g_malloc0 (sizeof (unw_dyn_info_t));
492 di->start_ip = (unw_word_t) start;
493 di->end_ip = (unw_word_t) code.buf;
495 di->format = UNW_INFO_FORMAT_DYNAMIC;
496 di->u.pi.name_ptr = (unw_word_t)"throw_corlib_exception_trampoline";
497 di->u.pi.regions = r_pro;
499 _U_dyn_register (di);
501 mono_arch_flush_icache (start, code.buf - start);
503 res = ia64_create_ftnptr (start);
509 /* mono_arch_find_jit_info:
511 * This function is used to gather information from @ctx. It return the
512 * MonoJitInfo of the corresponding function, unwinds one stack frame and
513 * stores the resulting context into @new_ctx. It also stores a string
514 * describing the stack location into @trace (if not NULL), and modifies
515 * the @lmf if necessary. @native_offset return the IP offset from the
516 * start of the function or -1 if that info is not available.
519 mono_arch_find_jit_info (MonoDomain *domain, MonoJitTlsData *jit_tls, MonoJitInfo *res, MonoJitInfo *prev_ji, MonoContext *ctx,
520 MonoContext *new_ctx, char **trace, MonoLMF **lmf, int *native_offset,
530 err = unw_get_reg (&new_ctx->cursor, UNW_IA64_IP, &ip);
533 /* Avoid costly table lookup during stack overflow */
534 if (prev_ji && ((guint8*)ip > (guint8*)prev_ji->code_start && ((guint8*)ip < ((guint8*)prev_ji->code_start) + prev_ji->code_size)))
537 ji = mono_jit_info_table_find (domain, (gpointer)ip);
547 unw_get_proc_name (&new_ctx->cursor, name, 256, &off);
548 printf ("F: %s\n", name);
554 if (!ji->method->wrapper_type)
560 /* This is an unmanaged frame, so just unwind through it */
561 /* FIXME: This returns -3 for the __clone2 frame in libc */
562 err = unw_step (&new_ctx->cursor);
571 //print_ctx (new_ctx);
573 err = unw_step (&new_ctx->cursor);
576 //print_ctx (new_ctx);
581 return (gpointer)(gssize)-1;
585 * mono_arch_handle_exception:
587 * @ctx: saved processor state
588 * @obj: the exception object
591 mono_arch_handle_exception (void *sigctx, gpointer obj, gboolean test_only)
593 /* libunwind takes care of this */
594 unw_context_t unw_ctx;
600 res = unw_getcontext (&unw_ctx);
602 res = unw_init_local (&ctx.cursor, &unw_ctx);
606 * Unwind until the first managed frame. This skips the signal handler frames
610 res = unw_get_reg (&ctx.cursor, UNW_IA64_IP, &ip);
613 ji = mono_jit_info_table_find (mono_domain_get (), (gpointer)ip);
618 res = unw_step (&ctx.cursor);
622 mono_handle_exception (&ctx, obj, (gpointer)ip, test_only);
624 restore_context (&ctx);
626 g_assert_not_reached ();
630 mono_arch_ip_from_context (void *sigctx)
632 ucontext_t *ctx = (ucontext_t*)sigctx;
634 return (gpointer)ctx->uc_mcontext.sc_ip;