blob: 684aa9679abd2cc0e7a5ba537e8830a211fcca4b [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"
KONRAD Frederic8d4e9142017-02-23 18:29:08 +000026#include "qemu/config-file.h"
Paolo Bonzini33c11872016-03-15 16:58:45 +010027#include "cpu.h"
Paolo Bonzini83c90892012-12-17 18:19:49 +010028#include "monitor/monitor.h"
Markus Armbrustere688df62018-02-01 12:18:31 +010029#include "qapi/error.h"
Markus Armbruster112ed242018-02-26 17:13:27 -060030#include "qapi/qapi-commands-misc.h"
Markus Armbruster9af23982018-02-11 10:36:01 +010031#include "qapi/qapi-events-run-state.h"
Wenchao Xiaa4e15de2014-06-18 08:43:36 +020032#include "qapi/qmp/qerror.h"
Markus Armbrusterd49b6832015-03-17 18:29:20 +010033#include "qemu/error-report.h"
Markus Armbruster76c86612019-04-17 21:17:53 +020034#include "qemu/qemu-print.h"
Paolo Bonzini9c17d612012-12-17 18:20:04 +010035#include "sysemu/sysemu.h"
Max Reitzda31d592016-03-16 19:54:32 +010036#include "sysemu/block-backend.h"
Paolo Bonzini022c62c2012-12-17 18:19:49 +010037#include "exec/gdbstub.h"
Paolo Bonzini9c17d612012-12-17 18:20:04 +010038#include "sysemu/dma.h"
Vincent Palatinb3946622017-01-10 11:59:55 +010039#include "sysemu/hw_accel.h"
Paolo Bonzini9c17d612012-12-17 18:20:04 +010040#include "sysemu/kvm.h"
Vincent Palatinb0cb0a62017-01-10 11:59:57 +010041#include "sysemu/hax.h"
Sergio Andres Gomez Del Realc97d6d22017-09-13 04:05:09 -050042#include "sysemu/hvf.h"
Justin Terry (VM)19306802018-01-22 13:07:49 -080043#include "sysemu/whpx.h"
Paolo Bonzini63c91552016-03-15 13:18:37 +010044#include "exec/exec-all.h"
Blue Swirl296af7c2010-03-29 19:23:50 +000045
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010046#include "qemu/thread.h"
Paolo Bonzini9c17d612012-12-17 18:20:04 +010047#include "sysemu/cpus.h"
48#include "sysemu/qtest.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010049#include "qemu/main-loop.h"
Markus Armbruster922a01a2018-02-01 12:18:46 +010050#include "qemu/option.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010051#include "qemu/bitmap.h"
Liu Ping Fancb365642013-09-25 14:20:58 +080052#include "qemu/seqlock.h"
KONRAD Frederic8d4e9142017-02-23 18:29:08 +000053#include "tcg.h"
Alexey Kardashevskiy9cb805f2014-08-20 22:16:33 +100054#include "hw/nmi.h"
Pavel Dovgalyuk8b427042015-09-17 19:24:05 +030055#include "sysemu/replay.h"
Igor Mammedovafed5a52017-05-10 13:29:55 +020056#include "hw/boards.h"
Jan Kiszka0ff0fc12011-06-23 10:15:55 +020057
Jan Kiszka6d9cb732011-02-01 22:15:58 +010058#ifdef CONFIG_LINUX
59
60#include <sys/prctl.h>
61
Marcelo Tosattic0532a72010-10-11 15:31:21 -030062#ifndef PR_MCE_KILL
63#define PR_MCE_KILL 33
64#endif
65
Jan Kiszka6d9cb732011-02-01 22:15:58 +010066#ifndef PR_MCE_KILL_SET
67#define PR_MCE_KILL_SET 1
68#endif
69
70#ifndef PR_MCE_KILL_EARLY
71#define PR_MCE_KILL_EARLY 1
72#endif
73
74#endif /* CONFIG_LINUX */
75
Sebastian Tanase27498be2014-07-25 11:56:33 +020076int64_t max_delay;
77int64_t max_advance;
Blue Swirl296af7c2010-03-29 19:23:50 +000078
Jason J. Herne2adcc852015-09-08 13:12:33 -040079/* vcpu throttling controls */
80static QEMUTimer *throttle_timer;
81static unsigned int throttle_percentage;
82
83#define CPU_THROTTLE_PCT_MIN 1
84#define CPU_THROTTLE_PCT_MAX 99
85#define CPU_THROTTLE_TIMESLICE_NS 10000000
86
Tiejun Chen321bc0b2013-08-02 09:43:09 +080087bool cpu_is_stopped(CPUState *cpu)
88{
89 return cpu->stopped || !runstate_is_running();
90}
91
Andreas Färbera98ae1d2013-05-26 23:21:08 +020092static bool cpu_thread_is_idle(CPUState *cpu)
Peter Maydellac873f12012-07-19 16:52:27 +010093{
Andreas Färberc64ca812012-05-03 02:11:45 +020094 if (cpu->stop || cpu->queued_work_first) {
Peter Maydellac873f12012-07-19 16:52:27 +010095 return false;
96 }
Tiejun Chen321bc0b2013-08-02 09:43:09 +080097 if (cpu_is_stopped(cpu)) {
Peter Maydellac873f12012-07-19 16:52:27 +010098 return true;
99 }
Andreas Färber8c2e1b02013-08-25 18:53:55 +0200100 if (!cpu->halted || cpu_has_work(cpu) ||
Alexander Graf215e79c2013-04-24 22:24:12 +0200101 kvm_halt_in_kernel()) {
Peter Maydellac873f12012-07-19 16:52:27 +0100102 return false;
103 }
104 return true;
105}
106
107static bool all_cpu_threads_idle(void)
108{
Andreas Färber182735e2013-05-29 22:29:20 +0200109 CPUState *cpu;
Peter Maydellac873f12012-07-19 16:52:27 +0100110
Andreas Färberbdc44642013-06-24 23:50:24 +0200111 CPU_FOREACH(cpu) {
Andreas Färber182735e2013-05-29 22:29:20 +0200112 if (!cpu_thread_is_idle(cpu)) {
Peter Maydellac873f12012-07-19 16:52:27 +0100113 return false;
114 }
115 }
116 return true;
117}
118
Blue Swirl296af7c2010-03-29 19:23:50 +0000119/***********************************************************/
Paolo Bonzini946fb272011-09-12 13:57:37 +0200120/* guest cycle counter */
121
Paolo Bonzinia3270e12013-10-07 17:18:15 +0200122/* Protected by TimersState seqlock */
123
Victor CLEMENT5045e9d92015-05-29 17:14:04 +0200124static bool icount_sleep = true;
Paolo Bonzini946fb272011-09-12 13:57:37 +0200125/* Arbitrarily pick 1MIPS as the minimum allowable speed. */
126#define MAX_ICOUNT_SHIFT 10
Paolo Bonzinia3270e12013-10-07 17:18:15 +0200127
Paolo Bonzini946fb272011-09-12 13:57:37 +0200128typedef struct TimersState {
Liu Ping Fancb365642013-09-25 14:20:58 +0800129 /* Protected by BQL. */
Paolo Bonzini946fb272011-09-12 13:57:37 +0200130 int64_t cpu_ticks_prev;
131 int64_t cpu_ticks_offset;
Liu Ping Fancb365642013-09-25 14:20:58 +0800132
Paolo Bonzini94377112018-08-14 09:57:16 +0200133 /* Protect fields that can be respectively read outside the
134 * BQL, and written from multiple threads.
Liu Ping Fancb365642013-09-25 14:20:58 +0800135 */
136 QemuSeqLock vm_clock_seqlock;
Paolo Bonzini94377112018-08-14 09:57:16 +0200137 QemuSpin vm_clock_lock;
138
139 int16_t cpu_ticks_enabled;
KONRAD Fredericc96778b2014-08-01 01:37:09 +0200140
Paolo Bonzinic1ff0732018-08-14 09:31:58 +0200141 /* Conversion factor from emulated instructions to virtual clock ticks. */
Paolo Bonzini94377112018-08-14 09:57:16 +0200142 int16_t icount_time_shift;
143
KONRAD Fredericc96778b2014-08-01 01:37:09 +0200144 /* Compensate for varying guest execution speed. */
145 int64_t qemu_icount_bias;
Paolo Bonzini94377112018-08-14 09:57:16 +0200146
147 int64_t vm_clock_warp_start;
148 int64_t cpu_clock_offset;
149
KONRAD Fredericc96778b2014-08-01 01:37:09 +0200150 /* Only written by TCG thread */
151 int64_t qemu_icount;
Paolo Bonzini94377112018-08-14 09:57:16 +0200152
Pavel Dovgalyukb39e3f32018-01-11 11:26:10 +0300153 /* for adjusting icount */
Pavel Dovgalyukb39e3f32018-01-11 11:26:10 +0300154 QEMUTimer *icount_rt_timer;
155 QEMUTimer *icount_vm_timer;
156 QEMUTimer *icount_warp_timer;
Paolo Bonzini946fb272011-09-12 13:57:37 +0200157} TimersState;
158
Liu Ping Fand9cd4002013-07-21 08:43:00 +0000159static TimersState timers_state;
KONRAD Frederic8d4e9142017-02-23 18:29:08 +0000160bool mttcg_enabled;
161
162/*
163 * We default to false if we know other options have been enabled
164 * which are currently incompatible with MTTCG. Otherwise when each
165 * guest (target) has been updated to support:
166 * - atomic instructions
167 * - memory ordering primitives (barriers)
168 * they can set the appropriate CONFIG flags in ${target}-softmmu.mak
169 *
170 * Once a guest architecture has been converted to the new primitives
171 * there are two remaining limitations to check.
172 *
173 * - The guest can't be oversized (e.g. 64 bit guest on 32 bit host)
174 * - The host must have a stronger memory order than the guest
175 *
176 * It may be possible in future to support strong guests on weak hosts
177 * but that will require tagging all load/stores in a guest with their
178 * implicit memory order requirements which would likely slow things
179 * down a lot.
180 */
181
182static bool check_tcg_memory_orders_compatible(void)
183{
184#if defined(TCG_GUEST_DEFAULT_MO) && defined(TCG_TARGET_DEFAULT_MO)
185 return (TCG_GUEST_DEFAULT_MO & ~TCG_TARGET_DEFAULT_MO) == 0;
186#else
187 return false;
188#endif
189}
190
191static bool default_mttcg_enabled(void)
192{
Alex Bennée83fd9622017-02-27 17:09:01 +0000193 if (use_icount || TCG_OVERSIZED_GUEST) {
KONRAD Frederic8d4e9142017-02-23 18:29:08 +0000194 return false;
195 } else {
196#ifdef TARGET_SUPPORTS_MTTCG
197 return check_tcg_memory_orders_compatible();
198#else
199 return false;
200#endif
201 }
202}
203
204void qemu_tcg_configure(QemuOpts *opts, Error **errp)
205{
206 const char *t = qemu_opt_get(opts, "thread");
207 if (t) {
208 if (strcmp(t, "multi") == 0) {
209 if (TCG_OVERSIZED_GUEST) {
210 error_setg(errp, "No MTTCG when guest word size > hosts");
Alex Bennée83fd9622017-02-27 17:09:01 +0000211 } else if (use_icount) {
212 error_setg(errp, "No MTTCG when icount is enabled");
KONRAD Frederic8d4e9142017-02-23 18:29:08 +0000213 } else {
Nikunj A Dadhania86953502017-04-10 11:36:55 +0530214#ifndef TARGET_SUPPORTS_MTTCG
Markus Armbruster07656912018-10-17 10:26:28 +0200215 warn_report("Guest not yet converted to MTTCG - "
216 "you may get unexpected results");
Alex Bennéec34c7622017-02-28 14:40:17 +0000217#endif
KONRAD Frederic8d4e9142017-02-23 18:29:08 +0000218 if (!check_tcg_memory_orders_compatible()) {
Markus Armbruster07656912018-10-17 10:26:28 +0200219 warn_report("Guest expects a stronger memory ordering "
220 "than the host provides");
Pranith Kumar8cfef892017-03-25 16:19:23 -0400221 error_printf("This may cause strange/hard to debug errors\n");
KONRAD Frederic8d4e9142017-02-23 18:29:08 +0000222 }
223 mttcg_enabled = true;
224 }
225 } else if (strcmp(t, "single") == 0) {
226 mttcg_enabled = false;
227 } else {
228 error_setg(errp, "Invalid 'thread' setting %s", t);
229 }
230 } else {
231 mttcg_enabled = default_mttcg_enabled();
232 }
233}
Paolo Bonzini946fb272011-09-12 13:57:37 +0200234
Alex Bennéee4cd9652017-03-31 16:09:42 +0100235/* The current number of executed instructions is based on what we
236 * originally budgeted minus the current state of the decrementing
237 * icount counters in extra/u16.low.
238 */
239static int64_t cpu_get_icount_executed(CPUState *cpu)
240{
241 return cpu->icount_budget - (cpu->icount_decr.u16.low + cpu->icount_extra);
242}
243
Alex Bennée512d3c82017-04-05 12:32:37 +0100244/*
245 * Update the global shared timer_state.qemu_icount to take into
246 * account executed instructions. This is done by the TCG vCPU
247 * thread so the main-loop can see time has moved forward.
248 */
Paolo Bonzini9b4e6f42018-09-11 13:15:32 +0200249static void cpu_update_icount_locked(CPUState *cpu)
Alex Bennée512d3c82017-04-05 12:32:37 +0100250{
251 int64_t executed = cpu_get_icount_executed(cpu);
252 cpu->icount_budget -= executed;
253
Emilio G. Cota38adcb62018-09-10 19:27:49 -0400254 atomic_set_i64(&timers_state.qemu_icount,
255 timers_state.qemu_icount + executed);
Paolo Bonzini9b4e6f42018-09-11 13:15:32 +0200256}
257
258/*
259 * Update the global shared timer_state.qemu_icount to take into
260 * account executed instructions. This is done by the TCG vCPU
261 * thread so the main-loop can see time has moved forward.
262 */
263void cpu_update_icount(CPUState *cpu)
264{
265 seqlock_write_lock(&timers_state.vm_clock_seqlock,
266 &timers_state.vm_clock_lock);
267 cpu_update_icount_locked(cpu);
Paolo Bonzini94377112018-08-14 09:57:16 +0200268 seqlock_write_unlock(&timers_state.vm_clock_seqlock,
269 &timers_state.vm_clock_lock);
Alex Bennée512d3c82017-04-05 12:32:37 +0100270}
271
Paolo Bonzinic1ff0732018-08-14 09:31:58 +0200272static int64_t cpu_get_icount_raw_locked(void)
Paolo Bonzini946fb272011-09-12 13:57:37 +0200273{
Andreas Färber4917cf42013-05-27 05:17:50 +0200274 CPUState *cpu = current_cpu;
Paolo Bonzini946fb272011-09-12 13:57:37 +0200275
Alex Bennée243c5f72017-03-30 18:49:22 +0100276 if (cpu && cpu->running) {
Paolo Bonzini414b15c2015-06-24 14:16:26 +0200277 if (!cpu->can_do_io) {
Alistair Francis493d89b2018-02-03 09:43:14 +0100278 error_report("Bad icount read");
Pavel Dovgalyuk2a629142014-12-08 10:53:45 +0300279 exit(1);
Paolo Bonzini946fb272011-09-12 13:57:37 +0200280 }
Alex Bennéee4cd9652017-03-31 16:09:42 +0100281 /* Take into account what has run */
Paolo Bonzini9b4e6f42018-09-11 13:15:32 +0200282 cpu_update_icount_locked(cpu);
Paolo Bonzini946fb272011-09-12 13:57:37 +0200283 }
Emilio G. Cota38adcb62018-09-10 19:27:49 -0400284 /* The read is protected by the seqlock, but needs atomic64 to avoid UB */
285 return atomic_read_i64(&timers_state.qemu_icount);
Paolo Bonzinic1ff0732018-08-14 09:31:58 +0200286}
287
288static int64_t cpu_get_icount_locked(void)
289{
290 int64_t icount = cpu_get_icount_raw_locked();
Emilio G. Cotac97595d2018-09-10 19:27:50 -0400291 return atomic_read_i64(&timers_state.qemu_icount_bias) +
292 cpu_icount_to_ns(icount);
Paolo Bonzinic1ff0732018-08-14 09:31:58 +0200293}
294
295int64_t cpu_get_icount_raw(void)
296{
297 int64_t icount;
298 unsigned start;
299
300 do {
301 start = seqlock_read_begin(&timers_state.vm_clock_seqlock);
302 icount = cpu_get_icount_raw_locked();
303 } while (seqlock_read_retry(&timers_state.vm_clock_seqlock, start));
304
305 return icount;
Pavel Dovgalyuk2a629142014-12-08 10:53:45 +0300306}
307
308/* Return the virtual CPU time, based on the instruction counter. */
Paolo Bonzini17a15f12013-10-03 15:17:25 +0200309int64_t cpu_get_icount(void)
310{
311 int64_t icount;
312 unsigned start;
313
314 do {
315 start = seqlock_read_begin(&timers_state.vm_clock_seqlock);
316 icount = cpu_get_icount_locked();
317 } while (seqlock_read_retry(&timers_state.vm_clock_seqlock, start));
318
319 return icount;
320}
321
KONRAD Frederic3f031312014-08-01 01:37:15 +0200322int64_t cpu_icount_to_ns(int64_t icount)
323{
Paolo Bonzinic1ff0732018-08-14 09:31:58 +0200324 return icount << atomic_read(&timers_state.icount_time_shift);
KONRAD Frederic3f031312014-08-01 01:37:15 +0200325}
326
Paolo Bonzinif2a4ad62018-08-18 09:36:16 +0200327static int64_t cpu_get_ticks_locked(void)
328{
329 int64_t ticks = timers_state.cpu_ticks_offset;
330 if (timers_state.cpu_ticks_enabled) {
331 ticks += cpu_get_host_ticks();
332 }
333
334 if (timers_state.cpu_ticks_prev > ticks) {
335 /* Non increasing ticks may happen if the host uses software suspend. */
336 timers_state.cpu_ticks_offset += timers_state.cpu_ticks_prev - ticks;
337 ticks = timers_state.cpu_ticks_prev;
338 }
339
340 timers_state.cpu_ticks_prev = ticks;
341 return ticks;
342}
343
Cao jind90f3cc2016-07-29 19:05:38 +0800344/* return the time elapsed in VM between vm_start and vm_stop. Unless
345 * icount is active, cpu_get_ticks() uses units of the host CPU cycle
346 * counter.
Cao jind90f3cc2016-07-29 19:05:38 +0800347 */
Paolo Bonzini946fb272011-09-12 13:57:37 +0200348int64_t cpu_get_ticks(void)
349{
Paolo Bonzini5f3e3102013-10-28 17:32:18 +0100350 int64_t ticks;
351
Paolo Bonzini946fb272011-09-12 13:57:37 +0200352 if (use_icount) {
353 return cpu_get_icount();
354 }
Paolo Bonzini5f3e3102013-10-28 17:32:18 +0100355
Paolo Bonzinif2a4ad62018-08-18 09:36:16 +0200356 qemu_spin_lock(&timers_state.vm_clock_lock);
357 ticks = cpu_get_ticks_locked();
358 qemu_spin_unlock(&timers_state.vm_clock_lock);
Paolo Bonzini5f3e3102013-10-28 17:32:18 +0100359 return ticks;
Paolo Bonzini946fb272011-09-12 13:57:37 +0200360}
361
Liu Ping Fancb365642013-09-25 14:20:58 +0800362static int64_t cpu_get_clock_locked(void)
363{
Cao jin1d45cea2016-07-29 19:05:37 +0800364 int64_t time;
Liu Ping Fancb365642013-09-25 14:20:58 +0800365
Cao jin1d45cea2016-07-29 19:05:37 +0800366 time = timers_state.cpu_clock_offset;
Paolo Bonzini5f3e3102013-10-28 17:32:18 +0100367 if (timers_state.cpu_ticks_enabled) {
Cao jin1d45cea2016-07-29 19:05:37 +0800368 time += get_clock();
Liu Ping Fancb365642013-09-25 14:20:58 +0800369 }
370
Cao jin1d45cea2016-07-29 19:05:37 +0800371 return time;
Liu Ping Fancb365642013-09-25 14:20:58 +0800372}
373
Cao jind90f3cc2016-07-29 19:05:38 +0800374/* Return the monotonic time elapsed in VM, i.e.,
Peter Maydell8212ff82016-09-15 10:24:22 +0100375 * the time between vm_start and vm_stop
376 */
Paolo Bonzini946fb272011-09-12 13:57:37 +0200377int64_t cpu_get_clock(void)
378{
379 int64_t ti;
Liu Ping Fancb365642013-09-25 14:20:58 +0800380 unsigned start;
381
382 do {
383 start = seqlock_read_begin(&timers_state.vm_clock_seqlock);
384 ti = cpu_get_clock_locked();
385 } while (seqlock_read_retry(&timers_state.vm_clock_seqlock, start));
386
387 return ti;
Paolo Bonzini946fb272011-09-12 13:57:37 +0200388}
389
Liu Ping Fancb365642013-09-25 14:20:58 +0800390/* enable cpu_get_ticks()
Cao jin3224e872016-07-08 18:31:37 +0800391 * Caller must hold BQL which serves as mutex for vm_clock_seqlock.
Liu Ping Fancb365642013-09-25 14:20:58 +0800392 */
Paolo Bonzini946fb272011-09-12 13:57:37 +0200393void cpu_enable_ticks(void)
394{
Paolo Bonzini94377112018-08-14 09:57:16 +0200395 seqlock_write_lock(&timers_state.vm_clock_seqlock,
396 &timers_state.vm_clock_lock);
Paolo Bonzini946fb272011-09-12 13:57:37 +0200397 if (!timers_state.cpu_ticks_enabled) {
Christopher Covington4a7428c2015-09-25 10:42:21 -0400398 timers_state.cpu_ticks_offset -= cpu_get_host_ticks();
Paolo Bonzini946fb272011-09-12 13:57:37 +0200399 timers_state.cpu_clock_offset -= get_clock();
400 timers_state.cpu_ticks_enabled = 1;
401 }
Paolo Bonzini94377112018-08-14 09:57:16 +0200402 seqlock_write_unlock(&timers_state.vm_clock_seqlock,
403 &timers_state.vm_clock_lock);
Paolo Bonzini946fb272011-09-12 13:57:37 +0200404}
405
406/* disable cpu_get_ticks() : the clock is stopped. You must not call
Liu Ping Fancb365642013-09-25 14:20:58 +0800407 * cpu_get_ticks() after that.
Cao jin3224e872016-07-08 18:31:37 +0800408 * Caller must hold BQL which serves as mutex for vm_clock_seqlock.
Liu Ping Fancb365642013-09-25 14:20:58 +0800409 */
Paolo Bonzini946fb272011-09-12 13:57:37 +0200410void cpu_disable_ticks(void)
411{
Paolo Bonzini94377112018-08-14 09:57:16 +0200412 seqlock_write_lock(&timers_state.vm_clock_seqlock,
413 &timers_state.vm_clock_lock);
Paolo Bonzini946fb272011-09-12 13:57:37 +0200414 if (timers_state.cpu_ticks_enabled) {
Christopher Covington4a7428c2015-09-25 10:42:21 -0400415 timers_state.cpu_ticks_offset += cpu_get_host_ticks();
Liu Ping Fancb365642013-09-25 14:20:58 +0800416 timers_state.cpu_clock_offset = cpu_get_clock_locked();
Paolo Bonzini946fb272011-09-12 13:57:37 +0200417 timers_state.cpu_ticks_enabled = 0;
418 }
Paolo Bonzini94377112018-08-14 09:57:16 +0200419 seqlock_write_unlock(&timers_state.vm_clock_seqlock,
420 &timers_state.vm_clock_lock);
Paolo Bonzini946fb272011-09-12 13:57:37 +0200421}
422
423/* Correlation between real and virtual time is always going to be
424 fairly approximate, so ignore small variation.
425 When the guest is idle real and virtual time will be aligned in
426 the IO wait loop. */
Rutuja Shah73bcb242016-03-21 21:32:30 +0530427#define ICOUNT_WOBBLE (NANOSECONDS_PER_SECOND / 10)
Paolo Bonzini946fb272011-09-12 13:57:37 +0200428
429static void icount_adjust(void)
430{
431 int64_t cur_time;
432 int64_t cur_icount;
433 int64_t delta;
Paolo Bonzinia3270e12013-10-07 17:18:15 +0200434
435 /* Protected by TimersState mutex. */
Paolo Bonzini946fb272011-09-12 13:57:37 +0200436 static int64_t last_delta;
Paolo Bonzini468cc7c2013-10-07 17:21:51 +0200437
Paolo Bonzini946fb272011-09-12 13:57:37 +0200438 /* If the VM is not running, then do nothing. */
439 if (!runstate_is_running()) {
440 return;
441 }
Paolo Bonzini468cc7c2013-10-07 17:21:51 +0200442
Paolo Bonzini94377112018-08-14 09:57:16 +0200443 seqlock_write_lock(&timers_state.vm_clock_seqlock,
444 &timers_state.vm_clock_lock);
Paolo Bonzini17a15f12013-10-03 15:17:25 +0200445 cur_time = cpu_get_clock_locked();
446 cur_icount = cpu_get_icount_locked();
Paolo Bonzini468cc7c2013-10-07 17:21:51 +0200447
Paolo Bonzini946fb272011-09-12 13:57:37 +0200448 delta = cur_icount - cur_time;
449 /* FIXME: This is a very crude algorithm, somewhat prone to oscillation. */
450 if (delta > 0
451 && last_delta + ICOUNT_WOBBLE < delta * 2
Paolo Bonzinic1ff0732018-08-14 09:31:58 +0200452 && timers_state.icount_time_shift > 0) {
Paolo Bonzini946fb272011-09-12 13:57:37 +0200453 /* The guest is getting too far ahead. Slow time down. */
Paolo Bonzinic1ff0732018-08-14 09:31:58 +0200454 atomic_set(&timers_state.icount_time_shift,
455 timers_state.icount_time_shift - 1);
Paolo Bonzini946fb272011-09-12 13:57:37 +0200456 }
457 if (delta < 0
458 && last_delta - ICOUNT_WOBBLE > delta * 2
Paolo Bonzinic1ff0732018-08-14 09:31:58 +0200459 && timers_state.icount_time_shift < MAX_ICOUNT_SHIFT) {
Paolo Bonzini946fb272011-09-12 13:57:37 +0200460 /* The guest is getting too far behind. Speed time up. */
Paolo Bonzinic1ff0732018-08-14 09:31:58 +0200461 atomic_set(&timers_state.icount_time_shift,
462 timers_state.icount_time_shift + 1);
Paolo Bonzini946fb272011-09-12 13:57:37 +0200463 }
464 last_delta = delta;
Emilio G. Cotac97595d2018-09-10 19:27:50 -0400465 atomic_set_i64(&timers_state.qemu_icount_bias,
466 cur_icount - (timers_state.qemu_icount
467 << timers_state.icount_time_shift));
Paolo Bonzini94377112018-08-14 09:57:16 +0200468 seqlock_write_unlock(&timers_state.vm_clock_seqlock,
469 &timers_state.vm_clock_lock);
Paolo Bonzini946fb272011-09-12 13:57:37 +0200470}
471
472static void icount_adjust_rt(void *opaque)
473{
Pavel Dovgalyukb39e3f32018-01-11 11:26:10 +0300474 timer_mod(timers_state.icount_rt_timer,
Pavel Dovgalyuk1979b902015-01-12 15:00:43 +0300475 qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL_RT) + 1000);
Paolo Bonzini946fb272011-09-12 13:57:37 +0200476 icount_adjust();
477}
478
479static void icount_adjust_vm(void *opaque)
480{
Pavel Dovgalyukb39e3f32018-01-11 11:26:10 +0300481 timer_mod(timers_state.icount_vm_timer,
Alex Bligh40daca52013-08-21 16:03:02 +0100482 qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) +
Rutuja Shah73bcb242016-03-21 21:32:30 +0530483 NANOSECONDS_PER_SECOND / 10);
Paolo Bonzini946fb272011-09-12 13:57:37 +0200484 icount_adjust();
485}
486
487static int64_t qemu_icount_round(int64_t count)
488{
Paolo Bonzinic1ff0732018-08-14 09:31:58 +0200489 int shift = atomic_read(&timers_state.icount_time_shift);
490 return (count + (1 << shift) - 1) >> shift;
Paolo Bonzini946fb272011-09-12 13:57:37 +0200491}
492
Pavel Dovgalyukefab87c2015-09-17 19:24:39 +0300493static void icount_warp_rt(void)
Paolo Bonzini946fb272011-09-12 13:57:37 +0200494{
Alex Bennéeccffff42016-04-04 15:35:48 +0100495 unsigned seq;
496 int64_t warp_start;
497
Paolo Bonzini17a15f12013-10-03 15:17:25 +0200498 /* The icount_warp_timer is rescheduled soon after vm_clock_warp_start
499 * changes from -1 to another value, so the race here is okay.
500 */
Alex Bennéeccffff42016-04-04 15:35:48 +0100501 do {
502 seq = seqlock_read_begin(&timers_state.vm_clock_seqlock);
Pavel Dovgalyukb39e3f32018-01-11 11:26:10 +0300503 warp_start = timers_state.vm_clock_warp_start;
Alex Bennéeccffff42016-04-04 15:35:48 +0100504 } while (seqlock_read_retry(&timers_state.vm_clock_seqlock, seq));
505
506 if (warp_start == -1) {
Paolo Bonzini946fb272011-09-12 13:57:37 +0200507 return;
508 }
509
Paolo Bonzini94377112018-08-14 09:57:16 +0200510 seqlock_write_lock(&timers_state.vm_clock_seqlock,
511 &timers_state.vm_clock_lock);
Paolo Bonzini946fb272011-09-12 13:57:37 +0200512 if (runstate_is_running()) {
Paolo Bonzini74c0b812018-10-08 13:24:14 +0200513 int64_t clock = REPLAY_CLOCK_LOCKED(REPLAY_CLOCK_VIRTUAL_RT,
514 cpu_get_clock_locked());
Paolo Bonzini8ed961d2013-10-07 17:26:07 +0200515 int64_t warp_delta;
516
Pavel Dovgalyukb39e3f32018-01-11 11:26:10 +0300517 warp_delta = clock - timers_state.vm_clock_warp_start;
Paolo Bonzini8ed961d2013-10-07 17:26:07 +0200518 if (use_icount == 2) {
Paolo Bonzini946fb272011-09-12 13:57:37 +0200519 /*
Alex Bligh40daca52013-08-21 16:03:02 +0100520 * In adaptive mode, do not let QEMU_CLOCK_VIRTUAL run too
Paolo Bonzini946fb272011-09-12 13:57:37 +0200521 * far ahead of real time.
522 */
Paolo Bonzini17a15f12013-10-03 15:17:25 +0200523 int64_t cur_icount = cpu_get_icount_locked();
Pavel Dovgalyukbf2a7dd2014-11-26 13:40:55 +0300524 int64_t delta = clock - cur_icount;
Paolo Bonzini8ed961d2013-10-07 17:26:07 +0200525 warp_delta = MIN(warp_delta, delta);
Paolo Bonzini946fb272011-09-12 13:57:37 +0200526 }
Emilio G. Cotac97595d2018-09-10 19:27:50 -0400527 atomic_set_i64(&timers_state.qemu_icount_bias,
528 timers_state.qemu_icount_bias + warp_delta);
Paolo Bonzini946fb272011-09-12 13:57:37 +0200529 }
Pavel Dovgalyukb39e3f32018-01-11 11:26:10 +0300530 timers_state.vm_clock_warp_start = -1;
Paolo Bonzini94377112018-08-14 09:57:16 +0200531 seqlock_write_unlock(&timers_state.vm_clock_seqlock,
532 &timers_state.vm_clock_lock);
Paolo Bonzini8ed961d2013-10-07 17:26:07 +0200533
534 if (qemu_clock_expired(QEMU_CLOCK_VIRTUAL)) {
535 qemu_clock_notify(QEMU_CLOCK_VIRTUAL);
536 }
Paolo Bonzini946fb272011-09-12 13:57:37 +0200537}
538
Pavel Dovgalyuke76d1792016-03-10 14:56:09 +0300539static void icount_timer_cb(void *opaque)
Pavel Dovgalyukefab87c2015-09-17 19:24:39 +0300540{
Pavel Dovgalyuke76d1792016-03-10 14:56:09 +0300541 /* No need for a checkpoint because the timer already synchronizes
542 * with CHECKPOINT_CLOCK_VIRTUAL_RT.
543 */
544 icount_warp_rt();
Pavel Dovgalyukefab87c2015-09-17 19:24:39 +0300545}
546
Paolo Bonzini8156be52012-03-28 15:42:04 +0200547void qtest_clock_warp(int64_t dest)
548{
Alex Bligh40daca52013-08-21 16:03:02 +0100549 int64_t clock = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
Fam Zhengefef88b2015-01-19 17:51:43 +0800550 AioContext *aio_context;
Paolo Bonzini8156be52012-03-28 15:42:04 +0200551 assert(qtest_enabled());
Fam Zhengefef88b2015-01-19 17:51:43 +0800552 aio_context = qemu_get_aio_context();
Paolo Bonzini8156be52012-03-28 15:42:04 +0200553 while (clock < dest) {
Alex Bligh40daca52013-08-21 16:03:02 +0100554 int64_t deadline = qemu_clock_deadline_ns_all(QEMU_CLOCK_VIRTUAL);
Sergey Fedorovc9299e22014-06-10 13:10:28 +0400555 int64_t warp = qemu_soonest_timeout(dest - clock, deadline);
Fam Zhengefef88b2015-01-19 17:51:43 +0800556
Paolo Bonzini94377112018-08-14 09:57:16 +0200557 seqlock_write_lock(&timers_state.vm_clock_seqlock,
558 &timers_state.vm_clock_lock);
Emilio G. Cotac97595d2018-09-10 19:27:50 -0400559 atomic_set_i64(&timers_state.qemu_icount_bias,
560 timers_state.qemu_icount_bias + warp);
Paolo Bonzini94377112018-08-14 09:57:16 +0200561 seqlock_write_unlock(&timers_state.vm_clock_seqlock,
562 &timers_state.vm_clock_lock);
Paolo Bonzini17a15f12013-10-03 15:17:25 +0200563
Alex Bligh40daca52013-08-21 16:03:02 +0100564 qemu_clock_run_timers(QEMU_CLOCK_VIRTUAL);
Fam Zhengefef88b2015-01-19 17:51:43 +0800565 timerlist_run_timers(aio_context->tlg.tl[QEMU_CLOCK_VIRTUAL]);
Alex Bligh40daca52013-08-21 16:03:02 +0100566 clock = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
Paolo Bonzini8156be52012-03-28 15:42:04 +0200567 }
Alex Bligh40daca52013-08-21 16:03:02 +0100568 qemu_clock_notify(QEMU_CLOCK_VIRTUAL);
Paolo Bonzini8156be52012-03-28 15:42:04 +0200569}
570
Pavel Dovgalyuke76d1792016-03-10 14:56:09 +0300571void qemu_start_warp_timer(void)
Paolo Bonzini946fb272011-09-12 13:57:37 +0200572{
Paolo Bonzinice78d182013-10-07 17:30:02 +0200573 int64_t clock;
Paolo Bonzini946fb272011-09-12 13:57:37 +0200574 int64_t deadline;
575
Pavel Dovgalyuke76d1792016-03-10 14:56:09 +0300576 if (!use_icount) {
Paolo Bonzini946fb272011-09-12 13:57:37 +0200577 return;
578 }
579
Pavel Dovgalyuk8bd7f712015-09-17 19:24:44 +0300580 /* Nothing to do if the VM is stopped: QEMU_CLOCK_VIRTUAL timers
581 * do not fire, so computing the deadline does not make sense.
582 */
583 if (!runstate_is_running()) {
584 return;
585 }
586
Pavel Dovgalyuk0c081852018-09-12 11:19:45 +0300587 if (replay_mode != REPLAY_MODE_PLAY) {
588 if (!all_cpu_threads_idle()) {
589 return;
590 }
Pavel Dovgalyuk8bd7f712015-09-17 19:24:44 +0300591
Pavel Dovgalyuk0c081852018-09-12 11:19:45 +0300592 if (qtest_enabled()) {
593 /* When testing, qtest commands advance icount. */
594 return;
595 }
Paolo Bonzini946fb272011-09-12 13:57:37 +0200596
Pavel Dovgalyuk0c081852018-09-12 11:19:45 +0300597 replay_checkpoint(CHECKPOINT_CLOCK_WARP_START);
598 } else {
599 /* warp clock deterministically in record/replay mode */
600 if (!replay_checkpoint(CHECKPOINT_CLOCK_WARP_START)) {
601 /* vCPU is sleeping and warp can't be started.
602 It is probably a race condition: notification sent
603 to vCPU was processed in advance and vCPU went to sleep.
604 Therefore we have to wake it up for doing someting. */
605 if (replay_has_checkpoint()) {
606 qemu_clock_notify(QEMU_CLOCK_VIRTUAL);
607 }
608 return;
609 }
Paolo Bonzini8156be52012-03-28 15:42:04 +0200610 }
611
Alex Blighac70aaf2013-08-21 16:02:57 +0100612 /* We want to use the earliest deadline from ALL vm_clocks */
Pavel Dovgalyukbf2a7dd2014-11-26 13:40:55 +0300613 clock = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL_RT);
Alex Bligh40daca52013-08-21 16:03:02 +0100614 deadline = qemu_clock_deadline_ns_all(QEMU_CLOCK_VIRTUAL);
Paolo Bonzinice78d182013-10-07 17:30:02 +0200615 if (deadline < 0) {
Victor CLEMENTd7a0f712015-05-29 17:14:06 +0200616 static bool notified;
617 if (!icount_sleep && !notified) {
Alistair Francis3dc6f862017-07-12 06:57:41 -0700618 warn_report("icount sleep disabled and no active timers");
Victor CLEMENTd7a0f712015-05-29 17:14:06 +0200619 notified = true;
620 }
Paolo Bonzinice78d182013-10-07 17:30:02 +0200621 return;
Alex Blighac70aaf2013-08-21 16:02:57 +0100622 }
623
Paolo Bonzini946fb272011-09-12 13:57:37 +0200624 if (deadline > 0) {
625 /*
Alex Bligh40daca52013-08-21 16:03:02 +0100626 * Ensure QEMU_CLOCK_VIRTUAL proceeds even when the virtual CPU goes to
Paolo Bonzini946fb272011-09-12 13:57:37 +0200627 * sleep. Otherwise, the CPU might be waiting for a future timer
628 * interrupt to wake it up, but the interrupt never comes because
629 * the vCPU isn't running any insns and thus doesn't advance the
Alex Bligh40daca52013-08-21 16:03:02 +0100630 * QEMU_CLOCK_VIRTUAL.
Paolo Bonzini946fb272011-09-12 13:57:37 +0200631 */
Victor CLEMENT5045e9d92015-05-29 17:14:04 +0200632 if (!icount_sleep) {
633 /*
634 * We never let VCPUs sleep in no sleep icount mode.
635 * If there is a pending QEMU_CLOCK_VIRTUAL timer we just advance
636 * to the next QEMU_CLOCK_VIRTUAL event and notify it.
637 * It is useful when we want a deterministic execution time,
638 * isolated from host latencies.
639 */
Paolo Bonzini94377112018-08-14 09:57:16 +0200640 seqlock_write_lock(&timers_state.vm_clock_seqlock,
641 &timers_state.vm_clock_lock);
Emilio G. Cotac97595d2018-09-10 19:27:50 -0400642 atomic_set_i64(&timers_state.qemu_icount_bias,
643 timers_state.qemu_icount_bias + deadline);
Paolo Bonzini94377112018-08-14 09:57:16 +0200644 seqlock_write_unlock(&timers_state.vm_clock_seqlock,
645 &timers_state.vm_clock_lock);
Victor CLEMENT5045e9d92015-05-29 17:14:04 +0200646 qemu_clock_notify(QEMU_CLOCK_VIRTUAL);
647 } else {
648 /*
649 * We do stop VCPUs and only advance QEMU_CLOCK_VIRTUAL after some
650 * "real" time, (related to the time left until the next event) has
651 * passed. The QEMU_CLOCK_VIRTUAL_RT clock will do this.
652 * This avoids that the warps are visible externally; for example,
653 * you will not be sending network packets continuously instead of
654 * every 100ms.
655 */
Paolo Bonzini94377112018-08-14 09:57:16 +0200656 seqlock_write_lock(&timers_state.vm_clock_seqlock,
657 &timers_state.vm_clock_lock);
Pavel Dovgalyukb39e3f32018-01-11 11:26:10 +0300658 if (timers_state.vm_clock_warp_start == -1
659 || timers_state.vm_clock_warp_start > clock) {
660 timers_state.vm_clock_warp_start = clock;
Victor CLEMENT5045e9d92015-05-29 17:14:04 +0200661 }
Paolo Bonzini94377112018-08-14 09:57:16 +0200662 seqlock_write_unlock(&timers_state.vm_clock_seqlock,
663 &timers_state.vm_clock_lock);
Pavel Dovgalyukb39e3f32018-01-11 11:26:10 +0300664 timer_mod_anticipate(timers_state.icount_warp_timer,
665 clock + deadline);
Paolo Bonzinice78d182013-10-07 17:30:02 +0200666 }
Alex Blighac70aaf2013-08-21 16:02:57 +0100667 } else if (deadline == 0) {
Alex Bligh40daca52013-08-21 16:03:02 +0100668 qemu_clock_notify(QEMU_CLOCK_VIRTUAL);
Paolo Bonzini946fb272011-09-12 13:57:37 +0200669 }
670}
671
Pavel Dovgalyuke76d1792016-03-10 14:56:09 +0300672static void qemu_account_warp_timer(void)
673{
674 if (!use_icount || !icount_sleep) {
675 return;
676 }
677
678 /* Nothing to do if the VM is stopped: QEMU_CLOCK_VIRTUAL timers
679 * do not fire, so computing the deadline does not make sense.
680 */
681 if (!runstate_is_running()) {
682 return;
683 }
684
685 /* warp clock deterministically in record/replay mode */
686 if (!replay_checkpoint(CHECKPOINT_CLOCK_WARP_ACCOUNT)) {
687 return;
688 }
689
Pavel Dovgalyukb39e3f32018-01-11 11:26:10 +0300690 timer_del(timers_state.icount_warp_timer);
Pavel Dovgalyuke76d1792016-03-10 14:56:09 +0300691 icount_warp_rt();
692}
693
KONRAD Fredericd09eae32014-08-01 01:37:10 +0200694static bool icount_state_needed(void *opaque)
695{
696 return use_icount;
697}
698
Pavel Dovgalyukb39e3f32018-01-11 11:26:10 +0300699static bool warp_timer_state_needed(void *opaque)
700{
701 TimersState *s = opaque;
702 return s->icount_warp_timer != NULL;
703}
704
705static bool adjust_timers_state_needed(void *opaque)
706{
707 TimersState *s = opaque;
708 return s->icount_rt_timer != NULL;
709}
710
711/*
712 * Subsection for warp timer migration is optional, because may not be created
713 */
714static const VMStateDescription icount_vmstate_warp_timer = {
715 .name = "timer/icount/warp_timer",
716 .version_id = 1,
717 .minimum_version_id = 1,
718 .needed = warp_timer_state_needed,
719 .fields = (VMStateField[]) {
720 VMSTATE_INT64(vm_clock_warp_start, TimersState),
721 VMSTATE_TIMER_PTR(icount_warp_timer, TimersState),
722 VMSTATE_END_OF_LIST()
723 }
724};
725
726static const VMStateDescription icount_vmstate_adjust_timers = {
727 .name = "timer/icount/timers",
728 .version_id = 1,
729 .minimum_version_id = 1,
730 .needed = adjust_timers_state_needed,
731 .fields = (VMStateField[]) {
732 VMSTATE_TIMER_PTR(icount_rt_timer, TimersState),
733 VMSTATE_TIMER_PTR(icount_vm_timer, TimersState),
734 VMSTATE_END_OF_LIST()
735 }
736};
737
KONRAD Fredericd09eae32014-08-01 01:37:10 +0200738/*
739 * This is a subsection for icount migration.
740 */
741static const VMStateDescription icount_vmstate_timers = {
742 .name = "timer/icount",
743 .version_id = 1,
744 .minimum_version_id = 1,
Juan Quintela5cd8cad2014-09-23 14:09:54 +0200745 .needed = icount_state_needed,
KONRAD Fredericd09eae32014-08-01 01:37:10 +0200746 .fields = (VMStateField[]) {
747 VMSTATE_INT64(qemu_icount_bias, TimersState),
748 VMSTATE_INT64(qemu_icount, TimersState),
749 VMSTATE_END_OF_LIST()
Pavel Dovgalyukb39e3f32018-01-11 11:26:10 +0300750 },
751 .subsections = (const VMStateDescription*[]) {
752 &icount_vmstate_warp_timer,
753 &icount_vmstate_adjust_timers,
754 NULL
KONRAD Fredericd09eae32014-08-01 01:37:10 +0200755 }
756};
757
Paolo Bonzini946fb272011-09-12 13:57:37 +0200758static const VMStateDescription vmstate_timers = {
759 .name = "timer",
760 .version_id = 2,
761 .minimum_version_id = 1,
Juan Quintela35d08452014-04-16 16:01:33 +0200762 .fields = (VMStateField[]) {
Paolo Bonzini946fb272011-09-12 13:57:37 +0200763 VMSTATE_INT64(cpu_ticks_offset, TimersState),
Paolo Bonzinic1ff0732018-08-14 09:31:58 +0200764 VMSTATE_UNUSED(8),
Paolo Bonzini946fb272011-09-12 13:57:37 +0200765 VMSTATE_INT64_V(cpu_clock_offset, TimersState, 2),
766 VMSTATE_END_OF_LIST()
KONRAD Fredericd09eae32014-08-01 01:37:10 +0200767 },
Juan Quintela5cd8cad2014-09-23 14:09:54 +0200768 .subsections = (const VMStateDescription*[]) {
769 &icount_vmstate_timers,
770 NULL
Paolo Bonzini946fb272011-09-12 13:57:37 +0200771 }
772};
773
Paolo Bonzini14e6fe12016-10-31 10:36:08 +0100774static void cpu_throttle_thread(CPUState *cpu, run_on_cpu_data opaque)
Jason J. Herne2adcc852015-09-08 13:12:33 -0400775{
Jason J. Herne2adcc852015-09-08 13:12:33 -0400776 double pct;
777 double throttle_ratio;
778 long sleeptime_ns;
779
780 if (!cpu_throttle_get_percentage()) {
781 return;
782 }
783
784 pct = (double)cpu_throttle_get_percentage()/100;
785 throttle_ratio = pct / (1 - pct);
786 sleeptime_ns = (long)(throttle_ratio * CPU_THROTTLE_TIMESLICE_NS);
787
788 qemu_mutex_unlock_iothread();
Jason J. Herne2adcc852015-09-08 13:12:33 -0400789 g_usleep(sleeptime_ns / 1000); /* Convert ns to us for usleep call */
790 qemu_mutex_lock_iothread();
Felipe Franciosi90bb0c02017-05-19 22:29:50 +0100791 atomic_set(&cpu->throttle_thread_scheduled, 0);
Jason J. Herne2adcc852015-09-08 13:12:33 -0400792}
793
794static void cpu_throttle_timer_tick(void *opaque)
795{
796 CPUState *cpu;
797 double pct;
798
799 /* Stop the timer if needed */
800 if (!cpu_throttle_get_percentage()) {
801 return;
802 }
803 CPU_FOREACH(cpu) {
804 if (!atomic_xchg(&cpu->throttle_thread_scheduled, 1)) {
Paolo Bonzini14e6fe12016-10-31 10:36:08 +0100805 async_run_on_cpu(cpu, cpu_throttle_thread,
806 RUN_ON_CPU_NULL);
Jason J. Herne2adcc852015-09-08 13:12:33 -0400807 }
808 }
809
810 pct = (double)cpu_throttle_get_percentage()/100;
811 timer_mod(throttle_timer, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL_RT) +
812 CPU_THROTTLE_TIMESLICE_NS / (1-pct));
813}
814
815void cpu_throttle_set(int new_throttle_pct)
816{
817 /* Ensure throttle percentage is within valid range */
818 new_throttle_pct = MIN(new_throttle_pct, CPU_THROTTLE_PCT_MAX);
819 new_throttle_pct = MAX(new_throttle_pct, CPU_THROTTLE_PCT_MIN);
820
821 atomic_set(&throttle_percentage, new_throttle_pct);
822
823 timer_mod(throttle_timer, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL_RT) +
824 CPU_THROTTLE_TIMESLICE_NS);
825}
826
827void cpu_throttle_stop(void)
828{
829 atomic_set(&throttle_percentage, 0);
830}
831
832bool cpu_throttle_active(void)
833{
834 return (cpu_throttle_get_percentage() != 0);
835}
836
837int cpu_throttle_get_percentage(void)
838{
839 return atomic_read(&throttle_percentage);
840}
841
Pavel Dovgalyuk4603ea02014-09-01 09:34:49 +0400842void cpu_ticks_init(void)
843{
Emilio G. Cotaccdb3c12016-06-08 14:55:20 -0400844 seqlock_init(&timers_state.vm_clock_seqlock);
Emilio G. Cota87a09cd2018-09-03 13:18:29 -0400845 qemu_spin_init(&timers_state.vm_clock_lock);
Pavel Dovgalyuk4603ea02014-09-01 09:34:49 +0400846 vmstate_register(NULL, 0, &vmstate_timers, &timers_state);
Jason J. Herne2adcc852015-09-08 13:12:33 -0400847 throttle_timer = timer_new_ns(QEMU_CLOCK_VIRTUAL_RT,
848 cpu_throttle_timer_tick, NULL);
Pavel Dovgalyuk4603ea02014-09-01 09:34:49 +0400849}
850
Sebastian Tanase1ad95802014-07-25 11:56:28 +0200851void configure_icount(QemuOpts *opts, Error **errp)
Paolo Bonzini946fb272011-09-12 13:57:37 +0200852{
Sebastian Tanase1ad95802014-07-25 11:56:28 +0200853 const char *option;
Sebastian Tanasea8bfac32014-07-25 11:56:29 +0200854 char *rem_str = NULL;
Sebastian Tanase1ad95802014-07-25 11:56:28 +0200855
Sebastian Tanase1ad95802014-07-25 11:56:28 +0200856 option = qemu_opt_get(opts, "shift");
Paolo Bonzini946fb272011-09-12 13:57:37 +0200857 if (!option) {
Sebastian Tanasea8bfac32014-07-25 11:56:29 +0200858 if (qemu_opt_get(opts, "align") != NULL) {
859 error_setg(errp, "Please specify shift option when using align");
860 }
Paolo Bonzini946fb272011-09-12 13:57:37 +0200861 return;
862 }
Victor CLEMENTf1f4b572015-05-29 17:14:05 +0200863
864 icount_sleep = qemu_opt_get_bool(opts, "sleep", true);
Victor CLEMENT5045e9d92015-05-29 17:14:04 +0200865 if (icount_sleep) {
Pavel Dovgalyukb39e3f32018-01-11 11:26:10 +0300866 timers_state.icount_warp_timer = timer_new_ns(QEMU_CLOCK_VIRTUAL_RT,
Pavel Dovgalyuke76d1792016-03-10 14:56:09 +0300867 icount_timer_cb, NULL);
Victor CLEMENT5045e9d92015-05-29 17:14:04 +0200868 }
Victor CLEMENTf1f4b572015-05-29 17:14:05 +0200869
Sebastian Tanasea8bfac32014-07-25 11:56:29 +0200870 icount_align_option = qemu_opt_get_bool(opts, "align", false);
Victor CLEMENTf1f4b572015-05-29 17:14:05 +0200871
872 if (icount_align_option && !icount_sleep) {
Pranith Kumar778d9f92016-02-26 10:16:51 -0500873 error_setg(errp, "align=on and sleep=off are incompatible");
Victor CLEMENTf1f4b572015-05-29 17:14:05 +0200874 }
Paolo Bonzini946fb272011-09-12 13:57:37 +0200875 if (strcmp(option, "auto") != 0) {
Sebastian Tanasea8bfac32014-07-25 11:56:29 +0200876 errno = 0;
Paolo Bonzinic1ff0732018-08-14 09:31:58 +0200877 timers_state.icount_time_shift = strtol(option, &rem_str, 0);
Sebastian Tanasea8bfac32014-07-25 11:56:29 +0200878 if (errno != 0 || *rem_str != '\0' || !strlen(option)) {
879 error_setg(errp, "icount: Invalid shift value");
880 }
Paolo Bonzini946fb272011-09-12 13:57:37 +0200881 use_icount = 1;
882 return;
Sebastian Tanasea8bfac32014-07-25 11:56:29 +0200883 } else if (icount_align_option) {
884 error_setg(errp, "shift=auto and align=on are incompatible");
Victor CLEMENTf1f4b572015-05-29 17:14:05 +0200885 } else if (!icount_sleep) {
Pranith Kumar778d9f92016-02-26 10:16:51 -0500886 error_setg(errp, "shift=auto and sleep=off are incompatible");
Paolo Bonzini946fb272011-09-12 13:57:37 +0200887 }
888
889 use_icount = 2;
890
891 /* 125MIPS seems a reasonable initial guess at the guest speed.
892 It will be corrected fairly quickly anyway. */
Paolo Bonzinic1ff0732018-08-14 09:31:58 +0200893 timers_state.icount_time_shift = 3;
Paolo Bonzini946fb272011-09-12 13:57:37 +0200894
895 /* Have both realtime and virtual time triggers for speed adjustment.
896 The realtime trigger catches emulated time passing too slowly,
897 the virtual time trigger catches emulated time passing too fast.
898 Realtime triggers occur even when idle, so use them less frequently
899 than VM triggers. */
Pavel Dovgalyukb39e3f32018-01-11 11:26:10 +0300900 timers_state.vm_clock_warp_start = -1;
901 timers_state.icount_rt_timer = timer_new_ms(QEMU_CLOCK_VIRTUAL_RT,
Pavel Dovgalyukbf2a7dd2014-11-26 13:40:55 +0300902 icount_adjust_rt, NULL);
Pavel Dovgalyukb39e3f32018-01-11 11:26:10 +0300903 timer_mod(timers_state.icount_rt_timer,
Pavel Dovgalyukbf2a7dd2014-11-26 13:40:55 +0300904 qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL_RT) + 1000);
Pavel Dovgalyukb39e3f32018-01-11 11:26:10 +0300905 timers_state.icount_vm_timer = timer_new_ns(QEMU_CLOCK_VIRTUAL,
Alex Bligh40daca52013-08-21 16:03:02 +0100906 icount_adjust_vm, NULL);
Pavel Dovgalyukb39e3f32018-01-11 11:26:10 +0300907 timer_mod(timers_state.icount_vm_timer,
Alex Bligh40daca52013-08-21 16:03:02 +0100908 qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) +
Rutuja Shah73bcb242016-03-21 21:32:30 +0530909 NANOSECONDS_PER_SECOND / 10);
Paolo Bonzini946fb272011-09-12 13:57:37 +0200910}
911
912/***********************************************************/
Alex Bennée65467062017-02-23 18:29:09 +0000913/* TCG vCPU kick timer
914 *
915 * The kick timer is responsible for moving single threaded vCPU
916 * emulation on to the next vCPU. If more than one vCPU is running a
917 * timer event with force a cpu->exit so the next vCPU can get
918 * scheduled.
919 *
920 * The timer is removed if all vCPUs are idle and restarted again once
921 * idleness is complete.
922 */
923
924static QEMUTimer *tcg_kick_vcpu_timer;
Alex Bennée791158d2017-02-23 18:29:10 +0000925static CPUState *tcg_current_rr_cpu;
Alex Bennée65467062017-02-23 18:29:09 +0000926
927#define TCG_KICK_PERIOD (NANOSECONDS_PER_SECOND / 10)
928
929static inline int64_t qemu_tcg_next_kick(void)
930{
931 return qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) + TCG_KICK_PERIOD;
932}
933
Alex Bennée791158d2017-02-23 18:29:10 +0000934/* Kick the currently round-robin scheduled vCPU */
935static void qemu_cpu_kick_rr_cpu(void)
936{
937 CPUState *cpu;
Alex Bennée791158d2017-02-23 18:29:10 +0000938 do {
939 cpu = atomic_mb_read(&tcg_current_rr_cpu);
940 if (cpu) {
941 cpu_exit(cpu);
942 }
943 } while (cpu != atomic_mb_read(&tcg_current_rr_cpu));
944}
945
Paolo Bonzini6b8f0182017-03-02 19:56:40 +0100946static void do_nothing(CPUState *cpu, run_on_cpu_data unused)
947{
948}
949
Paolo Bonzini3f53bc62017-03-03 11:50:29 +0100950void qemu_timer_notify_cb(void *opaque, QEMUClockType type)
951{
Paolo Bonzini6b8f0182017-03-02 19:56:40 +0100952 if (!use_icount || type != QEMU_CLOCK_VIRTUAL) {
953 qemu_notify_event();
954 return;
955 }
956
Peter Maydellc52e7132018-04-10 13:02:25 +0100957 if (qemu_in_vcpu_thread()) {
958 /* A CPU is currently running; kick it back out to the
959 * tcg_cpu_exec() loop so it will recalculate its
960 * icount deadline immediately.
961 */
962 qemu_cpu_kick(current_cpu);
963 } else if (first_cpu) {
Paolo Bonzini6b8f0182017-03-02 19:56:40 +0100964 /* qemu_cpu_kick is not enough to kick a halted CPU out of
965 * qemu_tcg_wait_io_event. async_run_on_cpu, instead,
966 * causes cpu_thread_is_idle to return false. This way,
967 * handle_icount_deadline can run.
Peter Maydellc52e7132018-04-10 13:02:25 +0100968 * If we have no CPUs at all for some reason, we don't
969 * need to do anything.
Paolo Bonzini6b8f0182017-03-02 19:56:40 +0100970 */
971 async_run_on_cpu(first_cpu, do_nothing, RUN_ON_CPU_NULL);
972 }
Paolo Bonzini3f53bc62017-03-03 11:50:29 +0100973}
974
Alex Bennée65467062017-02-23 18:29:09 +0000975static void kick_tcg_thread(void *opaque)
976{
977 timer_mod(tcg_kick_vcpu_timer, qemu_tcg_next_kick());
Alex Bennée791158d2017-02-23 18:29:10 +0000978 qemu_cpu_kick_rr_cpu();
Alex Bennée65467062017-02-23 18:29:09 +0000979}
980
981static void start_tcg_kick_timer(void)
982{
Paolo Bonzinidb08b682018-01-11 13:53:12 +0100983 assert(!mttcg_enabled);
984 if (!tcg_kick_vcpu_timer && CPU_NEXT(first_cpu)) {
Alex Bennée65467062017-02-23 18:29:09 +0000985 tcg_kick_vcpu_timer = timer_new_ns(QEMU_CLOCK_VIRTUAL,
986 kick_tcg_thread, NULL);
Alex Bennée1926ab22018-09-27 18:17:24 +0100987 }
988 if (tcg_kick_vcpu_timer && !timer_pending(tcg_kick_vcpu_timer)) {
Alex Bennée65467062017-02-23 18:29:09 +0000989 timer_mod(tcg_kick_vcpu_timer, qemu_tcg_next_kick());
990 }
991}
992
993static void stop_tcg_kick_timer(void)
994{
Paolo Bonzinidb08b682018-01-11 13:53:12 +0100995 assert(!mttcg_enabled);
Alex Bennée1926ab22018-09-27 18:17:24 +0100996 if (tcg_kick_vcpu_timer && timer_pending(tcg_kick_vcpu_timer)) {
Alex Bennée65467062017-02-23 18:29:09 +0000997 timer_del(tcg_kick_vcpu_timer);
Alex Bennée65467062017-02-23 18:29:09 +0000998 }
999}
1000
Alex Bennée65467062017-02-23 18:29:09 +00001001/***********************************************************/
Blue Swirl296af7c2010-03-29 19:23:50 +00001002void hw_error(const char *fmt, ...)
1003{
1004 va_list ap;
Andreas Färber55e5c282012-12-17 06:18:02 +01001005 CPUState *cpu;
Blue Swirl296af7c2010-03-29 19:23:50 +00001006
1007 va_start(ap, fmt);
1008 fprintf(stderr, "qemu: hardware error: ");
1009 vfprintf(stderr, fmt, ap);
1010 fprintf(stderr, "\n");
Andreas Färberbdc44642013-06-24 23:50:24 +02001011 CPU_FOREACH(cpu) {
Andreas Färber55e5c282012-12-17 06:18:02 +01001012 fprintf(stderr, "CPU #%d:\n", cpu->cpu_index);
Andreas Färber878096e2013-05-27 01:33:50 +02001013 cpu_dump_state(cpu, stderr, fprintf, CPU_DUMP_FPU);
Blue Swirl296af7c2010-03-29 19:23:50 +00001014 }
1015 va_end(ap);
1016 abort();
1017}
1018
1019void cpu_synchronize_all_states(void)
1020{
Andreas Färber182735e2013-05-29 22:29:20 +02001021 CPUState *cpu;
Blue Swirl296af7c2010-03-29 19:23:50 +00001022
Andreas Färberbdc44642013-06-24 23:50:24 +02001023 CPU_FOREACH(cpu) {
Andreas Färber182735e2013-05-29 22:29:20 +02001024 cpu_synchronize_state(cpu);
Sergio Andres Gomez Del Realc97d6d22017-09-13 04:05:09 -05001025 /* TODO: move to cpu_synchronize_state() */
1026 if (hvf_enabled()) {
1027 hvf_cpu_synchronize_state(cpu);
1028 }
Blue Swirl296af7c2010-03-29 19:23:50 +00001029 }
1030}
1031
1032void cpu_synchronize_all_post_reset(void)
1033{
Andreas Färber182735e2013-05-29 22:29:20 +02001034 CPUState *cpu;
Blue Swirl296af7c2010-03-29 19:23:50 +00001035
Andreas Färberbdc44642013-06-24 23:50:24 +02001036 CPU_FOREACH(cpu) {
Andreas Färber182735e2013-05-29 22:29:20 +02001037 cpu_synchronize_post_reset(cpu);
Sergio Andres Gomez Del Realc97d6d22017-09-13 04:05:09 -05001038 /* TODO: move to cpu_synchronize_post_reset() */
1039 if (hvf_enabled()) {
1040 hvf_cpu_synchronize_post_reset(cpu);
1041 }
Blue Swirl296af7c2010-03-29 19:23:50 +00001042 }
1043}
1044
1045void cpu_synchronize_all_post_init(void)
1046{
Andreas Färber182735e2013-05-29 22:29:20 +02001047 CPUState *cpu;
Blue Swirl296af7c2010-03-29 19:23:50 +00001048
Andreas Färberbdc44642013-06-24 23:50:24 +02001049 CPU_FOREACH(cpu) {
Andreas Färber182735e2013-05-29 22:29:20 +02001050 cpu_synchronize_post_init(cpu);
Sergio Andres Gomez Del Realc97d6d22017-09-13 04:05:09 -05001051 /* TODO: move to cpu_synchronize_post_init() */
1052 if (hvf_enabled()) {
1053 hvf_cpu_synchronize_post_init(cpu);
1054 }
Blue Swirl296af7c2010-03-29 19:23:50 +00001055 }
1056}
1057
David Gibson75e972d2017-05-26 14:46:28 +10001058void cpu_synchronize_all_pre_loadvm(void)
1059{
1060 CPUState *cpu;
1061
1062 CPU_FOREACH(cpu) {
1063 cpu_synchronize_pre_loadvm(cpu);
1064 }
1065}
1066
Stefan Hajnoczi4486e892018-03-07 14:42:05 +00001067static int do_vm_stop(RunState state, bool send_stop)
Blue Swirl296af7c2010-03-29 19:23:50 +00001068{
Kevin Wolf56983462013-07-05 13:49:54 +02001069 int ret = 0;
1070
Luiz Capitulino13548692011-07-29 15:36:43 -03001071 if (runstate_is_running()) {
Blue Swirl296af7c2010-03-29 19:23:50 +00001072 cpu_disable_ticks();
Blue Swirl296af7c2010-03-29 19:23:50 +00001073 pause_all_vcpus();
Luiz Capitulinof5bbfba2011-07-29 15:04:45 -03001074 runstate_set(state);
Luiz Capitulino1dfb4dd2011-07-29 14:26:33 -03001075 vm_state_notify(0, state);
Stefan Hajnoczi4486e892018-03-07 14:42:05 +00001076 if (send_stop) {
Peter Xu3ab72382018-08-15 21:37:37 +08001077 qapi_event_send_stop();
Stefan Hajnoczi4486e892018-03-07 14:42:05 +00001078 }
Blue Swirl296af7c2010-03-29 19:23:50 +00001079 }
Kevin Wolf56983462013-07-05 13:49:54 +02001080
Kevin Wolf594a45c2013-07-18 14:52:19 +02001081 bdrv_drain_all();
Pavel Dovgalyuk6d0ceb82016-09-26 11:08:16 +03001082 replay_disable_events();
John Snow22af08e2016-09-22 21:45:51 -04001083 ret = bdrv_flush_all();
Kevin Wolf594a45c2013-07-18 14:52:19 +02001084
Kevin Wolf56983462013-07-05 13:49:54 +02001085 return ret;
Blue Swirl296af7c2010-03-29 19:23:50 +00001086}
1087
Stefan Hajnoczi4486e892018-03-07 14:42:05 +00001088/* Special vm_stop() variant for terminating the process. Historically clients
1089 * did not expect a QMP STOP event and so we need to retain compatibility.
1090 */
1091int vm_shutdown(void)
1092{
1093 return do_vm_stop(RUN_STATE_SHUTDOWN, false);
1094}
1095
Andreas Färbera1fcaa72012-05-02 23:42:26 +02001096static bool cpu_can_run(CPUState *cpu)
Blue Swirl296af7c2010-03-29 19:23:50 +00001097{
Andreas Färber4fdeee72012-05-02 23:10:09 +02001098 if (cpu->stop) {
Andreas Färbera1fcaa72012-05-02 23:42:26 +02001099 return false;
Jan Kiszka0ab07c62011-02-07 12:19:14 +01001100 }
Tiejun Chen321bc0b2013-08-02 09:43:09 +08001101 if (cpu_is_stopped(cpu)) {
Andreas Färbera1fcaa72012-05-02 23:42:26 +02001102 return false;
Jan Kiszka0ab07c62011-02-07 12:19:14 +01001103 }
Andreas Färbera1fcaa72012-05-02 23:42:26 +02001104 return true;
Blue Swirl296af7c2010-03-29 19:23:50 +00001105}
1106
Andreas Färber91325042013-05-27 02:07:49 +02001107static void cpu_handle_guest_debug(CPUState *cpu)
Jan Kiszka3c638d02010-06-25 16:56:56 +02001108{
Andreas Färber64f6b342013-05-27 02:06:09 +02001109 gdb_set_stop_cpu(cpu);
Jan Kiszka8cf71712011-02-07 12:19:16 +01001110 qemu_system_debug_request();
Andreas Färberf324e762012-05-02 23:26:21 +02001111 cpu->stopped = true;
Jan Kiszka3c638d02010-06-25 16:56:56 +02001112}
1113
Jan Kiszka6d9cb732011-02-01 22:15:58 +01001114#ifdef CONFIG_LINUX
1115static void sigbus_reraise(void)
1116{
1117 sigset_t set;
1118 struct sigaction action;
1119
1120 memset(&action, 0, sizeof(action));
1121 action.sa_handler = SIG_DFL;
1122 if (!sigaction(SIGBUS, &action, NULL)) {
1123 raise(SIGBUS);
1124 sigemptyset(&set);
1125 sigaddset(&set, SIGBUS);
Peter Maydella2d17612016-05-16 18:33:59 +01001126 pthread_sigmask(SIG_UNBLOCK, &set, NULL);
Jan Kiszka6d9cb732011-02-01 22:15:58 +01001127 }
1128 perror("Failed to re-raise SIGBUS!\n");
1129 abort();
1130}
1131
Paolo Bonzinid98d4072017-02-08 13:22:12 +01001132static void sigbus_handler(int n, siginfo_t *siginfo, void *ctx)
Jan Kiszka6d9cb732011-02-01 22:15:58 +01001133{
Paolo Bonzinia16fc072017-02-09 09:50:02 +01001134 if (siginfo->si_code != BUS_MCEERR_AO && siginfo->si_code != BUS_MCEERR_AR) {
1135 sigbus_reraise();
1136 }
1137
Paolo Bonzini2ae41db2017-02-08 12:48:54 +01001138 if (current_cpu) {
1139 /* Called asynchronously in VCPU thread. */
1140 if (kvm_on_sigbus_vcpu(current_cpu, siginfo->si_code, siginfo->si_addr)) {
1141 sigbus_reraise();
1142 }
1143 } else {
1144 /* Called synchronously (via signalfd) in main thread. */
1145 if (kvm_on_sigbus(siginfo->si_code, siginfo->si_addr)) {
1146 sigbus_reraise();
1147 }
Jan Kiszka6d9cb732011-02-01 22:15:58 +01001148 }
1149}
1150
1151static void qemu_init_sigbus(void)
1152{
1153 struct sigaction action;
1154
1155 memset(&action, 0, sizeof(action));
1156 action.sa_flags = SA_SIGINFO;
Paolo Bonzinid98d4072017-02-08 13:22:12 +01001157 action.sa_sigaction = sigbus_handler;
Jan Kiszka6d9cb732011-02-01 22:15:58 +01001158 sigaction(SIGBUS, &action, NULL);
1159
1160 prctl(PR_MCE_KILL, PR_MCE_KILL_SET, PR_MCE_KILL_EARLY, 0, 0);
1161}
Paolo Bonzinia16fc072017-02-09 09:50:02 +01001162#else /* !CONFIG_LINUX */
1163static void qemu_init_sigbus(void)
1164{
1165}
Paolo Bonzinia16fc072017-02-09 09:50:02 +01001166#endif /* !CONFIG_LINUX */
Blue Swirl296af7c2010-03-29 19:23:50 +00001167
Stefan Weilb2532d82012-09-27 07:41:42 +02001168static QemuMutex qemu_global_mutex;
Blue Swirl296af7c2010-03-29 19:23:50 +00001169
1170static QemuThread io_thread;
1171
Blue Swirl296af7c2010-03-29 19:23:50 +00001172/* cpu creation */
1173static QemuCond qemu_cpu_cond;
1174/* system init */
Blue Swirl296af7c2010-03-29 19:23:50 +00001175static QemuCond qemu_pause_cond;
1176
Paolo Bonzinid3b12f52011-09-13 10:30:52 +02001177void qemu_init_cpu_loop(void)
Blue Swirl296af7c2010-03-29 19:23:50 +00001178{
Jan Kiszka6d9cb732011-02-01 22:15:58 +01001179 qemu_init_sigbus();
Anthony Liguoried945922011-02-08 18:18:18 +01001180 qemu_cond_init(&qemu_cpu_cond);
Anthony Liguoried945922011-02-08 18:18:18 +01001181 qemu_cond_init(&qemu_pause_cond);
Blue Swirl296af7c2010-03-29 19:23:50 +00001182 qemu_mutex_init(&qemu_global_mutex);
Blue Swirl296af7c2010-03-29 19:23:50 +00001183
Jan Kiszkab7680cb2011-03-12 17:43:51 +01001184 qemu_thread_get_self(&io_thread);
Blue Swirl296af7c2010-03-29 19:23:50 +00001185}
1186
Paolo Bonzini14e6fe12016-10-31 10:36:08 +01001187void run_on_cpu(CPUState *cpu, run_on_cpu_func func, run_on_cpu_data data)
Marcelo Tosattie82bcec2010-05-04 09:45:22 -03001188{
Sergey Fedorovd148d902016-08-29 09:51:00 +02001189 do_run_on_cpu(cpu, func, data, &qemu_global_mutex);
Chegu Vinod3c022702013-06-24 03:49:41 -06001190}
1191
Gu Zheng4c055ab2016-05-12 09:18:13 +05301192static void qemu_kvm_destroy_vcpu(CPUState *cpu)
1193{
1194 if (kvm_destroy_vcpu(cpu) < 0) {
1195 error_report("kvm_destroy_vcpu failed");
1196 exit(EXIT_FAILURE);
1197 }
1198}
1199
1200static void qemu_tcg_destroy_vcpu(CPUState *cpu)
1201{
1202}
1203
David Hildenbrandebd05fe2017-11-29 20:12:15 +01001204static void qemu_cpu_stop(CPUState *cpu, bool exit)
1205{
1206 g_assert(qemu_cpu_is_self(cpu));
1207 cpu->stop = false;
1208 cpu->stopped = true;
1209 if (exit) {
1210 cpu_exit(cpu);
1211 }
1212 qemu_cond_broadcast(&qemu_pause_cond);
1213}
1214
Andreas Färber509a0d72012-05-03 02:18:09 +02001215static void qemu_wait_io_event_common(CPUState *cpu)
Blue Swirl296af7c2010-03-29 19:23:50 +00001216{
Alex Bennée37257942017-02-23 18:29:14 +00001217 atomic_mb_set(&cpu->thread_kicked, false);
Andreas Färber4fdeee72012-05-02 23:10:09 +02001218 if (cpu->stop) {
David Hildenbrandebd05fe2017-11-29 20:12:15 +01001219 qemu_cpu_stop(cpu, false);
Blue Swirl296af7c2010-03-29 19:23:50 +00001220 }
Sergey Fedorova5403c62016-08-02 18:27:36 +01001221 process_queued_cpu_work(cpu);
Alex Bennée37257942017-02-23 18:29:14 +00001222}
1223
Paolo Bonzinia8efa602018-11-14 12:36:57 +01001224static void qemu_tcg_rr_wait_io_event(void)
Alex Bennée37257942017-02-23 18:29:14 +00001225{
Paolo Bonzinia8efa602018-11-14 12:36:57 +01001226 CPUState *cpu;
1227
Paolo Bonzinidb08b682018-01-11 13:53:12 +01001228 while (all_cpu_threads_idle()) {
Alex Bennée65467062017-02-23 18:29:09 +00001229 stop_tcg_kick_timer();
Paolo Bonzinia8efa602018-11-14 12:36:57 +01001230 qemu_cond_wait(first_cpu->halt_cond, &qemu_global_mutex);
Jan Kiszka16400322011-02-09 16:29:37 +01001231 }
Blue Swirl296af7c2010-03-29 19:23:50 +00001232
Alex Bennée65467062017-02-23 18:29:09 +00001233 start_tcg_kick_timer();
1234
Paolo Bonzinia8efa602018-11-14 12:36:57 +01001235 CPU_FOREACH(cpu) {
1236 qemu_wait_io_event_common(cpu);
1237 }
Blue Swirl296af7c2010-03-29 19:23:50 +00001238}
1239
Paolo Bonzinidb08b682018-01-11 13:53:12 +01001240static void qemu_wait_io_event(CPUState *cpu)
Blue Swirl296af7c2010-03-29 19:23:50 +00001241{
Andreas Färbera98ae1d2013-05-26 23:21:08 +02001242 while (cpu_thread_is_idle(cpu)) {
Andreas Färberf5c121b2012-05-03 01:22:49 +02001243 qemu_cond_wait(cpu->halt_cond, &qemu_global_mutex);
Jan Kiszka16400322011-02-09 16:29:37 +01001244 }
Blue Swirl296af7c2010-03-29 19:23:50 +00001245
Paolo Bonzinidb08b682018-01-11 13:53:12 +01001246#ifdef _WIN32
1247 /* Eat dummy APC queued by qemu_cpu_kick_thread. */
1248 if (!tcg_enabled()) {
1249 SleepEx(0, TRUE);
Sergio Andres Gomez Del Realc97d6d22017-09-13 04:05:09 -05001250 }
Paolo Bonzinidb08b682018-01-11 13:53:12 +01001251#endif
Sergio Andres Gomez Del Realc97d6d22017-09-13 04:05:09 -05001252 qemu_wait_io_event_common(cpu);
1253}
1254
Jan Kiszka7e97cd82011-02-07 12:19:12 +01001255static void *qemu_kvm_cpu_thread_fn(void *arg)
Blue Swirl296af7c2010-03-29 19:23:50 +00001256{
Andreas Färber48a106b2013-05-27 02:20:39 +02001257 CPUState *cpu = arg;
Jan Kiszka84b49152011-02-01 22:15:50 +01001258 int r;
Blue Swirl296af7c2010-03-29 19:23:50 +00001259
Paolo Bonziniab28bd22015-07-09 08:55:38 +02001260 rcu_register_thread();
1261
Paolo Bonzini2e7f7a32015-06-18 18:47:18 +02001262 qemu_mutex_lock_iothread();
Andreas Färber814e6122012-05-02 17:00:37 +02001263 qemu_thread_get_self(cpu->thread);
Andreas Färber9f09e182012-05-03 06:59:07 +02001264 cpu->thread_id = qemu_get_thread_id();
Pavel Dovgalyuk626cf8f2014-12-08 10:53:17 +03001265 cpu->can_do_io = 1;
Andreas Färber4917cf42013-05-27 05:17:50 +02001266 current_cpu = cpu;
Blue Swirl296af7c2010-03-29 19:23:50 +00001267
Andreas Färber504134d2012-12-17 06:38:45 +01001268 r = kvm_init_vcpu(cpu);
Jan Kiszka84b49152011-02-01 22:15:50 +01001269 if (r < 0) {
Alistair Francis493d89b2018-02-03 09:43:14 +01001270 error_report("kvm_init_vcpu failed: %s", strerror(-r));
Jan Kiszka84b49152011-02-01 22:15:50 +01001271 exit(1);
1272 }
Blue Swirl296af7c2010-03-29 19:23:50 +00001273
Paolo Bonzini18268b62017-02-09 09:41:14 +01001274 kvm_init_cpu_signals(cpu);
Blue Swirl296af7c2010-03-29 19:23:50 +00001275
1276 /* signal CPU creation */
Andreas Färber61a46212012-05-02 22:49:36 +02001277 cpu->created = true;
Blue Swirl296af7c2010-03-29 19:23:50 +00001278 qemu_cond_signal(&qemu_cpu_cond);
1279
Gu Zheng4c055ab2016-05-12 09:18:13 +05301280 do {
Andreas Färbera1fcaa72012-05-02 23:42:26 +02001281 if (cpu_can_run(cpu)) {
Andreas Färber1458c362013-05-26 23:46:55 +02001282 r = kvm_cpu_exec(cpu);
Jan Kiszka83f338f2011-02-07 12:19:17 +01001283 if (r == EXCP_DEBUG) {
Andreas Färber91325042013-05-27 02:07:49 +02001284 cpu_handle_guest_debug(cpu);
Jan Kiszka83f338f2011-02-07 12:19:17 +01001285 }
Jan Kiszka0ab07c62011-02-07 12:19:14 +01001286 }
Paolo Bonzinidb08b682018-01-11 13:53:12 +01001287 qemu_wait_io_event(cpu);
Gu Zheng4c055ab2016-05-12 09:18:13 +05301288 } while (!cpu->unplug || cpu_can_run(cpu));
Blue Swirl296af7c2010-03-29 19:23:50 +00001289
Gu Zheng4c055ab2016-05-12 09:18:13 +05301290 qemu_kvm_destroy_vcpu(cpu);
Bharata B Rao2c579042016-05-12 09:18:14 +05301291 cpu->created = false;
1292 qemu_cond_signal(&qemu_cpu_cond);
Gu Zheng4c055ab2016-05-12 09:18:13 +05301293 qemu_mutex_unlock_iothread();
Paolo Bonzini57615ed2018-01-30 11:04:36 -05001294 rcu_unregister_thread();
Blue Swirl296af7c2010-03-29 19:23:50 +00001295 return NULL;
1296}
1297
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +02001298static void *qemu_dummy_cpu_thread_fn(void *arg)
1299{
1300#ifdef _WIN32
Alistair Francis493d89b2018-02-03 09:43:14 +01001301 error_report("qtest is not supported under Windows");
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +02001302 exit(1);
1303#else
Andreas Färber10a90212013-05-27 02:24:35 +02001304 CPUState *cpu = arg;
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +02001305 sigset_t waitset;
1306 int r;
1307
Paolo Bonziniab28bd22015-07-09 08:55:38 +02001308 rcu_register_thread();
1309
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +02001310 qemu_mutex_lock_iothread();
Andreas Färber814e6122012-05-02 17:00:37 +02001311 qemu_thread_get_self(cpu->thread);
Andreas Färber9f09e182012-05-03 06:59:07 +02001312 cpu->thread_id = qemu_get_thread_id();
Pavel Dovgalyuk626cf8f2014-12-08 10:53:17 +03001313 cpu->can_do_io = 1;
Alex Bennée37257942017-02-23 18:29:14 +00001314 current_cpu = cpu;
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +02001315
1316 sigemptyset(&waitset);
1317 sigaddset(&waitset, SIG_IPI);
1318
1319 /* signal CPU creation */
Andreas Färber61a46212012-05-02 22:49:36 +02001320 cpu->created = true;
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +02001321 qemu_cond_signal(&qemu_cpu_cond);
1322
Paolo Bonzinid2831ab2018-01-30 11:04:53 -05001323 do {
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +02001324 qemu_mutex_unlock_iothread();
1325 do {
1326 int sig;
1327 r = sigwait(&waitset, &sig);
1328 } while (r == -1 && (errno == EAGAIN || errno == EINTR));
1329 if (r == -1) {
1330 perror("sigwait");
1331 exit(1);
1332 }
1333 qemu_mutex_lock_iothread();
Paolo Bonzinidb08b682018-01-11 13:53:12 +01001334 qemu_wait_io_event(cpu);
Paolo Bonzinid2831ab2018-01-30 11:04:53 -05001335 } while (!cpu->unplug);
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +02001336
David Hildenbrandd40bfcb2019-02-18 10:21:57 +01001337 qemu_mutex_unlock_iothread();
Paolo Bonzinid2831ab2018-01-30 11:04:53 -05001338 rcu_unregister_thread();
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +02001339 return NULL;
1340#endif
1341}
1342
Alex Bennée1be7fcb2016-10-27 16:10:08 +01001343static int64_t tcg_get_icount_limit(void)
1344{
1345 int64_t deadline;
1346
1347 if (replay_mode != REPLAY_MODE_PLAY) {
1348 deadline = qemu_clock_deadline_ns_all(QEMU_CLOCK_VIRTUAL);
1349
1350 /* Maintain prior (possibly buggy) behaviour where if no deadline
1351 * was set (as there is no QEMU_CLOCK_VIRTUAL timer) or it is more than
1352 * INT32_MAX nanoseconds ahead, we still use INT32_MAX
1353 * nanoseconds.
1354 */
1355 if ((deadline < 0) || (deadline > INT32_MAX)) {
1356 deadline = INT32_MAX;
1357 }
1358
1359 return qemu_icount_round(deadline);
1360 } else {
1361 return replay_get_instructions();
1362 }
1363}
1364
Alex Bennée12e97002016-10-27 16:10:14 +01001365static void handle_icount_deadline(void)
1366{
Paolo Bonzini6b8f0182017-03-02 19:56:40 +01001367 assert(qemu_in_vcpu_thread());
Alex Bennée12e97002016-10-27 16:10:14 +01001368 if (use_icount) {
1369 int64_t deadline =
1370 qemu_clock_deadline_ns_all(QEMU_CLOCK_VIRTUAL);
1371
1372 if (deadline == 0) {
Paolo Bonzini6b8f0182017-03-02 19:56:40 +01001373 /* Wake up other AioContexts. */
Alex Bennée12e97002016-10-27 16:10:14 +01001374 qemu_clock_notify(QEMU_CLOCK_VIRTUAL);
Paolo Bonzini6b8f0182017-03-02 19:56:40 +01001375 qemu_clock_run_timers(QEMU_CLOCK_VIRTUAL);
Alex Bennée12e97002016-10-27 16:10:14 +01001376 }
1377 }
1378}
1379
Alex Bennée05248382017-03-29 16:46:59 +01001380static void prepare_icount_for_run(CPUState *cpu)
1381{
1382 if (use_icount) {
Alex Bennéeeda5f7c2017-04-05 12:35:48 +01001383 int insns_left;
Alex Bennée05248382017-03-29 16:46:59 +01001384
1385 /* These should always be cleared by process_icount_data after
1386 * each vCPU execution. However u16.high can be raised
1387 * asynchronously by cpu_exit/cpu_interrupt/tcg_handle_interrupt
1388 */
1389 g_assert(cpu->icount_decr.u16.low == 0);
1390 g_assert(cpu->icount_extra == 0);
1391
Alex Bennéeeda5f7c2017-04-05 12:35:48 +01001392 cpu->icount_budget = tcg_get_icount_limit();
1393 insns_left = MIN(0xffff, cpu->icount_budget);
1394 cpu->icount_decr.u16.low = insns_left;
1395 cpu->icount_extra = cpu->icount_budget - insns_left;
Alex Bennéed759c952018-02-27 12:52:48 +03001396
1397 replay_mutex_lock();
Alex Bennée05248382017-03-29 16:46:59 +01001398 }
1399}
1400
1401static void process_icount_data(CPUState *cpu)
1402{
1403 if (use_icount) {
Alex Bennéee4cd9652017-03-31 16:09:42 +01001404 /* Account for executed instructions */
Alex Bennée512d3c82017-04-05 12:32:37 +01001405 cpu_update_icount(cpu);
Alex Bennée05248382017-03-29 16:46:59 +01001406
1407 /* Reset the counters */
1408 cpu->icount_decr.u16.low = 0;
1409 cpu->icount_extra = 0;
Alex Bennéee4cd9652017-03-31 16:09:42 +01001410 cpu->icount_budget = 0;
1411
Alex Bennée05248382017-03-29 16:46:59 +01001412 replay_account_executed_instructions();
Alex Bennéed759c952018-02-27 12:52:48 +03001413
1414 replay_mutex_unlock();
Alex Bennée05248382017-03-29 16:46:59 +01001415 }
1416}
1417
1418
Alex Bennée1be7fcb2016-10-27 16:10:08 +01001419static int tcg_cpu_exec(CPUState *cpu)
1420{
1421 int ret;
1422#ifdef CONFIG_PROFILER
1423 int64_t ti;
1424#endif
1425
Emilio G. Cotaf28d0df2018-06-22 13:45:31 -04001426 assert(tcg_enabled());
Alex Bennée1be7fcb2016-10-27 16:10:08 +01001427#ifdef CONFIG_PROFILER
1428 ti = profile_getclock();
1429#endif
Alex Bennée1be7fcb2016-10-27 16:10:08 +01001430 cpu_exec_start(cpu);
1431 ret = cpu_exec(cpu);
1432 cpu_exec_end(cpu);
1433#ifdef CONFIG_PROFILER
Emilio G. Cota72fd2ef2018-10-10 10:48:53 -04001434 atomic_set(&tcg_ctx->prof.cpu_exec_time,
1435 tcg_ctx->prof.cpu_exec_time + profile_getclock() - ti);
Alex Bennée1be7fcb2016-10-27 16:10:08 +01001436#endif
Alex Bennée1be7fcb2016-10-27 16:10:08 +01001437 return ret;
1438}
1439
Alex Bennéec93bbbe2016-10-27 16:10:09 +01001440/* Destroy any remaining vCPUs which have been unplugged and have
1441 * finished running
1442 */
1443static void deal_with_unplugged_cpus(void)
Alex Bennée1be7fcb2016-10-27 16:10:08 +01001444{
Alex Bennéec93bbbe2016-10-27 16:10:09 +01001445 CPUState *cpu;
Alex Bennée1be7fcb2016-10-27 16:10:08 +01001446
Alex Bennéec93bbbe2016-10-27 16:10:09 +01001447 CPU_FOREACH(cpu) {
1448 if (cpu->unplug && !cpu_can_run(cpu)) {
1449 qemu_tcg_destroy_vcpu(cpu);
1450 cpu->created = false;
1451 qemu_cond_signal(&qemu_cpu_cond);
Alex Bennée1be7fcb2016-10-27 16:10:08 +01001452 break;
1453 }
1454 }
Alex Bennée1be7fcb2016-10-27 16:10:08 +01001455}
Jan Kiszkabdb7ca62011-09-26 09:40:39 +02001456
Alex Bennée65467062017-02-23 18:29:09 +00001457/* Single-threaded TCG
1458 *
1459 * In the single-threaded case each vCPU is simulated in turn. If
1460 * there is more than a single vCPU we create a simple timer to kick
1461 * the vCPU and ensure we don't get stuck in a tight loop in one vCPU.
1462 * This is done explicitly rather than relying on side-effects
1463 * elsewhere.
1464 */
1465
Alex Bennée37257942017-02-23 18:29:14 +00001466static void *qemu_tcg_rr_cpu_thread_fn(void *arg)
Blue Swirl296af7c2010-03-29 19:23:50 +00001467{
Andreas Färberc3586ba2012-05-03 01:41:24 +02001468 CPUState *cpu = arg;
Blue Swirl296af7c2010-03-29 19:23:50 +00001469
Emilio G. Cotaf28d0df2018-06-22 13:45:31 -04001470 assert(tcg_enabled());
Paolo Bonziniab28bd22015-07-09 08:55:38 +02001471 rcu_register_thread();
Emilio G. Cota3468b592017-07-19 18:57:58 -04001472 tcg_register_thread();
Paolo Bonziniab28bd22015-07-09 08:55:38 +02001473
Paolo Bonzini2e7f7a32015-06-18 18:47:18 +02001474 qemu_mutex_lock_iothread();
Andreas Färber814e6122012-05-02 17:00:37 +02001475 qemu_thread_get_self(cpu->thread);
Blue Swirl296af7c2010-03-29 19:23:50 +00001476
David Hildenbrand5a9c9732018-02-09 20:52:39 +01001477 cpu->thread_id = qemu_get_thread_id();
1478 cpu->created = true;
1479 cpu->can_do_io = 1;
Blue Swirl296af7c2010-03-29 19:23:50 +00001480 qemu_cond_signal(&qemu_cpu_cond);
1481
Jan Kiszkafa7d1862011-08-22 18:35:25 +02001482 /* wait for initial kick-off after machine start */
Emilio G. Cotac28e3992015-04-27 12:45:28 -04001483 while (first_cpu->stopped) {
KONRAD Fredericd5f8d612015-08-10 17:27:06 +02001484 qemu_cond_wait(first_cpu->halt_cond, &qemu_global_mutex);
Jan Kiszka8e564b42012-02-17 18:31:15 +01001485
1486 /* process any pending work */
Andreas Färberbdc44642013-06-24 23:50:24 +02001487 CPU_FOREACH(cpu) {
Alex Bennée37257942017-02-23 18:29:14 +00001488 current_cpu = cpu;
Andreas Färber182735e2013-05-29 22:29:20 +02001489 qemu_wait_io_event_common(cpu);
Jan Kiszka8e564b42012-02-17 18:31:15 +01001490 }
Jan Kiszka0ab07c62011-02-07 12:19:14 +01001491 }
Blue Swirl296af7c2010-03-29 19:23:50 +00001492
Alex Bennée65467062017-02-23 18:29:09 +00001493 start_tcg_kick_timer();
1494
Alex Bennéec93bbbe2016-10-27 16:10:09 +01001495 cpu = first_cpu;
1496
Alex Bennéee5143e32017-02-23 18:29:12 +00001497 /* process any pending work */
1498 cpu->exit_request = 1;
1499
Blue Swirl296af7c2010-03-29 19:23:50 +00001500 while (1) {
Alex Bennéed759c952018-02-27 12:52:48 +03001501 qemu_mutex_unlock_iothread();
1502 replay_mutex_lock();
1503 qemu_mutex_lock_iothread();
Alex Bennéec93bbbe2016-10-27 16:10:09 +01001504 /* Account partial waits to QEMU_CLOCK_VIRTUAL. */
1505 qemu_account_warp_timer();
1506
Paolo Bonzini6b8f0182017-03-02 19:56:40 +01001507 /* Run the timers here. This is much more efficient than
1508 * waking up the I/O thread and waiting for completion.
1509 */
1510 handle_icount_deadline();
1511
Alex Bennéed759c952018-02-27 12:52:48 +03001512 replay_mutex_unlock();
1513
Alex Bennéec93bbbe2016-10-27 16:10:09 +01001514 if (!cpu) {
1515 cpu = first_cpu;
1516 }
1517
Alex Bennéee5143e32017-02-23 18:29:12 +00001518 while (cpu && !cpu->queued_work_first && !cpu->exit_request) {
1519
Alex Bennée791158d2017-02-23 18:29:10 +00001520 atomic_mb_set(&tcg_current_rr_cpu, cpu);
Alex Bennée37257942017-02-23 18:29:14 +00001521 current_cpu = cpu;
Alex Bennéec93bbbe2016-10-27 16:10:09 +01001522
1523 qemu_clock_enable(QEMU_CLOCK_VIRTUAL,
1524 (cpu->singlestep_enabled & SSTEP_NOTIMER) == 0);
1525
1526 if (cpu_can_run(cpu)) {
1527 int r;
Alex Bennée05248382017-03-29 16:46:59 +01001528
Alex Bennéed759c952018-02-27 12:52:48 +03001529 qemu_mutex_unlock_iothread();
Alex Bennée05248382017-03-29 16:46:59 +01001530 prepare_icount_for_run(cpu);
1531
Alex Bennéec93bbbe2016-10-27 16:10:09 +01001532 r = tcg_cpu_exec(cpu);
Alex Bennée05248382017-03-29 16:46:59 +01001533
1534 process_icount_data(cpu);
Alex Bennéed759c952018-02-27 12:52:48 +03001535 qemu_mutex_lock_iothread();
Alex Bennée05248382017-03-29 16:46:59 +01001536
Alex Bennéec93bbbe2016-10-27 16:10:09 +01001537 if (r == EXCP_DEBUG) {
1538 cpu_handle_guest_debug(cpu);
1539 break;
Pranith Kumar08e73c42017-02-23 18:29:15 +00001540 } else if (r == EXCP_ATOMIC) {
1541 qemu_mutex_unlock_iothread();
1542 cpu_exec_step_atomic(cpu);
1543 qemu_mutex_lock_iothread();
1544 break;
Alex Bennéec93bbbe2016-10-27 16:10:09 +01001545 }
Alex Bennée37257942017-02-23 18:29:14 +00001546 } else if (cpu->stop) {
Alex Bennéec93bbbe2016-10-27 16:10:09 +01001547 if (cpu->unplug) {
1548 cpu = CPU_NEXT(cpu);
1549 }
1550 break;
1551 }
1552
Alex Bennéee5143e32017-02-23 18:29:12 +00001553 cpu = CPU_NEXT(cpu);
1554 } /* while (cpu && !cpu->exit_request).. */
1555
Alex Bennée791158d2017-02-23 18:29:10 +00001556 /* Does not need atomic_mb_set because a spurious wakeup is okay. */
1557 atomic_set(&tcg_current_rr_cpu, NULL);
Alex Bennéec93bbbe2016-10-27 16:10:09 +01001558
Alex Bennéee5143e32017-02-23 18:29:12 +00001559 if (cpu && cpu->exit_request) {
1560 atomic_mb_set(&cpu->exit_request, 0);
1561 }
Alex Blighac70aaf2013-08-21 16:02:57 +01001562
Clement Deschamps013aabd2018-10-21 16:21:03 +02001563 if (use_icount && all_cpu_threads_idle()) {
1564 /*
1565 * When all cpus are sleeping (e.g in WFI), to avoid a deadlock
1566 * in the main_loop, wake it up in order to start the warp timer.
1567 */
1568 qemu_notify_event();
1569 }
1570
Paolo Bonzinia8efa602018-11-14 12:36:57 +01001571 qemu_tcg_rr_wait_io_event();
Alex Bennéec93bbbe2016-10-27 16:10:09 +01001572 deal_with_unplugged_cpus();
Blue Swirl296af7c2010-03-29 19:23:50 +00001573 }
1574
Paolo Bonzini9b0605f2018-01-30 11:05:06 -05001575 rcu_unregister_thread();
Blue Swirl296af7c2010-03-29 19:23:50 +00001576 return NULL;
1577}
1578
Vincent Palatinb0cb0a62017-01-10 11:59:57 +01001579static void *qemu_hax_cpu_thread_fn(void *arg)
1580{
1581 CPUState *cpu = arg;
1582 int r;
Vincent Palatinb3d3a422017-03-20 11:15:49 +01001583
Paolo Bonzini9857c2d2018-01-30 16:28:49 +01001584 rcu_register_thread();
Vincent Palatinb3d3a422017-03-20 11:15:49 +01001585 qemu_mutex_lock_iothread();
Vincent Palatinb0cb0a62017-01-10 11:59:57 +01001586 qemu_thread_get_self(cpu->thread);
Vincent Palatinb0cb0a62017-01-10 11:59:57 +01001587
1588 cpu->thread_id = qemu_get_thread_id();
1589 cpu->created = true;
1590 cpu->halted = 0;
1591 current_cpu = cpu;
1592
1593 hax_init_vcpu(cpu);
1594 qemu_cond_signal(&qemu_cpu_cond);
1595
Paolo Bonzini9857c2d2018-01-30 16:28:49 +01001596 do {
Vincent Palatinb0cb0a62017-01-10 11:59:57 +01001597 if (cpu_can_run(cpu)) {
1598 r = hax_smp_cpu_exec(cpu);
1599 if (r == EXCP_DEBUG) {
1600 cpu_handle_guest_debug(cpu);
1601 }
1602 }
1603
Paolo Bonzinidb08b682018-01-11 13:53:12 +01001604 qemu_wait_io_event(cpu);
Paolo Bonzini9857c2d2018-01-30 16:28:49 +01001605 } while (!cpu->unplug || cpu_can_run(cpu));
1606 rcu_unregister_thread();
Vincent Palatinb0cb0a62017-01-10 11:59:57 +01001607 return NULL;
1608}
1609
Sergio Andres Gomez Del Realc97d6d22017-09-13 04:05:09 -05001610/* The HVF-specific vCPU thread function. This one should only run when the host
1611 * CPU supports the VMX "unrestricted guest" feature. */
1612static void *qemu_hvf_cpu_thread_fn(void *arg)
1613{
1614 CPUState *cpu = arg;
1615
1616 int r;
1617
1618 assert(hvf_enabled());
1619
1620 rcu_register_thread();
1621
1622 qemu_mutex_lock_iothread();
1623 qemu_thread_get_self(cpu->thread);
1624
1625 cpu->thread_id = qemu_get_thread_id();
1626 cpu->can_do_io = 1;
1627 current_cpu = cpu;
1628
1629 hvf_init_vcpu(cpu);
1630
1631 /* signal CPU creation */
1632 cpu->created = true;
1633 qemu_cond_signal(&qemu_cpu_cond);
1634
1635 do {
1636 if (cpu_can_run(cpu)) {
1637 r = hvf_vcpu_exec(cpu);
1638 if (r == EXCP_DEBUG) {
1639 cpu_handle_guest_debug(cpu);
1640 }
1641 }
Paolo Bonzinidb08b682018-01-11 13:53:12 +01001642 qemu_wait_io_event(cpu);
Sergio Andres Gomez Del Realc97d6d22017-09-13 04:05:09 -05001643 } while (!cpu->unplug || cpu_can_run(cpu));
1644
1645 hvf_vcpu_destroy(cpu);
1646 cpu->created = false;
1647 qemu_cond_signal(&qemu_cpu_cond);
1648 qemu_mutex_unlock_iothread();
Paolo Bonzini8178e632018-01-30 11:05:21 -05001649 rcu_unregister_thread();
Sergio Andres Gomez Del Realc97d6d22017-09-13 04:05:09 -05001650 return NULL;
1651}
1652
Justin Terry (VM)19306802018-01-22 13:07:49 -08001653static void *qemu_whpx_cpu_thread_fn(void *arg)
1654{
1655 CPUState *cpu = arg;
1656 int r;
1657
1658 rcu_register_thread();
1659
1660 qemu_mutex_lock_iothread();
1661 qemu_thread_get_self(cpu->thread);
1662 cpu->thread_id = qemu_get_thread_id();
1663 current_cpu = cpu;
1664
1665 r = whpx_init_vcpu(cpu);
1666 if (r < 0) {
1667 fprintf(stderr, "whpx_init_vcpu failed: %s\n", strerror(-r));
1668 exit(1);
1669 }
1670
1671 /* signal CPU creation */
1672 cpu->created = true;
1673 qemu_cond_signal(&qemu_cpu_cond);
1674
1675 do {
1676 if (cpu_can_run(cpu)) {
1677 r = whpx_vcpu_exec(cpu);
1678 if (r == EXCP_DEBUG) {
1679 cpu_handle_guest_debug(cpu);
1680 }
1681 }
1682 while (cpu_thread_is_idle(cpu)) {
1683 qemu_cond_wait(cpu->halt_cond, &qemu_global_mutex);
1684 }
1685 qemu_wait_io_event_common(cpu);
1686 } while (!cpu->unplug || cpu_can_run(cpu));
1687
1688 whpx_destroy_vcpu(cpu);
1689 cpu->created = false;
1690 qemu_cond_signal(&qemu_cpu_cond);
1691 qemu_mutex_unlock_iothread();
1692 rcu_unregister_thread();
Vincent Palatinb0cb0a62017-01-10 11:59:57 +01001693 return NULL;
1694}
1695
1696#ifdef _WIN32
1697static void CALLBACK dummy_apc_func(ULONG_PTR unused)
1698{
1699}
1700#endif
1701
Alex Bennée37257942017-02-23 18:29:14 +00001702/* Multi-threaded TCG
1703 *
1704 * In the multi-threaded case each vCPU has its own thread. The TLS
1705 * variable current_cpu can be used deep in the code to find the
1706 * current CPUState for a given thread.
1707 */
1708
1709static void *qemu_tcg_cpu_thread_fn(void *arg)
1710{
1711 CPUState *cpu = arg;
1712
Emilio G. Cotaf28d0df2018-06-22 13:45:31 -04001713 assert(tcg_enabled());
Alex Bennéebf51c722017-03-30 18:32:29 +01001714 g_assert(!use_icount);
1715
Alex Bennée37257942017-02-23 18:29:14 +00001716 rcu_register_thread();
Emilio G. Cota3468b592017-07-19 18:57:58 -04001717 tcg_register_thread();
Alex Bennée37257942017-02-23 18:29:14 +00001718
1719 qemu_mutex_lock_iothread();
1720 qemu_thread_get_self(cpu->thread);
1721
1722 cpu->thread_id = qemu_get_thread_id();
1723 cpu->created = true;
1724 cpu->can_do_io = 1;
1725 current_cpu = cpu;
1726 qemu_cond_signal(&qemu_cpu_cond);
1727
1728 /* process any pending work */
1729 cpu->exit_request = 1;
1730
Cédric Le Goater54961aa2018-04-25 15:18:28 +02001731 do {
Alex Bennée37257942017-02-23 18:29:14 +00001732 if (cpu_can_run(cpu)) {
1733 int r;
Alex Bennéed759c952018-02-27 12:52:48 +03001734 qemu_mutex_unlock_iothread();
Alex Bennée37257942017-02-23 18:29:14 +00001735 r = tcg_cpu_exec(cpu);
Alex Bennéed759c952018-02-27 12:52:48 +03001736 qemu_mutex_lock_iothread();
Alex Bennée37257942017-02-23 18:29:14 +00001737 switch (r) {
1738 case EXCP_DEBUG:
1739 cpu_handle_guest_debug(cpu);
1740 break;
1741 case EXCP_HALTED:
1742 /* during start-up the vCPU is reset and the thread is
1743 * kicked several times. If we don't ensure we go back
1744 * to sleep in the halted state we won't cleanly
1745 * start-up when the vCPU is enabled.
1746 *
1747 * cpu->halted should ensure we sleep in wait_io_event
1748 */
1749 g_assert(cpu->halted);
1750 break;
Pranith Kumar08e73c42017-02-23 18:29:15 +00001751 case EXCP_ATOMIC:
1752 qemu_mutex_unlock_iothread();
1753 cpu_exec_step_atomic(cpu);
1754 qemu_mutex_lock_iothread();
Alex Bennée37257942017-02-23 18:29:14 +00001755 default:
1756 /* Ignore everything else? */
1757 break;
1758 }
1759 }
1760
Alex Bennée37257942017-02-23 18:29:14 +00001761 atomic_mb_set(&cpu->exit_request, 0);
Paolo Bonzinidb08b682018-01-11 13:53:12 +01001762 qemu_wait_io_event(cpu);
Paolo Bonzini9b0605f2018-01-30 11:05:06 -05001763 } while (!cpu->unplug || cpu_can_run(cpu));
Alex Bennée37257942017-02-23 18:29:14 +00001764
Paolo Bonzini9b0605f2018-01-30 11:05:06 -05001765 qemu_tcg_destroy_vcpu(cpu);
1766 cpu->created = false;
1767 qemu_cond_signal(&qemu_cpu_cond);
1768 qemu_mutex_unlock_iothread();
1769 rcu_unregister_thread();
Alex Bennée37257942017-02-23 18:29:14 +00001770 return NULL;
1771}
1772
Andreas Färber2ff09a42012-05-03 00:23:30 +02001773static void qemu_cpu_kick_thread(CPUState *cpu)
Paolo Bonzinicc015e92011-03-12 17:44:08 +01001774{
1775#ifndef _WIN32
1776 int err;
1777
Paolo Bonzinie0c38212015-08-26 00:19:19 +02001778 if (cpu->thread_kicked) {
1779 return;
Paolo Bonzini9102ded2015-08-18 06:52:09 -07001780 }
Paolo Bonzinie0c38212015-08-26 00:19:19 +02001781 cpu->thread_kicked = true;
Andreas Färber814e6122012-05-02 17:00:37 +02001782 err = pthread_kill(cpu->thread->thread, SIG_IPI);
Laurent Vivierd455ebc2019-01-02 15:16:03 +01001783 if (err && err != ESRCH) {
Paolo Bonzinicc015e92011-03-12 17:44:08 +01001784 fprintf(stderr, "qemu:%s: %s", __func__, strerror(err));
1785 exit(1);
1786 }
1787#else /* _WIN32 */
Vincent Palatinb0cb0a62017-01-10 11:59:57 +01001788 if (!qemu_cpu_is_self(cpu)) {
Justin Terry (VM)19306802018-01-22 13:07:49 -08001789 if (whpx_enabled()) {
1790 whpx_vcpu_kick(cpu);
1791 } else if (!QueueUserAPC(dummy_apc_func, cpu->hThread, 0)) {
Vincent Palatinb0cb0a62017-01-10 11:59:57 +01001792 fprintf(stderr, "%s: QueueUserAPC failed with error %lu\n",
1793 __func__, GetLastError());
1794 exit(1);
1795 }
1796 }
Paolo Bonzinicc015e92011-03-12 17:44:08 +01001797#endif
1798}
1799
Andreas Färberc08d7422012-05-03 04:34:15 +02001800void qemu_cpu_kick(CPUState *cpu)
Blue Swirl296af7c2010-03-29 19:23:50 +00001801{
Andreas Färberf5c121b2012-05-03 01:22:49 +02001802 qemu_cond_broadcast(cpu->halt_cond);
Paolo Bonzinie0c38212015-08-26 00:19:19 +02001803 if (tcg_enabled()) {
Alex Bennée791158d2017-02-23 18:29:10 +00001804 cpu_exit(cpu);
Alex Bennée37257942017-02-23 18:29:14 +00001805 /* NOP unless doing single-thread RR */
Alex Bennée791158d2017-02-23 18:29:10 +00001806 qemu_cpu_kick_rr_cpu();
Paolo Bonzinie0c38212015-08-26 00:19:19 +02001807 } else {
Vincent Palatinb0cb0a62017-01-10 11:59:57 +01001808 if (hax_enabled()) {
1809 /*
1810 * FIXME: race condition with the exit_request check in
1811 * hax_vcpu_hax_exec
1812 */
1813 cpu->exit_request = 1;
1814 }
Paolo Bonzinie0c38212015-08-26 00:19:19 +02001815 qemu_cpu_kick_thread(cpu);
1816 }
Blue Swirl296af7c2010-03-29 19:23:50 +00001817}
1818
Jan Kiszka46d62fa2011-02-01 22:15:59 +01001819void qemu_cpu_kick_self(void)
1820{
Andreas Färber4917cf42013-05-27 05:17:50 +02001821 assert(current_cpu);
Paolo Bonzini9102ded2015-08-18 06:52:09 -07001822 qemu_cpu_kick_thread(current_cpu);
Blue Swirl296af7c2010-03-29 19:23:50 +00001823}
1824
Andreas Färber60e82572012-05-02 22:23:49 +02001825bool qemu_cpu_is_self(CPUState *cpu)
Blue Swirl296af7c2010-03-29 19:23:50 +00001826{
Andreas Färber814e6122012-05-02 17:00:37 +02001827 return qemu_thread_is_self(cpu->thread);
Blue Swirl296af7c2010-03-29 19:23:50 +00001828}
1829
Paolo Bonzini79e2b9a2015-01-21 12:09:14 +01001830bool qemu_in_vcpu_thread(void)
Juan Quintelaaa723c22012-09-18 16:30:11 +02001831{
Andreas Färber4917cf42013-05-27 05:17:50 +02001832 return current_cpu && qemu_cpu_is_self(current_cpu);
Juan Quintelaaa723c22012-09-18 16:30:11 +02001833}
1834
Paolo Bonziniafbe7052015-06-18 18:47:19 +02001835static __thread bool iothread_locked = false;
1836
1837bool qemu_mutex_iothread_locked(void)
1838{
1839 return iothread_locked;
1840}
1841
Emilio G. Cotacb764d02017-10-28 02:16:41 -04001842/*
1843 * The BQL is taken from so many places that it is worth profiling the
1844 * callers directly, instead of funneling them all through a single function.
1845 */
1846void qemu_mutex_lock_iothread_impl(const char *file, int line)
Blue Swirl296af7c2010-03-29 19:23:50 +00001847{
Emilio G. Cotacb764d02017-10-28 02:16:41 -04001848 QemuMutexLockFunc bql_lock = atomic_read(&qemu_bql_mutex_lock_func);
1849
Jan Kiszka8d04fb52017-02-23 18:29:11 +00001850 g_assert(!qemu_mutex_iothread_locked());
Emilio G. Cotacb764d02017-10-28 02:16:41 -04001851 bql_lock(&qemu_global_mutex, file, line);
Paolo Bonziniafbe7052015-06-18 18:47:19 +02001852 iothread_locked = true;
Blue Swirl296af7c2010-03-29 19:23:50 +00001853}
1854
1855void qemu_mutex_unlock_iothread(void)
1856{
Jan Kiszka8d04fb52017-02-23 18:29:11 +00001857 g_assert(qemu_mutex_iothread_locked());
Paolo Bonziniafbe7052015-06-18 18:47:19 +02001858 iothread_locked = false;
Blue Swirl296af7c2010-03-29 19:23:50 +00001859 qemu_mutex_unlock(&qemu_global_mutex);
1860}
1861
Alex Bennéee8faee02016-10-27 16:09:58 +01001862static bool all_vcpus_paused(void)
Blue Swirl296af7c2010-03-29 19:23:50 +00001863{
Andreas Färberbdc44642013-06-24 23:50:24 +02001864 CPUState *cpu;
Blue Swirl296af7c2010-03-29 19:23:50 +00001865
Andreas Färberbdc44642013-06-24 23:50:24 +02001866 CPU_FOREACH(cpu) {
Andreas Färber182735e2013-05-29 22:29:20 +02001867 if (!cpu->stopped) {
Alex Bennéee8faee02016-10-27 16:09:58 +01001868 return false;
Jan Kiszka0ab07c62011-02-07 12:19:14 +01001869 }
Blue Swirl296af7c2010-03-29 19:23:50 +00001870 }
1871
Alex Bennéee8faee02016-10-27 16:09:58 +01001872 return true;
Blue Swirl296af7c2010-03-29 19:23:50 +00001873}
1874
1875void pause_all_vcpus(void)
1876{
Andreas Färberbdc44642013-06-24 23:50:24 +02001877 CPUState *cpu;
Blue Swirl296af7c2010-03-29 19:23:50 +00001878
Alex Bligh40daca52013-08-21 16:03:02 +01001879 qemu_clock_enable(QEMU_CLOCK_VIRTUAL, false);
Andreas Färberbdc44642013-06-24 23:50:24 +02001880 CPU_FOREACH(cpu) {
David Hildenbrandebd05fe2017-11-29 20:12:15 +01001881 if (qemu_cpu_is_self(cpu)) {
1882 qemu_cpu_stop(cpu, true);
1883 } else {
1884 cpu->stop = true;
1885 qemu_cpu_kick(cpu);
1886 }
Jan Kiszkad798e972012-02-17 18:31:16 +01001887 }
1888
Alex Bennéed759c952018-02-27 12:52:48 +03001889 /* We need to drop the replay_lock so any vCPU threads woken up
1890 * can finish their replay tasks
1891 */
1892 replay_mutex_unlock();
1893
Blue Swirl296af7c2010-03-29 19:23:50 +00001894 while (!all_vcpus_paused()) {
Paolo Bonzinibe7d6c52011-03-12 17:44:02 +01001895 qemu_cond_wait(&qemu_pause_cond, &qemu_global_mutex);
Andreas Färberbdc44642013-06-24 23:50:24 +02001896 CPU_FOREACH(cpu) {
Andreas Färber182735e2013-05-29 22:29:20 +02001897 qemu_cpu_kick(cpu);
Blue Swirl296af7c2010-03-29 19:23:50 +00001898 }
1899 }
Alex Bennéed759c952018-02-27 12:52:48 +03001900
1901 qemu_mutex_unlock_iothread();
1902 replay_mutex_lock();
1903 qemu_mutex_lock_iothread();
Blue Swirl296af7c2010-03-29 19:23:50 +00001904}
1905
Igor Mammedov29936832013-04-23 10:29:37 +02001906void cpu_resume(CPUState *cpu)
1907{
1908 cpu->stop = false;
1909 cpu->stopped = false;
1910 qemu_cpu_kick(cpu);
1911}
1912
Blue Swirl296af7c2010-03-29 19:23:50 +00001913void resume_all_vcpus(void)
1914{
Andreas Färberbdc44642013-06-24 23:50:24 +02001915 CPUState *cpu;
Blue Swirl296af7c2010-03-29 19:23:50 +00001916
Alex Bligh40daca52013-08-21 16:03:02 +01001917 qemu_clock_enable(QEMU_CLOCK_VIRTUAL, true);
Andreas Färberbdc44642013-06-24 23:50:24 +02001918 CPU_FOREACH(cpu) {
Andreas Färber182735e2013-05-29 22:29:20 +02001919 cpu_resume(cpu);
Blue Swirl296af7c2010-03-29 19:23:50 +00001920 }
1921}
1922
Paolo Bonzinidbadee42018-01-30 16:40:12 +01001923void cpu_remove_sync(CPUState *cpu)
Gu Zheng4c055ab2016-05-12 09:18:13 +05301924{
1925 cpu->stop = true;
1926 cpu->unplug = true;
1927 qemu_cpu_kick(cpu);
Paolo Bonzinidbadee42018-01-30 16:40:12 +01001928 qemu_mutex_unlock_iothread();
1929 qemu_thread_join(cpu->thread);
1930 qemu_mutex_lock_iothread();
Bharata B Rao2c579042016-05-12 09:18:14 +05301931}
1932
Dr. David Alan Gilbert49001162014-01-30 10:20:32 +00001933/* For temporary buffers for forming a name */
1934#define VCPU_THREAD_NAME_SIZE 16
1935
Andreas Färbere5ab30a2012-05-03 01:50:44 +02001936static void qemu_tcg_init_vcpu(CPUState *cpu)
Blue Swirl296af7c2010-03-29 19:23:50 +00001937{
Dr. David Alan Gilbert49001162014-01-30 10:20:32 +00001938 char thread_name[VCPU_THREAD_NAME_SIZE];
Alex Bennée37257942017-02-23 18:29:14 +00001939 static QemuCond *single_tcg_halt_cond;
1940 static QemuThread *single_tcg_cpu_thread;
Emilio G. Cotae8feb962017-07-07 19:24:20 -04001941 static int tcg_region_inited;
1942
Emilio G. Cotaf28d0df2018-06-22 13:45:31 -04001943 assert(tcg_enabled());
Emilio G. Cotae8feb962017-07-07 19:24:20 -04001944 /*
1945 * Initialize TCG regions--once. Now is a good time, because:
1946 * (1) TCG's init context, prologue and target globals have been set up.
1947 * (2) qemu_tcg_mttcg_enabled() works now (TCG init code runs before the
1948 * -accel flag is processed, so the check doesn't work then).
1949 */
1950 if (!tcg_region_inited) {
1951 tcg_region_inited = 1;
1952 tcg_region_init();
1953 }
Dr. David Alan Gilbert49001162014-01-30 10:20:32 +00001954
Alex Bennée37257942017-02-23 18:29:14 +00001955 if (qemu_tcg_mttcg_enabled() || !single_tcg_cpu_thread) {
Andreas Färber814e6122012-05-02 17:00:37 +02001956 cpu->thread = g_malloc0(sizeof(QemuThread));
Andreas Färberf5c121b2012-05-03 01:22:49 +02001957 cpu->halt_cond = g_malloc0(sizeof(QemuCond));
1958 qemu_cond_init(cpu->halt_cond);
Alex Bennée37257942017-02-23 18:29:14 +00001959
1960 if (qemu_tcg_mttcg_enabled()) {
1961 /* create a thread per vCPU with TCG (MTTCG) */
1962 parallel_cpus = true;
1963 snprintf(thread_name, VCPU_THREAD_NAME_SIZE, "CPU %d/TCG",
Dr. David Alan Gilbert49001162014-01-30 10:20:32 +00001964 cpu->cpu_index);
Alex Bennée37257942017-02-23 18:29:14 +00001965
1966 qemu_thread_create(cpu->thread, thread_name, qemu_tcg_cpu_thread_fn,
1967 cpu, QEMU_THREAD_JOINABLE);
1968
1969 } else {
1970 /* share a single thread for all cpus with TCG */
1971 snprintf(thread_name, VCPU_THREAD_NAME_SIZE, "ALL CPUs/TCG");
1972 qemu_thread_create(cpu->thread, thread_name,
1973 qemu_tcg_rr_cpu_thread_fn,
1974 cpu, QEMU_THREAD_JOINABLE);
1975
1976 single_tcg_halt_cond = cpu->halt_cond;
1977 single_tcg_cpu_thread = cpu->thread;
1978 }
Paolo Bonzini1ecf47b2011-12-13 13:43:52 +01001979#ifdef _WIN32
Andreas Färber814e6122012-05-02 17:00:37 +02001980 cpu->hThread = qemu_thread_get_handle(cpu->thread);
Paolo Bonzini1ecf47b2011-12-13 13:43:52 +01001981#endif
Blue Swirl296af7c2010-03-29 19:23:50 +00001982 } else {
Alex Bennée37257942017-02-23 18:29:14 +00001983 /* For non-MTTCG cases we share the thread */
1984 cpu->thread = single_tcg_cpu_thread;
1985 cpu->halt_cond = single_tcg_halt_cond;
David Hildenbranda3421732018-02-09 20:52:37 +01001986 cpu->thread_id = first_cpu->thread_id;
1987 cpu->can_do_io = 1;
1988 cpu->created = true;
Blue Swirl296af7c2010-03-29 19:23:50 +00001989 }
1990}
1991
Vincent Palatinb0cb0a62017-01-10 11:59:57 +01001992static void qemu_hax_start_vcpu(CPUState *cpu)
1993{
1994 char thread_name[VCPU_THREAD_NAME_SIZE];
1995
1996 cpu->thread = g_malloc0(sizeof(QemuThread));
1997 cpu->halt_cond = g_malloc0(sizeof(QemuCond));
1998 qemu_cond_init(cpu->halt_cond);
1999
2000 snprintf(thread_name, VCPU_THREAD_NAME_SIZE, "CPU %d/HAX",
2001 cpu->cpu_index);
2002 qemu_thread_create(cpu->thread, thread_name, qemu_hax_cpu_thread_fn,
2003 cpu, QEMU_THREAD_JOINABLE);
2004#ifdef _WIN32
2005 cpu->hThread = qemu_thread_get_handle(cpu->thread);
2006#endif
Vincent Palatinb0cb0a62017-01-10 11:59:57 +01002007}
2008
Andreas Färber48a106b2013-05-27 02:20:39 +02002009static void qemu_kvm_start_vcpu(CPUState *cpu)
Blue Swirl296af7c2010-03-29 19:23:50 +00002010{
Dr. David Alan Gilbert49001162014-01-30 10:20:32 +00002011 char thread_name[VCPU_THREAD_NAME_SIZE];
2012
Andreas Färber814e6122012-05-02 17:00:37 +02002013 cpu->thread = g_malloc0(sizeof(QemuThread));
Andreas Färberf5c121b2012-05-03 01:22:49 +02002014 cpu->halt_cond = g_malloc0(sizeof(QemuCond));
2015 qemu_cond_init(cpu->halt_cond);
Dr. David Alan Gilbert49001162014-01-30 10:20:32 +00002016 snprintf(thread_name, VCPU_THREAD_NAME_SIZE, "CPU %d/KVM",
2017 cpu->cpu_index);
2018 qemu_thread_create(cpu->thread, thread_name, qemu_kvm_cpu_thread_fn,
2019 cpu, QEMU_THREAD_JOINABLE);
Blue Swirl296af7c2010-03-29 19:23:50 +00002020}
2021
Sergio Andres Gomez Del Realc97d6d22017-09-13 04:05:09 -05002022static void qemu_hvf_start_vcpu(CPUState *cpu)
2023{
2024 char thread_name[VCPU_THREAD_NAME_SIZE];
2025
2026 /* HVF currently does not support TCG, and only runs in
2027 * unrestricted-guest mode. */
2028 assert(hvf_enabled());
2029
2030 cpu->thread = g_malloc0(sizeof(QemuThread));
2031 cpu->halt_cond = g_malloc0(sizeof(QemuCond));
2032 qemu_cond_init(cpu->halt_cond);
2033
2034 snprintf(thread_name, VCPU_THREAD_NAME_SIZE, "CPU %d/HVF",
2035 cpu->cpu_index);
2036 qemu_thread_create(cpu->thread, thread_name, qemu_hvf_cpu_thread_fn,
2037 cpu, QEMU_THREAD_JOINABLE);
Sergio Andres Gomez Del Realc97d6d22017-09-13 04:05:09 -05002038}
2039
Justin Terry (VM)19306802018-01-22 13:07:49 -08002040static void qemu_whpx_start_vcpu(CPUState *cpu)
2041{
2042 char thread_name[VCPU_THREAD_NAME_SIZE];
2043
2044 cpu->thread = g_malloc0(sizeof(QemuThread));
2045 cpu->halt_cond = g_malloc0(sizeof(QemuCond));
2046 qemu_cond_init(cpu->halt_cond);
2047 snprintf(thread_name, VCPU_THREAD_NAME_SIZE, "CPU %d/WHPX",
2048 cpu->cpu_index);
2049 qemu_thread_create(cpu->thread, thread_name, qemu_whpx_cpu_thread_fn,
2050 cpu, QEMU_THREAD_JOINABLE);
2051#ifdef _WIN32
2052 cpu->hThread = qemu_thread_get_handle(cpu->thread);
2053#endif
Justin Terry (VM)19306802018-01-22 13:07:49 -08002054}
2055
Andreas Färber10a90212013-05-27 02:24:35 +02002056static void qemu_dummy_start_vcpu(CPUState *cpu)
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +02002057{
Dr. David Alan Gilbert49001162014-01-30 10:20:32 +00002058 char thread_name[VCPU_THREAD_NAME_SIZE];
2059
Andreas Färber814e6122012-05-02 17:00:37 +02002060 cpu->thread = g_malloc0(sizeof(QemuThread));
Andreas Färberf5c121b2012-05-03 01:22:49 +02002061 cpu->halt_cond = g_malloc0(sizeof(QemuCond));
2062 qemu_cond_init(cpu->halt_cond);
Dr. David Alan Gilbert49001162014-01-30 10:20:32 +00002063 snprintf(thread_name, VCPU_THREAD_NAME_SIZE, "CPU %d/DUMMY",
2064 cpu->cpu_index);
2065 qemu_thread_create(cpu->thread, thread_name, qemu_dummy_cpu_thread_fn, cpu,
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +02002066 QEMU_THREAD_JOINABLE);
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +02002067}
2068
Andreas Färberc643bed2013-05-27 03:23:24 +02002069void qemu_init_vcpu(CPUState *cpu)
Blue Swirl296af7c2010-03-29 19:23:50 +00002070{
Andreas Färberce3960e2012-12-17 03:27:07 +01002071 cpu->nr_cores = smp_cores;
2072 cpu->nr_threads = smp_threads;
Andreas Färberf324e762012-05-02 23:26:21 +02002073 cpu->stopped = true;
Peter Maydell56943e82016-01-21 14:15:04 +00002074
2075 if (!cpu->as) {
2076 /* If the target cpu hasn't set up any address spaces itself,
2077 * give it the default one.
2078 */
Peter Maydell12ebc9a2016-01-21 14:15:04 +00002079 cpu->num_ases = 1;
Peter Xu80ceb072017-11-23 17:23:32 +08002080 cpu_address_space_init(cpu, 0, "cpu-memory", cpu->memory);
Peter Maydell56943e82016-01-21 14:15:04 +00002081 }
2082
Jan Kiszka0ab07c62011-02-07 12:19:14 +01002083 if (kvm_enabled()) {
Andreas Färber48a106b2013-05-27 02:20:39 +02002084 qemu_kvm_start_vcpu(cpu);
Vincent Palatinb0cb0a62017-01-10 11:59:57 +01002085 } else if (hax_enabled()) {
2086 qemu_hax_start_vcpu(cpu);
Sergio Andres Gomez Del Realc97d6d22017-09-13 04:05:09 -05002087 } else if (hvf_enabled()) {
2088 qemu_hvf_start_vcpu(cpu);
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +02002089 } else if (tcg_enabled()) {
Andreas Färbere5ab30a2012-05-03 01:50:44 +02002090 qemu_tcg_init_vcpu(cpu);
Justin Terry (VM)19306802018-01-22 13:07:49 -08002091 } else if (whpx_enabled()) {
2092 qemu_whpx_start_vcpu(cpu);
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +02002093 } else {
Andreas Färber10a90212013-05-27 02:24:35 +02002094 qemu_dummy_start_vcpu(cpu);
Jan Kiszka0ab07c62011-02-07 12:19:14 +01002095 }
David Hildenbrand81e96312018-02-09 20:52:38 +01002096
2097 while (!cpu->created) {
2098 qemu_cond_wait(&qemu_cpu_cond, &qemu_global_mutex);
2099 }
Blue Swirl296af7c2010-03-29 19:23:50 +00002100}
2101
Jan Kiszkab4a3d962011-02-01 22:15:43 +01002102void cpu_stop_current(void)
Blue Swirl296af7c2010-03-29 19:23:50 +00002103{
Andreas Färber4917cf42013-05-27 05:17:50 +02002104 if (current_cpu) {
Peter Maydell0ec7e672019-01-07 15:23:47 +00002105 current_cpu->stop = true;
2106 cpu_exit(current_cpu);
Jan Kiszkab4a3d962011-02-01 22:15:43 +01002107 }
Blue Swirl296af7c2010-03-29 19:23:50 +00002108}
2109
Kevin Wolf56983462013-07-05 13:49:54 +02002110int vm_stop(RunState state)
Blue Swirl296af7c2010-03-29 19:23:50 +00002111{
Juan Quintelaaa723c22012-09-18 16:30:11 +02002112 if (qemu_in_vcpu_thread()) {
Paolo Bonzini74892d22014-06-05 14:53:58 +02002113 qemu_system_vmstop_request_prepare();
Luiz Capitulino1dfb4dd2011-07-29 14:26:33 -03002114 qemu_system_vmstop_request(state);
Blue Swirl296af7c2010-03-29 19:23:50 +00002115 /*
2116 * FIXME: should not return to device code in case
2117 * vm_stop() has been requested.
2118 */
Jan Kiszkab4a3d962011-02-01 22:15:43 +01002119 cpu_stop_current();
Kevin Wolf56983462013-07-05 13:49:54 +02002120 return 0;
Blue Swirl296af7c2010-03-29 19:23:50 +00002121 }
Kevin Wolf56983462013-07-05 13:49:54 +02002122
Stefan Hajnoczi4486e892018-03-07 14:42:05 +00002123 return do_vm_stop(state, true);
Blue Swirl296af7c2010-03-29 19:23:50 +00002124}
2125
Claudio Imbrenda2d76e822017-02-14 18:07:47 +01002126/**
2127 * Prepare for (re)starting the VM.
2128 * Returns -1 if the vCPUs are not to be restarted (e.g. if they are already
2129 * running or in case of an error condition), 0 otherwise.
2130 */
2131int vm_prepare_start(void)
2132{
2133 RunState requested;
Claudio Imbrenda2d76e822017-02-14 18:07:47 +01002134
2135 qemu_vmstop_requested(&requested);
2136 if (runstate_is_running() && requested == RUN_STATE__MAX) {
2137 return -1;
2138 }
2139
2140 /* Ensure that a STOP/RESUME pair of events is emitted if a
2141 * vmstop request was pending. The BLOCK_IO_ERROR event, for
2142 * example, according to documentation is always followed by
2143 * the STOP event.
2144 */
2145 if (runstate_is_running()) {
Peter Xu3ab72382018-08-15 21:37:37 +08002146 qapi_event_send_stop();
2147 qapi_event_send_resume();
Markus Armbrusterf0561582018-04-23 10:45:18 +02002148 return -1;
Claudio Imbrenda2d76e822017-02-14 18:07:47 +01002149 }
2150
2151 /* We are sending this now, but the CPUs will be resumed shortly later */
Peter Xu3ab72382018-08-15 21:37:37 +08002152 qapi_event_send_resume();
Markus Armbrusterf0561582018-04-23 10:45:18 +02002153
2154 replay_enable_events();
2155 cpu_enable_ticks();
2156 runstate_set(RUN_STATE_RUNNING);
2157 vm_state_notify(1, RUN_STATE_RUNNING);
2158 return 0;
Claudio Imbrenda2d76e822017-02-14 18:07:47 +01002159}
2160
2161void vm_start(void)
2162{
2163 if (!vm_prepare_start()) {
2164 resume_all_vcpus();
2165 }
2166}
2167
Luiz Capitulino8a9236f2011-10-14 11:18:09 -03002168/* does a state transition even if the VM is already stopped,
2169 current state is forgotten forever */
Kevin Wolf56983462013-07-05 13:49:54 +02002170int vm_stop_force_state(RunState state)
Luiz Capitulino8a9236f2011-10-14 11:18:09 -03002171{
2172 if (runstate_is_running()) {
Kevin Wolf56983462013-07-05 13:49:54 +02002173 return vm_stop(state);
Luiz Capitulino8a9236f2011-10-14 11:18:09 -03002174 } else {
2175 runstate_set(state);
Wen Congyangb2780d32015-11-20 17:34:38 +08002176
2177 bdrv_drain_all();
Kevin Wolf594a45c2013-07-18 14:52:19 +02002178 /* Make sure to return an error if the flush in a previous vm_stop()
2179 * failed. */
John Snow22af08e2016-09-22 21:45:51 -04002180 return bdrv_flush_all();
Luiz Capitulino8a9236f2011-10-14 11:18:09 -03002181 }
2182}
2183
Stefan Weil9a78eea2010-10-22 23:03:33 +02002184void list_cpus(FILE *f, fprintf_function cpu_fprintf, const char *optarg)
Blue Swirl262353c2010-05-04 19:55:35 +00002185{
2186 /* XXX: implement xxx_cpu_list for targets that still miss it */
Peter Maydelle916cbf2012-09-05 17:41:08 -03002187#if defined(cpu_list)
2188 cpu_list(f, cpu_fprintf);
Blue Swirl262353c2010-05-04 19:55:35 +00002189#endif
2190}
Luiz Capitulinode0b36b2011-09-21 16:38:35 -03002191
2192CpuInfoList *qmp_query_cpus(Error **errp)
2193{
Igor Mammedovafed5a52017-05-10 13:29:55 +02002194 MachineState *ms = MACHINE(qdev_get_machine());
2195 MachineClass *mc = MACHINE_GET_CLASS(ms);
Luiz Capitulinode0b36b2011-09-21 16:38:35 -03002196 CpuInfoList *head = NULL, *cur_item = NULL;
Andreas Färber182735e2013-05-29 22:29:20 +02002197 CPUState *cpu;
Luiz Capitulinode0b36b2011-09-21 16:38:35 -03002198
Andreas Färberbdc44642013-06-24 23:50:24 +02002199 CPU_FOREACH(cpu) {
Luiz Capitulinode0b36b2011-09-21 16:38:35 -03002200 CpuInfoList *info;
Andreas Färber182735e2013-05-29 22:29:20 +02002201#if defined(TARGET_I386)
2202 X86CPU *x86_cpu = X86_CPU(cpu);
2203 CPUX86State *env = &x86_cpu->env;
2204#elif defined(TARGET_PPC)
2205 PowerPCCPU *ppc_cpu = POWERPC_CPU(cpu);
2206 CPUPPCState *env = &ppc_cpu->env;
2207#elif defined(TARGET_SPARC)
2208 SPARCCPU *sparc_cpu = SPARC_CPU(cpu);
2209 CPUSPARCState *env = &sparc_cpu->env;
Michael Clark25fa1942018-03-03 01:32:59 +13002210#elif defined(TARGET_RISCV)
2211 RISCVCPU *riscv_cpu = RISCV_CPU(cpu);
2212 CPURISCVState *env = &riscv_cpu->env;
Andreas Färber182735e2013-05-29 22:29:20 +02002213#elif defined(TARGET_MIPS)
2214 MIPSCPU *mips_cpu = MIPS_CPU(cpu);
2215 CPUMIPSState *env = &mips_cpu->env;
Bastian Koppelmann48e06fe2014-09-01 12:59:46 +01002216#elif defined(TARGET_TRICORE)
2217 TriCoreCPU *tricore_cpu = TRICORE_CPU(cpu);
2218 CPUTriCoreState *env = &tricore_cpu->env;
Viktor Mihajlovski9d0306d2018-02-16 17:08:37 +01002219#elif defined(TARGET_S390X)
2220 S390CPU *s390_cpu = S390_CPU(cpu);
2221 CPUS390XState *env = &s390_cpu->env;
Andreas Färber182735e2013-05-29 22:29:20 +02002222#endif
Luiz Capitulinode0b36b2011-09-21 16:38:35 -03002223
Andreas Färbercb446ec2013-05-01 14:24:52 +02002224 cpu_synchronize_state(cpu);
Luiz Capitulinode0b36b2011-09-21 16:38:35 -03002225
2226 info = g_malloc0(sizeof(*info));
2227 info->value = g_malloc0(sizeof(*info->value));
Andreas Färber55e5c282012-12-17 06:18:02 +01002228 info->value->CPU = cpu->cpu_index;
Andreas Färber182735e2013-05-29 22:29:20 +02002229 info->value->current = (cpu == first_cpu);
Andreas Färber259186a2013-01-17 18:51:17 +01002230 info->value->halted = cpu->halted;
Eduardo Habkost58f88d42015-05-08 16:04:22 -03002231 info->value->qom_path = object_get_canonical_path(OBJECT(cpu));
Andreas Färber9f09e182012-05-03 06:59:07 +02002232 info->value->thread_id = cpu->thread_id;
Luiz Capitulinode0b36b2011-09-21 16:38:35 -03002233#if defined(TARGET_I386)
Eric Blake86f4b682015-11-18 01:52:59 -07002234 info->value->arch = CPU_INFO_ARCH_X86;
Eric Blake544a3732016-02-17 23:48:27 -07002235 info->value->u.x86.pc = env->eip + env->segs[R_CS].base;
Luiz Capitulinode0b36b2011-09-21 16:38:35 -03002236#elif defined(TARGET_PPC)
Eric Blake86f4b682015-11-18 01:52:59 -07002237 info->value->arch = CPU_INFO_ARCH_PPC;
Eric Blake544a3732016-02-17 23:48:27 -07002238 info->value->u.ppc.nip = env->nip;
Luiz Capitulinode0b36b2011-09-21 16:38:35 -03002239#elif defined(TARGET_SPARC)
Eric Blake86f4b682015-11-18 01:52:59 -07002240 info->value->arch = CPU_INFO_ARCH_SPARC;
Eric Blake544a3732016-02-17 23:48:27 -07002241 info->value->u.q_sparc.pc = env->pc;
2242 info->value->u.q_sparc.npc = env->npc;
Luiz Capitulinode0b36b2011-09-21 16:38:35 -03002243#elif defined(TARGET_MIPS)
Eric Blake86f4b682015-11-18 01:52:59 -07002244 info->value->arch = CPU_INFO_ARCH_MIPS;
Eric Blake544a3732016-02-17 23:48:27 -07002245 info->value->u.q_mips.PC = env->active_tc.PC;
Bastian Koppelmann48e06fe2014-09-01 12:59:46 +01002246#elif defined(TARGET_TRICORE)
Eric Blake86f4b682015-11-18 01:52:59 -07002247 info->value->arch = CPU_INFO_ARCH_TRICORE;
Eric Blake544a3732016-02-17 23:48:27 -07002248 info->value->u.tricore.PC = env->PC;
Viktor Mihajlovski9d0306d2018-02-16 17:08:37 +01002249#elif defined(TARGET_S390X)
2250 info->value->arch = CPU_INFO_ARCH_S390;
2251 info->value->u.s390.cpu_state = env->cpu_state;
Michael Clark25fa1942018-03-03 01:32:59 +13002252#elif defined(TARGET_RISCV)
2253 info->value->arch = CPU_INFO_ARCH_RISCV;
2254 info->value->u.riscv.pc = env->pc;
Eric Blake86f4b682015-11-18 01:52:59 -07002255#else
2256 info->value->arch = CPU_INFO_ARCH_OTHER;
Luiz Capitulinode0b36b2011-09-21 16:38:35 -03002257#endif
Igor Mammedovafed5a52017-05-10 13:29:55 +02002258 info->value->has_props = !!mc->cpu_index_to_instance_props;
2259 if (info->value->has_props) {
2260 CpuInstanceProperties *props;
2261 props = g_malloc0(sizeof(*props));
2262 *props = mc->cpu_index_to_instance_props(ms, cpu->cpu_index);
2263 info->value->props = props;
2264 }
Luiz Capitulinode0b36b2011-09-21 16:38:35 -03002265
2266 /* XXX: waiting for the qapi to support GSList */
2267 if (!cur_item) {
2268 head = cur_item = info;
2269 } else {
2270 cur_item->next = info;
2271 cur_item = info;
2272 }
2273 }
2274
2275 return head;
2276}
Luiz Capitulino0cfd6a92011-11-22 16:32:37 -02002277
Laszlo Ersekdaa9d2b2018-04-27 21:28:51 +02002278static CpuInfoArch sysemu_target_to_cpuinfo_arch(SysEmuTarget target)
2279{
2280 /*
2281 * The @SysEmuTarget -> @CpuInfoArch mapping below is based on the
2282 * TARGET_ARCH -> TARGET_BASE_ARCH mapping in the "configure" script.
2283 */
2284 switch (target) {
2285 case SYS_EMU_TARGET_I386:
2286 case SYS_EMU_TARGET_X86_64:
2287 return CPU_INFO_ARCH_X86;
2288
2289 case SYS_EMU_TARGET_PPC:
Laszlo Ersekdaa9d2b2018-04-27 21:28:51 +02002290 case SYS_EMU_TARGET_PPC64:
2291 return CPU_INFO_ARCH_PPC;
2292
2293 case SYS_EMU_TARGET_SPARC:
2294 case SYS_EMU_TARGET_SPARC64:
2295 return CPU_INFO_ARCH_SPARC;
2296
2297 case SYS_EMU_TARGET_MIPS:
2298 case SYS_EMU_TARGET_MIPSEL:
2299 case SYS_EMU_TARGET_MIPS64:
2300 case SYS_EMU_TARGET_MIPS64EL:
2301 return CPU_INFO_ARCH_MIPS;
2302
2303 case SYS_EMU_TARGET_TRICORE:
2304 return CPU_INFO_ARCH_TRICORE;
2305
2306 case SYS_EMU_TARGET_S390X:
2307 return CPU_INFO_ARCH_S390;
2308
2309 case SYS_EMU_TARGET_RISCV32:
2310 case SYS_EMU_TARGET_RISCV64:
2311 return CPU_INFO_ARCH_RISCV;
2312
2313 default:
2314 return CPU_INFO_ARCH_OTHER;
2315 }
2316}
2317
2318static void cpustate_to_cpuinfo_s390(CpuInfoS390 *info, const CPUState *cpu)
2319{
2320#ifdef TARGET_S390X
2321 S390CPU *s390_cpu = S390_CPU(cpu);
2322 CPUS390XState *env = &s390_cpu->env;
2323
2324 info->cpu_state = env->cpu_state;
2325#else
2326 abort();
2327#endif
2328}
2329
Luiz Capitulinoce74ee32018-02-16 17:08:38 +01002330/*
2331 * fast means: we NEVER interrupt vCPU threads to retrieve
2332 * information from KVM.
2333 */
2334CpuInfoFastList *qmp_query_cpus_fast(Error **errp)
2335{
2336 MachineState *ms = MACHINE(qdev_get_machine());
2337 MachineClass *mc = MACHINE_GET_CLASS(ms);
2338 CpuInfoFastList *head = NULL, *cur_item = NULL;
Laszlo Ersekdaa9d2b2018-04-27 21:28:51 +02002339 SysEmuTarget target = qapi_enum_parse(&SysEmuTarget_lookup, TARGET_NAME,
2340 -1, &error_abort);
Luiz Capitulinoce74ee32018-02-16 17:08:38 +01002341 CPUState *cpu;
2342
2343 CPU_FOREACH(cpu) {
2344 CpuInfoFastList *info = g_malloc0(sizeof(*info));
2345 info->value = g_malloc0(sizeof(*info->value));
2346
2347 info->value->cpu_index = cpu->cpu_index;
2348 info->value->qom_path = object_get_canonical_path(OBJECT(cpu));
2349 info->value->thread_id = cpu->thread_id;
2350
2351 info->value->has_props = !!mc->cpu_index_to_instance_props;
2352 if (info->value->has_props) {
2353 CpuInstanceProperties *props;
2354 props = g_malloc0(sizeof(*props));
2355 *props = mc->cpu_index_to_instance_props(ms, cpu->cpu_index);
2356 info->value->props = props;
2357 }
2358
Laszlo Ersekdaa9d2b2018-04-27 21:28:51 +02002359 info->value->arch = sysemu_target_to_cpuinfo_arch(target);
2360 info->value->target = target;
2361 if (target == SYS_EMU_TARGET_S390X) {
2362 cpustate_to_cpuinfo_s390(&info->value->u.s390x, cpu);
Laszlo Ersekdaa9d2b2018-04-27 21:28:51 +02002363 }
2364
Luiz Capitulinoce74ee32018-02-16 17:08:38 +01002365 if (!cur_item) {
2366 head = cur_item = info;
2367 } else {
2368 cur_item->next = info;
2369 cur_item = info;
2370 }
2371 }
2372
2373 return head;
2374}
2375
Luiz Capitulino0cfd6a92011-11-22 16:32:37 -02002376void qmp_memsave(int64_t addr, int64_t size, const char *filename,
2377 bool has_cpu, int64_t cpu_index, Error **errp)
2378{
2379 FILE *f;
2380 uint32_t l;
Andreas Färber55e5c282012-12-17 06:18:02 +01002381 CPUState *cpu;
Luiz Capitulino0cfd6a92011-11-22 16:32:37 -02002382 uint8_t buf[1024];
Borislav Petkov0dc9daf2015-02-08 13:14:38 +01002383 int64_t orig_addr = addr, orig_size = size;
Luiz Capitulino0cfd6a92011-11-22 16:32:37 -02002384
2385 if (!has_cpu) {
2386 cpu_index = 0;
2387 }
2388
Andreas Färber151d1322013-02-15 15:41:49 +01002389 cpu = qemu_get_cpu(cpu_index);
2390 if (cpu == NULL) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01002391 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "cpu-index",
2392 "a CPU number");
Luiz Capitulino0cfd6a92011-11-22 16:32:37 -02002393 return;
2394 }
2395
2396 f = fopen(filename, "wb");
2397 if (!f) {
Luiz Capitulino618da852013-06-07 14:35:06 -04002398 error_setg_file_open(errp, errno, filename);
Luiz Capitulino0cfd6a92011-11-22 16:32:37 -02002399 return;
2400 }
2401
2402 while (size != 0) {
2403 l = sizeof(buf);
2404 if (l > size)
2405 l = size;
Aneesh Kumar K.V2f4d0f52013-10-01 21:49:30 +05302406 if (cpu_memory_rw_debug(cpu, addr, buf, l, 0) != 0) {
Borislav Petkov0dc9daf2015-02-08 13:14:38 +01002407 error_setg(errp, "Invalid addr 0x%016" PRIx64 "/size %" PRId64
2408 " specified", orig_addr, orig_size);
Aneesh Kumar K.V2f4d0f52013-10-01 21:49:30 +05302409 goto exit;
2410 }
Luiz Capitulino0cfd6a92011-11-22 16:32:37 -02002411 if (fwrite(buf, 1, l, f) != l) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01002412 error_setg(errp, QERR_IO_ERROR);
Luiz Capitulino0cfd6a92011-11-22 16:32:37 -02002413 goto exit;
2414 }
2415 addr += l;
2416 size -= l;
2417 }
2418
2419exit:
2420 fclose(f);
2421}
Luiz Capitulino6d3962b2011-11-22 17:26:46 -02002422
2423void qmp_pmemsave(int64_t addr, int64_t size, const char *filename,
2424 Error **errp)
2425{
2426 FILE *f;
2427 uint32_t l;
2428 uint8_t buf[1024];
2429
2430 f = fopen(filename, "wb");
2431 if (!f) {
Luiz Capitulino618da852013-06-07 14:35:06 -04002432 error_setg_file_open(errp, errno, filename);
Luiz Capitulino6d3962b2011-11-22 17:26:46 -02002433 return;
2434 }
2435
2436 while (size != 0) {
2437 l = sizeof(buf);
2438 if (l > size)
2439 l = size;
Stefan Weileb6282f2014-04-07 20:28:23 +02002440 cpu_physical_memory_read(addr, buf, l);
Luiz Capitulino6d3962b2011-11-22 17:26:46 -02002441 if (fwrite(buf, 1, l, f) != l) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01002442 error_setg(errp, QERR_IO_ERROR);
Luiz Capitulino6d3962b2011-11-22 17:26:46 -02002443 goto exit;
2444 }
2445 addr += l;
2446 size -= l;
2447 }
2448
2449exit:
2450 fclose(f);
2451}
Luiz Capitulinoab49ab52011-11-23 12:55:53 -02002452
2453void qmp_inject_nmi(Error **errp)
2454{
Alexey Kardashevskiy9cb805f2014-08-20 22:16:33 +10002455 nmi_monitor_handle(monitor_get_cpu_index(), errp);
Luiz Capitulinoab49ab52011-11-23 12:55:53 -02002456}
Sebastian Tanase27498be2014-07-25 11:56:33 +02002457
Markus Armbruster76c86612019-04-17 21:17:53 +02002458void dump_drift_info(void)
Sebastian Tanase27498be2014-07-25 11:56:33 +02002459{
2460 if (!use_icount) {
2461 return;
2462 }
2463
Markus Armbruster76c86612019-04-17 21:17:53 +02002464 qemu_printf("Host - Guest clock %"PRIi64" ms\n",
Sebastian Tanase27498be2014-07-25 11:56:33 +02002465 (cpu_get_clock() - cpu_get_icount())/SCALE_MS);
2466 if (icount_align_option) {
Markus Armbruster76c86612019-04-17 21:17:53 +02002467 qemu_printf("Max guest delay %"PRIi64" ms\n",
2468 -max_delay / SCALE_MS);
2469 qemu_printf("Max guest advance %"PRIi64" ms\n",
2470 max_advance / SCALE_MS);
Sebastian Tanase27498be2014-07-25 11:56:33 +02002471 } else {
Markus Armbruster76c86612019-04-17 21:17:53 +02002472 qemu_printf("Max guest delay NA\n");
2473 qemu_printf("Max guest advance NA\n");
Sebastian Tanase27498be2014-07-25 11:56:33 +02002474 }
2475}