Moving BSTR conv to native code in SecureStringToBSTR.
[mono.git] / mono / metadata / sgen-os-mach.c
1 /*
2  * sgen-os-mach.c: Mach-OS support.
3  *
4  * Author:
5  *      Paolo Molaro (lupus@ximian.com)
6  *      Mark Probst (mprobst@novell.com)
7  *      Geoff Norton (gnorton@novell.com)
8  *
9  * Copyright 2010 Novell, Inc (http://www.novell.com)
10  * Copyright (C) 2012 Xamarin Inc
11  *
12  * Licensed under the MIT license. See LICENSE file in the project root for full license information.
13  */
14
15 #include "config.h"
16 #ifdef HAVE_SGEN_GC
17
18
19 #include <glib.h>
20 #include "sgen/sgen-gc.h"
21 #include "sgen/sgen-archdep.h"
22 #include "sgen/sgen-protocol.h"
23 #include "sgen/sgen-thread-pool.h"
24 #include "metadata/object-internals.h"
25 #include "metadata/gc-internals.h"
26
27 #if defined(__MACH__)
28 #include "utils/mach-support.h"
29 #endif
30
31 #if defined(__MACH__) && MONO_MACH_ARCH_SUPPORTED
32
33 #if !defined(USE_COOP_GC)
34 gboolean
35 sgen_resume_thread (SgenThreadInfo *info)
36 {
37         kern_return_t ret;
38         do {
39                 ret = thread_resume (info->client_info.info.native_handle);
40         } while (ret == KERN_ABORTED);
41         return ret == KERN_SUCCESS;
42 }
43
44 gboolean
45 sgen_suspend_thread (SgenThreadInfo *info)
46 {
47         mach_msg_type_number_t num_state;
48         thread_state_t state;
49         kern_return_t ret;
50         ucontext_t ctx;
51         mcontext_t mctx;
52
53         gpointer stack_start;
54
55         state = (thread_state_t) alloca (mono_mach_arch_get_thread_state_size ());
56         mctx = (mcontext_t) alloca (mono_mach_arch_get_mcontext_size ());
57
58         do {
59                 ret = thread_suspend (info->client_info.info.native_handle);
60         } while (ret == KERN_ABORTED);
61         if (ret != KERN_SUCCESS)
62                 return FALSE;
63
64         do {
65                 ret = mono_mach_arch_get_thread_state (info->client_info.info.native_handle, state, &num_state);
66         } while (ret == KERN_ABORTED);
67         if (ret != KERN_SUCCESS)
68                 return FALSE;
69
70         mono_mach_arch_thread_state_to_mcontext (state, mctx);
71         ctx.uc_mcontext = mctx;
72
73         info->client_info.stopped_domain = mono_thread_info_tls_get (info, TLS_KEY_DOMAIN);
74         info->client_info.stopped_ip = (gpointer) mono_mach_arch_get_ip (state);
75         info->client_info.stack_start = NULL;
76         stack_start = (char*) mono_mach_arch_get_sp (state) - REDZONE_SIZE;
77         /* If stack_start is not within the limits, then don't set it in info and we will be restarted. */
78         if (stack_start >= info->client_info.stack_start_limit && stack_start <= info->client_info.stack_end) {
79                 info->client_info.stack_start = stack_start;
80
81                 mono_sigctx_to_monoctx (&ctx, &info->client_info.ctx);
82         } else {
83                 g_assert (!info->client_info.stack_start);
84         }
85
86         /* Notify the JIT */
87         if (mono_gc_get_gc_callbacks ()->thread_suspend_func)
88                 mono_gc_get_gc_callbacks ()->thread_suspend_func (info->client_info.runtime_data, &ctx, NULL);
89
90         SGEN_LOG (2, "thread %p stopped at %p stack_start=%p", (void*)(gsize)info->client_info.info.native_handle, info->client_info.stopped_ip, info->client_info.stack_start);
91         binary_protocol_thread_suspend ((gpointer)mono_thread_info_get_tid (info), info->client_info.stopped_ip);
92
93         return TRUE;
94 }
95
96 void
97 sgen_wait_for_suspend_ack (int count)
98 {
99     /* mach thread_resume is synchronous so we dont need to wait for them */
100 }
101
102 /* LOCKING: assumes the GC lock is held */
103 int
104 sgen_thread_handshake (BOOL suspend)
105 {
106         SgenThreadInfo *cur_thread = mono_thread_info_current ();
107         kern_return_t ret;
108
109         int count = 0;
110
111         cur_thread->client_info.suspend_done = TRUE;
112         FOREACH_THREAD (info) {
113                 if (info == cur_thread || sgen_thread_pool_is_thread_pool_thread (mono_thread_info_get_tid (info)))
114                         continue;
115
116                 info->client_info.suspend_done = FALSE;
117                 if (info->client_info.gc_disabled)
118                         continue;
119
120                 if (suspend) {
121                         if (!sgen_suspend_thread (info))
122                                 continue;
123                 } else {
124                         do {
125                                 ret = thread_resume (info->client_info.info.native_handle);
126                         } while (ret == KERN_ABORTED);
127                         if (ret != KERN_SUCCESS)
128                                 continue;
129                 }
130                 count ++;
131         } FOREACH_THREAD_END
132         return count;
133 }
134
135 void
136 sgen_os_init (void)
137 {
138 }
139
140 int
141 mono_gc_get_suspend_signal (void)
142 {
143         return -1;
144 }
145
146 int
147 mono_gc_get_restart_signal (void)
148 {
149         return -1;
150 }
151 #endif
152 #endif
153 #endif