]> git.proxmox.com Git - qemu.git/blob - coroutine-ucontext.c
6f8ffa85e3f9e2320348fc057e1293cc2a01c2dc
[qemu.git] / coroutine-ucontext.c
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"
31 #include "block/coroutine_int.h"
32
33 #ifdef CONFIG_VALGRIND_H
34 #include <valgrind/valgrind.h>
35 #endif
36
37 typedef struct {
38 Coroutine base;
39 void *stack;
40 jmp_buf env;
41
42 #ifdef CONFIG_VALGRIND_H
43 unsigned int valgrind_stack_id;
44 #endif
45
46 } CoroutineUContext;
47
48 /**
49 * Per-thread coroutine bookkeeping
50 */
51 typedef struct {
52 /** Currently executing coroutine */
53 Coroutine *current;
54
55 /** The default coroutine */
56 CoroutineUContext leader;
57 } CoroutineThreadState;
58
59 static pthread_key_t thread_state_key;
60
61 /*
62 * va_args to makecontext() must be type 'int', so passing
63 * the pointer we need may require several int args. This
64 * union is a quick hack to let us do that
65 */
66 union cc_arg {
67 void *p;
68 int i[2];
69 };
70
71 static CoroutineThreadState *coroutine_get_thread_state(void)
72 {
73 CoroutineThreadState *s = pthread_getspecific(thread_state_key);
74
75 if (!s) {
76 s = g_malloc0(sizeof(*s));
77 s->current = &s->leader.base;
78 pthread_setspecific(thread_state_key, s);
79 }
80 return s;
81 }
82
83 static void qemu_coroutine_thread_cleanup(void *opaque)
84 {
85 CoroutineThreadState *s = opaque;
86
87 g_free(s);
88 }
89
90 static void __attribute__((constructor)) coroutine_init(void)
91 {
92 int ret;
93
94 ret = pthread_key_create(&thread_state_key, qemu_coroutine_thread_cleanup);
95 if (ret != 0) {
96 fprintf(stderr, "unable to create leader key: %s\n", strerror(errno));
97 abort();
98 }
99 }
100
101 static void coroutine_trampoline(int i0, int i1)
102 {
103 union cc_arg arg;
104 CoroutineUContext *self;
105 Coroutine *co;
106
107 arg.i[0] = i0;
108 arg.i[1] = i1;
109 self = arg.p;
110 co = &self->base;
111
112 /* Initialize longjmp environment and switch back the caller */
113 if (!setjmp(self->env)) {
114 longjmp(*(jmp_buf *)co->entry_arg, 1);
115 }
116
117 while (true) {
118 co->entry(co->entry_arg);
119 qemu_coroutine_switch(co, co->caller, COROUTINE_TERMINATE);
120 }
121 }
122
123 Coroutine *qemu_coroutine_new(void)
124 {
125 const size_t stack_size = 1 << 20;
126 CoroutineUContext *co;
127 ucontext_t old_uc, uc;
128 jmp_buf old_env;
129 union cc_arg arg = {0};
130
131 /* The ucontext functions preserve signal masks which incurs a system call
132 * overhead. setjmp()/longjmp() does not preserve signal masks but only
133 * works on the current stack. Since we need a way to create and switch to
134 * a new stack, use the ucontext functions for that but setjmp()/longjmp()
135 * for everything else.
136 */
137
138 if (getcontext(&uc) == -1) {
139 abort();
140 }
141
142 co = g_malloc0(sizeof(*co));
143 co->stack = g_malloc(stack_size);
144 co->base.entry_arg = &old_env; /* stash away our jmp_buf */
145
146 uc.uc_link = &old_uc;
147 uc.uc_stack.ss_sp = co->stack;
148 uc.uc_stack.ss_size = stack_size;
149 uc.uc_stack.ss_flags = 0;
150
151 #ifdef CONFIG_VALGRIND_H
152 co->valgrind_stack_id =
153 VALGRIND_STACK_REGISTER(co->stack, co->stack + stack_size);
154 #endif
155
156 arg.p = co;
157
158 makecontext(&uc, (void (*)(void))coroutine_trampoline,
159 2, arg.i[0], arg.i[1]);
160
161 /* swapcontext() in, longjmp() back out */
162 if (!setjmp(old_env)) {
163 swapcontext(&old_uc, &uc);
164 }
165 return &co->base;
166 }
167
168 #ifdef CONFIG_VALGRIND_H
169 #ifdef CONFIG_PRAGMA_DIAGNOSTIC_AVAILABLE
170 /* Work around an unused variable in the valgrind.h macro... */
171 #pragma GCC diagnostic ignored "-Wunused-but-set-variable"
172 #endif
173 static inline void valgrind_stack_deregister(CoroutineUContext *co)
174 {
175 VALGRIND_STACK_DEREGISTER(co->valgrind_stack_id);
176 }
177 #ifdef CONFIG_PRAGMA_DIAGNOSTIC_AVAILABLE
178 #pragma GCC diagnostic error "-Wunused-but-set-variable"
179 #endif
180 #endif
181
182 void qemu_coroutine_delete(Coroutine *co_)
183 {
184 CoroutineUContext *co = DO_UPCAST(CoroutineUContext, base, co_);
185
186 #ifdef CONFIG_VALGRIND_H
187 valgrind_stack_deregister(co);
188 #endif
189
190 g_free(co->stack);
191 g_free(co);
192 }
193
194 CoroutineAction qemu_coroutine_switch(Coroutine *from_, Coroutine *to_,
195 CoroutineAction action)
196 {
197 CoroutineUContext *from = DO_UPCAST(CoroutineUContext, base, from_);
198 CoroutineUContext *to = DO_UPCAST(CoroutineUContext, base, to_);
199 CoroutineThreadState *s = coroutine_get_thread_state();
200 int ret;
201
202 s->current = to_;
203
204 ret = setjmp(from->env);
205 if (ret == 0) {
206 longjmp(to->env, action);
207 }
208 return ret;
209 }
210
211 Coroutine *qemu_coroutine_self(void)
212 {
213 CoroutineThreadState *s = coroutine_get_thread_state();
214
215 return s->current;
216 }
217
218 bool qemu_in_coroutine(void)
219 {
220 CoroutineThreadState *s = pthread_getspecific(thread_state_key);
221
222 return s && s->current->caller;
223 }