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