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