2010-07-05 Atsushi Enomoto <atsushi@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 #include <mono/utils/mono-mmap.h>
24
25 #include "mini.h"
26 #include "mini-x86.h"
27 #include "tasklets.h"
28 #include "debug-mini.h"
29
30 static gpointer signal_exception_trampoline;
31
32 gpointer
33 mono_x86_get_signal_exception_trampoline (MonoTrampInfo **info, gboolean aot) MONO_INTERNAL;
34
35 #ifdef TARGET_WIN32
36 static void (*restore_stack) (void *);
37
38 static MonoW32ExceptionHandler fpe_handler;
39 static MonoW32ExceptionHandler ill_handler;
40 static MonoW32ExceptionHandler segv_handler;
41
42 static LPTOP_LEVEL_EXCEPTION_FILTER old_handler;
43
44 #define W32_SEH_HANDLE_EX(_ex) \
45         if (_ex##_handler) _ex##_handler(0, er, sctx)
46
47 /*
48  * mono_win32_get_handle_stackoverflow (void):
49  *
50  * Returns a pointer to a method which restores the current context stack
51  * and calls handle_exceptions, when done restores the original stack.
52  */
53 static gpointer
54 mono_win32_get_handle_stackoverflow (void)
55 {
56         static guint8 *start = NULL;
57         guint8 *code;
58
59         if (start)
60                 return start;
61
62         /* restore_contect (void *sigctx) */
63         start = code = mono_global_codeman_reserve (128);
64
65         /* load context into ebx */
66         x86_mov_reg_membase (code, X86_EBX, X86_ESP, 4, 4);
67
68         /* move current stack into edi for later restore */
69         x86_mov_reg_reg (code, X86_EDI, X86_ESP, 4);
70
71         /* use the new freed stack from sigcontext */
72         x86_mov_reg_membase (code, X86_ESP, X86_EBX,  G_STRUCT_OFFSET (struct sigcontext, esp), 4);
73
74         /* get the current domain */
75         x86_call_code (code, mono_domain_get);
76
77         /* get stack overflow exception from domain object */
78         x86_mov_reg_membase (code, X86_EAX, X86_EAX, G_STRUCT_OFFSET (MonoDomain, stack_overflow_ex), 4);
79
80         /* call mono_arch_handle_exception (sctx, stack_overflow_exception_obj, FALSE) */
81         x86_push_imm (code, 0);
82         x86_push_reg (code, X86_EAX);
83         x86_push_reg (code, X86_EBX);
84         x86_call_code (code, mono_arch_handle_exception);
85
86         /* restore the SEH handler stack */
87         x86_mov_reg_reg (code, X86_ESP, X86_EDI, 4);
88
89         /* return */
90         x86_ret (code);
91
92         return start;
93 }
94
95 /* Special hack to workaround the fact that the
96  * when the SEH handler is called the stack is
97  * to small to recover.
98  *
99  * Stack walking part of this method is from mono_handle_exception
100  *
101  * The idea is simple; 
102  *  - walk the stack to free some space (64k)
103  *  - set esp to new stack location
104  *  - call mono_arch_handle_exception with stack overflow exception
105  *  - set esp to SEH handlers stack
106  *  - done
107  */
108 static void 
109 win32_handle_stack_overflow (EXCEPTION_POINTERS* ep, struct sigcontext *sctx) 
110 {
111         SYSTEM_INFO si;
112         DWORD page_size;
113         MonoDomain *domain = mono_domain_get ();
114         MonoJitInfo rji;
115         MonoJitTlsData *jit_tls = TlsGetValue (mono_jit_tls_id);
116         MonoLMF *lmf = jit_tls->lmf;            
117         MonoContext initial_ctx;
118         MonoContext ctx;
119         guint32 free_stack = 0;
120         StackFrameInfo frame;
121
122         /* convert sigcontext to MonoContext (due to reuse of stack walking helpers */
123         mono_arch_sigctx_to_monoctx (sctx, &ctx);
124         
125         /* get our os page size */
126         GetSystemInfo(&si);
127         page_size = si.dwPageSize;
128
129         /* Let's walk the stack to recover
130          * the needed stack space (if possible)
131          */
132         memset (&rji, 0, sizeof (rji));
133
134         initial_ctx = ctx;
135         free_stack = (guint8*)(MONO_CONTEXT_GET_BP (&ctx)) - (guint8*)(MONO_CONTEXT_GET_BP (&initial_ctx));
136
137         /* try to free 64kb from our stack */
138         do {
139                 MonoContext new_ctx;
140
141                 mono_arch_find_jit_info_ext (domain, jit_tls, &rji, &ctx, &new_ctx, &lmf, &frame);
142                 if (!frame.ji) {
143                         g_warning ("Exception inside function without unwind info");
144                         g_assert_not_reached ();
145                 }
146
147                 if (frame.ji != (gpointer)-1) {
148                         free_stack = (guint8*)(MONO_CONTEXT_GET_BP (&ctx)) - (guint8*)(MONO_CONTEXT_GET_BP (&initial_ctx));
149                 }
150
151                 /* todo: we should call abort if ji is -1 */
152                 ctx = new_ctx;
153         } while (free_stack < 64 * 1024 && frame.ji != (gpointer) -1);
154
155         /* convert into sigcontext to be used in mono_arch_handle_exception */
156         mono_arch_monoctx_to_sigctx (&ctx, sctx);
157
158         /* todo: install new stack-guard page */
159
160         /* use the new stack and call mono_arch_handle_exception () */
161         restore_stack (sctx);
162 }
163
164 /*
165  * Unhandled Exception Filter
166  * Top-level per-process exception handler.
167  */
168 LONG CALLBACK seh_handler(EXCEPTION_POINTERS* ep)
169 {
170         EXCEPTION_RECORD* er;
171         CONTEXT* ctx;
172         struct sigcontext* sctx;
173         LONG res;
174
175         res = EXCEPTION_CONTINUE_EXECUTION;
176
177         er = ep->ExceptionRecord;
178         ctx = ep->ContextRecord;
179         sctx = g_malloc(sizeof(struct sigcontext));
180
181         /* Copy Win32 context to UNIX style context */
182         sctx->eax = ctx->Eax;
183         sctx->ebx = ctx->Ebx;
184         sctx->ecx = ctx->Ecx;
185         sctx->edx = ctx->Edx;
186         sctx->ebp = ctx->Ebp;
187         sctx->esp = ctx->Esp;
188         sctx->esi = ctx->Esi;
189         sctx->edi = ctx->Edi;
190         sctx->eip = ctx->Eip;
191
192         switch (er->ExceptionCode) {
193         case EXCEPTION_STACK_OVERFLOW:
194                 win32_handle_stack_overflow (ep, sctx);
195                 break;
196         case EXCEPTION_ACCESS_VIOLATION:
197                 W32_SEH_HANDLE_EX(segv);
198                 break;
199         case EXCEPTION_ILLEGAL_INSTRUCTION:
200                 W32_SEH_HANDLE_EX(ill);
201                 break;
202         case EXCEPTION_INT_DIVIDE_BY_ZERO:
203         case EXCEPTION_INT_OVERFLOW:
204         case EXCEPTION_FLT_DIVIDE_BY_ZERO:
205         case EXCEPTION_FLT_OVERFLOW:
206         case EXCEPTION_FLT_UNDERFLOW:
207         case EXCEPTION_FLT_INEXACT_RESULT:
208                 W32_SEH_HANDLE_EX(fpe);
209                 break;
210         default:
211                 break;
212         }
213
214         /* Copy context back */
215         ctx->Eax = sctx->eax;
216         ctx->Ebx = sctx->ebx;
217         ctx->Ecx = sctx->ecx;
218         ctx->Edx = sctx->edx;
219         ctx->Ebp = sctx->ebp;
220         ctx->Esp = sctx->esp;
221         ctx->Esi = sctx->esi;
222         ctx->Edi = sctx->edi;
223         ctx->Eip = sctx->eip;
224
225         g_free (sctx);
226
227         return res;
228 }
229
230 void win32_seh_init()
231 {
232         /* install restore stack helper */
233         if (!restore_stack)
234                 restore_stack = mono_win32_get_handle_stackoverflow ();
235
236         old_handler = SetUnhandledExceptionFilter(seh_handler);
237 }
238
239 void win32_seh_cleanup()
240 {
241         if (old_handler) SetUnhandledExceptionFilter(old_handler);
242 }
243
244 void win32_seh_set_handler(int type, MonoW32ExceptionHandler handler)
245 {
246         switch (type) {
247         case SIGFPE:
248                 fpe_handler = handler;
249                 break;
250         case SIGILL:
251                 ill_handler = handler;
252                 break;
253         case SIGSEGV:
254                 segv_handler = handler;
255                 break;
256         default:
257                 break;
258         }
259 }
260
261 #endif /* TARGET_WIN32 */
262
263 /*
264  * mono_arch_get_restore_context:
265  *
266  * Returns a pointer to a method which restores a previously saved sigcontext.
267  */
268 gpointer
269 mono_arch_get_restore_context (MonoTrampInfo **info, gboolean aot)
270 {
271         guint8 *start = NULL;
272         guint8 *code;
273         MonoJumpInfo *ji = NULL;
274         GSList *unwind_ops = NULL;
275
276         /* restore_contect (MonoContext *ctx) */
277
278         start = code = mono_global_codeman_reserve (128);
279         
280         /* load ctx */
281         x86_mov_reg_membase (code, X86_EAX, X86_ESP, 4, 4);
282
283         /* get return address, stored in ECX */
284         x86_mov_reg_membase (code, X86_ECX, X86_EAX,  G_STRUCT_OFFSET (MonoContext, eip), 4);
285         /* restore EBX */
286         x86_mov_reg_membase (code, X86_EBX, X86_EAX,  G_STRUCT_OFFSET (MonoContext, ebx), 4);
287         /* restore EDI */
288         x86_mov_reg_membase (code, X86_EDI, X86_EAX,  G_STRUCT_OFFSET (MonoContext, edi), 4);
289         /* restore ESI */
290         x86_mov_reg_membase (code, X86_ESI, X86_EAX,  G_STRUCT_OFFSET (MonoContext, esi), 4);
291         /* restore ESP */
292         x86_mov_reg_membase (code, X86_ESP, X86_EAX,  G_STRUCT_OFFSET (MonoContext, esp), 4);
293         /* save the return addr to the restored stack */
294         x86_push_reg (code, X86_ECX);
295         /* restore EBP */
296         x86_mov_reg_membase (code, X86_EBP, X86_EAX,  G_STRUCT_OFFSET (MonoContext, ebp), 4);
297         /* restore ECX */
298         x86_mov_reg_membase (code, X86_ECX, X86_EAX,  G_STRUCT_OFFSET (MonoContext, ecx), 4);
299         /* restore EDX */
300         x86_mov_reg_membase (code, X86_EDX, X86_EAX,  G_STRUCT_OFFSET (MonoContext, edx), 4);
301         /* restore EAX */
302         x86_mov_reg_membase (code, X86_EAX, X86_EAX,  G_STRUCT_OFFSET (MonoContext, eax), 4);
303
304         /* jump to the saved IP */
305         x86_ret (code);
306
307         if (info)
308                 *info = mono_tramp_info_create (g_strdup_printf ("restore_context"), start, code - start, ji, unwind_ops);
309
310         return start;
311 }
312
313 /*
314  * mono_arch_get_call_filter:
315  *
316  * Returns a pointer to a method which calls an exception filter. We
317  * also use this function to call finally handlers (we pass NULL as 
318  * @exc object in this case).
319  */
320 gpointer
321 mono_arch_get_call_filter (MonoTrampInfo **info, gboolean aot)
322 {
323         guint8* start;
324         guint8 *code;
325         MonoJumpInfo *ji = NULL;
326         GSList *unwind_ops = NULL;
327
328         /* call_filter (MonoContext *ctx, unsigned long eip) */
329         start = code = mono_global_codeman_reserve (64);
330
331         x86_push_reg (code, X86_EBP);
332         x86_mov_reg_reg (code, X86_EBP, X86_ESP, 4);
333         x86_push_reg (code, X86_EBX);
334         x86_push_reg (code, X86_EDI);
335         x86_push_reg (code, X86_ESI);
336
337         /* load ctx */
338         x86_mov_reg_membase (code, X86_EAX, X86_EBP, 8, 4);
339         /* load eip */
340         x86_mov_reg_membase (code, X86_ECX, X86_EBP, 12, 4);
341         /* save EBP */
342         x86_push_reg (code, X86_EBP);
343
344         /* set new EBP */
345         x86_mov_reg_membase (code, X86_EBP, X86_EAX,  G_STRUCT_OFFSET (MonoContext, ebp), 4);
346         /* restore registers used by global register allocation (EBX & ESI) */
347         x86_mov_reg_membase (code, X86_EBX, X86_EAX,  G_STRUCT_OFFSET (MonoContext, ebx), 4);
348         x86_mov_reg_membase (code, X86_ESI, X86_EAX,  G_STRUCT_OFFSET (MonoContext, esi), 4);
349         x86_mov_reg_membase (code, X86_EDI, X86_EAX,  G_STRUCT_OFFSET (MonoContext, edi), 4);
350
351         /* align stack and save ESP */
352         x86_mov_reg_reg (code, X86_EDX, X86_ESP, 4);
353         x86_alu_reg_imm (code, X86_AND, X86_ESP, -MONO_ARCH_FRAME_ALIGNMENT);
354         g_assert (MONO_ARCH_FRAME_ALIGNMENT >= 8);
355         x86_alu_reg_imm (code, X86_SUB, X86_ESP, MONO_ARCH_FRAME_ALIGNMENT - 8);
356         x86_push_reg (code, X86_EDX);
357
358         /* call the handler */
359         x86_call_reg (code, X86_ECX);
360
361         /* restore ESP */
362         x86_pop_reg (code, X86_ESP);
363
364         /* restore EBP */
365         x86_pop_reg (code, X86_EBP);
366
367         /* restore saved regs */
368         x86_pop_reg (code, X86_ESI);
369         x86_pop_reg (code, X86_EDI);
370         x86_pop_reg (code, X86_EBX);
371         x86_leave (code);
372         x86_ret (code);
373
374         if (info)
375                 *info = mono_tramp_info_create (g_strdup_printf ("call_filter"), start, code - start, ji, unwind_ops);
376
377         g_assert ((code - start) < 64);
378         return start;
379 }
380
381 /*
382  * mono_x86_throw_exception:
383  *
384  *   C function called from the throw trampolines.
385  */
386 void
387 mono_x86_throw_exception (mgreg_t *regs, MonoObject *exc, 
388                                                   mgreg_t eip, gboolean rethrow)
389 {
390         static void (*restore_context) (MonoContext *);
391         MonoContext ctx;
392
393         if (!restore_context)
394                 restore_context = mono_get_restore_context ();
395
396         ctx.esp = regs [X86_ESP];
397         ctx.eip = eip;
398         ctx.ebp = regs [X86_EBP];
399         ctx.edi = regs [X86_EDI];
400         ctx.esi = regs [X86_ESI];
401         ctx.ebx = regs [X86_EBX];
402         ctx.edx = regs [X86_EDX];
403         ctx.ecx = regs [X86_ECX];
404         ctx.eax = regs [X86_EAX];
405
406 #ifdef __APPLE__
407         /* The OSX ABI specifies 16 byte alignment at call sites */
408         g_assert ((ctx.esp % MONO_ARCH_FRAME_ALIGNMENT) == 0);
409 #endif
410
411         if (mono_object_isinst (exc, mono_defaults.exception_class)) {
412                 MonoException *mono_ex = (MonoException*)exc;
413                 if (!rethrow)
414                         mono_ex->stack_trace = NULL;
415         }
416
417         if (mono_debug_using_mono_debugger ()) {
418                 guint8 buf [16], *code;
419
420                 mono_breakpoint_clean_code (NULL, (gpointer)eip, 8, buf, sizeof (buf));
421                 code = buf + 8;
422
423                 if (buf [3] == 0xe8) {
424                         MonoContext ctx_cp = ctx;
425                         ctx_cp.eip = eip - 5;
426
427                         if (mono_debugger_handle_exception (&ctx_cp, exc)) {
428                                 restore_context (&ctx_cp);
429                                 g_assert_not_reached ();
430                         }
431                 }
432         }
433
434         /* adjust eip so that it point into the call instruction */
435         ctx.eip -= 1;
436
437         mono_handle_exception (&ctx, exc, (gpointer)eip, FALSE);
438
439         restore_context (&ctx);
440
441         g_assert_not_reached ();
442 }
443
444 void
445 mono_x86_throw_corlib_exception (mgreg_t *regs, guint32 ex_token_index, 
446                                                                  mgreg_t eip, gint32 pc_offset)
447 {
448         guint32 ex_token = MONO_TOKEN_TYPE_DEF | ex_token_index;
449         MonoException *ex;
450
451         ex = mono_exception_from_token (mono_defaults.exception_class->image, ex_token);
452
453         eip -= pc_offset;
454
455         /* Negate the ip adjustment done in mono_x86_throw_exception () */
456         eip += 1;
457
458         mono_x86_throw_exception (regs, (MonoObject*)ex, eip, FALSE);
459 }
460
461 static void
462 mono_x86_resume_unwind (mgreg_t *regs, MonoObject *exc, 
463                                                 mgreg_t eip, gboolean rethrow)
464 {
465         MonoContext ctx;
466
467         ctx.esp = regs [X86_ESP];
468         ctx.eip = eip;
469         ctx.ebp = regs [X86_EBP];
470         ctx.edi = regs [X86_EDI];
471         ctx.esi = regs [X86_ESI];
472         ctx.ebx = regs [X86_EBX];
473         ctx.edx = regs [X86_EDX];
474         ctx.ecx = regs [X86_ECX];
475         ctx.eax = regs [X86_EAX];
476
477         mono_resume_unwind (&ctx);
478 }
479
480 /*
481  * get_throw_trampoline:
482  *
483  *  Generate a call to mono_x86_throw_exception/
484  * mono_x86_throw_corlib_exception.
485  * If LLVM is true, generate code which assumes the caller is LLVM generated code, 
486  * which doesn't push the arguments.
487  */
488 static guint8*
489 get_throw_trampoline (const char *name, gboolean rethrow, gboolean llvm, gboolean corlib, gboolean llvm_abs, gboolean resume_unwind, MonoTrampInfo **info, gboolean aot)
490 {
491         guint8 *start, *code;
492         int i, stack_size, stack_offset, arg_offsets [5], regs_offset;
493         MonoJumpInfo *ji = NULL;
494         GSList *unwind_ops = NULL;
495
496         start = code = mono_global_codeman_reserve (128);
497
498         stack_size = 128;
499
500         /* 
501          * On apple, the stack is misaligned by the pushing of the return address.
502          */
503         if (!llvm && corlib)
504                 /* On OSX, we don't generate alignment code to save space */
505                 stack_size += 4;
506         else
507                 stack_size += MONO_ARCH_FRAME_ALIGNMENT - 4;
508
509         /*
510          * The stack looks like this:
511          * <pc offset> (only if corlib is TRUE)
512          * <exception object>/<type token>
513          * <return addr> <- esp (unaligned on apple)
514          */
515
516         mono_add_unwind_op_def_cfa (unwind_ops, (guint8*)NULL, (guint8*)NULL, X86_ESP, 4);
517         mono_add_unwind_op_offset (unwind_ops, (guint8*)NULL, (guint8*)NULL, X86_NREG, -4);
518
519         /* Alloc frame */
520         x86_alu_reg_imm (code, X86_SUB, X86_ESP, stack_size);
521         mono_add_unwind_op_def_cfa_offset (unwind_ops, code, start, stack_size + 4);
522
523         arg_offsets [0] = 0;
524         arg_offsets [1] = 4;
525         arg_offsets [2] = 8;
526         arg_offsets [3] = 12;
527         regs_offset = 16;
528
529         /* Save registers */
530         for (i = 0; i < X86_NREG; ++i)
531                 if (i != X86_ESP)
532                         x86_mov_membase_reg (code, X86_ESP, regs_offset + (i * 4), i, 4);
533         /* Calculate the offset between the current sp and the sp of the caller */
534         if (llvm) {
535                 /* LLVM doesn't push the arguments */
536                 stack_offset = stack_size + 4;
537         } else {
538                 if (corlib) {
539                         /* Two arguments */
540                         stack_offset = stack_size + 4 + 8;
541 #ifdef __APPLE__
542                         /* We don't generate stack alignment code on osx to save space */
543 #endif
544                 } else {
545                         /* One argument */
546                         stack_offset = stack_size + 4 + 4;
547 #ifdef __APPLE__
548                         /* Pop the alignment added by OP_THROW too */
549                         stack_offset += MONO_ARCH_FRAME_ALIGNMENT - 4;
550 #endif
551                 }
552         }
553         /* Save ESP */
554         x86_lea_membase (code, X86_EAX, X86_ESP, stack_offset);
555         x86_mov_membase_reg (code, X86_ESP, regs_offset + (X86_ESP * 4), X86_EAX, 4);
556
557         /* Set arg1 == regs */
558         x86_lea_membase (code, X86_EAX, X86_ESP, regs_offset);
559         x86_mov_membase_reg (code, X86_ESP, arg_offsets [0], X86_EAX, 4);
560         /* Set arg2 == exc/ex_token_index */
561         if (resume_unwind)
562                 x86_mov_reg_imm (code, X86_EAX, 0);
563         else
564                 x86_mov_reg_membase (code, X86_EAX, X86_ESP, stack_size + 4, 4);
565         x86_mov_membase_reg (code, X86_ESP, arg_offsets [1], X86_EAX, 4);
566         /* Set arg3 == eip */
567         if (llvm_abs)
568                 x86_alu_reg_reg (code, X86_XOR, X86_EAX, X86_EAX);
569         else
570                 x86_mov_reg_membase (code, X86_EAX, X86_ESP, stack_size, 4);
571         x86_mov_membase_reg (code, X86_ESP, arg_offsets [2], X86_EAX, 4);
572         /* Set arg4 == rethrow/pc_offset */
573         if (resume_unwind) {
574                 x86_mov_membase_imm (code, X86_ESP, arg_offsets [3], 0, 4);
575         } else if (corlib) {
576                 x86_mov_reg_membase (code, X86_EAX, X86_ESP, stack_size + 8, 4);
577                 if (llvm_abs) {
578                         /* 
579                          * The caller is LLVM code which passes the absolute address not a pc offset,
580                          * so compensate by passing 0 as 'ip' and passing the negated abs address as
581                          * the pc offset.
582                          */
583                         x86_neg_reg (code, X86_EAX);
584                 }
585                 x86_mov_membase_reg (code, X86_ESP, arg_offsets [3], X86_EAX, 4);
586         } else {
587                 x86_mov_membase_imm (code, X86_ESP, arg_offsets [3], rethrow, 4);
588         }
589         /* Make the call */
590         if (aot) {
591                 // This can be called from runtime code, which can't guarantee that
592                 // ebx contains the got address.
593                 // So emit the got address loading code too
594                 code = mono_arch_emit_load_got_addr (start, code, NULL, &ji);
595                 code = mono_arch_emit_load_aotconst (start, code, &ji, MONO_PATCH_INFO_JIT_ICALL_ADDR, corlib ? "mono_x86_throw_corlib_exception" : "mono_x86_throw_exception");
596                 x86_call_reg (code, X86_EAX);
597         } else {
598                 x86_call_code (code, resume_unwind ? (mono_x86_resume_unwind) : (corlib ? (gpointer)mono_x86_throw_corlib_exception : (gpointer)mono_x86_throw_exception));
599         }
600         x86_breakpoint (code);
601
602         g_assert ((code - start) < 128);
603
604         mono_save_trampoline_xdebug_info (name, start, code - start, unwind_ops);
605
606         if (info)
607                 *info = mono_tramp_info_create (name, start, code - start, ji, unwind_ops);
608
609         return start;
610 }
611
612 /**
613  * mono_arch_get_throw_exception:
614  *
615  * Returns a function pointer which can be used to raise 
616  * exceptions. The returned function has the following 
617  * signature: void (*func) (MonoException *exc); 
618  * For example to raise an arithmetic exception you can use:
619  *
620  * x86_push_imm (code, mono_get_exception_arithmetic ()); 
621  * x86_call_code (code, arch_get_throw_exception ()); 
622  *
623  */
624 gpointer 
625 mono_arch_get_throw_exception (MonoTrampInfo **info, gboolean aot)
626 {
627         return get_throw_trampoline ("throw_exception", FALSE, FALSE, FALSE, FALSE, FALSE, info, aot);
628 }
629
630 gpointer 
631 mono_arch_get_rethrow_exception (MonoTrampInfo **info, gboolean aot)
632 {
633         return get_throw_trampoline ("rethow_exception", TRUE, FALSE, FALSE, FALSE, FALSE, info, aot);
634 }
635
636 /**
637  * mono_arch_get_throw_corlib_exception:
638  *
639  * Returns a function pointer which can be used to raise 
640  * corlib exceptions. The returned function has the following 
641  * signature: void (*func) (guint32 ex_token, guint32 offset); 
642  * Here, offset is the offset which needs to be substracted from the caller IP 
643  * to get the IP of the throw. Passing the offset has the advantage that it 
644  * needs no relocations in the caller.
645  */
646 gpointer 
647 mono_arch_get_throw_corlib_exception (MonoTrampInfo **info, gboolean aot)
648 {
649         return get_throw_trampoline ("throw_corlib_exception", FALSE, FALSE, TRUE, FALSE, FALSE, info, aot);
650 }
651
652 void
653 mono_arch_exceptions_init (void)
654 {
655         guint8 *tramp;
656
657         if (mono_aot_only) {
658                 signal_exception_trampoline = mono_aot_get_trampoline ("x86_signal_exception_trampoline");
659                 return;
660         }
661
662         /* LLVM needs different throw trampolines */
663         tramp = get_throw_trampoline ("llvm_throw_exception_trampoline", FALSE, TRUE, FALSE, FALSE, FALSE, NULL, FALSE);
664         mono_register_jit_icall (tramp, "mono_arch_llvm_throw_exception", NULL, TRUE);
665
666         tramp = get_throw_trampoline ("llvm_rethrow_exception_trampoline", FALSE, TRUE, FALSE, FALSE, FALSE, NULL, FALSE);
667         mono_register_jit_icall (tramp, "mono_arch_llvm_rethrow_exception", NULL, TRUE);
668
669         tramp = get_throw_trampoline ("llvm_throw_corlib_exception_trampoline", FALSE, TRUE, TRUE, FALSE, FALSE, NULL, FALSE);
670         mono_register_jit_icall (tramp, "mono_arch_llvm_throw_corlib_exception", NULL, TRUE);
671
672         tramp = get_throw_trampoline ("llvm_throw_corlib_exception_trampoline_abs", FALSE, TRUE, TRUE, TRUE, FALSE, NULL, FALSE);
673         mono_register_jit_icall (tramp, "mono_arch_llvm_throw_corlib_exception_abs", NULL, TRUE);
674
675         tramp = get_throw_trampoline ("llvm_resume_unwind_trampoline", FALSE, FALSE, FALSE, FALSE, TRUE, NULL, FALSE);
676         mono_register_jit_icall (tramp, "mono_llvm_resume_unwind_trampoline", NULL, TRUE);
677
678         signal_exception_trampoline = mono_x86_get_signal_exception_trampoline (NULL, FALSE);
679 }
680
681 /*
682  * mono_arch_find_jit_info_ext:
683  *
684  * See exceptions-amd64.c for docs.
685  */
686 gboolean
687 mono_arch_find_jit_info_ext (MonoDomain *domain, MonoJitTlsData *jit_tls, 
688                                                          MonoJitInfo *ji, MonoContext *ctx, 
689                                                          MonoContext *new_ctx, MonoLMF **lmf, 
690                                                          StackFrameInfo *frame)
691 {
692         gpointer ip = MONO_CONTEXT_GET_IP (ctx);
693
694         memset (frame, 0, sizeof (StackFrameInfo));
695         frame->ji = ji;
696         frame->managed = FALSE;
697
698         *new_ctx = *ctx;
699
700         if (ji != NULL) {
701                 gssize regs [MONO_MAX_IREGS + 1];
702                 guint8 *cfa;
703                 guint32 unwind_info_len;
704                 guint8 *unwind_info;
705
706                 frame->type = FRAME_TYPE_MANAGED;
707
708                 if (!ji->method->wrapper_type || ji->method->wrapper_type == MONO_WRAPPER_DYNAMIC_METHOD)
709                         frame->managed = TRUE;
710
711                 if (ji->from_aot)
712                         unwind_info = mono_aot_get_unwind_info (ji, &unwind_info_len);
713                 else
714                         unwind_info = mono_get_cached_unwind_info (ji->used_regs, &unwind_info_len);
715
716                 regs [X86_EAX] = new_ctx->eax;
717                 regs [X86_EBX] = new_ctx->ebx;
718                 regs [X86_ECX] = new_ctx->ecx;
719                 regs [X86_EDX] = new_ctx->edx;
720                 regs [X86_ESP] = new_ctx->esp;
721                 regs [X86_EBP] = new_ctx->ebp;
722                 regs [X86_ESI] = new_ctx->esi;
723                 regs [X86_EDI] = new_ctx->edi;
724                 regs [X86_NREG] = new_ctx->eip;
725
726                 mono_unwind_frame (unwind_info, unwind_info_len, ji->code_start, 
727                                                    (guint8*)ji->code_start + ji->code_size,
728                                                    ip, regs, MONO_MAX_IREGS + 1, &cfa);
729
730                 new_ctx->eax = regs [X86_EAX];
731                 new_ctx->ebx = regs [X86_EBX];
732                 new_ctx->ecx = regs [X86_ECX];
733                 new_ctx->edx = regs [X86_EDX];
734                 new_ctx->esp = regs [X86_ESP];
735                 new_ctx->ebp = regs [X86_EBP];
736                 new_ctx->esi = regs [X86_ESI];
737                 new_ctx->edi = regs [X86_EDI];
738                 new_ctx->eip = regs [X86_NREG];
739
740                 /* The CFA becomes the new SP value */
741                 new_ctx->esp = (gssize)cfa;
742
743                 /* Adjust IP */
744                 new_ctx->eip --;
745
746                 if (*lmf && (MONO_CONTEXT_GET_BP (ctx) >= (gpointer)(*lmf)->ebp)) {
747                         /* remove any unused lmf */
748                         *lmf = (gpointer)(((gsize)(*lmf)->previous_lmf) & ~3);
749                 }
750
751                 /* Pop arguments off the stack */
752                 /* 
753                  * FIXME: LLVM doesn't push these, we can't use ji->from_llvm as it describes
754                  * the caller.
755                  */
756 #ifndef ENABLE_LLVM
757                 {
758                         MonoJitArgumentInfo *arg_info = g_newa (MonoJitArgumentInfo, mono_method_signature (ji->method)->param_count + 1);
759
760                         guint32 stack_to_pop = mono_arch_get_argument_info (mono_method_signature (ji->method), mono_method_signature (ji->method)->param_count, arg_info);
761                         new_ctx->esp += stack_to_pop;
762                 }
763 #endif
764
765                 return TRUE;
766         } else if (*lmf) {
767
768                 if (((guint64)(*lmf)->previous_lmf) & 2) {
769                         /* 
770                          * This LMF entry is created by the soft debug code to mark transitions to
771                          * managed code done during invokes.
772                          */
773                         MonoLMFExt *ext = (MonoLMFExt*)(*lmf);
774
775                         g_assert (ext->debugger_invoke);
776
777                         memcpy (new_ctx, &ext->ctx, sizeof (MonoContext));
778
779                         *lmf = (gpointer)(((gsize)(*lmf)->previous_lmf) & ~3);
780
781                         frame->type = FRAME_TYPE_DEBUGGER_INVOKE;
782
783                         return TRUE;
784                 }
785                 
786                 if ((ji = mini_jit_info_table_find (domain, (gpointer)(*lmf)->eip, NULL))) {
787                 } else {
788                         if (!((guint32)((*lmf)->previous_lmf) & 1))
789                                 /* Top LMF entry */
790                                 return FALSE;
791                         /* Trampoline lmf frame */
792                         frame->method = (*lmf)->method;
793                 }
794
795                 new_ctx->esi = (*lmf)->esi;
796                 new_ctx->edi = (*lmf)->edi;
797                 new_ctx->ebx = (*lmf)->ebx;
798                 new_ctx->ebp = (*lmf)->ebp;
799                 new_ctx->eip = (*lmf)->eip;
800
801                 /* Adjust IP */
802                 new_ctx->eip --;
803
804                 frame->ji = ji;
805                 frame->type = FRAME_TYPE_MANAGED_TO_NATIVE;
806
807                 /* Check if we are in a trampoline LMF frame */
808                 if ((guint32)((*lmf)->previous_lmf) & 1) {
809                         /* lmf->esp is set by the trampoline code */
810                         new_ctx->esp = (*lmf)->esp;
811
812                         /* Pop arguments off the stack */
813                         /* FIXME: Handle the delegate case too ((*lmf)->method == NULL) */
814                         /* FIXME: Handle the IMT/vtable case too */
815 #ifndef ENABLE_LLVM
816                         if ((*lmf)->method) {
817                                 MonoMethod *method = (*lmf)->method;
818                                 MonoJitArgumentInfo *arg_info = g_newa (MonoJitArgumentInfo, mono_method_signature (method)->param_count + 1);
819
820                                 guint32 stack_to_pop = mono_arch_get_argument_info (mono_method_signature (method), mono_method_signature (method)->param_count, arg_info);
821                                 new_ctx->esp += stack_to_pop;
822                         }
823 #endif
824                 }
825                 else
826                         /* the lmf is always stored on the stack, so the following
827                          * expression points to a stack location which can be used as ESP */
828                         new_ctx->esp = (unsigned long)&((*lmf)->eip);
829
830                 *lmf = (gpointer)(((gsize)(*lmf)->previous_lmf) & ~3);
831
832                 return TRUE;
833         }
834
835         return FALSE;
836 }
837
838 #ifdef __sun
839 #define REG_EAX EAX
840 #define REG_EBX EBX
841 #define REG_ECX ECX
842 #define REG_EDX EDX
843 #define REG_EBP EBP
844 #define REG_ESP ESP
845 #define REG_ESI ESI
846 #define REG_EDI EDI
847 #define REG_EIP EIP
848 #endif
849
850 void
851 mono_arch_sigctx_to_monoctx (void *sigctx, MonoContext *mctx)
852 {
853 #ifdef MONO_ARCH_USE_SIGACTION
854         ucontext_t *ctx = (ucontext_t*)sigctx;
855         
856         mctx->eax = UCONTEXT_REG_EAX (ctx);
857         mctx->ebx = UCONTEXT_REG_EBX (ctx);
858         mctx->ecx = UCONTEXT_REG_ECX (ctx);
859         mctx->edx = UCONTEXT_REG_EDX (ctx);
860         mctx->ebp = UCONTEXT_REG_EBP (ctx);
861         mctx->esp = UCONTEXT_REG_ESP (ctx);
862         mctx->esi = UCONTEXT_REG_ESI (ctx);
863         mctx->edi = UCONTEXT_REG_EDI (ctx);
864         mctx->eip = UCONTEXT_REG_EIP (ctx);
865 #else   
866         struct sigcontext *ctx = (struct sigcontext *)sigctx;
867
868         mctx->eax = ctx->SC_EAX;
869         mctx->ebx = ctx->SC_EBX;
870         mctx->ecx = ctx->SC_ECX;
871         mctx->edx = ctx->SC_EDX;
872         mctx->ebp = ctx->SC_EBP;
873         mctx->esp = ctx->SC_ESP;
874         mctx->esi = ctx->SC_ESI;
875         mctx->edi = ctx->SC_EDI;
876         mctx->eip = ctx->SC_EIP;
877 #endif
878 }
879
880 void
881 mono_arch_monoctx_to_sigctx (MonoContext *mctx, void *sigctx)
882 {
883 #ifdef MONO_ARCH_USE_SIGACTION
884         ucontext_t *ctx = (ucontext_t*)sigctx;
885
886         UCONTEXT_REG_EAX (ctx) = mctx->eax;
887         UCONTEXT_REG_EBX (ctx) = mctx->ebx;
888         UCONTEXT_REG_ECX (ctx) = mctx->ecx;
889         UCONTEXT_REG_EDX (ctx) = mctx->edx;
890         UCONTEXT_REG_EBP (ctx) = mctx->ebp;
891         UCONTEXT_REG_ESP (ctx) = mctx->esp;
892         UCONTEXT_REG_ESI (ctx) = mctx->esi;
893         UCONTEXT_REG_EDI (ctx) = mctx->edi;
894         UCONTEXT_REG_EIP (ctx) = mctx->eip;
895 #else
896         struct sigcontext *ctx = (struct sigcontext *)sigctx;
897
898         ctx->SC_EAX = mctx->eax;
899         ctx->SC_EBX = mctx->ebx;
900         ctx->SC_ECX = mctx->ecx;
901         ctx->SC_EDX = mctx->edx;
902         ctx->SC_EBP = mctx->ebp;
903         ctx->SC_ESP = mctx->esp;
904         ctx->SC_ESI = mctx->esi;
905         ctx->SC_EDI = mctx->edi;
906         ctx->SC_EIP = mctx->eip;
907 #endif
908 }       
909
910 gpointer
911 mono_arch_ip_from_context (void *sigctx)
912 {
913 #ifdef MONO_ARCH_USE_SIGACTION
914         ucontext_t *ctx = (ucontext_t*)sigctx;
915         return (gpointer)UCONTEXT_REG_EIP (ctx);
916 #else
917         struct sigcontext *ctx = sigctx;
918         return (gpointer)ctx->SC_EIP;
919 #endif  
920 }
921
922 /*
923  * handle_exception:
924  *
925  *   Called by resuming from a signal handler.
926  */
927 static void
928 handle_signal_exception (gpointer obj)
929 {
930         MonoJitTlsData *jit_tls = TlsGetValue (mono_jit_tls_id);
931         MonoContext ctx;
932         static void (*restore_context) (MonoContext *);
933
934         if (!restore_context)
935                 restore_context = mono_get_restore_context ();
936
937         memcpy (&ctx, &jit_tls->ex_ctx, sizeof (MonoContext));
938
939         if (mono_debugger_handle_exception (&ctx, (MonoObject *)obj))
940                 return;
941
942         mono_handle_exception (&ctx, obj, MONO_CONTEXT_GET_IP (&ctx), FALSE);
943
944         restore_context (&ctx);
945 }
946
947 /*
948  * mono_x86_get_signal_exception_trampoline:
949  *
950  *   This x86 specific trampoline is used to call handle_signal_exception.
951  */
952 gpointer
953 mono_x86_get_signal_exception_trampoline (MonoTrampInfo **info, gboolean aot)
954 {
955         guint8 *start, *code;
956         MonoJumpInfo *ji = NULL;
957         GSList *unwind_ops = NULL;
958         int stack_size;
959
960         start = code = mono_global_codeman_reserve (128);
961
962         /* Caller ip */
963         x86_push_reg (code, X86_ECX);
964
965         mono_add_unwind_op_def_cfa (unwind_ops, (guint8*)NULL, (guint8*)NULL, X86_ESP, 4);
966         mono_add_unwind_op_offset (unwind_ops, (guint8*)NULL, (guint8*)NULL, X86_NREG, -4);
967
968         /* Fix the alignment to be what apple expects */
969         stack_size = 12;
970
971         x86_alu_reg_imm (code, X86_SUB, X86_ESP, stack_size);
972         mono_add_unwind_op_def_cfa_offset (unwind_ops, code, start, stack_size + 4);
973
974         /* Arg1 */
975         x86_mov_membase_reg (code, X86_ESP, 0, X86_EAX, 4);
976         /* Branch to target */
977         x86_call_reg (code, X86_EDX);
978
979         g_assert ((code - start) < 128);
980
981         mono_save_trampoline_xdebug_info ("x86_signal_exception_trampoline", start, code - start, unwind_ops);
982
983         if (info)
984                 *info = mono_tramp_info_create (g_strdup ("x86_signal_exception_trampoline"), start, code - start, ji, unwind_ops);
985
986         return start;
987 }
988
989 gboolean
990 mono_arch_handle_exception (void *sigctx, gpointer obj, gboolean test_only)
991 {
992 #if defined(MONO_ARCH_USE_SIGACTION)
993         /*
994          * Handling the exception in the signal handler is problematic, since the original
995          * signal is disabled, and we could run arbitrary code though the debugger. So
996          * resume into the normal stack and do most work there if possible.
997          */
998         MonoJitTlsData *jit_tls = TlsGetValue (mono_jit_tls_id);
999         guint64 sp = UCONTEXT_REG_ESP (sigctx);
1000
1001         /* Pass the ctx parameter in TLS */
1002         mono_arch_sigctx_to_monoctx (sigctx, &jit_tls->ex_ctx);
1003         /*
1004          * Can't pass the obj on the stack, since we are executing on the
1005          * same stack. Can't save it into MonoJitTlsData, since it needs GC tracking.
1006          * So put it into a register, and branch to a trampoline which
1007          * pushes it.
1008          */
1009         g_assert (!test_only);
1010         UCONTEXT_REG_EAX (sigctx) = (gsize)obj;
1011         UCONTEXT_REG_ECX (sigctx) = UCONTEXT_REG_EIP (sigctx);
1012         UCONTEXT_REG_EDX (sigctx) = (gsize)handle_signal_exception;
1013
1014         /* Allocate a stack frame, align it to 16 bytes which is needed on apple */
1015         sp -= 16;
1016         sp &= ~15;
1017         UCONTEXT_REG_ESP (sigctx) = sp;
1018
1019         UCONTEXT_REG_EIP (sigctx) = (gsize)signal_exception_trampoline;
1020
1021         return TRUE;
1022 #elif defined (TARGET_WIN32)
1023         MonoJitTlsData *jit_tls = TlsGetValue (mono_jit_tls_id);
1024         struct sigcontext *ctx = (struct sigcontext *)sigctx;
1025         guint64 sp = ctx->SC_ESP;
1026
1027         mono_arch_sigctx_to_monoctx (sigctx, &jit_tls->ex_ctx);
1028
1029         /*
1030          * Can't pass the obj on the stack, since we are executing on the
1031          * same stack. Can't save it into MonoJitTlsData, since it needs GC tracking.
1032          * So put it into a register, and branch to a trampoline which
1033          * pushes it.
1034          */
1035         g_assert (!test_only);
1036         ctx->SC_EAX = (gsize)obj;
1037         ctx->SC_ECX = ctx->SC_EIP;
1038         ctx->SC_EDX = (gsize)handle_signal_exception;
1039
1040         /* Allocate a stack frame, align it to 16 bytes which is needed on apple */
1041         sp -= 16;
1042         sp &= ~15;
1043         ctx->SC_ESP = sp;
1044
1045         ctx->SC_EIP = (gsize)signal_exception_trampoline;
1046
1047         return TRUE;
1048 #else
1049         MonoContext mctx;
1050
1051         mono_arch_sigctx_to_monoctx (sigctx, &mctx);
1052
1053         if (mono_debugger_handle_exception (&mctx, (MonoObject *)obj))
1054                 return TRUE;
1055
1056         mono_handle_exception (&mctx, obj, (gpointer)mctx.eip, test_only);
1057
1058         mono_arch_monoctx_to_sigctx (&mctx, sigctx);
1059
1060         return TRUE;
1061 #endif
1062 }
1063
1064 static void
1065 restore_soft_guard_pages (void)
1066 {
1067         MonoJitTlsData *jit_tls = TlsGetValue (mono_jit_tls_id);
1068         if (jit_tls->stack_ovf_guard_base)
1069                 mono_mprotect (jit_tls->stack_ovf_guard_base, jit_tls->stack_ovf_guard_size, MONO_MMAP_NONE);
1070 }
1071
1072 /* 
1073  * this function modifies mctx so that when it is restored, it
1074  * won't execcute starting at mctx.eip, but in a function that
1075  * will restore the protection on the soft-guard pages and return back to
1076  * continue at mctx.eip.
1077  */
1078 static void
1079 prepare_for_guard_pages (MonoContext *mctx)
1080 {
1081         gpointer *sp;
1082         sp = (gpointer)(mctx->esp);
1083         sp -= 1;
1084         /* the resturn addr */
1085         sp [0] = (gpointer)(mctx->eip);
1086         mctx->eip = (unsigned long)restore_soft_guard_pages;
1087         mctx->esp = (unsigned long)sp;
1088 }
1089
1090 static void
1091 altstack_handle_and_restore (void *sigctx, gpointer obj, gboolean stack_ovf)
1092 {
1093         void (*restore_context) (MonoContext *);
1094         MonoContext mctx;
1095
1096         restore_context = mono_get_restore_context ();
1097         mono_arch_sigctx_to_monoctx (sigctx, &mctx);
1098
1099         if (mono_debugger_handle_exception (&mctx, (MonoObject *)obj)) {
1100                 if (stack_ovf)
1101                         prepare_for_guard_pages (&mctx);
1102                 restore_context (&mctx);
1103         }
1104
1105         mono_handle_exception (&mctx, obj, (gpointer)mctx.eip, FALSE);
1106         if (stack_ovf)
1107                 prepare_for_guard_pages (&mctx);
1108         restore_context (&mctx);
1109 }
1110
1111 void
1112 mono_arch_handle_altstack_exception (void *sigctx, gpointer fault_addr, gboolean stack_ovf)
1113 {
1114 #ifdef MONO_ARCH_USE_SIGACTION
1115         MonoException *exc = NULL;
1116         ucontext_t *ctx = (ucontext_t*)sigctx;
1117         MonoJitInfo *ji = mini_jit_info_table_find (mono_domain_get (), (gpointer)UCONTEXT_REG_EIP (ctx), NULL);
1118         gpointer *sp;
1119         int frame_size;
1120
1121         /* if we didn't find a managed method for the ip address and it matches the fault
1122          * address, we assume we followed a broken pointer during an indirect call, so
1123          * we try the lookup again with the return address pushed on the stack
1124          */
1125         if (!ji && fault_addr == (gpointer)UCONTEXT_REG_EIP (ctx)) {
1126                 glong *sp = (gpointer)UCONTEXT_REG_ESP (ctx);
1127                 ji = mini_jit_info_table_find (mono_domain_get (), (gpointer)sp [0], NULL);
1128                 if (ji)
1129                         UCONTEXT_REG_EIP (ctx) = sp [0];
1130         }
1131         if (stack_ovf)
1132                 exc = mono_domain_get ()->stack_overflow_ex;
1133         if (!ji)
1134                 mono_handle_native_sigsegv (SIGSEGV, sigctx);
1135         /* setup a call frame on the real stack so that control is returned there
1136          * and exception handling can continue.
1137          * If this was a stack overflow the caller already ensured the stack pages
1138          * needed have been unprotected.
1139          * The frame looks like:
1140          *   ucontext struct
1141          *   test_only arg
1142          *   exception arg
1143          *   ctx arg
1144          *   return ip
1145          */
1146         frame_size = sizeof (ucontext_t) + sizeof (gpointer) * 4;
1147         frame_size += 15;
1148         frame_size &= ~15;
1149         sp = (gpointer)(UCONTEXT_REG_ESP (ctx) & ~15);
1150         sp = (gpointer)((char*)sp - frame_size);
1151         /* the incoming arguments are aligned to 16 bytes boundaries, so the return address IP
1152          * goes at sp [-1]
1153          */
1154         sp [-1] = (gpointer)UCONTEXT_REG_EIP (ctx);
1155         sp [0] = sp + 4;
1156         sp [1] = exc;
1157         sp [2] = (gpointer)stack_ovf;
1158         /* may need to adjust pointers in the new struct copy, depending on the OS */
1159         memcpy (sp + 4, ctx, sizeof (ucontext_t));
1160         /* at the return form the signal handler execution starts in altstack_handle_and_restore() */
1161         UCONTEXT_REG_EIP (ctx) = (unsigned long)altstack_handle_and_restore;
1162         UCONTEXT_REG_ESP (ctx) = (unsigned long)(sp - 1);
1163 #endif
1164 }
1165
1166 #if MONO_SUPPORT_TASKLETS
1167 MonoContinuationRestore
1168 mono_tasklets_arch_restore (void)
1169 {
1170         static guint8* saved = NULL;
1171         guint8 *code, *start;
1172
1173         if (saved)
1174                 return (MonoContinuationRestore)saved;
1175         code = start = mono_global_codeman_reserve (48);
1176         /* the signature is: restore (MonoContinuation *cont, int state, MonoLMF **lmf_addr) */
1177         /* put cont in edx */
1178         x86_mov_reg_membase (code, X86_EDX, X86_ESP, 4, 4);
1179         /* setup the copy of the stack */
1180         x86_mov_reg_membase (code, X86_ECX, X86_EDX, G_STRUCT_OFFSET (MonoContinuation, stack_used_size), 4);
1181         x86_shift_reg_imm (code, X86_SHR, X86_ECX, 2);
1182         x86_cld (code);
1183         x86_mov_reg_membase (code, X86_ESI, X86_EDX, G_STRUCT_OFFSET (MonoContinuation, saved_stack), 4);
1184         x86_mov_reg_membase (code, X86_EDI, X86_EDX, G_STRUCT_OFFSET (MonoContinuation, return_sp), 4);
1185         x86_prefix (code, X86_REP_PREFIX);
1186         x86_movsl (code);
1187
1188         /* now restore the registers from the LMF */
1189         x86_mov_reg_membase (code, X86_ECX, X86_EDX, G_STRUCT_OFFSET (MonoContinuation, lmf), 4);
1190         x86_mov_reg_membase (code, X86_EBX, X86_ECX, G_STRUCT_OFFSET (MonoLMF, ebx), 4);
1191         x86_mov_reg_membase (code, X86_EBP, X86_ECX, G_STRUCT_OFFSET (MonoLMF, ebp), 4);
1192         x86_mov_reg_membase (code, X86_ESI, X86_ECX, G_STRUCT_OFFSET (MonoLMF, esi), 4);
1193         x86_mov_reg_membase (code, X86_EDI, X86_ECX, G_STRUCT_OFFSET (MonoLMF, edi), 4);
1194
1195         /* restore the lmf chain */
1196         /*x86_mov_reg_membase (code, X86_ECX, X86_ESP, 12, 4);
1197         x86_mov_membase_reg (code, X86_ECX, 0, X86_EDX, 4);*/
1198
1199         /* state in eax, so it's setup as the return value */
1200         x86_mov_reg_membase (code, X86_EAX, X86_ESP, 8, 4);
1201         x86_jump_membase (code, X86_EDX, G_STRUCT_OFFSET (MonoContinuation, return_ip));
1202         g_assert ((code - start) <= 48);
1203         saved = start;
1204         return (MonoContinuationRestore)saved;
1205 }
1206 #endif
1207