Merge pull request #2092 from kasthack/system-web-stuff-import
[mono.git] / mono / utils / mach-support-amd64.c
1 /*
2  * mach-support-x86.c: mach support for x86
3  *
4  * Authors:
5  *   Geoff Norton (gnorton@novell.com)
6  *   Rodrigo Kumpera (kumpera@gmail.com)
7  *
8  * (C) 2010 Novell, Inc.
9  * (C) 2013 Xamarin, Inc.
10  */
11
12 #include <config.h>
13
14 #if defined(__MACH__)
15 #include <stdint.h>
16 #include <glib.h>
17 #include <pthread.h>
18 #include "utils/mono-sigcontext.h"
19 #include "mach-support.h"
20
21 //For reg numbers
22 #include <mono/arch/amd64/amd64-codegen.h>
23
24 /* Known offsets used for TLS storage*/
25
26 /* All OSX versions up to 10.8 */
27 #define TLS_VECTOR_OFFSET_CATS 0x60
28 #define TLS_VECTOR_OFFSET_10_9 0xe0
29 #define TLS_VECTOR_OFFSET_10_11 0x100
30
31 /* This is 2 slots less than the known low */
32 #define TLS_PROBE_LOW_WATERMARK 0x50
33 /* This is 28 slots above the know high, which is more than the known high-low*/
34 #define TLS_PROBE_HIGH_WATERMARK 0x200
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_state64_t *arch_state = (x86_thread_state64_t *) state;
43
44         return (void *) arch_state->__rip;
45 }
46
47 void *
48 mono_mach_arch_get_sp (thread_state_t state)
49 {
50         x86_thread_state64_t *arch_state = (x86_thread_state64_t *) state;
51
52         return (void *) arch_state->__rsp;
53 }
54
55 int
56 mono_mach_arch_get_mcontext_size ()
57 {
58         return sizeof (struct __darwin_mcontext64);
59 }
60
61 void
62 mono_mach_arch_thread_state_to_mcontext (thread_state_t state, void *context)
63 {
64         x86_thread_state64_t *arch_state = (x86_thread_state64_t *) state;
65         struct __darwin_mcontext64 *ctx = (struct __darwin_mcontext64 *) context;
66
67         ctx->__ss = *arch_state;
68 }
69
70 void
71 mono_mach_arch_mcontext_to_thread_state (void *context, thread_state_t state)
72 {
73         x86_thread_state64_t *arch_state = (x86_thread_state64_t *) state;
74         struct __darwin_mcontext64 *ctx = (struct __darwin_mcontext64 *) context;
75
76         *arch_state = ctx->__ss;
77 }
78
79 void
80 mono_mach_arch_thread_state_to_mono_context (thread_state_t state, MonoContext *context)
81 {
82         x86_thread_state64_t *arch_state = (x86_thread_state64_t *) state;
83         context->gregs [AMD64_RAX] = arch_state->__rax;
84         context->gregs [AMD64_RBX] = arch_state->__rbx;
85         context->gregs [AMD64_RCX] = arch_state->__rcx;
86         context->gregs [AMD64_RDX] = arch_state->__rdx;
87         context->gregs [AMD64_RDI] = arch_state->__rdi;
88         context->gregs [AMD64_RBP] = arch_state->__rbp;
89         context->gregs [AMD64_RSP] = arch_state->__rsp;
90         context->gregs [AMD64_R8] = arch_state->__r8;
91         context->gregs [AMD64_R9] = arch_state->__r9;
92         context->gregs [AMD64_R10] = arch_state->__r10;
93         context->gregs [AMD64_R11] = arch_state->__r11;
94         context->gregs [AMD64_R12] = arch_state->__r12;
95         context->gregs [AMD64_R13] = arch_state->__r13;
96         context->gregs [AMD64_R14] = arch_state->__r14;
97         context->gregs [AMD64_R15] = arch_state->__r15;
98         context->gregs [AMD64_RIP] = arch_state->__rip;
99 }
100
101 int
102 mono_mach_arch_get_thread_state_size ()
103 {
104         return sizeof (x86_thread_state64_t);
105 }
106
107 kern_return_t
108 mono_mach_arch_get_thread_state (thread_port_t thread, thread_state_t state, mach_msg_type_number_t *count)
109 {
110         x86_thread_state64_t *arch_state = (x86_thread_state64_t *) state;
111         kern_return_t ret;
112
113         *count = x86_THREAD_STATE64_COUNT;
114
115         ret = thread_get_state (thread, x86_THREAD_STATE64, (thread_state_t) arch_state, count);
116
117         return ret;
118 }
119
120 kern_return_t
121 mono_mach_arch_set_thread_state (thread_port_t thread, thread_state_t state, mach_msg_type_number_t count)
122 {
123         return thread_set_state (thread, x86_THREAD_STATE64, state, count);
124 }
125
126 void *
127 mono_mach_get_tls_address_from_thread (pthread_t thread, pthread_key_t key)
128 {
129         /* OSX stores TLS values in a hidden array inside the pthread_t structure
130          * They are keyed off a giant array from a known offset into the pointer.  This value
131          * is baked into their pthread_getspecific implementation
132          */
133         intptr_t *p = (intptr_t *)thread;
134         intptr_t **tsd = (intptr_t **) ((char*)p + tls_vector_offset);
135         g_assert (tls_vector_offset != -1);
136
137         return (void *) &tsd [key];
138 }
139
140 void *
141 mono_mach_arch_get_tls_value_from_thread (pthread_t thread, guint32 key)
142 {
143         return *(void**)mono_mach_get_tls_address_from_thread (thread, key);
144 }
145
146 void
147 mono_mach_init (pthread_key_t key)
148 {
149         int i;
150         void *old_value = pthread_getspecific (key);
151         void *canary = (void*)0xDEADBEEFu;
152
153         pthread_key_create (&key, NULL);
154         g_assert (old_value != canary);
155
156         pthread_setspecific (key, canary);
157
158         /*First we probe for cats*/
159         tls_vector_offset = TLS_VECTOR_OFFSET_CATS;
160         if (mono_mach_arch_get_tls_value_from_thread (pthread_self (), key) == canary)
161                 goto ok;
162
163         tls_vector_offset = TLS_VECTOR_OFFSET_10_9;
164         if (mono_mach_arch_get_tls_value_from_thread (pthread_self (), key) == canary)
165                 goto ok;
166
167         tls_vector_offset = TLS_VECTOR_OFFSET_10_11;
168         if (mono_mach_arch_get_tls_value_from_thread (pthread_self (), key) == canary)
169                 goto ok;
170
171         /*Fallback to scanning a large range of offsets*/
172         for (i = TLS_PROBE_LOW_WATERMARK; i <= TLS_PROBE_HIGH_WATERMARK; i += 4) {
173                 tls_vector_offset = i;
174                 if (mono_mach_arch_get_tls_value_from_thread (pthread_self (), key) == canary) {
175                         g_warning ("Found new TLS offset at %d", i);
176                         goto ok;
177                 }
178         }
179
180         tls_vector_offset = -1;
181         g_warning ("could not discover the mach TLS offset");
182 ok:
183         pthread_setspecific (key, old_value);
184 }
185
186 #endif