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