2 * mach-support-arm.c: mach support for ARM
5 * Geoff Norton (gnorton@novell.com)
6 * Rodrigo Kumpera (kumpera@gmail.com)
8 * (C) 2010 Novell, Inc.
9 * (C) 2011 Xamarin, Inc.
18 #include "utils/mono-sigcontext.h"
19 #include "mach-support.h"
21 /* Known offsets used for TLS storage*/
24 #define TLS_VECTOR_OFFSET_0 0x48
25 #define TLS_VECTOR_OFFSET_1 0xA8
27 static int tls_vector_offset;
30 mono_mach_arch_get_ip (thread_state_t state)
32 arm_thread_state_t *arch_state = (arm_thread_state_t *) state;
34 return (void *) arch_state->__pc;
38 mono_mach_arch_get_sp (thread_state_t state)
40 arm_thread_state_t *arch_state = (arm_thread_state_t *) state;
42 return (void *) arch_state->__sp;
46 mono_mach_arch_get_mcontext_size ()
48 return sizeof (struct __darwin_mcontext);
52 mono_mach_arch_thread_state_to_mcontext (thread_state_t state, void *context)
54 arm_thread_state_t *arch_state = (arm_thread_state_t *) state;
55 struct __darwin_mcontext *ctx = (struct __darwin_mcontext *) context;
57 ctx->__ss = *arch_state;
61 mono_mach_arch_mcontext_to_thread_state (void *context, thread_state_t state)
63 arm_thread_state_t *arch_state = (arm_thread_state_t *) state;
64 struct __darwin_mcontext *ctx = (struct __darwin_mcontext *) context;
66 *arch_state = ctx->__ss;
70 mono_mach_arch_get_thread_state_size ()
72 return sizeof (arm_thread_state_t);
76 mono_mach_arch_get_thread_state (thread_port_t thread, thread_state_t state, mach_msg_type_number_t *count)
78 arm_thread_state_t *arch_state = (arm_thread_state_t *) state;
81 *count = ARM_THREAD_STATE_COUNT;
83 ret = thread_get_state (thread, ARM_THREAD_STATE, (thread_state_t) arch_state, count);
89 mono_mach_arch_set_thread_state (thread_port_t thread, thread_state_t state, mach_msg_type_number_t count)
91 return thread_set_state (thread, ARM_THREAD_STATE_COUNT, state, count);
95 mono_mach_get_tls_address_from_thread (pthread_t thread, pthread_key_t key)
97 /* Mach stores TLS values in a hidden array inside the pthread_t structure
98 * They are keyed off a giant array from a known offset into the pointer. This value
99 * is baked into their pthread_getspecific implementation
101 intptr_t *p = (intptr_t *) thread;
102 intptr_t **tsd = (intptr_t **) ((char*)p + tls_vector_offset);
104 return (void *) &tsd [key];
108 mono_mach_arch_get_tls_value_from_thread (pthread_t thread, guint32 key)
110 return *(void**)mono_mach_get_tls_address_from_thread (thread, key);
114 mono_mach_init (pthread_key_t key)
116 void *old_value = pthread_getspecific (key);
117 void *canary = (void*)0xDEADBEEFu;
119 pthread_key_create (&key, NULL);
120 g_assert (old_value != canary);
122 pthread_setspecific (key, canary);
124 /*First we probe for cats*/
125 tls_vector_offset = TLS_VECTOR_OFFSET_0;
126 if (mono_mach_arch_get_tls_value_from_thread (pthread_self (), key) == canary)
129 tls_vector_offset = TLS_VECTOR_OFFSET_1;
130 if (mono_mach_arch_get_tls_value_from_thread (pthread_self (), key) == canary)
133 g_error ("could not discover the mach TLS offset");
135 pthread_setspecific (key, old_value);