6 * Geoff Norton (gnorton@novell.com)
7 * Rodrigo Kumpera (kumpera@gmail.com)
9 * (C) 2010 Novell, Inc.
10 * (C) 2011 Xamarin, Inc.
19 #include "utils/mono-sigcontext.h"
20 #include "utils/mono-compiler.h"
21 #include "mach-support.h"
23 /* _mcontext.h now defines __darwin_mcontext32, not __darwin_mcontext, starting with Xcode 5.1 */
24 #ifdef _STRUCT_MCONTEXT32
25 #define __darwin_mcontext __darwin_mcontext32
28 /* Known offsets used for TLS storage*/
31 static const int known_tls_offsets[] = {
32 0x48, /*Found on iOS 6 */
37 #define TLS_PROBE_COUNT (sizeof (known_tls_offsets) / sizeof (int))
39 /* This is 2 slots less than the known low */
40 #define TLS_PROBE_LOW_WATERMARK 0x40
41 /* This is 24 slots above the know high, which is the same diff as the knowns high-low*/
42 #define TLS_PROBE_HIGH_WATERMARK 0x108
44 static int tls_vector_offset;
47 mono_mach_arch_get_ip (thread_state_t state)
49 /* 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;
56 mono_mach_arch_get_sp (thread_state_t state)
58 arm_unified_thread_state_t *arch_state = (arm_unified_thread_state_t *) state;
60 return (void *) arch_state->ts_64.__sp;
64 mono_mach_arch_get_mcontext_size ()
66 return sizeof (struct __darwin_mcontext64);
70 mono_mach_arch_thread_states_to_mcontext (thread_state_t state, thread_state_t fpstate, void *context)
72 arm_unified_thread_state_t *arch_state = (arm_unified_thread_state_t *) state;
73 arm_neon_state64_t *arch_fpstate = (arm_neon_state64_t*) fpstate;
74 struct __darwin_mcontext64 *ctx = (struct __darwin_mcontext64 *) context;
76 ctx->__ss = arch_state->ts_64;
77 ctx->__ns = *arch_fpstate;
81 mono_mach_arch_mcontext_to_thread_states (void *context, thread_state_t state, thread_state_t fpstate)
83 arm_unified_thread_state_t *arch_state = (arm_unified_thread_state_t *) state;
84 arm_neon_state64_t *arch_fpstate = (arm_neon_state64_t*) fpstate;
85 struct __darwin_mcontext64 *ctx = (struct __darwin_mcontext64 *) context;
87 arch_state->ts_64 = ctx->__ss;
88 *arch_fpstate = ctx->__ns;
92 mono_mach_arch_thread_states_to_mono_context (thread_state_t state, thread_state_t fpstate, MonoContext *context)
95 arm_unified_thread_state_t *arch_state = (arm_unified_thread_state_t *) state;
96 arm_neon_state64_t *arch_fpstate = (arm_neon_state64_t*) fpstate;
98 for (i = 0; i < 29; ++i)
99 context->regs [i] = arch_state->ts_64.__x [i];
101 context->regs [ARMREG_R29] = arch_state->ts_64.__fp;
102 context->regs [ARMREG_R30] = arch_state->ts_64.__lr;
103 context->regs [ARMREG_SP] = arch_state->ts_64.__sp;
104 context->pc = arch_state->ts_64.__pc;
106 for (i = 0; i < 32; ++i)
107 context->fregs [i] = arch_fpstate->__v [i];
111 mono_mach_arch_get_thread_state_size ()
113 return sizeof (arm_unified_thread_state_t);
117 mono_mach_arch_get_thread_fpstate_size ()
119 return sizeof (arm_neon_state64_t);
123 mono_mach_arch_get_thread_states (thread_port_t thread, thread_state_t state, mach_msg_type_number_t *count, thread_state_t fpstate, mach_msg_type_number_t *fpcount)
125 arm_unified_thread_state_t *arch_state = (arm_unified_thread_state_t *) state;
126 arm_neon_state64_t *arch_fpstate = (arm_neon_state64_t *) fpstate;
129 *count = ARM_UNIFIED_THREAD_STATE_COUNT;
130 ret = thread_get_state (thread, ARM_UNIFIED_THREAD_STATE, (thread_state_t) arch_state, count);
131 if (ret != KERN_SUCCESS)
134 *fpcount = ARM_NEON_STATE64_COUNT;
135 ret = thread_get_state (thread, ARM_NEON_STATE64, (thread_state_t) arch_fpstate, fpcount);
140 mono_mach_arch_set_thread_states (thread_port_t thread, thread_state_t state, mach_msg_type_number_t count, thread_state_t fpstate, mach_msg_type_number_t fpcount)
143 ret = thread_set_state (thread, ARM_UNIFIED_THREAD_STATE, state, count);
144 if (ret != KERN_SUCCESS)
146 ret = thread_set_state (thread, ARM_NEON_STATE64, fpstate, fpcount);
151 mono_mach_get_tls_address_from_thread (pthread_t thread, pthread_key_t key)
153 /* Mach stores TLS values in a hidden array inside the pthread_t structure
154 * They are keyed off a giant array from a known offset into the pointer. This value
155 * is baked into their pthread_getspecific implementation
157 intptr_t *p = (intptr_t *) thread;
158 intptr_t **tsd = (intptr_t **) ((char*)p + tls_vector_offset);
159 g_assert (tls_vector_offset != -1);
161 return (void *) &tsd [key];
165 mono_mach_arch_get_tls_value_from_thread (pthread_t thread, guint32 key)
167 return *(void**)mono_mach_get_tls_address_from_thread (thread, key);
171 mono_mach_init (pthread_key_t key)
174 void *old_value = pthread_getspecific (key);
175 void *canary = (void*)0xDEADBEEFu;
177 pthread_key_create (&key, NULL);
178 g_assert (old_value != canary);
180 pthread_setspecific (key, canary);
182 /*First we probe for cats*/
183 for (i = 0; i < TLS_PROBE_COUNT; ++i) {
184 tls_vector_offset = known_tls_offsets [i];
185 if (mono_mach_arch_get_tls_value_from_thread (pthread_self (), key) == canary)
189 /*Fallback to scanning a large range of offsets*/
190 for (i = TLS_PROBE_LOW_WATERMARK; i <= TLS_PROBE_HIGH_WATERMARK; i += 4) {
191 tls_vector_offset = i;
192 if (mono_mach_arch_get_tls_value_from_thread (pthread_self (), key) == canary) {
193 g_warning ("Found new TLS offset at %d", i);
198 tls_vector_offset = -1;
199 g_warning ("could not discover the mach TLS offset");
201 pthread_setspecific (key, old_value);