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
25 static int tls_vector_offset;
28 mono_mach_arch_get_ip (thread_state_t state)
30 x86_thread_state32_t *arch_state = (x86_thread_state32_t *) state;
32 return (void *) arch_state->__eip;
36 mono_mach_arch_get_sp (thread_state_t state)
38 x86_thread_state32_t *arch_state = (x86_thread_state32_t *) state;
40 return (void *) arch_state->__esp;
44 mono_mach_arch_get_mcontext_size ()
46 return sizeof (struct __darwin_mcontext32);
50 mono_mach_arch_thread_state_to_mcontext (thread_state_t state, void *context)
52 x86_thread_state32_t *arch_state = (x86_thread_state32_t *) state;
53 struct __darwin_mcontext32 *ctx = (struct __darwin_mcontext32 *) context;
55 ctx->__ss = *arch_state;
59 mono_mach_arch_mcontext_to_thread_state (void *context, thread_state_t state)
61 x86_thread_state32_t *arch_state = (x86_thread_state32_t *) state;
62 struct __darwin_mcontext32 *ctx = (struct __darwin_mcontext32 *) context;
64 *arch_state = ctx->__ss;
68 mono_mach_arch_get_thread_state_size ()
70 return sizeof (x86_thread_state32_t);
74 mono_mach_arch_get_thread_state (thread_port_t thread, thread_state_t state, mach_msg_type_number_t *count)
76 x86_thread_state32_t *arch_state = (x86_thread_state32_t *) state;
79 *count = x86_THREAD_STATE32_COUNT;
81 ret = thread_get_state (thread, x86_THREAD_STATE32, (thread_state_t) arch_state, count);
87 mono_mach_arch_set_thread_state (thread_port_t thread, thread_state_t state, mach_msg_type_number_t count)
89 return thread_set_state (thread, x86_THREAD_STATE32, state, count);
93 mono_mach_get_tls_address_from_thread (pthread_t thread, pthread_key_t key)
95 /* OSX stores TLS values in a hidden array inside the pthread_t structure
96 * They are keyed off a giant array from a known offset into the pointer. This value
97 * is baked into their pthread_getspecific implementation
99 intptr_t *p = (intptr_t *) thread;
100 intptr_t **tsd = (intptr_t **) ((char*)p + tls_vector_offset);
102 return (void *) &tsd [key];
106 mono_mach_arch_get_tls_value_from_thread (pthread_t thread, guint32 key)
108 return *(void**)mono_mach_get_tls_address_from_thread (thread, key);
112 mono_mach_init (pthread_key_t key)
114 void *old_value = pthread_getspecific (key);
115 void *canary = (void*)0xDEADBEEFu;
117 pthread_key_create (&key, NULL);
118 g_assert (old_value != canary);
120 pthread_setspecific (key, canary);
122 /*First we probe for cats*/
123 tls_vector_offset = TLS_VECTOR_OFFSET_CATS;
124 if (mono_mach_arch_get_tls_value_from_thread (pthread_self (), key) == canary)
127 tls_vector_offset = TLS_VECTOR_OFFSET_10_9;
128 if (mono_mach_arch_get_tls_value_from_thread (pthread_self (), key) == canary)
131 g_error ("could not discover the mach TLS offset");
133 pthread_setspecific (key, old_value);