blob: cbeb1f6139ea35de86b7684b5a527ae72bf306d4 [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"
Blue Swirl296af7c2010-03-29 19:23:50 +000027
Paolo Bonzini83c90892012-12-17 18:19:49 +010028#include "monitor/monitor.h"
Wenchao Xiaa4e15de2014-06-18 08:43:36 +020029#include "qapi/qmp/qerror.h"
Markus Armbrusterd49b6832015-03-17 18:29:20 +010030#include "qemu/error-report.h"
Paolo Bonzini9c17d612012-12-17 18:20:04 +010031#include "sysemu/sysemu.h"
Max Reitzda31d592016-03-16 19:54:32 +010032#include "sysemu/block-backend.h"
Paolo Bonzini022c62c2012-12-17 18:19:49 +010033#include "exec/gdbstub.h"
Paolo Bonzini9c17d612012-12-17 18:20:04 +010034#include "sysemu/dma.h"
35#include "sysemu/kvm.h"
Luiz Capitulinode0b36b2011-09-21 16:38:35 -030036#include "qmp-commands.h"
Blue Swirl296af7c2010-03-29 19:23:50 +000037
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010038#include "qemu/thread.h"
Paolo Bonzini9c17d612012-12-17 18:20:04 +010039#include "sysemu/cpus.h"
40#include "sysemu/qtest.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010041#include "qemu/main-loop.h"
42#include "qemu/bitmap.h"
Liu Ping Fancb365642013-09-25 14:20:58 +080043#include "qemu/seqlock.h"
Wenchao Xiaa4e15de2014-06-18 08:43:36 +020044#include "qapi-event.h"
Alexey Kardashevskiy9cb805f2014-08-20 22:16:33 +100045#include "hw/nmi.h"
Pavel Dovgalyuk8b427042015-09-17 19:24:05 +030046#include "sysemu/replay.h"
Jan Kiszka0ff0fc12011-06-23 10:15:55 +020047
48#ifndef _WIN32
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010049#include "qemu/compatfd.h"
Jan Kiszka0ff0fc12011-06-23 10:15:55 +020050#endif
Blue Swirl296af7c2010-03-29 19:23:50 +000051
Jan Kiszka6d9cb732011-02-01 22:15:58 +010052#ifdef CONFIG_LINUX
53
54#include <sys/prctl.h>
55
Marcelo Tosattic0532a72010-10-11 15:31:21 -030056#ifndef PR_MCE_KILL
57#define PR_MCE_KILL 33
58#endif
59
Jan Kiszka6d9cb732011-02-01 22:15:58 +010060#ifndef PR_MCE_KILL_SET
61#define PR_MCE_KILL_SET 1
62#endif
63
64#ifndef PR_MCE_KILL_EARLY
65#define PR_MCE_KILL_EARLY 1
66#endif
67
68#endif /* CONFIG_LINUX */
69
Andreas Färber182735e2013-05-29 22:29:20 +020070static CPUState *next_cpu;
Sebastian Tanase27498be2014-07-25 11:56:33 +020071int64_t max_delay;
72int64_t max_advance;
Blue Swirl296af7c2010-03-29 19:23:50 +000073
Jason J. Herne2adcc852015-09-08 13:12:33 -040074/* vcpu throttling controls */
75static QEMUTimer *throttle_timer;
76static unsigned int throttle_percentage;
77
78#define CPU_THROTTLE_PCT_MIN 1
79#define CPU_THROTTLE_PCT_MAX 99
80#define CPU_THROTTLE_TIMESLICE_NS 10000000
81
Tiejun Chen321bc0b2013-08-02 09:43:09 +080082bool cpu_is_stopped(CPUState *cpu)
83{
84 return cpu->stopped || !runstate_is_running();
85}
86
Andreas Färbera98ae1d2013-05-26 23:21:08 +020087static bool cpu_thread_is_idle(CPUState *cpu)
Peter Maydellac873f12012-07-19 16:52:27 +010088{
Andreas Färberc64ca812012-05-03 02:11:45 +020089 if (cpu->stop || cpu->queued_work_first) {
Peter Maydellac873f12012-07-19 16:52:27 +010090 return false;
91 }
Tiejun Chen321bc0b2013-08-02 09:43:09 +080092 if (cpu_is_stopped(cpu)) {
Peter Maydellac873f12012-07-19 16:52:27 +010093 return true;
94 }
Andreas Färber8c2e1b02013-08-25 18:53:55 +020095 if (!cpu->halted || cpu_has_work(cpu) ||
Alexander Graf215e79c2013-04-24 22:24:12 +020096 kvm_halt_in_kernel()) {
Peter Maydellac873f12012-07-19 16:52:27 +010097 return false;
98 }
99 return true;
100}
101
102static bool all_cpu_threads_idle(void)
103{
Andreas Färber182735e2013-05-29 22:29:20 +0200104 CPUState *cpu;
Peter Maydellac873f12012-07-19 16:52:27 +0100105
Andreas Färberbdc44642013-06-24 23:50:24 +0200106 CPU_FOREACH(cpu) {
Andreas Färber182735e2013-05-29 22:29:20 +0200107 if (!cpu_thread_is_idle(cpu)) {
Peter Maydellac873f12012-07-19 16:52:27 +0100108 return false;
109 }
110 }
111 return true;
112}
113
Blue Swirl296af7c2010-03-29 19:23:50 +0000114/***********************************************************/
Paolo Bonzini946fb272011-09-12 13:57:37 +0200115/* guest cycle counter */
116
Paolo Bonzinia3270e12013-10-07 17:18:15 +0200117/* Protected by TimersState seqlock */
118
Victor CLEMENT5045e9d92015-05-29 17:14:04 +0200119static bool icount_sleep = true;
Sebastian Tanase71468392014-07-23 11:47:50 +0200120static int64_t vm_clock_warp_start = -1;
Paolo Bonzini946fb272011-09-12 13:57:37 +0200121/* Conversion factor from emulated instructions to virtual clock ticks. */
122static int icount_time_shift;
123/* Arbitrarily pick 1MIPS as the minimum allowable speed. */
124#define MAX_ICOUNT_SHIFT 10
Paolo Bonzinia3270e12013-10-07 17:18:15 +0200125
Paolo Bonzini946fb272011-09-12 13:57:37 +0200126static QEMUTimer *icount_rt_timer;
127static QEMUTimer *icount_vm_timer;
128static QEMUTimer *icount_warp_timer;
Paolo Bonzini946fb272011-09-12 13:57:37 +0200129
130typedef struct TimersState {
Liu Ping Fancb365642013-09-25 14:20:58 +0800131 /* Protected by BQL. */
Paolo Bonzini946fb272011-09-12 13:57:37 +0200132 int64_t cpu_ticks_prev;
133 int64_t cpu_ticks_offset;
Liu Ping Fancb365642013-09-25 14:20:58 +0800134
135 /* cpu_clock_offset can be read out of BQL, so protect it with
136 * this lock.
137 */
138 QemuSeqLock vm_clock_seqlock;
Paolo Bonzini946fb272011-09-12 13:57:37 +0200139 int64_t cpu_clock_offset;
140 int32_t cpu_ticks_enabled;
141 int64_t dummy;
KONRAD Fredericc96778b2014-08-01 01:37:09 +0200142
143 /* Compensate for varying guest execution speed. */
144 int64_t qemu_icount_bias;
145 /* Only written by TCG thread */
146 int64_t qemu_icount;
Paolo Bonzini946fb272011-09-12 13:57:37 +0200147} TimersState;
148
Liu Ping Fand9cd4002013-07-21 08:43:00 +0000149static TimersState timers_state;
Paolo Bonzini946fb272011-09-12 13:57:37 +0200150
Pavel Dovgalyuk2a629142014-12-08 10:53:45 +0300151int64_t cpu_get_icount_raw(void)
Paolo Bonzini946fb272011-09-12 13:57:37 +0200152{
153 int64_t icount;
Andreas Färber4917cf42013-05-27 05:17:50 +0200154 CPUState *cpu = current_cpu;
Paolo Bonzini946fb272011-09-12 13:57:37 +0200155
KONRAD Fredericc96778b2014-08-01 01:37:09 +0200156 icount = timers_state.qemu_icount;
Andreas Färber4917cf42013-05-27 05:17:50 +0200157 if (cpu) {
Paolo Bonzini414b15c2015-06-24 14:16:26 +0200158 if (!cpu->can_do_io) {
Pavel Dovgalyuk2a629142014-12-08 10:53:45 +0300159 fprintf(stderr, "Bad icount read\n");
160 exit(1);
Paolo Bonzini946fb272011-09-12 13:57:37 +0200161 }
Andreas Färber28ecfd72013-08-26 05:51:49 +0200162 icount -= (cpu->icount_decr.u16.low + cpu->icount_extra);
Paolo Bonzini946fb272011-09-12 13:57:37 +0200163 }
Pavel Dovgalyuk2a629142014-12-08 10:53:45 +0300164 return icount;
165}
166
167/* Return the virtual CPU time, based on the instruction counter. */
168static int64_t cpu_get_icount_locked(void)
169{
170 int64_t icount = cpu_get_icount_raw();
KONRAD Frederic3f031312014-08-01 01:37:15 +0200171 return timers_state.qemu_icount_bias + cpu_icount_to_ns(icount);
Paolo Bonzini946fb272011-09-12 13:57:37 +0200172}
173
Paolo Bonzini17a15f12013-10-03 15:17:25 +0200174int64_t cpu_get_icount(void)
175{
176 int64_t icount;
177 unsigned start;
178
179 do {
180 start = seqlock_read_begin(&timers_state.vm_clock_seqlock);
181 icount = cpu_get_icount_locked();
182 } while (seqlock_read_retry(&timers_state.vm_clock_seqlock, start));
183
184 return icount;
185}
186
KONRAD Frederic3f031312014-08-01 01:37:15 +0200187int64_t cpu_icount_to_ns(int64_t icount)
188{
189 return icount << icount_time_shift;
190}
191
Paolo Bonzini946fb272011-09-12 13:57:37 +0200192/* return the host CPU cycle counter and handle stop/restart */
Liu Ping Fancb365642013-09-25 14:20:58 +0800193/* Caller must hold the BQL */
Paolo Bonzini946fb272011-09-12 13:57:37 +0200194int64_t cpu_get_ticks(void)
195{
Paolo Bonzini5f3e3102013-10-28 17:32:18 +0100196 int64_t ticks;
197
Paolo Bonzini946fb272011-09-12 13:57:37 +0200198 if (use_icount) {
199 return cpu_get_icount();
200 }
Paolo Bonzini5f3e3102013-10-28 17:32:18 +0100201
202 ticks = timers_state.cpu_ticks_offset;
203 if (timers_state.cpu_ticks_enabled) {
Christopher Covington4a7428c2015-09-25 10:42:21 -0400204 ticks += cpu_get_host_ticks();
Paolo Bonzini946fb272011-09-12 13:57:37 +0200205 }
Paolo Bonzini5f3e3102013-10-28 17:32:18 +0100206
207 if (timers_state.cpu_ticks_prev > ticks) {
208 /* Note: non increasing ticks may happen if the host uses
209 software suspend */
210 timers_state.cpu_ticks_offset += timers_state.cpu_ticks_prev - ticks;
211 ticks = timers_state.cpu_ticks_prev;
212 }
213
214 timers_state.cpu_ticks_prev = ticks;
215 return ticks;
Paolo Bonzini946fb272011-09-12 13:57:37 +0200216}
217
Liu Ping Fancb365642013-09-25 14:20:58 +0800218static int64_t cpu_get_clock_locked(void)
219{
Paolo Bonzini5f3e3102013-10-28 17:32:18 +0100220 int64_t ticks;
Liu Ping Fancb365642013-09-25 14:20:58 +0800221
Paolo Bonzini5f3e3102013-10-28 17:32:18 +0100222 ticks = timers_state.cpu_clock_offset;
223 if (timers_state.cpu_ticks_enabled) {
224 ticks += get_clock();
Liu Ping Fancb365642013-09-25 14:20:58 +0800225 }
226
Paolo Bonzini5f3e3102013-10-28 17:32:18 +0100227 return ticks;
Liu Ping Fancb365642013-09-25 14:20:58 +0800228}
229
Paolo Bonzini946fb272011-09-12 13:57:37 +0200230/* return the host CPU monotonic timer and handle stop/restart */
231int64_t cpu_get_clock(void)
232{
233 int64_t ti;
Liu Ping Fancb365642013-09-25 14:20:58 +0800234 unsigned start;
235
236 do {
237 start = seqlock_read_begin(&timers_state.vm_clock_seqlock);
238 ti = cpu_get_clock_locked();
239 } while (seqlock_read_retry(&timers_state.vm_clock_seqlock, start));
240
241 return ti;
Paolo Bonzini946fb272011-09-12 13:57:37 +0200242}
243
Liu Ping Fancb365642013-09-25 14:20:58 +0800244/* enable cpu_get_ticks()
245 * Caller must hold BQL which server as mutex for vm_clock_seqlock.
246 */
Paolo Bonzini946fb272011-09-12 13:57:37 +0200247void cpu_enable_ticks(void)
248{
Liu Ping Fancb365642013-09-25 14:20:58 +0800249 /* Here, the really thing protected by seqlock is cpu_clock_offset. */
250 seqlock_write_lock(&timers_state.vm_clock_seqlock);
Paolo Bonzini946fb272011-09-12 13:57:37 +0200251 if (!timers_state.cpu_ticks_enabled) {
Christopher Covington4a7428c2015-09-25 10:42:21 -0400252 timers_state.cpu_ticks_offset -= cpu_get_host_ticks();
Paolo Bonzini946fb272011-09-12 13:57:37 +0200253 timers_state.cpu_clock_offset -= get_clock();
254 timers_state.cpu_ticks_enabled = 1;
255 }
Liu Ping Fancb365642013-09-25 14:20:58 +0800256 seqlock_write_unlock(&timers_state.vm_clock_seqlock);
Paolo Bonzini946fb272011-09-12 13:57:37 +0200257}
258
259/* disable cpu_get_ticks() : the clock is stopped. You must not call
Liu Ping Fancb365642013-09-25 14:20:58 +0800260 * cpu_get_ticks() after that.
261 * Caller must hold BQL which server as mutex for vm_clock_seqlock.
262 */
Paolo Bonzini946fb272011-09-12 13:57:37 +0200263void cpu_disable_ticks(void)
264{
Liu Ping Fancb365642013-09-25 14:20:58 +0800265 /* Here, the really thing protected by seqlock is cpu_clock_offset. */
266 seqlock_write_lock(&timers_state.vm_clock_seqlock);
Paolo Bonzini946fb272011-09-12 13:57:37 +0200267 if (timers_state.cpu_ticks_enabled) {
Christopher Covington4a7428c2015-09-25 10:42:21 -0400268 timers_state.cpu_ticks_offset += cpu_get_host_ticks();
Liu Ping Fancb365642013-09-25 14:20:58 +0800269 timers_state.cpu_clock_offset = cpu_get_clock_locked();
Paolo Bonzini946fb272011-09-12 13:57:37 +0200270 timers_state.cpu_ticks_enabled = 0;
271 }
Liu Ping Fancb365642013-09-25 14:20:58 +0800272 seqlock_write_unlock(&timers_state.vm_clock_seqlock);
Paolo Bonzini946fb272011-09-12 13:57:37 +0200273}
274
275/* Correlation between real and virtual time is always going to be
276 fairly approximate, so ignore small variation.
277 When the guest is idle real and virtual time will be aligned in
278 the IO wait loop. */
Rutuja Shah73bcb242016-03-21 21:32:30 +0530279#define ICOUNT_WOBBLE (NANOSECONDS_PER_SECOND / 10)
Paolo Bonzini946fb272011-09-12 13:57:37 +0200280
281static void icount_adjust(void)
282{
283 int64_t cur_time;
284 int64_t cur_icount;
285 int64_t delta;
Paolo Bonzinia3270e12013-10-07 17:18:15 +0200286
287 /* Protected by TimersState mutex. */
Paolo Bonzini946fb272011-09-12 13:57:37 +0200288 static int64_t last_delta;
Paolo Bonzini468cc7c2013-10-07 17:21:51 +0200289
Paolo Bonzini946fb272011-09-12 13:57:37 +0200290 /* If the VM is not running, then do nothing. */
291 if (!runstate_is_running()) {
292 return;
293 }
Paolo Bonzini468cc7c2013-10-07 17:21:51 +0200294
Paolo Bonzini17a15f12013-10-03 15:17:25 +0200295 seqlock_write_lock(&timers_state.vm_clock_seqlock);
296 cur_time = cpu_get_clock_locked();
297 cur_icount = cpu_get_icount_locked();
Paolo Bonzini468cc7c2013-10-07 17:21:51 +0200298
Paolo Bonzini946fb272011-09-12 13:57:37 +0200299 delta = cur_icount - cur_time;
300 /* FIXME: This is a very crude algorithm, somewhat prone to oscillation. */
301 if (delta > 0
302 && last_delta + ICOUNT_WOBBLE < delta * 2
303 && icount_time_shift > 0) {
304 /* The guest is getting too far ahead. Slow time down. */
305 icount_time_shift--;
306 }
307 if (delta < 0
308 && last_delta - ICOUNT_WOBBLE > delta * 2
309 && icount_time_shift < MAX_ICOUNT_SHIFT) {
310 /* The guest is getting too far behind. Speed time up. */
311 icount_time_shift++;
312 }
313 last_delta = delta;
KONRAD Fredericc96778b2014-08-01 01:37:09 +0200314 timers_state.qemu_icount_bias = cur_icount
315 - (timers_state.qemu_icount << icount_time_shift);
Paolo Bonzini17a15f12013-10-03 15:17:25 +0200316 seqlock_write_unlock(&timers_state.vm_clock_seqlock);
Paolo Bonzini946fb272011-09-12 13:57:37 +0200317}
318
319static void icount_adjust_rt(void *opaque)
320{
Alex Bligh40daca52013-08-21 16:03:02 +0100321 timer_mod(icount_rt_timer,
Pavel Dovgalyuk1979b902015-01-12 15:00:43 +0300322 qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL_RT) + 1000);
Paolo Bonzini946fb272011-09-12 13:57:37 +0200323 icount_adjust();
324}
325
326static void icount_adjust_vm(void *opaque)
327{
Alex Bligh40daca52013-08-21 16:03:02 +0100328 timer_mod(icount_vm_timer,
329 qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) +
Rutuja Shah73bcb242016-03-21 21:32:30 +0530330 NANOSECONDS_PER_SECOND / 10);
Paolo Bonzini946fb272011-09-12 13:57:37 +0200331 icount_adjust();
332}
333
334static int64_t qemu_icount_round(int64_t count)
335{
336 return (count + (1 << icount_time_shift) - 1) >> icount_time_shift;
337}
338
Pavel Dovgalyukefab87c2015-09-17 19:24:39 +0300339static void icount_warp_rt(void)
Paolo Bonzini946fb272011-09-12 13:57:37 +0200340{
Alex Bennéeccffff42016-04-04 15:35:48 +0100341 unsigned seq;
342 int64_t warp_start;
343
Paolo Bonzini17a15f12013-10-03 15:17:25 +0200344 /* The icount_warp_timer is rescheduled soon after vm_clock_warp_start
345 * changes from -1 to another value, so the race here is okay.
346 */
Alex Bennéeccffff42016-04-04 15:35:48 +0100347 do {
348 seq = seqlock_read_begin(&timers_state.vm_clock_seqlock);
349 warp_start = vm_clock_warp_start;
350 } while (seqlock_read_retry(&timers_state.vm_clock_seqlock, seq));
351
352 if (warp_start == -1) {
Paolo Bonzini946fb272011-09-12 13:57:37 +0200353 return;
354 }
355
Paolo Bonzini17a15f12013-10-03 15:17:25 +0200356 seqlock_write_lock(&timers_state.vm_clock_seqlock);
Paolo Bonzini946fb272011-09-12 13:57:37 +0200357 if (runstate_is_running()) {
Pavel Dovgalyuk8eda2062015-09-17 19:24:28 +0300358 int64_t clock = REPLAY_CLOCK(REPLAY_CLOCK_VIRTUAL_RT,
359 cpu_get_clock_locked());
Paolo Bonzini8ed961d2013-10-07 17:26:07 +0200360 int64_t warp_delta;
361
362 warp_delta = clock - vm_clock_warp_start;
363 if (use_icount == 2) {
Paolo Bonzini946fb272011-09-12 13:57:37 +0200364 /*
Alex Bligh40daca52013-08-21 16:03:02 +0100365 * In adaptive mode, do not let QEMU_CLOCK_VIRTUAL run too
Paolo Bonzini946fb272011-09-12 13:57:37 +0200366 * far ahead of real time.
367 */
Paolo Bonzini17a15f12013-10-03 15:17:25 +0200368 int64_t cur_icount = cpu_get_icount_locked();
Pavel Dovgalyukbf2a7dd2014-11-26 13:40:55 +0300369 int64_t delta = clock - cur_icount;
Paolo Bonzini8ed961d2013-10-07 17:26:07 +0200370 warp_delta = MIN(warp_delta, delta);
Paolo Bonzini946fb272011-09-12 13:57:37 +0200371 }
KONRAD Fredericc96778b2014-08-01 01:37:09 +0200372 timers_state.qemu_icount_bias += warp_delta;
Paolo Bonzini946fb272011-09-12 13:57:37 +0200373 }
374 vm_clock_warp_start = -1;
Paolo Bonzini17a15f12013-10-03 15:17:25 +0200375 seqlock_write_unlock(&timers_state.vm_clock_seqlock);
Paolo Bonzini8ed961d2013-10-07 17:26:07 +0200376
377 if (qemu_clock_expired(QEMU_CLOCK_VIRTUAL)) {
378 qemu_clock_notify(QEMU_CLOCK_VIRTUAL);
379 }
Paolo Bonzini946fb272011-09-12 13:57:37 +0200380}
381
Pavel Dovgalyuke76d1792016-03-10 14:56:09 +0300382static void icount_timer_cb(void *opaque)
Pavel Dovgalyukefab87c2015-09-17 19:24:39 +0300383{
Pavel Dovgalyuke76d1792016-03-10 14:56:09 +0300384 /* No need for a checkpoint because the timer already synchronizes
385 * with CHECKPOINT_CLOCK_VIRTUAL_RT.
386 */
387 icount_warp_rt();
Pavel Dovgalyukefab87c2015-09-17 19:24:39 +0300388}
389
Paolo Bonzini8156be52012-03-28 15:42:04 +0200390void qtest_clock_warp(int64_t dest)
391{
Alex Bligh40daca52013-08-21 16:03:02 +0100392 int64_t clock = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
Fam Zhengefef88b2015-01-19 17:51:43 +0800393 AioContext *aio_context;
Paolo Bonzini8156be52012-03-28 15:42:04 +0200394 assert(qtest_enabled());
Fam Zhengefef88b2015-01-19 17:51:43 +0800395 aio_context = qemu_get_aio_context();
Paolo Bonzini8156be52012-03-28 15:42:04 +0200396 while (clock < dest) {
Alex Bligh40daca52013-08-21 16:03:02 +0100397 int64_t deadline = qemu_clock_deadline_ns_all(QEMU_CLOCK_VIRTUAL);
Sergey Fedorovc9299e22014-06-10 13:10:28 +0400398 int64_t warp = qemu_soonest_timeout(dest - clock, deadline);
Fam Zhengefef88b2015-01-19 17:51:43 +0800399
Paolo Bonzini17a15f12013-10-03 15:17:25 +0200400 seqlock_write_lock(&timers_state.vm_clock_seqlock);
KONRAD Fredericc96778b2014-08-01 01:37:09 +0200401 timers_state.qemu_icount_bias += warp;
Paolo Bonzini17a15f12013-10-03 15:17:25 +0200402 seqlock_write_unlock(&timers_state.vm_clock_seqlock);
403
Alex Bligh40daca52013-08-21 16:03:02 +0100404 qemu_clock_run_timers(QEMU_CLOCK_VIRTUAL);
Fam Zhengefef88b2015-01-19 17:51:43 +0800405 timerlist_run_timers(aio_context->tlg.tl[QEMU_CLOCK_VIRTUAL]);
Alex Bligh40daca52013-08-21 16:03:02 +0100406 clock = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
Paolo Bonzini8156be52012-03-28 15:42:04 +0200407 }
Alex Bligh40daca52013-08-21 16:03:02 +0100408 qemu_clock_notify(QEMU_CLOCK_VIRTUAL);
Paolo Bonzini8156be52012-03-28 15:42:04 +0200409}
410
Pavel Dovgalyuke76d1792016-03-10 14:56:09 +0300411void qemu_start_warp_timer(void)
Paolo Bonzini946fb272011-09-12 13:57:37 +0200412{
Paolo Bonzinice78d182013-10-07 17:30:02 +0200413 int64_t clock;
Paolo Bonzini946fb272011-09-12 13:57:37 +0200414 int64_t deadline;
415
Pavel Dovgalyuke76d1792016-03-10 14:56:09 +0300416 if (!use_icount) {
Paolo Bonzini946fb272011-09-12 13:57:37 +0200417 return;
418 }
419
Pavel Dovgalyuk8bd7f712015-09-17 19:24:44 +0300420 /* Nothing to do if the VM is stopped: QEMU_CLOCK_VIRTUAL timers
421 * do not fire, so computing the deadline does not make sense.
422 */
423 if (!runstate_is_running()) {
424 return;
425 }
426
427 /* warp clock deterministically in record/replay mode */
Pavel Dovgalyuke76d1792016-03-10 14:56:09 +0300428 if (!replay_checkpoint(CHECKPOINT_CLOCK_WARP_START)) {
Pavel Dovgalyuk8bd7f712015-09-17 19:24:44 +0300429 return;
430 }
431
Paolo Bonzinice78d182013-10-07 17:30:02 +0200432 if (!all_cpu_threads_idle()) {
Paolo Bonzini946fb272011-09-12 13:57:37 +0200433 return;
434 }
435
Paolo Bonzini8156be52012-03-28 15:42:04 +0200436 if (qtest_enabled()) {
437 /* When testing, qtest commands advance icount. */
Pavel Dovgalyuke76d1792016-03-10 14:56:09 +0300438 return;
Paolo Bonzini8156be52012-03-28 15:42:04 +0200439 }
440
Alex Blighac70aaf2013-08-21 16:02:57 +0100441 /* We want to use the earliest deadline from ALL vm_clocks */
Pavel Dovgalyukbf2a7dd2014-11-26 13:40:55 +0300442 clock = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL_RT);
Alex Bligh40daca52013-08-21 16:03:02 +0100443 deadline = qemu_clock_deadline_ns_all(QEMU_CLOCK_VIRTUAL);
Paolo Bonzinice78d182013-10-07 17:30:02 +0200444 if (deadline < 0) {
Victor CLEMENTd7a0f712015-05-29 17:14:06 +0200445 static bool notified;
446 if (!icount_sleep && !notified) {
447 error_report("WARNING: icount sleep disabled and no active timers");
448 notified = true;
449 }
Paolo Bonzinice78d182013-10-07 17:30:02 +0200450 return;
Alex Blighac70aaf2013-08-21 16:02:57 +0100451 }
452
Paolo Bonzini946fb272011-09-12 13:57:37 +0200453 if (deadline > 0) {
454 /*
Alex Bligh40daca52013-08-21 16:03:02 +0100455 * Ensure QEMU_CLOCK_VIRTUAL proceeds even when the virtual CPU goes to
Paolo Bonzini946fb272011-09-12 13:57:37 +0200456 * sleep. Otherwise, the CPU might be waiting for a future timer
457 * interrupt to wake it up, but the interrupt never comes because
458 * the vCPU isn't running any insns and thus doesn't advance the
Alex Bligh40daca52013-08-21 16:03:02 +0100459 * QEMU_CLOCK_VIRTUAL.
Paolo Bonzini946fb272011-09-12 13:57:37 +0200460 */
Victor CLEMENT5045e9d92015-05-29 17:14:04 +0200461 if (!icount_sleep) {
462 /*
463 * We never let VCPUs sleep in no sleep icount mode.
464 * If there is a pending QEMU_CLOCK_VIRTUAL timer we just advance
465 * to the next QEMU_CLOCK_VIRTUAL event and notify it.
466 * It is useful when we want a deterministic execution time,
467 * isolated from host latencies.
468 */
469 seqlock_write_lock(&timers_state.vm_clock_seqlock);
470 timers_state.qemu_icount_bias += deadline;
471 seqlock_write_unlock(&timers_state.vm_clock_seqlock);
472 qemu_clock_notify(QEMU_CLOCK_VIRTUAL);
473 } else {
474 /*
475 * We do stop VCPUs and only advance QEMU_CLOCK_VIRTUAL after some
476 * "real" time, (related to the time left until the next event) has
477 * passed. The QEMU_CLOCK_VIRTUAL_RT clock will do this.
478 * This avoids that the warps are visible externally; for example,
479 * you will not be sending network packets continuously instead of
480 * every 100ms.
481 */
482 seqlock_write_lock(&timers_state.vm_clock_seqlock);
483 if (vm_clock_warp_start == -1 || vm_clock_warp_start > clock) {
484 vm_clock_warp_start = clock;
485 }
486 seqlock_write_unlock(&timers_state.vm_clock_seqlock);
487 timer_mod_anticipate(icount_warp_timer, clock + deadline);
Paolo Bonzinice78d182013-10-07 17:30:02 +0200488 }
Alex Blighac70aaf2013-08-21 16:02:57 +0100489 } else if (deadline == 0) {
Alex Bligh40daca52013-08-21 16:03:02 +0100490 qemu_clock_notify(QEMU_CLOCK_VIRTUAL);
Paolo Bonzini946fb272011-09-12 13:57:37 +0200491 }
492}
493
Pavel Dovgalyuke76d1792016-03-10 14:56:09 +0300494static void qemu_account_warp_timer(void)
495{
496 if (!use_icount || !icount_sleep) {
497 return;
498 }
499
500 /* Nothing to do if the VM is stopped: QEMU_CLOCK_VIRTUAL timers
501 * do not fire, so computing the deadline does not make sense.
502 */
503 if (!runstate_is_running()) {
504 return;
505 }
506
507 /* warp clock deterministically in record/replay mode */
508 if (!replay_checkpoint(CHECKPOINT_CLOCK_WARP_ACCOUNT)) {
509 return;
510 }
511
512 timer_del(icount_warp_timer);
513 icount_warp_rt();
514}
515
KONRAD Fredericd09eae32014-08-01 01:37:10 +0200516static bool icount_state_needed(void *opaque)
517{
518 return use_icount;
519}
520
521/*
522 * This is a subsection for icount migration.
523 */
524static const VMStateDescription icount_vmstate_timers = {
525 .name = "timer/icount",
526 .version_id = 1,
527 .minimum_version_id = 1,
Juan Quintela5cd8cad2014-09-23 14:09:54 +0200528 .needed = icount_state_needed,
KONRAD Fredericd09eae32014-08-01 01:37:10 +0200529 .fields = (VMStateField[]) {
530 VMSTATE_INT64(qemu_icount_bias, TimersState),
531 VMSTATE_INT64(qemu_icount, TimersState),
532 VMSTATE_END_OF_LIST()
533 }
534};
535
Paolo Bonzini946fb272011-09-12 13:57:37 +0200536static const VMStateDescription vmstate_timers = {
537 .name = "timer",
538 .version_id = 2,
539 .minimum_version_id = 1,
Juan Quintela35d08452014-04-16 16:01:33 +0200540 .fields = (VMStateField[]) {
Paolo Bonzini946fb272011-09-12 13:57:37 +0200541 VMSTATE_INT64(cpu_ticks_offset, TimersState),
542 VMSTATE_INT64(dummy, TimersState),
543 VMSTATE_INT64_V(cpu_clock_offset, TimersState, 2),
544 VMSTATE_END_OF_LIST()
KONRAD Fredericd09eae32014-08-01 01:37:10 +0200545 },
Juan Quintela5cd8cad2014-09-23 14:09:54 +0200546 .subsections = (const VMStateDescription*[]) {
547 &icount_vmstate_timers,
548 NULL
Paolo Bonzini946fb272011-09-12 13:57:37 +0200549 }
550};
551
Jason J. Herne2adcc852015-09-08 13:12:33 -0400552static void cpu_throttle_thread(void *opaque)
553{
554 CPUState *cpu = opaque;
555 double pct;
556 double throttle_ratio;
557 long sleeptime_ns;
558
559 if (!cpu_throttle_get_percentage()) {
560 return;
561 }
562
563 pct = (double)cpu_throttle_get_percentage()/100;
564 throttle_ratio = pct / (1 - pct);
565 sleeptime_ns = (long)(throttle_ratio * CPU_THROTTLE_TIMESLICE_NS);
566
567 qemu_mutex_unlock_iothread();
568 atomic_set(&cpu->throttle_thread_scheduled, 0);
569 g_usleep(sleeptime_ns / 1000); /* Convert ns to us for usleep call */
570 qemu_mutex_lock_iothread();
571}
572
573static void cpu_throttle_timer_tick(void *opaque)
574{
575 CPUState *cpu;
576 double pct;
577
578 /* Stop the timer if needed */
579 if (!cpu_throttle_get_percentage()) {
580 return;
581 }
582 CPU_FOREACH(cpu) {
583 if (!atomic_xchg(&cpu->throttle_thread_scheduled, 1)) {
584 async_run_on_cpu(cpu, cpu_throttle_thread, cpu);
585 }
586 }
587
588 pct = (double)cpu_throttle_get_percentage()/100;
589 timer_mod(throttle_timer, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL_RT) +
590 CPU_THROTTLE_TIMESLICE_NS / (1-pct));
591}
592
593void cpu_throttle_set(int new_throttle_pct)
594{
595 /* Ensure throttle percentage is within valid range */
596 new_throttle_pct = MIN(new_throttle_pct, CPU_THROTTLE_PCT_MAX);
597 new_throttle_pct = MAX(new_throttle_pct, CPU_THROTTLE_PCT_MIN);
598
599 atomic_set(&throttle_percentage, new_throttle_pct);
600
601 timer_mod(throttle_timer, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL_RT) +
602 CPU_THROTTLE_TIMESLICE_NS);
603}
604
605void cpu_throttle_stop(void)
606{
607 atomic_set(&throttle_percentage, 0);
608}
609
610bool cpu_throttle_active(void)
611{
612 return (cpu_throttle_get_percentage() != 0);
613}
614
615int cpu_throttle_get_percentage(void)
616{
617 return atomic_read(&throttle_percentage);
618}
619
Pavel Dovgalyuk4603ea02014-09-01 09:34:49 +0400620void cpu_ticks_init(void)
621{
622 seqlock_init(&timers_state.vm_clock_seqlock, NULL);
623 vmstate_register(NULL, 0, &vmstate_timers, &timers_state);
Jason J. Herne2adcc852015-09-08 13:12:33 -0400624 throttle_timer = timer_new_ns(QEMU_CLOCK_VIRTUAL_RT,
625 cpu_throttle_timer_tick, NULL);
Pavel Dovgalyuk4603ea02014-09-01 09:34:49 +0400626}
627
Sebastian Tanase1ad95802014-07-25 11:56:28 +0200628void configure_icount(QemuOpts *opts, Error **errp)
Paolo Bonzini946fb272011-09-12 13:57:37 +0200629{
Sebastian Tanase1ad95802014-07-25 11:56:28 +0200630 const char *option;
Sebastian Tanasea8bfac32014-07-25 11:56:29 +0200631 char *rem_str = NULL;
Sebastian Tanase1ad95802014-07-25 11:56:28 +0200632
Sebastian Tanase1ad95802014-07-25 11:56:28 +0200633 option = qemu_opt_get(opts, "shift");
Paolo Bonzini946fb272011-09-12 13:57:37 +0200634 if (!option) {
Sebastian Tanasea8bfac32014-07-25 11:56:29 +0200635 if (qemu_opt_get(opts, "align") != NULL) {
636 error_setg(errp, "Please specify shift option when using align");
637 }
Paolo Bonzini946fb272011-09-12 13:57:37 +0200638 return;
639 }
Victor CLEMENTf1f4b572015-05-29 17:14:05 +0200640
641 icount_sleep = qemu_opt_get_bool(opts, "sleep", true);
Victor CLEMENT5045e9d92015-05-29 17:14:04 +0200642 if (icount_sleep) {
643 icount_warp_timer = timer_new_ns(QEMU_CLOCK_VIRTUAL_RT,
Pavel Dovgalyuke76d1792016-03-10 14:56:09 +0300644 icount_timer_cb, NULL);
Victor CLEMENT5045e9d92015-05-29 17:14:04 +0200645 }
Victor CLEMENTf1f4b572015-05-29 17:14:05 +0200646
Sebastian Tanasea8bfac32014-07-25 11:56:29 +0200647 icount_align_option = qemu_opt_get_bool(opts, "align", false);
Victor CLEMENTf1f4b572015-05-29 17:14:05 +0200648
649 if (icount_align_option && !icount_sleep) {
Pranith Kumar778d9f92016-02-26 10:16:51 -0500650 error_setg(errp, "align=on and sleep=off are incompatible");
Victor CLEMENTf1f4b572015-05-29 17:14:05 +0200651 }
Paolo Bonzini946fb272011-09-12 13:57:37 +0200652 if (strcmp(option, "auto") != 0) {
Sebastian Tanasea8bfac32014-07-25 11:56:29 +0200653 errno = 0;
654 icount_time_shift = strtol(option, &rem_str, 0);
655 if (errno != 0 || *rem_str != '\0' || !strlen(option)) {
656 error_setg(errp, "icount: Invalid shift value");
657 }
Paolo Bonzini946fb272011-09-12 13:57:37 +0200658 use_icount = 1;
659 return;
Sebastian Tanasea8bfac32014-07-25 11:56:29 +0200660 } else if (icount_align_option) {
661 error_setg(errp, "shift=auto and align=on are incompatible");
Victor CLEMENTf1f4b572015-05-29 17:14:05 +0200662 } else if (!icount_sleep) {
Pranith Kumar778d9f92016-02-26 10:16:51 -0500663 error_setg(errp, "shift=auto and sleep=off are incompatible");
Paolo Bonzini946fb272011-09-12 13:57:37 +0200664 }
665
666 use_icount = 2;
667
668 /* 125MIPS seems a reasonable initial guess at the guest speed.
669 It will be corrected fairly quickly anyway. */
670 icount_time_shift = 3;
671
672 /* Have both realtime and virtual time triggers for speed adjustment.
673 The realtime trigger catches emulated time passing too slowly,
674 the virtual time trigger catches emulated time passing too fast.
675 Realtime triggers occur even when idle, so use them less frequently
676 than VM triggers. */
Pavel Dovgalyukbf2a7dd2014-11-26 13:40:55 +0300677 icount_rt_timer = timer_new_ms(QEMU_CLOCK_VIRTUAL_RT,
678 icount_adjust_rt, NULL);
Alex Bligh40daca52013-08-21 16:03:02 +0100679 timer_mod(icount_rt_timer,
Pavel Dovgalyukbf2a7dd2014-11-26 13:40:55 +0300680 qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL_RT) + 1000);
Alex Bligh40daca52013-08-21 16:03:02 +0100681 icount_vm_timer = timer_new_ns(QEMU_CLOCK_VIRTUAL,
682 icount_adjust_vm, NULL);
683 timer_mod(icount_vm_timer,
684 qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) +
Rutuja Shah73bcb242016-03-21 21:32:30 +0530685 NANOSECONDS_PER_SECOND / 10);
Paolo Bonzini946fb272011-09-12 13:57:37 +0200686}
687
688/***********************************************************/
Blue Swirl296af7c2010-03-29 19:23:50 +0000689void hw_error(const char *fmt, ...)
690{
691 va_list ap;
Andreas Färber55e5c282012-12-17 06:18:02 +0100692 CPUState *cpu;
Blue Swirl296af7c2010-03-29 19:23:50 +0000693
694 va_start(ap, fmt);
695 fprintf(stderr, "qemu: hardware error: ");
696 vfprintf(stderr, fmt, ap);
697 fprintf(stderr, "\n");
Andreas Färberbdc44642013-06-24 23:50:24 +0200698 CPU_FOREACH(cpu) {
Andreas Färber55e5c282012-12-17 06:18:02 +0100699 fprintf(stderr, "CPU #%d:\n", cpu->cpu_index);
Andreas Färber878096e2013-05-27 01:33:50 +0200700 cpu_dump_state(cpu, stderr, fprintf, CPU_DUMP_FPU);
Blue Swirl296af7c2010-03-29 19:23:50 +0000701 }
702 va_end(ap);
703 abort();
704}
705
706void cpu_synchronize_all_states(void)
707{
Andreas Färber182735e2013-05-29 22:29:20 +0200708 CPUState *cpu;
Blue Swirl296af7c2010-03-29 19:23:50 +0000709
Andreas Färberbdc44642013-06-24 23:50:24 +0200710 CPU_FOREACH(cpu) {
Andreas Färber182735e2013-05-29 22:29:20 +0200711 cpu_synchronize_state(cpu);
Blue Swirl296af7c2010-03-29 19:23:50 +0000712 }
713}
714
715void cpu_synchronize_all_post_reset(void)
716{
Andreas Färber182735e2013-05-29 22:29:20 +0200717 CPUState *cpu;
Blue Swirl296af7c2010-03-29 19:23:50 +0000718
Andreas Färberbdc44642013-06-24 23:50:24 +0200719 CPU_FOREACH(cpu) {
Andreas Färber182735e2013-05-29 22:29:20 +0200720 cpu_synchronize_post_reset(cpu);
Blue Swirl296af7c2010-03-29 19:23:50 +0000721 }
722}
723
724void cpu_synchronize_all_post_init(void)
725{
Andreas Färber182735e2013-05-29 22:29:20 +0200726 CPUState *cpu;
Blue Swirl296af7c2010-03-29 19:23:50 +0000727
Andreas Färberbdc44642013-06-24 23:50:24 +0200728 CPU_FOREACH(cpu) {
Andreas Färber182735e2013-05-29 22:29:20 +0200729 cpu_synchronize_post_init(cpu);
Blue Swirl296af7c2010-03-29 19:23:50 +0000730 }
731}
732
Kevin Wolf56983462013-07-05 13:49:54 +0200733static int do_vm_stop(RunState state)
Blue Swirl296af7c2010-03-29 19:23:50 +0000734{
Kevin Wolf56983462013-07-05 13:49:54 +0200735 int ret = 0;
736
Luiz Capitulino13548692011-07-29 15:36:43 -0300737 if (runstate_is_running()) {
Blue Swirl296af7c2010-03-29 19:23:50 +0000738 cpu_disable_ticks();
Blue Swirl296af7c2010-03-29 19:23:50 +0000739 pause_all_vcpus();
Luiz Capitulinof5bbfba2011-07-29 15:04:45 -0300740 runstate_set(state);
Luiz Capitulino1dfb4dd2011-07-29 14:26:33 -0300741 vm_state_notify(0, state);
Wenchao Xiaa4e15de2014-06-18 08:43:36 +0200742 qapi_event_send_stop(&error_abort);
Blue Swirl296af7c2010-03-29 19:23:50 +0000743 }
Kevin Wolf56983462013-07-05 13:49:54 +0200744
Kevin Wolf594a45c2013-07-18 14:52:19 +0200745 bdrv_drain_all();
Max Reitzda31d592016-03-16 19:54:32 +0100746 ret = blk_flush_all();
Kevin Wolf594a45c2013-07-18 14:52:19 +0200747
Kevin Wolf56983462013-07-05 13:49:54 +0200748 return ret;
Blue Swirl296af7c2010-03-29 19:23:50 +0000749}
750
Andreas Färbera1fcaa72012-05-02 23:42:26 +0200751static bool cpu_can_run(CPUState *cpu)
Blue Swirl296af7c2010-03-29 19:23:50 +0000752{
Andreas Färber4fdeee72012-05-02 23:10:09 +0200753 if (cpu->stop) {
Andreas Färbera1fcaa72012-05-02 23:42:26 +0200754 return false;
Jan Kiszka0ab07c62011-02-07 12:19:14 +0100755 }
Tiejun Chen321bc0b2013-08-02 09:43:09 +0800756 if (cpu_is_stopped(cpu)) {
Andreas Färbera1fcaa72012-05-02 23:42:26 +0200757 return false;
Jan Kiszka0ab07c62011-02-07 12:19:14 +0100758 }
Andreas Färbera1fcaa72012-05-02 23:42:26 +0200759 return true;
Blue Swirl296af7c2010-03-29 19:23:50 +0000760}
761
Andreas Färber91325042013-05-27 02:07:49 +0200762static void cpu_handle_guest_debug(CPUState *cpu)
Jan Kiszka3c638d02010-06-25 16:56:56 +0200763{
Andreas Färber64f6b342013-05-27 02:06:09 +0200764 gdb_set_stop_cpu(cpu);
Jan Kiszka8cf71712011-02-07 12:19:16 +0100765 qemu_system_debug_request();
Andreas Färberf324e762012-05-02 23:26:21 +0200766 cpu->stopped = true;
Jan Kiszka3c638d02010-06-25 16:56:56 +0200767}
768
Jan Kiszka6d9cb732011-02-01 22:15:58 +0100769#ifdef CONFIG_LINUX
770static void sigbus_reraise(void)
771{
772 sigset_t set;
773 struct sigaction action;
774
775 memset(&action, 0, sizeof(action));
776 action.sa_handler = SIG_DFL;
777 if (!sigaction(SIGBUS, &action, NULL)) {
778 raise(SIGBUS);
779 sigemptyset(&set);
780 sigaddset(&set, SIGBUS);
781 sigprocmask(SIG_UNBLOCK, &set, NULL);
782 }
783 perror("Failed to re-raise SIGBUS!\n");
784 abort();
785}
786
787static void sigbus_handler(int n, struct qemu_signalfd_siginfo *siginfo,
788 void *ctx)
789{
790 if (kvm_on_sigbus(siginfo->ssi_code,
791 (void *)(intptr_t)siginfo->ssi_addr)) {
792 sigbus_reraise();
793 }
794}
795
796static void qemu_init_sigbus(void)
797{
798 struct sigaction action;
799
800 memset(&action, 0, sizeof(action));
801 action.sa_flags = SA_SIGINFO;
802 action.sa_sigaction = (void (*)(int, siginfo_t*, void*))sigbus_handler;
803 sigaction(SIGBUS, &action, NULL);
804
805 prctl(PR_MCE_KILL, PR_MCE_KILL_SET, PR_MCE_KILL_EARLY, 0, 0);
806}
807
Andreas Färber290adf32013-01-17 09:30:27 +0100808static void qemu_kvm_eat_signals(CPUState *cpu)
Jan Kiszka1ab3c6c2011-03-15 12:26:12 +0100809{
810 struct timespec ts = { 0, 0 };
811 siginfo_t siginfo;
812 sigset_t waitset;
813 sigset_t chkset;
814 int r;
815
816 sigemptyset(&waitset);
817 sigaddset(&waitset, SIG_IPI);
818 sigaddset(&waitset, SIGBUS);
819
820 do {
821 r = sigtimedwait(&waitset, &siginfo, &ts);
822 if (r == -1 && !(errno == EAGAIN || errno == EINTR)) {
823 perror("sigtimedwait");
824 exit(1);
825 }
826
827 switch (r) {
828 case SIGBUS:
Andreas Färber290adf32013-01-17 09:30:27 +0100829 if (kvm_on_sigbus_vcpu(cpu, siginfo.si_code, siginfo.si_addr)) {
Jan Kiszka1ab3c6c2011-03-15 12:26:12 +0100830 sigbus_reraise();
831 }
832 break;
833 default:
834 break;
835 }
836
837 r = sigpending(&chkset);
838 if (r == -1) {
839 perror("sigpending");
840 exit(1);
841 }
842 } while (sigismember(&chkset, SIG_IPI) || sigismember(&chkset, SIGBUS));
Jan Kiszka1ab3c6c2011-03-15 12:26:12 +0100843}
844
Jan Kiszka6d9cb732011-02-01 22:15:58 +0100845#else /* !CONFIG_LINUX */
846
847static void qemu_init_sigbus(void)
848{
849}
Jan Kiszka1ab3c6c2011-03-15 12:26:12 +0100850
Andreas Färber290adf32013-01-17 09:30:27 +0100851static void qemu_kvm_eat_signals(CPUState *cpu)
Jan Kiszka1ab3c6c2011-03-15 12:26:12 +0100852{
853}
Jan Kiszka6d9cb732011-02-01 22:15:58 +0100854#endif /* !CONFIG_LINUX */
855
Blue Swirl296af7c2010-03-29 19:23:50 +0000856#ifndef _WIN32
Jan Kiszka55f8d6a2011-02-01 22:15:52 +0100857static void dummy_signal(int sig)
Blue Swirl296af7c2010-03-29 19:23:50 +0000858{
859}
860
Andreas Färber13618e02013-05-26 23:41:00 +0200861static void qemu_kvm_init_cpu_signals(CPUState *cpu)
Paolo Bonzini714bd042011-03-12 17:44:06 +0100862{
863 int r;
864 sigset_t set;
865 struct sigaction sigact;
866
867 memset(&sigact, 0, sizeof(sigact));
868 sigact.sa_handler = dummy_signal;
869 sigaction(SIG_IPI, &sigact, NULL);
870
Paolo Bonzini714bd042011-03-12 17:44:06 +0100871 pthread_sigmask(SIG_BLOCK, NULL, &set);
872 sigdelset(&set, SIG_IPI);
873 sigdelset(&set, SIGBUS);
Andreas Färber491d6e82013-05-26 23:38:10 +0200874 r = kvm_set_signal_mask(cpu, &set);
Paolo Bonzini714bd042011-03-12 17:44:06 +0100875 if (r) {
876 fprintf(stderr, "kvm_set_signal_mask: %s\n", strerror(-r));
877 exit(1);
878 }
Paolo Bonzini714bd042011-03-12 17:44:06 +0100879}
880
Jan Kiszka55f8d6a2011-02-01 22:15:52 +0100881#else /* _WIN32 */
Andreas Färber13618e02013-05-26 23:41:00 +0200882static void qemu_kvm_init_cpu_signals(CPUState *cpu)
Paolo Bonzini714bd042011-03-12 17:44:06 +0100883{
884 abort();
885}
Jan Kiszka55f8d6a2011-02-01 22:15:52 +0100886#endif /* _WIN32 */
Blue Swirl296af7c2010-03-29 19:23:50 +0000887
Stefan Weilb2532d82012-09-27 07:41:42 +0200888static QemuMutex qemu_global_mutex;
Paolo Bonzini46daff12011-06-09 13:10:24 +0200889static QemuCond qemu_io_proceeded_cond;
Paolo Bonzini6b498092015-02-27 19:58:23 +0100890static unsigned iothread_requesting_mutex;
Blue Swirl296af7c2010-03-29 19:23:50 +0000891
892static QemuThread io_thread;
893
Blue Swirl296af7c2010-03-29 19:23:50 +0000894/* cpu creation */
895static QemuCond qemu_cpu_cond;
896/* system init */
Blue Swirl296af7c2010-03-29 19:23:50 +0000897static QemuCond qemu_pause_cond;
Marcelo Tosattie82bcec2010-05-04 09:45:22 -0300898static QemuCond qemu_work_cond;
Blue Swirl296af7c2010-03-29 19:23:50 +0000899
Paolo Bonzinid3b12f52011-09-13 10:30:52 +0200900void qemu_init_cpu_loop(void)
Blue Swirl296af7c2010-03-29 19:23:50 +0000901{
Jan Kiszka6d9cb732011-02-01 22:15:58 +0100902 qemu_init_sigbus();
Anthony Liguoried945922011-02-08 18:18:18 +0100903 qemu_cond_init(&qemu_cpu_cond);
Anthony Liguoried945922011-02-08 18:18:18 +0100904 qemu_cond_init(&qemu_pause_cond);
905 qemu_cond_init(&qemu_work_cond);
Paolo Bonzini46daff12011-06-09 13:10:24 +0200906 qemu_cond_init(&qemu_io_proceeded_cond);
Blue Swirl296af7c2010-03-29 19:23:50 +0000907 qemu_mutex_init(&qemu_global_mutex);
Blue Swirl296af7c2010-03-29 19:23:50 +0000908
Jan Kiszkab7680cb2011-03-12 17:43:51 +0100909 qemu_thread_get_self(&io_thread);
Blue Swirl296af7c2010-03-29 19:23:50 +0000910}
911
Andreas Färberf100f0b2012-05-03 14:58:47 +0200912void run_on_cpu(CPUState *cpu, void (*func)(void *data), void *data)
Marcelo Tosattie82bcec2010-05-04 09:45:22 -0300913{
914 struct qemu_work_item wi;
915
Andreas Färber60e82572012-05-02 22:23:49 +0200916 if (qemu_cpu_is_self(cpu)) {
Marcelo Tosattie82bcec2010-05-04 09:45:22 -0300917 func(data);
918 return;
919 }
920
921 wi.func = func;
922 wi.data = data;
Chegu Vinod3c022702013-06-24 03:49:41 -0600923 wi.free = false;
Paolo Bonzini376692b2015-07-10 12:32:32 +0200924
925 qemu_mutex_lock(&cpu->work_mutex);
Andreas Färberc64ca812012-05-03 02:11:45 +0200926 if (cpu->queued_work_first == NULL) {
927 cpu->queued_work_first = &wi;
Jan Kiszka0ab07c62011-02-07 12:19:14 +0100928 } else {
Andreas Färberc64ca812012-05-03 02:11:45 +0200929 cpu->queued_work_last->next = &wi;
Jan Kiszka0ab07c62011-02-07 12:19:14 +0100930 }
Andreas Färberc64ca812012-05-03 02:11:45 +0200931 cpu->queued_work_last = &wi;
Marcelo Tosattie82bcec2010-05-04 09:45:22 -0300932 wi.next = NULL;
933 wi.done = false;
Paolo Bonzini376692b2015-07-10 12:32:32 +0200934 qemu_mutex_unlock(&cpu->work_mutex);
Marcelo Tosattie82bcec2010-05-04 09:45:22 -0300935
Andreas Färberc08d7422012-05-03 04:34:15 +0200936 qemu_cpu_kick(cpu);
Paolo Bonzini376692b2015-07-10 12:32:32 +0200937 while (!atomic_mb_read(&wi.done)) {
Andreas Färber4917cf42013-05-27 05:17:50 +0200938 CPUState *self_cpu = current_cpu;
Marcelo Tosattie82bcec2010-05-04 09:45:22 -0300939
940 qemu_cond_wait(&qemu_work_cond, &qemu_global_mutex);
Andreas Färber4917cf42013-05-27 05:17:50 +0200941 current_cpu = self_cpu;
Marcelo Tosattie82bcec2010-05-04 09:45:22 -0300942 }
943}
944
Chegu Vinod3c022702013-06-24 03:49:41 -0600945void async_run_on_cpu(CPUState *cpu, void (*func)(void *data), void *data)
946{
947 struct qemu_work_item *wi;
948
949 if (qemu_cpu_is_self(cpu)) {
950 func(data);
951 return;
952 }
953
954 wi = g_malloc0(sizeof(struct qemu_work_item));
955 wi->func = func;
956 wi->data = data;
957 wi->free = true;
Paolo Bonzini376692b2015-07-10 12:32:32 +0200958
959 qemu_mutex_lock(&cpu->work_mutex);
Chegu Vinod3c022702013-06-24 03:49:41 -0600960 if (cpu->queued_work_first == NULL) {
961 cpu->queued_work_first = wi;
962 } else {
963 cpu->queued_work_last->next = wi;
964 }
965 cpu->queued_work_last = wi;
966 wi->next = NULL;
967 wi->done = false;
Paolo Bonzini376692b2015-07-10 12:32:32 +0200968 qemu_mutex_unlock(&cpu->work_mutex);
Chegu Vinod3c022702013-06-24 03:49:41 -0600969
970 qemu_cpu_kick(cpu);
971}
972
Andreas Färber6d45b102012-05-03 02:13:22 +0200973static void flush_queued_work(CPUState *cpu)
Marcelo Tosattie82bcec2010-05-04 09:45:22 -0300974{
975 struct qemu_work_item *wi;
976
Andreas Färberc64ca812012-05-03 02:11:45 +0200977 if (cpu->queued_work_first == NULL) {
Marcelo Tosattie82bcec2010-05-04 09:45:22 -0300978 return;
Jan Kiszka0ab07c62011-02-07 12:19:14 +0100979 }
Marcelo Tosattie82bcec2010-05-04 09:45:22 -0300980
Paolo Bonzini376692b2015-07-10 12:32:32 +0200981 qemu_mutex_lock(&cpu->work_mutex);
982 while (cpu->queued_work_first != NULL) {
983 wi = cpu->queued_work_first;
Andreas Färberc64ca812012-05-03 02:11:45 +0200984 cpu->queued_work_first = wi->next;
Paolo Bonzini376692b2015-07-10 12:32:32 +0200985 if (!cpu->queued_work_first) {
986 cpu->queued_work_last = NULL;
987 }
988 qemu_mutex_unlock(&cpu->work_mutex);
Marcelo Tosattie82bcec2010-05-04 09:45:22 -0300989 wi->func(wi->data);
Paolo Bonzini376692b2015-07-10 12:32:32 +0200990 qemu_mutex_lock(&cpu->work_mutex);
Chegu Vinod3c022702013-06-24 03:49:41 -0600991 if (wi->free) {
992 g_free(wi);
Paolo Bonzini376692b2015-07-10 12:32:32 +0200993 } else {
994 atomic_mb_set(&wi->done, true);
Chegu Vinod3c022702013-06-24 03:49:41 -0600995 }
Marcelo Tosattie82bcec2010-05-04 09:45:22 -0300996 }
Paolo Bonzini376692b2015-07-10 12:32:32 +0200997 qemu_mutex_unlock(&cpu->work_mutex);
Marcelo Tosattie82bcec2010-05-04 09:45:22 -0300998 qemu_cond_broadcast(&qemu_work_cond);
999}
1000
Andreas Färber509a0d72012-05-03 02:18:09 +02001001static void qemu_wait_io_event_common(CPUState *cpu)
Blue Swirl296af7c2010-03-29 19:23:50 +00001002{
Andreas Färber4fdeee72012-05-02 23:10:09 +02001003 if (cpu->stop) {
1004 cpu->stop = false;
Andreas Färberf324e762012-05-02 23:26:21 +02001005 cpu->stopped = true;
Dr. David Alan Gilbert96bce682016-01-25 10:08:18 +00001006 qemu_cond_broadcast(&qemu_pause_cond);
Blue Swirl296af7c2010-03-29 19:23:50 +00001007 }
Andreas Färber6d45b102012-05-03 02:13:22 +02001008 flush_queued_work(cpu);
Andreas Färber216fc9a2012-05-02 17:49:49 +02001009 cpu->thread_kicked = false;
Blue Swirl296af7c2010-03-29 19:23:50 +00001010}
1011
KONRAD Fredericd5f8d612015-08-10 17:27:06 +02001012static void qemu_tcg_wait_io_event(CPUState *cpu)
Blue Swirl296af7c2010-03-29 19:23:50 +00001013{
Jan Kiszka16400322011-02-09 16:29:37 +01001014 while (all_cpu_threads_idle()) {
KONRAD Fredericd5f8d612015-08-10 17:27:06 +02001015 qemu_cond_wait(cpu->halt_cond, &qemu_global_mutex);
Jan Kiszka16400322011-02-09 16:29:37 +01001016 }
Blue Swirl296af7c2010-03-29 19:23:50 +00001017
Paolo Bonzini46daff12011-06-09 13:10:24 +02001018 while (iothread_requesting_mutex) {
1019 qemu_cond_wait(&qemu_io_proceeded_cond, &qemu_global_mutex);
1020 }
Jan Kiszka6cabe1f2010-06-25 16:56:53 +02001021
Andreas Färberbdc44642013-06-24 23:50:24 +02001022 CPU_FOREACH(cpu) {
Andreas Färber182735e2013-05-29 22:29:20 +02001023 qemu_wait_io_event_common(cpu);
Jan Kiszka6cabe1f2010-06-25 16:56:53 +02001024 }
Blue Swirl296af7c2010-03-29 19:23:50 +00001025}
1026
Andreas Färberfd529e82013-05-26 23:24:55 +02001027static void qemu_kvm_wait_io_event(CPUState *cpu)
Blue Swirl296af7c2010-03-29 19:23:50 +00001028{
Andreas Färbera98ae1d2013-05-26 23:21:08 +02001029 while (cpu_thread_is_idle(cpu)) {
Andreas Färberf5c121b2012-05-03 01:22:49 +02001030 qemu_cond_wait(cpu->halt_cond, &qemu_global_mutex);
Jan Kiszka16400322011-02-09 16:29:37 +01001031 }
Blue Swirl296af7c2010-03-29 19:23:50 +00001032
Andreas Färber290adf32013-01-17 09:30:27 +01001033 qemu_kvm_eat_signals(cpu);
Andreas Färber509a0d72012-05-03 02:18:09 +02001034 qemu_wait_io_event_common(cpu);
Blue Swirl296af7c2010-03-29 19:23:50 +00001035}
1036
Jan Kiszka7e97cd82011-02-07 12:19:12 +01001037static void *qemu_kvm_cpu_thread_fn(void *arg)
Blue Swirl296af7c2010-03-29 19:23:50 +00001038{
Andreas Färber48a106b2013-05-27 02:20:39 +02001039 CPUState *cpu = arg;
Jan Kiszka84b49152011-02-01 22:15:50 +01001040 int r;
Blue Swirl296af7c2010-03-29 19:23:50 +00001041
Paolo Bonziniab28bd22015-07-09 08:55:38 +02001042 rcu_register_thread();
1043
Paolo Bonzini2e7f7a32015-06-18 18:47:18 +02001044 qemu_mutex_lock_iothread();
Andreas Färber814e6122012-05-02 17:00:37 +02001045 qemu_thread_get_self(cpu->thread);
Andreas Färber9f09e182012-05-03 06:59:07 +02001046 cpu->thread_id = qemu_get_thread_id();
Pavel Dovgalyuk626cf8f2014-12-08 10:53:17 +03001047 cpu->can_do_io = 1;
Andreas Färber4917cf42013-05-27 05:17:50 +02001048 current_cpu = cpu;
Blue Swirl296af7c2010-03-29 19:23:50 +00001049
Andreas Färber504134d2012-12-17 06:38:45 +01001050 r = kvm_init_vcpu(cpu);
Jan Kiszka84b49152011-02-01 22:15:50 +01001051 if (r < 0) {
1052 fprintf(stderr, "kvm_init_vcpu failed: %s\n", strerror(-r));
1053 exit(1);
1054 }
Blue Swirl296af7c2010-03-29 19:23:50 +00001055
Andreas Färber13618e02013-05-26 23:41:00 +02001056 qemu_kvm_init_cpu_signals(cpu);
Blue Swirl296af7c2010-03-29 19:23:50 +00001057
1058 /* signal CPU creation */
Andreas Färber61a46212012-05-02 22:49:36 +02001059 cpu->created = true;
Blue Swirl296af7c2010-03-29 19:23:50 +00001060 qemu_cond_signal(&qemu_cpu_cond);
1061
Blue Swirl296af7c2010-03-29 19:23:50 +00001062 while (1) {
Andreas Färbera1fcaa72012-05-02 23:42:26 +02001063 if (cpu_can_run(cpu)) {
Andreas Färber1458c362013-05-26 23:46:55 +02001064 r = kvm_cpu_exec(cpu);
Jan Kiszka83f338f2011-02-07 12:19:17 +01001065 if (r == EXCP_DEBUG) {
Andreas Färber91325042013-05-27 02:07:49 +02001066 cpu_handle_guest_debug(cpu);
Jan Kiszka83f338f2011-02-07 12:19:17 +01001067 }
Jan Kiszka0ab07c62011-02-07 12:19:14 +01001068 }
Andreas Färberfd529e82013-05-26 23:24:55 +02001069 qemu_kvm_wait_io_event(cpu);
Blue Swirl296af7c2010-03-29 19:23:50 +00001070 }
1071
1072 return NULL;
1073}
1074
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +02001075static void *qemu_dummy_cpu_thread_fn(void *arg)
1076{
1077#ifdef _WIN32
1078 fprintf(stderr, "qtest is not supported under Windows\n");
1079 exit(1);
1080#else
Andreas Färber10a90212013-05-27 02:24:35 +02001081 CPUState *cpu = arg;
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +02001082 sigset_t waitset;
1083 int r;
1084
Paolo Bonziniab28bd22015-07-09 08:55:38 +02001085 rcu_register_thread();
1086
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +02001087 qemu_mutex_lock_iothread();
Andreas Färber814e6122012-05-02 17:00:37 +02001088 qemu_thread_get_self(cpu->thread);
Andreas Färber9f09e182012-05-03 06:59:07 +02001089 cpu->thread_id = qemu_get_thread_id();
Pavel Dovgalyuk626cf8f2014-12-08 10:53:17 +03001090 cpu->can_do_io = 1;
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +02001091
1092 sigemptyset(&waitset);
1093 sigaddset(&waitset, SIG_IPI);
1094
1095 /* signal CPU creation */
Andreas Färber61a46212012-05-02 22:49:36 +02001096 cpu->created = true;
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +02001097 qemu_cond_signal(&qemu_cpu_cond);
1098
Andreas Färber4917cf42013-05-27 05:17:50 +02001099 current_cpu = cpu;
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +02001100 while (1) {
Andreas Färber4917cf42013-05-27 05:17:50 +02001101 current_cpu = NULL;
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +02001102 qemu_mutex_unlock_iothread();
1103 do {
1104 int sig;
1105 r = sigwait(&waitset, &sig);
1106 } while (r == -1 && (errno == EAGAIN || errno == EINTR));
1107 if (r == -1) {
1108 perror("sigwait");
1109 exit(1);
1110 }
1111 qemu_mutex_lock_iothread();
Andreas Färber4917cf42013-05-27 05:17:50 +02001112 current_cpu = cpu;
Andreas Färber509a0d72012-05-03 02:18:09 +02001113 qemu_wait_io_event_common(cpu);
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +02001114 }
1115
1116 return NULL;
1117#endif
1118}
1119
Jan Kiszkabdb7ca62011-09-26 09:40:39 +02001120static void tcg_exec_all(void);
1121
Jan Kiszka7e97cd82011-02-07 12:19:12 +01001122static void *qemu_tcg_cpu_thread_fn(void *arg)
Blue Swirl296af7c2010-03-29 19:23:50 +00001123{
Andreas Färberc3586ba2012-05-03 01:41:24 +02001124 CPUState *cpu = arg;
Blue Swirl296af7c2010-03-29 19:23:50 +00001125
Paolo Bonziniab28bd22015-07-09 08:55:38 +02001126 rcu_register_thread();
1127
Paolo Bonzini2e7f7a32015-06-18 18:47:18 +02001128 qemu_mutex_lock_iothread();
Andreas Färber814e6122012-05-02 17:00:37 +02001129 qemu_thread_get_self(cpu->thread);
Blue Swirl296af7c2010-03-29 19:23:50 +00001130
Andreas Färber38fcbd32013-07-07 19:50:23 +02001131 CPU_FOREACH(cpu) {
1132 cpu->thread_id = qemu_get_thread_id();
1133 cpu->created = true;
Pavel Dovgalyuk626cf8f2014-12-08 10:53:17 +03001134 cpu->can_do_io = 1;
Andreas Färber38fcbd32013-07-07 19:50:23 +02001135 }
Blue Swirl296af7c2010-03-29 19:23:50 +00001136 qemu_cond_signal(&qemu_cpu_cond);
1137
Jan Kiszkafa7d1862011-08-22 18:35:25 +02001138 /* wait for initial kick-off after machine start */
Emilio G. Cotac28e3992015-04-27 12:45:28 -04001139 while (first_cpu->stopped) {
KONRAD Fredericd5f8d612015-08-10 17:27:06 +02001140 qemu_cond_wait(first_cpu->halt_cond, &qemu_global_mutex);
Jan Kiszka8e564b42012-02-17 18:31:15 +01001141
1142 /* process any pending work */
Andreas Färberbdc44642013-06-24 23:50:24 +02001143 CPU_FOREACH(cpu) {
Andreas Färber182735e2013-05-29 22:29:20 +02001144 qemu_wait_io_event_common(cpu);
Jan Kiszka8e564b42012-02-17 18:31:15 +01001145 }
Jan Kiszka0ab07c62011-02-07 12:19:14 +01001146 }
Blue Swirl296af7c2010-03-29 19:23:50 +00001147
Paolo Bonzini21618b32015-02-27 20:01:03 +01001148 /* process any pending work */
Paolo Bonziniaed807c2015-08-18 06:43:15 -07001149 atomic_mb_set(&exit_request, 1);
Paolo Bonzini21618b32015-02-27 20:01:03 +01001150
Blue Swirl296af7c2010-03-29 19:23:50 +00001151 while (1) {
Jan Kiszkabdb7ca62011-09-26 09:40:39 +02001152 tcg_exec_all();
Alex Blighac70aaf2013-08-21 16:02:57 +01001153
1154 if (use_icount) {
Alex Bligh40daca52013-08-21 16:03:02 +01001155 int64_t deadline = qemu_clock_deadline_ns_all(QEMU_CLOCK_VIRTUAL);
Alex Blighac70aaf2013-08-21 16:02:57 +01001156
1157 if (deadline == 0) {
Alex Bligh40daca52013-08-21 16:03:02 +01001158 qemu_clock_notify(QEMU_CLOCK_VIRTUAL);
Alex Blighac70aaf2013-08-21 16:02:57 +01001159 }
Paolo Bonzini3b2319a2011-04-13 10:03:43 +02001160 }
KONRAD Fredericd5f8d612015-08-10 17:27:06 +02001161 qemu_tcg_wait_io_event(QTAILQ_FIRST(&cpus));
Blue Swirl296af7c2010-03-29 19:23:50 +00001162 }
1163
1164 return NULL;
1165}
1166
Andreas Färber2ff09a42012-05-03 00:23:30 +02001167static void qemu_cpu_kick_thread(CPUState *cpu)
Paolo Bonzinicc015e92011-03-12 17:44:08 +01001168{
1169#ifndef _WIN32
1170 int err;
1171
Paolo Bonzinie0c38212015-08-26 00:19:19 +02001172 if (cpu->thread_kicked) {
1173 return;
Paolo Bonzini9102ded2015-08-18 06:52:09 -07001174 }
Paolo Bonzinie0c38212015-08-26 00:19:19 +02001175 cpu->thread_kicked = true;
Andreas Färber814e6122012-05-02 17:00:37 +02001176 err = pthread_kill(cpu->thread->thread, SIG_IPI);
Paolo Bonzinicc015e92011-03-12 17:44:08 +01001177 if (err) {
1178 fprintf(stderr, "qemu:%s: %s", __func__, strerror(err));
1179 exit(1);
1180 }
1181#else /* _WIN32 */
Paolo Bonzinie0c38212015-08-26 00:19:19 +02001182 abort();
Paolo Bonzinicc015e92011-03-12 17:44:08 +01001183#endif
1184}
1185
Paolo Bonzinie0c38212015-08-26 00:19:19 +02001186static void qemu_cpu_kick_no_halt(void)
1187{
1188 CPUState *cpu;
1189 /* Ensure whatever caused the exit has reached the CPU threads before
1190 * writing exit_request.
1191 */
1192 atomic_mb_set(&exit_request, 1);
1193 cpu = atomic_mb_read(&tcg_current_cpu);
1194 if (cpu) {
1195 cpu_exit(cpu);
1196 }
1197}
1198
Andreas Färberc08d7422012-05-03 04:34:15 +02001199void qemu_cpu_kick(CPUState *cpu)
Blue Swirl296af7c2010-03-29 19:23:50 +00001200{
Andreas Färberf5c121b2012-05-03 01:22:49 +02001201 qemu_cond_broadcast(cpu->halt_cond);
Paolo Bonzinie0c38212015-08-26 00:19:19 +02001202 if (tcg_enabled()) {
1203 qemu_cpu_kick_no_halt();
1204 } else {
1205 qemu_cpu_kick_thread(cpu);
1206 }
Blue Swirl296af7c2010-03-29 19:23:50 +00001207}
1208
Jan Kiszka46d62fa2011-02-01 22:15:59 +01001209void qemu_cpu_kick_self(void)
1210{
Andreas Färber4917cf42013-05-27 05:17:50 +02001211 assert(current_cpu);
Paolo Bonzini9102ded2015-08-18 06:52:09 -07001212 qemu_cpu_kick_thread(current_cpu);
Blue Swirl296af7c2010-03-29 19:23:50 +00001213}
1214
Andreas Färber60e82572012-05-02 22:23:49 +02001215bool qemu_cpu_is_self(CPUState *cpu)
Blue Swirl296af7c2010-03-29 19:23:50 +00001216{
Andreas Färber814e6122012-05-02 17:00:37 +02001217 return qemu_thread_is_self(cpu->thread);
Blue Swirl296af7c2010-03-29 19:23:50 +00001218}
1219
Paolo Bonzini79e2b9a2015-01-21 12:09:14 +01001220bool qemu_in_vcpu_thread(void)
Juan Quintelaaa723c22012-09-18 16:30:11 +02001221{
Andreas Färber4917cf42013-05-27 05:17:50 +02001222 return current_cpu && qemu_cpu_is_self(current_cpu);
Juan Quintelaaa723c22012-09-18 16:30:11 +02001223}
1224
Paolo Bonziniafbe7052015-06-18 18:47:19 +02001225static __thread bool iothread_locked = false;
1226
1227bool qemu_mutex_iothread_locked(void)
1228{
1229 return iothread_locked;
1230}
1231
Blue Swirl296af7c2010-03-29 19:23:50 +00001232void qemu_mutex_lock_iothread(void)
1233{
Paolo Bonzini21618b32015-02-27 20:01:03 +01001234 atomic_inc(&iothread_requesting_mutex);
Paolo Bonzini2e7f7a32015-06-18 18:47:18 +02001235 /* In the simple case there is no need to bump the VCPU thread out of
1236 * TCG code execution.
1237 */
1238 if (!tcg_enabled() || qemu_in_vcpu_thread() ||
Aníbal Limón46036b22015-09-03 15:48:33 -05001239 !first_cpu || !first_cpu->created) {
Blue Swirl296af7c2010-03-29 19:23:50 +00001240 qemu_mutex_lock(&qemu_global_mutex);
Paolo Bonzini21618b32015-02-27 20:01:03 +01001241 atomic_dec(&iothread_requesting_mutex);
Marcelo Tosatti1a28cac2010-05-04 09:45:20 -03001242 } else {
Marcelo Tosatti1a28cac2010-05-04 09:45:20 -03001243 if (qemu_mutex_trylock(&qemu_global_mutex)) {
Paolo Bonzinie0c38212015-08-26 00:19:19 +02001244 qemu_cpu_kick_no_halt();
Marcelo Tosatti1a28cac2010-05-04 09:45:20 -03001245 qemu_mutex_lock(&qemu_global_mutex);
1246 }
Paolo Bonzini6b498092015-02-27 19:58:23 +01001247 atomic_dec(&iothread_requesting_mutex);
Paolo Bonzini46daff12011-06-09 13:10:24 +02001248 qemu_cond_broadcast(&qemu_io_proceeded_cond);
Marcelo Tosatti1a28cac2010-05-04 09:45:20 -03001249 }
Paolo Bonziniafbe7052015-06-18 18:47:19 +02001250 iothread_locked = true;
Blue Swirl296af7c2010-03-29 19:23:50 +00001251}
1252
1253void qemu_mutex_unlock_iothread(void)
1254{
Paolo Bonziniafbe7052015-06-18 18:47:19 +02001255 iothread_locked = false;
Blue Swirl296af7c2010-03-29 19:23:50 +00001256 qemu_mutex_unlock(&qemu_global_mutex);
1257}
1258
1259static int all_vcpus_paused(void)
1260{
Andreas Färberbdc44642013-06-24 23:50:24 +02001261 CPUState *cpu;
Blue Swirl296af7c2010-03-29 19:23:50 +00001262
Andreas Färberbdc44642013-06-24 23:50:24 +02001263 CPU_FOREACH(cpu) {
Andreas Färber182735e2013-05-29 22:29:20 +02001264 if (!cpu->stopped) {
Blue Swirl296af7c2010-03-29 19:23:50 +00001265 return 0;
Jan Kiszka0ab07c62011-02-07 12:19:14 +01001266 }
Blue Swirl296af7c2010-03-29 19:23:50 +00001267 }
1268
1269 return 1;
1270}
1271
1272void pause_all_vcpus(void)
1273{
Andreas Färberbdc44642013-06-24 23:50:24 +02001274 CPUState *cpu;
Blue Swirl296af7c2010-03-29 19:23:50 +00001275
Alex Bligh40daca52013-08-21 16:03:02 +01001276 qemu_clock_enable(QEMU_CLOCK_VIRTUAL, false);
Andreas Färberbdc44642013-06-24 23:50:24 +02001277 CPU_FOREACH(cpu) {
Andreas Färber182735e2013-05-29 22:29:20 +02001278 cpu->stop = true;
1279 qemu_cpu_kick(cpu);
Blue Swirl296af7c2010-03-29 19:23:50 +00001280 }
1281
Juan Quintelaaa723c22012-09-18 16:30:11 +02001282 if (qemu_in_vcpu_thread()) {
Jan Kiszkad798e972012-02-17 18:31:16 +01001283 cpu_stop_current();
1284 if (!kvm_enabled()) {
Andreas Färberbdc44642013-06-24 23:50:24 +02001285 CPU_FOREACH(cpu) {
Andreas Färber182735e2013-05-29 22:29:20 +02001286 cpu->stop = false;
1287 cpu->stopped = true;
Jan Kiszkad798e972012-02-17 18:31:16 +01001288 }
1289 return;
1290 }
1291 }
1292
Blue Swirl296af7c2010-03-29 19:23:50 +00001293 while (!all_vcpus_paused()) {
Paolo Bonzinibe7d6c52011-03-12 17:44:02 +01001294 qemu_cond_wait(&qemu_pause_cond, &qemu_global_mutex);
Andreas Färberbdc44642013-06-24 23:50:24 +02001295 CPU_FOREACH(cpu) {
Andreas Färber182735e2013-05-29 22:29:20 +02001296 qemu_cpu_kick(cpu);
Blue Swirl296af7c2010-03-29 19:23:50 +00001297 }
1298 }
1299}
1300
Igor Mammedov29936832013-04-23 10:29:37 +02001301void cpu_resume(CPUState *cpu)
1302{
1303 cpu->stop = false;
1304 cpu->stopped = false;
1305 qemu_cpu_kick(cpu);
1306}
1307
Blue Swirl296af7c2010-03-29 19:23:50 +00001308void resume_all_vcpus(void)
1309{
Andreas Färberbdc44642013-06-24 23:50:24 +02001310 CPUState *cpu;
Blue Swirl296af7c2010-03-29 19:23:50 +00001311
Alex Bligh40daca52013-08-21 16:03:02 +01001312 qemu_clock_enable(QEMU_CLOCK_VIRTUAL, true);
Andreas Färberbdc44642013-06-24 23:50:24 +02001313 CPU_FOREACH(cpu) {
Andreas Färber182735e2013-05-29 22:29:20 +02001314 cpu_resume(cpu);
Blue Swirl296af7c2010-03-29 19:23:50 +00001315 }
1316}
1317
Dr. David Alan Gilbert49001162014-01-30 10:20:32 +00001318/* For temporary buffers for forming a name */
1319#define VCPU_THREAD_NAME_SIZE 16
1320
Andreas Färbere5ab30a2012-05-03 01:50:44 +02001321static void qemu_tcg_init_vcpu(CPUState *cpu)
Blue Swirl296af7c2010-03-29 19:23:50 +00001322{
Dr. David Alan Gilbert49001162014-01-30 10:20:32 +00001323 char thread_name[VCPU_THREAD_NAME_SIZE];
KONRAD Fredericd5f8d612015-08-10 17:27:06 +02001324 static QemuCond *tcg_halt_cond;
1325 static QemuThread *tcg_cpu_thread;
Dr. David Alan Gilbert49001162014-01-30 10:20:32 +00001326
Blue Swirl296af7c2010-03-29 19:23:50 +00001327 /* share a single thread for all cpus with TCG */
1328 if (!tcg_cpu_thread) {
Andreas Färber814e6122012-05-02 17:00:37 +02001329 cpu->thread = g_malloc0(sizeof(QemuThread));
Andreas Färberf5c121b2012-05-03 01:22:49 +02001330 cpu->halt_cond = g_malloc0(sizeof(QemuCond));
1331 qemu_cond_init(cpu->halt_cond);
1332 tcg_halt_cond = cpu->halt_cond;
Dr. David Alan Gilbert49001162014-01-30 10:20:32 +00001333 snprintf(thread_name, VCPU_THREAD_NAME_SIZE, "CPU %d/TCG",
1334 cpu->cpu_index);
1335 qemu_thread_create(cpu->thread, thread_name, qemu_tcg_cpu_thread_fn,
1336 cpu, QEMU_THREAD_JOINABLE);
Paolo Bonzini1ecf47b2011-12-13 13:43:52 +01001337#ifdef _WIN32
Andreas Färber814e6122012-05-02 17:00:37 +02001338 cpu->hThread = qemu_thread_get_handle(cpu->thread);
Paolo Bonzini1ecf47b2011-12-13 13:43:52 +01001339#endif
Andreas Färber61a46212012-05-02 22:49:36 +02001340 while (!cpu->created) {
Paolo Bonzini18a85722011-03-12 17:44:03 +01001341 qemu_cond_wait(&qemu_cpu_cond, &qemu_global_mutex);
Jan Kiszka0ab07c62011-02-07 12:19:14 +01001342 }
Andreas Färber814e6122012-05-02 17:00:37 +02001343 tcg_cpu_thread = cpu->thread;
Blue Swirl296af7c2010-03-29 19:23:50 +00001344 } else {
Andreas Färber814e6122012-05-02 17:00:37 +02001345 cpu->thread = tcg_cpu_thread;
Andreas Färberf5c121b2012-05-03 01:22:49 +02001346 cpu->halt_cond = tcg_halt_cond;
Blue Swirl296af7c2010-03-29 19:23:50 +00001347 }
1348}
1349
Andreas Färber48a106b2013-05-27 02:20:39 +02001350static void qemu_kvm_start_vcpu(CPUState *cpu)
Blue Swirl296af7c2010-03-29 19:23:50 +00001351{
Dr. David Alan Gilbert49001162014-01-30 10:20:32 +00001352 char thread_name[VCPU_THREAD_NAME_SIZE];
1353
Andreas Färber814e6122012-05-02 17:00:37 +02001354 cpu->thread = g_malloc0(sizeof(QemuThread));
Andreas Färberf5c121b2012-05-03 01:22:49 +02001355 cpu->halt_cond = g_malloc0(sizeof(QemuCond));
1356 qemu_cond_init(cpu->halt_cond);
Dr. David Alan Gilbert49001162014-01-30 10:20:32 +00001357 snprintf(thread_name, VCPU_THREAD_NAME_SIZE, "CPU %d/KVM",
1358 cpu->cpu_index);
1359 qemu_thread_create(cpu->thread, thread_name, qemu_kvm_cpu_thread_fn,
1360 cpu, QEMU_THREAD_JOINABLE);
Andreas Färber61a46212012-05-02 22:49:36 +02001361 while (!cpu->created) {
Paolo Bonzini18a85722011-03-12 17:44:03 +01001362 qemu_cond_wait(&qemu_cpu_cond, &qemu_global_mutex);
Jan Kiszka0ab07c62011-02-07 12:19:14 +01001363 }
Blue Swirl296af7c2010-03-29 19:23:50 +00001364}
1365
Andreas Färber10a90212013-05-27 02:24:35 +02001366static void qemu_dummy_start_vcpu(CPUState *cpu)
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +02001367{
Dr. David Alan Gilbert49001162014-01-30 10:20:32 +00001368 char thread_name[VCPU_THREAD_NAME_SIZE];
1369
Andreas Färber814e6122012-05-02 17:00:37 +02001370 cpu->thread = g_malloc0(sizeof(QemuThread));
Andreas Färberf5c121b2012-05-03 01:22:49 +02001371 cpu->halt_cond = g_malloc0(sizeof(QemuCond));
1372 qemu_cond_init(cpu->halt_cond);
Dr. David Alan Gilbert49001162014-01-30 10:20:32 +00001373 snprintf(thread_name, VCPU_THREAD_NAME_SIZE, "CPU %d/DUMMY",
1374 cpu->cpu_index);
1375 qemu_thread_create(cpu->thread, thread_name, qemu_dummy_cpu_thread_fn, cpu,
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +02001376 QEMU_THREAD_JOINABLE);
Andreas Färber61a46212012-05-02 22:49:36 +02001377 while (!cpu->created) {
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +02001378 qemu_cond_wait(&qemu_cpu_cond, &qemu_global_mutex);
1379 }
1380}
1381
Andreas Färberc643bed2013-05-27 03:23:24 +02001382void qemu_init_vcpu(CPUState *cpu)
Blue Swirl296af7c2010-03-29 19:23:50 +00001383{
Andreas Färberce3960e2012-12-17 03:27:07 +01001384 cpu->nr_cores = smp_cores;
1385 cpu->nr_threads = smp_threads;
Andreas Färberf324e762012-05-02 23:26:21 +02001386 cpu->stopped = true;
Peter Maydell56943e82016-01-21 14:15:04 +00001387
1388 if (!cpu->as) {
1389 /* If the target cpu hasn't set up any address spaces itself,
1390 * give it the default one.
1391 */
Peter Crosthwaite6731d862016-01-21 14:15:06 +00001392 AddressSpace *as = address_space_init_shareable(cpu->memory,
1393 "cpu-memory");
Peter Maydell12ebc9a2016-01-21 14:15:04 +00001394 cpu->num_ases = 1;
Peter Crosthwaite6731d862016-01-21 14:15:06 +00001395 cpu_address_space_init(cpu, as, 0);
Peter Maydell56943e82016-01-21 14:15:04 +00001396 }
1397
Jan Kiszka0ab07c62011-02-07 12:19:14 +01001398 if (kvm_enabled()) {
Andreas Färber48a106b2013-05-27 02:20:39 +02001399 qemu_kvm_start_vcpu(cpu);
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +02001400 } else if (tcg_enabled()) {
Andreas Färbere5ab30a2012-05-03 01:50:44 +02001401 qemu_tcg_init_vcpu(cpu);
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +02001402 } else {
Andreas Färber10a90212013-05-27 02:24:35 +02001403 qemu_dummy_start_vcpu(cpu);
Jan Kiszka0ab07c62011-02-07 12:19:14 +01001404 }
Blue Swirl296af7c2010-03-29 19:23:50 +00001405}
1406
Jan Kiszkab4a3d962011-02-01 22:15:43 +01001407void cpu_stop_current(void)
Blue Swirl296af7c2010-03-29 19:23:50 +00001408{
Andreas Färber4917cf42013-05-27 05:17:50 +02001409 if (current_cpu) {
1410 current_cpu->stop = false;
1411 current_cpu->stopped = true;
1412 cpu_exit(current_cpu);
Dr. David Alan Gilbert96bce682016-01-25 10:08:18 +00001413 qemu_cond_broadcast(&qemu_pause_cond);
Jan Kiszkab4a3d962011-02-01 22:15:43 +01001414 }
Blue Swirl296af7c2010-03-29 19:23:50 +00001415}
1416
Kevin Wolf56983462013-07-05 13:49:54 +02001417int vm_stop(RunState state)
Blue Swirl296af7c2010-03-29 19:23:50 +00001418{
Juan Quintelaaa723c22012-09-18 16:30:11 +02001419 if (qemu_in_vcpu_thread()) {
Paolo Bonzini74892d22014-06-05 14:53:58 +02001420 qemu_system_vmstop_request_prepare();
Luiz Capitulino1dfb4dd2011-07-29 14:26:33 -03001421 qemu_system_vmstop_request(state);
Blue Swirl296af7c2010-03-29 19:23:50 +00001422 /*
1423 * FIXME: should not return to device code in case
1424 * vm_stop() has been requested.
1425 */
Jan Kiszkab4a3d962011-02-01 22:15:43 +01001426 cpu_stop_current();
Kevin Wolf56983462013-07-05 13:49:54 +02001427 return 0;
Blue Swirl296af7c2010-03-29 19:23:50 +00001428 }
Kevin Wolf56983462013-07-05 13:49:54 +02001429
1430 return do_vm_stop(state);
Blue Swirl296af7c2010-03-29 19:23:50 +00001431}
1432
Luiz Capitulino8a9236f2011-10-14 11:18:09 -03001433/* does a state transition even if the VM is already stopped,
1434 current state is forgotten forever */
Kevin Wolf56983462013-07-05 13:49:54 +02001435int vm_stop_force_state(RunState state)
Luiz Capitulino8a9236f2011-10-14 11:18:09 -03001436{
1437 if (runstate_is_running()) {
Kevin Wolf56983462013-07-05 13:49:54 +02001438 return vm_stop(state);
Luiz Capitulino8a9236f2011-10-14 11:18:09 -03001439 } else {
1440 runstate_set(state);
Wen Congyangb2780d32015-11-20 17:34:38 +08001441
1442 bdrv_drain_all();
Kevin Wolf594a45c2013-07-18 14:52:19 +02001443 /* Make sure to return an error if the flush in a previous vm_stop()
1444 * failed. */
Max Reitzda31d592016-03-16 19:54:32 +01001445 return blk_flush_all();
Luiz Capitulino8a9236f2011-10-14 11:18:09 -03001446 }
1447}
1448
Pavel Dovgalyuk8b427042015-09-17 19:24:05 +03001449static int64_t tcg_get_icount_limit(void)
1450{
1451 int64_t deadline;
1452
1453 if (replay_mode != REPLAY_MODE_PLAY) {
1454 deadline = qemu_clock_deadline_ns_all(QEMU_CLOCK_VIRTUAL);
1455
1456 /* Maintain prior (possibly buggy) behaviour where if no deadline
1457 * was set (as there is no QEMU_CLOCK_VIRTUAL timer) or it is more than
1458 * INT32_MAX nanoseconds ahead, we still use INT32_MAX
1459 * nanoseconds.
1460 */
1461 if ((deadline < 0) || (deadline > INT32_MAX)) {
1462 deadline = INT32_MAX;
1463 }
1464
1465 return qemu_icount_round(deadline);
1466 } else {
1467 return replay_get_instructions();
1468 }
1469}
1470
Peter Crosthwaite3d57f782015-06-23 19:31:17 -07001471static int tcg_cpu_exec(CPUState *cpu)
Blue Swirl296af7c2010-03-29 19:23:50 +00001472{
1473 int ret;
1474#ifdef CONFIG_PROFILER
1475 int64_t ti;
1476#endif
1477
1478#ifdef CONFIG_PROFILER
1479 ti = profile_getclock();
1480#endif
1481 if (use_icount) {
1482 int64_t count;
1483 int decr;
KONRAD Fredericc96778b2014-08-01 01:37:09 +02001484 timers_state.qemu_icount -= (cpu->icount_decr.u16.low
1485 + cpu->icount_extra);
Andreas Färber28ecfd72013-08-26 05:51:49 +02001486 cpu->icount_decr.u16.low = 0;
Andreas Färberefee7342013-08-26 05:39:29 +02001487 cpu->icount_extra = 0;
Pavel Dovgalyuk8b427042015-09-17 19:24:05 +03001488 count = tcg_get_icount_limit();
KONRAD Fredericc96778b2014-08-01 01:37:09 +02001489 timers_state.qemu_icount += count;
Blue Swirl296af7c2010-03-29 19:23:50 +00001490 decr = (count > 0xffff) ? 0xffff : count;
1491 count -= decr;
Andreas Färber28ecfd72013-08-26 05:51:49 +02001492 cpu->icount_decr.u16.low = decr;
Andreas Färberefee7342013-08-26 05:39:29 +02001493 cpu->icount_extra = count;
Blue Swirl296af7c2010-03-29 19:23:50 +00001494 }
Peter Crosthwaiteea3e9842015-06-18 10:24:55 -07001495 ret = cpu_exec(cpu);
Blue Swirl296af7c2010-03-29 19:23:50 +00001496#ifdef CONFIG_PROFILER
Alexey Kardashevskiy89d5cbd2015-03-16 14:57:38 +11001497 tcg_time += profile_getclock() - ti;
Blue Swirl296af7c2010-03-29 19:23:50 +00001498#endif
1499 if (use_icount) {
1500 /* Fold pending instructions back into the
1501 instruction counter, and clear the interrupt flag. */
KONRAD Fredericc96778b2014-08-01 01:37:09 +02001502 timers_state.qemu_icount -= (cpu->icount_decr.u16.low
1503 + cpu->icount_extra);
Andreas Färber28ecfd72013-08-26 05:51:49 +02001504 cpu->icount_decr.u32 = 0;
Andreas Färberefee7342013-08-26 05:39:29 +02001505 cpu->icount_extra = 0;
Pavel Dovgalyuk8b427042015-09-17 19:24:05 +03001506 replay_account_executed_instructions();
Blue Swirl296af7c2010-03-29 19:23:50 +00001507 }
1508 return ret;
1509}
1510
Jan Kiszkabdb7ca62011-09-26 09:40:39 +02001511static void tcg_exec_all(void)
Blue Swirl296af7c2010-03-29 19:23:50 +00001512{
Jan Kiszka9a360852011-02-01 22:15:55 +01001513 int r;
1514
Alex Bligh40daca52013-08-21 16:03:02 +01001515 /* Account partial waits to QEMU_CLOCK_VIRTUAL. */
Pavel Dovgalyuke76d1792016-03-10 14:56:09 +03001516 qemu_account_warp_timer();
Paolo Bonziniab33fcd2011-04-13 10:03:44 +02001517
Jan Kiszka0ab07c62011-02-07 12:19:14 +01001518 if (next_cpu == NULL) {
Blue Swirl296af7c2010-03-29 19:23:50 +00001519 next_cpu = first_cpu;
Jan Kiszka0ab07c62011-02-07 12:19:14 +01001520 }
Andreas Färberbdc44642013-06-24 23:50:24 +02001521 for (; next_cpu != NULL && !exit_request; next_cpu = CPU_NEXT(next_cpu)) {
Andreas Färber182735e2013-05-29 22:29:20 +02001522 CPUState *cpu = next_cpu;
Blue Swirl296af7c2010-03-29 19:23:50 +00001523
Alex Bligh40daca52013-08-21 16:03:02 +01001524 qemu_clock_enable(QEMU_CLOCK_VIRTUAL,
Andreas Färbered2803d2013-06-21 20:20:45 +02001525 (cpu->singlestep_enabled & SSTEP_NOTIMER) == 0);
Blue Swirl296af7c2010-03-29 19:23:50 +00001526
Andreas Färbera1fcaa72012-05-02 23:42:26 +02001527 if (cpu_can_run(cpu)) {
Peter Crosthwaite3d57f782015-06-23 19:31:17 -07001528 r = tcg_cpu_exec(cpu);
Jan Kiszka9a360852011-02-01 22:15:55 +01001529 if (r == EXCP_DEBUG) {
Andreas Färber91325042013-05-27 02:07:49 +02001530 cpu_handle_guest_debug(cpu);
Jan Kiszka3c638d02010-06-25 16:56:56 +02001531 break;
1532 }
Andreas Färberf324e762012-05-02 23:26:21 +02001533 } else if (cpu->stop || cpu->stopped) {
Blue Swirl296af7c2010-03-29 19:23:50 +00001534 break;
1535 }
1536 }
Paolo Bonziniaed807c2015-08-18 06:43:15 -07001537
1538 /* Pairs with smp_wmb in qemu_cpu_kick. */
1539 atomic_mb_set(&exit_request, 0);
Blue Swirl296af7c2010-03-29 19:23:50 +00001540}
1541
Stefan Weil9a78eea2010-10-22 23:03:33 +02001542void list_cpus(FILE *f, fprintf_function cpu_fprintf, const char *optarg)
Blue Swirl262353c2010-05-04 19:55:35 +00001543{
1544 /* XXX: implement xxx_cpu_list for targets that still miss it */
Peter Maydelle916cbf2012-09-05 17:41:08 -03001545#if defined(cpu_list)
1546 cpu_list(f, cpu_fprintf);
Blue Swirl262353c2010-05-04 19:55:35 +00001547#endif
1548}
Luiz Capitulinode0b36b2011-09-21 16:38:35 -03001549
1550CpuInfoList *qmp_query_cpus(Error **errp)
1551{
1552 CpuInfoList *head = NULL, *cur_item = NULL;
Andreas Färber182735e2013-05-29 22:29:20 +02001553 CPUState *cpu;
Luiz Capitulinode0b36b2011-09-21 16:38:35 -03001554
Andreas Färberbdc44642013-06-24 23:50:24 +02001555 CPU_FOREACH(cpu) {
Luiz Capitulinode0b36b2011-09-21 16:38:35 -03001556 CpuInfoList *info;
Andreas Färber182735e2013-05-29 22:29:20 +02001557#if defined(TARGET_I386)
1558 X86CPU *x86_cpu = X86_CPU(cpu);
1559 CPUX86State *env = &x86_cpu->env;
1560#elif defined(TARGET_PPC)
1561 PowerPCCPU *ppc_cpu = POWERPC_CPU(cpu);
1562 CPUPPCState *env = &ppc_cpu->env;
1563#elif defined(TARGET_SPARC)
1564 SPARCCPU *sparc_cpu = SPARC_CPU(cpu);
1565 CPUSPARCState *env = &sparc_cpu->env;
1566#elif defined(TARGET_MIPS)
1567 MIPSCPU *mips_cpu = MIPS_CPU(cpu);
1568 CPUMIPSState *env = &mips_cpu->env;
Bastian Koppelmann48e06fe2014-09-01 12:59:46 +01001569#elif defined(TARGET_TRICORE)
1570 TriCoreCPU *tricore_cpu = TRICORE_CPU(cpu);
1571 CPUTriCoreState *env = &tricore_cpu->env;
Andreas Färber182735e2013-05-29 22:29:20 +02001572#endif
Luiz Capitulinode0b36b2011-09-21 16:38:35 -03001573
Andreas Färbercb446ec2013-05-01 14:24:52 +02001574 cpu_synchronize_state(cpu);
Luiz Capitulinode0b36b2011-09-21 16:38:35 -03001575
1576 info = g_malloc0(sizeof(*info));
1577 info->value = g_malloc0(sizeof(*info->value));
Andreas Färber55e5c282012-12-17 06:18:02 +01001578 info->value->CPU = cpu->cpu_index;
Andreas Färber182735e2013-05-29 22:29:20 +02001579 info->value->current = (cpu == first_cpu);
Andreas Färber259186a2013-01-17 18:51:17 +01001580 info->value->halted = cpu->halted;
Eduardo Habkost58f88d42015-05-08 16:04:22 -03001581 info->value->qom_path = object_get_canonical_path(OBJECT(cpu));
Andreas Färber9f09e182012-05-03 06:59:07 +02001582 info->value->thread_id = cpu->thread_id;
Luiz Capitulinode0b36b2011-09-21 16:38:35 -03001583#if defined(TARGET_I386)
Eric Blake86f4b682015-11-18 01:52:59 -07001584 info->value->arch = CPU_INFO_ARCH_X86;
Eric Blake544a3732016-02-17 23:48:27 -07001585 info->value->u.x86.pc = env->eip + env->segs[R_CS].base;
Luiz Capitulinode0b36b2011-09-21 16:38:35 -03001586#elif defined(TARGET_PPC)
Eric Blake86f4b682015-11-18 01:52:59 -07001587 info->value->arch = CPU_INFO_ARCH_PPC;
Eric Blake544a3732016-02-17 23:48:27 -07001588 info->value->u.ppc.nip = env->nip;
Luiz Capitulinode0b36b2011-09-21 16:38:35 -03001589#elif defined(TARGET_SPARC)
Eric Blake86f4b682015-11-18 01:52:59 -07001590 info->value->arch = CPU_INFO_ARCH_SPARC;
Eric Blake544a3732016-02-17 23:48:27 -07001591 info->value->u.q_sparc.pc = env->pc;
1592 info->value->u.q_sparc.npc = env->npc;
Luiz Capitulinode0b36b2011-09-21 16:38:35 -03001593#elif defined(TARGET_MIPS)
Eric Blake86f4b682015-11-18 01:52:59 -07001594 info->value->arch = CPU_INFO_ARCH_MIPS;
Eric Blake544a3732016-02-17 23:48:27 -07001595 info->value->u.q_mips.PC = env->active_tc.PC;
Bastian Koppelmann48e06fe2014-09-01 12:59:46 +01001596#elif defined(TARGET_TRICORE)
Eric Blake86f4b682015-11-18 01:52:59 -07001597 info->value->arch = CPU_INFO_ARCH_TRICORE;
Eric Blake544a3732016-02-17 23:48:27 -07001598 info->value->u.tricore.PC = env->PC;
Eric Blake86f4b682015-11-18 01:52:59 -07001599#else
1600 info->value->arch = CPU_INFO_ARCH_OTHER;
Luiz Capitulinode0b36b2011-09-21 16:38:35 -03001601#endif
1602
1603 /* XXX: waiting for the qapi to support GSList */
1604 if (!cur_item) {
1605 head = cur_item = info;
1606 } else {
1607 cur_item->next = info;
1608 cur_item = info;
1609 }
1610 }
1611
1612 return head;
1613}
Luiz Capitulino0cfd6a92011-11-22 16:32:37 -02001614
1615void qmp_memsave(int64_t addr, int64_t size, const char *filename,
1616 bool has_cpu, int64_t cpu_index, Error **errp)
1617{
1618 FILE *f;
1619 uint32_t l;
Andreas Färber55e5c282012-12-17 06:18:02 +01001620 CPUState *cpu;
Luiz Capitulino0cfd6a92011-11-22 16:32:37 -02001621 uint8_t buf[1024];
Borislav Petkov0dc9daf2015-02-08 13:14:38 +01001622 int64_t orig_addr = addr, orig_size = size;
Luiz Capitulino0cfd6a92011-11-22 16:32:37 -02001623
1624 if (!has_cpu) {
1625 cpu_index = 0;
1626 }
1627
Andreas Färber151d1322013-02-15 15:41:49 +01001628 cpu = qemu_get_cpu(cpu_index);
1629 if (cpu == NULL) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001630 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "cpu-index",
1631 "a CPU number");
Luiz Capitulino0cfd6a92011-11-22 16:32:37 -02001632 return;
1633 }
1634
1635 f = fopen(filename, "wb");
1636 if (!f) {
Luiz Capitulino618da852013-06-07 14:35:06 -04001637 error_setg_file_open(errp, errno, filename);
Luiz Capitulino0cfd6a92011-11-22 16:32:37 -02001638 return;
1639 }
1640
1641 while (size != 0) {
1642 l = sizeof(buf);
1643 if (l > size)
1644 l = size;
Aneesh Kumar K.V2f4d0f52013-10-01 21:49:30 +05301645 if (cpu_memory_rw_debug(cpu, addr, buf, l, 0) != 0) {
Borislav Petkov0dc9daf2015-02-08 13:14:38 +01001646 error_setg(errp, "Invalid addr 0x%016" PRIx64 "/size %" PRId64
1647 " specified", orig_addr, orig_size);
Aneesh Kumar K.V2f4d0f52013-10-01 21:49:30 +05301648 goto exit;
1649 }
Luiz Capitulino0cfd6a92011-11-22 16:32:37 -02001650 if (fwrite(buf, 1, l, f) != l) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001651 error_setg(errp, QERR_IO_ERROR);
Luiz Capitulino0cfd6a92011-11-22 16:32:37 -02001652 goto exit;
1653 }
1654 addr += l;
1655 size -= l;
1656 }
1657
1658exit:
1659 fclose(f);
1660}
Luiz Capitulino6d3962b2011-11-22 17:26:46 -02001661
1662void qmp_pmemsave(int64_t addr, int64_t size, const char *filename,
1663 Error **errp)
1664{
1665 FILE *f;
1666 uint32_t l;
1667 uint8_t buf[1024];
1668
1669 f = fopen(filename, "wb");
1670 if (!f) {
Luiz Capitulino618da852013-06-07 14:35:06 -04001671 error_setg_file_open(errp, errno, filename);
Luiz Capitulino6d3962b2011-11-22 17:26:46 -02001672 return;
1673 }
1674
1675 while (size != 0) {
1676 l = sizeof(buf);
1677 if (l > size)
1678 l = size;
Stefan Weileb6282f2014-04-07 20:28:23 +02001679 cpu_physical_memory_read(addr, buf, l);
Luiz Capitulino6d3962b2011-11-22 17:26:46 -02001680 if (fwrite(buf, 1, l, f) != l) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001681 error_setg(errp, QERR_IO_ERROR);
Luiz Capitulino6d3962b2011-11-22 17:26:46 -02001682 goto exit;
1683 }
1684 addr += l;
1685 size -= l;
1686 }
1687
1688exit:
1689 fclose(f);
1690}
Luiz Capitulinoab49ab52011-11-23 12:55:53 -02001691
1692void qmp_inject_nmi(Error **errp)
1693{
1694#if defined(TARGET_I386)
Andreas Färber182735e2013-05-29 22:29:20 +02001695 CPUState *cs;
Luiz Capitulinoab49ab52011-11-23 12:55:53 -02001696
Andreas Färberbdc44642013-06-24 23:50:24 +02001697 CPU_FOREACH(cs) {
Andreas Färber182735e2013-05-29 22:29:20 +02001698 X86CPU *cpu = X86_CPU(cs);
Andreas Färber182735e2013-05-29 22:29:20 +02001699
Chen Fan02e51482013-12-23 17:04:02 +08001700 if (!cpu->apic_state) {
Andreas Färber182735e2013-05-29 22:29:20 +02001701 cpu_interrupt(cs, CPU_INTERRUPT_NMI);
Jan Kiszka02c09192011-10-18 00:00:06 +08001702 } else {
Chen Fan02e51482013-12-23 17:04:02 +08001703 apic_deliver_nmi(cpu->apic_state);
Jan Kiszka02c09192011-10-18 00:00:06 +08001704 }
Luiz Capitulinoab49ab52011-11-23 12:55:53 -02001705 }
1706#else
Alexey Kardashevskiy9cb805f2014-08-20 22:16:33 +10001707 nmi_monitor_handle(monitor_get_cpu_index(), errp);
Luiz Capitulinoab49ab52011-11-23 12:55:53 -02001708#endif
1709}
Sebastian Tanase27498be2014-07-25 11:56:33 +02001710
1711void dump_drift_info(FILE *f, fprintf_function cpu_fprintf)
1712{
1713 if (!use_icount) {
1714 return;
1715 }
1716
1717 cpu_fprintf(f, "Host - Guest clock %"PRIi64" ms\n",
1718 (cpu_get_clock() - cpu_get_icount())/SCALE_MS);
1719 if (icount_align_option) {
1720 cpu_fprintf(f, "Max guest delay %"PRIi64" ms\n", -max_delay/SCALE_MS);
1721 cpu_fprintf(f, "Max guest advance %"PRIi64" ms\n", max_advance/SCALE_MS);
1722 } else {
1723 cpu_fprintf(f, "Max guest delay NA\n");
1724 cpu_fprintf(f, "Max guest advance NA\n");
1725 }
1726}