fix win thread size calc.
[mono.git] / mono / utils / mach-support-amd64.c
index 7c4e6112ef2d42ea9bf293ef37278147bfbc2a49..19040885da612aaf9908a3dedec6831aab1b491e 100644 (file)
@@ -3,8 +3,10 @@
  *
  * Authors:
  *   Geoff Norton (gnorton@novell.com)
+ *   Rodrigo Kumpera (kumpera@gmail.com)
  *
  * (C) 2010 Novell, Inc.
+ * (C) 2013 Xamarin, Inc.
  */
 
 #include <config.h>
 #include "utils/mono-sigcontext.h"
 #include "mach-support.h"
 
+/* Known offsets used for TLS storage*/
+
+/* All OSX versions up to 10.8 */
+#define TLS_VECTOR_OFFSET_CATS 0x60
+#define TLS_VECTOR_OFFSET_10_9 0xe0
+
+static int tls_vector_offset;
+
 void *
 mono_mach_arch_get_ip (thread_state_t state)
 {
@@ -39,14 +49,23 @@ mono_mach_arch_get_mcontext_size ()
 }
 
 void
-mono_mach_arch_thread_state_to_mcontext (thread_state_t state, mcontext_t context)
+mono_mach_arch_thread_state_to_mcontext (thread_state_t state, void *context)
 {
        x86_thread_state64_t *arch_state = (x86_thread_state64_t *) state;
-       struct __darwin_mcontext64 *ctx = (struct __darwin_mcontex64 *) context;
+       struct __darwin_mcontext64 *ctx = (struct __darwin_mcontext64 *) context;
 
        ctx->__ss = *arch_state;
 }
 
+void
+mono_mach_arch_mcontext_to_thread_state (void *context, thread_state_t state)
+{
+       x86_thread_state64_t *arch_state = (x86_thread_state64_t *) state;
+       struct __darwin_mcontext64 *ctx = (struct __darwin_mcontext64 *) context;
+
+       *arch_state = ctx->__ss;
+}
+
 int
 mono_mach_arch_get_thread_state_size ()
 {
@@ -66,16 +85,54 @@ mono_mach_arch_get_thread_state (thread_port_t thread, thread_state_t state, mac
        return ret;
 }
 
+kern_return_t
+mono_mach_arch_set_thread_state (thread_port_t thread, thread_state_t state, mach_msg_type_number_t count)
+{
+       return thread_set_state (thread, x86_THREAD_STATE64, state, count);
+}
+
 void *
-mono_mach_arch_get_tls_value_from_thread (pthread_t thread, guint32 key)
+mono_mach_get_tls_address_from_thread (pthread_t thread, pthread_key_t key)
 {
        /* OSX stores TLS values in a hidden array inside the pthread_t structure
-        * They are keyed off a giant array offset 0x60 into the pointer.  This value
+        * They are keyed off a giant array from a known offset into the pointer.  This value
         * is baked into their pthread_getspecific implementation
         */
        intptr_t *p = (intptr_t *)thread;
-       intptr_t **tsd = (intptr_t **) ((char*)p + 0x60);
+       intptr_t **tsd = (intptr_t **) ((char*)p + tls_vector_offset);
 
-       return (void *) tsd [key];
+       return (void *) &tsd [key];
 }
+
+void *
+mono_mach_arch_get_tls_value_from_thread (pthread_t thread, guint32 key)
+{
+       return *(void**)mono_mach_get_tls_address_from_thread (thread, key);
+}
+
+void
+mono_mach_init (pthread_key_t key)
+{
+       void *old_value = pthread_getspecific (key);
+       void *canary = (void*)0xDEADBEEFu;
+
+       pthread_key_create (&key, NULL);
+       g_assert (old_value != canary);
+
+       pthread_setspecific (key, canary);
+
+       /*First we probe for cats*/
+       tls_vector_offset = TLS_VECTOR_OFFSET_CATS;
+       if (mono_mach_arch_get_tls_value_from_thread (pthread_self (), key) == canary)
+               goto ok;
+
+       tls_vector_offset = TLS_VECTOR_OFFSET_10_9;
+       if (mono_mach_arch_get_tls_value_from_thread (pthread_self (), key) == canary)
+               goto ok;
+
+       g_error ("could not discover the mach TLS offset");
+ok:
+       pthread_setspecific (key, old_value);
+}
+
 #endif