]>
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 | ||
00dccaf1 KW |
40 | typedef struct { |
41 | Coroutine base; | |
42 | void *stack; | |
ddba1591 | 43 | size_t stack_size; |
6ab7e546 | 44 | sigjmp_buf env; |
3f4349dc KW |
45 | |
46 | #ifdef CONFIG_VALGRIND_H | |
47 | unsigned int valgrind_stack_id; | |
48 | #endif | |
49 | ||
00dccaf1 KW |
50 | } CoroutineUContext; |
51 | ||
52 | /** | |
53 | * Per-thread coroutine bookkeeping | |
54 | */ | |
d1d1b206 PB |
55 | static __thread CoroutineUContext leader; |
56 | static __thread Coroutine *current; | |
00dccaf1 KW |
57 | |
58 | /* | |
59 | * va_args to makecontext() must be type 'int', so passing | |
60 | * the pointer we need may require several int args. This | |
61 | * union is a quick hack to let us do that | |
62 | */ | |
63 | union cc_arg { | |
64 | void *p; | |
65 | int i[2]; | |
66 | }; | |
67 | ||
d83414e1 MAL |
68 | static void finish_switch_fiber(void *fake_stack_save) |
69 | { | |
70 | #ifdef CONFIG_ASAN | |
71 | const void *bottom_old; | |
72 | size_t size_old; | |
73 | ||
74 | __sanitizer_finish_switch_fiber(fake_stack_save, &bottom_old, &size_old); | |
75 | ||
76 | if (!leader.stack) { | |
77 | leader.stack = (void *)bottom_old; | |
78 | leader.stack_size = size_old; | |
79 | } | |
80 | #endif | |
81 | } | |
82 | ||
83 | static void start_switch_fiber(void **fake_stack_save, | |
84 | const void *bottom, size_t size) | |
85 | { | |
86 | #ifdef CONFIG_ASAN | |
87 | __sanitizer_start_switch_fiber(fake_stack_save, bottom, size); | |
88 | #endif | |
89 | } | |
90 | ||
00dccaf1 KW |
91 | static void coroutine_trampoline(int i0, int i1) |
92 | { | |
93 | union cc_arg arg; | |
94 | CoroutineUContext *self; | |
95 | Coroutine *co; | |
d83414e1 MAL |
96 | void *fake_stack_save = NULL; |
97 | ||
98 | finish_switch_fiber(NULL); | |
00dccaf1 KW |
99 | |
100 | arg.i[0] = i0; | |
101 | arg.i[1] = i1; | |
102 | self = arg.p; | |
103 | co = &self->base; | |
104 | ||
105 | /* Initialize longjmp environment and switch back the caller */ | |
6ab7e546 | 106 | if (!sigsetjmp(self->env, 0)) { |
d83414e1 MAL |
107 | start_switch_fiber(&fake_stack_save, |
108 | leader.stack, leader.stack_size); | |
6ab7e546 | 109 | siglongjmp(*(sigjmp_buf *)co->entry_arg, 1); |
00dccaf1 KW |
110 | } |
111 | ||
d83414e1 MAL |
112 | finish_switch_fiber(fake_stack_save); |
113 | ||
00dccaf1 KW |
114 | while (true) { |
115 | co->entry(co->entry_arg); | |
116 | qemu_coroutine_switch(co, co->caller, COROUTINE_TERMINATE); | |
117 | } | |
118 | } | |
119 | ||
40239784 | 120 | Coroutine *qemu_coroutine_new(void) |
00dccaf1 | 121 | { |
00dccaf1 KW |
122 | CoroutineUContext *co; |
123 | ucontext_t old_uc, uc; | |
6ab7e546 | 124 | sigjmp_buf old_env; |
32b74677 | 125 | union cc_arg arg = {0}; |
d83414e1 | 126 | void *fake_stack_save = NULL; |
00dccaf1 | 127 | |
6ab7e546 PM |
128 | /* The ucontext functions preserve signal masks which incurs a |
129 | * system call overhead. sigsetjmp(buf, 0)/siglongjmp() does not | |
130 | * preserve signal masks but only works on the current stack. | |
131 | * Since we need a way to create and switch to a new stack, use | |
132 | * the ucontext functions for that but sigsetjmp()/siglongjmp() for | |
133 | * everything else. | |
00dccaf1 KW |
134 | */ |
135 | ||
136 | if (getcontext(&uc) == -1) { | |
137 | abort(); | |
138 | } | |
139 | ||
7267c094 | 140 | co = g_malloc0(sizeof(*co)); |
ddba1591 PL |
141 | co->stack_size = COROUTINE_STACK_SIZE; |
142 | co->stack = qemu_alloc_stack(&co->stack_size); | |
00dccaf1 KW |
143 | co->base.entry_arg = &old_env; /* stash away our jmp_buf */ |
144 | ||
145 | uc.uc_link = &old_uc; | |
146 | uc.uc_stack.ss_sp = co->stack; | |
ddba1591 | 147 | uc.uc_stack.ss_size = co->stack_size; |
00dccaf1 KW |
148 | uc.uc_stack.ss_flags = 0; |
149 | ||
3f4349dc KW |
150 | #ifdef CONFIG_VALGRIND_H |
151 | co->valgrind_stack_id = | |
ddba1591 | 152 | VALGRIND_STACK_REGISTER(co->stack, co->stack + co->stack_size); |
3f4349dc KW |
153 | #endif |
154 | ||
00dccaf1 KW |
155 | arg.p = co; |
156 | ||
157 | makecontext(&uc, (void (*)(void))coroutine_trampoline, | |
158 | 2, arg.i[0], arg.i[1]); | |
159 | ||
6ab7e546 PM |
160 | /* swapcontext() in, siglongjmp() back out */ |
161 | if (!sigsetjmp(old_env, 0)) { | |
d83414e1 | 162 | start_switch_fiber(&fake_stack_save, co->stack, co->stack_size); |
00dccaf1 KW |
163 | swapcontext(&old_uc, &uc); |
164 | } | |
d83414e1 MAL |
165 | |
166 | finish_switch_fiber(fake_stack_save); | |
167 | ||
00dccaf1 KW |
168 | return &co->base; |
169 | } | |
170 | ||
3f4349dc | 171 | #ifdef CONFIG_VALGRIND_H |
0e39c4aa | 172 | #if defined(CONFIG_PRAGMA_DIAGNOSTIC_AVAILABLE) && !defined(__clang__) |
3f4349dc | 173 | /* Work around an unused variable in the valgrind.h macro... */ |
e6f53fd5 | 174 | #pragma GCC diagnostic push |
3f4349dc | 175 | #pragma GCC diagnostic ignored "-Wunused-but-set-variable" |
06d71fa1 | 176 | #endif |
3f4349dc KW |
177 | static inline void valgrind_stack_deregister(CoroutineUContext *co) |
178 | { | |
179 | VALGRIND_STACK_DEREGISTER(co->valgrind_stack_id); | |
180 | } | |
0e39c4aa | 181 | #if defined(CONFIG_PRAGMA_DIAGNOSTIC_AVAILABLE) && !defined(__clang__) |
e6f53fd5 | 182 | #pragma GCC diagnostic pop |
3f4349dc | 183 | #endif |
06d71fa1 | 184 | #endif |
3f4349dc | 185 | |
00dccaf1 KW |
186 | void qemu_coroutine_delete(Coroutine *co_) |
187 | { | |
00dccaf1 KW |
188 | CoroutineUContext *co = DO_UPCAST(CoroutineUContext, base, co_); |
189 | ||
3f4349dc KW |
190 | #ifdef CONFIG_VALGRIND_H |
191 | valgrind_stack_deregister(co); | |
192 | #endif | |
193 | ||
ddba1591 | 194 | qemu_free_stack(co->stack, co->stack_size); |
7267c094 | 195 | g_free(co); |
00dccaf1 KW |
196 | } |
197 | ||
d1d1b206 PB |
198 | /* This function is marked noinline to prevent GCC from inlining it |
199 | * into coroutine_trampoline(). If we allow it to do that then it | |
200 | * hoists the code to get the address of the TLS variable "current" | |
201 | * out of the while() loop. This is an invalid transformation because | |
202 | * the sigsetjmp() call may be called when running thread A but | |
203 | * return in thread B, and so we might be in a different thread | |
204 | * context each time round the loop. | |
205 | */ | |
206 | CoroutineAction __attribute__((noinline)) | |
207 | qemu_coroutine_switch(Coroutine *from_, Coroutine *to_, | |
208 | CoroutineAction action) | |
00dccaf1 KW |
209 | { |
210 | CoroutineUContext *from = DO_UPCAST(CoroutineUContext, base, from_); | |
211 | CoroutineUContext *to = DO_UPCAST(CoroutineUContext, base, to_); | |
00dccaf1 | 212 | int ret; |
d83414e1 | 213 | void *fake_stack_save = NULL; |
00dccaf1 | 214 | |
d1d1b206 | 215 | current = to_; |
00dccaf1 | 216 | |
6ab7e546 | 217 | ret = sigsetjmp(from->env, 0); |
00dccaf1 | 218 | if (ret == 0) { |
d83414e1 MAL |
219 | start_switch_fiber(action == COROUTINE_TERMINATE ? |
220 | NULL : &fake_stack_save, to->stack, to->stack_size); | |
6ab7e546 | 221 | siglongjmp(to->env, action); |
00dccaf1 | 222 | } |
d83414e1 MAL |
223 | |
224 | finish_switch_fiber(fake_stack_save); | |
225 | ||
00dccaf1 KW |
226 | return ret; |
227 | } | |
228 | ||
229 | Coroutine *qemu_coroutine_self(void) | |
230 | { | |
d1d1b206 PB |
231 | if (!current) { |
232 | current = &leader.base; | |
233 | } | |
234 | return current; | |
00dccaf1 KW |
235 | } |
236 | ||
237 | bool qemu_in_coroutine(void) | |
238 | { | |
d1d1b206 | 239 | return current && current->caller; |
00dccaf1 | 240 | } |