blob: 85cd451a86a9c38718f5ca85581f1f36dfcd1f29 [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"
Markus Armbruster54d31232019-08-12 07:23:59 +020058#include "sysemu/runstate.h"
Like Xu5cc87672019-05-19 04:54:21 +080059#include "hw/boards.h"
Markus Armbruster650d1032019-08-12 07:23:48 +020060#include "hw/hw.h"
Jan Kiszka0ff0fc12011-06-23 10:15:55 +020061
Jan Kiszka6d9cb732011-02-01 22:15:58 +010062#ifdef CONFIG_LINUX
63
64#include <sys/prctl.h>
65
Marcelo Tosattic0532a72010-10-11 15:31:21 -030066#ifndef PR_MCE_KILL
67#define PR_MCE_KILL 33
68#endif
69
Jan Kiszka6d9cb732011-02-01 22:15:58 +010070#ifndef PR_MCE_KILL_SET
71#define PR_MCE_KILL_SET 1
72#endif
73
74#ifndef PR_MCE_KILL_EARLY
75#define PR_MCE_KILL_EARLY 1
76#endif
77
78#endif /* CONFIG_LINUX */
79
Sebastian Tanase27498be2014-07-25 11:56:33 +020080int64_t max_delay;
81int64_t max_advance;
Blue Swirl296af7c2010-03-29 19:23:50 +000082
Jason J. Herne2adcc852015-09-08 13:12:33 -040083/* vcpu throttling controls */
84static QEMUTimer *throttle_timer;
85static unsigned int throttle_percentage;
86
87#define CPU_THROTTLE_PCT_MIN 1
88#define CPU_THROTTLE_PCT_MAX 99
89#define CPU_THROTTLE_TIMESLICE_NS 10000000
90
Tiejun Chen321bc0b2013-08-02 09:43:09 +080091bool cpu_is_stopped(CPUState *cpu)
92{
93 return cpu->stopped || !runstate_is_running();
94}
95
Andreas Färbera98ae1d2013-05-26 23:21:08 +020096static bool cpu_thread_is_idle(CPUState *cpu)
Peter Maydellac873f12012-07-19 16:52:27 +010097{
Andreas Färberc64ca812012-05-03 02:11:45 +020098 if (cpu->stop || cpu->queued_work_first) {
Peter Maydellac873f12012-07-19 16:52:27 +010099 return false;
100 }
Tiejun Chen321bc0b2013-08-02 09:43:09 +0800101 if (cpu_is_stopped(cpu)) {
Peter Maydellac873f12012-07-19 16:52:27 +0100102 return true;
103 }
Andreas Färber8c2e1b02013-08-25 18:53:55 +0200104 if (!cpu->halted || cpu_has_work(cpu) ||
Alexander Graf215e79c2013-04-24 22:24:12 +0200105 kvm_halt_in_kernel()) {
Peter Maydellac873f12012-07-19 16:52:27 +0100106 return false;
107 }
108 return true;
109}
110
111static bool all_cpu_threads_idle(void)
112{
Andreas Färber182735e2013-05-29 22:29:20 +0200113 CPUState *cpu;
Peter Maydellac873f12012-07-19 16:52:27 +0100114
Andreas Färberbdc44642013-06-24 23:50:24 +0200115 CPU_FOREACH(cpu) {
Andreas Färber182735e2013-05-29 22:29:20 +0200116 if (!cpu_thread_is_idle(cpu)) {
Peter Maydellac873f12012-07-19 16:52:27 +0100117 return false;
118 }
119 }
120 return true;
121}
122
Blue Swirl296af7c2010-03-29 19:23:50 +0000123/***********************************************************/
Paolo Bonzini946fb272011-09-12 13:57:37 +0200124/* guest cycle counter */
125
Paolo Bonzinia3270e12013-10-07 17:18:15 +0200126/* Protected by TimersState seqlock */
127
Victor CLEMENT5045e9d92015-05-29 17:14:04 +0200128static bool icount_sleep = true;
Paolo Bonzini946fb272011-09-12 13:57:37 +0200129/* Arbitrarily pick 1MIPS as the minimum allowable speed. */
130#define MAX_ICOUNT_SHIFT 10
Paolo Bonzinia3270e12013-10-07 17:18:15 +0200131
Paolo Bonzini946fb272011-09-12 13:57:37 +0200132typedef struct TimersState {
Liu Ping Fancb365642013-09-25 14:20:58 +0800133 /* Protected by BQL. */
Paolo Bonzini946fb272011-09-12 13:57:37 +0200134 int64_t cpu_ticks_prev;
135 int64_t cpu_ticks_offset;
Liu Ping Fancb365642013-09-25 14:20:58 +0800136
Paolo Bonzini94377112018-08-14 09:57:16 +0200137 /* Protect fields that can be respectively read outside the
138 * BQL, and written from multiple threads.
Liu Ping Fancb365642013-09-25 14:20:58 +0800139 */
140 QemuSeqLock vm_clock_seqlock;
Paolo Bonzini94377112018-08-14 09:57:16 +0200141 QemuSpin vm_clock_lock;
142
143 int16_t cpu_ticks_enabled;
KONRAD Fredericc96778b2014-08-01 01:37:09 +0200144
Paolo Bonzinic1ff0732018-08-14 09:31:58 +0200145 /* Conversion factor from emulated instructions to virtual clock ticks. */
Paolo Bonzini94377112018-08-14 09:57:16 +0200146 int16_t icount_time_shift;
147
KONRAD Fredericc96778b2014-08-01 01:37:09 +0200148 /* Compensate for varying guest execution speed. */
149 int64_t qemu_icount_bias;
Paolo Bonzini94377112018-08-14 09:57:16 +0200150
151 int64_t vm_clock_warp_start;
152 int64_t cpu_clock_offset;
153
KONRAD Fredericc96778b2014-08-01 01:37:09 +0200154 /* Only written by TCG thread */
155 int64_t qemu_icount;
Paolo Bonzini94377112018-08-14 09:57:16 +0200156
Pavel Dovgalyukb39e3f32018-01-11 11:26:10 +0300157 /* for adjusting icount */
Pavel Dovgalyukb39e3f32018-01-11 11:26:10 +0300158 QEMUTimer *icount_rt_timer;
159 QEMUTimer *icount_vm_timer;
160 QEMUTimer *icount_warp_timer;
Paolo Bonzini946fb272011-09-12 13:57:37 +0200161} TimersState;
162
Liu Ping Fand9cd4002013-07-21 08:43:00 +0000163static TimersState timers_state;
KONRAD Frederic8d4e9142017-02-23 18:29:08 +0000164bool mttcg_enabled;
165
166/*
167 * We default to false if we know other options have been enabled
168 * which are currently incompatible with MTTCG. Otherwise when each
169 * guest (target) has been updated to support:
170 * - atomic instructions
171 * - memory ordering primitives (barriers)
172 * they can set the appropriate CONFIG flags in ${target}-softmmu.mak
173 *
174 * Once a guest architecture has been converted to the new primitives
175 * there are two remaining limitations to check.
176 *
177 * - The guest can't be oversized (e.g. 64 bit guest on 32 bit host)
178 * - The host must have a stronger memory order than the guest
179 *
180 * It may be possible in future to support strong guests on weak hosts
181 * but that will require tagging all load/stores in a guest with their
182 * implicit memory order requirements which would likely slow things
183 * down a lot.
184 */
185
186static bool check_tcg_memory_orders_compatible(void)
187{
188#if defined(TCG_GUEST_DEFAULT_MO) && defined(TCG_TARGET_DEFAULT_MO)
189 return (TCG_GUEST_DEFAULT_MO & ~TCG_TARGET_DEFAULT_MO) == 0;
190#else
191 return false;
192#endif
193}
194
195static bool default_mttcg_enabled(void)
196{
Alex Bennée83fd9622017-02-27 17:09:01 +0000197 if (use_icount || TCG_OVERSIZED_GUEST) {
KONRAD Frederic8d4e9142017-02-23 18:29:08 +0000198 return false;
199 } else {
200#ifdef TARGET_SUPPORTS_MTTCG
201 return check_tcg_memory_orders_compatible();
202#else
203 return false;
204#endif
205 }
206}
207
208void qemu_tcg_configure(QemuOpts *opts, Error **errp)
209{
210 const char *t = qemu_opt_get(opts, "thread");
211 if (t) {
212 if (strcmp(t, "multi") == 0) {
213 if (TCG_OVERSIZED_GUEST) {
214 error_setg(errp, "No MTTCG when guest word size > hosts");
Alex Bennée83fd9622017-02-27 17:09:01 +0000215 } else if (use_icount) {
216 error_setg(errp, "No MTTCG when icount is enabled");
KONRAD Frederic8d4e9142017-02-23 18:29:08 +0000217 } else {
Nikunj A Dadhania86953502017-04-10 11:36:55 +0530218#ifndef TARGET_SUPPORTS_MTTCG
Markus Armbruster07656912018-10-17 10:26:28 +0200219 warn_report("Guest not yet converted to MTTCG - "
220 "you may get unexpected results");
Alex Bennéec34c7622017-02-28 14:40:17 +0000221#endif
KONRAD Frederic8d4e9142017-02-23 18:29:08 +0000222 if (!check_tcg_memory_orders_compatible()) {
Markus Armbruster07656912018-10-17 10:26:28 +0200223 warn_report("Guest expects a stronger memory ordering "
224 "than the host provides");
Pranith Kumar8cfef892017-03-25 16:19:23 -0400225 error_printf("This may cause strange/hard to debug errors\n");
KONRAD Frederic8d4e9142017-02-23 18:29:08 +0000226 }
227 mttcg_enabled = true;
228 }
229 } else if (strcmp(t, "single") == 0) {
230 mttcg_enabled = false;
231 } else {
232 error_setg(errp, "Invalid 'thread' setting %s", t);
233 }
234 } else {
235 mttcg_enabled = default_mttcg_enabled();
236 }
237}
Paolo Bonzini946fb272011-09-12 13:57:37 +0200238
Alex Bennéee4cd9652017-03-31 16:09:42 +0100239/* The current number of executed instructions is based on what we
240 * originally budgeted minus the current state of the decrementing
241 * icount counters in extra/u16.low.
242 */
243static int64_t cpu_get_icount_executed(CPUState *cpu)
244{
Richard Henderson5e140192019-03-28 11:54:23 -1000245 return (cpu->icount_budget -
246 (cpu_neg(cpu)->icount_decr.u16.low + cpu->icount_extra));
Alex Bennéee4cd9652017-03-31 16:09:42 +0100247}
248
Alex Bennée512d3c82017-04-05 12:32:37 +0100249/*
250 * Update the global shared timer_state.qemu_icount to take into
251 * account executed instructions. This is done by the TCG vCPU
252 * thread so the main-loop can see time has moved forward.
253 */
Paolo Bonzini9b4e6f42018-09-11 13:15:32 +0200254static void cpu_update_icount_locked(CPUState *cpu)
Alex Bennée512d3c82017-04-05 12:32:37 +0100255{
256 int64_t executed = cpu_get_icount_executed(cpu);
257 cpu->icount_budget -= executed;
258
Emilio G. Cota38adcb62018-09-10 19:27:49 -0400259 atomic_set_i64(&timers_state.qemu_icount,
260 timers_state.qemu_icount + executed);
Paolo Bonzini9b4e6f42018-09-11 13:15:32 +0200261}
262
263/*
264 * Update the global shared timer_state.qemu_icount to take into
265 * account executed instructions. This is done by the TCG vCPU
266 * thread so the main-loop can see time has moved forward.
267 */
268void cpu_update_icount(CPUState *cpu)
269{
270 seqlock_write_lock(&timers_state.vm_clock_seqlock,
271 &timers_state.vm_clock_lock);
272 cpu_update_icount_locked(cpu);
Paolo Bonzini94377112018-08-14 09:57:16 +0200273 seqlock_write_unlock(&timers_state.vm_clock_seqlock,
274 &timers_state.vm_clock_lock);
Alex Bennée512d3c82017-04-05 12:32:37 +0100275}
276
Paolo Bonzinic1ff0732018-08-14 09:31:58 +0200277static int64_t cpu_get_icount_raw_locked(void)
Paolo Bonzini946fb272011-09-12 13:57:37 +0200278{
Andreas Färber4917cf42013-05-27 05:17:50 +0200279 CPUState *cpu = current_cpu;
Paolo Bonzini946fb272011-09-12 13:57:37 +0200280
Alex Bennée243c5f72017-03-30 18:49:22 +0100281 if (cpu && cpu->running) {
Paolo Bonzini414b15c2015-06-24 14:16:26 +0200282 if (!cpu->can_do_io) {
Alistair Francis493d89b2018-02-03 09:43:14 +0100283 error_report("Bad icount read");
Pavel Dovgalyuk2a629142014-12-08 10:53:45 +0300284 exit(1);
Paolo Bonzini946fb272011-09-12 13:57:37 +0200285 }
Alex Bennéee4cd9652017-03-31 16:09:42 +0100286 /* Take into account what has run */
Paolo Bonzini9b4e6f42018-09-11 13:15:32 +0200287 cpu_update_icount_locked(cpu);
Paolo Bonzini946fb272011-09-12 13:57:37 +0200288 }
Emilio G. Cota38adcb62018-09-10 19:27:49 -0400289 /* The read is protected by the seqlock, but needs atomic64 to avoid UB */
290 return atomic_read_i64(&timers_state.qemu_icount);
Paolo Bonzinic1ff0732018-08-14 09:31:58 +0200291}
292
293static int64_t cpu_get_icount_locked(void)
294{
295 int64_t icount = cpu_get_icount_raw_locked();
Emilio G. Cotac97595d2018-09-10 19:27:50 -0400296 return atomic_read_i64(&timers_state.qemu_icount_bias) +
297 cpu_icount_to_ns(icount);
Paolo Bonzinic1ff0732018-08-14 09:31:58 +0200298}
299
300int64_t cpu_get_icount_raw(void)
301{
302 int64_t icount;
303 unsigned start;
304
305 do {
306 start = seqlock_read_begin(&timers_state.vm_clock_seqlock);
307 icount = cpu_get_icount_raw_locked();
308 } while (seqlock_read_retry(&timers_state.vm_clock_seqlock, start));
309
310 return icount;
Pavel Dovgalyuk2a629142014-12-08 10:53:45 +0300311}
312
313/* Return the virtual CPU time, based on the instruction counter. */
Paolo Bonzini17a15f12013-10-03 15:17:25 +0200314int64_t cpu_get_icount(void)
315{
316 int64_t icount;
317 unsigned start;
318
319 do {
320 start = seqlock_read_begin(&timers_state.vm_clock_seqlock);
321 icount = cpu_get_icount_locked();
322 } while (seqlock_read_retry(&timers_state.vm_clock_seqlock, start));
323
324 return icount;
325}
326
KONRAD Frederic3f031312014-08-01 01:37:15 +0200327int64_t cpu_icount_to_ns(int64_t icount)
328{
Paolo Bonzinic1ff0732018-08-14 09:31:58 +0200329 return icount << atomic_read(&timers_state.icount_time_shift);
KONRAD Frederic3f031312014-08-01 01:37:15 +0200330}
331
Paolo Bonzinif2a4ad62018-08-18 09:36:16 +0200332static int64_t cpu_get_ticks_locked(void)
333{
334 int64_t ticks = timers_state.cpu_ticks_offset;
335 if (timers_state.cpu_ticks_enabled) {
336 ticks += cpu_get_host_ticks();
337 }
338
339 if (timers_state.cpu_ticks_prev > ticks) {
340 /* Non increasing ticks may happen if the host uses software suspend. */
341 timers_state.cpu_ticks_offset += timers_state.cpu_ticks_prev - ticks;
342 ticks = timers_state.cpu_ticks_prev;
343 }
344
345 timers_state.cpu_ticks_prev = ticks;
346 return ticks;
347}
348
Cao jind90f3cc2016-07-29 19:05:38 +0800349/* return the time elapsed in VM between vm_start and vm_stop. Unless
350 * icount is active, cpu_get_ticks() uses units of the host CPU cycle
351 * counter.
Cao jind90f3cc2016-07-29 19:05:38 +0800352 */
Paolo Bonzini946fb272011-09-12 13:57:37 +0200353int64_t cpu_get_ticks(void)
354{
Paolo Bonzini5f3e3102013-10-28 17:32:18 +0100355 int64_t ticks;
356
Paolo Bonzini946fb272011-09-12 13:57:37 +0200357 if (use_icount) {
358 return cpu_get_icount();
359 }
Paolo Bonzini5f3e3102013-10-28 17:32:18 +0100360
Paolo Bonzinif2a4ad62018-08-18 09:36:16 +0200361 qemu_spin_lock(&timers_state.vm_clock_lock);
362 ticks = cpu_get_ticks_locked();
363 qemu_spin_unlock(&timers_state.vm_clock_lock);
Paolo Bonzini5f3e3102013-10-28 17:32:18 +0100364 return ticks;
Paolo Bonzini946fb272011-09-12 13:57:37 +0200365}
366
Liu Ping Fancb365642013-09-25 14:20:58 +0800367static int64_t cpu_get_clock_locked(void)
368{
Cao jin1d45cea2016-07-29 19:05:37 +0800369 int64_t time;
Liu Ping Fancb365642013-09-25 14:20:58 +0800370
Cao jin1d45cea2016-07-29 19:05:37 +0800371 time = timers_state.cpu_clock_offset;
Paolo Bonzini5f3e3102013-10-28 17:32:18 +0100372 if (timers_state.cpu_ticks_enabled) {
Cao jin1d45cea2016-07-29 19:05:37 +0800373 time += get_clock();
Liu Ping Fancb365642013-09-25 14:20:58 +0800374 }
375
Cao jin1d45cea2016-07-29 19:05:37 +0800376 return time;
Liu Ping Fancb365642013-09-25 14:20:58 +0800377}
378
Cao jind90f3cc2016-07-29 19:05:38 +0800379/* Return the monotonic time elapsed in VM, i.e.,
Peter Maydell8212ff82016-09-15 10:24:22 +0100380 * the time between vm_start and vm_stop
381 */
Paolo Bonzini946fb272011-09-12 13:57:37 +0200382int64_t cpu_get_clock(void)
383{
384 int64_t ti;
Liu Ping Fancb365642013-09-25 14:20:58 +0800385 unsigned start;
386
387 do {
388 start = seqlock_read_begin(&timers_state.vm_clock_seqlock);
389 ti = cpu_get_clock_locked();
390 } while (seqlock_read_retry(&timers_state.vm_clock_seqlock, start));
391
392 return ti;
Paolo Bonzini946fb272011-09-12 13:57:37 +0200393}
394
Liu Ping Fancb365642013-09-25 14:20:58 +0800395/* enable cpu_get_ticks()
Cao jin3224e872016-07-08 18:31:37 +0800396 * Caller must hold BQL which serves as mutex for vm_clock_seqlock.
Liu Ping Fancb365642013-09-25 14:20:58 +0800397 */
Paolo Bonzini946fb272011-09-12 13:57:37 +0200398void cpu_enable_ticks(void)
399{
Paolo Bonzini94377112018-08-14 09:57:16 +0200400 seqlock_write_lock(&timers_state.vm_clock_seqlock,
401 &timers_state.vm_clock_lock);
Paolo Bonzini946fb272011-09-12 13:57:37 +0200402 if (!timers_state.cpu_ticks_enabled) {
Christopher Covington4a7428c2015-09-25 10:42:21 -0400403 timers_state.cpu_ticks_offset -= cpu_get_host_ticks();
Paolo Bonzini946fb272011-09-12 13:57:37 +0200404 timers_state.cpu_clock_offset -= get_clock();
405 timers_state.cpu_ticks_enabled = 1;
406 }
Paolo Bonzini94377112018-08-14 09:57:16 +0200407 seqlock_write_unlock(&timers_state.vm_clock_seqlock,
408 &timers_state.vm_clock_lock);
Paolo Bonzini946fb272011-09-12 13:57:37 +0200409}
410
411/* disable cpu_get_ticks() : the clock is stopped. You must not call
Liu Ping Fancb365642013-09-25 14:20:58 +0800412 * cpu_get_ticks() after that.
Cao jin3224e872016-07-08 18:31:37 +0800413 * Caller must hold BQL which serves as mutex for vm_clock_seqlock.
Liu Ping Fancb365642013-09-25 14:20:58 +0800414 */
Paolo Bonzini946fb272011-09-12 13:57:37 +0200415void cpu_disable_ticks(void)
416{
Paolo Bonzini94377112018-08-14 09:57:16 +0200417 seqlock_write_lock(&timers_state.vm_clock_seqlock,
418 &timers_state.vm_clock_lock);
Paolo Bonzini946fb272011-09-12 13:57:37 +0200419 if (timers_state.cpu_ticks_enabled) {
Christopher Covington4a7428c2015-09-25 10:42:21 -0400420 timers_state.cpu_ticks_offset += cpu_get_host_ticks();
Liu Ping Fancb365642013-09-25 14:20:58 +0800421 timers_state.cpu_clock_offset = cpu_get_clock_locked();
Paolo Bonzini946fb272011-09-12 13:57:37 +0200422 timers_state.cpu_ticks_enabled = 0;
423 }
Paolo Bonzini94377112018-08-14 09:57:16 +0200424 seqlock_write_unlock(&timers_state.vm_clock_seqlock,
425 &timers_state.vm_clock_lock);
Paolo Bonzini946fb272011-09-12 13:57:37 +0200426}
427
428/* Correlation between real and virtual time is always going to be
429 fairly approximate, so ignore small variation.
430 When the guest is idle real and virtual time will be aligned in
431 the IO wait loop. */
Rutuja Shah73bcb242016-03-21 21:32:30 +0530432#define ICOUNT_WOBBLE (NANOSECONDS_PER_SECOND / 10)
Paolo Bonzini946fb272011-09-12 13:57:37 +0200433
434static void icount_adjust(void)
435{
436 int64_t cur_time;
437 int64_t cur_icount;
438 int64_t delta;
Paolo Bonzinia3270e12013-10-07 17:18:15 +0200439
440 /* Protected by TimersState mutex. */
Paolo Bonzini946fb272011-09-12 13:57:37 +0200441 static int64_t last_delta;
Paolo Bonzini468cc7c2013-10-07 17:21:51 +0200442
Paolo Bonzini946fb272011-09-12 13:57:37 +0200443 /* If the VM is not running, then do nothing. */
444 if (!runstate_is_running()) {
445 return;
446 }
Paolo Bonzini468cc7c2013-10-07 17:21:51 +0200447
Paolo Bonzini94377112018-08-14 09:57:16 +0200448 seqlock_write_lock(&timers_state.vm_clock_seqlock,
449 &timers_state.vm_clock_lock);
Paolo Bonzini17a15f12013-10-03 15:17:25 +0200450 cur_time = cpu_get_clock_locked();
451 cur_icount = cpu_get_icount_locked();
Paolo Bonzini468cc7c2013-10-07 17:21:51 +0200452
Paolo Bonzini946fb272011-09-12 13:57:37 +0200453 delta = cur_icount - cur_time;
454 /* FIXME: This is a very crude algorithm, somewhat prone to oscillation. */
455 if (delta > 0
456 && last_delta + ICOUNT_WOBBLE < delta * 2
Paolo Bonzinic1ff0732018-08-14 09:31:58 +0200457 && timers_state.icount_time_shift > 0) {
Paolo Bonzini946fb272011-09-12 13:57:37 +0200458 /* The guest is getting too far ahead. Slow time down. */
Paolo Bonzinic1ff0732018-08-14 09:31:58 +0200459 atomic_set(&timers_state.icount_time_shift,
460 timers_state.icount_time_shift - 1);
Paolo Bonzini946fb272011-09-12 13:57:37 +0200461 }
462 if (delta < 0
463 && last_delta - ICOUNT_WOBBLE > delta * 2
Paolo Bonzinic1ff0732018-08-14 09:31:58 +0200464 && timers_state.icount_time_shift < MAX_ICOUNT_SHIFT) {
Paolo Bonzini946fb272011-09-12 13:57:37 +0200465 /* The guest is getting too far behind. Speed time up. */
Paolo Bonzinic1ff0732018-08-14 09:31:58 +0200466 atomic_set(&timers_state.icount_time_shift,
467 timers_state.icount_time_shift + 1);
Paolo Bonzini946fb272011-09-12 13:57:37 +0200468 }
469 last_delta = delta;
Emilio G. Cotac97595d2018-09-10 19:27:50 -0400470 atomic_set_i64(&timers_state.qemu_icount_bias,
471 cur_icount - (timers_state.qemu_icount
472 << timers_state.icount_time_shift));
Paolo Bonzini94377112018-08-14 09:57:16 +0200473 seqlock_write_unlock(&timers_state.vm_clock_seqlock,
474 &timers_state.vm_clock_lock);
Paolo Bonzini946fb272011-09-12 13:57:37 +0200475}
476
477static void icount_adjust_rt(void *opaque)
478{
Pavel Dovgalyukb39e3f32018-01-11 11:26:10 +0300479 timer_mod(timers_state.icount_rt_timer,
Pavel Dovgalyuk1979b902015-01-12 15:00:43 +0300480 qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL_RT) + 1000);
Paolo Bonzini946fb272011-09-12 13:57:37 +0200481 icount_adjust();
482}
483
484static void icount_adjust_vm(void *opaque)
485{
Pavel Dovgalyukb39e3f32018-01-11 11:26:10 +0300486 timer_mod(timers_state.icount_vm_timer,
Alex Bligh40daca52013-08-21 16:03:02 +0100487 qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) +
Rutuja Shah73bcb242016-03-21 21:32:30 +0530488 NANOSECONDS_PER_SECOND / 10);
Paolo Bonzini946fb272011-09-12 13:57:37 +0200489 icount_adjust();
490}
491
492static int64_t qemu_icount_round(int64_t count)
493{
Paolo Bonzinic1ff0732018-08-14 09:31:58 +0200494 int shift = atomic_read(&timers_state.icount_time_shift);
495 return (count + (1 << shift) - 1) >> shift;
Paolo Bonzini946fb272011-09-12 13:57:37 +0200496}
497
Pavel Dovgalyukefab87c2015-09-17 19:24:39 +0300498static void icount_warp_rt(void)
Paolo Bonzini946fb272011-09-12 13:57:37 +0200499{
Alex Bennéeccffff42016-04-04 15:35:48 +0100500 unsigned seq;
501 int64_t warp_start;
502
Paolo Bonzini17a15f12013-10-03 15:17:25 +0200503 /* The icount_warp_timer is rescheduled soon after vm_clock_warp_start
504 * changes from -1 to another value, so the race here is okay.
505 */
Alex Bennéeccffff42016-04-04 15:35:48 +0100506 do {
507 seq = seqlock_read_begin(&timers_state.vm_clock_seqlock);
Pavel Dovgalyukb39e3f32018-01-11 11:26:10 +0300508 warp_start = timers_state.vm_clock_warp_start;
Alex Bennéeccffff42016-04-04 15:35:48 +0100509 } while (seqlock_read_retry(&timers_state.vm_clock_seqlock, seq));
510
511 if (warp_start == -1) {
Paolo Bonzini946fb272011-09-12 13:57:37 +0200512 return;
513 }
514
Paolo Bonzini94377112018-08-14 09:57:16 +0200515 seqlock_write_lock(&timers_state.vm_clock_seqlock,
516 &timers_state.vm_clock_lock);
Paolo Bonzini946fb272011-09-12 13:57:37 +0200517 if (runstate_is_running()) {
Paolo Bonzini74c0b812018-10-08 13:24:14 +0200518 int64_t clock = REPLAY_CLOCK_LOCKED(REPLAY_CLOCK_VIRTUAL_RT,
519 cpu_get_clock_locked());
Paolo Bonzini8ed961d2013-10-07 17:26:07 +0200520 int64_t warp_delta;
521
Pavel Dovgalyukb39e3f32018-01-11 11:26:10 +0300522 warp_delta = clock - timers_state.vm_clock_warp_start;
Paolo Bonzini8ed961d2013-10-07 17:26:07 +0200523 if (use_icount == 2) {
Paolo Bonzini946fb272011-09-12 13:57:37 +0200524 /*
Alex Bligh40daca52013-08-21 16:03:02 +0100525 * In adaptive mode, do not let QEMU_CLOCK_VIRTUAL run too
Paolo Bonzini946fb272011-09-12 13:57:37 +0200526 * far ahead of real time.
527 */
Paolo Bonzini17a15f12013-10-03 15:17:25 +0200528 int64_t cur_icount = cpu_get_icount_locked();
Pavel Dovgalyukbf2a7dd2014-11-26 13:40:55 +0300529 int64_t delta = clock - cur_icount;
Paolo Bonzini8ed961d2013-10-07 17:26:07 +0200530 warp_delta = MIN(warp_delta, delta);
Paolo Bonzini946fb272011-09-12 13:57:37 +0200531 }
Emilio G. Cotac97595d2018-09-10 19:27:50 -0400532 atomic_set_i64(&timers_state.qemu_icount_bias,
533 timers_state.qemu_icount_bias + warp_delta);
Paolo Bonzini946fb272011-09-12 13:57:37 +0200534 }
Pavel Dovgalyukb39e3f32018-01-11 11:26:10 +0300535 timers_state.vm_clock_warp_start = -1;
Paolo Bonzini94377112018-08-14 09:57:16 +0200536 seqlock_write_unlock(&timers_state.vm_clock_seqlock,
537 &timers_state.vm_clock_lock);
Paolo Bonzini8ed961d2013-10-07 17:26:07 +0200538
539 if (qemu_clock_expired(QEMU_CLOCK_VIRTUAL)) {
540 qemu_clock_notify(QEMU_CLOCK_VIRTUAL);
541 }
Paolo Bonzini946fb272011-09-12 13:57:37 +0200542}
543
Pavel Dovgalyuke76d1792016-03-10 14:56:09 +0300544static void icount_timer_cb(void *opaque)
Pavel Dovgalyukefab87c2015-09-17 19:24:39 +0300545{
Pavel Dovgalyuke76d1792016-03-10 14:56:09 +0300546 /* No need for a checkpoint because the timer already synchronizes
547 * with CHECKPOINT_CLOCK_VIRTUAL_RT.
548 */
549 icount_warp_rt();
Pavel Dovgalyukefab87c2015-09-17 19:24:39 +0300550}
551
Paolo Bonzini8156be52012-03-28 15:42:04 +0200552void qtest_clock_warp(int64_t dest)
553{
Alex Bligh40daca52013-08-21 16:03:02 +0100554 int64_t clock = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
Fam Zhengefef88b2015-01-19 17:51:43 +0800555 AioContext *aio_context;
Paolo Bonzini8156be52012-03-28 15:42:04 +0200556 assert(qtest_enabled());
Fam Zhengefef88b2015-01-19 17:51:43 +0800557 aio_context = qemu_get_aio_context();
Paolo Bonzini8156be52012-03-28 15:42:04 +0200558 while (clock < dest) {
Pavel Dovgalyukdcb15782019-07-25 11:44:26 +0300559 int64_t deadline = qemu_clock_deadline_ns_all(QEMU_CLOCK_VIRTUAL,
560 QEMU_TIMER_ATTR_ALL);
Sergey Fedorovc9299e22014-06-10 13:10:28 +0400561 int64_t warp = qemu_soonest_timeout(dest - clock, deadline);
Fam Zhengefef88b2015-01-19 17:51:43 +0800562
Paolo Bonzini94377112018-08-14 09:57:16 +0200563 seqlock_write_lock(&timers_state.vm_clock_seqlock,
564 &timers_state.vm_clock_lock);
Emilio G. Cotac97595d2018-09-10 19:27:50 -0400565 atomic_set_i64(&timers_state.qemu_icount_bias,
566 timers_state.qemu_icount_bias + warp);
Paolo Bonzini94377112018-08-14 09:57:16 +0200567 seqlock_write_unlock(&timers_state.vm_clock_seqlock,
568 &timers_state.vm_clock_lock);
Paolo Bonzini17a15f12013-10-03 15:17:25 +0200569
Alex Bligh40daca52013-08-21 16:03:02 +0100570 qemu_clock_run_timers(QEMU_CLOCK_VIRTUAL);
Fam Zhengefef88b2015-01-19 17:51:43 +0800571 timerlist_run_timers(aio_context->tlg.tl[QEMU_CLOCK_VIRTUAL]);
Alex Bligh40daca52013-08-21 16:03:02 +0100572 clock = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
Paolo Bonzini8156be52012-03-28 15:42:04 +0200573 }
Alex Bligh40daca52013-08-21 16:03:02 +0100574 qemu_clock_notify(QEMU_CLOCK_VIRTUAL);
Paolo Bonzini8156be52012-03-28 15:42:04 +0200575}
576
Pavel Dovgalyuke76d1792016-03-10 14:56:09 +0300577void qemu_start_warp_timer(void)
Paolo Bonzini946fb272011-09-12 13:57:37 +0200578{
Paolo Bonzinice78d182013-10-07 17:30:02 +0200579 int64_t clock;
Paolo Bonzini946fb272011-09-12 13:57:37 +0200580 int64_t deadline;
581
Pavel Dovgalyuke76d1792016-03-10 14:56:09 +0300582 if (!use_icount) {
Paolo Bonzini946fb272011-09-12 13:57:37 +0200583 return;
584 }
585
Pavel Dovgalyuk8bd7f712015-09-17 19:24:44 +0300586 /* Nothing to do if the VM is stopped: QEMU_CLOCK_VIRTUAL timers
587 * do not fire, so computing the deadline does not make sense.
588 */
589 if (!runstate_is_running()) {
590 return;
591 }
592
Pavel Dovgalyuk0c081852018-09-12 11:19:45 +0300593 if (replay_mode != REPLAY_MODE_PLAY) {
594 if (!all_cpu_threads_idle()) {
595 return;
596 }
Pavel Dovgalyuk8bd7f712015-09-17 19:24:44 +0300597
Pavel Dovgalyuk0c081852018-09-12 11:19:45 +0300598 if (qtest_enabled()) {
599 /* When testing, qtest commands advance icount. */
600 return;
601 }
Paolo Bonzini946fb272011-09-12 13:57:37 +0200602
Pavel Dovgalyuk0c081852018-09-12 11:19:45 +0300603 replay_checkpoint(CHECKPOINT_CLOCK_WARP_START);
604 } else {
605 /* warp clock deterministically in record/replay mode */
606 if (!replay_checkpoint(CHECKPOINT_CLOCK_WARP_START)) {
607 /* vCPU is sleeping and warp can't be started.
608 It is probably a race condition: notification sent
609 to vCPU was processed in advance and vCPU went to sleep.
610 Therefore we have to wake it up for doing someting. */
611 if (replay_has_checkpoint()) {
612 qemu_clock_notify(QEMU_CLOCK_VIRTUAL);
613 }
614 return;
615 }
Paolo Bonzini8156be52012-03-28 15:42:04 +0200616 }
617
Alex Blighac70aaf2013-08-21 16:02:57 +0100618 /* We want to use the earliest deadline from ALL vm_clocks */
Pavel Dovgalyukbf2a7dd2014-11-26 13:40:55 +0300619 clock = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL_RT);
Pavel Dovgalyukdcb15782019-07-25 11:44:26 +0300620 deadline = qemu_clock_deadline_ns_all(QEMU_CLOCK_VIRTUAL,
621 ~QEMU_TIMER_ATTR_EXTERNAL);
Paolo Bonzinice78d182013-10-07 17:30:02 +0200622 if (deadline < 0) {
Victor CLEMENTd7a0f712015-05-29 17:14:06 +0200623 static bool notified;
624 if (!icount_sleep && !notified) {
Alistair Francis3dc6f862017-07-12 06:57:41 -0700625 warn_report("icount sleep disabled and no active timers");
Victor CLEMENTd7a0f712015-05-29 17:14:06 +0200626 notified = true;
627 }
Paolo Bonzinice78d182013-10-07 17:30:02 +0200628 return;
Alex Blighac70aaf2013-08-21 16:02:57 +0100629 }
630
Paolo Bonzini946fb272011-09-12 13:57:37 +0200631 if (deadline > 0) {
632 /*
Alex Bligh40daca52013-08-21 16:03:02 +0100633 * Ensure QEMU_CLOCK_VIRTUAL proceeds even when the virtual CPU goes to
Paolo Bonzini946fb272011-09-12 13:57:37 +0200634 * sleep. Otherwise, the CPU might be waiting for a future timer
635 * interrupt to wake it up, but the interrupt never comes because
636 * the vCPU isn't running any insns and thus doesn't advance the
Alex Bligh40daca52013-08-21 16:03:02 +0100637 * QEMU_CLOCK_VIRTUAL.
Paolo Bonzini946fb272011-09-12 13:57:37 +0200638 */
Victor CLEMENT5045e9d92015-05-29 17:14:04 +0200639 if (!icount_sleep) {
640 /*
641 * We never let VCPUs sleep in no sleep icount mode.
642 * If there is a pending QEMU_CLOCK_VIRTUAL timer we just advance
643 * to the next QEMU_CLOCK_VIRTUAL event and notify it.
644 * It is useful when we want a deterministic execution time,
645 * isolated from host latencies.
646 */
Paolo Bonzini94377112018-08-14 09:57:16 +0200647 seqlock_write_lock(&timers_state.vm_clock_seqlock,
648 &timers_state.vm_clock_lock);
Emilio G. Cotac97595d2018-09-10 19:27:50 -0400649 atomic_set_i64(&timers_state.qemu_icount_bias,
650 timers_state.qemu_icount_bias + deadline);
Paolo Bonzini94377112018-08-14 09:57:16 +0200651 seqlock_write_unlock(&timers_state.vm_clock_seqlock,
652 &timers_state.vm_clock_lock);
Victor CLEMENT5045e9d92015-05-29 17:14:04 +0200653 qemu_clock_notify(QEMU_CLOCK_VIRTUAL);
654 } else {
655 /*
656 * We do stop VCPUs and only advance QEMU_CLOCK_VIRTUAL after some
657 * "real" time, (related to the time left until the next event) has
658 * passed. The QEMU_CLOCK_VIRTUAL_RT clock will do this.
659 * This avoids that the warps are visible externally; for example,
660 * you will not be sending network packets continuously instead of
661 * every 100ms.
662 */
Paolo Bonzini94377112018-08-14 09:57:16 +0200663 seqlock_write_lock(&timers_state.vm_clock_seqlock,
664 &timers_state.vm_clock_lock);
Pavel Dovgalyukb39e3f32018-01-11 11:26:10 +0300665 if (timers_state.vm_clock_warp_start == -1
666 || timers_state.vm_clock_warp_start > clock) {
667 timers_state.vm_clock_warp_start = clock;
Victor CLEMENT5045e9d92015-05-29 17:14:04 +0200668 }
Paolo Bonzini94377112018-08-14 09:57:16 +0200669 seqlock_write_unlock(&timers_state.vm_clock_seqlock,
670 &timers_state.vm_clock_lock);
Pavel Dovgalyukb39e3f32018-01-11 11:26:10 +0300671 timer_mod_anticipate(timers_state.icount_warp_timer,
672 clock + deadline);
Paolo Bonzinice78d182013-10-07 17:30:02 +0200673 }
Alex Blighac70aaf2013-08-21 16:02:57 +0100674 } else if (deadline == 0) {
Alex Bligh40daca52013-08-21 16:03:02 +0100675 qemu_clock_notify(QEMU_CLOCK_VIRTUAL);
Paolo Bonzini946fb272011-09-12 13:57:37 +0200676 }
677}
678
Pavel Dovgalyuke76d1792016-03-10 14:56:09 +0300679static void qemu_account_warp_timer(void)
680{
681 if (!use_icount || !icount_sleep) {
682 return;
683 }
684
685 /* Nothing to do if the VM is stopped: QEMU_CLOCK_VIRTUAL timers
686 * do not fire, so computing the deadline does not make sense.
687 */
688 if (!runstate_is_running()) {
689 return;
690 }
691
692 /* warp clock deterministically in record/replay mode */
693 if (!replay_checkpoint(CHECKPOINT_CLOCK_WARP_ACCOUNT)) {
694 return;
695 }
696
Pavel Dovgalyukb39e3f32018-01-11 11:26:10 +0300697 timer_del(timers_state.icount_warp_timer);
Pavel Dovgalyuke76d1792016-03-10 14:56:09 +0300698 icount_warp_rt();
699}
700
KONRAD Fredericd09eae32014-08-01 01:37:10 +0200701static bool icount_state_needed(void *opaque)
702{
703 return use_icount;
704}
705
Pavel Dovgalyukb39e3f32018-01-11 11:26:10 +0300706static bool warp_timer_state_needed(void *opaque)
707{
708 TimersState *s = opaque;
709 return s->icount_warp_timer != NULL;
710}
711
712static bool adjust_timers_state_needed(void *opaque)
713{
714 TimersState *s = opaque;
715 return s->icount_rt_timer != NULL;
716}
717
718/*
719 * Subsection for warp timer migration is optional, because may not be created
720 */
721static const VMStateDescription icount_vmstate_warp_timer = {
722 .name = "timer/icount/warp_timer",
723 .version_id = 1,
724 .minimum_version_id = 1,
725 .needed = warp_timer_state_needed,
726 .fields = (VMStateField[]) {
727 VMSTATE_INT64(vm_clock_warp_start, TimersState),
728 VMSTATE_TIMER_PTR(icount_warp_timer, TimersState),
729 VMSTATE_END_OF_LIST()
730 }
731};
732
733static const VMStateDescription icount_vmstate_adjust_timers = {
734 .name = "timer/icount/timers",
735 .version_id = 1,
736 .minimum_version_id = 1,
737 .needed = adjust_timers_state_needed,
738 .fields = (VMStateField[]) {
739 VMSTATE_TIMER_PTR(icount_rt_timer, TimersState),
740 VMSTATE_TIMER_PTR(icount_vm_timer, TimersState),
741 VMSTATE_END_OF_LIST()
742 }
743};
744
KONRAD Fredericd09eae32014-08-01 01:37:10 +0200745/*
746 * This is a subsection for icount migration.
747 */
748static const VMStateDescription icount_vmstate_timers = {
749 .name = "timer/icount",
750 .version_id = 1,
751 .minimum_version_id = 1,
Juan Quintela5cd8cad2014-09-23 14:09:54 +0200752 .needed = icount_state_needed,
KONRAD Fredericd09eae32014-08-01 01:37:10 +0200753 .fields = (VMStateField[]) {
754 VMSTATE_INT64(qemu_icount_bias, TimersState),
755 VMSTATE_INT64(qemu_icount, TimersState),
756 VMSTATE_END_OF_LIST()
Pavel Dovgalyukb39e3f32018-01-11 11:26:10 +0300757 },
758 .subsections = (const VMStateDescription*[]) {
759 &icount_vmstate_warp_timer,
760 &icount_vmstate_adjust_timers,
761 NULL
KONRAD Fredericd09eae32014-08-01 01:37:10 +0200762 }
763};
764
Paolo Bonzini946fb272011-09-12 13:57:37 +0200765static const VMStateDescription vmstate_timers = {
766 .name = "timer",
767 .version_id = 2,
768 .minimum_version_id = 1,
Juan Quintela35d08452014-04-16 16:01:33 +0200769 .fields = (VMStateField[]) {
Paolo Bonzini946fb272011-09-12 13:57:37 +0200770 VMSTATE_INT64(cpu_ticks_offset, TimersState),
Paolo Bonzinic1ff0732018-08-14 09:31:58 +0200771 VMSTATE_UNUSED(8),
Paolo Bonzini946fb272011-09-12 13:57:37 +0200772 VMSTATE_INT64_V(cpu_clock_offset, TimersState, 2),
773 VMSTATE_END_OF_LIST()
KONRAD Fredericd09eae32014-08-01 01:37:10 +0200774 },
Juan Quintela5cd8cad2014-09-23 14:09:54 +0200775 .subsections = (const VMStateDescription*[]) {
776 &icount_vmstate_timers,
777 NULL
Paolo Bonzini946fb272011-09-12 13:57:37 +0200778 }
779};
780
Paolo Bonzini14e6fe12016-10-31 10:36:08 +0100781static void cpu_throttle_thread(CPUState *cpu, run_on_cpu_data opaque)
Jason J. Herne2adcc852015-09-08 13:12:33 -0400782{
Jason J. Herne2adcc852015-09-08 13:12:33 -0400783 double pct;
784 double throttle_ratio;
785 long sleeptime_ns;
786
787 if (!cpu_throttle_get_percentage()) {
788 return;
789 }
790
791 pct = (double)cpu_throttle_get_percentage()/100;
792 throttle_ratio = pct / (1 - pct);
793 sleeptime_ns = (long)(throttle_ratio * CPU_THROTTLE_TIMESLICE_NS);
794
795 qemu_mutex_unlock_iothread();
Jason J. Herne2adcc852015-09-08 13:12:33 -0400796 g_usleep(sleeptime_ns / 1000); /* Convert ns to us for usleep call */
797 qemu_mutex_lock_iothread();
Felipe Franciosi90bb0c02017-05-19 22:29:50 +0100798 atomic_set(&cpu->throttle_thread_scheduled, 0);
Jason J. Herne2adcc852015-09-08 13:12:33 -0400799}
800
801static void cpu_throttle_timer_tick(void *opaque)
802{
803 CPUState *cpu;
804 double pct;
805
806 /* Stop the timer if needed */
807 if (!cpu_throttle_get_percentage()) {
808 return;
809 }
810 CPU_FOREACH(cpu) {
811 if (!atomic_xchg(&cpu->throttle_thread_scheduled, 1)) {
Paolo Bonzini14e6fe12016-10-31 10:36:08 +0100812 async_run_on_cpu(cpu, cpu_throttle_thread,
813 RUN_ON_CPU_NULL);
Jason J. Herne2adcc852015-09-08 13:12:33 -0400814 }
815 }
816
817 pct = (double)cpu_throttle_get_percentage()/100;
818 timer_mod(throttle_timer, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL_RT) +
819 CPU_THROTTLE_TIMESLICE_NS / (1-pct));
820}
821
822void cpu_throttle_set(int new_throttle_pct)
823{
824 /* Ensure throttle percentage is within valid range */
825 new_throttle_pct = MIN(new_throttle_pct, CPU_THROTTLE_PCT_MAX);
826 new_throttle_pct = MAX(new_throttle_pct, CPU_THROTTLE_PCT_MIN);
827
828 atomic_set(&throttle_percentage, new_throttle_pct);
829
830 timer_mod(throttle_timer, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL_RT) +
831 CPU_THROTTLE_TIMESLICE_NS);
832}
833
834void cpu_throttle_stop(void)
835{
836 atomic_set(&throttle_percentage, 0);
837}
838
839bool cpu_throttle_active(void)
840{
841 return (cpu_throttle_get_percentage() != 0);
842}
843
844int cpu_throttle_get_percentage(void)
845{
846 return atomic_read(&throttle_percentage);
847}
848
Pavel Dovgalyuk4603ea02014-09-01 09:34:49 +0400849void cpu_ticks_init(void)
850{
Emilio G. Cotaccdb3c12016-06-08 14:55:20 -0400851 seqlock_init(&timers_state.vm_clock_seqlock);
Emilio G. Cota87a09cd2018-09-03 13:18:29 -0400852 qemu_spin_init(&timers_state.vm_clock_lock);
Pavel Dovgalyuk4603ea02014-09-01 09:34:49 +0400853 vmstate_register(NULL, 0, &vmstate_timers, &timers_state);
Jason J. Herne2adcc852015-09-08 13:12:33 -0400854 throttle_timer = timer_new_ns(QEMU_CLOCK_VIRTUAL_RT,
855 cpu_throttle_timer_tick, NULL);
Pavel Dovgalyuk4603ea02014-09-01 09:34:49 +0400856}
857
Sebastian Tanase1ad95802014-07-25 11:56:28 +0200858void configure_icount(QemuOpts *opts, Error **errp)
Paolo Bonzini946fb272011-09-12 13:57:37 +0200859{
Sebastian Tanase1ad95802014-07-25 11:56:28 +0200860 const char *option;
Sebastian Tanasea8bfac32014-07-25 11:56:29 +0200861 char *rem_str = NULL;
Sebastian Tanase1ad95802014-07-25 11:56:28 +0200862
Sebastian Tanase1ad95802014-07-25 11:56:28 +0200863 option = qemu_opt_get(opts, "shift");
Paolo Bonzini946fb272011-09-12 13:57:37 +0200864 if (!option) {
Sebastian Tanasea8bfac32014-07-25 11:56:29 +0200865 if (qemu_opt_get(opts, "align") != NULL) {
866 error_setg(errp, "Please specify shift option when using align");
867 }
Paolo Bonzini946fb272011-09-12 13:57:37 +0200868 return;
869 }
Victor CLEMENTf1f4b572015-05-29 17:14:05 +0200870
871 icount_sleep = qemu_opt_get_bool(opts, "sleep", true);
Victor CLEMENT5045e9d92015-05-29 17:14:04 +0200872 if (icount_sleep) {
Pavel Dovgalyukb39e3f32018-01-11 11:26:10 +0300873 timers_state.icount_warp_timer = timer_new_ns(QEMU_CLOCK_VIRTUAL_RT,
Pavel Dovgalyuke76d1792016-03-10 14:56:09 +0300874 icount_timer_cb, NULL);
Victor CLEMENT5045e9d92015-05-29 17:14:04 +0200875 }
Victor CLEMENTf1f4b572015-05-29 17:14:05 +0200876
Sebastian Tanasea8bfac32014-07-25 11:56:29 +0200877 icount_align_option = qemu_opt_get_bool(opts, "align", false);
Victor CLEMENTf1f4b572015-05-29 17:14:05 +0200878
879 if (icount_align_option && !icount_sleep) {
Pranith Kumar778d9f92016-02-26 10:16:51 -0500880 error_setg(errp, "align=on and sleep=off are incompatible");
Victor CLEMENTf1f4b572015-05-29 17:14:05 +0200881 }
Paolo Bonzini946fb272011-09-12 13:57:37 +0200882 if (strcmp(option, "auto") != 0) {
Sebastian Tanasea8bfac32014-07-25 11:56:29 +0200883 errno = 0;
Paolo Bonzinic1ff0732018-08-14 09:31:58 +0200884 timers_state.icount_time_shift = strtol(option, &rem_str, 0);
Sebastian Tanasea8bfac32014-07-25 11:56:29 +0200885 if (errno != 0 || *rem_str != '\0' || !strlen(option)) {
886 error_setg(errp, "icount: Invalid shift value");
887 }
Paolo Bonzini946fb272011-09-12 13:57:37 +0200888 use_icount = 1;
889 return;
Sebastian Tanasea8bfac32014-07-25 11:56:29 +0200890 } else if (icount_align_option) {
891 error_setg(errp, "shift=auto and align=on are incompatible");
Victor CLEMENTf1f4b572015-05-29 17:14:05 +0200892 } else if (!icount_sleep) {
Pranith Kumar778d9f92016-02-26 10:16:51 -0500893 error_setg(errp, "shift=auto and sleep=off are incompatible");
Paolo Bonzini946fb272011-09-12 13:57:37 +0200894 }
895
896 use_icount = 2;
897
898 /* 125MIPS seems a reasonable initial guess at the guest speed.
899 It will be corrected fairly quickly anyway. */
Paolo Bonzinic1ff0732018-08-14 09:31:58 +0200900 timers_state.icount_time_shift = 3;
Paolo Bonzini946fb272011-09-12 13:57:37 +0200901
902 /* Have both realtime and virtual time triggers for speed adjustment.
903 The realtime trigger catches emulated time passing too slowly,
904 the virtual time trigger catches emulated time passing too fast.
905 Realtime triggers occur even when idle, so use them less frequently
906 than VM triggers. */
Pavel Dovgalyukb39e3f32018-01-11 11:26:10 +0300907 timers_state.vm_clock_warp_start = -1;
908 timers_state.icount_rt_timer = timer_new_ms(QEMU_CLOCK_VIRTUAL_RT,
Pavel Dovgalyukbf2a7dd2014-11-26 13:40:55 +0300909 icount_adjust_rt, NULL);
Pavel Dovgalyukb39e3f32018-01-11 11:26:10 +0300910 timer_mod(timers_state.icount_rt_timer,
Pavel Dovgalyukbf2a7dd2014-11-26 13:40:55 +0300911 qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL_RT) + 1000);
Pavel Dovgalyukb39e3f32018-01-11 11:26:10 +0300912 timers_state.icount_vm_timer = timer_new_ns(QEMU_CLOCK_VIRTUAL,
Alex Bligh40daca52013-08-21 16:03:02 +0100913 icount_adjust_vm, NULL);
Pavel Dovgalyukb39e3f32018-01-11 11:26:10 +0300914 timer_mod(timers_state.icount_vm_timer,
Alex Bligh40daca52013-08-21 16:03:02 +0100915 qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) +
Rutuja Shah73bcb242016-03-21 21:32:30 +0530916 NANOSECONDS_PER_SECOND / 10);
Paolo Bonzini946fb272011-09-12 13:57:37 +0200917}
918
919/***********************************************************/
Alex Bennée65467062017-02-23 18:29:09 +0000920/* TCG vCPU kick timer
921 *
922 * The kick timer is responsible for moving single threaded vCPU
923 * emulation on to the next vCPU. If more than one vCPU is running a
924 * timer event with force a cpu->exit so the next vCPU can get
925 * scheduled.
926 *
927 * The timer is removed if all vCPUs are idle and restarted again once
928 * idleness is complete.
929 */
930
931static QEMUTimer *tcg_kick_vcpu_timer;
Alex Bennée791158d2017-02-23 18:29:10 +0000932static CPUState *tcg_current_rr_cpu;
Alex Bennée65467062017-02-23 18:29:09 +0000933
934#define TCG_KICK_PERIOD (NANOSECONDS_PER_SECOND / 10)
935
936static inline int64_t qemu_tcg_next_kick(void)
937{
938 return qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) + TCG_KICK_PERIOD;
939}
940
Alex Bennée791158d2017-02-23 18:29:10 +0000941/* Kick the currently round-robin scheduled vCPU */
942static void qemu_cpu_kick_rr_cpu(void)
943{
944 CPUState *cpu;
Alex Bennée791158d2017-02-23 18:29:10 +0000945 do {
946 cpu = atomic_mb_read(&tcg_current_rr_cpu);
947 if (cpu) {
948 cpu_exit(cpu);
949 }
950 } while (cpu != atomic_mb_read(&tcg_current_rr_cpu));
951}
952
Paolo Bonzini6b8f0182017-03-02 19:56:40 +0100953static void do_nothing(CPUState *cpu, run_on_cpu_data unused)
954{
955}
956
Paolo Bonzini3f53bc62017-03-03 11:50:29 +0100957void qemu_timer_notify_cb(void *opaque, QEMUClockType type)
958{
Paolo Bonzini6b8f0182017-03-02 19:56:40 +0100959 if (!use_icount || type != QEMU_CLOCK_VIRTUAL) {
960 qemu_notify_event();
961 return;
962 }
963
Peter Maydellc52e7132018-04-10 13:02:25 +0100964 if (qemu_in_vcpu_thread()) {
965 /* A CPU is currently running; kick it back out to the
966 * tcg_cpu_exec() loop so it will recalculate its
967 * icount deadline immediately.
968 */
969 qemu_cpu_kick(current_cpu);
970 } else if (first_cpu) {
Paolo Bonzini6b8f0182017-03-02 19:56:40 +0100971 /* qemu_cpu_kick is not enough to kick a halted CPU out of
972 * qemu_tcg_wait_io_event. async_run_on_cpu, instead,
973 * causes cpu_thread_is_idle to return false. This way,
974 * handle_icount_deadline can run.
Peter Maydellc52e7132018-04-10 13:02:25 +0100975 * If we have no CPUs at all for some reason, we don't
976 * need to do anything.
Paolo Bonzini6b8f0182017-03-02 19:56:40 +0100977 */
978 async_run_on_cpu(first_cpu, do_nothing, RUN_ON_CPU_NULL);
979 }
Paolo Bonzini3f53bc62017-03-03 11:50:29 +0100980}
981
Alex Bennée65467062017-02-23 18:29:09 +0000982static void kick_tcg_thread(void *opaque)
983{
984 timer_mod(tcg_kick_vcpu_timer, qemu_tcg_next_kick());
Alex Bennée791158d2017-02-23 18:29:10 +0000985 qemu_cpu_kick_rr_cpu();
Alex Bennée65467062017-02-23 18:29:09 +0000986}
987
988static void start_tcg_kick_timer(void)
989{
Paolo Bonzinidb08b682018-01-11 13:53:12 +0100990 assert(!mttcg_enabled);
991 if (!tcg_kick_vcpu_timer && CPU_NEXT(first_cpu)) {
Alex Bennée65467062017-02-23 18:29:09 +0000992 tcg_kick_vcpu_timer = timer_new_ns(QEMU_CLOCK_VIRTUAL,
993 kick_tcg_thread, NULL);
Alex Bennée1926ab22018-09-27 18:17:24 +0100994 }
995 if (tcg_kick_vcpu_timer && !timer_pending(tcg_kick_vcpu_timer)) {
Alex Bennée65467062017-02-23 18:29:09 +0000996 timer_mod(tcg_kick_vcpu_timer, qemu_tcg_next_kick());
997 }
998}
999
1000static void stop_tcg_kick_timer(void)
1001{
Paolo Bonzinidb08b682018-01-11 13:53:12 +01001002 assert(!mttcg_enabled);
Alex Bennée1926ab22018-09-27 18:17:24 +01001003 if (tcg_kick_vcpu_timer && timer_pending(tcg_kick_vcpu_timer)) {
Alex Bennée65467062017-02-23 18:29:09 +00001004 timer_del(tcg_kick_vcpu_timer);
Alex Bennée65467062017-02-23 18:29:09 +00001005 }
1006}
1007
Alex Bennée65467062017-02-23 18:29:09 +00001008/***********************************************************/
Blue Swirl296af7c2010-03-29 19:23:50 +00001009void hw_error(const char *fmt, ...)
1010{
1011 va_list ap;
Andreas Färber55e5c282012-12-17 06:18:02 +01001012 CPUState *cpu;
Blue Swirl296af7c2010-03-29 19:23:50 +00001013
1014 va_start(ap, fmt);
1015 fprintf(stderr, "qemu: hardware error: ");
1016 vfprintf(stderr, fmt, ap);
1017 fprintf(stderr, "\n");
Andreas Färberbdc44642013-06-24 23:50:24 +02001018 CPU_FOREACH(cpu) {
Andreas Färber55e5c282012-12-17 06:18:02 +01001019 fprintf(stderr, "CPU #%d:\n", cpu->cpu_index);
Markus Armbruster90c84c52019-04-17 21:18:02 +02001020 cpu_dump_state(cpu, stderr, CPU_DUMP_FPU);
Blue Swirl296af7c2010-03-29 19:23:50 +00001021 }
1022 va_end(ap);
1023 abort();
1024}
1025
1026void cpu_synchronize_all_states(void)
1027{
Andreas Färber182735e2013-05-29 22:29:20 +02001028 CPUState *cpu;
Blue Swirl296af7c2010-03-29 19:23:50 +00001029
Andreas Färberbdc44642013-06-24 23:50:24 +02001030 CPU_FOREACH(cpu) {
Andreas Färber182735e2013-05-29 22:29:20 +02001031 cpu_synchronize_state(cpu);
Sergio Andres Gomez Del Realc97d6d22017-09-13 04:05:09 -05001032 /* TODO: move to cpu_synchronize_state() */
1033 if (hvf_enabled()) {
1034 hvf_cpu_synchronize_state(cpu);
1035 }
Blue Swirl296af7c2010-03-29 19:23:50 +00001036 }
1037}
1038
1039void cpu_synchronize_all_post_reset(void)
1040{
Andreas Färber182735e2013-05-29 22:29:20 +02001041 CPUState *cpu;
Blue Swirl296af7c2010-03-29 19:23:50 +00001042
Andreas Färberbdc44642013-06-24 23:50:24 +02001043 CPU_FOREACH(cpu) {
Andreas Färber182735e2013-05-29 22:29:20 +02001044 cpu_synchronize_post_reset(cpu);
Sergio Andres Gomez Del Realc97d6d22017-09-13 04:05:09 -05001045 /* TODO: move to cpu_synchronize_post_reset() */
1046 if (hvf_enabled()) {
1047 hvf_cpu_synchronize_post_reset(cpu);
1048 }
Blue Swirl296af7c2010-03-29 19:23:50 +00001049 }
1050}
1051
1052void cpu_synchronize_all_post_init(void)
1053{
Andreas Färber182735e2013-05-29 22:29:20 +02001054 CPUState *cpu;
Blue Swirl296af7c2010-03-29 19:23:50 +00001055
Andreas Färberbdc44642013-06-24 23:50:24 +02001056 CPU_FOREACH(cpu) {
Andreas Färber182735e2013-05-29 22:29:20 +02001057 cpu_synchronize_post_init(cpu);
Sergio Andres Gomez Del Realc97d6d22017-09-13 04:05:09 -05001058 /* TODO: move to cpu_synchronize_post_init() */
1059 if (hvf_enabled()) {
1060 hvf_cpu_synchronize_post_init(cpu);
1061 }
Blue Swirl296af7c2010-03-29 19:23:50 +00001062 }
1063}
1064
David Gibson75e972d2017-05-26 14:46:28 +10001065void cpu_synchronize_all_pre_loadvm(void)
1066{
1067 CPUState *cpu;
1068
1069 CPU_FOREACH(cpu) {
1070 cpu_synchronize_pre_loadvm(cpu);
1071 }
1072}
1073
Stefan Hajnoczi4486e892018-03-07 14:42:05 +00001074static int do_vm_stop(RunState state, bool send_stop)
Blue Swirl296af7c2010-03-29 19:23:50 +00001075{
Kevin Wolf56983462013-07-05 13:49:54 +02001076 int ret = 0;
1077
Luiz Capitulino13548692011-07-29 15:36:43 -03001078 if (runstate_is_running()) {
Blue Swirl296af7c2010-03-29 19:23:50 +00001079 cpu_disable_ticks();
Blue Swirl296af7c2010-03-29 19:23:50 +00001080 pause_all_vcpus();
Luiz Capitulinof5bbfba2011-07-29 15:04:45 -03001081 runstate_set(state);
Luiz Capitulino1dfb4dd2011-07-29 14:26:33 -03001082 vm_state_notify(0, state);
Stefan Hajnoczi4486e892018-03-07 14:42:05 +00001083 if (send_stop) {
Peter Xu3ab72382018-08-15 21:37:37 +08001084 qapi_event_send_stop();
Stefan Hajnoczi4486e892018-03-07 14:42:05 +00001085 }
Blue Swirl296af7c2010-03-29 19:23:50 +00001086 }
Kevin Wolf56983462013-07-05 13:49:54 +02001087
Kevin Wolf594a45c2013-07-18 14:52:19 +02001088 bdrv_drain_all();
Pavel Dovgalyuk6d0ceb82016-09-26 11:08:16 +03001089 replay_disable_events();
John Snow22af08e2016-09-22 21:45:51 -04001090 ret = bdrv_flush_all();
Kevin Wolf594a45c2013-07-18 14:52:19 +02001091
Kevin Wolf56983462013-07-05 13:49:54 +02001092 return ret;
Blue Swirl296af7c2010-03-29 19:23:50 +00001093}
1094
Stefan Hajnoczi4486e892018-03-07 14:42:05 +00001095/* Special vm_stop() variant for terminating the process. Historically clients
1096 * did not expect a QMP STOP event and so we need to retain compatibility.
1097 */
1098int vm_shutdown(void)
1099{
1100 return do_vm_stop(RUN_STATE_SHUTDOWN, false);
1101}
1102
Andreas Färbera1fcaa72012-05-02 23:42:26 +02001103static bool cpu_can_run(CPUState *cpu)
Blue Swirl296af7c2010-03-29 19:23:50 +00001104{
Andreas Färber4fdeee72012-05-02 23:10:09 +02001105 if (cpu->stop) {
Andreas Färbera1fcaa72012-05-02 23:42:26 +02001106 return false;
Jan Kiszka0ab07c62011-02-07 12:19:14 +01001107 }
Tiejun Chen321bc0b2013-08-02 09:43:09 +08001108 if (cpu_is_stopped(cpu)) {
Andreas Färbera1fcaa72012-05-02 23:42:26 +02001109 return false;
Jan Kiszka0ab07c62011-02-07 12:19:14 +01001110 }
Andreas Färbera1fcaa72012-05-02 23:42:26 +02001111 return true;
Blue Swirl296af7c2010-03-29 19:23:50 +00001112}
1113
Andreas Färber91325042013-05-27 02:07:49 +02001114static void cpu_handle_guest_debug(CPUState *cpu)
Jan Kiszka3c638d02010-06-25 16:56:56 +02001115{
Andreas Färber64f6b342013-05-27 02:06:09 +02001116 gdb_set_stop_cpu(cpu);
Jan Kiszka8cf71712011-02-07 12:19:16 +01001117 qemu_system_debug_request();
Andreas Färberf324e762012-05-02 23:26:21 +02001118 cpu->stopped = true;
Jan Kiszka3c638d02010-06-25 16:56:56 +02001119}
1120
Jan Kiszka6d9cb732011-02-01 22:15:58 +01001121#ifdef CONFIG_LINUX
1122static void sigbus_reraise(void)
1123{
1124 sigset_t set;
1125 struct sigaction action;
1126
1127 memset(&action, 0, sizeof(action));
1128 action.sa_handler = SIG_DFL;
1129 if (!sigaction(SIGBUS, &action, NULL)) {
1130 raise(SIGBUS);
1131 sigemptyset(&set);
1132 sigaddset(&set, SIGBUS);
Peter Maydella2d17612016-05-16 18:33:59 +01001133 pthread_sigmask(SIG_UNBLOCK, &set, NULL);
Jan Kiszka6d9cb732011-02-01 22:15:58 +01001134 }
1135 perror("Failed to re-raise SIGBUS!\n");
1136 abort();
1137}
1138
Paolo Bonzinid98d4072017-02-08 13:22:12 +01001139static void sigbus_handler(int n, siginfo_t *siginfo, void *ctx)
Jan Kiszka6d9cb732011-02-01 22:15:58 +01001140{
Paolo Bonzinia16fc072017-02-09 09:50:02 +01001141 if (siginfo->si_code != BUS_MCEERR_AO && siginfo->si_code != BUS_MCEERR_AR) {
1142 sigbus_reraise();
1143 }
1144
Paolo Bonzini2ae41db2017-02-08 12:48:54 +01001145 if (current_cpu) {
1146 /* Called asynchronously in VCPU thread. */
1147 if (kvm_on_sigbus_vcpu(current_cpu, siginfo->si_code, siginfo->si_addr)) {
1148 sigbus_reraise();
1149 }
1150 } else {
1151 /* Called synchronously (via signalfd) in main thread. */
1152 if (kvm_on_sigbus(siginfo->si_code, siginfo->si_addr)) {
1153 sigbus_reraise();
1154 }
Jan Kiszka6d9cb732011-02-01 22:15:58 +01001155 }
1156}
1157
1158static void qemu_init_sigbus(void)
1159{
1160 struct sigaction action;
1161
1162 memset(&action, 0, sizeof(action));
1163 action.sa_flags = SA_SIGINFO;
Paolo Bonzinid98d4072017-02-08 13:22:12 +01001164 action.sa_sigaction = sigbus_handler;
Jan Kiszka6d9cb732011-02-01 22:15:58 +01001165 sigaction(SIGBUS, &action, NULL);
1166
1167 prctl(PR_MCE_KILL, PR_MCE_KILL_SET, PR_MCE_KILL_EARLY, 0, 0);
1168}
Paolo Bonzinia16fc072017-02-09 09:50:02 +01001169#else /* !CONFIG_LINUX */
1170static void qemu_init_sigbus(void)
1171{
1172}
Paolo Bonzinia16fc072017-02-09 09:50:02 +01001173#endif /* !CONFIG_LINUX */
Blue Swirl296af7c2010-03-29 19:23:50 +00001174
Stefan Weilb2532d82012-09-27 07:41:42 +02001175static QemuMutex qemu_global_mutex;
Blue Swirl296af7c2010-03-29 19:23:50 +00001176
1177static QemuThread io_thread;
1178
Blue Swirl296af7c2010-03-29 19:23:50 +00001179/* cpu creation */
1180static QemuCond qemu_cpu_cond;
1181/* system init */
Blue Swirl296af7c2010-03-29 19:23:50 +00001182static QemuCond qemu_pause_cond;
1183
Paolo Bonzinid3b12f52011-09-13 10:30:52 +02001184void qemu_init_cpu_loop(void)
Blue Swirl296af7c2010-03-29 19:23:50 +00001185{
Jan Kiszka6d9cb732011-02-01 22:15:58 +01001186 qemu_init_sigbus();
Anthony Liguoried945922011-02-08 18:18:18 +01001187 qemu_cond_init(&qemu_cpu_cond);
Anthony Liguoried945922011-02-08 18:18:18 +01001188 qemu_cond_init(&qemu_pause_cond);
Blue Swirl296af7c2010-03-29 19:23:50 +00001189 qemu_mutex_init(&qemu_global_mutex);
Blue Swirl296af7c2010-03-29 19:23:50 +00001190
Jan Kiszkab7680cb2011-03-12 17:43:51 +01001191 qemu_thread_get_self(&io_thread);
Blue Swirl296af7c2010-03-29 19:23:50 +00001192}
1193
Paolo Bonzini14e6fe12016-10-31 10:36:08 +01001194void run_on_cpu(CPUState *cpu, run_on_cpu_func func, run_on_cpu_data data)
Marcelo Tosattie82bcec2010-05-04 09:45:22 -03001195{
Sergey Fedorovd148d902016-08-29 09:51:00 +02001196 do_run_on_cpu(cpu, func, data, &qemu_global_mutex);
Chegu Vinod3c022702013-06-24 03:49:41 -06001197}
1198
Gu Zheng4c055ab2016-05-12 09:18:13 +05301199static void qemu_kvm_destroy_vcpu(CPUState *cpu)
1200{
1201 if (kvm_destroy_vcpu(cpu) < 0) {
1202 error_report("kvm_destroy_vcpu failed");
1203 exit(EXIT_FAILURE);
1204 }
1205}
1206
1207static void qemu_tcg_destroy_vcpu(CPUState *cpu)
1208{
1209}
1210
David Hildenbrandebd05fe2017-11-29 20:12:15 +01001211static void qemu_cpu_stop(CPUState *cpu, bool exit)
1212{
1213 g_assert(qemu_cpu_is_self(cpu));
1214 cpu->stop = false;
1215 cpu->stopped = true;
1216 if (exit) {
1217 cpu_exit(cpu);
1218 }
1219 qemu_cond_broadcast(&qemu_pause_cond);
1220}
1221
Andreas Färber509a0d72012-05-03 02:18:09 +02001222static void qemu_wait_io_event_common(CPUState *cpu)
Blue Swirl296af7c2010-03-29 19:23:50 +00001223{
Alex Bennée37257942017-02-23 18:29:14 +00001224 atomic_mb_set(&cpu->thread_kicked, false);
Andreas Färber4fdeee72012-05-02 23:10:09 +02001225 if (cpu->stop) {
David Hildenbrandebd05fe2017-11-29 20:12:15 +01001226 qemu_cpu_stop(cpu, false);
Blue Swirl296af7c2010-03-29 19:23:50 +00001227 }
Sergey Fedorova5403c62016-08-02 18:27:36 +01001228 process_queued_cpu_work(cpu);
Alex Bennée37257942017-02-23 18:29:14 +00001229}
1230
Paolo Bonzinia8efa602018-11-14 12:36:57 +01001231static void qemu_tcg_rr_wait_io_event(void)
Alex Bennée37257942017-02-23 18:29:14 +00001232{
Paolo Bonzinia8efa602018-11-14 12:36:57 +01001233 CPUState *cpu;
1234
Paolo Bonzinidb08b682018-01-11 13:53:12 +01001235 while (all_cpu_threads_idle()) {
Alex Bennée65467062017-02-23 18:29:09 +00001236 stop_tcg_kick_timer();
Paolo Bonzinia8efa602018-11-14 12:36:57 +01001237 qemu_cond_wait(first_cpu->halt_cond, &qemu_global_mutex);
Jan Kiszka16400322011-02-09 16:29:37 +01001238 }
Blue Swirl296af7c2010-03-29 19:23:50 +00001239
Alex Bennée65467062017-02-23 18:29:09 +00001240 start_tcg_kick_timer();
1241
Paolo Bonzinia8efa602018-11-14 12:36:57 +01001242 CPU_FOREACH(cpu) {
1243 qemu_wait_io_event_common(cpu);
1244 }
Blue Swirl296af7c2010-03-29 19:23:50 +00001245}
1246
Paolo Bonzinidb08b682018-01-11 13:53:12 +01001247static void qemu_wait_io_event(CPUState *cpu)
Blue Swirl296af7c2010-03-29 19:23:50 +00001248{
Andreas Färbera98ae1d2013-05-26 23:21:08 +02001249 while (cpu_thread_is_idle(cpu)) {
Andreas Färberf5c121b2012-05-03 01:22:49 +02001250 qemu_cond_wait(cpu->halt_cond, &qemu_global_mutex);
Jan Kiszka16400322011-02-09 16:29:37 +01001251 }
Blue Swirl296af7c2010-03-29 19:23:50 +00001252
Paolo Bonzinidb08b682018-01-11 13:53:12 +01001253#ifdef _WIN32
1254 /* Eat dummy APC queued by qemu_cpu_kick_thread. */
1255 if (!tcg_enabled()) {
1256 SleepEx(0, TRUE);
Sergio Andres Gomez Del Realc97d6d22017-09-13 04:05:09 -05001257 }
Paolo Bonzinidb08b682018-01-11 13:53:12 +01001258#endif
Sergio Andres Gomez Del Realc97d6d22017-09-13 04:05:09 -05001259 qemu_wait_io_event_common(cpu);
1260}
1261
Jan Kiszka7e97cd82011-02-07 12:19:12 +01001262static void *qemu_kvm_cpu_thread_fn(void *arg)
Blue Swirl296af7c2010-03-29 19:23:50 +00001263{
Andreas Färber48a106b2013-05-27 02:20:39 +02001264 CPUState *cpu = arg;
Jan Kiszka84b49152011-02-01 22:15:50 +01001265 int r;
Blue Swirl296af7c2010-03-29 19:23:50 +00001266
Paolo Bonziniab28bd22015-07-09 08:55:38 +02001267 rcu_register_thread();
1268
Paolo Bonzini2e7f7a32015-06-18 18:47:18 +02001269 qemu_mutex_lock_iothread();
Andreas Färber814e6122012-05-02 17:00:37 +02001270 qemu_thread_get_self(cpu->thread);
Andreas Färber9f09e182012-05-03 06:59:07 +02001271 cpu->thread_id = qemu_get_thread_id();
Pavel Dovgalyuk626cf8f2014-12-08 10:53:17 +03001272 cpu->can_do_io = 1;
Andreas Färber4917cf42013-05-27 05:17:50 +02001273 current_cpu = cpu;
Blue Swirl296af7c2010-03-29 19:23:50 +00001274
Andreas Färber504134d2012-12-17 06:38:45 +01001275 r = kvm_init_vcpu(cpu);
Jan Kiszka84b49152011-02-01 22:15:50 +01001276 if (r < 0) {
Alistair Francis493d89b2018-02-03 09:43:14 +01001277 error_report("kvm_init_vcpu failed: %s", strerror(-r));
Jan Kiszka84b49152011-02-01 22:15:50 +01001278 exit(1);
1279 }
Blue Swirl296af7c2010-03-29 19:23:50 +00001280
Paolo Bonzini18268b62017-02-09 09:41:14 +01001281 kvm_init_cpu_signals(cpu);
Blue Swirl296af7c2010-03-29 19:23:50 +00001282
1283 /* signal CPU creation */
Andreas Färber61a46212012-05-02 22:49:36 +02001284 cpu->created = true;
Blue Swirl296af7c2010-03-29 19:23:50 +00001285 qemu_cond_signal(&qemu_cpu_cond);
Richard Henderson9c09a252019-03-14 13:06:29 -07001286 qemu_guest_random_seed_thread_part2(cpu->random_seed);
Blue Swirl296af7c2010-03-29 19:23:50 +00001287
Gu Zheng4c055ab2016-05-12 09:18:13 +05301288 do {
Andreas Färbera1fcaa72012-05-02 23:42:26 +02001289 if (cpu_can_run(cpu)) {
Andreas Färber1458c362013-05-26 23:46:55 +02001290 r = kvm_cpu_exec(cpu);
Jan Kiszka83f338f2011-02-07 12:19:17 +01001291 if (r == EXCP_DEBUG) {
Andreas Färber91325042013-05-27 02:07:49 +02001292 cpu_handle_guest_debug(cpu);
Jan Kiszka83f338f2011-02-07 12:19:17 +01001293 }
Jan Kiszka0ab07c62011-02-07 12:19:14 +01001294 }
Paolo Bonzinidb08b682018-01-11 13:53:12 +01001295 qemu_wait_io_event(cpu);
Gu Zheng4c055ab2016-05-12 09:18:13 +05301296 } while (!cpu->unplug || cpu_can_run(cpu));
Blue Swirl296af7c2010-03-29 19:23:50 +00001297
Gu Zheng4c055ab2016-05-12 09:18:13 +05301298 qemu_kvm_destroy_vcpu(cpu);
Bharata B Rao2c579042016-05-12 09:18:14 +05301299 cpu->created = false;
1300 qemu_cond_signal(&qemu_cpu_cond);
Gu Zheng4c055ab2016-05-12 09:18:13 +05301301 qemu_mutex_unlock_iothread();
Paolo Bonzini57615ed2018-01-30 11:04:36 -05001302 rcu_unregister_thread();
Blue Swirl296af7c2010-03-29 19:23:50 +00001303 return NULL;
1304}
1305
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +02001306static void *qemu_dummy_cpu_thread_fn(void *arg)
1307{
1308#ifdef _WIN32
Alistair Francis493d89b2018-02-03 09:43:14 +01001309 error_report("qtest is not supported under Windows");
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +02001310 exit(1);
1311#else
Andreas Färber10a90212013-05-27 02:24:35 +02001312 CPUState *cpu = arg;
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +02001313 sigset_t waitset;
1314 int r;
1315
Paolo Bonziniab28bd22015-07-09 08:55:38 +02001316 rcu_register_thread();
1317
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +02001318 qemu_mutex_lock_iothread();
Andreas Färber814e6122012-05-02 17:00:37 +02001319 qemu_thread_get_self(cpu->thread);
Andreas Färber9f09e182012-05-03 06:59:07 +02001320 cpu->thread_id = qemu_get_thread_id();
Pavel Dovgalyuk626cf8f2014-12-08 10:53:17 +03001321 cpu->can_do_io = 1;
Alex Bennée37257942017-02-23 18:29:14 +00001322 current_cpu = cpu;
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +02001323
1324 sigemptyset(&waitset);
1325 sigaddset(&waitset, SIG_IPI);
1326
1327 /* signal CPU creation */
Andreas Färber61a46212012-05-02 22:49:36 +02001328 cpu->created = true;
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +02001329 qemu_cond_signal(&qemu_cpu_cond);
Richard Henderson9c09a252019-03-14 13:06:29 -07001330 qemu_guest_random_seed_thread_part2(cpu->random_seed);
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +02001331
Paolo Bonzinid2831ab2018-01-30 11:04:53 -05001332 do {
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +02001333 qemu_mutex_unlock_iothread();
1334 do {
1335 int sig;
1336 r = sigwait(&waitset, &sig);
1337 } while (r == -1 && (errno == EAGAIN || errno == EINTR));
1338 if (r == -1) {
1339 perror("sigwait");
1340 exit(1);
1341 }
1342 qemu_mutex_lock_iothread();
Paolo Bonzinidb08b682018-01-11 13:53:12 +01001343 qemu_wait_io_event(cpu);
Paolo Bonzinid2831ab2018-01-30 11:04:53 -05001344 } while (!cpu->unplug);
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +02001345
David Hildenbrandd40bfcb2019-02-18 10:21:57 +01001346 qemu_mutex_unlock_iothread();
Paolo Bonzinid2831ab2018-01-30 11:04:53 -05001347 rcu_unregister_thread();
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +02001348 return NULL;
1349#endif
1350}
1351
Alex Bennée1be7fcb2016-10-27 16:10:08 +01001352static int64_t tcg_get_icount_limit(void)
1353{
1354 int64_t deadline;
1355
1356 if (replay_mode != REPLAY_MODE_PLAY) {
Pavel Dovgalyukdcb15782019-07-25 11:44:26 +03001357 /*
1358 * Include all the timers, because they may need an attention.
1359 * Too long CPU execution may create unnecessary delay in UI.
1360 */
1361 deadline = qemu_clock_deadline_ns_all(QEMU_CLOCK_VIRTUAL,
1362 QEMU_TIMER_ATTR_ALL);
Alex Bennée1be7fcb2016-10-27 16:10:08 +01001363
1364 /* Maintain prior (possibly buggy) behaviour where if no deadline
1365 * was set (as there is no QEMU_CLOCK_VIRTUAL timer) or it is more than
1366 * INT32_MAX nanoseconds ahead, we still use INT32_MAX
1367 * nanoseconds.
1368 */
1369 if ((deadline < 0) || (deadline > INT32_MAX)) {
1370 deadline = INT32_MAX;
1371 }
1372
1373 return qemu_icount_round(deadline);
1374 } else {
1375 return replay_get_instructions();
1376 }
1377}
1378
Alex Bennée12e97002016-10-27 16:10:14 +01001379static void handle_icount_deadline(void)
1380{
Paolo Bonzini6b8f0182017-03-02 19:56:40 +01001381 assert(qemu_in_vcpu_thread());
Alex Bennée12e97002016-10-27 16:10:14 +01001382 if (use_icount) {
Pavel Dovgalyukdcb15782019-07-25 11:44:26 +03001383 int64_t deadline = qemu_clock_deadline_ns_all(QEMU_CLOCK_VIRTUAL,
1384 QEMU_TIMER_ATTR_ALL);
Alex Bennée12e97002016-10-27 16:10:14 +01001385
1386 if (deadline == 0) {
Paolo Bonzini6b8f0182017-03-02 19:56:40 +01001387 /* Wake up other AioContexts. */
Alex Bennée12e97002016-10-27 16:10:14 +01001388 qemu_clock_notify(QEMU_CLOCK_VIRTUAL);
Paolo Bonzini6b8f0182017-03-02 19:56:40 +01001389 qemu_clock_run_timers(QEMU_CLOCK_VIRTUAL);
Alex Bennée12e97002016-10-27 16:10:14 +01001390 }
1391 }
1392}
1393
Alex Bennée05248382017-03-29 16:46:59 +01001394static void prepare_icount_for_run(CPUState *cpu)
1395{
1396 if (use_icount) {
Alex Bennéeeda5f7c2017-04-05 12:35:48 +01001397 int insns_left;
Alex Bennée05248382017-03-29 16:46:59 +01001398
1399 /* These should always be cleared by process_icount_data after
1400 * each vCPU execution. However u16.high can be raised
1401 * asynchronously by cpu_exit/cpu_interrupt/tcg_handle_interrupt
1402 */
Richard Henderson5e140192019-03-28 11:54:23 -10001403 g_assert(cpu_neg(cpu)->icount_decr.u16.low == 0);
Alex Bennée05248382017-03-29 16:46:59 +01001404 g_assert(cpu->icount_extra == 0);
1405
Alex Bennéeeda5f7c2017-04-05 12:35:48 +01001406 cpu->icount_budget = tcg_get_icount_limit();
1407 insns_left = MIN(0xffff, cpu->icount_budget);
Richard Henderson5e140192019-03-28 11:54:23 -10001408 cpu_neg(cpu)->icount_decr.u16.low = insns_left;
Alex Bennéeeda5f7c2017-04-05 12:35:48 +01001409 cpu->icount_extra = cpu->icount_budget - insns_left;
Alex Bennéed759c952018-02-27 12:52:48 +03001410
1411 replay_mutex_lock();
Alex Bennée05248382017-03-29 16:46:59 +01001412 }
1413}
1414
1415static void process_icount_data(CPUState *cpu)
1416{
1417 if (use_icount) {
Alex Bennéee4cd9652017-03-31 16:09:42 +01001418 /* Account for executed instructions */
Alex Bennée512d3c82017-04-05 12:32:37 +01001419 cpu_update_icount(cpu);
Alex Bennée05248382017-03-29 16:46:59 +01001420
1421 /* Reset the counters */
Richard Henderson5e140192019-03-28 11:54:23 -10001422 cpu_neg(cpu)->icount_decr.u16.low = 0;
Alex Bennée05248382017-03-29 16:46:59 +01001423 cpu->icount_extra = 0;
Alex Bennéee4cd9652017-03-31 16:09:42 +01001424 cpu->icount_budget = 0;
1425
Alex Bennée05248382017-03-29 16:46:59 +01001426 replay_account_executed_instructions();
Alex Bennéed759c952018-02-27 12:52:48 +03001427
1428 replay_mutex_unlock();
Alex Bennée05248382017-03-29 16:46:59 +01001429 }
1430}
1431
1432
Alex Bennée1be7fcb2016-10-27 16:10:08 +01001433static int tcg_cpu_exec(CPUState *cpu)
1434{
1435 int ret;
1436#ifdef CONFIG_PROFILER
1437 int64_t ti;
1438#endif
1439
Emilio G. Cotaf28d0df2018-06-22 13:45:31 -04001440 assert(tcg_enabled());
Alex Bennée1be7fcb2016-10-27 16:10:08 +01001441#ifdef CONFIG_PROFILER
1442 ti = profile_getclock();
1443#endif
Alex Bennée1be7fcb2016-10-27 16:10:08 +01001444 cpu_exec_start(cpu);
1445 ret = cpu_exec(cpu);
1446 cpu_exec_end(cpu);
1447#ifdef CONFIG_PROFILER
Emilio G. Cota72fd2ef2018-10-10 10:48:53 -04001448 atomic_set(&tcg_ctx->prof.cpu_exec_time,
1449 tcg_ctx->prof.cpu_exec_time + profile_getclock() - ti);
Alex Bennée1be7fcb2016-10-27 16:10:08 +01001450#endif
Alex Bennée1be7fcb2016-10-27 16:10:08 +01001451 return ret;
1452}
1453
Alex Bennéec93bbbe2016-10-27 16:10:09 +01001454/* Destroy any remaining vCPUs which have been unplugged and have
1455 * finished running
1456 */
1457static void deal_with_unplugged_cpus(void)
Alex Bennée1be7fcb2016-10-27 16:10:08 +01001458{
Alex Bennéec93bbbe2016-10-27 16:10:09 +01001459 CPUState *cpu;
Alex Bennée1be7fcb2016-10-27 16:10:08 +01001460
Alex Bennéec93bbbe2016-10-27 16:10:09 +01001461 CPU_FOREACH(cpu) {
1462 if (cpu->unplug && !cpu_can_run(cpu)) {
1463 qemu_tcg_destroy_vcpu(cpu);
1464 cpu->created = false;
1465 qemu_cond_signal(&qemu_cpu_cond);
Alex Bennée1be7fcb2016-10-27 16:10:08 +01001466 break;
1467 }
1468 }
Alex Bennée1be7fcb2016-10-27 16:10:08 +01001469}
Jan Kiszkabdb7ca62011-09-26 09:40:39 +02001470
Alex Bennée65467062017-02-23 18:29:09 +00001471/* Single-threaded TCG
1472 *
1473 * In the single-threaded case each vCPU is simulated in turn. If
1474 * there is more than a single vCPU we create a simple timer to kick
1475 * the vCPU and ensure we don't get stuck in a tight loop in one vCPU.
1476 * This is done explicitly rather than relying on side-effects
1477 * elsewhere.
1478 */
1479
Alex Bennée37257942017-02-23 18:29:14 +00001480static void *qemu_tcg_rr_cpu_thread_fn(void *arg)
Blue Swirl296af7c2010-03-29 19:23:50 +00001481{
Andreas Färberc3586ba2012-05-03 01:41:24 +02001482 CPUState *cpu = arg;
Blue Swirl296af7c2010-03-29 19:23:50 +00001483
Emilio G. Cotaf28d0df2018-06-22 13:45:31 -04001484 assert(tcg_enabled());
Paolo Bonziniab28bd22015-07-09 08:55:38 +02001485 rcu_register_thread();
Emilio G. Cota3468b592017-07-19 18:57:58 -04001486 tcg_register_thread();
Paolo Bonziniab28bd22015-07-09 08:55:38 +02001487
Paolo Bonzini2e7f7a32015-06-18 18:47:18 +02001488 qemu_mutex_lock_iothread();
Andreas Färber814e6122012-05-02 17:00:37 +02001489 qemu_thread_get_self(cpu->thread);
Blue Swirl296af7c2010-03-29 19:23:50 +00001490
David Hildenbrand5a9c9732018-02-09 20:52:39 +01001491 cpu->thread_id = qemu_get_thread_id();
1492 cpu->created = true;
1493 cpu->can_do_io = 1;
Blue Swirl296af7c2010-03-29 19:23:50 +00001494 qemu_cond_signal(&qemu_cpu_cond);
Richard Henderson9c09a252019-03-14 13:06:29 -07001495 qemu_guest_random_seed_thread_part2(cpu->random_seed);
Blue Swirl296af7c2010-03-29 19:23:50 +00001496
Jan Kiszkafa7d1862011-08-22 18:35:25 +02001497 /* wait for initial kick-off after machine start */
Emilio G. Cotac28e3992015-04-27 12:45:28 -04001498 while (first_cpu->stopped) {
KONRAD Fredericd5f8d612015-08-10 17:27:06 +02001499 qemu_cond_wait(first_cpu->halt_cond, &qemu_global_mutex);
Jan Kiszka8e564b42012-02-17 18:31:15 +01001500
1501 /* process any pending work */
Andreas Färberbdc44642013-06-24 23:50:24 +02001502 CPU_FOREACH(cpu) {
Alex Bennée37257942017-02-23 18:29:14 +00001503 current_cpu = cpu;
Andreas Färber182735e2013-05-29 22:29:20 +02001504 qemu_wait_io_event_common(cpu);
Jan Kiszka8e564b42012-02-17 18:31:15 +01001505 }
Jan Kiszka0ab07c62011-02-07 12:19:14 +01001506 }
Blue Swirl296af7c2010-03-29 19:23:50 +00001507
Alex Bennée65467062017-02-23 18:29:09 +00001508 start_tcg_kick_timer();
1509
Alex Bennéec93bbbe2016-10-27 16:10:09 +01001510 cpu = first_cpu;
1511
Alex Bennéee5143e32017-02-23 18:29:12 +00001512 /* process any pending work */
1513 cpu->exit_request = 1;
1514
Blue Swirl296af7c2010-03-29 19:23:50 +00001515 while (1) {
Alex Bennéed759c952018-02-27 12:52:48 +03001516 qemu_mutex_unlock_iothread();
1517 replay_mutex_lock();
1518 qemu_mutex_lock_iothread();
Alex Bennéec93bbbe2016-10-27 16:10:09 +01001519 /* Account partial waits to QEMU_CLOCK_VIRTUAL. */
1520 qemu_account_warp_timer();
1521
Paolo Bonzini6b8f0182017-03-02 19:56:40 +01001522 /* Run the timers here. This is much more efficient than
1523 * waking up the I/O thread and waiting for completion.
1524 */
1525 handle_icount_deadline();
1526
Alex Bennéed759c952018-02-27 12:52:48 +03001527 replay_mutex_unlock();
1528
Alex Bennéec93bbbe2016-10-27 16:10:09 +01001529 if (!cpu) {
1530 cpu = first_cpu;
1531 }
1532
Alex Bennéee5143e32017-02-23 18:29:12 +00001533 while (cpu && !cpu->queued_work_first && !cpu->exit_request) {
1534
Alex Bennée791158d2017-02-23 18:29:10 +00001535 atomic_mb_set(&tcg_current_rr_cpu, cpu);
Alex Bennée37257942017-02-23 18:29:14 +00001536 current_cpu = cpu;
Alex Bennéec93bbbe2016-10-27 16:10:09 +01001537
1538 qemu_clock_enable(QEMU_CLOCK_VIRTUAL,
1539 (cpu->singlestep_enabled & SSTEP_NOTIMER) == 0);
1540
1541 if (cpu_can_run(cpu)) {
1542 int r;
Alex Bennée05248382017-03-29 16:46:59 +01001543
Alex Bennéed759c952018-02-27 12:52:48 +03001544 qemu_mutex_unlock_iothread();
Alex Bennée05248382017-03-29 16:46:59 +01001545 prepare_icount_for_run(cpu);
1546
Alex Bennéec93bbbe2016-10-27 16:10:09 +01001547 r = tcg_cpu_exec(cpu);
Alex Bennée05248382017-03-29 16:46:59 +01001548
1549 process_icount_data(cpu);
Alex Bennéed759c952018-02-27 12:52:48 +03001550 qemu_mutex_lock_iothread();
Alex Bennée05248382017-03-29 16:46:59 +01001551
Alex Bennéec93bbbe2016-10-27 16:10:09 +01001552 if (r == EXCP_DEBUG) {
1553 cpu_handle_guest_debug(cpu);
1554 break;
Pranith Kumar08e73c42017-02-23 18:29:15 +00001555 } else if (r == EXCP_ATOMIC) {
1556 qemu_mutex_unlock_iothread();
1557 cpu_exec_step_atomic(cpu);
1558 qemu_mutex_lock_iothread();
1559 break;
Alex Bennéec93bbbe2016-10-27 16:10:09 +01001560 }
Alex Bennée37257942017-02-23 18:29:14 +00001561 } else if (cpu->stop) {
Alex Bennéec93bbbe2016-10-27 16:10:09 +01001562 if (cpu->unplug) {
1563 cpu = CPU_NEXT(cpu);
1564 }
1565 break;
1566 }
1567
Alex Bennéee5143e32017-02-23 18:29:12 +00001568 cpu = CPU_NEXT(cpu);
1569 } /* while (cpu && !cpu->exit_request).. */
1570
Alex Bennée791158d2017-02-23 18:29:10 +00001571 /* Does not need atomic_mb_set because a spurious wakeup is okay. */
1572 atomic_set(&tcg_current_rr_cpu, NULL);
Alex Bennéec93bbbe2016-10-27 16:10:09 +01001573
Alex Bennéee5143e32017-02-23 18:29:12 +00001574 if (cpu && cpu->exit_request) {
1575 atomic_mb_set(&cpu->exit_request, 0);
1576 }
Alex Blighac70aaf2013-08-21 16:02:57 +01001577
Clement Deschamps013aabd2018-10-21 16:21:03 +02001578 if (use_icount && all_cpu_threads_idle()) {
1579 /*
1580 * When all cpus are sleeping (e.g in WFI), to avoid a deadlock
1581 * in the main_loop, wake it up in order to start the warp timer.
1582 */
1583 qemu_notify_event();
1584 }
1585
Paolo Bonzinia8efa602018-11-14 12:36:57 +01001586 qemu_tcg_rr_wait_io_event();
Alex Bennéec93bbbe2016-10-27 16:10:09 +01001587 deal_with_unplugged_cpus();
Blue Swirl296af7c2010-03-29 19:23:50 +00001588 }
1589
Paolo Bonzini9b0605f2018-01-30 11:05:06 -05001590 rcu_unregister_thread();
Blue Swirl296af7c2010-03-29 19:23:50 +00001591 return NULL;
1592}
1593
Vincent Palatinb0cb0a62017-01-10 11:59:57 +01001594static void *qemu_hax_cpu_thread_fn(void *arg)
1595{
1596 CPUState *cpu = arg;
1597 int r;
Vincent Palatinb3d3a422017-03-20 11:15:49 +01001598
Paolo Bonzini9857c2d2018-01-30 16:28:49 +01001599 rcu_register_thread();
Vincent Palatinb3d3a422017-03-20 11:15:49 +01001600 qemu_mutex_lock_iothread();
Vincent Palatinb0cb0a62017-01-10 11:59:57 +01001601 qemu_thread_get_self(cpu->thread);
Vincent Palatinb0cb0a62017-01-10 11:59:57 +01001602
1603 cpu->thread_id = qemu_get_thread_id();
1604 cpu->created = true;
Vincent Palatinb0cb0a62017-01-10 11:59:57 +01001605 current_cpu = cpu;
1606
1607 hax_init_vcpu(cpu);
1608 qemu_cond_signal(&qemu_cpu_cond);
Richard Henderson9c09a252019-03-14 13:06:29 -07001609 qemu_guest_random_seed_thread_part2(cpu->random_seed);
Vincent Palatinb0cb0a62017-01-10 11:59:57 +01001610
Paolo Bonzini9857c2d2018-01-30 16:28:49 +01001611 do {
Vincent Palatinb0cb0a62017-01-10 11:59:57 +01001612 if (cpu_can_run(cpu)) {
1613 r = hax_smp_cpu_exec(cpu);
1614 if (r == EXCP_DEBUG) {
1615 cpu_handle_guest_debug(cpu);
1616 }
1617 }
1618
Paolo Bonzinidb08b682018-01-11 13:53:12 +01001619 qemu_wait_io_event(cpu);
Paolo Bonzini9857c2d2018-01-30 16:28:49 +01001620 } while (!cpu->unplug || cpu_can_run(cpu));
1621 rcu_unregister_thread();
Vincent Palatinb0cb0a62017-01-10 11:59:57 +01001622 return NULL;
1623}
1624
Sergio Andres Gomez Del Realc97d6d22017-09-13 04:05:09 -05001625/* The HVF-specific vCPU thread function. This one should only run when the host
1626 * CPU supports the VMX "unrestricted guest" feature. */
1627static void *qemu_hvf_cpu_thread_fn(void *arg)
1628{
1629 CPUState *cpu = arg;
1630
1631 int r;
1632
1633 assert(hvf_enabled());
1634
1635 rcu_register_thread();
1636
1637 qemu_mutex_lock_iothread();
1638 qemu_thread_get_self(cpu->thread);
1639
1640 cpu->thread_id = qemu_get_thread_id();
1641 cpu->can_do_io = 1;
1642 current_cpu = cpu;
1643
1644 hvf_init_vcpu(cpu);
1645
1646 /* signal CPU creation */
1647 cpu->created = true;
1648 qemu_cond_signal(&qemu_cpu_cond);
Richard Henderson9c09a252019-03-14 13:06:29 -07001649 qemu_guest_random_seed_thread_part2(cpu->random_seed);
Sergio Andres Gomez Del Realc97d6d22017-09-13 04:05:09 -05001650
1651 do {
1652 if (cpu_can_run(cpu)) {
1653 r = hvf_vcpu_exec(cpu);
1654 if (r == EXCP_DEBUG) {
1655 cpu_handle_guest_debug(cpu);
1656 }
1657 }
Paolo Bonzinidb08b682018-01-11 13:53:12 +01001658 qemu_wait_io_event(cpu);
Sergio Andres Gomez Del Realc97d6d22017-09-13 04:05:09 -05001659 } while (!cpu->unplug || cpu_can_run(cpu));
1660
1661 hvf_vcpu_destroy(cpu);
1662 cpu->created = false;
1663 qemu_cond_signal(&qemu_cpu_cond);
1664 qemu_mutex_unlock_iothread();
Paolo Bonzini8178e632018-01-30 11:05:21 -05001665 rcu_unregister_thread();
Sergio Andres Gomez Del Realc97d6d22017-09-13 04:05:09 -05001666 return NULL;
1667}
1668
Justin Terry (VM)19306802018-01-22 13:07:49 -08001669static void *qemu_whpx_cpu_thread_fn(void *arg)
1670{
1671 CPUState *cpu = arg;
1672 int r;
1673
1674 rcu_register_thread();
1675
1676 qemu_mutex_lock_iothread();
1677 qemu_thread_get_self(cpu->thread);
1678 cpu->thread_id = qemu_get_thread_id();
1679 current_cpu = cpu;
1680
1681 r = whpx_init_vcpu(cpu);
1682 if (r < 0) {
1683 fprintf(stderr, "whpx_init_vcpu failed: %s\n", strerror(-r));
1684 exit(1);
1685 }
1686
1687 /* signal CPU creation */
1688 cpu->created = true;
1689 qemu_cond_signal(&qemu_cpu_cond);
Richard Henderson9c09a252019-03-14 13:06:29 -07001690 qemu_guest_random_seed_thread_part2(cpu->random_seed);
Justin Terry (VM)19306802018-01-22 13:07:49 -08001691
1692 do {
1693 if (cpu_can_run(cpu)) {
1694 r = whpx_vcpu_exec(cpu);
1695 if (r == EXCP_DEBUG) {
1696 cpu_handle_guest_debug(cpu);
1697 }
1698 }
1699 while (cpu_thread_is_idle(cpu)) {
1700 qemu_cond_wait(cpu->halt_cond, &qemu_global_mutex);
1701 }
1702 qemu_wait_io_event_common(cpu);
1703 } while (!cpu->unplug || cpu_can_run(cpu));
1704
1705 whpx_destroy_vcpu(cpu);
1706 cpu->created = false;
1707 qemu_cond_signal(&qemu_cpu_cond);
1708 qemu_mutex_unlock_iothread();
1709 rcu_unregister_thread();
Vincent Palatinb0cb0a62017-01-10 11:59:57 +01001710 return NULL;
1711}
1712
1713#ifdef _WIN32
1714static void CALLBACK dummy_apc_func(ULONG_PTR unused)
1715{
1716}
1717#endif
1718
Alex Bennée37257942017-02-23 18:29:14 +00001719/* Multi-threaded TCG
1720 *
1721 * In the multi-threaded case each vCPU has its own thread. The TLS
1722 * variable current_cpu can be used deep in the code to find the
1723 * current CPUState for a given thread.
1724 */
1725
1726static void *qemu_tcg_cpu_thread_fn(void *arg)
1727{
1728 CPUState *cpu = arg;
1729
Emilio G. Cotaf28d0df2018-06-22 13:45:31 -04001730 assert(tcg_enabled());
Alex Bennéebf51c722017-03-30 18:32:29 +01001731 g_assert(!use_icount);
1732
Alex Bennée37257942017-02-23 18:29:14 +00001733 rcu_register_thread();
Emilio G. Cota3468b592017-07-19 18:57:58 -04001734 tcg_register_thread();
Alex Bennée37257942017-02-23 18:29:14 +00001735
1736 qemu_mutex_lock_iothread();
1737 qemu_thread_get_self(cpu->thread);
1738
1739 cpu->thread_id = qemu_get_thread_id();
1740 cpu->created = true;
1741 cpu->can_do_io = 1;
1742 current_cpu = cpu;
1743 qemu_cond_signal(&qemu_cpu_cond);
Richard Henderson9c09a252019-03-14 13:06:29 -07001744 qemu_guest_random_seed_thread_part2(cpu->random_seed);
Alex Bennée37257942017-02-23 18:29:14 +00001745
1746 /* process any pending work */
1747 cpu->exit_request = 1;
1748
Cédric Le Goater54961aa2018-04-25 15:18:28 +02001749 do {
Alex Bennée37257942017-02-23 18:29:14 +00001750 if (cpu_can_run(cpu)) {
1751 int r;
Alex Bennéed759c952018-02-27 12:52:48 +03001752 qemu_mutex_unlock_iothread();
Alex Bennée37257942017-02-23 18:29:14 +00001753 r = tcg_cpu_exec(cpu);
Alex Bennéed759c952018-02-27 12:52:48 +03001754 qemu_mutex_lock_iothread();
Alex Bennée37257942017-02-23 18:29:14 +00001755 switch (r) {
1756 case EXCP_DEBUG:
1757 cpu_handle_guest_debug(cpu);
1758 break;
1759 case EXCP_HALTED:
1760 /* during start-up the vCPU is reset and the thread is
1761 * kicked several times. If we don't ensure we go back
1762 * to sleep in the halted state we won't cleanly
1763 * start-up when the vCPU is enabled.
1764 *
1765 * cpu->halted should ensure we sleep in wait_io_event
1766 */
1767 g_assert(cpu->halted);
1768 break;
Pranith Kumar08e73c42017-02-23 18:29:15 +00001769 case EXCP_ATOMIC:
1770 qemu_mutex_unlock_iothread();
1771 cpu_exec_step_atomic(cpu);
1772 qemu_mutex_lock_iothread();
Alex Bennée37257942017-02-23 18:29:14 +00001773 default:
1774 /* Ignore everything else? */
1775 break;
1776 }
1777 }
1778
Alex Bennée37257942017-02-23 18:29:14 +00001779 atomic_mb_set(&cpu->exit_request, 0);
Paolo Bonzinidb08b682018-01-11 13:53:12 +01001780 qemu_wait_io_event(cpu);
Paolo Bonzini9b0605f2018-01-30 11:05:06 -05001781 } while (!cpu->unplug || cpu_can_run(cpu));
Alex Bennée37257942017-02-23 18:29:14 +00001782
Paolo Bonzini9b0605f2018-01-30 11:05:06 -05001783 qemu_tcg_destroy_vcpu(cpu);
1784 cpu->created = false;
1785 qemu_cond_signal(&qemu_cpu_cond);
1786 qemu_mutex_unlock_iothread();
1787 rcu_unregister_thread();
Alex Bennée37257942017-02-23 18:29:14 +00001788 return NULL;
1789}
1790
Andreas Färber2ff09a42012-05-03 00:23:30 +02001791static void qemu_cpu_kick_thread(CPUState *cpu)
Paolo Bonzinicc015e92011-03-12 17:44:08 +01001792{
1793#ifndef _WIN32
1794 int err;
1795
Paolo Bonzinie0c38212015-08-26 00:19:19 +02001796 if (cpu->thread_kicked) {
1797 return;
Paolo Bonzini9102ded2015-08-18 06:52:09 -07001798 }
Paolo Bonzinie0c38212015-08-26 00:19:19 +02001799 cpu->thread_kicked = true;
Andreas Färber814e6122012-05-02 17:00:37 +02001800 err = pthread_kill(cpu->thread->thread, SIG_IPI);
Laurent Vivierd455ebc2019-01-02 15:16:03 +01001801 if (err && err != ESRCH) {
Paolo Bonzinicc015e92011-03-12 17:44:08 +01001802 fprintf(stderr, "qemu:%s: %s", __func__, strerror(err));
1803 exit(1);
1804 }
1805#else /* _WIN32 */
Vincent Palatinb0cb0a62017-01-10 11:59:57 +01001806 if (!qemu_cpu_is_self(cpu)) {
Justin Terry (VM)19306802018-01-22 13:07:49 -08001807 if (whpx_enabled()) {
1808 whpx_vcpu_kick(cpu);
1809 } else if (!QueueUserAPC(dummy_apc_func, cpu->hThread, 0)) {
Vincent Palatinb0cb0a62017-01-10 11:59:57 +01001810 fprintf(stderr, "%s: QueueUserAPC failed with error %lu\n",
1811 __func__, GetLastError());
1812 exit(1);
1813 }
1814 }
Paolo Bonzinicc015e92011-03-12 17:44:08 +01001815#endif
1816}
1817
Andreas Färberc08d7422012-05-03 04:34:15 +02001818void qemu_cpu_kick(CPUState *cpu)
Blue Swirl296af7c2010-03-29 19:23:50 +00001819{
Andreas Färberf5c121b2012-05-03 01:22:49 +02001820 qemu_cond_broadcast(cpu->halt_cond);
Paolo Bonzinie0c38212015-08-26 00:19:19 +02001821 if (tcg_enabled()) {
Alex Bennée791158d2017-02-23 18:29:10 +00001822 cpu_exit(cpu);
Alex Bennée37257942017-02-23 18:29:14 +00001823 /* NOP unless doing single-thread RR */
Alex Bennée791158d2017-02-23 18:29:10 +00001824 qemu_cpu_kick_rr_cpu();
Paolo Bonzinie0c38212015-08-26 00:19:19 +02001825 } else {
Vincent Palatinb0cb0a62017-01-10 11:59:57 +01001826 if (hax_enabled()) {
1827 /*
1828 * FIXME: race condition with the exit_request check in
1829 * hax_vcpu_hax_exec
1830 */
1831 cpu->exit_request = 1;
1832 }
Paolo Bonzinie0c38212015-08-26 00:19:19 +02001833 qemu_cpu_kick_thread(cpu);
1834 }
Blue Swirl296af7c2010-03-29 19:23:50 +00001835}
1836
Jan Kiszka46d62fa2011-02-01 22:15:59 +01001837void qemu_cpu_kick_self(void)
1838{
Andreas Färber4917cf42013-05-27 05:17:50 +02001839 assert(current_cpu);
Paolo Bonzini9102ded2015-08-18 06:52:09 -07001840 qemu_cpu_kick_thread(current_cpu);
Blue Swirl296af7c2010-03-29 19:23:50 +00001841}
1842
Andreas Färber60e82572012-05-02 22:23:49 +02001843bool qemu_cpu_is_self(CPUState *cpu)
Blue Swirl296af7c2010-03-29 19:23:50 +00001844{
Andreas Färber814e6122012-05-02 17:00:37 +02001845 return qemu_thread_is_self(cpu->thread);
Blue Swirl296af7c2010-03-29 19:23:50 +00001846}
1847
Paolo Bonzini79e2b9a2015-01-21 12:09:14 +01001848bool qemu_in_vcpu_thread(void)
Juan Quintelaaa723c22012-09-18 16:30:11 +02001849{
Andreas Färber4917cf42013-05-27 05:17:50 +02001850 return current_cpu && qemu_cpu_is_self(current_cpu);
Juan Quintelaaa723c22012-09-18 16:30:11 +02001851}
1852
Paolo Bonziniafbe7052015-06-18 18:47:19 +02001853static __thread bool iothread_locked = false;
1854
1855bool qemu_mutex_iothread_locked(void)
1856{
1857 return iothread_locked;
1858}
1859
Emilio G. Cotacb764d02017-10-28 02:16:41 -04001860/*
1861 * The BQL is taken from so many places that it is worth profiling the
1862 * callers directly, instead of funneling them all through a single function.
1863 */
1864void qemu_mutex_lock_iothread_impl(const char *file, int line)
Blue Swirl296af7c2010-03-29 19:23:50 +00001865{
Emilio G. Cotacb764d02017-10-28 02:16:41 -04001866 QemuMutexLockFunc bql_lock = atomic_read(&qemu_bql_mutex_lock_func);
1867
Jan Kiszka8d04fb52017-02-23 18:29:11 +00001868 g_assert(!qemu_mutex_iothread_locked());
Emilio G. Cotacb764d02017-10-28 02:16:41 -04001869 bql_lock(&qemu_global_mutex, file, line);
Paolo Bonziniafbe7052015-06-18 18:47:19 +02001870 iothread_locked = true;
Blue Swirl296af7c2010-03-29 19:23:50 +00001871}
1872
1873void qemu_mutex_unlock_iothread(void)
1874{
Jan Kiszka8d04fb52017-02-23 18:29:11 +00001875 g_assert(qemu_mutex_iothread_locked());
Paolo Bonziniafbe7052015-06-18 18:47:19 +02001876 iothread_locked = false;
Blue Swirl296af7c2010-03-29 19:23:50 +00001877 qemu_mutex_unlock(&qemu_global_mutex);
1878}
1879
Alex Bennéee8faee02016-10-27 16:09:58 +01001880static bool all_vcpus_paused(void)
Blue Swirl296af7c2010-03-29 19:23:50 +00001881{
Andreas Färberbdc44642013-06-24 23:50:24 +02001882 CPUState *cpu;
Blue Swirl296af7c2010-03-29 19:23:50 +00001883
Andreas Färberbdc44642013-06-24 23:50:24 +02001884 CPU_FOREACH(cpu) {
Andreas Färber182735e2013-05-29 22:29:20 +02001885 if (!cpu->stopped) {
Alex Bennéee8faee02016-10-27 16:09:58 +01001886 return false;
Jan Kiszka0ab07c62011-02-07 12:19:14 +01001887 }
Blue Swirl296af7c2010-03-29 19:23:50 +00001888 }
1889
Alex Bennéee8faee02016-10-27 16:09:58 +01001890 return true;
Blue Swirl296af7c2010-03-29 19:23:50 +00001891}
1892
1893void pause_all_vcpus(void)
1894{
Andreas Färberbdc44642013-06-24 23:50:24 +02001895 CPUState *cpu;
Blue Swirl296af7c2010-03-29 19:23:50 +00001896
Alex Bligh40daca52013-08-21 16:03:02 +01001897 qemu_clock_enable(QEMU_CLOCK_VIRTUAL, false);
Andreas Färberbdc44642013-06-24 23:50:24 +02001898 CPU_FOREACH(cpu) {
David Hildenbrandebd05fe2017-11-29 20:12:15 +01001899 if (qemu_cpu_is_self(cpu)) {
1900 qemu_cpu_stop(cpu, true);
1901 } else {
1902 cpu->stop = true;
1903 qemu_cpu_kick(cpu);
1904 }
Jan Kiszkad798e972012-02-17 18:31:16 +01001905 }
1906
Alex Bennéed759c952018-02-27 12:52:48 +03001907 /* We need to drop the replay_lock so any vCPU threads woken up
1908 * can finish their replay tasks
1909 */
1910 replay_mutex_unlock();
1911
Blue Swirl296af7c2010-03-29 19:23:50 +00001912 while (!all_vcpus_paused()) {
Paolo Bonzinibe7d6c52011-03-12 17:44:02 +01001913 qemu_cond_wait(&qemu_pause_cond, &qemu_global_mutex);
Andreas Färberbdc44642013-06-24 23:50:24 +02001914 CPU_FOREACH(cpu) {
Andreas Färber182735e2013-05-29 22:29:20 +02001915 qemu_cpu_kick(cpu);
Blue Swirl296af7c2010-03-29 19:23:50 +00001916 }
1917 }
Alex Bennéed759c952018-02-27 12:52:48 +03001918
1919 qemu_mutex_unlock_iothread();
1920 replay_mutex_lock();
1921 qemu_mutex_lock_iothread();
Blue Swirl296af7c2010-03-29 19:23:50 +00001922}
1923
Igor Mammedov29936832013-04-23 10:29:37 +02001924void cpu_resume(CPUState *cpu)
1925{
1926 cpu->stop = false;
1927 cpu->stopped = false;
1928 qemu_cpu_kick(cpu);
1929}
1930
Blue Swirl296af7c2010-03-29 19:23:50 +00001931void resume_all_vcpus(void)
1932{
Andreas Färberbdc44642013-06-24 23:50:24 +02001933 CPUState *cpu;
Blue Swirl296af7c2010-03-29 19:23:50 +00001934
Alex Bligh40daca52013-08-21 16:03:02 +01001935 qemu_clock_enable(QEMU_CLOCK_VIRTUAL, true);
Andreas Färberbdc44642013-06-24 23:50:24 +02001936 CPU_FOREACH(cpu) {
Andreas Färber182735e2013-05-29 22:29:20 +02001937 cpu_resume(cpu);
Blue Swirl296af7c2010-03-29 19:23:50 +00001938 }
1939}
1940
Paolo Bonzinidbadee42018-01-30 16:40:12 +01001941void cpu_remove_sync(CPUState *cpu)
Gu Zheng4c055ab2016-05-12 09:18:13 +05301942{
1943 cpu->stop = true;
1944 cpu->unplug = true;
1945 qemu_cpu_kick(cpu);
Paolo Bonzinidbadee42018-01-30 16:40:12 +01001946 qemu_mutex_unlock_iothread();
1947 qemu_thread_join(cpu->thread);
1948 qemu_mutex_lock_iothread();
Bharata B Rao2c579042016-05-12 09:18:14 +05301949}
1950
Dr. David Alan Gilbert49001162014-01-30 10:20:32 +00001951/* For temporary buffers for forming a name */
1952#define VCPU_THREAD_NAME_SIZE 16
1953
Andreas Färbere5ab30a2012-05-03 01:50:44 +02001954static void qemu_tcg_init_vcpu(CPUState *cpu)
Blue Swirl296af7c2010-03-29 19:23:50 +00001955{
Dr. David Alan Gilbert49001162014-01-30 10:20:32 +00001956 char thread_name[VCPU_THREAD_NAME_SIZE];
Alex Bennée37257942017-02-23 18:29:14 +00001957 static QemuCond *single_tcg_halt_cond;
1958 static QemuThread *single_tcg_cpu_thread;
Emilio G. Cotae8feb962017-07-07 19:24:20 -04001959 static int tcg_region_inited;
1960
Emilio G. Cotaf28d0df2018-06-22 13:45:31 -04001961 assert(tcg_enabled());
Emilio G. Cotae8feb962017-07-07 19:24:20 -04001962 /*
1963 * Initialize TCG regions--once. Now is a good time, because:
1964 * (1) TCG's init context, prologue and target globals have been set up.
1965 * (2) qemu_tcg_mttcg_enabled() works now (TCG init code runs before the
1966 * -accel flag is processed, so the check doesn't work then).
1967 */
1968 if (!tcg_region_inited) {
1969 tcg_region_inited = 1;
1970 tcg_region_init();
1971 }
Dr. David Alan Gilbert49001162014-01-30 10:20:32 +00001972
Alex Bennée37257942017-02-23 18:29:14 +00001973 if (qemu_tcg_mttcg_enabled() || !single_tcg_cpu_thread) {
Andreas Färber814e6122012-05-02 17:00:37 +02001974 cpu->thread = g_malloc0(sizeof(QemuThread));
Andreas Färberf5c121b2012-05-03 01:22:49 +02001975 cpu->halt_cond = g_malloc0(sizeof(QemuCond));
1976 qemu_cond_init(cpu->halt_cond);
Alex Bennée37257942017-02-23 18:29:14 +00001977
1978 if (qemu_tcg_mttcg_enabled()) {
1979 /* create a thread per vCPU with TCG (MTTCG) */
1980 parallel_cpus = true;
1981 snprintf(thread_name, VCPU_THREAD_NAME_SIZE, "CPU %d/TCG",
Dr. David Alan Gilbert49001162014-01-30 10:20:32 +00001982 cpu->cpu_index);
Alex Bennée37257942017-02-23 18:29:14 +00001983
1984 qemu_thread_create(cpu->thread, thread_name, qemu_tcg_cpu_thread_fn,
1985 cpu, QEMU_THREAD_JOINABLE);
1986
1987 } else {
1988 /* share a single thread for all cpus with TCG */
1989 snprintf(thread_name, VCPU_THREAD_NAME_SIZE, "ALL CPUs/TCG");
1990 qemu_thread_create(cpu->thread, thread_name,
1991 qemu_tcg_rr_cpu_thread_fn,
1992 cpu, QEMU_THREAD_JOINABLE);
1993
1994 single_tcg_halt_cond = cpu->halt_cond;
1995 single_tcg_cpu_thread = cpu->thread;
1996 }
Paolo Bonzini1ecf47b2011-12-13 13:43:52 +01001997#ifdef _WIN32
Andreas Färber814e6122012-05-02 17:00:37 +02001998 cpu->hThread = qemu_thread_get_handle(cpu->thread);
Paolo Bonzini1ecf47b2011-12-13 13:43:52 +01001999#endif
Blue Swirl296af7c2010-03-29 19:23:50 +00002000 } else {
Alex Bennée37257942017-02-23 18:29:14 +00002001 /* For non-MTTCG cases we share the thread */
2002 cpu->thread = single_tcg_cpu_thread;
2003 cpu->halt_cond = single_tcg_halt_cond;
David Hildenbranda3421732018-02-09 20:52:37 +01002004 cpu->thread_id = first_cpu->thread_id;
2005 cpu->can_do_io = 1;
2006 cpu->created = true;
Blue Swirl296af7c2010-03-29 19:23:50 +00002007 }
2008}
2009
Vincent Palatinb0cb0a62017-01-10 11:59:57 +01002010static void qemu_hax_start_vcpu(CPUState *cpu)
2011{
2012 char thread_name[VCPU_THREAD_NAME_SIZE];
2013
2014 cpu->thread = g_malloc0(sizeof(QemuThread));
2015 cpu->halt_cond = g_malloc0(sizeof(QemuCond));
2016 qemu_cond_init(cpu->halt_cond);
2017
2018 snprintf(thread_name, VCPU_THREAD_NAME_SIZE, "CPU %d/HAX",
2019 cpu->cpu_index);
2020 qemu_thread_create(cpu->thread, thread_name, qemu_hax_cpu_thread_fn,
2021 cpu, QEMU_THREAD_JOINABLE);
2022#ifdef _WIN32
2023 cpu->hThread = qemu_thread_get_handle(cpu->thread);
2024#endif
Vincent Palatinb0cb0a62017-01-10 11:59:57 +01002025}
2026
Andreas Färber48a106b2013-05-27 02:20:39 +02002027static void qemu_kvm_start_vcpu(CPUState *cpu)
Blue Swirl296af7c2010-03-29 19:23:50 +00002028{
Dr. David Alan Gilbert49001162014-01-30 10:20:32 +00002029 char thread_name[VCPU_THREAD_NAME_SIZE];
2030
Andreas Färber814e6122012-05-02 17:00:37 +02002031 cpu->thread = g_malloc0(sizeof(QemuThread));
Andreas Färberf5c121b2012-05-03 01:22:49 +02002032 cpu->halt_cond = g_malloc0(sizeof(QemuCond));
2033 qemu_cond_init(cpu->halt_cond);
Dr. David Alan Gilbert49001162014-01-30 10:20:32 +00002034 snprintf(thread_name, VCPU_THREAD_NAME_SIZE, "CPU %d/KVM",
2035 cpu->cpu_index);
2036 qemu_thread_create(cpu->thread, thread_name, qemu_kvm_cpu_thread_fn,
2037 cpu, QEMU_THREAD_JOINABLE);
Blue Swirl296af7c2010-03-29 19:23:50 +00002038}
2039
Sergio Andres Gomez Del Realc97d6d22017-09-13 04:05:09 -05002040static void qemu_hvf_start_vcpu(CPUState *cpu)
2041{
2042 char thread_name[VCPU_THREAD_NAME_SIZE];
2043
2044 /* HVF currently does not support TCG, and only runs in
2045 * unrestricted-guest mode. */
2046 assert(hvf_enabled());
2047
2048 cpu->thread = g_malloc0(sizeof(QemuThread));
2049 cpu->halt_cond = g_malloc0(sizeof(QemuCond));
2050 qemu_cond_init(cpu->halt_cond);
2051
2052 snprintf(thread_name, VCPU_THREAD_NAME_SIZE, "CPU %d/HVF",
2053 cpu->cpu_index);
2054 qemu_thread_create(cpu->thread, thread_name, qemu_hvf_cpu_thread_fn,
2055 cpu, QEMU_THREAD_JOINABLE);
Sergio Andres Gomez Del Realc97d6d22017-09-13 04:05:09 -05002056}
2057
Justin Terry (VM)19306802018-01-22 13:07:49 -08002058static void qemu_whpx_start_vcpu(CPUState *cpu)
2059{
2060 char thread_name[VCPU_THREAD_NAME_SIZE];
2061
2062 cpu->thread = g_malloc0(sizeof(QemuThread));
2063 cpu->halt_cond = g_malloc0(sizeof(QemuCond));
2064 qemu_cond_init(cpu->halt_cond);
2065 snprintf(thread_name, VCPU_THREAD_NAME_SIZE, "CPU %d/WHPX",
2066 cpu->cpu_index);
2067 qemu_thread_create(cpu->thread, thread_name, qemu_whpx_cpu_thread_fn,
2068 cpu, QEMU_THREAD_JOINABLE);
2069#ifdef _WIN32
2070 cpu->hThread = qemu_thread_get_handle(cpu->thread);
2071#endif
Justin Terry (VM)19306802018-01-22 13:07:49 -08002072}
2073
Andreas Färber10a90212013-05-27 02:24:35 +02002074static void qemu_dummy_start_vcpu(CPUState *cpu)
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +02002075{
Dr. David Alan Gilbert49001162014-01-30 10:20:32 +00002076 char thread_name[VCPU_THREAD_NAME_SIZE];
2077
Andreas Färber814e6122012-05-02 17:00:37 +02002078 cpu->thread = g_malloc0(sizeof(QemuThread));
Andreas Färberf5c121b2012-05-03 01:22:49 +02002079 cpu->halt_cond = g_malloc0(sizeof(QemuCond));
2080 qemu_cond_init(cpu->halt_cond);
Dr. David Alan Gilbert49001162014-01-30 10:20:32 +00002081 snprintf(thread_name, VCPU_THREAD_NAME_SIZE, "CPU %d/DUMMY",
2082 cpu->cpu_index);
2083 qemu_thread_create(cpu->thread, thread_name, qemu_dummy_cpu_thread_fn, cpu,
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +02002084 QEMU_THREAD_JOINABLE);
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +02002085}
2086
Andreas Färberc643bed2013-05-27 03:23:24 +02002087void qemu_init_vcpu(CPUState *cpu)
Blue Swirl296af7c2010-03-29 19:23:50 +00002088{
Like Xu5cc87672019-05-19 04:54:21 +08002089 MachineState *ms = MACHINE(qdev_get_machine());
2090
2091 cpu->nr_cores = ms->smp.cores;
2092 cpu->nr_threads = ms->smp.threads;
Andreas Färberf324e762012-05-02 23:26:21 +02002093 cpu->stopped = true;
Richard Henderson9c09a252019-03-14 13:06:29 -07002094 cpu->random_seed = qemu_guest_random_seed_thread_part1();
Peter Maydell56943e82016-01-21 14:15:04 +00002095
2096 if (!cpu->as) {
2097 /* If the target cpu hasn't set up any address spaces itself,
2098 * give it the default one.
2099 */
Peter Maydell12ebc9a2016-01-21 14:15:04 +00002100 cpu->num_ases = 1;
Peter Xu80ceb072017-11-23 17:23:32 +08002101 cpu_address_space_init(cpu, 0, "cpu-memory", cpu->memory);
Peter Maydell56943e82016-01-21 14:15:04 +00002102 }
2103
Jan Kiszka0ab07c62011-02-07 12:19:14 +01002104 if (kvm_enabled()) {
Andreas Färber48a106b2013-05-27 02:20:39 +02002105 qemu_kvm_start_vcpu(cpu);
Vincent Palatinb0cb0a62017-01-10 11:59:57 +01002106 } else if (hax_enabled()) {
2107 qemu_hax_start_vcpu(cpu);
Sergio Andres Gomez Del Realc97d6d22017-09-13 04:05:09 -05002108 } else if (hvf_enabled()) {
2109 qemu_hvf_start_vcpu(cpu);
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +02002110 } else if (tcg_enabled()) {
Andreas Färbere5ab30a2012-05-03 01:50:44 +02002111 qemu_tcg_init_vcpu(cpu);
Justin Terry (VM)19306802018-01-22 13:07:49 -08002112 } else if (whpx_enabled()) {
2113 qemu_whpx_start_vcpu(cpu);
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +02002114 } else {
Andreas Färber10a90212013-05-27 02:24:35 +02002115 qemu_dummy_start_vcpu(cpu);
Jan Kiszka0ab07c62011-02-07 12:19:14 +01002116 }
David Hildenbrand81e96312018-02-09 20:52:38 +01002117
2118 while (!cpu->created) {
2119 qemu_cond_wait(&qemu_cpu_cond, &qemu_global_mutex);
2120 }
Blue Swirl296af7c2010-03-29 19:23:50 +00002121}
2122
Jan Kiszkab4a3d962011-02-01 22:15:43 +01002123void cpu_stop_current(void)
Blue Swirl296af7c2010-03-29 19:23:50 +00002124{
Andreas Färber4917cf42013-05-27 05:17:50 +02002125 if (current_cpu) {
Peter Maydell0ec7e672019-01-07 15:23:47 +00002126 current_cpu->stop = true;
2127 cpu_exit(current_cpu);
Jan Kiszkab4a3d962011-02-01 22:15:43 +01002128 }
Blue Swirl296af7c2010-03-29 19:23:50 +00002129}
2130
Kevin Wolf56983462013-07-05 13:49:54 +02002131int vm_stop(RunState state)
Blue Swirl296af7c2010-03-29 19:23:50 +00002132{
Juan Quintelaaa723c22012-09-18 16:30:11 +02002133 if (qemu_in_vcpu_thread()) {
Paolo Bonzini74892d22014-06-05 14:53:58 +02002134 qemu_system_vmstop_request_prepare();
Luiz Capitulino1dfb4dd2011-07-29 14:26:33 -03002135 qemu_system_vmstop_request(state);
Blue Swirl296af7c2010-03-29 19:23:50 +00002136 /*
2137 * FIXME: should not return to device code in case
2138 * vm_stop() has been requested.
2139 */
Jan Kiszkab4a3d962011-02-01 22:15:43 +01002140 cpu_stop_current();
Kevin Wolf56983462013-07-05 13:49:54 +02002141 return 0;
Blue Swirl296af7c2010-03-29 19:23:50 +00002142 }
Kevin Wolf56983462013-07-05 13:49:54 +02002143
Stefan Hajnoczi4486e892018-03-07 14:42:05 +00002144 return do_vm_stop(state, true);
Blue Swirl296af7c2010-03-29 19:23:50 +00002145}
2146
Claudio Imbrenda2d76e822017-02-14 18:07:47 +01002147/**
2148 * Prepare for (re)starting the VM.
2149 * Returns -1 if the vCPUs are not to be restarted (e.g. if they are already
2150 * running or in case of an error condition), 0 otherwise.
2151 */
2152int vm_prepare_start(void)
2153{
2154 RunState requested;
Claudio Imbrenda2d76e822017-02-14 18:07:47 +01002155
2156 qemu_vmstop_requested(&requested);
2157 if (runstate_is_running() && requested == RUN_STATE__MAX) {
2158 return -1;
2159 }
2160
2161 /* Ensure that a STOP/RESUME pair of events is emitted if a
2162 * vmstop request was pending. The BLOCK_IO_ERROR event, for
2163 * example, according to documentation is always followed by
2164 * the STOP event.
2165 */
2166 if (runstate_is_running()) {
Peter Xu3ab72382018-08-15 21:37:37 +08002167 qapi_event_send_stop();
2168 qapi_event_send_resume();
Markus Armbrusterf0561582018-04-23 10:45:18 +02002169 return -1;
Claudio Imbrenda2d76e822017-02-14 18:07:47 +01002170 }
2171
2172 /* We are sending this now, but the CPUs will be resumed shortly later */
Peter Xu3ab72382018-08-15 21:37:37 +08002173 qapi_event_send_resume();
Markus Armbrusterf0561582018-04-23 10:45:18 +02002174
2175 replay_enable_events();
2176 cpu_enable_ticks();
2177 runstate_set(RUN_STATE_RUNNING);
2178 vm_state_notify(1, RUN_STATE_RUNNING);
2179 return 0;
Claudio Imbrenda2d76e822017-02-14 18:07:47 +01002180}
2181
2182void vm_start(void)
2183{
2184 if (!vm_prepare_start()) {
2185 resume_all_vcpus();
2186 }
2187}
2188
Luiz Capitulino8a9236f2011-10-14 11:18:09 -03002189/* does a state transition even if the VM is already stopped,
2190 current state is forgotten forever */
Kevin Wolf56983462013-07-05 13:49:54 +02002191int vm_stop_force_state(RunState state)
Luiz Capitulino8a9236f2011-10-14 11:18:09 -03002192{
2193 if (runstate_is_running()) {
Kevin Wolf56983462013-07-05 13:49:54 +02002194 return vm_stop(state);
Luiz Capitulino8a9236f2011-10-14 11:18:09 -03002195 } else {
2196 runstate_set(state);
Wen Congyangb2780d32015-11-20 17:34:38 +08002197
2198 bdrv_drain_all();
Kevin Wolf594a45c2013-07-18 14:52:19 +02002199 /* Make sure to return an error if the flush in a previous vm_stop()
2200 * failed. */
John Snow22af08e2016-09-22 21:45:51 -04002201 return bdrv_flush_all();
Luiz Capitulino8a9236f2011-10-14 11:18:09 -03002202 }
2203}
2204
Markus Armbruster04424282019-04-17 21:17:57 +02002205void list_cpus(const char *optarg)
Blue Swirl262353c2010-05-04 19:55:35 +00002206{
2207 /* XXX: implement xxx_cpu_list for targets that still miss it */
Peter Maydelle916cbf2012-09-05 17:41:08 -03002208#if defined(cpu_list)
Markus Armbruster04424282019-04-17 21:17:57 +02002209 cpu_list();
Blue Swirl262353c2010-05-04 19:55:35 +00002210#endif
2211}
Luiz Capitulinode0b36b2011-09-21 16:38:35 -03002212
Luiz Capitulino0cfd6a92011-11-22 16:32:37 -02002213void qmp_memsave(int64_t addr, int64_t size, const char *filename,
2214 bool has_cpu, int64_t cpu_index, Error **errp)
2215{
2216 FILE *f;
2217 uint32_t l;
Andreas Färber55e5c282012-12-17 06:18:02 +01002218 CPUState *cpu;
Luiz Capitulino0cfd6a92011-11-22 16:32:37 -02002219 uint8_t buf[1024];
Borislav Petkov0dc9daf2015-02-08 13:14:38 +01002220 int64_t orig_addr = addr, orig_size = size;
Luiz Capitulino0cfd6a92011-11-22 16:32:37 -02002221
2222 if (!has_cpu) {
2223 cpu_index = 0;
2224 }
2225
Andreas Färber151d1322013-02-15 15:41:49 +01002226 cpu = qemu_get_cpu(cpu_index);
2227 if (cpu == NULL) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01002228 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "cpu-index",
2229 "a CPU number");
Luiz Capitulino0cfd6a92011-11-22 16:32:37 -02002230 return;
2231 }
2232
2233 f = fopen(filename, "wb");
2234 if (!f) {
Luiz Capitulino618da852013-06-07 14:35:06 -04002235 error_setg_file_open(errp, errno, filename);
Luiz Capitulino0cfd6a92011-11-22 16:32:37 -02002236 return;
2237 }
2238
2239 while (size != 0) {
2240 l = sizeof(buf);
2241 if (l > size)
2242 l = size;
Aneesh Kumar K.V2f4d0f52013-10-01 21:49:30 +05302243 if (cpu_memory_rw_debug(cpu, addr, buf, l, 0) != 0) {
Borislav Petkov0dc9daf2015-02-08 13:14:38 +01002244 error_setg(errp, "Invalid addr 0x%016" PRIx64 "/size %" PRId64
2245 " specified", orig_addr, orig_size);
Aneesh Kumar K.V2f4d0f52013-10-01 21:49:30 +05302246 goto exit;
2247 }
Luiz Capitulino0cfd6a92011-11-22 16:32:37 -02002248 if (fwrite(buf, 1, l, f) != l) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01002249 error_setg(errp, QERR_IO_ERROR);
Luiz Capitulino0cfd6a92011-11-22 16:32:37 -02002250 goto exit;
2251 }
2252 addr += l;
2253 size -= l;
2254 }
2255
2256exit:
2257 fclose(f);
2258}
Luiz Capitulino6d3962b2011-11-22 17:26:46 -02002259
2260void qmp_pmemsave(int64_t addr, int64_t size, const char *filename,
2261 Error **errp)
2262{
2263 FILE *f;
2264 uint32_t l;
2265 uint8_t buf[1024];
2266
2267 f = fopen(filename, "wb");
2268 if (!f) {
Luiz Capitulino618da852013-06-07 14:35:06 -04002269 error_setg_file_open(errp, errno, filename);
Luiz Capitulino6d3962b2011-11-22 17:26:46 -02002270 return;
2271 }
2272
2273 while (size != 0) {
2274 l = sizeof(buf);
2275 if (l > size)
2276 l = size;
Stefan Weileb6282f2014-04-07 20:28:23 +02002277 cpu_physical_memory_read(addr, buf, l);
Luiz Capitulino6d3962b2011-11-22 17:26:46 -02002278 if (fwrite(buf, 1, l, f) != l) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01002279 error_setg(errp, QERR_IO_ERROR);
Luiz Capitulino6d3962b2011-11-22 17:26:46 -02002280 goto exit;
2281 }
2282 addr += l;
2283 size -= l;
2284 }
2285
2286exit:
2287 fclose(f);
2288}
Luiz Capitulinoab49ab52011-11-23 12:55:53 -02002289
2290void qmp_inject_nmi(Error **errp)
2291{
Alexey Kardashevskiy9cb805f2014-08-20 22:16:33 +10002292 nmi_monitor_handle(monitor_get_cpu_index(), errp);
Luiz Capitulinoab49ab52011-11-23 12:55:53 -02002293}
Sebastian Tanase27498be2014-07-25 11:56:33 +02002294
Markus Armbruster76c86612019-04-17 21:17:53 +02002295void dump_drift_info(void)
Sebastian Tanase27498be2014-07-25 11:56:33 +02002296{
2297 if (!use_icount) {
2298 return;
2299 }
2300
Markus Armbruster76c86612019-04-17 21:17:53 +02002301 qemu_printf("Host - Guest clock %"PRIi64" ms\n",
Sebastian Tanase27498be2014-07-25 11:56:33 +02002302 (cpu_get_clock() - cpu_get_icount())/SCALE_MS);
2303 if (icount_align_option) {
Markus Armbruster76c86612019-04-17 21:17:53 +02002304 qemu_printf("Max guest delay %"PRIi64" ms\n",
2305 -max_delay / SCALE_MS);
2306 qemu_printf("Max guest advance %"PRIi64" ms\n",
2307 max_advance / SCALE_MS);
Sebastian Tanase27498be2014-07-25 11:56:33 +02002308 } else {
Markus Armbruster76c86612019-04-17 21:17:53 +02002309 qemu_printf("Max guest delay NA\n");
2310 qemu_printf("Max guest advance NA\n");
Sebastian Tanase27498be2014-07-25 11:56:33 +02002311 }
2312}