3 * Darwin/MacOS support for Mono.
6 * Mono Team (mono-list@lists.ximian.com)
8 * Copyright 2001-2003 Ximian, Inc.
9 * Copyright 2003-2011 Novell, Inc (http://www.novell.com)
10 * Copyright 2011 Xamarin, Inc (http://www.xamarin.com)
12 * Licensed under the MIT license. See LICENSE file in the project root for full license information.
23 #ifdef HAVE_SYS_TIME_H
27 #include <mono/metadata/assembly.h>
28 #include <mono/metadata/loader.h>
29 #include <mono/metadata/tabledefs.h>
30 #include <mono/metadata/class.h>
31 #include <mono/metadata/object.h>
32 #include <mono/metadata/tokentype.h>
33 #include <mono/metadata/tabledefs.h>
34 #include <mono/metadata/threads.h>
35 #include <mono/metadata/appdomain.h>
36 #include <mono/metadata/debug-helpers.h>
37 #include <mono/metadata/profiler-private.h>
38 #include <mono/metadata/mono-config.h>
39 #include <mono/metadata/environment.h>
40 #include <mono/metadata/mono-debug.h>
41 #include <mono/metadata/threads-types.h>
42 #include <mono/metadata/verify.h>
43 #include <mono/metadata/verify-internals.h>
44 #include <mono/metadata/mempool-internals.h>
45 #include <mono/metadata/attach.h>
46 #include <mono/metadata/gc-internals.h>
47 #include <mono/utils/mono-math.h>
48 #include <mono/utils/mono-compiler.h>
49 #include <mono/utils/mono-counters.h>
50 #include <mono/utils/mono-logger-internals.h>
51 #include <mono/utils/mono-mmap.h>
52 #include <mono/utils/dtrace.h>
55 #include "mini-runtime.h"
61 #include "jit-icalls.h"
64 #include <mach/mach.h>
65 #include <mach/mach_error.h>
66 #include <mach/exception.h>
67 #include <mach/task.h>
70 #include <AvailabilityMacros.h>
72 /* This is #define'd by Boehm GC to _GC_dlopen. */
75 void* dlopen(const char* path
, int mode
);
78 mono_runtime_install_handlers (void)
80 mono_runtime_posix_install_handlers ();
82 #if !defined (HOST_WATCHOS) && !defined (HOST_TVOS)
83 /* LLDB installs task-wide Mach exception handlers. XNU dispatches Mach
84 * exceptions first to any registered "activation" handler and then to
85 * any registered task handler before dispatching the exception to a
86 * host-wide Mach exception handler that does translation to POSIX
87 * signals. This makes it impossible to use LLDB with an
88 * implicit-null-check-enabled Mono; continuing execution after LLDB
89 * traps an EXC_BAD_ACCESS will result in LLDB's EXC_BAD_ACCESS handler
90 * being invoked again. This also interferes with the translation of
91 * SIGFPEs to .NET-level ArithmeticExceptions. Work around this here by
92 * installing a no-op task-wide Mach exception handler for
93 * EXC_BAD_ACCESS and EXC_ARITHMETIC.
95 kern_return_t kr
= task_set_exception_ports (
97 EXC_MASK_BAD_ACCESS
| EXC_MASK_ARITHMETIC
, /* SIGSEGV, SIGFPE */
99 EXCEPTION_STATE_IDENTITY
,
100 MACHINE_THREAD_STATE
);
101 if (kr
!= KERN_SUCCESS
)
102 g_warning ("mono_runtime_install_handlers: task_set_exception_ports failed");
105 /* Snow Leopard has a horrible bug: http://openradar.appspot.com/7209349
106 * This causes obscure SIGTRAP's for any application that comes across this built on
107 * Snow Leopard. This is a horrible hack to ensure that the private __CFInitialize
108 * is run on the main thread, so that we don't get SIGTRAPs later
110 #if defined (__APPLE__) && (defined (__i386__) || defined (__x86_64__))
112 void *handle
= dlopen ("/System/Library/Frameworks/CoreFoundation.framework/CoreFoundation", RTLD_LAZY
);
122 mono_thread_state_init_from_handle (MonoThreadUnwindState
*tctx
, MonoThreadInfo
*info
, void *sigctx
)
125 mach_msg_type_number_t num_state
, num_fpstate
;
126 thread_state_t state
, fpstate
;
129 /*Zero enough state to make sure the caller doesn't confuse itself*/
131 tctx
->unwind_data
[MONO_UNWIND_DATA_DOMAIN
] = NULL
;
132 tctx
->unwind_data
[MONO_UNWIND_DATA_LMF
] = NULL
;
133 tctx
->unwind_data
[MONO_UNWIND_DATA_JIT_TLS
] = NULL
;
135 state
= (thread_state_t
) alloca (mono_mach_arch_get_thread_state_size ());
136 fpstate
= (thread_state_t
) alloca (mono_mach_arch_get_thread_fpstate_size ());
139 ret
= mono_mach_arch_get_thread_states (info
->native_handle
, state
, &num_state
, fpstate
, &num_fpstate
);
140 } while (ret
== KERN_ABORTED
);
141 if (ret
!= KERN_SUCCESS
)
144 mono_mach_arch_thread_states_to_mono_context (state
, fpstate
, &tctx
->ctx
);
146 /* mono_set_jit_tls () sets this */
147 void *jit_tls
= mono_thread_info_tls_get (info
, TLS_KEY_JIT_TLS
);
148 /* SET_APPDOMAIN () sets this */
149 void *domain
= mono_thread_info_tls_get (info
, TLS_KEY_DOMAIN
);
151 /*Thread already started to cleanup, can no longer capture unwind state*/
152 if (!jit_tls
|| !domain
)
156 * The current LMF address is kept in a separate TLS variable, and its hard to read its value without
157 * arch-specific code. But the address of the TLS variable is stored in another TLS variable which
158 * can be accessed through MonoThreadInfo.
160 /* mono_set_lmf_addr () sets this */
162 MonoLMF
**addr
= (MonoLMF
**)mono_thread_info_tls_get (info
, TLS_KEY_LMF_ADDR
);
166 tctx
->unwind_data
[MONO_UNWIND_DATA_DOMAIN
] = domain
;
167 tctx
->unwind_data
[MONO_UNWIND_DATA_JIT_TLS
] = jit_tls
;
168 tctx
->unwind_data
[MONO_UNWIND_DATA_LMF
] = lmf
;