2 * sgen-os-mach.c: Mach-OS 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.
31 #include "metadata/sgen-gc.h"
32 #include "metadata/sgen-archdep.h"
33 #include "metadata/sgen-protocol.h"
34 #include "metadata/sgen-thread-pool.h"
35 #include "metadata/object-internals.h"
36 #include "metadata/gc-internal.h"
39 #include "utils/mach-support.h"
42 #if defined(__MACH__) && MONO_MACH_ARCH_SUPPORTED
44 sgen_resume_thread (SgenThreadInfo *info)
46 return thread_resume (info->info.native_handle) == KERN_SUCCESS;
50 sgen_suspend_thread (SgenThreadInfo *info)
52 mach_msg_type_number_t num_state;
60 state = (thread_state_t) alloca (mono_mach_arch_get_thread_state_size ());
61 mctx = (mcontext_t) alloca (mono_mach_arch_get_mcontext_size ());
63 ret = thread_suspend (info->info.native_handle);
64 if (ret != KERN_SUCCESS)
67 ret = mono_mach_arch_get_thread_state (info->info.native_handle, state, &num_state);
68 if (ret != KERN_SUCCESS)
71 mono_mach_arch_thread_state_to_mcontext (state, mctx);
72 ctx.uc_mcontext = mctx;
74 info->stopped_domain = mono_thread_info_tls_get (info, TLS_KEY_DOMAIN);
75 info->stopped_ip = (gpointer) mono_mach_arch_get_ip (state);
76 info->stack_start = NULL;
77 stack_start = (char*) mono_mach_arch_get_sp (state) - REDZONE_SIZE;
78 /* If stack_start is not within the limits, then don't set it in info and we will be restarted. */
79 if (stack_start >= info->stack_start_limit && stack_start <= info->stack_end) {
80 info->stack_start = stack_start;
83 mono_sigctx_to_monoctx (&ctx, &info->ctx);
85 ARCH_COPY_SIGCTX_REGS (&info->regs, &ctx);
88 g_assert (!info->stack_start);
92 if (mono_gc_get_gc_callbacks ()->thread_suspend_func)
93 mono_gc_get_gc_callbacks ()->thread_suspend_func (info->runtime_data, &ctx, NULL);
95 SGEN_LOG (2, "thread %p stopped at %p stack_start=%p", (void*)(gsize)info->info.native_handle, info->stopped_ip, info->stack_start);
97 binary_protocol_thread_suspend ((gpointer)mono_thread_info_get_tid (info), info->stopped_ip);
103 sgen_wait_for_suspend_ack (int count)
105 /* mach thread_resume is synchronous so we dont need to wait for them */
108 /* LOCKING: assumes the GC lock is held */
110 sgen_thread_handshake (BOOL suspend)
112 SgenThreadInfo *cur_thread = mono_thread_info_current ();
114 SgenThreadInfo *info;
118 cur_thread->suspend_done = TRUE;
119 FOREACH_THREAD_SAFE (info) {
120 if (info == cur_thread || sgen_thread_pool_is_thread_pool_thread (mono_thread_info_get_tid (info)))
123 info->suspend_done = FALSE;
124 if (info->gc_disabled)
128 if (!sgen_suspend_thread (info))
131 ret = thread_resume (info->info.native_handle);
132 if (ret != KERN_SUCCESS)
136 } END_FOREACH_THREAD_SAFE
146 mono_gc_get_suspend_signal (void)
152 mono_gc_get_restart_signal (void)