blob: 19c5d3710808ea157bd836372519aef8ea501c56 [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"
KONRAD Frederic8d4e9142017-02-23 18:29:08 +000026#include "qemu/config-file.h"
Paolo Bonzini33c11872016-03-15 16:58:45 +010027#include "cpu.h"
Paolo Bonzini83c90892012-12-17 18:19:49 +010028#include "monitor/monitor.h"
Markus Armbrustere688df62018-02-01 12:18:31 +010029#include "qapi/error.h"
Markus Armbruster112ed242018-02-26 17:13:27 -060030#include "qapi/qapi-commands-misc.h"
Markus Armbruster9af23982018-02-11 10:36:01 +010031#include "qapi/qapi-events-run-state.h"
Wenchao Xiaa4e15de2014-06-18 08:43:36 +020032#include "qapi/qmp/qerror.h"
Markus Armbrusterd49b6832015-03-17 18:29:20 +010033#include "qemu/error-report.h"
Paolo Bonzini9c17d612012-12-17 18:20:04 +010034#include "sysemu/sysemu.h"
Max Reitzda31d592016-03-16 19:54:32 +010035#include "sysemu/block-backend.h"
Paolo Bonzini022c62c2012-12-17 18:19:49 +010036#include "exec/gdbstub.h"
Paolo Bonzini9c17d612012-12-17 18:20:04 +010037#include "sysemu/dma.h"
Vincent Palatinb3946622017-01-10 11:59:55 +010038#include "sysemu/hw_accel.h"
Paolo Bonzini9c17d612012-12-17 18:20:04 +010039#include "sysemu/kvm.h"
Vincent Palatinb0cb0a62017-01-10 11:59:57 +010040#include "sysemu/hax.h"
Sergio Andres Gomez Del Realc97d6d22017-09-13 04:05:09 -050041#include "sysemu/hvf.h"
Justin Terry (VM)19306802018-01-22 13:07:49 -080042#include "sysemu/whpx.h"
Paolo Bonzini63c91552016-03-15 13:18:37 +010043#include "exec/exec-all.h"
Blue Swirl296af7c2010-03-29 19:23:50 +000044
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010045#include "qemu/thread.h"
Paolo Bonzini9c17d612012-12-17 18:20:04 +010046#include "sysemu/cpus.h"
47#include "sysemu/qtest.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010048#include "qemu/main-loop.h"
Markus Armbruster922a01a2018-02-01 12:18:46 +010049#include "qemu/option.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010050#include "qemu/bitmap.h"
Liu Ping Fancb365642013-09-25 14:20:58 +080051#include "qemu/seqlock.h"
KONRAD Frederic8d4e9142017-02-23 18:29:08 +000052#include "tcg.h"
Alexey Kardashevskiy9cb805f2014-08-20 22:16:33 +100053#include "hw/nmi.h"
Pavel Dovgalyuk8b427042015-09-17 19:24:05 +030054#include "sysemu/replay.h"
Igor Mammedovafed5a52017-05-10 13:29:55 +020055#include "hw/boards.h"
Jan Kiszka0ff0fc12011-06-23 10:15:55 +020056
Jan Kiszka6d9cb732011-02-01 22:15:58 +010057#ifdef CONFIG_LINUX
58
59#include <sys/prctl.h>
60
Marcelo Tosattic0532a72010-10-11 15:31:21 -030061#ifndef PR_MCE_KILL
62#define PR_MCE_KILL 33
63#endif
64
Jan Kiszka6d9cb732011-02-01 22:15:58 +010065#ifndef PR_MCE_KILL_SET
66#define PR_MCE_KILL_SET 1
67#endif
68
69#ifndef PR_MCE_KILL_EARLY
70#define PR_MCE_KILL_EARLY 1
71#endif
72
73#endif /* CONFIG_LINUX */
74
Sebastian Tanase27498be2014-07-25 11:56:33 +020075int64_t max_delay;
76int64_t max_advance;
Blue Swirl296af7c2010-03-29 19:23:50 +000077
Jason J. Herne2adcc852015-09-08 13:12:33 -040078/* vcpu throttling controls */
79static QEMUTimer *throttle_timer;
80static unsigned int throttle_percentage;
81
82#define CPU_THROTTLE_PCT_MIN 1
83#define CPU_THROTTLE_PCT_MAX 99
84#define CPU_THROTTLE_TIMESLICE_NS 10000000
85
Tiejun Chen321bc0b2013-08-02 09:43:09 +080086bool cpu_is_stopped(CPUState *cpu)
87{
88 return cpu->stopped || !runstate_is_running();
89}
90
Andreas Färbera98ae1d2013-05-26 23:21:08 +020091static bool cpu_thread_is_idle(CPUState *cpu)
Peter Maydellac873f12012-07-19 16:52:27 +010092{
Andreas Färberc64ca812012-05-03 02:11:45 +020093 if (cpu->stop || cpu->queued_work_first) {
Peter Maydellac873f12012-07-19 16:52:27 +010094 return false;
95 }
Tiejun Chen321bc0b2013-08-02 09:43:09 +080096 if (cpu_is_stopped(cpu)) {
Peter Maydellac873f12012-07-19 16:52:27 +010097 return true;
98 }
Andreas Färber8c2e1b02013-08-25 18:53:55 +020099 if (!cpu->halted || cpu_has_work(cpu) ||
Alexander Graf215e79c2013-04-24 22:24:12 +0200100 kvm_halt_in_kernel()) {
Peter Maydellac873f12012-07-19 16:52:27 +0100101 return false;
102 }
103 return true;
104}
105
106static bool all_cpu_threads_idle(void)
107{
Andreas Färber182735e2013-05-29 22:29:20 +0200108 CPUState *cpu;
Peter Maydellac873f12012-07-19 16:52:27 +0100109
Andreas Färberbdc44642013-06-24 23:50:24 +0200110 CPU_FOREACH(cpu) {
Andreas Färber182735e2013-05-29 22:29:20 +0200111 if (!cpu_thread_is_idle(cpu)) {
Peter Maydellac873f12012-07-19 16:52:27 +0100112 return false;
113 }
114 }
115 return true;
116}
117
Blue Swirl296af7c2010-03-29 19:23:50 +0000118/***********************************************************/
Paolo Bonzini946fb272011-09-12 13:57:37 +0200119/* guest cycle counter */
120
Paolo Bonzinia3270e12013-10-07 17:18:15 +0200121/* Protected by TimersState seqlock */
122
Victor CLEMENT5045e9d92015-05-29 17:14:04 +0200123static bool icount_sleep = true;
Paolo Bonzini946fb272011-09-12 13:57:37 +0200124/* Conversion factor from emulated instructions to virtual clock ticks. */
125static int icount_time_shift;
126/* Arbitrarily pick 1MIPS as the minimum allowable speed. */
127#define MAX_ICOUNT_SHIFT 10
Paolo Bonzinia3270e12013-10-07 17:18:15 +0200128
Paolo Bonzini946fb272011-09-12 13:57:37 +0200129typedef struct TimersState {
Liu Ping Fancb365642013-09-25 14:20:58 +0800130 /* Protected by BQL. */
Paolo Bonzini946fb272011-09-12 13:57:37 +0200131 int64_t cpu_ticks_prev;
132 int64_t cpu_ticks_offset;
Liu Ping Fancb365642013-09-25 14:20:58 +0800133
134 /* cpu_clock_offset can be read out of BQL, so protect it with
135 * this lock.
136 */
137 QemuSeqLock vm_clock_seqlock;
Paolo Bonzini946fb272011-09-12 13:57:37 +0200138 int64_t cpu_clock_offset;
139 int32_t cpu_ticks_enabled;
140 int64_t dummy;
KONRAD Fredericc96778b2014-08-01 01:37:09 +0200141
142 /* Compensate for varying guest execution speed. */
143 int64_t qemu_icount_bias;
144 /* Only written by TCG thread */
145 int64_t qemu_icount;
Pavel Dovgalyukb39e3f32018-01-11 11:26:10 +0300146 /* for adjusting icount */
147 int64_t vm_clock_warp_start;
148 QEMUTimer *icount_rt_timer;
149 QEMUTimer *icount_vm_timer;
150 QEMUTimer *icount_warp_timer;
Paolo Bonzini946fb272011-09-12 13:57:37 +0200151} TimersState;
152
Liu Ping Fand9cd4002013-07-21 08:43:00 +0000153static TimersState timers_state;
KONRAD Frederic8d4e9142017-02-23 18:29:08 +0000154bool mttcg_enabled;
155
156/*
157 * We default to false if we know other options have been enabled
158 * which are currently incompatible with MTTCG. Otherwise when each
159 * guest (target) has been updated to support:
160 * - atomic instructions
161 * - memory ordering primitives (barriers)
162 * they can set the appropriate CONFIG flags in ${target}-softmmu.mak
163 *
164 * Once a guest architecture has been converted to the new primitives
165 * there are two remaining limitations to check.
166 *
167 * - The guest can't be oversized (e.g. 64 bit guest on 32 bit host)
168 * - The host must have a stronger memory order than the guest
169 *
170 * It may be possible in future to support strong guests on weak hosts
171 * but that will require tagging all load/stores in a guest with their
172 * implicit memory order requirements which would likely slow things
173 * down a lot.
174 */
175
176static bool check_tcg_memory_orders_compatible(void)
177{
178#if defined(TCG_GUEST_DEFAULT_MO) && defined(TCG_TARGET_DEFAULT_MO)
179 return (TCG_GUEST_DEFAULT_MO & ~TCG_TARGET_DEFAULT_MO) == 0;
180#else
181 return false;
182#endif
183}
184
185static bool default_mttcg_enabled(void)
186{
Alex Bennée83fd9622017-02-27 17:09:01 +0000187 if (use_icount || TCG_OVERSIZED_GUEST) {
KONRAD Frederic8d4e9142017-02-23 18:29:08 +0000188 return false;
189 } else {
190#ifdef TARGET_SUPPORTS_MTTCG
191 return check_tcg_memory_orders_compatible();
192#else
193 return false;
194#endif
195 }
196}
197
198void qemu_tcg_configure(QemuOpts *opts, Error **errp)
199{
200 const char *t = qemu_opt_get(opts, "thread");
201 if (t) {
202 if (strcmp(t, "multi") == 0) {
203 if (TCG_OVERSIZED_GUEST) {
204 error_setg(errp, "No MTTCG when guest word size > hosts");
Alex Bennée83fd9622017-02-27 17:09:01 +0000205 } else if (use_icount) {
206 error_setg(errp, "No MTTCG when icount is enabled");
KONRAD Frederic8d4e9142017-02-23 18:29:08 +0000207 } else {
Nikunj A Dadhania86953502017-04-10 11:36:55 +0530208#ifndef TARGET_SUPPORTS_MTTCG
Alex Bennéec34c7622017-02-28 14:40:17 +0000209 error_report("Guest not yet converted to MTTCG - "
210 "you may get unexpected results");
211#endif
KONRAD Frederic8d4e9142017-02-23 18:29:08 +0000212 if (!check_tcg_memory_orders_compatible()) {
213 error_report("Guest expects a stronger memory ordering "
214 "than the host provides");
Pranith Kumar8cfef892017-03-25 16:19:23 -0400215 error_printf("This may cause strange/hard to debug errors\n");
KONRAD Frederic8d4e9142017-02-23 18:29:08 +0000216 }
217 mttcg_enabled = true;
218 }
219 } else if (strcmp(t, "single") == 0) {
220 mttcg_enabled = false;
221 } else {
222 error_setg(errp, "Invalid 'thread' setting %s", t);
223 }
224 } else {
225 mttcg_enabled = default_mttcg_enabled();
226 }
227}
Paolo Bonzini946fb272011-09-12 13:57:37 +0200228
Alex Bennéee4cd9652017-03-31 16:09:42 +0100229/* The current number of executed instructions is based on what we
230 * originally budgeted minus the current state of the decrementing
231 * icount counters in extra/u16.low.
232 */
233static int64_t cpu_get_icount_executed(CPUState *cpu)
234{
235 return cpu->icount_budget - (cpu->icount_decr.u16.low + cpu->icount_extra);
236}
237
Alex Bennée512d3c82017-04-05 12:32:37 +0100238/*
239 * Update the global shared timer_state.qemu_icount to take into
240 * account executed instructions. This is done by the TCG vCPU
241 * thread so the main-loop can see time has moved forward.
242 */
243void cpu_update_icount(CPUState *cpu)
244{
245 int64_t executed = cpu_get_icount_executed(cpu);
246 cpu->icount_budget -= executed;
247
248#ifdef CONFIG_ATOMIC64
249 atomic_set__nocheck(&timers_state.qemu_icount,
250 atomic_read__nocheck(&timers_state.qemu_icount) +
251 executed);
252#else /* FIXME: we need 64bit atomics to do this safely */
253 timers_state.qemu_icount += executed;
254#endif
255}
256
Pavel Dovgalyuk2a629142014-12-08 10:53:45 +0300257int64_t cpu_get_icount_raw(void)
Paolo Bonzini946fb272011-09-12 13:57:37 +0200258{
Andreas Färber4917cf42013-05-27 05:17:50 +0200259 CPUState *cpu = current_cpu;
Paolo Bonzini946fb272011-09-12 13:57:37 +0200260
Alex Bennée243c5f72017-03-30 18:49:22 +0100261 if (cpu && cpu->running) {
Paolo Bonzini414b15c2015-06-24 14:16:26 +0200262 if (!cpu->can_do_io) {
Alistair Francis493d89b2018-02-03 09:43:14 +0100263 error_report("Bad icount read");
Pavel Dovgalyuk2a629142014-12-08 10:53:45 +0300264 exit(1);
Paolo Bonzini946fb272011-09-12 13:57:37 +0200265 }
Alex Bennéee4cd9652017-03-31 16:09:42 +0100266 /* Take into account what has run */
Alex Bennée1d059062017-04-05 10:53:47 +0100267 cpu_update_icount(cpu);
Paolo Bonzini946fb272011-09-12 13:57:37 +0200268 }
Alex Bennée1d059062017-04-05 10:53:47 +0100269#ifdef CONFIG_ATOMIC64
270 return atomic_read__nocheck(&timers_state.qemu_icount);
271#else /* FIXME: we need 64bit atomics to do this safely */
272 return timers_state.qemu_icount;
273#endif
Pavel Dovgalyuk2a629142014-12-08 10:53:45 +0300274}
275
276/* Return the virtual CPU time, based on the instruction counter. */
277static int64_t cpu_get_icount_locked(void)
278{
279 int64_t icount = cpu_get_icount_raw();
KONRAD Frederic3f031312014-08-01 01:37:15 +0200280 return timers_state.qemu_icount_bias + cpu_icount_to_ns(icount);
Paolo Bonzini946fb272011-09-12 13:57:37 +0200281}
282
Paolo Bonzini17a15f12013-10-03 15:17:25 +0200283int64_t cpu_get_icount(void)
284{
285 int64_t icount;
286 unsigned start;
287
288 do {
289 start = seqlock_read_begin(&timers_state.vm_clock_seqlock);
290 icount = cpu_get_icount_locked();
291 } while (seqlock_read_retry(&timers_state.vm_clock_seqlock, start));
292
293 return icount;
294}
295
KONRAD Frederic3f031312014-08-01 01:37:15 +0200296int64_t cpu_icount_to_ns(int64_t icount)
297{
298 return icount << icount_time_shift;
299}
300
Cao jind90f3cc2016-07-29 19:05:38 +0800301/* return the time elapsed in VM between vm_start and vm_stop. Unless
302 * icount is active, cpu_get_ticks() uses units of the host CPU cycle
303 * counter.
304 *
305 * Caller must hold the BQL
306 */
Paolo Bonzini946fb272011-09-12 13:57:37 +0200307int64_t cpu_get_ticks(void)
308{
Paolo Bonzini5f3e3102013-10-28 17:32:18 +0100309 int64_t ticks;
310
Paolo Bonzini946fb272011-09-12 13:57:37 +0200311 if (use_icount) {
312 return cpu_get_icount();
313 }
Paolo Bonzini5f3e3102013-10-28 17:32:18 +0100314
315 ticks = timers_state.cpu_ticks_offset;
316 if (timers_state.cpu_ticks_enabled) {
Christopher Covington4a7428c2015-09-25 10:42:21 -0400317 ticks += cpu_get_host_ticks();
Paolo Bonzini946fb272011-09-12 13:57:37 +0200318 }
Paolo Bonzini5f3e3102013-10-28 17:32:18 +0100319
320 if (timers_state.cpu_ticks_prev > ticks) {
321 /* Note: non increasing ticks may happen if the host uses
322 software suspend */
323 timers_state.cpu_ticks_offset += timers_state.cpu_ticks_prev - ticks;
324 ticks = timers_state.cpu_ticks_prev;
325 }
326
327 timers_state.cpu_ticks_prev = ticks;
328 return ticks;
Paolo Bonzini946fb272011-09-12 13:57:37 +0200329}
330
Liu Ping Fancb365642013-09-25 14:20:58 +0800331static int64_t cpu_get_clock_locked(void)
332{
Cao jin1d45cea2016-07-29 19:05:37 +0800333 int64_t time;
Liu Ping Fancb365642013-09-25 14:20:58 +0800334
Cao jin1d45cea2016-07-29 19:05:37 +0800335 time = timers_state.cpu_clock_offset;
Paolo Bonzini5f3e3102013-10-28 17:32:18 +0100336 if (timers_state.cpu_ticks_enabled) {
Cao jin1d45cea2016-07-29 19:05:37 +0800337 time += get_clock();
Liu Ping Fancb365642013-09-25 14:20:58 +0800338 }
339
Cao jin1d45cea2016-07-29 19:05:37 +0800340 return time;
Liu Ping Fancb365642013-09-25 14:20:58 +0800341}
342
Cao jind90f3cc2016-07-29 19:05:38 +0800343/* Return the monotonic time elapsed in VM, i.e.,
Peter Maydell8212ff82016-09-15 10:24:22 +0100344 * the time between vm_start and vm_stop
345 */
Paolo Bonzini946fb272011-09-12 13:57:37 +0200346int64_t cpu_get_clock(void)
347{
348 int64_t ti;
Liu Ping Fancb365642013-09-25 14:20:58 +0800349 unsigned start;
350
351 do {
352 start = seqlock_read_begin(&timers_state.vm_clock_seqlock);
353 ti = cpu_get_clock_locked();
354 } while (seqlock_read_retry(&timers_state.vm_clock_seqlock, start));
355
356 return ti;
Paolo Bonzini946fb272011-09-12 13:57:37 +0200357}
358
Liu Ping Fancb365642013-09-25 14:20:58 +0800359/* enable cpu_get_ticks()
Cao jin3224e872016-07-08 18:31:37 +0800360 * Caller must hold BQL which serves as mutex for vm_clock_seqlock.
Liu Ping Fancb365642013-09-25 14:20:58 +0800361 */
Paolo Bonzini946fb272011-09-12 13:57:37 +0200362void cpu_enable_ticks(void)
363{
Liu Ping Fancb365642013-09-25 14:20:58 +0800364 /* Here, the really thing protected by seqlock is cpu_clock_offset. */
Emilio G. Cota03719e42016-06-08 14:55:21 -0400365 seqlock_write_begin(&timers_state.vm_clock_seqlock);
Paolo Bonzini946fb272011-09-12 13:57:37 +0200366 if (!timers_state.cpu_ticks_enabled) {
Christopher Covington4a7428c2015-09-25 10:42:21 -0400367 timers_state.cpu_ticks_offset -= cpu_get_host_ticks();
Paolo Bonzini946fb272011-09-12 13:57:37 +0200368 timers_state.cpu_clock_offset -= get_clock();
369 timers_state.cpu_ticks_enabled = 1;
370 }
Emilio G. Cota03719e42016-06-08 14:55:21 -0400371 seqlock_write_end(&timers_state.vm_clock_seqlock);
Paolo Bonzini946fb272011-09-12 13:57:37 +0200372}
373
374/* disable cpu_get_ticks() : the clock is stopped. You must not call
Liu Ping Fancb365642013-09-25 14:20:58 +0800375 * cpu_get_ticks() after that.
Cao jin3224e872016-07-08 18:31:37 +0800376 * Caller must hold BQL which serves as mutex for vm_clock_seqlock.
Liu Ping Fancb365642013-09-25 14:20:58 +0800377 */
Paolo Bonzini946fb272011-09-12 13:57:37 +0200378void cpu_disable_ticks(void)
379{
Liu Ping Fancb365642013-09-25 14:20:58 +0800380 /* Here, the really thing protected by seqlock is cpu_clock_offset. */
Emilio G. Cota03719e42016-06-08 14:55:21 -0400381 seqlock_write_begin(&timers_state.vm_clock_seqlock);
Paolo Bonzini946fb272011-09-12 13:57:37 +0200382 if (timers_state.cpu_ticks_enabled) {
Christopher Covington4a7428c2015-09-25 10:42:21 -0400383 timers_state.cpu_ticks_offset += cpu_get_host_ticks();
Liu Ping Fancb365642013-09-25 14:20:58 +0800384 timers_state.cpu_clock_offset = cpu_get_clock_locked();
Paolo Bonzini946fb272011-09-12 13:57:37 +0200385 timers_state.cpu_ticks_enabled = 0;
386 }
Emilio G. Cota03719e42016-06-08 14:55:21 -0400387 seqlock_write_end(&timers_state.vm_clock_seqlock);
Paolo Bonzini946fb272011-09-12 13:57:37 +0200388}
389
390/* Correlation between real and virtual time is always going to be
391 fairly approximate, so ignore small variation.
392 When the guest is idle real and virtual time will be aligned in
393 the IO wait loop. */
Rutuja Shah73bcb242016-03-21 21:32:30 +0530394#define ICOUNT_WOBBLE (NANOSECONDS_PER_SECOND / 10)
Paolo Bonzini946fb272011-09-12 13:57:37 +0200395
396static void icount_adjust(void)
397{
398 int64_t cur_time;
399 int64_t cur_icount;
400 int64_t delta;
Paolo Bonzinia3270e12013-10-07 17:18:15 +0200401
402 /* Protected by TimersState mutex. */
Paolo Bonzini946fb272011-09-12 13:57:37 +0200403 static int64_t last_delta;
Paolo Bonzini468cc7c2013-10-07 17:21:51 +0200404
Paolo Bonzini946fb272011-09-12 13:57:37 +0200405 /* If the VM is not running, then do nothing. */
406 if (!runstate_is_running()) {
407 return;
408 }
Paolo Bonzini468cc7c2013-10-07 17:21:51 +0200409
Emilio G. Cota03719e42016-06-08 14:55:21 -0400410 seqlock_write_begin(&timers_state.vm_clock_seqlock);
Paolo Bonzini17a15f12013-10-03 15:17:25 +0200411 cur_time = cpu_get_clock_locked();
412 cur_icount = cpu_get_icount_locked();
Paolo Bonzini468cc7c2013-10-07 17:21:51 +0200413
Paolo Bonzini946fb272011-09-12 13:57:37 +0200414 delta = cur_icount - cur_time;
415 /* FIXME: This is a very crude algorithm, somewhat prone to oscillation. */
416 if (delta > 0
417 && last_delta + ICOUNT_WOBBLE < delta * 2
418 && icount_time_shift > 0) {
419 /* The guest is getting too far ahead. Slow time down. */
420 icount_time_shift--;
421 }
422 if (delta < 0
423 && last_delta - ICOUNT_WOBBLE > delta * 2
424 && icount_time_shift < MAX_ICOUNT_SHIFT) {
425 /* The guest is getting too far behind. Speed time up. */
426 icount_time_shift++;
427 }
428 last_delta = delta;
KONRAD Fredericc96778b2014-08-01 01:37:09 +0200429 timers_state.qemu_icount_bias = cur_icount
430 - (timers_state.qemu_icount << icount_time_shift);
Emilio G. Cota03719e42016-06-08 14:55:21 -0400431 seqlock_write_end(&timers_state.vm_clock_seqlock);
Paolo Bonzini946fb272011-09-12 13:57:37 +0200432}
433
434static void icount_adjust_rt(void *opaque)
435{
Pavel Dovgalyukb39e3f32018-01-11 11:26:10 +0300436 timer_mod(timers_state.icount_rt_timer,
Pavel Dovgalyuk1979b902015-01-12 15:00:43 +0300437 qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL_RT) + 1000);
Paolo Bonzini946fb272011-09-12 13:57:37 +0200438 icount_adjust();
439}
440
441static void icount_adjust_vm(void *opaque)
442{
Pavel Dovgalyukb39e3f32018-01-11 11:26:10 +0300443 timer_mod(timers_state.icount_vm_timer,
Alex Bligh40daca52013-08-21 16:03:02 +0100444 qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) +
Rutuja Shah73bcb242016-03-21 21:32:30 +0530445 NANOSECONDS_PER_SECOND / 10);
Paolo Bonzini946fb272011-09-12 13:57:37 +0200446 icount_adjust();
447}
448
449static int64_t qemu_icount_round(int64_t count)
450{
451 return (count + (1 << icount_time_shift) - 1) >> icount_time_shift;
452}
453
Pavel Dovgalyukefab87c2015-09-17 19:24:39 +0300454static void icount_warp_rt(void)
Paolo Bonzini946fb272011-09-12 13:57:37 +0200455{
Alex Bennéeccffff42016-04-04 15:35:48 +0100456 unsigned seq;
457 int64_t warp_start;
458
Paolo Bonzini17a15f12013-10-03 15:17:25 +0200459 /* The icount_warp_timer is rescheduled soon after vm_clock_warp_start
460 * changes from -1 to another value, so the race here is okay.
461 */
Alex Bennéeccffff42016-04-04 15:35:48 +0100462 do {
463 seq = seqlock_read_begin(&timers_state.vm_clock_seqlock);
Pavel Dovgalyukb39e3f32018-01-11 11:26:10 +0300464 warp_start = timers_state.vm_clock_warp_start;
Alex Bennéeccffff42016-04-04 15:35:48 +0100465 } while (seqlock_read_retry(&timers_state.vm_clock_seqlock, seq));
466
467 if (warp_start == -1) {
Paolo Bonzini946fb272011-09-12 13:57:37 +0200468 return;
469 }
470
Emilio G. Cota03719e42016-06-08 14:55:21 -0400471 seqlock_write_begin(&timers_state.vm_clock_seqlock);
Paolo Bonzini946fb272011-09-12 13:57:37 +0200472 if (runstate_is_running()) {
Pavel Dovgalyuk8eda2062015-09-17 19:24:28 +0300473 int64_t clock = REPLAY_CLOCK(REPLAY_CLOCK_VIRTUAL_RT,
474 cpu_get_clock_locked());
Paolo Bonzini8ed961d2013-10-07 17:26:07 +0200475 int64_t warp_delta;
476
Pavel Dovgalyukb39e3f32018-01-11 11:26:10 +0300477 warp_delta = clock - timers_state.vm_clock_warp_start;
Paolo Bonzini8ed961d2013-10-07 17:26:07 +0200478 if (use_icount == 2) {
Paolo Bonzini946fb272011-09-12 13:57:37 +0200479 /*
Alex Bligh40daca52013-08-21 16:03:02 +0100480 * In adaptive mode, do not let QEMU_CLOCK_VIRTUAL run too
Paolo Bonzini946fb272011-09-12 13:57:37 +0200481 * far ahead of real time.
482 */
Paolo Bonzini17a15f12013-10-03 15:17:25 +0200483 int64_t cur_icount = cpu_get_icount_locked();
Pavel Dovgalyukbf2a7dd2014-11-26 13:40:55 +0300484 int64_t delta = clock - cur_icount;
Paolo Bonzini8ed961d2013-10-07 17:26:07 +0200485 warp_delta = MIN(warp_delta, delta);
Paolo Bonzini946fb272011-09-12 13:57:37 +0200486 }
KONRAD Fredericc96778b2014-08-01 01:37:09 +0200487 timers_state.qemu_icount_bias += warp_delta;
Paolo Bonzini946fb272011-09-12 13:57:37 +0200488 }
Pavel Dovgalyukb39e3f32018-01-11 11:26:10 +0300489 timers_state.vm_clock_warp_start = -1;
Emilio G. Cota03719e42016-06-08 14:55:21 -0400490 seqlock_write_end(&timers_state.vm_clock_seqlock);
Paolo Bonzini8ed961d2013-10-07 17:26:07 +0200491
492 if (qemu_clock_expired(QEMU_CLOCK_VIRTUAL)) {
493 qemu_clock_notify(QEMU_CLOCK_VIRTUAL);
494 }
Paolo Bonzini946fb272011-09-12 13:57:37 +0200495}
496
Pavel Dovgalyuke76d1792016-03-10 14:56:09 +0300497static void icount_timer_cb(void *opaque)
Pavel Dovgalyukefab87c2015-09-17 19:24:39 +0300498{
Pavel Dovgalyuke76d1792016-03-10 14:56:09 +0300499 /* No need for a checkpoint because the timer already synchronizes
500 * with CHECKPOINT_CLOCK_VIRTUAL_RT.
501 */
502 icount_warp_rt();
Pavel Dovgalyukefab87c2015-09-17 19:24:39 +0300503}
504
Paolo Bonzini8156be52012-03-28 15:42:04 +0200505void qtest_clock_warp(int64_t dest)
506{
Alex Bligh40daca52013-08-21 16:03:02 +0100507 int64_t clock = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
Fam Zhengefef88b2015-01-19 17:51:43 +0800508 AioContext *aio_context;
Paolo Bonzini8156be52012-03-28 15:42:04 +0200509 assert(qtest_enabled());
Fam Zhengefef88b2015-01-19 17:51:43 +0800510 aio_context = qemu_get_aio_context();
Paolo Bonzini8156be52012-03-28 15:42:04 +0200511 while (clock < dest) {
Alex Bligh40daca52013-08-21 16:03:02 +0100512 int64_t deadline = qemu_clock_deadline_ns_all(QEMU_CLOCK_VIRTUAL);
Sergey Fedorovc9299e22014-06-10 13:10:28 +0400513 int64_t warp = qemu_soonest_timeout(dest - clock, deadline);
Fam Zhengefef88b2015-01-19 17:51:43 +0800514
Emilio G. Cota03719e42016-06-08 14:55:21 -0400515 seqlock_write_begin(&timers_state.vm_clock_seqlock);
KONRAD Fredericc96778b2014-08-01 01:37:09 +0200516 timers_state.qemu_icount_bias += warp;
Emilio G. Cota03719e42016-06-08 14:55:21 -0400517 seqlock_write_end(&timers_state.vm_clock_seqlock);
Paolo Bonzini17a15f12013-10-03 15:17:25 +0200518
Alex Bligh40daca52013-08-21 16:03:02 +0100519 qemu_clock_run_timers(QEMU_CLOCK_VIRTUAL);
Fam Zhengefef88b2015-01-19 17:51:43 +0800520 timerlist_run_timers(aio_context->tlg.tl[QEMU_CLOCK_VIRTUAL]);
Alex Bligh40daca52013-08-21 16:03:02 +0100521 clock = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
Paolo Bonzini8156be52012-03-28 15:42:04 +0200522 }
Alex Bligh40daca52013-08-21 16:03:02 +0100523 qemu_clock_notify(QEMU_CLOCK_VIRTUAL);
Paolo Bonzini8156be52012-03-28 15:42:04 +0200524}
525
Pavel Dovgalyuke76d1792016-03-10 14:56:09 +0300526void qemu_start_warp_timer(void)
Paolo Bonzini946fb272011-09-12 13:57:37 +0200527{
Paolo Bonzinice78d182013-10-07 17:30:02 +0200528 int64_t clock;
Paolo Bonzini946fb272011-09-12 13:57:37 +0200529 int64_t deadline;
530
Pavel Dovgalyuke76d1792016-03-10 14:56:09 +0300531 if (!use_icount) {
Paolo Bonzini946fb272011-09-12 13:57:37 +0200532 return;
533 }
534
Pavel Dovgalyuk8bd7f712015-09-17 19:24:44 +0300535 /* Nothing to do if the VM is stopped: QEMU_CLOCK_VIRTUAL timers
536 * do not fire, so computing the deadline does not make sense.
537 */
538 if (!runstate_is_running()) {
539 return;
540 }
541
542 /* warp clock deterministically in record/replay mode */
Pavel Dovgalyuke76d1792016-03-10 14:56:09 +0300543 if (!replay_checkpoint(CHECKPOINT_CLOCK_WARP_START)) {
Pavel Dovgalyuk8bd7f712015-09-17 19:24:44 +0300544 return;
545 }
546
Paolo Bonzinice78d182013-10-07 17:30:02 +0200547 if (!all_cpu_threads_idle()) {
Paolo Bonzini946fb272011-09-12 13:57:37 +0200548 return;
549 }
550
Paolo Bonzini8156be52012-03-28 15:42:04 +0200551 if (qtest_enabled()) {
552 /* When testing, qtest commands advance icount. */
Pavel Dovgalyuke76d1792016-03-10 14:56:09 +0300553 return;
Paolo Bonzini8156be52012-03-28 15:42:04 +0200554 }
555
Alex Blighac70aaf2013-08-21 16:02:57 +0100556 /* We want to use the earliest deadline from ALL vm_clocks */
Pavel Dovgalyukbf2a7dd2014-11-26 13:40:55 +0300557 clock = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL_RT);
Alex Bligh40daca52013-08-21 16:03:02 +0100558 deadline = qemu_clock_deadline_ns_all(QEMU_CLOCK_VIRTUAL);
Paolo Bonzinice78d182013-10-07 17:30:02 +0200559 if (deadline < 0) {
Victor CLEMENTd7a0f712015-05-29 17:14:06 +0200560 static bool notified;
561 if (!icount_sleep && !notified) {
Alistair Francis3dc6f862017-07-12 06:57:41 -0700562 warn_report("icount sleep disabled and no active timers");
Victor CLEMENTd7a0f712015-05-29 17:14:06 +0200563 notified = true;
564 }
Paolo Bonzinice78d182013-10-07 17:30:02 +0200565 return;
Alex Blighac70aaf2013-08-21 16:02:57 +0100566 }
567
Paolo Bonzini946fb272011-09-12 13:57:37 +0200568 if (deadline > 0) {
569 /*
Alex Bligh40daca52013-08-21 16:03:02 +0100570 * Ensure QEMU_CLOCK_VIRTUAL proceeds even when the virtual CPU goes to
Paolo Bonzini946fb272011-09-12 13:57:37 +0200571 * sleep. Otherwise, the CPU might be waiting for a future timer
572 * interrupt to wake it up, but the interrupt never comes because
573 * the vCPU isn't running any insns and thus doesn't advance the
Alex Bligh40daca52013-08-21 16:03:02 +0100574 * QEMU_CLOCK_VIRTUAL.
Paolo Bonzini946fb272011-09-12 13:57:37 +0200575 */
Victor CLEMENT5045e9d92015-05-29 17:14:04 +0200576 if (!icount_sleep) {
577 /*
578 * We never let VCPUs sleep in no sleep icount mode.
579 * If there is a pending QEMU_CLOCK_VIRTUAL timer we just advance
580 * to the next QEMU_CLOCK_VIRTUAL event and notify it.
581 * It is useful when we want a deterministic execution time,
582 * isolated from host latencies.
583 */
Emilio G. Cota03719e42016-06-08 14:55:21 -0400584 seqlock_write_begin(&timers_state.vm_clock_seqlock);
Victor CLEMENT5045e9d92015-05-29 17:14:04 +0200585 timers_state.qemu_icount_bias += deadline;
Emilio G. Cota03719e42016-06-08 14:55:21 -0400586 seqlock_write_end(&timers_state.vm_clock_seqlock);
Victor CLEMENT5045e9d92015-05-29 17:14:04 +0200587 qemu_clock_notify(QEMU_CLOCK_VIRTUAL);
588 } else {
589 /*
590 * We do stop VCPUs and only advance QEMU_CLOCK_VIRTUAL after some
591 * "real" time, (related to the time left until the next event) has
592 * passed. The QEMU_CLOCK_VIRTUAL_RT clock will do this.
593 * This avoids that the warps are visible externally; for example,
594 * you will not be sending network packets continuously instead of
595 * every 100ms.
596 */
Emilio G. Cota03719e42016-06-08 14:55:21 -0400597 seqlock_write_begin(&timers_state.vm_clock_seqlock);
Pavel Dovgalyukb39e3f32018-01-11 11:26:10 +0300598 if (timers_state.vm_clock_warp_start == -1
599 || timers_state.vm_clock_warp_start > clock) {
600 timers_state.vm_clock_warp_start = clock;
Victor CLEMENT5045e9d92015-05-29 17:14:04 +0200601 }
Emilio G. Cota03719e42016-06-08 14:55:21 -0400602 seqlock_write_end(&timers_state.vm_clock_seqlock);
Pavel Dovgalyukb39e3f32018-01-11 11:26:10 +0300603 timer_mod_anticipate(timers_state.icount_warp_timer,
604 clock + deadline);
Paolo Bonzinice78d182013-10-07 17:30:02 +0200605 }
Alex Blighac70aaf2013-08-21 16:02:57 +0100606 } else if (deadline == 0) {
Alex Bligh40daca52013-08-21 16:03:02 +0100607 qemu_clock_notify(QEMU_CLOCK_VIRTUAL);
Paolo Bonzini946fb272011-09-12 13:57:37 +0200608 }
609}
610
Pavel Dovgalyuke76d1792016-03-10 14:56:09 +0300611static void qemu_account_warp_timer(void)
612{
613 if (!use_icount || !icount_sleep) {
614 return;
615 }
616
617 /* Nothing to do if the VM is stopped: QEMU_CLOCK_VIRTUAL timers
618 * do not fire, so computing the deadline does not make sense.
619 */
620 if (!runstate_is_running()) {
621 return;
622 }
623
624 /* warp clock deterministically in record/replay mode */
625 if (!replay_checkpoint(CHECKPOINT_CLOCK_WARP_ACCOUNT)) {
626 return;
627 }
628
Pavel Dovgalyukb39e3f32018-01-11 11:26:10 +0300629 timer_del(timers_state.icount_warp_timer);
Pavel Dovgalyuke76d1792016-03-10 14:56:09 +0300630 icount_warp_rt();
631}
632
KONRAD Fredericd09eae32014-08-01 01:37:10 +0200633static bool icount_state_needed(void *opaque)
634{
635 return use_icount;
636}
637
Pavel Dovgalyukb39e3f32018-01-11 11:26:10 +0300638static bool warp_timer_state_needed(void *opaque)
639{
640 TimersState *s = opaque;
641 return s->icount_warp_timer != NULL;
642}
643
644static bool adjust_timers_state_needed(void *opaque)
645{
646 TimersState *s = opaque;
647 return s->icount_rt_timer != NULL;
648}
649
650/*
651 * Subsection for warp timer migration is optional, because may not be created
652 */
653static const VMStateDescription icount_vmstate_warp_timer = {
654 .name = "timer/icount/warp_timer",
655 .version_id = 1,
656 .minimum_version_id = 1,
657 .needed = warp_timer_state_needed,
658 .fields = (VMStateField[]) {
659 VMSTATE_INT64(vm_clock_warp_start, TimersState),
660 VMSTATE_TIMER_PTR(icount_warp_timer, TimersState),
661 VMSTATE_END_OF_LIST()
662 }
663};
664
665static const VMStateDescription icount_vmstate_adjust_timers = {
666 .name = "timer/icount/timers",
667 .version_id = 1,
668 .minimum_version_id = 1,
669 .needed = adjust_timers_state_needed,
670 .fields = (VMStateField[]) {
671 VMSTATE_TIMER_PTR(icount_rt_timer, TimersState),
672 VMSTATE_TIMER_PTR(icount_vm_timer, TimersState),
673 VMSTATE_END_OF_LIST()
674 }
675};
676
KONRAD Fredericd09eae32014-08-01 01:37:10 +0200677/*
678 * This is a subsection for icount migration.
679 */
680static const VMStateDescription icount_vmstate_timers = {
681 .name = "timer/icount",
682 .version_id = 1,
683 .minimum_version_id = 1,
Juan Quintela5cd8cad2014-09-23 14:09:54 +0200684 .needed = icount_state_needed,
KONRAD Fredericd09eae32014-08-01 01:37:10 +0200685 .fields = (VMStateField[]) {
686 VMSTATE_INT64(qemu_icount_bias, TimersState),
687 VMSTATE_INT64(qemu_icount, TimersState),
688 VMSTATE_END_OF_LIST()
Pavel Dovgalyukb39e3f32018-01-11 11:26:10 +0300689 },
690 .subsections = (const VMStateDescription*[]) {
691 &icount_vmstate_warp_timer,
692 &icount_vmstate_adjust_timers,
693 NULL
KONRAD Fredericd09eae32014-08-01 01:37:10 +0200694 }
695};
696
Paolo Bonzini946fb272011-09-12 13:57:37 +0200697static const VMStateDescription vmstate_timers = {
698 .name = "timer",
699 .version_id = 2,
700 .minimum_version_id = 1,
Juan Quintela35d08452014-04-16 16:01:33 +0200701 .fields = (VMStateField[]) {
Paolo Bonzini946fb272011-09-12 13:57:37 +0200702 VMSTATE_INT64(cpu_ticks_offset, TimersState),
703 VMSTATE_INT64(dummy, TimersState),
704 VMSTATE_INT64_V(cpu_clock_offset, TimersState, 2),
705 VMSTATE_END_OF_LIST()
KONRAD Fredericd09eae32014-08-01 01:37:10 +0200706 },
Juan Quintela5cd8cad2014-09-23 14:09:54 +0200707 .subsections = (const VMStateDescription*[]) {
708 &icount_vmstate_timers,
709 NULL
Paolo Bonzini946fb272011-09-12 13:57:37 +0200710 }
711};
712
Paolo Bonzini14e6fe12016-10-31 10:36:08 +0100713static void cpu_throttle_thread(CPUState *cpu, run_on_cpu_data opaque)
Jason J. Herne2adcc852015-09-08 13:12:33 -0400714{
Jason J. Herne2adcc852015-09-08 13:12:33 -0400715 double pct;
716 double throttle_ratio;
717 long sleeptime_ns;
718
719 if (!cpu_throttle_get_percentage()) {
720 return;
721 }
722
723 pct = (double)cpu_throttle_get_percentage()/100;
724 throttle_ratio = pct / (1 - pct);
725 sleeptime_ns = (long)(throttle_ratio * CPU_THROTTLE_TIMESLICE_NS);
726
727 qemu_mutex_unlock_iothread();
Jason J. Herne2adcc852015-09-08 13:12:33 -0400728 g_usleep(sleeptime_ns / 1000); /* Convert ns to us for usleep call */
729 qemu_mutex_lock_iothread();
Felipe Franciosi90bb0c02017-05-19 22:29:50 +0100730 atomic_set(&cpu->throttle_thread_scheduled, 0);
Jason J. Herne2adcc852015-09-08 13:12:33 -0400731}
732
733static void cpu_throttle_timer_tick(void *opaque)
734{
735 CPUState *cpu;
736 double pct;
737
738 /* Stop the timer if needed */
739 if (!cpu_throttle_get_percentage()) {
740 return;
741 }
742 CPU_FOREACH(cpu) {
743 if (!atomic_xchg(&cpu->throttle_thread_scheduled, 1)) {
Paolo Bonzini14e6fe12016-10-31 10:36:08 +0100744 async_run_on_cpu(cpu, cpu_throttle_thread,
745 RUN_ON_CPU_NULL);
Jason J. Herne2adcc852015-09-08 13:12:33 -0400746 }
747 }
748
749 pct = (double)cpu_throttle_get_percentage()/100;
750 timer_mod(throttle_timer, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL_RT) +
751 CPU_THROTTLE_TIMESLICE_NS / (1-pct));
752}
753
754void cpu_throttle_set(int new_throttle_pct)
755{
756 /* Ensure throttle percentage is within valid range */
757 new_throttle_pct = MIN(new_throttle_pct, CPU_THROTTLE_PCT_MAX);
758 new_throttle_pct = MAX(new_throttle_pct, CPU_THROTTLE_PCT_MIN);
759
760 atomic_set(&throttle_percentage, new_throttle_pct);
761
762 timer_mod(throttle_timer, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL_RT) +
763 CPU_THROTTLE_TIMESLICE_NS);
764}
765
766void cpu_throttle_stop(void)
767{
768 atomic_set(&throttle_percentage, 0);
769}
770
771bool cpu_throttle_active(void)
772{
773 return (cpu_throttle_get_percentage() != 0);
774}
775
776int cpu_throttle_get_percentage(void)
777{
778 return atomic_read(&throttle_percentage);
779}
780
Pavel Dovgalyuk4603ea02014-09-01 09:34:49 +0400781void cpu_ticks_init(void)
782{
Emilio G. Cotaccdb3c12016-06-08 14:55:20 -0400783 seqlock_init(&timers_state.vm_clock_seqlock);
Pavel Dovgalyuk4603ea02014-09-01 09:34:49 +0400784 vmstate_register(NULL, 0, &vmstate_timers, &timers_state);
Jason J. Herne2adcc852015-09-08 13:12:33 -0400785 throttle_timer = timer_new_ns(QEMU_CLOCK_VIRTUAL_RT,
786 cpu_throttle_timer_tick, NULL);
Pavel Dovgalyuk4603ea02014-09-01 09:34:49 +0400787}
788
Sebastian Tanase1ad95802014-07-25 11:56:28 +0200789void configure_icount(QemuOpts *opts, Error **errp)
Paolo Bonzini946fb272011-09-12 13:57:37 +0200790{
Sebastian Tanase1ad95802014-07-25 11:56:28 +0200791 const char *option;
Sebastian Tanasea8bfac32014-07-25 11:56:29 +0200792 char *rem_str = NULL;
Sebastian Tanase1ad95802014-07-25 11:56:28 +0200793
Sebastian Tanase1ad95802014-07-25 11:56:28 +0200794 option = qemu_opt_get(opts, "shift");
Paolo Bonzini946fb272011-09-12 13:57:37 +0200795 if (!option) {
Sebastian Tanasea8bfac32014-07-25 11:56:29 +0200796 if (qemu_opt_get(opts, "align") != NULL) {
797 error_setg(errp, "Please specify shift option when using align");
798 }
Paolo Bonzini946fb272011-09-12 13:57:37 +0200799 return;
800 }
Victor CLEMENTf1f4b572015-05-29 17:14:05 +0200801
802 icount_sleep = qemu_opt_get_bool(opts, "sleep", true);
Victor CLEMENT5045e9d92015-05-29 17:14:04 +0200803 if (icount_sleep) {
Pavel Dovgalyukb39e3f32018-01-11 11:26:10 +0300804 timers_state.icount_warp_timer = timer_new_ns(QEMU_CLOCK_VIRTUAL_RT,
Pavel Dovgalyuke76d1792016-03-10 14:56:09 +0300805 icount_timer_cb, NULL);
Victor CLEMENT5045e9d92015-05-29 17:14:04 +0200806 }
Victor CLEMENTf1f4b572015-05-29 17:14:05 +0200807
Sebastian Tanasea8bfac32014-07-25 11:56:29 +0200808 icount_align_option = qemu_opt_get_bool(opts, "align", false);
Victor CLEMENTf1f4b572015-05-29 17:14:05 +0200809
810 if (icount_align_option && !icount_sleep) {
Pranith Kumar778d9f92016-02-26 10:16:51 -0500811 error_setg(errp, "align=on and sleep=off are incompatible");
Victor CLEMENTf1f4b572015-05-29 17:14:05 +0200812 }
Paolo Bonzini946fb272011-09-12 13:57:37 +0200813 if (strcmp(option, "auto") != 0) {
Sebastian Tanasea8bfac32014-07-25 11:56:29 +0200814 errno = 0;
815 icount_time_shift = strtol(option, &rem_str, 0);
816 if (errno != 0 || *rem_str != '\0' || !strlen(option)) {
817 error_setg(errp, "icount: Invalid shift value");
818 }
Paolo Bonzini946fb272011-09-12 13:57:37 +0200819 use_icount = 1;
820 return;
Sebastian Tanasea8bfac32014-07-25 11:56:29 +0200821 } else if (icount_align_option) {
822 error_setg(errp, "shift=auto and align=on are incompatible");
Victor CLEMENTf1f4b572015-05-29 17:14:05 +0200823 } else if (!icount_sleep) {
Pranith Kumar778d9f92016-02-26 10:16:51 -0500824 error_setg(errp, "shift=auto and sleep=off are incompatible");
Paolo Bonzini946fb272011-09-12 13:57:37 +0200825 }
826
827 use_icount = 2;
828
829 /* 125MIPS seems a reasonable initial guess at the guest speed.
830 It will be corrected fairly quickly anyway. */
831 icount_time_shift = 3;
832
833 /* Have both realtime and virtual time triggers for speed adjustment.
834 The realtime trigger catches emulated time passing too slowly,
835 the virtual time trigger catches emulated time passing too fast.
836 Realtime triggers occur even when idle, so use them less frequently
837 than VM triggers. */
Pavel Dovgalyukb39e3f32018-01-11 11:26:10 +0300838 timers_state.vm_clock_warp_start = -1;
839 timers_state.icount_rt_timer = timer_new_ms(QEMU_CLOCK_VIRTUAL_RT,
Pavel Dovgalyukbf2a7dd2014-11-26 13:40:55 +0300840 icount_adjust_rt, NULL);
Pavel Dovgalyukb39e3f32018-01-11 11:26:10 +0300841 timer_mod(timers_state.icount_rt_timer,
Pavel Dovgalyukbf2a7dd2014-11-26 13:40:55 +0300842 qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL_RT) + 1000);
Pavel Dovgalyukb39e3f32018-01-11 11:26:10 +0300843 timers_state.icount_vm_timer = timer_new_ns(QEMU_CLOCK_VIRTUAL,
Alex Bligh40daca52013-08-21 16:03:02 +0100844 icount_adjust_vm, NULL);
Pavel Dovgalyukb39e3f32018-01-11 11:26:10 +0300845 timer_mod(timers_state.icount_vm_timer,
Alex Bligh40daca52013-08-21 16:03:02 +0100846 qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) +
Rutuja Shah73bcb242016-03-21 21:32:30 +0530847 NANOSECONDS_PER_SECOND / 10);
Paolo Bonzini946fb272011-09-12 13:57:37 +0200848}
849
850/***********************************************************/
Alex Bennée65467062017-02-23 18:29:09 +0000851/* TCG vCPU kick timer
852 *
853 * The kick timer is responsible for moving single threaded vCPU
854 * emulation on to the next vCPU. If more than one vCPU is running a
855 * timer event with force a cpu->exit so the next vCPU can get
856 * scheduled.
857 *
858 * The timer is removed if all vCPUs are idle and restarted again once
859 * idleness is complete.
860 */
861
862static QEMUTimer *tcg_kick_vcpu_timer;
Alex Bennée791158d2017-02-23 18:29:10 +0000863static CPUState *tcg_current_rr_cpu;
Alex Bennée65467062017-02-23 18:29:09 +0000864
865#define TCG_KICK_PERIOD (NANOSECONDS_PER_SECOND / 10)
866
867static inline int64_t qemu_tcg_next_kick(void)
868{
869 return qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) + TCG_KICK_PERIOD;
870}
871
Alex Bennée791158d2017-02-23 18:29:10 +0000872/* Kick the currently round-robin scheduled vCPU */
873static void qemu_cpu_kick_rr_cpu(void)
874{
875 CPUState *cpu;
Alex Bennée791158d2017-02-23 18:29:10 +0000876 do {
877 cpu = atomic_mb_read(&tcg_current_rr_cpu);
878 if (cpu) {
879 cpu_exit(cpu);
880 }
881 } while (cpu != atomic_mb_read(&tcg_current_rr_cpu));
882}
883
Paolo Bonzini6b8f0182017-03-02 19:56:40 +0100884static void do_nothing(CPUState *cpu, run_on_cpu_data unused)
885{
886}
887
Paolo Bonzini3f53bc62017-03-03 11:50:29 +0100888void qemu_timer_notify_cb(void *opaque, QEMUClockType type)
889{
Paolo Bonzini6b8f0182017-03-02 19:56:40 +0100890 if (!use_icount || type != QEMU_CLOCK_VIRTUAL) {
891 qemu_notify_event();
892 return;
893 }
894
Peter Maydellc52e7132018-04-10 13:02:25 +0100895 if (qemu_in_vcpu_thread()) {
896 /* A CPU is currently running; kick it back out to the
897 * tcg_cpu_exec() loop so it will recalculate its
898 * icount deadline immediately.
899 */
900 qemu_cpu_kick(current_cpu);
901 } else if (first_cpu) {
Paolo Bonzini6b8f0182017-03-02 19:56:40 +0100902 /* qemu_cpu_kick is not enough to kick a halted CPU out of
903 * qemu_tcg_wait_io_event. async_run_on_cpu, instead,
904 * causes cpu_thread_is_idle to return false. This way,
905 * handle_icount_deadline can run.
Peter Maydellc52e7132018-04-10 13:02:25 +0100906 * If we have no CPUs at all for some reason, we don't
907 * need to do anything.
Paolo Bonzini6b8f0182017-03-02 19:56:40 +0100908 */
909 async_run_on_cpu(first_cpu, do_nothing, RUN_ON_CPU_NULL);
910 }
Paolo Bonzini3f53bc62017-03-03 11:50:29 +0100911}
912
Alex Bennée65467062017-02-23 18:29:09 +0000913static void kick_tcg_thread(void *opaque)
914{
915 timer_mod(tcg_kick_vcpu_timer, qemu_tcg_next_kick());
Alex Bennée791158d2017-02-23 18:29:10 +0000916 qemu_cpu_kick_rr_cpu();
Alex Bennée65467062017-02-23 18:29:09 +0000917}
918
919static void start_tcg_kick_timer(void)
920{
Paolo Bonzinidb08b682018-01-11 13:53:12 +0100921 assert(!mttcg_enabled);
922 if (!tcg_kick_vcpu_timer && CPU_NEXT(first_cpu)) {
Alex Bennée65467062017-02-23 18:29:09 +0000923 tcg_kick_vcpu_timer = timer_new_ns(QEMU_CLOCK_VIRTUAL,
924 kick_tcg_thread, NULL);
925 timer_mod(tcg_kick_vcpu_timer, qemu_tcg_next_kick());
926 }
927}
928
929static void stop_tcg_kick_timer(void)
930{
Paolo Bonzinidb08b682018-01-11 13:53:12 +0100931 assert(!mttcg_enabled);
Alex Bennée65467062017-02-23 18:29:09 +0000932 if (tcg_kick_vcpu_timer) {
933 timer_del(tcg_kick_vcpu_timer);
934 tcg_kick_vcpu_timer = NULL;
935 }
936}
937
Alex Bennée65467062017-02-23 18:29:09 +0000938/***********************************************************/
Blue Swirl296af7c2010-03-29 19:23:50 +0000939void hw_error(const char *fmt, ...)
940{
941 va_list ap;
Andreas Färber55e5c282012-12-17 06:18:02 +0100942 CPUState *cpu;
Blue Swirl296af7c2010-03-29 19:23:50 +0000943
944 va_start(ap, fmt);
945 fprintf(stderr, "qemu: hardware error: ");
946 vfprintf(stderr, fmt, ap);
947 fprintf(stderr, "\n");
Andreas Färberbdc44642013-06-24 23:50:24 +0200948 CPU_FOREACH(cpu) {
Andreas Färber55e5c282012-12-17 06:18:02 +0100949 fprintf(stderr, "CPU #%d:\n", cpu->cpu_index);
Andreas Färber878096e2013-05-27 01:33:50 +0200950 cpu_dump_state(cpu, stderr, fprintf, CPU_DUMP_FPU);
Blue Swirl296af7c2010-03-29 19:23:50 +0000951 }
952 va_end(ap);
953 abort();
954}
955
956void cpu_synchronize_all_states(void)
957{
Andreas Färber182735e2013-05-29 22:29:20 +0200958 CPUState *cpu;
Blue Swirl296af7c2010-03-29 19:23:50 +0000959
Andreas Färberbdc44642013-06-24 23:50:24 +0200960 CPU_FOREACH(cpu) {
Andreas Färber182735e2013-05-29 22:29:20 +0200961 cpu_synchronize_state(cpu);
Sergio Andres Gomez Del Realc97d6d22017-09-13 04:05:09 -0500962 /* TODO: move to cpu_synchronize_state() */
963 if (hvf_enabled()) {
964 hvf_cpu_synchronize_state(cpu);
965 }
Blue Swirl296af7c2010-03-29 19:23:50 +0000966 }
967}
968
969void cpu_synchronize_all_post_reset(void)
970{
Andreas Färber182735e2013-05-29 22:29:20 +0200971 CPUState *cpu;
Blue Swirl296af7c2010-03-29 19:23:50 +0000972
Andreas Färberbdc44642013-06-24 23:50:24 +0200973 CPU_FOREACH(cpu) {
Andreas Färber182735e2013-05-29 22:29:20 +0200974 cpu_synchronize_post_reset(cpu);
Sergio Andres Gomez Del Realc97d6d22017-09-13 04:05:09 -0500975 /* TODO: move to cpu_synchronize_post_reset() */
976 if (hvf_enabled()) {
977 hvf_cpu_synchronize_post_reset(cpu);
978 }
Blue Swirl296af7c2010-03-29 19:23:50 +0000979 }
980}
981
982void cpu_synchronize_all_post_init(void)
983{
Andreas Färber182735e2013-05-29 22:29:20 +0200984 CPUState *cpu;
Blue Swirl296af7c2010-03-29 19:23:50 +0000985
Andreas Färberbdc44642013-06-24 23:50:24 +0200986 CPU_FOREACH(cpu) {
Andreas Färber182735e2013-05-29 22:29:20 +0200987 cpu_synchronize_post_init(cpu);
Sergio Andres Gomez Del Realc97d6d22017-09-13 04:05:09 -0500988 /* TODO: move to cpu_synchronize_post_init() */
989 if (hvf_enabled()) {
990 hvf_cpu_synchronize_post_init(cpu);
991 }
Blue Swirl296af7c2010-03-29 19:23:50 +0000992 }
993}
994
David Gibson75e972d2017-05-26 14:46:28 +1000995void cpu_synchronize_all_pre_loadvm(void)
996{
997 CPUState *cpu;
998
999 CPU_FOREACH(cpu) {
1000 cpu_synchronize_pre_loadvm(cpu);
1001 }
1002}
1003
Stefan Hajnoczi4486e892018-03-07 14:42:05 +00001004static int do_vm_stop(RunState state, bool send_stop)
Blue Swirl296af7c2010-03-29 19:23:50 +00001005{
Kevin Wolf56983462013-07-05 13:49:54 +02001006 int ret = 0;
1007
Luiz Capitulino13548692011-07-29 15:36:43 -03001008 if (runstate_is_running()) {
Blue Swirl296af7c2010-03-29 19:23:50 +00001009 cpu_disable_ticks();
Blue Swirl296af7c2010-03-29 19:23:50 +00001010 pause_all_vcpus();
Luiz Capitulinof5bbfba2011-07-29 15:04:45 -03001011 runstate_set(state);
Luiz Capitulino1dfb4dd2011-07-29 14:26:33 -03001012 vm_state_notify(0, state);
Stefan Hajnoczi4486e892018-03-07 14:42:05 +00001013 if (send_stop) {
1014 qapi_event_send_stop(&error_abort);
1015 }
Blue Swirl296af7c2010-03-29 19:23:50 +00001016 }
Kevin Wolf56983462013-07-05 13:49:54 +02001017
Kevin Wolf594a45c2013-07-18 14:52:19 +02001018 bdrv_drain_all();
Pavel Dovgalyuk6d0ceb82016-09-26 11:08:16 +03001019 replay_disable_events();
John Snow22af08e2016-09-22 21:45:51 -04001020 ret = bdrv_flush_all();
Kevin Wolf594a45c2013-07-18 14:52:19 +02001021
Kevin Wolf56983462013-07-05 13:49:54 +02001022 return ret;
Blue Swirl296af7c2010-03-29 19:23:50 +00001023}
1024
Stefan Hajnoczi4486e892018-03-07 14:42:05 +00001025/* Special vm_stop() variant for terminating the process. Historically clients
1026 * did not expect a QMP STOP event and so we need to retain compatibility.
1027 */
1028int vm_shutdown(void)
1029{
1030 return do_vm_stop(RUN_STATE_SHUTDOWN, false);
1031}
1032
Andreas Färbera1fcaa72012-05-02 23:42:26 +02001033static bool cpu_can_run(CPUState *cpu)
Blue Swirl296af7c2010-03-29 19:23:50 +00001034{
Andreas Färber4fdeee72012-05-02 23:10:09 +02001035 if (cpu->stop) {
Andreas Färbera1fcaa72012-05-02 23:42:26 +02001036 return false;
Jan Kiszka0ab07c62011-02-07 12:19:14 +01001037 }
Tiejun Chen321bc0b2013-08-02 09:43:09 +08001038 if (cpu_is_stopped(cpu)) {
Andreas Färbera1fcaa72012-05-02 23:42:26 +02001039 return false;
Jan Kiszka0ab07c62011-02-07 12:19:14 +01001040 }
Andreas Färbera1fcaa72012-05-02 23:42:26 +02001041 return true;
Blue Swirl296af7c2010-03-29 19:23:50 +00001042}
1043
Andreas Färber91325042013-05-27 02:07:49 +02001044static void cpu_handle_guest_debug(CPUState *cpu)
Jan Kiszka3c638d02010-06-25 16:56:56 +02001045{
Andreas Färber64f6b342013-05-27 02:06:09 +02001046 gdb_set_stop_cpu(cpu);
Jan Kiszka8cf71712011-02-07 12:19:16 +01001047 qemu_system_debug_request();
Andreas Färberf324e762012-05-02 23:26:21 +02001048 cpu->stopped = true;
Jan Kiszka3c638d02010-06-25 16:56:56 +02001049}
1050
Jan Kiszka6d9cb732011-02-01 22:15:58 +01001051#ifdef CONFIG_LINUX
1052static void sigbus_reraise(void)
1053{
1054 sigset_t set;
1055 struct sigaction action;
1056
1057 memset(&action, 0, sizeof(action));
1058 action.sa_handler = SIG_DFL;
1059 if (!sigaction(SIGBUS, &action, NULL)) {
1060 raise(SIGBUS);
1061 sigemptyset(&set);
1062 sigaddset(&set, SIGBUS);
Peter Maydella2d17612016-05-16 18:33:59 +01001063 pthread_sigmask(SIG_UNBLOCK, &set, NULL);
Jan Kiszka6d9cb732011-02-01 22:15:58 +01001064 }
1065 perror("Failed to re-raise SIGBUS!\n");
1066 abort();
1067}
1068
Paolo Bonzinid98d4072017-02-08 13:22:12 +01001069static void sigbus_handler(int n, siginfo_t *siginfo, void *ctx)
Jan Kiszka6d9cb732011-02-01 22:15:58 +01001070{
Paolo Bonzinia16fc072017-02-09 09:50:02 +01001071 if (siginfo->si_code != BUS_MCEERR_AO && siginfo->si_code != BUS_MCEERR_AR) {
1072 sigbus_reraise();
1073 }
1074
Paolo Bonzini2ae41db2017-02-08 12:48:54 +01001075 if (current_cpu) {
1076 /* Called asynchronously in VCPU thread. */
1077 if (kvm_on_sigbus_vcpu(current_cpu, siginfo->si_code, siginfo->si_addr)) {
1078 sigbus_reraise();
1079 }
1080 } else {
1081 /* Called synchronously (via signalfd) in main thread. */
1082 if (kvm_on_sigbus(siginfo->si_code, siginfo->si_addr)) {
1083 sigbus_reraise();
1084 }
Jan Kiszka6d9cb732011-02-01 22:15:58 +01001085 }
1086}
1087
1088static void qemu_init_sigbus(void)
1089{
1090 struct sigaction action;
1091
1092 memset(&action, 0, sizeof(action));
1093 action.sa_flags = SA_SIGINFO;
Paolo Bonzinid98d4072017-02-08 13:22:12 +01001094 action.sa_sigaction = sigbus_handler;
Jan Kiszka6d9cb732011-02-01 22:15:58 +01001095 sigaction(SIGBUS, &action, NULL);
1096
1097 prctl(PR_MCE_KILL, PR_MCE_KILL_SET, PR_MCE_KILL_EARLY, 0, 0);
1098}
Paolo Bonzinia16fc072017-02-09 09:50:02 +01001099#else /* !CONFIG_LINUX */
1100static void qemu_init_sigbus(void)
1101{
1102}
Paolo Bonzinia16fc072017-02-09 09:50:02 +01001103#endif /* !CONFIG_LINUX */
Blue Swirl296af7c2010-03-29 19:23:50 +00001104
Stefan Weilb2532d82012-09-27 07:41:42 +02001105static QemuMutex qemu_global_mutex;
Blue Swirl296af7c2010-03-29 19:23:50 +00001106
1107static QemuThread io_thread;
1108
Blue Swirl296af7c2010-03-29 19:23:50 +00001109/* cpu creation */
1110static QemuCond qemu_cpu_cond;
1111/* system init */
Blue Swirl296af7c2010-03-29 19:23:50 +00001112static QemuCond qemu_pause_cond;
1113
Paolo Bonzinid3b12f52011-09-13 10:30:52 +02001114void qemu_init_cpu_loop(void)
Blue Swirl296af7c2010-03-29 19:23:50 +00001115{
Jan Kiszka6d9cb732011-02-01 22:15:58 +01001116 qemu_init_sigbus();
Anthony Liguoried945922011-02-08 18:18:18 +01001117 qemu_cond_init(&qemu_cpu_cond);
Anthony Liguoried945922011-02-08 18:18:18 +01001118 qemu_cond_init(&qemu_pause_cond);
Blue Swirl296af7c2010-03-29 19:23:50 +00001119 qemu_mutex_init(&qemu_global_mutex);
Blue Swirl296af7c2010-03-29 19:23:50 +00001120
Jan Kiszkab7680cb2011-03-12 17:43:51 +01001121 qemu_thread_get_self(&io_thread);
Blue Swirl296af7c2010-03-29 19:23:50 +00001122}
1123
Paolo Bonzini14e6fe12016-10-31 10:36:08 +01001124void run_on_cpu(CPUState *cpu, run_on_cpu_func func, run_on_cpu_data data)
Marcelo Tosattie82bcec2010-05-04 09:45:22 -03001125{
Sergey Fedorovd148d902016-08-29 09:51:00 +02001126 do_run_on_cpu(cpu, func, data, &qemu_global_mutex);
Chegu Vinod3c022702013-06-24 03:49:41 -06001127}
1128
Gu Zheng4c055ab2016-05-12 09:18:13 +05301129static void qemu_kvm_destroy_vcpu(CPUState *cpu)
1130{
1131 if (kvm_destroy_vcpu(cpu) < 0) {
1132 error_report("kvm_destroy_vcpu failed");
1133 exit(EXIT_FAILURE);
1134 }
1135}
1136
1137static void qemu_tcg_destroy_vcpu(CPUState *cpu)
1138{
1139}
1140
David Hildenbrandebd05fe2017-11-29 20:12:15 +01001141static void qemu_cpu_stop(CPUState *cpu, bool exit)
1142{
1143 g_assert(qemu_cpu_is_self(cpu));
1144 cpu->stop = false;
1145 cpu->stopped = true;
1146 if (exit) {
1147 cpu_exit(cpu);
1148 }
1149 qemu_cond_broadcast(&qemu_pause_cond);
1150}
1151
Andreas Färber509a0d72012-05-03 02:18:09 +02001152static void qemu_wait_io_event_common(CPUState *cpu)
Blue Swirl296af7c2010-03-29 19:23:50 +00001153{
Alex Bennée37257942017-02-23 18:29:14 +00001154 atomic_mb_set(&cpu->thread_kicked, false);
Andreas Färber4fdeee72012-05-02 23:10:09 +02001155 if (cpu->stop) {
David Hildenbrandebd05fe2017-11-29 20:12:15 +01001156 qemu_cpu_stop(cpu, false);
Blue Swirl296af7c2010-03-29 19:23:50 +00001157 }
Sergey Fedorova5403c62016-08-02 18:27:36 +01001158 process_queued_cpu_work(cpu);
Alex Bennée37257942017-02-23 18:29:14 +00001159}
1160
Paolo Bonzinidb08b682018-01-11 13:53:12 +01001161static void qemu_tcg_rr_wait_io_event(CPUState *cpu)
Alex Bennée37257942017-02-23 18:29:14 +00001162{
Paolo Bonzinidb08b682018-01-11 13:53:12 +01001163 while (all_cpu_threads_idle()) {
Alex Bennée65467062017-02-23 18:29:09 +00001164 stop_tcg_kick_timer();
KONRAD Fredericd5f8d612015-08-10 17:27:06 +02001165 qemu_cond_wait(cpu->halt_cond, &qemu_global_mutex);
Jan Kiszka16400322011-02-09 16:29:37 +01001166 }
Blue Swirl296af7c2010-03-29 19:23:50 +00001167
Alex Bennée65467062017-02-23 18:29:09 +00001168 start_tcg_kick_timer();
1169
Alex Bennée37257942017-02-23 18:29:14 +00001170 qemu_wait_io_event_common(cpu);
Blue Swirl296af7c2010-03-29 19:23:50 +00001171}
1172
Paolo Bonzinidb08b682018-01-11 13:53:12 +01001173static void qemu_wait_io_event(CPUState *cpu)
Blue Swirl296af7c2010-03-29 19:23:50 +00001174{
Andreas Färbera98ae1d2013-05-26 23:21:08 +02001175 while (cpu_thread_is_idle(cpu)) {
Andreas Färberf5c121b2012-05-03 01:22:49 +02001176 qemu_cond_wait(cpu->halt_cond, &qemu_global_mutex);
Jan Kiszka16400322011-02-09 16:29:37 +01001177 }
Blue Swirl296af7c2010-03-29 19:23:50 +00001178
Paolo Bonzinidb08b682018-01-11 13:53:12 +01001179#ifdef _WIN32
1180 /* Eat dummy APC queued by qemu_cpu_kick_thread. */
1181 if (!tcg_enabled()) {
1182 SleepEx(0, TRUE);
Sergio Andres Gomez Del Realc97d6d22017-09-13 04:05:09 -05001183 }
Paolo Bonzinidb08b682018-01-11 13:53:12 +01001184#endif
Sergio Andres Gomez Del Realc97d6d22017-09-13 04:05:09 -05001185 qemu_wait_io_event_common(cpu);
1186}
1187
Jan Kiszka7e97cd82011-02-07 12:19:12 +01001188static void *qemu_kvm_cpu_thread_fn(void *arg)
Blue Swirl296af7c2010-03-29 19:23:50 +00001189{
Andreas Färber48a106b2013-05-27 02:20:39 +02001190 CPUState *cpu = arg;
Jan Kiszka84b49152011-02-01 22:15:50 +01001191 int r;
Blue Swirl296af7c2010-03-29 19:23:50 +00001192
Paolo Bonziniab28bd22015-07-09 08:55:38 +02001193 rcu_register_thread();
1194
Paolo Bonzini2e7f7a32015-06-18 18:47:18 +02001195 qemu_mutex_lock_iothread();
Andreas Färber814e6122012-05-02 17:00:37 +02001196 qemu_thread_get_self(cpu->thread);
Andreas Färber9f09e182012-05-03 06:59:07 +02001197 cpu->thread_id = qemu_get_thread_id();
Pavel Dovgalyuk626cf8f2014-12-08 10:53:17 +03001198 cpu->can_do_io = 1;
Andreas Färber4917cf42013-05-27 05:17:50 +02001199 current_cpu = cpu;
Blue Swirl296af7c2010-03-29 19:23:50 +00001200
Andreas Färber504134d2012-12-17 06:38:45 +01001201 r = kvm_init_vcpu(cpu);
Jan Kiszka84b49152011-02-01 22:15:50 +01001202 if (r < 0) {
Alistair Francis493d89b2018-02-03 09:43:14 +01001203 error_report("kvm_init_vcpu failed: %s", strerror(-r));
Jan Kiszka84b49152011-02-01 22:15:50 +01001204 exit(1);
1205 }
Blue Swirl296af7c2010-03-29 19:23:50 +00001206
Paolo Bonzini18268b62017-02-09 09:41:14 +01001207 kvm_init_cpu_signals(cpu);
Blue Swirl296af7c2010-03-29 19:23:50 +00001208
1209 /* signal CPU creation */
Andreas Färber61a46212012-05-02 22:49:36 +02001210 cpu->created = true;
Blue Swirl296af7c2010-03-29 19:23:50 +00001211 qemu_cond_signal(&qemu_cpu_cond);
1212
Gu Zheng4c055ab2016-05-12 09:18:13 +05301213 do {
Andreas Färbera1fcaa72012-05-02 23:42:26 +02001214 if (cpu_can_run(cpu)) {
Andreas Färber1458c362013-05-26 23:46:55 +02001215 r = kvm_cpu_exec(cpu);
Jan Kiszka83f338f2011-02-07 12:19:17 +01001216 if (r == EXCP_DEBUG) {
Andreas Färber91325042013-05-27 02:07:49 +02001217 cpu_handle_guest_debug(cpu);
Jan Kiszka83f338f2011-02-07 12:19:17 +01001218 }
Jan Kiszka0ab07c62011-02-07 12:19:14 +01001219 }
Paolo Bonzinidb08b682018-01-11 13:53:12 +01001220 qemu_wait_io_event(cpu);
Gu Zheng4c055ab2016-05-12 09:18:13 +05301221 } while (!cpu->unplug || cpu_can_run(cpu));
Blue Swirl296af7c2010-03-29 19:23:50 +00001222
Gu Zheng4c055ab2016-05-12 09:18:13 +05301223 qemu_kvm_destroy_vcpu(cpu);
Bharata B Rao2c579042016-05-12 09:18:14 +05301224 cpu->created = false;
1225 qemu_cond_signal(&qemu_cpu_cond);
Gu Zheng4c055ab2016-05-12 09:18:13 +05301226 qemu_mutex_unlock_iothread();
Paolo Bonzini57615ed2018-01-30 11:04:36 -05001227 rcu_unregister_thread();
Blue Swirl296af7c2010-03-29 19:23:50 +00001228 return NULL;
1229}
1230
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +02001231static void *qemu_dummy_cpu_thread_fn(void *arg)
1232{
1233#ifdef _WIN32
Alistair Francis493d89b2018-02-03 09:43:14 +01001234 error_report("qtest is not supported under Windows");
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +02001235 exit(1);
1236#else
Andreas Färber10a90212013-05-27 02:24:35 +02001237 CPUState *cpu = arg;
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +02001238 sigset_t waitset;
1239 int r;
1240
Paolo Bonziniab28bd22015-07-09 08:55:38 +02001241 rcu_register_thread();
1242
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +02001243 qemu_mutex_lock_iothread();
Andreas Färber814e6122012-05-02 17:00:37 +02001244 qemu_thread_get_self(cpu->thread);
Andreas Färber9f09e182012-05-03 06:59:07 +02001245 cpu->thread_id = qemu_get_thread_id();
Pavel Dovgalyuk626cf8f2014-12-08 10:53:17 +03001246 cpu->can_do_io = 1;
Alex Bennée37257942017-02-23 18:29:14 +00001247 current_cpu = cpu;
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +02001248
1249 sigemptyset(&waitset);
1250 sigaddset(&waitset, SIG_IPI);
1251
1252 /* signal CPU creation */
Andreas Färber61a46212012-05-02 22:49:36 +02001253 cpu->created = true;
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +02001254 qemu_cond_signal(&qemu_cpu_cond);
1255
Paolo Bonzinid2831ab2018-01-30 11:04:53 -05001256 do {
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +02001257 qemu_mutex_unlock_iothread();
1258 do {
1259 int sig;
1260 r = sigwait(&waitset, &sig);
1261 } while (r == -1 && (errno == EAGAIN || errno == EINTR));
1262 if (r == -1) {
1263 perror("sigwait");
1264 exit(1);
1265 }
1266 qemu_mutex_lock_iothread();
Paolo Bonzinidb08b682018-01-11 13:53:12 +01001267 qemu_wait_io_event(cpu);
Paolo Bonzinid2831ab2018-01-30 11:04:53 -05001268 } while (!cpu->unplug);
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +02001269
Paolo Bonzinid2831ab2018-01-30 11:04:53 -05001270 rcu_unregister_thread();
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +02001271 return NULL;
1272#endif
1273}
1274
Alex Bennée1be7fcb2016-10-27 16:10:08 +01001275static int64_t tcg_get_icount_limit(void)
1276{
1277 int64_t deadline;
1278
1279 if (replay_mode != REPLAY_MODE_PLAY) {
1280 deadline = qemu_clock_deadline_ns_all(QEMU_CLOCK_VIRTUAL);
1281
1282 /* Maintain prior (possibly buggy) behaviour where if no deadline
1283 * was set (as there is no QEMU_CLOCK_VIRTUAL timer) or it is more than
1284 * INT32_MAX nanoseconds ahead, we still use INT32_MAX
1285 * nanoseconds.
1286 */
1287 if ((deadline < 0) || (deadline > INT32_MAX)) {
1288 deadline = INT32_MAX;
1289 }
1290
1291 return qemu_icount_round(deadline);
1292 } else {
1293 return replay_get_instructions();
1294 }
1295}
1296
Alex Bennée12e97002016-10-27 16:10:14 +01001297static void handle_icount_deadline(void)
1298{
Paolo Bonzini6b8f0182017-03-02 19:56:40 +01001299 assert(qemu_in_vcpu_thread());
Alex Bennée12e97002016-10-27 16:10:14 +01001300 if (use_icount) {
1301 int64_t deadline =
1302 qemu_clock_deadline_ns_all(QEMU_CLOCK_VIRTUAL);
1303
1304 if (deadline == 0) {
Paolo Bonzini6b8f0182017-03-02 19:56:40 +01001305 /* Wake up other AioContexts. */
Alex Bennée12e97002016-10-27 16:10:14 +01001306 qemu_clock_notify(QEMU_CLOCK_VIRTUAL);
Paolo Bonzini6b8f0182017-03-02 19:56:40 +01001307 qemu_clock_run_timers(QEMU_CLOCK_VIRTUAL);
Alex Bennée12e97002016-10-27 16:10:14 +01001308 }
1309 }
1310}
1311
Alex Bennée05248382017-03-29 16:46:59 +01001312static void prepare_icount_for_run(CPUState *cpu)
1313{
1314 if (use_icount) {
Alex Bennéeeda5f7c2017-04-05 12:35:48 +01001315 int insns_left;
Alex Bennée05248382017-03-29 16:46:59 +01001316
1317 /* These should always be cleared by process_icount_data after
1318 * each vCPU execution. However u16.high can be raised
1319 * asynchronously by cpu_exit/cpu_interrupt/tcg_handle_interrupt
1320 */
1321 g_assert(cpu->icount_decr.u16.low == 0);
1322 g_assert(cpu->icount_extra == 0);
1323
Alex Bennéeeda5f7c2017-04-05 12:35:48 +01001324 cpu->icount_budget = tcg_get_icount_limit();
1325 insns_left = MIN(0xffff, cpu->icount_budget);
1326 cpu->icount_decr.u16.low = insns_left;
1327 cpu->icount_extra = cpu->icount_budget - insns_left;
Alex Bennéed759c952018-02-27 12:52:48 +03001328
1329 replay_mutex_lock();
Alex Bennée05248382017-03-29 16:46:59 +01001330 }
1331}
1332
1333static void process_icount_data(CPUState *cpu)
1334{
1335 if (use_icount) {
Alex Bennéee4cd9652017-03-31 16:09:42 +01001336 /* Account for executed instructions */
Alex Bennée512d3c82017-04-05 12:32:37 +01001337 cpu_update_icount(cpu);
Alex Bennée05248382017-03-29 16:46:59 +01001338
1339 /* Reset the counters */
1340 cpu->icount_decr.u16.low = 0;
1341 cpu->icount_extra = 0;
Alex Bennéee4cd9652017-03-31 16:09:42 +01001342 cpu->icount_budget = 0;
1343
Alex Bennée05248382017-03-29 16:46:59 +01001344 replay_account_executed_instructions();
Alex Bennéed759c952018-02-27 12:52:48 +03001345
1346 replay_mutex_unlock();
Alex Bennée05248382017-03-29 16:46:59 +01001347 }
1348}
1349
1350
Alex Bennée1be7fcb2016-10-27 16:10:08 +01001351static int tcg_cpu_exec(CPUState *cpu)
1352{
1353 int ret;
1354#ifdef CONFIG_PROFILER
1355 int64_t ti;
1356#endif
1357
1358#ifdef CONFIG_PROFILER
1359 ti = profile_getclock();
1360#endif
Alex Bennée1be7fcb2016-10-27 16:10:08 +01001361 cpu_exec_start(cpu);
1362 ret = cpu_exec(cpu);
1363 cpu_exec_end(cpu);
1364#ifdef CONFIG_PROFILER
1365 tcg_time += profile_getclock() - ti;
1366#endif
Alex Bennée1be7fcb2016-10-27 16:10:08 +01001367 return ret;
1368}
1369
Alex Bennéec93bbbe2016-10-27 16:10:09 +01001370/* Destroy any remaining vCPUs which have been unplugged and have
1371 * finished running
1372 */
1373static void deal_with_unplugged_cpus(void)
Alex Bennée1be7fcb2016-10-27 16:10:08 +01001374{
Alex Bennéec93bbbe2016-10-27 16:10:09 +01001375 CPUState *cpu;
Alex Bennée1be7fcb2016-10-27 16:10:08 +01001376
Alex Bennéec93bbbe2016-10-27 16:10:09 +01001377 CPU_FOREACH(cpu) {
1378 if (cpu->unplug && !cpu_can_run(cpu)) {
1379 qemu_tcg_destroy_vcpu(cpu);
1380 cpu->created = false;
1381 qemu_cond_signal(&qemu_cpu_cond);
Alex Bennée1be7fcb2016-10-27 16:10:08 +01001382 break;
1383 }
1384 }
Alex Bennée1be7fcb2016-10-27 16:10:08 +01001385}
Jan Kiszkabdb7ca62011-09-26 09:40:39 +02001386
Alex Bennée65467062017-02-23 18:29:09 +00001387/* Single-threaded TCG
1388 *
1389 * In the single-threaded case each vCPU is simulated in turn. If
1390 * there is more than a single vCPU we create a simple timer to kick
1391 * the vCPU and ensure we don't get stuck in a tight loop in one vCPU.
1392 * This is done explicitly rather than relying on side-effects
1393 * elsewhere.
1394 */
1395
Alex Bennée37257942017-02-23 18:29:14 +00001396static void *qemu_tcg_rr_cpu_thread_fn(void *arg)
Blue Swirl296af7c2010-03-29 19:23:50 +00001397{
Andreas Färberc3586ba2012-05-03 01:41:24 +02001398 CPUState *cpu = arg;
Blue Swirl296af7c2010-03-29 19:23:50 +00001399
Paolo Bonziniab28bd22015-07-09 08:55:38 +02001400 rcu_register_thread();
Emilio G. Cota3468b592017-07-19 18:57:58 -04001401 tcg_register_thread();
Paolo Bonziniab28bd22015-07-09 08:55:38 +02001402
Paolo Bonzini2e7f7a32015-06-18 18:47:18 +02001403 qemu_mutex_lock_iothread();
Andreas Färber814e6122012-05-02 17:00:37 +02001404 qemu_thread_get_self(cpu->thread);
Blue Swirl296af7c2010-03-29 19:23:50 +00001405
David Hildenbrand5a9c9732018-02-09 20:52:39 +01001406 cpu->thread_id = qemu_get_thread_id();
1407 cpu->created = true;
1408 cpu->can_do_io = 1;
Blue Swirl296af7c2010-03-29 19:23:50 +00001409 qemu_cond_signal(&qemu_cpu_cond);
1410
Jan Kiszkafa7d1862011-08-22 18:35:25 +02001411 /* wait for initial kick-off after machine start */
Emilio G. Cotac28e3992015-04-27 12:45:28 -04001412 while (first_cpu->stopped) {
KONRAD Fredericd5f8d612015-08-10 17:27:06 +02001413 qemu_cond_wait(first_cpu->halt_cond, &qemu_global_mutex);
Jan Kiszka8e564b42012-02-17 18:31:15 +01001414
1415 /* process any pending work */
Andreas Färberbdc44642013-06-24 23:50:24 +02001416 CPU_FOREACH(cpu) {
Alex Bennée37257942017-02-23 18:29:14 +00001417 current_cpu = cpu;
Andreas Färber182735e2013-05-29 22:29:20 +02001418 qemu_wait_io_event_common(cpu);
Jan Kiszka8e564b42012-02-17 18:31:15 +01001419 }
Jan Kiszka0ab07c62011-02-07 12:19:14 +01001420 }
Blue Swirl296af7c2010-03-29 19:23:50 +00001421
Alex Bennée65467062017-02-23 18:29:09 +00001422 start_tcg_kick_timer();
1423
Alex Bennéec93bbbe2016-10-27 16:10:09 +01001424 cpu = first_cpu;
1425
Alex Bennéee5143e32017-02-23 18:29:12 +00001426 /* process any pending work */
1427 cpu->exit_request = 1;
1428
Blue Swirl296af7c2010-03-29 19:23:50 +00001429 while (1) {
Alex Bennéed759c952018-02-27 12:52:48 +03001430 qemu_mutex_unlock_iothread();
1431 replay_mutex_lock();
1432 qemu_mutex_lock_iothread();
Alex Bennéec93bbbe2016-10-27 16:10:09 +01001433 /* Account partial waits to QEMU_CLOCK_VIRTUAL. */
1434 qemu_account_warp_timer();
1435
Paolo Bonzini6b8f0182017-03-02 19:56:40 +01001436 /* Run the timers here. This is much more efficient than
1437 * waking up the I/O thread and waiting for completion.
1438 */
1439 handle_icount_deadline();
1440
Alex Bennéed759c952018-02-27 12:52:48 +03001441 replay_mutex_unlock();
1442
Alex Bennéec93bbbe2016-10-27 16:10:09 +01001443 if (!cpu) {
1444 cpu = first_cpu;
1445 }
1446
Alex Bennéee5143e32017-02-23 18:29:12 +00001447 while (cpu && !cpu->queued_work_first && !cpu->exit_request) {
1448
Alex Bennée791158d2017-02-23 18:29:10 +00001449 atomic_mb_set(&tcg_current_rr_cpu, cpu);
Alex Bennée37257942017-02-23 18:29:14 +00001450 current_cpu = cpu;
Alex Bennéec93bbbe2016-10-27 16:10:09 +01001451
1452 qemu_clock_enable(QEMU_CLOCK_VIRTUAL,
1453 (cpu->singlestep_enabled & SSTEP_NOTIMER) == 0);
1454
1455 if (cpu_can_run(cpu)) {
1456 int r;
Alex Bennée05248382017-03-29 16:46:59 +01001457
Alex Bennéed759c952018-02-27 12:52:48 +03001458 qemu_mutex_unlock_iothread();
Alex Bennée05248382017-03-29 16:46:59 +01001459 prepare_icount_for_run(cpu);
1460
Alex Bennéec93bbbe2016-10-27 16:10:09 +01001461 r = tcg_cpu_exec(cpu);
Alex Bennée05248382017-03-29 16:46:59 +01001462
1463 process_icount_data(cpu);
Alex Bennéed759c952018-02-27 12:52:48 +03001464 qemu_mutex_lock_iothread();
Alex Bennée05248382017-03-29 16:46:59 +01001465
Alex Bennéec93bbbe2016-10-27 16:10:09 +01001466 if (r == EXCP_DEBUG) {
1467 cpu_handle_guest_debug(cpu);
1468 break;
Pranith Kumar08e73c42017-02-23 18:29:15 +00001469 } else if (r == EXCP_ATOMIC) {
1470 qemu_mutex_unlock_iothread();
1471 cpu_exec_step_atomic(cpu);
1472 qemu_mutex_lock_iothread();
1473 break;
Alex Bennéec93bbbe2016-10-27 16:10:09 +01001474 }
Alex Bennée37257942017-02-23 18:29:14 +00001475 } else if (cpu->stop) {
Alex Bennéec93bbbe2016-10-27 16:10:09 +01001476 if (cpu->unplug) {
1477 cpu = CPU_NEXT(cpu);
1478 }
1479 break;
1480 }
1481
Alex Bennéee5143e32017-02-23 18:29:12 +00001482 cpu = CPU_NEXT(cpu);
1483 } /* while (cpu && !cpu->exit_request).. */
1484
Alex Bennée791158d2017-02-23 18:29:10 +00001485 /* Does not need atomic_mb_set because a spurious wakeup is okay. */
1486 atomic_set(&tcg_current_rr_cpu, NULL);
Alex Bennéec93bbbe2016-10-27 16:10:09 +01001487
Alex Bennéee5143e32017-02-23 18:29:12 +00001488 if (cpu && cpu->exit_request) {
1489 atomic_mb_set(&cpu->exit_request, 0);
1490 }
Alex Blighac70aaf2013-08-21 16:02:57 +01001491
Paolo Bonzinidb08b682018-01-11 13:53:12 +01001492 qemu_tcg_rr_wait_io_event(cpu ? cpu : QTAILQ_FIRST(&cpus));
Alex Bennéec93bbbe2016-10-27 16:10:09 +01001493 deal_with_unplugged_cpus();
Blue Swirl296af7c2010-03-29 19:23:50 +00001494 }
1495
Paolo Bonzini9b0605f2018-01-30 11:05:06 -05001496 rcu_unregister_thread();
Blue Swirl296af7c2010-03-29 19:23:50 +00001497 return NULL;
1498}
1499
Vincent Palatinb0cb0a62017-01-10 11:59:57 +01001500static void *qemu_hax_cpu_thread_fn(void *arg)
1501{
1502 CPUState *cpu = arg;
1503 int r;
Vincent Palatinb3d3a422017-03-20 11:15:49 +01001504
Paolo Bonzini9857c2d2018-01-30 16:28:49 +01001505 rcu_register_thread();
Vincent Palatinb3d3a422017-03-20 11:15:49 +01001506 qemu_mutex_lock_iothread();
Vincent Palatinb0cb0a62017-01-10 11:59:57 +01001507 qemu_thread_get_self(cpu->thread);
Vincent Palatinb0cb0a62017-01-10 11:59:57 +01001508
1509 cpu->thread_id = qemu_get_thread_id();
1510 cpu->created = true;
1511 cpu->halted = 0;
1512 current_cpu = cpu;
1513
1514 hax_init_vcpu(cpu);
1515 qemu_cond_signal(&qemu_cpu_cond);
1516
Paolo Bonzini9857c2d2018-01-30 16:28:49 +01001517 do {
Vincent Palatinb0cb0a62017-01-10 11:59:57 +01001518 if (cpu_can_run(cpu)) {
1519 r = hax_smp_cpu_exec(cpu);
1520 if (r == EXCP_DEBUG) {
1521 cpu_handle_guest_debug(cpu);
1522 }
1523 }
1524
Paolo Bonzinidb08b682018-01-11 13:53:12 +01001525 qemu_wait_io_event(cpu);
Paolo Bonzini9857c2d2018-01-30 16:28:49 +01001526 } while (!cpu->unplug || cpu_can_run(cpu));
1527 rcu_unregister_thread();
Vincent Palatinb0cb0a62017-01-10 11:59:57 +01001528 return NULL;
1529}
1530
Sergio Andres Gomez Del Realc97d6d22017-09-13 04:05:09 -05001531/* The HVF-specific vCPU thread function. This one should only run when the host
1532 * CPU supports the VMX "unrestricted guest" feature. */
1533static void *qemu_hvf_cpu_thread_fn(void *arg)
1534{
1535 CPUState *cpu = arg;
1536
1537 int r;
1538
1539 assert(hvf_enabled());
1540
1541 rcu_register_thread();
1542
1543 qemu_mutex_lock_iothread();
1544 qemu_thread_get_self(cpu->thread);
1545
1546 cpu->thread_id = qemu_get_thread_id();
1547 cpu->can_do_io = 1;
1548 current_cpu = cpu;
1549
1550 hvf_init_vcpu(cpu);
1551
1552 /* signal CPU creation */
1553 cpu->created = true;
1554 qemu_cond_signal(&qemu_cpu_cond);
1555
1556 do {
1557 if (cpu_can_run(cpu)) {
1558 r = hvf_vcpu_exec(cpu);
1559 if (r == EXCP_DEBUG) {
1560 cpu_handle_guest_debug(cpu);
1561 }
1562 }
Paolo Bonzinidb08b682018-01-11 13:53:12 +01001563 qemu_wait_io_event(cpu);
Sergio Andres Gomez Del Realc97d6d22017-09-13 04:05:09 -05001564 } while (!cpu->unplug || cpu_can_run(cpu));
1565
1566 hvf_vcpu_destroy(cpu);
1567 cpu->created = false;
1568 qemu_cond_signal(&qemu_cpu_cond);
1569 qemu_mutex_unlock_iothread();
Paolo Bonzini8178e632018-01-30 11:05:21 -05001570 rcu_unregister_thread();
Sergio Andres Gomez Del Realc97d6d22017-09-13 04:05:09 -05001571 return NULL;
1572}
1573
Justin Terry (VM)19306802018-01-22 13:07:49 -08001574static void *qemu_whpx_cpu_thread_fn(void *arg)
1575{
1576 CPUState *cpu = arg;
1577 int r;
1578
1579 rcu_register_thread();
1580
1581 qemu_mutex_lock_iothread();
1582 qemu_thread_get_self(cpu->thread);
1583 cpu->thread_id = qemu_get_thread_id();
1584 current_cpu = cpu;
1585
1586 r = whpx_init_vcpu(cpu);
1587 if (r < 0) {
1588 fprintf(stderr, "whpx_init_vcpu failed: %s\n", strerror(-r));
1589 exit(1);
1590 }
1591
1592 /* signal CPU creation */
1593 cpu->created = true;
1594 qemu_cond_signal(&qemu_cpu_cond);
1595
1596 do {
1597 if (cpu_can_run(cpu)) {
1598 r = whpx_vcpu_exec(cpu);
1599 if (r == EXCP_DEBUG) {
1600 cpu_handle_guest_debug(cpu);
1601 }
1602 }
1603 while (cpu_thread_is_idle(cpu)) {
1604 qemu_cond_wait(cpu->halt_cond, &qemu_global_mutex);
1605 }
1606 qemu_wait_io_event_common(cpu);
1607 } while (!cpu->unplug || cpu_can_run(cpu));
1608
1609 whpx_destroy_vcpu(cpu);
1610 cpu->created = false;
1611 qemu_cond_signal(&qemu_cpu_cond);
1612 qemu_mutex_unlock_iothread();
1613 rcu_unregister_thread();
Vincent Palatinb0cb0a62017-01-10 11:59:57 +01001614 return NULL;
1615}
1616
1617#ifdef _WIN32
1618static void CALLBACK dummy_apc_func(ULONG_PTR unused)
1619{
1620}
1621#endif
1622
Alex Bennée37257942017-02-23 18:29:14 +00001623/* Multi-threaded TCG
1624 *
1625 * In the multi-threaded case each vCPU has its own thread. The TLS
1626 * variable current_cpu can be used deep in the code to find the
1627 * current CPUState for a given thread.
1628 */
1629
1630static void *qemu_tcg_cpu_thread_fn(void *arg)
1631{
1632 CPUState *cpu = arg;
1633
Alex Bennéebf51c722017-03-30 18:32:29 +01001634 g_assert(!use_icount);
1635
Alex Bennée37257942017-02-23 18:29:14 +00001636 rcu_register_thread();
Emilio G. Cota3468b592017-07-19 18:57:58 -04001637 tcg_register_thread();
Alex Bennée37257942017-02-23 18:29:14 +00001638
1639 qemu_mutex_lock_iothread();
1640 qemu_thread_get_self(cpu->thread);
1641
1642 cpu->thread_id = qemu_get_thread_id();
1643 cpu->created = true;
1644 cpu->can_do_io = 1;
1645 current_cpu = cpu;
1646 qemu_cond_signal(&qemu_cpu_cond);
1647
1648 /* process any pending work */
1649 cpu->exit_request = 1;
1650
Cédric Le Goater54961aa2018-04-25 15:18:28 +02001651 do {
Alex Bennée37257942017-02-23 18:29:14 +00001652 if (cpu_can_run(cpu)) {
1653 int r;
Alex Bennéed759c952018-02-27 12:52:48 +03001654 qemu_mutex_unlock_iothread();
Alex Bennée37257942017-02-23 18:29:14 +00001655 r = tcg_cpu_exec(cpu);
Alex Bennéed759c952018-02-27 12:52:48 +03001656 qemu_mutex_lock_iothread();
Alex Bennée37257942017-02-23 18:29:14 +00001657 switch (r) {
1658 case EXCP_DEBUG:
1659 cpu_handle_guest_debug(cpu);
1660 break;
1661 case EXCP_HALTED:
1662 /* during start-up the vCPU is reset and the thread is
1663 * kicked several times. If we don't ensure we go back
1664 * to sleep in the halted state we won't cleanly
1665 * start-up when the vCPU is enabled.
1666 *
1667 * cpu->halted should ensure we sleep in wait_io_event
1668 */
1669 g_assert(cpu->halted);
1670 break;
Pranith Kumar08e73c42017-02-23 18:29:15 +00001671 case EXCP_ATOMIC:
1672 qemu_mutex_unlock_iothread();
1673 cpu_exec_step_atomic(cpu);
1674 qemu_mutex_lock_iothread();
Alex Bennée37257942017-02-23 18:29:14 +00001675 default:
1676 /* Ignore everything else? */
1677 break;
1678 }
1679 }
1680
Alex Bennée37257942017-02-23 18:29:14 +00001681 atomic_mb_set(&cpu->exit_request, 0);
Paolo Bonzinidb08b682018-01-11 13:53:12 +01001682 qemu_wait_io_event(cpu);
Paolo Bonzini9b0605f2018-01-30 11:05:06 -05001683 } while (!cpu->unplug || cpu_can_run(cpu));
Alex Bennée37257942017-02-23 18:29:14 +00001684
Paolo Bonzini9b0605f2018-01-30 11:05:06 -05001685 qemu_tcg_destroy_vcpu(cpu);
1686 cpu->created = false;
1687 qemu_cond_signal(&qemu_cpu_cond);
1688 qemu_mutex_unlock_iothread();
1689 rcu_unregister_thread();
Alex Bennée37257942017-02-23 18:29:14 +00001690 return NULL;
1691}
1692
Andreas Färber2ff09a42012-05-03 00:23:30 +02001693static void qemu_cpu_kick_thread(CPUState *cpu)
Paolo Bonzinicc015e92011-03-12 17:44:08 +01001694{
1695#ifndef _WIN32
1696 int err;
1697
Paolo Bonzinie0c38212015-08-26 00:19:19 +02001698 if (cpu->thread_kicked) {
1699 return;
Paolo Bonzini9102ded2015-08-18 06:52:09 -07001700 }
Paolo Bonzinie0c38212015-08-26 00:19:19 +02001701 cpu->thread_kicked = true;
Andreas Färber814e6122012-05-02 17:00:37 +02001702 err = pthread_kill(cpu->thread->thread, SIG_IPI);
Paolo Bonzinicc015e92011-03-12 17:44:08 +01001703 if (err) {
1704 fprintf(stderr, "qemu:%s: %s", __func__, strerror(err));
1705 exit(1);
1706 }
1707#else /* _WIN32 */
Vincent Palatinb0cb0a62017-01-10 11:59:57 +01001708 if (!qemu_cpu_is_self(cpu)) {
Justin Terry (VM)19306802018-01-22 13:07:49 -08001709 if (whpx_enabled()) {
1710 whpx_vcpu_kick(cpu);
1711 } else if (!QueueUserAPC(dummy_apc_func, cpu->hThread, 0)) {
Vincent Palatinb0cb0a62017-01-10 11:59:57 +01001712 fprintf(stderr, "%s: QueueUserAPC failed with error %lu\n",
1713 __func__, GetLastError());
1714 exit(1);
1715 }
1716 }
Paolo Bonzinicc015e92011-03-12 17:44:08 +01001717#endif
1718}
1719
Andreas Färberc08d7422012-05-03 04:34:15 +02001720void qemu_cpu_kick(CPUState *cpu)
Blue Swirl296af7c2010-03-29 19:23:50 +00001721{
Andreas Färberf5c121b2012-05-03 01:22:49 +02001722 qemu_cond_broadcast(cpu->halt_cond);
Paolo Bonzinie0c38212015-08-26 00:19:19 +02001723 if (tcg_enabled()) {
Alex Bennée791158d2017-02-23 18:29:10 +00001724 cpu_exit(cpu);
Alex Bennée37257942017-02-23 18:29:14 +00001725 /* NOP unless doing single-thread RR */
Alex Bennée791158d2017-02-23 18:29:10 +00001726 qemu_cpu_kick_rr_cpu();
Paolo Bonzinie0c38212015-08-26 00:19:19 +02001727 } else {
Vincent Palatinb0cb0a62017-01-10 11:59:57 +01001728 if (hax_enabled()) {
1729 /*
1730 * FIXME: race condition with the exit_request check in
1731 * hax_vcpu_hax_exec
1732 */
1733 cpu->exit_request = 1;
1734 }
Paolo Bonzinie0c38212015-08-26 00:19:19 +02001735 qemu_cpu_kick_thread(cpu);
1736 }
Blue Swirl296af7c2010-03-29 19:23:50 +00001737}
1738
Jan Kiszka46d62fa2011-02-01 22:15:59 +01001739void qemu_cpu_kick_self(void)
1740{
Andreas Färber4917cf42013-05-27 05:17:50 +02001741 assert(current_cpu);
Paolo Bonzini9102ded2015-08-18 06:52:09 -07001742 qemu_cpu_kick_thread(current_cpu);
Blue Swirl296af7c2010-03-29 19:23:50 +00001743}
1744
Andreas Färber60e82572012-05-02 22:23:49 +02001745bool qemu_cpu_is_self(CPUState *cpu)
Blue Swirl296af7c2010-03-29 19:23:50 +00001746{
Andreas Färber814e6122012-05-02 17:00:37 +02001747 return qemu_thread_is_self(cpu->thread);
Blue Swirl296af7c2010-03-29 19:23:50 +00001748}
1749
Paolo Bonzini79e2b9a2015-01-21 12:09:14 +01001750bool qemu_in_vcpu_thread(void)
Juan Quintelaaa723c22012-09-18 16:30:11 +02001751{
Andreas Färber4917cf42013-05-27 05:17:50 +02001752 return current_cpu && qemu_cpu_is_self(current_cpu);
Juan Quintelaaa723c22012-09-18 16:30:11 +02001753}
1754
Paolo Bonziniafbe7052015-06-18 18:47:19 +02001755static __thread bool iothread_locked = false;
1756
1757bool qemu_mutex_iothread_locked(void)
1758{
1759 return iothread_locked;
1760}
1761
Blue Swirl296af7c2010-03-29 19:23:50 +00001762void qemu_mutex_lock_iothread(void)
1763{
Jan Kiszka8d04fb52017-02-23 18:29:11 +00001764 g_assert(!qemu_mutex_iothread_locked());
1765 qemu_mutex_lock(&qemu_global_mutex);
Paolo Bonziniafbe7052015-06-18 18:47:19 +02001766 iothread_locked = true;
Blue Swirl296af7c2010-03-29 19:23:50 +00001767}
1768
1769void qemu_mutex_unlock_iothread(void)
1770{
Jan Kiszka8d04fb52017-02-23 18:29:11 +00001771 g_assert(qemu_mutex_iothread_locked());
Paolo Bonziniafbe7052015-06-18 18:47:19 +02001772 iothread_locked = false;
Blue Swirl296af7c2010-03-29 19:23:50 +00001773 qemu_mutex_unlock(&qemu_global_mutex);
1774}
1775
Alex Bennéee8faee02016-10-27 16:09:58 +01001776static bool all_vcpus_paused(void)
Blue Swirl296af7c2010-03-29 19:23:50 +00001777{
Andreas Färberbdc44642013-06-24 23:50:24 +02001778 CPUState *cpu;
Blue Swirl296af7c2010-03-29 19:23:50 +00001779
Andreas Färberbdc44642013-06-24 23:50:24 +02001780 CPU_FOREACH(cpu) {
Andreas Färber182735e2013-05-29 22:29:20 +02001781 if (!cpu->stopped) {
Alex Bennéee8faee02016-10-27 16:09:58 +01001782 return false;
Jan Kiszka0ab07c62011-02-07 12:19:14 +01001783 }
Blue Swirl296af7c2010-03-29 19:23:50 +00001784 }
1785
Alex Bennéee8faee02016-10-27 16:09:58 +01001786 return true;
Blue Swirl296af7c2010-03-29 19:23:50 +00001787}
1788
1789void pause_all_vcpus(void)
1790{
Andreas Färberbdc44642013-06-24 23:50:24 +02001791 CPUState *cpu;
Blue Swirl296af7c2010-03-29 19:23:50 +00001792
Alex Bligh40daca52013-08-21 16:03:02 +01001793 qemu_clock_enable(QEMU_CLOCK_VIRTUAL, false);
Andreas Färberbdc44642013-06-24 23:50:24 +02001794 CPU_FOREACH(cpu) {
David Hildenbrandebd05fe2017-11-29 20:12:15 +01001795 if (qemu_cpu_is_self(cpu)) {
1796 qemu_cpu_stop(cpu, true);
1797 } else {
1798 cpu->stop = true;
1799 qemu_cpu_kick(cpu);
1800 }
Jan Kiszkad798e972012-02-17 18:31:16 +01001801 }
1802
Alex Bennéed759c952018-02-27 12:52:48 +03001803 /* We need to drop the replay_lock so any vCPU threads woken up
1804 * can finish their replay tasks
1805 */
1806 replay_mutex_unlock();
1807
Blue Swirl296af7c2010-03-29 19:23:50 +00001808 while (!all_vcpus_paused()) {
Paolo Bonzinibe7d6c52011-03-12 17:44:02 +01001809 qemu_cond_wait(&qemu_pause_cond, &qemu_global_mutex);
Andreas Färberbdc44642013-06-24 23:50:24 +02001810 CPU_FOREACH(cpu) {
Andreas Färber182735e2013-05-29 22:29:20 +02001811 qemu_cpu_kick(cpu);
Blue Swirl296af7c2010-03-29 19:23:50 +00001812 }
1813 }
Alex Bennéed759c952018-02-27 12:52:48 +03001814
1815 qemu_mutex_unlock_iothread();
1816 replay_mutex_lock();
1817 qemu_mutex_lock_iothread();
Blue Swirl296af7c2010-03-29 19:23:50 +00001818}
1819
Igor Mammedov29936832013-04-23 10:29:37 +02001820void cpu_resume(CPUState *cpu)
1821{
1822 cpu->stop = false;
1823 cpu->stopped = false;
1824 qemu_cpu_kick(cpu);
1825}
1826
Blue Swirl296af7c2010-03-29 19:23:50 +00001827void resume_all_vcpus(void)
1828{
Andreas Färberbdc44642013-06-24 23:50:24 +02001829 CPUState *cpu;
Blue Swirl296af7c2010-03-29 19:23:50 +00001830
Alex Bligh40daca52013-08-21 16:03:02 +01001831 qemu_clock_enable(QEMU_CLOCK_VIRTUAL, true);
Andreas Färberbdc44642013-06-24 23:50:24 +02001832 CPU_FOREACH(cpu) {
Andreas Färber182735e2013-05-29 22:29:20 +02001833 cpu_resume(cpu);
Blue Swirl296af7c2010-03-29 19:23:50 +00001834 }
1835}
1836
Paolo Bonzinidbadee42018-01-30 16:40:12 +01001837void cpu_remove_sync(CPUState *cpu)
Gu Zheng4c055ab2016-05-12 09:18:13 +05301838{
1839 cpu->stop = true;
1840 cpu->unplug = true;
1841 qemu_cpu_kick(cpu);
Paolo Bonzinidbadee42018-01-30 16:40:12 +01001842 qemu_mutex_unlock_iothread();
1843 qemu_thread_join(cpu->thread);
1844 qemu_mutex_lock_iothread();
Bharata B Rao2c579042016-05-12 09:18:14 +05301845}
1846
Dr. David Alan Gilbert49001162014-01-30 10:20:32 +00001847/* For temporary buffers for forming a name */
1848#define VCPU_THREAD_NAME_SIZE 16
1849
Andreas Färbere5ab30a2012-05-03 01:50:44 +02001850static void qemu_tcg_init_vcpu(CPUState *cpu)
Blue Swirl296af7c2010-03-29 19:23:50 +00001851{
Dr. David Alan Gilbert49001162014-01-30 10:20:32 +00001852 char thread_name[VCPU_THREAD_NAME_SIZE];
Alex Bennée37257942017-02-23 18:29:14 +00001853 static QemuCond *single_tcg_halt_cond;
1854 static QemuThread *single_tcg_cpu_thread;
Emilio G. Cotae8feb962017-07-07 19:24:20 -04001855 static int tcg_region_inited;
1856
1857 /*
1858 * Initialize TCG regions--once. Now is a good time, because:
1859 * (1) TCG's init context, prologue and target globals have been set up.
1860 * (2) qemu_tcg_mttcg_enabled() works now (TCG init code runs before the
1861 * -accel flag is processed, so the check doesn't work then).
1862 */
1863 if (!tcg_region_inited) {
1864 tcg_region_inited = 1;
1865 tcg_region_init();
1866 }
Dr. David Alan Gilbert49001162014-01-30 10:20:32 +00001867
Alex Bennée37257942017-02-23 18:29:14 +00001868 if (qemu_tcg_mttcg_enabled() || !single_tcg_cpu_thread) {
Andreas Färber814e6122012-05-02 17:00:37 +02001869 cpu->thread = g_malloc0(sizeof(QemuThread));
Andreas Färberf5c121b2012-05-03 01:22:49 +02001870 cpu->halt_cond = g_malloc0(sizeof(QemuCond));
1871 qemu_cond_init(cpu->halt_cond);
Alex Bennée37257942017-02-23 18:29:14 +00001872
1873 if (qemu_tcg_mttcg_enabled()) {
1874 /* create a thread per vCPU with TCG (MTTCG) */
1875 parallel_cpus = true;
1876 snprintf(thread_name, VCPU_THREAD_NAME_SIZE, "CPU %d/TCG",
Dr. David Alan Gilbert49001162014-01-30 10:20:32 +00001877 cpu->cpu_index);
Alex Bennée37257942017-02-23 18:29:14 +00001878
1879 qemu_thread_create(cpu->thread, thread_name, qemu_tcg_cpu_thread_fn,
1880 cpu, QEMU_THREAD_JOINABLE);
1881
1882 } else {
1883 /* share a single thread for all cpus with TCG */
1884 snprintf(thread_name, VCPU_THREAD_NAME_SIZE, "ALL CPUs/TCG");
1885 qemu_thread_create(cpu->thread, thread_name,
1886 qemu_tcg_rr_cpu_thread_fn,
1887 cpu, QEMU_THREAD_JOINABLE);
1888
1889 single_tcg_halt_cond = cpu->halt_cond;
1890 single_tcg_cpu_thread = cpu->thread;
1891 }
Paolo Bonzini1ecf47b2011-12-13 13:43:52 +01001892#ifdef _WIN32
Andreas Färber814e6122012-05-02 17:00:37 +02001893 cpu->hThread = qemu_thread_get_handle(cpu->thread);
Paolo Bonzini1ecf47b2011-12-13 13:43:52 +01001894#endif
Blue Swirl296af7c2010-03-29 19:23:50 +00001895 } else {
Alex Bennée37257942017-02-23 18:29:14 +00001896 /* For non-MTTCG cases we share the thread */
1897 cpu->thread = single_tcg_cpu_thread;
1898 cpu->halt_cond = single_tcg_halt_cond;
David Hildenbranda3421732018-02-09 20:52:37 +01001899 cpu->thread_id = first_cpu->thread_id;
1900 cpu->can_do_io = 1;
1901 cpu->created = true;
Blue Swirl296af7c2010-03-29 19:23:50 +00001902 }
1903}
1904
Vincent Palatinb0cb0a62017-01-10 11:59:57 +01001905static void qemu_hax_start_vcpu(CPUState *cpu)
1906{
1907 char thread_name[VCPU_THREAD_NAME_SIZE];
1908
1909 cpu->thread = g_malloc0(sizeof(QemuThread));
1910 cpu->halt_cond = g_malloc0(sizeof(QemuCond));
1911 qemu_cond_init(cpu->halt_cond);
1912
1913 snprintf(thread_name, VCPU_THREAD_NAME_SIZE, "CPU %d/HAX",
1914 cpu->cpu_index);
1915 qemu_thread_create(cpu->thread, thread_name, qemu_hax_cpu_thread_fn,
1916 cpu, QEMU_THREAD_JOINABLE);
1917#ifdef _WIN32
1918 cpu->hThread = qemu_thread_get_handle(cpu->thread);
1919#endif
Vincent Palatinb0cb0a62017-01-10 11:59:57 +01001920}
1921
Andreas Färber48a106b2013-05-27 02:20:39 +02001922static void qemu_kvm_start_vcpu(CPUState *cpu)
Blue Swirl296af7c2010-03-29 19:23:50 +00001923{
Dr. David Alan Gilbert49001162014-01-30 10:20:32 +00001924 char thread_name[VCPU_THREAD_NAME_SIZE];
1925
Andreas Färber814e6122012-05-02 17:00:37 +02001926 cpu->thread = g_malloc0(sizeof(QemuThread));
Andreas Färberf5c121b2012-05-03 01:22:49 +02001927 cpu->halt_cond = g_malloc0(sizeof(QemuCond));
1928 qemu_cond_init(cpu->halt_cond);
Dr. David Alan Gilbert49001162014-01-30 10:20:32 +00001929 snprintf(thread_name, VCPU_THREAD_NAME_SIZE, "CPU %d/KVM",
1930 cpu->cpu_index);
1931 qemu_thread_create(cpu->thread, thread_name, qemu_kvm_cpu_thread_fn,
1932 cpu, QEMU_THREAD_JOINABLE);
Blue Swirl296af7c2010-03-29 19:23:50 +00001933}
1934
Sergio Andres Gomez Del Realc97d6d22017-09-13 04:05:09 -05001935static void qemu_hvf_start_vcpu(CPUState *cpu)
1936{
1937 char thread_name[VCPU_THREAD_NAME_SIZE];
1938
1939 /* HVF currently does not support TCG, and only runs in
1940 * unrestricted-guest mode. */
1941 assert(hvf_enabled());
1942
1943 cpu->thread = g_malloc0(sizeof(QemuThread));
1944 cpu->halt_cond = g_malloc0(sizeof(QemuCond));
1945 qemu_cond_init(cpu->halt_cond);
1946
1947 snprintf(thread_name, VCPU_THREAD_NAME_SIZE, "CPU %d/HVF",
1948 cpu->cpu_index);
1949 qemu_thread_create(cpu->thread, thread_name, qemu_hvf_cpu_thread_fn,
1950 cpu, QEMU_THREAD_JOINABLE);
Sergio Andres Gomez Del Realc97d6d22017-09-13 04:05:09 -05001951}
1952
Justin Terry (VM)19306802018-01-22 13:07:49 -08001953static void qemu_whpx_start_vcpu(CPUState *cpu)
1954{
1955 char thread_name[VCPU_THREAD_NAME_SIZE];
1956
1957 cpu->thread = g_malloc0(sizeof(QemuThread));
1958 cpu->halt_cond = g_malloc0(sizeof(QemuCond));
1959 qemu_cond_init(cpu->halt_cond);
1960 snprintf(thread_name, VCPU_THREAD_NAME_SIZE, "CPU %d/WHPX",
1961 cpu->cpu_index);
1962 qemu_thread_create(cpu->thread, thread_name, qemu_whpx_cpu_thread_fn,
1963 cpu, QEMU_THREAD_JOINABLE);
1964#ifdef _WIN32
1965 cpu->hThread = qemu_thread_get_handle(cpu->thread);
1966#endif
Justin Terry (VM)19306802018-01-22 13:07:49 -08001967}
1968
Andreas Färber10a90212013-05-27 02:24:35 +02001969static void qemu_dummy_start_vcpu(CPUState *cpu)
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +02001970{
Dr. David Alan Gilbert49001162014-01-30 10:20:32 +00001971 char thread_name[VCPU_THREAD_NAME_SIZE];
1972
Andreas Färber814e6122012-05-02 17:00:37 +02001973 cpu->thread = g_malloc0(sizeof(QemuThread));
Andreas Färberf5c121b2012-05-03 01:22:49 +02001974 cpu->halt_cond = g_malloc0(sizeof(QemuCond));
1975 qemu_cond_init(cpu->halt_cond);
Dr. David Alan Gilbert49001162014-01-30 10:20:32 +00001976 snprintf(thread_name, VCPU_THREAD_NAME_SIZE, "CPU %d/DUMMY",
1977 cpu->cpu_index);
1978 qemu_thread_create(cpu->thread, thread_name, qemu_dummy_cpu_thread_fn, cpu,
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +02001979 QEMU_THREAD_JOINABLE);
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +02001980}
1981
Andreas Färberc643bed2013-05-27 03:23:24 +02001982void qemu_init_vcpu(CPUState *cpu)
Blue Swirl296af7c2010-03-29 19:23:50 +00001983{
Andreas Färberce3960e2012-12-17 03:27:07 +01001984 cpu->nr_cores = smp_cores;
1985 cpu->nr_threads = smp_threads;
Andreas Färberf324e762012-05-02 23:26:21 +02001986 cpu->stopped = true;
Peter Maydell56943e82016-01-21 14:15:04 +00001987
1988 if (!cpu->as) {
1989 /* If the target cpu hasn't set up any address spaces itself,
1990 * give it the default one.
1991 */
Peter Maydell12ebc9a2016-01-21 14:15:04 +00001992 cpu->num_ases = 1;
Peter Xu80ceb072017-11-23 17:23:32 +08001993 cpu_address_space_init(cpu, 0, "cpu-memory", cpu->memory);
Peter Maydell56943e82016-01-21 14:15:04 +00001994 }
1995
Jan Kiszka0ab07c62011-02-07 12:19:14 +01001996 if (kvm_enabled()) {
Andreas Färber48a106b2013-05-27 02:20:39 +02001997 qemu_kvm_start_vcpu(cpu);
Vincent Palatinb0cb0a62017-01-10 11:59:57 +01001998 } else if (hax_enabled()) {
1999 qemu_hax_start_vcpu(cpu);
Sergio Andres Gomez Del Realc97d6d22017-09-13 04:05:09 -05002000 } else if (hvf_enabled()) {
2001 qemu_hvf_start_vcpu(cpu);
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +02002002 } else if (tcg_enabled()) {
Andreas Färbere5ab30a2012-05-03 01:50:44 +02002003 qemu_tcg_init_vcpu(cpu);
Justin Terry (VM)19306802018-01-22 13:07:49 -08002004 } else if (whpx_enabled()) {
2005 qemu_whpx_start_vcpu(cpu);
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +02002006 } else {
Andreas Färber10a90212013-05-27 02:24:35 +02002007 qemu_dummy_start_vcpu(cpu);
Jan Kiszka0ab07c62011-02-07 12:19:14 +01002008 }
David Hildenbrand81e96312018-02-09 20:52:38 +01002009
2010 while (!cpu->created) {
2011 qemu_cond_wait(&qemu_cpu_cond, &qemu_global_mutex);
2012 }
Blue Swirl296af7c2010-03-29 19:23:50 +00002013}
2014
Jan Kiszkab4a3d962011-02-01 22:15:43 +01002015void cpu_stop_current(void)
Blue Swirl296af7c2010-03-29 19:23:50 +00002016{
Andreas Färber4917cf42013-05-27 05:17:50 +02002017 if (current_cpu) {
David Hildenbrandebd05fe2017-11-29 20:12:15 +01002018 qemu_cpu_stop(current_cpu, true);
Jan Kiszkab4a3d962011-02-01 22:15:43 +01002019 }
Blue Swirl296af7c2010-03-29 19:23:50 +00002020}
2021
Kevin Wolf56983462013-07-05 13:49:54 +02002022int vm_stop(RunState state)
Blue Swirl296af7c2010-03-29 19:23:50 +00002023{
Juan Quintelaaa723c22012-09-18 16:30:11 +02002024 if (qemu_in_vcpu_thread()) {
Paolo Bonzini74892d22014-06-05 14:53:58 +02002025 qemu_system_vmstop_request_prepare();
Luiz Capitulino1dfb4dd2011-07-29 14:26:33 -03002026 qemu_system_vmstop_request(state);
Blue Swirl296af7c2010-03-29 19:23:50 +00002027 /*
2028 * FIXME: should not return to device code in case
2029 * vm_stop() has been requested.
2030 */
Jan Kiszkab4a3d962011-02-01 22:15:43 +01002031 cpu_stop_current();
Kevin Wolf56983462013-07-05 13:49:54 +02002032 return 0;
Blue Swirl296af7c2010-03-29 19:23:50 +00002033 }
Kevin Wolf56983462013-07-05 13:49:54 +02002034
Stefan Hajnoczi4486e892018-03-07 14:42:05 +00002035 return do_vm_stop(state, true);
Blue Swirl296af7c2010-03-29 19:23:50 +00002036}
2037
Claudio Imbrenda2d76e822017-02-14 18:07:47 +01002038/**
2039 * Prepare for (re)starting the VM.
2040 * Returns -1 if the vCPUs are not to be restarted (e.g. if they are already
2041 * running or in case of an error condition), 0 otherwise.
2042 */
2043int vm_prepare_start(void)
2044{
2045 RunState requested;
Claudio Imbrenda2d76e822017-02-14 18:07:47 +01002046
2047 qemu_vmstop_requested(&requested);
2048 if (runstate_is_running() && requested == RUN_STATE__MAX) {
2049 return -1;
2050 }
2051
2052 /* Ensure that a STOP/RESUME pair of events is emitted if a
2053 * vmstop request was pending. The BLOCK_IO_ERROR event, for
2054 * example, according to documentation is always followed by
2055 * the STOP event.
2056 */
2057 if (runstate_is_running()) {
2058 qapi_event_send_stop(&error_abort);
Markus Armbrusterf0561582018-04-23 10:45:18 +02002059 qapi_event_send_resume(&error_abort);
2060 return -1;
Claudio Imbrenda2d76e822017-02-14 18:07:47 +01002061 }
2062
2063 /* We are sending this now, but the CPUs will be resumed shortly later */
2064 qapi_event_send_resume(&error_abort);
Markus Armbrusterf0561582018-04-23 10:45:18 +02002065
2066 replay_enable_events();
2067 cpu_enable_ticks();
2068 runstate_set(RUN_STATE_RUNNING);
2069 vm_state_notify(1, RUN_STATE_RUNNING);
2070 return 0;
Claudio Imbrenda2d76e822017-02-14 18:07:47 +01002071}
2072
2073void vm_start(void)
2074{
2075 if (!vm_prepare_start()) {
2076 resume_all_vcpus();
2077 }
2078}
2079
Luiz Capitulino8a9236f2011-10-14 11:18:09 -03002080/* does a state transition even if the VM is already stopped,
2081 current state is forgotten forever */
Kevin Wolf56983462013-07-05 13:49:54 +02002082int vm_stop_force_state(RunState state)
Luiz Capitulino8a9236f2011-10-14 11:18:09 -03002083{
2084 if (runstate_is_running()) {
Kevin Wolf56983462013-07-05 13:49:54 +02002085 return vm_stop(state);
Luiz Capitulino8a9236f2011-10-14 11:18:09 -03002086 } else {
2087 runstate_set(state);
Wen Congyangb2780d32015-11-20 17:34:38 +08002088
2089 bdrv_drain_all();
Kevin Wolf594a45c2013-07-18 14:52:19 +02002090 /* Make sure to return an error if the flush in a previous vm_stop()
2091 * failed. */
John Snow22af08e2016-09-22 21:45:51 -04002092 return bdrv_flush_all();
Luiz Capitulino8a9236f2011-10-14 11:18:09 -03002093 }
2094}
2095
Stefan Weil9a78eea2010-10-22 23:03:33 +02002096void list_cpus(FILE *f, fprintf_function cpu_fprintf, const char *optarg)
Blue Swirl262353c2010-05-04 19:55:35 +00002097{
2098 /* XXX: implement xxx_cpu_list for targets that still miss it */
Peter Maydelle916cbf2012-09-05 17:41:08 -03002099#if defined(cpu_list)
2100 cpu_list(f, cpu_fprintf);
Blue Swirl262353c2010-05-04 19:55:35 +00002101#endif
2102}
Luiz Capitulinode0b36b2011-09-21 16:38:35 -03002103
2104CpuInfoList *qmp_query_cpus(Error **errp)
2105{
Igor Mammedovafed5a52017-05-10 13:29:55 +02002106 MachineState *ms = MACHINE(qdev_get_machine());
2107 MachineClass *mc = MACHINE_GET_CLASS(ms);
Luiz Capitulinode0b36b2011-09-21 16:38:35 -03002108 CpuInfoList *head = NULL, *cur_item = NULL;
Andreas Färber182735e2013-05-29 22:29:20 +02002109 CPUState *cpu;
Luiz Capitulinode0b36b2011-09-21 16:38:35 -03002110
Andreas Färberbdc44642013-06-24 23:50:24 +02002111 CPU_FOREACH(cpu) {
Luiz Capitulinode0b36b2011-09-21 16:38:35 -03002112 CpuInfoList *info;
Andreas Färber182735e2013-05-29 22:29:20 +02002113#if defined(TARGET_I386)
2114 X86CPU *x86_cpu = X86_CPU(cpu);
2115 CPUX86State *env = &x86_cpu->env;
2116#elif defined(TARGET_PPC)
2117 PowerPCCPU *ppc_cpu = POWERPC_CPU(cpu);
2118 CPUPPCState *env = &ppc_cpu->env;
2119#elif defined(TARGET_SPARC)
2120 SPARCCPU *sparc_cpu = SPARC_CPU(cpu);
2121 CPUSPARCState *env = &sparc_cpu->env;
Michael Clark25fa1942018-03-03 01:32:59 +13002122#elif defined(TARGET_RISCV)
2123 RISCVCPU *riscv_cpu = RISCV_CPU(cpu);
2124 CPURISCVState *env = &riscv_cpu->env;
Andreas Färber182735e2013-05-29 22:29:20 +02002125#elif defined(TARGET_MIPS)
2126 MIPSCPU *mips_cpu = MIPS_CPU(cpu);
2127 CPUMIPSState *env = &mips_cpu->env;
Bastian Koppelmann48e06fe2014-09-01 12:59:46 +01002128#elif defined(TARGET_TRICORE)
2129 TriCoreCPU *tricore_cpu = TRICORE_CPU(cpu);
2130 CPUTriCoreState *env = &tricore_cpu->env;
Viktor Mihajlovski9d0306d2018-02-16 17:08:37 +01002131#elif defined(TARGET_S390X)
2132 S390CPU *s390_cpu = S390_CPU(cpu);
2133 CPUS390XState *env = &s390_cpu->env;
Andreas Färber182735e2013-05-29 22:29:20 +02002134#endif
Luiz Capitulinode0b36b2011-09-21 16:38:35 -03002135
Andreas Färbercb446ec2013-05-01 14:24:52 +02002136 cpu_synchronize_state(cpu);
Luiz Capitulinode0b36b2011-09-21 16:38:35 -03002137
2138 info = g_malloc0(sizeof(*info));
2139 info->value = g_malloc0(sizeof(*info->value));
Andreas Färber55e5c282012-12-17 06:18:02 +01002140 info->value->CPU = cpu->cpu_index;
Andreas Färber182735e2013-05-29 22:29:20 +02002141 info->value->current = (cpu == first_cpu);
Andreas Färber259186a2013-01-17 18:51:17 +01002142 info->value->halted = cpu->halted;
Eduardo Habkost58f88d42015-05-08 16:04:22 -03002143 info->value->qom_path = object_get_canonical_path(OBJECT(cpu));
Andreas Färber9f09e182012-05-03 06:59:07 +02002144 info->value->thread_id = cpu->thread_id;
Luiz Capitulinode0b36b2011-09-21 16:38:35 -03002145#if defined(TARGET_I386)
Eric Blake86f4b682015-11-18 01:52:59 -07002146 info->value->arch = CPU_INFO_ARCH_X86;
Eric Blake544a3732016-02-17 23:48:27 -07002147 info->value->u.x86.pc = env->eip + env->segs[R_CS].base;
Luiz Capitulinode0b36b2011-09-21 16:38:35 -03002148#elif defined(TARGET_PPC)
Eric Blake86f4b682015-11-18 01:52:59 -07002149 info->value->arch = CPU_INFO_ARCH_PPC;
Eric Blake544a3732016-02-17 23:48:27 -07002150 info->value->u.ppc.nip = env->nip;
Luiz Capitulinode0b36b2011-09-21 16:38:35 -03002151#elif defined(TARGET_SPARC)
Eric Blake86f4b682015-11-18 01:52:59 -07002152 info->value->arch = CPU_INFO_ARCH_SPARC;
Eric Blake544a3732016-02-17 23:48:27 -07002153 info->value->u.q_sparc.pc = env->pc;
2154 info->value->u.q_sparc.npc = env->npc;
Luiz Capitulinode0b36b2011-09-21 16:38:35 -03002155#elif defined(TARGET_MIPS)
Eric Blake86f4b682015-11-18 01:52:59 -07002156 info->value->arch = CPU_INFO_ARCH_MIPS;
Eric Blake544a3732016-02-17 23:48:27 -07002157 info->value->u.q_mips.PC = env->active_tc.PC;
Bastian Koppelmann48e06fe2014-09-01 12:59:46 +01002158#elif defined(TARGET_TRICORE)
Eric Blake86f4b682015-11-18 01:52:59 -07002159 info->value->arch = CPU_INFO_ARCH_TRICORE;
Eric Blake544a3732016-02-17 23:48:27 -07002160 info->value->u.tricore.PC = env->PC;
Viktor Mihajlovski9d0306d2018-02-16 17:08:37 +01002161#elif defined(TARGET_S390X)
2162 info->value->arch = CPU_INFO_ARCH_S390;
2163 info->value->u.s390.cpu_state = env->cpu_state;
Michael Clark25fa1942018-03-03 01:32:59 +13002164#elif defined(TARGET_RISCV)
2165 info->value->arch = CPU_INFO_ARCH_RISCV;
2166 info->value->u.riscv.pc = env->pc;
Eric Blake86f4b682015-11-18 01:52:59 -07002167#else
2168 info->value->arch = CPU_INFO_ARCH_OTHER;
Luiz Capitulinode0b36b2011-09-21 16:38:35 -03002169#endif
Igor Mammedovafed5a52017-05-10 13:29:55 +02002170 info->value->has_props = !!mc->cpu_index_to_instance_props;
2171 if (info->value->has_props) {
2172 CpuInstanceProperties *props;
2173 props = g_malloc0(sizeof(*props));
2174 *props = mc->cpu_index_to_instance_props(ms, cpu->cpu_index);
2175 info->value->props = props;
2176 }
Luiz Capitulinode0b36b2011-09-21 16:38:35 -03002177
2178 /* XXX: waiting for the qapi to support GSList */
2179 if (!cur_item) {
2180 head = cur_item = info;
2181 } else {
2182 cur_item->next = info;
2183 cur_item = info;
2184 }
2185 }
2186
2187 return head;
2188}
Luiz Capitulino0cfd6a92011-11-22 16:32:37 -02002189
Laszlo Ersekdaa9d2b2018-04-27 21:28:51 +02002190static CpuInfoArch sysemu_target_to_cpuinfo_arch(SysEmuTarget target)
2191{
2192 /*
2193 * The @SysEmuTarget -> @CpuInfoArch mapping below is based on the
2194 * TARGET_ARCH -> TARGET_BASE_ARCH mapping in the "configure" script.
2195 */
2196 switch (target) {
2197 case SYS_EMU_TARGET_I386:
2198 case SYS_EMU_TARGET_X86_64:
2199 return CPU_INFO_ARCH_X86;
2200
2201 case SYS_EMU_TARGET_PPC:
2202 case SYS_EMU_TARGET_PPCEMB:
2203 case SYS_EMU_TARGET_PPC64:
2204 return CPU_INFO_ARCH_PPC;
2205
2206 case SYS_EMU_TARGET_SPARC:
2207 case SYS_EMU_TARGET_SPARC64:
2208 return CPU_INFO_ARCH_SPARC;
2209
2210 case SYS_EMU_TARGET_MIPS:
2211 case SYS_EMU_TARGET_MIPSEL:
2212 case SYS_EMU_TARGET_MIPS64:
2213 case SYS_EMU_TARGET_MIPS64EL:
2214 return CPU_INFO_ARCH_MIPS;
2215
2216 case SYS_EMU_TARGET_TRICORE:
2217 return CPU_INFO_ARCH_TRICORE;
2218
2219 case SYS_EMU_TARGET_S390X:
2220 return CPU_INFO_ARCH_S390;
2221
2222 case SYS_EMU_TARGET_RISCV32:
2223 case SYS_EMU_TARGET_RISCV64:
2224 return CPU_INFO_ARCH_RISCV;
2225
2226 default:
2227 return CPU_INFO_ARCH_OTHER;
2228 }
2229}
2230
2231static void cpustate_to_cpuinfo_s390(CpuInfoS390 *info, const CPUState *cpu)
2232{
2233#ifdef TARGET_S390X
2234 S390CPU *s390_cpu = S390_CPU(cpu);
2235 CPUS390XState *env = &s390_cpu->env;
2236
2237 info->cpu_state = env->cpu_state;
2238#else
2239 abort();
2240#endif
2241}
2242
Luiz Capitulinoce74ee32018-02-16 17:08:38 +01002243/*
2244 * fast means: we NEVER interrupt vCPU threads to retrieve
2245 * information from KVM.
2246 */
2247CpuInfoFastList *qmp_query_cpus_fast(Error **errp)
2248{
2249 MachineState *ms = MACHINE(qdev_get_machine());
2250 MachineClass *mc = MACHINE_GET_CLASS(ms);
2251 CpuInfoFastList *head = NULL, *cur_item = NULL;
Laszlo Ersekdaa9d2b2018-04-27 21:28:51 +02002252 SysEmuTarget target = qapi_enum_parse(&SysEmuTarget_lookup, TARGET_NAME,
2253 -1, &error_abort);
Luiz Capitulinoce74ee32018-02-16 17:08:38 +01002254 CPUState *cpu;
2255
2256 CPU_FOREACH(cpu) {
2257 CpuInfoFastList *info = g_malloc0(sizeof(*info));
2258 info->value = g_malloc0(sizeof(*info->value));
2259
2260 info->value->cpu_index = cpu->cpu_index;
2261 info->value->qom_path = object_get_canonical_path(OBJECT(cpu));
2262 info->value->thread_id = cpu->thread_id;
2263
2264 info->value->has_props = !!mc->cpu_index_to_instance_props;
2265 if (info->value->has_props) {
2266 CpuInstanceProperties *props;
2267 props = g_malloc0(sizeof(*props));
2268 *props = mc->cpu_index_to_instance_props(ms, cpu->cpu_index);
2269 info->value->props = props;
2270 }
2271
Laszlo Ersekdaa9d2b2018-04-27 21:28:51 +02002272 info->value->arch = sysemu_target_to_cpuinfo_arch(target);
2273 info->value->target = target;
2274 if (target == SYS_EMU_TARGET_S390X) {
2275 cpustate_to_cpuinfo_s390(&info->value->u.s390x, cpu);
Laszlo Ersekdaa9d2b2018-04-27 21:28:51 +02002276 }
2277
Luiz Capitulinoce74ee32018-02-16 17:08:38 +01002278 if (!cur_item) {
2279 head = cur_item = info;
2280 } else {
2281 cur_item->next = info;
2282 cur_item = info;
2283 }
2284 }
2285
2286 return head;
2287}
2288
Luiz Capitulino0cfd6a92011-11-22 16:32:37 -02002289void qmp_memsave(int64_t addr, int64_t size, const char *filename,
2290 bool has_cpu, int64_t cpu_index, Error **errp)
2291{
2292 FILE *f;
2293 uint32_t l;
Andreas Färber55e5c282012-12-17 06:18:02 +01002294 CPUState *cpu;
Luiz Capitulino0cfd6a92011-11-22 16:32:37 -02002295 uint8_t buf[1024];
Borislav Petkov0dc9daf2015-02-08 13:14:38 +01002296 int64_t orig_addr = addr, orig_size = size;
Luiz Capitulino0cfd6a92011-11-22 16:32:37 -02002297
2298 if (!has_cpu) {
2299 cpu_index = 0;
2300 }
2301
Andreas Färber151d1322013-02-15 15:41:49 +01002302 cpu = qemu_get_cpu(cpu_index);
2303 if (cpu == NULL) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01002304 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "cpu-index",
2305 "a CPU number");
Luiz Capitulino0cfd6a92011-11-22 16:32:37 -02002306 return;
2307 }
2308
2309 f = fopen(filename, "wb");
2310 if (!f) {
Luiz Capitulino618da852013-06-07 14:35:06 -04002311 error_setg_file_open(errp, errno, filename);
Luiz Capitulino0cfd6a92011-11-22 16:32:37 -02002312 return;
2313 }
2314
2315 while (size != 0) {
2316 l = sizeof(buf);
2317 if (l > size)
2318 l = size;
Aneesh Kumar K.V2f4d0f52013-10-01 21:49:30 +05302319 if (cpu_memory_rw_debug(cpu, addr, buf, l, 0) != 0) {
Borislav Petkov0dc9daf2015-02-08 13:14:38 +01002320 error_setg(errp, "Invalid addr 0x%016" PRIx64 "/size %" PRId64
2321 " specified", orig_addr, orig_size);
Aneesh Kumar K.V2f4d0f52013-10-01 21:49:30 +05302322 goto exit;
2323 }
Luiz Capitulino0cfd6a92011-11-22 16:32:37 -02002324 if (fwrite(buf, 1, l, f) != l) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01002325 error_setg(errp, QERR_IO_ERROR);
Luiz Capitulino0cfd6a92011-11-22 16:32:37 -02002326 goto exit;
2327 }
2328 addr += l;
2329 size -= l;
2330 }
2331
2332exit:
2333 fclose(f);
2334}
Luiz Capitulino6d3962b2011-11-22 17:26:46 -02002335
2336void qmp_pmemsave(int64_t addr, int64_t size, const char *filename,
2337 Error **errp)
2338{
2339 FILE *f;
2340 uint32_t l;
2341 uint8_t buf[1024];
2342
2343 f = fopen(filename, "wb");
2344 if (!f) {
Luiz Capitulino618da852013-06-07 14:35:06 -04002345 error_setg_file_open(errp, errno, filename);
Luiz Capitulino6d3962b2011-11-22 17:26:46 -02002346 return;
2347 }
2348
2349 while (size != 0) {
2350 l = sizeof(buf);
2351 if (l > size)
2352 l = size;
Stefan Weileb6282f2014-04-07 20:28:23 +02002353 cpu_physical_memory_read(addr, buf, l);
Luiz Capitulino6d3962b2011-11-22 17:26:46 -02002354 if (fwrite(buf, 1, l, f) != l) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01002355 error_setg(errp, QERR_IO_ERROR);
Luiz Capitulino6d3962b2011-11-22 17:26:46 -02002356 goto exit;
2357 }
2358 addr += l;
2359 size -= l;
2360 }
2361
2362exit:
2363 fclose(f);
2364}
Luiz Capitulinoab49ab52011-11-23 12:55:53 -02002365
2366void qmp_inject_nmi(Error **errp)
2367{
Alexey Kardashevskiy9cb805f2014-08-20 22:16:33 +10002368 nmi_monitor_handle(monitor_get_cpu_index(), errp);
Luiz Capitulinoab49ab52011-11-23 12:55:53 -02002369}
Sebastian Tanase27498be2014-07-25 11:56:33 +02002370
2371void dump_drift_info(FILE *f, fprintf_function cpu_fprintf)
2372{
2373 if (!use_icount) {
2374 return;
2375 }
2376
2377 cpu_fprintf(f, "Host - Guest clock %"PRIi64" ms\n",
2378 (cpu_get_clock() - cpu_get_icount())/SCALE_MS);
2379 if (icount_align_option) {
2380 cpu_fprintf(f, "Max guest delay %"PRIi64" ms\n", -max_delay/SCALE_MS);
2381 cpu_fprintf(f, "Max guest advance %"PRIi64" ms\n", max_advance/SCALE_MS);
2382 } else {
2383 cpu_fprintf(f, "Max guest delay NA\n");
2384 cpu_fprintf(f, "Max guest advance NA\n");
2385 }
2386}