Add support for OP_GENERIC_CLASS_INIT
[mono.git] / mono / utils / mach-support-x86.c
1 /*
2  * mach-support-x86.c: mach support for x86
3  *
4  * Authors:
5  *   Geoff Norton (gnorton@novell.com)
6  *
7  * (C) 2010 Novell, Inc.
8  */
9
10 #include <config.h>
11
12 #if defined(__MACH__)
13 #include <stdint.h>
14 #include <glib.h>
15 #include <pthread.h>
16 #include "utils/mono-sigcontext.h"
17 #include "mach-support.h"
18
19 /* Known offsets used for TLS storage*/
20
21 /* All OSX versions up to 10.8 */
22 #define TLS_VECTOR_OFFSET_CATS 0x48
23 #define TLS_VECTOR_OFFSET_10_9 0xb0
24
25 static int tls_vector_offset;
26
27 void *
28 mono_mach_arch_get_ip (thread_state_t state)
29 {
30         x86_thread_state32_t *arch_state = (x86_thread_state32_t *) state;
31
32         return (void *) arch_state->__eip;
33 }
34
35 void *
36 mono_mach_arch_get_sp (thread_state_t state)
37 {
38         x86_thread_state32_t *arch_state = (x86_thread_state32_t *) state;
39
40         return (void *) arch_state->__esp;
41 }
42
43 int
44 mono_mach_arch_get_mcontext_size ()
45 {
46         return sizeof (struct __darwin_mcontext32);
47 }
48
49 void
50 mono_mach_arch_thread_state_to_mcontext (thread_state_t state, void *context)
51 {
52         x86_thread_state32_t *arch_state = (x86_thread_state32_t *) state;
53         struct __darwin_mcontext32 *ctx = (struct __darwin_mcontext32 *) context;
54
55         ctx->__ss = *arch_state;
56 }
57
58 void
59 mono_mach_arch_mcontext_to_thread_state (void *context, thread_state_t state)
60 {
61         x86_thread_state32_t *arch_state = (x86_thread_state32_t *) state;
62         struct __darwin_mcontext32 *ctx = (struct __darwin_mcontext32 *) context;
63
64         *arch_state = ctx->__ss;
65 }
66
67 int
68 mono_mach_arch_get_thread_state_size ()
69 {
70         return sizeof (x86_thread_state32_t);
71 }
72
73 kern_return_t
74 mono_mach_arch_get_thread_state (thread_port_t thread, thread_state_t state, mach_msg_type_number_t *count)
75 {
76         x86_thread_state32_t *arch_state = (x86_thread_state32_t *) state;
77         kern_return_t ret;
78
79         *count = x86_THREAD_STATE32_COUNT;
80
81         ret = thread_get_state (thread, x86_THREAD_STATE32, (thread_state_t) arch_state, count);
82
83         return ret;
84 }
85
86 kern_return_t
87 mono_mach_arch_set_thread_state (thread_port_t thread, thread_state_t state, mach_msg_type_number_t count)
88 {
89         return thread_set_state (thread, x86_THREAD_STATE32, state, count);
90 }
91
92 void *
93 mono_mach_get_tls_address_from_thread (pthread_t thread, pthread_key_t key)
94 {
95         /* OSX stores TLS values in a hidden array inside the pthread_t structure
96          * They are keyed off a giant array from a known offset into the pointer.  This value
97          * is baked into their pthread_getspecific implementation
98          */
99         intptr_t *p = (intptr_t *) thread;
100         intptr_t **tsd = (intptr_t **) ((char*)p + tls_vector_offset);
101
102         return (void *) &tsd [key];     
103 }
104
105 void *
106 mono_mach_arch_get_tls_value_from_thread (pthread_t thread, guint32 key)
107 {
108         return *(void**)mono_mach_get_tls_address_from_thread (thread, key);
109 }
110
111 void
112 mono_mach_init (pthread_key_t key)
113 {
114         void *old_value = pthread_getspecific (key);
115         void *canary = (void*)0xDEADBEEFu;
116
117         pthread_key_create (&key, NULL);
118         g_assert (old_value != canary);
119
120         pthread_setspecific (key, canary);
121
122         /*First we probe for cats*/
123         tls_vector_offset = TLS_VECTOR_OFFSET_CATS;
124         if (mono_mach_arch_get_tls_value_from_thread (pthread_self (), key) == canary)
125                 goto ok;
126
127         tls_vector_offset = TLS_VECTOR_OFFSET_10_9;
128         if (mono_mach_arch_get_tls_value_from_thread (pthread_self (), key) == canary)
129                 goto ok;
130
131         g_error ("could not discover the mach TLS offset");
132 ok:
133         pthread_setspecific (key, old_value);
134 }
135
136 #endif