Merge pull request #4630 from BrzVlad/feature-valloc-limit
[mono-project.git] / mono / utils / mach-support-arm.c
blob649487ccc096f9d67f96614b9c8096a6dc2b06a9
1 /**
2 * \file
3 * mach support for ARM
5 * Authors:
6 * Geoff Norton (gnorton@novell.com)
7 * Rodrigo Kumpera (kumpera@gmail.com)
9 * (C) 2010 Novell, Inc.
10 * (C) 2011 Xamarin, Inc.
13 #include <config.h>
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"
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
28 /* Known offsets used for TLS storage*/
31 static const int known_tls_offsets[] = {
32 0x48, /*Found on iOS 6 */
33 0xA4,
34 0xA8,
37 #define TLS_PROBE_COUNT (sizeof (known_tls_offsets) / sizeof (int))
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
44 static int tls_vector_offset;
46 void *
47 mono_mach_arch_get_ip (thread_state_t state)
49 /* Can't use unified_thread_state on !ARM64 since this has to compile on armv6 too */
50 arm_thread_state_t *arch_state = (arm_thread_state_t *) state;
52 return (void *) arch_state->__pc;
55 void *
56 mono_mach_arch_get_sp (thread_state_t state)
58 arm_thread_state_t *arch_state = (arm_thread_state_t *) state;
60 return (void *) arch_state->__sp;
63 int
64 mono_mach_arch_get_mcontext_size ()
66 return sizeof (struct __darwin_mcontext);
69 void
70 mono_mach_arch_thread_states_to_mcontext (thread_state_t state, thread_state_t fpstate, void *context)
72 arm_thread_state_t *arch_state = (arm_thread_state_t *) state;
73 struct __darwin_mcontext *ctx = (struct __darwin_mcontext *) context;
75 ctx->__ss = *arch_state;
78 void
79 mono_mach_arch_mcontext_to_thread_states (void *context, thread_state_t state, thread_state_t fpstate)
81 arm_thread_state_t *arch_state = (arm_thread_state_t *) state;
82 struct __darwin_mcontext *ctx = (struct __darwin_mcontext *) context;
84 *arch_state = ctx->__ss;
87 void
88 mono_mach_arch_thread_states_to_mono_context (thread_state_t state, thread_state_t fpstate, MonoContext *context)
90 int i;
91 arm_thread_state_t *arch_state = (arm_thread_state_t *) state;
92 for (i = 0; i < 13; ++i)
93 context->regs [i] = arch_state->__r [i];
94 context->regs [ARMREG_R13] = arch_state->__sp;
95 context->regs [ARMREG_R14] = arch_state->__lr;
96 context->regs [ARMREG_R15] = arch_state->__pc;
97 context->pc = arch_state->__pc;
98 context->cpsr = arch_state->__cpsr;
102 mono_mach_arch_get_thread_state_size ()
104 return sizeof (arm_thread_state_t);
108 mono_mach_arch_get_thread_fpstate_size ()
110 return sizeof (arm_neon_state_t);
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)
116 #if defined(HOST_WATCHOS)
117 g_error ("thread_get_state() is not supported by this platform");
118 #else
119 arm_thread_state_t *arch_state = (arm_thread_state_t *) state;
120 kern_return_t ret;
122 *count = ARM_THREAD_STATE_COUNT;
124 ret = thread_get_state (thread, ARM_THREAD_STATE, (thread_state_t) arch_state, count);
125 return ret;
126 #endif
129 kern_return_t
130 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)
132 #if defined(HOST_WATCHOS)
133 g_error ("thread_set_state() is not supported by this platform");
134 #else
135 return thread_set_state (thread, ARM_THREAD_STATE, state, count);
136 #endif
139 void *
140 mono_mach_get_tls_address_from_thread (pthread_t thread, pthread_key_t key)
142 /* Mach stores TLS values in a hidden array inside the pthread_t structure
143 * They are keyed off a giant array from a known offset into the pointer. This value
144 * is baked into their pthread_getspecific implementation
146 intptr_t *p = (intptr_t *) thread;
147 intptr_t **tsd = (intptr_t **) ((char*)p + tls_vector_offset);
148 g_assert (tls_vector_offset != -1);
150 return (void *) &tsd [key];
153 void *
154 mono_mach_arch_get_tls_value_from_thread (pthread_t thread, guint32 key)
156 return *(void**)mono_mach_get_tls_address_from_thread (thread, key);
159 void
160 mono_mach_init (pthread_key_t key)
162 int i;
163 void *old_value = pthread_getspecific (key);
164 void *canary = (void*)0xDEADBEEFu;
166 pthread_key_create (&key, NULL);
167 g_assert (old_value != canary);
169 pthread_setspecific (key, canary);
171 /*First we probe for cats*/
172 for (i = 0; i < TLS_PROBE_COUNT; ++i) {
173 tls_vector_offset = known_tls_offsets [i];
174 if (mono_mach_arch_get_tls_value_from_thread (pthread_self (), key) == canary)
175 goto ok;
178 /*Fallback to scanning a large range of offsets*/
179 for (i = TLS_PROBE_LOW_WATERMARK; i <= TLS_PROBE_HIGH_WATERMARK; i += 4) {
180 tls_vector_offset = i;
181 if (mono_mach_arch_get_tls_value_from_thread (pthread_self (), key) == canary) {
182 g_warning ("Found new TLS offset at %d", i);
183 goto ok;
187 tls_vector_offset = -1;
188 g_warning ("could not discover the mach TLS offset");
190 pthread_setspecific (key, old_value);
193 #endif