Tue May 18 11:05:18 CEST 2004 Paolo Molaro <lupus@ximian.com>
[mono.git] / mono / mini / exceptions-s390.c
1 /*------------------------------------------------------------------*/
2 /*                                                                  */
3 /* Name        - exceptions-s390.c                                  */
4 /*                                                                  */
5 /* Function    - Exception support for S/390.                       */
6 /*                                                                  */
7 /* Name        - Neale Ferguson (Neale.Ferguson@SoftwareAG-usa.com) */
8 /*                                                                  */
9 /* Date        - January, 2004                                      */
10 /*                                                                  */
11 /* Derivation  - From exceptions-x86 & exceptions-ppc               */
12 /*               Paolo Molaro (lupus@ximian.com)                    */
13 /*               Dietmar Maurer (dietmar@ximian.com)                */
14 /*                                                                  */
15 /* Copyright   - 2001 Ximian, Inc.                                  */
16 /*                                                                  */
17 /*------------------------------------------------------------------*/
18
19 /*------------------------------------------------------------------*/
20 /*                 D e f i n e s                                    */
21 /*------------------------------------------------------------------*/
22
23 #define MONO_CONTEXT_SET_IP(ctx,ip)                                     \
24         do {                                                            \
25                 (ctx)->uc_mcontext.gregs[14] = (unsigned long)ip;       \
26                 (ctx)->uc_mcontext.psw.addr = (unsigned long)ip;        \
27         } while (0); 
28
29 #define MONO_CONTEXT_SET_BP(ctx,bp)                                     \
30         do {                                                            \
31                 (ctx)->uc_mcontext.gregs[15] = (unsigned long)bp;       \
32                 (ctx)->uc_stack.ss_sp        = (unsigned long)bp;       \
33         } while (0); 
34
35 #define MONO_CONTEXT_GET_IP(ctx) context_get_ip ((ctx))
36 #define MONO_CONTEXT_GET_BP(ctx) ((gpointer)((ctx)->uc_mcontext.gregs[15]))
37
38 /* disable this for now */
39 #undef MONO_USE_EXC_TABLES
40
41 #define S390_CALLFILTER_INTREGS         S390_MINIMAL_STACK_SIZE
42 #define S390_CALLFILTER_FLTREGS         S390_CALLFILTER_INTREGS+(16*sizeof(gulong))
43 #define S390_CALLFILTER_ACCREGS         S390_CALLFILTER_FLTREGS+(16*sizeof(gdouble))
44 #define S390_CALLFILTER_SIZE            (S390_CALLFILTER_ACCREGS+(16*sizeof(gulong)))
45
46 #define S390_THROWSTACK_ACCPRM          S390_MINIMAL_STACK_SIZE
47 #define S390_THROWSTACK_FPCPRM          S390_THROWSTACK_ACCPRM+sizeof(gpointer)
48 #define S390_THROWSTACK_INTREGS         S390_THROWSTACK_FPCPRM+sizeof(gulong)
49 #define S390_THROWSTACK_FLTREGS         S390_THROWSTACK_INTREGS+(16*sizeof(gulong))
50 #define S390_THROWSTACK_ACCREGS         S390_THROWSTACK_FLTREGS+(16*sizeof(gdouble))
51 #define S390_THROWSTACK_SIZE            (S390_THROWSTACK_ACCREGS+(16*sizeof(gulong)))
52
53 /*========================= End of Defines =========================*/
54
55 /*------------------------------------------------------------------*/
56 /*                 I n c l u d e s                                  */
57 /*------------------------------------------------------------------*/
58
59 #include <config.h>
60 #include <glib.h>
61 #include <signal.h>
62 #include <string.h>
63
64 #include <mono/arch/s390/s390-codegen.h>
65 #include <mono/metadata/appdomain.h>
66 #include <mono/metadata/tabledefs.h>
67 #include <mono/metadata/threads.h>
68 #include <mono/metadata/debug-helpers.h>
69 #include <mono/metadata/exception.h>
70 #include <mono/metadata/mono-debug.h>
71
72 #include "mini.h"
73 #include "mini-s390.h"
74
75 /*========================= End of Includes ========================*/
76
77 /*------------------------------------------------------------------*/
78 /*                 T y p e d e f s                                  */
79 /*------------------------------------------------------------------*/
80
81 typedef struct
82 {
83   void *prev;
84   void *unused[5];
85   void *reg6;
86   void *reg7;
87   void *reg8;
88   void *reg9;
89   void *reg10;
90   void *reg11;
91   void *reg12;
92   void *reg13;
93   void *return_address;
94 } MonoS390StackFrame;
95
96 #ifdef MONO_USE_EXC_TABLES
97
98 /*************************************/
99 /*    STACK UNWINDING STUFF          */
100 /*************************************/
101
102 /* These definitions are from unwind-dw2.c in glibc 2.2.5 */
103
104 /* For x86 */
105 #define DWARF_FRAME_REGISTERS 17
106
107 typedef struct frame_state
108 {
109   void *cfa;
110   void *eh_ptr;
111   long cfa_offset;
112   long args_size;
113   long reg_or_offset[DWARF_FRAME_REGISTERS+1];
114   unsigned short cfa_reg;
115   unsigned short retaddr_column;
116   char saved[DWARF_FRAME_REGISTERS+1];
117 } frame_state;
118
119 typedef struct frame_state * (*framesf) (void *, struct frame_state *);
120
121 #endif
122
123 /*========================= End of Typedefs ========================*/
124
125 /*------------------------------------------------------------------*/
126 /*                   P r o t o t y p e s                            */
127 /*------------------------------------------------------------------*/
128
129 gboolean mono_arch_handle_exception (void     *ctx,
130                                      gpointer obj, 
131                                      gboolean test_only);
132
133 /*========================= End of Prototypes ======================*/
134
135 /*------------------------------------------------------------------*/
136 /*                 G l o b a l   V a r i a b l e s                  */
137 /*------------------------------------------------------------------*/
138
139 #ifdef MONO_USE_EXC_TABLES
140
141 static framesf frame_state_for = NULL;
142
143 static gboolean inited = FALSE;
144
145 typedef char ** (*get_backtrace_symbols_type) (void *__const *__array, int __size);
146
147 static get_backtrace_symbols_type get_backtrace_symbols = NULL;
148
149 #endif
150
151 /*====================== End of Global Variables ===================*/
152
153 /*------------------------------------------------------------------*/
154 /*                                                                  */
155 /* Name         - context_get_ip                                    */
156 /*                                                                  */
157 /* Function     - Extract the current instruction address from the  */
158 /*                context.                                          */
159 /*                                                                  */
160 /*------------------------------------------------------------------*/
161
162 static inline gpointer 
163 context_get_ip (MonoContext *ctx) 
164 {
165         gpointer ip;
166
167         ip = (gpointer) ((gint32) (ctx->uc_mcontext.psw.addr) & 0x7fffffff);
168         return ip;
169 }
170
171 /*========================= End of Function ========================*/
172
173 #ifdef MONO_USE_EXC_TABLES
174
175 /*------------------------------------------------------------------*/
176 /*                                                                  */
177 /* Name         - init_frame_state_for                              */
178 /*                                                                  */
179 /* Function     - Load the __frame_state_for from libc.             */
180 /*                There are two versions of __frame_state_for: one  */ 
181 /*                in libgcc.a and the other in glibc.so. We need    */
182 /*                the version from glibc. For more information, see:*/
183 /*                http://gcc.gnu.org/ml/gcc/2002-08/msg00192.html   */
184 /*                                                                  */
185 /*------------------------------------------------------------------*/
186
187 static void
188 init_frame_state_for (void)
189 {
190         GModule *module;
191
192         if ((module = g_module_open ("libc.so.6", G_MODULE_BIND_LAZY))) {
193         
194                 if (!g_module_symbol (module, "__frame_state_for", (gpointer*)&frame_state_for))
195                         frame_state_for = NULL;
196
197                 if (!g_module_symbol (module, "backtrace_symbols", (gpointer*)&get_backtrace_symbols)) {
198                         get_backtrace_symbols = NULL;
199                         frame_state_for = NULL;
200                 }
201
202                 g_module_close (module);
203         }
204
205         inited = TRUE;
206 }
207
208 /*========================= End of Function ========================*/
209
210 #endif
211
212 /*------------------------------------------------------------------*/
213 /*                                                                  */
214 /* Name         - mono_arch_has_unwind_info                         */
215 /*                                                                  */
216 /* Function     - Tests if a function has a DWARF exception table   */
217 /*                that is able to restore all caller saved registers*/
218 /*                                                                  */
219 /*------------------------------------------------------------------*/
220
221 gboolean
222 mono_arch_has_unwind_info (gconstpointer addr)
223 {
224 #if 0
225         struct frame_state state_in;
226         struct frame_state *res;
227
228         if (!inited) 
229                 init_frame_state_for ();
230         
231         if (!frame_state_for)
232                 return FALSE;
233
234         g_assert (method->addr);
235
236         memset (&state_in, 0, sizeof (state_in));
237
238         /* offset 10 is just a guess, but it works for all methods tested */
239         if ((res = frame_state_for ((char *)method->addr + 10, &state_in))) {
240
241                 if (res->saved [X86_EBX] != 1 ||
242                     res->saved [X86_EDI] != 1 ||
243                     res->saved [X86_EBP] != 1 ||
244                     res->saved [X86_ESI] != 1) {
245                         return FALSE;
246                 }
247                 return TRUE;
248         } else {
249                 return FALSE;
250         }
251 #else
252         return FALSE;
253 #endif
254 }
255
256 /*========================= End of Function ========================*/
257
258 /*------------------------------------------------------------------*/
259 /*                                                                  */
260 /* Name         - s390_unwind_native_frame                          */
261 /*                                                                  */
262 /* Function     - Use the context to unwind a stack frame.          */
263 /*                                                                  */
264 /*------------------------------------------------------------------*/
265
266 static MonoJitInfo *
267 s390_unwind_native_frame (MonoDomain *domain, MonoJitTlsData *jit_tls, 
268                           struct sigcontext *ctx, struct sigcontext *new_ctx, 
269                           MonoLMF *lmf, char **trace)
270 {
271 #if 0
272         struct stack_frame *frame;
273         gpointer max_stack;
274         MonoJitInfo *ji;
275         struct frame_state state_in;
276         struct frame_state *res;
277
278         if (trace)
279                 *trace = NULL;
280
281         if (!inited) 
282                 init_frame_state_for ();
283
284         if (!frame_state_for)
285                 return FALSE;
286
287         frame = MONO_CONTEXT_GET_BP (ctx);
288
289         max_stack = lmf && lmf->method ? lmf : jit_tls->end_of_stack;
290
291         *new_ctx = *ctx;
292
293         memset (&state_in, 0, sizeof (state_in));
294
295         while ((gpointer)frame->next < (gpointer)max_stack) {
296                 gpointer ip, addr = frame->return_address;
297                 void *cfa;
298                 char *tmp, **symbols;
299
300                 if (trace) {
301                         ip = MONO_CONTEXT_GET_IP (new_ctx);
302                         symbols = get_backtrace_symbols (&ip, 1);
303                         if (*trace)
304                                 tmp = g_strdup_printf ("%s\nin (unmanaged) %s", *trace, symbols [0]);
305                         else
306                                 tmp = g_strdup_printf ("in (unmanaged) %s", symbols [0]);
307
308                         free (symbols);
309                         g_free (*trace);
310                         *trace = tmp;
311                 }
312
313                 if ((res = frame_state_for (addr, &state_in))) {        
314                         int i;
315
316                         cfa = (gint8*) (get_sigcontext_reg (new_ctx, res->cfa_reg) + res->cfa_offset);
317                         frame = (struct stack_frame *)((gint8*)cfa - 8);
318                         for (i = 0; i < DWARF_FRAME_REGISTERS + 1; i++) {
319                                 int how = res->saved[i];
320                                 long val;
321                                 g_assert ((how == 0) || (how == 1));
322                         
323                                 if (how == 1) {
324                                         val = * (long*) ((gint8*)cfa + res->reg_or_offset[i]);
325                                         set_sigcontext_reg (new_ctx, i, val);
326                                 }
327                         }
328                         new_ctx->SC_ESP = (long)cfa;
329
330                         if (res->saved [X86_EBX] == 1 &&
331                             res->saved [X86_EDI] == 1 &&
332                             res->saved [X86_EBP] == 1 &&
333                             res->saved [X86_ESI] == 1 &&
334                             (ji = mono_jit_info_table_find (domain, frame->return_address))) {
335                                 //printf ("FRAME CFA %s\n", mono_method_full_name (ji->method, TRUE));
336                                 return ji;
337                         }
338
339                 } else {
340 //                      printf ("FRAME %p %p %p\n", 
341 //                              frame, MONO_CONTEXT_GET_IP (new_ctx), 
342 //                              mono_jit_info_table_find (domain, MONO_CONTEXT_GET_IP (new_ctx)));
343
344                         MONO_CONTEXT_SET_IP (new_ctx, frame->return_address);
345                         frame = frame->next;
346                         MONO_CONTEXT_SET_BP (new_ctx, frame);
347
348                         /* stop if !frame or when we detect an unexpected managed frame */
349                         if (!frame || mono_jit_info_table_find (domain, frame->return_address)) {
350                                 if (trace) {
351                                         g_free (*trace);
352                                         *trace = NULL;
353                                 }
354                                 return NULL;
355                         }
356                 }
357         }
358
359         if (trace) {
360                 g_free (*trace);
361                 *trace = NULL;
362         }
363 #endif
364         return NULL;
365 }
366
367 /*========================= End of Function ========================*/
368
369 /*------------------------------------------------------------------*/
370 /*                                                                  */
371 /* Name         - arch_get_call_filter                              */
372 /*                                                                  */
373 /* Function     - Return a pointer to a method which calls an       */
374 /*                exception filter. We also use this function to    */
375 /*                call finally handlers (we pass NULL as @exc       */
376 /*                object in this case).                             */
377 /*                                                                  */
378 /*------------------------------------------------------------------*/
379
380 static gpointer
381 arch_get_call_filter (void)
382 {
383         static guint8 start [256];
384         static int inited = 0;
385         guint8 *code;
386         int alloc_size, pos, i;
387
388         if (inited)
389                 return start;
390
391         inited = 1;
392         /* call_filter (MonoContext *ctx, unsigned long eip, gpointer exc) */
393         code = start;
394
395         s390_stm (code, s390_r6, s390_r14, STK_BASE, S390_REG_SAVE_OFFSET);
396         s390_lr  (code, s390_r14, STK_BASE);
397         alloc_size = S390_ALIGN(S390_CALLFILTER_SIZE, S390_STACK_ALIGNMENT);
398         s390_ahi (code, STK_BASE, -alloc_size);
399         s390_st  (code, s390_r14, 0, STK_BASE, 0);
400
401         /*------------------------------------------------------*/
402         /* save general registers on stack                      */
403         /*------------------------------------------------------*/
404         s390_stm (code, s390_r0, s390_r13, STK_BASE, S390_CALLFILTER_INTREGS);
405
406         /*------------------------------------------------------*/
407         /* save floating point registers on stack               */
408         /*------------------------------------------------------*/
409         s390_stm (code, s390_r0, s390_r13, STK_BASE, S390_CALLFILTER_INTREGS);
410         pos = S390_CALLFILTER_FLTREGS;
411         for (i = 0; i < 16; ++i) {
412                 s390_std (code, i, 0, STK_BASE, pos);
413                 pos += sizeof (gdouble);
414         }
415
416         /*------------------------------------------------------*/
417         /* save access registers on stack                       */
418         /*------------------------------------------------------*/
419         s390_stam (code, s390_r0, s390_r15, STK_BASE, S390_CALLFILTER_ACCREGS);
420
421         s390_lr   (code, s390_r13, s390_r2);
422         s390_lr   (code, s390_r14, s390_r3);
423         s390_lr   (code, s390_r2, s390_r4);
424
425         s390_lm   (code, s390_r0, s390_r1, s390_r13, G_STRUCT_OFFSET(MonoContext, uc_mcontext.gregs));
426         s390_lm   (code, s390_r3, s390_r12, s390_r13, G_STRUCT_OFFSET(MonoContext, uc_mcontext.gregs[2]));
427         pos = G_STRUCT_OFFSET(MonoContext, uc_mcontext.fpregs.fprs[0]);
428         for (i = 0; i < 16; ++i) {
429                 s390_ld  (code, i, 0, s390_r13, pos);
430                 pos += sizeof(gdouble);
431         }
432
433         s390_basr (code, s390_r14, s390_r14);
434
435         /* restore all the regs from the stack */
436         s390_lm (code, s390_r0, s390_r13, STK_BASE, S390_CALLFILTER_INTREGS);
437         pos = S390_CALLFILTER_FLTREGS;
438         for (i = 0; i < 16; ++i) {
439                 s390_ld (code, i, 0, STK_BASE, pos);
440                 pos += sizeof (gdouble);
441         }
442
443         s390_lam  (code, s390_r0, s390_r15, STK_BASE, S390_CALLFILTER_ACCREGS);
444         s390_ahi  (code, s390_r15, alloc_size);
445         s390_lm   (code, s390_r6, s390_r14, STK_BASE, S390_REG_SAVE_OFFSET);
446         s390_br   (code, s390_r14);
447
448         g_assert ((code - start) < sizeof(start));
449         return start;
450 }
451
452 /*========================= End of Function ========================*/
453
454 /*------------------------------------------------------------------*/
455 /*                                                                  */
456 /* Name         - arch_get_restore_context                          */
457 /*                                                                  */
458 /* Function     - Return a pointer to a method which restores a     */
459 /*                previously saved context.                         */
460 /*                                                                  */
461 /*------------------------------------------------------------------*/
462
463 static void
464 throw_exception (MonoObject *exc, unsigned long ip, unsigned long sp, 
465                  gulong *int_regs, gdouble *fp_regs, gulong *acc_regs, guint fpc)
466 {
467         static void (*restore_context) (MonoContext *);
468         MonoContext ctx;
469         int iReg;
470         
471         memset(&ctx, 0, sizeof(ctx));
472
473         getcontext(&ctx);
474
475         /* adjust eip so that it point into the call instruction */
476         ip -= 6;
477
478         for (iReg = 0; iReg < 16; iReg++) {
479                 ctx.uc_mcontext.gregs[iReg]         = int_regs[iReg];
480                 ctx.uc_mcontext.fpregs.fprs[iReg].d = fp_regs[iReg];
481                 ctx.uc_mcontext.aregs[iReg]         = acc_regs[iReg];
482         }
483
484         ctx.uc_mcontext.fpregs.fpc = fpc;
485
486         MONO_CONTEXT_SET_BP (&ctx, sp);
487         MONO_CONTEXT_SET_IP (&ctx, ip);
488         
489         mono_arch_handle_exception (&ctx, exc, FALSE);
490         setcontext(&ctx);
491
492         g_assert_not_reached ();
493 }
494
495 /*========================= End of Function ========================*/
496
497 /*------------------------------------------------------------------*/
498 /*                                                                  */
499 /* Name         - arch_get_throw_exception_generic                  */
500 /*                                                                  */
501 /* Function     - Return a function pointer which can be used to    */
502 /*                raise exceptions. The returned function has the   */
503 /*                following signature:                              */
504 /*                void (*func) (MonoException *exc); or,            */
505 /*                void (*func) (char *exc_name);                    */
506 /*                                                                  */
507 /*------------------------------------------------------------------*/
508
509 static gpointer 
510 mono_arch_get_throw_exception_generic (guint8 *start, int size, int by_name)
511 {
512         guint8 *code;
513         int alloc_size, pos, i, offset;
514
515         code = start;
516
517         s390_stm (code, s390_r6, s390_r14, STK_BASE, S390_REG_SAVE_OFFSET);
518         alloc_size = S390_ALIGN(S390_THROWSTACK_SIZE, S390_STACK_ALIGNMENT);
519         s390_lr   (code, s390_r14, STK_BASE);
520         s390_ahi  (code, STK_BASE, -alloc_size);
521         s390_st   (code, s390_r14, 0, STK_BASE, 0);
522         if (by_name) {
523                 s390_lr   (code, s390_r4, s390_r2);
524                 s390_bras (code, s390_r13, 6);
525                 s390_word (code, mono_defaults.corlib);
526                 s390_word (code, "System");
527                 s390_l    (code, s390_r2, 0, s390_r13, 0);
528                 s390_l    (code, s390_r3, 0, s390_r13, 4);
529                 offset = (guint32) S390_RELATIVE(mono_exception_from_name, code);
530                 s390_brasl(code, s390_r14, offset);
531         }
532         /*------------------------------------------------------*/
533         /* save the general registers on the stack              */
534         /*------------------------------------------------------*/
535         s390_stm (code, s390_r0, s390_r13, STK_BASE, S390_THROWSTACK_INTREGS);
536
537         s390_lr  (code, s390_r1, STK_BASE);
538         s390_ahi (code, s390_r1, alloc_size);
539         /*------------------------------------------------------*/
540         /* save the return address in the parameter register    */
541         /*------------------------------------------------------*/
542         s390_l   (code, s390_r3, 0, s390_r1, S390_RET_ADDR_OFFSET);
543
544         /*------------------------------------------------------*/
545         /* save the floating point registers                    */
546         /*------------------------------------------------------*/
547         pos = S390_THROWSTACK_FLTREGS;
548         for (i = 0; i < 16; ++i) {
549                 s390_std (code, i, 0,STK_BASE, pos);
550                 pos += sizeof (gdouble);
551         }
552         /*------------------------------------------------------*/
553         /* save the access registers                            */
554         /*------------------------------------------------------*/
555         s390_stam (code, s390_r0, s390_r15, STK_BASE, S390_THROWSTACK_ACCREGS);
556
557         /*------------------------------------------------------*/
558         /* call throw_exception (exc, ip, sp, gr, fr, ar)       */
559         /* exc is already in place in r2                        */
560         /*------------------------------------------------------*/
561         s390_lr   (code, s390_r4, s390_r1);        /* caller sp */
562         /*------------------------------------------------------*/
563         /* pointer to the saved int regs                        */
564         /*------------------------------------------------------*/
565         s390_la   (code, s390_r5, 0, STK_BASE, S390_THROWSTACK_INTREGS);
566         s390_la   (code, s390_r6, 0, STK_BASE, S390_THROWSTACK_FLTREGS);
567         s390_la   (code, s390_r7, 0, STK_BASE, S390_THROWSTACK_ACCREGS);
568         s390_st   (code, s390_r7, 0, STK_BASE, S390_THROWSTACK_ACCPRM);
569         s390_stfpc(code, STK_BASE, S390_THROWSTACK_FPCPRM);
570         offset = (guint32) S390_RELATIVE(throw_exception, code);
571         s390_brasl(code, s390_r14, offset);
572         /* we should never reach this breakpoint */
573         s390_break (code);
574         g_assert ((code - start) < size);
575         return start;
576 }
577
578 /*========================= End of Function ========================*/
579
580 /*------------------------------------------------------------------*/
581 /*                                                                  */
582 /* Name         - arch_get_throw_exception                          */
583 /*                                                                  */
584 /* Function     - Return a function pointer which can be used to    */
585 /*                raise exceptions. The returned function has the   */
586 /*                following signature:                              */
587 /*                void (*func) (MonoException *exc);                */
588 /*                                                                  */
589 /*------------------------------------------------------------------*/
590
591 gpointer 
592 mono_arch_get_throw_exception (void)
593 {
594         static guint8 start [128];
595         static int inited = 0;
596
597         if (inited)
598                 return start;
599         mono_arch_get_throw_exception_generic (start, sizeof (start), FALSE);
600         inited = 1;
601         return start;
602 }
603
604 /*========================= End of Function ========================*/
605
606 /*------------------------------------------------------------------*/
607 /*                                                                  */
608 /* Name         - arch_get_throw_exception_by_name                  */
609 /*                                                                  */
610 /* Function     - Return a function pointer which can be used to    */
611 /*                raise corlib exceptions. The return function has  */
612 /*                the following signature:                          */
613 /*                void (*func) (char *exc_name);                    */
614 /*                                                                  */
615 /*------------------------------------------------------------------*/
616
617 gpointer 
618 mono_arch_get_throw_exception_by_name (void)
619 {
620         static guint8 start [160];
621         static int inited = 0;
622
623         if (inited)
624                 return start;
625         mono_arch_get_throw_exception_generic (start, sizeof (start), TRUE);
626         inited = 1;
627         return start;
628 }       
629
630 /*========================= End of Function ========================*/
631
632 /*------------------------------------------------------------------*/
633 /*                                                                  */
634 /* Name         - glist_to_array                                    */
635 /*                                                                  */
636 /* Function     - Convert a list to a mono array.                   */
637 /*                                                                  */
638 /*------------------------------------------------------------------*/
639
640 static MonoArray *
641 glist_to_array (GList *list) 
642 {
643         MonoDomain *domain = mono_domain_get ();
644         MonoArray *res;
645         int len, i;
646
647         if (!list)
648                 return NULL;
649
650         len = g_list_length (list);
651         res = mono_array_new (domain, mono_defaults.int_class, len);
652
653         for (i = 0; list; list = list->next, i++)
654                 mono_array_set (res, gpointer, i, list->data);
655
656         return res;
657 }
658
659 /*========================= End of Function ========================*/
660
661 /*------------------------------------------------------------------*/
662 /*                                                                  */
663 /* Name         - mono_arch_find_jit_info                           */
664 /*                                                                  */
665 /* Function     - This function is used to gather informatoin from  */
666 /*                @ctx. It returns the MonoJitInfo of the corres-   */
667 /*                ponding function, unwinds one stack frame and     */
668 /*                stores the resulting context into @new_ctx. It    */
669 /*                also stores a string describing the stack location*/
670 /*                into @trace (if not NULL), and modifies the @lmf  */
671 /*                if necessary. @native_offset returns the IP off-  */
672 /*                set from the start of the function or -1 if that  */
673 /*                informatoin is not available.                     */
674 /*                                                                  */
675 /*------------------------------------------------------------------*/
676
677 MonoJitInfo *
678 mono_arch_find_jit_info (MonoDomain *domain, MonoJitTlsData *jit_tls, 
679                          MonoJitInfo *res, MonoJitInfo *prev_ji, MonoContext *ctx, 
680                          MonoContext *new_ctx, char **trace, MonoLMF **lmf, 
681                          int *native_offset, gboolean *managed)
682 {
683         MonoJitInfo *ji;
684         gpointer ip = MONO_CONTEXT_GET_IP (ctx);
685         unsigned long *ptr;
686         char *p;
687         MonoS390StackFrame *sframe;
688
689         if (prev_ji && 
690             (ip > prev_ji->code_start && 
691             ((guint8 *) ip < ((guint8 *) prev_ji->code_start) + prev_ji->code_size)))
692                 ji = prev_ji;
693         else
694                 ji = mono_jit_info_table_find (domain, ip);
695
696         if (trace)
697                 *trace = NULL;
698
699         if (native_offset)
700                 *native_offset = -1;
701
702         if (managed)
703                 *managed = FALSE;
704
705         if (ji != NULL) {
706                 char *source_location, *tmpaddr, *fname;
707                 gint32 address, iloffset;
708                 int offset;
709
710                 *new_ctx = *ctx;
711
712                 if (*lmf && (MONO_CONTEXT_GET_BP (ctx) >= (gpointer)(*lmf)->ebp)) {
713                         /* remove any unused lmf */
714                         *lmf = (*lmf)->previous_lmf;
715                 }
716
717                 address = (char *)ip - (char *)ji->code_start;
718
719                 if (native_offset)
720                         *native_offset = address;
721
722                 if (managed)
723                         if (!ji->method->wrapper_type)
724                                 *managed = TRUE;
725
726                 if (trace) {
727                         source_location = mono_debug_source_location_from_address (ji->method, address, NULL, domain);
728                         iloffset = mono_debug_il_offset_from_address (ji->method, address, domain);
729
730                         if (iloffset < 0)
731                                 tmpaddr = g_strdup_printf ("<0x%05x>", address);
732                         else
733                                 tmpaddr = g_strdup_printf ("[0x%05x]", iloffset);
734                 
735                         fname = mono_method_full_name (ji->method, TRUE);
736
737                         if (source_location)
738                                 *trace = g_strdup_printf ("in %s (at %s) %s", tmpaddr, source_location, fname);
739                         else
740                                 *trace = g_strdup_printf ("in %s %s", tmpaddr, fname);
741
742                         g_free (fname);
743                         g_free (source_location);
744                         g_free (tmpaddr);
745                 }
746                 sframe = (MonoS390StackFrame *) MONO_CONTEXT_GET_BP (ctx);
747                 MONO_CONTEXT_SET_BP (new_ctx, sframe->prev);
748                 MONO_CONTEXT_SET_IP (new_ctx, sframe->return_address);
749                 *res = *ji;
750                 return res;
751 #ifdef MONO_USE_EXC_TABLES
752         } else if ((ji = s390_unwind_native_frame (domain, jit_tls, ctx, new_ctx, *lmf, trace))) {
753                 *res = *ji;
754                 return res;
755 #endif
756         } else if (*lmf) {
757                 
758                 *new_ctx = *ctx;
759
760                 if (!(*lmf)->method)
761                         return (gpointer)-1;
762
763                 if (trace)
764                         *trace = g_strdup_printf ("in (unmanaged) %s", mono_method_full_name ((*lmf)->method, TRUE));
765                 
766                 if ((ji = mono_jit_info_table_find (domain, (gpointer)(*lmf)->eip))) {
767                         *res = *ji;
768                 } else {
769                         memset (res, 0, sizeof (MonoJitInfo));
770                         res->method = (*lmf)->method;
771                 }
772
773 /*
774                 MONO_CONTEXT_SET_BP (ctx, (*lmf)->ebp);
775                 MONO_CONTEXT_SET_IP (ctx, (*lmf)->eip);
776 */
777                 MONO_CONTEXT_SET_BP (new_ctx, (*lmf)->ebp);
778                 MONO_CONTEXT_SET_IP (new_ctx, (*lmf)->eip);
779                 *lmf = (*lmf)->previous_lmf;
780
781                 return res;
782                 
783         }
784
785         return NULL;
786 }
787
788 /*========================= End of Function ========================*/
789
790 /*------------------------------------------------------------------*/
791 /*                                                                  */
792 /* Name         - ves_icall_get_trace                               */
793 /*                                                                  */
794 /* Function     -                                                   */
795 /*                                                                  */
796 /*------------------------------------------------------------------*/
797
798 MonoArray *
799 ves_icall_get_trace (MonoException *exc, gint32 skip, MonoBoolean need_file_info)
800 {
801         MonoDomain *domain = mono_domain_get ();
802         MonoArray *res;
803         MonoArray *ta = exc->trace_ips;
804         int i, len;
805         
806         len = mono_array_length (ta);
807
808         res = mono_array_new (domain, mono_defaults.stack_frame_class, len > skip ? len - skip : 0);
809
810         for (i = skip; i < len; i++) {
811                 MonoJitInfo *ji;
812                 MonoStackFrame *sf = (MonoStackFrame *)mono_object_new (domain, mono_defaults.stack_frame_class);
813                 gpointer ip = mono_array_get (ta, gpointer, i);
814
815                 ji = mono_jit_info_table_find (domain, ip);
816                 g_assert (ji != NULL);
817
818                 sf->method = mono_method_get_object (domain, ji->method, NULL);
819                 sf->native_offset = (char *)ip - (char *)ji->code_start;
820
821                 sf->il_offset = mono_debug_il_offset_from_address (ji->method, sf->native_offset, domain);
822
823                 if (need_file_info) {
824                         gchar *filename;
825                         
826                         filename = mono_debug_source_location_from_address (ji->method, sf->native_offset, &sf->line, domain);
827
828                         sf->filename = filename? mono_string_new (domain, filename): NULL;
829                         sf->column = 0;
830
831                         g_free (filename);
832                 }
833
834                 mono_array_set (res, gpointer, i, sf);
835         }
836
837         return res;
838 }
839
840 /*========================= End of Function ========================*/
841
842 /*------------------------------------------------------------------*/
843 /*                                                                  */
844 /* Name         - mono_jit_walk_stack                               */
845 /*                                                                  */
846 /* Function     -                                                   */
847 /*                                                                  */
848 /*------------------------------------------------------------------*/
849
850 void
851 mono_jit_walk_stack (MonoStackWalk func, gpointer user_data) {
852         MonoDomain *domain = mono_domain_get ();
853         MonoJitTlsData *jit_tls = TlsGetValue (mono_jit_tls_id);
854         MonoLMF *lmf = jit_tls->lmf;
855         MonoJitInfo *ji, rji;
856         gint native_offset, il_offset;
857         gboolean managed;
858         MonoContext ctx, new_ctx;
859
860         MONO_CONTEXT_SET_IP (&ctx, __builtin_return_address (0));
861         MONO_CONTEXT_SET_BP (&ctx, __builtin_frame_address (1));
862
863         while (MONO_CONTEXT_GET_BP (&ctx) < jit_tls->end_of_stack) {
864                 
865                 ji = mono_arch_find_jit_info (domain, jit_tls, &rji, NULL, 
866                                               &ctx, &new_ctx, NULL, &lmf, 
867                                               &native_offset, &managed);
868                 g_assert (ji);
869
870                 if (ji == (gpointer)-1)
871                         return;
872
873                 il_offset = mono_debug_il_offset_from_address (ji->method, native_offset, domain);
874
875                 if (func (ji->method, native_offset, il_offset, managed, user_data))
876                         return;
877                 
878                 ctx = new_ctx;
879         }
880 }
881
882 /*========================= End of Function ========================*/
883
884 /*------------------------------------------------------------------*/
885 /*                                                                  */
886 /* Name         - ves_icall_get_frame_info                          */
887 /*                                                                  */
888 /* Function     -                                                   */
889 /*                                                                  */
890 /*------------------------------------------------------------------*/
891
892 MonoBoolean
893 ves_icall_get_frame_info (gint32 skip, MonoBoolean need_file_info, 
894                           MonoReflectionMethod **method, 
895                           gint32 *iloffset, gint32 *native_offset,
896                           MonoString **file, gint32 *line, gint32 *column)
897 {
898         MonoDomain *domain = mono_domain_get ();
899         MonoJitTlsData *jit_tls = TlsGetValue (mono_jit_tls_id);
900         MonoLMF *lmf = jit_tls->lmf;
901         MonoJitInfo *ji, rji;
902         MonoContext ctx, new_ctx;
903
904         MONO_CONTEXT_SET_IP (&ctx, ves_icall_get_frame_info);
905         MONO_CONTEXT_SET_BP (&ctx, __builtin_frame_address (0));
906
907         skip++;
908
909         do {
910                 ji = mono_arch_find_jit_info (domain, jit_tls, &rji, NULL, 
911                                               &ctx, &new_ctx, NULL, &lmf, 
912                                               native_offset, NULL);
913
914                 ctx = new_ctx;
915                 
916                 if (!ji || ji == (gpointer)-1 || MONO_CONTEXT_GET_BP (&ctx) >= jit_tls->end_of_stack)
917                         return FALSE;
918
919                 /* skip all wrappers ??*/
920                 if (ji->method->wrapper_type == MONO_WRAPPER_RUNTIME_INVOKE ||
921                     ji->method->wrapper_type == MONO_WRAPPER_REMOTING_INVOKE_WITH_CHECK ||
922                     ji->method->wrapper_type == MONO_WRAPPER_REMOTING_INVOKE)
923                         continue;
924
925                 skip--;
926
927         } while (skip >= 0);
928
929         *method = mono_method_get_object (domain, ji->method, NULL);
930         *iloffset = mono_debug_il_offset_from_address (ji->method, *native_offset, domain);
931
932         if (need_file_info) {
933                 gchar *filename;
934
935                 filename = mono_debug_source_location_from_address (ji->method, *native_offset, line, domain);
936
937                 *file = filename? mono_string_new (domain, filename): NULL;
938                 *column = 0;
939
940                 g_free (filename);
941         }
942
943         return TRUE;
944 }
945
946 /*========================= End of Function ========================*/
947
948 /*------------------------------------------------------------------*/
949 /*                                                                  */
950 /* Name         - mono_arch_handle_exception                        */
951 /*                                                                  */
952 /* Function     - Handle an exception raised by the JIT code.       */
953 /*                                                                  */
954 /* Parameters   - ctx       - Saved processor state                 */
955 /*                obj       - The exception object                  */
956 /*                test_only - Only test if the exception is caught, */
957 /*                            but don't call handlers               */
958 /*                                                                  */
959 /*------------------------------------------------------------------*/
960
961 gboolean
962 mono_arch_handle_exception (void *uc, gpointer obj, gboolean test_only)
963 {
964         MonoContext     *ctx = uc;
965         MonoDomain      *domain = mono_domain_get ();
966         MonoJitInfo     *ji, rji;
967         static int      (*call_filter) (MonoContext *, gpointer, gpointer) = NULL;
968         MonoJitTlsData  *jit_tls = TlsGetValue (mono_jit_tls_id);
969         MonoLMF         *lmf = jit_tls->lmf;            
970         GList           *trace_ips = NULL;
971         MonoException   *mono_ex;
972
973         g_assert (ctx != NULL);
974         memset(&rji, 0, sizeof(rji));
975         if (!obj) {
976                 MonoException *ex = mono_get_exception_null_reference ();
977                 ex->message = mono_string_new (domain, 
978                         "Object reference not set to an instance of an object");
979                 obj = (MonoObject *)ex;
980         } 
981
982         if (mono_object_isinst (obj, mono_defaults.exception_class)) {
983                 mono_ex = (MonoException*)obj;
984                 mono_ex->stack_trace = NULL;
985         } else {
986                 mono_ex = NULL;
987         }
988
989
990         if (!call_filter)
991                 call_filter = arch_get_call_filter ();
992
993         g_assert (jit_tls->end_of_stack);
994         g_assert (jit_tls->abort_func);
995
996         if (!test_only) {
997                 MonoContext ctx_cp = *ctx;
998                 if (mono_jit_trace_calls != NULL)
999                         g_print ("EXCEPTION handling: %s\n", mono_object_class (obj)->name);
1000                 if (!mono_arch_handle_exception (&ctx_cp, obj, TRUE)) {
1001                         if (mono_break_on_exc)
1002                                 G_BREAKPOINT ();
1003                         mono_unhandled_exception (obj);
1004                 }
1005         }
1006
1007         while (1) {
1008                 MonoContext new_ctx;
1009                 char *trace = NULL;
1010                 
1011                 ji = mono_arch_find_jit_info (domain, jit_tls, &rji, &rji, ctx, &new_ctx, 
1012                                               test_only ? &trace : NULL, &lmf, NULL, NULL);
1013                 if (!ji) {
1014                         g_warning ("Exception inside function without unwind info");
1015                         g_assert_not_reached ();
1016                 }
1017
1018                 if (ji != (gpointer)-1) {
1019                         
1020                         if (test_only && ji->method->wrapper_type != MONO_WRAPPER_RUNTIME_INVOKE && mono_ex) {
1021                                 char *tmp, *strace;
1022
1023                                 trace_ips = g_list_append (trace_ips, MONO_CONTEXT_GET_IP (ctx));
1024
1025                                 if (!mono_ex->stack_trace)
1026                                         strace = g_strdup ("");
1027                                 else
1028                                         strace = mono_string_to_utf8 (mono_ex->stack_trace);
1029                         
1030                                 tmp = g_strdup_printf ("%s%s\n", strace, trace);
1031                                 g_free (strace);
1032
1033 printf("%s\n",tmp);
1034                                 mono_ex->stack_trace = mono_string_new (domain, tmp);
1035
1036                                 g_free (tmp);
1037                         }
1038
1039                         if (ji->num_clauses) {
1040                                 int i;
1041                                 
1042                                 g_assert (ji->clauses);
1043                         
1044                                 for (i = 0; i < ji->num_clauses; i++) {
1045                                         MonoJitExceptionInfo *ei = &ji->clauses [i];
1046
1047                                         if (ei->try_start <= MONO_CONTEXT_GET_IP (ctx) && 
1048                                             MONO_CONTEXT_GET_IP (ctx) <= ei->try_end) { 
1049                                                 /* catch block */
1050                                                 if ((ei->flags == MONO_EXCEPTION_CLAUSE_NONE && 
1051                                                      mono_object_isinst (obj, mono_class_get (ji->method->klass->image, ei->data.token))) ||
1052                                                     ((ei->flags == MONO_EXCEPTION_CLAUSE_FILTER &&
1053                                                       call_filter (ctx, ei->data.filter, obj)))) {
1054                                                         if (test_only) {
1055                                                                 if (mono_ex)
1056                                                                         mono_ex->trace_ips = glist_to_array (trace_ips);
1057                                                                 g_list_free (trace_ips);
1058                                                                 g_free (trace);
1059                                                                 return TRUE;
1060                                                         }
1061                                                         if (mono_jit_trace_calls != NULL)
1062                                                                 g_print ("EXCEPTION: catch found at clause %d of %s\n", i, mono_method_full_name (ji->method, TRUE));
1063                                                         MONO_CONTEXT_SET_IP (ctx, ei->handler_start);
1064                                                         *((gpointer *)((char *)MONO_CONTEXT_GET_BP (ctx) + ji->exvar_offset)) = obj;
1065                                                         jit_tls->lmf = lmf;
1066                                                         g_free (trace);
1067                                                         return 0;
1068                                                 }
1069                                                 if (!test_only && ei->try_start <= MONO_CONTEXT_GET_IP (ctx) && 
1070                                                     MONO_CONTEXT_GET_IP (ctx) < ei->try_end &&
1071                                                     (ei->flags & MONO_EXCEPTION_CLAUSE_FINALLY)) {
1072                                                         if (mono_jit_trace_calls != NULL)
1073                                                                 g_print ("EXCEPTION: finally clause %d of %s\n", i, mono_method_full_name (ji->method, TRUE));
1074                                                         call_filter (ctx, ei->handler_start, NULL);
1075                                                 }
1076                                                 
1077                                         }
1078                                 }
1079                         }
1080                 }
1081
1082                 g_free (trace);
1083                         
1084                 *ctx = new_ctx;
1085
1086                 if ((ji == (gpointer)-1) || MONO_CONTEXT_GET_BP (ctx) >= jit_tls->end_of_stack) {
1087                         if (!test_only) {
1088                                 jit_tls->lmf = lmf;
1089                                 jit_tls->abort_func (obj);
1090                                 g_assert_not_reached ();
1091                         } else {
1092                                 if (mono_ex)
1093                                         mono_ex->trace_ips = glist_to_array (trace_ips);
1094                                 g_list_free (trace_ips);
1095                                 return FALSE;
1096                         }
1097                 }
1098         }
1099
1100         g_assert_not_reached ();
1101 }
1102
1103 /*========================= End of Function ========================*/
1104
1105 gpointer
1106 mono_arch_ip_from_context (void *sigctx)
1107 {
1108         return context_get_ip (sigctx);
1109 }
1110