blob: 3783651e6995c295034654d8ad714aac640ac6fb [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/* Arbitrarily pick 1MIPS as the minimum allowable speed. */
125#define MAX_ICOUNT_SHIFT 10
Paolo Bonzinia3270e12013-10-07 17:18:15 +0200126
Paolo Bonzini946fb272011-09-12 13:57:37 +0200127typedef struct TimersState {
Liu Ping Fancb365642013-09-25 14:20:58 +0800128 /* Protected by BQL. */
Paolo Bonzini946fb272011-09-12 13:57:37 +0200129 int64_t cpu_ticks_prev;
130 int64_t cpu_ticks_offset;
Liu Ping Fancb365642013-09-25 14:20:58 +0800131
132 /* cpu_clock_offset can be read out of BQL, so protect it with
133 * this lock.
134 */
135 QemuSeqLock vm_clock_seqlock;
Paolo Bonzini946fb272011-09-12 13:57:37 +0200136 int64_t cpu_clock_offset;
137 int32_t cpu_ticks_enabled;
KONRAD Fredericc96778b2014-08-01 01:37:09 +0200138
Paolo Bonzinic1ff0732018-08-14 09:31:58 +0200139 /* Conversion factor from emulated instructions to virtual clock ticks. */
140 int icount_time_shift;
KONRAD Fredericc96778b2014-08-01 01:37:09 +0200141 /* Compensate for varying guest execution speed. */
142 int64_t qemu_icount_bias;
143 /* Only written by TCG thread */
144 int64_t qemu_icount;
Pavel Dovgalyukb39e3f32018-01-11 11:26:10 +0300145 /* for adjusting icount */
146 int64_t vm_clock_warp_start;
147 QEMUTimer *icount_rt_timer;
148 QEMUTimer *icount_vm_timer;
149 QEMUTimer *icount_warp_timer;
Paolo Bonzini946fb272011-09-12 13:57:37 +0200150} TimersState;
151
Liu Ping Fand9cd4002013-07-21 08:43:00 +0000152static TimersState timers_state;
KONRAD Frederic8d4e9142017-02-23 18:29:08 +0000153bool mttcg_enabled;
154
155/*
156 * We default to false if we know other options have been enabled
157 * which are currently incompatible with MTTCG. Otherwise when each
158 * guest (target) has been updated to support:
159 * - atomic instructions
160 * - memory ordering primitives (barriers)
161 * they can set the appropriate CONFIG flags in ${target}-softmmu.mak
162 *
163 * Once a guest architecture has been converted to the new primitives
164 * there are two remaining limitations to check.
165 *
166 * - The guest can't be oversized (e.g. 64 bit guest on 32 bit host)
167 * - The host must have a stronger memory order than the guest
168 *
169 * It may be possible in future to support strong guests on weak hosts
170 * but that will require tagging all load/stores in a guest with their
171 * implicit memory order requirements which would likely slow things
172 * down a lot.
173 */
174
175static bool check_tcg_memory_orders_compatible(void)
176{
177#if defined(TCG_GUEST_DEFAULT_MO) && defined(TCG_TARGET_DEFAULT_MO)
178 return (TCG_GUEST_DEFAULT_MO & ~TCG_TARGET_DEFAULT_MO) == 0;
179#else
180 return false;
181#endif
182}
183
184static bool default_mttcg_enabled(void)
185{
Alex Bennée83fd9622017-02-27 17:09:01 +0000186 if (use_icount || TCG_OVERSIZED_GUEST) {
KONRAD Frederic8d4e9142017-02-23 18:29:08 +0000187 return false;
188 } else {
189#ifdef TARGET_SUPPORTS_MTTCG
190 return check_tcg_memory_orders_compatible();
191#else
192 return false;
193#endif
194 }
195}
196
197void qemu_tcg_configure(QemuOpts *opts, Error **errp)
198{
199 const char *t = qemu_opt_get(opts, "thread");
200 if (t) {
201 if (strcmp(t, "multi") == 0) {
202 if (TCG_OVERSIZED_GUEST) {
203 error_setg(errp, "No MTTCG when guest word size > hosts");
Alex Bennée83fd9622017-02-27 17:09:01 +0000204 } else if (use_icount) {
205 error_setg(errp, "No MTTCG when icount is enabled");
KONRAD Frederic8d4e9142017-02-23 18:29:08 +0000206 } else {
Nikunj A Dadhania86953502017-04-10 11:36:55 +0530207#ifndef TARGET_SUPPORTS_MTTCG
Alex Bennéec34c7622017-02-28 14:40:17 +0000208 error_report("Guest not yet converted to MTTCG - "
209 "you may get unexpected results");
210#endif
KONRAD Frederic8d4e9142017-02-23 18:29:08 +0000211 if (!check_tcg_memory_orders_compatible()) {
212 error_report("Guest expects a stronger memory ordering "
213 "than the host provides");
Pranith Kumar8cfef892017-03-25 16:19:23 -0400214 error_printf("This may cause strange/hard to debug errors\n");
KONRAD Frederic8d4e9142017-02-23 18:29:08 +0000215 }
216 mttcg_enabled = true;
217 }
218 } else if (strcmp(t, "single") == 0) {
219 mttcg_enabled = false;
220 } else {
221 error_setg(errp, "Invalid 'thread' setting %s", t);
222 }
223 } else {
224 mttcg_enabled = default_mttcg_enabled();
225 }
226}
Paolo Bonzini946fb272011-09-12 13:57:37 +0200227
Alex Bennéee4cd9652017-03-31 16:09:42 +0100228/* The current number of executed instructions is based on what we
229 * originally budgeted minus the current state of the decrementing
230 * icount counters in extra/u16.low.
231 */
232static int64_t cpu_get_icount_executed(CPUState *cpu)
233{
234 return cpu->icount_budget - (cpu->icount_decr.u16.low + cpu->icount_extra);
235}
236
Alex Bennée512d3c82017-04-05 12:32:37 +0100237/*
238 * Update the global shared timer_state.qemu_icount to take into
239 * account executed instructions. This is done by the TCG vCPU
240 * thread so the main-loop can see time has moved forward.
241 */
242void cpu_update_icount(CPUState *cpu)
243{
244 int64_t executed = cpu_get_icount_executed(cpu);
245 cpu->icount_budget -= executed;
246
247#ifdef CONFIG_ATOMIC64
248 atomic_set__nocheck(&timers_state.qemu_icount,
Paolo Bonzinic1ff0732018-08-14 09:31:58 +0200249 timers_state.qemu_icount + executed);
Alex Bennée512d3c82017-04-05 12:32:37 +0100250#else /* FIXME: we need 64bit atomics to do this safely */
251 timers_state.qemu_icount += executed;
252#endif
253}
254
Paolo Bonzinic1ff0732018-08-14 09:31:58 +0200255static int64_t cpu_get_icount_raw_locked(void)
Paolo Bonzini946fb272011-09-12 13:57:37 +0200256{
Andreas Färber4917cf42013-05-27 05:17:50 +0200257 CPUState *cpu = current_cpu;
Paolo Bonzini946fb272011-09-12 13:57:37 +0200258
Alex Bennée243c5f72017-03-30 18:49:22 +0100259 if (cpu && cpu->running) {
Paolo Bonzini414b15c2015-06-24 14:16:26 +0200260 if (!cpu->can_do_io) {
Alistair Francis493d89b2018-02-03 09:43:14 +0100261 error_report("Bad icount read");
Pavel Dovgalyuk2a629142014-12-08 10:53:45 +0300262 exit(1);
Paolo Bonzini946fb272011-09-12 13:57:37 +0200263 }
Alex Bennéee4cd9652017-03-31 16:09:42 +0100264 /* Take into account what has run */
Alex Bennée1d059062017-04-05 10:53:47 +0100265 cpu_update_icount(cpu);
Paolo Bonzini946fb272011-09-12 13:57:37 +0200266 }
Paolo Bonzinic1ff0732018-08-14 09:31:58 +0200267 /* The read is protected by the seqlock, so __nocheck is okay. */
Alex Bennée1d059062017-04-05 10:53:47 +0100268 return atomic_read__nocheck(&timers_state.qemu_icount);
Paolo Bonzinic1ff0732018-08-14 09:31:58 +0200269}
270
271static int64_t cpu_get_icount_locked(void)
272{
273 int64_t icount = cpu_get_icount_raw_locked();
274 return atomic_read__nocheck(&timers_state.qemu_icount_bias) + cpu_icount_to_ns(icount);
275}
276
277int64_t cpu_get_icount_raw(void)
278{
279 int64_t icount;
280 unsigned start;
281
282 do {
283 start = seqlock_read_begin(&timers_state.vm_clock_seqlock);
284 icount = cpu_get_icount_raw_locked();
285 } while (seqlock_read_retry(&timers_state.vm_clock_seqlock, start));
286
287 return icount;
Pavel Dovgalyuk2a629142014-12-08 10:53:45 +0300288}
289
290/* Return the virtual CPU time, based on the instruction counter. */
Paolo Bonzini17a15f12013-10-03 15:17:25 +0200291int64_t cpu_get_icount(void)
292{
293 int64_t icount;
294 unsigned start;
295
296 do {
297 start = seqlock_read_begin(&timers_state.vm_clock_seqlock);
298 icount = cpu_get_icount_locked();
299 } while (seqlock_read_retry(&timers_state.vm_clock_seqlock, start));
300
301 return icount;
302}
303
KONRAD Frederic3f031312014-08-01 01:37:15 +0200304int64_t cpu_icount_to_ns(int64_t icount)
305{
Paolo Bonzinic1ff0732018-08-14 09:31:58 +0200306 return icount << atomic_read(&timers_state.icount_time_shift);
KONRAD Frederic3f031312014-08-01 01:37:15 +0200307}
308
Cao jind90f3cc2016-07-29 19:05:38 +0800309/* return the time elapsed in VM between vm_start and vm_stop. Unless
310 * icount is active, cpu_get_ticks() uses units of the host CPU cycle
311 * counter.
312 *
313 * Caller must hold the BQL
314 */
Paolo Bonzini946fb272011-09-12 13:57:37 +0200315int64_t cpu_get_ticks(void)
316{
Paolo Bonzini5f3e3102013-10-28 17:32:18 +0100317 int64_t ticks;
318
Paolo Bonzini946fb272011-09-12 13:57:37 +0200319 if (use_icount) {
320 return cpu_get_icount();
321 }
Paolo Bonzini5f3e3102013-10-28 17:32:18 +0100322
323 ticks = timers_state.cpu_ticks_offset;
324 if (timers_state.cpu_ticks_enabled) {
Christopher Covington4a7428c2015-09-25 10:42:21 -0400325 ticks += cpu_get_host_ticks();
Paolo Bonzini946fb272011-09-12 13:57:37 +0200326 }
Paolo Bonzini5f3e3102013-10-28 17:32:18 +0100327
328 if (timers_state.cpu_ticks_prev > ticks) {
329 /* Note: non increasing ticks may happen if the host uses
330 software suspend */
331 timers_state.cpu_ticks_offset += timers_state.cpu_ticks_prev - ticks;
332 ticks = timers_state.cpu_ticks_prev;
333 }
334
335 timers_state.cpu_ticks_prev = ticks;
336 return ticks;
Paolo Bonzini946fb272011-09-12 13:57:37 +0200337}
338
Liu Ping Fancb365642013-09-25 14:20:58 +0800339static int64_t cpu_get_clock_locked(void)
340{
Cao jin1d45cea2016-07-29 19:05:37 +0800341 int64_t time;
Liu Ping Fancb365642013-09-25 14:20:58 +0800342
Cao jin1d45cea2016-07-29 19:05:37 +0800343 time = timers_state.cpu_clock_offset;
Paolo Bonzini5f3e3102013-10-28 17:32:18 +0100344 if (timers_state.cpu_ticks_enabled) {
Cao jin1d45cea2016-07-29 19:05:37 +0800345 time += get_clock();
Liu Ping Fancb365642013-09-25 14:20:58 +0800346 }
347
Cao jin1d45cea2016-07-29 19:05:37 +0800348 return time;
Liu Ping Fancb365642013-09-25 14:20:58 +0800349}
350
Cao jind90f3cc2016-07-29 19:05:38 +0800351/* Return the monotonic time elapsed in VM, i.e.,
Peter Maydell8212ff82016-09-15 10:24:22 +0100352 * the time between vm_start and vm_stop
353 */
Paolo Bonzini946fb272011-09-12 13:57:37 +0200354int64_t cpu_get_clock(void)
355{
356 int64_t ti;
Liu Ping Fancb365642013-09-25 14:20:58 +0800357 unsigned start;
358
359 do {
360 start = seqlock_read_begin(&timers_state.vm_clock_seqlock);
361 ti = cpu_get_clock_locked();
362 } while (seqlock_read_retry(&timers_state.vm_clock_seqlock, start));
363
364 return ti;
Paolo Bonzini946fb272011-09-12 13:57:37 +0200365}
366
Liu Ping Fancb365642013-09-25 14:20:58 +0800367/* enable cpu_get_ticks()
Cao jin3224e872016-07-08 18:31:37 +0800368 * Caller must hold BQL which serves as mutex for vm_clock_seqlock.
Liu Ping Fancb365642013-09-25 14:20:58 +0800369 */
Paolo Bonzini946fb272011-09-12 13:57:37 +0200370void cpu_enable_ticks(void)
371{
Liu Ping Fancb365642013-09-25 14:20:58 +0800372 /* Here, the really thing protected by seqlock is cpu_clock_offset. */
Emilio G. Cota03719e42016-06-08 14:55:21 -0400373 seqlock_write_begin(&timers_state.vm_clock_seqlock);
Paolo Bonzini946fb272011-09-12 13:57:37 +0200374 if (!timers_state.cpu_ticks_enabled) {
Christopher Covington4a7428c2015-09-25 10:42:21 -0400375 timers_state.cpu_ticks_offset -= cpu_get_host_ticks();
Paolo Bonzini946fb272011-09-12 13:57:37 +0200376 timers_state.cpu_clock_offset -= get_clock();
377 timers_state.cpu_ticks_enabled = 1;
378 }
Emilio G. Cota03719e42016-06-08 14:55:21 -0400379 seqlock_write_end(&timers_state.vm_clock_seqlock);
Paolo Bonzini946fb272011-09-12 13:57:37 +0200380}
381
382/* disable cpu_get_ticks() : the clock is stopped. You must not call
Liu Ping Fancb365642013-09-25 14:20:58 +0800383 * cpu_get_ticks() after that.
Cao jin3224e872016-07-08 18:31:37 +0800384 * Caller must hold BQL which serves as mutex for vm_clock_seqlock.
Liu Ping Fancb365642013-09-25 14:20:58 +0800385 */
Paolo Bonzini946fb272011-09-12 13:57:37 +0200386void cpu_disable_ticks(void)
387{
Liu Ping Fancb365642013-09-25 14:20:58 +0800388 /* Here, the really thing protected by seqlock is cpu_clock_offset. */
Emilio G. Cota03719e42016-06-08 14:55:21 -0400389 seqlock_write_begin(&timers_state.vm_clock_seqlock);
Paolo Bonzini946fb272011-09-12 13:57:37 +0200390 if (timers_state.cpu_ticks_enabled) {
Christopher Covington4a7428c2015-09-25 10:42:21 -0400391 timers_state.cpu_ticks_offset += cpu_get_host_ticks();
Liu Ping Fancb365642013-09-25 14:20:58 +0800392 timers_state.cpu_clock_offset = cpu_get_clock_locked();
Paolo Bonzini946fb272011-09-12 13:57:37 +0200393 timers_state.cpu_ticks_enabled = 0;
394 }
Emilio G. Cota03719e42016-06-08 14:55:21 -0400395 seqlock_write_end(&timers_state.vm_clock_seqlock);
Paolo Bonzini946fb272011-09-12 13:57:37 +0200396}
397
398/* Correlation between real and virtual time is always going to be
399 fairly approximate, so ignore small variation.
400 When the guest is idle real and virtual time will be aligned in
401 the IO wait loop. */
Rutuja Shah73bcb242016-03-21 21:32:30 +0530402#define ICOUNT_WOBBLE (NANOSECONDS_PER_SECOND / 10)
Paolo Bonzini946fb272011-09-12 13:57:37 +0200403
404static void icount_adjust(void)
405{
406 int64_t cur_time;
407 int64_t cur_icount;
408 int64_t delta;
Paolo Bonzinia3270e12013-10-07 17:18:15 +0200409
410 /* Protected by TimersState mutex. */
Paolo Bonzini946fb272011-09-12 13:57:37 +0200411 static int64_t last_delta;
Paolo Bonzini468cc7c2013-10-07 17:21:51 +0200412
Paolo Bonzini946fb272011-09-12 13:57:37 +0200413 /* If the VM is not running, then do nothing. */
414 if (!runstate_is_running()) {
415 return;
416 }
Paolo Bonzini468cc7c2013-10-07 17:21:51 +0200417
Emilio G. Cota03719e42016-06-08 14:55:21 -0400418 seqlock_write_begin(&timers_state.vm_clock_seqlock);
Paolo Bonzini17a15f12013-10-03 15:17:25 +0200419 cur_time = cpu_get_clock_locked();
420 cur_icount = cpu_get_icount_locked();
Paolo Bonzini468cc7c2013-10-07 17:21:51 +0200421
Paolo Bonzini946fb272011-09-12 13:57:37 +0200422 delta = cur_icount - cur_time;
423 /* FIXME: This is a very crude algorithm, somewhat prone to oscillation. */
424 if (delta > 0
425 && last_delta + ICOUNT_WOBBLE < delta * 2
Paolo Bonzinic1ff0732018-08-14 09:31:58 +0200426 && timers_state.icount_time_shift > 0) {
Paolo Bonzini946fb272011-09-12 13:57:37 +0200427 /* The guest is getting too far ahead. Slow time down. */
Paolo Bonzinic1ff0732018-08-14 09:31:58 +0200428 atomic_set(&timers_state.icount_time_shift,
429 timers_state.icount_time_shift - 1);
Paolo Bonzini946fb272011-09-12 13:57:37 +0200430 }
431 if (delta < 0
432 && last_delta - ICOUNT_WOBBLE > delta * 2
Paolo Bonzinic1ff0732018-08-14 09:31:58 +0200433 && timers_state.icount_time_shift < MAX_ICOUNT_SHIFT) {
Paolo Bonzini946fb272011-09-12 13:57:37 +0200434 /* The guest is getting too far behind. Speed time up. */
Paolo Bonzinic1ff0732018-08-14 09:31:58 +0200435 atomic_set(&timers_state.icount_time_shift,
436 timers_state.icount_time_shift + 1);
Paolo Bonzini946fb272011-09-12 13:57:37 +0200437 }
438 last_delta = delta;
Paolo Bonzinic1ff0732018-08-14 09:31:58 +0200439 atomic_set__nocheck(&timers_state.qemu_icount_bias,
440 cur_icount - (timers_state.qemu_icount
441 << timers_state.icount_time_shift));
Emilio G. Cota03719e42016-06-08 14:55:21 -0400442 seqlock_write_end(&timers_state.vm_clock_seqlock);
Paolo Bonzini946fb272011-09-12 13:57:37 +0200443}
444
445static void icount_adjust_rt(void *opaque)
446{
Pavel Dovgalyukb39e3f32018-01-11 11:26:10 +0300447 timer_mod(timers_state.icount_rt_timer,
Pavel Dovgalyuk1979b902015-01-12 15:00:43 +0300448 qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL_RT) + 1000);
Paolo Bonzini946fb272011-09-12 13:57:37 +0200449 icount_adjust();
450}
451
452static void icount_adjust_vm(void *opaque)
453{
Pavel Dovgalyukb39e3f32018-01-11 11:26:10 +0300454 timer_mod(timers_state.icount_vm_timer,
Alex Bligh40daca52013-08-21 16:03:02 +0100455 qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) +
Rutuja Shah73bcb242016-03-21 21:32:30 +0530456 NANOSECONDS_PER_SECOND / 10);
Paolo Bonzini946fb272011-09-12 13:57:37 +0200457 icount_adjust();
458}
459
460static int64_t qemu_icount_round(int64_t count)
461{
Paolo Bonzinic1ff0732018-08-14 09:31:58 +0200462 int shift = atomic_read(&timers_state.icount_time_shift);
463 return (count + (1 << shift) - 1) >> shift;
Paolo Bonzini946fb272011-09-12 13:57:37 +0200464}
465
Pavel Dovgalyukefab87c2015-09-17 19:24:39 +0300466static void icount_warp_rt(void)
Paolo Bonzini946fb272011-09-12 13:57:37 +0200467{
Alex Bennéeccffff42016-04-04 15:35:48 +0100468 unsigned seq;
469 int64_t warp_start;
470
Paolo Bonzini17a15f12013-10-03 15:17:25 +0200471 /* The icount_warp_timer is rescheduled soon after vm_clock_warp_start
472 * changes from -1 to another value, so the race here is okay.
473 */
Alex Bennéeccffff42016-04-04 15:35:48 +0100474 do {
475 seq = seqlock_read_begin(&timers_state.vm_clock_seqlock);
Pavel Dovgalyukb39e3f32018-01-11 11:26:10 +0300476 warp_start = timers_state.vm_clock_warp_start;
Alex Bennéeccffff42016-04-04 15:35:48 +0100477 } while (seqlock_read_retry(&timers_state.vm_clock_seqlock, seq));
478
479 if (warp_start == -1) {
Paolo Bonzini946fb272011-09-12 13:57:37 +0200480 return;
481 }
482
Emilio G. Cota03719e42016-06-08 14:55:21 -0400483 seqlock_write_begin(&timers_state.vm_clock_seqlock);
Paolo Bonzini946fb272011-09-12 13:57:37 +0200484 if (runstate_is_running()) {
Pavel Dovgalyuk8eda2062015-09-17 19:24:28 +0300485 int64_t clock = REPLAY_CLOCK(REPLAY_CLOCK_VIRTUAL_RT,
486 cpu_get_clock_locked());
Paolo Bonzini8ed961d2013-10-07 17:26:07 +0200487 int64_t warp_delta;
488
Pavel Dovgalyukb39e3f32018-01-11 11:26:10 +0300489 warp_delta = clock - timers_state.vm_clock_warp_start;
Paolo Bonzini8ed961d2013-10-07 17:26:07 +0200490 if (use_icount == 2) {
Paolo Bonzini946fb272011-09-12 13:57:37 +0200491 /*
Alex Bligh40daca52013-08-21 16:03:02 +0100492 * In adaptive mode, do not let QEMU_CLOCK_VIRTUAL run too
Paolo Bonzini946fb272011-09-12 13:57:37 +0200493 * far ahead of real time.
494 */
Paolo Bonzini17a15f12013-10-03 15:17:25 +0200495 int64_t cur_icount = cpu_get_icount_locked();
Pavel Dovgalyukbf2a7dd2014-11-26 13:40:55 +0300496 int64_t delta = clock - cur_icount;
Paolo Bonzini8ed961d2013-10-07 17:26:07 +0200497 warp_delta = MIN(warp_delta, delta);
Paolo Bonzini946fb272011-09-12 13:57:37 +0200498 }
Paolo Bonzinic1ff0732018-08-14 09:31:58 +0200499 atomic_set__nocheck(&timers_state.qemu_icount_bias,
500 timers_state.qemu_icount_bias + warp_delta);
Paolo Bonzini946fb272011-09-12 13:57:37 +0200501 }
Pavel Dovgalyukb39e3f32018-01-11 11:26:10 +0300502 timers_state.vm_clock_warp_start = -1;
Emilio G. Cota03719e42016-06-08 14:55:21 -0400503 seqlock_write_end(&timers_state.vm_clock_seqlock);
Paolo Bonzini8ed961d2013-10-07 17:26:07 +0200504
505 if (qemu_clock_expired(QEMU_CLOCK_VIRTUAL)) {
506 qemu_clock_notify(QEMU_CLOCK_VIRTUAL);
507 }
Paolo Bonzini946fb272011-09-12 13:57:37 +0200508}
509
Pavel Dovgalyuke76d1792016-03-10 14:56:09 +0300510static void icount_timer_cb(void *opaque)
Pavel Dovgalyukefab87c2015-09-17 19:24:39 +0300511{
Pavel Dovgalyuke76d1792016-03-10 14:56:09 +0300512 /* No need for a checkpoint because the timer already synchronizes
513 * with CHECKPOINT_CLOCK_VIRTUAL_RT.
514 */
515 icount_warp_rt();
Pavel Dovgalyukefab87c2015-09-17 19:24:39 +0300516}
517
Paolo Bonzini8156be52012-03-28 15:42:04 +0200518void qtest_clock_warp(int64_t dest)
519{
Alex Bligh40daca52013-08-21 16:03:02 +0100520 int64_t clock = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
Fam Zhengefef88b2015-01-19 17:51:43 +0800521 AioContext *aio_context;
Paolo Bonzini8156be52012-03-28 15:42:04 +0200522 assert(qtest_enabled());
Fam Zhengefef88b2015-01-19 17:51:43 +0800523 aio_context = qemu_get_aio_context();
Paolo Bonzini8156be52012-03-28 15:42:04 +0200524 while (clock < dest) {
Alex Bligh40daca52013-08-21 16:03:02 +0100525 int64_t deadline = qemu_clock_deadline_ns_all(QEMU_CLOCK_VIRTUAL);
Sergey Fedorovc9299e22014-06-10 13:10:28 +0400526 int64_t warp = qemu_soonest_timeout(dest - clock, deadline);
Fam Zhengefef88b2015-01-19 17:51:43 +0800527
Emilio G. Cota03719e42016-06-08 14:55:21 -0400528 seqlock_write_begin(&timers_state.vm_clock_seqlock);
Paolo Bonzinic1ff0732018-08-14 09:31:58 +0200529 atomic_set__nocheck(&timers_state.qemu_icount_bias,
530 timers_state.qemu_icount_bias + warp);
Emilio G. Cota03719e42016-06-08 14:55:21 -0400531 seqlock_write_end(&timers_state.vm_clock_seqlock);
Paolo Bonzini17a15f12013-10-03 15:17:25 +0200532
Alex Bligh40daca52013-08-21 16:03:02 +0100533 qemu_clock_run_timers(QEMU_CLOCK_VIRTUAL);
Fam Zhengefef88b2015-01-19 17:51:43 +0800534 timerlist_run_timers(aio_context->tlg.tl[QEMU_CLOCK_VIRTUAL]);
Alex Bligh40daca52013-08-21 16:03:02 +0100535 clock = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
Paolo Bonzini8156be52012-03-28 15:42:04 +0200536 }
Alex Bligh40daca52013-08-21 16:03:02 +0100537 qemu_clock_notify(QEMU_CLOCK_VIRTUAL);
Paolo Bonzini8156be52012-03-28 15:42:04 +0200538}
539
Pavel Dovgalyuke76d1792016-03-10 14:56:09 +0300540void qemu_start_warp_timer(void)
Paolo Bonzini946fb272011-09-12 13:57:37 +0200541{
Paolo Bonzinice78d182013-10-07 17:30:02 +0200542 int64_t clock;
Paolo Bonzini946fb272011-09-12 13:57:37 +0200543 int64_t deadline;
544
Pavel Dovgalyuke76d1792016-03-10 14:56:09 +0300545 if (!use_icount) {
Paolo Bonzini946fb272011-09-12 13:57:37 +0200546 return;
547 }
548
Pavel Dovgalyuk8bd7f712015-09-17 19:24:44 +0300549 /* Nothing to do if the VM is stopped: QEMU_CLOCK_VIRTUAL timers
550 * do not fire, so computing the deadline does not make sense.
551 */
552 if (!runstate_is_running()) {
553 return;
554 }
555
556 /* warp clock deterministically in record/replay mode */
Pavel Dovgalyuke76d1792016-03-10 14:56:09 +0300557 if (!replay_checkpoint(CHECKPOINT_CLOCK_WARP_START)) {
Pavel Dovgalyuk8bd7f712015-09-17 19:24:44 +0300558 return;
559 }
560
Paolo Bonzinice78d182013-10-07 17:30:02 +0200561 if (!all_cpu_threads_idle()) {
Paolo Bonzini946fb272011-09-12 13:57:37 +0200562 return;
563 }
564
Paolo Bonzini8156be52012-03-28 15:42:04 +0200565 if (qtest_enabled()) {
566 /* When testing, qtest commands advance icount. */
Pavel Dovgalyuke76d1792016-03-10 14:56:09 +0300567 return;
Paolo Bonzini8156be52012-03-28 15:42:04 +0200568 }
569
Alex Blighac70aaf2013-08-21 16:02:57 +0100570 /* We want to use the earliest deadline from ALL vm_clocks */
Pavel Dovgalyukbf2a7dd2014-11-26 13:40:55 +0300571 clock = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL_RT);
Alex Bligh40daca52013-08-21 16:03:02 +0100572 deadline = qemu_clock_deadline_ns_all(QEMU_CLOCK_VIRTUAL);
Paolo Bonzinice78d182013-10-07 17:30:02 +0200573 if (deadline < 0) {
Victor CLEMENTd7a0f712015-05-29 17:14:06 +0200574 static bool notified;
575 if (!icount_sleep && !notified) {
Alistair Francis3dc6f862017-07-12 06:57:41 -0700576 warn_report("icount sleep disabled and no active timers");
Victor CLEMENTd7a0f712015-05-29 17:14:06 +0200577 notified = true;
578 }
Paolo Bonzinice78d182013-10-07 17:30:02 +0200579 return;
Alex Blighac70aaf2013-08-21 16:02:57 +0100580 }
581
Paolo Bonzini946fb272011-09-12 13:57:37 +0200582 if (deadline > 0) {
583 /*
Alex Bligh40daca52013-08-21 16:03:02 +0100584 * Ensure QEMU_CLOCK_VIRTUAL proceeds even when the virtual CPU goes to
Paolo Bonzini946fb272011-09-12 13:57:37 +0200585 * sleep. Otherwise, the CPU might be waiting for a future timer
586 * interrupt to wake it up, but the interrupt never comes because
587 * the vCPU isn't running any insns and thus doesn't advance the
Alex Bligh40daca52013-08-21 16:03:02 +0100588 * QEMU_CLOCK_VIRTUAL.
Paolo Bonzini946fb272011-09-12 13:57:37 +0200589 */
Victor CLEMENT5045e9d92015-05-29 17:14:04 +0200590 if (!icount_sleep) {
591 /*
592 * We never let VCPUs sleep in no sleep icount mode.
593 * If there is a pending QEMU_CLOCK_VIRTUAL timer we just advance
594 * to the next QEMU_CLOCK_VIRTUAL event and notify it.
595 * It is useful when we want a deterministic execution time,
596 * isolated from host latencies.
597 */
Emilio G. Cota03719e42016-06-08 14:55:21 -0400598 seqlock_write_begin(&timers_state.vm_clock_seqlock);
Paolo Bonzinic1ff0732018-08-14 09:31:58 +0200599 atomic_set__nocheck(&timers_state.qemu_icount_bias,
600 timers_state.qemu_icount_bias + deadline);
Emilio G. Cota03719e42016-06-08 14:55:21 -0400601 seqlock_write_end(&timers_state.vm_clock_seqlock);
Victor CLEMENT5045e9d92015-05-29 17:14:04 +0200602 qemu_clock_notify(QEMU_CLOCK_VIRTUAL);
603 } else {
604 /*
605 * We do stop VCPUs and only advance QEMU_CLOCK_VIRTUAL after some
606 * "real" time, (related to the time left until the next event) has
607 * passed. The QEMU_CLOCK_VIRTUAL_RT clock will do this.
608 * This avoids that the warps are visible externally; for example,
609 * you will not be sending network packets continuously instead of
610 * every 100ms.
611 */
Emilio G. Cota03719e42016-06-08 14:55:21 -0400612 seqlock_write_begin(&timers_state.vm_clock_seqlock);
Pavel Dovgalyukb39e3f32018-01-11 11:26:10 +0300613 if (timers_state.vm_clock_warp_start == -1
614 || timers_state.vm_clock_warp_start > clock) {
615 timers_state.vm_clock_warp_start = clock;
Victor CLEMENT5045e9d92015-05-29 17:14:04 +0200616 }
Emilio G. Cota03719e42016-06-08 14:55:21 -0400617 seqlock_write_end(&timers_state.vm_clock_seqlock);
Pavel Dovgalyukb39e3f32018-01-11 11:26:10 +0300618 timer_mod_anticipate(timers_state.icount_warp_timer,
619 clock + deadline);
Paolo Bonzinice78d182013-10-07 17:30:02 +0200620 }
Alex Blighac70aaf2013-08-21 16:02:57 +0100621 } else if (deadline == 0) {
Alex Bligh40daca52013-08-21 16:03:02 +0100622 qemu_clock_notify(QEMU_CLOCK_VIRTUAL);
Paolo Bonzini946fb272011-09-12 13:57:37 +0200623 }
624}
625
Pavel Dovgalyuke76d1792016-03-10 14:56:09 +0300626static void qemu_account_warp_timer(void)
627{
628 if (!use_icount || !icount_sleep) {
629 return;
630 }
631
632 /* Nothing to do if the VM is stopped: QEMU_CLOCK_VIRTUAL timers
633 * do not fire, so computing the deadline does not make sense.
634 */
635 if (!runstate_is_running()) {
636 return;
637 }
638
639 /* warp clock deterministically in record/replay mode */
640 if (!replay_checkpoint(CHECKPOINT_CLOCK_WARP_ACCOUNT)) {
641 return;
642 }
643
Pavel Dovgalyukb39e3f32018-01-11 11:26:10 +0300644 timer_del(timers_state.icount_warp_timer);
Pavel Dovgalyuke76d1792016-03-10 14:56:09 +0300645 icount_warp_rt();
646}
647
KONRAD Fredericd09eae32014-08-01 01:37:10 +0200648static bool icount_state_needed(void *opaque)
649{
650 return use_icount;
651}
652
Pavel Dovgalyukb39e3f32018-01-11 11:26:10 +0300653static bool warp_timer_state_needed(void *opaque)
654{
655 TimersState *s = opaque;
656 return s->icount_warp_timer != NULL;
657}
658
659static bool adjust_timers_state_needed(void *opaque)
660{
661 TimersState *s = opaque;
662 return s->icount_rt_timer != NULL;
663}
664
665/*
666 * Subsection for warp timer migration is optional, because may not be created
667 */
668static const VMStateDescription icount_vmstate_warp_timer = {
669 .name = "timer/icount/warp_timer",
670 .version_id = 1,
671 .minimum_version_id = 1,
672 .needed = warp_timer_state_needed,
673 .fields = (VMStateField[]) {
674 VMSTATE_INT64(vm_clock_warp_start, TimersState),
675 VMSTATE_TIMER_PTR(icount_warp_timer, TimersState),
676 VMSTATE_END_OF_LIST()
677 }
678};
679
680static const VMStateDescription icount_vmstate_adjust_timers = {
681 .name = "timer/icount/timers",
682 .version_id = 1,
683 .minimum_version_id = 1,
684 .needed = adjust_timers_state_needed,
685 .fields = (VMStateField[]) {
686 VMSTATE_TIMER_PTR(icount_rt_timer, TimersState),
687 VMSTATE_TIMER_PTR(icount_vm_timer, TimersState),
688 VMSTATE_END_OF_LIST()
689 }
690};
691
KONRAD Fredericd09eae32014-08-01 01:37:10 +0200692/*
693 * This is a subsection for icount migration.
694 */
695static const VMStateDescription icount_vmstate_timers = {
696 .name = "timer/icount",
697 .version_id = 1,
698 .minimum_version_id = 1,
Juan Quintela5cd8cad2014-09-23 14:09:54 +0200699 .needed = icount_state_needed,
KONRAD Fredericd09eae32014-08-01 01:37:10 +0200700 .fields = (VMStateField[]) {
701 VMSTATE_INT64(qemu_icount_bias, TimersState),
702 VMSTATE_INT64(qemu_icount, TimersState),
703 VMSTATE_END_OF_LIST()
Pavel Dovgalyukb39e3f32018-01-11 11:26:10 +0300704 },
705 .subsections = (const VMStateDescription*[]) {
706 &icount_vmstate_warp_timer,
707 &icount_vmstate_adjust_timers,
708 NULL
KONRAD Fredericd09eae32014-08-01 01:37:10 +0200709 }
710};
711
Paolo Bonzini946fb272011-09-12 13:57:37 +0200712static const VMStateDescription vmstate_timers = {
713 .name = "timer",
714 .version_id = 2,
715 .minimum_version_id = 1,
Juan Quintela35d08452014-04-16 16:01:33 +0200716 .fields = (VMStateField[]) {
Paolo Bonzini946fb272011-09-12 13:57:37 +0200717 VMSTATE_INT64(cpu_ticks_offset, TimersState),
Paolo Bonzinic1ff0732018-08-14 09:31:58 +0200718 VMSTATE_UNUSED(8),
Paolo Bonzini946fb272011-09-12 13:57:37 +0200719 VMSTATE_INT64_V(cpu_clock_offset, TimersState, 2),
720 VMSTATE_END_OF_LIST()
KONRAD Fredericd09eae32014-08-01 01:37:10 +0200721 },
Juan Quintela5cd8cad2014-09-23 14:09:54 +0200722 .subsections = (const VMStateDescription*[]) {
723 &icount_vmstate_timers,
724 NULL
Paolo Bonzini946fb272011-09-12 13:57:37 +0200725 }
726};
727
Paolo Bonzini14e6fe12016-10-31 10:36:08 +0100728static void cpu_throttle_thread(CPUState *cpu, run_on_cpu_data opaque)
Jason J. Herne2adcc852015-09-08 13:12:33 -0400729{
Jason J. Herne2adcc852015-09-08 13:12:33 -0400730 double pct;
731 double throttle_ratio;
732 long sleeptime_ns;
733
734 if (!cpu_throttle_get_percentage()) {
735 return;
736 }
737
738 pct = (double)cpu_throttle_get_percentage()/100;
739 throttle_ratio = pct / (1 - pct);
740 sleeptime_ns = (long)(throttle_ratio * CPU_THROTTLE_TIMESLICE_NS);
741
742 qemu_mutex_unlock_iothread();
Jason J. Herne2adcc852015-09-08 13:12:33 -0400743 g_usleep(sleeptime_ns / 1000); /* Convert ns to us for usleep call */
744 qemu_mutex_lock_iothread();
Felipe Franciosi90bb0c02017-05-19 22:29:50 +0100745 atomic_set(&cpu->throttle_thread_scheduled, 0);
Jason J. Herne2adcc852015-09-08 13:12:33 -0400746}
747
748static void cpu_throttle_timer_tick(void *opaque)
749{
750 CPUState *cpu;
751 double pct;
752
753 /* Stop the timer if needed */
754 if (!cpu_throttle_get_percentage()) {
755 return;
756 }
757 CPU_FOREACH(cpu) {
758 if (!atomic_xchg(&cpu->throttle_thread_scheduled, 1)) {
Paolo Bonzini14e6fe12016-10-31 10:36:08 +0100759 async_run_on_cpu(cpu, cpu_throttle_thread,
760 RUN_ON_CPU_NULL);
Jason J. Herne2adcc852015-09-08 13:12:33 -0400761 }
762 }
763
764 pct = (double)cpu_throttle_get_percentage()/100;
765 timer_mod(throttle_timer, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL_RT) +
766 CPU_THROTTLE_TIMESLICE_NS / (1-pct));
767}
768
769void cpu_throttle_set(int new_throttle_pct)
770{
771 /* Ensure throttle percentage is within valid range */
772 new_throttle_pct = MIN(new_throttle_pct, CPU_THROTTLE_PCT_MAX);
773 new_throttle_pct = MAX(new_throttle_pct, CPU_THROTTLE_PCT_MIN);
774
775 atomic_set(&throttle_percentage, new_throttle_pct);
776
777 timer_mod(throttle_timer, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL_RT) +
778 CPU_THROTTLE_TIMESLICE_NS);
779}
780
781void cpu_throttle_stop(void)
782{
783 atomic_set(&throttle_percentage, 0);
784}
785
786bool cpu_throttle_active(void)
787{
788 return (cpu_throttle_get_percentage() != 0);
789}
790
791int cpu_throttle_get_percentage(void)
792{
793 return atomic_read(&throttle_percentage);
794}
795
Pavel Dovgalyuk4603ea02014-09-01 09:34:49 +0400796void cpu_ticks_init(void)
797{
Emilio G. Cotaccdb3c12016-06-08 14:55:20 -0400798 seqlock_init(&timers_state.vm_clock_seqlock);
Pavel Dovgalyuk4603ea02014-09-01 09:34:49 +0400799 vmstate_register(NULL, 0, &vmstate_timers, &timers_state);
Jason J. Herne2adcc852015-09-08 13:12:33 -0400800 throttle_timer = timer_new_ns(QEMU_CLOCK_VIRTUAL_RT,
801 cpu_throttle_timer_tick, NULL);
Pavel Dovgalyuk4603ea02014-09-01 09:34:49 +0400802}
803
Sebastian Tanase1ad95802014-07-25 11:56:28 +0200804void configure_icount(QemuOpts *opts, Error **errp)
Paolo Bonzini946fb272011-09-12 13:57:37 +0200805{
Sebastian Tanase1ad95802014-07-25 11:56:28 +0200806 const char *option;
Sebastian Tanasea8bfac32014-07-25 11:56:29 +0200807 char *rem_str = NULL;
Sebastian Tanase1ad95802014-07-25 11:56:28 +0200808
Sebastian Tanase1ad95802014-07-25 11:56:28 +0200809 option = qemu_opt_get(opts, "shift");
Paolo Bonzini946fb272011-09-12 13:57:37 +0200810 if (!option) {
Sebastian Tanasea8bfac32014-07-25 11:56:29 +0200811 if (qemu_opt_get(opts, "align") != NULL) {
812 error_setg(errp, "Please specify shift option when using align");
813 }
Paolo Bonzini946fb272011-09-12 13:57:37 +0200814 return;
815 }
Victor CLEMENTf1f4b572015-05-29 17:14:05 +0200816
817 icount_sleep = qemu_opt_get_bool(opts, "sleep", true);
Victor CLEMENT5045e9d92015-05-29 17:14:04 +0200818 if (icount_sleep) {
Pavel Dovgalyukb39e3f32018-01-11 11:26:10 +0300819 timers_state.icount_warp_timer = timer_new_ns(QEMU_CLOCK_VIRTUAL_RT,
Pavel Dovgalyuke76d1792016-03-10 14:56:09 +0300820 icount_timer_cb, NULL);
Victor CLEMENT5045e9d92015-05-29 17:14:04 +0200821 }
Victor CLEMENTf1f4b572015-05-29 17:14:05 +0200822
Sebastian Tanasea8bfac32014-07-25 11:56:29 +0200823 icount_align_option = qemu_opt_get_bool(opts, "align", false);
Victor CLEMENTf1f4b572015-05-29 17:14:05 +0200824
825 if (icount_align_option && !icount_sleep) {
Pranith Kumar778d9f92016-02-26 10:16:51 -0500826 error_setg(errp, "align=on and sleep=off are incompatible");
Victor CLEMENTf1f4b572015-05-29 17:14:05 +0200827 }
Paolo Bonzini946fb272011-09-12 13:57:37 +0200828 if (strcmp(option, "auto") != 0) {
Sebastian Tanasea8bfac32014-07-25 11:56:29 +0200829 errno = 0;
Paolo Bonzinic1ff0732018-08-14 09:31:58 +0200830 timers_state.icount_time_shift = strtol(option, &rem_str, 0);
Sebastian Tanasea8bfac32014-07-25 11:56:29 +0200831 if (errno != 0 || *rem_str != '\0' || !strlen(option)) {
832 error_setg(errp, "icount: Invalid shift value");
833 }
Paolo Bonzini946fb272011-09-12 13:57:37 +0200834 use_icount = 1;
835 return;
Sebastian Tanasea8bfac32014-07-25 11:56:29 +0200836 } else if (icount_align_option) {
837 error_setg(errp, "shift=auto and align=on are incompatible");
Victor CLEMENTf1f4b572015-05-29 17:14:05 +0200838 } else if (!icount_sleep) {
Pranith Kumar778d9f92016-02-26 10:16:51 -0500839 error_setg(errp, "shift=auto and sleep=off are incompatible");
Paolo Bonzini946fb272011-09-12 13:57:37 +0200840 }
841
842 use_icount = 2;
843
844 /* 125MIPS seems a reasonable initial guess at the guest speed.
845 It will be corrected fairly quickly anyway. */
Paolo Bonzinic1ff0732018-08-14 09:31:58 +0200846 timers_state.icount_time_shift = 3;
Paolo Bonzini946fb272011-09-12 13:57:37 +0200847
848 /* Have both realtime and virtual time triggers for speed adjustment.
849 The realtime trigger catches emulated time passing too slowly,
850 the virtual time trigger catches emulated time passing too fast.
851 Realtime triggers occur even when idle, so use them less frequently
852 than VM triggers. */
Pavel Dovgalyukb39e3f32018-01-11 11:26:10 +0300853 timers_state.vm_clock_warp_start = -1;
854 timers_state.icount_rt_timer = timer_new_ms(QEMU_CLOCK_VIRTUAL_RT,
Pavel Dovgalyukbf2a7dd2014-11-26 13:40:55 +0300855 icount_adjust_rt, NULL);
Pavel Dovgalyukb39e3f32018-01-11 11:26:10 +0300856 timer_mod(timers_state.icount_rt_timer,
Pavel Dovgalyukbf2a7dd2014-11-26 13:40:55 +0300857 qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL_RT) + 1000);
Pavel Dovgalyukb39e3f32018-01-11 11:26:10 +0300858 timers_state.icount_vm_timer = timer_new_ns(QEMU_CLOCK_VIRTUAL,
Alex Bligh40daca52013-08-21 16:03:02 +0100859 icount_adjust_vm, NULL);
Pavel Dovgalyukb39e3f32018-01-11 11:26:10 +0300860 timer_mod(timers_state.icount_vm_timer,
Alex Bligh40daca52013-08-21 16:03:02 +0100861 qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) +
Rutuja Shah73bcb242016-03-21 21:32:30 +0530862 NANOSECONDS_PER_SECOND / 10);
Paolo Bonzini946fb272011-09-12 13:57:37 +0200863}
864
865/***********************************************************/
Alex Bennée65467062017-02-23 18:29:09 +0000866/* TCG vCPU kick timer
867 *
868 * The kick timer is responsible for moving single threaded vCPU
869 * emulation on to the next vCPU. If more than one vCPU is running a
870 * timer event with force a cpu->exit so the next vCPU can get
871 * scheduled.
872 *
873 * The timer is removed if all vCPUs are idle and restarted again once
874 * idleness is complete.
875 */
876
877static QEMUTimer *tcg_kick_vcpu_timer;
Alex Bennée791158d2017-02-23 18:29:10 +0000878static CPUState *tcg_current_rr_cpu;
Alex Bennée65467062017-02-23 18:29:09 +0000879
880#define TCG_KICK_PERIOD (NANOSECONDS_PER_SECOND / 10)
881
882static inline int64_t qemu_tcg_next_kick(void)
883{
884 return qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) + TCG_KICK_PERIOD;
885}
886
Alex Bennée791158d2017-02-23 18:29:10 +0000887/* Kick the currently round-robin scheduled vCPU */
888static void qemu_cpu_kick_rr_cpu(void)
889{
890 CPUState *cpu;
Alex Bennée791158d2017-02-23 18:29:10 +0000891 do {
892 cpu = atomic_mb_read(&tcg_current_rr_cpu);
893 if (cpu) {
894 cpu_exit(cpu);
895 }
896 } while (cpu != atomic_mb_read(&tcg_current_rr_cpu));
897}
898
Paolo Bonzini6b8f0182017-03-02 19:56:40 +0100899static void do_nothing(CPUState *cpu, run_on_cpu_data unused)
900{
901}
902
Paolo Bonzini3f53bc62017-03-03 11:50:29 +0100903void qemu_timer_notify_cb(void *opaque, QEMUClockType type)
904{
Paolo Bonzini6b8f0182017-03-02 19:56:40 +0100905 if (!use_icount || type != QEMU_CLOCK_VIRTUAL) {
906 qemu_notify_event();
907 return;
908 }
909
Peter Maydellc52e7132018-04-10 13:02:25 +0100910 if (qemu_in_vcpu_thread()) {
911 /* A CPU is currently running; kick it back out to the
912 * tcg_cpu_exec() loop so it will recalculate its
913 * icount deadline immediately.
914 */
915 qemu_cpu_kick(current_cpu);
916 } else if (first_cpu) {
Paolo Bonzini6b8f0182017-03-02 19:56:40 +0100917 /* qemu_cpu_kick is not enough to kick a halted CPU out of
918 * qemu_tcg_wait_io_event. async_run_on_cpu, instead,
919 * causes cpu_thread_is_idle to return false. This way,
920 * handle_icount_deadline can run.
Peter Maydellc52e7132018-04-10 13:02:25 +0100921 * If we have no CPUs at all for some reason, we don't
922 * need to do anything.
Paolo Bonzini6b8f0182017-03-02 19:56:40 +0100923 */
924 async_run_on_cpu(first_cpu, do_nothing, RUN_ON_CPU_NULL);
925 }
Paolo Bonzini3f53bc62017-03-03 11:50:29 +0100926}
927
Alex Bennée65467062017-02-23 18:29:09 +0000928static void kick_tcg_thread(void *opaque)
929{
930 timer_mod(tcg_kick_vcpu_timer, qemu_tcg_next_kick());
Alex Bennée791158d2017-02-23 18:29:10 +0000931 qemu_cpu_kick_rr_cpu();
Alex Bennée65467062017-02-23 18:29:09 +0000932}
933
934static void start_tcg_kick_timer(void)
935{
Paolo Bonzinidb08b682018-01-11 13:53:12 +0100936 assert(!mttcg_enabled);
937 if (!tcg_kick_vcpu_timer && CPU_NEXT(first_cpu)) {
Alex Bennée65467062017-02-23 18:29:09 +0000938 tcg_kick_vcpu_timer = timer_new_ns(QEMU_CLOCK_VIRTUAL,
939 kick_tcg_thread, NULL);
940 timer_mod(tcg_kick_vcpu_timer, qemu_tcg_next_kick());
941 }
942}
943
944static void stop_tcg_kick_timer(void)
945{
Paolo Bonzinidb08b682018-01-11 13:53:12 +0100946 assert(!mttcg_enabled);
Alex Bennée65467062017-02-23 18:29:09 +0000947 if (tcg_kick_vcpu_timer) {
948 timer_del(tcg_kick_vcpu_timer);
949 tcg_kick_vcpu_timer = NULL;
950 }
951}
952
Alex Bennée65467062017-02-23 18:29:09 +0000953/***********************************************************/
Blue Swirl296af7c2010-03-29 19:23:50 +0000954void hw_error(const char *fmt, ...)
955{
956 va_list ap;
Andreas Färber55e5c282012-12-17 06:18:02 +0100957 CPUState *cpu;
Blue Swirl296af7c2010-03-29 19:23:50 +0000958
959 va_start(ap, fmt);
960 fprintf(stderr, "qemu: hardware error: ");
961 vfprintf(stderr, fmt, ap);
962 fprintf(stderr, "\n");
Andreas Färberbdc44642013-06-24 23:50:24 +0200963 CPU_FOREACH(cpu) {
Andreas Färber55e5c282012-12-17 06:18:02 +0100964 fprintf(stderr, "CPU #%d:\n", cpu->cpu_index);
Andreas Färber878096e2013-05-27 01:33:50 +0200965 cpu_dump_state(cpu, stderr, fprintf, CPU_DUMP_FPU);
Blue Swirl296af7c2010-03-29 19:23:50 +0000966 }
967 va_end(ap);
968 abort();
969}
970
971void cpu_synchronize_all_states(void)
972{
Andreas Färber182735e2013-05-29 22:29:20 +0200973 CPUState *cpu;
Blue Swirl296af7c2010-03-29 19:23:50 +0000974
Andreas Färberbdc44642013-06-24 23:50:24 +0200975 CPU_FOREACH(cpu) {
Andreas Färber182735e2013-05-29 22:29:20 +0200976 cpu_synchronize_state(cpu);
Sergio Andres Gomez Del Realc97d6d22017-09-13 04:05:09 -0500977 /* TODO: move to cpu_synchronize_state() */
978 if (hvf_enabled()) {
979 hvf_cpu_synchronize_state(cpu);
980 }
Blue Swirl296af7c2010-03-29 19:23:50 +0000981 }
982}
983
984void cpu_synchronize_all_post_reset(void)
985{
Andreas Färber182735e2013-05-29 22:29:20 +0200986 CPUState *cpu;
Blue Swirl296af7c2010-03-29 19:23:50 +0000987
Andreas Färberbdc44642013-06-24 23:50:24 +0200988 CPU_FOREACH(cpu) {
Andreas Färber182735e2013-05-29 22:29:20 +0200989 cpu_synchronize_post_reset(cpu);
Sergio Andres Gomez Del Realc97d6d22017-09-13 04:05:09 -0500990 /* TODO: move to cpu_synchronize_post_reset() */
991 if (hvf_enabled()) {
992 hvf_cpu_synchronize_post_reset(cpu);
993 }
Blue Swirl296af7c2010-03-29 19:23:50 +0000994 }
995}
996
997void cpu_synchronize_all_post_init(void)
998{
Andreas Färber182735e2013-05-29 22:29:20 +0200999 CPUState *cpu;
Blue Swirl296af7c2010-03-29 19:23:50 +00001000
Andreas Färberbdc44642013-06-24 23:50:24 +02001001 CPU_FOREACH(cpu) {
Andreas Färber182735e2013-05-29 22:29:20 +02001002 cpu_synchronize_post_init(cpu);
Sergio Andres Gomez Del Realc97d6d22017-09-13 04:05:09 -05001003 /* TODO: move to cpu_synchronize_post_init() */
1004 if (hvf_enabled()) {
1005 hvf_cpu_synchronize_post_init(cpu);
1006 }
Blue Swirl296af7c2010-03-29 19:23:50 +00001007 }
1008}
1009
David Gibson75e972d2017-05-26 14:46:28 +10001010void cpu_synchronize_all_pre_loadvm(void)
1011{
1012 CPUState *cpu;
1013
1014 CPU_FOREACH(cpu) {
1015 cpu_synchronize_pre_loadvm(cpu);
1016 }
1017}
1018
Stefan Hajnoczi4486e892018-03-07 14:42:05 +00001019static int do_vm_stop(RunState state, bool send_stop)
Blue Swirl296af7c2010-03-29 19:23:50 +00001020{
Kevin Wolf56983462013-07-05 13:49:54 +02001021 int ret = 0;
1022
Luiz Capitulino13548692011-07-29 15:36:43 -03001023 if (runstate_is_running()) {
Blue Swirl296af7c2010-03-29 19:23:50 +00001024 cpu_disable_ticks();
Blue Swirl296af7c2010-03-29 19:23:50 +00001025 pause_all_vcpus();
Luiz Capitulinof5bbfba2011-07-29 15:04:45 -03001026 runstate_set(state);
Luiz Capitulino1dfb4dd2011-07-29 14:26:33 -03001027 vm_state_notify(0, state);
Stefan Hajnoczi4486e892018-03-07 14:42:05 +00001028 if (send_stop) {
1029 qapi_event_send_stop(&error_abort);
1030 }
Blue Swirl296af7c2010-03-29 19:23:50 +00001031 }
Kevin Wolf56983462013-07-05 13:49:54 +02001032
Kevin Wolf594a45c2013-07-18 14:52:19 +02001033 bdrv_drain_all();
Pavel Dovgalyuk6d0ceb82016-09-26 11:08:16 +03001034 replay_disable_events();
John Snow22af08e2016-09-22 21:45:51 -04001035 ret = bdrv_flush_all();
Kevin Wolf594a45c2013-07-18 14:52:19 +02001036
Kevin Wolf56983462013-07-05 13:49:54 +02001037 return ret;
Blue Swirl296af7c2010-03-29 19:23:50 +00001038}
1039
Stefan Hajnoczi4486e892018-03-07 14:42:05 +00001040/* Special vm_stop() variant for terminating the process. Historically clients
1041 * did not expect a QMP STOP event and so we need to retain compatibility.
1042 */
1043int vm_shutdown(void)
1044{
1045 return do_vm_stop(RUN_STATE_SHUTDOWN, false);
1046}
1047
Andreas Färbera1fcaa72012-05-02 23:42:26 +02001048static bool cpu_can_run(CPUState *cpu)
Blue Swirl296af7c2010-03-29 19:23:50 +00001049{
Andreas Färber4fdeee72012-05-02 23:10:09 +02001050 if (cpu->stop) {
Andreas Färbera1fcaa72012-05-02 23:42:26 +02001051 return false;
Jan Kiszka0ab07c62011-02-07 12:19:14 +01001052 }
Tiejun Chen321bc0b2013-08-02 09:43:09 +08001053 if (cpu_is_stopped(cpu)) {
Andreas Färbera1fcaa72012-05-02 23:42:26 +02001054 return false;
Jan Kiszka0ab07c62011-02-07 12:19:14 +01001055 }
Andreas Färbera1fcaa72012-05-02 23:42:26 +02001056 return true;
Blue Swirl296af7c2010-03-29 19:23:50 +00001057}
1058
Andreas Färber91325042013-05-27 02:07:49 +02001059static void cpu_handle_guest_debug(CPUState *cpu)
Jan Kiszka3c638d02010-06-25 16:56:56 +02001060{
Andreas Färber64f6b342013-05-27 02:06:09 +02001061 gdb_set_stop_cpu(cpu);
Jan Kiszka8cf71712011-02-07 12:19:16 +01001062 qemu_system_debug_request();
Andreas Färberf324e762012-05-02 23:26:21 +02001063 cpu->stopped = true;
Jan Kiszka3c638d02010-06-25 16:56:56 +02001064}
1065
Jan Kiszka6d9cb732011-02-01 22:15:58 +01001066#ifdef CONFIG_LINUX
1067static void sigbus_reraise(void)
1068{
1069 sigset_t set;
1070 struct sigaction action;
1071
1072 memset(&action, 0, sizeof(action));
1073 action.sa_handler = SIG_DFL;
1074 if (!sigaction(SIGBUS, &action, NULL)) {
1075 raise(SIGBUS);
1076 sigemptyset(&set);
1077 sigaddset(&set, SIGBUS);
Peter Maydella2d17612016-05-16 18:33:59 +01001078 pthread_sigmask(SIG_UNBLOCK, &set, NULL);
Jan Kiszka6d9cb732011-02-01 22:15:58 +01001079 }
1080 perror("Failed to re-raise SIGBUS!\n");
1081 abort();
1082}
1083
Paolo Bonzinid98d4072017-02-08 13:22:12 +01001084static void sigbus_handler(int n, siginfo_t *siginfo, void *ctx)
Jan Kiszka6d9cb732011-02-01 22:15:58 +01001085{
Paolo Bonzinia16fc072017-02-09 09:50:02 +01001086 if (siginfo->si_code != BUS_MCEERR_AO && siginfo->si_code != BUS_MCEERR_AR) {
1087 sigbus_reraise();
1088 }
1089
Paolo Bonzini2ae41db2017-02-08 12:48:54 +01001090 if (current_cpu) {
1091 /* Called asynchronously in VCPU thread. */
1092 if (kvm_on_sigbus_vcpu(current_cpu, siginfo->si_code, siginfo->si_addr)) {
1093 sigbus_reraise();
1094 }
1095 } else {
1096 /* Called synchronously (via signalfd) in main thread. */
1097 if (kvm_on_sigbus(siginfo->si_code, siginfo->si_addr)) {
1098 sigbus_reraise();
1099 }
Jan Kiszka6d9cb732011-02-01 22:15:58 +01001100 }
1101}
1102
1103static void qemu_init_sigbus(void)
1104{
1105 struct sigaction action;
1106
1107 memset(&action, 0, sizeof(action));
1108 action.sa_flags = SA_SIGINFO;
Paolo Bonzinid98d4072017-02-08 13:22:12 +01001109 action.sa_sigaction = sigbus_handler;
Jan Kiszka6d9cb732011-02-01 22:15:58 +01001110 sigaction(SIGBUS, &action, NULL);
1111
1112 prctl(PR_MCE_KILL, PR_MCE_KILL_SET, PR_MCE_KILL_EARLY, 0, 0);
1113}
Paolo Bonzinia16fc072017-02-09 09:50:02 +01001114#else /* !CONFIG_LINUX */
1115static void qemu_init_sigbus(void)
1116{
1117}
Paolo Bonzinia16fc072017-02-09 09:50:02 +01001118#endif /* !CONFIG_LINUX */
Blue Swirl296af7c2010-03-29 19:23:50 +00001119
Stefan Weilb2532d82012-09-27 07:41:42 +02001120static QemuMutex qemu_global_mutex;
Blue Swirl296af7c2010-03-29 19:23:50 +00001121
1122static QemuThread io_thread;
1123
Blue Swirl296af7c2010-03-29 19:23:50 +00001124/* cpu creation */
1125static QemuCond qemu_cpu_cond;
1126/* system init */
Blue Swirl296af7c2010-03-29 19:23:50 +00001127static QemuCond qemu_pause_cond;
1128
Paolo Bonzinid3b12f52011-09-13 10:30:52 +02001129void qemu_init_cpu_loop(void)
Blue Swirl296af7c2010-03-29 19:23:50 +00001130{
Jan Kiszka6d9cb732011-02-01 22:15:58 +01001131 qemu_init_sigbus();
Anthony Liguoried945922011-02-08 18:18:18 +01001132 qemu_cond_init(&qemu_cpu_cond);
Anthony Liguoried945922011-02-08 18:18:18 +01001133 qemu_cond_init(&qemu_pause_cond);
Blue Swirl296af7c2010-03-29 19:23:50 +00001134 qemu_mutex_init(&qemu_global_mutex);
Blue Swirl296af7c2010-03-29 19:23:50 +00001135
Jan Kiszkab7680cb2011-03-12 17:43:51 +01001136 qemu_thread_get_self(&io_thread);
Blue Swirl296af7c2010-03-29 19:23:50 +00001137}
1138
Paolo Bonzini14e6fe12016-10-31 10:36:08 +01001139void run_on_cpu(CPUState *cpu, run_on_cpu_func func, run_on_cpu_data data)
Marcelo Tosattie82bcec2010-05-04 09:45:22 -03001140{
Sergey Fedorovd148d902016-08-29 09:51:00 +02001141 do_run_on_cpu(cpu, func, data, &qemu_global_mutex);
Chegu Vinod3c022702013-06-24 03:49:41 -06001142}
1143
Gu Zheng4c055ab2016-05-12 09:18:13 +05301144static void qemu_kvm_destroy_vcpu(CPUState *cpu)
1145{
1146 if (kvm_destroy_vcpu(cpu) < 0) {
1147 error_report("kvm_destroy_vcpu failed");
1148 exit(EXIT_FAILURE);
1149 }
1150}
1151
1152static void qemu_tcg_destroy_vcpu(CPUState *cpu)
1153{
1154}
1155
David Hildenbrandebd05fe2017-11-29 20:12:15 +01001156static void qemu_cpu_stop(CPUState *cpu, bool exit)
1157{
1158 g_assert(qemu_cpu_is_self(cpu));
1159 cpu->stop = false;
1160 cpu->stopped = true;
1161 if (exit) {
1162 cpu_exit(cpu);
1163 }
1164 qemu_cond_broadcast(&qemu_pause_cond);
1165}
1166
Andreas Färber509a0d72012-05-03 02:18:09 +02001167static void qemu_wait_io_event_common(CPUState *cpu)
Blue Swirl296af7c2010-03-29 19:23:50 +00001168{
Alex Bennée37257942017-02-23 18:29:14 +00001169 atomic_mb_set(&cpu->thread_kicked, false);
Andreas Färber4fdeee72012-05-02 23:10:09 +02001170 if (cpu->stop) {
David Hildenbrandebd05fe2017-11-29 20:12:15 +01001171 qemu_cpu_stop(cpu, false);
Blue Swirl296af7c2010-03-29 19:23:50 +00001172 }
Sergey Fedorova5403c62016-08-02 18:27:36 +01001173 process_queued_cpu_work(cpu);
Alex Bennée37257942017-02-23 18:29:14 +00001174}
1175
Paolo Bonzinidb08b682018-01-11 13:53:12 +01001176static void qemu_tcg_rr_wait_io_event(CPUState *cpu)
Alex Bennée37257942017-02-23 18:29:14 +00001177{
Paolo Bonzinidb08b682018-01-11 13:53:12 +01001178 while (all_cpu_threads_idle()) {
Alex Bennée65467062017-02-23 18:29:09 +00001179 stop_tcg_kick_timer();
KONRAD Fredericd5f8d612015-08-10 17:27:06 +02001180 qemu_cond_wait(cpu->halt_cond, &qemu_global_mutex);
Jan Kiszka16400322011-02-09 16:29:37 +01001181 }
Blue Swirl296af7c2010-03-29 19:23:50 +00001182
Alex Bennée65467062017-02-23 18:29:09 +00001183 start_tcg_kick_timer();
1184
Alex Bennée37257942017-02-23 18:29:14 +00001185 qemu_wait_io_event_common(cpu);
Blue Swirl296af7c2010-03-29 19:23:50 +00001186}
1187
Paolo Bonzinidb08b682018-01-11 13:53:12 +01001188static void qemu_wait_io_event(CPUState *cpu)
Blue Swirl296af7c2010-03-29 19:23:50 +00001189{
Andreas Färbera98ae1d2013-05-26 23:21:08 +02001190 while (cpu_thread_is_idle(cpu)) {
Andreas Färberf5c121b2012-05-03 01:22:49 +02001191 qemu_cond_wait(cpu->halt_cond, &qemu_global_mutex);
Jan Kiszka16400322011-02-09 16:29:37 +01001192 }
Blue Swirl296af7c2010-03-29 19:23:50 +00001193
Paolo Bonzinidb08b682018-01-11 13:53:12 +01001194#ifdef _WIN32
1195 /* Eat dummy APC queued by qemu_cpu_kick_thread. */
1196 if (!tcg_enabled()) {
1197 SleepEx(0, TRUE);
Sergio Andres Gomez Del Realc97d6d22017-09-13 04:05:09 -05001198 }
Paolo Bonzinidb08b682018-01-11 13:53:12 +01001199#endif
Sergio Andres Gomez Del Realc97d6d22017-09-13 04:05:09 -05001200 qemu_wait_io_event_common(cpu);
1201}
1202
Jan Kiszka7e97cd82011-02-07 12:19:12 +01001203static void *qemu_kvm_cpu_thread_fn(void *arg)
Blue Swirl296af7c2010-03-29 19:23:50 +00001204{
Andreas Färber48a106b2013-05-27 02:20:39 +02001205 CPUState *cpu = arg;
Jan Kiszka84b49152011-02-01 22:15:50 +01001206 int r;
Blue Swirl296af7c2010-03-29 19:23:50 +00001207
Paolo Bonziniab28bd22015-07-09 08:55:38 +02001208 rcu_register_thread();
1209
Paolo Bonzini2e7f7a32015-06-18 18:47:18 +02001210 qemu_mutex_lock_iothread();
Andreas Färber814e6122012-05-02 17:00:37 +02001211 qemu_thread_get_self(cpu->thread);
Andreas Färber9f09e182012-05-03 06:59:07 +02001212 cpu->thread_id = qemu_get_thread_id();
Pavel Dovgalyuk626cf8f2014-12-08 10:53:17 +03001213 cpu->can_do_io = 1;
Andreas Färber4917cf42013-05-27 05:17:50 +02001214 current_cpu = cpu;
Blue Swirl296af7c2010-03-29 19:23:50 +00001215
Andreas Färber504134d2012-12-17 06:38:45 +01001216 r = kvm_init_vcpu(cpu);
Jan Kiszka84b49152011-02-01 22:15:50 +01001217 if (r < 0) {
Alistair Francis493d89b2018-02-03 09:43:14 +01001218 error_report("kvm_init_vcpu failed: %s", strerror(-r));
Jan Kiszka84b49152011-02-01 22:15:50 +01001219 exit(1);
1220 }
Blue Swirl296af7c2010-03-29 19:23:50 +00001221
Paolo Bonzini18268b62017-02-09 09:41:14 +01001222 kvm_init_cpu_signals(cpu);
Blue Swirl296af7c2010-03-29 19:23:50 +00001223
1224 /* signal CPU creation */
Andreas Färber61a46212012-05-02 22:49:36 +02001225 cpu->created = true;
Blue Swirl296af7c2010-03-29 19:23:50 +00001226 qemu_cond_signal(&qemu_cpu_cond);
1227
Gu Zheng4c055ab2016-05-12 09:18:13 +05301228 do {
Andreas Färbera1fcaa72012-05-02 23:42:26 +02001229 if (cpu_can_run(cpu)) {
Andreas Färber1458c362013-05-26 23:46:55 +02001230 r = kvm_cpu_exec(cpu);
Jan Kiszka83f338f2011-02-07 12:19:17 +01001231 if (r == EXCP_DEBUG) {
Andreas Färber91325042013-05-27 02:07:49 +02001232 cpu_handle_guest_debug(cpu);
Jan Kiszka83f338f2011-02-07 12:19:17 +01001233 }
Jan Kiszka0ab07c62011-02-07 12:19:14 +01001234 }
Paolo Bonzinidb08b682018-01-11 13:53:12 +01001235 qemu_wait_io_event(cpu);
Gu Zheng4c055ab2016-05-12 09:18:13 +05301236 } while (!cpu->unplug || cpu_can_run(cpu));
Blue Swirl296af7c2010-03-29 19:23:50 +00001237
Gu Zheng4c055ab2016-05-12 09:18:13 +05301238 qemu_kvm_destroy_vcpu(cpu);
Bharata B Rao2c579042016-05-12 09:18:14 +05301239 cpu->created = false;
1240 qemu_cond_signal(&qemu_cpu_cond);
Gu Zheng4c055ab2016-05-12 09:18:13 +05301241 qemu_mutex_unlock_iothread();
Paolo Bonzini57615ed2018-01-30 11:04:36 -05001242 rcu_unregister_thread();
Blue Swirl296af7c2010-03-29 19:23:50 +00001243 return NULL;
1244}
1245
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +02001246static void *qemu_dummy_cpu_thread_fn(void *arg)
1247{
1248#ifdef _WIN32
Alistair Francis493d89b2018-02-03 09:43:14 +01001249 error_report("qtest is not supported under Windows");
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +02001250 exit(1);
1251#else
Andreas Färber10a90212013-05-27 02:24:35 +02001252 CPUState *cpu = arg;
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +02001253 sigset_t waitset;
1254 int r;
1255
Paolo Bonziniab28bd22015-07-09 08:55:38 +02001256 rcu_register_thread();
1257
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +02001258 qemu_mutex_lock_iothread();
Andreas Färber814e6122012-05-02 17:00:37 +02001259 qemu_thread_get_self(cpu->thread);
Andreas Färber9f09e182012-05-03 06:59:07 +02001260 cpu->thread_id = qemu_get_thread_id();
Pavel Dovgalyuk626cf8f2014-12-08 10:53:17 +03001261 cpu->can_do_io = 1;
Alex Bennée37257942017-02-23 18:29:14 +00001262 current_cpu = cpu;
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +02001263
1264 sigemptyset(&waitset);
1265 sigaddset(&waitset, SIG_IPI);
1266
1267 /* signal CPU creation */
Andreas Färber61a46212012-05-02 22:49:36 +02001268 cpu->created = true;
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +02001269 qemu_cond_signal(&qemu_cpu_cond);
1270
Paolo Bonzinid2831ab2018-01-30 11:04:53 -05001271 do {
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +02001272 qemu_mutex_unlock_iothread();
1273 do {
1274 int sig;
1275 r = sigwait(&waitset, &sig);
1276 } while (r == -1 && (errno == EAGAIN || errno == EINTR));
1277 if (r == -1) {
1278 perror("sigwait");
1279 exit(1);
1280 }
1281 qemu_mutex_lock_iothread();
Paolo Bonzinidb08b682018-01-11 13:53:12 +01001282 qemu_wait_io_event(cpu);
Paolo Bonzinid2831ab2018-01-30 11:04:53 -05001283 } while (!cpu->unplug);
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +02001284
Paolo Bonzinid2831ab2018-01-30 11:04:53 -05001285 rcu_unregister_thread();
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +02001286 return NULL;
1287#endif
1288}
1289
Alex Bennée1be7fcb2016-10-27 16:10:08 +01001290static int64_t tcg_get_icount_limit(void)
1291{
1292 int64_t deadline;
1293
1294 if (replay_mode != REPLAY_MODE_PLAY) {
1295 deadline = qemu_clock_deadline_ns_all(QEMU_CLOCK_VIRTUAL);
1296
1297 /* Maintain prior (possibly buggy) behaviour where if no deadline
1298 * was set (as there is no QEMU_CLOCK_VIRTUAL timer) or it is more than
1299 * INT32_MAX nanoseconds ahead, we still use INT32_MAX
1300 * nanoseconds.
1301 */
1302 if ((deadline < 0) || (deadline > INT32_MAX)) {
1303 deadline = INT32_MAX;
1304 }
1305
1306 return qemu_icount_round(deadline);
1307 } else {
1308 return replay_get_instructions();
1309 }
1310}
1311
Alex Bennée12e97002016-10-27 16:10:14 +01001312static void handle_icount_deadline(void)
1313{
Paolo Bonzini6b8f0182017-03-02 19:56:40 +01001314 assert(qemu_in_vcpu_thread());
Alex Bennée12e97002016-10-27 16:10:14 +01001315 if (use_icount) {
1316 int64_t deadline =
1317 qemu_clock_deadline_ns_all(QEMU_CLOCK_VIRTUAL);
1318
1319 if (deadline == 0) {
Paolo Bonzini6b8f0182017-03-02 19:56:40 +01001320 /* Wake up other AioContexts. */
Alex Bennée12e97002016-10-27 16:10:14 +01001321 qemu_clock_notify(QEMU_CLOCK_VIRTUAL);
Paolo Bonzini6b8f0182017-03-02 19:56:40 +01001322 qemu_clock_run_timers(QEMU_CLOCK_VIRTUAL);
Alex Bennée12e97002016-10-27 16:10:14 +01001323 }
1324 }
1325}
1326
Alex Bennée05248382017-03-29 16:46:59 +01001327static void prepare_icount_for_run(CPUState *cpu)
1328{
1329 if (use_icount) {
Alex Bennéeeda5f7c2017-04-05 12:35:48 +01001330 int insns_left;
Alex Bennée05248382017-03-29 16:46:59 +01001331
1332 /* These should always be cleared by process_icount_data after
1333 * each vCPU execution. However u16.high can be raised
1334 * asynchronously by cpu_exit/cpu_interrupt/tcg_handle_interrupt
1335 */
1336 g_assert(cpu->icount_decr.u16.low == 0);
1337 g_assert(cpu->icount_extra == 0);
1338
Alex Bennéeeda5f7c2017-04-05 12:35:48 +01001339 cpu->icount_budget = tcg_get_icount_limit();
1340 insns_left = MIN(0xffff, cpu->icount_budget);
1341 cpu->icount_decr.u16.low = insns_left;
1342 cpu->icount_extra = cpu->icount_budget - insns_left;
Alex Bennéed759c952018-02-27 12:52:48 +03001343
1344 replay_mutex_lock();
Alex Bennée05248382017-03-29 16:46:59 +01001345 }
1346}
1347
1348static void process_icount_data(CPUState *cpu)
1349{
1350 if (use_icount) {
Alex Bennéee4cd9652017-03-31 16:09:42 +01001351 /* Account for executed instructions */
Alex Bennée512d3c82017-04-05 12:32:37 +01001352 cpu_update_icount(cpu);
Alex Bennée05248382017-03-29 16:46:59 +01001353
1354 /* Reset the counters */
1355 cpu->icount_decr.u16.low = 0;
1356 cpu->icount_extra = 0;
Alex Bennéee4cd9652017-03-31 16:09:42 +01001357 cpu->icount_budget = 0;
1358
Alex Bennée05248382017-03-29 16:46:59 +01001359 replay_account_executed_instructions();
Alex Bennéed759c952018-02-27 12:52:48 +03001360
1361 replay_mutex_unlock();
Alex Bennée05248382017-03-29 16:46:59 +01001362 }
1363}
1364
1365
Alex Bennée1be7fcb2016-10-27 16:10:08 +01001366static int tcg_cpu_exec(CPUState *cpu)
1367{
1368 int ret;
1369#ifdef CONFIG_PROFILER
1370 int64_t ti;
1371#endif
1372
Emilio G. Cotaf28d0df2018-06-22 13:45:31 -04001373 assert(tcg_enabled());
Alex Bennée1be7fcb2016-10-27 16:10:08 +01001374#ifdef CONFIG_PROFILER
1375 ti = profile_getclock();
1376#endif
Alex Bennée1be7fcb2016-10-27 16:10:08 +01001377 cpu_exec_start(cpu);
1378 ret = cpu_exec(cpu);
1379 cpu_exec_end(cpu);
1380#ifdef CONFIG_PROFILER
1381 tcg_time += profile_getclock() - ti;
1382#endif
Alex Bennée1be7fcb2016-10-27 16:10:08 +01001383 return ret;
1384}
1385
Alex Bennéec93bbbe2016-10-27 16:10:09 +01001386/* Destroy any remaining vCPUs which have been unplugged and have
1387 * finished running
1388 */
1389static void deal_with_unplugged_cpus(void)
Alex Bennée1be7fcb2016-10-27 16:10:08 +01001390{
Alex Bennéec93bbbe2016-10-27 16:10:09 +01001391 CPUState *cpu;
Alex Bennée1be7fcb2016-10-27 16:10:08 +01001392
Alex Bennéec93bbbe2016-10-27 16:10:09 +01001393 CPU_FOREACH(cpu) {
1394 if (cpu->unplug && !cpu_can_run(cpu)) {
1395 qemu_tcg_destroy_vcpu(cpu);
1396 cpu->created = false;
1397 qemu_cond_signal(&qemu_cpu_cond);
Alex Bennée1be7fcb2016-10-27 16:10:08 +01001398 break;
1399 }
1400 }
Alex Bennée1be7fcb2016-10-27 16:10:08 +01001401}
Jan Kiszkabdb7ca62011-09-26 09:40:39 +02001402
Alex Bennée65467062017-02-23 18:29:09 +00001403/* Single-threaded TCG
1404 *
1405 * In the single-threaded case each vCPU is simulated in turn. If
1406 * there is more than a single vCPU we create a simple timer to kick
1407 * the vCPU and ensure we don't get stuck in a tight loop in one vCPU.
1408 * This is done explicitly rather than relying on side-effects
1409 * elsewhere.
1410 */
1411
Alex Bennée37257942017-02-23 18:29:14 +00001412static void *qemu_tcg_rr_cpu_thread_fn(void *arg)
Blue Swirl296af7c2010-03-29 19:23:50 +00001413{
Andreas Färberc3586ba2012-05-03 01:41:24 +02001414 CPUState *cpu = arg;
Blue Swirl296af7c2010-03-29 19:23:50 +00001415
Emilio G. Cotaf28d0df2018-06-22 13:45:31 -04001416 assert(tcg_enabled());
Paolo Bonziniab28bd22015-07-09 08:55:38 +02001417 rcu_register_thread();
Emilio G. Cota3468b592017-07-19 18:57:58 -04001418 tcg_register_thread();
Paolo Bonziniab28bd22015-07-09 08:55:38 +02001419
Paolo Bonzini2e7f7a32015-06-18 18:47:18 +02001420 qemu_mutex_lock_iothread();
Andreas Färber814e6122012-05-02 17:00:37 +02001421 qemu_thread_get_self(cpu->thread);
Blue Swirl296af7c2010-03-29 19:23:50 +00001422
David Hildenbrand5a9c9732018-02-09 20:52:39 +01001423 cpu->thread_id = qemu_get_thread_id();
1424 cpu->created = true;
1425 cpu->can_do_io = 1;
Blue Swirl296af7c2010-03-29 19:23:50 +00001426 qemu_cond_signal(&qemu_cpu_cond);
1427
Jan Kiszkafa7d1862011-08-22 18:35:25 +02001428 /* wait for initial kick-off after machine start */
Emilio G. Cotac28e3992015-04-27 12:45:28 -04001429 while (first_cpu->stopped) {
KONRAD Fredericd5f8d612015-08-10 17:27:06 +02001430 qemu_cond_wait(first_cpu->halt_cond, &qemu_global_mutex);
Jan Kiszka8e564b42012-02-17 18:31:15 +01001431
1432 /* process any pending work */
Andreas Färberbdc44642013-06-24 23:50:24 +02001433 CPU_FOREACH(cpu) {
Alex Bennée37257942017-02-23 18:29:14 +00001434 current_cpu = cpu;
Andreas Färber182735e2013-05-29 22:29:20 +02001435 qemu_wait_io_event_common(cpu);
Jan Kiszka8e564b42012-02-17 18:31:15 +01001436 }
Jan Kiszka0ab07c62011-02-07 12:19:14 +01001437 }
Blue Swirl296af7c2010-03-29 19:23:50 +00001438
Alex Bennée65467062017-02-23 18:29:09 +00001439 start_tcg_kick_timer();
1440
Alex Bennéec93bbbe2016-10-27 16:10:09 +01001441 cpu = first_cpu;
1442
Alex Bennéee5143e32017-02-23 18:29:12 +00001443 /* process any pending work */
1444 cpu->exit_request = 1;
1445
Blue Swirl296af7c2010-03-29 19:23:50 +00001446 while (1) {
Alex Bennéed759c952018-02-27 12:52:48 +03001447 qemu_mutex_unlock_iothread();
1448 replay_mutex_lock();
1449 qemu_mutex_lock_iothread();
Alex Bennéec93bbbe2016-10-27 16:10:09 +01001450 /* Account partial waits to QEMU_CLOCK_VIRTUAL. */
1451 qemu_account_warp_timer();
1452
Paolo Bonzini6b8f0182017-03-02 19:56:40 +01001453 /* Run the timers here. This is much more efficient than
1454 * waking up the I/O thread and waiting for completion.
1455 */
1456 handle_icount_deadline();
1457
Alex Bennéed759c952018-02-27 12:52:48 +03001458 replay_mutex_unlock();
1459
Alex Bennéec93bbbe2016-10-27 16:10:09 +01001460 if (!cpu) {
1461 cpu = first_cpu;
1462 }
1463
Alex Bennéee5143e32017-02-23 18:29:12 +00001464 while (cpu && !cpu->queued_work_first && !cpu->exit_request) {
1465
Alex Bennée791158d2017-02-23 18:29:10 +00001466 atomic_mb_set(&tcg_current_rr_cpu, cpu);
Alex Bennée37257942017-02-23 18:29:14 +00001467 current_cpu = cpu;
Alex Bennéec93bbbe2016-10-27 16:10:09 +01001468
1469 qemu_clock_enable(QEMU_CLOCK_VIRTUAL,
1470 (cpu->singlestep_enabled & SSTEP_NOTIMER) == 0);
1471
1472 if (cpu_can_run(cpu)) {
1473 int r;
Alex Bennée05248382017-03-29 16:46:59 +01001474
Alex Bennéed759c952018-02-27 12:52:48 +03001475 qemu_mutex_unlock_iothread();
Alex Bennée05248382017-03-29 16:46:59 +01001476 prepare_icount_for_run(cpu);
1477
Alex Bennéec93bbbe2016-10-27 16:10:09 +01001478 r = tcg_cpu_exec(cpu);
Alex Bennée05248382017-03-29 16:46:59 +01001479
1480 process_icount_data(cpu);
Alex Bennéed759c952018-02-27 12:52:48 +03001481 qemu_mutex_lock_iothread();
Alex Bennée05248382017-03-29 16:46:59 +01001482
Alex Bennéec93bbbe2016-10-27 16:10:09 +01001483 if (r == EXCP_DEBUG) {
1484 cpu_handle_guest_debug(cpu);
1485 break;
Pranith Kumar08e73c42017-02-23 18:29:15 +00001486 } else if (r == EXCP_ATOMIC) {
1487 qemu_mutex_unlock_iothread();
1488 cpu_exec_step_atomic(cpu);
1489 qemu_mutex_lock_iothread();
1490 break;
Alex Bennéec93bbbe2016-10-27 16:10:09 +01001491 }
Alex Bennée37257942017-02-23 18:29:14 +00001492 } else if (cpu->stop) {
Alex Bennéec93bbbe2016-10-27 16:10:09 +01001493 if (cpu->unplug) {
1494 cpu = CPU_NEXT(cpu);
1495 }
1496 break;
1497 }
1498
Alex Bennéee5143e32017-02-23 18:29:12 +00001499 cpu = CPU_NEXT(cpu);
1500 } /* while (cpu && !cpu->exit_request).. */
1501
Alex Bennée791158d2017-02-23 18:29:10 +00001502 /* Does not need atomic_mb_set because a spurious wakeup is okay. */
1503 atomic_set(&tcg_current_rr_cpu, NULL);
Alex Bennéec93bbbe2016-10-27 16:10:09 +01001504
Alex Bennéee5143e32017-02-23 18:29:12 +00001505 if (cpu && cpu->exit_request) {
1506 atomic_mb_set(&cpu->exit_request, 0);
1507 }
Alex Blighac70aaf2013-08-21 16:02:57 +01001508
Emilio G. Cota068a5ea2018-08-19 05:13:35 -04001509 qemu_tcg_rr_wait_io_event(cpu ? cpu : first_cpu);
Alex Bennéec93bbbe2016-10-27 16:10:09 +01001510 deal_with_unplugged_cpus();
Blue Swirl296af7c2010-03-29 19:23:50 +00001511 }
1512
Paolo Bonzini9b0605f2018-01-30 11:05:06 -05001513 rcu_unregister_thread();
Blue Swirl296af7c2010-03-29 19:23:50 +00001514 return NULL;
1515}
1516
Vincent Palatinb0cb0a62017-01-10 11:59:57 +01001517static void *qemu_hax_cpu_thread_fn(void *arg)
1518{
1519 CPUState *cpu = arg;
1520 int r;
Vincent Palatinb3d3a422017-03-20 11:15:49 +01001521
Paolo Bonzini9857c2d2018-01-30 16:28:49 +01001522 rcu_register_thread();
Vincent Palatinb3d3a422017-03-20 11:15:49 +01001523 qemu_mutex_lock_iothread();
Vincent Palatinb0cb0a62017-01-10 11:59:57 +01001524 qemu_thread_get_self(cpu->thread);
Vincent Palatinb0cb0a62017-01-10 11:59:57 +01001525
1526 cpu->thread_id = qemu_get_thread_id();
1527 cpu->created = true;
1528 cpu->halted = 0;
1529 current_cpu = cpu;
1530
1531 hax_init_vcpu(cpu);
1532 qemu_cond_signal(&qemu_cpu_cond);
1533
Paolo Bonzini9857c2d2018-01-30 16:28:49 +01001534 do {
Vincent Palatinb0cb0a62017-01-10 11:59:57 +01001535 if (cpu_can_run(cpu)) {
1536 r = hax_smp_cpu_exec(cpu);
1537 if (r == EXCP_DEBUG) {
1538 cpu_handle_guest_debug(cpu);
1539 }
1540 }
1541
Paolo Bonzinidb08b682018-01-11 13:53:12 +01001542 qemu_wait_io_event(cpu);
Paolo Bonzini9857c2d2018-01-30 16:28:49 +01001543 } while (!cpu->unplug || cpu_can_run(cpu));
1544 rcu_unregister_thread();
Vincent Palatinb0cb0a62017-01-10 11:59:57 +01001545 return NULL;
1546}
1547
Sergio Andres Gomez Del Realc97d6d22017-09-13 04:05:09 -05001548/* The HVF-specific vCPU thread function. This one should only run when the host
1549 * CPU supports the VMX "unrestricted guest" feature. */
1550static void *qemu_hvf_cpu_thread_fn(void *arg)
1551{
1552 CPUState *cpu = arg;
1553
1554 int r;
1555
1556 assert(hvf_enabled());
1557
1558 rcu_register_thread();
1559
1560 qemu_mutex_lock_iothread();
1561 qemu_thread_get_self(cpu->thread);
1562
1563 cpu->thread_id = qemu_get_thread_id();
1564 cpu->can_do_io = 1;
1565 current_cpu = cpu;
1566
1567 hvf_init_vcpu(cpu);
1568
1569 /* signal CPU creation */
1570 cpu->created = true;
1571 qemu_cond_signal(&qemu_cpu_cond);
1572
1573 do {
1574 if (cpu_can_run(cpu)) {
1575 r = hvf_vcpu_exec(cpu);
1576 if (r == EXCP_DEBUG) {
1577 cpu_handle_guest_debug(cpu);
1578 }
1579 }
Paolo Bonzinidb08b682018-01-11 13:53:12 +01001580 qemu_wait_io_event(cpu);
Sergio Andres Gomez Del Realc97d6d22017-09-13 04:05:09 -05001581 } while (!cpu->unplug || cpu_can_run(cpu));
1582
1583 hvf_vcpu_destroy(cpu);
1584 cpu->created = false;
1585 qemu_cond_signal(&qemu_cpu_cond);
1586 qemu_mutex_unlock_iothread();
Paolo Bonzini8178e632018-01-30 11:05:21 -05001587 rcu_unregister_thread();
Sergio Andres Gomez Del Realc97d6d22017-09-13 04:05:09 -05001588 return NULL;
1589}
1590
Justin Terry (VM)19306802018-01-22 13:07:49 -08001591static void *qemu_whpx_cpu_thread_fn(void *arg)
1592{
1593 CPUState *cpu = arg;
1594 int r;
1595
1596 rcu_register_thread();
1597
1598 qemu_mutex_lock_iothread();
1599 qemu_thread_get_self(cpu->thread);
1600 cpu->thread_id = qemu_get_thread_id();
1601 current_cpu = cpu;
1602
1603 r = whpx_init_vcpu(cpu);
1604 if (r < 0) {
1605 fprintf(stderr, "whpx_init_vcpu failed: %s\n", strerror(-r));
1606 exit(1);
1607 }
1608
1609 /* signal CPU creation */
1610 cpu->created = true;
1611 qemu_cond_signal(&qemu_cpu_cond);
1612
1613 do {
1614 if (cpu_can_run(cpu)) {
1615 r = whpx_vcpu_exec(cpu);
1616 if (r == EXCP_DEBUG) {
1617 cpu_handle_guest_debug(cpu);
1618 }
1619 }
1620 while (cpu_thread_is_idle(cpu)) {
1621 qemu_cond_wait(cpu->halt_cond, &qemu_global_mutex);
1622 }
1623 qemu_wait_io_event_common(cpu);
1624 } while (!cpu->unplug || cpu_can_run(cpu));
1625
1626 whpx_destroy_vcpu(cpu);
1627 cpu->created = false;
1628 qemu_cond_signal(&qemu_cpu_cond);
1629 qemu_mutex_unlock_iothread();
1630 rcu_unregister_thread();
Vincent Palatinb0cb0a62017-01-10 11:59:57 +01001631 return NULL;
1632}
1633
1634#ifdef _WIN32
1635static void CALLBACK dummy_apc_func(ULONG_PTR unused)
1636{
1637}
1638#endif
1639
Alex Bennée37257942017-02-23 18:29:14 +00001640/* Multi-threaded TCG
1641 *
1642 * In the multi-threaded case each vCPU has its own thread. The TLS
1643 * variable current_cpu can be used deep in the code to find the
1644 * current CPUState for a given thread.
1645 */
1646
1647static void *qemu_tcg_cpu_thread_fn(void *arg)
1648{
1649 CPUState *cpu = arg;
1650
Emilio G. Cotaf28d0df2018-06-22 13:45:31 -04001651 assert(tcg_enabled());
Alex Bennéebf51c722017-03-30 18:32:29 +01001652 g_assert(!use_icount);
1653
Alex Bennée37257942017-02-23 18:29:14 +00001654 rcu_register_thread();
Emilio G. Cota3468b592017-07-19 18:57:58 -04001655 tcg_register_thread();
Alex Bennée37257942017-02-23 18:29:14 +00001656
1657 qemu_mutex_lock_iothread();
1658 qemu_thread_get_self(cpu->thread);
1659
1660 cpu->thread_id = qemu_get_thread_id();
1661 cpu->created = true;
1662 cpu->can_do_io = 1;
1663 current_cpu = cpu;
1664 qemu_cond_signal(&qemu_cpu_cond);
1665
1666 /* process any pending work */
1667 cpu->exit_request = 1;
1668
Cédric Le Goater54961aa2018-04-25 15:18:28 +02001669 do {
Alex Bennée37257942017-02-23 18:29:14 +00001670 if (cpu_can_run(cpu)) {
1671 int r;
Alex Bennéed759c952018-02-27 12:52:48 +03001672 qemu_mutex_unlock_iothread();
Alex Bennée37257942017-02-23 18:29:14 +00001673 r = tcg_cpu_exec(cpu);
Alex Bennéed759c952018-02-27 12:52:48 +03001674 qemu_mutex_lock_iothread();
Alex Bennée37257942017-02-23 18:29:14 +00001675 switch (r) {
1676 case EXCP_DEBUG:
1677 cpu_handle_guest_debug(cpu);
1678 break;
1679 case EXCP_HALTED:
1680 /* during start-up the vCPU is reset and the thread is
1681 * kicked several times. If we don't ensure we go back
1682 * to sleep in the halted state we won't cleanly
1683 * start-up when the vCPU is enabled.
1684 *
1685 * cpu->halted should ensure we sleep in wait_io_event
1686 */
1687 g_assert(cpu->halted);
1688 break;
Pranith Kumar08e73c42017-02-23 18:29:15 +00001689 case EXCP_ATOMIC:
1690 qemu_mutex_unlock_iothread();
1691 cpu_exec_step_atomic(cpu);
1692 qemu_mutex_lock_iothread();
Alex Bennée37257942017-02-23 18:29:14 +00001693 default:
1694 /* Ignore everything else? */
1695 break;
1696 }
1697 }
1698
Alex Bennée37257942017-02-23 18:29:14 +00001699 atomic_mb_set(&cpu->exit_request, 0);
Paolo Bonzinidb08b682018-01-11 13:53:12 +01001700 qemu_wait_io_event(cpu);
Paolo Bonzini9b0605f2018-01-30 11:05:06 -05001701 } while (!cpu->unplug || cpu_can_run(cpu));
Alex Bennée37257942017-02-23 18:29:14 +00001702
Paolo Bonzini9b0605f2018-01-30 11:05:06 -05001703 qemu_tcg_destroy_vcpu(cpu);
1704 cpu->created = false;
1705 qemu_cond_signal(&qemu_cpu_cond);
1706 qemu_mutex_unlock_iothread();
1707 rcu_unregister_thread();
Alex Bennée37257942017-02-23 18:29:14 +00001708 return NULL;
1709}
1710
Andreas Färber2ff09a42012-05-03 00:23:30 +02001711static void qemu_cpu_kick_thread(CPUState *cpu)
Paolo Bonzinicc015e92011-03-12 17:44:08 +01001712{
1713#ifndef _WIN32
1714 int err;
1715
Paolo Bonzinie0c38212015-08-26 00:19:19 +02001716 if (cpu->thread_kicked) {
1717 return;
Paolo Bonzini9102ded2015-08-18 06:52:09 -07001718 }
Paolo Bonzinie0c38212015-08-26 00:19:19 +02001719 cpu->thread_kicked = true;
Andreas Färber814e6122012-05-02 17:00:37 +02001720 err = pthread_kill(cpu->thread->thread, SIG_IPI);
Paolo Bonzinicc015e92011-03-12 17:44:08 +01001721 if (err) {
1722 fprintf(stderr, "qemu:%s: %s", __func__, strerror(err));
1723 exit(1);
1724 }
1725#else /* _WIN32 */
Vincent Palatinb0cb0a62017-01-10 11:59:57 +01001726 if (!qemu_cpu_is_self(cpu)) {
Justin Terry (VM)19306802018-01-22 13:07:49 -08001727 if (whpx_enabled()) {
1728 whpx_vcpu_kick(cpu);
1729 } else if (!QueueUserAPC(dummy_apc_func, cpu->hThread, 0)) {
Vincent Palatinb0cb0a62017-01-10 11:59:57 +01001730 fprintf(stderr, "%s: QueueUserAPC failed with error %lu\n",
1731 __func__, GetLastError());
1732 exit(1);
1733 }
1734 }
Paolo Bonzinicc015e92011-03-12 17:44:08 +01001735#endif
1736}
1737
Andreas Färberc08d7422012-05-03 04:34:15 +02001738void qemu_cpu_kick(CPUState *cpu)
Blue Swirl296af7c2010-03-29 19:23:50 +00001739{
Andreas Färberf5c121b2012-05-03 01:22:49 +02001740 qemu_cond_broadcast(cpu->halt_cond);
Paolo Bonzinie0c38212015-08-26 00:19:19 +02001741 if (tcg_enabled()) {
Alex Bennée791158d2017-02-23 18:29:10 +00001742 cpu_exit(cpu);
Alex Bennée37257942017-02-23 18:29:14 +00001743 /* NOP unless doing single-thread RR */
Alex Bennée791158d2017-02-23 18:29:10 +00001744 qemu_cpu_kick_rr_cpu();
Paolo Bonzinie0c38212015-08-26 00:19:19 +02001745 } else {
Vincent Palatinb0cb0a62017-01-10 11:59:57 +01001746 if (hax_enabled()) {
1747 /*
1748 * FIXME: race condition with the exit_request check in
1749 * hax_vcpu_hax_exec
1750 */
1751 cpu->exit_request = 1;
1752 }
Paolo Bonzinie0c38212015-08-26 00:19:19 +02001753 qemu_cpu_kick_thread(cpu);
1754 }
Blue Swirl296af7c2010-03-29 19:23:50 +00001755}
1756
Jan Kiszka46d62fa2011-02-01 22:15:59 +01001757void qemu_cpu_kick_self(void)
1758{
Andreas Färber4917cf42013-05-27 05:17:50 +02001759 assert(current_cpu);
Paolo Bonzini9102ded2015-08-18 06:52:09 -07001760 qemu_cpu_kick_thread(current_cpu);
Blue Swirl296af7c2010-03-29 19:23:50 +00001761}
1762
Andreas Färber60e82572012-05-02 22:23:49 +02001763bool qemu_cpu_is_self(CPUState *cpu)
Blue Swirl296af7c2010-03-29 19:23:50 +00001764{
Andreas Färber814e6122012-05-02 17:00:37 +02001765 return qemu_thread_is_self(cpu->thread);
Blue Swirl296af7c2010-03-29 19:23:50 +00001766}
1767
Paolo Bonzini79e2b9a2015-01-21 12:09:14 +01001768bool qemu_in_vcpu_thread(void)
Juan Quintelaaa723c22012-09-18 16:30:11 +02001769{
Andreas Färber4917cf42013-05-27 05:17:50 +02001770 return current_cpu && qemu_cpu_is_self(current_cpu);
Juan Quintelaaa723c22012-09-18 16:30:11 +02001771}
1772
Paolo Bonziniafbe7052015-06-18 18:47:19 +02001773static __thread bool iothread_locked = false;
1774
1775bool qemu_mutex_iothread_locked(void)
1776{
1777 return iothread_locked;
1778}
1779
Emilio G. Cotacb764d02017-10-28 02:16:41 -04001780/*
1781 * The BQL is taken from so many places that it is worth profiling the
1782 * callers directly, instead of funneling them all through a single function.
1783 */
1784void qemu_mutex_lock_iothread_impl(const char *file, int line)
Blue Swirl296af7c2010-03-29 19:23:50 +00001785{
Emilio G. Cotacb764d02017-10-28 02:16:41 -04001786 QemuMutexLockFunc bql_lock = atomic_read(&qemu_bql_mutex_lock_func);
1787
Jan Kiszka8d04fb52017-02-23 18:29:11 +00001788 g_assert(!qemu_mutex_iothread_locked());
Emilio G. Cotacb764d02017-10-28 02:16:41 -04001789 bql_lock(&qemu_global_mutex, file, line);
Paolo Bonziniafbe7052015-06-18 18:47:19 +02001790 iothread_locked = true;
Blue Swirl296af7c2010-03-29 19:23:50 +00001791}
1792
1793void qemu_mutex_unlock_iothread(void)
1794{
Jan Kiszka8d04fb52017-02-23 18:29:11 +00001795 g_assert(qemu_mutex_iothread_locked());
Paolo Bonziniafbe7052015-06-18 18:47:19 +02001796 iothread_locked = false;
Blue Swirl296af7c2010-03-29 19:23:50 +00001797 qemu_mutex_unlock(&qemu_global_mutex);
1798}
1799
Alex Bennéee8faee02016-10-27 16:09:58 +01001800static bool all_vcpus_paused(void)
Blue Swirl296af7c2010-03-29 19:23:50 +00001801{
Andreas Färberbdc44642013-06-24 23:50:24 +02001802 CPUState *cpu;
Blue Swirl296af7c2010-03-29 19:23:50 +00001803
Andreas Färberbdc44642013-06-24 23:50:24 +02001804 CPU_FOREACH(cpu) {
Andreas Färber182735e2013-05-29 22:29:20 +02001805 if (!cpu->stopped) {
Alex Bennéee8faee02016-10-27 16:09:58 +01001806 return false;
Jan Kiszka0ab07c62011-02-07 12:19:14 +01001807 }
Blue Swirl296af7c2010-03-29 19:23:50 +00001808 }
1809
Alex Bennéee8faee02016-10-27 16:09:58 +01001810 return true;
Blue Swirl296af7c2010-03-29 19:23:50 +00001811}
1812
1813void pause_all_vcpus(void)
1814{
Andreas Färberbdc44642013-06-24 23:50:24 +02001815 CPUState *cpu;
Blue Swirl296af7c2010-03-29 19:23:50 +00001816
Alex Bligh40daca52013-08-21 16:03:02 +01001817 qemu_clock_enable(QEMU_CLOCK_VIRTUAL, false);
Andreas Färberbdc44642013-06-24 23:50:24 +02001818 CPU_FOREACH(cpu) {
David Hildenbrandebd05fe2017-11-29 20:12:15 +01001819 if (qemu_cpu_is_self(cpu)) {
1820 qemu_cpu_stop(cpu, true);
1821 } else {
1822 cpu->stop = true;
1823 qemu_cpu_kick(cpu);
1824 }
Jan Kiszkad798e972012-02-17 18:31:16 +01001825 }
1826
Alex Bennéed759c952018-02-27 12:52:48 +03001827 /* We need to drop the replay_lock so any vCPU threads woken up
1828 * can finish their replay tasks
1829 */
1830 replay_mutex_unlock();
1831
Blue Swirl296af7c2010-03-29 19:23:50 +00001832 while (!all_vcpus_paused()) {
Paolo Bonzinibe7d6c52011-03-12 17:44:02 +01001833 qemu_cond_wait(&qemu_pause_cond, &qemu_global_mutex);
Andreas Färberbdc44642013-06-24 23:50:24 +02001834 CPU_FOREACH(cpu) {
Andreas Färber182735e2013-05-29 22:29:20 +02001835 qemu_cpu_kick(cpu);
Blue Swirl296af7c2010-03-29 19:23:50 +00001836 }
1837 }
Alex Bennéed759c952018-02-27 12:52:48 +03001838
1839 qemu_mutex_unlock_iothread();
1840 replay_mutex_lock();
1841 qemu_mutex_lock_iothread();
Blue Swirl296af7c2010-03-29 19:23:50 +00001842}
1843
Igor Mammedov29936832013-04-23 10:29:37 +02001844void cpu_resume(CPUState *cpu)
1845{
1846 cpu->stop = false;
1847 cpu->stopped = false;
1848 qemu_cpu_kick(cpu);
1849}
1850
Blue Swirl296af7c2010-03-29 19:23:50 +00001851void resume_all_vcpus(void)
1852{
Andreas Färberbdc44642013-06-24 23:50:24 +02001853 CPUState *cpu;
Blue Swirl296af7c2010-03-29 19:23:50 +00001854
Alex Bligh40daca52013-08-21 16:03:02 +01001855 qemu_clock_enable(QEMU_CLOCK_VIRTUAL, true);
Andreas Färberbdc44642013-06-24 23:50:24 +02001856 CPU_FOREACH(cpu) {
Andreas Färber182735e2013-05-29 22:29:20 +02001857 cpu_resume(cpu);
Blue Swirl296af7c2010-03-29 19:23:50 +00001858 }
1859}
1860
Paolo Bonzinidbadee42018-01-30 16:40:12 +01001861void cpu_remove_sync(CPUState *cpu)
Gu Zheng4c055ab2016-05-12 09:18:13 +05301862{
1863 cpu->stop = true;
1864 cpu->unplug = true;
1865 qemu_cpu_kick(cpu);
Paolo Bonzinidbadee42018-01-30 16:40:12 +01001866 qemu_mutex_unlock_iothread();
1867 qemu_thread_join(cpu->thread);
1868 qemu_mutex_lock_iothread();
Bharata B Rao2c579042016-05-12 09:18:14 +05301869}
1870
Dr. David Alan Gilbert49001162014-01-30 10:20:32 +00001871/* For temporary buffers for forming a name */
1872#define VCPU_THREAD_NAME_SIZE 16
1873
Andreas Färbere5ab30a2012-05-03 01:50:44 +02001874static void qemu_tcg_init_vcpu(CPUState *cpu)
Blue Swirl296af7c2010-03-29 19:23:50 +00001875{
Dr. David Alan Gilbert49001162014-01-30 10:20:32 +00001876 char thread_name[VCPU_THREAD_NAME_SIZE];
Alex Bennée37257942017-02-23 18:29:14 +00001877 static QemuCond *single_tcg_halt_cond;
1878 static QemuThread *single_tcg_cpu_thread;
Emilio G. Cotae8feb962017-07-07 19:24:20 -04001879 static int tcg_region_inited;
1880
Emilio G. Cotaf28d0df2018-06-22 13:45:31 -04001881 assert(tcg_enabled());
Emilio G. Cotae8feb962017-07-07 19:24:20 -04001882 /*
1883 * Initialize TCG regions--once. Now is a good time, because:
1884 * (1) TCG's init context, prologue and target globals have been set up.
1885 * (2) qemu_tcg_mttcg_enabled() works now (TCG init code runs before the
1886 * -accel flag is processed, so the check doesn't work then).
1887 */
1888 if (!tcg_region_inited) {
1889 tcg_region_inited = 1;
1890 tcg_region_init();
1891 }
Dr. David Alan Gilbert49001162014-01-30 10:20:32 +00001892
Alex Bennée37257942017-02-23 18:29:14 +00001893 if (qemu_tcg_mttcg_enabled() || !single_tcg_cpu_thread) {
Andreas Färber814e6122012-05-02 17:00:37 +02001894 cpu->thread = g_malloc0(sizeof(QemuThread));
Andreas Färberf5c121b2012-05-03 01:22:49 +02001895 cpu->halt_cond = g_malloc0(sizeof(QemuCond));
1896 qemu_cond_init(cpu->halt_cond);
Alex Bennée37257942017-02-23 18:29:14 +00001897
1898 if (qemu_tcg_mttcg_enabled()) {
1899 /* create a thread per vCPU with TCG (MTTCG) */
1900 parallel_cpus = true;
1901 snprintf(thread_name, VCPU_THREAD_NAME_SIZE, "CPU %d/TCG",
Dr. David Alan Gilbert49001162014-01-30 10:20:32 +00001902 cpu->cpu_index);
Alex Bennée37257942017-02-23 18:29:14 +00001903
1904 qemu_thread_create(cpu->thread, thread_name, qemu_tcg_cpu_thread_fn,
1905 cpu, QEMU_THREAD_JOINABLE);
1906
1907 } else {
1908 /* share a single thread for all cpus with TCG */
1909 snprintf(thread_name, VCPU_THREAD_NAME_SIZE, "ALL CPUs/TCG");
1910 qemu_thread_create(cpu->thread, thread_name,
1911 qemu_tcg_rr_cpu_thread_fn,
1912 cpu, QEMU_THREAD_JOINABLE);
1913
1914 single_tcg_halt_cond = cpu->halt_cond;
1915 single_tcg_cpu_thread = cpu->thread;
1916 }
Paolo Bonzini1ecf47b2011-12-13 13:43:52 +01001917#ifdef _WIN32
Andreas Färber814e6122012-05-02 17:00:37 +02001918 cpu->hThread = qemu_thread_get_handle(cpu->thread);
Paolo Bonzini1ecf47b2011-12-13 13:43:52 +01001919#endif
Blue Swirl296af7c2010-03-29 19:23:50 +00001920 } else {
Alex Bennée37257942017-02-23 18:29:14 +00001921 /* For non-MTTCG cases we share the thread */
1922 cpu->thread = single_tcg_cpu_thread;
1923 cpu->halt_cond = single_tcg_halt_cond;
David Hildenbranda3421732018-02-09 20:52:37 +01001924 cpu->thread_id = first_cpu->thread_id;
1925 cpu->can_do_io = 1;
1926 cpu->created = true;
Blue Swirl296af7c2010-03-29 19:23:50 +00001927 }
1928}
1929
Vincent Palatinb0cb0a62017-01-10 11:59:57 +01001930static void qemu_hax_start_vcpu(CPUState *cpu)
1931{
1932 char thread_name[VCPU_THREAD_NAME_SIZE];
1933
1934 cpu->thread = g_malloc0(sizeof(QemuThread));
1935 cpu->halt_cond = g_malloc0(sizeof(QemuCond));
1936 qemu_cond_init(cpu->halt_cond);
1937
1938 snprintf(thread_name, VCPU_THREAD_NAME_SIZE, "CPU %d/HAX",
1939 cpu->cpu_index);
1940 qemu_thread_create(cpu->thread, thread_name, qemu_hax_cpu_thread_fn,
1941 cpu, QEMU_THREAD_JOINABLE);
1942#ifdef _WIN32
1943 cpu->hThread = qemu_thread_get_handle(cpu->thread);
1944#endif
Vincent Palatinb0cb0a62017-01-10 11:59:57 +01001945}
1946
Andreas Färber48a106b2013-05-27 02:20:39 +02001947static void qemu_kvm_start_vcpu(CPUState *cpu)
Blue Swirl296af7c2010-03-29 19:23:50 +00001948{
Dr. David Alan Gilbert49001162014-01-30 10:20:32 +00001949 char thread_name[VCPU_THREAD_NAME_SIZE];
1950
Andreas Färber814e6122012-05-02 17:00:37 +02001951 cpu->thread = g_malloc0(sizeof(QemuThread));
Andreas Färberf5c121b2012-05-03 01:22:49 +02001952 cpu->halt_cond = g_malloc0(sizeof(QemuCond));
1953 qemu_cond_init(cpu->halt_cond);
Dr. David Alan Gilbert49001162014-01-30 10:20:32 +00001954 snprintf(thread_name, VCPU_THREAD_NAME_SIZE, "CPU %d/KVM",
1955 cpu->cpu_index);
1956 qemu_thread_create(cpu->thread, thread_name, qemu_kvm_cpu_thread_fn,
1957 cpu, QEMU_THREAD_JOINABLE);
Blue Swirl296af7c2010-03-29 19:23:50 +00001958}
1959
Sergio Andres Gomez Del Realc97d6d22017-09-13 04:05:09 -05001960static void qemu_hvf_start_vcpu(CPUState *cpu)
1961{
1962 char thread_name[VCPU_THREAD_NAME_SIZE];
1963
1964 /* HVF currently does not support TCG, and only runs in
1965 * unrestricted-guest mode. */
1966 assert(hvf_enabled());
1967
1968 cpu->thread = g_malloc0(sizeof(QemuThread));
1969 cpu->halt_cond = g_malloc0(sizeof(QemuCond));
1970 qemu_cond_init(cpu->halt_cond);
1971
1972 snprintf(thread_name, VCPU_THREAD_NAME_SIZE, "CPU %d/HVF",
1973 cpu->cpu_index);
1974 qemu_thread_create(cpu->thread, thread_name, qemu_hvf_cpu_thread_fn,
1975 cpu, QEMU_THREAD_JOINABLE);
Sergio Andres Gomez Del Realc97d6d22017-09-13 04:05:09 -05001976}
1977
Justin Terry (VM)19306802018-01-22 13:07:49 -08001978static void qemu_whpx_start_vcpu(CPUState *cpu)
1979{
1980 char thread_name[VCPU_THREAD_NAME_SIZE];
1981
1982 cpu->thread = g_malloc0(sizeof(QemuThread));
1983 cpu->halt_cond = g_malloc0(sizeof(QemuCond));
1984 qemu_cond_init(cpu->halt_cond);
1985 snprintf(thread_name, VCPU_THREAD_NAME_SIZE, "CPU %d/WHPX",
1986 cpu->cpu_index);
1987 qemu_thread_create(cpu->thread, thread_name, qemu_whpx_cpu_thread_fn,
1988 cpu, QEMU_THREAD_JOINABLE);
1989#ifdef _WIN32
1990 cpu->hThread = qemu_thread_get_handle(cpu->thread);
1991#endif
Justin Terry (VM)19306802018-01-22 13:07:49 -08001992}
1993
Andreas Färber10a90212013-05-27 02:24:35 +02001994static void qemu_dummy_start_vcpu(CPUState *cpu)
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +02001995{
Dr. David Alan Gilbert49001162014-01-30 10:20:32 +00001996 char thread_name[VCPU_THREAD_NAME_SIZE];
1997
Andreas Färber814e6122012-05-02 17:00:37 +02001998 cpu->thread = g_malloc0(sizeof(QemuThread));
Andreas Färberf5c121b2012-05-03 01:22:49 +02001999 cpu->halt_cond = g_malloc0(sizeof(QemuCond));
2000 qemu_cond_init(cpu->halt_cond);
Dr. David Alan Gilbert49001162014-01-30 10:20:32 +00002001 snprintf(thread_name, VCPU_THREAD_NAME_SIZE, "CPU %d/DUMMY",
2002 cpu->cpu_index);
2003 qemu_thread_create(cpu->thread, thread_name, qemu_dummy_cpu_thread_fn, cpu,
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +02002004 QEMU_THREAD_JOINABLE);
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +02002005}
2006
Andreas Färberc643bed2013-05-27 03:23:24 +02002007void qemu_init_vcpu(CPUState *cpu)
Blue Swirl296af7c2010-03-29 19:23:50 +00002008{
Andreas Färberce3960e2012-12-17 03:27:07 +01002009 cpu->nr_cores = smp_cores;
2010 cpu->nr_threads = smp_threads;
Andreas Färberf324e762012-05-02 23:26:21 +02002011 cpu->stopped = true;
Peter Maydell56943e82016-01-21 14:15:04 +00002012
2013 if (!cpu->as) {
2014 /* If the target cpu hasn't set up any address spaces itself,
2015 * give it the default one.
2016 */
Peter Maydell12ebc9a2016-01-21 14:15:04 +00002017 cpu->num_ases = 1;
Peter Xu80ceb072017-11-23 17:23:32 +08002018 cpu_address_space_init(cpu, 0, "cpu-memory", cpu->memory);
Peter Maydell56943e82016-01-21 14:15:04 +00002019 }
2020
Jan Kiszka0ab07c62011-02-07 12:19:14 +01002021 if (kvm_enabled()) {
Andreas Färber48a106b2013-05-27 02:20:39 +02002022 qemu_kvm_start_vcpu(cpu);
Vincent Palatinb0cb0a62017-01-10 11:59:57 +01002023 } else if (hax_enabled()) {
2024 qemu_hax_start_vcpu(cpu);
Sergio Andres Gomez Del Realc97d6d22017-09-13 04:05:09 -05002025 } else if (hvf_enabled()) {
2026 qemu_hvf_start_vcpu(cpu);
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +02002027 } else if (tcg_enabled()) {
Andreas Färbere5ab30a2012-05-03 01:50:44 +02002028 qemu_tcg_init_vcpu(cpu);
Justin Terry (VM)19306802018-01-22 13:07:49 -08002029 } else if (whpx_enabled()) {
2030 qemu_whpx_start_vcpu(cpu);
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +02002031 } else {
Andreas Färber10a90212013-05-27 02:24:35 +02002032 qemu_dummy_start_vcpu(cpu);
Jan Kiszka0ab07c62011-02-07 12:19:14 +01002033 }
David Hildenbrand81e96312018-02-09 20:52:38 +01002034
2035 while (!cpu->created) {
2036 qemu_cond_wait(&qemu_cpu_cond, &qemu_global_mutex);
2037 }
Blue Swirl296af7c2010-03-29 19:23:50 +00002038}
2039
Jan Kiszkab4a3d962011-02-01 22:15:43 +01002040void cpu_stop_current(void)
Blue Swirl296af7c2010-03-29 19:23:50 +00002041{
Andreas Färber4917cf42013-05-27 05:17:50 +02002042 if (current_cpu) {
David Hildenbrandebd05fe2017-11-29 20:12:15 +01002043 qemu_cpu_stop(current_cpu, true);
Jan Kiszkab4a3d962011-02-01 22:15:43 +01002044 }
Blue Swirl296af7c2010-03-29 19:23:50 +00002045}
2046
Kevin Wolf56983462013-07-05 13:49:54 +02002047int vm_stop(RunState state)
Blue Swirl296af7c2010-03-29 19:23:50 +00002048{
Juan Quintelaaa723c22012-09-18 16:30:11 +02002049 if (qemu_in_vcpu_thread()) {
Paolo Bonzini74892d22014-06-05 14:53:58 +02002050 qemu_system_vmstop_request_prepare();
Luiz Capitulino1dfb4dd2011-07-29 14:26:33 -03002051 qemu_system_vmstop_request(state);
Blue Swirl296af7c2010-03-29 19:23:50 +00002052 /*
2053 * FIXME: should not return to device code in case
2054 * vm_stop() has been requested.
2055 */
Jan Kiszkab4a3d962011-02-01 22:15:43 +01002056 cpu_stop_current();
Kevin Wolf56983462013-07-05 13:49:54 +02002057 return 0;
Blue Swirl296af7c2010-03-29 19:23:50 +00002058 }
Kevin Wolf56983462013-07-05 13:49:54 +02002059
Stefan Hajnoczi4486e892018-03-07 14:42:05 +00002060 return do_vm_stop(state, true);
Blue Swirl296af7c2010-03-29 19:23:50 +00002061}
2062
Claudio Imbrenda2d76e822017-02-14 18:07:47 +01002063/**
2064 * Prepare for (re)starting the VM.
2065 * Returns -1 if the vCPUs are not to be restarted (e.g. if they are already
2066 * running or in case of an error condition), 0 otherwise.
2067 */
2068int vm_prepare_start(void)
2069{
2070 RunState requested;
Claudio Imbrenda2d76e822017-02-14 18:07:47 +01002071
2072 qemu_vmstop_requested(&requested);
2073 if (runstate_is_running() && requested == RUN_STATE__MAX) {
2074 return -1;
2075 }
2076
2077 /* Ensure that a STOP/RESUME pair of events is emitted if a
2078 * vmstop request was pending. The BLOCK_IO_ERROR event, for
2079 * example, according to documentation is always followed by
2080 * the STOP event.
2081 */
2082 if (runstate_is_running()) {
2083 qapi_event_send_stop(&error_abort);
Markus Armbrusterf0561582018-04-23 10:45:18 +02002084 qapi_event_send_resume(&error_abort);
2085 return -1;
Claudio Imbrenda2d76e822017-02-14 18:07:47 +01002086 }
2087
2088 /* We are sending this now, but the CPUs will be resumed shortly later */
2089 qapi_event_send_resume(&error_abort);
Markus Armbrusterf0561582018-04-23 10:45:18 +02002090
2091 replay_enable_events();
2092 cpu_enable_ticks();
2093 runstate_set(RUN_STATE_RUNNING);
2094 vm_state_notify(1, RUN_STATE_RUNNING);
2095 return 0;
Claudio Imbrenda2d76e822017-02-14 18:07:47 +01002096}
2097
2098void vm_start(void)
2099{
2100 if (!vm_prepare_start()) {
2101 resume_all_vcpus();
2102 }
2103}
2104
Luiz Capitulino8a9236f2011-10-14 11:18:09 -03002105/* does a state transition even if the VM is already stopped,
2106 current state is forgotten forever */
Kevin Wolf56983462013-07-05 13:49:54 +02002107int vm_stop_force_state(RunState state)
Luiz Capitulino8a9236f2011-10-14 11:18:09 -03002108{
2109 if (runstate_is_running()) {
Kevin Wolf56983462013-07-05 13:49:54 +02002110 return vm_stop(state);
Luiz Capitulino8a9236f2011-10-14 11:18:09 -03002111 } else {
2112 runstate_set(state);
Wen Congyangb2780d32015-11-20 17:34:38 +08002113
2114 bdrv_drain_all();
Kevin Wolf594a45c2013-07-18 14:52:19 +02002115 /* Make sure to return an error if the flush in a previous vm_stop()
2116 * failed. */
John Snow22af08e2016-09-22 21:45:51 -04002117 return bdrv_flush_all();
Luiz Capitulino8a9236f2011-10-14 11:18:09 -03002118 }
2119}
2120
Stefan Weil9a78eea2010-10-22 23:03:33 +02002121void list_cpus(FILE *f, fprintf_function cpu_fprintf, const char *optarg)
Blue Swirl262353c2010-05-04 19:55:35 +00002122{
2123 /* XXX: implement xxx_cpu_list for targets that still miss it */
Peter Maydelle916cbf2012-09-05 17:41:08 -03002124#if defined(cpu_list)
2125 cpu_list(f, cpu_fprintf);
Blue Swirl262353c2010-05-04 19:55:35 +00002126#endif
2127}
Luiz Capitulinode0b36b2011-09-21 16:38:35 -03002128
2129CpuInfoList *qmp_query_cpus(Error **errp)
2130{
Igor Mammedovafed5a52017-05-10 13:29:55 +02002131 MachineState *ms = MACHINE(qdev_get_machine());
2132 MachineClass *mc = MACHINE_GET_CLASS(ms);
Luiz Capitulinode0b36b2011-09-21 16:38:35 -03002133 CpuInfoList *head = NULL, *cur_item = NULL;
Andreas Färber182735e2013-05-29 22:29:20 +02002134 CPUState *cpu;
Luiz Capitulinode0b36b2011-09-21 16:38:35 -03002135
Andreas Färberbdc44642013-06-24 23:50:24 +02002136 CPU_FOREACH(cpu) {
Luiz Capitulinode0b36b2011-09-21 16:38:35 -03002137 CpuInfoList *info;
Andreas Färber182735e2013-05-29 22:29:20 +02002138#if defined(TARGET_I386)
2139 X86CPU *x86_cpu = X86_CPU(cpu);
2140 CPUX86State *env = &x86_cpu->env;
2141#elif defined(TARGET_PPC)
2142 PowerPCCPU *ppc_cpu = POWERPC_CPU(cpu);
2143 CPUPPCState *env = &ppc_cpu->env;
2144#elif defined(TARGET_SPARC)
2145 SPARCCPU *sparc_cpu = SPARC_CPU(cpu);
2146 CPUSPARCState *env = &sparc_cpu->env;
Michael Clark25fa1942018-03-03 01:32:59 +13002147#elif defined(TARGET_RISCV)
2148 RISCVCPU *riscv_cpu = RISCV_CPU(cpu);
2149 CPURISCVState *env = &riscv_cpu->env;
Andreas Färber182735e2013-05-29 22:29:20 +02002150#elif defined(TARGET_MIPS)
2151 MIPSCPU *mips_cpu = MIPS_CPU(cpu);
2152 CPUMIPSState *env = &mips_cpu->env;
Bastian Koppelmann48e06fe2014-09-01 12:59:46 +01002153#elif defined(TARGET_TRICORE)
2154 TriCoreCPU *tricore_cpu = TRICORE_CPU(cpu);
2155 CPUTriCoreState *env = &tricore_cpu->env;
Viktor Mihajlovski9d0306d2018-02-16 17:08:37 +01002156#elif defined(TARGET_S390X)
2157 S390CPU *s390_cpu = S390_CPU(cpu);
2158 CPUS390XState *env = &s390_cpu->env;
Andreas Färber182735e2013-05-29 22:29:20 +02002159#endif
Luiz Capitulinode0b36b2011-09-21 16:38:35 -03002160
Andreas Färbercb446ec2013-05-01 14:24:52 +02002161 cpu_synchronize_state(cpu);
Luiz Capitulinode0b36b2011-09-21 16:38:35 -03002162
2163 info = g_malloc0(sizeof(*info));
2164 info->value = g_malloc0(sizeof(*info->value));
Andreas Färber55e5c282012-12-17 06:18:02 +01002165 info->value->CPU = cpu->cpu_index;
Andreas Färber182735e2013-05-29 22:29:20 +02002166 info->value->current = (cpu == first_cpu);
Andreas Färber259186a2013-01-17 18:51:17 +01002167 info->value->halted = cpu->halted;
Eduardo Habkost58f88d42015-05-08 16:04:22 -03002168 info->value->qom_path = object_get_canonical_path(OBJECT(cpu));
Andreas Färber9f09e182012-05-03 06:59:07 +02002169 info->value->thread_id = cpu->thread_id;
Luiz Capitulinode0b36b2011-09-21 16:38:35 -03002170#if defined(TARGET_I386)
Eric Blake86f4b682015-11-18 01:52:59 -07002171 info->value->arch = CPU_INFO_ARCH_X86;
Eric Blake544a3732016-02-17 23:48:27 -07002172 info->value->u.x86.pc = env->eip + env->segs[R_CS].base;
Luiz Capitulinode0b36b2011-09-21 16:38:35 -03002173#elif defined(TARGET_PPC)
Eric Blake86f4b682015-11-18 01:52:59 -07002174 info->value->arch = CPU_INFO_ARCH_PPC;
Eric Blake544a3732016-02-17 23:48:27 -07002175 info->value->u.ppc.nip = env->nip;
Luiz Capitulinode0b36b2011-09-21 16:38:35 -03002176#elif defined(TARGET_SPARC)
Eric Blake86f4b682015-11-18 01:52:59 -07002177 info->value->arch = CPU_INFO_ARCH_SPARC;
Eric Blake544a3732016-02-17 23:48:27 -07002178 info->value->u.q_sparc.pc = env->pc;
2179 info->value->u.q_sparc.npc = env->npc;
Luiz Capitulinode0b36b2011-09-21 16:38:35 -03002180#elif defined(TARGET_MIPS)
Eric Blake86f4b682015-11-18 01:52:59 -07002181 info->value->arch = CPU_INFO_ARCH_MIPS;
Eric Blake544a3732016-02-17 23:48:27 -07002182 info->value->u.q_mips.PC = env->active_tc.PC;
Bastian Koppelmann48e06fe2014-09-01 12:59:46 +01002183#elif defined(TARGET_TRICORE)
Eric Blake86f4b682015-11-18 01:52:59 -07002184 info->value->arch = CPU_INFO_ARCH_TRICORE;
Eric Blake544a3732016-02-17 23:48:27 -07002185 info->value->u.tricore.PC = env->PC;
Viktor Mihajlovski9d0306d2018-02-16 17:08:37 +01002186#elif defined(TARGET_S390X)
2187 info->value->arch = CPU_INFO_ARCH_S390;
2188 info->value->u.s390.cpu_state = env->cpu_state;
Michael Clark25fa1942018-03-03 01:32:59 +13002189#elif defined(TARGET_RISCV)
2190 info->value->arch = CPU_INFO_ARCH_RISCV;
2191 info->value->u.riscv.pc = env->pc;
Eric Blake86f4b682015-11-18 01:52:59 -07002192#else
2193 info->value->arch = CPU_INFO_ARCH_OTHER;
Luiz Capitulinode0b36b2011-09-21 16:38:35 -03002194#endif
Igor Mammedovafed5a52017-05-10 13:29:55 +02002195 info->value->has_props = !!mc->cpu_index_to_instance_props;
2196 if (info->value->has_props) {
2197 CpuInstanceProperties *props;
2198 props = g_malloc0(sizeof(*props));
2199 *props = mc->cpu_index_to_instance_props(ms, cpu->cpu_index);
2200 info->value->props = props;
2201 }
Luiz Capitulinode0b36b2011-09-21 16:38:35 -03002202
2203 /* XXX: waiting for the qapi to support GSList */
2204 if (!cur_item) {
2205 head = cur_item = info;
2206 } else {
2207 cur_item->next = info;
2208 cur_item = info;
2209 }
2210 }
2211
2212 return head;
2213}
Luiz Capitulino0cfd6a92011-11-22 16:32:37 -02002214
Laszlo Ersekdaa9d2b2018-04-27 21:28:51 +02002215static CpuInfoArch sysemu_target_to_cpuinfo_arch(SysEmuTarget target)
2216{
2217 /*
2218 * The @SysEmuTarget -> @CpuInfoArch mapping below is based on the
2219 * TARGET_ARCH -> TARGET_BASE_ARCH mapping in the "configure" script.
2220 */
2221 switch (target) {
2222 case SYS_EMU_TARGET_I386:
2223 case SYS_EMU_TARGET_X86_64:
2224 return CPU_INFO_ARCH_X86;
2225
2226 case SYS_EMU_TARGET_PPC:
2227 case SYS_EMU_TARGET_PPCEMB:
2228 case SYS_EMU_TARGET_PPC64:
2229 return CPU_INFO_ARCH_PPC;
2230
2231 case SYS_EMU_TARGET_SPARC:
2232 case SYS_EMU_TARGET_SPARC64:
2233 return CPU_INFO_ARCH_SPARC;
2234
2235 case SYS_EMU_TARGET_MIPS:
2236 case SYS_EMU_TARGET_MIPSEL:
2237 case SYS_EMU_TARGET_MIPS64:
2238 case SYS_EMU_TARGET_MIPS64EL:
2239 return CPU_INFO_ARCH_MIPS;
2240
2241 case SYS_EMU_TARGET_TRICORE:
2242 return CPU_INFO_ARCH_TRICORE;
2243
2244 case SYS_EMU_TARGET_S390X:
2245 return CPU_INFO_ARCH_S390;
2246
2247 case SYS_EMU_TARGET_RISCV32:
2248 case SYS_EMU_TARGET_RISCV64:
2249 return CPU_INFO_ARCH_RISCV;
2250
2251 default:
2252 return CPU_INFO_ARCH_OTHER;
2253 }
2254}
2255
2256static void cpustate_to_cpuinfo_s390(CpuInfoS390 *info, const CPUState *cpu)
2257{
2258#ifdef TARGET_S390X
2259 S390CPU *s390_cpu = S390_CPU(cpu);
2260 CPUS390XState *env = &s390_cpu->env;
2261
2262 info->cpu_state = env->cpu_state;
2263#else
2264 abort();
2265#endif
2266}
2267
Luiz Capitulinoce74ee32018-02-16 17:08:38 +01002268/*
2269 * fast means: we NEVER interrupt vCPU threads to retrieve
2270 * information from KVM.
2271 */
2272CpuInfoFastList *qmp_query_cpus_fast(Error **errp)
2273{
2274 MachineState *ms = MACHINE(qdev_get_machine());
2275 MachineClass *mc = MACHINE_GET_CLASS(ms);
2276 CpuInfoFastList *head = NULL, *cur_item = NULL;
Laszlo Ersekdaa9d2b2018-04-27 21:28:51 +02002277 SysEmuTarget target = qapi_enum_parse(&SysEmuTarget_lookup, TARGET_NAME,
2278 -1, &error_abort);
Luiz Capitulinoce74ee32018-02-16 17:08:38 +01002279 CPUState *cpu;
2280
2281 CPU_FOREACH(cpu) {
2282 CpuInfoFastList *info = g_malloc0(sizeof(*info));
2283 info->value = g_malloc0(sizeof(*info->value));
2284
2285 info->value->cpu_index = cpu->cpu_index;
2286 info->value->qom_path = object_get_canonical_path(OBJECT(cpu));
2287 info->value->thread_id = cpu->thread_id;
2288
2289 info->value->has_props = !!mc->cpu_index_to_instance_props;
2290 if (info->value->has_props) {
2291 CpuInstanceProperties *props;
2292 props = g_malloc0(sizeof(*props));
2293 *props = mc->cpu_index_to_instance_props(ms, cpu->cpu_index);
2294 info->value->props = props;
2295 }
2296
Laszlo Ersekdaa9d2b2018-04-27 21:28:51 +02002297 info->value->arch = sysemu_target_to_cpuinfo_arch(target);
2298 info->value->target = target;
2299 if (target == SYS_EMU_TARGET_S390X) {
2300 cpustate_to_cpuinfo_s390(&info->value->u.s390x, cpu);
Laszlo Ersekdaa9d2b2018-04-27 21:28:51 +02002301 }
2302
Luiz Capitulinoce74ee32018-02-16 17:08:38 +01002303 if (!cur_item) {
2304 head = cur_item = info;
2305 } else {
2306 cur_item->next = info;
2307 cur_item = info;
2308 }
2309 }
2310
2311 return head;
2312}
2313
Luiz Capitulino0cfd6a92011-11-22 16:32:37 -02002314void qmp_memsave(int64_t addr, int64_t size, const char *filename,
2315 bool has_cpu, int64_t cpu_index, Error **errp)
2316{
2317 FILE *f;
2318 uint32_t l;
Andreas Färber55e5c282012-12-17 06:18:02 +01002319 CPUState *cpu;
Luiz Capitulino0cfd6a92011-11-22 16:32:37 -02002320 uint8_t buf[1024];
Borislav Petkov0dc9daf2015-02-08 13:14:38 +01002321 int64_t orig_addr = addr, orig_size = size;
Luiz Capitulino0cfd6a92011-11-22 16:32:37 -02002322
2323 if (!has_cpu) {
2324 cpu_index = 0;
2325 }
2326
Andreas Färber151d1322013-02-15 15:41:49 +01002327 cpu = qemu_get_cpu(cpu_index);
2328 if (cpu == NULL) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01002329 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "cpu-index",
2330 "a CPU number");
Luiz Capitulino0cfd6a92011-11-22 16:32:37 -02002331 return;
2332 }
2333
2334 f = fopen(filename, "wb");
2335 if (!f) {
Luiz Capitulino618da852013-06-07 14:35:06 -04002336 error_setg_file_open(errp, errno, filename);
Luiz Capitulino0cfd6a92011-11-22 16:32:37 -02002337 return;
2338 }
2339
2340 while (size != 0) {
2341 l = sizeof(buf);
2342 if (l > size)
2343 l = size;
Aneesh Kumar K.V2f4d0f52013-10-01 21:49:30 +05302344 if (cpu_memory_rw_debug(cpu, addr, buf, l, 0) != 0) {
Borislav Petkov0dc9daf2015-02-08 13:14:38 +01002345 error_setg(errp, "Invalid addr 0x%016" PRIx64 "/size %" PRId64
2346 " specified", orig_addr, orig_size);
Aneesh Kumar K.V2f4d0f52013-10-01 21:49:30 +05302347 goto exit;
2348 }
Luiz Capitulino0cfd6a92011-11-22 16:32:37 -02002349 if (fwrite(buf, 1, l, f) != l) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01002350 error_setg(errp, QERR_IO_ERROR);
Luiz Capitulino0cfd6a92011-11-22 16:32:37 -02002351 goto exit;
2352 }
2353 addr += l;
2354 size -= l;
2355 }
2356
2357exit:
2358 fclose(f);
2359}
Luiz Capitulino6d3962b2011-11-22 17:26:46 -02002360
2361void qmp_pmemsave(int64_t addr, int64_t size, const char *filename,
2362 Error **errp)
2363{
2364 FILE *f;
2365 uint32_t l;
2366 uint8_t buf[1024];
2367
2368 f = fopen(filename, "wb");
2369 if (!f) {
Luiz Capitulino618da852013-06-07 14:35:06 -04002370 error_setg_file_open(errp, errno, filename);
Luiz Capitulino6d3962b2011-11-22 17:26:46 -02002371 return;
2372 }
2373
2374 while (size != 0) {
2375 l = sizeof(buf);
2376 if (l > size)
2377 l = size;
Stefan Weileb6282f2014-04-07 20:28:23 +02002378 cpu_physical_memory_read(addr, buf, l);
Luiz Capitulino6d3962b2011-11-22 17:26:46 -02002379 if (fwrite(buf, 1, l, f) != l) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01002380 error_setg(errp, QERR_IO_ERROR);
Luiz Capitulino6d3962b2011-11-22 17:26:46 -02002381 goto exit;
2382 }
2383 addr += l;
2384 size -= l;
2385 }
2386
2387exit:
2388 fclose(f);
2389}
Luiz Capitulinoab49ab52011-11-23 12:55:53 -02002390
2391void qmp_inject_nmi(Error **errp)
2392{
Alexey Kardashevskiy9cb805f2014-08-20 22:16:33 +10002393 nmi_monitor_handle(monitor_get_cpu_index(), errp);
Luiz Capitulinoab49ab52011-11-23 12:55:53 -02002394}
Sebastian Tanase27498be2014-07-25 11:56:33 +02002395
2396void dump_drift_info(FILE *f, fprintf_function cpu_fprintf)
2397{
2398 if (!use_icount) {
2399 return;
2400 }
2401
2402 cpu_fprintf(f, "Host - Guest clock %"PRIi64" ms\n",
2403 (cpu_get_clock() - cpu_get_icount())/SCALE_MS);
2404 if (icount_align_option) {
2405 cpu_fprintf(f, "Max guest delay %"PRIi64" ms\n", -max_delay/SCALE_MS);
2406 cpu_fprintf(f, "Max guest advance %"PRIi64" ms\n", max_advance/SCALE_MS);
2407 } else {
2408 cpu_fprintf(f, "Max guest delay NA\n");
2409 cpu_fprintf(f, "Max guest advance NA\n");
2410 }
2411}