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