Merge pull request #2820 from kumpera/license-change-rebased
[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         return thread_resume (info->client_info.info.native_handle) == KERN_SUCCESS;
38 }
39
40 gboolean
41 sgen_suspend_thread (SgenThreadInfo *info)
42 {
43         mach_msg_type_number_t num_state;
44         thread_state_t state;
45         kern_return_t ret;
46         ucontext_t ctx;
47         mcontext_t mctx;
48
49         gpointer stack_start;
50
51         state = (thread_state_t) alloca (mono_mach_arch_get_thread_state_size ());
52         mctx = (mcontext_t) alloca (mono_mach_arch_get_mcontext_size ());
53
54         ret = thread_suspend (info->client_info.info.native_handle);
55         if (ret != KERN_SUCCESS)
56                 return FALSE;
57
58         ret = mono_mach_arch_get_thread_state (info->client_info.info.native_handle, state, &num_state);
59         if (ret != KERN_SUCCESS)
60                 return FALSE;
61
62         mono_mach_arch_thread_state_to_mcontext (state, mctx);
63         ctx.uc_mcontext = mctx;
64
65         info->client_info.stopped_domain = mono_thread_info_tls_get (info, TLS_KEY_DOMAIN);
66         info->client_info.stopped_ip = (gpointer) mono_mach_arch_get_ip (state);
67         info->client_info.stack_start = NULL;
68         stack_start = (char*) mono_mach_arch_get_sp (state) - REDZONE_SIZE;
69         /* If stack_start is not within the limits, then don't set it in info and we will be restarted. */
70         if (stack_start >= info->client_info.stack_start_limit && stack_start <= info->client_info.stack_end) {
71                 info->client_info.stack_start = stack_start;
72
73 #ifdef USE_MONO_CTX
74                 mono_sigctx_to_monoctx (&ctx, &info->client_info.ctx);
75 #else
76                 ARCH_COPY_SIGCTX_REGS (&info->client_info.regs, &ctx);
77 #endif
78         } else {
79                 g_assert (!info->client_info.stack_start);
80         }
81
82         /* Notify the JIT */
83         if (mono_gc_get_gc_callbacks ()->thread_suspend_func)
84                 mono_gc_get_gc_callbacks ()->thread_suspend_func (info->client_info.runtime_data, &ctx, NULL);
85
86         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);
87         binary_protocol_thread_suspend ((gpointer)mono_thread_info_get_tid (info), info->client_info.stopped_ip);
88
89         return TRUE;
90 }
91
92 void
93 sgen_wait_for_suspend_ack (int count)
94 {
95     /* mach thread_resume is synchronous so we dont need to wait for them */
96 }
97
98 /* LOCKING: assumes the GC lock is held */
99 int
100 sgen_thread_handshake (BOOL suspend)
101 {
102         SgenThreadInfo *cur_thread = mono_thread_info_current ();
103         kern_return_t ret;
104
105         int count = 0;
106
107         cur_thread->client_info.suspend_done = TRUE;
108         FOREACH_THREAD (info) {
109                 if (info == cur_thread || sgen_thread_pool_is_thread_pool_thread (mono_thread_info_get_tid (info)))
110                         continue;
111
112                 info->client_info.suspend_done = FALSE;
113                 if (info->client_info.gc_disabled)
114                         continue;
115
116                 if (suspend) {
117                         if (!sgen_suspend_thread (info))
118                                 continue;
119                 } else {
120                         ret = thread_resume (info->client_info.info.native_handle);
121                         if (ret != KERN_SUCCESS)
122                                 continue;
123                 }
124                 count ++;
125         } FOREACH_THREAD_END
126         return count;
127 }
128
129 void
130 sgen_os_init (void)
131 {
132 }
133
134 int
135 mono_gc_get_suspend_signal (void)
136 {
137         return -1;
138 }
139
140 int
141 mono_gc_get_restart_signal (void)
142 {
143         return -1;
144 }
145 #endif
146 #endif
147 #endif