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