2008-01-26 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 = mono_jit_info_table_find (mono_domain_get (), (gpointer)ip);
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
278         mono_handle_exception (&ctx, exc, (gpointer)(ip), FALSE);
279         restore_context (&ctx);
280
281         g_assert_not_reached ();
282 }
283
284 static gpointer
285 get_throw_trampoline (gboolean rethrow)
286 {
287         guint8* start;
288         Ia64CodegenState code;
289         gpointer ptr = throw_exception;
290         int i, in0, local0, out0;
291         unw_dyn_info_t *di;
292         unw_dyn_region_info_t *r_pro;
293
294         start = mono_global_codeman_reserve (256);
295
296         in0 = 32;
297         local0 = in0 + 1;
298         out0 = local0 + 2;
299
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);   
303
304         /* FIXME: This depends on the current instruction emitter */
305
306         r_pro = g_malloc0 (_U_dyn_region_info_size (2));
307         r_pro->op_count = 2;
308         r_pro->insn_count = 6;
309         i = 0;
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);     
315
316         /* Set args */
317         ia64_mov (code, out0 + 0, in0 + 0);
318         ia64_adds_imm (code, out0 + 1, rethrow, IA64_R0);
319
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);
326
327         /* Not reached */
328         ia64_break_i (code, 1000);
329         ia64_codegen_close (code);
330
331         g_assert ((code.buf - start) <= 256);
332
333         mono_arch_flush_icache (start, code.buf - start);
334
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;
338         di->gp = 0;
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;
342
343         _U_dyn_register (di);
344
345         return ia64_create_ftnptr (start);
346 }
347
348 /**
349  * mono_arch_get_throw_exception:
350  *
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); 
354  *
355  */
356 gpointer 
357 mono_arch_get_throw_exception (void)
358 {
359         static guint8* start;
360         static gboolean inited = FALSE;
361
362         if (inited)
363                 return start;
364
365         start = get_throw_trampoline (FALSE);
366
367         inited = TRUE;
368
369         return start;
370 }
371
372 gpointer 
373 mono_arch_get_rethrow_exception (void)
374 {
375         static guint8* start;
376         static gboolean inited = FALSE;
377
378         if (inited)
379                 return start;
380
381         start = get_throw_trampoline (TRUE);
382
383         inited = TRUE;
384
385         return start;
386 }
387
388 gpointer 
389 mono_arch_get_throw_exception_by_name (void)
390 {       
391         guint8* start;
392         Ia64CodegenState code;
393
394         start = mono_global_codeman_reserve (64);
395
396         /* Not used on ia64 */
397         ia64_codegen_init (code, start);
398         ia64_break_i (code, 1001);
399         ia64_codegen_close (code);
400
401         g_assert ((code.buf - start) <= 256);
402
403         mono_arch_flush_icache (start, code.buf - start);
404
405         return start;
406 }
407
408 /**
409  * mono_arch_get_throw_corlib_exception:
410  *
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.
417  */
418 gpointer 
419 mono_arch_get_throw_corlib_exception (void)
420 {
421         static guint8* res;
422         static gboolean inited = FALSE;
423         guint8 *start;
424         gpointer ptr;
425         int i, in0, local0, out0, nout;
426         Ia64CodegenState code;
427         unw_dyn_info_t *di;
428         unw_dyn_region_info_t *r_pro;
429
430         if (inited)
431                 return res;
432
433         start = mono_global_codeman_reserve (1024);
434
435         in0 = 32;
436         local0 = in0 + 2;
437         out0 = local0 + 4;
438         nout = 3;
439
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);
443
444         r_pro = g_malloc0 (_U_dyn_region_info_size (2));
445         r_pro->op_count = 2;
446         r_pro->insn_count = 6;
447         i = 0;
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);     
453
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);
466
467         /* Compute throw ip */
468         ia64_mov (code, local0 + 2, local0 + 1);
469         ia64_sub (code, local0 + 2, local0 + 2, in0 + 1);
470
471         /* Trick the unwind library into using throw_ip as the IP in the caller frame */
472         ia64_mov (code, local0 + 1, local0 + 2);
473
474         /* Set args */
475         ia64_mov (code, out0 + 0, local0 + 3);
476         ia64_mov (code, out0 + 1, IA64_R0);
477
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);
485
486         ia64_break_i (code, 1002);
487         ia64_codegen_close (code);
488
489         g_assert ((code.buf - start) <= 1024);
490
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;
494         di->gp = 0;
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;
498
499         _U_dyn_register (di);
500
501         mono_arch_flush_icache (start, code.buf - start);
502
503         res = ia64_create_ftnptr (start);
504         inited = TRUE;
505
506         return res;
507 }
508
509 /* mono_arch_find_jit_info:
510  *
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.
517  */
518 MonoJitInfo *
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,
521                          gboolean *managed)
522 {
523         MonoJitInfo *ji;
524         int err;
525         unw_word_t ip;
526
527         *new_ctx = *ctx;
528
529         while (TRUE) {
530                 err = unw_get_reg (&new_ctx->cursor, UNW_IA64_IP, &ip);
531                 g_assert (err == 0);
532
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)))
535                         ji = prev_ji;
536                 else
537                         ji = mono_jit_info_table_find (domain, (gpointer)ip);
538
539                 if (managed)
540                         *managed = FALSE;
541
542                 /*
543                 {
544                         char name[256];
545                         unw_word_t off;
546
547                         unw_get_proc_name (&new_ctx->cursor, name, 256, &off);
548                         printf ("F: %s\n", name);
549                 }
550                 */
551
552                 if (ji != NULL) {
553                         if (managed)
554                                 if (!ji->method->wrapper_type)
555                                         *managed = TRUE;
556
557                         break;
558                 }
559
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);
563                 if (err < 0)
564                         break;
565
566                 if (err == 0)
567                         break;
568         }
569
570         if (ji) {
571                 //print_ctx (new_ctx);
572
573                 err = unw_step (&new_ctx->cursor);
574                 g_assert (err >= 0);
575
576                 //print_ctx (new_ctx);
577
578                 return ji;
579         }
580         else
581                 return (gpointer)(gssize)-1;
582 }
583
584 /**
585  * mono_arch_handle_exception:
586  *
587  * @ctx: saved processor state
588  * @obj: the exception object
589  */
590 gboolean
591 mono_arch_handle_exception (void *sigctx, gpointer obj, gboolean test_only)
592 {
593         /* libunwind takes care of this */
594         unw_context_t unw_ctx;
595         MonoContext ctx;
596         MonoJitInfo *ji;
597         unw_word_t ip;
598         int res;
599
600         res = unw_getcontext (&unw_ctx);
601         g_assert (res == 0);
602         res = unw_init_local (&ctx.cursor, &unw_ctx);
603         g_assert (res == 0);
604
605         /* 
606          * Unwind until the first managed frame. This skips the signal handler frames
607          * too.
608          */
609         while (TRUE) {
610                 res = unw_get_reg (&ctx.cursor, UNW_IA64_IP, &ip);
611                 g_assert (res == 0);
612
613                 ji = mono_jit_info_table_find (mono_domain_get (), (gpointer)ip);
614
615                 if (ji)
616                         break;
617
618                 res = unw_step (&ctx.cursor);
619                 g_assert (res >= 0);
620         }
621
622         mono_handle_exception (&ctx, obj, (gpointer)ip, test_only);
623
624         restore_context (&ctx);
625
626         g_assert_not_reached ();
627 }
628
629 gpointer
630 mono_arch_ip_from_context (void *sigctx)
631 {
632         ucontext_t *ctx = (ucontext_t*)sigctx;
633
634         return (gpointer)ctx->uc_mcontext.sc_ip;
635 }