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