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