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"
22 #include <mono/arch/amd64/amd64-codegen.h>
24 /* Known offsets used for TLS storage*/
26 /* All OSX versions up to 10.8 */
27 #define TLS_VECTOR_OFFSET_CATS 0x60
28 #define TLS_VECTOR_OFFSET_10_9 0xe0
29 #define TLS_VECTOR_OFFSET_10_11 0x100
31 /* This is 2 slots less than the known low */
32 #define TLS_PROBE_LOW_WATERMARK 0x50
33 /* This is 28 slots above the know high, which is more than the known high-low*/
34 #define TLS_PROBE_HIGH_WATERMARK 0x200
37 static int tls_vector_offset;
40 mono_mach_arch_get_ip (thread_state_t state)
42 x86_thread_state64_t *arch_state = (x86_thread_state64_t *) state;
44 return (void *) arch_state->__rip;
48 mono_mach_arch_get_sp (thread_state_t state)
50 x86_thread_state64_t *arch_state = (x86_thread_state64_t *) state;
52 return (void *) arch_state->__rsp;
56 mono_mach_arch_get_mcontext_size ()
58 return sizeof (struct __darwin_mcontext64);
62 mono_mach_arch_thread_state_to_mcontext (thread_state_t state, void *context)
64 x86_thread_state64_t *arch_state = (x86_thread_state64_t *) state;
65 struct __darwin_mcontext64 *ctx = (struct __darwin_mcontext64 *) context;
67 ctx->__ss = *arch_state;
71 mono_mach_arch_mcontext_to_thread_state (void *context, thread_state_t state)
73 x86_thread_state64_t *arch_state = (x86_thread_state64_t *) state;
74 struct __darwin_mcontext64 *ctx = (struct __darwin_mcontext64 *) context;
76 *arch_state = ctx->__ss;
80 mono_mach_arch_thread_state_to_mono_context (thread_state_t state, MonoContext *context)
82 x86_thread_state64_t *arch_state = (x86_thread_state64_t *) state;
83 context->gregs [AMD64_RAX] = arch_state->__rax;
84 context->gregs [AMD64_RBX] = arch_state->__rbx;
85 context->gregs [AMD64_RCX] = arch_state->__rcx;
86 context->gregs [AMD64_RDX] = arch_state->__rdx;
87 context->gregs [AMD64_RDI] = arch_state->__rdi;
88 context->gregs [AMD64_RBP] = arch_state->__rbp;
89 context->gregs [AMD64_RSP] = arch_state->__rsp;
90 context->gregs [AMD64_R8] = arch_state->__r8;
91 context->gregs [AMD64_R9] = arch_state->__r9;
92 context->gregs [AMD64_R10] = arch_state->__r10;
93 context->gregs [AMD64_R11] = arch_state->__r11;
94 context->gregs [AMD64_R12] = arch_state->__r12;
95 context->gregs [AMD64_R13] = arch_state->__r13;
96 context->gregs [AMD64_R14] = arch_state->__r14;
97 context->gregs [AMD64_R15] = arch_state->__r15;
98 context->gregs [AMD64_RIP] = arch_state->__rip;
102 mono_mach_arch_get_thread_state_size ()
104 return sizeof (x86_thread_state64_t);
108 mono_mach_arch_get_thread_state (thread_port_t thread, thread_state_t state, mach_msg_type_number_t *count)
110 x86_thread_state64_t *arch_state = (x86_thread_state64_t *) state;
113 *count = x86_THREAD_STATE64_COUNT;
115 ret = thread_get_state (thread, x86_THREAD_STATE64, (thread_state_t) arch_state, count);
121 mono_mach_arch_set_thread_state (thread_port_t thread, thread_state_t state, mach_msg_type_number_t count)
123 return thread_set_state (thread, x86_THREAD_STATE64, state, count);
127 mono_mach_get_tls_address_from_thread (pthread_t thread, pthread_key_t key)
129 /* OSX stores TLS values in a hidden array inside the pthread_t structure
130 * They are keyed off a giant array from a known offset into the pointer. This value
131 * is baked into their pthread_getspecific implementation
133 intptr_t *p = (intptr_t *)thread;
134 intptr_t **tsd = (intptr_t **) ((char*)p + tls_vector_offset);
135 g_assert (tls_vector_offset != -1);
137 return (void *) &tsd [key];
141 mono_mach_arch_get_tls_value_from_thread (pthread_t thread, guint32 key)
143 return *(void**)mono_mach_get_tls_address_from_thread (thread, key);
147 mono_mach_init (pthread_key_t key)
150 void *old_value = pthread_getspecific (key);
151 void *canary = (void*)0xDEADBEEFu;
153 pthread_key_create (&key, NULL);
154 g_assert (old_value != canary);
156 pthread_setspecific (key, canary);
158 /*First we probe for cats*/
159 tls_vector_offset = TLS_VECTOR_OFFSET_CATS;
160 if (mono_mach_arch_get_tls_value_from_thread (pthread_self (), key) == canary)
163 tls_vector_offset = TLS_VECTOR_OFFSET_10_9;
164 if (mono_mach_arch_get_tls_value_from_thread (pthread_self (), key) == canary)
167 tls_vector_offset = TLS_VECTOR_OFFSET_10_11;
168 if (mono_mach_arch_get_tls_value_from_thread (pthread_self (), key) == canary)
171 /*Fallback to scanning a large range of offsets*/
172 for (i = TLS_PROBE_LOW_WATERMARK; i <= TLS_PROBE_HIGH_WATERMARK; i += 4) {
173 tls_vector_offset = i;
174 if (mono_mach_arch_get_tls_value_from_thread (pthread_self (), key) == canary) {
175 g_warning ("Found new TLS offset at %d", i);
180 tls_vector_offset = -1;
181 g_warning ("could not discover the mach TLS offset");
183 pthread_setspecific (key, old_value);