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> |
Kevin Wolf | 00dccaf | 2011-01-17 16:08:14 +0000 | [diff] [blame] | 28 | #include <ucontext.h> |
| 29 | #include "qemu-common.h" |
Daniel P. Berrange | 10817bf | 2015-09-01 14:48:02 +0100 | [diff] [blame] | 30 | #include "qemu/coroutine_int.h" |
Kevin Wolf | 00dccaf | 2011-01-17 16:08:14 +0000 | [diff] [blame] | 31 | |
Kevin Wolf | 3f4349d | 2012-06-29 13:40:27 +0200 | [diff] [blame] | 32 | #ifdef CONFIG_VALGRIND_H |
| 33 | #include <valgrind/valgrind.h> |
| 34 | #endif |
| 35 | |
Kevin Wolf | 00dccaf | 2011-01-17 16:08:14 +0000 | [diff] [blame] | 36 | typedef struct { |
| 37 | Coroutine base; |
| 38 | void *stack; |
Peter Maydell | 6ab7e54 | 2013-02-20 15:21:09 +0000 | [diff] [blame] | 39 | sigjmp_buf env; |
Kevin Wolf | 3f4349d | 2012-06-29 13:40:27 +0200 | [diff] [blame] | 40 | |
| 41 | #ifdef CONFIG_VALGRIND_H |
| 42 | unsigned int valgrind_stack_id; |
| 43 | #endif |
| 44 | |
Kevin Wolf | 00dccaf | 2011-01-17 16:08:14 +0000 | [diff] [blame] | 45 | } CoroutineUContext; |
| 46 | |
| 47 | /** |
| 48 | * Per-thread coroutine bookkeeping |
| 49 | */ |
Paolo Bonzini | d1d1b20 | 2014-12-02 12:05:44 +0100 | [diff] [blame] | 50 | static __thread CoroutineUContext leader; |
| 51 | static __thread Coroutine *current; |
Kevin Wolf | 00dccaf | 2011-01-17 16:08:14 +0000 | [diff] [blame] | 52 | |
| 53 | /* |
| 54 | * va_args to makecontext() must be type 'int', so passing |
| 55 | * the pointer we need may require several int args. This |
| 56 | * union is a quick hack to let us do that |
| 57 | */ |
| 58 | union cc_arg { |
| 59 | void *p; |
| 60 | int i[2]; |
| 61 | }; |
| 62 | |
Kevin Wolf | 00dccaf | 2011-01-17 16:08:14 +0000 | [diff] [blame] | 63 | static void coroutine_trampoline(int i0, int i1) |
| 64 | { |
| 65 | union cc_arg arg; |
| 66 | CoroutineUContext *self; |
| 67 | Coroutine *co; |
| 68 | |
| 69 | arg.i[0] = i0; |
| 70 | arg.i[1] = i1; |
| 71 | self = arg.p; |
| 72 | co = &self->base; |
| 73 | |
| 74 | /* Initialize longjmp environment and switch back the caller */ |
Peter Maydell | 6ab7e54 | 2013-02-20 15:21:09 +0000 | [diff] [blame] | 75 | if (!sigsetjmp(self->env, 0)) { |
| 76 | siglongjmp(*(sigjmp_buf *)co->entry_arg, 1); |
Kevin Wolf | 00dccaf | 2011-01-17 16:08:14 +0000 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | while (true) { |
| 80 | co->entry(co->entry_arg); |
| 81 | qemu_coroutine_switch(co, co->caller, COROUTINE_TERMINATE); |
| 82 | } |
| 83 | } |
| 84 | |
Paolo Bonzini | 4023978 | 2013-02-19 11:59:09 +0100 | [diff] [blame] | 85 | Coroutine *qemu_coroutine_new(void) |
Kevin Wolf | 00dccaf | 2011-01-17 16:08:14 +0000 | [diff] [blame] | 86 | { |
| 87 | const size_t stack_size = 1 << 20; |
| 88 | CoroutineUContext *co; |
| 89 | ucontext_t old_uc, uc; |
Peter Maydell | 6ab7e54 | 2013-02-20 15:21:09 +0000 | [diff] [blame] | 90 | sigjmp_buf old_env; |
malc | 32b7467 | 2011-08-08 13:46:51 +0400 | [diff] [blame] | 91 | union cc_arg arg = {0}; |
Kevin Wolf | 00dccaf | 2011-01-17 16:08:14 +0000 | [diff] [blame] | 92 | |
Peter Maydell | 6ab7e54 | 2013-02-20 15:21:09 +0000 | [diff] [blame] | 93 | /* The ucontext functions preserve signal masks which incurs a |
| 94 | * system call overhead. sigsetjmp(buf, 0)/siglongjmp() does not |
| 95 | * preserve signal masks but only works on the current stack. |
| 96 | * Since we need a way to create and switch to a new stack, use |
| 97 | * the ucontext functions for that but sigsetjmp()/siglongjmp() for |
| 98 | * everything else. |
Kevin Wolf | 00dccaf | 2011-01-17 16:08:14 +0000 | [diff] [blame] | 99 | */ |
| 100 | |
| 101 | if (getcontext(&uc) == -1) { |
| 102 | abort(); |
| 103 | } |
| 104 | |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 105 | co = g_malloc0(sizeof(*co)); |
| 106 | co->stack = g_malloc(stack_size); |
Kevin Wolf | 00dccaf | 2011-01-17 16:08:14 +0000 | [diff] [blame] | 107 | co->base.entry_arg = &old_env; /* stash away our jmp_buf */ |
| 108 | |
| 109 | uc.uc_link = &old_uc; |
| 110 | uc.uc_stack.ss_sp = co->stack; |
| 111 | uc.uc_stack.ss_size = stack_size; |
| 112 | uc.uc_stack.ss_flags = 0; |
| 113 | |
Kevin Wolf | 3f4349d | 2012-06-29 13:40:27 +0200 | [diff] [blame] | 114 | #ifdef CONFIG_VALGRIND_H |
| 115 | co->valgrind_stack_id = |
| 116 | VALGRIND_STACK_REGISTER(co->stack, co->stack + stack_size); |
| 117 | #endif |
| 118 | |
Kevin Wolf | 00dccaf | 2011-01-17 16:08:14 +0000 | [diff] [blame] | 119 | arg.p = co; |
| 120 | |
| 121 | makecontext(&uc, (void (*)(void))coroutine_trampoline, |
| 122 | 2, arg.i[0], arg.i[1]); |
| 123 | |
Peter Maydell | 6ab7e54 | 2013-02-20 15:21:09 +0000 | [diff] [blame] | 124 | /* swapcontext() in, siglongjmp() back out */ |
| 125 | if (!sigsetjmp(old_env, 0)) { |
Kevin Wolf | 00dccaf | 2011-01-17 16:08:14 +0000 | [diff] [blame] | 126 | swapcontext(&old_uc, &uc); |
| 127 | } |
| 128 | return &co->base; |
| 129 | } |
| 130 | |
Kevin Wolf | 3f4349d | 2012-06-29 13:40:27 +0200 | [diff] [blame] | 131 | #ifdef CONFIG_VALGRIND_H |
Gerd Hoffmann | cc6e3ca | 2013-01-09 10:17:07 +0100 | [diff] [blame] | 132 | #ifdef CONFIG_PRAGMA_DIAGNOSTIC_AVAILABLE |
Kevin Wolf | 3f4349d | 2012-06-29 13:40:27 +0200 | [diff] [blame] | 133 | /* Work around an unused variable in the valgrind.h macro... */ |
Markus Armbruster | e6f53fd | 2013-04-16 13:51:06 +0200 | [diff] [blame] | 134 | #pragma GCC diagnostic push |
Kevin Wolf | 3f4349d | 2012-06-29 13:40:27 +0200 | [diff] [blame] | 135 | #pragma GCC diagnostic ignored "-Wunused-but-set-variable" |
Peter Maydell | 06d71fa | 2012-07-30 16:13:07 +0100 | [diff] [blame] | 136 | #endif |
Kevin Wolf | 3f4349d | 2012-06-29 13:40:27 +0200 | [diff] [blame] | 137 | static inline void valgrind_stack_deregister(CoroutineUContext *co) |
| 138 | { |
| 139 | VALGRIND_STACK_DEREGISTER(co->valgrind_stack_id); |
| 140 | } |
Gerd Hoffmann | cc6e3ca | 2013-01-09 10:17:07 +0100 | [diff] [blame] | 141 | #ifdef CONFIG_PRAGMA_DIAGNOSTIC_AVAILABLE |
Markus Armbruster | e6f53fd | 2013-04-16 13:51:06 +0200 | [diff] [blame] | 142 | #pragma GCC diagnostic pop |
Kevin Wolf | 3f4349d | 2012-06-29 13:40:27 +0200 | [diff] [blame] | 143 | #endif |
Peter Maydell | 06d71fa | 2012-07-30 16:13:07 +0100 | [diff] [blame] | 144 | #endif |
Kevin Wolf | 3f4349d | 2012-06-29 13:40:27 +0200 | [diff] [blame] | 145 | |
Kevin Wolf | 00dccaf | 2011-01-17 16:08:14 +0000 | [diff] [blame] | 146 | void qemu_coroutine_delete(Coroutine *co_) |
| 147 | { |
Kevin Wolf | 00dccaf | 2011-01-17 16:08:14 +0000 | [diff] [blame] | 148 | CoroutineUContext *co = DO_UPCAST(CoroutineUContext, base, co_); |
| 149 | |
Kevin Wolf | 3f4349d | 2012-06-29 13:40:27 +0200 | [diff] [blame] | 150 | #ifdef CONFIG_VALGRIND_H |
| 151 | valgrind_stack_deregister(co); |
| 152 | #endif |
| 153 | |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 154 | g_free(co->stack); |
| 155 | g_free(co); |
Kevin Wolf | 00dccaf | 2011-01-17 16:08:14 +0000 | [diff] [blame] | 156 | } |
| 157 | |
Paolo Bonzini | d1d1b20 | 2014-12-02 12:05:44 +0100 | [diff] [blame] | 158 | /* This function is marked noinline to prevent GCC from inlining it |
| 159 | * into coroutine_trampoline(). If we allow it to do that then it |
| 160 | * hoists the code to get the address of the TLS variable "current" |
| 161 | * out of the while() loop. This is an invalid transformation because |
| 162 | * the sigsetjmp() call may be called when running thread A but |
| 163 | * return in thread B, and so we might be in a different thread |
| 164 | * context each time round the loop. |
| 165 | */ |
| 166 | CoroutineAction __attribute__((noinline)) |
| 167 | qemu_coroutine_switch(Coroutine *from_, Coroutine *to_, |
| 168 | CoroutineAction action) |
Kevin Wolf | 00dccaf | 2011-01-17 16:08:14 +0000 | [diff] [blame] | 169 | { |
| 170 | CoroutineUContext *from = DO_UPCAST(CoroutineUContext, base, from_); |
| 171 | CoroutineUContext *to = DO_UPCAST(CoroutineUContext, base, to_); |
Kevin Wolf | 00dccaf | 2011-01-17 16:08:14 +0000 | [diff] [blame] | 172 | int ret; |
| 173 | |
Paolo Bonzini | d1d1b20 | 2014-12-02 12:05:44 +0100 | [diff] [blame] | 174 | current = to_; |
Kevin Wolf | 00dccaf | 2011-01-17 16:08:14 +0000 | [diff] [blame] | 175 | |
Peter Maydell | 6ab7e54 | 2013-02-20 15:21:09 +0000 | [diff] [blame] | 176 | ret = sigsetjmp(from->env, 0); |
Kevin Wolf | 00dccaf | 2011-01-17 16:08:14 +0000 | [diff] [blame] | 177 | if (ret == 0) { |
Peter Maydell | 6ab7e54 | 2013-02-20 15:21:09 +0000 | [diff] [blame] | 178 | siglongjmp(to->env, action); |
Kevin Wolf | 00dccaf | 2011-01-17 16:08:14 +0000 | [diff] [blame] | 179 | } |
| 180 | return ret; |
| 181 | } |
| 182 | |
| 183 | Coroutine *qemu_coroutine_self(void) |
| 184 | { |
Paolo Bonzini | d1d1b20 | 2014-12-02 12:05:44 +0100 | [diff] [blame] | 185 | if (!current) { |
| 186 | current = &leader.base; |
| 187 | } |
| 188 | return current; |
Kevin Wolf | 00dccaf | 2011-01-17 16:08:14 +0000 | [diff] [blame] | 189 | } |
| 190 | |
| 191 | bool qemu_in_coroutine(void) |
| 192 | { |
Paolo Bonzini | d1d1b20 | 2014-12-02 12:05:44 +0100 | [diff] [blame] | 193 | return current && current->caller; |
Kevin Wolf | 00dccaf | 2011-01-17 16:08:14 +0000 | [diff] [blame] | 194 | } |