6 * Geoff Norton (gnorton@novell.com)
7 * Rodrigo Kumpera (kumpera@gmail.com)
9 * (C) 2010 Novell, Inc.
10 * (C) 2013 Xamarin, Inc.
19 #include "utils/mono-sigcontext.h"
20 #include "mach-support.h"
23 #include <mono/arch/amd64/amd64-codegen.h>
25 /* Known offsets used for TLS storage*/
27 /* All OSX versions up to 10.8 */
28 #define TLS_VECTOR_OFFSET_CATS 0x60
29 #define TLS_VECTOR_OFFSET_10_9 0xe0
30 #define TLS_VECTOR_OFFSET_10_11 0x100
32 /* This is 2 slots less than the known low */
33 #define TLS_PROBE_LOW_WATERMARK 0x50
34 /* This is 28 slots above the know high, which is more than the known high-low*/
35 #define TLS_PROBE_HIGH_WATERMARK 0x200
38 static int tls_vector_offset;
41 mono_mach_arch_get_ip (thread_state_t state)
43 x86_thread_state64_t *arch_state = (x86_thread_state64_t *) state;
45 return (void *) arch_state->__rip;
49 mono_mach_arch_get_sp (thread_state_t state)
51 x86_thread_state64_t *arch_state = (x86_thread_state64_t *) state;
53 return (void *) arch_state->__rsp;
57 mono_mach_arch_get_mcontext_size ()
59 return sizeof (struct __darwin_mcontext64);
63 mono_mach_arch_thread_states_to_mcontext (thread_state_t state, thread_state_t fpstate, void *context)
65 x86_thread_state64_t *arch_state = (x86_thread_state64_t *) state;
66 x86_float_state64_t *arch_fpstate = (x86_float_state64_t *) fpstate;
67 struct __darwin_mcontext64 *ctx = (struct __darwin_mcontext64 *) context;
68 ctx->__ss = *arch_state;
69 ctx->__fs = *arch_fpstate;
73 mono_mach_arch_mcontext_to_thread_states (void *context, thread_state_t state, thread_state_t fpstate)
75 x86_thread_state64_t *arch_state = (x86_thread_state64_t *) state;
76 x86_float_state64_t *arch_fpstate = (x86_float_state64_t *) fpstate;
77 struct __darwin_mcontext64 *ctx = (struct __darwin_mcontext64 *) context;
78 *arch_state = ctx->__ss;
79 *arch_fpstate = ctx->__fs;
83 mono_mach_arch_thread_states_to_mono_context (thread_state_t state, thread_state_t fpstate, MonoContext *context)
85 x86_thread_state64_t *arch_state = (x86_thread_state64_t *) state;
86 x86_float_state64_t *arch_fpstate = (x86_float_state64_t *) fpstate;
87 context->gregs [AMD64_RAX] = arch_state->__rax;
88 context->gregs [AMD64_RBX] = arch_state->__rbx;
89 context->gregs [AMD64_RCX] = arch_state->__rcx;
90 context->gregs [AMD64_RDX] = arch_state->__rdx;
91 context->gregs [AMD64_RDI] = arch_state->__rdi;
92 context->gregs [AMD64_RBP] = arch_state->__rbp;
93 context->gregs [AMD64_RSP] = arch_state->__rsp;
94 context->gregs [AMD64_R8] = arch_state->__r8;
95 context->gregs [AMD64_R9] = arch_state->__r9;
96 context->gregs [AMD64_R10] = arch_state->__r10;
97 context->gregs [AMD64_R11] = arch_state->__r11;
98 context->gregs [AMD64_R12] = arch_state->__r12;
99 context->gregs [AMD64_R13] = arch_state->__r13;
100 context->gregs [AMD64_R14] = arch_state->__r14;
101 context->gregs [AMD64_R15] = arch_state->__r15;
102 context->gregs [AMD64_RIP] = arch_state->__rip;
103 context->fregs [AMD64_XMM0] = arch_fpstate->__fpu_xmm0;
104 context->fregs [AMD64_XMM1] = arch_fpstate->__fpu_xmm1;
105 context->fregs [AMD64_XMM2] = arch_fpstate->__fpu_xmm2;
106 context->fregs [AMD64_XMM3] = arch_fpstate->__fpu_xmm3;
107 context->fregs [AMD64_XMM4] = arch_fpstate->__fpu_xmm4;
108 context->fregs [AMD64_XMM5] = arch_fpstate->__fpu_xmm5;
109 context->fregs [AMD64_XMM6] = arch_fpstate->__fpu_xmm6;
110 context->fregs [AMD64_XMM7] = arch_fpstate->__fpu_xmm7;
111 context->fregs [AMD64_XMM8] = arch_fpstate->__fpu_xmm8;
112 context->fregs [AMD64_XMM9] = arch_fpstate->__fpu_xmm9;
113 context->fregs [AMD64_XMM10] = arch_fpstate->__fpu_xmm10;
114 context->fregs [AMD64_XMM11] = arch_fpstate->__fpu_xmm11;
115 context->fregs [AMD64_XMM12] = arch_fpstate->__fpu_xmm12;
116 context->fregs [AMD64_XMM13] = arch_fpstate->__fpu_xmm13;
117 context->fregs [AMD64_XMM14] = arch_fpstate->__fpu_xmm14;
118 context->fregs [AMD64_XMM15] = arch_fpstate->__fpu_xmm15;
122 mono_mach_arch_get_thread_state_size ()
124 return sizeof (x86_thread_state64_t);
128 mono_mach_arch_get_thread_fpstate_size ()
130 return sizeof (x86_float_state64_t);
134 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)
136 x86_thread_state64_t *arch_state = (x86_thread_state64_t *)state;
137 x86_float_state64_t *arch_fpstate = (x86_float_state64_t *)fpstate;
140 *count = x86_THREAD_STATE64_COUNT;
141 *fpcount = x86_FLOAT_STATE64_COUNT;
143 ret = thread_get_state (thread, x86_THREAD_STATE64, (thread_state_t)arch_state, count);
144 if (ret != KERN_SUCCESS)
147 ret = thread_get_state (thread, x86_FLOAT_STATE64, (thread_state_t)arch_fpstate, fpcount);
152 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)
155 ret = thread_set_state (thread, x86_THREAD_STATE64, state, count);
156 if (ret != KERN_SUCCESS)
158 ret = thread_set_state (thread, x86_FLOAT_STATE64, fpstate, fpcount);
163 mono_mach_get_tls_address_from_thread (pthread_t thread, pthread_key_t key)
165 /* OSX stores TLS values in a hidden array inside the pthread_t structure
166 * They are keyed off a giant array from a known offset into the pointer. This value
167 * is baked into their pthread_getspecific implementation
169 intptr_t *p = (intptr_t *)thread;
170 intptr_t **tsd = (intptr_t **) ((char*)p + tls_vector_offset);
171 g_assert (tls_vector_offset != -1);
173 return (void *) &tsd [key];
177 mono_mach_arch_get_tls_value_from_thread (pthread_t thread, guint32 key)
179 return *(void**)mono_mach_get_tls_address_from_thread (thread, key);
183 mono_mach_init (pthread_key_t key)
186 void *old_value = pthread_getspecific (key);
187 void *canary = (void*)0xDEADBEEFu;
189 pthread_key_create (&key, NULL);
190 g_assert (old_value != canary);
192 pthread_setspecific (key, canary);
194 /*First we probe for cats*/
195 tls_vector_offset = TLS_VECTOR_OFFSET_CATS;
196 if (mono_mach_arch_get_tls_value_from_thread (pthread_self (), key) == canary)
199 tls_vector_offset = TLS_VECTOR_OFFSET_10_9;
200 if (mono_mach_arch_get_tls_value_from_thread (pthread_self (), key) == canary)
203 tls_vector_offset = TLS_VECTOR_OFFSET_10_11;
204 if (mono_mach_arch_get_tls_value_from_thread (pthread_self (), key) == canary)
207 /*Fallback to scanning a large range of offsets*/
208 for (i = TLS_PROBE_LOW_WATERMARK; i <= TLS_PROBE_HIGH_WATERMARK; i += 4) {
209 tls_vector_offset = i;
210 if (mono_mach_arch_get_tls_value_from_thread (pthread_self (), key) == canary) {
211 g_warning ("Found new TLS offset at %d", i);
216 tls_vector_offset = -1;
217 g_warning ("could not discover the mach TLS offset");
219 pthread_setspecific (key, old_value);