2 * mach-support-x86.c: mach support for x86
5 * Geoff Norton (gnorton@novell.com)
6 * Rodrigo Kumpera (kumpera@gmail.com)
8 * (C) 2010 Novell, Inc.
9 * (C) 2013 Xamarin, Inc.
18 #include "utils/mono-sigcontext.h"
19 #include "mach-support.h"
21 /* Known offsets used for TLS storage*/
23 /* All OSX versions up to 10.8 */
24 #define TLS_VECTOR_OFFSET_CATS 0x60
25 #define TLS_VECTOR_OFFSET_10_9 0xe0
26 #define TLS_VECTOR_OFFSET_10_11 0x100
28 /* This is 2 slots less than the known low */
29 #define TLS_PROBE_LOW_WATERMARK 0x50
30 /* This is 28 slots above the know high, which is more than the known high-low*/
31 #define TLS_PROBE_HIGH_WATERMARK 0x200
34 static int tls_vector_offset;
37 mono_mach_arch_get_ip (thread_state_t state)
39 x86_thread_state64_t *arch_state = (x86_thread_state64_t *) state;
41 return (void *) arch_state->__rip;
45 mono_mach_arch_get_sp (thread_state_t state)
47 x86_thread_state64_t *arch_state = (x86_thread_state64_t *) state;
49 return (void *) arch_state->__rsp;
53 mono_mach_arch_get_mcontext_size ()
55 return sizeof (struct __darwin_mcontext64);
59 mono_mach_arch_thread_state_to_mcontext (thread_state_t state, void *context)
61 x86_thread_state64_t *arch_state = (x86_thread_state64_t *) state;
62 struct __darwin_mcontext64 *ctx = (struct __darwin_mcontext64 *) context;
64 ctx->__ss = *arch_state;
68 mono_mach_arch_mcontext_to_thread_state (void *context, thread_state_t state)
70 x86_thread_state64_t *arch_state = (x86_thread_state64_t *) state;
71 struct __darwin_mcontext64 *ctx = (struct __darwin_mcontext64 *) context;
73 *arch_state = ctx->__ss;
77 mono_mach_arch_get_thread_state_size ()
79 return sizeof (x86_thread_state64_t);
83 mono_mach_arch_get_thread_state (thread_port_t thread, thread_state_t state, mach_msg_type_number_t *count)
85 x86_thread_state64_t *arch_state = (x86_thread_state64_t *) state;
88 *count = x86_THREAD_STATE64_COUNT;
90 ret = thread_get_state (thread, x86_THREAD_STATE64, (thread_state_t) arch_state, count);
96 mono_mach_arch_set_thread_state (thread_port_t thread, thread_state_t state, mach_msg_type_number_t count)
98 return thread_set_state (thread, x86_THREAD_STATE64, state, count);
102 mono_mach_get_tls_address_from_thread (pthread_t thread, pthread_key_t key)
104 /* OSX stores TLS values in a hidden array inside the pthread_t structure
105 * They are keyed off a giant array from a known offset into the pointer. This value
106 * is baked into their pthread_getspecific implementation
108 intptr_t *p = (intptr_t *)thread;
109 intptr_t **tsd = (intptr_t **) ((char*)p + tls_vector_offset);
110 g_assert (tls_vector_offset != -1);
112 return (void *) &tsd [key];
116 mono_mach_arch_get_tls_value_from_thread (pthread_t thread, guint32 key)
118 return *(void**)mono_mach_get_tls_address_from_thread (thread, key);
122 mono_mach_init (pthread_key_t key)
125 void *old_value = pthread_getspecific (key);
126 void *canary = (void*)0xDEADBEEFu;
128 pthread_key_create (&key, NULL);
129 g_assert (old_value != canary);
131 pthread_setspecific (key, canary);
133 /*First we probe for cats*/
134 tls_vector_offset = TLS_VECTOR_OFFSET_CATS;
135 if (mono_mach_arch_get_tls_value_from_thread (pthread_self (), key) == canary)
138 tls_vector_offset = TLS_VECTOR_OFFSET_10_9;
139 if (mono_mach_arch_get_tls_value_from_thread (pthread_self (), key) == canary)
142 tls_vector_offset = TLS_VECTOR_OFFSET_10_11;
143 if (mono_mach_arch_get_tls_value_from_thread (pthread_self (), key) == canary)
146 /*Fallback to scanning a large range of offsets*/
147 for (i = TLS_PROBE_LOW_WATERMARK; i <= TLS_PROBE_HIGH_WATERMARK; i += 4) {
148 tls_vector_offset = i;
149 if (mono_mach_arch_get_tls_value_from_thread (pthread_self (), key) == canary) {
150 g_warning ("Found new TLS offset at %d", i);
155 tls_vector_offset = -1;
156 g_warning ("could not discover the mach TLS offset");
158 pthread_setspecific (key, old_value);