2009-10-24 Zoltan Varga <vargaz@gmail.com>
[mono.git] / mono / mini / exceptions-ia64.c
1 /*
2  * exceptions-ia64.c: exception support for IA64
3  *
4  * Authors:
5  *   Zoltan Varga (vargaz@gmail.com)
6  *
7  * (C) 2001 Ximian, Inc.
8  */
9
10 /*
11  * We implement exception handling with the help of the libuwind library:
12  * 
13  * http://www.hpl.hp.com/research/linux/libunwind/
14  *
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.
18  */
19
20 #include <config.h>
21 #include <glib.h>
22 #include <signal.h>
23 #include <string.h>
24 #include <sys/ucontext.h>
25
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>
34
35 #include "mini.h"
36 #include "mini-ia64.h"
37
38 #define ALIGN_TO(val,align) (((val) + ((align) - 1)) & ~((align) - 1))
39
40 #define GP_SCRATCH_REG 31
41 #define GP_SCRATCH_REG2 30
42
43 G_GNUC_UNUSED static void
44 print_ctx (MonoContext *ctx)
45 {
46         char name[256];
47         unw_word_t off, ip, sp;
48         unw_proc_info_t pi;
49         int res;
50
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);
57
58         printf ("%s:%lx [%lx-%lx] SP: %lx\n", name, ip - pi.start_ip, pi.start_ip, pi.end_ip, sp);
59 }
60
61 static gpointer
62 ia64_create_ftnptr (gpointer ptr)
63 {
64         gpointer *desc = mono_global_codeman_reserve (2 * sizeof (gpointer));
65         desc [0] = ptr;
66         desc [1] = NULL;
67
68         return desc;
69 }
70
71 static void
72 restore_context (MonoContext *ctx)
73 {
74         int res;
75         unw_word_t ip;
76
77         res = unw_get_reg (&ctx->cursor, UNW_IA64_IP, &ip);
78         g_assert (res == 0);
79
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);
83
84         unw_resume (&ctx->cursor);
85 }
86
87 /*
88  * mono_arch_get_restore_context:
89  *
90  * Returns a pointer to a method which restores a previously saved sigcontext.
91  */
92 gpointer
93 mono_arch_get_restore_context (void)
94 {
95         return restore_context;
96 }
97
98 static gpointer
99 get_real_call_filter (void)
100 {
101         static gpointer filter;
102         static gboolean inited = FALSE;
103         guint8 *start;
104         Ia64CodegenState code;
105         int in0, local0, out0, nout;
106         unw_dyn_info_t *di;
107         unw_dyn_region_info_t *r_pro, *r_body, *r_epilog;
108
109         if (inited)
110                 return filter;
111
112         start = mono_global_codeman_reserve (1024);
113
114         /* int call_filter (guint64 fp, guint64 ip) */
115
116         /*
117          * We have to create a register+stack frame similar to the frame which 
118          * contains the filter. 
119          * - setting fp
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.
129          */
130
131         in0 = 32;
132         local0 = in0 + 2;
133         out0 = local0 + 4;
134         nout = 0;
135
136         ia64_codegen_init (code, start);
137
138         ia64_codegen_set_one_ins_per_bundle (code, TRUE);
139
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);
144
145         ia64_begin_bundle (code);
146
147         r_pro = mono_ia64_create_unwind_region (&code);
148
149         /* Frame pointer */
150         ia64_mov (code, IA64_R15, in0 + 0);
151         /* Target ip */
152         ia64_mov_to_br (code, IA64_B6, in0 + 1);
153
154         /* Call the filter */
155         ia64_br_call_reg (code, IA64_B0, IA64_B6);
156
157         /* R8 contains the result of the filter */
158
159         /* FIXME: Add unwind info for this */
160
161         ia64_begin_bundle (code);
162
163         r_body = mono_ia64_create_unwind_region (&code);
164         r_pro->next = r_body;
165
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);
169
170         ia64_begin_bundle (code);
171
172         r_epilog = mono_ia64_create_unwind_region (&code);
173         r_body->next = r_epilog;
174
175         ia64_codegen_set_one_ins_per_bundle (code, FALSE);
176
177         ia64_codegen_close (code);
178
179         g_assert ((code.buf - start) <= 256);
180
181         mono_arch_flush_icache (start, code.buf - start);
182
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;
186         di->gp = 0;
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;
190
191         _U_dyn_register (di);
192
193     filter = ia64_create_ftnptr (start);
194
195         inited = TRUE;
196
197         return filter;
198 }
199
200 static int
201 call_filter (MonoContext *ctx, gpointer ip)
202 {
203         int (*filter) (MonoContext *, gpointer);
204         gpointer fp = MONO_CONTEXT_GET_BP (ctx);
205
206         filter = get_real_call_filter ();
207
208         return filter (fp, ip);
209 }
210
211 /*
212  * mono_arch_get_call_filter:
213  *
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).
217  */
218 gpointer
219 mono_arch_get_call_filter (void)
220 {
221         /* Initialize the real filter non-lazily */
222         get_real_call_filter ();
223
224         return call_filter;
225 }
226
227 static void
228 throw_exception (MonoObject *exc, guint64 rethrow)
229 {
230         unw_context_t unw_ctx;
231         MonoContext ctx;
232         MonoJitInfo *ji;
233         unw_word_t ip, sp;
234         int res;
235
236         if (mono_object_isinst (exc, mono_defaults.exception_class)) {
237                 MonoException *mono_ex = (MonoException*)exc;
238                 if (!rethrow)
239                         mono_ex->stack_trace = NULL;
240         }
241
242         res = unw_getcontext (&unw_ctx);
243         g_assert (res == 0);
244         res = unw_init_local (&ctx.cursor, &unw_ctx);
245         g_assert (res == 0);
246
247         /* 
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.
251          */
252         while (TRUE) {
253                 res = unw_get_reg (&ctx.cursor, UNW_IA64_IP, &ip);
254                 g_assert (res == 0);
255
256                 res = unw_get_reg (&ctx.cursor, UNW_IA64_SP, &sp);
257                 g_assert (res == 0);
258
259                 ji = mini_jit_info_table_find (mono_domain_get (), (gpointer)ip, NULL);
260
261                 //printf ("UN: %s %lx %lx\n", ji ? ji->method->name : "", ip, sp);
262
263                 if (ji)
264                         break;
265
266                 res = unw_step (&ctx.cursor);
267
268                 if (res == 0) {
269                         /*
270                          * This means an unhandled exception during the compilation of a
271                          * topmost method like Main
272                          */
273                         break;
274                 }
275                 g_assert (res >= 0);
276         }
277         ctx.precise_ip = FALSE;
278
279         mono_handle_exception (&ctx, exc, (gpointer)(ip), FALSE);
280         restore_context (&ctx);
281
282         g_assert_not_reached ();
283 }
284
285 static gpointer
286 get_throw_trampoline (gboolean rethrow)
287 {
288         guint8* start;
289         Ia64CodegenState code;
290         gpointer ptr = throw_exception;
291         int i, in0, local0, out0;
292         unw_dyn_info_t *di;
293         unw_dyn_region_info_t *r_pro;
294
295         start = mono_global_codeman_reserve (256);
296
297         in0 = 32;
298         local0 = in0 + 1;
299         out0 = local0 + 2;
300
301         ia64_codegen_init (code, start);
302         ia64_alloc (code, local0 + 0, local0 - in0, out0 - local0, 3, 0);
303         ia64_mov_from_br (code, local0 + 1, IA64_B0);   
304
305         /* FIXME: This depends on the current instruction emitter */
306
307         r_pro = g_malloc0 (_U_dyn_region_info_size (2));
308         r_pro->op_count = 2;
309         r_pro->insn_count = 6;
310         i = 0;
311         _U_dyn_op_save_reg (&r_pro->op[i++], _U_QP_TRUE, /* when=*/ 2,
312                                                 /* reg=*/ UNW_IA64_AR_PFS, /* dst=*/ UNW_IA64_GR + local0 + 0);
313         _U_dyn_op_save_reg (&r_pro->op[i++], _U_QP_TRUE, /* when=*/ 5,
314                                                 /* reg=*/ UNW_IA64_RP, /* dst=*/ UNW_IA64_GR + local0 + 1);
315         g_assert ((unsigned) i <= r_pro->op_count);     
316
317         /* Set args */
318         ia64_mov (code, out0 + 0, in0 + 0);
319         ia64_adds_imm (code, out0 + 1, rethrow, IA64_R0);
320
321         /* Call throw_exception */
322         ia64_movl (code, GP_SCRATCH_REG, ptr);
323         ia64_ld8_inc_imm (code, GP_SCRATCH_REG2, GP_SCRATCH_REG, 8);
324         ia64_mov_to_br (code, IA64_B6, GP_SCRATCH_REG2);
325         ia64_ld8 (code, IA64_GP, GP_SCRATCH_REG);
326         ia64_br_call_reg (code, IA64_B0, IA64_B6);
327
328         /* Not reached */
329         ia64_break_i (code, 1000);
330         ia64_codegen_close (code);
331
332         g_assert ((code.buf - start) <= 256);
333
334         mono_arch_flush_icache (start, code.buf - start);
335
336         di = g_malloc0 (sizeof (unw_dyn_info_t));
337         di->start_ip = (unw_word_t) start;
338         di->end_ip = (unw_word_t) code.buf;
339         di->gp = 0;
340         di->format = UNW_INFO_FORMAT_DYNAMIC;
341         di->u.pi.name_ptr = (unw_word_t)"throw_trampoline";
342         di->u.pi.regions = r_pro;
343
344         _U_dyn_register (di);
345
346         return ia64_create_ftnptr (start);
347 }
348
349 /**
350  * mono_arch_get_throw_exception:
351  *
352  * Returns a function pointer which can be used to raise 
353  * exceptions. The returned function has the following 
354  * signature: void (*func) (MonoException *exc); 
355  *
356  */
357 gpointer 
358 mono_arch_get_throw_exception (void)
359 {
360         static guint8* start;
361         static gboolean inited = FALSE;
362
363         if (inited)
364                 return start;
365
366         start = get_throw_trampoline (FALSE);
367
368         inited = TRUE;
369
370         return start;
371 }
372
373 gpointer 
374 mono_arch_get_rethrow_exception (void)
375 {
376         static guint8* start;
377         static gboolean inited = FALSE;
378
379         if (inited)
380                 return start;
381
382         start = get_throw_trampoline (TRUE);
383
384         inited = TRUE;
385
386         return start;
387 }
388
389 gpointer 
390 mono_arch_get_throw_exception_by_name (void)
391 {       
392         guint8* start;
393         Ia64CodegenState code;
394
395         start = mono_global_codeman_reserve (64);
396
397         /* Not used on ia64 */
398         ia64_codegen_init (code, start);
399         ia64_break_i (code, 1001);
400         ia64_codegen_close (code);
401
402         g_assert ((code.buf - start) <= 256);
403
404         mono_arch_flush_icache (start, code.buf - start);
405
406         return start;
407 }
408
409 /**
410  * mono_arch_get_throw_corlib_exception:
411  *
412  * Returns a function pointer which can be used to raise 
413  * corlib exceptions. The returned function has the following 
414  * signature: void (*func) (guint32 ex_token_index, guint32 offset); 
415  * Here, offset is the offset which needs to be substracted from the caller IP 
416  * to get the IP of the throw. Passing the offset has the advantage that it 
417  * needs no relocations in the caller.
418  */
419 gpointer 
420 mono_arch_get_throw_corlib_exception (void)
421 {
422         static guint8* res;
423         static gboolean inited = FALSE;
424         guint8 *start;
425         gpointer ptr;
426         int i, in0, local0, out0, nout;
427         Ia64CodegenState code;
428         unw_dyn_info_t *di;
429         unw_dyn_region_info_t *r_pro;
430
431         if (inited)
432                 return res;
433
434         start = mono_global_codeman_reserve (1024);
435
436         in0 = 32;
437         local0 = in0 + 2;
438         out0 = local0 + 4;
439         nout = 3;
440
441         ia64_codegen_init (code, start);
442         ia64_alloc (code, local0 + 0, local0 - in0, out0 - local0, nout, 0);
443         ia64_mov_from_br (code, local0 + 1, IA64_RP);
444
445         r_pro = g_malloc0 (_U_dyn_region_info_size (2));
446         r_pro->op_count = 2;
447         r_pro->insn_count = 6;
448         i = 0;
449         _U_dyn_op_save_reg (&r_pro->op[i++], _U_QP_TRUE, /* when=*/ 2,
450                                                 /* reg=*/ UNW_IA64_AR_PFS, /* dst=*/ UNW_IA64_GR + local0 + 0);
451         _U_dyn_op_save_reg (&r_pro->op[i++], _U_QP_TRUE, /* when=*/ 5,
452                                                 /* reg=*/ UNW_IA64_RP, /* dst=*/ UNW_IA64_GR + local0 + 1);
453         g_assert ((unsigned) i <= r_pro->op_count);     
454
455         /* Call exception_from_token */
456         ia64_movl (code, out0 + 0, mono_defaults.exception_class->image);
457         ia64_mov (code, out0 + 1, in0 + 0);
458         ia64_movl (code, GP_SCRATCH_REG, MONO_TOKEN_TYPE_DEF);
459         ia64_add (code, out0 + 1, in0 + 0, GP_SCRATCH_REG);
460         ptr = mono_exception_from_token;
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);
466         ia64_mov (code, local0 + 3, IA64_R8);
467
468         /* Compute throw ip */
469         ia64_mov (code, local0 + 2, local0 + 1);
470         ia64_sub (code, local0 + 2, local0 + 2, in0 + 1);
471
472         /* Trick the unwind library into using throw_ip as the IP in the caller frame */
473         ia64_mov (code, local0 + 1, local0 + 2);
474
475         /* Set args */
476         ia64_mov (code, out0 + 0, local0 + 3);
477         ia64_mov (code, out0 + 1, IA64_R0);
478
479         /* Call throw_exception */
480         ptr = throw_exception;
481         ia64_movl (code, GP_SCRATCH_REG, ptr);
482         ia64_ld8_inc_imm (code, GP_SCRATCH_REG2, GP_SCRATCH_REG, 8);
483         ia64_mov_to_br (code, IA64_B6, GP_SCRATCH_REG2);
484         ia64_ld8 (code, IA64_GP, GP_SCRATCH_REG);
485         ia64_br_call_reg (code, IA64_B0, IA64_B6);
486
487         ia64_break_i (code, 1002);
488         ia64_codegen_close (code);
489
490         g_assert ((code.buf - start) <= 1024);
491
492         di = g_malloc0 (sizeof (unw_dyn_info_t));
493         di->start_ip = (unw_word_t) start;
494         di->end_ip = (unw_word_t) code.buf;
495         di->gp = 0;
496         di->format = UNW_INFO_FORMAT_DYNAMIC;
497         di->u.pi.name_ptr = (unw_word_t)"throw_corlib_exception_trampoline";
498         di->u.pi.regions = r_pro;
499
500         _U_dyn_register (di);
501
502         mono_arch_flush_icache (start, code.buf - start);
503
504         res = ia64_create_ftnptr (start);
505         inited = TRUE;
506
507         return res;
508 }
509
510 /* mono_arch_find_jit_info:
511  *
512  * This function is used to gather information from @ctx. It return the 
513  * MonoJitInfo of the corresponding function, unwinds one stack frame and
514  * stores the resulting context into @new_ctx. It also stores a string 
515  * describing the stack location into @trace (if not NULL), and modifies
516  * the @lmf if necessary. @native_offset return the IP offset from the 
517  * start of the function or -1 if that info is not available.
518  */
519 MonoJitInfo *
520 mono_arch_find_jit_info (MonoDomain *domain, MonoJitTlsData *jit_tls, MonoJitInfo *res, MonoJitInfo *prev_ji, MonoContext *ctx, 
521                          MonoContext *new_ctx, MonoLMF **lmf, gboolean *managed)
522 {
523         MonoJitInfo *ji;
524         int err;
525         unw_word_t ip;
526
527         *new_ctx = *ctx;
528         new_ctx->precise_ip = FALSE;
529
530         while (TRUE) {
531                 err = unw_get_reg (&new_ctx->cursor, UNW_IA64_IP, &ip);
532                 g_assert (err == 0);
533
534                 /* Avoid costly table lookup during stack overflow */
535                 if (prev_ji && ((guint8*)ip > (guint8*)prev_ji->code_start && ((guint8*)ip < ((guint8*)prev_ji->code_start) + prev_ji->code_size)))
536                         ji = prev_ji;
537                 else
538                         ji = mini_jit_info_table_find (domain, (gpointer)ip, NULL);
539
540                 if (managed)
541                         *managed = FALSE;
542
543                 /*
544                 {
545                         char name[256];
546                         unw_word_t off;
547
548                         unw_get_proc_name (&new_ctx->cursor, name, 256, &off);
549                         printf ("F: %s\n", name);
550                 }
551                 */
552
553                 if (ji != NULL) {
554                         if (managed)
555                                 if (!ji->method->wrapper_type)
556                                         *managed = TRUE;
557
558                         break;
559                 }
560
561                 /* This is an unmanaged frame, so just unwind through it */
562                 /* FIXME: This returns -3 for the __clone2 frame in libc */
563                 err = unw_step (&new_ctx->cursor);
564                 if (err < 0)
565                         break;
566
567                 if (err == 0)
568                         break;
569         }
570
571         if (ji) {
572                 //print_ctx (new_ctx);
573
574                 err = unw_step (&new_ctx->cursor);
575                 g_assert (err >= 0);
576
577                 //print_ctx (new_ctx);
578
579                 return ji;
580         }
581         else
582                 return (gpointer)(gssize)-1;
583 }
584
585 /**
586  * mono_arch_handle_exception:
587  *
588  * @ctx: saved processor state
589  * @obj: the exception object
590  */
591 gboolean
592 mono_arch_handle_exception (void *sigctx, gpointer obj, gboolean test_only)
593 {
594         /* libunwind takes care of this */
595         unw_context_t unw_ctx;
596         MonoContext ctx;
597         MonoJitInfo *ji;
598         unw_word_t ip;
599         int res;
600
601         res = unw_getcontext (&unw_ctx);
602         g_assert (res == 0);
603         res = unw_init_local (&ctx.cursor, &unw_ctx);
604         g_assert (res == 0);
605
606         /* 
607          * Unwind until the first managed frame. This skips the signal handler frames
608          * too.
609          */
610         while (TRUE) {
611                 res = unw_get_reg (&ctx.cursor, UNW_IA64_IP, &ip);
612                 g_assert (res == 0);
613
614                 ji = mini_jit_info_table_find (mono_domain_get (), (gpointer)ip, NULL);
615
616                 if (ji)
617                         break;
618
619                 res = unw_step (&ctx.cursor);
620                 g_assert (res >= 0);
621         }
622         ctx.precise_ip = TRUE;
623
624         mono_handle_exception (&ctx, obj, (gpointer)ip, test_only);
625
626         restore_context (&ctx);
627
628         g_assert_not_reached ();
629 }
630
631 gpointer
632 mono_arch_ip_from_context (void *sigctx)
633 {
634         ucontext_t *ctx = (ucontext_t*)sigctx;
635
636         return (gpointer)ctx->uc_mcontext.sc_ip;
637 }