1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
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
40typedef 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
54
55static __thread CoroutineUContext leader;
56static __thread Coroutine *current;
57
58
59
60
61
62
63union cc_arg {
64 void *p;
65 int i[2];
66};
67
68static 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
83static 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
91static 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
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
120Coroutine *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
129
130
131
132
133
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;
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
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
174#pragma GCC diagnostic push
175#pragma GCC diagnostic ignored "-Wunused-but-set-variable"
176#endif
177static 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
186void 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
199
200
201
202
203
204
205
206CoroutineAction __attribute__((noinline))
207qemu_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
229Coroutine *qemu_coroutine_self(void)
230{
231 if (!current) {
232 current = &leader.base;
233 }
234 return current;
235}
236
237bool qemu_in_coroutine(void)
238{
239 return current && current->caller;
240}
241