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