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 "qemu-coroutine-int.h"
32
33enum {
34
35 POOL_MAX_SIZE = 64,
36};
37
38
39static QSLIST_HEAD(, Coroutine) pool = QSLIST_HEAD_INITIALIZER(pool);
40static unsigned int pool_size;
41
42typedef struct {
43 Coroutine base;
44 void *stack;
45 jmp_buf env;
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__((destructor)) coroutine_cleanup(void)
91{
92 Coroutine *co;
93 Coroutine *tmp;
94
95 QSLIST_FOREACH_SAFE(co, &pool, pool_next, tmp) {
96 g_free(DO_UPCAST(CoroutineUContext, base, co)->stack);
97 g_free(co);
98 }
99}
100
101static void __attribute__((constructor)) coroutine_init(void)
102{
103 int ret;
104
105 ret = pthread_key_create(&thread_state_key, qemu_coroutine_thread_cleanup);
106 if (ret != 0) {
107 fprintf(stderr, "unable to create leader key: %s\n", strerror(errno));
108 abort();
109 }
110}
111
112static void coroutine_trampoline(int i0, int i1)
113{
114 union cc_arg arg;
115 CoroutineUContext *self;
116 Coroutine *co;
117
118 arg.i[0] = i0;
119 arg.i[1] = i1;
120 self = arg.p;
121 co = &self->base;
122
123
124 if (!setjmp(self->env)) {
125 longjmp(*(jmp_buf *)co->entry_arg, 1);
126 }
127
128 while (true) {
129 co->entry(co->entry_arg);
130 qemu_coroutine_switch(co, co->caller, COROUTINE_TERMINATE);
131 }
132}
133
134static Coroutine *coroutine_new(void)
135{
136 const size_t stack_size = 1 << 20;
137 CoroutineUContext *co;
138 ucontext_t old_uc, uc;
139 jmp_buf old_env;
140 union cc_arg arg = {0};
141
142
143
144
145
146
147
148
149 if (getcontext(&uc) == -1) {
150 abort();
151 }
152
153 co = g_malloc0(sizeof(*co));
154 co->stack = g_malloc(stack_size);
155 co->base.entry_arg = &old_env;
156
157 uc.uc_link = &old_uc;
158 uc.uc_stack.ss_sp = co->stack;
159 uc.uc_stack.ss_size = stack_size;
160 uc.uc_stack.ss_flags = 0;
161
162 arg.p = co;
163
164 makecontext(&uc, (void (*)(void))coroutine_trampoline,
165 2, arg.i[0], arg.i[1]);
166
167
168 if (!setjmp(old_env)) {
169 swapcontext(&old_uc, &uc);
170 }
171 return &co->base;
172}
173
174Coroutine *qemu_coroutine_new(void)
175{
176 Coroutine *co;
177
178 co = QSLIST_FIRST(&pool);
179 if (co) {
180 QSLIST_REMOVE_HEAD(&pool, pool_next);
181 pool_size--;
182 } else {
183 co = coroutine_new();
184 }
185 return co;
186}
187
188void qemu_coroutine_delete(Coroutine *co_)
189{
190 CoroutineUContext *co = DO_UPCAST(CoroutineUContext, base, co_);
191
192 if (pool_size < POOL_MAX_SIZE) {
193 QSLIST_INSERT_HEAD(&pool, &co->base, pool_next);
194 co->base.caller = NULL;
195 pool_size++;
196 return;
197 }
198
199 g_free(co->stack);
200 g_free(co);
201}
202
203CoroutineAction qemu_coroutine_switch(Coroutine *from_, Coroutine *to_,
204 CoroutineAction action)
205{
206 CoroutineUContext *from = DO_UPCAST(CoroutineUContext, base, from_);
207 CoroutineUContext *to = DO_UPCAST(CoroutineUContext, base, to_);
208 CoroutineThreadState *s = coroutine_get_thread_state();
209 int ret;
210
211 s->current = to_;
212
213 ret = setjmp(from->env);
214 if (ret == 0) {
215 longjmp(to->env, action);
216 }
217 return ret;
218}
219
220Coroutine *qemu_coroutine_self(void)
221{
222 CoroutineThreadState *s = coroutine_get_thread_state();
223
224 return s->current;
225}
226
227bool qemu_in_coroutine(void)
228{
229 CoroutineThreadState *s = pthread_getspecific(thread_state_key);
230
231 return s && s->current->caller;
232}
233