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