blob: dde3b7b9813c2882b0ade13c7d4c1ad2a31c4b6b [file] [log] [blame]
Blue Swirl296af7c2010-03-29 19:23:50 +00001/*
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
Peter Maydell7b31bbc2016-01-26 18:16:56 +000025#include "qemu/osdep.h"
Markus Armbrustera8d25322019-05-23 16:35:08 +020026#include "qemu-common.h"
KONRAD Frederic8d4e9142017-02-23 18:29:08 +000027#include "qemu/config-file.h"
Paolo Bonzini33c11872016-03-15 16:58:45 +010028#include "cpu.h"
Paolo Bonzini83c90892012-12-17 18:19:49 +010029#include "monitor/monitor.h"
Markus Armbrustere688df62018-02-01 12:18:31 +010030#include "qapi/error.h"
Markus Armbruster112ed242018-02-26 17:13:27 -060031#include "qapi/qapi-commands-misc.h"
Markus Armbruster9af23982018-02-11 10:36:01 +010032#include "qapi/qapi-events-run-state.h"
Wenchao Xiaa4e15de2014-06-18 08:43:36 +020033#include "qapi/qmp/qerror.h"
Markus Armbrusterd49b6832015-03-17 18:29:20 +010034#include "qemu/error-report.h"
Markus Armbruster76c86612019-04-17 21:17:53 +020035#include "qemu/qemu-print.h"
Paolo Bonzini9c17d612012-12-17 18:20:04 +010036#include "sysemu/sysemu.h"
Markus Armbruster14a48c12019-05-23 16:35:05 +020037#include "sysemu/tcg.h"
Max Reitzda31d592016-03-16 19:54:32 +010038#include "sysemu/block-backend.h"
Paolo Bonzini022c62c2012-12-17 18:19:49 +010039#include "exec/gdbstub.h"
Paolo Bonzini9c17d612012-12-17 18:20:04 +010040#include "sysemu/dma.h"
Vincent Palatinb3946622017-01-10 11:59:55 +010041#include "sysemu/hw_accel.h"
Paolo Bonzini9c17d612012-12-17 18:20:04 +010042#include "sysemu/kvm.h"
Vincent Palatinb0cb0a62017-01-10 11:59:57 +010043#include "sysemu/hax.h"
Sergio Andres Gomez Del Realc97d6d22017-09-13 04:05:09 -050044#include "sysemu/hvf.h"
Justin Terry (VM)19306802018-01-22 13:07:49 -080045#include "sysemu/whpx.h"
Paolo Bonzini63c91552016-03-15 13:18:37 +010046#include "exec/exec-all.h"
Blue Swirl296af7c2010-03-29 19:23:50 +000047
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010048#include "qemu/thread.h"
Paolo Bonzini9c17d612012-12-17 18:20:04 +010049#include "sysemu/cpus.h"
50#include "sysemu/qtest.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010051#include "qemu/main-loop.h"
Markus Armbruster922a01a2018-02-01 12:18:46 +010052#include "qemu/option.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010053#include "qemu/bitmap.h"
Liu Ping Fancb365642013-09-25 14:20:58 +080054#include "qemu/seqlock.h"
Richard Henderson9c09a252019-03-14 13:06:29 -070055#include "qemu/guest-random.h"
KONRAD Frederic8d4e9142017-02-23 18:29:08 +000056#include "tcg.h"
Alexey Kardashevskiy9cb805f2014-08-20 22:16:33 +100057#include "hw/nmi.h"
Pavel Dovgalyuk8b427042015-09-17 19:24:05 +030058#include "sysemu/replay.h"
Igor Mammedovafed5a52017-05-10 13:29:55 +020059#include "hw/boards.h"
Jan Kiszka0ff0fc12011-06-23 10:15:55 +020060
Jan Kiszka6d9cb732011-02-01 22:15:58 +010061#ifdef CONFIG_LINUX
62
63#include <sys/prctl.h>
64
Marcelo Tosattic0532a72010-10-11 15:31:21 -030065#ifndef PR_MCE_KILL
66#define PR_MCE_KILL 33
67#endif
68
Jan Kiszka6d9cb732011-02-01 22:15:58 +010069#ifndef PR_MCE_KILL_SET
70#define PR_MCE_KILL_SET 1
71#endif
72
73#ifndef PR_MCE_KILL_EARLY
74#define PR_MCE_KILL_EARLY 1
75#endif
76
77#endif /* CONFIG_LINUX */
78
Sebastian Tanase27498be2014-07-25 11:56:33 +020079int64_t max_delay;
80int64_t max_advance;
Blue Swirl296af7c2010-03-29 19:23:50 +000081
Jason J. Herne2adcc852015-09-08 13:12:33 -040082/* vcpu throttling controls */
83static QEMUTimer *throttle_timer;
84static unsigned int throttle_percentage;
85
86#define CPU_THROTTLE_PCT_MIN 1
87#define CPU_THROTTLE_PCT_MAX 99
88#define CPU_THROTTLE_TIMESLICE_NS 10000000
89
Tiejun Chen321bc0b2013-08-02 09:43:09 +080090bool cpu_is_stopped(CPUState *cpu)
91{
92 return cpu->stopped || !runstate_is_running();
93}
94
Andreas Färbera98ae1d2013-05-26 23:21:08 +020095static bool cpu_thread_is_idle(CPUState *cpu)
Peter Maydellac873f12012-07-19 16:52:27 +010096{
Andreas Färberc64ca812012-05-03 02:11:45 +020097 if (cpu->stop || cpu->queued_work_first) {
Peter Maydellac873f12012-07-19 16:52:27 +010098 return false;
99 }
Tiejun Chen321bc0b2013-08-02 09:43:09 +0800100 if (cpu_is_stopped(cpu)) {
Peter Maydellac873f12012-07-19 16:52:27 +0100101 return true;
102 }
Andreas Färber8c2e1b02013-08-25 18:53:55 +0200103 if (!cpu->halted || cpu_has_work(cpu) ||
Alexander Graf215e79c2013-04-24 22:24:12 +0200104 kvm_halt_in_kernel()) {
Peter Maydellac873f12012-07-19 16:52:27 +0100105 return false;
106 }
107 return true;
108}
109
110static bool all_cpu_threads_idle(void)
111{
Andreas Färber182735e2013-05-29 22:29:20 +0200112 CPUState *cpu;
Peter Maydellac873f12012-07-19 16:52:27 +0100113
Andreas Färberbdc44642013-06-24 23:50:24 +0200114 CPU_FOREACH(cpu) {
Andreas Färber182735e2013-05-29 22:29:20 +0200115 if (!cpu_thread_is_idle(cpu)) {
Peter Maydellac873f12012-07-19 16:52:27 +0100116 return false;
117 }
118 }
119 return true;
120}
121
Blue Swirl296af7c2010-03-29 19:23:50 +0000122/***********************************************************/
Paolo Bonzini946fb272011-09-12 13:57:37 +0200123/* guest cycle counter */
124
Paolo Bonzinia3270e12013-10-07 17:18:15 +0200125/* Protected by TimersState seqlock */
126
Victor CLEMENT5045e9d92015-05-29 17:14:04 +0200127static bool icount_sleep = true;
Paolo Bonzini946fb272011-09-12 13:57:37 +0200128/* Arbitrarily pick 1MIPS as the minimum allowable speed. */
129#define MAX_ICOUNT_SHIFT 10
Paolo Bonzinia3270e12013-10-07 17:18:15 +0200130
Paolo Bonzini946fb272011-09-12 13:57:37 +0200131typedef struct TimersState {
Liu Ping Fancb365642013-09-25 14:20:58 +0800132 /* Protected by BQL. */
Paolo Bonzini946fb272011-09-12 13:57:37 +0200133 int64_t cpu_ticks_prev;
134 int64_t cpu_ticks_offset;
Liu Ping Fancb365642013-09-25 14:20:58 +0800135
Paolo Bonzini94377112018-08-14 09:57:16 +0200136 /* Protect fields that can be respectively read outside the
137 * BQL, and written from multiple threads.
Liu Ping Fancb365642013-09-25 14:20:58 +0800138 */
139 QemuSeqLock vm_clock_seqlock;
Paolo Bonzini94377112018-08-14 09:57:16 +0200140 QemuSpin vm_clock_lock;
141
142 int16_t cpu_ticks_enabled;
KONRAD Fredericc96778b2014-08-01 01:37:09 +0200143
Paolo Bonzinic1ff0732018-08-14 09:31:58 +0200144 /* Conversion factor from emulated instructions to virtual clock ticks. */
Paolo Bonzini94377112018-08-14 09:57:16 +0200145 int16_t icount_time_shift;
146
KONRAD Fredericc96778b2014-08-01 01:37:09 +0200147 /* Compensate for varying guest execution speed. */
148 int64_t qemu_icount_bias;
Paolo Bonzini94377112018-08-14 09:57:16 +0200149
150 int64_t vm_clock_warp_start;
151 int64_t cpu_clock_offset;
152
KONRAD Fredericc96778b2014-08-01 01:37:09 +0200153 /* Only written by TCG thread */
154 int64_t qemu_icount;
Paolo Bonzini94377112018-08-14 09:57:16 +0200155
Pavel Dovgalyukb39e3f32018-01-11 11:26:10 +0300156 /* for adjusting icount */
Pavel Dovgalyukb39e3f32018-01-11 11:26:10 +0300157 QEMUTimer *icount_rt_timer;
158 QEMUTimer *icount_vm_timer;
159 QEMUTimer *icount_warp_timer;
Paolo Bonzini946fb272011-09-12 13:57:37 +0200160} TimersState;
161
Liu Ping Fand9cd4002013-07-21 08:43:00 +0000162static TimersState timers_state;
KONRAD Frederic8d4e9142017-02-23 18:29:08 +0000163bool mttcg_enabled;
164
165/*
166 * We default to false if we know other options have been enabled
167 * which are currently incompatible with MTTCG. Otherwise when each
168 * guest (target) has been updated to support:
169 * - atomic instructions
170 * - memory ordering primitives (barriers)
171 * they can set the appropriate CONFIG flags in ${target}-softmmu.mak
172 *
173 * Once a guest architecture has been converted to the new primitives
174 * there are two remaining limitations to check.
175 *
176 * - The guest can't be oversized (e.g. 64 bit guest on 32 bit host)
177 * - The host must have a stronger memory order than the guest
178 *
179 * It may be possible in future to support strong guests on weak hosts
180 * but that will require tagging all load/stores in a guest with their
181 * implicit memory order requirements which would likely slow things
182 * down a lot.
183 */
184
185static bool check_tcg_memory_orders_compatible(void)
186{
187#if defined(TCG_GUEST_DEFAULT_MO) && defined(TCG_TARGET_DEFAULT_MO)
188 return (TCG_GUEST_DEFAULT_MO & ~TCG_TARGET_DEFAULT_MO) == 0;
189#else
190 return false;
191#endif
192}
193
194static bool default_mttcg_enabled(void)
195{
Alex Bennée83fd9622017-02-27 17:09:01 +0000196 if (use_icount || TCG_OVERSIZED_GUEST) {
KONRAD Frederic8d4e9142017-02-23 18:29:08 +0000197 return false;
198 } else {
199#ifdef TARGET_SUPPORTS_MTTCG
200 return check_tcg_memory_orders_compatible();
201#else
202 return false;
203#endif
204 }
205}
206
207void qemu_tcg_configure(QemuOpts *opts, Error **errp)
208{
209 const char *t = qemu_opt_get(opts, "thread");
210 if (t) {
211 if (strcmp(t, "multi") == 0) {
212 if (TCG_OVERSIZED_GUEST) {
213 error_setg(errp, "No MTTCG when guest word size > hosts");
Alex Bennée83fd9622017-02-27 17:09:01 +0000214 } else if (use_icount) {
215 error_setg(errp, "No MTTCG when icount is enabled");
KONRAD Frederic8d4e9142017-02-23 18:29:08 +0000216 } else {
Nikunj A Dadhania86953502017-04-10 11:36:55 +0530217#ifndef TARGET_SUPPORTS_MTTCG
Markus Armbruster07656912018-10-17 10:26:28 +0200218 warn_report("Guest not yet converted to MTTCG - "
219 "you may get unexpected results");
Alex Bennéec34c7622017-02-28 14:40:17 +0000220#endif
KONRAD Frederic8d4e9142017-02-23 18:29:08 +0000221 if (!check_tcg_memory_orders_compatible()) {
Markus Armbruster07656912018-10-17 10:26:28 +0200222 warn_report("Guest expects a stronger memory ordering "
223 "than the host provides");
Pranith Kumar8cfef892017-03-25 16:19:23 -0400224 error_printf("This may cause strange/hard to debug errors\n");
KONRAD Frederic8d4e9142017-02-23 18:29:08 +0000225 }
226 mttcg_enabled = true;
227 }
228 } else if (strcmp(t, "single") == 0) {
229 mttcg_enabled = false;
230 } else {
231 error_setg(errp, "Invalid 'thread' setting %s", t);
232 }
233 } else {
234 mttcg_enabled = default_mttcg_enabled();
235 }
236}
Paolo Bonzini946fb272011-09-12 13:57:37 +0200237
Alex Bennéee4cd9652017-03-31 16:09:42 +0100238/* The current number of executed instructions is based on what we
239 * originally budgeted minus the current state of the decrementing
240 * icount counters in extra/u16.low.
241 */
242static int64_t cpu_get_icount_executed(CPUState *cpu)
243{
Richard Henderson5e140192019-03-28 11:54:23 -1000244 return (cpu->icount_budget -
245 (cpu_neg(cpu)->icount_decr.u16.low + cpu->icount_extra));
Alex Bennéee4cd9652017-03-31 16:09:42 +0100246}
247
Alex Bennée512d3c82017-04-05 12:32:37 +0100248/*
249 * Update the global shared timer_state.qemu_icount to take into
250 * account executed instructions. This is done by the TCG vCPU
251 * thread so the main-loop can see time has moved forward.
252 */
Paolo Bonzini9b4e6f42018-09-11 13:15:32 +0200253static void cpu_update_icount_locked(CPUState *cpu)
Alex Bennée512d3c82017-04-05 12:32:37 +0100254{
255 int64_t executed = cpu_get_icount_executed(cpu);
256 cpu->icount_budget -= executed;
257
Emilio G. Cota38adcb62018-09-10 19:27:49 -0400258 atomic_set_i64(&timers_state.qemu_icount,
259 timers_state.qemu_icount + executed);
Paolo Bonzini9b4e6f42018-09-11 13:15:32 +0200260}
261
262/*
263 * Update the global shared timer_state.qemu_icount to take into
264 * account executed instructions. This is done by the TCG vCPU
265 * thread so the main-loop can see time has moved forward.
266 */
267void cpu_update_icount(CPUState *cpu)
268{
269 seqlock_write_lock(&timers_state.vm_clock_seqlock,
270 &timers_state.vm_clock_lock);
271 cpu_update_icount_locked(cpu);
Paolo Bonzini94377112018-08-14 09:57:16 +0200272 seqlock_write_unlock(&timers_state.vm_clock_seqlock,
273 &timers_state.vm_clock_lock);
Alex Bennée512d3c82017-04-05 12:32:37 +0100274}
275
Paolo Bonzinic1ff0732018-08-14 09:31:58 +0200276static int64_t cpu_get_icount_raw_locked(void)
Paolo Bonzini946fb272011-09-12 13:57:37 +0200277{
Andreas Färber4917cf42013-05-27 05:17:50 +0200278 CPUState *cpu = current_cpu;
Paolo Bonzini946fb272011-09-12 13:57:37 +0200279
Alex Bennée243c5f72017-03-30 18:49:22 +0100280 if (cpu && cpu->running) {
Paolo Bonzini414b15c2015-06-24 14:16:26 +0200281 if (!cpu->can_do_io) {
Alistair Francis493d89b2018-02-03 09:43:14 +0100282 error_report("Bad icount read");
Pavel Dovgalyuk2a629142014-12-08 10:53:45 +0300283 exit(1);
Paolo Bonzini946fb272011-09-12 13:57:37 +0200284 }
Alex Bennéee4cd9652017-03-31 16:09:42 +0100285 /* Take into account what has run */
Paolo Bonzini9b4e6f42018-09-11 13:15:32 +0200286 cpu_update_icount_locked(cpu);
Paolo Bonzini946fb272011-09-12 13:57:37 +0200287 }
Emilio G. Cota38adcb62018-09-10 19:27:49 -0400288 /* The read is protected by the seqlock, but needs atomic64 to avoid UB */
289 return atomic_read_i64(&timers_state.qemu_icount);
Paolo Bonzinic1ff0732018-08-14 09:31:58 +0200290}
291
292static int64_t cpu_get_icount_locked(void)
293{
294 int64_t icount = cpu_get_icount_raw_locked();
Emilio G. Cotac97595d2018-09-10 19:27:50 -0400295 return atomic_read_i64(&timers_state.qemu_icount_bias) +
296 cpu_icount_to_ns(icount);
Paolo Bonzinic1ff0732018-08-14 09:31:58 +0200297}
298
299int64_t cpu_get_icount_raw(void)
300{
301 int64_t icount;
302 unsigned start;
303
304 do {
305 start = seqlock_read_begin(&timers_state.vm_clock_seqlock);
306 icount = cpu_get_icount_raw_locked();
307 } while (seqlock_read_retry(&timers_state.vm_clock_seqlock, start));
308
309 return icount;
Pavel Dovgalyuk2a629142014-12-08 10:53:45 +0300310}
311
312/* Return the virtual CPU time, based on the instruction counter. */
Paolo Bonzini17a15f12013-10-03 15:17:25 +0200313int64_t cpu_get_icount(void)
314{
315 int64_t icount;
316 unsigned start;
317
318 do {
319 start = seqlock_read_begin(&timers_state.vm_clock_seqlock);
320 icount = cpu_get_icount_locked();
321 } while (seqlock_read_retry(&timers_state.vm_clock_seqlock, start));
322
323 return icount;
324}
325
KONRAD Frederic3f031312014-08-01 01:37:15 +0200326int64_t cpu_icount_to_ns(int64_t icount)
327{
Paolo Bonzinic1ff0732018-08-14 09:31:58 +0200328 return icount << atomic_read(&timers_state.icount_time_shift);
KONRAD Frederic3f031312014-08-01 01:37:15 +0200329}
330
Paolo Bonzinif2a4ad62018-08-18 09:36:16 +0200331static int64_t cpu_get_ticks_locked(void)
332{
333 int64_t ticks = timers_state.cpu_ticks_offset;
334 if (timers_state.cpu_ticks_enabled) {
335 ticks += cpu_get_host_ticks();
336 }
337
338 if (timers_state.cpu_ticks_prev > ticks) {
339 /* Non increasing ticks may happen if the host uses software suspend. */
340 timers_state.cpu_ticks_offset += timers_state.cpu_ticks_prev - ticks;
341 ticks = timers_state.cpu_ticks_prev;
342 }
343
344 timers_state.cpu_ticks_prev = ticks;
345 return ticks;
346}
347
Cao jind90f3cc2016-07-29 19:05:38 +0800348/* return the time elapsed in VM between vm_start and vm_stop. Unless
349 * icount is active, cpu_get_ticks() uses units of the host CPU cycle
350 * counter.
Cao jind90f3cc2016-07-29 19:05:38 +0800351 */
Paolo Bonzini946fb272011-09-12 13:57:37 +0200352int64_t cpu_get_ticks(void)
353{
Paolo Bonzini5f3e3102013-10-28 17:32:18 +0100354 int64_t ticks;
355
Paolo Bonzini946fb272011-09-12 13:57:37 +0200356 if (use_icount) {
357 return cpu_get_icount();
358 }
Paolo Bonzini5f3e3102013-10-28 17:32:18 +0100359
Paolo Bonzinif2a4ad62018-08-18 09:36:16 +0200360 qemu_spin_lock(&timers_state.vm_clock_lock);
361 ticks = cpu_get_ticks_locked();
362 qemu_spin_unlock(&timers_state.vm_clock_lock);
Paolo Bonzini5f3e3102013-10-28 17:32:18 +0100363 return ticks;
Paolo Bonzini946fb272011-09-12 13:57:37 +0200364}
365
Liu Ping Fancb365642013-09-25 14:20:58 +0800366static int64_t cpu_get_clock_locked(void)
367{
Cao jin1d45cea2016-07-29 19:05:37 +0800368 int64_t time;
Liu Ping Fancb365642013-09-25 14:20:58 +0800369
Cao jin1d45cea2016-07-29 19:05:37 +0800370 time = timers_state.cpu_clock_offset;
Paolo Bonzini5f3e3102013-10-28 17:32:18 +0100371 if (timers_state.cpu_ticks_enabled) {
Cao jin1d45cea2016-07-29 19:05:37 +0800372 time += get_clock();
Liu Ping Fancb365642013-09-25 14:20:58 +0800373 }
374
Cao jin1d45cea2016-07-29 19:05:37 +0800375 return time;
Liu Ping Fancb365642013-09-25 14:20:58 +0800376}
377
Cao jind90f3cc2016-07-29 19:05:38 +0800378/* Return the monotonic time elapsed in VM, i.e.,
Peter Maydell8212ff82016-09-15 10:24:22 +0100379 * the time between vm_start and vm_stop
380 */
Paolo Bonzini946fb272011-09-12 13:57:37 +0200381int64_t cpu_get_clock(void)
382{
383 int64_t ti;
Liu Ping Fancb365642013-09-25 14:20:58 +0800384 unsigned start;
385
386 do {
387 start = seqlock_read_begin(&timers_state.vm_clock_seqlock);
388 ti = cpu_get_clock_locked();
389 } while (seqlock_read_retry(&timers_state.vm_clock_seqlock, start));
390
391 return ti;
Paolo Bonzini946fb272011-09-12 13:57:37 +0200392}
393
Liu Ping Fancb365642013-09-25 14:20:58 +0800394/* enable cpu_get_ticks()
Cao jin3224e872016-07-08 18:31:37 +0800395 * Caller must hold BQL which serves as mutex for vm_clock_seqlock.
Liu Ping Fancb365642013-09-25 14:20:58 +0800396 */
Paolo Bonzini946fb272011-09-12 13:57:37 +0200397void cpu_enable_ticks(void)
398{
Paolo Bonzini94377112018-08-14 09:57:16 +0200399 seqlock_write_lock(&timers_state.vm_clock_seqlock,
400 &timers_state.vm_clock_lock);
Paolo Bonzini946fb272011-09-12 13:57:37 +0200401 if (!timers_state.cpu_ticks_enabled) {
Christopher Covington4a7428c2015-09-25 10:42:21 -0400402 timers_state.cpu_ticks_offset -= cpu_get_host_ticks();
Paolo Bonzini946fb272011-09-12 13:57:37 +0200403 timers_state.cpu_clock_offset -= get_clock();
404 timers_state.cpu_ticks_enabled = 1;
405 }
Paolo Bonzini94377112018-08-14 09:57:16 +0200406 seqlock_write_unlock(&timers_state.vm_clock_seqlock,
407 &timers_state.vm_clock_lock);
Paolo Bonzini946fb272011-09-12 13:57:37 +0200408}
409
410/* disable cpu_get_ticks() : the clock is stopped. You must not call
Liu Ping Fancb365642013-09-25 14:20:58 +0800411 * cpu_get_ticks() after that.
Cao jin3224e872016-07-08 18:31:37 +0800412 * Caller must hold BQL which serves as mutex for vm_clock_seqlock.
Liu Ping Fancb365642013-09-25 14:20:58 +0800413 */
Paolo Bonzini946fb272011-09-12 13:57:37 +0200414void cpu_disable_ticks(void)
415{
Paolo Bonzini94377112018-08-14 09:57:16 +0200416 seqlock_write_lock(&timers_state.vm_clock_seqlock,
417 &timers_state.vm_clock_lock);
Paolo Bonzini946fb272011-09-12 13:57:37 +0200418 if (timers_state.cpu_ticks_enabled) {
Christopher Covington4a7428c2015-09-25 10:42:21 -0400419 timers_state.cpu_ticks_offset += cpu_get_host_ticks();
Liu Ping Fancb365642013-09-25 14:20:58 +0800420 timers_state.cpu_clock_offset = cpu_get_clock_locked();
Paolo Bonzini946fb272011-09-12 13:57:37 +0200421 timers_state.cpu_ticks_enabled = 0;
422 }
Paolo Bonzini94377112018-08-14 09:57:16 +0200423 seqlock_write_unlock(&timers_state.vm_clock_seqlock,
424 &timers_state.vm_clock_lock);
Paolo Bonzini946fb272011-09-12 13:57:37 +0200425}
426
427/* Correlation between real and virtual time is always going to be
428 fairly approximate, so ignore small variation.
429 When the guest is idle real and virtual time will be aligned in
430 the IO wait loop. */
Rutuja Shah73bcb242016-03-21 21:32:30 +0530431#define ICOUNT_WOBBLE (NANOSECONDS_PER_SECOND / 10)
Paolo Bonzini946fb272011-09-12 13:57:37 +0200432
433static void icount_adjust(void)
434{
435 int64_t cur_time;
436 int64_t cur_icount;
437 int64_t delta;
Paolo Bonzinia3270e12013-10-07 17:18:15 +0200438
439 /* Protected by TimersState mutex. */
Paolo Bonzini946fb272011-09-12 13:57:37 +0200440 static int64_t last_delta;
Paolo Bonzini468cc7c2013-10-07 17:21:51 +0200441
Paolo Bonzini946fb272011-09-12 13:57:37 +0200442 /* If the VM is not running, then do nothing. */
443 if (!runstate_is_running()) {
444 return;
445 }
Paolo Bonzini468cc7c2013-10-07 17:21:51 +0200446
Paolo Bonzini94377112018-08-14 09:57:16 +0200447 seqlock_write_lock(&timers_state.vm_clock_seqlock,
448 &timers_state.vm_clock_lock);
Paolo Bonzini17a15f12013-10-03 15:17:25 +0200449 cur_time = cpu_get_clock_locked();
450 cur_icount = cpu_get_icount_locked();
Paolo Bonzini468cc7c2013-10-07 17:21:51 +0200451
Paolo Bonzini946fb272011-09-12 13:57:37 +0200452 delta = cur_icount - cur_time;
453 /* FIXME: This is a very crude algorithm, somewhat prone to oscillation. */
454 if (delta > 0
455 && last_delta + ICOUNT_WOBBLE < delta * 2
Paolo Bonzinic1ff0732018-08-14 09:31:58 +0200456 && timers_state.icount_time_shift > 0) {
Paolo Bonzini946fb272011-09-12 13:57:37 +0200457 /* The guest is getting too far ahead. Slow time down. */
Paolo Bonzinic1ff0732018-08-14 09:31:58 +0200458 atomic_set(&timers_state.icount_time_shift,
459 timers_state.icount_time_shift - 1);
Paolo Bonzini946fb272011-09-12 13:57:37 +0200460 }
461 if (delta < 0
462 && last_delta - ICOUNT_WOBBLE > delta * 2
Paolo Bonzinic1ff0732018-08-14 09:31:58 +0200463 && timers_state.icount_time_shift < MAX_ICOUNT_SHIFT) {
Paolo Bonzini946fb272011-09-12 13:57:37 +0200464 /* The guest is getting too far behind. Speed time up. */
Paolo Bonzinic1ff0732018-08-14 09:31:58 +0200465 atomic_set(&timers_state.icount_time_shift,
466 timers_state.icount_time_shift + 1);
Paolo Bonzini946fb272011-09-12 13:57:37 +0200467 }
468 last_delta = delta;
Emilio G. Cotac97595d2018-09-10 19:27:50 -0400469 atomic_set_i64(&timers_state.qemu_icount_bias,
470 cur_icount - (timers_state.qemu_icount
471 << timers_state.icount_time_shift));
Paolo Bonzini94377112018-08-14 09:57:16 +0200472 seqlock_write_unlock(&timers_state.vm_clock_seqlock,
473 &timers_state.vm_clock_lock);
Paolo Bonzini946fb272011-09-12 13:57:37 +0200474}
475
476static void icount_adjust_rt(void *opaque)
477{
Pavel Dovgalyukb39e3f32018-01-11 11:26:10 +0300478 timer_mod(timers_state.icount_rt_timer,
Pavel Dovgalyuk1979b902015-01-12 15:00:43 +0300479 qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL_RT) + 1000);
Paolo Bonzini946fb272011-09-12 13:57:37 +0200480 icount_adjust();
481}
482
483static void icount_adjust_vm(void *opaque)
484{
Pavel Dovgalyukb39e3f32018-01-11 11:26:10 +0300485 timer_mod(timers_state.icount_vm_timer,
Alex Bligh40daca52013-08-21 16:03:02 +0100486 qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) +
Rutuja Shah73bcb242016-03-21 21:32:30 +0530487 NANOSECONDS_PER_SECOND / 10);
Paolo Bonzini946fb272011-09-12 13:57:37 +0200488 icount_adjust();
489}
490
491static int64_t qemu_icount_round(int64_t count)
492{
Paolo Bonzinic1ff0732018-08-14 09:31:58 +0200493 int shift = atomic_read(&timers_state.icount_time_shift);
494 return (count + (1 << shift) - 1) >> shift;
Paolo Bonzini946fb272011-09-12 13:57:37 +0200495}
496
Pavel Dovgalyukefab87c2015-09-17 19:24:39 +0300497static void icount_warp_rt(void)
Paolo Bonzini946fb272011-09-12 13:57:37 +0200498{
Alex Bennéeccffff42016-04-04 15:35:48 +0100499 unsigned seq;
500 int64_t warp_start;
501
Paolo Bonzini17a15f12013-10-03 15:17:25 +0200502 /* The icount_warp_timer is rescheduled soon after vm_clock_warp_start
503 * changes from -1 to another value, so the race here is okay.
504 */
Alex Bennéeccffff42016-04-04 15:35:48 +0100505 do {
506 seq = seqlock_read_begin(&timers_state.vm_clock_seqlock);
Pavel Dovgalyukb39e3f32018-01-11 11:26:10 +0300507 warp_start = timers_state.vm_clock_warp_start;
Alex Bennéeccffff42016-04-04 15:35:48 +0100508 } while (seqlock_read_retry(&timers_state.vm_clock_seqlock, seq));
509
510 if (warp_start == -1) {
Paolo Bonzini946fb272011-09-12 13:57:37 +0200511 return;
512 }
513
Paolo Bonzini94377112018-08-14 09:57:16 +0200514 seqlock_write_lock(&timers_state.vm_clock_seqlock,
515 &timers_state.vm_clock_lock);
Paolo Bonzini946fb272011-09-12 13:57:37 +0200516 if (runstate_is_running()) {
Paolo Bonzini74c0b812018-10-08 13:24:14 +0200517 int64_t clock = REPLAY_CLOCK_LOCKED(REPLAY_CLOCK_VIRTUAL_RT,
518 cpu_get_clock_locked());
Paolo Bonzini8ed961d2013-10-07 17:26:07 +0200519 int64_t warp_delta;
520
Pavel Dovgalyukb39e3f32018-01-11 11:26:10 +0300521 warp_delta = clock - timers_state.vm_clock_warp_start;
Paolo Bonzini8ed961d2013-10-07 17:26:07 +0200522 if (use_icount == 2) {
Paolo Bonzini946fb272011-09-12 13:57:37 +0200523 /*
Alex Bligh40daca52013-08-21 16:03:02 +0100524 * In adaptive mode, do not let QEMU_CLOCK_VIRTUAL run too
Paolo Bonzini946fb272011-09-12 13:57:37 +0200525 * far ahead of real time.
526 */
Paolo Bonzini17a15f12013-10-03 15:17:25 +0200527 int64_t cur_icount = cpu_get_icount_locked();
Pavel Dovgalyukbf2a7dd2014-11-26 13:40:55 +0300528 int64_t delta = clock - cur_icount;
Paolo Bonzini8ed961d2013-10-07 17:26:07 +0200529 warp_delta = MIN(warp_delta, delta);
Paolo Bonzini946fb272011-09-12 13:57:37 +0200530 }
Emilio G. Cotac97595d2018-09-10 19:27:50 -0400531 atomic_set_i64(&timers_state.qemu_icount_bias,
532 timers_state.qemu_icount_bias + warp_delta);
Paolo Bonzini946fb272011-09-12 13:57:37 +0200533 }
Pavel Dovgalyukb39e3f32018-01-11 11:26:10 +0300534 timers_state.vm_clock_warp_start = -1;
Paolo Bonzini94377112018-08-14 09:57:16 +0200535 seqlock_write_unlock(&timers_state.vm_clock_seqlock,
536 &timers_state.vm_clock_lock);
Paolo Bonzini8ed961d2013-10-07 17:26:07 +0200537
538 if (qemu_clock_expired(QEMU_CLOCK_VIRTUAL)) {
539 qemu_clock_notify(QEMU_CLOCK_VIRTUAL);
540 }
Paolo Bonzini946fb272011-09-12 13:57:37 +0200541}
542
Pavel Dovgalyuke76d1792016-03-10 14:56:09 +0300543static void icount_timer_cb(void *opaque)
Pavel Dovgalyukefab87c2015-09-17 19:24:39 +0300544{
Pavel Dovgalyuke76d1792016-03-10 14:56:09 +0300545 /* No need for a checkpoint because the timer already synchronizes
546 * with CHECKPOINT_CLOCK_VIRTUAL_RT.
547 */
548 icount_warp_rt();
Pavel Dovgalyukefab87c2015-09-17 19:24:39 +0300549}
550
Paolo Bonzini8156be52012-03-28 15:42:04 +0200551void qtest_clock_warp(int64_t dest)
552{
Alex Bligh40daca52013-08-21 16:03:02 +0100553 int64_t clock = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
Fam Zhengefef88b2015-01-19 17:51:43 +0800554 AioContext *aio_context;
Paolo Bonzini8156be52012-03-28 15:42:04 +0200555 assert(qtest_enabled());
Fam Zhengefef88b2015-01-19 17:51:43 +0800556 aio_context = qemu_get_aio_context();
Paolo Bonzini8156be52012-03-28 15:42:04 +0200557 while (clock < dest) {
Alex Bligh40daca52013-08-21 16:03:02 +0100558 int64_t deadline = qemu_clock_deadline_ns_all(QEMU_CLOCK_VIRTUAL);
Sergey Fedorovc9299e22014-06-10 13:10:28 +0400559 int64_t warp = qemu_soonest_timeout(dest - clock, deadline);
Fam Zhengefef88b2015-01-19 17:51:43 +0800560
Paolo Bonzini94377112018-08-14 09:57:16 +0200561 seqlock_write_lock(&timers_state.vm_clock_seqlock,
562 &timers_state.vm_clock_lock);
Emilio G. Cotac97595d2018-09-10 19:27:50 -0400563 atomic_set_i64(&timers_state.qemu_icount_bias,
564 timers_state.qemu_icount_bias + warp);
Paolo Bonzini94377112018-08-14 09:57:16 +0200565 seqlock_write_unlock(&timers_state.vm_clock_seqlock,
566 &timers_state.vm_clock_lock);
Paolo Bonzini17a15f12013-10-03 15:17:25 +0200567
Alex Bligh40daca52013-08-21 16:03:02 +0100568 qemu_clock_run_timers(QEMU_CLOCK_VIRTUAL);
Fam Zhengefef88b2015-01-19 17:51:43 +0800569 timerlist_run_timers(aio_context->tlg.tl[QEMU_CLOCK_VIRTUAL]);
Alex Bligh40daca52013-08-21 16:03:02 +0100570 clock = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
Paolo Bonzini8156be52012-03-28 15:42:04 +0200571 }
Alex Bligh40daca52013-08-21 16:03:02 +0100572 qemu_clock_notify(QEMU_CLOCK_VIRTUAL);
Paolo Bonzini8156be52012-03-28 15:42:04 +0200573}
574
Pavel Dovgalyuke76d1792016-03-10 14:56:09 +0300575void qemu_start_warp_timer(void)
Paolo Bonzini946fb272011-09-12 13:57:37 +0200576{
Paolo Bonzinice78d182013-10-07 17:30:02 +0200577 int64_t clock;
Paolo Bonzini946fb272011-09-12 13:57:37 +0200578 int64_t deadline;
579
Pavel Dovgalyuke76d1792016-03-10 14:56:09 +0300580 if (!use_icount) {
Paolo Bonzini946fb272011-09-12 13:57:37 +0200581 return;
582 }
583
Pavel Dovgalyuk8bd7f712015-09-17 19:24:44 +0300584 /* Nothing to do if the VM is stopped: QEMU_CLOCK_VIRTUAL timers
585 * do not fire, so computing the deadline does not make sense.
586 */
587 if (!runstate_is_running()) {
588 return;
589 }
590
Pavel Dovgalyuk0c081852018-09-12 11:19:45 +0300591 if (replay_mode != REPLAY_MODE_PLAY) {
592 if (!all_cpu_threads_idle()) {
593 return;
594 }
Pavel Dovgalyuk8bd7f712015-09-17 19:24:44 +0300595
Pavel Dovgalyuk0c081852018-09-12 11:19:45 +0300596 if (qtest_enabled()) {
597 /* When testing, qtest commands advance icount. */
598 return;
599 }
Paolo Bonzini946fb272011-09-12 13:57:37 +0200600
Pavel Dovgalyuk0c081852018-09-12 11:19:45 +0300601 replay_checkpoint(CHECKPOINT_CLOCK_WARP_START);
602 } else {
603 /* warp clock deterministically in record/replay mode */
604 if (!replay_checkpoint(CHECKPOINT_CLOCK_WARP_START)) {
605 /* vCPU is sleeping and warp can't be started.
606 It is probably a race condition: notification sent
607 to vCPU was processed in advance and vCPU went to sleep.
608 Therefore we have to wake it up for doing someting. */
609 if (replay_has_checkpoint()) {
610 qemu_clock_notify(QEMU_CLOCK_VIRTUAL);
611 }
612 return;
613 }
Paolo Bonzini8156be52012-03-28 15:42:04 +0200614 }
615
Alex Blighac70aaf2013-08-21 16:02:57 +0100616 /* We want to use the earliest deadline from ALL vm_clocks */
Pavel Dovgalyukbf2a7dd2014-11-26 13:40:55 +0300617 clock = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL_RT);
Alex Bligh40daca52013-08-21 16:03:02 +0100618 deadline = qemu_clock_deadline_ns_all(QEMU_CLOCK_VIRTUAL);
Paolo Bonzinice78d182013-10-07 17:30:02 +0200619 if (deadline < 0) {
Victor CLEMENTd7a0f712015-05-29 17:14:06 +0200620 static bool notified;
621 if (!icount_sleep && !notified) {
Alistair Francis3dc6f862017-07-12 06:57:41 -0700622 warn_report("icount sleep disabled and no active timers");
Victor CLEMENTd7a0f712015-05-29 17:14:06 +0200623 notified = true;
624 }
Paolo Bonzinice78d182013-10-07 17:30:02 +0200625 return;
Alex Blighac70aaf2013-08-21 16:02:57 +0100626 }
627
Paolo Bonzini946fb272011-09-12 13:57:37 +0200628 if (deadline > 0) {
629 /*
Alex Bligh40daca52013-08-21 16:03:02 +0100630 * Ensure QEMU_CLOCK_VIRTUAL proceeds even when the virtual CPU goes to
Paolo Bonzini946fb272011-09-12 13:57:37 +0200631 * sleep. Otherwise, the CPU might be waiting for a future timer
632 * interrupt to wake it up, but the interrupt never comes because
633 * the vCPU isn't running any insns and thus doesn't advance the
Alex Bligh40daca52013-08-21 16:03:02 +0100634 * QEMU_CLOCK_VIRTUAL.
Paolo Bonzini946fb272011-09-12 13:57:37 +0200635 */
Victor CLEMENT5045e9d92015-05-29 17:14:04 +0200636 if (!icount_sleep) {
637 /*
638 * We never let VCPUs sleep in no sleep icount mode.
639 * If there is a pending QEMU_CLOCK_VIRTUAL timer we just advance
640 * to the next QEMU_CLOCK_VIRTUAL event and notify it.
641 * It is useful when we want a deterministic execution time,
642 * isolated from host latencies.
643 */
Paolo Bonzini94377112018-08-14 09:57:16 +0200644 seqlock_write_lock(&timers_state.vm_clock_seqlock,
645 &timers_state.vm_clock_lock);
Emilio G. Cotac97595d2018-09-10 19:27:50 -0400646 atomic_set_i64(&timers_state.qemu_icount_bias,
647 timers_state.qemu_icount_bias + deadline);
Paolo Bonzini94377112018-08-14 09:57:16 +0200648 seqlock_write_unlock(&timers_state.vm_clock_seqlock,
649 &timers_state.vm_clock_lock);
Victor CLEMENT5045e9d92015-05-29 17:14:04 +0200650 qemu_clock_notify(QEMU_CLOCK_VIRTUAL);
651 } else {
652 /*
653 * We do stop VCPUs and only advance QEMU_CLOCK_VIRTUAL after some
654 * "real" time, (related to the time left until the next event) has
655 * passed. The QEMU_CLOCK_VIRTUAL_RT clock will do this.
656 * This avoids that the warps are visible externally; for example,
657 * you will not be sending network packets continuously instead of
658 * every 100ms.
659 */
Paolo Bonzini94377112018-08-14 09:57:16 +0200660 seqlock_write_lock(&timers_state.vm_clock_seqlock,
661 &timers_state.vm_clock_lock);
Pavel Dovgalyukb39e3f32018-01-11 11:26:10 +0300662 if (timers_state.vm_clock_warp_start == -1
663 || timers_state.vm_clock_warp_start > clock) {
664 timers_state.vm_clock_warp_start = clock;
Victor CLEMENT5045e9d92015-05-29 17:14:04 +0200665 }
Paolo Bonzini94377112018-08-14 09:57:16 +0200666 seqlock_write_unlock(&timers_state.vm_clock_seqlock,
667 &timers_state.vm_clock_lock);
Pavel Dovgalyukb39e3f32018-01-11 11:26:10 +0300668 timer_mod_anticipate(timers_state.icount_warp_timer,
669 clock + deadline);
Paolo Bonzinice78d182013-10-07 17:30:02 +0200670 }
Alex Blighac70aaf2013-08-21 16:02:57 +0100671 } else if (deadline == 0) {
Alex Bligh40daca52013-08-21 16:03:02 +0100672 qemu_clock_notify(QEMU_CLOCK_VIRTUAL);
Paolo Bonzini946fb272011-09-12 13:57:37 +0200673 }
674}
675
Pavel Dovgalyuke76d1792016-03-10 14:56:09 +0300676static void qemu_account_warp_timer(void)
677{
678 if (!use_icount || !icount_sleep) {
679 return;
680 }
681
682 /* Nothing to do if the VM is stopped: QEMU_CLOCK_VIRTUAL timers
683 * do not fire, so computing the deadline does not make sense.
684 */
685 if (!runstate_is_running()) {
686 return;
687 }
688
689 /* warp clock deterministically in record/replay mode */
690 if (!replay_checkpoint(CHECKPOINT_CLOCK_WARP_ACCOUNT)) {
691 return;
692 }
693
Pavel Dovgalyukb39e3f32018-01-11 11:26:10 +0300694 timer_del(timers_state.icount_warp_timer);
Pavel Dovgalyuke76d1792016-03-10 14:56:09 +0300695 icount_warp_rt();
696}
697
KONRAD Fredericd09eae32014-08-01 01:37:10 +0200698static bool icount_state_needed(void *opaque)
699{
700 return use_icount;
701}
702
Pavel Dovgalyukb39e3f32018-01-11 11:26:10 +0300703static bool warp_timer_state_needed(void *opaque)
704{
705 TimersState *s = opaque;
706 return s->icount_warp_timer != NULL;
707}
708
709static bool adjust_timers_state_needed(void *opaque)
710{
711 TimersState *s = opaque;
712 return s->icount_rt_timer != NULL;
713}
714
715/*
716 * Subsection for warp timer migration is optional, because may not be created
717 */
718static const VMStateDescription icount_vmstate_warp_timer = {
719 .name = "timer/icount/warp_timer",
720 .version_id = 1,
721 .minimum_version_id = 1,
722 .needed = warp_timer_state_needed,
723 .fields = (VMStateField[]) {
724 VMSTATE_INT64(vm_clock_warp_start, TimersState),
725 VMSTATE_TIMER_PTR(icount_warp_timer, TimersState),
726 VMSTATE_END_OF_LIST()
727 }
728};
729
730static const VMStateDescription icount_vmstate_adjust_timers = {
731 .name = "timer/icount/timers",
732 .version_id = 1,
733 .minimum_version_id = 1,
734 .needed = adjust_timers_state_needed,
735 .fields = (VMStateField[]) {
736 VMSTATE_TIMER_PTR(icount_rt_timer, TimersState),
737 VMSTATE_TIMER_PTR(icount_vm_timer, TimersState),
738 VMSTATE_END_OF_LIST()
739 }
740};
741
KONRAD Fredericd09eae32014-08-01 01:37:10 +0200742/*
743 * This is a subsection for icount migration.
744 */
745static const VMStateDescription icount_vmstate_timers = {
746 .name = "timer/icount",
747 .version_id = 1,
748 .minimum_version_id = 1,
Juan Quintela5cd8cad2014-09-23 14:09:54 +0200749 .needed = icount_state_needed,
KONRAD Fredericd09eae32014-08-01 01:37:10 +0200750 .fields = (VMStateField[]) {
751 VMSTATE_INT64(qemu_icount_bias, TimersState),
752 VMSTATE_INT64(qemu_icount, TimersState),
753 VMSTATE_END_OF_LIST()
Pavel Dovgalyukb39e3f32018-01-11 11:26:10 +0300754 },
755 .subsections = (const VMStateDescription*[]) {
756 &icount_vmstate_warp_timer,
757 &icount_vmstate_adjust_timers,
758 NULL
KONRAD Fredericd09eae32014-08-01 01:37:10 +0200759 }
760};
761
Paolo Bonzini946fb272011-09-12 13:57:37 +0200762static const VMStateDescription vmstate_timers = {
763 .name = "timer",
764 .version_id = 2,
765 .minimum_version_id = 1,
Juan Quintela35d08452014-04-16 16:01:33 +0200766 .fields = (VMStateField[]) {
Paolo Bonzini946fb272011-09-12 13:57:37 +0200767 VMSTATE_INT64(cpu_ticks_offset, TimersState),
Paolo Bonzinic1ff0732018-08-14 09:31:58 +0200768 VMSTATE_UNUSED(8),
Paolo Bonzini946fb272011-09-12 13:57:37 +0200769 VMSTATE_INT64_V(cpu_clock_offset, TimersState, 2),
770 VMSTATE_END_OF_LIST()
KONRAD Fredericd09eae32014-08-01 01:37:10 +0200771 },
Juan Quintela5cd8cad2014-09-23 14:09:54 +0200772 .subsections = (const VMStateDescription*[]) {
773 &icount_vmstate_timers,
774 NULL
Paolo Bonzini946fb272011-09-12 13:57:37 +0200775 }
776};
777
Paolo Bonzini14e6fe12016-10-31 10:36:08 +0100778static void cpu_throttle_thread(CPUState *cpu, run_on_cpu_data opaque)
Jason J. Herne2adcc852015-09-08 13:12:33 -0400779{
Jason J. Herne2adcc852015-09-08 13:12:33 -0400780 double pct;
781 double throttle_ratio;
782 long sleeptime_ns;
783
784 if (!cpu_throttle_get_percentage()) {
785 return;
786 }
787
788 pct = (double)cpu_throttle_get_percentage()/100;
789 throttle_ratio = pct / (1 - pct);
790 sleeptime_ns = (long)(throttle_ratio * CPU_THROTTLE_TIMESLICE_NS);
791
792 qemu_mutex_unlock_iothread();
Jason J. Herne2adcc852015-09-08 13:12:33 -0400793 g_usleep(sleeptime_ns / 1000); /* Convert ns to us for usleep call */
794 qemu_mutex_lock_iothread();
Felipe Franciosi90bb0c02017-05-19 22:29:50 +0100795 atomic_set(&cpu->throttle_thread_scheduled, 0);
Jason J. Herne2adcc852015-09-08 13:12:33 -0400796}
797
798static void cpu_throttle_timer_tick(void *opaque)
799{
800 CPUState *cpu;
801 double pct;
802
803 /* Stop the timer if needed */
804 if (!cpu_throttle_get_percentage()) {
805 return;
806 }
807 CPU_FOREACH(cpu) {
808 if (!atomic_xchg(&cpu->throttle_thread_scheduled, 1)) {
Paolo Bonzini14e6fe12016-10-31 10:36:08 +0100809 async_run_on_cpu(cpu, cpu_throttle_thread,
810 RUN_ON_CPU_NULL);
Jason J. Herne2adcc852015-09-08 13:12:33 -0400811 }
812 }
813
814 pct = (double)cpu_throttle_get_percentage()/100;
815 timer_mod(throttle_timer, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL_RT) +
816 CPU_THROTTLE_TIMESLICE_NS / (1-pct));
817}
818
819void cpu_throttle_set(int new_throttle_pct)
820{
821 /* Ensure throttle percentage is within valid range */
822 new_throttle_pct = MIN(new_throttle_pct, CPU_THROTTLE_PCT_MAX);
823 new_throttle_pct = MAX(new_throttle_pct, CPU_THROTTLE_PCT_MIN);
824
825 atomic_set(&throttle_percentage, new_throttle_pct);
826
827 timer_mod(throttle_timer, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL_RT) +
828 CPU_THROTTLE_TIMESLICE_NS);
829}
830
831void cpu_throttle_stop(void)
832{
833 atomic_set(&throttle_percentage, 0);
834}
835
836bool cpu_throttle_active(void)
837{
838 return (cpu_throttle_get_percentage() != 0);
839}
840
841int cpu_throttle_get_percentage(void)
842{
843 return atomic_read(&throttle_percentage);
844}
845
Pavel Dovgalyuk4603ea02014-09-01 09:34:49 +0400846void cpu_ticks_init(void)
847{
Emilio G. Cotaccdb3c12016-06-08 14:55:20 -0400848 seqlock_init(&timers_state.vm_clock_seqlock);
Emilio G. Cota87a09cd2018-09-03 13:18:29 -0400849 qemu_spin_init(&timers_state.vm_clock_lock);
Pavel Dovgalyuk4603ea02014-09-01 09:34:49 +0400850 vmstate_register(NULL, 0, &vmstate_timers, &timers_state);
Jason J. Herne2adcc852015-09-08 13:12:33 -0400851 throttle_timer = timer_new_ns(QEMU_CLOCK_VIRTUAL_RT,
852 cpu_throttle_timer_tick, NULL);
Pavel Dovgalyuk4603ea02014-09-01 09:34:49 +0400853}
854
Sebastian Tanase1ad95802014-07-25 11:56:28 +0200855void configure_icount(QemuOpts *opts, Error **errp)
Paolo Bonzini946fb272011-09-12 13:57:37 +0200856{
Sebastian Tanase1ad95802014-07-25 11:56:28 +0200857 const char *option;
Sebastian Tanasea8bfac32014-07-25 11:56:29 +0200858 char *rem_str = NULL;
Sebastian Tanase1ad95802014-07-25 11:56:28 +0200859
Sebastian Tanase1ad95802014-07-25 11:56:28 +0200860 option = qemu_opt_get(opts, "shift");
Paolo Bonzini946fb272011-09-12 13:57:37 +0200861 if (!option) {
Sebastian Tanasea8bfac32014-07-25 11:56:29 +0200862 if (qemu_opt_get(opts, "align") != NULL) {
863 error_setg(errp, "Please specify shift option when using align");
864 }
Paolo Bonzini946fb272011-09-12 13:57:37 +0200865 return;
866 }
Victor CLEMENTf1f4b572015-05-29 17:14:05 +0200867
868 icount_sleep = qemu_opt_get_bool(opts, "sleep", true);
Victor CLEMENT5045e9d92015-05-29 17:14:04 +0200869 if (icount_sleep) {
Pavel Dovgalyukb39e3f32018-01-11 11:26:10 +0300870 timers_state.icount_warp_timer = timer_new_ns(QEMU_CLOCK_VIRTUAL_RT,
Pavel Dovgalyuke76d1792016-03-10 14:56:09 +0300871 icount_timer_cb, NULL);
Victor CLEMENT5045e9d92015-05-29 17:14:04 +0200872 }
Victor CLEMENTf1f4b572015-05-29 17:14:05 +0200873
Sebastian Tanasea8bfac32014-07-25 11:56:29 +0200874 icount_align_option = qemu_opt_get_bool(opts, "align", false);
Victor CLEMENTf1f4b572015-05-29 17:14:05 +0200875
876 if (icount_align_option && !icount_sleep) {
Pranith Kumar778d9f92016-02-26 10:16:51 -0500877 error_setg(errp, "align=on and sleep=off are incompatible");
Victor CLEMENTf1f4b572015-05-29 17:14:05 +0200878 }
Paolo Bonzini946fb272011-09-12 13:57:37 +0200879 if (strcmp(option, "auto") != 0) {
Sebastian Tanasea8bfac32014-07-25 11:56:29 +0200880 errno = 0;
Paolo Bonzinic1ff0732018-08-14 09:31:58 +0200881 timers_state.icount_time_shift = strtol(option, &rem_str, 0);
Sebastian Tanasea8bfac32014-07-25 11:56:29 +0200882 if (errno != 0 || *rem_str != '\0' || !strlen(option)) {
883 error_setg(errp, "icount: Invalid shift value");
884 }
Paolo Bonzini946fb272011-09-12 13:57:37 +0200885 use_icount = 1;
886 return;
Sebastian Tanasea8bfac32014-07-25 11:56:29 +0200887 } else if (icount_align_option) {
888 error_setg(errp, "shift=auto and align=on are incompatible");
Victor CLEMENTf1f4b572015-05-29 17:14:05 +0200889 } else if (!icount_sleep) {
Pranith Kumar778d9f92016-02-26 10:16:51 -0500890 error_setg(errp, "shift=auto and sleep=off are incompatible");
Paolo Bonzini946fb272011-09-12 13:57:37 +0200891 }
892
893 use_icount = 2;
894
895 /* 125MIPS seems a reasonable initial guess at the guest speed.
896 It will be corrected fairly quickly anyway. */
Paolo Bonzinic1ff0732018-08-14 09:31:58 +0200897 timers_state.icount_time_shift = 3;
Paolo Bonzini946fb272011-09-12 13:57:37 +0200898
899 /* Have both realtime and virtual time triggers for speed adjustment.
900 The realtime trigger catches emulated time passing too slowly,
901 the virtual time trigger catches emulated time passing too fast.
902 Realtime triggers occur even when idle, so use them less frequently
903 than VM triggers. */
Pavel Dovgalyukb39e3f32018-01-11 11:26:10 +0300904 timers_state.vm_clock_warp_start = -1;
905 timers_state.icount_rt_timer = timer_new_ms(QEMU_CLOCK_VIRTUAL_RT,
Pavel Dovgalyukbf2a7dd2014-11-26 13:40:55 +0300906 icount_adjust_rt, NULL);
Pavel Dovgalyukb39e3f32018-01-11 11:26:10 +0300907 timer_mod(timers_state.icount_rt_timer,
Pavel Dovgalyukbf2a7dd2014-11-26 13:40:55 +0300908 qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL_RT) + 1000);
Pavel Dovgalyukb39e3f32018-01-11 11:26:10 +0300909 timers_state.icount_vm_timer = timer_new_ns(QEMU_CLOCK_VIRTUAL,
Alex Bligh40daca52013-08-21 16:03:02 +0100910 icount_adjust_vm, NULL);
Pavel Dovgalyukb39e3f32018-01-11 11:26:10 +0300911 timer_mod(timers_state.icount_vm_timer,
Alex Bligh40daca52013-08-21 16:03:02 +0100912 qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) +
Rutuja Shah73bcb242016-03-21 21:32:30 +0530913 NANOSECONDS_PER_SECOND / 10);
Paolo Bonzini946fb272011-09-12 13:57:37 +0200914}
915
916/***********************************************************/
Alex Bennée65467062017-02-23 18:29:09 +0000917/* TCG vCPU kick timer
918 *
919 * The kick timer is responsible for moving single threaded vCPU
920 * emulation on to the next vCPU. If more than one vCPU is running a
921 * timer event with force a cpu->exit so the next vCPU can get
922 * scheduled.
923 *
924 * The timer is removed if all vCPUs are idle and restarted again once
925 * idleness is complete.
926 */
927
928static QEMUTimer *tcg_kick_vcpu_timer;
Alex Bennée791158d2017-02-23 18:29:10 +0000929static CPUState *tcg_current_rr_cpu;
Alex Bennée65467062017-02-23 18:29:09 +0000930
931#define TCG_KICK_PERIOD (NANOSECONDS_PER_SECOND / 10)
932
933static inline int64_t qemu_tcg_next_kick(void)
934{
935 return qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) + TCG_KICK_PERIOD;
936}
937
Alex Bennée791158d2017-02-23 18:29:10 +0000938/* Kick the currently round-robin scheduled vCPU */
939static void qemu_cpu_kick_rr_cpu(void)
940{
941 CPUState *cpu;
Alex Bennée791158d2017-02-23 18:29:10 +0000942 do {
943 cpu = atomic_mb_read(&tcg_current_rr_cpu);
944 if (cpu) {
945 cpu_exit(cpu);
946 }
947 } while (cpu != atomic_mb_read(&tcg_current_rr_cpu));
948}
949
Paolo Bonzini6b8f0182017-03-02 19:56:40 +0100950static void do_nothing(CPUState *cpu, run_on_cpu_data unused)
951{
952}
953
Paolo Bonzini3f53bc62017-03-03 11:50:29 +0100954void qemu_timer_notify_cb(void *opaque, QEMUClockType type)
955{
Paolo Bonzini6b8f0182017-03-02 19:56:40 +0100956 if (!use_icount || type != QEMU_CLOCK_VIRTUAL) {
957 qemu_notify_event();
958 return;
959 }
960
Peter Maydellc52e7132018-04-10 13:02:25 +0100961 if (qemu_in_vcpu_thread()) {
962 /* A CPU is currently running; kick it back out to the
963 * tcg_cpu_exec() loop so it will recalculate its
964 * icount deadline immediately.
965 */
966 qemu_cpu_kick(current_cpu);
967 } else if (first_cpu) {
Paolo Bonzini6b8f0182017-03-02 19:56:40 +0100968 /* qemu_cpu_kick is not enough to kick a halted CPU out of
969 * qemu_tcg_wait_io_event. async_run_on_cpu, instead,
970 * causes cpu_thread_is_idle to return false. This way,
971 * handle_icount_deadline can run.
Peter Maydellc52e7132018-04-10 13:02:25 +0100972 * If we have no CPUs at all for some reason, we don't
973 * need to do anything.
Paolo Bonzini6b8f0182017-03-02 19:56:40 +0100974 */
975 async_run_on_cpu(first_cpu, do_nothing, RUN_ON_CPU_NULL);
976 }
Paolo Bonzini3f53bc62017-03-03 11:50:29 +0100977}
978
Alex Bennée65467062017-02-23 18:29:09 +0000979static void kick_tcg_thread(void *opaque)
980{
981 timer_mod(tcg_kick_vcpu_timer, qemu_tcg_next_kick());
Alex Bennée791158d2017-02-23 18:29:10 +0000982 qemu_cpu_kick_rr_cpu();
Alex Bennée65467062017-02-23 18:29:09 +0000983}
984
985static void start_tcg_kick_timer(void)
986{
Paolo Bonzinidb08b682018-01-11 13:53:12 +0100987 assert(!mttcg_enabled);
988 if (!tcg_kick_vcpu_timer && CPU_NEXT(first_cpu)) {
Alex Bennée65467062017-02-23 18:29:09 +0000989 tcg_kick_vcpu_timer = timer_new_ns(QEMU_CLOCK_VIRTUAL,
990 kick_tcg_thread, NULL);
Alex Bennée1926ab22018-09-27 18:17:24 +0100991 }
992 if (tcg_kick_vcpu_timer && !timer_pending(tcg_kick_vcpu_timer)) {
Alex Bennée65467062017-02-23 18:29:09 +0000993 timer_mod(tcg_kick_vcpu_timer, qemu_tcg_next_kick());
994 }
995}
996
997static void stop_tcg_kick_timer(void)
998{
Paolo Bonzinidb08b682018-01-11 13:53:12 +0100999 assert(!mttcg_enabled);
Alex Bennée1926ab22018-09-27 18:17:24 +01001000 if (tcg_kick_vcpu_timer && timer_pending(tcg_kick_vcpu_timer)) {
Alex Bennée65467062017-02-23 18:29:09 +00001001 timer_del(tcg_kick_vcpu_timer);
Alex Bennée65467062017-02-23 18:29:09 +00001002 }
1003}
1004
Alex Bennée65467062017-02-23 18:29:09 +00001005/***********************************************************/
Blue Swirl296af7c2010-03-29 19:23:50 +00001006void hw_error(const char *fmt, ...)
1007{
1008 va_list ap;
Andreas Färber55e5c282012-12-17 06:18:02 +01001009 CPUState *cpu;
Blue Swirl296af7c2010-03-29 19:23:50 +00001010
1011 va_start(ap, fmt);
1012 fprintf(stderr, "qemu: hardware error: ");
1013 vfprintf(stderr, fmt, ap);
1014 fprintf(stderr, "\n");
Andreas Färberbdc44642013-06-24 23:50:24 +02001015 CPU_FOREACH(cpu) {
Andreas Färber55e5c282012-12-17 06:18:02 +01001016 fprintf(stderr, "CPU #%d:\n", cpu->cpu_index);
Markus Armbruster90c84c52019-04-17 21:18:02 +02001017 cpu_dump_state(cpu, stderr, CPU_DUMP_FPU);
Blue Swirl296af7c2010-03-29 19:23:50 +00001018 }
1019 va_end(ap);
1020 abort();
1021}
1022
1023void cpu_synchronize_all_states(void)
1024{
Andreas Färber182735e2013-05-29 22:29:20 +02001025 CPUState *cpu;
Blue Swirl296af7c2010-03-29 19:23:50 +00001026
Andreas Färberbdc44642013-06-24 23:50:24 +02001027 CPU_FOREACH(cpu) {
Andreas Färber182735e2013-05-29 22:29:20 +02001028 cpu_synchronize_state(cpu);
Sergio Andres Gomez Del Realc97d6d22017-09-13 04:05:09 -05001029 /* TODO: move to cpu_synchronize_state() */
1030 if (hvf_enabled()) {
1031 hvf_cpu_synchronize_state(cpu);
1032 }
Blue Swirl296af7c2010-03-29 19:23:50 +00001033 }
1034}
1035
1036void cpu_synchronize_all_post_reset(void)
1037{
Andreas Färber182735e2013-05-29 22:29:20 +02001038 CPUState *cpu;
Blue Swirl296af7c2010-03-29 19:23:50 +00001039
Andreas Färberbdc44642013-06-24 23:50:24 +02001040 CPU_FOREACH(cpu) {
Andreas Färber182735e2013-05-29 22:29:20 +02001041 cpu_synchronize_post_reset(cpu);
Sergio Andres Gomez Del Realc97d6d22017-09-13 04:05:09 -05001042 /* TODO: move to cpu_synchronize_post_reset() */
1043 if (hvf_enabled()) {
1044 hvf_cpu_synchronize_post_reset(cpu);
1045 }
Blue Swirl296af7c2010-03-29 19:23:50 +00001046 }
1047}
1048
1049void cpu_synchronize_all_post_init(void)
1050{
Andreas Färber182735e2013-05-29 22:29:20 +02001051 CPUState *cpu;
Blue Swirl296af7c2010-03-29 19:23:50 +00001052
Andreas Färberbdc44642013-06-24 23:50:24 +02001053 CPU_FOREACH(cpu) {
Andreas Färber182735e2013-05-29 22:29:20 +02001054 cpu_synchronize_post_init(cpu);
Sergio Andres Gomez Del Realc97d6d22017-09-13 04:05:09 -05001055 /* TODO: move to cpu_synchronize_post_init() */
1056 if (hvf_enabled()) {
1057 hvf_cpu_synchronize_post_init(cpu);
1058 }
Blue Swirl296af7c2010-03-29 19:23:50 +00001059 }
1060}
1061
David Gibson75e972d2017-05-26 14:46:28 +10001062void cpu_synchronize_all_pre_loadvm(void)
1063{
1064 CPUState *cpu;
1065
1066 CPU_FOREACH(cpu) {
1067 cpu_synchronize_pre_loadvm(cpu);
1068 }
1069}
1070
Stefan Hajnoczi4486e892018-03-07 14:42:05 +00001071static int do_vm_stop(RunState state, bool send_stop)
Blue Swirl296af7c2010-03-29 19:23:50 +00001072{
Kevin Wolf56983462013-07-05 13:49:54 +02001073 int ret = 0;
1074
Luiz Capitulino13548692011-07-29 15:36:43 -03001075 if (runstate_is_running()) {
Blue Swirl296af7c2010-03-29 19:23:50 +00001076 cpu_disable_ticks();
Blue Swirl296af7c2010-03-29 19:23:50 +00001077 pause_all_vcpus();
Luiz Capitulinof5bbfba2011-07-29 15:04:45 -03001078 runstate_set(state);
Luiz Capitulino1dfb4dd2011-07-29 14:26:33 -03001079 vm_state_notify(0, state);
Stefan Hajnoczi4486e892018-03-07 14:42:05 +00001080 if (send_stop) {
Peter Xu3ab72382018-08-15 21:37:37 +08001081 qapi_event_send_stop();
Stefan Hajnoczi4486e892018-03-07 14:42:05 +00001082 }
Blue Swirl296af7c2010-03-29 19:23:50 +00001083 }
Kevin Wolf56983462013-07-05 13:49:54 +02001084
Kevin Wolf594a45c2013-07-18 14:52:19 +02001085 bdrv_drain_all();
Pavel Dovgalyuk6d0ceb82016-09-26 11:08:16 +03001086 replay_disable_events();
John Snow22af08e2016-09-22 21:45:51 -04001087 ret = bdrv_flush_all();
Kevin Wolf594a45c2013-07-18 14:52:19 +02001088
Kevin Wolf56983462013-07-05 13:49:54 +02001089 return ret;
Blue Swirl296af7c2010-03-29 19:23:50 +00001090}
1091
Stefan Hajnoczi4486e892018-03-07 14:42:05 +00001092/* Special vm_stop() variant for terminating the process. Historically clients
1093 * did not expect a QMP STOP event and so we need to retain compatibility.
1094 */
1095int vm_shutdown(void)
1096{
1097 return do_vm_stop(RUN_STATE_SHUTDOWN, false);
1098}
1099
Andreas Färbera1fcaa72012-05-02 23:42:26 +02001100static bool cpu_can_run(CPUState *cpu)
Blue Swirl296af7c2010-03-29 19:23:50 +00001101{
Andreas Färber4fdeee72012-05-02 23:10:09 +02001102 if (cpu->stop) {
Andreas Färbera1fcaa72012-05-02 23:42:26 +02001103 return false;
Jan Kiszka0ab07c62011-02-07 12:19:14 +01001104 }
Tiejun Chen321bc0b2013-08-02 09:43:09 +08001105 if (cpu_is_stopped(cpu)) {
Andreas Färbera1fcaa72012-05-02 23:42:26 +02001106 return false;
Jan Kiszka0ab07c62011-02-07 12:19:14 +01001107 }
Andreas Färbera1fcaa72012-05-02 23:42:26 +02001108 return true;
Blue Swirl296af7c2010-03-29 19:23:50 +00001109}
1110
Andreas Färber91325042013-05-27 02:07:49 +02001111static void cpu_handle_guest_debug(CPUState *cpu)
Jan Kiszka3c638d02010-06-25 16:56:56 +02001112{
Andreas Färber64f6b342013-05-27 02:06:09 +02001113 gdb_set_stop_cpu(cpu);
Jan Kiszka8cf71712011-02-07 12:19:16 +01001114 qemu_system_debug_request();
Andreas Färberf324e762012-05-02 23:26:21 +02001115 cpu->stopped = true;
Jan Kiszka3c638d02010-06-25 16:56:56 +02001116}
1117
Jan Kiszka6d9cb732011-02-01 22:15:58 +01001118#ifdef CONFIG_LINUX
1119static void sigbus_reraise(void)
1120{
1121 sigset_t set;
1122 struct sigaction action;
1123
1124 memset(&action, 0, sizeof(action));
1125 action.sa_handler = SIG_DFL;
1126 if (!sigaction(SIGBUS, &action, NULL)) {
1127 raise(SIGBUS);
1128 sigemptyset(&set);
1129 sigaddset(&set, SIGBUS);
Peter Maydella2d17612016-05-16 18:33:59 +01001130 pthread_sigmask(SIG_UNBLOCK, &set, NULL);
Jan Kiszka6d9cb732011-02-01 22:15:58 +01001131 }
1132 perror("Failed to re-raise SIGBUS!\n");
1133 abort();
1134}
1135
Paolo Bonzinid98d4072017-02-08 13:22:12 +01001136static void sigbus_handler(int n, siginfo_t *siginfo, void *ctx)
Jan Kiszka6d9cb732011-02-01 22:15:58 +01001137{
Paolo Bonzinia16fc072017-02-09 09:50:02 +01001138 if (siginfo->si_code != BUS_MCEERR_AO && siginfo->si_code != BUS_MCEERR_AR) {
1139 sigbus_reraise();
1140 }
1141
Paolo Bonzini2ae41db2017-02-08 12:48:54 +01001142 if (current_cpu) {
1143 /* Called asynchronously in VCPU thread. */
1144 if (kvm_on_sigbus_vcpu(current_cpu, siginfo->si_code, siginfo->si_addr)) {
1145 sigbus_reraise();
1146 }
1147 } else {
1148 /* Called synchronously (via signalfd) in main thread. */
1149 if (kvm_on_sigbus(siginfo->si_code, siginfo->si_addr)) {
1150 sigbus_reraise();
1151 }
Jan Kiszka6d9cb732011-02-01 22:15:58 +01001152 }
1153}
1154
1155static void qemu_init_sigbus(void)
1156{
1157 struct sigaction action;
1158
1159 memset(&action, 0, sizeof(action));
1160 action.sa_flags = SA_SIGINFO;
Paolo Bonzinid98d4072017-02-08 13:22:12 +01001161 action.sa_sigaction = sigbus_handler;
Jan Kiszka6d9cb732011-02-01 22:15:58 +01001162 sigaction(SIGBUS, &action, NULL);
1163
1164 prctl(PR_MCE_KILL, PR_MCE_KILL_SET, PR_MCE_KILL_EARLY, 0, 0);
1165}
Paolo Bonzinia16fc072017-02-09 09:50:02 +01001166#else /* !CONFIG_LINUX */
1167static void qemu_init_sigbus(void)
1168{
1169}
Paolo Bonzinia16fc072017-02-09 09:50:02 +01001170#endif /* !CONFIG_LINUX */
Blue Swirl296af7c2010-03-29 19:23:50 +00001171
Stefan Weilb2532d82012-09-27 07:41:42 +02001172static QemuMutex qemu_global_mutex;
Blue Swirl296af7c2010-03-29 19:23:50 +00001173
1174static QemuThread io_thread;
1175
Blue Swirl296af7c2010-03-29 19:23:50 +00001176/* cpu creation */
1177static QemuCond qemu_cpu_cond;
1178/* system init */
Blue Swirl296af7c2010-03-29 19:23:50 +00001179static QemuCond qemu_pause_cond;
1180
Paolo Bonzinid3b12f52011-09-13 10:30:52 +02001181void qemu_init_cpu_loop(void)
Blue Swirl296af7c2010-03-29 19:23:50 +00001182{
Jan Kiszka6d9cb732011-02-01 22:15:58 +01001183 qemu_init_sigbus();
Anthony Liguoried945922011-02-08 18:18:18 +01001184 qemu_cond_init(&qemu_cpu_cond);
Anthony Liguoried945922011-02-08 18:18:18 +01001185 qemu_cond_init(&qemu_pause_cond);
Blue Swirl296af7c2010-03-29 19:23:50 +00001186 qemu_mutex_init(&qemu_global_mutex);
Blue Swirl296af7c2010-03-29 19:23:50 +00001187
Jan Kiszkab7680cb2011-03-12 17:43:51 +01001188 qemu_thread_get_self(&io_thread);
Blue Swirl296af7c2010-03-29 19:23:50 +00001189}
1190
Paolo Bonzini14e6fe12016-10-31 10:36:08 +01001191void run_on_cpu(CPUState *cpu, run_on_cpu_func func, run_on_cpu_data data)
Marcelo Tosattie82bcec2010-05-04 09:45:22 -03001192{
Sergey Fedorovd148d902016-08-29 09:51:00 +02001193 do_run_on_cpu(cpu, func, data, &qemu_global_mutex);
Chegu Vinod3c022702013-06-24 03:49:41 -06001194}
1195
Gu Zheng4c055ab2016-05-12 09:18:13 +05301196static void qemu_kvm_destroy_vcpu(CPUState *cpu)
1197{
1198 if (kvm_destroy_vcpu(cpu) < 0) {
1199 error_report("kvm_destroy_vcpu failed");
1200 exit(EXIT_FAILURE);
1201 }
1202}
1203
1204static void qemu_tcg_destroy_vcpu(CPUState *cpu)
1205{
1206}
1207
David Hildenbrandebd05fe2017-11-29 20:12:15 +01001208static void qemu_cpu_stop(CPUState *cpu, bool exit)
1209{
1210 g_assert(qemu_cpu_is_self(cpu));
1211 cpu->stop = false;
1212 cpu->stopped = true;
1213 if (exit) {
1214 cpu_exit(cpu);
1215 }
1216 qemu_cond_broadcast(&qemu_pause_cond);
1217}
1218
Andreas Färber509a0d72012-05-03 02:18:09 +02001219static void qemu_wait_io_event_common(CPUState *cpu)
Blue Swirl296af7c2010-03-29 19:23:50 +00001220{
Alex Bennée37257942017-02-23 18:29:14 +00001221 atomic_mb_set(&cpu->thread_kicked, false);
Andreas Färber4fdeee72012-05-02 23:10:09 +02001222 if (cpu->stop) {
David Hildenbrandebd05fe2017-11-29 20:12:15 +01001223 qemu_cpu_stop(cpu, false);
Blue Swirl296af7c2010-03-29 19:23:50 +00001224 }
Sergey Fedorova5403c62016-08-02 18:27:36 +01001225 process_queued_cpu_work(cpu);
Alex Bennée37257942017-02-23 18:29:14 +00001226}
1227
Paolo Bonzinia8efa602018-11-14 12:36:57 +01001228static void qemu_tcg_rr_wait_io_event(void)
Alex Bennée37257942017-02-23 18:29:14 +00001229{
Paolo Bonzinia8efa602018-11-14 12:36:57 +01001230 CPUState *cpu;
1231
Paolo Bonzinidb08b682018-01-11 13:53:12 +01001232 while (all_cpu_threads_idle()) {
Alex Bennée65467062017-02-23 18:29:09 +00001233 stop_tcg_kick_timer();
Paolo Bonzinia8efa602018-11-14 12:36:57 +01001234 qemu_cond_wait(first_cpu->halt_cond, &qemu_global_mutex);
Jan Kiszka16400322011-02-09 16:29:37 +01001235 }
Blue Swirl296af7c2010-03-29 19:23:50 +00001236
Alex Bennée65467062017-02-23 18:29:09 +00001237 start_tcg_kick_timer();
1238
Paolo Bonzinia8efa602018-11-14 12:36:57 +01001239 CPU_FOREACH(cpu) {
1240 qemu_wait_io_event_common(cpu);
1241 }
Blue Swirl296af7c2010-03-29 19:23:50 +00001242}
1243
Paolo Bonzinidb08b682018-01-11 13:53:12 +01001244static void qemu_wait_io_event(CPUState *cpu)
Blue Swirl296af7c2010-03-29 19:23:50 +00001245{
Andreas Färbera98ae1d2013-05-26 23:21:08 +02001246 while (cpu_thread_is_idle(cpu)) {
Andreas Färberf5c121b2012-05-03 01:22:49 +02001247 qemu_cond_wait(cpu->halt_cond, &qemu_global_mutex);
Jan Kiszka16400322011-02-09 16:29:37 +01001248 }
Blue Swirl296af7c2010-03-29 19:23:50 +00001249
Paolo Bonzinidb08b682018-01-11 13:53:12 +01001250#ifdef _WIN32
1251 /* Eat dummy APC queued by qemu_cpu_kick_thread. */
1252 if (!tcg_enabled()) {
1253 SleepEx(0, TRUE);
Sergio Andres Gomez Del Realc97d6d22017-09-13 04:05:09 -05001254 }
Paolo Bonzinidb08b682018-01-11 13:53:12 +01001255#endif
Sergio Andres Gomez Del Realc97d6d22017-09-13 04:05:09 -05001256 qemu_wait_io_event_common(cpu);
1257}
1258
Jan Kiszka7e97cd82011-02-07 12:19:12 +01001259static void *qemu_kvm_cpu_thread_fn(void *arg)
Blue Swirl296af7c2010-03-29 19:23:50 +00001260{
Andreas Färber48a106b2013-05-27 02:20:39 +02001261 CPUState *cpu = arg;
Jan Kiszka84b49152011-02-01 22:15:50 +01001262 int r;
Blue Swirl296af7c2010-03-29 19:23:50 +00001263
Paolo Bonziniab28bd22015-07-09 08:55:38 +02001264 rcu_register_thread();
1265
Paolo Bonzini2e7f7a32015-06-18 18:47:18 +02001266 qemu_mutex_lock_iothread();
Andreas Färber814e6122012-05-02 17:00:37 +02001267 qemu_thread_get_self(cpu->thread);
Andreas Färber9f09e182012-05-03 06:59:07 +02001268 cpu->thread_id = qemu_get_thread_id();
Pavel Dovgalyuk626cf8f2014-12-08 10:53:17 +03001269 cpu->can_do_io = 1;
Andreas Färber4917cf42013-05-27 05:17:50 +02001270 current_cpu = cpu;
Blue Swirl296af7c2010-03-29 19:23:50 +00001271
Andreas Färber504134d2012-12-17 06:38:45 +01001272 r = kvm_init_vcpu(cpu);
Jan Kiszka84b49152011-02-01 22:15:50 +01001273 if (r < 0) {
Alistair Francis493d89b2018-02-03 09:43:14 +01001274 error_report("kvm_init_vcpu failed: %s", strerror(-r));
Jan Kiszka84b49152011-02-01 22:15:50 +01001275 exit(1);
1276 }
Blue Swirl296af7c2010-03-29 19:23:50 +00001277
Paolo Bonzini18268b62017-02-09 09:41:14 +01001278 kvm_init_cpu_signals(cpu);
Blue Swirl296af7c2010-03-29 19:23:50 +00001279
1280 /* signal CPU creation */
Andreas Färber61a46212012-05-02 22:49:36 +02001281 cpu->created = true;
Blue Swirl296af7c2010-03-29 19:23:50 +00001282 qemu_cond_signal(&qemu_cpu_cond);
Richard Henderson9c09a252019-03-14 13:06:29 -07001283 qemu_guest_random_seed_thread_part2(cpu->random_seed);
Blue Swirl296af7c2010-03-29 19:23:50 +00001284
Gu Zheng4c055ab2016-05-12 09:18:13 +05301285 do {
Andreas Färbera1fcaa72012-05-02 23:42:26 +02001286 if (cpu_can_run(cpu)) {
Andreas Färber1458c362013-05-26 23:46:55 +02001287 r = kvm_cpu_exec(cpu);
Jan Kiszka83f338f2011-02-07 12:19:17 +01001288 if (r == EXCP_DEBUG) {
Andreas Färber91325042013-05-27 02:07:49 +02001289 cpu_handle_guest_debug(cpu);
Jan Kiszka83f338f2011-02-07 12:19:17 +01001290 }
Jan Kiszka0ab07c62011-02-07 12:19:14 +01001291 }
Paolo Bonzinidb08b682018-01-11 13:53:12 +01001292 qemu_wait_io_event(cpu);
Gu Zheng4c055ab2016-05-12 09:18:13 +05301293 } while (!cpu->unplug || cpu_can_run(cpu));
Blue Swirl296af7c2010-03-29 19:23:50 +00001294
Gu Zheng4c055ab2016-05-12 09:18:13 +05301295 qemu_kvm_destroy_vcpu(cpu);
Bharata B Rao2c579042016-05-12 09:18:14 +05301296 cpu->created = false;
1297 qemu_cond_signal(&qemu_cpu_cond);
Gu Zheng4c055ab2016-05-12 09:18:13 +05301298 qemu_mutex_unlock_iothread();
Paolo Bonzini57615ed2018-01-30 11:04:36 -05001299 rcu_unregister_thread();
Blue Swirl296af7c2010-03-29 19:23:50 +00001300 return NULL;
1301}
1302
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +02001303static void *qemu_dummy_cpu_thread_fn(void *arg)
1304{
1305#ifdef _WIN32
Alistair Francis493d89b2018-02-03 09:43:14 +01001306 error_report("qtest is not supported under Windows");
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +02001307 exit(1);
1308#else
Andreas Färber10a90212013-05-27 02:24:35 +02001309 CPUState *cpu = arg;
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +02001310 sigset_t waitset;
1311 int r;
1312
Paolo Bonziniab28bd22015-07-09 08:55:38 +02001313 rcu_register_thread();
1314
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +02001315 qemu_mutex_lock_iothread();
Andreas Färber814e6122012-05-02 17:00:37 +02001316 qemu_thread_get_self(cpu->thread);
Andreas Färber9f09e182012-05-03 06:59:07 +02001317 cpu->thread_id = qemu_get_thread_id();
Pavel Dovgalyuk626cf8f2014-12-08 10:53:17 +03001318 cpu->can_do_io = 1;
Alex Bennée37257942017-02-23 18:29:14 +00001319 current_cpu = cpu;
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +02001320
1321 sigemptyset(&waitset);
1322 sigaddset(&waitset, SIG_IPI);
1323
1324 /* signal CPU creation */
Andreas Färber61a46212012-05-02 22:49:36 +02001325 cpu->created = true;
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +02001326 qemu_cond_signal(&qemu_cpu_cond);
Richard Henderson9c09a252019-03-14 13:06:29 -07001327 qemu_guest_random_seed_thread_part2(cpu->random_seed);
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +02001328
Paolo Bonzinid2831ab2018-01-30 11:04:53 -05001329 do {
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +02001330 qemu_mutex_unlock_iothread();
1331 do {
1332 int sig;
1333 r = sigwait(&waitset, &sig);
1334 } while (r == -1 && (errno == EAGAIN || errno == EINTR));
1335 if (r == -1) {
1336 perror("sigwait");
1337 exit(1);
1338 }
1339 qemu_mutex_lock_iothread();
Paolo Bonzinidb08b682018-01-11 13:53:12 +01001340 qemu_wait_io_event(cpu);
Paolo Bonzinid2831ab2018-01-30 11:04:53 -05001341 } while (!cpu->unplug);
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +02001342
David Hildenbrandd40bfcb2019-02-18 10:21:57 +01001343 qemu_mutex_unlock_iothread();
Paolo Bonzinid2831ab2018-01-30 11:04:53 -05001344 rcu_unregister_thread();
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +02001345 return NULL;
1346#endif
1347}
1348
Alex Bennée1be7fcb2016-10-27 16:10:08 +01001349static int64_t tcg_get_icount_limit(void)
1350{
1351 int64_t deadline;
1352
1353 if (replay_mode != REPLAY_MODE_PLAY) {
1354 deadline = qemu_clock_deadline_ns_all(QEMU_CLOCK_VIRTUAL);
1355
1356 /* Maintain prior (possibly buggy) behaviour where if no deadline
1357 * was set (as there is no QEMU_CLOCK_VIRTUAL timer) or it is more than
1358 * INT32_MAX nanoseconds ahead, we still use INT32_MAX
1359 * nanoseconds.
1360 */
1361 if ((deadline < 0) || (deadline > INT32_MAX)) {
1362 deadline = INT32_MAX;
1363 }
1364
1365 return qemu_icount_round(deadline);
1366 } else {
1367 return replay_get_instructions();
1368 }
1369}
1370
Alex Bennée12e97002016-10-27 16:10:14 +01001371static void handle_icount_deadline(void)
1372{
Paolo Bonzini6b8f0182017-03-02 19:56:40 +01001373 assert(qemu_in_vcpu_thread());
Alex Bennée12e97002016-10-27 16:10:14 +01001374 if (use_icount) {
1375 int64_t deadline =
1376 qemu_clock_deadline_ns_all(QEMU_CLOCK_VIRTUAL);
1377
1378 if (deadline == 0) {
Paolo Bonzini6b8f0182017-03-02 19:56:40 +01001379 /* Wake up other AioContexts. */
Alex Bennée12e97002016-10-27 16:10:14 +01001380 qemu_clock_notify(QEMU_CLOCK_VIRTUAL);
Paolo Bonzini6b8f0182017-03-02 19:56:40 +01001381 qemu_clock_run_timers(QEMU_CLOCK_VIRTUAL);
Alex Bennée12e97002016-10-27 16:10:14 +01001382 }
1383 }
1384}
1385
Alex Bennée05248382017-03-29 16:46:59 +01001386static void prepare_icount_for_run(CPUState *cpu)
1387{
1388 if (use_icount) {
Alex Bennéeeda5f7c2017-04-05 12:35:48 +01001389 int insns_left;
Alex Bennée05248382017-03-29 16:46:59 +01001390
1391 /* These should always be cleared by process_icount_data after
1392 * each vCPU execution. However u16.high can be raised
1393 * asynchronously by cpu_exit/cpu_interrupt/tcg_handle_interrupt
1394 */
Richard Henderson5e140192019-03-28 11:54:23 -10001395 g_assert(cpu_neg(cpu)->icount_decr.u16.low == 0);
Alex Bennée05248382017-03-29 16:46:59 +01001396 g_assert(cpu->icount_extra == 0);
1397
Alex Bennéeeda5f7c2017-04-05 12:35:48 +01001398 cpu->icount_budget = tcg_get_icount_limit();
1399 insns_left = MIN(0xffff, cpu->icount_budget);
Richard Henderson5e140192019-03-28 11:54:23 -10001400 cpu_neg(cpu)->icount_decr.u16.low = insns_left;
Alex Bennéeeda5f7c2017-04-05 12:35:48 +01001401 cpu->icount_extra = cpu->icount_budget - insns_left;
Alex Bennéed759c952018-02-27 12:52:48 +03001402
1403 replay_mutex_lock();
Alex Bennée05248382017-03-29 16:46:59 +01001404 }
1405}
1406
1407static void process_icount_data(CPUState *cpu)
1408{
1409 if (use_icount) {
Alex Bennéee4cd9652017-03-31 16:09:42 +01001410 /* Account for executed instructions */
Alex Bennée512d3c82017-04-05 12:32:37 +01001411 cpu_update_icount(cpu);
Alex Bennée05248382017-03-29 16:46:59 +01001412
1413 /* Reset the counters */
Richard Henderson5e140192019-03-28 11:54:23 -10001414 cpu_neg(cpu)->icount_decr.u16.low = 0;
Alex Bennée05248382017-03-29 16:46:59 +01001415 cpu->icount_extra = 0;
Alex Bennéee4cd9652017-03-31 16:09:42 +01001416 cpu->icount_budget = 0;
1417
Alex Bennée05248382017-03-29 16:46:59 +01001418 replay_account_executed_instructions();
Alex Bennéed759c952018-02-27 12:52:48 +03001419
1420 replay_mutex_unlock();
Alex Bennée05248382017-03-29 16:46:59 +01001421 }
1422}
1423
1424
Alex Bennée1be7fcb2016-10-27 16:10:08 +01001425static int tcg_cpu_exec(CPUState *cpu)
1426{
1427 int ret;
1428#ifdef CONFIG_PROFILER
1429 int64_t ti;
1430#endif
1431
Emilio G. Cotaf28d0df2018-06-22 13:45:31 -04001432 assert(tcg_enabled());
Alex Bennée1be7fcb2016-10-27 16:10:08 +01001433#ifdef CONFIG_PROFILER
1434 ti = profile_getclock();
1435#endif
Alex Bennée1be7fcb2016-10-27 16:10:08 +01001436 cpu_exec_start(cpu);
1437 ret = cpu_exec(cpu);
1438 cpu_exec_end(cpu);
1439#ifdef CONFIG_PROFILER
Emilio G. Cota72fd2ef2018-10-10 10:48:53 -04001440 atomic_set(&tcg_ctx->prof.cpu_exec_time,
1441 tcg_ctx->prof.cpu_exec_time + profile_getclock() - ti);
Alex Bennée1be7fcb2016-10-27 16:10:08 +01001442#endif
Alex Bennée1be7fcb2016-10-27 16:10:08 +01001443 return ret;
1444}
1445
Alex Bennéec93bbbe2016-10-27 16:10:09 +01001446/* Destroy any remaining vCPUs which have been unplugged and have
1447 * finished running
1448 */
1449static void deal_with_unplugged_cpus(void)
Alex Bennée1be7fcb2016-10-27 16:10:08 +01001450{
Alex Bennéec93bbbe2016-10-27 16:10:09 +01001451 CPUState *cpu;
Alex Bennée1be7fcb2016-10-27 16:10:08 +01001452
Alex Bennéec93bbbe2016-10-27 16:10:09 +01001453 CPU_FOREACH(cpu) {
1454 if (cpu->unplug && !cpu_can_run(cpu)) {
1455 qemu_tcg_destroy_vcpu(cpu);
1456 cpu->created = false;
1457 qemu_cond_signal(&qemu_cpu_cond);
Alex Bennée1be7fcb2016-10-27 16:10:08 +01001458 break;
1459 }
1460 }
Alex Bennée1be7fcb2016-10-27 16:10:08 +01001461}
Jan Kiszkabdb7ca62011-09-26 09:40:39 +02001462
Alex Bennée65467062017-02-23 18:29:09 +00001463/* Single-threaded TCG
1464 *
1465 * In the single-threaded case each vCPU is simulated in turn. If
1466 * there is more than a single vCPU we create a simple timer to kick
1467 * the vCPU and ensure we don't get stuck in a tight loop in one vCPU.
1468 * This is done explicitly rather than relying on side-effects
1469 * elsewhere.
1470 */
1471
Alex Bennée37257942017-02-23 18:29:14 +00001472static void *qemu_tcg_rr_cpu_thread_fn(void *arg)
Blue Swirl296af7c2010-03-29 19:23:50 +00001473{
Andreas Färberc3586ba2012-05-03 01:41:24 +02001474 CPUState *cpu = arg;
Blue Swirl296af7c2010-03-29 19:23:50 +00001475
Emilio G. Cotaf28d0df2018-06-22 13:45:31 -04001476 assert(tcg_enabled());
Paolo Bonziniab28bd22015-07-09 08:55:38 +02001477 rcu_register_thread();
Emilio G. Cota3468b592017-07-19 18:57:58 -04001478 tcg_register_thread();
Paolo Bonziniab28bd22015-07-09 08:55:38 +02001479
Paolo Bonzini2e7f7a32015-06-18 18:47:18 +02001480 qemu_mutex_lock_iothread();
Andreas Färber814e6122012-05-02 17:00:37 +02001481 qemu_thread_get_self(cpu->thread);
Blue Swirl296af7c2010-03-29 19:23:50 +00001482
David Hildenbrand5a9c9732018-02-09 20:52:39 +01001483 cpu->thread_id = qemu_get_thread_id();
1484 cpu->created = true;
1485 cpu->can_do_io = 1;
Blue Swirl296af7c2010-03-29 19:23:50 +00001486 qemu_cond_signal(&qemu_cpu_cond);
Richard Henderson9c09a252019-03-14 13:06:29 -07001487 qemu_guest_random_seed_thread_part2(cpu->random_seed);
Blue Swirl296af7c2010-03-29 19:23:50 +00001488
Jan Kiszkafa7d1862011-08-22 18:35:25 +02001489 /* wait for initial kick-off after machine start */
Emilio G. Cotac28e3992015-04-27 12:45:28 -04001490 while (first_cpu->stopped) {
KONRAD Fredericd5f8d612015-08-10 17:27:06 +02001491 qemu_cond_wait(first_cpu->halt_cond, &qemu_global_mutex);
Jan Kiszka8e564b42012-02-17 18:31:15 +01001492
1493 /* process any pending work */
Andreas Färberbdc44642013-06-24 23:50:24 +02001494 CPU_FOREACH(cpu) {
Alex Bennée37257942017-02-23 18:29:14 +00001495 current_cpu = cpu;
Andreas Färber182735e2013-05-29 22:29:20 +02001496 qemu_wait_io_event_common(cpu);
Jan Kiszka8e564b42012-02-17 18:31:15 +01001497 }
Jan Kiszka0ab07c62011-02-07 12:19:14 +01001498 }
Blue Swirl296af7c2010-03-29 19:23:50 +00001499
Alex Bennée65467062017-02-23 18:29:09 +00001500 start_tcg_kick_timer();
1501
Alex Bennéec93bbbe2016-10-27 16:10:09 +01001502 cpu = first_cpu;
1503
Alex Bennéee5143e32017-02-23 18:29:12 +00001504 /* process any pending work */
1505 cpu->exit_request = 1;
1506
Blue Swirl296af7c2010-03-29 19:23:50 +00001507 while (1) {
Alex Bennéed759c952018-02-27 12:52:48 +03001508 qemu_mutex_unlock_iothread();
1509 replay_mutex_lock();
1510 qemu_mutex_lock_iothread();
Alex Bennéec93bbbe2016-10-27 16:10:09 +01001511 /* Account partial waits to QEMU_CLOCK_VIRTUAL. */
1512 qemu_account_warp_timer();
1513
Paolo Bonzini6b8f0182017-03-02 19:56:40 +01001514 /* Run the timers here. This is much more efficient than
1515 * waking up the I/O thread and waiting for completion.
1516 */
1517 handle_icount_deadline();
1518
Alex Bennéed759c952018-02-27 12:52:48 +03001519 replay_mutex_unlock();
1520
Alex Bennéec93bbbe2016-10-27 16:10:09 +01001521 if (!cpu) {
1522 cpu = first_cpu;
1523 }
1524
Alex Bennéee5143e32017-02-23 18:29:12 +00001525 while (cpu && !cpu->queued_work_first && !cpu->exit_request) {
1526
Alex Bennée791158d2017-02-23 18:29:10 +00001527 atomic_mb_set(&tcg_current_rr_cpu, cpu);
Alex Bennée37257942017-02-23 18:29:14 +00001528 current_cpu = cpu;
Alex Bennéec93bbbe2016-10-27 16:10:09 +01001529
1530 qemu_clock_enable(QEMU_CLOCK_VIRTUAL,
1531 (cpu->singlestep_enabled & SSTEP_NOTIMER) == 0);
1532
1533 if (cpu_can_run(cpu)) {
1534 int r;
Alex Bennée05248382017-03-29 16:46:59 +01001535
Alex Bennéed759c952018-02-27 12:52:48 +03001536 qemu_mutex_unlock_iothread();
Alex Bennée05248382017-03-29 16:46:59 +01001537 prepare_icount_for_run(cpu);
1538
Alex Bennéec93bbbe2016-10-27 16:10:09 +01001539 r = tcg_cpu_exec(cpu);
Alex Bennée05248382017-03-29 16:46:59 +01001540
1541 process_icount_data(cpu);
Alex Bennéed759c952018-02-27 12:52:48 +03001542 qemu_mutex_lock_iothread();
Alex Bennée05248382017-03-29 16:46:59 +01001543
Alex Bennéec93bbbe2016-10-27 16:10:09 +01001544 if (r == EXCP_DEBUG) {
1545 cpu_handle_guest_debug(cpu);
1546 break;
Pranith Kumar08e73c42017-02-23 18:29:15 +00001547 } else if (r == EXCP_ATOMIC) {
1548 qemu_mutex_unlock_iothread();
1549 cpu_exec_step_atomic(cpu);
1550 qemu_mutex_lock_iothread();
1551 break;
Alex Bennéec93bbbe2016-10-27 16:10:09 +01001552 }
Alex Bennée37257942017-02-23 18:29:14 +00001553 } else if (cpu->stop) {
Alex Bennéec93bbbe2016-10-27 16:10:09 +01001554 if (cpu->unplug) {
1555 cpu = CPU_NEXT(cpu);
1556 }
1557 break;
1558 }
1559
Alex Bennéee5143e32017-02-23 18:29:12 +00001560 cpu = CPU_NEXT(cpu);
1561 } /* while (cpu && !cpu->exit_request).. */
1562
Alex Bennée791158d2017-02-23 18:29:10 +00001563 /* Does not need atomic_mb_set because a spurious wakeup is okay. */
1564 atomic_set(&tcg_current_rr_cpu, NULL);
Alex Bennéec93bbbe2016-10-27 16:10:09 +01001565
Alex Bennéee5143e32017-02-23 18:29:12 +00001566 if (cpu && cpu->exit_request) {
1567 atomic_mb_set(&cpu->exit_request, 0);
1568 }
Alex Blighac70aaf2013-08-21 16:02:57 +01001569
Clement Deschamps013aabd2018-10-21 16:21:03 +02001570 if (use_icount && all_cpu_threads_idle()) {
1571 /*
1572 * When all cpus are sleeping (e.g in WFI), to avoid a deadlock
1573 * in the main_loop, wake it up in order to start the warp timer.
1574 */
1575 qemu_notify_event();
1576 }
1577
Paolo Bonzinia8efa602018-11-14 12:36:57 +01001578 qemu_tcg_rr_wait_io_event();
Alex Bennéec93bbbe2016-10-27 16:10:09 +01001579 deal_with_unplugged_cpus();
Blue Swirl296af7c2010-03-29 19:23:50 +00001580 }
1581
Paolo Bonzini9b0605f2018-01-30 11:05:06 -05001582 rcu_unregister_thread();
Blue Swirl296af7c2010-03-29 19:23:50 +00001583 return NULL;
1584}
1585
Vincent Palatinb0cb0a62017-01-10 11:59:57 +01001586static void *qemu_hax_cpu_thread_fn(void *arg)
1587{
1588 CPUState *cpu = arg;
1589 int r;
Vincent Palatinb3d3a422017-03-20 11:15:49 +01001590
Paolo Bonzini9857c2d2018-01-30 16:28:49 +01001591 rcu_register_thread();
Vincent Palatinb3d3a422017-03-20 11:15:49 +01001592 qemu_mutex_lock_iothread();
Vincent Palatinb0cb0a62017-01-10 11:59:57 +01001593 qemu_thread_get_self(cpu->thread);
Vincent Palatinb0cb0a62017-01-10 11:59:57 +01001594
1595 cpu->thread_id = qemu_get_thread_id();
1596 cpu->created = true;
1597 cpu->halted = 0;
1598 current_cpu = cpu;
1599
1600 hax_init_vcpu(cpu);
1601 qemu_cond_signal(&qemu_cpu_cond);
Richard Henderson9c09a252019-03-14 13:06:29 -07001602 qemu_guest_random_seed_thread_part2(cpu->random_seed);
Vincent Palatinb0cb0a62017-01-10 11:59:57 +01001603
Paolo Bonzini9857c2d2018-01-30 16:28:49 +01001604 do {
Vincent Palatinb0cb0a62017-01-10 11:59:57 +01001605 if (cpu_can_run(cpu)) {
1606 r = hax_smp_cpu_exec(cpu);
1607 if (r == EXCP_DEBUG) {
1608 cpu_handle_guest_debug(cpu);
1609 }
1610 }
1611
Paolo Bonzinidb08b682018-01-11 13:53:12 +01001612 qemu_wait_io_event(cpu);
Paolo Bonzini9857c2d2018-01-30 16:28:49 +01001613 } while (!cpu->unplug || cpu_can_run(cpu));
1614 rcu_unregister_thread();
Vincent Palatinb0cb0a62017-01-10 11:59:57 +01001615 return NULL;
1616}
1617
Sergio Andres Gomez Del Realc97d6d22017-09-13 04:05:09 -05001618/* The HVF-specific vCPU thread function. This one should only run when the host
1619 * CPU supports the VMX "unrestricted guest" feature. */
1620static void *qemu_hvf_cpu_thread_fn(void *arg)
1621{
1622 CPUState *cpu = arg;
1623
1624 int r;
1625
1626 assert(hvf_enabled());
1627
1628 rcu_register_thread();
1629
1630 qemu_mutex_lock_iothread();
1631 qemu_thread_get_self(cpu->thread);
1632
1633 cpu->thread_id = qemu_get_thread_id();
1634 cpu->can_do_io = 1;
1635 current_cpu = cpu;
1636
1637 hvf_init_vcpu(cpu);
1638
1639 /* signal CPU creation */
1640 cpu->created = true;
1641 qemu_cond_signal(&qemu_cpu_cond);
Richard Henderson9c09a252019-03-14 13:06:29 -07001642 qemu_guest_random_seed_thread_part2(cpu->random_seed);
Sergio Andres Gomez Del Realc97d6d22017-09-13 04:05:09 -05001643
1644 do {
1645 if (cpu_can_run(cpu)) {
1646 r = hvf_vcpu_exec(cpu);
1647 if (r == EXCP_DEBUG) {
1648 cpu_handle_guest_debug(cpu);
1649 }
1650 }
Paolo Bonzinidb08b682018-01-11 13:53:12 +01001651 qemu_wait_io_event(cpu);
Sergio Andres Gomez Del Realc97d6d22017-09-13 04:05:09 -05001652 } while (!cpu->unplug || cpu_can_run(cpu));
1653
1654 hvf_vcpu_destroy(cpu);
1655 cpu->created = false;
1656 qemu_cond_signal(&qemu_cpu_cond);
1657 qemu_mutex_unlock_iothread();
Paolo Bonzini8178e632018-01-30 11:05:21 -05001658 rcu_unregister_thread();
Sergio Andres Gomez Del Realc97d6d22017-09-13 04:05:09 -05001659 return NULL;
1660}
1661
Justin Terry (VM)19306802018-01-22 13:07:49 -08001662static void *qemu_whpx_cpu_thread_fn(void *arg)
1663{
1664 CPUState *cpu = arg;
1665 int r;
1666
1667 rcu_register_thread();
1668
1669 qemu_mutex_lock_iothread();
1670 qemu_thread_get_self(cpu->thread);
1671 cpu->thread_id = qemu_get_thread_id();
1672 current_cpu = cpu;
1673
1674 r = whpx_init_vcpu(cpu);
1675 if (r < 0) {
1676 fprintf(stderr, "whpx_init_vcpu failed: %s\n", strerror(-r));
1677 exit(1);
1678 }
1679
1680 /* signal CPU creation */
1681 cpu->created = true;
1682 qemu_cond_signal(&qemu_cpu_cond);
Richard Henderson9c09a252019-03-14 13:06:29 -07001683 qemu_guest_random_seed_thread_part2(cpu->random_seed);
Justin Terry (VM)19306802018-01-22 13:07:49 -08001684
1685 do {
1686 if (cpu_can_run(cpu)) {
1687 r = whpx_vcpu_exec(cpu);
1688 if (r == EXCP_DEBUG) {
1689 cpu_handle_guest_debug(cpu);
1690 }
1691 }
1692 while (cpu_thread_is_idle(cpu)) {
1693 qemu_cond_wait(cpu->halt_cond, &qemu_global_mutex);
1694 }
1695 qemu_wait_io_event_common(cpu);
1696 } while (!cpu->unplug || cpu_can_run(cpu));
1697
1698 whpx_destroy_vcpu(cpu);
1699 cpu->created = false;
1700 qemu_cond_signal(&qemu_cpu_cond);
1701 qemu_mutex_unlock_iothread();
1702 rcu_unregister_thread();
Vincent Palatinb0cb0a62017-01-10 11:59:57 +01001703 return NULL;
1704}
1705
1706#ifdef _WIN32
1707static void CALLBACK dummy_apc_func(ULONG_PTR unused)
1708{
1709}
1710#endif
1711
Alex Bennée37257942017-02-23 18:29:14 +00001712/* Multi-threaded TCG
1713 *
1714 * In the multi-threaded case each vCPU has its own thread. The TLS
1715 * variable current_cpu can be used deep in the code to find the
1716 * current CPUState for a given thread.
1717 */
1718
1719static void *qemu_tcg_cpu_thread_fn(void *arg)
1720{
1721 CPUState *cpu = arg;
1722
Emilio G. Cotaf28d0df2018-06-22 13:45:31 -04001723 assert(tcg_enabled());
Alex Bennéebf51c722017-03-30 18:32:29 +01001724 g_assert(!use_icount);
1725
Alex Bennée37257942017-02-23 18:29:14 +00001726 rcu_register_thread();
Emilio G. Cota3468b592017-07-19 18:57:58 -04001727 tcg_register_thread();
Alex Bennée37257942017-02-23 18:29:14 +00001728
1729 qemu_mutex_lock_iothread();
1730 qemu_thread_get_self(cpu->thread);
1731
1732 cpu->thread_id = qemu_get_thread_id();
1733 cpu->created = true;
1734 cpu->can_do_io = 1;
1735 current_cpu = cpu;
1736 qemu_cond_signal(&qemu_cpu_cond);
Richard Henderson9c09a252019-03-14 13:06:29 -07001737 qemu_guest_random_seed_thread_part2(cpu->random_seed);
Alex Bennée37257942017-02-23 18:29:14 +00001738
1739 /* process any pending work */
1740 cpu->exit_request = 1;
1741
Cédric Le Goater54961aa2018-04-25 15:18:28 +02001742 do {
Alex Bennée37257942017-02-23 18:29:14 +00001743 if (cpu_can_run(cpu)) {
1744 int r;
Alex Bennéed759c952018-02-27 12:52:48 +03001745 qemu_mutex_unlock_iothread();
Alex Bennée37257942017-02-23 18:29:14 +00001746 r = tcg_cpu_exec(cpu);
Alex Bennéed759c952018-02-27 12:52:48 +03001747 qemu_mutex_lock_iothread();
Alex Bennée37257942017-02-23 18:29:14 +00001748 switch (r) {
1749 case EXCP_DEBUG:
1750 cpu_handle_guest_debug(cpu);
1751 break;
1752 case EXCP_HALTED:
1753 /* during start-up the vCPU is reset and the thread is
1754 * kicked several times. If we don't ensure we go back
1755 * to sleep in the halted state we won't cleanly
1756 * start-up when the vCPU is enabled.
1757 *
1758 * cpu->halted should ensure we sleep in wait_io_event
1759 */
1760 g_assert(cpu->halted);
1761 break;
Pranith Kumar08e73c42017-02-23 18:29:15 +00001762 case EXCP_ATOMIC:
1763 qemu_mutex_unlock_iothread();
1764 cpu_exec_step_atomic(cpu);
1765 qemu_mutex_lock_iothread();
Alex Bennée37257942017-02-23 18:29:14 +00001766 default:
1767 /* Ignore everything else? */
1768 break;
1769 }
1770 }
1771
Alex Bennée37257942017-02-23 18:29:14 +00001772 atomic_mb_set(&cpu->exit_request, 0);
Paolo Bonzinidb08b682018-01-11 13:53:12 +01001773 qemu_wait_io_event(cpu);
Paolo Bonzini9b0605f2018-01-30 11:05:06 -05001774 } while (!cpu->unplug || cpu_can_run(cpu));
Alex Bennée37257942017-02-23 18:29:14 +00001775
Paolo Bonzini9b0605f2018-01-30 11:05:06 -05001776 qemu_tcg_destroy_vcpu(cpu);
1777 cpu->created = false;
1778 qemu_cond_signal(&qemu_cpu_cond);
1779 qemu_mutex_unlock_iothread();
1780 rcu_unregister_thread();
Alex Bennée37257942017-02-23 18:29:14 +00001781 return NULL;
1782}
1783
Andreas Färber2ff09a42012-05-03 00:23:30 +02001784static void qemu_cpu_kick_thread(CPUState *cpu)
Paolo Bonzinicc015e92011-03-12 17:44:08 +01001785{
1786#ifndef _WIN32
1787 int err;
1788
Paolo Bonzinie0c38212015-08-26 00:19:19 +02001789 if (cpu->thread_kicked) {
1790 return;
Paolo Bonzini9102ded2015-08-18 06:52:09 -07001791 }
Paolo Bonzinie0c38212015-08-26 00:19:19 +02001792 cpu->thread_kicked = true;
Andreas Färber814e6122012-05-02 17:00:37 +02001793 err = pthread_kill(cpu->thread->thread, SIG_IPI);
Laurent Vivierd455ebc2019-01-02 15:16:03 +01001794 if (err && err != ESRCH) {
Paolo Bonzinicc015e92011-03-12 17:44:08 +01001795 fprintf(stderr, "qemu:%s: %s", __func__, strerror(err));
1796 exit(1);
1797 }
1798#else /* _WIN32 */
Vincent Palatinb0cb0a62017-01-10 11:59:57 +01001799 if (!qemu_cpu_is_self(cpu)) {
Justin Terry (VM)19306802018-01-22 13:07:49 -08001800 if (whpx_enabled()) {
1801 whpx_vcpu_kick(cpu);
1802 } else if (!QueueUserAPC(dummy_apc_func, cpu->hThread, 0)) {
Vincent Palatinb0cb0a62017-01-10 11:59:57 +01001803 fprintf(stderr, "%s: QueueUserAPC failed with error %lu\n",
1804 __func__, GetLastError());
1805 exit(1);
1806 }
1807 }
Paolo Bonzinicc015e92011-03-12 17:44:08 +01001808#endif
1809}
1810
Andreas Färberc08d7422012-05-03 04:34:15 +02001811void qemu_cpu_kick(CPUState *cpu)
Blue Swirl296af7c2010-03-29 19:23:50 +00001812{
Andreas Färberf5c121b2012-05-03 01:22:49 +02001813 qemu_cond_broadcast(cpu->halt_cond);
Paolo Bonzinie0c38212015-08-26 00:19:19 +02001814 if (tcg_enabled()) {
Alex Bennée791158d2017-02-23 18:29:10 +00001815 cpu_exit(cpu);
Alex Bennée37257942017-02-23 18:29:14 +00001816 /* NOP unless doing single-thread RR */
Alex Bennée791158d2017-02-23 18:29:10 +00001817 qemu_cpu_kick_rr_cpu();
Paolo Bonzinie0c38212015-08-26 00:19:19 +02001818 } else {
Vincent Palatinb0cb0a62017-01-10 11:59:57 +01001819 if (hax_enabled()) {
1820 /*
1821 * FIXME: race condition with the exit_request check in
1822 * hax_vcpu_hax_exec
1823 */
1824 cpu->exit_request = 1;
1825 }
Paolo Bonzinie0c38212015-08-26 00:19:19 +02001826 qemu_cpu_kick_thread(cpu);
1827 }
Blue Swirl296af7c2010-03-29 19:23:50 +00001828}
1829
Jan Kiszka46d62fa2011-02-01 22:15:59 +01001830void qemu_cpu_kick_self(void)
1831{
Andreas Färber4917cf42013-05-27 05:17:50 +02001832 assert(current_cpu);
Paolo Bonzini9102ded2015-08-18 06:52:09 -07001833 qemu_cpu_kick_thread(current_cpu);
Blue Swirl296af7c2010-03-29 19:23:50 +00001834}
1835
Andreas Färber60e82572012-05-02 22:23:49 +02001836bool qemu_cpu_is_self(CPUState *cpu)
Blue Swirl296af7c2010-03-29 19:23:50 +00001837{
Andreas Färber814e6122012-05-02 17:00:37 +02001838 return qemu_thread_is_self(cpu->thread);
Blue Swirl296af7c2010-03-29 19:23:50 +00001839}
1840
Paolo Bonzini79e2b9a2015-01-21 12:09:14 +01001841bool qemu_in_vcpu_thread(void)
Juan Quintelaaa723c22012-09-18 16:30:11 +02001842{
Andreas Färber4917cf42013-05-27 05:17:50 +02001843 return current_cpu && qemu_cpu_is_self(current_cpu);
Juan Quintelaaa723c22012-09-18 16:30:11 +02001844}
1845
Paolo Bonziniafbe7052015-06-18 18:47:19 +02001846static __thread bool iothread_locked = false;
1847
1848bool qemu_mutex_iothread_locked(void)
1849{
1850 return iothread_locked;
1851}
1852
Emilio G. Cotacb764d02017-10-28 02:16:41 -04001853/*
1854 * The BQL is taken from so many places that it is worth profiling the
1855 * callers directly, instead of funneling them all through a single function.
1856 */
1857void qemu_mutex_lock_iothread_impl(const char *file, int line)
Blue Swirl296af7c2010-03-29 19:23:50 +00001858{
Emilio G. Cotacb764d02017-10-28 02:16:41 -04001859 QemuMutexLockFunc bql_lock = atomic_read(&qemu_bql_mutex_lock_func);
1860
Jan Kiszka8d04fb52017-02-23 18:29:11 +00001861 g_assert(!qemu_mutex_iothread_locked());
Emilio G. Cotacb764d02017-10-28 02:16:41 -04001862 bql_lock(&qemu_global_mutex, file, line);
Paolo Bonziniafbe7052015-06-18 18:47:19 +02001863 iothread_locked = true;
Blue Swirl296af7c2010-03-29 19:23:50 +00001864}
1865
1866void qemu_mutex_unlock_iothread(void)
1867{
Jan Kiszka8d04fb52017-02-23 18:29:11 +00001868 g_assert(qemu_mutex_iothread_locked());
Paolo Bonziniafbe7052015-06-18 18:47:19 +02001869 iothread_locked = false;
Blue Swirl296af7c2010-03-29 19:23:50 +00001870 qemu_mutex_unlock(&qemu_global_mutex);
1871}
1872
Alex Bennéee8faee02016-10-27 16:09:58 +01001873static bool all_vcpus_paused(void)
Blue Swirl296af7c2010-03-29 19:23:50 +00001874{
Andreas Färberbdc44642013-06-24 23:50:24 +02001875 CPUState *cpu;
Blue Swirl296af7c2010-03-29 19:23:50 +00001876
Andreas Färberbdc44642013-06-24 23:50:24 +02001877 CPU_FOREACH(cpu) {
Andreas Färber182735e2013-05-29 22:29:20 +02001878 if (!cpu->stopped) {
Alex Bennéee8faee02016-10-27 16:09:58 +01001879 return false;
Jan Kiszka0ab07c62011-02-07 12:19:14 +01001880 }
Blue Swirl296af7c2010-03-29 19:23:50 +00001881 }
1882
Alex Bennéee8faee02016-10-27 16:09:58 +01001883 return true;
Blue Swirl296af7c2010-03-29 19:23:50 +00001884}
1885
1886void pause_all_vcpus(void)
1887{
Andreas Färberbdc44642013-06-24 23:50:24 +02001888 CPUState *cpu;
Blue Swirl296af7c2010-03-29 19:23:50 +00001889
Alex Bligh40daca52013-08-21 16:03:02 +01001890 qemu_clock_enable(QEMU_CLOCK_VIRTUAL, false);
Andreas Färberbdc44642013-06-24 23:50:24 +02001891 CPU_FOREACH(cpu) {
David Hildenbrandebd05fe2017-11-29 20:12:15 +01001892 if (qemu_cpu_is_self(cpu)) {
1893 qemu_cpu_stop(cpu, true);
1894 } else {
1895 cpu->stop = true;
1896 qemu_cpu_kick(cpu);
1897 }
Jan Kiszkad798e972012-02-17 18:31:16 +01001898 }
1899
Alex Bennéed759c952018-02-27 12:52:48 +03001900 /* We need to drop the replay_lock so any vCPU threads woken up
1901 * can finish their replay tasks
1902 */
1903 replay_mutex_unlock();
1904
Blue Swirl296af7c2010-03-29 19:23:50 +00001905 while (!all_vcpus_paused()) {
Paolo Bonzinibe7d6c52011-03-12 17:44:02 +01001906 qemu_cond_wait(&qemu_pause_cond, &qemu_global_mutex);
Andreas Färberbdc44642013-06-24 23:50:24 +02001907 CPU_FOREACH(cpu) {
Andreas Färber182735e2013-05-29 22:29:20 +02001908 qemu_cpu_kick(cpu);
Blue Swirl296af7c2010-03-29 19:23:50 +00001909 }
1910 }
Alex Bennéed759c952018-02-27 12:52:48 +03001911
1912 qemu_mutex_unlock_iothread();
1913 replay_mutex_lock();
1914 qemu_mutex_lock_iothread();
Blue Swirl296af7c2010-03-29 19:23:50 +00001915}
1916
Igor Mammedov29936832013-04-23 10:29:37 +02001917void cpu_resume(CPUState *cpu)
1918{
1919 cpu->stop = false;
1920 cpu->stopped = false;
1921 qemu_cpu_kick(cpu);
1922}
1923
Blue Swirl296af7c2010-03-29 19:23:50 +00001924void resume_all_vcpus(void)
1925{
Andreas Färberbdc44642013-06-24 23:50:24 +02001926 CPUState *cpu;
Blue Swirl296af7c2010-03-29 19:23:50 +00001927
Alex Bligh40daca52013-08-21 16:03:02 +01001928 qemu_clock_enable(QEMU_CLOCK_VIRTUAL, true);
Andreas Färberbdc44642013-06-24 23:50:24 +02001929 CPU_FOREACH(cpu) {
Andreas Färber182735e2013-05-29 22:29:20 +02001930 cpu_resume(cpu);
Blue Swirl296af7c2010-03-29 19:23:50 +00001931 }
1932}
1933
Paolo Bonzinidbadee42018-01-30 16:40:12 +01001934void cpu_remove_sync(CPUState *cpu)
Gu Zheng4c055ab2016-05-12 09:18:13 +05301935{
1936 cpu->stop = true;
1937 cpu->unplug = true;
1938 qemu_cpu_kick(cpu);
Paolo Bonzinidbadee42018-01-30 16:40:12 +01001939 qemu_mutex_unlock_iothread();
1940 qemu_thread_join(cpu->thread);
1941 qemu_mutex_lock_iothread();
Bharata B Rao2c579042016-05-12 09:18:14 +05301942}
1943
Dr. David Alan Gilbert49001162014-01-30 10:20:32 +00001944/* For temporary buffers for forming a name */
1945#define VCPU_THREAD_NAME_SIZE 16
1946
Andreas Färbere5ab30a2012-05-03 01:50:44 +02001947static void qemu_tcg_init_vcpu(CPUState *cpu)
Blue Swirl296af7c2010-03-29 19:23:50 +00001948{
Dr. David Alan Gilbert49001162014-01-30 10:20:32 +00001949 char thread_name[VCPU_THREAD_NAME_SIZE];
Alex Bennée37257942017-02-23 18:29:14 +00001950 static QemuCond *single_tcg_halt_cond;
1951 static QemuThread *single_tcg_cpu_thread;
Emilio G. Cotae8feb962017-07-07 19:24:20 -04001952 static int tcg_region_inited;
1953
Emilio G. Cotaf28d0df2018-06-22 13:45:31 -04001954 assert(tcg_enabled());
Emilio G. Cotae8feb962017-07-07 19:24:20 -04001955 /*
1956 * Initialize TCG regions--once. Now is a good time, because:
1957 * (1) TCG's init context, prologue and target globals have been set up.
1958 * (2) qemu_tcg_mttcg_enabled() works now (TCG init code runs before the
1959 * -accel flag is processed, so the check doesn't work then).
1960 */
1961 if (!tcg_region_inited) {
1962 tcg_region_inited = 1;
1963 tcg_region_init();
1964 }
Dr. David Alan Gilbert49001162014-01-30 10:20:32 +00001965
Alex Bennée37257942017-02-23 18:29:14 +00001966 if (qemu_tcg_mttcg_enabled() || !single_tcg_cpu_thread) {
Andreas Färber814e6122012-05-02 17:00:37 +02001967 cpu->thread = g_malloc0(sizeof(QemuThread));
Andreas Färberf5c121b2012-05-03 01:22:49 +02001968 cpu->halt_cond = g_malloc0(sizeof(QemuCond));
1969 qemu_cond_init(cpu->halt_cond);
Alex Bennée37257942017-02-23 18:29:14 +00001970
1971 if (qemu_tcg_mttcg_enabled()) {
1972 /* create a thread per vCPU with TCG (MTTCG) */
1973 parallel_cpus = true;
1974 snprintf(thread_name, VCPU_THREAD_NAME_SIZE, "CPU %d/TCG",
Dr. David Alan Gilbert49001162014-01-30 10:20:32 +00001975 cpu->cpu_index);
Alex Bennée37257942017-02-23 18:29:14 +00001976
1977 qemu_thread_create(cpu->thread, thread_name, qemu_tcg_cpu_thread_fn,
1978 cpu, QEMU_THREAD_JOINABLE);
1979
1980 } else {
1981 /* share a single thread for all cpus with TCG */
1982 snprintf(thread_name, VCPU_THREAD_NAME_SIZE, "ALL CPUs/TCG");
1983 qemu_thread_create(cpu->thread, thread_name,
1984 qemu_tcg_rr_cpu_thread_fn,
1985 cpu, QEMU_THREAD_JOINABLE);
1986
1987 single_tcg_halt_cond = cpu->halt_cond;
1988 single_tcg_cpu_thread = cpu->thread;
1989 }
Paolo Bonzini1ecf47b2011-12-13 13:43:52 +01001990#ifdef _WIN32
Andreas Färber814e6122012-05-02 17:00:37 +02001991 cpu->hThread = qemu_thread_get_handle(cpu->thread);
Paolo Bonzini1ecf47b2011-12-13 13:43:52 +01001992#endif
Blue Swirl296af7c2010-03-29 19:23:50 +00001993 } else {
Alex Bennée37257942017-02-23 18:29:14 +00001994 /* For non-MTTCG cases we share the thread */
1995 cpu->thread = single_tcg_cpu_thread;
1996 cpu->halt_cond = single_tcg_halt_cond;
David Hildenbranda3421732018-02-09 20:52:37 +01001997 cpu->thread_id = first_cpu->thread_id;
1998 cpu->can_do_io = 1;
1999 cpu->created = true;
Blue Swirl296af7c2010-03-29 19:23:50 +00002000 }
2001}
2002
Vincent Palatinb0cb0a62017-01-10 11:59:57 +01002003static void qemu_hax_start_vcpu(CPUState *cpu)
2004{
2005 char thread_name[VCPU_THREAD_NAME_SIZE];
2006
2007 cpu->thread = g_malloc0(sizeof(QemuThread));
2008 cpu->halt_cond = g_malloc0(sizeof(QemuCond));
2009 qemu_cond_init(cpu->halt_cond);
2010
2011 snprintf(thread_name, VCPU_THREAD_NAME_SIZE, "CPU %d/HAX",
2012 cpu->cpu_index);
2013 qemu_thread_create(cpu->thread, thread_name, qemu_hax_cpu_thread_fn,
2014 cpu, QEMU_THREAD_JOINABLE);
2015#ifdef _WIN32
2016 cpu->hThread = qemu_thread_get_handle(cpu->thread);
2017#endif
Vincent Palatinb0cb0a62017-01-10 11:59:57 +01002018}
2019
Andreas Färber48a106b2013-05-27 02:20:39 +02002020static void qemu_kvm_start_vcpu(CPUState *cpu)
Blue Swirl296af7c2010-03-29 19:23:50 +00002021{
Dr. David Alan Gilbert49001162014-01-30 10:20:32 +00002022 char thread_name[VCPU_THREAD_NAME_SIZE];
2023
Andreas Färber814e6122012-05-02 17:00:37 +02002024 cpu->thread = g_malloc0(sizeof(QemuThread));
Andreas Färberf5c121b2012-05-03 01:22:49 +02002025 cpu->halt_cond = g_malloc0(sizeof(QemuCond));
2026 qemu_cond_init(cpu->halt_cond);
Dr. David Alan Gilbert49001162014-01-30 10:20:32 +00002027 snprintf(thread_name, VCPU_THREAD_NAME_SIZE, "CPU %d/KVM",
2028 cpu->cpu_index);
2029 qemu_thread_create(cpu->thread, thread_name, qemu_kvm_cpu_thread_fn,
2030 cpu, QEMU_THREAD_JOINABLE);
Blue Swirl296af7c2010-03-29 19:23:50 +00002031}
2032
Sergio Andres Gomez Del Realc97d6d22017-09-13 04:05:09 -05002033static void qemu_hvf_start_vcpu(CPUState *cpu)
2034{
2035 char thread_name[VCPU_THREAD_NAME_SIZE];
2036
2037 /* HVF currently does not support TCG, and only runs in
2038 * unrestricted-guest mode. */
2039 assert(hvf_enabled());
2040
2041 cpu->thread = g_malloc0(sizeof(QemuThread));
2042 cpu->halt_cond = g_malloc0(sizeof(QemuCond));
2043 qemu_cond_init(cpu->halt_cond);
2044
2045 snprintf(thread_name, VCPU_THREAD_NAME_SIZE, "CPU %d/HVF",
2046 cpu->cpu_index);
2047 qemu_thread_create(cpu->thread, thread_name, qemu_hvf_cpu_thread_fn,
2048 cpu, QEMU_THREAD_JOINABLE);
Sergio Andres Gomez Del Realc97d6d22017-09-13 04:05:09 -05002049}
2050
Justin Terry (VM)19306802018-01-22 13:07:49 -08002051static void qemu_whpx_start_vcpu(CPUState *cpu)
2052{
2053 char thread_name[VCPU_THREAD_NAME_SIZE];
2054
2055 cpu->thread = g_malloc0(sizeof(QemuThread));
2056 cpu->halt_cond = g_malloc0(sizeof(QemuCond));
2057 qemu_cond_init(cpu->halt_cond);
2058 snprintf(thread_name, VCPU_THREAD_NAME_SIZE, "CPU %d/WHPX",
2059 cpu->cpu_index);
2060 qemu_thread_create(cpu->thread, thread_name, qemu_whpx_cpu_thread_fn,
2061 cpu, QEMU_THREAD_JOINABLE);
2062#ifdef _WIN32
2063 cpu->hThread = qemu_thread_get_handle(cpu->thread);
2064#endif
Justin Terry (VM)19306802018-01-22 13:07:49 -08002065}
2066
Andreas Färber10a90212013-05-27 02:24:35 +02002067static void qemu_dummy_start_vcpu(CPUState *cpu)
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +02002068{
Dr. David Alan Gilbert49001162014-01-30 10:20:32 +00002069 char thread_name[VCPU_THREAD_NAME_SIZE];
2070
Andreas Färber814e6122012-05-02 17:00:37 +02002071 cpu->thread = g_malloc0(sizeof(QemuThread));
Andreas Färberf5c121b2012-05-03 01:22:49 +02002072 cpu->halt_cond = g_malloc0(sizeof(QemuCond));
2073 qemu_cond_init(cpu->halt_cond);
Dr. David Alan Gilbert49001162014-01-30 10:20:32 +00002074 snprintf(thread_name, VCPU_THREAD_NAME_SIZE, "CPU %d/DUMMY",
2075 cpu->cpu_index);
2076 qemu_thread_create(cpu->thread, thread_name, qemu_dummy_cpu_thread_fn, cpu,
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +02002077 QEMU_THREAD_JOINABLE);
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +02002078}
2079
Andreas Färberc643bed2013-05-27 03:23:24 +02002080void qemu_init_vcpu(CPUState *cpu)
Blue Swirl296af7c2010-03-29 19:23:50 +00002081{
Andreas Färberce3960e2012-12-17 03:27:07 +01002082 cpu->nr_cores = smp_cores;
2083 cpu->nr_threads = smp_threads;
Andreas Färberf324e762012-05-02 23:26:21 +02002084 cpu->stopped = true;
Richard Henderson9c09a252019-03-14 13:06:29 -07002085 cpu->random_seed = qemu_guest_random_seed_thread_part1();
Peter Maydell56943e82016-01-21 14:15:04 +00002086
2087 if (!cpu->as) {
2088 /* If the target cpu hasn't set up any address spaces itself,
2089 * give it the default one.
2090 */
Peter Maydell12ebc9a2016-01-21 14:15:04 +00002091 cpu->num_ases = 1;
Peter Xu80ceb072017-11-23 17:23:32 +08002092 cpu_address_space_init(cpu, 0, "cpu-memory", cpu->memory);
Peter Maydell56943e82016-01-21 14:15:04 +00002093 }
2094
Jan Kiszka0ab07c62011-02-07 12:19:14 +01002095 if (kvm_enabled()) {
Andreas Färber48a106b2013-05-27 02:20:39 +02002096 qemu_kvm_start_vcpu(cpu);
Vincent Palatinb0cb0a62017-01-10 11:59:57 +01002097 } else if (hax_enabled()) {
2098 qemu_hax_start_vcpu(cpu);
Sergio Andres Gomez Del Realc97d6d22017-09-13 04:05:09 -05002099 } else if (hvf_enabled()) {
2100 qemu_hvf_start_vcpu(cpu);
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +02002101 } else if (tcg_enabled()) {
Andreas Färbere5ab30a2012-05-03 01:50:44 +02002102 qemu_tcg_init_vcpu(cpu);
Justin Terry (VM)19306802018-01-22 13:07:49 -08002103 } else if (whpx_enabled()) {
2104 qemu_whpx_start_vcpu(cpu);
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +02002105 } else {
Andreas Färber10a90212013-05-27 02:24:35 +02002106 qemu_dummy_start_vcpu(cpu);
Jan Kiszka0ab07c62011-02-07 12:19:14 +01002107 }
David Hildenbrand81e96312018-02-09 20:52:38 +01002108
2109 while (!cpu->created) {
2110 qemu_cond_wait(&qemu_cpu_cond, &qemu_global_mutex);
2111 }
Blue Swirl296af7c2010-03-29 19:23:50 +00002112}
2113
Jan Kiszkab4a3d962011-02-01 22:15:43 +01002114void cpu_stop_current(void)
Blue Swirl296af7c2010-03-29 19:23:50 +00002115{
Andreas Färber4917cf42013-05-27 05:17:50 +02002116 if (current_cpu) {
Peter Maydell0ec7e672019-01-07 15:23:47 +00002117 current_cpu->stop = true;
2118 cpu_exit(current_cpu);
Jan Kiszkab4a3d962011-02-01 22:15:43 +01002119 }
Blue Swirl296af7c2010-03-29 19:23:50 +00002120}
2121
Kevin Wolf56983462013-07-05 13:49:54 +02002122int vm_stop(RunState state)
Blue Swirl296af7c2010-03-29 19:23:50 +00002123{
Juan Quintelaaa723c22012-09-18 16:30:11 +02002124 if (qemu_in_vcpu_thread()) {
Paolo Bonzini74892d22014-06-05 14:53:58 +02002125 qemu_system_vmstop_request_prepare();
Luiz Capitulino1dfb4dd2011-07-29 14:26:33 -03002126 qemu_system_vmstop_request(state);
Blue Swirl296af7c2010-03-29 19:23:50 +00002127 /*
2128 * FIXME: should not return to device code in case
2129 * vm_stop() has been requested.
2130 */
Jan Kiszkab4a3d962011-02-01 22:15:43 +01002131 cpu_stop_current();
Kevin Wolf56983462013-07-05 13:49:54 +02002132 return 0;
Blue Swirl296af7c2010-03-29 19:23:50 +00002133 }
Kevin Wolf56983462013-07-05 13:49:54 +02002134
Stefan Hajnoczi4486e892018-03-07 14:42:05 +00002135 return do_vm_stop(state, true);
Blue Swirl296af7c2010-03-29 19:23:50 +00002136}
2137
Claudio Imbrenda2d76e822017-02-14 18:07:47 +01002138/**
2139 * Prepare for (re)starting the VM.
2140 * Returns -1 if the vCPUs are not to be restarted (e.g. if they are already
2141 * running or in case of an error condition), 0 otherwise.
2142 */
2143int vm_prepare_start(void)
2144{
2145 RunState requested;
Claudio Imbrenda2d76e822017-02-14 18:07:47 +01002146
2147 qemu_vmstop_requested(&requested);
2148 if (runstate_is_running() && requested == RUN_STATE__MAX) {
2149 return -1;
2150 }
2151
2152 /* Ensure that a STOP/RESUME pair of events is emitted if a
2153 * vmstop request was pending. The BLOCK_IO_ERROR event, for
2154 * example, according to documentation is always followed by
2155 * the STOP event.
2156 */
2157 if (runstate_is_running()) {
Peter Xu3ab72382018-08-15 21:37:37 +08002158 qapi_event_send_stop();
2159 qapi_event_send_resume();
Markus Armbrusterf0561582018-04-23 10:45:18 +02002160 return -1;
Claudio Imbrenda2d76e822017-02-14 18:07:47 +01002161 }
2162
2163 /* We are sending this now, but the CPUs will be resumed shortly later */
Peter Xu3ab72382018-08-15 21:37:37 +08002164 qapi_event_send_resume();
Markus Armbrusterf0561582018-04-23 10:45:18 +02002165
2166 replay_enable_events();
2167 cpu_enable_ticks();
2168 runstate_set(RUN_STATE_RUNNING);
2169 vm_state_notify(1, RUN_STATE_RUNNING);
2170 return 0;
Claudio Imbrenda2d76e822017-02-14 18:07:47 +01002171}
2172
2173void vm_start(void)
2174{
2175 if (!vm_prepare_start()) {
2176 resume_all_vcpus();
2177 }
2178}
2179
Luiz Capitulino8a9236f2011-10-14 11:18:09 -03002180/* does a state transition even if the VM is already stopped,
2181 current state is forgotten forever */
Kevin Wolf56983462013-07-05 13:49:54 +02002182int vm_stop_force_state(RunState state)
Luiz Capitulino8a9236f2011-10-14 11:18:09 -03002183{
2184 if (runstate_is_running()) {
Kevin Wolf56983462013-07-05 13:49:54 +02002185 return vm_stop(state);
Luiz Capitulino8a9236f2011-10-14 11:18:09 -03002186 } else {
2187 runstate_set(state);
Wen Congyangb2780d32015-11-20 17:34:38 +08002188
2189 bdrv_drain_all();
Kevin Wolf594a45c2013-07-18 14:52:19 +02002190 /* Make sure to return an error if the flush in a previous vm_stop()
2191 * failed. */
John Snow22af08e2016-09-22 21:45:51 -04002192 return bdrv_flush_all();
Luiz Capitulino8a9236f2011-10-14 11:18:09 -03002193 }
2194}
2195
Markus Armbruster04424282019-04-17 21:17:57 +02002196void list_cpus(const char *optarg)
Blue Swirl262353c2010-05-04 19:55:35 +00002197{
2198 /* XXX: implement xxx_cpu_list for targets that still miss it */
Peter Maydelle916cbf2012-09-05 17:41:08 -03002199#if defined(cpu_list)
Markus Armbruster04424282019-04-17 21:17:57 +02002200 cpu_list();
Blue Swirl262353c2010-05-04 19:55:35 +00002201#endif
2202}
Luiz Capitulinode0b36b2011-09-21 16:38:35 -03002203
2204CpuInfoList *qmp_query_cpus(Error **errp)
2205{
Igor Mammedovafed5a52017-05-10 13:29:55 +02002206 MachineState *ms = MACHINE(qdev_get_machine());
2207 MachineClass *mc = MACHINE_GET_CLASS(ms);
Luiz Capitulinode0b36b2011-09-21 16:38:35 -03002208 CpuInfoList *head = NULL, *cur_item = NULL;
Andreas Färber182735e2013-05-29 22:29:20 +02002209 CPUState *cpu;
Luiz Capitulinode0b36b2011-09-21 16:38:35 -03002210
Andreas Färberbdc44642013-06-24 23:50:24 +02002211 CPU_FOREACH(cpu) {
Luiz Capitulinode0b36b2011-09-21 16:38:35 -03002212 CpuInfoList *info;
Andreas Färber182735e2013-05-29 22:29:20 +02002213#if defined(TARGET_I386)
2214 X86CPU *x86_cpu = X86_CPU(cpu);
2215 CPUX86State *env = &x86_cpu->env;
2216#elif defined(TARGET_PPC)
2217 PowerPCCPU *ppc_cpu = POWERPC_CPU(cpu);
2218 CPUPPCState *env = &ppc_cpu->env;
2219#elif defined(TARGET_SPARC)
2220 SPARCCPU *sparc_cpu = SPARC_CPU(cpu);
2221 CPUSPARCState *env = &sparc_cpu->env;
Michael Clark25fa1942018-03-03 01:32:59 +13002222#elif defined(TARGET_RISCV)
2223 RISCVCPU *riscv_cpu = RISCV_CPU(cpu);
2224 CPURISCVState *env = &riscv_cpu->env;
Andreas Färber182735e2013-05-29 22:29:20 +02002225#elif defined(TARGET_MIPS)
2226 MIPSCPU *mips_cpu = MIPS_CPU(cpu);
2227 CPUMIPSState *env = &mips_cpu->env;
Bastian Koppelmann48e06fe2014-09-01 12:59:46 +01002228#elif defined(TARGET_TRICORE)
2229 TriCoreCPU *tricore_cpu = TRICORE_CPU(cpu);
2230 CPUTriCoreState *env = &tricore_cpu->env;
Viktor Mihajlovski9d0306d2018-02-16 17:08:37 +01002231#elif defined(TARGET_S390X)
2232 S390CPU *s390_cpu = S390_CPU(cpu);
2233 CPUS390XState *env = &s390_cpu->env;
Andreas Färber182735e2013-05-29 22:29:20 +02002234#endif
Luiz Capitulinode0b36b2011-09-21 16:38:35 -03002235
Andreas Färbercb446ec2013-05-01 14:24:52 +02002236 cpu_synchronize_state(cpu);
Luiz Capitulinode0b36b2011-09-21 16:38:35 -03002237
2238 info = g_malloc0(sizeof(*info));
2239 info->value = g_malloc0(sizeof(*info->value));
Andreas Färber55e5c282012-12-17 06:18:02 +01002240 info->value->CPU = cpu->cpu_index;
Andreas Färber182735e2013-05-29 22:29:20 +02002241 info->value->current = (cpu == first_cpu);
Andreas Färber259186a2013-01-17 18:51:17 +01002242 info->value->halted = cpu->halted;
Eduardo Habkost58f88d42015-05-08 16:04:22 -03002243 info->value->qom_path = object_get_canonical_path(OBJECT(cpu));
Andreas Färber9f09e182012-05-03 06:59:07 +02002244 info->value->thread_id = cpu->thread_id;
Luiz Capitulinode0b36b2011-09-21 16:38:35 -03002245#if defined(TARGET_I386)
Eric Blake86f4b682015-11-18 01:52:59 -07002246 info->value->arch = CPU_INFO_ARCH_X86;
Eric Blake544a3732016-02-17 23:48:27 -07002247 info->value->u.x86.pc = env->eip + env->segs[R_CS].base;
Luiz Capitulinode0b36b2011-09-21 16:38:35 -03002248#elif defined(TARGET_PPC)
Eric Blake86f4b682015-11-18 01:52:59 -07002249 info->value->arch = CPU_INFO_ARCH_PPC;
Eric Blake544a3732016-02-17 23:48:27 -07002250 info->value->u.ppc.nip = env->nip;
Luiz Capitulinode0b36b2011-09-21 16:38:35 -03002251#elif defined(TARGET_SPARC)
Eric Blake86f4b682015-11-18 01:52:59 -07002252 info->value->arch = CPU_INFO_ARCH_SPARC;
Eric Blake544a3732016-02-17 23:48:27 -07002253 info->value->u.q_sparc.pc = env->pc;
2254 info->value->u.q_sparc.npc = env->npc;
Luiz Capitulinode0b36b2011-09-21 16:38:35 -03002255#elif defined(TARGET_MIPS)
Eric Blake86f4b682015-11-18 01:52:59 -07002256 info->value->arch = CPU_INFO_ARCH_MIPS;
Eric Blake544a3732016-02-17 23:48:27 -07002257 info->value->u.q_mips.PC = env->active_tc.PC;
Bastian Koppelmann48e06fe2014-09-01 12:59:46 +01002258#elif defined(TARGET_TRICORE)
Eric Blake86f4b682015-11-18 01:52:59 -07002259 info->value->arch = CPU_INFO_ARCH_TRICORE;
Eric Blake544a3732016-02-17 23:48:27 -07002260 info->value->u.tricore.PC = env->PC;
Viktor Mihajlovski9d0306d2018-02-16 17:08:37 +01002261#elif defined(TARGET_S390X)
2262 info->value->arch = CPU_INFO_ARCH_S390;
2263 info->value->u.s390.cpu_state = env->cpu_state;
Michael Clark25fa1942018-03-03 01:32:59 +13002264#elif defined(TARGET_RISCV)
2265 info->value->arch = CPU_INFO_ARCH_RISCV;
2266 info->value->u.riscv.pc = env->pc;
Eric Blake86f4b682015-11-18 01:52:59 -07002267#else
2268 info->value->arch = CPU_INFO_ARCH_OTHER;
Luiz Capitulinode0b36b2011-09-21 16:38:35 -03002269#endif
Igor Mammedovafed5a52017-05-10 13:29:55 +02002270 info->value->has_props = !!mc->cpu_index_to_instance_props;
2271 if (info->value->has_props) {
2272 CpuInstanceProperties *props;
2273 props = g_malloc0(sizeof(*props));
2274 *props = mc->cpu_index_to_instance_props(ms, cpu->cpu_index);
2275 info->value->props = props;
2276 }
Luiz Capitulinode0b36b2011-09-21 16:38:35 -03002277
2278 /* XXX: waiting for the qapi to support GSList */
2279 if (!cur_item) {
2280 head = cur_item = info;
2281 } else {
2282 cur_item->next = info;
2283 cur_item = info;
2284 }
2285 }
2286
2287 return head;
2288}
Luiz Capitulino0cfd6a92011-11-22 16:32:37 -02002289
Laszlo Ersekdaa9d2b2018-04-27 21:28:51 +02002290static CpuInfoArch sysemu_target_to_cpuinfo_arch(SysEmuTarget target)
2291{
2292 /*
2293 * The @SysEmuTarget -> @CpuInfoArch mapping below is based on the
2294 * TARGET_ARCH -> TARGET_BASE_ARCH mapping in the "configure" script.
2295 */
2296 switch (target) {
2297 case SYS_EMU_TARGET_I386:
2298 case SYS_EMU_TARGET_X86_64:
2299 return CPU_INFO_ARCH_X86;
2300
2301 case SYS_EMU_TARGET_PPC:
Laszlo Ersekdaa9d2b2018-04-27 21:28:51 +02002302 case SYS_EMU_TARGET_PPC64:
2303 return CPU_INFO_ARCH_PPC;
2304
2305 case SYS_EMU_TARGET_SPARC:
2306 case SYS_EMU_TARGET_SPARC64:
2307 return CPU_INFO_ARCH_SPARC;
2308
2309 case SYS_EMU_TARGET_MIPS:
2310 case SYS_EMU_TARGET_MIPSEL:
2311 case SYS_EMU_TARGET_MIPS64:
2312 case SYS_EMU_TARGET_MIPS64EL:
2313 return CPU_INFO_ARCH_MIPS;
2314
2315 case SYS_EMU_TARGET_TRICORE:
2316 return CPU_INFO_ARCH_TRICORE;
2317
2318 case SYS_EMU_TARGET_S390X:
2319 return CPU_INFO_ARCH_S390;
2320
2321 case SYS_EMU_TARGET_RISCV32:
2322 case SYS_EMU_TARGET_RISCV64:
2323 return CPU_INFO_ARCH_RISCV;
2324
2325 default:
2326 return CPU_INFO_ARCH_OTHER;
2327 }
2328}
2329
2330static void cpustate_to_cpuinfo_s390(CpuInfoS390 *info, const CPUState *cpu)
2331{
2332#ifdef TARGET_S390X
2333 S390CPU *s390_cpu = S390_CPU(cpu);
2334 CPUS390XState *env = &s390_cpu->env;
2335
2336 info->cpu_state = env->cpu_state;
2337#else
2338 abort();
2339#endif
2340}
2341
Luiz Capitulinoce74ee32018-02-16 17:08:38 +01002342/*
2343 * fast means: we NEVER interrupt vCPU threads to retrieve
2344 * information from KVM.
2345 */
2346CpuInfoFastList *qmp_query_cpus_fast(Error **errp)
2347{
2348 MachineState *ms = MACHINE(qdev_get_machine());
2349 MachineClass *mc = MACHINE_GET_CLASS(ms);
2350 CpuInfoFastList *head = NULL, *cur_item = NULL;
Laszlo Ersekdaa9d2b2018-04-27 21:28:51 +02002351 SysEmuTarget target = qapi_enum_parse(&SysEmuTarget_lookup, TARGET_NAME,
2352 -1, &error_abort);
Luiz Capitulinoce74ee32018-02-16 17:08:38 +01002353 CPUState *cpu;
2354
2355 CPU_FOREACH(cpu) {
2356 CpuInfoFastList *info = g_malloc0(sizeof(*info));
2357 info->value = g_malloc0(sizeof(*info->value));
2358
2359 info->value->cpu_index = cpu->cpu_index;
2360 info->value->qom_path = object_get_canonical_path(OBJECT(cpu));
2361 info->value->thread_id = cpu->thread_id;
2362
2363 info->value->has_props = !!mc->cpu_index_to_instance_props;
2364 if (info->value->has_props) {
2365 CpuInstanceProperties *props;
2366 props = g_malloc0(sizeof(*props));
2367 *props = mc->cpu_index_to_instance_props(ms, cpu->cpu_index);
2368 info->value->props = props;
2369 }
2370
Laszlo Ersekdaa9d2b2018-04-27 21:28:51 +02002371 info->value->arch = sysemu_target_to_cpuinfo_arch(target);
2372 info->value->target = target;
2373 if (target == SYS_EMU_TARGET_S390X) {
2374 cpustate_to_cpuinfo_s390(&info->value->u.s390x, cpu);
Laszlo Ersekdaa9d2b2018-04-27 21:28:51 +02002375 }
2376
Luiz Capitulinoce74ee32018-02-16 17:08:38 +01002377 if (!cur_item) {
2378 head = cur_item = info;
2379 } else {
2380 cur_item->next = info;
2381 cur_item = info;
2382 }
2383 }
2384
2385 return head;
2386}
2387
Luiz Capitulino0cfd6a92011-11-22 16:32:37 -02002388void qmp_memsave(int64_t addr, int64_t size, const char *filename,
2389 bool has_cpu, int64_t cpu_index, Error **errp)
2390{
2391 FILE *f;
2392 uint32_t l;
Andreas Färber55e5c282012-12-17 06:18:02 +01002393 CPUState *cpu;
Luiz Capitulino0cfd6a92011-11-22 16:32:37 -02002394 uint8_t buf[1024];
Borislav Petkov0dc9daf2015-02-08 13:14:38 +01002395 int64_t orig_addr = addr, orig_size = size;
Luiz Capitulino0cfd6a92011-11-22 16:32:37 -02002396
2397 if (!has_cpu) {
2398 cpu_index = 0;
2399 }
2400
Andreas Färber151d1322013-02-15 15:41:49 +01002401 cpu = qemu_get_cpu(cpu_index);
2402 if (cpu == NULL) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01002403 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "cpu-index",
2404 "a CPU number");
Luiz Capitulino0cfd6a92011-11-22 16:32:37 -02002405 return;
2406 }
2407
2408 f = fopen(filename, "wb");
2409 if (!f) {
Luiz Capitulino618da852013-06-07 14:35:06 -04002410 error_setg_file_open(errp, errno, filename);
Luiz Capitulino0cfd6a92011-11-22 16:32:37 -02002411 return;
2412 }
2413
2414 while (size != 0) {
2415 l = sizeof(buf);
2416 if (l > size)
2417 l = size;
Aneesh Kumar K.V2f4d0f52013-10-01 21:49:30 +05302418 if (cpu_memory_rw_debug(cpu, addr, buf, l, 0) != 0) {
Borislav Petkov0dc9daf2015-02-08 13:14:38 +01002419 error_setg(errp, "Invalid addr 0x%016" PRIx64 "/size %" PRId64
2420 " specified", orig_addr, orig_size);
Aneesh Kumar K.V2f4d0f52013-10-01 21:49:30 +05302421 goto exit;
2422 }
Luiz Capitulino0cfd6a92011-11-22 16:32:37 -02002423 if (fwrite(buf, 1, l, f) != l) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01002424 error_setg(errp, QERR_IO_ERROR);
Luiz Capitulino0cfd6a92011-11-22 16:32:37 -02002425 goto exit;
2426 }
2427 addr += l;
2428 size -= l;
2429 }
2430
2431exit:
2432 fclose(f);
2433}
Luiz Capitulino6d3962b2011-11-22 17:26:46 -02002434
2435void qmp_pmemsave(int64_t addr, int64_t size, const char *filename,
2436 Error **errp)
2437{
2438 FILE *f;
2439 uint32_t l;
2440 uint8_t buf[1024];
2441
2442 f = fopen(filename, "wb");
2443 if (!f) {
Luiz Capitulino618da852013-06-07 14:35:06 -04002444 error_setg_file_open(errp, errno, filename);
Luiz Capitulino6d3962b2011-11-22 17:26:46 -02002445 return;
2446 }
2447
2448 while (size != 0) {
2449 l = sizeof(buf);
2450 if (l > size)
2451 l = size;
Stefan Weileb6282f2014-04-07 20:28:23 +02002452 cpu_physical_memory_read(addr, buf, l);
Luiz Capitulino6d3962b2011-11-22 17:26:46 -02002453 if (fwrite(buf, 1, l, f) != l) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01002454 error_setg(errp, QERR_IO_ERROR);
Luiz Capitulino6d3962b2011-11-22 17:26:46 -02002455 goto exit;
2456 }
2457 addr += l;
2458 size -= l;
2459 }
2460
2461exit:
2462 fclose(f);
2463}
Luiz Capitulinoab49ab52011-11-23 12:55:53 -02002464
2465void qmp_inject_nmi(Error **errp)
2466{
Alexey Kardashevskiy9cb805f2014-08-20 22:16:33 +10002467 nmi_monitor_handle(monitor_get_cpu_index(), errp);
Luiz Capitulinoab49ab52011-11-23 12:55:53 -02002468}
Sebastian Tanase27498be2014-07-25 11:56:33 +02002469
Markus Armbruster76c86612019-04-17 21:17:53 +02002470void dump_drift_info(void)
Sebastian Tanase27498be2014-07-25 11:56:33 +02002471{
2472 if (!use_icount) {
2473 return;
2474 }
2475
Markus Armbruster76c86612019-04-17 21:17:53 +02002476 qemu_printf("Host - Guest clock %"PRIi64" ms\n",
Sebastian Tanase27498be2014-07-25 11:56:33 +02002477 (cpu_get_clock() - cpu_get_icount())/SCALE_MS);
2478 if (icount_align_option) {
Markus Armbruster76c86612019-04-17 21:17:53 +02002479 qemu_printf("Max guest delay %"PRIi64" ms\n",
2480 -max_delay / SCALE_MS);
2481 qemu_printf("Max guest advance %"PRIi64" ms\n",
2482 max_advance / SCALE_MS);
Sebastian Tanase27498be2014-07-25 11:56:33 +02002483 } else {
Markus Armbruster76c86612019-04-17 21:17:53 +02002484 qemu_printf("Max guest delay NA\n");
2485 qemu_printf("Max guest advance NA\n");
Sebastian Tanase27498be2014-07-25 11:56:33 +02002486 }
2487}