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" |
Paolo Bonzini | 737e150 | 2012-12-17 18:19:44 +0100 | [diff] [blame] | 31 | #include "block/coroutine_int.h" |
Kevin Wolf | 00dccaf | 2011-01-17 16:08:14 +0000 | [diff] [blame] | 32 | |
Kevin Wolf | 3f4349d | 2012-06-29 13:40:27 +0200 | [diff] [blame] | 33 | #ifdef CONFIG_VALGRIND_H |
| 34 | #include <valgrind/valgrind.h> |
| 35 | #endif |
| 36 | |
Kevin Wolf | 00dccaf | 2011-01-17 16:08:14 +0000 | [diff] [blame] | 37 | typedef struct { |
| 38 | Coroutine base; |
| 39 | void *stack; |
Peter Maydell | 6ab7e54 | 2013-02-20 15:21:09 +0000 | [diff] [blame] | 40 | sigjmp_buf env; |
Kevin Wolf | 3f4349d | 2012-06-29 13:40:27 +0200 | [diff] [blame] | 41 | |
| 42 | #ifdef CONFIG_VALGRIND_H |
| 43 | unsigned int valgrind_stack_id; |
| 44 | #endif |
| 45 | |
Kevin Wolf | 00dccaf | 2011-01-17 16:08:14 +0000 | [diff] [blame] | 46 | } CoroutineUContext; |
| 47 | |
| 48 | /** |
| 49 | * Per-thread coroutine bookkeeping |
| 50 | */ |
| 51 | typedef struct { |
| 52 | /** Currently executing coroutine */ |
| 53 | Coroutine *current; |
| 54 | |
Kevin Wolf | 00dccaf | 2011-01-17 16:08:14 +0000 | [diff] [blame] | 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; |
Kevin Wolf | 00dccaf | 2011-01-17 16:08:14 +0000 | [diff] [blame] | 78 | pthread_setspecific(thread_state_key, s); |
| 79 | } |
| 80 | return s; |
| 81 | } |
| 82 | |
| 83 | static void qemu_coroutine_thread_cleanup(void *opaque) |
| 84 | { |
| 85 | CoroutineThreadState *s = opaque; |
Avi Kivity | 39a7a36 | 2011-12-05 19:20:12 +0200 | [diff] [blame] | 86 | |
| 87 | g_free(s); |
| 88 | } |
| 89 | |
Kevin Wolf | 00dccaf | 2011-01-17 16:08:14 +0000 | [diff] [blame] | 90 | static void __attribute__((constructor)) coroutine_init(void) |
| 91 | { |
| 92 | int ret; |
| 93 | |
| 94 | ret = pthread_key_create(&thread_state_key, qemu_coroutine_thread_cleanup); |
| 95 | if (ret != 0) { |
| 96 | fprintf(stderr, "unable to create leader key: %s\n", strerror(errno)); |
| 97 | abort(); |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | static void coroutine_trampoline(int i0, int i1) |
| 102 | { |
| 103 | union cc_arg arg; |
| 104 | CoroutineUContext *self; |
| 105 | Coroutine *co; |
| 106 | |
| 107 | arg.i[0] = i0; |
| 108 | arg.i[1] = i1; |
| 109 | self = arg.p; |
| 110 | co = &self->base; |
| 111 | |
| 112 | /* Initialize longjmp environment and switch back the caller */ |
Peter Maydell | 6ab7e54 | 2013-02-20 15:21:09 +0000 | [diff] [blame] | 113 | if (!sigsetjmp(self->env, 0)) { |
| 114 | siglongjmp(*(sigjmp_buf *)co->entry_arg, 1); |
Kevin Wolf | 00dccaf | 2011-01-17 16:08:14 +0000 | [diff] [blame] | 115 | } |
| 116 | |
| 117 | while (true) { |
| 118 | co->entry(co->entry_arg); |
| 119 | qemu_coroutine_switch(co, co->caller, COROUTINE_TERMINATE); |
| 120 | } |
| 121 | } |
| 122 | |
Paolo Bonzini | 4023978 | 2013-02-19 11:59:09 +0100 | [diff] [blame] | 123 | Coroutine *qemu_coroutine_new(void) |
Kevin Wolf | 00dccaf | 2011-01-17 16:08:14 +0000 | [diff] [blame] | 124 | { |
| 125 | const size_t stack_size = 1 << 20; |
| 126 | CoroutineUContext *co; |
| 127 | ucontext_t old_uc, uc; |
Peter Maydell | 6ab7e54 | 2013-02-20 15:21:09 +0000 | [diff] [blame] | 128 | sigjmp_buf old_env; |
malc | 32b7467 | 2011-08-08 13:46:51 +0400 | [diff] [blame] | 129 | union cc_arg arg = {0}; |
Kevin Wolf | 00dccaf | 2011-01-17 16:08:14 +0000 | [diff] [blame] | 130 | |
Peter Maydell | 6ab7e54 | 2013-02-20 15:21:09 +0000 | [diff] [blame] | 131 | /* The ucontext functions preserve signal masks which incurs a |
| 132 | * system call overhead. sigsetjmp(buf, 0)/siglongjmp() does not |
| 133 | * preserve signal masks but only works on the current stack. |
| 134 | * Since we need a way to create and switch to a new stack, use |
| 135 | * the ucontext functions for that but sigsetjmp()/siglongjmp() for |
| 136 | * everything else. |
Kevin Wolf | 00dccaf | 2011-01-17 16:08:14 +0000 | [diff] [blame] | 137 | */ |
| 138 | |
| 139 | if (getcontext(&uc) == -1) { |
| 140 | abort(); |
| 141 | } |
| 142 | |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 143 | co = g_malloc0(sizeof(*co)); |
| 144 | co->stack = g_malloc(stack_size); |
Kevin Wolf | 00dccaf | 2011-01-17 16:08:14 +0000 | [diff] [blame] | 145 | co->base.entry_arg = &old_env; /* stash away our jmp_buf */ |
| 146 | |
| 147 | uc.uc_link = &old_uc; |
| 148 | uc.uc_stack.ss_sp = co->stack; |
| 149 | uc.uc_stack.ss_size = stack_size; |
| 150 | uc.uc_stack.ss_flags = 0; |
| 151 | |
Kevin Wolf | 3f4349d | 2012-06-29 13:40:27 +0200 | [diff] [blame] | 152 | #ifdef CONFIG_VALGRIND_H |
| 153 | co->valgrind_stack_id = |
| 154 | VALGRIND_STACK_REGISTER(co->stack, co->stack + stack_size); |
| 155 | #endif |
| 156 | |
Kevin Wolf | 00dccaf | 2011-01-17 16:08:14 +0000 | [diff] [blame] | 157 | arg.p = co; |
| 158 | |
| 159 | makecontext(&uc, (void (*)(void))coroutine_trampoline, |
| 160 | 2, arg.i[0], arg.i[1]); |
| 161 | |
Peter Maydell | 6ab7e54 | 2013-02-20 15:21:09 +0000 | [diff] [blame] | 162 | /* swapcontext() in, siglongjmp() back out */ |
| 163 | if (!sigsetjmp(old_env, 0)) { |
Kevin Wolf | 00dccaf | 2011-01-17 16:08:14 +0000 | [diff] [blame] | 164 | swapcontext(&old_uc, &uc); |
| 165 | } |
| 166 | return &co->base; |
| 167 | } |
| 168 | |
Kevin Wolf | 3f4349d | 2012-06-29 13:40:27 +0200 | [diff] [blame] | 169 | #ifdef CONFIG_VALGRIND_H |
Gerd Hoffmann | cc6e3ca | 2013-01-09 10:17:07 +0100 | [diff] [blame] | 170 | #ifdef CONFIG_PRAGMA_DIAGNOSTIC_AVAILABLE |
Kevin Wolf | 3f4349d | 2012-06-29 13:40:27 +0200 | [diff] [blame] | 171 | /* Work around an unused variable in the valgrind.h macro... */ |
Markus Armbruster | e6f53fd | 2013-04-16 13:51:06 +0200 | [diff] [blame] | 172 | #pragma GCC diagnostic push |
Kevin Wolf | 3f4349d | 2012-06-29 13:40:27 +0200 | [diff] [blame] | 173 | #pragma GCC diagnostic ignored "-Wunused-but-set-variable" |
Peter Maydell | 06d71fa | 2012-07-30 16:13:07 +0100 | [diff] [blame] | 174 | #endif |
Kevin Wolf | 3f4349d | 2012-06-29 13:40:27 +0200 | [diff] [blame] | 175 | static inline void valgrind_stack_deregister(CoroutineUContext *co) |
| 176 | { |
| 177 | VALGRIND_STACK_DEREGISTER(co->valgrind_stack_id); |
| 178 | } |
Gerd Hoffmann | cc6e3ca | 2013-01-09 10:17:07 +0100 | [diff] [blame] | 179 | #ifdef CONFIG_PRAGMA_DIAGNOSTIC_AVAILABLE |
Markus Armbruster | e6f53fd | 2013-04-16 13:51:06 +0200 | [diff] [blame] | 180 | #pragma GCC diagnostic pop |
Kevin Wolf | 3f4349d | 2012-06-29 13:40:27 +0200 | [diff] [blame] | 181 | #endif |
Peter Maydell | 06d71fa | 2012-07-30 16:13:07 +0100 | [diff] [blame] | 182 | #endif |
Kevin Wolf | 3f4349d | 2012-06-29 13:40:27 +0200 | [diff] [blame] | 183 | |
Kevin Wolf | 00dccaf | 2011-01-17 16:08:14 +0000 | [diff] [blame] | 184 | void qemu_coroutine_delete(Coroutine *co_) |
| 185 | { |
Kevin Wolf | 00dccaf | 2011-01-17 16:08:14 +0000 | [diff] [blame] | 186 | CoroutineUContext *co = DO_UPCAST(CoroutineUContext, base, co_); |
| 187 | |
Kevin Wolf | 3f4349d | 2012-06-29 13:40:27 +0200 | [diff] [blame] | 188 | #ifdef CONFIG_VALGRIND_H |
| 189 | valgrind_stack_deregister(co); |
| 190 | #endif |
| 191 | |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 192 | g_free(co->stack); |
| 193 | g_free(co); |
Kevin Wolf | 00dccaf | 2011-01-17 16:08:14 +0000 | [diff] [blame] | 194 | } |
| 195 | |
| 196 | CoroutineAction qemu_coroutine_switch(Coroutine *from_, Coroutine *to_, |
| 197 | CoroutineAction action) |
| 198 | { |
| 199 | CoroutineUContext *from = DO_UPCAST(CoroutineUContext, base, from_); |
| 200 | CoroutineUContext *to = DO_UPCAST(CoroutineUContext, base, to_); |
| 201 | CoroutineThreadState *s = coroutine_get_thread_state(); |
| 202 | int ret; |
| 203 | |
| 204 | s->current = to_; |
| 205 | |
Peter Maydell | 6ab7e54 | 2013-02-20 15:21:09 +0000 | [diff] [blame] | 206 | ret = sigsetjmp(from->env, 0); |
Kevin Wolf | 00dccaf | 2011-01-17 16:08:14 +0000 | [diff] [blame] | 207 | if (ret == 0) { |
Peter Maydell | 6ab7e54 | 2013-02-20 15:21:09 +0000 | [diff] [blame] | 208 | siglongjmp(to->env, action); |
Kevin Wolf | 00dccaf | 2011-01-17 16:08:14 +0000 | [diff] [blame] | 209 | } |
| 210 | return ret; |
| 211 | } |
| 212 | |
| 213 | Coroutine *qemu_coroutine_self(void) |
| 214 | { |
| 215 | CoroutineThreadState *s = coroutine_get_thread_state(); |
| 216 | |
| 217 | return s->current; |
| 218 | } |
| 219 | |
| 220 | bool qemu_in_coroutine(void) |
| 221 | { |
| 222 | CoroutineThreadState *s = pthread_getspecific(thread_state_key); |
| 223 | |
| 224 | return s && s->current->caller; |
| 225 | } |