2
0

coroutine-ucontext.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  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.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. #ifdef CONFIG_VALGRIND_H
  37. #include <valgrind/valgrind.h>
  38. #endif
  39. #if defined(__SANITIZE_ADDRESS__) || __has_feature(address_sanitizer)
  40. #ifdef CONFIG_ASAN_IFACE_FIBER
  41. #define CONFIG_ASAN 1
  42. #include <sanitizer/asan_interface.h>
  43. #endif
  44. #endif
  45. typedef struct {
  46. Coroutine base;
  47. void *stack;
  48. size_t stack_size;
  49. sigjmp_buf env;
  50. #ifdef CONFIG_VALGRIND_H
  51. unsigned int valgrind_stack_id;
  52. #endif
  53. } CoroutineUContext;
  54. /**
  55. * Per-thread coroutine bookkeeping
  56. */
  57. static __thread CoroutineUContext leader;
  58. static __thread Coroutine *current;
  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. static void finish_switch_fiber(void *fake_stack_save)
  69. {
  70. #ifdef CONFIG_ASAN
  71. const void *bottom_old;
  72. size_t size_old;
  73. __sanitizer_finish_switch_fiber(fake_stack_save, &bottom_old, &size_old);
  74. if (!leader.stack) {
  75. leader.stack = (void *)bottom_old;
  76. leader.stack_size = size_old;
  77. }
  78. #endif
  79. }
  80. static void start_switch_fiber(void **fake_stack_save,
  81. const void *bottom, size_t size)
  82. {
  83. #ifdef CONFIG_ASAN
  84. __sanitizer_start_switch_fiber(fake_stack_save, bottom, size);
  85. #endif
  86. }
  87. static void coroutine_trampoline(int i0, int i1)
  88. {
  89. union cc_arg arg;
  90. CoroutineUContext *self;
  91. Coroutine *co;
  92. void *fake_stack_save = NULL;
  93. finish_switch_fiber(NULL);
  94. arg.i[0] = i0;
  95. arg.i[1] = i1;
  96. self = arg.p;
  97. co = &self->base;
  98. /* Initialize longjmp environment and switch back the caller */
  99. if (!sigsetjmp(self->env, 0)) {
  100. start_switch_fiber(&fake_stack_save,
  101. leader.stack, leader.stack_size);
  102. siglongjmp(*(sigjmp_buf *)co->entry_arg, 1);
  103. }
  104. finish_switch_fiber(fake_stack_save);
  105. while (true) {
  106. co->entry(co->entry_arg);
  107. qemu_coroutine_switch(co, co->caller, COROUTINE_TERMINATE);
  108. }
  109. }
  110. Coroutine *qemu_coroutine_new(void)
  111. {
  112. CoroutineUContext *co;
  113. ucontext_t old_uc, uc;
  114. sigjmp_buf old_env;
  115. union cc_arg arg = {0};
  116. void *fake_stack_save = NULL;
  117. /* The ucontext functions preserve signal masks which incurs a
  118. * system call overhead. sigsetjmp(buf, 0)/siglongjmp() does not
  119. * preserve signal masks but only works on the current stack.
  120. * Since we need a way to create and switch to a new stack, use
  121. * the ucontext functions for that but sigsetjmp()/siglongjmp() for
  122. * everything else.
  123. */
  124. if (getcontext(&uc) == -1) {
  125. abort();
  126. }
  127. co = g_malloc0(sizeof(*co));
  128. co->stack_size = COROUTINE_STACK_SIZE;
  129. co->stack = qemu_alloc_stack(&co->stack_size);
  130. co->base.entry_arg = &old_env; /* stash away our jmp_buf */
  131. uc.uc_link = &old_uc;
  132. uc.uc_stack.ss_sp = co->stack;
  133. uc.uc_stack.ss_size = co->stack_size;
  134. uc.uc_stack.ss_flags = 0;
  135. #ifdef CONFIG_VALGRIND_H
  136. co->valgrind_stack_id =
  137. VALGRIND_STACK_REGISTER(co->stack, co->stack + co->stack_size);
  138. #endif
  139. arg.p = co;
  140. makecontext(&uc, (void (*)(void))coroutine_trampoline,
  141. 2, arg.i[0], arg.i[1]);
  142. /* swapcontext() in, siglongjmp() back out */
  143. if (!sigsetjmp(old_env, 0)) {
  144. start_switch_fiber(&fake_stack_save, co->stack, co->stack_size);
  145. swapcontext(&old_uc, &uc);
  146. }
  147. finish_switch_fiber(fake_stack_save);
  148. return &co->base;
  149. }
  150. #ifdef CONFIG_VALGRIND_H
  151. #if defined(CONFIG_PRAGMA_DIAGNOSTIC_AVAILABLE) && !defined(__clang__)
  152. /* Work around an unused variable in the valgrind.h macro... */
  153. #pragma GCC diagnostic push
  154. #pragma GCC diagnostic ignored "-Wunused-but-set-variable"
  155. #endif
  156. static inline void valgrind_stack_deregister(CoroutineUContext *co)
  157. {
  158. VALGRIND_STACK_DEREGISTER(co->valgrind_stack_id);
  159. }
  160. #if defined(CONFIG_PRAGMA_DIAGNOSTIC_AVAILABLE) && !defined(__clang__)
  161. #pragma GCC diagnostic pop
  162. #endif
  163. #endif
  164. void qemu_coroutine_delete(Coroutine *co_)
  165. {
  166. CoroutineUContext *co = DO_UPCAST(CoroutineUContext, base, co_);
  167. #ifdef CONFIG_VALGRIND_H
  168. valgrind_stack_deregister(co);
  169. #endif
  170. qemu_free_stack(co->stack, co->stack_size);
  171. g_free(co);
  172. }
  173. /* This function is marked noinline to prevent GCC from inlining it
  174. * into coroutine_trampoline(). If we allow it to do that then it
  175. * hoists the code to get the address of the TLS variable "current"
  176. * out of the while() loop. This is an invalid transformation because
  177. * the sigsetjmp() call may be called when running thread A but
  178. * return in thread B, and so we might be in a different thread
  179. * context each time round the loop.
  180. */
  181. CoroutineAction __attribute__((noinline))
  182. qemu_coroutine_switch(Coroutine *from_, Coroutine *to_,
  183. CoroutineAction action)
  184. {
  185. CoroutineUContext *from = DO_UPCAST(CoroutineUContext, base, from_);
  186. CoroutineUContext *to = DO_UPCAST(CoroutineUContext, base, to_);
  187. int ret;
  188. void *fake_stack_save = NULL;
  189. current = to_;
  190. ret = sigsetjmp(from->env, 0);
  191. if (ret == 0) {
  192. start_switch_fiber(action == COROUTINE_TERMINATE ?
  193. NULL : &fake_stack_save, to->stack, to->stack_size);
  194. siglongjmp(to->env, action);
  195. }
  196. finish_switch_fiber(fake_stack_save);
  197. return ret;
  198. }
  199. Coroutine *qemu_coroutine_self(void)
  200. {
  201. if (!current) {
  202. current = &leader.base;
  203. }
  204. return current;
  205. }
  206. bool qemu_in_coroutine(void)
  207. {
  208. return current && current->caller;
  209. }