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