Merge pull request #3406 from alexanderkyte/mobile_static_default
[mono.git] / mono / utils / mach-support-arm64.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         arm_unified_thread_state_t *arch_state = (arm_unified_thread_state_t *) state;
50
51         return (void *) arch_state->ts_64.__pc;
52 }
53
54 void *
55 mono_mach_arch_get_sp (thread_state_t state)
56 {
57         arm_unified_thread_state_t *arch_state = (arm_unified_thread_state_t *) state;
58
59         return (void *) arch_state->ts_64.__sp;
60 }
61
62 int
63 mono_mach_arch_get_mcontext_size ()
64 {
65         return sizeof (struct __darwin_mcontext64);
66 }
67
68 void
69 mono_mach_arch_thread_states_to_mcontext (thread_state_t state, thread_state_t fpstate, void *context)
70 {
71         arm_unified_thread_state_t *arch_state = (arm_unified_thread_state_t *) state;
72         struct __darwin_mcontext64 *ctx = (struct __darwin_mcontext64 *) context;
73
74         ctx->__ss = arch_state->ts_64;
75 }
76
77 void
78 mono_mach_arch_mcontext_to_thread_states (void *context, thread_state_t state, thread_state_t fpstate)
79 {
80         arm_unified_thread_state_t *arch_state = (arm_unified_thread_state_t *) state;
81         struct __darwin_mcontext64 *ctx = (struct __darwin_mcontext64 *) context;
82
83         arch_state->ts_64 = ctx->__ss;
84 }
85
86 void
87 mono_mach_arch_thread_states_to_mono_context (thread_state_t state, thread_state_t fpstate, MonoContext *context)
88 {
89         int i;
90         arm_unified_thread_state_t *arch_state = (arm_unified_thread_state_t *) state;
91         for (i = 0; i < 29; ++i)
92                 context->regs [i] = arch_state->ts_64.__x [i];
93
94         context->regs [ARMREG_R29] = arch_state->ts_64.__fp;
95         context->regs [ARMREG_R30] = arch_state->ts_64.__lr;
96         context->regs [ARMREG_SP] = arch_state->ts_64.__sp;
97         context->pc = arch_state->ts_64.__pc;
98 }
99
100 int
101 mono_mach_arch_get_thread_state_size ()
102 {
103         return sizeof (arm_unified_thread_state_t);
104 }
105
106 int
107 mono_mach_arch_get_thread_fpstate_size ()
108 {
109         g_assert_not_reached ();
110 }
111
112 kern_return_t
113 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)
114 {
115         arm_unified_thread_state_t *arch_state = (arm_unified_thread_state_t *) state;
116         kern_return_t ret;
117
118         *count = ARM_UNIFIED_THREAD_STATE_COUNT;
119
120         ret = thread_get_state (thread, ARM_UNIFIED_THREAD_STATE, (thread_state_t) arch_state, count);
121         return ret;
122 }
123
124 kern_return_t
125 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)
126 {
127         return thread_set_state (thread, ARM_UNIFIED_THREAD_STATE, state, count);
128 }
129
130 void *
131 mono_mach_get_tls_address_from_thread (pthread_t thread, pthread_key_t key)
132 {
133         /* Mach stores TLS values in a hidden array inside the pthread_t structure
134          * They are keyed off a giant array from a known offset into the pointer. This value
135          * is baked into their pthread_getspecific implementation
136          */
137         intptr_t *p = (intptr_t *) thread;
138         intptr_t **tsd = (intptr_t **) ((char*)p + tls_vector_offset);
139         g_assert (tls_vector_offset != -1);
140
141         return (void *) &tsd [key];
142 }
143
144 void *
145 mono_mach_arch_get_tls_value_from_thread (pthread_t thread, guint32 key)
146 {
147         return *(void**)mono_mach_get_tls_address_from_thread (thread, key);
148 }
149
150 void
151 mono_mach_init (pthread_key_t key)
152 {
153         int i;
154         void *old_value = pthread_getspecific (key);
155         void *canary = (void*)0xDEADBEEFu;
156
157         pthread_key_create (&key, NULL);
158         g_assert (old_value != canary);
159
160         pthread_setspecific (key, canary);
161
162         /*First we probe for cats*/
163         for (i = 0; i < TLS_PROBE_COUNT; ++i) {
164                 tls_vector_offset = known_tls_offsets [i];
165                 if (mono_mach_arch_get_tls_value_from_thread (pthread_self (), key) == canary)
166                         goto ok;
167         }
168
169         /*Fallback to scanning a large range of offsets*/
170         for (i = TLS_PROBE_LOW_WATERMARK; i <= TLS_PROBE_HIGH_WATERMARK; i += 4) {
171                 tls_vector_offset = i;
172                 if (mono_mach_arch_get_tls_value_from_thread (pthread_self (), key) == canary) {
173                         g_warning ("Found new TLS offset at %d", i);
174                         goto ok;
175                 }
176         }
177
178         tls_vector_offset = -1;
179         g_warning ("could not discover the mach TLS offset");
180 ok:
181         pthread_setspecific (key, old_value);
182 }
183
184 #endif