coroutine-ucontext.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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 "block/coroutine_int.h"
  31. #ifdef CONFIG_VALGRIND_H
  32. #include <valgrind/valgrind.h>
  33. #endif
  34. typedef struct {
  35. Coroutine base;
  36. void *stack;
  37. sigjmp_buf env;
  38. #ifdef CONFIG_VALGRIND_H
  39. unsigned int valgrind_stack_id;
  40. #endif
  41. } CoroutineUContext;
  42. /**
  43. * Per-thread coroutine bookkeeping
  44. */
  45. typedef struct {
  46. /** Currently executing coroutine */
  47. Coroutine *current;
  48. /** The default coroutine */
  49. CoroutineUContext leader;
  50. } CoroutineThreadState;
  51. static pthread_key_t thread_state_key;
  52. /*
  53. * va_args to makecontext() must be type 'int', so passing
  54. * the pointer we need may require several int args. This
  55. * union is a quick hack to let us do that
  56. */
  57. union cc_arg {
  58. void *p;
  59. int i[2];
  60. };
  61. static CoroutineThreadState *coroutine_get_thread_state(void)
  62. {
  63. CoroutineThreadState *s = pthread_getspecific(thread_state_key);
  64. if (!s) {
  65. s = g_malloc0(sizeof(*s));
  66. s->current = &s->leader.base;
  67. pthread_setspecific(thread_state_key, s);
  68. }
  69. return s;
  70. }
  71. static void qemu_coroutine_thread_cleanup(void *opaque)
  72. {
  73. CoroutineThreadState *s = opaque;
  74. g_free(s);
  75. }
  76. static void __attribute__((constructor)) coroutine_init(void)
  77. {
  78. int ret;
  79. ret = pthread_key_create(&thread_state_key, qemu_coroutine_thread_cleanup);
  80. if (ret != 0) {
  81. fprintf(stderr, "unable to create leader key: %s\n", strerror(errno));
  82. abort();
  83. }
  84. }
  85. static void coroutine_trampoline(int i0, int i1)
  86. {
  87. union cc_arg arg;
  88. CoroutineUContext *self;
  89. Coroutine *co;
  90. arg.i[0] = i0;
  91. arg.i[1] = i1;
  92. self = arg.p;
  93. co = &self->base;
  94. /* Initialize longjmp environment and switch back the caller */
  95. if (!sigsetjmp(self->env, 0)) {
  96. siglongjmp(*(sigjmp_buf *)co->entry_arg, 1);
  97. }
  98. while (true) {
  99. co->entry(co->entry_arg);
  100. qemu_coroutine_switch(co, co->caller, COROUTINE_TERMINATE);
  101. }
  102. }
  103. Coroutine *qemu_coroutine_new(void)
  104. {
  105. const size_t stack_size = 1 << 20;
  106. CoroutineUContext *co;
  107. ucontext_t old_uc, uc;
  108. sigjmp_buf old_env;
  109. union cc_arg arg = {0};
  110. /* The ucontext functions preserve signal masks which incurs a
  111. * system call overhead. sigsetjmp(buf, 0)/siglongjmp() does not
  112. * preserve signal masks but only works on the current stack.
  113. * Since we need a way to create and switch to a new stack, use
  114. * the ucontext functions for that but sigsetjmp()/siglongjmp() for
  115. * everything else.
  116. */
  117. if (getcontext(&uc) == -1) {
  118. abort();
  119. }
  120. co = g_malloc0(sizeof(*co));
  121. co->stack = g_malloc(stack_size);
  122. co->base.entry_arg = &old_env; /* stash away our jmp_buf */
  123. uc.uc_link = &old_uc;
  124. uc.uc_stack.ss_sp = co->stack;
  125. uc.uc_stack.ss_size = stack_size;
  126. uc.uc_stack.ss_flags = 0;
  127. #ifdef CONFIG_VALGRIND_H
  128. co->valgrind_stack_id =
  129. VALGRIND_STACK_REGISTER(co->stack, co->stack + stack_size);
  130. #endif
  131. arg.p = co;
  132. makecontext(&uc, (void (*)(void))coroutine_trampoline,
  133. 2, arg.i[0], arg.i[1]);
  134. /* swapcontext() in, siglongjmp() back out */
  135. if (!sigsetjmp(old_env, 0)) {
  136. swapcontext(&old_uc, &uc);
  137. }
  138. return &co->base;
  139. }
  140. #ifdef CONFIG_VALGRIND_H
  141. #ifdef CONFIG_PRAGMA_DIAGNOSTIC_AVAILABLE
  142. /* Work around an unused variable in the valgrind.h macro... */
  143. #pragma GCC diagnostic push
  144. #pragma GCC diagnostic ignored "-Wunused-but-set-variable"
  145. #endif
  146. static inline void valgrind_stack_deregister(CoroutineUContext *co)
  147. {
  148. VALGRIND_STACK_DEREGISTER(co->valgrind_stack_id);
  149. }
  150. #ifdef CONFIG_PRAGMA_DIAGNOSTIC_AVAILABLE
  151. #pragma GCC diagnostic pop
  152. #endif
  153. #endif
  154. void qemu_coroutine_delete(Coroutine *co_)
  155. {
  156. CoroutineUContext *co = DO_UPCAST(CoroutineUContext, base, co_);
  157. #ifdef CONFIG_VALGRIND_H
  158. valgrind_stack_deregister(co);
  159. #endif
  160. g_free(co->stack);
  161. g_free(co);
  162. }
  163. CoroutineAction qemu_coroutine_switch(Coroutine *from_, Coroutine *to_,
  164. CoroutineAction action)
  165. {
  166. CoroutineUContext *from = DO_UPCAST(CoroutineUContext, base, from_);
  167. CoroutineUContext *to = DO_UPCAST(CoroutineUContext, base, to_);
  168. CoroutineThreadState *s = coroutine_get_thread_state();
  169. int ret;
  170. s->current = to_;
  171. ret = sigsetjmp(from->env, 0);
  172. if (ret == 0) {
  173. siglongjmp(to->env, action);
  174. }
  175. return ret;
  176. }
  177. Coroutine *qemu_coroutine_self(void)
  178. {
  179. CoroutineThreadState *s = coroutine_get_thread_state();
  180. return s->current;
  181. }
  182. bool qemu_in_coroutine(void)
  183. {
  184. CoroutineThreadState *s = pthread_getspecific(thread_state_key);
  185. return s && s->current->caller;
  186. }