blob: be2d655f371c55b03bf88f0ca7ac0f0c0501c0a9 [file] [log] [blame]
Blue Swirl296af7c2010-03-29 19:23:50 +00001/*
2 * QEMU System Emulator
3 *
4 * Copyright (c) 2003-2008 Fabrice Bellard
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
23 */
24
Peter Maydell7b31bbc2016-01-26 18:16:56 +000025#include "qemu/osdep.h"
Markus Armbrustera8d25322019-05-23 16:35:08 +020026#include "qemu-common.h"
KONRAD Frederic8d4e9142017-02-23 18:29:08 +000027#include "qemu/config-file.h"
Markus Armbrusterd6454272019-08-12 07:23:45 +020028#include "migration/vmstate.h"
Paolo Bonzini83c90892012-12-17 18:19:49 +010029#include "monitor/monitor.h"
Markus Armbrustere688df62018-02-01 12:18:31 +010030#include "qapi/error.h"
Markus Armbruster112ed242018-02-26 17:13:27 -060031#include "qapi/qapi-commands-misc.h"
Markus Armbruster9af23982018-02-11 10:36:01 +010032#include "qapi/qapi-events-run-state.h"
Wenchao Xiaa4e15de2014-06-18 08:43:36 +020033#include "qapi/qmp/qerror.h"
Markus Armbrusterd49b6832015-03-17 18:29:20 +010034#include "qemu/error-report.h"
Markus Armbruster76c86612019-04-17 21:17:53 +020035#include "qemu/qemu-print.h"
Markus Armbruster14a48c12019-05-23 16:35:05 +020036#include "sysemu/tcg.h"
Max Reitzda31d592016-03-16 19:54:32 +010037#include "sysemu/block-backend.h"
Paolo Bonzini022c62c2012-12-17 18:19:49 +010038#include "exec/gdbstub.h"
Paolo Bonzini9c17d612012-12-17 18:20:04 +010039#include "sysemu/dma.h"
Vincent Palatinb3946622017-01-10 11:59:55 +010040#include "sysemu/hw_accel.h"
Paolo Bonzini9c17d612012-12-17 18:20:04 +010041#include "sysemu/kvm.h"
Vincent Palatinb0cb0a62017-01-10 11:59:57 +010042#include "sysemu/hax.h"
Sergio Andres Gomez Del Realc97d6d22017-09-13 04:05:09 -050043#include "sysemu/hvf.h"
Justin Terry (VM)19306802018-01-22 13:07:49 -080044#include "sysemu/whpx.h"
Paolo Bonzini63c91552016-03-15 13:18:37 +010045#include "exec/exec-all.h"
Blue Swirl296af7c2010-03-29 19:23:50 +000046
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010047#include "qemu/thread.h"
Emilio G. Cota30865f32018-10-21 13:30:35 -040048#include "qemu/plugin.h"
Paolo Bonzini9c17d612012-12-17 18:20:04 +010049#include "sysemu/cpus.h"
50#include "sysemu/qtest.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010051#include "qemu/main-loop.h"
Markus Armbruster922a01a2018-02-01 12:18:46 +010052#include "qemu/option.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010053#include "qemu/bitmap.h"
Liu Ping Fancb365642013-09-25 14:20:58 +080054#include "qemu/seqlock.h"
Richard Henderson9c09a252019-03-14 13:06:29 -070055#include "qemu/guest-random.h"
KONRAD Frederic8d4e9142017-02-23 18:29:08 +000056#include "tcg.h"
Alexey Kardashevskiy9cb805f2014-08-20 22:16:33 +100057#include "hw/nmi.h"
Pavel Dovgalyuk8b427042015-09-17 19:24:05 +030058#include "sysemu/replay.h"
Markus Armbruster54d31232019-08-12 07:23:59 +020059#include "sysemu/runstate.h"
Like Xu5cc87672019-05-19 04:54:21 +080060#include "hw/boards.h"
Markus Armbruster650d1032019-08-12 07:23:48 +020061#include "hw/hw.h"
Jan Kiszka0ff0fc12011-06-23 10:15:55 +020062
Jan Kiszka6d9cb732011-02-01 22:15:58 +010063#ifdef CONFIG_LINUX
64
65#include <sys/prctl.h>
66
Marcelo Tosattic0532a72010-10-11 15:31:21 -030067#ifndef PR_MCE_KILL
68#define PR_MCE_KILL 33
69#endif
70
Jan Kiszka6d9cb732011-02-01 22:15:58 +010071#ifndef PR_MCE_KILL_SET
72#define PR_MCE_KILL_SET 1
73#endif
74
75#ifndef PR_MCE_KILL_EARLY
76#define PR_MCE_KILL_EARLY 1
77#endif
78
79#endif /* CONFIG_LINUX */
80
Yury Kotovbd1f7ff2019-09-09 16:13:34 +030081static QemuMutex qemu_global_mutex;
82
Sebastian Tanase27498be2014-07-25 11:56:33 +020083int64_t max_delay;
84int64_t max_advance;
Blue Swirl296af7c2010-03-29 19:23:50 +000085
Jason J. Herne2adcc852015-09-08 13:12:33 -040086/* vcpu throttling controls */
87static QEMUTimer *throttle_timer;
88static unsigned int throttle_percentage;
89
90#define CPU_THROTTLE_PCT_MIN 1
91#define CPU_THROTTLE_PCT_MAX 99
92#define CPU_THROTTLE_TIMESLICE_NS 10000000
93
Tiejun Chen321bc0b2013-08-02 09:43:09 +080094bool cpu_is_stopped(CPUState *cpu)
95{
96 return cpu->stopped || !runstate_is_running();
97}
98
Andreas Färbera98ae1d2013-05-26 23:21:08 +020099static bool cpu_thread_is_idle(CPUState *cpu)
Peter Maydellac873f12012-07-19 16:52:27 +0100100{
Andreas Färberc64ca812012-05-03 02:11:45 +0200101 if (cpu->stop || cpu->queued_work_first) {
Peter Maydellac873f12012-07-19 16:52:27 +0100102 return false;
103 }
Tiejun Chen321bc0b2013-08-02 09:43:09 +0800104 if (cpu_is_stopped(cpu)) {
Peter Maydellac873f12012-07-19 16:52:27 +0100105 return true;
106 }
Andreas Färber8c2e1b02013-08-25 18:53:55 +0200107 if (!cpu->halted || cpu_has_work(cpu) ||
Alexander Graf215e79c2013-04-24 22:24:12 +0200108 kvm_halt_in_kernel()) {
Peter Maydellac873f12012-07-19 16:52:27 +0100109 return false;
110 }
111 return true;
112}
113
114static bool all_cpu_threads_idle(void)
115{
Andreas Färber182735e2013-05-29 22:29:20 +0200116 CPUState *cpu;
Peter Maydellac873f12012-07-19 16:52:27 +0100117
Andreas Färberbdc44642013-06-24 23:50:24 +0200118 CPU_FOREACH(cpu) {
Andreas Färber182735e2013-05-29 22:29:20 +0200119 if (!cpu_thread_is_idle(cpu)) {
Peter Maydellac873f12012-07-19 16:52:27 +0100120 return false;
121 }
122 }
123 return true;
124}
125
Blue Swirl296af7c2010-03-29 19:23:50 +0000126/***********************************************************/
Paolo Bonzini946fb272011-09-12 13:57:37 +0200127/* guest cycle counter */
128
Paolo Bonzinia3270e12013-10-07 17:18:15 +0200129/* Protected by TimersState seqlock */
130
Victor CLEMENT5045e9d92015-05-29 17:14:04 +0200131static bool icount_sleep = true;
Paolo Bonzini946fb272011-09-12 13:57:37 +0200132/* Arbitrarily pick 1MIPS as the minimum allowable speed. */
133#define MAX_ICOUNT_SHIFT 10
Paolo Bonzinia3270e12013-10-07 17:18:15 +0200134
Paolo Bonzini946fb272011-09-12 13:57:37 +0200135typedef struct TimersState {
Liu Ping Fancb365642013-09-25 14:20:58 +0800136 /* Protected by BQL. */
Paolo Bonzini946fb272011-09-12 13:57:37 +0200137 int64_t cpu_ticks_prev;
138 int64_t cpu_ticks_offset;
Liu Ping Fancb365642013-09-25 14:20:58 +0800139
Paolo Bonzini94377112018-08-14 09:57:16 +0200140 /* Protect fields that can be respectively read outside the
141 * BQL, and written from multiple threads.
Liu Ping Fancb365642013-09-25 14:20:58 +0800142 */
143 QemuSeqLock vm_clock_seqlock;
Paolo Bonzini94377112018-08-14 09:57:16 +0200144 QemuSpin vm_clock_lock;
145
146 int16_t cpu_ticks_enabled;
KONRAD Fredericc96778b2014-08-01 01:37:09 +0200147
Paolo Bonzinic1ff0732018-08-14 09:31:58 +0200148 /* Conversion factor from emulated instructions to virtual clock ticks. */
Paolo Bonzini94377112018-08-14 09:57:16 +0200149 int16_t icount_time_shift;
150
KONRAD Fredericc96778b2014-08-01 01:37:09 +0200151 /* Compensate for varying guest execution speed. */
152 int64_t qemu_icount_bias;
Paolo Bonzini94377112018-08-14 09:57:16 +0200153
154 int64_t vm_clock_warp_start;
155 int64_t cpu_clock_offset;
156
KONRAD Fredericc96778b2014-08-01 01:37:09 +0200157 /* Only written by TCG thread */
158 int64_t qemu_icount;
Paolo Bonzini94377112018-08-14 09:57:16 +0200159
Pavel Dovgalyukb39e3f32018-01-11 11:26:10 +0300160 /* for adjusting icount */
Pavel Dovgalyukb39e3f32018-01-11 11:26:10 +0300161 QEMUTimer *icount_rt_timer;
162 QEMUTimer *icount_vm_timer;
163 QEMUTimer *icount_warp_timer;
Paolo Bonzini946fb272011-09-12 13:57:37 +0200164} TimersState;
165
Liu Ping Fand9cd4002013-07-21 08:43:00 +0000166static TimersState timers_state;
KONRAD Frederic8d4e9142017-02-23 18:29:08 +0000167bool mttcg_enabled;
168
Paolo Bonzini946fb272011-09-12 13:57:37 +0200169
Alex Bennéee4cd9652017-03-31 16:09:42 +0100170/* The current number of executed instructions is based on what we
171 * originally budgeted minus the current state of the decrementing
172 * icount counters in extra/u16.low.
173 */
174static int64_t cpu_get_icount_executed(CPUState *cpu)
175{
Richard Henderson5e140192019-03-28 11:54:23 -1000176 return (cpu->icount_budget -
177 (cpu_neg(cpu)->icount_decr.u16.low + cpu->icount_extra));
Alex Bennéee4cd9652017-03-31 16:09:42 +0100178}
179
Alex Bennée512d3c82017-04-05 12:32:37 +0100180/*
181 * Update the global shared timer_state.qemu_icount to take into
182 * account executed instructions. This is done by the TCG vCPU
183 * thread so the main-loop can see time has moved forward.
184 */
Paolo Bonzini9b4e6f42018-09-11 13:15:32 +0200185static void cpu_update_icount_locked(CPUState *cpu)
Alex Bennée512d3c82017-04-05 12:32:37 +0100186{
187 int64_t executed = cpu_get_icount_executed(cpu);
188 cpu->icount_budget -= executed;
189
Emilio G. Cota38adcb62018-09-10 19:27:49 -0400190 atomic_set_i64(&timers_state.qemu_icount,
191 timers_state.qemu_icount + executed);
Paolo Bonzini9b4e6f42018-09-11 13:15:32 +0200192}
193
194/*
195 * Update the global shared timer_state.qemu_icount to take into
196 * account executed instructions. This is done by the TCG vCPU
197 * thread so the main-loop can see time has moved forward.
198 */
199void cpu_update_icount(CPUState *cpu)
200{
201 seqlock_write_lock(&timers_state.vm_clock_seqlock,
202 &timers_state.vm_clock_lock);
203 cpu_update_icount_locked(cpu);
Paolo Bonzini94377112018-08-14 09:57:16 +0200204 seqlock_write_unlock(&timers_state.vm_clock_seqlock,
205 &timers_state.vm_clock_lock);
Alex Bennée512d3c82017-04-05 12:32:37 +0100206}
207
Paolo Bonzinic1ff0732018-08-14 09:31:58 +0200208static int64_t cpu_get_icount_raw_locked(void)
Paolo Bonzini946fb272011-09-12 13:57:37 +0200209{
Andreas Färber4917cf42013-05-27 05:17:50 +0200210 CPUState *cpu = current_cpu;
Paolo Bonzini946fb272011-09-12 13:57:37 +0200211
Alex Bennée243c5f72017-03-30 18:49:22 +0100212 if (cpu && cpu->running) {
Paolo Bonzini414b15c2015-06-24 14:16:26 +0200213 if (!cpu->can_do_io) {
Alistair Francis493d89b2018-02-03 09:43:14 +0100214 error_report("Bad icount read");
Pavel Dovgalyuk2a629142014-12-08 10:53:45 +0300215 exit(1);
Paolo Bonzini946fb272011-09-12 13:57:37 +0200216 }
Alex Bennéee4cd9652017-03-31 16:09:42 +0100217 /* Take into account what has run */
Paolo Bonzini9b4e6f42018-09-11 13:15:32 +0200218 cpu_update_icount_locked(cpu);
Paolo Bonzini946fb272011-09-12 13:57:37 +0200219 }
Emilio G. Cota38adcb62018-09-10 19:27:49 -0400220 /* The read is protected by the seqlock, but needs atomic64 to avoid UB */
221 return atomic_read_i64(&timers_state.qemu_icount);
Paolo Bonzinic1ff0732018-08-14 09:31:58 +0200222}
223
224static int64_t cpu_get_icount_locked(void)
225{
226 int64_t icount = cpu_get_icount_raw_locked();
Emilio G. Cotac97595d2018-09-10 19:27:50 -0400227 return atomic_read_i64(&timers_state.qemu_icount_bias) +
228 cpu_icount_to_ns(icount);
Paolo Bonzinic1ff0732018-08-14 09:31:58 +0200229}
230
231int64_t cpu_get_icount_raw(void)
232{
233 int64_t icount;
234 unsigned start;
235
236 do {
237 start = seqlock_read_begin(&timers_state.vm_clock_seqlock);
238 icount = cpu_get_icount_raw_locked();
239 } while (seqlock_read_retry(&timers_state.vm_clock_seqlock, start));
240
241 return icount;
Pavel Dovgalyuk2a629142014-12-08 10:53:45 +0300242}
243
244/* Return the virtual CPU time, based on the instruction counter. */
Paolo Bonzini17a15f12013-10-03 15:17:25 +0200245int64_t cpu_get_icount(void)
246{
247 int64_t icount;
248 unsigned start;
249
250 do {
251 start = seqlock_read_begin(&timers_state.vm_clock_seqlock);
252 icount = cpu_get_icount_locked();
253 } while (seqlock_read_retry(&timers_state.vm_clock_seqlock, start));
254
255 return icount;
256}
257
KONRAD Frederic3f031312014-08-01 01:37:15 +0200258int64_t cpu_icount_to_ns(int64_t icount)
259{
Paolo Bonzinic1ff0732018-08-14 09:31:58 +0200260 return icount << atomic_read(&timers_state.icount_time_shift);
KONRAD Frederic3f031312014-08-01 01:37:15 +0200261}
262
Paolo Bonzinif2a4ad62018-08-18 09:36:16 +0200263static int64_t cpu_get_ticks_locked(void)
264{
265 int64_t ticks = timers_state.cpu_ticks_offset;
266 if (timers_state.cpu_ticks_enabled) {
267 ticks += cpu_get_host_ticks();
268 }
269
270 if (timers_state.cpu_ticks_prev > ticks) {
271 /* Non increasing ticks may happen if the host uses software suspend. */
272 timers_state.cpu_ticks_offset += timers_state.cpu_ticks_prev - ticks;
273 ticks = timers_state.cpu_ticks_prev;
274 }
275
276 timers_state.cpu_ticks_prev = ticks;
277 return ticks;
278}
279
Cao jind90f3cc2016-07-29 19:05:38 +0800280/* return the time elapsed in VM between vm_start and vm_stop. Unless
281 * icount is active, cpu_get_ticks() uses units of the host CPU cycle
282 * counter.
Cao jind90f3cc2016-07-29 19:05:38 +0800283 */
Paolo Bonzini946fb272011-09-12 13:57:37 +0200284int64_t cpu_get_ticks(void)
285{
Paolo Bonzini5f3e3102013-10-28 17:32:18 +0100286 int64_t ticks;
287
Paolo Bonzini946fb272011-09-12 13:57:37 +0200288 if (use_icount) {
289 return cpu_get_icount();
290 }
Paolo Bonzini5f3e3102013-10-28 17:32:18 +0100291
Paolo Bonzinif2a4ad62018-08-18 09:36:16 +0200292 qemu_spin_lock(&timers_state.vm_clock_lock);
293 ticks = cpu_get_ticks_locked();
294 qemu_spin_unlock(&timers_state.vm_clock_lock);
Paolo Bonzini5f3e3102013-10-28 17:32:18 +0100295 return ticks;
Paolo Bonzini946fb272011-09-12 13:57:37 +0200296}
297
Liu Ping Fancb365642013-09-25 14:20:58 +0800298static int64_t cpu_get_clock_locked(void)
299{
Cao jin1d45cea2016-07-29 19:05:37 +0800300 int64_t time;
Liu Ping Fancb365642013-09-25 14:20:58 +0800301
Cao jin1d45cea2016-07-29 19:05:37 +0800302 time = timers_state.cpu_clock_offset;
Paolo Bonzini5f3e3102013-10-28 17:32:18 +0100303 if (timers_state.cpu_ticks_enabled) {
Cao jin1d45cea2016-07-29 19:05:37 +0800304 time += get_clock();
Liu Ping Fancb365642013-09-25 14:20:58 +0800305 }
306
Cao jin1d45cea2016-07-29 19:05:37 +0800307 return time;
Liu Ping Fancb365642013-09-25 14:20:58 +0800308}
309
Cao jind90f3cc2016-07-29 19:05:38 +0800310/* Return the monotonic time elapsed in VM, i.e.,
Peter Maydell8212ff82016-09-15 10:24:22 +0100311 * the time between vm_start and vm_stop
312 */
Paolo Bonzini946fb272011-09-12 13:57:37 +0200313int64_t cpu_get_clock(void)
314{
315 int64_t ti;
Liu Ping Fancb365642013-09-25 14:20:58 +0800316 unsigned start;
317
318 do {
319 start = seqlock_read_begin(&timers_state.vm_clock_seqlock);
320 ti = cpu_get_clock_locked();
321 } while (seqlock_read_retry(&timers_state.vm_clock_seqlock, start));
322
323 return ti;
Paolo Bonzini946fb272011-09-12 13:57:37 +0200324}
325
Liu Ping Fancb365642013-09-25 14:20:58 +0800326/* enable cpu_get_ticks()
Cao jin3224e872016-07-08 18:31:37 +0800327 * Caller must hold BQL which serves as mutex for vm_clock_seqlock.
Liu Ping Fancb365642013-09-25 14:20:58 +0800328 */
Paolo Bonzini946fb272011-09-12 13:57:37 +0200329void cpu_enable_ticks(void)
330{
Paolo Bonzini94377112018-08-14 09:57:16 +0200331 seqlock_write_lock(&timers_state.vm_clock_seqlock,
332 &timers_state.vm_clock_lock);
Paolo Bonzini946fb272011-09-12 13:57:37 +0200333 if (!timers_state.cpu_ticks_enabled) {
Christopher Covington4a7428c2015-09-25 10:42:21 -0400334 timers_state.cpu_ticks_offset -= cpu_get_host_ticks();
Paolo Bonzini946fb272011-09-12 13:57:37 +0200335 timers_state.cpu_clock_offset -= get_clock();
336 timers_state.cpu_ticks_enabled = 1;
337 }
Paolo Bonzini94377112018-08-14 09:57:16 +0200338 seqlock_write_unlock(&timers_state.vm_clock_seqlock,
339 &timers_state.vm_clock_lock);
Paolo Bonzini946fb272011-09-12 13:57:37 +0200340}
341
342/* disable cpu_get_ticks() : the clock is stopped. You must not call
Liu Ping Fancb365642013-09-25 14:20:58 +0800343 * cpu_get_ticks() after that.
Cao jin3224e872016-07-08 18:31:37 +0800344 * Caller must hold BQL which serves as mutex for vm_clock_seqlock.
Liu Ping Fancb365642013-09-25 14:20:58 +0800345 */
Paolo Bonzini946fb272011-09-12 13:57:37 +0200346void cpu_disable_ticks(void)
347{
Paolo Bonzini94377112018-08-14 09:57:16 +0200348 seqlock_write_lock(&timers_state.vm_clock_seqlock,
349 &timers_state.vm_clock_lock);
Paolo Bonzini946fb272011-09-12 13:57:37 +0200350 if (timers_state.cpu_ticks_enabled) {
Christopher Covington4a7428c2015-09-25 10:42:21 -0400351 timers_state.cpu_ticks_offset += cpu_get_host_ticks();
Liu Ping Fancb365642013-09-25 14:20:58 +0800352 timers_state.cpu_clock_offset = cpu_get_clock_locked();
Paolo Bonzini946fb272011-09-12 13:57:37 +0200353 timers_state.cpu_ticks_enabled = 0;
354 }
Paolo Bonzini94377112018-08-14 09:57:16 +0200355 seqlock_write_unlock(&timers_state.vm_clock_seqlock,
356 &timers_state.vm_clock_lock);
Paolo Bonzini946fb272011-09-12 13:57:37 +0200357}
358
359/* Correlation between real and virtual time is always going to be
360 fairly approximate, so ignore small variation.
361 When the guest is idle real and virtual time will be aligned in
362 the IO wait loop. */
Rutuja Shah73bcb242016-03-21 21:32:30 +0530363#define ICOUNT_WOBBLE (NANOSECONDS_PER_SECOND / 10)
Paolo Bonzini946fb272011-09-12 13:57:37 +0200364
365static void icount_adjust(void)
366{
367 int64_t cur_time;
368 int64_t cur_icount;
369 int64_t delta;
Paolo Bonzinia3270e12013-10-07 17:18:15 +0200370
371 /* Protected by TimersState mutex. */
Paolo Bonzini946fb272011-09-12 13:57:37 +0200372 static int64_t last_delta;
Paolo Bonzini468cc7c2013-10-07 17:21:51 +0200373
Paolo Bonzini946fb272011-09-12 13:57:37 +0200374 /* If the VM is not running, then do nothing. */
375 if (!runstate_is_running()) {
376 return;
377 }
Paolo Bonzini468cc7c2013-10-07 17:21:51 +0200378
Paolo Bonzini94377112018-08-14 09:57:16 +0200379 seqlock_write_lock(&timers_state.vm_clock_seqlock,
380 &timers_state.vm_clock_lock);
Paolo Bonzini17a15f12013-10-03 15:17:25 +0200381 cur_time = cpu_get_clock_locked();
382 cur_icount = cpu_get_icount_locked();
Paolo Bonzini468cc7c2013-10-07 17:21:51 +0200383
Paolo Bonzini946fb272011-09-12 13:57:37 +0200384 delta = cur_icount - cur_time;
385 /* FIXME: This is a very crude algorithm, somewhat prone to oscillation. */
386 if (delta > 0
387 && last_delta + ICOUNT_WOBBLE < delta * 2
Paolo Bonzinic1ff0732018-08-14 09:31:58 +0200388 && timers_state.icount_time_shift > 0) {
Paolo Bonzini946fb272011-09-12 13:57:37 +0200389 /* The guest is getting too far ahead. Slow time down. */
Paolo Bonzinic1ff0732018-08-14 09:31:58 +0200390 atomic_set(&timers_state.icount_time_shift,
391 timers_state.icount_time_shift - 1);
Paolo Bonzini946fb272011-09-12 13:57:37 +0200392 }
393 if (delta < 0
394 && last_delta - ICOUNT_WOBBLE > delta * 2
Paolo Bonzinic1ff0732018-08-14 09:31:58 +0200395 && timers_state.icount_time_shift < MAX_ICOUNT_SHIFT) {
Paolo Bonzini946fb272011-09-12 13:57:37 +0200396 /* The guest is getting too far behind. Speed time up. */
Paolo Bonzinic1ff0732018-08-14 09:31:58 +0200397 atomic_set(&timers_state.icount_time_shift,
398 timers_state.icount_time_shift + 1);
Paolo Bonzini946fb272011-09-12 13:57:37 +0200399 }
400 last_delta = delta;
Emilio G. Cotac97595d2018-09-10 19:27:50 -0400401 atomic_set_i64(&timers_state.qemu_icount_bias,
402 cur_icount - (timers_state.qemu_icount
403 << timers_state.icount_time_shift));
Paolo Bonzini94377112018-08-14 09:57:16 +0200404 seqlock_write_unlock(&timers_state.vm_clock_seqlock,
405 &timers_state.vm_clock_lock);
Paolo Bonzini946fb272011-09-12 13:57:37 +0200406}
407
408static void icount_adjust_rt(void *opaque)
409{
Pavel Dovgalyukb39e3f32018-01-11 11:26:10 +0300410 timer_mod(timers_state.icount_rt_timer,
Pavel Dovgalyuk1979b902015-01-12 15:00:43 +0300411 qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL_RT) + 1000);
Paolo Bonzini946fb272011-09-12 13:57:37 +0200412 icount_adjust();
413}
414
415static void icount_adjust_vm(void *opaque)
416{
Pavel Dovgalyukb39e3f32018-01-11 11:26:10 +0300417 timer_mod(timers_state.icount_vm_timer,
Alex Bligh40daca52013-08-21 16:03:02 +0100418 qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) +
Rutuja Shah73bcb242016-03-21 21:32:30 +0530419 NANOSECONDS_PER_SECOND / 10);
Paolo Bonzini946fb272011-09-12 13:57:37 +0200420 icount_adjust();
421}
422
423static int64_t qemu_icount_round(int64_t count)
424{
Paolo Bonzinic1ff0732018-08-14 09:31:58 +0200425 int shift = atomic_read(&timers_state.icount_time_shift);
426 return (count + (1 << shift) - 1) >> shift;
Paolo Bonzini946fb272011-09-12 13:57:37 +0200427}
428
Pavel Dovgalyukefab87c2015-09-17 19:24:39 +0300429static void icount_warp_rt(void)
Paolo Bonzini946fb272011-09-12 13:57:37 +0200430{
Alex Bennéeccffff42016-04-04 15:35:48 +0100431 unsigned seq;
432 int64_t warp_start;
433
Paolo Bonzini17a15f12013-10-03 15:17:25 +0200434 /* The icount_warp_timer is rescheduled soon after vm_clock_warp_start
435 * changes from -1 to another value, so the race here is okay.
436 */
Alex Bennéeccffff42016-04-04 15:35:48 +0100437 do {
438 seq = seqlock_read_begin(&timers_state.vm_clock_seqlock);
Pavel Dovgalyukb39e3f32018-01-11 11:26:10 +0300439 warp_start = timers_state.vm_clock_warp_start;
Alex Bennéeccffff42016-04-04 15:35:48 +0100440 } while (seqlock_read_retry(&timers_state.vm_clock_seqlock, seq));
441
442 if (warp_start == -1) {
Paolo Bonzini946fb272011-09-12 13:57:37 +0200443 return;
444 }
445
Paolo Bonzini94377112018-08-14 09:57:16 +0200446 seqlock_write_lock(&timers_state.vm_clock_seqlock,
447 &timers_state.vm_clock_lock);
Paolo Bonzini946fb272011-09-12 13:57:37 +0200448 if (runstate_is_running()) {
Paolo Bonzini74c0b812018-10-08 13:24:14 +0200449 int64_t clock = REPLAY_CLOCK_LOCKED(REPLAY_CLOCK_VIRTUAL_RT,
450 cpu_get_clock_locked());
Paolo Bonzini8ed961d2013-10-07 17:26:07 +0200451 int64_t warp_delta;
452
Pavel Dovgalyukb39e3f32018-01-11 11:26:10 +0300453 warp_delta = clock - timers_state.vm_clock_warp_start;
Paolo Bonzini8ed961d2013-10-07 17:26:07 +0200454 if (use_icount == 2) {
Paolo Bonzini946fb272011-09-12 13:57:37 +0200455 /*
Alex Bligh40daca52013-08-21 16:03:02 +0100456 * In adaptive mode, do not let QEMU_CLOCK_VIRTUAL run too
Paolo Bonzini946fb272011-09-12 13:57:37 +0200457 * far ahead of real time.
458 */
Paolo Bonzini17a15f12013-10-03 15:17:25 +0200459 int64_t cur_icount = cpu_get_icount_locked();
Pavel Dovgalyukbf2a7dd2014-11-26 13:40:55 +0300460 int64_t delta = clock - cur_icount;
Paolo Bonzini8ed961d2013-10-07 17:26:07 +0200461 warp_delta = MIN(warp_delta, delta);
Paolo Bonzini946fb272011-09-12 13:57:37 +0200462 }
Emilio G. Cotac97595d2018-09-10 19:27:50 -0400463 atomic_set_i64(&timers_state.qemu_icount_bias,
464 timers_state.qemu_icount_bias + warp_delta);
Paolo Bonzini946fb272011-09-12 13:57:37 +0200465 }
Pavel Dovgalyukb39e3f32018-01-11 11:26:10 +0300466 timers_state.vm_clock_warp_start = -1;
Paolo Bonzini94377112018-08-14 09:57:16 +0200467 seqlock_write_unlock(&timers_state.vm_clock_seqlock,
468 &timers_state.vm_clock_lock);
Paolo Bonzini8ed961d2013-10-07 17:26:07 +0200469
470 if (qemu_clock_expired(QEMU_CLOCK_VIRTUAL)) {
471 qemu_clock_notify(QEMU_CLOCK_VIRTUAL);
472 }
Paolo Bonzini946fb272011-09-12 13:57:37 +0200473}
474
Pavel Dovgalyuke76d1792016-03-10 14:56:09 +0300475static void icount_timer_cb(void *opaque)
Pavel Dovgalyukefab87c2015-09-17 19:24:39 +0300476{
Pavel Dovgalyuke76d1792016-03-10 14:56:09 +0300477 /* No need for a checkpoint because the timer already synchronizes
478 * with CHECKPOINT_CLOCK_VIRTUAL_RT.
479 */
480 icount_warp_rt();
Pavel Dovgalyukefab87c2015-09-17 19:24:39 +0300481}
482
Paolo Bonzini8156be52012-03-28 15:42:04 +0200483void qtest_clock_warp(int64_t dest)
484{
Alex Bligh40daca52013-08-21 16:03:02 +0100485 int64_t clock = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
Fam Zhengefef88b2015-01-19 17:51:43 +0800486 AioContext *aio_context;
Paolo Bonzini8156be52012-03-28 15:42:04 +0200487 assert(qtest_enabled());
Fam Zhengefef88b2015-01-19 17:51:43 +0800488 aio_context = qemu_get_aio_context();
Paolo Bonzini8156be52012-03-28 15:42:04 +0200489 while (clock < dest) {
Pavel Dovgalyukdcb15782019-07-25 11:44:26 +0300490 int64_t deadline = qemu_clock_deadline_ns_all(QEMU_CLOCK_VIRTUAL,
491 QEMU_TIMER_ATTR_ALL);
Sergey Fedorovc9299e22014-06-10 13:10:28 +0400492 int64_t warp = qemu_soonest_timeout(dest - clock, deadline);
Fam Zhengefef88b2015-01-19 17:51:43 +0800493
Paolo Bonzini94377112018-08-14 09:57:16 +0200494 seqlock_write_lock(&timers_state.vm_clock_seqlock,
495 &timers_state.vm_clock_lock);
Emilio G. Cotac97595d2018-09-10 19:27:50 -0400496 atomic_set_i64(&timers_state.qemu_icount_bias,
497 timers_state.qemu_icount_bias + warp);
Paolo Bonzini94377112018-08-14 09:57:16 +0200498 seqlock_write_unlock(&timers_state.vm_clock_seqlock,
499 &timers_state.vm_clock_lock);
Paolo Bonzini17a15f12013-10-03 15:17:25 +0200500
Alex Bligh40daca52013-08-21 16:03:02 +0100501 qemu_clock_run_timers(QEMU_CLOCK_VIRTUAL);
Fam Zhengefef88b2015-01-19 17:51:43 +0800502 timerlist_run_timers(aio_context->tlg.tl[QEMU_CLOCK_VIRTUAL]);
Alex Bligh40daca52013-08-21 16:03:02 +0100503 clock = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
Paolo Bonzini8156be52012-03-28 15:42:04 +0200504 }
Alex Bligh40daca52013-08-21 16:03:02 +0100505 qemu_clock_notify(QEMU_CLOCK_VIRTUAL);
Paolo Bonzini8156be52012-03-28 15:42:04 +0200506}
507
Pavel Dovgalyuke76d1792016-03-10 14:56:09 +0300508void qemu_start_warp_timer(void)
Paolo Bonzini946fb272011-09-12 13:57:37 +0200509{
Paolo Bonzinice78d182013-10-07 17:30:02 +0200510 int64_t clock;
Paolo Bonzini946fb272011-09-12 13:57:37 +0200511 int64_t deadline;
512
Pavel Dovgalyuke76d1792016-03-10 14:56:09 +0300513 if (!use_icount) {
Paolo Bonzini946fb272011-09-12 13:57:37 +0200514 return;
515 }
516
Pavel Dovgalyuk8bd7f712015-09-17 19:24:44 +0300517 /* Nothing to do if the VM is stopped: QEMU_CLOCK_VIRTUAL timers
518 * do not fire, so computing the deadline does not make sense.
519 */
520 if (!runstate_is_running()) {
521 return;
522 }
523
Pavel Dovgalyuk0c081852018-09-12 11:19:45 +0300524 if (replay_mode != REPLAY_MODE_PLAY) {
525 if (!all_cpu_threads_idle()) {
526 return;
527 }
Pavel Dovgalyuk8bd7f712015-09-17 19:24:44 +0300528
Pavel Dovgalyuk0c081852018-09-12 11:19:45 +0300529 if (qtest_enabled()) {
530 /* When testing, qtest commands advance icount. */
531 return;
532 }
Paolo Bonzini946fb272011-09-12 13:57:37 +0200533
Pavel Dovgalyuk0c081852018-09-12 11:19:45 +0300534 replay_checkpoint(CHECKPOINT_CLOCK_WARP_START);
535 } else {
536 /* warp clock deterministically in record/replay mode */
537 if (!replay_checkpoint(CHECKPOINT_CLOCK_WARP_START)) {
538 /* vCPU is sleeping and warp can't be started.
539 It is probably a race condition: notification sent
540 to vCPU was processed in advance and vCPU went to sleep.
541 Therefore we have to wake it up for doing someting. */
542 if (replay_has_checkpoint()) {
543 qemu_clock_notify(QEMU_CLOCK_VIRTUAL);
544 }
545 return;
546 }
Paolo Bonzini8156be52012-03-28 15:42:04 +0200547 }
548
Alex Blighac70aaf2013-08-21 16:02:57 +0100549 /* We want to use the earliest deadline from ALL vm_clocks */
Pavel Dovgalyukbf2a7dd2014-11-26 13:40:55 +0300550 clock = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL_RT);
Pavel Dovgalyukdcb15782019-07-25 11:44:26 +0300551 deadline = qemu_clock_deadline_ns_all(QEMU_CLOCK_VIRTUAL,
552 ~QEMU_TIMER_ATTR_EXTERNAL);
Paolo Bonzinice78d182013-10-07 17:30:02 +0200553 if (deadline < 0) {
Victor CLEMENTd7a0f712015-05-29 17:14:06 +0200554 static bool notified;
555 if (!icount_sleep && !notified) {
Alistair Francis3dc6f862017-07-12 06:57:41 -0700556 warn_report("icount sleep disabled and no active timers");
Victor CLEMENTd7a0f712015-05-29 17:14:06 +0200557 notified = true;
558 }
Paolo Bonzinice78d182013-10-07 17:30:02 +0200559 return;
Alex Blighac70aaf2013-08-21 16:02:57 +0100560 }
561
Paolo Bonzini946fb272011-09-12 13:57:37 +0200562 if (deadline > 0) {
563 /*
Alex Bligh40daca52013-08-21 16:03:02 +0100564 * Ensure QEMU_CLOCK_VIRTUAL proceeds even when the virtual CPU goes to
Paolo Bonzini946fb272011-09-12 13:57:37 +0200565 * sleep. Otherwise, the CPU might be waiting for a future timer
566 * interrupt to wake it up, but the interrupt never comes because
567 * the vCPU isn't running any insns and thus doesn't advance the
Alex Bligh40daca52013-08-21 16:03:02 +0100568 * QEMU_CLOCK_VIRTUAL.
Paolo Bonzini946fb272011-09-12 13:57:37 +0200569 */
Victor CLEMENT5045e9d92015-05-29 17:14:04 +0200570 if (!icount_sleep) {
571 /*
572 * We never let VCPUs sleep in no sleep icount mode.
573 * If there is a pending QEMU_CLOCK_VIRTUAL timer we just advance
574 * to the next QEMU_CLOCK_VIRTUAL event and notify it.
575 * It is useful when we want a deterministic execution time,
576 * isolated from host latencies.
577 */
Paolo Bonzini94377112018-08-14 09:57:16 +0200578 seqlock_write_lock(&timers_state.vm_clock_seqlock,
579 &timers_state.vm_clock_lock);
Emilio G. Cotac97595d2018-09-10 19:27:50 -0400580 atomic_set_i64(&timers_state.qemu_icount_bias,
581 timers_state.qemu_icount_bias + deadline);
Paolo Bonzini94377112018-08-14 09:57:16 +0200582 seqlock_write_unlock(&timers_state.vm_clock_seqlock,
583 &timers_state.vm_clock_lock);
Victor CLEMENT5045e9d92015-05-29 17:14:04 +0200584 qemu_clock_notify(QEMU_CLOCK_VIRTUAL);
585 } else {
586 /*
587 * We do stop VCPUs and only advance QEMU_CLOCK_VIRTUAL after some
588 * "real" time, (related to the time left until the next event) has
589 * passed. The QEMU_CLOCK_VIRTUAL_RT clock will do this.
590 * This avoids that the warps are visible externally; for example,
591 * you will not be sending network packets continuously instead of
592 * every 100ms.
593 */
Paolo Bonzini94377112018-08-14 09:57:16 +0200594 seqlock_write_lock(&timers_state.vm_clock_seqlock,
595 &timers_state.vm_clock_lock);
Pavel Dovgalyukb39e3f32018-01-11 11:26:10 +0300596 if (timers_state.vm_clock_warp_start == -1
597 || timers_state.vm_clock_warp_start > clock) {
598 timers_state.vm_clock_warp_start = clock;
Victor CLEMENT5045e9d92015-05-29 17:14:04 +0200599 }
Paolo Bonzini94377112018-08-14 09:57:16 +0200600 seqlock_write_unlock(&timers_state.vm_clock_seqlock,
601 &timers_state.vm_clock_lock);
Pavel Dovgalyukb39e3f32018-01-11 11:26:10 +0300602 timer_mod_anticipate(timers_state.icount_warp_timer,
603 clock + deadline);
Paolo Bonzinice78d182013-10-07 17:30:02 +0200604 }
Alex Blighac70aaf2013-08-21 16:02:57 +0100605 } else if (deadline == 0) {
Alex Bligh40daca52013-08-21 16:03:02 +0100606 qemu_clock_notify(QEMU_CLOCK_VIRTUAL);
Paolo Bonzini946fb272011-09-12 13:57:37 +0200607 }
608}
609
Pavel Dovgalyuke76d1792016-03-10 14:56:09 +0300610static void qemu_account_warp_timer(void)
611{
612 if (!use_icount || !icount_sleep) {
613 return;
614 }
615
616 /* Nothing to do if the VM is stopped: QEMU_CLOCK_VIRTUAL timers
617 * do not fire, so computing the deadline does not make sense.
618 */
619 if (!runstate_is_running()) {
620 return;
621 }
622
623 /* warp clock deterministically in record/replay mode */
624 if (!replay_checkpoint(CHECKPOINT_CLOCK_WARP_ACCOUNT)) {
625 return;
626 }
627
Pavel Dovgalyukb39e3f32018-01-11 11:26:10 +0300628 timer_del(timers_state.icount_warp_timer);
Pavel Dovgalyuke76d1792016-03-10 14:56:09 +0300629 icount_warp_rt();
630}
631
KONRAD Fredericd09eae32014-08-01 01:37:10 +0200632static bool icount_state_needed(void *opaque)
633{
634 return use_icount;
635}
636
Pavel Dovgalyukb39e3f32018-01-11 11:26:10 +0300637static bool warp_timer_state_needed(void *opaque)
638{
639 TimersState *s = opaque;
640 return s->icount_warp_timer != NULL;
641}
642
643static bool adjust_timers_state_needed(void *opaque)
644{
645 TimersState *s = opaque;
646 return s->icount_rt_timer != NULL;
647}
648
649/*
650 * Subsection for warp timer migration is optional, because may not be created
651 */
652static const VMStateDescription icount_vmstate_warp_timer = {
653 .name = "timer/icount/warp_timer",
654 .version_id = 1,
655 .minimum_version_id = 1,
656 .needed = warp_timer_state_needed,
657 .fields = (VMStateField[]) {
658 VMSTATE_INT64(vm_clock_warp_start, TimersState),
659 VMSTATE_TIMER_PTR(icount_warp_timer, TimersState),
660 VMSTATE_END_OF_LIST()
661 }
662};
663
664static const VMStateDescription icount_vmstate_adjust_timers = {
665 .name = "timer/icount/timers",
666 .version_id = 1,
667 .minimum_version_id = 1,
668 .needed = adjust_timers_state_needed,
669 .fields = (VMStateField[]) {
670 VMSTATE_TIMER_PTR(icount_rt_timer, TimersState),
671 VMSTATE_TIMER_PTR(icount_vm_timer, TimersState),
672 VMSTATE_END_OF_LIST()
673 }
674};
675
KONRAD Fredericd09eae32014-08-01 01:37:10 +0200676/*
677 * This is a subsection for icount migration.
678 */
679static const VMStateDescription icount_vmstate_timers = {
680 .name = "timer/icount",
681 .version_id = 1,
682 .minimum_version_id = 1,
Juan Quintela5cd8cad2014-09-23 14:09:54 +0200683 .needed = icount_state_needed,
KONRAD Fredericd09eae32014-08-01 01:37:10 +0200684 .fields = (VMStateField[]) {
685 VMSTATE_INT64(qemu_icount_bias, TimersState),
686 VMSTATE_INT64(qemu_icount, TimersState),
687 VMSTATE_END_OF_LIST()
Pavel Dovgalyukb39e3f32018-01-11 11:26:10 +0300688 },
689 .subsections = (const VMStateDescription*[]) {
690 &icount_vmstate_warp_timer,
691 &icount_vmstate_adjust_timers,
692 NULL
KONRAD Fredericd09eae32014-08-01 01:37:10 +0200693 }
694};
695
Paolo Bonzini946fb272011-09-12 13:57:37 +0200696static const VMStateDescription vmstate_timers = {
697 .name = "timer",
698 .version_id = 2,
699 .minimum_version_id = 1,
Juan Quintela35d08452014-04-16 16:01:33 +0200700 .fields = (VMStateField[]) {
Paolo Bonzini946fb272011-09-12 13:57:37 +0200701 VMSTATE_INT64(cpu_ticks_offset, TimersState),
Paolo Bonzinic1ff0732018-08-14 09:31:58 +0200702 VMSTATE_UNUSED(8),
Paolo Bonzini946fb272011-09-12 13:57:37 +0200703 VMSTATE_INT64_V(cpu_clock_offset, TimersState, 2),
704 VMSTATE_END_OF_LIST()
KONRAD Fredericd09eae32014-08-01 01:37:10 +0200705 },
Juan Quintela5cd8cad2014-09-23 14:09:54 +0200706 .subsections = (const VMStateDescription*[]) {
707 &icount_vmstate_timers,
708 NULL
Paolo Bonzini946fb272011-09-12 13:57:37 +0200709 }
710};
711
Paolo Bonzini14e6fe12016-10-31 10:36:08 +0100712static void cpu_throttle_thread(CPUState *cpu, run_on_cpu_data opaque)
Jason J. Herne2adcc852015-09-08 13:12:33 -0400713{
Jason J. Herne2adcc852015-09-08 13:12:33 -0400714 double pct;
715 double throttle_ratio;
Yury Kotovbd1f7ff2019-09-09 16:13:34 +0300716 int64_t sleeptime_ns, endtime_ns;
Jason J. Herne2adcc852015-09-08 13:12:33 -0400717
718 if (!cpu_throttle_get_percentage()) {
719 return;
720 }
721
722 pct = (double)cpu_throttle_get_percentage()/100;
723 throttle_ratio = pct / (1 - pct);
Yury Kotovbd1f7ff2019-09-09 16:13:34 +0300724 /* Add 1ns to fix double's rounding error (like 0.9999999...) */
725 sleeptime_ns = (int64_t)(throttle_ratio * CPU_THROTTLE_TIMESLICE_NS + 1);
726 endtime_ns = qemu_clock_get_ns(QEMU_CLOCK_REALTIME) + sleeptime_ns;
727 while (sleeptime_ns > 0 && !cpu->stop) {
728 if (sleeptime_ns > SCALE_MS) {
729 qemu_cond_timedwait(cpu->halt_cond, &qemu_global_mutex,
730 sleeptime_ns / SCALE_MS);
731 } else {
732 qemu_mutex_unlock_iothread();
733 g_usleep(sleeptime_ns / SCALE_US);
734 qemu_mutex_lock_iothread();
735 }
736 sleeptime_ns = endtime_ns - qemu_clock_get_ns(QEMU_CLOCK_REALTIME);
737 }
Felipe Franciosi90bb0c02017-05-19 22:29:50 +0100738 atomic_set(&cpu->throttle_thread_scheduled, 0);
Jason J. Herne2adcc852015-09-08 13:12:33 -0400739}
740
741static void cpu_throttle_timer_tick(void *opaque)
742{
743 CPUState *cpu;
744 double pct;
745
746 /* Stop the timer if needed */
747 if (!cpu_throttle_get_percentage()) {
748 return;
749 }
750 CPU_FOREACH(cpu) {
751 if (!atomic_xchg(&cpu->throttle_thread_scheduled, 1)) {
Paolo Bonzini14e6fe12016-10-31 10:36:08 +0100752 async_run_on_cpu(cpu, cpu_throttle_thread,
753 RUN_ON_CPU_NULL);
Jason J. Herne2adcc852015-09-08 13:12:33 -0400754 }
755 }
756
757 pct = (double)cpu_throttle_get_percentage()/100;
758 timer_mod(throttle_timer, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL_RT) +
759 CPU_THROTTLE_TIMESLICE_NS / (1-pct));
760}
761
762void cpu_throttle_set(int new_throttle_pct)
763{
764 /* Ensure throttle percentage is within valid range */
765 new_throttle_pct = MIN(new_throttle_pct, CPU_THROTTLE_PCT_MAX);
766 new_throttle_pct = MAX(new_throttle_pct, CPU_THROTTLE_PCT_MIN);
767
768 atomic_set(&throttle_percentage, new_throttle_pct);
769
770 timer_mod(throttle_timer, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL_RT) +
771 CPU_THROTTLE_TIMESLICE_NS);
772}
773
774void cpu_throttle_stop(void)
775{
776 atomic_set(&throttle_percentage, 0);
777}
778
779bool cpu_throttle_active(void)
780{
781 return (cpu_throttle_get_percentage() != 0);
782}
783
784int cpu_throttle_get_percentage(void)
785{
786 return atomic_read(&throttle_percentage);
787}
788
Pavel Dovgalyuk4603ea02014-09-01 09:34:49 +0400789void cpu_ticks_init(void)
790{
Emilio G. Cotaccdb3c12016-06-08 14:55:20 -0400791 seqlock_init(&timers_state.vm_clock_seqlock);
Emilio G. Cota87a09cd2018-09-03 13:18:29 -0400792 qemu_spin_init(&timers_state.vm_clock_lock);
Pavel Dovgalyuk4603ea02014-09-01 09:34:49 +0400793 vmstate_register(NULL, 0, &vmstate_timers, &timers_state);
Jason J. Herne2adcc852015-09-08 13:12:33 -0400794 throttle_timer = timer_new_ns(QEMU_CLOCK_VIRTUAL_RT,
795 cpu_throttle_timer_tick, NULL);
Pavel Dovgalyuk4603ea02014-09-01 09:34:49 +0400796}
797
Sebastian Tanase1ad95802014-07-25 11:56:28 +0200798void configure_icount(QemuOpts *opts, Error **errp)
Paolo Bonzini946fb272011-09-12 13:57:37 +0200799{
Sebastian Tanase1ad95802014-07-25 11:56:28 +0200800 const char *option;
Sebastian Tanasea8bfac32014-07-25 11:56:29 +0200801 char *rem_str = NULL;
Sebastian Tanase1ad95802014-07-25 11:56:28 +0200802
Sebastian Tanase1ad95802014-07-25 11:56:28 +0200803 option = qemu_opt_get(opts, "shift");
Paolo Bonzini946fb272011-09-12 13:57:37 +0200804 if (!option) {
Sebastian Tanasea8bfac32014-07-25 11:56:29 +0200805 if (qemu_opt_get(opts, "align") != NULL) {
806 error_setg(errp, "Please specify shift option when using align");
807 }
Paolo Bonzini946fb272011-09-12 13:57:37 +0200808 return;
809 }
Victor CLEMENTf1f4b572015-05-29 17:14:05 +0200810
811 icount_sleep = qemu_opt_get_bool(opts, "sleep", true);
Victor CLEMENT5045e9d92015-05-29 17:14:04 +0200812 if (icount_sleep) {
Pavel Dovgalyukb39e3f32018-01-11 11:26:10 +0300813 timers_state.icount_warp_timer = timer_new_ns(QEMU_CLOCK_VIRTUAL_RT,
Pavel Dovgalyuke76d1792016-03-10 14:56:09 +0300814 icount_timer_cb, NULL);
Victor CLEMENT5045e9d92015-05-29 17:14:04 +0200815 }
Victor CLEMENTf1f4b572015-05-29 17:14:05 +0200816
Sebastian Tanasea8bfac32014-07-25 11:56:29 +0200817 icount_align_option = qemu_opt_get_bool(opts, "align", false);
Victor CLEMENTf1f4b572015-05-29 17:14:05 +0200818
819 if (icount_align_option && !icount_sleep) {
Pranith Kumar778d9f92016-02-26 10:16:51 -0500820 error_setg(errp, "align=on and sleep=off are incompatible");
Victor CLEMENTf1f4b572015-05-29 17:14:05 +0200821 }
Paolo Bonzini946fb272011-09-12 13:57:37 +0200822 if (strcmp(option, "auto") != 0) {
Sebastian Tanasea8bfac32014-07-25 11:56:29 +0200823 errno = 0;
Paolo Bonzinic1ff0732018-08-14 09:31:58 +0200824 timers_state.icount_time_shift = strtol(option, &rem_str, 0);
Sebastian Tanasea8bfac32014-07-25 11:56:29 +0200825 if (errno != 0 || *rem_str != '\0' || !strlen(option)) {
826 error_setg(errp, "icount: Invalid shift value");
827 }
Paolo Bonzini946fb272011-09-12 13:57:37 +0200828 use_icount = 1;
829 return;
Sebastian Tanasea8bfac32014-07-25 11:56:29 +0200830 } else if (icount_align_option) {
831 error_setg(errp, "shift=auto and align=on are incompatible");
Victor CLEMENTf1f4b572015-05-29 17:14:05 +0200832 } else if (!icount_sleep) {
Pranith Kumar778d9f92016-02-26 10:16:51 -0500833 error_setg(errp, "shift=auto and sleep=off are incompatible");
Paolo Bonzini946fb272011-09-12 13:57:37 +0200834 }
835
836 use_icount = 2;
837
838 /* 125MIPS seems a reasonable initial guess at the guest speed.
839 It will be corrected fairly quickly anyway. */
Paolo Bonzinic1ff0732018-08-14 09:31:58 +0200840 timers_state.icount_time_shift = 3;
Paolo Bonzini946fb272011-09-12 13:57:37 +0200841
842 /* Have both realtime and virtual time triggers for speed adjustment.
843 The realtime trigger catches emulated time passing too slowly,
844 the virtual time trigger catches emulated time passing too fast.
845 Realtime triggers occur even when idle, so use them less frequently
846 than VM triggers. */
Pavel Dovgalyukb39e3f32018-01-11 11:26:10 +0300847 timers_state.vm_clock_warp_start = -1;
848 timers_state.icount_rt_timer = timer_new_ms(QEMU_CLOCK_VIRTUAL_RT,
Pavel Dovgalyukbf2a7dd2014-11-26 13:40:55 +0300849 icount_adjust_rt, NULL);
Pavel Dovgalyukb39e3f32018-01-11 11:26:10 +0300850 timer_mod(timers_state.icount_rt_timer,
Pavel Dovgalyukbf2a7dd2014-11-26 13:40:55 +0300851 qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL_RT) + 1000);
Pavel Dovgalyukb39e3f32018-01-11 11:26:10 +0300852 timers_state.icount_vm_timer = timer_new_ns(QEMU_CLOCK_VIRTUAL,
Alex Bligh40daca52013-08-21 16:03:02 +0100853 icount_adjust_vm, NULL);
Pavel Dovgalyukb39e3f32018-01-11 11:26:10 +0300854 timer_mod(timers_state.icount_vm_timer,
Alex Bligh40daca52013-08-21 16:03:02 +0100855 qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) +
Rutuja Shah73bcb242016-03-21 21:32:30 +0530856 NANOSECONDS_PER_SECOND / 10);
Paolo Bonzini946fb272011-09-12 13:57:37 +0200857}
858
859/***********************************************************/
Alex Bennée65467062017-02-23 18:29:09 +0000860/* TCG vCPU kick timer
861 *
862 * The kick timer is responsible for moving single threaded vCPU
863 * emulation on to the next vCPU. If more than one vCPU is running a
864 * timer event with force a cpu->exit so the next vCPU can get
865 * scheduled.
866 *
867 * The timer is removed if all vCPUs are idle and restarted again once
868 * idleness is complete.
869 */
870
871static QEMUTimer *tcg_kick_vcpu_timer;
Alex Bennée791158d2017-02-23 18:29:10 +0000872static CPUState *tcg_current_rr_cpu;
Alex Bennée65467062017-02-23 18:29:09 +0000873
874#define TCG_KICK_PERIOD (NANOSECONDS_PER_SECOND / 10)
875
876static inline int64_t qemu_tcg_next_kick(void)
877{
878 return qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) + TCG_KICK_PERIOD;
879}
880
Alex Bennéee8f22f72019-10-01 17:04:26 +0100881/* Kick the currently round-robin scheduled vCPU to next */
882static void qemu_cpu_kick_rr_next_cpu(void)
Alex Bennée791158d2017-02-23 18:29:10 +0000883{
884 CPUState *cpu;
Alex Bennée791158d2017-02-23 18:29:10 +0000885 do {
886 cpu = atomic_mb_read(&tcg_current_rr_cpu);
887 if (cpu) {
888 cpu_exit(cpu);
889 }
890 } while (cpu != atomic_mb_read(&tcg_current_rr_cpu));
891}
892
Alex Bennéee8f22f72019-10-01 17:04:26 +0100893/* Kick all RR vCPUs */
894static void qemu_cpu_kick_rr_cpus(void)
895{
896 CPUState *cpu;
897
898 CPU_FOREACH(cpu) {
899 cpu_exit(cpu);
900 };
901}
902
Paolo Bonzini6b8f0182017-03-02 19:56:40 +0100903static void do_nothing(CPUState *cpu, run_on_cpu_data unused)
904{
905}
906
Paolo Bonzini3f53bc62017-03-03 11:50:29 +0100907void qemu_timer_notify_cb(void *opaque, QEMUClockType type)
908{
Paolo Bonzini6b8f0182017-03-02 19:56:40 +0100909 if (!use_icount || type != QEMU_CLOCK_VIRTUAL) {
910 qemu_notify_event();
911 return;
912 }
913
Peter Maydellc52e7132018-04-10 13:02:25 +0100914 if (qemu_in_vcpu_thread()) {
915 /* A CPU is currently running; kick it back out to the
916 * tcg_cpu_exec() loop so it will recalculate its
917 * icount deadline immediately.
918 */
919 qemu_cpu_kick(current_cpu);
920 } else if (first_cpu) {
Paolo Bonzini6b8f0182017-03-02 19:56:40 +0100921 /* qemu_cpu_kick is not enough to kick a halted CPU out of
922 * qemu_tcg_wait_io_event. async_run_on_cpu, instead,
923 * causes cpu_thread_is_idle to return false. This way,
924 * handle_icount_deadline can run.
Peter Maydellc52e7132018-04-10 13:02:25 +0100925 * If we have no CPUs at all for some reason, we don't
926 * need to do anything.
Paolo Bonzini6b8f0182017-03-02 19:56:40 +0100927 */
928 async_run_on_cpu(first_cpu, do_nothing, RUN_ON_CPU_NULL);
929 }
Paolo Bonzini3f53bc62017-03-03 11:50:29 +0100930}
931
Alex Bennée65467062017-02-23 18:29:09 +0000932static void kick_tcg_thread(void *opaque)
933{
934 timer_mod(tcg_kick_vcpu_timer, qemu_tcg_next_kick());
Alex Bennéee8f22f72019-10-01 17:04:26 +0100935 qemu_cpu_kick_rr_next_cpu();
Alex Bennée65467062017-02-23 18:29:09 +0000936}
937
938static void start_tcg_kick_timer(void)
939{
Paolo Bonzinidb08b682018-01-11 13:53:12 +0100940 assert(!mttcg_enabled);
941 if (!tcg_kick_vcpu_timer && CPU_NEXT(first_cpu)) {
Alex Bennée65467062017-02-23 18:29:09 +0000942 tcg_kick_vcpu_timer = timer_new_ns(QEMU_CLOCK_VIRTUAL,
943 kick_tcg_thread, NULL);
Alex Bennée1926ab22018-09-27 18:17:24 +0100944 }
945 if (tcg_kick_vcpu_timer && !timer_pending(tcg_kick_vcpu_timer)) {
Alex Bennée65467062017-02-23 18:29:09 +0000946 timer_mod(tcg_kick_vcpu_timer, qemu_tcg_next_kick());
947 }
948}
949
950static void stop_tcg_kick_timer(void)
951{
Paolo Bonzinidb08b682018-01-11 13:53:12 +0100952 assert(!mttcg_enabled);
Alex Bennée1926ab22018-09-27 18:17:24 +0100953 if (tcg_kick_vcpu_timer && timer_pending(tcg_kick_vcpu_timer)) {
Alex Bennée65467062017-02-23 18:29:09 +0000954 timer_del(tcg_kick_vcpu_timer);
Alex Bennée65467062017-02-23 18:29:09 +0000955 }
956}
957
Alex Bennée65467062017-02-23 18:29:09 +0000958/***********************************************************/
Blue Swirl296af7c2010-03-29 19:23:50 +0000959void hw_error(const char *fmt, ...)
960{
961 va_list ap;
Andreas Färber55e5c282012-12-17 06:18:02 +0100962 CPUState *cpu;
Blue Swirl296af7c2010-03-29 19:23:50 +0000963
964 va_start(ap, fmt);
965 fprintf(stderr, "qemu: hardware error: ");
966 vfprintf(stderr, fmt, ap);
967 fprintf(stderr, "\n");
Andreas Färberbdc44642013-06-24 23:50:24 +0200968 CPU_FOREACH(cpu) {
Andreas Färber55e5c282012-12-17 06:18:02 +0100969 fprintf(stderr, "CPU #%d:\n", cpu->cpu_index);
Markus Armbruster90c84c52019-04-17 21:18:02 +0200970 cpu_dump_state(cpu, stderr, CPU_DUMP_FPU);
Blue Swirl296af7c2010-03-29 19:23:50 +0000971 }
972 va_end(ap);
973 abort();
974}
975
976void cpu_synchronize_all_states(void)
977{
Andreas Färber182735e2013-05-29 22:29:20 +0200978 CPUState *cpu;
Blue Swirl296af7c2010-03-29 19:23:50 +0000979
Andreas Färberbdc44642013-06-24 23:50:24 +0200980 CPU_FOREACH(cpu) {
Andreas Färber182735e2013-05-29 22:29:20 +0200981 cpu_synchronize_state(cpu);
Sergio Andres Gomez Del Realc97d6d22017-09-13 04:05:09 -0500982 /* TODO: move to cpu_synchronize_state() */
983 if (hvf_enabled()) {
984 hvf_cpu_synchronize_state(cpu);
985 }
Blue Swirl296af7c2010-03-29 19:23:50 +0000986 }
987}
988
989void cpu_synchronize_all_post_reset(void)
990{
Andreas Färber182735e2013-05-29 22:29:20 +0200991 CPUState *cpu;
Blue Swirl296af7c2010-03-29 19:23:50 +0000992
Andreas Färberbdc44642013-06-24 23:50:24 +0200993 CPU_FOREACH(cpu) {
Andreas Färber182735e2013-05-29 22:29:20 +0200994 cpu_synchronize_post_reset(cpu);
Sergio Andres Gomez Del Realc97d6d22017-09-13 04:05:09 -0500995 /* TODO: move to cpu_synchronize_post_reset() */
996 if (hvf_enabled()) {
997 hvf_cpu_synchronize_post_reset(cpu);
998 }
Blue Swirl296af7c2010-03-29 19:23:50 +0000999 }
1000}
1001
1002void cpu_synchronize_all_post_init(void)
1003{
Andreas Färber182735e2013-05-29 22:29:20 +02001004 CPUState *cpu;
Blue Swirl296af7c2010-03-29 19:23:50 +00001005
Andreas Färberbdc44642013-06-24 23:50:24 +02001006 CPU_FOREACH(cpu) {
Andreas Färber182735e2013-05-29 22:29:20 +02001007 cpu_synchronize_post_init(cpu);
Sergio Andres Gomez Del Realc97d6d22017-09-13 04:05:09 -05001008 /* TODO: move to cpu_synchronize_post_init() */
1009 if (hvf_enabled()) {
1010 hvf_cpu_synchronize_post_init(cpu);
1011 }
Blue Swirl296af7c2010-03-29 19:23:50 +00001012 }
1013}
1014
David Gibson75e972d2017-05-26 14:46:28 +10001015void cpu_synchronize_all_pre_loadvm(void)
1016{
1017 CPUState *cpu;
1018
1019 CPU_FOREACH(cpu) {
1020 cpu_synchronize_pre_loadvm(cpu);
1021 }
1022}
1023
Stefan Hajnoczi4486e892018-03-07 14:42:05 +00001024static int do_vm_stop(RunState state, bool send_stop)
Blue Swirl296af7c2010-03-29 19:23:50 +00001025{
Kevin Wolf56983462013-07-05 13:49:54 +02001026 int ret = 0;
1027
Luiz Capitulino13548692011-07-29 15:36:43 -03001028 if (runstate_is_running()) {
Blue Swirl296af7c2010-03-29 19:23:50 +00001029 cpu_disable_ticks();
Blue Swirl296af7c2010-03-29 19:23:50 +00001030 pause_all_vcpus();
Luiz Capitulinof5bbfba2011-07-29 15:04:45 -03001031 runstate_set(state);
Luiz Capitulino1dfb4dd2011-07-29 14:26:33 -03001032 vm_state_notify(0, state);
Stefan Hajnoczi4486e892018-03-07 14:42:05 +00001033 if (send_stop) {
Peter Xu3ab72382018-08-15 21:37:37 +08001034 qapi_event_send_stop();
Stefan Hajnoczi4486e892018-03-07 14:42:05 +00001035 }
Blue Swirl296af7c2010-03-29 19:23:50 +00001036 }
Kevin Wolf56983462013-07-05 13:49:54 +02001037
Kevin Wolf594a45c2013-07-18 14:52:19 +02001038 bdrv_drain_all();
John Snow22af08e2016-09-22 21:45:51 -04001039 ret = bdrv_flush_all();
Kevin Wolf594a45c2013-07-18 14:52:19 +02001040
Kevin Wolf56983462013-07-05 13:49:54 +02001041 return ret;
Blue Swirl296af7c2010-03-29 19:23:50 +00001042}
1043
Stefan Hajnoczi4486e892018-03-07 14:42:05 +00001044/* Special vm_stop() variant for terminating the process. Historically clients
1045 * did not expect a QMP STOP event and so we need to retain compatibility.
1046 */
1047int vm_shutdown(void)
1048{
1049 return do_vm_stop(RUN_STATE_SHUTDOWN, false);
1050}
1051
Andreas Färbera1fcaa72012-05-02 23:42:26 +02001052static bool cpu_can_run(CPUState *cpu)
Blue Swirl296af7c2010-03-29 19:23:50 +00001053{
Andreas Färber4fdeee72012-05-02 23:10:09 +02001054 if (cpu->stop) {
Andreas Färbera1fcaa72012-05-02 23:42:26 +02001055 return false;
Jan Kiszka0ab07c62011-02-07 12:19:14 +01001056 }
Tiejun Chen321bc0b2013-08-02 09:43:09 +08001057 if (cpu_is_stopped(cpu)) {
Andreas Färbera1fcaa72012-05-02 23:42:26 +02001058 return false;
Jan Kiszka0ab07c62011-02-07 12:19:14 +01001059 }
Andreas Färbera1fcaa72012-05-02 23:42:26 +02001060 return true;
Blue Swirl296af7c2010-03-29 19:23:50 +00001061}
1062
Andreas Färber91325042013-05-27 02:07:49 +02001063static void cpu_handle_guest_debug(CPUState *cpu)
Jan Kiszka3c638d02010-06-25 16:56:56 +02001064{
Andreas Färber64f6b342013-05-27 02:06:09 +02001065 gdb_set_stop_cpu(cpu);
Jan Kiszka8cf71712011-02-07 12:19:16 +01001066 qemu_system_debug_request();
Andreas Färberf324e762012-05-02 23:26:21 +02001067 cpu->stopped = true;
Jan Kiszka3c638d02010-06-25 16:56:56 +02001068}
1069
Jan Kiszka6d9cb732011-02-01 22:15:58 +01001070#ifdef CONFIG_LINUX
1071static void sigbus_reraise(void)
1072{
1073 sigset_t set;
1074 struct sigaction action;
1075
1076 memset(&action, 0, sizeof(action));
1077 action.sa_handler = SIG_DFL;
1078 if (!sigaction(SIGBUS, &action, NULL)) {
1079 raise(SIGBUS);
1080 sigemptyset(&set);
1081 sigaddset(&set, SIGBUS);
Peter Maydella2d17612016-05-16 18:33:59 +01001082 pthread_sigmask(SIG_UNBLOCK, &set, NULL);
Jan Kiszka6d9cb732011-02-01 22:15:58 +01001083 }
1084 perror("Failed to re-raise SIGBUS!\n");
1085 abort();
1086}
1087
Paolo Bonzinid98d4072017-02-08 13:22:12 +01001088static void sigbus_handler(int n, siginfo_t *siginfo, void *ctx)
Jan Kiszka6d9cb732011-02-01 22:15:58 +01001089{
Paolo Bonzinia16fc072017-02-09 09:50:02 +01001090 if (siginfo->si_code != BUS_MCEERR_AO && siginfo->si_code != BUS_MCEERR_AR) {
1091 sigbus_reraise();
1092 }
1093
Paolo Bonzini2ae41db2017-02-08 12:48:54 +01001094 if (current_cpu) {
1095 /* Called asynchronously in VCPU thread. */
1096 if (kvm_on_sigbus_vcpu(current_cpu, siginfo->si_code, siginfo->si_addr)) {
1097 sigbus_reraise();
1098 }
1099 } else {
1100 /* Called synchronously (via signalfd) in main thread. */
1101 if (kvm_on_sigbus(siginfo->si_code, siginfo->si_addr)) {
1102 sigbus_reraise();
1103 }
Jan Kiszka6d9cb732011-02-01 22:15:58 +01001104 }
1105}
1106
1107static void qemu_init_sigbus(void)
1108{
1109 struct sigaction action;
1110
1111 memset(&action, 0, sizeof(action));
1112 action.sa_flags = SA_SIGINFO;
Paolo Bonzinid98d4072017-02-08 13:22:12 +01001113 action.sa_sigaction = sigbus_handler;
Jan Kiszka6d9cb732011-02-01 22:15:58 +01001114 sigaction(SIGBUS, &action, NULL);
1115
1116 prctl(PR_MCE_KILL, PR_MCE_KILL_SET, PR_MCE_KILL_EARLY, 0, 0);
1117}
Paolo Bonzinia16fc072017-02-09 09:50:02 +01001118#else /* !CONFIG_LINUX */
1119static void qemu_init_sigbus(void)
1120{
1121}
Paolo Bonzinia16fc072017-02-09 09:50:02 +01001122#endif /* !CONFIG_LINUX */
Blue Swirl296af7c2010-03-29 19:23:50 +00001123
Blue Swirl296af7c2010-03-29 19:23:50 +00001124static QemuThread io_thread;
1125
Blue Swirl296af7c2010-03-29 19:23:50 +00001126/* cpu creation */
1127static QemuCond qemu_cpu_cond;
1128/* system init */
Blue Swirl296af7c2010-03-29 19:23:50 +00001129static QemuCond qemu_pause_cond;
1130
Paolo Bonzinid3b12f52011-09-13 10:30:52 +02001131void qemu_init_cpu_loop(void)
Blue Swirl296af7c2010-03-29 19:23:50 +00001132{
Jan Kiszka6d9cb732011-02-01 22:15:58 +01001133 qemu_init_sigbus();
Anthony Liguoried945922011-02-08 18:18:18 +01001134 qemu_cond_init(&qemu_cpu_cond);
Anthony Liguoried945922011-02-08 18:18:18 +01001135 qemu_cond_init(&qemu_pause_cond);
Blue Swirl296af7c2010-03-29 19:23:50 +00001136 qemu_mutex_init(&qemu_global_mutex);
Blue Swirl296af7c2010-03-29 19:23:50 +00001137
Jan Kiszkab7680cb2011-03-12 17:43:51 +01001138 qemu_thread_get_self(&io_thread);
Blue Swirl296af7c2010-03-29 19:23:50 +00001139}
1140
Paolo Bonzini14e6fe12016-10-31 10:36:08 +01001141void run_on_cpu(CPUState *cpu, run_on_cpu_func func, run_on_cpu_data data)
Marcelo Tosattie82bcec2010-05-04 09:45:22 -03001142{
Sergey Fedorovd148d902016-08-29 09:51:00 +02001143 do_run_on_cpu(cpu, func, data, &qemu_global_mutex);
Chegu Vinod3c022702013-06-24 03:49:41 -06001144}
1145
Gu Zheng4c055ab2016-05-12 09:18:13 +05301146static void qemu_kvm_destroy_vcpu(CPUState *cpu)
1147{
1148 if (kvm_destroy_vcpu(cpu) < 0) {
1149 error_report("kvm_destroy_vcpu failed");
1150 exit(EXIT_FAILURE);
1151 }
1152}
1153
1154static void qemu_tcg_destroy_vcpu(CPUState *cpu)
1155{
1156}
1157
David Hildenbrandebd05fe2017-11-29 20:12:15 +01001158static void qemu_cpu_stop(CPUState *cpu, bool exit)
1159{
1160 g_assert(qemu_cpu_is_self(cpu));
1161 cpu->stop = false;
1162 cpu->stopped = true;
1163 if (exit) {
1164 cpu_exit(cpu);
1165 }
1166 qemu_cond_broadcast(&qemu_pause_cond);
1167}
1168
Andreas Färber509a0d72012-05-03 02:18:09 +02001169static void qemu_wait_io_event_common(CPUState *cpu)
Blue Swirl296af7c2010-03-29 19:23:50 +00001170{
Alex Bennée37257942017-02-23 18:29:14 +00001171 atomic_mb_set(&cpu->thread_kicked, false);
Andreas Färber4fdeee72012-05-02 23:10:09 +02001172 if (cpu->stop) {
David Hildenbrandebd05fe2017-11-29 20:12:15 +01001173 qemu_cpu_stop(cpu, false);
Blue Swirl296af7c2010-03-29 19:23:50 +00001174 }
Sergey Fedorova5403c62016-08-02 18:27:36 +01001175 process_queued_cpu_work(cpu);
Alex Bennée37257942017-02-23 18:29:14 +00001176}
1177
Paolo Bonzinia8efa602018-11-14 12:36:57 +01001178static void qemu_tcg_rr_wait_io_event(void)
Alex Bennée37257942017-02-23 18:29:14 +00001179{
Paolo Bonzinia8efa602018-11-14 12:36:57 +01001180 CPUState *cpu;
1181
Paolo Bonzinidb08b682018-01-11 13:53:12 +01001182 while (all_cpu_threads_idle()) {
Alex Bennée65467062017-02-23 18:29:09 +00001183 stop_tcg_kick_timer();
Paolo Bonzinia8efa602018-11-14 12:36:57 +01001184 qemu_cond_wait(first_cpu->halt_cond, &qemu_global_mutex);
Jan Kiszka16400322011-02-09 16:29:37 +01001185 }
Blue Swirl296af7c2010-03-29 19:23:50 +00001186
Alex Bennée65467062017-02-23 18:29:09 +00001187 start_tcg_kick_timer();
1188
Paolo Bonzinia8efa602018-11-14 12:36:57 +01001189 CPU_FOREACH(cpu) {
1190 qemu_wait_io_event_common(cpu);
1191 }
Blue Swirl296af7c2010-03-29 19:23:50 +00001192}
1193
Paolo Bonzinidb08b682018-01-11 13:53:12 +01001194static void qemu_wait_io_event(CPUState *cpu)
Blue Swirl296af7c2010-03-29 19:23:50 +00001195{
Emilio G. Cota30865f32018-10-21 13:30:35 -04001196 bool slept = false;
1197
Andreas Färbera98ae1d2013-05-26 23:21:08 +02001198 while (cpu_thread_is_idle(cpu)) {
Emilio G. Cota30865f32018-10-21 13:30:35 -04001199 if (!slept) {
1200 slept = true;
1201 qemu_plugin_vcpu_idle_cb(cpu);
1202 }
Andreas Färberf5c121b2012-05-03 01:22:49 +02001203 qemu_cond_wait(cpu->halt_cond, &qemu_global_mutex);
Jan Kiszka16400322011-02-09 16:29:37 +01001204 }
Emilio G. Cota30865f32018-10-21 13:30:35 -04001205 if (slept) {
1206 qemu_plugin_vcpu_resume_cb(cpu);
1207 }
Blue Swirl296af7c2010-03-29 19:23:50 +00001208
Paolo Bonzinidb08b682018-01-11 13:53:12 +01001209#ifdef _WIN32
1210 /* Eat dummy APC queued by qemu_cpu_kick_thread. */
1211 if (!tcg_enabled()) {
1212 SleepEx(0, TRUE);
Sergio Andres Gomez Del Realc97d6d22017-09-13 04:05:09 -05001213 }
Paolo Bonzinidb08b682018-01-11 13:53:12 +01001214#endif
Sergio Andres Gomez Del Realc97d6d22017-09-13 04:05:09 -05001215 qemu_wait_io_event_common(cpu);
1216}
1217
Jan Kiszka7e97cd82011-02-07 12:19:12 +01001218static void *qemu_kvm_cpu_thread_fn(void *arg)
Blue Swirl296af7c2010-03-29 19:23:50 +00001219{
Andreas Färber48a106b2013-05-27 02:20:39 +02001220 CPUState *cpu = arg;
Jan Kiszka84b49152011-02-01 22:15:50 +01001221 int r;
Blue Swirl296af7c2010-03-29 19:23:50 +00001222
Paolo Bonziniab28bd22015-07-09 08:55:38 +02001223 rcu_register_thread();
1224
Paolo Bonzini2e7f7a32015-06-18 18:47:18 +02001225 qemu_mutex_lock_iothread();
Andreas Färber814e6122012-05-02 17:00:37 +02001226 qemu_thread_get_self(cpu->thread);
Andreas Färber9f09e182012-05-03 06:59:07 +02001227 cpu->thread_id = qemu_get_thread_id();
Pavel Dovgalyuk626cf8f2014-12-08 10:53:17 +03001228 cpu->can_do_io = 1;
Andreas Färber4917cf42013-05-27 05:17:50 +02001229 current_cpu = cpu;
Blue Swirl296af7c2010-03-29 19:23:50 +00001230
Andreas Färber504134d2012-12-17 06:38:45 +01001231 r = kvm_init_vcpu(cpu);
Jan Kiszka84b49152011-02-01 22:15:50 +01001232 if (r < 0) {
Alistair Francis493d89b2018-02-03 09:43:14 +01001233 error_report("kvm_init_vcpu failed: %s", strerror(-r));
Jan Kiszka84b49152011-02-01 22:15:50 +01001234 exit(1);
1235 }
Blue Swirl296af7c2010-03-29 19:23:50 +00001236
Paolo Bonzini18268b62017-02-09 09:41:14 +01001237 kvm_init_cpu_signals(cpu);
Blue Swirl296af7c2010-03-29 19:23:50 +00001238
1239 /* signal CPU creation */
Andreas Färber61a46212012-05-02 22:49:36 +02001240 cpu->created = true;
Blue Swirl296af7c2010-03-29 19:23:50 +00001241 qemu_cond_signal(&qemu_cpu_cond);
Richard Henderson9c09a252019-03-14 13:06:29 -07001242 qemu_guest_random_seed_thread_part2(cpu->random_seed);
Blue Swirl296af7c2010-03-29 19:23:50 +00001243
Gu Zheng4c055ab2016-05-12 09:18:13 +05301244 do {
Andreas Färbera1fcaa72012-05-02 23:42:26 +02001245 if (cpu_can_run(cpu)) {
Andreas Färber1458c362013-05-26 23:46:55 +02001246 r = kvm_cpu_exec(cpu);
Jan Kiszka83f338f2011-02-07 12:19:17 +01001247 if (r == EXCP_DEBUG) {
Andreas Färber91325042013-05-27 02:07:49 +02001248 cpu_handle_guest_debug(cpu);
Jan Kiszka83f338f2011-02-07 12:19:17 +01001249 }
Jan Kiszka0ab07c62011-02-07 12:19:14 +01001250 }
Paolo Bonzinidb08b682018-01-11 13:53:12 +01001251 qemu_wait_io_event(cpu);
Gu Zheng4c055ab2016-05-12 09:18:13 +05301252 } while (!cpu->unplug || cpu_can_run(cpu));
Blue Swirl296af7c2010-03-29 19:23:50 +00001253
Gu Zheng4c055ab2016-05-12 09:18:13 +05301254 qemu_kvm_destroy_vcpu(cpu);
Bharata B Rao2c579042016-05-12 09:18:14 +05301255 cpu->created = false;
1256 qemu_cond_signal(&qemu_cpu_cond);
Gu Zheng4c055ab2016-05-12 09:18:13 +05301257 qemu_mutex_unlock_iothread();
Paolo Bonzini57615ed2018-01-30 11:04:36 -05001258 rcu_unregister_thread();
Blue Swirl296af7c2010-03-29 19:23:50 +00001259 return NULL;
1260}
1261
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +02001262static void *qemu_dummy_cpu_thread_fn(void *arg)
1263{
1264#ifdef _WIN32
Alistair Francis493d89b2018-02-03 09:43:14 +01001265 error_report("qtest is not supported under Windows");
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +02001266 exit(1);
1267#else
Andreas Färber10a90212013-05-27 02:24:35 +02001268 CPUState *cpu = arg;
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +02001269 sigset_t waitset;
1270 int r;
1271
Paolo Bonziniab28bd22015-07-09 08:55:38 +02001272 rcu_register_thread();
1273
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +02001274 qemu_mutex_lock_iothread();
Andreas Färber814e6122012-05-02 17:00:37 +02001275 qemu_thread_get_self(cpu->thread);
Andreas Färber9f09e182012-05-03 06:59:07 +02001276 cpu->thread_id = qemu_get_thread_id();
Pavel Dovgalyuk626cf8f2014-12-08 10:53:17 +03001277 cpu->can_do_io = 1;
Alex Bennée37257942017-02-23 18:29:14 +00001278 current_cpu = cpu;
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +02001279
1280 sigemptyset(&waitset);
1281 sigaddset(&waitset, SIG_IPI);
1282
1283 /* signal CPU creation */
Andreas Färber61a46212012-05-02 22:49:36 +02001284 cpu->created = true;
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +02001285 qemu_cond_signal(&qemu_cpu_cond);
Richard Henderson9c09a252019-03-14 13:06:29 -07001286 qemu_guest_random_seed_thread_part2(cpu->random_seed);
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +02001287
Paolo Bonzinid2831ab2018-01-30 11:04:53 -05001288 do {
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +02001289 qemu_mutex_unlock_iothread();
1290 do {
1291 int sig;
1292 r = sigwait(&waitset, &sig);
1293 } while (r == -1 && (errno == EAGAIN || errno == EINTR));
1294 if (r == -1) {
1295 perror("sigwait");
1296 exit(1);
1297 }
1298 qemu_mutex_lock_iothread();
Paolo Bonzinidb08b682018-01-11 13:53:12 +01001299 qemu_wait_io_event(cpu);
Paolo Bonzinid2831ab2018-01-30 11:04:53 -05001300 } while (!cpu->unplug);
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +02001301
David Hildenbrandd40bfcb2019-02-18 10:21:57 +01001302 qemu_mutex_unlock_iothread();
Paolo Bonzinid2831ab2018-01-30 11:04:53 -05001303 rcu_unregister_thread();
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +02001304 return NULL;
1305#endif
1306}
1307
Alex Bennée1be7fcb2016-10-27 16:10:08 +01001308static int64_t tcg_get_icount_limit(void)
1309{
1310 int64_t deadline;
1311
1312 if (replay_mode != REPLAY_MODE_PLAY) {
Pavel Dovgalyukdcb15782019-07-25 11:44:26 +03001313 /*
1314 * Include all the timers, because they may need an attention.
1315 * Too long CPU execution may create unnecessary delay in UI.
1316 */
1317 deadline = qemu_clock_deadline_ns_all(QEMU_CLOCK_VIRTUAL,
1318 QEMU_TIMER_ATTR_ALL);
Pavel Dovgalyukfc6b2db2019-12-19 15:46:28 +03001319 /* Check realtime timers, because they help with input processing */
1320 deadline = qemu_soonest_timeout(deadline,
1321 qemu_clock_deadline_ns_all(QEMU_CLOCK_REALTIME,
1322 QEMU_TIMER_ATTR_ALL));
Alex Bennée1be7fcb2016-10-27 16:10:08 +01001323
1324 /* Maintain prior (possibly buggy) behaviour where if no deadline
1325 * was set (as there is no QEMU_CLOCK_VIRTUAL timer) or it is more than
1326 * INT32_MAX nanoseconds ahead, we still use INT32_MAX
1327 * nanoseconds.
1328 */
1329 if ((deadline < 0) || (deadline > INT32_MAX)) {
1330 deadline = INT32_MAX;
1331 }
1332
1333 return qemu_icount_round(deadline);
1334 } else {
1335 return replay_get_instructions();
1336 }
1337}
1338
Alex Bennée12e97002016-10-27 16:10:14 +01001339static void handle_icount_deadline(void)
1340{
Paolo Bonzini6b8f0182017-03-02 19:56:40 +01001341 assert(qemu_in_vcpu_thread());
Alex Bennée12e97002016-10-27 16:10:14 +01001342 if (use_icount) {
Pavel Dovgalyukdcb15782019-07-25 11:44:26 +03001343 int64_t deadline = qemu_clock_deadline_ns_all(QEMU_CLOCK_VIRTUAL,
1344 QEMU_TIMER_ATTR_ALL);
Alex Bennée12e97002016-10-27 16:10:14 +01001345
1346 if (deadline == 0) {
Paolo Bonzini6b8f0182017-03-02 19:56:40 +01001347 /* Wake up other AioContexts. */
Alex Bennée12e97002016-10-27 16:10:14 +01001348 qemu_clock_notify(QEMU_CLOCK_VIRTUAL);
Paolo Bonzini6b8f0182017-03-02 19:56:40 +01001349 qemu_clock_run_timers(QEMU_CLOCK_VIRTUAL);
Alex Bennée12e97002016-10-27 16:10:14 +01001350 }
1351 }
1352}
1353
Alex Bennée05248382017-03-29 16:46:59 +01001354static void prepare_icount_for_run(CPUState *cpu)
1355{
1356 if (use_icount) {
Alex Bennéeeda5f7c2017-04-05 12:35:48 +01001357 int insns_left;
Alex Bennée05248382017-03-29 16:46:59 +01001358
1359 /* These should always be cleared by process_icount_data after
1360 * each vCPU execution. However u16.high can be raised
1361 * asynchronously by cpu_exit/cpu_interrupt/tcg_handle_interrupt
1362 */
Richard Henderson5e140192019-03-28 11:54:23 -10001363 g_assert(cpu_neg(cpu)->icount_decr.u16.low == 0);
Alex Bennée05248382017-03-29 16:46:59 +01001364 g_assert(cpu->icount_extra == 0);
1365
Alex Bennéeeda5f7c2017-04-05 12:35:48 +01001366 cpu->icount_budget = tcg_get_icount_limit();
1367 insns_left = MIN(0xffff, cpu->icount_budget);
Richard Henderson5e140192019-03-28 11:54:23 -10001368 cpu_neg(cpu)->icount_decr.u16.low = insns_left;
Alex Bennéeeda5f7c2017-04-05 12:35:48 +01001369 cpu->icount_extra = cpu->icount_budget - insns_left;
Alex Bennéed759c952018-02-27 12:52:48 +03001370
1371 replay_mutex_lock();
Alex Bennée05248382017-03-29 16:46:59 +01001372 }
1373}
1374
1375static void process_icount_data(CPUState *cpu)
1376{
1377 if (use_icount) {
Alex Bennéee4cd9652017-03-31 16:09:42 +01001378 /* Account for executed instructions */
Alex Bennée512d3c82017-04-05 12:32:37 +01001379 cpu_update_icount(cpu);
Alex Bennée05248382017-03-29 16:46:59 +01001380
1381 /* Reset the counters */
Richard Henderson5e140192019-03-28 11:54:23 -10001382 cpu_neg(cpu)->icount_decr.u16.low = 0;
Alex Bennée05248382017-03-29 16:46:59 +01001383 cpu->icount_extra = 0;
Alex Bennéee4cd9652017-03-31 16:09:42 +01001384 cpu->icount_budget = 0;
1385
Alex Bennée05248382017-03-29 16:46:59 +01001386 replay_account_executed_instructions();
Alex Bennéed759c952018-02-27 12:52:48 +03001387
1388 replay_mutex_unlock();
Alex Bennée05248382017-03-29 16:46:59 +01001389 }
1390}
1391
1392
Alex Bennée1be7fcb2016-10-27 16:10:08 +01001393static int tcg_cpu_exec(CPUState *cpu)
1394{
1395 int ret;
1396#ifdef CONFIG_PROFILER
1397 int64_t ti;
1398#endif
1399
Emilio G. Cotaf28d0df2018-06-22 13:45:31 -04001400 assert(tcg_enabled());
Alex Bennée1be7fcb2016-10-27 16:10:08 +01001401#ifdef CONFIG_PROFILER
1402 ti = profile_getclock();
1403#endif
Alex Bennée1be7fcb2016-10-27 16:10:08 +01001404 cpu_exec_start(cpu);
1405 ret = cpu_exec(cpu);
1406 cpu_exec_end(cpu);
1407#ifdef CONFIG_PROFILER
Emilio G. Cota72fd2ef2018-10-10 10:48:53 -04001408 atomic_set(&tcg_ctx->prof.cpu_exec_time,
1409 tcg_ctx->prof.cpu_exec_time + profile_getclock() - ti);
Alex Bennée1be7fcb2016-10-27 16:10:08 +01001410#endif
Alex Bennée1be7fcb2016-10-27 16:10:08 +01001411 return ret;
1412}
1413
Alex Bennéec93bbbe2016-10-27 16:10:09 +01001414/* Destroy any remaining vCPUs which have been unplugged and have
1415 * finished running
1416 */
1417static void deal_with_unplugged_cpus(void)
Alex Bennée1be7fcb2016-10-27 16:10:08 +01001418{
Alex Bennéec93bbbe2016-10-27 16:10:09 +01001419 CPUState *cpu;
Alex Bennée1be7fcb2016-10-27 16:10:08 +01001420
Alex Bennéec93bbbe2016-10-27 16:10:09 +01001421 CPU_FOREACH(cpu) {
1422 if (cpu->unplug && !cpu_can_run(cpu)) {
1423 qemu_tcg_destroy_vcpu(cpu);
1424 cpu->created = false;
1425 qemu_cond_signal(&qemu_cpu_cond);
Alex Bennée1be7fcb2016-10-27 16:10:08 +01001426 break;
1427 }
1428 }
Alex Bennée1be7fcb2016-10-27 16:10:08 +01001429}
Jan Kiszkabdb7ca62011-09-26 09:40:39 +02001430
Alex Bennée65467062017-02-23 18:29:09 +00001431/* Single-threaded TCG
1432 *
1433 * In the single-threaded case each vCPU is simulated in turn. If
1434 * there is more than a single vCPU we create a simple timer to kick
1435 * the vCPU and ensure we don't get stuck in a tight loop in one vCPU.
1436 * This is done explicitly rather than relying on side-effects
1437 * elsewhere.
1438 */
1439
Alex Bennée37257942017-02-23 18:29:14 +00001440static void *qemu_tcg_rr_cpu_thread_fn(void *arg)
Blue Swirl296af7c2010-03-29 19:23:50 +00001441{
Andreas Färberc3586ba2012-05-03 01:41:24 +02001442 CPUState *cpu = arg;
Blue Swirl296af7c2010-03-29 19:23:50 +00001443
Emilio G. Cotaf28d0df2018-06-22 13:45:31 -04001444 assert(tcg_enabled());
Paolo Bonziniab28bd22015-07-09 08:55:38 +02001445 rcu_register_thread();
Emilio G. Cota3468b592017-07-19 18:57:58 -04001446 tcg_register_thread();
Paolo Bonziniab28bd22015-07-09 08:55:38 +02001447
Paolo Bonzini2e7f7a32015-06-18 18:47:18 +02001448 qemu_mutex_lock_iothread();
Andreas Färber814e6122012-05-02 17:00:37 +02001449 qemu_thread_get_self(cpu->thread);
Blue Swirl296af7c2010-03-29 19:23:50 +00001450
David Hildenbrand5a9c9732018-02-09 20:52:39 +01001451 cpu->thread_id = qemu_get_thread_id();
1452 cpu->created = true;
1453 cpu->can_do_io = 1;
Blue Swirl296af7c2010-03-29 19:23:50 +00001454 qemu_cond_signal(&qemu_cpu_cond);
Richard Henderson9c09a252019-03-14 13:06:29 -07001455 qemu_guest_random_seed_thread_part2(cpu->random_seed);
Blue Swirl296af7c2010-03-29 19:23:50 +00001456
Jan Kiszkafa7d1862011-08-22 18:35:25 +02001457 /* wait for initial kick-off after machine start */
Emilio G. Cotac28e3992015-04-27 12:45:28 -04001458 while (first_cpu->stopped) {
KONRAD Fredericd5f8d612015-08-10 17:27:06 +02001459 qemu_cond_wait(first_cpu->halt_cond, &qemu_global_mutex);
Jan Kiszka8e564b42012-02-17 18:31:15 +01001460
1461 /* process any pending work */
Andreas Färberbdc44642013-06-24 23:50:24 +02001462 CPU_FOREACH(cpu) {
Alex Bennée37257942017-02-23 18:29:14 +00001463 current_cpu = cpu;
Andreas Färber182735e2013-05-29 22:29:20 +02001464 qemu_wait_io_event_common(cpu);
Jan Kiszka8e564b42012-02-17 18:31:15 +01001465 }
Jan Kiszka0ab07c62011-02-07 12:19:14 +01001466 }
Blue Swirl296af7c2010-03-29 19:23:50 +00001467
Alex Bennée65467062017-02-23 18:29:09 +00001468 start_tcg_kick_timer();
1469
Alex Bennéec93bbbe2016-10-27 16:10:09 +01001470 cpu = first_cpu;
1471
Alex Bennéee5143e32017-02-23 18:29:12 +00001472 /* process any pending work */
1473 cpu->exit_request = 1;
1474
Blue Swirl296af7c2010-03-29 19:23:50 +00001475 while (1) {
Alex Bennéed759c952018-02-27 12:52:48 +03001476 qemu_mutex_unlock_iothread();
1477 replay_mutex_lock();
1478 qemu_mutex_lock_iothread();
Alex Bennéec93bbbe2016-10-27 16:10:09 +01001479 /* Account partial waits to QEMU_CLOCK_VIRTUAL. */
1480 qemu_account_warp_timer();
1481
Paolo Bonzini6b8f0182017-03-02 19:56:40 +01001482 /* Run the timers here. This is much more efficient than
1483 * waking up the I/O thread and waiting for completion.
1484 */
1485 handle_icount_deadline();
1486
Alex Bennéed759c952018-02-27 12:52:48 +03001487 replay_mutex_unlock();
1488
Alex Bennéec93bbbe2016-10-27 16:10:09 +01001489 if (!cpu) {
1490 cpu = first_cpu;
1491 }
1492
Alex Bennéee5143e32017-02-23 18:29:12 +00001493 while (cpu && !cpu->queued_work_first && !cpu->exit_request) {
1494
Alex Bennée791158d2017-02-23 18:29:10 +00001495 atomic_mb_set(&tcg_current_rr_cpu, cpu);
Alex Bennée37257942017-02-23 18:29:14 +00001496 current_cpu = cpu;
Alex Bennéec93bbbe2016-10-27 16:10:09 +01001497
1498 qemu_clock_enable(QEMU_CLOCK_VIRTUAL,
1499 (cpu->singlestep_enabled & SSTEP_NOTIMER) == 0);
1500
1501 if (cpu_can_run(cpu)) {
1502 int r;
Alex Bennée05248382017-03-29 16:46:59 +01001503
Alex Bennéed759c952018-02-27 12:52:48 +03001504 qemu_mutex_unlock_iothread();
Alex Bennée05248382017-03-29 16:46:59 +01001505 prepare_icount_for_run(cpu);
1506
Alex Bennéec93bbbe2016-10-27 16:10:09 +01001507 r = tcg_cpu_exec(cpu);
Alex Bennée05248382017-03-29 16:46:59 +01001508
1509 process_icount_data(cpu);
Alex Bennéed759c952018-02-27 12:52:48 +03001510 qemu_mutex_lock_iothread();
Alex Bennée05248382017-03-29 16:46:59 +01001511
Alex Bennéec93bbbe2016-10-27 16:10:09 +01001512 if (r == EXCP_DEBUG) {
1513 cpu_handle_guest_debug(cpu);
1514 break;
Pranith Kumar08e73c42017-02-23 18:29:15 +00001515 } else if (r == EXCP_ATOMIC) {
1516 qemu_mutex_unlock_iothread();
1517 cpu_exec_step_atomic(cpu);
1518 qemu_mutex_lock_iothread();
1519 break;
Alex Bennéec93bbbe2016-10-27 16:10:09 +01001520 }
Alex Bennée37257942017-02-23 18:29:14 +00001521 } else if (cpu->stop) {
Alex Bennéec93bbbe2016-10-27 16:10:09 +01001522 if (cpu->unplug) {
1523 cpu = CPU_NEXT(cpu);
1524 }
1525 break;
1526 }
1527
Alex Bennéee5143e32017-02-23 18:29:12 +00001528 cpu = CPU_NEXT(cpu);
1529 } /* while (cpu && !cpu->exit_request).. */
1530
Alex Bennée791158d2017-02-23 18:29:10 +00001531 /* Does not need atomic_mb_set because a spurious wakeup is okay. */
1532 atomic_set(&tcg_current_rr_cpu, NULL);
Alex Bennéec93bbbe2016-10-27 16:10:09 +01001533
Alex Bennéee5143e32017-02-23 18:29:12 +00001534 if (cpu && cpu->exit_request) {
1535 atomic_mb_set(&cpu->exit_request, 0);
1536 }
Alex Blighac70aaf2013-08-21 16:02:57 +01001537
Clement Deschamps013aabd2018-10-21 16:21:03 +02001538 if (use_icount && all_cpu_threads_idle()) {
1539 /*
1540 * When all cpus are sleeping (e.g in WFI), to avoid a deadlock
1541 * in the main_loop, wake it up in order to start the warp timer.
1542 */
1543 qemu_notify_event();
1544 }
1545
Paolo Bonzinia8efa602018-11-14 12:36:57 +01001546 qemu_tcg_rr_wait_io_event();
Alex Bennéec93bbbe2016-10-27 16:10:09 +01001547 deal_with_unplugged_cpus();
Blue Swirl296af7c2010-03-29 19:23:50 +00001548 }
1549
Paolo Bonzini9b0605f2018-01-30 11:05:06 -05001550 rcu_unregister_thread();
Blue Swirl296af7c2010-03-29 19:23:50 +00001551 return NULL;
1552}
1553
Vincent Palatinb0cb0a62017-01-10 11:59:57 +01001554static void *qemu_hax_cpu_thread_fn(void *arg)
1555{
1556 CPUState *cpu = arg;
1557 int r;
Vincent Palatinb3d3a422017-03-20 11:15:49 +01001558
Paolo Bonzini9857c2d2018-01-30 16:28:49 +01001559 rcu_register_thread();
Vincent Palatinb3d3a422017-03-20 11:15:49 +01001560 qemu_mutex_lock_iothread();
Vincent Palatinb0cb0a62017-01-10 11:59:57 +01001561 qemu_thread_get_self(cpu->thread);
Vincent Palatinb0cb0a62017-01-10 11:59:57 +01001562
1563 cpu->thread_id = qemu_get_thread_id();
1564 cpu->created = true;
Vincent Palatinb0cb0a62017-01-10 11:59:57 +01001565 current_cpu = cpu;
1566
1567 hax_init_vcpu(cpu);
1568 qemu_cond_signal(&qemu_cpu_cond);
Richard Henderson9c09a252019-03-14 13:06:29 -07001569 qemu_guest_random_seed_thread_part2(cpu->random_seed);
Vincent Palatinb0cb0a62017-01-10 11:59:57 +01001570
Paolo Bonzini9857c2d2018-01-30 16:28:49 +01001571 do {
Vincent Palatinb0cb0a62017-01-10 11:59:57 +01001572 if (cpu_can_run(cpu)) {
1573 r = hax_smp_cpu_exec(cpu);
1574 if (r == EXCP_DEBUG) {
1575 cpu_handle_guest_debug(cpu);
1576 }
1577 }
1578
Paolo Bonzinidb08b682018-01-11 13:53:12 +01001579 qemu_wait_io_event(cpu);
Paolo Bonzini9857c2d2018-01-30 16:28:49 +01001580 } while (!cpu->unplug || cpu_can_run(cpu));
1581 rcu_unregister_thread();
Vincent Palatinb0cb0a62017-01-10 11:59:57 +01001582 return NULL;
1583}
1584
Sergio Andres Gomez Del Realc97d6d22017-09-13 04:05:09 -05001585/* The HVF-specific vCPU thread function. This one should only run when the host
1586 * CPU supports the VMX "unrestricted guest" feature. */
1587static void *qemu_hvf_cpu_thread_fn(void *arg)
1588{
1589 CPUState *cpu = arg;
1590
1591 int r;
1592
1593 assert(hvf_enabled());
1594
1595 rcu_register_thread();
1596
1597 qemu_mutex_lock_iothread();
1598 qemu_thread_get_self(cpu->thread);
1599
1600 cpu->thread_id = qemu_get_thread_id();
1601 cpu->can_do_io = 1;
1602 current_cpu = cpu;
1603
1604 hvf_init_vcpu(cpu);
1605
1606 /* signal CPU creation */
1607 cpu->created = true;
1608 qemu_cond_signal(&qemu_cpu_cond);
Richard Henderson9c09a252019-03-14 13:06:29 -07001609 qemu_guest_random_seed_thread_part2(cpu->random_seed);
Sergio Andres Gomez Del Realc97d6d22017-09-13 04:05:09 -05001610
1611 do {
1612 if (cpu_can_run(cpu)) {
1613 r = hvf_vcpu_exec(cpu);
1614 if (r == EXCP_DEBUG) {
1615 cpu_handle_guest_debug(cpu);
1616 }
1617 }
Paolo Bonzinidb08b682018-01-11 13:53:12 +01001618 qemu_wait_io_event(cpu);
Sergio Andres Gomez Del Realc97d6d22017-09-13 04:05:09 -05001619 } while (!cpu->unplug || cpu_can_run(cpu));
1620
1621 hvf_vcpu_destroy(cpu);
1622 cpu->created = false;
1623 qemu_cond_signal(&qemu_cpu_cond);
1624 qemu_mutex_unlock_iothread();
Paolo Bonzini8178e632018-01-30 11:05:21 -05001625 rcu_unregister_thread();
Sergio Andres Gomez Del Realc97d6d22017-09-13 04:05:09 -05001626 return NULL;
1627}
1628
Justin Terry (VM)19306802018-01-22 13:07:49 -08001629static void *qemu_whpx_cpu_thread_fn(void *arg)
1630{
1631 CPUState *cpu = arg;
1632 int r;
1633
1634 rcu_register_thread();
1635
1636 qemu_mutex_lock_iothread();
1637 qemu_thread_get_self(cpu->thread);
1638 cpu->thread_id = qemu_get_thread_id();
1639 current_cpu = cpu;
1640
1641 r = whpx_init_vcpu(cpu);
1642 if (r < 0) {
1643 fprintf(stderr, "whpx_init_vcpu failed: %s\n", strerror(-r));
1644 exit(1);
1645 }
1646
1647 /* signal CPU creation */
1648 cpu->created = true;
1649 qemu_cond_signal(&qemu_cpu_cond);
Richard Henderson9c09a252019-03-14 13:06:29 -07001650 qemu_guest_random_seed_thread_part2(cpu->random_seed);
Justin Terry (VM)19306802018-01-22 13:07:49 -08001651
1652 do {
1653 if (cpu_can_run(cpu)) {
1654 r = whpx_vcpu_exec(cpu);
1655 if (r == EXCP_DEBUG) {
1656 cpu_handle_guest_debug(cpu);
1657 }
1658 }
1659 while (cpu_thread_is_idle(cpu)) {
1660 qemu_cond_wait(cpu->halt_cond, &qemu_global_mutex);
1661 }
1662 qemu_wait_io_event_common(cpu);
1663 } while (!cpu->unplug || cpu_can_run(cpu));
1664
1665 whpx_destroy_vcpu(cpu);
1666 cpu->created = false;
1667 qemu_cond_signal(&qemu_cpu_cond);
1668 qemu_mutex_unlock_iothread();
1669 rcu_unregister_thread();
Vincent Palatinb0cb0a62017-01-10 11:59:57 +01001670 return NULL;
1671}
1672
1673#ifdef _WIN32
1674static void CALLBACK dummy_apc_func(ULONG_PTR unused)
1675{
1676}
1677#endif
1678
Alex Bennée37257942017-02-23 18:29:14 +00001679/* Multi-threaded TCG
1680 *
1681 * In the multi-threaded case each vCPU has its own thread. The TLS
1682 * variable current_cpu can be used deep in the code to find the
1683 * current CPUState for a given thread.
1684 */
1685
1686static void *qemu_tcg_cpu_thread_fn(void *arg)
1687{
1688 CPUState *cpu = arg;
1689
Emilio G. Cotaf28d0df2018-06-22 13:45:31 -04001690 assert(tcg_enabled());
Alex Bennéebf51c722017-03-30 18:32:29 +01001691 g_assert(!use_icount);
1692
Alex Bennée37257942017-02-23 18:29:14 +00001693 rcu_register_thread();
Emilio G. Cota3468b592017-07-19 18:57:58 -04001694 tcg_register_thread();
Alex Bennée37257942017-02-23 18:29:14 +00001695
1696 qemu_mutex_lock_iothread();
1697 qemu_thread_get_self(cpu->thread);
1698
1699 cpu->thread_id = qemu_get_thread_id();
1700 cpu->created = true;
1701 cpu->can_do_io = 1;
1702 current_cpu = cpu;
1703 qemu_cond_signal(&qemu_cpu_cond);
Richard Henderson9c09a252019-03-14 13:06:29 -07001704 qemu_guest_random_seed_thread_part2(cpu->random_seed);
Alex Bennée37257942017-02-23 18:29:14 +00001705
1706 /* process any pending work */
1707 cpu->exit_request = 1;
1708
Cédric Le Goater54961aa2018-04-25 15:18:28 +02001709 do {
Alex Bennée37257942017-02-23 18:29:14 +00001710 if (cpu_can_run(cpu)) {
1711 int r;
Alex Bennéed759c952018-02-27 12:52:48 +03001712 qemu_mutex_unlock_iothread();
Alex Bennée37257942017-02-23 18:29:14 +00001713 r = tcg_cpu_exec(cpu);
Alex Bennéed759c952018-02-27 12:52:48 +03001714 qemu_mutex_lock_iothread();
Alex Bennée37257942017-02-23 18:29:14 +00001715 switch (r) {
1716 case EXCP_DEBUG:
1717 cpu_handle_guest_debug(cpu);
1718 break;
1719 case EXCP_HALTED:
1720 /* during start-up the vCPU is reset and the thread is
1721 * kicked several times. If we don't ensure we go back
1722 * to sleep in the halted state we won't cleanly
1723 * start-up when the vCPU is enabled.
1724 *
1725 * cpu->halted should ensure we sleep in wait_io_event
1726 */
1727 g_assert(cpu->halted);
1728 break;
Pranith Kumar08e73c42017-02-23 18:29:15 +00001729 case EXCP_ATOMIC:
1730 qemu_mutex_unlock_iothread();
1731 cpu_exec_step_atomic(cpu);
1732 qemu_mutex_lock_iothread();
Alex Bennée37257942017-02-23 18:29:14 +00001733 default:
1734 /* Ignore everything else? */
1735 break;
1736 }
1737 }
1738
Alex Bennée37257942017-02-23 18:29:14 +00001739 atomic_mb_set(&cpu->exit_request, 0);
Paolo Bonzinidb08b682018-01-11 13:53:12 +01001740 qemu_wait_io_event(cpu);
Paolo Bonzini9b0605f2018-01-30 11:05:06 -05001741 } while (!cpu->unplug || cpu_can_run(cpu));
Alex Bennée37257942017-02-23 18:29:14 +00001742
Paolo Bonzini9b0605f2018-01-30 11:05:06 -05001743 qemu_tcg_destroy_vcpu(cpu);
1744 cpu->created = false;
1745 qemu_cond_signal(&qemu_cpu_cond);
1746 qemu_mutex_unlock_iothread();
1747 rcu_unregister_thread();
Alex Bennée37257942017-02-23 18:29:14 +00001748 return NULL;
1749}
1750
Andreas Färber2ff09a42012-05-03 00:23:30 +02001751static void qemu_cpu_kick_thread(CPUState *cpu)
Paolo Bonzinicc015e92011-03-12 17:44:08 +01001752{
1753#ifndef _WIN32
1754 int err;
1755
Paolo Bonzinie0c38212015-08-26 00:19:19 +02001756 if (cpu->thread_kicked) {
1757 return;
Paolo Bonzini9102ded2015-08-18 06:52:09 -07001758 }
Paolo Bonzinie0c38212015-08-26 00:19:19 +02001759 cpu->thread_kicked = true;
Andreas Färber814e6122012-05-02 17:00:37 +02001760 err = pthread_kill(cpu->thread->thread, SIG_IPI);
Laurent Vivierd455ebc2019-01-02 15:16:03 +01001761 if (err && err != ESRCH) {
Paolo Bonzinicc015e92011-03-12 17:44:08 +01001762 fprintf(stderr, "qemu:%s: %s", __func__, strerror(err));
1763 exit(1);
1764 }
1765#else /* _WIN32 */
Vincent Palatinb0cb0a62017-01-10 11:59:57 +01001766 if (!qemu_cpu_is_self(cpu)) {
Justin Terry (VM)19306802018-01-22 13:07:49 -08001767 if (whpx_enabled()) {
1768 whpx_vcpu_kick(cpu);
1769 } else if (!QueueUserAPC(dummy_apc_func, cpu->hThread, 0)) {
Vincent Palatinb0cb0a62017-01-10 11:59:57 +01001770 fprintf(stderr, "%s: QueueUserAPC failed with error %lu\n",
1771 __func__, GetLastError());
1772 exit(1);
1773 }
1774 }
Paolo Bonzinicc015e92011-03-12 17:44:08 +01001775#endif
1776}
1777
Andreas Färberc08d7422012-05-03 04:34:15 +02001778void qemu_cpu_kick(CPUState *cpu)
Blue Swirl296af7c2010-03-29 19:23:50 +00001779{
Andreas Färberf5c121b2012-05-03 01:22:49 +02001780 qemu_cond_broadcast(cpu->halt_cond);
Paolo Bonzinie0c38212015-08-26 00:19:19 +02001781 if (tcg_enabled()) {
Alex Bennéee8f22f72019-10-01 17:04:26 +01001782 if (qemu_tcg_mttcg_enabled()) {
1783 cpu_exit(cpu);
1784 } else {
1785 qemu_cpu_kick_rr_cpus();
1786 }
Paolo Bonzinie0c38212015-08-26 00:19:19 +02001787 } else {
Vincent Palatinb0cb0a62017-01-10 11:59:57 +01001788 if (hax_enabled()) {
1789 /*
1790 * FIXME: race condition with the exit_request check in
1791 * hax_vcpu_hax_exec
1792 */
1793 cpu->exit_request = 1;
1794 }
Paolo Bonzinie0c38212015-08-26 00:19:19 +02001795 qemu_cpu_kick_thread(cpu);
1796 }
Blue Swirl296af7c2010-03-29 19:23:50 +00001797}
1798
Jan Kiszka46d62fa2011-02-01 22:15:59 +01001799void qemu_cpu_kick_self(void)
1800{
Andreas Färber4917cf42013-05-27 05:17:50 +02001801 assert(current_cpu);
Paolo Bonzini9102ded2015-08-18 06:52:09 -07001802 qemu_cpu_kick_thread(current_cpu);
Blue Swirl296af7c2010-03-29 19:23:50 +00001803}
1804
Andreas Färber60e82572012-05-02 22:23:49 +02001805bool qemu_cpu_is_self(CPUState *cpu)
Blue Swirl296af7c2010-03-29 19:23:50 +00001806{
Andreas Färber814e6122012-05-02 17:00:37 +02001807 return qemu_thread_is_self(cpu->thread);
Blue Swirl296af7c2010-03-29 19:23:50 +00001808}
1809
Paolo Bonzini79e2b9a2015-01-21 12:09:14 +01001810bool qemu_in_vcpu_thread(void)
Juan Quintelaaa723c22012-09-18 16:30:11 +02001811{
Andreas Färber4917cf42013-05-27 05:17:50 +02001812 return current_cpu && qemu_cpu_is_self(current_cpu);
Juan Quintelaaa723c22012-09-18 16:30:11 +02001813}
1814
Paolo Bonziniafbe7052015-06-18 18:47:19 +02001815static __thread bool iothread_locked = false;
1816
1817bool qemu_mutex_iothread_locked(void)
1818{
1819 return iothread_locked;
1820}
1821
Emilio G. Cotacb764d02017-10-28 02:16:41 -04001822/*
1823 * The BQL is taken from so many places that it is worth profiling the
1824 * callers directly, instead of funneling them all through a single function.
1825 */
1826void qemu_mutex_lock_iothread_impl(const char *file, int line)
Blue Swirl296af7c2010-03-29 19:23:50 +00001827{
Emilio G. Cotacb764d02017-10-28 02:16:41 -04001828 QemuMutexLockFunc bql_lock = atomic_read(&qemu_bql_mutex_lock_func);
1829
Jan Kiszka8d04fb52017-02-23 18:29:11 +00001830 g_assert(!qemu_mutex_iothread_locked());
Emilio G. Cotacb764d02017-10-28 02:16:41 -04001831 bql_lock(&qemu_global_mutex, file, line);
Paolo Bonziniafbe7052015-06-18 18:47:19 +02001832 iothread_locked = true;
Blue Swirl296af7c2010-03-29 19:23:50 +00001833}
1834
1835void qemu_mutex_unlock_iothread(void)
1836{
Jan Kiszka8d04fb52017-02-23 18:29:11 +00001837 g_assert(qemu_mutex_iothread_locked());
Paolo Bonziniafbe7052015-06-18 18:47:19 +02001838 iothread_locked = false;
Blue Swirl296af7c2010-03-29 19:23:50 +00001839 qemu_mutex_unlock(&qemu_global_mutex);
1840}
1841
Alex Bennéee8faee02016-10-27 16:09:58 +01001842static bool all_vcpus_paused(void)
Blue Swirl296af7c2010-03-29 19:23:50 +00001843{
Andreas Färberbdc44642013-06-24 23:50:24 +02001844 CPUState *cpu;
Blue Swirl296af7c2010-03-29 19:23:50 +00001845
Andreas Färberbdc44642013-06-24 23:50:24 +02001846 CPU_FOREACH(cpu) {
Andreas Färber182735e2013-05-29 22:29:20 +02001847 if (!cpu->stopped) {
Alex Bennéee8faee02016-10-27 16:09:58 +01001848 return false;
Jan Kiszka0ab07c62011-02-07 12:19:14 +01001849 }
Blue Swirl296af7c2010-03-29 19:23:50 +00001850 }
1851
Alex Bennéee8faee02016-10-27 16:09:58 +01001852 return true;
Blue Swirl296af7c2010-03-29 19:23:50 +00001853}
1854
1855void pause_all_vcpus(void)
1856{
Andreas Färberbdc44642013-06-24 23:50:24 +02001857 CPUState *cpu;
Blue Swirl296af7c2010-03-29 19:23:50 +00001858
Alex Bligh40daca52013-08-21 16:03:02 +01001859 qemu_clock_enable(QEMU_CLOCK_VIRTUAL, false);
Andreas Färberbdc44642013-06-24 23:50:24 +02001860 CPU_FOREACH(cpu) {
David Hildenbrandebd05fe2017-11-29 20:12:15 +01001861 if (qemu_cpu_is_self(cpu)) {
1862 qemu_cpu_stop(cpu, true);
1863 } else {
1864 cpu->stop = true;
1865 qemu_cpu_kick(cpu);
1866 }
Jan Kiszkad798e972012-02-17 18:31:16 +01001867 }
1868
Alex Bennéed759c952018-02-27 12:52:48 +03001869 /* We need to drop the replay_lock so any vCPU threads woken up
1870 * can finish their replay tasks
1871 */
1872 replay_mutex_unlock();
1873
Blue Swirl296af7c2010-03-29 19:23:50 +00001874 while (!all_vcpus_paused()) {
Paolo Bonzinibe7d6c52011-03-12 17:44:02 +01001875 qemu_cond_wait(&qemu_pause_cond, &qemu_global_mutex);
Andreas Färberbdc44642013-06-24 23:50:24 +02001876 CPU_FOREACH(cpu) {
Andreas Färber182735e2013-05-29 22:29:20 +02001877 qemu_cpu_kick(cpu);
Blue Swirl296af7c2010-03-29 19:23:50 +00001878 }
1879 }
Alex Bennéed759c952018-02-27 12:52:48 +03001880
1881 qemu_mutex_unlock_iothread();
1882 replay_mutex_lock();
1883 qemu_mutex_lock_iothread();
Blue Swirl296af7c2010-03-29 19:23:50 +00001884}
1885
Igor Mammedov29936832013-04-23 10:29:37 +02001886void cpu_resume(CPUState *cpu)
1887{
1888 cpu->stop = false;
1889 cpu->stopped = false;
1890 qemu_cpu_kick(cpu);
1891}
1892
Blue Swirl296af7c2010-03-29 19:23:50 +00001893void resume_all_vcpus(void)
1894{
Andreas Färberbdc44642013-06-24 23:50:24 +02001895 CPUState *cpu;
Blue Swirl296af7c2010-03-29 19:23:50 +00001896
Alex Bligh40daca52013-08-21 16:03:02 +01001897 qemu_clock_enable(QEMU_CLOCK_VIRTUAL, true);
Andreas Färberbdc44642013-06-24 23:50:24 +02001898 CPU_FOREACH(cpu) {
Andreas Färber182735e2013-05-29 22:29:20 +02001899 cpu_resume(cpu);
Blue Swirl296af7c2010-03-29 19:23:50 +00001900 }
1901}
1902
Paolo Bonzinidbadee42018-01-30 16:40:12 +01001903void cpu_remove_sync(CPUState *cpu)
Gu Zheng4c055ab2016-05-12 09:18:13 +05301904{
1905 cpu->stop = true;
1906 cpu->unplug = true;
1907 qemu_cpu_kick(cpu);
Paolo Bonzinidbadee42018-01-30 16:40:12 +01001908 qemu_mutex_unlock_iothread();
1909 qemu_thread_join(cpu->thread);
1910 qemu_mutex_lock_iothread();
Bharata B Rao2c579042016-05-12 09:18:14 +05301911}
1912
Dr. David Alan Gilbert49001162014-01-30 10:20:32 +00001913/* For temporary buffers for forming a name */
1914#define VCPU_THREAD_NAME_SIZE 16
1915
Andreas Färbere5ab30a2012-05-03 01:50:44 +02001916static void qemu_tcg_init_vcpu(CPUState *cpu)
Blue Swirl296af7c2010-03-29 19:23:50 +00001917{
Dr. David Alan Gilbert49001162014-01-30 10:20:32 +00001918 char thread_name[VCPU_THREAD_NAME_SIZE];
Alex Bennée37257942017-02-23 18:29:14 +00001919 static QemuCond *single_tcg_halt_cond;
1920 static QemuThread *single_tcg_cpu_thread;
Emilio G. Cotae8feb962017-07-07 19:24:20 -04001921 static int tcg_region_inited;
1922
Emilio G. Cotaf28d0df2018-06-22 13:45:31 -04001923 assert(tcg_enabled());
Emilio G. Cotae8feb962017-07-07 19:24:20 -04001924 /*
1925 * Initialize TCG regions--once. Now is a good time, because:
1926 * (1) TCG's init context, prologue and target globals have been set up.
1927 * (2) qemu_tcg_mttcg_enabled() works now (TCG init code runs before the
1928 * -accel flag is processed, so the check doesn't work then).
1929 */
1930 if (!tcg_region_inited) {
1931 tcg_region_inited = 1;
1932 tcg_region_init();
1933 }
Dr. David Alan Gilbert49001162014-01-30 10:20:32 +00001934
Alex Bennée37257942017-02-23 18:29:14 +00001935 if (qemu_tcg_mttcg_enabled() || !single_tcg_cpu_thread) {
Andreas Färber814e6122012-05-02 17:00:37 +02001936 cpu->thread = g_malloc0(sizeof(QemuThread));
Andreas Färberf5c121b2012-05-03 01:22:49 +02001937 cpu->halt_cond = g_malloc0(sizeof(QemuCond));
1938 qemu_cond_init(cpu->halt_cond);
Alex Bennée37257942017-02-23 18:29:14 +00001939
1940 if (qemu_tcg_mttcg_enabled()) {
1941 /* create a thread per vCPU with TCG (MTTCG) */
1942 parallel_cpus = true;
1943 snprintf(thread_name, VCPU_THREAD_NAME_SIZE, "CPU %d/TCG",
Dr. David Alan Gilbert49001162014-01-30 10:20:32 +00001944 cpu->cpu_index);
Alex Bennée37257942017-02-23 18:29:14 +00001945
1946 qemu_thread_create(cpu->thread, thread_name, qemu_tcg_cpu_thread_fn,
1947 cpu, QEMU_THREAD_JOINABLE);
1948
1949 } else {
1950 /* share a single thread for all cpus with TCG */
1951 snprintf(thread_name, VCPU_THREAD_NAME_SIZE, "ALL CPUs/TCG");
1952 qemu_thread_create(cpu->thread, thread_name,
1953 qemu_tcg_rr_cpu_thread_fn,
1954 cpu, QEMU_THREAD_JOINABLE);
1955
1956 single_tcg_halt_cond = cpu->halt_cond;
1957 single_tcg_cpu_thread = cpu->thread;
1958 }
Paolo Bonzini1ecf47b2011-12-13 13:43:52 +01001959#ifdef _WIN32
Andreas Färber814e6122012-05-02 17:00:37 +02001960 cpu->hThread = qemu_thread_get_handle(cpu->thread);
Paolo Bonzini1ecf47b2011-12-13 13:43:52 +01001961#endif
Blue Swirl296af7c2010-03-29 19:23:50 +00001962 } else {
Alex Bennée37257942017-02-23 18:29:14 +00001963 /* For non-MTTCG cases we share the thread */
1964 cpu->thread = single_tcg_cpu_thread;
1965 cpu->halt_cond = single_tcg_halt_cond;
David Hildenbranda3421732018-02-09 20:52:37 +01001966 cpu->thread_id = first_cpu->thread_id;
1967 cpu->can_do_io = 1;
1968 cpu->created = true;
Blue Swirl296af7c2010-03-29 19:23:50 +00001969 }
1970}
1971
Vincent Palatinb0cb0a62017-01-10 11:59:57 +01001972static void qemu_hax_start_vcpu(CPUState *cpu)
1973{
1974 char thread_name[VCPU_THREAD_NAME_SIZE];
1975
1976 cpu->thread = g_malloc0(sizeof(QemuThread));
1977 cpu->halt_cond = g_malloc0(sizeof(QemuCond));
1978 qemu_cond_init(cpu->halt_cond);
1979
1980 snprintf(thread_name, VCPU_THREAD_NAME_SIZE, "CPU %d/HAX",
1981 cpu->cpu_index);
1982 qemu_thread_create(cpu->thread, thread_name, qemu_hax_cpu_thread_fn,
1983 cpu, QEMU_THREAD_JOINABLE);
1984#ifdef _WIN32
1985 cpu->hThread = qemu_thread_get_handle(cpu->thread);
1986#endif
Vincent Palatinb0cb0a62017-01-10 11:59:57 +01001987}
1988
Andreas Färber48a106b2013-05-27 02:20:39 +02001989static void qemu_kvm_start_vcpu(CPUState *cpu)
Blue Swirl296af7c2010-03-29 19:23:50 +00001990{
Dr. David Alan Gilbert49001162014-01-30 10:20:32 +00001991 char thread_name[VCPU_THREAD_NAME_SIZE];
1992
Andreas Färber814e6122012-05-02 17:00:37 +02001993 cpu->thread = g_malloc0(sizeof(QemuThread));
Andreas Färberf5c121b2012-05-03 01:22:49 +02001994 cpu->halt_cond = g_malloc0(sizeof(QemuCond));
1995 qemu_cond_init(cpu->halt_cond);
Dr. David Alan Gilbert49001162014-01-30 10:20:32 +00001996 snprintf(thread_name, VCPU_THREAD_NAME_SIZE, "CPU %d/KVM",
1997 cpu->cpu_index);
1998 qemu_thread_create(cpu->thread, thread_name, qemu_kvm_cpu_thread_fn,
1999 cpu, QEMU_THREAD_JOINABLE);
Blue Swirl296af7c2010-03-29 19:23:50 +00002000}
2001
Sergio Andres Gomez Del Realc97d6d22017-09-13 04:05:09 -05002002static void qemu_hvf_start_vcpu(CPUState *cpu)
2003{
2004 char thread_name[VCPU_THREAD_NAME_SIZE];
2005
2006 /* HVF currently does not support TCG, and only runs in
2007 * unrestricted-guest mode. */
2008 assert(hvf_enabled());
2009
2010 cpu->thread = g_malloc0(sizeof(QemuThread));
2011 cpu->halt_cond = g_malloc0(sizeof(QemuCond));
2012 qemu_cond_init(cpu->halt_cond);
2013
2014 snprintf(thread_name, VCPU_THREAD_NAME_SIZE, "CPU %d/HVF",
2015 cpu->cpu_index);
2016 qemu_thread_create(cpu->thread, thread_name, qemu_hvf_cpu_thread_fn,
2017 cpu, QEMU_THREAD_JOINABLE);
Sergio Andres Gomez Del Realc97d6d22017-09-13 04:05:09 -05002018}
2019
Justin Terry (VM)19306802018-01-22 13:07:49 -08002020static void qemu_whpx_start_vcpu(CPUState *cpu)
2021{
2022 char thread_name[VCPU_THREAD_NAME_SIZE];
2023
2024 cpu->thread = g_malloc0(sizeof(QemuThread));
2025 cpu->halt_cond = g_malloc0(sizeof(QemuCond));
2026 qemu_cond_init(cpu->halt_cond);
2027 snprintf(thread_name, VCPU_THREAD_NAME_SIZE, "CPU %d/WHPX",
2028 cpu->cpu_index);
2029 qemu_thread_create(cpu->thread, thread_name, qemu_whpx_cpu_thread_fn,
2030 cpu, QEMU_THREAD_JOINABLE);
2031#ifdef _WIN32
2032 cpu->hThread = qemu_thread_get_handle(cpu->thread);
2033#endif
Justin Terry (VM)19306802018-01-22 13:07:49 -08002034}
2035
Andreas Färber10a90212013-05-27 02:24:35 +02002036static void qemu_dummy_start_vcpu(CPUState *cpu)
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +02002037{
Dr. David Alan Gilbert49001162014-01-30 10:20:32 +00002038 char thread_name[VCPU_THREAD_NAME_SIZE];
2039
Andreas Färber814e6122012-05-02 17:00:37 +02002040 cpu->thread = g_malloc0(sizeof(QemuThread));
Andreas Färberf5c121b2012-05-03 01:22:49 +02002041 cpu->halt_cond = g_malloc0(sizeof(QemuCond));
2042 qemu_cond_init(cpu->halt_cond);
Dr. David Alan Gilbert49001162014-01-30 10:20:32 +00002043 snprintf(thread_name, VCPU_THREAD_NAME_SIZE, "CPU %d/DUMMY",
2044 cpu->cpu_index);
2045 qemu_thread_create(cpu->thread, thread_name, qemu_dummy_cpu_thread_fn, cpu,
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +02002046 QEMU_THREAD_JOINABLE);
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +02002047}
2048
Andreas Färberc643bed2013-05-27 03:23:24 +02002049void qemu_init_vcpu(CPUState *cpu)
Blue Swirl296af7c2010-03-29 19:23:50 +00002050{
Like Xu5cc87672019-05-19 04:54:21 +08002051 MachineState *ms = MACHINE(qdev_get_machine());
2052
2053 cpu->nr_cores = ms->smp.cores;
2054 cpu->nr_threads = ms->smp.threads;
Andreas Färberf324e762012-05-02 23:26:21 +02002055 cpu->stopped = true;
Richard Henderson9c09a252019-03-14 13:06:29 -07002056 cpu->random_seed = qemu_guest_random_seed_thread_part1();
Peter Maydell56943e82016-01-21 14:15:04 +00002057
2058 if (!cpu->as) {
2059 /* If the target cpu hasn't set up any address spaces itself,
2060 * give it the default one.
2061 */
Peter Maydell12ebc9a2016-01-21 14:15:04 +00002062 cpu->num_ases = 1;
Peter Xu80ceb072017-11-23 17:23:32 +08002063 cpu_address_space_init(cpu, 0, "cpu-memory", cpu->memory);
Peter Maydell56943e82016-01-21 14:15:04 +00002064 }
2065
Jan Kiszka0ab07c62011-02-07 12:19:14 +01002066 if (kvm_enabled()) {
Andreas Färber48a106b2013-05-27 02:20:39 +02002067 qemu_kvm_start_vcpu(cpu);
Vincent Palatinb0cb0a62017-01-10 11:59:57 +01002068 } else if (hax_enabled()) {
2069 qemu_hax_start_vcpu(cpu);
Sergio Andres Gomez Del Realc97d6d22017-09-13 04:05:09 -05002070 } else if (hvf_enabled()) {
2071 qemu_hvf_start_vcpu(cpu);
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +02002072 } else if (tcg_enabled()) {
Andreas Färbere5ab30a2012-05-03 01:50:44 +02002073 qemu_tcg_init_vcpu(cpu);
Justin Terry (VM)19306802018-01-22 13:07:49 -08002074 } else if (whpx_enabled()) {
2075 qemu_whpx_start_vcpu(cpu);
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +02002076 } else {
Andreas Färber10a90212013-05-27 02:24:35 +02002077 qemu_dummy_start_vcpu(cpu);
Jan Kiszka0ab07c62011-02-07 12:19:14 +01002078 }
David Hildenbrand81e96312018-02-09 20:52:38 +01002079
2080 while (!cpu->created) {
2081 qemu_cond_wait(&qemu_cpu_cond, &qemu_global_mutex);
2082 }
Blue Swirl296af7c2010-03-29 19:23:50 +00002083}
2084
Jan Kiszkab4a3d962011-02-01 22:15:43 +01002085void cpu_stop_current(void)
Blue Swirl296af7c2010-03-29 19:23:50 +00002086{
Andreas Färber4917cf42013-05-27 05:17:50 +02002087 if (current_cpu) {
Peter Maydell0ec7e672019-01-07 15:23:47 +00002088 current_cpu->stop = true;
2089 cpu_exit(current_cpu);
Jan Kiszkab4a3d962011-02-01 22:15:43 +01002090 }
Blue Swirl296af7c2010-03-29 19:23:50 +00002091}
2092
Kevin Wolf56983462013-07-05 13:49:54 +02002093int vm_stop(RunState state)
Blue Swirl296af7c2010-03-29 19:23:50 +00002094{
Juan Quintelaaa723c22012-09-18 16:30:11 +02002095 if (qemu_in_vcpu_thread()) {
Paolo Bonzini74892d22014-06-05 14:53:58 +02002096 qemu_system_vmstop_request_prepare();
Luiz Capitulino1dfb4dd2011-07-29 14:26:33 -03002097 qemu_system_vmstop_request(state);
Blue Swirl296af7c2010-03-29 19:23:50 +00002098 /*
2099 * FIXME: should not return to device code in case
2100 * vm_stop() has been requested.
2101 */
Jan Kiszkab4a3d962011-02-01 22:15:43 +01002102 cpu_stop_current();
Kevin Wolf56983462013-07-05 13:49:54 +02002103 return 0;
Blue Swirl296af7c2010-03-29 19:23:50 +00002104 }
Kevin Wolf56983462013-07-05 13:49:54 +02002105
Stefan Hajnoczi4486e892018-03-07 14:42:05 +00002106 return do_vm_stop(state, true);
Blue Swirl296af7c2010-03-29 19:23:50 +00002107}
2108
Claudio Imbrenda2d76e822017-02-14 18:07:47 +01002109/**
2110 * Prepare for (re)starting the VM.
2111 * Returns -1 if the vCPUs are not to be restarted (e.g. if they are already
2112 * running or in case of an error condition), 0 otherwise.
2113 */
2114int vm_prepare_start(void)
2115{
2116 RunState requested;
Claudio Imbrenda2d76e822017-02-14 18:07:47 +01002117
2118 qemu_vmstop_requested(&requested);
2119 if (runstate_is_running() && requested == RUN_STATE__MAX) {
2120 return -1;
2121 }
2122
2123 /* Ensure that a STOP/RESUME pair of events is emitted if a
2124 * vmstop request was pending. The BLOCK_IO_ERROR event, for
2125 * example, according to documentation is always followed by
2126 * the STOP event.
2127 */
2128 if (runstate_is_running()) {
Peter Xu3ab72382018-08-15 21:37:37 +08002129 qapi_event_send_stop();
2130 qapi_event_send_resume();
Markus Armbrusterf0561582018-04-23 10:45:18 +02002131 return -1;
Claudio Imbrenda2d76e822017-02-14 18:07:47 +01002132 }
2133
2134 /* We are sending this now, but the CPUs will be resumed shortly later */
Peter Xu3ab72382018-08-15 21:37:37 +08002135 qapi_event_send_resume();
Markus Armbrusterf0561582018-04-23 10:45:18 +02002136
Markus Armbrusterf0561582018-04-23 10:45:18 +02002137 cpu_enable_ticks();
2138 runstate_set(RUN_STATE_RUNNING);
2139 vm_state_notify(1, RUN_STATE_RUNNING);
2140 return 0;
Claudio Imbrenda2d76e822017-02-14 18:07:47 +01002141}
2142
2143void vm_start(void)
2144{
2145 if (!vm_prepare_start()) {
2146 resume_all_vcpus();
2147 }
2148}
2149
Luiz Capitulino8a9236f2011-10-14 11:18:09 -03002150/* does a state transition even if the VM is already stopped,
2151 current state is forgotten forever */
Kevin Wolf56983462013-07-05 13:49:54 +02002152int vm_stop_force_state(RunState state)
Luiz Capitulino8a9236f2011-10-14 11:18:09 -03002153{
2154 if (runstate_is_running()) {
Kevin Wolf56983462013-07-05 13:49:54 +02002155 return vm_stop(state);
Luiz Capitulino8a9236f2011-10-14 11:18:09 -03002156 } else {
2157 runstate_set(state);
Wen Congyangb2780d32015-11-20 17:34:38 +08002158
2159 bdrv_drain_all();
Kevin Wolf594a45c2013-07-18 14:52:19 +02002160 /* Make sure to return an error if the flush in a previous vm_stop()
2161 * failed. */
John Snow22af08e2016-09-22 21:45:51 -04002162 return bdrv_flush_all();
Luiz Capitulino8a9236f2011-10-14 11:18:09 -03002163 }
2164}
2165
Markus Armbruster04424282019-04-17 21:17:57 +02002166void list_cpus(const char *optarg)
Blue Swirl262353c2010-05-04 19:55:35 +00002167{
2168 /* XXX: implement xxx_cpu_list for targets that still miss it */
Peter Maydelle916cbf2012-09-05 17:41:08 -03002169#if defined(cpu_list)
Markus Armbruster04424282019-04-17 21:17:57 +02002170 cpu_list();
Blue Swirl262353c2010-05-04 19:55:35 +00002171#endif
2172}
Luiz Capitulinode0b36b2011-09-21 16:38:35 -03002173
Luiz Capitulino0cfd6a92011-11-22 16:32:37 -02002174void qmp_memsave(int64_t addr, int64_t size, const char *filename,
2175 bool has_cpu, int64_t cpu_index, Error **errp)
2176{
2177 FILE *f;
2178 uint32_t l;
Andreas Färber55e5c282012-12-17 06:18:02 +01002179 CPUState *cpu;
Luiz Capitulino0cfd6a92011-11-22 16:32:37 -02002180 uint8_t buf[1024];
Borislav Petkov0dc9daf2015-02-08 13:14:38 +01002181 int64_t orig_addr = addr, orig_size = size;
Luiz Capitulino0cfd6a92011-11-22 16:32:37 -02002182
2183 if (!has_cpu) {
2184 cpu_index = 0;
2185 }
2186
Andreas Färber151d1322013-02-15 15:41:49 +01002187 cpu = qemu_get_cpu(cpu_index);
2188 if (cpu == NULL) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01002189 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "cpu-index",
2190 "a CPU number");
Luiz Capitulino0cfd6a92011-11-22 16:32:37 -02002191 return;
2192 }
2193
2194 f = fopen(filename, "wb");
2195 if (!f) {
Luiz Capitulino618da852013-06-07 14:35:06 -04002196 error_setg_file_open(errp, errno, filename);
Luiz Capitulino0cfd6a92011-11-22 16:32:37 -02002197 return;
2198 }
2199
2200 while (size != 0) {
2201 l = sizeof(buf);
2202 if (l > size)
2203 l = size;
Aneesh Kumar K.V2f4d0f52013-10-01 21:49:30 +05302204 if (cpu_memory_rw_debug(cpu, addr, buf, l, 0) != 0) {
Borislav Petkov0dc9daf2015-02-08 13:14:38 +01002205 error_setg(errp, "Invalid addr 0x%016" PRIx64 "/size %" PRId64
2206 " specified", orig_addr, orig_size);
Aneesh Kumar K.V2f4d0f52013-10-01 21:49:30 +05302207 goto exit;
2208 }
Luiz Capitulino0cfd6a92011-11-22 16:32:37 -02002209 if (fwrite(buf, 1, l, f) != l) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01002210 error_setg(errp, QERR_IO_ERROR);
Luiz Capitulino0cfd6a92011-11-22 16:32:37 -02002211 goto exit;
2212 }
2213 addr += l;
2214 size -= l;
2215 }
2216
2217exit:
2218 fclose(f);
2219}
Luiz Capitulino6d3962b2011-11-22 17:26:46 -02002220
2221void qmp_pmemsave(int64_t addr, int64_t size, const char *filename,
2222 Error **errp)
2223{
2224 FILE *f;
2225 uint32_t l;
2226 uint8_t buf[1024];
2227
2228 f = fopen(filename, "wb");
2229 if (!f) {
Luiz Capitulino618da852013-06-07 14:35:06 -04002230 error_setg_file_open(errp, errno, filename);
Luiz Capitulino6d3962b2011-11-22 17:26:46 -02002231 return;
2232 }
2233
2234 while (size != 0) {
2235 l = sizeof(buf);
2236 if (l > size)
2237 l = size;
Stefan Weileb6282f2014-04-07 20:28:23 +02002238 cpu_physical_memory_read(addr, buf, l);
Luiz Capitulino6d3962b2011-11-22 17:26:46 -02002239 if (fwrite(buf, 1, l, f) != l) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01002240 error_setg(errp, QERR_IO_ERROR);
Luiz Capitulino6d3962b2011-11-22 17:26:46 -02002241 goto exit;
2242 }
2243 addr += l;
2244 size -= l;
2245 }
2246
2247exit:
2248 fclose(f);
2249}
Luiz Capitulinoab49ab52011-11-23 12:55:53 -02002250
2251void qmp_inject_nmi(Error **errp)
2252{
Alexey Kardashevskiy9cb805f2014-08-20 22:16:33 +10002253 nmi_monitor_handle(monitor_get_cpu_index(), errp);
Luiz Capitulinoab49ab52011-11-23 12:55:53 -02002254}
Sebastian Tanase27498be2014-07-25 11:56:33 +02002255
Markus Armbruster76c86612019-04-17 21:17:53 +02002256void dump_drift_info(void)
Sebastian Tanase27498be2014-07-25 11:56:33 +02002257{
2258 if (!use_icount) {
2259 return;
2260 }
2261
Markus Armbruster76c86612019-04-17 21:17:53 +02002262 qemu_printf("Host - Guest clock %"PRIi64" ms\n",
Sebastian Tanase27498be2014-07-25 11:56:33 +02002263 (cpu_get_clock() - cpu_get_icount())/SCALE_MS);
2264 if (icount_align_option) {
Markus Armbruster76c86612019-04-17 21:17:53 +02002265 qemu_printf("Max guest delay %"PRIi64" ms\n",
2266 -max_delay / SCALE_MS);
2267 qemu_printf("Max guest advance %"PRIi64" ms\n",
2268 max_advance / SCALE_MS);
Sebastian Tanase27498be2014-07-25 11:56:33 +02002269 } else {
Markus Armbruster76c86612019-04-17 21:17:53 +02002270 qemu_printf("Max guest delay NA\n");
2271 qemu_printf("Max guest advance NA\n");
Sebastian Tanase27498be2014-07-25 11:56:33 +02002272 }
2273}