[tests] Reenable enum equals test on interpreter (#18673)
[mono-project.git] / mono / mini / mini-darwin.c
blobb0d2a4d674edbcd409cc8100fdd4da4e1c18c797
1 /**
2 * \file
3 * Darwin/MacOS support for Mono.
5 * Authors:
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.
14 #include <config.h>
15 #include <signal.h>
16 #ifdef HAVE_ALLOCA_H
17 #include <alloca.h>
18 #endif
19 #ifdef HAVE_UNISTD_H
20 #include <unistd.h>
21 #endif
22 #include <math.h>
23 #ifdef HAVE_SYS_TIME_H
24 #include <sys/time.h>
25 #endif
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>
54 #include "mini.h"
55 #include "mini-runtime.h"
56 #include <string.h>
57 #include <ctype.h>
58 #include "trace.h"
59 #include "version.h"
61 #include "jit-icalls.h"
63 /* MacOS includes */
64 #include <mach/mach.h>
65 #include <mach/mach_error.h>
66 #include <mach/exception.h>
67 #include <mach/task.h>
68 #include <pthread.h>
69 #include <dlfcn.h>
70 #include <AvailabilityMacros.h>
72 /* This is #define'd by Boehm GC to _GC_dlopen. */
73 #undef dlopen
75 void* dlopen(const char* path, int mode);
77 void
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);
90 if (handle == NULL)
91 return;
93 dlclose (handle);
95 #endif
98 gboolean
99 mono_thread_state_init_from_handle (MonoThreadUnwindState *tctx, MonoThreadInfo *info, void *sigctx)
101 kern_return_t ret;
102 mach_msg_type_number_t num_state, num_fpstate;
103 thread_state_t state, fpstate;
105 g_assert (info);
106 /*Zero enough state to make sure the caller doesn't confuse itself*/
107 tctx->valid = FALSE;
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 ());
115 do {
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)
119 return FALSE;
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)
130 return FALSE;
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 */
138 MonoLMF *lmf = NULL;
139 MonoLMF **addr = (MonoLMF**)mono_thread_info_tls_get (info, TLS_KEY_LMF_ADDR);
140 if (addr)
141 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;
146 tctx->valid = TRUE;
148 return TRUE;