2011-01-17 19:08:14 +03:00
|
|
|
/*
|
|
|
|
* ucontext coroutine initialization code
|
|
|
|
*
|
|
|
|
* Copyright (C) 2006 Anthony Liguori <anthony@codemonkey.ws>
|
|
|
|
* Copyright (C) 2011 Kevin Wolf <kwolf@redhat.com>
|
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.0 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with this library; if not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* XXX Is there a nicer way to disable glibc's stack check for longjmp? */
|
|
|
|
#ifdef _FORTIFY_SOURCE
|
|
|
|
#undef _FORTIFY_SOURCE
|
|
|
|
#endif
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <setjmp.h>
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <pthread.h>
|
|
|
|
#include <ucontext.h>
|
|
|
|
#include "qemu-common.h"
|
|
|
|
#include "qemu-coroutine-int.h"
|
|
|
|
|
2012-06-29 15:40:27 +04:00
|
|
|
#ifdef CONFIG_VALGRIND_H
|
|
|
|
#include <valgrind/valgrind.h>
|
|
|
|
#endif
|
|
|
|
|
2011-01-17 19:08:14 +03:00
|
|
|
enum {
|
|
|
|
/* Maximum free pool size prevents holding too many freed coroutines */
|
|
|
|
POOL_MAX_SIZE = 64,
|
|
|
|
};
|
|
|
|
|
2011-12-05 21:20:12 +04:00
|
|
|
/** Free list to speed up creation */
|
2012-01-13 20:34:04 +04:00
|
|
|
static QSLIST_HEAD(, Coroutine) pool = QSLIST_HEAD_INITIALIZER(pool);
|
2011-12-05 21:20:12 +04:00
|
|
|
static unsigned int pool_size;
|
|
|
|
|
2011-01-17 19:08:14 +03:00
|
|
|
typedef struct {
|
|
|
|
Coroutine base;
|
|
|
|
void *stack;
|
|
|
|
jmp_buf env;
|
2012-06-29 15:40:27 +04:00
|
|
|
|
|
|
|
#ifdef CONFIG_VALGRIND_H
|
|
|
|
unsigned int valgrind_stack_id;
|
|
|
|
#endif
|
|
|
|
|
2011-01-17 19:08:14 +03:00
|
|
|
} CoroutineUContext;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Per-thread coroutine bookkeeping
|
|
|
|
*/
|
|
|
|
typedef struct {
|
|
|
|
/** Currently executing coroutine */
|
|
|
|
Coroutine *current;
|
|
|
|
|
|
|
|
/** The default coroutine */
|
|
|
|
CoroutineUContext leader;
|
|
|
|
} CoroutineThreadState;
|
|
|
|
|
|
|
|
static pthread_key_t thread_state_key;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* va_args to makecontext() must be type 'int', so passing
|
|
|
|
* the pointer we need may require several int args. This
|
|
|
|
* union is a quick hack to let us do that
|
|
|
|
*/
|
|
|
|
union cc_arg {
|
|
|
|
void *p;
|
|
|
|
int i[2];
|
|
|
|
};
|
|
|
|
|
|
|
|
static CoroutineThreadState *coroutine_get_thread_state(void)
|
|
|
|
{
|
|
|
|
CoroutineThreadState *s = pthread_getspecific(thread_state_key);
|
|
|
|
|
|
|
|
if (!s) {
|
2011-08-21 07:09:37 +04:00
|
|
|
s = g_malloc0(sizeof(*s));
|
2011-01-17 19:08:14 +03:00
|
|
|
s->current = &s->leader.base;
|
|
|
|
pthread_setspecific(thread_state_key, s);
|
|
|
|
}
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void qemu_coroutine_thread_cleanup(void *opaque)
|
|
|
|
{
|
|
|
|
CoroutineThreadState *s = opaque;
|
2011-12-05 21:20:12 +04:00
|
|
|
|
|
|
|
g_free(s);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void __attribute__((destructor)) coroutine_cleanup(void)
|
|
|
|
{
|
2011-01-17 19:08:14 +03:00
|
|
|
Coroutine *co;
|
|
|
|
Coroutine *tmp;
|
|
|
|
|
2012-01-13 20:34:04 +04:00
|
|
|
QSLIST_FOREACH_SAFE(co, &pool, pool_next, tmp) {
|
2011-08-21 07:09:37 +04:00
|
|
|
g_free(DO_UPCAST(CoroutineUContext, base, co)->stack);
|
|
|
|
g_free(co);
|
2011-01-17 19:08:14 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void __attribute__((constructor)) coroutine_init(void)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
ret = pthread_key_create(&thread_state_key, qemu_coroutine_thread_cleanup);
|
|
|
|
if (ret != 0) {
|
|
|
|
fprintf(stderr, "unable to create leader key: %s\n", strerror(errno));
|
|
|
|
abort();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void coroutine_trampoline(int i0, int i1)
|
|
|
|
{
|
|
|
|
union cc_arg arg;
|
|
|
|
CoroutineUContext *self;
|
|
|
|
Coroutine *co;
|
|
|
|
|
|
|
|
arg.i[0] = i0;
|
|
|
|
arg.i[1] = i1;
|
|
|
|
self = arg.p;
|
|
|
|
co = &self->base;
|
|
|
|
|
|
|
|
/* Initialize longjmp environment and switch back the caller */
|
|
|
|
if (!setjmp(self->env)) {
|
|
|
|
longjmp(*(jmp_buf *)co->entry_arg, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
while (true) {
|
|
|
|
co->entry(co->entry_arg);
|
|
|
|
qemu_coroutine_switch(co, co->caller, COROUTINE_TERMINATE);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static Coroutine *coroutine_new(void)
|
|
|
|
{
|
|
|
|
const size_t stack_size = 1 << 20;
|
|
|
|
CoroutineUContext *co;
|
|
|
|
ucontext_t old_uc, uc;
|
|
|
|
jmp_buf old_env;
|
2011-08-08 13:46:51 +04:00
|
|
|
union cc_arg arg = {0};
|
2011-01-17 19:08:14 +03:00
|
|
|
|
|
|
|
/* The ucontext functions preserve signal masks which incurs a system call
|
|
|
|
* overhead. setjmp()/longjmp() does not preserve signal masks but only
|
|
|
|
* works on the current stack. Since we need a way to create and switch to
|
|
|
|
* a new stack, use the ucontext functions for that but setjmp()/longjmp()
|
|
|
|
* for everything else.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (getcontext(&uc) == -1) {
|
|
|
|
abort();
|
|
|
|
}
|
|
|
|
|
2011-08-21 07:09:37 +04:00
|
|
|
co = g_malloc0(sizeof(*co));
|
|
|
|
co->stack = g_malloc(stack_size);
|
2011-01-17 19:08:14 +03:00
|
|
|
co->base.entry_arg = &old_env; /* stash away our jmp_buf */
|
|
|
|
|
|
|
|
uc.uc_link = &old_uc;
|
|
|
|
uc.uc_stack.ss_sp = co->stack;
|
|
|
|
uc.uc_stack.ss_size = stack_size;
|
|
|
|
uc.uc_stack.ss_flags = 0;
|
|
|
|
|
2012-06-29 15:40:27 +04:00
|
|
|
#ifdef CONFIG_VALGRIND_H
|
|
|
|
co->valgrind_stack_id =
|
|
|
|
VALGRIND_STACK_REGISTER(co->stack, co->stack + stack_size);
|
|
|
|
#endif
|
|
|
|
|
2011-01-17 19:08:14 +03:00
|
|
|
arg.p = co;
|
|
|
|
|
|
|
|
makecontext(&uc, (void (*)(void))coroutine_trampoline,
|
|
|
|
2, arg.i[0], arg.i[1]);
|
|
|
|
|
|
|
|
/* swapcontext() in, longjmp() back out */
|
|
|
|
if (!setjmp(old_env)) {
|
|
|
|
swapcontext(&old_uc, &uc);
|
|
|
|
}
|
|
|
|
return &co->base;
|
|
|
|
}
|
|
|
|
|
|
|
|
Coroutine *qemu_coroutine_new(void)
|
|
|
|
{
|
|
|
|
Coroutine *co;
|
|
|
|
|
2012-01-13 20:34:04 +04:00
|
|
|
co = QSLIST_FIRST(&pool);
|
2011-01-17 19:08:14 +03:00
|
|
|
if (co) {
|
2012-01-13 20:34:04 +04:00
|
|
|
QSLIST_REMOVE_HEAD(&pool, pool_next);
|
2011-12-05 21:20:12 +04:00
|
|
|
pool_size--;
|
2011-01-17 19:08:14 +03:00
|
|
|
} else {
|
|
|
|
co = coroutine_new();
|
|
|
|
}
|
|
|
|
return co;
|
|
|
|
}
|
|
|
|
|
2012-06-29 15:40:27 +04:00
|
|
|
#ifdef CONFIG_VALGRIND_H
|
2012-07-30 19:13:07 +04:00
|
|
|
#ifdef CONFIG_PRAGMA_DISABLE_UNUSED_BUT_SET
|
2012-06-29 15:40:27 +04:00
|
|
|
/* Work around an unused variable in the valgrind.h macro... */
|
|
|
|
#pragma GCC diagnostic ignored "-Wunused-but-set-variable"
|
2012-07-30 19:13:07 +04:00
|
|
|
#endif
|
2012-06-29 15:40:27 +04:00
|
|
|
static inline void valgrind_stack_deregister(CoroutineUContext *co)
|
|
|
|
{
|
|
|
|
VALGRIND_STACK_DEREGISTER(co->valgrind_stack_id);
|
|
|
|
}
|
2012-07-30 19:13:07 +04:00
|
|
|
#ifdef CONFIG_PRAGMA_DISABLE_UNUSED_BUT_SET
|
2012-06-29 15:40:27 +04:00
|
|
|
#pragma GCC diagnostic error "-Wunused-but-set-variable"
|
|
|
|
#endif
|
2012-07-30 19:13:07 +04:00
|
|
|
#endif
|
2012-06-29 15:40:27 +04:00
|
|
|
|
2011-01-17 19:08:14 +03:00
|
|
|
void qemu_coroutine_delete(Coroutine *co_)
|
|
|
|
{
|
|
|
|
CoroutineUContext *co = DO_UPCAST(CoroutineUContext, base, co_);
|
|
|
|
|
2011-12-05 21:20:12 +04:00
|
|
|
if (pool_size < POOL_MAX_SIZE) {
|
2012-01-13 20:34:04 +04:00
|
|
|
QSLIST_INSERT_HEAD(&pool, &co->base, pool_next);
|
2011-01-17 19:08:14 +03:00
|
|
|
co->base.caller = NULL;
|
2011-12-05 21:20:12 +04:00
|
|
|
pool_size++;
|
2011-01-17 19:08:14 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-06-29 15:40:27 +04:00
|
|
|
#ifdef CONFIG_VALGRIND_H
|
|
|
|
valgrind_stack_deregister(co);
|
|
|
|
#endif
|
|
|
|
|
2011-08-21 07:09:37 +04:00
|
|
|
g_free(co->stack);
|
|
|
|
g_free(co);
|
2011-01-17 19:08:14 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
CoroutineAction qemu_coroutine_switch(Coroutine *from_, Coroutine *to_,
|
|
|
|
CoroutineAction action)
|
|
|
|
{
|
|
|
|
CoroutineUContext *from = DO_UPCAST(CoroutineUContext, base, from_);
|
|
|
|
CoroutineUContext *to = DO_UPCAST(CoroutineUContext, base, to_);
|
|
|
|
CoroutineThreadState *s = coroutine_get_thread_state();
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
s->current = to_;
|
|
|
|
|
|
|
|
ret = setjmp(from->env);
|
|
|
|
if (ret == 0) {
|
|
|
|
longjmp(to->env, action);
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
Coroutine *qemu_coroutine_self(void)
|
|
|
|
{
|
|
|
|
CoroutineThreadState *s = coroutine_get_thread_state();
|
|
|
|
|
|
|
|
return s->current;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool qemu_in_coroutine(void)
|
|
|
|
{
|
|
|
|
CoroutineThreadState *s = pthread_getspecific(thread_state_key);
|
|
|
|
|
|
|
|
return s && s->current->caller;
|
|
|
|
}
|