2 * sgen-os-posix.c: Posix support.
5 * Paolo Molaro (lupus@ximian.com)
6 * Mark Probst (mprobst@novell.com)
7 * Geoff Norton (gnorton@novell.com)
9 * Copyright 2010 Novell, Inc (http://www.novell.com)
10 * Copyright (C) 2012 Xamarin Inc
12 * This library is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU Library General Public
14 * License 2.0 as published by the Free Software Foundation;
16 * This library is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * Library General Public License for more details.
21 * You should have received a copy of the GNU Library General Public
22 * License 2.0 along with this library; if not, write to the Free
23 * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
28 #if defined(HAVE_SGEN_GC) && !defined(USE_COOP_GC)
29 #if !defined(__MACH__) && !MONO_MACH_ARCH_SUPPORTED && defined(HAVE_PTHREAD_KILL)
33 #include "sgen/sgen-gc.h"
34 #include "metadata/gc-internal.h"
35 #include "sgen/sgen-archdep.h"
36 #include "metadata/object-internals.h"
37 #include "utils/mono-signal-handler.h"
39 #if defined(__APPLE__) || defined(__OpenBSD__) || defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
40 const static int suspend_signal_num = SIGXFSZ;
42 const static int suspend_signal_num = SIGPWR;
44 const static int restart_signal_num = SIGXCPU;
46 static SgenSemaphore suspend_ack_semaphore;
47 static SgenSemaphore *suspend_ack_semaphore_ptr;
49 static sigset_t suspend_signal_mask;
50 static sigset_t suspend_ack_signal_mask;
53 suspend_thread (SgenThreadInfo *info, void *context)
57 gpointer regs [ARCH_NUM_REGS];
62 info->client_info.stopped_domain = mono_domain_get ();
63 info->client_info.signal = 0;
64 stop_count = sgen_global_stop_count;
65 /* duplicate signal */
66 if (0 && info->client_info.stop_count == stop_count)
71 mono_sigctx_to_monoctx (context, &ctx);
72 info->client_info.stopped_ip = MONO_CONTEXT_GET_IP (&ctx);
73 stack_start = (((guint8 *) MONO_CONTEXT_GET_SP (&ctx)) - REDZONE_SIZE);
75 info->client_info.stopped_ip = NULL;
79 info->client_info.stopped_ip = context ? (gpointer) ARCH_SIGCTX_IP (context) : NULL;
80 stack_start = context ? (char*) ARCH_SIGCTX_SP (context) - REDZONE_SIZE : NULL;
83 /* If stack_start is not within the limits, then don't set it
84 in info and we will be restarted. */
85 if (stack_start >= info->client_info.stack_start_limit && stack_start <= info->client_info.stack_end) {
86 info->client_info.stack_start = stack_start;
90 memcpy (&info->client_info.ctx, &ctx, sizeof (MonoContext));
92 memset (&info->client_info.ctx, 0, sizeof (MonoContext));
96 ARCH_COPY_SIGCTX_REGS (regs, context);
97 memcpy (&info->client_info.regs, regs, sizeof (info->client_info.regs));
99 memset (&info->client_info.regs, 0, sizeof (info->client_info.regs));
103 g_assert (!info->client_info.stack_start);
107 if (mono_gc_get_gc_callbacks ()->thread_suspend_func)
108 mono_gc_get_gc_callbacks ()->thread_suspend_func (info->client_info.runtime_data, context, NULL);
110 SGEN_LOG (4, "Posting suspend_ack_semaphore for suspend from %p %p", info, (gpointer)mono_native_thread_id_get ());
113 Block the restart signal.
114 We need to block the restart signal while posting to the suspend_ack semaphore or we race to sigsuspend,
115 which might miss the signal and get stuck.
117 pthread_sigmask (SIG_BLOCK, &suspend_ack_signal_mask, NULL);
119 /* notify the waiting thread */
120 SGEN_SEMAPHORE_POST (suspend_ack_semaphore_ptr);
121 info->client_info.stop_count = stop_count;
123 /* wait until we receive the restart signal */
125 info->client_info.signal = 0;
126 sigsuspend (&suspend_signal_mask);
127 } while (info->client_info.signal != restart_signal_num);
129 /* Unblock the restart signal. */
130 pthread_sigmask (SIG_UNBLOCK, &suspend_ack_signal_mask, NULL);
132 SGEN_LOG (4, "Posting suspend_ack_semaphore for resume from %p %p\n", info, (gpointer)mono_native_thread_id_get ());
133 /* notify the waiting thread */
134 SGEN_SEMAPHORE_POST (suspend_ack_semaphore_ptr);
137 /* LOCKING: assumes the GC lock is held (by the stopping thread) */
138 MONO_SIG_HANDLER_FUNC (static, suspend_handler)
141 * The suspend signal handler potentially uses syscalls that
142 * can set errno, and it calls functions that use the hazard
143 * pointer machinery. Since we're interrupting other code we
144 * must restore those to the values they had when we
147 SgenThreadInfo *info;
148 int old_errno = errno;
149 int hp_save_index = mono_hazard_pointer_save_for_signal_handler ();
150 MONO_SIG_HANDLER_GET_CONTEXT;
152 info = mono_thread_info_current ();
153 suspend_thread (info, ctx);
155 mono_hazard_pointer_restore_for_signal_handler (hp_save_index);
159 MONO_SIG_HANDLER_FUNC (static, restart_handler)
161 SgenThreadInfo *info;
162 int old_errno = errno;
164 info = mono_thread_info_current ();
165 info->client_info.signal = restart_signal_num;
166 SGEN_LOG (4, "Restart handler in %p %p", info, (gpointer)mono_native_thread_id_get ());
171 sgen_resume_thread (SgenThreadInfo *info)
173 return mono_threads_pthread_kill (info, restart_signal_num) == 0;
177 sgen_suspend_thread (SgenThreadInfo *info)
179 return mono_threads_pthread_kill (info, suspend_signal_num) == 0;
183 sgen_wait_for_suspend_ack (int count)
187 for (i = 0; i < count; ++i) {
188 while ((result = SGEN_SEMAPHORE_WAIT (suspend_ack_semaphore_ptr)) != 0) {
189 if (errno != EINTR) {
190 g_error ("SGEN_SEMAPHORE_WAIT FAILED with %d errno %d (%s)", result, errno, strerror (errno));
197 sgen_thread_handshake (BOOL suspend)
200 SgenThreadInfo *info;
201 int signum = suspend ? suspend_signal_num : restart_signal_num;
203 MonoNativeThreadId me = mono_native_thread_id_get ();
206 mono_thread_info_current ()->client_info.suspend_done = TRUE;
207 FOREACH_THREAD_SAFE (info) {
208 if (mono_native_thread_id_equals (mono_thread_info_get_tid (info), me)) {
211 info->client_info.suspend_done = FALSE;
212 if (info->client_info.gc_disabled)
214 /*if (signum == suspend_signal_num && info->stop_count == global_stop_count)
216 result = mono_threads_pthread_kill (info, signum);
220 info->client_info.skip = 1;
222 } END_FOREACH_THREAD_SAFE
224 sgen_wait_for_suspend_ack (count);
226 SGEN_LOG (4, "%s handshake for %d threads\n", suspend ? "suspend" : "resume", count);
234 struct sigaction sinfo;
236 if (mono_thread_info_unified_management_enabled ())
239 suspend_ack_semaphore_ptr = &suspend_ack_semaphore;
240 SGEN_SEMAPHORE_INIT (&suspend_ack_semaphore, 0);
242 sigfillset (&sinfo.sa_mask);
243 sinfo.sa_flags = SA_RESTART | SA_SIGINFO;
244 sinfo.sa_sigaction = suspend_handler;
245 if (sigaction (suspend_signal_num, &sinfo, NULL) != 0) {
246 g_error ("failed sigaction");
249 sinfo.sa_handler = (void*) restart_handler;
250 if (sigaction (restart_signal_num, &sinfo, NULL) != 0) {
251 g_error ("failed sigaction");
254 sigfillset (&suspend_signal_mask);
255 sigdelset (&suspend_signal_mask, restart_signal_num);
257 sigemptyset (&suspend_ack_signal_mask);
258 sigaddset (&suspend_ack_signal_mask, restart_signal_num);
263 mono_gc_get_suspend_signal (void)
265 return suspend_signal_num;
269 mono_gc_get_restart_signal (void)
271 return restart_signal_num;