Revert "update rx to the latest rx-oss-v1.1 build."
[mono-project.git] / mono / mini / exceptions-ia64.c
blob85c7792be30fae4b8258fc864f744cab998efead
1 /*
2 * exceptions-ia64.c: exception support for IA64
4 * Authors:
5 * Zoltan Varga (vargaz@gmail.com)
7 * (C) 2001 Ximian, Inc.
8 */
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.
20 #include <config.h>
21 #include <glib.h>
22 #include <signal.h>
23 #include <string.h>
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>
35 #include "mini.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)
46 char name[256];
47 unw_word_t off, ip, sp;
48 unw_proc_info_t pi;
49 int res;
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);
54 g_assert (res == 0);
55 res = unw_get_reg (&ctx->cursor, UNW_IA64_SP, &sp);
56 g_assert (res == 0);
58 printf ("%s:%lx [%lx-%lx] SP: %lx\n", name, ip - pi.start_ip, pi.start_ip, pi.end_ip, sp);
61 static gpointer
62 ia64_create_ftnptr (gpointer ptr)
64 gpointer *desc = mono_global_codeman_reserve (2 * sizeof (gpointer));
65 desc [0] = ptr;
66 desc [1] = NULL;
68 return desc;
71 static void
72 restore_context (MonoContext *ctx)
74 int res;
75 unw_word_t ip;
77 res = unw_get_reg (&ctx->cursor, UNW_IA64_IP, &ip);
78 g_assert (res == 0);
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);
82 g_assert (res == 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.
92 gpointer
93 mono_arch_get_restore_context (MonoTrampInfo **info, gboolean aot)
95 g_assert (!aot);
96 if (info)
97 *info = NULL;
99 return restore_context;
102 static gpointer
103 get_real_call_filter (void)
105 static gpointer filter;
106 static gboolean inited = FALSE;
107 guint8 *start;
108 Ia64CodegenState code;
109 int in0, local0, out0, nout;
110 unw_dyn_info_t *di;
111 unw_dyn_region_info_t *r_pro, *r_body, *r_epilog;
113 if (inited)
114 return filter;
116 start = mono_global_codeman_reserve (1024);
118 /* int call_filter (guint64 fp, guint64 ip) */
121 * We have to create a register+stack frame similar to the frame which
122 * contains the filter.
123 * - setting fp
124 * - setting up a register stack frame
125 * These cannot be set up in this function, because the fp register is a
126 * stacked register which is different in each method. Also, the register
127 * stack frame is different in each method. So we pass the FP value in a a
128 * non-stacked register and the code generated by the OP_START_HANDLER
129 * opcode will copy it to the appropriate register after setting up the
130 * register stack frame.
131 * The stacked registers are not need to be set since variables used in
132 * handler regions are never allocated to registers.
135 in0 = 32;
136 local0 = in0 + 2;
137 out0 = local0 + 4;
138 nout = 0;
140 ia64_codegen_init (code, start);
142 ia64_codegen_set_one_ins_per_bundle (code, TRUE);
144 ia64_unw_save_reg (code, UNW_IA64_AR_PFS, UNW_IA64_GR + local0 + 0);
145 ia64_alloc (code, local0 + 0, local0 - in0, out0 - local0, nout, 0);
146 ia64_unw_save_reg (code, UNW_IA64_RP, UNW_IA64_GR + local0 + 1);
147 ia64_mov_from_br (code, local0 + 1, IA64_B0);
149 ia64_begin_bundle (code);
151 r_pro = mono_ia64_create_unwind_region (&code);
153 /* Frame pointer */
154 ia64_mov (code, IA64_R15, in0 + 0);
155 /* Target ip */
156 ia64_mov_to_br (code, IA64_B6, in0 + 1);
158 /* Call the filter */
159 ia64_br_call_reg (code, IA64_B0, IA64_B6);
161 /* R8 contains the result of the filter */
163 /* FIXME: Add unwind info for this */
165 ia64_begin_bundle (code);
167 r_body = mono_ia64_create_unwind_region (&code);
168 r_pro->next = r_body;
170 ia64_mov_to_ar_i (code, IA64_PFS, local0 + 0);
171 ia64_mov_ret_to_br (code, IA64_B0, local0 + 1);
172 ia64_br_ret_reg (code, IA64_B0);
174 ia64_begin_bundle (code);
176 r_epilog = mono_ia64_create_unwind_region (&code);
177 r_body->next = r_epilog;
179 ia64_codegen_set_one_ins_per_bundle (code, FALSE);
181 ia64_codegen_close (code);
183 g_assert ((code.buf - start) <= 256);
185 mono_arch_flush_icache (start, code.buf - start);
187 di = g_malloc0 (sizeof (unw_dyn_info_t));
188 di->start_ip = (unw_word_t) start;
189 di->end_ip = (unw_word_t) code.buf;
190 di->gp = 0;
191 di->format = UNW_INFO_FORMAT_DYNAMIC;
192 di->u.pi.name_ptr = (unw_word_t)"throw_trampoline";
193 di->u.pi.regions = r_body;
195 _U_dyn_register (di);
197 filter = ia64_create_ftnptr (start);
199 inited = TRUE;
201 return filter;
204 static int
205 call_filter (MonoContext *ctx, gpointer ip)
207 int (*filter) (MonoContext *, gpointer);
208 gpointer fp = MONO_CONTEXT_GET_BP (ctx);
210 filter = get_real_call_filter ();
212 return filter (fp, ip);
216 * mono_arch_get_call_filter:
218 * Returns a pointer to a method which calls an exception filter. We
219 * also use this function to call finally handlers (we pass NULL as
220 * @exc object in this case).
222 gpointer
223 mono_arch_get_call_filter (MonoTrampInfo **info, gboolean aot)
225 g_assert (!aot);
226 if (info)
227 *info = NULL;
229 /* Initialize the real filter non-lazily */
230 get_real_call_filter ();
232 return call_filter;
235 static void
236 throw_exception (MonoObject *exc, guint64 rethrow)
238 unw_context_t unw_ctx;
239 MonoContext ctx;
240 MonoJitInfo *ji;
241 unw_word_t ip, sp;
242 int res;
244 if (mono_object_isinst (exc, mono_defaults.exception_class)) {
245 MonoException *mono_ex = (MonoException*)exc;
246 if (!rethrow)
247 mono_ex->stack_trace = NULL;
250 res = unw_getcontext (&unw_ctx);
251 g_assert (res == 0);
252 res = unw_init_local (&ctx.cursor, &unw_ctx);
253 g_assert (res == 0);
256 * Unwind until the first managed frame. This is needed since
257 * mono_handle_exception expects the variables in the original context to
258 * correspond to the method returned by mono_find_jit_info.
260 while (TRUE) {
261 res = unw_get_reg (&ctx.cursor, UNW_IA64_IP, &ip);
262 g_assert (res == 0);
264 res = unw_get_reg (&ctx.cursor, UNW_IA64_SP, &sp);
265 g_assert (res == 0);
267 ji = mini_jit_info_table_find (mono_domain_get (), (gpointer)ip, NULL);
269 //printf ("UN: %s %lx %lx\n", ji ? ji->method->name : "", ip, sp);
271 if (ji)
272 break;
274 res = unw_step (&ctx.cursor);
276 if (res == 0) {
278 * This means an unhandled exception during the compilation of a
279 * topmost method like Main
281 break;
283 g_assert (res >= 0);
285 ctx.precise_ip = FALSE;
287 mono_handle_exception (&ctx, exc);
288 restore_context (&ctx);
290 g_assert_not_reached ();
293 static gpointer
294 get_throw_trampoline (gboolean rethrow)
296 guint8* start;
297 Ia64CodegenState code;
298 gpointer ptr = throw_exception;
299 int i, in0, local0, out0;
300 unw_dyn_info_t *di;
301 unw_dyn_region_info_t *r_pro;
303 start = mono_global_codeman_reserve (256);
305 in0 = 32;
306 local0 = in0 + 1;
307 out0 = local0 + 2;
309 ia64_codegen_init (code, start);
310 ia64_alloc (code, local0 + 0, local0 - in0, out0 - local0, 3, 0);
311 ia64_mov_from_br (code, local0 + 1, IA64_B0);
313 /* FIXME: This depends on the current instruction emitter */
315 r_pro = g_malloc0 (_U_dyn_region_info_size (2));
316 r_pro->op_count = 2;
317 r_pro->insn_count = 6;
318 i = 0;
319 _U_dyn_op_save_reg (&r_pro->op[i++], _U_QP_TRUE, /* when=*/ 2,
320 /* reg=*/ UNW_IA64_AR_PFS, /* dst=*/ UNW_IA64_GR + local0 + 0);
321 _U_dyn_op_save_reg (&r_pro->op[i++], _U_QP_TRUE, /* when=*/ 5,
322 /* reg=*/ UNW_IA64_RP, /* dst=*/ UNW_IA64_GR + local0 + 1);
323 g_assert ((unsigned) i <= r_pro->op_count);
325 /* Set args */
326 ia64_mov (code, out0 + 0, in0 + 0);
327 ia64_adds_imm (code, out0 + 1, rethrow, IA64_R0);
329 /* Call throw_exception */
330 ia64_movl (code, GP_SCRATCH_REG, ptr);
331 ia64_ld8_inc_imm (code, GP_SCRATCH_REG2, GP_SCRATCH_REG, 8);
332 ia64_mov_to_br (code, IA64_B6, GP_SCRATCH_REG2);
333 ia64_ld8 (code, IA64_GP, GP_SCRATCH_REG);
334 ia64_br_call_reg (code, IA64_B0, IA64_B6);
336 /* Not reached */
337 ia64_break_i (code, 1000);
338 ia64_codegen_close (code);
340 g_assert ((code.buf - start) <= 256);
342 mono_arch_flush_icache (start, code.buf - start);
344 di = g_malloc0 (sizeof (unw_dyn_info_t));
345 di->start_ip = (unw_word_t) start;
346 di->end_ip = (unw_word_t) code.buf;
347 di->gp = 0;
348 di->format = UNW_INFO_FORMAT_DYNAMIC;
349 di->u.pi.name_ptr = (unw_word_t)"throw_trampoline";
350 di->u.pi.regions = r_pro;
352 _U_dyn_register (di);
354 return ia64_create_ftnptr (start);
358 * mono_arch_get_throw_exception:
360 * Returns a function pointer which can be used to raise
361 * exceptions. The returned function has the following
362 * signature: void (*func) (MonoException *exc);
365 gpointer
366 mono_arch_get_throw_exception (MonoTrampInfo **info, gboolean aot)
368 g_assert (!aot);
369 if (info)
370 *info = NULL;
372 return get_throw_trampoline (FALSE);
375 gpointer
376 mono_arch_get_rethrow_exception (MonoTrampInfo **info, gboolean aot)
378 g_assert (!aot);
379 if (info)
380 *info = NULL;
382 return get_throw_trampoline (TRUE);
386 * mono_arch_get_throw_corlib_exception:
388 * Returns a function pointer which can be used to raise
389 * corlib exceptions. The returned function has the following
390 * signature: void (*func) (guint32 ex_token_index, guint32 offset);
391 * Here, offset is the offset which needs to be substracted from the caller IP
392 * to get the IP of the throw. Passing the offset has the advantage that it
393 * needs no relocations in the caller.
395 gpointer
396 mono_arch_get_throw_corlib_exception (MonoTrampInfo **info, gboolean aot)
398 static guint8* res;
399 static gboolean inited = FALSE;
400 guint8 *start;
401 gpointer ptr;
402 int i, in0, local0, out0, nout;
403 Ia64CodegenState code;
404 unw_dyn_info_t *di;
405 unw_dyn_region_info_t *r_pro;
407 g_assert (!aot);
408 if (info)
409 *info = NULL;
411 if (inited)
412 return res;
414 start = mono_global_codeman_reserve (1024);
416 in0 = 32;
417 local0 = in0 + 2;
418 out0 = local0 + 4;
419 nout = 3;
421 ia64_codegen_init (code, start);
422 ia64_alloc (code, local0 + 0, local0 - in0, out0 - local0, nout, 0);
423 ia64_mov_from_br (code, local0 + 1, IA64_RP);
425 r_pro = g_malloc0 (_U_dyn_region_info_size (2));
426 r_pro->op_count = 2;
427 r_pro->insn_count = 6;
428 i = 0;
429 _U_dyn_op_save_reg (&r_pro->op[i++], _U_QP_TRUE, /* when=*/ 2,
430 /* reg=*/ UNW_IA64_AR_PFS, /* dst=*/ UNW_IA64_GR + local0 + 0);
431 _U_dyn_op_save_reg (&r_pro->op[i++], _U_QP_TRUE, /* when=*/ 5,
432 /* reg=*/ UNW_IA64_RP, /* dst=*/ UNW_IA64_GR + local0 + 1);
433 g_assert ((unsigned) i <= r_pro->op_count);
435 /* Call exception_from_token */
436 ia64_movl (code, out0 + 0, mono_defaults.exception_class->image);
437 ia64_mov (code, out0 + 1, in0 + 0);
438 ia64_movl (code, GP_SCRATCH_REG, MONO_TOKEN_TYPE_DEF);
439 ia64_add (code, out0 + 1, in0 + 0, GP_SCRATCH_REG);
440 ptr = mono_exception_from_token;
441 ia64_movl (code, GP_SCRATCH_REG, ptr);
442 ia64_ld8_inc_imm (code, GP_SCRATCH_REG2, GP_SCRATCH_REG, 8);
443 ia64_mov_to_br (code, IA64_B6, GP_SCRATCH_REG2);
444 ia64_ld8 (code, IA64_GP, GP_SCRATCH_REG);
445 ia64_br_call_reg (code, IA64_B0, IA64_B6);
446 ia64_mov (code, local0 + 3, IA64_R8);
448 /* Compute throw ip */
449 ia64_mov (code, local0 + 2, local0 + 1);
450 ia64_sub (code, local0 + 2, local0 + 2, in0 + 1);
452 /* Trick the unwind library into using throw_ip as the IP in the caller frame */
453 ia64_mov (code, local0 + 1, local0 + 2);
455 /* Set args */
456 ia64_mov (code, out0 + 0, local0 + 3);
457 ia64_mov (code, out0 + 1, IA64_R0);
459 /* Call throw_exception */
460 ptr = throw_exception;
461 ia64_movl (code, GP_SCRATCH_REG, ptr);
462 ia64_ld8_inc_imm (code, GP_SCRATCH_REG2, GP_SCRATCH_REG, 8);
463 ia64_mov_to_br (code, IA64_B6, GP_SCRATCH_REG2);
464 ia64_ld8 (code, IA64_GP, GP_SCRATCH_REG);
465 ia64_br_call_reg (code, IA64_B0, IA64_B6);
467 ia64_break_i (code, 1002);
468 ia64_codegen_close (code);
470 g_assert ((code.buf - start) <= 1024);
472 di = g_malloc0 (sizeof (unw_dyn_info_t));
473 di->start_ip = (unw_word_t) start;
474 di->end_ip = (unw_word_t) code.buf;
475 di->gp = 0;
476 di->format = UNW_INFO_FORMAT_DYNAMIC;
477 di->u.pi.name_ptr = (unw_word_t)"throw_corlib_exception_trampoline";
478 di->u.pi.regions = r_pro;
480 _U_dyn_register (di);
482 mono_arch_flush_icache (start, code.buf - start);
484 res = ia64_create_ftnptr (start);
485 inited = TRUE;
487 return res;
491 * mono_arch_find_jit_info:
493 * This function is used to gather information from @ctx, and store it in @frame_info.
494 * It unwinds one stack frame, and stores the resulting context into @new_ctx. @lmf
495 * is modified if needed.
496 * Returns TRUE on success, FALSE otherwise.
498 gboolean
499 mono_arch_find_jit_info (MonoDomain *domain, MonoJitTlsData *jit_tls,
500 MonoJitInfo *ji, MonoContext *ctx,
501 MonoContext *new_ctx, MonoLMF **lmf,
502 mgreg_t **save_locations,
503 StackFrameInfo *frame)
505 int err;
506 unw_word_t ip;
508 memset (frame, 0, sizeof (StackFrameInfo));
509 frame->ji = ji;
511 *new_ctx = *ctx;
512 new_ctx->precise_ip = FALSE;
514 if (!ji) {
515 while (TRUE) {
516 err = unw_get_reg (&new_ctx->cursor, UNW_IA64_IP, &ip);
517 g_assert (err == 0);
519 ji = mini_jit_info_table_find (domain, (gpointer)ip, NULL);
523 char name[256];
524 unw_word_t off;
526 unw_get_proc_name (&new_ctx->cursor, name, 256, &off);
527 printf ("F: %s\n", name);
531 if (ji)
532 break;
534 /* This is an unmanaged frame, so just unwind through it */
535 /* FIXME: This returns -3 for the __clone2 frame in libc */
536 err = unw_step (&new_ctx->cursor);
537 if (err < 0)
538 break;
540 if (err == 0)
541 break;
545 if (ji) {
546 frame->type = FRAME_TYPE_MANAGED;
547 frame->ji = ji;
549 //print_ctx (new_ctx);
551 err = unw_step (&new_ctx->cursor);
552 g_assert (err >= 0);
554 //print_ctx (new_ctx);
556 return TRUE;
558 else
559 return FALSE;
563 * mono_arch_handle_exception:
565 * @ctx: saved processor state
566 * @obj: the exception object
568 gboolean
569 mono_arch_handle_exception (void *sigctx, gpointer obj)
571 /* libunwind takes care of this */
572 unw_context_t unw_ctx;
573 MonoContext ctx;
574 MonoJitInfo *ji;
575 unw_word_t ip;
576 int res;
578 res = unw_getcontext (&unw_ctx);
579 g_assert (res == 0);
580 res = unw_init_local (&ctx.cursor, &unw_ctx);
581 g_assert (res == 0);
584 * Unwind until the first managed frame. This skips the signal handler frames
585 * too.
587 while (TRUE) {
588 res = unw_get_reg (&ctx.cursor, UNW_IA64_IP, &ip);
589 g_assert (res == 0);
591 ji = mini_jit_info_table_find (mono_domain_get (), (gpointer)ip, NULL);
593 if (ji)
594 break;
596 res = unw_step (&ctx.cursor);
597 g_assert (res >= 0);
599 ctx.precise_ip = TRUE;
601 mono_handle_exception (&ctx, obj);
603 restore_context (&ctx);
605 g_assert_not_reached ();
608 gpointer
609 mono_arch_ip_from_context (void *sigctx)
611 ucontext_t *ctx = (ucontext_t*)sigctx;
613 return (gpointer)ctx->uc_mcontext.sc_ip;