Merge pull request #1109 from adbre/iss358
[mono.git] / mono / utils / mach-support-amd64.c
1 /*
2  * mach-support-x86.c: mach support for x86
3  *
4  * Authors:
5  *   Geoff Norton (gnorton@novell.com)
6  *   Rodrigo Kumpera (kumpera@gmail.com)
7  *
8  * (C) 2010 Novell, Inc.
9  * (C) 2013 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 "mach-support.h"
20
21 /* Known offsets used for TLS storage*/
22
23 /* All OSX versions up to 10.8 */
24 #define TLS_VECTOR_OFFSET_CATS 0x60
25 #define TLS_VECTOR_OFFSET_10_9 0xe0
26
27 static int tls_vector_offset;
28
29 void *
30 mono_mach_arch_get_ip (thread_state_t state)
31 {
32         x86_thread_state64_t *arch_state = (x86_thread_state64_t *) state;
33
34         return (void *) arch_state->__rip;
35 }
36
37 void *
38 mono_mach_arch_get_sp (thread_state_t state)
39 {
40         x86_thread_state64_t *arch_state = (x86_thread_state64_t *) state;
41
42         return (void *) arch_state->__rsp;
43 }
44
45 int
46 mono_mach_arch_get_mcontext_size ()
47 {
48         return sizeof (struct __darwin_mcontext64);
49 }
50
51 void
52 mono_mach_arch_thread_state_to_mcontext (thread_state_t state, void *context)
53 {
54         x86_thread_state64_t *arch_state = (x86_thread_state64_t *) state;
55         struct __darwin_mcontext64 *ctx = (struct __darwin_mcontext64 *) context;
56
57         ctx->__ss = *arch_state;
58 }
59
60 void
61 mono_mach_arch_mcontext_to_thread_state (void *context, thread_state_t state)
62 {
63         x86_thread_state64_t *arch_state = (x86_thread_state64_t *) state;
64         struct __darwin_mcontext64 *ctx = (struct __darwin_mcontext64 *) context;
65
66         *arch_state = ctx->__ss;
67 }
68
69 int
70 mono_mach_arch_get_thread_state_size ()
71 {
72         return sizeof (x86_thread_state64_t);
73 }
74
75 kern_return_t
76 mono_mach_arch_get_thread_state (thread_port_t thread, thread_state_t state, mach_msg_type_number_t *count)
77 {
78         x86_thread_state64_t *arch_state = (x86_thread_state64_t *) state;
79         kern_return_t ret;
80
81         *count = x86_THREAD_STATE64_COUNT;
82
83         ret = thread_get_state (thread, x86_THREAD_STATE64, (thread_state_t) arch_state, count);
84
85         return ret;
86 }
87
88 kern_return_t
89 mono_mach_arch_set_thread_state (thread_port_t thread, thread_state_t state, mach_msg_type_number_t count)
90 {
91         return thread_set_state (thread, x86_THREAD_STATE64, state, count);
92 }
93
94 void *
95 mono_mach_get_tls_address_from_thread (pthread_t thread, pthread_key_t key)
96 {
97         /* OSX 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
100          */
101         intptr_t *p = (intptr_t *)thread;
102         intptr_t **tsd = (intptr_t **) ((char*)p + tls_vector_offset);
103
104         return (void *) &tsd [key];
105 }
106
107 void *
108 mono_mach_arch_get_tls_value_from_thread (pthread_t thread, guint32 key)
109 {
110         return *(void**)mono_mach_get_tls_address_from_thread (thread, key);
111 }
112
113 void
114 mono_mach_init (pthread_key_t key)
115 {
116         void *old_value = pthread_getspecific (key);
117         void *canary = (void*)0xDEADBEEFu;
118
119         pthread_key_create (&key, NULL);
120         g_assert (old_value != canary);
121
122         pthread_setspecific (key, canary);
123
124         /*First we probe for cats*/
125         tls_vector_offset = TLS_VECTOR_OFFSET_CATS;
126         if (mono_mach_arch_get_tls_value_from_thread (pthread_self (), key) == canary)
127                 goto ok;
128
129         tls_vector_offset = TLS_VECTOR_OFFSET_10_9;
130         if (mono_mach_arch_get_tls_value_from_thread (pthread_self (), key) == canary)
131                 goto ok;
132
133         g_error ("could not discover the mach TLS offset");
134 ok:
135         pthread_setspecific (key, old_value);
136 }
137
138 #endif