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