blob: 740b8dc3f808b320cce92c434af93f8bc315eb79 [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
25/* Needed early for CONFIG_BSD etc. */
Peter Maydell7b31bbc2016-01-26 18:16:56 +000026#include "qemu/osdep.h"
Paolo Bonzini33c11872016-03-15 16:58:45 +010027#include "qemu-common.h"
KONRAD Frederic8d4e9142017-02-23 18:29:08 +000028#include "qemu/config-file.h"
Paolo Bonzini33c11872016-03-15 16:58:45 +010029#include "cpu.h"
Paolo Bonzini83c90892012-12-17 18:19:49 +010030#include "monitor/monitor.h"
Wenchao Xiaa4e15de2014-06-18 08:43:36 +020031#include "qapi/qmp/qerror.h"
Markus Armbrusterd49b6832015-03-17 18:29:20 +010032#include "qemu/error-report.h"
Paolo Bonzini9c17d612012-12-17 18:20:04 +010033#include "sysemu/sysemu.h"
Max Reitzda31d592016-03-16 19:54:32 +010034#include "sysemu/block-backend.h"
Paolo Bonzini022c62c2012-12-17 18:19:49 +010035#include "exec/gdbstub.h"
Paolo Bonzini9c17d612012-12-17 18:20:04 +010036#include "sysemu/dma.h"
Vincent Palatinb3946622017-01-10 11:59:55 +010037#include "sysemu/hw_accel.h"
Paolo Bonzini9c17d612012-12-17 18:20:04 +010038#include "sysemu/kvm.h"
Vincent Palatinb0cb0a62017-01-10 11:59:57 +010039#include "sysemu/hax.h"
Luiz Capitulinode0b36b2011-09-21 16:38:35 -030040#include "qmp-commands.h"
Paolo Bonzini63c91552016-03-15 13:18:37 +010041#include "exec/exec-all.h"
Blue Swirl296af7c2010-03-29 19:23:50 +000042
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010043#include "qemu/thread.h"
Paolo Bonzini9c17d612012-12-17 18:20:04 +010044#include "sysemu/cpus.h"
45#include "sysemu/qtest.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010046#include "qemu/main-loop.h"
47#include "qemu/bitmap.h"
Liu Ping Fancb365642013-09-25 14:20:58 +080048#include "qemu/seqlock.h"
KONRAD Frederic8d4e9142017-02-23 18:29:08 +000049#include "tcg.h"
Wenchao Xiaa4e15de2014-06-18 08:43:36 +020050#include "qapi-event.h"
Alexey Kardashevskiy9cb805f2014-08-20 22:16:33 +100051#include "hw/nmi.h"
Pavel Dovgalyuk8b427042015-09-17 19:24:05 +030052#include "sysemu/replay.h"
Jan Kiszka0ff0fc12011-06-23 10:15:55 +020053
Jan Kiszka6d9cb732011-02-01 22:15:58 +010054#ifdef CONFIG_LINUX
55
56#include <sys/prctl.h>
57
Marcelo Tosattic0532a72010-10-11 15:31:21 -030058#ifndef PR_MCE_KILL
59#define PR_MCE_KILL 33
60#endif
61
Jan Kiszka6d9cb732011-02-01 22:15:58 +010062#ifndef PR_MCE_KILL_SET
63#define PR_MCE_KILL_SET 1
64#endif
65
66#ifndef PR_MCE_KILL_EARLY
67#define PR_MCE_KILL_EARLY 1
68#endif
69
70#endif /* CONFIG_LINUX */
71
Sebastian Tanase27498be2014-07-25 11:56:33 +020072int64_t max_delay;
73int64_t max_advance;
Blue Swirl296af7c2010-03-29 19:23:50 +000074
Jason J. Herne2adcc852015-09-08 13:12:33 -040075/* vcpu throttling controls */
76static QEMUTimer *throttle_timer;
77static unsigned int throttle_percentage;
78
79#define CPU_THROTTLE_PCT_MIN 1
80#define CPU_THROTTLE_PCT_MAX 99
81#define CPU_THROTTLE_TIMESLICE_NS 10000000
82
Tiejun Chen321bc0b2013-08-02 09:43:09 +080083bool cpu_is_stopped(CPUState *cpu)
84{
85 return cpu->stopped || !runstate_is_running();
86}
87
Andreas Färbera98ae1d2013-05-26 23:21:08 +020088static bool cpu_thread_is_idle(CPUState *cpu)
Peter Maydellac873f12012-07-19 16:52:27 +010089{
Andreas Färberc64ca812012-05-03 02:11:45 +020090 if (cpu->stop || cpu->queued_work_first) {
Peter Maydellac873f12012-07-19 16:52:27 +010091 return false;
92 }
Tiejun Chen321bc0b2013-08-02 09:43:09 +080093 if (cpu_is_stopped(cpu)) {
Peter Maydellac873f12012-07-19 16:52:27 +010094 return true;
95 }
Andreas Färber8c2e1b02013-08-25 18:53:55 +020096 if (!cpu->halted || cpu_has_work(cpu) ||
Alexander Graf215e79c2013-04-24 22:24:12 +020097 kvm_halt_in_kernel()) {
Peter Maydellac873f12012-07-19 16:52:27 +010098 return false;
99 }
100 return true;
101}
102
103static bool all_cpu_threads_idle(void)
104{
Andreas Färber182735e2013-05-29 22:29:20 +0200105 CPUState *cpu;
Peter Maydellac873f12012-07-19 16:52:27 +0100106
Andreas Färberbdc44642013-06-24 23:50:24 +0200107 CPU_FOREACH(cpu) {
Andreas Färber182735e2013-05-29 22:29:20 +0200108 if (!cpu_thread_is_idle(cpu)) {
Peter Maydellac873f12012-07-19 16:52:27 +0100109 return false;
110 }
111 }
112 return true;
113}
114
Blue Swirl296af7c2010-03-29 19:23:50 +0000115/***********************************************************/
Paolo Bonzini946fb272011-09-12 13:57:37 +0200116/* guest cycle counter */
117
Paolo Bonzinia3270e12013-10-07 17:18:15 +0200118/* Protected by TimersState seqlock */
119
Victor CLEMENT5045e9d92015-05-29 17:14:04 +0200120static bool icount_sleep = true;
Sebastian Tanase71468392014-07-23 11:47:50 +0200121static int64_t vm_clock_warp_start = -1;
Paolo Bonzini946fb272011-09-12 13:57:37 +0200122/* Conversion factor from emulated instructions to virtual clock ticks. */
123static int icount_time_shift;
124/* Arbitrarily pick 1MIPS as the minimum allowable speed. */
125#define MAX_ICOUNT_SHIFT 10
Paolo Bonzinia3270e12013-10-07 17:18:15 +0200126
Paolo Bonzini946fb272011-09-12 13:57:37 +0200127static QEMUTimer *icount_rt_timer;
128static QEMUTimer *icount_vm_timer;
129static QEMUTimer *icount_warp_timer;
Paolo Bonzini946fb272011-09-12 13:57:37 +0200130
131typedef struct TimersState {
Liu Ping Fancb365642013-09-25 14:20:58 +0800132 /* Protected by BQL. */
Paolo Bonzini946fb272011-09-12 13:57:37 +0200133 int64_t cpu_ticks_prev;
134 int64_t cpu_ticks_offset;
Liu Ping Fancb365642013-09-25 14:20:58 +0800135
136 /* cpu_clock_offset can be read out of BQL, so protect it with
137 * this lock.
138 */
139 QemuSeqLock vm_clock_seqlock;
Paolo Bonzini946fb272011-09-12 13:57:37 +0200140 int64_t cpu_clock_offset;
141 int32_t cpu_ticks_enabled;
142 int64_t dummy;
KONRAD Fredericc96778b2014-08-01 01:37:09 +0200143
144 /* Compensate for varying guest execution speed. */
145 int64_t qemu_icount_bias;
146 /* Only written by TCG thread */
147 int64_t qemu_icount;
Paolo Bonzini946fb272011-09-12 13:57:37 +0200148} TimersState;
149
Liu Ping Fand9cd4002013-07-21 08:43:00 +0000150static TimersState timers_state;
KONRAD Frederic8d4e9142017-02-23 18:29:08 +0000151bool mttcg_enabled;
152
153/*
154 * We default to false if we know other options have been enabled
155 * which are currently incompatible with MTTCG. Otherwise when each
156 * guest (target) has been updated to support:
157 * - atomic instructions
158 * - memory ordering primitives (barriers)
159 * they can set the appropriate CONFIG flags in ${target}-softmmu.mak
160 *
161 * Once a guest architecture has been converted to the new primitives
162 * there are two remaining limitations to check.
163 *
164 * - The guest can't be oversized (e.g. 64 bit guest on 32 bit host)
165 * - The host must have a stronger memory order than the guest
166 *
167 * It may be possible in future to support strong guests on weak hosts
168 * but that will require tagging all load/stores in a guest with their
169 * implicit memory order requirements which would likely slow things
170 * down a lot.
171 */
172
173static bool check_tcg_memory_orders_compatible(void)
174{
175#if defined(TCG_GUEST_DEFAULT_MO) && defined(TCG_TARGET_DEFAULT_MO)
176 return (TCG_GUEST_DEFAULT_MO & ~TCG_TARGET_DEFAULT_MO) == 0;
177#else
178 return false;
179#endif
180}
181
182static bool default_mttcg_enabled(void)
183{
Alex Bennée83fd9622017-02-27 17:09:01 +0000184 if (use_icount || TCG_OVERSIZED_GUEST) {
KONRAD Frederic8d4e9142017-02-23 18:29:08 +0000185 return false;
186 } else {
187#ifdef TARGET_SUPPORTS_MTTCG
188 return check_tcg_memory_orders_compatible();
189#else
190 return false;
191#endif
192 }
193}
194
195void qemu_tcg_configure(QemuOpts *opts, Error **errp)
196{
197 const char *t = qemu_opt_get(opts, "thread");
198 if (t) {
199 if (strcmp(t, "multi") == 0) {
200 if (TCG_OVERSIZED_GUEST) {
201 error_setg(errp, "No MTTCG when guest word size > hosts");
Alex Bennée83fd9622017-02-27 17:09:01 +0000202 } else if (use_icount) {
203 error_setg(errp, "No MTTCG when icount is enabled");
KONRAD Frederic8d4e9142017-02-23 18:29:08 +0000204 } else {
Nikunj A Dadhania86953502017-04-10 11:36:55 +0530205#ifndef TARGET_SUPPORTS_MTTCG
Alex Bennéec34c7622017-02-28 14:40:17 +0000206 error_report("Guest not yet converted to MTTCG - "
207 "you may get unexpected results");
208#endif
KONRAD Frederic8d4e9142017-02-23 18:29:08 +0000209 if (!check_tcg_memory_orders_compatible()) {
210 error_report("Guest expects a stronger memory ordering "
211 "than the host provides");
Pranith Kumar8cfef892017-03-25 16:19:23 -0400212 error_printf("This may cause strange/hard to debug errors\n");
KONRAD Frederic8d4e9142017-02-23 18:29:08 +0000213 }
214 mttcg_enabled = true;
215 }
216 } else if (strcmp(t, "single") == 0) {
217 mttcg_enabled = false;
218 } else {
219 error_setg(errp, "Invalid 'thread' setting %s", t);
220 }
221 } else {
222 mttcg_enabled = default_mttcg_enabled();
223 }
224}
Paolo Bonzini946fb272011-09-12 13:57:37 +0200225
Alex Bennéee4cd9652017-03-31 16:09:42 +0100226/* The current number of executed instructions is based on what we
227 * originally budgeted minus the current state of the decrementing
228 * icount counters in extra/u16.low.
229 */
230static int64_t cpu_get_icount_executed(CPUState *cpu)
231{
232 return cpu->icount_budget - (cpu->icount_decr.u16.low + cpu->icount_extra);
233}
234
Alex Bennée512d3c82017-04-05 12:32:37 +0100235/*
236 * Update the global shared timer_state.qemu_icount to take into
237 * account executed instructions. This is done by the TCG vCPU
238 * thread so the main-loop can see time has moved forward.
239 */
240void cpu_update_icount(CPUState *cpu)
241{
242 int64_t executed = cpu_get_icount_executed(cpu);
243 cpu->icount_budget -= executed;
244
245#ifdef CONFIG_ATOMIC64
246 atomic_set__nocheck(&timers_state.qemu_icount,
247 atomic_read__nocheck(&timers_state.qemu_icount) +
248 executed);
249#else /* FIXME: we need 64bit atomics to do this safely */
250 timers_state.qemu_icount += executed;
251#endif
252}
253
Pavel Dovgalyuk2a629142014-12-08 10:53:45 +0300254int64_t cpu_get_icount_raw(void)
Paolo Bonzini946fb272011-09-12 13:57:37 +0200255{
Andreas Färber4917cf42013-05-27 05:17:50 +0200256 CPUState *cpu = current_cpu;
Paolo Bonzini946fb272011-09-12 13:57:37 +0200257
Alex Bennée243c5f72017-03-30 18:49:22 +0100258 if (cpu && cpu->running) {
Paolo Bonzini414b15c2015-06-24 14:16:26 +0200259 if (!cpu->can_do_io) {
Pavel Dovgalyuk2a629142014-12-08 10:53:45 +0300260 fprintf(stderr, "Bad icount read\n");
261 exit(1);
Paolo Bonzini946fb272011-09-12 13:57:37 +0200262 }
Alex Bennéee4cd9652017-03-31 16:09:42 +0100263 /* Take into account what has run */
Alex Bennée1d059062017-04-05 10:53:47 +0100264 cpu_update_icount(cpu);
Paolo Bonzini946fb272011-09-12 13:57:37 +0200265 }
Alex Bennée1d059062017-04-05 10:53:47 +0100266#ifdef CONFIG_ATOMIC64
267 return atomic_read__nocheck(&timers_state.qemu_icount);
268#else /* FIXME: we need 64bit atomics to do this safely */
269 return timers_state.qemu_icount;
270#endif
Pavel Dovgalyuk2a629142014-12-08 10:53:45 +0300271}
272
273/* Return the virtual CPU time, based on the instruction counter. */
274static int64_t cpu_get_icount_locked(void)
275{
276 int64_t icount = cpu_get_icount_raw();
KONRAD Frederic3f031312014-08-01 01:37:15 +0200277 return timers_state.qemu_icount_bias + cpu_icount_to_ns(icount);
Paolo Bonzini946fb272011-09-12 13:57:37 +0200278}
279
Paolo Bonzini17a15f12013-10-03 15:17:25 +0200280int64_t cpu_get_icount(void)
281{
282 int64_t icount;
283 unsigned start;
284
285 do {
286 start = seqlock_read_begin(&timers_state.vm_clock_seqlock);
287 icount = cpu_get_icount_locked();
288 } while (seqlock_read_retry(&timers_state.vm_clock_seqlock, start));
289
290 return icount;
291}
292
KONRAD Frederic3f031312014-08-01 01:37:15 +0200293int64_t cpu_icount_to_ns(int64_t icount)
294{
295 return icount << icount_time_shift;
296}
297
Cao jind90f3cc2016-07-29 19:05:38 +0800298/* return the time elapsed in VM between vm_start and vm_stop. Unless
299 * icount is active, cpu_get_ticks() uses units of the host CPU cycle
300 * counter.
301 *
302 * Caller must hold the BQL
303 */
Paolo Bonzini946fb272011-09-12 13:57:37 +0200304int64_t cpu_get_ticks(void)
305{
Paolo Bonzini5f3e3102013-10-28 17:32:18 +0100306 int64_t ticks;
307
Paolo Bonzini946fb272011-09-12 13:57:37 +0200308 if (use_icount) {
309 return cpu_get_icount();
310 }
Paolo Bonzini5f3e3102013-10-28 17:32:18 +0100311
312 ticks = timers_state.cpu_ticks_offset;
313 if (timers_state.cpu_ticks_enabled) {
Christopher Covington4a7428c2015-09-25 10:42:21 -0400314 ticks += cpu_get_host_ticks();
Paolo Bonzini946fb272011-09-12 13:57:37 +0200315 }
Paolo Bonzini5f3e3102013-10-28 17:32:18 +0100316
317 if (timers_state.cpu_ticks_prev > ticks) {
318 /* Note: non increasing ticks may happen if the host uses
319 software suspend */
320 timers_state.cpu_ticks_offset += timers_state.cpu_ticks_prev - ticks;
321 ticks = timers_state.cpu_ticks_prev;
322 }
323
324 timers_state.cpu_ticks_prev = ticks;
325 return ticks;
Paolo Bonzini946fb272011-09-12 13:57:37 +0200326}
327
Liu Ping Fancb365642013-09-25 14:20:58 +0800328static int64_t cpu_get_clock_locked(void)
329{
Cao jin1d45cea2016-07-29 19:05:37 +0800330 int64_t time;
Liu Ping Fancb365642013-09-25 14:20:58 +0800331
Cao jin1d45cea2016-07-29 19:05:37 +0800332 time = timers_state.cpu_clock_offset;
Paolo Bonzini5f3e3102013-10-28 17:32:18 +0100333 if (timers_state.cpu_ticks_enabled) {
Cao jin1d45cea2016-07-29 19:05:37 +0800334 time += get_clock();
Liu Ping Fancb365642013-09-25 14:20:58 +0800335 }
336
Cao jin1d45cea2016-07-29 19:05:37 +0800337 return time;
Liu Ping Fancb365642013-09-25 14:20:58 +0800338}
339
Cao jind90f3cc2016-07-29 19:05:38 +0800340/* Return the monotonic time elapsed in VM, i.e.,
Peter Maydell8212ff82016-09-15 10:24:22 +0100341 * the time between vm_start and vm_stop
342 */
Paolo Bonzini946fb272011-09-12 13:57:37 +0200343int64_t cpu_get_clock(void)
344{
345 int64_t ti;
Liu Ping Fancb365642013-09-25 14:20:58 +0800346 unsigned start;
347
348 do {
349 start = seqlock_read_begin(&timers_state.vm_clock_seqlock);
350 ti = cpu_get_clock_locked();
351 } while (seqlock_read_retry(&timers_state.vm_clock_seqlock, start));
352
353 return ti;
Paolo Bonzini946fb272011-09-12 13:57:37 +0200354}
355
Liu Ping Fancb365642013-09-25 14:20:58 +0800356/* enable cpu_get_ticks()
Cao jin3224e872016-07-08 18:31:37 +0800357 * Caller must hold BQL which serves as mutex for vm_clock_seqlock.
Liu Ping Fancb365642013-09-25 14:20:58 +0800358 */
Paolo Bonzini946fb272011-09-12 13:57:37 +0200359void cpu_enable_ticks(void)
360{
Liu Ping Fancb365642013-09-25 14:20:58 +0800361 /* Here, the really thing protected by seqlock is cpu_clock_offset. */
Emilio G. Cota03719e42016-06-08 14:55:21 -0400362 seqlock_write_begin(&timers_state.vm_clock_seqlock);
Paolo Bonzini946fb272011-09-12 13:57:37 +0200363 if (!timers_state.cpu_ticks_enabled) {
Christopher Covington4a7428c2015-09-25 10:42:21 -0400364 timers_state.cpu_ticks_offset -= cpu_get_host_ticks();
Paolo Bonzini946fb272011-09-12 13:57:37 +0200365 timers_state.cpu_clock_offset -= get_clock();
366 timers_state.cpu_ticks_enabled = 1;
367 }
Emilio G. Cota03719e42016-06-08 14:55:21 -0400368 seqlock_write_end(&timers_state.vm_clock_seqlock);
Paolo Bonzini946fb272011-09-12 13:57:37 +0200369}
370
371/* disable cpu_get_ticks() : the clock is stopped. You must not call
Liu Ping Fancb365642013-09-25 14:20:58 +0800372 * cpu_get_ticks() after that.
Cao jin3224e872016-07-08 18:31:37 +0800373 * Caller must hold BQL which serves as mutex for vm_clock_seqlock.
Liu Ping Fancb365642013-09-25 14:20:58 +0800374 */
Paolo Bonzini946fb272011-09-12 13:57:37 +0200375void cpu_disable_ticks(void)
376{
Liu Ping Fancb365642013-09-25 14:20:58 +0800377 /* Here, the really thing protected by seqlock is cpu_clock_offset. */
Emilio G. Cota03719e42016-06-08 14:55:21 -0400378 seqlock_write_begin(&timers_state.vm_clock_seqlock);
Paolo Bonzini946fb272011-09-12 13:57:37 +0200379 if (timers_state.cpu_ticks_enabled) {
Christopher Covington4a7428c2015-09-25 10:42:21 -0400380 timers_state.cpu_ticks_offset += cpu_get_host_ticks();
Liu Ping Fancb365642013-09-25 14:20:58 +0800381 timers_state.cpu_clock_offset = cpu_get_clock_locked();
Paolo Bonzini946fb272011-09-12 13:57:37 +0200382 timers_state.cpu_ticks_enabled = 0;
383 }
Emilio G. Cota03719e42016-06-08 14:55:21 -0400384 seqlock_write_end(&timers_state.vm_clock_seqlock);
Paolo Bonzini946fb272011-09-12 13:57:37 +0200385}
386
387/* Correlation between real and virtual time is always going to be
388 fairly approximate, so ignore small variation.
389 When the guest is idle real and virtual time will be aligned in
390 the IO wait loop. */
Rutuja Shah73bcb242016-03-21 21:32:30 +0530391#define ICOUNT_WOBBLE (NANOSECONDS_PER_SECOND / 10)
Paolo Bonzini946fb272011-09-12 13:57:37 +0200392
393static void icount_adjust(void)
394{
395 int64_t cur_time;
396 int64_t cur_icount;
397 int64_t delta;
Paolo Bonzinia3270e12013-10-07 17:18:15 +0200398
399 /* Protected by TimersState mutex. */
Paolo Bonzini946fb272011-09-12 13:57:37 +0200400 static int64_t last_delta;
Paolo Bonzini468cc7c2013-10-07 17:21:51 +0200401
Paolo Bonzini946fb272011-09-12 13:57:37 +0200402 /* If the VM is not running, then do nothing. */
403 if (!runstate_is_running()) {
404 return;
405 }
Paolo Bonzini468cc7c2013-10-07 17:21:51 +0200406
Emilio G. Cota03719e42016-06-08 14:55:21 -0400407 seqlock_write_begin(&timers_state.vm_clock_seqlock);
Paolo Bonzini17a15f12013-10-03 15:17:25 +0200408 cur_time = cpu_get_clock_locked();
409 cur_icount = cpu_get_icount_locked();
Paolo Bonzini468cc7c2013-10-07 17:21:51 +0200410
Paolo Bonzini946fb272011-09-12 13:57:37 +0200411 delta = cur_icount - cur_time;
412 /* FIXME: This is a very crude algorithm, somewhat prone to oscillation. */
413 if (delta > 0
414 && last_delta + ICOUNT_WOBBLE < delta * 2
415 && icount_time_shift > 0) {
416 /* The guest is getting too far ahead. Slow time down. */
417 icount_time_shift--;
418 }
419 if (delta < 0
420 && last_delta - ICOUNT_WOBBLE > delta * 2
421 && icount_time_shift < MAX_ICOUNT_SHIFT) {
422 /* The guest is getting too far behind. Speed time up. */
423 icount_time_shift++;
424 }
425 last_delta = delta;
KONRAD Fredericc96778b2014-08-01 01:37:09 +0200426 timers_state.qemu_icount_bias = cur_icount
427 - (timers_state.qemu_icount << icount_time_shift);
Emilio G. Cota03719e42016-06-08 14:55:21 -0400428 seqlock_write_end(&timers_state.vm_clock_seqlock);
Paolo Bonzini946fb272011-09-12 13:57:37 +0200429}
430
431static void icount_adjust_rt(void *opaque)
432{
Alex Bligh40daca52013-08-21 16:03:02 +0100433 timer_mod(icount_rt_timer,
Pavel Dovgalyuk1979b902015-01-12 15:00:43 +0300434 qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL_RT) + 1000);
Paolo Bonzini946fb272011-09-12 13:57:37 +0200435 icount_adjust();
436}
437
438static void icount_adjust_vm(void *opaque)
439{
Alex Bligh40daca52013-08-21 16:03:02 +0100440 timer_mod(icount_vm_timer,
441 qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) +
Rutuja Shah73bcb242016-03-21 21:32:30 +0530442 NANOSECONDS_PER_SECOND / 10);
Paolo Bonzini946fb272011-09-12 13:57:37 +0200443 icount_adjust();
444}
445
446static int64_t qemu_icount_round(int64_t count)
447{
448 return (count + (1 << icount_time_shift) - 1) >> icount_time_shift;
449}
450
Pavel Dovgalyukefab87c2015-09-17 19:24:39 +0300451static void icount_warp_rt(void)
Paolo Bonzini946fb272011-09-12 13:57:37 +0200452{
Alex Bennéeccffff42016-04-04 15:35:48 +0100453 unsigned seq;
454 int64_t warp_start;
455
Paolo Bonzini17a15f12013-10-03 15:17:25 +0200456 /* The icount_warp_timer is rescheduled soon after vm_clock_warp_start
457 * changes from -1 to another value, so the race here is okay.
458 */
Alex Bennéeccffff42016-04-04 15:35:48 +0100459 do {
460 seq = seqlock_read_begin(&timers_state.vm_clock_seqlock);
461 warp_start = vm_clock_warp_start;
462 } while (seqlock_read_retry(&timers_state.vm_clock_seqlock, seq));
463
464 if (warp_start == -1) {
Paolo Bonzini946fb272011-09-12 13:57:37 +0200465 return;
466 }
467
Emilio G. Cota03719e42016-06-08 14:55:21 -0400468 seqlock_write_begin(&timers_state.vm_clock_seqlock);
Paolo Bonzini946fb272011-09-12 13:57:37 +0200469 if (runstate_is_running()) {
Pavel Dovgalyuk8eda2062015-09-17 19:24:28 +0300470 int64_t clock = REPLAY_CLOCK(REPLAY_CLOCK_VIRTUAL_RT,
471 cpu_get_clock_locked());
Paolo Bonzini8ed961d2013-10-07 17:26:07 +0200472 int64_t warp_delta;
473
474 warp_delta = clock - vm_clock_warp_start;
475 if (use_icount == 2) {
Paolo Bonzini946fb272011-09-12 13:57:37 +0200476 /*
Alex Bligh40daca52013-08-21 16:03:02 +0100477 * In adaptive mode, do not let QEMU_CLOCK_VIRTUAL run too
Paolo Bonzini946fb272011-09-12 13:57:37 +0200478 * far ahead of real time.
479 */
Paolo Bonzini17a15f12013-10-03 15:17:25 +0200480 int64_t cur_icount = cpu_get_icount_locked();
Pavel Dovgalyukbf2a7dd2014-11-26 13:40:55 +0300481 int64_t delta = clock - cur_icount;
Paolo Bonzini8ed961d2013-10-07 17:26:07 +0200482 warp_delta = MIN(warp_delta, delta);
Paolo Bonzini946fb272011-09-12 13:57:37 +0200483 }
KONRAD Fredericc96778b2014-08-01 01:37:09 +0200484 timers_state.qemu_icount_bias += warp_delta;
Paolo Bonzini946fb272011-09-12 13:57:37 +0200485 }
486 vm_clock_warp_start = -1;
Emilio G. Cota03719e42016-06-08 14:55:21 -0400487 seqlock_write_end(&timers_state.vm_clock_seqlock);
Paolo Bonzini8ed961d2013-10-07 17:26:07 +0200488
489 if (qemu_clock_expired(QEMU_CLOCK_VIRTUAL)) {
490 qemu_clock_notify(QEMU_CLOCK_VIRTUAL);
491 }
Paolo Bonzini946fb272011-09-12 13:57:37 +0200492}
493
Pavel Dovgalyuke76d1792016-03-10 14:56:09 +0300494static void icount_timer_cb(void *opaque)
Pavel Dovgalyukefab87c2015-09-17 19:24:39 +0300495{
Pavel Dovgalyuke76d1792016-03-10 14:56:09 +0300496 /* No need for a checkpoint because the timer already synchronizes
497 * with CHECKPOINT_CLOCK_VIRTUAL_RT.
498 */
499 icount_warp_rt();
Pavel Dovgalyukefab87c2015-09-17 19:24:39 +0300500}
501
Paolo Bonzini8156be52012-03-28 15:42:04 +0200502void qtest_clock_warp(int64_t dest)
503{
Alex Bligh40daca52013-08-21 16:03:02 +0100504 int64_t clock = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
Fam Zhengefef88b2015-01-19 17:51:43 +0800505 AioContext *aio_context;
Paolo Bonzini8156be52012-03-28 15:42:04 +0200506 assert(qtest_enabled());
Fam Zhengefef88b2015-01-19 17:51:43 +0800507 aio_context = qemu_get_aio_context();
Paolo Bonzini8156be52012-03-28 15:42:04 +0200508 while (clock < dest) {
Alex Bligh40daca52013-08-21 16:03:02 +0100509 int64_t deadline = qemu_clock_deadline_ns_all(QEMU_CLOCK_VIRTUAL);
Sergey Fedorovc9299e22014-06-10 13:10:28 +0400510 int64_t warp = qemu_soonest_timeout(dest - clock, deadline);
Fam Zhengefef88b2015-01-19 17:51:43 +0800511
Emilio G. Cota03719e42016-06-08 14:55:21 -0400512 seqlock_write_begin(&timers_state.vm_clock_seqlock);
KONRAD Fredericc96778b2014-08-01 01:37:09 +0200513 timers_state.qemu_icount_bias += warp;
Emilio G. Cota03719e42016-06-08 14:55:21 -0400514 seqlock_write_end(&timers_state.vm_clock_seqlock);
Paolo Bonzini17a15f12013-10-03 15:17:25 +0200515
Alex Bligh40daca52013-08-21 16:03:02 +0100516 qemu_clock_run_timers(QEMU_CLOCK_VIRTUAL);
Fam Zhengefef88b2015-01-19 17:51:43 +0800517 timerlist_run_timers(aio_context->tlg.tl[QEMU_CLOCK_VIRTUAL]);
Alex Bligh40daca52013-08-21 16:03:02 +0100518 clock = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
Paolo Bonzini8156be52012-03-28 15:42:04 +0200519 }
Alex Bligh40daca52013-08-21 16:03:02 +0100520 qemu_clock_notify(QEMU_CLOCK_VIRTUAL);
Paolo Bonzini8156be52012-03-28 15:42:04 +0200521}
522
Pavel Dovgalyuke76d1792016-03-10 14:56:09 +0300523void qemu_start_warp_timer(void)
Paolo Bonzini946fb272011-09-12 13:57:37 +0200524{
Paolo Bonzinice78d182013-10-07 17:30:02 +0200525 int64_t clock;
Paolo Bonzini946fb272011-09-12 13:57:37 +0200526 int64_t deadline;
527
Pavel Dovgalyuke76d1792016-03-10 14:56:09 +0300528 if (!use_icount) {
Paolo Bonzini946fb272011-09-12 13:57:37 +0200529 return;
530 }
531
Pavel Dovgalyuk8bd7f712015-09-17 19:24:44 +0300532 /* Nothing to do if the VM is stopped: QEMU_CLOCK_VIRTUAL timers
533 * do not fire, so computing the deadline does not make sense.
534 */
535 if (!runstate_is_running()) {
536 return;
537 }
538
539 /* warp clock deterministically in record/replay mode */
Pavel Dovgalyuke76d1792016-03-10 14:56:09 +0300540 if (!replay_checkpoint(CHECKPOINT_CLOCK_WARP_START)) {
Pavel Dovgalyuk8bd7f712015-09-17 19:24:44 +0300541 return;
542 }
543
Paolo Bonzinice78d182013-10-07 17:30:02 +0200544 if (!all_cpu_threads_idle()) {
Paolo Bonzini946fb272011-09-12 13:57:37 +0200545 return;
546 }
547
Paolo Bonzini8156be52012-03-28 15:42:04 +0200548 if (qtest_enabled()) {
549 /* When testing, qtest commands advance icount. */
Pavel Dovgalyuke76d1792016-03-10 14:56:09 +0300550 return;
Paolo Bonzini8156be52012-03-28 15:42:04 +0200551 }
552
Alex Blighac70aaf2013-08-21 16:02:57 +0100553 /* We want to use the earliest deadline from ALL vm_clocks */
Pavel Dovgalyukbf2a7dd2014-11-26 13:40:55 +0300554 clock = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL_RT);
Alex Bligh40daca52013-08-21 16:03:02 +0100555 deadline = qemu_clock_deadline_ns_all(QEMU_CLOCK_VIRTUAL);
Paolo Bonzinice78d182013-10-07 17:30:02 +0200556 if (deadline < 0) {
Victor CLEMENTd7a0f712015-05-29 17:14:06 +0200557 static bool notified;
558 if (!icount_sleep && !notified) {
559 error_report("WARNING: icount sleep disabled and no active timers");
560 notified = true;
561 }
Paolo Bonzinice78d182013-10-07 17:30:02 +0200562 return;
Alex Blighac70aaf2013-08-21 16:02:57 +0100563 }
564
Paolo Bonzini946fb272011-09-12 13:57:37 +0200565 if (deadline > 0) {
566 /*
Alex Bligh40daca52013-08-21 16:03:02 +0100567 * Ensure QEMU_CLOCK_VIRTUAL proceeds even when the virtual CPU goes to
Paolo Bonzini946fb272011-09-12 13:57:37 +0200568 * sleep. Otherwise, the CPU might be waiting for a future timer
569 * interrupt to wake it up, but the interrupt never comes because
570 * the vCPU isn't running any insns and thus doesn't advance the
Alex Bligh40daca52013-08-21 16:03:02 +0100571 * QEMU_CLOCK_VIRTUAL.
Paolo Bonzini946fb272011-09-12 13:57:37 +0200572 */
Victor CLEMENT5045e9d92015-05-29 17:14:04 +0200573 if (!icount_sleep) {
574 /*
575 * We never let VCPUs sleep in no sleep icount mode.
576 * If there is a pending QEMU_CLOCK_VIRTUAL timer we just advance
577 * to the next QEMU_CLOCK_VIRTUAL event and notify it.
578 * It is useful when we want a deterministic execution time,
579 * isolated from host latencies.
580 */
Emilio G. Cota03719e42016-06-08 14:55:21 -0400581 seqlock_write_begin(&timers_state.vm_clock_seqlock);
Victor CLEMENT5045e9d92015-05-29 17:14:04 +0200582 timers_state.qemu_icount_bias += deadline;
Emilio G. Cota03719e42016-06-08 14:55:21 -0400583 seqlock_write_end(&timers_state.vm_clock_seqlock);
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 */
Emilio G. Cota03719e42016-06-08 14:55:21 -0400594 seqlock_write_begin(&timers_state.vm_clock_seqlock);
Victor CLEMENT5045e9d92015-05-29 17:14:04 +0200595 if (vm_clock_warp_start == -1 || vm_clock_warp_start > clock) {
596 vm_clock_warp_start = clock;
597 }
Emilio G. Cota03719e42016-06-08 14:55:21 -0400598 seqlock_write_end(&timers_state.vm_clock_seqlock);
Victor CLEMENT5045e9d92015-05-29 17:14:04 +0200599 timer_mod_anticipate(icount_warp_timer, clock + deadline);
Paolo Bonzinice78d182013-10-07 17:30:02 +0200600 }
Alex Blighac70aaf2013-08-21 16:02:57 +0100601 } else if (deadline == 0) {
Alex Bligh40daca52013-08-21 16:03:02 +0100602 qemu_clock_notify(QEMU_CLOCK_VIRTUAL);
Paolo Bonzini946fb272011-09-12 13:57:37 +0200603 }
604}
605
Pavel Dovgalyuke76d1792016-03-10 14:56:09 +0300606static void qemu_account_warp_timer(void)
607{
608 if (!use_icount || !icount_sleep) {
609 return;
610 }
611
612 /* Nothing to do if the VM is stopped: QEMU_CLOCK_VIRTUAL timers
613 * do not fire, so computing the deadline does not make sense.
614 */
615 if (!runstate_is_running()) {
616 return;
617 }
618
619 /* warp clock deterministically in record/replay mode */
620 if (!replay_checkpoint(CHECKPOINT_CLOCK_WARP_ACCOUNT)) {
621 return;
622 }
623
624 timer_del(icount_warp_timer);
625 icount_warp_rt();
626}
627
KONRAD Fredericd09eae32014-08-01 01:37:10 +0200628static bool icount_state_needed(void *opaque)
629{
630 return use_icount;
631}
632
633/*
634 * This is a subsection for icount migration.
635 */
636static const VMStateDescription icount_vmstate_timers = {
637 .name = "timer/icount",
638 .version_id = 1,
639 .minimum_version_id = 1,
Juan Quintela5cd8cad2014-09-23 14:09:54 +0200640 .needed = icount_state_needed,
KONRAD Fredericd09eae32014-08-01 01:37:10 +0200641 .fields = (VMStateField[]) {
642 VMSTATE_INT64(qemu_icount_bias, TimersState),
643 VMSTATE_INT64(qemu_icount, TimersState),
644 VMSTATE_END_OF_LIST()
645 }
646};
647
Paolo Bonzini946fb272011-09-12 13:57:37 +0200648static const VMStateDescription vmstate_timers = {
649 .name = "timer",
650 .version_id = 2,
651 .minimum_version_id = 1,
Juan Quintela35d08452014-04-16 16:01:33 +0200652 .fields = (VMStateField[]) {
Paolo Bonzini946fb272011-09-12 13:57:37 +0200653 VMSTATE_INT64(cpu_ticks_offset, TimersState),
654 VMSTATE_INT64(dummy, TimersState),
655 VMSTATE_INT64_V(cpu_clock_offset, TimersState, 2),
656 VMSTATE_END_OF_LIST()
KONRAD Fredericd09eae32014-08-01 01:37:10 +0200657 },
Juan Quintela5cd8cad2014-09-23 14:09:54 +0200658 .subsections = (const VMStateDescription*[]) {
659 &icount_vmstate_timers,
660 NULL
Paolo Bonzini946fb272011-09-12 13:57:37 +0200661 }
662};
663
Paolo Bonzini14e6fe12016-10-31 10:36:08 +0100664static void cpu_throttle_thread(CPUState *cpu, run_on_cpu_data opaque)
Jason J. Herne2adcc852015-09-08 13:12:33 -0400665{
Jason J. Herne2adcc852015-09-08 13:12:33 -0400666 double pct;
667 double throttle_ratio;
668 long sleeptime_ns;
669
670 if (!cpu_throttle_get_percentage()) {
671 return;
672 }
673
674 pct = (double)cpu_throttle_get_percentage()/100;
675 throttle_ratio = pct / (1 - pct);
676 sleeptime_ns = (long)(throttle_ratio * CPU_THROTTLE_TIMESLICE_NS);
677
678 qemu_mutex_unlock_iothread();
679 atomic_set(&cpu->throttle_thread_scheduled, 0);
680 g_usleep(sleeptime_ns / 1000); /* Convert ns to us for usleep call */
681 qemu_mutex_lock_iothread();
682}
683
684static void cpu_throttle_timer_tick(void *opaque)
685{
686 CPUState *cpu;
687 double pct;
688
689 /* Stop the timer if needed */
690 if (!cpu_throttle_get_percentage()) {
691 return;
692 }
693 CPU_FOREACH(cpu) {
694 if (!atomic_xchg(&cpu->throttle_thread_scheduled, 1)) {
Paolo Bonzini14e6fe12016-10-31 10:36:08 +0100695 async_run_on_cpu(cpu, cpu_throttle_thread,
696 RUN_ON_CPU_NULL);
Jason J. Herne2adcc852015-09-08 13:12:33 -0400697 }
698 }
699
700 pct = (double)cpu_throttle_get_percentage()/100;
701 timer_mod(throttle_timer, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL_RT) +
702 CPU_THROTTLE_TIMESLICE_NS / (1-pct));
703}
704
705void cpu_throttle_set(int new_throttle_pct)
706{
707 /* Ensure throttle percentage is within valid range */
708 new_throttle_pct = MIN(new_throttle_pct, CPU_THROTTLE_PCT_MAX);
709 new_throttle_pct = MAX(new_throttle_pct, CPU_THROTTLE_PCT_MIN);
710
711 atomic_set(&throttle_percentage, new_throttle_pct);
712
713 timer_mod(throttle_timer, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL_RT) +
714 CPU_THROTTLE_TIMESLICE_NS);
715}
716
717void cpu_throttle_stop(void)
718{
719 atomic_set(&throttle_percentage, 0);
720}
721
722bool cpu_throttle_active(void)
723{
724 return (cpu_throttle_get_percentage() != 0);
725}
726
727int cpu_throttle_get_percentage(void)
728{
729 return atomic_read(&throttle_percentage);
730}
731
Pavel Dovgalyuk4603ea02014-09-01 09:34:49 +0400732void cpu_ticks_init(void)
733{
Emilio G. Cotaccdb3c12016-06-08 14:55:20 -0400734 seqlock_init(&timers_state.vm_clock_seqlock);
Pavel Dovgalyuk4603ea02014-09-01 09:34:49 +0400735 vmstate_register(NULL, 0, &vmstate_timers, &timers_state);
Jason J. Herne2adcc852015-09-08 13:12:33 -0400736 throttle_timer = timer_new_ns(QEMU_CLOCK_VIRTUAL_RT,
737 cpu_throttle_timer_tick, NULL);
Pavel Dovgalyuk4603ea02014-09-01 09:34:49 +0400738}
739
Sebastian Tanase1ad95802014-07-25 11:56:28 +0200740void configure_icount(QemuOpts *opts, Error **errp)
Paolo Bonzini946fb272011-09-12 13:57:37 +0200741{
Sebastian Tanase1ad95802014-07-25 11:56:28 +0200742 const char *option;
Sebastian Tanasea8bfac32014-07-25 11:56:29 +0200743 char *rem_str = NULL;
Sebastian Tanase1ad95802014-07-25 11:56:28 +0200744
Sebastian Tanase1ad95802014-07-25 11:56:28 +0200745 option = qemu_opt_get(opts, "shift");
Paolo Bonzini946fb272011-09-12 13:57:37 +0200746 if (!option) {
Sebastian Tanasea8bfac32014-07-25 11:56:29 +0200747 if (qemu_opt_get(opts, "align") != NULL) {
748 error_setg(errp, "Please specify shift option when using align");
749 }
Paolo Bonzini946fb272011-09-12 13:57:37 +0200750 return;
751 }
Victor CLEMENTf1f4b572015-05-29 17:14:05 +0200752
753 icount_sleep = qemu_opt_get_bool(opts, "sleep", true);
Victor CLEMENT5045e9d92015-05-29 17:14:04 +0200754 if (icount_sleep) {
755 icount_warp_timer = timer_new_ns(QEMU_CLOCK_VIRTUAL_RT,
Pavel Dovgalyuke76d1792016-03-10 14:56:09 +0300756 icount_timer_cb, NULL);
Victor CLEMENT5045e9d92015-05-29 17:14:04 +0200757 }
Victor CLEMENTf1f4b572015-05-29 17:14:05 +0200758
Sebastian Tanasea8bfac32014-07-25 11:56:29 +0200759 icount_align_option = qemu_opt_get_bool(opts, "align", false);
Victor CLEMENTf1f4b572015-05-29 17:14:05 +0200760
761 if (icount_align_option && !icount_sleep) {
Pranith Kumar778d9f92016-02-26 10:16:51 -0500762 error_setg(errp, "align=on and sleep=off are incompatible");
Victor CLEMENTf1f4b572015-05-29 17:14:05 +0200763 }
Paolo Bonzini946fb272011-09-12 13:57:37 +0200764 if (strcmp(option, "auto") != 0) {
Sebastian Tanasea8bfac32014-07-25 11:56:29 +0200765 errno = 0;
766 icount_time_shift = strtol(option, &rem_str, 0);
767 if (errno != 0 || *rem_str != '\0' || !strlen(option)) {
768 error_setg(errp, "icount: Invalid shift value");
769 }
Paolo Bonzini946fb272011-09-12 13:57:37 +0200770 use_icount = 1;
771 return;
Sebastian Tanasea8bfac32014-07-25 11:56:29 +0200772 } else if (icount_align_option) {
773 error_setg(errp, "shift=auto and align=on are incompatible");
Victor CLEMENTf1f4b572015-05-29 17:14:05 +0200774 } else if (!icount_sleep) {
Pranith Kumar778d9f92016-02-26 10:16:51 -0500775 error_setg(errp, "shift=auto and sleep=off are incompatible");
Paolo Bonzini946fb272011-09-12 13:57:37 +0200776 }
777
778 use_icount = 2;
779
780 /* 125MIPS seems a reasonable initial guess at the guest speed.
781 It will be corrected fairly quickly anyway. */
782 icount_time_shift = 3;
783
784 /* Have both realtime and virtual time triggers for speed adjustment.
785 The realtime trigger catches emulated time passing too slowly,
786 the virtual time trigger catches emulated time passing too fast.
787 Realtime triggers occur even when idle, so use them less frequently
788 than VM triggers. */
Pavel Dovgalyukbf2a7dd2014-11-26 13:40:55 +0300789 icount_rt_timer = timer_new_ms(QEMU_CLOCK_VIRTUAL_RT,
790 icount_adjust_rt, NULL);
Alex Bligh40daca52013-08-21 16:03:02 +0100791 timer_mod(icount_rt_timer,
Pavel Dovgalyukbf2a7dd2014-11-26 13:40:55 +0300792 qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL_RT) + 1000);
Alex Bligh40daca52013-08-21 16:03:02 +0100793 icount_vm_timer = timer_new_ns(QEMU_CLOCK_VIRTUAL,
794 icount_adjust_vm, NULL);
795 timer_mod(icount_vm_timer,
796 qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) +
Rutuja Shah73bcb242016-03-21 21:32:30 +0530797 NANOSECONDS_PER_SECOND / 10);
Paolo Bonzini946fb272011-09-12 13:57:37 +0200798}
799
800/***********************************************************/
Alex Bennée65467062017-02-23 18:29:09 +0000801/* TCG vCPU kick timer
802 *
803 * The kick timer is responsible for moving single threaded vCPU
804 * emulation on to the next vCPU. If more than one vCPU is running a
805 * timer event with force a cpu->exit so the next vCPU can get
806 * scheduled.
807 *
808 * The timer is removed if all vCPUs are idle and restarted again once
809 * idleness is complete.
810 */
811
812static QEMUTimer *tcg_kick_vcpu_timer;
Alex Bennée791158d2017-02-23 18:29:10 +0000813static CPUState *tcg_current_rr_cpu;
Alex Bennée65467062017-02-23 18:29:09 +0000814
815#define TCG_KICK_PERIOD (NANOSECONDS_PER_SECOND / 10)
816
817static inline int64_t qemu_tcg_next_kick(void)
818{
819 return qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) + TCG_KICK_PERIOD;
820}
821
Alex Bennée791158d2017-02-23 18:29:10 +0000822/* Kick the currently round-robin scheduled vCPU */
823static void qemu_cpu_kick_rr_cpu(void)
824{
825 CPUState *cpu;
Alex Bennée791158d2017-02-23 18:29:10 +0000826 do {
827 cpu = atomic_mb_read(&tcg_current_rr_cpu);
828 if (cpu) {
829 cpu_exit(cpu);
830 }
831 } while (cpu != atomic_mb_read(&tcg_current_rr_cpu));
832}
833
Paolo Bonzini6b8f0182017-03-02 19:56:40 +0100834static void do_nothing(CPUState *cpu, run_on_cpu_data unused)
835{
836}
837
Paolo Bonzini3f53bc62017-03-03 11:50:29 +0100838void qemu_timer_notify_cb(void *opaque, QEMUClockType type)
839{
Paolo Bonzini6b8f0182017-03-02 19:56:40 +0100840 if (!use_icount || type != QEMU_CLOCK_VIRTUAL) {
841 qemu_notify_event();
842 return;
843 }
844
845 if (!qemu_in_vcpu_thread() && first_cpu) {
846 /* qemu_cpu_kick is not enough to kick a halted CPU out of
847 * qemu_tcg_wait_io_event. async_run_on_cpu, instead,
848 * causes cpu_thread_is_idle to return false. This way,
849 * handle_icount_deadline can run.
850 */
851 async_run_on_cpu(first_cpu, do_nothing, RUN_ON_CPU_NULL);
852 }
Paolo Bonzini3f53bc62017-03-03 11:50:29 +0100853}
854
Alex Bennée65467062017-02-23 18:29:09 +0000855static void kick_tcg_thread(void *opaque)
856{
857 timer_mod(tcg_kick_vcpu_timer, qemu_tcg_next_kick());
Alex Bennée791158d2017-02-23 18:29:10 +0000858 qemu_cpu_kick_rr_cpu();
Alex Bennée65467062017-02-23 18:29:09 +0000859}
860
861static void start_tcg_kick_timer(void)
862{
Alex Bennée37257942017-02-23 18:29:14 +0000863 if (!mttcg_enabled && !tcg_kick_vcpu_timer && CPU_NEXT(first_cpu)) {
Alex Bennée65467062017-02-23 18:29:09 +0000864 tcg_kick_vcpu_timer = timer_new_ns(QEMU_CLOCK_VIRTUAL,
865 kick_tcg_thread, NULL);
866 timer_mod(tcg_kick_vcpu_timer, qemu_tcg_next_kick());
867 }
868}
869
870static void stop_tcg_kick_timer(void)
871{
872 if (tcg_kick_vcpu_timer) {
873 timer_del(tcg_kick_vcpu_timer);
874 tcg_kick_vcpu_timer = NULL;
875 }
876}
877
Alex Bennée65467062017-02-23 18:29:09 +0000878/***********************************************************/
Blue Swirl296af7c2010-03-29 19:23:50 +0000879void hw_error(const char *fmt, ...)
880{
881 va_list ap;
Andreas Färber55e5c282012-12-17 06:18:02 +0100882 CPUState *cpu;
Blue Swirl296af7c2010-03-29 19:23:50 +0000883
884 va_start(ap, fmt);
885 fprintf(stderr, "qemu: hardware error: ");
886 vfprintf(stderr, fmt, ap);
887 fprintf(stderr, "\n");
Andreas Färberbdc44642013-06-24 23:50:24 +0200888 CPU_FOREACH(cpu) {
Andreas Färber55e5c282012-12-17 06:18:02 +0100889 fprintf(stderr, "CPU #%d:\n", cpu->cpu_index);
Andreas Färber878096e2013-05-27 01:33:50 +0200890 cpu_dump_state(cpu, stderr, fprintf, CPU_DUMP_FPU);
Blue Swirl296af7c2010-03-29 19:23:50 +0000891 }
892 va_end(ap);
893 abort();
894}
895
896void cpu_synchronize_all_states(void)
897{
Andreas Färber182735e2013-05-29 22:29:20 +0200898 CPUState *cpu;
Blue Swirl296af7c2010-03-29 19:23:50 +0000899
Andreas Färberbdc44642013-06-24 23:50:24 +0200900 CPU_FOREACH(cpu) {
Andreas Färber182735e2013-05-29 22:29:20 +0200901 cpu_synchronize_state(cpu);
Blue Swirl296af7c2010-03-29 19:23:50 +0000902 }
903}
904
905void cpu_synchronize_all_post_reset(void)
906{
Andreas Färber182735e2013-05-29 22:29:20 +0200907 CPUState *cpu;
Blue Swirl296af7c2010-03-29 19:23:50 +0000908
Andreas Färberbdc44642013-06-24 23:50:24 +0200909 CPU_FOREACH(cpu) {
Andreas Färber182735e2013-05-29 22:29:20 +0200910 cpu_synchronize_post_reset(cpu);
Blue Swirl296af7c2010-03-29 19:23:50 +0000911 }
912}
913
914void cpu_synchronize_all_post_init(void)
915{
Andreas Färber182735e2013-05-29 22:29:20 +0200916 CPUState *cpu;
Blue Swirl296af7c2010-03-29 19:23:50 +0000917
Andreas Färberbdc44642013-06-24 23:50:24 +0200918 CPU_FOREACH(cpu) {
Andreas Färber182735e2013-05-29 22:29:20 +0200919 cpu_synchronize_post_init(cpu);
Blue Swirl296af7c2010-03-29 19:23:50 +0000920 }
921}
922
Kevin Wolf56983462013-07-05 13:49:54 +0200923static int do_vm_stop(RunState state)
Blue Swirl296af7c2010-03-29 19:23:50 +0000924{
Kevin Wolf56983462013-07-05 13:49:54 +0200925 int ret = 0;
926
Luiz Capitulino13548692011-07-29 15:36:43 -0300927 if (runstate_is_running()) {
Blue Swirl296af7c2010-03-29 19:23:50 +0000928 cpu_disable_ticks();
Blue Swirl296af7c2010-03-29 19:23:50 +0000929 pause_all_vcpus();
Luiz Capitulinof5bbfba2011-07-29 15:04:45 -0300930 runstate_set(state);
Luiz Capitulino1dfb4dd2011-07-29 14:26:33 -0300931 vm_state_notify(0, state);
Wenchao Xiaa4e15de2014-06-18 08:43:36 +0200932 qapi_event_send_stop(&error_abort);
Blue Swirl296af7c2010-03-29 19:23:50 +0000933 }
Kevin Wolf56983462013-07-05 13:49:54 +0200934
Kevin Wolf594a45c2013-07-18 14:52:19 +0200935 bdrv_drain_all();
Pavel Dovgalyuk6d0ceb82016-09-26 11:08:16 +0300936 replay_disable_events();
John Snow22af08e2016-09-22 21:45:51 -0400937 ret = bdrv_flush_all();
Kevin Wolf594a45c2013-07-18 14:52:19 +0200938
Kevin Wolf56983462013-07-05 13:49:54 +0200939 return ret;
Blue Swirl296af7c2010-03-29 19:23:50 +0000940}
941
Andreas Färbera1fcaa72012-05-02 23:42:26 +0200942static bool cpu_can_run(CPUState *cpu)
Blue Swirl296af7c2010-03-29 19:23:50 +0000943{
Andreas Färber4fdeee72012-05-02 23:10:09 +0200944 if (cpu->stop) {
Andreas Färbera1fcaa72012-05-02 23:42:26 +0200945 return false;
Jan Kiszka0ab07c62011-02-07 12:19:14 +0100946 }
Tiejun Chen321bc0b2013-08-02 09:43:09 +0800947 if (cpu_is_stopped(cpu)) {
Andreas Färbera1fcaa72012-05-02 23:42:26 +0200948 return false;
Jan Kiszka0ab07c62011-02-07 12:19:14 +0100949 }
Andreas Färbera1fcaa72012-05-02 23:42:26 +0200950 return true;
Blue Swirl296af7c2010-03-29 19:23:50 +0000951}
952
Andreas Färber91325042013-05-27 02:07:49 +0200953static void cpu_handle_guest_debug(CPUState *cpu)
Jan Kiszka3c638d02010-06-25 16:56:56 +0200954{
Andreas Färber64f6b342013-05-27 02:06:09 +0200955 gdb_set_stop_cpu(cpu);
Jan Kiszka8cf71712011-02-07 12:19:16 +0100956 qemu_system_debug_request();
Andreas Färberf324e762012-05-02 23:26:21 +0200957 cpu->stopped = true;
Jan Kiszka3c638d02010-06-25 16:56:56 +0200958}
959
Jan Kiszka6d9cb732011-02-01 22:15:58 +0100960#ifdef CONFIG_LINUX
961static void sigbus_reraise(void)
962{
963 sigset_t set;
964 struct sigaction action;
965
966 memset(&action, 0, sizeof(action));
967 action.sa_handler = SIG_DFL;
968 if (!sigaction(SIGBUS, &action, NULL)) {
969 raise(SIGBUS);
970 sigemptyset(&set);
971 sigaddset(&set, SIGBUS);
Peter Maydella2d17612016-05-16 18:33:59 +0100972 pthread_sigmask(SIG_UNBLOCK, &set, NULL);
Jan Kiszka6d9cb732011-02-01 22:15:58 +0100973 }
974 perror("Failed to re-raise SIGBUS!\n");
975 abort();
976}
977
Paolo Bonzinid98d4072017-02-08 13:22:12 +0100978static void sigbus_handler(int n, siginfo_t *siginfo, void *ctx)
Jan Kiszka6d9cb732011-02-01 22:15:58 +0100979{
Paolo Bonzinia16fc072017-02-09 09:50:02 +0100980 if (siginfo->si_code != BUS_MCEERR_AO && siginfo->si_code != BUS_MCEERR_AR) {
981 sigbus_reraise();
982 }
983
Paolo Bonzini2ae41db2017-02-08 12:48:54 +0100984 if (current_cpu) {
985 /* Called asynchronously in VCPU thread. */
986 if (kvm_on_sigbus_vcpu(current_cpu, siginfo->si_code, siginfo->si_addr)) {
987 sigbus_reraise();
988 }
989 } else {
990 /* Called synchronously (via signalfd) in main thread. */
991 if (kvm_on_sigbus(siginfo->si_code, siginfo->si_addr)) {
992 sigbus_reraise();
993 }
Jan Kiszka6d9cb732011-02-01 22:15:58 +0100994 }
995}
996
997static void qemu_init_sigbus(void)
998{
999 struct sigaction action;
1000
1001 memset(&action, 0, sizeof(action));
1002 action.sa_flags = SA_SIGINFO;
Paolo Bonzinid98d4072017-02-08 13:22:12 +01001003 action.sa_sigaction = sigbus_handler;
Jan Kiszka6d9cb732011-02-01 22:15:58 +01001004 sigaction(SIGBUS, &action, NULL);
1005
1006 prctl(PR_MCE_KILL, PR_MCE_KILL_SET, PR_MCE_KILL_EARLY, 0, 0);
1007}
Paolo Bonzinia16fc072017-02-09 09:50:02 +01001008#else /* !CONFIG_LINUX */
1009static void qemu_init_sigbus(void)
1010{
1011}
Paolo Bonzinia16fc072017-02-09 09:50:02 +01001012#endif /* !CONFIG_LINUX */
Blue Swirl296af7c2010-03-29 19:23:50 +00001013
Stefan Weilb2532d82012-09-27 07:41:42 +02001014static QemuMutex qemu_global_mutex;
Blue Swirl296af7c2010-03-29 19:23:50 +00001015
1016static QemuThread io_thread;
1017
Blue Swirl296af7c2010-03-29 19:23:50 +00001018/* cpu creation */
1019static QemuCond qemu_cpu_cond;
1020/* system init */
Blue Swirl296af7c2010-03-29 19:23:50 +00001021static QemuCond qemu_pause_cond;
1022
Paolo Bonzinid3b12f52011-09-13 10:30:52 +02001023void qemu_init_cpu_loop(void)
Blue Swirl296af7c2010-03-29 19:23:50 +00001024{
Jan Kiszka6d9cb732011-02-01 22:15:58 +01001025 qemu_init_sigbus();
Anthony Liguoried945922011-02-08 18:18:18 +01001026 qemu_cond_init(&qemu_cpu_cond);
Anthony Liguoried945922011-02-08 18:18:18 +01001027 qemu_cond_init(&qemu_pause_cond);
Blue Swirl296af7c2010-03-29 19:23:50 +00001028 qemu_mutex_init(&qemu_global_mutex);
Blue Swirl296af7c2010-03-29 19:23:50 +00001029
Jan Kiszkab7680cb2011-03-12 17:43:51 +01001030 qemu_thread_get_self(&io_thread);
Blue Swirl296af7c2010-03-29 19:23:50 +00001031}
1032
Paolo Bonzini14e6fe12016-10-31 10:36:08 +01001033void run_on_cpu(CPUState *cpu, run_on_cpu_func func, run_on_cpu_data data)
Marcelo Tosattie82bcec2010-05-04 09:45:22 -03001034{
Sergey Fedorovd148d902016-08-29 09:51:00 +02001035 do_run_on_cpu(cpu, func, data, &qemu_global_mutex);
Chegu Vinod3c022702013-06-24 03:49:41 -06001036}
1037
Gu Zheng4c055ab2016-05-12 09:18:13 +05301038static void qemu_kvm_destroy_vcpu(CPUState *cpu)
1039{
1040 if (kvm_destroy_vcpu(cpu) < 0) {
1041 error_report("kvm_destroy_vcpu failed");
1042 exit(EXIT_FAILURE);
1043 }
1044}
1045
1046static void qemu_tcg_destroy_vcpu(CPUState *cpu)
1047{
1048}
1049
Andreas Färber509a0d72012-05-03 02:18:09 +02001050static void qemu_wait_io_event_common(CPUState *cpu)
Blue Swirl296af7c2010-03-29 19:23:50 +00001051{
Alex Bennée37257942017-02-23 18:29:14 +00001052 atomic_mb_set(&cpu->thread_kicked, false);
Andreas Färber4fdeee72012-05-02 23:10:09 +02001053 if (cpu->stop) {
1054 cpu->stop = false;
Andreas Färberf324e762012-05-02 23:26:21 +02001055 cpu->stopped = true;
Dr. David Alan Gilbert96bce682016-01-25 10:08:18 +00001056 qemu_cond_broadcast(&qemu_pause_cond);
Blue Swirl296af7c2010-03-29 19:23:50 +00001057 }
Sergey Fedorova5403c62016-08-02 18:27:36 +01001058 process_queued_cpu_work(cpu);
Alex Bennée37257942017-02-23 18:29:14 +00001059}
1060
1061static bool qemu_tcg_should_sleep(CPUState *cpu)
1062{
1063 if (mttcg_enabled) {
1064 return cpu_thread_is_idle(cpu);
1065 } else {
1066 return all_cpu_threads_idle();
1067 }
Blue Swirl296af7c2010-03-29 19:23:50 +00001068}
1069
KONRAD Fredericd5f8d612015-08-10 17:27:06 +02001070static void qemu_tcg_wait_io_event(CPUState *cpu)
Blue Swirl296af7c2010-03-29 19:23:50 +00001071{
Alex Bennée37257942017-02-23 18:29:14 +00001072 while (qemu_tcg_should_sleep(cpu)) {
Alex Bennée65467062017-02-23 18:29:09 +00001073 stop_tcg_kick_timer();
KONRAD Fredericd5f8d612015-08-10 17:27:06 +02001074 qemu_cond_wait(cpu->halt_cond, &qemu_global_mutex);
Jan Kiszka16400322011-02-09 16:29:37 +01001075 }
Blue Swirl296af7c2010-03-29 19:23:50 +00001076
Alex Bennée65467062017-02-23 18:29:09 +00001077 start_tcg_kick_timer();
1078
Alex Bennée37257942017-02-23 18:29:14 +00001079 qemu_wait_io_event_common(cpu);
Blue Swirl296af7c2010-03-29 19:23:50 +00001080}
1081
Andreas Färberfd529e82013-05-26 23:24:55 +02001082static void qemu_kvm_wait_io_event(CPUState *cpu)
Blue Swirl296af7c2010-03-29 19:23:50 +00001083{
Andreas Färbera98ae1d2013-05-26 23:21:08 +02001084 while (cpu_thread_is_idle(cpu)) {
Andreas Färberf5c121b2012-05-03 01:22:49 +02001085 qemu_cond_wait(cpu->halt_cond, &qemu_global_mutex);
Jan Kiszka16400322011-02-09 16:29:37 +01001086 }
Blue Swirl296af7c2010-03-29 19:23:50 +00001087
Andreas Färber509a0d72012-05-03 02:18:09 +02001088 qemu_wait_io_event_common(cpu);
Blue Swirl296af7c2010-03-29 19:23:50 +00001089}
1090
Jan Kiszka7e97cd82011-02-07 12:19:12 +01001091static void *qemu_kvm_cpu_thread_fn(void *arg)
Blue Swirl296af7c2010-03-29 19:23:50 +00001092{
Andreas Färber48a106b2013-05-27 02:20:39 +02001093 CPUState *cpu = arg;
Jan Kiszka84b49152011-02-01 22:15:50 +01001094 int r;
Blue Swirl296af7c2010-03-29 19:23:50 +00001095
Paolo Bonziniab28bd22015-07-09 08:55:38 +02001096 rcu_register_thread();
1097
Paolo Bonzini2e7f7a32015-06-18 18:47:18 +02001098 qemu_mutex_lock_iothread();
Andreas Färber814e6122012-05-02 17:00:37 +02001099 qemu_thread_get_self(cpu->thread);
Andreas Färber9f09e182012-05-03 06:59:07 +02001100 cpu->thread_id = qemu_get_thread_id();
Pavel Dovgalyuk626cf8f2014-12-08 10:53:17 +03001101 cpu->can_do_io = 1;
Andreas Färber4917cf42013-05-27 05:17:50 +02001102 current_cpu = cpu;
Blue Swirl296af7c2010-03-29 19:23:50 +00001103
Andreas Färber504134d2012-12-17 06:38:45 +01001104 r = kvm_init_vcpu(cpu);
Jan Kiszka84b49152011-02-01 22:15:50 +01001105 if (r < 0) {
1106 fprintf(stderr, "kvm_init_vcpu failed: %s\n", strerror(-r));
1107 exit(1);
1108 }
Blue Swirl296af7c2010-03-29 19:23:50 +00001109
Paolo Bonzini18268b62017-02-09 09:41:14 +01001110 kvm_init_cpu_signals(cpu);
Blue Swirl296af7c2010-03-29 19:23:50 +00001111
1112 /* signal CPU creation */
Andreas Färber61a46212012-05-02 22:49:36 +02001113 cpu->created = true;
Blue Swirl296af7c2010-03-29 19:23:50 +00001114 qemu_cond_signal(&qemu_cpu_cond);
1115
Gu Zheng4c055ab2016-05-12 09:18:13 +05301116 do {
Andreas Färbera1fcaa72012-05-02 23:42:26 +02001117 if (cpu_can_run(cpu)) {
Andreas Färber1458c362013-05-26 23:46:55 +02001118 r = kvm_cpu_exec(cpu);
Jan Kiszka83f338f2011-02-07 12:19:17 +01001119 if (r == EXCP_DEBUG) {
Andreas Färber91325042013-05-27 02:07:49 +02001120 cpu_handle_guest_debug(cpu);
Jan Kiszka83f338f2011-02-07 12:19:17 +01001121 }
Jan Kiszka0ab07c62011-02-07 12:19:14 +01001122 }
Andreas Färberfd529e82013-05-26 23:24:55 +02001123 qemu_kvm_wait_io_event(cpu);
Gu Zheng4c055ab2016-05-12 09:18:13 +05301124 } while (!cpu->unplug || cpu_can_run(cpu));
Blue Swirl296af7c2010-03-29 19:23:50 +00001125
Gu Zheng4c055ab2016-05-12 09:18:13 +05301126 qemu_kvm_destroy_vcpu(cpu);
Bharata B Rao2c579042016-05-12 09:18:14 +05301127 cpu->created = false;
1128 qemu_cond_signal(&qemu_cpu_cond);
Gu Zheng4c055ab2016-05-12 09:18:13 +05301129 qemu_mutex_unlock_iothread();
Blue Swirl296af7c2010-03-29 19:23:50 +00001130 return NULL;
1131}
1132
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +02001133static void *qemu_dummy_cpu_thread_fn(void *arg)
1134{
1135#ifdef _WIN32
1136 fprintf(stderr, "qtest is not supported under Windows\n");
1137 exit(1);
1138#else
Andreas Färber10a90212013-05-27 02:24:35 +02001139 CPUState *cpu = arg;
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +02001140 sigset_t waitset;
1141 int r;
1142
Paolo Bonziniab28bd22015-07-09 08:55:38 +02001143 rcu_register_thread();
1144
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +02001145 qemu_mutex_lock_iothread();
Andreas Färber814e6122012-05-02 17:00:37 +02001146 qemu_thread_get_self(cpu->thread);
Andreas Färber9f09e182012-05-03 06:59:07 +02001147 cpu->thread_id = qemu_get_thread_id();
Pavel Dovgalyuk626cf8f2014-12-08 10:53:17 +03001148 cpu->can_do_io = 1;
Alex Bennée37257942017-02-23 18:29:14 +00001149 current_cpu = cpu;
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +02001150
1151 sigemptyset(&waitset);
1152 sigaddset(&waitset, SIG_IPI);
1153
1154 /* signal CPU creation */
Andreas Färber61a46212012-05-02 22:49:36 +02001155 cpu->created = true;
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +02001156 qemu_cond_signal(&qemu_cpu_cond);
1157
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +02001158 while (1) {
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +02001159 qemu_mutex_unlock_iothread();
1160 do {
1161 int sig;
1162 r = sigwait(&waitset, &sig);
1163 } while (r == -1 && (errno == EAGAIN || errno == EINTR));
1164 if (r == -1) {
1165 perror("sigwait");
1166 exit(1);
1167 }
1168 qemu_mutex_lock_iothread();
Andreas Färber509a0d72012-05-03 02:18:09 +02001169 qemu_wait_io_event_common(cpu);
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +02001170 }
1171
1172 return NULL;
1173#endif
1174}
1175
Alex Bennée1be7fcb2016-10-27 16:10:08 +01001176static int64_t tcg_get_icount_limit(void)
1177{
1178 int64_t deadline;
1179
1180 if (replay_mode != REPLAY_MODE_PLAY) {
1181 deadline = qemu_clock_deadline_ns_all(QEMU_CLOCK_VIRTUAL);
1182
1183 /* Maintain prior (possibly buggy) behaviour where if no deadline
1184 * was set (as there is no QEMU_CLOCK_VIRTUAL timer) or it is more than
1185 * INT32_MAX nanoseconds ahead, we still use INT32_MAX
1186 * nanoseconds.
1187 */
1188 if ((deadline < 0) || (deadline > INT32_MAX)) {
1189 deadline = INT32_MAX;
1190 }
1191
1192 return qemu_icount_round(deadline);
1193 } else {
1194 return replay_get_instructions();
1195 }
1196}
1197
Alex Bennée12e97002016-10-27 16:10:14 +01001198static void handle_icount_deadline(void)
1199{
Paolo Bonzini6b8f0182017-03-02 19:56:40 +01001200 assert(qemu_in_vcpu_thread());
Alex Bennée12e97002016-10-27 16:10:14 +01001201 if (use_icount) {
1202 int64_t deadline =
1203 qemu_clock_deadline_ns_all(QEMU_CLOCK_VIRTUAL);
1204
1205 if (deadline == 0) {
Paolo Bonzini6b8f0182017-03-02 19:56:40 +01001206 /* Wake up other AioContexts. */
Alex Bennée12e97002016-10-27 16:10:14 +01001207 qemu_clock_notify(QEMU_CLOCK_VIRTUAL);
Paolo Bonzini6b8f0182017-03-02 19:56:40 +01001208 qemu_clock_run_timers(QEMU_CLOCK_VIRTUAL);
Alex Bennée12e97002016-10-27 16:10:14 +01001209 }
1210 }
1211}
1212
Alex Bennée05248382017-03-29 16:46:59 +01001213static void prepare_icount_for_run(CPUState *cpu)
1214{
1215 if (use_icount) {
Alex Bennéeeda5f7c2017-04-05 12:35:48 +01001216 int insns_left;
Alex Bennée05248382017-03-29 16:46:59 +01001217
1218 /* These should always be cleared by process_icount_data after
1219 * each vCPU execution. However u16.high can be raised
1220 * asynchronously by cpu_exit/cpu_interrupt/tcg_handle_interrupt
1221 */
1222 g_assert(cpu->icount_decr.u16.low == 0);
1223 g_assert(cpu->icount_extra == 0);
1224
Alex Bennéeeda5f7c2017-04-05 12:35:48 +01001225 cpu->icount_budget = tcg_get_icount_limit();
1226 insns_left = MIN(0xffff, cpu->icount_budget);
1227 cpu->icount_decr.u16.low = insns_left;
1228 cpu->icount_extra = cpu->icount_budget - insns_left;
Alex Bennée05248382017-03-29 16:46:59 +01001229 }
1230}
1231
1232static void process_icount_data(CPUState *cpu)
1233{
1234 if (use_icount) {
Alex Bennéee4cd9652017-03-31 16:09:42 +01001235 /* Account for executed instructions */
Alex Bennée512d3c82017-04-05 12:32:37 +01001236 cpu_update_icount(cpu);
Alex Bennée05248382017-03-29 16:46:59 +01001237
1238 /* Reset the counters */
1239 cpu->icount_decr.u16.low = 0;
1240 cpu->icount_extra = 0;
Alex Bennéee4cd9652017-03-31 16:09:42 +01001241 cpu->icount_budget = 0;
1242
Alex Bennée05248382017-03-29 16:46:59 +01001243 replay_account_executed_instructions();
1244 }
1245}
1246
1247
Alex Bennée1be7fcb2016-10-27 16:10:08 +01001248static int tcg_cpu_exec(CPUState *cpu)
1249{
1250 int ret;
1251#ifdef CONFIG_PROFILER
1252 int64_t ti;
1253#endif
1254
1255#ifdef CONFIG_PROFILER
1256 ti = profile_getclock();
1257#endif
Jan Kiszka8d04fb52017-02-23 18:29:11 +00001258 qemu_mutex_unlock_iothread();
Alex Bennée1be7fcb2016-10-27 16:10:08 +01001259 cpu_exec_start(cpu);
1260 ret = cpu_exec(cpu);
1261 cpu_exec_end(cpu);
Jan Kiszka8d04fb52017-02-23 18:29:11 +00001262 qemu_mutex_lock_iothread();
Alex Bennée1be7fcb2016-10-27 16:10:08 +01001263#ifdef CONFIG_PROFILER
1264 tcg_time += profile_getclock() - ti;
1265#endif
Alex Bennée1be7fcb2016-10-27 16:10:08 +01001266 return ret;
1267}
1268
Alex Bennéec93bbbe2016-10-27 16:10:09 +01001269/* Destroy any remaining vCPUs which have been unplugged and have
1270 * finished running
1271 */
1272static void deal_with_unplugged_cpus(void)
Alex Bennée1be7fcb2016-10-27 16:10:08 +01001273{
Alex Bennéec93bbbe2016-10-27 16:10:09 +01001274 CPUState *cpu;
Alex Bennée1be7fcb2016-10-27 16:10:08 +01001275
Alex Bennéec93bbbe2016-10-27 16:10:09 +01001276 CPU_FOREACH(cpu) {
1277 if (cpu->unplug && !cpu_can_run(cpu)) {
1278 qemu_tcg_destroy_vcpu(cpu);
1279 cpu->created = false;
1280 qemu_cond_signal(&qemu_cpu_cond);
Alex Bennée1be7fcb2016-10-27 16:10:08 +01001281 break;
1282 }
1283 }
Alex Bennée1be7fcb2016-10-27 16:10:08 +01001284}
Jan Kiszkabdb7ca62011-09-26 09:40:39 +02001285
Alex Bennée65467062017-02-23 18:29:09 +00001286/* Single-threaded TCG
1287 *
1288 * In the single-threaded case each vCPU is simulated in turn. If
1289 * there is more than a single vCPU we create a simple timer to kick
1290 * the vCPU and ensure we don't get stuck in a tight loop in one vCPU.
1291 * This is done explicitly rather than relying on side-effects
1292 * elsewhere.
1293 */
1294
Alex Bennée37257942017-02-23 18:29:14 +00001295static void *qemu_tcg_rr_cpu_thread_fn(void *arg)
Blue Swirl296af7c2010-03-29 19:23:50 +00001296{
Andreas Färberc3586ba2012-05-03 01:41:24 +02001297 CPUState *cpu = arg;
Blue Swirl296af7c2010-03-29 19:23:50 +00001298
Paolo Bonziniab28bd22015-07-09 08:55:38 +02001299 rcu_register_thread();
1300
Paolo Bonzini2e7f7a32015-06-18 18:47:18 +02001301 qemu_mutex_lock_iothread();
Andreas Färber814e6122012-05-02 17:00:37 +02001302 qemu_thread_get_self(cpu->thread);
Blue Swirl296af7c2010-03-29 19:23:50 +00001303
Andreas Färber38fcbd32013-07-07 19:50:23 +02001304 CPU_FOREACH(cpu) {
1305 cpu->thread_id = qemu_get_thread_id();
1306 cpu->created = true;
Pavel Dovgalyuk626cf8f2014-12-08 10:53:17 +03001307 cpu->can_do_io = 1;
Andreas Färber38fcbd32013-07-07 19:50:23 +02001308 }
Blue Swirl296af7c2010-03-29 19:23:50 +00001309 qemu_cond_signal(&qemu_cpu_cond);
1310
Jan Kiszkafa7d1862011-08-22 18:35:25 +02001311 /* wait for initial kick-off after machine start */
Emilio G. Cotac28e3992015-04-27 12:45:28 -04001312 while (first_cpu->stopped) {
KONRAD Fredericd5f8d612015-08-10 17:27:06 +02001313 qemu_cond_wait(first_cpu->halt_cond, &qemu_global_mutex);
Jan Kiszka8e564b42012-02-17 18:31:15 +01001314
1315 /* process any pending work */
Andreas Färberbdc44642013-06-24 23:50:24 +02001316 CPU_FOREACH(cpu) {
Alex Bennée37257942017-02-23 18:29:14 +00001317 current_cpu = cpu;
Andreas Färber182735e2013-05-29 22:29:20 +02001318 qemu_wait_io_event_common(cpu);
Jan Kiszka8e564b42012-02-17 18:31:15 +01001319 }
Jan Kiszka0ab07c62011-02-07 12:19:14 +01001320 }
Blue Swirl296af7c2010-03-29 19:23:50 +00001321
Alex Bennée65467062017-02-23 18:29:09 +00001322 start_tcg_kick_timer();
1323
Alex Bennéec93bbbe2016-10-27 16:10:09 +01001324 cpu = first_cpu;
1325
Alex Bennéee5143e32017-02-23 18:29:12 +00001326 /* process any pending work */
1327 cpu->exit_request = 1;
1328
Blue Swirl296af7c2010-03-29 19:23:50 +00001329 while (1) {
Alex Bennéec93bbbe2016-10-27 16:10:09 +01001330 /* Account partial waits to QEMU_CLOCK_VIRTUAL. */
1331 qemu_account_warp_timer();
1332
Paolo Bonzini6b8f0182017-03-02 19:56:40 +01001333 /* Run the timers here. This is much more efficient than
1334 * waking up the I/O thread and waiting for completion.
1335 */
1336 handle_icount_deadline();
1337
Alex Bennéec93bbbe2016-10-27 16:10:09 +01001338 if (!cpu) {
1339 cpu = first_cpu;
1340 }
1341
Alex Bennéee5143e32017-02-23 18:29:12 +00001342 while (cpu && !cpu->queued_work_first && !cpu->exit_request) {
1343
Alex Bennée791158d2017-02-23 18:29:10 +00001344 atomic_mb_set(&tcg_current_rr_cpu, cpu);
Alex Bennée37257942017-02-23 18:29:14 +00001345 current_cpu = cpu;
Alex Bennéec93bbbe2016-10-27 16:10:09 +01001346
1347 qemu_clock_enable(QEMU_CLOCK_VIRTUAL,
1348 (cpu->singlestep_enabled & SSTEP_NOTIMER) == 0);
1349
1350 if (cpu_can_run(cpu)) {
1351 int r;
Alex Bennée05248382017-03-29 16:46:59 +01001352
1353 prepare_icount_for_run(cpu);
1354
Alex Bennéec93bbbe2016-10-27 16:10:09 +01001355 r = tcg_cpu_exec(cpu);
Alex Bennée05248382017-03-29 16:46:59 +01001356
1357 process_icount_data(cpu);
1358
Alex Bennéec93bbbe2016-10-27 16:10:09 +01001359 if (r == EXCP_DEBUG) {
1360 cpu_handle_guest_debug(cpu);
1361 break;
Pranith Kumar08e73c42017-02-23 18:29:15 +00001362 } else if (r == EXCP_ATOMIC) {
1363 qemu_mutex_unlock_iothread();
1364 cpu_exec_step_atomic(cpu);
1365 qemu_mutex_lock_iothread();
1366 break;
Alex Bennéec93bbbe2016-10-27 16:10:09 +01001367 }
Alex Bennée37257942017-02-23 18:29:14 +00001368 } else if (cpu->stop) {
Alex Bennéec93bbbe2016-10-27 16:10:09 +01001369 if (cpu->unplug) {
1370 cpu = CPU_NEXT(cpu);
1371 }
1372 break;
1373 }
1374
Alex Bennéee5143e32017-02-23 18:29:12 +00001375 cpu = CPU_NEXT(cpu);
1376 } /* while (cpu && !cpu->exit_request).. */
1377
Alex Bennée791158d2017-02-23 18:29:10 +00001378 /* Does not need atomic_mb_set because a spurious wakeup is okay. */
1379 atomic_set(&tcg_current_rr_cpu, NULL);
Alex Bennéec93bbbe2016-10-27 16:10:09 +01001380
Alex Bennéee5143e32017-02-23 18:29:12 +00001381 if (cpu && cpu->exit_request) {
1382 atomic_mb_set(&cpu->exit_request, 0);
1383 }
Alex Blighac70aaf2013-08-21 16:02:57 +01001384
Alex Bennée37257942017-02-23 18:29:14 +00001385 qemu_tcg_wait_io_event(cpu ? cpu : QTAILQ_FIRST(&cpus));
Alex Bennéec93bbbe2016-10-27 16:10:09 +01001386 deal_with_unplugged_cpus();
Blue Swirl296af7c2010-03-29 19:23:50 +00001387 }
1388
1389 return NULL;
1390}
1391
Vincent Palatinb0cb0a62017-01-10 11:59:57 +01001392static void *qemu_hax_cpu_thread_fn(void *arg)
1393{
1394 CPUState *cpu = arg;
1395 int r;
Vincent Palatinb3d3a422017-03-20 11:15:49 +01001396
1397 qemu_mutex_lock_iothread();
Vincent Palatinb0cb0a62017-01-10 11:59:57 +01001398 qemu_thread_get_self(cpu->thread);
Vincent Palatinb0cb0a62017-01-10 11:59:57 +01001399
1400 cpu->thread_id = qemu_get_thread_id();
1401 cpu->created = true;
1402 cpu->halted = 0;
1403 current_cpu = cpu;
1404
1405 hax_init_vcpu(cpu);
1406 qemu_cond_signal(&qemu_cpu_cond);
1407
1408 while (1) {
1409 if (cpu_can_run(cpu)) {
1410 r = hax_smp_cpu_exec(cpu);
1411 if (r == EXCP_DEBUG) {
1412 cpu_handle_guest_debug(cpu);
1413 }
1414 }
1415
1416 while (cpu_thread_is_idle(cpu)) {
1417 qemu_cond_wait(cpu->halt_cond, &qemu_global_mutex);
1418 }
1419#ifdef _WIN32
1420 SleepEx(0, TRUE);
1421#endif
1422 qemu_wait_io_event_common(cpu);
1423 }
1424 return NULL;
1425}
1426
1427#ifdef _WIN32
1428static void CALLBACK dummy_apc_func(ULONG_PTR unused)
1429{
1430}
1431#endif
1432
Alex Bennée37257942017-02-23 18:29:14 +00001433/* Multi-threaded TCG
1434 *
1435 * In the multi-threaded case each vCPU has its own thread. The TLS
1436 * variable current_cpu can be used deep in the code to find the
1437 * current CPUState for a given thread.
1438 */
1439
1440static void *qemu_tcg_cpu_thread_fn(void *arg)
1441{
1442 CPUState *cpu = arg;
1443
Alex Bennéebf51c722017-03-30 18:32:29 +01001444 g_assert(!use_icount);
1445
Alex Bennée37257942017-02-23 18:29:14 +00001446 rcu_register_thread();
1447
1448 qemu_mutex_lock_iothread();
1449 qemu_thread_get_self(cpu->thread);
1450
1451 cpu->thread_id = qemu_get_thread_id();
1452 cpu->created = true;
1453 cpu->can_do_io = 1;
1454 current_cpu = cpu;
1455 qemu_cond_signal(&qemu_cpu_cond);
1456
1457 /* process any pending work */
1458 cpu->exit_request = 1;
1459
1460 while (1) {
1461 if (cpu_can_run(cpu)) {
1462 int r;
1463 r = tcg_cpu_exec(cpu);
1464 switch (r) {
1465 case EXCP_DEBUG:
1466 cpu_handle_guest_debug(cpu);
1467 break;
1468 case EXCP_HALTED:
1469 /* during start-up the vCPU is reset and the thread is
1470 * kicked several times. If we don't ensure we go back
1471 * to sleep in the halted state we won't cleanly
1472 * start-up when the vCPU is enabled.
1473 *
1474 * cpu->halted should ensure we sleep in wait_io_event
1475 */
1476 g_assert(cpu->halted);
1477 break;
Pranith Kumar08e73c42017-02-23 18:29:15 +00001478 case EXCP_ATOMIC:
1479 qemu_mutex_unlock_iothread();
1480 cpu_exec_step_atomic(cpu);
1481 qemu_mutex_lock_iothread();
Alex Bennée37257942017-02-23 18:29:14 +00001482 default:
1483 /* Ignore everything else? */
1484 break;
1485 }
1486 }
1487
Alex Bennée37257942017-02-23 18:29:14 +00001488 atomic_mb_set(&cpu->exit_request, 0);
1489 qemu_tcg_wait_io_event(cpu);
1490 }
1491
1492 return NULL;
1493}
1494
Andreas Färber2ff09a42012-05-03 00:23:30 +02001495static void qemu_cpu_kick_thread(CPUState *cpu)
Paolo Bonzinicc015e92011-03-12 17:44:08 +01001496{
1497#ifndef _WIN32
1498 int err;
1499
Paolo Bonzinie0c38212015-08-26 00:19:19 +02001500 if (cpu->thread_kicked) {
1501 return;
Paolo Bonzini9102ded2015-08-18 06:52:09 -07001502 }
Paolo Bonzinie0c38212015-08-26 00:19:19 +02001503 cpu->thread_kicked = true;
Andreas Färber814e6122012-05-02 17:00:37 +02001504 err = pthread_kill(cpu->thread->thread, SIG_IPI);
Paolo Bonzinicc015e92011-03-12 17:44:08 +01001505 if (err) {
1506 fprintf(stderr, "qemu:%s: %s", __func__, strerror(err));
1507 exit(1);
1508 }
1509#else /* _WIN32 */
Vincent Palatinb0cb0a62017-01-10 11:59:57 +01001510 if (!qemu_cpu_is_self(cpu)) {
1511 if (!QueueUserAPC(dummy_apc_func, cpu->hThread, 0)) {
1512 fprintf(stderr, "%s: QueueUserAPC failed with error %lu\n",
1513 __func__, GetLastError());
1514 exit(1);
1515 }
1516 }
Paolo Bonzinicc015e92011-03-12 17:44:08 +01001517#endif
1518}
1519
Andreas Färberc08d7422012-05-03 04:34:15 +02001520void qemu_cpu_kick(CPUState *cpu)
Blue Swirl296af7c2010-03-29 19:23:50 +00001521{
Andreas Färberf5c121b2012-05-03 01:22:49 +02001522 qemu_cond_broadcast(cpu->halt_cond);
Paolo Bonzinie0c38212015-08-26 00:19:19 +02001523 if (tcg_enabled()) {
Alex Bennée791158d2017-02-23 18:29:10 +00001524 cpu_exit(cpu);
Alex Bennée37257942017-02-23 18:29:14 +00001525 /* NOP unless doing single-thread RR */
Alex Bennée791158d2017-02-23 18:29:10 +00001526 qemu_cpu_kick_rr_cpu();
Paolo Bonzinie0c38212015-08-26 00:19:19 +02001527 } else {
Vincent Palatinb0cb0a62017-01-10 11:59:57 +01001528 if (hax_enabled()) {
1529 /*
1530 * FIXME: race condition with the exit_request check in
1531 * hax_vcpu_hax_exec
1532 */
1533 cpu->exit_request = 1;
1534 }
Paolo Bonzinie0c38212015-08-26 00:19:19 +02001535 qemu_cpu_kick_thread(cpu);
1536 }
Blue Swirl296af7c2010-03-29 19:23:50 +00001537}
1538
Jan Kiszka46d62fa2011-02-01 22:15:59 +01001539void qemu_cpu_kick_self(void)
1540{
Andreas Färber4917cf42013-05-27 05:17:50 +02001541 assert(current_cpu);
Paolo Bonzini9102ded2015-08-18 06:52:09 -07001542 qemu_cpu_kick_thread(current_cpu);
Blue Swirl296af7c2010-03-29 19:23:50 +00001543}
1544
Andreas Färber60e82572012-05-02 22:23:49 +02001545bool qemu_cpu_is_self(CPUState *cpu)
Blue Swirl296af7c2010-03-29 19:23:50 +00001546{
Andreas Färber814e6122012-05-02 17:00:37 +02001547 return qemu_thread_is_self(cpu->thread);
Blue Swirl296af7c2010-03-29 19:23:50 +00001548}
1549
Paolo Bonzini79e2b9a2015-01-21 12:09:14 +01001550bool qemu_in_vcpu_thread(void)
Juan Quintelaaa723c22012-09-18 16:30:11 +02001551{
Andreas Färber4917cf42013-05-27 05:17:50 +02001552 return current_cpu && qemu_cpu_is_self(current_cpu);
Juan Quintelaaa723c22012-09-18 16:30:11 +02001553}
1554
Paolo Bonziniafbe7052015-06-18 18:47:19 +02001555static __thread bool iothread_locked = false;
1556
1557bool qemu_mutex_iothread_locked(void)
1558{
1559 return iothread_locked;
1560}
1561
Blue Swirl296af7c2010-03-29 19:23:50 +00001562void qemu_mutex_lock_iothread(void)
1563{
Jan Kiszka8d04fb52017-02-23 18:29:11 +00001564 g_assert(!qemu_mutex_iothread_locked());
1565 qemu_mutex_lock(&qemu_global_mutex);
Paolo Bonziniafbe7052015-06-18 18:47:19 +02001566 iothread_locked = true;
Blue Swirl296af7c2010-03-29 19:23:50 +00001567}
1568
1569void qemu_mutex_unlock_iothread(void)
1570{
Jan Kiszka8d04fb52017-02-23 18:29:11 +00001571 g_assert(qemu_mutex_iothread_locked());
Paolo Bonziniafbe7052015-06-18 18:47:19 +02001572 iothread_locked = false;
Blue Swirl296af7c2010-03-29 19:23:50 +00001573 qemu_mutex_unlock(&qemu_global_mutex);
1574}
1575
Alex Bennéee8faee02016-10-27 16:09:58 +01001576static bool all_vcpus_paused(void)
Blue Swirl296af7c2010-03-29 19:23:50 +00001577{
Andreas Färberbdc44642013-06-24 23:50:24 +02001578 CPUState *cpu;
Blue Swirl296af7c2010-03-29 19:23:50 +00001579
Andreas Färberbdc44642013-06-24 23:50:24 +02001580 CPU_FOREACH(cpu) {
Andreas Färber182735e2013-05-29 22:29:20 +02001581 if (!cpu->stopped) {
Alex Bennéee8faee02016-10-27 16:09:58 +01001582 return false;
Jan Kiszka0ab07c62011-02-07 12:19:14 +01001583 }
Blue Swirl296af7c2010-03-29 19:23:50 +00001584 }
1585
Alex Bennéee8faee02016-10-27 16:09:58 +01001586 return true;
Blue Swirl296af7c2010-03-29 19:23:50 +00001587}
1588
1589void pause_all_vcpus(void)
1590{
Andreas Färberbdc44642013-06-24 23:50:24 +02001591 CPUState *cpu;
Blue Swirl296af7c2010-03-29 19:23:50 +00001592
Alex Bligh40daca52013-08-21 16:03:02 +01001593 qemu_clock_enable(QEMU_CLOCK_VIRTUAL, false);
Andreas Färberbdc44642013-06-24 23:50:24 +02001594 CPU_FOREACH(cpu) {
Andreas Färber182735e2013-05-29 22:29:20 +02001595 cpu->stop = true;
1596 qemu_cpu_kick(cpu);
Blue Swirl296af7c2010-03-29 19:23:50 +00001597 }
1598
Juan Quintelaaa723c22012-09-18 16:30:11 +02001599 if (qemu_in_vcpu_thread()) {
Jan Kiszkad798e972012-02-17 18:31:16 +01001600 cpu_stop_current();
Jan Kiszkad798e972012-02-17 18:31:16 +01001601 }
1602
Blue Swirl296af7c2010-03-29 19:23:50 +00001603 while (!all_vcpus_paused()) {
Paolo Bonzinibe7d6c52011-03-12 17:44:02 +01001604 qemu_cond_wait(&qemu_pause_cond, &qemu_global_mutex);
Andreas Färberbdc44642013-06-24 23:50:24 +02001605 CPU_FOREACH(cpu) {
Andreas Färber182735e2013-05-29 22:29:20 +02001606 qemu_cpu_kick(cpu);
Blue Swirl296af7c2010-03-29 19:23:50 +00001607 }
1608 }
1609}
1610
Igor Mammedov29936832013-04-23 10:29:37 +02001611void cpu_resume(CPUState *cpu)
1612{
1613 cpu->stop = false;
1614 cpu->stopped = false;
1615 qemu_cpu_kick(cpu);
1616}
1617
Blue Swirl296af7c2010-03-29 19:23:50 +00001618void resume_all_vcpus(void)
1619{
Andreas Färberbdc44642013-06-24 23:50:24 +02001620 CPUState *cpu;
Blue Swirl296af7c2010-03-29 19:23:50 +00001621
Alex Bligh40daca52013-08-21 16:03:02 +01001622 qemu_clock_enable(QEMU_CLOCK_VIRTUAL, true);
Andreas Färberbdc44642013-06-24 23:50:24 +02001623 CPU_FOREACH(cpu) {
Andreas Färber182735e2013-05-29 22:29:20 +02001624 cpu_resume(cpu);
Blue Swirl296af7c2010-03-29 19:23:50 +00001625 }
1626}
1627
Gu Zheng4c055ab2016-05-12 09:18:13 +05301628void cpu_remove(CPUState *cpu)
1629{
1630 cpu->stop = true;
1631 cpu->unplug = true;
1632 qemu_cpu_kick(cpu);
1633}
1634
Bharata B Rao2c579042016-05-12 09:18:14 +05301635void cpu_remove_sync(CPUState *cpu)
1636{
1637 cpu_remove(cpu);
1638 while (cpu->created) {
1639 qemu_cond_wait(&qemu_cpu_cond, &qemu_global_mutex);
1640 }
1641}
1642
Dr. David Alan Gilbert49001162014-01-30 10:20:32 +00001643/* For temporary buffers for forming a name */
1644#define VCPU_THREAD_NAME_SIZE 16
1645
Andreas Färbere5ab30a2012-05-03 01:50:44 +02001646static void qemu_tcg_init_vcpu(CPUState *cpu)
Blue Swirl296af7c2010-03-29 19:23:50 +00001647{
Dr. David Alan Gilbert49001162014-01-30 10:20:32 +00001648 char thread_name[VCPU_THREAD_NAME_SIZE];
Alex Bennée37257942017-02-23 18:29:14 +00001649 static QemuCond *single_tcg_halt_cond;
1650 static QemuThread *single_tcg_cpu_thread;
Dr. David Alan Gilbert49001162014-01-30 10:20:32 +00001651
Alex Bennée37257942017-02-23 18:29:14 +00001652 if (qemu_tcg_mttcg_enabled() || !single_tcg_cpu_thread) {
Andreas Färber814e6122012-05-02 17:00:37 +02001653 cpu->thread = g_malloc0(sizeof(QemuThread));
Andreas Färberf5c121b2012-05-03 01:22:49 +02001654 cpu->halt_cond = g_malloc0(sizeof(QemuCond));
1655 qemu_cond_init(cpu->halt_cond);
Alex Bennée37257942017-02-23 18:29:14 +00001656
1657 if (qemu_tcg_mttcg_enabled()) {
1658 /* create a thread per vCPU with TCG (MTTCG) */
1659 parallel_cpus = true;
1660 snprintf(thread_name, VCPU_THREAD_NAME_SIZE, "CPU %d/TCG",
Dr. David Alan Gilbert49001162014-01-30 10:20:32 +00001661 cpu->cpu_index);
Alex Bennée37257942017-02-23 18:29:14 +00001662
1663 qemu_thread_create(cpu->thread, thread_name, qemu_tcg_cpu_thread_fn,
1664 cpu, QEMU_THREAD_JOINABLE);
1665
1666 } else {
1667 /* share a single thread for all cpus with TCG */
1668 snprintf(thread_name, VCPU_THREAD_NAME_SIZE, "ALL CPUs/TCG");
1669 qemu_thread_create(cpu->thread, thread_name,
1670 qemu_tcg_rr_cpu_thread_fn,
1671 cpu, QEMU_THREAD_JOINABLE);
1672
1673 single_tcg_halt_cond = cpu->halt_cond;
1674 single_tcg_cpu_thread = cpu->thread;
1675 }
Paolo Bonzini1ecf47b2011-12-13 13:43:52 +01001676#ifdef _WIN32
Andreas Färber814e6122012-05-02 17:00:37 +02001677 cpu->hThread = qemu_thread_get_handle(cpu->thread);
Paolo Bonzini1ecf47b2011-12-13 13:43:52 +01001678#endif
Andreas Färber61a46212012-05-02 22:49:36 +02001679 while (!cpu->created) {
Paolo Bonzini18a85722011-03-12 17:44:03 +01001680 qemu_cond_wait(&qemu_cpu_cond, &qemu_global_mutex);
Jan Kiszka0ab07c62011-02-07 12:19:14 +01001681 }
Blue Swirl296af7c2010-03-29 19:23:50 +00001682 } else {
Alex Bennée37257942017-02-23 18:29:14 +00001683 /* For non-MTTCG cases we share the thread */
1684 cpu->thread = single_tcg_cpu_thread;
1685 cpu->halt_cond = single_tcg_halt_cond;
Blue Swirl296af7c2010-03-29 19:23:50 +00001686 }
1687}
1688
Vincent Palatinb0cb0a62017-01-10 11:59:57 +01001689static void qemu_hax_start_vcpu(CPUState *cpu)
1690{
1691 char thread_name[VCPU_THREAD_NAME_SIZE];
1692
1693 cpu->thread = g_malloc0(sizeof(QemuThread));
1694 cpu->halt_cond = g_malloc0(sizeof(QemuCond));
1695 qemu_cond_init(cpu->halt_cond);
1696
1697 snprintf(thread_name, VCPU_THREAD_NAME_SIZE, "CPU %d/HAX",
1698 cpu->cpu_index);
1699 qemu_thread_create(cpu->thread, thread_name, qemu_hax_cpu_thread_fn,
1700 cpu, QEMU_THREAD_JOINABLE);
1701#ifdef _WIN32
1702 cpu->hThread = qemu_thread_get_handle(cpu->thread);
1703#endif
1704 while (!cpu->created) {
1705 qemu_cond_wait(&qemu_cpu_cond, &qemu_global_mutex);
1706 }
1707}
1708
Andreas Färber48a106b2013-05-27 02:20:39 +02001709static void qemu_kvm_start_vcpu(CPUState *cpu)
Blue Swirl296af7c2010-03-29 19:23:50 +00001710{
Dr. David Alan Gilbert49001162014-01-30 10:20:32 +00001711 char thread_name[VCPU_THREAD_NAME_SIZE];
1712
Andreas Färber814e6122012-05-02 17:00:37 +02001713 cpu->thread = g_malloc0(sizeof(QemuThread));
Andreas Färberf5c121b2012-05-03 01:22:49 +02001714 cpu->halt_cond = g_malloc0(sizeof(QemuCond));
1715 qemu_cond_init(cpu->halt_cond);
Dr. David Alan Gilbert49001162014-01-30 10:20:32 +00001716 snprintf(thread_name, VCPU_THREAD_NAME_SIZE, "CPU %d/KVM",
1717 cpu->cpu_index);
1718 qemu_thread_create(cpu->thread, thread_name, qemu_kvm_cpu_thread_fn,
1719 cpu, QEMU_THREAD_JOINABLE);
Andreas Färber61a46212012-05-02 22:49:36 +02001720 while (!cpu->created) {
Paolo Bonzini18a85722011-03-12 17:44:03 +01001721 qemu_cond_wait(&qemu_cpu_cond, &qemu_global_mutex);
Jan Kiszka0ab07c62011-02-07 12:19:14 +01001722 }
Blue Swirl296af7c2010-03-29 19:23:50 +00001723}
1724
Andreas Färber10a90212013-05-27 02:24:35 +02001725static void qemu_dummy_start_vcpu(CPUState *cpu)
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +02001726{
Dr. David Alan Gilbert49001162014-01-30 10:20:32 +00001727 char thread_name[VCPU_THREAD_NAME_SIZE];
1728
Andreas Färber814e6122012-05-02 17:00:37 +02001729 cpu->thread = g_malloc0(sizeof(QemuThread));
Andreas Färberf5c121b2012-05-03 01:22:49 +02001730 cpu->halt_cond = g_malloc0(sizeof(QemuCond));
1731 qemu_cond_init(cpu->halt_cond);
Dr. David Alan Gilbert49001162014-01-30 10:20:32 +00001732 snprintf(thread_name, VCPU_THREAD_NAME_SIZE, "CPU %d/DUMMY",
1733 cpu->cpu_index);
1734 qemu_thread_create(cpu->thread, thread_name, qemu_dummy_cpu_thread_fn, cpu,
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +02001735 QEMU_THREAD_JOINABLE);
Andreas Färber61a46212012-05-02 22:49:36 +02001736 while (!cpu->created) {
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +02001737 qemu_cond_wait(&qemu_cpu_cond, &qemu_global_mutex);
1738 }
1739}
1740
Andreas Färberc643bed2013-05-27 03:23:24 +02001741void qemu_init_vcpu(CPUState *cpu)
Blue Swirl296af7c2010-03-29 19:23:50 +00001742{
Andreas Färberce3960e2012-12-17 03:27:07 +01001743 cpu->nr_cores = smp_cores;
1744 cpu->nr_threads = smp_threads;
Andreas Färberf324e762012-05-02 23:26:21 +02001745 cpu->stopped = true;
Peter Maydell56943e82016-01-21 14:15:04 +00001746
1747 if (!cpu->as) {
1748 /* If the target cpu hasn't set up any address spaces itself,
1749 * give it the default one.
1750 */
Peter Crosthwaite6731d862016-01-21 14:15:06 +00001751 AddressSpace *as = address_space_init_shareable(cpu->memory,
1752 "cpu-memory");
Peter Maydell12ebc9a2016-01-21 14:15:04 +00001753 cpu->num_ases = 1;
Peter Crosthwaite6731d862016-01-21 14:15:06 +00001754 cpu_address_space_init(cpu, as, 0);
Peter Maydell56943e82016-01-21 14:15:04 +00001755 }
1756
Jan Kiszka0ab07c62011-02-07 12:19:14 +01001757 if (kvm_enabled()) {
Andreas Färber48a106b2013-05-27 02:20:39 +02001758 qemu_kvm_start_vcpu(cpu);
Vincent Palatinb0cb0a62017-01-10 11:59:57 +01001759 } else if (hax_enabled()) {
1760 qemu_hax_start_vcpu(cpu);
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +02001761 } else if (tcg_enabled()) {
Andreas Färbere5ab30a2012-05-03 01:50:44 +02001762 qemu_tcg_init_vcpu(cpu);
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +02001763 } else {
Andreas Färber10a90212013-05-27 02:24:35 +02001764 qemu_dummy_start_vcpu(cpu);
Jan Kiszka0ab07c62011-02-07 12:19:14 +01001765 }
Blue Swirl296af7c2010-03-29 19:23:50 +00001766}
1767
Jan Kiszkab4a3d962011-02-01 22:15:43 +01001768void cpu_stop_current(void)
Blue Swirl296af7c2010-03-29 19:23:50 +00001769{
Andreas Färber4917cf42013-05-27 05:17:50 +02001770 if (current_cpu) {
1771 current_cpu->stop = false;
1772 current_cpu->stopped = true;
1773 cpu_exit(current_cpu);
Dr. David Alan Gilbert96bce682016-01-25 10:08:18 +00001774 qemu_cond_broadcast(&qemu_pause_cond);
Jan Kiszkab4a3d962011-02-01 22:15:43 +01001775 }
Blue Swirl296af7c2010-03-29 19:23:50 +00001776}
1777
Kevin Wolf56983462013-07-05 13:49:54 +02001778int vm_stop(RunState state)
Blue Swirl296af7c2010-03-29 19:23:50 +00001779{
Juan Quintelaaa723c22012-09-18 16:30:11 +02001780 if (qemu_in_vcpu_thread()) {
Paolo Bonzini74892d22014-06-05 14:53:58 +02001781 qemu_system_vmstop_request_prepare();
Luiz Capitulino1dfb4dd2011-07-29 14:26:33 -03001782 qemu_system_vmstop_request(state);
Blue Swirl296af7c2010-03-29 19:23:50 +00001783 /*
1784 * FIXME: should not return to device code in case
1785 * vm_stop() has been requested.
1786 */
Jan Kiszkab4a3d962011-02-01 22:15:43 +01001787 cpu_stop_current();
Kevin Wolf56983462013-07-05 13:49:54 +02001788 return 0;
Blue Swirl296af7c2010-03-29 19:23:50 +00001789 }
Kevin Wolf56983462013-07-05 13:49:54 +02001790
1791 return do_vm_stop(state);
Blue Swirl296af7c2010-03-29 19:23:50 +00001792}
1793
Claudio Imbrenda2d76e822017-02-14 18:07:47 +01001794/**
1795 * Prepare for (re)starting the VM.
1796 * Returns -1 if the vCPUs are not to be restarted (e.g. if they are already
1797 * running or in case of an error condition), 0 otherwise.
1798 */
1799int vm_prepare_start(void)
1800{
1801 RunState requested;
1802 int res = 0;
1803
1804 qemu_vmstop_requested(&requested);
1805 if (runstate_is_running() && requested == RUN_STATE__MAX) {
1806 return -1;
1807 }
1808
1809 /* Ensure that a STOP/RESUME pair of events is emitted if a
1810 * vmstop request was pending. The BLOCK_IO_ERROR event, for
1811 * example, according to documentation is always followed by
1812 * the STOP event.
1813 */
1814 if (runstate_is_running()) {
1815 qapi_event_send_stop(&error_abort);
1816 res = -1;
1817 } else {
1818 replay_enable_events();
1819 cpu_enable_ticks();
1820 runstate_set(RUN_STATE_RUNNING);
1821 vm_state_notify(1, RUN_STATE_RUNNING);
1822 }
1823
1824 /* We are sending this now, but the CPUs will be resumed shortly later */
1825 qapi_event_send_resume(&error_abort);
1826 return res;
1827}
1828
1829void vm_start(void)
1830{
1831 if (!vm_prepare_start()) {
1832 resume_all_vcpus();
1833 }
1834}
1835
Luiz Capitulino8a9236f2011-10-14 11:18:09 -03001836/* does a state transition even if the VM is already stopped,
1837 current state is forgotten forever */
Kevin Wolf56983462013-07-05 13:49:54 +02001838int vm_stop_force_state(RunState state)
Luiz Capitulino8a9236f2011-10-14 11:18:09 -03001839{
1840 if (runstate_is_running()) {
Kevin Wolf56983462013-07-05 13:49:54 +02001841 return vm_stop(state);
Luiz Capitulino8a9236f2011-10-14 11:18:09 -03001842 } else {
1843 runstate_set(state);
Wen Congyangb2780d32015-11-20 17:34:38 +08001844
1845 bdrv_drain_all();
Kevin Wolf594a45c2013-07-18 14:52:19 +02001846 /* Make sure to return an error if the flush in a previous vm_stop()
1847 * failed. */
John Snow22af08e2016-09-22 21:45:51 -04001848 return bdrv_flush_all();
Luiz Capitulino8a9236f2011-10-14 11:18:09 -03001849 }
1850}
1851
Stefan Weil9a78eea2010-10-22 23:03:33 +02001852void list_cpus(FILE *f, fprintf_function cpu_fprintf, const char *optarg)
Blue Swirl262353c2010-05-04 19:55:35 +00001853{
1854 /* XXX: implement xxx_cpu_list for targets that still miss it */
Peter Maydelle916cbf2012-09-05 17:41:08 -03001855#if defined(cpu_list)
1856 cpu_list(f, cpu_fprintf);
Blue Swirl262353c2010-05-04 19:55:35 +00001857#endif
1858}
Luiz Capitulinode0b36b2011-09-21 16:38:35 -03001859
1860CpuInfoList *qmp_query_cpus(Error **errp)
1861{
1862 CpuInfoList *head = NULL, *cur_item = NULL;
Andreas Färber182735e2013-05-29 22:29:20 +02001863 CPUState *cpu;
Luiz Capitulinode0b36b2011-09-21 16:38:35 -03001864
Andreas Färberbdc44642013-06-24 23:50:24 +02001865 CPU_FOREACH(cpu) {
Luiz Capitulinode0b36b2011-09-21 16:38:35 -03001866 CpuInfoList *info;
Andreas Färber182735e2013-05-29 22:29:20 +02001867#if defined(TARGET_I386)
1868 X86CPU *x86_cpu = X86_CPU(cpu);
1869 CPUX86State *env = &x86_cpu->env;
1870#elif defined(TARGET_PPC)
1871 PowerPCCPU *ppc_cpu = POWERPC_CPU(cpu);
1872 CPUPPCState *env = &ppc_cpu->env;
1873#elif defined(TARGET_SPARC)
1874 SPARCCPU *sparc_cpu = SPARC_CPU(cpu);
1875 CPUSPARCState *env = &sparc_cpu->env;
1876#elif defined(TARGET_MIPS)
1877 MIPSCPU *mips_cpu = MIPS_CPU(cpu);
1878 CPUMIPSState *env = &mips_cpu->env;
Bastian Koppelmann48e06fe2014-09-01 12:59:46 +01001879#elif defined(TARGET_TRICORE)
1880 TriCoreCPU *tricore_cpu = TRICORE_CPU(cpu);
1881 CPUTriCoreState *env = &tricore_cpu->env;
Andreas Färber182735e2013-05-29 22:29:20 +02001882#endif
Luiz Capitulinode0b36b2011-09-21 16:38:35 -03001883
Andreas Färbercb446ec2013-05-01 14:24:52 +02001884 cpu_synchronize_state(cpu);
Luiz Capitulinode0b36b2011-09-21 16:38:35 -03001885
1886 info = g_malloc0(sizeof(*info));
1887 info->value = g_malloc0(sizeof(*info->value));
Andreas Färber55e5c282012-12-17 06:18:02 +01001888 info->value->CPU = cpu->cpu_index;
Andreas Färber182735e2013-05-29 22:29:20 +02001889 info->value->current = (cpu == first_cpu);
Andreas Färber259186a2013-01-17 18:51:17 +01001890 info->value->halted = cpu->halted;
Eduardo Habkost58f88d42015-05-08 16:04:22 -03001891 info->value->qom_path = object_get_canonical_path(OBJECT(cpu));
Andreas Färber9f09e182012-05-03 06:59:07 +02001892 info->value->thread_id = cpu->thread_id;
Luiz Capitulinode0b36b2011-09-21 16:38:35 -03001893#if defined(TARGET_I386)
Eric Blake86f4b682015-11-18 01:52:59 -07001894 info->value->arch = CPU_INFO_ARCH_X86;
Eric Blake544a3732016-02-17 23:48:27 -07001895 info->value->u.x86.pc = env->eip + env->segs[R_CS].base;
Luiz Capitulinode0b36b2011-09-21 16:38:35 -03001896#elif defined(TARGET_PPC)
Eric Blake86f4b682015-11-18 01:52:59 -07001897 info->value->arch = CPU_INFO_ARCH_PPC;
Eric Blake544a3732016-02-17 23:48:27 -07001898 info->value->u.ppc.nip = env->nip;
Luiz Capitulinode0b36b2011-09-21 16:38:35 -03001899#elif defined(TARGET_SPARC)
Eric Blake86f4b682015-11-18 01:52:59 -07001900 info->value->arch = CPU_INFO_ARCH_SPARC;
Eric Blake544a3732016-02-17 23:48:27 -07001901 info->value->u.q_sparc.pc = env->pc;
1902 info->value->u.q_sparc.npc = env->npc;
Luiz Capitulinode0b36b2011-09-21 16:38:35 -03001903#elif defined(TARGET_MIPS)
Eric Blake86f4b682015-11-18 01:52:59 -07001904 info->value->arch = CPU_INFO_ARCH_MIPS;
Eric Blake544a3732016-02-17 23:48:27 -07001905 info->value->u.q_mips.PC = env->active_tc.PC;
Bastian Koppelmann48e06fe2014-09-01 12:59:46 +01001906#elif defined(TARGET_TRICORE)
Eric Blake86f4b682015-11-18 01:52:59 -07001907 info->value->arch = CPU_INFO_ARCH_TRICORE;
Eric Blake544a3732016-02-17 23:48:27 -07001908 info->value->u.tricore.PC = env->PC;
Eric Blake86f4b682015-11-18 01:52:59 -07001909#else
1910 info->value->arch = CPU_INFO_ARCH_OTHER;
Luiz Capitulinode0b36b2011-09-21 16:38:35 -03001911#endif
1912
1913 /* XXX: waiting for the qapi to support GSList */
1914 if (!cur_item) {
1915 head = cur_item = info;
1916 } else {
1917 cur_item->next = info;
1918 cur_item = info;
1919 }
1920 }
1921
1922 return head;
1923}
Luiz Capitulino0cfd6a92011-11-22 16:32:37 -02001924
1925void qmp_memsave(int64_t addr, int64_t size, const char *filename,
1926 bool has_cpu, int64_t cpu_index, Error **errp)
1927{
1928 FILE *f;
1929 uint32_t l;
Andreas Färber55e5c282012-12-17 06:18:02 +01001930 CPUState *cpu;
Luiz Capitulino0cfd6a92011-11-22 16:32:37 -02001931 uint8_t buf[1024];
Borislav Petkov0dc9daf2015-02-08 13:14:38 +01001932 int64_t orig_addr = addr, orig_size = size;
Luiz Capitulino0cfd6a92011-11-22 16:32:37 -02001933
1934 if (!has_cpu) {
1935 cpu_index = 0;
1936 }
1937
Andreas Färber151d1322013-02-15 15:41:49 +01001938 cpu = qemu_get_cpu(cpu_index);
1939 if (cpu == NULL) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001940 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "cpu-index",
1941 "a CPU number");
Luiz Capitulino0cfd6a92011-11-22 16:32:37 -02001942 return;
1943 }
1944
1945 f = fopen(filename, "wb");
1946 if (!f) {
Luiz Capitulino618da852013-06-07 14:35:06 -04001947 error_setg_file_open(errp, errno, filename);
Luiz Capitulino0cfd6a92011-11-22 16:32:37 -02001948 return;
1949 }
1950
1951 while (size != 0) {
1952 l = sizeof(buf);
1953 if (l > size)
1954 l = size;
Aneesh Kumar K.V2f4d0f52013-10-01 21:49:30 +05301955 if (cpu_memory_rw_debug(cpu, addr, buf, l, 0) != 0) {
Borislav Petkov0dc9daf2015-02-08 13:14:38 +01001956 error_setg(errp, "Invalid addr 0x%016" PRIx64 "/size %" PRId64
1957 " specified", orig_addr, orig_size);
Aneesh Kumar K.V2f4d0f52013-10-01 21:49:30 +05301958 goto exit;
1959 }
Luiz Capitulino0cfd6a92011-11-22 16:32:37 -02001960 if (fwrite(buf, 1, l, f) != l) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001961 error_setg(errp, QERR_IO_ERROR);
Luiz Capitulino0cfd6a92011-11-22 16:32:37 -02001962 goto exit;
1963 }
1964 addr += l;
1965 size -= l;
1966 }
1967
1968exit:
1969 fclose(f);
1970}
Luiz Capitulino6d3962b2011-11-22 17:26:46 -02001971
1972void qmp_pmemsave(int64_t addr, int64_t size, const char *filename,
1973 Error **errp)
1974{
1975 FILE *f;
1976 uint32_t l;
1977 uint8_t buf[1024];
1978
1979 f = fopen(filename, "wb");
1980 if (!f) {
Luiz Capitulino618da852013-06-07 14:35:06 -04001981 error_setg_file_open(errp, errno, filename);
Luiz Capitulino6d3962b2011-11-22 17:26:46 -02001982 return;
1983 }
1984
1985 while (size != 0) {
1986 l = sizeof(buf);
1987 if (l > size)
1988 l = size;
Stefan Weileb6282f2014-04-07 20:28:23 +02001989 cpu_physical_memory_read(addr, buf, l);
Luiz Capitulino6d3962b2011-11-22 17:26:46 -02001990 if (fwrite(buf, 1, l, f) != l) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001991 error_setg(errp, QERR_IO_ERROR);
Luiz Capitulino6d3962b2011-11-22 17:26:46 -02001992 goto exit;
1993 }
1994 addr += l;
1995 size -= l;
1996 }
1997
1998exit:
1999 fclose(f);
2000}
Luiz Capitulinoab49ab52011-11-23 12:55:53 -02002001
2002void qmp_inject_nmi(Error **errp)
2003{
Alexey Kardashevskiy9cb805f2014-08-20 22:16:33 +10002004 nmi_monitor_handle(monitor_get_cpu_index(), errp);
Luiz Capitulinoab49ab52011-11-23 12:55:53 -02002005}
Sebastian Tanase27498be2014-07-25 11:56:33 +02002006
2007void dump_drift_info(FILE *f, fprintf_function cpu_fprintf)
2008{
2009 if (!use_icount) {
2010 return;
2011 }
2012
2013 cpu_fprintf(f, "Host - Guest clock %"PRIi64" ms\n",
2014 (cpu_get_clock() - cpu_get_icount())/SCALE_MS);
2015 if (icount_align_option) {
2016 cpu_fprintf(f, "Max guest delay %"PRIi64" ms\n", -max_delay/SCALE_MS);
2017 cpu_fprintf(f, "Max guest advance %"PRIi64" ms\n", max_advance/SCALE_MS);
2018 } else {
2019 cpu_fprintf(f, "Max guest delay NA\n");
2020 cpu_fprintf(f, "Max guest advance NA\n");
2021 }
2022}