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