This commit was manufactured by cvs2svn to create branch 'mono-1-0'.
[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                 memcpy(new_ctx, ctx, sizeof(*new_ctx));
712
713                 if (*lmf && (MONO_CONTEXT_GET_BP (ctx) >= (gpointer)(*lmf)->ebp)) {
714                         /* remove any unused lmf */
715                         *lmf = (*lmf)->previous_lmf;
716                 }
717
718                 address = (char *)ip - (char *)ji->code_start;
719
720                 if (native_offset)
721                         *native_offset = address;
722
723                 if (managed)
724                         if (!ji->method->wrapper_type)
725                                 *managed = TRUE;
726
727                 if (trace) {
728                         source_location = mono_debug_source_location_from_address (ji->method, address, NULL, domain);
729                         iloffset = mono_debug_il_offset_from_address (ji->method, address, domain);
730
731                         if (iloffset < 0)
732                                 tmpaddr = g_strdup_printf ("<0x%08x>", address);
733                         else
734                                 tmpaddr = g_strdup_printf ("[0x%08x]", iloffset);
735                 
736                         fname = mono_method_full_name (ji->method, TRUE);
737
738                         if (source_location)
739                                 *trace = g_strdup_printf ("in %s (at %s) %s", tmpaddr, source_location, fname);
740                         else
741                                 *trace = g_strdup_printf ("in %s %s", tmpaddr, fname);
742
743                         g_free (fname);
744                         g_free (source_location);
745                         g_free (tmpaddr);
746                 }
747                 sframe = (MonoS390StackFrame *) MONO_CONTEXT_GET_BP (ctx);
748                 if (ji->used_regs) {
749                 }
750                 MONO_CONTEXT_SET_BP (new_ctx, sframe->prev);
751                 sframe = (MonoS390StackFrame *) sframe->prev;
752                 MONO_CONTEXT_SET_IP (new_ctx, sframe->return_address);
753                 *res = *ji;
754                 return res;
755 #ifdef MONO_USE_EXC_TABLES
756         } else if ((ji = s390_unwind_native_frame (domain, jit_tls, ctx, new_ctx, *lmf, trace))) {
757                 *res = *ji;
758                 return res;
759 #endif
760         } else if (*lmf) {
761                 
762                 *new_ctx = *ctx;
763
764                 if (!(*lmf)->method)
765                         return (gpointer)-1;
766
767                 if (trace)
768                         *trace = g_strdup_printf ("in (unmanaged) %s", mono_method_full_name ((*lmf)->method, TRUE));
769                 
770                 if ((ji = mono_jit_info_table_find (domain, (gpointer)(*lmf)->eip))) {
771                         *res = *ji;
772                 } else {
773                         memset (res, 0, sizeof (MonoJitInfo));
774                         res->method = (*lmf)->method;
775                 }
776
777 /*
778                 MONO_CONTEXT_SET_BP (ctx, (*lmf)->ebp);
779                 MONO_CONTEXT_SET_IP (ctx, (*lmf)->eip);
780 */
781                 memcpy(new_ctx->uc_mcontext.gregs, (*lmf)->gregs, sizeof((*lmf)->gregs));
782                 memcpy(new_ctx->uc_mcontext.fpregs.fprs, (*lmf)->fregs, sizeof((*lmf)->fregs));
783                 MONO_CONTEXT_SET_BP (new_ctx, (*lmf)->ebp);
784                 MONO_CONTEXT_SET_IP (new_ctx, (*lmf)->eip);
785                 *lmf = (*lmf)->previous_lmf;
786
787                 return res;
788                 
789         }
790
791         return NULL;
792 }
793
794 /*========================= End of Function ========================*/
795
796 /*------------------------------------------------------------------*/
797 /*                                                                  */
798 /* Name         - ves_icall_get_trace                               */
799 /*                                                                  */
800 /* Function     -                                                   */
801 /*                                                                  */
802 /*------------------------------------------------------------------*/
803
804 MonoArray *
805 ves_icall_get_trace (MonoException *exc, gint32 skip, MonoBoolean need_file_info)
806 {
807         MonoDomain *domain = mono_domain_get ();
808         MonoArray *res;
809         MonoArray *ta = exc->trace_ips;
810         int i, len;
811         
812         len = mono_array_length (ta);
813
814         res = mono_array_new (domain, mono_defaults.stack_frame_class, len > skip ? len - skip : 0);
815
816         for (i = skip; i < len; i++) {
817                 MonoJitInfo *ji;
818                 MonoStackFrame *sf = (MonoStackFrame *)mono_object_new (domain, mono_defaults.stack_frame_class);
819                 gpointer ip = mono_array_get (ta, gpointer, i);
820
821                 ji = mono_jit_info_table_find (domain, ip);
822                 g_assert (ji != NULL);
823
824                 sf->method = mono_method_get_object (domain, ji->method, NULL);
825                 sf->native_offset = (char *)ip - (char *)ji->code_start;
826
827                 sf->il_offset = mono_debug_il_offset_from_address (ji->method, sf->native_offset, domain);
828
829                 if (need_file_info) {
830                         gchar *filename;
831                         
832                         filename = mono_debug_source_location_from_address (ji->method, sf->native_offset, &sf->line, domain);
833
834                         sf->filename = filename? mono_string_new (domain, filename): NULL;
835                         sf->column = 0;
836
837                         g_free (filename);
838                 }
839
840                 mono_array_set (res, gpointer, i, sf);
841         }
842
843         return res;
844 }
845
846 /*========================= End of Function ========================*/
847
848 /*------------------------------------------------------------------*/
849 /*                                                                  */
850 /* Name         - mono_jit_walk_stack                               */
851 /*                                                                  */
852 /* Function     -                                                   */
853 /*                                                                  */
854 /*------------------------------------------------------------------*/
855
856 void
857 mono_jit_walk_stack (MonoStackWalk func, gpointer user_data) {
858         MonoDomain *domain = mono_domain_get ();
859         MonoJitTlsData *jit_tls = TlsGetValue (mono_jit_tls_id);
860         MonoLMF *lmf = jit_tls->lmf;
861         MonoJitInfo *ji, rji;
862         gint native_offset, il_offset;
863         gboolean managed;
864         MonoContext ctx, new_ctx;
865
866         MONO_CONTEXT_SET_IP (&ctx, __builtin_return_address (0));
867         MONO_CONTEXT_SET_BP (&ctx, __builtin_frame_address (1));
868
869         while (MONO_CONTEXT_GET_BP (&ctx) < jit_tls->end_of_stack) {
870                 
871                 ji = mono_arch_find_jit_info (domain, jit_tls, &rji, NULL, 
872                                               &ctx, &new_ctx, NULL, &lmf, 
873                                               &native_offset, &managed);
874                 g_assert (ji);
875
876                 if (ji == (gpointer)-1)
877                         return;
878
879                 il_offset = mono_debug_il_offset_from_address (ji->method, native_offset, domain);
880
881                 if (func (ji->method, native_offset, il_offset, managed, user_data))
882                         return;
883                 
884                 ctx = new_ctx;
885         }
886 }
887
888 /*========================= End of Function ========================*/
889
890 /*------------------------------------------------------------------*/
891 /*                                                                  */
892 /* Name         - ves_icall_get_frame_info                          */
893 /*                                                                  */
894 /* Function     -                                                   */
895 /*                                                                  */
896 /*------------------------------------------------------------------*/
897
898 MonoBoolean
899 ves_icall_get_frame_info (gint32 skip, MonoBoolean need_file_info, 
900                           MonoReflectionMethod **method, 
901                           gint32 *iloffset, gint32 *native_offset,
902                           MonoString **file, gint32 *line, gint32 *column)
903 {
904         MonoDomain *domain = mono_domain_get ();
905         MonoJitTlsData *jit_tls = TlsGetValue (mono_jit_tls_id);
906         MonoLMF *lmf = jit_tls->lmf;
907         MonoJitInfo *ji, rji;
908         MonoContext ctx, new_ctx;
909
910         MONO_CONTEXT_SET_IP (&ctx, ves_icall_get_frame_info);
911         MONO_CONTEXT_SET_BP (&ctx, __builtin_frame_address (0));
912
913         skip++;
914
915         do {
916                 ji = mono_arch_find_jit_info (domain, jit_tls, &rji, NULL, 
917                                               &ctx, &new_ctx, NULL, &lmf, 
918                                               native_offset, NULL);
919
920                 ctx = new_ctx;
921                 
922                 if (!ji || ji == (gpointer)-1 || MONO_CONTEXT_GET_BP (&ctx) >= jit_tls->end_of_stack)
923                         return FALSE;
924
925                 /* skip all wrappers ??*/
926                 if (ji->method->wrapper_type == MONO_WRAPPER_RUNTIME_INVOKE ||
927                     ji->method->wrapper_type == MONO_WRAPPER_REMOTING_INVOKE_WITH_CHECK ||
928                     ji->method->wrapper_type == MONO_WRAPPER_REMOTING_INVOKE)
929                         continue;
930
931                 skip--;
932
933         } while (skip >= 0);
934
935         *method = mono_method_get_object (domain, ji->method, NULL);
936         *iloffset = mono_debug_il_offset_from_address (ji->method, *native_offset, domain);
937
938         if (need_file_info) {
939                 gchar *filename;
940
941                 filename = mono_debug_source_location_from_address (ji->method, *native_offset, line, domain);
942
943                 *file = filename? mono_string_new (domain, filename): NULL;
944                 *column = 0;
945
946                 g_free (filename);
947         }
948
949         return TRUE;
950 }
951
952 /*========================= End of Function ========================*/
953
954 /*------------------------------------------------------------------*/
955 /*                                                                  */
956 /* Name         - mono_arch_handle_exception                        */
957 /*                                                                  */
958 /* Function     - Handle an exception raised by the JIT code.       */
959 /*                                                                  */
960 /* Parameters   - ctx       - Saved processor state                 */
961 /*                obj       - The exception object                  */
962 /*                test_only - Only test if the exception is caught, */
963 /*                            but don't call handlers               */
964 /*                                                                  */
965 /*------------------------------------------------------------------*/
966
967 gboolean
968 mono_arch_handle_exception (void *uc, gpointer obj, gboolean test_only)
969 {
970         MonoContext     *ctx = uc;
971         MonoDomain      *domain = mono_domain_get ();
972         MonoJitInfo     *ji, rji;
973         static int      (*call_filter) (MonoContext *, gpointer, gpointer) = NULL;
974         MonoJitTlsData  *jit_tls = TlsGetValue (mono_jit_tls_id);
975         MonoLMF         *lmf = jit_tls->lmf;            
976         GList           *trace_ips = NULL;
977         MonoException   *mono_ex;
978
979         g_assert (ctx != NULL);
980         memset(&rji, 0, sizeof(rji));
981         if (!obj) {
982                 MonoException *ex = mono_get_exception_null_reference ();
983                 ex->message = mono_string_new (domain, 
984                         "Object reference not set to an instance of an object");
985                 obj = (MonoObject *)ex;
986         } 
987
988         if (mono_object_isinst (obj, mono_defaults.exception_class)) {
989                 mono_ex = (MonoException*)obj;
990                 mono_ex->stack_trace = NULL;
991         } else {
992                 mono_ex = NULL;
993         }
994
995
996         if (!call_filter)
997                 call_filter = arch_get_call_filter ();
998
999         g_assert (jit_tls->end_of_stack);
1000         g_assert (jit_tls->abort_func);
1001
1002         if (!test_only) {
1003                 MonoContext ctx_cp = *ctx;
1004                 if (mono_jit_trace_calls != NULL)
1005                         g_print ("EXCEPTION handling: %s\n", mono_object_class (obj)->name);
1006                 if (!mono_arch_handle_exception (&ctx_cp, obj, TRUE)) {
1007                         if (mono_break_on_exc)
1008                                 G_BREAKPOINT ();
1009                         mono_unhandled_exception (obj);
1010                 }
1011         }
1012
1013         while (1) {
1014                 MonoContext new_ctx;
1015                 char *trace = NULL;
1016                 
1017                 ji = mono_arch_find_jit_info (domain, jit_tls, &rji, &rji, ctx, &new_ctx, 
1018                                               test_only ? &trace : NULL, &lmf, NULL, NULL);
1019                 if (!ji) {
1020                         g_warning ("Exception inside function without unwind info");
1021                         g_assert_not_reached ();
1022                 }
1023
1024                 if (ji != (gpointer)-1) {
1025                         
1026                         if (test_only && ji->method->wrapper_type != MONO_WRAPPER_RUNTIME_INVOKE && mono_ex) {
1027                                 char *tmp, *strace;
1028
1029                                 trace_ips = g_list_append (trace_ips, MONO_CONTEXT_GET_IP (ctx));
1030
1031                                 if (!mono_ex->stack_trace)
1032                                         strace = g_strdup ("");
1033                                 else
1034                                         strace = mono_string_to_utf8 (mono_ex->stack_trace);
1035                         
1036                                 tmp = g_strdup_printf ("%s%s\n", strace, trace);
1037                                 g_free (strace);
1038
1039                                 mono_ex->stack_trace = mono_string_new (domain, tmp);
1040
1041                                 g_free (tmp);
1042                         }
1043
1044                         if (ji->num_clauses) {
1045                                 int i;
1046                                 
1047                                 g_assert (ji->clauses);
1048                         
1049                                 for (i = 0; i < ji->num_clauses; i++) {
1050                                         MonoJitExceptionInfo *ei = &ji->clauses [i];
1051
1052                                         if (ei->try_start <= MONO_CONTEXT_GET_IP (ctx) && 
1053                                             MONO_CONTEXT_GET_IP (ctx) <= ei->try_end) { 
1054                                                 /* catch block */
1055                                                 if ((ei->flags == MONO_EXCEPTION_CLAUSE_NONE && 
1056                                                      mono_object_isinst (obj, mono_class_get (ji->method->klass->image, ei->data.token))) ||
1057                                                     ((ei->flags == MONO_EXCEPTION_CLAUSE_FILTER &&
1058                                                       call_filter (ctx, ei->data.filter, obj)))) {
1059                                                         if (test_only) {
1060                                                                 if (mono_ex)
1061                                                                         mono_ex->trace_ips = glist_to_array (trace_ips);
1062                                                                 g_list_free (trace_ips);
1063                                                                 g_free (trace);
1064                                                                 return TRUE;
1065                                                         }
1066 //                                                      memcpy(ctx, &new_ctx, sizeof(new_ctx));
1067                                                         if (mono_jit_trace_calls != NULL)
1068                                                                 g_print ("EXCEPTION: catch found at clause %d of %s - caught at %p with sp %p\n", 
1069                                                                          i, mono_method_full_name (ji->method, TRUE),
1070                                                                          ei->handler_start,
1071                                                                          MONO_CONTEXT_GET_BP(ctx));
1072                                                         MONO_CONTEXT_SET_IP (ctx, ei->handler_start);
1073                                                         *((gpointer *)((char *)MONO_CONTEXT_GET_BP (ctx) + ji->exvar_offset)) = obj;
1074                                                         jit_tls->lmf = lmf;
1075                                                         g_free (trace);
1076                                                         return 0;
1077                                                 }
1078                                                 if (!test_only && ei->try_start <= MONO_CONTEXT_GET_IP (ctx) && 
1079                                                     MONO_CONTEXT_GET_IP (ctx) < ei->try_end &&
1080                                                     (ei->flags & MONO_EXCEPTION_CLAUSE_FINALLY)) {
1081                                                         if (mono_jit_trace_calls != NULL)
1082                                                                 g_print ("EXCEPTION: finally clause %d of %s\n", i, mono_method_full_name (ji->method, TRUE));
1083                                                         call_filter (ctx, ei->handler_start, NULL);
1084                                                 }
1085                                                 
1086                                         }
1087                                 }
1088                         }
1089                 }
1090
1091                 g_free (trace);
1092                         
1093                 *ctx = new_ctx;
1094
1095                 if ((ji == (gpointer)-1) || MONO_CONTEXT_GET_BP (ctx) >= jit_tls->end_of_stack) {
1096                         if (!test_only) {
1097                                 jit_tls->lmf = lmf;
1098                                 jit_tls->abort_func (obj);
1099                                 g_assert_not_reached ();
1100                         } else {
1101                                 if (mono_ex)
1102                                         mono_ex->trace_ips = glist_to_array (trace_ips);
1103                                 g_list_free (trace_ips);
1104                                 return FALSE;
1105                         }
1106                 }
1107         }
1108
1109         g_assert_not_reached ();
1110 }
1111
1112 /*========================= End of Function ========================*/
1113
1114 /*------------------------------------------------------------------*/
1115 /*                                                                  */
1116 /* Name         - mono_arch_ip_from_context                         */
1117 /*                                                                  */
1118 /* Function     - Return the instruction pointer from the context.  */
1119 /*                                                                  */
1120 /* Parameters   - sigctx    - Saved processor state                 */
1121 /*                                                                  */
1122 /*------------------------------------------------------------------*/
1123
1124 gpointer
1125 mono_arch_ip_from_context (void *sigctx)
1126 {
1127         return context_get_ip (sigctx);
1128 }
1129