Merge pull request #2819 from BrzVlad/fix-major-log
[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-internals.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 (MonoTrampInfo **info, gboolean aot)
94 {
95         g_assert (!aot);
96         if (info)
97                 *info = NULL;
98
99         return restore_context;
100 }
101
102 static gpointer
103 get_real_call_filter (void)
104 {
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;
112
113         if (inited)
114                 return filter;
115
116         start = mono_global_codeman_reserve (1024);
117
118         /* int call_filter (guint64 fp, guint64 ip) */
119
120         /*
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.
133          */
134
135         in0 = 32;
136         local0 = in0 + 2;
137         out0 = local0 + 4;
138         nout = 0;
139
140         ia64_codegen_init (code, start);
141
142         ia64_codegen_set_one_ins_per_bundle (code, TRUE);
143
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);
148
149         ia64_begin_bundle (code);
150
151         r_pro = mono_ia64_create_unwind_region (&code);
152
153         /* Frame pointer */
154         ia64_mov (code, IA64_R15, in0 + 0);
155         /* Target ip */
156         ia64_mov_to_br (code, IA64_B6, in0 + 1);
157
158         /* Call the filter */
159         ia64_br_call_reg (code, IA64_B0, IA64_B6);
160
161         /* R8 contains the result of the filter */
162
163         /* FIXME: Add unwind info for this */
164
165         ia64_begin_bundle (code);
166
167         r_body = mono_ia64_create_unwind_region (&code);
168         r_pro->next = r_body;
169
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);
173
174         ia64_begin_bundle (code);
175
176         r_epilog = mono_ia64_create_unwind_region (&code);
177         r_body->next = r_epilog;
178
179         ia64_codegen_set_one_ins_per_bundle (code, FALSE);
180
181         ia64_codegen_close (code);
182
183         g_assert ((code.buf - start) <= 256);
184
185         mono_arch_flush_icache (start, code.buf - start);
186
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;
194
195         _U_dyn_register (di);
196
197     filter = ia64_create_ftnptr (start);
198
199         inited = TRUE;
200
201         return filter;
202 }
203
204 static int
205 call_filter (MonoContext *ctx, gpointer ip)
206 {
207         int (*filter) (MonoContext *, gpointer);
208         gpointer fp = MONO_CONTEXT_GET_BP (ctx);
209
210         filter = get_real_call_filter ();
211
212         return filter (fp, ip);
213 }
214
215 /*
216  * mono_arch_get_call_filter:
217  *
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).
221  */
222 gpointer
223 mono_arch_get_call_filter (MonoTrampInfo **info, gboolean aot)
224 {
225         g_assert (!aot);
226         if (info)
227                 *info = NULL;
228
229         /* Initialize the real filter non-lazily */
230         get_real_call_filter ();
231
232         return call_filter;
233 }
234
235 static void
236 throw_exception (MonoObject *exc, guint64 rethrow)
237 {
238         unw_context_t unw_ctx;
239         MonoError error;
240         MonoContext ctx;
241         MonoJitInfo *ji;
242         unw_word_t ip, sp;
243         int res;
244
245         if (mono_object_isinst_checked (exc, mono_defaults.exception_class, &error)) {
246                 MonoException *mono_ex = (MonoException*)exc;
247                 if (!rethrow) {
248                         mono_ex->stack_trace = NULL;
249                         mono_ex->trace_ips = NULL;
250                 }
251         }
252         mono_error_assert_ok (&error);
253
254         res = unw_getcontext (&unw_ctx);
255         g_assert (res == 0);
256         res = unw_init_local (&ctx.cursor, &unw_ctx);
257         g_assert (res == 0);
258
259         /* 
260          * Unwind until the first managed frame. This is needed since 
261          * mono_handle_exception expects the variables in the original context to
262          * correspond to the method returned by mono_find_jit_info.
263          */
264         while (TRUE) {
265                 res = unw_get_reg (&ctx.cursor, UNW_IA64_IP, &ip);
266                 g_assert (res == 0);
267
268                 res = unw_get_reg (&ctx.cursor, UNW_IA64_SP, &sp);
269                 g_assert (res == 0);
270
271                 ji = mini_jit_info_table_find (mono_domain_get (), (gpointer)ip, NULL);
272
273                 //printf ("UN: %s %lx %lx\n", ji ? jinfo_get_method (ji)->name : "", ip, sp);
274
275                 if (ji)
276                         break;
277
278                 res = unw_step (&ctx.cursor);
279
280                 if (res == 0) {
281                         /*
282                          * This means an unhandled exception during the compilation of a
283                          * topmost method like Main
284                          */
285                         break;
286                 }
287                 g_assert (res >= 0);
288         }
289         ctx.precise_ip = FALSE;
290
291         mono_handle_exception (&ctx, exc);
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 (MonoTrampInfo **info, gboolean aot)
371 {
372         g_assert (!aot);
373         if (info)
374                 *info = NULL;
375
376         return get_throw_trampoline (FALSE);
377 }
378
379 gpointer
380 mono_arch_get_rethrow_exception (MonoTrampInfo **info, gboolean aot)
381 {
382         g_assert (!aot);
383         if (info)
384                 *info = NULL;
385
386         return get_throw_trampoline (TRUE);
387 }
388
389 /**
390  * mono_arch_get_throw_corlib_exception:
391  *
392  * Returns a function pointer which can be used to raise 
393  * corlib exceptions. The returned function has the following 
394  * signature: void (*func) (guint32 ex_token_index, guint32 offset); 
395  * Here, offset is the offset which needs to be substracted from the caller IP 
396  * to get the IP of the throw. Passing the offset has the advantage that it 
397  * needs no relocations in the caller.
398  */
399 gpointer
400 mono_arch_get_throw_corlib_exception (MonoTrampInfo **info, gboolean aot)
401 {
402         static guint8* res;
403         static gboolean inited = FALSE;
404         guint8 *start;
405         gpointer ptr;
406         int i, in0, local0, out0, nout;
407         Ia64CodegenState code;
408         unw_dyn_info_t *di;
409         unw_dyn_region_info_t *r_pro;
410
411         g_assert (!aot);
412         if (info)
413                 *info = NULL;
414
415         if (inited)
416                 return res;
417
418         start = mono_global_codeman_reserve (1024);
419
420         in0 = 32;
421         local0 = in0 + 2;
422         out0 = local0 + 4;
423         nout = 3;
424
425         ia64_codegen_init (code, start);
426         ia64_alloc (code, local0 + 0, local0 - in0, out0 - local0, nout, 0);
427         ia64_mov_from_br (code, local0 + 1, IA64_RP);
428
429         r_pro = g_malloc0 (_U_dyn_region_info_size (2));
430         r_pro->op_count = 2;
431         r_pro->insn_count = 6;
432         i = 0;
433         _U_dyn_op_save_reg (&r_pro->op[i++], _U_QP_TRUE, /* when=*/ 2,
434                                                 /* reg=*/ UNW_IA64_AR_PFS, /* dst=*/ UNW_IA64_GR + local0 + 0);
435         _U_dyn_op_save_reg (&r_pro->op[i++], _U_QP_TRUE, /* when=*/ 5,
436                                                 /* reg=*/ UNW_IA64_RP, /* dst=*/ UNW_IA64_GR + local0 + 1);
437         g_assert ((unsigned) i <= r_pro->op_count);     
438
439         /* Call exception_from_token */
440         ia64_movl (code, out0 + 0, mono_defaults.exception_class->image);
441         ia64_mov (code, out0 + 1, in0 + 0);
442         ia64_movl (code, GP_SCRATCH_REG, MONO_TOKEN_TYPE_DEF);
443         ia64_add (code, out0 + 1, in0 + 0, GP_SCRATCH_REG);
444         ptr = mono_exception_from_token;
445         ia64_movl (code, GP_SCRATCH_REG, ptr);
446         ia64_ld8_inc_imm (code, GP_SCRATCH_REG2, GP_SCRATCH_REG, 8);
447         ia64_mov_to_br (code, IA64_B6, GP_SCRATCH_REG2);
448         ia64_ld8 (code, IA64_GP, GP_SCRATCH_REG);
449         ia64_br_call_reg (code, IA64_B0, IA64_B6);
450         ia64_mov (code, local0 + 3, IA64_R8);
451
452         /* Compute throw ip */
453         ia64_mov (code, local0 + 2, local0 + 1);
454         ia64_sub (code, local0 + 2, local0 + 2, in0 + 1);
455
456         /* Trick the unwind library into using throw_ip as the IP in the caller frame */
457         ia64_mov (code, local0 + 1, local0 + 2);
458
459         /* Set args */
460         ia64_mov (code, out0 + 0, local0 + 3);
461         ia64_mov (code, out0 + 1, IA64_R0);
462
463         /* Call throw_exception */
464         ptr = throw_exception;
465         ia64_movl (code, GP_SCRATCH_REG, ptr);
466         ia64_ld8_inc_imm (code, GP_SCRATCH_REG2, GP_SCRATCH_REG, 8);
467         ia64_mov_to_br (code, IA64_B6, GP_SCRATCH_REG2);
468         ia64_ld8 (code, IA64_GP, GP_SCRATCH_REG);
469         ia64_br_call_reg (code, IA64_B0, IA64_B6);
470
471         ia64_break_i (code, 1002);
472         ia64_codegen_close (code);
473
474         g_assert ((code.buf - start) <= 1024);
475
476         di = g_malloc0 (sizeof (unw_dyn_info_t));
477         di->start_ip = (unw_word_t) start;
478         di->end_ip = (unw_word_t) code.buf;
479         di->gp = 0;
480         di->format = UNW_INFO_FORMAT_DYNAMIC;
481         di->u.pi.name_ptr = (unw_word_t)"throw_corlib_exception_trampoline";
482         di->u.pi.regions = r_pro;
483
484         _U_dyn_register (di);
485
486         mono_arch_flush_icache (start, code.buf - start);
487
488         res = ia64_create_ftnptr (start);
489         inited = TRUE;
490
491         return res;
492 }
493
494 /*
495  * mono_arch_unwind_frame:
496  *
497  * This function is used to gather information from @ctx, and store it in @frame_info.
498  * It unwinds one stack frame, and stores the resulting context into @new_ctx. @lmf
499  * is modified if needed.
500  * Returns TRUE on success, FALSE otherwise.
501  */
502 gboolean
503 mono_arch_unwind_frame (MonoDomain *domain, MonoJitTlsData *jit_tls, 
504                                                          MonoJitInfo *ji, MonoContext *ctx, 
505                                                          MonoContext *new_ctx, MonoLMF **lmf,
506                                                          mgreg_t **save_locations,
507                                                          StackFrameInfo *frame)
508 {
509         int err;
510         unw_word_t ip;
511
512         memset (frame, 0, sizeof (StackFrameInfo));
513         frame->ji = ji;
514
515         *new_ctx = *ctx;
516         new_ctx->precise_ip = FALSE;
517
518         if (!ji) {
519                 while (TRUE) {
520                         err = unw_get_reg (&new_ctx->cursor, UNW_IA64_IP, &ip);
521                         g_assert (err == 0);
522
523                         ji = mini_jit_info_table_find (domain, (gpointer)ip, NULL);
524
525                         /*
526                           {
527                           char name[256];
528                           unw_word_t off;
529
530                           unw_get_proc_name (&new_ctx->cursor, name, 256, &off);
531                           printf ("F: %s\n", name);
532                           }
533                         */
534
535                         if (ji)
536                                 break;
537
538                         /* This is an unmanaged frame, so just unwind through it */
539                         /* FIXME: This returns -3 for the __clone2 frame in libc */
540                         err = unw_step (&new_ctx->cursor);
541                         if (err < 0)
542                                 break;
543
544                         if (err == 0)
545                                 break;
546                 }
547         }
548
549         if (ji) {
550                 if (ji->is_trampoline)
551                         frame->type = FRAME_TYPE_TRAMPOLINE;
552                 else
553                         frame->type = FRAME_TYPE_MANAGED;
554                 frame->ji = ji;
555
556                 //print_ctx (new_ctx);
557
558                 err = unw_step (&new_ctx->cursor);
559                 g_assert (err >= 0);
560
561                 //print_ctx (new_ctx);
562
563                 return TRUE;
564         }
565         else
566                 return FALSE;
567 }
568
569 /**
570  * mono_arch_handle_exception:
571  *
572  * @ctx: saved processor state
573  * @obj: the exception object
574  */
575 gboolean
576 mono_arch_handle_exception (void *sigctx, gpointer obj)
577 {
578         /* libunwind takes care of this */
579         unw_context_t unw_ctx;
580         MonoContext ctx;
581         MonoJitInfo *ji;
582         unw_word_t ip;
583         int res;
584
585         res = unw_getcontext (&unw_ctx);
586         g_assert (res == 0);
587         res = unw_init_local (&ctx.cursor, &unw_ctx);
588         g_assert (res == 0);
589
590         /* 
591          * Unwind until the first managed frame. This skips the signal handler frames
592          * too.
593          */
594         while (TRUE) {
595                 res = unw_get_reg (&ctx.cursor, UNW_IA64_IP, &ip);
596                 g_assert (res == 0);
597
598                 ji = mini_jit_info_table_find (mono_domain_get (), (gpointer)ip, NULL);
599
600                 if (ji)
601                         break;
602
603                 res = unw_step (&ctx.cursor);
604                 g_assert (res >= 0);
605         }
606         ctx.precise_ip = TRUE;
607
608         mono_handle_exception (&ctx, obj);
609
610         restore_context (&ctx);
611
612         g_assert_not_reached ();
613 }
614
615 gpointer
616 mono_arch_ip_from_context (void *sigctx)
617 {
618         ucontext_t *ctx = (ucontext_t*)sigctx;
619
620         return (gpointer)ctx->uc_mcontext.sc_ip;
621 }