[amd64] Save missing register
[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_RSI] = arch_state->__rsi;
93         context->gregs [AMD64_RBP] = arch_state->__rbp;
94         context->gregs [AMD64_RSP] = arch_state->__rsp;
95         context->gregs [AMD64_R8] = arch_state->__r8;
96         context->gregs [AMD64_R9] = arch_state->__r9;
97         context->gregs [AMD64_R10] = arch_state->__r10;
98         context->gregs [AMD64_R11] = arch_state->__r11;
99         context->gregs [AMD64_R12] = arch_state->__r12;
100         context->gregs [AMD64_R13] = arch_state->__r13;
101         context->gregs [AMD64_R14] = arch_state->__r14;
102         context->gregs [AMD64_R15] = arch_state->__r15;
103         context->gregs [AMD64_RIP] = arch_state->__rip;
104         context->fregs [AMD64_XMM0] = arch_fpstate->__fpu_xmm0;
105         context->fregs [AMD64_XMM1] = arch_fpstate->__fpu_xmm1;
106         context->fregs [AMD64_XMM2] = arch_fpstate->__fpu_xmm2;
107         context->fregs [AMD64_XMM3] = arch_fpstate->__fpu_xmm3;
108         context->fregs [AMD64_XMM4] = arch_fpstate->__fpu_xmm4;
109         context->fregs [AMD64_XMM5] = arch_fpstate->__fpu_xmm5;
110         context->fregs [AMD64_XMM6] = arch_fpstate->__fpu_xmm6;
111         context->fregs [AMD64_XMM7] = arch_fpstate->__fpu_xmm7;
112         context->fregs [AMD64_XMM8] = arch_fpstate->__fpu_xmm8;
113         context->fregs [AMD64_XMM9] = arch_fpstate->__fpu_xmm9;
114         context->fregs [AMD64_XMM10] = arch_fpstate->__fpu_xmm10;
115         context->fregs [AMD64_XMM11] = arch_fpstate->__fpu_xmm11;
116         context->fregs [AMD64_XMM12] = arch_fpstate->__fpu_xmm12;
117         context->fregs [AMD64_XMM13] = arch_fpstate->__fpu_xmm13;
118         context->fregs [AMD64_XMM14] = arch_fpstate->__fpu_xmm14;
119         context->fregs [AMD64_XMM15] = arch_fpstate->__fpu_xmm15;
120 }
121
122 int
123 mono_mach_arch_get_thread_state_size ()
124 {
125         return sizeof (x86_thread_state64_t);
126 }
127
128 int
129 mono_mach_arch_get_thread_fpstate_size ()
130 {
131         return sizeof (x86_float_state64_t);
132 }
133
134 kern_return_t
135 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 {
137         x86_thread_state64_t *arch_state = (x86_thread_state64_t *)state;
138         x86_float_state64_t *arch_fpstate = (x86_float_state64_t *)fpstate;
139         kern_return_t ret;
140
141         *count = x86_THREAD_STATE64_COUNT;
142         *fpcount = x86_FLOAT_STATE64_COUNT;
143
144         ret = thread_get_state (thread, x86_THREAD_STATE64, (thread_state_t)arch_state, count);
145         if (ret != KERN_SUCCESS)
146                 return ret;
147
148         ret = thread_get_state (thread, x86_FLOAT_STATE64, (thread_state_t)arch_fpstate, fpcount);
149         return ret;
150 }
151
152 kern_return_t
153 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)
154 {
155         kern_return_t ret;
156         ret = thread_set_state (thread, x86_THREAD_STATE64, state, count);
157         if (ret != KERN_SUCCESS)
158                 return ret;
159         ret = thread_set_state (thread, x86_FLOAT_STATE64, fpstate, fpcount);
160         return ret;
161 }
162
163 void *
164 mono_mach_get_tls_address_from_thread (pthread_t thread, pthread_key_t key)
165 {
166         /* OSX stores TLS values in a hidden array inside the pthread_t structure
167          * They are keyed off a giant array from a known offset into the pointer.  This value
168          * is baked into their pthread_getspecific implementation
169          */
170         intptr_t *p = (intptr_t *)thread;
171         intptr_t **tsd = (intptr_t **) ((char*)p + tls_vector_offset);
172         g_assert (tls_vector_offset != -1);
173
174         return (void *) &tsd [key];
175 }
176
177 void *
178 mono_mach_arch_get_tls_value_from_thread (pthread_t thread, guint32 key)
179 {
180         return *(void**)mono_mach_get_tls_address_from_thread (thread, key);
181 }
182
183 void
184 mono_mach_init (pthread_key_t key)
185 {
186         int i;
187         void *old_value = pthread_getspecific (key);
188         void *canary = (void*)0xDEADBEEFu;
189
190         pthread_key_create (&key, NULL);
191         g_assert (old_value != canary);
192
193         pthread_setspecific (key, canary);
194
195         /*First we probe for cats*/
196         tls_vector_offset = TLS_VECTOR_OFFSET_CATS;
197         if (mono_mach_arch_get_tls_value_from_thread (pthread_self (), key) == canary)
198                 goto ok;
199
200         tls_vector_offset = TLS_VECTOR_OFFSET_10_9;
201         if (mono_mach_arch_get_tls_value_from_thread (pthread_self (), key) == canary)
202                 goto ok;
203
204         tls_vector_offset = TLS_VECTOR_OFFSET_10_11;
205         if (mono_mach_arch_get_tls_value_from_thread (pthread_self (), key) == canary)
206                 goto ok;
207
208         /*Fallback to scanning a large range of offsets*/
209         for (i = TLS_PROBE_LOW_WATERMARK; i <= TLS_PROBE_HIGH_WATERMARK; i += 4) {
210                 tls_vector_offset = i;
211                 if (mono_mach_arch_get_tls_value_from_thread (pthread_self (), key) == canary) {
212                         g_warning ("Found new TLS offset at %d", i);
213                         goto ok;
214                 }
215         }
216
217         tls_vector_offset = -1;
218         g_warning ("could not discover the mach TLS offset");
219 ok:
220         pthread_setspecific (key, old_value);
221 }
222
223 #endif