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