Merge pull request #498 from Unroll-Me/master
[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  *
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 void *
20 mono_mach_arch_get_ip (thread_state_t state)
21 {
22         x86_thread_state64_t *arch_state = (x86_thread_state64_t *) state;
23
24         return (void *) arch_state->__rip;
25 }
26
27 void *
28 mono_mach_arch_get_sp (thread_state_t state)
29 {
30         x86_thread_state64_t *arch_state = (x86_thread_state64_t *) state;
31
32         return (void *) arch_state->__rsp;
33 }
34
35 int
36 mono_mach_arch_get_mcontext_size ()
37 {
38         return sizeof (struct __darwin_mcontext64);
39 }
40
41 void
42 mono_mach_arch_thread_state_to_mcontext (thread_state_t state, void *context)
43 {
44         x86_thread_state64_t *arch_state = (x86_thread_state64_t *) state;
45         struct __darwin_mcontext64 *ctx = (struct __darwin_mcontext64 *) context;
46
47         ctx->__ss = *arch_state;
48 }
49
50 void
51 mono_mach_arch_mcontext_to_thread_state (void *context, thread_state_t state)
52 {
53         x86_thread_state64_t *arch_state = (x86_thread_state64_t *) state;
54         struct __darwin_mcontext64 *ctx = (struct __darwin_mcontext64 *) context;
55
56         *arch_state = ctx->__ss;
57 }
58
59 int
60 mono_mach_arch_get_thread_state_size ()
61 {
62         return sizeof (x86_thread_state64_t);
63 }
64
65 kern_return_t
66 mono_mach_arch_get_thread_state (thread_port_t thread, thread_state_t state, mach_msg_type_number_t *count)
67 {
68         x86_thread_state64_t *arch_state = (x86_thread_state64_t *) state;
69         kern_return_t ret;
70
71         *count = x86_THREAD_STATE64_COUNT;
72
73         ret = thread_get_state (thread, x86_THREAD_STATE64, (thread_state_t) arch_state, count);
74
75         return ret;
76 }
77
78 kern_return_t
79 mono_mach_arch_set_thread_state (thread_port_t thread, thread_state_t state, mach_msg_type_number_t count)
80 {
81         return thread_set_state (thread, x86_THREAD_STATE64, state, count);
82 }
83
84 void *
85 mono_mach_get_tls_address_from_thread (pthread_t thread, pthread_key_t key)
86 {
87         /* OSX stores TLS values in a hidden array inside the pthread_t structure
88          * They are keyed off a giant array offset 0x60 into the pointer.  This value
89          * is baked into their pthread_getspecific implementation
90          */
91         intptr_t *p = (intptr_t *)thread;
92         intptr_t **tsd = (intptr_t **) ((char*)p + 0x60);
93
94         return (void *) &tsd [key];
95 }
96
97 void *
98 mono_mach_arch_get_tls_value_from_thread (pthread_t thread, guint32 key)
99 {
100         return *(void**)mono_mach_get_tls_address_from_thread (thread, key);
101 }
102
103 #endif