]> git.proxmox.com Git - mirror_qemu.git/blame - cpus-common.c
qapi: Reformat recent doc comments to conform to current conventions
[mirror_qemu.git] / cpus-common.c
CommitLineData
267f685b
PB
1/*
2 * CPU thread main loop - common bits for user and system mode emulation
3 *
4 * Copyright (c) 2003-2005 Fabrice Bellard
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
d6ea4236 9 * version 2.1 of the License, or (at your option) any later version.
267f685b
PB
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
18 */
19
20#include "qemu/osdep.h"
53f5ed95 21#include "qemu/main-loop.h"
267f685b 22#include "exec/cpu-common.h"
2e5b09fd 23#include "hw/core/cpu.h"
267f685b 24#include "sysemu/cpus.h"
6e8a355d 25#include "qemu/lockable.h"
8a8dc267 26#include "trace/trace-root.h"
267f685b 27
370ed600 28QemuMutex qemu_cpu_list_lock;
ab129972
PB
29static QemuCond exclusive_cond;
30static QemuCond exclusive_resume;
d148d90e 31static QemuCond qemu_work_cond;
267f685b 32
c265e976
PB
33/* >= 1 if a thread is inside start_exclusive/end_exclusive. Written
34 * under qemu_cpu_list_lock, read with atomic operations.
35 */
ab129972
PB
36static int pending_cpus;
37
267f685b
PB
38void qemu_init_cpu_list(void)
39{
ab129972
PB
40 /* This is needed because qemu_init_cpu_list is also called by the
41 * child process in a fork. */
42 pending_cpus = 0;
43
267f685b 44 qemu_mutex_init(&qemu_cpu_list_lock);
ab129972
PB
45 qemu_cond_init(&exclusive_cond);
46 qemu_cond_init(&exclusive_resume);
d148d90e 47 qemu_cond_init(&qemu_work_cond);
267f685b
PB
48}
49
50void cpu_list_lock(void)
51{
52 qemu_mutex_lock(&qemu_cpu_list_lock);
53}
54
55void cpu_list_unlock(void)
56{
57 qemu_mutex_unlock(&qemu_cpu_list_lock);
58}
59
60static bool cpu_index_auto_assigned;
61
62static int cpu_get_free_index(void)
63{
64 CPUState *some_cpu;
716386e3 65 int max_cpu_index = 0;
267f685b
PB
66
67 cpu_index_auto_assigned = true;
68 CPU_FOREACH(some_cpu) {
716386e3
AB
69 if (some_cpu->cpu_index >= max_cpu_index) {
70 max_cpu_index = some_cpu->cpu_index + 1;
71 }
267f685b 72 }
716386e3 73 return max_cpu_index;
267f685b
PB
74}
75
421a75e2 76CPUTailQ cpus = QTAILQ_HEAD_INITIALIZER(cpus);
ab1a161f
HH
77static unsigned int cpu_list_generation_id;
78
79unsigned int cpu_list_generation_id_get(void)
80{
81 return cpu_list_generation_id;
82}
421a75e2 83
267f685b
PB
84void cpu_list_add(CPUState *cpu)
85{
6e8a355d 86 QEMU_LOCK_GUARD(&qemu_cpu_list_lock);
267f685b
PB
87 if (cpu->cpu_index == UNASSIGNED_CPU_INDEX) {
88 cpu->cpu_index = cpu_get_free_index();
89 assert(cpu->cpu_index != UNASSIGNED_CPU_INDEX);
90 } else {
91 assert(!cpu_index_auto_assigned);
92 }
068a5ea0 93 QTAILQ_INSERT_TAIL_RCU(&cpus, cpu, node);
ab1a161f 94 cpu_list_generation_id++;
267f685b
PB
95}
96
97void cpu_list_remove(CPUState *cpu)
98{
6e8a355d 99 QEMU_LOCK_GUARD(&qemu_cpu_list_lock);
267f685b
PB
100 if (!QTAILQ_IN_USE(cpu, node)) {
101 /* there is nothing to undo since cpu_exec_init() hasn't been called */
267f685b
PB
102 return;
103 }
104
068a5ea0 105 QTAILQ_REMOVE_RCU(&cpus, cpu, node);
267f685b 106 cpu->cpu_index = UNASSIGNED_CPU_INDEX;
ab1a161f 107 cpu_list_generation_id++;
267f685b 108}
d148d90e 109
421a75e2
PMD
110CPUState *qemu_get_cpu(int index)
111{
112 CPUState *cpu;
113
114 CPU_FOREACH(cpu) {
115 if (cpu->cpu_index == index) {
116 return cpu;
117 }
118 }
119
120 return NULL;
121}
122
123/* current CPU in the current thread. It is only valid inside cpu_exec() */
124__thread CPUState *current_cpu;
125
d148d90e 126struct qemu_work_item {
0c0fcc20 127 QSIMPLEQ_ENTRY(qemu_work_item) node;
d148d90e 128 run_on_cpu_func func;
14e6fe12 129 run_on_cpu_data data;
53f5ed95 130 bool free, exclusive, done;
d148d90e
SF
131};
132
133static void queue_work_on_cpu(CPUState *cpu, struct qemu_work_item *wi)
134{
135 qemu_mutex_lock(&cpu->work_mutex);
0c0fcc20 136 QSIMPLEQ_INSERT_TAIL(&cpu->work_list, wi, node);
d148d90e
SF
137 wi->done = false;
138 qemu_mutex_unlock(&cpu->work_mutex);
139
140 qemu_cpu_kick(cpu);
141}
142
14e6fe12 143void do_run_on_cpu(CPUState *cpu, run_on_cpu_func func, run_on_cpu_data data,
d148d90e
SF
144 QemuMutex *mutex)
145{
146 struct qemu_work_item wi;
147
148 if (qemu_cpu_is_self(cpu)) {
149 func(cpu, data);
150 return;
151 }
152
153 wi.func = func;
154 wi.data = data;
0e55539c 155 wi.done = false;
d148d90e 156 wi.free = false;
53f5ed95 157 wi.exclusive = false;
d148d90e
SF
158
159 queue_work_on_cpu(cpu, &wi);
42abcc58 160 while (!qatomic_load_acquire(&wi.done)) {
d148d90e
SF
161 CPUState *self_cpu = current_cpu;
162
163 qemu_cond_wait(&qemu_work_cond, mutex);
164 current_cpu = self_cpu;
165 }
166}
167
14e6fe12 168void async_run_on_cpu(CPUState *cpu, run_on_cpu_func func, run_on_cpu_data data)
d148d90e
SF
169{
170 struct qemu_work_item *wi;
171
b21e2380 172 wi = g_new0(struct qemu_work_item, 1);
d148d90e
SF
173 wi->func = func;
174 wi->data = data;
175 wi->free = true;
176
177 queue_work_on_cpu(cpu, wi);
178}
179
ab129972
PB
180/* Wait for pending exclusive operations to complete. The CPU list lock
181 must be held. */
182static inline void exclusive_idle(void)
183{
184 while (pending_cpus) {
185 qemu_cond_wait(&exclusive_resume, &qemu_cpu_list_lock);
186 }
187}
188
189/* Start an exclusive operation.
758e1b2b 190 Must only be called from outside cpu_exec. */
ab129972
PB
191void start_exclusive(void)
192{
193 CPUState *other_cpu;
c265e976 194 int running_cpus;
ab129972 195
df8a6880
IL
196 if (current_cpu->exclusive_context_count) {
197 current_cpu->exclusive_context_count++;
198 return;
199 }
200
ab129972
PB
201 qemu_mutex_lock(&qemu_cpu_list_lock);
202 exclusive_idle();
203
204 /* Make all other cpus stop executing. */
d73415a3 205 qatomic_set(&pending_cpus, 1);
c265e976
PB
206
207 /* Write pending_cpus before reading other_cpu->running. */
208 smp_mb();
209 running_cpus = 0;
ab129972 210 CPU_FOREACH(other_cpu) {
d73415a3 211 if (qatomic_read(&other_cpu->running)) {
c265e976
PB
212 other_cpu->has_waiter = true;
213 running_cpus++;
ab129972
PB
214 qemu_cpu_kick(other_cpu);
215 }
216 }
c265e976 217
d73415a3 218 qatomic_set(&pending_cpus, running_cpus + 1);
ab129972
PB
219 while (pending_cpus > 1) {
220 qemu_cond_wait(&exclusive_cond, &qemu_cpu_list_lock);
221 }
758e1b2b
PB
222
223 /* Can release mutex, no one will enter another exclusive
224 * section until end_exclusive resets pending_cpus to 0.
225 */
226 qemu_mutex_unlock(&qemu_cpu_list_lock);
cfbc3c60 227
df8a6880 228 current_cpu->exclusive_context_count = 1;
ab129972
PB
229}
230
758e1b2b 231/* Finish an exclusive operation. */
ab129972
PB
232void end_exclusive(void)
233{
df8a6880
IL
234 current_cpu->exclusive_context_count--;
235 if (current_cpu->exclusive_context_count) {
236 return;
237 }
cfbc3c60 238
758e1b2b 239 qemu_mutex_lock(&qemu_cpu_list_lock);
d73415a3 240 qatomic_set(&pending_cpus, 0);
ab129972
PB
241 qemu_cond_broadcast(&exclusive_resume);
242 qemu_mutex_unlock(&qemu_cpu_list_lock);
243}
244
245/* Wait for exclusive ops to finish, and begin cpu execution. */
246void cpu_exec_start(CPUState *cpu)
247{
d73415a3 248 qatomic_set(&cpu->running, true);
c265e976
PB
249
250 /* Write cpu->running before reading pending_cpus. */
251 smp_mb();
252
253 /* 1. start_exclusive saw cpu->running == true and pending_cpus >= 1.
254 * After taking the lock we'll see cpu->has_waiter == true and run---not
255 * for long because start_exclusive kicked us. cpu_exec_end will
256 * decrement pending_cpus and signal the waiter.
257 *
258 * 2. start_exclusive saw cpu->running == false but pending_cpus >= 1.
259 * This includes the case when an exclusive item is running now.
260 * Then we'll see cpu->has_waiter == false and wait for the item to
261 * complete.
262 *
263 * 3. pending_cpus == 0. Then start_exclusive is definitely going to
264 * see cpu->running == true, and it will kick the CPU.
265 */
d73415a3 266 if (unlikely(qatomic_read(&pending_cpus))) {
6e8a355d 267 QEMU_LOCK_GUARD(&qemu_cpu_list_lock);
c265e976
PB
268 if (!cpu->has_waiter) {
269 /* Not counted in pending_cpus, let the exclusive item
270 * run. Since we have the lock, just set cpu->running to true
271 * while holding it; no need to check pending_cpus again.
272 */
d73415a3 273 qatomic_set(&cpu->running, false);
c265e976
PB
274 exclusive_idle();
275 /* Now pending_cpus is zero. */
d73415a3 276 qatomic_set(&cpu->running, true);
c265e976
PB
277 } else {
278 /* Counted in pending_cpus, go ahead and release the
279 * waiter at cpu_exec_end.
280 */
281 }
c265e976 282 }
ab129972
PB
283}
284
285/* Mark cpu as not executing, and release pending exclusive ops. */
286void cpu_exec_end(CPUState *cpu)
287{
d73415a3 288 qatomic_set(&cpu->running, false);
c265e976
PB
289
290 /* Write cpu->running before reading pending_cpus. */
291 smp_mb();
292
293 /* 1. start_exclusive saw cpu->running == true. Then it will increment
294 * pending_cpus and wait for exclusive_cond. After taking the lock
295 * we'll see cpu->has_waiter == true.
296 *
297 * 2. start_exclusive saw cpu->running == false but here pending_cpus >= 1.
298 * This includes the case when an exclusive item started after setting
299 * cpu->running to false and before we read pending_cpus. Then we'll see
300 * cpu->has_waiter == false and not touch pending_cpus. The next call to
301 * cpu_exec_start will run exclusive_idle if still necessary, thus waiting
302 * for the item to complete.
303 *
304 * 3. pending_cpus == 0. Then start_exclusive is definitely going to
305 * see cpu->running == false, and it can ignore this CPU until the
306 * next cpu_exec_start.
307 */
d73415a3 308 if (unlikely(qatomic_read(&pending_cpus))) {
6e8a355d 309 QEMU_LOCK_GUARD(&qemu_cpu_list_lock);
c265e976
PB
310 if (cpu->has_waiter) {
311 cpu->has_waiter = false;
d73415a3 312 qatomic_set(&pending_cpus, pending_cpus - 1);
c265e976
PB
313 if (pending_cpus == 1) {
314 qemu_cond_signal(&exclusive_cond);
315 }
ab129972
PB
316 }
317 }
ab129972
PB
318}
319
14e6fe12
PB
320void async_safe_run_on_cpu(CPUState *cpu, run_on_cpu_func func,
321 run_on_cpu_data data)
53f5ed95
PB
322{
323 struct qemu_work_item *wi;
324
b21e2380 325 wi = g_new0(struct qemu_work_item, 1);
53f5ed95
PB
326 wi->func = func;
327 wi->data = data;
328 wi->free = true;
329 wi->exclusive = true;
330
331 queue_work_on_cpu(cpu, wi);
332}
333
d148d90e
SF
334void process_queued_cpu_work(CPUState *cpu)
335{
336 struct qemu_work_item *wi;
337
0c0fcc20
EC
338 qemu_mutex_lock(&cpu->work_mutex);
339 if (QSIMPLEQ_EMPTY(&cpu->work_list)) {
340 qemu_mutex_unlock(&cpu->work_mutex);
d148d90e
SF
341 return;
342 }
0c0fcc20
EC
343 while (!QSIMPLEQ_EMPTY(&cpu->work_list)) {
344 wi = QSIMPLEQ_FIRST(&cpu->work_list);
345 QSIMPLEQ_REMOVE_HEAD(&cpu->work_list, node);
d148d90e 346 qemu_mutex_unlock(&cpu->work_mutex);
53f5ed95
PB
347 if (wi->exclusive) {
348 /* Running work items outside the BQL avoids the following deadlock:
349 * 1) start_exclusive() is called with the BQL taken while another
350 * CPU is running; 2) cpu_exec in the other CPU tries to takes the
351 * BQL, so it goes to sleep; start_exclusive() is sleeping too, so
352 * neither CPU can proceed.
353 */
354 qemu_mutex_unlock_iothread();
355 start_exclusive();
356 wi->func(cpu, wi->data);
357 end_exclusive();
358 qemu_mutex_lock_iothread();
359 } else {
360 wi->func(cpu, wi->data);
361 }
d148d90e
SF
362 qemu_mutex_lock(&cpu->work_mutex);
363 if (wi->free) {
364 g_free(wi);
365 } else {
42abcc58 366 qatomic_store_release(&wi->done, true);
d148d90e
SF
367 }
368 }
369 qemu_mutex_unlock(&cpu->work_mutex);
370 qemu_cond_broadcast(&qemu_work_cond);
371}
8a8dc267
PMD
372
373/* Add a breakpoint. */
374int cpu_breakpoint_insert(CPUState *cpu, vaddr pc, int flags,
375 CPUBreakpoint **breakpoint)
376{
377 CPUClass *cc = CPU_GET_CLASS(cpu);
378 CPUBreakpoint *bp;
379
380 if (cc->gdb_adjust_breakpoint) {
381 pc = cc->gdb_adjust_breakpoint(cpu, pc);
382 }
383
384 bp = g_malloc(sizeof(*bp));
385
386 bp->pc = pc;
387 bp->flags = flags;
388
389 /* keep all GDB-injected breakpoints in front */
390 if (flags & BP_GDB) {
391 QTAILQ_INSERT_HEAD(&cpu->breakpoints, bp, entry);
392 } else {
393 QTAILQ_INSERT_TAIL(&cpu->breakpoints, bp, entry);
394 }
395
396 if (breakpoint) {
397 *breakpoint = bp;
398 }
399
400 trace_breakpoint_insert(cpu->cpu_index, pc, flags);
401 return 0;
402}
403
404/* Remove a specific breakpoint. */
405int cpu_breakpoint_remove(CPUState *cpu, vaddr pc, int flags)
406{
407 CPUClass *cc = CPU_GET_CLASS(cpu);
408 CPUBreakpoint *bp;
409
410 if (cc->gdb_adjust_breakpoint) {
411 pc = cc->gdb_adjust_breakpoint(cpu, pc);
412 }
413
414 QTAILQ_FOREACH(bp, &cpu->breakpoints, entry) {
415 if (bp->pc == pc && bp->flags == flags) {
416 cpu_breakpoint_remove_by_ref(cpu, bp);
417 return 0;
418 }
419 }
420 return -ENOENT;
421}
422
423/* Remove a specific breakpoint by reference. */
424void cpu_breakpoint_remove_by_ref(CPUState *cpu, CPUBreakpoint *bp)
425{
426 QTAILQ_REMOVE(&cpu->breakpoints, bp, entry);
427
428 trace_breakpoint_remove(cpu->cpu_index, bp->pc, bp->flags);
429 g_free(bp);
430}
431
432/* Remove all matching breakpoints. */
433void cpu_breakpoint_remove_all(CPUState *cpu, int mask)
434{
435 CPUBreakpoint *bp, *next;
436
437 QTAILQ_FOREACH_SAFE(bp, &cpu->breakpoints, entry, next) {
438 if (bp->flags & mask) {
439 cpu_breakpoint_remove_by_ref(cpu, bp);
440 }
441 }
442}