[x86] Remove the now unused push based parameter passing code.
[mono.git] / mono / mini / exceptions-x86.c
1 /*
2  * exceptions-x86.c: exception support for x86
3  *
4  * Authors:
5  *   Dietmar Maurer (dietmar@ximian.com)
6  *
7  * (C) 2001 Ximian, Inc.
8  */
9
10 #include <config.h>
11
12 #include <glib.h>
13 #include <signal.h>
14 #include <string.h>
15
16 #include <mono/metadata/abi-details.h>
17 #include <mono/arch/x86/x86-codegen.h>
18 #include <mono/metadata/appdomain.h>
19 #include <mono/metadata/tabledefs.h>
20 #include <mono/metadata/threads.h>
21 #include <mono/metadata/debug-helpers.h>
22 #include <mono/metadata/exception.h>
23 #include <mono/metadata/gc-internal.h>
24 #include <mono/metadata/mono-debug.h>
25 #include <mono/utils/mono-mmap.h>
26
27 #include "mini.h"
28 #include "mini-x86.h"
29 #include "tasklets.h"
30
31 static gpointer signal_exception_trampoline;
32
33 gpointer
34 mono_x86_get_signal_exception_trampoline (MonoTrampInfo **info, gboolean aot) MONO_INTERNAL;
35
36 #ifdef TARGET_WIN32
37 static void (*restore_stack) (void *);
38
39 static MonoW32ExceptionHandler fpe_handler;
40 static MonoW32ExceptionHandler ill_handler;
41 static MonoW32ExceptionHandler segv_handler;
42
43 LPTOP_LEVEL_EXCEPTION_FILTER mono_old_win_toplevel_exception_filter;
44 gpointer mono_win_vectored_exception_handle;
45 extern int (*gUnhandledExceptionHandler)(EXCEPTION_POINTERS*);
46
47 #ifndef PROCESS_CALLBACK_FILTER_ENABLED
48 #       define PROCESS_CALLBACK_FILTER_ENABLED 1
49 #endif
50
51 #define W32_SEH_HANDLE_EX(_ex) \
52         if (_ex##_handler) _ex##_handler(0, ep, ctx)
53
54 LONG CALLBACK seh_unhandled_exception_filter(EXCEPTION_POINTERS* ep)
55 {
56 #ifndef MONO_CROSS_COMPILE
57         if (mono_old_win_toplevel_exception_filter) {
58                 return (*mono_old_win_toplevel_exception_filter)(ep);
59         }
60 #endif
61
62         mono_handle_native_sigsegv (SIGSEGV, NULL);
63
64         return EXCEPTION_CONTINUE_SEARCH;
65 }
66
67 /*
68  * mono_win32_get_handle_stackoverflow (void):
69  *
70  * Returns a pointer to a method which restores the current context stack
71  * and calls handle_exceptions, when done restores the original stack.
72  */
73 static gpointer
74 mono_win32_get_handle_stackoverflow (void)
75 {
76         static guint8 *start = NULL;
77         guint8 *code;
78
79         if (start)
80                 return start;
81
82         /* restore_contect (void *sigctx) */
83         start = code = mono_global_codeman_reserve (128);
84
85         /* load context into ebx */
86         x86_mov_reg_membase (code, X86_EBX, X86_ESP, 4, 4);
87
88         /* move current stack into edi for later restore */
89         x86_mov_reg_reg (code, X86_EDI, X86_ESP, 4);
90
91         /* use the new freed stack from sigcontext */
92         /* XXX replace usage of struct sigcontext with MonoContext so we can use MONO_STRUCT_OFFSET */
93         x86_mov_reg_membase (code, X86_ESP, X86_EBX,  G_STRUCT_OFFSET (struct sigcontext, esp), 4);
94
95         /* get the current domain */
96         x86_call_code (code, mono_domain_get);
97
98         /* get stack overflow exception from domain object */
99         x86_mov_reg_membase (code, X86_EAX, X86_EAX, MONO_STRUCT_OFFSET (MonoDomain, stack_overflow_ex), 4);
100
101         /* call mono_arch_handle_exception (sctx, stack_overflow_exception_obj) */
102         x86_push_reg (code, X86_EAX);
103         x86_push_reg (code, X86_EBX);
104         x86_call_code (code, mono_arch_handle_exception);
105
106         /* restore the SEH handler stack */
107         x86_mov_reg_reg (code, X86_ESP, X86_EDI, 4);
108
109         /* return */
110         x86_ret (code);
111
112         return start;
113 }
114
115 /* Special hack to workaround the fact that the
116  * when the SEH handler is called the stack is
117  * to small to recover.
118  *
119  * Stack walking part of this method is from mono_handle_exception
120  *
121  * The idea is simple; 
122  *  - walk the stack to free some space (64k)
123  *  - set esp to new stack location
124  *  - call mono_arch_handle_exception with stack overflow exception
125  *  - set esp to SEH handlers stack
126  *  - done
127  */
128 static void 
129 win32_handle_stack_overflow (EXCEPTION_POINTERS* ep, struct sigcontext *sctx) 
130 {
131         SYSTEM_INFO si;
132         DWORD page_size;
133         MonoDomain *domain = mono_domain_get ();
134         MonoJitInfo rji;
135         MonoJitTlsData *jit_tls = mono_native_tls_get_value (mono_jit_tls_id);
136         MonoLMF *lmf = jit_tls->lmf;            
137         MonoContext initial_ctx;
138         MonoContext ctx;
139         guint32 free_stack = 0;
140         StackFrameInfo frame;
141
142         /* convert sigcontext to MonoContext (due to reuse of stack walking helpers */
143         mono_arch_sigctx_to_monoctx (sctx, &ctx);
144         
145         /* get our os page size */
146         GetSystemInfo(&si);
147         page_size = si.dwPageSize;
148
149         /* Let's walk the stack to recover
150          * the needed stack space (if possible)
151          */
152         memset (&rji, 0, sizeof (rji));
153
154         initial_ctx = ctx;
155         free_stack = (guint8*)(MONO_CONTEXT_GET_BP (&ctx)) - (guint8*)(MONO_CONTEXT_GET_BP (&initial_ctx));
156
157         /* try to free 64kb from our stack */
158         do {
159                 MonoContext new_ctx;
160
161                 mono_arch_find_jit_info (domain, jit_tls, &rji, &ctx, &new_ctx, &lmf, NULL, &frame);
162                 if (!frame.ji) {
163                         g_warning ("Exception inside function without unwind info");
164                         g_assert_not_reached ();
165                 }
166
167                 if (frame.ji != (gpointer)-1) {
168                         free_stack = (guint8*)(MONO_CONTEXT_GET_BP (&ctx)) - (guint8*)(MONO_CONTEXT_GET_BP (&initial_ctx));
169                 }
170
171                 /* todo: we should call abort if ji is -1 */
172                 ctx = new_ctx;
173         } while (free_stack < 64 * 1024 && frame.ji != (gpointer) -1);
174
175         /* convert into sigcontext to be used in mono_arch_handle_exception */
176         mono_arch_monoctx_to_sigctx (&ctx, sctx);
177
178         /* todo: install new stack-guard page */
179
180         /* use the new stack and call mono_arch_handle_exception () */
181         restore_stack (sctx);
182 }
183
184 /*
185  * Unhandled Exception Filter
186  * Top-level per-process exception handler.
187  */
188 LONG CALLBACK seh_vectored_exception_handler(EXCEPTION_POINTERS* ep)
189 {
190         EXCEPTION_RECORD* er;
191         CONTEXT* ctx;
192         LONG res;
193         MonoJitTlsData *jit_tls = mono_native_tls_get_value (mono_jit_tls_id);
194
195         /* If the thread is not managed by the runtime return early */
196         if (!jit_tls)
197                 return EXCEPTION_CONTINUE_SEARCH;
198
199         jit_tls->mono_win_chained_exception_needs_run = FALSE;
200         res = EXCEPTION_CONTINUE_EXECUTION;
201
202         er = ep->ExceptionRecord;
203         ctx = ep->ContextRecord;
204
205         switch (er->ExceptionCode) {
206         case EXCEPTION_STACK_OVERFLOW:
207                 win32_handle_stack_overflow (ep, ctx);
208                 break;
209         case EXCEPTION_ACCESS_VIOLATION:
210                 W32_SEH_HANDLE_EX(segv);
211                 break;
212         case EXCEPTION_ILLEGAL_INSTRUCTION:
213                 W32_SEH_HANDLE_EX(ill);
214                 break;
215         case EXCEPTION_INT_DIVIDE_BY_ZERO:
216         case EXCEPTION_INT_OVERFLOW:
217         case EXCEPTION_FLT_DIVIDE_BY_ZERO:
218         case EXCEPTION_FLT_OVERFLOW:
219         case EXCEPTION_FLT_UNDERFLOW:
220         case EXCEPTION_FLT_INEXACT_RESULT:
221                 W32_SEH_HANDLE_EX(fpe);
222                 break;
223         default:
224                 jit_tls->mono_win_chained_exception_needs_run = TRUE;
225                 break;
226         }
227
228         if (jit_tls->mono_win_chained_exception_needs_run) {
229                 /* Don't copy context back if we chained exception
230                 * as the handler may have modfied the EXCEPTION_POINTERS
231                 * directly. We don't pass sigcontext to chained handlers.
232                 * Return continue search so the UnhandledExceptionFilter
233                 * can correctly chain the exception.
234                 */
235                 res = EXCEPTION_CONTINUE_SEARCH;
236         }
237
238         return res;
239 }
240
241 void win32_seh_init()
242 {
243         /* install restore stack helper */
244         if (!restore_stack)
245                 restore_stack = mono_win32_get_handle_stackoverflow ();
246
247         mono_old_win_toplevel_exception_filter = SetUnhandledExceptionFilter(seh_unhandled_exception_filter);
248         mono_win_vectored_exception_handle = AddVectoredExceptionHandler (1, seh_vectored_exception_handler);
249 }
250
251 void win32_seh_cleanup()
252 {
253         if (mono_old_win_toplevel_exception_filter)
254                 SetUnhandledExceptionFilter(mono_old_win_toplevel_exception_filter);
255         RemoveVectoredExceptionHandler (mono_win_vectored_exception_handle);
256 }
257
258 void win32_seh_set_handler(int type, MonoW32ExceptionHandler handler)
259 {
260         switch (type) {
261         case SIGFPE:
262                 fpe_handler = handler;
263                 break;
264         case SIGILL:
265                 ill_handler = handler;
266                 break;
267         case SIGSEGV:
268                 segv_handler = handler;
269                 break;
270         default:
271                 break;
272         }
273 }
274
275 #endif /* TARGET_WIN32 */
276
277 /*
278  * mono_arch_get_restore_context:
279  *
280  * Returns a pointer to a method which restores a previously saved sigcontext.
281  */
282 gpointer
283 mono_arch_get_restore_context (MonoTrampInfo **info, gboolean aot)
284 {
285         guint8 *start = NULL;
286         guint8 *code;
287         MonoJumpInfo *ji = NULL;
288         GSList *unwind_ops = NULL;
289
290         /* restore_contect (MonoContext *ctx) */
291
292         start = code = mono_global_codeman_reserve (128);
293         
294         /* load ctx */
295         x86_mov_reg_membase (code, X86_EAX, X86_ESP, 4, 4);
296
297         /* restore EBX */
298         x86_mov_reg_membase (code, X86_EBX, X86_EAX,  MONO_STRUCT_OFFSET (MonoContext, ebx), 4);
299
300         /* restore EDI */
301         x86_mov_reg_membase (code, X86_EDI, X86_EAX,  MONO_STRUCT_OFFSET (MonoContext, edi), 4);
302
303         /* restore ESI */
304         x86_mov_reg_membase (code, X86_ESI, X86_EAX,  MONO_STRUCT_OFFSET (MonoContext, esi), 4);
305
306         /* restore EDX */
307         x86_mov_reg_membase (code, X86_EDX, X86_EAX,  MONO_STRUCT_OFFSET (MonoContext, edx), 4);
308
309         /*
310          * The context resides on the stack, in the stack frame of the
311          * caller of this function.  The stack pointer that we need to
312          * restore is potentially many stack frames higher up, so the
313          * distance between them can easily be more than the red zone
314          * size.  Hence the stack pointer can be restored only after
315          * we have finished loading everything from the context.
316          */
317
318         /* load ESP into EBP */
319         x86_mov_reg_membase (code, X86_EBP, X86_EAX,  MONO_STRUCT_OFFSET (MonoContext, esp), 4);
320         /* load return address into ECX */
321         x86_mov_reg_membase (code, X86_ECX, X86_EAX,  MONO_STRUCT_OFFSET (MonoContext, eip), 4);
322         /* save the return addr to the restored stack - 4 */
323         x86_mov_membase_reg (code, X86_EBP, -4, X86_ECX, 4);
324
325         /* load EBP into ECX */
326         x86_mov_reg_membase (code, X86_ECX, X86_EAX,  MONO_STRUCT_OFFSET (MonoContext, ebp), 4);
327         /* save EBP to the restored stack - 8 */
328         x86_mov_membase_reg (code, X86_EBP, -8, X86_ECX, 4);
329
330         /* load EAX into ECX */
331         x86_mov_reg_membase (code, X86_ECX, X86_EAX,  MONO_STRUCT_OFFSET (MonoContext, eax), 4);
332         /* save EAX to the restored stack - 12 */
333         x86_mov_membase_reg (code, X86_EBP, -12, X86_ECX, 4);
334
335         /* restore ECX */
336         x86_mov_reg_membase (code, X86_ECX, X86_EAX,  MONO_STRUCT_OFFSET (MonoContext, ecx), 4);
337
338         /* restore ESP - 12 */
339         x86_lea_membase (code, X86_ESP, X86_EBP, -12);
340         /* restore EAX */
341         x86_pop_reg (code, X86_EAX);
342         /* restore EBP */
343         x86_pop_reg (code, X86_EBP);
344         /* jump to the saved IP */
345         x86_ret (code);
346
347         nacl_global_codeman_validate(&start, 128, &code);
348
349         if (info)
350                 *info = mono_tramp_info_create ("restore_context", start, code - start, ji, unwind_ops);
351         else {
352                 GSList *l;
353
354                 for (l = unwind_ops; l; l = l->next)
355                         g_free (l->data);
356                 g_slist_free (unwind_ops);
357         }
358
359         return start;
360 }
361
362 /*
363  * mono_arch_get_call_filter:
364  *
365  * Returns a pointer to a method which calls an exception filter. We
366  * also use this function to call finally handlers (we pass NULL as 
367  * @exc object in this case).
368  */
369 gpointer
370 mono_arch_get_call_filter (MonoTrampInfo **info, gboolean aot)
371 {
372         guint8* start;
373         guint8 *code;
374         MonoJumpInfo *ji = NULL;
375         GSList *unwind_ops = NULL;
376         guint kMaxCodeSize = NACL_SIZE (64, 128);
377
378         /* call_filter (MonoContext *ctx, unsigned long eip) */
379         start = code = mono_global_codeman_reserve (kMaxCodeSize);
380
381         x86_push_reg (code, X86_EBP);
382         x86_mov_reg_reg (code, X86_EBP, X86_ESP, 4);
383         x86_push_reg (code, X86_EBX);
384         x86_push_reg (code, X86_EDI);
385         x86_push_reg (code, X86_ESI);
386
387         /* load ctx */
388         x86_mov_reg_membase (code, X86_EAX, X86_EBP, 8, 4);
389         /* load eip */
390         x86_mov_reg_membase (code, X86_ECX, X86_EBP, 12, 4);
391         /* save EBP */
392         x86_push_reg (code, X86_EBP);
393
394         /* set new EBP */
395         x86_mov_reg_membase (code, X86_EBP, X86_EAX,  MONO_STRUCT_OFFSET (MonoContext, ebp), 4);
396         /* restore registers used by global register allocation (EBX & ESI) */
397         x86_mov_reg_membase (code, X86_EBX, X86_EAX,  MONO_STRUCT_OFFSET (MonoContext, ebx), 4);
398         x86_mov_reg_membase (code, X86_ESI, X86_EAX,  MONO_STRUCT_OFFSET (MonoContext, esi), 4);
399         x86_mov_reg_membase (code, X86_EDI, X86_EAX,  MONO_STRUCT_OFFSET (MonoContext, edi), 4);
400
401         /* align stack and save ESP */
402         x86_mov_reg_reg (code, X86_EDX, X86_ESP, 4);
403         x86_alu_reg_imm (code, X86_AND, X86_ESP, -MONO_ARCH_FRAME_ALIGNMENT);
404         g_assert (MONO_ARCH_FRAME_ALIGNMENT >= 8);
405         x86_alu_reg_imm (code, X86_SUB, X86_ESP, MONO_ARCH_FRAME_ALIGNMENT - 8);
406         x86_push_reg (code, X86_EDX);
407
408         /* call the handler */
409         x86_call_reg (code, X86_ECX);
410
411         /* restore ESP */
412         x86_pop_reg (code, X86_ESP);
413
414         /* restore EBP */
415         x86_pop_reg (code, X86_EBP);
416
417         /* restore saved regs */
418         x86_pop_reg (code, X86_ESI);
419         x86_pop_reg (code, X86_EDI);
420         x86_pop_reg (code, X86_EBX);
421         x86_leave (code);
422         x86_ret (code);
423
424         nacl_global_codeman_validate(&start, kMaxCodeSize, &code);
425
426         if (info)
427                 *info = mono_tramp_info_create ("call_filter", start, code - start, ji, unwind_ops);
428         else {
429                 GSList *l;
430
431                 for (l = unwind_ops; l; l = l->next)
432                         g_free (l->data);
433                 g_slist_free (unwind_ops);
434         }
435
436         g_assert ((code - start) < kMaxCodeSize);
437         return start;
438 }
439
440 /*
441  * mono_x86_throw_exception:
442  *
443  *   C function called from the throw trampolines.
444  */
445 void
446 mono_x86_throw_exception (mgreg_t *regs, MonoObject *exc, 
447                                                   mgreg_t eip, gboolean rethrow)
448 {
449         MonoContext ctx;
450
451         ctx.esp = regs [X86_ESP];
452         ctx.eip = eip;
453         ctx.ebp = regs [X86_EBP];
454         ctx.edi = regs [X86_EDI];
455         ctx.esi = regs [X86_ESI];
456         ctx.ebx = regs [X86_EBX];
457         ctx.edx = regs [X86_EDX];
458         ctx.ecx = regs [X86_ECX];
459         ctx.eax = regs [X86_EAX];
460
461 #ifdef __APPLE__
462         /* The OSX ABI specifies 16 byte alignment at call sites */
463         g_assert ((ctx.esp % MONO_ARCH_FRAME_ALIGNMENT) == 0);
464 #endif
465
466         if (mono_object_isinst (exc, mono_defaults.exception_class)) {
467                 MonoException *mono_ex = (MonoException*)exc;
468                 if (!rethrow)
469                         mono_ex->stack_trace = NULL;
470         }
471
472         /* adjust eip so that it point into the call instruction */
473         ctx.eip -= 1;
474
475         mono_handle_exception (&ctx, exc);
476
477         mono_restore_context (&ctx);
478
479         g_assert_not_reached ();
480 }
481
482 void
483 mono_x86_throw_corlib_exception (mgreg_t *regs, guint32 ex_token_index, 
484                                                                  mgreg_t eip, gint32 pc_offset)
485 {
486         guint32 ex_token = MONO_TOKEN_TYPE_DEF | ex_token_index;
487         MonoException *ex;
488
489         ex = mono_exception_from_token (mono_defaults.exception_class->image, ex_token);
490
491         eip -= pc_offset;
492
493         /* Negate the ip adjustment done in mono_x86_throw_exception () */
494         eip += 1;
495
496         mono_x86_throw_exception (regs, (MonoObject*)ex, eip, FALSE);
497 }
498
499 static void
500 mono_x86_resume_unwind (mgreg_t *regs, MonoObject *exc, 
501                                                 mgreg_t eip, gboolean rethrow)
502 {
503         MonoContext ctx;
504
505         ctx.esp = regs [X86_ESP];
506         ctx.eip = eip;
507         ctx.ebp = regs [X86_EBP];
508         ctx.edi = regs [X86_EDI];
509         ctx.esi = regs [X86_ESI];
510         ctx.ebx = regs [X86_EBX];
511         ctx.edx = regs [X86_EDX];
512         ctx.ecx = regs [X86_ECX];
513         ctx.eax = regs [X86_EAX];
514
515         mono_resume_unwind (&ctx);
516 }
517
518 /*
519  * get_throw_trampoline:
520  *
521  *  Generate a call to mono_x86_throw_exception/
522  * mono_x86_throw_corlib_exception.
523  * If LLVM is true, generate code which assumes the caller is LLVM generated code, 
524  * which doesn't push the arguments.
525  */
526 static guint8*
527 get_throw_trampoline (const char *name, gboolean rethrow, gboolean llvm, gboolean corlib, gboolean llvm_abs, gboolean resume_unwind, MonoTrampInfo **info, gboolean aot)
528 {
529         guint8 *start, *code;
530         int i, stack_size, stack_offset, arg_offsets [5], regs_offset;
531         MonoJumpInfo *ji = NULL;
532         GSList *unwind_ops = NULL;
533         guint kMaxCodeSize = NACL_SIZE (128, 256);
534
535         start = code = mono_global_codeman_reserve (kMaxCodeSize);
536
537         stack_size = 128;
538
539         /* 
540          * On apple, the stack is misaligned by the pushing of the return address.
541          */
542         if (!llvm && corlib)
543                 /* On OSX, we don't generate alignment code to save space */
544                 stack_size += 4;
545         else
546                 stack_size += MONO_ARCH_FRAME_ALIGNMENT - 4;
547
548         /*
549          * The stack looks like this:
550          * <pc offset> (only if corlib is TRUE)
551          * <exception object>/<type token>
552          * <return addr> <- esp (unaligned on apple)
553          */
554
555         mono_add_unwind_op_def_cfa (unwind_ops, (guint8*)NULL, (guint8*)NULL, X86_ESP, 4);
556         mono_add_unwind_op_offset (unwind_ops, (guint8*)NULL, (guint8*)NULL, X86_NREG, -4);
557
558         /* Alloc frame */
559         x86_alu_reg_imm (code, X86_SUB, X86_ESP, stack_size);
560         mono_add_unwind_op_def_cfa_offset (unwind_ops, code, start, stack_size + 4);
561
562         arg_offsets [0] = 0;
563         arg_offsets [1] = 4;
564         arg_offsets [2] = 8;
565         arg_offsets [3] = 12;
566         regs_offset = 16;
567
568         /* Save registers */
569         for (i = 0; i < X86_NREG; ++i)
570                 if (i != X86_ESP)
571                         x86_mov_membase_reg (code, X86_ESP, regs_offset + (i * 4), i, 4);
572         /* Calculate the offset between the current sp and the sp of the caller */
573         if (llvm) {
574                 /* LLVM doesn't push the arguments */
575                 stack_offset = stack_size + 4;
576         } else {
577                 if (corlib) {
578                         /* Two arguments */
579                         stack_offset = stack_size + 4 + 8;
580 #ifdef __APPLE__
581                         /* We don't generate stack alignment code on osx to save space */
582 #endif
583                 } else {
584                         /* One argument + stack alignment */
585                         stack_offset = stack_size + 4 + 4;
586 #ifdef __APPLE__
587                         /* Pop the alignment added by OP_THROW too */
588                         stack_offset += MONO_ARCH_FRAME_ALIGNMENT - 4;
589 #else
590                         if (mono_do_x86_stack_align)
591                                 stack_offset += MONO_ARCH_FRAME_ALIGNMENT - 4;
592 #endif
593                 }
594         }
595         /* Save ESP */
596         x86_lea_membase (code, X86_EAX, X86_ESP, stack_offset);
597         x86_mov_membase_reg (code, X86_ESP, regs_offset + (X86_ESP * 4), X86_EAX, 4);
598
599         /* Set arg1 == regs */
600         x86_lea_membase (code, X86_EAX, X86_ESP, regs_offset);
601         x86_mov_membase_reg (code, X86_ESP, arg_offsets [0], X86_EAX, 4);
602         /* Set arg2 == exc/ex_token_index */
603         if (resume_unwind)
604                 x86_mov_reg_imm (code, X86_EAX, 0);
605         else
606                 x86_mov_reg_membase (code, X86_EAX, X86_ESP, stack_size + 4, 4);
607         x86_mov_membase_reg (code, X86_ESP, arg_offsets [1], X86_EAX, 4);
608         /* Set arg3 == eip */
609         if (llvm_abs)
610                 x86_alu_reg_reg (code, X86_XOR, X86_EAX, X86_EAX);
611         else
612                 x86_mov_reg_membase (code, X86_EAX, X86_ESP, stack_size, 4);
613         x86_mov_membase_reg (code, X86_ESP, arg_offsets [2], X86_EAX, 4);
614         /* Set arg4 == rethrow/pc_offset */
615         if (resume_unwind) {
616                 x86_mov_membase_imm (code, X86_ESP, arg_offsets [3], 0, 4);
617         } else if (corlib) {
618                 x86_mov_reg_membase (code, X86_EAX, X86_ESP, stack_size + 8, 4);
619                 if (llvm_abs) {
620                         /* 
621                          * The caller is LLVM code which passes the absolute address not a pc offset,
622                          * so compensate by passing 0 as 'ip' and passing the negated abs address as
623                          * the pc offset.
624                          */
625                         x86_neg_reg (code, X86_EAX);
626                 }
627                 x86_mov_membase_reg (code, X86_ESP, arg_offsets [3], X86_EAX, 4);
628         } else {
629                 x86_mov_membase_imm (code, X86_ESP, arg_offsets [3], rethrow, 4);
630         }
631         /* Make the call */
632         if (aot) {
633                 // This can be called from runtime code, which can't guarantee that
634                 // ebx contains the got address.
635                 // So emit the got address loading code too
636                 code = mono_arch_emit_load_got_addr (start, code, NULL, &ji);
637                 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");
638                 x86_call_reg (code, X86_EAX);
639         } else {
640                 x86_call_code (code, resume_unwind ? (gpointer)(mono_x86_resume_unwind) : (corlib ? (gpointer)mono_x86_throw_corlib_exception : (gpointer)mono_x86_throw_exception));
641         }
642         x86_breakpoint (code);
643
644         nacl_global_codeman_validate(&start, kMaxCodeSize, &code);
645
646         g_assert ((code - start) < kMaxCodeSize);
647
648         if (info)
649                 *info = mono_tramp_info_create (name, start, code - start, ji, unwind_ops);
650         else {
651                 GSList *l;
652
653                 for (l = unwind_ops; l; l = l->next)
654                         g_free (l->data);
655                 g_slist_free (unwind_ops);
656         }
657
658         return start;
659 }
660
661 /**
662  * mono_arch_get_throw_exception:
663  *
664  * Returns a function pointer which can be used to raise 
665  * exceptions. The returned function has the following 
666  * signature: void (*func) (MonoException *exc); 
667  * For example to raise an arithmetic exception you can use:
668  *
669  * x86_push_imm (code, mono_get_exception_arithmetic ()); 
670  * x86_call_code (code, arch_get_throw_exception ()); 
671  *
672  */
673 gpointer 
674 mono_arch_get_throw_exception (MonoTrampInfo **info, gboolean aot)
675 {
676         return get_throw_trampoline ("throw_exception", FALSE, FALSE, FALSE, FALSE, FALSE, info, aot);
677 }
678
679 gpointer 
680 mono_arch_get_rethrow_exception (MonoTrampInfo **info, gboolean aot)
681 {
682         return get_throw_trampoline ("rethrow_exception", TRUE, FALSE, FALSE, FALSE, FALSE, info, aot);
683 }
684
685 /**
686  * mono_arch_get_throw_corlib_exception:
687  *
688  * Returns a function pointer which can be used to raise 
689  * corlib exceptions. The returned function has the following 
690  * signature: void (*func) (guint32 ex_token, guint32 offset); 
691  * Here, offset is the offset which needs to be substracted from the caller IP 
692  * to get the IP of the throw. Passing the offset has the advantage that it 
693  * needs no relocations in the caller.
694  */
695 gpointer 
696 mono_arch_get_throw_corlib_exception (MonoTrampInfo **info, gboolean aot)
697 {
698         return get_throw_trampoline ("throw_corlib_exception", FALSE, FALSE, TRUE, FALSE, FALSE, info, aot);
699 }
700
701 void
702 mono_arch_exceptions_init (void)
703 {
704         guint8 *tramp;
705
706 /* 
707  * If we're running WoW64, we need to set the usermode exception policy 
708  * for SEHs to behave. This requires hotfix http://support.microsoft.com/kb/976038
709  * or (eventually) Windows 7 SP1.
710  */
711 #ifdef HOST_WIN32
712         DWORD flags;
713         FARPROC getter;
714         FARPROC setter;
715         HMODULE kernel32 = LoadLibraryW (L"kernel32.dll");
716
717         if (kernel32) {
718                 getter = GetProcAddress (kernel32, "GetProcessUserModeExceptionPolicy");
719                 setter = GetProcAddress (kernel32, "SetProcessUserModeExceptionPolicy");
720                 if (getter && setter) {
721                         if (getter (&flags))
722                                 setter (flags & ~PROCESS_CALLBACK_FILTER_ENABLED);
723                 }
724         }
725 #endif
726
727         if (mono_aot_only) {
728                 signal_exception_trampoline = mono_aot_get_trampoline ("x86_signal_exception_trampoline");
729                 return;
730         }
731
732         /* LLVM needs different throw trampolines */
733         tramp = get_throw_trampoline ("llvm_throw_exception_trampoline", FALSE, TRUE, FALSE, FALSE, FALSE, NULL, FALSE);
734         mono_register_jit_icall (tramp, "llvm_throw_exception_trampoline", NULL, TRUE);
735
736         tramp = get_throw_trampoline ("llvm_rethrow_exception_trampoline", FALSE, TRUE, FALSE, FALSE, FALSE, NULL, FALSE);
737         mono_register_jit_icall (tramp, "llvm_rethrow_exception_trampoline", NULL, TRUE);
738
739         tramp = get_throw_trampoline ("llvm_throw_corlib_exception_trampoline", FALSE, TRUE, TRUE, FALSE, FALSE, NULL, FALSE);
740         mono_register_jit_icall (tramp, "llvm_throw_corlib_exception_trampoline", NULL, TRUE);
741
742         tramp = get_throw_trampoline ("llvm_throw_corlib_exception_abs_trampoline", FALSE, TRUE, TRUE, TRUE, FALSE, NULL, FALSE);
743         mono_register_jit_icall (tramp, "llvm_throw_corlib_exception_abs_trampoline", NULL, TRUE);
744
745         tramp = get_throw_trampoline ("llvm_resume_unwind_trampoline", FALSE, FALSE, FALSE, FALSE, TRUE, NULL, FALSE);
746         mono_register_jit_icall (tramp, "llvm_resume_unwind_trampoline", NULL, TRUE);
747
748         signal_exception_trampoline = mono_x86_get_signal_exception_trampoline (NULL, FALSE);
749 }
750
751 /*
752  * mono_arch_find_jit_info:
753  *
754  * See exceptions-amd64.c for docs.
755  */
756 gboolean
757 mono_arch_find_jit_info (MonoDomain *domain, MonoJitTlsData *jit_tls, 
758                                                          MonoJitInfo *ji, MonoContext *ctx, 
759                                                          MonoContext *new_ctx, MonoLMF **lmf,
760                                                          mgreg_t **save_locations,
761                                                          StackFrameInfo *frame)
762 {
763         gpointer ip = MONO_CONTEXT_GET_IP (ctx);
764
765         memset (frame, 0, sizeof (StackFrameInfo));
766         frame->ji = ji;
767
768         *new_ctx = *ctx;
769
770         if (ji != NULL) {
771                 gssize regs [MONO_MAX_IREGS + 1];
772                 guint8 *cfa;
773                 guint32 unwind_info_len;
774                 guint8 *unwind_info;
775
776                 frame->type = FRAME_TYPE_MANAGED;
777
778                 unwind_info = mono_jinfo_get_unwind_info (ji, &unwind_info_len);
779
780                 regs [X86_EAX] = new_ctx->eax;
781                 regs [X86_EBX] = new_ctx->ebx;
782                 regs [X86_ECX] = new_ctx->ecx;
783                 regs [X86_EDX] = new_ctx->edx;
784                 regs [X86_ESP] = new_ctx->esp;
785                 regs [X86_EBP] = new_ctx->ebp;
786                 regs [X86_ESI] = new_ctx->esi;
787                 regs [X86_EDI] = new_ctx->edi;
788                 regs [X86_NREG] = new_ctx->eip;
789
790                 mono_unwind_frame (unwind_info, unwind_info_len, ji->code_start, 
791                                                    (guint8*)ji->code_start + ji->code_size,
792                                                    ip, NULL, regs, MONO_MAX_IREGS + 1,
793                                                    save_locations, MONO_MAX_IREGS, &cfa);
794
795                 new_ctx->eax = regs [X86_EAX];
796                 new_ctx->ebx = regs [X86_EBX];
797                 new_ctx->ecx = regs [X86_ECX];
798                 new_ctx->edx = regs [X86_EDX];
799                 new_ctx->esp = regs [X86_ESP];
800                 new_ctx->ebp = regs [X86_EBP];
801                 new_ctx->esi = regs [X86_ESI];
802                 new_ctx->edi = regs [X86_EDI];
803                 new_ctx->eip = regs [X86_NREG];
804
805                 /* The CFA becomes the new SP value */
806                 new_ctx->esp = (gssize)cfa;
807
808                 /* Adjust IP */
809                 new_ctx->eip --;
810
811                 return TRUE;
812         } else if (*lmf) {
813
814                 if (((guint64)(*lmf)->previous_lmf) & 2) {
815                         /* 
816                          * This LMF entry is created by the soft debug code to mark transitions to
817                          * managed code done during invokes.
818                          */
819                         MonoLMFExt *ext = (MonoLMFExt*)(*lmf);
820
821                         g_assert (ext->debugger_invoke);
822
823                         memcpy (new_ctx, &ext->ctx, sizeof (MonoContext));
824
825                         *lmf = (gpointer)(((gsize)(*lmf)->previous_lmf) & ~3);
826
827                         frame->type = FRAME_TYPE_DEBUGGER_INVOKE;
828
829                         return TRUE;
830                 }
831                 
832                 if ((ji = mini_jit_info_table_find (domain, (gpointer)(*lmf)->eip, NULL))) {
833                 } else {
834                         if (!((guint32)((*lmf)->previous_lmf) & 1))
835                                 /* Top LMF entry */
836                                 return FALSE;
837                         g_assert_not_reached ();
838                         /* Trampoline lmf frame */
839                         frame->method = (*lmf)->method;
840                 }
841
842                 new_ctx->esi = (*lmf)->esi;
843                 new_ctx->edi = (*lmf)->edi;
844                 new_ctx->ebx = (*lmf)->ebx;
845                 new_ctx->ebp = (*lmf)->ebp;
846                 new_ctx->eip = (*lmf)->eip;
847
848                 /* Adjust IP */
849                 new_ctx->eip --;
850
851                 frame->ji = ji;
852                 frame->type = FRAME_TYPE_MANAGED_TO_NATIVE;
853
854                 /* Check if we are in a trampoline LMF frame */
855                 if ((guint32)((*lmf)->previous_lmf) & 1) {
856                         /* lmf->esp is set by the trampoline code */
857                         new_ctx->esp = (*lmf)->esp;
858
859                         /* Pop arguments off the stack */
860                         /* FIXME: Handle the delegate case too ((*lmf)->method == NULL) */
861                         /* FIXME: Handle the IMT/vtable case too */
862 #if 0
863 #ifndef ENABLE_LLVM
864                         if ((*lmf)->method) {
865                                 MonoMethod *method = (*lmf)->method;
866                                 MonoJitArgumentInfo *arg_info = g_newa (MonoJitArgumentInfo, mono_method_signature (method)->param_count + 1);
867
868                                 guint32 stack_to_pop = mono_arch_get_argument_info (NULL, mono_method_signature (method), mono_method_signature (method)->param_count, arg_info);
869                                 new_ctx->esp += stack_to_pop;
870                         }
871 #endif
872 #endif
873                 }
874                 else
875                         /* the lmf is always stored on the stack, so the following
876                          * expression points to a stack location which can be used as ESP */
877                         new_ctx->esp = (unsigned long)&((*lmf)->eip);
878
879                 *lmf = (gpointer)(((gsize)(*lmf)->previous_lmf) & ~3);
880
881                 return TRUE;
882         }
883
884         return FALSE;
885 }
886
887 void
888 mono_arch_sigctx_to_monoctx (void *sigctx, MonoContext *mctx)
889 {
890         mono_sigctx_to_monoctx (sigctx, mctx);
891 }
892
893 void
894 mono_arch_monoctx_to_sigctx (MonoContext *mctx, void *sigctx)
895 {
896         mono_monoctx_to_sigctx (mctx, sigctx);
897 }
898
899 gpointer
900 mono_arch_ip_from_context (void *sigctx)
901 {
902 #if defined(__native_client__)
903         printf("WARNING: mono_arch_ip_from_context() called!\n");
904         return (NULL);
905 #elif defined(MONO_ARCH_USE_SIGACTION)
906         ucontext_t *ctx = (ucontext_t*)sigctx;
907         return (gpointer)UCONTEXT_REG_EIP (ctx);
908 #elif defined(HOST_WIN32)
909         return ((CONTEXT*)sigctx)->Eip;
910 #else
911         struct sigcontext *ctx = sigctx;
912         return (gpointer)ctx->SC_EIP;
913 #endif
914 }
915
916 /*
917  * handle_exception:
918  *
919  *   Called by resuming from a signal handler.
920  */
921 static void
922 handle_signal_exception (gpointer obj)
923 {
924         MonoJitTlsData *jit_tls = mono_native_tls_get_value (mono_jit_tls_id);
925         MonoContext ctx;
926
927         memcpy (&ctx, &jit_tls->ex_ctx, sizeof (MonoContext));
928
929         mono_handle_exception (&ctx, obj);
930
931         mono_restore_context (&ctx);
932 }
933
934 /*
935  * mono_x86_get_signal_exception_trampoline:
936  *
937  *   This x86 specific trampoline is used to call handle_signal_exception.
938  */
939 gpointer
940 mono_x86_get_signal_exception_trampoline (MonoTrampInfo **info, gboolean aot)
941 {
942         guint8 *start, *code;
943         MonoJumpInfo *ji = NULL;
944         GSList *unwind_ops = NULL;
945         int stack_size;
946
947         start = code = mono_global_codeman_reserve (128);
948
949         /* Caller ip */
950         x86_push_reg (code, X86_ECX);
951
952         mono_add_unwind_op_def_cfa (unwind_ops, (guint8*)NULL, (guint8*)NULL, X86_ESP, 4);
953         mono_add_unwind_op_offset (unwind_ops, (guint8*)NULL, (guint8*)NULL, X86_NREG, -4);
954
955         /* Fix the alignment to be what apple expects */
956         stack_size = 12;
957
958         x86_alu_reg_imm (code, X86_SUB, X86_ESP, stack_size);
959         mono_add_unwind_op_def_cfa_offset (unwind_ops, code, start, stack_size + 4);
960
961         /* Arg1 */
962         x86_mov_membase_reg (code, X86_ESP, 0, X86_EAX, 4);
963         /* Branch to target */
964         x86_call_reg (code, X86_EDX);
965
966         g_assert ((code - start) < 128);
967
968         if (info)
969                 *info = mono_tramp_info_create ("x86_signal_exception_trampoline", start, code - start, ji, unwind_ops);
970         else {
971                 GSList *l;
972
973                 for (l = unwind_ops; l; l = l->next)
974                         g_free (l->data);
975                 g_slist_free (unwind_ops);
976         }
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_arch_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_arch_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_arch_sigctx_to_monoctx (sigctx, &mctx);
1038
1039         mono_handle_exception (&mctx, obj);
1040
1041         mono_arch_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, 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);
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 }