2 * mach-support-arm.c: mach support for ARM
5 * Geoff Norton (gnorton@novell.com)
6 * Rodrigo Kumpera (kumpera@gmail.com)
8 * (C) 2010 Novell, Inc.
9 * (C) 2011 Xamarin, Inc.
18 #include "utils/mono-sigcontext.h"
19 #include "utils/mono-compiler.h"
20 #include "mach-support.h"
22 /* _mcontext.h now defines __darwin_mcontext32, not __darwin_mcontext, starting with Xcode 5.1 */
23 #ifdef _STRUCT_MCONTEXT32
24 #define __darwin_mcontext __darwin_mcontext32
27 /* Known offsets used for TLS storage*/
30 static const int known_tls_offsets[] = {
31 0x48, /*Found on iOS 6 */
36 #define TLS_PROBE_COUNT (sizeof (known_tls_offsets) / sizeof (int))
38 /* This is 2 slots less than the known low */
39 #define TLS_PROBE_LOW_WATERMARK 0x40
40 /* This is 24 slots above the know high, which is the same diff as the knowns high-low*/
41 #define TLS_PROBE_HIGH_WATERMARK 0x108
43 static int tls_vector_offset;
46 mono_mach_arch_get_ip (thread_state_t state)
48 /* Can't use unified_thread_state on !ARM64 since this has to compile on armv6 too */
50 arm_unified_thread_state_t *arch_state = (arm_unified_thread_state_t *) state;
52 return (void *) arch_state->ts_64.__pc;
54 arm_thread_state_t *arch_state = (arm_thread_state_t *) state;
56 return (void *) arch_state->__pc;
61 mono_mach_arch_get_sp (thread_state_t state)
64 arm_unified_thread_state_t *arch_state = (arm_unified_thread_state_t *) state;
66 return (void *) arch_state->ts_64.__sp;
68 arm_thread_state_t *arch_state = (arm_thread_state_t *) state;
70 return (void *) arch_state->__sp;
75 mono_mach_arch_get_mcontext_size ()
78 return sizeof (struct __darwin_mcontext64);
80 return sizeof (struct __darwin_mcontext);
85 mono_mach_arch_thread_state_to_mcontext (thread_state_t state, void *context)
88 arm_unified_thread_state_t *arch_state = (arm_unified_thread_state_t *) state;
89 struct __darwin_mcontext64 *ctx = (struct __darwin_mcontext64 *) context;
91 ctx->__ss = arch_state->ts_64;
93 arm_thread_state_t *arch_state = (arm_thread_state_t *) state;
94 struct __darwin_mcontext *ctx = (struct __darwin_mcontext *) context;
96 ctx->__ss = *arch_state;
101 mono_mach_arch_mcontext_to_thread_state (void *context, thread_state_t state)
104 arm_unified_thread_state_t *arch_state = (arm_unified_thread_state_t *) state;
105 struct __darwin_mcontext64 *ctx = (struct __darwin_mcontext64 *) context;
107 arch_state->ts_64 = ctx->__ss;
109 arm_thread_state_t *arch_state = (arm_thread_state_t *) state;
110 struct __darwin_mcontext *ctx = (struct __darwin_mcontext *) context;
112 *arch_state = ctx->__ss;
117 mono_mach_arch_get_thread_state_size ()
120 return sizeof (arm_unified_thread_state_t);
122 return sizeof (arm_thread_state_t);
127 mono_mach_arch_get_thread_state (thread_port_t thread, thread_state_t state, mach_msg_type_number_t *count)
130 arm_unified_thread_state_t *arch_state = (arm_unified_thread_state_t *) state;
133 *count = ARM_UNIFIED_THREAD_STATE_COUNT;
135 ret = thread_get_state (thread, ARM_UNIFIED_THREAD_STATE, (thread_state_t) arch_state, count);
137 arm_thread_state_t *arch_state = (arm_thread_state_t *) state;
140 *count = ARM_THREAD_STATE_COUNT;
142 ret = thread_get_state (thread, ARM_THREAD_STATE, (thread_state_t) arch_state, count);
148 mono_mach_arch_set_thread_state (thread_port_t thread, thread_state_t state, mach_msg_type_number_t count)
151 return thread_set_state (thread, ARM_UNIFIED_THREAD_STATE_COUNT, state, count);
153 return thread_set_state (thread, ARM_THREAD_STATE_COUNT, state, count);
158 mono_mach_get_tls_address_from_thread (pthread_t thread, pthread_key_t key)
160 /* Mach stores TLS values in a hidden array inside the pthread_t structure
161 * They are keyed off a giant array from a known offset into the pointer. This value
162 * is baked into their pthread_getspecific implementation
164 intptr_t *p = (intptr_t *) thread;
165 intptr_t **tsd = (intptr_t **) ((char*)p + tls_vector_offset);
167 return (void *) &tsd [key];
171 mono_mach_arch_get_tls_value_from_thread (pthread_t thread, guint32 key)
173 return *(void**)mono_mach_get_tls_address_from_thread (thread, key);
177 mono_mach_init (pthread_key_t key)
180 void *old_value = pthread_getspecific (key);
181 void *canary = (void*)0xDEADBEEFu;
183 pthread_key_create (&key, NULL);
184 g_assert (old_value != canary);
186 pthread_setspecific (key, canary);
188 /*First we probe for cats*/
189 for (i = 0; i < TLS_PROBE_COUNT; ++i) {
190 tls_vector_offset = known_tls_offsets [i];
191 if (mono_mach_arch_get_tls_value_from_thread (pthread_self (), key) == canary)
195 /*Fallback to scanning a large range of offsets*/
196 for (i = TLS_PROBE_LOW_WATERMARK; i <= TLS_PROBE_HIGH_WATERMARK; i += 4) {
197 tls_vector_offset = i;
198 if (mono_mach_arch_get_tls_value_from_thread (pthread_self (), key) == canary) {
199 g_warning ("Found new TLS offset at %d", i);
204 g_error ("could not discover the mach TLS offset");
206 pthread_setspecific (key, old_value);