5f242752ef75d6e211a390373268f94e92bc4bb2
[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         arm_thread_state_t *arch_state = (arm_thread_state_t *) state;
49
50         return (void *) arch_state->__pc;
51 }
52
53 void *
54 mono_mach_arch_get_sp (thread_state_t state)
55 {
56         arm_thread_state_t *arch_state = (arm_thread_state_t *) state;
57
58         return (void *) arch_state->__sp;
59 }
60
61 int
62 mono_mach_arch_get_mcontext_size ()
63 {
64         return sizeof (struct __darwin_mcontext);
65 }
66
67 void
68 mono_mach_arch_thread_state_to_mcontext (thread_state_t state, void *context)
69 {
70         arm_thread_state_t *arch_state = (arm_thread_state_t *) state;
71         struct __darwin_mcontext *ctx = (struct __darwin_mcontext *) context;
72
73         ctx->__ss = *arch_state;
74 }
75
76 void
77 mono_mach_arch_mcontext_to_thread_state (void *context, thread_state_t state)
78 {
79         arm_thread_state_t *arch_state = (arm_thread_state_t *) state;
80         struct __darwin_mcontext *ctx = (struct __darwin_mcontext *) context;
81
82         *arch_state = ctx->__ss;
83 }
84
85 int
86 mono_mach_arch_get_thread_state_size ()
87 {
88         return sizeof (arm_thread_state_t);
89 }
90
91 kern_return_t
92 mono_mach_arch_get_thread_state (thread_port_t thread, thread_state_t state, mach_msg_type_number_t *count)
93 {
94         arm_thread_state_t *arch_state = (arm_thread_state_t *) state;
95         kern_return_t ret;
96
97         *count = ARM_THREAD_STATE_COUNT;
98
99         ret = thread_get_state (thread, ARM_THREAD_STATE, (thread_state_t) arch_state, count);
100
101         return ret;
102 }
103
104 kern_return_t
105 mono_mach_arch_set_thread_state (thread_port_t thread, thread_state_t state, mach_msg_type_number_t count)
106 {
107         return thread_set_state (thread, ARM_THREAD_STATE_COUNT, state, count);
108 }
109
110 void *
111 mono_mach_get_tls_address_from_thread (pthread_t thread, pthread_key_t key)
112 {
113         /* Mach stores TLS values in a hidden array inside the pthread_t structure
114          * They are keyed off a giant array from a known offset into the pointer. This value
115          * is baked into their pthread_getspecific implementation
116          */
117         intptr_t *p = (intptr_t *) thread;
118         intptr_t **tsd = (intptr_t **) ((char*)p + tls_vector_offset);
119
120         return (void *) &tsd [key];
121 }
122
123 void *
124 mono_mach_arch_get_tls_value_from_thread (pthread_t thread, guint32 key)
125 {
126         return *(void**)mono_mach_get_tls_address_from_thread (thread, key);
127 }
128
129 void
130 mono_mach_init (pthread_key_t key)
131 {
132         int i;
133         void *old_value = pthread_getspecific (key);
134         void *canary = (void*)0xDEADBEEFu;
135
136         pthread_key_create (&key, NULL);
137         g_assert (old_value != canary);
138
139         pthread_setspecific (key, canary);
140
141         /*First we probe for cats*/
142         for (i = 0; i < TLS_PROBE_COUNT; ++i) {
143                 tls_vector_offset = known_tls_offsets [i];
144                 if (mono_mach_arch_get_tls_value_from_thread (pthread_self (), key) == canary)
145                         goto ok;
146         }
147
148         /*Fallback to scanning a large range of offsets*/
149         for (i = TLS_PROBE_LOW_WATERMARK; i <= TLS_PROBE_HIGH_WATERMARK; i += 4) {
150                 tls_vector_offset = i;
151                 if (mono_mach_arch_get_tls_value_from_thread (pthread_self (), key) == canary) {
152                         g_warning ("Found new TLS offset at %d", i);
153                         goto ok;
154                 }
155         }
156
157         g_error ("could not discover the mach TLS offset");
158 ok:
159         pthread_setspecific (key, old_value);
160 }
161
162 #endif