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"
20 #include <mono/arch/amd64/amd64-codegen.h>
22 /* Known offsets used for TLS storage*/
24 /* All OSX versions up to 10.8 */
25 #define TLS_VECTOR_OFFSET_CATS 0x48
26 #define TLS_VECTOR_OFFSET_10_9 0xb0
27 #define TLS_VECTOR_OFFSET_10_11 0x100
30 /* This is 2 slots less than the known low */
31 #define TLS_PROBE_LOW_WATERMARK 0x40
32 /* This is 28 slots above the know high, which is more than the known high-low*/
33 #define TLS_PROBE_HIGH_WATERMARK 0x120
36 static int tls_vector_offset;
39 mono_mach_arch_get_ip (thread_state_t state)
41 x86_thread_state32_t *arch_state = (x86_thread_state32_t *) state;
43 return (void *) arch_state->__eip;
47 mono_mach_arch_get_sp (thread_state_t state)
49 x86_thread_state32_t *arch_state = (x86_thread_state32_t *) state;
51 return (void *) arch_state->__esp;
55 mono_mach_arch_get_mcontext_size ()
57 return sizeof (struct __darwin_mcontext32);
61 mono_mach_arch_thread_states_to_mcontext (thread_state_t state, thread_state_t fpstate, void *context)
63 x86_thread_state32_t *arch_state = (x86_thread_state32_t *) state;
64 x86_float_state32_t *arch_fpstate = (x86_float_state32_t *) fpstate;
65 struct __darwin_mcontext32 *ctx = (struct __darwin_mcontext32 *) context;
66 ctx->__ss = *arch_state;
67 ctx->__fs = *arch_fpstate;
71 mono_mach_arch_mcontext_to_thread_states (void *context, thread_state_t state, thread_state_t fpstate)
73 x86_thread_state32_t *arch_state = (x86_thread_state32_t *) state;
74 x86_float_state32_t *arch_fpstate = (x86_float_state32_t *) fpstate;
75 struct __darwin_mcontext32 *ctx = (struct __darwin_mcontext32 *) context;
76 *arch_state = ctx->__ss;
77 *arch_fpstate = ctx->__fs;
81 mono_mach_arch_thread_states_to_mono_context (thread_state_t state, thread_state_t fpstate, MonoContext *context)
83 x86_thread_state32_t *arch_state = (x86_thread_state32_t *) state;
84 x86_float_state32_t *arch_fpstate = (x86_float_state32_t *) state;
85 context->eax = arch_state->__eax;
86 context->ebx = arch_state->__ebx;
87 context->ecx = arch_state->__ecx;
88 context->edx = arch_state->__edx;
89 context->ebp = arch_state->__ebp;
90 context->esp = arch_state->__esp;
91 context->esi = arch_state->__edi;
92 context->edi = arch_state->__esi;
93 context->eip = arch_state->__eip;
94 context->fregs [X86_XMM0] = arch_fpstate->__fpu_xmm0;
95 context->fregs [X86_XMM1] = arch_fpstate->__fpu_xmm1;
96 context->fregs [X86_XMM2] = arch_fpstate->__fpu_xmm2;
97 context->fregs [X86_XMM3] = arch_fpstate->__fpu_xmm3;
98 context->fregs [X86_XMM4] = arch_fpstate->__fpu_xmm4;
99 context->fregs [X86_XMM5] = arch_fpstate->__fpu_xmm5;
100 context->fregs [X86_XMM6] = arch_fpstate->__fpu_xmm6;
101 context->fregs [X86_XMM7] = arch_fpstate->__fpu_xmm7;
105 mono_mach_arch_get_thread_state_size ()
107 return sizeof (x86_thread_state32_t);
111 mono_mach_arch_get_thread_fpstate_size ()
113 return sizeof (x86_float_state32_t);
117 mono_mach_arch_get_thread_states (thread_port_t thread, thread_state_t state, mach_msg_type_number_t *count, thread_state_t fpstate, mach_msg_type_number_t *fpcount)
119 #if defined(HOST_WATCHOS)
120 g_error ("thread_get_state() is not supported by this platform");
122 x86_thread_state32_t *arch_state = (x86_thread_state32_t *) state;
123 x86_float_state32_t *arch_fpstate = (x86_float_state32_t *) fpstate;
126 *count = x86_THREAD_STATE32_COUNT;
127 *fpcount = x86_FLOAT_STATE32_COUNT;
129 ret = thread_get_state (thread, x86_THREAD_STATE32, (thread_state_t)arch_state, count);
130 if (ret != KERN_SUCCESS)
133 ret = thread_get_state (thread, x86_FLOAT_STATE32, (thread_state_t)arch_fpstate, fpcount);
139 mono_mach_arch_set_thread_states (thread_port_t thread, thread_state_t state, mach_msg_type_number_t count, thread_state_t fpstate, mach_msg_type_number_t fpcount)
141 #if defined(HOST_WATCHOS)
142 g_error ("thread_set_state() is not supported by this platform");
145 ret = thread_set_state (thread, x86_THREAD_STATE32, state, count);
146 if (ret != KERN_SUCCESS)
148 ret = thread_set_state (thread, x86_FLOAT_STATE32, fpstate, fpcount);
154 mono_mach_get_tls_address_from_thread (pthread_t thread, pthread_key_t key)
156 /* OSX stores TLS values in a hidden array inside the pthread_t structure
157 * They are keyed off a giant array from a known offset into the pointer. This value
158 * is baked into their pthread_getspecific implementation
160 intptr_t *p = (intptr_t *) thread;
161 intptr_t **tsd = (intptr_t **) ((char*)p + tls_vector_offset);
162 g_assert (tls_vector_offset != -1);
164 return (void *) &tsd [key];
168 mono_mach_arch_get_tls_value_from_thread (pthread_t thread, guint32 key)
170 return *(void**)mono_mach_get_tls_address_from_thread (thread, key);
174 mono_mach_init (pthread_key_t key)
177 void *old_value = pthread_getspecific (key);
178 void *canary = (void*)0xDEADBEEFu;
180 pthread_key_create (&key, NULL);
181 g_assert (old_value != canary);
183 pthread_setspecific (key, canary);
185 /*First we probe for cats*/
186 tls_vector_offset = TLS_VECTOR_OFFSET_CATS;
187 if (mono_mach_arch_get_tls_value_from_thread (pthread_self (), key) == canary)
190 tls_vector_offset = TLS_VECTOR_OFFSET_10_9;
191 if (mono_mach_arch_get_tls_value_from_thread (pthread_self (), key) == canary)
194 tls_vector_offset = TLS_VECTOR_OFFSET_10_11;
195 if (mono_mach_arch_get_tls_value_from_thread (pthread_self (), key) == canary)
198 /*Fallback to scanning a large range of offsets*/
199 for (i = TLS_PROBE_LOW_WATERMARK; i <= TLS_PROBE_HIGH_WATERMARK; i += 4) {
200 tls_vector_offset = i;
201 if (mono_mach_arch_get_tls_value_from_thread (pthread_self (), key) == canary) {
202 g_warning ("Found new TLS offset at %d", i);
207 tls_vector_offset = -1;
208 g_warning ("could not discover the mach TLS offset");
210 pthread_setspecific (key, old_value);