Statistics
| Branch: | Revision:

root / coroutine-ucontext.c @ 6ab7e546

History | View | Annotate | Download (6.5 kB)

1 00dccaf1 Kevin Wolf
/*
2 00dccaf1 Kevin Wolf
 * ucontext coroutine initialization code
3 00dccaf1 Kevin Wolf
 *
4 00dccaf1 Kevin Wolf
 * Copyright (C) 2006  Anthony Liguori <anthony@codemonkey.ws>
5 00dccaf1 Kevin Wolf
 * Copyright (C) 2011  Kevin Wolf <kwolf@redhat.com>
6 00dccaf1 Kevin Wolf
 *
7 00dccaf1 Kevin Wolf
 * This library is free software; you can redistribute it and/or
8 00dccaf1 Kevin Wolf
 * modify it under the terms of the GNU Lesser General Public
9 00dccaf1 Kevin Wolf
 * License as published by the Free Software Foundation; either
10 00dccaf1 Kevin Wolf
 * version 2.0 of the License, or (at your option) any later version.
11 00dccaf1 Kevin Wolf
 *
12 00dccaf1 Kevin Wolf
 * This library is distributed in the hope that it will be useful,
13 00dccaf1 Kevin Wolf
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 00dccaf1 Kevin Wolf
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15 00dccaf1 Kevin Wolf
 * Lesser General Public License for more details.
16 00dccaf1 Kevin Wolf
 *
17 00dccaf1 Kevin Wolf
 * You should have received a copy of the GNU Lesser General Public
18 00dccaf1 Kevin Wolf
 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
19 00dccaf1 Kevin Wolf
 */
20 00dccaf1 Kevin Wolf
21 00dccaf1 Kevin Wolf
/* XXX Is there a nicer way to disable glibc's stack check for longjmp? */
22 00dccaf1 Kevin Wolf
#ifdef _FORTIFY_SOURCE
23 00dccaf1 Kevin Wolf
#undef _FORTIFY_SOURCE
24 00dccaf1 Kevin Wolf
#endif
25 00dccaf1 Kevin Wolf
#include <stdlib.h>
26 00dccaf1 Kevin Wolf
#include <setjmp.h>
27 00dccaf1 Kevin Wolf
#include <stdint.h>
28 00dccaf1 Kevin Wolf
#include <pthread.h>
29 00dccaf1 Kevin Wolf
#include <ucontext.h>
30 00dccaf1 Kevin Wolf
#include "qemu-common.h"
31 737e150e Paolo Bonzini
#include "block/coroutine_int.h"
32 00dccaf1 Kevin Wolf
33 3f4349dc Kevin Wolf
#ifdef CONFIG_VALGRIND_H
34 3f4349dc Kevin Wolf
#include <valgrind/valgrind.h>
35 3f4349dc Kevin Wolf
#endif
36 3f4349dc Kevin Wolf
37 00dccaf1 Kevin Wolf
enum {
38 00dccaf1 Kevin Wolf
    /* Maximum free pool size prevents holding too many freed coroutines */
39 00dccaf1 Kevin Wolf
    POOL_MAX_SIZE = 64,
40 00dccaf1 Kevin Wolf
};
41 00dccaf1 Kevin Wolf
42 39a7a362 Avi Kivity
/** Free list to speed up creation */
43 1bbbdabd Paolo Bonzini
static QSLIST_HEAD(, Coroutine) pool = QSLIST_HEAD_INITIALIZER(pool);
44 39a7a362 Avi Kivity
static unsigned int pool_size;
45 39a7a362 Avi Kivity
46 00dccaf1 Kevin Wolf
typedef struct {
47 00dccaf1 Kevin Wolf
    Coroutine base;
48 00dccaf1 Kevin Wolf
    void *stack;
49 6ab7e546 Peter Maydell
    sigjmp_buf env;
50 3f4349dc Kevin Wolf
51 3f4349dc Kevin Wolf
#ifdef CONFIG_VALGRIND_H
52 3f4349dc Kevin Wolf
    unsigned int valgrind_stack_id;
53 3f4349dc Kevin Wolf
#endif
54 3f4349dc Kevin Wolf
55 00dccaf1 Kevin Wolf
} CoroutineUContext;
56 00dccaf1 Kevin Wolf
57 00dccaf1 Kevin Wolf
/**
58 00dccaf1 Kevin Wolf
 * Per-thread coroutine bookkeeping
59 00dccaf1 Kevin Wolf
 */
60 00dccaf1 Kevin Wolf
typedef struct {
61 00dccaf1 Kevin Wolf
    /** Currently executing coroutine */
62 00dccaf1 Kevin Wolf
    Coroutine *current;
63 00dccaf1 Kevin Wolf
64 00dccaf1 Kevin Wolf
    /** The default coroutine */
65 00dccaf1 Kevin Wolf
    CoroutineUContext leader;
66 00dccaf1 Kevin Wolf
} CoroutineThreadState;
67 00dccaf1 Kevin Wolf
68 00dccaf1 Kevin Wolf
static pthread_key_t thread_state_key;
69 00dccaf1 Kevin Wolf
70 00dccaf1 Kevin Wolf
/*
71 00dccaf1 Kevin Wolf
 * va_args to makecontext() must be type 'int', so passing
72 00dccaf1 Kevin Wolf
 * the pointer we need may require several int args. This
73 00dccaf1 Kevin Wolf
 * union is a quick hack to let us do that
74 00dccaf1 Kevin Wolf
 */
75 00dccaf1 Kevin Wolf
union cc_arg {
76 00dccaf1 Kevin Wolf
    void *p;
77 00dccaf1 Kevin Wolf
    int i[2];
78 00dccaf1 Kevin Wolf
};
79 00dccaf1 Kevin Wolf
80 00dccaf1 Kevin Wolf
static CoroutineThreadState *coroutine_get_thread_state(void)
81 00dccaf1 Kevin Wolf
{
82 00dccaf1 Kevin Wolf
    CoroutineThreadState *s = pthread_getspecific(thread_state_key);
83 00dccaf1 Kevin Wolf
84 00dccaf1 Kevin Wolf
    if (!s) {
85 7267c094 Anthony Liguori
        s = g_malloc0(sizeof(*s));
86 00dccaf1 Kevin Wolf
        s->current = &s->leader.base;
87 00dccaf1 Kevin Wolf
        pthread_setspecific(thread_state_key, s);
88 00dccaf1 Kevin Wolf
    }
89 00dccaf1 Kevin Wolf
    return s;
90 00dccaf1 Kevin Wolf
}
91 00dccaf1 Kevin Wolf
92 00dccaf1 Kevin Wolf
static void qemu_coroutine_thread_cleanup(void *opaque)
93 00dccaf1 Kevin Wolf
{
94 00dccaf1 Kevin Wolf
    CoroutineThreadState *s = opaque;
95 39a7a362 Avi Kivity
96 39a7a362 Avi Kivity
    g_free(s);
97 39a7a362 Avi Kivity
}
98 39a7a362 Avi Kivity
99 39a7a362 Avi Kivity
static void __attribute__((destructor)) coroutine_cleanup(void)
100 39a7a362 Avi Kivity
{
101 00dccaf1 Kevin Wolf
    Coroutine *co;
102 00dccaf1 Kevin Wolf
    Coroutine *tmp;
103 00dccaf1 Kevin Wolf
104 1bbbdabd Paolo Bonzini
    QSLIST_FOREACH_SAFE(co, &pool, pool_next, tmp) {
105 7267c094 Anthony Liguori
        g_free(DO_UPCAST(CoroutineUContext, base, co)->stack);
106 7267c094 Anthony Liguori
        g_free(co);
107 00dccaf1 Kevin Wolf
    }
108 00dccaf1 Kevin Wolf
}
109 00dccaf1 Kevin Wolf
110 00dccaf1 Kevin Wolf
static void __attribute__((constructor)) coroutine_init(void)
111 00dccaf1 Kevin Wolf
{
112 00dccaf1 Kevin Wolf
    int ret;
113 00dccaf1 Kevin Wolf
114 00dccaf1 Kevin Wolf
    ret = pthread_key_create(&thread_state_key, qemu_coroutine_thread_cleanup);
115 00dccaf1 Kevin Wolf
    if (ret != 0) {
116 00dccaf1 Kevin Wolf
        fprintf(stderr, "unable to create leader key: %s\n", strerror(errno));
117 00dccaf1 Kevin Wolf
        abort();
118 00dccaf1 Kevin Wolf
    }
119 00dccaf1 Kevin Wolf
}
120 00dccaf1 Kevin Wolf
121 00dccaf1 Kevin Wolf
static void coroutine_trampoline(int i0, int i1)
122 00dccaf1 Kevin Wolf
{
123 00dccaf1 Kevin Wolf
    union cc_arg arg;
124 00dccaf1 Kevin Wolf
    CoroutineUContext *self;
125 00dccaf1 Kevin Wolf
    Coroutine *co;
126 00dccaf1 Kevin Wolf
127 00dccaf1 Kevin Wolf
    arg.i[0] = i0;
128 00dccaf1 Kevin Wolf
    arg.i[1] = i1;
129 00dccaf1 Kevin Wolf
    self = arg.p;
130 00dccaf1 Kevin Wolf
    co = &self->base;
131 00dccaf1 Kevin Wolf
132 00dccaf1 Kevin Wolf
    /* Initialize longjmp environment and switch back the caller */
133 6ab7e546 Peter Maydell
    if (!sigsetjmp(self->env, 0)) {
134 6ab7e546 Peter Maydell
        siglongjmp(*(sigjmp_buf *)co->entry_arg, 1);
135 00dccaf1 Kevin Wolf
    }
136 00dccaf1 Kevin Wolf
137 00dccaf1 Kevin Wolf
    while (true) {
138 00dccaf1 Kevin Wolf
        co->entry(co->entry_arg);
139 00dccaf1 Kevin Wolf
        qemu_coroutine_switch(co, co->caller, COROUTINE_TERMINATE);
140 00dccaf1 Kevin Wolf
    }
141 00dccaf1 Kevin Wolf
}
142 00dccaf1 Kevin Wolf
143 00dccaf1 Kevin Wolf
static Coroutine *coroutine_new(void)
144 00dccaf1 Kevin Wolf
{
145 00dccaf1 Kevin Wolf
    const size_t stack_size = 1 << 20;
146 00dccaf1 Kevin Wolf
    CoroutineUContext *co;
147 00dccaf1 Kevin Wolf
    ucontext_t old_uc, uc;
148 6ab7e546 Peter Maydell
    sigjmp_buf old_env;
149 32b74677 malc
    union cc_arg arg = {0};
150 00dccaf1 Kevin Wolf
151 6ab7e546 Peter Maydell
    /* The ucontext functions preserve signal masks which incurs a
152 6ab7e546 Peter Maydell
     * system call overhead.  sigsetjmp(buf, 0)/siglongjmp() does not
153 6ab7e546 Peter Maydell
     * preserve signal masks but only works on the current stack.
154 6ab7e546 Peter Maydell
     * Since we need a way to create and switch to a new stack, use
155 6ab7e546 Peter Maydell
     * the ucontext functions for that but sigsetjmp()/siglongjmp() for
156 6ab7e546 Peter Maydell
     * everything else.
157 00dccaf1 Kevin Wolf
     */
158 00dccaf1 Kevin Wolf
159 00dccaf1 Kevin Wolf
    if (getcontext(&uc) == -1) {
160 00dccaf1 Kevin Wolf
        abort();
161 00dccaf1 Kevin Wolf
    }
162 00dccaf1 Kevin Wolf
163 7267c094 Anthony Liguori
    co = g_malloc0(sizeof(*co));
164 7267c094 Anthony Liguori
    co->stack = g_malloc(stack_size);
165 00dccaf1 Kevin Wolf
    co->base.entry_arg = &old_env; /* stash away our jmp_buf */
166 00dccaf1 Kevin Wolf
167 00dccaf1 Kevin Wolf
    uc.uc_link = &old_uc;
168 00dccaf1 Kevin Wolf
    uc.uc_stack.ss_sp = co->stack;
169 00dccaf1 Kevin Wolf
    uc.uc_stack.ss_size = stack_size;
170 00dccaf1 Kevin Wolf
    uc.uc_stack.ss_flags = 0;
171 00dccaf1 Kevin Wolf
172 3f4349dc Kevin Wolf
#ifdef CONFIG_VALGRIND_H
173 3f4349dc Kevin Wolf
    co->valgrind_stack_id =
174 3f4349dc Kevin Wolf
        VALGRIND_STACK_REGISTER(co->stack, co->stack + stack_size);
175 3f4349dc Kevin Wolf
#endif
176 3f4349dc Kevin Wolf
177 00dccaf1 Kevin Wolf
    arg.p = co;
178 00dccaf1 Kevin Wolf
179 00dccaf1 Kevin Wolf
    makecontext(&uc, (void (*)(void))coroutine_trampoline,
180 00dccaf1 Kevin Wolf
                2, arg.i[0], arg.i[1]);
181 00dccaf1 Kevin Wolf
182 6ab7e546 Peter Maydell
    /* swapcontext() in, siglongjmp() back out */
183 6ab7e546 Peter Maydell
    if (!sigsetjmp(old_env, 0)) {
184 00dccaf1 Kevin Wolf
        swapcontext(&old_uc, &uc);
185 00dccaf1 Kevin Wolf
    }
186 00dccaf1 Kevin Wolf
    return &co->base;
187 00dccaf1 Kevin Wolf
}
188 00dccaf1 Kevin Wolf
189 00dccaf1 Kevin Wolf
Coroutine *qemu_coroutine_new(void)
190 00dccaf1 Kevin Wolf
{
191 00dccaf1 Kevin Wolf
    Coroutine *co;
192 00dccaf1 Kevin Wolf
193 1bbbdabd Paolo Bonzini
    co = QSLIST_FIRST(&pool);
194 00dccaf1 Kevin Wolf
    if (co) {
195 1bbbdabd Paolo Bonzini
        QSLIST_REMOVE_HEAD(&pool, pool_next);
196 39a7a362 Avi Kivity
        pool_size--;
197 00dccaf1 Kevin Wolf
    } else {
198 00dccaf1 Kevin Wolf
        co = coroutine_new();
199 00dccaf1 Kevin Wolf
    }
200 00dccaf1 Kevin Wolf
    return co;
201 00dccaf1 Kevin Wolf
}
202 00dccaf1 Kevin Wolf
203 3f4349dc Kevin Wolf
#ifdef CONFIG_VALGRIND_H
204 cc6e3ca9 Gerd Hoffmann
#ifdef CONFIG_PRAGMA_DIAGNOSTIC_AVAILABLE
205 3f4349dc Kevin Wolf
/* Work around an unused variable in the valgrind.h macro... */
206 3f4349dc Kevin Wolf
#pragma GCC diagnostic ignored "-Wunused-but-set-variable"
207 06d71fa1 Peter Maydell
#endif
208 3f4349dc Kevin Wolf
static inline void valgrind_stack_deregister(CoroutineUContext *co)
209 3f4349dc Kevin Wolf
{
210 3f4349dc Kevin Wolf
    VALGRIND_STACK_DEREGISTER(co->valgrind_stack_id);
211 3f4349dc Kevin Wolf
}
212 cc6e3ca9 Gerd Hoffmann
#ifdef CONFIG_PRAGMA_DIAGNOSTIC_AVAILABLE
213 3f4349dc Kevin Wolf
#pragma GCC diagnostic error "-Wunused-but-set-variable"
214 3f4349dc Kevin Wolf
#endif
215 06d71fa1 Peter Maydell
#endif
216 3f4349dc Kevin Wolf
217 00dccaf1 Kevin Wolf
void qemu_coroutine_delete(Coroutine *co_)
218 00dccaf1 Kevin Wolf
{
219 00dccaf1 Kevin Wolf
    CoroutineUContext *co = DO_UPCAST(CoroutineUContext, base, co_);
220 00dccaf1 Kevin Wolf
221 39a7a362 Avi Kivity
    if (pool_size < POOL_MAX_SIZE) {
222 1bbbdabd Paolo Bonzini
        QSLIST_INSERT_HEAD(&pool, &co->base, pool_next);
223 00dccaf1 Kevin Wolf
        co->base.caller = NULL;
224 39a7a362 Avi Kivity
        pool_size++;
225 00dccaf1 Kevin Wolf
        return;
226 00dccaf1 Kevin Wolf
    }
227 00dccaf1 Kevin Wolf
228 3f4349dc Kevin Wolf
#ifdef CONFIG_VALGRIND_H
229 3f4349dc Kevin Wolf
    valgrind_stack_deregister(co);
230 3f4349dc Kevin Wolf
#endif
231 3f4349dc Kevin Wolf
232 7267c094 Anthony Liguori
    g_free(co->stack);
233 7267c094 Anthony Liguori
    g_free(co);
234 00dccaf1 Kevin Wolf
}
235 00dccaf1 Kevin Wolf
236 00dccaf1 Kevin Wolf
CoroutineAction qemu_coroutine_switch(Coroutine *from_, Coroutine *to_,
237 00dccaf1 Kevin Wolf
                                      CoroutineAction action)
238 00dccaf1 Kevin Wolf
{
239 00dccaf1 Kevin Wolf
    CoroutineUContext *from = DO_UPCAST(CoroutineUContext, base, from_);
240 00dccaf1 Kevin Wolf
    CoroutineUContext *to = DO_UPCAST(CoroutineUContext, base, to_);
241 00dccaf1 Kevin Wolf
    CoroutineThreadState *s = coroutine_get_thread_state();
242 00dccaf1 Kevin Wolf
    int ret;
243 00dccaf1 Kevin Wolf
244 00dccaf1 Kevin Wolf
    s->current = to_;
245 00dccaf1 Kevin Wolf
246 6ab7e546 Peter Maydell
    ret = sigsetjmp(from->env, 0);
247 00dccaf1 Kevin Wolf
    if (ret == 0) {
248 6ab7e546 Peter Maydell
        siglongjmp(to->env, action);
249 00dccaf1 Kevin Wolf
    }
250 00dccaf1 Kevin Wolf
    return ret;
251 00dccaf1 Kevin Wolf
}
252 00dccaf1 Kevin Wolf
253 00dccaf1 Kevin Wolf
Coroutine *qemu_coroutine_self(void)
254 00dccaf1 Kevin Wolf
{
255 00dccaf1 Kevin Wolf
    CoroutineThreadState *s = coroutine_get_thread_state();
256 00dccaf1 Kevin Wolf
257 00dccaf1 Kevin Wolf
    return s->current;
258 00dccaf1 Kevin Wolf
}
259 00dccaf1 Kevin Wolf
260 00dccaf1 Kevin Wolf
bool qemu_in_coroutine(void)
261 00dccaf1 Kevin Wolf
{
262 00dccaf1 Kevin Wolf
    CoroutineThreadState *s = pthread_getspecific(thread_state_key);
263 00dccaf1 Kevin Wolf
264 00dccaf1 Kevin Wolf
    return s && s->current->caller;
265 00dccaf1 Kevin Wolf
}