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 <stdlib.h>
26#include <setjmp.h>
27#include <stdint.h>
28#include <pthread.h>
29#include <ucontext.h>
30#include "qemu-common.h"
31#include "block/coroutine_int.h"
32
33#ifdef CONFIG_VALGRIND_H
34#include <valgrind/valgrind.h>
35#endif
36
37typedef struct {
38 Coroutine base;
39 void *stack;
40 sigjmp_buf env;
41
42#ifdef CONFIG_VALGRIND_H
43 unsigned int valgrind_stack_id;
44#endif
45
46} CoroutineUContext;
47
48
49
50
51typedef struct {
52
53 Coroutine *current;
54
55
56 CoroutineUContext leader;
57} CoroutineThreadState;
58
59static pthread_key_t thread_state_key;
60
61
62
63
64
65
66union cc_arg {
67 void *p;
68 int i[2];
69};
70
71static CoroutineThreadState *coroutine_get_thread_state(void)
72{
73 CoroutineThreadState *s = pthread_getspecific(thread_state_key);
74
75 if (!s) {
76 s = g_malloc0(sizeof(*s));
77 s->current = &s->leader.base;
78 pthread_setspecific(thread_state_key, s);
79 }
80 return s;
81}
82
83static void qemu_coroutine_thread_cleanup(void *opaque)
84{
85 CoroutineThreadState *s = opaque;
86
87 g_free(s);
88}
89
90static void __attribute__((constructor)) coroutine_init(void)
91{
92 int ret;
93
94 ret = pthread_key_create(&thread_state_key, qemu_coroutine_thread_cleanup);
95 if (ret != 0) {
96 fprintf(stderr, "unable to create leader key: %s\n", strerror(errno));
97 abort();
98 }
99}
100
101static void coroutine_trampoline(int i0, int i1)
102{
103 union cc_arg arg;
104 CoroutineUContext *self;
105 Coroutine *co;
106
107 arg.i[0] = i0;
108 arg.i[1] = i1;
109 self = arg.p;
110 co = &self->base;
111
112
113 if (!sigsetjmp(self->env, 0)) {
114 siglongjmp(*(sigjmp_buf *)co->entry_arg, 1);
115 }
116
117 while (true) {
118 co->entry(co->entry_arg);
119 qemu_coroutine_switch(co, co->caller, COROUTINE_TERMINATE);
120 }
121}
122
123Coroutine *qemu_coroutine_new(void)
124{
125 const size_t stack_size = 1 << 20;
126 CoroutineUContext *co;
127 ucontext_t old_uc, uc;
128 sigjmp_buf old_env;
129 union cc_arg arg = {0};
130
131
132
133
134
135
136
137
138
139 if (getcontext(&uc) == -1) {
140 abort();
141 }
142
143 co = g_malloc0(sizeof(*co));
144 co->stack = g_malloc(stack_size);
145 co->base.entry_arg = &old_env;
146
147 uc.uc_link = &old_uc;
148 uc.uc_stack.ss_sp = co->stack;
149 uc.uc_stack.ss_size = stack_size;
150 uc.uc_stack.ss_flags = 0;
151
152#ifdef CONFIG_VALGRIND_H
153 co->valgrind_stack_id =
154 VALGRIND_STACK_REGISTER(co->stack, co->stack + stack_size);
155#endif
156
157 arg.p = co;
158
159 makecontext(&uc, (void (*)(void))coroutine_trampoline,
160 2, arg.i[0], arg.i[1]);
161
162
163 if (!sigsetjmp(old_env, 0)) {
164 swapcontext(&old_uc, &uc);
165 }
166 return &co->base;
167}
168
169#ifdef CONFIG_VALGRIND_H
170#ifdef CONFIG_PRAGMA_DIAGNOSTIC_AVAILABLE
171
172#pragma GCC diagnostic push
173#pragma GCC diagnostic ignored "-Wunused-but-set-variable"
174#endif
175static inline void valgrind_stack_deregister(CoroutineUContext *co)
176{
177 VALGRIND_STACK_DEREGISTER(co->valgrind_stack_id);
178}
179#ifdef CONFIG_PRAGMA_DIAGNOSTIC_AVAILABLE
180#pragma GCC diagnostic pop
181#endif
182#endif
183
184void qemu_coroutine_delete(Coroutine *co_)
185{
186 CoroutineUContext *co = DO_UPCAST(CoroutineUContext, base, co_);
187
188#ifdef CONFIG_VALGRIND_H
189 valgrind_stack_deregister(co);
190#endif
191
192 g_free(co->stack);
193 g_free(co);
194}
195
196CoroutineAction qemu_coroutine_switch(Coroutine *from_, Coroutine *to_,
197 CoroutineAction action)
198{
199 CoroutineUContext *from = DO_UPCAST(CoroutineUContext, base, from_);
200 CoroutineUContext *to = DO_UPCAST(CoroutineUContext, base, to_);
201 CoroutineThreadState *s = coroutine_get_thread_state();
202 int ret;
203
204 s->current = to_;
205
206 ret = sigsetjmp(from->env, 0);
207 if (ret == 0) {
208 siglongjmp(to->env, action);
209 }
210 return ret;
211}
212
213Coroutine *qemu_coroutine_self(void)
214{
215 CoroutineThreadState *s = coroutine_get_thread_state();
216
217 return s->current;
218}
219
220bool qemu_in_coroutine(void)
221{
222 CoroutineThreadState *s = pthread_getspecific(thread_state_key);
223
224 return s && s->current->caller;
225}
226