Merge pull request #819 from brendanzagaeski/patch-1
[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 "metadata/sgen-gc.h"
32 #include "metadata/sgen-archdep.h"
33 #include "metadata/sgen-protocol.h"
34 #include "metadata/object-internals.h"
35 #include "metadata/gc-internal.h"
36
37 #if defined(__MACH__)
38 #include "utils/mach-support.h"
39 #endif
40
41 #if defined(__MACH__) && MONO_MACH_ARCH_SUPPORTED
42 gboolean
43 sgen_resume_thread (SgenThreadInfo *info)
44 {
45         return thread_resume (info->info.native_handle) == KERN_SUCCESS;
46 }
47
48 gboolean
49 sgen_suspend_thread (SgenThreadInfo *info)
50 {
51         mach_msg_type_number_t num_state;
52         thread_state_t state;
53         kern_return_t ret;
54         ucontext_t ctx;
55         mcontext_t mctx;
56
57         gpointer stack_start;
58
59         state = (thread_state_t) alloca (mono_mach_arch_get_thread_state_size ());
60         mctx = (mcontext_t) alloca (mono_mach_arch_get_mcontext_size ());
61
62         ret = thread_suspend (info->info.native_handle);
63         if (ret != KERN_SUCCESS)
64                 return FALSE;
65
66         ret = mono_mach_arch_get_thread_state (info->info.native_handle, state, &num_state);
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->stopped_domain = mono_thread_info_tls_get (info, TLS_KEY_DOMAIN);
74         info->stopped_ip = (gpointer) mono_mach_arch_get_ip (state);
75         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->stack_start_limit && info->stack_start <= info->stack_end) {
79                 info->stack_start = stack_start;
80
81 #ifdef USE_MONO_CTX
82                 mono_sigctx_to_monoctx (&ctx, &info->ctx);
83 #else
84                 ARCH_COPY_SIGCTX_REGS (&info->regs, &ctx);
85 #endif
86         } else {
87                 g_assert (!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->runtime_data, &ctx, NULL);
93
94         SGEN_LOG (2, "thread %p stopped at %p stack_start=%p", (void*)(gsize)info->info.native_handle, info->stopped_ip, info->stack_start);
95
96         binary_protocol_thread_suspend ((gpointer)mono_thread_info_get_tid (info), info->stopped_ip);
97
98         return TRUE;
99 }
100
101 void
102 sgen_wait_for_suspend_ack (int count)
103 {
104     /* mach thread_resume is synchronous so we dont need to wait for them */
105 }
106
107 /* LOCKING: assumes the GC lock is held */
108 int
109 sgen_thread_handshake (BOOL suspend)
110 {
111         SgenThreadInfo *cur_thread = mono_thread_info_current ();
112         kern_return_t ret;
113         SgenThreadInfo *info;
114
115         int count = 0;
116
117         FOREACH_THREAD_SAFE (info) {
118                 if (info == cur_thread || sgen_is_worker_thread (mono_thread_info_get_tid (info)))
119                         continue;
120                 if (info->gc_disabled)
121                         continue;
122
123                 if (suspend) {
124                         if (!sgen_suspend_thread (info))
125                                 continue;
126                 } else {
127                         ret = thread_resume (info->info.native_handle);
128                         if (ret != KERN_SUCCESS)
129                                 continue;
130                 }
131                 count ++;
132         } END_FOREACH_THREAD_SAFE
133         return count;
134 }
135
136 void
137 sgen_os_init (void)
138 {
139 }
140
141 int
142 mono_gc_get_suspend_signal (void)
143 {
144         return -1;
145 }
146 #endif
147 #endif