Merge pull request #733 from amoiseev-softheme/bugfix/monofix
[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 /* Known offsets used for TLS storage*/
23
24
25 static const int known_tls_offsets[] = {
26         0x48, /*Found on iOS 6 */
27         0xA4,
28         0xA8,
29 };
30
31 #define TLS_PROBE_COUNT (sizeof (known_tls_offsets) / sizeof (int))
32
33 /* This is 2 slots less than the known low */
34 #define TLS_PROBE_LOW_WATERMARK 0x40
35 /* This is 24 slots above the know high, which is the same diff as the knowns high-low*/
36 #define TLS_PROBE_HIGH_WATERMARK 0x108
37
38 static int tls_vector_offset;
39
40 void *
41 mono_mach_arch_get_ip (thread_state_t state)
42 {
43         arm_thread_state_t *arch_state = (arm_thread_state_t *) state;
44
45         return (void *) arch_state->__pc;
46 }
47
48 void *
49 mono_mach_arch_get_sp (thread_state_t state)
50 {
51         arm_thread_state_t *arch_state = (arm_thread_state_t *) state;
52
53         return (void *) arch_state->__sp;
54 }
55
56 int
57 mono_mach_arch_get_mcontext_size ()
58 {
59         return sizeof (struct __darwin_mcontext);
60 }
61
62 void
63 mono_mach_arch_thread_state_to_mcontext (thread_state_t state, void *context)
64 {
65         arm_thread_state_t *arch_state = (arm_thread_state_t *) state;
66         struct __darwin_mcontext *ctx = (struct __darwin_mcontext *) context;
67
68         ctx->__ss = *arch_state;
69 }
70
71 void
72 mono_mach_arch_mcontext_to_thread_state (void *context, thread_state_t state)
73 {
74         arm_thread_state_t *arch_state = (arm_thread_state_t *) state;
75         struct __darwin_mcontext *ctx = (struct __darwin_mcontext *) context;
76
77         *arch_state = ctx->__ss;
78 }
79
80 int
81 mono_mach_arch_get_thread_state_size ()
82 {
83         return sizeof (arm_thread_state_t);
84 }
85
86 kern_return_t
87 mono_mach_arch_get_thread_state (thread_port_t thread, thread_state_t state, mach_msg_type_number_t *count)
88 {
89         arm_thread_state_t *arch_state = (arm_thread_state_t *) state;
90         kern_return_t ret;
91
92         *count = ARM_THREAD_STATE_COUNT;
93
94         ret = thread_get_state (thread, ARM_THREAD_STATE, (thread_state_t) arch_state, count);
95
96         return ret;
97 }
98
99 kern_return_t
100 mono_mach_arch_set_thread_state (thread_port_t thread, thread_state_t state, mach_msg_type_number_t count)
101 {
102         return thread_set_state (thread, ARM_THREAD_STATE_COUNT, state, count);
103 }
104
105 void *
106 mono_mach_get_tls_address_from_thread (pthread_t thread, pthread_key_t key)
107 {
108         /* Mach stores TLS values in a hidden array inside the pthread_t structure
109          * They are keyed off a giant array from a known offset into the pointer. This value
110          * is baked into their pthread_getspecific implementation
111          */
112         intptr_t *p = (intptr_t *) thread;
113         intptr_t **tsd = (intptr_t **) ((char*)p + tls_vector_offset);
114
115         return (void *) &tsd [key];
116 }
117
118 void *
119 mono_mach_arch_get_tls_value_from_thread (pthread_t thread, guint32 key)
120 {
121         return *(void**)mono_mach_get_tls_address_from_thread (thread, key);
122 }
123
124 void
125 mono_mach_init (pthread_key_t key)
126 {
127         int i;
128         void *old_value = pthread_getspecific (key);
129         void *canary = (void*)0xDEADBEEFu;
130
131         pthread_key_create (&key, NULL);
132         g_assert (old_value != canary);
133
134         pthread_setspecific (key, canary);
135
136         /*First we probe for cats*/
137         for (i = 0; i < TLS_PROBE_COUNT; ++i) {
138                 tls_vector_offset = known_tls_offsets [i];
139                 if (mono_mach_arch_get_tls_value_from_thread (pthread_self (), key) == canary)
140                         goto ok;
141         }
142
143         /*Fallback to scanning a large range of offsets*/
144         for (i = TLS_PROBE_LOW_WATERMARK; i <= TLS_PROBE_HIGH_WATERMARK; i += 4) {
145                 tls_vector_offset = i;
146                 if (mono_mach_arch_get_tls_value_from_thread (pthread_self (), key) == canary) {
147                         g_warning ("Found new TLS offset at %d", i);
148                         goto ok;
149                 }
150         }
151
152         g_error ("could not discover the mach TLS offset");
153 ok:
154         pthread_setspecific (key, old_value);
155 }
156
157 #endif