[mono-config] use right type for result of strlen
[mono.git] / mono / utils / mach-support-arm.c
1 /*
2  * mach-support-arm.c: mach support for ARM
3  *
4  * Authors:
5  *   Geoff Norton (gnorton@novell.com)
6  *   Rodrigo Kumpera (kumpera@gmail.com)
7  *
8  * (C) 2010 Novell, Inc.
9  * (C) 2011 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 "utils/mono-compiler.h"
20 #include "mach-support.h"
21
22 /* _mcontext.h now defines __darwin_mcontext32, not __darwin_mcontext, starting with Xcode 5.1 */
23 #ifdef _STRUCT_MCONTEXT32
24        #define __darwin_mcontext       __darwin_mcontext32
25 #endif
26
27 /* Known offsets used for TLS storage*/
28
29
30 static const int known_tls_offsets[] = {
31         0x48, /*Found on iOS 6 */
32         0xA4,
33         0xA8,
34 };
35
36 #define TLS_PROBE_COUNT (sizeof (known_tls_offsets) / sizeof (int))
37
38 /* This is 2 slots less than the known low */
39 #define TLS_PROBE_LOW_WATERMARK 0x40
40 /* This is 24 slots above the know high, which is the same diff as the knowns high-low*/
41 #define TLS_PROBE_HIGH_WATERMARK 0x108
42
43 static int tls_vector_offset;
44
45 void *
46 mono_mach_arch_get_ip (thread_state_t state)
47 {
48         /* Can't use unified_thread_state on !ARM64 since this has to compile on armv6 too */
49 #ifdef TARGET_ARM64
50         arm_unified_thread_state_t *arch_state = (arm_unified_thread_state_t *) state;
51
52         return (void *) arch_state->ts_64.__pc;
53 #else
54         arm_thread_state_t *arch_state = (arm_thread_state_t *) state;
55
56         return (void *) arch_state->__pc;
57 #endif
58 }
59
60 void *
61 mono_mach_arch_get_sp (thread_state_t state)
62 {
63 #ifdef TARGET_ARM64
64         arm_unified_thread_state_t *arch_state = (arm_unified_thread_state_t *) state;
65
66         return (void *) arch_state->ts_64.__sp;
67 #else
68         arm_thread_state_t *arch_state = (arm_thread_state_t *) state;
69
70         return (void *) arch_state->__sp;
71 #endif
72 }
73
74 int
75 mono_mach_arch_get_mcontext_size ()
76 {
77 #ifdef TARGET_ARM64
78         return sizeof (struct __darwin_mcontext64);
79 #else
80         return sizeof (struct __darwin_mcontext);
81 #endif
82 }
83
84 void
85 mono_mach_arch_thread_state_to_mcontext (thread_state_t state, void *context)
86 {
87 #ifdef TARGET_ARM64
88         arm_unified_thread_state_t *arch_state = (arm_unified_thread_state_t *) state;
89         struct __darwin_mcontext64 *ctx = (struct __darwin_mcontext64 *) context;
90
91         ctx->__ss = arch_state->ts_64;
92 #else
93         arm_thread_state_t *arch_state = (arm_thread_state_t *) state;
94         struct __darwin_mcontext *ctx = (struct __darwin_mcontext *) context;
95
96         ctx->__ss = *arch_state;
97 #endif
98 }
99
100 void
101 mono_mach_arch_mcontext_to_thread_state (void *context, thread_state_t state)
102 {
103 #ifdef TARGET_ARM64
104         arm_unified_thread_state_t *arch_state = (arm_unified_thread_state_t *) state;
105         struct __darwin_mcontext64 *ctx = (struct __darwin_mcontext64 *) context;
106
107         arch_state->ts_64 = ctx->__ss;
108 #else
109         arm_thread_state_t *arch_state = (arm_thread_state_t *) state;
110         struct __darwin_mcontext *ctx = (struct __darwin_mcontext *) context;
111
112         *arch_state = ctx->__ss;
113 #endif
114 }
115
116 int
117 mono_mach_arch_get_thread_state_size ()
118 {
119 #ifdef TARGET_ARM64
120         return sizeof (arm_unified_thread_state_t);
121 #else
122         return sizeof (arm_thread_state_t);
123 #endif
124 }
125
126 kern_return_t
127 mono_mach_arch_get_thread_state (thread_port_t thread, thread_state_t state, mach_msg_type_number_t *count)
128 {
129 #if defined(HOST_WATCHOS)
130         g_error ("thread_get_state() is not supported by this platform");
131 #else   
132 #ifdef TARGET_ARM64
133         arm_unified_thread_state_t *arch_state = (arm_unified_thread_state_t *) state;
134         kern_return_t ret;
135
136         *count = ARM_UNIFIED_THREAD_STATE_COUNT;
137
138         ret = thread_get_state (thread, ARM_UNIFIED_THREAD_STATE, (thread_state_t) arch_state, count);
139 #else
140         arm_thread_state_t *arch_state = (arm_thread_state_t *) state;
141         kern_return_t ret;
142
143         *count = ARM_THREAD_STATE_COUNT;
144
145         ret = thread_get_state (thread, ARM_THREAD_STATE, (thread_state_t) arch_state, count);
146 #endif
147         return ret;
148 #endif
149 }
150
151 kern_return_t
152 mono_mach_arch_set_thread_state (thread_port_t thread, thread_state_t state, mach_msg_type_number_t count)
153 {
154 #if defined(HOST_WATCHOS)
155         g_error ("thread_set_state() is not supported by this platform");
156 #else
157 #ifdef TARGET_ARM64
158         return thread_set_state (thread, ARM_UNIFIED_THREAD_STATE, state, count);
159 #else
160         return thread_set_state (thread, ARM_THREAD_STATE, state, count);
161 #endif
162 #endif
163 }
164
165 void *
166 mono_mach_get_tls_address_from_thread (pthread_t thread, pthread_key_t key)
167 {
168         /* Mach stores TLS values in a hidden array inside the pthread_t structure
169          * They are keyed off a giant array from a known offset into the pointer. This value
170          * is baked into their pthread_getspecific implementation
171          */
172         intptr_t *p = (intptr_t *) thread;
173         intptr_t **tsd = (intptr_t **) ((char*)p + tls_vector_offset);
174         g_assert (tls_vector_offset != -1);
175
176         return (void *) &tsd [key];
177 }
178
179 void *
180 mono_mach_arch_get_tls_value_from_thread (pthread_t thread, guint32 key)
181 {
182         return *(void**)mono_mach_get_tls_address_from_thread (thread, key);
183 }
184
185 void
186 mono_mach_init (pthread_key_t key)
187 {
188         int i;
189         void *old_value = pthread_getspecific (key);
190         void *canary = (void*)0xDEADBEEFu;
191
192         pthread_key_create (&key, NULL);
193         g_assert (old_value != canary);
194
195         pthread_setspecific (key, canary);
196
197         /*First we probe for cats*/
198         for (i = 0; i < TLS_PROBE_COUNT; ++i) {
199                 tls_vector_offset = known_tls_offsets [i];
200                 if (mono_mach_arch_get_tls_value_from_thread (pthread_self (), key) == canary)
201                         goto ok;
202         }
203
204         /*Fallback to scanning a large range of offsets*/
205         for (i = TLS_PROBE_LOW_WATERMARK; i <= TLS_PROBE_HIGH_WATERMARK; i += 4) {
206                 tls_vector_offset = i;
207                 if (mono_mach_arch_get_tls_value_from_thread (pthread_self (), key) == canary) {
208                         g_warning ("Found new TLS offset at %d", i);
209                         goto ok;
210                 }
211         }
212
213         tls_vector_offset = -1;
214         g_warning ("could not discover the mach TLS offset");
215 ok:
216         pthread_setspecific (key, old_value);
217 }
218
219 #endif