1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25#ifdef _FORTIFY_SOURCE
26#undef _FORTIFY_SOURCE
27#endif
28#include <stdlib.h>
29#include <setjmp.h>
30#include <stdint.h>
31#include <pthread.h>
32#include <signal.h>
33#include "qemu-common.h"
34#include "block/coroutine_int.h"
35
36typedef struct {
37 Coroutine base;
38 void *stack;
39 sigjmp_buf env;
40} CoroutineUContext;
41
42
43
44
45typedef struct {
46
47 Coroutine *current;
48
49
50 CoroutineUContext leader;
51
52
53 sigjmp_buf tr_reenter;
54 volatile sig_atomic_t tr_called;
55 void *tr_handler;
56} CoroutineThreadState;
57
58static pthread_key_t thread_state_key;
59
60static CoroutineThreadState *coroutine_get_thread_state(void)
61{
62 CoroutineThreadState *s = pthread_getspecific(thread_state_key);
63
64 if (!s) {
65 s = g_malloc0(sizeof(*s));
66 s->current = &s->leader.base;
67 pthread_setspecific(thread_state_key, s);
68 }
69 return s;
70}
71
72static void qemu_coroutine_thread_cleanup(void *opaque)
73{
74 CoroutineThreadState *s = opaque;
75
76 g_free(s);
77}
78
79static void __attribute__((constructor)) coroutine_init(void)
80{
81 int ret;
82
83 ret = pthread_key_create(&thread_state_key, qemu_coroutine_thread_cleanup);
84 if (ret != 0) {
85 fprintf(stderr, "unable to create leader key: %s\n", strerror(errno));
86 abort();
87 }
88}
89
90
91
92
93
94
95static void coroutine_bootstrap(CoroutineUContext *self, Coroutine *co)
96{
97
98 if (!sigsetjmp(self->env, 0)) {
99 siglongjmp(*(sigjmp_buf *)co->entry_arg, 1);
100 }
101
102 while (true) {
103 co->entry(co->entry_arg);
104 qemu_coroutine_switch(co, co->caller, COROUTINE_TERMINATE);
105 }
106}
107
108
109
110
111
112
113static void coroutine_trampoline(int signal)
114{
115 CoroutineUContext *self;
116 Coroutine *co;
117 CoroutineThreadState *coTS;
118
119
120 coTS = coroutine_get_thread_state();
121 self = coTS->tr_handler;
122 coTS->tr_called = 1;
123 co = &self->base;
124
125
126
127
128
129
130 if (!sigsetjmp(coTS->tr_reenter, 0)) {
131 return;
132 }
133
134
135
136
137
138
139
140
141
142
143
144 coroutine_bootstrap(self, co);
145}
146
147Coroutine *qemu_coroutine_new(void)
148{
149 const size_t stack_size = 1 << 20;
150 CoroutineUContext *co;
151 CoroutineThreadState *coTS;
152 struct sigaction sa;
153 struct sigaction osa;
154 stack_t ss;
155 stack_t oss;
156 sigset_t sigs;
157 sigset_t osigs;
158 jmp_buf old_env;
159
160
161
162
163
164
165
166
167
168
169 co = g_malloc0(sizeof(*co));
170 co->stack = g_malloc(stack_size);
171 co->base.entry_arg = &old_env;
172
173 coTS = coroutine_get_thread_state();
174 coTS->tr_handler = co;
175
176
177
178
179
180
181 sigemptyset(&sigs);
182 sigaddset(&sigs, SIGUSR2);
183 pthread_sigmask(SIG_BLOCK, &sigs, &osigs);
184 sa.sa_handler = coroutine_trampoline;
185 sigfillset(&sa.sa_mask);
186 sa.sa_flags = SA_ONSTACK;
187 if (sigaction(SIGUSR2, &sa, &osa) != 0) {
188 abort();
189 }
190
191
192
193
194 ss.ss_sp = co->stack;
195 ss.ss_size = stack_size;
196 ss.ss_flags = 0;
197 if (sigaltstack(&ss, &oss) < 0) {
198 abort();
199 }
200
201
202
203
204
205
206
207
208 coTS->tr_called = 0;
209 pthread_kill(pthread_self(), SIGUSR2);
210 sigfillset(&sigs);
211 sigdelset(&sigs, SIGUSR2);
212 while (!coTS->tr_called) {
213 sigsuspend(&sigs);
214 }
215
216
217
218
219
220
221 sigaltstack(NULL, &ss);
222 ss.ss_flags = SS_DISABLE;
223 if (sigaltstack(&ss, NULL) < 0) {
224 abort();
225 }
226 sigaltstack(NULL, &ss);
227 if (!(oss.ss_flags & SS_DISABLE)) {
228 sigaltstack(&oss, NULL);
229 }
230
231
232
233
234 sigaction(SIGUSR2, &osa, NULL);
235 pthread_sigmask(SIG_SETMASK, &osigs, NULL);
236
237
238
239
240
241
242
243
244 if (!sigsetjmp(old_env, 0)) {
245 siglongjmp(coTS->tr_reenter, 1);
246 }
247
248
249
250
251
252 return &co->base;
253}
254
255void qemu_coroutine_delete(Coroutine *co_)
256{
257 CoroutineUContext *co = DO_UPCAST(CoroutineUContext, base, co_);
258
259 g_free(co->stack);
260 g_free(co);
261}
262
263CoroutineAction qemu_coroutine_switch(Coroutine *from_, Coroutine *to_,
264 CoroutineAction action)
265{
266 CoroutineUContext *from = DO_UPCAST(CoroutineUContext, base, from_);
267 CoroutineUContext *to = DO_UPCAST(CoroutineUContext, base, to_);
268 CoroutineThreadState *s = coroutine_get_thread_state();
269 int ret;
270
271 s->current = to_;
272
273 ret = sigsetjmp(from->env, 0);
274 if (ret == 0) {
275 siglongjmp(to->env, action);
276 }
277 return ret;
278}
279
280Coroutine *qemu_coroutine_self(void)
281{
282 CoroutineThreadState *s = coroutine_get_thread_state();
283
284 return s->current;
285}
286
287bool qemu_in_coroutine(void)
288{
289 CoroutineThreadState *s = pthread_getspecific(thread_state_key);
290
291 return s && s->current->caller;
292}
293
294