1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20#include "qemu/osdep.h"
21#include "qemu/main-loop.h"
22#include "exec/cpu-common.h"
23#include "hw/core/cpu.h"
24#include "sysemu/cpus.h"
25
26static QemuMutex qemu_cpu_list_lock;
27static QemuCond exclusive_cond;
28static QemuCond exclusive_resume;
29static QemuCond qemu_work_cond;
30
31
32
33
34static int pending_cpus;
35
36void qemu_init_cpu_list(void)
37{
38
39
40 pending_cpus = 0;
41
42 qemu_mutex_init(&qemu_cpu_list_lock);
43 qemu_cond_init(&exclusive_cond);
44 qemu_cond_init(&exclusive_resume);
45 qemu_cond_init(&qemu_work_cond);
46}
47
48void cpu_list_lock(void)
49{
50 qemu_mutex_lock(&qemu_cpu_list_lock);
51}
52
53void cpu_list_unlock(void)
54{
55 qemu_mutex_unlock(&qemu_cpu_list_lock);
56}
57
58static bool cpu_index_auto_assigned;
59
60static int cpu_get_free_index(void)
61{
62 CPUState *some_cpu;
63 int cpu_index = 0;
64
65 cpu_index_auto_assigned = true;
66 CPU_FOREACH(some_cpu) {
67 cpu_index++;
68 }
69 return cpu_index;
70}
71
72void cpu_list_add(CPUState *cpu)
73{
74 qemu_mutex_lock(&qemu_cpu_list_lock);
75 if (cpu->cpu_index == UNASSIGNED_CPU_INDEX) {
76 cpu->cpu_index = cpu_get_free_index();
77 assert(cpu->cpu_index != UNASSIGNED_CPU_INDEX);
78 } else {
79 assert(!cpu_index_auto_assigned);
80 }
81 QTAILQ_INSERT_TAIL_RCU(&cpus, cpu, node);
82 qemu_mutex_unlock(&qemu_cpu_list_lock);
83}
84
85void cpu_list_remove(CPUState *cpu)
86{
87 qemu_mutex_lock(&qemu_cpu_list_lock);
88 if (!QTAILQ_IN_USE(cpu, node)) {
89
90 qemu_mutex_unlock(&qemu_cpu_list_lock);
91 return;
92 }
93
94 assert(!(cpu_index_auto_assigned && cpu != QTAILQ_LAST(&cpus)));
95
96 QTAILQ_REMOVE_RCU(&cpus, cpu, node);
97 cpu->cpu_index = UNASSIGNED_CPU_INDEX;
98 qemu_mutex_unlock(&qemu_cpu_list_lock);
99}
100
101struct qemu_work_item {
102 struct qemu_work_item *next;
103 run_on_cpu_func func;
104 run_on_cpu_data data;
105 bool free, exclusive, done;
106};
107
108static void queue_work_on_cpu(CPUState *cpu, struct qemu_work_item *wi)
109{
110 qemu_mutex_lock(&cpu->work_mutex);
111 if (cpu->queued_work_first == NULL) {
112 cpu->queued_work_first = wi;
113 } else {
114 cpu->queued_work_last->next = wi;
115 }
116 cpu->queued_work_last = wi;
117 wi->next = NULL;
118 wi->done = false;
119 qemu_mutex_unlock(&cpu->work_mutex);
120
121 qemu_cpu_kick(cpu);
122}
123
124void do_run_on_cpu(CPUState *cpu, run_on_cpu_func func, run_on_cpu_data data,
125 QemuMutex *mutex)
126{
127 struct qemu_work_item wi;
128
129 if (qemu_cpu_is_self(cpu)) {
130 func(cpu, data);
131 return;
132 }
133
134 wi.func = func;
135 wi.data = data;
136 wi.done = false;
137 wi.free = false;
138 wi.exclusive = false;
139
140 queue_work_on_cpu(cpu, &wi);
141 while (!atomic_mb_read(&wi.done)) {
142 CPUState *self_cpu = current_cpu;
143
144 qemu_cond_wait(&qemu_work_cond, mutex);
145 current_cpu = self_cpu;
146 }
147}
148
149void async_run_on_cpu(CPUState *cpu, run_on_cpu_func func, run_on_cpu_data data)
150{
151 struct qemu_work_item *wi;
152
153 wi = g_malloc0(sizeof(struct qemu_work_item));
154 wi->func = func;
155 wi->data = data;
156 wi->free = true;
157
158 queue_work_on_cpu(cpu, wi);
159}
160
161
162
163static inline void exclusive_idle(void)
164{
165 while (pending_cpus) {
166 qemu_cond_wait(&exclusive_resume, &qemu_cpu_list_lock);
167 }
168}
169
170
171
172void start_exclusive(void)
173{
174 CPUState *other_cpu;
175 int running_cpus;
176
177 qemu_mutex_lock(&qemu_cpu_list_lock);
178 exclusive_idle();
179
180
181 atomic_set(&pending_cpus, 1);
182
183
184 smp_mb();
185 running_cpus = 0;
186 CPU_FOREACH(other_cpu) {
187 if (atomic_read(&other_cpu->running)) {
188 other_cpu->has_waiter = true;
189 running_cpus++;
190 qemu_cpu_kick(other_cpu);
191 }
192 }
193
194 atomic_set(&pending_cpus, running_cpus + 1);
195 while (pending_cpus > 1) {
196 qemu_cond_wait(&exclusive_cond, &qemu_cpu_list_lock);
197 }
198
199
200
201
202 qemu_mutex_unlock(&qemu_cpu_list_lock);
203
204 current_cpu->in_exclusive_context = true;
205}
206
207
208void end_exclusive(void)
209{
210 current_cpu->in_exclusive_context = false;
211
212 qemu_mutex_lock(&qemu_cpu_list_lock);
213 atomic_set(&pending_cpus, 0);
214 qemu_cond_broadcast(&exclusive_resume);
215 qemu_mutex_unlock(&qemu_cpu_list_lock);
216}
217
218
219void cpu_exec_start(CPUState *cpu)
220{
221 atomic_set(&cpu->running, true);
222
223
224 smp_mb();
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239 if (unlikely(atomic_read(&pending_cpus))) {
240 qemu_mutex_lock(&qemu_cpu_list_lock);
241 if (!cpu->has_waiter) {
242
243
244
245
246 atomic_set(&cpu->running, false);
247 exclusive_idle();
248
249 atomic_set(&cpu->running, true);
250 } else {
251
252
253
254 }
255 qemu_mutex_unlock(&qemu_cpu_list_lock);
256 }
257}
258
259
260void cpu_exec_end(CPUState *cpu)
261{
262 atomic_set(&cpu->running, false);
263
264
265 smp_mb();
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282 if (unlikely(atomic_read(&pending_cpus))) {
283 qemu_mutex_lock(&qemu_cpu_list_lock);
284 if (cpu->has_waiter) {
285 cpu->has_waiter = false;
286 atomic_set(&pending_cpus, pending_cpus - 1);
287 if (pending_cpus == 1) {
288 qemu_cond_signal(&exclusive_cond);
289 }
290 }
291 qemu_mutex_unlock(&qemu_cpu_list_lock);
292 }
293}
294
295void async_safe_run_on_cpu(CPUState *cpu, run_on_cpu_func func,
296 run_on_cpu_data data)
297{
298 struct qemu_work_item *wi;
299
300 wi = g_malloc0(sizeof(struct qemu_work_item));
301 wi->func = func;
302 wi->data = data;
303 wi->free = true;
304 wi->exclusive = true;
305
306 queue_work_on_cpu(cpu, wi);
307}
308
309void process_queued_cpu_work(CPUState *cpu)
310{
311 struct qemu_work_item *wi;
312
313 if (cpu->queued_work_first == NULL) {
314 return;
315 }
316
317 qemu_mutex_lock(&cpu->work_mutex);
318 while (cpu->queued_work_first != NULL) {
319 wi = cpu->queued_work_first;
320 cpu->queued_work_first = wi->next;
321 if (!cpu->queued_work_first) {
322 cpu->queued_work_last = NULL;
323 }
324 qemu_mutex_unlock(&cpu->work_mutex);
325 if (wi->exclusive) {
326
327
328
329
330
331
332 qemu_mutex_unlock_iothread();
333 start_exclusive();
334 wi->func(cpu, wi->data);
335 end_exclusive();
336 qemu_mutex_lock_iothread();
337 } else {
338 wi->func(cpu, wi->data);
339 }
340 qemu_mutex_lock(&cpu->work_mutex);
341 if (wi->free) {
342 g_free(wi);
343 } else {
344 atomic_mb_set(&wi->done, true);
345 }
346 }
347 qemu_mutex_unlock(&cpu->work_mutex);
348 qemu_cond_broadcast(&qemu_work_cond);
349}
350