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