[bcl] Enable tests for the monodroid profile.
[mono.git] / mono / utils / mach-support-x86.c
1 /*
2  * mach-support-x86.c: mach support for x86
3  *
4  * Authors:
5  *   Geoff Norton (gnorton@novell.com)
6  *
7  * (C) 2010 Novell, Inc.
8  */
9
10 #include <config.h>
11
12 #if defined(__MACH__)
13 #include <stdint.h>
14 #include <glib.h>
15 #include <pthread.h>
16 #include "utils/mono-sigcontext.h"
17 #include "mach-support.h"
18
19 // For reg numbers
20 #include <mono/arch/amd64/amd64-codegen.h>
21
22 /* Known offsets used for TLS storage*/
23
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
28
29
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
34
35
36 static int tls_vector_offset;
37
38 void *
39 mono_mach_arch_get_ip (thread_state_t state)
40 {
41         x86_thread_state32_t *arch_state = (x86_thread_state32_t *) state;
42
43         return (void *) arch_state->__eip;
44 }
45
46 void *
47 mono_mach_arch_get_sp (thread_state_t state)
48 {
49         x86_thread_state32_t *arch_state = (x86_thread_state32_t *) state;
50
51         return (void *) arch_state->__esp;
52 }
53
54 int
55 mono_mach_arch_get_mcontext_size ()
56 {
57         return sizeof (struct __darwin_mcontext32);
58 }
59
60 void
61 mono_mach_arch_thread_states_to_mcontext (thread_state_t state, thread_state_t fpstate, void *context)
62 {
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;
68 }
69
70 void
71 mono_mach_arch_mcontext_to_thread_states (void *context, thread_state_t state, thread_state_t fpstate)
72 {
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;
78 }
79
80 void
81 mono_mach_arch_thread_states_to_mono_context (thread_state_t state, thread_state_t fpstate, MonoContext *context)
82 {
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;
102 }
103
104 int
105 mono_mach_arch_get_thread_state_size ()
106 {
107         return sizeof (x86_thread_state32_t);
108 }
109
110 int
111 mono_mach_arch_get_thread_fpstate_size ()
112 {
113         return sizeof (x86_float_state32_t);
114 }
115
116 kern_return_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)
118 {
119 #if defined(HOST_WATCHOS)
120         g_error ("thread_get_state() is not supported by this platform");
121 #else
122         x86_thread_state32_t *arch_state = (x86_thread_state32_t *) state;
123         x86_float_state32_t *arch_fpstate = (x86_float_state32_t *) fpstate;
124         kern_return_t ret;
125
126         *count = x86_THREAD_STATE32_COUNT;
127         *fpcount = x86_FLOAT_STATE32_COUNT;
128
129         ret = thread_get_state (thread, x86_THREAD_STATE32, (thread_state_t)arch_state, count);
130         if (ret != KERN_SUCCESS)
131                 return ret;
132
133         ret = thread_get_state (thread, x86_FLOAT_STATE32, (thread_state_t)arch_fpstate, fpcount);
134         return ret;
135 #endif
136 }
137
138 kern_return_t
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)
140 {
141 #if defined(HOST_WATCHOS)
142         g_error ("thread_set_state() is not supported by this platform");
143 #else
144         kern_return_t ret;
145         ret = thread_set_state (thread, x86_THREAD_STATE32, state, count);
146         if (ret != KERN_SUCCESS)
147                 return ret;
148         ret = thread_set_state (thread, x86_FLOAT_STATE32, fpstate, fpcount);
149         return ret;
150 #endif  
151 }
152
153 void *
154 mono_mach_get_tls_address_from_thread (pthread_t thread, pthread_key_t key)
155 {
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
159          */
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);
163
164         return (void *) &tsd [key];     
165 }
166
167 void *
168 mono_mach_arch_get_tls_value_from_thread (pthread_t thread, guint32 key)
169 {
170         return *(void**)mono_mach_get_tls_address_from_thread (thread, key);
171 }
172
173 void
174 mono_mach_init (pthread_key_t key)
175 {
176         int i;
177         void *old_value = pthread_getspecific (key);
178         void *canary = (void*)0xDEADBEEFu;
179
180         pthread_key_create (&key, NULL);
181         g_assert (old_value != canary);
182
183         pthread_setspecific (key, canary);
184
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)
188                 goto ok;
189
190         tls_vector_offset = TLS_VECTOR_OFFSET_10_9;
191         if (mono_mach_arch_get_tls_value_from_thread (pthread_self (), key) == canary)
192                 goto ok;
193
194         tls_vector_offset = TLS_VECTOR_OFFSET_10_11;
195         if (mono_mach_arch_get_tls_value_from_thread (pthread_self (), key) == canary)
196                 goto ok;
197
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);
203                         goto ok;
204                 }
205         }
206
207         tls_vector_offset = -1;
208         g_warning ("could not discover the mach TLS offset");
209 ok:
210         pthread_setspecific (key, old_value);
211 }
212
213 #endif