blob: 63bda152f5743d4cbb80512b996c89d893aeed9e [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"
Markus Armbrusterd6454272019-08-12 07:23:45 +020028#include "migration/vmstate.h"
Paolo Bonzini83c90892012-12-17 18:19:49 +010029#include "monitor/monitor.h"
Markus Armbrustere688df62018-02-01 12:18:31 +010030#include "qapi/error.h"
Markus Armbruster112ed242018-02-26 17:13:27 -060031#include "qapi/qapi-commands-misc.h"
Markus Armbruster9af23982018-02-11 10:36:01 +010032#include "qapi/qapi-events-run-state.h"
Wenchao Xiaa4e15de2014-06-18 08:43:36 +020033#include "qapi/qmp/qerror.h"
Markus Armbrusterd49b6832015-03-17 18:29:20 +010034#include "qemu/error-report.h"
Markus Armbruster76c86612019-04-17 21:17:53 +020035#include "qemu/qemu-print.h"
Markus Armbruster14a48c12019-05-23 16:35:05 +020036#include "sysemu/tcg.h"
Max Reitzda31d592016-03-16 19:54:32 +010037#include "sysemu/block-backend.h"
Paolo Bonzini022c62c2012-12-17 18:19:49 +010038#include "exec/gdbstub.h"
Paolo Bonzini9c17d612012-12-17 18:20:04 +010039#include "sysemu/dma.h"
Vincent Palatinb3946622017-01-10 11:59:55 +010040#include "sysemu/hw_accel.h"
Paolo Bonzini9c17d612012-12-17 18:20:04 +010041#include "sysemu/kvm.h"
Vincent Palatinb0cb0a62017-01-10 11:59:57 +010042#include "sysemu/hax.h"
Sergio Andres Gomez Del Realc97d6d22017-09-13 04:05:09 -050043#include "sysemu/hvf.h"
Justin Terry (VM)19306802018-01-22 13:07:49 -080044#include "sysemu/whpx.h"
Paolo Bonzini63c91552016-03-15 13:18:37 +010045#include "exec/exec-all.h"
Blue Swirl296af7c2010-03-29 19:23:50 +000046
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010047#include "qemu/thread.h"
Emilio G. Cota30865f32018-10-21 13:30:35 -040048#include "qemu/plugin.h"
Paolo Bonzini9c17d612012-12-17 18:20:04 +010049#include "sysemu/cpus.h"
50#include "sysemu/qtest.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010051#include "qemu/main-loop.h"
Markus Armbruster922a01a2018-02-01 12:18:46 +010052#include "qemu/option.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010053#include "qemu/bitmap.h"
Liu Ping Fancb365642013-09-25 14:20:58 +080054#include "qemu/seqlock.h"
Richard Henderson9c09a252019-03-14 13:06:29 -070055#include "qemu/guest-random.h"
KONRAD Frederic8d4e9142017-02-23 18:29:08 +000056#include "tcg.h"
Alexey Kardashevskiy9cb805f2014-08-20 22:16:33 +100057#include "hw/nmi.h"
Pavel Dovgalyuk8b427042015-09-17 19:24:05 +030058#include "sysemu/replay.h"
Markus Armbruster54d31232019-08-12 07:23:59 +020059#include "sysemu/runstate.h"
Like Xu5cc87672019-05-19 04:54:21 +080060#include "hw/boards.h"
Markus Armbruster650d1032019-08-12 07:23:48 +020061#include "hw/hw.h"
Jan Kiszka0ff0fc12011-06-23 10:15:55 +020062
Jan Kiszka6d9cb732011-02-01 22:15:58 +010063#ifdef CONFIG_LINUX
64
65#include <sys/prctl.h>
66
Marcelo Tosattic0532a72010-10-11 15:31:21 -030067#ifndef PR_MCE_KILL
68#define PR_MCE_KILL 33
69#endif
70
Jan Kiszka6d9cb732011-02-01 22:15:58 +010071#ifndef PR_MCE_KILL_SET
72#define PR_MCE_KILL_SET 1
73#endif
74
75#ifndef PR_MCE_KILL_EARLY
76#define PR_MCE_KILL_EARLY 1
77#endif
78
79#endif /* CONFIG_LINUX */
80
Yury Kotovbd1f7ff2019-09-09 16:13:34 +030081static QemuMutex qemu_global_mutex;
82
Sebastian Tanase27498be2014-07-25 11:56:33 +020083int64_t max_delay;
84int64_t max_advance;
Blue Swirl296af7c2010-03-29 19:23:50 +000085
Jason J. Herne2adcc852015-09-08 13:12:33 -040086/* vcpu throttling controls */
87static QEMUTimer *throttle_timer;
88static unsigned int throttle_percentage;
89
90#define CPU_THROTTLE_PCT_MIN 1
91#define CPU_THROTTLE_PCT_MAX 99
92#define CPU_THROTTLE_TIMESLICE_NS 10000000
93
Tiejun Chen321bc0b2013-08-02 09:43:09 +080094bool cpu_is_stopped(CPUState *cpu)
95{
96 return cpu->stopped || !runstate_is_running();
97}
98
Andreas Färbera98ae1d2013-05-26 23:21:08 +020099static bool cpu_thread_is_idle(CPUState *cpu)
Peter Maydellac873f12012-07-19 16:52:27 +0100100{
Andreas Färberc64ca812012-05-03 02:11:45 +0200101 if (cpu->stop || cpu->queued_work_first) {
Peter Maydellac873f12012-07-19 16:52:27 +0100102 return false;
103 }
Tiejun Chen321bc0b2013-08-02 09:43:09 +0800104 if (cpu_is_stopped(cpu)) {
Peter Maydellac873f12012-07-19 16:52:27 +0100105 return true;
106 }
Andreas Färber8c2e1b02013-08-25 18:53:55 +0200107 if (!cpu->halted || cpu_has_work(cpu) ||
Alexander Graf215e79c2013-04-24 22:24:12 +0200108 kvm_halt_in_kernel()) {
Peter Maydellac873f12012-07-19 16:52:27 +0100109 return false;
110 }
111 return true;
112}
113
114static bool all_cpu_threads_idle(void)
115{
Andreas Färber182735e2013-05-29 22:29:20 +0200116 CPUState *cpu;
Peter Maydellac873f12012-07-19 16:52:27 +0100117
Andreas Färberbdc44642013-06-24 23:50:24 +0200118 CPU_FOREACH(cpu) {
Andreas Färber182735e2013-05-29 22:29:20 +0200119 if (!cpu_thread_is_idle(cpu)) {
Peter Maydellac873f12012-07-19 16:52:27 +0100120 return false;
121 }
122 }
123 return true;
124}
125
Blue Swirl296af7c2010-03-29 19:23:50 +0000126/***********************************************************/
Paolo Bonzini946fb272011-09-12 13:57:37 +0200127/* guest cycle counter */
128
Paolo Bonzinia3270e12013-10-07 17:18:15 +0200129/* Protected by TimersState seqlock */
130
Victor CLEMENT5045e9d92015-05-29 17:14:04 +0200131static bool icount_sleep = true;
Paolo Bonzini946fb272011-09-12 13:57:37 +0200132/* Arbitrarily pick 1MIPS as the minimum allowable speed. */
133#define MAX_ICOUNT_SHIFT 10
Paolo Bonzinia3270e12013-10-07 17:18:15 +0200134
Paolo Bonzini946fb272011-09-12 13:57:37 +0200135typedef struct TimersState {
Liu Ping Fancb365642013-09-25 14:20:58 +0800136 /* Protected by BQL. */
Paolo Bonzini946fb272011-09-12 13:57:37 +0200137 int64_t cpu_ticks_prev;
138 int64_t cpu_ticks_offset;
Liu Ping Fancb365642013-09-25 14:20:58 +0800139
Paolo Bonzini94377112018-08-14 09:57:16 +0200140 /* Protect fields that can be respectively read outside the
141 * BQL, and written from multiple threads.
Liu Ping Fancb365642013-09-25 14:20:58 +0800142 */
143 QemuSeqLock vm_clock_seqlock;
Paolo Bonzini94377112018-08-14 09:57:16 +0200144 QemuSpin vm_clock_lock;
145
146 int16_t cpu_ticks_enabled;
KONRAD Fredericc96778b2014-08-01 01:37:09 +0200147
Paolo Bonzinic1ff0732018-08-14 09:31:58 +0200148 /* Conversion factor from emulated instructions to virtual clock ticks. */
Paolo Bonzini94377112018-08-14 09:57:16 +0200149 int16_t icount_time_shift;
150
KONRAD Fredericc96778b2014-08-01 01:37:09 +0200151 /* Compensate for varying guest execution speed. */
152 int64_t qemu_icount_bias;
Paolo Bonzini94377112018-08-14 09:57:16 +0200153
154 int64_t vm_clock_warp_start;
155 int64_t cpu_clock_offset;
156
KONRAD Fredericc96778b2014-08-01 01:37:09 +0200157 /* Only written by TCG thread */
158 int64_t qemu_icount;
Paolo Bonzini94377112018-08-14 09:57:16 +0200159
Pavel Dovgalyukb39e3f32018-01-11 11:26:10 +0300160 /* for adjusting icount */
Pavel Dovgalyukb39e3f32018-01-11 11:26:10 +0300161 QEMUTimer *icount_rt_timer;
162 QEMUTimer *icount_vm_timer;
163 QEMUTimer *icount_warp_timer;
Paolo Bonzini946fb272011-09-12 13:57:37 +0200164} TimersState;
165
Liu Ping Fand9cd4002013-07-21 08:43:00 +0000166static TimersState timers_state;
KONRAD Frederic8d4e9142017-02-23 18:29:08 +0000167bool mttcg_enabled;
168
169/*
170 * We default to false if we know other options have been enabled
171 * which are currently incompatible with MTTCG. Otherwise when each
172 * guest (target) has been updated to support:
173 * - atomic instructions
174 * - memory ordering primitives (barriers)
175 * they can set the appropriate CONFIG flags in ${target}-softmmu.mak
176 *
177 * Once a guest architecture has been converted to the new primitives
178 * there are two remaining limitations to check.
179 *
180 * - The guest can't be oversized (e.g. 64 bit guest on 32 bit host)
181 * - The host must have a stronger memory order than the guest
182 *
183 * It may be possible in future to support strong guests on weak hosts
184 * but that will require tagging all load/stores in a guest with their
185 * implicit memory order requirements which would likely slow things
186 * down a lot.
187 */
188
189static bool check_tcg_memory_orders_compatible(void)
190{
191#if defined(TCG_GUEST_DEFAULT_MO) && defined(TCG_TARGET_DEFAULT_MO)
192 return (TCG_GUEST_DEFAULT_MO & ~TCG_TARGET_DEFAULT_MO) == 0;
193#else
194 return false;
195#endif
196}
197
198static bool default_mttcg_enabled(void)
199{
Alex Bennée83fd9622017-02-27 17:09:01 +0000200 if (use_icount || TCG_OVERSIZED_GUEST) {
KONRAD Frederic8d4e9142017-02-23 18:29:08 +0000201 return false;
202 } else {
203#ifdef TARGET_SUPPORTS_MTTCG
204 return check_tcg_memory_orders_compatible();
205#else
206 return false;
207#endif
208 }
209}
210
211void qemu_tcg_configure(QemuOpts *opts, Error **errp)
212{
213 const char *t = qemu_opt_get(opts, "thread");
214 if (t) {
215 if (strcmp(t, "multi") == 0) {
216 if (TCG_OVERSIZED_GUEST) {
217 error_setg(errp, "No MTTCG when guest word size > hosts");
Alex Bennée83fd9622017-02-27 17:09:01 +0000218 } else if (use_icount) {
219 error_setg(errp, "No MTTCG when icount is enabled");
KONRAD Frederic8d4e9142017-02-23 18:29:08 +0000220 } else {
Nikunj A Dadhania86953502017-04-10 11:36:55 +0530221#ifndef TARGET_SUPPORTS_MTTCG
Markus Armbruster07656912018-10-17 10:26:28 +0200222 warn_report("Guest not yet converted to MTTCG - "
223 "you may get unexpected results");
Alex Bennéec34c7622017-02-28 14:40:17 +0000224#endif
KONRAD Frederic8d4e9142017-02-23 18:29:08 +0000225 if (!check_tcg_memory_orders_compatible()) {
Markus Armbruster07656912018-10-17 10:26:28 +0200226 warn_report("Guest expects a stronger memory ordering "
227 "than the host provides");
Pranith Kumar8cfef892017-03-25 16:19:23 -0400228 error_printf("This may cause strange/hard to debug errors\n");
KONRAD Frederic8d4e9142017-02-23 18:29:08 +0000229 }
230 mttcg_enabled = true;
231 }
232 } else if (strcmp(t, "single") == 0) {
233 mttcg_enabled = false;
234 } else {
235 error_setg(errp, "Invalid 'thread' setting %s", t);
236 }
237 } else {
238 mttcg_enabled = default_mttcg_enabled();
239 }
240}
Paolo Bonzini946fb272011-09-12 13:57:37 +0200241
Alex Bennéee4cd9652017-03-31 16:09:42 +0100242/* The current number of executed instructions is based on what we
243 * originally budgeted minus the current state of the decrementing
244 * icount counters in extra/u16.low.
245 */
246static int64_t cpu_get_icount_executed(CPUState *cpu)
247{
Richard Henderson5e140192019-03-28 11:54:23 -1000248 return (cpu->icount_budget -
249 (cpu_neg(cpu)->icount_decr.u16.low + cpu->icount_extra));
Alex Bennéee4cd9652017-03-31 16:09:42 +0100250}
251
Alex Bennée512d3c82017-04-05 12:32:37 +0100252/*
253 * Update the global shared timer_state.qemu_icount to take into
254 * account executed instructions. This is done by the TCG vCPU
255 * thread so the main-loop can see time has moved forward.
256 */
Paolo Bonzini9b4e6f42018-09-11 13:15:32 +0200257static void cpu_update_icount_locked(CPUState *cpu)
Alex Bennée512d3c82017-04-05 12:32:37 +0100258{
259 int64_t executed = cpu_get_icount_executed(cpu);
260 cpu->icount_budget -= executed;
261
Emilio G. Cota38adcb62018-09-10 19:27:49 -0400262 atomic_set_i64(&timers_state.qemu_icount,
263 timers_state.qemu_icount + executed);
Paolo Bonzini9b4e6f42018-09-11 13:15:32 +0200264}
265
266/*
267 * Update the global shared timer_state.qemu_icount to take into
268 * account executed instructions. This is done by the TCG vCPU
269 * thread so the main-loop can see time has moved forward.
270 */
271void cpu_update_icount(CPUState *cpu)
272{
273 seqlock_write_lock(&timers_state.vm_clock_seqlock,
274 &timers_state.vm_clock_lock);
275 cpu_update_icount_locked(cpu);
Paolo Bonzini94377112018-08-14 09:57:16 +0200276 seqlock_write_unlock(&timers_state.vm_clock_seqlock,
277 &timers_state.vm_clock_lock);
Alex Bennée512d3c82017-04-05 12:32:37 +0100278}
279
Paolo Bonzinic1ff0732018-08-14 09:31:58 +0200280static int64_t cpu_get_icount_raw_locked(void)
Paolo Bonzini946fb272011-09-12 13:57:37 +0200281{
Andreas Färber4917cf42013-05-27 05:17:50 +0200282 CPUState *cpu = current_cpu;
Paolo Bonzini946fb272011-09-12 13:57:37 +0200283
Alex Bennée243c5f72017-03-30 18:49:22 +0100284 if (cpu && cpu->running) {
Paolo Bonzini414b15c2015-06-24 14:16:26 +0200285 if (!cpu->can_do_io) {
Alistair Francis493d89b2018-02-03 09:43:14 +0100286 error_report("Bad icount read");
Pavel Dovgalyuk2a629142014-12-08 10:53:45 +0300287 exit(1);
Paolo Bonzini946fb272011-09-12 13:57:37 +0200288 }
Alex Bennéee4cd9652017-03-31 16:09:42 +0100289 /* Take into account what has run */
Paolo Bonzini9b4e6f42018-09-11 13:15:32 +0200290 cpu_update_icount_locked(cpu);
Paolo Bonzini946fb272011-09-12 13:57:37 +0200291 }
Emilio G. Cota38adcb62018-09-10 19:27:49 -0400292 /* The read is protected by the seqlock, but needs atomic64 to avoid UB */
293 return atomic_read_i64(&timers_state.qemu_icount);
Paolo Bonzinic1ff0732018-08-14 09:31:58 +0200294}
295
296static int64_t cpu_get_icount_locked(void)
297{
298 int64_t icount = cpu_get_icount_raw_locked();
Emilio G. Cotac97595d2018-09-10 19:27:50 -0400299 return atomic_read_i64(&timers_state.qemu_icount_bias) +
300 cpu_icount_to_ns(icount);
Paolo Bonzinic1ff0732018-08-14 09:31:58 +0200301}
302
303int64_t cpu_get_icount_raw(void)
304{
305 int64_t icount;
306 unsigned start;
307
308 do {
309 start = seqlock_read_begin(&timers_state.vm_clock_seqlock);
310 icount = cpu_get_icount_raw_locked();
311 } while (seqlock_read_retry(&timers_state.vm_clock_seqlock, start));
312
313 return icount;
Pavel Dovgalyuk2a629142014-12-08 10:53:45 +0300314}
315
316/* Return the virtual CPU time, based on the instruction counter. */
Paolo Bonzini17a15f12013-10-03 15:17:25 +0200317int64_t cpu_get_icount(void)
318{
319 int64_t icount;
320 unsigned start;
321
322 do {
323 start = seqlock_read_begin(&timers_state.vm_clock_seqlock);
324 icount = cpu_get_icount_locked();
325 } while (seqlock_read_retry(&timers_state.vm_clock_seqlock, start));
326
327 return icount;
328}
329
KONRAD Frederic3f031312014-08-01 01:37:15 +0200330int64_t cpu_icount_to_ns(int64_t icount)
331{
Paolo Bonzinic1ff0732018-08-14 09:31:58 +0200332 return icount << atomic_read(&timers_state.icount_time_shift);
KONRAD Frederic3f031312014-08-01 01:37:15 +0200333}
334
Paolo Bonzinif2a4ad62018-08-18 09:36:16 +0200335static int64_t cpu_get_ticks_locked(void)
336{
337 int64_t ticks = timers_state.cpu_ticks_offset;
338 if (timers_state.cpu_ticks_enabled) {
339 ticks += cpu_get_host_ticks();
340 }
341
342 if (timers_state.cpu_ticks_prev > ticks) {
343 /* Non increasing ticks may happen if the host uses software suspend. */
344 timers_state.cpu_ticks_offset += timers_state.cpu_ticks_prev - ticks;
345 ticks = timers_state.cpu_ticks_prev;
346 }
347
348 timers_state.cpu_ticks_prev = ticks;
349 return ticks;
350}
351
Cao jind90f3cc2016-07-29 19:05:38 +0800352/* return the time elapsed in VM between vm_start and vm_stop. Unless
353 * icount is active, cpu_get_ticks() uses units of the host CPU cycle
354 * counter.
Cao jind90f3cc2016-07-29 19:05:38 +0800355 */
Paolo Bonzini946fb272011-09-12 13:57:37 +0200356int64_t cpu_get_ticks(void)
357{
Paolo Bonzini5f3e3102013-10-28 17:32:18 +0100358 int64_t ticks;
359
Paolo Bonzini946fb272011-09-12 13:57:37 +0200360 if (use_icount) {
361 return cpu_get_icount();
362 }
Paolo Bonzini5f3e3102013-10-28 17:32:18 +0100363
Paolo Bonzinif2a4ad62018-08-18 09:36:16 +0200364 qemu_spin_lock(&timers_state.vm_clock_lock);
365 ticks = cpu_get_ticks_locked();
366 qemu_spin_unlock(&timers_state.vm_clock_lock);
Paolo Bonzini5f3e3102013-10-28 17:32:18 +0100367 return ticks;
Paolo Bonzini946fb272011-09-12 13:57:37 +0200368}
369
Liu Ping Fancb365642013-09-25 14:20:58 +0800370static int64_t cpu_get_clock_locked(void)
371{
Cao jin1d45cea2016-07-29 19:05:37 +0800372 int64_t time;
Liu Ping Fancb365642013-09-25 14:20:58 +0800373
Cao jin1d45cea2016-07-29 19:05:37 +0800374 time = timers_state.cpu_clock_offset;
Paolo Bonzini5f3e3102013-10-28 17:32:18 +0100375 if (timers_state.cpu_ticks_enabled) {
Cao jin1d45cea2016-07-29 19:05:37 +0800376 time += get_clock();
Liu Ping Fancb365642013-09-25 14:20:58 +0800377 }
378
Cao jin1d45cea2016-07-29 19:05:37 +0800379 return time;
Liu Ping Fancb365642013-09-25 14:20:58 +0800380}
381
Cao jind90f3cc2016-07-29 19:05:38 +0800382/* Return the monotonic time elapsed in VM, i.e.,
Peter Maydell8212ff82016-09-15 10:24:22 +0100383 * the time between vm_start and vm_stop
384 */
Paolo Bonzini946fb272011-09-12 13:57:37 +0200385int64_t cpu_get_clock(void)
386{
387 int64_t ti;
Liu Ping Fancb365642013-09-25 14:20:58 +0800388 unsigned start;
389
390 do {
391 start = seqlock_read_begin(&timers_state.vm_clock_seqlock);
392 ti = cpu_get_clock_locked();
393 } while (seqlock_read_retry(&timers_state.vm_clock_seqlock, start));
394
395 return ti;
Paolo Bonzini946fb272011-09-12 13:57:37 +0200396}
397
Liu Ping Fancb365642013-09-25 14:20:58 +0800398/* enable cpu_get_ticks()
Cao jin3224e872016-07-08 18:31:37 +0800399 * Caller must hold BQL which serves as mutex for vm_clock_seqlock.
Liu Ping Fancb365642013-09-25 14:20:58 +0800400 */
Paolo Bonzini946fb272011-09-12 13:57:37 +0200401void cpu_enable_ticks(void)
402{
Paolo Bonzini94377112018-08-14 09:57:16 +0200403 seqlock_write_lock(&timers_state.vm_clock_seqlock,
404 &timers_state.vm_clock_lock);
Paolo Bonzini946fb272011-09-12 13:57:37 +0200405 if (!timers_state.cpu_ticks_enabled) {
Christopher Covington4a7428c2015-09-25 10:42:21 -0400406 timers_state.cpu_ticks_offset -= cpu_get_host_ticks();
Paolo Bonzini946fb272011-09-12 13:57:37 +0200407 timers_state.cpu_clock_offset -= get_clock();
408 timers_state.cpu_ticks_enabled = 1;
409 }
Paolo Bonzini94377112018-08-14 09:57:16 +0200410 seqlock_write_unlock(&timers_state.vm_clock_seqlock,
411 &timers_state.vm_clock_lock);
Paolo Bonzini946fb272011-09-12 13:57:37 +0200412}
413
414/* disable cpu_get_ticks() : the clock is stopped. You must not call
Liu Ping Fancb365642013-09-25 14:20:58 +0800415 * cpu_get_ticks() after that.
Cao jin3224e872016-07-08 18:31:37 +0800416 * Caller must hold BQL which serves as mutex for vm_clock_seqlock.
Liu Ping Fancb365642013-09-25 14:20:58 +0800417 */
Paolo Bonzini946fb272011-09-12 13:57:37 +0200418void cpu_disable_ticks(void)
419{
Paolo Bonzini94377112018-08-14 09:57:16 +0200420 seqlock_write_lock(&timers_state.vm_clock_seqlock,
421 &timers_state.vm_clock_lock);
Paolo Bonzini946fb272011-09-12 13:57:37 +0200422 if (timers_state.cpu_ticks_enabled) {
Christopher Covington4a7428c2015-09-25 10:42:21 -0400423 timers_state.cpu_ticks_offset += cpu_get_host_ticks();
Liu Ping Fancb365642013-09-25 14:20:58 +0800424 timers_state.cpu_clock_offset = cpu_get_clock_locked();
Paolo Bonzini946fb272011-09-12 13:57:37 +0200425 timers_state.cpu_ticks_enabled = 0;
426 }
Paolo Bonzini94377112018-08-14 09:57:16 +0200427 seqlock_write_unlock(&timers_state.vm_clock_seqlock,
428 &timers_state.vm_clock_lock);
Paolo Bonzini946fb272011-09-12 13:57:37 +0200429}
430
431/* Correlation between real and virtual time is always going to be
432 fairly approximate, so ignore small variation.
433 When the guest is idle real and virtual time will be aligned in
434 the IO wait loop. */
Rutuja Shah73bcb242016-03-21 21:32:30 +0530435#define ICOUNT_WOBBLE (NANOSECONDS_PER_SECOND / 10)
Paolo Bonzini946fb272011-09-12 13:57:37 +0200436
437static void icount_adjust(void)
438{
439 int64_t cur_time;
440 int64_t cur_icount;
441 int64_t delta;
Paolo Bonzinia3270e12013-10-07 17:18:15 +0200442
443 /* Protected by TimersState mutex. */
Paolo Bonzini946fb272011-09-12 13:57:37 +0200444 static int64_t last_delta;
Paolo Bonzini468cc7c2013-10-07 17:21:51 +0200445
Paolo Bonzini946fb272011-09-12 13:57:37 +0200446 /* If the VM is not running, then do nothing. */
447 if (!runstate_is_running()) {
448 return;
449 }
Paolo Bonzini468cc7c2013-10-07 17:21:51 +0200450
Paolo Bonzini94377112018-08-14 09:57:16 +0200451 seqlock_write_lock(&timers_state.vm_clock_seqlock,
452 &timers_state.vm_clock_lock);
Paolo Bonzini17a15f12013-10-03 15:17:25 +0200453 cur_time = cpu_get_clock_locked();
454 cur_icount = cpu_get_icount_locked();
Paolo Bonzini468cc7c2013-10-07 17:21:51 +0200455
Paolo Bonzini946fb272011-09-12 13:57:37 +0200456 delta = cur_icount - cur_time;
457 /* FIXME: This is a very crude algorithm, somewhat prone to oscillation. */
458 if (delta > 0
459 && last_delta + ICOUNT_WOBBLE < delta * 2
Paolo Bonzinic1ff0732018-08-14 09:31:58 +0200460 && timers_state.icount_time_shift > 0) {
Paolo Bonzini946fb272011-09-12 13:57:37 +0200461 /* The guest is getting too far ahead. Slow time down. */
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 if (delta < 0
466 && last_delta - ICOUNT_WOBBLE > delta * 2
Paolo Bonzinic1ff0732018-08-14 09:31:58 +0200467 && timers_state.icount_time_shift < MAX_ICOUNT_SHIFT) {
Paolo Bonzini946fb272011-09-12 13:57:37 +0200468 /* The guest is getting too far behind. Speed time up. */
Paolo Bonzinic1ff0732018-08-14 09:31:58 +0200469 atomic_set(&timers_state.icount_time_shift,
470 timers_state.icount_time_shift + 1);
Paolo Bonzini946fb272011-09-12 13:57:37 +0200471 }
472 last_delta = delta;
Emilio G. Cotac97595d2018-09-10 19:27:50 -0400473 atomic_set_i64(&timers_state.qemu_icount_bias,
474 cur_icount - (timers_state.qemu_icount
475 << timers_state.icount_time_shift));
Paolo Bonzini94377112018-08-14 09:57:16 +0200476 seqlock_write_unlock(&timers_state.vm_clock_seqlock,
477 &timers_state.vm_clock_lock);
Paolo Bonzini946fb272011-09-12 13:57:37 +0200478}
479
480static void icount_adjust_rt(void *opaque)
481{
Pavel Dovgalyukb39e3f32018-01-11 11:26:10 +0300482 timer_mod(timers_state.icount_rt_timer,
Pavel Dovgalyuk1979b902015-01-12 15:00:43 +0300483 qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL_RT) + 1000);
Paolo Bonzini946fb272011-09-12 13:57:37 +0200484 icount_adjust();
485}
486
487static void icount_adjust_vm(void *opaque)
488{
Pavel Dovgalyukb39e3f32018-01-11 11:26:10 +0300489 timer_mod(timers_state.icount_vm_timer,
Alex Bligh40daca52013-08-21 16:03:02 +0100490 qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) +
Rutuja Shah73bcb242016-03-21 21:32:30 +0530491 NANOSECONDS_PER_SECOND / 10);
Paolo Bonzini946fb272011-09-12 13:57:37 +0200492 icount_adjust();
493}
494
495static int64_t qemu_icount_round(int64_t count)
496{
Paolo Bonzinic1ff0732018-08-14 09:31:58 +0200497 int shift = atomic_read(&timers_state.icount_time_shift);
498 return (count + (1 << shift) - 1) >> shift;
Paolo Bonzini946fb272011-09-12 13:57:37 +0200499}
500
Pavel Dovgalyukefab87c2015-09-17 19:24:39 +0300501static void icount_warp_rt(void)
Paolo Bonzini946fb272011-09-12 13:57:37 +0200502{
Alex Bennéeccffff42016-04-04 15:35:48 +0100503 unsigned seq;
504 int64_t warp_start;
505
Paolo Bonzini17a15f12013-10-03 15:17:25 +0200506 /* The icount_warp_timer is rescheduled soon after vm_clock_warp_start
507 * changes from -1 to another value, so the race here is okay.
508 */
Alex Bennéeccffff42016-04-04 15:35:48 +0100509 do {
510 seq = seqlock_read_begin(&timers_state.vm_clock_seqlock);
Pavel Dovgalyukb39e3f32018-01-11 11:26:10 +0300511 warp_start = timers_state.vm_clock_warp_start;
Alex Bennéeccffff42016-04-04 15:35:48 +0100512 } while (seqlock_read_retry(&timers_state.vm_clock_seqlock, seq));
513
514 if (warp_start == -1) {
Paolo Bonzini946fb272011-09-12 13:57:37 +0200515 return;
516 }
517
Paolo Bonzini94377112018-08-14 09:57:16 +0200518 seqlock_write_lock(&timers_state.vm_clock_seqlock,
519 &timers_state.vm_clock_lock);
Paolo Bonzini946fb272011-09-12 13:57:37 +0200520 if (runstate_is_running()) {
Paolo Bonzini74c0b812018-10-08 13:24:14 +0200521 int64_t clock = REPLAY_CLOCK_LOCKED(REPLAY_CLOCK_VIRTUAL_RT,
522 cpu_get_clock_locked());
Paolo Bonzini8ed961d2013-10-07 17:26:07 +0200523 int64_t warp_delta;
524
Pavel Dovgalyukb39e3f32018-01-11 11:26:10 +0300525 warp_delta = clock - timers_state.vm_clock_warp_start;
Paolo Bonzini8ed961d2013-10-07 17:26:07 +0200526 if (use_icount == 2) {
Paolo Bonzini946fb272011-09-12 13:57:37 +0200527 /*
Alex Bligh40daca52013-08-21 16:03:02 +0100528 * In adaptive mode, do not let QEMU_CLOCK_VIRTUAL run too
Paolo Bonzini946fb272011-09-12 13:57:37 +0200529 * far ahead of real time.
530 */
Paolo Bonzini17a15f12013-10-03 15:17:25 +0200531 int64_t cur_icount = cpu_get_icount_locked();
Pavel Dovgalyukbf2a7dd2014-11-26 13:40:55 +0300532 int64_t delta = clock - cur_icount;
Paolo Bonzini8ed961d2013-10-07 17:26:07 +0200533 warp_delta = MIN(warp_delta, delta);
Paolo Bonzini946fb272011-09-12 13:57:37 +0200534 }
Emilio G. Cotac97595d2018-09-10 19:27:50 -0400535 atomic_set_i64(&timers_state.qemu_icount_bias,
536 timers_state.qemu_icount_bias + warp_delta);
Paolo Bonzini946fb272011-09-12 13:57:37 +0200537 }
Pavel Dovgalyukb39e3f32018-01-11 11:26:10 +0300538 timers_state.vm_clock_warp_start = -1;
Paolo Bonzini94377112018-08-14 09:57:16 +0200539 seqlock_write_unlock(&timers_state.vm_clock_seqlock,
540 &timers_state.vm_clock_lock);
Paolo Bonzini8ed961d2013-10-07 17:26:07 +0200541
542 if (qemu_clock_expired(QEMU_CLOCK_VIRTUAL)) {
543 qemu_clock_notify(QEMU_CLOCK_VIRTUAL);
544 }
Paolo Bonzini946fb272011-09-12 13:57:37 +0200545}
546
Pavel Dovgalyuke76d1792016-03-10 14:56:09 +0300547static void icount_timer_cb(void *opaque)
Pavel Dovgalyukefab87c2015-09-17 19:24:39 +0300548{
Pavel Dovgalyuke76d1792016-03-10 14:56:09 +0300549 /* No need for a checkpoint because the timer already synchronizes
550 * with CHECKPOINT_CLOCK_VIRTUAL_RT.
551 */
552 icount_warp_rt();
Pavel Dovgalyukefab87c2015-09-17 19:24:39 +0300553}
554
Paolo Bonzini8156be52012-03-28 15:42:04 +0200555void qtest_clock_warp(int64_t dest)
556{
Alex Bligh40daca52013-08-21 16:03:02 +0100557 int64_t clock = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
Fam Zhengefef88b2015-01-19 17:51:43 +0800558 AioContext *aio_context;
Paolo Bonzini8156be52012-03-28 15:42:04 +0200559 assert(qtest_enabled());
Fam Zhengefef88b2015-01-19 17:51:43 +0800560 aio_context = qemu_get_aio_context();
Paolo Bonzini8156be52012-03-28 15:42:04 +0200561 while (clock < dest) {
Pavel Dovgalyukdcb15782019-07-25 11:44:26 +0300562 int64_t deadline = qemu_clock_deadline_ns_all(QEMU_CLOCK_VIRTUAL,
563 QEMU_TIMER_ATTR_ALL);
Sergey Fedorovc9299e22014-06-10 13:10:28 +0400564 int64_t warp = qemu_soonest_timeout(dest - clock, deadline);
Fam Zhengefef88b2015-01-19 17:51:43 +0800565
Paolo Bonzini94377112018-08-14 09:57:16 +0200566 seqlock_write_lock(&timers_state.vm_clock_seqlock,
567 &timers_state.vm_clock_lock);
Emilio G. Cotac97595d2018-09-10 19:27:50 -0400568 atomic_set_i64(&timers_state.qemu_icount_bias,
569 timers_state.qemu_icount_bias + warp);
Paolo Bonzini94377112018-08-14 09:57:16 +0200570 seqlock_write_unlock(&timers_state.vm_clock_seqlock,
571 &timers_state.vm_clock_lock);
Paolo Bonzini17a15f12013-10-03 15:17:25 +0200572
Alex Bligh40daca52013-08-21 16:03:02 +0100573 qemu_clock_run_timers(QEMU_CLOCK_VIRTUAL);
Fam Zhengefef88b2015-01-19 17:51:43 +0800574 timerlist_run_timers(aio_context->tlg.tl[QEMU_CLOCK_VIRTUAL]);
Alex Bligh40daca52013-08-21 16:03:02 +0100575 clock = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
Paolo Bonzini8156be52012-03-28 15:42:04 +0200576 }
Alex Bligh40daca52013-08-21 16:03:02 +0100577 qemu_clock_notify(QEMU_CLOCK_VIRTUAL);
Paolo Bonzini8156be52012-03-28 15:42:04 +0200578}
579
Pavel Dovgalyuke76d1792016-03-10 14:56:09 +0300580void qemu_start_warp_timer(void)
Paolo Bonzini946fb272011-09-12 13:57:37 +0200581{
Paolo Bonzinice78d182013-10-07 17:30:02 +0200582 int64_t clock;
Paolo Bonzini946fb272011-09-12 13:57:37 +0200583 int64_t deadline;
584
Pavel Dovgalyuke76d1792016-03-10 14:56:09 +0300585 if (!use_icount) {
Paolo Bonzini946fb272011-09-12 13:57:37 +0200586 return;
587 }
588
Pavel Dovgalyuk8bd7f712015-09-17 19:24:44 +0300589 /* Nothing to do if the VM is stopped: QEMU_CLOCK_VIRTUAL timers
590 * do not fire, so computing the deadline does not make sense.
591 */
592 if (!runstate_is_running()) {
593 return;
594 }
595
Pavel Dovgalyuk0c081852018-09-12 11:19:45 +0300596 if (replay_mode != REPLAY_MODE_PLAY) {
597 if (!all_cpu_threads_idle()) {
598 return;
599 }
Pavel Dovgalyuk8bd7f712015-09-17 19:24:44 +0300600
Pavel Dovgalyuk0c081852018-09-12 11:19:45 +0300601 if (qtest_enabled()) {
602 /* When testing, qtest commands advance icount. */
603 return;
604 }
Paolo Bonzini946fb272011-09-12 13:57:37 +0200605
Pavel Dovgalyuk0c081852018-09-12 11:19:45 +0300606 replay_checkpoint(CHECKPOINT_CLOCK_WARP_START);
607 } else {
608 /* warp clock deterministically in record/replay mode */
609 if (!replay_checkpoint(CHECKPOINT_CLOCK_WARP_START)) {
610 /* vCPU is sleeping and warp can't be started.
611 It is probably a race condition: notification sent
612 to vCPU was processed in advance and vCPU went to sleep.
613 Therefore we have to wake it up for doing someting. */
614 if (replay_has_checkpoint()) {
615 qemu_clock_notify(QEMU_CLOCK_VIRTUAL);
616 }
617 return;
618 }
Paolo Bonzini8156be52012-03-28 15:42:04 +0200619 }
620
Alex Blighac70aaf2013-08-21 16:02:57 +0100621 /* We want to use the earliest deadline from ALL vm_clocks */
Pavel Dovgalyukbf2a7dd2014-11-26 13:40:55 +0300622 clock = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL_RT);
Pavel Dovgalyukdcb15782019-07-25 11:44:26 +0300623 deadline = qemu_clock_deadline_ns_all(QEMU_CLOCK_VIRTUAL,
624 ~QEMU_TIMER_ATTR_EXTERNAL);
Paolo Bonzinice78d182013-10-07 17:30:02 +0200625 if (deadline < 0) {
Victor CLEMENTd7a0f712015-05-29 17:14:06 +0200626 static bool notified;
627 if (!icount_sleep && !notified) {
Alistair Francis3dc6f862017-07-12 06:57:41 -0700628 warn_report("icount sleep disabled and no active timers");
Victor CLEMENTd7a0f712015-05-29 17:14:06 +0200629 notified = true;
630 }
Paolo Bonzinice78d182013-10-07 17:30:02 +0200631 return;
Alex Blighac70aaf2013-08-21 16:02:57 +0100632 }
633
Paolo Bonzini946fb272011-09-12 13:57:37 +0200634 if (deadline > 0) {
635 /*
Alex Bligh40daca52013-08-21 16:03:02 +0100636 * Ensure QEMU_CLOCK_VIRTUAL proceeds even when the virtual CPU goes to
Paolo Bonzini946fb272011-09-12 13:57:37 +0200637 * sleep. Otherwise, the CPU might be waiting for a future timer
638 * interrupt to wake it up, but the interrupt never comes because
639 * the vCPU isn't running any insns and thus doesn't advance the
Alex Bligh40daca52013-08-21 16:03:02 +0100640 * QEMU_CLOCK_VIRTUAL.
Paolo Bonzini946fb272011-09-12 13:57:37 +0200641 */
Victor CLEMENT5045e9d92015-05-29 17:14:04 +0200642 if (!icount_sleep) {
643 /*
644 * We never let VCPUs sleep in no sleep icount mode.
645 * If there is a pending QEMU_CLOCK_VIRTUAL timer we just advance
646 * to the next QEMU_CLOCK_VIRTUAL event and notify it.
647 * It is useful when we want a deterministic execution time,
648 * isolated from host latencies.
649 */
Paolo Bonzini94377112018-08-14 09:57:16 +0200650 seqlock_write_lock(&timers_state.vm_clock_seqlock,
651 &timers_state.vm_clock_lock);
Emilio G. Cotac97595d2018-09-10 19:27:50 -0400652 atomic_set_i64(&timers_state.qemu_icount_bias,
653 timers_state.qemu_icount_bias + deadline);
Paolo Bonzini94377112018-08-14 09:57:16 +0200654 seqlock_write_unlock(&timers_state.vm_clock_seqlock,
655 &timers_state.vm_clock_lock);
Victor CLEMENT5045e9d92015-05-29 17:14:04 +0200656 qemu_clock_notify(QEMU_CLOCK_VIRTUAL);
657 } else {
658 /*
659 * We do stop VCPUs and only advance QEMU_CLOCK_VIRTUAL after some
660 * "real" time, (related to the time left until the next event) has
661 * passed. The QEMU_CLOCK_VIRTUAL_RT clock will do this.
662 * This avoids that the warps are visible externally; for example,
663 * you will not be sending network packets continuously instead of
664 * every 100ms.
665 */
Paolo Bonzini94377112018-08-14 09:57:16 +0200666 seqlock_write_lock(&timers_state.vm_clock_seqlock,
667 &timers_state.vm_clock_lock);
Pavel Dovgalyukb39e3f32018-01-11 11:26:10 +0300668 if (timers_state.vm_clock_warp_start == -1
669 || timers_state.vm_clock_warp_start > clock) {
670 timers_state.vm_clock_warp_start = clock;
Victor CLEMENT5045e9d92015-05-29 17:14:04 +0200671 }
Paolo Bonzini94377112018-08-14 09:57:16 +0200672 seqlock_write_unlock(&timers_state.vm_clock_seqlock,
673 &timers_state.vm_clock_lock);
Pavel Dovgalyukb39e3f32018-01-11 11:26:10 +0300674 timer_mod_anticipate(timers_state.icount_warp_timer,
675 clock + deadline);
Paolo Bonzinice78d182013-10-07 17:30:02 +0200676 }
Alex Blighac70aaf2013-08-21 16:02:57 +0100677 } else if (deadline == 0) {
Alex Bligh40daca52013-08-21 16:03:02 +0100678 qemu_clock_notify(QEMU_CLOCK_VIRTUAL);
Paolo Bonzini946fb272011-09-12 13:57:37 +0200679 }
680}
681
Pavel Dovgalyuke76d1792016-03-10 14:56:09 +0300682static void qemu_account_warp_timer(void)
683{
684 if (!use_icount || !icount_sleep) {
685 return;
686 }
687
688 /* Nothing to do if the VM is stopped: QEMU_CLOCK_VIRTUAL timers
689 * do not fire, so computing the deadline does not make sense.
690 */
691 if (!runstate_is_running()) {
692 return;
693 }
694
695 /* warp clock deterministically in record/replay mode */
696 if (!replay_checkpoint(CHECKPOINT_CLOCK_WARP_ACCOUNT)) {
697 return;
698 }
699
Pavel Dovgalyukb39e3f32018-01-11 11:26:10 +0300700 timer_del(timers_state.icount_warp_timer);
Pavel Dovgalyuke76d1792016-03-10 14:56:09 +0300701 icount_warp_rt();
702}
703
KONRAD Fredericd09eae32014-08-01 01:37:10 +0200704static bool icount_state_needed(void *opaque)
705{
706 return use_icount;
707}
708
Pavel Dovgalyukb39e3f32018-01-11 11:26:10 +0300709static bool warp_timer_state_needed(void *opaque)
710{
711 TimersState *s = opaque;
712 return s->icount_warp_timer != NULL;
713}
714
715static bool adjust_timers_state_needed(void *opaque)
716{
717 TimersState *s = opaque;
718 return s->icount_rt_timer != NULL;
719}
720
721/*
722 * Subsection for warp timer migration is optional, because may not be created
723 */
724static const VMStateDescription icount_vmstate_warp_timer = {
725 .name = "timer/icount/warp_timer",
726 .version_id = 1,
727 .minimum_version_id = 1,
728 .needed = warp_timer_state_needed,
729 .fields = (VMStateField[]) {
730 VMSTATE_INT64(vm_clock_warp_start, TimersState),
731 VMSTATE_TIMER_PTR(icount_warp_timer, TimersState),
732 VMSTATE_END_OF_LIST()
733 }
734};
735
736static const VMStateDescription icount_vmstate_adjust_timers = {
737 .name = "timer/icount/timers",
738 .version_id = 1,
739 .minimum_version_id = 1,
740 .needed = adjust_timers_state_needed,
741 .fields = (VMStateField[]) {
742 VMSTATE_TIMER_PTR(icount_rt_timer, TimersState),
743 VMSTATE_TIMER_PTR(icount_vm_timer, TimersState),
744 VMSTATE_END_OF_LIST()
745 }
746};
747
KONRAD Fredericd09eae32014-08-01 01:37:10 +0200748/*
749 * This is a subsection for icount migration.
750 */
751static const VMStateDescription icount_vmstate_timers = {
752 .name = "timer/icount",
753 .version_id = 1,
754 .minimum_version_id = 1,
Juan Quintela5cd8cad2014-09-23 14:09:54 +0200755 .needed = icount_state_needed,
KONRAD Fredericd09eae32014-08-01 01:37:10 +0200756 .fields = (VMStateField[]) {
757 VMSTATE_INT64(qemu_icount_bias, TimersState),
758 VMSTATE_INT64(qemu_icount, TimersState),
759 VMSTATE_END_OF_LIST()
Pavel Dovgalyukb39e3f32018-01-11 11:26:10 +0300760 },
761 .subsections = (const VMStateDescription*[]) {
762 &icount_vmstate_warp_timer,
763 &icount_vmstate_adjust_timers,
764 NULL
KONRAD Fredericd09eae32014-08-01 01:37:10 +0200765 }
766};
767
Paolo Bonzini946fb272011-09-12 13:57:37 +0200768static const VMStateDescription vmstate_timers = {
769 .name = "timer",
770 .version_id = 2,
771 .minimum_version_id = 1,
Juan Quintela35d08452014-04-16 16:01:33 +0200772 .fields = (VMStateField[]) {
Paolo Bonzini946fb272011-09-12 13:57:37 +0200773 VMSTATE_INT64(cpu_ticks_offset, TimersState),
Paolo Bonzinic1ff0732018-08-14 09:31:58 +0200774 VMSTATE_UNUSED(8),
Paolo Bonzini946fb272011-09-12 13:57:37 +0200775 VMSTATE_INT64_V(cpu_clock_offset, TimersState, 2),
776 VMSTATE_END_OF_LIST()
KONRAD Fredericd09eae32014-08-01 01:37:10 +0200777 },
Juan Quintela5cd8cad2014-09-23 14:09:54 +0200778 .subsections = (const VMStateDescription*[]) {
779 &icount_vmstate_timers,
780 NULL
Paolo Bonzini946fb272011-09-12 13:57:37 +0200781 }
782};
783
Paolo Bonzini14e6fe12016-10-31 10:36:08 +0100784static void cpu_throttle_thread(CPUState *cpu, run_on_cpu_data opaque)
Jason J. Herne2adcc852015-09-08 13:12:33 -0400785{
Jason J. Herne2adcc852015-09-08 13:12:33 -0400786 double pct;
787 double throttle_ratio;
Yury Kotovbd1f7ff2019-09-09 16:13:34 +0300788 int64_t sleeptime_ns, endtime_ns;
Jason J. Herne2adcc852015-09-08 13:12:33 -0400789
790 if (!cpu_throttle_get_percentage()) {
791 return;
792 }
793
794 pct = (double)cpu_throttle_get_percentage()/100;
795 throttle_ratio = pct / (1 - pct);
Yury Kotovbd1f7ff2019-09-09 16:13:34 +0300796 /* Add 1ns to fix double's rounding error (like 0.9999999...) */
797 sleeptime_ns = (int64_t)(throttle_ratio * CPU_THROTTLE_TIMESLICE_NS + 1);
798 endtime_ns = qemu_clock_get_ns(QEMU_CLOCK_REALTIME) + sleeptime_ns;
799 while (sleeptime_ns > 0 && !cpu->stop) {
800 if (sleeptime_ns > SCALE_MS) {
801 qemu_cond_timedwait(cpu->halt_cond, &qemu_global_mutex,
802 sleeptime_ns / SCALE_MS);
803 } else {
804 qemu_mutex_unlock_iothread();
805 g_usleep(sleeptime_ns / SCALE_US);
806 qemu_mutex_lock_iothread();
807 }
808 sleeptime_ns = endtime_ns - qemu_clock_get_ns(QEMU_CLOCK_REALTIME);
809 }
Felipe Franciosi90bb0c02017-05-19 22:29:50 +0100810 atomic_set(&cpu->throttle_thread_scheduled, 0);
Jason J. Herne2adcc852015-09-08 13:12:33 -0400811}
812
813static void cpu_throttle_timer_tick(void *opaque)
814{
815 CPUState *cpu;
816 double pct;
817
818 /* Stop the timer if needed */
819 if (!cpu_throttle_get_percentage()) {
820 return;
821 }
822 CPU_FOREACH(cpu) {
823 if (!atomic_xchg(&cpu->throttle_thread_scheduled, 1)) {
Paolo Bonzini14e6fe12016-10-31 10:36:08 +0100824 async_run_on_cpu(cpu, cpu_throttle_thread,
825 RUN_ON_CPU_NULL);
Jason J. Herne2adcc852015-09-08 13:12:33 -0400826 }
827 }
828
829 pct = (double)cpu_throttle_get_percentage()/100;
830 timer_mod(throttle_timer, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL_RT) +
831 CPU_THROTTLE_TIMESLICE_NS / (1-pct));
832}
833
834void cpu_throttle_set(int new_throttle_pct)
835{
836 /* Ensure throttle percentage is within valid range */
837 new_throttle_pct = MIN(new_throttle_pct, CPU_THROTTLE_PCT_MAX);
838 new_throttle_pct = MAX(new_throttle_pct, CPU_THROTTLE_PCT_MIN);
839
840 atomic_set(&throttle_percentage, new_throttle_pct);
841
842 timer_mod(throttle_timer, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL_RT) +
843 CPU_THROTTLE_TIMESLICE_NS);
844}
845
846void cpu_throttle_stop(void)
847{
848 atomic_set(&throttle_percentage, 0);
849}
850
851bool cpu_throttle_active(void)
852{
853 return (cpu_throttle_get_percentage() != 0);
854}
855
856int cpu_throttle_get_percentage(void)
857{
858 return atomic_read(&throttle_percentage);
859}
860
Pavel Dovgalyuk4603ea02014-09-01 09:34:49 +0400861void cpu_ticks_init(void)
862{
Emilio G. Cotaccdb3c12016-06-08 14:55:20 -0400863 seqlock_init(&timers_state.vm_clock_seqlock);
Emilio G. Cota87a09cd2018-09-03 13:18:29 -0400864 qemu_spin_init(&timers_state.vm_clock_lock);
Pavel Dovgalyuk4603ea02014-09-01 09:34:49 +0400865 vmstate_register(NULL, 0, &vmstate_timers, &timers_state);
Jason J. Herne2adcc852015-09-08 13:12:33 -0400866 throttle_timer = timer_new_ns(QEMU_CLOCK_VIRTUAL_RT,
867 cpu_throttle_timer_tick, NULL);
Pavel Dovgalyuk4603ea02014-09-01 09:34:49 +0400868}
869
Sebastian Tanase1ad95802014-07-25 11:56:28 +0200870void configure_icount(QemuOpts *opts, Error **errp)
Paolo Bonzini946fb272011-09-12 13:57:37 +0200871{
Sebastian Tanase1ad95802014-07-25 11:56:28 +0200872 const char *option;
Sebastian Tanasea8bfac32014-07-25 11:56:29 +0200873 char *rem_str = NULL;
Sebastian Tanase1ad95802014-07-25 11:56:28 +0200874
Sebastian Tanase1ad95802014-07-25 11:56:28 +0200875 option = qemu_opt_get(opts, "shift");
Paolo Bonzini946fb272011-09-12 13:57:37 +0200876 if (!option) {
Sebastian Tanasea8bfac32014-07-25 11:56:29 +0200877 if (qemu_opt_get(opts, "align") != NULL) {
878 error_setg(errp, "Please specify shift option when using align");
879 }
Paolo Bonzini946fb272011-09-12 13:57:37 +0200880 return;
881 }
Victor CLEMENTf1f4b572015-05-29 17:14:05 +0200882
883 icount_sleep = qemu_opt_get_bool(opts, "sleep", true);
Victor CLEMENT5045e9d92015-05-29 17:14:04 +0200884 if (icount_sleep) {
Pavel Dovgalyukb39e3f32018-01-11 11:26:10 +0300885 timers_state.icount_warp_timer = timer_new_ns(QEMU_CLOCK_VIRTUAL_RT,
Pavel Dovgalyuke76d1792016-03-10 14:56:09 +0300886 icount_timer_cb, NULL);
Victor CLEMENT5045e9d92015-05-29 17:14:04 +0200887 }
Victor CLEMENTf1f4b572015-05-29 17:14:05 +0200888
Sebastian Tanasea8bfac32014-07-25 11:56:29 +0200889 icount_align_option = qemu_opt_get_bool(opts, "align", false);
Victor CLEMENTf1f4b572015-05-29 17:14:05 +0200890
891 if (icount_align_option && !icount_sleep) {
Pranith Kumar778d9f92016-02-26 10:16:51 -0500892 error_setg(errp, "align=on and sleep=off are incompatible");
Victor CLEMENTf1f4b572015-05-29 17:14:05 +0200893 }
Paolo Bonzini946fb272011-09-12 13:57:37 +0200894 if (strcmp(option, "auto") != 0) {
Sebastian Tanasea8bfac32014-07-25 11:56:29 +0200895 errno = 0;
Paolo Bonzinic1ff0732018-08-14 09:31:58 +0200896 timers_state.icount_time_shift = strtol(option, &rem_str, 0);
Sebastian Tanasea8bfac32014-07-25 11:56:29 +0200897 if (errno != 0 || *rem_str != '\0' || !strlen(option)) {
898 error_setg(errp, "icount: Invalid shift value");
899 }
Paolo Bonzini946fb272011-09-12 13:57:37 +0200900 use_icount = 1;
901 return;
Sebastian Tanasea8bfac32014-07-25 11:56:29 +0200902 } else if (icount_align_option) {
903 error_setg(errp, "shift=auto and align=on are incompatible");
Victor CLEMENTf1f4b572015-05-29 17:14:05 +0200904 } else if (!icount_sleep) {
Pranith Kumar778d9f92016-02-26 10:16:51 -0500905 error_setg(errp, "shift=auto and sleep=off are incompatible");
Paolo Bonzini946fb272011-09-12 13:57:37 +0200906 }
907
908 use_icount = 2;
909
910 /* 125MIPS seems a reasonable initial guess at the guest speed.
911 It will be corrected fairly quickly anyway. */
Paolo Bonzinic1ff0732018-08-14 09:31:58 +0200912 timers_state.icount_time_shift = 3;
Paolo Bonzini946fb272011-09-12 13:57:37 +0200913
914 /* Have both realtime and virtual time triggers for speed adjustment.
915 The realtime trigger catches emulated time passing too slowly,
916 the virtual time trigger catches emulated time passing too fast.
917 Realtime triggers occur even when idle, so use them less frequently
918 than VM triggers. */
Pavel Dovgalyukb39e3f32018-01-11 11:26:10 +0300919 timers_state.vm_clock_warp_start = -1;
920 timers_state.icount_rt_timer = timer_new_ms(QEMU_CLOCK_VIRTUAL_RT,
Pavel Dovgalyukbf2a7dd2014-11-26 13:40:55 +0300921 icount_adjust_rt, NULL);
Pavel Dovgalyukb39e3f32018-01-11 11:26:10 +0300922 timer_mod(timers_state.icount_rt_timer,
Pavel Dovgalyukbf2a7dd2014-11-26 13:40:55 +0300923 qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL_RT) + 1000);
Pavel Dovgalyukb39e3f32018-01-11 11:26:10 +0300924 timers_state.icount_vm_timer = timer_new_ns(QEMU_CLOCK_VIRTUAL,
Alex Bligh40daca52013-08-21 16:03:02 +0100925 icount_adjust_vm, NULL);
Pavel Dovgalyukb39e3f32018-01-11 11:26:10 +0300926 timer_mod(timers_state.icount_vm_timer,
Alex Bligh40daca52013-08-21 16:03:02 +0100927 qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) +
Rutuja Shah73bcb242016-03-21 21:32:30 +0530928 NANOSECONDS_PER_SECOND / 10);
Paolo Bonzini946fb272011-09-12 13:57:37 +0200929}
930
931/***********************************************************/
Alex Bennée65467062017-02-23 18:29:09 +0000932/* TCG vCPU kick timer
933 *
934 * The kick timer is responsible for moving single threaded vCPU
935 * emulation on to the next vCPU. If more than one vCPU is running a
936 * timer event with force a cpu->exit so the next vCPU can get
937 * scheduled.
938 *
939 * The timer is removed if all vCPUs are idle and restarted again once
940 * idleness is complete.
941 */
942
943static QEMUTimer *tcg_kick_vcpu_timer;
Alex Bennée791158d2017-02-23 18:29:10 +0000944static CPUState *tcg_current_rr_cpu;
Alex Bennée65467062017-02-23 18:29:09 +0000945
946#define TCG_KICK_PERIOD (NANOSECONDS_PER_SECOND / 10)
947
948static inline int64_t qemu_tcg_next_kick(void)
949{
950 return qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) + TCG_KICK_PERIOD;
951}
952
Alex Bennéee8f22f72019-10-01 17:04:26 +0100953/* Kick the currently round-robin scheduled vCPU to next */
954static void qemu_cpu_kick_rr_next_cpu(void)
Alex Bennée791158d2017-02-23 18:29:10 +0000955{
956 CPUState *cpu;
Alex Bennée791158d2017-02-23 18:29:10 +0000957 do {
958 cpu = atomic_mb_read(&tcg_current_rr_cpu);
959 if (cpu) {
960 cpu_exit(cpu);
961 }
962 } while (cpu != atomic_mb_read(&tcg_current_rr_cpu));
963}
964
Alex Bennéee8f22f72019-10-01 17:04:26 +0100965/* Kick all RR vCPUs */
966static void qemu_cpu_kick_rr_cpus(void)
967{
968 CPUState *cpu;
969
970 CPU_FOREACH(cpu) {
971 cpu_exit(cpu);
972 };
973}
974
Paolo Bonzini6b8f0182017-03-02 19:56:40 +0100975static void do_nothing(CPUState *cpu, run_on_cpu_data unused)
976{
977}
978
Paolo Bonzini3f53bc62017-03-03 11:50:29 +0100979void qemu_timer_notify_cb(void *opaque, QEMUClockType type)
980{
Paolo Bonzini6b8f0182017-03-02 19:56:40 +0100981 if (!use_icount || type != QEMU_CLOCK_VIRTUAL) {
982 qemu_notify_event();
983 return;
984 }
985
Peter Maydellc52e7132018-04-10 13:02:25 +0100986 if (qemu_in_vcpu_thread()) {
987 /* A CPU is currently running; kick it back out to the
988 * tcg_cpu_exec() loop so it will recalculate its
989 * icount deadline immediately.
990 */
991 qemu_cpu_kick(current_cpu);
992 } else if (first_cpu) {
Paolo Bonzini6b8f0182017-03-02 19:56:40 +0100993 /* qemu_cpu_kick is not enough to kick a halted CPU out of
994 * qemu_tcg_wait_io_event. async_run_on_cpu, instead,
995 * causes cpu_thread_is_idle to return false. This way,
996 * handle_icount_deadline can run.
Peter Maydellc52e7132018-04-10 13:02:25 +0100997 * If we have no CPUs at all for some reason, we don't
998 * need to do anything.
Paolo Bonzini6b8f0182017-03-02 19:56:40 +0100999 */
1000 async_run_on_cpu(first_cpu, do_nothing, RUN_ON_CPU_NULL);
1001 }
Paolo Bonzini3f53bc62017-03-03 11:50:29 +01001002}
1003
Alex Bennée65467062017-02-23 18:29:09 +00001004static void kick_tcg_thread(void *opaque)
1005{
1006 timer_mod(tcg_kick_vcpu_timer, qemu_tcg_next_kick());
Alex Bennéee8f22f72019-10-01 17:04:26 +01001007 qemu_cpu_kick_rr_next_cpu();
Alex Bennée65467062017-02-23 18:29:09 +00001008}
1009
1010static void start_tcg_kick_timer(void)
1011{
Paolo Bonzinidb08b682018-01-11 13:53:12 +01001012 assert(!mttcg_enabled);
1013 if (!tcg_kick_vcpu_timer && CPU_NEXT(first_cpu)) {
Alex Bennée65467062017-02-23 18:29:09 +00001014 tcg_kick_vcpu_timer = timer_new_ns(QEMU_CLOCK_VIRTUAL,
1015 kick_tcg_thread, NULL);
Alex Bennée1926ab22018-09-27 18:17:24 +01001016 }
1017 if (tcg_kick_vcpu_timer && !timer_pending(tcg_kick_vcpu_timer)) {
Alex Bennée65467062017-02-23 18:29:09 +00001018 timer_mod(tcg_kick_vcpu_timer, qemu_tcg_next_kick());
1019 }
1020}
1021
1022static void stop_tcg_kick_timer(void)
1023{
Paolo Bonzinidb08b682018-01-11 13:53:12 +01001024 assert(!mttcg_enabled);
Alex Bennée1926ab22018-09-27 18:17:24 +01001025 if (tcg_kick_vcpu_timer && timer_pending(tcg_kick_vcpu_timer)) {
Alex Bennée65467062017-02-23 18:29:09 +00001026 timer_del(tcg_kick_vcpu_timer);
Alex Bennée65467062017-02-23 18:29:09 +00001027 }
1028}
1029
Alex Bennée65467062017-02-23 18:29:09 +00001030/***********************************************************/
Blue Swirl296af7c2010-03-29 19:23:50 +00001031void hw_error(const char *fmt, ...)
1032{
1033 va_list ap;
Andreas Färber55e5c282012-12-17 06:18:02 +01001034 CPUState *cpu;
Blue Swirl296af7c2010-03-29 19:23:50 +00001035
1036 va_start(ap, fmt);
1037 fprintf(stderr, "qemu: hardware error: ");
1038 vfprintf(stderr, fmt, ap);
1039 fprintf(stderr, "\n");
Andreas Färberbdc44642013-06-24 23:50:24 +02001040 CPU_FOREACH(cpu) {
Andreas Färber55e5c282012-12-17 06:18:02 +01001041 fprintf(stderr, "CPU #%d:\n", cpu->cpu_index);
Markus Armbruster90c84c52019-04-17 21:18:02 +02001042 cpu_dump_state(cpu, stderr, CPU_DUMP_FPU);
Blue Swirl296af7c2010-03-29 19:23:50 +00001043 }
1044 va_end(ap);
1045 abort();
1046}
1047
1048void cpu_synchronize_all_states(void)
1049{
Andreas Färber182735e2013-05-29 22:29:20 +02001050 CPUState *cpu;
Blue Swirl296af7c2010-03-29 19:23:50 +00001051
Andreas Färberbdc44642013-06-24 23:50:24 +02001052 CPU_FOREACH(cpu) {
Andreas Färber182735e2013-05-29 22:29:20 +02001053 cpu_synchronize_state(cpu);
Sergio Andres Gomez Del Realc97d6d22017-09-13 04:05:09 -05001054 /* TODO: move to cpu_synchronize_state() */
1055 if (hvf_enabled()) {
1056 hvf_cpu_synchronize_state(cpu);
1057 }
Blue Swirl296af7c2010-03-29 19:23:50 +00001058 }
1059}
1060
1061void cpu_synchronize_all_post_reset(void)
1062{
Andreas Färber182735e2013-05-29 22:29:20 +02001063 CPUState *cpu;
Blue Swirl296af7c2010-03-29 19:23:50 +00001064
Andreas Färberbdc44642013-06-24 23:50:24 +02001065 CPU_FOREACH(cpu) {
Andreas Färber182735e2013-05-29 22:29:20 +02001066 cpu_synchronize_post_reset(cpu);
Sergio Andres Gomez Del Realc97d6d22017-09-13 04:05:09 -05001067 /* TODO: move to cpu_synchronize_post_reset() */
1068 if (hvf_enabled()) {
1069 hvf_cpu_synchronize_post_reset(cpu);
1070 }
Blue Swirl296af7c2010-03-29 19:23:50 +00001071 }
1072}
1073
1074void cpu_synchronize_all_post_init(void)
1075{
Andreas Färber182735e2013-05-29 22:29:20 +02001076 CPUState *cpu;
Blue Swirl296af7c2010-03-29 19:23:50 +00001077
Andreas Färberbdc44642013-06-24 23:50:24 +02001078 CPU_FOREACH(cpu) {
Andreas Färber182735e2013-05-29 22:29:20 +02001079 cpu_synchronize_post_init(cpu);
Sergio Andres Gomez Del Realc97d6d22017-09-13 04:05:09 -05001080 /* TODO: move to cpu_synchronize_post_init() */
1081 if (hvf_enabled()) {
1082 hvf_cpu_synchronize_post_init(cpu);
1083 }
Blue Swirl296af7c2010-03-29 19:23:50 +00001084 }
1085}
1086
David Gibson75e972d2017-05-26 14:46:28 +10001087void cpu_synchronize_all_pre_loadvm(void)
1088{
1089 CPUState *cpu;
1090
1091 CPU_FOREACH(cpu) {
1092 cpu_synchronize_pre_loadvm(cpu);
1093 }
1094}
1095
Stefan Hajnoczi4486e892018-03-07 14:42:05 +00001096static int do_vm_stop(RunState state, bool send_stop)
Blue Swirl296af7c2010-03-29 19:23:50 +00001097{
Kevin Wolf56983462013-07-05 13:49:54 +02001098 int ret = 0;
1099
Luiz Capitulino13548692011-07-29 15:36:43 -03001100 if (runstate_is_running()) {
Blue Swirl296af7c2010-03-29 19:23:50 +00001101 cpu_disable_ticks();
Blue Swirl296af7c2010-03-29 19:23:50 +00001102 pause_all_vcpus();
Luiz Capitulinof5bbfba2011-07-29 15:04:45 -03001103 runstate_set(state);
Luiz Capitulino1dfb4dd2011-07-29 14:26:33 -03001104 vm_state_notify(0, state);
Stefan Hajnoczi4486e892018-03-07 14:42:05 +00001105 if (send_stop) {
Peter Xu3ab72382018-08-15 21:37:37 +08001106 qapi_event_send_stop();
Stefan Hajnoczi4486e892018-03-07 14:42:05 +00001107 }
Blue Swirl296af7c2010-03-29 19:23:50 +00001108 }
Kevin Wolf56983462013-07-05 13:49:54 +02001109
Kevin Wolf594a45c2013-07-18 14:52:19 +02001110 bdrv_drain_all();
John Snow22af08e2016-09-22 21:45:51 -04001111 ret = bdrv_flush_all();
Kevin Wolf594a45c2013-07-18 14:52:19 +02001112
Kevin Wolf56983462013-07-05 13:49:54 +02001113 return ret;
Blue Swirl296af7c2010-03-29 19:23:50 +00001114}
1115
Stefan Hajnoczi4486e892018-03-07 14:42:05 +00001116/* Special vm_stop() variant for terminating the process. Historically clients
1117 * did not expect a QMP STOP event and so we need to retain compatibility.
1118 */
1119int vm_shutdown(void)
1120{
1121 return do_vm_stop(RUN_STATE_SHUTDOWN, false);
1122}
1123
Andreas Färbera1fcaa72012-05-02 23:42:26 +02001124static bool cpu_can_run(CPUState *cpu)
Blue Swirl296af7c2010-03-29 19:23:50 +00001125{
Andreas Färber4fdeee72012-05-02 23:10:09 +02001126 if (cpu->stop) {
Andreas Färbera1fcaa72012-05-02 23:42:26 +02001127 return false;
Jan Kiszka0ab07c62011-02-07 12:19:14 +01001128 }
Tiejun Chen321bc0b2013-08-02 09:43:09 +08001129 if (cpu_is_stopped(cpu)) {
Andreas Färbera1fcaa72012-05-02 23:42:26 +02001130 return false;
Jan Kiszka0ab07c62011-02-07 12:19:14 +01001131 }
Andreas Färbera1fcaa72012-05-02 23:42:26 +02001132 return true;
Blue Swirl296af7c2010-03-29 19:23:50 +00001133}
1134
Andreas Färber91325042013-05-27 02:07:49 +02001135static void cpu_handle_guest_debug(CPUState *cpu)
Jan Kiszka3c638d02010-06-25 16:56:56 +02001136{
Andreas Färber64f6b342013-05-27 02:06:09 +02001137 gdb_set_stop_cpu(cpu);
Jan Kiszka8cf71712011-02-07 12:19:16 +01001138 qemu_system_debug_request();
Andreas Färberf324e762012-05-02 23:26:21 +02001139 cpu->stopped = true;
Jan Kiszka3c638d02010-06-25 16:56:56 +02001140}
1141
Jan Kiszka6d9cb732011-02-01 22:15:58 +01001142#ifdef CONFIG_LINUX
1143static void sigbus_reraise(void)
1144{
1145 sigset_t set;
1146 struct sigaction action;
1147
1148 memset(&action, 0, sizeof(action));
1149 action.sa_handler = SIG_DFL;
1150 if (!sigaction(SIGBUS, &action, NULL)) {
1151 raise(SIGBUS);
1152 sigemptyset(&set);
1153 sigaddset(&set, SIGBUS);
Peter Maydella2d17612016-05-16 18:33:59 +01001154 pthread_sigmask(SIG_UNBLOCK, &set, NULL);
Jan Kiszka6d9cb732011-02-01 22:15:58 +01001155 }
1156 perror("Failed to re-raise SIGBUS!\n");
1157 abort();
1158}
1159
Paolo Bonzinid98d4072017-02-08 13:22:12 +01001160static void sigbus_handler(int n, siginfo_t *siginfo, void *ctx)
Jan Kiszka6d9cb732011-02-01 22:15:58 +01001161{
Paolo Bonzinia16fc072017-02-09 09:50:02 +01001162 if (siginfo->si_code != BUS_MCEERR_AO && siginfo->si_code != BUS_MCEERR_AR) {
1163 sigbus_reraise();
1164 }
1165
Paolo Bonzini2ae41db2017-02-08 12:48:54 +01001166 if (current_cpu) {
1167 /* Called asynchronously in VCPU thread. */
1168 if (kvm_on_sigbus_vcpu(current_cpu, siginfo->si_code, siginfo->si_addr)) {
1169 sigbus_reraise();
1170 }
1171 } else {
1172 /* Called synchronously (via signalfd) in main thread. */
1173 if (kvm_on_sigbus(siginfo->si_code, siginfo->si_addr)) {
1174 sigbus_reraise();
1175 }
Jan Kiszka6d9cb732011-02-01 22:15:58 +01001176 }
1177}
1178
1179static void qemu_init_sigbus(void)
1180{
1181 struct sigaction action;
1182
1183 memset(&action, 0, sizeof(action));
1184 action.sa_flags = SA_SIGINFO;
Paolo Bonzinid98d4072017-02-08 13:22:12 +01001185 action.sa_sigaction = sigbus_handler;
Jan Kiszka6d9cb732011-02-01 22:15:58 +01001186 sigaction(SIGBUS, &action, NULL);
1187
1188 prctl(PR_MCE_KILL, PR_MCE_KILL_SET, PR_MCE_KILL_EARLY, 0, 0);
1189}
Paolo Bonzinia16fc072017-02-09 09:50:02 +01001190#else /* !CONFIG_LINUX */
1191static void qemu_init_sigbus(void)
1192{
1193}
Paolo Bonzinia16fc072017-02-09 09:50:02 +01001194#endif /* !CONFIG_LINUX */
Blue Swirl296af7c2010-03-29 19:23:50 +00001195
Blue Swirl296af7c2010-03-29 19:23:50 +00001196static QemuThread io_thread;
1197
Blue Swirl296af7c2010-03-29 19:23:50 +00001198/* cpu creation */
1199static QemuCond qemu_cpu_cond;
1200/* system init */
Blue Swirl296af7c2010-03-29 19:23:50 +00001201static QemuCond qemu_pause_cond;
1202
Paolo Bonzinid3b12f52011-09-13 10:30:52 +02001203void qemu_init_cpu_loop(void)
Blue Swirl296af7c2010-03-29 19:23:50 +00001204{
Jan Kiszka6d9cb732011-02-01 22:15:58 +01001205 qemu_init_sigbus();
Anthony Liguoried945922011-02-08 18:18:18 +01001206 qemu_cond_init(&qemu_cpu_cond);
Anthony Liguoried945922011-02-08 18:18:18 +01001207 qemu_cond_init(&qemu_pause_cond);
Blue Swirl296af7c2010-03-29 19:23:50 +00001208 qemu_mutex_init(&qemu_global_mutex);
Blue Swirl296af7c2010-03-29 19:23:50 +00001209
Jan Kiszkab7680cb2011-03-12 17:43:51 +01001210 qemu_thread_get_self(&io_thread);
Blue Swirl296af7c2010-03-29 19:23:50 +00001211}
1212
Paolo Bonzini14e6fe12016-10-31 10:36:08 +01001213void run_on_cpu(CPUState *cpu, run_on_cpu_func func, run_on_cpu_data data)
Marcelo Tosattie82bcec2010-05-04 09:45:22 -03001214{
Sergey Fedorovd148d902016-08-29 09:51:00 +02001215 do_run_on_cpu(cpu, func, data, &qemu_global_mutex);
Chegu Vinod3c022702013-06-24 03:49:41 -06001216}
1217
Gu Zheng4c055ab2016-05-12 09:18:13 +05301218static void qemu_kvm_destroy_vcpu(CPUState *cpu)
1219{
1220 if (kvm_destroy_vcpu(cpu) < 0) {
1221 error_report("kvm_destroy_vcpu failed");
1222 exit(EXIT_FAILURE);
1223 }
1224}
1225
1226static void qemu_tcg_destroy_vcpu(CPUState *cpu)
1227{
1228}
1229
David Hildenbrandebd05fe2017-11-29 20:12:15 +01001230static void qemu_cpu_stop(CPUState *cpu, bool exit)
1231{
1232 g_assert(qemu_cpu_is_self(cpu));
1233 cpu->stop = false;
1234 cpu->stopped = true;
1235 if (exit) {
1236 cpu_exit(cpu);
1237 }
1238 qemu_cond_broadcast(&qemu_pause_cond);
1239}
1240
Andreas Färber509a0d72012-05-03 02:18:09 +02001241static void qemu_wait_io_event_common(CPUState *cpu)
Blue Swirl296af7c2010-03-29 19:23:50 +00001242{
Alex Bennée37257942017-02-23 18:29:14 +00001243 atomic_mb_set(&cpu->thread_kicked, false);
Andreas Färber4fdeee72012-05-02 23:10:09 +02001244 if (cpu->stop) {
David Hildenbrandebd05fe2017-11-29 20:12:15 +01001245 qemu_cpu_stop(cpu, false);
Blue Swirl296af7c2010-03-29 19:23:50 +00001246 }
Sergey Fedorova5403c62016-08-02 18:27:36 +01001247 process_queued_cpu_work(cpu);
Alex Bennée37257942017-02-23 18:29:14 +00001248}
1249
Paolo Bonzinia8efa602018-11-14 12:36:57 +01001250static void qemu_tcg_rr_wait_io_event(void)
Alex Bennée37257942017-02-23 18:29:14 +00001251{
Paolo Bonzinia8efa602018-11-14 12:36:57 +01001252 CPUState *cpu;
1253
Paolo Bonzinidb08b682018-01-11 13:53:12 +01001254 while (all_cpu_threads_idle()) {
Alex Bennée65467062017-02-23 18:29:09 +00001255 stop_tcg_kick_timer();
Paolo Bonzinia8efa602018-11-14 12:36:57 +01001256 qemu_cond_wait(first_cpu->halt_cond, &qemu_global_mutex);
Jan Kiszka16400322011-02-09 16:29:37 +01001257 }
Blue Swirl296af7c2010-03-29 19:23:50 +00001258
Alex Bennée65467062017-02-23 18:29:09 +00001259 start_tcg_kick_timer();
1260
Paolo Bonzinia8efa602018-11-14 12:36:57 +01001261 CPU_FOREACH(cpu) {
1262 qemu_wait_io_event_common(cpu);
1263 }
Blue Swirl296af7c2010-03-29 19:23:50 +00001264}
1265
Paolo Bonzinidb08b682018-01-11 13:53:12 +01001266static void qemu_wait_io_event(CPUState *cpu)
Blue Swirl296af7c2010-03-29 19:23:50 +00001267{
Emilio G. Cota30865f32018-10-21 13:30:35 -04001268 bool slept = false;
1269
Andreas Färbera98ae1d2013-05-26 23:21:08 +02001270 while (cpu_thread_is_idle(cpu)) {
Emilio G. Cota30865f32018-10-21 13:30:35 -04001271 if (!slept) {
1272 slept = true;
1273 qemu_plugin_vcpu_idle_cb(cpu);
1274 }
Andreas Färberf5c121b2012-05-03 01:22:49 +02001275 qemu_cond_wait(cpu->halt_cond, &qemu_global_mutex);
Jan Kiszka16400322011-02-09 16:29:37 +01001276 }
Emilio G. Cota30865f32018-10-21 13:30:35 -04001277 if (slept) {
1278 qemu_plugin_vcpu_resume_cb(cpu);
1279 }
Blue Swirl296af7c2010-03-29 19:23:50 +00001280
Paolo Bonzinidb08b682018-01-11 13:53:12 +01001281#ifdef _WIN32
1282 /* Eat dummy APC queued by qemu_cpu_kick_thread. */
1283 if (!tcg_enabled()) {
1284 SleepEx(0, TRUE);
Sergio Andres Gomez Del Realc97d6d22017-09-13 04:05:09 -05001285 }
Paolo Bonzinidb08b682018-01-11 13:53:12 +01001286#endif
Sergio Andres Gomez Del Realc97d6d22017-09-13 04:05:09 -05001287 qemu_wait_io_event_common(cpu);
1288}
1289
Jan Kiszka7e97cd82011-02-07 12:19:12 +01001290static void *qemu_kvm_cpu_thread_fn(void *arg)
Blue Swirl296af7c2010-03-29 19:23:50 +00001291{
Andreas Färber48a106b2013-05-27 02:20:39 +02001292 CPUState *cpu = arg;
Jan Kiszka84b49152011-02-01 22:15:50 +01001293 int r;
Blue Swirl296af7c2010-03-29 19:23:50 +00001294
Paolo Bonziniab28bd22015-07-09 08:55:38 +02001295 rcu_register_thread();
1296
Paolo Bonzini2e7f7a32015-06-18 18:47:18 +02001297 qemu_mutex_lock_iothread();
Andreas Färber814e6122012-05-02 17:00:37 +02001298 qemu_thread_get_self(cpu->thread);
Andreas Färber9f09e182012-05-03 06:59:07 +02001299 cpu->thread_id = qemu_get_thread_id();
Pavel Dovgalyuk626cf8f2014-12-08 10:53:17 +03001300 cpu->can_do_io = 1;
Andreas Färber4917cf42013-05-27 05:17:50 +02001301 current_cpu = cpu;
Blue Swirl296af7c2010-03-29 19:23:50 +00001302
Andreas Färber504134d2012-12-17 06:38:45 +01001303 r = kvm_init_vcpu(cpu);
Jan Kiszka84b49152011-02-01 22:15:50 +01001304 if (r < 0) {
Alistair Francis493d89b2018-02-03 09:43:14 +01001305 error_report("kvm_init_vcpu failed: %s", strerror(-r));
Jan Kiszka84b49152011-02-01 22:15:50 +01001306 exit(1);
1307 }
Blue Swirl296af7c2010-03-29 19:23:50 +00001308
Paolo Bonzini18268b62017-02-09 09:41:14 +01001309 kvm_init_cpu_signals(cpu);
Blue Swirl296af7c2010-03-29 19:23:50 +00001310
1311 /* signal CPU creation */
Andreas Färber61a46212012-05-02 22:49:36 +02001312 cpu->created = true;
Blue Swirl296af7c2010-03-29 19:23:50 +00001313 qemu_cond_signal(&qemu_cpu_cond);
Richard Henderson9c09a252019-03-14 13:06:29 -07001314 qemu_guest_random_seed_thread_part2(cpu->random_seed);
Blue Swirl296af7c2010-03-29 19:23:50 +00001315
Gu Zheng4c055ab2016-05-12 09:18:13 +05301316 do {
Andreas Färbera1fcaa72012-05-02 23:42:26 +02001317 if (cpu_can_run(cpu)) {
Andreas Färber1458c362013-05-26 23:46:55 +02001318 r = kvm_cpu_exec(cpu);
Jan Kiszka83f338f2011-02-07 12:19:17 +01001319 if (r == EXCP_DEBUG) {
Andreas Färber91325042013-05-27 02:07:49 +02001320 cpu_handle_guest_debug(cpu);
Jan Kiszka83f338f2011-02-07 12:19:17 +01001321 }
Jan Kiszka0ab07c62011-02-07 12:19:14 +01001322 }
Paolo Bonzinidb08b682018-01-11 13:53:12 +01001323 qemu_wait_io_event(cpu);
Gu Zheng4c055ab2016-05-12 09:18:13 +05301324 } while (!cpu->unplug || cpu_can_run(cpu));
Blue Swirl296af7c2010-03-29 19:23:50 +00001325
Gu Zheng4c055ab2016-05-12 09:18:13 +05301326 qemu_kvm_destroy_vcpu(cpu);
Bharata B Rao2c579042016-05-12 09:18:14 +05301327 cpu->created = false;
1328 qemu_cond_signal(&qemu_cpu_cond);
Gu Zheng4c055ab2016-05-12 09:18:13 +05301329 qemu_mutex_unlock_iothread();
Paolo Bonzini57615ed2018-01-30 11:04:36 -05001330 rcu_unregister_thread();
Blue Swirl296af7c2010-03-29 19:23:50 +00001331 return NULL;
1332}
1333
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +02001334static void *qemu_dummy_cpu_thread_fn(void *arg)
1335{
1336#ifdef _WIN32
Alistair Francis493d89b2018-02-03 09:43:14 +01001337 error_report("qtest is not supported under Windows");
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +02001338 exit(1);
1339#else
Andreas Färber10a90212013-05-27 02:24:35 +02001340 CPUState *cpu = arg;
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +02001341 sigset_t waitset;
1342 int r;
1343
Paolo Bonziniab28bd22015-07-09 08:55:38 +02001344 rcu_register_thread();
1345
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +02001346 qemu_mutex_lock_iothread();
Andreas Färber814e6122012-05-02 17:00:37 +02001347 qemu_thread_get_self(cpu->thread);
Andreas Färber9f09e182012-05-03 06:59:07 +02001348 cpu->thread_id = qemu_get_thread_id();
Pavel Dovgalyuk626cf8f2014-12-08 10:53:17 +03001349 cpu->can_do_io = 1;
Alex Bennée37257942017-02-23 18:29:14 +00001350 current_cpu = cpu;
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +02001351
1352 sigemptyset(&waitset);
1353 sigaddset(&waitset, SIG_IPI);
1354
1355 /* signal CPU creation */
Andreas Färber61a46212012-05-02 22:49:36 +02001356 cpu->created = true;
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +02001357 qemu_cond_signal(&qemu_cpu_cond);
Richard Henderson9c09a252019-03-14 13:06:29 -07001358 qemu_guest_random_seed_thread_part2(cpu->random_seed);
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +02001359
Paolo Bonzinid2831ab2018-01-30 11:04:53 -05001360 do {
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +02001361 qemu_mutex_unlock_iothread();
1362 do {
1363 int sig;
1364 r = sigwait(&waitset, &sig);
1365 } while (r == -1 && (errno == EAGAIN || errno == EINTR));
1366 if (r == -1) {
1367 perror("sigwait");
1368 exit(1);
1369 }
1370 qemu_mutex_lock_iothread();
Paolo Bonzinidb08b682018-01-11 13:53:12 +01001371 qemu_wait_io_event(cpu);
Paolo Bonzinid2831ab2018-01-30 11:04:53 -05001372 } while (!cpu->unplug);
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +02001373
David Hildenbrandd40bfcb2019-02-18 10:21:57 +01001374 qemu_mutex_unlock_iothread();
Paolo Bonzinid2831ab2018-01-30 11:04:53 -05001375 rcu_unregister_thread();
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +02001376 return NULL;
1377#endif
1378}
1379
Alex Bennée1be7fcb2016-10-27 16:10:08 +01001380static int64_t tcg_get_icount_limit(void)
1381{
1382 int64_t deadline;
1383
1384 if (replay_mode != REPLAY_MODE_PLAY) {
Pavel Dovgalyukdcb15782019-07-25 11:44:26 +03001385 /*
1386 * Include all the timers, because they may need an attention.
1387 * Too long CPU execution may create unnecessary delay in UI.
1388 */
1389 deadline = qemu_clock_deadline_ns_all(QEMU_CLOCK_VIRTUAL,
1390 QEMU_TIMER_ATTR_ALL);
Alex Bennée1be7fcb2016-10-27 16:10:08 +01001391
1392 /* Maintain prior (possibly buggy) behaviour where if no deadline
1393 * was set (as there is no QEMU_CLOCK_VIRTUAL timer) or it is more than
1394 * INT32_MAX nanoseconds ahead, we still use INT32_MAX
1395 * nanoseconds.
1396 */
1397 if ((deadline < 0) || (deadline > INT32_MAX)) {
1398 deadline = INT32_MAX;
1399 }
1400
1401 return qemu_icount_round(deadline);
1402 } else {
1403 return replay_get_instructions();
1404 }
1405}
1406
Alex Bennée12e97002016-10-27 16:10:14 +01001407static void handle_icount_deadline(void)
1408{
Paolo Bonzini6b8f0182017-03-02 19:56:40 +01001409 assert(qemu_in_vcpu_thread());
Alex Bennée12e97002016-10-27 16:10:14 +01001410 if (use_icount) {
Pavel Dovgalyukdcb15782019-07-25 11:44:26 +03001411 int64_t deadline = qemu_clock_deadline_ns_all(QEMU_CLOCK_VIRTUAL,
1412 QEMU_TIMER_ATTR_ALL);
Alex Bennée12e97002016-10-27 16:10:14 +01001413
1414 if (deadline == 0) {
Paolo Bonzini6b8f0182017-03-02 19:56:40 +01001415 /* Wake up other AioContexts. */
Alex Bennée12e97002016-10-27 16:10:14 +01001416 qemu_clock_notify(QEMU_CLOCK_VIRTUAL);
Paolo Bonzini6b8f0182017-03-02 19:56:40 +01001417 qemu_clock_run_timers(QEMU_CLOCK_VIRTUAL);
Alex Bennée12e97002016-10-27 16:10:14 +01001418 }
1419 }
1420}
1421
Alex Bennée05248382017-03-29 16:46:59 +01001422static void prepare_icount_for_run(CPUState *cpu)
1423{
1424 if (use_icount) {
Alex Bennéeeda5f7c2017-04-05 12:35:48 +01001425 int insns_left;
Alex Bennée05248382017-03-29 16:46:59 +01001426
1427 /* These should always be cleared by process_icount_data after
1428 * each vCPU execution. However u16.high can be raised
1429 * asynchronously by cpu_exit/cpu_interrupt/tcg_handle_interrupt
1430 */
Richard Henderson5e140192019-03-28 11:54:23 -10001431 g_assert(cpu_neg(cpu)->icount_decr.u16.low == 0);
Alex Bennée05248382017-03-29 16:46:59 +01001432 g_assert(cpu->icount_extra == 0);
1433
Alex Bennéeeda5f7c2017-04-05 12:35:48 +01001434 cpu->icount_budget = tcg_get_icount_limit();
1435 insns_left = MIN(0xffff, cpu->icount_budget);
Richard Henderson5e140192019-03-28 11:54:23 -10001436 cpu_neg(cpu)->icount_decr.u16.low = insns_left;
Alex Bennéeeda5f7c2017-04-05 12:35:48 +01001437 cpu->icount_extra = cpu->icount_budget - insns_left;
Alex Bennéed759c952018-02-27 12:52:48 +03001438
1439 replay_mutex_lock();
Alex Bennée05248382017-03-29 16:46:59 +01001440 }
1441}
1442
1443static void process_icount_data(CPUState *cpu)
1444{
1445 if (use_icount) {
Alex Bennéee4cd9652017-03-31 16:09:42 +01001446 /* Account for executed instructions */
Alex Bennée512d3c82017-04-05 12:32:37 +01001447 cpu_update_icount(cpu);
Alex Bennée05248382017-03-29 16:46:59 +01001448
1449 /* Reset the counters */
Richard Henderson5e140192019-03-28 11:54:23 -10001450 cpu_neg(cpu)->icount_decr.u16.low = 0;
Alex Bennée05248382017-03-29 16:46:59 +01001451 cpu->icount_extra = 0;
Alex Bennéee4cd9652017-03-31 16:09:42 +01001452 cpu->icount_budget = 0;
1453
Alex Bennée05248382017-03-29 16:46:59 +01001454 replay_account_executed_instructions();
Alex Bennéed759c952018-02-27 12:52:48 +03001455
1456 replay_mutex_unlock();
Alex Bennée05248382017-03-29 16:46:59 +01001457 }
1458}
1459
1460
Alex Bennée1be7fcb2016-10-27 16:10:08 +01001461static int tcg_cpu_exec(CPUState *cpu)
1462{
1463 int ret;
1464#ifdef CONFIG_PROFILER
1465 int64_t ti;
1466#endif
1467
Emilio G. Cotaf28d0df2018-06-22 13:45:31 -04001468 assert(tcg_enabled());
Alex Bennée1be7fcb2016-10-27 16:10:08 +01001469#ifdef CONFIG_PROFILER
1470 ti = profile_getclock();
1471#endif
Alex Bennée1be7fcb2016-10-27 16:10:08 +01001472 cpu_exec_start(cpu);
1473 ret = cpu_exec(cpu);
1474 cpu_exec_end(cpu);
1475#ifdef CONFIG_PROFILER
Emilio G. Cota72fd2ef2018-10-10 10:48:53 -04001476 atomic_set(&tcg_ctx->prof.cpu_exec_time,
1477 tcg_ctx->prof.cpu_exec_time + profile_getclock() - ti);
Alex Bennée1be7fcb2016-10-27 16:10:08 +01001478#endif
Alex Bennée1be7fcb2016-10-27 16:10:08 +01001479 return ret;
1480}
1481
Alex Bennéec93bbbe2016-10-27 16:10:09 +01001482/* Destroy any remaining vCPUs which have been unplugged and have
1483 * finished running
1484 */
1485static void deal_with_unplugged_cpus(void)
Alex Bennée1be7fcb2016-10-27 16:10:08 +01001486{
Alex Bennéec93bbbe2016-10-27 16:10:09 +01001487 CPUState *cpu;
Alex Bennée1be7fcb2016-10-27 16:10:08 +01001488
Alex Bennéec93bbbe2016-10-27 16:10:09 +01001489 CPU_FOREACH(cpu) {
1490 if (cpu->unplug && !cpu_can_run(cpu)) {
1491 qemu_tcg_destroy_vcpu(cpu);
1492 cpu->created = false;
1493 qemu_cond_signal(&qemu_cpu_cond);
Alex Bennée1be7fcb2016-10-27 16:10:08 +01001494 break;
1495 }
1496 }
Alex Bennée1be7fcb2016-10-27 16:10:08 +01001497}
Jan Kiszkabdb7ca62011-09-26 09:40:39 +02001498
Alex Bennée65467062017-02-23 18:29:09 +00001499/* Single-threaded TCG
1500 *
1501 * In the single-threaded case each vCPU is simulated in turn. If
1502 * there is more than a single vCPU we create a simple timer to kick
1503 * the vCPU and ensure we don't get stuck in a tight loop in one vCPU.
1504 * This is done explicitly rather than relying on side-effects
1505 * elsewhere.
1506 */
1507
Alex Bennée37257942017-02-23 18:29:14 +00001508static void *qemu_tcg_rr_cpu_thread_fn(void *arg)
Blue Swirl296af7c2010-03-29 19:23:50 +00001509{
Andreas Färberc3586ba2012-05-03 01:41:24 +02001510 CPUState *cpu = arg;
Blue Swirl296af7c2010-03-29 19:23:50 +00001511
Emilio G. Cotaf28d0df2018-06-22 13:45:31 -04001512 assert(tcg_enabled());
Paolo Bonziniab28bd22015-07-09 08:55:38 +02001513 rcu_register_thread();
Emilio G. Cota3468b592017-07-19 18:57:58 -04001514 tcg_register_thread();
Paolo Bonziniab28bd22015-07-09 08:55:38 +02001515
Paolo Bonzini2e7f7a32015-06-18 18:47:18 +02001516 qemu_mutex_lock_iothread();
Andreas Färber814e6122012-05-02 17:00:37 +02001517 qemu_thread_get_self(cpu->thread);
Blue Swirl296af7c2010-03-29 19:23:50 +00001518
David Hildenbrand5a9c9732018-02-09 20:52:39 +01001519 cpu->thread_id = qemu_get_thread_id();
1520 cpu->created = true;
1521 cpu->can_do_io = 1;
Blue Swirl296af7c2010-03-29 19:23:50 +00001522 qemu_cond_signal(&qemu_cpu_cond);
Richard Henderson9c09a252019-03-14 13:06:29 -07001523 qemu_guest_random_seed_thread_part2(cpu->random_seed);
Blue Swirl296af7c2010-03-29 19:23:50 +00001524
Jan Kiszkafa7d1862011-08-22 18:35:25 +02001525 /* wait for initial kick-off after machine start */
Emilio G. Cotac28e3992015-04-27 12:45:28 -04001526 while (first_cpu->stopped) {
KONRAD Fredericd5f8d612015-08-10 17:27:06 +02001527 qemu_cond_wait(first_cpu->halt_cond, &qemu_global_mutex);
Jan Kiszka8e564b42012-02-17 18:31:15 +01001528
1529 /* process any pending work */
Andreas Färberbdc44642013-06-24 23:50:24 +02001530 CPU_FOREACH(cpu) {
Alex Bennée37257942017-02-23 18:29:14 +00001531 current_cpu = cpu;
Andreas Färber182735e2013-05-29 22:29:20 +02001532 qemu_wait_io_event_common(cpu);
Jan Kiszka8e564b42012-02-17 18:31:15 +01001533 }
Jan Kiszka0ab07c62011-02-07 12:19:14 +01001534 }
Blue Swirl296af7c2010-03-29 19:23:50 +00001535
Alex Bennée65467062017-02-23 18:29:09 +00001536 start_tcg_kick_timer();
1537
Alex Bennéec93bbbe2016-10-27 16:10:09 +01001538 cpu = first_cpu;
1539
Alex Bennéee5143e32017-02-23 18:29:12 +00001540 /* process any pending work */
1541 cpu->exit_request = 1;
1542
Blue Swirl296af7c2010-03-29 19:23:50 +00001543 while (1) {
Alex Bennéed759c952018-02-27 12:52:48 +03001544 qemu_mutex_unlock_iothread();
1545 replay_mutex_lock();
1546 qemu_mutex_lock_iothread();
Alex Bennéec93bbbe2016-10-27 16:10:09 +01001547 /* Account partial waits to QEMU_CLOCK_VIRTUAL. */
1548 qemu_account_warp_timer();
1549
Paolo Bonzini6b8f0182017-03-02 19:56:40 +01001550 /* Run the timers here. This is much more efficient than
1551 * waking up the I/O thread and waiting for completion.
1552 */
1553 handle_icount_deadline();
1554
Alex Bennéed759c952018-02-27 12:52:48 +03001555 replay_mutex_unlock();
1556
Alex Bennéec93bbbe2016-10-27 16:10:09 +01001557 if (!cpu) {
1558 cpu = first_cpu;
1559 }
1560
Alex Bennéee5143e32017-02-23 18:29:12 +00001561 while (cpu && !cpu->queued_work_first && !cpu->exit_request) {
1562
Alex Bennée791158d2017-02-23 18:29:10 +00001563 atomic_mb_set(&tcg_current_rr_cpu, cpu);
Alex Bennée37257942017-02-23 18:29:14 +00001564 current_cpu = cpu;
Alex Bennéec93bbbe2016-10-27 16:10:09 +01001565
1566 qemu_clock_enable(QEMU_CLOCK_VIRTUAL,
1567 (cpu->singlestep_enabled & SSTEP_NOTIMER) == 0);
1568
1569 if (cpu_can_run(cpu)) {
1570 int r;
Alex Bennée05248382017-03-29 16:46:59 +01001571
Alex Bennéed759c952018-02-27 12:52:48 +03001572 qemu_mutex_unlock_iothread();
Alex Bennée05248382017-03-29 16:46:59 +01001573 prepare_icount_for_run(cpu);
1574
Alex Bennéec93bbbe2016-10-27 16:10:09 +01001575 r = tcg_cpu_exec(cpu);
Alex Bennée05248382017-03-29 16:46:59 +01001576
1577 process_icount_data(cpu);
Alex Bennéed759c952018-02-27 12:52:48 +03001578 qemu_mutex_lock_iothread();
Alex Bennée05248382017-03-29 16:46:59 +01001579
Alex Bennéec93bbbe2016-10-27 16:10:09 +01001580 if (r == EXCP_DEBUG) {
1581 cpu_handle_guest_debug(cpu);
1582 break;
Pranith Kumar08e73c42017-02-23 18:29:15 +00001583 } else if (r == EXCP_ATOMIC) {
1584 qemu_mutex_unlock_iothread();
1585 cpu_exec_step_atomic(cpu);
1586 qemu_mutex_lock_iothread();
1587 break;
Alex Bennéec93bbbe2016-10-27 16:10:09 +01001588 }
Alex Bennée37257942017-02-23 18:29:14 +00001589 } else if (cpu->stop) {
Alex Bennéec93bbbe2016-10-27 16:10:09 +01001590 if (cpu->unplug) {
1591 cpu = CPU_NEXT(cpu);
1592 }
1593 break;
1594 }
1595
Alex Bennéee5143e32017-02-23 18:29:12 +00001596 cpu = CPU_NEXT(cpu);
1597 } /* while (cpu && !cpu->exit_request).. */
1598
Alex Bennée791158d2017-02-23 18:29:10 +00001599 /* Does not need atomic_mb_set because a spurious wakeup is okay. */
1600 atomic_set(&tcg_current_rr_cpu, NULL);
Alex Bennéec93bbbe2016-10-27 16:10:09 +01001601
Alex Bennéee5143e32017-02-23 18:29:12 +00001602 if (cpu && cpu->exit_request) {
1603 atomic_mb_set(&cpu->exit_request, 0);
1604 }
Alex Blighac70aaf2013-08-21 16:02:57 +01001605
Clement Deschamps013aabd2018-10-21 16:21:03 +02001606 if (use_icount && all_cpu_threads_idle()) {
1607 /*
1608 * When all cpus are sleeping (e.g in WFI), to avoid a deadlock
1609 * in the main_loop, wake it up in order to start the warp timer.
1610 */
1611 qemu_notify_event();
1612 }
1613
Paolo Bonzinia8efa602018-11-14 12:36:57 +01001614 qemu_tcg_rr_wait_io_event();
Alex Bennéec93bbbe2016-10-27 16:10:09 +01001615 deal_with_unplugged_cpus();
Blue Swirl296af7c2010-03-29 19:23:50 +00001616 }
1617
Paolo Bonzini9b0605f2018-01-30 11:05:06 -05001618 rcu_unregister_thread();
Blue Swirl296af7c2010-03-29 19:23:50 +00001619 return NULL;
1620}
1621
Vincent Palatinb0cb0a62017-01-10 11:59:57 +01001622static void *qemu_hax_cpu_thread_fn(void *arg)
1623{
1624 CPUState *cpu = arg;
1625 int r;
Vincent Palatinb3d3a422017-03-20 11:15:49 +01001626
Paolo Bonzini9857c2d2018-01-30 16:28:49 +01001627 rcu_register_thread();
Vincent Palatinb3d3a422017-03-20 11:15:49 +01001628 qemu_mutex_lock_iothread();
Vincent Palatinb0cb0a62017-01-10 11:59:57 +01001629 qemu_thread_get_self(cpu->thread);
Vincent Palatinb0cb0a62017-01-10 11:59:57 +01001630
1631 cpu->thread_id = qemu_get_thread_id();
1632 cpu->created = true;
Vincent Palatinb0cb0a62017-01-10 11:59:57 +01001633 current_cpu = cpu;
1634
1635 hax_init_vcpu(cpu);
1636 qemu_cond_signal(&qemu_cpu_cond);
Richard Henderson9c09a252019-03-14 13:06:29 -07001637 qemu_guest_random_seed_thread_part2(cpu->random_seed);
Vincent Palatinb0cb0a62017-01-10 11:59:57 +01001638
Paolo Bonzini9857c2d2018-01-30 16:28:49 +01001639 do {
Vincent Palatinb0cb0a62017-01-10 11:59:57 +01001640 if (cpu_can_run(cpu)) {
1641 r = hax_smp_cpu_exec(cpu);
1642 if (r == EXCP_DEBUG) {
1643 cpu_handle_guest_debug(cpu);
1644 }
1645 }
1646
Paolo Bonzinidb08b682018-01-11 13:53:12 +01001647 qemu_wait_io_event(cpu);
Paolo Bonzini9857c2d2018-01-30 16:28:49 +01001648 } while (!cpu->unplug || cpu_can_run(cpu));
1649 rcu_unregister_thread();
Vincent Palatinb0cb0a62017-01-10 11:59:57 +01001650 return NULL;
1651}
1652
Sergio Andres Gomez Del Realc97d6d22017-09-13 04:05:09 -05001653/* The HVF-specific vCPU thread function. This one should only run when the host
1654 * CPU supports the VMX "unrestricted guest" feature. */
1655static void *qemu_hvf_cpu_thread_fn(void *arg)
1656{
1657 CPUState *cpu = arg;
1658
1659 int r;
1660
1661 assert(hvf_enabled());
1662
1663 rcu_register_thread();
1664
1665 qemu_mutex_lock_iothread();
1666 qemu_thread_get_self(cpu->thread);
1667
1668 cpu->thread_id = qemu_get_thread_id();
1669 cpu->can_do_io = 1;
1670 current_cpu = cpu;
1671
1672 hvf_init_vcpu(cpu);
1673
1674 /* signal CPU creation */
1675 cpu->created = true;
1676 qemu_cond_signal(&qemu_cpu_cond);
Richard Henderson9c09a252019-03-14 13:06:29 -07001677 qemu_guest_random_seed_thread_part2(cpu->random_seed);
Sergio Andres Gomez Del Realc97d6d22017-09-13 04:05:09 -05001678
1679 do {
1680 if (cpu_can_run(cpu)) {
1681 r = hvf_vcpu_exec(cpu);
1682 if (r == EXCP_DEBUG) {
1683 cpu_handle_guest_debug(cpu);
1684 }
1685 }
Paolo Bonzinidb08b682018-01-11 13:53:12 +01001686 qemu_wait_io_event(cpu);
Sergio Andres Gomez Del Realc97d6d22017-09-13 04:05:09 -05001687 } while (!cpu->unplug || cpu_can_run(cpu));
1688
1689 hvf_vcpu_destroy(cpu);
1690 cpu->created = false;
1691 qemu_cond_signal(&qemu_cpu_cond);
1692 qemu_mutex_unlock_iothread();
Paolo Bonzini8178e632018-01-30 11:05:21 -05001693 rcu_unregister_thread();
Sergio Andres Gomez Del Realc97d6d22017-09-13 04:05:09 -05001694 return NULL;
1695}
1696
Justin Terry (VM)19306802018-01-22 13:07:49 -08001697static void *qemu_whpx_cpu_thread_fn(void *arg)
1698{
1699 CPUState *cpu = arg;
1700 int r;
1701
1702 rcu_register_thread();
1703
1704 qemu_mutex_lock_iothread();
1705 qemu_thread_get_self(cpu->thread);
1706 cpu->thread_id = qemu_get_thread_id();
1707 current_cpu = cpu;
1708
1709 r = whpx_init_vcpu(cpu);
1710 if (r < 0) {
1711 fprintf(stderr, "whpx_init_vcpu failed: %s\n", strerror(-r));
1712 exit(1);
1713 }
1714
1715 /* signal CPU creation */
1716 cpu->created = true;
1717 qemu_cond_signal(&qemu_cpu_cond);
Richard Henderson9c09a252019-03-14 13:06:29 -07001718 qemu_guest_random_seed_thread_part2(cpu->random_seed);
Justin Terry (VM)19306802018-01-22 13:07:49 -08001719
1720 do {
1721 if (cpu_can_run(cpu)) {
1722 r = whpx_vcpu_exec(cpu);
1723 if (r == EXCP_DEBUG) {
1724 cpu_handle_guest_debug(cpu);
1725 }
1726 }
1727 while (cpu_thread_is_idle(cpu)) {
1728 qemu_cond_wait(cpu->halt_cond, &qemu_global_mutex);
1729 }
1730 qemu_wait_io_event_common(cpu);
1731 } while (!cpu->unplug || cpu_can_run(cpu));
1732
1733 whpx_destroy_vcpu(cpu);
1734 cpu->created = false;
1735 qemu_cond_signal(&qemu_cpu_cond);
1736 qemu_mutex_unlock_iothread();
1737 rcu_unregister_thread();
Vincent Palatinb0cb0a62017-01-10 11:59:57 +01001738 return NULL;
1739}
1740
1741#ifdef _WIN32
1742static void CALLBACK dummy_apc_func(ULONG_PTR unused)
1743{
1744}
1745#endif
1746
Alex Bennée37257942017-02-23 18:29:14 +00001747/* Multi-threaded TCG
1748 *
1749 * In the multi-threaded case each vCPU has its own thread. The TLS
1750 * variable current_cpu can be used deep in the code to find the
1751 * current CPUState for a given thread.
1752 */
1753
1754static void *qemu_tcg_cpu_thread_fn(void *arg)
1755{
1756 CPUState *cpu = arg;
1757
Emilio G. Cotaf28d0df2018-06-22 13:45:31 -04001758 assert(tcg_enabled());
Alex Bennéebf51c722017-03-30 18:32:29 +01001759 g_assert(!use_icount);
1760
Alex Bennée37257942017-02-23 18:29:14 +00001761 rcu_register_thread();
Emilio G. Cota3468b592017-07-19 18:57:58 -04001762 tcg_register_thread();
Alex Bennée37257942017-02-23 18:29:14 +00001763
1764 qemu_mutex_lock_iothread();
1765 qemu_thread_get_self(cpu->thread);
1766
1767 cpu->thread_id = qemu_get_thread_id();
1768 cpu->created = true;
1769 cpu->can_do_io = 1;
1770 current_cpu = cpu;
1771 qemu_cond_signal(&qemu_cpu_cond);
Richard Henderson9c09a252019-03-14 13:06:29 -07001772 qemu_guest_random_seed_thread_part2(cpu->random_seed);
Alex Bennée37257942017-02-23 18:29:14 +00001773
1774 /* process any pending work */
1775 cpu->exit_request = 1;
1776
Cédric Le Goater54961aa2018-04-25 15:18:28 +02001777 do {
Alex Bennée37257942017-02-23 18:29:14 +00001778 if (cpu_can_run(cpu)) {
1779 int r;
Alex Bennéed759c952018-02-27 12:52:48 +03001780 qemu_mutex_unlock_iothread();
Alex Bennée37257942017-02-23 18:29:14 +00001781 r = tcg_cpu_exec(cpu);
Alex Bennéed759c952018-02-27 12:52:48 +03001782 qemu_mutex_lock_iothread();
Alex Bennée37257942017-02-23 18:29:14 +00001783 switch (r) {
1784 case EXCP_DEBUG:
1785 cpu_handle_guest_debug(cpu);
1786 break;
1787 case EXCP_HALTED:
1788 /* during start-up the vCPU is reset and the thread is
1789 * kicked several times. If we don't ensure we go back
1790 * to sleep in the halted state we won't cleanly
1791 * start-up when the vCPU is enabled.
1792 *
1793 * cpu->halted should ensure we sleep in wait_io_event
1794 */
1795 g_assert(cpu->halted);
1796 break;
Pranith Kumar08e73c42017-02-23 18:29:15 +00001797 case EXCP_ATOMIC:
1798 qemu_mutex_unlock_iothread();
1799 cpu_exec_step_atomic(cpu);
1800 qemu_mutex_lock_iothread();
Alex Bennée37257942017-02-23 18:29:14 +00001801 default:
1802 /* Ignore everything else? */
1803 break;
1804 }
1805 }
1806
Alex Bennée37257942017-02-23 18:29:14 +00001807 atomic_mb_set(&cpu->exit_request, 0);
Paolo Bonzinidb08b682018-01-11 13:53:12 +01001808 qemu_wait_io_event(cpu);
Paolo Bonzini9b0605f2018-01-30 11:05:06 -05001809 } while (!cpu->unplug || cpu_can_run(cpu));
Alex Bennée37257942017-02-23 18:29:14 +00001810
Paolo Bonzini9b0605f2018-01-30 11:05:06 -05001811 qemu_tcg_destroy_vcpu(cpu);
1812 cpu->created = false;
1813 qemu_cond_signal(&qemu_cpu_cond);
1814 qemu_mutex_unlock_iothread();
1815 rcu_unregister_thread();
Alex Bennée37257942017-02-23 18:29:14 +00001816 return NULL;
1817}
1818
Andreas Färber2ff09a42012-05-03 00:23:30 +02001819static void qemu_cpu_kick_thread(CPUState *cpu)
Paolo Bonzinicc015e92011-03-12 17:44:08 +01001820{
1821#ifndef _WIN32
1822 int err;
1823
Paolo Bonzinie0c38212015-08-26 00:19:19 +02001824 if (cpu->thread_kicked) {
1825 return;
Paolo Bonzini9102ded2015-08-18 06:52:09 -07001826 }
Paolo Bonzinie0c38212015-08-26 00:19:19 +02001827 cpu->thread_kicked = true;
Andreas Färber814e6122012-05-02 17:00:37 +02001828 err = pthread_kill(cpu->thread->thread, SIG_IPI);
Laurent Vivierd455ebc2019-01-02 15:16:03 +01001829 if (err && err != ESRCH) {
Paolo Bonzinicc015e92011-03-12 17:44:08 +01001830 fprintf(stderr, "qemu:%s: %s", __func__, strerror(err));
1831 exit(1);
1832 }
1833#else /* _WIN32 */
Vincent Palatinb0cb0a62017-01-10 11:59:57 +01001834 if (!qemu_cpu_is_self(cpu)) {
Justin Terry (VM)19306802018-01-22 13:07:49 -08001835 if (whpx_enabled()) {
1836 whpx_vcpu_kick(cpu);
1837 } else if (!QueueUserAPC(dummy_apc_func, cpu->hThread, 0)) {
Vincent Palatinb0cb0a62017-01-10 11:59:57 +01001838 fprintf(stderr, "%s: QueueUserAPC failed with error %lu\n",
1839 __func__, GetLastError());
1840 exit(1);
1841 }
1842 }
Paolo Bonzinicc015e92011-03-12 17:44:08 +01001843#endif
1844}
1845
Andreas Färberc08d7422012-05-03 04:34:15 +02001846void qemu_cpu_kick(CPUState *cpu)
Blue Swirl296af7c2010-03-29 19:23:50 +00001847{
Andreas Färberf5c121b2012-05-03 01:22:49 +02001848 qemu_cond_broadcast(cpu->halt_cond);
Paolo Bonzinie0c38212015-08-26 00:19:19 +02001849 if (tcg_enabled()) {
Alex Bennéee8f22f72019-10-01 17:04:26 +01001850 if (qemu_tcg_mttcg_enabled()) {
1851 cpu_exit(cpu);
1852 } else {
1853 qemu_cpu_kick_rr_cpus();
1854 }
Paolo Bonzinie0c38212015-08-26 00:19:19 +02001855 } else {
Vincent Palatinb0cb0a62017-01-10 11:59:57 +01001856 if (hax_enabled()) {
1857 /*
1858 * FIXME: race condition with the exit_request check in
1859 * hax_vcpu_hax_exec
1860 */
1861 cpu->exit_request = 1;
1862 }
Paolo Bonzinie0c38212015-08-26 00:19:19 +02001863 qemu_cpu_kick_thread(cpu);
1864 }
Blue Swirl296af7c2010-03-29 19:23:50 +00001865}
1866
Jan Kiszka46d62fa2011-02-01 22:15:59 +01001867void qemu_cpu_kick_self(void)
1868{
Andreas Färber4917cf42013-05-27 05:17:50 +02001869 assert(current_cpu);
Paolo Bonzini9102ded2015-08-18 06:52:09 -07001870 qemu_cpu_kick_thread(current_cpu);
Blue Swirl296af7c2010-03-29 19:23:50 +00001871}
1872
Andreas Färber60e82572012-05-02 22:23:49 +02001873bool qemu_cpu_is_self(CPUState *cpu)
Blue Swirl296af7c2010-03-29 19:23:50 +00001874{
Andreas Färber814e6122012-05-02 17:00:37 +02001875 return qemu_thread_is_self(cpu->thread);
Blue Swirl296af7c2010-03-29 19:23:50 +00001876}
1877
Paolo Bonzini79e2b9a2015-01-21 12:09:14 +01001878bool qemu_in_vcpu_thread(void)
Juan Quintelaaa723c22012-09-18 16:30:11 +02001879{
Andreas Färber4917cf42013-05-27 05:17:50 +02001880 return current_cpu && qemu_cpu_is_self(current_cpu);
Juan Quintelaaa723c22012-09-18 16:30:11 +02001881}
1882
Paolo Bonziniafbe7052015-06-18 18:47:19 +02001883static __thread bool iothread_locked = false;
1884
1885bool qemu_mutex_iothread_locked(void)
1886{
1887 return iothread_locked;
1888}
1889
Emilio G. Cotacb764d02017-10-28 02:16:41 -04001890/*
1891 * The BQL is taken from so many places that it is worth profiling the
1892 * callers directly, instead of funneling them all through a single function.
1893 */
1894void qemu_mutex_lock_iothread_impl(const char *file, int line)
Blue Swirl296af7c2010-03-29 19:23:50 +00001895{
Emilio G. Cotacb764d02017-10-28 02:16:41 -04001896 QemuMutexLockFunc bql_lock = atomic_read(&qemu_bql_mutex_lock_func);
1897
Jan Kiszka8d04fb52017-02-23 18:29:11 +00001898 g_assert(!qemu_mutex_iothread_locked());
Emilio G. Cotacb764d02017-10-28 02:16:41 -04001899 bql_lock(&qemu_global_mutex, file, line);
Paolo Bonziniafbe7052015-06-18 18:47:19 +02001900 iothread_locked = true;
Blue Swirl296af7c2010-03-29 19:23:50 +00001901}
1902
1903void qemu_mutex_unlock_iothread(void)
1904{
Jan Kiszka8d04fb52017-02-23 18:29:11 +00001905 g_assert(qemu_mutex_iothread_locked());
Paolo Bonziniafbe7052015-06-18 18:47:19 +02001906 iothread_locked = false;
Blue Swirl296af7c2010-03-29 19:23:50 +00001907 qemu_mutex_unlock(&qemu_global_mutex);
1908}
1909
Alex Bennéee8faee02016-10-27 16:09:58 +01001910static bool all_vcpus_paused(void)
Blue Swirl296af7c2010-03-29 19:23:50 +00001911{
Andreas Färberbdc44642013-06-24 23:50:24 +02001912 CPUState *cpu;
Blue Swirl296af7c2010-03-29 19:23:50 +00001913
Andreas Färberbdc44642013-06-24 23:50:24 +02001914 CPU_FOREACH(cpu) {
Andreas Färber182735e2013-05-29 22:29:20 +02001915 if (!cpu->stopped) {
Alex Bennéee8faee02016-10-27 16:09:58 +01001916 return false;
Jan Kiszka0ab07c62011-02-07 12:19:14 +01001917 }
Blue Swirl296af7c2010-03-29 19:23:50 +00001918 }
1919
Alex Bennéee8faee02016-10-27 16:09:58 +01001920 return true;
Blue Swirl296af7c2010-03-29 19:23:50 +00001921}
1922
1923void pause_all_vcpus(void)
1924{
Andreas Färberbdc44642013-06-24 23:50:24 +02001925 CPUState *cpu;
Blue Swirl296af7c2010-03-29 19:23:50 +00001926
Alex Bligh40daca52013-08-21 16:03:02 +01001927 qemu_clock_enable(QEMU_CLOCK_VIRTUAL, false);
Andreas Färberbdc44642013-06-24 23:50:24 +02001928 CPU_FOREACH(cpu) {
David Hildenbrandebd05fe2017-11-29 20:12:15 +01001929 if (qemu_cpu_is_self(cpu)) {
1930 qemu_cpu_stop(cpu, true);
1931 } else {
1932 cpu->stop = true;
1933 qemu_cpu_kick(cpu);
1934 }
Jan Kiszkad798e972012-02-17 18:31:16 +01001935 }
1936
Alex Bennéed759c952018-02-27 12:52:48 +03001937 /* We need to drop the replay_lock so any vCPU threads woken up
1938 * can finish their replay tasks
1939 */
1940 replay_mutex_unlock();
1941
Blue Swirl296af7c2010-03-29 19:23:50 +00001942 while (!all_vcpus_paused()) {
Paolo Bonzinibe7d6c52011-03-12 17:44:02 +01001943 qemu_cond_wait(&qemu_pause_cond, &qemu_global_mutex);
Andreas Färberbdc44642013-06-24 23:50:24 +02001944 CPU_FOREACH(cpu) {
Andreas Färber182735e2013-05-29 22:29:20 +02001945 qemu_cpu_kick(cpu);
Blue Swirl296af7c2010-03-29 19:23:50 +00001946 }
1947 }
Alex Bennéed759c952018-02-27 12:52:48 +03001948
1949 qemu_mutex_unlock_iothread();
1950 replay_mutex_lock();
1951 qemu_mutex_lock_iothread();
Blue Swirl296af7c2010-03-29 19:23:50 +00001952}
1953
Igor Mammedov29936832013-04-23 10:29:37 +02001954void cpu_resume(CPUState *cpu)
1955{
1956 cpu->stop = false;
1957 cpu->stopped = false;
1958 qemu_cpu_kick(cpu);
1959}
1960
Blue Swirl296af7c2010-03-29 19:23:50 +00001961void resume_all_vcpus(void)
1962{
Andreas Färberbdc44642013-06-24 23:50:24 +02001963 CPUState *cpu;
Blue Swirl296af7c2010-03-29 19:23:50 +00001964
Alex Bligh40daca52013-08-21 16:03:02 +01001965 qemu_clock_enable(QEMU_CLOCK_VIRTUAL, true);
Andreas Färberbdc44642013-06-24 23:50:24 +02001966 CPU_FOREACH(cpu) {
Andreas Färber182735e2013-05-29 22:29:20 +02001967 cpu_resume(cpu);
Blue Swirl296af7c2010-03-29 19:23:50 +00001968 }
1969}
1970
Paolo Bonzinidbadee42018-01-30 16:40:12 +01001971void cpu_remove_sync(CPUState *cpu)
Gu Zheng4c055ab2016-05-12 09:18:13 +05301972{
1973 cpu->stop = true;
1974 cpu->unplug = true;
1975 qemu_cpu_kick(cpu);
Paolo Bonzinidbadee42018-01-30 16:40:12 +01001976 qemu_mutex_unlock_iothread();
1977 qemu_thread_join(cpu->thread);
1978 qemu_mutex_lock_iothread();
Bharata B Rao2c579042016-05-12 09:18:14 +05301979}
1980
Dr. David Alan Gilbert49001162014-01-30 10:20:32 +00001981/* For temporary buffers for forming a name */
1982#define VCPU_THREAD_NAME_SIZE 16
1983
Andreas Färbere5ab30a2012-05-03 01:50:44 +02001984static void qemu_tcg_init_vcpu(CPUState *cpu)
Blue Swirl296af7c2010-03-29 19:23:50 +00001985{
Dr. David Alan Gilbert49001162014-01-30 10:20:32 +00001986 char thread_name[VCPU_THREAD_NAME_SIZE];
Alex Bennée37257942017-02-23 18:29:14 +00001987 static QemuCond *single_tcg_halt_cond;
1988 static QemuThread *single_tcg_cpu_thread;
Emilio G. Cotae8feb962017-07-07 19:24:20 -04001989 static int tcg_region_inited;
1990
Emilio G. Cotaf28d0df2018-06-22 13:45:31 -04001991 assert(tcg_enabled());
Emilio G. Cotae8feb962017-07-07 19:24:20 -04001992 /*
1993 * Initialize TCG regions--once. Now is a good time, because:
1994 * (1) TCG's init context, prologue and target globals have been set up.
1995 * (2) qemu_tcg_mttcg_enabled() works now (TCG init code runs before the
1996 * -accel flag is processed, so the check doesn't work then).
1997 */
1998 if (!tcg_region_inited) {
1999 tcg_region_inited = 1;
2000 tcg_region_init();
2001 }
Dr. David Alan Gilbert49001162014-01-30 10:20:32 +00002002
Alex Bennée37257942017-02-23 18:29:14 +00002003 if (qemu_tcg_mttcg_enabled() || !single_tcg_cpu_thread) {
Andreas Färber814e6122012-05-02 17:00:37 +02002004 cpu->thread = g_malloc0(sizeof(QemuThread));
Andreas Färberf5c121b2012-05-03 01:22:49 +02002005 cpu->halt_cond = g_malloc0(sizeof(QemuCond));
2006 qemu_cond_init(cpu->halt_cond);
Alex Bennée37257942017-02-23 18:29:14 +00002007
2008 if (qemu_tcg_mttcg_enabled()) {
2009 /* create a thread per vCPU with TCG (MTTCG) */
2010 parallel_cpus = true;
2011 snprintf(thread_name, VCPU_THREAD_NAME_SIZE, "CPU %d/TCG",
Dr. David Alan Gilbert49001162014-01-30 10:20:32 +00002012 cpu->cpu_index);
Alex Bennée37257942017-02-23 18:29:14 +00002013
2014 qemu_thread_create(cpu->thread, thread_name, qemu_tcg_cpu_thread_fn,
2015 cpu, QEMU_THREAD_JOINABLE);
2016
2017 } else {
2018 /* share a single thread for all cpus with TCG */
2019 snprintf(thread_name, VCPU_THREAD_NAME_SIZE, "ALL CPUs/TCG");
2020 qemu_thread_create(cpu->thread, thread_name,
2021 qemu_tcg_rr_cpu_thread_fn,
2022 cpu, QEMU_THREAD_JOINABLE);
2023
2024 single_tcg_halt_cond = cpu->halt_cond;
2025 single_tcg_cpu_thread = cpu->thread;
2026 }
Paolo Bonzini1ecf47b2011-12-13 13:43:52 +01002027#ifdef _WIN32
Andreas Färber814e6122012-05-02 17:00:37 +02002028 cpu->hThread = qemu_thread_get_handle(cpu->thread);
Paolo Bonzini1ecf47b2011-12-13 13:43:52 +01002029#endif
Blue Swirl296af7c2010-03-29 19:23:50 +00002030 } else {
Alex Bennée37257942017-02-23 18:29:14 +00002031 /* For non-MTTCG cases we share the thread */
2032 cpu->thread = single_tcg_cpu_thread;
2033 cpu->halt_cond = single_tcg_halt_cond;
David Hildenbranda3421732018-02-09 20:52:37 +01002034 cpu->thread_id = first_cpu->thread_id;
2035 cpu->can_do_io = 1;
2036 cpu->created = true;
Blue Swirl296af7c2010-03-29 19:23:50 +00002037 }
2038}
2039
Vincent Palatinb0cb0a62017-01-10 11:59:57 +01002040static void qemu_hax_start_vcpu(CPUState *cpu)
2041{
2042 char thread_name[VCPU_THREAD_NAME_SIZE];
2043
2044 cpu->thread = g_malloc0(sizeof(QemuThread));
2045 cpu->halt_cond = g_malloc0(sizeof(QemuCond));
2046 qemu_cond_init(cpu->halt_cond);
2047
2048 snprintf(thread_name, VCPU_THREAD_NAME_SIZE, "CPU %d/HAX",
2049 cpu->cpu_index);
2050 qemu_thread_create(cpu->thread, thread_name, qemu_hax_cpu_thread_fn,
2051 cpu, QEMU_THREAD_JOINABLE);
2052#ifdef _WIN32
2053 cpu->hThread = qemu_thread_get_handle(cpu->thread);
2054#endif
Vincent Palatinb0cb0a62017-01-10 11:59:57 +01002055}
2056
Andreas Färber48a106b2013-05-27 02:20:39 +02002057static void qemu_kvm_start_vcpu(CPUState *cpu)
Blue Swirl296af7c2010-03-29 19:23:50 +00002058{
Dr. David Alan Gilbert49001162014-01-30 10:20:32 +00002059 char thread_name[VCPU_THREAD_NAME_SIZE];
2060
Andreas Färber814e6122012-05-02 17:00:37 +02002061 cpu->thread = g_malloc0(sizeof(QemuThread));
Andreas Färberf5c121b2012-05-03 01:22:49 +02002062 cpu->halt_cond = g_malloc0(sizeof(QemuCond));
2063 qemu_cond_init(cpu->halt_cond);
Dr. David Alan Gilbert49001162014-01-30 10:20:32 +00002064 snprintf(thread_name, VCPU_THREAD_NAME_SIZE, "CPU %d/KVM",
2065 cpu->cpu_index);
2066 qemu_thread_create(cpu->thread, thread_name, qemu_kvm_cpu_thread_fn,
2067 cpu, QEMU_THREAD_JOINABLE);
Blue Swirl296af7c2010-03-29 19:23:50 +00002068}
2069
Sergio Andres Gomez Del Realc97d6d22017-09-13 04:05:09 -05002070static void qemu_hvf_start_vcpu(CPUState *cpu)
2071{
2072 char thread_name[VCPU_THREAD_NAME_SIZE];
2073
2074 /* HVF currently does not support TCG, and only runs in
2075 * unrestricted-guest mode. */
2076 assert(hvf_enabled());
2077
2078 cpu->thread = g_malloc0(sizeof(QemuThread));
2079 cpu->halt_cond = g_malloc0(sizeof(QemuCond));
2080 qemu_cond_init(cpu->halt_cond);
2081
2082 snprintf(thread_name, VCPU_THREAD_NAME_SIZE, "CPU %d/HVF",
2083 cpu->cpu_index);
2084 qemu_thread_create(cpu->thread, thread_name, qemu_hvf_cpu_thread_fn,
2085 cpu, QEMU_THREAD_JOINABLE);
Sergio Andres Gomez Del Realc97d6d22017-09-13 04:05:09 -05002086}
2087
Justin Terry (VM)19306802018-01-22 13:07:49 -08002088static void qemu_whpx_start_vcpu(CPUState *cpu)
2089{
2090 char thread_name[VCPU_THREAD_NAME_SIZE];
2091
2092 cpu->thread = g_malloc0(sizeof(QemuThread));
2093 cpu->halt_cond = g_malloc0(sizeof(QemuCond));
2094 qemu_cond_init(cpu->halt_cond);
2095 snprintf(thread_name, VCPU_THREAD_NAME_SIZE, "CPU %d/WHPX",
2096 cpu->cpu_index);
2097 qemu_thread_create(cpu->thread, thread_name, qemu_whpx_cpu_thread_fn,
2098 cpu, QEMU_THREAD_JOINABLE);
2099#ifdef _WIN32
2100 cpu->hThread = qemu_thread_get_handle(cpu->thread);
2101#endif
Justin Terry (VM)19306802018-01-22 13:07:49 -08002102}
2103
Andreas Färber10a90212013-05-27 02:24:35 +02002104static void qemu_dummy_start_vcpu(CPUState *cpu)
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +02002105{
Dr. David Alan Gilbert49001162014-01-30 10:20:32 +00002106 char thread_name[VCPU_THREAD_NAME_SIZE];
2107
Andreas Färber814e6122012-05-02 17:00:37 +02002108 cpu->thread = g_malloc0(sizeof(QemuThread));
Andreas Färberf5c121b2012-05-03 01:22:49 +02002109 cpu->halt_cond = g_malloc0(sizeof(QemuCond));
2110 qemu_cond_init(cpu->halt_cond);
Dr. David Alan Gilbert49001162014-01-30 10:20:32 +00002111 snprintf(thread_name, VCPU_THREAD_NAME_SIZE, "CPU %d/DUMMY",
2112 cpu->cpu_index);
2113 qemu_thread_create(cpu->thread, thread_name, qemu_dummy_cpu_thread_fn, cpu,
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +02002114 QEMU_THREAD_JOINABLE);
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +02002115}
2116
Andreas Färberc643bed2013-05-27 03:23:24 +02002117void qemu_init_vcpu(CPUState *cpu)
Blue Swirl296af7c2010-03-29 19:23:50 +00002118{
Like Xu5cc87672019-05-19 04:54:21 +08002119 MachineState *ms = MACHINE(qdev_get_machine());
2120
2121 cpu->nr_cores = ms->smp.cores;
2122 cpu->nr_threads = ms->smp.threads;
Andreas Färberf324e762012-05-02 23:26:21 +02002123 cpu->stopped = true;
Richard Henderson9c09a252019-03-14 13:06:29 -07002124 cpu->random_seed = qemu_guest_random_seed_thread_part1();
Peter Maydell56943e82016-01-21 14:15:04 +00002125
2126 if (!cpu->as) {
2127 /* If the target cpu hasn't set up any address spaces itself,
2128 * give it the default one.
2129 */
Peter Maydell12ebc9a2016-01-21 14:15:04 +00002130 cpu->num_ases = 1;
Peter Xu80ceb072017-11-23 17:23:32 +08002131 cpu_address_space_init(cpu, 0, "cpu-memory", cpu->memory);
Peter Maydell56943e82016-01-21 14:15:04 +00002132 }
2133
Jan Kiszka0ab07c62011-02-07 12:19:14 +01002134 if (kvm_enabled()) {
Andreas Färber48a106b2013-05-27 02:20:39 +02002135 qemu_kvm_start_vcpu(cpu);
Vincent Palatinb0cb0a62017-01-10 11:59:57 +01002136 } else if (hax_enabled()) {
2137 qemu_hax_start_vcpu(cpu);
Sergio Andres Gomez Del Realc97d6d22017-09-13 04:05:09 -05002138 } else if (hvf_enabled()) {
2139 qemu_hvf_start_vcpu(cpu);
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +02002140 } else if (tcg_enabled()) {
Andreas Färbere5ab30a2012-05-03 01:50:44 +02002141 qemu_tcg_init_vcpu(cpu);
Justin Terry (VM)19306802018-01-22 13:07:49 -08002142 } else if (whpx_enabled()) {
2143 qemu_whpx_start_vcpu(cpu);
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +02002144 } else {
Andreas Färber10a90212013-05-27 02:24:35 +02002145 qemu_dummy_start_vcpu(cpu);
Jan Kiszka0ab07c62011-02-07 12:19:14 +01002146 }
David Hildenbrand81e96312018-02-09 20:52:38 +01002147
2148 while (!cpu->created) {
2149 qemu_cond_wait(&qemu_cpu_cond, &qemu_global_mutex);
2150 }
Blue Swirl296af7c2010-03-29 19:23:50 +00002151}
2152
Jan Kiszkab4a3d962011-02-01 22:15:43 +01002153void cpu_stop_current(void)
Blue Swirl296af7c2010-03-29 19:23:50 +00002154{
Andreas Färber4917cf42013-05-27 05:17:50 +02002155 if (current_cpu) {
Peter Maydell0ec7e672019-01-07 15:23:47 +00002156 current_cpu->stop = true;
2157 cpu_exit(current_cpu);
Jan Kiszkab4a3d962011-02-01 22:15:43 +01002158 }
Blue Swirl296af7c2010-03-29 19:23:50 +00002159}
2160
Kevin Wolf56983462013-07-05 13:49:54 +02002161int vm_stop(RunState state)
Blue Swirl296af7c2010-03-29 19:23:50 +00002162{
Juan Quintelaaa723c22012-09-18 16:30:11 +02002163 if (qemu_in_vcpu_thread()) {
Paolo Bonzini74892d22014-06-05 14:53:58 +02002164 qemu_system_vmstop_request_prepare();
Luiz Capitulino1dfb4dd2011-07-29 14:26:33 -03002165 qemu_system_vmstop_request(state);
Blue Swirl296af7c2010-03-29 19:23:50 +00002166 /*
2167 * FIXME: should not return to device code in case
2168 * vm_stop() has been requested.
2169 */
Jan Kiszkab4a3d962011-02-01 22:15:43 +01002170 cpu_stop_current();
Kevin Wolf56983462013-07-05 13:49:54 +02002171 return 0;
Blue Swirl296af7c2010-03-29 19:23:50 +00002172 }
Kevin Wolf56983462013-07-05 13:49:54 +02002173
Stefan Hajnoczi4486e892018-03-07 14:42:05 +00002174 return do_vm_stop(state, true);
Blue Swirl296af7c2010-03-29 19:23:50 +00002175}
2176
Claudio Imbrenda2d76e822017-02-14 18:07:47 +01002177/**
2178 * Prepare for (re)starting the VM.
2179 * Returns -1 if the vCPUs are not to be restarted (e.g. if they are already
2180 * running or in case of an error condition), 0 otherwise.
2181 */
2182int vm_prepare_start(void)
2183{
2184 RunState requested;
Claudio Imbrenda2d76e822017-02-14 18:07:47 +01002185
2186 qemu_vmstop_requested(&requested);
2187 if (runstate_is_running() && requested == RUN_STATE__MAX) {
2188 return -1;
2189 }
2190
2191 /* Ensure that a STOP/RESUME pair of events is emitted if a
2192 * vmstop request was pending. The BLOCK_IO_ERROR event, for
2193 * example, according to documentation is always followed by
2194 * the STOP event.
2195 */
2196 if (runstate_is_running()) {
Peter Xu3ab72382018-08-15 21:37:37 +08002197 qapi_event_send_stop();
2198 qapi_event_send_resume();
Markus Armbrusterf0561582018-04-23 10:45:18 +02002199 return -1;
Claudio Imbrenda2d76e822017-02-14 18:07:47 +01002200 }
2201
2202 /* We are sending this now, but the CPUs will be resumed shortly later */
Peter Xu3ab72382018-08-15 21:37:37 +08002203 qapi_event_send_resume();
Markus Armbrusterf0561582018-04-23 10:45:18 +02002204
Markus Armbrusterf0561582018-04-23 10:45:18 +02002205 cpu_enable_ticks();
2206 runstate_set(RUN_STATE_RUNNING);
2207 vm_state_notify(1, RUN_STATE_RUNNING);
2208 return 0;
Claudio Imbrenda2d76e822017-02-14 18:07:47 +01002209}
2210
2211void vm_start(void)
2212{
2213 if (!vm_prepare_start()) {
2214 resume_all_vcpus();
2215 }
2216}
2217
Luiz Capitulino8a9236f2011-10-14 11:18:09 -03002218/* does a state transition even if the VM is already stopped,
2219 current state is forgotten forever */
Kevin Wolf56983462013-07-05 13:49:54 +02002220int vm_stop_force_state(RunState state)
Luiz Capitulino8a9236f2011-10-14 11:18:09 -03002221{
2222 if (runstate_is_running()) {
Kevin Wolf56983462013-07-05 13:49:54 +02002223 return vm_stop(state);
Luiz Capitulino8a9236f2011-10-14 11:18:09 -03002224 } else {
2225 runstate_set(state);
Wen Congyangb2780d32015-11-20 17:34:38 +08002226
2227 bdrv_drain_all();
Kevin Wolf594a45c2013-07-18 14:52:19 +02002228 /* Make sure to return an error if the flush in a previous vm_stop()
2229 * failed. */
John Snow22af08e2016-09-22 21:45:51 -04002230 return bdrv_flush_all();
Luiz Capitulino8a9236f2011-10-14 11:18:09 -03002231 }
2232}
2233
Markus Armbruster04424282019-04-17 21:17:57 +02002234void list_cpus(const char *optarg)
Blue Swirl262353c2010-05-04 19:55:35 +00002235{
2236 /* XXX: implement xxx_cpu_list for targets that still miss it */
Peter Maydelle916cbf2012-09-05 17:41:08 -03002237#if defined(cpu_list)
Markus Armbruster04424282019-04-17 21:17:57 +02002238 cpu_list();
Blue Swirl262353c2010-05-04 19:55:35 +00002239#endif
2240}
Luiz Capitulinode0b36b2011-09-21 16:38:35 -03002241
Luiz Capitulino0cfd6a92011-11-22 16:32:37 -02002242void qmp_memsave(int64_t addr, int64_t size, const char *filename,
2243 bool has_cpu, int64_t cpu_index, Error **errp)
2244{
2245 FILE *f;
2246 uint32_t l;
Andreas Färber55e5c282012-12-17 06:18:02 +01002247 CPUState *cpu;
Luiz Capitulino0cfd6a92011-11-22 16:32:37 -02002248 uint8_t buf[1024];
Borislav Petkov0dc9daf2015-02-08 13:14:38 +01002249 int64_t orig_addr = addr, orig_size = size;
Luiz Capitulino0cfd6a92011-11-22 16:32:37 -02002250
2251 if (!has_cpu) {
2252 cpu_index = 0;
2253 }
2254
Andreas Färber151d1322013-02-15 15:41:49 +01002255 cpu = qemu_get_cpu(cpu_index);
2256 if (cpu == NULL) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01002257 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "cpu-index",
2258 "a CPU number");
Luiz Capitulino0cfd6a92011-11-22 16:32:37 -02002259 return;
2260 }
2261
2262 f = fopen(filename, "wb");
2263 if (!f) {
Luiz Capitulino618da852013-06-07 14:35:06 -04002264 error_setg_file_open(errp, errno, filename);
Luiz Capitulino0cfd6a92011-11-22 16:32:37 -02002265 return;
2266 }
2267
2268 while (size != 0) {
2269 l = sizeof(buf);
2270 if (l > size)
2271 l = size;
Aneesh Kumar K.V2f4d0f52013-10-01 21:49:30 +05302272 if (cpu_memory_rw_debug(cpu, addr, buf, l, 0) != 0) {
Borislav Petkov0dc9daf2015-02-08 13:14:38 +01002273 error_setg(errp, "Invalid addr 0x%016" PRIx64 "/size %" PRId64
2274 " specified", orig_addr, orig_size);
Aneesh Kumar K.V2f4d0f52013-10-01 21:49:30 +05302275 goto exit;
2276 }
Luiz Capitulino0cfd6a92011-11-22 16:32:37 -02002277 if (fwrite(buf, 1, l, f) != l) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01002278 error_setg(errp, QERR_IO_ERROR);
Luiz Capitulino0cfd6a92011-11-22 16:32:37 -02002279 goto exit;
2280 }
2281 addr += l;
2282 size -= l;
2283 }
2284
2285exit:
2286 fclose(f);
2287}
Luiz Capitulino6d3962b2011-11-22 17:26:46 -02002288
2289void qmp_pmemsave(int64_t addr, int64_t size, const char *filename,
2290 Error **errp)
2291{
2292 FILE *f;
2293 uint32_t l;
2294 uint8_t buf[1024];
2295
2296 f = fopen(filename, "wb");
2297 if (!f) {
Luiz Capitulino618da852013-06-07 14:35:06 -04002298 error_setg_file_open(errp, errno, filename);
Luiz Capitulino6d3962b2011-11-22 17:26:46 -02002299 return;
2300 }
2301
2302 while (size != 0) {
2303 l = sizeof(buf);
2304 if (l > size)
2305 l = size;
Stefan Weileb6282f2014-04-07 20:28:23 +02002306 cpu_physical_memory_read(addr, buf, l);
Luiz Capitulino6d3962b2011-11-22 17:26:46 -02002307 if (fwrite(buf, 1, l, f) != l) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01002308 error_setg(errp, QERR_IO_ERROR);
Luiz Capitulino6d3962b2011-11-22 17:26:46 -02002309 goto exit;
2310 }
2311 addr += l;
2312 size -= l;
2313 }
2314
2315exit:
2316 fclose(f);
2317}
Luiz Capitulinoab49ab52011-11-23 12:55:53 -02002318
2319void qmp_inject_nmi(Error **errp)
2320{
Alexey Kardashevskiy9cb805f2014-08-20 22:16:33 +10002321 nmi_monitor_handle(monitor_get_cpu_index(), errp);
Luiz Capitulinoab49ab52011-11-23 12:55:53 -02002322}
Sebastian Tanase27498be2014-07-25 11:56:33 +02002323
Markus Armbruster76c86612019-04-17 21:17:53 +02002324void dump_drift_info(void)
Sebastian Tanase27498be2014-07-25 11:56:33 +02002325{
2326 if (!use_icount) {
2327 return;
2328 }
2329
Markus Armbruster76c86612019-04-17 21:17:53 +02002330 qemu_printf("Host - Guest clock %"PRIi64" ms\n",
Sebastian Tanase27498be2014-07-25 11:56:33 +02002331 (cpu_get_clock() - cpu_get_icount())/SCALE_MS);
2332 if (icount_align_option) {
Markus Armbruster76c86612019-04-17 21:17:53 +02002333 qemu_printf("Max guest delay %"PRIi64" ms\n",
2334 -max_delay / SCALE_MS);
2335 qemu_printf("Max guest advance %"PRIi64" ms\n",
2336 max_advance / SCALE_MS);
Sebastian Tanase27498be2014-07-25 11:56:33 +02002337 } else {
Markus Armbruster76c86612019-04-17 21:17:53 +02002338 qemu_printf("Max guest delay NA\n");
2339 qemu_printf("Max guest advance NA\n");
Sebastian Tanase27498be2014-07-25 11:56:33 +02002340 }
2341}