[w32file] Remove dead code
[mono.git] / mono / utils / mach-support-x86.c
1 /**
2  * \file
3  * mach support for x86
4  *
5  * Authors:
6  *   Geoff Norton (gnorton@novell.com)
7  *
8  * (C) 2010 Novell, Inc.
9  */
10
11 #include <config.h>
12
13 #if defined(__MACH__)
14 #include <stdint.h>
15 #include <glib.h>
16 #include <pthread.h>
17 #include "utils/mono-sigcontext.h"
18 #include "mach-support.h"
19
20 // For reg numbers
21 #include <mono/arch/amd64/amd64-codegen.h>
22
23 /* Known offsets used for TLS storage*/
24
25 /* All OSX versions up to 10.8 */
26 #define TLS_VECTOR_OFFSET_CATS 0x48
27 #define TLS_VECTOR_OFFSET_10_9 0xb0
28 #define TLS_VECTOR_OFFSET_10_11 0x100
29
30
31 /* This is 2 slots less than the known low */
32 #define TLS_PROBE_LOW_WATERMARK 0x40
33 /* This is 28 slots above the know high, which is more than the known high-low*/
34 #define TLS_PROBE_HIGH_WATERMARK 0x120
35
36
37 static int tls_vector_offset;
38
39 void *
40 mono_mach_arch_get_ip (thread_state_t state)
41 {
42         x86_thread_state32_t *arch_state = (x86_thread_state32_t *) state;
43
44         return (void *) arch_state->__eip;
45 }
46
47 void *
48 mono_mach_arch_get_sp (thread_state_t state)
49 {
50         x86_thread_state32_t *arch_state = (x86_thread_state32_t *) state;
51
52         return (void *) arch_state->__esp;
53 }
54
55 int
56 mono_mach_arch_get_mcontext_size ()
57 {
58         return sizeof (struct __darwin_mcontext32);
59 }
60
61 void
62 mono_mach_arch_thread_states_to_mcontext (thread_state_t state, thread_state_t fpstate, void *context)
63 {
64         x86_thread_state32_t *arch_state = (x86_thread_state32_t *) state;
65         x86_float_state32_t *arch_fpstate = (x86_float_state32_t *) fpstate;
66         struct __darwin_mcontext32 *ctx = (struct __darwin_mcontext32 *) context;
67         ctx->__ss = *arch_state;
68         ctx->__fs = *arch_fpstate;
69 }
70
71 void
72 mono_mach_arch_mcontext_to_thread_states (void *context, thread_state_t state, thread_state_t fpstate)
73 {
74         x86_thread_state32_t *arch_state = (x86_thread_state32_t *) state;
75         x86_float_state32_t *arch_fpstate = (x86_float_state32_t *) fpstate;
76         struct __darwin_mcontext32 *ctx = (struct __darwin_mcontext32 *) context;
77         *arch_state = ctx->__ss;
78         *arch_fpstate = ctx->__fs;
79 }
80
81 void
82 mono_mach_arch_thread_states_to_mono_context (thread_state_t state, thread_state_t fpstate, MonoContext *context)
83 {
84         x86_thread_state32_t *arch_state = (x86_thread_state32_t *) state;
85         x86_float_state32_t *arch_fpstate = (x86_float_state32_t *) state;
86         context->eax = arch_state->__eax;
87         context->ebx = arch_state->__ebx;
88         context->ecx = arch_state->__ecx;
89         context->edx = arch_state->__edx;
90         context->ebp = arch_state->__ebp;
91         context->esp = arch_state->__esp;
92         context->esi = arch_state->__edi;
93         context->edi = arch_state->__esi;
94         context->eip = arch_state->__eip;
95         context->fregs [X86_XMM0] = arch_fpstate->__fpu_xmm0;
96         context->fregs [X86_XMM1] = arch_fpstate->__fpu_xmm1;
97         context->fregs [X86_XMM2] = arch_fpstate->__fpu_xmm2;
98         context->fregs [X86_XMM3] = arch_fpstate->__fpu_xmm3;
99         context->fregs [X86_XMM4] = arch_fpstate->__fpu_xmm4;
100         context->fregs [X86_XMM5] = arch_fpstate->__fpu_xmm5;
101         context->fregs [X86_XMM6] = arch_fpstate->__fpu_xmm6;
102         context->fregs [X86_XMM7] = arch_fpstate->__fpu_xmm7;
103 }
104
105 int
106 mono_mach_arch_get_thread_state_size ()
107 {
108         return sizeof (x86_thread_state32_t);
109 }
110
111 int
112 mono_mach_arch_get_thread_fpstate_size ()
113 {
114         return sizeof (x86_float_state32_t);
115 }
116
117 kern_return_t
118 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 {
120 #if defined(HOST_WATCHOS)
121         g_error ("thread_get_state() is not supported by this platform");
122 #else
123         x86_thread_state32_t *arch_state = (x86_thread_state32_t *) state;
124         x86_float_state32_t *arch_fpstate = (x86_float_state32_t *) fpstate;
125         kern_return_t ret;
126
127         *count = x86_THREAD_STATE32_COUNT;
128         *fpcount = x86_FLOAT_STATE32_COUNT;
129
130         ret = thread_get_state (thread, x86_THREAD_STATE32, (thread_state_t)arch_state, count);
131         if (ret != KERN_SUCCESS)
132                 return ret;
133
134         ret = thread_get_state (thread, x86_FLOAT_STATE32, (thread_state_t)arch_fpstate, fpcount);
135         return ret;
136 #endif
137 }
138
139 kern_return_t
140 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 {
142 #if defined(HOST_WATCHOS)
143         g_error ("thread_set_state() is not supported by this platform");
144 #else
145         kern_return_t ret;
146         ret = thread_set_state (thread, x86_THREAD_STATE32, state, count);
147         if (ret != KERN_SUCCESS)
148                 return ret;
149         ret = thread_set_state (thread, x86_FLOAT_STATE32, fpstate, fpcount);
150         return ret;
151 #endif  
152 }
153
154 void *
155 mono_mach_get_tls_address_from_thread (pthread_t thread, pthread_key_t key)
156 {
157         /* OSX stores TLS values in a hidden array inside the pthread_t structure
158          * They are keyed off a giant array from a known offset into the pointer.  This value
159          * is baked into their pthread_getspecific implementation
160          */
161         intptr_t *p = (intptr_t *) thread;
162         intptr_t **tsd = (intptr_t **) ((char*)p + tls_vector_offset);
163         g_assert (tls_vector_offset != -1);
164
165         return (void *) &tsd [key];     
166 }
167
168 void *
169 mono_mach_arch_get_tls_value_from_thread (pthread_t thread, guint32 key)
170 {
171         return *(void**)mono_mach_get_tls_address_from_thread (thread, key);
172 }
173
174 void
175 mono_mach_init (pthread_key_t key)
176 {
177         int i;
178         void *old_value = pthread_getspecific (key);
179         void *canary = (void*)0xDEADBEEFu;
180
181         pthread_key_create (&key, NULL);
182         g_assert (old_value != canary);
183
184         pthread_setspecific (key, canary);
185
186         /*First we probe for cats*/
187         tls_vector_offset = TLS_VECTOR_OFFSET_CATS;
188         if (mono_mach_arch_get_tls_value_from_thread (pthread_self (), key) == canary)
189                 goto ok;
190
191         tls_vector_offset = TLS_VECTOR_OFFSET_10_9;
192         if (mono_mach_arch_get_tls_value_from_thread (pthread_self (), key) == canary)
193                 goto ok;
194
195         tls_vector_offset = TLS_VECTOR_OFFSET_10_11;
196         if (mono_mach_arch_get_tls_value_from_thread (pthread_self (), key) == canary)
197                 goto ok;
198
199         /*Fallback to scanning a large range of offsets*/
200         for (i = TLS_PROBE_LOW_WATERMARK; i <= TLS_PROBE_HIGH_WATERMARK; i += 4) {
201                 tls_vector_offset = i;
202                 if (mono_mach_arch_get_tls_value_from_thread (pthread_self (), key) == canary) {
203                         g_warning ("Found new TLS offset at %d", i);
204                         goto ok;
205                 }
206         }
207
208         tls_vector_offset = -1;
209         g_warning ("could not discover the mach TLS offset");
210 ok:
211         pthread_setspecific (key, old_value);
212 }
213
214 #endif