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 /* Snow Leopard has a horrible bug: http://openradar.appspot.com/7209349
83 * This causes obscure SIGTRAP's for any application that comes across this built on
84 * Snow Leopard. This is a horrible hack to ensure that the private __CFInitialize
85 * is run on the main thread, so that we don't get SIGTRAPs later
87 #if defined (__APPLE__) && (defined (__i386__) || defined (__x86_64__))
89 void *handle
= dlopen ("/System/Library/Frameworks/CoreFoundation.framework/CoreFoundation", RTLD_LAZY
);
99 mono_thread_state_init_from_handle (MonoThreadUnwindState
*tctx
, MonoThreadInfo
*info
, void *sigctx
)
102 mach_msg_type_number_t num_state
, num_fpstate
;
103 thread_state_t state
, fpstate
;
106 /*Zero enough state to make sure the caller doesn't confuse itself*/
108 tctx
->unwind_data
[MONO_UNWIND_DATA_DOMAIN
] = NULL
;
109 tctx
->unwind_data
[MONO_UNWIND_DATA_LMF
] = NULL
;
110 tctx
->unwind_data
[MONO_UNWIND_DATA_JIT_TLS
] = NULL
;
112 state
= (thread_state_t
) alloca (mono_mach_arch_get_thread_state_size ());
113 fpstate
= (thread_state_t
) alloca (mono_mach_arch_get_thread_fpstate_size ());
116 ret
= mono_mach_arch_get_thread_states (info
->native_handle
, state
, &num_state
, fpstate
, &num_fpstate
);
117 } while (ret
== KERN_ABORTED
);
118 if (ret
!= KERN_SUCCESS
)
121 mono_mach_arch_thread_states_to_mono_context (state
, fpstate
, &tctx
->ctx
);
123 /* mono_set_jit_tls () sets this */
124 void *jit_tls
= mono_thread_info_tls_get (info
, TLS_KEY_JIT_TLS
);
125 /* SET_APPDOMAIN () sets this */
126 void *domain
= mono_thread_info_tls_get (info
, TLS_KEY_DOMAIN
);
128 /*Thread already started to cleanup, can no longer capture unwind state*/
129 if (!jit_tls
|| !domain
)
133 * The current LMF address is kept in a separate TLS variable, and its hard to read its value without
134 * arch-specific code. But the address of the TLS variable is stored in another TLS variable which
135 * can be accessed through MonoThreadInfo.
137 /* mono_set_lmf_addr () sets this */
139 MonoLMF
**addr
= (MonoLMF
**)mono_thread_info_tls_get (info
, TLS_KEY_LMF_ADDR
);
143 tctx
->unwind_data
[MONO_UNWIND_DATA_DOMAIN
] = domain
;
144 tctx
->unwind_data
[MONO_UNWIND_DATA_JIT_TLS
] = jit_tls
;
145 tctx
->unwind_data
[MONO_UNWIND_DATA_LMF
] = lmf
;