coroutine-ucontext.c 10 KB

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