xref: /openbmc/qemu/util/coroutine-ucontext.c (revision 8e6fe6b8)
1 /*
2  * ucontext coroutine initialization code
3  *
4  * Copyright (C) 2006  Anthony Liguori <anthony@codemonkey.ws>
5  * Copyright (C) 2011  Kevin Wolf <kwolf@redhat.com>
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
25 #include "qemu/osdep.h"
26 #include <ucontext.h>
27 #include "qemu/coroutine_int.h"
28 
29 #ifdef CONFIG_VALGRIND_H
30 #include <valgrind/valgrind.h>
31 #endif
32 
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 
40 typedef struct {
41     Coroutine base;
42     void *stack;
43     size_t stack_size;
44     sigjmp_buf env;
45 
46 #ifdef CONFIG_VALGRIND_H
47     unsigned int valgrind_stack_id;
48 #endif
49 
50 } CoroutineUContext;
51 
52 /**
53  * Per-thread coroutine bookkeeping
54  */
55 static __thread CoroutineUContext leader;
56 static __thread Coroutine *current;
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 
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 
91 static void coroutine_trampoline(int i0, int i1)
92 {
93     union cc_arg arg;
94     CoroutineUContext *self;
95     Coroutine *co;
96     void *fake_stack_save = NULL;
97 
98     finish_switch_fiber(NULL);
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 */
106     if (!sigsetjmp(self->env, 0)) {
107         start_switch_fiber(&fake_stack_save,
108                            leader.stack, leader.stack_size);
109         siglongjmp(*(sigjmp_buf *)co->entry_arg, 1);
110     }
111 
112     finish_switch_fiber(fake_stack_save);
113 
114     while (true) {
115         co->entry(co->entry_arg);
116         qemu_coroutine_switch(co, co->caller, COROUTINE_TERMINATE);
117     }
118 }
119 
120 Coroutine *qemu_coroutine_new(void)
121 {
122     CoroutineUContext *co;
123     ucontext_t old_uc, uc;
124     sigjmp_buf old_env;
125     union cc_arg arg = {0};
126     void *fake_stack_save = NULL;
127 
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.
134      */
135 
136     if (getcontext(&uc) == -1) {
137         abort();
138     }
139 
140     co = g_malloc0(sizeof(*co));
141     co->stack_size = COROUTINE_STACK_SIZE;
142     co->stack = qemu_alloc_stack(&co->stack_size);
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;
147     uc.uc_stack.ss_size = co->stack_size;
148     uc.uc_stack.ss_flags = 0;
149 
150 #ifdef CONFIG_VALGRIND_H
151     co->valgrind_stack_id =
152         VALGRIND_STACK_REGISTER(co->stack, co->stack + co->stack_size);
153 #endif
154 
155     arg.p = co;
156 
157     makecontext(&uc, (void (*)(void))coroutine_trampoline,
158                 2, arg.i[0], arg.i[1]);
159 
160     /* swapcontext() in, siglongjmp() back out */
161     if (!sigsetjmp(old_env, 0)) {
162         start_switch_fiber(&fake_stack_save, co->stack, co->stack_size);
163         swapcontext(&old_uc, &uc);
164     }
165 
166     finish_switch_fiber(fake_stack_save);
167 
168     return &co->base;
169 }
170 
171 #ifdef CONFIG_VALGRIND_H
172 #if defined(CONFIG_PRAGMA_DIAGNOSTIC_AVAILABLE) && !defined(__clang__)
173 /* Work around an unused variable in the valgrind.h macro... */
174 #pragma GCC diagnostic push
175 #pragma GCC diagnostic ignored "-Wunused-but-set-variable"
176 #endif
177 static inline void valgrind_stack_deregister(CoroutineUContext *co)
178 {
179     VALGRIND_STACK_DEREGISTER(co->valgrind_stack_id);
180 }
181 #if defined(CONFIG_PRAGMA_DIAGNOSTIC_AVAILABLE) && !defined(__clang__)
182 #pragma GCC diagnostic pop
183 #endif
184 #endif
185 
186 void qemu_coroutine_delete(Coroutine *co_)
187 {
188     CoroutineUContext *co = DO_UPCAST(CoroutineUContext, base, co_);
189 
190 #ifdef CONFIG_VALGRIND_H
191     valgrind_stack_deregister(co);
192 #endif
193 
194     qemu_free_stack(co->stack, co->stack_size);
195     g_free(co);
196 }
197 
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)
209 {
210     CoroutineUContext *from = DO_UPCAST(CoroutineUContext, base, from_);
211     CoroutineUContext *to = DO_UPCAST(CoroutineUContext, base, to_);
212     int ret;
213     void *fake_stack_save = NULL;
214 
215     current = to_;
216 
217     ret = sigsetjmp(from->env, 0);
218     if (ret == 0) {
219         start_switch_fiber(action == COROUTINE_TERMINATE ?
220                            NULL : &fake_stack_save, to->stack, to->stack_size);
221         siglongjmp(to->env, action);
222     }
223 
224     finish_switch_fiber(fake_stack_save);
225 
226     return ret;
227 }
228 
229 Coroutine *qemu_coroutine_self(void)
230 {
231     if (!current) {
232         current = &leader.base;
233     }
234     return current;
235 }
236 
237 bool qemu_in_coroutine(void)
238 {
239     return current && current->caller;
240 }
241