2
0

coroutine-ucontext.c 10 KB

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