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 * Licensed under the MIT license. See LICENSE file in the project root for full license information.
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"
28 #include "utils/mach-support.h"
31 #if defined(__MACH__) && MONO_MACH_ARCH_SUPPORTED
33 #if !defined(USE_COOP_GC)
35 sgen_resume_thread (SgenThreadInfo *info)
37 return thread_resume (info->client_info.info.native_handle) == KERN_SUCCESS;
41 sgen_suspend_thread (SgenThreadInfo *info)
43 mach_msg_type_number_t num_state;
51 state = (thread_state_t) alloca (mono_mach_arch_get_thread_state_size ());
52 mctx = (mcontext_t) alloca (mono_mach_arch_get_mcontext_size ());
54 ret = thread_suspend (info->client_info.info.native_handle);
55 if (ret != KERN_SUCCESS)
58 ret = mono_mach_arch_get_thread_state (info->client_info.info.native_handle, state, &num_state);
59 if (ret != KERN_SUCCESS)
62 mono_mach_arch_thread_state_to_mcontext (state, mctx);
63 ctx.uc_mcontext = mctx;
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;
74 mono_sigctx_to_monoctx (&ctx, &info->client_info.ctx);
76 ARCH_COPY_SIGCTX_REGS (&info->client_info.regs, &ctx);
79 g_assert (!info->client_info.stack_start);
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);
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);
93 sgen_wait_for_suspend_ack (int count)
95 /* mach thread_resume is synchronous so we dont need to wait for them */
98 /* LOCKING: assumes the GC lock is held */
100 sgen_thread_handshake (BOOL suspend)
102 SgenThreadInfo *cur_thread = mono_thread_info_current ();
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)))
112 info->client_info.suspend_done = FALSE;
113 if (info->client_info.gc_disabled)
117 if (!sgen_suspend_thread (info))
120 ret = thread_resume (info->client_info.info.native_handle);
121 if (ret != KERN_SUCCESS)
135 mono_gc_get_suspend_signal (void)
141 mono_gc_get_restart_signal (void)