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