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