coroutine-sigaltstack.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. /*
  2. * sigaltstack coroutine initialization code
  3. *
  4. * Copyright (C) 2006 Anthony Liguori <anthony@codemonkey.ws>
  5. * Copyright (C) 2011 Kevin Wolf <kwolf@redhat.com>
  6. * Copyright (C) 2012 Alex Barcelo <abarcelo@ac.upc.edu>
  7. ** This file is partly based on pth_mctx.c, from the GNU Portable Threads
  8. ** Copyright (c) 1999-2006 Ralf S. Engelschall <rse@engelschall.com>
  9. *
  10. * This library is free software; you can redistribute it and/or
  11. * modify it under the terms of the GNU Lesser General Public
  12. * License as published by the Free Software Foundation; either
  13. * version 2.1 of the License, or (at your option) any later version.
  14. *
  15. * This library is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  18. * Lesser General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Lesser General Public
  21. * License along with this library; if not, see <http://www.gnu.org/licenses/>.
  22. */
  23. /* XXX Is there a nicer way to disable glibc's stack check for longjmp? */
  24. #ifdef _FORTIFY_SOURCE
  25. #undef _FORTIFY_SOURCE
  26. #endif
  27. #include "qemu/osdep.h"
  28. #include <pthread.h>
  29. #include "qemu-common.h"
  30. #include "qemu/coroutine_int.h"
  31. typedef struct {
  32. Coroutine base;
  33. void *stack;
  34. size_t stack_size;
  35. sigjmp_buf env;
  36. } CoroutineSigAltStack;
  37. /**
  38. * Per-thread coroutine bookkeeping
  39. */
  40. typedef struct {
  41. /** Currently executing coroutine */
  42. Coroutine *current;
  43. /** The default coroutine */
  44. CoroutineSigAltStack leader;
  45. /** Information for the signal handler (trampoline) */
  46. sigjmp_buf tr_reenter;
  47. volatile sig_atomic_t tr_called;
  48. void *tr_handler;
  49. } CoroutineThreadState;
  50. static pthread_key_t thread_state_key;
  51. static CoroutineThreadState *coroutine_get_thread_state(void)
  52. {
  53. CoroutineThreadState *s = pthread_getspecific(thread_state_key);
  54. if (!s) {
  55. s = g_malloc0(sizeof(*s));
  56. s->current = &s->leader.base;
  57. pthread_setspecific(thread_state_key, s);
  58. }
  59. return s;
  60. }
  61. static void qemu_coroutine_thread_cleanup(void *opaque)
  62. {
  63. CoroutineThreadState *s = opaque;
  64. g_free(s);
  65. }
  66. static void __attribute__((constructor)) coroutine_init(void)
  67. {
  68. int ret;
  69. ret = pthread_key_create(&thread_state_key, qemu_coroutine_thread_cleanup);
  70. if (ret != 0) {
  71. fprintf(stderr, "unable to create leader key: %s\n", strerror(errno));
  72. abort();
  73. }
  74. }
  75. /* "boot" function
  76. * This is what starts the coroutine, is called from the trampoline
  77. * (from the signal handler when it is not signal handling, read ahead
  78. * for more information).
  79. */
  80. static void coroutine_bootstrap(CoroutineSigAltStack *self, Coroutine *co)
  81. {
  82. /* Initialize longjmp environment and switch back the caller */
  83. if (!sigsetjmp(self->env, 0)) {
  84. siglongjmp(*(sigjmp_buf *)co->entry_arg, 1);
  85. }
  86. while (true) {
  87. co->entry(co->entry_arg);
  88. qemu_coroutine_switch(co, co->caller, COROUTINE_TERMINATE);
  89. }
  90. }
  91. /*
  92. * This is used as the signal handler. This is called with the brand new stack
  93. * (thanks to sigaltstack). We have to return, given that this is a signal
  94. * handler and the sigmask and some other things are changed.
  95. */
  96. static void coroutine_trampoline(int signal)
  97. {
  98. CoroutineSigAltStack *self;
  99. Coroutine *co;
  100. CoroutineThreadState *coTS;
  101. /* Get the thread specific information */
  102. coTS = coroutine_get_thread_state();
  103. self = coTS->tr_handler;
  104. coTS->tr_called = 1;
  105. co = &self->base;
  106. /*
  107. * Here we have to do a bit of a ping pong between the caller, given that
  108. * this is a signal handler and we have to do a return "soon". Then the
  109. * caller can reestablish everything and do a siglongjmp here again.
  110. */
  111. if (!sigsetjmp(coTS->tr_reenter, 0)) {
  112. return;
  113. }
  114. /*
  115. * Ok, the caller has siglongjmp'ed back to us, so now prepare
  116. * us for the real machine state switching. We have to jump
  117. * into another function here to get a new stack context for
  118. * the auto variables (which have to be auto-variables
  119. * because the start of the thread happens later). Else with
  120. * PIC (i.e. Position Independent Code which is used when PTH
  121. * is built as a shared library) most platforms would
  122. * horrible core dump as experience showed.
  123. */
  124. coroutine_bootstrap(self, co);
  125. }
  126. Coroutine *qemu_coroutine_new(void)
  127. {
  128. CoroutineSigAltStack *co;
  129. CoroutineThreadState *coTS;
  130. struct sigaction sa;
  131. struct sigaction osa;
  132. stack_t ss;
  133. stack_t oss;
  134. sigset_t sigs;
  135. sigset_t osigs;
  136. sigjmp_buf old_env;
  137. /* The way to manipulate stack is with the sigaltstack function. We
  138. * prepare a stack, with it delivering a signal to ourselves and then
  139. * put sigsetjmp/siglongjmp where needed.
  140. * This has been done keeping coroutine-ucontext as a model and with the
  141. * pth ideas (GNU Portable Threads). See coroutine-ucontext for the basics
  142. * of the coroutines and see pth_mctx.c (from the pth project) for the
  143. * sigaltstack way of manipulating stacks.
  144. */
  145. co = g_malloc0(sizeof(*co));
  146. co->stack_size = COROUTINE_STACK_SIZE;
  147. co->stack = qemu_alloc_stack(&co->stack_size);
  148. co->base.entry_arg = &old_env; /* stash away our jmp_buf */
  149. coTS = coroutine_get_thread_state();
  150. coTS->tr_handler = co;
  151. /*
  152. * Preserve the SIGUSR2 signal state, block SIGUSR2,
  153. * and establish our signal handler. The signal will
  154. * later transfer control onto the signal stack.
  155. */
  156. sigemptyset(&sigs);
  157. sigaddset(&sigs, SIGUSR2);
  158. pthread_sigmask(SIG_BLOCK, &sigs, &osigs);
  159. sa.sa_handler = coroutine_trampoline;
  160. sigfillset(&sa.sa_mask);
  161. sa.sa_flags = SA_ONSTACK;
  162. if (sigaction(SIGUSR2, &sa, &osa) != 0) {
  163. abort();
  164. }
  165. /*
  166. * Set the new stack.
  167. */
  168. ss.ss_sp = co->stack;
  169. ss.ss_size = co->stack_size;
  170. ss.ss_flags = 0;
  171. if (sigaltstack(&ss, &oss) < 0) {
  172. abort();
  173. }
  174. /*
  175. * Now transfer control onto the signal stack and set it up.
  176. * It will return immediately via "return" after the sigsetjmp()
  177. * was performed. Be careful here with race conditions. The
  178. * signal can be delivered the first time sigsuspend() is
  179. * called.
  180. */
  181. coTS->tr_called = 0;
  182. pthread_kill(pthread_self(), SIGUSR2);
  183. sigfillset(&sigs);
  184. sigdelset(&sigs, SIGUSR2);
  185. while (!coTS->tr_called) {
  186. sigsuspend(&sigs);
  187. }
  188. /*
  189. * Inform the system that we are back off the signal stack by
  190. * removing the alternative signal stack. Be careful here: It
  191. * first has to be disabled, before it can be removed.
  192. */
  193. sigaltstack(NULL, &ss);
  194. ss.ss_flags = SS_DISABLE;
  195. if (sigaltstack(&ss, NULL) < 0) {
  196. abort();
  197. }
  198. sigaltstack(NULL, &ss);
  199. if (!(oss.ss_flags & SS_DISABLE)) {
  200. sigaltstack(&oss, NULL);
  201. }
  202. /*
  203. * Restore the old SIGUSR2 signal handler and mask
  204. */
  205. sigaction(SIGUSR2, &osa, NULL);
  206. pthread_sigmask(SIG_SETMASK, &osigs, NULL);
  207. /*
  208. * Now enter the trampoline again, but this time not as a signal
  209. * handler. Instead we jump into it directly. The functionally
  210. * redundant ping-pong pointer arithmetic is necessary to avoid
  211. * type-conversion warnings related to the `volatile' qualifier and
  212. * the fact that `jmp_buf' usually is an array type.
  213. */
  214. if (!sigsetjmp(old_env, 0)) {
  215. siglongjmp(coTS->tr_reenter, 1);
  216. }
  217. /*
  218. * Ok, we returned again, so now we're finished
  219. */
  220. return &co->base;
  221. }
  222. void qemu_coroutine_delete(Coroutine *co_)
  223. {
  224. CoroutineSigAltStack *co = DO_UPCAST(CoroutineSigAltStack, base, co_);
  225. qemu_free_stack(co->stack, co->stack_size);
  226. g_free(co);
  227. }
  228. CoroutineAction qemu_coroutine_switch(Coroutine *from_, Coroutine *to_,
  229. CoroutineAction action)
  230. {
  231. CoroutineSigAltStack *from = DO_UPCAST(CoroutineSigAltStack, base, from_);
  232. CoroutineSigAltStack *to = DO_UPCAST(CoroutineSigAltStack, base, to_);
  233. CoroutineThreadState *s = coroutine_get_thread_state();
  234. int ret;
  235. s->current = to_;
  236. ret = sigsetjmp(from->env, 0);
  237. if (ret == 0) {
  238. siglongjmp(to->env, action);
  239. }
  240. return ret;
  241. }
  242. Coroutine *qemu_coroutine_self(void)
  243. {
  244. CoroutineThreadState *s = coroutine_get_thread_state();
  245. return s->current;
  246. }
  247. bool qemu_in_coroutine(void)
  248. {
  249. CoroutineThreadState *s = pthread_getspecific(thread_state_key);
  250. return s && s->current->caller;
  251. }