3 * exception support for x86
6 * Dietmar Maurer (dietmar@ximian.com)
8 * (C) 2001 Ximian, Inc.
16 #ifdef HAVE_UCONTEXT_H
20 #include <mono/metadata/abi-details.h>
21 #include <mono/arch/x86/x86-codegen.h>
22 #include <mono/metadata/appdomain.h>
23 #include <mono/metadata/tabledefs.h>
24 #include <mono/metadata/threads.h>
25 #include <mono/metadata/debug-helpers.h>
26 #include <mono/metadata/exception.h>
27 #include <mono/metadata/gc-internals.h>
28 #include <mono/metadata/mono-debug.h>
29 #include <mono/utils/mono-mmap.h>
33 #include "mini-runtime.h"
35 #include "aot-runtime.h"
37 static gpointer signal_exception_trampoline
;
40 mono_x86_get_signal_exception_trampoline (MonoTrampInfo
**info
, gboolean aot
);
43 static void (*restore_stack
) (void *);
45 static MonoW32ExceptionHandler fpe_handler
;
46 static MonoW32ExceptionHandler ill_handler
;
47 static MonoW32ExceptionHandler segv_handler
;
49 LPTOP_LEVEL_EXCEPTION_FILTER mono_old_win_toplevel_exception_filter
;
50 gpointer mono_win_vectored_exception_handle
;
51 extern int (*gUnhandledExceptionHandler
)(EXCEPTION_POINTERS
*);
53 #ifndef PROCESS_CALLBACK_FILTER_ENABLED
54 # define PROCESS_CALLBACK_FILTER_ENABLED 1
57 #define W32_SEH_HANDLE_EX(_ex) \
58 if (_ex##_handler) _ex##_handler(er->ExceptionCode, &info, ctx)
60 LONG CALLBACK
seh_unhandled_exception_filter(EXCEPTION_POINTERS
* ep
)
62 #ifndef MONO_CROSS_COMPILE
63 if (mono_old_win_toplevel_exception_filter
) {
64 return (*mono_old_win_toplevel_exception_filter
)(ep
);
68 mono_handle_native_crash ("SIGSEGV", NULL
, NULL
);
70 return EXCEPTION_CONTINUE_SEARCH
;
74 * mono_win32_get_handle_stackoverflow (void):
76 * Returns a pointer to a method which restores the current context stack
77 * and calls handle_exceptions, when done restores the original stack.
80 mono_win32_get_handle_stackoverflow (void)
82 static guint8
*start
= NULL
;
88 /* restore_contect (void *sigctx) */
89 start
= code
= mono_global_codeman_reserve (128);
91 /* load context into ebx */
92 x86_mov_reg_membase (code
, X86_EBX
, X86_ESP
, 4, 4);
94 /* move current stack into edi for later restore */
95 x86_mov_reg_reg (code
, X86_EDI
, X86_ESP
);
97 /* use the new freed stack from sigcontext */
98 /* XXX replace usage of struct sigcontext with MonoContext so we can use MONO_STRUCT_OFFSET */
99 x86_mov_reg_membase (code
, X86_ESP
, X86_EBX
, G_STRUCT_OFFSET (struct sigcontext
, esp
), 4);
101 /* get the current domain */
102 x86_call_code (code
, mono_domain_get
);
104 /* get stack overflow exception from domain object */
105 x86_mov_reg_membase (code
, X86_EAX
, X86_EAX
, G_STRUCT_OFFSET (MonoDomain
, stack_overflow_ex
), 4);
107 /* call mono_arch_handle_exception (sctx, stack_overflow_exception_obj) */
108 x86_push_reg (code
, X86_EAX
);
109 x86_push_reg (code
, X86_EBX
);
110 x86_call_code (code
, mono_arch_handle_exception
);
112 /* restore the SEH handler stack */
113 x86_mov_reg_reg (code
, X86_ESP
, X86_EDI
);
118 mono_arch_flush_icache (start
, code
- start
);
119 MONO_PROFILER_RAISE (jit_code_buffer
, (start
, code
- start
, MONO_PROFILER_CODE_BUFFER_EXCEPTION_HANDLING
, NULL
));
124 /* Special hack to workaround the fact that the
125 * when the SEH handler is called the stack is
126 * to small to recover.
128 * Stack walking part of this method is from mono_handle_exception
130 * The idea is simple;
131 * - walk the stack to free some space (64k)
132 * - set esp to new stack location
133 * - call mono_arch_handle_exception with stack overflow exception
134 * - set esp to SEH handlers stack
138 win32_handle_stack_overflow (EXCEPTION_POINTERS
* ep
, CONTEXT
*sctx
)
142 MonoDomain
*domain
= mono_domain_get ();
144 MonoJitTlsData
*jit_tls
= mono_tls_get_jit_tls ();
145 MonoLMF
*lmf
= jit_tls
->lmf
;
146 MonoContext initial_ctx
;
148 guint32 free_stack
= 0;
149 StackFrameInfo frame
;
151 mono_sigctx_to_monoctx (sctx
, &ctx
);
153 /* get our os page size */
155 page_size
= si
.dwPageSize
;
157 /* Let's walk the stack to recover
158 * the needed stack space (if possible)
160 memset (&rji
, 0, sizeof (rji
));
163 free_stack
= (guint8
*)(MONO_CONTEXT_GET_BP (&ctx
)) - (guint8
*)(MONO_CONTEXT_GET_BP (&initial_ctx
));
165 /* try to free 64kb from our stack */
169 mono_arch_unwind_frame (domain
, jit_tls
, &rji
, &ctx
, &new_ctx
, &lmf
, NULL
, &frame
);
171 g_warning ("Exception inside function without unwind info");
172 g_assert_not_reached ();
175 if (frame
.ji
!= (gpointer
)-1) {
176 free_stack
= (guint8
*)(MONO_CONTEXT_GET_BP (&ctx
)) - (guint8
*)(MONO_CONTEXT_GET_BP (&initial_ctx
));
179 /* todo: we should call abort if ji is -1 */
181 } while (free_stack
< 64 * 1024 && frame
.ji
!= (gpointer
) -1);
183 mono_monoctx_to_sigctx (&ctx
, sctx
);
185 /* todo: install new stack-guard page */
187 /* use the new stack and call mono_arch_handle_exception () */
188 restore_stack (sctx
);
192 * Unhandled Exception Filter
193 * Top-level per-process exception handler.
195 LONG CALLBACK
seh_vectored_exception_handler(EXCEPTION_POINTERS
* ep
)
197 EXCEPTION_RECORD
* er
;
200 MonoJitTlsData
*jit_tls
= mono_tls_get_jit_tls ();
201 MonoWindowsSigHandlerInfo info
= { TRUE
, ep
};
203 /* If the thread is not managed by the runtime return early */
205 return EXCEPTION_CONTINUE_SEARCH
;
207 res
= EXCEPTION_CONTINUE_EXECUTION
;
209 er
= ep
->ExceptionRecord
;
210 ctx
= ep
->ContextRecord
;
212 switch (er
->ExceptionCode
) {
213 case EXCEPTION_STACK_OVERFLOW
:
214 win32_handle_stack_overflow (ep
, ctx
);
216 case EXCEPTION_ACCESS_VIOLATION
:
217 W32_SEH_HANDLE_EX(segv
);
219 case EXCEPTION_ILLEGAL_INSTRUCTION
:
220 W32_SEH_HANDLE_EX(ill
);
222 case EXCEPTION_INT_DIVIDE_BY_ZERO
:
223 case EXCEPTION_INT_OVERFLOW
:
224 case EXCEPTION_FLT_DIVIDE_BY_ZERO
:
225 case EXCEPTION_FLT_OVERFLOW
:
226 case EXCEPTION_FLT_UNDERFLOW
:
227 case EXCEPTION_FLT_INEXACT_RESULT
:
228 W32_SEH_HANDLE_EX(fpe
);
231 info
.handled
= FALSE
;
236 /* Don't copy context back if we chained exception
237 * as the handler may have modfied the EXCEPTION_POINTERS
238 * directly. We don't pass sigcontext to chained handlers.
239 * Return continue search so the UnhandledExceptionFilter
240 * can correctly chain the exception.
242 res
= EXCEPTION_CONTINUE_SEARCH
;
248 void win32_seh_init()
250 /* install restore stack helper */
252 restore_stack
= (void (*) (void*))mono_win32_get_handle_stackoverflow ();
254 mono_old_win_toplevel_exception_filter
= SetUnhandledExceptionFilter(seh_unhandled_exception_filter
);
255 mono_win_vectored_exception_handle
= AddVectoredExceptionHandler (1, seh_vectored_exception_handler
);
258 void win32_seh_cleanup()
260 if (mono_old_win_toplevel_exception_filter
)
261 SetUnhandledExceptionFilter(mono_old_win_toplevel_exception_filter
);
262 RemoveVectoredExceptionHandler (mono_win_vectored_exception_handle
);
265 void win32_seh_set_handler(int type
, MonoW32ExceptionHandler handler
)
269 fpe_handler
= handler
;
272 ill_handler
= handler
;
275 segv_handler
= handler
;
282 #endif /* TARGET_WIN32 */
285 * mono_arch_get_restore_context:
287 * Returns a pointer to a method which restores a previously saved sigcontext.
290 mono_arch_get_restore_context (MonoTrampInfo
**info
, gboolean aot
)
292 guint8
*start
= NULL
;
294 MonoJumpInfo
*ji
= NULL
;
295 GSList
*unwind_ops
= NULL
;
297 /* restore_contect (MonoContext *ctx) */
299 start
= code
= mono_global_codeman_reserve (128);
302 x86_mov_reg_membase (code
, X86_EAX
, X86_ESP
, 4, 4);
305 x86_mov_reg_membase (code
, X86_EBX
, X86_EAX
, MONO_STRUCT_OFFSET (MonoContext
, ebx
), 4);
308 x86_mov_reg_membase (code
, X86_EDI
, X86_EAX
, MONO_STRUCT_OFFSET (MonoContext
, edi
), 4);
311 x86_mov_reg_membase (code
, X86_ESI
, X86_EAX
, MONO_STRUCT_OFFSET (MonoContext
, esi
), 4);
314 x86_mov_reg_membase (code
, X86_EDX
, X86_EAX
, MONO_STRUCT_OFFSET (MonoContext
, edx
), 4);
317 * The context resides on the stack, in the stack frame of the
318 * caller of this function. The stack pointer that we need to
319 * restore is potentially many stack frames higher up, so the
320 * distance between them can easily be more than the red zone
321 * size. Hence the stack pointer can be restored only after
322 * we have finished loading everything from the context.
325 /* load ESP into EBP */
326 x86_mov_reg_membase (code
, X86_EBP
, X86_EAX
, MONO_STRUCT_OFFSET (MonoContext
, esp
), 4);
327 /* load return address into ECX */
328 x86_mov_reg_membase (code
, X86_ECX
, X86_EAX
, MONO_STRUCT_OFFSET (MonoContext
, eip
), 4);
329 /* save the return addr to the restored stack - 4 */
330 x86_mov_membase_reg (code
, X86_EBP
, -4, X86_ECX
, 4);
332 /* load EBP into ECX */
333 x86_mov_reg_membase (code
, X86_ECX
, X86_EAX
, MONO_STRUCT_OFFSET (MonoContext
, ebp
), 4);
334 /* save EBP to the restored stack - 8 */
335 x86_mov_membase_reg (code
, X86_EBP
, -8, X86_ECX
, 4);
337 /* load EAX into ECX */
338 x86_mov_reg_membase (code
, X86_ECX
, X86_EAX
, MONO_STRUCT_OFFSET (MonoContext
, eax
), 4);
339 /* save EAX to the restored stack - 12 */
340 x86_mov_membase_reg (code
, X86_EBP
, -12, X86_ECX
, 4);
343 x86_mov_reg_membase (code
, X86_ECX
, X86_EAX
, MONO_STRUCT_OFFSET (MonoContext
, ecx
), 4);
345 /* restore ESP - 12 */
346 x86_lea_membase (code
, X86_ESP
, X86_EBP
, -12);
348 x86_pop_reg (code
, X86_EAX
);
350 x86_pop_reg (code
, X86_EBP
);
351 /* jump to the saved IP */
355 *info
= mono_tramp_info_create ("restore_context", start
, code
- start
, ji
, unwind_ops
);
359 for (l
= unwind_ops
; l
; l
= l
->next
)
361 g_slist_free (unwind_ops
);
364 mono_arch_flush_icache (start
, code
- start
);
365 MONO_PROFILER_RAISE (jit_code_buffer
, (start
, code
- start
, MONO_PROFILER_CODE_BUFFER_EXCEPTION_HANDLING
, NULL
));
371 * mono_arch_get_call_filter:
373 * Returns a pointer to a method which calls an exception filter. We
374 * also use this function to call finally handlers (we pass NULL as
375 * @exc object in this case).
378 mono_arch_get_call_filter (MonoTrampInfo
**info
, gboolean aot
)
382 MonoJumpInfo
*ji
= NULL
;
383 GSList
*unwind_ops
= NULL
;
384 guint kMaxCodeSize
= 64;
386 /* call_filter (MonoContext *ctx, unsigned long eip) */
387 start
= code
= mono_global_codeman_reserve (kMaxCodeSize
);
389 x86_push_reg (code
, X86_EBP
);
390 x86_mov_reg_reg (code
, X86_EBP
, X86_ESP
);
391 x86_push_reg (code
, X86_EBX
);
392 x86_push_reg (code
, X86_EDI
);
393 x86_push_reg (code
, X86_ESI
);
396 x86_mov_reg_membase (code
, X86_EAX
, X86_EBP
, 8, 4);
398 x86_mov_reg_membase (code
, X86_ECX
, X86_EBP
, 12, 4);
400 x86_push_reg (code
, X86_EBP
);
403 x86_mov_reg_membase (code
, X86_EBP
, X86_EAX
, MONO_STRUCT_OFFSET (MonoContext
, ebp
), 4);
404 /* restore registers used by global register allocation (EBX & ESI) */
405 x86_mov_reg_membase (code
, X86_EBX
, X86_EAX
, MONO_STRUCT_OFFSET (MonoContext
, ebx
), 4);
406 x86_mov_reg_membase (code
, X86_ESI
, X86_EAX
, MONO_STRUCT_OFFSET (MonoContext
, esi
), 4);
407 x86_mov_reg_membase (code
, X86_EDI
, X86_EAX
, MONO_STRUCT_OFFSET (MonoContext
, edi
), 4);
409 /* align stack and save ESP */
410 x86_mov_reg_reg (code
, X86_EDX
, X86_ESP
);
411 x86_alu_reg_imm (code
, X86_AND
, X86_ESP
, -MONO_ARCH_FRAME_ALIGNMENT
);
412 g_assert (MONO_ARCH_FRAME_ALIGNMENT
>= 8);
413 x86_alu_reg_imm (code
, X86_SUB
, X86_ESP
, MONO_ARCH_FRAME_ALIGNMENT
- 8);
414 x86_push_reg (code
, X86_EDX
);
416 /* call the handler */
417 x86_call_reg (code
, X86_ECX
);
420 x86_pop_reg (code
, X86_ESP
);
423 x86_pop_reg (code
, X86_EBP
);
425 /* restore saved regs */
426 x86_pop_reg (code
, X86_ESI
);
427 x86_pop_reg (code
, X86_EDI
);
428 x86_pop_reg (code
, X86_EBX
);
433 *info
= mono_tramp_info_create ("call_filter", start
, code
- start
, ji
, unwind_ops
);
437 for (l
= unwind_ops
; l
; l
= l
->next
)
439 g_slist_free (unwind_ops
);
442 mono_arch_flush_icache (start
, code
- start
);
443 MONO_PROFILER_RAISE (jit_code_buffer
, (start
, code
- start
, MONO_PROFILER_CODE_BUFFER_EXCEPTION_HANDLING
, NULL
));
445 g_assert ((code
- start
) < kMaxCodeSize
);
450 * mono_x86_throw_exception:
452 * C function called from the throw trampolines.
455 mono_x86_throw_exception (host_mgreg_t
*regs
, MonoObject
*exc
,
456 host_mgreg_t eip
, gboolean rethrow
, gboolean preserve_ips
)
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_checked (exc
, mono_defaults
.exception_class
, error
)) {
477 MonoException
*mono_ex
= (MonoException
*)exc
;
479 mono_ex
->stack_trace
= NULL
;
480 mono_ex
->trace_ips
= NULL
;
481 } else if (preserve_ips
) {
482 mono_ex
->caught_in_unmanaged
= TRUE
;
485 mono_error_assert_ok (error
);
487 /* adjust eip so that it point into the call instruction */
490 mono_handle_exception (&ctx
, exc
);
492 mono_restore_context (&ctx
);
494 g_assert_not_reached ();
498 mono_x86_throw_corlib_exception (host_mgreg_t
*regs
, guint32 ex_token_index
,
499 host_mgreg_t eip
, gint32 pc_offset
)
501 guint32 ex_token
= MONO_TOKEN_TYPE_DEF
| ex_token_index
;
504 ex
= mono_exception_from_token (m_class_get_image (mono_defaults
.exception_class
), ex_token
);
508 /* Negate the ip adjustment done in mono_x86_throw_exception () */
511 mono_x86_throw_exception (regs
, (MonoObject
*)ex
, eip
, FALSE
, FALSE
);
515 mono_x86_resume_unwind (host_mgreg_t
*regs
, MonoObject
*exc
,
516 host_mgreg_t eip
, gboolean rethrow
)
520 ctx
.esp
= regs
[X86_ESP
];
522 ctx
.ebp
= regs
[X86_EBP
];
523 ctx
.edi
= regs
[X86_EDI
];
524 ctx
.esi
= regs
[X86_ESI
];
525 ctx
.ebx
= regs
[X86_EBX
];
526 ctx
.edx
= regs
[X86_EDX
];
527 ctx
.ecx
= regs
[X86_ECX
];
528 ctx
.eax
= regs
[X86_EAX
];
530 mono_resume_unwind (&ctx
);
534 * get_throw_trampoline:
536 * Generate a call to mono_x86_throw_exception/
537 * mono_x86_throw_corlib_exception.
538 * If LLVM is true, generate code which assumes the caller is LLVM generated code,
539 * which doesn't push the arguments.
542 get_throw_trampoline (const char *name
, gboolean rethrow
, gboolean llvm
, gboolean corlib
, gboolean llvm_abs
, gboolean resume_unwind
, MonoTrampInfo
**info
, gboolean aot
, gboolean preserve_ips
)
544 guint8
*start
, *code
, *labels
[16];
545 int i
, stack_size
, stack_offset
, arg_offsets
[5], regs_offset
;
546 MonoJumpInfo
*ji
= NULL
;
547 GSList
*unwind_ops
= NULL
;
548 guint kMaxCodeSize
= 192;
550 start
= code
= mono_global_codeman_reserve (kMaxCodeSize
);
555 * On apple, the stack is misaligned by the pushing of the return address.
558 /* On OSX, we don't generate alignment code to save space */
561 stack_size
+= MONO_ARCH_FRAME_ALIGNMENT
- 4;
564 * The stack looks like this:
565 * <pc offset> (only if corlib is TRUE)
566 * <exception object>/<type token>
567 * <return addr> <- esp (unaligned on apple)
570 unwind_ops
= mono_arch_get_cie_program ();
573 x86_alu_reg_imm (code
, X86_SUB
, X86_ESP
, stack_size
);
574 mono_add_unwind_op_def_cfa_offset (unwind_ops
, code
, start
, stack_size
+ 4);
579 arg_offsets
[3] = 12;
580 arg_offsets
[4] = 16;
584 for (i
= 0; i
< X86_NREG
; ++i
)
586 x86_mov_membase_reg (code
, X86_ESP
, regs_offset
+ (i
* 4), i
, 4);
587 /* Calculate the offset between the current sp and the sp of the caller */
589 /* LLVM doesn't push the arguments */
590 stack_offset
= stack_size
+ 4;
594 stack_offset
= stack_size
+ 4 + 8;
596 /* We don't generate stack alignment code on osx to save space */
599 /* One argument + stack alignment */
600 stack_offset
= stack_size
+ 4 + 4;
602 /* Pop the alignment added by OP_THROW too */
603 stack_offset
+= MONO_ARCH_FRAME_ALIGNMENT
- 4;
605 if (mono_do_x86_stack_align
)
606 stack_offset
+= MONO_ARCH_FRAME_ALIGNMENT
- 4;
611 x86_lea_membase (code
, X86_EAX
, X86_ESP
, stack_offset
);
612 x86_mov_membase_reg (code
, X86_ESP
, regs_offset
+ (X86_ESP
* 4), X86_EAX
, 4);
617 x86_shift_reg_imm (code
, X86_SHR
, X86_EAX
, 11);
618 x86_alu_reg_imm (code
, X86_AND
, X86_EAX
, 7);
619 x86_alu_reg_imm (code
, X86_CMP
, X86_EAX
, 0);
621 x86_branch8 (code
, X86_CC_EQ
, 0, FALSE
);
623 x86_jump_code (code
, labels
[0]);
624 mono_x86_patch (labels
[1], code
);
626 /* Set arg1 == regs */
627 x86_lea_membase (code
, X86_EAX
, X86_ESP
, regs_offset
);
628 x86_mov_membase_reg (code
, X86_ESP
, arg_offsets
[0], X86_EAX
, 4);
629 /* Set arg2 == exc/ex_token_index */
631 x86_mov_reg_imm (code
, X86_EAX
, 0);
633 x86_mov_reg_membase (code
, X86_EAX
, X86_ESP
, stack_size
+ 4, 4);
634 x86_mov_membase_reg (code
, X86_ESP
, arg_offsets
[1], X86_EAX
, 4);
635 /* Set arg3 == eip */
637 x86_alu_reg_reg (code
, X86_XOR
, X86_EAX
, X86_EAX
);
639 x86_mov_reg_membase (code
, X86_EAX
, X86_ESP
, stack_size
, 4);
640 x86_mov_membase_reg (code
, X86_ESP
, arg_offsets
[2], X86_EAX
, 4);
641 /* Set arg4 == rethrow/pc_offset */
643 x86_mov_membase_imm (code
, X86_ESP
, arg_offsets
[3], 0, 4);
645 x86_mov_reg_membase (code
, X86_EAX
, X86_ESP
, stack_size
+ 8, 4);
648 * The caller is LLVM code which passes the absolute address not a pc offset,
649 * so compensate by passing 0 as 'ip' and passing the negated abs address as
652 x86_neg_reg (code
, X86_EAX
);
654 x86_mov_membase_reg (code
, X86_ESP
, arg_offsets
[3], X86_EAX
, 4);
656 x86_mov_membase_imm (code
, X86_ESP
, arg_offsets
[3], rethrow
, 4);
658 /* Set arg4 == preserve_ips */
659 x86_mov_membase_imm (code
, X86_ESP
, arg_offsets
[4], preserve_ips
, 4);
663 // This can be called from runtime code, which can't guarantee that
664 // ebx contains the got address.
665 // So emit the got address loading code too
666 code
= mono_arch_emit_load_got_addr (start
, code
, NULL
, &ji
);
667 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");
668 x86_call_reg (code
, X86_EAX
);
670 x86_call_code (code
, resume_unwind
? (gpointer
)(mono_x86_resume_unwind
) : (corlib
? (gpointer
)mono_x86_throw_corlib_exception
: (gpointer
)mono_x86_throw_exception
));
672 x86_breakpoint (code
);
674 g_assert ((code
- start
) < kMaxCodeSize
);
677 *info
= mono_tramp_info_create (name
, start
, code
- start
, ji
, unwind_ops
);
681 for (l
= unwind_ops
; l
; l
= l
->next
)
683 g_slist_free (unwind_ops
);
686 mono_arch_flush_icache (start
, code
- start
);
687 MONO_PROFILER_RAISE (jit_code_buffer
, (start
, code
- start
, MONO_PROFILER_CODE_BUFFER_EXCEPTION_HANDLING
, NULL
));
693 * mono_arch_get_throw_exception:
694 * \returns a function pointer which can be used to raise
695 * exceptions. The returned function has the following
696 * signature: void (*func) (MonoException *exc);
697 * For example to raise an arithmetic exception you can use:
699 * x86_push_imm (code, mono_get_exception_arithmetic ());
700 * x86_call_code (code, arch_get_throw_exception ());
704 mono_arch_get_throw_exception (MonoTrampInfo
**info
, gboolean aot
)
706 return get_throw_trampoline ("throw_exception", FALSE
, FALSE
, FALSE
, FALSE
, FALSE
, info
, aot
, FALSE
);
710 mono_arch_get_rethrow_exception (MonoTrampInfo
**info
, gboolean aot
)
712 return get_throw_trampoline ("rethrow_exception", TRUE
, FALSE
, FALSE
, FALSE
, FALSE
, info
, aot
, FALSE
);
716 mono_arch_get_rethrow_preserve_exception (MonoTrampInfo
**info
, gboolean aot
)
718 return get_throw_trampoline ("rethrow_preserve_exception", TRUE
, FALSE
, FALSE
, FALSE
, FALSE
, info
, aot
, TRUE
);
722 * mono_arch_get_throw_corlib_exception:
723 * \returns a function pointer which can be used to raise
724 * corlib exceptions. The returned function has the following
725 * signature: void (*func) (guint32 ex_token, guint32 offset);
726 * Here, offset is the offset which needs to be substracted from the caller IP
727 * to get the IP of the throw. Passing the offset has the advantage that it
728 * needs no relocations in the caller.
731 mono_arch_get_throw_corlib_exception (MonoTrampInfo
**info
, gboolean aot
)
733 return get_throw_trampoline ("throw_corlib_exception", FALSE
, FALSE
, TRUE
, FALSE
, FALSE
, info
, aot
, FALSE
);
737 mono_arch_exceptions_init (void)
740 MonoTrampInfo
*tinfo
;
743 * If we're running WoW64, we need to set the usermode exception policy
744 * for SEHs to behave. This requires hotfix http://support.microsoft.com/kb/976038
745 * or (eventually) Windows 7 SP1.
748 HMODULE kernel32
= LoadLibraryW (L
"kernel32.dll");
750 typedef BOOL (WINAPI
* SetProcessUserModeExceptionPolicy_t
) (DWORD dwFlags
);
751 typedef BOOL (WINAPI
* GetProcessUserModeExceptionPolicy_t
) (PDWORD dwFlags
);
752 GetProcessUserModeExceptionPolicy_t
const getter
= (GetProcessUserModeExceptionPolicy_t
)GetProcAddress (kernel32
, "GetProcessUserModeExceptionPolicy");
754 SetProcessUserModeExceptionPolicy_t
const setter
= (SetProcessUserModeExceptionPolicy_t
)GetProcAddress (kernel32
, "SetProcessUserModeExceptionPolicy");
758 setter (flags
& ~PROCESS_CALLBACK_FILTER_ENABLED
);
765 signal_exception_trampoline
= mono_aot_get_trampoline ("x86_signal_exception_trampoline");
769 /* LLVM needs different throw trampolines */
770 tramp
= get_throw_trampoline ("llvm_throw_exception_trampoline", FALSE
, TRUE
, FALSE
, FALSE
, FALSE
, &tinfo
, FALSE
, FALSE
);
771 mono_register_jit_icall (tramp
, "llvm_throw_exception_trampoline", NULL
, TRUE
);
772 mono_tramp_info_register (tinfo
, NULL
);
774 tramp
= get_throw_trampoline ("llvm_rethrow_exception_trampoline", TRUE
, TRUE
, FALSE
, FALSE
, FALSE
, &tinfo
, FALSE
, FALSE
);
775 mono_register_jit_icall (tramp
, "llvm_rethrow_exception_trampoline", NULL
, TRUE
);
776 mono_tramp_info_register (tinfo
, NULL
);
778 tramp
= get_throw_trampoline ("llvm_throw_corlib_exception_trampoline", FALSE
, TRUE
, TRUE
, FALSE
, FALSE
, &tinfo
, FALSE
, FALSE
);
779 mono_register_jit_icall (tramp
, "llvm_throw_corlib_exception_trampoline", NULL
, TRUE
);
780 mono_tramp_info_register (tinfo
, NULL
);
782 tramp
= get_throw_trampoline ("llvm_throw_corlib_exception_abs_trampoline", FALSE
, TRUE
, TRUE
, TRUE
, FALSE
, &tinfo
, FALSE
, FALSE
);
783 mono_register_jit_icall (tramp
, "llvm_throw_corlib_exception_abs_trampoline", NULL
, TRUE
);
784 mono_tramp_info_register (tinfo
, NULL
);
786 tramp
= get_throw_trampoline ("llvm_resume_unwind_trampoline", FALSE
, FALSE
, FALSE
, FALSE
, TRUE
, &tinfo
, FALSE
, FALSE
);
787 mono_register_jit_icall (tramp
, "llvm_resume_unwind_trampoline", NULL
, TRUE
);
788 mono_tramp_info_register (tinfo
, NULL
);
790 signal_exception_trampoline
= mono_x86_get_signal_exception_trampoline (&tinfo
, FALSE
);
791 mono_tramp_info_register (tinfo
, NULL
);
795 * mono_arch_unwind_frame:
797 * See exceptions-amd64.c for docs.
800 mono_arch_unwind_frame (MonoDomain
*domain
, MonoJitTlsData
*jit_tls
,
801 MonoJitInfo
*ji
, MonoContext
*ctx
,
802 MonoContext
*new_ctx
, MonoLMF
**lmf
,
803 host_mgreg_t
**save_locations
,
804 StackFrameInfo
*frame
)
806 gpointer ip
= MONO_CONTEXT_GET_IP (ctx
);
808 memset (frame
, 0, sizeof (StackFrameInfo
));
814 host_mgreg_t regs
[MONO_MAX_IREGS
+ 1];
816 guint32 unwind_info_len
;
819 if (ji
->is_trampoline
)
820 frame
->type
= FRAME_TYPE_TRAMPOLINE
;
822 frame
->type
= FRAME_TYPE_MANAGED
;
824 unwind_info
= mono_jinfo_get_unwind_info (ji
, &unwind_info_len
);
826 regs
[X86_EAX
] = new_ctx
->eax
;
827 regs
[X86_EBX
] = new_ctx
->ebx
;
828 regs
[X86_ECX
] = new_ctx
->ecx
;
829 regs
[X86_EDX
] = new_ctx
->edx
;
830 regs
[X86_ESP
] = new_ctx
->esp
;
831 regs
[X86_EBP
] = new_ctx
->ebp
;
832 regs
[X86_ESI
] = new_ctx
->esi
;
833 regs
[X86_EDI
] = new_ctx
->edi
;
834 regs
[X86_NREG
] = new_ctx
->eip
;
836 gboolean success
= mono_unwind_frame ((guint8
*)unwind_info
, unwind_info_len
, (guint8
*)ji
->code_start
,
837 (guint8
*)ji
->code_start
+ ji
->code_size
,
838 (guint8
*)ip
, NULL
, regs
, MONO_MAX_IREGS
+ 1,
839 save_locations
, MONO_MAX_IREGS
, &cfa
);
844 new_ctx
->eax
= regs
[X86_EAX
];
845 new_ctx
->ebx
= regs
[X86_EBX
];
846 new_ctx
->ecx
= regs
[X86_ECX
];
847 new_ctx
->edx
= regs
[X86_EDX
];
848 new_ctx
->esp
= regs
[X86_ESP
];
849 new_ctx
->ebp
= regs
[X86_EBP
];
850 new_ctx
->esi
= regs
[X86_ESI
];
851 new_ctx
->edi
= regs
[X86_EDI
];
852 new_ctx
->eip
= regs
[X86_NREG
];
854 /* The CFA becomes the new SP value */
855 new_ctx
->esp
= (gssize
)cfa
;
862 g_assert ((((gsize
)(*lmf
)->previous_lmf
) & 2) == 0);
864 if ((ji
= mini_jit_info_table_find (domain
, (gpointer
)(uintptr_t)(*lmf
)->eip
, NULL
))) {
869 frame
->method
= (*lmf
)->method
;
872 new_ctx
->esi
= (*lmf
)->esi
;
873 new_ctx
->edi
= (*lmf
)->edi
;
874 new_ctx
->ebx
= (*lmf
)->ebx
;
875 new_ctx
->ebp
= (*lmf
)->ebp
;
876 new_ctx
->eip
= (*lmf
)->eip
;
881 frame
->type
= FRAME_TYPE_MANAGED_TO_NATIVE
;
883 /* Check if we are in a trampoline LMF frame */
884 if ((gsize
)((*lmf
)->previous_lmf
) & 1) {
885 /* lmf->esp is set by the trampoline code */
886 new_ctx
->esp
= (*lmf
)->esp
;
889 /* the lmf is always stored on the stack, so the following
890 * expression points to a stack location which can be used as ESP */
891 new_ctx
->esp
= (unsigned long)&((*lmf
)->eip
);
893 *lmf
= (MonoLMF
*)(((gsize
)(*lmf
)->previous_lmf
) & ~3);
902 mono_arch_ip_from_context (void *sigctx
)
904 #if defined(HOST_WATCHOS)
905 printf("WARNING: mono_arch_ip_from_context() called!\n");
907 #elif defined(MONO_CROSS_COMPILE)
908 g_assert_not_reached ();
910 #elif defined(MONO_ARCH_USE_SIGACTION)
911 ucontext_t
*ctx
= (ucontext_t
*)sigctx
;
912 return (gpointer
)UCONTEXT_REG_EIP (ctx
);
913 #elif defined(HOST_WIN32)
914 return (gpointer
)((CONTEXT
*)sigctx
)->Eip
;
916 struct sigcontext
*ctx
= sigctx
;
917 return (gpointer
)ctx
->SC_EIP
;
924 * Called by resuming from a signal handler.
927 handle_signal_exception (gpointer obj
)
929 MonoJitTlsData
*jit_tls
= mono_tls_get_jit_tls ();
932 memcpy (&ctx
, &jit_tls
->ex_ctx
, sizeof (MonoContext
));
934 mono_handle_exception (&ctx
, (MonoObject
*)obj
);
936 mono_restore_context (&ctx
);
940 * mono_x86_get_signal_exception_trampoline:
942 * This x86 specific trampoline is used to call handle_signal_exception.
945 mono_x86_get_signal_exception_trampoline (MonoTrampInfo
**info
, gboolean aot
)
947 guint8
*start
, *code
;
948 MonoJumpInfo
*ji
= NULL
;
949 GSList
*unwind_ops
= NULL
;
952 start
= code
= mono_global_codeman_reserve (128);
954 /* FIXME no unwind before we push ip */
956 x86_push_reg (code
, X86_ECX
);
958 mono_add_unwind_op_def_cfa (unwind_ops
, code
, start
, X86_ESP
, 4);
959 mono_add_unwind_op_offset (unwind_ops
, code
, start
, X86_NREG
, -4);
961 /* Fix the alignment to be what apple expects */
964 x86_alu_reg_imm (code
, X86_SUB
, X86_ESP
, stack_size
);
965 mono_add_unwind_op_def_cfa_offset (unwind_ops
, code
, start
, stack_size
+ 4);
968 x86_mov_membase_reg (code
, X86_ESP
, 0, X86_EAX
, 4);
969 /* Branch to target */
970 x86_call_reg (code
, X86_EDX
);
972 g_assert ((code
- start
) < 128);
975 *info
= mono_tramp_info_create ("x86_signal_exception_trampoline", start
, code
- start
, ji
, unwind_ops
);
979 for (l
= unwind_ops
; l
; l
= l
->next
)
981 g_slist_free (unwind_ops
);
984 mono_arch_flush_icache (start
, code
- start
);
985 MONO_PROFILER_RAISE (jit_code_buffer
, (start
, code
- start
, MONO_PROFILER_CODE_BUFFER_EXCEPTION_HANDLING
, NULL
));
992 mono_arch_setup_async_callback (MonoContext
*ctx
, void (*async_cb
)(void *fun
), gpointer user_data
)
995 * Can't pass the obj on the stack, since we are executing on the
996 * same stack. Can't save it into MonoJitTlsData, since it needs GC tracking.
997 * So put it into a register, and branch to a trampoline which
1000 ctx
->eax
= (host_mgreg_t
)(gsize
)user_data
;
1001 ctx
->ecx
= ctx
->eip
;
1002 ctx
->edx
= (host_mgreg_t
)(gsize
)async_cb
;
1005 ctx
->esp
= (ctx
->esp
- 16) & ~15;
1006 ctx
->eip
= (host_mgreg_t
)(gsize
)signal_exception_trampoline
;
1010 mono_arch_handle_exception (void *sigctx
, gpointer obj
)
1012 #if defined(MONO_ARCH_USE_SIGACTION)
1014 ucontext_t
*ctx
= (ucontext_t
*)sigctx
;
1017 * Handling the exception in the signal handler is problematic, since the original
1018 * signal is disabled, and we could run arbitrary code though the debugger. So
1019 * resume into the normal stack and do most work there if possible.
1021 MonoJitTlsData
*jit_tls
= mono_tls_get_jit_tls ();
1023 /* Pass the ctx parameter in TLS */
1024 mono_sigctx_to_monoctx (ctx
, &jit_tls
->ex_ctx
);
1026 mctx
= jit_tls
->ex_ctx
;
1027 mono_setup_async_callback (&mctx
, handle_signal_exception
, obj
);
1028 mono_monoctx_to_sigctx (&mctx
, sigctx
);
1031 #elif defined (TARGET_WIN32)
1033 MonoJitTlsData
*jit_tls
= mono_tls_get_jit_tls ();
1035 mono_sigctx_to_monoctx (sigctx
, &jit_tls
->ex_ctx
);
1037 mctx
= jit_tls
->ex_ctx
;
1038 mono_setup_async_callback (&mctx
, handle_signal_exception
, obj
);
1039 mono_monoctx_to_sigctx (&mctx
, sigctx
);
1045 mono_sigctx_to_monoctx (sigctx
, &mctx
);
1047 mono_handle_exception (&mctx
, obj
);
1049 mono_monoctx_to_sigctx (&mctx
, sigctx
);
1056 restore_soft_guard_pages (void)
1058 MonoJitTlsData
*jit_tls
= mono_tls_get_jit_tls ();
1060 if (jit_tls
->stack_ovf_guard_base
)
1061 mono_mprotect (jit_tls
->stack_ovf_guard_base
, jit_tls
->stack_ovf_guard_size
, MONO_MMAP_NONE
);
1063 if (jit_tls
->stack_ovf_pending
) {
1064 MonoDomain
*domain
= mono_domain_get ();
1065 jit_tls
->stack_ovf_pending
= 0;
1066 return (MonoObject
*) domain
->stack_overflow_ex
;
1072 * this function modifies mctx so that when it is restored, it
1073 * won't execcute starting at mctx.eip, but in a function that
1074 * will restore the protection on the soft-guard pages and return back to
1075 * continue at mctx.eip.
1078 prepare_for_guard_pages (MonoContext
*mctx
)
1081 sp
= (gpointer
*)(mctx
->esp
);
1083 /* the return addr */
1084 sp
[0] = (gpointer
)(mctx
->eip
);
1085 mctx
->eip
= (unsigned long)restore_soft_guard_pages
;
1086 mctx
->esp
= (unsigned long)sp
;
1091 altstack_handle_and_restore (MonoContext
*ctx
, gpointer obj
, gboolean stack_ovf
)
1097 mono_handle_exception (&mctx
, (MonoObject
*)obj
);
1099 MonoJitTlsData
*jit_tls
= mono_tls_get_jit_tls ();
1100 jit_tls
->stack_ovf_pending
= 1;
1101 prepare_for_guard_pages (&mctx
);
1103 mono_restore_context (&mctx
);
1107 mono_arch_handle_altstack_exception (void *sigctx
, MONO_SIG_HANDLER_INFO_TYPE
*siginfo
, gpointer fault_addr
, gboolean stack_ovf
)
1109 #if defined (MONO_ARCH_USE_SIGACTION) && !defined (MONO_CROSS_COMPILE)
1110 MonoException
*exc
= NULL
;
1111 ucontext_t
*ctx
= (ucontext_t
*)sigctx
;
1112 MonoJitInfo
*ji
= mini_jit_info_table_find (mono_domain_get (), (gpointer
)UCONTEXT_REG_EIP (ctx
), NULL
);
1116 /* if we didn't find a managed method for the ip address and it matches the fault
1117 * address, we assume we followed a broken pointer during an indirect call, so
1118 * we try the lookup again with the return address pushed on the stack
1120 if (!ji
&& fault_addr
== (gpointer
)UCONTEXT_REG_EIP (ctx
)) {
1121 glong
*sp
= (glong
*)UCONTEXT_REG_ESP (ctx
);
1122 ji
= mini_jit_info_table_find (mono_domain_get (), (gpointer
)sp
[0], NULL
);
1124 UCONTEXT_REG_EIP (ctx
) = sp
[0];
1127 exc
= mono_domain_get ()->stack_overflow_ex
;
1130 mono_sigctx_to_monoctx (sigctx
, &mctx
);
1131 mono_handle_native_crash ("SIGSEGV", &mctx
, siginfo
);
1133 /* setup a call frame on the real stack so that control is returned there
1134 * and exception handling can continue.
1135 * If this was a stack overflow the caller already ensured the stack pages
1136 * needed have been unprotected.
1137 * The frame looks like:
1144 // FIXME: test_only is no more.
1145 frame_size
= sizeof (MonoContext
) + sizeof (gpointer
) * 4;
1148 sp
= (gpointer
*)(UCONTEXT_REG_ESP (ctx
) & ~15);
1149 sp
= (gpointer
*)((char*)sp
- frame_size
);
1150 /* the incoming arguments are aligned to 16 bytes boundaries, so the return address IP
1153 sp
[-1] = (gpointer
)UCONTEXT_REG_EIP (ctx
);
1156 sp
[2] = (gpointer
)stack_ovf
;
1157 mono_sigctx_to_monoctx (sigctx
, (MonoContext
*)(sp
+ 4));
1158 /* at the return form the signal handler execution starts in altstack_handle_and_restore() */
1159 UCONTEXT_REG_EIP (ctx
) = (unsigned long)altstack_handle_and_restore
;
1160 UCONTEXT_REG_ESP (ctx
) = (unsigned long)(sp
- 1);
1164 #if MONO_SUPPORT_TASKLETS
1165 MonoContinuationRestore
1166 mono_tasklets_arch_restore (void)
1168 static guint8
* saved
= NULL
;
1169 guint8
*code
, *start
;
1172 return (MonoContinuationRestore
)saved
;
1173 code
= start
= mono_global_codeman_reserve (48);
1174 /* the signature is: restore (MonoContinuation *cont, int state, MonoLMF **lmf_addr) */
1175 /* put cont in edx */
1176 x86_mov_reg_membase (code
, X86_EDX
, X86_ESP
, 4, 4);
1177 /* state in eax, so it's setup as the return value */
1178 x86_mov_reg_membase (code
, X86_EAX
, X86_ESP
, 8, 4);
1179 /* lmf_addr in ebx */
1180 x86_mov_reg_membase(code
, X86_EBX
, X86_ESP
, 0x0C, 4);
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);
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
);
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_EBP
, X86_ECX
, MONO_STRUCT_OFFSET (MonoLMF
, ebp
), 4);
1194 x86_mov_reg_membase (code
, X86_ESP
, X86_ECX
, MONO_STRUCT_OFFSET (MonoLMF
, esp
), 4);
1196 /* restore the lmf chain */
1197 /*x86_mov_reg_membase (code, X86_ECX, X86_ESP, 12, 4);
1198 x86_mov_membase_reg (code, X86_ECX, 0, X86_EDX, 4);*/
1200 x86_jump_membase (code
, X86_EDX
, MONO_STRUCT_OFFSET (MonoContinuation
, return_ip
));
1202 mono_arch_flush_icache (start
, code
- start
);
1203 MONO_PROFILER_RAISE (jit_code_buffer
, (start
, code
- start
, MONO_PROFILER_CODE_BUFFER_EXCEPTION_HANDLING
, NULL
));
1204 g_assert ((code
- start
) <= 48);
1207 return (MonoContinuationRestore
)saved
;
1212 * mono_arch_setup_resume_sighandler_ctx:
1214 * Setup CTX so execution continues at FUNC.
1217 mono_arch_setup_resume_sighandler_ctx (MonoContext
*ctx
, gpointer func
)
1219 int align
= (((gint32
)MONO_CONTEXT_GET_SP (ctx
)) % MONO_ARCH_FRAME_ALIGNMENT
+ 4);
1222 MONO_CONTEXT_SET_SP (ctx
, (gsize
)MONO_CONTEXT_GET_SP (ctx
) - align
);
1224 MONO_CONTEXT_SET_IP (ctx
, func
);