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>
46 #ifdef CONFIG_VALGRIND_H
47 unsigned int valgrind_stack_id
;
53 * Per-thread coroutine bookkeeping
55 static __thread CoroutineUContext leader
;
56 static __thread Coroutine
*current
;
59 * va_args to makecontext() must be type 'int', so passing
60 * the pointer we need may require several int args. This
61 * union is a quick hack to let us do that
68 static void finish_switch_fiber(void *fake_stack_save
)
71 const void *bottom_old
;
74 __sanitizer_finish_switch_fiber(fake_stack_save
, &bottom_old
, &size_old
);
77 leader
.stack
= (void *)bottom_old
;
78 leader
.stack_size
= size_old
;
83 static void start_switch_fiber(void **fake_stack_save
,
84 const void *bottom
, size_t size
)
87 __sanitizer_start_switch_fiber(fake_stack_save
, bottom
, size
);
91 static void coroutine_trampoline(int i0
, int i1
)
94 CoroutineUContext
*self
;
96 void *fake_stack_save
= NULL
;
98 finish_switch_fiber(NULL
);
105 /* Initialize longjmp environment and switch back the caller */
106 if (!sigsetjmp(self
->env
, 0)) {
107 start_switch_fiber(&fake_stack_save
,
108 leader
.stack
, leader
.stack_size
);
109 siglongjmp(*(sigjmp_buf
*)co
->entry_arg
, 1);
112 finish_switch_fiber(fake_stack_save
);
115 co
->entry(co
->entry_arg
);
116 qemu_coroutine_switch(co
, co
->caller
, COROUTINE_TERMINATE
);
120 Coroutine
*qemu_coroutine_new(void)
122 CoroutineUContext
*co
;
123 ucontext_t old_uc
, uc
;
125 union cc_arg arg
= {0};
126 void *fake_stack_save
= NULL
;
128 /* The ucontext functions preserve signal masks which incurs a
129 * system call overhead. sigsetjmp(buf, 0)/siglongjmp() does not
130 * preserve signal masks but only works on the current stack.
131 * Since we need a way to create and switch to a new stack, use
132 * the ucontext functions for that but sigsetjmp()/siglongjmp() for
136 if (getcontext(&uc
) == -1) {
140 co
= g_malloc0(sizeof(*co
));
141 co
->stack_size
= COROUTINE_STACK_SIZE
;
142 co
->stack
= qemu_alloc_stack(&co
->stack_size
);
143 co
->base
.entry_arg
= &old_env
; /* stash away our jmp_buf */
145 uc
.uc_link
= &old_uc
;
146 uc
.uc_stack
.ss_sp
= co
->stack
;
147 uc
.uc_stack
.ss_size
= co
->stack_size
;
148 uc
.uc_stack
.ss_flags
= 0;
150 #ifdef CONFIG_VALGRIND_H
151 co
->valgrind_stack_id
=
152 VALGRIND_STACK_REGISTER(co
->stack
, co
->stack
+ co
->stack_size
);
157 makecontext(&uc
, (void (*)(void))coroutine_trampoline
,
158 2, arg
.i
[0], arg
.i
[1]);
160 /* swapcontext() in, siglongjmp() back out */
161 if (!sigsetjmp(old_env
, 0)) {
162 start_switch_fiber(&fake_stack_save
, co
->stack
, co
->stack_size
);
163 swapcontext(&old_uc
, &uc
);
166 finish_switch_fiber(fake_stack_save
);
171 #ifdef CONFIG_VALGRIND_H
172 #if defined(CONFIG_PRAGMA_DIAGNOSTIC_AVAILABLE) && !defined(__clang__)
173 /* Work around an unused variable in the valgrind.h macro... */
174 #pragma GCC diagnostic push
175 #pragma GCC diagnostic ignored "-Wunused-but-set-variable"
177 static inline void valgrind_stack_deregister(CoroutineUContext
*co
)
179 VALGRIND_STACK_DEREGISTER(co
->valgrind_stack_id
);
181 #if defined(CONFIG_PRAGMA_DIAGNOSTIC_AVAILABLE) && !defined(__clang__)
182 #pragma GCC diagnostic pop
186 void qemu_coroutine_delete(Coroutine
*co_
)
188 CoroutineUContext
*co
= DO_UPCAST(CoroutineUContext
, base
, co_
);
190 #ifdef CONFIG_VALGRIND_H
191 valgrind_stack_deregister(co
);
194 qemu_free_stack(co
->stack
, co
->stack_size
);
198 /* This function is marked noinline to prevent GCC from inlining it
199 * into coroutine_trampoline(). If we allow it to do that then it
200 * hoists the code to get the address of the TLS variable "current"
201 * out of the while() loop. This is an invalid transformation because
202 * the sigsetjmp() call may be called when running thread A but
203 * return in thread B, and so we might be in a different thread
204 * context each time round the loop.
206 CoroutineAction
__attribute__((noinline
))
207 qemu_coroutine_switch(Coroutine
*from_
, Coroutine
*to_
,
208 CoroutineAction action
)
210 CoroutineUContext
*from
= DO_UPCAST(CoroutineUContext
, base
, from_
);
211 CoroutineUContext
*to
= DO_UPCAST(CoroutineUContext
, base
, to_
);
213 void *fake_stack_save
= NULL
;
217 ret
= sigsetjmp(from
->env
, 0);
219 start_switch_fiber(action
== COROUTINE_TERMINATE
?
220 NULL
: &fake_stack_save
, to
->stack
, to
->stack_size
);
221 siglongjmp(to
->env
, action
);
224 finish_switch_fiber(fake_stack_save
);
229 Coroutine
*qemu_coroutine_self(void)
232 current
= &leader
.base
;
237 bool qemu_in_coroutine(void)
239 return current
&& current
->caller
;