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-common.h"
28 #include "qemu/coroutine_int.h"
30 #ifdef CONFIG_VALGRIND_H
31 #include <valgrind/valgrind.h>
40 #ifdef CONFIG_VALGRIND_H
41 unsigned int valgrind_stack_id
;
47 * Per-thread coroutine bookkeeping
49 static __thread CoroutineUContext leader
;
50 static __thread Coroutine
*current
;
53 * va_args to makecontext() must be type 'int', so passing
54 * the pointer we need may require several int args. This
55 * union is a quick hack to let us do that
62 static void coroutine_trampoline(int i0
, int i1
)
65 CoroutineUContext
*self
;
73 /* Initialize longjmp environment and switch back the caller */
74 if (!sigsetjmp(self
->env
, 0)) {
75 siglongjmp(*(sigjmp_buf
*)co
->entry_arg
, 1);
79 co
->entry(co
->entry_arg
);
80 qemu_coroutine_switch(co
, co
->caller
, COROUTINE_TERMINATE
);
84 Coroutine
*qemu_coroutine_new(void)
86 CoroutineUContext
*co
;
87 ucontext_t old_uc
, uc
;
89 union cc_arg arg
= {0};
91 /* The ucontext functions preserve signal masks which incurs a
92 * system call overhead. sigsetjmp(buf, 0)/siglongjmp() does not
93 * preserve signal masks but only works on the current stack.
94 * Since we need a way to create and switch to a new stack, use
95 * the ucontext functions for that but sigsetjmp()/siglongjmp() for
99 if (getcontext(&uc
) == -1) {
103 co
= g_malloc0(sizeof(*co
));
104 co
->stack_size
= COROUTINE_STACK_SIZE
;
105 co
->stack
= qemu_alloc_stack(&co
->stack_size
);
106 co
->base
.entry_arg
= &old_env
; /* stash away our jmp_buf */
108 uc
.uc_link
= &old_uc
;
109 uc
.uc_stack
.ss_sp
= co
->stack
;
110 uc
.uc_stack
.ss_size
= co
->stack_size
;
111 uc
.uc_stack
.ss_flags
= 0;
113 #ifdef CONFIG_VALGRIND_H
114 co
->valgrind_stack_id
=
115 VALGRIND_STACK_REGISTER(co
->stack
, co
->stack
+ co
->stack_size
);
120 makecontext(&uc
, (void (*)(void))coroutine_trampoline
,
121 2, arg
.i
[0], arg
.i
[1]);
123 /* swapcontext() in, siglongjmp() back out */
124 if (!sigsetjmp(old_env
, 0)) {
125 swapcontext(&old_uc
, &uc
);
130 #ifdef CONFIG_VALGRIND_H
131 #ifdef CONFIG_PRAGMA_DIAGNOSTIC_AVAILABLE
132 /* Work around an unused variable in the valgrind.h macro... */
133 #pragma GCC diagnostic push
134 #pragma GCC diagnostic ignored "-Wunused-but-set-variable"
136 static inline void valgrind_stack_deregister(CoroutineUContext
*co
)
138 VALGRIND_STACK_DEREGISTER(co
->valgrind_stack_id
);
140 #ifdef CONFIG_PRAGMA_DIAGNOSTIC_AVAILABLE
141 #pragma GCC diagnostic pop
145 void qemu_coroutine_delete(Coroutine
*co_
)
147 CoroutineUContext
*co
= DO_UPCAST(CoroutineUContext
, base
, co_
);
149 #ifdef CONFIG_VALGRIND_H
150 valgrind_stack_deregister(co
);
153 qemu_free_stack(co
->stack
, co
->stack_size
);
157 /* This function is marked noinline to prevent GCC from inlining it
158 * into coroutine_trampoline(). If we allow it to do that then it
159 * hoists the code to get the address of the TLS variable "current"
160 * out of the while() loop. This is an invalid transformation because
161 * the sigsetjmp() call may be called when running thread A but
162 * return in thread B, and so we might be in a different thread
163 * context each time round the loop.
165 CoroutineAction
__attribute__((noinline
))
166 qemu_coroutine_switch(Coroutine
*from_
, Coroutine
*to_
,
167 CoroutineAction action
)
169 CoroutineUContext
*from
= DO_UPCAST(CoroutineUContext
, base
, from_
);
170 CoroutineUContext
*to
= DO_UPCAST(CoroutineUContext
, base
, to_
);
175 ret
= sigsetjmp(from
->env
, 0);
177 siglongjmp(to
->env
, action
);
182 Coroutine
*qemu_coroutine_self(void)
185 current
= &leader
.base
;
190 bool qemu_in_coroutine(void)
192 return current
&& current
->caller
;