1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22#ifdef _FORTIFY_SOURCE
23#undef _FORTIFY_SOURCE
24#endif
25#include "qemu/osdep.h"
26#include <ucontext.h>
27#include "qemu/coroutine_int.h"
28#include "qemu/coroutine-tls.h"
29
30#ifdef CONFIG_VALGRIND_H
31#include <valgrind/valgrind.h>
32#endif
33
34#ifdef QEMU_SANITIZE_ADDRESS
35#ifdef CONFIG_ASAN_IFACE_FIBER
36#define CONFIG_ASAN 1
37#include <sanitizer/asan_interface.h>
38#endif
39#endif
40
41#ifdef CONFIG_TSAN
42#include <sanitizer/tsan_interface.h>
43#endif
44
45typedef struct {
46 Coroutine base;
47 void *stack;
48 size_t stack_size;
49#ifdef CONFIG_SAFESTACK
50
51 void *unsafe_stack;
52 size_t unsafe_stack_size;
53#endif
54 sigjmp_buf env;
55
56#ifdef CONFIG_TSAN
57 void *tsan_co_fiber;
58 void *tsan_caller_fiber;
59#endif
60
61#ifdef CONFIG_VALGRIND_H
62 unsigned int valgrind_stack_id;
63#endif
64
65} CoroutineUContext;
66
67
68
69
70QEMU_DEFINE_STATIC_CO_TLS(Coroutine *, current);
71QEMU_DEFINE_STATIC_CO_TLS(CoroutineUContext, leader);
72
73
74
75
76
77
78union cc_arg {
79 void *p;
80 int i[2];
81};
82
83
84
85
86
87static inline __attribute__((always_inline))
88void on_new_fiber(CoroutineUContext *co)
89{
90#ifdef CONFIG_TSAN
91 co->tsan_co_fiber = __tsan_create_fiber(0);
92 co->tsan_caller_fiber = __tsan_get_current_fiber();
93#endif
94}
95
96
97static inline __attribute__((always_inline))
98void finish_switch_fiber(void *fake_stack_save)
99{
100#ifdef CONFIG_ASAN
101 CoroutineUContext *leaderp = get_ptr_leader();
102 const void *bottom_old;
103 size_t size_old;
104
105 __sanitizer_finish_switch_fiber(fake_stack_save, &bottom_old, &size_old);
106
107 if (!leaderp->stack) {
108 leaderp->stack = (void *)bottom_old;
109 leaderp->stack_size = size_old;
110 }
111#endif
112#ifdef CONFIG_TSAN
113 if (fake_stack_save) {
114 __tsan_release(fake_stack_save);
115 __tsan_switch_to_fiber(fake_stack_save, 0);
116 }
117#endif
118}
119
120
121static inline __attribute__((always_inline))
122void start_switch_fiber_asan(CoroutineAction action, void **fake_stack_save,
123 const void *bottom, size_t size)
124{
125#ifdef CONFIG_ASAN
126 __sanitizer_start_switch_fiber(
127 action == COROUTINE_TERMINATE ? NULL : fake_stack_save,
128 bottom, size);
129#endif
130}
131
132
133static inline __attribute__((always_inline))
134void start_switch_fiber_tsan(void **fake_stack_save,
135 CoroutineUContext *co,
136 bool caller)
137{
138#ifdef CONFIG_TSAN
139 void *new_fiber = caller ?
140 co->tsan_caller_fiber :
141 co->tsan_co_fiber;
142 void *curr_fiber = __tsan_get_current_fiber();
143 __tsan_acquire(curr_fiber);
144
145 *fake_stack_save = curr_fiber;
146 __tsan_switch_to_fiber(new_fiber, 0);
147#endif
148}
149
150static void coroutine_trampoline(int i0, int i1)
151{
152 union cc_arg arg;
153 CoroutineUContext *self;
154 Coroutine *co;
155 void *fake_stack_save = NULL;
156
157 finish_switch_fiber(NULL);
158
159 arg.i[0] = i0;
160 arg.i[1] = i1;
161 self = arg.p;
162 co = &self->base;
163
164
165 if (!sigsetjmp(self->env, 0)) {
166 CoroutineUContext *leaderp = get_ptr_leader();
167
168 start_switch_fiber_asan(COROUTINE_YIELD, &fake_stack_save,
169 leaderp->stack, leaderp->stack_size);
170 start_switch_fiber_tsan(&fake_stack_save, self, true);
171 siglongjmp(*(sigjmp_buf *)co->entry_arg, 1);
172 }
173
174 finish_switch_fiber(fake_stack_save);
175
176 while (true) {
177 co->entry(co->entry_arg);
178 qemu_coroutine_switch(co, co->caller, COROUTINE_TERMINATE);
179 }
180}
181
182Coroutine *qemu_coroutine_new(void)
183{
184 CoroutineUContext *co;
185 ucontext_t old_uc, uc;
186 sigjmp_buf old_env;
187 union cc_arg arg = {0};
188 void *fake_stack_save = NULL;
189
190
191
192
193
194
195
196
197
198 if (getcontext(&uc) == -1) {
199 abort();
200 }
201
202 co = g_malloc0(sizeof(*co));
203 co->stack_size = COROUTINE_STACK_SIZE;
204 co->stack = qemu_alloc_stack(&co->stack_size);
205#ifdef CONFIG_SAFESTACK
206 co->unsafe_stack_size = COROUTINE_STACK_SIZE;
207 co->unsafe_stack = qemu_alloc_stack(&co->unsafe_stack_size);
208#endif
209 co->base.entry_arg = &old_env;
210
211 uc.uc_link = &old_uc;
212 uc.uc_stack.ss_sp = co->stack;
213 uc.uc_stack.ss_size = co->stack_size;
214 uc.uc_stack.ss_flags = 0;
215
216#ifdef CONFIG_VALGRIND_H
217 co->valgrind_stack_id =
218 VALGRIND_STACK_REGISTER(co->stack, co->stack + co->stack_size);
219#endif
220
221 arg.p = co;
222
223 on_new_fiber(co);
224 makecontext(&uc, (void (*)(void))coroutine_trampoline,
225 2, arg.i[0], arg.i[1]);
226
227
228 if (!sigsetjmp(old_env, 0)) {
229 start_switch_fiber_asan(COROUTINE_YIELD, &fake_stack_save, co->stack,
230 co->stack_size);
231 start_switch_fiber_tsan(&fake_stack_save,
232 co, false);
233
234#ifdef CONFIG_SAFESTACK
235
236
237
238
239
240
241
242
243
244
245 void *usp = co->unsafe_stack + co->unsafe_stack_size;
246 __safestack_unsafe_stack_ptr = usp;
247#endif
248
249 swapcontext(&old_uc, &uc);
250 }
251
252 finish_switch_fiber(fake_stack_save);
253
254 return &co->base;
255}
256
257#ifdef CONFIG_VALGRIND_H
258
259#if !defined(__clang__)
260#pragma GCC diagnostic push
261#pragma GCC diagnostic ignored "-Wunused-but-set-variable"
262#endif
263static inline void valgrind_stack_deregister(CoroutineUContext *co)
264{
265 VALGRIND_STACK_DEREGISTER(co->valgrind_stack_id);
266}
267#if !defined(__clang__)
268#pragma GCC diagnostic pop
269#endif
270#endif
271
272void qemu_coroutine_delete(Coroutine *co_)
273{
274 CoroutineUContext *co = DO_UPCAST(CoroutineUContext, base, co_);
275
276#ifdef CONFIG_VALGRIND_H
277 valgrind_stack_deregister(co);
278#endif
279
280 qemu_free_stack(co->stack, co->stack_size);
281#ifdef CONFIG_SAFESTACK
282 qemu_free_stack(co->unsafe_stack, co->unsafe_stack_size);
283#endif
284 g_free(co);
285}
286
287
288
289
290
291
292
293
294
295CoroutineAction __attribute__((noinline))
296qemu_coroutine_switch(Coroutine *from_, Coroutine *to_,
297 CoroutineAction action)
298{
299 CoroutineUContext *from = DO_UPCAST(CoroutineUContext, base, from_);
300 CoroutineUContext *to = DO_UPCAST(CoroutineUContext, base, to_);
301 int ret;
302 void *fake_stack_save = NULL;
303
304 set_current(to_);
305
306 ret = sigsetjmp(from->env, 0);
307 if (ret == 0) {
308 start_switch_fiber_asan(action, &fake_stack_save, to->stack,
309 to->stack_size);
310 start_switch_fiber_tsan(&fake_stack_save,
311 to, false);
312 siglongjmp(to->env, action);
313 }
314
315 finish_switch_fiber(fake_stack_save);
316
317 return ret;
318}
319
320Coroutine *qemu_coroutine_self(void)
321{
322 Coroutine *self = get_current();
323 CoroutineUContext *leaderp = get_ptr_leader();
324
325 if (!self) {
326 self = &leaderp->base;
327 set_current(self);
328 }
329#ifdef CONFIG_TSAN
330 if (!leaderp->tsan_co_fiber) {
331 leaderp->tsan_co_fiber = __tsan_get_current_fiber();
332 }
333#endif
334 return self;
335}
336
337bool qemu_in_coroutine(void)
338{
339 Coroutine *self = get_current();
340
341 return self && self->caller;
342}
343