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