2 * exceptions-x86.c: exception support for x86
5 * Dietmar Maurer (dietmar@ximian.com)
7 * (C) 2001 Ximian, Inc.
15 #ifdef HAVE_UCONTEXT_H
19 #include <mono/metadata/abi-details.h>
20 #include <mono/arch/x86/x86-codegen.h>
21 #include <mono/metadata/appdomain.h>
22 #include <mono/metadata/tabledefs.h>
23 #include <mono/metadata/threads.h>
24 #include <mono/metadata/debug-helpers.h>
25 #include <mono/metadata/exception.h>
26 #include <mono/metadata/gc-internal.h>
27 #include <mono/metadata/mono-debug.h>
28 #include <mono/utils/mono-mmap.h>
34 static gpointer signal_exception_trampoline;
37 mono_x86_get_signal_exception_trampoline (MonoTrampInfo **info, gboolean aot);
40 static void (*restore_stack) (void *);
42 static MonoW32ExceptionHandler fpe_handler;
43 static MonoW32ExceptionHandler ill_handler;
44 static MonoW32ExceptionHandler segv_handler;
46 LPTOP_LEVEL_EXCEPTION_FILTER mono_old_win_toplevel_exception_filter;
47 gpointer mono_win_vectored_exception_handle;
48 extern int (*gUnhandledExceptionHandler)(EXCEPTION_POINTERS*);
50 #ifndef PROCESS_CALLBACK_FILTER_ENABLED
51 # define PROCESS_CALLBACK_FILTER_ENABLED 1
54 #define W32_SEH_HANDLE_EX(_ex) \
55 if (_ex##_handler) _ex##_handler(0, ep, ctx)
57 LONG CALLBACK seh_unhandled_exception_filter(EXCEPTION_POINTERS* ep)
59 #ifndef MONO_CROSS_COMPILE
60 if (mono_old_win_toplevel_exception_filter) {
61 return (*mono_old_win_toplevel_exception_filter)(ep);
65 mono_handle_native_sigsegv (SIGSEGV, NULL, NULL);
67 return EXCEPTION_CONTINUE_SEARCH;
71 * mono_win32_get_handle_stackoverflow (void):
73 * Returns a pointer to a method which restores the current context stack
74 * and calls handle_exceptions, when done restores the original stack.
77 mono_win32_get_handle_stackoverflow (void)
79 static guint8 *start = NULL;
85 /* restore_contect (void *sigctx) */
86 start = code = mono_global_codeman_reserve (128);
88 /* load context into ebx */
89 x86_mov_reg_membase (code, X86_EBX, X86_ESP, 4, 4);
91 /* move current stack into edi for later restore */
92 x86_mov_reg_reg (code, X86_EDI, X86_ESP, 4);
94 /* use the new freed stack from sigcontext */
95 /* XXX replace usage of struct sigcontext with MonoContext so we can use MONO_STRUCT_OFFSET */
96 x86_mov_reg_membase (code, X86_ESP, X86_EBX, G_STRUCT_OFFSET (struct sigcontext, esp), 4);
98 /* get the current domain */
99 x86_call_code (code, mono_domain_get);
101 /* get stack overflow exception from domain object */
102 x86_mov_reg_membase (code, X86_EAX, X86_EAX, G_STRUCT_OFFSET (MonoDomain, stack_overflow_ex), 4);
104 /* call mono_arch_handle_exception (sctx, stack_overflow_exception_obj) */
105 x86_push_reg (code, X86_EAX);
106 x86_push_reg (code, X86_EBX);
107 x86_call_code (code, mono_arch_handle_exception);
109 /* restore the SEH handler stack */
110 x86_mov_reg_reg (code, X86_ESP, X86_EDI, 4);
115 mono_arch_flush_icache (start, code - start);
116 mono_profiler_code_buffer_new (start, code - start, MONO_PROFILER_CODE_BUFFER_EXCEPTION_HANDLING, NULL);
121 /* Special hack to workaround the fact that the
122 * when the SEH handler is called the stack is
123 * to small to recover.
125 * Stack walking part of this method is from mono_handle_exception
127 * The idea is simple;
128 * - walk the stack to free some space (64k)
129 * - set esp to new stack location
130 * - call mono_arch_handle_exception with stack overflow exception
131 * - set esp to SEH handlers stack
135 win32_handle_stack_overflow (EXCEPTION_POINTERS* ep, struct sigcontext *sctx)
139 MonoDomain *domain = mono_domain_get ();
141 MonoJitTlsData *jit_tls = mono_native_tls_get_value (mono_jit_tls_id);
142 MonoLMF *lmf = jit_tls->lmf;
143 MonoContext initial_ctx;
145 guint32 free_stack = 0;
146 StackFrameInfo frame;
148 mono_sigctx_to_monoctx (sctx, &ctx);
150 /* get our os page size */
152 page_size = si.dwPageSize;
154 /* Let's walk the stack to recover
155 * the needed stack space (if possible)
157 memset (&rji, 0, sizeof (rji));
160 free_stack = (guint8*)(MONO_CONTEXT_GET_BP (&ctx)) - (guint8*)(MONO_CONTEXT_GET_BP (&initial_ctx));
162 /* try to free 64kb from our stack */
166 mono_arch_unwind_frame (domain, jit_tls, &rji, &ctx, &new_ctx, &lmf, NULL, &frame);
168 g_warning ("Exception inside function without unwind info");
169 g_assert_not_reached ();
172 if (frame.ji != (gpointer)-1) {
173 free_stack = (guint8*)(MONO_CONTEXT_GET_BP (&ctx)) - (guint8*)(MONO_CONTEXT_GET_BP (&initial_ctx));
176 /* todo: we should call abort if ji is -1 */
178 } while (free_stack < 64 * 1024 && frame.ji != (gpointer) -1);
180 mono_monoctx_to_sigctx (&ctx, sctx);
182 /* todo: install new stack-guard page */
184 /* use the new stack and call mono_arch_handle_exception () */
185 restore_stack (sctx);
189 * Unhandled Exception Filter
190 * Top-level per-process exception handler.
192 LONG CALLBACK seh_vectored_exception_handler(EXCEPTION_POINTERS* ep)
194 EXCEPTION_RECORD* er;
197 MonoJitTlsData *jit_tls = mono_native_tls_get_value (mono_jit_tls_id);
199 /* If the thread is not managed by the runtime return early */
201 return EXCEPTION_CONTINUE_SEARCH;
203 jit_tls->mono_win_chained_exception_needs_run = FALSE;
204 res = EXCEPTION_CONTINUE_EXECUTION;
206 er = ep->ExceptionRecord;
207 ctx = ep->ContextRecord;
209 switch (er->ExceptionCode) {
210 case EXCEPTION_STACK_OVERFLOW:
211 win32_handle_stack_overflow (ep, ctx);
213 case EXCEPTION_ACCESS_VIOLATION:
214 W32_SEH_HANDLE_EX(segv);
216 case EXCEPTION_ILLEGAL_INSTRUCTION:
217 W32_SEH_HANDLE_EX(ill);
219 case EXCEPTION_INT_DIVIDE_BY_ZERO:
220 case EXCEPTION_INT_OVERFLOW:
221 case EXCEPTION_FLT_DIVIDE_BY_ZERO:
222 case EXCEPTION_FLT_OVERFLOW:
223 case EXCEPTION_FLT_UNDERFLOW:
224 case EXCEPTION_FLT_INEXACT_RESULT:
225 W32_SEH_HANDLE_EX(fpe);
228 jit_tls->mono_win_chained_exception_needs_run = TRUE;
232 if (jit_tls->mono_win_chained_exception_needs_run) {
233 /* Don't copy context back if we chained exception
234 * as the handler may have modfied the EXCEPTION_POINTERS
235 * directly. We don't pass sigcontext to chained handlers.
236 * Return continue search so the UnhandledExceptionFilter
237 * can correctly chain the exception.
239 res = EXCEPTION_CONTINUE_SEARCH;
245 void win32_seh_init()
247 /* install restore stack helper */
249 restore_stack = mono_win32_get_handle_stackoverflow ();
251 mono_old_win_toplevel_exception_filter = SetUnhandledExceptionFilter(seh_unhandled_exception_filter);
252 mono_win_vectored_exception_handle = AddVectoredExceptionHandler (1, seh_vectored_exception_handler);
255 void win32_seh_cleanup()
257 if (mono_old_win_toplevel_exception_filter)
258 SetUnhandledExceptionFilter(mono_old_win_toplevel_exception_filter);
259 RemoveVectoredExceptionHandler (mono_win_vectored_exception_handle);
262 void win32_seh_set_handler(int type, MonoW32ExceptionHandler handler)
266 fpe_handler = handler;
269 ill_handler = handler;
272 segv_handler = handler;
279 #endif /* TARGET_WIN32 */
282 * mono_arch_get_restore_context:
284 * Returns a pointer to a method which restores a previously saved sigcontext.
287 mono_arch_get_restore_context (MonoTrampInfo **info, gboolean aot)
289 guint8 *start = NULL;
291 MonoJumpInfo *ji = NULL;
292 GSList *unwind_ops = NULL;
294 /* restore_contect (MonoContext *ctx) */
296 start = code = mono_global_codeman_reserve (128);
299 x86_mov_reg_membase (code, X86_EAX, X86_ESP, 4, 4);
302 x86_mov_reg_membase (code, X86_EBX, X86_EAX, MONO_STRUCT_OFFSET (MonoContext, ebx), 4);
305 x86_mov_reg_membase (code, X86_EDI, X86_EAX, MONO_STRUCT_OFFSET (MonoContext, edi), 4);
308 x86_mov_reg_membase (code, X86_ESI, X86_EAX, MONO_STRUCT_OFFSET (MonoContext, esi), 4);
311 x86_mov_reg_membase (code, X86_EDX, X86_EAX, MONO_STRUCT_OFFSET (MonoContext, edx), 4);
314 * The context resides on the stack, in the stack frame of the
315 * caller of this function. The stack pointer that we need to
316 * restore is potentially many stack frames higher up, so the
317 * distance between them can easily be more than the red zone
318 * size. Hence the stack pointer can be restored only after
319 * we have finished loading everything from the context.
322 /* load ESP into EBP */
323 x86_mov_reg_membase (code, X86_EBP, X86_EAX, MONO_STRUCT_OFFSET (MonoContext, esp), 4);
324 /* load return address into ECX */
325 x86_mov_reg_membase (code, X86_ECX, X86_EAX, MONO_STRUCT_OFFSET (MonoContext, eip), 4);
326 /* save the return addr to the restored stack - 4 */
327 x86_mov_membase_reg (code, X86_EBP, -4, X86_ECX, 4);
329 /* load EBP into ECX */
330 x86_mov_reg_membase (code, X86_ECX, X86_EAX, MONO_STRUCT_OFFSET (MonoContext, ebp), 4);
331 /* save EBP to the restored stack - 8 */
332 x86_mov_membase_reg (code, X86_EBP, -8, X86_ECX, 4);
334 /* load EAX into ECX */
335 x86_mov_reg_membase (code, X86_ECX, X86_EAX, MONO_STRUCT_OFFSET (MonoContext, eax), 4);
336 /* save EAX to the restored stack - 12 */
337 x86_mov_membase_reg (code, X86_EBP, -12, X86_ECX, 4);
340 x86_mov_reg_membase (code, X86_ECX, X86_EAX, MONO_STRUCT_OFFSET (MonoContext, ecx), 4);
342 /* restore ESP - 12 */
343 x86_lea_membase (code, X86_ESP, X86_EBP, -12);
345 x86_pop_reg (code, X86_EAX);
347 x86_pop_reg (code, X86_EBP);
348 /* jump to the saved IP */
351 nacl_global_codeman_validate(&start, 128, &code);
354 *info = mono_tramp_info_create ("restore_context", start, code - start, ji, unwind_ops);
358 for (l = unwind_ops; l; l = l->next)
360 g_slist_free (unwind_ops);
363 mono_arch_flush_icache (start, code - start);
364 mono_profiler_code_buffer_new (start, code - start, MONO_PROFILER_CODE_BUFFER_EXCEPTION_HANDLING, NULL);
370 * mono_arch_get_call_filter:
372 * Returns a pointer to a method which calls an exception filter. We
373 * also use this function to call finally handlers (we pass NULL as
374 * @exc object in this case).
377 mono_arch_get_call_filter (MonoTrampInfo **info, gboolean aot)
381 MonoJumpInfo *ji = NULL;
382 GSList *unwind_ops = NULL;
383 guint kMaxCodeSize = NACL_SIZE (64, 128);
385 /* call_filter (MonoContext *ctx, unsigned long eip) */
386 start = code = mono_global_codeman_reserve (kMaxCodeSize);
388 x86_push_reg (code, X86_EBP);
389 x86_mov_reg_reg (code, X86_EBP, X86_ESP, 4);
390 x86_push_reg (code, X86_EBX);
391 x86_push_reg (code, X86_EDI);
392 x86_push_reg (code, X86_ESI);
395 x86_mov_reg_membase (code, X86_EAX, X86_EBP, 8, 4);
397 x86_mov_reg_membase (code, X86_ECX, X86_EBP, 12, 4);
399 x86_push_reg (code, X86_EBP);
402 x86_mov_reg_membase (code, X86_EBP, X86_EAX, MONO_STRUCT_OFFSET (MonoContext, ebp), 4);
403 /* restore registers used by global register allocation (EBX & ESI) */
404 x86_mov_reg_membase (code, X86_EBX, X86_EAX, MONO_STRUCT_OFFSET (MonoContext, ebx), 4);
405 x86_mov_reg_membase (code, X86_ESI, X86_EAX, MONO_STRUCT_OFFSET (MonoContext, esi), 4);
406 x86_mov_reg_membase (code, X86_EDI, X86_EAX, MONO_STRUCT_OFFSET (MonoContext, edi), 4);
408 /* align stack and save ESP */
409 x86_mov_reg_reg (code, X86_EDX, X86_ESP, 4);
410 x86_alu_reg_imm (code, X86_AND, X86_ESP, -MONO_ARCH_FRAME_ALIGNMENT);
411 g_assert (MONO_ARCH_FRAME_ALIGNMENT >= 8);
412 x86_alu_reg_imm (code, X86_SUB, X86_ESP, MONO_ARCH_FRAME_ALIGNMENT - 8);
413 x86_push_reg (code, X86_EDX);
415 /* call the handler */
416 x86_call_reg (code, X86_ECX);
419 x86_pop_reg (code, X86_ESP);
422 x86_pop_reg (code, X86_EBP);
424 /* restore saved regs */
425 x86_pop_reg (code, X86_ESI);
426 x86_pop_reg (code, X86_EDI);
427 x86_pop_reg (code, X86_EBX);
431 nacl_global_codeman_validate(&start, kMaxCodeSize, &code);
434 *info = mono_tramp_info_create ("call_filter", start, code - start, ji, unwind_ops);
438 for (l = unwind_ops; l; l = l->next)
440 g_slist_free (unwind_ops);
443 mono_arch_flush_icache (start, code - start);
444 mono_profiler_code_buffer_new (start, code - start, MONO_PROFILER_CODE_BUFFER_EXCEPTION_HANDLING, NULL);
446 g_assert ((code - start) < kMaxCodeSize);
451 * mono_x86_throw_exception:
453 * C function called from the throw trampolines.
456 mono_x86_throw_exception (mgreg_t *regs, MonoObject *exc,
457 mgreg_t eip, gboolean rethrow)
461 ctx.esp = regs [X86_ESP];
463 ctx.ebp = regs [X86_EBP];
464 ctx.edi = regs [X86_EDI];
465 ctx.esi = regs [X86_ESI];
466 ctx.ebx = regs [X86_EBX];
467 ctx.edx = regs [X86_EDX];
468 ctx.ecx = regs [X86_ECX];
469 ctx.eax = regs [X86_EAX];
472 /* The OSX ABI specifies 16 byte alignment at call sites */
473 g_assert ((ctx.esp % MONO_ARCH_FRAME_ALIGNMENT) == 0);
476 if (mono_object_isinst (exc, mono_defaults.exception_class)) {
477 MonoException *mono_ex = (MonoException*)exc;
479 mono_ex->stack_trace = NULL;
480 mono_ex->trace_ips = NULL;
484 /* adjust eip so that it point into the call instruction */
487 mono_handle_exception (&ctx, exc);
489 mono_restore_context (&ctx);
491 g_assert_not_reached ();
495 mono_x86_throw_corlib_exception (mgreg_t *regs, guint32 ex_token_index,
496 mgreg_t eip, gint32 pc_offset)
498 guint32 ex_token = MONO_TOKEN_TYPE_DEF | ex_token_index;
501 ex = mono_exception_from_token (mono_defaults.exception_class->image, ex_token);
505 /* Negate the ip adjustment done in mono_x86_throw_exception () */
508 mono_x86_throw_exception (regs, (MonoObject*)ex, eip, FALSE);
512 mono_x86_resume_unwind (mgreg_t *regs, MonoObject *exc,
513 mgreg_t eip, gboolean rethrow)
517 ctx.esp = regs [X86_ESP];
519 ctx.ebp = regs [X86_EBP];
520 ctx.edi = regs [X86_EDI];
521 ctx.esi = regs [X86_ESI];
522 ctx.ebx = regs [X86_EBX];
523 ctx.edx = regs [X86_EDX];
524 ctx.ecx = regs [X86_ECX];
525 ctx.eax = regs [X86_EAX];
527 mono_resume_unwind (&ctx);
531 * get_throw_trampoline:
533 * Generate a call to mono_x86_throw_exception/
534 * mono_x86_throw_corlib_exception.
535 * If LLVM is true, generate code which assumes the caller is LLVM generated code,
536 * which doesn't push the arguments.
539 get_throw_trampoline (const char *name, gboolean rethrow, gboolean llvm, gboolean corlib, gboolean llvm_abs, gboolean resume_unwind, MonoTrampInfo **info, gboolean aot)
541 guint8 *start, *code;
542 int i, stack_size, stack_offset, arg_offsets [5], regs_offset;
543 MonoJumpInfo *ji = NULL;
544 GSList *unwind_ops = NULL;
545 guint kMaxCodeSize = NACL_SIZE (128, 256);
547 start = code = mono_global_codeman_reserve (kMaxCodeSize);
552 * On apple, the stack is misaligned by the pushing of the return address.
555 /* On OSX, we don't generate alignment code to save space */
558 stack_size += MONO_ARCH_FRAME_ALIGNMENT - 4;
561 * The stack looks like this:
562 * <pc offset> (only if corlib is TRUE)
563 * <exception object>/<type token>
564 * <return addr> <- esp (unaligned on apple)
567 mono_add_unwind_op_def_cfa (unwind_ops, (guint8*)NULL, (guint8*)NULL, X86_ESP, 4);
568 mono_add_unwind_op_offset (unwind_ops, (guint8*)NULL, (guint8*)NULL, X86_NREG, -4);
571 x86_alu_reg_imm (code, X86_SUB, X86_ESP, stack_size);
572 mono_add_unwind_op_def_cfa_offset (unwind_ops, code, start, stack_size + 4);
577 arg_offsets [3] = 12;
581 for (i = 0; i < X86_NREG; ++i)
583 x86_mov_membase_reg (code, X86_ESP, regs_offset + (i * 4), i, 4);
584 /* Calculate the offset between the current sp and the sp of the caller */
586 /* LLVM doesn't push the arguments */
587 stack_offset = stack_size + 4;
591 stack_offset = stack_size + 4 + 8;
593 /* We don't generate stack alignment code on osx to save space */
596 /* One argument + stack alignment */
597 stack_offset = stack_size + 4 + 4;
599 /* Pop the alignment added by OP_THROW too */
600 stack_offset += MONO_ARCH_FRAME_ALIGNMENT - 4;
602 if (mono_do_x86_stack_align)
603 stack_offset += MONO_ARCH_FRAME_ALIGNMENT - 4;
608 x86_lea_membase (code, X86_EAX, X86_ESP, stack_offset);
609 x86_mov_membase_reg (code, X86_ESP, regs_offset + (X86_ESP * 4), X86_EAX, 4);
611 /* Set arg1 == regs */
612 x86_lea_membase (code, X86_EAX, X86_ESP, regs_offset);
613 x86_mov_membase_reg (code, X86_ESP, arg_offsets [0], X86_EAX, 4);
614 /* Set arg2 == exc/ex_token_index */
616 x86_mov_reg_imm (code, X86_EAX, 0);
618 x86_mov_reg_membase (code, X86_EAX, X86_ESP, stack_size + 4, 4);
619 x86_mov_membase_reg (code, X86_ESP, arg_offsets [1], X86_EAX, 4);
620 /* Set arg3 == eip */
622 x86_alu_reg_reg (code, X86_XOR, X86_EAX, X86_EAX);
624 x86_mov_reg_membase (code, X86_EAX, X86_ESP, stack_size, 4);
625 x86_mov_membase_reg (code, X86_ESP, arg_offsets [2], X86_EAX, 4);
626 /* Set arg4 == rethrow/pc_offset */
628 x86_mov_membase_imm (code, X86_ESP, arg_offsets [3], 0, 4);
630 x86_mov_reg_membase (code, X86_EAX, X86_ESP, stack_size + 8, 4);
633 * The caller is LLVM code which passes the absolute address not a pc offset,
634 * so compensate by passing 0 as 'ip' and passing the negated abs address as
637 x86_neg_reg (code, X86_EAX);
639 x86_mov_membase_reg (code, X86_ESP, arg_offsets [3], X86_EAX, 4);
641 x86_mov_membase_imm (code, X86_ESP, arg_offsets [3], rethrow, 4);
645 // This can be called from runtime code, which can't guarantee that
646 // ebx contains the got address.
647 // So emit the got address loading code too
648 code = mono_arch_emit_load_got_addr (start, code, NULL, &ji);
649 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");
650 x86_call_reg (code, X86_EAX);
652 x86_call_code (code, resume_unwind ? (gpointer)(mono_x86_resume_unwind) : (corlib ? (gpointer)mono_x86_throw_corlib_exception : (gpointer)mono_x86_throw_exception));
654 x86_breakpoint (code);
656 nacl_global_codeman_validate(&start, kMaxCodeSize, &code);
658 g_assert ((code - start) < kMaxCodeSize);
661 *info = mono_tramp_info_create (name, start, code - start, ji, unwind_ops);
665 for (l = unwind_ops; l; l = l->next)
667 g_slist_free (unwind_ops);
670 mono_arch_flush_icache (start, code - start);
671 mono_profiler_code_buffer_new (start, code - start, MONO_PROFILER_CODE_BUFFER_EXCEPTION_HANDLING, NULL);
677 * mono_arch_get_throw_exception:
679 * Returns a function pointer which can be used to raise
680 * exceptions. The returned function has the following
681 * signature: void (*func) (MonoException *exc);
682 * For example to raise an arithmetic exception you can use:
684 * x86_push_imm (code, mono_get_exception_arithmetic ());
685 * x86_call_code (code, arch_get_throw_exception ());
689 mono_arch_get_throw_exception (MonoTrampInfo **info, gboolean aot)
691 return get_throw_trampoline ("throw_exception", FALSE, FALSE, FALSE, FALSE, FALSE, info, aot);
695 mono_arch_get_rethrow_exception (MonoTrampInfo **info, gboolean aot)
697 return get_throw_trampoline ("rethrow_exception", TRUE, FALSE, FALSE, FALSE, FALSE, info, aot);
701 * mono_arch_get_throw_corlib_exception:
703 * Returns a function pointer which can be used to raise
704 * corlib exceptions. The returned function has the following
705 * signature: void (*func) (guint32 ex_token, guint32 offset);
706 * Here, offset is the offset which needs to be substracted from the caller IP
707 * to get the IP of the throw. Passing the offset has the advantage that it
708 * needs no relocations in the caller.
711 mono_arch_get_throw_corlib_exception (MonoTrampInfo **info, gboolean aot)
713 return get_throw_trampoline ("throw_corlib_exception", FALSE, FALSE, TRUE, FALSE, FALSE, info, aot);
717 mono_arch_exceptions_init (void)
722 * If we're running WoW64, we need to set the usermode exception policy
723 * for SEHs to behave. This requires hotfix http://support.microsoft.com/kb/976038
724 * or (eventually) Windows 7 SP1.
730 HMODULE kernel32 = LoadLibraryW (L"kernel32.dll");
733 getter = GetProcAddress (kernel32, "GetProcessUserModeExceptionPolicy");
734 setter = GetProcAddress (kernel32, "SetProcessUserModeExceptionPolicy");
735 if (getter && setter) {
737 setter (flags & ~PROCESS_CALLBACK_FILTER_ENABLED);
743 signal_exception_trampoline = mono_aot_get_trampoline ("x86_signal_exception_trampoline");
747 /* LLVM needs different throw trampolines */
748 tramp = get_throw_trampoline ("llvm_throw_exception_trampoline", FALSE, TRUE, FALSE, FALSE, FALSE, NULL, FALSE);
749 mono_register_jit_icall (tramp, "llvm_throw_exception_trampoline", NULL, TRUE);
751 tramp = get_throw_trampoline ("llvm_rethrow_exception_trampoline", TRUE, TRUE, FALSE, FALSE, FALSE, NULL, FALSE);
752 mono_register_jit_icall (tramp, "llvm_rethrow_exception_trampoline", NULL, TRUE);
754 tramp = get_throw_trampoline ("llvm_throw_corlib_exception_trampoline", FALSE, TRUE, TRUE, FALSE, FALSE, NULL, FALSE);
755 mono_register_jit_icall (tramp, "llvm_throw_corlib_exception_trampoline", NULL, TRUE);
757 tramp = get_throw_trampoline ("llvm_throw_corlib_exception_abs_trampoline", FALSE, TRUE, TRUE, TRUE, FALSE, NULL, FALSE);
758 mono_register_jit_icall (tramp, "llvm_throw_corlib_exception_abs_trampoline", NULL, TRUE);
760 tramp = get_throw_trampoline ("llvm_resume_unwind_trampoline", FALSE, FALSE, FALSE, FALSE, TRUE, NULL, FALSE);
761 mono_register_jit_icall (tramp, "llvm_resume_unwind_trampoline", NULL, TRUE);
763 signal_exception_trampoline = mono_x86_get_signal_exception_trampoline (NULL, FALSE);
767 * mono_arch_unwind_frame:
769 * See exceptions-amd64.c for docs.
772 mono_arch_unwind_frame (MonoDomain *domain, MonoJitTlsData *jit_tls,
773 MonoJitInfo *ji, MonoContext *ctx,
774 MonoContext *new_ctx, MonoLMF **lmf,
775 mgreg_t **save_locations,
776 StackFrameInfo *frame)
778 gpointer ip = MONO_CONTEXT_GET_IP (ctx);
780 memset (frame, 0, sizeof (StackFrameInfo));
786 gssize regs [MONO_MAX_IREGS + 1];
788 guint32 unwind_info_len;
791 frame->type = FRAME_TYPE_MANAGED;
793 unwind_info = mono_jinfo_get_unwind_info (ji, &unwind_info_len);
795 regs [X86_EAX] = new_ctx->eax;
796 regs [X86_EBX] = new_ctx->ebx;
797 regs [X86_ECX] = new_ctx->ecx;
798 regs [X86_EDX] = new_ctx->edx;
799 regs [X86_ESP] = new_ctx->esp;
800 regs [X86_EBP] = new_ctx->ebp;
801 regs [X86_ESI] = new_ctx->esi;
802 regs [X86_EDI] = new_ctx->edi;
803 regs [X86_NREG] = new_ctx->eip;
805 mono_unwind_frame (unwind_info, unwind_info_len, ji->code_start,
806 (guint8*)ji->code_start + ji->code_size,
807 ip, NULL, regs, MONO_MAX_IREGS + 1,
808 save_locations, MONO_MAX_IREGS, &cfa);
810 new_ctx->eax = regs [X86_EAX];
811 new_ctx->ebx = regs [X86_EBX];
812 new_ctx->ecx = regs [X86_ECX];
813 new_ctx->edx = regs [X86_EDX];
814 new_ctx->esp = regs [X86_ESP];
815 new_ctx->ebp = regs [X86_EBP];
816 new_ctx->esi = regs [X86_ESI];
817 new_ctx->edi = regs [X86_EDI];
818 new_ctx->eip = regs [X86_NREG];
820 /* The CFA becomes the new SP value */
821 new_ctx->esp = (gssize)cfa;
829 if (((guint64)(*lmf)->previous_lmf) & 2) {
831 * This LMF entry is created by the soft debug code to mark transitions to
832 * managed code done during invokes.
834 MonoLMFExt *ext = (MonoLMFExt*)(*lmf);
836 g_assert (ext->debugger_invoke);
838 memcpy (new_ctx, &ext->ctx, sizeof (MonoContext));
840 *lmf = (gpointer)(((gsize)(*lmf)->previous_lmf) & ~3);
842 frame->type = FRAME_TYPE_DEBUGGER_INVOKE;
847 if ((ji = mini_jit_info_table_find (domain, (gpointer)(*lmf)->eip, NULL))) {
852 frame->method = (*lmf)->method;
855 new_ctx->esi = (*lmf)->esi;
856 new_ctx->edi = (*lmf)->edi;
857 new_ctx->ebx = (*lmf)->ebx;
858 new_ctx->ebp = (*lmf)->ebp;
859 new_ctx->eip = (*lmf)->eip;
864 frame->type = FRAME_TYPE_MANAGED_TO_NATIVE;
866 /* Check if we are in a trampoline LMF frame */
867 if ((guint32)((*lmf)->previous_lmf) & 1) {
868 /* lmf->esp is set by the trampoline code */
869 new_ctx->esp = (*lmf)->esp;
872 /* the lmf is always stored on the stack, so the following
873 * expression points to a stack location which can be used as ESP */
874 new_ctx->esp = (unsigned long)&((*lmf)->eip);
876 *lmf = (gpointer)(((gsize)(*lmf)->previous_lmf) & ~3);
885 mono_arch_ip_from_context (void *sigctx)
887 #if defined(__native_client__) || defined(HOST_WATCHOS)
888 printf("WARNING: mono_arch_ip_from_context() called!\n");
890 #elif defined(MONO_ARCH_USE_SIGACTION)
891 ucontext_t *ctx = (ucontext_t*)sigctx;
892 return (gpointer)UCONTEXT_REG_EIP (ctx);
893 #elif defined(HOST_WIN32)
894 return ((CONTEXT*)sigctx)->Eip;
896 struct sigcontext *ctx = sigctx;
897 return (gpointer)ctx->SC_EIP;
904 * Called by resuming from a signal handler.
907 handle_signal_exception (gpointer obj)
909 MonoJitTlsData *jit_tls = mono_native_tls_get_value (mono_jit_tls_id);
912 memcpy (&ctx, &jit_tls->ex_ctx, sizeof (MonoContext));
914 mono_handle_exception (&ctx, obj);
916 mono_restore_context (&ctx);
920 * mono_x86_get_signal_exception_trampoline:
922 * This x86 specific trampoline is used to call handle_signal_exception.
925 mono_x86_get_signal_exception_trampoline (MonoTrampInfo **info, gboolean aot)
927 guint8 *start, *code;
928 MonoJumpInfo *ji = NULL;
929 GSList *unwind_ops = NULL;
932 start = code = mono_global_codeman_reserve (128);
935 x86_push_reg (code, X86_ECX);
937 mono_add_unwind_op_def_cfa (unwind_ops, (guint8*)NULL, (guint8*)NULL, X86_ESP, 4);
938 mono_add_unwind_op_offset (unwind_ops, (guint8*)NULL, (guint8*)NULL, X86_NREG, -4);
940 /* Fix the alignment to be what apple expects */
943 x86_alu_reg_imm (code, X86_SUB, X86_ESP, stack_size);
944 mono_add_unwind_op_def_cfa_offset (unwind_ops, code, start, stack_size + 4);
947 x86_mov_membase_reg (code, X86_ESP, 0, X86_EAX, 4);
948 /* Branch to target */
949 x86_call_reg (code, X86_EDX);
951 g_assert ((code - start) < 128);
954 *info = mono_tramp_info_create ("x86_signal_exception_trampoline", start, code - start, ji, unwind_ops);
958 for (l = unwind_ops; l; l = l->next)
960 g_slist_free (unwind_ops);
963 mono_arch_flush_icache (start, code - start);
964 mono_profiler_code_buffer_new (start, code - start, MONO_PROFILER_CODE_BUFFER_EXCEPTION_HANDLING, NULL);
971 mono_arch_setup_async_callback (MonoContext *ctx, void (*async_cb)(void *fun), gpointer user_data)
974 * Can't pass the obj on the stack, since we are executing on the
975 * same stack. Can't save it into MonoJitTlsData, since it needs GC tracking.
976 * So put it into a register, and branch to a trampoline which
979 ctx->eax = (mgreg_t)user_data;
981 ctx->edx = (mgreg_t)async_cb;
984 ctx->esp = (ctx->esp - 16) & ~15;
985 ctx->eip = (mgreg_t)signal_exception_trampoline;
989 mono_arch_handle_exception (void *sigctx, gpointer obj)
991 #if defined(MONO_ARCH_USE_SIGACTION)
993 ucontext_t *ctx = (ucontext_t*)sigctx;
996 * Handling the exception in the signal handler is problematic, since the original
997 * signal is disabled, and we could run arbitrary code though the debugger. So
998 * resume into the normal stack and do most work there if possible.
1000 MonoJitTlsData *jit_tls = mono_native_tls_get_value (mono_jit_tls_id);
1002 /* Pass the ctx parameter in TLS */
1003 mono_sigctx_to_monoctx (ctx, &jit_tls->ex_ctx);
1005 mctx = jit_tls->ex_ctx;
1006 mono_setup_async_callback (&mctx, handle_signal_exception, obj);
1007 mono_monoctx_to_sigctx (&mctx, sigctx);
1010 #elif defined (TARGET_WIN32)
1012 MonoJitTlsData *jit_tls = mono_native_tls_get_value (mono_jit_tls_id);
1013 struct sigcontext *ctx = (struct sigcontext *)sigctx;
1015 mono_sigctx_to_monoctx (sigctx, &jit_tls->ex_ctx);
1017 mctx = jit_tls->ex_ctx;
1018 mono_setup_async_callback (&mctx, handle_signal_exception, obj);
1019 mono_monoctx_to_sigctx (&mctx, sigctx);
1025 mono_sigctx_to_monoctx (sigctx, &mctx);
1027 mono_handle_exception (&mctx, obj);
1029 mono_monoctx_to_sigctx (&mctx, sigctx);
1036 restore_soft_guard_pages (void)
1038 MonoJitTlsData *jit_tls = mono_native_tls_get_value (mono_jit_tls_id);
1039 if (jit_tls->stack_ovf_guard_base)
1040 mono_mprotect (jit_tls->stack_ovf_guard_base, jit_tls->stack_ovf_guard_size, MONO_MMAP_NONE);
1044 * this function modifies mctx so that when it is restored, it
1045 * won't execcute starting at mctx.eip, but in a function that
1046 * will restore the protection on the soft-guard pages and return back to
1047 * continue at mctx.eip.
1050 prepare_for_guard_pages (MonoContext *mctx)
1053 sp = (gpointer)(mctx->esp);
1055 /* the resturn addr */
1056 sp [0] = (gpointer)(mctx->eip);
1057 mctx->eip = (unsigned long)restore_soft_guard_pages;
1058 mctx->esp = (unsigned long)sp;
1062 altstack_handle_and_restore (MonoContext *ctx, gpointer obj, gboolean stack_ovf)
1068 mono_handle_exception (&mctx, obj);
1070 prepare_for_guard_pages (&mctx);
1071 mono_restore_context (&mctx);
1075 mono_arch_handle_altstack_exception (void *sigctx, MONO_SIG_HANDLER_INFO_TYPE *siginfo, gpointer fault_addr, gboolean stack_ovf)
1077 #ifdef MONO_ARCH_USE_SIGACTION
1078 MonoException *exc = NULL;
1079 ucontext_t *ctx = (ucontext_t*)sigctx;
1080 MonoJitInfo *ji = mini_jit_info_table_find (mono_domain_get (), (gpointer)UCONTEXT_REG_EIP (ctx), NULL);
1084 /* if we didn't find a managed method for the ip address and it matches the fault
1085 * address, we assume we followed a broken pointer during an indirect call, so
1086 * we try the lookup again with the return address pushed on the stack
1088 if (!ji && fault_addr == (gpointer)UCONTEXT_REG_EIP (ctx)) {
1089 glong *sp = (gpointer)UCONTEXT_REG_ESP (ctx);
1090 ji = mini_jit_info_table_find (mono_domain_get (), (gpointer)sp [0], NULL);
1092 UCONTEXT_REG_EIP (ctx) = sp [0];
1095 exc = mono_domain_get ()->stack_overflow_ex;
1097 mono_handle_native_sigsegv (SIGSEGV, sigctx, siginfo);
1098 /* setup a call frame on the real stack so that control is returned there
1099 * and exception handling can continue.
1100 * If this was a stack overflow the caller already ensured the stack pages
1101 * needed have been unprotected.
1102 * The frame looks like:
1109 // FIXME: test_only is no more.
1110 frame_size = sizeof (MonoContext) + sizeof (gpointer) * 4;
1113 sp = (gpointer)(UCONTEXT_REG_ESP (ctx) & ~15);
1114 sp = (gpointer)((char*)sp - frame_size);
1115 /* the incoming arguments are aligned to 16 bytes boundaries, so the return address IP
1118 sp [-1] = (gpointer)UCONTEXT_REG_EIP (ctx);
1121 sp [2] = (gpointer)stack_ovf;
1122 mono_sigctx_to_monoctx (sigctx, (MonoContext*)(sp + 4));
1123 /* at the return form the signal handler execution starts in altstack_handle_and_restore() */
1124 UCONTEXT_REG_EIP (ctx) = (unsigned long)altstack_handle_and_restore;
1125 UCONTEXT_REG_ESP (ctx) = (unsigned long)(sp - 1);
1129 #if MONO_SUPPORT_TASKLETS
1130 MonoContinuationRestore
1131 mono_tasklets_arch_restore (void)
1133 static guint8* saved = NULL;
1134 guint8 *code, *start;
1136 #ifdef __native_client_codegen__
1137 g_print("mono_tasklets_arch_restore needs to be aligned for Native Client\n");
1140 return (MonoContinuationRestore)saved;
1141 code = start = mono_global_codeman_reserve (48);
1142 /* the signature is: restore (MonoContinuation *cont, int state, MonoLMF **lmf_addr) */
1143 /* put cont in edx */
1144 x86_mov_reg_membase (code, X86_EDX, X86_ESP, 4, 4);
1145 /* state in eax, so it's setup as the return value */
1146 x86_mov_reg_membase (code, X86_EAX, X86_ESP, 8, 4);
1148 /* setup the copy of the stack */
1149 x86_mov_reg_membase (code, X86_ECX, X86_EDX, MONO_STRUCT_OFFSET (MonoContinuation, stack_used_size), 4);
1150 x86_shift_reg_imm (code, X86_SHR, X86_ECX, 2);
1152 x86_mov_reg_membase (code, X86_ESI, X86_EDX, MONO_STRUCT_OFFSET (MonoContinuation, saved_stack), 4);
1153 x86_mov_reg_membase (code, X86_EDI, X86_EDX, MONO_STRUCT_OFFSET (MonoContinuation, return_sp), 4);
1154 x86_prefix (code, X86_REP_PREFIX);
1157 /* now restore the registers from the LMF */
1158 x86_mov_reg_membase (code, X86_ECX, X86_EDX, MONO_STRUCT_OFFSET (MonoContinuation, lmf), 4);
1159 x86_mov_reg_membase (code, X86_EBX, X86_ECX, MONO_STRUCT_OFFSET (MonoLMF, ebx), 4);
1160 x86_mov_reg_membase (code, X86_EBP, X86_ECX, MONO_STRUCT_OFFSET (MonoLMF, ebp), 4);
1161 x86_mov_reg_membase (code, X86_ESI, X86_ECX, MONO_STRUCT_OFFSET (MonoLMF, esi), 4);
1162 x86_mov_reg_membase (code, X86_EDI, X86_ECX, MONO_STRUCT_OFFSET (MonoLMF, edi), 4);
1164 /* restore the lmf chain */
1165 /*x86_mov_reg_membase (code, X86_ECX, X86_ESP, 12, 4);
1166 x86_mov_membase_reg (code, X86_ECX, 0, X86_EDX, 4);*/
1168 x86_jump_membase (code, X86_EDX, MONO_STRUCT_OFFSET (MonoContinuation, return_ip));
1169 g_assert ((code - start) <= 48);
1171 return (MonoContinuationRestore)saved;
1176 * mono_arch_setup_resume_sighandler_ctx:
1178 * Setup CTX so execution continues at FUNC.
1181 mono_arch_setup_resume_sighandler_ctx (MonoContext *ctx, gpointer func)
1183 int align = (((gint32)MONO_CONTEXT_GET_SP (ctx)) % MONO_ARCH_FRAME_ALIGNMENT + 4);
1186 MONO_CONTEXT_SET_SP (ctx, (gsize)MONO_CONTEXT_GET_SP (ctx) - align);
1188 MONO_CONTEXT_SET_IP (ctx, func);