coroutine-sigaltstack.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  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. #undef _FORTIFY_SOURCE
  25. #define _FORTIFY_SOURCE 0
  26. #include "qemu/osdep.h"
  27. #include <pthread.h>
  28. #include "qemu/coroutine_int.h"
  29. #ifdef CONFIG_SAFESTACK
  30. #error "SafeStack is not compatible with code run in alternate signal stacks"
  31. #endif
  32. typedef struct {
  33. Coroutine base;
  34. void *stack;
  35. size_t stack_size;
  36. sigjmp_buf env;
  37. } CoroutineSigAltStack;
  38. /**
  39. * Per-thread coroutine bookkeeping
  40. */
  41. typedef struct {
  42. /** Currently executing coroutine */
  43. Coroutine *current;
  44. /** The default coroutine */
  45. CoroutineSigAltStack leader;
  46. /** Information for the signal handler (trampoline) */
  47. sigjmp_buf tr_reenter;
  48. volatile sig_atomic_t tr_called;
  49. void *tr_handler;
  50. } CoroutineThreadState;
  51. static pthread_key_t thread_state_key;
  52. static CoroutineThreadState *coroutine_get_thread_state(void)
  53. {
  54. CoroutineThreadState *s = pthread_getspecific(thread_state_key);
  55. if (!s) {
  56. s = g_malloc0(sizeof(*s));
  57. s->current = &s->leader.base;
  58. pthread_setspecific(thread_state_key, s);
  59. }
  60. return s;
  61. }
  62. static void qemu_coroutine_thread_cleanup(void *opaque)
  63. {
  64. CoroutineThreadState *s = opaque;
  65. g_free(s);
  66. }
  67. static void __attribute__((constructor)) coroutine_init(void)
  68. {
  69. int ret;
  70. ret = pthread_key_create(&thread_state_key, qemu_coroutine_thread_cleanup);
  71. if (ret != 0) {
  72. fprintf(stderr, "unable to create leader key: %s\n", strerror(errno));
  73. abort();
  74. }
  75. }
  76. /* "boot" function
  77. * This is what starts the coroutine, is called from the trampoline
  78. * (from the signal handler when it is not signal handling, read ahead
  79. * for more information).
  80. */
  81. static void coroutine_bootstrap(CoroutineSigAltStack *self, Coroutine *co)
  82. {
  83. /* Initialize longjmp environment and switch back the caller */
  84. if (!sigsetjmp(self->env, 0)) {
  85. siglongjmp(*(sigjmp_buf *)co->entry_arg, 1);
  86. }
  87. while (true) {
  88. co->entry(co->entry_arg);
  89. qemu_coroutine_switch(co, co->caller, COROUTINE_TERMINATE);
  90. }
  91. }
  92. /*
  93. * This is used as the signal handler. This is called with the brand new stack
  94. * (thanks to sigaltstack). We have to return, given that this is a signal
  95. * handler and the sigmask and some other things are changed.
  96. */
  97. static void coroutine_trampoline(int signal)
  98. {
  99. CoroutineSigAltStack *self;
  100. Coroutine *co;
  101. CoroutineThreadState *coTS;
  102. /* Get the thread specific information */
  103. coTS = coroutine_get_thread_state();
  104. self = coTS->tr_handler;
  105. coTS->tr_called = 1;
  106. co = &self->base;
  107. /*
  108. * Here we have to do a bit of a ping pong between the caller, given that
  109. * this is a signal handler and we have to do a return "soon". Then the
  110. * caller can reestablish everything and do a siglongjmp here again.
  111. */
  112. if (!sigsetjmp(coTS->tr_reenter, 0)) {
  113. return;
  114. }
  115. /*
  116. * Ok, the caller has siglongjmp'ed back to us, so now prepare
  117. * us for the real machine state switching. We have to jump
  118. * into another function here to get a new stack context for
  119. * the auto variables (which have to be auto-variables
  120. * because the start of the thread happens later). Else with
  121. * PIC (i.e. Position Independent Code which is used when PTH
  122. * is built as a shared library) most platforms would
  123. * horrible core dump as experience showed.
  124. */
  125. coroutine_bootstrap(self, co);
  126. }
  127. Coroutine *qemu_coroutine_new(void)
  128. {
  129. CoroutineSigAltStack *co;
  130. CoroutineThreadState *coTS;
  131. struct sigaction sa;
  132. struct sigaction osa;
  133. stack_t ss;
  134. stack_t oss;
  135. sigset_t sigs;
  136. sigset_t osigs;
  137. sigjmp_buf old_env;
  138. static pthread_mutex_t sigusr2_mutex = PTHREAD_MUTEX_INITIALIZER;
  139. /* The way to manipulate stack is with the sigaltstack function. We
  140. * prepare a stack, with it delivering a signal to ourselves and then
  141. * put sigsetjmp/siglongjmp where needed.
  142. * This has been done keeping coroutine-ucontext as a model and with the
  143. * pth ideas (GNU Portable Threads). See coroutine-ucontext for the basics
  144. * of the coroutines and see pth_mctx.c (from the pth project) for the
  145. * sigaltstack way of manipulating stacks.
  146. */
  147. co = g_malloc0(sizeof(*co));
  148. co->stack_size = COROUTINE_STACK_SIZE;
  149. co->stack = qemu_alloc_stack(&co->stack_size);
  150. co->base.entry_arg = &old_env; /* stash away our jmp_buf */
  151. coTS = coroutine_get_thread_state();
  152. coTS->tr_handler = co;
  153. /*
  154. * Preserve the SIGUSR2 signal state, block SIGUSR2,
  155. * and establish our signal handler. The signal will
  156. * later transfer control onto the signal stack.
  157. */
  158. sigemptyset(&sigs);
  159. sigaddset(&sigs, SIGUSR2);
  160. pthread_sigmask(SIG_BLOCK, &sigs, &osigs);
  161. sa.sa_handler = coroutine_trampoline;
  162. sigfillset(&sa.sa_mask);
  163. sa.sa_flags = SA_ONSTACK;
  164. /*
  165. * sigaction() is a process-global operation. We must not run
  166. * this code in multiple threads at once.
  167. */
  168. pthread_mutex_lock(&sigusr2_mutex);
  169. if (sigaction(SIGUSR2, &sa, &osa) != 0) {
  170. abort();
  171. }
  172. /*
  173. * Set the new stack.
  174. */
  175. ss.ss_sp = co->stack;
  176. ss.ss_size = co->stack_size;
  177. ss.ss_flags = 0;
  178. if (sigaltstack(&ss, &oss) < 0) {
  179. abort();
  180. }
  181. /*
  182. * Now transfer control onto the signal stack and set it up.
  183. * It will return immediately via "return" after the sigsetjmp()
  184. * was performed. Be careful here with race conditions. The
  185. * signal can be delivered the first time sigsuspend() is
  186. * called.
  187. */
  188. coTS->tr_called = 0;
  189. pthread_kill(pthread_self(), SIGUSR2);
  190. sigfillset(&sigs);
  191. sigdelset(&sigs, SIGUSR2);
  192. while (!coTS->tr_called) {
  193. sigsuspend(&sigs);
  194. }
  195. /*
  196. * Inform the system that we are back off the signal stack by
  197. * removing the alternative signal stack. Be careful here: It
  198. * first has to be disabled, before it can be removed.
  199. */
  200. sigaltstack(NULL, &ss);
  201. ss.ss_flags = SS_DISABLE;
  202. if (sigaltstack(&ss, NULL) < 0) {
  203. abort();
  204. }
  205. sigaltstack(NULL, &ss);
  206. if (!(oss.ss_flags & SS_DISABLE)) {
  207. sigaltstack(&oss, NULL);
  208. }
  209. /*
  210. * Restore the old SIGUSR2 signal handler and mask
  211. */
  212. sigaction(SIGUSR2, &osa, NULL);
  213. pthread_mutex_unlock(&sigusr2_mutex);
  214. pthread_sigmask(SIG_SETMASK, &osigs, NULL);
  215. /*
  216. * Now enter the trampoline again, but this time not as a signal
  217. * handler. Instead we jump into it directly. The functionally
  218. * redundant ping-pong pointer arithmetic is necessary to avoid
  219. * type-conversion warnings related to the `volatile' qualifier and
  220. * the fact that `jmp_buf' usually is an array type.
  221. */
  222. if (!sigsetjmp(old_env, 0)) {
  223. siglongjmp(coTS->tr_reenter, 1);
  224. }
  225. /*
  226. * Ok, we returned again, so now we're finished
  227. */
  228. return &co->base;
  229. }
  230. void qemu_coroutine_delete(Coroutine *co_)
  231. {
  232. CoroutineSigAltStack *co = DO_UPCAST(CoroutineSigAltStack, base, co_);
  233. qemu_free_stack(co->stack, co->stack_size);
  234. g_free(co);
  235. }
  236. CoroutineAction qemu_coroutine_switch(Coroutine *from_, Coroutine *to_,
  237. CoroutineAction action)
  238. {
  239. CoroutineSigAltStack *from = DO_UPCAST(CoroutineSigAltStack, base, from_);
  240. CoroutineSigAltStack *to = DO_UPCAST(CoroutineSigAltStack, base, to_);
  241. CoroutineThreadState *s = coroutine_get_thread_state();
  242. int ret;
  243. s->current = to_;
  244. ret = sigsetjmp(from->env, 0);
  245. if (ret == 0) {
  246. siglongjmp(to->env, action);
  247. }
  248. return ret;
  249. }
  250. Coroutine *qemu_coroutine_self(void)
  251. {
  252. CoroutineThreadState *s = coroutine_get_thread_state();
  253. return s->current;
  254. }
  255. bool qemu_in_coroutine(void)
  256. {
  257. CoroutineThreadState *s = pthread_getspecific(thread_state_key);
  258. return s && s->current->caller;
  259. }