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