Merge pull request #2880 from kumpera/fix_5644
[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 #ifdef USE_MONO_CTX
82                 mono_sigctx_to_monoctx (&ctx, &info->client_info.ctx);
83 #else
84                 ARCH_COPY_SIGCTX_REGS (&info->client_info.regs, &ctx);
85 #endif
86         } else {
87                 g_assert (!info->client_info.stack_start);
88         }
89
90         /* Notify the JIT */
91         if (mono_gc_get_gc_callbacks ()->thread_suspend_func)
92                 mono_gc_get_gc_callbacks ()->thread_suspend_func (info->client_info.runtime_data, &ctx, NULL);
93
94         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);
95         binary_protocol_thread_suspend ((gpointer)mono_thread_info_get_tid (info), info->client_info.stopped_ip);
96
97         return TRUE;
98 }
99
100 void
101 sgen_wait_for_suspend_ack (int count)
102 {
103     /* mach thread_resume is synchronous so we dont need to wait for them */
104 }
105
106 /* LOCKING: assumes the GC lock is held */
107 int
108 sgen_thread_handshake (BOOL suspend)
109 {
110         SgenThreadInfo *cur_thread = mono_thread_info_current ();
111         kern_return_t ret;
112
113         int count = 0;
114
115         cur_thread->client_info.suspend_done = TRUE;
116         FOREACH_THREAD (info) {
117                 if (info == cur_thread || sgen_thread_pool_is_thread_pool_thread (mono_thread_info_get_tid (info)))
118                         continue;
119
120                 info->client_info.suspend_done = FALSE;
121                 if (info->client_info.gc_disabled)
122                         continue;
123
124                 if (suspend) {
125                         if (!sgen_suspend_thread (info))
126                                 continue;
127                 } else {
128                         do {
129                                 ret = thread_resume (info->client_info.info.native_handle);
130                         } while (ret == KERN_ABORTED);
131                         if (ret != KERN_SUCCESS)
132                                 continue;
133                 }
134                 count ++;
135         } FOREACH_THREAD_END
136         return count;
137 }
138
139 void
140 sgen_os_init (void)
141 {
142 }
143
144 int
145 mono_gc_get_suspend_signal (void)
146 {
147         return -1;
148 }
149
150 int
151 mono_gc_get_restart_signal (void)
152 {
153         return -1;
154 }
155 #endif
156 #endif
157 #endif