2 * mach-support-x86.c: mach support for x86
5 * Geoff Norton (gnorton@novell.com)
7 * (C) 2010 Novell, Inc.
16 #include "utils/mono-sigcontext.h"
17 #include "mach-support.h"
19 /* Known offsets used for TLS storage*/
21 /* All OSX versions up to 10.8 */
22 #define TLS_VECTOR_OFFSET_CATS 0x48
23 #define TLS_VECTOR_OFFSET_10_9 0xb0
24 #define TLS_VECTOR_OFFSET_10_11 0x100
27 /* This is 2 slots less than the known low */
28 #define TLS_PROBE_LOW_WATERMARK 0x40
29 /* This is 28 slots above the know high, which is more than the known high-low*/
30 #define TLS_PROBE_HIGH_WATERMARK 0x120
33 static int tls_vector_offset;
36 mono_mach_arch_get_ip (thread_state_t state)
38 x86_thread_state32_t *arch_state = (x86_thread_state32_t *) state;
40 return (void *) arch_state->__eip;
44 mono_mach_arch_get_sp (thread_state_t state)
46 x86_thread_state32_t *arch_state = (x86_thread_state32_t *) state;
48 return (void *) arch_state->__esp;
52 mono_mach_arch_get_mcontext_size ()
54 return sizeof (struct __darwin_mcontext32);
58 mono_mach_arch_thread_state_to_mcontext (thread_state_t state, void *context)
60 x86_thread_state32_t *arch_state = (x86_thread_state32_t *) state;
61 struct __darwin_mcontext32 *ctx = (struct __darwin_mcontext32 *) context;
63 ctx->__ss = *arch_state;
67 mono_mach_arch_mcontext_to_thread_state (void *context, thread_state_t state)
69 x86_thread_state32_t *arch_state = (x86_thread_state32_t *) state;
70 struct __darwin_mcontext32 *ctx = (struct __darwin_mcontext32 *) context;
72 *arch_state = ctx->__ss;
76 mono_mach_arch_thread_state_to_mono_context (thread_state_t state, MonoContext *context)
78 x86_thread_state32_t *arch_state = (x86_thread_state32_t *) state;
79 context->eax = arch_state->__eax;
80 context->ebx = arch_state->__ebx;
81 context->ecx = arch_state->__ecx;
82 context->edx = arch_state->__edx;
83 context->ebp = arch_state->__ebp;
84 context->esp = arch_state->__esp;
85 context->esi = arch_state->__edi;
86 context->edi = arch_state->__esi;
87 context->eip = arch_state->__eip;
92 mono_mach_arch_get_thread_state_size ()
94 return sizeof (x86_thread_state32_t);
98 mono_mach_arch_get_thread_state (thread_port_t thread, thread_state_t state, mach_msg_type_number_t *count)
100 #if defined(HOST_WATCHOS)
101 g_error ("thread_get_state() is not supported by this platform");
103 x86_thread_state32_t *arch_state = (x86_thread_state32_t *) state;
106 *count = x86_THREAD_STATE32_COUNT;
107 ret = thread_get_state (thread, x86_THREAD_STATE32, (thread_state_t) arch_state, count);
114 mono_mach_arch_set_thread_state (thread_port_t thread, thread_state_t state, mach_msg_type_number_t count)
116 #if defined(HOST_WATCHOS)
117 g_error ("thread_set_state() is not supported by this platform");
119 return thread_set_state (thread, x86_THREAD_STATE32, state, count);
124 mono_mach_get_tls_address_from_thread (pthread_t thread, pthread_key_t key)
126 /* OSX stores TLS values in a hidden array inside the pthread_t structure
127 * They are keyed off a giant array from a known offset into the pointer. This value
128 * is baked into their pthread_getspecific implementation
130 intptr_t *p = (intptr_t *) thread;
131 intptr_t **tsd = (intptr_t **) ((char*)p + tls_vector_offset);
132 g_assert (tls_vector_offset != -1);
134 return (void *) &tsd [key];
138 mono_mach_arch_get_tls_value_from_thread (pthread_t thread, guint32 key)
140 return *(void**)mono_mach_get_tls_address_from_thread (thread, key);
144 mono_mach_init (pthread_key_t key)
147 void *old_value = pthread_getspecific (key);
148 void *canary = (void*)0xDEADBEEFu;
150 pthread_key_create (&key, NULL);
151 g_assert (old_value != canary);
153 pthread_setspecific (key, canary);
155 /*First we probe for cats*/
156 tls_vector_offset = TLS_VECTOR_OFFSET_CATS;
157 if (mono_mach_arch_get_tls_value_from_thread (pthread_self (), key) == canary)
160 tls_vector_offset = TLS_VECTOR_OFFSET_10_9;
161 if (mono_mach_arch_get_tls_value_from_thread (pthread_self (), key) == canary)
164 tls_vector_offset = TLS_VECTOR_OFFSET_10_11;
165 if (mono_mach_arch_get_tls_value_from_thread (pthread_self (), key) == canary)
168 /*Fallback to scanning a large range of offsets*/
169 for (i = TLS_PROBE_LOW_WATERMARK; i <= TLS_PROBE_HIGH_WATERMARK; i += 4) {
170 tls_vector_offset = i;
171 if (mono_mach_arch_get_tls_value_from_thread (pthread_self (), key) == canary) {
172 g_warning ("Found new TLS offset at %d", i);
177 tls_vector_offset = -1;
178 g_warning ("could not discover the mach TLS offset");
180 pthread_setspecific (key, old_value);