2 * ucontext coroutine initialization code
4 * Copyright (C) 2006 Anthony Liguori <anthony@codemonkey.ws>
5 * Copyright (C) 2011 Kevin Wolf <kwolf@redhat.com>
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.0 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
21 /* XXX Is there a nicer way to disable glibc's stack check for longjmp? */
22 #ifdef _FORTIFY_SOURCE
23 #undef _FORTIFY_SOURCE
25 #include "qemu/osdep.h"
27 #include "qemu/coroutine_int.h"
29 #ifdef CONFIG_VALGRIND_H
30 #include <valgrind/valgrind.h>
33 #if defined(__SANITIZE_ADDRESS__) || __has_feature(address_sanitizer)
34 #ifdef CONFIG_ASAN_IFACE_FIBER
36 #include <sanitizer/asan_interface.h>
41 #include <sanitizer/tsan_interface.h>
48 #ifdef CONFIG_SAFESTACK
49 /* Need an unsafe stack for each coroutine */
51 size_t unsafe_stack_size
;
57 void *tsan_caller_fiber
;
60 #ifdef CONFIG_VALGRIND_H
61 unsigned int valgrind_stack_id
;
67 * Per-thread coroutine bookkeeping
69 static __thread CoroutineUContext leader
;
70 static __thread Coroutine
*current
;
73 * va_args to makecontext() must be type 'int', so passing
74 * the pointer we need may require several int args. This
75 * union is a quick hack to let us do that
83 * QEMU_ALWAYS_INLINE only does so if __OPTIMIZE__, so we cannot use it.
84 * always_inline is required to avoid TSan runtime fatal errors.
86 static inline __attribute__((always_inline
))
87 void on_new_fiber(CoroutineUContext
*co
)
90 co
->tsan_co_fiber
= __tsan_create_fiber(0); /* flags: sync on switch */
91 co
->tsan_caller_fiber
= __tsan_get_current_fiber();
95 /* always_inline is required to avoid TSan runtime fatal errors. */
96 static inline __attribute__((always_inline
))
97 void finish_switch_fiber(void *fake_stack_save
)
100 const void *bottom_old
;
103 __sanitizer_finish_switch_fiber(fake_stack_save
, &bottom_old
, &size_old
);
106 leader
.stack
= (void *)bottom_old
;
107 leader
.stack_size
= size_old
;
111 if (fake_stack_save
) {
112 __tsan_release(fake_stack_save
);
113 __tsan_switch_to_fiber(fake_stack_save
, 0); /* 0=synchronize */
118 /* always_inline is required to avoid TSan runtime fatal errors. */
119 static inline __attribute__((always_inline
))
120 void start_switch_fiber_asan(CoroutineAction action
, void **fake_stack_save
,
121 const void *bottom
, size_t size
)
124 __sanitizer_start_switch_fiber(
125 action
== COROUTINE_TERMINATE
? NULL
: fake_stack_save
,
130 /* always_inline is required to avoid TSan runtime fatal errors. */
131 static inline __attribute__((always_inline
))
132 void start_switch_fiber_tsan(void **fake_stack_save
,
133 CoroutineUContext
*co
,
137 void *new_fiber
= caller
?
138 co
->tsan_caller_fiber
:
140 void *curr_fiber
= __tsan_get_current_fiber();
141 __tsan_acquire(curr_fiber
);
143 *fake_stack_save
= curr_fiber
;
144 __tsan_switch_to_fiber(new_fiber
, 0); /* 0=synchronize */
148 static void coroutine_trampoline(int i0
, int i1
)
151 CoroutineUContext
*self
;
153 void *fake_stack_save
= NULL
;
155 finish_switch_fiber(NULL
);
162 /* Initialize longjmp environment and switch back the caller */
163 if (!sigsetjmp(self
->env
, 0)) {
164 start_switch_fiber_asan(COROUTINE_YIELD
, &fake_stack_save
, leader
.stack
,
166 start_switch_fiber_tsan(&fake_stack_save
, self
, true); /* true=caller */
167 siglongjmp(*(sigjmp_buf
*)co
->entry_arg
, 1);
170 finish_switch_fiber(fake_stack_save
);
173 co
->entry(co
->entry_arg
);
174 qemu_coroutine_switch(co
, co
->caller
, COROUTINE_TERMINATE
);
178 Coroutine
*qemu_coroutine_new(void)
180 CoroutineUContext
*co
;
181 ucontext_t old_uc
, uc
;
183 union cc_arg arg
= {0};
184 void *fake_stack_save
= NULL
;
186 /* The ucontext functions preserve signal masks which incurs a
187 * system call overhead. sigsetjmp(buf, 0)/siglongjmp() does not
188 * preserve signal masks but only works on the current stack.
189 * Since we need a way to create and switch to a new stack, use
190 * the ucontext functions for that but sigsetjmp()/siglongjmp() for
194 if (getcontext(&uc
) == -1) {
198 co
= g_malloc0(sizeof(*co
));
199 co
->stack_size
= COROUTINE_STACK_SIZE
;
200 co
->stack
= qemu_alloc_stack(&co
->stack_size
);
201 #ifdef CONFIG_SAFESTACK
202 co
->unsafe_stack_size
= COROUTINE_STACK_SIZE
;
203 co
->unsafe_stack
= qemu_alloc_stack(&co
->unsafe_stack_size
);
205 co
->base
.entry_arg
= &old_env
; /* stash away our jmp_buf */
207 uc
.uc_link
= &old_uc
;
208 uc
.uc_stack
.ss_sp
= co
->stack
;
209 uc
.uc_stack
.ss_size
= co
->stack_size
;
210 uc
.uc_stack
.ss_flags
= 0;
212 #ifdef CONFIG_VALGRIND_H
213 co
->valgrind_stack_id
=
214 VALGRIND_STACK_REGISTER(co
->stack
, co
->stack
+ co
->stack_size
);
220 makecontext(&uc
, (void (*)(void))coroutine_trampoline
,
221 2, arg
.i
[0], arg
.i
[1]);
223 /* swapcontext() in, siglongjmp() back out */
224 if (!sigsetjmp(old_env
, 0)) {
225 start_switch_fiber_asan(COROUTINE_YIELD
, &fake_stack_save
, co
->stack
,
227 start_switch_fiber_tsan(&fake_stack_save
,
228 co
, false); /* false=not caller */
230 #ifdef CONFIG_SAFESTACK
232 * Before we swap the context, set the new unsafe stack
233 * The unsafe stack grows just like the normal stack, so start from
234 * the last usable location of the memory area.
235 * NOTE: we don't have to re-set the usp afterwards because we are
236 * coming back to this context through a siglongjmp.
237 * The compiler already wrapped the corresponding sigsetjmp call with
238 * code that saves the usp on the (safe) stack before the call, and
239 * restores it right after (which is where we return with siglongjmp).
241 void *usp
= co
->unsafe_stack
+ co
->unsafe_stack_size
;
242 __safestack_unsafe_stack_ptr
= usp
;
245 swapcontext(&old_uc
, &uc
);
248 finish_switch_fiber(fake_stack_save
);
253 #ifdef CONFIG_VALGRIND_H
254 /* Work around an unused variable in the valgrind.h macro... */
255 #if !defined(__clang__)
256 #pragma GCC diagnostic push
257 #pragma GCC diagnostic ignored "-Wunused-but-set-variable"
259 static inline void valgrind_stack_deregister(CoroutineUContext
*co
)
261 VALGRIND_STACK_DEREGISTER(co
->valgrind_stack_id
);
263 #if !defined(__clang__)
264 #pragma GCC diagnostic pop
268 void qemu_coroutine_delete(Coroutine
*co_
)
270 CoroutineUContext
*co
= DO_UPCAST(CoroutineUContext
, base
, co_
);
272 #ifdef CONFIG_VALGRIND_H
273 valgrind_stack_deregister(co
);
276 qemu_free_stack(co
->stack
, co
->stack_size
);
277 #ifdef CONFIG_SAFESTACK
278 qemu_free_stack(co
->unsafe_stack
, co
->unsafe_stack_size
);
283 /* This function is marked noinline to prevent GCC from inlining it
284 * into coroutine_trampoline(). If we allow it to do that then it
285 * hoists the code to get the address of the TLS variable "current"
286 * out of the while() loop. This is an invalid transformation because
287 * the sigsetjmp() call may be called when running thread A but
288 * return in thread B, and so we might be in a different thread
289 * context each time round the loop.
291 CoroutineAction
__attribute__((noinline
))
292 qemu_coroutine_switch(Coroutine
*from_
, Coroutine
*to_
,
293 CoroutineAction action
)
295 CoroutineUContext
*from
= DO_UPCAST(CoroutineUContext
, base
, from_
);
296 CoroutineUContext
*to
= DO_UPCAST(CoroutineUContext
, base
, to_
);
298 void *fake_stack_save
= NULL
;
302 ret
= sigsetjmp(from
->env
, 0);
304 start_switch_fiber_asan(action
, &fake_stack_save
, to
->stack
,
306 start_switch_fiber_tsan(&fake_stack_save
,
307 to
, false); /* false=not caller */
308 siglongjmp(to
->env
, action
);
311 finish_switch_fiber(fake_stack_save
);
316 Coroutine
*qemu_coroutine_self(void)
319 current
= &leader
.base
;
322 if (!leader
.tsan_co_fiber
) {
323 leader
.tsan_co_fiber
= __tsan_get_current_fiber();
329 bool qemu_in_coroutine(void)
331 return current
&& current
->caller
;