Fri May 14 14:28:22 CEST 2004 Paolo Molaro <lupus@ximian.com>
[mono.git] / mono / mini / exceptions-x86.c
1 /*
2  * exceptions-x86.c: exception support for x86
3  *
4  * Authors:
5  *   Dietmar Maurer (dietmar@ximian.com)
6  *
7  * (C) 2001 Ximian, Inc.
8  */
9
10 #include <config.h>
11 #include <glib.h>
12 #include <signal.h>
13 #include <string.h>
14
15 #include <mono/arch/x86/x86-codegen.h>
16 #include <mono/metadata/appdomain.h>
17 #include <mono/metadata/tabledefs.h>
18 #include <mono/metadata/threads.h>
19 #include <mono/metadata/debug-helpers.h>
20 #include <mono/metadata/exception.h>
21 #include <mono/metadata/gc-internal.h>
22 #include <mono/metadata/mono-debug.h>
23
24 #include "mini.h"
25 #include "mini-x86.h"
26
27 #ifdef PLATFORM_WIN32
28 static MonoW32ExceptionHandler fpe_handler;
29 static MonoW32ExceptionHandler ill_handler;
30 static MonoW32ExceptionHandler segv_handler;
31
32 static LPTOP_LEVEL_EXCEPTION_FILTER old_handler;
33
34 #define W32_SEH_HANDLE_EX(_ex) \
35         if (_ex##_handler) _ex##_handler((int)sctx)
36
37 /*
38  * Unhandled Exception Filter
39  * Top-level per-process exception handler.
40  */
41 LONG CALLBACK seh_handler(EXCEPTION_POINTERS* ep)
42 {
43         EXCEPTION_RECORD* er;
44         CONTEXT* ctx;
45         struct sigcontext* sctx;
46         LONG res;
47
48         res = EXCEPTION_CONTINUE_EXECUTION;
49
50         er = ep->ExceptionRecord;
51         ctx = ep->ContextRecord;
52         sctx = g_malloc(sizeof(struct sigcontext));
53
54         /* Copy Win32 context to UNIX style context */
55         sctx->eax = ctx->Eax;
56         sctx->ebx = ctx->Ebx;
57         sctx->ecx = ctx->Ecx;
58         sctx->edx = ctx->Edx;
59         sctx->ebp = ctx->Ebp;
60         sctx->esp = ctx->Esp;
61         sctx->esi = ctx->Esi;
62         sctx->edi = ctx->Edi;
63         sctx->eip = ctx->Eip;
64
65         switch (er->ExceptionCode) {
66         case EXCEPTION_ACCESS_VIOLATION:
67                 W32_SEH_HANDLE_EX(segv);
68                 break;
69         case EXCEPTION_ILLEGAL_INSTRUCTION:
70                 W32_SEH_HANDLE_EX(ill);
71                 break;
72         case EXCEPTION_INT_DIVIDE_BY_ZERO:
73         case EXCEPTION_INT_OVERFLOW:
74         case EXCEPTION_FLT_DIVIDE_BY_ZERO:
75         case EXCEPTION_FLT_OVERFLOW:
76         case EXCEPTION_FLT_UNDERFLOW:
77         case EXCEPTION_FLT_INEXACT_RESULT:
78                 W32_SEH_HANDLE_EX(fpe);
79                 break;
80         default:
81                 break;
82         }
83
84         /* Copy context back */
85         ctx->Eax = sctx->eax;
86         ctx->Ebx = sctx->ebx;
87         ctx->Ecx = sctx->ecx;
88         ctx->Edx = sctx->edx;
89         ctx->Ebp = sctx->ebp;
90         ctx->Esp = sctx->esp;
91         ctx->Esi = sctx->esi;
92         ctx->Edi = sctx->edi;
93         ctx->Eip = sctx->eip;
94
95         return res;
96 }
97
98 void win32_seh_init()
99 {
100         old_handler = SetUnhandledExceptionFilter(seh_handler);
101 }
102
103 void win32_seh_cleanup()
104 {
105         if (old_handler) SetUnhandledExceptionFilter(old_handler);
106 }
107
108 void win32_seh_set_handler(int type, MonoW32ExceptionHandler handler)
109 {
110         switch (type) {
111         case SIGFPE:
112                 fpe_handler = handler;
113                 break;
114         case SIGILL:
115                 ill_handler = handler;
116                 break;
117         case SIGSEGV:
118                 segv_handler = handler;
119                 break;
120         default:
121                 break;
122         }
123 }
124
125 #endif /* PLATFORM_WIN32 */
126
127
128 #ifdef MONO_USE_EXC_TABLES
129
130 /*************************************/
131 /*    STACK UNWINDING STUFF          */
132 /*************************************/
133
134 /* These definitions are from unwind-dw2.c in glibc 2.2.5 */
135
136 /* For x86 */
137 #define DWARF_FRAME_REGISTERS 17
138
139 typedef struct frame_state
140 {
141   void *cfa;
142   void *eh_ptr;
143   long cfa_offset;
144   long args_size;
145   long reg_or_offset[DWARF_FRAME_REGISTERS+1];
146   unsigned short cfa_reg;
147   unsigned short retaddr_column;
148   char saved[DWARF_FRAME_REGISTERS+1];
149 } frame_state;
150
151 static long
152 get_sigcontext_reg (struct sigcontext *ctx, int dwarf_regnum)
153 {
154         switch (dwarf_regnum) {
155         case X86_EAX:
156                 return ctx->SC_EAX;
157         case X86_EBX:
158                 return ctx->SC_EBX;
159         case X86_ECX:
160                 return ctx->SC_ECX;
161         case X86_EDX:
162                 return ctx->SC_EDX;
163         case X86_ESI:
164                 return ctx->SC_ESI;
165         case X86_EDI:
166                 return ctx->SC_EDI;
167         case X86_EBP:
168                 return ctx->SC_EBP;
169         case X86_ESP:
170                 return ctx->SC_ESP;
171         default:
172                 g_assert_not_reached ();
173         }
174
175         return 0;
176 }
177
178 static void
179 set_sigcontext_reg (struct sigcontext *ctx, int dwarf_regnum, long value)
180 {
181         switch (dwarf_regnum) {
182         case X86_EAX:
183                 ctx->SC_EAX = value;
184                 break;
185         case X86_EBX:
186                 ctx->SC_EBX = value;
187                 break;
188         case X86_ECX:
189                 ctx->SC_ECX = value;
190                 break;
191         case X86_EDX:
192                 ctx->SC_EDX = value;
193                 break;
194         case X86_ESI:
195                 ctx->SC_ESI = value;
196                 break;
197         case X86_EDI:
198                 ctx->SC_EDI = value;
199                 break;
200         case X86_EBP:
201                 ctx->SC_EBP = value;
202                 break;
203         case X86_ESP:
204                 ctx->SC_ESP = value;
205                 break;
206         case 8:
207                 ctx->SC_EIP = value;
208                 break;
209         default:
210                 g_assert_not_reached ();
211         }
212 }
213
214 typedef struct frame_state * (*framesf) (void *, struct frame_state *);
215
216 static framesf frame_state_for = NULL;
217
218 static gboolean inited = FALSE;
219
220 typedef char ** (*get_backtrace_symbols_type) (void *__const *__array, int __size);
221
222 static get_backtrace_symbols_type get_backtrace_symbols = NULL;
223
224 static void
225 init_frame_state_for (void)
226 {
227         GModule *module;
228
229         /*
230          * There are two versions of __frame_state_for: one in libgcc.a and the
231          * other in glibc.so. We need the version from glibc.
232          * For more info, see this:
233          * http://gcc.gnu.org/ml/gcc/2002-08/msg00192.html
234          */
235         if ((module = g_module_open ("libc.so.6", G_MODULE_BIND_LAZY))) {
236         
237                 if (!g_module_symbol (module, "__frame_state_for", (gpointer*)&frame_state_for))
238                         frame_state_for = NULL;
239
240                 if (!g_module_symbol (module, "backtrace_symbols", (gpointer*)&get_backtrace_symbols)) {
241                         get_backtrace_symbols = NULL;
242                         frame_state_for = NULL;
243                 }
244
245                 g_module_close (module);
246         }
247
248         inited = TRUE;
249 }
250
251 /* mono_arch_has_unwind_info:
252  *
253  * Tests if a function has an DWARF exception table able to restore
254  * all caller saved registers. 
255  */
256 gboolean
257 mono_arch_has_unwind_info (gconstpointer addr)
258 {
259         struct frame_state state_in;
260         struct frame_state *res;
261
262         if (!inited) 
263                 init_frame_state_for ();
264         
265         if (!frame_state_for)
266                 return FALSE;
267
268         g_assert (addr);
269
270         memset (&state_in, 0, sizeof (state_in));
271
272         /* offset 10 is just a guess, but it works for all methods tested */
273         if ((res = frame_state_for ((char *)addr + 10, &state_in))) {
274
275                 if (res->saved [X86_EBX] == 1 &&
276                     res->saved [X86_EDI] == 1 &&
277                     res->saved [X86_EBP] == 1 &&
278                     res->saved [X86_ESI] == 1)
279                         return TRUE;
280         }
281
282         return FALSE;
283 }
284
285 struct stack_frame
286 {
287   void *next;
288   void *return_address;
289 };
290
291 static MonoJitInfo *
292 x86_unwind_native_frame (MonoDomain *domain, MonoJitTlsData *jit_tls, struct sigcontext *ctx, 
293                          struct sigcontext *new_ctx, MonoLMF *lmf, char **trace)
294 {
295         struct stack_frame *frame;
296         gpointer max_stack;
297         MonoJitInfo *ji;
298         struct frame_state state_in;
299         struct frame_state *res;
300
301         if (trace)
302                 *trace = NULL;
303
304         if (!inited) 
305                 init_frame_state_for ();
306
307         if (!frame_state_for)
308                 return FALSE;
309
310         frame = MONO_CONTEXT_GET_BP (ctx);
311
312         max_stack = lmf && lmf->method ? lmf : jit_tls->end_of_stack;
313
314         *new_ctx = *ctx;
315
316         memset (&state_in, 0, sizeof (state_in));
317
318         while ((gpointer)frame->next < (gpointer)max_stack) {
319                 gpointer ip, addr = frame->return_address;
320                 void *cfa;
321                 char *tmp, **symbols;
322
323                 if (trace) {
324                         ip = MONO_CONTEXT_GET_IP (new_ctx);
325                         symbols = get_backtrace_symbols (&ip, 1);
326                         if (*trace)
327                                 tmp = g_strdup_printf ("%s\nin (unmanaged) %s", *trace, symbols [0]);
328                         else
329                                 tmp = g_strdup_printf ("in (unmanaged) %s", symbols [0]);
330
331                         free (symbols);
332                         g_free (*trace);
333                         *trace = tmp;
334                 }
335
336                 if ((res = frame_state_for (addr, &state_in))) {        
337                         int i;
338
339                         cfa = (gint8*) (get_sigcontext_reg (new_ctx, res->cfa_reg) + res->cfa_offset);
340                         frame = (struct stack_frame *)((gint8*)cfa - 8);
341                         for (i = 0; i < DWARF_FRAME_REGISTERS + 1; i++) {
342                                 int how = res->saved[i];
343                                 long val;
344                                 g_assert ((how == 0) || (how == 1));
345                         
346                                 if (how == 1) {
347                                         val = * (long*) ((gint8*)cfa + res->reg_or_offset[i]);
348                                         set_sigcontext_reg (new_ctx, i, val);
349                                 }
350                         }
351                         new_ctx->SC_ESP = (long)cfa;
352
353                         if (res->saved [X86_EBX] == 1 &&
354                             res->saved [X86_EDI] == 1 &&
355                             res->saved [X86_EBP] == 1 &&
356                             res->saved [X86_ESI] == 1 &&
357                             (ji = mono_jit_info_table_find (domain, frame->return_address))) {
358                                 //printf ("FRAME CFA %s\n", mono_method_full_name (ji->method, TRUE));
359                                 return ji;
360                         }
361
362                 } else {
363                         //printf ("FRAME %p %p %p\n", frame, MONO_CONTEXT_GET_IP (new_ctx), mono_jit_info_table_find (domain, MONO_CONTEXT_GET_IP (new_ctx)));
364
365                         MONO_CONTEXT_SET_IP (new_ctx, frame->return_address);
366                         frame = frame->next;
367                         MONO_CONTEXT_SET_BP (new_ctx, frame);
368
369                         /* stop if !frame or when we detect an unexpected managed frame */
370                         if (!frame || mono_jit_info_table_find (domain, frame->return_address)) {
371                                 if (trace) {
372                                         g_free (*trace);
373                                         *trace = NULL;
374                                 }
375                                 return NULL;
376                         }
377                 }
378         }
379
380         //if (!lmf)
381         //g_assert_not_reached ();
382
383         if (trace) {
384                 g_free (*trace);
385                 *trace = NULL;
386         }
387         return NULL;
388 }
389
390 #endif
391
392 /*
393  * mono_arch_get_restore_context:
394  *
395  * Returns a pointer to a method which restores a previously saved sigcontext.
396  */
397 gpointer
398 mono_arch_get_restore_context (void)
399 {
400         static guint8 *start = NULL;
401         guint8 *code;
402
403         if (start)
404                 return start;
405
406         /* restore_contect (struct sigcontext *ctx) */
407         /* we do not restore X86_EAX, X86_EDX */
408
409         start = code = g_malloc (1024);
410         
411         /* load ctx */
412         x86_mov_reg_membase (code, X86_EAX, X86_ESP, 4, 4);
413
414         /* get return address, stored in EDX */
415         x86_mov_reg_membase (code, X86_EDX, X86_EAX,  G_STRUCT_OFFSET (struct sigcontext, SC_EIP), 4);
416         /* restore EBX */
417         x86_mov_reg_membase (code, X86_EBX, X86_EAX,  G_STRUCT_OFFSET (struct sigcontext, SC_EBX), 4);
418         /* restore EDI */
419         x86_mov_reg_membase (code, X86_EDI, X86_EAX,  G_STRUCT_OFFSET (struct sigcontext, SC_EDI), 4);
420         /* restore ESI */
421         x86_mov_reg_membase (code, X86_ESI, X86_EAX,  G_STRUCT_OFFSET (struct sigcontext, SC_ESI), 4);
422         /* restore ESP */
423         x86_mov_reg_membase (code, X86_ESP, X86_EAX,  G_STRUCT_OFFSET (struct sigcontext, SC_ESP), 4);
424         /* restore EBP */
425         x86_mov_reg_membase (code, X86_EBP, X86_EAX,  G_STRUCT_OFFSET (struct sigcontext, SC_EBP), 4);
426
427         /* jump to the saved IP */
428         x86_jump_reg (code, X86_EDX);
429
430         return start;
431 }
432
433 /*
434  * mono_arch_get_call_filter:
435  *
436  * Returns a pointer to a method which calls an exception filter. We
437  * also use this function to call finally handlers (we pass NULL as 
438  * @exc object in this case).
439  */
440 gpointer
441 mono_arch_get_call_filter (void)
442 {
443         static guint8 start [64];
444         static int inited = 0;
445         guint8 *code;
446
447         if (inited)
448                 return start;
449
450         inited = 1;
451         /* call_filter (struct sigcontext *ctx, unsigned long eip) */
452         code = start;
453
454         x86_push_reg (code, X86_EBP);
455         x86_mov_reg_reg (code, X86_EBP, X86_ESP, 4);
456         x86_push_reg (code, X86_EBX);
457         x86_push_reg (code, X86_EDI);
458         x86_push_reg (code, X86_ESI);
459
460         /* load ctx */
461         x86_mov_reg_membase (code, X86_EAX, X86_EBP, 8, 4);
462         /* load eip */
463         x86_mov_reg_membase (code, X86_ECX, X86_EBP, 12, 4);
464         /* save EBP */
465         x86_push_reg (code, X86_EBP);
466
467         /* set new EBP */
468         x86_mov_reg_membase (code, X86_EBP, X86_EAX,  G_STRUCT_OFFSET (struct sigcontext, SC_EBP), 4);
469         /* restore registers used by global register allocation (EBX & ESI) */
470         x86_mov_reg_membase (code, X86_EBX, X86_EAX,  G_STRUCT_OFFSET (struct sigcontext, SC_EBX), 4);
471         x86_mov_reg_membase (code, X86_ESI, X86_EAX,  G_STRUCT_OFFSET (struct sigcontext, SC_ESI), 4);
472         x86_mov_reg_membase (code, X86_EDI, X86_EAX,  G_STRUCT_OFFSET (struct sigcontext, SC_EDI), 4);
473
474         /* call the handler */
475         x86_call_reg (code, X86_ECX);
476
477         /* restore EBP */
478         x86_pop_reg (code, X86_EBP);
479
480         /* restore saved regs */
481         x86_pop_reg (code, X86_ESI);
482         x86_pop_reg (code, X86_EDI);
483         x86_pop_reg (code, X86_EBX);
484         x86_leave (code);
485         x86_ret (code);
486
487         g_assert ((code - start) < 64);
488         return start;
489 }
490
491 static void
492 throw_exception (unsigned long eax, unsigned long ecx, unsigned long edx, unsigned long ebx,
493                  unsigned long esi, unsigned long edi, unsigned long ebp, MonoObject *exc,
494                  unsigned long eip,  unsigned long esp)
495 {
496         static void (*restore_context) (struct sigcontext *);
497         struct sigcontext ctx;
498
499         if (!restore_context)
500                 restore_context = mono_arch_get_restore_context ();
501
502         /* adjust eip so that it point into the call instruction */
503         eip -= 1;
504
505         /* Pop argument and return address */
506         ctx.SC_ESP = esp + (2 * sizeof (gpointer));
507         ctx.SC_EIP = eip;
508         ctx.SC_EBP = ebp;
509         ctx.SC_EDI = edi;
510         ctx.SC_ESI = esi;
511         ctx.SC_EBX = ebx;
512         ctx.SC_EDX = edx;
513         ctx.SC_ECX = ecx;
514         ctx.SC_EAX = eax;
515         
516         mono_arch_handle_exception (&ctx, exc, FALSE);
517         restore_context (&ctx);
518
519         g_assert_not_reached ();
520 }
521
522 /**
523  * mono_arch_get_throw_exception:
524  *
525  * Returns a function pointer which can be used to raise 
526  * exceptions. The returned function has the following 
527  * signature: void (*func) (MonoException *exc); 
528  * For example to raise an arithmetic exception you can use:
529  *
530  * x86_push_imm (code, mono_get_exception_arithmetic ()); 
531  * x86_call_code (code, arch_get_throw_exception ()); 
532  *
533  */
534 gpointer 
535 mono_arch_get_throw_exception (void)
536 {
537         static guint8 start [24];
538         static int inited = 0;
539         guint8 *code;
540
541         if (inited)
542                 return start;
543
544         inited = 1;
545         code = start;
546
547         x86_push_reg (code, X86_ESP);
548         x86_push_membase (code, X86_ESP, 4); /* IP */
549         x86_push_membase (code, X86_ESP, 12); /* exception */
550         x86_push_reg (code, X86_EBP);
551         x86_push_reg (code, X86_EDI);
552         x86_push_reg (code, X86_ESI);
553         x86_push_reg (code, X86_EBX);
554         x86_push_reg (code, X86_EDX);
555         x86_push_reg (code, X86_ECX);
556         x86_push_reg (code, X86_EAX);
557         x86_call_code (code, throw_exception);
558         /* we should never reach this breakpoint */
559         x86_breakpoint (code);
560
561         g_assert ((code - start) < 24);
562         return start;
563 }
564
565 /**
566  * mono_arch_get_throw_exception_by_name:
567  *
568  * Returns a function pointer which can be used to raise 
569  * corlib exceptions. The returned function has the following 
570  * signature: void (*func) (char *exc_name); 
571  * For example to raise an arithmetic exception you can use:
572  *
573  * x86_push_imm (code, "ArithmeticException"); 
574  * x86_call_code (code, arch_get_throw_exception_by_name ()); 
575  *
576  */
577 gpointer 
578 mono_arch_get_throw_exception_by_name (void)
579 {
580         static guint8 start [32];
581         static int inited = 0;
582         guint8 *code;
583
584         if (inited)
585                 return start;
586
587         inited = 1;
588         code = start;
589
590         x86_push_membase (code, X86_ESP, 4); /* exception name */
591         x86_push_imm (code, "System");
592         x86_push_imm (code, mono_defaults.exception_class->image);
593         x86_call_code (code, mono_exception_from_name);
594         x86_alu_reg_imm (code, X86_ADD, X86_ESP, 12);
595         /* save the newly create object (overwrite exception name)*/
596         x86_mov_membase_reg (code, X86_ESP, 4, X86_EAX, 4);
597         x86_jump_code (code, mono_arch_get_throw_exception ());
598
599         g_assert ((code - start) < 32);
600
601         return start;
602 }
603
604 /* mono_arch_find_jit_info:
605  *
606  * This function is used to gather information from @ctx. It return the 
607  * MonoJitInfo of the corresponding function, unwinds one stack frame and
608  * stores the resulting context into @new_ctx. It also stores a string 
609  * describing the stack location into @trace (if not NULL), and modifies
610  * the @lmf if necessary. @native_offset return the IP offset from the 
611  * start of the function or -1 if that info is not available.
612  */
613 MonoJitInfo *
614 mono_arch_find_jit_info (MonoDomain *domain, MonoJitTlsData *jit_tls, MonoJitInfo *res, MonoJitInfo *prev_ji, MonoContext *ctx, 
615                          MonoContext *new_ctx, char **trace, MonoLMF **lmf, int *native_offset,
616                          gboolean *managed)
617 {
618         MonoJitInfo *ji;
619         gpointer ip = MONO_CONTEXT_GET_IP (ctx);
620
621         /* Avoid costly table lookup during stack overflow */
622         if (prev_ji && (ip > prev_ji->code_start && ((guint8*)ip < ((guint8*)prev_ji->code_start) + prev_ji->code_size)))
623                 ji = prev_ji;
624         else
625                 ji = mono_jit_info_table_find (domain, ip);
626
627         if (trace)
628                 *trace = NULL;
629
630         if (native_offset)
631                 *native_offset = -1;
632
633         if (managed)
634                 *managed = FALSE;
635
636         if (ji != NULL) {
637                 char *source_location, *tmpaddr, *fname;
638                 gint32 address, iloffset;
639                 int offset;
640
641                 *new_ctx = *ctx;
642
643                 address = (char *)ip - (char *)ji->code_start;
644
645                 if (native_offset)
646                         *native_offset = address;
647
648                 if (managed)
649                         if (!ji->method->wrapper_type)
650                                 *managed = TRUE;
651
652                 if (trace) {
653                         source_location = mono_debug_source_location_from_address (ji->method, address, NULL, domain);
654                         iloffset = mono_debug_il_offset_from_address (ji->method, address, domain);
655
656                         if (iloffset < 0)
657                                 tmpaddr = g_strdup_printf ("<0x%05x>", address);
658                         else
659                                 tmpaddr = g_strdup_printf ("[0x%05x]", iloffset);
660                 
661                         fname = mono_method_full_name (ji->method, TRUE);
662
663                         if (source_location)
664                                 *trace = g_strdup_printf ("in %s (at %s) %s", tmpaddr, source_location, fname);
665                         else
666                                 *trace = g_strdup_printf ("in %s %s", tmpaddr, fname);
667
668                         g_free (fname);
669                         g_free (source_location);
670                         g_free (tmpaddr);
671                 }
672
673                 /*
674                  * Some managed methods like pinvoke wrappers might have save_lmf set.
675                  * In this case, register save/restore code is not generated by the 
676                  * JIT, so we have to restore callee saved registers from the lmf.
677                  */
678                 if (ji->method->save_lmf) {
679                         /* 
680                          * We only need to do this if the exception was raised in managed
681                          * code, since otherwise the lmf was already popped of the stack.
682                          */
683                         if (*lmf && (MONO_CONTEXT_GET_BP (ctx) >= (gpointer)(*lmf)->ebp)) {
684                                 new_ctx->SC_ESI = (*lmf)->esi;
685                                 new_ctx->SC_EDI = (*lmf)->edi;
686                                 new_ctx->SC_EBX = (*lmf)->ebx;
687                         }
688                 }
689                 else {
690                         offset = -1;
691                         /* restore caller saved registers */
692                         if (ji->used_regs & X86_EBX_MASK) {
693                                 new_ctx->SC_EBX = *((int *)ctx->SC_EBP + offset);
694                                 offset--;
695                         }
696                         if (ji->used_regs & X86_EDI_MASK) {
697                                 new_ctx->SC_EDI = *((int *)ctx->SC_EBP + offset);
698                                 offset--;
699                         }
700                         if (ji->used_regs & X86_ESI_MASK) {
701                                 new_ctx->SC_ESI = *((int *)ctx->SC_EBP + offset);
702                         }
703                 }
704
705                 if (*lmf && (MONO_CONTEXT_GET_BP (ctx) >= (gpointer)(*lmf)->ebp)) {
706                         /* remove any unused lmf */
707                         *lmf = (*lmf)->previous_lmf;
708                 }
709
710                 /* Pop EBP and the return address */
711                 new_ctx->SC_ESP = ctx->SC_EBP + (2 * sizeof (gpointer));
712                 /* we substract 1, so that the IP points into the call instruction */
713                 new_ctx->SC_EIP = *((int *)ctx->SC_EBP + 1) - 1;
714                 new_ctx->SC_EBP = *((int *)ctx->SC_EBP);
715
716                 *res = *ji;
717                 return res;
718 #ifdef MONO_USE_EXC_TABLES
719         } else if ((ji = x86_unwind_native_frame (domain, jit_tls, ctx, new_ctx, *lmf, trace))) {
720                 *res = *ji;             
721                 return res;
722 #endif
723         } else if (*lmf) {
724                 
725                 *new_ctx = *ctx;
726
727                 if (!(*lmf)->method)
728                         return (gpointer)-1;
729
730                 if (trace)
731                         *trace = g_strdup_printf ("in (unmanaged) %s", mono_method_full_name ((*lmf)->method, TRUE));
732                 
733                 if ((ji = mono_jit_info_table_find (domain, (gpointer)(*lmf)->eip))) {
734                         *res = *ji;
735                 } else {
736                         memset (res, 0, sizeof (MonoJitInfo));
737                         res->method = (*lmf)->method;
738                 }
739
740                 new_ctx->SC_ESI = (*lmf)->esi;
741                 new_ctx->SC_EDI = (*lmf)->edi;
742                 new_ctx->SC_EBX = (*lmf)->ebx;
743                 new_ctx->SC_EBP = (*lmf)->ebp;
744                 new_ctx->SC_EIP = (*lmf)->eip;
745                 /* the lmf is always stored on the stack, so the following
746                  * expression points to a stack location which can be used as ESP */
747                 new_ctx->SC_ESP = (unsigned long)&((*lmf)->eip);
748
749                 *lmf = (*lmf)->previous_lmf;
750
751                 return res;
752                 
753         }
754
755         return NULL;
756 }
757
758 /**
759  * mono_arch_handle_exception:
760  *
761  * @ctx: saved processor state
762  * @obj: the exception object
763  */
764 gboolean
765 mono_arch_handle_exception (void *sigctx, gpointer obj, gboolean test_only)
766 {
767         return mono_handle_exception (sigctx, obj, test_only);
768 }