]>
Commit | Line | Data |
---|---|---|
00dccaf1 KW |
1 | /* |
2 | * ucontext coroutine initialization code | |
3 | * | |
4 | * Copyright (C) 2006 Anthony Liguori <[email protected]> | |
5 | * Copyright (C) 2011 Kevin Wolf <[email protected]> | |
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 | ||
21 | /* XXX Is there a nicer way to disable glibc's stack check for longjmp? */ | |
22 | #ifdef _FORTIFY_SOURCE | |
23 | #undef _FORTIFY_SOURCE | |
24 | #endif | |
aafd7584 | 25 | #include "qemu/osdep.h" |
00dccaf1 | 26 | #include <ucontext.h> |
10817bf0 | 27 | #include "qemu/coroutine_int.h" |
00dccaf1 | 28 | |
3f4349dc KW |
29 | #ifdef CONFIG_VALGRIND_H |
30 | #include <valgrind/valgrind.h> | |
31 | #endif | |
32 | ||
d83414e1 MAL |
33 | #if defined(__SANITIZE_ADDRESS__) || __has_feature(address_sanitizer) |
34 | #ifdef CONFIG_ASAN_IFACE_FIBER | |
35 | #define CONFIG_ASAN 1 | |
36 | #include <sanitizer/asan_interface.h> | |
37 | #endif | |
38 | #endif | |
39 | ||
0aebab04 LY |
40 | #ifdef CONFIG_TSAN |
41 | #include <sanitizer/tsan_interface.h> | |
42 | #endif | |
43 | ||
00dccaf1 KW |
44 | typedef struct { |
45 | Coroutine base; | |
46 | void *stack; | |
ddba1591 | 47 | size_t stack_size; |
58ebc2c3 DB |
48 | #ifdef CONFIG_SAFESTACK |
49 | /* Need an unsafe stack for each coroutine */ | |
50 | void *unsafe_stack; | |
51 | size_t unsafe_stack_size; | |
52 | #endif | |
6ab7e546 | 53 | sigjmp_buf env; |
3f4349dc | 54 | |
995f5c3c | 55 | #ifdef CONFIG_TSAN |
0aebab04 LY |
56 | void *tsan_co_fiber; |
57 | void *tsan_caller_fiber; | |
995f5c3c | 58 | #endif |
0aebab04 | 59 | |
3f4349dc KW |
60 | #ifdef CONFIG_VALGRIND_H |
61 | unsigned int valgrind_stack_id; | |
62 | #endif | |
63 | ||
00dccaf1 KW |
64 | } CoroutineUContext; |
65 | ||
66 | /** | |
67 | * Per-thread coroutine bookkeeping | |
68 | */ | |
d1d1b206 PB |
69 | static __thread CoroutineUContext leader; |
70 | static __thread Coroutine *current; | |
00dccaf1 KW |
71 | |
72 | /* | |
73 | * va_args to makecontext() must be type 'int', so passing | |
74 | * the pointer we need may require several int args. This | |
75 | * union is a quick hack to let us do that | |
76 | */ | |
77 | union cc_arg { | |
78 | void *p; | |
79 | int i[2]; | |
80 | }; | |
81 | ||
995f5c3c RF |
82 | /* |
83 | * QEMU_ALWAYS_INLINE only does so if __OPTIMIZE__, so we cannot use it. | |
84 | * always_inline is required to avoid TSan runtime fatal errors. | |
85 | */ | |
0aebab04 LY |
86 | static inline __attribute__((always_inline)) |
87 | void on_new_fiber(CoroutineUContext *co) | |
88 | { | |
89 | #ifdef CONFIG_TSAN | |
90 | co->tsan_co_fiber = __tsan_create_fiber(0); /* flags: sync on switch */ | |
91 | co->tsan_caller_fiber = __tsan_get_current_fiber(); | |
92 | #endif | |
93 | } | |
94 | ||
995f5c3c | 95 | /* always_inline is required to avoid TSan runtime fatal errors. */ |
0aebab04 LY |
96 | static inline __attribute__((always_inline)) |
97 | void finish_switch_fiber(void *fake_stack_save) | |
d83414e1 MAL |
98 | { |
99 | #ifdef CONFIG_ASAN | |
100 | const void *bottom_old; | |
101 | size_t size_old; | |
102 | ||
103 | __sanitizer_finish_switch_fiber(fake_stack_save, &bottom_old, &size_old); | |
104 | ||
105 | if (!leader.stack) { | |
106 | leader.stack = (void *)bottom_old; | |
107 | leader.stack_size = size_old; | |
108 | } | |
109 | #endif | |
0aebab04 LY |
110 | #ifdef CONFIG_TSAN |
111 | if (fake_stack_save) { | |
112 | __tsan_release(fake_stack_save); | |
113 | __tsan_switch_to_fiber(fake_stack_save, 0); /* 0=synchronize */ | |
114 | } | |
115 | #endif | |
d83414e1 MAL |
116 | } |
117 | ||
995f5c3c RF |
118 | /* always_inline is required to avoid TSan runtime fatal errors. */ |
119 | static inline __attribute__((always_inline)) | |
120 | void start_switch_fiber_asan(CoroutineAction action, void **fake_stack_save, | |
121 | const void *bottom, size_t size) | |
d83414e1 MAL |
122 | { |
123 | #ifdef CONFIG_ASAN | |
0aebab04 LY |
124 | __sanitizer_start_switch_fiber( |
125 | action == COROUTINE_TERMINATE ? NULL : fake_stack_save, | |
126 | bottom, size); | |
127 | #endif | |
995f5c3c RF |
128 | } |
129 | ||
130 | /* always_inline is required to avoid TSan runtime fatal errors. */ | |
131 | static inline __attribute__((always_inline)) | |
132 | void start_switch_fiber_tsan(void **fake_stack_save, | |
133 | CoroutineUContext *co, | |
134 | bool caller) | |
135 | { | |
0aebab04 | 136 | #ifdef CONFIG_TSAN |
995f5c3c RF |
137 | void *new_fiber = caller ? |
138 | co->tsan_caller_fiber : | |
139 | co->tsan_co_fiber; | |
140 | void *curr_fiber = __tsan_get_current_fiber(); | |
0aebab04 LY |
141 | __tsan_acquire(curr_fiber); |
142 | ||
143 | *fake_stack_save = curr_fiber; | |
144 | __tsan_switch_to_fiber(new_fiber, 0); /* 0=synchronize */ | |
d83414e1 MAL |
145 | #endif |
146 | } | |
147 | ||
00dccaf1 KW |
148 | static void coroutine_trampoline(int i0, int i1) |
149 | { | |
150 | union cc_arg arg; | |
151 | CoroutineUContext *self; | |
152 | Coroutine *co; | |
d83414e1 MAL |
153 | void *fake_stack_save = NULL; |
154 | ||
155 | finish_switch_fiber(NULL); | |
00dccaf1 KW |
156 | |
157 | arg.i[0] = i0; | |
158 | arg.i[1] = i1; | |
159 | self = arg.p; | |
160 | co = &self->base; | |
161 | ||
162 | /* Initialize longjmp environment and switch back the caller */ | |
6ab7e546 | 163 | if (!sigsetjmp(self->env, 0)) { |
995f5c3c RF |
164 | start_switch_fiber_asan(COROUTINE_YIELD, &fake_stack_save, leader.stack, |
165 | leader.stack_size); | |
166 | start_switch_fiber_tsan(&fake_stack_save, self, true); /* true=caller */ | |
6ab7e546 | 167 | siglongjmp(*(sigjmp_buf *)co->entry_arg, 1); |
00dccaf1 KW |
168 | } |
169 | ||
d83414e1 MAL |
170 | finish_switch_fiber(fake_stack_save); |
171 | ||
00dccaf1 KW |
172 | while (true) { |
173 | co->entry(co->entry_arg); | |
174 | qemu_coroutine_switch(co, co->caller, COROUTINE_TERMINATE); | |
175 | } | |
176 | } | |
177 | ||
40239784 | 178 | Coroutine *qemu_coroutine_new(void) |
00dccaf1 | 179 | { |
00dccaf1 KW |
180 | CoroutineUContext *co; |
181 | ucontext_t old_uc, uc; | |
6ab7e546 | 182 | sigjmp_buf old_env; |
32b74677 | 183 | union cc_arg arg = {0}; |
d83414e1 | 184 | void *fake_stack_save = NULL; |
00dccaf1 | 185 | |
6ab7e546 PM |
186 | /* The ucontext functions preserve signal masks which incurs a |
187 | * system call overhead. sigsetjmp(buf, 0)/siglongjmp() does not | |
188 | * preserve signal masks but only works on the current stack. | |
189 | * Since we need a way to create and switch to a new stack, use | |
190 | * the ucontext functions for that but sigsetjmp()/siglongjmp() for | |
191 | * everything else. | |
00dccaf1 KW |
192 | */ |
193 | ||
194 | if (getcontext(&uc) == -1) { | |
195 | abort(); | |
196 | } | |
197 | ||
7267c094 | 198 | co = g_malloc0(sizeof(*co)); |
ddba1591 PL |
199 | co->stack_size = COROUTINE_STACK_SIZE; |
200 | co->stack = qemu_alloc_stack(&co->stack_size); | |
58ebc2c3 DB |
201 | #ifdef CONFIG_SAFESTACK |
202 | co->unsafe_stack_size = COROUTINE_STACK_SIZE; | |
203 | co->unsafe_stack = qemu_alloc_stack(&co->unsafe_stack_size); | |
204 | #endif | |
00dccaf1 KW |
205 | co->base.entry_arg = &old_env; /* stash away our jmp_buf */ |
206 | ||
207 | uc.uc_link = &old_uc; | |
208 | uc.uc_stack.ss_sp = co->stack; | |
ddba1591 | 209 | uc.uc_stack.ss_size = co->stack_size; |
00dccaf1 KW |
210 | uc.uc_stack.ss_flags = 0; |
211 | ||
3f4349dc KW |
212 | #ifdef CONFIG_VALGRIND_H |
213 | co->valgrind_stack_id = | |
ddba1591 | 214 | VALGRIND_STACK_REGISTER(co->stack, co->stack + co->stack_size); |
3f4349dc KW |
215 | #endif |
216 | ||
00dccaf1 KW |
217 | arg.p = co; |
218 | ||
0aebab04 | 219 | on_new_fiber(co); |
00dccaf1 KW |
220 | makecontext(&uc, (void (*)(void))coroutine_trampoline, |
221 | 2, arg.i[0], arg.i[1]); | |
222 | ||
6ab7e546 PM |
223 | /* swapcontext() in, siglongjmp() back out */ |
224 | if (!sigsetjmp(old_env, 0)) { | |
995f5c3c RF |
225 | start_switch_fiber_asan(COROUTINE_YIELD, &fake_stack_save, co->stack, |
226 | co->stack_size); | |
227 | start_switch_fiber_tsan(&fake_stack_save, | |
228 | co, false); /* false=not caller */ | |
58ebc2c3 DB |
229 | |
230 | #ifdef CONFIG_SAFESTACK | |
231 | /* | |
232 | * Before we swap the context, set the new unsafe stack | |
233 | * The unsafe stack grows just like the normal stack, so start from | |
234 | * the last usable location of the memory area. | |
235 | * NOTE: we don't have to re-set the usp afterwards because we are | |
236 | * coming back to this context through a siglongjmp. | |
237 | * The compiler already wrapped the corresponding sigsetjmp call with | |
238 | * code that saves the usp on the (safe) stack before the call, and | |
239 | * restores it right after (which is where we return with siglongjmp). | |
240 | */ | |
241 | void *usp = co->unsafe_stack + co->unsafe_stack_size; | |
242 | __safestack_unsafe_stack_ptr = usp; | |
243 | #endif | |
244 | ||
00dccaf1 KW |
245 | swapcontext(&old_uc, &uc); |
246 | } | |
d83414e1 MAL |
247 | |
248 | finish_switch_fiber(fake_stack_save); | |
249 | ||
00dccaf1 KW |
250 | return &co->base; |
251 | } | |
252 | ||
3f4349dc KW |
253 | #ifdef CONFIG_VALGRIND_H |
254 | /* Work around an unused variable in the valgrind.h macro... */ | |
7aa12aa2 | 255 | #if !defined(__clang__) |
e6f53fd5 | 256 | #pragma GCC diagnostic push |
3f4349dc | 257 | #pragma GCC diagnostic ignored "-Wunused-but-set-variable" |
06d71fa1 | 258 | #endif |
3f4349dc KW |
259 | static inline void valgrind_stack_deregister(CoroutineUContext *co) |
260 | { | |
261 | VALGRIND_STACK_DEREGISTER(co->valgrind_stack_id); | |
262 | } | |
7aa12aa2 | 263 | #if !defined(__clang__) |
e6f53fd5 | 264 | #pragma GCC diagnostic pop |
3f4349dc | 265 | #endif |
06d71fa1 | 266 | #endif |
3f4349dc | 267 | |
00dccaf1 KW |
268 | void qemu_coroutine_delete(Coroutine *co_) |
269 | { | |
00dccaf1 KW |
270 | CoroutineUContext *co = DO_UPCAST(CoroutineUContext, base, co_); |
271 | ||
3f4349dc KW |
272 | #ifdef CONFIG_VALGRIND_H |
273 | valgrind_stack_deregister(co); | |
274 | #endif | |
275 | ||
ddba1591 | 276 | qemu_free_stack(co->stack, co->stack_size); |
58ebc2c3 DB |
277 | #ifdef CONFIG_SAFESTACK |
278 | qemu_free_stack(co->unsafe_stack, co->unsafe_stack_size); | |
279 | #endif | |
7267c094 | 280 | g_free(co); |
00dccaf1 KW |
281 | } |
282 | ||
d1d1b206 PB |
283 | /* This function is marked noinline to prevent GCC from inlining it |
284 | * into coroutine_trampoline(). If we allow it to do that then it | |
285 | * hoists the code to get the address of the TLS variable "current" | |
286 | * out of the while() loop. This is an invalid transformation because | |
287 | * the sigsetjmp() call may be called when running thread A but | |
288 | * return in thread B, and so we might be in a different thread | |
289 | * context each time round the loop. | |
290 | */ | |
291 | CoroutineAction __attribute__((noinline)) | |
292 | qemu_coroutine_switch(Coroutine *from_, Coroutine *to_, | |
293 | CoroutineAction action) | |
00dccaf1 KW |
294 | { |
295 | CoroutineUContext *from = DO_UPCAST(CoroutineUContext, base, from_); | |
296 | CoroutineUContext *to = DO_UPCAST(CoroutineUContext, base, to_); | |
00dccaf1 | 297 | int ret; |
d83414e1 | 298 | void *fake_stack_save = NULL; |
00dccaf1 | 299 | |
d1d1b206 | 300 | current = to_; |
00dccaf1 | 301 | |
6ab7e546 | 302 | ret = sigsetjmp(from->env, 0); |
00dccaf1 | 303 | if (ret == 0) { |
995f5c3c RF |
304 | start_switch_fiber_asan(action, &fake_stack_save, to->stack, |
305 | to->stack_size); | |
306 | start_switch_fiber_tsan(&fake_stack_save, | |
307 | to, false); /* false=not caller */ | |
6ab7e546 | 308 | siglongjmp(to->env, action); |
00dccaf1 | 309 | } |
d83414e1 MAL |
310 | |
311 | finish_switch_fiber(fake_stack_save); | |
312 | ||
00dccaf1 KW |
313 | return ret; |
314 | } | |
315 | ||
316 | Coroutine *qemu_coroutine_self(void) | |
317 | { | |
d1d1b206 PB |
318 | if (!current) { |
319 | current = &leader.base; | |
320 | } | |
0aebab04 LY |
321 | #ifdef CONFIG_TSAN |
322 | if (!leader.tsan_co_fiber) { | |
323 | leader.tsan_co_fiber = __tsan_get_current_fiber(); | |
324 | } | |
325 | #endif | |
d1d1b206 | 326 | return current; |
00dccaf1 KW |
327 | } |
328 | ||
329 | bool qemu_in_coroutine(void) | |
330 | { | |
d1d1b206 | 331 | return current && current->caller; |
00dccaf1 | 332 | } |