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