Merge pull request #1888 from joelmartinez/mdoc-flagsbug
[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 #define TLS_VECTOR_OFFSET_10_11 0x100
27
28 static int tls_vector_offset;
29
30 void *
31 mono_mach_arch_get_ip (thread_state_t state)
32 {
33         x86_thread_state64_t *arch_state = (x86_thread_state64_t *) state;
34
35         return (void *) arch_state->__rip;
36 }
37
38 void *
39 mono_mach_arch_get_sp (thread_state_t state)
40 {
41         x86_thread_state64_t *arch_state = (x86_thread_state64_t *) state;
42
43         return (void *) arch_state->__rsp;
44 }
45
46 int
47 mono_mach_arch_get_mcontext_size ()
48 {
49         return sizeof (struct __darwin_mcontext64);
50 }
51
52 void
53 mono_mach_arch_thread_state_to_mcontext (thread_state_t state, void *context)
54 {
55         x86_thread_state64_t *arch_state = (x86_thread_state64_t *) state;
56         struct __darwin_mcontext64 *ctx = (struct __darwin_mcontext64 *) context;
57
58         ctx->__ss = *arch_state;
59 }
60
61 void
62 mono_mach_arch_mcontext_to_thread_state (void *context, thread_state_t state)
63 {
64         x86_thread_state64_t *arch_state = (x86_thread_state64_t *) state;
65         struct __darwin_mcontext64 *ctx = (struct __darwin_mcontext64 *) context;
66
67         *arch_state = ctx->__ss;
68 }
69
70 int
71 mono_mach_arch_get_thread_state_size ()
72 {
73         return sizeof (x86_thread_state64_t);
74 }
75
76 kern_return_t
77 mono_mach_arch_get_thread_state (thread_port_t thread, thread_state_t state, mach_msg_type_number_t *count)
78 {
79         x86_thread_state64_t *arch_state = (x86_thread_state64_t *) state;
80         kern_return_t ret;
81
82         *count = x86_THREAD_STATE64_COUNT;
83
84         ret = thread_get_state (thread, x86_THREAD_STATE64, (thread_state_t) arch_state, count);
85
86         return ret;
87 }
88
89 kern_return_t
90 mono_mach_arch_set_thread_state (thread_port_t thread, thread_state_t state, mach_msg_type_number_t count)
91 {
92         return thread_set_state (thread, x86_THREAD_STATE64, state, count);
93 }
94
95 void *
96 mono_mach_get_tls_address_from_thread (pthread_t thread, pthread_key_t key)
97 {
98         /* OSX stores TLS values in a hidden array inside the pthread_t structure
99          * They are keyed off a giant array from a known offset into the pointer.  This value
100          * is baked into their pthread_getspecific implementation
101          */
102         intptr_t *p = (intptr_t *)thread;
103         intptr_t **tsd = (intptr_t **) ((char*)p + tls_vector_offset);
104
105         return (void *) &tsd [key];
106 }
107
108 void *
109 mono_mach_arch_get_tls_value_from_thread (pthread_t thread, guint32 key)
110 {
111         return *(void**)mono_mach_get_tls_address_from_thread (thread, key);
112 }
113
114 void
115 mono_mach_init (pthread_key_t key)
116 {
117         void *old_value = pthread_getspecific (key);
118         void *canary = (void*)0xDEADBEEFu;
119
120         pthread_key_create (&key, NULL);
121         g_assert (old_value != canary);
122
123         pthread_setspecific (key, canary);
124
125         /*First we probe for cats*/
126         tls_vector_offset = TLS_VECTOR_OFFSET_CATS;
127         if (mono_mach_arch_get_tls_value_from_thread (pthread_self (), key) == canary)
128                 goto ok;
129
130         tls_vector_offset = TLS_VECTOR_OFFSET_10_9;
131         if (mono_mach_arch_get_tls_value_from_thread (pthread_self (), key) == canary)
132                 goto ok;
133
134         tls_vector_offset = TLS_VECTOR_OFFSET_10_11;
135         if (mono_mach_arch_get_tls_value_from_thread (pthread_self (), key) == canary)
136                 goto ok;
137
138         g_error ("could not discover the mach TLS offset");
139 ok:
140         pthread_setspecific (key, old_value);
141 }
142
143 #endif