2 * exceptions-x86.c: exception support for x86
5 * Dietmar Maurer (dietmar@ximian.com)
7 * (C) 2001 Ximian, Inc.
16 #include <mono/arch/x86/x86-codegen.h>
17 #include <mono/metadata/appdomain.h>
18 #include <mono/metadata/tabledefs.h>
19 #include <mono/metadata/threads.h>
20 #include <mono/metadata/debug-helpers.h>
21 #include <mono/metadata/exception.h>
22 #include <mono/metadata/gc-internal.h>
23 #include <mono/metadata/mono-debug.h>
24 #include <mono/utils/mono-mmap.h>
30 static gpointer signal_exception_trampoline;
33 mono_x86_get_signal_exception_trampoline (MonoTrampInfo **info, gboolean aot) MONO_INTERNAL;
36 static void (*restore_stack) (void *);
38 static MonoW32ExceptionHandler fpe_handler;
39 static MonoW32ExceptionHandler ill_handler;
40 static MonoW32ExceptionHandler segv_handler;
42 LPTOP_LEVEL_EXCEPTION_FILTER mono_old_win_toplevel_exception_filter;
43 gpointer mono_win_vectored_exception_handle;
44 extern int (*gUnhandledExceptionHandler)(EXCEPTION_POINTERS*);
46 #ifndef PROCESS_CALLBACK_FILTER_ENABLED
47 # define PROCESS_CALLBACK_FILTER_ENABLED 1
50 #define W32_SEH_HANDLE_EX(_ex) \
51 if (_ex##_handler) _ex##_handler(0, ep, sctx)
53 LONG CALLBACK seh_unhandled_exception_filter(EXCEPTION_POINTERS* ep)
55 #ifndef MONO_CROSS_COMPILE
56 if (mono_old_win_toplevel_exception_filter) {
57 return (*mono_old_win_toplevel_exception_filter)(ep);
61 mono_handle_native_sigsegv (SIGSEGV, NULL);
63 return EXCEPTION_CONTINUE_SEARCH;
67 * mono_win32_get_handle_stackoverflow (void):
69 * Returns a pointer to a method which restores the current context stack
70 * and calls handle_exceptions, when done restores the original stack.
73 mono_win32_get_handle_stackoverflow (void)
75 static guint8 *start = NULL;
81 /* restore_contect (void *sigctx) */
82 start = code = mono_global_codeman_reserve (128);
84 /* load context into ebx */
85 x86_mov_reg_membase (code, X86_EBX, X86_ESP, 4, 4);
87 /* move current stack into edi for later restore */
88 x86_mov_reg_reg (code, X86_EDI, X86_ESP, 4);
90 /* use the new freed stack from sigcontext */
91 x86_mov_reg_membase (code, X86_ESP, X86_EBX, G_STRUCT_OFFSET (struct sigcontext, esp), 4);
93 /* get the current domain */
94 x86_call_code (code, mono_domain_get);
96 /* get stack overflow exception from domain object */
97 x86_mov_reg_membase (code, X86_EAX, X86_EAX, G_STRUCT_OFFSET (MonoDomain, stack_overflow_ex), 4);
99 /* call mono_arch_handle_exception (sctx, stack_overflow_exception_obj) */
100 x86_push_reg (code, X86_EAX);
101 x86_push_reg (code, X86_EBX);
102 x86_call_code (code, mono_arch_handle_exception);
104 /* restore the SEH handler stack */
105 x86_mov_reg_reg (code, X86_ESP, X86_EDI, 4);
113 /* Special hack to workaround the fact that the
114 * when the SEH handler is called the stack is
115 * to small to recover.
117 * Stack walking part of this method is from mono_handle_exception
119 * The idea is simple;
120 * - walk the stack to free some space (64k)
121 * - set esp to new stack location
122 * - call mono_arch_handle_exception with stack overflow exception
123 * - set esp to SEH handlers stack
127 win32_handle_stack_overflow (EXCEPTION_POINTERS* ep, struct sigcontext *sctx)
131 MonoDomain *domain = mono_domain_get ();
133 MonoJitTlsData *jit_tls = mono_native_tls_get_value (mono_jit_tls_id);
134 MonoLMF *lmf = jit_tls->lmf;
135 MonoContext initial_ctx;
137 guint32 free_stack = 0;
138 StackFrameInfo frame;
140 /* convert sigcontext to MonoContext (due to reuse of stack walking helpers */
141 mono_arch_sigctx_to_monoctx (sctx, &ctx);
143 /* get our os page size */
145 page_size = si.dwPageSize;
147 /* Let's walk the stack to recover
148 * the needed stack space (if possible)
150 memset (&rji, 0, sizeof (rji));
153 free_stack = (guint8*)(MONO_CONTEXT_GET_BP (&ctx)) - (guint8*)(MONO_CONTEXT_GET_BP (&initial_ctx));
155 /* try to free 64kb from our stack */
159 mono_arch_find_jit_info (domain, jit_tls, &rji, &ctx, &new_ctx, &lmf, NULL, &frame);
161 g_warning ("Exception inside function without unwind info");
162 g_assert_not_reached ();
165 if (frame.ji != (gpointer)-1) {
166 free_stack = (guint8*)(MONO_CONTEXT_GET_BP (&ctx)) - (guint8*)(MONO_CONTEXT_GET_BP (&initial_ctx));
169 /* todo: we should call abort if ji is -1 */
171 } while (free_stack < 64 * 1024 && frame.ji != (gpointer) -1);
173 /* convert into sigcontext to be used in mono_arch_handle_exception */
174 mono_arch_monoctx_to_sigctx (&ctx, sctx);
176 /* todo: install new stack-guard page */
178 /* use the new stack and call mono_arch_handle_exception () */
179 restore_stack (sctx);
183 * Unhandled Exception Filter
184 * Top-level per-process exception handler.
186 LONG CALLBACK seh_vectored_exception_handler(EXCEPTION_POINTERS* ep)
188 EXCEPTION_RECORD* er;
190 struct sigcontext* sctx;
192 MonoJitTlsData *jit_tls = mono_native_tls_get_value (mono_jit_tls_id);
194 /* If the thread is not managed by the runtime return early */
196 return EXCEPTION_CONTINUE_SEARCH;
198 jit_tls->mono_win_chained_exception_needs_run = FALSE;
199 res = EXCEPTION_CONTINUE_EXECUTION;
201 er = ep->ExceptionRecord;
202 ctx = ep->ContextRecord;
203 sctx = g_malloc(sizeof(struct sigcontext));
205 /* Copy Win32 context to UNIX style context */
206 sctx->eax = ctx->Eax;
207 sctx->ebx = ctx->Ebx;
208 sctx->ecx = ctx->Ecx;
209 sctx->edx = ctx->Edx;
210 sctx->ebp = ctx->Ebp;
211 sctx->esp = ctx->Esp;
212 sctx->esi = ctx->Esi;
213 sctx->edi = ctx->Edi;
214 sctx->eip = ctx->Eip;
216 switch (er->ExceptionCode) {
217 case EXCEPTION_STACK_OVERFLOW:
218 win32_handle_stack_overflow (ep, sctx);
220 case EXCEPTION_ACCESS_VIOLATION:
221 W32_SEH_HANDLE_EX(segv);
223 case EXCEPTION_ILLEGAL_INSTRUCTION:
224 W32_SEH_HANDLE_EX(ill);
226 case EXCEPTION_INT_DIVIDE_BY_ZERO:
227 case EXCEPTION_INT_OVERFLOW:
228 case EXCEPTION_FLT_DIVIDE_BY_ZERO:
229 case EXCEPTION_FLT_OVERFLOW:
230 case EXCEPTION_FLT_UNDERFLOW:
231 case EXCEPTION_FLT_INEXACT_RESULT:
232 W32_SEH_HANDLE_EX(fpe);
235 jit_tls->mono_win_chained_exception_needs_run = TRUE;
239 if (jit_tls->mono_win_chained_exception_needs_run) {
240 /* Don't copy context back if we chained exception
241 * as the handler may have modfied the EXCEPTION_POINTERS
242 * directly. We don't pass sigcontext to chained handlers.
243 * Return continue search so the UnhandledExceptionFilter
244 * can correctly chain the exception.
246 res = EXCEPTION_CONTINUE_SEARCH;
248 /* Copy context back */
249 ctx->Eax = sctx->eax;
250 ctx->Ebx = sctx->ebx;
251 ctx->Ecx = sctx->ecx;
252 ctx->Edx = sctx->edx;
253 ctx->Ebp = sctx->ebp;
254 ctx->Esp = sctx->esp;
255 ctx->Esi = sctx->esi;
256 ctx->Edi = sctx->edi;
257 ctx->Eip = sctx->eip;
260 /* TODO: Find right place to free this in stack overflow case */
261 if (er->ExceptionCode != EXCEPTION_STACK_OVERFLOW)
267 void win32_seh_init()
269 /* install restore stack helper */
271 restore_stack = mono_win32_get_handle_stackoverflow ();
273 mono_old_win_toplevel_exception_filter = SetUnhandledExceptionFilter(seh_unhandled_exception_filter);
274 mono_win_vectored_exception_handle = AddVectoredExceptionHandler (1, seh_vectored_exception_handler);
277 void win32_seh_cleanup()
279 if (mono_old_win_toplevel_exception_filter) SetUnhandledExceptionFilter(mono_old_win_toplevel_exception_filter);
280 RemoveVectoredExceptionHandler (seh_unhandled_exception_filter);
283 void win32_seh_set_handler(int type, MonoW32ExceptionHandler handler)
287 fpe_handler = handler;
290 ill_handler = handler;
293 segv_handler = handler;
300 #endif /* TARGET_WIN32 */
303 * mono_arch_get_restore_context:
305 * Returns a pointer to a method which restores a previously saved sigcontext.
308 mono_arch_get_restore_context (MonoTrampInfo **info, gboolean aot)
310 guint8 *start = NULL;
312 MonoJumpInfo *ji = NULL;
313 GSList *unwind_ops = NULL;
315 /* restore_contect (MonoContext *ctx) */
317 start = code = mono_global_codeman_reserve (128);
320 x86_mov_reg_membase (code, X86_EAX, X86_ESP, 4, 4);
323 x86_mov_reg_membase (code, X86_EBX, X86_EAX, G_STRUCT_OFFSET (MonoContext, ebx), 4);
326 x86_mov_reg_membase (code, X86_EDI, X86_EAX, G_STRUCT_OFFSET (MonoContext, edi), 4);
329 x86_mov_reg_membase (code, X86_ESI, X86_EAX, G_STRUCT_OFFSET (MonoContext, esi), 4);
332 x86_mov_reg_membase (code, X86_EDX, X86_EAX, G_STRUCT_OFFSET (MonoContext, edx), 4);
335 * The context resides on the stack, in the stack frame of the
336 * caller of this function. The stack pointer that we need to
337 * restore is potentially many stack frames higher up, so the
338 * distance between them can easily be more than the red zone
339 * size. Hence the stack pointer can be restored only after
340 * we have finished loading everything from the context.
343 /* load ESP into EBP */
344 x86_mov_reg_membase (code, X86_EBP, X86_EAX, G_STRUCT_OFFSET (MonoContext, esp), 4);
345 /* load return address into ECX */
346 x86_mov_reg_membase (code, X86_ECX, X86_EAX, G_STRUCT_OFFSET (MonoContext, eip), 4);
347 /* save the return addr to the restored stack - 4 */
348 x86_mov_membase_reg (code, X86_EBP, -4, X86_ECX, 4);
350 /* load EBP into ECX */
351 x86_mov_reg_membase (code, X86_ECX, X86_EAX, G_STRUCT_OFFSET (MonoContext, ebp), 4);
352 /* save EBP to the restored stack - 8 */
353 x86_mov_membase_reg (code, X86_EBP, -8, X86_ECX, 4);
355 /* load EAX into ECX */
356 x86_mov_reg_membase (code, X86_ECX, X86_EAX, G_STRUCT_OFFSET (MonoContext, eax), 4);
357 /* save EAX to the restored stack - 12 */
358 x86_mov_membase_reg (code, X86_EBP, -12, X86_ECX, 4);
361 x86_mov_reg_membase (code, X86_ECX, X86_EAX, G_STRUCT_OFFSET (MonoContext, ecx), 4);
363 /* restore ESP - 12 */
364 x86_lea_membase (code, X86_ESP, X86_EBP, -12);
366 x86_pop_reg (code, X86_EAX);
368 x86_pop_reg (code, X86_EBP);
369 /* jump to the saved IP */
372 nacl_global_codeman_validate(&start, 128, &code);
375 *info = mono_tramp_info_create ("restore_context", start, code - start, ji, unwind_ops);
379 for (l = unwind_ops; l; l = l->next)
381 g_slist_free (unwind_ops);
388 * mono_arch_get_call_filter:
390 * Returns a pointer to a method which calls an exception filter. We
391 * also use this function to call finally handlers (we pass NULL as
392 * @exc object in this case).
395 mono_arch_get_call_filter (MonoTrampInfo **info, gboolean aot)
399 MonoJumpInfo *ji = NULL;
400 GSList *unwind_ops = NULL;
401 guint kMaxCodeSize = NACL_SIZE (64, 128);
403 /* call_filter (MonoContext *ctx, unsigned long eip) */
404 start = code = mono_global_codeman_reserve (kMaxCodeSize);
406 x86_push_reg (code, X86_EBP);
407 x86_mov_reg_reg (code, X86_EBP, X86_ESP, 4);
408 x86_push_reg (code, X86_EBX);
409 x86_push_reg (code, X86_EDI);
410 x86_push_reg (code, X86_ESI);
413 x86_mov_reg_membase (code, X86_EAX, X86_EBP, 8, 4);
415 x86_mov_reg_membase (code, X86_ECX, X86_EBP, 12, 4);
417 x86_push_reg (code, X86_EBP);
420 x86_mov_reg_membase (code, X86_EBP, X86_EAX, G_STRUCT_OFFSET (MonoContext, ebp), 4);
421 /* restore registers used by global register allocation (EBX & ESI) */
422 x86_mov_reg_membase (code, X86_EBX, X86_EAX, G_STRUCT_OFFSET (MonoContext, ebx), 4);
423 x86_mov_reg_membase (code, X86_ESI, X86_EAX, G_STRUCT_OFFSET (MonoContext, esi), 4);
424 x86_mov_reg_membase (code, X86_EDI, X86_EAX, G_STRUCT_OFFSET (MonoContext, edi), 4);
426 /* align stack and save ESP */
427 x86_mov_reg_reg (code, X86_EDX, X86_ESP, 4);
428 x86_alu_reg_imm (code, X86_AND, X86_ESP, -MONO_ARCH_FRAME_ALIGNMENT);
429 g_assert (MONO_ARCH_FRAME_ALIGNMENT >= 8);
430 x86_alu_reg_imm (code, X86_SUB, X86_ESP, MONO_ARCH_FRAME_ALIGNMENT - 8);
431 x86_push_reg (code, X86_EDX);
433 /* call the handler */
434 x86_call_reg (code, X86_ECX);
437 x86_pop_reg (code, X86_ESP);
440 x86_pop_reg (code, X86_EBP);
442 /* restore saved regs */
443 x86_pop_reg (code, X86_ESI);
444 x86_pop_reg (code, X86_EDI);
445 x86_pop_reg (code, X86_EBX);
449 nacl_global_codeman_validate(&start, kMaxCodeSize, &code);
452 *info = mono_tramp_info_create ("call_filter", start, code - start, ji, unwind_ops);
456 for (l = unwind_ops; l; l = l->next)
458 g_slist_free (unwind_ops);
461 g_assert ((code - start) < kMaxCodeSize);
466 * mono_x86_throw_exception:
468 * C function called from the throw trampolines.
471 mono_x86_throw_exception (mgreg_t *regs, MonoObject *exc,
472 mgreg_t eip, gboolean rethrow)
476 ctx.esp = regs [X86_ESP];
478 ctx.ebp = regs [X86_EBP];
479 ctx.edi = regs [X86_EDI];
480 ctx.esi = regs [X86_ESI];
481 ctx.ebx = regs [X86_EBX];
482 ctx.edx = regs [X86_EDX];
483 ctx.ecx = regs [X86_ECX];
484 ctx.eax = regs [X86_EAX];
487 /* The OSX ABI specifies 16 byte alignment at call sites */
488 g_assert ((ctx.esp % MONO_ARCH_FRAME_ALIGNMENT) == 0);
491 if (mono_object_isinst (exc, mono_defaults.exception_class)) {
492 MonoException *mono_ex = (MonoException*)exc;
494 mono_ex->stack_trace = NULL;
497 /* adjust eip so that it point into the call instruction */
500 mono_handle_exception (&ctx, exc);
502 mono_restore_context (&ctx);
504 g_assert_not_reached ();
508 mono_x86_throw_corlib_exception (mgreg_t *regs, guint32 ex_token_index,
509 mgreg_t eip, gint32 pc_offset)
511 guint32 ex_token = MONO_TOKEN_TYPE_DEF | ex_token_index;
514 ex = mono_exception_from_token (mono_defaults.exception_class->image, ex_token);
518 /* Negate the ip adjustment done in mono_x86_throw_exception () */
521 mono_x86_throw_exception (regs, (MonoObject*)ex, eip, FALSE);
525 mono_x86_resume_unwind (mgreg_t *regs, MonoObject *exc,
526 mgreg_t eip, gboolean rethrow)
530 ctx.esp = regs [X86_ESP];
532 ctx.ebp = regs [X86_EBP];
533 ctx.edi = regs [X86_EDI];
534 ctx.esi = regs [X86_ESI];
535 ctx.ebx = regs [X86_EBX];
536 ctx.edx = regs [X86_EDX];
537 ctx.ecx = regs [X86_ECX];
538 ctx.eax = regs [X86_EAX];
540 mono_resume_unwind (&ctx);
544 * get_throw_trampoline:
546 * Generate a call to mono_x86_throw_exception/
547 * mono_x86_throw_corlib_exception.
548 * If LLVM is true, generate code which assumes the caller is LLVM generated code,
549 * which doesn't push the arguments.
552 get_throw_trampoline (const char *name, gboolean rethrow, gboolean llvm, gboolean corlib, gboolean llvm_abs, gboolean resume_unwind, MonoTrampInfo **info, gboolean aot)
554 guint8 *start, *code;
555 int i, stack_size, stack_offset, arg_offsets [5], regs_offset;
556 MonoJumpInfo *ji = NULL;
557 GSList *unwind_ops = NULL;
558 guint kMaxCodeSize = NACL_SIZE (128, 256);
560 start = code = mono_global_codeman_reserve (kMaxCodeSize);
565 * On apple, the stack is misaligned by the pushing of the return address.
568 /* On OSX, we don't generate alignment code to save space */
571 stack_size += MONO_ARCH_FRAME_ALIGNMENT - 4;
574 * The stack looks like this:
575 * <pc offset> (only if corlib is TRUE)
576 * <exception object>/<type token>
577 * <return addr> <- esp (unaligned on apple)
580 mono_add_unwind_op_def_cfa (unwind_ops, (guint8*)NULL, (guint8*)NULL, X86_ESP, 4);
581 mono_add_unwind_op_offset (unwind_ops, (guint8*)NULL, (guint8*)NULL, X86_NREG, -4);
584 x86_alu_reg_imm (code, X86_SUB, X86_ESP, stack_size);
585 mono_add_unwind_op_def_cfa_offset (unwind_ops, code, start, stack_size + 4);
590 arg_offsets [3] = 12;
594 for (i = 0; i < X86_NREG; ++i)
596 x86_mov_membase_reg (code, X86_ESP, regs_offset + (i * 4), i, 4);
597 /* Calculate the offset between the current sp and the sp of the caller */
599 /* LLVM doesn't push the arguments */
600 stack_offset = stack_size + 4;
604 stack_offset = stack_size + 4 + 8;
606 /* We don't generate stack alignment code on osx to save space */
609 /* One argument + stack alignment */
610 stack_offset = stack_size + 4 + 4;
612 /* Pop the alignment added by OP_THROW too */
613 stack_offset += MONO_ARCH_FRAME_ALIGNMENT - 4;
615 if (mono_do_x86_stack_align)
616 stack_offset += MONO_ARCH_FRAME_ALIGNMENT - 4;
621 x86_lea_membase (code, X86_EAX, X86_ESP, stack_offset);
622 x86_mov_membase_reg (code, X86_ESP, regs_offset + (X86_ESP * 4), X86_EAX, 4);
624 /* Set arg1 == regs */
625 x86_lea_membase (code, X86_EAX, X86_ESP, regs_offset);
626 x86_mov_membase_reg (code, X86_ESP, arg_offsets [0], X86_EAX, 4);
627 /* Set arg2 == exc/ex_token_index */
629 x86_mov_reg_imm (code, X86_EAX, 0);
631 x86_mov_reg_membase (code, X86_EAX, X86_ESP, stack_size + 4, 4);
632 x86_mov_membase_reg (code, X86_ESP, arg_offsets [1], X86_EAX, 4);
633 /* Set arg3 == eip */
635 x86_alu_reg_reg (code, X86_XOR, X86_EAX, X86_EAX);
637 x86_mov_reg_membase (code, X86_EAX, X86_ESP, stack_size, 4);
638 x86_mov_membase_reg (code, X86_ESP, arg_offsets [2], X86_EAX, 4);
639 /* Set arg4 == rethrow/pc_offset */
641 x86_mov_membase_imm (code, X86_ESP, arg_offsets [3], 0, 4);
643 x86_mov_reg_membase (code, X86_EAX, X86_ESP, stack_size + 8, 4);
646 * The caller is LLVM code which passes the absolute address not a pc offset,
647 * so compensate by passing 0 as 'ip' and passing the negated abs address as
650 x86_neg_reg (code, X86_EAX);
652 x86_mov_membase_reg (code, X86_ESP, arg_offsets [3], X86_EAX, 4);
654 x86_mov_membase_imm (code, X86_ESP, arg_offsets [3], rethrow, 4);
658 // This can be called from runtime code, which can't guarantee that
659 // ebx contains the got address.
660 // So emit the got address loading code too
661 code = mono_arch_emit_load_got_addr (start, code, NULL, &ji);
662 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");
663 x86_call_reg (code, X86_EAX);
665 x86_call_code (code, resume_unwind ? (gpointer)(mono_x86_resume_unwind) : (corlib ? (gpointer)mono_x86_throw_corlib_exception : (gpointer)mono_x86_throw_exception));
667 x86_breakpoint (code);
669 nacl_global_codeman_validate(&start, kMaxCodeSize, &code);
671 g_assert ((code - start) < kMaxCodeSize);
674 *info = mono_tramp_info_create (name, start, code - start, ji, unwind_ops);
678 for (l = unwind_ops; l; l = l->next)
680 g_slist_free (unwind_ops);
687 * mono_arch_get_throw_exception:
689 * Returns a function pointer which can be used to raise
690 * exceptions. The returned function has the following
691 * signature: void (*func) (MonoException *exc);
692 * For example to raise an arithmetic exception you can use:
694 * x86_push_imm (code, mono_get_exception_arithmetic ());
695 * x86_call_code (code, arch_get_throw_exception ());
699 mono_arch_get_throw_exception (MonoTrampInfo **info, gboolean aot)
701 return get_throw_trampoline ("throw_exception", FALSE, FALSE, FALSE, FALSE, FALSE, info, aot);
705 mono_arch_get_rethrow_exception (MonoTrampInfo **info, gboolean aot)
707 return get_throw_trampoline ("rethrow_exception", TRUE, FALSE, FALSE, FALSE, FALSE, info, aot);
711 * mono_arch_get_throw_corlib_exception:
713 * Returns a function pointer which can be used to raise
714 * corlib exceptions. The returned function has the following
715 * signature: void (*func) (guint32 ex_token, guint32 offset);
716 * Here, offset is the offset which needs to be substracted from the caller IP
717 * to get the IP of the throw. Passing the offset has the advantage that it
718 * needs no relocations in the caller.
721 mono_arch_get_throw_corlib_exception (MonoTrampInfo **info, gboolean aot)
723 return get_throw_trampoline ("throw_corlib_exception", FALSE, FALSE, TRUE, FALSE, FALSE, info, aot);
727 mono_arch_exceptions_init (void)
732 * If we're running WoW64, we need to set the usermode exception policy
733 * for SEHs to behave. This requires hotfix http://support.microsoft.com/kb/976038
734 * or (eventually) Windows 7 SP1.
740 HMODULE kernel32 = LoadLibraryW (L"kernel32.dll");
743 getter = GetProcAddress (kernel32, "GetProcessUserModeExceptionPolicy");
744 setter = GetProcAddress (kernel32, "SetProcessUserModeExceptionPolicy");
745 if (getter && setter) {
747 setter (flags & ~PROCESS_CALLBACK_FILTER_ENABLED);
753 signal_exception_trampoline = mono_aot_get_trampoline ("x86_signal_exception_trampoline");
757 /* LLVM needs different throw trampolines */
758 tramp = get_throw_trampoline ("llvm_throw_exception_trampoline", FALSE, TRUE, FALSE, FALSE, FALSE, NULL, FALSE);
759 mono_register_jit_icall (tramp, "llvm_throw_exception_trampoline", NULL, TRUE);
761 tramp = get_throw_trampoline ("llvm_rethrow_exception_trampoline", FALSE, TRUE, FALSE, FALSE, FALSE, NULL, FALSE);
762 mono_register_jit_icall (tramp, "llvm_rethrow_exception_trampoline", NULL, TRUE);
764 tramp = get_throw_trampoline ("llvm_throw_corlib_exception_trampoline", FALSE, TRUE, TRUE, FALSE, FALSE, NULL, FALSE);
765 mono_register_jit_icall (tramp, "llvm_throw_corlib_exception_trampoline", NULL, TRUE);
767 tramp = get_throw_trampoline ("llvm_throw_corlib_exception_abs_trampoline", FALSE, TRUE, TRUE, TRUE, FALSE, NULL, FALSE);
768 mono_register_jit_icall (tramp, "llvm_throw_corlib_exception_abs_trampoline", NULL, TRUE);
770 tramp = get_throw_trampoline ("llvm_resume_unwind_trampoline", FALSE, FALSE, FALSE, FALSE, TRUE, NULL, FALSE);
771 mono_register_jit_icall (tramp, "llvm_resume_unwind_trampoline", NULL, TRUE);
773 signal_exception_trampoline = mono_x86_get_signal_exception_trampoline (NULL, FALSE);
777 * mono_arch_find_jit_info:
779 * See exceptions-amd64.c for docs.
782 mono_arch_find_jit_info (MonoDomain *domain, MonoJitTlsData *jit_tls,
783 MonoJitInfo *ji, MonoContext *ctx,
784 MonoContext *new_ctx, MonoLMF **lmf,
785 mgreg_t **save_locations,
786 StackFrameInfo *frame)
788 gpointer ip = MONO_CONTEXT_GET_IP (ctx);
790 memset (frame, 0, sizeof (StackFrameInfo));
796 gssize regs [MONO_MAX_IREGS + 1];
798 guint32 unwind_info_len;
801 frame->type = FRAME_TYPE_MANAGED;
803 unwind_info = mono_jinfo_get_unwind_info (ji, &unwind_info_len);
805 regs [X86_EAX] = new_ctx->eax;
806 regs [X86_EBX] = new_ctx->ebx;
807 regs [X86_ECX] = new_ctx->ecx;
808 regs [X86_EDX] = new_ctx->edx;
809 regs [X86_ESP] = new_ctx->esp;
810 regs [X86_EBP] = new_ctx->ebp;
811 regs [X86_ESI] = new_ctx->esi;
812 regs [X86_EDI] = new_ctx->edi;
813 regs [X86_NREG] = new_ctx->eip;
815 mono_unwind_frame (unwind_info, unwind_info_len, ji->code_start,
816 (guint8*)ji->code_start + ji->code_size,
817 ip, regs, MONO_MAX_IREGS + 1,
818 save_locations, MONO_MAX_IREGS, &cfa);
820 new_ctx->eax = regs [X86_EAX];
821 new_ctx->ebx = regs [X86_EBX];
822 new_ctx->ecx = regs [X86_ECX];
823 new_ctx->edx = regs [X86_EDX];
824 new_ctx->esp = regs [X86_ESP];
825 new_ctx->ebp = regs [X86_EBP];
826 new_ctx->esi = regs [X86_ESI];
827 new_ctx->edi = regs [X86_EDI];
828 new_ctx->eip = regs [X86_NREG];
830 /* The CFA becomes the new SP value */
831 new_ctx->esp = (gssize)cfa;
837 #ifndef MONO_X86_NO_PUSHES
838 /* Pop arguments off the stack */
839 if (ji->has_arch_eh_info) {
842 stack_size = mono_jit_info_get_arch_eh_info (ji)->stack_size;
846 MonoJitInfo *caller_ji;
848 caller_ji = mini_jit_info_table_find (domain, (char*)new_ctx->eip, NULL);
849 /* LLVM doesn't push the arguments */
850 if (caller_ji && !caller_ji->from_llvm)
851 new_ctx->esp += stack_size;
853 new_ctx->esp += stack_size;
862 if (((guint64)(*lmf)->previous_lmf) & 2) {
864 * This LMF entry is created by the soft debug code to mark transitions to
865 * managed code done during invokes.
867 MonoLMFExt *ext = (MonoLMFExt*)(*lmf);
869 g_assert (ext->debugger_invoke);
871 memcpy (new_ctx, &ext->ctx, sizeof (MonoContext));
873 *lmf = (gpointer)(((gsize)(*lmf)->previous_lmf) & ~3);
875 frame->type = FRAME_TYPE_DEBUGGER_INVOKE;
880 if ((ji = mini_jit_info_table_find (domain, (gpointer)(*lmf)->eip, NULL))) {
882 if (!((guint32)((*lmf)->previous_lmf) & 1))
885 g_assert_not_reached ();
886 /* Trampoline lmf frame */
887 frame->method = (*lmf)->method;
890 new_ctx->esi = (*lmf)->esi;
891 new_ctx->edi = (*lmf)->edi;
892 new_ctx->ebx = (*lmf)->ebx;
893 new_ctx->ebp = (*lmf)->ebp;
894 new_ctx->eip = (*lmf)->eip;
900 frame->type = FRAME_TYPE_MANAGED_TO_NATIVE;
902 /* Check if we are in a trampoline LMF frame */
903 if ((guint32)((*lmf)->previous_lmf) & 1) {
904 /* lmf->esp is set by the trampoline code */
905 new_ctx->esp = (*lmf)->esp;
907 /* Pop arguments off the stack */
908 /* FIXME: Handle the delegate case too ((*lmf)->method == NULL) */
909 /* FIXME: Handle the IMT/vtable case too */
912 if ((*lmf)->method) {
913 MonoMethod *method = (*lmf)->method;
914 MonoJitArgumentInfo *arg_info = g_newa (MonoJitArgumentInfo, mono_method_signature (method)->param_count + 1);
916 guint32 stack_to_pop = mono_arch_get_argument_info (NULL, mono_method_signature (method), mono_method_signature (method)->param_count, arg_info);
917 new_ctx->esp += stack_to_pop;
923 /* the lmf is always stored on the stack, so the following
924 * expression points to a stack location which can be used as ESP */
925 new_ctx->esp = (unsigned long)&((*lmf)->eip);
927 *lmf = (gpointer)(((gsize)(*lmf)->previous_lmf) & ~3);
936 mono_arch_sigctx_to_monoctx (void *sigctx, MonoContext *mctx)
938 mono_sigctx_to_monoctx (sigctx, mctx);
942 mono_arch_monoctx_to_sigctx (MonoContext *mctx, void *sigctx)
944 mono_monoctx_to_sigctx (mctx, sigctx);
948 mono_arch_ip_from_context (void *sigctx)
950 #if defined(__native_client__)
951 printf("WARNING: mono_arch_ip_from_context() called!\n");
954 #ifdef MONO_ARCH_USE_SIGACTION
955 ucontext_t *ctx = (ucontext_t*)sigctx;
956 return (gpointer)UCONTEXT_REG_EIP (ctx);
958 struct sigcontext *ctx = sigctx;
959 return (gpointer)ctx->SC_EIP;
961 #endif /* __native_client__ */
967 * Called by resuming from a signal handler.
970 handle_signal_exception (gpointer obj)
972 MonoJitTlsData *jit_tls = mono_native_tls_get_value (mono_jit_tls_id);
975 memcpy (&ctx, &jit_tls->ex_ctx, sizeof (MonoContext));
977 mono_handle_exception (&ctx, obj);
979 mono_restore_context (&ctx);
983 * mono_x86_get_signal_exception_trampoline:
985 * This x86 specific trampoline is used to call handle_signal_exception.
988 mono_x86_get_signal_exception_trampoline (MonoTrampInfo **info, gboolean aot)
990 guint8 *start, *code;
991 MonoJumpInfo *ji = NULL;
992 GSList *unwind_ops = NULL;
995 start = code = mono_global_codeman_reserve (128);
998 x86_push_reg (code, X86_ECX);
1000 mono_add_unwind_op_def_cfa (unwind_ops, (guint8*)NULL, (guint8*)NULL, X86_ESP, 4);
1001 mono_add_unwind_op_offset (unwind_ops, (guint8*)NULL, (guint8*)NULL, X86_NREG, -4);
1003 /* Fix the alignment to be what apple expects */
1006 x86_alu_reg_imm (code, X86_SUB, X86_ESP, stack_size);
1007 mono_add_unwind_op_def_cfa_offset (unwind_ops, code, start, stack_size + 4);
1010 x86_mov_membase_reg (code, X86_ESP, 0, X86_EAX, 4);
1011 /* Branch to target */
1012 x86_call_reg (code, X86_EDX);
1014 g_assert ((code - start) < 128);
1017 *info = mono_tramp_info_create ("x86_signal_exception_trampoline", start, code - start, ji, unwind_ops);
1021 for (l = unwind_ops; l; l = l->next)
1023 g_slist_free (unwind_ops);
1031 mono_arch_setup_async_callback (MonoContext *ctx, void (*async_cb)(void *fun), gpointer user_data)
1034 * Can't pass the obj on the stack, since we are executing on the
1035 * same stack. Can't save it into MonoJitTlsData, since it needs GC tracking.
1036 * So put it into a register, and branch to a trampoline which
1039 ctx->eax = (mgreg_t)user_data;
1040 ctx->ecx = ctx->eip;
1041 ctx->edx = (mgreg_t)async_cb;
1044 ctx->esp = (ctx->esp - 16) & ~15;
1045 ctx->eip = (mgreg_t)signal_exception_trampoline;
1049 mono_arch_handle_exception (void *sigctx, gpointer obj)
1051 #if defined(MONO_ARCH_USE_SIGACTION)
1053 ucontext_t *ctx = (ucontext_t*)sigctx;
1056 * Handling the exception in the signal handler is problematic, since the original
1057 * signal is disabled, and we could run arbitrary code though the debugger. So
1058 * resume into the normal stack and do most work there if possible.
1060 MonoJitTlsData *jit_tls = mono_native_tls_get_value (mono_jit_tls_id);
1062 /* Pass the ctx parameter in TLS */
1063 mono_arch_sigctx_to_monoctx (ctx, &jit_tls->ex_ctx);
1065 mctx = jit_tls->ex_ctx;
1066 mono_setup_async_callback (&mctx, handle_signal_exception, obj);
1067 mono_monoctx_to_sigctx (&mctx, sigctx);
1070 #elif defined (TARGET_WIN32)
1072 MonoJitTlsData *jit_tls = mono_native_tls_get_value (mono_jit_tls_id);
1073 struct sigcontext *ctx = (struct sigcontext *)sigctx;
1075 mono_arch_sigctx_to_monoctx (sigctx, &jit_tls->ex_ctx);
1077 mctx = jit_tls->ex_ctx;
1078 mono_setup_async_callback (&mctx, handle_signal_exception, obj);
1079 mono_monoctx_to_sigctx (&mctx, sigctx);
1085 mono_arch_sigctx_to_monoctx (sigctx, &mctx);
1087 mono_handle_exception (&mctx, obj);
1089 mono_arch_monoctx_to_sigctx (&mctx, sigctx);
1096 restore_soft_guard_pages (void)
1098 MonoJitTlsData *jit_tls = mono_native_tls_get_value (mono_jit_tls_id);
1099 if (jit_tls->stack_ovf_guard_base)
1100 mono_mprotect (jit_tls->stack_ovf_guard_base, jit_tls->stack_ovf_guard_size, MONO_MMAP_NONE);
1104 * this function modifies mctx so that when it is restored, it
1105 * won't execcute starting at mctx.eip, but in a function that
1106 * will restore the protection on the soft-guard pages and return back to
1107 * continue at mctx.eip.
1110 prepare_for_guard_pages (MonoContext *mctx)
1113 sp = (gpointer)(mctx->esp);
1115 /* the resturn addr */
1116 sp [0] = (gpointer)(mctx->eip);
1117 mctx->eip = (unsigned long)restore_soft_guard_pages;
1118 mctx->esp = (unsigned long)sp;
1122 altstack_handle_and_restore (MonoContext *ctx, gpointer obj, gboolean stack_ovf)
1128 mono_handle_exception (&mctx, obj);
1130 prepare_for_guard_pages (&mctx);
1131 mono_restore_context (&mctx);
1135 mono_arch_handle_altstack_exception (void *sigctx, gpointer fault_addr, gboolean stack_ovf)
1137 #ifdef MONO_ARCH_USE_SIGACTION
1138 MonoException *exc = NULL;
1139 ucontext_t *ctx = (ucontext_t*)sigctx;
1140 MonoJitInfo *ji = mini_jit_info_table_find (mono_domain_get (), (gpointer)UCONTEXT_REG_EIP (ctx), NULL);
1144 /* if we didn't find a managed method for the ip address and it matches the fault
1145 * address, we assume we followed a broken pointer during an indirect call, so
1146 * we try the lookup again with the return address pushed on the stack
1148 if (!ji && fault_addr == (gpointer)UCONTEXT_REG_EIP (ctx)) {
1149 glong *sp = (gpointer)UCONTEXT_REG_ESP (ctx);
1150 ji = mini_jit_info_table_find (mono_domain_get (), (gpointer)sp [0], NULL);
1152 UCONTEXT_REG_EIP (ctx) = sp [0];
1155 exc = mono_domain_get ()->stack_overflow_ex;
1157 mono_handle_native_sigsegv (SIGSEGV, sigctx);
1158 /* setup a call frame on the real stack so that control is returned there
1159 * and exception handling can continue.
1160 * If this was a stack overflow the caller already ensured the stack pages
1161 * needed have been unprotected.
1162 * The frame looks like:
1169 // FIXME: test_only is no more.
1170 frame_size = sizeof (MonoContext) + sizeof (gpointer) * 4;
1173 sp = (gpointer)(UCONTEXT_REG_ESP (ctx) & ~15);
1174 sp = (gpointer)((char*)sp - frame_size);
1175 /* the incoming arguments are aligned to 16 bytes boundaries, so the return address IP
1178 sp [-1] = (gpointer)UCONTEXT_REG_EIP (ctx);
1181 sp [2] = (gpointer)stack_ovf;
1182 mono_sigctx_to_monoctx (sigctx, (MonoContext*)(sp + 4));
1183 /* at the return form the signal handler execution starts in altstack_handle_and_restore() */
1184 UCONTEXT_REG_EIP (ctx) = (unsigned long)altstack_handle_and_restore;
1185 UCONTEXT_REG_ESP (ctx) = (unsigned long)(sp - 1);
1189 #if MONO_SUPPORT_TASKLETS
1190 MonoContinuationRestore
1191 mono_tasklets_arch_restore (void)
1193 static guint8* saved = NULL;
1194 guint8 *code, *start;
1196 #ifdef __native_client_codegen__
1197 g_print("mono_tasklets_arch_restore needs to be aligned for Native Client\n");
1200 return (MonoContinuationRestore)saved;
1201 code = start = mono_global_codeman_reserve (48);
1202 /* the signature is: restore (MonoContinuation *cont, int state, MonoLMF **lmf_addr) */
1203 /* put cont in edx */
1204 x86_mov_reg_membase (code, X86_EDX, X86_ESP, 4, 4);
1205 /* state in eax, so it's setup as the return value */
1206 x86_mov_reg_membase (code, X86_EAX, X86_ESP, 8, 4);
1208 /* setup the copy of the stack */
1209 x86_mov_reg_membase (code, X86_ECX, X86_EDX, G_STRUCT_OFFSET (MonoContinuation, stack_used_size), 4);
1210 x86_shift_reg_imm (code, X86_SHR, X86_ECX, 2);
1212 x86_mov_reg_membase (code, X86_ESI, X86_EDX, G_STRUCT_OFFSET (MonoContinuation, saved_stack), 4);
1213 x86_mov_reg_membase (code, X86_EDI, X86_EDX, G_STRUCT_OFFSET (MonoContinuation, return_sp), 4);
1214 x86_prefix (code, X86_REP_PREFIX);
1217 /* now restore the registers from the LMF */
1218 x86_mov_reg_membase (code, X86_ECX, X86_EDX, G_STRUCT_OFFSET (MonoContinuation, lmf), 4);
1219 x86_mov_reg_membase (code, X86_EBX, X86_ECX, G_STRUCT_OFFSET (MonoLMF, ebx), 4);
1220 x86_mov_reg_membase (code, X86_EBP, X86_ECX, G_STRUCT_OFFSET (MonoLMF, ebp), 4);
1221 x86_mov_reg_membase (code, X86_ESI, X86_ECX, G_STRUCT_OFFSET (MonoLMF, esi), 4);
1222 x86_mov_reg_membase (code, X86_EDI, X86_ECX, G_STRUCT_OFFSET (MonoLMF, edi), 4);
1224 /* restore the lmf chain */
1225 /*x86_mov_reg_membase (code, X86_ECX, X86_ESP, 12, 4);
1226 x86_mov_membase_reg (code, X86_ECX, 0, X86_EDX, 4);*/
1228 x86_jump_membase (code, X86_EDX, G_STRUCT_OFFSET (MonoContinuation, return_ip));
1229 g_assert ((code - start) <= 48);
1231 return (MonoContinuationRestore)saved;
1236 * mono_arch_setup_resume_sighandler_ctx:
1238 * Setup CTX so execution continues at FUNC.
1241 mono_arch_setup_resume_sighandler_ctx (MonoContext *ctx, gpointer func)
1243 int align = (((gint32)MONO_CONTEXT_GET_SP (ctx)) % MONO_ARCH_FRAME_ALIGNMENT + 4);
1246 MONO_CONTEXT_SET_SP (ctx, (gsize)MONO_CONTEXT_GET_SP (ctx) - align);
1248 MONO_CONTEXT_SET_IP (ctx, func);