Update
[mono.git] / mono / mini / exceptions-mips.c
1 /*
2  * exceptions-mips.c: exception support for MIPS
3  *
4  * Authors:
5  *   Mark Mason (mason@broadcom.com)
6  *
7  * Based on exceptions-ppc.c by:
8  *   Dietmar Maurer (dietmar@ximian.com)
9  *   Paolo Molaro (lupus@ximian.com)
10  *
11  * (C) 2006 Broadcom
12  * (C) 2001 Ximian, Inc.
13  */
14
15 #include <config.h>
16 #include <glib.h>
17 #include <signal.h>
18 #include <string.h>
19 #include <ucontext.h>
20
21 #include <mono/arch/mips/mips-codegen.h>
22 #include <mono/metadata/appdomain.h>
23 #include <mono/metadata/tabledefs.h>
24 #include <mono/metadata/threads.h>
25 #include <mono/metadata/debug-helpers.h>
26 #include <mono/metadata/exception.h>
27 #include <mono/metadata/mono-debug.h>
28
29 #include "mini.h"
30 #include "mini-mips.h"
31
32 #define GENERIC_EXCEPTION_SIZE 256
33
34 #ifdef CUSTOM_EXCEPTION_HANDLING
35 static gboolean arch_handle_exception (MonoContext *ctx, gpointer obj, gboolean test_only);
36 #endif
37
38 /* XXX */
39 #if 1
40 #define restore_regs_from_context(ctx_reg,ip_reg,tmp_reg) do {  \
41         } while (0)
42 #else
43 #define restore_regs_from_context(ctx_reg,pc,tmp_reg) do {      \
44                 int reg;        \
45                 ppc_lwz (code, pc, G_STRUCT_OFFSET (MonoContext, sc_pc), ctx_reg);      \
46                 ppc_lmw (code, ppc_r13, ctx_reg, G_STRUCT_OFFSET (MonoContext, sc_regs));       \
47                 for (reg = 0; reg < MONO_SAVED_FREGS; ++reg) {  \
48                         ppc_lfd (code, (14 + reg), G_STRUCT_OFFSET(MonoLMF, sc_fpregs) + reg * sizeof (gdouble), ctx_reg);      \
49                 }       \
50         } while (0)
51 #endif
52
53 /* nothing to do */
54 #define setup_context(ctx) do { \
55                 memset ((ctx), 0, sizeof(*(ctx)));      \
56         } while (0);
57
58 /*
59  * mono_arch_get_restore_context:
60  *
61  * Returns a pointer to a method which restores a previously saved MonoContext.
62  * The first argument in a0 is the pointer to the MonoContext.
63  */
64 gpointer
65 mono_arch_get_restore_context (void)
66 {
67         int i;
68         guint8 *code;
69         static guint8 start [128];
70         static int inited = 0;
71
72         if (inited)
73                 return start;
74         inited = 1;
75         code = start;
76
77         for (i = 0; i < MONO_SAVED_GREGS; ++i) {
78                 if (MONO_ARCH_CALLEE_SAVED_REGS & (1 << i)) {
79                         mips_lw (code, i, mips_a0, G_STRUCT_OFFSET (MonoContext, sc_regs[i]));
80                 }
81         }
82
83         /* restore also the sp, fp and ra */
84         mips_lw (code, mips_sp, mips_a0, G_STRUCT_OFFSET (MonoContext, sc_regs[mips_sp]));
85         mips_lw (code, mips_fp, mips_a0, G_STRUCT_OFFSET (MonoContext, sc_regs[mips_fp]));
86         mips_lw (code, mips_ra, mips_a0, G_STRUCT_OFFSET (MonoContext, sc_regs[mips_ra]));
87
88         /* Get the address to return to */
89         mips_lw (code, mips_t9, mips_a0, G_STRUCT_OFFSET (MonoContext, sc_pc));
90
91         /* jump to the saved IP */
92         mips_jr (code, mips_t9);
93         mips_nop (code);
94
95         /* never reached */
96         mips_break (code, 0xff);
97
98         g_assert ((code - start) < sizeof(start));
99         mono_arch_flush_icache (start, code - start);
100         return start;
101 }
102
103 /*
104  * mono_arch_get_call_filter:
105  *
106  * Returns a pointer to a method which calls an exception filter. We
107  * also use this function to call finally handlers (we pass NULL as 
108  * @exc object in this case).
109  *
110  * This function is invoked as
111  *      call_handler (MonoContext *ctx, handler)
112  *
113  * Where 'handler' is a function to be invoked as:
114  *      handler (void)
115  */
116 gpointer
117 mono_arch_get_call_filter (void)
118 {
119         static guint8 start [320];
120         static int inited = 0;
121         guint8 *code;
122         int alloc_size;
123         int offset = 16;
124
125         if (inited)
126                 return start;
127
128         inited = 1;
129
130         code = start;
131
132         alloc_size = 64;
133         g_assert ((alloc_size & (MIPS_STACK_ALIGNMENT-1)) == 0);
134
135         mips_addiu (code, mips_sp, mips_sp, -alloc_size);
136         mips_sw (code, mips_ra, mips_sp, alloc_size + MIPS_RET_ADDR_OFFSET);
137
138         /* Save global registers on stack (s0 - s7) */
139         mips_sw (code, mips_s0, mips_sp, offset); offset += 4;
140         mips_sw (code, mips_s1, mips_sp, offset); offset += 4;
141         mips_sw (code, mips_s2, mips_sp, offset); offset += 4;
142         mips_sw (code, mips_s3, mips_sp, offset); offset += 4;
143         mips_sw (code, mips_s4, mips_sp, offset); offset += 4;
144         mips_sw (code, mips_s5, mips_sp, offset); offset += 4;
145         mips_sw (code, mips_s6, mips_sp, offset); offset += 4;
146         mips_sw (code, mips_s7, mips_sp, offset); offset += 4;
147         mips_sw (code, mips_fp, mips_sp, offset); offset += 4;
148
149         /* Restore global registers (s0-s7) */
150         mips_lw (code, mips_s0, mips_a0, G_STRUCT_OFFSET (MonoContext, sc_regs[mips_s0]));
151         mips_lw (code, mips_s1, mips_a0, G_STRUCT_OFFSET (MonoContext, sc_regs[mips_s1]));
152         mips_lw (code, mips_s2, mips_a0, G_STRUCT_OFFSET (MonoContext, sc_regs[mips_s2]));
153         mips_lw (code, mips_s3, mips_a0, G_STRUCT_OFFSET (MonoContext, sc_regs[mips_s3]));
154         mips_lw (code, mips_s4, mips_a0, G_STRUCT_OFFSET (MonoContext, sc_regs[mips_s4]));
155         mips_lw (code, mips_s5, mips_a0, G_STRUCT_OFFSET (MonoContext, sc_regs[mips_s5]));
156         mips_lw (code, mips_s6, mips_a0, G_STRUCT_OFFSET (MonoContext, sc_regs[mips_s6]));
157         mips_lw (code, mips_s7, mips_a0, G_STRUCT_OFFSET (MonoContext, sc_regs[mips_s7]));
158         mips_lw (code, mips_fp, mips_a0, G_STRUCT_OFFSET (MonoContext, sc_regs[mips_fp]));
159
160         /* a1 is the handler to call */
161         mips_move (code, mips_t9, mips_a1);
162
163         /* jump to the saved IP */
164         mips_jalr (code, mips_t9, mips_ra);
165         mips_nop (code);
166
167         /* restore all regs from the stack */
168         offset = 16;
169         mips_lw (code, mips_s0, mips_sp, offset); offset += 4;
170         mips_lw (code, mips_s1, mips_sp, offset); offset += 4;
171         mips_lw (code, mips_s2, mips_sp, offset); offset += 4;
172         mips_lw (code, mips_s3, mips_sp, offset); offset += 4;
173         mips_lw (code, mips_s4, mips_sp, offset); offset += 4;
174         mips_lw (code, mips_s5, mips_sp, offset); offset += 4;
175         mips_lw (code, mips_s6, mips_sp, offset); offset += 4;
176         mips_lw (code, mips_s7, mips_sp, offset); offset += 4;
177         mips_lw (code, mips_fp, mips_sp, offset); offset += 4;
178
179         /* epilog */
180         mips_lw (code, mips_ra, mips_sp, alloc_size + MIPS_RET_ADDR_OFFSET);
181         mips_addiu (code, mips_sp, mips_sp, alloc_size);
182         mips_jr (code, mips_ra);
183         mips_nop (code);
184
185         g_assert ((code - start) < sizeof(start));
186         mono_arch_flush_icache (start, code - start);
187         return start;
188 }
189
190 static void
191 throw_exception (MonoObject *exc, unsigned long eip, unsigned long esp, gboolean rethrow)
192 {
193         static void (*restore_context) (MonoContext *);
194         MonoContext ctx;
195
196 #if 0
197         printf("throw_exception: exc=%p eip=%x esp=%x rethrow=%d\n",
198                exc, (unsigned int) eip, (unsigned int) esp, rethrow);
199 #endif
200
201         if (!restore_context)
202                 restore_context = mono_arch_get_restore_context ();
203
204         /* adjust eip so that it point into the call instruction */
205         eip -= 8;
206
207         setup_context (&ctx);
208
209         /*printf ("stack in throw: %p\n", esp);*/
210         memcpy (&ctx.sc_regs, (void *)(esp + MIPS_STACK_PARAM_OFFSET),
211                 sizeof (gulong) * MONO_SAVED_GREGS);
212 #if 0
213         memcpy (&ctx.sc_fpregs, fp_regs, sizeof (float) * MONO_SAVED_FREGS);
214 #else
215         memset (&ctx.sc_fpregs, 0, sizeof (float) * MONO_SAVED_FREGS);
216 #endif
217         MONO_CONTEXT_SET_IP (&ctx, eip);
218
219         if (mono_object_isinst (exc, mono_defaults.exception_class)) {
220                 MonoException *mono_ex = (MonoException*)exc;
221                 if (!rethrow)
222                         mono_ex->stack_trace = NULL;
223         }
224 #ifdef CUSTOM_EXCEPTION_HANDLING
225         arch_handle_exception (&ctx, exc, FALSE);
226 #else
227         mono_handle_exception (&ctx, exc, (void *)eip, FALSE);
228 #endif
229         restore_context (&ctx);
230
231         g_assert_not_reached ();
232 }
233
234 /**
235  * arch_get_throw_exception_generic:
236  *
237  * Returns a function pointer which can be used to raise 
238  * exceptions. The returned function has the following 
239  * signature: void (*func) (MonoException *exc); or
240  * void (*func) (char *exc_name);
241  *
242  */
243 static gpointer 
244 mono_arch_get_throw_exception_generic (guint8 *start, int size, int by_name, gboolean rethrow)
245 {
246         guint8 *code;
247         int alloc_size, pos, i;
248
249         code = start;
250
251         //printf("mono_arch_get_throw_exception_generic: code=%p\n", code);
252
253         pos = 0;
254         /* XXX - save all the FP regs on the stack ? */
255
256         pos += MONO_MAX_IREGS * sizeof(guint32);
257
258         alloc_size = MIPS_MINIMAL_STACK_SIZE + pos + 64;
259         // align to MIPS_STACK_ALIGNMENT bytes
260         alloc_size += MIPS_STACK_ALIGNMENT - 1;
261         alloc_size &= ~(MIPS_STACK_ALIGNMENT - 1);
262
263         g_assert ((alloc_size & (MIPS_STACK_ALIGNMENT-1)) == 0);
264         mips_addiu (code, mips_sp, mips_sp, -alloc_size);
265         mips_sw (code, mips_ra, mips_sp, alloc_size + MIPS_RET_ADDR_OFFSET);
266
267         /* Save all the regs on the stack */
268         for (i = 0; i < MONO_MAX_IREGS; i++) {
269                 if (i != mips_sp)
270                         mips_sw (code, i, mips_sp, i*sizeof(guint32) + MIPS_STACK_PARAM_OFFSET);
271                 else {
272                         mips_addiu (code, mips_at, mips_sp, alloc_size);
273                         mips_sw (code, mips_at, mips_sp, i*sizeof(guint32) + MIPS_STACK_PARAM_OFFSET);
274                 }
275         }
276
277         if (by_name) {
278                 mips_move (code, mips_a2, mips_a0);
279                 mips_load (code, mips_a0, mono_defaults.corlib);
280                 mips_load (code, mips_a1, "System");
281                 mips_load (code, mips_t9, mono_exception_from_name);
282                 mips_jalr (code, mips_t9, mips_ra);
283                 mips_nop (code);
284                 mips_move (code, mips_a0, mips_v0);
285         }
286         /* call throw_exception (exc, ip, sp, rethrow) */
287
288         /* exc is already in place in a0 */
289
290         /* pointer to ip */
291         if (by_name)
292                 mips_lw (code, mips_a1, mips_sp, alloc_size + MIPS_RET_ADDR_OFFSET);
293         else
294                 mips_move (code, mips_a1, mips_ra);
295
296         /* current sp & rethrow */
297         mips_move (code, mips_a2, mips_sp);
298         mips_addiu (code, mips_a3, mips_zero, rethrow);
299
300         mips_load (code, mips_t9, throw_exception);
301         mips_jr (code, mips_t9);
302         mips_nop (code);
303         /* we should never reach this breakpoint */
304         mips_break (code, 0xfe);
305
306         g_assert ((code - start) < size);
307         mono_arch_flush_icache (start, code - start);
308         return start;
309 }
310
311 /**
312  * mono_arch_get_rethrow_exception:
313  *
314  * Returns a function pointer which can be used to rethrow 
315  * exceptions. The returned function has the following 
316  * signature: void (*func) (MonoException *exc); 
317  *
318  */
319 gpointer
320 mono_arch_get_rethrow_exception (void)
321 {
322         static guint8 start [GENERIC_EXCEPTION_SIZE];
323         static int inited = 0;
324
325         if (inited)
326                 return start;
327         mono_arch_get_throw_exception_generic (start, sizeof (start), FALSE, TRUE);
328         inited = 1;
329         return start;
330 }
331
332 /**
333  * arch_get_throw_exception:
334  *
335  * Returns a function pointer which can be used to raise 
336  * exceptions. The returned function has the following 
337  * signature: void (*func) (MonoException *exc); 
338  * For example to raise an arithmetic exception you can use:
339  *
340  * x86_push_imm (code, mono_get_exception_arithmetic ()); 
341  * x86_call_code (code, arch_get_throw_exception ()); 
342  *
343  */
344 gpointer 
345 mono_arch_get_throw_exception (void)
346 {
347         static guint8 start [GENERIC_EXCEPTION_SIZE];
348         static int inited = 0;
349
350         if (inited)
351                 return start;
352         mono_arch_get_throw_exception_generic (start, sizeof (start), FALSE, FALSE);
353         inited = 1;
354         return start;
355 }
356
357 /**
358  * arch_get_throw_exception_by_name:
359  *
360  * Returns a function pointer which can be used to raise 
361  * corlib exceptions. The returned function has the following 
362  * signature: void (*func) (char *exc_name); 
363  * For example to raise an arithmetic exception you can use:
364  *
365  * x86_push_imm (code, "ArithmeticException"); 
366  * x86_call_code (code, arch_get_throw_exception_by_name ()); 
367  *
368  */
369 gpointer 
370 mono_arch_get_throw_exception_by_name (void)
371 {
372         static guint8 start [GENERIC_EXCEPTION_SIZE];
373         static int inited = 0;
374
375         if (inited)
376                 return start;
377         mono_arch_get_throw_exception_generic (start, sizeof (start), TRUE, FALSE);
378         inited = 1;
379         return start;
380 }       
381
382 static MonoArray *
383 glist_to_array (GList *list, MonoClass *eclass) 
384 {
385         MonoDomain *domain = mono_domain_get ();
386         MonoArray *res;
387         int len, i;
388
389         if (!list)
390                 return NULL;
391
392         len = g_list_length (list);
393         res = mono_array_new (domain, eclass, len);
394
395         for (i = 0; list; list = list->next, i++)
396                 mono_array_set (res, gpointer, i, list->data);
397
398         return res;
399 }
400
401 /* mono_arch_find_jit_info:
402  *
403  * This function is used to gather information from @ctx. It returns the 
404  * MonoJitInfo of the corresponding function, unwinds one stack frame and
405  * stores the resulting context into @new_ctx. It also stores a string 
406  * describing the stack location into @trace (if not NULL), and modifies
407  * the @lmf if necessary. @native_offset return the IP offset from the 
408  * start of the function or -1 if that info is not available.
409  */
410 MonoJitInfo *
411 mono_arch_find_jit_info (MonoDomain *domain, MonoJitTlsData *jit_tls,
412                          MonoJitInfo *res, MonoJitInfo *prev_ji,
413                          MonoContext *ctx, MonoContext *new_ctx,
414                          char **trace, MonoLMF **lmf,
415                          int *native_offset, gboolean *managed)
416 {
417         MonoJitInfo *ji;
418         gpointer ip = MONO_CONTEXT_GET_IP (ctx);
419         guint32 sp;
420
421         /* Avoid costly table lookup during stack overflow */
422         if (prev_ji && (ip > prev_ji->code_start && ((guint8*)ip < ((guint8*)prev_ji->code_start) + prev_ji->code_size)))
423                 ji = prev_ji;
424         else
425                 ji = mono_jit_info_table_find (domain, ip);
426
427         if (trace)
428                 *trace = NULL;
429
430         if (native_offset)
431                 *native_offset = -1;
432
433         if (managed)
434                 *managed = FALSE;
435
436         if (ji != NULL) {
437                 int i;
438                 gint32 address;
439                 int offset = 0;
440
441                 *new_ctx = *ctx;
442                 setup_context (new_ctx);
443
444                 if (*lmf && (MONO_CONTEXT_GET_BP (ctx) >= (gpointer)(*lmf)->ebp)) {
445                         /* remove any unused lmf */
446                         *lmf = (*lmf)->previous_lmf;
447                 }
448
449                 address = (char *)ip - (char *)ji->code_start;
450
451                 if (native_offset)
452                         *native_offset = address;
453
454                 if (managed)
455                         if (!ji->method->wrapper_type)
456                                 *managed = TRUE;
457
458                 if (trace) {
459                         *trace = mono_debug_print_stack_frame (ji->method, offset, domain);
460                 }
461                 /* Compute the previous stack frame */
462                 sp = (guint32)(MONO_CONTEXT_GET_BP (ctx)) - (short)(*(guint32 *)(ji->code_start));
463
464                 /* Sanity check the frame */
465                 if (!sp || (sp == 0xffffffff)
466                     || (sp & 0x07) || (sp < 64*1024))
467                         return (gpointer)-1;
468                 MONO_CONTEXT_SET_BP (new_ctx, sp);
469
470                 if (ji->method->save_lmf && 0) {
471                         memcpy (&new_ctx->sc_fpregs, (char*)sp - sizeof (float) * MONO_SAVED_FREGS, sizeof (float) * MONO_SAVED_FREGS);
472                         memcpy (&new_ctx->sc_regs, (char*)sp - sizeof (float) * MONO_SAVED_FREGS - sizeof (gulong) * MONO_SAVED_GREGS, sizeof (gulong) * MONO_SAVED_GREGS);
473                 } else if (ji->used_regs) {
474                         /* keep updated with emit_prolog in mini-mips.c */
475                         offset = 0;
476                         /* FIXME handle floating point args */
477                         for (i = 0; i < MONO_MAX_IREGS; i++) {
478                                 new_ctx->sc_regs [i] = 0;
479                         }
480                         new_ctx->sc_regs [mips_fp] = sp;
481                         new_ctx->sc_regs [mips_sp] = sp;
482                         new_ctx->sc_regs [mips_ra] = *(guint32 *)(sp-4);
483                 }
484                 /* we substract 8, so that the IP points into the call instruction */
485                 MONO_CONTEXT_SET_IP (new_ctx, new_ctx->sc_regs[mips_ra] - 8);
486
487                 return ji;
488         } else if (*lmf) {
489                 
490                 *new_ctx = *ctx;
491                 setup_context (new_ctx);
492
493                 if (!(*lmf)->method)
494                         return (gpointer)-1;
495
496                 if (trace) {
497                         char *fname = mono_method_full_name ((*lmf)->method, TRUE);
498                         *trace = g_strdup_printf ("in (unmanaged) %s", fname);
499                         g_free (fname);
500                 }
501                 
502                 if ((ji = mono_jit_info_table_find (domain, (gpointer)(*lmf)->eip))) {
503                 } else {
504                         memset (res, 0, sizeof (MonoJitInfo));
505                         res->method = (*lmf)->method;
506                 }
507
508 #if 0
509                 memcpy (&new_ctx->sc_regs, (*lmf)->iregs, sizeof (gulong) * MONO_SAVED_GREGS);
510                 memcpy (&new_ctx->sc_fpregs, (*lmf)->fregs, sizeof (float) * MONO_SAVED_FREGS);
511 #endif
512                 MONO_CONTEXT_SET_BP (new_ctx, (*lmf)->ebp);
513                 MONO_CONTEXT_SET_IP (new_ctx, (*lmf)->eip);
514                 *lmf = (*lmf)->previous_lmf;
515
516                 return ji ? ji : res;
517         }
518
519         return NULL;
520 }
521
522 #ifdef CUSTOM_STACK_WALK
523
524 void
525 mono_jit_walk_stack (MonoStackWalk func, gboolean do_il_offset, gpointer user_data) {
526         MonoDomain *domain = mono_domain_get ();
527         MonoJitTlsData *jit_tls = TlsGetValue (mono_jit_tls_id);
528         MonoLMF *lmf = jit_tls->lmf;
529         MonoJitInfo *ji, rji;
530         gint native_offset, il_offset;
531         gboolean managed;
532         MonoMipsStackFrame *sframe;
533
534         MonoContext ctx, new_ctx;
535
536         setup_context (&ctx);
537         setup_context (&new_ctx);
538
539         MONO_INIT_CONTEXT_FROM_FUNC (&ctx, mono_jit_walk_stack);
540         MONO_INIT_CONTEXT_FROM_FUNC (&new_ctx, mono_jit_walk_stack);
541
542         while (MONO_CONTEXT_GET_BP (&ctx) < jit_tls->end_of_stack) {
543                 
544                 ji = mono_find_jit_info (domain, jit_tls, &rji, NULL, &ctx, &new_ctx, NULL, &lmf, &native_offset, &managed);
545                 g_assert (ji);
546
547                 if (ji == (gpointer)-1)
548                         return;
549
550                 if (do_il_offset) {
551                         MonoDebugSourceLocation *source;
552
553                         source = mono_debug_lookup_source_location (ji->method, native_offset, domain);
554                         il_offset = source ? source->il_offset : -1;
555                         mono_debug_free_source_location (source);
556                 } else
557                         il_offset = -1;
558
559                 if (func (ji->method, native_offset, il_offset, managed, user_data))
560                         return;
561                 
562                 ctx = new_ctx;
563                 setup_context (&ctx);
564         }
565 }
566
567
568 MonoBoolean
569 ves_icall_get_frame_info (gint32 skip, MonoBoolean need_file_info, 
570                           MonoReflectionMethod **method, 
571                           gint32 *iloffset, gint32 *native_offset,
572                           MonoString **file, gint32 *line, gint32 *column)
573 {
574         MonoDomain *domain = mono_domain_get ();
575         MonoJitTlsData *jit_tls = TlsGetValue (mono_jit_tls_id);
576         MonoLMF *lmf = jit_tls->lmf;
577         MonoJitInfo *ji, rji;
578         MonoContext ctx, new_ctx;
579         MonoMipsStackFrame *sframe;
580         MonoDebugSourceLocation *location;
581
582 #if 0
583 #ifdef __APPLE__
584         __asm__ volatile("lwz   %0,0(r1)" : "=r" (sframe));
585 #else
586         __asm__ volatile("lwz   %0,0(1)" : "=r" (sframe));
587 #endif
588 #endif
589         MONO_CONTEXT_SET_BP (&ctx, sframe->sp);
590         sframe = (MonoMipsStackFrame*)sframe->sp;
591         MONO_CONTEXT_SET_IP (&ctx, sframe->ra);
592         /*MONO_CONTEXT_SET_IP (&ctx, ves_icall_get_frame_info);
593         MONO_CONTEXT_SET_BP (&ctx, __builtin_frame_address (0));*/
594
595         skip++;
596
597         do {
598                 ji = mono_find_jit_info (domain, jit_tls, &rji, NULL, &ctx, &new_ctx, NULL, &lmf, native_offset, NULL);
599
600                 ctx = new_ctx;
601                 
602                 if (!ji || ji == (gpointer)-1 || MONO_CONTEXT_GET_BP (&ctx) >= jit_tls->end_of_stack)
603                         return FALSE;
604
605                 /* skip all wrappers ??*/
606                 if (ji->method->wrapper_type == MONO_WRAPPER_RUNTIME_INVOKE ||
607                     ji->method->wrapper_type == MONO_WRAPPER_REMOTING_INVOKE_WITH_CHECK ||
608                     ji->method->wrapper_type == MONO_WRAPPER_XDOMAIN_INVOKE ||
609                     ji->method->wrapper_type == MONO_WRAPPER_XDOMAIN_DISPATCH ||
610                     ji->method->wrapper_type == MONO_WRAPPER_REMOTING_INVOKE)
611                         continue;
612
613                 skip--;
614
615         } while (skip >= 0);
616
617         *method = mono_method_get_object (domain, ji->method, NULL);
618
619         location = mono_debug_lookup_source_location (ji->method, *native_offset, domain);
620         if (location)
621                 *iloffset = location->il_offset;
622         else
623                 *iloffset = 0;
624
625         if (need_file_info) {
626                 if (location) {
627                         *file = mono_string_new (domain, location->source_file);
628                         *line = location->row;
629                         *column = location->column;
630                 } else {
631                         *file = NULL;
632                         *line = *column = 0;
633                 }
634         }
635
636         mono_debug_free_source_location (location);
637
638         return TRUE;
639 }
640
641 #endif /* CUSTOM_STACK_WALK */
642
643 void
644 mono_arch_sigctx_to_monoctx (void *sigctx, MonoContext *mctx)
645 {
646         int i;
647         struct sigcontext *ctx = (struct sigcontext *)sigctx;
648
649         mctx->sc_pc = ctx->sc_pc;
650         for (i = 0; i < 32; ++i) {
651                 mctx->sc_regs[i] = ctx->sc_regs[i];
652                 mctx->sc_fpregs[i] = ctx->sc_fpregs[i];
653         }
654 }
655
656 void
657 mono_arch_monoctx_to_sigctx (MonoContext *mctx, void *sigctx)
658 {
659         int i;
660         struct sigcontext *ctx = (struct sigcontext *)sigctx;
661
662         ctx->sc_pc = mctx->sc_pc;
663         for (i = 0; i < 32; ++i) {
664                 ctx->sc_regs[i] = mctx->sc_regs[i];
665                 ctx->sc_fpregs[i] = mctx->sc_fpregs[i];
666         }
667 }       
668
669 gpointer
670 mono_arch_ip_from_context (void *sigctx)
671 {
672         struct ucontext *uc = sigctx;
673         return (gpointer)(guint32)uc->uc_mcontext.pc;
674 }
675
676 /*
677  * This is the function called from the signal handler
678  */
679 gboolean
680 mono_arch_handle_exception (void *ctx, gpointer obj, gboolean test_only)
681 {
682         MonoContext mctx;
683         gboolean result;
684         
685         mono_arch_sigctx_to_monoctx (ctx, &mctx);
686 #ifdef CUSTOM_EXCEPTION_HANDLING
687         result = arch_handle_exception (&mctx, obj, test_only);
688 #else
689         mono_handle_exception (&mctx, obj, (gpointer)mctx.sc_pc, test_only);
690         result = TRUE;
691 #endif
692
693         /* restore the context so that returning from the signal handler
694          * will invoke the catch clause 
695          */
696         mono_arch_monoctx_to_sigctx (&mctx, ctx);
697
698         return result;
699 }
700
701 #ifdef CUSTOM_EXCEPTION_HANDLING
702 /**
703  * arch_handle_exception:
704  * @ctx: saved processor state
705  * @obj: the exception object
706  * @test_only: only test if the exception is caught, but dont call handlers
707  *
708  *
709  */
710 static gboolean
711 arch_handle_exception (MonoContext *ctx, gpointer obj, gboolean test_only)
712 {
713         MonoDomain *domain = mono_domain_get ();
714         MonoJitInfo *ji, rji;
715         static int (*call_filter) (MonoContext *, gpointer, gpointer) = NULL;
716         MonoJitTlsData *jit_tls = TlsGetValue (mono_jit_tls_id);
717         MonoLMF *lmf = jit_tls->lmf;            
718         GList *trace_ips = NULL;
719         MonoException *mono_ex;
720         MonoArray *initial_trace_ips = NULL;
721         int frame_count = 0;
722         gboolean has_dynamic_methods = FALSE;
723
724         g_assert (ctx != NULL);
725         if (!obj) {
726                 MonoException *ex = mono_get_exception_null_reference ();
727                 ex->message = mono_string_new (domain, 
728                         "Object reference not set to an instance of an object");
729                 obj = (MonoObject *)ex;
730         } 
731
732         if (mono_object_isinst (obj, mono_defaults.exception_class)) {
733                 mono_ex = (MonoException*)obj;
734                 initial_trace_ips = mono_ex->trace_ips;
735         } else {
736                 mono_ex = NULL;
737         }
738
739
740         if (!call_filter)
741                 call_filter = mono_arch_get_call_filter ();
742
743         g_assert (jit_tls->end_of_stack);
744         g_assert (jit_tls->abort_func);
745
746         if (!test_only) {
747                 MonoContext ctx_cp = *ctx;
748                 setup_context (&ctx_cp);
749                 if (mono_jit_trace_calls != NULL)
750                         g_print ("EXCEPTION handling: %s\n", mono_object_class (obj)->name);
751                 if (!arch_handle_exception (&ctx_cp, obj, TRUE)) {
752                         if (mono_break_on_exc)
753                                 G_BREAKPOINT ();
754                         mono_unhandled_exception (obj);
755                 }
756         }
757
758         memset (&rji, 0, sizeof (rji));
759
760         while (1) {
761                 MonoContext new_ctx;
762
763                 setup_context (&new_ctx);
764                 ji = mono_arch_find_jit_info (domain, jit_tls, &rji, &rji, ctx, &new_ctx, 
765                                               NULL, &lmf, NULL, NULL);
766                 if (!ji) {
767                         g_warning ("Exception inside function without unwind info");
768                         g_assert_not_reached ();
769                 }
770
771                 if (ji != (gpointer)-1) {
772                         frame_count ++;
773                         
774                         if (test_only && ji->method->wrapper_type != MONO_WRAPPER_RUNTIME_INVOKE && mono_ex) {
775                                 /* 
776                                  * Avoid overwriting the stack trace if the exception is
777                                  * rethrown. Also avoid giant stack traces during a stack
778                                  * overflow.
779                                  */
780                                 if (!initial_trace_ips && (frame_count < 1000)) {
781                                         trace_ips = g_list_prepend (trace_ips, MONO_CONTEXT_GET_IP (ctx));
782
783                                 }
784                         }
785
786                         if (ji->method->dynamic)
787                                 has_dynamic_methods = TRUE;
788
789                         if (ji->num_clauses) {
790                                 int i;
791                                 
792                                 g_assert (ji->clauses);
793                         
794                                 for (i = 0; i < ji->num_clauses; i++) {
795                                         MonoJitExceptionInfo *ei = &ji->clauses [i];
796                                         gboolean filtered = FALSE;
797
798                                         if (ei->try_start <= MONO_CONTEXT_GET_IP (ctx) && 
799                                             MONO_CONTEXT_GET_IP (ctx) <= ei->try_end) { 
800                                                 /* catch block */
801
802                                                 if ((ei->flags == MONO_EXCEPTION_CLAUSE_NONE) || (ei->flags == MONO_EXCEPTION_CLAUSE_FILTER)) {
803                                                         /* store the exception object int cfg->excvar */
804                                                         g_assert (ei->exvar_offset);
805                                                         /* need to use the frame pointer (mips_fp): methods with clauses always have mips_fp */
806                                                         *((gpointer *)(void*)((char *)(ctx->sc_regs [mips_fp]) + ei->exvar_offset)) = obj;
807                                                 }
808
809                                                 if (ei->flags == MONO_EXCEPTION_CLAUSE_FILTER)
810                                                         filtered = call_filter (ctx, ei->data.filter, mono_ex);
811
812                                                 if ((ei->flags == MONO_EXCEPTION_CLAUSE_NONE && 
813                                                      mono_object_isinst (obj, ei->data.catch_class)) || filtered) {
814                                                         if (test_only) {
815                                                                 if (mono_ex && !initial_trace_ips) {
816                                                                         trace_ips = g_list_reverse (trace_ips);
817                                                                         mono_ex->trace_ips = glist_to_array (trace_ips, mono_defaults.int_class);
818                                                                         if (has_dynamic_methods)
819                                                                                 /* These methods could go away anytime, so compute the stack trace now */
820                                                                                 mono_ex->stack_trace = ves_icall_System_Exception_get_trace (mono_ex);
821                                                                 }
822                                                                 g_list_free (trace_ips);
823                                                                 return TRUE;
824                                                         }
825                                                         if (mono_jit_trace_calls != NULL)
826                                                                 g_print ("EXCEPTION: catch found at clause %d of %s\n", i, mono_method_full_name (ji->method, TRUE));
827                                                         /*printf ("stack for catch: %p\n", MONO_CONTEXT_GET_BP (ctx));*/
828                                                         MONO_CONTEXT_SET_IP (ctx, ei->handler_start);
829                                                         jit_tls->lmf = lmf;
830                                                         return 0;
831                                                 }
832                                                 if (!test_only && ei->try_start <= MONO_CONTEXT_GET_IP (ctx) && 
833                                                     MONO_CONTEXT_GET_IP (ctx) < ei->try_end &&
834                                                     (ei->flags & MONO_EXCEPTION_CLAUSE_FINALLY)) {
835                                                         if (mono_jit_trace_calls != NULL)
836                                                                 g_print ("EXCEPTION: finally clause %d of %s\n", i, mono_method_full_name (ji->method, TRUE));
837                                                         call_filter (ctx, ei->handler_start, NULL);
838                                                 }
839                                                 
840                                         }
841                                 }
842                         }
843                 }
844
845                 *ctx = new_ctx;
846                 setup_context (ctx);
847
848                 if ((ji == (gpointer)-1) || MONO_CONTEXT_GET_BP (ctx) >= jit_tls->end_of_stack) {
849                         if (!test_only) {
850                                 jit_tls->lmf = lmf;
851                                 jit_tls->abort_func (obj);
852                                 g_assert_not_reached ();
853                         } else {
854                                 if (mono_ex && !initial_trace_ips) {
855                                         trace_ips = g_list_reverse (trace_ips);
856                                         mono_ex->trace_ips = glist_to_array (trace_ips, mono_defaults.int_class);
857                                         if (has_dynamic_methods)
858                                                 /* These methods could go away anytime, so compute the stack trace now */
859                                                 mono_ex->stack_trace = ves_icall_System_Exception_get_trace (mono_ex);
860                                 }
861                                 g_list_free (trace_ips);
862                                 return FALSE;
863                         }
864                 }
865         }
866
867         g_assert_not_reached ();
868 }
869 #endif /* CUSTOM_EXCEPTION_HANDLING */
870
871 gboolean
872 mono_arch_has_unwind_info (gconstpointer addr)
873 {
874         return FALSE;
875 }
876