[docs] Enable documentation for mini.
[mono.git] / mono / mini / exceptions-ia64.c
1 /**
2  * \file
3  * exception support for IA64
4  *
5  * Authors:
6  *   Zoltan Varga (vargaz@gmail.com)
7  *
8  * (C) 2001 Ximian, Inc.
9  */
10
11 /*
12  * We implement exception handling with the help of the libuwind library:
13  * 
14  * http://www.hpl.hp.com/research/linux/libunwind/
15  *
16  *  Under IA64 all functions are assumed to have unwind info, we do not need to save
17  * the machine state in the LMF. But we have to generate unwind info for all 
18  * dynamically generated code.
19  */
20
21 #include <config.h>
22 #include <glib.h>
23 #include <signal.h>
24 #include <string.h>
25 #include <sys/ucontext.h>
26
27 #include <mono/arch/ia64/ia64-codegen.h>
28 #include <mono/metadata/appdomain.h>
29 #include <mono/metadata/tabledefs.h>
30 #include <mono/metadata/threads.h>
31 #include <mono/metadata/debug-helpers.h>
32 #include <mono/metadata/exception.h>
33 #include <mono/metadata/gc-internals.h>
34 #include <mono/metadata/mono-debug.h>
35
36 #include "mini.h"
37 #include "mini-ia64.h"
38
39 #define ALIGN_TO(val,align) (((val) + ((align) - 1)) & ~((align) - 1))
40
41 #define GP_SCRATCH_REG 31
42 #define GP_SCRATCH_REG2 30
43
44 G_GNUC_UNUSED static void
45 print_ctx (MonoContext *ctx)
46 {
47         char name[256];
48         unw_word_t off, ip, sp;
49         unw_proc_info_t pi;
50         int res;
51
52         unw_get_proc_name (&ctx->cursor, name, 256, &off);
53         unw_get_proc_info(&ctx->cursor, &pi);
54         res = unw_get_reg (&ctx->cursor, UNW_IA64_IP, &ip);
55         g_assert (res == 0);
56         res = unw_get_reg (&ctx->cursor, UNW_IA64_SP, &sp);
57         g_assert (res == 0);
58
59         printf ("%s:%lx [%lx-%lx] SP: %lx\n", name, ip - pi.start_ip, pi.start_ip, pi.end_ip, sp);
60 }
61
62 static gpointer
63 ia64_create_ftnptr (gpointer ptr)
64 {
65         gpointer *desc = mono_global_codeman_reserve (2 * sizeof (gpointer));
66         desc [0] = ptr;
67         desc [1] = NULL;
68
69         return desc;
70 }
71
72 static void
73 restore_context (MonoContext *ctx)
74 {
75         int res;
76         unw_word_t ip;
77
78         res = unw_get_reg (&ctx->cursor, UNW_IA64_IP, &ip);
79         g_assert (res == 0);
80
81         /* Set this to 0 to tell OP_START_HANDLER that it doesn't have to set the frame pointer */
82         res = unw_set_reg (&ctx->cursor, UNW_IA64_GR + 15, 0);
83         g_assert (res == 0);
84
85         unw_resume (&ctx->cursor);
86 }
87
88 /*
89  * mono_arch_get_restore_context:
90  *
91  * Returns a pointer to a method which restores a previously saved sigcontext.
92  */
93 gpointer
94 mono_arch_get_restore_context (MonoTrampInfo **info, gboolean aot)
95 {
96         g_assert (!aot);
97         if (info)
98                 *info = NULL;
99
100         return restore_context;
101 }
102
103 static gpointer
104 get_real_call_filter (void)
105 {
106         static gpointer filter;
107         static gboolean inited = FALSE;
108         guint8 *start;
109         Ia64CodegenState code;
110         int in0, local0, out0, nout;
111         unw_dyn_info_t *di;
112         unw_dyn_region_info_t *r_pro, *r_body, *r_epilog;
113
114         if (inited)
115                 return filter;
116
117         start = mono_global_codeman_reserve (1024);
118
119         /* int call_filter (guint64 fp, guint64 ip) */
120
121         /*
122          * We have to create a register+stack frame similar to the frame which 
123          * contains the filter. 
124          * - setting fp
125          * - setting up a register stack frame
126          * These cannot be set up in this function, because the fp register is a 
127          * stacked register which is different in each method. Also, the register 
128          * stack frame is different in each method. So we pass the FP value in a a 
129          * non-stacked register and the code generated by the OP_START_HANDLER 
130          * opcode will copy it to the appropriate register after setting up the 
131          * register stack frame.
132          * The stacked registers are not need to be set since variables used in
133          * handler regions are never allocated to registers.
134          */
135
136         in0 = 32;
137         local0 = in0 + 2;
138         out0 = local0 + 4;
139         nout = 0;
140
141         ia64_codegen_init (code, start);
142
143         ia64_codegen_set_one_ins_per_bundle (code, TRUE);
144
145         ia64_unw_save_reg (code, UNW_IA64_AR_PFS, UNW_IA64_GR + local0 + 0);
146         ia64_alloc (code, local0 + 0, local0 - in0, out0 - local0, nout, 0);
147         ia64_unw_save_reg (code, UNW_IA64_RP, UNW_IA64_GR + local0 + 1);
148         ia64_mov_from_br (code, local0 + 1, IA64_B0);
149
150         ia64_begin_bundle (code);
151
152         r_pro = mono_ia64_create_unwind_region (&code);
153
154         /* Frame pointer */
155         ia64_mov (code, IA64_R15, in0 + 0);
156         /* Target ip */
157         ia64_mov_to_br (code, IA64_B6, in0 + 1);
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
164         /* FIXME: Add unwind info for this */
165
166         ia64_begin_bundle (code);
167
168         r_body = mono_ia64_create_unwind_region (&code);
169         r_pro->next = r_body;
170
171         ia64_mov_to_ar_i (code, IA64_PFS, local0 + 0);
172         ia64_mov_ret_to_br (code, IA64_B0, local0 + 1);
173         ia64_br_ret_reg (code, IA64_B0);
174
175         ia64_begin_bundle (code);
176
177         r_epilog = mono_ia64_create_unwind_region (&code);
178         r_body->next = r_epilog;
179
180         ia64_codegen_set_one_ins_per_bundle (code, FALSE);
181
182         ia64_codegen_close (code);
183
184         g_assert ((code.buf - start) <= 256);
185
186         mono_arch_flush_icache (start, code.buf - start);
187
188         di = g_malloc0 (sizeof (unw_dyn_info_t));
189         di->start_ip = (unw_word_t) start;
190         di->end_ip = (unw_word_t) code.buf;
191         di->gp = 0;
192         di->format = UNW_INFO_FORMAT_DYNAMIC;
193         di->u.pi.name_ptr = (unw_word_t)"throw_trampoline";
194         di->u.pi.regions = r_body;
195
196         _U_dyn_register (di);
197
198     filter = ia64_create_ftnptr (start);
199
200         inited = TRUE;
201
202         return filter;
203 }
204
205 static int
206 call_filter (MonoContext *ctx, gpointer ip)
207 {
208         int (*filter) (MonoContext *, gpointer);
209         gpointer fp = MONO_CONTEXT_GET_BP (ctx);
210
211         filter = get_real_call_filter ();
212
213         return filter (fp, ip);
214 }
215
216 /*
217  * mono_arch_get_call_filter:
218  *
219  * Returns a pointer to a method which calls an exception filter. We
220  * also use this function to call finally handlers (we pass NULL as 
221  * @exc object in this case).
222  */
223 gpointer
224 mono_arch_get_call_filter (MonoTrampInfo **info, gboolean aot)
225 {
226         g_assert (!aot);
227         if (info)
228                 *info = NULL;
229
230         /* Initialize the real filter non-lazily */
231         get_real_call_filter ();
232
233         return call_filter;
234 }
235
236 static void
237 throw_exception (MonoObject *exc, guint64 rethrow)
238 {
239         unw_context_t unw_ctx;
240         MonoError error;
241         MonoContext ctx;
242         MonoJitInfo *ji;
243         unw_word_t ip, sp;
244         int res;
245
246         if (mono_object_isinst_checked (exc, mono_defaults.exception_class, &error)) {
247                 MonoException *mono_ex = (MonoException*)exc;
248                 if (!rethrow) {
249                         mono_ex->stack_trace = NULL;
250                         mono_ex->trace_ips = NULL;
251                 }
252         }
253         mono_error_assert_ok (&error);
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 = mini_jit_info_table_find (mono_domain_get (), (gpointer)ip, NULL);
273
274                 //printf ("UN: %s %lx %lx\n", ji ? jinfo_get_method (ji)->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         ctx.precise_ip = FALSE;
291
292         mono_handle_exception (&ctx, exc);
293         restore_context (&ctx);
294
295         g_assert_not_reached ();
296 }
297
298 static gpointer
299 get_throw_trampoline (gboolean rethrow)
300 {
301         guint8* start;
302         Ia64CodegenState code;
303         gpointer ptr = throw_exception;
304         int i, in0, local0, out0;
305         unw_dyn_info_t *di;
306         unw_dyn_region_info_t *r_pro;
307
308         start = mono_global_codeman_reserve (256);
309
310         in0 = 32;
311         local0 = in0 + 1;
312         out0 = local0 + 2;
313
314         ia64_codegen_init (code, start);
315         ia64_alloc (code, local0 + 0, local0 - in0, out0 - local0, 3, 0);
316         ia64_mov_from_br (code, local0 + 1, IA64_B0);   
317
318         /* FIXME: This depends on the current instruction emitter */
319
320         r_pro = g_malloc0 (_U_dyn_region_info_size (2));
321         r_pro->op_count = 2;
322         r_pro->insn_count = 6;
323         i = 0;
324         _U_dyn_op_save_reg (&r_pro->op[i++], _U_QP_TRUE, /* when=*/ 2,
325                                                 /* reg=*/ UNW_IA64_AR_PFS, /* dst=*/ UNW_IA64_GR + local0 + 0);
326         _U_dyn_op_save_reg (&r_pro->op[i++], _U_QP_TRUE, /* when=*/ 5,
327                                                 /* reg=*/ UNW_IA64_RP, /* dst=*/ UNW_IA64_GR + local0 + 1);
328         g_assert ((unsigned) i <= r_pro->op_count);     
329
330         /* Set args */
331         ia64_mov (code, out0 + 0, in0 + 0);
332         ia64_adds_imm (code, out0 + 1, rethrow, IA64_R0);
333
334         /* Call throw_exception */
335         ia64_movl (code, GP_SCRATCH_REG, ptr);
336         ia64_ld8_inc_imm (code, GP_SCRATCH_REG2, GP_SCRATCH_REG, 8);
337         ia64_mov_to_br (code, IA64_B6, GP_SCRATCH_REG2);
338         ia64_ld8 (code, IA64_GP, GP_SCRATCH_REG);
339         ia64_br_call_reg (code, IA64_B0, IA64_B6);
340
341         /* Not reached */
342         ia64_break_i (code, 1000);
343         ia64_codegen_close (code);
344
345         g_assert ((code.buf - start) <= 256);
346
347         mono_arch_flush_icache (start, code.buf - start);
348
349         di = g_malloc0 (sizeof (unw_dyn_info_t));
350         di->start_ip = (unw_word_t) start;
351         di->end_ip = (unw_word_t) code.buf;
352         di->gp = 0;
353         di->format = UNW_INFO_FORMAT_DYNAMIC;
354         di->u.pi.name_ptr = (unw_word_t)"throw_trampoline";
355         di->u.pi.regions = r_pro;
356
357         _U_dyn_register (di);
358
359         return ia64_create_ftnptr (start);
360 }
361
362 /**
363  * mono_arch_get_throw_exception:
364  *
365  * Returns a function pointer which can be used to raise 
366  * exceptions. The returned function has the following 
367  * signature: void (*func) (MonoException *exc); 
368  *
369  */
370 gpointer
371 mono_arch_get_throw_exception (MonoTrampInfo **info, gboolean aot)
372 {
373         g_assert (!aot);
374         if (info)
375                 *info = NULL;
376
377         return get_throw_trampoline (FALSE);
378 }
379
380 gpointer
381 mono_arch_get_rethrow_exception (MonoTrampInfo **info, gboolean aot)
382 {
383         g_assert (!aot);
384         if (info)
385                 *info = NULL;
386
387         return get_throw_trampoline (TRUE);
388 }
389
390 /**
391  * mono_arch_get_throw_corlib_exception:
392  *
393  * Returns a function pointer which can be used to raise 
394  * corlib exceptions. The returned function has the following 
395  * signature: void (*func) (guint32 ex_token_index, guint32 offset); 
396  * Here, offset is the offset which needs to be substracted from the caller IP 
397  * to get the IP of the throw. Passing the offset has the advantage that it 
398  * needs no relocations in the caller.
399  */
400 gpointer
401 mono_arch_get_throw_corlib_exception (MonoTrampInfo **info, gboolean aot)
402 {
403         static guint8* res;
404         static gboolean inited = FALSE;
405         guint8 *start;
406         gpointer ptr;
407         int i, in0, local0, out0, nout;
408         Ia64CodegenState code;
409         unw_dyn_info_t *di;
410         unw_dyn_region_info_t *r_pro;
411
412         g_assert (!aot);
413         if (info)
414                 *info = NULL;
415
416         if (inited)
417                 return res;
418
419         start = mono_global_codeman_reserve (1024);
420
421         in0 = 32;
422         local0 = in0 + 2;
423         out0 = local0 + 4;
424         nout = 3;
425
426         ia64_codegen_init (code, start);
427         ia64_alloc (code, local0 + 0, local0 - in0, out0 - local0, nout, 0);
428         ia64_mov_from_br (code, local0 + 1, IA64_RP);
429
430         r_pro = g_malloc0 (_U_dyn_region_info_size (2));
431         r_pro->op_count = 2;
432         r_pro->insn_count = 6;
433         i = 0;
434         _U_dyn_op_save_reg (&r_pro->op[i++], _U_QP_TRUE, /* when=*/ 2,
435                                                 /* reg=*/ UNW_IA64_AR_PFS, /* dst=*/ UNW_IA64_GR + local0 + 0);
436         _U_dyn_op_save_reg (&r_pro->op[i++], _U_QP_TRUE, /* when=*/ 5,
437                                                 /* reg=*/ UNW_IA64_RP, /* dst=*/ UNW_IA64_GR + local0 + 1);
438         g_assert ((unsigned) i <= r_pro->op_count);     
439
440         /* Call exception_from_token */
441         ia64_movl (code, out0 + 0, mono_defaults.exception_class->image);
442         ia64_mov (code, out0 + 1, in0 + 0);
443         ia64_movl (code, GP_SCRATCH_REG, MONO_TOKEN_TYPE_DEF);
444         ia64_add (code, out0 + 1, in0 + 0, GP_SCRATCH_REG);
445         ptr = mono_exception_from_token;
446         ia64_movl (code, GP_SCRATCH_REG, ptr);
447         ia64_ld8_inc_imm (code, GP_SCRATCH_REG2, GP_SCRATCH_REG, 8);
448         ia64_mov_to_br (code, IA64_B6, GP_SCRATCH_REG2);
449         ia64_ld8 (code, IA64_GP, GP_SCRATCH_REG);
450         ia64_br_call_reg (code, IA64_B0, IA64_B6);
451         ia64_mov (code, local0 + 3, IA64_R8);
452
453         /* Compute throw ip */
454         ia64_mov (code, local0 + 2, local0 + 1);
455         ia64_sub (code, local0 + 2, local0 + 2, in0 + 1);
456
457         /* Trick the unwind library into using throw_ip as the IP in the caller frame */
458         ia64_mov (code, local0 + 1, local0 + 2);
459
460         /* Set args */
461         ia64_mov (code, out0 + 0, local0 + 3);
462         ia64_mov (code, out0 + 1, IA64_R0);
463
464         /* Call throw_exception */
465         ptr = throw_exception;
466         ia64_movl (code, GP_SCRATCH_REG, ptr);
467         ia64_ld8_inc_imm (code, GP_SCRATCH_REG2, GP_SCRATCH_REG, 8);
468         ia64_mov_to_br (code, IA64_B6, GP_SCRATCH_REG2);
469         ia64_ld8 (code, IA64_GP, GP_SCRATCH_REG);
470         ia64_br_call_reg (code, IA64_B0, IA64_B6);
471
472         ia64_break_i (code, 1002);
473         ia64_codegen_close (code);
474
475         g_assert ((code.buf - start) <= 1024);
476
477         di = g_malloc0 (sizeof (unw_dyn_info_t));
478         di->start_ip = (unw_word_t) start;
479         di->end_ip = (unw_word_t) code.buf;
480         di->gp = 0;
481         di->format = UNW_INFO_FORMAT_DYNAMIC;
482         di->u.pi.name_ptr = (unw_word_t)"throw_corlib_exception_trampoline";
483         di->u.pi.regions = r_pro;
484
485         _U_dyn_register (di);
486
487         mono_arch_flush_icache (start, code.buf - start);
488
489         res = ia64_create_ftnptr (start);
490         inited = TRUE;
491
492         return res;
493 }
494
495 /*
496  * mono_arch_unwind_frame:
497  *
498  * This function is used to gather information from @ctx, and store it in @frame_info.
499  * It unwinds one stack frame, and stores the resulting context into @new_ctx. @lmf
500  * is modified if needed.
501  * Returns TRUE on success, FALSE otherwise.
502  */
503 gboolean
504 mono_arch_unwind_frame (MonoDomain *domain, MonoJitTlsData *jit_tls, 
505                                                          MonoJitInfo *ji, MonoContext *ctx, 
506                                                          MonoContext *new_ctx, MonoLMF **lmf,
507                                                          mgreg_t **save_locations,
508                                                          StackFrameInfo *frame)
509 {
510         int err;
511         unw_word_t ip;
512
513         memset (frame, 0, sizeof (StackFrameInfo));
514         frame->ji = ji;
515
516         *new_ctx = *ctx;
517         new_ctx->precise_ip = FALSE;
518
519         if (!ji) {
520                 while (TRUE) {
521                         err = unw_get_reg (&new_ctx->cursor, UNW_IA64_IP, &ip);
522                         g_assert (err == 0);
523
524                         ji = mini_jit_info_table_find (domain, (gpointer)ip, NULL);
525
526                         /*
527                           {
528                           char name[256];
529                           unw_word_t off;
530
531                           unw_get_proc_name (&new_ctx->cursor, name, 256, &off);
532                           printf ("F: %s\n", name);
533                           }
534                         */
535
536                         if (ji)
537                                 break;
538
539                         /* This is an unmanaged frame, so just unwind through it */
540                         /* FIXME: This returns -3 for the __clone2 frame in libc */
541                         err = unw_step (&new_ctx->cursor);
542                         if (err < 0)
543                                 break;
544
545                         if (err == 0)
546                                 break;
547                 }
548         }
549
550         if (ji) {
551                 if (ji->is_trampoline)
552                         frame->type = FRAME_TYPE_TRAMPOLINE;
553                 else
554                         frame->type = FRAME_TYPE_MANAGED;
555                 frame->ji = ji;
556
557                 //print_ctx (new_ctx);
558
559                 err = unw_step (&new_ctx->cursor);
560                 g_assert (err >= 0);
561
562                 //print_ctx (new_ctx);
563
564                 return TRUE;
565         }
566         else
567                 return FALSE;
568 }
569
570 /**
571  * mono_arch_handle_exception:
572  *
573  * @ctx: saved processor state
574  * @obj: the exception object
575  */
576 gboolean
577 mono_arch_handle_exception (void *sigctx, gpointer obj)
578 {
579         /* libunwind takes care of this */
580         unw_context_t unw_ctx;
581         MonoContext ctx;
582         MonoJitInfo *ji;
583         unw_word_t ip;
584         int res;
585
586         res = unw_getcontext (&unw_ctx);
587         g_assert (res == 0);
588         res = unw_init_local (&ctx.cursor, &unw_ctx);
589         g_assert (res == 0);
590
591         /* 
592          * Unwind until the first managed frame. This skips the signal handler frames
593          * too.
594          */
595         while (TRUE) {
596                 res = unw_get_reg (&ctx.cursor, UNW_IA64_IP, &ip);
597                 g_assert (res == 0);
598
599                 ji = mini_jit_info_table_find (mono_domain_get (), (gpointer)ip, NULL);
600
601                 if (ji)
602                         break;
603
604                 res = unw_step (&ctx.cursor);
605                 g_assert (res >= 0);
606         }
607         ctx.precise_ip = TRUE;
608
609         mono_handle_exception (&ctx, obj);
610
611         restore_context (&ctx);
612
613         g_assert_not_reached ();
614 }
615
616 gpointer
617 mono_arch_ip_from_context (void *sigctx)
618 {
619         ucontext_t *ctx = (ucontext_t*)sigctx;
620
621         return (gpointer)ctx->uc_mcontext.sc_ip;
622 }