blob: 2cb0af9b2249f77998cf205108bff42e2f51c2fd [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"
Sergio Andres Gomez Del Realc97d6d22017-09-13 04:05:09 -050040#include "sysemu/hvf.h"
Luiz Capitulinode0b36b2011-09-21 16:38:35 -030041#include "qmp-commands.h"
Paolo Bonzini63c91552016-03-15 13:18:37 +010042#include "exec/exec-all.h"
Blue Swirl296af7c2010-03-29 19:23:50 +000043
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010044#include "qemu/thread.h"
Paolo Bonzini9c17d612012-12-17 18:20:04 +010045#include "sysemu/cpus.h"
46#include "sysemu/qtest.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010047#include "qemu/main-loop.h"
48#include "qemu/bitmap.h"
Liu Ping Fancb365642013-09-25 14:20:58 +080049#include "qemu/seqlock.h"
KONRAD Frederic8d4e9142017-02-23 18:29:08 +000050#include "tcg.h"
Wenchao Xiaa4e15de2014-06-18 08:43:36 +020051#include "qapi-event.h"
Alexey Kardashevskiy9cb805f2014-08-20 22:16:33 +100052#include "hw/nmi.h"
Pavel Dovgalyuk8b427042015-09-17 19:24:05 +030053#include "sysemu/replay.h"
Igor Mammedovafed5a52017-05-10 13:29:55 +020054#include "hw/boards.h"
Jan Kiszka0ff0fc12011-06-23 10:15:55 +020055
Jan Kiszka6d9cb732011-02-01 22:15:58 +010056#ifdef CONFIG_LINUX
57
58#include <sys/prctl.h>
59
Marcelo Tosattic0532a72010-10-11 15:31:21 -030060#ifndef PR_MCE_KILL
61#define PR_MCE_KILL 33
62#endif
63
Jan Kiszka6d9cb732011-02-01 22:15:58 +010064#ifndef PR_MCE_KILL_SET
65#define PR_MCE_KILL_SET 1
66#endif
67
68#ifndef PR_MCE_KILL_EARLY
69#define PR_MCE_KILL_EARLY 1
70#endif
71
72#endif /* CONFIG_LINUX */
73
Sebastian Tanase27498be2014-07-25 11:56:33 +020074int64_t max_delay;
75int64_t max_advance;
Blue Swirl296af7c2010-03-29 19:23:50 +000076
Jason J. Herne2adcc852015-09-08 13:12:33 -040077/* vcpu throttling controls */
78static QEMUTimer *throttle_timer;
79static unsigned int throttle_percentage;
80
81#define CPU_THROTTLE_PCT_MIN 1
82#define CPU_THROTTLE_PCT_MAX 99
83#define CPU_THROTTLE_TIMESLICE_NS 10000000
84
Tiejun Chen321bc0b2013-08-02 09:43:09 +080085bool cpu_is_stopped(CPUState *cpu)
86{
87 return cpu->stopped || !runstate_is_running();
88}
89
Andreas Färbera98ae1d2013-05-26 23:21:08 +020090static bool cpu_thread_is_idle(CPUState *cpu)
Peter Maydellac873f12012-07-19 16:52:27 +010091{
Andreas Färberc64ca812012-05-03 02:11:45 +020092 if (cpu->stop || cpu->queued_work_first) {
Peter Maydellac873f12012-07-19 16:52:27 +010093 return false;
94 }
Tiejun Chen321bc0b2013-08-02 09:43:09 +080095 if (cpu_is_stopped(cpu)) {
Peter Maydellac873f12012-07-19 16:52:27 +010096 return true;
97 }
Andreas Färber8c2e1b02013-08-25 18:53:55 +020098 if (!cpu->halted || cpu_has_work(cpu) ||
Alexander Graf215e79c2013-04-24 22:24:12 +020099 kvm_halt_in_kernel()) {
Peter Maydellac873f12012-07-19 16:52:27 +0100100 return false;
101 }
102 return true;
103}
104
105static bool all_cpu_threads_idle(void)
106{
Andreas Färber182735e2013-05-29 22:29:20 +0200107 CPUState *cpu;
Peter Maydellac873f12012-07-19 16:52:27 +0100108
Andreas Färberbdc44642013-06-24 23:50:24 +0200109 CPU_FOREACH(cpu) {
Andreas Färber182735e2013-05-29 22:29:20 +0200110 if (!cpu_thread_is_idle(cpu)) {
Peter Maydellac873f12012-07-19 16:52:27 +0100111 return false;
112 }
113 }
114 return true;
115}
116
Blue Swirl296af7c2010-03-29 19:23:50 +0000117/***********************************************************/
Paolo Bonzini946fb272011-09-12 13:57:37 +0200118/* guest cycle counter */
119
Paolo Bonzinia3270e12013-10-07 17:18:15 +0200120/* Protected by TimersState seqlock */
121
Victor CLEMENT5045e9d92015-05-29 17:14:04 +0200122static bool icount_sleep = true;
Paolo Bonzini946fb272011-09-12 13:57:37 +0200123/* Conversion factor from emulated instructions to virtual clock ticks. */
124static int icount_time_shift;
125/* Arbitrarily pick 1MIPS as the minimum allowable speed. */
126#define MAX_ICOUNT_SHIFT 10
Paolo Bonzinia3270e12013-10-07 17:18:15 +0200127
Paolo Bonzini946fb272011-09-12 13:57:37 +0200128typedef struct TimersState {
Liu Ping Fancb365642013-09-25 14:20:58 +0800129 /* Protected by BQL. */
Paolo Bonzini946fb272011-09-12 13:57:37 +0200130 int64_t cpu_ticks_prev;
131 int64_t cpu_ticks_offset;
Liu Ping Fancb365642013-09-25 14:20:58 +0800132
133 /* cpu_clock_offset can be read out of BQL, so protect it with
134 * this lock.
135 */
136 QemuSeqLock vm_clock_seqlock;
Paolo Bonzini946fb272011-09-12 13:57:37 +0200137 int64_t cpu_clock_offset;
138 int32_t cpu_ticks_enabled;
139 int64_t dummy;
KONRAD Fredericc96778b2014-08-01 01:37:09 +0200140
141 /* Compensate for varying guest execution speed. */
142 int64_t qemu_icount_bias;
143 /* Only written by TCG thread */
144 int64_t qemu_icount;
Pavel Dovgalyukb39e3f32018-01-11 11:26:10 +0300145 /* for adjusting icount */
146 int64_t vm_clock_warp_start;
147 QEMUTimer *icount_rt_timer;
148 QEMUTimer *icount_vm_timer;
149 QEMUTimer *icount_warp_timer;
Paolo Bonzini946fb272011-09-12 13:57:37 +0200150} TimersState;
151
Liu Ping Fand9cd4002013-07-21 08:43:00 +0000152static TimersState timers_state;
KONRAD Frederic8d4e9142017-02-23 18:29:08 +0000153bool mttcg_enabled;
154
155/*
156 * We default to false if we know other options have been enabled
157 * which are currently incompatible with MTTCG. Otherwise when each
158 * guest (target) has been updated to support:
159 * - atomic instructions
160 * - memory ordering primitives (barriers)
161 * they can set the appropriate CONFIG flags in ${target}-softmmu.mak
162 *
163 * Once a guest architecture has been converted to the new primitives
164 * there are two remaining limitations to check.
165 *
166 * - The guest can't be oversized (e.g. 64 bit guest on 32 bit host)
167 * - The host must have a stronger memory order than the guest
168 *
169 * It may be possible in future to support strong guests on weak hosts
170 * but that will require tagging all load/stores in a guest with their
171 * implicit memory order requirements which would likely slow things
172 * down a lot.
173 */
174
175static bool check_tcg_memory_orders_compatible(void)
176{
177#if defined(TCG_GUEST_DEFAULT_MO) && defined(TCG_TARGET_DEFAULT_MO)
178 return (TCG_GUEST_DEFAULT_MO & ~TCG_TARGET_DEFAULT_MO) == 0;
179#else
180 return false;
181#endif
182}
183
184static bool default_mttcg_enabled(void)
185{
Alex Bennée83fd9622017-02-27 17:09:01 +0000186 if (use_icount || TCG_OVERSIZED_GUEST) {
KONRAD Frederic8d4e9142017-02-23 18:29:08 +0000187 return false;
188 } else {
189#ifdef TARGET_SUPPORTS_MTTCG
190 return check_tcg_memory_orders_compatible();
191#else
192 return false;
193#endif
194 }
195}
196
197void qemu_tcg_configure(QemuOpts *opts, Error **errp)
198{
199 const char *t = qemu_opt_get(opts, "thread");
200 if (t) {
201 if (strcmp(t, "multi") == 0) {
202 if (TCG_OVERSIZED_GUEST) {
203 error_setg(errp, "No MTTCG when guest word size > hosts");
Alex Bennée83fd9622017-02-27 17:09:01 +0000204 } else if (use_icount) {
205 error_setg(errp, "No MTTCG when icount is enabled");
KONRAD Frederic8d4e9142017-02-23 18:29:08 +0000206 } else {
Nikunj A Dadhania86953502017-04-10 11:36:55 +0530207#ifndef TARGET_SUPPORTS_MTTCG
Alex Bennéec34c7622017-02-28 14:40:17 +0000208 error_report("Guest not yet converted to MTTCG - "
209 "you may get unexpected results");
210#endif
KONRAD Frederic8d4e9142017-02-23 18:29:08 +0000211 if (!check_tcg_memory_orders_compatible()) {
212 error_report("Guest expects a stronger memory ordering "
213 "than the host provides");
Pranith Kumar8cfef892017-03-25 16:19:23 -0400214 error_printf("This may cause strange/hard to debug errors\n");
KONRAD Frederic8d4e9142017-02-23 18:29:08 +0000215 }
216 mttcg_enabled = true;
217 }
218 } else if (strcmp(t, "single") == 0) {
219 mttcg_enabled = false;
220 } else {
221 error_setg(errp, "Invalid 'thread' setting %s", t);
222 }
223 } else {
224 mttcg_enabled = default_mttcg_enabled();
225 }
226}
Paolo Bonzini946fb272011-09-12 13:57:37 +0200227
Alex Bennéee4cd9652017-03-31 16:09:42 +0100228/* The current number of executed instructions is based on what we
229 * originally budgeted minus the current state of the decrementing
230 * icount counters in extra/u16.low.
231 */
232static int64_t cpu_get_icount_executed(CPUState *cpu)
233{
234 return cpu->icount_budget - (cpu->icount_decr.u16.low + cpu->icount_extra);
235}
236
Alex Bennée512d3c82017-04-05 12:32:37 +0100237/*
238 * Update the global shared timer_state.qemu_icount to take into
239 * account executed instructions. This is done by the TCG vCPU
240 * thread so the main-loop can see time has moved forward.
241 */
242void cpu_update_icount(CPUState *cpu)
243{
244 int64_t executed = cpu_get_icount_executed(cpu);
245 cpu->icount_budget -= executed;
246
247#ifdef CONFIG_ATOMIC64
248 atomic_set__nocheck(&timers_state.qemu_icount,
249 atomic_read__nocheck(&timers_state.qemu_icount) +
250 executed);
251#else /* FIXME: we need 64bit atomics to do this safely */
252 timers_state.qemu_icount += executed;
253#endif
254}
255
Pavel Dovgalyuk2a629142014-12-08 10:53:45 +0300256int64_t cpu_get_icount_raw(void)
Paolo Bonzini946fb272011-09-12 13:57:37 +0200257{
Andreas Färber4917cf42013-05-27 05:17:50 +0200258 CPUState *cpu = current_cpu;
Paolo Bonzini946fb272011-09-12 13:57:37 +0200259
Alex Bennée243c5f72017-03-30 18:49:22 +0100260 if (cpu && cpu->running) {
Paolo Bonzini414b15c2015-06-24 14:16:26 +0200261 if (!cpu->can_do_io) {
Pavel Dovgalyuk2a629142014-12-08 10:53:45 +0300262 fprintf(stderr, "Bad icount read\n");
263 exit(1);
Paolo Bonzini946fb272011-09-12 13:57:37 +0200264 }
Alex Bennéee4cd9652017-03-31 16:09:42 +0100265 /* Take into account what has run */
Alex Bennée1d059062017-04-05 10:53:47 +0100266 cpu_update_icount(cpu);
Paolo Bonzini946fb272011-09-12 13:57:37 +0200267 }
Alex Bennée1d059062017-04-05 10:53:47 +0100268#ifdef CONFIG_ATOMIC64
269 return atomic_read__nocheck(&timers_state.qemu_icount);
270#else /* FIXME: we need 64bit atomics to do this safely */
271 return timers_state.qemu_icount;
272#endif
Pavel Dovgalyuk2a629142014-12-08 10:53:45 +0300273}
274
275/* Return the virtual CPU time, based on the instruction counter. */
276static int64_t cpu_get_icount_locked(void)
277{
278 int64_t icount = cpu_get_icount_raw();
KONRAD Frederic3f031312014-08-01 01:37:15 +0200279 return timers_state.qemu_icount_bias + cpu_icount_to_ns(icount);
Paolo Bonzini946fb272011-09-12 13:57:37 +0200280}
281
Paolo Bonzini17a15f12013-10-03 15:17:25 +0200282int64_t cpu_get_icount(void)
283{
284 int64_t icount;
285 unsigned start;
286
287 do {
288 start = seqlock_read_begin(&timers_state.vm_clock_seqlock);
289 icount = cpu_get_icount_locked();
290 } while (seqlock_read_retry(&timers_state.vm_clock_seqlock, start));
291
292 return icount;
293}
294
KONRAD Frederic3f031312014-08-01 01:37:15 +0200295int64_t cpu_icount_to_ns(int64_t icount)
296{
297 return icount << icount_time_shift;
298}
299
Cao jind90f3cc2016-07-29 19:05:38 +0800300/* return the time elapsed in VM between vm_start and vm_stop. Unless
301 * icount is active, cpu_get_ticks() uses units of the host CPU cycle
302 * counter.
303 *
304 * Caller must hold the BQL
305 */
Paolo Bonzini946fb272011-09-12 13:57:37 +0200306int64_t cpu_get_ticks(void)
307{
Paolo Bonzini5f3e3102013-10-28 17:32:18 +0100308 int64_t ticks;
309
Paolo Bonzini946fb272011-09-12 13:57:37 +0200310 if (use_icount) {
311 return cpu_get_icount();
312 }
Paolo Bonzini5f3e3102013-10-28 17:32:18 +0100313
314 ticks = timers_state.cpu_ticks_offset;
315 if (timers_state.cpu_ticks_enabled) {
Christopher Covington4a7428c2015-09-25 10:42:21 -0400316 ticks += cpu_get_host_ticks();
Paolo Bonzini946fb272011-09-12 13:57:37 +0200317 }
Paolo Bonzini5f3e3102013-10-28 17:32:18 +0100318
319 if (timers_state.cpu_ticks_prev > ticks) {
320 /* Note: non increasing ticks may happen if the host uses
321 software suspend */
322 timers_state.cpu_ticks_offset += timers_state.cpu_ticks_prev - ticks;
323 ticks = timers_state.cpu_ticks_prev;
324 }
325
326 timers_state.cpu_ticks_prev = ticks;
327 return ticks;
Paolo Bonzini946fb272011-09-12 13:57:37 +0200328}
329
Liu Ping Fancb365642013-09-25 14:20:58 +0800330static int64_t cpu_get_clock_locked(void)
331{
Cao jin1d45cea2016-07-29 19:05:37 +0800332 int64_t time;
Liu Ping Fancb365642013-09-25 14:20:58 +0800333
Cao jin1d45cea2016-07-29 19:05:37 +0800334 time = timers_state.cpu_clock_offset;
Paolo Bonzini5f3e3102013-10-28 17:32:18 +0100335 if (timers_state.cpu_ticks_enabled) {
Cao jin1d45cea2016-07-29 19:05:37 +0800336 time += get_clock();
Liu Ping Fancb365642013-09-25 14:20:58 +0800337 }
338
Cao jin1d45cea2016-07-29 19:05:37 +0800339 return time;
Liu Ping Fancb365642013-09-25 14:20:58 +0800340}
341
Cao jind90f3cc2016-07-29 19:05:38 +0800342/* Return the monotonic time elapsed in VM, i.e.,
Peter Maydell8212ff82016-09-15 10:24:22 +0100343 * the time between vm_start and vm_stop
344 */
Paolo Bonzini946fb272011-09-12 13:57:37 +0200345int64_t cpu_get_clock(void)
346{
347 int64_t ti;
Liu Ping Fancb365642013-09-25 14:20:58 +0800348 unsigned start;
349
350 do {
351 start = seqlock_read_begin(&timers_state.vm_clock_seqlock);
352 ti = cpu_get_clock_locked();
353 } while (seqlock_read_retry(&timers_state.vm_clock_seqlock, start));
354
355 return ti;
Paolo Bonzini946fb272011-09-12 13:57:37 +0200356}
357
Liu Ping Fancb365642013-09-25 14:20:58 +0800358/* enable cpu_get_ticks()
Cao jin3224e872016-07-08 18:31:37 +0800359 * Caller must hold BQL which serves as mutex for vm_clock_seqlock.
Liu Ping Fancb365642013-09-25 14:20:58 +0800360 */
Paolo Bonzini946fb272011-09-12 13:57:37 +0200361void cpu_enable_ticks(void)
362{
Liu Ping Fancb365642013-09-25 14:20:58 +0800363 /* Here, the really thing protected by seqlock is cpu_clock_offset. */
Emilio G. Cota03719e42016-06-08 14:55:21 -0400364 seqlock_write_begin(&timers_state.vm_clock_seqlock);
Paolo Bonzini946fb272011-09-12 13:57:37 +0200365 if (!timers_state.cpu_ticks_enabled) {
Christopher Covington4a7428c2015-09-25 10:42:21 -0400366 timers_state.cpu_ticks_offset -= cpu_get_host_ticks();
Paolo Bonzini946fb272011-09-12 13:57:37 +0200367 timers_state.cpu_clock_offset -= get_clock();
368 timers_state.cpu_ticks_enabled = 1;
369 }
Emilio G. Cota03719e42016-06-08 14:55:21 -0400370 seqlock_write_end(&timers_state.vm_clock_seqlock);
Paolo Bonzini946fb272011-09-12 13:57:37 +0200371}
372
373/* disable cpu_get_ticks() : the clock is stopped. You must not call
Liu Ping Fancb365642013-09-25 14:20:58 +0800374 * cpu_get_ticks() after that.
Cao jin3224e872016-07-08 18:31:37 +0800375 * Caller must hold BQL which serves as mutex for vm_clock_seqlock.
Liu Ping Fancb365642013-09-25 14:20:58 +0800376 */
Paolo Bonzini946fb272011-09-12 13:57:37 +0200377void cpu_disable_ticks(void)
378{
Liu Ping Fancb365642013-09-25 14:20:58 +0800379 /* Here, the really thing protected by seqlock is cpu_clock_offset. */
Emilio G. Cota03719e42016-06-08 14:55:21 -0400380 seqlock_write_begin(&timers_state.vm_clock_seqlock);
Paolo Bonzini946fb272011-09-12 13:57:37 +0200381 if (timers_state.cpu_ticks_enabled) {
Christopher Covington4a7428c2015-09-25 10:42:21 -0400382 timers_state.cpu_ticks_offset += cpu_get_host_ticks();
Liu Ping Fancb365642013-09-25 14:20:58 +0800383 timers_state.cpu_clock_offset = cpu_get_clock_locked();
Paolo Bonzini946fb272011-09-12 13:57:37 +0200384 timers_state.cpu_ticks_enabled = 0;
385 }
Emilio G. Cota03719e42016-06-08 14:55:21 -0400386 seqlock_write_end(&timers_state.vm_clock_seqlock);
Paolo Bonzini946fb272011-09-12 13:57:37 +0200387}
388
389/* Correlation between real and virtual time is always going to be
390 fairly approximate, so ignore small variation.
391 When the guest is idle real and virtual time will be aligned in
392 the IO wait loop. */
Rutuja Shah73bcb242016-03-21 21:32:30 +0530393#define ICOUNT_WOBBLE (NANOSECONDS_PER_SECOND / 10)
Paolo Bonzini946fb272011-09-12 13:57:37 +0200394
395static void icount_adjust(void)
396{
397 int64_t cur_time;
398 int64_t cur_icount;
399 int64_t delta;
Paolo Bonzinia3270e12013-10-07 17:18:15 +0200400
401 /* Protected by TimersState mutex. */
Paolo Bonzini946fb272011-09-12 13:57:37 +0200402 static int64_t last_delta;
Paolo Bonzini468cc7c2013-10-07 17:21:51 +0200403
Paolo Bonzini946fb272011-09-12 13:57:37 +0200404 /* If the VM is not running, then do nothing. */
405 if (!runstate_is_running()) {
406 return;
407 }
Paolo Bonzini468cc7c2013-10-07 17:21:51 +0200408
Emilio G. Cota03719e42016-06-08 14:55:21 -0400409 seqlock_write_begin(&timers_state.vm_clock_seqlock);
Paolo Bonzini17a15f12013-10-03 15:17:25 +0200410 cur_time = cpu_get_clock_locked();
411 cur_icount = cpu_get_icount_locked();
Paolo Bonzini468cc7c2013-10-07 17:21:51 +0200412
Paolo Bonzini946fb272011-09-12 13:57:37 +0200413 delta = cur_icount - cur_time;
414 /* FIXME: This is a very crude algorithm, somewhat prone to oscillation. */
415 if (delta > 0
416 && last_delta + ICOUNT_WOBBLE < delta * 2
417 && icount_time_shift > 0) {
418 /* The guest is getting too far ahead. Slow time down. */
419 icount_time_shift--;
420 }
421 if (delta < 0
422 && last_delta - ICOUNT_WOBBLE > delta * 2
423 && icount_time_shift < MAX_ICOUNT_SHIFT) {
424 /* The guest is getting too far behind. Speed time up. */
425 icount_time_shift++;
426 }
427 last_delta = delta;
KONRAD Fredericc96778b2014-08-01 01:37:09 +0200428 timers_state.qemu_icount_bias = cur_icount
429 - (timers_state.qemu_icount << icount_time_shift);
Emilio G. Cota03719e42016-06-08 14:55:21 -0400430 seqlock_write_end(&timers_state.vm_clock_seqlock);
Paolo Bonzini946fb272011-09-12 13:57:37 +0200431}
432
433static void icount_adjust_rt(void *opaque)
434{
Pavel Dovgalyukb39e3f32018-01-11 11:26:10 +0300435 timer_mod(timers_state.icount_rt_timer,
Pavel Dovgalyuk1979b902015-01-12 15:00:43 +0300436 qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL_RT) + 1000);
Paolo Bonzini946fb272011-09-12 13:57:37 +0200437 icount_adjust();
438}
439
440static void icount_adjust_vm(void *opaque)
441{
Pavel Dovgalyukb39e3f32018-01-11 11:26:10 +0300442 timer_mod(timers_state.icount_vm_timer,
Alex Bligh40daca52013-08-21 16:03:02 +0100443 qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) +
Rutuja Shah73bcb242016-03-21 21:32:30 +0530444 NANOSECONDS_PER_SECOND / 10);
Paolo Bonzini946fb272011-09-12 13:57:37 +0200445 icount_adjust();
446}
447
448static int64_t qemu_icount_round(int64_t count)
449{
450 return (count + (1 << icount_time_shift) - 1) >> icount_time_shift;
451}
452
Pavel Dovgalyukefab87c2015-09-17 19:24:39 +0300453static void icount_warp_rt(void)
Paolo Bonzini946fb272011-09-12 13:57:37 +0200454{
Alex Bennéeccffff42016-04-04 15:35:48 +0100455 unsigned seq;
456 int64_t warp_start;
457
Paolo Bonzini17a15f12013-10-03 15:17:25 +0200458 /* The icount_warp_timer is rescheduled soon after vm_clock_warp_start
459 * changes from -1 to another value, so the race here is okay.
460 */
Alex Bennéeccffff42016-04-04 15:35:48 +0100461 do {
462 seq = seqlock_read_begin(&timers_state.vm_clock_seqlock);
Pavel Dovgalyukb39e3f32018-01-11 11:26:10 +0300463 warp_start = timers_state.vm_clock_warp_start;
Alex Bennéeccffff42016-04-04 15:35:48 +0100464 } while (seqlock_read_retry(&timers_state.vm_clock_seqlock, seq));
465
466 if (warp_start == -1) {
Paolo Bonzini946fb272011-09-12 13:57:37 +0200467 return;
468 }
469
Emilio G. Cota03719e42016-06-08 14:55:21 -0400470 seqlock_write_begin(&timers_state.vm_clock_seqlock);
Paolo Bonzini946fb272011-09-12 13:57:37 +0200471 if (runstate_is_running()) {
Pavel Dovgalyuk8eda2062015-09-17 19:24:28 +0300472 int64_t clock = REPLAY_CLOCK(REPLAY_CLOCK_VIRTUAL_RT,
473 cpu_get_clock_locked());
Paolo Bonzini8ed961d2013-10-07 17:26:07 +0200474 int64_t warp_delta;
475
Pavel Dovgalyukb39e3f32018-01-11 11:26:10 +0300476 warp_delta = clock - timers_state.vm_clock_warp_start;
Paolo Bonzini8ed961d2013-10-07 17:26:07 +0200477 if (use_icount == 2) {
Paolo Bonzini946fb272011-09-12 13:57:37 +0200478 /*
Alex Bligh40daca52013-08-21 16:03:02 +0100479 * In adaptive mode, do not let QEMU_CLOCK_VIRTUAL run too
Paolo Bonzini946fb272011-09-12 13:57:37 +0200480 * far ahead of real time.
481 */
Paolo Bonzini17a15f12013-10-03 15:17:25 +0200482 int64_t cur_icount = cpu_get_icount_locked();
Pavel Dovgalyukbf2a7dd2014-11-26 13:40:55 +0300483 int64_t delta = clock - cur_icount;
Paolo Bonzini8ed961d2013-10-07 17:26:07 +0200484 warp_delta = MIN(warp_delta, delta);
Paolo Bonzini946fb272011-09-12 13:57:37 +0200485 }
KONRAD Fredericc96778b2014-08-01 01:37:09 +0200486 timers_state.qemu_icount_bias += warp_delta;
Paolo Bonzini946fb272011-09-12 13:57:37 +0200487 }
Pavel Dovgalyukb39e3f32018-01-11 11:26:10 +0300488 timers_state.vm_clock_warp_start = -1;
Emilio G. Cota03719e42016-06-08 14:55:21 -0400489 seqlock_write_end(&timers_state.vm_clock_seqlock);
Paolo Bonzini8ed961d2013-10-07 17:26:07 +0200490
491 if (qemu_clock_expired(QEMU_CLOCK_VIRTUAL)) {
492 qemu_clock_notify(QEMU_CLOCK_VIRTUAL);
493 }
Paolo Bonzini946fb272011-09-12 13:57:37 +0200494}
495
Pavel Dovgalyuke76d1792016-03-10 14:56:09 +0300496static void icount_timer_cb(void *opaque)
Pavel Dovgalyukefab87c2015-09-17 19:24:39 +0300497{
Pavel Dovgalyuke76d1792016-03-10 14:56:09 +0300498 /* No need for a checkpoint because the timer already synchronizes
499 * with CHECKPOINT_CLOCK_VIRTUAL_RT.
500 */
501 icount_warp_rt();
Pavel Dovgalyukefab87c2015-09-17 19:24:39 +0300502}
503
Paolo Bonzini8156be52012-03-28 15:42:04 +0200504void qtest_clock_warp(int64_t dest)
505{
Alex Bligh40daca52013-08-21 16:03:02 +0100506 int64_t clock = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
Fam Zhengefef88b2015-01-19 17:51:43 +0800507 AioContext *aio_context;
Paolo Bonzini8156be52012-03-28 15:42:04 +0200508 assert(qtest_enabled());
Fam Zhengefef88b2015-01-19 17:51:43 +0800509 aio_context = qemu_get_aio_context();
Paolo Bonzini8156be52012-03-28 15:42:04 +0200510 while (clock < dest) {
Alex Bligh40daca52013-08-21 16:03:02 +0100511 int64_t deadline = qemu_clock_deadline_ns_all(QEMU_CLOCK_VIRTUAL);
Sergey Fedorovc9299e22014-06-10 13:10:28 +0400512 int64_t warp = qemu_soonest_timeout(dest - clock, deadline);
Fam Zhengefef88b2015-01-19 17:51:43 +0800513
Emilio G. Cota03719e42016-06-08 14:55:21 -0400514 seqlock_write_begin(&timers_state.vm_clock_seqlock);
KONRAD Fredericc96778b2014-08-01 01:37:09 +0200515 timers_state.qemu_icount_bias += warp;
Emilio G. Cota03719e42016-06-08 14:55:21 -0400516 seqlock_write_end(&timers_state.vm_clock_seqlock);
Paolo Bonzini17a15f12013-10-03 15:17:25 +0200517
Alex Bligh40daca52013-08-21 16:03:02 +0100518 qemu_clock_run_timers(QEMU_CLOCK_VIRTUAL);
Fam Zhengefef88b2015-01-19 17:51:43 +0800519 timerlist_run_timers(aio_context->tlg.tl[QEMU_CLOCK_VIRTUAL]);
Alex Bligh40daca52013-08-21 16:03:02 +0100520 clock = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
Paolo Bonzini8156be52012-03-28 15:42:04 +0200521 }
Alex Bligh40daca52013-08-21 16:03:02 +0100522 qemu_clock_notify(QEMU_CLOCK_VIRTUAL);
Paolo Bonzini8156be52012-03-28 15:42:04 +0200523}
524
Pavel Dovgalyuke76d1792016-03-10 14:56:09 +0300525void qemu_start_warp_timer(void)
Paolo Bonzini946fb272011-09-12 13:57:37 +0200526{
Paolo Bonzinice78d182013-10-07 17:30:02 +0200527 int64_t clock;
Paolo Bonzini946fb272011-09-12 13:57:37 +0200528 int64_t deadline;
529
Pavel Dovgalyuke76d1792016-03-10 14:56:09 +0300530 if (!use_icount) {
Paolo Bonzini946fb272011-09-12 13:57:37 +0200531 return;
532 }
533
Pavel Dovgalyuk8bd7f712015-09-17 19:24:44 +0300534 /* Nothing to do if the VM is stopped: QEMU_CLOCK_VIRTUAL timers
535 * do not fire, so computing the deadline does not make sense.
536 */
537 if (!runstate_is_running()) {
538 return;
539 }
540
541 /* warp clock deterministically in record/replay mode */
Pavel Dovgalyuke76d1792016-03-10 14:56:09 +0300542 if (!replay_checkpoint(CHECKPOINT_CLOCK_WARP_START)) {
Pavel Dovgalyuk8bd7f712015-09-17 19:24:44 +0300543 return;
544 }
545
Paolo Bonzinice78d182013-10-07 17:30:02 +0200546 if (!all_cpu_threads_idle()) {
Paolo Bonzini946fb272011-09-12 13:57:37 +0200547 return;
548 }
549
Paolo Bonzini8156be52012-03-28 15:42:04 +0200550 if (qtest_enabled()) {
551 /* When testing, qtest commands advance icount. */
Pavel Dovgalyuke76d1792016-03-10 14:56:09 +0300552 return;
Paolo Bonzini8156be52012-03-28 15:42:04 +0200553 }
554
Alex Blighac70aaf2013-08-21 16:02:57 +0100555 /* We want to use the earliest deadline from ALL vm_clocks */
Pavel Dovgalyukbf2a7dd2014-11-26 13:40:55 +0300556 clock = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL_RT);
Alex Bligh40daca52013-08-21 16:03:02 +0100557 deadline = qemu_clock_deadline_ns_all(QEMU_CLOCK_VIRTUAL);
Paolo Bonzinice78d182013-10-07 17:30:02 +0200558 if (deadline < 0) {
Victor CLEMENTd7a0f712015-05-29 17:14:06 +0200559 static bool notified;
560 if (!icount_sleep && !notified) {
Alistair Francis3dc6f862017-07-12 06:57:41 -0700561 warn_report("icount sleep disabled and no active timers");
Victor CLEMENTd7a0f712015-05-29 17:14:06 +0200562 notified = true;
563 }
Paolo Bonzinice78d182013-10-07 17:30:02 +0200564 return;
Alex Blighac70aaf2013-08-21 16:02:57 +0100565 }
566
Paolo Bonzini946fb272011-09-12 13:57:37 +0200567 if (deadline > 0) {
568 /*
Alex Bligh40daca52013-08-21 16:03:02 +0100569 * Ensure QEMU_CLOCK_VIRTUAL proceeds even when the virtual CPU goes to
Paolo Bonzini946fb272011-09-12 13:57:37 +0200570 * sleep. Otherwise, the CPU might be waiting for a future timer
571 * interrupt to wake it up, but the interrupt never comes because
572 * the vCPU isn't running any insns and thus doesn't advance the
Alex Bligh40daca52013-08-21 16:03:02 +0100573 * QEMU_CLOCK_VIRTUAL.
Paolo Bonzini946fb272011-09-12 13:57:37 +0200574 */
Victor CLEMENT5045e9d92015-05-29 17:14:04 +0200575 if (!icount_sleep) {
576 /*
577 * We never let VCPUs sleep in no sleep icount mode.
578 * If there is a pending QEMU_CLOCK_VIRTUAL timer we just advance
579 * to the next QEMU_CLOCK_VIRTUAL event and notify it.
580 * It is useful when we want a deterministic execution time,
581 * isolated from host latencies.
582 */
Emilio G. Cota03719e42016-06-08 14:55:21 -0400583 seqlock_write_begin(&timers_state.vm_clock_seqlock);
Victor CLEMENT5045e9d92015-05-29 17:14:04 +0200584 timers_state.qemu_icount_bias += deadline;
Emilio G. Cota03719e42016-06-08 14:55:21 -0400585 seqlock_write_end(&timers_state.vm_clock_seqlock);
Victor CLEMENT5045e9d92015-05-29 17:14:04 +0200586 qemu_clock_notify(QEMU_CLOCK_VIRTUAL);
587 } else {
588 /*
589 * We do stop VCPUs and only advance QEMU_CLOCK_VIRTUAL after some
590 * "real" time, (related to the time left until the next event) has
591 * passed. The QEMU_CLOCK_VIRTUAL_RT clock will do this.
592 * This avoids that the warps are visible externally; for example,
593 * you will not be sending network packets continuously instead of
594 * every 100ms.
595 */
Emilio G. Cota03719e42016-06-08 14:55:21 -0400596 seqlock_write_begin(&timers_state.vm_clock_seqlock);
Pavel Dovgalyukb39e3f32018-01-11 11:26:10 +0300597 if (timers_state.vm_clock_warp_start == -1
598 || timers_state.vm_clock_warp_start > clock) {
599 timers_state.vm_clock_warp_start = clock;
Victor CLEMENT5045e9d92015-05-29 17:14:04 +0200600 }
Emilio G. Cota03719e42016-06-08 14:55:21 -0400601 seqlock_write_end(&timers_state.vm_clock_seqlock);
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),
702 VMSTATE_INT64(dummy, TimersState),
703 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;
716 long sleeptime_ns;
717
718 if (!cpu_throttle_get_percentage()) {
719 return;
720 }
721
722 pct = (double)cpu_throttle_get_percentage()/100;
723 throttle_ratio = pct / (1 - pct);
724 sleeptime_ns = (long)(throttle_ratio * CPU_THROTTLE_TIMESLICE_NS);
725
726 qemu_mutex_unlock_iothread();
Jason J. Herne2adcc852015-09-08 13:12:33 -0400727 g_usleep(sleeptime_ns / 1000); /* Convert ns to us for usleep call */
728 qemu_mutex_lock_iothread();
Felipe Franciosi90bb0c02017-05-19 22:29:50 +0100729 atomic_set(&cpu->throttle_thread_scheduled, 0);
Jason J. Herne2adcc852015-09-08 13:12:33 -0400730}
731
732static void cpu_throttle_timer_tick(void *opaque)
733{
734 CPUState *cpu;
735 double pct;
736
737 /* Stop the timer if needed */
738 if (!cpu_throttle_get_percentage()) {
739 return;
740 }
741 CPU_FOREACH(cpu) {
742 if (!atomic_xchg(&cpu->throttle_thread_scheduled, 1)) {
Paolo Bonzini14e6fe12016-10-31 10:36:08 +0100743 async_run_on_cpu(cpu, cpu_throttle_thread,
744 RUN_ON_CPU_NULL);
Jason J. Herne2adcc852015-09-08 13:12:33 -0400745 }
746 }
747
748 pct = (double)cpu_throttle_get_percentage()/100;
749 timer_mod(throttle_timer, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL_RT) +
750 CPU_THROTTLE_TIMESLICE_NS / (1-pct));
751}
752
753void cpu_throttle_set(int new_throttle_pct)
754{
755 /* Ensure throttle percentage is within valid range */
756 new_throttle_pct = MIN(new_throttle_pct, CPU_THROTTLE_PCT_MAX);
757 new_throttle_pct = MAX(new_throttle_pct, CPU_THROTTLE_PCT_MIN);
758
759 atomic_set(&throttle_percentage, new_throttle_pct);
760
761 timer_mod(throttle_timer, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL_RT) +
762 CPU_THROTTLE_TIMESLICE_NS);
763}
764
765void cpu_throttle_stop(void)
766{
767 atomic_set(&throttle_percentage, 0);
768}
769
770bool cpu_throttle_active(void)
771{
772 return (cpu_throttle_get_percentage() != 0);
773}
774
775int cpu_throttle_get_percentage(void)
776{
777 return atomic_read(&throttle_percentage);
778}
779
Pavel Dovgalyuk4603ea02014-09-01 09:34:49 +0400780void cpu_ticks_init(void)
781{
Emilio G. Cotaccdb3c12016-06-08 14:55:20 -0400782 seqlock_init(&timers_state.vm_clock_seqlock);
Pavel Dovgalyuk4603ea02014-09-01 09:34:49 +0400783 vmstate_register(NULL, 0, &vmstate_timers, &timers_state);
Jason J. Herne2adcc852015-09-08 13:12:33 -0400784 throttle_timer = timer_new_ns(QEMU_CLOCK_VIRTUAL_RT,
785 cpu_throttle_timer_tick, NULL);
Pavel Dovgalyuk4603ea02014-09-01 09:34:49 +0400786}
787
Sebastian Tanase1ad95802014-07-25 11:56:28 +0200788void configure_icount(QemuOpts *opts, Error **errp)
Paolo Bonzini946fb272011-09-12 13:57:37 +0200789{
Sebastian Tanase1ad95802014-07-25 11:56:28 +0200790 const char *option;
Sebastian Tanasea8bfac32014-07-25 11:56:29 +0200791 char *rem_str = NULL;
Sebastian Tanase1ad95802014-07-25 11:56:28 +0200792
Sebastian Tanase1ad95802014-07-25 11:56:28 +0200793 option = qemu_opt_get(opts, "shift");
Paolo Bonzini946fb272011-09-12 13:57:37 +0200794 if (!option) {
Sebastian Tanasea8bfac32014-07-25 11:56:29 +0200795 if (qemu_opt_get(opts, "align") != NULL) {
796 error_setg(errp, "Please specify shift option when using align");
797 }
Paolo Bonzini946fb272011-09-12 13:57:37 +0200798 return;
799 }
Victor CLEMENTf1f4b572015-05-29 17:14:05 +0200800
801 icount_sleep = qemu_opt_get_bool(opts, "sleep", true);
Victor CLEMENT5045e9d92015-05-29 17:14:04 +0200802 if (icount_sleep) {
Pavel Dovgalyukb39e3f32018-01-11 11:26:10 +0300803 timers_state.icount_warp_timer = timer_new_ns(QEMU_CLOCK_VIRTUAL_RT,
Pavel Dovgalyuke76d1792016-03-10 14:56:09 +0300804 icount_timer_cb, NULL);
Victor CLEMENT5045e9d92015-05-29 17:14:04 +0200805 }
Victor CLEMENTf1f4b572015-05-29 17:14:05 +0200806
Sebastian Tanasea8bfac32014-07-25 11:56:29 +0200807 icount_align_option = qemu_opt_get_bool(opts, "align", false);
Victor CLEMENTf1f4b572015-05-29 17:14:05 +0200808
809 if (icount_align_option && !icount_sleep) {
Pranith Kumar778d9f92016-02-26 10:16:51 -0500810 error_setg(errp, "align=on and sleep=off are incompatible");
Victor CLEMENTf1f4b572015-05-29 17:14:05 +0200811 }
Paolo Bonzini946fb272011-09-12 13:57:37 +0200812 if (strcmp(option, "auto") != 0) {
Sebastian Tanasea8bfac32014-07-25 11:56:29 +0200813 errno = 0;
814 icount_time_shift = strtol(option, &rem_str, 0);
815 if (errno != 0 || *rem_str != '\0' || !strlen(option)) {
816 error_setg(errp, "icount: Invalid shift value");
817 }
Paolo Bonzini946fb272011-09-12 13:57:37 +0200818 use_icount = 1;
819 return;
Sebastian Tanasea8bfac32014-07-25 11:56:29 +0200820 } else if (icount_align_option) {
821 error_setg(errp, "shift=auto and align=on are incompatible");
Victor CLEMENTf1f4b572015-05-29 17:14:05 +0200822 } else if (!icount_sleep) {
Pranith Kumar778d9f92016-02-26 10:16:51 -0500823 error_setg(errp, "shift=auto and sleep=off are incompatible");
Paolo Bonzini946fb272011-09-12 13:57:37 +0200824 }
825
826 use_icount = 2;
827
828 /* 125MIPS seems a reasonable initial guess at the guest speed.
829 It will be corrected fairly quickly anyway. */
830 icount_time_shift = 3;
831
832 /* Have both realtime and virtual time triggers for speed adjustment.
833 The realtime trigger catches emulated time passing too slowly,
834 the virtual time trigger catches emulated time passing too fast.
835 Realtime triggers occur even when idle, so use them less frequently
836 than VM triggers. */
Pavel Dovgalyukb39e3f32018-01-11 11:26:10 +0300837 timers_state.vm_clock_warp_start = -1;
838 timers_state.icount_rt_timer = timer_new_ms(QEMU_CLOCK_VIRTUAL_RT,
Pavel Dovgalyukbf2a7dd2014-11-26 13:40:55 +0300839 icount_adjust_rt, NULL);
Pavel Dovgalyukb39e3f32018-01-11 11:26:10 +0300840 timer_mod(timers_state.icount_rt_timer,
Pavel Dovgalyukbf2a7dd2014-11-26 13:40:55 +0300841 qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL_RT) + 1000);
Pavel Dovgalyukb39e3f32018-01-11 11:26:10 +0300842 timers_state.icount_vm_timer = timer_new_ns(QEMU_CLOCK_VIRTUAL,
Alex Bligh40daca52013-08-21 16:03:02 +0100843 icount_adjust_vm, NULL);
Pavel Dovgalyukb39e3f32018-01-11 11:26:10 +0300844 timer_mod(timers_state.icount_vm_timer,
Alex Bligh40daca52013-08-21 16:03:02 +0100845 qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) +
Rutuja Shah73bcb242016-03-21 21:32:30 +0530846 NANOSECONDS_PER_SECOND / 10);
Paolo Bonzini946fb272011-09-12 13:57:37 +0200847}
848
849/***********************************************************/
Alex Bennée65467062017-02-23 18:29:09 +0000850/* TCG vCPU kick timer
851 *
852 * The kick timer is responsible for moving single threaded vCPU
853 * emulation on to the next vCPU. If more than one vCPU is running a
854 * timer event with force a cpu->exit so the next vCPU can get
855 * scheduled.
856 *
857 * The timer is removed if all vCPUs are idle and restarted again once
858 * idleness is complete.
859 */
860
861static QEMUTimer *tcg_kick_vcpu_timer;
Alex Bennée791158d2017-02-23 18:29:10 +0000862static CPUState *tcg_current_rr_cpu;
Alex Bennée65467062017-02-23 18:29:09 +0000863
864#define TCG_KICK_PERIOD (NANOSECONDS_PER_SECOND / 10)
865
866static inline int64_t qemu_tcg_next_kick(void)
867{
868 return qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) + TCG_KICK_PERIOD;
869}
870
Alex Bennée791158d2017-02-23 18:29:10 +0000871/* Kick the currently round-robin scheduled vCPU */
872static void qemu_cpu_kick_rr_cpu(void)
873{
874 CPUState *cpu;
Alex Bennée791158d2017-02-23 18:29:10 +0000875 do {
876 cpu = atomic_mb_read(&tcg_current_rr_cpu);
877 if (cpu) {
878 cpu_exit(cpu);
879 }
880 } while (cpu != atomic_mb_read(&tcg_current_rr_cpu));
881}
882
Paolo Bonzini6b8f0182017-03-02 19:56:40 +0100883static void do_nothing(CPUState *cpu, run_on_cpu_data unused)
884{
885}
886
Paolo Bonzini3f53bc62017-03-03 11:50:29 +0100887void qemu_timer_notify_cb(void *opaque, QEMUClockType type)
888{
Paolo Bonzini6b8f0182017-03-02 19:56:40 +0100889 if (!use_icount || type != QEMU_CLOCK_VIRTUAL) {
890 qemu_notify_event();
891 return;
892 }
893
894 if (!qemu_in_vcpu_thread() && first_cpu) {
895 /* qemu_cpu_kick is not enough to kick a halted CPU out of
896 * qemu_tcg_wait_io_event. async_run_on_cpu, instead,
897 * causes cpu_thread_is_idle to return false. This way,
898 * handle_icount_deadline can run.
899 */
900 async_run_on_cpu(first_cpu, do_nothing, RUN_ON_CPU_NULL);
901 }
Paolo Bonzini3f53bc62017-03-03 11:50:29 +0100902}
903
Alex Bennée65467062017-02-23 18:29:09 +0000904static void kick_tcg_thread(void *opaque)
905{
906 timer_mod(tcg_kick_vcpu_timer, qemu_tcg_next_kick());
Alex Bennée791158d2017-02-23 18:29:10 +0000907 qemu_cpu_kick_rr_cpu();
Alex Bennée65467062017-02-23 18:29:09 +0000908}
909
910static void start_tcg_kick_timer(void)
911{
Paolo Bonzinidb08b682018-01-11 13:53:12 +0100912 assert(!mttcg_enabled);
913 if (!tcg_kick_vcpu_timer && CPU_NEXT(first_cpu)) {
Alex Bennée65467062017-02-23 18:29:09 +0000914 tcg_kick_vcpu_timer = timer_new_ns(QEMU_CLOCK_VIRTUAL,
915 kick_tcg_thread, NULL);
916 timer_mod(tcg_kick_vcpu_timer, qemu_tcg_next_kick());
917 }
918}
919
920static void stop_tcg_kick_timer(void)
921{
Paolo Bonzinidb08b682018-01-11 13:53:12 +0100922 assert(!mttcg_enabled);
Alex Bennée65467062017-02-23 18:29:09 +0000923 if (tcg_kick_vcpu_timer) {
924 timer_del(tcg_kick_vcpu_timer);
925 tcg_kick_vcpu_timer = NULL;
926 }
927}
928
Alex Bennée65467062017-02-23 18:29:09 +0000929/***********************************************************/
Blue Swirl296af7c2010-03-29 19:23:50 +0000930void hw_error(const char *fmt, ...)
931{
932 va_list ap;
Andreas Färber55e5c282012-12-17 06:18:02 +0100933 CPUState *cpu;
Blue Swirl296af7c2010-03-29 19:23:50 +0000934
935 va_start(ap, fmt);
936 fprintf(stderr, "qemu: hardware error: ");
937 vfprintf(stderr, fmt, ap);
938 fprintf(stderr, "\n");
Andreas Färberbdc44642013-06-24 23:50:24 +0200939 CPU_FOREACH(cpu) {
Andreas Färber55e5c282012-12-17 06:18:02 +0100940 fprintf(stderr, "CPU #%d:\n", cpu->cpu_index);
Andreas Färber878096e2013-05-27 01:33:50 +0200941 cpu_dump_state(cpu, stderr, fprintf, CPU_DUMP_FPU);
Blue Swirl296af7c2010-03-29 19:23:50 +0000942 }
943 va_end(ap);
944 abort();
945}
946
947void cpu_synchronize_all_states(void)
948{
Andreas Färber182735e2013-05-29 22:29:20 +0200949 CPUState *cpu;
Blue Swirl296af7c2010-03-29 19:23:50 +0000950
Andreas Färberbdc44642013-06-24 23:50:24 +0200951 CPU_FOREACH(cpu) {
Andreas Färber182735e2013-05-29 22:29:20 +0200952 cpu_synchronize_state(cpu);
Sergio Andres Gomez Del Realc97d6d22017-09-13 04:05:09 -0500953 /* TODO: move to cpu_synchronize_state() */
954 if (hvf_enabled()) {
955 hvf_cpu_synchronize_state(cpu);
956 }
Blue Swirl296af7c2010-03-29 19:23:50 +0000957 }
958}
959
960void cpu_synchronize_all_post_reset(void)
961{
Andreas Färber182735e2013-05-29 22:29:20 +0200962 CPUState *cpu;
Blue Swirl296af7c2010-03-29 19:23:50 +0000963
Andreas Färberbdc44642013-06-24 23:50:24 +0200964 CPU_FOREACH(cpu) {
Andreas Färber182735e2013-05-29 22:29:20 +0200965 cpu_synchronize_post_reset(cpu);
Sergio Andres Gomez Del Realc97d6d22017-09-13 04:05:09 -0500966 /* TODO: move to cpu_synchronize_post_reset() */
967 if (hvf_enabled()) {
968 hvf_cpu_synchronize_post_reset(cpu);
969 }
Blue Swirl296af7c2010-03-29 19:23:50 +0000970 }
971}
972
973void cpu_synchronize_all_post_init(void)
974{
Andreas Färber182735e2013-05-29 22:29:20 +0200975 CPUState *cpu;
Blue Swirl296af7c2010-03-29 19:23:50 +0000976
Andreas Färberbdc44642013-06-24 23:50:24 +0200977 CPU_FOREACH(cpu) {
Andreas Färber182735e2013-05-29 22:29:20 +0200978 cpu_synchronize_post_init(cpu);
Sergio Andres Gomez Del Realc97d6d22017-09-13 04:05:09 -0500979 /* TODO: move to cpu_synchronize_post_init() */
980 if (hvf_enabled()) {
981 hvf_cpu_synchronize_post_init(cpu);
982 }
Blue Swirl296af7c2010-03-29 19:23:50 +0000983 }
984}
985
David Gibson75e972d2017-05-26 14:46:28 +1000986void cpu_synchronize_all_pre_loadvm(void)
987{
988 CPUState *cpu;
989
990 CPU_FOREACH(cpu) {
991 cpu_synchronize_pre_loadvm(cpu);
992 }
993}
994
Kevin Wolf56983462013-07-05 13:49:54 +0200995static int do_vm_stop(RunState state)
Blue Swirl296af7c2010-03-29 19:23:50 +0000996{
Kevin Wolf56983462013-07-05 13:49:54 +0200997 int ret = 0;
998
Luiz Capitulino13548692011-07-29 15:36:43 -0300999 if (runstate_is_running()) {
Blue Swirl296af7c2010-03-29 19:23:50 +00001000 cpu_disable_ticks();
Blue Swirl296af7c2010-03-29 19:23:50 +00001001 pause_all_vcpus();
Luiz Capitulinof5bbfba2011-07-29 15:04:45 -03001002 runstate_set(state);
Luiz Capitulino1dfb4dd2011-07-29 14:26:33 -03001003 vm_state_notify(0, state);
Wenchao Xiaa4e15de2014-06-18 08:43:36 +02001004 qapi_event_send_stop(&error_abort);
Blue Swirl296af7c2010-03-29 19:23:50 +00001005 }
Kevin Wolf56983462013-07-05 13:49:54 +02001006
Kevin Wolf594a45c2013-07-18 14:52:19 +02001007 bdrv_drain_all();
Pavel Dovgalyuk6d0ceb82016-09-26 11:08:16 +03001008 replay_disable_events();
John Snow22af08e2016-09-22 21:45:51 -04001009 ret = bdrv_flush_all();
Kevin Wolf594a45c2013-07-18 14:52:19 +02001010
Kevin Wolf56983462013-07-05 13:49:54 +02001011 return ret;
Blue Swirl296af7c2010-03-29 19:23:50 +00001012}
1013
Andreas Färbera1fcaa72012-05-02 23:42:26 +02001014static bool cpu_can_run(CPUState *cpu)
Blue Swirl296af7c2010-03-29 19:23:50 +00001015{
Andreas Färber4fdeee72012-05-02 23:10:09 +02001016 if (cpu->stop) {
Andreas Färbera1fcaa72012-05-02 23:42:26 +02001017 return false;
Jan Kiszka0ab07c62011-02-07 12:19:14 +01001018 }
Tiejun Chen321bc0b2013-08-02 09:43:09 +08001019 if (cpu_is_stopped(cpu)) {
Andreas Färbera1fcaa72012-05-02 23:42:26 +02001020 return false;
Jan Kiszka0ab07c62011-02-07 12:19:14 +01001021 }
Andreas Färbera1fcaa72012-05-02 23:42:26 +02001022 return true;
Blue Swirl296af7c2010-03-29 19:23:50 +00001023}
1024
Andreas Färber91325042013-05-27 02:07:49 +02001025static void cpu_handle_guest_debug(CPUState *cpu)
Jan Kiszka3c638d02010-06-25 16:56:56 +02001026{
Andreas Färber64f6b342013-05-27 02:06:09 +02001027 gdb_set_stop_cpu(cpu);
Jan Kiszka8cf71712011-02-07 12:19:16 +01001028 qemu_system_debug_request();
Andreas Färberf324e762012-05-02 23:26:21 +02001029 cpu->stopped = true;
Jan Kiszka3c638d02010-06-25 16:56:56 +02001030}
1031
Jan Kiszka6d9cb732011-02-01 22:15:58 +01001032#ifdef CONFIG_LINUX
1033static void sigbus_reraise(void)
1034{
1035 sigset_t set;
1036 struct sigaction action;
1037
1038 memset(&action, 0, sizeof(action));
1039 action.sa_handler = SIG_DFL;
1040 if (!sigaction(SIGBUS, &action, NULL)) {
1041 raise(SIGBUS);
1042 sigemptyset(&set);
1043 sigaddset(&set, SIGBUS);
Peter Maydella2d17612016-05-16 18:33:59 +01001044 pthread_sigmask(SIG_UNBLOCK, &set, NULL);
Jan Kiszka6d9cb732011-02-01 22:15:58 +01001045 }
1046 perror("Failed to re-raise SIGBUS!\n");
1047 abort();
1048}
1049
Paolo Bonzinid98d4072017-02-08 13:22:12 +01001050static void sigbus_handler(int n, siginfo_t *siginfo, void *ctx)
Jan Kiszka6d9cb732011-02-01 22:15:58 +01001051{
Paolo Bonzinia16fc072017-02-09 09:50:02 +01001052 if (siginfo->si_code != BUS_MCEERR_AO && siginfo->si_code != BUS_MCEERR_AR) {
1053 sigbus_reraise();
1054 }
1055
Paolo Bonzini2ae41db2017-02-08 12:48:54 +01001056 if (current_cpu) {
1057 /* Called asynchronously in VCPU thread. */
1058 if (kvm_on_sigbus_vcpu(current_cpu, siginfo->si_code, siginfo->si_addr)) {
1059 sigbus_reraise();
1060 }
1061 } else {
1062 /* Called synchronously (via signalfd) in main thread. */
1063 if (kvm_on_sigbus(siginfo->si_code, siginfo->si_addr)) {
1064 sigbus_reraise();
1065 }
Jan Kiszka6d9cb732011-02-01 22:15:58 +01001066 }
1067}
1068
1069static void qemu_init_sigbus(void)
1070{
1071 struct sigaction action;
1072
1073 memset(&action, 0, sizeof(action));
1074 action.sa_flags = SA_SIGINFO;
Paolo Bonzinid98d4072017-02-08 13:22:12 +01001075 action.sa_sigaction = sigbus_handler;
Jan Kiszka6d9cb732011-02-01 22:15:58 +01001076 sigaction(SIGBUS, &action, NULL);
1077
1078 prctl(PR_MCE_KILL, PR_MCE_KILL_SET, PR_MCE_KILL_EARLY, 0, 0);
1079}
Paolo Bonzinia16fc072017-02-09 09:50:02 +01001080#else /* !CONFIG_LINUX */
1081static void qemu_init_sigbus(void)
1082{
1083}
Paolo Bonzinia16fc072017-02-09 09:50:02 +01001084#endif /* !CONFIG_LINUX */
Blue Swirl296af7c2010-03-29 19:23:50 +00001085
Stefan Weilb2532d82012-09-27 07:41:42 +02001086static QemuMutex qemu_global_mutex;
Blue Swirl296af7c2010-03-29 19:23:50 +00001087
1088static QemuThread io_thread;
1089
Blue Swirl296af7c2010-03-29 19:23:50 +00001090/* cpu creation */
1091static QemuCond qemu_cpu_cond;
1092/* system init */
Blue Swirl296af7c2010-03-29 19:23:50 +00001093static QemuCond qemu_pause_cond;
1094
Paolo Bonzinid3b12f52011-09-13 10:30:52 +02001095void qemu_init_cpu_loop(void)
Blue Swirl296af7c2010-03-29 19:23:50 +00001096{
Jan Kiszka6d9cb732011-02-01 22:15:58 +01001097 qemu_init_sigbus();
Anthony Liguoried945922011-02-08 18:18:18 +01001098 qemu_cond_init(&qemu_cpu_cond);
Anthony Liguoried945922011-02-08 18:18:18 +01001099 qemu_cond_init(&qemu_pause_cond);
Blue Swirl296af7c2010-03-29 19:23:50 +00001100 qemu_mutex_init(&qemu_global_mutex);
Blue Swirl296af7c2010-03-29 19:23:50 +00001101
Jan Kiszkab7680cb2011-03-12 17:43:51 +01001102 qemu_thread_get_self(&io_thread);
Blue Swirl296af7c2010-03-29 19:23:50 +00001103}
1104
Paolo Bonzini14e6fe12016-10-31 10:36:08 +01001105void run_on_cpu(CPUState *cpu, run_on_cpu_func func, run_on_cpu_data data)
Marcelo Tosattie82bcec2010-05-04 09:45:22 -03001106{
Sergey Fedorovd148d902016-08-29 09:51:00 +02001107 do_run_on_cpu(cpu, func, data, &qemu_global_mutex);
Chegu Vinod3c022702013-06-24 03:49:41 -06001108}
1109
Gu Zheng4c055ab2016-05-12 09:18:13 +05301110static void qemu_kvm_destroy_vcpu(CPUState *cpu)
1111{
1112 if (kvm_destroy_vcpu(cpu) < 0) {
1113 error_report("kvm_destroy_vcpu failed");
1114 exit(EXIT_FAILURE);
1115 }
1116}
1117
1118static void qemu_tcg_destroy_vcpu(CPUState *cpu)
1119{
1120}
1121
David Hildenbrandebd05fe2017-11-29 20:12:15 +01001122static void qemu_cpu_stop(CPUState *cpu, bool exit)
1123{
1124 g_assert(qemu_cpu_is_self(cpu));
1125 cpu->stop = false;
1126 cpu->stopped = true;
1127 if (exit) {
1128 cpu_exit(cpu);
1129 }
1130 qemu_cond_broadcast(&qemu_pause_cond);
1131}
1132
Andreas Färber509a0d72012-05-03 02:18:09 +02001133static void qemu_wait_io_event_common(CPUState *cpu)
Blue Swirl296af7c2010-03-29 19:23:50 +00001134{
Alex Bennée37257942017-02-23 18:29:14 +00001135 atomic_mb_set(&cpu->thread_kicked, false);
Andreas Färber4fdeee72012-05-02 23:10:09 +02001136 if (cpu->stop) {
David Hildenbrandebd05fe2017-11-29 20:12:15 +01001137 qemu_cpu_stop(cpu, false);
Blue Swirl296af7c2010-03-29 19:23:50 +00001138 }
Sergey Fedorova5403c62016-08-02 18:27:36 +01001139 process_queued_cpu_work(cpu);
Alex Bennée37257942017-02-23 18:29:14 +00001140}
1141
Paolo Bonzinidb08b682018-01-11 13:53:12 +01001142static void qemu_tcg_rr_wait_io_event(CPUState *cpu)
Alex Bennée37257942017-02-23 18:29:14 +00001143{
Paolo Bonzinidb08b682018-01-11 13:53:12 +01001144 while (all_cpu_threads_idle()) {
Alex Bennée65467062017-02-23 18:29:09 +00001145 stop_tcg_kick_timer();
KONRAD Fredericd5f8d612015-08-10 17:27:06 +02001146 qemu_cond_wait(cpu->halt_cond, &qemu_global_mutex);
Jan Kiszka16400322011-02-09 16:29:37 +01001147 }
Blue Swirl296af7c2010-03-29 19:23:50 +00001148
Alex Bennée65467062017-02-23 18:29:09 +00001149 start_tcg_kick_timer();
1150
Alex Bennée37257942017-02-23 18:29:14 +00001151 qemu_wait_io_event_common(cpu);
Blue Swirl296af7c2010-03-29 19:23:50 +00001152}
1153
Paolo Bonzinidb08b682018-01-11 13:53:12 +01001154static void qemu_wait_io_event(CPUState *cpu)
Blue Swirl296af7c2010-03-29 19:23:50 +00001155{
Andreas Färbera98ae1d2013-05-26 23:21:08 +02001156 while (cpu_thread_is_idle(cpu)) {
Andreas Färberf5c121b2012-05-03 01:22:49 +02001157 qemu_cond_wait(cpu->halt_cond, &qemu_global_mutex);
Jan Kiszka16400322011-02-09 16:29:37 +01001158 }
Blue Swirl296af7c2010-03-29 19:23:50 +00001159
Paolo Bonzinidb08b682018-01-11 13:53:12 +01001160#ifdef _WIN32
1161 /* Eat dummy APC queued by qemu_cpu_kick_thread. */
1162 if (!tcg_enabled()) {
1163 SleepEx(0, TRUE);
Sergio Andres Gomez Del Realc97d6d22017-09-13 04:05:09 -05001164 }
Paolo Bonzinidb08b682018-01-11 13:53:12 +01001165#endif
Sergio Andres Gomez Del Realc97d6d22017-09-13 04:05:09 -05001166 qemu_wait_io_event_common(cpu);
1167}
1168
Jan Kiszka7e97cd82011-02-07 12:19:12 +01001169static void *qemu_kvm_cpu_thread_fn(void *arg)
Blue Swirl296af7c2010-03-29 19:23:50 +00001170{
Andreas Färber48a106b2013-05-27 02:20:39 +02001171 CPUState *cpu = arg;
Jan Kiszka84b49152011-02-01 22:15:50 +01001172 int r;
Blue Swirl296af7c2010-03-29 19:23:50 +00001173
Paolo Bonziniab28bd22015-07-09 08:55:38 +02001174 rcu_register_thread();
1175
Paolo Bonzini2e7f7a32015-06-18 18:47:18 +02001176 qemu_mutex_lock_iothread();
Andreas Färber814e6122012-05-02 17:00:37 +02001177 qemu_thread_get_self(cpu->thread);
Andreas Färber9f09e182012-05-03 06:59:07 +02001178 cpu->thread_id = qemu_get_thread_id();
Pavel Dovgalyuk626cf8f2014-12-08 10:53:17 +03001179 cpu->can_do_io = 1;
Andreas Färber4917cf42013-05-27 05:17:50 +02001180 current_cpu = cpu;
Blue Swirl296af7c2010-03-29 19:23:50 +00001181
Andreas Färber504134d2012-12-17 06:38:45 +01001182 r = kvm_init_vcpu(cpu);
Jan Kiszka84b49152011-02-01 22:15:50 +01001183 if (r < 0) {
1184 fprintf(stderr, "kvm_init_vcpu failed: %s\n", strerror(-r));
1185 exit(1);
1186 }
Blue Swirl296af7c2010-03-29 19:23:50 +00001187
Paolo Bonzini18268b62017-02-09 09:41:14 +01001188 kvm_init_cpu_signals(cpu);
Blue Swirl296af7c2010-03-29 19:23:50 +00001189
1190 /* signal CPU creation */
Andreas Färber61a46212012-05-02 22:49:36 +02001191 cpu->created = true;
Blue Swirl296af7c2010-03-29 19:23:50 +00001192 qemu_cond_signal(&qemu_cpu_cond);
1193
Gu Zheng4c055ab2016-05-12 09:18:13 +05301194 do {
Andreas Färbera1fcaa72012-05-02 23:42:26 +02001195 if (cpu_can_run(cpu)) {
Andreas Färber1458c362013-05-26 23:46:55 +02001196 r = kvm_cpu_exec(cpu);
Jan Kiszka83f338f2011-02-07 12:19:17 +01001197 if (r == EXCP_DEBUG) {
Andreas Färber91325042013-05-27 02:07:49 +02001198 cpu_handle_guest_debug(cpu);
Jan Kiszka83f338f2011-02-07 12:19:17 +01001199 }
Jan Kiszka0ab07c62011-02-07 12:19:14 +01001200 }
Paolo Bonzinidb08b682018-01-11 13:53:12 +01001201 qemu_wait_io_event(cpu);
Gu Zheng4c055ab2016-05-12 09:18:13 +05301202 } while (!cpu->unplug || cpu_can_run(cpu));
Blue Swirl296af7c2010-03-29 19:23:50 +00001203
Gu Zheng4c055ab2016-05-12 09:18:13 +05301204 qemu_kvm_destroy_vcpu(cpu);
Bharata B Rao2c579042016-05-12 09:18:14 +05301205 cpu->created = false;
1206 qemu_cond_signal(&qemu_cpu_cond);
Gu Zheng4c055ab2016-05-12 09:18:13 +05301207 qemu_mutex_unlock_iothread();
Blue Swirl296af7c2010-03-29 19:23:50 +00001208 return NULL;
1209}
1210
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +02001211static void *qemu_dummy_cpu_thread_fn(void *arg)
1212{
1213#ifdef _WIN32
1214 fprintf(stderr, "qtest is not supported under Windows\n");
1215 exit(1);
1216#else
Andreas Färber10a90212013-05-27 02:24:35 +02001217 CPUState *cpu = arg;
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +02001218 sigset_t waitset;
1219 int r;
1220
Paolo Bonziniab28bd22015-07-09 08:55:38 +02001221 rcu_register_thread();
1222
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +02001223 qemu_mutex_lock_iothread();
Andreas Färber814e6122012-05-02 17:00:37 +02001224 qemu_thread_get_self(cpu->thread);
Andreas Färber9f09e182012-05-03 06:59:07 +02001225 cpu->thread_id = qemu_get_thread_id();
Pavel Dovgalyuk626cf8f2014-12-08 10:53:17 +03001226 cpu->can_do_io = 1;
Alex Bennée37257942017-02-23 18:29:14 +00001227 current_cpu = cpu;
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +02001228
1229 sigemptyset(&waitset);
1230 sigaddset(&waitset, SIG_IPI);
1231
1232 /* signal CPU creation */
Andreas Färber61a46212012-05-02 22:49:36 +02001233 cpu->created = true;
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +02001234 qemu_cond_signal(&qemu_cpu_cond);
1235
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +02001236 while (1) {
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +02001237 qemu_mutex_unlock_iothread();
1238 do {
1239 int sig;
1240 r = sigwait(&waitset, &sig);
1241 } while (r == -1 && (errno == EAGAIN || errno == EINTR));
1242 if (r == -1) {
1243 perror("sigwait");
1244 exit(1);
1245 }
1246 qemu_mutex_lock_iothread();
Paolo Bonzinidb08b682018-01-11 13:53:12 +01001247 qemu_wait_io_event(cpu);
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +02001248 }
1249
1250 return NULL;
1251#endif
1252}
1253
Alex Bennée1be7fcb2016-10-27 16:10:08 +01001254static int64_t tcg_get_icount_limit(void)
1255{
1256 int64_t deadline;
1257
1258 if (replay_mode != REPLAY_MODE_PLAY) {
1259 deadline = qemu_clock_deadline_ns_all(QEMU_CLOCK_VIRTUAL);
1260
1261 /* Maintain prior (possibly buggy) behaviour where if no deadline
1262 * was set (as there is no QEMU_CLOCK_VIRTUAL timer) or it is more than
1263 * INT32_MAX nanoseconds ahead, we still use INT32_MAX
1264 * nanoseconds.
1265 */
1266 if ((deadline < 0) || (deadline > INT32_MAX)) {
1267 deadline = INT32_MAX;
1268 }
1269
1270 return qemu_icount_round(deadline);
1271 } else {
1272 return replay_get_instructions();
1273 }
1274}
1275
Alex Bennée12e97002016-10-27 16:10:14 +01001276static void handle_icount_deadline(void)
1277{
Paolo Bonzini6b8f0182017-03-02 19:56:40 +01001278 assert(qemu_in_vcpu_thread());
Alex Bennée12e97002016-10-27 16:10:14 +01001279 if (use_icount) {
1280 int64_t deadline =
1281 qemu_clock_deadline_ns_all(QEMU_CLOCK_VIRTUAL);
1282
1283 if (deadline == 0) {
Paolo Bonzini6b8f0182017-03-02 19:56:40 +01001284 /* Wake up other AioContexts. */
Alex Bennée12e97002016-10-27 16:10:14 +01001285 qemu_clock_notify(QEMU_CLOCK_VIRTUAL);
Paolo Bonzini6b8f0182017-03-02 19:56:40 +01001286 qemu_clock_run_timers(QEMU_CLOCK_VIRTUAL);
Alex Bennée12e97002016-10-27 16:10:14 +01001287 }
1288 }
1289}
1290
Alex Bennée05248382017-03-29 16:46:59 +01001291static void prepare_icount_for_run(CPUState *cpu)
1292{
1293 if (use_icount) {
Alex Bennéeeda5f7c2017-04-05 12:35:48 +01001294 int insns_left;
Alex Bennée05248382017-03-29 16:46:59 +01001295
1296 /* These should always be cleared by process_icount_data after
1297 * each vCPU execution. However u16.high can be raised
1298 * asynchronously by cpu_exit/cpu_interrupt/tcg_handle_interrupt
1299 */
1300 g_assert(cpu->icount_decr.u16.low == 0);
1301 g_assert(cpu->icount_extra == 0);
1302
Alex Bennéeeda5f7c2017-04-05 12:35:48 +01001303 cpu->icount_budget = tcg_get_icount_limit();
1304 insns_left = MIN(0xffff, cpu->icount_budget);
1305 cpu->icount_decr.u16.low = insns_left;
1306 cpu->icount_extra = cpu->icount_budget - insns_left;
Alex Bennée05248382017-03-29 16:46:59 +01001307 }
1308}
1309
1310static void process_icount_data(CPUState *cpu)
1311{
1312 if (use_icount) {
Alex Bennéee4cd9652017-03-31 16:09:42 +01001313 /* Account for executed instructions */
Alex Bennée512d3c82017-04-05 12:32:37 +01001314 cpu_update_icount(cpu);
Alex Bennée05248382017-03-29 16:46:59 +01001315
1316 /* Reset the counters */
1317 cpu->icount_decr.u16.low = 0;
1318 cpu->icount_extra = 0;
Alex Bennéee4cd9652017-03-31 16:09:42 +01001319 cpu->icount_budget = 0;
1320
Alex Bennée05248382017-03-29 16:46:59 +01001321 replay_account_executed_instructions();
1322 }
1323}
1324
1325
Alex Bennée1be7fcb2016-10-27 16:10:08 +01001326static int tcg_cpu_exec(CPUState *cpu)
1327{
1328 int ret;
1329#ifdef CONFIG_PROFILER
1330 int64_t ti;
1331#endif
1332
1333#ifdef CONFIG_PROFILER
1334 ti = profile_getclock();
1335#endif
Jan Kiszka8d04fb52017-02-23 18:29:11 +00001336 qemu_mutex_unlock_iothread();
Alex Bennée1be7fcb2016-10-27 16:10:08 +01001337 cpu_exec_start(cpu);
1338 ret = cpu_exec(cpu);
1339 cpu_exec_end(cpu);
Jan Kiszka8d04fb52017-02-23 18:29:11 +00001340 qemu_mutex_lock_iothread();
Alex Bennée1be7fcb2016-10-27 16:10:08 +01001341#ifdef CONFIG_PROFILER
1342 tcg_time += profile_getclock() - ti;
1343#endif
Alex Bennée1be7fcb2016-10-27 16:10:08 +01001344 return ret;
1345}
1346
Alex Bennéec93bbbe2016-10-27 16:10:09 +01001347/* Destroy any remaining vCPUs which have been unplugged and have
1348 * finished running
1349 */
1350static void deal_with_unplugged_cpus(void)
Alex Bennée1be7fcb2016-10-27 16:10:08 +01001351{
Alex Bennéec93bbbe2016-10-27 16:10:09 +01001352 CPUState *cpu;
Alex Bennée1be7fcb2016-10-27 16:10:08 +01001353
Alex Bennéec93bbbe2016-10-27 16:10:09 +01001354 CPU_FOREACH(cpu) {
1355 if (cpu->unplug && !cpu_can_run(cpu)) {
1356 qemu_tcg_destroy_vcpu(cpu);
1357 cpu->created = false;
1358 qemu_cond_signal(&qemu_cpu_cond);
Alex Bennée1be7fcb2016-10-27 16:10:08 +01001359 break;
1360 }
1361 }
Alex Bennée1be7fcb2016-10-27 16:10:08 +01001362}
Jan Kiszkabdb7ca62011-09-26 09:40:39 +02001363
Alex Bennée65467062017-02-23 18:29:09 +00001364/* Single-threaded TCG
1365 *
1366 * In the single-threaded case each vCPU is simulated in turn. If
1367 * there is more than a single vCPU we create a simple timer to kick
1368 * the vCPU and ensure we don't get stuck in a tight loop in one vCPU.
1369 * This is done explicitly rather than relying on side-effects
1370 * elsewhere.
1371 */
1372
Alex Bennée37257942017-02-23 18:29:14 +00001373static void *qemu_tcg_rr_cpu_thread_fn(void *arg)
Blue Swirl296af7c2010-03-29 19:23:50 +00001374{
Andreas Färberc3586ba2012-05-03 01:41:24 +02001375 CPUState *cpu = arg;
Blue Swirl296af7c2010-03-29 19:23:50 +00001376
Paolo Bonziniab28bd22015-07-09 08:55:38 +02001377 rcu_register_thread();
Emilio G. Cota3468b592017-07-19 18:57:58 -04001378 tcg_register_thread();
Paolo Bonziniab28bd22015-07-09 08:55:38 +02001379
Paolo Bonzini2e7f7a32015-06-18 18:47:18 +02001380 qemu_mutex_lock_iothread();
Andreas Färber814e6122012-05-02 17:00:37 +02001381 qemu_thread_get_self(cpu->thread);
Blue Swirl296af7c2010-03-29 19:23:50 +00001382
Andreas Färber38fcbd32013-07-07 19:50:23 +02001383 CPU_FOREACH(cpu) {
1384 cpu->thread_id = qemu_get_thread_id();
1385 cpu->created = true;
Pavel Dovgalyuk626cf8f2014-12-08 10:53:17 +03001386 cpu->can_do_io = 1;
Andreas Färber38fcbd32013-07-07 19:50:23 +02001387 }
Blue Swirl296af7c2010-03-29 19:23:50 +00001388 qemu_cond_signal(&qemu_cpu_cond);
1389
Jan Kiszkafa7d1862011-08-22 18:35:25 +02001390 /* wait for initial kick-off after machine start */
Emilio G. Cotac28e3992015-04-27 12:45:28 -04001391 while (first_cpu->stopped) {
KONRAD Fredericd5f8d612015-08-10 17:27:06 +02001392 qemu_cond_wait(first_cpu->halt_cond, &qemu_global_mutex);
Jan Kiszka8e564b42012-02-17 18:31:15 +01001393
1394 /* process any pending work */
Andreas Färberbdc44642013-06-24 23:50:24 +02001395 CPU_FOREACH(cpu) {
Alex Bennée37257942017-02-23 18:29:14 +00001396 current_cpu = cpu;
Andreas Färber182735e2013-05-29 22:29:20 +02001397 qemu_wait_io_event_common(cpu);
Jan Kiszka8e564b42012-02-17 18:31:15 +01001398 }
Jan Kiszka0ab07c62011-02-07 12:19:14 +01001399 }
Blue Swirl296af7c2010-03-29 19:23:50 +00001400
Alex Bennée65467062017-02-23 18:29:09 +00001401 start_tcg_kick_timer();
1402
Alex Bennéec93bbbe2016-10-27 16:10:09 +01001403 cpu = first_cpu;
1404
Alex Bennéee5143e32017-02-23 18:29:12 +00001405 /* process any pending work */
1406 cpu->exit_request = 1;
1407
Blue Swirl296af7c2010-03-29 19:23:50 +00001408 while (1) {
Alex Bennéec93bbbe2016-10-27 16:10:09 +01001409 /* Account partial waits to QEMU_CLOCK_VIRTUAL. */
1410 qemu_account_warp_timer();
1411
Paolo Bonzini6b8f0182017-03-02 19:56:40 +01001412 /* Run the timers here. This is much more efficient than
1413 * waking up the I/O thread and waiting for completion.
1414 */
1415 handle_icount_deadline();
1416
Alex Bennéec93bbbe2016-10-27 16:10:09 +01001417 if (!cpu) {
1418 cpu = first_cpu;
1419 }
1420
Alex Bennéee5143e32017-02-23 18:29:12 +00001421 while (cpu && !cpu->queued_work_first && !cpu->exit_request) {
1422
Alex Bennée791158d2017-02-23 18:29:10 +00001423 atomic_mb_set(&tcg_current_rr_cpu, cpu);
Alex Bennée37257942017-02-23 18:29:14 +00001424 current_cpu = cpu;
Alex Bennéec93bbbe2016-10-27 16:10:09 +01001425
1426 qemu_clock_enable(QEMU_CLOCK_VIRTUAL,
1427 (cpu->singlestep_enabled & SSTEP_NOTIMER) == 0);
1428
1429 if (cpu_can_run(cpu)) {
1430 int r;
Alex Bennée05248382017-03-29 16:46:59 +01001431
1432 prepare_icount_for_run(cpu);
1433
Alex Bennéec93bbbe2016-10-27 16:10:09 +01001434 r = tcg_cpu_exec(cpu);
Alex Bennée05248382017-03-29 16:46:59 +01001435
1436 process_icount_data(cpu);
1437
Alex Bennéec93bbbe2016-10-27 16:10:09 +01001438 if (r == EXCP_DEBUG) {
1439 cpu_handle_guest_debug(cpu);
1440 break;
Pranith Kumar08e73c42017-02-23 18:29:15 +00001441 } else if (r == EXCP_ATOMIC) {
1442 qemu_mutex_unlock_iothread();
1443 cpu_exec_step_atomic(cpu);
1444 qemu_mutex_lock_iothread();
1445 break;
Alex Bennéec93bbbe2016-10-27 16:10:09 +01001446 }
Alex Bennée37257942017-02-23 18:29:14 +00001447 } else if (cpu->stop) {
Alex Bennéec93bbbe2016-10-27 16:10:09 +01001448 if (cpu->unplug) {
1449 cpu = CPU_NEXT(cpu);
1450 }
1451 break;
1452 }
1453
Alex Bennéee5143e32017-02-23 18:29:12 +00001454 cpu = CPU_NEXT(cpu);
1455 } /* while (cpu && !cpu->exit_request).. */
1456
Alex Bennée791158d2017-02-23 18:29:10 +00001457 /* Does not need atomic_mb_set because a spurious wakeup is okay. */
1458 atomic_set(&tcg_current_rr_cpu, NULL);
Alex Bennéec93bbbe2016-10-27 16:10:09 +01001459
Alex Bennéee5143e32017-02-23 18:29:12 +00001460 if (cpu && cpu->exit_request) {
1461 atomic_mb_set(&cpu->exit_request, 0);
1462 }
Alex Blighac70aaf2013-08-21 16:02:57 +01001463
Paolo Bonzinidb08b682018-01-11 13:53:12 +01001464 qemu_tcg_rr_wait_io_event(cpu ? cpu : QTAILQ_FIRST(&cpus));
Alex Bennéec93bbbe2016-10-27 16:10:09 +01001465 deal_with_unplugged_cpus();
Blue Swirl296af7c2010-03-29 19:23:50 +00001466 }
1467
1468 return NULL;
1469}
1470
Vincent Palatinb0cb0a62017-01-10 11:59:57 +01001471static void *qemu_hax_cpu_thread_fn(void *arg)
1472{
1473 CPUState *cpu = arg;
1474 int r;
Vincent Palatinb3d3a422017-03-20 11:15:49 +01001475
1476 qemu_mutex_lock_iothread();
Vincent Palatinb0cb0a62017-01-10 11:59:57 +01001477 qemu_thread_get_self(cpu->thread);
Vincent Palatinb0cb0a62017-01-10 11:59:57 +01001478
1479 cpu->thread_id = qemu_get_thread_id();
1480 cpu->created = true;
1481 cpu->halted = 0;
1482 current_cpu = cpu;
1483
1484 hax_init_vcpu(cpu);
1485 qemu_cond_signal(&qemu_cpu_cond);
1486
1487 while (1) {
1488 if (cpu_can_run(cpu)) {
1489 r = hax_smp_cpu_exec(cpu);
1490 if (r == EXCP_DEBUG) {
1491 cpu_handle_guest_debug(cpu);
1492 }
1493 }
1494
Paolo Bonzinidb08b682018-01-11 13:53:12 +01001495 qemu_wait_io_event(cpu);
Vincent Palatinb0cb0a62017-01-10 11:59:57 +01001496 }
1497 return NULL;
1498}
1499
Sergio Andres Gomez Del Realc97d6d22017-09-13 04:05:09 -05001500/* The HVF-specific vCPU thread function. This one should only run when the host
1501 * CPU supports the VMX "unrestricted guest" feature. */
1502static void *qemu_hvf_cpu_thread_fn(void *arg)
1503{
1504 CPUState *cpu = arg;
1505
1506 int r;
1507
1508 assert(hvf_enabled());
1509
1510 rcu_register_thread();
1511
1512 qemu_mutex_lock_iothread();
1513 qemu_thread_get_self(cpu->thread);
1514
1515 cpu->thread_id = qemu_get_thread_id();
1516 cpu->can_do_io = 1;
1517 current_cpu = cpu;
1518
1519 hvf_init_vcpu(cpu);
1520
1521 /* signal CPU creation */
1522 cpu->created = true;
1523 qemu_cond_signal(&qemu_cpu_cond);
1524
1525 do {
1526 if (cpu_can_run(cpu)) {
1527 r = hvf_vcpu_exec(cpu);
1528 if (r == EXCP_DEBUG) {
1529 cpu_handle_guest_debug(cpu);
1530 }
1531 }
Paolo Bonzinidb08b682018-01-11 13:53:12 +01001532 qemu_wait_io_event(cpu);
Sergio Andres Gomez Del Realc97d6d22017-09-13 04:05:09 -05001533 } while (!cpu->unplug || cpu_can_run(cpu));
1534
1535 hvf_vcpu_destroy(cpu);
1536 cpu->created = false;
1537 qemu_cond_signal(&qemu_cpu_cond);
1538 qemu_mutex_unlock_iothread();
1539 return NULL;
1540}
1541
Vincent Palatinb0cb0a62017-01-10 11:59:57 +01001542#ifdef _WIN32
1543static void CALLBACK dummy_apc_func(ULONG_PTR unused)
1544{
1545}
1546#endif
1547
Alex Bennée37257942017-02-23 18:29:14 +00001548/* Multi-threaded TCG
1549 *
1550 * In the multi-threaded case each vCPU has its own thread. The TLS
1551 * variable current_cpu can be used deep in the code to find the
1552 * current CPUState for a given thread.
1553 */
1554
1555static void *qemu_tcg_cpu_thread_fn(void *arg)
1556{
1557 CPUState *cpu = arg;
1558
Alex Bennéebf51c722017-03-30 18:32:29 +01001559 g_assert(!use_icount);
1560
Alex Bennée37257942017-02-23 18:29:14 +00001561 rcu_register_thread();
Emilio G. Cota3468b592017-07-19 18:57:58 -04001562 tcg_register_thread();
Alex Bennée37257942017-02-23 18:29:14 +00001563
1564 qemu_mutex_lock_iothread();
1565 qemu_thread_get_self(cpu->thread);
1566
1567 cpu->thread_id = qemu_get_thread_id();
1568 cpu->created = true;
1569 cpu->can_do_io = 1;
1570 current_cpu = cpu;
1571 qemu_cond_signal(&qemu_cpu_cond);
1572
1573 /* process any pending work */
1574 cpu->exit_request = 1;
1575
1576 while (1) {
1577 if (cpu_can_run(cpu)) {
1578 int r;
1579 r = tcg_cpu_exec(cpu);
1580 switch (r) {
1581 case EXCP_DEBUG:
1582 cpu_handle_guest_debug(cpu);
1583 break;
1584 case EXCP_HALTED:
1585 /* during start-up the vCPU is reset and the thread is
1586 * kicked several times. If we don't ensure we go back
1587 * to sleep in the halted state we won't cleanly
1588 * start-up when the vCPU is enabled.
1589 *
1590 * cpu->halted should ensure we sleep in wait_io_event
1591 */
1592 g_assert(cpu->halted);
1593 break;
Pranith Kumar08e73c42017-02-23 18:29:15 +00001594 case EXCP_ATOMIC:
1595 qemu_mutex_unlock_iothread();
1596 cpu_exec_step_atomic(cpu);
1597 qemu_mutex_lock_iothread();
Alex Bennée37257942017-02-23 18:29:14 +00001598 default:
1599 /* Ignore everything else? */
1600 break;
1601 }
Bharata B Raoa3e53272017-04-27 10:48:22 +05301602 } else if (cpu->unplug) {
1603 qemu_tcg_destroy_vcpu(cpu);
1604 cpu->created = false;
1605 qemu_cond_signal(&qemu_cpu_cond);
1606 qemu_mutex_unlock_iothread();
1607 return NULL;
Alex Bennée37257942017-02-23 18:29:14 +00001608 }
1609
Alex Bennée37257942017-02-23 18:29:14 +00001610 atomic_mb_set(&cpu->exit_request, 0);
Paolo Bonzinidb08b682018-01-11 13:53:12 +01001611 qemu_wait_io_event(cpu);
Alex Bennée37257942017-02-23 18:29:14 +00001612 }
1613
1614 return NULL;
1615}
1616
Andreas Färber2ff09a42012-05-03 00:23:30 +02001617static void qemu_cpu_kick_thread(CPUState *cpu)
Paolo Bonzinicc015e92011-03-12 17:44:08 +01001618{
1619#ifndef _WIN32
1620 int err;
1621
Paolo Bonzinie0c38212015-08-26 00:19:19 +02001622 if (cpu->thread_kicked) {
1623 return;
Paolo Bonzini9102ded2015-08-18 06:52:09 -07001624 }
Paolo Bonzinie0c38212015-08-26 00:19:19 +02001625 cpu->thread_kicked = true;
Andreas Färber814e6122012-05-02 17:00:37 +02001626 err = pthread_kill(cpu->thread->thread, SIG_IPI);
Paolo Bonzinicc015e92011-03-12 17:44:08 +01001627 if (err) {
1628 fprintf(stderr, "qemu:%s: %s", __func__, strerror(err));
1629 exit(1);
1630 }
1631#else /* _WIN32 */
Vincent Palatinb0cb0a62017-01-10 11:59:57 +01001632 if (!qemu_cpu_is_self(cpu)) {
1633 if (!QueueUserAPC(dummy_apc_func, cpu->hThread, 0)) {
1634 fprintf(stderr, "%s: QueueUserAPC failed with error %lu\n",
1635 __func__, GetLastError());
1636 exit(1);
1637 }
1638 }
Paolo Bonzinicc015e92011-03-12 17:44:08 +01001639#endif
1640}
1641
Andreas Färberc08d7422012-05-03 04:34:15 +02001642void qemu_cpu_kick(CPUState *cpu)
Blue Swirl296af7c2010-03-29 19:23:50 +00001643{
Andreas Färberf5c121b2012-05-03 01:22:49 +02001644 qemu_cond_broadcast(cpu->halt_cond);
Paolo Bonzinie0c38212015-08-26 00:19:19 +02001645 if (tcg_enabled()) {
Alex Bennée791158d2017-02-23 18:29:10 +00001646 cpu_exit(cpu);
Alex Bennée37257942017-02-23 18:29:14 +00001647 /* NOP unless doing single-thread RR */
Alex Bennée791158d2017-02-23 18:29:10 +00001648 qemu_cpu_kick_rr_cpu();
Paolo Bonzinie0c38212015-08-26 00:19:19 +02001649 } else {
Vincent Palatinb0cb0a62017-01-10 11:59:57 +01001650 if (hax_enabled()) {
1651 /*
1652 * FIXME: race condition with the exit_request check in
1653 * hax_vcpu_hax_exec
1654 */
1655 cpu->exit_request = 1;
1656 }
Paolo Bonzinie0c38212015-08-26 00:19:19 +02001657 qemu_cpu_kick_thread(cpu);
1658 }
Blue Swirl296af7c2010-03-29 19:23:50 +00001659}
1660
Jan Kiszka46d62fa2011-02-01 22:15:59 +01001661void qemu_cpu_kick_self(void)
1662{
Andreas Färber4917cf42013-05-27 05:17:50 +02001663 assert(current_cpu);
Paolo Bonzini9102ded2015-08-18 06:52:09 -07001664 qemu_cpu_kick_thread(current_cpu);
Blue Swirl296af7c2010-03-29 19:23:50 +00001665}
1666
Andreas Färber60e82572012-05-02 22:23:49 +02001667bool qemu_cpu_is_self(CPUState *cpu)
Blue Swirl296af7c2010-03-29 19:23:50 +00001668{
Andreas Färber814e6122012-05-02 17:00:37 +02001669 return qemu_thread_is_self(cpu->thread);
Blue Swirl296af7c2010-03-29 19:23:50 +00001670}
1671
Paolo Bonzini79e2b9a2015-01-21 12:09:14 +01001672bool qemu_in_vcpu_thread(void)
Juan Quintelaaa723c22012-09-18 16:30:11 +02001673{
Andreas Färber4917cf42013-05-27 05:17:50 +02001674 return current_cpu && qemu_cpu_is_self(current_cpu);
Juan Quintelaaa723c22012-09-18 16:30:11 +02001675}
1676
Paolo Bonziniafbe7052015-06-18 18:47:19 +02001677static __thread bool iothread_locked = false;
1678
1679bool qemu_mutex_iothread_locked(void)
1680{
1681 return iothread_locked;
1682}
1683
Blue Swirl296af7c2010-03-29 19:23:50 +00001684void qemu_mutex_lock_iothread(void)
1685{
Jan Kiszka8d04fb52017-02-23 18:29:11 +00001686 g_assert(!qemu_mutex_iothread_locked());
1687 qemu_mutex_lock(&qemu_global_mutex);
Paolo Bonziniafbe7052015-06-18 18:47:19 +02001688 iothread_locked = true;
Blue Swirl296af7c2010-03-29 19:23:50 +00001689}
1690
1691void qemu_mutex_unlock_iothread(void)
1692{
Jan Kiszka8d04fb52017-02-23 18:29:11 +00001693 g_assert(qemu_mutex_iothread_locked());
Paolo Bonziniafbe7052015-06-18 18:47:19 +02001694 iothread_locked = false;
Blue Swirl296af7c2010-03-29 19:23:50 +00001695 qemu_mutex_unlock(&qemu_global_mutex);
1696}
1697
Alex Bennéee8faee02016-10-27 16:09:58 +01001698static bool all_vcpus_paused(void)
Blue Swirl296af7c2010-03-29 19:23:50 +00001699{
Andreas Färberbdc44642013-06-24 23:50:24 +02001700 CPUState *cpu;
Blue Swirl296af7c2010-03-29 19:23:50 +00001701
Andreas Färberbdc44642013-06-24 23:50:24 +02001702 CPU_FOREACH(cpu) {
Andreas Färber182735e2013-05-29 22:29:20 +02001703 if (!cpu->stopped) {
Alex Bennéee8faee02016-10-27 16:09:58 +01001704 return false;
Jan Kiszka0ab07c62011-02-07 12:19:14 +01001705 }
Blue Swirl296af7c2010-03-29 19:23:50 +00001706 }
1707
Alex Bennéee8faee02016-10-27 16:09:58 +01001708 return true;
Blue Swirl296af7c2010-03-29 19:23:50 +00001709}
1710
1711void pause_all_vcpus(void)
1712{
Andreas Färberbdc44642013-06-24 23:50:24 +02001713 CPUState *cpu;
Blue Swirl296af7c2010-03-29 19:23:50 +00001714
Alex Bligh40daca52013-08-21 16:03:02 +01001715 qemu_clock_enable(QEMU_CLOCK_VIRTUAL, false);
Andreas Färberbdc44642013-06-24 23:50:24 +02001716 CPU_FOREACH(cpu) {
David Hildenbrandebd05fe2017-11-29 20:12:15 +01001717 if (qemu_cpu_is_self(cpu)) {
1718 qemu_cpu_stop(cpu, true);
1719 } else {
1720 cpu->stop = true;
1721 qemu_cpu_kick(cpu);
1722 }
Jan Kiszkad798e972012-02-17 18:31:16 +01001723 }
1724
Blue Swirl296af7c2010-03-29 19:23:50 +00001725 while (!all_vcpus_paused()) {
Paolo Bonzinibe7d6c52011-03-12 17:44:02 +01001726 qemu_cond_wait(&qemu_pause_cond, &qemu_global_mutex);
Andreas Färberbdc44642013-06-24 23:50:24 +02001727 CPU_FOREACH(cpu) {
Andreas Färber182735e2013-05-29 22:29:20 +02001728 qemu_cpu_kick(cpu);
Blue Swirl296af7c2010-03-29 19:23:50 +00001729 }
1730 }
1731}
1732
Igor Mammedov29936832013-04-23 10:29:37 +02001733void cpu_resume(CPUState *cpu)
1734{
1735 cpu->stop = false;
1736 cpu->stopped = false;
1737 qemu_cpu_kick(cpu);
1738}
1739
Blue Swirl296af7c2010-03-29 19:23:50 +00001740void resume_all_vcpus(void)
1741{
Andreas Färberbdc44642013-06-24 23:50:24 +02001742 CPUState *cpu;
Blue Swirl296af7c2010-03-29 19:23:50 +00001743
Alex Bligh40daca52013-08-21 16:03:02 +01001744 qemu_clock_enable(QEMU_CLOCK_VIRTUAL, true);
Andreas Färberbdc44642013-06-24 23:50:24 +02001745 CPU_FOREACH(cpu) {
Andreas Färber182735e2013-05-29 22:29:20 +02001746 cpu_resume(cpu);
Blue Swirl296af7c2010-03-29 19:23:50 +00001747 }
1748}
1749
Gu Zheng4c055ab2016-05-12 09:18:13 +05301750void cpu_remove(CPUState *cpu)
1751{
1752 cpu->stop = true;
1753 cpu->unplug = true;
1754 qemu_cpu_kick(cpu);
1755}
1756
Bharata B Rao2c579042016-05-12 09:18:14 +05301757void cpu_remove_sync(CPUState *cpu)
1758{
1759 cpu_remove(cpu);
1760 while (cpu->created) {
1761 qemu_cond_wait(&qemu_cpu_cond, &qemu_global_mutex);
1762 }
1763}
1764
Dr. David Alan Gilbert49001162014-01-30 10:20:32 +00001765/* For temporary buffers for forming a name */
1766#define VCPU_THREAD_NAME_SIZE 16
1767
Andreas Färbere5ab30a2012-05-03 01:50:44 +02001768static void qemu_tcg_init_vcpu(CPUState *cpu)
Blue Swirl296af7c2010-03-29 19:23:50 +00001769{
Dr. David Alan Gilbert49001162014-01-30 10:20:32 +00001770 char thread_name[VCPU_THREAD_NAME_SIZE];
Alex Bennée37257942017-02-23 18:29:14 +00001771 static QemuCond *single_tcg_halt_cond;
1772 static QemuThread *single_tcg_cpu_thread;
Emilio G. Cotae8feb962017-07-07 19:24:20 -04001773 static int tcg_region_inited;
1774
1775 /*
1776 * Initialize TCG regions--once. Now is a good time, because:
1777 * (1) TCG's init context, prologue and target globals have been set up.
1778 * (2) qemu_tcg_mttcg_enabled() works now (TCG init code runs before the
1779 * -accel flag is processed, so the check doesn't work then).
1780 */
1781 if (!tcg_region_inited) {
1782 tcg_region_inited = 1;
1783 tcg_region_init();
1784 }
Dr. David Alan Gilbert49001162014-01-30 10:20:32 +00001785
Alex Bennée37257942017-02-23 18:29:14 +00001786 if (qemu_tcg_mttcg_enabled() || !single_tcg_cpu_thread) {
Andreas Färber814e6122012-05-02 17:00:37 +02001787 cpu->thread = g_malloc0(sizeof(QemuThread));
Andreas Färberf5c121b2012-05-03 01:22:49 +02001788 cpu->halt_cond = g_malloc0(sizeof(QemuCond));
1789 qemu_cond_init(cpu->halt_cond);
Alex Bennée37257942017-02-23 18:29:14 +00001790
1791 if (qemu_tcg_mttcg_enabled()) {
1792 /* create a thread per vCPU with TCG (MTTCG) */
1793 parallel_cpus = true;
1794 snprintf(thread_name, VCPU_THREAD_NAME_SIZE, "CPU %d/TCG",
Dr. David Alan Gilbert49001162014-01-30 10:20:32 +00001795 cpu->cpu_index);
Alex Bennée37257942017-02-23 18:29:14 +00001796
1797 qemu_thread_create(cpu->thread, thread_name, qemu_tcg_cpu_thread_fn,
1798 cpu, QEMU_THREAD_JOINABLE);
1799
1800 } else {
1801 /* share a single thread for all cpus with TCG */
1802 snprintf(thread_name, VCPU_THREAD_NAME_SIZE, "ALL CPUs/TCG");
1803 qemu_thread_create(cpu->thread, thread_name,
1804 qemu_tcg_rr_cpu_thread_fn,
1805 cpu, QEMU_THREAD_JOINABLE);
1806
1807 single_tcg_halt_cond = cpu->halt_cond;
1808 single_tcg_cpu_thread = cpu->thread;
1809 }
Paolo Bonzini1ecf47b2011-12-13 13:43:52 +01001810#ifdef _WIN32
Andreas Färber814e6122012-05-02 17:00:37 +02001811 cpu->hThread = qemu_thread_get_handle(cpu->thread);
Paolo Bonzini1ecf47b2011-12-13 13:43:52 +01001812#endif
Andreas Färber61a46212012-05-02 22:49:36 +02001813 while (!cpu->created) {
Paolo Bonzini18a85722011-03-12 17:44:03 +01001814 qemu_cond_wait(&qemu_cpu_cond, &qemu_global_mutex);
Jan Kiszka0ab07c62011-02-07 12:19:14 +01001815 }
Blue Swirl296af7c2010-03-29 19:23:50 +00001816 } else {
Alex Bennée37257942017-02-23 18:29:14 +00001817 /* For non-MTTCG cases we share the thread */
1818 cpu->thread = single_tcg_cpu_thread;
1819 cpu->halt_cond = single_tcg_halt_cond;
Blue Swirl296af7c2010-03-29 19:23:50 +00001820 }
1821}
1822
Vincent Palatinb0cb0a62017-01-10 11:59:57 +01001823static void qemu_hax_start_vcpu(CPUState *cpu)
1824{
1825 char thread_name[VCPU_THREAD_NAME_SIZE];
1826
1827 cpu->thread = g_malloc0(sizeof(QemuThread));
1828 cpu->halt_cond = g_malloc0(sizeof(QemuCond));
1829 qemu_cond_init(cpu->halt_cond);
1830
1831 snprintf(thread_name, VCPU_THREAD_NAME_SIZE, "CPU %d/HAX",
1832 cpu->cpu_index);
1833 qemu_thread_create(cpu->thread, thread_name, qemu_hax_cpu_thread_fn,
1834 cpu, QEMU_THREAD_JOINABLE);
1835#ifdef _WIN32
1836 cpu->hThread = qemu_thread_get_handle(cpu->thread);
1837#endif
1838 while (!cpu->created) {
1839 qemu_cond_wait(&qemu_cpu_cond, &qemu_global_mutex);
1840 }
1841}
1842
Andreas Färber48a106b2013-05-27 02:20:39 +02001843static void qemu_kvm_start_vcpu(CPUState *cpu)
Blue Swirl296af7c2010-03-29 19:23:50 +00001844{
Dr. David Alan Gilbert49001162014-01-30 10:20:32 +00001845 char thread_name[VCPU_THREAD_NAME_SIZE];
1846
Andreas Färber814e6122012-05-02 17:00:37 +02001847 cpu->thread = g_malloc0(sizeof(QemuThread));
Andreas Färberf5c121b2012-05-03 01:22:49 +02001848 cpu->halt_cond = g_malloc0(sizeof(QemuCond));
1849 qemu_cond_init(cpu->halt_cond);
Dr. David Alan Gilbert49001162014-01-30 10:20:32 +00001850 snprintf(thread_name, VCPU_THREAD_NAME_SIZE, "CPU %d/KVM",
1851 cpu->cpu_index);
1852 qemu_thread_create(cpu->thread, thread_name, qemu_kvm_cpu_thread_fn,
1853 cpu, QEMU_THREAD_JOINABLE);
Andreas Färber61a46212012-05-02 22:49:36 +02001854 while (!cpu->created) {
Paolo Bonzini18a85722011-03-12 17:44:03 +01001855 qemu_cond_wait(&qemu_cpu_cond, &qemu_global_mutex);
Jan Kiszka0ab07c62011-02-07 12:19:14 +01001856 }
Blue Swirl296af7c2010-03-29 19:23:50 +00001857}
1858
Sergio Andres Gomez Del Realc97d6d22017-09-13 04:05:09 -05001859static void qemu_hvf_start_vcpu(CPUState *cpu)
1860{
1861 char thread_name[VCPU_THREAD_NAME_SIZE];
1862
1863 /* HVF currently does not support TCG, and only runs in
1864 * unrestricted-guest mode. */
1865 assert(hvf_enabled());
1866
1867 cpu->thread = g_malloc0(sizeof(QemuThread));
1868 cpu->halt_cond = g_malloc0(sizeof(QemuCond));
1869 qemu_cond_init(cpu->halt_cond);
1870
1871 snprintf(thread_name, VCPU_THREAD_NAME_SIZE, "CPU %d/HVF",
1872 cpu->cpu_index);
1873 qemu_thread_create(cpu->thread, thread_name, qemu_hvf_cpu_thread_fn,
1874 cpu, QEMU_THREAD_JOINABLE);
1875 while (!cpu->created) {
1876 qemu_cond_wait(&qemu_cpu_cond, &qemu_global_mutex);
1877 }
1878}
1879
Andreas Färber10a90212013-05-27 02:24:35 +02001880static void qemu_dummy_start_vcpu(CPUState *cpu)
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +02001881{
Dr. David Alan Gilbert49001162014-01-30 10:20:32 +00001882 char thread_name[VCPU_THREAD_NAME_SIZE];
1883
Andreas Färber814e6122012-05-02 17:00:37 +02001884 cpu->thread = g_malloc0(sizeof(QemuThread));
Andreas Färberf5c121b2012-05-03 01:22:49 +02001885 cpu->halt_cond = g_malloc0(sizeof(QemuCond));
1886 qemu_cond_init(cpu->halt_cond);
Dr. David Alan Gilbert49001162014-01-30 10:20:32 +00001887 snprintf(thread_name, VCPU_THREAD_NAME_SIZE, "CPU %d/DUMMY",
1888 cpu->cpu_index);
1889 qemu_thread_create(cpu->thread, thread_name, qemu_dummy_cpu_thread_fn, cpu,
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +02001890 QEMU_THREAD_JOINABLE);
Andreas Färber61a46212012-05-02 22:49:36 +02001891 while (!cpu->created) {
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +02001892 qemu_cond_wait(&qemu_cpu_cond, &qemu_global_mutex);
1893 }
1894}
1895
Andreas Färberc643bed2013-05-27 03:23:24 +02001896void qemu_init_vcpu(CPUState *cpu)
Blue Swirl296af7c2010-03-29 19:23:50 +00001897{
Andreas Färberce3960e2012-12-17 03:27:07 +01001898 cpu->nr_cores = smp_cores;
1899 cpu->nr_threads = smp_threads;
Andreas Färberf324e762012-05-02 23:26:21 +02001900 cpu->stopped = true;
Peter Maydell56943e82016-01-21 14:15:04 +00001901
1902 if (!cpu->as) {
1903 /* If the target cpu hasn't set up any address spaces itself,
1904 * give it the default one.
1905 */
Peter Maydell12ebc9a2016-01-21 14:15:04 +00001906 cpu->num_ases = 1;
Peter Xu80ceb072017-11-23 17:23:32 +08001907 cpu_address_space_init(cpu, 0, "cpu-memory", cpu->memory);
Peter Maydell56943e82016-01-21 14:15:04 +00001908 }
1909
Jan Kiszka0ab07c62011-02-07 12:19:14 +01001910 if (kvm_enabled()) {
Andreas Färber48a106b2013-05-27 02:20:39 +02001911 qemu_kvm_start_vcpu(cpu);
Vincent Palatinb0cb0a62017-01-10 11:59:57 +01001912 } else if (hax_enabled()) {
1913 qemu_hax_start_vcpu(cpu);
Sergio Andres Gomez Del Realc97d6d22017-09-13 04:05:09 -05001914 } else if (hvf_enabled()) {
1915 qemu_hvf_start_vcpu(cpu);
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +02001916 } else if (tcg_enabled()) {
Andreas Färbere5ab30a2012-05-03 01:50:44 +02001917 qemu_tcg_init_vcpu(cpu);
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +02001918 } else {
Andreas Färber10a90212013-05-27 02:24:35 +02001919 qemu_dummy_start_vcpu(cpu);
Jan Kiszka0ab07c62011-02-07 12:19:14 +01001920 }
Blue Swirl296af7c2010-03-29 19:23:50 +00001921}
1922
Jan Kiszkab4a3d962011-02-01 22:15:43 +01001923void cpu_stop_current(void)
Blue Swirl296af7c2010-03-29 19:23:50 +00001924{
Andreas Färber4917cf42013-05-27 05:17:50 +02001925 if (current_cpu) {
David Hildenbrandebd05fe2017-11-29 20:12:15 +01001926 qemu_cpu_stop(current_cpu, true);
Jan Kiszkab4a3d962011-02-01 22:15:43 +01001927 }
Blue Swirl296af7c2010-03-29 19:23:50 +00001928}
1929
Kevin Wolf56983462013-07-05 13:49:54 +02001930int vm_stop(RunState state)
Blue Swirl296af7c2010-03-29 19:23:50 +00001931{
Juan Quintelaaa723c22012-09-18 16:30:11 +02001932 if (qemu_in_vcpu_thread()) {
Paolo Bonzini74892d22014-06-05 14:53:58 +02001933 qemu_system_vmstop_request_prepare();
Luiz Capitulino1dfb4dd2011-07-29 14:26:33 -03001934 qemu_system_vmstop_request(state);
Blue Swirl296af7c2010-03-29 19:23:50 +00001935 /*
1936 * FIXME: should not return to device code in case
1937 * vm_stop() has been requested.
1938 */
Jan Kiszkab4a3d962011-02-01 22:15:43 +01001939 cpu_stop_current();
Kevin Wolf56983462013-07-05 13:49:54 +02001940 return 0;
Blue Swirl296af7c2010-03-29 19:23:50 +00001941 }
Kevin Wolf56983462013-07-05 13:49:54 +02001942
1943 return do_vm_stop(state);
Blue Swirl296af7c2010-03-29 19:23:50 +00001944}
1945
Claudio Imbrenda2d76e822017-02-14 18:07:47 +01001946/**
1947 * Prepare for (re)starting the VM.
1948 * Returns -1 if the vCPUs are not to be restarted (e.g. if they are already
1949 * running or in case of an error condition), 0 otherwise.
1950 */
1951int vm_prepare_start(void)
1952{
1953 RunState requested;
1954 int res = 0;
1955
1956 qemu_vmstop_requested(&requested);
1957 if (runstate_is_running() && requested == RUN_STATE__MAX) {
1958 return -1;
1959 }
1960
1961 /* Ensure that a STOP/RESUME pair of events is emitted if a
1962 * vmstop request was pending. The BLOCK_IO_ERROR event, for
1963 * example, according to documentation is always followed by
1964 * the STOP event.
1965 */
1966 if (runstate_is_running()) {
1967 qapi_event_send_stop(&error_abort);
1968 res = -1;
1969 } else {
1970 replay_enable_events();
1971 cpu_enable_ticks();
1972 runstate_set(RUN_STATE_RUNNING);
1973 vm_state_notify(1, RUN_STATE_RUNNING);
1974 }
1975
1976 /* We are sending this now, but the CPUs will be resumed shortly later */
1977 qapi_event_send_resume(&error_abort);
1978 return res;
1979}
1980
1981void vm_start(void)
1982{
1983 if (!vm_prepare_start()) {
1984 resume_all_vcpus();
1985 }
1986}
1987
Luiz Capitulino8a9236f2011-10-14 11:18:09 -03001988/* does a state transition even if the VM is already stopped,
1989 current state is forgotten forever */
Kevin Wolf56983462013-07-05 13:49:54 +02001990int vm_stop_force_state(RunState state)
Luiz Capitulino8a9236f2011-10-14 11:18:09 -03001991{
1992 if (runstate_is_running()) {
Kevin Wolf56983462013-07-05 13:49:54 +02001993 return vm_stop(state);
Luiz Capitulino8a9236f2011-10-14 11:18:09 -03001994 } else {
1995 runstate_set(state);
Wen Congyangb2780d32015-11-20 17:34:38 +08001996
1997 bdrv_drain_all();
Kevin Wolf594a45c2013-07-18 14:52:19 +02001998 /* Make sure to return an error if the flush in a previous vm_stop()
1999 * failed. */
John Snow22af08e2016-09-22 21:45:51 -04002000 return bdrv_flush_all();
Luiz Capitulino8a9236f2011-10-14 11:18:09 -03002001 }
2002}
2003
Stefan Weil9a78eea2010-10-22 23:03:33 +02002004void list_cpus(FILE *f, fprintf_function cpu_fprintf, const char *optarg)
Blue Swirl262353c2010-05-04 19:55:35 +00002005{
2006 /* XXX: implement xxx_cpu_list for targets that still miss it */
Peter Maydelle916cbf2012-09-05 17:41:08 -03002007#if defined(cpu_list)
2008 cpu_list(f, cpu_fprintf);
Blue Swirl262353c2010-05-04 19:55:35 +00002009#endif
2010}
Luiz Capitulinode0b36b2011-09-21 16:38:35 -03002011
2012CpuInfoList *qmp_query_cpus(Error **errp)
2013{
Igor Mammedovafed5a52017-05-10 13:29:55 +02002014 MachineState *ms = MACHINE(qdev_get_machine());
2015 MachineClass *mc = MACHINE_GET_CLASS(ms);
Luiz Capitulinode0b36b2011-09-21 16:38:35 -03002016 CpuInfoList *head = NULL, *cur_item = NULL;
Andreas Färber182735e2013-05-29 22:29:20 +02002017 CPUState *cpu;
Luiz Capitulinode0b36b2011-09-21 16:38:35 -03002018
Andreas Färberbdc44642013-06-24 23:50:24 +02002019 CPU_FOREACH(cpu) {
Luiz Capitulinode0b36b2011-09-21 16:38:35 -03002020 CpuInfoList *info;
Andreas Färber182735e2013-05-29 22:29:20 +02002021#if defined(TARGET_I386)
2022 X86CPU *x86_cpu = X86_CPU(cpu);
2023 CPUX86State *env = &x86_cpu->env;
2024#elif defined(TARGET_PPC)
2025 PowerPCCPU *ppc_cpu = POWERPC_CPU(cpu);
2026 CPUPPCState *env = &ppc_cpu->env;
2027#elif defined(TARGET_SPARC)
2028 SPARCCPU *sparc_cpu = SPARC_CPU(cpu);
2029 CPUSPARCState *env = &sparc_cpu->env;
2030#elif defined(TARGET_MIPS)
2031 MIPSCPU *mips_cpu = MIPS_CPU(cpu);
2032 CPUMIPSState *env = &mips_cpu->env;
Bastian Koppelmann48e06fe2014-09-01 12:59:46 +01002033#elif defined(TARGET_TRICORE)
2034 TriCoreCPU *tricore_cpu = TRICORE_CPU(cpu);
2035 CPUTriCoreState *env = &tricore_cpu->env;
Andreas Färber182735e2013-05-29 22:29:20 +02002036#endif
Luiz Capitulinode0b36b2011-09-21 16:38:35 -03002037
Andreas Färbercb446ec2013-05-01 14:24:52 +02002038 cpu_synchronize_state(cpu);
Luiz Capitulinode0b36b2011-09-21 16:38:35 -03002039
2040 info = g_malloc0(sizeof(*info));
2041 info->value = g_malloc0(sizeof(*info->value));
Andreas Färber55e5c282012-12-17 06:18:02 +01002042 info->value->CPU = cpu->cpu_index;
Andreas Färber182735e2013-05-29 22:29:20 +02002043 info->value->current = (cpu == first_cpu);
Andreas Färber259186a2013-01-17 18:51:17 +01002044 info->value->halted = cpu->halted;
Eduardo Habkost58f88d42015-05-08 16:04:22 -03002045 info->value->qom_path = object_get_canonical_path(OBJECT(cpu));
Andreas Färber9f09e182012-05-03 06:59:07 +02002046 info->value->thread_id = cpu->thread_id;
Luiz Capitulinode0b36b2011-09-21 16:38:35 -03002047#if defined(TARGET_I386)
Eric Blake86f4b682015-11-18 01:52:59 -07002048 info->value->arch = CPU_INFO_ARCH_X86;
Eric Blake544a3732016-02-17 23:48:27 -07002049 info->value->u.x86.pc = env->eip + env->segs[R_CS].base;
Luiz Capitulinode0b36b2011-09-21 16:38:35 -03002050#elif defined(TARGET_PPC)
Eric Blake86f4b682015-11-18 01:52:59 -07002051 info->value->arch = CPU_INFO_ARCH_PPC;
Eric Blake544a3732016-02-17 23:48:27 -07002052 info->value->u.ppc.nip = env->nip;
Luiz Capitulinode0b36b2011-09-21 16:38:35 -03002053#elif defined(TARGET_SPARC)
Eric Blake86f4b682015-11-18 01:52:59 -07002054 info->value->arch = CPU_INFO_ARCH_SPARC;
Eric Blake544a3732016-02-17 23:48:27 -07002055 info->value->u.q_sparc.pc = env->pc;
2056 info->value->u.q_sparc.npc = env->npc;
Luiz Capitulinode0b36b2011-09-21 16:38:35 -03002057#elif defined(TARGET_MIPS)
Eric Blake86f4b682015-11-18 01:52:59 -07002058 info->value->arch = CPU_INFO_ARCH_MIPS;
Eric Blake544a3732016-02-17 23:48:27 -07002059 info->value->u.q_mips.PC = env->active_tc.PC;
Bastian Koppelmann48e06fe2014-09-01 12:59:46 +01002060#elif defined(TARGET_TRICORE)
Eric Blake86f4b682015-11-18 01:52:59 -07002061 info->value->arch = CPU_INFO_ARCH_TRICORE;
Eric Blake544a3732016-02-17 23:48:27 -07002062 info->value->u.tricore.PC = env->PC;
Eric Blake86f4b682015-11-18 01:52:59 -07002063#else
2064 info->value->arch = CPU_INFO_ARCH_OTHER;
Luiz Capitulinode0b36b2011-09-21 16:38:35 -03002065#endif
Igor Mammedovafed5a52017-05-10 13:29:55 +02002066 info->value->has_props = !!mc->cpu_index_to_instance_props;
2067 if (info->value->has_props) {
2068 CpuInstanceProperties *props;
2069 props = g_malloc0(sizeof(*props));
2070 *props = mc->cpu_index_to_instance_props(ms, cpu->cpu_index);
2071 info->value->props = props;
2072 }
Luiz Capitulinode0b36b2011-09-21 16:38:35 -03002073
2074 /* XXX: waiting for the qapi to support GSList */
2075 if (!cur_item) {
2076 head = cur_item = info;
2077 } else {
2078 cur_item->next = info;
2079 cur_item = info;
2080 }
2081 }
2082
2083 return head;
2084}
Luiz Capitulino0cfd6a92011-11-22 16:32:37 -02002085
2086void qmp_memsave(int64_t addr, int64_t size, const char *filename,
2087 bool has_cpu, int64_t cpu_index, Error **errp)
2088{
2089 FILE *f;
2090 uint32_t l;
Andreas Färber55e5c282012-12-17 06:18:02 +01002091 CPUState *cpu;
Luiz Capitulino0cfd6a92011-11-22 16:32:37 -02002092 uint8_t buf[1024];
Borislav Petkov0dc9daf2015-02-08 13:14:38 +01002093 int64_t orig_addr = addr, orig_size = size;
Luiz Capitulino0cfd6a92011-11-22 16:32:37 -02002094
2095 if (!has_cpu) {
2096 cpu_index = 0;
2097 }
2098
Andreas Färber151d1322013-02-15 15:41:49 +01002099 cpu = qemu_get_cpu(cpu_index);
2100 if (cpu == NULL) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01002101 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "cpu-index",
2102 "a CPU number");
Luiz Capitulino0cfd6a92011-11-22 16:32:37 -02002103 return;
2104 }
2105
2106 f = fopen(filename, "wb");
2107 if (!f) {
Luiz Capitulino618da852013-06-07 14:35:06 -04002108 error_setg_file_open(errp, errno, filename);
Luiz Capitulino0cfd6a92011-11-22 16:32:37 -02002109 return;
2110 }
2111
2112 while (size != 0) {
2113 l = sizeof(buf);
2114 if (l > size)
2115 l = size;
Aneesh Kumar K.V2f4d0f52013-10-01 21:49:30 +05302116 if (cpu_memory_rw_debug(cpu, addr, buf, l, 0) != 0) {
Borislav Petkov0dc9daf2015-02-08 13:14:38 +01002117 error_setg(errp, "Invalid addr 0x%016" PRIx64 "/size %" PRId64
2118 " specified", orig_addr, orig_size);
Aneesh Kumar K.V2f4d0f52013-10-01 21:49:30 +05302119 goto exit;
2120 }
Luiz Capitulino0cfd6a92011-11-22 16:32:37 -02002121 if (fwrite(buf, 1, l, f) != l) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01002122 error_setg(errp, QERR_IO_ERROR);
Luiz Capitulino0cfd6a92011-11-22 16:32:37 -02002123 goto exit;
2124 }
2125 addr += l;
2126 size -= l;
2127 }
2128
2129exit:
2130 fclose(f);
2131}
Luiz Capitulino6d3962b2011-11-22 17:26:46 -02002132
2133void qmp_pmemsave(int64_t addr, int64_t size, const char *filename,
2134 Error **errp)
2135{
2136 FILE *f;
2137 uint32_t l;
2138 uint8_t buf[1024];
2139
2140 f = fopen(filename, "wb");
2141 if (!f) {
Luiz Capitulino618da852013-06-07 14:35:06 -04002142 error_setg_file_open(errp, errno, filename);
Luiz Capitulino6d3962b2011-11-22 17:26:46 -02002143 return;
2144 }
2145
2146 while (size != 0) {
2147 l = sizeof(buf);
2148 if (l > size)
2149 l = size;
Stefan Weileb6282f2014-04-07 20:28:23 +02002150 cpu_physical_memory_read(addr, buf, l);
Luiz Capitulino6d3962b2011-11-22 17:26:46 -02002151 if (fwrite(buf, 1, l, f) != l) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01002152 error_setg(errp, QERR_IO_ERROR);
Luiz Capitulino6d3962b2011-11-22 17:26:46 -02002153 goto exit;
2154 }
2155 addr += l;
2156 size -= l;
2157 }
2158
2159exit:
2160 fclose(f);
2161}
Luiz Capitulinoab49ab52011-11-23 12:55:53 -02002162
2163void qmp_inject_nmi(Error **errp)
2164{
Alexey Kardashevskiy9cb805f2014-08-20 22:16:33 +10002165 nmi_monitor_handle(monitor_get_cpu_index(), errp);
Luiz Capitulinoab49ab52011-11-23 12:55:53 -02002166}
Sebastian Tanase27498be2014-07-25 11:56:33 +02002167
2168void dump_drift_info(FILE *f, fprintf_function cpu_fprintf)
2169{
2170 if (!use_icount) {
2171 return;
2172 }
2173
2174 cpu_fprintf(f, "Host - Guest clock %"PRIi64" ms\n",
2175 (cpu_get_clock() - cpu_get_icount())/SCALE_MS);
2176 if (icount_align_option) {
2177 cpu_fprintf(f, "Max guest delay %"PRIi64" ms\n", -max_delay/SCALE_MS);
2178 cpu_fprintf(f, "Max guest advance %"PRIi64" ms\n", max_advance/SCALE_MS);
2179 } else {
2180 cpu_fprintf(f, "Max guest delay NA\n");
2181 cpu_fprintf(f, "Max guest advance NA\n");
2182 }
2183}