Merge pull request #757 from mlintner/master
[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 /* Known offsets used for TLS storage*/
22
23 /* All OSX versions up to 10.8 */
24 #define TLS_VECTOR_OFFSET_CATS 0x60
25 #define TLS_VECTOR_OFFSET_10_9 0xe0
26 #define TLS_VECTOR_OFFSET_10_11 0x100
27
28 /* This is 2 slots less than the known low */
29 #define TLS_PROBE_LOW_WATERMARK 0x50
30 /* This is 28 slots above the know high, which is more than the known high-low*/
31 #define TLS_PROBE_HIGH_WATERMARK 0x200
32
33
34 static int tls_vector_offset;
35
36 void *
37 mono_mach_arch_get_ip (thread_state_t state)
38 {
39         x86_thread_state64_t *arch_state = (x86_thread_state64_t *) state;
40
41         return (void *) arch_state->__rip;
42 }
43
44 void *
45 mono_mach_arch_get_sp (thread_state_t state)
46 {
47         x86_thread_state64_t *arch_state = (x86_thread_state64_t *) state;
48
49         return (void *) arch_state->__rsp;
50 }
51
52 int
53 mono_mach_arch_get_mcontext_size ()
54 {
55         return sizeof (struct __darwin_mcontext64);
56 }
57
58 void
59 mono_mach_arch_thread_state_to_mcontext (thread_state_t state, void *context)
60 {
61         x86_thread_state64_t *arch_state = (x86_thread_state64_t *) state;
62         struct __darwin_mcontext64 *ctx = (struct __darwin_mcontext64 *) context;
63
64         ctx->__ss = *arch_state;
65 }
66
67 void
68 mono_mach_arch_mcontext_to_thread_state (void *context, thread_state_t state)
69 {
70         x86_thread_state64_t *arch_state = (x86_thread_state64_t *) state;
71         struct __darwin_mcontext64 *ctx = (struct __darwin_mcontext64 *) context;
72
73         *arch_state = ctx->__ss;
74 }
75
76 int
77 mono_mach_arch_get_thread_state_size ()
78 {
79         return sizeof (x86_thread_state64_t);
80 }
81
82 kern_return_t
83 mono_mach_arch_get_thread_state (thread_port_t thread, thread_state_t state, mach_msg_type_number_t *count)
84 {
85         x86_thread_state64_t *arch_state = (x86_thread_state64_t *) state;
86         kern_return_t ret;
87
88         *count = x86_THREAD_STATE64_COUNT;
89
90         ret = thread_get_state (thread, x86_THREAD_STATE64, (thread_state_t) arch_state, count);
91
92         return ret;
93 }
94
95 kern_return_t
96 mono_mach_arch_set_thread_state (thread_port_t thread, thread_state_t state, mach_msg_type_number_t count)
97 {
98         return thread_set_state (thread, x86_THREAD_STATE64, state, count);
99 }
100
101 void *
102 mono_mach_get_tls_address_from_thread (pthread_t thread, pthread_key_t key)
103 {
104         /* OSX stores TLS values in a hidden array inside the pthread_t structure
105          * They are keyed off a giant array from a known offset into the pointer.  This value
106          * is baked into their pthread_getspecific implementation
107          */
108         intptr_t *p = (intptr_t *)thread;
109         intptr_t **tsd = (intptr_t **) ((char*)p + tls_vector_offset);
110         g_assert (tls_vector_offset != -1);
111
112         return (void *) &tsd [key];
113 }
114
115 void *
116 mono_mach_arch_get_tls_value_from_thread (pthread_t thread, guint32 key)
117 {
118         return *(void**)mono_mach_get_tls_address_from_thread (thread, key);
119 }
120
121 void
122 mono_mach_init (pthread_key_t key)
123 {
124         int i;
125         void *old_value = pthread_getspecific (key);
126         void *canary = (void*)0xDEADBEEFu;
127
128         pthread_key_create (&key, NULL);
129         g_assert (old_value != canary);
130
131         pthread_setspecific (key, canary);
132
133         /*First we probe for cats*/
134         tls_vector_offset = TLS_VECTOR_OFFSET_CATS;
135         if (mono_mach_arch_get_tls_value_from_thread (pthread_self (), key) == canary)
136                 goto ok;
137
138         tls_vector_offset = TLS_VECTOR_OFFSET_10_9;
139         if (mono_mach_arch_get_tls_value_from_thread (pthread_self (), key) == canary)
140                 goto ok;
141
142         tls_vector_offset = TLS_VECTOR_OFFSET_10_11;
143         if (mono_mach_arch_get_tls_value_from_thread (pthread_self (), key) == canary)
144                 goto ok;
145
146         /*Fallback to scanning a large range of offsets*/
147         for (i = TLS_PROBE_LOW_WATERMARK; i <= TLS_PROBE_HIGH_WATERMARK; i += 4) {
148                 tls_vector_offset = i;
149                 if (mono_mach_arch_get_tls_value_from_thread (pthread_self (), key) == canary) {
150                         g_warning ("Found new TLS offset at %d", i);
151                         goto ok;
152                 }
153         }
154
155         tls_vector_offset = -1;
156         g_warning ("could not discover the mach TLS offset");
157 ok:
158         pthread_setspecific (key, old_value);
159 }
160
161 #endif