23
#define _FORTIFY_SOURCE 0
25
#include "qemu/osdep.h"
27
#include "qemu/coroutine_int.h"
28
#include "qemu/coroutine-tls.h"
30
#ifdef CONFIG_VALGRIND_H
31
#include <valgrind/valgrind.h>
34
#ifdef QEMU_SANITIZE_ADDRESS
35
#ifdef CONFIG_ASAN_IFACE_FIBER
37
#include <sanitizer/asan_interface.h>
42
#include <sanitizer/tsan_interface.h>
49
#ifdef CONFIG_SAFESTACK
52
size_t unsafe_stack_size;
58
void *tsan_caller_fiber;
61
#ifdef CONFIG_VALGRIND_H
62
unsigned int valgrind_stack_id;
70
QEMU_DEFINE_STATIC_CO_TLS(Coroutine *, current);
71
QEMU_DEFINE_STATIC_CO_TLS(CoroutineUContext, leader);
87
static inline __attribute__((always_inline))
88
void on_new_fiber(CoroutineUContext *co)
91
co->tsan_co_fiber = __tsan_create_fiber(0);
92
co->tsan_caller_fiber = __tsan_get_current_fiber();
97
static inline __attribute__((always_inline))
98
void finish_switch_fiber(void *fake_stack_save)
101
CoroutineUContext *leaderp = get_ptr_leader();
102
const void *bottom_old;
105
__sanitizer_finish_switch_fiber(fake_stack_save, &bottom_old, &size_old);
107
if (!leaderp->stack) {
108
leaderp->stack = (void *)bottom_old;
109
leaderp->stack_size = size_old;
113
if (fake_stack_save) {
114
__tsan_release(fake_stack_save);
115
__tsan_switch_to_fiber(fake_stack_save, 0);
121
static inline __attribute__((always_inline))
122
void start_switch_fiber_asan(void **fake_stack_save,
123
const void *bottom, size_t size)
126
__sanitizer_start_switch_fiber(fake_stack_save, bottom, size);
131
static inline __attribute__((always_inline))
132
void start_switch_fiber_tsan(void **fake_stack_save,
133
CoroutineUContext *co,
137
void *new_fiber = caller ?
138
co->tsan_caller_fiber :
140
void *curr_fiber = __tsan_get_current_fiber();
141
__tsan_acquire(curr_fiber);
143
*fake_stack_save = curr_fiber;
144
__tsan_switch_to_fiber(new_fiber, 0);
148
static void coroutine_trampoline(int i0, int i1)
151
CoroutineUContext *self;
153
void *fake_stack_save = NULL;
155
finish_switch_fiber(NULL);
163
if (!sigsetjmp(self->env, 0)) {
164
CoroutineUContext *leaderp = get_ptr_leader();
166
start_switch_fiber_asan(&fake_stack_save,
167
leaderp->stack, leaderp->stack_size);
168
start_switch_fiber_tsan(&fake_stack_save, self, true);
169
siglongjmp(*(sigjmp_buf *)co->entry_arg, 1);
172
finish_switch_fiber(fake_stack_save);
175
co->entry(co->entry_arg);
176
qemu_coroutine_switch(co, co->caller, COROUTINE_TERMINATE);
180
Coroutine *qemu_coroutine_new(void)
182
CoroutineUContext *co;
183
ucontext_t old_uc, uc;
185
union cc_arg arg = {0};
186
void *fake_stack_save = NULL;
196
if (getcontext(&uc) == -1) {
200
co = g_malloc0(sizeof(*co));
201
co->stack_size = COROUTINE_STACK_SIZE;
202
co->stack = qemu_alloc_stack(&co->stack_size);
203
#ifdef CONFIG_SAFESTACK
204
co->unsafe_stack_size = COROUTINE_STACK_SIZE;
205
co->unsafe_stack = qemu_alloc_stack(&co->unsafe_stack_size);
207
co->base.entry_arg = &old_env;
209
uc.uc_link = &old_uc;
210
uc.uc_stack.ss_sp = co->stack;
211
uc.uc_stack.ss_size = co->stack_size;
212
uc.uc_stack.ss_flags = 0;
214
#ifdef CONFIG_VALGRIND_H
215
co->valgrind_stack_id =
216
VALGRIND_STACK_REGISTER(co->stack, co->stack + co->stack_size);
222
makecontext(&uc, (void (*)(void))coroutine_trampoline,
223
2, arg.i[0], arg.i[1]);
226
if (!sigsetjmp(old_env, 0)) {
227
start_switch_fiber_asan(&fake_stack_save, co->stack, co->stack_size);
228
start_switch_fiber_tsan(&fake_stack_save,
231
#ifdef CONFIG_SAFESTACK
242
void *usp = co->unsafe_stack + co->unsafe_stack_size;
243
__safestack_unsafe_stack_ptr = usp;
246
swapcontext(&old_uc, &uc);
249
finish_switch_fiber(fake_stack_save);
254
#ifdef CONFIG_VALGRIND_H
256
#if !defined(__clang__)
257
#pragma GCC diagnostic push
258
#pragma GCC diagnostic ignored "-Wunused-but-set-variable"
260
static inline void valgrind_stack_deregister(CoroutineUContext *co)
262
VALGRIND_STACK_DEREGISTER(co->valgrind_stack_id);
264
#if !defined(__clang__)
265
#pragma GCC diagnostic pop
269
#if defined(CONFIG_ASAN) && defined(CONFIG_COROUTINE_POOL)
270
static void coroutine_fn terminate_asan(void *opaque)
272
CoroutineUContext *to = DO_UPCAST(CoroutineUContext, base, opaque);
275
start_switch_fiber_asan(NULL, to->stack, to->stack_size);
276
G_STATIC_ASSERT(!IS_ENABLED(CONFIG_TSAN));
277
siglongjmp(to->env, COROUTINE_ENTER);
281
void qemu_coroutine_delete(Coroutine *co_)
283
CoroutineUContext *co = DO_UPCAST(CoroutineUContext, base, co_);
285
#if defined(CONFIG_ASAN) && defined(CONFIG_COROUTINE_POOL)
286
co_->entry_arg = qemu_coroutine_self();
287
co_->entry = terminate_asan;
288
qemu_coroutine_switch(co_->entry_arg, co_, COROUTINE_ENTER);
291
#ifdef CONFIG_VALGRIND_H
292
valgrind_stack_deregister(co);
295
qemu_free_stack(co->stack, co->stack_size);
296
#ifdef CONFIG_SAFESTACK
297
qemu_free_stack(co->unsafe_stack, co->unsafe_stack_size);
310
CoroutineAction __attribute__((noinline))
311
qemu_coroutine_switch(Coroutine *from_, Coroutine *to_,
312
CoroutineAction action)
314
CoroutineUContext *from = DO_UPCAST(CoroutineUContext, base, from_);
315
CoroutineUContext *to = DO_UPCAST(CoroutineUContext, base, to_);
317
void *fake_stack_save = NULL;
321
ret = sigsetjmp(from->env, 0);
323
start_switch_fiber_asan(IS_ENABLED(CONFIG_COROUTINE_POOL) ||
324
action != COROUTINE_TERMINATE ?
325
&fake_stack_save : NULL,
326
to->stack, to->stack_size);
327
start_switch_fiber_tsan(&fake_stack_save,
329
siglongjmp(to->env, action);
332
finish_switch_fiber(fake_stack_save);
337
Coroutine *qemu_coroutine_self(void)
339
Coroutine *self = get_current();
340
CoroutineUContext *leaderp = get_ptr_leader();
343
self = &leaderp->base;
347
if (!leaderp->tsan_co_fiber) {
348
leaderp->tsan_co_fiber = __tsan_get_current_fiber();
354
bool qemu_in_coroutine(void)
356
Coroutine *self = get_current();
358
return self && self->caller;