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 /* Known offsets used for TLS storage*/
25 static const int known_tls_offsets[] = {
26 0x48, /*Found on iOS 6 */
31 #define TLS_PROBE_COUNT (sizeof (known_tls_offsets) / sizeof (int))
33 /* This is 2 slots less than the known low */
34 #define TLS_PROBE_LOW_WATERMARK 0x40
35 /* This is 24 slots above the know high, which is the same diff as the knowns high-low*/
36 #define TLS_PROBE_HIGH_WATERMARK 0x108
38 static int tls_vector_offset;
41 mono_mach_arch_get_ip (thread_state_t state)
43 arm_thread_state_t *arch_state = (arm_thread_state_t *) state;
45 return (void *) arch_state->__pc;
49 mono_mach_arch_get_sp (thread_state_t state)
51 arm_thread_state_t *arch_state = (arm_thread_state_t *) state;
53 return (void *) arch_state->__sp;
57 mono_mach_arch_get_mcontext_size ()
59 return sizeof (struct __darwin_mcontext);
63 mono_mach_arch_thread_state_to_mcontext (thread_state_t state, void *context)
65 arm_thread_state_t *arch_state = (arm_thread_state_t *) state;
66 struct __darwin_mcontext *ctx = (struct __darwin_mcontext *) context;
68 ctx->__ss = *arch_state;
72 mono_mach_arch_mcontext_to_thread_state (void *context, thread_state_t state)
74 arm_thread_state_t *arch_state = (arm_thread_state_t *) state;
75 struct __darwin_mcontext *ctx = (struct __darwin_mcontext *) context;
77 *arch_state = ctx->__ss;
81 mono_mach_arch_get_thread_state_size ()
83 return sizeof (arm_thread_state_t);
87 mono_mach_arch_get_thread_state (thread_port_t thread, thread_state_t state, mach_msg_type_number_t *count)
89 arm_thread_state_t *arch_state = (arm_thread_state_t *) state;
92 *count = ARM_THREAD_STATE_COUNT;
94 ret = thread_get_state (thread, ARM_THREAD_STATE, (thread_state_t) arch_state, count);
100 mono_mach_arch_set_thread_state (thread_port_t thread, thread_state_t state, mach_msg_type_number_t count)
102 return thread_set_state (thread, ARM_THREAD_STATE_COUNT, state, count);
106 mono_mach_get_tls_address_from_thread (pthread_t thread, pthread_key_t key)
108 /* Mach stores TLS values in a hidden array inside the pthread_t structure
109 * They are keyed off a giant array from a known offset into the pointer. This value
110 * is baked into their pthread_getspecific implementation
112 intptr_t *p = (intptr_t *) thread;
113 intptr_t **tsd = (intptr_t **) ((char*)p + tls_vector_offset);
115 return (void *) &tsd [key];
119 mono_mach_arch_get_tls_value_from_thread (pthread_t thread, guint32 key)
121 return *(void**)mono_mach_get_tls_address_from_thread (thread, key);
125 mono_mach_init (pthread_key_t key)
128 void *old_value = pthread_getspecific (key);
129 void *canary = (void*)0xDEADBEEFu;
131 pthread_key_create (&key, NULL);
132 g_assert (old_value != canary);
134 pthread_setspecific (key, canary);
136 /*First we probe for cats*/
137 for (i = 0; i < TLS_PROBE_COUNT; ++i) {
138 tls_vector_offset = known_tls_offsets [i];
139 if (mono_mach_arch_get_tls_value_from_thread (pthread_self (), key) == canary)
143 /*Fallback to scanning a large range of offsets*/
144 for (i = TLS_PROBE_LOW_WATERMARK; i <= TLS_PROBE_HIGH_WATERMARK; i += 4) {
145 tls_vector_offset = i;
146 if (mono_mach_arch_get_tls_value_from_thread (pthread_self (), key) == canary) {
147 g_warning ("Found new TLS offset at %d", i);
152 g_error ("could not discover the mach TLS offset");
154 pthread_setspecific (key, old_value);