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