2008-08-19 Zoltan Varga <vargaz@gmail.com>
[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         guint32 iregs_to_restore;
72
73         if (inited)
74                 return start;
75         inited = 1;
76         code = start;
77
78         iregs_to_restore = (MONO_ARCH_CALLEE_SAVED_REGS \
79                             | (1 << mips_sp) | (1 << mips_ra));
80         for (i = 0; i < MONO_SAVED_GREGS; ++i) {
81                 if (iregs_to_restore & (1 << i)) {
82                         mips_lw (code, i, mips_a0, G_STRUCT_OFFSET (MonoContext, sc_regs[i]));
83                 }
84         }
85
86         /* Get the address to return to */
87         mips_lw (code, mips_t9, mips_a0, G_STRUCT_OFFSET (MonoContext, sc_pc));
88
89         /* jump to the saved IP */
90         mips_jr (code, mips_t9);
91         mips_nop (code);
92
93         /* never reached */
94         mips_break (code, 0xff);
95
96         g_assert ((code - start) < sizeof(start));
97         mono_arch_flush_icache (start, code - start);
98         return start;
99 }
100
101 /*
102  * mono_arch_get_call_filter:
103  *
104  * Returns a pointer to a method which calls an exception filter. We
105  * also use this function to call finally handlers (we pass NULL as 
106  * @exc object in this case).
107  *
108  * This function is invoked as
109  *      call_handler (MonoContext *ctx, handler)
110  *
111  * Where 'handler' is a function to be invoked as:
112  *      handler (void)
113  */
114 gpointer
115 mono_arch_get_call_filter (void)
116 {
117         static guint8 start [320];
118         static int inited = 0;
119         guint8 *code;
120         int alloc_size;
121         int offset;
122
123         if (inited)
124                 return start;
125
126         inited = 1;
127         code = start;
128
129         alloc_size = 64;
130         g_assert ((alloc_size & (MIPS_STACK_ALIGNMENT-1)) == 0);
131
132         mips_addiu (code, mips_sp, mips_sp, -alloc_size);
133         mips_sw (code, mips_ra, mips_sp, alloc_size + MIPS_RET_ADDR_OFFSET);
134
135         /* Save global registers on stack (s0 - s7) */
136         offset = 16;
137         mips_sw (code, mips_s0, mips_sp, offset); offset += 4;
138         mips_sw (code, mips_s1, mips_sp, offset); offset += 4;
139         mips_sw (code, mips_s2, mips_sp, offset); offset += 4;
140         mips_sw (code, mips_s3, mips_sp, offset); offset += 4;
141         mips_sw (code, mips_s4, mips_sp, offset); offset += 4;
142         mips_sw (code, mips_s5, mips_sp, offset); offset += 4;
143         mips_sw (code, mips_s6, mips_sp, offset); offset += 4;
144         mips_sw (code, mips_s7, mips_sp, offset); offset += 4;
145         mips_sw (code, mips_fp, mips_sp, offset); offset += 4;
146
147         /* Restore global registers from MonoContext, including the frame pointer */
148         mips_lw (code, mips_s0, mips_a0, G_STRUCT_OFFSET (MonoContext, sc_regs[mips_s0]));
149         mips_lw (code, mips_s1, mips_a0, G_STRUCT_OFFSET (MonoContext, sc_regs[mips_s1]));
150         mips_lw (code, mips_s2, mips_a0, G_STRUCT_OFFSET (MonoContext, sc_regs[mips_s2]));
151         mips_lw (code, mips_s3, mips_a0, G_STRUCT_OFFSET (MonoContext, sc_regs[mips_s3]));
152         mips_lw (code, mips_s4, mips_a0, G_STRUCT_OFFSET (MonoContext, sc_regs[mips_s4]));
153         mips_lw (code, mips_s5, mips_a0, G_STRUCT_OFFSET (MonoContext, sc_regs[mips_s5]));
154         mips_lw (code, mips_s6, mips_a0, G_STRUCT_OFFSET (MonoContext, sc_regs[mips_s6]));
155         mips_lw (code, mips_s7, mips_a0, G_STRUCT_OFFSET (MonoContext, sc_regs[mips_s7]));
156         mips_lw (code, mips_fp, mips_a0, G_STRUCT_OFFSET (MonoContext, sc_regs[mips_fp]));
157
158         /* a1 is the handler to call */
159         mips_move (code, mips_t9, mips_a1);
160
161         /* jump to the saved IP */
162         mips_jalr (code, mips_t9, mips_ra);
163         mips_nop (code);
164
165         /* restore all regs from the stack */
166         offset = 16;
167         mips_lw (code, mips_s0, mips_sp, offset); offset += 4;
168         mips_lw (code, mips_s1, mips_sp, offset); offset += 4;
169         mips_lw (code, mips_s2, mips_sp, offset); offset += 4;
170         mips_lw (code, mips_s3, mips_sp, offset); offset += 4;
171         mips_lw (code, mips_s4, mips_sp, offset); offset += 4;
172         mips_lw (code, mips_s5, mips_sp, offset); offset += 4;
173         mips_lw (code, mips_s6, mips_sp, offset); offset += 4;
174         mips_lw (code, mips_s7, mips_sp, offset); offset += 4;
175         mips_lw (code, mips_fp, mips_sp, offset); offset += 4;
176
177         /* epilog */
178         mips_lw (code, mips_ra, mips_sp, alloc_size + MIPS_RET_ADDR_OFFSET);
179         mips_addiu (code, mips_sp, mips_sp, alloc_size);
180         mips_jr (code, mips_ra);
181         mips_nop (code);
182
183         g_assert ((code - start) < sizeof(start));
184         mono_arch_flush_icache (start, code - start);
185         return start;
186 }
187
188 static void
189 throw_exception (MonoObject *exc, unsigned long eip, unsigned long esp, gboolean rethrow)
190 {
191         static void (*restore_context) (MonoContext *);
192         MonoContext ctx;
193
194 #ifdef DEBUG_EXCEPTIONS
195         g_print ("throw_exception: exc=%p eip=%p esp=%p rethrow=%d\n",
196                  exc, (void *)eip, (void *) esp, rethrow);
197 #endif
198
199         if (!restore_context)
200                 restore_context = mono_arch_get_restore_context ();
201
202         /* adjust eip so that it point into the call instruction */
203         eip -= 8;
204
205         setup_context (&ctx);
206
207         /*g_print  ("stack in throw: %p\n", esp);*/
208         memcpy (&ctx.sc_regs, (void *)(esp + MIPS_STACK_PARAM_OFFSET),
209                 sizeof (gulong) * MONO_SAVED_GREGS);
210 #if 0
211         memcpy (&ctx.sc_fpregs, fp_regs, sizeof (float) * MONO_SAVED_FREGS);
212 #else
213         memset (&ctx.sc_fpregs, 0, sizeof (float) * MONO_SAVED_FREGS);
214 #endif
215         MONO_CONTEXT_SET_IP (&ctx, eip);
216
217         if (mono_object_isinst (exc, mono_defaults.exception_class)) {
218                 MonoException *mono_ex = (MonoException*)exc;
219                 if (!rethrow)
220                         mono_ex->stack_trace = NULL;
221         }
222 #ifdef CUSTOM_EXCEPTION_HANDLING
223         arch_handle_exception (&ctx, exc, FALSE);
224 #else
225         mono_handle_exception (&ctx, exc, (void *)eip, FALSE);
226 #endif
227 #ifdef DEBUG_EXCEPTIONS
228         g_print ("throw_exception: restore to pc=%p sp=%p fp=%p ctx=%p\n",
229                  (void *) ctx.sc_pc, (void *) ctx.sc_regs[mips_sp],
230                  (void *) ctx.sc_regs[mips_fp], &ctx);
231 #endif
232         restore_context (&ctx);
233
234         g_assert_not_reached ();
235 }
236
237 /**
238  * arch_get_throw_exception_generic:
239  *
240  * Returns a function pointer which can be used to raise 
241  * exceptions. The returned function has the following 
242  * signature: void (*func) (MonoException *exc); or
243  * void (*func) (char *exc_name);
244  *
245  */
246 static gpointer 
247 mono_arch_get_throw_exception_generic (guint8 *start, int size, int by_name, gboolean rethrow)
248 {
249         guint8 *code;
250         int alloc_size, pos, i;
251
252         code = start;
253
254         //g_print ("mono_arch_get_throw_exception_generic: code=%p\n", code);
255
256         pos = 0;
257         /* XXX - save all the FP regs on the stack ? */
258
259         pos += MONO_MAX_IREGS * sizeof(guint32);
260
261         alloc_size = MIPS_MINIMAL_STACK_SIZE + pos + 64;
262         // align to MIPS_STACK_ALIGNMENT bytes
263         alloc_size += MIPS_STACK_ALIGNMENT - 1;
264         alloc_size &= ~(MIPS_STACK_ALIGNMENT - 1);
265
266         g_assert ((alloc_size & (MIPS_STACK_ALIGNMENT-1)) == 0);
267         mips_addiu (code, mips_sp, mips_sp, -alloc_size);
268         mips_sw (code, mips_ra, mips_sp, alloc_size + MIPS_RET_ADDR_OFFSET);
269
270         /* Save all the regs on the stack */
271         for (i = 0; i < MONO_MAX_IREGS; i++) {
272                 if (i != mips_sp)
273                         mips_sw (code, i, mips_sp, i*sizeof(guint32) + MIPS_STACK_PARAM_OFFSET);
274                 else {
275                         mips_addiu (code, mips_at, mips_sp, alloc_size);
276                         mips_sw (code, mips_at, mips_sp, i*sizeof(guint32) + MIPS_STACK_PARAM_OFFSET);
277                 }
278         }
279
280         if (by_name) {
281                 mips_move (code, mips_a2, mips_a0);
282                 mips_load (code, mips_a0, mono_defaults.corlib);
283                 mips_load (code, mips_a1, "System");
284                 mips_load (code, mips_t9, mono_exception_from_name);
285                 mips_jalr (code, mips_t9, mips_ra);
286                 mips_nop (code);
287                 mips_move (code, mips_a0, mips_v0);
288         }
289         /* call throw_exception (exc, ip, sp, rethrow) */
290
291         /* exc is already in place in a0 */
292
293         /* pointer to ip */
294         if (by_name)
295                 mips_lw (code, mips_a1, mips_sp, alloc_size + MIPS_RET_ADDR_OFFSET);
296         else
297                 mips_move (code, mips_a1, mips_ra);
298
299         /* current sp & rethrow */
300         mips_move (code, mips_a2, mips_sp);
301         mips_addiu (code, mips_a3, mips_zero, rethrow);
302
303         mips_load (code, mips_t9, throw_exception);
304         mips_jr (code, mips_t9);
305         mips_nop (code);
306         /* we should never reach this breakpoint */
307         mips_break (code, 0xfe);
308
309         g_assert ((code - start) < size);
310         mono_arch_flush_icache (start, code - start);
311         return start;
312 }
313
314 /**
315  * mono_arch_get_rethrow_exception:
316  *
317  * Returns a function pointer which can be used to rethrow 
318  * exceptions. The returned function has the following 
319  * signature: void (*func) (MonoException *exc); 
320  *
321  */
322 gpointer
323 mono_arch_get_rethrow_exception (void)
324 {
325         static guint8 start [GENERIC_EXCEPTION_SIZE];
326         static int inited = 0;
327
328         if (inited)
329                 return start;
330         mono_arch_get_throw_exception_generic (start, sizeof (start), FALSE, TRUE);
331         inited = 1;
332         return start;
333 }
334
335 /**
336  * arch_get_throw_exception:
337  *
338  * Returns a function pointer which can be used to raise 
339  * exceptions. The returned function has the following 
340  * signature: void (*func) (MonoException *exc); 
341  * For example to raise an arithmetic exception you can use:
342  *
343  * x86_push_imm (code, mono_get_exception_arithmetic ()); 
344  * x86_call_code (code, arch_get_throw_exception ()); 
345  *
346  */
347 gpointer 
348 mono_arch_get_throw_exception (void)
349 {
350         static guint8 start [GENERIC_EXCEPTION_SIZE];
351         static int inited = 0;
352
353         if (inited)
354                 return start;
355         mono_arch_get_throw_exception_generic (start, sizeof (start), FALSE, FALSE);
356         inited = 1;
357         return start;
358 }
359
360 /**
361  * arch_get_throw_exception_by_name:
362  *
363  * Returns a function pointer which can be used to raise 
364  * corlib exceptions. The returned function has the following 
365  * signature: void (*func) (char *exc_name); 
366  * For example to raise an arithmetic exception you can use:
367  *
368  * x86_push_imm (code, "ArithmeticException"); 
369  * x86_call_code (code, arch_get_throw_exception_by_name ()); 
370  *
371  */
372 gpointer 
373 mono_arch_get_throw_exception_by_name (void)
374 {
375         static guint8 start [GENERIC_EXCEPTION_SIZE];
376         static int inited = 0;
377
378         if (inited)
379                 return start;
380         mono_arch_get_throw_exception_generic (start, sizeof (start), TRUE, FALSE);
381         inited = 1;
382         return start;
383 }       
384
385 static MonoArray *
386 glist_to_array (GList *list, MonoClass *eclass) 
387 {
388         MonoDomain *domain = mono_domain_get ();
389         MonoArray *res;
390         int len, i;
391
392         if (!list)
393                 return NULL;
394
395         len = g_list_length (list);
396         res = mono_array_new (domain, eclass, len);
397
398         for (i = 0; list; list = list->next, i++)
399                 mono_array_set (res, gpointer, i, list->data);
400
401         return res;
402 }
403
404 /* mono_arch_find_jit_info:
405  *
406  * This function is used to gather information from @ctx. It returns the 
407  * MonoJitInfo of the corresponding function, unwinds one stack frame and
408  * stores the resulting context into @new_ctx. It also stores a string 
409  * describing the stack location into @trace (if not NULL), and modifies
410  * the @lmf if necessary. @native_offset return the IP offset from the 
411  * start of the function or -1 if that info is not available.
412  */
413 MonoJitInfo *
414 mono_arch_find_jit_info (MonoDomain *domain, MonoJitTlsData *jit_tls,
415                          MonoJitInfo *res, MonoJitInfo *prev_ji,
416                          MonoContext *ctx, MonoContext *new_ctx,
417                          MonoLMF **lmf, gboolean *managed)
418 {
419         MonoJitInfo *ji;
420         gpointer ip = MONO_CONTEXT_GET_IP (ctx);
421         gpointer fp = MONO_CONTEXT_GET_BP (ctx);
422         guint32 sp;
423
424         /* Avoid costly table lookup during stack overflow */
425         if (prev_ji && (ip > prev_ji->code_start && ((guint8*)ip < ((guint8*)prev_ji->code_start) + prev_ji->code_size)))
426                 ji = prev_ji;
427         else
428                 ji = mono_jit_info_table_find (domain, ip);
429
430         if (managed)
431                 *managed = FALSE;
432
433         *new_ctx = *ctx;
434         setup_context (new_ctx);
435
436         if (ji != NULL) {
437                 int i;
438                 gint32 address;
439                 int offset = 0;
440
441                 if (*lmf && (MONO_CONTEXT_GET_BP (ctx) >= (gpointer)(*lmf)->ebp)) {
442                         /* remove any unused lmf */
443                         *lmf = (*lmf)->previous_lmf;
444                 }
445
446                 address = (char *)ip - (char *)ji->code_start;
447
448                 if (managed)
449                         if (!ji->method->wrapper_type)
450                                 *managed = TRUE;
451
452                 /* My stack frame */
453                 fp = MONO_CONTEXT_GET_BP (ctx);
454
455                 /* Compute the previous stack frame */
456                 sp = (guint32)(fp) - (short)(*(guint32 *)(ji->code_start));
457
458                 /* Sanity check the frame */
459                 if (!sp || (sp == 0xffffffff)
460                     || (sp & 0x07) || (sp < 64*1024)) {
461 #ifdef DEBUG_EXCEPTIONS
462                         g_print ("mono_arch_find_jit_info: bad stack sp=%p\n", (void *) sp);
463 #endif
464                         return (gpointer)-1;
465                 }
466
467                 if (ji->method->save_lmf && 0) {
468                         /* only enable this when prologue stops emitting
469                          * normal save of s-regs when save_lmf is true.
470                          * Will have to sync with prologue code at that point.
471                          */
472                         memcpy (&new_ctx->sc_fpregs,
473                                 (char*)sp - sizeof (float) * MONO_SAVED_FREGS,
474                                 sizeof (float) * MONO_SAVED_FREGS);
475                         memcpy (&new_ctx->sc_regs,
476                                 (char*)sp - sizeof (float) * MONO_SAVED_FREGS - sizeof (gulong) * MONO_SAVED_GREGS,
477                                 sizeof (gulong) * MONO_SAVED_GREGS);
478                 } else if (ji->used_regs) {
479                         guint32 *insn;
480                         guint32 mask = ji->used_regs;
481
482                         /* these all happen before adjustment of fp */
483                         /* Look for sw ??, ????(sp) */
484                         insn = ((guint32 *)ji->code_start) + 1;
485                         while (!*insn || (*insn & 0xffe00000) == 0xafa00000) {
486                                 int reg = (*insn >> 16) & 0x1f;
487                                 mask &= ~(1 << reg);
488                                 new_ctx->sc_regs [reg] = *(guint32 *)(((guint32)fp) + (short)(*insn & 0x0000ffff));
489                                 insn++;
490                         }
491                         MONO_CONTEXT_SET_SP (new_ctx, sp);
492                         MONO_CONTEXT_SET_BP (new_ctx, sp);
493                         /* assert that we found all registers we were supposed to */
494                         g_assert (!mask);
495                 }
496                 /* we substract 8, so that the IP points into the call instruction */
497                 MONO_CONTEXT_SET_IP (new_ctx, new_ctx->sc_regs[mips_ra] - 8);
498
499                 /* Sanity check -- we should have made progress here */
500                 g_assert (new_ctx->sc_pc != ctx->sc_pc);
501                 return ji;
502         } else if (*lmf) {
503                 if (!(*lmf)->method) {
504 #ifdef DEBUG_EXCEPTIONS
505                         g_print ("mono_arch_find_jit_info: bad lmf @ %p\n", (void *) *lmf);
506 #endif
507                         return (gpointer)-1;
508                 }
509                 g_assert (((*lmf)->magic == MIPS_LMF_MAGIC1) || ((*lmf)->magic == MIPS_LMF_MAGIC2));
510
511                 if ((ji = mono_jit_info_table_find (domain, (gpointer)(*lmf)->eip))) {
512                 } else {
513                         memset (res, 0, sizeof (MonoJitInfo));
514                         res->method = (*lmf)->method;
515                 }
516                 memcpy (&new_ctx->sc_regs, (*lmf)->iregs, sizeof (gulong) * MONO_SAVED_GREGS);
517                 memcpy (&new_ctx->sc_fpregs, (*lmf)->fregs, sizeof (float) * MONO_SAVED_FREGS);
518                 MONO_CONTEXT_SET_IP (new_ctx, (*lmf)->eip);
519                 /* ensure that we've made progress */
520                 g_assert (new_ctx->sc_pc != ctx->sc_pc);
521                 *lmf = (*lmf)->previous_lmf;
522
523                 return ji ? ji : res;
524         }
525
526         return NULL;
527 }
528
529 void
530 mono_arch_sigctx_to_monoctx (void *sigctx, MonoContext *mctx)
531 {
532         int i;
533         struct sigcontext *ctx = (struct sigcontext *)sigctx;
534
535         mctx->sc_pc = ctx->sc_pc;
536         for (i = 0; i < 32; ++i) {
537                 mctx->sc_regs[i] = ctx->sc_regs[i];
538                 mctx->sc_fpregs[i] = ctx->sc_fpregs[i];
539         }
540 }
541
542 void
543 mono_arch_monoctx_to_sigctx (MonoContext *mctx, void *sigctx)
544 {
545         int i;
546         struct sigcontext *ctx = (struct sigcontext *)sigctx;
547
548         ctx->sc_pc = mctx->sc_pc;
549         for (i = 0; i < 32; ++i) {
550                 ctx->sc_regs[i] = mctx->sc_regs[i];
551                 ctx->sc_fpregs[i] = mctx->sc_fpregs[i];
552         }
553 }       
554
555 gpointer
556 mono_arch_ip_from_context (void *sigctx)
557 {
558         struct sigcontext *ctx = (struct sigcontext *)sigctx;
559         return (gpointer)(guint32)ctx->sc_pc;
560 }
561
562 /*
563  * This is the function called from the signal handler
564  */
565 gboolean
566 mono_arch_handle_exception (void *ctx, gpointer obj, gboolean test_only)
567 {
568         MonoContext mctx;
569         gboolean result;
570         
571         mono_arch_sigctx_to_monoctx (ctx, &mctx);
572 #ifdef DEBUG_EXCEPTIONS
573         g_print ("mono_arch_handle_exception: pc=%p\n", (void *) mctx.sc_pc);
574 #endif
575 #ifdef CUSTOM_EXCEPTION_HANDLING
576         result = arch_handle_exception (&mctx, obj, test_only);
577 #else
578         mono_handle_exception (&mctx, obj, (gpointer)mctx.sc_pc, test_only);
579         result = TRUE;
580 #endif
581
582 #ifdef DEBUG_EXCEPTIONS
583         g_print ("mono_arch_handle_exception: restore pc=%p\n", (void *)mctx.sc_pc);
584 #endif
585         /* restore the context so that returning from the signal handler
586          * will invoke the catch clause 
587          */
588         mono_arch_monoctx_to_sigctx (&mctx, ctx);
589
590         return result;
591 }
592
593 #ifdef CUSTOM_EXCEPTION_HANDLING
594 /**
595  * arch_handle_exception:
596  * @ctx: saved processor state
597  * @obj: the exception object
598  * @test_only: only test if the exception is caught, but dont call handlers
599  *
600  *
601  */
602 static gboolean
603 arch_handle_exception (MonoContext *ctx, gpointer obj, gboolean test_only)
604 {
605         MonoDomain *domain = mono_domain_get ();
606         MonoJitInfo *ji, rji;
607         static int (*call_filter) (MonoContext *, gpointer, gpointer) = NULL;
608         MonoJitTlsData *jit_tls = TlsGetValue (mono_jit_tls_id);
609         MonoLMF *lmf = jit_tls->lmf;            
610         GList *trace_ips = NULL;
611         MonoException *mono_ex;
612         MonoArray *initial_trace_ips = NULL;
613         int frame_count = 0;
614         gboolean has_dynamic_methods = FALSE;
615
616         g_assert (ctx != NULL);
617         if (!obj) {
618                 MonoException *ex = mono_get_exception_null_reference ();
619                 ex->message = mono_string_new (domain, 
620                         "Object reference not set to an instance of an object");
621                 obj = (MonoObject *)ex;
622         } 
623
624         if (mono_object_isinst (obj, mono_defaults.exception_class)) {
625                 mono_ex = (MonoException*)obj;
626                 initial_trace_ips = mono_ex->trace_ips;
627         } else {
628                 mono_ex = NULL;
629         }
630
631
632         if (!call_filter)
633                 call_filter = mono_arch_get_call_filter ();
634
635         g_assert (jit_tls->end_of_stack);
636         g_assert (jit_tls->abort_func);
637
638         if (!test_only) {
639                 MonoContext ctx_cp = *ctx;
640                 setup_context (&ctx_cp);
641                 if (mono_jit_trace_calls != NULL)
642                         g_print ("EXCEPTION handling: %s\n", mono_object_class (obj)->name);
643                 if (!arch_handle_exception (&ctx_cp, obj, TRUE)) {
644                         if (mono_break_on_exc)
645                                 G_BREAKPOINT ();
646                         mono_unhandled_exception (obj);
647                 }
648         }
649
650         memset (&rji, 0, sizeof (rji));
651
652         while (1) {
653                 MonoContext new_ctx;
654
655                 setup_context (&new_ctx);
656                 ji = mono_arch_find_jit_info (domain, jit_tls, &rji, &rji, ctx, &new_ctx, 
657                                               NULL, &lmf, NULL, NULL);
658                 if (!ji) {
659                         g_warning ("Exception inside function without unwind info");
660                         g_assert_not_reached ();
661                 }
662
663                 if (ji != (gpointer)-1) {
664                         frame_count ++;
665                         
666                         if (test_only && ji->method->wrapper_type != MONO_WRAPPER_RUNTIME_INVOKE && mono_ex) {
667                                 /* 
668                                  * Avoid overwriting the stack trace if the exception is
669                                  * rethrown. Also avoid giant stack traces during a stack
670                                  * overflow.
671                                  */
672                                 if (!initial_trace_ips && (frame_count < 1000)) {
673                                         trace_ips = g_list_prepend (trace_ips, MONO_CONTEXT_GET_IP (ctx));
674
675                                 }
676                         }
677
678                         if (ji->method->dynamic)
679                                 has_dynamic_methods = TRUE;
680
681                         if (ji->num_clauses) {
682                                 int i;
683                                 
684                                 g_assert (ji->clauses);
685                         
686                                 for (i = 0; i < ji->num_clauses; i++) {
687                                         MonoJitExceptionInfo *ei = &ji->clauses [i];
688                                         gboolean filtered = FALSE;
689
690                                         if (ei->try_start <= MONO_CONTEXT_GET_IP (ctx) && 
691                                             MONO_CONTEXT_GET_IP (ctx) <= ei->try_end) { 
692                                                 /* catch block */
693
694                                                 if ((ei->flags == MONO_EXCEPTION_CLAUSE_NONE) || (ei->flags == MONO_EXCEPTION_CLAUSE_FILTER)) {
695                                                         /* store the exception object int cfg->excvar */
696                                                         g_assert (ei->exvar_offset);
697                                                         /* need to use the frame pointer (mips_fp): methods with clauses always have mips_fp */
698                                                         *((gpointer *)(void*)((char *)(ctx->sc_regs [mips_fp]) + ei->exvar_offset)) = obj;
699                                                 }
700
701                                                 if (ei->flags == MONO_EXCEPTION_CLAUSE_FILTER)
702                                                         filtered = call_filter (ctx, ei->data.filter, mono_ex);
703
704                                                 if ((ei->flags == MONO_EXCEPTION_CLAUSE_NONE && 
705                                                      mono_object_isinst (obj, ei->data.catch_class)) || filtered) {
706                                                         if (test_only) {
707                                                                 if (mono_ex && !initial_trace_ips) {
708                                                                         trace_ips = g_list_reverse (trace_ips);
709                                                                         mono_ex->trace_ips = glist_to_array (trace_ips, mono_defaults.int_class);
710                                                                         if (has_dynamic_methods)
711                                                                                 /* These methods could go away anytime, so compute the stack trace now */
712                                                                                 mono_ex->stack_trace = ves_icall_System_Exception_get_trace (mono_ex);
713                                                                 }
714                                                                 g_list_free (trace_ips);
715                                                                 return TRUE;
716                                                         }
717                                                         if (mono_jit_trace_calls != NULL)
718                                                                 g_print ("EXCEPTION: catch found at clause %d of %s\n", i, mono_method_full_name (ji->method, TRUE));
719                                                         /*g_print  ("stack for catch: %p\n", MONO_CONTEXT_GET_BP (ctx));*/
720                                                         MONO_CONTEXT_SET_IP (ctx, ei->handler_start);
721                                                         jit_tls->lmf = lmf;
722                                                         return 0;
723                                                 }
724                                                 if (!test_only && ei->try_start <= MONO_CONTEXT_GET_IP (ctx) && 
725                                                     MONO_CONTEXT_GET_IP (ctx) < ei->try_end &&
726                                                     (ei->flags & MONO_EXCEPTION_CLAUSE_FINALLY)) {
727                                                         if (mono_jit_trace_calls != NULL)
728                                                                 g_print ("EXCEPTION: finally clause %d of %s\n", i, mono_method_full_name (ji->method, TRUE));
729                                                         call_filter (ctx, ei->handler_start, NULL);
730                                                 }
731                                                 
732                                         }
733                                 }
734                         }
735                 }
736
737                 *ctx = new_ctx;
738                 setup_context (ctx);
739
740                 if ((ji == (gpointer)-1) || MONO_CONTEXT_GET_BP (ctx) >= jit_tls->end_of_stack) {
741                         if (!test_only) {
742                                 jit_tls->lmf = lmf;
743                                 jit_tls->abort_func (obj);
744                                 g_assert_not_reached ();
745                         } else {
746                                 if (mono_ex && !initial_trace_ips) {
747                                         trace_ips = g_list_reverse (trace_ips);
748                                         mono_ex->trace_ips = glist_to_array (trace_ips, mono_defaults.int_class);
749                                         if (has_dynamic_methods)
750                                                 /* These methods could go away anytime, so compute the stack trace now */
751                                                 mono_ex->stack_trace = ves_icall_System_Exception_get_trace (mono_ex);
752                                 }
753                                 g_list_free (trace_ips);
754                                 return FALSE;
755                         }
756                 }
757         }
758
759         g_assert_not_reached ();
760 }
761 #endif /* CUSTOM_EXCEPTION_HANDLING */
762
763 gboolean
764 mono_arch_has_unwind_info (gconstpointer addr)
765 {
766         return FALSE;
767 }
768