Merge pull request #1909 from esdrubal/reflection
[mono.git] / mono / utils / mach-support-x86.c
1 /*
2  * mach-support-x86.c: mach support for x86
3  *
4  * Authors:
5  *   Geoff Norton (gnorton@novell.com)
6  *
7  * (C) 2010 Novell, Inc.
8  */
9
10 #include <config.h>
11
12 #if defined(__MACH__)
13 #include <stdint.h>
14 #include <glib.h>
15 #include <pthread.h>
16 #include "utils/mono-sigcontext.h"
17 #include "mach-support.h"
18
19 /* Known offsets used for TLS storage*/
20
21 /* All OSX versions up to 10.8 */
22 #define TLS_VECTOR_OFFSET_CATS 0x48
23 #define TLS_VECTOR_OFFSET_10_9 0xb0
24 #define TLS_VECTOR_OFFSET_10_11 0x100
25
26
27 /* This is 2 slots less than the known low */
28 #define TLS_PROBE_LOW_WATERMARK 0x40
29 /* This is 28 slots above the know high, which is more than the known high-low*/
30 #define TLS_PROBE_HIGH_WATERMARK 0x120
31
32
33 static int tls_vector_offset;
34
35 void *
36 mono_mach_arch_get_ip (thread_state_t state)
37 {
38         x86_thread_state32_t *arch_state = (x86_thread_state32_t *) state;
39
40         return (void *) arch_state->__eip;
41 }
42
43 void *
44 mono_mach_arch_get_sp (thread_state_t state)
45 {
46         x86_thread_state32_t *arch_state = (x86_thread_state32_t *) state;
47
48         return (void *) arch_state->__esp;
49 }
50
51 int
52 mono_mach_arch_get_mcontext_size ()
53 {
54         return sizeof (struct __darwin_mcontext32);
55 }
56
57 void
58 mono_mach_arch_thread_state_to_mcontext (thread_state_t state, void *context)
59 {
60         x86_thread_state32_t *arch_state = (x86_thread_state32_t *) state;
61         struct __darwin_mcontext32 *ctx = (struct __darwin_mcontext32 *) context;
62
63         ctx->__ss = *arch_state;
64 }
65
66 void
67 mono_mach_arch_mcontext_to_thread_state (void *context, thread_state_t state)
68 {
69         x86_thread_state32_t *arch_state = (x86_thread_state32_t *) state;
70         struct __darwin_mcontext32 *ctx = (struct __darwin_mcontext32 *) context;
71
72         *arch_state = ctx->__ss;
73 }
74
75 int
76 mono_mach_arch_get_thread_state_size ()
77 {
78         return sizeof (x86_thread_state32_t);
79 }
80
81 kern_return_t
82 mono_mach_arch_get_thread_state (thread_port_t thread, thread_state_t state, mach_msg_type_number_t *count)
83 {
84 #if defined(HOST_WATCHOS)
85         g_error ("thread_get_state() is not supported by this platform");
86 #else
87         x86_thread_state32_t *arch_state = (x86_thread_state32_t *) state;
88         kern_return_t ret;
89
90         *count = x86_THREAD_STATE32_COUNT;
91         ret = thread_get_state (thread, x86_THREAD_STATE32, (thread_state_t) arch_state, count);
92
93         return ret;
94 #endif
95 }
96
97 kern_return_t
98 mono_mach_arch_set_thread_state (thread_port_t thread, thread_state_t state, mach_msg_type_number_t count)
99 {
100 #if defined(HOST_WATCHOS)
101         g_error ("thread_set_state() is not supported by this platform");
102 #else
103         return thread_set_state (thread, x86_THREAD_STATE32, state, count);
104 #endif  
105 }
106
107 void *
108 mono_mach_get_tls_address_from_thread (pthread_t thread, pthread_key_t key)
109 {
110         /* OSX stores TLS values in a hidden array inside the pthread_t structure
111          * They are keyed off a giant array from a known offset into the pointer.  This value
112          * is baked into their pthread_getspecific implementation
113          */
114         intptr_t *p = (intptr_t *) thread;
115         intptr_t **tsd = (intptr_t **) ((char*)p + tls_vector_offset);
116         g_assert (tls_vector_offset != -1);
117
118         return (void *) &tsd [key];     
119 }
120
121 void *
122 mono_mach_arch_get_tls_value_from_thread (pthread_t thread, guint32 key)
123 {
124         return *(void**)mono_mach_get_tls_address_from_thread (thread, key);
125 }
126
127 void
128 mono_mach_init (pthread_key_t key)
129 {
130         int i;
131         void *old_value = pthread_getspecific (key);
132         void *canary = (void*)0xDEADBEEFu;
133
134         pthread_key_create (&key, NULL);
135         g_assert (old_value != canary);
136
137         pthread_setspecific (key, canary);
138
139         /*First we probe for cats*/
140         tls_vector_offset = TLS_VECTOR_OFFSET_CATS;
141         if (mono_mach_arch_get_tls_value_from_thread (pthread_self (), key) == canary)
142                 goto ok;
143
144         tls_vector_offset = TLS_VECTOR_OFFSET_10_9;
145         if (mono_mach_arch_get_tls_value_from_thread (pthread_self (), key) == canary)
146                 goto ok;
147
148         tls_vector_offset = TLS_VECTOR_OFFSET_10_11;
149         if (mono_mach_arch_get_tls_value_from_thread (pthread_self (), key) == canary)
150                 goto ok;
151
152         /*Fallback to scanning a large range of offsets*/
153         for (i = TLS_PROBE_LOW_WATERMARK; i <= TLS_PROBE_HIGH_WATERMARK; i += 4) {
154                 tls_vector_offset = i;
155                 if (mono_mach_arch_get_tls_value_from_thread (pthread_self (), key) == canary) {
156                         g_warning ("Found new TLS offset at %d", i);
157                         goto ok;
158                 }
159         }
160
161         tls_vector_offset = -1;
162         g_warning ("could not discover the mach TLS offset");
163 ok:
164         pthread_setspecific (key, old_value);
165 }
166
167 #endif