tests/docker: remove unused feature "ccache"
[qemu/rayw.git] / util / coroutine-sigaltstack.c
bloba7c33665538b6e8ad32e5fb13474b73f1cf03cb2
1 /*
2 * sigaltstack coroutine initialization code
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>
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.
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.
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/>.
24 /* XXX Is there a nicer way to disable glibc's stack check for longjmp? */
25 #ifdef _FORTIFY_SOURCE
26 #undef _FORTIFY_SOURCE
27 #endif
28 #include "qemu/osdep.h"
29 #include <pthread.h>
30 #include "qemu-common.h"
31 #include "qemu/coroutine_int.h"
33 typedef struct {
34 Coroutine base;
35 void *stack;
36 sigjmp_buf env;
37 } CoroutineUContext;
39 /**
40 * Per-thread coroutine bookkeeping
42 typedef struct {
43 /** Currently executing coroutine */
44 Coroutine *current;
46 /** The default coroutine */
47 CoroutineUContext leader;
49 /** Information for the signal handler (trampoline) */
50 sigjmp_buf tr_reenter;
51 volatile sig_atomic_t tr_called;
52 void *tr_handler;
53 } CoroutineThreadState;
55 static pthread_key_t thread_state_key;
57 static CoroutineThreadState *coroutine_get_thread_state(void)
59 CoroutineThreadState *s = pthread_getspecific(thread_state_key);
61 if (!s) {
62 s = g_malloc0(sizeof(*s));
63 s->current = &s->leader.base;
64 pthread_setspecific(thread_state_key, s);
66 return s;
69 static void qemu_coroutine_thread_cleanup(void *opaque)
71 CoroutineThreadState *s = opaque;
73 g_free(s);
76 static void __attribute__((constructor)) coroutine_init(void)
78 int ret;
80 ret = pthread_key_create(&thread_state_key, qemu_coroutine_thread_cleanup);
81 if (ret != 0) {
82 fprintf(stderr, "unable to create leader key: %s\n", strerror(errno));
83 abort();
87 /* "boot" function
88 * This is what starts the coroutine, is called from the trampoline
89 * (from the signal handler when it is not signal handling, read ahead
90 * for more information).
92 static void coroutine_bootstrap(CoroutineUContext *self, Coroutine *co)
94 /* Initialize longjmp environment and switch back the caller */
95 if (!sigsetjmp(self->env, 0)) {
96 siglongjmp(*(sigjmp_buf *)co->entry_arg, 1);
99 while (true) {
100 co->entry(co->entry_arg);
101 qemu_coroutine_switch(co, co->caller, COROUTINE_TERMINATE);
106 * This is used as the signal handler. This is called with the brand new stack
107 * (thanks to sigaltstack). We have to return, given that this is a signal
108 * handler and the sigmask and some other things are changed.
110 static void coroutine_trampoline(int signal)
112 CoroutineUContext *self;
113 Coroutine *co;
114 CoroutineThreadState *coTS;
116 /* Get the thread specific information */
117 coTS = coroutine_get_thread_state();
118 self = coTS->tr_handler;
119 coTS->tr_called = 1;
120 co = &self->base;
123 * Here we have to do a bit of a ping pong between the caller, given that
124 * this is a signal handler and we have to do a return "soon". Then the
125 * caller can reestablish everything and do a siglongjmp here again.
127 if (!sigsetjmp(coTS->tr_reenter, 0)) {
128 return;
132 * Ok, the caller has siglongjmp'ed back to us, so now prepare
133 * us for the real machine state switching. We have to jump
134 * into another function here to get a new stack context for
135 * the auto variables (which have to be auto-variables
136 * because the start of the thread happens later). Else with
137 * PIC (i.e. Position Independent Code which is used when PTH
138 * is built as a shared library) most platforms would
139 * horrible core dump as experience showed.
141 coroutine_bootstrap(self, co);
144 Coroutine *qemu_coroutine_new(void)
146 const size_t stack_size = 1 << 20;
147 CoroutineUContext *co;
148 CoroutineThreadState *coTS;
149 struct sigaction sa;
150 struct sigaction osa;
151 stack_t ss;
152 stack_t oss;
153 sigset_t sigs;
154 sigset_t osigs;
155 sigjmp_buf old_env;
157 /* The way to manipulate stack is with the sigaltstack function. We
158 * prepare a stack, with it delivering a signal to ourselves and then
159 * put sigsetjmp/siglongjmp where needed.
160 * This has been done keeping coroutine-ucontext as a model and with the
161 * pth ideas (GNU Portable Threads). See coroutine-ucontext for the basics
162 * of the coroutines and see pth_mctx.c (from the pth project) for the
163 * sigaltstack way of manipulating stacks.
166 co = g_malloc0(sizeof(*co));
167 co->stack = g_malloc(stack_size);
168 co->base.entry_arg = &old_env; /* stash away our jmp_buf */
170 coTS = coroutine_get_thread_state();
171 coTS->tr_handler = co;
174 * Preserve the SIGUSR2 signal state, block SIGUSR2,
175 * and establish our signal handler. The signal will
176 * later transfer control onto the signal stack.
178 sigemptyset(&sigs);
179 sigaddset(&sigs, SIGUSR2);
180 pthread_sigmask(SIG_BLOCK, &sigs, &osigs);
181 sa.sa_handler = coroutine_trampoline;
182 sigfillset(&sa.sa_mask);
183 sa.sa_flags = SA_ONSTACK;
184 if (sigaction(SIGUSR2, &sa, &osa) != 0) {
185 abort();
189 * Set the new stack.
191 ss.ss_sp = co->stack;
192 ss.ss_size = stack_size;
193 ss.ss_flags = 0;
194 if (sigaltstack(&ss, &oss) < 0) {
195 abort();
199 * Now transfer control onto the signal stack and set it up.
200 * It will return immediately via "return" after the sigsetjmp()
201 * was performed. Be careful here with race conditions. The
202 * signal can be delivered the first time sigsuspend() is
203 * called.
205 coTS->tr_called = 0;
206 pthread_kill(pthread_self(), SIGUSR2);
207 sigfillset(&sigs);
208 sigdelset(&sigs, SIGUSR2);
209 while (!coTS->tr_called) {
210 sigsuspend(&sigs);
214 * Inform the system that we are back off the signal stack by
215 * removing the alternative signal stack. Be careful here: It
216 * first has to be disabled, before it can be removed.
218 sigaltstack(NULL, &ss);
219 ss.ss_flags = SS_DISABLE;
220 if (sigaltstack(&ss, NULL) < 0) {
221 abort();
223 sigaltstack(NULL, &ss);
224 if (!(oss.ss_flags & SS_DISABLE)) {
225 sigaltstack(&oss, NULL);
229 * Restore the old SIGUSR2 signal handler and mask
231 sigaction(SIGUSR2, &osa, NULL);
232 pthread_sigmask(SIG_SETMASK, &osigs, NULL);
235 * Now enter the trampoline again, but this time not as a signal
236 * handler. Instead we jump into it directly. The functionally
237 * redundant ping-pong pointer arithmetic is necessary to avoid
238 * type-conversion warnings related to the `volatile' qualifier and
239 * the fact that `jmp_buf' usually is an array type.
241 if (!sigsetjmp(old_env, 0)) {
242 siglongjmp(coTS->tr_reenter, 1);
246 * Ok, we returned again, so now we're finished
249 return &co->base;
252 void qemu_coroutine_delete(Coroutine *co_)
254 CoroutineUContext *co = DO_UPCAST(CoroutineUContext, base, co_);
256 g_free(co->stack);
257 g_free(co);
260 CoroutineAction qemu_coroutine_switch(Coroutine *from_, Coroutine *to_,
261 CoroutineAction action)
263 CoroutineUContext *from = DO_UPCAST(CoroutineUContext, base, from_);
264 CoroutineUContext *to = DO_UPCAST(CoroutineUContext, base, to_);
265 CoroutineThreadState *s = coroutine_get_thread_state();
266 int ret;
268 s->current = to_;
270 ret = sigsetjmp(from->env, 0);
271 if (ret == 0) {
272 siglongjmp(to->env, action);
274 return ret;
277 Coroutine *qemu_coroutine_self(void)
279 CoroutineThreadState *s = coroutine_get_thread_state();
281 return s->current;
284 bool qemu_in_coroutine(void)
286 CoroutineThreadState *s = pthread_getspecific(thread_state_key);
288 return s && s->current->caller;