blob: e70cc58e316fe90634800a5f0c5814db170ea819 [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"
Paolo Bonzini9c17d612012-12-17 18:20:04 +010048#include "sysemu/cpus.h"
49#include "sysemu/qtest.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010050#include "qemu/main-loop.h"
Markus Armbruster922a01a2018-02-01 12:18:46 +010051#include "qemu/option.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010052#include "qemu/bitmap.h"
Liu Ping Fancb365642013-09-25 14:20:58 +080053#include "qemu/seqlock.h"
Richard Henderson9c09a252019-03-14 13:06:29 -070054#include "qemu/guest-random.h"
KONRAD Frederic8d4e9142017-02-23 18:29:08 +000055#include "tcg.h"
Alexey Kardashevskiy9cb805f2014-08-20 22:16:33 +100056#include "hw/nmi.h"
Pavel Dovgalyuk8b427042015-09-17 19:24:05 +030057#include "sysemu/replay.h"
Like Xu5cc87672019-05-19 04:54:21 +080058#include "hw/boards.h"
Markus Armbruster650d1032019-08-12 07:23:48 +020059#include "hw/hw.h"
Jan Kiszka0ff0fc12011-06-23 10:15:55 +020060
Jan Kiszka6d9cb732011-02-01 22:15:58 +010061#ifdef CONFIG_LINUX
62
63#include <sys/prctl.h>
64
Marcelo Tosattic0532a72010-10-11 15:31:21 -030065#ifndef PR_MCE_KILL
66#define PR_MCE_KILL 33
67#endif
68
Jan Kiszka6d9cb732011-02-01 22:15:58 +010069#ifndef PR_MCE_KILL_SET
70#define PR_MCE_KILL_SET 1
71#endif
72
73#ifndef PR_MCE_KILL_EARLY
74#define PR_MCE_KILL_EARLY 1
75#endif
76
77#endif /* CONFIG_LINUX */
78
Sebastian Tanase27498be2014-07-25 11:56:33 +020079int64_t max_delay;
80int64_t max_advance;
Blue Swirl296af7c2010-03-29 19:23:50 +000081
Jason J. Herne2adcc852015-09-08 13:12:33 -040082/* vcpu throttling controls */
83static QEMUTimer *throttle_timer;
84static unsigned int throttle_percentage;
85
86#define CPU_THROTTLE_PCT_MIN 1
87#define CPU_THROTTLE_PCT_MAX 99
88#define CPU_THROTTLE_TIMESLICE_NS 10000000
89
Tiejun Chen321bc0b2013-08-02 09:43:09 +080090bool cpu_is_stopped(CPUState *cpu)
91{
92 return cpu->stopped || !runstate_is_running();
93}
94
Andreas Färbera98ae1d2013-05-26 23:21:08 +020095static bool cpu_thread_is_idle(CPUState *cpu)
Peter Maydellac873f12012-07-19 16:52:27 +010096{
Andreas Färberc64ca812012-05-03 02:11:45 +020097 if (cpu->stop || cpu->queued_work_first) {
Peter Maydellac873f12012-07-19 16:52:27 +010098 return false;
99 }
Tiejun Chen321bc0b2013-08-02 09:43:09 +0800100 if (cpu_is_stopped(cpu)) {
Peter Maydellac873f12012-07-19 16:52:27 +0100101 return true;
102 }
Andreas Färber8c2e1b02013-08-25 18:53:55 +0200103 if (!cpu->halted || cpu_has_work(cpu) ||
Alexander Graf215e79c2013-04-24 22:24:12 +0200104 kvm_halt_in_kernel()) {
Peter Maydellac873f12012-07-19 16:52:27 +0100105 return false;
106 }
107 return true;
108}
109
110static bool all_cpu_threads_idle(void)
111{
Andreas Färber182735e2013-05-29 22:29:20 +0200112 CPUState *cpu;
Peter Maydellac873f12012-07-19 16:52:27 +0100113
Andreas Färberbdc44642013-06-24 23:50:24 +0200114 CPU_FOREACH(cpu) {
Andreas Färber182735e2013-05-29 22:29:20 +0200115 if (!cpu_thread_is_idle(cpu)) {
Peter Maydellac873f12012-07-19 16:52:27 +0100116 return false;
117 }
118 }
119 return true;
120}
121
Blue Swirl296af7c2010-03-29 19:23:50 +0000122/***********************************************************/
Paolo Bonzini946fb272011-09-12 13:57:37 +0200123/* guest cycle counter */
124
Paolo Bonzinia3270e12013-10-07 17:18:15 +0200125/* Protected by TimersState seqlock */
126
Victor CLEMENT5045e9d92015-05-29 17:14:04 +0200127static bool icount_sleep = true;
Paolo Bonzini946fb272011-09-12 13:57:37 +0200128/* Arbitrarily pick 1MIPS as the minimum allowable speed. */
129#define MAX_ICOUNT_SHIFT 10
Paolo Bonzinia3270e12013-10-07 17:18:15 +0200130
Paolo Bonzini946fb272011-09-12 13:57:37 +0200131typedef struct TimersState {
Liu Ping Fancb365642013-09-25 14:20:58 +0800132 /* Protected by BQL. */
Paolo Bonzini946fb272011-09-12 13:57:37 +0200133 int64_t cpu_ticks_prev;
134 int64_t cpu_ticks_offset;
Liu Ping Fancb365642013-09-25 14:20:58 +0800135
Paolo Bonzini94377112018-08-14 09:57:16 +0200136 /* Protect fields that can be respectively read outside the
137 * BQL, and written from multiple threads.
Liu Ping Fancb365642013-09-25 14:20:58 +0800138 */
139 QemuSeqLock vm_clock_seqlock;
Paolo Bonzini94377112018-08-14 09:57:16 +0200140 QemuSpin vm_clock_lock;
141
142 int16_t cpu_ticks_enabled;
KONRAD Fredericc96778b2014-08-01 01:37:09 +0200143
Paolo Bonzinic1ff0732018-08-14 09:31:58 +0200144 /* Conversion factor from emulated instructions to virtual clock ticks. */
Paolo Bonzini94377112018-08-14 09:57:16 +0200145 int16_t icount_time_shift;
146
KONRAD Fredericc96778b2014-08-01 01:37:09 +0200147 /* Compensate for varying guest execution speed. */
148 int64_t qemu_icount_bias;
Paolo Bonzini94377112018-08-14 09:57:16 +0200149
150 int64_t vm_clock_warp_start;
151 int64_t cpu_clock_offset;
152
KONRAD Fredericc96778b2014-08-01 01:37:09 +0200153 /* Only written by TCG thread */
154 int64_t qemu_icount;
Paolo Bonzini94377112018-08-14 09:57:16 +0200155
Pavel Dovgalyukb39e3f32018-01-11 11:26:10 +0300156 /* for adjusting icount */
Pavel Dovgalyukb39e3f32018-01-11 11:26:10 +0300157 QEMUTimer *icount_rt_timer;
158 QEMUTimer *icount_vm_timer;
159 QEMUTimer *icount_warp_timer;
Paolo Bonzini946fb272011-09-12 13:57:37 +0200160} TimersState;
161
Liu Ping Fand9cd4002013-07-21 08:43:00 +0000162static TimersState timers_state;
KONRAD Frederic8d4e9142017-02-23 18:29:08 +0000163bool mttcg_enabled;
164
165/*
166 * We default to false if we know other options have been enabled
167 * which are currently incompatible with MTTCG. Otherwise when each
168 * guest (target) has been updated to support:
169 * - atomic instructions
170 * - memory ordering primitives (barriers)
171 * they can set the appropriate CONFIG flags in ${target}-softmmu.mak
172 *
173 * Once a guest architecture has been converted to the new primitives
174 * there are two remaining limitations to check.
175 *
176 * - The guest can't be oversized (e.g. 64 bit guest on 32 bit host)
177 * - The host must have a stronger memory order than the guest
178 *
179 * It may be possible in future to support strong guests on weak hosts
180 * but that will require tagging all load/stores in a guest with their
181 * implicit memory order requirements which would likely slow things
182 * down a lot.
183 */
184
185static bool check_tcg_memory_orders_compatible(void)
186{
187#if defined(TCG_GUEST_DEFAULT_MO) && defined(TCG_TARGET_DEFAULT_MO)
188 return (TCG_GUEST_DEFAULT_MO & ~TCG_TARGET_DEFAULT_MO) == 0;
189#else
190 return false;
191#endif
192}
193
194static bool default_mttcg_enabled(void)
195{
Alex Bennée83fd9622017-02-27 17:09:01 +0000196 if (use_icount || TCG_OVERSIZED_GUEST) {
KONRAD Frederic8d4e9142017-02-23 18:29:08 +0000197 return false;
198 } else {
199#ifdef TARGET_SUPPORTS_MTTCG
200 return check_tcg_memory_orders_compatible();
201#else
202 return false;
203#endif
204 }
205}
206
207void qemu_tcg_configure(QemuOpts *opts, Error **errp)
208{
209 const char *t = qemu_opt_get(opts, "thread");
210 if (t) {
211 if (strcmp(t, "multi") == 0) {
212 if (TCG_OVERSIZED_GUEST) {
213 error_setg(errp, "No MTTCG when guest word size > hosts");
Alex Bennée83fd9622017-02-27 17:09:01 +0000214 } else if (use_icount) {
215 error_setg(errp, "No MTTCG when icount is enabled");
KONRAD Frederic8d4e9142017-02-23 18:29:08 +0000216 } else {
Nikunj A Dadhania86953502017-04-10 11:36:55 +0530217#ifndef TARGET_SUPPORTS_MTTCG
Markus Armbruster07656912018-10-17 10:26:28 +0200218 warn_report("Guest not yet converted to MTTCG - "
219 "you may get unexpected results");
Alex Bennéec34c7622017-02-28 14:40:17 +0000220#endif
KONRAD Frederic8d4e9142017-02-23 18:29:08 +0000221 if (!check_tcg_memory_orders_compatible()) {
Markus Armbruster07656912018-10-17 10:26:28 +0200222 warn_report("Guest expects a stronger memory ordering "
223 "than the host provides");
Pranith Kumar8cfef892017-03-25 16:19:23 -0400224 error_printf("This may cause strange/hard to debug errors\n");
KONRAD Frederic8d4e9142017-02-23 18:29:08 +0000225 }
226 mttcg_enabled = true;
227 }
228 } else if (strcmp(t, "single") == 0) {
229 mttcg_enabled = false;
230 } else {
231 error_setg(errp, "Invalid 'thread' setting %s", t);
232 }
233 } else {
234 mttcg_enabled = default_mttcg_enabled();
235 }
236}
Paolo Bonzini946fb272011-09-12 13:57:37 +0200237
Alex Bennéee4cd9652017-03-31 16:09:42 +0100238/* The current number of executed instructions is based on what we
239 * originally budgeted minus the current state of the decrementing
240 * icount counters in extra/u16.low.
241 */
242static int64_t cpu_get_icount_executed(CPUState *cpu)
243{
Richard Henderson5e140192019-03-28 11:54:23 -1000244 return (cpu->icount_budget -
245 (cpu_neg(cpu)->icount_decr.u16.low + cpu->icount_extra));
Alex Bennéee4cd9652017-03-31 16:09:42 +0100246}
247
Alex Bennée512d3c82017-04-05 12:32:37 +0100248/*
249 * Update the global shared timer_state.qemu_icount to take into
250 * account executed instructions. This is done by the TCG vCPU
251 * thread so the main-loop can see time has moved forward.
252 */
Paolo Bonzini9b4e6f42018-09-11 13:15:32 +0200253static void cpu_update_icount_locked(CPUState *cpu)
Alex Bennée512d3c82017-04-05 12:32:37 +0100254{
255 int64_t executed = cpu_get_icount_executed(cpu);
256 cpu->icount_budget -= executed;
257
Emilio G. Cota38adcb62018-09-10 19:27:49 -0400258 atomic_set_i64(&timers_state.qemu_icount,
259 timers_state.qemu_icount + executed);
Paolo Bonzini9b4e6f42018-09-11 13:15:32 +0200260}
261
262/*
263 * Update the global shared timer_state.qemu_icount to take into
264 * account executed instructions. This is done by the TCG vCPU
265 * thread so the main-loop can see time has moved forward.
266 */
267void cpu_update_icount(CPUState *cpu)
268{
269 seqlock_write_lock(&timers_state.vm_clock_seqlock,
270 &timers_state.vm_clock_lock);
271 cpu_update_icount_locked(cpu);
Paolo Bonzini94377112018-08-14 09:57:16 +0200272 seqlock_write_unlock(&timers_state.vm_clock_seqlock,
273 &timers_state.vm_clock_lock);
Alex Bennée512d3c82017-04-05 12:32:37 +0100274}
275
Paolo Bonzinic1ff0732018-08-14 09:31:58 +0200276static int64_t cpu_get_icount_raw_locked(void)
Paolo Bonzini946fb272011-09-12 13:57:37 +0200277{
Andreas Färber4917cf42013-05-27 05:17:50 +0200278 CPUState *cpu = current_cpu;
Paolo Bonzini946fb272011-09-12 13:57:37 +0200279
Alex Bennée243c5f72017-03-30 18:49:22 +0100280 if (cpu && cpu->running) {
Paolo Bonzini414b15c2015-06-24 14:16:26 +0200281 if (!cpu->can_do_io) {
Alistair Francis493d89b2018-02-03 09:43:14 +0100282 error_report("Bad icount read");
Pavel Dovgalyuk2a629142014-12-08 10:53:45 +0300283 exit(1);
Paolo Bonzini946fb272011-09-12 13:57:37 +0200284 }
Alex Bennéee4cd9652017-03-31 16:09:42 +0100285 /* Take into account what has run */
Paolo Bonzini9b4e6f42018-09-11 13:15:32 +0200286 cpu_update_icount_locked(cpu);
Paolo Bonzini946fb272011-09-12 13:57:37 +0200287 }
Emilio G. Cota38adcb62018-09-10 19:27:49 -0400288 /* The read is protected by the seqlock, but needs atomic64 to avoid UB */
289 return atomic_read_i64(&timers_state.qemu_icount);
Paolo Bonzinic1ff0732018-08-14 09:31:58 +0200290}
291
292static int64_t cpu_get_icount_locked(void)
293{
294 int64_t icount = cpu_get_icount_raw_locked();
Emilio G. Cotac97595d2018-09-10 19:27:50 -0400295 return atomic_read_i64(&timers_state.qemu_icount_bias) +
296 cpu_icount_to_ns(icount);
Paolo Bonzinic1ff0732018-08-14 09:31:58 +0200297}
298
299int64_t cpu_get_icount_raw(void)
300{
301 int64_t icount;
302 unsigned start;
303
304 do {
305 start = seqlock_read_begin(&timers_state.vm_clock_seqlock);
306 icount = cpu_get_icount_raw_locked();
307 } while (seqlock_read_retry(&timers_state.vm_clock_seqlock, start));
308
309 return icount;
Pavel Dovgalyuk2a629142014-12-08 10:53:45 +0300310}
311
312/* Return the virtual CPU time, based on the instruction counter. */
Paolo Bonzini17a15f12013-10-03 15:17:25 +0200313int64_t cpu_get_icount(void)
314{
315 int64_t icount;
316 unsigned start;
317
318 do {
319 start = seqlock_read_begin(&timers_state.vm_clock_seqlock);
320 icount = cpu_get_icount_locked();
321 } while (seqlock_read_retry(&timers_state.vm_clock_seqlock, start));
322
323 return icount;
324}
325
KONRAD Frederic3f031312014-08-01 01:37:15 +0200326int64_t cpu_icount_to_ns(int64_t icount)
327{
Paolo Bonzinic1ff0732018-08-14 09:31:58 +0200328 return icount << atomic_read(&timers_state.icount_time_shift);
KONRAD Frederic3f031312014-08-01 01:37:15 +0200329}
330
Paolo Bonzinif2a4ad62018-08-18 09:36:16 +0200331static int64_t cpu_get_ticks_locked(void)
332{
333 int64_t ticks = timers_state.cpu_ticks_offset;
334 if (timers_state.cpu_ticks_enabled) {
335 ticks += cpu_get_host_ticks();
336 }
337
338 if (timers_state.cpu_ticks_prev > ticks) {
339 /* Non increasing ticks may happen if the host uses software suspend. */
340 timers_state.cpu_ticks_offset += timers_state.cpu_ticks_prev - ticks;
341 ticks = timers_state.cpu_ticks_prev;
342 }
343
344 timers_state.cpu_ticks_prev = ticks;
345 return ticks;
346}
347
Cao jind90f3cc2016-07-29 19:05:38 +0800348/* return the time elapsed in VM between vm_start and vm_stop. Unless
349 * icount is active, cpu_get_ticks() uses units of the host CPU cycle
350 * counter.
Cao jind90f3cc2016-07-29 19:05:38 +0800351 */
Paolo Bonzini946fb272011-09-12 13:57:37 +0200352int64_t cpu_get_ticks(void)
353{
Paolo Bonzini5f3e3102013-10-28 17:32:18 +0100354 int64_t ticks;
355
Paolo Bonzini946fb272011-09-12 13:57:37 +0200356 if (use_icount) {
357 return cpu_get_icount();
358 }
Paolo Bonzini5f3e3102013-10-28 17:32:18 +0100359
Paolo Bonzinif2a4ad62018-08-18 09:36:16 +0200360 qemu_spin_lock(&timers_state.vm_clock_lock);
361 ticks = cpu_get_ticks_locked();
362 qemu_spin_unlock(&timers_state.vm_clock_lock);
Paolo Bonzini5f3e3102013-10-28 17:32:18 +0100363 return ticks;
Paolo Bonzini946fb272011-09-12 13:57:37 +0200364}
365
Liu Ping Fancb365642013-09-25 14:20:58 +0800366static int64_t cpu_get_clock_locked(void)
367{
Cao jin1d45cea2016-07-29 19:05:37 +0800368 int64_t time;
Liu Ping Fancb365642013-09-25 14:20:58 +0800369
Cao jin1d45cea2016-07-29 19:05:37 +0800370 time = timers_state.cpu_clock_offset;
Paolo Bonzini5f3e3102013-10-28 17:32:18 +0100371 if (timers_state.cpu_ticks_enabled) {
Cao jin1d45cea2016-07-29 19:05:37 +0800372 time += get_clock();
Liu Ping Fancb365642013-09-25 14:20:58 +0800373 }
374
Cao jin1d45cea2016-07-29 19:05:37 +0800375 return time;
Liu Ping Fancb365642013-09-25 14:20:58 +0800376}
377
Cao jind90f3cc2016-07-29 19:05:38 +0800378/* Return the monotonic time elapsed in VM, i.e.,
Peter Maydell8212ff82016-09-15 10:24:22 +0100379 * the time between vm_start and vm_stop
380 */
Paolo Bonzini946fb272011-09-12 13:57:37 +0200381int64_t cpu_get_clock(void)
382{
383 int64_t ti;
Liu Ping Fancb365642013-09-25 14:20:58 +0800384 unsigned start;
385
386 do {
387 start = seqlock_read_begin(&timers_state.vm_clock_seqlock);
388 ti = cpu_get_clock_locked();
389 } while (seqlock_read_retry(&timers_state.vm_clock_seqlock, start));
390
391 return ti;
Paolo Bonzini946fb272011-09-12 13:57:37 +0200392}
393
Liu Ping Fancb365642013-09-25 14:20:58 +0800394/* enable cpu_get_ticks()
Cao jin3224e872016-07-08 18:31:37 +0800395 * Caller must hold BQL which serves as mutex for vm_clock_seqlock.
Liu Ping Fancb365642013-09-25 14:20:58 +0800396 */
Paolo Bonzini946fb272011-09-12 13:57:37 +0200397void cpu_enable_ticks(void)
398{
Paolo Bonzini94377112018-08-14 09:57:16 +0200399 seqlock_write_lock(&timers_state.vm_clock_seqlock,
400 &timers_state.vm_clock_lock);
Paolo Bonzini946fb272011-09-12 13:57:37 +0200401 if (!timers_state.cpu_ticks_enabled) {
Christopher Covington4a7428c2015-09-25 10:42:21 -0400402 timers_state.cpu_ticks_offset -= cpu_get_host_ticks();
Paolo Bonzini946fb272011-09-12 13:57:37 +0200403 timers_state.cpu_clock_offset -= get_clock();
404 timers_state.cpu_ticks_enabled = 1;
405 }
Paolo Bonzini94377112018-08-14 09:57:16 +0200406 seqlock_write_unlock(&timers_state.vm_clock_seqlock,
407 &timers_state.vm_clock_lock);
Paolo Bonzini946fb272011-09-12 13:57:37 +0200408}
409
410/* disable cpu_get_ticks() : the clock is stopped. You must not call
Liu Ping Fancb365642013-09-25 14:20:58 +0800411 * cpu_get_ticks() after that.
Cao jin3224e872016-07-08 18:31:37 +0800412 * Caller must hold BQL which serves as mutex for vm_clock_seqlock.
Liu Ping Fancb365642013-09-25 14:20:58 +0800413 */
Paolo Bonzini946fb272011-09-12 13:57:37 +0200414void cpu_disable_ticks(void)
415{
Paolo Bonzini94377112018-08-14 09:57:16 +0200416 seqlock_write_lock(&timers_state.vm_clock_seqlock,
417 &timers_state.vm_clock_lock);
Paolo Bonzini946fb272011-09-12 13:57:37 +0200418 if (timers_state.cpu_ticks_enabled) {
Christopher Covington4a7428c2015-09-25 10:42:21 -0400419 timers_state.cpu_ticks_offset += cpu_get_host_ticks();
Liu Ping Fancb365642013-09-25 14:20:58 +0800420 timers_state.cpu_clock_offset = cpu_get_clock_locked();
Paolo Bonzini946fb272011-09-12 13:57:37 +0200421 timers_state.cpu_ticks_enabled = 0;
422 }
Paolo Bonzini94377112018-08-14 09:57:16 +0200423 seqlock_write_unlock(&timers_state.vm_clock_seqlock,
424 &timers_state.vm_clock_lock);
Paolo Bonzini946fb272011-09-12 13:57:37 +0200425}
426
427/* Correlation between real and virtual time is always going to be
428 fairly approximate, so ignore small variation.
429 When the guest is idle real and virtual time will be aligned in
430 the IO wait loop. */
Rutuja Shah73bcb242016-03-21 21:32:30 +0530431#define ICOUNT_WOBBLE (NANOSECONDS_PER_SECOND / 10)
Paolo Bonzini946fb272011-09-12 13:57:37 +0200432
433static void icount_adjust(void)
434{
435 int64_t cur_time;
436 int64_t cur_icount;
437 int64_t delta;
Paolo Bonzinia3270e12013-10-07 17:18:15 +0200438
439 /* Protected by TimersState mutex. */
Paolo Bonzini946fb272011-09-12 13:57:37 +0200440 static int64_t last_delta;
Paolo Bonzini468cc7c2013-10-07 17:21:51 +0200441
Paolo Bonzini946fb272011-09-12 13:57:37 +0200442 /* If the VM is not running, then do nothing. */
443 if (!runstate_is_running()) {
444 return;
445 }
Paolo Bonzini468cc7c2013-10-07 17:21:51 +0200446
Paolo Bonzini94377112018-08-14 09:57:16 +0200447 seqlock_write_lock(&timers_state.vm_clock_seqlock,
448 &timers_state.vm_clock_lock);
Paolo Bonzini17a15f12013-10-03 15:17:25 +0200449 cur_time = cpu_get_clock_locked();
450 cur_icount = cpu_get_icount_locked();
Paolo Bonzini468cc7c2013-10-07 17:21:51 +0200451
Paolo Bonzini946fb272011-09-12 13:57:37 +0200452 delta = cur_icount - cur_time;
453 /* FIXME: This is a very crude algorithm, somewhat prone to oscillation. */
454 if (delta > 0
455 && last_delta + ICOUNT_WOBBLE < delta * 2
Paolo Bonzinic1ff0732018-08-14 09:31:58 +0200456 && timers_state.icount_time_shift > 0) {
Paolo Bonzini946fb272011-09-12 13:57:37 +0200457 /* The guest is getting too far ahead. Slow time down. */
Paolo Bonzinic1ff0732018-08-14 09:31:58 +0200458 atomic_set(&timers_state.icount_time_shift,
459 timers_state.icount_time_shift - 1);
Paolo Bonzini946fb272011-09-12 13:57:37 +0200460 }
461 if (delta < 0
462 && last_delta - ICOUNT_WOBBLE > delta * 2
Paolo Bonzinic1ff0732018-08-14 09:31:58 +0200463 && timers_state.icount_time_shift < MAX_ICOUNT_SHIFT) {
Paolo Bonzini946fb272011-09-12 13:57:37 +0200464 /* The guest is getting too far behind. Speed time up. */
Paolo Bonzinic1ff0732018-08-14 09:31:58 +0200465 atomic_set(&timers_state.icount_time_shift,
466 timers_state.icount_time_shift + 1);
Paolo Bonzini946fb272011-09-12 13:57:37 +0200467 }
468 last_delta = delta;
Emilio G. Cotac97595d2018-09-10 19:27:50 -0400469 atomic_set_i64(&timers_state.qemu_icount_bias,
470 cur_icount - (timers_state.qemu_icount
471 << timers_state.icount_time_shift));
Paolo Bonzini94377112018-08-14 09:57:16 +0200472 seqlock_write_unlock(&timers_state.vm_clock_seqlock,
473 &timers_state.vm_clock_lock);
Paolo Bonzini946fb272011-09-12 13:57:37 +0200474}
475
476static void icount_adjust_rt(void *opaque)
477{
Pavel Dovgalyukb39e3f32018-01-11 11:26:10 +0300478 timer_mod(timers_state.icount_rt_timer,
Pavel Dovgalyuk1979b902015-01-12 15:00:43 +0300479 qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL_RT) + 1000);
Paolo Bonzini946fb272011-09-12 13:57:37 +0200480 icount_adjust();
481}
482
483static void icount_adjust_vm(void *opaque)
484{
Pavel Dovgalyukb39e3f32018-01-11 11:26:10 +0300485 timer_mod(timers_state.icount_vm_timer,
Alex Bligh40daca52013-08-21 16:03:02 +0100486 qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) +
Rutuja Shah73bcb242016-03-21 21:32:30 +0530487 NANOSECONDS_PER_SECOND / 10);
Paolo Bonzini946fb272011-09-12 13:57:37 +0200488 icount_adjust();
489}
490
491static int64_t qemu_icount_round(int64_t count)
492{
Paolo Bonzinic1ff0732018-08-14 09:31:58 +0200493 int shift = atomic_read(&timers_state.icount_time_shift);
494 return (count + (1 << shift) - 1) >> shift;
Paolo Bonzini946fb272011-09-12 13:57:37 +0200495}
496
Pavel Dovgalyukefab87c2015-09-17 19:24:39 +0300497static void icount_warp_rt(void)
Paolo Bonzini946fb272011-09-12 13:57:37 +0200498{
Alex Bennéeccffff42016-04-04 15:35:48 +0100499 unsigned seq;
500 int64_t warp_start;
501
Paolo Bonzini17a15f12013-10-03 15:17:25 +0200502 /* The icount_warp_timer is rescheduled soon after vm_clock_warp_start
503 * changes from -1 to another value, so the race here is okay.
504 */
Alex Bennéeccffff42016-04-04 15:35:48 +0100505 do {
506 seq = seqlock_read_begin(&timers_state.vm_clock_seqlock);
Pavel Dovgalyukb39e3f32018-01-11 11:26:10 +0300507 warp_start = timers_state.vm_clock_warp_start;
Alex Bennéeccffff42016-04-04 15:35:48 +0100508 } while (seqlock_read_retry(&timers_state.vm_clock_seqlock, seq));
509
510 if (warp_start == -1) {
Paolo Bonzini946fb272011-09-12 13:57:37 +0200511 return;
512 }
513
Paolo Bonzini94377112018-08-14 09:57:16 +0200514 seqlock_write_lock(&timers_state.vm_clock_seqlock,
515 &timers_state.vm_clock_lock);
Paolo Bonzini946fb272011-09-12 13:57:37 +0200516 if (runstate_is_running()) {
Paolo Bonzini74c0b812018-10-08 13:24:14 +0200517 int64_t clock = REPLAY_CLOCK_LOCKED(REPLAY_CLOCK_VIRTUAL_RT,
518 cpu_get_clock_locked());
Paolo Bonzini8ed961d2013-10-07 17:26:07 +0200519 int64_t warp_delta;
520
Pavel Dovgalyukb39e3f32018-01-11 11:26:10 +0300521 warp_delta = clock - timers_state.vm_clock_warp_start;
Paolo Bonzini8ed961d2013-10-07 17:26:07 +0200522 if (use_icount == 2) {
Paolo Bonzini946fb272011-09-12 13:57:37 +0200523 /*
Alex Bligh40daca52013-08-21 16:03:02 +0100524 * In adaptive mode, do not let QEMU_CLOCK_VIRTUAL run too
Paolo Bonzini946fb272011-09-12 13:57:37 +0200525 * far ahead of real time.
526 */
Paolo Bonzini17a15f12013-10-03 15:17:25 +0200527 int64_t cur_icount = cpu_get_icount_locked();
Pavel Dovgalyukbf2a7dd2014-11-26 13:40:55 +0300528 int64_t delta = clock - cur_icount;
Paolo Bonzini8ed961d2013-10-07 17:26:07 +0200529 warp_delta = MIN(warp_delta, delta);
Paolo Bonzini946fb272011-09-12 13:57:37 +0200530 }
Emilio G. Cotac97595d2018-09-10 19:27:50 -0400531 atomic_set_i64(&timers_state.qemu_icount_bias,
532 timers_state.qemu_icount_bias + warp_delta);
Paolo Bonzini946fb272011-09-12 13:57:37 +0200533 }
Pavel Dovgalyukb39e3f32018-01-11 11:26:10 +0300534 timers_state.vm_clock_warp_start = -1;
Paolo Bonzini94377112018-08-14 09:57:16 +0200535 seqlock_write_unlock(&timers_state.vm_clock_seqlock,
536 &timers_state.vm_clock_lock);
Paolo Bonzini8ed961d2013-10-07 17:26:07 +0200537
538 if (qemu_clock_expired(QEMU_CLOCK_VIRTUAL)) {
539 qemu_clock_notify(QEMU_CLOCK_VIRTUAL);
540 }
Paolo Bonzini946fb272011-09-12 13:57:37 +0200541}
542
Pavel Dovgalyuke76d1792016-03-10 14:56:09 +0300543static void icount_timer_cb(void *opaque)
Pavel Dovgalyukefab87c2015-09-17 19:24:39 +0300544{
Pavel Dovgalyuke76d1792016-03-10 14:56:09 +0300545 /* No need for a checkpoint because the timer already synchronizes
546 * with CHECKPOINT_CLOCK_VIRTUAL_RT.
547 */
548 icount_warp_rt();
Pavel Dovgalyukefab87c2015-09-17 19:24:39 +0300549}
550
Paolo Bonzini8156be52012-03-28 15:42:04 +0200551void qtest_clock_warp(int64_t dest)
552{
Alex Bligh40daca52013-08-21 16:03:02 +0100553 int64_t clock = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
Fam Zhengefef88b2015-01-19 17:51:43 +0800554 AioContext *aio_context;
Paolo Bonzini8156be52012-03-28 15:42:04 +0200555 assert(qtest_enabled());
Fam Zhengefef88b2015-01-19 17:51:43 +0800556 aio_context = qemu_get_aio_context();
Paolo Bonzini8156be52012-03-28 15:42:04 +0200557 while (clock < dest) {
Alex Bligh40daca52013-08-21 16:03:02 +0100558 int64_t deadline = qemu_clock_deadline_ns_all(QEMU_CLOCK_VIRTUAL);
Sergey Fedorovc9299e22014-06-10 13:10:28 +0400559 int64_t warp = qemu_soonest_timeout(dest - clock, deadline);
Fam Zhengefef88b2015-01-19 17:51:43 +0800560
Paolo Bonzini94377112018-08-14 09:57:16 +0200561 seqlock_write_lock(&timers_state.vm_clock_seqlock,
562 &timers_state.vm_clock_lock);
Emilio G. Cotac97595d2018-09-10 19:27:50 -0400563 atomic_set_i64(&timers_state.qemu_icount_bias,
564 timers_state.qemu_icount_bias + warp);
Paolo Bonzini94377112018-08-14 09:57:16 +0200565 seqlock_write_unlock(&timers_state.vm_clock_seqlock,
566 &timers_state.vm_clock_lock);
Paolo Bonzini17a15f12013-10-03 15:17:25 +0200567
Alex Bligh40daca52013-08-21 16:03:02 +0100568 qemu_clock_run_timers(QEMU_CLOCK_VIRTUAL);
Fam Zhengefef88b2015-01-19 17:51:43 +0800569 timerlist_run_timers(aio_context->tlg.tl[QEMU_CLOCK_VIRTUAL]);
Alex Bligh40daca52013-08-21 16:03:02 +0100570 clock = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
Paolo Bonzini8156be52012-03-28 15:42:04 +0200571 }
Alex Bligh40daca52013-08-21 16:03:02 +0100572 qemu_clock_notify(QEMU_CLOCK_VIRTUAL);
Paolo Bonzini8156be52012-03-28 15:42:04 +0200573}
574
Pavel Dovgalyuke76d1792016-03-10 14:56:09 +0300575void qemu_start_warp_timer(void)
Paolo Bonzini946fb272011-09-12 13:57:37 +0200576{
Paolo Bonzinice78d182013-10-07 17:30:02 +0200577 int64_t clock;
Paolo Bonzini946fb272011-09-12 13:57:37 +0200578 int64_t deadline;
579
Pavel Dovgalyuke76d1792016-03-10 14:56:09 +0300580 if (!use_icount) {
Paolo Bonzini946fb272011-09-12 13:57:37 +0200581 return;
582 }
583
Pavel Dovgalyuk8bd7f712015-09-17 19:24:44 +0300584 /* Nothing to do if the VM is stopped: QEMU_CLOCK_VIRTUAL timers
585 * do not fire, so computing the deadline does not make sense.
586 */
587 if (!runstate_is_running()) {
588 return;
589 }
590
Pavel Dovgalyuk0c081852018-09-12 11:19:45 +0300591 if (replay_mode != REPLAY_MODE_PLAY) {
592 if (!all_cpu_threads_idle()) {
593 return;
594 }
Pavel Dovgalyuk8bd7f712015-09-17 19:24:44 +0300595
Pavel Dovgalyuk0c081852018-09-12 11:19:45 +0300596 if (qtest_enabled()) {
597 /* When testing, qtest commands advance icount. */
598 return;
599 }
Paolo Bonzini946fb272011-09-12 13:57:37 +0200600
Pavel Dovgalyuk0c081852018-09-12 11:19:45 +0300601 replay_checkpoint(CHECKPOINT_CLOCK_WARP_START);
602 } else {
603 /* warp clock deterministically in record/replay mode */
604 if (!replay_checkpoint(CHECKPOINT_CLOCK_WARP_START)) {
605 /* vCPU is sleeping and warp can't be started.
606 It is probably a race condition: notification sent
607 to vCPU was processed in advance and vCPU went to sleep.
608 Therefore we have to wake it up for doing someting. */
609 if (replay_has_checkpoint()) {
610 qemu_clock_notify(QEMU_CLOCK_VIRTUAL);
611 }
612 return;
613 }
Paolo Bonzini8156be52012-03-28 15:42:04 +0200614 }
615
Alex Blighac70aaf2013-08-21 16:02:57 +0100616 /* We want to use the earliest deadline from ALL vm_clocks */
Pavel Dovgalyukbf2a7dd2014-11-26 13:40:55 +0300617 clock = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL_RT);
Alex Bligh40daca52013-08-21 16:03:02 +0100618 deadline = qemu_clock_deadline_ns_all(QEMU_CLOCK_VIRTUAL);
Paolo Bonzinice78d182013-10-07 17:30:02 +0200619 if (deadline < 0) {
Victor CLEMENTd7a0f712015-05-29 17:14:06 +0200620 static bool notified;
621 if (!icount_sleep && !notified) {
Alistair Francis3dc6f862017-07-12 06:57:41 -0700622 warn_report("icount sleep disabled and no active timers");
Victor CLEMENTd7a0f712015-05-29 17:14:06 +0200623 notified = true;
624 }
Paolo Bonzinice78d182013-10-07 17:30:02 +0200625 return;
Alex Blighac70aaf2013-08-21 16:02:57 +0100626 }
627
Paolo Bonzini946fb272011-09-12 13:57:37 +0200628 if (deadline > 0) {
629 /*
Alex Bligh40daca52013-08-21 16:03:02 +0100630 * Ensure QEMU_CLOCK_VIRTUAL proceeds even when the virtual CPU goes to
Paolo Bonzini946fb272011-09-12 13:57:37 +0200631 * sleep. Otherwise, the CPU might be waiting for a future timer
632 * interrupt to wake it up, but the interrupt never comes because
633 * the vCPU isn't running any insns and thus doesn't advance the
Alex Bligh40daca52013-08-21 16:03:02 +0100634 * QEMU_CLOCK_VIRTUAL.
Paolo Bonzini946fb272011-09-12 13:57:37 +0200635 */
Victor CLEMENT5045e9d92015-05-29 17:14:04 +0200636 if (!icount_sleep) {
637 /*
638 * We never let VCPUs sleep in no sleep icount mode.
639 * If there is a pending QEMU_CLOCK_VIRTUAL timer we just advance
640 * to the next QEMU_CLOCK_VIRTUAL event and notify it.
641 * It is useful when we want a deterministic execution time,
642 * isolated from host latencies.
643 */
Paolo Bonzini94377112018-08-14 09:57:16 +0200644 seqlock_write_lock(&timers_state.vm_clock_seqlock,
645 &timers_state.vm_clock_lock);
Emilio G. Cotac97595d2018-09-10 19:27:50 -0400646 atomic_set_i64(&timers_state.qemu_icount_bias,
647 timers_state.qemu_icount_bias + deadline);
Paolo Bonzini94377112018-08-14 09:57:16 +0200648 seqlock_write_unlock(&timers_state.vm_clock_seqlock,
649 &timers_state.vm_clock_lock);
Victor CLEMENT5045e9d92015-05-29 17:14:04 +0200650 qemu_clock_notify(QEMU_CLOCK_VIRTUAL);
651 } else {
652 /*
653 * We do stop VCPUs and only advance QEMU_CLOCK_VIRTUAL after some
654 * "real" time, (related to the time left until the next event) has
655 * passed. The QEMU_CLOCK_VIRTUAL_RT clock will do this.
656 * This avoids that the warps are visible externally; for example,
657 * you will not be sending network packets continuously instead of
658 * every 100ms.
659 */
Paolo Bonzini94377112018-08-14 09:57:16 +0200660 seqlock_write_lock(&timers_state.vm_clock_seqlock,
661 &timers_state.vm_clock_lock);
Pavel Dovgalyukb39e3f32018-01-11 11:26:10 +0300662 if (timers_state.vm_clock_warp_start == -1
663 || timers_state.vm_clock_warp_start > clock) {
664 timers_state.vm_clock_warp_start = clock;
Victor CLEMENT5045e9d92015-05-29 17:14:04 +0200665 }
Paolo Bonzini94377112018-08-14 09:57:16 +0200666 seqlock_write_unlock(&timers_state.vm_clock_seqlock,
667 &timers_state.vm_clock_lock);
Pavel Dovgalyukb39e3f32018-01-11 11:26:10 +0300668 timer_mod_anticipate(timers_state.icount_warp_timer,
669 clock + deadline);
Paolo Bonzinice78d182013-10-07 17:30:02 +0200670 }
Alex Blighac70aaf2013-08-21 16:02:57 +0100671 } else if (deadline == 0) {
Alex Bligh40daca52013-08-21 16:03:02 +0100672 qemu_clock_notify(QEMU_CLOCK_VIRTUAL);
Paolo Bonzini946fb272011-09-12 13:57:37 +0200673 }
674}
675
Pavel Dovgalyuke76d1792016-03-10 14:56:09 +0300676static void qemu_account_warp_timer(void)
677{
678 if (!use_icount || !icount_sleep) {
679 return;
680 }
681
682 /* Nothing to do if the VM is stopped: QEMU_CLOCK_VIRTUAL timers
683 * do not fire, so computing the deadline does not make sense.
684 */
685 if (!runstate_is_running()) {
686 return;
687 }
688
689 /* warp clock deterministically in record/replay mode */
690 if (!replay_checkpoint(CHECKPOINT_CLOCK_WARP_ACCOUNT)) {
691 return;
692 }
693
Pavel Dovgalyukb39e3f32018-01-11 11:26:10 +0300694 timer_del(timers_state.icount_warp_timer);
Pavel Dovgalyuke76d1792016-03-10 14:56:09 +0300695 icount_warp_rt();
696}
697
KONRAD Fredericd09eae32014-08-01 01:37:10 +0200698static bool icount_state_needed(void *opaque)
699{
700 return use_icount;
701}
702
Pavel Dovgalyukb39e3f32018-01-11 11:26:10 +0300703static bool warp_timer_state_needed(void *opaque)
704{
705 TimersState *s = opaque;
706 return s->icount_warp_timer != NULL;
707}
708
709static bool adjust_timers_state_needed(void *opaque)
710{
711 TimersState *s = opaque;
712 return s->icount_rt_timer != NULL;
713}
714
715/*
716 * Subsection for warp timer migration is optional, because may not be created
717 */
718static const VMStateDescription icount_vmstate_warp_timer = {
719 .name = "timer/icount/warp_timer",
720 .version_id = 1,
721 .minimum_version_id = 1,
722 .needed = warp_timer_state_needed,
723 .fields = (VMStateField[]) {
724 VMSTATE_INT64(vm_clock_warp_start, TimersState),
725 VMSTATE_TIMER_PTR(icount_warp_timer, TimersState),
726 VMSTATE_END_OF_LIST()
727 }
728};
729
730static const VMStateDescription icount_vmstate_adjust_timers = {
731 .name = "timer/icount/timers",
732 .version_id = 1,
733 .minimum_version_id = 1,
734 .needed = adjust_timers_state_needed,
735 .fields = (VMStateField[]) {
736 VMSTATE_TIMER_PTR(icount_rt_timer, TimersState),
737 VMSTATE_TIMER_PTR(icount_vm_timer, TimersState),
738 VMSTATE_END_OF_LIST()
739 }
740};
741
KONRAD Fredericd09eae32014-08-01 01:37:10 +0200742/*
743 * This is a subsection for icount migration.
744 */
745static const VMStateDescription icount_vmstate_timers = {
746 .name = "timer/icount",
747 .version_id = 1,
748 .minimum_version_id = 1,
Juan Quintela5cd8cad2014-09-23 14:09:54 +0200749 .needed = icount_state_needed,
KONRAD Fredericd09eae32014-08-01 01:37:10 +0200750 .fields = (VMStateField[]) {
751 VMSTATE_INT64(qemu_icount_bias, TimersState),
752 VMSTATE_INT64(qemu_icount, TimersState),
753 VMSTATE_END_OF_LIST()
Pavel Dovgalyukb39e3f32018-01-11 11:26:10 +0300754 },
755 .subsections = (const VMStateDescription*[]) {
756 &icount_vmstate_warp_timer,
757 &icount_vmstate_adjust_timers,
758 NULL
KONRAD Fredericd09eae32014-08-01 01:37:10 +0200759 }
760};
761
Paolo Bonzini946fb272011-09-12 13:57:37 +0200762static const VMStateDescription vmstate_timers = {
763 .name = "timer",
764 .version_id = 2,
765 .minimum_version_id = 1,
Juan Quintela35d08452014-04-16 16:01:33 +0200766 .fields = (VMStateField[]) {
Paolo Bonzini946fb272011-09-12 13:57:37 +0200767 VMSTATE_INT64(cpu_ticks_offset, TimersState),
Paolo Bonzinic1ff0732018-08-14 09:31:58 +0200768 VMSTATE_UNUSED(8),
Paolo Bonzini946fb272011-09-12 13:57:37 +0200769 VMSTATE_INT64_V(cpu_clock_offset, TimersState, 2),
770 VMSTATE_END_OF_LIST()
KONRAD Fredericd09eae32014-08-01 01:37:10 +0200771 },
Juan Quintela5cd8cad2014-09-23 14:09:54 +0200772 .subsections = (const VMStateDescription*[]) {
773 &icount_vmstate_timers,
774 NULL
Paolo Bonzini946fb272011-09-12 13:57:37 +0200775 }
776};
777
Paolo Bonzini14e6fe12016-10-31 10:36:08 +0100778static void cpu_throttle_thread(CPUState *cpu, run_on_cpu_data opaque)
Jason J. Herne2adcc852015-09-08 13:12:33 -0400779{
Jason J. Herne2adcc852015-09-08 13:12:33 -0400780 double pct;
781 double throttle_ratio;
782 long sleeptime_ns;
783
784 if (!cpu_throttle_get_percentage()) {
785 return;
786 }
787
788 pct = (double)cpu_throttle_get_percentage()/100;
789 throttle_ratio = pct / (1 - pct);
790 sleeptime_ns = (long)(throttle_ratio * CPU_THROTTLE_TIMESLICE_NS);
791
792 qemu_mutex_unlock_iothread();
Jason J. Herne2adcc852015-09-08 13:12:33 -0400793 g_usleep(sleeptime_ns / 1000); /* Convert ns to us for usleep call */
794 qemu_mutex_lock_iothread();
Felipe Franciosi90bb0c02017-05-19 22:29:50 +0100795 atomic_set(&cpu->throttle_thread_scheduled, 0);
Jason J. Herne2adcc852015-09-08 13:12:33 -0400796}
797
798static void cpu_throttle_timer_tick(void *opaque)
799{
800 CPUState *cpu;
801 double pct;
802
803 /* Stop the timer if needed */
804 if (!cpu_throttle_get_percentage()) {
805 return;
806 }
807 CPU_FOREACH(cpu) {
808 if (!atomic_xchg(&cpu->throttle_thread_scheduled, 1)) {
Paolo Bonzini14e6fe12016-10-31 10:36:08 +0100809 async_run_on_cpu(cpu, cpu_throttle_thread,
810 RUN_ON_CPU_NULL);
Jason J. Herne2adcc852015-09-08 13:12:33 -0400811 }
812 }
813
814 pct = (double)cpu_throttle_get_percentage()/100;
815 timer_mod(throttle_timer, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL_RT) +
816 CPU_THROTTLE_TIMESLICE_NS / (1-pct));
817}
818
819void cpu_throttle_set(int new_throttle_pct)
820{
821 /* Ensure throttle percentage is within valid range */
822 new_throttle_pct = MIN(new_throttle_pct, CPU_THROTTLE_PCT_MAX);
823 new_throttle_pct = MAX(new_throttle_pct, CPU_THROTTLE_PCT_MIN);
824
825 atomic_set(&throttle_percentage, new_throttle_pct);
826
827 timer_mod(throttle_timer, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL_RT) +
828 CPU_THROTTLE_TIMESLICE_NS);
829}
830
831void cpu_throttle_stop(void)
832{
833 atomic_set(&throttle_percentage, 0);
834}
835
836bool cpu_throttle_active(void)
837{
838 return (cpu_throttle_get_percentage() != 0);
839}
840
841int cpu_throttle_get_percentage(void)
842{
843 return atomic_read(&throttle_percentage);
844}
845
Pavel Dovgalyuk4603ea02014-09-01 09:34:49 +0400846void cpu_ticks_init(void)
847{
Emilio G. Cotaccdb3c12016-06-08 14:55:20 -0400848 seqlock_init(&timers_state.vm_clock_seqlock);
Emilio G. Cota87a09cd2018-09-03 13:18:29 -0400849 qemu_spin_init(&timers_state.vm_clock_lock);
Pavel Dovgalyuk4603ea02014-09-01 09:34:49 +0400850 vmstate_register(NULL, 0, &vmstate_timers, &timers_state);
Jason J. Herne2adcc852015-09-08 13:12:33 -0400851 throttle_timer = timer_new_ns(QEMU_CLOCK_VIRTUAL_RT,
852 cpu_throttle_timer_tick, NULL);
Pavel Dovgalyuk4603ea02014-09-01 09:34:49 +0400853}
854
Sebastian Tanase1ad95802014-07-25 11:56:28 +0200855void configure_icount(QemuOpts *opts, Error **errp)
Paolo Bonzini946fb272011-09-12 13:57:37 +0200856{
Sebastian Tanase1ad95802014-07-25 11:56:28 +0200857 const char *option;
Sebastian Tanasea8bfac32014-07-25 11:56:29 +0200858 char *rem_str = NULL;
Sebastian Tanase1ad95802014-07-25 11:56:28 +0200859
Sebastian Tanase1ad95802014-07-25 11:56:28 +0200860 option = qemu_opt_get(opts, "shift");
Paolo Bonzini946fb272011-09-12 13:57:37 +0200861 if (!option) {
Sebastian Tanasea8bfac32014-07-25 11:56:29 +0200862 if (qemu_opt_get(opts, "align") != NULL) {
863 error_setg(errp, "Please specify shift option when using align");
864 }
Paolo Bonzini946fb272011-09-12 13:57:37 +0200865 return;
866 }
Victor CLEMENTf1f4b572015-05-29 17:14:05 +0200867
868 icount_sleep = qemu_opt_get_bool(opts, "sleep", true);
Victor CLEMENT5045e9d92015-05-29 17:14:04 +0200869 if (icount_sleep) {
Pavel Dovgalyukb39e3f32018-01-11 11:26:10 +0300870 timers_state.icount_warp_timer = timer_new_ns(QEMU_CLOCK_VIRTUAL_RT,
Pavel Dovgalyuke76d1792016-03-10 14:56:09 +0300871 icount_timer_cb, NULL);
Victor CLEMENT5045e9d92015-05-29 17:14:04 +0200872 }
Victor CLEMENTf1f4b572015-05-29 17:14:05 +0200873
Sebastian Tanasea8bfac32014-07-25 11:56:29 +0200874 icount_align_option = qemu_opt_get_bool(opts, "align", false);
Victor CLEMENTf1f4b572015-05-29 17:14:05 +0200875
876 if (icount_align_option && !icount_sleep) {
Pranith Kumar778d9f92016-02-26 10:16:51 -0500877 error_setg(errp, "align=on and sleep=off are incompatible");
Victor CLEMENTf1f4b572015-05-29 17:14:05 +0200878 }
Paolo Bonzini946fb272011-09-12 13:57:37 +0200879 if (strcmp(option, "auto") != 0) {
Sebastian Tanasea8bfac32014-07-25 11:56:29 +0200880 errno = 0;
Paolo Bonzinic1ff0732018-08-14 09:31:58 +0200881 timers_state.icount_time_shift = strtol(option, &rem_str, 0);
Sebastian Tanasea8bfac32014-07-25 11:56:29 +0200882 if (errno != 0 || *rem_str != '\0' || !strlen(option)) {
883 error_setg(errp, "icount: Invalid shift value");
884 }
Paolo Bonzini946fb272011-09-12 13:57:37 +0200885 use_icount = 1;
886 return;
Sebastian Tanasea8bfac32014-07-25 11:56:29 +0200887 } else if (icount_align_option) {
888 error_setg(errp, "shift=auto and align=on are incompatible");
Victor CLEMENTf1f4b572015-05-29 17:14:05 +0200889 } else if (!icount_sleep) {
Pranith Kumar778d9f92016-02-26 10:16:51 -0500890 error_setg(errp, "shift=auto and sleep=off are incompatible");
Paolo Bonzini946fb272011-09-12 13:57:37 +0200891 }
892
893 use_icount = 2;
894
895 /* 125MIPS seems a reasonable initial guess at the guest speed.
896 It will be corrected fairly quickly anyway. */
Paolo Bonzinic1ff0732018-08-14 09:31:58 +0200897 timers_state.icount_time_shift = 3;
Paolo Bonzini946fb272011-09-12 13:57:37 +0200898
899 /* Have both realtime and virtual time triggers for speed adjustment.
900 The realtime trigger catches emulated time passing too slowly,
901 the virtual time trigger catches emulated time passing too fast.
902 Realtime triggers occur even when idle, so use them less frequently
903 than VM triggers. */
Pavel Dovgalyukb39e3f32018-01-11 11:26:10 +0300904 timers_state.vm_clock_warp_start = -1;
905 timers_state.icount_rt_timer = timer_new_ms(QEMU_CLOCK_VIRTUAL_RT,
Pavel Dovgalyukbf2a7dd2014-11-26 13:40:55 +0300906 icount_adjust_rt, NULL);
Pavel Dovgalyukb39e3f32018-01-11 11:26:10 +0300907 timer_mod(timers_state.icount_rt_timer,
Pavel Dovgalyukbf2a7dd2014-11-26 13:40:55 +0300908 qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL_RT) + 1000);
Pavel Dovgalyukb39e3f32018-01-11 11:26:10 +0300909 timers_state.icount_vm_timer = timer_new_ns(QEMU_CLOCK_VIRTUAL,
Alex Bligh40daca52013-08-21 16:03:02 +0100910 icount_adjust_vm, NULL);
Pavel Dovgalyukb39e3f32018-01-11 11:26:10 +0300911 timer_mod(timers_state.icount_vm_timer,
Alex Bligh40daca52013-08-21 16:03:02 +0100912 qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) +
Rutuja Shah73bcb242016-03-21 21:32:30 +0530913 NANOSECONDS_PER_SECOND / 10);
Paolo Bonzini946fb272011-09-12 13:57:37 +0200914}
915
916/***********************************************************/
Alex Bennée65467062017-02-23 18:29:09 +0000917/* TCG vCPU kick timer
918 *
919 * The kick timer is responsible for moving single threaded vCPU
920 * emulation on to the next vCPU. If more than one vCPU is running a
921 * timer event with force a cpu->exit so the next vCPU can get
922 * scheduled.
923 *
924 * The timer is removed if all vCPUs are idle and restarted again once
925 * idleness is complete.
926 */
927
928static QEMUTimer *tcg_kick_vcpu_timer;
Alex Bennée791158d2017-02-23 18:29:10 +0000929static CPUState *tcg_current_rr_cpu;
Alex Bennée65467062017-02-23 18:29:09 +0000930
931#define TCG_KICK_PERIOD (NANOSECONDS_PER_SECOND / 10)
932
933static inline int64_t qemu_tcg_next_kick(void)
934{
935 return qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) + TCG_KICK_PERIOD;
936}
937
Alex Bennée791158d2017-02-23 18:29:10 +0000938/* Kick the currently round-robin scheduled vCPU */
939static void qemu_cpu_kick_rr_cpu(void)
940{
941 CPUState *cpu;
Alex Bennée791158d2017-02-23 18:29:10 +0000942 do {
943 cpu = atomic_mb_read(&tcg_current_rr_cpu);
944 if (cpu) {
945 cpu_exit(cpu);
946 }
947 } while (cpu != atomic_mb_read(&tcg_current_rr_cpu));
948}
949
Paolo Bonzini6b8f0182017-03-02 19:56:40 +0100950static void do_nothing(CPUState *cpu, run_on_cpu_data unused)
951{
952}
953
Paolo Bonzini3f53bc62017-03-03 11:50:29 +0100954void qemu_timer_notify_cb(void *opaque, QEMUClockType type)
955{
Paolo Bonzini6b8f0182017-03-02 19:56:40 +0100956 if (!use_icount || type != QEMU_CLOCK_VIRTUAL) {
957 qemu_notify_event();
958 return;
959 }
960
Peter Maydellc52e7132018-04-10 13:02:25 +0100961 if (qemu_in_vcpu_thread()) {
962 /* A CPU is currently running; kick it back out to the
963 * tcg_cpu_exec() loop so it will recalculate its
964 * icount deadline immediately.
965 */
966 qemu_cpu_kick(current_cpu);
967 } else if (first_cpu) {
Paolo Bonzini6b8f0182017-03-02 19:56:40 +0100968 /* qemu_cpu_kick is not enough to kick a halted CPU out of
969 * qemu_tcg_wait_io_event. async_run_on_cpu, instead,
970 * causes cpu_thread_is_idle to return false. This way,
971 * handle_icount_deadline can run.
Peter Maydellc52e7132018-04-10 13:02:25 +0100972 * If we have no CPUs at all for some reason, we don't
973 * need to do anything.
Paolo Bonzini6b8f0182017-03-02 19:56:40 +0100974 */
975 async_run_on_cpu(first_cpu, do_nothing, RUN_ON_CPU_NULL);
976 }
Paolo Bonzini3f53bc62017-03-03 11:50:29 +0100977}
978
Alex Bennée65467062017-02-23 18:29:09 +0000979static void kick_tcg_thread(void *opaque)
980{
981 timer_mod(tcg_kick_vcpu_timer, qemu_tcg_next_kick());
Alex Bennée791158d2017-02-23 18:29:10 +0000982 qemu_cpu_kick_rr_cpu();
Alex Bennée65467062017-02-23 18:29:09 +0000983}
984
985static void start_tcg_kick_timer(void)
986{
Paolo Bonzinidb08b682018-01-11 13:53:12 +0100987 assert(!mttcg_enabled);
988 if (!tcg_kick_vcpu_timer && CPU_NEXT(first_cpu)) {
Alex Bennée65467062017-02-23 18:29:09 +0000989 tcg_kick_vcpu_timer = timer_new_ns(QEMU_CLOCK_VIRTUAL,
990 kick_tcg_thread, NULL);
Alex Bennée1926ab22018-09-27 18:17:24 +0100991 }
992 if (tcg_kick_vcpu_timer && !timer_pending(tcg_kick_vcpu_timer)) {
Alex Bennée65467062017-02-23 18:29:09 +0000993 timer_mod(tcg_kick_vcpu_timer, qemu_tcg_next_kick());
994 }
995}
996
997static void stop_tcg_kick_timer(void)
998{
Paolo Bonzinidb08b682018-01-11 13:53:12 +0100999 assert(!mttcg_enabled);
Alex Bennée1926ab22018-09-27 18:17:24 +01001000 if (tcg_kick_vcpu_timer && timer_pending(tcg_kick_vcpu_timer)) {
Alex Bennée65467062017-02-23 18:29:09 +00001001 timer_del(tcg_kick_vcpu_timer);
Alex Bennée65467062017-02-23 18:29:09 +00001002 }
1003}
1004
Alex Bennée65467062017-02-23 18:29:09 +00001005/***********************************************************/
Blue Swirl296af7c2010-03-29 19:23:50 +00001006void hw_error(const char *fmt, ...)
1007{
1008 va_list ap;
Andreas Färber55e5c282012-12-17 06:18:02 +01001009 CPUState *cpu;
Blue Swirl296af7c2010-03-29 19:23:50 +00001010
1011 va_start(ap, fmt);
1012 fprintf(stderr, "qemu: hardware error: ");
1013 vfprintf(stderr, fmt, ap);
1014 fprintf(stderr, "\n");
Andreas Färberbdc44642013-06-24 23:50:24 +02001015 CPU_FOREACH(cpu) {
Andreas Färber55e5c282012-12-17 06:18:02 +01001016 fprintf(stderr, "CPU #%d:\n", cpu->cpu_index);
Markus Armbruster90c84c52019-04-17 21:18:02 +02001017 cpu_dump_state(cpu, stderr, CPU_DUMP_FPU);
Blue Swirl296af7c2010-03-29 19:23:50 +00001018 }
1019 va_end(ap);
1020 abort();
1021}
1022
1023void cpu_synchronize_all_states(void)
1024{
Andreas Färber182735e2013-05-29 22:29:20 +02001025 CPUState *cpu;
Blue Swirl296af7c2010-03-29 19:23:50 +00001026
Andreas Färberbdc44642013-06-24 23:50:24 +02001027 CPU_FOREACH(cpu) {
Andreas Färber182735e2013-05-29 22:29:20 +02001028 cpu_synchronize_state(cpu);
Sergio Andres Gomez Del Realc97d6d22017-09-13 04:05:09 -05001029 /* TODO: move to cpu_synchronize_state() */
1030 if (hvf_enabled()) {
1031 hvf_cpu_synchronize_state(cpu);
1032 }
Blue Swirl296af7c2010-03-29 19:23:50 +00001033 }
1034}
1035
1036void cpu_synchronize_all_post_reset(void)
1037{
Andreas Färber182735e2013-05-29 22:29:20 +02001038 CPUState *cpu;
Blue Swirl296af7c2010-03-29 19:23:50 +00001039
Andreas Färberbdc44642013-06-24 23:50:24 +02001040 CPU_FOREACH(cpu) {
Andreas Färber182735e2013-05-29 22:29:20 +02001041 cpu_synchronize_post_reset(cpu);
Sergio Andres Gomez Del Realc97d6d22017-09-13 04:05:09 -05001042 /* TODO: move to cpu_synchronize_post_reset() */
1043 if (hvf_enabled()) {
1044 hvf_cpu_synchronize_post_reset(cpu);
1045 }
Blue Swirl296af7c2010-03-29 19:23:50 +00001046 }
1047}
1048
1049void cpu_synchronize_all_post_init(void)
1050{
Andreas Färber182735e2013-05-29 22:29:20 +02001051 CPUState *cpu;
Blue Swirl296af7c2010-03-29 19:23:50 +00001052
Andreas Färberbdc44642013-06-24 23:50:24 +02001053 CPU_FOREACH(cpu) {
Andreas Färber182735e2013-05-29 22:29:20 +02001054 cpu_synchronize_post_init(cpu);
Sergio Andres Gomez Del Realc97d6d22017-09-13 04:05:09 -05001055 /* TODO: move to cpu_synchronize_post_init() */
1056 if (hvf_enabled()) {
1057 hvf_cpu_synchronize_post_init(cpu);
1058 }
Blue Swirl296af7c2010-03-29 19:23:50 +00001059 }
1060}
1061
David Gibson75e972d2017-05-26 14:46:28 +10001062void cpu_synchronize_all_pre_loadvm(void)
1063{
1064 CPUState *cpu;
1065
1066 CPU_FOREACH(cpu) {
1067 cpu_synchronize_pre_loadvm(cpu);
1068 }
1069}
1070
Stefan Hajnoczi4486e892018-03-07 14:42:05 +00001071static int do_vm_stop(RunState state, bool send_stop)
Blue Swirl296af7c2010-03-29 19:23:50 +00001072{
Kevin Wolf56983462013-07-05 13:49:54 +02001073 int ret = 0;
1074
Luiz Capitulino13548692011-07-29 15:36:43 -03001075 if (runstate_is_running()) {
Blue Swirl296af7c2010-03-29 19:23:50 +00001076 cpu_disable_ticks();
Blue Swirl296af7c2010-03-29 19:23:50 +00001077 pause_all_vcpus();
Luiz Capitulinof5bbfba2011-07-29 15:04:45 -03001078 runstate_set(state);
Luiz Capitulino1dfb4dd2011-07-29 14:26:33 -03001079 vm_state_notify(0, state);
Stefan Hajnoczi4486e892018-03-07 14:42:05 +00001080 if (send_stop) {
Peter Xu3ab72382018-08-15 21:37:37 +08001081 qapi_event_send_stop();
Stefan Hajnoczi4486e892018-03-07 14:42:05 +00001082 }
Blue Swirl296af7c2010-03-29 19:23:50 +00001083 }
Kevin Wolf56983462013-07-05 13:49:54 +02001084
Kevin Wolf594a45c2013-07-18 14:52:19 +02001085 bdrv_drain_all();
Pavel Dovgalyuk6d0ceb82016-09-26 11:08:16 +03001086 replay_disable_events();
John Snow22af08e2016-09-22 21:45:51 -04001087 ret = bdrv_flush_all();
Kevin Wolf594a45c2013-07-18 14:52:19 +02001088
Kevin Wolf56983462013-07-05 13:49:54 +02001089 return ret;
Blue Swirl296af7c2010-03-29 19:23:50 +00001090}
1091
Stefan Hajnoczi4486e892018-03-07 14:42:05 +00001092/* Special vm_stop() variant for terminating the process. Historically clients
1093 * did not expect a QMP STOP event and so we need to retain compatibility.
1094 */
1095int vm_shutdown(void)
1096{
1097 return do_vm_stop(RUN_STATE_SHUTDOWN, false);
1098}
1099
Andreas Färbera1fcaa72012-05-02 23:42:26 +02001100static bool cpu_can_run(CPUState *cpu)
Blue Swirl296af7c2010-03-29 19:23:50 +00001101{
Andreas Färber4fdeee72012-05-02 23:10:09 +02001102 if (cpu->stop) {
Andreas Färbera1fcaa72012-05-02 23:42:26 +02001103 return false;
Jan Kiszka0ab07c62011-02-07 12:19:14 +01001104 }
Tiejun Chen321bc0b2013-08-02 09:43:09 +08001105 if (cpu_is_stopped(cpu)) {
Andreas Färbera1fcaa72012-05-02 23:42:26 +02001106 return false;
Jan Kiszka0ab07c62011-02-07 12:19:14 +01001107 }
Andreas Färbera1fcaa72012-05-02 23:42:26 +02001108 return true;
Blue Swirl296af7c2010-03-29 19:23:50 +00001109}
1110
Andreas Färber91325042013-05-27 02:07:49 +02001111static void cpu_handle_guest_debug(CPUState *cpu)
Jan Kiszka3c638d02010-06-25 16:56:56 +02001112{
Andreas Färber64f6b342013-05-27 02:06:09 +02001113 gdb_set_stop_cpu(cpu);
Jan Kiszka8cf71712011-02-07 12:19:16 +01001114 qemu_system_debug_request();
Andreas Färberf324e762012-05-02 23:26:21 +02001115 cpu->stopped = true;
Jan Kiszka3c638d02010-06-25 16:56:56 +02001116}
1117
Jan Kiszka6d9cb732011-02-01 22:15:58 +01001118#ifdef CONFIG_LINUX
1119static void sigbus_reraise(void)
1120{
1121 sigset_t set;
1122 struct sigaction action;
1123
1124 memset(&action, 0, sizeof(action));
1125 action.sa_handler = SIG_DFL;
1126 if (!sigaction(SIGBUS, &action, NULL)) {
1127 raise(SIGBUS);
1128 sigemptyset(&set);
1129 sigaddset(&set, SIGBUS);
Peter Maydella2d17612016-05-16 18:33:59 +01001130 pthread_sigmask(SIG_UNBLOCK, &set, NULL);
Jan Kiszka6d9cb732011-02-01 22:15:58 +01001131 }
1132 perror("Failed to re-raise SIGBUS!\n");
1133 abort();
1134}
1135
Paolo Bonzinid98d4072017-02-08 13:22:12 +01001136static void sigbus_handler(int n, siginfo_t *siginfo, void *ctx)
Jan Kiszka6d9cb732011-02-01 22:15:58 +01001137{
Paolo Bonzinia16fc072017-02-09 09:50:02 +01001138 if (siginfo->si_code != BUS_MCEERR_AO && siginfo->si_code != BUS_MCEERR_AR) {
1139 sigbus_reraise();
1140 }
1141
Paolo Bonzini2ae41db2017-02-08 12:48:54 +01001142 if (current_cpu) {
1143 /* Called asynchronously in VCPU thread. */
1144 if (kvm_on_sigbus_vcpu(current_cpu, siginfo->si_code, siginfo->si_addr)) {
1145 sigbus_reraise();
1146 }
1147 } else {
1148 /* Called synchronously (via signalfd) in main thread. */
1149 if (kvm_on_sigbus(siginfo->si_code, siginfo->si_addr)) {
1150 sigbus_reraise();
1151 }
Jan Kiszka6d9cb732011-02-01 22:15:58 +01001152 }
1153}
1154
1155static void qemu_init_sigbus(void)
1156{
1157 struct sigaction action;
1158
1159 memset(&action, 0, sizeof(action));
1160 action.sa_flags = SA_SIGINFO;
Paolo Bonzinid98d4072017-02-08 13:22:12 +01001161 action.sa_sigaction = sigbus_handler;
Jan Kiszka6d9cb732011-02-01 22:15:58 +01001162 sigaction(SIGBUS, &action, NULL);
1163
1164 prctl(PR_MCE_KILL, PR_MCE_KILL_SET, PR_MCE_KILL_EARLY, 0, 0);
1165}
Paolo Bonzinia16fc072017-02-09 09:50:02 +01001166#else /* !CONFIG_LINUX */
1167static void qemu_init_sigbus(void)
1168{
1169}
Paolo Bonzinia16fc072017-02-09 09:50:02 +01001170#endif /* !CONFIG_LINUX */
Blue Swirl296af7c2010-03-29 19:23:50 +00001171
Stefan Weilb2532d82012-09-27 07:41:42 +02001172static QemuMutex qemu_global_mutex;
Blue Swirl296af7c2010-03-29 19:23:50 +00001173
1174static QemuThread io_thread;
1175
Blue Swirl296af7c2010-03-29 19:23:50 +00001176/* cpu creation */
1177static QemuCond qemu_cpu_cond;
1178/* system init */
Blue Swirl296af7c2010-03-29 19:23:50 +00001179static QemuCond qemu_pause_cond;
1180
Paolo Bonzinid3b12f52011-09-13 10:30:52 +02001181void qemu_init_cpu_loop(void)
Blue Swirl296af7c2010-03-29 19:23:50 +00001182{
Jan Kiszka6d9cb732011-02-01 22:15:58 +01001183 qemu_init_sigbus();
Anthony Liguoried945922011-02-08 18:18:18 +01001184 qemu_cond_init(&qemu_cpu_cond);
Anthony Liguoried945922011-02-08 18:18:18 +01001185 qemu_cond_init(&qemu_pause_cond);
Blue Swirl296af7c2010-03-29 19:23:50 +00001186 qemu_mutex_init(&qemu_global_mutex);
Blue Swirl296af7c2010-03-29 19:23:50 +00001187
Jan Kiszkab7680cb2011-03-12 17:43:51 +01001188 qemu_thread_get_self(&io_thread);
Blue Swirl296af7c2010-03-29 19:23:50 +00001189}
1190
Paolo Bonzini14e6fe12016-10-31 10:36:08 +01001191void run_on_cpu(CPUState *cpu, run_on_cpu_func func, run_on_cpu_data data)
Marcelo Tosattie82bcec2010-05-04 09:45:22 -03001192{
Sergey Fedorovd148d902016-08-29 09:51:00 +02001193 do_run_on_cpu(cpu, func, data, &qemu_global_mutex);
Chegu Vinod3c022702013-06-24 03:49:41 -06001194}
1195
Gu Zheng4c055ab2016-05-12 09:18:13 +05301196static void qemu_kvm_destroy_vcpu(CPUState *cpu)
1197{
1198 if (kvm_destroy_vcpu(cpu) < 0) {
1199 error_report("kvm_destroy_vcpu failed");
1200 exit(EXIT_FAILURE);
1201 }
1202}
1203
1204static void qemu_tcg_destroy_vcpu(CPUState *cpu)
1205{
1206}
1207
David Hildenbrandebd05fe2017-11-29 20:12:15 +01001208static void qemu_cpu_stop(CPUState *cpu, bool exit)
1209{
1210 g_assert(qemu_cpu_is_self(cpu));
1211 cpu->stop = false;
1212 cpu->stopped = true;
1213 if (exit) {
1214 cpu_exit(cpu);
1215 }
1216 qemu_cond_broadcast(&qemu_pause_cond);
1217}
1218
Andreas Färber509a0d72012-05-03 02:18:09 +02001219static void qemu_wait_io_event_common(CPUState *cpu)
Blue Swirl296af7c2010-03-29 19:23:50 +00001220{
Alex Bennée37257942017-02-23 18:29:14 +00001221 atomic_mb_set(&cpu->thread_kicked, false);
Andreas Färber4fdeee72012-05-02 23:10:09 +02001222 if (cpu->stop) {
David Hildenbrandebd05fe2017-11-29 20:12:15 +01001223 qemu_cpu_stop(cpu, false);
Blue Swirl296af7c2010-03-29 19:23:50 +00001224 }
Sergey Fedorova5403c62016-08-02 18:27:36 +01001225 process_queued_cpu_work(cpu);
Alex Bennée37257942017-02-23 18:29:14 +00001226}
1227
Paolo Bonzinia8efa602018-11-14 12:36:57 +01001228static void qemu_tcg_rr_wait_io_event(void)
Alex Bennée37257942017-02-23 18:29:14 +00001229{
Paolo Bonzinia8efa602018-11-14 12:36:57 +01001230 CPUState *cpu;
1231
Paolo Bonzinidb08b682018-01-11 13:53:12 +01001232 while (all_cpu_threads_idle()) {
Alex Bennée65467062017-02-23 18:29:09 +00001233 stop_tcg_kick_timer();
Paolo Bonzinia8efa602018-11-14 12:36:57 +01001234 qemu_cond_wait(first_cpu->halt_cond, &qemu_global_mutex);
Jan Kiszka16400322011-02-09 16:29:37 +01001235 }
Blue Swirl296af7c2010-03-29 19:23:50 +00001236
Alex Bennée65467062017-02-23 18:29:09 +00001237 start_tcg_kick_timer();
1238
Paolo Bonzinia8efa602018-11-14 12:36:57 +01001239 CPU_FOREACH(cpu) {
1240 qemu_wait_io_event_common(cpu);
1241 }
Blue Swirl296af7c2010-03-29 19:23:50 +00001242}
1243
Paolo Bonzinidb08b682018-01-11 13:53:12 +01001244static void qemu_wait_io_event(CPUState *cpu)
Blue Swirl296af7c2010-03-29 19:23:50 +00001245{
Andreas Färbera98ae1d2013-05-26 23:21:08 +02001246 while (cpu_thread_is_idle(cpu)) {
Andreas Färberf5c121b2012-05-03 01:22:49 +02001247 qemu_cond_wait(cpu->halt_cond, &qemu_global_mutex);
Jan Kiszka16400322011-02-09 16:29:37 +01001248 }
Blue Swirl296af7c2010-03-29 19:23:50 +00001249
Paolo Bonzinidb08b682018-01-11 13:53:12 +01001250#ifdef _WIN32
1251 /* Eat dummy APC queued by qemu_cpu_kick_thread. */
1252 if (!tcg_enabled()) {
1253 SleepEx(0, TRUE);
Sergio Andres Gomez Del Realc97d6d22017-09-13 04:05:09 -05001254 }
Paolo Bonzinidb08b682018-01-11 13:53:12 +01001255#endif
Sergio Andres Gomez Del Realc97d6d22017-09-13 04:05:09 -05001256 qemu_wait_io_event_common(cpu);
1257}
1258
Jan Kiszka7e97cd82011-02-07 12:19:12 +01001259static void *qemu_kvm_cpu_thread_fn(void *arg)
Blue Swirl296af7c2010-03-29 19:23:50 +00001260{
Andreas Färber48a106b2013-05-27 02:20:39 +02001261 CPUState *cpu = arg;
Jan Kiszka84b49152011-02-01 22:15:50 +01001262 int r;
Blue Swirl296af7c2010-03-29 19:23:50 +00001263
Paolo Bonziniab28bd22015-07-09 08:55:38 +02001264 rcu_register_thread();
1265
Paolo Bonzini2e7f7a32015-06-18 18:47:18 +02001266 qemu_mutex_lock_iothread();
Andreas Färber814e6122012-05-02 17:00:37 +02001267 qemu_thread_get_self(cpu->thread);
Andreas Färber9f09e182012-05-03 06:59:07 +02001268 cpu->thread_id = qemu_get_thread_id();
Pavel Dovgalyuk626cf8f2014-12-08 10:53:17 +03001269 cpu->can_do_io = 1;
Andreas Färber4917cf42013-05-27 05:17:50 +02001270 current_cpu = cpu;
Blue Swirl296af7c2010-03-29 19:23:50 +00001271
Andreas Färber504134d2012-12-17 06:38:45 +01001272 r = kvm_init_vcpu(cpu);
Jan Kiszka84b49152011-02-01 22:15:50 +01001273 if (r < 0) {
Alistair Francis493d89b2018-02-03 09:43:14 +01001274 error_report("kvm_init_vcpu failed: %s", strerror(-r));
Jan Kiszka84b49152011-02-01 22:15:50 +01001275 exit(1);
1276 }
Blue Swirl296af7c2010-03-29 19:23:50 +00001277
Paolo Bonzini18268b62017-02-09 09:41:14 +01001278 kvm_init_cpu_signals(cpu);
Blue Swirl296af7c2010-03-29 19:23:50 +00001279
1280 /* signal CPU creation */
Andreas Färber61a46212012-05-02 22:49:36 +02001281 cpu->created = true;
Blue Swirl296af7c2010-03-29 19:23:50 +00001282 qemu_cond_signal(&qemu_cpu_cond);
Richard Henderson9c09a252019-03-14 13:06:29 -07001283 qemu_guest_random_seed_thread_part2(cpu->random_seed);
Blue Swirl296af7c2010-03-29 19:23:50 +00001284
Gu Zheng4c055ab2016-05-12 09:18:13 +05301285 do {
Andreas Färbera1fcaa72012-05-02 23:42:26 +02001286 if (cpu_can_run(cpu)) {
Andreas Färber1458c362013-05-26 23:46:55 +02001287 r = kvm_cpu_exec(cpu);
Jan Kiszka83f338f2011-02-07 12:19:17 +01001288 if (r == EXCP_DEBUG) {
Andreas Färber91325042013-05-27 02:07:49 +02001289 cpu_handle_guest_debug(cpu);
Jan Kiszka83f338f2011-02-07 12:19:17 +01001290 }
Jan Kiszka0ab07c62011-02-07 12:19:14 +01001291 }
Paolo Bonzinidb08b682018-01-11 13:53:12 +01001292 qemu_wait_io_event(cpu);
Gu Zheng4c055ab2016-05-12 09:18:13 +05301293 } while (!cpu->unplug || cpu_can_run(cpu));
Blue Swirl296af7c2010-03-29 19:23:50 +00001294
Gu Zheng4c055ab2016-05-12 09:18:13 +05301295 qemu_kvm_destroy_vcpu(cpu);
Bharata B Rao2c579042016-05-12 09:18:14 +05301296 cpu->created = false;
1297 qemu_cond_signal(&qemu_cpu_cond);
Gu Zheng4c055ab2016-05-12 09:18:13 +05301298 qemu_mutex_unlock_iothread();
Paolo Bonzini57615ed2018-01-30 11:04:36 -05001299 rcu_unregister_thread();
Blue Swirl296af7c2010-03-29 19:23:50 +00001300 return NULL;
1301}
1302
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +02001303static void *qemu_dummy_cpu_thread_fn(void *arg)
1304{
1305#ifdef _WIN32
Alistair Francis493d89b2018-02-03 09:43:14 +01001306 error_report("qtest is not supported under Windows");
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +02001307 exit(1);
1308#else
Andreas Färber10a90212013-05-27 02:24:35 +02001309 CPUState *cpu = arg;
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +02001310 sigset_t waitset;
1311 int r;
1312
Paolo Bonziniab28bd22015-07-09 08:55:38 +02001313 rcu_register_thread();
1314
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +02001315 qemu_mutex_lock_iothread();
Andreas Färber814e6122012-05-02 17:00:37 +02001316 qemu_thread_get_self(cpu->thread);
Andreas Färber9f09e182012-05-03 06:59:07 +02001317 cpu->thread_id = qemu_get_thread_id();
Pavel Dovgalyuk626cf8f2014-12-08 10:53:17 +03001318 cpu->can_do_io = 1;
Alex Bennée37257942017-02-23 18:29:14 +00001319 current_cpu = cpu;
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +02001320
1321 sigemptyset(&waitset);
1322 sigaddset(&waitset, SIG_IPI);
1323
1324 /* signal CPU creation */
Andreas Färber61a46212012-05-02 22:49:36 +02001325 cpu->created = true;
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +02001326 qemu_cond_signal(&qemu_cpu_cond);
Richard Henderson9c09a252019-03-14 13:06:29 -07001327 qemu_guest_random_seed_thread_part2(cpu->random_seed);
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +02001328
Paolo Bonzinid2831ab2018-01-30 11:04:53 -05001329 do {
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +02001330 qemu_mutex_unlock_iothread();
1331 do {
1332 int sig;
1333 r = sigwait(&waitset, &sig);
1334 } while (r == -1 && (errno == EAGAIN || errno == EINTR));
1335 if (r == -1) {
1336 perror("sigwait");
1337 exit(1);
1338 }
1339 qemu_mutex_lock_iothread();
Paolo Bonzinidb08b682018-01-11 13:53:12 +01001340 qemu_wait_io_event(cpu);
Paolo Bonzinid2831ab2018-01-30 11:04:53 -05001341 } while (!cpu->unplug);
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +02001342
David Hildenbrandd40bfcb2019-02-18 10:21:57 +01001343 qemu_mutex_unlock_iothread();
Paolo Bonzinid2831ab2018-01-30 11:04:53 -05001344 rcu_unregister_thread();
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +02001345 return NULL;
1346#endif
1347}
1348
Alex Bennée1be7fcb2016-10-27 16:10:08 +01001349static int64_t tcg_get_icount_limit(void)
1350{
1351 int64_t deadline;
1352
1353 if (replay_mode != REPLAY_MODE_PLAY) {
1354 deadline = qemu_clock_deadline_ns_all(QEMU_CLOCK_VIRTUAL);
1355
1356 /* Maintain prior (possibly buggy) behaviour where if no deadline
1357 * was set (as there is no QEMU_CLOCK_VIRTUAL timer) or it is more than
1358 * INT32_MAX nanoseconds ahead, we still use INT32_MAX
1359 * nanoseconds.
1360 */
1361 if ((deadline < 0) || (deadline > INT32_MAX)) {
1362 deadline = INT32_MAX;
1363 }
1364
1365 return qemu_icount_round(deadline);
1366 } else {
1367 return replay_get_instructions();
1368 }
1369}
1370
Alex Bennée12e97002016-10-27 16:10:14 +01001371static void handle_icount_deadline(void)
1372{
Paolo Bonzini6b8f0182017-03-02 19:56:40 +01001373 assert(qemu_in_vcpu_thread());
Alex Bennée12e97002016-10-27 16:10:14 +01001374 if (use_icount) {
1375 int64_t deadline =
1376 qemu_clock_deadline_ns_all(QEMU_CLOCK_VIRTUAL);
1377
1378 if (deadline == 0) {
Paolo Bonzini6b8f0182017-03-02 19:56:40 +01001379 /* Wake up other AioContexts. */
Alex Bennée12e97002016-10-27 16:10:14 +01001380 qemu_clock_notify(QEMU_CLOCK_VIRTUAL);
Paolo Bonzini6b8f0182017-03-02 19:56:40 +01001381 qemu_clock_run_timers(QEMU_CLOCK_VIRTUAL);
Alex Bennée12e97002016-10-27 16:10:14 +01001382 }
1383 }
1384}
1385
Alex Bennée05248382017-03-29 16:46:59 +01001386static void prepare_icount_for_run(CPUState *cpu)
1387{
1388 if (use_icount) {
Alex Bennéeeda5f7c2017-04-05 12:35:48 +01001389 int insns_left;
Alex Bennée05248382017-03-29 16:46:59 +01001390
1391 /* These should always be cleared by process_icount_data after
1392 * each vCPU execution. However u16.high can be raised
1393 * asynchronously by cpu_exit/cpu_interrupt/tcg_handle_interrupt
1394 */
Richard Henderson5e140192019-03-28 11:54:23 -10001395 g_assert(cpu_neg(cpu)->icount_decr.u16.low == 0);
Alex Bennée05248382017-03-29 16:46:59 +01001396 g_assert(cpu->icount_extra == 0);
1397
Alex Bennéeeda5f7c2017-04-05 12:35:48 +01001398 cpu->icount_budget = tcg_get_icount_limit();
1399 insns_left = MIN(0xffff, cpu->icount_budget);
Richard Henderson5e140192019-03-28 11:54:23 -10001400 cpu_neg(cpu)->icount_decr.u16.low = insns_left;
Alex Bennéeeda5f7c2017-04-05 12:35:48 +01001401 cpu->icount_extra = cpu->icount_budget - insns_left;
Alex Bennéed759c952018-02-27 12:52:48 +03001402
1403 replay_mutex_lock();
Alex Bennée05248382017-03-29 16:46:59 +01001404 }
1405}
1406
1407static void process_icount_data(CPUState *cpu)
1408{
1409 if (use_icount) {
Alex Bennéee4cd9652017-03-31 16:09:42 +01001410 /* Account for executed instructions */
Alex Bennée512d3c82017-04-05 12:32:37 +01001411 cpu_update_icount(cpu);
Alex Bennée05248382017-03-29 16:46:59 +01001412
1413 /* Reset the counters */
Richard Henderson5e140192019-03-28 11:54:23 -10001414 cpu_neg(cpu)->icount_decr.u16.low = 0;
Alex Bennée05248382017-03-29 16:46:59 +01001415 cpu->icount_extra = 0;
Alex Bennéee4cd9652017-03-31 16:09:42 +01001416 cpu->icount_budget = 0;
1417
Alex Bennée05248382017-03-29 16:46:59 +01001418 replay_account_executed_instructions();
Alex Bennéed759c952018-02-27 12:52:48 +03001419
1420 replay_mutex_unlock();
Alex Bennée05248382017-03-29 16:46:59 +01001421 }
1422}
1423
1424
Alex Bennée1be7fcb2016-10-27 16:10:08 +01001425static int tcg_cpu_exec(CPUState *cpu)
1426{
1427 int ret;
1428#ifdef CONFIG_PROFILER
1429 int64_t ti;
1430#endif
1431
Emilio G. Cotaf28d0df2018-06-22 13:45:31 -04001432 assert(tcg_enabled());
Alex Bennée1be7fcb2016-10-27 16:10:08 +01001433#ifdef CONFIG_PROFILER
1434 ti = profile_getclock();
1435#endif
Alex Bennée1be7fcb2016-10-27 16:10:08 +01001436 cpu_exec_start(cpu);
1437 ret = cpu_exec(cpu);
1438 cpu_exec_end(cpu);
1439#ifdef CONFIG_PROFILER
Emilio G. Cota72fd2ef2018-10-10 10:48:53 -04001440 atomic_set(&tcg_ctx->prof.cpu_exec_time,
1441 tcg_ctx->prof.cpu_exec_time + profile_getclock() - ti);
Alex Bennée1be7fcb2016-10-27 16:10:08 +01001442#endif
Alex Bennée1be7fcb2016-10-27 16:10:08 +01001443 return ret;
1444}
1445
Alex Bennéec93bbbe2016-10-27 16:10:09 +01001446/* Destroy any remaining vCPUs which have been unplugged and have
1447 * finished running
1448 */
1449static void deal_with_unplugged_cpus(void)
Alex Bennée1be7fcb2016-10-27 16:10:08 +01001450{
Alex Bennéec93bbbe2016-10-27 16:10:09 +01001451 CPUState *cpu;
Alex Bennée1be7fcb2016-10-27 16:10:08 +01001452
Alex Bennéec93bbbe2016-10-27 16:10:09 +01001453 CPU_FOREACH(cpu) {
1454 if (cpu->unplug && !cpu_can_run(cpu)) {
1455 qemu_tcg_destroy_vcpu(cpu);
1456 cpu->created = false;
1457 qemu_cond_signal(&qemu_cpu_cond);
Alex Bennée1be7fcb2016-10-27 16:10:08 +01001458 break;
1459 }
1460 }
Alex Bennée1be7fcb2016-10-27 16:10:08 +01001461}
Jan Kiszkabdb7ca62011-09-26 09:40:39 +02001462
Alex Bennée65467062017-02-23 18:29:09 +00001463/* Single-threaded TCG
1464 *
1465 * In the single-threaded case each vCPU is simulated in turn. If
1466 * there is more than a single vCPU we create a simple timer to kick
1467 * the vCPU and ensure we don't get stuck in a tight loop in one vCPU.
1468 * This is done explicitly rather than relying on side-effects
1469 * elsewhere.
1470 */
1471
Alex Bennée37257942017-02-23 18:29:14 +00001472static void *qemu_tcg_rr_cpu_thread_fn(void *arg)
Blue Swirl296af7c2010-03-29 19:23:50 +00001473{
Andreas Färberc3586ba2012-05-03 01:41:24 +02001474 CPUState *cpu = arg;
Blue Swirl296af7c2010-03-29 19:23:50 +00001475
Emilio G. Cotaf28d0df2018-06-22 13:45:31 -04001476 assert(tcg_enabled());
Paolo Bonziniab28bd22015-07-09 08:55:38 +02001477 rcu_register_thread();
Emilio G. Cota3468b592017-07-19 18:57:58 -04001478 tcg_register_thread();
Paolo Bonziniab28bd22015-07-09 08:55:38 +02001479
Paolo Bonzini2e7f7a32015-06-18 18:47:18 +02001480 qemu_mutex_lock_iothread();
Andreas Färber814e6122012-05-02 17:00:37 +02001481 qemu_thread_get_self(cpu->thread);
Blue Swirl296af7c2010-03-29 19:23:50 +00001482
David Hildenbrand5a9c9732018-02-09 20:52:39 +01001483 cpu->thread_id = qemu_get_thread_id();
1484 cpu->created = true;
1485 cpu->can_do_io = 1;
Blue Swirl296af7c2010-03-29 19:23:50 +00001486 qemu_cond_signal(&qemu_cpu_cond);
Richard Henderson9c09a252019-03-14 13:06:29 -07001487 qemu_guest_random_seed_thread_part2(cpu->random_seed);
Blue Swirl296af7c2010-03-29 19:23:50 +00001488
Jan Kiszkafa7d1862011-08-22 18:35:25 +02001489 /* wait for initial kick-off after machine start */
Emilio G. Cotac28e3992015-04-27 12:45:28 -04001490 while (first_cpu->stopped) {
KONRAD Fredericd5f8d612015-08-10 17:27:06 +02001491 qemu_cond_wait(first_cpu->halt_cond, &qemu_global_mutex);
Jan Kiszka8e564b42012-02-17 18:31:15 +01001492
1493 /* process any pending work */
Andreas Färberbdc44642013-06-24 23:50:24 +02001494 CPU_FOREACH(cpu) {
Alex Bennée37257942017-02-23 18:29:14 +00001495 current_cpu = cpu;
Andreas Färber182735e2013-05-29 22:29:20 +02001496 qemu_wait_io_event_common(cpu);
Jan Kiszka8e564b42012-02-17 18:31:15 +01001497 }
Jan Kiszka0ab07c62011-02-07 12:19:14 +01001498 }
Blue Swirl296af7c2010-03-29 19:23:50 +00001499
Alex Bennée65467062017-02-23 18:29:09 +00001500 start_tcg_kick_timer();
1501
Alex Bennéec93bbbe2016-10-27 16:10:09 +01001502 cpu = first_cpu;
1503
Alex Bennéee5143e32017-02-23 18:29:12 +00001504 /* process any pending work */
1505 cpu->exit_request = 1;
1506
Blue Swirl296af7c2010-03-29 19:23:50 +00001507 while (1) {
Alex Bennéed759c952018-02-27 12:52:48 +03001508 qemu_mutex_unlock_iothread();
1509 replay_mutex_lock();
1510 qemu_mutex_lock_iothread();
Alex Bennéec93bbbe2016-10-27 16:10:09 +01001511 /* Account partial waits to QEMU_CLOCK_VIRTUAL. */
1512 qemu_account_warp_timer();
1513
Paolo Bonzini6b8f0182017-03-02 19:56:40 +01001514 /* Run the timers here. This is much more efficient than
1515 * waking up the I/O thread and waiting for completion.
1516 */
1517 handle_icount_deadline();
1518
Alex Bennéed759c952018-02-27 12:52:48 +03001519 replay_mutex_unlock();
1520
Alex Bennéec93bbbe2016-10-27 16:10:09 +01001521 if (!cpu) {
1522 cpu = first_cpu;
1523 }
1524
Alex Bennéee5143e32017-02-23 18:29:12 +00001525 while (cpu && !cpu->queued_work_first && !cpu->exit_request) {
1526
Alex Bennée791158d2017-02-23 18:29:10 +00001527 atomic_mb_set(&tcg_current_rr_cpu, cpu);
Alex Bennée37257942017-02-23 18:29:14 +00001528 current_cpu = cpu;
Alex Bennéec93bbbe2016-10-27 16:10:09 +01001529
1530 qemu_clock_enable(QEMU_CLOCK_VIRTUAL,
1531 (cpu->singlestep_enabled & SSTEP_NOTIMER) == 0);
1532
1533 if (cpu_can_run(cpu)) {
1534 int r;
Alex Bennée05248382017-03-29 16:46:59 +01001535
Alex Bennéed759c952018-02-27 12:52:48 +03001536 qemu_mutex_unlock_iothread();
Alex Bennée05248382017-03-29 16:46:59 +01001537 prepare_icount_for_run(cpu);
1538
Alex Bennéec93bbbe2016-10-27 16:10:09 +01001539 r = tcg_cpu_exec(cpu);
Alex Bennée05248382017-03-29 16:46:59 +01001540
1541 process_icount_data(cpu);
Alex Bennéed759c952018-02-27 12:52:48 +03001542 qemu_mutex_lock_iothread();
Alex Bennée05248382017-03-29 16:46:59 +01001543
Alex Bennéec93bbbe2016-10-27 16:10:09 +01001544 if (r == EXCP_DEBUG) {
1545 cpu_handle_guest_debug(cpu);
1546 break;
Pranith Kumar08e73c42017-02-23 18:29:15 +00001547 } else if (r == EXCP_ATOMIC) {
1548 qemu_mutex_unlock_iothread();
1549 cpu_exec_step_atomic(cpu);
1550 qemu_mutex_lock_iothread();
1551 break;
Alex Bennéec93bbbe2016-10-27 16:10:09 +01001552 }
Alex Bennée37257942017-02-23 18:29:14 +00001553 } else if (cpu->stop) {
Alex Bennéec93bbbe2016-10-27 16:10:09 +01001554 if (cpu->unplug) {
1555 cpu = CPU_NEXT(cpu);
1556 }
1557 break;
1558 }
1559
Alex Bennéee5143e32017-02-23 18:29:12 +00001560 cpu = CPU_NEXT(cpu);
1561 } /* while (cpu && !cpu->exit_request).. */
1562
Alex Bennée791158d2017-02-23 18:29:10 +00001563 /* Does not need atomic_mb_set because a spurious wakeup is okay. */
1564 atomic_set(&tcg_current_rr_cpu, NULL);
Alex Bennéec93bbbe2016-10-27 16:10:09 +01001565
Alex Bennéee5143e32017-02-23 18:29:12 +00001566 if (cpu && cpu->exit_request) {
1567 atomic_mb_set(&cpu->exit_request, 0);
1568 }
Alex Blighac70aaf2013-08-21 16:02:57 +01001569
Clement Deschamps013aabd2018-10-21 16:21:03 +02001570 if (use_icount && all_cpu_threads_idle()) {
1571 /*
1572 * When all cpus are sleeping (e.g in WFI), to avoid a deadlock
1573 * in the main_loop, wake it up in order to start the warp timer.
1574 */
1575 qemu_notify_event();
1576 }
1577
Paolo Bonzinia8efa602018-11-14 12:36:57 +01001578 qemu_tcg_rr_wait_io_event();
Alex Bennéec93bbbe2016-10-27 16:10:09 +01001579 deal_with_unplugged_cpus();
Blue Swirl296af7c2010-03-29 19:23:50 +00001580 }
1581
Paolo Bonzini9b0605f2018-01-30 11:05:06 -05001582 rcu_unregister_thread();
Blue Swirl296af7c2010-03-29 19:23:50 +00001583 return NULL;
1584}
1585
Vincent Palatinb0cb0a62017-01-10 11:59:57 +01001586static void *qemu_hax_cpu_thread_fn(void *arg)
1587{
1588 CPUState *cpu = arg;
1589 int r;
Vincent Palatinb3d3a422017-03-20 11:15:49 +01001590
Paolo Bonzini9857c2d2018-01-30 16:28:49 +01001591 rcu_register_thread();
Vincent Palatinb3d3a422017-03-20 11:15:49 +01001592 qemu_mutex_lock_iothread();
Vincent Palatinb0cb0a62017-01-10 11:59:57 +01001593 qemu_thread_get_self(cpu->thread);
Vincent Palatinb0cb0a62017-01-10 11:59:57 +01001594
1595 cpu->thread_id = qemu_get_thread_id();
1596 cpu->created = true;
Vincent Palatinb0cb0a62017-01-10 11:59:57 +01001597 current_cpu = cpu;
1598
1599 hax_init_vcpu(cpu);
1600 qemu_cond_signal(&qemu_cpu_cond);
Richard Henderson9c09a252019-03-14 13:06:29 -07001601 qemu_guest_random_seed_thread_part2(cpu->random_seed);
Vincent Palatinb0cb0a62017-01-10 11:59:57 +01001602
Paolo Bonzini9857c2d2018-01-30 16:28:49 +01001603 do {
Vincent Palatinb0cb0a62017-01-10 11:59:57 +01001604 if (cpu_can_run(cpu)) {
1605 r = hax_smp_cpu_exec(cpu);
1606 if (r == EXCP_DEBUG) {
1607 cpu_handle_guest_debug(cpu);
1608 }
1609 }
1610
Paolo Bonzinidb08b682018-01-11 13:53:12 +01001611 qemu_wait_io_event(cpu);
Paolo Bonzini9857c2d2018-01-30 16:28:49 +01001612 } while (!cpu->unplug || cpu_can_run(cpu));
1613 rcu_unregister_thread();
Vincent Palatinb0cb0a62017-01-10 11:59:57 +01001614 return NULL;
1615}
1616
Sergio Andres Gomez Del Realc97d6d22017-09-13 04:05:09 -05001617/* The HVF-specific vCPU thread function. This one should only run when the host
1618 * CPU supports the VMX "unrestricted guest" feature. */
1619static void *qemu_hvf_cpu_thread_fn(void *arg)
1620{
1621 CPUState *cpu = arg;
1622
1623 int r;
1624
1625 assert(hvf_enabled());
1626
1627 rcu_register_thread();
1628
1629 qemu_mutex_lock_iothread();
1630 qemu_thread_get_self(cpu->thread);
1631
1632 cpu->thread_id = qemu_get_thread_id();
1633 cpu->can_do_io = 1;
1634 current_cpu = cpu;
1635
1636 hvf_init_vcpu(cpu);
1637
1638 /* signal CPU creation */
1639 cpu->created = true;
1640 qemu_cond_signal(&qemu_cpu_cond);
Richard Henderson9c09a252019-03-14 13:06:29 -07001641 qemu_guest_random_seed_thread_part2(cpu->random_seed);
Sergio Andres Gomez Del Realc97d6d22017-09-13 04:05:09 -05001642
1643 do {
1644 if (cpu_can_run(cpu)) {
1645 r = hvf_vcpu_exec(cpu);
1646 if (r == EXCP_DEBUG) {
1647 cpu_handle_guest_debug(cpu);
1648 }
1649 }
Paolo Bonzinidb08b682018-01-11 13:53:12 +01001650 qemu_wait_io_event(cpu);
Sergio Andres Gomez Del Realc97d6d22017-09-13 04:05:09 -05001651 } while (!cpu->unplug || cpu_can_run(cpu));
1652
1653 hvf_vcpu_destroy(cpu);
1654 cpu->created = false;
1655 qemu_cond_signal(&qemu_cpu_cond);
1656 qemu_mutex_unlock_iothread();
Paolo Bonzini8178e632018-01-30 11:05:21 -05001657 rcu_unregister_thread();
Sergio Andres Gomez Del Realc97d6d22017-09-13 04:05:09 -05001658 return NULL;
1659}
1660
Justin Terry (VM)19306802018-01-22 13:07:49 -08001661static void *qemu_whpx_cpu_thread_fn(void *arg)
1662{
1663 CPUState *cpu = arg;
1664 int r;
1665
1666 rcu_register_thread();
1667
1668 qemu_mutex_lock_iothread();
1669 qemu_thread_get_self(cpu->thread);
1670 cpu->thread_id = qemu_get_thread_id();
1671 current_cpu = cpu;
1672
1673 r = whpx_init_vcpu(cpu);
1674 if (r < 0) {
1675 fprintf(stderr, "whpx_init_vcpu failed: %s\n", strerror(-r));
1676 exit(1);
1677 }
1678
1679 /* signal CPU creation */
1680 cpu->created = true;
1681 qemu_cond_signal(&qemu_cpu_cond);
Richard Henderson9c09a252019-03-14 13:06:29 -07001682 qemu_guest_random_seed_thread_part2(cpu->random_seed);
Justin Terry (VM)19306802018-01-22 13:07:49 -08001683
1684 do {
1685 if (cpu_can_run(cpu)) {
1686 r = whpx_vcpu_exec(cpu);
1687 if (r == EXCP_DEBUG) {
1688 cpu_handle_guest_debug(cpu);
1689 }
1690 }
1691 while (cpu_thread_is_idle(cpu)) {
1692 qemu_cond_wait(cpu->halt_cond, &qemu_global_mutex);
1693 }
1694 qemu_wait_io_event_common(cpu);
1695 } while (!cpu->unplug || cpu_can_run(cpu));
1696
1697 whpx_destroy_vcpu(cpu);
1698 cpu->created = false;
1699 qemu_cond_signal(&qemu_cpu_cond);
1700 qemu_mutex_unlock_iothread();
1701 rcu_unregister_thread();
Vincent Palatinb0cb0a62017-01-10 11:59:57 +01001702 return NULL;
1703}
1704
1705#ifdef _WIN32
1706static void CALLBACK dummy_apc_func(ULONG_PTR unused)
1707{
1708}
1709#endif
1710
Alex Bennée37257942017-02-23 18:29:14 +00001711/* Multi-threaded TCG
1712 *
1713 * In the multi-threaded case each vCPU has its own thread. The TLS
1714 * variable current_cpu can be used deep in the code to find the
1715 * current CPUState for a given thread.
1716 */
1717
1718static void *qemu_tcg_cpu_thread_fn(void *arg)
1719{
1720 CPUState *cpu = arg;
1721
Emilio G. Cotaf28d0df2018-06-22 13:45:31 -04001722 assert(tcg_enabled());
Alex Bennéebf51c722017-03-30 18:32:29 +01001723 g_assert(!use_icount);
1724
Alex Bennée37257942017-02-23 18:29:14 +00001725 rcu_register_thread();
Emilio G. Cota3468b592017-07-19 18:57:58 -04001726 tcg_register_thread();
Alex Bennée37257942017-02-23 18:29:14 +00001727
1728 qemu_mutex_lock_iothread();
1729 qemu_thread_get_self(cpu->thread);
1730
1731 cpu->thread_id = qemu_get_thread_id();
1732 cpu->created = true;
1733 cpu->can_do_io = 1;
1734 current_cpu = cpu;
1735 qemu_cond_signal(&qemu_cpu_cond);
Richard Henderson9c09a252019-03-14 13:06:29 -07001736 qemu_guest_random_seed_thread_part2(cpu->random_seed);
Alex Bennée37257942017-02-23 18:29:14 +00001737
1738 /* process any pending work */
1739 cpu->exit_request = 1;
1740
Cédric Le Goater54961aa2018-04-25 15:18:28 +02001741 do {
Alex Bennée37257942017-02-23 18:29:14 +00001742 if (cpu_can_run(cpu)) {
1743 int r;
Alex Bennéed759c952018-02-27 12:52:48 +03001744 qemu_mutex_unlock_iothread();
Alex Bennée37257942017-02-23 18:29:14 +00001745 r = tcg_cpu_exec(cpu);
Alex Bennéed759c952018-02-27 12:52:48 +03001746 qemu_mutex_lock_iothread();
Alex Bennée37257942017-02-23 18:29:14 +00001747 switch (r) {
1748 case EXCP_DEBUG:
1749 cpu_handle_guest_debug(cpu);
1750 break;
1751 case EXCP_HALTED:
1752 /* during start-up the vCPU is reset and the thread is
1753 * kicked several times. If we don't ensure we go back
1754 * to sleep in the halted state we won't cleanly
1755 * start-up when the vCPU is enabled.
1756 *
1757 * cpu->halted should ensure we sleep in wait_io_event
1758 */
1759 g_assert(cpu->halted);
1760 break;
Pranith Kumar08e73c42017-02-23 18:29:15 +00001761 case EXCP_ATOMIC:
1762 qemu_mutex_unlock_iothread();
1763 cpu_exec_step_atomic(cpu);
1764 qemu_mutex_lock_iothread();
Alex Bennée37257942017-02-23 18:29:14 +00001765 default:
1766 /* Ignore everything else? */
1767 break;
1768 }
1769 }
1770
Alex Bennée37257942017-02-23 18:29:14 +00001771 atomic_mb_set(&cpu->exit_request, 0);
Paolo Bonzinidb08b682018-01-11 13:53:12 +01001772 qemu_wait_io_event(cpu);
Paolo Bonzini9b0605f2018-01-30 11:05:06 -05001773 } while (!cpu->unplug || cpu_can_run(cpu));
Alex Bennée37257942017-02-23 18:29:14 +00001774
Paolo Bonzini9b0605f2018-01-30 11:05:06 -05001775 qemu_tcg_destroy_vcpu(cpu);
1776 cpu->created = false;
1777 qemu_cond_signal(&qemu_cpu_cond);
1778 qemu_mutex_unlock_iothread();
1779 rcu_unregister_thread();
Alex Bennée37257942017-02-23 18:29:14 +00001780 return NULL;
1781}
1782
Andreas Färber2ff09a42012-05-03 00:23:30 +02001783static void qemu_cpu_kick_thread(CPUState *cpu)
Paolo Bonzinicc015e92011-03-12 17:44:08 +01001784{
1785#ifndef _WIN32
1786 int err;
1787
Paolo Bonzinie0c38212015-08-26 00:19:19 +02001788 if (cpu->thread_kicked) {
1789 return;
Paolo Bonzini9102ded2015-08-18 06:52:09 -07001790 }
Paolo Bonzinie0c38212015-08-26 00:19:19 +02001791 cpu->thread_kicked = true;
Andreas Färber814e6122012-05-02 17:00:37 +02001792 err = pthread_kill(cpu->thread->thread, SIG_IPI);
Laurent Vivierd455ebc2019-01-02 15:16:03 +01001793 if (err && err != ESRCH) {
Paolo Bonzinicc015e92011-03-12 17:44:08 +01001794 fprintf(stderr, "qemu:%s: %s", __func__, strerror(err));
1795 exit(1);
1796 }
1797#else /* _WIN32 */
Vincent Palatinb0cb0a62017-01-10 11:59:57 +01001798 if (!qemu_cpu_is_self(cpu)) {
Justin Terry (VM)19306802018-01-22 13:07:49 -08001799 if (whpx_enabled()) {
1800 whpx_vcpu_kick(cpu);
1801 } else if (!QueueUserAPC(dummy_apc_func, cpu->hThread, 0)) {
Vincent Palatinb0cb0a62017-01-10 11:59:57 +01001802 fprintf(stderr, "%s: QueueUserAPC failed with error %lu\n",
1803 __func__, GetLastError());
1804 exit(1);
1805 }
1806 }
Paolo Bonzinicc015e92011-03-12 17:44:08 +01001807#endif
1808}
1809
Andreas Färberc08d7422012-05-03 04:34:15 +02001810void qemu_cpu_kick(CPUState *cpu)
Blue Swirl296af7c2010-03-29 19:23:50 +00001811{
Andreas Färberf5c121b2012-05-03 01:22:49 +02001812 qemu_cond_broadcast(cpu->halt_cond);
Paolo Bonzinie0c38212015-08-26 00:19:19 +02001813 if (tcg_enabled()) {
Alex Bennée791158d2017-02-23 18:29:10 +00001814 cpu_exit(cpu);
Alex Bennée37257942017-02-23 18:29:14 +00001815 /* NOP unless doing single-thread RR */
Alex Bennée791158d2017-02-23 18:29:10 +00001816 qemu_cpu_kick_rr_cpu();
Paolo Bonzinie0c38212015-08-26 00:19:19 +02001817 } else {
Vincent Palatinb0cb0a62017-01-10 11:59:57 +01001818 if (hax_enabled()) {
1819 /*
1820 * FIXME: race condition with the exit_request check in
1821 * hax_vcpu_hax_exec
1822 */
1823 cpu->exit_request = 1;
1824 }
Paolo Bonzinie0c38212015-08-26 00:19:19 +02001825 qemu_cpu_kick_thread(cpu);
1826 }
Blue Swirl296af7c2010-03-29 19:23:50 +00001827}
1828
Jan Kiszka46d62fa2011-02-01 22:15:59 +01001829void qemu_cpu_kick_self(void)
1830{
Andreas Färber4917cf42013-05-27 05:17:50 +02001831 assert(current_cpu);
Paolo Bonzini9102ded2015-08-18 06:52:09 -07001832 qemu_cpu_kick_thread(current_cpu);
Blue Swirl296af7c2010-03-29 19:23:50 +00001833}
1834
Andreas Färber60e82572012-05-02 22:23:49 +02001835bool qemu_cpu_is_self(CPUState *cpu)
Blue Swirl296af7c2010-03-29 19:23:50 +00001836{
Andreas Färber814e6122012-05-02 17:00:37 +02001837 return qemu_thread_is_self(cpu->thread);
Blue Swirl296af7c2010-03-29 19:23:50 +00001838}
1839
Paolo Bonzini79e2b9a2015-01-21 12:09:14 +01001840bool qemu_in_vcpu_thread(void)
Juan Quintelaaa723c22012-09-18 16:30:11 +02001841{
Andreas Färber4917cf42013-05-27 05:17:50 +02001842 return current_cpu && qemu_cpu_is_self(current_cpu);
Juan Quintelaaa723c22012-09-18 16:30:11 +02001843}
1844
Paolo Bonziniafbe7052015-06-18 18:47:19 +02001845static __thread bool iothread_locked = false;
1846
1847bool qemu_mutex_iothread_locked(void)
1848{
1849 return iothread_locked;
1850}
1851
Emilio G. Cotacb764d02017-10-28 02:16:41 -04001852/*
1853 * The BQL is taken from so many places that it is worth profiling the
1854 * callers directly, instead of funneling them all through a single function.
1855 */
1856void qemu_mutex_lock_iothread_impl(const char *file, int line)
Blue Swirl296af7c2010-03-29 19:23:50 +00001857{
Emilio G. Cotacb764d02017-10-28 02:16:41 -04001858 QemuMutexLockFunc bql_lock = atomic_read(&qemu_bql_mutex_lock_func);
1859
Jan Kiszka8d04fb52017-02-23 18:29:11 +00001860 g_assert(!qemu_mutex_iothread_locked());
Emilio G. Cotacb764d02017-10-28 02:16:41 -04001861 bql_lock(&qemu_global_mutex, file, line);
Paolo Bonziniafbe7052015-06-18 18:47:19 +02001862 iothread_locked = true;
Blue Swirl296af7c2010-03-29 19:23:50 +00001863}
1864
1865void qemu_mutex_unlock_iothread(void)
1866{
Jan Kiszka8d04fb52017-02-23 18:29:11 +00001867 g_assert(qemu_mutex_iothread_locked());
Paolo Bonziniafbe7052015-06-18 18:47:19 +02001868 iothread_locked = false;
Blue Swirl296af7c2010-03-29 19:23:50 +00001869 qemu_mutex_unlock(&qemu_global_mutex);
1870}
1871
Alex Bennéee8faee02016-10-27 16:09:58 +01001872static bool all_vcpus_paused(void)
Blue Swirl296af7c2010-03-29 19:23:50 +00001873{
Andreas Färberbdc44642013-06-24 23:50:24 +02001874 CPUState *cpu;
Blue Swirl296af7c2010-03-29 19:23:50 +00001875
Andreas Färberbdc44642013-06-24 23:50:24 +02001876 CPU_FOREACH(cpu) {
Andreas Färber182735e2013-05-29 22:29:20 +02001877 if (!cpu->stopped) {
Alex Bennéee8faee02016-10-27 16:09:58 +01001878 return false;
Jan Kiszka0ab07c62011-02-07 12:19:14 +01001879 }
Blue Swirl296af7c2010-03-29 19:23:50 +00001880 }
1881
Alex Bennéee8faee02016-10-27 16:09:58 +01001882 return true;
Blue Swirl296af7c2010-03-29 19:23:50 +00001883}
1884
1885void pause_all_vcpus(void)
1886{
Andreas Färberbdc44642013-06-24 23:50:24 +02001887 CPUState *cpu;
Blue Swirl296af7c2010-03-29 19:23:50 +00001888
Alex Bligh40daca52013-08-21 16:03:02 +01001889 qemu_clock_enable(QEMU_CLOCK_VIRTUAL, false);
Andreas Färberbdc44642013-06-24 23:50:24 +02001890 CPU_FOREACH(cpu) {
David Hildenbrandebd05fe2017-11-29 20:12:15 +01001891 if (qemu_cpu_is_self(cpu)) {
1892 qemu_cpu_stop(cpu, true);
1893 } else {
1894 cpu->stop = true;
1895 qemu_cpu_kick(cpu);
1896 }
Jan Kiszkad798e972012-02-17 18:31:16 +01001897 }
1898
Alex Bennéed759c952018-02-27 12:52:48 +03001899 /* We need to drop the replay_lock so any vCPU threads woken up
1900 * can finish their replay tasks
1901 */
1902 replay_mutex_unlock();
1903
Blue Swirl296af7c2010-03-29 19:23:50 +00001904 while (!all_vcpus_paused()) {
Paolo Bonzinibe7d6c52011-03-12 17:44:02 +01001905 qemu_cond_wait(&qemu_pause_cond, &qemu_global_mutex);
Andreas Färberbdc44642013-06-24 23:50:24 +02001906 CPU_FOREACH(cpu) {
Andreas Färber182735e2013-05-29 22:29:20 +02001907 qemu_cpu_kick(cpu);
Blue Swirl296af7c2010-03-29 19:23:50 +00001908 }
1909 }
Alex Bennéed759c952018-02-27 12:52:48 +03001910
1911 qemu_mutex_unlock_iothread();
1912 replay_mutex_lock();
1913 qemu_mutex_lock_iothread();
Blue Swirl296af7c2010-03-29 19:23:50 +00001914}
1915
Igor Mammedov29936832013-04-23 10:29:37 +02001916void cpu_resume(CPUState *cpu)
1917{
1918 cpu->stop = false;
1919 cpu->stopped = false;
1920 qemu_cpu_kick(cpu);
1921}
1922
Blue Swirl296af7c2010-03-29 19:23:50 +00001923void resume_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, true);
Andreas Färberbdc44642013-06-24 23:50:24 +02001928 CPU_FOREACH(cpu) {
Andreas Färber182735e2013-05-29 22:29:20 +02001929 cpu_resume(cpu);
Blue Swirl296af7c2010-03-29 19:23:50 +00001930 }
1931}
1932
Paolo Bonzinidbadee42018-01-30 16:40:12 +01001933void cpu_remove_sync(CPUState *cpu)
Gu Zheng4c055ab2016-05-12 09:18:13 +05301934{
1935 cpu->stop = true;
1936 cpu->unplug = true;
1937 qemu_cpu_kick(cpu);
Paolo Bonzinidbadee42018-01-30 16:40:12 +01001938 qemu_mutex_unlock_iothread();
1939 qemu_thread_join(cpu->thread);
1940 qemu_mutex_lock_iothread();
Bharata B Rao2c579042016-05-12 09:18:14 +05301941}
1942
Dr. David Alan Gilbert49001162014-01-30 10:20:32 +00001943/* For temporary buffers for forming a name */
1944#define VCPU_THREAD_NAME_SIZE 16
1945
Andreas Färbere5ab30a2012-05-03 01:50:44 +02001946static void qemu_tcg_init_vcpu(CPUState *cpu)
Blue Swirl296af7c2010-03-29 19:23:50 +00001947{
Dr. David Alan Gilbert49001162014-01-30 10:20:32 +00001948 char thread_name[VCPU_THREAD_NAME_SIZE];
Alex Bennée37257942017-02-23 18:29:14 +00001949 static QemuCond *single_tcg_halt_cond;
1950 static QemuThread *single_tcg_cpu_thread;
Emilio G. Cotae8feb962017-07-07 19:24:20 -04001951 static int tcg_region_inited;
1952
Emilio G. Cotaf28d0df2018-06-22 13:45:31 -04001953 assert(tcg_enabled());
Emilio G. Cotae8feb962017-07-07 19:24:20 -04001954 /*
1955 * Initialize TCG regions--once. Now is a good time, because:
1956 * (1) TCG's init context, prologue and target globals have been set up.
1957 * (2) qemu_tcg_mttcg_enabled() works now (TCG init code runs before the
1958 * -accel flag is processed, so the check doesn't work then).
1959 */
1960 if (!tcg_region_inited) {
1961 tcg_region_inited = 1;
1962 tcg_region_init();
1963 }
Dr. David Alan Gilbert49001162014-01-30 10:20:32 +00001964
Alex Bennée37257942017-02-23 18:29:14 +00001965 if (qemu_tcg_mttcg_enabled() || !single_tcg_cpu_thread) {
Andreas Färber814e6122012-05-02 17:00:37 +02001966 cpu->thread = g_malloc0(sizeof(QemuThread));
Andreas Färberf5c121b2012-05-03 01:22:49 +02001967 cpu->halt_cond = g_malloc0(sizeof(QemuCond));
1968 qemu_cond_init(cpu->halt_cond);
Alex Bennée37257942017-02-23 18:29:14 +00001969
1970 if (qemu_tcg_mttcg_enabled()) {
1971 /* create a thread per vCPU with TCG (MTTCG) */
1972 parallel_cpus = true;
1973 snprintf(thread_name, VCPU_THREAD_NAME_SIZE, "CPU %d/TCG",
Dr. David Alan Gilbert49001162014-01-30 10:20:32 +00001974 cpu->cpu_index);
Alex Bennée37257942017-02-23 18:29:14 +00001975
1976 qemu_thread_create(cpu->thread, thread_name, qemu_tcg_cpu_thread_fn,
1977 cpu, QEMU_THREAD_JOINABLE);
1978
1979 } else {
1980 /* share a single thread for all cpus with TCG */
1981 snprintf(thread_name, VCPU_THREAD_NAME_SIZE, "ALL CPUs/TCG");
1982 qemu_thread_create(cpu->thread, thread_name,
1983 qemu_tcg_rr_cpu_thread_fn,
1984 cpu, QEMU_THREAD_JOINABLE);
1985
1986 single_tcg_halt_cond = cpu->halt_cond;
1987 single_tcg_cpu_thread = cpu->thread;
1988 }
Paolo Bonzini1ecf47b2011-12-13 13:43:52 +01001989#ifdef _WIN32
Andreas Färber814e6122012-05-02 17:00:37 +02001990 cpu->hThread = qemu_thread_get_handle(cpu->thread);
Paolo Bonzini1ecf47b2011-12-13 13:43:52 +01001991#endif
Blue Swirl296af7c2010-03-29 19:23:50 +00001992 } else {
Alex Bennée37257942017-02-23 18:29:14 +00001993 /* For non-MTTCG cases we share the thread */
1994 cpu->thread = single_tcg_cpu_thread;
1995 cpu->halt_cond = single_tcg_halt_cond;
David Hildenbranda3421732018-02-09 20:52:37 +01001996 cpu->thread_id = first_cpu->thread_id;
1997 cpu->can_do_io = 1;
1998 cpu->created = true;
Blue Swirl296af7c2010-03-29 19:23:50 +00001999 }
2000}
2001
Vincent Palatinb0cb0a62017-01-10 11:59:57 +01002002static void qemu_hax_start_vcpu(CPUState *cpu)
2003{
2004 char thread_name[VCPU_THREAD_NAME_SIZE];
2005
2006 cpu->thread = g_malloc0(sizeof(QemuThread));
2007 cpu->halt_cond = g_malloc0(sizeof(QemuCond));
2008 qemu_cond_init(cpu->halt_cond);
2009
2010 snprintf(thread_name, VCPU_THREAD_NAME_SIZE, "CPU %d/HAX",
2011 cpu->cpu_index);
2012 qemu_thread_create(cpu->thread, thread_name, qemu_hax_cpu_thread_fn,
2013 cpu, QEMU_THREAD_JOINABLE);
2014#ifdef _WIN32
2015 cpu->hThread = qemu_thread_get_handle(cpu->thread);
2016#endif
Vincent Palatinb0cb0a62017-01-10 11:59:57 +01002017}
2018
Andreas Färber48a106b2013-05-27 02:20:39 +02002019static void qemu_kvm_start_vcpu(CPUState *cpu)
Blue Swirl296af7c2010-03-29 19:23:50 +00002020{
Dr. David Alan Gilbert49001162014-01-30 10:20:32 +00002021 char thread_name[VCPU_THREAD_NAME_SIZE];
2022
Andreas Färber814e6122012-05-02 17:00:37 +02002023 cpu->thread = g_malloc0(sizeof(QemuThread));
Andreas Färberf5c121b2012-05-03 01:22:49 +02002024 cpu->halt_cond = g_malloc0(sizeof(QemuCond));
2025 qemu_cond_init(cpu->halt_cond);
Dr. David Alan Gilbert49001162014-01-30 10:20:32 +00002026 snprintf(thread_name, VCPU_THREAD_NAME_SIZE, "CPU %d/KVM",
2027 cpu->cpu_index);
2028 qemu_thread_create(cpu->thread, thread_name, qemu_kvm_cpu_thread_fn,
2029 cpu, QEMU_THREAD_JOINABLE);
Blue Swirl296af7c2010-03-29 19:23:50 +00002030}
2031
Sergio Andres Gomez Del Realc97d6d22017-09-13 04:05:09 -05002032static void qemu_hvf_start_vcpu(CPUState *cpu)
2033{
2034 char thread_name[VCPU_THREAD_NAME_SIZE];
2035
2036 /* HVF currently does not support TCG, and only runs in
2037 * unrestricted-guest mode. */
2038 assert(hvf_enabled());
2039
2040 cpu->thread = g_malloc0(sizeof(QemuThread));
2041 cpu->halt_cond = g_malloc0(sizeof(QemuCond));
2042 qemu_cond_init(cpu->halt_cond);
2043
2044 snprintf(thread_name, VCPU_THREAD_NAME_SIZE, "CPU %d/HVF",
2045 cpu->cpu_index);
2046 qemu_thread_create(cpu->thread, thread_name, qemu_hvf_cpu_thread_fn,
2047 cpu, QEMU_THREAD_JOINABLE);
Sergio Andres Gomez Del Realc97d6d22017-09-13 04:05:09 -05002048}
2049
Justin Terry (VM)19306802018-01-22 13:07:49 -08002050static void qemu_whpx_start_vcpu(CPUState *cpu)
2051{
2052 char thread_name[VCPU_THREAD_NAME_SIZE];
2053
2054 cpu->thread = g_malloc0(sizeof(QemuThread));
2055 cpu->halt_cond = g_malloc0(sizeof(QemuCond));
2056 qemu_cond_init(cpu->halt_cond);
2057 snprintf(thread_name, VCPU_THREAD_NAME_SIZE, "CPU %d/WHPX",
2058 cpu->cpu_index);
2059 qemu_thread_create(cpu->thread, thread_name, qemu_whpx_cpu_thread_fn,
2060 cpu, QEMU_THREAD_JOINABLE);
2061#ifdef _WIN32
2062 cpu->hThread = qemu_thread_get_handle(cpu->thread);
2063#endif
Justin Terry (VM)19306802018-01-22 13:07:49 -08002064}
2065
Andreas Färber10a90212013-05-27 02:24:35 +02002066static void qemu_dummy_start_vcpu(CPUState *cpu)
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +02002067{
Dr. David Alan Gilbert49001162014-01-30 10:20:32 +00002068 char thread_name[VCPU_THREAD_NAME_SIZE];
2069
Andreas Färber814e6122012-05-02 17:00:37 +02002070 cpu->thread = g_malloc0(sizeof(QemuThread));
Andreas Färberf5c121b2012-05-03 01:22:49 +02002071 cpu->halt_cond = g_malloc0(sizeof(QemuCond));
2072 qemu_cond_init(cpu->halt_cond);
Dr. David Alan Gilbert49001162014-01-30 10:20:32 +00002073 snprintf(thread_name, VCPU_THREAD_NAME_SIZE, "CPU %d/DUMMY",
2074 cpu->cpu_index);
2075 qemu_thread_create(cpu->thread, thread_name, qemu_dummy_cpu_thread_fn, cpu,
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +02002076 QEMU_THREAD_JOINABLE);
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +02002077}
2078
Andreas Färberc643bed2013-05-27 03:23:24 +02002079void qemu_init_vcpu(CPUState *cpu)
Blue Swirl296af7c2010-03-29 19:23:50 +00002080{
Like Xu5cc87672019-05-19 04:54:21 +08002081 MachineState *ms = MACHINE(qdev_get_machine());
2082
2083 cpu->nr_cores = ms->smp.cores;
2084 cpu->nr_threads = ms->smp.threads;
Andreas Färberf324e762012-05-02 23:26:21 +02002085 cpu->stopped = true;
Richard Henderson9c09a252019-03-14 13:06:29 -07002086 cpu->random_seed = qemu_guest_random_seed_thread_part1();
Peter Maydell56943e82016-01-21 14:15:04 +00002087
2088 if (!cpu->as) {
2089 /* If the target cpu hasn't set up any address spaces itself,
2090 * give it the default one.
2091 */
Peter Maydell12ebc9a2016-01-21 14:15:04 +00002092 cpu->num_ases = 1;
Peter Xu80ceb072017-11-23 17:23:32 +08002093 cpu_address_space_init(cpu, 0, "cpu-memory", cpu->memory);
Peter Maydell56943e82016-01-21 14:15:04 +00002094 }
2095
Jan Kiszka0ab07c62011-02-07 12:19:14 +01002096 if (kvm_enabled()) {
Andreas Färber48a106b2013-05-27 02:20:39 +02002097 qemu_kvm_start_vcpu(cpu);
Vincent Palatinb0cb0a62017-01-10 11:59:57 +01002098 } else if (hax_enabled()) {
2099 qemu_hax_start_vcpu(cpu);
Sergio Andres Gomez Del Realc97d6d22017-09-13 04:05:09 -05002100 } else if (hvf_enabled()) {
2101 qemu_hvf_start_vcpu(cpu);
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +02002102 } else if (tcg_enabled()) {
Andreas Färbere5ab30a2012-05-03 01:50:44 +02002103 qemu_tcg_init_vcpu(cpu);
Justin Terry (VM)19306802018-01-22 13:07:49 -08002104 } else if (whpx_enabled()) {
2105 qemu_whpx_start_vcpu(cpu);
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +02002106 } else {
Andreas Färber10a90212013-05-27 02:24:35 +02002107 qemu_dummy_start_vcpu(cpu);
Jan Kiszka0ab07c62011-02-07 12:19:14 +01002108 }
David Hildenbrand81e96312018-02-09 20:52:38 +01002109
2110 while (!cpu->created) {
2111 qemu_cond_wait(&qemu_cpu_cond, &qemu_global_mutex);
2112 }
Blue Swirl296af7c2010-03-29 19:23:50 +00002113}
2114
Jan Kiszkab4a3d962011-02-01 22:15:43 +01002115void cpu_stop_current(void)
Blue Swirl296af7c2010-03-29 19:23:50 +00002116{
Andreas Färber4917cf42013-05-27 05:17:50 +02002117 if (current_cpu) {
Peter Maydell0ec7e672019-01-07 15:23:47 +00002118 current_cpu->stop = true;
2119 cpu_exit(current_cpu);
Jan Kiszkab4a3d962011-02-01 22:15:43 +01002120 }
Blue Swirl296af7c2010-03-29 19:23:50 +00002121}
2122
Kevin Wolf56983462013-07-05 13:49:54 +02002123int vm_stop(RunState state)
Blue Swirl296af7c2010-03-29 19:23:50 +00002124{
Juan Quintelaaa723c22012-09-18 16:30:11 +02002125 if (qemu_in_vcpu_thread()) {
Paolo Bonzini74892d22014-06-05 14:53:58 +02002126 qemu_system_vmstop_request_prepare();
Luiz Capitulino1dfb4dd2011-07-29 14:26:33 -03002127 qemu_system_vmstop_request(state);
Blue Swirl296af7c2010-03-29 19:23:50 +00002128 /*
2129 * FIXME: should not return to device code in case
2130 * vm_stop() has been requested.
2131 */
Jan Kiszkab4a3d962011-02-01 22:15:43 +01002132 cpu_stop_current();
Kevin Wolf56983462013-07-05 13:49:54 +02002133 return 0;
Blue Swirl296af7c2010-03-29 19:23:50 +00002134 }
Kevin Wolf56983462013-07-05 13:49:54 +02002135
Stefan Hajnoczi4486e892018-03-07 14:42:05 +00002136 return do_vm_stop(state, true);
Blue Swirl296af7c2010-03-29 19:23:50 +00002137}
2138
Claudio Imbrenda2d76e822017-02-14 18:07:47 +01002139/**
2140 * Prepare for (re)starting the VM.
2141 * Returns -1 if the vCPUs are not to be restarted (e.g. if they are already
2142 * running or in case of an error condition), 0 otherwise.
2143 */
2144int vm_prepare_start(void)
2145{
2146 RunState requested;
Claudio Imbrenda2d76e822017-02-14 18:07:47 +01002147
2148 qemu_vmstop_requested(&requested);
2149 if (runstate_is_running() && requested == RUN_STATE__MAX) {
2150 return -1;
2151 }
2152
2153 /* Ensure that a STOP/RESUME pair of events is emitted if a
2154 * vmstop request was pending. The BLOCK_IO_ERROR event, for
2155 * example, according to documentation is always followed by
2156 * the STOP event.
2157 */
2158 if (runstate_is_running()) {
Peter Xu3ab72382018-08-15 21:37:37 +08002159 qapi_event_send_stop();
2160 qapi_event_send_resume();
Markus Armbrusterf0561582018-04-23 10:45:18 +02002161 return -1;
Claudio Imbrenda2d76e822017-02-14 18:07:47 +01002162 }
2163
2164 /* We are sending this now, but the CPUs will be resumed shortly later */
Peter Xu3ab72382018-08-15 21:37:37 +08002165 qapi_event_send_resume();
Markus Armbrusterf0561582018-04-23 10:45:18 +02002166
2167 replay_enable_events();
2168 cpu_enable_ticks();
2169 runstate_set(RUN_STATE_RUNNING);
2170 vm_state_notify(1, RUN_STATE_RUNNING);
2171 return 0;
Claudio Imbrenda2d76e822017-02-14 18:07:47 +01002172}
2173
2174void vm_start(void)
2175{
2176 if (!vm_prepare_start()) {
2177 resume_all_vcpus();
2178 }
2179}
2180
Luiz Capitulino8a9236f2011-10-14 11:18:09 -03002181/* does a state transition even if the VM is already stopped,
2182 current state is forgotten forever */
Kevin Wolf56983462013-07-05 13:49:54 +02002183int vm_stop_force_state(RunState state)
Luiz Capitulino8a9236f2011-10-14 11:18:09 -03002184{
2185 if (runstate_is_running()) {
Kevin Wolf56983462013-07-05 13:49:54 +02002186 return vm_stop(state);
Luiz Capitulino8a9236f2011-10-14 11:18:09 -03002187 } else {
2188 runstate_set(state);
Wen Congyangb2780d32015-11-20 17:34:38 +08002189
2190 bdrv_drain_all();
Kevin Wolf594a45c2013-07-18 14:52:19 +02002191 /* Make sure to return an error if the flush in a previous vm_stop()
2192 * failed. */
John Snow22af08e2016-09-22 21:45:51 -04002193 return bdrv_flush_all();
Luiz Capitulino8a9236f2011-10-14 11:18:09 -03002194 }
2195}
2196
Markus Armbruster04424282019-04-17 21:17:57 +02002197void list_cpus(const char *optarg)
Blue Swirl262353c2010-05-04 19:55:35 +00002198{
2199 /* XXX: implement xxx_cpu_list for targets that still miss it */
Peter Maydelle916cbf2012-09-05 17:41:08 -03002200#if defined(cpu_list)
Markus Armbruster04424282019-04-17 21:17:57 +02002201 cpu_list();
Blue Swirl262353c2010-05-04 19:55:35 +00002202#endif
2203}
Luiz Capitulinode0b36b2011-09-21 16:38:35 -03002204
Luiz Capitulino0cfd6a92011-11-22 16:32:37 -02002205void qmp_memsave(int64_t addr, int64_t size, const char *filename,
2206 bool has_cpu, int64_t cpu_index, Error **errp)
2207{
2208 FILE *f;
2209 uint32_t l;
Andreas Färber55e5c282012-12-17 06:18:02 +01002210 CPUState *cpu;
Luiz Capitulino0cfd6a92011-11-22 16:32:37 -02002211 uint8_t buf[1024];
Borislav Petkov0dc9daf2015-02-08 13:14:38 +01002212 int64_t orig_addr = addr, orig_size = size;
Luiz Capitulino0cfd6a92011-11-22 16:32:37 -02002213
2214 if (!has_cpu) {
2215 cpu_index = 0;
2216 }
2217
Andreas Färber151d1322013-02-15 15:41:49 +01002218 cpu = qemu_get_cpu(cpu_index);
2219 if (cpu == NULL) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01002220 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "cpu-index",
2221 "a CPU number");
Luiz Capitulino0cfd6a92011-11-22 16:32:37 -02002222 return;
2223 }
2224
2225 f = fopen(filename, "wb");
2226 if (!f) {
Luiz Capitulino618da852013-06-07 14:35:06 -04002227 error_setg_file_open(errp, errno, filename);
Luiz Capitulino0cfd6a92011-11-22 16:32:37 -02002228 return;
2229 }
2230
2231 while (size != 0) {
2232 l = sizeof(buf);
2233 if (l > size)
2234 l = size;
Aneesh Kumar K.V2f4d0f52013-10-01 21:49:30 +05302235 if (cpu_memory_rw_debug(cpu, addr, buf, l, 0) != 0) {
Borislav Petkov0dc9daf2015-02-08 13:14:38 +01002236 error_setg(errp, "Invalid addr 0x%016" PRIx64 "/size %" PRId64
2237 " specified", orig_addr, orig_size);
Aneesh Kumar K.V2f4d0f52013-10-01 21:49:30 +05302238 goto exit;
2239 }
Luiz Capitulino0cfd6a92011-11-22 16:32:37 -02002240 if (fwrite(buf, 1, l, f) != l) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01002241 error_setg(errp, QERR_IO_ERROR);
Luiz Capitulino0cfd6a92011-11-22 16:32:37 -02002242 goto exit;
2243 }
2244 addr += l;
2245 size -= l;
2246 }
2247
2248exit:
2249 fclose(f);
2250}
Luiz Capitulino6d3962b2011-11-22 17:26:46 -02002251
2252void qmp_pmemsave(int64_t addr, int64_t size, const char *filename,
2253 Error **errp)
2254{
2255 FILE *f;
2256 uint32_t l;
2257 uint8_t buf[1024];
2258
2259 f = fopen(filename, "wb");
2260 if (!f) {
Luiz Capitulino618da852013-06-07 14:35:06 -04002261 error_setg_file_open(errp, errno, filename);
Luiz Capitulino6d3962b2011-11-22 17:26:46 -02002262 return;
2263 }
2264
2265 while (size != 0) {
2266 l = sizeof(buf);
2267 if (l > size)
2268 l = size;
Stefan Weileb6282f2014-04-07 20:28:23 +02002269 cpu_physical_memory_read(addr, buf, l);
Luiz Capitulino6d3962b2011-11-22 17:26:46 -02002270 if (fwrite(buf, 1, l, f) != l) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01002271 error_setg(errp, QERR_IO_ERROR);
Luiz Capitulino6d3962b2011-11-22 17:26:46 -02002272 goto exit;
2273 }
2274 addr += l;
2275 size -= l;
2276 }
2277
2278exit:
2279 fclose(f);
2280}
Luiz Capitulinoab49ab52011-11-23 12:55:53 -02002281
2282void qmp_inject_nmi(Error **errp)
2283{
Alexey Kardashevskiy9cb805f2014-08-20 22:16:33 +10002284 nmi_monitor_handle(monitor_get_cpu_index(), errp);
Luiz Capitulinoab49ab52011-11-23 12:55:53 -02002285}
Sebastian Tanase27498be2014-07-25 11:56:33 +02002286
Markus Armbruster76c86612019-04-17 21:17:53 +02002287void dump_drift_info(void)
Sebastian Tanase27498be2014-07-25 11:56:33 +02002288{
2289 if (!use_icount) {
2290 return;
2291 }
2292
Markus Armbruster76c86612019-04-17 21:17:53 +02002293 qemu_printf("Host - Guest clock %"PRIi64" ms\n",
Sebastian Tanase27498be2014-07-25 11:56:33 +02002294 (cpu_get_clock() - cpu_get_icount())/SCALE_MS);
2295 if (icount_align_option) {
Markus Armbruster76c86612019-04-17 21:17:53 +02002296 qemu_printf("Max guest delay %"PRIi64" ms\n",
2297 -max_delay / SCALE_MS);
2298 qemu_printf("Max guest advance %"PRIi64" ms\n",
2299 max_advance / SCALE_MS);
Sebastian Tanase27498be2014-07-25 11:56:33 +02002300 } else {
Markus Armbruster76c86612019-04-17 21:17:53 +02002301 qemu_printf("Max guest delay NA\n");
2302 qemu_printf("Max guest advance NA\n");
Sebastian Tanase27498be2014-07-25 11:56:33 +02002303 }
2304}