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