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 "qemu/osdep.h" 26 #include <ucontext.h> 27 #include "qemu/coroutine_int.h" 28 29 #ifdef CONFIG_VALGRIND_H 30 #include <valgrind/valgrind.h> 31 #endif 32 33 #if defined(__SANITIZE_ADDRESS__) || __has_feature(address_sanitizer) 34 #ifdef CONFIG_ASAN_IFACE_FIBER 35 #define CONFIG_ASAN 1 36 #include <sanitizer/asan_interface.h> 37 #endif 38 #endif 39 40 #ifdef CONFIG_TSAN 41 #include <sanitizer/tsan_interface.h> 42 #endif 43 44 typedef struct { 45 Coroutine base; 46 void *stack; 47 size_t stack_size; 48 #ifdef CONFIG_SAFESTACK 49 /* Need an unsafe stack for each coroutine */ 50 void *unsafe_stack; 51 size_t unsafe_stack_size; 52 #endif 53 sigjmp_buf env; 54 55 void *tsan_co_fiber; 56 void *tsan_caller_fiber; 57 58 #ifdef CONFIG_VALGRIND_H 59 unsigned int valgrind_stack_id; 60 #endif 61 62 } CoroutineUContext; 63 64 /** 65 * Per-thread coroutine bookkeeping 66 */ 67 static __thread CoroutineUContext leader; 68 static __thread Coroutine *current; 69 70 /* 71 * va_args to makecontext() must be type 'int', so passing 72 * the pointer we need may require several int args. This 73 * union is a quick hack to let us do that 74 */ 75 union cc_arg { 76 void *p; 77 int i[2]; 78 }; 79 80 /* QEMU_ALWAYS_INLINE only does so if __OPTIMIZE__, so we cannot use it. */ 81 static inline __attribute__((always_inline)) 82 void on_new_fiber(CoroutineUContext *co) 83 { 84 #ifdef CONFIG_TSAN 85 co->tsan_co_fiber = __tsan_create_fiber(0); /* flags: sync on switch */ 86 co->tsan_caller_fiber = __tsan_get_current_fiber(); 87 #endif 88 } 89 90 static inline __attribute__((always_inline)) 91 void finish_switch_fiber(void *fake_stack_save) 92 { 93 #ifdef CONFIG_ASAN 94 const void *bottom_old; 95 size_t size_old; 96 97 __sanitizer_finish_switch_fiber(fake_stack_save, &bottom_old, &size_old); 98 99 if (!leader.stack) { 100 leader.stack = (void *)bottom_old; 101 leader.stack_size = size_old; 102 } 103 #endif 104 #ifdef CONFIG_TSAN 105 if (fake_stack_save) { 106 __tsan_release(fake_stack_save); 107 __tsan_switch_to_fiber(fake_stack_save, 0); /* 0=synchronize */ 108 } 109 #endif 110 } 111 112 static inline __attribute__((always_inline)) void start_switch_fiber( 113 CoroutineAction action, void **fake_stack_save, 114 const void *bottom, size_t size, void *new_fiber) 115 { 116 #ifdef CONFIG_ASAN 117 __sanitizer_start_switch_fiber( 118 action == COROUTINE_TERMINATE ? NULL : fake_stack_save, 119 bottom, size); 120 #endif 121 #ifdef CONFIG_TSAN 122 void *curr_fiber = 123 __tsan_get_current_fiber(); 124 __tsan_acquire(curr_fiber); 125 126 *fake_stack_save = curr_fiber; 127 __tsan_switch_to_fiber(new_fiber, 0); /* 0=synchronize */ 128 #endif 129 } 130 131 static void coroutine_trampoline(int i0, int i1) 132 { 133 union cc_arg arg; 134 CoroutineUContext *self; 135 Coroutine *co; 136 void *fake_stack_save = NULL; 137 138 finish_switch_fiber(NULL); 139 140 arg.i[0] = i0; 141 arg.i[1] = i1; 142 self = arg.p; 143 co = &self->base; 144 145 /* Initialize longjmp environment and switch back the caller */ 146 if (!sigsetjmp(self->env, 0)) { 147 start_switch_fiber( 148 COROUTINE_YIELD, 149 &fake_stack_save, 150 leader.stack, 151 leader.stack_size, 152 self->tsan_caller_fiber); 153 siglongjmp(*(sigjmp_buf *)co->entry_arg, 1); 154 } 155 156 finish_switch_fiber(fake_stack_save); 157 158 while (true) { 159 co->entry(co->entry_arg); 160 qemu_coroutine_switch(co, co->caller, COROUTINE_TERMINATE); 161 } 162 } 163 164 Coroutine *qemu_coroutine_new(void) 165 { 166 CoroutineUContext *co; 167 ucontext_t old_uc, uc; 168 sigjmp_buf old_env; 169 union cc_arg arg = {0}; 170 void *fake_stack_save = NULL; 171 172 /* The ucontext functions preserve signal masks which incurs a 173 * system call overhead. sigsetjmp(buf, 0)/siglongjmp() does not 174 * preserve signal masks but only works on the current stack. 175 * Since we need a way to create and switch to a new stack, use 176 * the ucontext functions for that but sigsetjmp()/siglongjmp() for 177 * everything else. 178 */ 179 180 if (getcontext(&uc) == -1) { 181 abort(); 182 } 183 184 co = g_malloc0(sizeof(*co)); 185 co->stack_size = COROUTINE_STACK_SIZE; 186 co->stack = qemu_alloc_stack(&co->stack_size); 187 #ifdef CONFIG_SAFESTACK 188 co->unsafe_stack_size = COROUTINE_STACK_SIZE; 189 co->unsafe_stack = qemu_alloc_stack(&co->unsafe_stack_size); 190 #endif 191 co->base.entry_arg = &old_env; /* stash away our jmp_buf */ 192 193 uc.uc_link = &old_uc; 194 uc.uc_stack.ss_sp = co->stack; 195 uc.uc_stack.ss_size = co->stack_size; 196 uc.uc_stack.ss_flags = 0; 197 198 #ifdef CONFIG_VALGRIND_H 199 co->valgrind_stack_id = 200 VALGRIND_STACK_REGISTER(co->stack, co->stack + co->stack_size); 201 #endif 202 203 arg.p = co; 204 205 on_new_fiber(co); 206 makecontext(&uc, (void (*)(void))coroutine_trampoline, 207 2, arg.i[0], arg.i[1]); 208 209 /* swapcontext() in, siglongjmp() back out */ 210 if (!sigsetjmp(old_env, 0)) { 211 start_switch_fiber( 212 COROUTINE_YIELD, 213 &fake_stack_save, 214 co->stack, co->stack_size, co->tsan_co_fiber); 215 216 #ifdef CONFIG_SAFESTACK 217 /* 218 * Before we swap the context, set the new unsafe stack 219 * The unsafe stack grows just like the normal stack, so start from 220 * the last usable location of the memory area. 221 * NOTE: we don't have to re-set the usp afterwards because we are 222 * coming back to this context through a siglongjmp. 223 * The compiler already wrapped the corresponding sigsetjmp call with 224 * code that saves the usp on the (safe) stack before the call, and 225 * restores it right after (which is where we return with siglongjmp). 226 */ 227 void *usp = co->unsafe_stack + co->unsafe_stack_size; 228 __safestack_unsafe_stack_ptr = usp; 229 #endif 230 231 swapcontext(&old_uc, &uc); 232 } 233 234 finish_switch_fiber(fake_stack_save); 235 236 return &co->base; 237 } 238 239 #ifdef CONFIG_VALGRIND_H 240 #if defined(CONFIG_PRAGMA_DIAGNOSTIC_AVAILABLE) && !defined(__clang__) 241 /* Work around an unused variable in the valgrind.h macro... */ 242 #pragma GCC diagnostic push 243 #pragma GCC diagnostic ignored "-Wunused-but-set-variable" 244 #endif 245 static inline void valgrind_stack_deregister(CoroutineUContext *co) 246 { 247 VALGRIND_STACK_DEREGISTER(co->valgrind_stack_id); 248 } 249 #if defined(CONFIG_PRAGMA_DIAGNOSTIC_AVAILABLE) && !defined(__clang__) 250 #pragma GCC diagnostic pop 251 #endif 252 #endif 253 254 void qemu_coroutine_delete(Coroutine *co_) 255 { 256 CoroutineUContext *co = DO_UPCAST(CoroutineUContext, base, co_); 257 258 #ifdef CONFIG_VALGRIND_H 259 valgrind_stack_deregister(co); 260 #endif 261 262 qemu_free_stack(co->stack, co->stack_size); 263 #ifdef CONFIG_SAFESTACK 264 qemu_free_stack(co->unsafe_stack, co->unsafe_stack_size); 265 #endif 266 g_free(co); 267 } 268 269 /* This function is marked noinline to prevent GCC from inlining it 270 * into coroutine_trampoline(). If we allow it to do that then it 271 * hoists the code to get the address of the TLS variable "current" 272 * out of the while() loop. This is an invalid transformation because 273 * the sigsetjmp() call may be called when running thread A but 274 * return in thread B, and so we might be in a different thread 275 * context each time round the loop. 276 */ 277 CoroutineAction __attribute__((noinline)) 278 qemu_coroutine_switch(Coroutine *from_, Coroutine *to_, 279 CoroutineAction action) 280 { 281 CoroutineUContext *from = DO_UPCAST(CoroutineUContext, base, from_); 282 CoroutineUContext *to = DO_UPCAST(CoroutineUContext, base, to_); 283 int ret; 284 void *fake_stack_save = NULL; 285 286 current = to_; 287 288 ret = sigsetjmp(from->env, 0); 289 if (ret == 0) { 290 start_switch_fiber(action, &fake_stack_save, 291 to->stack, to->stack_size, to->tsan_co_fiber); 292 siglongjmp(to->env, action); 293 } 294 295 finish_switch_fiber(fake_stack_save); 296 297 return ret; 298 } 299 300 Coroutine *qemu_coroutine_self(void) 301 { 302 if (!current) { 303 current = &leader.base; 304 } 305 #ifdef CONFIG_TSAN 306 if (!leader.tsan_co_fiber) { 307 leader.tsan_co_fiber = __tsan_get_current_fiber(); 308 } 309 #endif 310 return current; 311 } 312 313 bool qemu_in_coroutine(void) 314 { 315 return current && current->caller; 316 } 317