]> git.proxmox.com Git - mirror_qemu.git/blame - coroutine-ucontext.c
Replace all setjmp()/longjmp() with sigsetjmp()/siglongjmp()
[mirror_qemu.git] / coroutine-ucontext.c
CommitLineData
00dccaf1
KW
1/*
2 * ucontext coroutine initialization code
3 *
4 * Copyright (C) 2006 Anthony Liguori <anthony@codemonkey.ws>
5 * Copyright (C) 2011 Kevin Wolf <kwolf@redhat.com>
6 *
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.
11 *
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.
16 *
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/>.
19 */
20
21/* XXX Is there a nicer way to disable glibc's stack check for longjmp? */
22#ifdef _FORTIFY_SOURCE
23#undef _FORTIFY_SOURCE
24#endif
25#include <stdlib.h>
26#include <setjmp.h>
27#include <stdint.h>
28#include <pthread.h>
29#include <ucontext.h>
30#include "qemu-common.h"
737e150e 31#include "block/coroutine_int.h"
00dccaf1 32
3f4349dc
KW
33#ifdef CONFIG_VALGRIND_H
34#include <valgrind/valgrind.h>
35#endif
36
00dccaf1
KW
37enum {
38 /* Maximum free pool size prevents holding too many freed coroutines */
39 POOL_MAX_SIZE = 64,
40};
41
39a7a362 42/** Free list to speed up creation */
1bbbdabd 43static QSLIST_HEAD(, Coroutine) pool = QSLIST_HEAD_INITIALIZER(pool);
39a7a362
AK
44static unsigned int pool_size;
45
00dccaf1
KW
46typedef struct {
47 Coroutine base;
48 void *stack;
6ab7e546 49 sigjmp_buf env;
3f4349dc
KW
50
51#ifdef CONFIG_VALGRIND_H
52 unsigned int valgrind_stack_id;
53#endif
54
00dccaf1
KW
55} CoroutineUContext;
56
57/**
58 * Per-thread coroutine bookkeeping
59 */
60typedef struct {
61 /** Currently executing coroutine */
62 Coroutine *current;
63
00dccaf1
KW
64 /** The default coroutine */
65 CoroutineUContext leader;
66} CoroutineThreadState;
67
68static pthread_key_t thread_state_key;
69
70/*
71 * va_args to makecontext() must be type 'int', so passing
72 * the pointer we need may require several int args. This
73 * union is a quick hack to let us do that
74 */
75union cc_arg {
76 void *p;
77 int i[2];
78};
79
80static CoroutineThreadState *coroutine_get_thread_state(void)
81{
82 CoroutineThreadState *s = pthread_getspecific(thread_state_key);
83
84 if (!s) {
7267c094 85 s = g_malloc0(sizeof(*s));
00dccaf1 86 s->current = &s->leader.base;
00dccaf1
KW
87 pthread_setspecific(thread_state_key, s);
88 }
89 return s;
90}
91
92static void qemu_coroutine_thread_cleanup(void *opaque)
93{
94 CoroutineThreadState *s = opaque;
39a7a362
AK
95
96 g_free(s);
97}
98
99static void __attribute__((destructor)) coroutine_cleanup(void)
100{
00dccaf1
KW
101 Coroutine *co;
102 Coroutine *tmp;
103
1bbbdabd 104 QSLIST_FOREACH_SAFE(co, &pool, pool_next, tmp) {
7267c094
AL
105 g_free(DO_UPCAST(CoroutineUContext, base, co)->stack);
106 g_free(co);
00dccaf1 107 }
00dccaf1
KW
108}
109
110static void __attribute__((constructor)) coroutine_init(void)
111{
112 int ret;
113
114 ret = pthread_key_create(&thread_state_key, qemu_coroutine_thread_cleanup);
115 if (ret != 0) {
116 fprintf(stderr, "unable to create leader key: %s\n", strerror(errno));
117 abort();
118 }
119}
120
121static void coroutine_trampoline(int i0, int i1)
122{
123 union cc_arg arg;
124 CoroutineUContext *self;
125 Coroutine *co;
126
127 arg.i[0] = i0;
128 arg.i[1] = i1;
129 self = arg.p;
130 co = &self->base;
131
132 /* Initialize longjmp environment and switch back the caller */
6ab7e546
PM
133 if (!sigsetjmp(self->env, 0)) {
134 siglongjmp(*(sigjmp_buf *)co->entry_arg, 1);
00dccaf1
KW
135 }
136
137 while (true) {
138 co->entry(co->entry_arg);
139 qemu_coroutine_switch(co, co->caller, COROUTINE_TERMINATE);
140 }
141}
142
143static Coroutine *coroutine_new(void)
144{
145 const size_t stack_size = 1 << 20;
146 CoroutineUContext *co;
147 ucontext_t old_uc, uc;
6ab7e546 148 sigjmp_buf old_env;
32b74677 149 union cc_arg arg = {0};
00dccaf1 150
6ab7e546
PM
151 /* The ucontext functions preserve signal masks which incurs a
152 * system call overhead. sigsetjmp(buf, 0)/siglongjmp() does not
153 * preserve signal masks but only works on the current stack.
154 * Since we need a way to create and switch to a new stack, use
155 * the ucontext functions for that but sigsetjmp()/siglongjmp() for
156 * everything else.
00dccaf1
KW
157 */
158
159 if (getcontext(&uc) == -1) {
160 abort();
161 }
162
7267c094
AL
163 co = g_malloc0(sizeof(*co));
164 co->stack = g_malloc(stack_size);
00dccaf1
KW
165 co->base.entry_arg = &old_env; /* stash away our jmp_buf */
166
167 uc.uc_link = &old_uc;
168 uc.uc_stack.ss_sp = co->stack;
169 uc.uc_stack.ss_size = stack_size;
170 uc.uc_stack.ss_flags = 0;
171
3f4349dc
KW
172#ifdef CONFIG_VALGRIND_H
173 co->valgrind_stack_id =
174 VALGRIND_STACK_REGISTER(co->stack, co->stack + stack_size);
175#endif
176
00dccaf1
KW
177 arg.p = co;
178
179 makecontext(&uc, (void (*)(void))coroutine_trampoline,
180 2, arg.i[0], arg.i[1]);
181
6ab7e546
PM
182 /* swapcontext() in, siglongjmp() back out */
183 if (!sigsetjmp(old_env, 0)) {
00dccaf1
KW
184 swapcontext(&old_uc, &uc);
185 }
186 return &co->base;
187}
188
189Coroutine *qemu_coroutine_new(void)
190{
00dccaf1
KW
191 Coroutine *co;
192
1bbbdabd 193 co = QSLIST_FIRST(&pool);
00dccaf1 194 if (co) {
1bbbdabd 195 QSLIST_REMOVE_HEAD(&pool, pool_next);
39a7a362 196 pool_size--;
00dccaf1
KW
197 } else {
198 co = coroutine_new();
199 }
200 return co;
201}
202
3f4349dc 203#ifdef CONFIG_VALGRIND_H
cc6e3ca9 204#ifdef CONFIG_PRAGMA_DIAGNOSTIC_AVAILABLE
3f4349dc
KW
205/* Work around an unused variable in the valgrind.h macro... */
206#pragma GCC diagnostic ignored "-Wunused-but-set-variable"
06d71fa1 207#endif
3f4349dc
KW
208static inline void valgrind_stack_deregister(CoroutineUContext *co)
209{
210 VALGRIND_STACK_DEREGISTER(co->valgrind_stack_id);
211}
cc6e3ca9 212#ifdef CONFIG_PRAGMA_DIAGNOSTIC_AVAILABLE
3f4349dc
KW
213#pragma GCC diagnostic error "-Wunused-but-set-variable"
214#endif
06d71fa1 215#endif
3f4349dc 216
00dccaf1
KW
217void qemu_coroutine_delete(Coroutine *co_)
218{
00dccaf1
KW
219 CoroutineUContext *co = DO_UPCAST(CoroutineUContext, base, co_);
220
39a7a362 221 if (pool_size < POOL_MAX_SIZE) {
1bbbdabd 222 QSLIST_INSERT_HEAD(&pool, &co->base, pool_next);
00dccaf1 223 co->base.caller = NULL;
39a7a362 224 pool_size++;
00dccaf1
KW
225 return;
226 }
227
3f4349dc
KW
228#ifdef CONFIG_VALGRIND_H
229 valgrind_stack_deregister(co);
230#endif
231
7267c094
AL
232 g_free(co->stack);
233 g_free(co);
00dccaf1
KW
234}
235
236CoroutineAction qemu_coroutine_switch(Coroutine *from_, Coroutine *to_,
237 CoroutineAction action)
238{
239 CoroutineUContext *from = DO_UPCAST(CoroutineUContext, base, from_);
240 CoroutineUContext *to = DO_UPCAST(CoroutineUContext, base, to_);
241 CoroutineThreadState *s = coroutine_get_thread_state();
242 int ret;
243
244 s->current = to_;
245
6ab7e546 246 ret = sigsetjmp(from->env, 0);
00dccaf1 247 if (ret == 0) {
6ab7e546 248 siglongjmp(to->env, action);
00dccaf1
KW
249 }
250 return ret;
251}
252
253Coroutine *qemu_coroutine_self(void)
254{
255 CoroutineThreadState *s = coroutine_get_thread_state();
256
257 return s->current;
258}
259
260bool qemu_in_coroutine(void)
261{
262 CoroutineThreadState *s = pthread_getspecific(thread_state_key);
263
264 return s && s->current->caller;
265}