blob: eef7b007aeb5d5739c5c924aa9ac29975f683916 [file] [log] [blame]
Blue Swirl296af7c2010-03-29 19:23:50 +00001/*
2 * QEMU System Emulator
3 *
4 * Copyright (c) 2003-2008 Fabrice Bellard
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
23 */
24
Peter Maydell7b31bbc2016-01-26 18:16:56 +000025#include "qemu/osdep.h"
Markus Armbrustera8d25322019-05-23 16:35:08 +020026#include "qemu-common.h"
KONRAD Frederic8d4e9142017-02-23 18:29:08 +000027#include "qemu/config-file.h"
Paolo 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"
Markus Armbruster14a48c12019-05-23 16:35:05 +020035#include "sysemu/tcg.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"
Richard Henderson9c09a252019-03-14 13:06:29 -070053#include "qemu/guest-random.h"
KONRAD Frederic8d4e9142017-02-23 18:29:08 +000054#include "tcg.h"
Alexey Kardashevskiy9cb805f2014-08-20 22:16:33 +100055#include "hw/nmi.h"
Pavel Dovgalyuk8b427042015-09-17 19:24:05 +030056#include "sysemu/replay.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{
Richard Henderson5e140192019-03-28 11:54:23 -1000241 return (cpu->icount_budget -
242 (cpu_neg(cpu)->icount_decr.u16.low + cpu->icount_extra));
Alex Bennéee4cd9652017-03-31 16:09:42 +0100243}
244
Alex Bennée512d3c82017-04-05 12:32:37 +0100245/*
246 * Update the global shared timer_state.qemu_icount to take into
247 * account executed instructions. This is done by the TCG vCPU
248 * thread so the main-loop can see time has moved forward.
249 */
Paolo Bonzini9b4e6f42018-09-11 13:15:32 +0200250static void cpu_update_icount_locked(CPUState *cpu)
Alex Bennée512d3c82017-04-05 12:32:37 +0100251{
252 int64_t executed = cpu_get_icount_executed(cpu);
253 cpu->icount_budget -= executed;
254
Emilio G. Cota38adcb62018-09-10 19:27:49 -0400255 atomic_set_i64(&timers_state.qemu_icount,
256 timers_state.qemu_icount + executed);
Paolo Bonzini9b4e6f42018-09-11 13:15:32 +0200257}
258
259/*
260 * Update the global shared timer_state.qemu_icount to take into
261 * account executed instructions. This is done by the TCG vCPU
262 * thread so the main-loop can see time has moved forward.
263 */
264void cpu_update_icount(CPUState *cpu)
265{
266 seqlock_write_lock(&timers_state.vm_clock_seqlock,
267 &timers_state.vm_clock_lock);
268 cpu_update_icount_locked(cpu);
Paolo Bonzini94377112018-08-14 09:57:16 +0200269 seqlock_write_unlock(&timers_state.vm_clock_seqlock,
270 &timers_state.vm_clock_lock);
Alex Bennée512d3c82017-04-05 12:32:37 +0100271}
272
Paolo Bonzinic1ff0732018-08-14 09:31:58 +0200273static int64_t cpu_get_icount_raw_locked(void)
Paolo Bonzini946fb272011-09-12 13:57:37 +0200274{
Andreas Färber4917cf42013-05-27 05:17:50 +0200275 CPUState *cpu = current_cpu;
Paolo Bonzini946fb272011-09-12 13:57:37 +0200276
Alex Bennée243c5f72017-03-30 18:49:22 +0100277 if (cpu && cpu->running) {
Paolo Bonzini414b15c2015-06-24 14:16:26 +0200278 if (!cpu->can_do_io) {
Alistair Francis493d89b2018-02-03 09:43:14 +0100279 error_report("Bad icount read");
Pavel Dovgalyuk2a629142014-12-08 10:53:45 +0300280 exit(1);
Paolo Bonzini946fb272011-09-12 13:57:37 +0200281 }
Alex Bennéee4cd9652017-03-31 16:09:42 +0100282 /* Take into account what has run */
Paolo Bonzini9b4e6f42018-09-11 13:15:32 +0200283 cpu_update_icount_locked(cpu);
Paolo Bonzini946fb272011-09-12 13:57:37 +0200284 }
Emilio G. Cota38adcb62018-09-10 19:27:49 -0400285 /* The read is protected by the seqlock, but needs atomic64 to avoid UB */
286 return atomic_read_i64(&timers_state.qemu_icount);
Paolo Bonzinic1ff0732018-08-14 09:31:58 +0200287}
288
289static int64_t cpu_get_icount_locked(void)
290{
291 int64_t icount = cpu_get_icount_raw_locked();
Emilio G. Cotac97595d2018-09-10 19:27:50 -0400292 return atomic_read_i64(&timers_state.qemu_icount_bias) +
293 cpu_icount_to_ns(icount);
Paolo Bonzinic1ff0732018-08-14 09:31:58 +0200294}
295
296int64_t cpu_get_icount_raw(void)
297{
298 int64_t icount;
299 unsigned start;
300
301 do {
302 start = seqlock_read_begin(&timers_state.vm_clock_seqlock);
303 icount = cpu_get_icount_raw_locked();
304 } while (seqlock_read_retry(&timers_state.vm_clock_seqlock, start));
305
306 return icount;
Pavel Dovgalyuk2a629142014-12-08 10:53:45 +0300307}
308
309/* Return the virtual CPU time, based on the instruction counter. */
Paolo Bonzini17a15f12013-10-03 15:17:25 +0200310int64_t cpu_get_icount(void)
311{
312 int64_t icount;
313 unsigned start;
314
315 do {
316 start = seqlock_read_begin(&timers_state.vm_clock_seqlock);
317 icount = cpu_get_icount_locked();
318 } while (seqlock_read_retry(&timers_state.vm_clock_seqlock, start));
319
320 return icount;
321}
322
KONRAD Frederic3f031312014-08-01 01:37:15 +0200323int64_t cpu_icount_to_ns(int64_t icount)
324{
Paolo Bonzinic1ff0732018-08-14 09:31:58 +0200325 return icount << atomic_read(&timers_state.icount_time_shift);
KONRAD Frederic3f031312014-08-01 01:37:15 +0200326}
327
Paolo Bonzinif2a4ad62018-08-18 09:36:16 +0200328static int64_t cpu_get_ticks_locked(void)
329{
330 int64_t ticks = timers_state.cpu_ticks_offset;
331 if (timers_state.cpu_ticks_enabled) {
332 ticks += cpu_get_host_ticks();
333 }
334
335 if (timers_state.cpu_ticks_prev > ticks) {
336 /* Non increasing ticks may happen if the host uses software suspend. */
337 timers_state.cpu_ticks_offset += timers_state.cpu_ticks_prev - ticks;
338 ticks = timers_state.cpu_ticks_prev;
339 }
340
341 timers_state.cpu_ticks_prev = ticks;
342 return ticks;
343}
344
Cao jind90f3cc2016-07-29 19:05:38 +0800345/* return the time elapsed in VM between vm_start and vm_stop. Unless
346 * icount is active, cpu_get_ticks() uses units of the host CPU cycle
347 * counter.
Cao jind90f3cc2016-07-29 19:05:38 +0800348 */
Paolo Bonzini946fb272011-09-12 13:57:37 +0200349int64_t cpu_get_ticks(void)
350{
Paolo Bonzini5f3e3102013-10-28 17:32:18 +0100351 int64_t ticks;
352
Paolo Bonzini946fb272011-09-12 13:57:37 +0200353 if (use_icount) {
354 return cpu_get_icount();
355 }
Paolo Bonzini5f3e3102013-10-28 17:32:18 +0100356
Paolo Bonzinif2a4ad62018-08-18 09:36:16 +0200357 qemu_spin_lock(&timers_state.vm_clock_lock);
358 ticks = cpu_get_ticks_locked();
359 qemu_spin_unlock(&timers_state.vm_clock_lock);
Paolo Bonzini5f3e3102013-10-28 17:32:18 +0100360 return ticks;
Paolo Bonzini946fb272011-09-12 13:57:37 +0200361}
362
Liu Ping Fancb365642013-09-25 14:20:58 +0800363static int64_t cpu_get_clock_locked(void)
364{
Cao jin1d45cea2016-07-29 19:05:37 +0800365 int64_t time;
Liu Ping Fancb365642013-09-25 14:20:58 +0800366
Cao jin1d45cea2016-07-29 19:05:37 +0800367 time = timers_state.cpu_clock_offset;
Paolo Bonzini5f3e3102013-10-28 17:32:18 +0100368 if (timers_state.cpu_ticks_enabled) {
Cao jin1d45cea2016-07-29 19:05:37 +0800369 time += get_clock();
Liu Ping Fancb365642013-09-25 14:20:58 +0800370 }
371
Cao jin1d45cea2016-07-29 19:05:37 +0800372 return time;
Liu Ping Fancb365642013-09-25 14:20:58 +0800373}
374
Cao jind90f3cc2016-07-29 19:05:38 +0800375/* Return the monotonic time elapsed in VM, i.e.,
Peter Maydell8212ff82016-09-15 10:24:22 +0100376 * the time between vm_start and vm_stop
377 */
Paolo Bonzini946fb272011-09-12 13:57:37 +0200378int64_t cpu_get_clock(void)
379{
380 int64_t ti;
Liu Ping Fancb365642013-09-25 14:20:58 +0800381 unsigned start;
382
383 do {
384 start = seqlock_read_begin(&timers_state.vm_clock_seqlock);
385 ti = cpu_get_clock_locked();
386 } while (seqlock_read_retry(&timers_state.vm_clock_seqlock, start));
387
388 return ti;
Paolo Bonzini946fb272011-09-12 13:57:37 +0200389}
390
Liu Ping Fancb365642013-09-25 14:20:58 +0800391/* enable cpu_get_ticks()
Cao jin3224e872016-07-08 18:31:37 +0800392 * Caller must hold BQL which serves as mutex for vm_clock_seqlock.
Liu Ping Fancb365642013-09-25 14:20:58 +0800393 */
Paolo Bonzini946fb272011-09-12 13:57:37 +0200394void cpu_enable_ticks(void)
395{
Paolo Bonzini94377112018-08-14 09:57:16 +0200396 seqlock_write_lock(&timers_state.vm_clock_seqlock,
397 &timers_state.vm_clock_lock);
Paolo Bonzini946fb272011-09-12 13:57:37 +0200398 if (!timers_state.cpu_ticks_enabled) {
Christopher Covington4a7428c2015-09-25 10:42:21 -0400399 timers_state.cpu_ticks_offset -= cpu_get_host_ticks();
Paolo Bonzini946fb272011-09-12 13:57:37 +0200400 timers_state.cpu_clock_offset -= get_clock();
401 timers_state.cpu_ticks_enabled = 1;
402 }
Paolo Bonzini94377112018-08-14 09:57:16 +0200403 seqlock_write_unlock(&timers_state.vm_clock_seqlock,
404 &timers_state.vm_clock_lock);
Paolo Bonzini946fb272011-09-12 13:57:37 +0200405}
406
407/* disable cpu_get_ticks() : the clock is stopped. You must not call
Liu Ping Fancb365642013-09-25 14:20:58 +0800408 * cpu_get_ticks() after that.
Cao jin3224e872016-07-08 18:31:37 +0800409 * Caller must hold BQL which serves as mutex for vm_clock_seqlock.
Liu Ping Fancb365642013-09-25 14:20:58 +0800410 */
Paolo Bonzini946fb272011-09-12 13:57:37 +0200411void cpu_disable_ticks(void)
412{
Paolo Bonzini94377112018-08-14 09:57:16 +0200413 seqlock_write_lock(&timers_state.vm_clock_seqlock,
414 &timers_state.vm_clock_lock);
Paolo Bonzini946fb272011-09-12 13:57:37 +0200415 if (timers_state.cpu_ticks_enabled) {
Christopher Covington4a7428c2015-09-25 10:42:21 -0400416 timers_state.cpu_ticks_offset += cpu_get_host_ticks();
Liu Ping Fancb365642013-09-25 14:20:58 +0800417 timers_state.cpu_clock_offset = cpu_get_clock_locked();
Paolo Bonzini946fb272011-09-12 13:57:37 +0200418 timers_state.cpu_ticks_enabled = 0;
419 }
Paolo Bonzini94377112018-08-14 09:57:16 +0200420 seqlock_write_unlock(&timers_state.vm_clock_seqlock,
421 &timers_state.vm_clock_lock);
Paolo Bonzini946fb272011-09-12 13:57:37 +0200422}
423
424/* Correlation between real and virtual time is always going to be
425 fairly approximate, so ignore small variation.
426 When the guest is idle real and virtual time will be aligned in
427 the IO wait loop. */
Rutuja Shah73bcb242016-03-21 21:32:30 +0530428#define ICOUNT_WOBBLE (NANOSECONDS_PER_SECOND / 10)
Paolo Bonzini946fb272011-09-12 13:57:37 +0200429
430static void icount_adjust(void)
431{
432 int64_t cur_time;
433 int64_t cur_icount;
434 int64_t delta;
Paolo Bonzinia3270e12013-10-07 17:18:15 +0200435
436 /* Protected by TimersState mutex. */
Paolo Bonzini946fb272011-09-12 13:57:37 +0200437 static int64_t last_delta;
Paolo Bonzini468cc7c2013-10-07 17:21:51 +0200438
Paolo Bonzini946fb272011-09-12 13:57:37 +0200439 /* If the VM is not running, then do nothing. */
440 if (!runstate_is_running()) {
441 return;
442 }
Paolo Bonzini468cc7c2013-10-07 17:21:51 +0200443
Paolo Bonzini94377112018-08-14 09:57:16 +0200444 seqlock_write_lock(&timers_state.vm_clock_seqlock,
445 &timers_state.vm_clock_lock);
Paolo Bonzini17a15f12013-10-03 15:17:25 +0200446 cur_time = cpu_get_clock_locked();
447 cur_icount = cpu_get_icount_locked();
Paolo Bonzini468cc7c2013-10-07 17:21:51 +0200448
Paolo Bonzini946fb272011-09-12 13:57:37 +0200449 delta = cur_icount - cur_time;
450 /* FIXME: This is a very crude algorithm, somewhat prone to oscillation. */
451 if (delta > 0
452 && last_delta + ICOUNT_WOBBLE < delta * 2
Paolo Bonzinic1ff0732018-08-14 09:31:58 +0200453 && timers_state.icount_time_shift > 0) {
Paolo Bonzini946fb272011-09-12 13:57:37 +0200454 /* The guest is getting too far ahead. Slow time down. */
Paolo Bonzinic1ff0732018-08-14 09:31:58 +0200455 atomic_set(&timers_state.icount_time_shift,
456 timers_state.icount_time_shift - 1);
Paolo Bonzini946fb272011-09-12 13:57:37 +0200457 }
458 if (delta < 0
459 && last_delta - ICOUNT_WOBBLE > delta * 2
Paolo Bonzinic1ff0732018-08-14 09:31:58 +0200460 && timers_state.icount_time_shift < MAX_ICOUNT_SHIFT) {
Paolo Bonzini946fb272011-09-12 13:57:37 +0200461 /* The guest is getting too far behind. Speed time up. */
Paolo Bonzinic1ff0732018-08-14 09:31:58 +0200462 atomic_set(&timers_state.icount_time_shift,
463 timers_state.icount_time_shift + 1);
Paolo Bonzini946fb272011-09-12 13:57:37 +0200464 }
465 last_delta = delta;
Emilio G. Cotac97595d2018-09-10 19:27:50 -0400466 atomic_set_i64(&timers_state.qemu_icount_bias,
467 cur_icount - (timers_state.qemu_icount
468 << timers_state.icount_time_shift));
Paolo Bonzini94377112018-08-14 09:57:16 +0200469 seqlock_write_unlock(&timers_state.vm_clock_seqlock,
470 &timers_state.vm_clock_lock);
Paolo Bonzini946fb272011-09-12 13:57:37 +0200471}
472
473static void icount_adjust_rt(void *opaque)
474{
Pavel Dovgalyukb39e3f32018-01-11 11:26:10 +0300475 timer_mod(timers_state.icount_rt_timer,
Pavel Dovgalyuk1979b902015-01-12 15:00:43 +0300476 qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL_RT) + 1000);
Paolo Bonzini946fb272011-09-12 13:57:37 +0200477 icount_adjust();
478}
479
480static void icount_adjust_vm(void *opaque)
481{
Pavel Dovgalyukb39e3f32018-01-11 11:26:10 +0300482 timer_mod(timers_state.icount_vm_timer,
Alex Bligh40daca52013-08-21 16:03:02 +0100483 qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) +
Rutuja Shah73bcb242016-03-21 21:32:30 +0530484 NANOSECONDS_PER_SECOND / 10);
Paolo Bonzini946fb272011-09-12 13:57:37 +0200485 icount_adjust();
486}
487
488static int64_t qemu_icount_round(int64_t count)
489{
Paolo Bonzinic1ff0732018-08-14 09:31:58 +0200490 int shift = atomic_read(&timers_state.icount_time_shift);
491 return (count + (1 << shift) - 1) >> shift;
Paolo Bonzini946fb272011-09-12 13:57:37 +0200492}
493
Pavel Dovgalyukefab87c2015-09-17 19:24:39 +0300494static void icount_warp_rt(void)
Paolo Bonzini946fb272011-09-12 13:57:37 +0200495{
Alex Bennéeccffff42016-04-04 15:35:48 +0100496 unsigned seq;
497 int64_t warp_start;
498
Paolo Bonzini17a15f12013-10-03 15:17:25 +0200499 /* The icount_warp_timer is rescheduled soon after vm_clock_warp_start
500 * changes from -1 to another value, so the race here is okay.
501 */
Alex Bennéeccffff42016-04-04 15:35:48 +0100502 do {
503 seq = seqlock_read_begin(&timers_state.vm_clock_seqlock);
Pavel Dovgalyukb39e3f32018-01-11 11:26:10 +0300504 warp_start = timers_state.vm_clock_warp_start;
Alex Bennéeccffff42016-04-04 15:35:48 +0100505 } while (seqlock_read_retry(&timers_state.vm_clock_seqlock, seq));
506
507 if (warp_start == -1) {
Paolo Bonzini946fb272011-09-12 13:57:37 +0200508 return;
509 }
510
Paolo Bonzini94377112018-08-14 09:57:16 +0200511 seqlock_write_lock(&timers_state.vm_clock_seqlock,
512 &timers_state.vm_clock_lock);
Paolo Bonzini946fb272011-09-12 13:57:37 +0200513 if (runstate_is_running()) {
Paolo Bonzini74c0b812018-10-08 13:24:14 +0200514 int64_t clock = REPLAY_CLOCK_LOCKED(REPLAY_CLOCK_VIRTUAL_RT,
515 cpu_get_clock_locked());
Paolo Bonzini8ed961d2013-10-07 17:26:07 +0200516 int64_t warp_delta;
517
Pavel Dovgalyukb39e3f32018-01-11 11:26:10 +0300518 warp_delta = clock - timers_state.vm_clock_warp_start;
Paolo Bonzini8ed961d2013-10-07 17:26:07 +0200519 if (use_icount == 2) {
Paolo Bonzini946fb272011-09-12 13:57:37 +0200520 /*
Alex Bligh40daca52013-08-21 16:03:02 +0100521 * In adaptive mode, do not let QEMU_CLOCK_VIRTUAL run too
Paolo Bonzini946fb272011-09-12 13:57:37 +0200522 * far ahead of real time.
523 */
Paolo Bonzini17a15f12013-10-03 15:17:25 +0200524 int64_t cur_icount = cpu_get_icount_locked();
Pavel Dovgalyukbf2a7dd2014-11-26 13:40:55 +0300525 int64_t delta = clock - cur_icount;
Paolo Bonzini8ed961d2013-10-07 17:26:07 +0200526 warp_delta = MIN(warp_delta, delta);
Paolo Bonzini946fb272011-09-12 13:57:37 +0200527 }
Emilio G. Cotac97595d2018-09-10 19:27:50 -0400528 atomic_set_i64(&timers_state.qemu_icount_bias,
529 timers_state.qemu_icount_bias + warp_delta);
Paolo Bonzini946fb272011-09-12 13:57:37 +0200530 }
Pavel Dovgalyukb39e3f32018-01-11 11:26:10 +0300531 timers_state.vm_clock_warp_start = -1;
Paolo Bonzini94377112018-08-14 09:57:16 +0200532 seqlock_write_unlock(&timers_state.vm_clock_seqlock,
533 &timers_state.vm_clock_lock);
Paolo Bonzini8ed961d2013-10-07 17:26:07 +0200534
535 if (qemu_clock_expired(QEMU_CLOCK_VIRTUAL)) {
536 qemu_clock_notify(QEMU_CLOCK_VIRTUAL);
537 }
Paolo Bonzini946fb272011-09-12 13:57:37 +0200538}
539
Pavel Dovgalyuke76d1792016-03-10 14:56:09 +0300540static void icount_timer_cb(void *opaque)
Pavel Dovgalyukefab87c2015-09-17 19:24:39 +0300541{
Pavel Dovgalyuke76d1792016-03-10 14:56:09 +0300542 /* No need for a checkpoint because the timer already synchronizes
543 * with CHECKPOINT_CLOCK_VIRTUAL_RT.
544 */
545 icount_warp_rt();
Pavel Dovgalyukefab87c2015-09-17 19:24:39 +0300546}
547
Paolo Bonzini8156be52012-03-28 15:42:04 +0200548void qtest_clock_warp(int64_t dest)
549{
Alex Bligh40daca52013-08-21 16:03:02 +0100550 int64_t clock = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
Fam Zhengefef88b2015-01-19 17:51:43 +0800551 AioContext *aio_context;
Paolo Bonzini8156be52012-03-28 15:42:04 +0200552 assert(qtest_enabled());
Fam Zhengefef88b2015-01-19 17:51:43 +0800553 aio_context = qemu_get_aio_context();
Paolo Bonzini8156be52012-03-28 15:42:04 +0200554 while (clock < dest) {
Alex Bligh40daca52013-08-21 16:03:02 +0100555 int64_t deadline = qemu_clock_deadline_ns_all(QEMU_CLOCK_VIRTUAL);
Sergey Fedorovc9299e22014-06-10 13:10:28 +0400556 int64_t warp = qemu_soonest_timeout(dest - clock, deadline);
Fam Zhengefef88b2015-01-19 17:51:43 +0800557
Paolo Bonzini94377112018-08-14 09:57:16 +0200558 seqlock_write_lock(&timers_state.vm_clock_seqlock,
559 &timers_state.vm_clock_lock);
Emilio G. Cotac97595d2018-09-10 19:27:50 -0400560 atomic_set_i64(&timers_state.qemu_icount_bias,
561 timers_state.qemu_icount_bias + warp);
Paolo Bonzini94377112018-08-14 09:57:16 +0200562 seqlock_write_unlock(&timers_state.vm_clock_seqlock,
563 &timers_state.vm_clock_lock);
Paolo Bonzini17a15f12013-10-03 15:17:25 +0200564
Alex Bligh40daca52013-08-21 16:03:02 +0100565 qemu_clock_run_timers(QEMU_CLOCK_VIRTUAL);
Fam Zhengefef88b2015-01-19 17:51:43 +0800566 timerlist_run_timers(aio_context->tlg.tl[QEMU_CLOCK_VIRTUAL]);
Alex Bligh40daca52013-08-21 16:03:02 +0100567 clock = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
Paolo Bonzini8156be52012-03-28 15:42:04 +0200568 }
Alex Bligh40daca52013-08-21 16:03:02 +0100569 qemu_clock_notify(QEMU_CLOCK_VIRTUAL);
Paolo Bonzini8156be52012-03-28 15:42:04 +0200570}
571
Pavel Dovgalyuke76d1792016-03-10 14:56:09 +0300572void qemu_start_warp_timer(void)
Paolo Bonzini946fb272011-09-12 13:57:37 +0200573{
Paolo Bonzinice78d182013-10-07 17:30:02 +0200574 int64_t clock;
Paolo Bonzini946fb272011-09-12 13:57:37 +0200575 int64_t deadline;
576
Pavel Dovgalyuke76d1792016-03-10 14:56:09 +0300577 if (!use_icount) {
Paolo Bonzini946fb272011-09-12 13:57:37 +0200578 return;
579 }
580
Pavel Dovgalyuk8bd7f712015-09-17 19:24:44 +0300581 /* Nothing to do if the VM is stopped: QEMU_CLOCK_VIRTUAL timers
582 * do not fire, so computing the deadline does not make sense.
583 */
584 if (!runstate_is_running()) {
585 return;
586 }
587
Pavel Dovgalyuk0c081852018-09-12 11:19:45 +0300588 if (replay_mode != REPLAY_MODE_PLAY) {
589 if (!all_cpu_threads_idle()) {
590 return;
591 }
Pavel Dovgalyuk8bd7f712015-09-17 19:24:44 +0300592
Pavel Dovgalyuk0c081852018-09-12 11:19:45 +0300593 if (qtest_enabled()) {
594 /* When testing, qtest commands advance icount. */
595 return;
596 }
Paolo Bonzini946fb272011-09-12 13:57:37 +0200597
Pavel Dovgalyuk0c081852018-09-12 11:19:45 +0300598 replay_checkpoint(CHECKPOINT_CLOCK_WARP_START);
599 } else {
600 /* warp clock deterministically in record/replay mode */
601 if (!replay_checkpoint(CHECKPOINT_CLOCK_WARP_START)) {
602 /* vCPU is sleeping and warp can't be started.
603 It is probably a race condition: notification sent
604 to vCPU was processed in advance and vCPU went to sleep.
605 Therefore we have to wake it up for doing someting. */
606 if (replay_has_checkpoint()) {
607 qemu_clock_notify(QEMU_CLOCK_VIRTUAL);
608 }
609 return;
610 }
Paolo Bonzini8156be52012-03-28 15:42:04 +0200611 }
612
Alex Blighac70aaf2013-08-21 16:02:57 +0100613 /* We want to use the earliest deadline from ALL vm_clocks */
Pavel Dovgalyukbf2a7dd2014-11-26 13:40:55 +0300614 clock = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL_RT);
Alex Bligh40daca52013-08-21 16:03:02 +0100615 deadline = qemu_clock_deadline_ns_all(QEMU_CLOCK_VIRTUAL);
Paolo Bonzinice78d182013-10-07 17:30:02 +0200616 if (deadline < 0) {
Victor CLEMENTd7a0f712015-05-29 17:14:06 +0200617 static bool notified;
618 if (!icount_sleep && !notified) {
Alistair Francis3dc6f862017-07-12 06:57:41 -0700619 warn_report("icount sleep disabled and no active timers");
Victor CLEMENTd7a0f712015-05-29 17:14:06 +0200620 notified = true;
621 }
Paolo Bonzinice78d182013-10-07 17:30:02 +0200622 return;
Alex Blighac70aaf2013-08-21 16:02:57 +0100623 }
624
Paolo Bonzini946fb272011-09-12 13:57:37 +0200625 if (deadline > 0) {
626 /*
Alex Bligh40daca52013-08-21 16:03:02 +0100627 * Ensure QEMU_CLOCK_VIRTUAL proceeds even when the virtual CPU goes to
Paolo Bonzini946fb272011-09-12 13:57:37 +0200628 * sleep. Otherwise, the CPU might be waiting for a future timer
629 * interrupt to wake it up, but the interrupt never comes because
630 * the vCPU isn't running any insns and thus doesn't advance the
Alex Bligh40daca52013-08-21 16:03:02 +0100631 * QEMU_CLOCK_VIRTUAL.
Paolo Bonzini946fb272011-09-12 13:57:37 +0200632 */
Victor CLEMENT5045e9d92015-05-29 17:14:04 +0200633 if (!icount_sleep) {
634 /*
635 * We never let VCPUs sleep in no sleep icount mode.
636 * If there is a pending QEMU_CLOCK_VIRTUAL timer we just advance
637 * to the next QEMU_CLOCK_VIRTUAL event and notify it.
638 * It is useful when we want a deterministic execution time,
639 * isolated from host latencies.
640 */
Paolo Bonzini94377112018-08-14 09:57:16 +0200641 seqlock_write_lock(&timers_state.vm_clock_seqlock,
642 &timers_state.vm_clock_lock);
Emilio G. Cotac97595d2018-09-10 19:27:50 -0400643 atomic_set_i64(&timers_state.qemu_icount_bias,
644 timers_state.qemu_icount_bias + deadline);
Paolo Bonzini94377112018-08-14 09:57:16 +0200645 seqlock_write_unlock(&timers_state.vm_clock_seqlock,
646 &timers_state.vm_clock_lock);
Victor CLEMENT5045e9d92015-05-29 17:14:04 +0200647 qemu_clock_notify(QEMU_CLOCK_VIRTUAL);
648 } else {
649 /*
650 * We do stop VCPUs and only advance QEMU_CLOCK_VIRTUAL after some
651 * "real" time, (related to the time left until the next event) has
652 * passed. The QEMU_CLOCK_VIRTUAL_RT clock will do this.
653 * This avoids that the warps are visible externally; for example,
654 * you will not be sending network packets continuously instead of
655 * every 100ms.
656 */
Paolo Bonzini94377112018-08-14 09:57:16 +0200657 seqlock_write_lock(&timers_state.vm_clock_seqlock,
658 &timers_state.vm_clock_lock);
Pavel Dovgalyukb39e3f32018-01-11 11:26:10 +0300659 if (timers_state.vm_clock_warp_start == -1
660 || timers_state.vm_clock_warp_start > clock) {
661 timers_state.vm_clock_warp_start = clock;
Victor CLEMENT5045e9d92015-05-29 17:14:04 +0200662 }
Paolo Bonzini94377112018-08-14 09:57:16 +0200663 seqlock_write_unlock(&timers_state.vm_clock_seqlock,
664 &timers_state.vm_clock_lock);
Pavel Dovgalyukb39e3f32018-01-11 11:26:10 +0300665 timer_mod_anticipate(timers_state.icount_warp_timer,
666 clock + deadline);
Paolo Bonzinice78d182013-10-07 17:30:02 +0200667 }
Alex Blighac70aaf2013-08-21 16:02:57 +0100668 } else if (deadline == 0) {
Alex Bligh40daca52013-08-21 16:03:02 +0100669 qemu_clock_notify(QEMU_CLOCK_VIRTUAL);
Paolo Bonzini946fb272011-09-12 13:57:37 +0200670 }
671}
672
Pavel Dovgalyuke76d1792016-03-10 14:56:09 +0300673static void qemu_account_warp_timer(void)
674{
675 if (!use_icount || !icount_sleep) {
676 return;
677 }
678
679 /* Nothing to do if the VM is stopped: QEMU_CLOCK_VIRTUAL timers
680 * do not fire, so computing the deadline does not make sense.
681 */
682 if (!runstate_is_running()) {
683 return;
684 }
685
686 /* warp clock deterministically in record/replay mode */
687 if (!replay_checkpoint(CHECKPOINT_CLOCK_WARP_ACCOUNT)) {
688 return;
689 }
690
Pavel Dovgalyukb39e3f32018-01-11 11:26:10 +0300691 timer_del(timers_state.icount_warp_timer);
Pavel Dovgalyuke76d1792016-03-10 14:56:09 +0300692 icount_warp_rt();
693}
694
KONRAD Fredericd09eae32014-08-01 01:37:10 +0200695static bool icount_state_needed(void *opaque)
696{
697 return use_icount;
698}
699
Pavel Dovgalyukb39e3f32018-01-11 11:26:10 +0300700static bool warp_timer_state_needed(void *opaque)
701{
702 TimersState *s = opaque;
703 return s->icount_warp_timer != NULL;
704}
705
706static bool adjust_timers_state_needed(void *opaque)
707{
708 TimersState *s = opaque;
709 return s->icount_rt_timer != NULL;
710}
711
712/*
713 * Subsection for warp timer migration is optional, because may not be created
714 */
715static const VMStateDescription icount_vmstate_warp_timer = {
716 .name = "timer/icount/warp_timer",
717 .version_id = 1,
718 .minimum_version_id = 1,
719 .needed = warp_timer_state_needed,
720 .fields = (VMStateField[]) {
721 VMSTATE_INT64(vm_clock_warp_start, TimersState),
722 VMSTATE_TIMER_PTR(icount_warp_timer, TimersState),
723 VMSTATE_END_OF_LIST()
724 }
725};
726
727static const VMStateDescription icount_vmstate_adjust_timers = {
728 .name = "timer/icount/timers",
729 .version_id = 1,
730 .minimum_version_id = 1,
731 .needed = adjust_timers_state_needed,
732 .fields = (VMStateField[]) {
733 VMSTATE_TIMER_PTR(icount_rt_timer, TimersState),
734 VMSTATE_TIMER_PTR(icount_vm_timer, TimersState),
735 VMSTATE_END_OF_LIST()
736 }
737};
738
KONRAD Fredericd09eae32014-08-01 01:37:10 +0200739/*
740 * This is a subsection for icount migration.
741 */
742static const VMStateDescription icount_vmstate_timers = {
743 .name = "timer/icount",
744 .version_id = 1,
745 .minimum_version_id = 1,
Juan Quintela5cd8cad2014-09-23 14:09:54 +0200746 .needed = icount_state_needed,
KONRAD Fredericd09eae32014-08-01 01:37:10 +0200747 .fields = (VMStateField[]) {
748 VMSTATE_INT64(qemu_icount_bias, TimersState),
749 VMSTATE_INT64(qemu_icount, TimersState),
750 VMSTATE_END_OF_LIST()
Pavel Dovgalyukb39e3f32018-01-11 11:26:10 +0300751 },
752 .subsections = (const VMStateDescription*[]) {
753 &icount_vmstate_warp_timer,
754 &icount_vmstate_adjust_timers,
755 NULL
KONRAD Fredericd09eae32014-08-01 01:37:10 +0200756 }
757};
758
Paolo Bonzini946fb272011-09-12 13:57:37 +0200759static const VMStateDescription vmstate_timers = {
760 .name = "timer",
761 .version_id = 2,
762 .minimum_version_id = 1,
Juan Quintela35d08452014-04-16 16:01:33 +0200763 .fields = (VMStateField[]) {
Paolo Bonzini946fb272011-09-12 13:57:37 +0200764 VMSTATE_INT64(cpu_ticks_offset, TimersState),
Paolo Bonzinic1ff0732018-08-14 09:31:58 +0200765 VMSTATE_UNUSED(8),
Paolo Bonzini946fb272011-09-12 13:57:37 +0200766 VMSTATE_INT64_V(cpu_clock_offset, TimersState, 2),
767 VMSTATE_END_OF_LIST()
KONRAD Fredericd09eae32014-08-01 01:37:10 +0200768 },
Juan Quintela5cd8cad2014-09-23 14:09:54 +0200769 .subsections = (const VMStateDescription*[]) {
770 &icount_vmstate_timers,
771 NULL
Paolo Bonzini946fb272011-09-12 13:57:37 +0200772 }
773};
774
Paolo Bonzini14e6fe12016-10-31 10:36:08 +0100775static void cpu_throttle_thread(CPUState *cpu, run_on_cpu_data opaque)
Jason J. Herne2adcc852015-09-08 13:12:33 -0400776{
Jason J. Herne2adcc852015-09-08 13:12:33 -0400777 double pct;
778 double throttle_ratio;
779 long sleeptime_ns;
780
781 if (!cpu_throttle_get_percentage()) {
782 return;
783 }
784
785 pct = (double)cpu_throttle_get_percentage()/100;
786 throttle_ratio = pct / (1 - pct);
787 sleeptime_ns = (long)(throttle_ratio * CPU_THROTTLE_TIMESLICE_NS);
788
789 qemu_mutex_unlock_iothread();
Jason J. Herne2adcc852015-09-08 13:12:33 -0400790 g_usleep(sleeptime_ns / 1000); /* Convert ns to us for usleep call */
791 qemu_mutex_lock_iothread();
Felipe Franciosi90bb0c02017-05-19 22:29:50 +0100792 atomic_set(&cpu->throttle_thread_scheduled, 0);
Jason J. Herne2adcc852015-09-08 13:12:33 -0400793}
794
795static void cpu_throttle_timer_tick(void *opaque)
796{
797 CPUState *cpu;
798 double pct;
799
800 /* Stop the timer if needed */
801 if (!cpu_throttle_get_percentage()) {
802 return;
803 }
804 CPU_FOREACH(cpu) {
805 if (!atomic_xchg(&cpu->throttle_thread_scheduled, 1)) {
Paolo Bonzini14e6fe12016-10-31 10:36:08 +0100806 async_run_on_cpu(cpu, cpu_throttle_thread,
807 RUN_ON_CPU_NULL);
Jason J. Herne2adcc852015-09-08 13:12:33 -0400808 }
809 }
810
811 pct = (double)cpu_throttle_get_percentage()/100;
812 timer_mod(throttle_timer, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL_RT) +
813 CPU_THROTTLE_TIMESLICE_NS / (1-pct));
814}
815
816void cpu_throttle_set(int new_throttle_pct)
817{
818 /* Ensure throttle percentage is within valid range */
819 new_throttle_pct = MIN(new_throttle_pct, CPU_THROTTLE_PCT_MAX);
820 new_throttle_pct = MAX(new_throttle_pct, CPU_THROTTLE_PCT_MIN);
821
822 atomic_set(&throttle_percentage, new_throttle_pct);
823
824 timer_mod(throttle_timer, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL_RT) +
825 CPU_THROTTLE_TIMESLICE_NS);
826}
827
828void cpu_throttle_stop(void)
829{
830 atomic_set(&throttle_percentage, 0);
831}
832
833bool cpu_throttle_active(void)
834{
835 return (cpu_throttle_get_percentage() != 0);
836}
837
838int cpu_throttle_get_percentage(void)
839{
840 return atomic_read(&throttle_percentage);
841}
842
Pavel Dovgalyuk4603ea02014-09-01 09:34:49 +0400843void cpu_ticks_init(void)
844{
Emilio G. Cotaccdb3c12016-06-08 14:55:20 -0400845 seqlock_init(&timers_state.vm_clock_seqlock);
Emilio G. Cota87a09cd2018-09-03 13:18:29 -0400846 qemu_spin_init(&timers_state.vm_clock_lock);
Pavel Dovgalyuk4603ea02014-09-01 09:34:49 +0400847 vmstate_register(NULL, 0, &vmstate_timers, &timers_state);
Jason J. Herne2adcc852015-09-08 13:12:33 -0400848 throttle_timer = timer_new_ns(QEMU_CLOCK_VIRTUAL_RT,
849 cpu_throttle_timer_tick, NULL);
Pavel Dovgalyuk4603ea02014-09-01 09:34:49 +0400850}
851
Sebastian Tanase1ad95802014-07-25 11:56:28 +0200852void configure_icount(QemuOpts *opts, Error **errp)
Paolo Bonzini946fb272011-09-12 13:57:37 +0200853{
Sebastian Tanase1ad95802014-07-25 11:56:28 +0200854 const char *option;
Sebastian Tanasea8bfac32014-07-25 11:56:29 +0200855 char *rem_str = NULL;
Sebastian Tanase1ad95802014-07-25 11:56:28 +0200856
Sebastian Tanase1ad95802014-07-25 11:56:28 +0200857 option = qemu_opt_get(opts, "shift");
Paolo Bonzini946fb272011-09-12 13:57:37 +0200858 if (!option) {
Sebastian Tanasea8bfac32014-07-25 11:56:29 +0200859 if (qemu_opt_get(opts, "align") != NULL) {
860 error_setg(errp, "Please specify shift option when using align");
861 }
Paolo Bonzini946fb272011-09-12 13:57:37 +0200862 return;
863 }
Victor CLEMENTf1f4b572015-05-29 17:14:05 +0200864
865 icount_sleep = qemu_opt_get_bool(opts, "sleep", true);
Victor CLEMENT5045e9d92015-05-29 17:14:04 +0200866 if (icount_sleep) {
Pavel Dovgalyukb39e3f32018-01-11 11:26:10 +0300867 timers_state.icount_warp_timer = timer_new_ns(QEMU_CLOCK_VIRTUAL_RT,
Pavel Dovgalyuke76d1792016-03-10 14:56:09 +0300868 icount_timer_cb, NULL);
Victor CLEMENT5045e9d92015-05-29 17:14:04 +0200869 }
Victor CLEMENTf1f4b572015-05-29 17:14:05 +0200870
Sebastian Tanasea8bfac32014-07-25 11:56:29 +0200871 icount_align_option = qemu_opt_get_bool(opts, "align", false);
Victor CLEMENTf1f4b572015-05-29 17:14:05 +0200872
873 if (icount_align_option && !icount_sleep) {
Pranith Kumar778d9f92016-02-26 10:16:51 -0500874 error_setg(errp, "align=on and sleep=off are incompatible");
Victor CLEMENTf1f4b572015-05-29 17:14:05 +0200875 }
Paolo Bonzini946fb272011-09-12 13:57:37 +0200876 if (strcmp(option, "auto") != 0) {
Sebastian Tanasea8bfac32014-07-25 11:56:29 +0200877 errno = 0;
Paolo Bonzinic1ff0732018-08-14 09:31:58 +0200878 timers_state.icount_time_shift = strtol(option, &rem_str, 0);
Sebastian Tanasea8bfac32014-07-25 11:56:29 +0200879 if (errno != 0 || *rem_str != '\0' || !strlen(option)) {
880 error_setg(errp, "icount: Invalid shift value");
881 }
Paolo Bonzini946fb272011-09-12 13:57:37 +0200882 use_icount = 1;
883 return;
Sebastian Tanasea8bfac32014-07-25 11:56:29 +0200884 } else if (icount_align_option) {
885 error_setg(errp, "shift=auto and align=on are incompatible");
Victor CLEMENTf1f4b572015-05-29 17:14:05 +0200886 } else if (!icount_sleep) {
Pranith Kumar778d9f92016-02-26 10:16:51 -0500887 error_setg(errp, "shift=auto and sleep=off are incompatible");
Paolo Bonzini946fb272011-09-12 13:57:37 +0200888 }
889
890 use_icount = 2;
891
892 /* 125MIPS seems a reasonable initial guess at the guest speed.
893 It will be corrected fairly quickly anyway. */
Paolo Bonzinic1ff0732018-08-14 09:31:58 +0200894 timers_state.icount_time_shift = 3;
Paolo Bonzini946fb272011-09-12 13:57:37 +0200895
896 /* Have both realtime and virtual time triggers for speed adjustment.
897 The realtime trigger catches emulated time passing too slowly,
898 the virtual time trigger catches emulated time passing too fast.
899 Realtime triggers occur even when idle, so use them less frequently
900 than VM triggers. */
Pavel Dovgalyukb39e3f32018-01-11 11:26:10 +0300901 timers_state.vm_clock_warp_start = -1;
902 timers_state.icount_rt_timer = timer_new_ms(QEMU_CLOCK_VIRTUAL_RT,
Pavel Dovgalyukbf2a7dd2014-11-26 13:40:55 +0300903 icount_adjust_rt, NULL);
Pavel Dovgalyukb39e3f32018-01-11 11:26:10 +0300904 timer_mod(timers_state.icount_rt_timer,
Pavel Dovgalyukbf2a7dd2014-11-26 13:40:55 +0300905 qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL_RT) + 1000);
Pavel Dovgalyukb39e3f32018-01-11 11:26:10 +0300906 timers_state.icount_vm_timer = timer_new_ns(QEMU_CLOCK_VIRTUAL,
Alex Bligh40daca52013-08-21 16:03:02 +0100907 icount_adjust_vm, NULL);
Pavel Dovgalyukb39e3f32018-01-11 11:26:10 +0300908 timer_mod(timers_state.icount_vm_timer,
Alex Bligh40daca52013-08-21 16:03:02 +0100909 qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) +
Rutuja Shah73bcb242016-03-21 21:32:30 +0530910 NANOSECONDS_PER_SECOND / 10);
Paolo Bonzini946fb272011-09-12 13:57:37 +0200911}
912
913/***********************************************************/
Alex Bennée65467062017-02-23 18:29:09 +0000914/* TCG vCPU kick timer
915 *
916 * The kick timer is responsible for moving single threaded vCPU
917 * emulation on to the next vCPU. If more than one vCPU is running a
918 * timer event with force a cpu->exit so the next vCPU can get
919 * scheduled.
920 *
921 * The timer is removed if all vCPUs are idle and restarted again once
922 * idleness is complete.
923 */
924
925static QEMUTimer *tcg_kick_vcpu_timer;
Alex Bennée791158d2017-02-23 18:29:10 +0000926static CPUState *tcg_current_rr_cpu;
Alex Bennée65467062017-02-23 18:29:09 +0000927
928#define TCG_KICK_PERIOD (NANOSECONDS_PER_SECOND / 10)
929
930static inline int64_t qemu_tcg_next_kick(void)
931{
932 return qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) + TCG_KICK_PERIOD;
933}
934
Alex Bennée791158d2017-02-23 18:29:10 +0000935/* Kick the currently round-robin scheduled vCPU */
936static void qemu_cpu_kick_rr_cpu(void)
937{
938 CPUState *cpu;
Alex Bennée791158d2017-02-23 18:29:10 +0000939 do {
940 cpu = atomic_mb_read(&tcg_current_rr_cpu);
941 if (cpu) {
942 cpu_exit(cpu);
943 }
944 } while (cpu != atomic_mb_read(&tcg_current_rr_cpu));
945}
946
Paolo Bonzini6b8f0182017-03-02 19:56:40 +0100947static void do_nothing(CPUState *cpu, run_on_cpu_data unused)
948{
949}
950
Paolo Bonzini3f53bc62017-03-03 11:50:29 +0100951void qemu_timer_notify_cb(void *opaque, QEMUClockType type)
952{
Paolo Bonzini6b8f0182017-03-02 19:56:40 +0100953 if (!use_icount || type != QEMU_CLOCK_VIRTUAL) {
954 qemu_notify_event();
955 return;
956 }
957
Peter Maydellc52e7132018-04-10 13:02:25 +0100958 if (qemu_in_vcpu_thread()) {
959 /* A CPU is currently running; kick it back out to the
960 * tcg_cpu_exec() loop so it will recalculate its
961 * icount deadline immediately.
962 */
963 qemu_cpu_kick(current_cpu);
964 } else if (first_cpu) {
Paolo Bonzini6b8f0182017-03-02 19:56:40 +0100965 /* qemu_cpu_kick is not enough to kick a halted CPU out of
966 * qemu_tcg_wait_io_event. async_run_on_cpu, instead,
967 * causes cpu_thread_is_idle to return false. This way,
968 * handle_icount_deadline can run.
Peter Maydellc52e7132018-04-10 13:02:25 +0100969 * If we have no CPUs at all for some reason, we don't
970 * need to do anything.
Paolo Bonzini6b8f0182017-03-02 19:56:40 +0100971 */
972 async_run_on_cpu(first_cpu, do_nothing, RUN_ON_CPU_NULL);
973 }
Paolo Bonzini3f53bc62017-03-03 11:50:29 +0100974}
975
Alex Bennée65467062017-02-23 18:29:09 +0000976static void kick_tcg_thread(void *opaque)
977{
978 timer_mod(tcg_kick_vcpu_timer, qemu_tcg_next_kick());
Alex Bennée791158d2017-02-23 18:29:10 +0000979 qemu_cpu_kick_rr_cpu();
Alex Bennée65467062017-02-23 18:29:09 +0000980}
981
982static void start_tcg_kick_timer(void)
983{
Paolo Bonzinidb08b682018-01-11 13:53:12 +0100984 assert(!mttcg_enabled);
985 if (!tcg_kick_vcpu_timer && CPU_NEXT(first_cpu)) {
Alex Bennée65467062017-02-23 18:29:09 +0000986 tcg_kick_vcpu_timer = timer_new_ns(QEMU_CLOCK_VIRTUAL,
987 kick_tcg_thread, NULL);
Alex Bennée1926ab22018-09-27 18:17:24 +0100988 }
989 if (tcg_kick_vcpu_timer && !timer_pending(tcg_kick_vcpu_timer)) {
Alex Bennée65467062017-02-23 18:29:09 +0000990 timer_mod(tcg_kick_vcpu_timer, qemu_tcg_next_kick());
991 }
992}
993
994static void stop_tcg_kick_timer(void)
995{
Paolo Bonzinidb08b682018-01-11 13:53:12 +0100996 assert(!mttcg_enabled);
Alex Bennée1926ab22018-09-27 18:17:24 +0100997 if (tcg_kick_vcpu_timer && timer_pending(tcg_kick_vcpu_timer)) {
Alex Bennée65467062017-02-23 18:29:09 +0000998 timer_del(tcg_kick_vcpu_timer);
Alex Bennée65467062017-02-23 18:29:09 +0000999 }
1000}
1001
Alex Bennée65467062017-02-23 18:29:09 +00001002/***********************************************************/
Blue Swirl296af7c2010-03-29 19:23:50 +00001003void hw_error(const char *fmt, ...)
1004{
1005 va_list ap;
Andreas Färber55e5c282012-12-17 06:18:02 +01001006 CPUState *cpu;
Blue Swirl296af7c2010-03-29 19:23:50 +00001007
1008 va_start(ap, fmt);
1009 fprintf(stderr, "qemu: hardware error: ");
1010 vfprintf(stderr, fmt, ap);
1011 fprintf(stderr, "\n");
Andreas Färberbdc44642013-06-24 23:50:24 +02001012 CPU_FOREACH(cpu) {
Andreas Färber55e5c282012-12-17 06:18:02 +01001013 fprintf(stderr, "CPU #%d:\n", cpu->cpu_index);
Markus Armbruster90c84c52019-04-17 21:18:02 +02001014 cpu_dump_state(cpu, stderr, CPU_DUMP_FPU);
Blue Swirl296af7c2010-03-29 19:23:50 +00001015 }
1016 va_end(ap);
1017 abort();
1018}
1019
1020void cpu_synchronize_all_states(void)
1021{
Andreas Färber182735e2013-05-29 22:29:20 +02001022 CPUState *cpu;
Blue Swirl296af7c2010-03-29 19:23:50 +00001023
Andreas Färberbdc44642013-06-24 23:50:24 +02001024 CPU_FOREACH(cpu) {
Andreas Färber182735e2013-05-29 22:29:20 +02001025 cpu_synchronize_state(cpu);
Sergio Andres Gomez Del Realc97d6d22017-09-13 04:05:09 -05001026 /* TODO: move to cpu_synchronize_state() */
1027 if (hvf_enabled()) {
1028 hvf_cpu_synchronize_state(cpu);
1029 }
Blue Swirl296af7c2010-03-29 19:23:50 +00001030 }
1031}
1032
1033void cpu_synchronize_all_post_reset(void)
1034{
Andreas Färber182735e2013-05-29 22:29:20 +02001035 CPUState *cpu;
Blue Swirl296af7c2010-03-29 19:23:50 +00001036
Andreas Färberbdc44642013-06-24 23:50:24 +02001037 CPU_FOREACH(cpu) {
Andreas Färber182735e2013-05-29 22:29:20 +02001038 cpu_synchronize_post_reset(cpu);
Sergio Andres Gomez Del Realc97d6d22017-09-13 04:05:09 -05001039 /* TODO: move to cpu_synchronize_post_reset() */
1040 if (hvf_enabled()) {
1041 hvf_cpu_synchronize_post_reset(cpu);
1042 }
Blue Swirl296af7c2010-03-29 19:23:50 +00001043 }
1044}
1045
1046void cpu_synchronize_all_post_init(void)
1047{
Andreas Färber182735e2013-05-29 22:29:20 +02001048 CPUState *cpu;
Blue Swirl296af7c2010-03-29 19:23:50 +00001049
Andreas Färberbdc44642013-06-24 23:50:24 +02001050 CPU_FOREACH(cpu) {
Andreas Färber182735e2013-05-29 22:29:20 +02001051 cpu_synchronize_post_init(cpu);
Sergio Andres Gomez Del Realc97d6d22017-09-13 04:05:09 -05001052 /* TODO: move to cpu_synchronize_post_init() */
1053 if (hvf_enabled()) {
1054 hvf_cpu_synchronize_post_init(cpu);
1055 }
Blue Swirl296af7c2010-03-29 19:23:50 +00001056 }
1057}
1058
David Gibson75e972d2017-05-26 14:46:28 +10001059void cpu_synchronize_all_pre_loadvm(void)
1060{
1061 CPUState *cpu;
1062
1063 CPU_FOREACH(cpu) {
1064 cpu_synchronize_pre_loadvm(cpu);
1065 }
1066}
1067
Stefan Hajnoczi4486e892018-03-07 14:42:05 +00001068static int do_vm_stop(RunState state, bool send_stop)
Blue Swirl296af7c2010-03-29 19:23:50 +00001069{
Kevin Wolf56983462013-07-05 13:49:54 +02001070 int ret = 0;
1071
Luiz Capitulino13548692011-07-29 15:36:43 -03001072 if (runstate_is_running()) {
Blue Swirl296af7c2010-03-29 19:23:50 +00001073 cpu_disable_ticks();
Blue Swirl296af7c2010-03-29 19:23:50 +00001074 pause_all_vcpus();
Luiz Capitulinof5bbfba2011-07-29 15:04:45 -03001075 runstate_set(state);
Luiz Capitulino1dfb4dd2011-07-29 14:26:33 -03001076 vm_state_notify(0, state);
Stefan Hajnoczi4486e892018-03-07 14:42:05 +00001077 if (send_stop) {
Peter Xu3ab72382018-08-15 21:37:37 +08001078 qapi_event_send_stop();
Stefan Hajnoczi4486e892018-03-07 14:42:05 +00001079 }
Blue Swirl296af7c2010-03-29 19:23:50 +00001080 }
Kevin Wolf56983462013-07-05 13:49:54 +02001081
Kevin Wolf594a45c2013-07-18 14:52:19 +02001082 bdrv_drain_all();
Pavel Dovgalyuk6d0ceb82016-09-26 11:08:16 +03001083 replay_disable_events();
John Snow22af08e2016-09-22 21:45:51 -04001084 ret = bdrv_flush_all();
Kevin Wolf594a45c2013-07-18 14:52:19 +02001085
Kevin Wolf56983462013-07-05 13:49:54 +02001086 return ret;
Blue Swirl296af7c2010-03-29 19:23:50 +00001087}
1088
Stefan Hajnoczi4486e892018-03-07 14:42:05 +00001089/* Special vm_stop() variant for terminating the process. Historically clients
1090 * did not expect a QMP STOP event and so we need to retain compatibility.
1091 */
1092int vm_shutdown(void)
1093{
1094 return do_vm_stop(RUN_STATE_SHUTDOWN, false);
1095}
1096
Andreas Färbera1fcaa72012-05-02 23:42:26 +02001097static bool cpu_can_run(CPUState *cpu)
Blue Swirl296af7c2010-03-29 19:23:50 +00001098{
Andreas Färber4fdeee72012-05-02 23:10:09 +02001099 if (cpu->stop) {
Andreas Färbera1fcaa72012-05-02 23:42:26 +02001100 return false;
Jan Kiszka0ab07c62011-02-07 12:19:14 +01001101 }
Tiejun Chen321bc0b2013-08-02 09:43:09 +08001102 if (cpu_is_stopped(cpu)) {
Andreas Färbera1fcaa72012-05-02 23:42:26 +02001103 return false;
Jan Kiszka0ab07c62011-02-07 12:19:14 +01001104 }
Andreas Färbera1fcaa72012-05-02 23:42:26 +02001105 return true;
Blue Swirl296af7c2010-03-29 19:23:50 +00001106}
1107
Andreas Färber91325042013-05-27 02:07:49 +02001108static void cpu_handle_guest_debug(CPUState *cpu)
Jan Kiszka3c638d02010-06-25 16:56:56 +02001109{
Andreas Färber64f6b342013-05-27 02:06:09 +02001110 gdb_set_stop_cpu(cpu);
Jan Kiszka8cf71712011-02-07 12:19:16 +01001111 qemu_system_debug_request();
Andreas Färberf324e762012-05-02 23:26:21 +02001112 cpu->stopped = true;
Jan Kiszka3c638d02010-06-25 16:56:56 +02001113}
1114
Jan Kiszka6d9cb732011-02-01 22:15:58 +01001115#ifdef CONFIG_LINUX
1116static void sigbus_reraise(void)
1117{
1118 sigset_t set;
1119 struct sigaction action;
1120
1121 memset(&action, 0, sizeof(action));
1122 action.sa_handler = SIG_DFL;
1123 if (!sigaction(SIGBUS, &action, NULL)) {
1124 raise(SIGBUS);
1125 sigemptyset(&set);
1126 sigaddset(&set, SIGBUS);
Peter Maydella2d17612016-05-16 18:33:59 +01001127 pthread_sigmask(SIG_UNBLOCK, &set, NULL);
Jan Kiszka6d9cb732011-02-01 22:15:58 +01001128 }
1129 perror("Failed to re-raise SIGBUS!\n");
1130 abort();
1131}
1132
Paolo Bonzinid98d4072017-02-08 13:22:12 +01001133static void sigbus_handler(int n, siginfo_t *siginfo, void *ctx)
Jan Kiszka6d9cb732011-02-01 22:15:58 +01001134{
Paolo Bonzinia16fc072017-02-09 09:50:02 +01001135 if (siginfo->si_code != BUS_MCEERR_AO && siginfo->si_code != BUS_MCEERR_AR) {
1136 sigbus_reraise();
1137 }
1138
Paolo Bonzini2ae41db2017-02-08 12:48:54 +01001139 if (current_cpu) {
1140 /* Called asynchronously in VCPU thread. */
1141 if (kvm_on_sigbus_vcpu(current_cpu, siginfo->si_code, siginfo->si_addr)) {
1142 sigbus_reraise();
1143 }
1144 } else {
1145 /* Called synchronously (via signalfd) in main thread. */
1146 if (kvm_on_sigbus(siginfo->si_code, siginfo->si_addr)) {
1147 sigbus_reraise();
1148 }
Jan Kiszka6d9cb732011-02-01 22:15:58 +01001149 }
1150}
1151
1152static void qemu_init_sigbus(void)
1153{
1154 struct sigaction action;
1155
1156 memset(&action, 0, sizeof(action));
1157 action.sa_flags = SA_SIGINFO;
Paolo Bonzinid98d4072017-02-08 13:22:12 +01001158 action.sa_sigaction = sigbus_handler;
Jan Kiszka6d9cb732011-02-01 22:15:58 +01001159 sigaction(SIGBUS, &action, NULL);
1160
1161 prctl(PR_MCE_KILL, PR_MCE_KILL_SET, PR_MCE_KILL_EARLY, 0, 0);
1162}
Paolo Bonzinia16fc072017-02-09 09:50:02 +01001163#else /* !CONFIG_LINUX */
1164static void qemu_init_sigbus(void)
1165{
1166}
Paolo Bonzinia16fc072017-02-09 09:50:02 +01001167#endif /* !CONFIG_LINUX */
Blue Swirl296af7c2010-03-29 19:23:50 +00001168
Stefan Weilb2532d82012-09-27 07:41:42 +02001169static QemuMutex qemu_global_mutex;
Blue Swirl296af7c2010-03-29 19:23:50 +00001170
1171static QemuThread io_thread;
1172
Blue Swirl296af7c2010-03-29 19:23:50 +00001173/* cpu creation */
1174static QemuCond qemu_cpu_cond;
1175/* system init */
Blue Swirl296af7c2010-03-29 19:23:50 +00001176static QemuCond qemu_pause_cond;
1177
Paolo Bonzinid3b12f52011-09-13 10:30:52 +02001178void qemu_init_cpu_loop(void)
Blue Swirl296af7c2010-03-29 19:23:50 +00001179{
Jan Kiszka6d9cb732011-02-01 22:15:58 +01001180 qemu_init_sigbus();
Anthony Liguoried945922011-02-08 18:18:18 +01001181 qemu_cond_init(&qemu_cpu_cond);
Anthony Liguoried945922011-02-08 18:18:18 +01001182 qemu_cond_init(&qemu_pause_cond);
Blue Swirl296af7c2010-03-29 19:23:50 +00001183 qemu_mutex_init(&qemu_global_mutex);
Blue Swirl296af7c2010-03-29 19:23:50 +00001184
Jan Kiszkab7680cb2011-03-12 17:43:51 +01001185 qemu_thread_get_self(&io_thread);
Blue Swirl296af7c2010-03-29 19:23:50 +00001186}
1187
Paolo Bonzini14e6fe12016-10-31 10:36:08 +01001188void run_on_cpu(CPUState *cpu, run_on_cpu_func func, run_on_cpu_data data)
Marcelo Tosattie82bcec2010-05-04 09:45:22 -03001189{
Sergey Fedorovd148d902016-08-29 09:51:00 +02001190 do_run_on_cpu(cpu, func, data, &qemu_global_mutex);
Chegu Vinod3c022702013-06-24 03:49:41 -06001191}
1192
Gu Zheng4c055ab2016-05-12 09:18:13 +05301193static void qemu_kvm_destroy_vcpu(CPUState *cpu)
1194{
1195 if (kvm_destroy_vcpu(cpu) < 0) {
1196 error_report("kvm_destroy_vcpu failed");
1197 exit(EXIT_FAILURE);
1198 }
1199}
1200
1201static void qemu_tcg_destroy_vcpu(CPUState *cpu)
1202{
1203}
1204
David Hildenbrandebd05fe2017-11-29 20:12:15 +01001205static void qemu_cpu_stop(CPUState *cpu, bool exit)
1206{
1207 g_assert(qemu_cpu_is_self(cpu));
1208 cpu->stop = false;
1209 cpu->stopped = true;
1210 if (exit) {
1211 cpu_exit(cpu);
1212 }
1213 qemu_cond_broadcast(&qemu_pause_cond);
1214}
1215
Andreas Färber509a0d72012-05-03 02:18:09 +02001216static void qemu_wait_io_event_common(CPUState *cpu)
Blue Swirl296af7c2010-03-29 19:23:50 +00001217{
Alex Bennée37257942017-02-23 18:29:14 +00001218 atomic_mb_set(&cpu->thread_kicked, false);
Andreas Färber4fdeee72012-05-02 23:10:09 +02001219 if (cpu->stop) {
David Hildenbrandebd05fe2017-11-29 20:12:15 +01001220 qemu_cpu_stop(cpu, false);
Blue Swirl296af7c2010-03-29 19:23:50 +00001221 }
Sergey Fedorova5403c62016-08-02 18:27:36 +01001222 process_queued_cpu_work(cpu);
Alex Bennée37257942017-02-23 18:29:14 +00001223}
1224
Paolo Bonzinia8efa602018-11-14 12:36:57 +01001225static void qemu_tcg_rr_wait_io_event(void)
Alex Bennée37257942017-02-23 18:29:14 +00001226{
Paolo Bonzinia8efa602018-11-14 12:36:57 +01001227 CPUState *cpu;
1228
Paolo Bonzinidb08b682018-01-11 13:53:12 +01001229 while (all_cpu_threads_idle()) {
Alex Bennée65467062017-02-23 18:29:09 +00001230 stop_tcg_kick_timer();
Paolo Bonzinia8efa602018-11-14 12:36:57 +01001231 qemu_cond_wait(first_cpu->halt_cond, &qemu_global_mutex);
Jan Kiszka16400322011-02-09 16:29:37 +01001232 }
Blue Swirl296af7c2010-03-29 19:23:50 +00001233
Alex Bennée65467062017-02-23 18:29:09 +00001234 start_tcg_kick_timer();
1235
Paolo Bonzinia8efa602018-11-14 12:36:57 +01001236 CPU_FOREACH(cpu) {
1237 qemu_wait_io_event_common(cpu);
1238 }
Blue Swirl296af7c2010-03-29 19:23:50 +00001239}
1240
Paolo Bonzinidb08b682018-01-11 13:53:12 +01001241static void qemu_wait_io_event(CPUState *cpu)
Blue Swirl296af7c2010-03-29 19:23:50 +00001242{
Andreas Färbera98ae1d2013-05-26 23:21:08 +02001243 while (cpu_thread_is_idle(cpu)) {
Andreas Färberf5c121b2012-05-03 01:22:49 +02001244 qemu_cond_wait(cpu->halt_cond, &qemu_global_mutex);
Jan Kiszka16400322011-02-09 16:29:37 +01001245 }
Blue Swirl296af7c2010-03-29 19:23:50 +00001246
Paolo Bonzinidb08b682018-01-11 13:53:12 +01001247#ifdef _WIN32
1248 /* Eat dummy APC queued by qemu_cpu_kick_thread. */
1249 if (!tcg_enabled()) {
1250 SleepEx(0, TRUE);
Sergio Andres Gomez Del Realc97d6d22017-09-13 04:05:09 -05001251 }
Paolo Bonzinidb08b682018-01-11 13:53:12 +01001252#endif
Sergio Andres Gomez Del Realc97d6d22017-09-13 04:05:09 -05001253 qemu_wait_io_event_common(cpu);
1254}
1255
Jan Kiszka7e97cd82011-02-07 12:19:12 +01001256static void *qemu_kvm_cpu_thread_fn(void *arg)
Blue Swirl296af7c2010-03-29 19:23:50 +00001257{
Andreas Färber48a106b2013-05-27 02:20:39 +02001258 CPUState *cpu = arg;
Jan Kiszka84b49152011-02-01 22:15:50 +01001259 int r;
Blue Swirl296af7c2010-03-29 19:23:50 +00001260
Paolo Bonziniab28bd22015-07-09 08:55:38 +02001261 rcu_register_thread();
1262
Paolo Bonzini2e7f7a32015-06-18 18:47:18 +02001263 qemu_mutex_lock_iothread();
Andreas Färber814e6122012-05-02 17:00:37 +02001264 qemu_thread_get_self(cpu->thread);
Andreas Färber9f09e182012-05-03 06:59:07 +02001265 cpu->thread_id = qemu_get_thread_id();
Pavel Dovgalyuk626cf8f2014-12-08 10:53:17 +03001266 cpu->can_do_io = 1;
Andreas Färber4917cf42013-05-27 05:17:50 +02001267 current_cpu = cpu;
Blue Swirl296af7c2010-03-29 19:23:50 +00001268
Andreas Färber504134d2012-12-17 06:38:45 +01001269 r = kvm_init_vcpu(cpu);
Jan Kiszka84b49152011-02-01 22:15:50 +01001270 if (r < 0) {
Alistair Francis493d89b2018-02-03 09:43:14 +01001271 error_report("kvm_init_vcpu failed: %s", strerror(-r));
Jan Kiszka84b49152011-02-01 22:15:50 +01001272 exit(1);
1273 }
Blue Swirl296af7c2010-03-29 19:23:50 +00001274
Paolo Bonzini18268b62017-02-09 09:41:14 +01001275 kvm_init_cpu_signals(cpu);
Blue Swirl296af7c2010-03-29 19:23:50 +00001276
1277 /* signal CPU creation */
Andreas Färber61a46212012-05-02 22:49:36 +02001278 cpu->created = true;
Blue Swirl296af7c2010-03-29 19:23:50 +00001279 qemu_cond_signal(&qemu_cpu_cond);
Richard Henderson9c09a252019-03-14 13:06:29 -07001280 qemu_guest_random_seed_thread_part2(cpu->random_seed);
Blue Swirl296af7c2010-03-29 19:23:50 +00001281
Gu Zheng4c055ab2016-05-12 09:18:13 +05301282 do {
Andreas Färbera1fcaa72012-05-02 23:42:26 +02001283 if (cpu_can_run(cpu)) {
Andreas Färber1458c362013-05-26 23:46:55 +02001284 r = kvm_cpu_exec(cpu);
Jan Kiszka83f338f2011-02-07 12:19:17 +01001285 if (r == EXCP_DEBUG) {
Andreas Färber91325042013-05-27 02:07:49 +02001286 cpu_handle_guest_debug(cpu);
Jan Kiszka83f338f2011-02-07 12:19:17 +01001287 }
Jan Kiszka0ab07c62011-02-07 12:19:14 +01001288 }
Paolo Bonzinidb08b682018-01-11 13:53:12 +01001289 qemu_wait_io_event(cpu);
Gu Zheng4c055ab2016-05-12 09:18:13 +05301290 } while (!cpu->unplug || cpu_can_run(cpu));
Blue Swirl296af7c2010-03-29 19:23:50 +00001291
Gu Zheng4c055ab2016-05-12 09:18:13 +05301292 qemu_kvm_destroy_vcpu(cpu);
Bharata B Rao2c579042016-05-12 09:18:14 +05301293 cpu->created = false;
1294 qemu_cond_signal(&qemu_cpu_cond);
Gu Zheng4c055ab2016-05-12 09:18:13 +05301295 qemu_mutex_unlock_iothread();
Paolo Bonzini57615ed2018-01-30 11:04:36 -05001296 rcu_unregister_thread();
Blue Swirl296af7c2010-03-29 19:23:50 +00001297 return NULL;
1298}
1299
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +02001300static void *qemu_dummy_cpu_thread_fn(void *arg)
1301{
1302#ifdef _WIN32
Alistair Francis493d89b2018-02-03 09:43:14 +01001303 error_report("qtest is not supported under Windows");
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +02001304 exit(1);
1305#else
Andreas Färber10a90212013-05-27 02:24:35 +02001306 CPUState *cpu = arg;
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +02001307 sigset_t waitset;
1308 int r;
1309
Paolo Bonziniab28bd22015-07-09 08:55:38 +02001310 rcu_register_thread();
1311
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +02001312 qemu_mutex_lock_iothread();
Andreas Färber814e6122012-05-02 17:00:37 +02001313 qemu_thread_get_self(cpu->thread);
Andreas Färber9f09e182012-05-03 06:59:07 +02001314 cpu->thread_id = qemu_get_thread_id();
Pavel Dovgalyuk626cf8f2014-12-08 10:53:17 +03001315 cpu->can_do_io = 1;
Alex Bennée37257942017-02-23 18:29:14 +00001316 current_cpu = cpu;
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +02001317
1318 sigemptyset(&waitset);
1319 sigaddset(&waitset, SIG_IPI);
1320
1321 /* signal CPU creation */
Andreas Färber61a46212012-05-02 22:49:36 +02001322 cpu->created = true;
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +02001323 qemu_cond_signal(&qemu_cpu_cond);
Richard Henderson9c09a252019-03-14 13:06:29 -07001324 qemu_guest_random_seed_thread_part2(cpu->random_seed);
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +02001325
Paolo Bonzinid2831ab2018-01-30 11:04:53 -05001326 do {
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +02001327 qemu_mutex_unlock_iothread();
1328 do {
1329 int sig;
1330 r = sigwait(&waitset, &sig);
1331 } while (r == -1 && (errno == EAGAIN || errno == EINTR));
1332 if (r == -1) {
1333 perror("sigwait");
1334 exit(1);
1335 }
1336 qemu_mutex_lock_iothread();
Paolo Bonzinidb08b682018-01-11 13:53:12 +01001337 qemu_wait_io_event(cpu);
Paolo Bonzinid2831ab2018-01-30 11:04:53 -05001338 } while (!cpu->unplug);
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +02001339
David Hildenbrandd40bfcb2019-02-18 10:21:57 +01001340 qemu_mutex_unlock_iothread();
Paolo Bonzinid2831ab2018-01-30 11:04:53 -05001341 rcu_unregister_thread();
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +02001342 return NULL;
1343#endif
1344}
1345
Alex Bennée1be7fcb2016-10-27 16:10:08 +01001346static int64_t tcg_get_icount_limit(void)
1347{
1348 int64_t deadline;
1349
1350 if (replay_mode != REPLAY_MODE_PLAY) {
1351 deadline = qemu_clock_deadline_ns_all(QEMU_CLOCK_VIRTUAL);
1352
1353 /* Maintain prior (possibly buggy) behaviour where if no deadline
1354 * was set (as there is no QEMU_CLOCK_VIRTUAL timer) or it is more than
1355 * INT32_MAX nanoseconds ahead, we still use INT32_MAX
1356 * nanoseconds.
1357 */
1358 if ((deadline < 0) || (deadline > INT32_MAX)) {
1359 deadline = INT32_MAX;
1360 }
1361
1362 return qemu_icount_round(deadline);
1363 } else {
1364 return replay_get_instructions();
1365 }
1366}
1367
Alex Bennée12e97002016-10-27 16:10:14 +01001368static void handle_icount_deadline(void)
1369{
Paolo Bonzini6b8f0182017-03-02 19:56:40 +01001370 assert(qemu_in_vcpu_thread());
Alex Bennée12e97002016-10-27 16:10:14 +01001371 if (use_icount) {
1372 int64_t deadline =
1373 qemu_clock_deadline_ns_all(QEMU_CLOCK_VIRTUAL);
1374
1375 if (deadline == 0) {
Paolo Bonzini6b8f0182017-03-02 19:56:40 +01001376 /* Wake up other AioContexts. */
Alex Bennée12e97002016-10-27 16:10:14 +01001377 qemu_clock_notify(QEMU_CLOCK_VIRTUAL);
Paolo Bonzini6b8f0182017-03-02 19:56:40 +01001378 qemu_clock_run_timers(QEMU_CLOCK_VIRTUAL);
Alex Bennée12e97002016-10-27 16:10:14 +01001379 }
1380 }
1381}
1382
Alex Bennée05248382017-03-29 16:46:59 +01001383static void prepare_icount_for_run(CPUState *cpu)
1384{
1385 if (use_icount) {
Alex Bennéeeda5f7c2017-04-05 12:35:48 +01001386 int insns_left;
Alex Bennée05248382017-03-29 16:46:59 +01001387
1388 /* These should always be cleared by process_icount_data after
1389 * each vCPU execution. However u16.high can be raised
1390 * asynchronously by cpu_exit/cpu_interrupt/tcg_handle_interrupt
1391 */
Richard Henderson5e140192019-03-28 11:54:23 -10001392 g_assert(cpu_neg(cpu)->icount_decr.u16.low == 0);
Alex Bennée05248382017-03-29 16:46:59 +01001393 g_assert(cpu->icount_extra == 0);
1394
Alex Bennéeeda5f7c2017-04-05 12:35:48 +01001395 cpu->icount_budget = tcg_get_icount_limit();
1396 insns_left = MIN(0xffff, cpu->icount_budget);
Richard Henderson5e140192019-03-28 11:54:23 -10001397 cpu_neg(cpu)->icount_decr.u16.low = insns_left;
Alex Bennéeeda5f7c2017-04-05 12:35:48 +01001398 cpu->icount_extra = cpu->icount_budget - insns_left;
Alex Bennéed759c952018-02-27 12:52:48 +03001399
1400 replay_mutex_lock();
Alex Bennée05248382017-03-29 16:46:59 +01001401 }
1402}
1403
1404static void process_icount_data(CPUState *cpu)
1405{
1406 if (use_icount) {
Alex Bennéee4cd9652017-03-31 16:09:42 +01001407 /* Account for executed instructions */
Alex Bennée512d3c82017-04-05 12:32:37 +01001408 cpu_update_icount(cpu);
Alex Bennée05248382017-03-29 16:46:59 +01001409
1410 /* Reset the counters */
Richard Henderson5e140192019-03-28 11:54:23 -10001411 cpu_neg(cpu)->icount_decr.u16.low = 0;
Alex Bennée05248382017-03-29 16:46:59 +01001412 cpu->icount_extra = 0;
Alex Bennéee4cd9652017-03-31 16:09:42 +01001413 cpu->icount_budget = 0;
1414
Alex Bennée05248382017-03-29 16:46:59 +01001415 replay_account_executed_instructions();
Alex Bennéed759c952018-02-27 12:52:48 +03001416
1417 replay_mutex_unlock();
Alex Bennée05248382017-03-29 16:46:59 +01001418 }
1419}
1420
1421
Alex Bennée1be7fcb2016-10-27 16:10:08 +01001422static int tcg_cpu_exec(CPUState *cpu)
1423{
1424 int ret;
1425#ifdef CONFIG_PROFILER
1426 int64_t ti;
1427#endif
1428
Emilio G. Cotaf28d0df2018-06-22 13:45:31 -04001429 assert(tcg_enabled());
Alex Bennée1be7fcb2016-10-27 16:10:08 +01001430#ifdef CONFIG_PROFILER
1431 ti = profile_getclock();
1432#endif
Alex Bennée1be7fcb2016-10-27 16:10:08 +01001433 cpu_exec_start(cpu);
1434 ret = cpu_exec(cpu);
1435 cpu_exec_end(cpu);
1436#ifdef CONFIG_PROFILER
Emilio G. Cota72fd2ef2018-10-10 10:48:53 -04001437 atomic_set(&tcg_ctx->prof.cpu_exec_time,
1438 tcg_ctx->prof.cpu_exec_time + profile_getclock() - ti);
Alex Bennée1be7fcb2016-10-27 16:10:08 +01001439#endif
Alex Bennée1be7fcb2016-10-27 16:10:08 +01001440 return ret;
1441}
1442
Alex Bennéec93bbbe2016-10-27 16:10:09 +01001443/* Destroy any remaining vCPUs which have been unplugged and have
1444 * finished running
1445 */
1446static void deal_with_unplugged_cpus(void)
Alex Bennée1be7fcb2016-10-27 16:10:08 +01001447{
Alex Bennéec93bbbe2016-10-27 16:10:09 +01001448 CPUState *cpu;
Alex Bennée1be7fcb2016-10-27 16:10:08 +01001449
Alex Bennéec93bbbe2016-10-27 16:10:09 +01001450 CPU_FOREACH(cpu) {
1451 if (cpu->unplug && !cpu_can_run(cpu)) {
1452 qemu_tcg_destroy_vcpu(cpu);
1453 cpu->created = false;
1454 qemu_cond_signal(&qemu_cpu_cond);
Alex Bennée1be7fcb2016-10-27 16:10:08 +01001455 break;
1456 }
1457 }
Alex Bennée1be7fcb2016-10-27 16:10:08 +01001458}
Jan Kiszkabdb7ca62011-09-26 09:40:39 +02001459
Alex Bennée65467062017-02-23 18:29:09 +00001460/* Single-threaded TCG
1461 *
1462 * In the single-threaded case each vCPU is simulated in turn. If
1463 * there is more than a single vCPU we create a simple timer to kick
1464 * the vCPU and ensure we don't get stuck in a tight loop in one vCPU.
1465 * This is done explicitly rather than relying on side-effects
1466 * elsewhere.
1467 */
1468
Alex Bennée37257942017-02-23 18:29:14 +00001469static void *qemu_tcg_rr_cpu_thread_fn(void *arg)
Blue Swirl296af7c2010-03-29 19:23:50 +00001470{
Andreas Färberc3586ba2012-05-03 01:41:24 +02001471 CPUState *cpu = arg;
Blue Swirl296af7c2010-03-29 19:23:50 +00001472
Emilio G. Cotaf28d0df2018-06-22 13:45:31 -04001473 assert(tcg_enabled());
Paolo Bonziniab28bd22015-07-09 08:55:38 +02001474 rcu_register_thread();
Emilio G. Cota3468b592017-07-19 18:57:58 -04001475 tcg_register_thread();
Paolo Bonziniab28bd22015-07-09 08:55:38 +02001476
Paolo Bonzini2e7f7a32015-06-18 18:47:18 +02001477 qemu_mutex_lock_iothread();
Andreas Färber814e6122012-05-02 17:00:37 +02001478 qemu_thread_get_self(cpu->thread);
Blue Swirl296af7c2010-03-29 19:23:50 +00001479
David Hildenbrand5a9c9732018-02-09 20:52:39 +01001480 cpu->thread_id = qemu_get_thread_id();
1481 cpu->created = true;
1482 cpu->can_do_io = 1;
Blue Swirl296af7c2010-03-29 19:23:50 +00001483 qemu_cond_signal(&qemu_cpu_cond);
Richard Henderson9c09a252019-03-14 13:06:29 -07001484 qemu_guest_random_seed_thread_part2(cpu->random_seed);
Blue Swirl296af7c2010-03-29 19:23:50 +00001485
Jan Kiszkafa7d1862011-08-22 18:35:25 +02001486 /* wait for initial kick-off after machine start */
Emilio G. Cotac28e3992015-04-27 12:45:28 -04001487 while (first_cpu->stopped) {
KONRAD Fredericd5f8d612015-08-10 17:27:06 +02001488 qemu_cond_wait(first_cpu->halt_cond, &qemu_global_mutex);
Jan Kiszka8e564b42012-02-17 18:31:15 +01001489
1490 /* process any pending work */
Andreas Färberbdc44642013-06-24 23:50:24 +02001491 CPU_FOREACH(cpu) {
Alex Bennée37257942017-02-23 18:29:14 +00001492 current_cpu = cpu;
Andreas Färber182735e2013-05-29 22:29:20 +02001493 qemu_wait_io_event_common(cpu);
Jan Kiszka8e564b42012-02-17 18:31:15 +01001494 }
Jan Kiszka0ab07c62011-02-07 12:19:14 +01001495 }
Blue Swirl296af7c2010-03-29 19:23:50 +00001496
Alex Bennée65467062017-02-23 18:29:09 +00001497 start_tcg_kick_timer();
1498
Alex Bennéec93bbbe2016-10-27 16:10:09 +01001499 cpu = first_cpu;
1500
Alex Bennéee5143e32017-02-23 18:29:12 +00001501 /* process any pending work */
1502 cpu->exit_request = 1;
1503
Blue Swirl296af7c2010-03-29 19:23:50 +00001504 while (1) {
Alex Bennéed759c952018-02-27 12:52:48 +03001505 qemu_mutex_unlock_iothread();
1506 replay_mutex_lock();
1507 qemu_mutex_lock_iothread();
Alex Bennéec93bbbe2016-10-27 16:10:09 +01001508 /* Account partial waits to QEMU_CLOCK_VIRTUAL. */
1509 qemu_account_warp_timer();
1510
Paolo Bonzini6b8f0182017-03-02 19:56:40 +01001511 /* Run the timers here. This is much more efficient than
1512 * waking up the I/O thread and waiting for completion.
1513 */
1514 handle_icount_deadline();
1515
Alex Bennéed759c952018-02-27 12:52:48 +03001516 replay_mutex_unlock();
1517
Alex Bennéec93bbbe2016-10-27 16:10:09 +01001518 if (!cpu) {
1519 cpu = first_cpu;
1520 }
1521
Alex Bennéee5143e32017-02-23 18:29:12 +00001522 while (cpu && !cpu->queued_work_first && !cpu->exit_request) {
1523
Alex Bennée791158d2017-02-23 18:29:10 +00001524 atomic_mb_set(&tcg_current_rr_cpu, cpu);
Alex Bennée37257942017-02-23 18:29:14 +00001525 current_cpu = cpu;
Alex Bennéec93bbbe2016-10-27 16:10:09 +01001526
1527 qemu_clock_enable(QEMU_CLOCK_VIRTUAL,
1528 (cpu->singlestep_enabled & SSTEP_NOTIMER) == 0);
1529
1530 if (cpu_can_run(cpu)) {
1531 int r;
Alex Bennée05248382017-03-29 16:46:59 +01001532
Alex Bennéed759c952018-02-27 12:52:48 +03001533 qemu_mutex_unlock_iothread();
Alex Bennée05248382017-03-29 16:46:59 +01001534 prepare_icount_for_run(cpu);
1535
Alex Bennéec93bbbe2016-10-27 16:10:09 +01001536 r = tcg_cpu_exec(cpu);
Alex Bennée05248382017-03-29 16:46:59 +01001537
1538 process_icount_data(cpu);
Alex Bennéed759c952018-02-27 12:52:48 +03001539 qemu_mutex_lock_iothread();
Alex Bennée05248382017-03-29 16:46:59 +01001540
Alex Bennéec93bbbe2016-10-27 16:10:09 +01001541 if (r == EXCP_DEBUG) {
1542 cpu_handle_guest_debug(cpu);
1543 break;
Pranith Kumar08e73c42017-02-23 18:29:15 +00001544 } else if (r == EXCP_ATOMIC) {
1545 qemu_mutex_unlock_iothread();
1546 cpu_exec_step_atomic(cpu);
1547 qemu_mutex_lock_iothread();
1548 break;
Alex Bennéec93bbbe2016-10-27 16:10:09 +01001549 }
Alex Bennée37257942017-02-23 18:29:14 +00001550 } else if (cpu->stop) {
Alex Bennéec93bbbe2016-10-27 16:10:09 +01001551 if (cpu->unplug) {
1552 cpu = CPU_NEXT(cpu);
1553 }
1554 break;
1555 }
1556
Alex Bennéee5143e32017-02-23 18:29:12 +00001557 cpu = CPU_NEXT(cpu);
1558 } /* while (cpu && !cpu->exit_request).. */
1559
Alex Bennée791158d2017-02-23 18:29:10 +00001560 /* Does not need atomic_mb_set because a spurious wakeup is okay. */
1561 atomic_set(&tcg_current_rr_cpu, NULL);
Alex Bennéec93bbbe2016-10-27 16:10:09 +01001562
Alex Bennéee5143e32017-02-23 18:29:12 +00001563 if (cpu && cpu->exit_request) {
1564 atomic_mb_set(&cpu->exit_request, 0);
1565 }
Alex Blighac70aaf2013-08-21 16:02:57 +01001566
Clement Deschamps013aabd2018-10-21 16:21:03 +02001567 if (use_icount && all_cpu_threads_idle()) {
1568 /*
1569 * When all cpus are sleeping (e.g in WFI), to avoid a deadlock
1570 * in the main_loop, wake it up in order to start the warp timer.
1571 */
1572 qemu_notify_event();
1573 }
1574
Paolo Bonzinia8efa602018-11-14 12:36:57 +01001575 qemu_tcg_rr_wait_io_event();
Alex Bennéec93bbbe2016-10-27 16:10:09 +01001576 deal_with_unplugged_cpus();
Blue Swirl296af7c2010-03-29 19:23:50 +00001577 }
1578
Paolo Bonzini9b0605f2018-01-30 11:05:06 -05001579 rcu_unregister_thread();
Blue Swirl296af7c2010-03-29 19:23:50 +00001580 return NULL;
1581}
1582
Vincent Palatinb0cb0a62017-01-10 11:59:57 +01001583static void *qemu_hax_cpu_thread_fn(void *arg)
1584{
1585 CPUState *cpu = arg;
1586 int r;
Vincent Palatinb3d3a422017-03-20 11:15:49 +01001587
Paolo Bonzini9857c2d2018-01-30 16:28:49 +01001588 rcu_register_thread();
Vincent Palatinb3d3a422017-03-20 11:15:49 +01001589 qemu_mutex_lock_iothread();
Vincent Palatinb0cb0a62017-01-10 11:59:57 +01001590 qemu_thread_get_self(cpu->thread);
Vincent Palatinb0cb0a62017-01-10 11:59:57 +01001591
1592 cpu->thread_id = qemu_get_thread_id();
1593 cpu->created = true;
Vincent Palatinb0cb0a62017-01-10 11:59:57 +01001594 current_cpu = cpu;
1595
1596 hax_init_vcpu(cpu);
1597 qemu_cond_signal(&qemu_cpu_cond);
Richard Henderson9c09a252019-03-14 13:06:29 -07001598 qemu_guest_random_seed_thread_part2(cpu->random_seed);
Vincent Palatinb0cb0a62017-01-10 11:59:57 +01001599
Paolo Bonzini9857c2d2018-01-30 16:28:49 +01001600 do {
Vincent Palatinb0cb0a62017-01-10 11:59:57 +01001601 if (cpu_can_run(cpu)) {
1602 r = hax_smp_cpu_exec(cpu);
1603 if (r == EXCP_DEBUG) {
1604 cpu_handle_guest_debug(cpu);
1605 }
1606 }
1607
Paolo Bonzinidb08b682018-01-11 13:53:12 +01001608 qemu_wait_io_event(cpu);
Paolo Bonzini9857c2d2018-01-30 16:28:49 +01001609 } while (!cpu->unplug || cpu_can_run(cpu));
1610 rcu_unregister_thread();
Vincent Palatinb0cb0a62017-01-10 11:59:57 +01001611 return NULL;
1612}
1613
Sergio Andres Gomez Del Realc97d6d22017-09-13 04:05:09 -05001614/* The HVF-specific vCPU thread function. This one should only run when the host
1615 * CPU supports the VMX "unrestricted guest" feature. */
1616static void *qemu_hvf_cpu_thread_fn(void *arg)
1617{
1618 CPUState *cpu = arg;
1619
1620 int r;
1621
1622 assert(hvf_enabled());
1623
1624 rcu_register_thread();
1625
1626 qemu_mutex_lock_iothread();
1627 qemu_thread_get_self(cpu->thread);
1628
1629 cpu->thread_id = qemu_get_thread_id();
1630 cpu->can_do_io = 1;
1631 current_cpu = cpu;
1632
1633 hvf_init_vcpu(cpu);
1634
1635 /* signal CPU creation */
1636 cpu->created = true;
1637 qemu_cond_signal(&qemu_cpu_cond);
Richard Henderson9c09a252019-03-14 13:06:29 -07001638 qemu_guest_random_seed_thread_part2(cpu->random_seed);
Sergio Andres Gomez Del Realc97d6d22017-09-13 04:05:09 -05001639
1640 do {
1641 if (cpu_can_run(cpu)) {
1642 r = hvf_vcpu_exec(cpu);
1643 if (r == EXCP_DEBUG) {
1644 cpu_handle_guest_debug(cpu);
1645 }
1646 }
Paolo Bonzinidb08b682018-01-11 13:53:12 +01001647 qemu_wait_io_event(cpu);
Sergio Andres Gomez Del Realc97d6d22017-09-13 04:05:09 -05001648 } while (!cpu->unplug || cpu_can_run(cpu));
1649
1650 hvf_vcpu_destroy(cpu);
1651 cpu->created = false;
1652 qemu_cond_signal(&qemu_cpu_cond);
1653 qemu_mutex_unlock_iothread();
Paolo Bonzini8178e632018-01-30 11:05:21 -05001654 rcu_unregister_thread();
Sergio Andres Gomez Del Realc97d6d22017-09-13 04:05:09 -05001655 return NULL;
1656}
1657
Justin Terry (VM)19306802018-01-22 13:07:49 -08001658static void *qemu_whpx_cpu_thread_fn(void *arg)
1659{
1660 CPUState *cpu = arg;
1661 int r;
1662
1663 rcu_register_thread();
1664
1665 qemu_mutex_lock_iothread();
1666 qemu_thread_get_self(cpu->thread);
1667 cpu->thread_id = qemu_get_thread_id();
1668 current_cpu = cpu;
1669
1670 r = whpx_init_vcpu(cpu);
1671 if (r < 0) {
1672 fprintf(stderr, "whpx_init_vcpu failed: %s\n", strerror(-r));
1673 exit(1);
1674 }
1675
1676 /* signal CPU creation */
1677 cpu->created = true;
1678 qemu_cond_signal(&qemu_cpu_cond);
Richard Henderson9c09a252019-03-14 13:06:29 -07001679 qemu_guest_random_seed_thread_part2(cpu->random_seed);
Justin Terry (VM)19306802018-01-22 13:07:49 -08001680
1681 do {
1682 if (cpu_can_run(cpu)) {
1683 r = whpx_vcpu_exec(cpu);
1684 if (r == EXCP_DEBUG) {
1685 cpu_handle_guest_debug(cpu);
1686 }
1687 }
1688 while (cpu_thread_is_idle(cpu)) {
1689 qemu_cond_wait(cpu->halt_cond, &qemu_global_mutex);
1690 }
1691 qemu_wait_io_event_common(cpu);
1692 } while (!cpu->unplug || cpu_can_run(cpu));
1693
1694 whpx_destroy_vcpu(cpu);
1695 cpu->created = false;
1696 qemu_cond_signal(&qemu_cpu_cond);
1697 qemu_mutex_unlock_iothread();
1698 rcu_unregister_thread();
Vincent Palatinb0cb0a62017-01-10 11:59:57 +01001699 return NULL;
1700}
1701
1702#ifdef _WIN32
1703static void CALLBACK dummy_apc_func(ULONG_PTR unused)
1704{
1705}
1706#endif
1707
Alex Bennée37257942017-02-23 18:29:14 +00001708/* Multi-threaded TCG
1709 *
1710 * In the multi-threaded case each vCPU has its own thread. The TLS
1711 * variable current_cpu can be used deep in the code to find the
1712 * current CPUState for a given thread.
1713 */
1714
1715static void *qemu_tcg_cpu_thread_fn(void *arg)
1716{
1717 CPUState *cpu = arg;
1718
Emilio G. Cotaf28d0df2018-06-22 13:45:31 -04001719 assert(tcg_enabled());
Alex Bennéebf51c722017-03-30 18:32:29 +01001720 g_assert(!use_icount);
1721
Alex Bennée37257942017-02-23 18:29:14 +00001722 rcu_register_thread();
Emilio G. Cota3468b592017-07-19 18:57:58 -04001723 tcg_register_thread();
Alex Bennée37257942017-02-23 18:29:14 +00001724
1725 qemu_mutex_lock_iothread();
1726 qemu_thread_get_self(cpu->thread);
1727
1728 cpu->thread_id = qemu_get_thread_id();
1729 cpu->created = true;
1730 cpu->can_do_io = 1;
1731 current_cpu = cpu;
1732 qemu_cond_signal(&qemu_cpu_cond);
Richard Henderson9c09a252019-03-14 13:06:29 -07001733 qemu_guest_random_seed_thread_part2(cpu->random_seed);
Alex Bennée37257942017-02-23 18:29:14 +00001734
1735 /* process any pending work */
1736 cpu->exit_request = 1;
1737
Cédric Le Goater54961aa2018-04-25 15:18:28 +02001738 do {
Alex Bennée37257942017-02-23 18:29:14 +00001739 if (cpu_can_run(cpu)) {
1740 int r;
Alex Bennéed759c952018-02-27 12:52:48 +03001741 qemu_mutex_unlock_iothread();
Alex Bennée37257942017-02-23 18:29:14 +00001742 r = tcg_cpu_exec(cpu);
Alex Bennéed759c952018-02-27 12:52:48 +03001743 qemu_mutex_lock_iothread();
Alex Bennée37257942017-02-23 18:29:14 +00001744 switch (r) {
1745 case EXCP_DEBUG:
1746 cpu_handle_guest_debug(cpu);
1747 break;
1748 case EXCP_HALTED:
1749 /* during start-up the vCPU is reset and the thread is
1750 * kicked several times. If we don't ensure we go back
1751 * to sleep in the halted state we won't cleanly
1752 * start-up when the vCPU is enabled.
1753 *
1754 * cpu->halted should ensure we sleep in wait_io_event
1755 */
1756 g_assert(cpu->halted);
1757 break;
Pranith Kumar08e73c42017-02-23 18:29:15 +00001758 case EXCP_ATOMIC:
1759 qemu_mutex_unlock_iothread();
1760 cpu_exec_step_atomic(cpu);
1761 qemu_mutex_lock_iothread();
Alex Bennée37257942017-02-23 18:29:14 +00001762 default:
1763 /* Ignore everything else? */
1764 break;
1765 }
1766 }
1767
Alex Bennée37257942017-02-23 18:29:14 +00001768 atomic_mb_set(&cpu->exit_request, 0);
Paolo Bonzinidb08b682018-01-11 13:53:12 +01001769 qemu_wait_io_event(cpu);
Paolo Bonzini9b0605f2018-01-30 11:05:06 -05001770 } while (!cpu->unplug || cpu_can_run(cpu));
Alex Bennée37257942017-02-23 18:29:14 +00001771
Paolo Bonzini9b0605f2018-01-30 11:05:06 -05001772 qemu_tcg_destroy_vcpu(cpu);
1773 cpu->created = false;
1774 qemu_cond_signal(&qemu_cpu_cond);
1775 qemu_mutex_unlock_iothread();
1776 rcu_unregister_thread();
Alex Bennée37257942017-02-23 18:29:14 +00001777 return NULL;
1778}
1779
Andreas Färber2ff09a42012-05-03 00:23:30 +02001780static void qemu_cpu_kick_thread(CPUState *cpu)
Paolo Bonzinicc015e92011-03-12 17:44:08 +01001781{
1782#ifndef _WIN32
1783 int err;
1784
Paolo Bonzinie0c38212015-08-26 00:19:19 +02001785 if (cpu->thread_kicked) {
1786 return;
Paolo Bonzini9102ded2015-08-18 06:52:09 -07001787 }
Paolo Bonzinie0c38212015-08-26 00:19:19 +02001788 cpu->thread_kicked = true;
Andreas Färber814e6122012-05-02 17:00:37 +02001789 err = pthread_kill(cpu->thread->thread, SIG_IPI);
Laurent Vivierd455ebc2019-01-02 15:16:03 +01001790 if (err && err != ESRCH) {
Paolo Bonzinicc015e92011-03-12 17:44:08 +01001791 fprintf(stderr, "qemu:%s: %s", __func__, strerror(err));
1792 exit(1);
1793 }
1794#else /* _WIN32 */
Vincent Palatinb0cb0a62017-01-10 11:59:57 +01001795 if (!qemu_cpu_is_self(cpu)) {
Justin Terry (VM)19306802018-01-22 13:07:49 -08001796 if (whpx_enabled()) {
1797 whpx_vcpu_kick(cpu);
1798 } else if (!QueueUserAPC(dummy_apc_func, cpu->hThread, 0)) {
Vincent Palatinb0cb0a62017-01-10 11:59:57 +01001799 fprintf(stderr, "%s: QueueUserAPC failed with error %lu\n",
1800 __func__, GetLastError());
1801 exit(1);
1802 }
1803 }
Paolo Bonzinicc015e92011-03-12 17:44:08 +01001804#endif
1805}
1806
Andreas Färberc08d7422012-05-03 04:34:15 +02001807void qemu_cpu_kick(CPUState *cpu)
Blue Swirl296af7c2010-03-29 19:23:50 +00001808{
Andreas Färberf5c121b2012-05-03 01:22:49 +02001809 qemu_cond_broadcast(cpu->halt_cond);
Paolo Bonzinie0c38212015-08-26 00:19:19 +02001810 if (tcg_enabled()) {
Alex Bennée791158d2017-02-23 18:29:10 +00001811 cpu_exit(cpu);
Alex Bennée37257942017-02-23 18:29:14 +00001812 /* NOP unless doing single-thread RR */
Alex Bennée791158d2017-02-23 18:29:10 +00001813 qemu_cpu_kick_rr_cpu();
Paolo Bonzinie0c38212015-08-26 00:19:19 +02001814 } else {
Vincent Palatinb0cb0a62017-01-10 11:59:57 +01001815 if (hax_enabled()) {
1816 /*
1817 * FIXME: race condition with the exit_request check in
1818 * hax_vcpu_hax_exec
1819 */
1820 cpu->exit_request = 1;
1821 }
Paolo Bonzinie0c38212015-08-26 00:19:19 +02001822 qemu_cpu_kick_thread(cpu);
1823 }
Blue Swirl296af7c2010-03-29 19:23:50 +00001824}
1825
Jan Kiszka46d62fa2011-02-01 22:15:59 +01001826void qemu_cpu_kick_self(void)
1827{
Andreas Färber4917cf42013-05-27 05:17:50 +02001828 assert(current_cpu);
Paolo Bonzini9102ded2015-08-18 06:52:09 -07001829 qemu_cpu_kick_thread(current_cpu);
Blue Swirl296af7c2010-03-29 19:23:50 +00001830}
1831
Andreas Färber60e82572012-05-02 22:23:49 +02001832bool qemu_cpu_is_self(CPUState *cpu)
Blue Swirl296af7c2010-03-29 19:23:50 +00001833{
Andreas Färber814e6122012-05-02 17:00:37 +02001834 return qemu_thread_is_self(cpu->thread);
Blue Swirl296af7c2010-03-29 19:23:50 +00001835}
1836
Paolo Bonzini79e2b9a2015-01-21 12:09:14 +01001837bool qemu_in_vcpu_thread(void)
Juan Quintelaaa723c22012-09-18 16:30:11 +02001838{
Andreas Färber4917cf42013-05-27 05:17:50 +02001839 return current_cpu && qemu_cpu_is_self(current_cpu);
Juan Quintelaaa723c22012-09-18 16:30:11 +02001840}
1841
Paolo Bonziniafbe7052015-06-18 18:47:19 +02001842static __thread bool iothread_locked = false;
1843
1844bool qemu_mutex_iothread_locked(void)
1845{
1846 return iothread_locked;
1847}
1848
Emilio G. Cotacb764d02017-10-28 02:16:41 -04001849/*
1850 * The BQL is taken from so many places that it is worth profiling the
1851 * callers directly, instead of funneling them all through a single function.
1852 */
1853void qemu_mutex_lock_iothread_impl(const char *file, int line)
Blue Swirl296af7c2010-03-29 19:23:50 +00001854{
Emilio G. Cotacb764d02017-10-28 02:16:41 -04001855 QemuMutexLockFunc bql_lock = atomic_read(&qemu_bql_mutex_lock_func);
1856
Jan Kiszka8d04fb52017-02-23 18:29:11 +00001857 g_assert(!qemu_mutex_iothread_locked());
Emilio G. Cotacb764d02017-10-28 02:16:41 -04001858 bql_lock(&qemu_global_mutex, file, line);
Paolo Bonziniafbe7052015-06-18 18:47:19 +02001859 iothread_locked = true;
Blue Swirl296af7c2010-03-29 19:23:50 +00001860}
1861
1862void qemu_mutex_unlock_iothread(void)
1863{
Jan Kiszka8d04fb52017-02-23 18:29:11 +00001864 g_assert(qemu_mutex_iothread_locked());
Paolo Bonziniafbe7052015-06-18 18:47:19 +02001865 iothread_locked = false;
Blue Swirl296af7c2010-03-29 19:23:50 +00001866 qemu_mutex_unlock(&qemu_global_mutex);
1867}
1868
Alex Bennéee8faee02016-10-27 16:09:58 +01001869static bool all_vcpus_paused(void)
Blue Swirl296af7c2010-03-29 19:23:50 +00001870{
Andreas Färberbdc44642013-06-24 23:50:24 +02001871 CPUState *cpu;
Blue Swirl296af7c2010-03-29 19:23:50 +00001872
Andreas Färberbdc44642013-06-24 23:50:24 +02001873 CPU_FOREACH(cpu) {
Andreas Färber182735e2013-05-29 22:29:20 +02001874 if (!cpu->stopped) {
Alex Bennéee8faee02016-10-27 16:09:58 +01001875 return false;
Jan Kiszka0ab07c62011-02-07 12:19:14 +01001876 }
Blue Swirl296af7c2010-03-29 19:23:50 +00001877 }
1878
Alex Bennéee8faee02016-10-27 16:09:58 +01001879 return true;
Blue Swirl296af7c2010-03-29 19:23:50 +00001880}
1881
1882void pause_all_vcpus(void)
1883{
Andreas Färberbdc44642013-06-24 23:50:24 +02001884 CPUState *cpu;
Blue Swirl296af7c2010-03-29 19:23:50 +00001885
Alex Bligh40daca52013-08-21 16:03:02 +01001886 qemu_clock_enable(QEMU_CLOCK_VIRTUAL, false);
Andreas Färberbdc44642013-06-24 23:50:24 +02001887 CPU_FOREACH(cpu) {
David Hildenbrandebd05fe2017-11-29 20:12:15 +01001888 if (qemu_cpu_is_self(cpu)) {
1889 qemu_cpu_stop(cpu, true);
1890 } else {
1891 cpu->stop = true;
1892 qemu_cpu_kick(cpu);
1893 }
Jan Kiszkad798e972012-02-17 18:31:16 +01001894 }
1895
Alex Bennéed759c952018-02-27 12:52:48 +03001896 /* We need to drop the replay_lock so any vCPU threads woken up
1897 * can finish their replay tasks
1898 */
1899 replay_mutex_unlock();
1900
Blue Swirl296af7c2010-03-29 19:23:50 +00001901 while (!all_vcpus_paused()) {
Paolo Bonzinibe7d6c52011-03-12 17:44:02 +01001902 qemu_cond_wait(&qemu_pause_cond, &qemu_global_mutex);
Andreas Färberbdc44642013-06-24 23:50:24 +02001903 CPU_FOREACH(cpu) {
Andreas Färber182735e2013-05-29 22:29:20 +02001904 qemu_cpu_kick(cpu);
Blue Swirl296af7c2010-03-29 19:23:50 +00001905 }
1906 }
Alex Bennéed759c952018-02-27 12:52:48 +03001907
1908 qemu_mutex_unlock_iothread();
1909 replay_mutex_lock();
1910 qemu_mutex_lock_iothread();
Blue Swirl296af7c2010-03-29 19:23:50 +00001911}
1912
Igor Mammedov29936832013-04-23 10:29:37 +02001913void cpu_resume(CPUState *cpu)
1914{
1915 cpu->stop = false;
1916 cpu->stopped = false;
1917 qemu_cpu_kick(cpu);
1918}
1919
Blue Swirl296af7c2010-03-29 19:23:50 +00001920void resume_all_vcpus(void)
1921{
Andreas Färberbdc44642013-06-24 23:50:24 +02001922 CPUState *cpu;
Blue Swirl296af7c2010-03-29 19:23:50 +00001923
Alex Bligh40daca52013-08-21 16:03:02 +01001924 qemu_clock_enable(QEMU_CLOCK_VIRTUAL, true);
Andreas Färberbdc44642013-06-24 23:50:24 +02001925 CPU_FOREACH(cpu) {
Andreas Färber182735e2013-05-29 22:29:20 +02001926 cpu_resume(cpu);
Blue Swirl296af7c2010-03-29 19:23:50 +00001927 }
1928}
1929
Paolo Bonzinidbadee42018-01-30 16:40:12 +01001930void cpu_remove_sync(CPUState *cpu)
Gu Zheng4c055ab2016-05-12 09:18:13 +05301931{
1932 cpu->stop = true;
1933 cpu->unplug = true;
1934 qemu_cpu_kick(cpu);
Paolo Bonzinidbadee42018-01-30 16:40:12 +01001935 qemu_mutex_unlock_iothread();
1936 qemu_thread_join(cpu->thread);
1937 qemu_mutex_lock_iothread();
Bharata B Rao2c579042016-05-12 09:18:14 +05301938}
1939
Dr. David Alan Gilbert49001162014-01-30 10:20:32 +00001940/* For temporary buffers for forming a name */
1941#define VCPU_THREAD_NAME_SIZE 16
1942
Andreas Färbere5ab30a2012-05-03 01:50:44 +02001943static void qemu_tcg_init_vcpu(CPUState *cpu)
Blue Swirl296af7c2010-03-29 19:23:50 +00001944{
Dr. David Alan Gilbert49001162014-01-30 10:20:32 +00001945 char thread_name[VCPU_THREAD_NAME_SIZE];
Alex Bennée37257942017-02-23 18:29:14 +00001946 static QemuCond *single_tcg_halt_cond;
1947 static QemuThread *single_tcg_cpu_thread;
Emilio G. Cotae8feb962017-07-07 19:24:20 -04001948 static int tcg_region_inited;
1949
Emilio G. Cotaf28d0df2018-06-22 13:45:31 -04001950 assert(tcg_enabled());
Emilio G. Cotae8feb962017-07-07 19:24:20 -04001951 /*
1952 * Initialize TCG regions--once. Now is a good time, because:
1953 * (1) TCG's init context, prologue and target globals have been set up.
1954 * (2) qemu_tcg_mttcg_enabled() works now (TCG init code runs before the
1955 * -accel flag is processed, so the check doesn't work then).
1956 */
1957 if (!tcg_region_inited) {
1958 tcg_region_inited = 1;
1959 tcg_region_init();
1960 }
Dr. David Alan Gilbert49001162014-01-30 10:20:32 +00001961
Alex Bennée37257942017-02-23 18:29:14 +00001962 if (qemu_tcg_mttcg_enabled() || !single_tcg_cpu_thread) {
Andreas Färber814e6122012-05-02 17:00:37 +02001963 cpu->thread = g_malloc0(sizeof(QemuThread));
Andreas Färberf5c121b2012-05-03 01:22:49 +02001964 cpu->halt_cond = g_malloc0(sizeof(QemuCond));
1965 qemu_cond_init(cpu->halt_cond);
Alex Bennée37257942017-02-23 18:29:14 +00001966
1967 if (qemu_tcg_mttcg_enabled()) {
1968 /* create a thread per vCPU with TCG (MTTCG) */
1969 parallel_cpus = true;
1970 snprintf(thread_name, VCPU_THREAD_NAME_SIZE, "CPU %d/TCG",
Dr. David Alan Gilbert49001162014-01-30 10:20:32 +00001971 cpu->cpu_index);
Alex Bennée37257942017-02-23 18:29:14 +00001972
1973 qemu_thread_create(cpu->thread, thread_name, qemu_tcg_cpu_thread_fn,
1974 cpu, QEMU_THREAD_JOINABLE);
1975
1976 } else {
1977 /* share a single thread for all cpus with TCG */
1978 snprintf(thread_name, VCPU_THREAD_NAME_SIZE, "ALL CPUs/TCG");
1979 qemu_thread_create(cpu->thread, thread_name,
1980 qemu_tcg_rr_cpu_thread_fn,
1981 cpu, QEMU_THREAD_JOINABLE);
1982
1983 single_tcg_halt_cond = cpu->halt_cond;
1984 single_tcg_cpu_thread = cpu->thread;
1985 }
Paolo Bonzini1ecf47b2011-12-13 13:43:52 +01001986#ifdef _WIN32
Andreas Färber814e6122012-05-02 17:00:37 +02001987 cpu->hThread = qemu_thread_get_handle(cpu->thread);
Paolo Bonzini1ecf47b2011-12-13 13:43:52 +01001988#endif
Blue Swirl296af7c2010-03-29 19:23:50 +00001989 } else {
Alex Bennée37257942017-02-23 18:29:14 +00001990 /* For non-MTTCG cases we share the thread */
1991 cpu->thread = single_tcg_cpu_thread;
1992 cpu->halt_cond = single_tcg_halt_cond;
David Hildenbranda3421732018-02-09 20:52:37 +01001993 cpu->thread_id = first_cpu->thread_id;
1994 cpu->can_do_io = 1;
1995 cpu->created = true;
Blue Swirl296af7c2010-03-29 19:23:50 +00001996 }
1997}
1998
Vincent Palatinb0cb0a62017-01-10 11:59:57 +01001999static void qemu_hax_start_vcpu(CPUState *cpu)
2000{
2001 char thread_name[VCPU_THREAD_NAME_SIZE];
2002
2003 cpu->thread = g_malloc0(sizeof(QemuThread));
2004 cpu->halt_cond = g_malloc0(sizeof(QemuCond));
2005 qemu_cond_init(cpu->halt_cond);
2006
2007 snprintf(thread_name, VCPU_THREAD_NAME_SIZE, "CPU %d/HAX",
2008 cpu->cpu_index);
2009 qemu_thread_create(cpu->thread, thread_name, qemu_hax_cpu_thread_fn,
2010 cpu, QEMU_THREAD_JOINABLE);
2011#ifdef _WIN32
2012 cpu->hThread = qemu_thread_get_handle(cpu->thread);
2013#endif
Vincent Palatinb0cb0a62017-01-10 11:59:57 +01002014}
2015
Andreas Färber48a106b2013-05-27 02:20:39 +02002016static void qemu_kvm_start_vcpu(CPUState *cpu)
Blue Swirl296af7c2010-03-29 19:23:50 +00002017{
Dr. David Alan Gilbert49001162014-01-30 10:20:32 +00002018 char thread_name[VCPU_THREAD_NAME_SIZE];
2019
Andreas Färber814e6122012-05-02 17:00:37 +02002020 cpu->thread = g_malloc0(sizeof(QemuThread));
Andreas Färberf5c121b2012-05-03 01:22:49 +02002021 cpu->halt_cond = g_malloc0(sizeof(QemuCond));
2022 qemu_cond_init(cpu->halt_cond);
Dr. David Alan Gilbert49001162014-01-30 10:20:32 +00002023 snprintf(thread_name, VCPU_THREAD_NAME_SIZE, "CPU %d/KVM",
2024 cpu->cpu_index);
2025 qemu_thread_create(cpu->thread, thread_name, qemu_kvm_cpu_thread_fn,
2026 cpu, QEMU_THREAD_JOINABLE);
Blue Swirl296af7c2010-03-29 19:23:50 +00002027}
2028
Sergio Andres Gomez Del Realc97d6d22017-09-13 04:05:09 -05002029static void qemu_hvf_start_vcpu(CPUState *cpu)
2030{
2031 char thread_name[VCPU_THREAD_NAME_SIZE];
2032
2033 /* HVF currently does not support TCG, and only runs in
2034 * unrestricted-guest mode. */
2035 assert(hvf_enabled());
2036
2037 cpu->thread = g_malloc0(sizeof(QemuThread));
2038 cpu->halt_cond = g_malloc0(sizeof(QemuCond));
2039 qemu_cond_init(cpu->halt_cond);
2040
2041 snprintf(thread_name, VCPU_THREAD_NAME_SIZE, "CPU %d/HVF",
2042 cpu->cpu_index);
2043 qemu_thread_create(cpu->thread, thread_name, qemu_hvf_cpu_thread_fn,
2044 cpu, QEMU_THREAD_JOINABLE);
Sergio Andres Gomez Del Realc97d6d22017-09-13 04:05:09 -05002045}
2046
Justin Terry (VM)19306802018-01-22 13:07:49 -08002047static void qemu_whpx_start_vcpu(CPUState *cpu)
2048{
2049 char thread_name[VCPU_THREAD_NAME_SIZE];
2050
2051 cpu->thread = g_malloc0(sizeof(QemuThread));
2052 cpu->halt_cond = g_malloc0(sizeof(QemuCond));
2053 qemu_cond_init(cpu->halt_cond);
2054 snprintf(thread_name, VCPU_THREAD_NAME_SIZE, "CPU %d/WHPX",
2055 cpu->cpu_index);
2056 qemu_thread_create(cpu->thread, thread_name, qemu_whpx_cpu_thread_fn,
2057 cpu, QEMU_THREAD_JOINABLE);
2058#ifdef _WIN32
2059 cpu->hThread = qemu_thread_get_handle(cpu->thread);
2060#endif
Justin Terry (VM)19306802018-01-22 13:07:49 -08002061}
2062
Andreas Färber10a90212013-05-27 02:24:35 +02002063static void qemu_dummy_start_vcpu(CPUState *cpu)
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +02002064{
Dr. David Alan Gilbert49001162014-01-30 10:20:32 +00002065 char thread_name[VCPU_THREAD_NAME_SIZE];
2066
Andreas Färber814e6122012-05-02 17:00:37 +02002067 cpu->thread = g_malloc0(sizeof(QemuThread));
Andreas Färberf5c121b2012-05-03 01:22:49 +02002068 cpu->halt_cond = g_malloc0(sizeof(QemuCond));
2069 qemu_cond_init(cpu->halt_cond);
Dr. David Alan Gilbert49001162014-01-30 10:20:32 +00002070 snprintf(thread_name, VCPU_THREAD_NAME_SIZE, "CPU %d/DUMMY",
2071 cpu->cpu_index);
2072 qemu_thread_create(cpu->thread, thread_name, qemu_dummy_cpu_thread_fn, cpu,
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +02002073 QEMU_THREAD_JOINABLE);
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +02002074}
2075
Andreas Färberc643bed2013-05-27 03:23:24 +02002076void qemu_init_vcpu(CPUState *cpu)
Blue Swirl296af7c2010-03-29 19:23:50 +00002077{
Andreas Färberce3960e2012-12-17 03:27:07 +01002078 cpu->nr_cores = smp_cores;
2079 cpu->nr_threads = smp_threads;
Andreas Färberf324e762012-05-02 23:26:21 +02002080 cpu->stopped = true;
Richard Henderson9c09a252019-03-14 13:06:29 -07002081 cpu->random_seed = qemu_guest_random_seed_thread_part1();
Peter Maydell56943e82016-01-21 14:15:04 +00002082
2083 if (!cpu->as) {
2084 /* If the target cpu hasn't set up any address spaces itself,
2085 * give it the default one.
2086 */
Peter Maydell12ebc9a2016-01-21 14:15:04 +00002087 cpu->num_ases = 1;
Peter Xu80ceb072017-11-23 17:23:32 +08002088 cpu_address_space_init(cpu, 0, "cpu-memory", cpu->memory);
Peter Maydell56943e82016-01-21 14:15:04 +00002089 }
2090
Jan Kiszka0ab07c62011-02-07 12:19:14 +01002091 if (kvm_enabled()) {
Andreas Färber48a106b2013-05-27 02:20:39 +02002092 qemu_kvm_start_vcpu(cpu);
Vincent Palatinb0cb0a62017-01-10 11:59:57 +01002093 } else if (hax_enabled()) {
2094 qemu_hax_start_vcpu(cpu);
Sergio Andres Gomez Del Realc97d6d22017-09-13 04:05:09 -05002095 } else if (hvf_enabled()) {
2096 qemu_hvf_start_vcpu(cpu);
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +02002097 } else if (tcg_enabled()) {
Andreas Färbere5ab30a2012-05-03 01:50:44 +02002098 qemu_tcg_init_vcpu(cpu);
Justin Terry (VM)19306802018-01-22 13:07:49 -08002099 } else if (whpx_enabled()) {
2100 qemu_whpx_start_vcpu(cpu);
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +02002101 } else {
Andreas Färber10a90212013-05-27 02:24:35 +02002102 qemu_dummy_start_vcpu(cpu);
Jan Kiszka0ab07c62011-02-07 12:19:14 +01002103 }
David Hildenbrand81e96312018-02-09 20:52:38 +01002104
2105 while (!cpu->created) {
2106 qemu_cond_wait(&qemu_cpu_cond, &qemu_global_mutex);
2107 }
Blue Swirl296af7c2010-03-29 19:23:50 +00002108}
2109
Jan Kiszkab4a3d962011-02-01 22:15:43 +01002110void cpu_stop_current(void)
Blue Swirl296af7c2010-03-29 19:23:50 +00002111{
Andreas Färber4917cf42013-05-27 05:17:50 +02002112 if (current_cpu) {
Peter Maydell0ec7e672019-01-07 15:23:47 +00002113 current_cpu->stop = true;
2114 cpu_exit(current_cpu);
Jan Kiszkab4a3d962011-02-01 22:15:43 +01002115 }
Blue Swirl296af7c2010-03-29 19:23:50 +00002116}
2117
Kevin Wolf56983462013-07-05 13:49:54 +02002118int vm_stop(RunState state)
Blue Swirl296af7c2010-03-29 19:23:50 +00002119{
Juan Quintelaaa723c22012-09-18 16:30:11 +02002120 if (qemu_in_vcpu_thread()) {
Paolo Bonzini74892d22014-06-05 14:53:58 +02002121 qemu_system_vmstop_request_prepare();
Luiz Capitulino1dfb4dd2011-07-29 14:26:33 -03002122 qemu_system_vmstop_request(state);
Blue Swirl296af7c2010-03-29 19:23:50 +00002123 /*
2124 * FIXME: should not return to device code in case
2125 * vm_stop() has been requested.
2126 */
Jan Kiszkab4a3d962011-02-01 22:15:43 +01002127 cpu_stop_current();
Kevin Wolf56983462013-07-05 13:49:54 +02002128 return 0;
Blue Swirl296af7c2010-03-29 19:23:50 +00002129 }
Kevin Wolf56983462013-07-05 13:49:54 +02002130
Stefan Hajnoczi4486e892018-03-07 14:42:05 +00002131 return do_vm_stop(state, true);
Blue Swirl296af7c2010-03-29 19:23:50 +00002132}
2133
Claudio Imbrenda2d76e822017-02-14 18:07:47 +01002134/**
2135 * Prepare for (re)starting the VM.
2136 * Returns -1 if the vCPUs are not to be restarted (e.g. if they are already
2137 * running or in case of an error condition), 0 otherwise.
2138 */
2139int vm_prepare_start(void)
2140{
2141 RunState requested;
Claudio Imbrenda2d76e822017-02-14 18:07:47 +01002142
2143 qemu_vmstop_requested(&requested);
2144 if (runstate_is_running() && requested == RUN_STATE__MAX) {
2145 return -1;
2146 }
2147
2148 /* Ensure that a STOP/RESUME pair of events is emitted if a
2149 * vmstop request was pending. The BLOCK_IO_ERROR event, for
2150 * example, according to documentation is always followed by
2151 * the STOP event.
2152 */
2153 if (runstate_is_running()) {
Peter Xu3ab72382018-08-15 21:37:37 +08002154 qapi_event_send_stop();
2155 qapi_event_send_resume();
Markus Armbrusterf0561582018-04-23 10:45:18 +02002156 return -1;
Claudio Imbrenda2d76e822017-02-14 18:07:47 +01002157 }
2158
2159 /* We are sending this now, but the CPUs will be resumed shortly later */
Peter Xu3ab72382018-08-15 21:37:37 +08002160 qapi_event_send_resume();
Markus Armbrusterf0561582018-04-23 10:45:18 +02002161
2162 replay_enable_events();
2163 cpu_enable_ticks();
2164 runstate_set(RUN_STATE_RUNNING);
2165 vm_state_notify(1, RUN_STATE_RUNNING);
2166 return 0;
Claudio Imbrenda2d76e822017-02-14 18:07:47 +01002167}
2168
2169void vm_start(void)
2170{
2171 if (!vm_prepare_start()) {
2172 resume_all_vcpus();
2173 }
2174}
2175
Luiz Capitulino8a9236f2011-10-14 11:18:09 -03002176/* does a state transition even if the VM is already stopped,
2177 current state is forgotten forever */
Kevin Wolf56983462013-07-05 13:49:54 +02002178int vm_stop_force_state(RunState state)
Luiz Capitulino8a9236f2011-10-14 11:18:09 -03002179{
2180 if (runstate_is_running()) {
Kevin Wolf56983462013-07-05 13:49:54 +02002181 return vm_stop(state);
Luiz Capitulino8a9236f2011-10-14 11:18:09 -03002182 } else {
2183 runstate_set(state);
Wen Congyangb2780d32015-11-20 17:34:38 +08002184
2185 bdrv_drain_all();
Kevin Wolf594a45c2013-07-18 14:52:19 +02002186 /* Make sure to return an error if the flush in a previous vm_stop()
2187 * failed. */
John Snow22af08e2016-09-22 21:45:51 -04002188 return bdrv_flush_all();
Luiz Capitulino8a9236f2011-10-14 11:18:09 -03002189 }
2190}
2191
Markus Armbruster04424282019-04-17 21:17:57 +02002192void list_cpus(const char *optarg)
Blue Swirl262353c2010-05-04 19:55:35 +00002193{
2194 /* XXX: implement xxx_cpu_list for targets that still miss it */
Peter Maydelle916cbf2012-09-05 17:41:08 -03002195#if defined(cpu_list)
Markus Armbruster04424282019-04-17 21:17:57 +02002196 cpu_list();
Blue Swirl262353c2010-05-04 19:55:35 +00002197#endif
2198}
Luiz Capitulinode0b36b2011-09-21 16:38:35 -03002199
Luiz Capitulino0cfd6a92011-11-22 16:32:37 -02002200void qmp_memsave(int64_t addr, int64_t size, const char *filename,
2201 bool has_cpu, int64_t cpu_index, Error **errp)
2202{
2203 FILE *f;
2204 uint32_t l;
Andreas Färber55e5c282012-12-17 06:18:02 +01002205 CPUState *cpu;
Luiz Capitulino0cfd6a92011-11-22 16:32:37 -02002206 uint8_t buf[1024];
Borislav Petkov0dc9daf2015-02-08 13:14:38 +01002207 int64_t orig_addr = addr, orig_size = size;
Luiz Capitulino0cfd6a92011-11-22 16:32:37 -02002208
2209 if (!has_cpu) {
2210 cpu_index = 0;
2211 }
2212
Andreas Färber151d1322013-02-15 15:41:49 +01002213 cpu = qemu_get_cpu(cpu_index);
2214 if (cpu == NULL) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01002215 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "cpu-index",
2216 "a CPU number");
Luiz Capitulino0cfd6a92011-11-22 16:32:37 -02002217 return;
2218 }
2219
2220 f = fopen(filename, "wb");
2221 if (!f) {
Luiz Capitulino618da852013-06-07 14:35:06 -04002222 error_setg_file_open(errp, errno, filename);
Luiz Capitulino0cfd6a92011-11-22 16:32:37 -02002223 return;
2224 }
2225
2226 while (size != 0) {
2227 l = sizeof(buf);
2228 if (l > size)
2229 l = size;
Aneesh Kumar K.V2f4d0f52013-10-01 21:49:30 +05302230 if (cpu_memory_rw_debug(cpu, addr, buf, l, 0) != 0) {
Borislav Petkov0dc9daf2015-02-08 13:14:38 +01002231 error_setg(errp, "Invalid addr 0x%016" PRIx64 "/size %" PRId64
2232 " specified", orig_addr, orig_size);
Aneesh Kumar K.V2f4d0f52013-10-01 21:49:30 +05302233 goto exit;
2234 }
Luiz Capitulino0cfd6a92011-11-22 16:32:37 -02002235 if (fwrite(buf, 1, l, f) != l) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01002236 error_setg(errp, QERR_IO_ERROR);
Luiz Capitulino0cfd6a92011-11-22 16:32:37 -02002237 goto exit;
2238 }
2239 addr += l;
2240 size -= l;
2241 }
2242
2243exit:
2244 fclose(f);
2245}
Luiz Capitulino6d3962b2011-11-22 17:26:46 -02002246
2247void qmp_pmemsave(int64_t addr, int64_t size, const char *filename,
2248 Error **errp)
2249{
2250 FILE *f;
2251 uint32_t l;
2252 uint8_t buf[1024];
2253
2254 f = fopen(filename, "wb");
2255 if (!f) {
Luiz Capitulino618da852013-06-07 14:35:06 -04002256 error_setg_file_open(errp, errno, filename);
Luiz Capitulino6d3962b2011-11-22 17:26:46 -02002257 return;
2258 }
2259
2260 while (size != 0) {
2261 l = sizeof(buf);
2262 if (l > size)
2263 l = size;
Stefan Weileb6282f2014-04-07 20:28:23 +02002264 cpu_physical_memory_read(addr, buf, l);
Luiz Capitulino6d3962b2011-11-22 17:26:46 -02002265 if (fwrite(buf, 1, l, f) != l) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01002266 error_setg(errp, QERR_IO_ERROR);
Luiz Capitulino6d3962b2011-11-22 17:26:46 -02002267 goto exit;
2268 }
2269 addr += l;
2270 size -= l;
2271 }
2272
2273exit:
2274 fclose(f);
2275}
Luiz Capitulinoab49ab52011-11-23 12:55:53 -02002276
2277void qmp_inject_nmi(Error **errp)
2278{
Alexey Kardashevskiy9cb805f2014-08-20 22:16:33 +10002279 nmi_monitor_handle(monitor_get_cpu_index(), errp);
Luiz Capitulinoab49ab52011-11-23 12:55:53 -02002280}
Sebastian Tanase27498be2014-07-25 11:56:33 +02002281
Markus Armbruster76c86612019-04-17 21:17:53 +02002282void dump_drift_info(void)
Sebastian Tanase27498be2014-07-25 11:56:33 +02002283{
2284 if (!use_icount) {
2285 return;
2286 }
2287
Markus Armbruster76c86612019-04-17 21:17:53 +02002288 qemu_printf("Host - Guest clock %"PRIi64" ms\n",
Sebastian Tanase27498be2014-07-25 11:56:33 +02002289 (cpu_get_clock() - cpu_get_icount())/SCALE_MS);
2290 if (icount_align_option) {
Markus Armbruster76c86612019-04-17 21:17:53 +02002291 qemu_printf("Max guest delay %"PRIi64" ms\n",
2292 -max_delay / SCALE_MS);
2293 qemu_printf("Max guest advance %"PRIi64" ms\n",
2294 max_advance / SCALE_MS);
Sebastian Tanase27498be2014-07-25 11:56:33 +02002295 } else {
Markus Armbruster76c86612019-04-17 21:17:53 +02002296 qemu_printf("Max guest delay NA\n");
2297 qemu_printf("Max guest advance NA\n");
Sebastian Tanase27498be2014-07-25 11:56:33 +02002298 }
2299}