]> git.proxmox.com Git - mirror_qemu.git/blame - cpus.c
CODING_STYLE: indent example code as all others
[mirror_qemu.git] / cpus.c
CommitLineData
296af7c9
BS
1/*
2 * QEMU System Emulator
3 *
4 * Copyright (c) 2003-2008 Fabrice Bellard
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
23 */
24
7b31bbc2 25#include "qemu/osdep.h"
8d4e9146 26#include "qemu/config-file.h"
33c11879 27#include "cpu.h"
83c9089e 28#include "monitor/monitor.h"
e688df6b 29#include "qapi/error.h"
112ed241 30#include "qapi/qapi-commands-misc.h"
9af23989 31#include "qapi/qapi-events-run-state.h"
a4e15de9 32#include "qapi/qmp/qerror.h"
d49b6836 33#include "qemu/error-report.h"
76c86615 34#include "qemu/qemu-print.h"
9c17d615 35#include "sysemu/sysemu.h"
da31d594 36#include "sysemu/block-backend.h"
022c62cb 37#include "exec/gdbstub.h"
9c17d615 38#include "sysemu/dma.h"
b3946626 39#include "sysemu/hw_accel.h"
9c17d615 40#include "sysemu/kvm.h"
b0cb0a66 41#include "sysemu/hax.h"
c97d6d2c 42#include "sysemu/hvf.h"
19306806 43#include "sysemu/whpx.h"
63c91552 44#include "exec/exec-all.h"
296af7c9 45
1de7afc9 46#include "qemu/thread.h"
9c17d615
PB
47#include "sysemu/cpus.h"
48#include "sysemu/qtest.h"
1de7afc9 49#include "qemu/main-loop.h"
922a01a0 50#include "qemu/option.h"
1de7afc9 51#include "qemu/bitmap.h"
cb365646 52#include "qemu/seqlock.h"
8d4e9146 53#include "tcg.h"
9cb805fd 54#include "hw/nmi.h"
8b427044 55#include "sysemu/replay.h"
afed5a5a 56#include "hw/boards.h"
0ff0fc19 57
6d9cb73c
JK
58#ifdef CONFIG_LINUX
59
60#include <sys/prctl.h>
61
c0532a76
MT
62#ifndef PR_MCE_KILL
63#define PR_MCE_KILL 33
64#endif
65
6d9cb73c
JK
66#ifndef PR_MCE_KILL_SET
67#define PR_MCE_KILL_SET 1
68#endif
69
70#ifndef PR_MCE_KILL_EARLY
71#define PR_MCE_KILL_EARLY 1
72#endif
73
74#endif /* CONFIG_LINUX */
75
27498bef
ST
76int64_t max_delay;
77int64_t max_advance;
296af7c9 78
2adcc85d
JH
79/* vcpu throttling controls */
80static QEMUTimer *throttle_timer;
81static unsigned int throttle_percentage;
82
83#define CPU_THROTTLE_PCT_MIN 1
84#define CPU_THROTTLE_PCT_MAX 99
85#define CPU_THROTTLE_TIMESLICE_NS 10000000
86
321bc0b2
TC
87bool cpu_is_stopped(CPUState *cpu)
88{
89 return cpu->stopped || !runstate_is_running();
90}
91
a98ae1d8 92static bool cpu_thread_is_idle(CPUState *cpu)
ac873f1e 93{
c64ca814 94 if (cpu->stop || cpu->queued_work_first) {
ac873f1e
PM
95 return false;
96 }
321bc0b2 97 if (cpu_is_stopped(cpu)) {
ac873f1e
PM
98 return true;
99 }
8c2e1b00 100 if (!cpu->halted || cpu_has_work(cpu) ||
215e79c0 101 kvm_halt_in_kernel()) {
ac873f1e
PM
102 return false;
103 }
104 return true;
105}
106
107static bool all_cpu_threads_idle(void)
108{
182735ef 109 CPUState *cpu;
ac873f1e 110
bdc44640 111 CPU_FOREACH(cpu) {
182735ef 112 if (!cpu_thread_is_idle(cpu)) {
ac873f1e
PM
113 return false;
114 }
115 }
116 return true;
117}
118
946fb27c
PB
119/***********************************************************/
120/* guest cycle counter */
121
a3270e19
PB
122/* Protected by TimersState seqlock */
123
5045e9d9 124static bool icount_sleep = true;
946fb27c
PB
125/* Arbitrarily pick 1MIPS as the minimum allowable speed. */
126#define MAX_ICOUNT_SHIFT 10
a3270e19 127
946fb27c 128typedef struct TimersState {
cb365646 129 /* Protected by BQL. */
946fb27c
PB
130 int64_t cpu_ticks_prev;
131 int64_t cpu_ticks_offset;
cb365646 132
94377115
PB
133 /* Protect fields that can be respectively read outside the
134 * BQL, and written from multiple threads.
cb365646
LPF
135 */
136 QemuSeqLock vm_clock_seqlock;
94377115
PB
137 QemuSpin vm_clock_lock;
138
139 int16_t cpu_ticks_enabled;
c96778bb 140
c1ff073c 141 /* Conversion factor from emulated instructions to virtual clock ticks. */
94377115
PB
142 int16_t icount_time_shift;
143
c96778bb
FK
144 /* Compensate for varying guest execution speed. */
145 int64_t qemu_icount_bias;
94377115
PB
146
147 int64_t vm_clock_warp_start;
148 int64_t cpu_clock_offset;
149
c96778bb
FK
150 /* Only written by TCG thread */
151 int64_t qemu_icount;
94377115 152
b39e3f34 153 /* for adjusting icount */
b39e3f34
PD
154 QEMUTimer *icount_rt_timer;
155 QEMUTimer *icount_vm_timer;
156 QEMUTimer *icount_warp_timer;
946fb27c
PB
157} TimersState;
158
d9cd4007 159static TimersState timers_state;
8d4e9146
FK
160bool mttcg_enabled;
161
162/*
163 * We default to false if we know other options have been enabled
164 * which are currently incompatible with MTTCG. Otherwise when each
165 * guest (target) has been updated to support:
166 * - atomic instructions
167 * - memory ordering primitives (barriers)
168 * they can set the appropriate CONFIG flags in ${target}-softmmu.mak
169 *
170 * Once a guest architecture has been converted to the new primitives
171 * there are two remaining limitations to check.
172 *
173 * - The guest can't be oversized (e.g. 64 bit guest on 32 bit host)
174 * - The host must have a stronger memory order than the guest
175 *
176 * It may be possible in future to support strong guests on weak hosts
177 * but that will require tagging all load/stores in a guest with their
178 * implicit memory order requirements which would likely slow things
179 * down a lot.
180 */
181
182static bool check_tcg_memory_orders_compatible(void)
183{
184#if defined(TCG_GUEST_DEFAULT_MO) && defined(TCG_TARGET_DEFAULT_MO)
185 return (TCG_GUEST_DEFAULT_MO & ~TCG_TARGET_DEFAULT_MO) == 0;
186#else
187 return false;
188#endif
189}
190
191static bool default_mttcg_enabled(void)
192{
83fd9629 193 if (use_icount || TCG_OVERSIZED_GUEST) {
8d4e9146
FK
194 return false;
195 } else {
196#ifdef TARGET_SUPPORTS_MTTCG
197 return check_tcg_memory_orders_compatible();
198#else
199 return false;
200#endif
201 }
202}
203
204void qemu_tcg_configure(QemuOpts *opts, Error **errp)
205{
206 const char *t = qemu_opt_get(opts, "thread");
207 if (t) {
208 if (strcmp(t, "multi") == 0) {
209 if (TCG_OVERSIZED_GUEST) {
210 error_setg(errp, "No MTTCG when guest word size > hosts");
83fd9629
AB
211 } else if (use_icount) {
212 error_setg(errp, "No MTTCG when icount is enabled");
8d4e9146 213 } else {
86953503 214#ifndef TARGET_SUPPORTS_MTTCG
0765691e
MA
215 warn_report("Guest not yet converted to MTTCG - "
216 "you may get unexpected results");
c34c7620 217#endif
8d4e9146 218 if (!check_tcg_memory_orders_compatible()) {
0765691e
MA
219 warn_report("Guest expects a stronger memory ordering "
220 "than the host provides");
8cfef892 221 error_printf("This may cause strange/hard to debug errors\n");
8d4e9146
FK
222 }
223 mttcg_enabled = true;
224 }
225 } else if (strcmp(t, "single") == 0) {
226 mttcg_enabled = false;
227 } else {
228 error_setg(errp, "Invalid 'thread' setting %s", t);
229 }
230 } else {
231 mttcg_enabled = default_mttcg_enabled();
232 }
233}
946fb27c 234
e4cd9657
AB
235/* The current number of executed instructions is based on what we
236 * originally budgeted minus the current state of the decrementing
237 * icount counters in extra/u16.low.
238 */
239static int64_t cpu_get_icount_executed(CPUState *cpu)
240{
241 return cpu->icount_budget - (cpu->icount_decr.u16.low + cpu->icount_extra);
242}
243
512d3c80
AB
244/*
245 * Update the global shared timer_state.qemu_icount to take into
246 * account executed instructions. This is done by the TCG vCPU
247 * thread so the main-loop can see time has moved forward.
248 */
9b4e6f49 249static void cpu_update_icount_locked(CPUState *cpu)
512d3c80
AB
250{
251 int64_t executed = cpu_get_icount_executed(cpu);
252 cpu->icount_budget -= executed;
253
38adcb6e
EC
254 atomic_set_i64(&timers_state.qemu_icount,
255 timers_state.qemu_icount + executed);
9b4e6f49
PB
256}
257
258/*
259 * Update the global shared timer_state.qemu_icount to take into
260 * account executed instructions. This is done by the TCG vCPU
261 * thread so the main-loop can see time has moved forward.
262 */
263void cpu_update_icount(CPUState *cpu)
264{
265 seqlock_write_lock(&timers_state.vm_clock_seqlock,
266 &timers_state.vm_clock_lock);
267 cpu_update_icount_locked(cpu);
94377115
PB
268 seqlock_write_unlock(&timers_state.vm_clock_seqlock,
269 &timers_state.vm_clock_lock);
512d3c80
AB
270}
271
c1ff073c 272static int64_t cpu_get_icount_raw_locked(void)
946fb27c 273{
4917cf44 274 CPUState *cpu = current_cpu;
946fb27c 275
243c5f77 276 if (cpu && cpu->running) {
414b15c9 277 if (!cpu->can_do_io) {
493d89bf 278 error_report("Bad icount read");
2a62914b 279 exit(1);
946fb27c 280 }
e4cd9657 281 /* Take into account what has run */
9b4e6f49 282 cpu_update_icount_locked(cpu);
946fb27c 283 }
38adcb6e
EC
284 /* The read is protected by the seqlock, but needs atomic64 to avoid UB */
285 return atomic_read_i64(&timers_state.qemu_icount);
2a62914b
PD
286}
287
2a62914b
PD
288static int64_t cpu_get_icount_locked(void)
289{
c1ff073c 290 int64_t icount = cpu_get_icount_raw_locked();
c97595d1
EC
291 return atomic_read_i64(&timers_state.qemu_icount_bias) +
292 cpu_icount_to_ns(icount);
c1ff073c
PB
293}
294
295int64_t cpu_get_icount_raw(void)
296{
297 int64_t icount;
298 unsigned start;
299
300 do {
301 start = seqlock_read_begin(&timers_state.vm_clock_seqlock);
302 icount = cpu_get_icount_raw_locked();
303 } while (seqlock_read_retry(&timers_state.vm_clock_seqlock, start));
304
305 return icount;
946fb27c
PB
306}
307
c1ff073c 308/* Return the virtual CPU time, based on the instruction counter. */
17a15f1b
PB
309int64_t cpu_get_icount(void)
310{
311 int64_t icount;
312 unsigned start;
313
314 do {
315 start = seqlock_read_begin(&timers_state.vm_clock_seqlock);
316 icount = cpu_get_icount_locked();
317 } while (seqlock_read_retry(&timers_state.vm_clock_seqlock, start));
318
319 return icount;
320}
321
3f031313
FK
322int64_t cpu_icount_to_ns(int64_t icount)
323{
c1ff073c 324 return icount << atomic_read(&timers_state.icount_time_shift);
3f031313
FK
325}
326
f2a4ad6d
PB
327static int64_t cpu_get_ticks_locked(void)
328{
329 int64_t ticks = timers_state.cpu_ticks_offset;
330 if (timers_state.cpu_ticks_enabled) {
331 ticks += cpu_get_host_ticks();
332 }
333
334 if (timers_state.cpu_ticks_prev > ticks) {
335 /* Non increasing ticks may happen if the host uses software suspend. */
336 timers_state.cpu_ticks_offset += timers_state.cpu_ticks_prev - ticks;
337 ticks = timers_state.cpu_ticks_prev;
338 }
339
340 timers_state.cpu_ticks_prev = ticks;
341 return ticks;
342}
343
d90f3cca
C
344/* return the time elapsed in VM between vm_start and vm_stop. Unless
345 * icount is active, cpu_get_ticks() uses units of the host CPU cycle
346 * counter.
d90f3cca 347 */
946fb27c
PB
348int64_t cpu_get_ticks(void)
349{
5f3e3101
PB
350 int64_t ticks;
351
946fb27c
PB
352 if (use_icount) {
353 return cpu_get_icount();
354 }
5f3e3101 355
f2a4ad6d
PB
356 qemu_spin_lock(&timers_state.vm_clock_lock);
357 ticks = cpu_get_ticks_locked();
358 qemu_spin_unlock(&timers_state.vm_clock_lock);
5f3e3101 359 return ticks;
946fb27c
PB
360}
361
cb365646 362static int64_t cpu_get_clock_locked(void)
946fb27c 363{
1d45cea5 364 int64_t time;
cb365646 365
1d45cea5 366 time = timers_state.cpu_clock_offset;
5f3e3101 367 if (timers_state.cpu_ticks_enabled) {
1d45cea5 368 time += get_clock();
946fb27c 369 }
cb365646 370
1d45cea5 371 return time;
cb365646
LPF
372}
373
d90f3cca 374/* Return the monotonic time elapsed in VM, i.e.,
8212ff86
PM
375 * the time between vm_start and vm_stop
376 */
cb365646
LPF
377int64_t cpu_get_clock(void)
378{
379 int64_t ti;
380 unsigned start;
381
382 do {
383 start = seqlock_read_begin(&timers_state.vm_clock_seqlock);
384 ti = cpu_get_clock_locked();
385 } while (seqlock_read_retry(&timers_state.vm_clock_seqlock, start));
386
387 return ti;
946fb27c
PB
388}
389
cb365646 390/* enable cpu_get_ticks()
3224e878 391 * Caller must hold BQL which serves as mutex for vm_clock_seqlock.
cb365646 392 */
946fb27c
PB
393void cpu_enable_ticks(void)
394{
94377115
PB
395 seqlock_write_lock(&timers_state.vm_clock_seqlock,
396 &timers_state.vm_clock_lock);
946fb27c 397 if (!timers_state.cpu_ticks_enabled) {
4a7428c5 398 timers_state.cpu_ticks_offset -= cpu_get_host_ticks();
946fb27c
PB
399 timers_state.cpu_clock_offset -= get_clock();
400 timers_state.cpu_ticks_enabled = 1;
401 }
94377115
PB
402 seqlock_write_unlock(&timers_state.vm_clock_seqlock,
403 &timers_state.vm_clock_lock);
946fb27c
PB
404}
405
406/* disable cpu_get_ticks() : the clock is stopped. You must not call
cb365646 407 * cpu_get_ticks() after that.
3224e878 408 * Caller must hold BQL which serves as mutex for vm_clock_seqlock.
cb365646 409 */
946fb27c
PB
410void cpu_disable_ticks(void)
411{
94377115
PB
412 seqlock_write_lock(&timers_state.vm_clock_seqlock,
413 &timers_state.vm_clock_lock);
946fb27c 414 if (timers_state.cpu_ticks_enabled) {
4a7428c5 415 timers_state.cpu_ticks_offset += cpu_get_host_ticks();
cb365646 416 timers_state.cpu_clock_offset = cpu_get_clock_locked();
946fb27c
PB
417 timers_state.cpu_ticks_enabled = 0;
418 }
94377115
PB
419 seqlock_write_unlock(&timers_state.vm_clock_seqlock,
420 &timers_state.vm_clock_lock);
946fb27c
PB
421}
422
423/* Correlation between real and virtual time is always going to be
424 fairly approximate, so ignore small variation.
425 When the guest is idle real and virtual time will be aligned in
426 the IO wait loop. */
73bcb24d 427#define ICOUNT_WOBBLE (NANOSECONDS_PER_SECOND / 10)
946fb27c
PB
428
429static void icount_adjust(void)
430{
431 int64_t cur_time;
432 int64_t cur_icount;
433 int64_t delta;
a3270e19
PB
434
435 /* Protected by TimersState mutex. */
946fb27c 436 static int64_t last_delta;
468cc7cf 437
946fb27c
PB
438 /* If the VM is not running, then do nothing. */
439 if (!runstate_is_running()) {
440 return;
441 }
468cc7cf 442
94377115
PB
443 seqlock_write_lock(&timers_state.vm_clock_seqlock,
444 &timers_state.vm_clock_lock);
17a15f1b
PB
445 cur_time = cpu_get_clock_locked();
446 cur_icount = cpu_get_icount_locked();
468cc7cf 447
946fb27c
PB
448 delta = cur_icount - cur_time;
449 /* FIXME: This is a very crude algorithm, somewhat prone to oscillation. */
450 if (delta > 0
451 && last_delta + ICOUNT_WOBBLE < delta * 2
c1ff073c 452 && timers_state.icount_time_shift > 0) {
946fb27c 453 /* The guest is getting too far ahead. Slow time down. */
c1ff073c
PB
454 atomic_set(&timers_state.icount_time_shift,
455 timers_state.icount_time_shift - 1);
946fb27c
PB
456 }
457 if (delta < 0
458 && last_delta - ICOUNT_WOBBLE > delta * 2
c1ff073c 459 && timers_state.icount_time_shift < MAX_ICOUNT_SHIFT) {
946fb27c 460 /* The guest is getting too far behind. Speed time up. */
c1ff073c
PB
461 atomic_set(&timers_state.icount_time_shift,
462 timers_state.icount_time_shift + 1);
946fb27c
PB
463 }
464 last_delta = delta;
c97595d1
EC
465 atomic_set_i64(&timers_state.qemu_icount_bias,
466 cur_icount - (timers_state.qemu_icount
467 << timers_state.icount_time_shift));
94377115
PB
468 seqlock_write_unlock(&timers_state.vm_clock_seqlock,
469 &timers_state.vm_clock_lock);
946fb27c
PB
470}
471
472static void icount_adjust_rt(void *opaque)
473{
b39e3f34 474 timer_mod(timers_state.icount_rt_timer,
1979b908 475 qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL_RT) + 1000);
946fb27c
PB
476 icount_adjust();
477}
478
479static void icount_adjust_vm(void *opaque)
480{
b39e3f34 481 timer_mod(timers_state.icount_vm_timer,
40daca54 482 qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) +
73bcb24d 483 NANOSECONDS_PER_SECOND / 10);
946fb27c
PB
484 icount_adjust();
485}
486
487static int64_t qemu_icount_round(int64_t count)
488{
c1ff073c
PB
489 int shift = atomic_read(&timers_state.icount_time_shift);
490 return (count + (1 << shift) - 1) >> shift;
946fb27c
PB
491}
492
efab87cf 493static void icount_warp_rt(void)
946fb27c 494{
ccffff48
AB
495 unsigned seq;
496 int64_t warp_start;
497
17a15f1b
PB
498 /* The icount_warp_timer is rescheduled soon after vm_clock_warp_start
499 * changes from -1 to another value, so the race here is okay.
500 */
ccffff48
AB
501 do {
502 seq = seqlock_read_begin(&timers_state.vm_clock_seqlock);
b39e3f34 503 warp_start = timers_state.vm_clock_warp_start;
ccffff48
AB
504 } while (seqlock_read_retry(&timers_state.vm_clock_seqlock, seq));
505
506 if (warp_start == -1) {
946fb27c
PB
507 return;
508 }
509
94377115
PB
510 seqlock_write_lock(&timers_state.vm_clock_seqlock,
511 &timers_state.vm_clock_lock);
946fb27c 512 if (runstate_is_running()) {
74c0b816
PB
513 int64_t clock = REPLAY_CLOCK_LOCKED(REPLAY_CLOCK_VIRTUAL_RT,
514 cpu_get_clock_locked());
8ed961d9
PB
515 int64_t warp_delta;
516
b39e3f34 517 warp_delta = clock - timers_state.vm_clock_warp_start;
8ed961d9 518 if (use_icount == 2) {
946fb27c 519 /*
40daca54 520 * In adaptive mode, do not let QEMU_CLOCK_VIRTUAL run too
946fb27c
PB
521 * far ahead of real time.
522 */
17a15f1b 523 int64_t cur_icount = cpu_get_icount_locked();
bf2a7ddb 524 int64_t delta = clock - cur_icount;
8ed961d9 525 warp_delta = MIN(warp_delta, delta);
946fb27c 526 }
c97595d1
EC
527 atomic_set_i64(&timers_state.qemu_icount_bias,
528 timers_state.qemu_icount_bias + warp_delta);
946fb27c 529 }
b39e3f34 530 timers_state.vm_clock_warp_start = -1;
94377115
PB
531 seqlock_write_unlock(&timers_state.vm_clock_seqlock,
532 &timers_state.vm_clock_lock);
8ed961d9
PB
533
534 if (qemu_clock_expired(QEMU_CLOCK_VIRTUAL)) {
535 qemu_clock_notify(QEMU_CLOCK_VIRTUAL);
536 }
946fb27c
PB
537}
538
e76d1798 539static void icount_timer_cb(void *opaque)
efab87cf 540{
e76d1798
PD
541 /* No need for a checkpoint because the timer already synchronizes
542 * with CHECKPOINT_CLOCK_VIRTUAL_RT.
543 */
544 icount_warp_rt();
efab87cf
PD
545}
546
8156be56
PB
547void qtest_clock_warp(int64_t dest)
548{
40daca54 549 int64_t clock = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
efef88b3 550 AioContext *aio_context;
8156be56 551 assert(qtest_enabled());
efef88b3 552 aio_context = qemu_get_aio_context();
8156be56 553 while (clock < dest) {
40daca54 554 int64_t deadline = qemu_clock_deadline_ns_all(QEMU_CLOCK_VIRTUAL);
c9299e2f 555 int64_t warp = qemu_soonest_timeout(dest - clock, deadline);
efef88b3 556
94377115
PB
557 seqlock_write_lock(&timers_state.vm_clock_seqlock,
558 &timers_state.vm_clock_lock);
c97595d1
EC
559 atomic_set_i64(&timers_state.qemu_icount_bias,
560 timers_state.qemu_icount_bias + warp);
94377115
PB
561 seqlock_write_unlock(&timers_state.vm_clock_seqlock,
562 &timers_state.vm_clock_lock);
17a15f1b 563
40daca54 564 qemu_clock_run_timers(QEMU_CLOCK_VIRTUAL);
efef88b3 565 timerlist_run_timers(aio_context->tlg.tl[QEMU_CLOCK_VIRTUAL]);
40daca54 566 clock = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
8156be56 567 }
40daca54 568 qemu_clock_notify(QEMU_CLOCK_VIRTUAL);
8156be56
PB
569}
570
e76d1798 571void qemu_start_warp_timer(void)
946fb27c 572{
ce78d18c 573 int64_t clock;
946fb27c
PB
574 int64_t deadline;
575
e76d1798 576 if (!use_icount) {
946fb27c
PB
577 return;
578 }
579
8bd7f71d
PD
580 /* Nothing to do if the VM is stopped: QEMU_CLOCK_VIRTUAL timers
581 * do not fire, so computing the deadline does not make sense.
582 */
583 if (!runstate_is_running()) {
584 return;
585 }
586
0c08185f
PD
587 if (replay_mode != REPLAY_MODE_PLAY) {
588 if (!all_cpu_threads_idle()) {
589 return;
590 }
8bd7f71d 591
0c08185f
PD
592 if (qtest_enabled()) {
593 /* When testing, qtest commands advance icount. */
594 return;
595 }
946fb27c 596
0c08185f
PD
597 replay_checkpoint(CHECKPOINT_CLOCK_WARP_START);
598 } else {
599 /* warp clock deterministically in record/replay mode */
600 if (!replay_checkpoint(CHECKPOINT_CLOCK_WARP_START)) {
601 /* vCPU is sleeping and warp can't be started.
602 It is probably a race condition: notification sent
603 to vCPU was processed in advance and vCPU went to sleep.
604 Therefore we have to wake it up for doing someting. */
605 if (replay_has_checkpoint()) {
606 qemu_clock_notify(QEMU_CLOCK_VIRTUAL);
607 }
608 return;
609 }
8156be56
PB
610 }
611
ac70aafc 612 /* We want to use the earliest deadline from ALL vm_clocks */
bf2a7ddb 613 clock = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL_RT);
40daca54 614 deadline = qemu_clock_deadline_ns_all(QEMU_CLOCK_VIRTUAL);
ce78d18c 615 if (deadline < 0) {
d7a0f71d
VC
616 static bool notified;
617 if (!icount_sleep && !notified) {
3dc6f869 618 warn_report("icount sleep disabled and no active timers");
d7a0f71d
VC
619 notified = true;
620 }
ce78d18c 621 return;
ac70aafc
AB
622 }
623
946fb27c
PB
624 if (deadline > 0) {
625 /*
40daca54 626 * Ensure QEMU_CLOCK_VIRTUAL proceeds even when the virtual CPU goes to
946fb27c
PB
627 * sleep. Otherwise, the CPU might be waiting for a future timer
628 * interrupt to wake it up, but the interrupt never comes because
629 * the vCPU isn't running any insns and thus doesn't advance the
40daca54 630 * QEMU_CLOCK_VIRTUAL.
946fb27c 631 */
5045e9d9
VC
632 if (!icount_sleep) {
633 /*
634 * We never let VCPUs sleep in no sleep icount mode.
635 * If there is a pending QEMU_CLOCK_VIRTUAL timer we just advance
636 * to the next QEMU_CLOCK_VIRTUAL event and notify it.
637 * It is useful when we want a deterministic execution time,
638 * isolated from host latencies.
639 */
94377115
PB
640 seqlock_write_lock(&timers_state.vm_clock_seqlock,
641 &timers_state.vm_clock_lock);
c97595d1
EC
642 atomic_set_i64(&timers_state.qemu_icount_bias,
643 timers_state.qemu_icount_bias + deadline);
94377115
PB
644 seqlock_write_unlock(&timers_state.vm_clock_seqlock,
645 &timers_state.vm_clock_lock);
5045e9d9
VC
646 qemu_clock_notify(QEMU_CLOCK_VIRTUAL);
647 } else {
648 /*
649 * We do stop VCPUs and only advance QEMU_CLOCK_VIRTUAL after some
650 * "real" time, (related to the time left until the next event) has
651 * passed. The QEMU_CLOCK_VIRTUAL_RT clock will do this.
652 * This avoids that the warps are visible externally; for example,
653 * you will not be sending network packets continuously instead of
654 * every 100ms.
655 */
94377115
PB
656 seqlock_write_lock(&timers_state.vm_clock_seqlock,
657 &timers_state.vm_clock_lock);
b39e3f34
PD
658 if (timers_state.vm_clock_warp_start == -1
659 || timers_state.vm_clock_warp_start > clock) {
660 timers_state.vm_clock_warp_start = clock;
5045e9d9 661 }
94377115
PB
662 seqlock_write_unlock(&timers_state.vm_clock_seqlock,
663 &timers_state.vm_clock_lock);
b39e3f34
PD
664 timer_mod_anticipate(timers_state.icount_warp_timer,
665 clock + deadline);
ce78d18c 666 }
ac70aafc 667 } else if (deadline == 0) {
40daca54 668 qemu_clock_notify(QEMU_CLOCK_VIRTUAL);
946fb27c
PB
669 }
670}
671
e76d1798
PD
672static void qemu_account_warp_timer(void)
673{
674 if (!use_icount || !icount_sleep) {
675 return;
676 }
677
678 /* Nothing to do if the VM is stopped: QEMU_CLOCK_VIRTUAL timers
679 * do not fire, so computing the deadline does not make sense.
680 */
681 if (!runstate_is_running()) {
682 return;
683 }
684
685 /* warp clock deterministically in record/replay mode */
686 if (!replay_checkpoint(CHECKPOINT_CLOCK_WARP_ACCOUNT)) {
687 return;
688 }
689
b39e3f34 690 timer_del(timers_state.icount_warp_timer);
e76d1798
PD
691 icount_warp_rt();
692}
693
d09eae37
FK
694static bool icount_state_needed(void *opaque)
695{
696 return use_icount;
697}
698
b39e3f34
PD
699static bool warp_timer_state_needed(void *opaque)
700{
701 TimersState *s = opaque;
702 return s->icount_warp_timer != NULL;
703}
704
705static bool adjust_timers_state_needed(void *opaque)
706{
707 TimersState *s = opaque;
708 return s->icount_rt_timer != NULL;
709}
710
711/*
712 * Subsection for warp timer migration is optional, because may not be created
713 */
714static const VMStateDescription icount_vmstate_warp_timer = {
715 .name = "timer/icount/warp_timer",
716 .version_id = 1,
717 .minimum_version_id = 1,
718 .needed = warp_timer_state_needed,
719 .fields = (VMStateField[]) {
720 VMSTATE_INT64(vm_clock_warp_start, TimersState),
721 VMSTATE_TIMER_PTR(icount_warp_timer, TimersState),
722 VMSTATE_END_OF_LIST()
723 }
724};
725
726static const VMStateDescription icount_vmstate_adjust_timers = {
727 .name = "timer/icount/timers",
728 .version_id = 1,
729 .minimum_version_id = 1,
730 .needed = adjust_timers_state_needed,
731 .fields = (VMStateField[]) {
732 VMSTATE_TIMER_PTR(icount_rt_timer, TimersState),
733 VMSTATE_TIMER_PTR(icount_vm_timer, TimersState),
734 VMSTATE_END_OF_LIST()
735 }
736};
737
d09eae37
FK
738/*
739 * This is a subsection for icount migration.
740 */
741static const VMStateDescription icount_vmstate_timers = {
742 .name = "timer/icount",
743 .version_id = 1,
744 .minimum_version_id = 1,
5cd8cada 745 .needed = icount_state_needed,
d09eae37
FK
746 .fields = (VMStateField[]) {
747 VMSTATE_INT64(qemu_icount_bias, TimersState),
748 VMSTATE_INT64(qemu_icount, TimersState),
749 VMSTATE_END_OF_LIST()
b39e3f34
PD
750 },
751 .subsections = (const VMStateDescription*[]) {
752 &icount_vmstate_warp_timer,
753 &icount_vmstate_adjust_timers,
754 NULL
d09eae37
FK
755 }
756};
757
946fb27c
PB
758static const VMStateDescription vmstate_timers = {
759 .name = "timer",
760 .version_id = 2,
761 .minimum_version_id = 1,
35d08458 762 .fields = (VMStateField[]) {
946fb27c 763 VMSTATE_INT64(cpu_ticks_offset, TimersState),
c1ff073c 764 VMSTATE_UNUSED(8),
946fb27c
PB
765 VMSTATE_INT64_V(cpu_clock_offset, TimersState, 2),
766 VMSTATE_END_OF_LIST()
d09eae37 767 },
5cd8cada
JQ
768 .subsections = (const VMStateDescription*[]) {
769 &icount_vmstate_timers,
770 NULL
946fb27c
PB
771 }
772};
773
14e6fe12 774static void cpu_throttle_thread(CPUState *cpu, run_on_cpu_data opaque)
2adcc85d 775{
2adcc85d
JH
776 double pct;
777 double throttle_ratio;
778 long sleeptime_ns;
779
780 if (!cpu_throttle_get_percentage()) {
781 return;
782 }
783
784 pct = (double)cpu_throttle_get_percentage()/100;
785 throttle_ratio = pct / (1 - pct);
786 sleeptime_ns = (long)(throttle_ratio * CPU_THROTTLE_TIMESLICE_NS);
787
788 qemu_mutex_unlock_iothread();
2adcc85d
JH
789 g_usleep(sleeptime_ns / 1000); /* Convert ns to us for usleep call */
790 qemu_mutex_lock_iothread();
90bb0c04 791 atomic_set(&cpu->throttle_thread_scheduled, 0);
2adcc85d
JH
792}
793
794static void cpu_throttle_timer_tick(void *opaque)
795{
796 CPUState *cpu;
797 double pct;
798
799 /* Stop the timer if needed */
800 if (!cpu_throttle_get_percentage()) {
801 return;
802 }
803 CPU_FOREACH(cpu) {
804 if (!atomic_xchg(&cpu->throttle_thread_scheduled, 1)) {
14e6fe12
PB
805 async_run_on_cpu(cpu, cpu_throttle_thread,
806 RUN_ON_CPU_NULL);
2adcc85d
JH
807 }
808 }
809
810 pct = (double)cpu_throttle_get_percentage()/100;
811 timer_mod(throttle_timer, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL_RT) +
812 CPU_THROTTLE_TIMESLICE_NS / (1-pct));
813}
814
815void cpu_throttle_set(int new_throttle_pct)
816{
817 /* Ensure throttle percentage is within valid range */
818 new_throttle_pct = MIN(new_throttle_pct, CPU_THROTTLE_PCT_MAX);
819 new_throttle_pct = MAX(new_throttle_pct, CPU_THROTTLE_PCT_MIN);
820
821 atomic_set(&throttle_percentage, new_throttle_pct);
822
823 timer_mod(throttle_timer, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL_RT) +
824 CPU_THROTTLE_TIMESLICE_NS);
825}
826
827void cpu_throttle_stop(void)
828{
829 atomic_set(&throttle_percentage, 0);
830}
831
832bool cpu_throttle_active(void)
833{
834 return (cpu_throttle_get_percentage() != 0);
835}
836
837int cpu_throttle_get_percentage(void)
838{
839 return atomic_read(&throttle_percentage);
840}
841
4603ea01
PD
842void cpu_ticks_init(void)
843{
ccdb3c1f 844 seqlock_init(&timers_state.vm_clock_seqlock);
87a09cdc 845 qemu_spin_init(&timers_state.vm_clock_lock);
4603ea01 846 vmstate_register(NULL, 0, &vmstate_timers, &timers_state);
2adcc85d
JH
847 throttle_timer = timer_new_ns(QEMU_CLOCK_VIRTUAL_RT,
848 cpu_throttle_timer_tick, NULL);
4603ea01
PD
849}
850
1ad9580b 851void configure_icount(QemuOpts *opts, Error **errp)
946fb27c 852{
1ad9580b 853 const char *option;
a8bfac37 854 char *rem_str = NULL;
1ad9580b 855
1ad9580b 856 option = qemu_opt_get(opts, "shift");
946fb27c 857 if (!option) {
a8bfac37
ST
858 if (qemu_opt_get(opts, "align") != NULL) {
859 error_setg(errp, "Please specify shift option when using align");
860 }
946fb27c
PB
861 return;
862 }
f1f4b57e
VC
863
864 icount_sleep = qemu_opt_get_bool(opts, "sleep", true);
5045e9d9 865 if (icount_sleep) {
b39e3f34 866 timers_state.icount_warp_timer = timer_new_ns(QEMU_CLOCK_VIRTUAL_RT,
e76d1798 867 icount_timer_cb, NULL);
5045e9d9 868 }
f1f4b57e 869
a8bfac37 870 icount_align_option = qemu_opt_get_bool(opts, "align", false);
f1f4b57e
VC
871
872 if (icount_align_option && !icount_sleep) {
778d9f9b 873 error_setg(errp, "align=on and sleep=off are incompatible");
f1f4b57e 874 }
946fb27c 875 if (strcmp(option, "auto") != 0) {
a8bfac37 876 errno = 0;
c1ff073c 877 timers_state.icount_time_shift = strtol(option, &rem_str, 0);
a8bfac37
ST
878 if (errno != 0 || *rem_str != '\0' || !strlen(option)) {
879 error_setg(errp, "icount: Invalid shift value");
880 }
946fb27c
PB
881 use_icount = 1;
882 return;
a8bfac37
ST
883 } else if (icount_align_option) {
884 error_setg(errp, "shift=auto and align=on are incompatible");
f1f4b57e 885 } else if (!icount_sleep) {
778d9f9b 886 error_setg(errp, "shift=auto and sleep=off are incompatible");
946fb27c
PB
887 }
888
889 use_icount = 2;
890
891 /* 125MIPS seems a reasonable initial guess at the guest speed.
892 It will be corrected fairly quickly anyway. */
c1ff073c 893 timers_state.icount_time_shift = 3;
946fb27c
PB
894
895 /* Have both realtime and virtual time triggers for speed adjustment.
896 The realtime trigger catches emulated time passing too slowly,
897 the virtual time trigger catches emulated time passing too fast.
898 Realtime triggers occur even when idle, so use them less frequently
899 than VM triggers. */
b39e3f34
PD
900 timers_state.vm_clock_warp_start = -1;
901 timers_state.icount_rt_timer = timer_new_ms(QEMU_CLOCK_VIRTUAL_RT,
bf2a7ddb 902 icount_adjust_rt, NULL);
b39e3f34 903 timer_mod(timers_state.icount_rt_timer,
bf2a7ddb 904 qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL_RT) + 1000);
b39e3f34 905 timers_state.icount_vm_timer = timer_new_ns(QEMU_CLOCK_VIRTUAL,
40daca54 906 icount_adjust_vm, NULL);
b39e3f34 907 timer_mod(timers_state.icount_vm_timer,
40daca54 908 qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) +
73bcb24d 909 NANOSECONDS_PER_SECOND / 10);
946fb27c
PB
910}
911
6546706d
AB
912/***********************************************************/
913/* TCG vCPU kick timer
914 *
915 * The kick timer is responsible for moving single threaded vCPU
916 * emulation on to the next vCPU. If more than one vCPU is running a
917 * timer event with force a cpu->exit so the next vCPU can get
918 * scheduled.
919 *
920 * The timer is removed if all vCPUs are idle and restarted again once
921 * idleness is complete.
922 */
923
924static QEMUTimer *tcg_kick_vcpu_timer;
791158d9 925static CPUState *tcg_current_rr_cpu;
6546706d
AB
926
927#define TCG_KICK_PERIOD (NANOSECONDS_PER_SECOND / 10)
928
929static inline int64_t qemu_tcg_next_kick(void)
930{
931 return qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) + TCG_KICK_PERIOD;
932}
933
791158d9
AB
934/* Kick the currently round-robin scheduled vCPU */
935static void qemu_cpu_kick_rr_cpu(void)
936{
937 CPUState *cpu;
791158d9
AB
938 do {
939 cpu = atomic_mb_read(&tcg_current_rr_cpu);
940 if (cpu) {
941 cpu_exit(cpu);
942 }
943 } while (cpu != atomic_mb_read(&tcg_current_rr_cpu));
944}
945
6b8f0187
PB
946static void do_nothing(CPUState *cpu, run_on_cpu_data unused)
947{
948}
949
3f53bc61
PB
950void qemu_timer_notify_cb(void *opaque, QEMUClockType type)
951{
6b8f0187
PB
952 if (!use_icount || type != QEMU_CLOCK_VIRTUAL) {
953 qemu_notify_event();
954 return;
955 }
956
c52e7132
PM
957 if (qemu_in_vcpu_thread()) {
958 /* A CPU is currently running; kick it back out to the
959 * tcg_cpu_exec() loop so it will recalculate its
960 * icount deadline immediately.
961 */
962 qemu_cpu_kick(current_cpu);
963 } else if (first_cpu) {
6b8f0187
PB
964 /* qemu_cpu_kick is not enough to kick a halted CPU out of
965 * qemu_tcg_wait_io_event. async_run_on_cpu, instead,
966 * causes cpu_thread_is_idle to return false. This way,
967 * handle_icount_deadline can run.
c52e7132
PM
968 * If we have no CPUs at all for some reason, we don't
969 * need to do anything.
6b8f0187
PB
970 */
971 async_run_on_cpu(first_cpu, do_nothing, RUN_ON_CPU_NULL);
972 }
3f53bc61
PB
973}
974
6546706d
AB
975static void kick_tcg_thread(void *opaque)
976{
977 timer_mod(tcg_kick_vcpu_timer, qemu_tcg_next_kick());
791158d9 978 qemu_cpu_kick_rr_cpu();
6546706d
AB
979}
980
981static void start_tcg_kick_timer(void)
982{
db08b687
PB
983 assert(!mttcg_enabled);
984 if (!tcg_kick_vcpu_timer && CPU_NEXT(first_cpu)) {
6546706d
AB
985 tcg_kick_vcpu_timer = timer_new_ns(QEMU_CLOCK_VIRTUAL,
986 kick_tcg_thread, NULL);
1926ab27
AB
987 }
988 if (tcg_kick_vcpu_timer && !timer_pending(tcg_kick_vcpu_timer)) {
6546706d
AB
989 timer_mod(tcg_kick_vcpu_timer, qemu_tcg_next_kick());
990 }
991}
992
993static void stop_tcg_kick_timer(void)
994{
db08b687 995 assert(!mttcg_enabled);
1926ab27 996 if (tcg_kick_vcpu_timer && timer_pending(tcg_kick_vcpu_timer)) {
6546706d 997 timer_del(tcg_kick_vcpu_timer);
6546706d
AB
998 }
999}
1000
296af7c9
BS
1001/***********************************************************/
1002void hw_error(const char *fmt, ...)
1003{
1004 va_list ap;
55e5c285 1005 CPUState *cpu;
296af7c9
BS
1006
1007 va_start(ap, fmt);
1008 fprintf(stderr, "qemu: hardware error: ");
1009 vfprintf(stderr, fmt, ap);
1010 fprintf(stderr, "\n");
bdc44640 1011 CPU_FOREACH(cpu) {
55e5c285 1012 fprintf(stderr, "CPU #%d:\n", cpu->cpu_index);
90c84c56 1013 cpu_dump_state(cpu, stderr, CPU_DUMP_FPU);
296af7c9
BS
1014 }
1015 va_end(ap);
1016 abort();
1017}
1018
1019void cpu_synchronize_all_states(void)
1020{
182735ef 1021 CPUState *cpu;
296af7c9 1022
bdc44640 1023 CPU_FOREACH(cpu) {
182735ef 1024 cpu_synchronize_state(cpu);
c97d6d2c
SAGDR
1025 /* TODO: move to cpu_synchronize_state() */
1026 if (hvf_enabled()) {
1027 hvf_cpu_synchronize_state(cpu);
1028 }
296af7c9
BS
1029 }
1030}
1031
1032void cpu_synchronize_all_post_reset(void)
1033{
182735ef 1034 CPUState *cpu;
296af7c9 1035
bdc44640 1036 CPU_FOREACH(cpu) {
182735ef 1037 cpu_synchronize_post_reset(cpu);
c97d6d2c
SAGDR
1038 /* TODO: move to cpu_synchronize_post_reset() */
1039 if (hvf_enabled()) {
1040 hvf_cpu_synchronize_post_reset(cpu);
1041 }
296af7c9
BS
1042 }
1043}
1044
1045void cpu_synchronize_all_post_init(void)
1046{
182735ef 1047 CPUState *cpu;
296af7c9 1048
bdc44640 1049 CPU_FOREACH(cpu) {
182735ef 1050 cpu_synchronize_post_init(cpu);
c97d6d2c
SAGDR
1051 /* TODO: move to cpu_synchronize_post_init() */
1052 if (hvf_enabled()) {
1053 hvf_cpu_synchronize_post_init(cpu);
1054 }
296af7c9
BS
1055 }
1056}
1057
75e972da
DG
1058void cpu_synchronize_all_pre_loadvm(void)
1059{
1060 CPUState *cpu;
1061
1062 CPU_FOREACH(cpu) {
1063 cpu_synchronize_pre_loadvm(cpu);
1064 }
1065}
1066
4486e89c 1067static int do_vm_stop(RunState state, bool send_stop)
296af7c9 1068{
56983463
KW
1069 int ret = 0;
1070
1354869c 1071 if (runstate_is_running()) {
296af7c9 1072 cpu_disable_ticks();
296af7c9 1073 pause_all_vcpus();
f5bbfba1 1074 runstate_set(state);
1dfb4dd9 1075 vm_state_notify(0, state);
4486e89c 1076 if (send_stop) {
3ab72385 1077 qapi_event_send_stop();
4486e89c 1078 }
296af7c9 1079 }
56983463 1080
594a45ce 1081 bdrv_drain_all();
6d0ceb80 1082 replay_disable_events();
22af08ea 1083 ret = bdrv_flush_all();
594a45ce 1084
56983463 1085 return ret;
296af7c9
BS
1086}
1087
4486e89c
SH
1088/* Special vm_stop() variant for terminating the process. Historically clients
1089 * did not expect a QMP STOP event and so we need to retain compatibility.
1090 */
1091int vm_shutdown(void)
1092{
1093 return do_vm_stop(RUN_STATE_SHUTDOWN, false);
1094}
1095
a1fcaa73 1096static bool cpu_can_run(CPUState *cpu)
296af7c9 1097{
4fdeee7c 1098 if (cpu->stop) {
a1fcaa73 1099 return false;
0ab07c62 1100 }
321bc0b2 1101 if (cpu_is_stopped(cpu)) {
a1fcaa73 1102 return false;
0ab07c62 1103 }
a1fcaa73 1104 return true;
296af7c9
BS
1105}
1106
91325046 1107static void cpu_handle_guest_debug(CPUState *cpu)
83f338f7 1108{
64f6b346 1109 gdb_set_stop_cpu(cpu);
8cf71710 1110 qemu_system_debug_request();
f324e766 1111 cpu->stopped = true;
3c638d06
JK
1112}
1113
6d9cb73c
JK
1114#ifdef CONFIG_LINUX
1115static void sigbus_reraise(void)
1116{
1117 sigset_t set;
1118 struct sigaction action;
1119
1120 memset(&action, 0, sizeof(action));
1121 action.sa_handler = SIG_DFL;
1122 if (!sigaction(SIGBUS, &action, NULL)) {
1123 raise(SIGBUS);
1124 sigemptyset(&set);
1125 sigaddset(&set, SIGBUS);
a2d1761d 1126 pthread_sigmask(SIG_UNBLOCK, &set, NULL);
6d9cb73c
JK
1127 }
1128 perror("Failed to re-raise SIGBUS!\n");
1129 abort();
1130}
1131
d98d4072 1132static void sigbus_handler(int n, siginfo_t *siginfo, void *ctx)
6d9cb73c 1133{
a16fc07e
PB
1134 if (siginfo->si_code != BUS_MCEERR_AO && siginfo->si_code != BUS_MCEERR_AR) {
1135 sigbus_reraise();
1136 }
1137
2ae41db2
PB
1138 if (current_cpu) {
1139 /* Called asynchronously in VCPU thread. */
1140 if (kvm_on_sigbus_vcpu(current_cpu, siginfo->si_code, siginfo->si_addr)) {
1141 sigbus_reraise();
1142 }
1143 } else {
1144 /* Called synchronously (via signalfd) in main thread. */
1145 if (kvm_on_sigbus(siginfo->si_code, siginfo->si_addr)) {
1146 sigbus_reraise();
1147 }
6d9cb73c
JK
1148 }
1149}
1150
1151static void qemu_init_sigbus(void)
1152{
1153 struct sigaction action;
1154
1155 memset(&action, 0, sizeof(action));
1156 action.sa_flags = SA_SIGINFO;
d98d4072 1157 action.sa_sigaction = sigbus_handler;
6d9cb73c
JK
1158 sigaction(SIGBUS, &action, NULL);
1159
1160 prctl(PR_MCE_KILL, PR_MCE_KILL_SET, PR_MCE_KILL_EARLY, 0, 0);
1161}
6d9cb73c 1162#else /* !CONFIG_LINUX */
6d9cb73c
JK
1163static void qemu_init_sigbus(void)
1164{
1165}
a16fc07e 1166#endif /* !CONFIG_LINUX */
ff48eb5f 1167
b2532d88 1168static QemuMutex qemu_global_mutex;
296af7c9
BS
1169
1170static QemuThread io_thread;
1171
296af7c9
BS
1172/* cpu creation */
1173static QemuCond qemu_cpu_cond;
1174/* system init */
296af7c9
BS
1175static QemuCond qemu_pause_cond;
1176
d3b12f5d 1177void qemu_init_cpu_loop(void)
296af7c9 1178{
6d9cb73c 1179 qemu_init_sigbus();
ed94592b 1180 qemu_cond_init(&qemu_cpu_cond);
ed94592b 1181 qemu_cond_init(&qemu_pause_cond);
296af7c9 1182 qemu_mutex_init(&qemu_global_mutex);
296af7c9 1183
b7680cb6 1184 qemu_thread_get_self(&io_thread);
296af7c9
BS
1185}
1186
14e6fe12 1187void run_on_cpu(CPUState *cpu, run_on_cpu_func func, run_on_cpu_data data)
e82bcec2 1188{
d148d90e 1189 do_run_on_cpu(cpu, func, data, &qemu_global_mutex);
3c02270d
CV
1190}
1191
4c055ab5
GZ
1192static void qemu_kvm_destroy_vcpu(CPUState *cpu)
1193{
1194 if (kvm_destroy_vcpu(cpu) < 0) {
1195 error_report("kvm_destroy_vcpu failed");
1196 exit(EXIT_FAILURE);
1197 }
1198}
1199
1200static void qemu_tcg_destroy_vcpu(CPUState *cpu)
1201{
1202}
1203
ebd05fea
DH
1204static void qemu_cpu_stop(CPUState *cpu, bool exit)
1205{
1206 g_assert(qemu_cpu_is_self(cpu));
1207 cpu->stop = false;
1208 cpu->stopped = true;
1209 if (exit) {
1210 cpu_exit(cpu);
1211 }
1212 qemu_cond_broadcast(&qemu_pause_cond);
1213}
1214
509a0d78 1215static void qemu_wait_io_event_common(CPUState *cpu)
296af7c9 1216{
37257942 1217 atomic_mb_set(&cpu->thread_kicked, false);
4fdeee7c 1218 if (cpu->stop) {
ebd05fea 1219 qemu_cpu_stop(cpu, false);
296af7c9 1220 }
a5403c69 1221 process_queued_cpu_work(cpu);
37257942
AB
1222}
1223
a8efa606 1224static void qemu_tcg_rr_wait_io_event(void)
37257942 1225{
a8efa606
PB
1226 CPUState *cpu;
1227
db08b687 1228 while (all_cpu_threads_idle()) {
6546706d 1229 stop_tcg_kick_timer();
a8efa606 1230 qemu_cond_wait(first_cpu->halt_cond, &qemu_global_mutex);
16400322 1231 }
296af7c9 1232
6546706d
AB
1233 start_tcg_kick_timer();
1234
a8efa606
PB
1235 CPU_FOREACH(cpu) {
1236 qemu_wait_io_event_common(cpu);
1237 }
296af7c9
BS
1238}
1239
db08b687 1240static void qemu_wait_io_event(CPUState *cpu)
296af7c9 1241{
a98ae1d8 1242 while (cpu_thread_is_idle(cpu)) {
f5c121b8 1243 qemu_cond_wait(cpu->halt_cond, &qemu_global_mutex);
16400322 1244 }
296af7c9 1245
db08b687
PB
1246#ifdef _WIN32
1247 /* Eat dummy APC queued by qemu_cpu_kick_thread. */
1248 if (!tcg_enabled()) {
1249 SleepEx(0, TRUE);
c97d6d2c 1250 }
db08b687 1251#endif
c97d6d2c
SAGDR
1252 qemu_wait_io_event_common(cpu);
1253}
1254
7e97cd88 1255static void *qemu_kvm_cpu_thread_fn(void *arg)
296af7c9 1256{
48a106bd 1257 CPUState *cpu = arg;
84b4915d 1258 int r;
296af7c9 1259
ab28bd23
PB
1260 rcu_register_thread();
1261
2e7f7a3c 1262 qemu_mutex_lock_iothread();
814e612e 1263 qemu_thread_get_self(cpu->thread);
9f09e18a 1264 cpu->thread_id = qemu_get_thread_id();
626cf8f4 1265 cpu->can_do_io = 1;
4917cf44 1266 current_cpu = cpu;
296af7c9 1267
504134d2 1268 r = kvm_init_vcpu(cpu);
84b4915d 1269 if (r < 0) {
493d89bf 1270 error_report("kvm_init_vcpu failed: %s", strerror(-r));
84b4915d
JK
1271 exit(1);
1272 }
296af7c9 1273
18268b60 1274 kvm_init_cpu_signals(cpu);
296af7c9
BS
1275
1276 /* signal CPU creation */
61a46217 1277 cpu->created = true;
296af7c9
BS
1278 qemu_cond_signal(&qemu_cpu_cond);
1279
4c055ab5 1280 do {
a1fcaa73 1281 if (cpu_can_run(cpu)) {
1458c363 1282 r = kvm_cpu_exec(cpu);
83f338f7 1283 if (r == EXCP_DEBUG) {
91325046 1284 cpu_handle_guest_debug(cpu);
83f338f7 1285 }
0ab07c62 1286 }
db08b687 1287 qemu_wait_io_event(cpu);
4c055ab5 1288 } while (!cpu->unplug || cpu_can_run(cpu));
296af7c9 1289
4c055ab5 1290 qemu_kvm_destroy_vcpu(cpu);
2c579042
BR
1291 cpu->created = false;
1292 qemu_cond_signal(&qemu_cpu_cond);
4c055ab5 1293 qemu_mutex_unlock_iothread();
57615ed5 1294 rcu_unregister_thread();
296af7c9
BS
1295 return NULL;
1296}
1297
c7f0f3b1
AL
1298static void *qemu_dummy_cpu_thread_fn(void *arg)
1299{
1300#ifdef _WIN32
493d89bf 1301 error_report("qtest is not supported under Windows");
c7f0f3b1
AL
1302 exit(1);
1303#else
10a9021d 1304 CPUState *cpu = arg;
c7f0f3b1
AL
1305 sigset_t waitset;
1306 int r;
1307
ab28bd23
PB
1308 rcu_register_thread();
1309
c7f0f3b1 1310 qemu_mutex_lock_iothread();
814e612e 1311 qemu_thread_get_self(cpu->thread);
9f09e18a 1312 cpu->thread_id = qemu_get_thread_id();
626cf8f4 1313 cpu->can_do_io = 1;
37257942 1314 current_cpu = cpu;
c7f0f3b1
AL
1315
1316 sigemptyset(&waitset);
1317 sigaddset(&waitset, SIG_IPI);
1318
1319 /* signal CPU creation */
61a46217 1320 cpu->created = true;
c7f0f3b1
AL
1321 qemu_cond_signal(&qemu_cpu_cond);
1322
d2831ab0 1323 do {
c7f0f3b1
AL
1324 qemu_mutex_unlock_iothread();
1325 do {
1326 int sig;
1327 r = sigwait(&waitset, &sig);
1328 } while (r == -1 && (errno == EAGAIN || errno == EINTR));
1329 if (r == -1) {
1330 perror("sigwait");
1331 exit(1);
1332 }
1333 qemu_mutex_lock_iothread();
db08b687 1334 qemu_wait_io_event(cpu);
d2831ab0 1335 } while (!cpu->unplug);
c7f0f3b1 1336
d40bfcbb 1337 qemu_mutex_unlock_iothread();
d2831ab0 1338 rcu_unregister_thread();
c7f0f3b1
AL
1339 return NULL;
1340#endif
1341}
1342
1be7fcb8
AB
1343static int64_t tcg_get_icount_limit(void)
1344{
1345 int64_t deadline;
1346
1347 if (replay_mode != REPLAY_MODE_PLAY) {
1348 deadline = qemu_clock_deadline_ns_all(QEMU_CLOCK_VIRTUAL);
1349
1350 /* Maintain prior (possibly buggy) behaviour where if no deadline
1351 * was set (as there is no QEMU_CLOCK_VIRTUAL timer) or it is more than
1352 * INT32_MAX nanoseconds ahead, we still use INT32_MAX
1353 * nanoseconds.
1354 */
1355 if ((deadline < 0) || (deadline > INT32_MAX)) {
1356 deadline = INT32_MAX;
1357 }
1358
1359 return qemu_icount_round(deadline);
1360 } else {
1361 return replay_get_instructions();
1362 }
1363}
1364
12e9700d
AB
1365static void handle_icount_deadline(void)
1366{
6b8f0187 1367 assert(qemu_in_vcpu_thread());
12e9700d
AB
1368 if (use_icount) {
1369 int64_t deadline =
1370 qemu_clock_deadline_ns_all(QEMU_CLOCK_VIRTUAL);
1371
1372 if (deadline == 0) {
6b8f0187 1373 /* Wake up other AioContexts. */
12e9700d 1374 qemu_clock_notify(QEMU_CLOCK_VIRTUAL);
6b8f0187 1375 qemu_clock_run_timers(QEMU_CLOCK_VIRTUAL);
12e9700d
AB
1376 }
1377 }
1378}
1379
05248382 1380static void prepare_icount_for_run(CPUState *cpu)
1be7fcb8 1381{
1be7fcb8 1382 if (use_icount) {
eda5f7c6 1383 int insns_left;
05248382
AB
1384
1385 /* These should always be cleared by process_icount_data after
1386 * each vCPU execution. However u16.high can be raised
1387 * asynchronously by cpu_exit/cpu_interrupt/tcg_handle_interrupt
1388 */
1389 g_assert(cpu->icount_decr.u16.low == 0);
1390 g_assert(cpu->icount_extra == 0);
1391
eda5f7c6
AB
1392 cpu->icount_budget = tcg_get_icount_limit();
1393 insns_left = MIN(0xffff, cpu->icount_budget);
1394 cpu->icount_decr.u16.low = insns_left;
1395 cpu->icount_extra = cpu->icount_budget - insns_left;
d759c951
AB
1396
1397 replay_mutex_lock();
1be7fcb8 1398 }
05248382
AB
1399}
1400
1401static void process_icount_data(CPUState *cpu)
1402{
1be7fcb8 1403 if (use_icount) {
e4cd9657 1404 /* Account for executed instructions */
512d3c80 1405 cpu_update_icount(cpu);
05248382
AB
1406
1407 /* Reset the counters */
1408 cpu->icount_decr.u16.low = 0;
1be7fcb8 1409 cpu->icount_extra = 0;
e4cd9657
AB
1410 cpu->icount_budget = 0;
1411
1be7fcb8 1412 replay_account_executed_instructions();
d759c951
AB
1413
1414 replay_mutex_unlock();
1be7fcb8 1415 }
05248382
AB
1416}
1417
1418
1419static int tcg_cpu_exec(CPUState *cpu)
1420{
1421 int ret;
1422#ifdef CONFIG_PROFILER
1423 int64_t ti;
1424#endif
1425
f28d0dfd 1426 assert(tcg_enabled());
05248382
AB
1427#ifdef CONFIG_PROFILER
1428 ti = profile_getclock();
1429#endif
05248382
AB
1430 cpu_exec_start(cpu);
1431 ret = cpu_exec(cpu);
1432 cpu_exec_end(cpu);
05248382 1433#ifdef CONFIG_PROFILER
72fd2efb
EC
1434 atomic_set(&tcg_ctx->prof.cpu_exec_time,
1435 tcg_ctx->prof.cpu_exec_time + profile_getclock() - ti);
05248382 1436#endif
1be7fcb8
AB
1437 return ret;
1438}
1439
c93bbbef
AB
1440/* Destroy any remaining vCPUs which have been unplugged and have
1441 * finished running
1442 */
1443static void deal_with_unplugged_cpus(void)
1be7fcb8 1444{
c93bbbef 1445 CPUState *cpu;
1be7fcb8 1446
c93bbbef
AB
1447 CPU_FOREACH(cpu) {
1448 if (cpu->unplug && !cpu_can_run(cpu)) {
1449 qemu_tcg_destroy_vcpu(cpu);
1450 cpu->created = false;
1451 qemu_cond_signal(&qemu_cpu_cond);
1be7fcb8
AB
1452 break;
1453 }
1454 }
1be7fcb8 1455}
bdb7ca67 1456
6546706d
AB
1457/* Single-threaded TCG
1458 *
1459 * In the single-threaded case each vCPU is simulated in turn. If
1460 * there is more than a single vCPU we create a simple timer to kick
1461 * the vCPU and ensure we don't get stuck in a tight loop in one vCPU.
1462 * This is done explicitly rather than relying on side-effects
1463 * elsewhere.
1464 */
1465
37257942 1466static void *qemu_tcg_rr_cpu_thread_fn(void *arg)
296af7c9 1467{
c3586ba7 1468 CPUState *cpu = arg;
296af7c9 1469
f28d0dfd 1470 assert(tcg_enabled());
ab28bd23 1471 rcu_register_thread();
3468b59e 1472 tcg_register_thread();
ab28bd23 1473
2e7f7a3c 1474 qemu_mutex_lock_iothread();
814e612e 1475 qemu_thread_get_self(cpu->thread);
296af7c9 1476
5a9c973b
DH
1477 cpu->thread_id = qemu_get_thread_id();
1478 cpu->created = true;
1479 cpu->can_do_io = 1;
296af7c9
BS
1480 qemu_cond_signal(&qemu_cpu_cond);
1481
fa7d1867 1482 /* wait for initial kick-off after machine start */
c28e399c 1483 while (first_cpu->stopped) {
d5f8d613 1484 qemu_cond_wait(first_cpu->halt_cond, &qemu_global_mutex);
8e564b4e
JK
1485
1486 /* process any pending work */
bdc44640 1487 CPU_FOREACH(cpu) {
37257942 1488 current_cpu = cpu;
182735ef 1489 qemu_wait_io_event_common(cpu);
8e564b4e 1490 }
0ab07c62 1491 }
296af7c9 1492
6546706d
AB
1493 start_tcg_kick_timer();
1494
c93bbbef
AB
1495 cpu = first_cpu;
1496
e5143e30
AB
1497 /* process any pending work */
1498 cpu->exit_request = 1;
1499
296af7c9 1500 while (1) {
d759c951
AB
1501 qemu_mutex_unlock_iothread();
1502 replay_mutex_lock();
1503 qemu_mutex_lock_iothread();
c93bbbef
AB
1504 /* Account partial waits to QEMU_CLOCK_VIRTUAL. */
1505 qemu_account_warp_timer();
1506
6b8f0187
PB
1507 /* Run the timers here. This is much more efficient than
1508 * waking up the I/O thread and waiting for completion.
1509 */
1510 handle_icount_deadline();
1511
d759c951
AB
1512 replay_mutex_unlock();
1513
c93bbbef
AB
1514 if (!cpu) {
1515 cpu = first_cpu;
1516 }
1517
e5143e30
AB
1518 while (cpu && !cpu->queued_work_first && !cpu->exit_request) {
1519
791158d9 1520 atomic_mb_set(&tcg_current_rr_cpu, cpu);
37257942 1521 current_cpu = cpu;
c93bbbef
AB
1522
1523 qemu_clock_enable(QEMU_CLOCK_VIRTUAL,
1524 (cpu->singlestep_enabled & SSTEP_NOTIMER) == 0);
1525
1526 if (cpu_can_run(cpu)) {
1527 int r;
05248382 1528
d759c951 1529 qemu_mutex_unlock_iothread();
05248382
AB
1530 prepare_icount_for_run(cpu);
1531
c93bbbef 1532 r = tcg_cpu_exec(cpu);
05248382
AB
1533
1534 process_icount_data(cpu);
d759c951 1535 qemu_mutex_lock_iothread();
05248382 1536
c93bbbef
AB
1537 if (r == EXCP_DEBUG) {
1538 cpu_handle_guest_debug(cpu);
1539 break;
08e73c48
PK
1540 } else if (r == EXCP_ATOMIC) {
1541 qemu_mutex_unlock_iothread();
1542 cpu_exec_step_atomic(cpu);
1543 qemu_mutex_lock_iothread();
1544 break;
c93bbbef 1545 }
37257942 1546 } else if (cpu->stop) {
c93bbbef
AB
1547 if (cpu->unplug) {
1548 cpu = CPU_NEXT(cpu);
1549 }
1550 break;
1551 }
1552
e5143e30
AB
1553 cpu = CPU_NEXT(cpu);
1554 } /* while (cpu && !cpu->exit_request).. */
1555
791158d9
AB
1556 /* Does not need atomic_mb_set because a spurious wakeup is okay. */
1557 atomic_set(&tcg_current_rr_cpu, NULL);
c93bbbef 1558
e5143e30
AB
1559 if (cpu && cpu->exit_request) {
1560 atomic_mb_set(&cpu->exit_request, 0);
1561 }
ac70aafc 1562
013aabdc
CD
1563 if (use_icount && all_cpu_threads_idle()) {
1564 /*
1565 * When all cpus are sleeping (e.g in WFI), to avoid a deadlock
1566 * in the main_loop, wake it up in order to start the warp timer.
1567 */
1568 qemu_notify_event();
1569 }
1570
a8efa606 1571 qemu_tcg_rr_wait_io_event();
c93bbbef 1572 deal_with_unplugged_cpus();
296af7c9
BS
1573 }
1574
9b0605f9 1575 rcu_unregister_thread();
296af7c9
BS
1576 return NULL;
1577}
1578
b0cb0a66
VP
1579static void *qemu_hax_cpu_thread_fn(void *arg)
1580{
1581 CPUState *cpu = arg;
1582 int r;
b3d3a426 1583
9857c2d2 1584 rcu_register_thread();
b3d3a426 1585 qemu_mutex_lock_iothread();
b0cb0a66 1586 qemu_thread_get_self(cpu->thread);
b0cb0a66
VP
1587
1588 cpu->thread_id = qemu_get_thread_id();
1589 cpu->created = true;
1590 cpu->halted = 0;
1591 current_cpu = cpu;
1592
1593 hax_init_vcpu(cpu);
1594 qemu_cond_signal(&qemu_cpu_cond);
1595
9857c2d2 1596 do {
b0cb0a66
VP
1597 if (cpu_can_run(cpu)) {
1598 r = hax_smp_cpu_exec(cpu);
1599 if (r == EXCP_DEBUG) {
1600 cpu_handle_guest_debug(cpu);
1601 }
1602 }
1603
db08b687 1604 qemu_wait_io_event(cpu);
9857c2d2
PB
1605 } while (!cpu->unplug || cpu_can_run(cpu));
1606 rcu_unregister_thread();
b0cb0a66
VP
1607 return NULL;
1608}
1609
c97d6d2c
SAGDR
1610/* The HVF-specific vCPU thread function. This one should only run when the host
1611 * CPU supports the VMX "unrestricted guest" feature. */
1612static void *qemu_hvf_cpu_thread_fn(void *arg)
1613{
1614 CPUState *cpu = arg;
1615
1616 int r;
1617
1618 assert(hvf_enabled());
1619
1620 rcu_register_thread();
1621
1622 qemu_mutex_lock_iothread();
1623 qemu_thread_get_self(cpu->thread);
1624
1625 cpu->thread_id = qemu_get_thread_id();
1626 cpu->can_do_io = 1;
1627 current_cpu = cpu;
1628
1629 hvf_init_vcpu(cpu);
1630
1631 /* signal CPU creation */
1632 cpu->created = true;
1633 qemu_cond_signal(&qemu_cpu_cond);
1634
1635 do {
1636 if (cpu_can_run(cpu)) {
1637 r = hvf_vcpu_exec(cpu);
1638 if (r == EXCP_DEBUG) {
1639 cpu_handle_guest_debug(cpu);
1640 }
1641 }
db08b687 1642 qemu_wait_io_event(cpu);
c97d6d2c
SAGDR
1643 } while (!cpu->unplug || cpu_can_run(cpu));
1644
1645 hvf_vcpu_destroy(cpu);
1646 cpu->created = false;
1647 qemu_cond_signal(&qemu_cpu_cond);
1648 qemu_mutex_unlock_iothread();
8178e637 1649 rcu_unregister_thread();
c97d6d2c
SAGDR
1650 return NULL;
1651}
1652
19306806
JTV
1653static void *qemu_whpx_cpu_thread_fn(void *arg)
1654{
1655 CPUState *cpu = arg;
1656 int r;
1657
1658 rcu_register_thread();
1659
1660 qemu_mutex_lock_iothread();
1661 qemu_thread_get_self(cpu->thread);
1662 cpu->thread_id = qemu_get_thread_id();
1663 current_cpu = cpu;
1664
1665 r = whpx_init_vcpu(cpu);
1666 if (r < 0) {
1667 fprintf(stderr, "whpx_init_vcpu failed: %s\n", strerror(-r));
1668 exit(1);
1669 }
1670
1671 /* signal CPU creation */
1672 cpu->created = true;
1673 qemu_cond_signal(&qemu_cpu_cond);
1674
1675 do {
1676 if (cpu_can_run(cpu)) {
1677 r = whpx_vcpu_exec(cpu);
1678 if (r == EXCP_DEBUG) {
1679 cpu_handle_guest_debug(cpu);
1680 }
1681 }
1682 while (cpu_thread_is_idle(cpu)) {
1683 qemu_cond_wait(cpu->halt_cond, &qemu_global_mutex);
1684 }
1685 qemu_wait_io_event_common(cpu);
1686 } while (!cpu->unplug || cpu_can_run(cpu));
1687
1688 whpx_destroy_vcpu(cpu);
1689 cpu->created = false;
1690 qemu_cond_signal(&qemu_cpu_cond);
1691 qemu_mutex_unlock_iothread();
1692 rcu_unregister_thread();
c97d6d2c
SAGDR
1693 return NULL;
1694}
1695
b0cb0a66
VP
1696#ifdef _WIN32
1697static void CALLBACK dummy_apc_func(ULONG_PTR unused)
1698{
1699}
1700#endif
1701
37257942
AB
1702/* Multi-threaded TCG
1703 *
1704 * In the multi-threaded case each vCPU has its own thread. The TLS
1705 * variable current_cpu can be used deep in the code to find the
1706 * current CPUState for a given thread.
1707 */
1708
1709static void *qemu_tcg_cpu_thread_fn(void *arg)
1710{
1711 CPUState *cpu = arg;
1712
f28d0dfd 1713 assert(tcg_enabled());
bf51c720
AB
1714 g_assert(!use_icount);
1715
37257942 1716 rcu_register_thread();
3468b59e 1717 tcg_register_thread();
37257942
AB
1718
1719 qemu_mutex_lock_iothread();
1720 qemu_thread_get_self(cpu->thread);
1721
1722 cpu->thread_id = qemu_get_thread_id();
1723 cpu->created = true;
1724 cpu->can_do_io = 1;
1725 current_cpu = cpu;
1726 qemu_cond_signal(&qemu_cpu_cond);
1727
1728 /* process any pending work */
1729 cpu->exit_request = 1;
1730
54961aac 1731 do {
37257942
AB
1732 if (cpu_can_run(cpu)) {
1733 int r;
d759c951 1734 qemu_mutex_unlock_iothread();
37257942 1735 r = tcg_cpu_exec(cpu);
d759c951 1736 qemu_mutex_lock_iothread();
37257942
AB
1737 switch (r) {
1738 case EXCP_DEBUG:
1739 cpu_handle_guest_debug(cpu);
1740 break;
1741 case EXCP_HALTED:
1742 /* during start-up the vCPU is reset and the thread is
1743 * kicked several times. If we don't ensure we go back
1744 * to sleep in the halted state we won't cleanly
1745 * start-up when the vCPU is enabled.
1746 *
1747 * cpu->halted should ensure we sleep in wait_io_event
1748 */
1749 g_assert(cpu->halted);
1750 break;
08e73c48
PK
1751 case EXCP_ATOMIC:
1752 qemu_mutex_unlock_iothread();
1753 cpu_exec_step_atomic(cpu);
1754 qemu_mutex_lock_iothread();
37257942
AB
1755 default:
1756 /* Ignore everything else? */
1757 break;
1758 }
1759 }
1760
37257942 1761 atomic_mb_set(&cpu->exit_request, 0);
db08b687 1762 qemu_wait_io_event(cpu);
9b0605f9 1763 } while (!cpu->unplug || cpu_can_run(cpu));
37257942 1764
9b0605f9
PB
1765 qemu_tcg_destroy_vcpu(cpu);
1766 cpu->created = false;
1767 qemu_cond_signal(&qemu_cpu_cond);
1768 qemu_mutex_unlock_iothread();
1769 rcu_unregister_thread();
37257942
AB
1770 return NULL;
1771}
1772
2ff09a40 1773static void qemu_cpu_kick_thread(CPUState *cpu)
cc015e9a
PB
1774{
1775#ifndef _WIN32
1776 int err;
1777
e0c38211
PB
1778 if (cpu->thread_kicked) {
1779 return;
9102deda 1780 }
e0c38211 1781 cpu->thread_kicked = true;
814e612e 1782 err = pthread_kill(cpu->thread->thread, SIG_IPI);
d455ebc4 1783 if (err && err != ESRCH) {
cc015e9a
PB
1784 fprintf(stderr, "qemu:%s: %s", __func__, strerror(err));
1785 exit(1);
1786 }
1787#else /* _WIN32 */
b0cb0a66 1788 if (!qemu_cpu_is_self(cpu)) {
19306806
JTV
1789 if (whpx_enabled()) {
1790 whpx_vcpu_kick(cpu);
1791 } else if (!QueueUserAPC(dummy_apc_func, cpu->hThread, 0)) {
b0cb0a66
VP
1792 fprintf(stderr, "%s: QueueUserAPC failed with error %lu\n",
1793 __func__, GetLastError());
1794 exit(1);
1795 }
1796 }
e0c38211
PB
1797#endif
1798}
ed9164a3 1799
c08d7424 1800void qemu_cpu_kick(CPUState *cpu)
296af7c9 1801{
f5c121b8 1802 qemu_cond_broadcast(cpu->halt_cond);
e0c38211 1803 if (tcg_enabled()) {
791158d9 1804 cpu_exit(cpu);
37257942 1805 /* NOP unless doing single-thread RR */
791158d9 1806 qemu_cpu_kick_rr_cpu();
e0c38211 1807 } else {
b0cb0a66
VP
1808 if (hax_enabled()) {
1809 /*
1810 * FIXME: race condition with the exit_request check in
1811 * hax_vcpu_hax_exec
1812 */
1813 cpu->exit_request = 1;
1814 }
e0c38211
PB
1815 qemu_cpu_kick_thread(cpu);
1816 }
296af7c9
BS
1817}
1818
46d62fac 1819void qemu_cpu_kick_self(void)
296af7c9 1820{
4917cf44 1821 assert(current_cpu);
9102deda 1822 qemu_cpu_kick_thread(current_cpu);
296af7c9
BS
1823}
1824
60e82579 1825bool qemu_cpu_is_self(CPUState *cpu)
296af7c9 1826{
814e612e 1827 return qemu_thread_is_self(cpu->thread);
296af7c9
BS
1828}
1829
79e2b9ae 1830bool qemu_in_vcpu_thread(void)
aa723c23 1831{
4917cf44 1832 return current_cpu && qemu_cpu_is_self(current_cpu);
aa723c23
JQ
1833}
1834
afbe7053
PB
1835static __thread bool iothread_locked = false;
1836
1837bool qemu_mutex_iothread_locked(void)
1838{
1839 return iothread_locked;
1840}
1841
cb764d06
EC
1842/*
1843 * The BQL is taken from so many places that it is worth profiling the
1844 * callers directly, instead of funneling them all through a single function.
1845 */
1846void qemu_mutex_lock_iothread_impl(const char *file, int line)
296af7c9 1847{
cb764d06
EC
1848 QemuMutexLockFunc bql_lock = atomic_read(&qemu_bql_mutex_lock_func);
1849
8d04fb55 1850 g_assert(!qemu_mutex_iothread_locked());
cb764d06 1851 bql_lock(&qemu_global_mutex, file, line);
afbe7053 1852 iothread_locked = true;
296af7c9
BS
1853}
1854
1855void qemu_mutex_unlock_iothread(void)
1856{
8d04fb55 1857 g_assert(qemu_mutex_iothread_locked());
afbe7053 1858 iothread_locked = false;
296af7c9
BS
1859 qemu_mutex_unlock(&qemu_global_mutex);
1860}
1861
e8faee06 1862static bool all_vcpus_paused(void)
296af7c9 1863{
bdc44640 1864 CPUState *cpu;
296af7c9 1865
bdc44640 1866 CPU_FOREACH(cpu) {
182735ef 1867 if (!cpu->stopped) {
e8faee06 1868 return false;
0ab07c62 1869 }
296af7c9
BS
1870 }
1871
e8faee06 1872 return true;
296af7c9
BS
1873}
1874
1875void pause_all_vcpus(void)
1876{
bdc44640 1877 CPUState *cpu;
296af7c9 1878
40daca54 1879 qemu_clock_enable(QEMU_CLOCK_VIRTUAL, false);
bdc44640 1880 CPU_FOREACH(cpu) {
ebd05fea
DH
1881 if (qemu_cpu_is_self(cpu)) {
1882 qemu_cpu_stop(cpu, true);
1883 } else {
1884 cpu->stop = true;
1885 qemu_cpu_kick(cpu);
1886 }
d798e974
JK
1887 }
1888
d759c951
AB
1889 /* We need to drop the replay_lock so any vCPU threads woken up
1890 * can finish their replay tasks
1891 */
1892 replay_mutex_unlock();
1893
296af7c9 1894 while (!all_vcpus_paused()) {
be7d6c57 1895 qemu_cond_wait(&qemu_pause_cond, &qemu_global_mutex);
bdc44640 1896 CPU_FOREACH(cpu) {
182735ef 1897 qemu_cpu_kick(cpu);
296af7c9
BS
1898 }
1899 }
d759c951
AB
1900
1901 qemu_mutex_unlock_iothread();
1902 replay_mutex_lock();
1903 qemu_mutex_lock_iothread();
296af7c9
BS
1904}
1905
2993683b
IM
1906void cpu_resume(CPUState *cpu)
1907{
1908 cpu->stop = false;
1909 cpu->stopped = false;
1910 qemu_cpu_kick(cpu);
1911}
1912
296af7c9
BS
1913void resume_all_vcpus(void)
1914{
bdc44640 1915 CPUState *cpu;
296af7c9 1916
40daca54 1917 qemu_clock_enable(QEMU_CLOCK_VIRTUAL, true);
bdc44640 1918 CPU_FOREACH(cpu) {
182735ef 1919 cpu_resume(cpu);
296af7c9
BS
1920 }
1921}
1922
dbadee4f 1923void cpu_remove_sync(CPUState *cpu)
4c055ab5
GZ
1924{
1925 cpu->stop = true;
1926 cpu->unplug = true;
1927 qemu_cpu_kick(cpu);
dbadee4f
PB
1928 qemu_mutex_unlock_iothread();
1929 qemu_thread_join(cpu->thread);
1930 qemu_mutex_lock_iothread();
2c579042
BR
1931}
1932
4900116e
DDAG
1933/* For temporary buffers for forming a name */
1934#define VCPU_THREAD_NAME_SIZE 16
1935
e5ab30a2 1936static void qemu_tcg_init_vcpu(CPUState *cpu)
296af7c9 1937{
4900116e 1938 char thread_name[VCPU_THREAD_NAME_SIZE];
37257942
AB
1939 static QemuCond *single_tcg_halt_cond;
1940 static QemuThread *single_tcg_cpu_thread;
e8feb96f
EC
1941 static int tcg_region_inited;
1942
f28d0dfd 1943 assert(tcg_enabled());
e8feb96f
EC
1944 /*
1945 * Initialize TCG regions--once. Now is a good time, because:
1946 * (1) TCG's init context, prologue and target globals have been set up.
1947 * (2) qemu_tcg_mttcg_enabled() works now (TCG init code runs before the
1948 * -accel flag is processed, so the check doesn't work then).
1949 */
1950 if (!tcg_region_inited) {
1951 tcg_region_inited = 1;
1952 tcg_region_init();
1953 }
4900116e 1954
37257942 1955 if (qemu_tcg_mttcg_enabled() || !single_tcg_cpu_thread) {
814e612e 1956 cpu->thread = g_malloc0(sizeof(QemuThread));
f5c121b8
AF
1957 cpu->halt_cond = g_malloc0(sizeof(QemuCond));
1958 qemu_cond_init(cpu->halt_cond);
37257942
AB
1959
1960 if (qemu_tcg_mttcg_enabled()) {
1961 /* create a thread per vCPU with TCG (MTTCG) */
1962 parallel_cpus = true;
1963 snprintf(thread_name, VCPU_THREAD_NAME_SIZE, "CPU %d/TCG",
4900116e 1964 cpu->cpu_index);
37257942
AB
1965
1966 qemu_thread_create(cpu->thread, thread_name, qemu_tcg_cpu_thread_fn,
1967 cpu, QEMU_THREAD_JOINABLE);
1968
1969 } else {
1970 /* share a single thread for all cpus with TCG */
1971 snprintf(thread_name, VCPU_THREAD_NAME_SIZE, "ALL CPUs/TCG");
1972 qemu_thread_create(cpu->thread, thread_name,
1973 qemu_tcg_rr_cpu_thread_fn,
1974 cpu, QEMU_THREAD_JOINABLE);
1975
1976 single_tcg_halt_cond = cpu->halt_cond;
1977 single_tcg_cpu_thread = cpu->thread;
1978 }
1ecf47bf 1979#ifdef _WIN32
814e612e 1980 cpu->hThread = qemu_thread_get_handle(cpu->thread);
1ecf47bf 1981#endif
296af7c9 1982 } else {
37257942
AB
1983 /* For non-MTTCG cases we share the thread */
1984 cpu->thread = single_tcg_cpu_thread;
1985 cpu->halt_cond = single_tcg_halt_cond;
a342173a
DH
1986 cpu->thread_id = first_cpu->thread_id;
1987 cpu->can_do_io = 1;
1988 cpu->created = true;
296af7c9
BS
1989 }
1990}
1991
b0cb0a66
VP
1992static void qemu_hax_start_vcpu(CPUState *cpu)
1993{
1994 char thread_name[VCPU_THREAD_NAME_SIZE];
1995
1996 cpu->thread = g_malloc0(sizeof(QemuThread));
1997 cpu->halt_cond = g_malloc0(sizeof(QemuCond));
1998 qemu_cond_init(cpu->halt_cond);
1999
2000 snprintf(thread_name, VCPU_THREAD_NAME_SIZE, "CPU %d/HAX",
2001 cpu->cpu_index);
2002 qemu_thread_create(cpu->thread, thread_name, qemu_hax_cpu_thread_fn,
2003 cpu, QEMU_THREAD_JOINABLE);
2004#ifdef _WIN32
2005 cpu->hThread = qemu_thread_get_handle(cpu->thread);
2006#endif
b0cb0a66
VP
2007}
2008
48a106bd 2009static void qemu_kvm_start_vcpu(CPUState *cpu)
296af7c9 2010{
4900116e
DDAG
2011 char thread_name[VCPU_THREAD_NAME_SIZE];
2012
814e612e 2013 cpu->thread = g_malloc0(sizeof(QemuThread));
f5c121b8
AF
2014 cpu->halt_cond = g_malloc0(sizeof(QemuCond));
2015 qemu_cond_init(cpu->halt_cond);
4900116e
DDAG
2016 snprintf(thread_name, VCPU_THREAD_NAME_SIZE, "CPU %d/KVM",
2017 cpu->cpu_index);
2018 qemu_thread_create(cpu->thread, thread_name, qemu_kvm_cpu_thread_fn,
2019 cpu, QEMU_THREAD_JOINABLE);
296af7c9
BS
2020}
2021
c97d6d2c
SAGDR
2022static void qemu_hvf_start_vcpu(CPUState *cpu)
2023{
2024 char thread_name[VCPU_THREAD_NAME_SIZE];
2025
2026 /* HVF currently does not support TCG, and only runs in
2027 * unrestricted-guest mode. */
2028 assert(hvf_enabled());
2029
2030 cpu->thread = g_malloc0(sizeof(QemuThread));
2031 cpu->halt_cond = g_malloc0(sizeof(QemuCond));
2032 qemu_cond_init(cpu->halt_cond);
2033
2034 snprintf(thread_name, VCPU_THREAD_NAME_SIZE, "CPU %d/HVF",
2035 cpu->cpu_index);
2036 qemu_thread_create(cpu->thread, thread_name, qemu_hvf_cpu_thread_fn,
2037 cpu, QEMU_THREAD_JOINABLE);
c97d6d2c
SAGDR
2038}
2039
19306806
JTV
2040static void qemu_whpx_start_vcpu(CPUState *cpu)
2041{
2042 char thread_name[VCPU_THREAD_NAME_SIZE];
2043
2044 cpu->thread = g_malloc0(sizeof(QemuThread));
2045 cpu->halt_cond = g_malloc0(sizeof(QemuCond));
2046 qemu_cond_init(cpu->halt_cond);
2047 snprintf(thread_name, VCPU_THREAD_NAME_SIZE, "CPU %d/WHPX",
2048 cpu->cpu_index);
2049 qemu_thread_create(cpu->thread, thread_name, qemu_whpx_cpu_thread_fn,
2050 cpu, QEMU_THREAD_JOINABLE);
2051#ifdef _WIN32
2052 cpu->hThread = qemu_thread_get_handle(cpu->thread);
2053#endif
19306806
JTV
2054}
2055
10a9021d 2056static void qemu_dummy_start_vcpu(CPUState *cpu)
c7f0f3b1 2057{
4900116e
DDAG
2058 char thread_name[VCPU_THREAD_NAME_SIZE];
2059
814e612e 2060 cpu->thread = g_malloc0(sizeof(QemuThread));
f5c121b8
AF
2061 cpu->halt_cond = g_malloc0(sizeof(QemuCond));
2062 qemu_cond_init(cpu->halt_cond);
4900116e
DDAG
2063 snprintf(thread_name, VCPU_THREAD_NAME_SIZE, "CPU %d/DUMMY",
2064 cpu->cpu_index);
2065 qemu_thread_create(cpu->thread, thread_name, qemu_dummy_cpu_thread_fn, cpu,
c7f0f3b1 2066 QEMU_THREAD_JOINABLE);
c7f0f3b1
AL
2067}
2068
c643bed9 2069void qemu_init_vcpu(CPUState *cpu)
296af7c9 2070{
ce3960eb
AF
2071 cpu->nr_cores = smp_cores;
2072 cpu->nr_threads = smp_threads;
f324e766 2073 cpu->stopped = true;
56943e8c
PM
2074
2075 if (!cpu->as) {
2076 /* If the target cpu hasn't set up any address spaces itself,
2077 * give it the default one.
2078 */
12ebc9a7 2079 cpu->num_ases = 1;
80ceb07a 2080 cpu_address_space_init(cpu, 0, "cpu-memory", cpu->memory);
56943e8c
PM
2081 }
2082
0ab07c62 2083 if (kvm_enabled()) {
48a106bd 2084 qemu_kvm_start_vcpu(cpu);
b0cb0a66
VP
2085 } else if (hax_enabled()) {
2086 qemu_hax_start_vcpu(cpu);
c97d6d2c
SAGDR
2087 } else if (hvf_enabled()) {
2088 qemu_hvf_start_vcpu(cpu);
c7f0f3b1 2089 } else if (tcg_enabled()) {
e5ab30a2 2090 qemu_tcg_init_vcpu(cpu);
19306806
JTV
2091 } else if (whpx_enabled()) {
2092 qemu_whpx_start_vcpu(cpu);
c7f0f3b1 2093 } else {
10a9021d 2094 qemu_dummy_start_vcpu(cpu);
0ab07c62 2095 }
81e96311
DH
2096
2097 while (!cpu->created) {
2098 qemu_cond_wait(&qemu_cpu_cond, &qemu_global_mutex);
2099 }
296af7c9
BS
2100}
2101
b4a3d965 2102void cpu_stop_current(void)
296af7c9 2103{
4917cf44 2104 if (current_cpu) {
0ec7e677
PM
2105 current_cpu->stop = true;
2106 cpu_exit(current_cpu);
b4a3d965 2107 }
296af7c9
BS
2108}
2109
56983463 2110int vm_stop(RunState state)
296af7c9 2111{
aa723c23 2112 if (qemu_in_vcpu_thread()) {
74892d24 2113 qemu_system_vmstop_request_prepare();
1dfb4dd9 2114 qemu_system_vmstop_request(state);
296af7c9
BS
2115 /*
2116 * FIXME: should not return to device code in case
2117 * vm_stop() has been requested.
2118 */
b4a3d965 2119 cpu_stop_current();
56983463 2120 return 0;
296af7c9 2121 }
56983463 2122
4486e89c 2123 return do_vm_stop(state, true);
296af7c9
BS
2124}
2125
2d76e823
CI
2126/**
2127 * Prepare for (re)starting the VM.
2128 * Returns -1 if the vCPUs are not to be restarted (e.g. if they are already
2129 * running or in case of an error condition), 0 otherwise.
2130 */
2131int vm_prepare_start(void)
2132{
2133 RunState requested;
2d76e823
CI
2134
2135 qemu_vmstop_requested(&requested);
2136 if (runstate_is_running() && requested == RUN_STATE__MAX) {
2137 return -1;
2138 }
2139
2140 /* Ensure that a STOP/RESUME pair of events is emitted if a
2141 * vmstop request was pending. The BLOCK_IO_ERROR event, for
2142 * example, according to documentation is always followed by
2143 * the STOP event.
2144 */
2145 if (runstate_is_running()) {
3ab72385
PX
2146 qapi_event_send_stop();
2147 qapi_event_send_resume();
f056158d 2148 return -1;
2d76e823
CI
2149 }
2150
2151 /* We are sending this now, but the CPUs will be resumed shortly later */
3ab72385 2152 qapi_event_send_resume();
f056158d
MA
2153
2154 replay_enable_events();
2155 cpu_enable_ticks();
2156 runstate_set(RUN_STATE_RUNNING);
2157 vm_state_notify(1, RUN_STATE_RUNNING);
2158 return 0;
2d76e823
CI
2159}
2160
2161void vm_start(void)
2162{
2163 if (!vm_prepare_start()) {
2164 resume_all_vcpus();
2165 }
2166}
2167
8a9236f1
LC
2168/* does a state transition even if the VM is already stopped,
2169 current state is forgotten forever */
56983463 2170int vm_stop_force_state(RunState state)
8a9236f1
LC
2171{
2172 if (runstate_is_running()) {
56983463 2173 return vm_stop(state);
8a9236f1
LC
2174 } else {
2175 runstate_set(state);
b2780d32
WC
2176
2177 bdrv_drain_all();
594a45ce
KW
2178 /* Make sure to return an error if the flush in a previous vm_stop()
2179 * failed. */
22af08ea 2180 return bdrv_flush_all();
8a9236f1
LC
2181 }
2182}
2183
0442428a 2184void list_cpus(const char *optarg)
262353cb
BS
2185{
2186 /* XXX: implement xxx_cpu_list for targets that still miss it */
e916cbf8 2187#if defined(cpu_list)
0442428a 2188 cpu_list();
262353cb
BS
2189#endif
2190}
de0b36b6
LC
2191
2192CpuInfoList *qmp_query_cpus(Error **errp)
2193{
afed5a5a
IM
2194 MachineState *ms = MACHINE(qdev_get_machine());
2195 MachineClass *mc = MACHINE_GET_CLASS(ms);
de0b36b6 2196 CpuInfoList *head = NULL, *cur_item = NULL;
182735ef 2197 CPUState *cpu;
de0b36b6 2198
bdc44640 2199 CPU_FOREACH(cpu) {
de0b36b6 2200 CpuInfoList *info;
182735ef
AF
2201#if defined(TARGET_I386)
2202 X86CPU *x86_cpu = X86_CPU(cpu);
2203 CPUX86State *env = &x86_cpu->env;
2204#elif defined(TARGET_PPC)
2205 PowerPCCPU *ppc_cpu = POWERPC_CPU(cpu);
2206 CPUPPCState *env = &ppc_cpu->env;
2207#elif defined(TARGET_SPARC)
2208 SPARCCPU *sparc_cpu = SPARC_CPU(cpu);
2209 CPUSPARCState *env = &sparc_cpu->env;
25fa194b
MC
2210#elif defined(TARGET_RISCV)
2211 RISCVCPU *riscv_cpu = RISCV_CPU(cpu);
2212 CPURISCVState *env = &riscv_cpu->env;
182735ef
AF
2213#elif defined(TARGET_MIPS)
2214 MIPSCPU *mips_cpu = MIPS_CPU(cpu);
2215 CPUMIPSState *env = &mips_cpu->env;
48e06fe0
BK
2216#elif defined(TARGET_TRICORE)
2217 TriCoreCPU *tricore_cpu = TRICORE_CPU(cpu);
2218 CPUTriCoreState *env = &tricore_cpu->env;
9d0306df
VM
2219#elif defined(TARGET_S390X)
2220 S390CPU *s390_cpu = S390_CPU(cpu);
2221 CPUS390XState *env = &s390_cpu->env;
182735ef 2222#endif
de0b36b6 2223
cb446eca 2224 cpu_synchronize_state(cpu);
de0b36b6
LC
2225
2226 info = g_malloc0(sizeof(*info));
2227 info->value = g_malloc0(sizeof(*info->value));
55e5c285 2228 info->value->CPU = cpu->cpu_index;
182735ef 2229 info->value->current = (cpu == first_cpu);
259186a7 2230 info->value->halted = cpu->halted;
58f88d4b 2231 info->value->qom_path = object_get_canonical_path(OBJECT(cpu));
9f09e18a 2232 info->value->thread_id = cpu->thread_id;
de0b36b6 2233#if defined(TARGET_I386)
86f4b687 2234 info->value->arch = CPU_INFO_ARCH_X86;
544a3731 2235 info->value->u.x86.pc = env->eip + env->segs[R_CS].base;
de0b36b6 2236#elif defined(TARGET_PPC)
86f4b687 2237 info->value->arch = CPU_INFO_ARCH_PPC;
544a3731 2238 info->value->u.ppc.nip = env->nip;
de0b36b6 2239#elif defined(TARGET_SPARC)
86f4b687 2240 info->value->arch = CPU_INFO_ARCH_SPARC;
544a3731
EB
2241 info->value->u.q_sparc.pc = env->pc;
2242 info->value->u.q_sparc.npc = env->npc;
de0b36b6 2243#elif defined(TARGET_MIPS)
86f4b687 2244 info->value->arch = CPU_INFO_ARCH_MIPS;
544a3731 2245 info->value->u.q_mips.PC = env->active_tc.PC;
48e06fe0 2246#elif defined(TARGET_TRICORE)
86f4b687 2247 info->value->arch = CPU_INFO_ARCH_TRICORE;
544a3731 2248 info->value->u.tricore.PC = env->PC;
9d0306df
VM
2249#elif defined(TARGET_S390X)
2250 info->value->arch = CPU_INFO_ARCH_S390;
2251 info->value->u.s390.cpu_state = env->cpu_state;
25fa194b
MC
2252#elif defined(TARGET_RISCV)
2253 info->value->arch = CPU_INFO_ARCH_RISCV;
2254 info->value->u.riscv.pc = env->pc;
86f4b687
EB
2255#else
2256 info->value->arch = CPU_INFO_ARCH_OTHER;
de0b36b6 2257#endif
afed5a5a
IM
2258 info->value->has_props = !!mc->cpu_index_to_instance_props;
2259 if (info->value->has_props) {
2260 CpuInstanceProperties *props;
2261 props = g_malloc0(sizeof(*props));
2262 *props = mc->cpu_index_to_instance_props(ms, cpu->cpu_index);
2263 info->value->props = props;
2264 }
de0b36b6
LC
2265
2266 /* XXX: waiting for the qapi to support GSList */
2267 if (!cur_item) {
2268 head = cur_item = info;
2269 } else {
2270 cur_item->next = info;
2271 cur_item = info;
2272 }
2273 }
2274
2275 return head;
2276}
0cfd6a9a 2277
daa9d2bc
LE
2278static CpuInfoArch sysemu_target_to_cpuinfo_arch(SysEmuTarget target)
2279{
2280 /*
2281 * The @SysEmuTarget -> @CpuInfoArch mapping below is based on the
2282 * TARGET_ARCH -> TARGET_BASE_ARCH mapping in the "configure" script.
2283 */
2284 switch (target) {
2285 case SYS_EMU_TARGET_I386:
2286 case SYS_EMU_TARGET_X86_64:
2287 return CPU_INFO_ARCH_X86;
2288
2289 case SYS_EMU_TARGET_PPC:
daa9d2bc
LE
2290 case SYS_EMU_TARGET_PPC64:
2291 return CPU_INFO_ARCH_PPC;
2292
2293 case SYS_EMU_TARGET_SPARC:
2294 case SYS_EMU_TARGET_SPARC64:
2295 return CPU_INFO_ARCH_SPARC;
2296
2297 case SYS_EMU_TARGET_MIPS:
2298 case SYS_EMU_TARGET_MIPSEL:
2299 case SYS_EMU_TARGET_MIPS64:
2300 case SYS_EMU_TARGET_MIPS64EL:
2301 return CPU_INFO_ARCH_MIPS;
2302
2303 case SYS_EMU_TARGET_TRICORE:
2304 return CPU_INFO_ARCH_TRICORE;
2305
2306 case SYS_EMU_TARGET_S390X:
2307 return CPU_INFO_ARCH_S390;
2308
2309 case SYS_EMU_TARGET_RISCV32:
2310 case SYS_EMU_TARGET_RISCV64:
2311 return CPU_INFO_ARCH_RISCV;
2312
2313 default:
2314 return CPU_INFO_ARCH_OTHER;
2315 }
2316}
2317
2318static void cpustate_to_cpuinfo_s390(CpuInfoS390 *info, const CPUState *cpu)
2319{
2320#ifdef TARGET_S390X
2321 S390CPU *s390_cpu = S390_CPU(cpu);
2322 CPUS390XState *env = &s390_cpu->env;
2323
2324 info->cpu_state = env->cpu_state;
2325#else
2326 abort();
2327#endif
2328}
2329
ce74ee3d
LC
2330/*
2331 * fast means: we NEVER interrupt vCPU threads to retrieve
2332 * information from KVM.
2333 */
2334CpuInfoFastList *qmp_query_cpus_fast(Error **errp)
2335{
2336 MachineState *ms = MACHINE(qdev_get_machine());
2337 MachineClass *mc = MACHINE_GET_CLASS(ms);
2338 CpuInfoFastList *head = NULL, *cur_item = NULL;
daa9d2bc
LE
2339 SysEmuTarget target = qapi_enum_parse(&SysEmuTarget_lookup, TARGET_NAME,
2340 -1, &error_abort);
ce74ee3d
LC
2341 CPUState *cpu;
2342
2343 CPU_FOREACH(cpu) {
2344 CpuInfoFastList *info = g_malloc0(sizeof(*info));
2345 info->value = g_malloc0(sizeof(*info->value));
2346
2347 info->value->cpu_index = cpu->cpu_index;
2348 info->value->qom_path = object_get_canonical_path(OBJECT(cpu));
2349 info->value->thread_id = cpu->thread_id;
2350
2351 info->value->has_props = !!mc->cpu_index_to_instance_props;
2352 if (info->value->has_props) {
2353 CpuInstanceProperties *props;
2354 props = g_malloc0(sizeof(*props));
2355 *props = mc->cpu_index_to_instance_props(ms, cpu->cpu_index);
2356 info->value->props = props;
2357 }
2358
daa9d2bc
LE
2359 info->value->arch = sysemu_target_to_cpuinfo_arch(target);
2360 info->value->target = target;
2361 if (target == SYS_EMU_TARGET_S390X) {
2362 cpustate_to_cpuinfo_s390(&info->value->u.s390x, cpu);
daa9d2bc
LE
2363 }
2364
ce74ee3d
LC
2365 if (!cur_item) {
2366 head = cur_item = info;
2367 } else {
2368 cur_item->next = info;
2369 cur_item = info;
2370 }
2371 }
2372
2373 return head;
2374}
2375
0cfd6a9a
LC
2376void qmp_memsave(int64_t addr, int64_t size, const char *filename,
2377 bool has_cpu, int64_t cpu_index, Error **errp)
2378{
2379 FILE *f;
2380 uint32_t l;
55e5c285 2381 CPUState *cpu;
0cfd6a9a 2382 uint8_t buf[1024];
0dc9daf0 2383 int64_t orig_addr = addr, orig_size = size;
0cfd6a9a
LC
2384
2385 if (!has_cpu) {
2386 cpu_index = 0;
2387 }
2388
151d1322
AF
2389 cpu = qemu_get_cpu(cpu_index);
2390 if (cpu == NULL) {
c6bd8c70
MA
2391 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "cpu-index",
2392 "a CPU number");
0cfd6a9a
LC
2393 return;
2394 }
2395
2396 f = fopen(filename, "wb");
2397 if (!f) {
618da851 2398 error_setg_file_open(errp, errno, filename);
0cfd6a9a
LC
2399 return;
2400 }
2401
2402 while (size != 0) {
2403 l = sizeof(buf);
2404 if (l > size)
2405 l = size;
2f4d0f59 2406 if (cpu_memory_rw_debug(cpu, addr, buf, l, 0) != 0) {
0dc9daf0
BP
2407 error_setg(errp, "Invalid addr 0x%016" PRIx64 "/size %" PRId64
2408 " specified", orig_addr, orig_size);
2f4d0f59
AK
2409 goto exit;
2410 }
0cfd6a9a 2411 if (fwrite(buf, 1, l, f) != l) {
c6bd8c70 2412 error_setg(errp, QERR_IO_ERROR);
0cfd6a9a
LC
2413 goto exit;
2414 }
2415 addr += l;
2416 size -= l;
2417 }
2418
2419exit:
2420 fclose(f);
2421}
6d3962bf
LC
2422
2423void qmp_pmemsave(int64_t addr, int64_t size, const char *filename,
2424 Error **errp)
2425{
2426 FILE *f;
2427 uint32_t l;
2428 uint8_t buf[1024];
2429
2430 f = fopen(filename, "wb");
2431 if (!f) {
618da851 2432 error_setg_file_open(errp, errno, filename);
6d3962bf
LC
2433 return;
2434 }
2435
2436 while (size != 0) {
2437 l = sizeof(buf);
2438 if (l > size)
2439 l = size;
eb6282f2 2440 cpu_physical_memory_read(addr, buf, l);
6d3962bf 2441 if (fwrite(buf, 1, l, f) != l) {
c6bd8c70 2442 error_setg(errp, QERR_IO_ERROR);
6d3962bf
LC
2443 goto exit;
2444 }
2445 addr += l;
2446 size -= l;
2447 }
2448
2449exit:
2450 fclose(f);
2451}
ab49ab5c
LC
2452
2453void qmp_inject_nmi(Error **errp)
2454{
9cb805fd 2455 nmi_monitor_handle(monitor_get_cpu_index(), errp);
ab49ab5c 2456}
27498bef 2457
76c86615 2458void dump_drift_info(void)
27498bef
ST
2459{
2460 if (!use_icount) {
2461 return;
2462 }
2463
76c86615 2464 qemu_printf("Host - Guest clock %"PRIi64" ms\n",
27498bef
ST
2465 (cpu_get_clock() - cpu_get_icount())/SCALE_MS);
2466 if (icount_align_option) {
76c86615
MA
2467 qemu_printf("Max guest delay %"PRIi64" ms\n",
2468 -max_delay / SCALE_MS);
2469 qemu_printf("Max guest advance %"PRIi64" ms\n",
2470 max_advance / SCALE_MS);
27498bef 2471 } else {
76c86615
MA
2472 qemu_printf("Max guest delay NA\n");
2473 qemu_printf("Max guest advance NA\n");
27498bef
ST
2474 }
2475}