blob: bee7209134d93ca28b3c48774d3d5b84a3e1ab5d [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
Yury Kotovbd1f7ff2019-09-09 16:13:34 +030080static QemuMutex qemu_global_mutex;
81
Sebastian Tanase27498be2014-07-25 11:56:33 +020082int64_t max_delay;
83int64_t max_advance;
Blue Swirl296af7c2010-03-29 19:23:50 +000084
Jason J. Herne2adcc852015-09-08 13:12:33 -040085/* vcpu throttling controls */
86static QEMUTimer *throttle_timer;
87static unsigned int throttle_percentage;
88
89#define CPU_THROTTLE_PCT_MIN 1
90#define CPU_THROTTLE_PCT_MAX 99
91#define CPU_THROTTLE_TIMESLICE_NS 10000000
92
Tiejun Chen321bc0b2013-08-02 09:43:09 +080093bool cpu_is_stopped(CPUState *cpu)
94{
95 return cpu->stopped || !runstate_is_running();
96}
97
Andreas Färbera98ae1d2013-05-26 23:21:08 +020098static bool cpu_thread_is_idle(CPUState *cpu)
Peter Maydellac873f12012-07-19 16:52:27 +010099{
Andreas Färberc64ca812012-05-03 02:11:45 +0200100 if (cpu->stop || cpu->queued_work_first) {
Peter Maydellac873f12012-07-19 16:52:27 +0100101 return false;
102 }
Tiejun Chen321bc0b2013-08-02 09:43:09 +0800103 if (cpu_is_stopped(cpu)) {
Peter Maydellac873f12012-07-19 16:52:27 +0100104 return true;
105 }
Andreas Färber8c2e1b02013-08-25 18:53:55 +0200106 if (!cpu->halted || cpu_has_work(cpu) ||
Alexander Graf215e79c2013-04-24 22:24:12 +0200107 kvm_halt_in_kernel()) {
Peter Maydellac873f12012-07-19 16:52:27 +0100108 return false;
109 }
110 return true;
111}
112
113static bool all_cpu_threads_idle(void)
114{
Andreas Färber182735e2013-05-29 22:29:20 +0200115 CPUState *cpu;
Peter Maydellac873f12012-07-19 16:52:27 +0100116
Andreas Färberbdc44642013-06-24 23:50:24 +0200117 CPU_FOREACH(cpu) {
Andreas Färber182735e2013-05-29 22:29:20 +0200118 if (!cpu_thread_is_idle(cpu)) {
Peter Maydellac873f12012-07-19 16:52:27 +0100119 return false;
120 }
121 }
122 return true;
123}
124
Blue Swirl296af7c2010-03-29 19:23:50 +0000125/***********************************************************/
Paolo Bonzini946fb272011-09-12 13:57:37 +0200126/* guest cycle counter */
127
Paolo Bonzinia3270e12013-10-07 17:18:15 +0200128/* Protected by TimersState seqlock */
129
Victor CLEMENT5045e9d92015-05-29 17:14:04 +0200130static bool icount_sleep = true;
Paolo Bonzini946fb272011-09-12 13:57:37 +0200131/* Arbitrarily pick 1MIPS as the minimum allowable speed. */
132#define MAX_ICOUNT_SHIFT 10
Paolo Bonzinia3270e12013-10-07 17:18:15 +0200133
Paolo Bonzini946fb272011-09-12 13:57:37 +0200134typedef struct TimersState {
Liu Ping Fancb365642013-09-25 14:20:58 +0800135 /* Protected by BQL. */
Paolo Bonzini946fb272011-09-12 13:57:37 +0200136 int64_t cpu_ticks_prev;
137 int64_t cpu_ticks_offset;
Liu Ping Fancb365642013-09-25 14:20:58 +0800138
Paolo Bonzini94377112018-08-14 09:57:16 +0200139 /* Protect fields that can be respectively read outside the
140 * BQL, and written from multiple threads.
Liu Ping Fancb365642013-09-25 14:20:58 +0800141 */
142 QemuSeqLock vm_clock_seqlock;
Paolo Bonzini94377112018-08-14 09:57:16 +0200143 QemuSpin vm_clock_lock;
144
145 int16_t cpu_ticks_enabled;
KONRAD Fredericc96778b2014-08-01 01:37:09 +0200146
Paolo Bonzinic1ff0732018-08-14 09:31:58 +0200147 /* Conversion factor from emulated instructions to virtual clock ticks. */
Paolo Bonzini94377112018-08-14 09:57:16 +0200148 int16_t icount_time_shift;
149
KONRAD Fredericc96778b2014-08-01 01:37:09 +0200150 /* Compensate for varying guest execution speed. */
151 int64_t qemu_icount_bias;
Paolo Bonzini94377112018-08-14 09:57:16 +0200152
153 int64_t vm_clock_warp_start;
154 int64_t cpu_clock_offset;
155
KONRAD Fredericc96778b2014-08-01 01:37:09 +0200156 /* Only written by TCG thread */
157 int64_t qemu_icount;
Paolo Bonzini94377112018-08-14 09:57:16 +0200158
Pavel Dovgalyukb39e3f32018-01-11 11:26:10 +0300159 /* for adjusting icount */
Pavel Dovgalyukb39e3f32018-01-11 11:26:10 +0300160 QEMUTimer *icount_rt_timer;
161 QEMUTimer *icount_vm_timer;
162 QEMUTimer *icount_warp_timer;
Paolo Bonzini946fb272011-09-12 13:57:37 +0200163} TimersState;
164
Liu Ping Fand9cd4002013-07-21 08:43:00 +0000165static TimersState timers_state;
KONRAD Frederic8d4e9142017-02-23 18:29:08 +0000166bool mttcg_enabled;
167
168/*
169 * We default to false if we know other options have been enabled
170 * which are currently incompatible with MTTCG. Otherwise when each
171 * guest (target) has been updated to support:
172 * - atomic instructions
173 * - memory ordering primitives (barriers)
174 * they can set the appropriate CONFIG flags in ${target}-softmmu.mak
175 *
176 * Once a guest architecture has been converted to the new primitives
177 * there are two remaining limitations to check.
178 *
179 * - The guest can't be oversized (e.g. 64 bit guest on 32 bit host)
180 * - The host must have a stronger memory order than the guest
181 *
182 * It may be possible in future to support strong guests on weak hosts
183 * but that will require tagging all load/stores in a guest with their
184 * implicit memory order requirements which would likely slow things
185 * down a lot.
186 */
187
188static bool check_tcg_memory_orders_compatible(void)
189{
190#if defined(TCG_GUEST_DEFAULT_MO) && defined(TCG_TARGET_DEFAULT_MO)
191 return (TCG_GUEST_DEFAULT_MO & ~TCG_TARGET_DEFAULT_MO) == 0;
192#else
193 return false;
194#endif
195}
196
197static bool default_mttcg_enabled(void)
198{
Alex Bennée83fd9622017-02-27 17:09:01 +0000199 if (use_icount || TCG_OVERSIZED_GUEST) {
KONRAD Frederic8d4e9142017-02-23 18:29:08 +0000200 return false;
201 } else {
202#ifdef TARGET_SUPPORTS_MTTCG
203 return check_tcg_memory_orders_compatible();
204#else
205 return false;
206#endif
207 }
208}
209
210void qemu_tcg_configure(QemuOpts *opts, Error **errp)
211{
212 const char *t = qemu_opt_get(opts, "thread");
213 if (t) {
214 if (strcmp(t, "multi") == 0) {
215 if (TCG_OVERSIZED_GUEST) {
216 error_setg(errp, "No MTTCG when guest word size > hosts");
Alex Bennée83fd9622017-02-27 17:09:01 +0000217 } else if (use_icount) {
218 error_setg(errp, "No MTTCG when icount is enabled");
KONRAD Frederic8d4e9142017-02-23 18:29:08 +0000219 } else {
Nikunj A Dadhania86953502017-04-10 11:36:55 +0530220#ifndef TARGET_SUPPORTS_MTTCG
Markus Armbruster07656912018-10-17 10:26:28 +0200221 warn_report("Guest not yet converted to MTTCG - "
222 "you may get unexpected results");
Alex Bennéec34c7622017-02-28 14:40:17 +0000223#endif
KONRAD Frederic8d4e9142017-02-23 18:29:08 +0000224 if (!check_tcg_memory_orders_compatible()) {
Markus Armbruster07656912018-10-17 10:26:28 +0200225 warn_report("Guest expects a stronger memory ordering "
226 "than the host provides");
Pranith Kumar8cfef892017-03-25 16:19:23 -0400227 error_printf("This may cause strange/hard to debug errors\n");
KONRAD Frederic8d4e9142017-02-23 18:29:08 +0000228 }
229 mttcg_enabled = true;
230 }
231 } else if (strcmp(t, "single") == 0) {
232 mttcg_enabled = false;
233 } else {
234 error_setg(errp, "Invalid 'thread' setting %s", t);
235 }
236 } else {
237 mttcg_enabled = default_mttcg_enabled();
238 }
239}
Paolo Bonzini946fb272011-09-12 13:57:37 +0200240
Alex Bennéee4cd9652017-03-31 16:09:42 +0100241/* The current number of executed instructions is based on what we
242 * originally budgeted minus the current state of the decrementing
243 * icount counters in extra/u16.low.
244 */
245static int64_t cpu_get_icount_executed(CPUState *cpu)
246{
Richard Henderson5e140192019-03-28 11:54:23 -1000247 return (cpu->icount_budget -
248 (cpu_neg(cpu)->icount_decr.u16.low + cpu->icount_extra));
Alex Bennéee4cd9652017-03-31 16:09:42 +0100249}
250
Alex Bennée512d3c82017-04-05 12:32:37 +0100251/*
252 * Update the global shared timer_state.qemu_icount to take into
253 * account executed instructions. This is done by the TCG vCPU
254 * thread so the main-loop can see time has moved forward.
255 */
Paolo Bonzini9b4e6f42018-09-11 13:15:32 +0200256static void cpu_update_icount_locked(CPUState *cpu)
Alex Bennée512d3c82017-04-05 12:32:37 +0100257{
258 int64_t executed = cpu_get_icount_executed(cpu);
259 cpu->icount_budget -= executed;
260
Emilio G. Cota38adcb62018-09-10 19:27:49 -0400261 atomic_set_i64(&timers_state.qemu_icount,
262 timers_state.qemu_icount + executed);
Paolo Bonzini9b4e6f42018-09-11 13:15:32 +0200263}
264
265/*
266 * Update the global shared timer_state.qemu_icount to take into
267 * account executed instructions. This is done by the TCG vCPU
268 * thread so the main-loop can see time has moved forward.
269 */
270void cpu_update_icount(CPUState *cpu)
271{
272 seqlock_write_lock(&timers_state.vm_clock_seqlock,
273 &timers_state.vm_clock_lock);
274 cpu_update_icount_locked(cpu);
Paolo Bonzini94377112018-08-14 09:57:16 +0200275 seqlock_write_unlock(&timers_state.vm_clock_seqlock,
276 &timers_state.vm_clock_lock);
Alex Bennée512d3c82017-04-05 12:32:37 +0100277}
278
Paolo Bonzinic1ff0732018-08-14 09:31:58 +0200279static int64_t cpu_get_icount_raw_locked(void)
Paolo Bonzini946fb272011-09-12 13:57:37 +0200280{
Andreas Färber4917cf42013-05-27 05:17:50 +0200281 CPUState *cpu = current_cpu;
Paolo Bonzini946fb272011-09-12 13:57:37 +0200282
Alex Bennée243c5f72017-03-30 18:49:22 +0100283 if (cpu && cpu->running) {
Paolo Bonzini414b15c2015-06-24 14:16:26 +0200284 if (!cpu->can_do_io) {
Alistair Francis493d89b2018-02-03 09:43:14 +0100285 error_report("Bad icount read");
Pavel Dovgalyuk2a629142014-12-08 10:53:45 +0300286 exit(1);
Paolo Bonzini946fb272011-09-12 13:57:37 +0200287 }
Alex Bennéee4cd9652017-03-31 16:09:42 +0100288 /* Take into account what has run */
Paolo Bonzini9b4e6f42018-09-11 13:15:32 +0200289 cpu_update_icount_locked(cpu);
Paolo Bonzini946fb272011-09-12 13:57:37 +0200290 }
Emilio G. Cota38adcb62018-09-10 19:27:49 -0400291 /* The read is protected by the seqlock, but needs atomic64 to avoid UB */
292 return atomic_read_i64(&timers_state.qemu_icount);
Paolo Bonzinic1ff0732018-08-14 09:31:58 +0200293}
294
295static int64_t cpu_get_icount_locked(void)
296{
297 int64_t icount = cpu_get_icount_raw_locked();
Emilio G. Cotac97595d2018-09-10 19:27:50 -0400298 return atomic_read_i64(&timers_state.qemu_icount_bias) +
299 cpu_icount_to_ns(icount);
Paolo Bonzinic1ff0732018-08-14 09:31:58 +0200300}
301
302int64_t cpu_get_icount_raw(void)
303{
304 int64_t icount;
305 unsigned start;
306
307 do {
308 start = seqlock_read_begin(&timers_state.vm_clock_seqlock);
309 icount = cpu_get_icount_raw_locked();
310 } while (seqlock_read_retry(&timers_state.vm_clock_seqlock, start));
311
312 return icount;
Pavel Dovgalyuk2a629142014-12-08 10:53:45 +0300313}
314
315/* Return the virtual CPU time, based on the instruction counter. */
Paolo Bonzini17a15f12013-10-03 15:17:25 +0200316int64_t cpu_get_icount(void)
317{
318 int64_t icount;
319 unsigned start;
320
321 do {
322 start = seqlock_read_begin(&timers_state.vm_clock_seqlock);
323 icount = cpu_get_icount_locked();
324 } while (seqlock_read_retry(&timers_state.vm_clock_seqlock, start));
325
326 return icount;
327}
328
KONRAD Frederic3f031312014-08-01 01:37:15 +0200329int64_t cpu_icount_to_ns(int64_t icount)
330{
Paolo Bonzinic1ff0732018-08-14 09:31:58 +0200331 return icount << atomic_read(&timers_state.icount_time_shift);
KONRAD Frederic3f031312014-08-01 01:37:15 +0200332}
333
Paolo Bonzinif2a4ad62018-08-18 09:36:16 +0200334static int64_t cpu_get_ticks_locked(void)
335{
336 int64_t ticks = timers_state.cpu_ticks_offset;
337 if (timers_state.cpu_ticks_enabled) {
338 ticks += cpu_get_host_ticks();
339 }
340
341 if (timers_state.cpu_ticks_prev > ticks) {
342 /* Non increasing ticks may happen if the host uses software suspend. */
343 timers_state.cpu_ticks_offset += timers_state.cpu_ticks_prev - ticks;
344 ticks = timers_state.cpu_ticks_prev;
345 }
346
347 timers_state.cpu_ticks_prev = ticks;
348 return ticks;
349}
350
Cao jind90f3cc2016-07-29 19:05:38 +0800351/* return the time elapsed in VM between vm_start and vm_stop. Unless
352 * icount is active, cpu_get_ticks() uses units of the host CPU cycle
353 * counter.
Cao jind90f3cc2016-07-29 19:05:38 +0800354 */
Paolo Bonzini946fb272011-09-12 13:57:37 +0200355int64_t cpu_get_ticks(void)
356{
Paolo Bonzini5f3e3102013-10-28 17:32:18 +0100357 int64_t ticks;
358
Paolo Bonzini946fb272011-09-12 13:57:37 +0200359 if (use_icount) {
360 return cpu_get_icount();
361 }
Paolo Bonzini5f3e3102013-10-28 17:32:18 +0100362
Paolo Bonzinif2a4ad62018-08-18 09:36:16 +0200363 qemu_spin_lock(&timers_state.vm_clock_lock);
364 ticks = cpu_get_ticks_locked();
365 qemu_spin_unlock(&timers_state.vm_clock_lock);
Paolo Bonzini5f3e3102013-10-28 17:32:18 +0100366 return ticks;
Paolo Bonzini946fb272011-09-12 13:57:37 +0200367}
368
Liu Ping Fancb365642013-09-25 14:20:58 +0800369static int64_t cpu_get_clock_locked(void)
370{
Cao jin1d45cea2016-07-29 19:05:37 +0800371 int64_t time;
Liu Ping Fancb365642013-09-25 14:20:58 +0800372
Cao jin1d45cea2016-07-29 19:05:37 +0800373 time = timers_state.cpu_clock_offset;
Paolo Bonzini5f3e3102013-10-28 17:32:18 +0100374 if (timers_state.cpu_ticks_enabled) {
Cao jin1d45cea2016-07-29 19:05:37 +0800375 time += get_clock();
Liu Ping Fancb365642013-09-25 14:20:58 +0800376 }
377
Cao jin1d45cea2016-07-29 19:05:37 +0800378 return time;
Liu Ping Fancb365642013-09-25 14:20:58 +0800379}
380
Cao jind90f3cc2016-07-29 19:05:38 +0800381/* Return the monotonic time elapsed in VM, i.e.,
Peter Maydell8212ff82016-09-15 10:24:22 +0100382 * the time between vm_start and vm_stop
383 */
Paolo Bonzini946fb272011-09-12 13:57:37 +0200384int64_t cpu_get_clock(void)
385{
386 int64_t ti;
Liu Ping Fancb365642013-09-25 14:20:58 +0800387 unsigned start;
388
389 do {
390 start = seqlock_read_begin(&timers_state.vm_clock_seqlock);
391 ti = cpu_get_clock_locked();
392 } while (seqlock_read_retry(&timers_state.vm_clock_seqlock, start));
393
394 return ti;
Paolo Bonzini946fb272011-09-12 13:57:37 +0200395}
396
Liu Ping Fancb365642013-09-25 14:20:58 +0800397/* enable cpu_get_ticks()
Cao jin3224e872016-07-08 18:31:37 +0800398 * Caller must hold BQL which serves as mutex for vm_clock_seqlock.
Liu Ping Fancb365642013-09-25 14:20:58 +0800399 */
Paolo Bonzini946fb272011-09-12 13:57:37 +0200400void cpu_enable_ticks(void)
401{
Paolo Bonzini94377112018-08-14 09:57:16 +0200402 seqlock_write_lock(&timers_state.vm_clock_seqlock,
403 &timers_state.vm_clock_lock);
Paolo Bonzini946fb272011-09-12 13:57:37 +0200404 if (!timers_state.cpu_ticks_enabled) {
Christopher Covington4a7428c2015-09-25 10:42:21 -0400405 timers_state.cpu_ticks_offset -= cpu_get_host_ticks();
Paolo Bonzini946fb272011-09-12 13:57:37 +0200406 timers_state.cpu_clock_offset -= get_clock();
407 timers_state.cpu_ticks_enabled = 1;
408 }
Paolo Bonzini94377112018-08-14 09:57:16 +0200409 seqlock_write_unlock(&timers_state.vm_clock_seqlock,
410 &timers_state.vm_clock_lock);
Paolo Bonzini946fb272011-09-12 13:57:37 +0200411}
412
413/* disable cpu_get_ticks() : the clock is stopped. You must not call
Liu Ping Fancb365642013-09-25 14:20:58 +0800414 * cpu_get_ticks() after that.
Cao jin3224e872016-07-08 18:31:37 +0800415 * Caller must hold BQL which serves as mutex for vm_clock_seqlock.
Liu Ping Fancb365642013-09-25 14:20:58 +0800416 */
Paolo Bonzini946fb272011-09-12 13:57:37 +0200417void cpu_disable_ticks(void)
418{
Paolo Bonzini94377112018-08-14 09:57:16 +0200419 seqlock_write_lock(&timers_state.vm_clock_seqlock,
420 &timers_state.vm_clock_lock);
Paolo Bonzini946fb272011-09-12 13:57:37 +0200421 if (timers_state.cpu_ticks_enabled) {
Christopher Covington4a7428c2015-09-25 10:42:21 -0400422 timers_state.cpu_ticks_offset += cpu_get_host_ticks();
Liu Ping Fancb365642013-09-25 14:20:58 +0800423 timers_state.cpu_clock_offset = cpu_get_clock_locked();
Paolo Bonzini946fb272011-09-12 13:57:37 +0200424 timers_state.cpu_ticks_enabled = 0;
425 }
Paolo Bonzini94377112018-08-14 09:57:16 +0200426 seqlock_write_unlock(&timers_state.vm_clock_seqlock,
427 &timers_state.vm_clock_lock);
Paolo Bonzini946fb272011-09-12 13:57:37 +0200428}
429
430/* Correlation between real and virtual time is always going to be
431 fairly approximate, so ignore small variation.
432 When the guest is idle real and virtual time will be aligned in
433 the IO wait loop. */
Rutuja Shah73bcb242016-03-21 21:32:30 +0530434#define ICOUNT_WOBBLE (NANOSECONDS_PER_SECOND / 10)
Paolo Bonzini946fb272011-09-12 13:57:37 +0200435
436static void icount_adjust(void)
437{
438 int64_t cur_time;
439 int64_t cur_icount;
440 int64_t delta;
Paolo Bonzinia3270e12013-10-07 17:18:15 +0200441
442 /* Protected by TimersState mutex. */
Paolo Bonzini946fb272011-09-12 13:57:37 +0200443 static int64_t last_delta;
Paolo Bonzini468cc7c2013-10-07 17:21:51 +0200444
Paolo Bonzini946fb272011-09-12 13:57:37 +0200445 /* If the VM is not running, then do nothing. */
446 if (!runstate_is_running()) {
447 return;
448 }
Paolo Bonzini468cc7c2013-10-07 17:21:51 +0200449
Paolo Bonzini94377112018-08-14 09:57:16 +0200450 seqlock_write_lock(&timers_state.vm_clock_seqlock,
451 &timers_state.vm_clock_lock);
Paolo Bonzini17a15f12013-10-03 15:17:25 +0200452 cur_time = cpu_get_clock_locked();
453 cur_icount = cpu_get_icount_locked();
Paolo Bonzini468cc7c2013-10-07 17:21:51 +0200454
Paolo Bonzini946fb272011-09-12 13:57:37 +0200455 delta = cur_icount - cur_time;
456 /* FIXME: This is a very crude algorithm, somewhat prone to oscillation. */
457 if (delta > 0
458 && last_delta + ICOUNT_WOBBLE < delta * 2
Paolo Bonzinic1ff0732018-08-14 09:31:58 +0200459 && timers_state.icount_time_shift > 0) {
Paolo Bonzini946fb272011-09-12 13:57:37 +0200460 /* The guest is getting too far ahead. Slow time down. */
Paolo Bonzinic1ff0732018-08-14 09:31:58 +0200461 atomic_set(&timers_state.icount_time_shift,
462 timers_state.icount_time_shift - 1);
Paolo Bonzini946fb272011-09-12 13:57:37 +0200463 }
464 if (delta < 0
465 && last_delta - ICOUNT_WOBBLE > delta * 2
Paolo Bonzinic1ff0732018-08-14 09:31:58 +0200466 && timers_state.icount_time_shift < MAX_ICOUNT_SHIFT) {
Paolo Bonzini946fb272011-09-12 13:57:37 +0200467 /* The guest is getting too far behind. Speed time up. */
Paolo Bonzinic1ff0732018-08-14 09:31:58 +0200468 atomic_set(&timers_state.icount_time_shift,
469 timers_state.icount_time_shift + 1);
Paolo Bonzini946fb272011-09-12 13:57:37 +0200470 }
471 last_delta = delta;
Emilio G. Cotac97595d2018-09-10 19:27:50 -0400472 atomic_set_i64(&timers_state.qemu_icount_bias,
473 cur_icount - (timers_state.qemu_icount
474 << timers_state.icount_time_shift));
Paolo Bonzini94377112018-08-14 09:57:16 +0200475 seqlock_write_unlock(&timers_state.vm_clock_seqlock,
476 &timers_state.vm_clock_lock);
Paolo Bonzini946fb272011-09-12 13:57:37 +0200477}
478
479static void icount_adjust_rt(void *opaque)
480{
Pavel Dovgalyukb39e3f32018-01-11 11:26:10 +0300481 timer_mod(timers_state.icount_rt_timer,
Pavel Dovgalyuk1979b902015-01-12 15:00:43 +0300482 qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL_RT) + 1000);
Paolo Bonzini946fb272011-09-12 13:57:37 +0200483 icount_adjust();
484}
485
486static void icount_adjust_vm(void *opaque)
487{
Pavel Dovgalyukb39e3f32018-01-11 11:26:10 +0300488 timer_mod(timers_state.icount_vm_timer,
Alex Bligh40daca52013-08-21 16:03:02 +0100489 qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) +
Rutuja Shah73bcb242016-03-21 21:32:30 +0530490 NANOSECONDS_PER_SECOND / 10);
Paolo Bonzini946fb272011-09-12 13:57:37 +0200491 icount_adjust();
492}
493
494static int64_t qemu_icount_round(int64_t count)
495{
Paolo Bonzinic1ff0732018-08-14 09:31:58 +0200496 int shift = atomic_read(&timers_state.icount_time_shift);
497 return (count + (1 << shift) - 1) >> shift;
Paolo Bonzini946fb272011-09-12 13:57:37 +0200498}
499
Pavel Dovgalyukefab87c2015-09-17 19:24:39 +0300500static void icount_warp_rt(void)
Paolo Bonzini946fb272011-09-12 13:57:37 +0200501{
Alex Bennéeccffff42016-04-04 15:35:48 +0100502 unsigned seq;
503 int64_t warp_start;
504
Paolo Bonzini17a15f12013-10-03 15:17:25 +0200505 /* The icount_warp_timer is rescheduled soon after vm_clock_warp_start
506 * changes from -1 to another value, so the race here is okay.
507 */
Alex Bennéeccffff42016-04-04 15:35:48 +0100508 do {
509 seq = seqlock_read_begin(&timers_state.vm_clock_seqlock);
Pavel Dovgalyukb39e3f32018-01-11 11:26:10 +0300510 warp_start = timers_state.vm_clock_warp_start;
Alex Bennéeccffff42016-04-04 15:35:48 +0100511 } while (seqlock_read_retry(&timers_state.vm_clock_seqlock, seq));
512
513 if (warp_start == -1) {
Paolo Bonzini946fb272011-09-12 13:57:37 +0200514 return;
515 }
516
Paolo Bonzini94377112018-08-14 09:57:16 +0200517 seqlock_write_lock(&timers_state.vm_clock_seqlock,
518 &timers_state.vm_clock_lock);
Paolo Bonzini946fb272011-09-12 13:57:37 +0200519 if (runstate_is_running()) {
Paolo Bonzini74c0b812018-10-08 13:24:14 +0200520 int64_t clock = REPLAY_CLOCK_LOCKED(REPLAY_CLOCK_VIRTUAL_RT,
521 cpu_get_clock_locked());
Paolo Bonzini8ed961d2013-10-07 17:26:07 +0200522 int64_t warp_delta;
523
Pavel Dovgalyukb39e3f32018-01-11 11:26:10 +0300524 warp_delta = clock - timers_state.vm_clock_warp_start;
Paolo Bonzini8ed961d2013-10-07 17:26:07 +0200525 if (use_icount == 2) {
Paolo Bonzini946fb272011-09-12 13:57:37 +0200526 /*
Alex Bligh40daca52013-08-21 16:03:02 +0100527 * In adaptive mode, do not let QEMU_CLOCK_VIRTUAL run too
Paolo Bonzini946fb272011-09-12 13:57:37 +0200528 * far ahead of real time.
529 */
Paolo Bonzini17a15f12013-10-03 15:17:25 +0200530 int64_t cur_icount = cpu_get_icount_locked();
Pavel Dovgalyukbf2a7dd2014-11-26 13:40:55 +0300531 int64_t delta = clock - cur_icount;
Paolo Bonzini8ed961d2013-10-07 17:26:07 +0200532 warp_delta = MIN(warp_delta, delta);
Paolo Bonzini946fb272011-09-12 13:57:37 +0200533 }
Emilio G. Cotac97595d2018-09-10 19:27:50 -0400534 atomic_set_i64(&timers_state.qemu_icount_bias,
535 timers_state.qemu_icount_bias + warp_delta);
Paolo Bonzini946fb272011-09-12 13:57:37 +0200536 }
Pavel Dovgalyukb39e3f32018-01-11 11:26:10 +0300537 timers_state.vm_clock_warp_start = -1;
Paolo Bonzini94377112018-08-14 09:57:16 +0200538 seqlock_write_unlock(&timers_state.vm_clock_seqlock,
539 &timers_state.vm_clock_lock);
Paolo Bonzini8ed961d2013-10-07 17:26:07 +0200540
541 if (qemu_clock_expired(QEMU_CLOCK_VIRTUAL)) {
542 qemu_clock_notify(QEMU_CLOCK_VIRTUAL);
543 }
Paolo Bonzini946fb272011-09-12 13:57:37 +0200544}
545
Pavel Dovgalyuke76d1792016-03-10 14:56:09 +0300546static void icount_timer_cb(void *opaque)
Pavel Dovgalyukefab87c2015-09-17 19:24:39 +0300547{
Pavel Dovgalyuke76d1792016-03-10 14:56:09 +0300548 /* No need for a checkpoint because the timer already synchronizes
549 * with CHECKPOINT_CLOCK_VIRTUAL_RT.
550 */
551 icount_warp_rt();
Pavel Dovgalyukefab87c2015-09-17 19:24:39 +0300552}
553
Paolo Bonzini8156be52012-03-28 15:42:04 +0200554void qtest_clock_warp(int64_t dest)
555{
Alex Bligh40daca52013-08-21 16:03:02 +0100556 int64_t clock = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
Fam Zhengefef88b2015-01-19 17:51:43 +0800557 AioContext *aio_context;
Paolo Bonzini8156be52012-03-28 15:42:04 +0200558 assert(qtest_enabled());
Fam Zhengefef88b2015-01-19 17:51:43 +0800559 aio_context = qemu_get_aio_context();
Paolo Bonzini8156be52012-03-28 15:42:04 +0200560 while (clock < dest) {
Pavel Dovgalyukdcb15782019-07-25 11:44:26 +0300561 int64_t deadline = qemu_clock_deadline_ns_all(QEMU_CLOCK_VIRTUAL,
562 QEMU_TIMER_ATTR_ALL);
Sergey Fedorovc9299e22014-06-10 13:10:28 +0400563 int64_t warp = qemu_soonest_timeout(dest - clock, deadline);
Fam Zhengefef88b2015-01-19 17:51:43 +0800564
Paolo Bonzini94377112018-08-14 09:57:16 +0200565 seqlock_write_lock(&timers_state.vm_clock_seqlock,
566 &timers_state.vm_clock_lock);
Emilio G. Cotac97595d2018-09-10 19:27:50 -0400567 atomic_set_i64(&timers_state.qemu_icount_bias,
568 timers_state.qemu_icount_bias + warp);
Paolo Bonzini94377112018-08-14 09:57:16 +0200569 seqlock_write_unlock(&timers_state.vm_clock_seqlock,
570 &timers_state.vm_clock_lock);
Paolo Bonzini17a15f12013-10-03 15:17:25 +0200571
Alex Bligh40daca52013-08-21 16:03:02 +0100572 qemu_clock_run_timers(QEMU_CLOCK_VIRTUAL);
Fam Zhengefef88b2015-01-19 17:51:43 +0800573 timerlist_run_timers(aio_context->tlg.tl[QEMU_CLOCK_VIRTUAL]);
Alex Bligh40daca52013-08-21 16:03:02 +0100574 clock = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
Paolo Bonzini8156be52012-03-28 15:42:04 +0200575 }
Alex Bligh40daca52013-08-21 16:03:02 +0100576 qemu_clock_notify(QEMU_CLOCK_VIRTUAL);
Paolo Bonzini8156be52012-03-28 15:42:04 +0200577}
578
Pavel Dovgalyuke76d1792016-03-10 14:56:09 +0300579void qemu_start_warp_timer(void)
Paolo Bonzini946fb272011-09-12 13:57:37 +0200580{
Paolo Bonzinice78d182013-10-07 17:30:02 +0200581 int64_t clock;
Paolo Bonzini946fb272011-09-12 13:57:37 +0200582 int64_t deadline;
583
Pavel Dovgalyuke76d1792016-03-10 14:56:09 +0300584 if (!use_icount) {
Paolo Bonzini946fb272011-09-12 13:57:37 +0200585 return;
586 }
587
Pavel Dovgalyuk8bd7f712015-09-17 19:24:44 +0300588 /* Nothing to do if the VM is stopped: QEMU_CLOCK_VIRTUAL timers
589 * do not fire, so computing the deadline does not make sense.
590 */
591 if (!runstate_is_running()) {
592 return;
593 }
594
Pavel Dovgalyuk0c081852018-09-12 11:19:45 +0300595 if (replay_mode != REPLAY_MODE_PLAY) {
596 if (!all_cpu_threads_idle()) {
597 return;
598 }
Pavel Dovgalyuk8bd7f712015-09-17 19:24:44 +0300599
Pavel Dovgalyuk0c081852018-09-12 11:19:45 +0300600 if (qtest_enabled()) {
601 /* When testing, qtest commands advance icount. */
602 return;
603 }
Paolo Bonzini946fb272011-09-12 13:57:37 +0200604
Pavel Dovgalyuk0c081852018-09-12 11:19:45 +0300605 replay_checkpoint(CHECKPOINT_CLOCK_WARP_START);
606 } else {
607 /* warp clock deterministically in record/replay mode */
608 if (!replay_checkpoint(CHECKPOINT_CLOCK_WARP_START)) {
609 /* vCPU is sleeping and warp can't be started.
610 It is probably a race condition: notification sent
611 to vCPU was processed in advance and vCPU went to sleep.
612 Therefore we have to wake it up for doing someting. */
613 if (replay_has_checkpoint()) {
614 qemu_clock_notify(QEMU_CLOCK_VIRTUAL);
615 }
616 return;
617 }
Paolo Bonzini8156be52012-03-28 15:42:04 +0200618 }
619
Alex Blighac70aaf2013-08-21 16:02:57 +0100620 /* We want to use the earliest deadline from ALL vm_clocks */
Pavel Dovgalyukbf2a7dd2014-11-26 13:40:55 +0300621 clock = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL_RT);
Pavel Dovgalyukdcb15782019-07-25 11:44:26 +0300622 deadline = qemu_clock_deadline_ns_all(QEMU_CLOCK_VIRTUAL,
623 ~QEMU_TIMER_ATTR_EXTERNAL);
Paolo Bonzinice78d182013-10-07 17:30:02 +0200624 if (deadline < 0) {
Victor CLEMENTd7a0f712015-05-29 17:14:06 +0200625 static bool notified;
626 if (!icount_sleep && !notified) {
Alistair Francis3dc6f862017-07-12 06:57:41 -0700627 warn_report("icount sleep disabled and no active timers");
Victor CLEMENTd7a0f712015-05-29 17:14:06 +0200628 notified = true;
629 }
Paolo Bonzinice78d182013-10-07 17:30:02 +0200630 return;
Alex Blighac70aaf2013-08-21 16:02:57 +0100631 }
632
Paolo Bonzini946fb272011-09-12 13:57:37 +0200633 if (deadline > 0) {
634 /*
Alex Bligh40daca52013-08-21 16:03:02 +0100635 * Ensure QEMU_CLOCK_VIRTUAL proceeds even when the virtual CPU goes to
Paolo Bonzini946fb272011-09-12 13:57:37 +0200636 * sleep. Otherwise, the CPU might be waiting for a future timer
637 * interrupt to wake it up, but the interrupt never comes because
638 * the vCPU isn't running any insns and thus doesn't advance the
Alex Bligh40daca52013-08-21 16:03:02 +0100639 * QEMU_CLOCK_VIRTUAL.
Paolo Bonzini946fb272011-09-12 13:57:37 +0200640 */
Victor CLEMENT5045e9d92015-05-29 17:14:04 +0200641 if (!icount_sleep) {
642 /*
643 * We never let VCPUs sleep in no sleep icount mode.
644 * If there is a pending QEMU_CLOCK_VIRTUAL timer we just advance
645 * to the next QEMU_CLOCK_VIRTUAL event and notify it.
646 * It is useful when we want a deterministic execution time,
647 * isolated from host latencies.
648 */
Paolo Bonzini94377112018-08-14 09:57:16 +0200649 seqlock_write_lock(&timers_state.vm_clock_seqlock,
650 &timers_state.vm_clock_lock);
Emilio G. Cotac97595d2018-09-10 19:27:50 -0400651 atomic_set_i64(&timers_state.qemu_icount_bias,
652 timers_state.qemu_icount_bias + deadline);
Paolo Bonzini94377112018-08-14 09:57:16 +0200653 seqlock_write_unlock(&timers_state.vm_clock_seqlock,
654 &timers_state.vm_clock_lock);
Victor CLEMENT5045e9d92015-05-29 17:14:04 +0200655 qemu_clock_notify(QEMU_CLOCK_VIRTUAL);
656 } else {
657 /*
658 * We do stop VCPUs and only advance QEMU_CLOCK_VIRTUAL after some
659 * "real" time, (related to the time left until the next event) has
660 * passed. The QEMU_CLOCK_VIRTUAL_RT clock will do this.
661 * This avoids that the warps are visible externally; for example,
662 * you will not be sending network packets continuously instead of
663 * every 100ms.
664 */
Paolo Bonzini94377112018-08-14 09:57:16 +0200665 seqlock_write_lock(&timers_state.vm_clock_seqlock,
666 &timers_state.vm_clock_lock);
Pavel Dovgalyukb39e3f32018-01-11 11:26:10 +0300667 if (timers_state.vm_clock_warp_start == -1
668 || timers_state.vm_clock_warp_start > clock) {
669 timers_state.vm_clock_warp_start = clock;
Victor CLEMENT5045e9d92015-05-29 17:14:04 +0200670 }
Paolo Bonzini94377112018-08-14 09:57:16 +0200671 seqlock_write_unlock(&timers_state.vm_clock_seqlock,
672 &timers_state.vm_clock_lock);
Pavel Dovgalyukb39e3f32018-01-11 11:26:10 +0300673 timer_mod_anticipate(timers_state.icount_warp_timer,
674 clock + deadline);
Paolo Bonzinice78d182013-10-07 17:30:02 +0200675 }
Alex Blighac70aaf2013-08-21 16:02:57 +0100676 } else if (deadline == 0) {
Alex Bligh40daca52013-08-21 16:03:02 +0100677 qemu_clock_notify(QEMU_CLOCK_VIRTUAL);
Paolo Bonzini946fb272011-09-12 13:57:37 +0200678 }
679}
680
Pavel Dovgalyuke76d1792016-03-10 14:56:09 +0300681static void qemu_account_warp_timer(void)
682{
683 if (!use_icount || !icount_sleep) {
684 return;
685 }
686
687 /* Nothing to do if the VM is stopped: QEMU_CLOCK_VIRTUAL timers
688 * do not fire, so computing the deadline does not make sense.
689 */
690 if (!runstate_is_running()) {
691 return;
692 }
693
694 /* warp clock deterministically in record/replay mode */
695 if (!replay_checkpoint(CHECKPOINT_CLOCK_WARP_ACCOUNT)) {
696 return;
697 }
698
Pavel Dovgalyukb39e3f32018-01-11 11:26:10 +0300699 timer_del(timers_state.icount_warp_timer);
Pavel Dovgalyuke76d1792016-03-10 14:56:09 +0300700 icount_warp_rt();
701}
702
KONRAD Fredericd09eae32014-08-01 01:37:10 +0200703static bool icount_state_needed(void *opaque)
704{
705 return use_icount;
706}
707
Pavel Dovgalyukb39e3f32018-01-11 11:26:10 +0300708static bool warp_timer_state_needed(void *opaque)
709{
710 TimersState *s = opaque;
711 return s->icount_warp_timer != NULL;
712}
713
714static bool adjust_timers_state_needed(void *opaque)
715{
716 TimersState *s = opaque;
717 return s->icount_rt_timer != NULL;
718}
719
720/*
721 * Subsection for warp timer migration is optional, because may not be created
722 */
723static const VMStateDescription icount_vmstate_warp_timer = {
724 .name = "timer/icount/warp_timer",
725 .version_id = 1,
726 .minimum_version_id = 1,
727 .needed = warp_timer_state_needed,
728 .fields = (VMStateField[]) {
729 VMSTATE_INT64(vm_clock_warp_start, TimersState),
730 VMSTATE_TIMER_PTR(icount_warp_timer, TimersState),
731 VMSTATE_END_OF_LIST()
732 }
733};
734
735static const VMStateDescription icount_vmstate_adjust_timers = {
736 .name = "timer/icount/timers",
737 .version_id = 1,
738 .minimum_version_id = 1,
739 .needed = adjust_timers_state_needed,
740 .fields = (VMStateField[]) {
741 VMSTATE_TIMER_PTR(icount_rt_timer, TimersState),
742 VMSTATE_TIMER_PTR(icount_vm_timer, TimersState),
743 VMSTATE_END_OF_LIST()
744 }
745};
746
KONRAD Fredericd09eae32014-08-01 01:37:10 +0200747/*
748 * This is a subsection for icount migration.
749 */
750static const VMStateDescription icount_vmstate_timers = {
751 .name = "timer/icount",
752 .version_id = 1,
753 .minimum_version_id = 1,
Juan Quintela5cd8cad2014-09-23 14:09:54 +0200754 .needed = icount_state_needed,
KONRAD Fredericd09eae32014-08-01 01:37:10 +0200755 .fields = (VMStateField[]) {
756 VMSTATE_INT64(qemu_icount_bias, TimersState),
757 VMSTATE_INT64(qemu_icount, TimersState),
758 VMSTATE_END_OF_LIST()
Pavel Dovgalyukb39e3f32018-01-11 11:26:10 +0300759 },
760 .subsections = (const VMStateDescription*[]) {
761 &icount_vmstate_warp_timer,
762 &icount_vmstate_adjust_timers,
763 NULL
KONRAD Fredericd09eae32014-08-01 01:37:10 +0200764 }
765};
766
Paolo Bonzini946fb272011-09-12 13:57:37 +0200767static const VMStateDescription vmstate_timers = {
768 .name = "timer",
769 .version_id = 2,
770 .minimum_version_id = 1,
Juan Quintela35d08452014-04-16 16:01:33 +0200771 .fields = (VMStateField[]) {
Paolo Bonzini946fb272011-09-12 13:57:37 +0200772 VMSTATE_INT64(cpu_ticks_offset, TimersState),
Paolo Bonzinic1ff0732018-08-14 09:31:58 +0200773 VMSTATE_UNUSED(8),
Paolo Bonzini946fb272011-09-12 13:57:37 +0200774 VMSTATE_INT64_V(cpu_clock_offset, TimersState, 2),
775 VMSTATE_END_OF_LIST()
KONRAD Fredericd09eae32014-08-01 01:37:10 +0200776 },
Juan Quintela5cd8cad2014-09-23 14:09:54 +0200777 .subsections = (const VMStateDescription*[]) {
778 &icount_vmstate_timers,
779 NULL
Paolo Bonzini946fb272011-09-12 13:57:37 +0200780 }
781};
782
Paolo Bonzini14e6fe12016-10-31 10:36:08 +0100783static void cpu_throttle_thread(CPUState *cpu, run_on_cpu_data opaque)
Jason J. Herne2adcc852015-09-08 13:12:33 -0400784{
Jason J. Herne2adcc852015-09-08 13:12:33 -0400785 double pct;
786 double throttle_ratio;
Yury Kotovbd1f7ff2019-09-09 16:13:34 +0300787 int64_t sleeptime_ns, endtime_ns;
Jason J. Herne2adcc852015-09-08 13:12:33 -0400788
789 if (!cpu_throttle_get_percentage()) {
790 return;
791 }
792
793 pct = (double)cpu_throttle_get_percentage()/100;
794 throttle_ratio = pct / (1 - pct);
Yury Kotovbd1f7ff2019-09-09 16:13:34 +0300795 /* Add 1ns to fix double's rounding error (like 0.9999999...) */
796 sleeptime_ns = (int64_t)(throttle_ratio * CPU_THROTTLE_TIMESLICE_NS + 1);
797 endtime_ns = qemu_clock_get_ns(QEMU_CLOCK_REALTIME) + sleeptime_ns;
798 while (sleeptime_ns > 0 && !cpu->stop) {
799 if (sleeptime_ns > SCALE_MS) {
800 qemu_cond_timedwait(cpu->halt_cond, &qemu_global_mutex,
801 sleeptime_ns / SCALE_MS);
802 } else {
803 qemu_mutex_unlock_iothread();
804 g_usleep(sleeptime_ns / SCALE_US);
805 qemu_mutex_lock_iothread();
806 }
807 sleeptime_ns = endtime_ns - qemu_clock_get_ns(QEMU_CLOCK_REALTIME);
808 }
Felipe Franciosi90bb0c02017-05-19 22:29:50 +0100809 atomic_set(&cpu->throttle_thread_scheduled, 0);
Jason J. Herne2adcc852015-09-08 13:12:33 -0400810}
811
812static void cpu_throttle_timer_tick(void *opaque)
813{
814 CPUState *cpu;
815 double pct;
816
817 /* Stop the timer if needed */
818 if (!cpu_throttle_get_percentage()) {
819 return;
820 }
821 CPU_FOREACH(cpu) {
822 if (!atomic_xchg(&cpu->throttle_thread_scheduled, 1)) {
Paolo Bonzini14e6fe12016-10-31 10:36:08 +0100823 async_run_on_cpu(cpu, cpu_throttle_thread,
824 RUN_ON_CPU_NULL);
Jason J. Herne2adcc852015-09-08 13:12:33 -0400825 }
826 }
827
828 pct = (double)cpu_throttle_get_percentage()/100;
829 timer_mod(throttle_timer, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL_RT) +
830 CPU_THROTTLE_TIMESLICE_NS / (1-pct));
831}
832
833void cpu_throttle_set(int new_throttle_pct)
834{
835 /* Ensure throttle percentage is within valid range */
836 new_throttle_pct = MIN(new_throttle_pct, CPU_THROTTLE_PCT_MAX);
837 new_throttle_pct = MAX(new_throttle_pct, CPU_THROTTLE_PCT_MIN);
838
839 atomic_set(&throttle_percentage, new_throttle_pct);
840
841 timer_mod(throttle_timer, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL_RT) +
842 CPU_THROTTLE_TIMESLICE_NS);
843}
844
845void cpu_throttle_stop(void)
846{
847 atomic_set(&throttle_percentage, 0);
848}
849
850bool cpu_throttle_active(void)
851{
852 return (cpu_throttle_get_percentage() != 0);
853}
854
855int cpu_throttle_get_percentage(void)
856{
857 return atomic_read(&throttle_percentage);
858}
859
Pavel Dovgalyuk4603ea02014-09-01 09:34:49 +0400860void cpu_ticks_init(void)
861{
Emilio G. Cotaccdb3c12016-06-08 14:55:20 -0400862 seqlock_init(&timers_state.vm_clock_seqlock);
Emilio G. Cota87a09cd2018-09-03 13:18:29 -0400863 qemu_spin_init(&timers_state.vm_clock_lock);
Pavel Dovgalyuk4603ea02014-09-01 09:34:49 +0400864 vmstate_register(NULL, 0, &vmstate_timers, &timers_state);
Jason J. Herne2adcc852015-09-08 13:12:33 -0400865 throttle_timer = timer_new_ns(QEMU_CLOCK_VIRTUAL_RT,
866 cpu_throttle_timer_tick, NULL);
Pavel Dovgalyuk4603ea02014-09-01 09:34:49 +0400867}
868
Sebastian Tanase1ad95802014-07-25 11:56:28 +0200869void configure_icount(QemuOpts *opts, Error **errp)
Paolo Bonzini946fb272011-09-12 13:57:37 +0200870{
Sebastian Tanase1ad95802014-07-25 11:56:28 +0200871 const char *option;
Sebastian Tanasea8bfac32014-07-25 11:56:29 +0200872 char *rem_str = NULL;
Sebastian Tanase1ad95802014-07-25 11:56:28 +0200873
Sebastian Tanase1ad95802014-07-25 11:56:28 +0200874 option = qemu_opt_get(opts, "shift");
Paolo Bonzini946fb272011-09-12 13:57:37 +0200875 if (!option) {
Sebastian Tanasea8bfac32014-07-25 11:56:29 +0200876 if (qemu_opt_get(opts, "align") != NULL) {
877 error_setg(errp, "Please specify shift option when using align");
878 }
Paolo Bonzini946fb272011-09-12 13:57:37 +0200879 return;
880 }
Victor CLEMENTf1f4b572015-05-29 17:14:05 +0200881
882 icount_sleep = qemu_opt_get_bool(opts, "sleep", true);
Victor CLEMENT5045e9d92015-05-29 17:14:04 +0200883 if (icount_sleep) {
Pavel Dovgalyukb39e3f32018-01-11 11:26:10 +0300884 timers_state.icount_warp_timer = timer_new_ns(QEMU_CLOCK_VIRTUAL_RT,
Pavel Dovgalyuke76d1792016-03-10 14:56:09 +0300885 icount_timer_cb, NULL);
Victor CLEMENT5045e9d92015-05-29 17:14:04 +0200886 }
Victor CLEMENTf1f4b572015-05-29 17:14:05 +0200887
Sebastian Tanasea8bfac32014-07-25 11:56:29 +0200888 icount_align_option = qemu_opt_get_bool(opts, "align", false);
Victor CLEMENTf1f4b572015-05-29 17:14:05 +0200889
890 if (icount_align_option && !icount_sleep) {
Pranith Kumar778d9f92016-02-26 10:16:51 -0500891 error_setg(errp, "align=on and sleep=off are incompatible");
Victor CLEMENTf1f4b572015-05-29 17:14:05 +0200892 }
Paolo Bonzini946fb272011-09-12 13:57:37 +0200893 if (strcmp(option, "auto") != 0) {
Sebastian Tanasea8bfac32014-07-25 11:56:29 +0200894 errno = 0;
Paolo Bonzinic1ff0732018-08-14 09:31:58 +0200895 timers_state.icount_time_shift = strtol(option, &rem_str, 0);
Sebastian Tanasea8bfac32014-07-25 11:56:29 +0200896 if (errno != 0 || *rem_str != '\0' || !strlen(option)) {
897 error_setg(errp, "icount: Invalid shift value");
898 }
Paolo Bonzini946fb272011-09-12 13:57:37 +0200899 use_icount = 1;
900 return;
Sebastian Tanasea8bfac32014-07-25 11:56:29 +0200901 } else if (icount_align_option) {
902 error_setg(errp, "shift=auto and align=on are incompatible");
Victor CLEMENTf1f4b572015-05-29 17:14:05 +0200903 } else if (!icount_sleep) {
Pranith Kumar778d9f92016-02-26 10:16:51 -0500904 error_setg(errp, "shift=auto and sleep=off are incompatible");
Paolo Bonzini946fb272011-09-12 13:57:37 +0200905 }
906
907 use_icount = 2;
908
909 /* 125MIPS seems a reasonable initial guess at the guest speed.
910 It will be corrected fairly quickly anyway. */
Paolo Bonzinic1ff0732018-08-14 09:31:58 +0200911 timers_state.icount_time_shift = 3;
Paolo Bonzini946fb272011-09-12 13:57:37 +0200912
913 /* Have both realtime and virtual time triggers for speed adjustment.
914 The realtime trigger catches emulated time passing too slowly,
915 the virtual time trigger catches emulated time passing too fast.
916 Realtime triggers occur even when idle, so use them less frequently
917 than VM triggers. */
Pavel Dovgalyukb39e3f32018-01-11 11:26:10 +0300918 timers_state.vm_clock_warp_start = -1;
919 timers_state.icount_rt_timer = timer_new_ms(QEMU_CLOCK_VIRTUAL_RT,
Pavel Dovgalyukbf2a7dd2014-11-26 13:40:55 +0300920 icount_adjust_rt, NULL);
Pavel Dovgalyukb39e3f32018-01-11 11:26:10 +0300921 timer_mod(timers_state.icount_rt_timer,
Pavel Dovgalyukbf2a7dd2014-11-26 13:40:55 +0300922 qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL_RT) + 1000);
Pavel Dovgalyukb39e3f32018-01-11 11:26:10 +0300923 timers_state.icount_vm_timer = timer_new_ns(QEMU_CLOCK_VIRTUAL,
Alex Bligh40daca52013-08-21 16:03:02 +0100924 icount_adjust_vm, NULL);
Pavel Dovgalyukb39e3f32018-01-11 11:26:10 +0300925 timer_mod(timers_state.icount_vm_timer,
Alex Bligh40daca52013-08-21 16:03:02 +0100926 qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) +
Rutuja Shah73bcb242016-03-21 21:32:30 +0530927 NANOSECONDS_PER_SECOND / 10);
Paolo Bonzini946fb272011-09-12 13:57:37 +0200928}
929
930/***********************************************************/
Alex Bennée65467062017-02-23 18:29:09 +0000931/* TCG vCPU kick timer
932 *
933 * The kick timer is responsible for moving single threaded vCPU
934 * emulation on to the next vCPU. If more than one vCPU is running a
935 * timer event with force a cpu->exit so the next vCPU can get
936 * scheduled.
937 *
938 * The timer is removed if all vCPUs are idle and restarted again once
939 * idleness is complete.
940 */
941
942static QEMUTimer *tcg_kick_vcpu_timer;
Alex Bennée791158d2017-02-23 18:29:10 +0000943static CPUState *tcg_current_rr_cpu;
Alex Bennée65467062017-02-23 18:29:09 +0000944
945#define TCG_KICK_PERIOD (NANOSECONDS_PER_SECOND / 10)
946
947static inline int64_t qemu_tcg_next_kick(void)
948{
949 return qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) + TCG_KICK_PERIOD;
950}
951
Alex Bennéee8f22f72019-10-01 17:04:26 +0100952/* Kick the currently round-robin scheduled vCPU to next */
953static void qemu_cpu_kick_rr_next_cpu(void)
Alex Bennée791158d2017-02-23 18:29:10 +0000954{
955 CPUState *cpu;
Alex Bennée791158d2017-02-23 18:29:10 +0000956 do {
957 cpu = atomic_mb_read(&tcg_current_rr_cpu);
958 if (cpu) {
959 cpu_exit(cpu);
960 }
961 } while (cpu != atomic_mb_read(&tcg_current_rr_cpu));
962}
963
Alex Bennéee8f22f72019-10-01 17:04:26 +0100964/* Kick all RR vCPUs */
965static void qemu_cpu_kick_rr_cpus(void)
966{
967 CPUState *cpu;
968
969 CPU_FOREACH(cpu) {
970 cpu_exit(cpu);
971 };
972}
973
Paolo Bonzini6b8f0182017-03-02 19:56:40 +0100974static void do_nothing(CPUState *cpu, run_on_cpu_data unused)
975{
976}
977
Paolo Bonzini3f53bc62017-03-03 11:50:29 +0100978void qemu_timer_notify_cb(void *opaque, QEMUClockType type)
979{
Paolo Bonzini6b8f0182017-03-02 19:56:40 +0100980 if (!use_icount || type != QEMU_CLOCK_VIRTUAL) {
981 qemu_notify_event();
982 return;
983 }
984
Peter Maydellc52e7132018-04-10 13:02:25 +0100985 if (qemu_in_vcpu_thread()) {
986 /* A CPU is currently running; kick it back out to the
987 * tcg_cpu_exec() loop so it will recalculate its
988 * icount deadline immediately.
989 */
990 qemu_cpu_kick(current_cpu);
991 } else if (first_cpu) {
Paolo Bonzini6b8f0182017-03-02 19:56:40 +0100992 /* qemu_cpu_kick is not enough to kick a halted CPU out of
993 * qemu_tcg_wait_io_event. async_run_on_cpu, instead,
994 * causes cpu_thread_is_idle to return false. This way,
995 * handle_icount_deadline can run.
Peter Maydellc52e7132018-04-10 13:02:25 +0100996 * If we have no CPUs at all for some reason, we don't
997 * need to do anything.
Paolo Bonzini6b8f0182017-03-02 19:56:40 +0100998 */
999 async_run_on_cpu(first_cpu, do_nothing, RUN_ON_CPU_NULL);
1000 }
Paolo Bonzini3f53bc62017-03-03 11:50:29 +01001001}
1002
Alex Bennée65467062017-02-23 18:29:09 +00001003static void kick_tcg_thread(void *opaque)
1004{
1005 timer_mod(tcg_kick_vcpu_timer, qemu_tcg_next_kick());
Alex Bennéee8f22f72019-10-01 17:04:26 +01001006 qemu_cpu_kick_rr_next_cpu();
Alex Bennée65467062017-02-23 18:29:09 +00001007}
1008
1009static void start_tcg_kick_timer(void)
1010{
Paolo Bonzinidb08b682018-01-11 13:53:12 +01001011 assert(!mttcg_enabled);
1012 if (!tcg_kick_vcpu_timer && CPU_NEXT(first_cpu)) {
Alex Bennée65467062017-02-23 18:29:09 +00001013 tcg_kick_vcpu_timer = timer_new_ns(QEMU_CLOCK_VIRTUAL,
1014 kick_tcg_thread, NULL);
Alex Bennée1926ab22018-09-27 18:17:24 +01001015 }
1016 if (tcg_kick_vcpu_timer && !timer_pending(tcg_kick_vcpu_timer)) {
Alex Bennée65467062017-02-23 18:29:09 +00001017 timer_mod(tcg_kick_vcpu_timer, qemu_tcg_next_kick());
1018 }
1019}
1020
1021static void stop_tcg_kick_timer(void)
1022{
Paolo Bonzinidb08b682018-01-11 13:53:12 +01001023 assert(!mttcg_enabled);
Alex Bennée1926ab22018-09-27 18:17:24 +01001024 if (tcg_kick_vcpu_timer && timer_pending(tcg_kick_vcpu_timer)) {
Alex Bennée65467062017-02-23 18:29:09 +00001025 timer_del(tcg_kick_vcpu_timer);
Alex Bennée65467062017-02-23 18:29:09 +00001026 }
1027}
1028
Alex Bennée65467062017-02-23 18:29:09 +00001029/***********************************************************/
Blue Swirl296af7c2010-03-29 19:23:50 +00001030void hw_error(const char *fmt, ...)
1031{
1032 va_list ap;
Andreas Färber55e5c282012-12-17 06:18:02 +01001033 CPUState *cpu;
Blue Swirl296af7c2010-03-29 19:23:50 +00001034
1035 va_start(ap, fmt);
1036 fprintf(stderr, "qemu: hardware error: ");
1037 vfprintf(stderr, fmt, ap);
1038 fprintf(stderr, "\n");
Andreas Färberbdc44642013-06-24 23:50:24 +02001039 CPU_FOREACH(cpu) {
Andreas Färber55e5c282012-12-17 06:18:02 +01001040 fprintf(stderr, "CPU #%d:\n", cpu->cpu_index);
Markus Armbruster90c84c52019-04-17 21:18:02 +02001041 cpu_dump_state(cpu, stderr, CPU_DUMP_FPU);
Blue Swirl296af7c2010-03-29 19:23:50 +00001042 }
1043 va_end(ap);
1044 abort();
1045}
1046
1047void cpu_synchronize_all_states(void)
1048{
Andreas Färber182735e2013-05-29 22:29:20 +02001049 CPUState *cpu;
Blue Swirl296af7c2010-03-29 19:23:50 +00001050
Andreas Färberbdc44642013-06-24 23:50:24 +02001051 CPU_FOREACH(cpu) {
Andreas Färber182735e2013-05-29 22:29:20 +02001052 cpu_synchronize_state(cpu);
Sergio Andres Gomez Del Realc97d6d22017-09-13 04:05:09 -05001053 /* TODO: move to cpu_synchronize_state() */
1054 if (hvf_enabled()) {
1055 hvf_cpu_synchronize_state(cpu);
1056 }
Blue Swirl296af7c2010-03-29 19:23:50 +00001057 }
1058}
1059
1060void cpu_synchronize_all_post_reset(void)
1061{
Andreas Färber182735e2013-05-29 22:29:20 +02001062 CPUState *cpu;
Blue Swirl296af7c2010-03-29 19:23:50 +00001063
Andreas Färberbdc44642013-06-24 23:50:24 +02001064 CPU_FOREACH(cpu) {
Andreas Färber182735e2013-05-29 22:29:20 +02001065 cpu_synchronize_post_reset(cpu);
Sergio Andres Gomez Del Realc97d6d22017-09-13 04:05:09 -05001066 /* TODO: move to cpu_synchronize_post_reset() */
1067 if (hvf_enabled()) {
1068 hvf_cpu_synchronize_post_reset(cpu);
1069 }
Blue Swirl296af7c2010-03-29 19:23:50 +00001070 }
1071}
1072
1073void cpu_synchronize_all_post_init(void)
1074{
Andreas Färber182735e2013-05-29 22:29:20 +02001075 CPUState *cpu;
Blue Swirl296af7c2010-03-29 19:23:50 +00001076
Andreas Färberbdc44642013-06-24 23:50:24 +02001077 CPU_FOREACH(cpu) {
Andreas Färber182735e2013-05-29 22:29:20 +02001078 cpu_synchronize_post_init(cpu);
Sergio Andres Gomez Del Realc97d6d22017-09-13 04:05:09 -05001079 /* TODO: move to cpu_synchronize_post_init() */
1080 if (hvf_enabled()) {
1081 hvf_cpu_synchronize_post_init(cpu);
1082 }
Blue Swirl296af7c2010-03-29 19:23:50 +00001083 }
1084}
1085
David Gibson75e972d2017-05-26 14:46:28 +10001086void cpu_synchronize_all_pre_loadvm(void)
1087{
1088 CPUState *cpu;
1089
1090 CPU_FOREACH(cpu) {
1091 cpu_synchronize_pre_loadvm(cpu);
1092 }
1093}
1094
Stefan Hajnoczi4486e892018-03-07 14:42:05 +00001095static int do_vm_stop(RunState state, bool send_stop)
Blue Swirl296af7c2010-03-29 19:23:50 +00001096{
Kevin Wolf56983462013-07-05 13:49:54 +02001097 int ret = 0;
1098
Luiz Capitulino13548692011-07-29 15:36:43 -03001099 if (runstate_is_running()) {
Blue Swirl296af7c2010-03-29 19:23:50 +00001100 cpu_disable_ticks();
Blue Swirl296af7c2010-03-29 19:23:50 +00001101 pause_all_vcpus();
Luiz Capitulinof5bbfba2011-07-29 15:04:45 -03001102 runstate_set(state);
Luiz Capitulino1dfb4dd2011-07-29 14:26:33 -03001103 vm_state_notify(0, state);
Stefan Hajnoczi4486e892018-03-07 14:42:05 +00001104 if (send_stop) {
Peter Xu3ab72382018-08-15 21:37:37 +08001105 qapi_event_send_stop();
Stefan Hajnoczi4486e892018-03-07 14:42:05 +00001106 }
Blue Swirl296af7c2010-03-29 19:23:50 +00001107 }
Kevin Wolf56983462013-07-05 13:49:54 +02001108
Kevin Wolf594a45c2013-07-18 14:52:19 +02001109 bdrv_drain_all();
Pavel Dovgalyuk6d0ceb82016-09-26 11:08:16 +03001110 replay_disable_events();
John Snow22af08e2016-09-22 21:45:51 -04001111 ret = bdrv_flush_all();
Kevin Wolf594a45c2013-07-18 14:52:19 +02001112
Kevin Wolf56983462013-07-05 13:49:54 +02001113 return ret;
Blue Swirl296af7c2010-03-29 19:23:50 +00001114}
1115
Stefan Hajnoczi4486e892018-03-07 14:42:05 +00001116/* Special vm_stop() variant for terminating the process. Historically clients
1117 * did not expect a QMP STOP event and so we need to retain compatibility.
1118 */
1119int vm_shutdown(void)
1120{
1121 return do_vm_stop(RUN_STATE_SHUTDOWN, false);
1122}
1123
Andreas Färbera1fcaa72012-05-02 23:42:26 +02001124static bool cpu_can_run(CPUState *cpu)
Blue Swirl296af7c2010-03-29 19:23:50 +00001125{
Andreas Färber4fdeee72012-05-02 23:10:09 +02001126 if (cpu->stop) {
Andreas Färbera1fcaa72012-05-02 23:42:26 +02001127 return false;
Jan Kiszka0ab07c62011-02-07 12:19:14 +01001128 }
Tiejun Chen321bc0b2013-08-02 09:43:09 +08001129 if (cpu_is_stopped(cpu)) {
Andreas Färbera1fcaa72012-05-02 23:42:26 +02001130 return false;
Jan Kiszka0ab07c62011-02-07 12:19:14 +01001131 }
Andreas Färbera1fcaa72012-05-02 23:42:26 +02001132 return true;
Blue Swirl296af7c2010-03-29 19:23:50 +00001133}
1134
Andreas Färber91325042013-05-27 02:07:49 +02001135static void cpu_handle_guest_debug(CPUState *cpu)
Jan Kiszka3c638d02010-06-25 16:56:56 +02001136{
Andreas Färber64f6b342013-05-27 02:06:09 +02001137 gdb_set_stop_cpu(cpu);
Jan Kiszka8cf71712011-02-07 12:19:16 +01001138 qemu_system_debug_request();
Andreas Färberf324e762012-05-02 23:26:21 +02001139 cpu->stopped = true;
Jan Kiszka3c638d02010-06-25 16:56:56 +02001140}
1141
Jan Kiszka6d9cb732011-02-01 22:15:58 +01001142#ifdef CONFIG_LINUX
1143static void sigbus_reraise(void)
1144{
1145 sigset_t set;
1146 struct sigaction action;
1147
1148 memset(&action, 0, sizeof(action));
1149 action.sa_handler = SIG_DFL;
1150 if (!sigaction(SIGBUS, &action, NULL)) {
1151 raise(SIGBUS);
1152 sigemptyset(&set);
1153 sigaddset(&set, SIGBUS);
Peter Maydella2d17612016-05-16 18:33:59 +01001154 pthread_sigmask(SIG_UNBLOCK, &set, NULL);
Jan Kiszka6d9cb732011-02-01 22:15:58 +01001155 }
1156 perror("Failed to re-raise SIGBUS!\n");
1157 abort();
1158}
1159
Paolo Bonzinid98d4072017-02-08 13:22:12 +01001160static void sigbus_handler(int n, siginfo_t *siginfo, void *ctx)
Jan Kiszka6d9cb732011-02-01 22:15:58 +01001161{
Paolo Bonzinia16fc072017-02-09 09:50:02 +01001162 if (siginfo->si_code != BUS_MCEERR_AO && siginfo->si_code != BUS_MCEERR_AR) {
1163 sigbus_reraise();
1164 }
1165
Paolo Bonzini2ae41db2017-02-08 12:48:54 +01001166 if (current_cpu) {
1167 /* Called asynchronously in VCPU thread. */
1168 if (kvm_on_sigbus_vcpu(current_cpu, siginfo->si_code, siginfo->si_addr)) {
1169 sigbus_reraise();
1170 }
1171 } else {
1172 /* Called synchronously (via signalfd) in main thread. */
1173 if (kvm_on_sigbus(siginfo->si_code, siginfo->si_addr)) {
1174 sigbus_reraise();
1175 }
Jan Kiszka6d9cb732011-02-01 22:15:58 +01001176 }
1177}
1178
1179static void qemu_init_sigbus(void)
1180{
1181 struct sigaction action;
1182
1183 memset(&action, 0, sizeof(action));
1184 action.sa_flags = SA_SIGINFO;
Paolo Bonzinid98d4072017-02-08 13:22:12 +01001185 action.sa_sigaction = sigbus_handler;
Jan Kiszka6d9cb732011-02-01 22:15:58 +01001186 sigaction(SIGBUS, &action, NULL);
1187
1188 prctl(PR_MCE_KILL, PR_MCE_KILL_SET, PR_MCE_KILL_EARLY, 0, 0);
1189}
Paolo Bonzinia16fc072017-02-09 09:50:02 +01001190#else /* !CONFIG_LINUX */
1191static void qemu_init_sigbus(void)
1192{
1193}
Paolo Bonzinia16fc072017-02-09 09:50:02 +01001194#endif /* !CONFIG_LINUX */
Blue Swirl296af7c2010-03-29 19:23:50 +00001195
Blue Swirl296af7c2010-03-29 19:23:50 +00001196static QemuThread io_thread;
1197
Blue Swirl296af7c2010-03-29 19:23:50 +00001198/* cpu creation */
1199static QemuCond qemu_cpu_cond;
1200/* system init */
Blue Swirl296af7c2010-03-29 19:23:50 +00001201static QemuCond qemu_pause_cond;
1202
Paolo Bonzinid3b12f52011-09-13 10:30:52 +02001203void qemu_init_cpu_loop(void)
Blue Swirl296af7c2010-03-29 19:23:50 +00001204{
Jan Kiszka6d9cb732011-02-01 22:15:58 +01001205 qemu_init_sigbus();
Anthony Liguoried945922011-02-08 18:18:18 +01001206 qemu_cond_init(&qemu_cpu_cond);
Anthony Liguoried945922011-02-08 18:18:18 +01001207 qemu_cond_init(&qemu_pause_cond);
Blue Swirl296af7c2010-03-29 19:23:50 +00001208 qemu_mutex_init(&qemu_global_mutex);
Blue Swirl296af7c2010-03-29 19:23:50 +00001209
Jan Kiszkab7680cb2011-03-12 17:43:51 +01001210 qemu_thread_get_self(&io_thread);
Blue Swirl296af7c2010-03-29 19:23:50 +00001211}
1212
Paolo Bonzini14e6fe12016-10-31 10:36:08 +01001213void run_on_cpu(CPUState *cpu, run_on_cpu_func func, run_on_cpu_data data)
Marcelo Tosattie82bcec2010-05-04 09:45:22 -03001214{
Sergey Fedorovd148d902016-08-29 09:51:00 +02001215 do_run_on_cpu(cpu, func, data, &qemu_global_mutex);
Chegu Vinod3c022702013-06-24 03:49:41 -06001216}
1217
Gu Zheng4c055ab2016-05-12 09:18:13 +05301218static void qemu_kvm_destroy_vcpu(CPUState *cpu)
1219{
1220 if (kvm_destroy_vcpu(cpu) < 0) {
1221 error_report("kvm_destroy_vcpu failed");
1222 exit(EXIT_FAILURE);
1223 }
1224}
1225
1226static void qemu_tcg_destroy_vcpu(CPUState *cpu)
1227{
1228}
1229
David Hildenbrandebd05fe2017-11-29 20:12:15 +01001230static void qemu_cpu_stop(CPUState *cpu, bool exit)
1231{
1232 g_assert(qemu_cpu_is_self(cpu));
1233 cpu->stop = false;
1234 cpu->stopped = true;
1235 if (exit) {
1236 cpu_exit(cpu);
1237 }
1238 qemu_cond_broadcast(&qemu_pause_cond);
1239}
1240
Andreas Färber509a0d72012-05-03 02:18:09 +02001241static void qemu_wait_io_event_common(CPUState *cpu)
Blue Swirl296af7c2010-03-29 19:23:50 +00001242{
Alex Bennée37257942017-02-23 18:29:14 +00001243 atomic_mb_set(&cpu->thread_kicked, false);
Andreas Färber4fdeee72012-05-02 23:10:09 +02001244 if (cpu->stop) {
David Hildenbrandebd05fe2017-11-29 20:12:15 +01001245 qemu_cpu_stop(cpu, false);
Blue Swirl296af7c2010-03-29 19:23:50 +00001246 }
Sergey Fedorova5403c62016-08-02 18:27:36 +01001247 process_queued_cpu_work(cpu);
Alex Bennée37257942017-02-23 18:29:14 +00001248}
1249
Paolo Bonzinia8efa602018-11-14 12:36:57 +01001250static void qemu_tcg_rr_wait_io_event(void)
Alex Bennée37257942017-02-23 18:29:14 +00001251{
Paolo Bonzinia8efa602018-11-14 12:36:57 +01001252 CPUState *cpu;
1253
Paolo Bonzinidb08b682018-01-11 13:53:12 +01001254 while (all_cpu_threads_idle()) {
Alex Bennée65467062017-02-23 18:29:09 +00001255 stop_tcg_kick_timer();
Paolo Bonzinia8efa602018-11-14 12:36:57 +01001256 qemu_cond_wait(first_cpu->halt_cond, &qemu_global_mutex);
Jan Kiszka16400322011-02-09 16:29:37 +01001257 }
Blue Swirl296af7c2010-03-29 19:23:50 +00001258
Alex Bennée65467062017-02-23 18:29:09 +00001259 start_tcg_kick_timer();
1260
Paolo Bonzinia8efa602018-11-14 12:36:57 +01001261 CPU_FOREACH(cpu) {
1262 qemu_wait_io_event_common(cpu);
1263 }
Blue Swirl296af7c2010-03-29 19:23:50 +00001264}
1265
Paolo Bonzinidb08b682018-01-11 13:53:12 +01001266static void qemu_wait_io_event(CPUState *cpu)
Blue Swirl296af7c2010-03-29 19:23:50 +00001267{
Andreas Färbera98ae1d2013-05-26 23:21:08 +02001268 while (cpu_thread_is_idle(cpu)) {
Andreas Färberf5c121b2012-05-03 01:22:49 +02001269 qemu_cond_wait(cpu->halt_cond, &qemu_global_mutex);
Jan Kiszka16400322011-02-09 16:29:37 +01001270 }
Blue Swirl296af7c2010-03-29 19:23:50 +00001271
Paolo Bonzinidb08b682018-01-11 13:53:12 +01001272#ifdef _WIN32
1273 /* Eat dummy APC queued by qemu_cpu_kick_thread. */
1274 if (!tcg_enabled()) {
1275 SleepEx(0, TRUE);
Sergio Andres Gomez Del Realc97d6d22017-09-13 04:05:09 -05001276 }
Paolo Bonzinidb08b682018-01-11 13:53:12 +01001277#endif
Sergio Andres Gomez Del Realc97d6d22017-09-13 04:05:09 -05001278 qemu_wait_io_event_common(cpu);
1279}
1280
Jan Kiszka7e97cd82011-02-07 12:19:12 +01001281static void *qemu_kvm_cpu_thread_fn(void *arg)
Blue Swirl296af7c2010-03-29 19:23:50 +00001282{
Andreas Färber48a106b2013-05-27 02:20:39 +02001283 CPUState *cpu = arg;
Jan Kiszka84b49152011-02-01 22:15:50 +01001284 int r;
Blue Swirl296af7c2010-03-29 19:23:50 +00001285
Paolo Bonziniab28bd22015-07-09 08:55:38 +02001286 rcu_register_thread();
1287
Paolo Bonzini2e7f7a32015-06-18 18:47:18 +02001288 qemu_mutex_lock_iothread();
Andreas Färber814e6122012-05-02 17:00:37 +02001289 qemu_thread_get_self(cpu->thread);
Andreas Färber9f09e182012-05-03 06:59:07 +02001290 cpu->thread_id = qemu_get_thread_id();
Pavel Dovgalyuk626cf8f2014-12-08 10:53:17 +03001291 cpu->can_do_io = 1;
Andreas Färber4917cf42013-05-27 05:17:50 +02001292 current_cpu = cpu;
Blue Swirl296af7c2010-03-29 19:23:50 +00001293
Andreas Färber504134d2012-12-17 06:38:45 +01001294 r = kvm_init_vcpu(cpu);
Jan Kiszka84b49152011-02-01 22:15:50 +01001295 if (r < 0) {
Alistair Francis493d89b2018-02-03 09:43:14 +01001296 error_report("kvm_init_vcpu failed: %s", strerror(-r));
Jan Kiszka84b49152011-02-01 22:15:50 +01001297 exit(1);
1298 }
Blue Swirl296af7c2010-03-29 19:23:50 +00001299
Paolo Bonzini18268b62017-02-09 09:41:14 +01001300 kvm_init_cpu_signals(cpu);
Blue Swirl296af7c2010-03-29 19:23:50 +00001301
1302 /* signal CPU creation */
Andreas Färber61a46212012-05-02 22:49:36 +02001303 cpu->created = true;
Blue Swirl296af7c2010-03-29 19:23:50 +00001304 qemu_cond_signal(&qemu_cpu_cond);
Richard Henderson9c09a252019-03-14 13:06:29 -07001305 qemu_guest_random_seed_thread_part2(cpu->random_seed);
Blue Swirl296af7c2010-03-29 19:23:50 +00001306
Gu Zheng4c055ab2016-05-12 09:18:13 +05301307 do {
Andreas Färbera1fcaa72012-05-02 23:42:26 +02001308 if (cpu_can_run(cpu)) {
Andreas Färber1458c362013-05-26 23:46:55 +02001309 r = kvm_cpu_exec(cpu);
Jan Kiszka83f338f2011-02-07 12:19:17 +01001310 if (r == EXCP_DEBUG) {
Andreas Färber91325042013-05-27 02:07:49 +02001311 cpu_handle_guest_debug(cpu);
Jan Kiszka83f338f2011-02-07 12:19:17 +01001312 }
Jan Kiszka0ab07c62011-02-07 12:19:14 +01001313 }
Paolo Bonzinidb08b682018-01-11 13:53:12 +01001314 qemu_wait_io_event(cpu);
Gu Zheng4c055ab2016-05-12 09:18:13 +05301315 } while (!cpu->unplug || cpu_can_run(cpu));
Blue Swirl296af7c2010-03-29 19:23:50 +00001316
Gu Zheng4c055ab2016-05-12 09:18:13 +05301317 qemu_kvm_destroy_vcpu(cpu);
Bharata B Rao2c579042016-05-12 09:18:14 +05301318 cpu->created = false;
1319 qemu_cond_signal(&qemu_cpu_cond);
Gu Zheng4c055ab2016-05-12 09:18:13 +05301320 qemu_mutex_unlock_iothread();
Paolo Bonzini57615ed2018-01-30 11:04:36 -05001321 rcu_unregister_thread();
Blue Swirl296af7c2010-03-29 19:23:50 +00001322 return NULL;
1323}
1324
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +02001325static void *qemu_dummy_cpu_thread_fn(void *arg)
1326{
1327#ifdef _WIN32
Alistair Francis493d89b2018-02-03 09:43:14 +01001328 error_report("qtest is not supported under Windows");
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +02001329 exit(1);
1330#else
Andreas Färber10a90212013-05-27 02:24:35 +02001331 CPUState *cpu = arg;
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +02001332 sigset_t waitset;
1333 int r;
1334
Paolo Bonziniab28bd22015-07-09 08:55:38 +02001335 rcu_register_thread();
1336
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +02001337 qemu_mutex_lock_iothread();
Andreas Färber814e6122012-05-02 17:00:37 +02001338 qemu_thread_get_self(cpu->thread);
Andreas Färber9f09e182012-05-03 06:59:07 +02001339 cpu->thread_id = qemu_get_thread_id();
Pavel Dovgalyuk626cf8f2014-12-08 10:53:17 +03001340 cpu->can_do_io = 1;
Alex Bennée37257942017-02-23 18:29:14 +00001341 current_cpu = cpu;
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +02001342
1343 sigemptyset(&waitset);
1344 sigaddset(&waitset, SIG_IPI);
1345
1346 /* signal CPU creation */
Andreas Färber61a46212012-05-02 22:49:36 +02001347 cpu->created = true;
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +02001348 qemu_cond_signal(&qemu_cpu_cond);
Richard Henderson9c09a252019-03-14 13:06:29 -07001349 qemu_guest_random_seed_thread_part2(cpu->random_seed);
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +02001350
Paolo Bonzinid2831ab2018-01-30 11:04:53 -05001351 do {
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +02001352 qemu_mutex_unlock_iothread();
1353 do {
1354 int sig;
1355 r = sigwait(&waitset, &sig);
1356 } while (r == -1 && (errno == EAGAIN || errno == EINTR));
1357 if (r == -1) {
1358 perror("sigwait");
1359 exit(1);
1360 }
1361 qemu_mutex_lock_iothread();
Paolo Bonzinidb08b682018-01-11 13:53:12 +01001362 qemu_wait_io_event(cpu);
Paolo Bonzinid2831ab2018-01-30 11:04:53 -05001363 } while (!cpu->unplug);
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +02001364
David Hildenbrandd40bfcb2019-02-18 10:21:57 +01001365 qemu_mutex_unlock_iothread();
Paolo Bonzinid2831ab2018-01-30 11:04:53 -05001366 rcu_unregister_thread();
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +02001367 return NULL;
1368#endif
1369}
1370
Alex Bennée1be7fcb2016-10-27 16:10:08 +01001371static int64_t tcg_get_icount_limit(void)
1372{
1373 int64_t deadline;
1374
1375 if (replay_mode != REPLAY_MODE_PLAY) {
Pavel Dovgalyukdcb15782019-07-25 11:44:26 +03001376 /*
1377 * Include all the timers, because they may need an attention.
1378 * Too long CPU execution may create unnecessary delay in UI.
1379 */
1380 deadline = qemu_clock_deadline_ns_all(QEMU_CLOCK_VIRTUAL,
1381 QEMU_TIMER_ATTR_ALL);
Alex Bennée1be7fcb2016-10-27 16:10:08 +01001382
1383 /* Maintain prior (possibly buggy) behaviour where if no deadline
1384 * was set (as there is no QEMU_CLOCK_VIRTUAL timer) or it is more than
1385 * INT32_MAX nanoseconds ahead, we still use INT32_MAX
1386 * nanoseconds.
1387 */
1388 if ((deadline < 0) || (deadline > INT32_MAX)) {
1389 deadline = INT32_MAX;
1390 }
1391
1392 return qemu_icount_round(deadline);
1393 } else {
1394 return replay_get_instructions();
1395 }
1396}
1397
Alex Bennée12e97002016-10-27 16:10:14 +01001398static void handle_icount_deadline(void)
1399{
Paolo Bonzini6b8f0182017-03-02 19:56:40 +01001400 assert(qemu_in_vcpu_thread());
Alex Bennée12e97002016-10-27 16:10:14 +01001401 if (use_icount) {
Pavel Dovgalyukdcb15782019-07-25 11:44:26 +03001402 int64_t deadline = qemu_clock_deadline_ns_all(QEMU_CLOCK_VIRTUAL,
1403 QEMU_TIMER_ATTR_ALL);
Alex Bennée12e97002016-10-27 16:10:14 +01001404
1405 if (deadline == 0) {
Paolo Bonzini6b8f0182017-03-02 19:56:40 +01001406 /* Wake up other AioContexts. */
Alex Bennée12e97002016-10-27 16:10:14 +01001407 qemu_clock_notify(QEMU_CLOCK_VIRTUAL);
Paolo Bonzini6b8f0182017-03-02 19:56:40 +01001408 qemu_clock_run_timers(QEMU_CLOCK_VIRTUAL);
Alex Bennée12e97002016-10-27 16:10:14 +01001409 }
1410 }
1411}
1412
Alex Bennée05248382017-03-29 16:46:59 +01001413static void prepare_icount_for_run(CPUState *cpu)
1414{
1415 if (use_icount) {
Alex Bennéeeda5f7c2017-04-05 12:35:48 +01001416 int insns_left;
Alex Bennée05248382017-03-29 16:46:59 +01001417
1418 /* These should always be cleared by process_icount_data after
1419 * each vCPU execution. However u16.high can be raised
1420 * asynchronously by cpu_exit/cpu_interrupt/tcg_handle_interrupt
1421 */
Richard Henderson5e140192019-03-28 11:54:23 -10001422 g_assert(cpu_neg(cpu)->icount_decr.u16.low == 0);
Alex Bennée05248382017-03-29 16:46:59 +01001423 g_assert(cpu->icount_extra == 0);
1424
Alex Bennéeeda5f7c2017-04-05 12:35:48 +01001425 cpu->icount_budget = tcg_get_icount_limit();
1426 insns_left = MIN(0xffff, cpu->icount_budget);
Richard Henderson5e140192019-03-28 11:54:23 -10001427 cpu_neg(cpu)->icount_decr.u16.low = insns_left;
Alex Bennéeeda5f7c2017-04-05 12:35:48 +01001428 cpu->icount_extra = cpu->icount_budget - insns_left;
Alex Bennéed759c952018-02-27 12:52:48 +03001429
1430 replay_mutex_lock();
Alex Bennée05248382017-03-29 16:46:59 +01001431 }
1432}
1433
1434static void process_icount_data(CPUState *cpu)
1435{
1436 if (use_icount) {
Alex Bennéee4cd9652017-03-31 16:09:42 +01001437 /* Account for executed instructions */
Alex Bennée512d3c82017-04-05 12:32:37 +01001438 cpu_update_icount(cpu);
Alex Bennée05248382017-03-29 16:46:59 +01001439
1440 /* Reset the counters */
Richard Henderson5e140192019-03-28 11:54:23 -10001441 cpu_neg(cpu)->icount_decr.u16.low = 0;
Alex Bennée05248382017-03-29 16:46:59 +01001442 cpu->icount_extra = 0;
Alex Bennéee4cd9652017-03-31 16:09:42 +01001443 cpu->icount_budget = 0;
1444
Alex Bennée05248382017-03-29 16:46:59 +01001445 replay_account_executed_instructions();
Alex Bennéed759c952018-02-27 12:52:48 +03001446
1447 replay_mutex_unlock();
Alex Bennée05248382017-03-29 16:46:59 +01001448 }
1449}
1450
1451
Alex Bennée1be7fcb2016-10-27 16:10:08 +01001452static int tcg_cpu_exec(CPUState *cpu)
1453{
1454 int ret;
1455#ifdef CONFIG_PROFILER
1456 int64_t ti;
1457#endif
1458
Emilio G. Cotaf28d0df2018-06-22 13:45:31 -04001459 assert(tcg_enabled());
Alex Bennée1be7fcb2016-10-27 16:10:08 +01001460#ifdef CONFIG_PROFILER
1461 ti = profile_getclock();
1462#endif
Alex Bennée1be7fcb2016-10-27 16:10:08 +01001463 cpu_exec_start(cpu);
1464 ret = cpu_exec(cpu);
1465 cpu_exec_end(cpu);
1466#ifdef CONFIG_PROFILER
Emilio G. Cota72fd2ef2018-10-10 10:48:53 -04001467 atomic_set(&tcg_ctx->prof.cpu_exec_time,
1468 tcg_ctx->prof.cpu_exec_time + profile_getclock() - ti);
Alex Bennée1be7fcb2016-10-27 16:10:08 +01001469#endif
Alex Bennée1be7fcb2016-10-27 16:10:08 +01001470 return ret;
1471}
1472
Alex Bennéec93bbbe2016-10-27 16:10:09 +01001473/* Destroy any remaining vCPUs which have been unplugged and have
1474 * finished running
1475 */
1476static void deal_with_unplugged_cpus(void)
Alex Bennée1be7fcb2016-10-27 16:10:08 +01001477{
Alex Bennéec93bbbe2016-10-27 16:10:09 +01001478 CPUState *cpu;
Alex Bennée1be7fcb2016-10-27 16:10:08 +01001479
Alex Bennéec93bbbe2016-10-27 16:10:09 +01001480 CPU_FOREACH(cpu) {
1481 if (cpu->unplug && !cpu_can_run(cpu)) {
1482 qemu_tcg_destroy_vcpu(cpu);
1483 cpu->created = false;
1484 qemu_cond_signal(&qemu_cpu_cond);
Alex Bennée1be7fcb2016-10-27 16:10:08 +01001485 break;
1486 }
1487 }
Alex Bennée1be7fcb2016-10-27 16:10:08 +01001488}
Jan Kiszkabdb7ca62011-09-26 09:40:39 +02001489
Alex Bennée65467062017-02-23 18:29:09 +00001490/* Single-threaded TCG
1491 *
1492 * In the single-threaded case each vCPU is simulated in turn. If
1493 * there is more than a single vCPU we create a simple timer to kick
1494 * the vCPU and ensure we don't get stuck in a tight loop in one vCPU.
1495 * This is done explicitly rather than relying on side-effects
1496 * elsewhere.
1497 */
1498
Alex Bennée37257942017-02-23 18:29:14 +00001499static void *qemu_tcg_rr_cpu_thread_fn(void *arg)
Blue Swirl296af7c2010-03-29 19:23:50 +00001500{
Andreas Färberc3586ba2012-05-03 01:41:24 +02001501 CPUState *cpu = arg;
Blue Swirl296af7c2010-03-29 19:23:50 +00001502
Emilio G. Cotaf28d0df2018-06-22 13:45:31 -04001503 assert(tcg_enabled());
Paolo Bonziniab28bd22015-07-09 08:55:38 +02001504 rcu_register_thread();
Emilio G. Cota3468b592017-07-19 18:57:58 -04001505 tcg_register_thread();
Paolo Bonziniab28bd22015-07-09 08:55:38 +02001506
Paolo Bonzini2e7f7a32015-06-18 18:47:18 +02001507 qemu_mutex_lock_iothread();
Andreas Färber814e6122012-05-02 17:00:37 +02001508 qemu_thread_get_self(cpu->thread);
Blue Swirl296af7c2010-03-29 19:23:50 +00001509
David Hildenbrand5a9c9732018-02-09 20:52:39 +01001510 cpu->thread_id = qemu_get_thread_id();
1511 cpu->created = true;
1512 cpu->can_do_io = 1;
Blue Swirl296af7c2010-03-29 19:23:50 +00001513 qemu_cond_signal(&qemu_cpu_cond);
Richard Henderson9c09a252019-03-14 13:06:29 -07001514 qemu_guest_random_seed_thread_part2(cpu->random_seed);
Blue Swirl296af7c2010-03-29 19:23:50 +00001515
Jan Kiszkafa7d1862011-08-22 18:35:25 +02001516 /* wait for initial kick-off after machine start */
Emilio G. Cotac28e3992015-04-27 12:45:28 -04001517 while (first_cpu->stopped) {
KONRAD Fredericd5f8d612015-08-10 17:27:06 +02001518 qemu_cond_wait(first_cpu->halt_cond, &qemu_global_mutex);
Jan Kiszka8e564b42012-02-17 18:31:15 +01001519
1520 /* process any pending work */
Andreas Färberbdc44642013-06-24 23:50:24 +02001521 CPU_FOREACH(cpu) {
Alex Bennée37257942017-02-23 18:29:14 +00001522 current_cpu = cpu;
Andreas Färber182735e2013-05-29 22:29:20 +02001523 qemu_wait_io_event_common(cpu);
Jan Kiszka8e564b42012-02-17 18:31:15 +01001524 }
Jan Kiszka0ab07c62011-02-07 12:19:14 +01001525 }
Blue Swirl296af7c2010-03-29 19:23:50 +00001526
Alex Bennée65467062017-02-23 18:29:09 +00001527 start_tcg_kick_timer();
1528
Alex Bennéec93bbbe2016-10-27 16:10:09 +01001529 cpu = first_cpu;
1530
Alex Bennéee5143e32017-02-23 18:29:12 +00001531 /* process any pending work */
1532 cpu->exit_request = 1;
1533
Blue Swirl296af7c2010-03-29 19:23:50 +00001534 while (1) {
Alex Bennéed759c952018-02-27 12:52:48 +03001535 qemu_mutex_unlock_iothread();
1536 replay_mutex_lock();
1537 qemu_mutex_lock_iothread();
Alex Bennéec93bbbe2016-10-27 16:10:09 +01001538 /* Account partial waits to QEMU_CLOCK_VIRTUAL. */
1539 qemu_account_warp_timer();
1540
Paolo Bonzini6b8f0182017-03-02 19:56:40 +01001541 /* Run the timers here. This is much more efficient than
1542 * waking up the I/O thread and waiting for completion.
1543 */
1544 handle_icount_deadline();
1545
Alex Bennéed759c952018-02-27 12:52:48 +03001546 replay_mutex_unlock();
1547
Alex Bennéec93bbbe2016-10-27 16:10:09 +01001548 if (!cpu) {
1549 cpu = first_cpu;
1550 }
1551
Alex Bennéee5143e32017-02-23 18:29:12 +00001552 while (cpu && !cpu->queued_work_first && !cpu->exit_request) {
1553
Alex Bennée791158d2017-02-23 18:29:10 +00001554 atomic_mb_set(&tcg_current_rr_cpu, cpu);
Alex Bennée37257942017-02-23 18:29:14 +00001555 current_cpu = cpu;
Alex Bennéec93bbbe2016-10-27 16:10:09 +01001556
1557 qemu_clock_enable(QEMU_CLOCK_VIRTUAL,
1558 (cpu->singlestep_enabled & SSTEP_NOTIMER) == 0);
1559
1560 if (cpu_can_run(cpu)) {
1561 int r;
Alex Bennée05248382017-03-29 16:46:59 +01001562
Alex Bennéed759c952018-02-27 12:52:48 +03001563 qemu_mutex_unlock_iothread();
Alex Bennée05248382017-03-29 16:46:59 +01001564 prepare_icount_for_run(cpu);
1565
Alex Bennéec93bbbe2016-10-27 16:10:09 +01001566 r = tcg_cpu_exec(cpu);
Alex Bennée05248382017-03-29 16:46:59 +01001567
1568 process_icount_data(cpu);
Alex Bennéed759c952018-02-27 12:52:48 +03001569 qemu_mutex_lock_iothread();
Alex Bennée05248382017-03-29 16:46:59 +01001570
Alex Bennéec93bbbe2016-10-27 16:10:09 +01001571 if (r == EXCP_DEBUG) {
1572 cpu_handle_guest_debug(cpu);
1573 break;
Pranith Kumar08e73c42017-02-23 18:29:15 +00001574 } else if (r == EXCP_ATOMIC) {
1575 qemu_mutex_unlock_iothread();
1576 cpu_exec_step_atomic(cpu);
1577 qemu_mutex_lock_iothread();
1578 break;
Alex Bennéec93bbbe2016-10-27 16:10:09 +01001579 }
Alex Bennée37257942017-02-23 18:29:14 +00001580 } else if (cpu->stop) {
Alex Bennéec93bbbe2016-10-27 16:10:09 +01001581 if (cpu->unplug) {
1582 cpu = CPU_NEXT(cpu);
1583 }
1584 break;
1585 }
1586
Alex Bennéee5143e32017-02-23 18:29:12 +00001587 cpu = CPU_NEXT(cpu);
1588 } /* while (cpu && !cpu->exit_request).. */
1589
Alex Bennée791158d2017-02-23 18:29:10 +00001590 /* Does not need atomic_mb_set because a spurious wakeup is okay. */
1591 atomic_set(&tcg_current_rr_cpu, NULL);
Alex Bennéec93bbbe2016-10-27 16:10:09 +01001592
Alex Bennéee5143e32017-02-23 18:29:12 +00001593 if (cpu && cpu->exit_request) {
1594 atomic_mb_set(&cpu->exit_request, 0);
1595 }
Alex Blighac70aaf2013-08-21 16:02:57 +01001596
Clement Deschamps013aabd2018-10-21 16:21:03 +02001597 if (use_icount && all_cpu_threads_idle()) {
1598 /*
1599 * When all cpus are sleeping (e.g in WFI), to avoid a deadlock
1600 * in the main_loop, wake it up in order to start the warp timer.
1601 */
1602 qemu_notify_event();
1603 }
1604
Paolo Bonzinia8efa602018-11-14 12:36:57 +01001605 qemu_tcg_rr_wait_io_event();
Alex Bennéec93bbbe2016-10-27 16:10:09 +01001606 deal_with_unplugged_cpus();
Blue Swirl296af7c2010-03-29 19:23:50 +00001607 }
1608
Paolo Bonzini9b0605f2018-01-30 11:05:06 -05001609 rcu_unregister_thread();
Blue Swirl296af7c2010-03-29 19:23:50 +00001610 return NULL;
1611}
1612
Vincent Palatinb0cb0a62017-01-10 11:59:57 +01001613static void *qemu_hax_cpu_thread_fn(void *arg)
1614{
1615 CPUState *cpu = arg;
1616 int r;
Vincent Palatinb3d3a422017-03-20 11:15:49 +01001617
Paolo Bonzini9857c2d2018-01-30 16:28:49 +01001618 rcu_register_thread();
Vincent Palatinb3d3a422017-03-20 11:15:49 +01001619 qemu_mutex_lock_iothread();
Vincent Palatinb0cb0a62017-01-10 11:59:57 +01001620 qemu_thread_get_self(cpu->thread);
Vincent Palatinb0cb0a62017-01-10 11:59:57 +01001621
1622 cpu->thread_id = qemu_get_thread_id();
1623 cpu->created = true;
Vincent Palatinb0cb0a62017-01-10 11:59:57 +01001624 current_cpu = cpu;
1625
1626 hax_init_vcpu(cpu);
1627 qemu_cond_signal(&qemu_cpu_cond);
Richard Henderson9c09a252019-03-14 13:06:29 -07001628 qemu_guest_random_seed_thread_part2(cpu->random_seed);
Vincent Palatinb0cb0a62017-01-10 11:59:57 +01001629
Paolo Bonzini9857c2d2018-01-30 16:28:49 +01001630 do {
Vincent Palatinb0cb0a62017-01-10 11:59:57 +01001631 if (cpu_can_run(cpu)) {
1632 r = hax_smp_cpu_exec(cpu);
1633 if (r == EXCP_DEBUG) {
1634 cpu_handle_guest_debug(cpu);
1635 }
1636 }
1637
Paolo Bonzinidb08b682018-01-11 13:53:12 +01001638 qemu_wait_io_event(cpu);
Paolo Bonzini9857c2d2018-01-30 16:28:49 +01001639 } while (!cpu->unplug || cpu_can_run(cpu));
1640 rcu_unregister_thread();
Vincent Palatinb0cb0a62017-01-10 11:59:57 +01001641 return NULL;
1642}
1643
Sergio Andres Gomez Del Realc97d6d22017-09-13 04:05:09 -05001644/* The HVF-specific vCPU thread function. This one should only run when the host
1645 * CPU supports the VMX "unrestricted guest" feature. */
1646static void *qemu_hvf_cpu_thread_fn(void *arg)
1647{
1648 CPUState *cpu = arg;
1649
1650 int r;
1651
1652 assert(hvf_enabled());
1653
1654 rcu_register_thread();
1655
1656 qemu_mutex_lock_iothread();
1657 qemu_thread_get_self(cpu->thread);
1658
1659 cpu->thread_id = qemu_get_thread_id();
1660 cpu->can_do_io = 1;
1661 current_cpu = cpu;
1662
1663 hvf_init_vcpu(cpu);
1664
1665 /* signal CPU creation */
1666 cpu->created = true;
1667 qemu_cond_signal(&qemu_cpu_cond);
Richard Henderson9c09a252019-03-14 13:06:29 -07001668 qemu_guest_random_seed_thread_part2(cpu->random_seed);
Sergio Andres Gomez Del Realc97d6d22017-09-13 04:05:09 -05001669
1670 do {
1671 if (cpu_can_run(cpu)) {
1672 r = hvf_vcpu_exec(cpu);
1673 if (r == EXCP_DEBUG) {
1674 cpu_handle_guest_debug(cpu);
1675 }
1676 }
Paolo Bonzinidb08b682018-01-11 13:53:12 +01001677 qemu_wait_io_event(cpu);
Sergio Andres Gomez Del Realc97d6d22017-09-13 04:05:09 -05001678 } while (!cpu->unplug || cpu_can_run(cpu));
1679
1680 hvf_vcpu_destroy(cpu);
1681 cpu->created = false;
1682 qemu_cond_signal(&qemu_cpu_cond);
1683 qemu_mutex_unlock_iothread();
Paolo Bonzini8178e632018-01-30 11:05:21 -05001684 rcu_unregister_thread();
Sergio Andres Gomez Del Realc97d6d22017-09-13 04:05:09 -05001685 return NULL;
1686}
1687
Justin Terry (VM)19306802018-01-22 13:07:49 -08001688static void *qemu_whpx_cpu_thread_fn(void *arg)
1689{
1690 CPUState *cpu = arg;
1691 int r;
1692
1693 rcu_register_thread();
1694
1695 qemu_mutex_lock_iothread();
1696 qemu_thread_get_self(cpu->thread);
1697 cpu->thread_id = qemu_get_thread_id();
1698 current_cpu = cpu;
1699
1700 r = whpx_init_vcpu(cpu);
1701 if (r < 0) {
1702 fprintf(stderr, "whpx_init_vcpu failed: %s\n", strerror(-r));
1703 exit(1);
1704 }
1705
1706 /* signal CPU creation */
1707 cpu->created = true;
1708 qemu_cond_signal(&qemu_cpu_cond);
Richard Henderson9c09a252019-03-14 13:06:29 -07001709 qemu_guest_random_seed_thread_part2(cpu->random_seed);
Justin Terry (VM)19306802018-01-22 13:07:49 -08001710
1711 do {
1712 if (cpu_can_run(cpu)) {
1713 r = whpx_vcpu_exec(cpu);
1714 if (r == EXCP_DEBUG) {
1715 cpu_handle_guest_debug(cpu);
1716 }
1717 }
1718 while (cpu_thread_is_idle(cpu)) {
1719 qemu_cond_wait(cpu->halt_cond, &qemu_global_mutex);
1720 }
1721 qemu_wait_io_event_common(cpu);
1722 } while (!cpu->unplug || cpu_can_run(cpu));
1723
1724 whpx_destroy_vcpu(cpu);
1725 cpu->created = false;
1726 qemu_cond_signal(&qemu_cpu_cond);
1727 qemu_mutex_unlock_iothread();
1728 rcu_unregister_thread();
Vincent Palatinb0cb0a62017-01-10 11:59:57 +01001729 return NULL;
1730}
1731
1732#ifdef _WIN32
1733static void CALLBACK dummy_apc_func(ULONG_PTR unused)
1734{
1735}
1736#endif
1737
Alex Bennée37257942017-02-23 18:29:14 +00001738/* Multi-threaded TCG
1739 *
1740 * In the multi-threaded case each vCPU has its own thread. The TLS
1741 * variable current_cpu can be used deep in the code to find the
1742 * current CPUState for a given thread.
1743 */
1744
1745static void *qemu_tcg_cpu_thread_fn(void *arg)
1746{
1747 CPUState *cpu = arg;
1748
Emilio G. Cotaf28d0df2018-06-22 13:45:31 -04001749 assert(tcg_enabled());
Alex Bennéebf51c722017-03-30 18:32:29 +01001750 g_assert(!use_icount);
1751
Alex Bennée37257942017-02-23 18:29:14 +00001752 rcu_register_thread();
Emilio G. Cota3468b592017-07-19 18:57:58 -04001753 tcg_register_thread();
Alex Bennée37257942017-02-23 18:29:14 +00001754
1755 qemu_mutex_lock_iothread();
1756 qemu_thread_get_self(cpu->thread);
1757
1758 cpu->thread_id = qemu_get_thread_id();
1759 cpu->created = true;
1760 cpu->can_do_io = 1;
1761 current_cpu = cpu;
1762 qemu_cond_signal(&qemu_cpu_cond);
Richard Henderson9c09a252019-03-14 13:06:29 -07001763 qemu_guest_random_seed_thread_part2(cpu->random_seed);
Alex Bennée37257942017-02-23 18:29:14 +00001764
1765 /* process any pending work */
1766 cpu->exit_request = 1;
1767
Cédric Le Goater54961aa2018-04-25 15:18:28 +02001768 do {
Alex Bennée37257942017-02-23 18:29:14 +00001769 if (cpu_can_run(cpu)) {
1770 int r;
Alex Bennéed759c952018-02-27 12:52:48 +03001771 qemu_mutex_unlock_iothread();
Alex Bennée37257942017-02-23 18:29:14 +00001772 r = tcg_cpu_exec(cpu);
Alex Bennéed759c952018-02-27 12:52:48 +03001773 qemu_mutex_lock_iothread();
Alex Bennée37257942017-02-23 18:29:14 +00001774 switch (r) {
1775 case EXCP_DEBUG:
1776 cpu_handle_guest_debug(cpu);
1777 break;
1778 case EXCP_HALTED:
1779 /* during start-up the vCPU is reset and the thread is
1780 * kicked several times. If we don't ensure we go back
1781 * to sleep in the halted state we won't cleanly
1782 * start-up when the vCPU is enabled.
1783 *
1784 * cpu->halted should ensure we sleep in wait_io_event
1785 */
1786 g_assert(cpu->halted);
1787 break;
Pranith Kumar08e73c42017-02-23 18:29:15 +00001788 case EXCP_ATOMIC:
1789 qemu_mutex_unlock_iothread();
1790 cpu_exec_step_atomic(cpu);
1791 qemu_mutex_lock_iothread();
Alex Bennée37257942017-02-23 18:29:14 +00001792 default:
1793 /* Ignore everything else? */
1794 break;
1795 }
1796 }
1797
Alex Bennée37257942017-02-23 18:29:14 +00001798 atomic_mb_set(&cpu->exit_request, 0);
Paolo Bonzinidb08b682018-01-11 13:53:12 +01001799 qemu_wait_io_event(cpu);
Paolo Bonzini9b0605f2018-01-30 11:05:06 -05001800 } while (!cpu->unplug || cpu_can_run(cpu));
Alex Bennée37257942017-02-23 18:29:14 +00001801
Paolo Bonzini9b0605f2018-01-30 11:05:06 -05001802 qemu_tcg_destroy_vcpu(cpu);
1803 cpu->created = false;
1804 qemu_cond_signal(&qemu_cpu_cond);
1805 qemu_mutex_unlock_iothread();
1806 rcu_unregister_thread();
Alex Bennée37257942017-02-23 18:29:14 +00001807 return NULL;
1808}
1809
Andreas Färber2ff09a42012-05-03 00:23:30 +02001810static void qemu_cpu_kick_thread(CPUState *cpu)
Paolo Bonzinicc015e92011-03-12 17:44:08 +01001811{
1812#ifndef _WIN32
1813 int err;
1814
Paolo Bonzinie0c38212015-08-26 00:19:19 +02001815 if (cpu->thread_kicked) {
1816 return;
Paolo Bonzini9102ded2015-08-18 06:52:09 -07001817 }
Paolo Bonzinie0c38212015-08-26 00:19:19 +02001818 cpu->thread_kicked = true;
Andreas Färber814e6122012-05-02 17:00:37 +02001819 err = pthread_kill(cpu->thread->thread, SIG_IPI);
Laurent Vivierd455ebc2019-01-02 15:16:03 +01001820 if (err && err != ESRCH) {
Paolo Bonzinicc015e92011-03-12 17:44:08 +01001821 fprintf(stderr, "qemu:%s: %s", __func__, strerror(err));
1822 exit(1);
1823 }
1824#else /* _WIN32 */
Vincent Palatinb0cb0a62017-01-10 11:59:57 +01001825 if (!qemu_cpu_is_self(cpu)) {
Justin Terry (VM)19306802018-01-22 13:07:49 -08001826 if (whpx_enabled()) {
1827 whpx_vcpu_kick(cpu);
1828 } else if (!QueueUserAPC(dummy_apc_func, cpu->hThread, 0)) {
Vincent Palatinb0cb0a62017-01-10 11:59:57 +01001829 fprintf(stderr, "%s: QueueUserAPC failed with error %lu\n",
1830 __func__, GetLastError());
1831 exit(1);
1832 }
1833 }
Paolo Bonzinicc015e92011-03-12 17:44:08 +01001834#endif
1835}
1836
Andreas Färberc08d7422012-05-03 04:34:15 +02001837void qemu_cpu_kick(CPUState *cpu)
Blue Swirl296af7c2010-03-29 19:23:50 +00001838{
Andreas Färberf5c121b2012-05-03 01:22:49 +02001839 qemu_cond_broadcast(cpu->halt_cond);
Paolo Bonzinie0c38212015-08-26 00:19:19 +02001840 if (tcg_enabled()) {
Alex Bennéee8f22f72019-10-01 17:04:26 +01001841 if (qemu_tcg_mttcg_enabled()) {
1842 cpu_exit(cpu);
1843 } else {
1844 qemu_cpu_kick_rr_cpus();
1845 }
Paolo Bonzinie0c38212015-08-26 00:19:19 +02001846 } else {
Vincent Palatinb0cb0a62017-01-10 11:59:57 +01001847 if (hax_enabled()) {
1848 /*
1849 * FIXME: race condition with the exit_request check in
1850 * hax_vcpu_hax_exec
1851 */
1852 cpu->exit_request = 1;
1853 }
Paolo Bonzinie0c38212015-08-26 00:19:19 +02001854 qemu_cpu_kick_thread(cpu);
1855 }
Blue Swirl296af7c2010-03-29 19:23:50 +00001856}
1857
Jan Kiszka46d62fa2011-02-01 22:15:59 +01001858void qemu_cpu_kick_self(void)
1859{
Andreas Färber4917cf42013-05-27 05:17:50 +02001860 assert(current_cpu);
Paolo Bonzini9102ded2015-08-18 06:52:09 -07001861 qemu_cpu_kick_thread(current_cpu);
Blue Swirl296af7c2010-03-29 19:23:50 +00001862}
1863
Andreas Färber60e82572012-05-02 22:23:49 +02001864bool qemu_cpu_is_self(CPUState *cpu)
Blue Swirl296af7c2010-03-29 19:23:50 +00001865{
Andreas Färber814e6122012-05-02 17:00:37 +02001866 return qemu_thread_is_self(cpu->thread);
Blue Swirl296af7c2010-03-29 19:23:50 +00001867}
1868
Paolo Bonzini79e2b9a2015-01-21 12:09:14 +01001869bool qemu_in_vcpu_thread(void)
Juan Quintelaaa723c22012-09-18 16:30:11 +02001870{
Andreas Färber4917cf42013-05-27 05:17:50 +02001871 return current_cpu && qemu_cpu_is_self(current_cpu);
Juan Quintelaaa723c22012-09-18 16:30:11 +02001872}
1873
Paolo Bonziniafbe7052015-06-18 18:47:19 +02001874static __thread bool iothread_locked = false;
1875
1876bool qemu_mutex_iothread_locked(void)
1877{
1878 return iothread_locked;
1879}
1880
Emilio G. Cotacb764d02017-10-28 02:16:41 -04001881/*
1882 * The BQL is taken from so many places that it is worth profiling the
1883 * callers directly, instead of funneling them all through a single function.
1884 */
1885void qemu_mutex_lock_iothread_impl(const char *file, int line)
Blue Swirl296af7c2010-03-29 19:23:50 +00001886{
Emilio G. Cotacb764d02017-10-28 02:16:41 -04001887 QemuMutexLockFunc bql_lock = atomic_read(&qemu_bql_mutex_lock_func);
1888
Jan Kiszka8d04fb52017-02-23 18:29:11 +00001889 g_assert(!qemu_mutex_iothread_locked());
Emilio G. Cotacb764d02017-10-28 02:16:41 -04001890 bql_lock(&qemu_global_mutex, file, line);
Paolo Bonziniafbe7052015-06-18 18:47:19 +02001891 iothread_locked = true;
Blue Swirl296af7c2010-03-29 19:23:50 +00001892}
1893
1894void qemu_mutex_unlock_iothread(void)
1895{
Jan Kiszka8d04fb52017-02-23 18:29:11 +00001896 g_assert(qemu_mutex_iothread_locked());
Paolo Bonziniafbe7052015-06-18 18:47:19 +02001897 iothread_locked = false;
Blue Swirl296af7c2010-03-29 19:23:50 +00001898 qemu_mutex_unlock(&qemu_global_mutex);
1899}
1900
Alex Bennéee8faee02016-10-27 16:09:58 +01001901static bool all_vcpus_paused(void)
Blue Swirl296af7c2010-03-29 19:23:50 +00001902{
Andreas Färberbdc44642013-06-24 23:50:24 +02001903 CPUState *cpu;
Blue Swirl296af7c2010-03-29 19:23:50 +00001904
Andreas Färberbdc44642013-06-24 23:50:24 +02001905 CPU_FOREACH(cpu) {
Andreas Färber182735e2013-05-29 22:29:20 +02001906 if (!cpu->stopped) {
Alex Bennéee8faee02016-10-27 16:09:58 +01001907 return false;
Jan Kiszka0ab07c62011-02-07 12:19:14 +01001908 }
Blue Swirl296af7c2010-03-29 19:23:50 +00001909 }
1910
Alex Bennéee8faee02016-10-27 16:09:58 +01001911 return true;
Blue Swirl296af7c2010-03-29 19:23:50 +00001912}
1913
1914void pause_all_vcpus(void)
1915{
Andreas Färberbdc44642013-06-24 23:50:24 +02001916 CPUState *cpu;
Blue Swirl296af7c2010-03-29 19:23:50 +00001917
Alex Bligh40daca52013-08-21 16:03:02 +01001918 qemu_clock_enable(QEMU_CLOCK_VIRTUAL, false);
Andreas Färberbdc44642013-06-24 23:50:24 +02001919 CPU_FOREACH(cpu) {
David Hildenbrandebd05fe2017-11-29 20:12:15 +01001920 if (qemu_cpu_is_self(cpu)) {
1921 qemu_cpu_stop(cpu, true);
1922 } else {
1923 cpu->stop = true;
1924 qemu_cpu_kick(cpu);
1925 }
Jan Kiszkad798e972012-02-17 18:31:16 +01001926 }
1927
Alex Bennéed759c952018-02-27 12:52:48 +03001928 /* We need to drop the replay_lock so any vCPU threads woken up
1929 * can finish their replay tasks
1930 */
1931 replay_mutex_unlock();
1932
Blue Swirl296af7c2010-03-29 19:23:50 +00001933 while (!all_vcpus_paused()) {
Paolo Bonzinibe7d6c52011-03-12 17:44:02 +01001934 qemu_cond_wait(&qemu_pause_cond, &qemu_global_mutex);
Andreas Färberbdc44642013-06-24 23:50:24 +02001935 CPU_FOREACH(cpu) {
Andreas Färber182735e2013-05-29 22:29:20 +02001936 qemu_cpu_kick(cpu);
Blue Swirl296af7c2010-03-29 19:23:50 +00001937 }
1938 }
Alex Bennéed759c952018-02-27 12:52:48 +03001939
1940 qemu_mutex_unlock_iothread();
1941 replay_mutex_lock();
1942 qemu_mutex_lock_iothread();
Blue Swirl296af7c2010-03-29 19:23:50 +00001943}
1944
Igor Mammedov29936832013-04-23 10:29:37 +02001945void cpu_resume(CPUState *cpu)
1946{
1947 cpu->stop = false;
1948 cpu->stopped = false;
1949 qemu_cpu_kick(cpu);
1950}
1951
Blue Swirl296af7c2010-03-29 19:23:50 +00001952void resume_all_vcpus(void)
1953{
Andreas Färberbdc44642013-06-24 23:50:24 +02001954 CPUState *cpu;
Blue Swirl296af7c2010-03-29 19:23:50 +00001955
Alex Bligh40daca52013-08-21 16:03:02 +01001956 qemu_clock_enable(QEMU_CLOCK_VIRTUAL, true);
Andreas Färberbdc44642013-06-24 23:50:24 +02001957 CPU_FOREACH(cpu) {
Andreas Färber182735e2013-05-29 22:29:20 +02001958 cpu_resume(cpu);
Blue Swirl296af7c2010-03-29 19:23:50 +00001959 }
1960}
1961
Paolo Bonzinidbadee42018-01-30 16:40:12 +01001962void cpu_remove_sync(CPUState *cpu)
Gu Zheng4c055ab2016-05-12 09:18:13 +05301963{
1964 cpu->stop = true;
1965 cpu->unplug = true;
1966 qemu_cpu_kick(cpu);
Paolo Bonzinidbadee42018-01-30 16:40:12 +01001967 qemu_mutex_unlock_iothread();
1968 qemu_thread_join(cpu->thread);
1969 qemu_mutex_lock_iothread();
Bharata B Rao2c579042016-05-12 09:18:14 +05301970}
1971
Dr. David Alan Gilbert49001162014-01-30 10:20:32 +00001972/* For temporary buffers for forming a name */
1973#define VCPU_THREAD_NAME_SIZE 16
1974
Andreas Färbere5ab30a2012-05-03 01:50:44 +02001975static void qemu_tcg_init_vcpu(CPUState *cpu)
Blue Swirl296af7c2010-03-29 19:23:50 +00001976{
Dr. David Alan Gilbert49001162014-01-30 10:20:32 +00001977 char thread_name[VCPU_THREAD_NAME_SIZE];
Alex Bennée37257942017-02-23 18:29:14 +00001978 static QemuCond *single_tcg_halt_cond;
1979 static QemuThread *single_tcg_cpu_thread;
Emilio G. Cotae8feb962017-07-07 19:24:20 -04001980 static int tcg_region_inited;
1981
Emilio G. Cotaf28d0df2018-06-22 13:45:31 -04001982 assert(tcg_enabled());
Emilio G. Cotae8feb962017-07-07 19:24:20 -04001983 /*
1984 * Initialize TCG regions--once. Now is a good time, because:
1985 * (1) TCG's init context, prologue and target globals have been set up.
1986 * (2) qemu_tcg_mttcg_enabled() works now (TCG init code runs before the
1987 * -accel flag is processed, so the check doesn't work then).
1988 */
1989 if (!tcg_region_inited) {
1990 tcg_region_inited = 1;
1991 tcg_region_init();
1992 }
Dr. David Alan Gilbert49001162014-01-30 10:20:32 +00001993
Alex Bennée37257942017-02-23 18:29:14 +00001994 if (qemu_tcg_mttcg_enabled() || !single_tcg_cpu_thread) {
Andreas Färber814e6122012-05-02 17:00:37 +02001995 cpu->thread = g_malloc0(sizeof(QemuThread));
Andreas Färberf5c121b2012-05-03 01:22:49 +02001996 cpu->halt_cond = g_malloc0(sizeof(QemuCond));
1997 qemu_cond_init(cpu->halt_cond);
Alex Bennée37257942017-02-23 18:29:14 +00001998
1999 if (qemu_tcg_mttcg_enabled()) {
2000 /* create a thread per vCPU with TCG (MTTCG) */
2001 parallel_cpus = true;
2002 snprintf(thread_name, VCPU_THREAD_NAME_SIZE, "CPU %d/TCG",
Dr. David Alan Gilbert49001162014-01-30 10:20:32 +00002003 cpu->cpu_index);
Alex Bennée37257942017-02-23 18:29:14 +00002004
2005 qemu_thread_create(cpu->thread, thread_name, qemu_tcg_cpu_thread_fn,
2006 cpu, QEMU_THREAD_JOINABLE);
2007
2008 } else {
2009 /* share a single thread for all cpus with TCG */
2010 snprintf(thread_name, VCPU_THREAD_NAME_SIZE, "ALL CPUs/TCG");
2011 qemu_thread_create(cpu->thread, thread_name,
2012 qemu_tcg_rr_cpu_thread_fn,
2013 cpu, QEMU_THREAD_JOINABLE);
2014
2015 single_tcg_halt_cond = cpu->halt_cond;
2016 single_tcg_cpu_thread = cpu->thread;
2017 }
Paolo Bonzini1ecf47b2011-12-13 13:43:52 +01002018#ifdef _WIN32
Andreas Färber814e6122012-05-02 17:00:37 +02002019 cpu->hThread = qemu_thread_get_handle(cpu->thread);
Paolo Bonzini1ecf47b2011-12-13 13:43:52 +01002020#endif
Blue Swirl296af7c2010-03-29 19:23:50 +00002021 } else {
Alex Bennée37257942017-02-23 18:29:14 +00002022 /* For non-MTTCG cases we share the thread */
2023 cpu->thread = single_tcg_cpu_thread;
2024 cpu->halt_cond = single_tcg_halt_cond;
David Hildenbranda3421732018-02-09 20:52:37 +01002025 cpu->thread_id = first_cpu->thread_id;
2026 cpu->can_do_io = 1;
2027 cpu->created = true;
Blue Swirl296af7c2010-03-29 19:23:50 +00002028 }
2029}
2030
Vincent Palatinb0cb0a62017-01-10 11:59:57 +01002031static void qemu_hax_start_vcpu(CPUState *cpu)
2032{
2033 char thread_name[VCPU_THREAD_NAME_SIZE];
2034
2035 cpu->thread = g_malloc0(sizeof(QemuThread));
2036 cpu->halt_cond = g_malloc0(sizeof(QemuCond));
2037 qemu_cond_init(cpu->halt_cond);
2038
2039 snprintf(thread_name, VCPU_THREAD_NAME_SIZE, "CPU %d/HAX",
2040 cpu->cpu_index);
2041 qemu_thread_create(cpu->thread, thread_name, qemu_hax_cpu_thread_fn,
2042 cpu, QEMU_THREAD_JOINABLE);
2043#ifdef _WIN32
2044 cpu->hThread = qemu_thread_get_handle(cpu->thread);
2045#endif
Vincent Palatinb0cb0a62017-01-10 11:59:57 +01002046}
2047
Andreas Färber48a106b2013-05-27 02:20:39 +02002048static void qemu_kvm_start_vcpu(CPUState *cpu)
Blue Swirl296af7c2010-03-29 19:23:50 +00002049{
Dr. David Alan Gilbert49001162014-01-30 10:20:32 +00002050 char thread_name[VCPU_THREAD_NAME_SIZE];
2051
Andreas Färber814e6122012-05-02 17:00:37 +02002052 cpu->thread = g_malloc0(sizeof(QemuThread));
Andreas Färberf5c121b2012-05-03 01:22:49 +02002053 cpu->halt_cond = g_malloc0(sizeof(QemuCond));
2054 qemu_cond_init(cpu->halt_cond);
Dr. David Alan Gilbert49001162014-01-30 10:20:32 +00002055 snprintf(thread_name, VCPU_THREAD_NAME_SIZE, "CPU %d/KVM",
2056 cpu->cpu_index);
2057 qemu_thread_create(cpu->thread, thread_name, qemu_kvm_cpu_thread_fn,
2058 cpu, QEMU_THREAD_JOINABLE);
Blue Swirl296af7c2010-03-29 19:23:50 +00002059}
2060
Sergio Andres Gomez Del Realc97d6d22017-09-13 04:05:09 -05002061static void qemu_hvf_start_vcpu(CPUState *cpu)
2062{
2063 char thread_name[VCPU_THREAD_NAME_SIZE];
2064
2065 /* HVF currently does not support TCG, and only runs in
2066 * unrestricted-guest mode. */
2067 assert(hvf_enabled());
2068
2069 cpu->thread = g_malloc0(sizeof(QemuThread));
2070 cpu->halt_cond = g_malloc0(sizeof(QemuCond));
2071 qemu_cond_init(cpu->halt_cond);
2072
2073 snprintf(thread_name, VCPU_THREAD_NAME_SIZE, "CPU %d/HVF",
2074 cpu->cpu_index);
2075 qemu_thread_create(cpu->thread, thread_name, qemu_hvf_cpu_thread_fn,
2076 cpu, QEMU_THREAD_JOINABLE);
Sergio Andres Gomez Del Realc97d6d22017-09-13 04:05:09 -05002077}
2078
Justin Terry (VM)19306802018-01-22 13:07:49 -08002079static void qemu_whpx_start_vcpu(CPUState *cpu)
2080{
2081 char thread_name[VCPU_THREAD_NAME_SIZE];
2082
2083 cpu->thread = g_malloc0(sizeof(QemuThread));
2084 cpu->halt_cond = g_malloc0(sizeof(QemuCond));
2085 qemu_cond_init(cpu->halt_cond);
2086 snprintf(thread_name, VCPU_THREAD_NAME_SIZE, "CPU %d/WHPX",
2087 cpu->cpu_index);
2088 qemu_thread_create(cpu->thread, thread_name, qemu_whpx_cpu_thread_fn,
2089 cpu, QEMU_THREAD_JOINABLE);
2090#ifdef _WIN32
2091 cpu->hThread = qemu_thread_get_handle(cpu->thread);
2092#endif
Justin Terry (VM)19306802018-01-22 13:07:49 -08002093}
2094
Andreas Färber10a90212013-05-27 02:24:35 +02002095static void qemu_dummy_start_vcpu(CPUState *cpu)
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +02002096{
Dr. David Alan Gilbert49001162014-01-30 10:20:32 +00002097 char thread_name[VCPU_THREAD_NAME_SIZE];
2098
Andreas Färber814e6122012-05-02 17:00:37 +02002099 cpu->thread = g_malloc0(sizeof(QemuThread));
Andreas Färberf5c121b2012-05-03 01:22:49 +02002100 cpu->halt_cond = g_malloc0(sizeof(QemuCond));
2101 qemu_cond_init(cpu->halt_cond);
Dr. David Alan Gilbert49001162014-01-30 10:20:32 +00002102 snprintf(thread_name, VCPU_THREAD_NAME_SIZE, "CPU %d/DUMMY",
2103 cpu->cpu_index);
2104 qemu_thread_create(cpu->thread, thread_name, qemu_dummy_cpu_thread_fn, cpu,
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +02002105 QEMU_THREAD_JOINABLE);
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +02002106}
2107
Andreas Färberc643bed2013-05-27 03:23:24 +02002108void qemu_init_vcpu(CPUState *cpu)
Blue Swirl296af7c2010-03-29 19:23:50 +00002109{
Like Xu5cc87672019-05-19 04:54:21 +08002110 MachineState *ms = MACHINE(qdev_get_machine());
2111
2112 cpu->nr_cores = ms->smp.cores;
2113 cpu->nr_threads = ms->smp.threads;
Andreas Färberf324e762012-05-02 23:26:21 +02002114 cpu->stopped = true;
Richard Henderson9c09a252019-03-14 13:06:29 -07002115 cpu->random_seed = qemu_guest_random_seed_thread_part1();
Peter Maydell56943e82016-01-21 14:15:04 +00002116
2117 if (!cpu->as) {
2118 /* If the target cpu hasn't set up any address spaces itself,
2119 * give it the default one.
2120 */
Peter Maydell12ebc9a2016-01-21 14:15:04 +00002121 cpu->num_ases = 1;
Peter Xu80ceb072017-11-23 17:23:32 +08002122 cpu_address_space_init(cpu, 0, "cpu-memory", cpu->memory);
Peter Maydell56943e82016-01-21 14:15:04 +00002123 }
2124
Jan Kiszka0ab07c62011-02-07 12:19:14 +01002125 if (kvm_enabled()) {
Andreas Färber48a106b2013-05-27 02:20:39 +02002126 qemu_kvm_start_vcpu(cpu);
Vincent Palatinb0cb0a62017-01-10 11:59:57 +01002127 } else if (hax_enabled()) {
2128 qemu_hax_start_vcpu(cpu);
Sergio Andres Gomez Del Realc97d6d22017-09-13 04:05:09 -05002129 } else if (hvf_enabled()) {
2130 qemu_hvf_start_vcpu(cpu);
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +02002131 } else if (tcg_enabled()) {
Andreas Färbere5ab30a2012-05-03 01:50:44 +02002132 qemu_tcg_init_vcpu(cpu);
Justin Terry (VM)19306802018-01-22 13:07:49 -08002133 } else if (whpx_enabled()) {
2134 qemu_whpx_start_vcpu(cpu);
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +02002135 } else {
Andreas Färber10a90212013-05-27 02:24:35 +02002136 qemu_dummy_start_vcpu(cpu);
Jan Kiszka0ab07c62011-02-07 12:19:14 +01002137 }
David Hildenbrand81e96312018-02-09 20:52:38 +01002138
2139 while (!cpu->created) {
2140 qemu_cond_wait(&qemu_cpu_cond, &qemu_global_mutex);
2141 }
Blue Swirl296af7c2010-03-29 19:23:50 +00002142}
2143
Jan Kiszkab4a3d962011-02-01 22:15:43 +01002144void cpu_stop_current(void)
Blue Swirl296af7c2010-03-29 19:23:50 +00002145{
Andreas Färber4917cf42013-05-27 05:17:50 +02002146 if (current_cpu) {
Peter Maydell0ec7e672019-01-07 15:23:47 +00002147 current_cpu->stop = true;
2148 cpu_exit(current_cpu);
Jan Kiszkab4a3d962011-02-01 22:15:43 +01002149 }
Blue Swirl296af7c2010-03-29 19:23:50 +00002150}
2151
Kevin Wolf56983462013-07-05 13:49:54 +02002152int vm_stop(RunState state)
Blue Swirl296af7c2010-03-29 19:23:50 +00002153{
Juan Quintelaaa723c22012-09-18 16:30:11 +02002154 if (qemu_in_vcpu_thread()) {
Paolo Bonzini74892d22014-06-05 14:53:58 +02002155 qemu_system_vmstop_request_prepare();
Luiz Capitulino1dfb4dd2011-07-29 14:26:33 -03002156 qemu_system_vmstop_request(state);
Blue Swirl296af7c2010-03-29 19:23:50 +00002157 /*
2158 * FIXME: should not return to device code in case
2159 * vm_stop() has been requested.
2160 */
Jan Kiszkab4a3d962011-02-01 22:15:43 +01002161 cpu_stop_current();
Kevin Wolf56983462013-07-05 13:49:54 +02002162 return 0;
Blue Swirl296af7c2010-03-29 19:23:50 +00002163 }
Kevin Wolf56983462013-07-05 13:49:54 +02002164
Stefan Hajnoczi4486e892018-03-07 14:42:05 +00002165 return do_vm_stop(state, true);
Blue Swirl296af7c2010-03-29 19:23:50 +00002166}
2167
Claudio Imbrenda2d76e822017-02-14 18:07:47 +01002168/**
2169 * Prepare for (re)starting the VM.
2170 * Returns -1 if the vCPUs are not to be restarted (e.g. if they are already
2171 * running or in case of an error condition), 0 otherwise.
2172 */
2173int vm_prepare_start(void)
2174{
2175 RunState requested;
Claudio Imbrenda2d76e822017-02-14 18:07:47 +01002176
2177 qemu_vmstop_requested(&requested);
2178 if (runstate_is_running() && requested == RUN_STATE__MAX) {
2179 return -1;
2180 }
2181
2182 /* Ensure that a STOP/RESUME pair of events is emitted if a
2183 * vmstop request was pending. The BLOCK_IO_ERROR event, for
2184 * example, according to documentation is always followed by
2185 * the STOP event.
2186 */
2187 if (runstate_is_running()) {
Peter Xu3ab72382018-08-15 21:37:37 +08002188 qapi_event_send_stop();
2189 qapi_event_send_resume();
Markus Armbrusterf0561582018-04-23 10:45:18 +02002190 return -1;
Claudio Imbrenda2d76e822017-02-14 18:07:47 +01002191 }
2192
2193 /* We are sending this now, but the CPUs will be resumed shortly later */
Peter Xu3ab72382018-08-15 21:37:37 +08002194 qapi_event_send_resume();
Markus Armbrusterf0561582018-04-23 10:45:18 +02002195
2196 replay_enable_events();
2197 cpu_enable_ticks();
2198 runstate_set(RUN_STATE_RUNNING);
2199 vm_state_notify(1, RUN_STATE_RUNNING);
2200 return 0;
Claudio Imbrenda2d76e822017-02-14 18:07:47 +01002201}
2202
2203void vm_start(void)
2204{
2205 if (!vm_prepare_start()) {
2206 resume_all_vcpus();
2207 }
2208}
2209
Luiz Capitulino8a9236f2011-10-14 11:18:09 -03002210/* does a state transition even if the VM is already stopped,
2211 current state is forgotten forever */
Kevin Wolf56983462013-07-05 13:49:54 +02002212int vm_stop_force_state(RunState state)
Luiz Capitulino8a9236f2011-10-14 11:18:09 -03002213{
2214 if (runstate_is_running()) {
Kevin Wolf56983462013-07-05 13:49:54 +02002215 return vm_stop(state);
Luiz Capitulino8a9236f2011-10-14 11:18:09 -03002216 } else {
2217 runstate_set(state);
Wen Congyangb2780d32015-11-20 17:34:38 +08002218
2219 bdrv_drain_all();
Kevin Wolf594a45c2013-07-18 14:52:19 +02002220 /* Make sure to return an error if the flush in a previous vm_stop()
2221 * failed. */
John Snow22af08e2016-09-22 21:45:51 -04002222 return bdrv_flush_all();
Luiz Capitulino8a9236f2011-10-14 11:18:09 -03002223 }
2224}
2225
Markus Armbruster04424282019-04-17 21:17:57 +02002226void list_cpus(const char *optarg)
Blue Swirl262353c2010-05-04 19:55:35 +00002227{
2228 /* XXX: implement xxx_cpu_list for targets that still miss it */
Peter Maydelle916cbf2012-09-05 17:41:08 -03002229#if defined(cpu_list)
Markus Armbruster04424282019-04-17 21:17:57 +02002230 cpu_list();
Blue Swirl262353c2010-05-04 19:55:35 +00002231#endif
2232}
Luiz Capitulinode0b36b2011-09-21 16:38:35 -03002233
Luiz Capitulino0cfd6a92011-11-22 16:32:37 -02002234void qmp_memsave(int64_t addr, int64_t size, const char *filename,
2235 bool has_cpu, int64_t cpu_index, Error **errp)
2236{
2237 FILE *f;
2238 uint32_t l;
Andreas Färber55e5c282012-12-17 06:18:02 +01002239 CPUState *cpu;
Luiz Capitulino0cfd6a92011-11-22 16:32:37 -02002240 uint8_t buf[1024];
Borislav Petkov0dc9daf2015-02-08 13:14:38 +01002241 int64_t orig_addr = addr, orig_size = size;
Luiz Capitulino0cfd6a92011-11-22 16:32:37 -02002242
2243 if (!has_cpu) {
2244 cpu_index = 0;
2245 }
2246
Andreas Färber151d1322013-02-15 15:41:49 +01002247 cpu = qemu_get_cpu(cpu_index);
2248 if (cpu == NULL) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01002249 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "cpu-index",
2250 "a CPU number");
Luiz Capitulino0cfd6a92011-11-22 16:32:37 -02002251 return;
2252 }
2253
2254 f = fopen(filename, "wb");
2255 if (!f) {
Luiz Capitulino618da852013-06-07 14:35:06 -04002256 error_setg_file_open(errp, errno, filename);
Luiz Capitulino0cfd6a92011-11-22 16:32:37 -02002257 return;
2258 }
2259
2260 while (size != 0) {
2261 l = sizeof(buf);
2262 if (l > size)
2263 l = size;
Aneesh Kumar K.V2f4d0f52013-10-01 21:49:30 +05302264 if (cpu_memory_rw_debug(cpu, addr, buf, l, 0) != 0) {
Borislav Petkov0dc9daf2015-02-08 13:14:38 +01002265 error_setg(errp, "Invalid addr 0x%016" PRIx64 "/size %" PRId64
2266 " specified", orig_addr, orig_size);
Aneesh Kumar K.V2f4d0f52013-10-01 21:49:30 +05302267 goto exit;
2268 }
Luiz Capitulino0cfd6a92011-11-22 16:32:37 -02002269 if (fwrite(buf, 1, l, f) != l) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01002270 error_setg(errp, QERR_IO_ERROR);
Luiz Capitulino0cfd6a92011-11-22 16:32:37 -02002271 goto exit;
2272 }
2273 addr += l;
2274 size -= l;
2275 }
2276
2277exit:
2278 fclose(f);
2279}
Luiz Capitulino6d3962b2011-11-22 17:26:46 -02002280
2281void qmp_pmemsave(int64_t addr, int64_t size, const char *filename,
2282 Error **errp)
2283{
2284 FILE *f;
2285 uint32_t l;
2286 uint8_t buf[1024];
2287
2288 f = fopen(filename, "wb");
2289 if (!f) {
Luiz Capitulino618da852013-06-07 14:35:06 -04002290 error_setg_file_open(errp, errno, filename);
Luiz Capitulino6d3962b2011-11-22 17:26:46 -02002291 return;
2292 }
2293
2294 while (size != 0) {
2295 l = sizeof(buf);
2296 if (l > size)
2297 l = size;
Stefan Weileb6282f2014-04-07 20:28:23 +02002298 cpu_physical_memory_read(addr, buf, l);
Luiz Capitulino6d3962b2011-11-22 17:26:46 -02002299 if (fwrite(buf, 1, l, f) != l) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01002300 error_setg(errp, QERR_IO_ERROR);
Luiz Capitulino6d3962b2011-11-22 17:26:46 -02002301 goto exit;
2302 }
2303 addr += l;
2304 size -= l;
2305 }
2306
2307exit:
2308 fclose(f);
2309}
Luiz Capitulinoab49ab52011-11-23 12:55:53 -02002310
2311void qmp_inject_nmi(Error **errp)
2312{
Alexey Kardashevskiy9cb805f2014-08-20 22:16:33 +10002313 nmi_monitor_handle(monitor_get_cpu_index(), errp);
Luiz Capitulinoab49ab52011-11-23 12:55:53 -02002314}
Sebastian Tanase27498be2014-07-25 11:56:33 +02002315
Markus Armbruster76c86612019-04-17 21:17:53 +02002316void dump_drift_info(void)
Sebastian Tanase27498be2014-07-25 11:56:33 +02002317{
2318 if (!use_icount) {
2319 return;
2320 }
2321
Markus Armbruster76c86612019-04-17 21:17:53 +02002322 qemu_printf("Host - Guest clock %"PRIi64" ms\n",
Sebastian Tanase27498be2014-07-25 11:56:33 +02002323 (cpu_get_clock() - cpu_get_icount())/SCALE_MS);
2324 if (icount_align_option) {
Markus Armbruster76c86612019-04-17 21:17:53 +02002325 qemu_printf("Max guest delay %"PRIi64" ms\n",
2326 -max_delay / SCALE_MS);
2327 qemu_printf("Max guest advance %"PRIi64" ms\n",
2328 max_advance / SCALE_MS);
Sebastian Tanase27498be2014-07-25 11:56:33 +02002329 } else {
Markus Armbruster76c86612019-04-17 21:17:53 +02002330 qemu_printf("Max guest delay NA\n");
2331 qemu_printf("Max guest advance NA\n");
Sebastian Tanase27498be2014-07-25 11:56:33 +02002332 }
2333}