733f94c343cf1c93b90f3e769fbc0f12c0610e40
[mono.git] / mono / utils / mach-support-amd64.c
1 /**
2  * \file
3  * mach support for x86
4  *
5  * Authors:
6  *   Geoff Norton (gnorton@novell.com)
7  *   Rodrigo Kumpera (kumpera@gmail.com)
8  *
9  * (C) 2010 Novell, Inc.
10  * (C) 2013 Xamarin, Inc.
11  */
12
13 #include <config.h>
14
15 #if defined(__MACH__)
16 #include <stdint.h>
17 #include <glib.h>
18 #include <pthread.h>
19 #include "utils/mono-sigcontext.h"
20 #include "mach-support.h"
21
22 //For reg numbers
23 #include <mono/arch/amd64/amd64-codegen.h>
24
25 /* Known offsets used for TLS storage*/
26
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
31
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
36
37
38 static int tls_vector_offset;
39
40 void *
41 mono_mach_arch_get_ip (thread_state_t state)
42 {
43         x86_thread_state64_t *arch_state = (x86_thread_state64_t *) state;
44
45         return (void *) arch_state->__rip;
46 }
47
48 void *
49 mono_mach_arch_get_sp (thread_state_t state)
50 {
51         x86_thread_state64_t *arch_state = (x86_thread_state64_t *) state;
52
53         return (void *) arch_state->__rsp;
54 }
55
56 int
57 mono_mach_arch_get_mcontext_size ()
58 {
59         return sizeof (struct __darwin_mcontext64);
60 }
61
62 void
63 mono_mach_arch_thread_states_to_mcontext (thread_state_t state, thread_state_t fpstate, void *context)
64 {
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;
70 }
71
72 void
73 mono_mach_arch_mcontext_to_thread_states (void *context, thread_state_t state, thread_state_t fpstate)
74 {
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;
80 }
81
82 void
83 mono_mach_arch_thread_states_to_mono_context (thread_state_t state, thread_state_t fpstate, MonoContext *context)
84 {
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;
119 }
120
121 int
122 mono_mach_arch_get_thread_state_size ()
123 {
124         return sizeof (x86_thread_state64_t);
125 }
126
127 int
128 mono_mach_arch_get_thread_fpstate_size ()
129 {
130         return sizeof (x86_float_state64_t);
131 }
132
133 kern_return_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)
135 {
136         x86_thread_state64_t *arch_state = (x86_thread_state64_t *)state;
137         x86_float_state64_t *arch_fpstate = (x86_float_state64_t *)fpstate;
138         kern_return_t ret;
139
140         *count = x86_THREAD_STATE64_COUNT;
141         *fpcount = x86_FLOAT_STATE64_COUNT;
142
143         ret = thread_get_state (thread, x86_THREAD_STATE64, (thread_state_t)arch_state, count);
144         if (ret != KERN_SUCCESS)
145                 return ret;
146
147         ret = thread_get_state (thread, x86_FLOAT_STATE64, (thread_state_t)arch_fpstate, fpcount);
148         return ret;
149 }
150
151 kern_return_t
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)
153 {
154         kern_return_t ret;
155         ret = thread_set_state (thread, x86_THREAD_STATE64, state, count);
156         if (ret != KERN_SUCCESS)
157                 return ret;
158         ret = thread_set_state (thread, x86_FLOAT_STATE64, fpstate, fpcount);
159         return ret;
160 }
161
162 void *
163 mono_mach_get_tls_address_from_thread (pthread_t thread, pthread_key_t key)
164 {
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
168          */
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);
172
173         return (void *) &tsd [key];
174 }
175
176 void *
177 mono_mach_arch_get_tls_value_from_thread (pthread_t thread, guint32 key)
178 {
179         return *(void**)mono_mach_get_tls_address_from_thread (thread, key);
180 }
181
182 void
183 mono_mach_init (pthread_key_t key)
184 {
185         int i;
186         void *old_value = pthread_getspecific (key);
187         void *canary = (void*)0xDEADBEEFu;
188
189         pthread_key_create (&key, NULL);
190         g_assert (old_value != canary);
191
192         pthread_setspecific (key, canary);
193
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)
197                 goto ok;
198
199         tls_vector_offset = TLS_VECTOR_OFFSET_10_9;
200         if (mono_mach_arch_get_tls_value_from_thread (pthread_self (), key) == canary)
201                 goto ok;
202
203         tls_vector_offset = TLS_VECTOR_OFFSET_10_11;
204         if (mono_mach_arch_get_tls_value_from_thread (pthread_self (), key) == canary)
205                 goto ok;
206
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);
212                         goto ok;
213                 }
214         }
215
216         tls_vector_offset = -1;
217         g_warning ("could not discover the mach TLS offset");
218 ok:
219         pthread_setspecific (key, old_value);
220 }
221
222 #endif