coroutine-ucontext.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  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 <stdlib.h>
  25. #include <setjmp.h>
  26. #include <stdint.h>
  27. #include <pthread.h>
  28. #include <ucontext.h>
  29. #include "qemu-common.h"
  30. #include "qemu-coroutine-int.h"
  31. #ifdef CONFIG_VALGRIND_H
  32. #include <valgrind/valgrind.h>
  33. #endif
  34. enum {
  35. /* Maximum free pool size prevents holding too many freed coroutines */
  36. POOL_MAX_SIZE = 64,
  37. };
  38. /** Free list to speed up creation */
  39. static QSLIST_HEAD(, Coroutine) pool = QSLIST_HEAD_INITIALIZER(pool);
  40. static unsigned int pool_size;
  41. typedef struct {
  42. Coroutine base;
  43. void *stack;
  44. jmp_buf env;
  45. #ifdef CONFIG_VALGRIND_H
  46. unsigned int valgrind_stack_id;
  47. #endif
  48. } CoroutineUContext;
  49. /**
  50. * Per-thread coroutine bookkeeping
  51. */
  52. typedef struct {
  53. /** Currently executing coroutine */
  54. Coroutine *current;
  55. /** The default coroutine */
  56. CoroutineUContext leader;
  57. } CoroutineThreadState;
  58. static pthread_key_t thread_state_key;
  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 CoroutineThreadState *coroutine_get_thread_state(void)
  69. {
  70. CoroutineThreadState *s = pthread_getspecific(thread_state_key);
  71. if (!s) {
  72. s = g_malloc0(sizeof(*s));
  73. s->current = &s->leader.base;
  74. pthread_setspecific(thread_state_key, s);
  75. }
  76. return s;
  77. }
  78. static void qemu_coroutine_thread_cleanup(void *opaque)
  79. {
  80. CoroutineThreadState *s = opaque;
  81. g_free(s);
  82. }
  83. static void __attribute__((destructor)) coroutine_cleanup(void)
  84. {
  85. Coroutine *co;
  86. Coroutine *tmp;
  87. QSLIST_FOREACH_SAFE(co, &pool, pool_next, tmp) {
  88. g_free(DO_UPCAST(CoroutineUContext, base, co)->stack);
  89. g_free(co);
  90. }
  91. }
  92. static void __attribute__((constructor)) coroutine_init(void)
  93. {
  94. int ret;
  95. ret = pthread_key_create(&thread_state_key, qemu_coroutine_thread_cleanup);
  96. if (ret != 0) {
  97. fprintf(stderr, "unable to create leader key: %s\n", strerror(errno));
  98. abort();
  99. }
  100. }
  101. static void coroutine_trampoline(int i0, int i1)
  102. {
  103. union cc_arg arg;
  104. CoroutineUContext *self;
  105. Coroutine *co;
  106. arg.i[0] = i0;
  107. arg.i[1] = i1;
  108. self = arg.p;
  109. co = &self->base;
  110. /* Initialize longjmp environment and switch back the caller */
  111. if (!setjmp(self->env)) {
  112. longjmp(*(jmp_buf *)co->entry_arg, 1);
  113. }
  114. while (true) {
  115. co->entry(co->entry_arg);
  116. qemu_coroutine_switch(co, co->caller, COROUTINE_TERMINATE);
  117. }
  118. }
  119. static Coroutine *coroutine_new(void)
  120. {
  121. const size_t stack_size = 1 << 20;
  122. CoroutineUContext *co;
  123. ucontext_t old_uc, uc;
  124. jmp_buf old_env;
  125. union cc_arg arg = {0};
  126. /* The ucontext functions preserve signal masks which incurs a system call
  127. * overhead. setjmp()/longjmp() does not preserve signal masks but only
  128. * works on the current stack. Since we need a way to create and switch to
  129. * a new stack, use the ucontext functions for that but setjmp()/longjmp()
  130. * for everything else.
  131. */
  132. if (getcontext(&uc) == -1) {
  133. abort();
  134. }
  135. co = g_malloc0(sizeof(*co));
  136. co->stack = g_malloc(stack_size);
  137. co->base.entry_arg = &old_env; /* stash away our jmp_buf */
  138. uc.uc_link = &old_uc;
  139. uc.uc_stack.ss_sp = co->stack;
  140. uc.uc_stack.ss_size = stack_size;
  141. uc.uc_stack.ss_flags = 0;
  142. #ifdef CONFIG_VALGRIND_H
  143. co->valgrind_stack_id =
  144. VALGRIND_STACK_REGISTER(co->stack, co->stack + stack_size);
  145. #endif
  146. arg.p = co;
  147. makecontext(&uc, (void (*)(void))coroutine_trampoline,
  148. 2, arg.i[0], arg.i[1]);
  149. /* swapcontext() in, longjmp() back out */
  150. if (!setjmp(old_env)) {
  151. swapcontext(&old_uc, &uc);
  152. }
  153. return &co->base;
  154. }
  155. Coroutine *qemu_coroutine_new(void)
  156. {
  157. Coroutine *co;
  158. co = QSLIST_FIRST(&pool);
  159. if (co) {
  160. QSLIST_REMOVE_HEAD(&pool, pool_next);
  161. pool_size--;
  162. } else {
  163. co = coroutine_new();
  164. }
  165. return co;
  166. }
  167. #ifdef CONFIG_VALGRIND_H
  168. #ifdef CONFIG_PRAGMA_DISABLE_UNUSED_BUT_SET
  169. /* Work around an unused variable in the valgrind.h macro... */
  170. #pragma GCC diagnostic ignored "-Wunused-but-set-variable"
  171. #endif
  172. static inline void valgrind_stack_deregister(CoroutineUContext *co)
  173. {
  174. VALGRIND_STACK_DEREGISTER(co->valgrind_stack_id);
  175. }
  176. #ifdef CONFIG_PRAGMA_DISABLE_UNUSED_BUT_SET
  177. #pragma GCC diagnostic error "-Wunused-but-set-variable"
  178. #endif
  179. #endif
  180. void qemu_coroutine_delete(Coroutine *co_)
  181. {
  182. CoroutineUContext *co = DO_UPCAST(CoroutineUContext, base, co_);
  183. if (pool_size < POOL_MAX_SIZE) {
  184. QSLIST_INSERT_HEAD(&pool, &co->base, pool_next);
  185. co->base.caller = NULL;
  186. pool_size++;
  187. return;
  188. }
  189. #ifdef CONFIG_VALGRIND_H
  190. valgrind_stack_deregister(co);
  191. #endif
  192. g_free(co->stack);
  193. g_free(co);
  194. }
  195. CoroutineAction qemu_coroutine_switch(Coroutine *from_, Coroutine *to_,
  196. CoroutineAction action)
  197. {
  198. CoroutineUContext *from = DO_UPCAST(CoroutineUContext, base, from_);
  199. CoroutineUContext *to = DO_UPCAST(CoroutineUContext, base, to_);
  200. CoroutineThreadState *s = coroutine_get_thread_state();
  201. int ret;
  202. s->current = to_;
  203. ret = setjmp(from->env);
  204. if (ret == 0) {
  205. longjmp(to->env, action);
  206. }
  207. return ret;
  208. }
  209. Coroutine *qemu_coroutine_self(void)
  210. {
  211. CoroutineThreadState *s = coroutine_get_thread_state();
  212. return s->current;
  213. }
  214. bool qemu_in_coroutine(void)
  215. {
  216. CoroutineThreadState *s = pthread_getspecific(thread_state_key);
  217. return s && s->current->caller;
  218. }