blob: 8ae477728d65758c4efc290b53107f960b1e630b [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{
Paolo Bonzini17a15f12013-10-03 15:17:25 +0200341 /* The icount_warp_timer is rescheduled soon after vm_clock_warp_start
342 * changes from -1 to another value, so the race here is okay.
343 */
344 if (atomic_read(&vm_clock_warp_start) == -1) {
Paolo Bonzini946fb272011-09-12 13:57:37 +0200345 return;
346 }
347
Paolo Bonzini17a15f12013-10-03 15:17:25 +0200348 seqlock_write_lock(&timers_state.vm_clock_seqlock);
Paolo Bonzini946fb272011-09-12 13:57:37 +0200349 if (runstate_is_running()) {
Pavel Dovgalyuk8eda2062015-09-17 19:24:28 +0300350 int64_t clock = REPLAY_CLOCK(REPLAY_CLOCK_VIRTUAL_RT,
351 cpu_get_clock_locked());
Paolo Bonzini8ed961d2013-10-07 17:26:07 +0200352 int64_t warp_delta;
353
354 warp_delta = clock - vm_clock_warp_start;
355 if (use_icount == 2) {
Paolo Bonzini946fb272011-09-12 13:57:37 +0200356 /*
Alex Bligh40daca52013-08-21 16:03:02 +0100357 * In adaptive mode, do not let QEMU_CLOCK_VIRTUAL run too
Paolo Bonzini946fb272011-09-12 13:57:37 +0200358 * far ahead of real time.
359 */
Paolo Bonzini17a15f12013-10-03 15:17:25 +0200360 int64_t cur_icount = cpu_get_icount_locked();
Pavel Dovgalyukbf2a7dd2014-11-26 13:40:55 +0300361 int64_t delta = clock - cur_icount;
Paolo Bonzini8ed961d2013-10-07 17:26:07 +0200362 warp_delta = MIN(warp_delta, delta);
Paolo Bonzini946fb272011-09-12 13:57:37 +0200363 }
KONRAD Fredericc96778b2014-08-01 01:37:09 +0200364 timers_state.qemu_icount_bias += warp_delta;
Paolo Bonzini946fb272011-09-12 13:57:37 +0200365 }
366 vm_clock_warp_start = -1;
Paolo Bonzini17a15f12013-10-03 15:17:25 +0200367 seqlock_write_unlock(&timers_state.vm_clock_seqlock);
Paolo Bonzini8ed961d2013-10-07 17:26:07 +0200368
369 if (qemu_clock_expired(QEMU_CLOCK_VIRTUAL)) {
370 qemu_clock_notify(QEMU_CLOCK_VIRTUAL);
371 }
Paolo Bonzini946fb272011-09-12 13:57:37 +0200372}
373
Pavel Dovgalyuke76d1792016-03-10 14:56:09 +0300374static void icount_timer_cb(void *opaque)
Pavel Dovgalyukefab87c2015-09-17 19:24:39 +0300375{
Pavel Dovgalyuke76d1792016-03-10 14:56:09 +0300376 /* No need for a checkpoint because the timer already synchronizes
377 * with CHECKPOINT_CLOCK_VIRTUAL_RT.
378 */
379 icount_warp_rt();
Pavel Dovgalyukefab87c2015-09-17 19:24:39 +0300380}
381
Paolo Bonzini8156be52012-03-28 15:42:04 +0200382void qtest_clock_warp(int64_t dest)
383{
Alex Bligh40daca52013-08-21 16:03:02 +0100384 int64_t clock = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
Fam Zhengefef88b2015-01-19 17:51:43 +0800385 AioContext *aio_context;
Paolo Bonzini8156be52012-03-28 15:42:04 +0200386 assert(qtest_enabled());
Fam Zhengefef88b2015-01-19 17:51:43 +0800387 aio_context = qemu_get_aio_context();
Paolo Bonzini8156be52012-03-28 15:42:04 +0200388 while (clock < dest) {
Alex Bligh40daca52013-08-21 16:03:02 +0100389 int64_t deadline = qemu_clock_deadline_ns_all(QEMU_CLOCK_VIRTUAL);
Sergey Fedorovc9299e22014-06-10 13:10:28 +0400390 int64_t warp = qemu_soonest_timeout(dest - clock, deadline);
Fam Zhengefef88b2015-01-19 17:51:43 +0800391
Paolo Bonzini17a15f12013-10-03 15:17:25 +0200392 seqlock_write_lock(&timers_state.vm_clock_seqlock);
KONRAD Fredericc96778b2014-08-01 01:37:09 +0200393 timers_state.qemu_icount_bias += warp;
Paolo Bonzini17a15f12013-10-03 15:17:25 +0200394 seqlock_write_unlock(&timers_state.vm_clock_seqlock);
395
Alex Bligh40daca52013-08-21 16:03:02 +0100396 qemu_clock_run_timers(QEMU_CLOCK_VIRTUAL);
Fam Zhengefef88b2015-01-19 17:51:43 +0800397 timerlist_run_timers(aio_context->tlg.tl[QEMU_CLOCK_VIRTUAL]);
Alex Bligh40daca52013-08-21 16:03:02 +0100398 clock = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
Paolo Bonzini8156be52012-03-28 15:42:04 +0200399 }
Alex Bligh40daca52013-08-21 16:03:02 +0100400 qemu_clock_notify(QEMU_CLOCK_VIRTUAL);
Paolo Bonzini8156be52012-03-28 15:42:04 +0200401}
402
Pavel Dovgalyuke76d1792016-03-10 14:56:09 +0300403void qemu_start_warp_timer(void)
Paolo Bonzini946fb272011-09-12 13:57:37 +0200404{
Paolo Bonzinice78d182013-10-07 17:30:02 +0200405 int64_t clock;
Paolo Bonzini946fb272011-09-12 13:57:37 +0200406 int64_t deadline;
407
Pavel Dovgalyuke76d1792016-03-10 14:56:09 +0300408 if (!use_icount) {
Paolo Bonzini946fb272011-09-12 13:57:37 +0200409 return;
410 }
411
Pavel Dovgalyuk8bd7f712015-09-17 19:24:44 +0300412 /* Nothing to do if the VM is stopped: QEMU_CLOCK_VIRTUAL timers
413 * do not fire, so computing the deadline does not make sense.
414 */
415 if (!runstate_is_running()) {
416 return;
417 }
418
419 /* warp clock deterministically in record/replay mode */
Pavel Dovgalyuke76d1792016-03-10 14:56:09 +0300420 if (!replay_checkpoint(CHECKPOINT_CLOCK_WARP_START)) {
Pavel Dovgalyuk8bd7f712015-09-17 19:24:44 +0300421 return;
422 }
423
Paolo Bonzinice78d182013-10-07 17:30:02 +0200424 if (!all_cpu_threads_idle()) {
Paolo Bonzini946fb272011-09-12 13:57:37 +0200425 return;
426 }
427
Paolo Bonzini8156be52012-03-28 15:42:04 +0200428 if (qtest_enabled()) {
429 /* When testing, qtest commands advance icount. */
Pavel Dovgalyuke76d1792016-03-10 14:56:09 +0300430 return;
Paolo Bonzini8156be52012-03-28 15:42:04 +0200431 }
432
Alex Blighac70aaf2013-08-21 16:02:57 +0100433 /* We want to use the earliest deadline from ALL vm_clocks */
Pavel Dovgalyukbf2a7dd2014-11-26 13:40:55 +0300434 clock = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL_RT);
Alex Bligh40daca52013-08-21 16:03:02 +0100435 deadline = qemu_clock_deadline_ns_all(QEMU_CLOCK_VIRTUAL);
Paolo Bonzinice78d182013-10-07 17:30:02 +0200436 if (deadline < 0) {
Victor CLEMENTd7a0f712015-05-29 17:14:06 +0200437 static bool notified;
438 if (!icount_sleep && !notified) {
439 error_report("WARNING: icount sleep disabled and no active timers");
440 notified = true;
441 }
Paolo Bonzinice78d182013-10-07 17:30:02 +0200442 return;
Alex Blighac70aaf2013-08-21 16:02:57 +0100443 }
444
Paolo Bonzini946fb272011-09-12 13:57:37 +0200445 if (deadline > 0) {
446 /*
Alex Bligh40daca52013-08-21 16:03:02 +0100447 * Ensure QEMU_CLOCK_VIRTUAL proceeds even when the virtual CPU goes to
Paolo Bonzini946fb272011-09-12 13:57:37 +0200448 * sleep. Otherwise, the CPU might be waiting for a future timer
449 * interrupt to wake it up, but the interrupt never comes because
450 * the vCPU isn't running any insns and thus doesn't advance the
Alex Bligh40daca52013-08-21 16:03:02 +0100451 * QEMU_CLOCK_VIRTUAL.
Paolo Bonzini946fb272011-09-12 13:57:37 +0200452 */
Victor CLEMENT5045e9d92015-05-29 17:14:04 +0200453 if (!icount_sleep) {
454 /*
455 * We never let VCPUs sleep in no sleep icount mode.
456 * If there is a pending QEMU_CLOCK_VIRTUAL timer we just advance
457 * to the next QEMU_CLOCK_VIRTUAL event and notify it.
458 * It is useful when we want a deterministic execution time,
459 * isolated from host latencies.
460 */
461 seqlock_write_lock(&timers_state.vm_clock_seqlock);
462 timers_state.qemu_icount_bias += deadline;
463 seqlock_write_unlock(&timers_state.vm_clock_seqlock);
464 qemu_clock_notify(QEMU_CLOCK_VIRTUAL);
465 } else {
466 /*
467 * We do stop VCPUs and only advance QEMU_CLOCK_VIRTUAL after some
468 * "real" time, (related to the time left until the next event) has
469 * passed. The QEMU_CLOCK_VIRTUAL_RT clock will do this.
470 * This avoids that the warps are visible externally; for example,
471 * you will not be sending network packets continuously instead of
472 * every 100ms.
473 */
474 seqlock_write_lock(&timers_state.vm_clock_seqlock);
475 if (vm_clock_warp_start == -1 || vm_clock_warp_start > clock) {
476 vm_clock_warp_start = clock;
477 }
478 seqlock_write_unlock(&timers_state.vm_clock_seqlock);
479 timer_mod_anticipate(icount_warp_timer, clock + deadline);
Paolo Bonzinice78d182013-10-07 17:30:02 +0200480 }
Alex Blighac70aaf2013-08-21 16:02:57 +0100481 } else if (deadline == 0) {
Alex Bligh40daca52013-08-21 16:03:02 +0100482 qemu_clock_notify(QEMU_CLOCK_VIRTUAL);
Paolo Bonzini946fb272011-09-12 13:57:37 +0200483 }
484}
485
Pavel Dovgalyuke76d1792016-03-10 14:56:09 +0300486static void qemu_account_warp_timer(void)
487{
488 if (!use_icount || !icount_sleep) {
489 return;
490 }
491
492 /* Nothing to do if the VM is stopped: QEMU_CLOCK_VIRTUAL timers
493 * do not fire, so computing the deadline does not make sense.
494 */
495 if (!runstate_is_running()) {
496 return;
497 }
498
499 /* warp clock deterministically in record/replay mode */
500 if (!replay_checkpoint(CHECKPOINT_CLOCK_WARP_ACCOUNT)) {
501 return;
502 }
503
504 timer_del(icount_warp_timer);
505 icount_warp_rt();
506}
507
KONRAD Fredericd09eae32014-08-01 01:37:10 +0200508static bool icount_state_needed(void *opaque)
509{
510 return use_icount;
511}
512
513/*
514 * This is a subsection for icount migration.
515 */
516static const VMStateDescription icount_vmstate_timers = {
517 .name = "timer/icount",
518 .version_id = 1,
519 .minimum_version_id = 1,
Juan Quintela5cd8cad2014-09-23 14:09:54 +0200520 .needed = icount_state_needed,
KONRAD Fredericd09eae32014-08-01 01:37:10 +0200521 .fields = (VMStateField[]) {
522 VMSTATE_INT64(qemu_icount_bias, TimersState),
523 VMSTATE_INT64(qemu_icount, TimersState),
524 VMSTATE_END_OF_LIST()
525 }
526};
527
Paolo Bonzini946fb272011-09-12 13:57:37 +0200528static const VMStateDescription vmstate_timers = {
529 .name = "timer",
530 .version_id = 2,
531 .minimum_version_id = 1,
Juan Quintela35d08452014-04-16 16:01:33 +0200532 .fields = (VMStateField[]) {
Paolo Bonzini946fb272011-09-12 13:57:37 +0200533 VMSTATE_INT64(cpu_ticks_offset, TimersState),
534 VMSTATE_INT64(dummy, TimersState),
535 VMSTATE_INT64_V(cpu_clock_offset, TimersState, 2),
536 VMSTATE_END_OF_LIST()
KONRAD Fredericd09eae32014-08-01 01:37:10 +0200537 },
Juan Quintela5cd8cad2014-09-23 14:09:54 +0200538 .subsections = (const VMStateDescription*[]) {
539 &icount_vmstate_timers,
540 NULL
Paolo Bonzini946fb272011-09-12 13:57:37 +0200541 }
542};
543
Jason J. Herne2adcc852015-09-08 13:12:33 -0400544static void cpu_throttle_thread(void *opaque)
545{
546 CPUState *cpu = opaque;
547 double pct;
548 double throttle_ratio;
549 long sleeptime_ns;
550
551 if (!cpu_throttle_get_percentage()) {
552 return;
553 }
554
555 pct = (double)cpu_throttle_get_percentage()/100;
556 throttle_ratio = pct / (1 - pct);
557 sleeptime_ns = (long)(throttle_ratio * CPU_THROTTLE_TIMESLICE_NS);
558
559 qemu_mutex_unlock_iothread();
560 atomic_set(&cpu->throttle_thread_scheduled, 0);
561 g_usleep(sleeptime_ns / 1000); /* Convert ns to us for usleep call */
562 qemu_mutex_lock_iothread();
563}
564
565static void cpu_throttle_timer_tick(void *opaque)
566{
567 CPUState *cpu;
568 double pct;
569
570 /* Stop the timer if needed */
571 if (!cpu_throttle_get_percentage()) {
572 return;
573 }
574 CPU_FOREACH(cpu) {
575 if (!atomic_xchg(&cpu->throttle_thread_scheduled, 1)) {
576 async_run_on_cpu(cpu, cpu_throttle_thread, cpu);
577 }
578 }
579
580 pct = (double)cpu_throttle_get_percentage()/100;
581 timer_mod(throttle_timer, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL_RT) +
582 CPU_THROTTLE_TIMESLICE_NS / (1-pct));
583}
584
585void cpu_throttle_set(int new_throttle_pct)
586{
587 /* Ensure throttle percentage is within valid range */
588 new_throttle_pct = MIN(new_throttle_pct, CPU_THROTTLE_PCT_MAX);
589 new_throttle_pct = MAX(new_throttle_pct, CPU_THROTTLE_PCT_MIN);
590
591 atomic_set(&throttle_percentage, new_throttle_pct);
592
593 timer_mod(throttle_timer, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL_RT) +
594 CPU_THROTTLE_TIMESLICE_NS);
595}
596
597void cpu_throttle_stop(void)
598{
599 atomic_set(&throttle_percentage, 0);
600}
601
602bool cpu_throttle_active(void)
603{
604 return (cpu_throttle_get_percentage() != 0);
605}
606
607int cpu_throttle_get_percentage(void)
608{
609 return atomic_read(&throttle_percentage);
610}
611
Pavel Dovgalyuk4603ea02014-09-01 09:34:49 +0400612void cpu_ticks_init(void)
613{
614 seqlock_init(&timers_state.vm_clock_seqlock, NULL);
615 vmstate_register(NULL, 0, &vmstate_timers, &timers_state);
Jason J. Herne2adcc852015-09-08 13:12:33 -0400616 throttle_timer = timer_new_ns(QEMU_CLOCK_VIRTUAL_RT,
617 cpu_throttle_timer_tick, NULL);
Pavel Dovgalyuk4603ea02014-09-01 09:34:49 +0400618}
619
Sebastian Tanase1ad95802014-07-25 11:56:28 +0200620void configure_icount(QemuOpts *opts, Error **errp)
Paolo Bonzini946fb272011-09-12 13:57:37 +0200621{
Sebastian Tanase1ad95802014-07-25 11:56:28 +0200622 const char *option;
Sebastian Tanasea8bfac32014-07-25 11:56:29 +0200623 char *rem_str = NULL;
Sebastian Tanase1ad95802014-07-25 11:56:28 +0200624
Sebastian Tanase1ad95802014-07-25 11:56:28 +0200625 option = qemu_opt_get(opts, "shift");
Paolo Bonzini946fb272011-09-12 13:57:37 +0200626 if (!option) {
Sebastian Tanasea8bfac32014-07-25 11:56:29 +0200627 if (qemu_opt_get(opts, "align") != NULL) {
628 error_setg(errp, "Please specify shift option when using align");
629 }
Paolo Bonzini946fb272011-09-12 13:57:37 +0200630 return;
631 }
Victor CLEMENTf1f4b572015-05-29 17:14:05 +0200632
633 icount_sleep = qemu_opt_get_bool(opts, "sleep", true);
Victor CLEMENT5045e9d92015-05-29 17:14:04 +0200634 if (icount_sleep) {
635 icount_warp_timer = timer_new_ns(QEMU_CLOCK_VIRTUAL_RT,
Pavel Dovgalyuke76d1792016-03-10 14:56:09 +0300636 icount_timer_cb, NULL);
Victor CLEMENT5045e9d92015-05-29 17:14:04 +0200637 }
Victor CLEMENTf1f4b572015-05-29 17:14:05 +0200638
Sebastian Tanasea8bfac32014-07-25 11:56:29 +0200639 icount_align_option = qemu_opt_get_bool(opts, "align", false);
Victor CLEMENTf1f4b572015-05-29 17:14:05 +0200640
641 if (icount_align_option && !icount_sleep) {
Pranith Kumar778d9f92016-02-26 10:16:51 -0500642 error_setg(errp, "align=on and sleep=off are incompatible");
Victor CLEMENTf1f4b572015-05-29 17:14:05 +0200643 }
Paolo Bonzini946fb272011-09-12 13:57:37 +0200644 if (strcmp(option, "auto") != 0) {
Sebastian Tanasea8bfac32014-07-25 11:56:29 +0200645 errno = 0;
646 icount_time_shift = strtol(option, &rem_str, 0);
647 if (errno != 0 || *rem_str != '\0' || !strlen(option)) {
648 error_setg(errp, "icount: Invalid shift value");
649 }
Paolo Bonzini946fb272011-09-12 13:57:37 +0200650 use_icount = 1;
651 return;
Sebastian Tanasea8bfac32014-07-25 11:56:29 +0200652 } else if (icount_align_option) {
653 error_setg(errp, "shift=auto and align=on are incompatible");
Victor CLEMENTf1f4b572015-05-29 17:14:05 +0200654 } else if (!icount_sleep) {
Pranith Kumar778d9f92016-02-26 10:16:51 -0500655 error_setg(errp, "shift=auto and sleep=off are incompatible");
Paolo Bonzini946fb272011-09-12 13:57:37 +0200656 }
657
658 use_icount = 2;
659
660 /* 125MIPS seems a reasonable initial guess at the guest speed.
661 It will be corrected fairly quickly anyway. */
662 icount_time_shift = 3;
663
664 /* Have both realtime and virtual time triggers for speed adjustment.
665 The realtime trigger catches emulated time passing too slowly,
666 the virtual time trigger catches emulated time passing too fast.
667 Realtime triggers occur even when idle, so use them less frequently
668 than VM triggers. */
Pavel Dovgalyukbf2a7dd2014-11-26 13:40:55 +0300669 icount_rt_timer = timer_new_ms(QEMU_CLOCK_VIRTUAL_RT,
670 icount_adjust_rt, NULL);
Alex Bligh40daca52013-08-21 16:03:02 +0100671 timer_mod(icount_rt_timer,
Pavel Dovgalyukbf2a7dd2014-11-26 13:40:55 +0300672 qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL_RT) + 1000);
Alex Bligh40daca52013-08-21 16:03:02 +0100673 icount_vm_timer = timer_new_ns(QEMU_CLOCK_VIRTUAL,
674 icount_adjust_vm, NULL);
675 timer_mod(icount_vm_timer,
676 qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) +
Rutuja Shah73bcb242016-03-21 21:32:30 +0530677 NANOSECONDS_PER_SECOND / 10);
Paolo Bonzini946fb272011-09-12 13:57:37 +0200678}
679
680/***********************************************************/
Blue Swirl296af7c2010-03-29 19:23:50 +0000681void hw_error(const char *fmt, ...)
682{
683 va_list ap;
Andreas Färber55e5c282012-12-17 06:18:02 +0100684 CPUState *cpu;
Blue Swirl296af7c2010-03-29 19:23:50 +0000685
686 va_start(ap, fmt);
687 fprintf(stderr, "qemu: hardware error: ");
688 vfprintf(stderr, fmt, ap);
689 fprintf(stderr, "\n");
Andreas Färberbdc44642013-06-24 23:50:24 +0200690 CPU_FOREACH(cpu) {
Andreas Färber55e5c282012-12-17 06:18:02 +0100691 fprintf(stderr, "CPU #%d:\n", cpu->cpu_index);
Andreas Färber878096e2013-05-27 01:33:50 +0200692 cpu_dump_state(cpu, stderr, fprintf, CPU_DUMP_FPU);
Blue Swirl296af7c2010-03-29 19:23:50 +0000693 }
694 va_end(ap);
695 abort();
696}
697
698void cpu_synchronize_all_states(void)
699{
Andreas Färber182735e2013-05-29 22:29:20 +0200700 CPUState *cpu;
Blue Swirl296af7c2010-03-29 19:23:50 +0000701
Andreas Färberbdc44642013-06-24 23:50:24 +0200702 CPU_FOREACH(cpu) {
Andreas Färber182735e2013-05-29 22:29:20 +0200703 cpu_synchronize_state(cpu);
Blue Swirl296af7c2010-03-29 19:23:50 +0000704 }
705}
706
707void cpu_synchronize_all_post_reset(void)
708{
Andreas Färber182735e2013-05-29 22:29:20 +0200709 CPUState *cpu;
Blue Swirl296af7c2010-03-29 19:23:50 +0000710
Andreas Färberbdc44642013-06-24 23:50:24 +0200711 CPU_FOREACH(cpu) {
Andreas Färber182735e2013-05-29 22:29:20 +0200712 cpu_synchronize_post_reset(cpu);
Blue Swirl296af7c2010-03-29 19:23:50 +0000713 }
714}
715
716void cpu_synchronize_all_post_init(void)
717{
Andreas Färber182735e2013-05-29 22:29:20 +0200718 CPUState *cpu;
Blue Swirl296af7c2010-03-29 19:23:50 +0000719
Andreas Färberbdc44642013-06-24 23:50:24 +0200720 CPU_FOREACH(cpu) {
Andreas Färber182735e2013-05-29 22:29:20 +0200721 cpu_synchronize_post_init(cpu);
Blue Swirl296af7c2010-03-29 19:23:50 +0000722 }
723}
724
Kevin Wolf56983462013-07-05 13:49:54 +0200725static int do_vm_stop(RunState state)
Blue Swirl296af7c2010-03-29 19:23:50 +0000726{
Kevin Wolf56983462013-07-05 13:49:54 +0200727 int ret = 0;
728
Luiz Capitulino13548692011-07-29 15:36:43 -0300729 if (runstate_is_running()) {
Blue Swirl296af7c2010-03-29 19:23:50 +0000730 cpu_disable_ticks();
Blue Swirl296af7c2010-03-29 19:23:50 +0000731 pause_all_vcpus();
Luiz Capitulinof5bbfba2011-07-29 15:04:45 -0300732 runstate_set(state);
Luiz Capitulino1dfb4dd2011-07-29 14:26:33 -0300733 vm_state_notify(0, state);
Wenchao Xiaa4e15de2014-06-18 08:43:36 +0200734 qapi_event_send_stop(&error_abort);
Blue Swirl296af7c2010-03-29 19:23:50 +0000735 }
Kevin Wolf56983462013-07-05 13:49:54 +0200736
Kevin Wolf594a45c2013-07-18 14:52:19 +0200737 bdrv_drain_all();
Max Reitzda31d592016-03-16 19:54:32 +0100738 ret = blk_flush_all();
Kevin Wolf594a45c2013-07-18 14:52:19 +0200739
Kevin Wolf56983462013-07-05 13:49:54 +0200740 return ret;
Blue Swirl296af7c2010-03-29 19:23:50 +0000741}
742
Andreas Färbera1fcaa72012-05-02 23:42:26 +0200743static bool cpu_can_run(CPUState *cpu)
Blue Swirl296af7c2010-03-29 19:23:50 +0000744{
Andreas Färber4fdeee72012-05-02 23:10:09 +0200745 if (cpu->stop) {
Andreas Färbera1fcaa72012-05-02 23:42:26 +0200746 return false;
Jan Kiszka0ab07c62011-02-07 12:19:14 +0100747 }
Tiejun Chen321bc0b2013-08-02 09:43:09 +0800748 if (cpu_is_stopped(cpu)) {
Andreas Färbera1fcaa72012-05-02 23:42:26 +0200749 return false;
Jan Kiszka0ab07c62011-02-07 12:19:14 +0100750 }
Andreas Färbera1fcaa72012-05-02 23:42:26 +0200751 return true;
Blue Swirl296af7c2010-03-29 19:23:50 +0000752}
753
Andreas Färber91325042013-05-27 02:07:49 +0200754static void cpu_handle_guest_debug(CPUState *cpu)
Jan Kiszka3c638d02010-06-25 16:56:56 +0200755{
Andreas Färber64f6b342013-05-27 02:06:09 +0200756 gdb_set_stop_cpu(cpu);
Jan Kiszka8cf71712011-02-07 12:19:16 +0100757 qemu_system_debug_request();
Andreas Färberf324e762012-05-02 23:26:21 +0200758 cpu->stopped = true;
Jan Kiszka3c638d02010-06-25 16:56:56 +0200759}
760
Jan Kiszka6d9cb732011-02-01 22:15:58 +0100761#ifdef CONFIG_LINUX
762static void sigbus_reraise(void)
763{
764 sigset_t set;
765 struct sigaction action;
766
767 memset(&action, 0, sizeof(action));
768 action.sa_handler = SIG_DFL;
769 if (!sigaction(SIGBUS, &action, NULL)) {
770 raise(SIGBUS);
771 sigemptyset(&set);
772 sigaddset(&set, SIGBUS);
773 sigprocmask(SIG_UNBLOCK, &set, NULL);
774 }
775 perror("Failed to re-raise SIGBUS!\n");
776 abort();
777}
778
779static void sigbus_handler(int n, struct qemu_signalfd_siginfo *siginfo,
780 void *ctx)
781{
782 if (kvm_on_sigbus(siginfo->ssi_code,
783 (void *)(intptr_t)siginfo->ssi_addr)) {
784 sigbus_reraise();
785 }
786}
787
788static void qemu_init_sigbus(void)
789{
790 struct sigaction action;
791
792 memset(&action, 0, sizeof(action));
793 action.sa_flags = SA_SIGINFO;
794 action.sa_sigaction = (void (*)(int, siginfo_t*, void*))sigbus_handler;
795 sigaction(SIGBUS, &action, NULL);
796
797 prctl(PR_MCE_KILL, PR_MCE_KILL_SET, PR_MCE_KILL_EARLY, 0, 0);
798}
799
Andreas Färber290adf32013-01-17 09:30:27 +0100800static void qemu_kvm_eat_signals(CPUState *cpu)
Jan Kiszka1ab3c6c2011-03-15 12:26:12 +0100801{
802 struct timespec ts = { 0, 0 };
803 siginfo_t siginfo;
804 sigset_t waitset;
805 sigset_t chkset;
806 int r;
807
808 sigemptyset(&waitset);
809 sigaddset(&waitset, SIG_IPI);
810 sigaddset(&waitset, SIGBUS);
811
812 do {
813 r = sigtimedwait(&waitset, &siginfo, &ts);
814 if (r == -1 && !(errno == EAGAIN || errno == EINTR)) {
815 perror("sigtimedwait");
816 exit(1);
817 }
818
819 switch (r) {
820 case SIGBUS:
Andreas Färber290adf32013-01-17 09:30:27 +0100821 if (kvm_on_sigbus_vcpu(cpu, siginfo.si_code, siginfo.si_addr)) {
Jan Kiszka1ab3c6c2011-03-15 12:26:12 +0100822 sigbus_reraise();
823 }
824 break;
825 default:
826 break;
827 }
828
829 r = sigpending(&chkset);
830 if (r == -1) {
831 perror("sigpending");
832 exit(1);
833 }
834 } while (sigismember(&chkset, SIG_IPI) || sigismember(&chkset, SIGBUS));
Jan Kiszka1ab3c6c2011-03-15 12:26:12 +0100835}
836
Jan Kiszka6d9cb732011-02-01 22:15:58 +0100837#else /* !CONFIG_LINUX */
838
839static void qemu_init_sigbus(void)
840{
841}
Jan Kiszka1ab3c6c2011-03-15 12:26:12 +0100842
Andreas Färber290adf32013-01-17 09:30:27 +0100843static void qemu_kvm_eat_signals(CPUState *cpu)
Jan Kiszka1ab3c6c2011-03-15 12:26:12 +0100844{
845}
Jan Kiszka6d9cb732011-02-01 22:15:58 +0100846#endif /* !CONFIG_LINUX */
847
Blue Swirl296af7c2010-03-29 19:23:50 +0000848#ifndef _WIN32
Jan Kiszka55f8d6a2011-02-01 22:15:52 +0100849static void dummy_signal(int sig)
Blue Swirl296af7c2010-03-29 19:23:50 +0000850{
851}
852
Andreas Färber13618e02013-05-26 23:41:00 +0200853static void qemu_kvm_init_cpu_signals(CPUState *cpu)
Paolo Bonzini714bd042011-03-12 17:44:06 +0100854{
855 int r;
856 sigset_t set;
857 struct sigaction sigact;
858
859 memset(&sigact, 0, sizeof(sigact));
860 sigact.sa_handler = dummy_signal;
861 sigaction(SIG_IPI, &sigact, NULL);
862
Paolo Bonzini714bd042011-03-12 17:44:06 +0100863 pthread_sigmask(SIG_BLOCK, NULL, &set);
864 sigdelset(&set, SIG_IPI);
865 sigdelset(&set, SIGBUS);
Andreas Färber491d6e82013-05-26 23:38:10 +0200866 r = kvm_set_signal_mask(cpu, &set);
Paolo Bonzini714bd042011-03-12 17:44:06 +0100867 if (r) {
868 fprintf(stderr, "kvm_set_signal_mask: %s\n", strerror(-r));
869 exit(1);
870 }
Paolo Bonzini714bd042011-03-12 17:44:06 +0100871}
872
Jan Kiszka55f8d6a2011-02-01 22:15:52 +0100873#else /* _WIN32 */
Andreas Färber13618e02013-05-26 23:41:00 +0200874static void qemu_kvm_init_cpu_signals(CPUState *cpu)
Paolo Bonzini714bd042011-03-12 17:44:06 +0100875{
876 abort();
877}
Jan Kiszka55f8d6a2011-02-01 22:15:52 +0100878#endif /* _WIN32 */
Blue Swirl296af7c2010-03-29 19:23:50 +0000879
Stefan Weilb2532d82012-09-27 07:41:42 +0200880static QemuMutex qemu_global_mutex;
Paolo Bonzini46daff12011-06-09 13:10:24 +0200881static QemuCond qemu_io_proceeded_cond;
Paolo Bonzini6b498092015-02-27 19:58:23 +0100882static unsigned iothread_requesting_mutex;
Blue Swirl296af7c2010-03-29 19:23:50 +0000883
884static QemuThread io_thread;
885
Blue Swirl296af7c2010-03-29 19:23:50 +0000886/* cpu creation */
887static QemuCond qemu_cpu_cond;
888/* system init */
Blue Swirl296af7c2010-03-29 19:23:50 +0000889static QemuCond qemu_pause_cond;
Marcelo Tosattie82bcec2010-05-04 09:45:22 -0300890static QemuCond qemu_work_cond;
Blue Swirl296af7c2010-03-29 19:23:50 +0000891
Paolo Bonzinid3b12f52011-09-13 10:30:52 +0200892void qemu_init_cpu_loop(void)
Blue Swirl296af7c2010-03-29 19:23:50 +0000893{
Jan Kiszka6d9cb732011-02-01 22:15:58 +0100894 qemu_init_sigbus();
Anthony Liguoried945922011-02-08 18:18:18 +0100895 qemu_cond_init(&qemu_cpu_cond);
Anthony Liguoried945922011-02-08 18:18:18 +0100896 qemu_cond_init(&qemu_pause_cond);
897 qemu_cond_init(&qemu_work_cond);
Paolo Bonzini46daff12011-06-09 13:10:24 +0200898 qemu_cond_init(&qemu_io_proceeded_cond);
Blue Swirl296af7c2010-03-29 19:23:50 +0000899 qemu_mutex_init(&qemu_global_mutex);
Blue Swirl296af7c2010-03-29 19:23:50 +0000900
Jan Kiszkab7680cb2011-03-12 17:43:51 +0100901 qemu_thread_get_self(&io_thread);
Blue Swirl296af7c2010-03-29 19:23:50 +0000902}
903
Andreas Färberf100f0b2012-05-03 14:58:47 +0200904void run_on_cpu(CPUState *cpu, void (*func)(void *data), void *data)
Marcelo Tosattie82bcec2010-05-04 09:45:22 -0300905{
906 struct qemu_work_item wi;
907
Andreas Färber60e82572012-05-02 22:23:49 +0200908 if (qemu_cpu_is_self(cpu)) {
Marcelo Tosattie82bcec2010-05-04 09:45:22 -0300909 func(data);
910 return;
911 }
912
913 wi.func = func;
914 wi.data = data;
Chegu Vinod3c022702013-06-24 03:49:41 -0600915 wi.free = false;
Paolo Bonzini376692b2015-07-10 12:32:32 +0200916
917 qemu_mutex_lock(&cpu->work_mutex);
Andreas Färberc64ca812012-05-03 02:11:45 +0200918 if (cpu->queued_work_first == NULL) {
919 cpu->queued_work_first = &wi;
Jan Kiszka0ab07c62011-02-07 12:19:14 +0100920 } else {
Andreas Färberc64ca812012-05-03 02:11:45 +0200921 cpu->queued_work_last->next = &wi;
Jan Kiszka0ab07c62011-02-07 12:19:14 +0100922 }
Andreas Färberc64ca812012-05-03 02:11:45 +0200923 cpu->queued_work_last = &wi;
Marcelo Tosattie82bcec2010-05-04 09:45:22 -0300924 wi.next = NULL;
925 wi.done = false;
Paolo Bonzini376692b2015-07-10 12:32:32 +0200926 qemu_mutex_unlock(&cpu->work_mutex);
Marcelo Tosattie82bcec2010-05-04 09:45:22 -0300927
Andreas Färberc08d7422012-05-03 04:34:15 +0200928 qemu_cpu_kick(cpu);
Paolo Bonzini376692b2015-07-10 12:32:32 +0200929 while (!atomic_mb_read(&wi.done)) {
Andreas Färber4917cf42013-05-27 05:17:50 +0200930 CPUState *self_cpu = current_cpu;
Marcelo Tosattie82bcec2010-05-04 09:45:22 -0300931
932 qemu_cond_wait(&qemu_work_cond, &qemu_global_mutex);
Andreas Färber4917cf42013-05-27 05:17:50 +0200933 current_cpu = self_cpu;
Marcelo Tosattie82bcec2010-05-04 09:45:22 -0300934 }
935}
936
Chegu Vinod3c022702013-06-24 03:49:41 -0600937void async_run_on_cpu(CPUState *cpu, void (*func)(void *data), void *data)
938{
939 struct qemu_work_item *wi;
940
941 if (qemu_cpu_is_self(cpu)) {
942 func(data);
943 return;
944 }
945
946 wi = g_malloc0(sizeof(struct qemu_work_item));
947 wi->func = func;
948 wi->data = data;
949 wi->free = true;
Paolo Bonzini376692b2015-07-10 12:32:32 +0200950
951 qemu_mutex_lock(&cpu->work_mutex);
Chegu Vinod3c022702013-06-24 03:49:41 -0600952 if (cpu->queued_work_first == NULL) {
953 cpu->queued_work_first = wi;
954 } else {
955 cpu->queued_work_last->next = wi;
956 }
957 cpu->queued_work_last = wi;
958 wi->next = NULL;
959 wi->done = false;
Paolo Bonzini376692b2015-07-10 12:32:32 +0200960 qemu_mutex_unlock(&cpu->work_mutex);
Chegu Vinod3c022702013-06-24 03:49:41 -0600961
962 qemu_cpu_kick(cpu);
963}
964
Andreas Färber6d45b102012-05-03 02:13:22 +0200965static void flush_queued_work(CPUState *cpu)
Marcelo Tosattie82bcec2010-05-04 09:45:22 -0300966{
967 struct qemu_work_item *wi;
968
Andreas Färberc64ca812012-05-03 02:11:45 +0200969 if (cpu->queued_work_first == NULL) {
Marcelo Tosattie82bcec2010-05-04 09:45:22 -0300970 return;
Jan Kiszka0ab07c62011-02-07 12:19:14 +0100971 }
Marcelo Tosattie82bcec2010-05-04 09:45:22 -0300972
Paolo Bonzini376692b2015-07-10 12:32:32 +0200973 qemu_mutex_lock(&cpu->work_mutex);
974 while (cpu->queued_work_first != NULL) {
975 wi = cpu->queued_work_first;
Andreas Färberc64ca812012-05-03 02:11:45 +0200976 cpu->queued_work_first = wi->next;
Paolo Bonzini376692b2015-07-10 12:32:32 +0200977 if (!cpu->queued_work_first) {
978 cpu->queued_work_last = NULL;
979 }
980 qemu_mutex_unlock(&cpu->work_mutex);
Marcelo Tosattie82bcec2010-05-04 09:45:22 -0300981 wi->func(wi->data);
Paolo Bonzini376692b2015-07-10 12:32:32 +0200982 qemu_mutex_lock(&cpu->work_mutex);
Chegu Vinod3c022702013-06-24 03:49:41 -0600983 if (wi->free) {
984 g_free(wi);
Paolo Bonzini376692b2015-07-10 12:32:32 +0200985 } else {
986 atomic_mb_set(&wi->done, true);
Chegu Vinod3c022702013-06-24 03:49:41 -0600987 }
Marcelo Tosattie82bcec2010-05-04 09:45:22 -0300988 }
Paolo Bonzini376692b2015-07-10 12:32:32 +0200989 qemu_mutex_unlock(&cpu->work_mutex);
Marcelo Tosattie82bcec2010-05-04 09:45:22 -0300990 qemu_cond_broadcast(&qemu_work_cond);
991}
992
Andreas Färber509a0d72012-05-03 02:18:09 +0200993static void qemu_wait_io_event_common(CPUState *cpu)
Blue Swirl296af7c2010-03-29 19:23:50 +0000994{
Andreas Färber4fdeee72012-05-02 23:10:09 +0200995 if (cpu->stop) {
996 cpu->stop = false;
Andreas Färberf324e762012-05-02 23:26:21 +0200997 cpu->stopped = true;
Dr. David Alan Gilbert96bce682016-01-25 10:08:18 +0000998 qemu_cond_broadcast(&qemu_pause_cond);
Blue Swirl296af7c2010-03-29 19:23:50 +0000999 }
Andreas Färber6d45b102012-05-03 02:13:22 +02001000 flush_queued_work(cpu);
Andreas Färber216fc9a2012-05-02 17:49:49 +02001001 cpu->thread_kicked = false;
Blue Swirl296af7c2010-03-29 19:23:50 +00001002}
1003
KONRAD Fredericd5f8d612015-08-10 17:27:06 +02001004static void qemu_tcg_wait_io_event(CPUState *cpu)
Blue Swirl296af7c2010-03-29 19:23:50 +00001005{
Jan Kiszka16400322011-02-09 16:29:37 +01001006 while (all_cpu_threads_idle()) {
KONRAD Fredericd5f8d612015-08-10 17:27:06 +02001007 qemu_cond_wait(cpu->halt_cond, &qemu_global_mutex);
Jan Kiszka16400322011-02-09 16:29:37 +01001008 }
Blue Swirl296af7c2010-03-29 19:23:50 +00001009
Paolo Bonzini46daff12011-06-09 13:10:24 +02001010 while (iothread_requesting_mutex) {
1011 qemu_cond_wait(&qemu_io_proceeded_cond, &qemu_global_mutex);
1012 }
Jan Kiszka6cabe1f2010-06-25 16:56:53 +02001013
Andreas Färberbdc44642013-06-24 23:50:24 +02001014 CPU_FOREACH(cpu) {
Andreas Färber182735e2013-05-29 22:29:20 +02001015 qemu_wait_io_event_common(cpu);
Jan Kiszka6cabe1f2010-06-25 16:56:53 +02001016 }
Blue Swirl296af7c2010-03-29 19:23:50 +00001017}
1018
Andreas Färberfd529e82013-05-26 23:24:55 +02001019static void qemu_kvm_wait_io_event(CPUState *cpu)
Blue Swirl296af7c2010-03-29 19:23:50 +00001020{
Andreas Färbera98ae1d2013-05-26 23:21:08 +02001021 while (cpu_thread_is_idle(cpu)) {
Andreas Färberf5c121b2012-05-03 01:22:49 +02001022 qemu_cond_wait(cpu->halt_cond, &qemu_global_mutex);
Jan Kiszka16400322011-02-09 16:29:37 +01001023 }
Blue Swirl296af7c2010-03-29 19:23:50 +00001024
Andreas Färber290adf32013-01-17 09:30:27 +01001025 qemu_kvm_eat_signals(cpu);
Andreas Färber509a0d72012-05-03 02:18:09 +02001026 qemu_wait_io_event_common(cpu);
Blue Swirl296af7c2010-03-29 19:23:50 +00001027}
1028
Jan Kiszka7e97cd82011-02-07 12:19:12 +01001029static void *qemu_kvm_cpu_thread_fn(void *arg)
Blue Swirl296af7c2010-03-29 19:23:50 +00001030{
Andreas Färber48a106b2013-05-27 02:20:39 +02001031 CPUState *cpu = arg;
Jan Kiszka84b49152011-02-01 22:15:50 +01001032 int r;
Blue Swirl296af7c2010-03-29 19:23:50 +00001033
Paolo Bonziniab28bd22015-07-09 08:55:38 +02001034 rcu_register_thread();
1035
Paolo Bonzini2e7f7a32015-06-18 18:47:18 +02001036 qemu_mutex_lock_iothread();
Andreas Färber814e6122012-05-02 17:00:37 +02001037 qemu_thread_get_self(cpu->thread);
Andreas Färber9f09e182012-05-03 06:59:07 +02001038 cpu->thread_id = qemu_get_thread_id();
Pavel Dovgalyuk626cf8f2014-12-08 10:53:17 +03001039 cpu->can_do_io = 1;
Andreas Färber4917cf42013-05-27 05:17:50 +02001040 current_cpu = cpu;
Blue Swirl296af7c2010-03-29 19:23:50 +00001041
Andreas Färber504134d2012-12-17 06:38:45 +01001042 r = kvm_init_vcpu(cpu);
Jan Kiszka84b49152011-02-01 22:15:50 +01001043 if (r < 0) {
1044 fprintf(stderr, "kvm_init_vcpu failed: %s\n", strerror(-r));
1045 exit(1);
1046 }
Blue Swirl296af7c2010-03-29 19:23:50 +00001047
Andreas Färber13618e02013-05-26 23:41:00 +02001048 qemu_kvm_init_cpu_signals(cpu);
Blue Swirl296af7c2010-03-29 19:23:50 +00001049
1050 /* signal CPU creation */
Andreas Färber61a46212012-05-02 22:49:36 +02001051 cpu->created = true;
Blue Swirl296af7c2010-03-29 19:23:50 +00001052 qemu_cond_signal(&qemu_cpu_cond);
1053
Blue Swirl296af7c2010-03-29 19:23:50 +00001054 while (1) {
Andreas Färbera1fcaa72012-05-02 23:42:26 +02001055 if (cpu_can_run(cpu)) {
Andreas Färber1458c362013-05-26 23:46:55 +02001056 r = kvm_cpu_exec(cpu);
Jan Kiszka83f338f2011-02-07 12:19:17 +01001057 if (r == EXCP_DEBUG) {
Andreas Färber91325042013-05-27 02:07:49 +02001058 cpu_handle_guest_debug(cpu);
Jan Kiszka83f338f2011-02-07 12:19:17 +01001059 }
Jan Kiszka0ab07c62011-02-07 12:19:14 +01001060 }
Andreas Färberfd529e82013-05-26 23:24:55 +02001061 qemu_kvm_wait_io_event(cpu);
Blue Swirl296af7c2010-03-29 19:23:50 +00001062 }
1063
1064 return NULL;
1065}
1066
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +02001067static void *qemu_dummy_cpu_thread_fn(void *arg)
1068{
1069#ifdef _WIN32
1070 fprintf(stderr, "qtest is not supported under Windows\n");
1071 exit(1);
1072#else
Andreas Färber10a90212013-05-27 02:24:35 +02001073 CPUState *cpu = arg;
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +02001074 sigset_t waitset;
1075 int r;
1076
Paolo Bonziniab28bd22015-07-09 08:55:38 +02001077 rcu_register_thread();
1078
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +02001079 qemu_mutex_lock_iothread();
Andreas Färber814e6122012-05-02 17:00:37 +02001080 qemu_thread_get_self(cpu->thread);
Andreas Färber9f09e182012-05-03 06:59:07 +02001081 cpu->thread_id = qemu_get_thread_id();
Pavel Dovgalyuk626cf8f2014-12-08 10:53:17 +03001082 cpu->can_do_io = 1;
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +02001083
1084 sigemptyset(&waitset);
1085 sigaddset(&waitset, SIG_IPI);
1086
1087 /* signal CPU creation */
Andreas Färber61a46212012-05-02 22:49:36 +02001088 cpu->created = true;
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +02001089 qemu_cond_signal(&qemu_cpu_cond);
1090
Andreas Färber4917cf42013-05-27 05:17:50 +02001091 current_cpu = cpu;
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +02001092 while (1) {
Andreas Färber4917cf42013-05-27 05:17:50 +02001093 current_cpu = NULL;
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +02001094 qemu_mutex_unlock_iothread();
1095 do {
1096 int sig;
1097 r = sigwait(&waitset, &sig);
1098 } while (r == -1 && (errno == EAGAIN || errno == EINTR));
1099 if (r == -1) {
1100 perror("sigwait");
1101 exit(1);
1102 }
1103 qemu_mutex_lock_iothread();
Andreas Färber4917cf42013-05-27 05:17:50 +02001104 current_cpu = cpu;
Andreas Färber509a0d72012-05-03 02:18:09 +02001105 qemu_wait_io_event_common(cpu);
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +02001106 }
1107
1108 return NULL;
1109#endif
1110}
1111
Jan Kiszkabdb7ca62011-09-26 09:40:39 +02001112static void tcg_exec_all(void);
1113
Jan Kiszka7e97cd82011-02-07 12:19:12 +01001114static void *qemu_tcg_cpu_thread_fn(void *arg)
Blue Swirl296af7c2010-03-29 19:23:50 +00001115{
Andreas Färberc3586ba2012-05-03 01:41:24 +02001116 CPUState *cpu = arg;
Blue Swirl296af7c2010-03-29 19:23:50 +00001117
Paolo Bonziniab28bd22015-07-09 08:55:38 +02001118 rcu_register_thread();
1119
Paolo Bonzini2e7f7a32015-06-18 18:47:18 +02001120 qemu_mutex_lock_iothread();
Andreas Färber814e6122012-05-02 17:00:37 +02001121 qemu_thread_get_self(cpu->thread);
Blue Swirl296af7c2010-03-29 19:23:50 +00001122
Andreas Färber38fcbd32013-07-07 19:50:23 +02001123 CPU_FOREACH(cpu) {
1124 cpu->thread_id = qemu_get_thread_id();
1125 cpu->created = true;
Pavel Dovgalyuk626cf8f2014-12-08 10:53:17 +03001126 cpu->can_do_io = 1;
Andreas Färber38fcbd32013-07-07 19:50:23 +02001127 }
Blue Swirl296af7c2010-03-29 19:23:50 +00001128 qemu_cond_signal(&qemu_cpu_cond);
1129
Jan Kiszkafa7d1862011-08-22 18:35:25 +02001130 /* wait for initial kick-off after machine start */
Emilio G. Cotac28e3992015-04-27 12:45:28 -04001131 while (first_cpu->stopped) {
KONRAD Fredericd5f8d612015-08-10 17:27:06 +02001132 qemu_cond_wait(first_cpu->halt_cond, &qemu_global_mutex);
Jan Kiszka8e564b42012-02-17 18:31:15 +01001133
1134 /* process any pending work */
Andreas Färberbdc44642013-06-24 23:50:24 +02001135 CPU_FOREACH(cpu) {
Andreas Färber182735e2013-05-29 22:29:20 +02001136 qemu_wait_io_event_common(cpu);
Jan Kiszka8e564b42012-02-17 18:31:15 +01001137 }
Jan Kiszka0ab07c62011-02-07 12:19:14 +01001138 }
Blue Swirl296af7c2010-03-29 19:23:50 +00001139
Paolo Bonzini21618b32015-02-27 20:01:03 +01001140 /* process any pending work */
Paolo Bonziniaed807c2015-08-18 06:43:15 -07001141 atomic_mb_set(&exit_request, 1);
Paolo Bonzini21618b32015-02-27 20:01:03 +01001142
Blue Swirl296af7c2010-03-29 19:23:50 +00001143 while (1) {
Jan Kiszkabdb7ca62011-09-26 09:40:39 +02001144 tcg_exec_all();
Alex Blighac70aaf2013-08-21 16:02:57 +01001145
1146 if (use_icount) {
Alex Bligh40daca52013-08-21 16:03:02 +01001147 int64_t deadline = qemu_clock_deadline_ns_all(QEMU_CLOCK_VIRTUAL);
Alex Blighac70aaf2013-08-21 16:02:57 +01001148
1149 if (deadline == 0) {
Alex Bligh40daca52013-08-21 16:03:02 +01001150 qemu_clock_notify(QEMU_CLOCK_VIRTUAL);
Alex Blighac70aaf2013-08-21 16:02:57 +01001151 }
Paolo Bonzini3b2319a2011-04-13 10:03:43 +02001152 }
KONRAD Fredericd5f8d612015-08-10 17:27:06 +02001153 qemu_tcg_wait_io_event(QTAILQ_FIRST(&cpus));
Blue Swirl296af7c2010-03-29 19:23:50 +00001154 }
1155
1156 return NULL;
1157}
1158
Andreas Färber2ff09a42012-05-03 00:23:30 +02001159static void qemu_cpu_kick_thread(CPUState *cpu)
Paolo Bonzinicc015e92011-03-12 17:44:08 +01001160{
1161#ifndef _WIN32
1162 int err;
1163
Paolo Bonzinie0c38212015-08-26 00:19:19 +02001164 if (cpu->thread_kicked) {
1165 return;
Paolo Bonzini9102ded2015-08-18 06:52:09 -07001166 }
Paolo Bonzinie0c38212015-08-26 00:19:19 +02001167 cpu->thread_kicked = true;
Andreas Färber814e6122012-05-02 17:00:37 +02001168 err = pthread_kill(cpu->thread->thread, SIG_IPI);
Paolo Bonzinicc015e92011-03-12 17:44:08 +01001169 if (err) {
1170 fprintf(stderr, "qemu:%s: %s", __func__, strerror(err));
1171 exit(1);
1172 }
1173#else /* _WIN32 */
Paolo Bonzinie0c38212015-08-26 00:19:19 +02001174 abort();
Paolo Bonzinicc015e92011-03-12 17:44:08 +01001175#endif
1176}
1177
Paolo Bonzinie0c38212015-08-26 00:19:19 +02001178static void qemu_cpu_kick_no_halt(void)
1179{
1180 CPUState *cpu;
1181 /* Ensure whatever caused the exit has reached the CPU threads before
1182 * writing exit_request.
1183 */
1184 atomic_mb_set(&exit_request, 1);
1185 cpu = atomic_mb_read(&tcg_current_cpu);
1186 if (cpu) {
1187 cpu_exit(cpu);
1188 }
1189}
1190
Andreas Färberc08d7422012-05-03 04:34:15 +02001191void qemu_cpu_kick(CPUState *cpu)
Blue Swirl296af7c2010-03-29 19:23:50 +00001192{
Andreas Färberf5c121b2012-05-03 01:22:49 +02001193 qemu_cond_broadcast(cpu->halt_cond);
Paolo Bonzinie0c38212015-08-26 00:19:19 +02001194 if (tcg_enabled()) {
1195 qemu_cpu_kick_no_halt();
1196 } else {
1197 qemu_cpu_kick_thread(cpu);
1198 }
Blue Swirl296af7c2010-03-29 19:23:50 +00001199}
1200
Jan Kiszka46d62fa2011-02-01 22:15:59 +01001201void qemu_cpu_kick_self(void)
1202{
Andreas Färber4917cf42013-05-27 05:17:50 +02001203 assert(current_cpu);
Paolo Bonzini9102ded2015-08-18 06:52:09 -07001204 qemu_cpu_kick_thread(current_cpu);
Blue Swirl296af7c2010-03-29 19:23:50 +00001205}
1206
Andreas Färber60e82572012-05-02 22:23:49 +02001207bool qemu_cpu_is_self(CPUState *cpu)
Blue Swirl296af7c2010-03-29 19:23:50 +00001208{
Andreas Färber814e6122012-05-02 17:00:37 +02001209 return qemu_thread_is_self(cpu->thread);
Blue Swirl296af7c2010-03-29 19:23:50 +00001210}
1211
Paolo Bonzini79e2b9a2015-01-21 12:09:14 +01001212bool qemu_in_vcpu_thread(void)
Juan Quintelaaa723c22012-09-18 16:30:11 +02001213{
Andreas Färber4917cf42013-05-27 05:17:50 +02001214 return current_cpu && qemu_cpu_is_self(current_cpu);
Juan Quintelaaa723c22012-09-18 16:30:11 +02001215}
1216
Paolo Bonziniafbe7052015-06-18 18:47:19 +02001217static __thread bool iothread_locked = false;
1218
1219bool qemu_mutex_iothread_locked(void)
1220{
1221 return iothread_locked;
1222}
1223
Blue Swirl296af7c2010-03-29 19:23:50 +00001224void qemu_mutex_lock_iothread(void)
1225{
Paolo Bonzini21618b32015-02-27 20:01:03 +01001226 atomic_inc(&iothread_requesting_mutex);
Paolo Bonzini2e7f7a32015-06-18 18:47:18 +02001227 /* In the simple case there is no need to bump the VCPU thread out of
1228 * TCG code execution.
1229 */
1230 if (!tcg_enabled() || qemu_in_vcpu_thread() ||
Aníbal Limón46036b22015-09-03 15:48:33 -05001231 !first_cpu || !first_cpu->created) {
Blue Swirl296af7c2010-03-29 19:23:50 +00001232 qemu_mutex_lock(&qemu_global_mutex);
Paolo Bonzini21618b32015-02-27 20:01:03 +01001233 atomic_dec(&iothread_requesting_mutex);
Marcelo Tosatti1a28cac2010-05-04 09:45:20 -03001234 } else {
Marcelo Tosatti1a28cac2010-05-04 09:45:20 -03001235 if (qemu_mutex_trylock(&qemu_global_mutex)) {
Paolo Bonzinie0c38212015-08-26 00:19:19 +02001236 qemu_cpu_kick_no_halt();
Marcelo Tosatti1a28cac2010-05-04 09:45:20 -03001237 qemu_mutex_lock(&qemu_global_mutex);
1238 }
Paolo Bonzini6b498092015-02-27 19:58:23 +01001239 atomic_dec(&iothread_requesting_mutex);
Paolo Bonzini46daff12011-06-09 13:10:24 +02001240 qemu_cond_broadcast(&qemu_io_proceeded_cond);
Marcelo Tosatti1a28cac2010-05-04 09:45:20 -03001241 }
Paolo Bonziniafbe7052015-06-18 18:47:19 +02001242 iothread_locked = true;
Blue Swirl296af7c2010-03-29 19:23:50 +00001243}
1244
1245void qemu_mutex_unlock_iothread(void)
1246{
Paolo Bonziniafbe7052015-06-18 18:47:19 +02001247 iothread_locked = false;
Blue Swirl296af7c2010-03-29 19:23:50 +00001248 qemu_mutex_unlock(&qemu_global_mutex);
1249}
1250
1251static int all_vcpus_paused(void)
1252{
Andreas Färberbdc44642013-06-24 23:50:24 +02001253 CPUState *cpu;
Blue Swirl296af7c2010-03-29 19:23:50 +00001254
Andreas Färberbdc44642013-06-24 23:50:24 +02001255 CPU_FOREACH(cpu) {
Andreas Färber182735e2013-05-29 22:29:20 +02001256 if (!cpu->stopped) {
Blue Swirl296af7c2010-03-29 19:23:50 +00001257 return 0;
Jan Kiszka0ab07c62011-02-07 12:19:14 +01001258 }
Blue Swirl296af7c2010-03-29 19:23:50 +00001259 }
1260
1261 return 1;
1262}
1263
1264void pause_all_vcpus(void)
1265{
Andreas Färberbdc44642013-06-24 23:50:24 +02001266 CPUState *cpu;
Blue Swirl296af7c2010-03-29 19:23:50 +00001267
Alex Bligh40daca52013-08-21 16:03:02 +01001268 qemu_clock_enable(QEMU_CLOCK_VIRTUAL, false);
Andreas Färberbdc44642013-06-24 23:50:24 +02001269 CPU_FOREACH(cpu) {
Andreas Färber182735e2013-05-29 22:29:20 +02001270 cpu->stop = true;
1271 qemu_cpu_kick(cpu);
Blue Swirl296af7c2010-03-29 19:23:50 +00001272 }
1273
Juan Quintelaaa723c22012-09-18 16:30:11 +02001274 if (qemu_in_vcpu_thread()) {
Jan Kiszkad798e972012-02-17 18:31:16 +01001275 cpu_stop_current();
1276 if (!kvm_enabled()) {
Andreas Färberbdc44642013-06-24 23:50:24 +02001277 CPU_FOREACH(cpu) {
Andreas Färber182735e2013-05-29 22:29:20 +02001278 cpu->stop = false;
1279 cpu->stopped = true;
Jan Kiszkad798e972012-02-17 18:31:16 +01001280 }
1281 return;
1282 }
1283 }
1284
Blue Swirl296af7c2010-03-29 19:23:50 +00001285 while (!all_vcpus_paused()) {
Paolo Bonzinibe7d6c52011-03-12 17:44:02 +01001286 qemu_cond_wait(&qemu_pause_cond, &qemu_global_mutex);
Andreas Färberbdc44642013-06-24 23:50:24 +02001287 CPU_FOREACH(cpu) {
Andreas Färber182735e2013-05-29 22:29:20 +02001288 qemu_cpu_kick(cpu);
Blue Swirl296af7c2010-03-29 19:23:50 +00001289 }
1290 }
1291}
1292
Igor Mammedov29936832013-04-23 10:29:37 +02001293void cpu_resume(CPUState *cpu)
1294{
1295 cpu->stop = false;
1296 cpu->stopped = false;
1297 qemu_cpu_kick(cpu);
1298}
1299
Blue Swirl296af7c2010-03-29 19:23:50 +00001300void resume_all_vcpus(void)
1301{
Andreas Färberbdc44642013-06-24 23:50:24 +02001302 CPUState *cpu;
Blue Swirl296af7c2010-03-29 19:23:50 +00001303
Alex Bligh40daca52013-08-21 16:03:02 +01001304 qemu_clock_enable(QEMU_CLOCK_VIRTUAL, true);
Andreas Färberbdc44642013-06-24 23:50:24 +02001305 CPU_FOREACH(cpu) {
Andreas Färber182735e2013-05-29 22:29:20 +02001306 cpu_resume(cpu);
Blue Swirl296af7c2010-03-29 19:23:50 +00001307 }
1308}
1309
Dr. David Alan Gilbert49001162014-01-30 10:20:32 +00001310/* For temporary buffers for forming a name */
1311#define VCPU_THREAD_NAME_SIZE 16
1312
Andreas Färbere5ab30a2012-05-03 01:50:44 +02001313static void qemu_tcg_init_vcpu(CPUState *cpu)
Blue Swirl296af7c2010-03-29 19:23:50 +00001314{
Dr. David Alan Gilbert49001162014-01-30 10:20:32 +00001315 char thread_name[VCPU_THREAD_NAME_SIZE];
KONRAD Fredericd5f8d612015-08-10 17:27:06 +02001316 static QemuCond *tcg_halt_cond;
1317 static QemuThread *tcg_cpu_thread;
Dr. David Alan Gilbert49001162014-01-30 10:20:32 +00001318
Blue Swirl296af7c2010-03-29 19:23:50 +00001319 /* share a single thread for all cpus with TCG */
1320 if (!tcg_cpu_thread) {
Andreas Färber814e6122012-05-02 17:00:37 +02001321 cpu->thread = g_malloc0(sizeof(QemuThread));
Andreas Färberf5c121b2012-05-03 01:22:49 +02001322 cpu->halt_cond = g_malloc0(sizeof(QemuCond));
1323 qemu_cond_init(cpu->halt_cond);
1324 tcg_halt_cond = cpu->halt_cond;
Dr. David Alan Gilbert49001162014-01-30 10:20:32 +00001325 snprintf(thread_name, VCPU_THREAD_NAME_SIZE, "CPU %d/TCG",
1326 cpu->cpu_index);
1327 qemu_thread_create(cpu->thread, thread_name, qemu_tcg_cpu_thread_fn,
1328 cpu, QEMU_THREAD_JOINABLE);
Paolo Bonzini1ecf47b2011-12-13 13:43:52 +01001329#ifdef _WIN32
Andreas Färber814e6122012-05-02 17:00:37 +02001330 cpu->hThread = qemu_thread_get_handle(cpu->thread);
Paolo Bonzini1ecf47b2011-12-13 13:43:52 +01001331#endif
Andreas Färber61a46212012-05-02 22:49:36 +02001332 while (!cpu->created) {
Paolo Bonzini18a85722011-03-12 17:44:03 +01001333 qemu_cond_wait(&qemu_cpu_cond, &qemu_global_mutex);
Jan Kiszka0ab07c62011-02-07 12:19:14 +01001334 }
Andreas Färber814e6122012-05-02 17:00:37 +02001335 tcg_cpu_thread = cpu->thread;
Blue Swirl296af7c2010-03-29 19:23:50 +00001336 } else {
Andreas Färber814e6122012-05-02 17:00:37 +02001337 cpu->thread = tcg_cpu_thread;
Andreas Färberf5c121b2012-05-03 01:22:49 +02001338 cpu->halt_cond = tcg_halt_cond;
Blue Swirl296af7c2010-03-29 19:23:50 +00001339 }
1340}
1341
Andreas Färber48a106b2013-05-27 02:20:39 +02001342static void qemu_kvm_start_vcpu(CPUState *cpu)
Blue Swirl296af7c2010-03-29 19:23:50 +00001343{
Dr. David Alan Gilbert49001162014-01-30 10:20:32 +00001344 char thread_name[VCPU_THREAD_NAME_SIZE];
1345
Andreas Färber814e6122012-05-02 17:00:37 +02001346 cpu->thread = g_malloc0(sizeof(QemuThread));
Andreas Färberf5c121b2012-05-03 01:22:49 +02001347 cpu->halt_cond = g_malloc0(sizeof(QemuCond));
1348 qemu_cond_init(cpu->halt_cond);
Dr. David Alan Gilbert49001162014-01-30 10:20:32 +00001349 snprintf(thread_name, VCPU_THREAD_NAME_SIZE, "CPU %d/KVM",
1350 cpu->cpu_index);
1351 qemu_thread_create(cpu->thread, thread_name, qemu_kvm_cpu_thread_fn,
1352 cpu, QEMU_THREAD_JOINABLE);
Andreas Färber61a46212012-05-02 22:49:36 +02001353 while (!cpu->created) {
Paolo Bonzini18a85722011-03-12 17:44:03 +01001354 qemu_cond_wait(&qemu_cpu_cond, &qemu_global_mutex);
Jan Kiszka0ab07c62011-02-07 12:19:14 +01001355 }
Blue Swirl296af7c2010-03-29 19:23:50 +00001356}
1357
Andreas Färber10a90212013-05-27 02:24:35 +02001358static void qemu_dummy_start_vcpu(CPUState *cpu)
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +02001359{
Dr. David Alan Gilbert49001162014-01-30 10:20:32 +00001360 char thread_name[VCPU_THREAD_NAME_SIZE];
1361
Andreas Färber814e6122012-05-02 17:00:37 +02001362 cpu->thread = g_malloc0(sizeof(QemuThread));
Andreas Färberf5c121b2012-05-03 01:22:49 +02001363 cpu->halt_cond = g_malloc0(sizeof(QemuCond));
1364 qemu_cond_init(cpu->halt_cond);
Dr. David Alan Gilbert49001162014-01-30 10:20:32 +00001365 snprintf(thread_name, VCPU_THREAD_NAME_SIZE, "CPU %d/DUMMY",
1366 cpu->cpu_index);
1367 qemu_thread_create(cpu->thread, thread_name, qemu_dummy_cpu_thread_fn, cpu,
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +02001368 QEMU_THREAD_JOINABLE);
Andreas Färber61a46212012-05-02 22:49:36 +02001369 while (!cpu->created) {
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +02001370 qemu_cond_wait(&qemu_cpu_cond, &qemu_global_mutex);
1371 }
1372}
1373
Andreas Färberc643bed2013-05-27 03:23:24 +02001374void qemu_init_vcpu(CPUState *cpu)
Blue Swirl296af7c2010-03-29 19:23:50 +00001375{
Andreas Färberce3960e2012-12-17 03:27:07 +01001376 cpu->nr_cores = smp_cores;
1377 cpu->nr_threads = smp_threads;
Andreas Färberf324e762012-05-02 23:26:21 +02001378 cpu->stopped = true;
Peter Maydell56943e82016-01-21 14:15:04 +00001379
1380 if (!cpu->as) {
1381 /* If the target cpu hasn't set up any address spaces itself,
1382 * give it the default one.
1383 */
Peter Crosthwaite6731d862016-01-21 14:15:06 +00001384 AddressSpace *as = address_space_init_shareable(cpu->memory,
1385 "cpu-memory");
Peter Maydell12ebc9a2016-01-21 14:15:04 +00001386 cpu->num_ases = 1;
Peter Crosthwaite6731d862016-01-21 14:15:06 +00001387 cpu_address_space_init(cpu, as, 0);
Peter Maydell56943e82016-01-21 14:15:04 +00001388 }
1389
Jan Kiszka0ab07c62011-02-07 12:19:14 +01001390 if (kvm_enabled()) {
Andreas Färber48a106b2013-05-27 02:20:39 +02001391 qemu_kvm_start_vcpu(cpu);
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +02001392 } else if (tcg_enabled()) {
Andreas Färbere5ab30a2012-05-03 01:50:44 +02001393 qemu_tcg_init_vcpu(cpu);
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +02001394 } else {
Andreas Färber10a90212013-05-27 02:24:35 +02001395 qemu_dummy_start_vcpu(cpu);
Jan Kiszka0ab07c62011-02-07 12:19:14 +01001396 }
Blue Swirl296af7c2010-03-29 19:23:50 +00001397}
1398
Jan Kiszkab4a3d962011-02-01 22:15:43 +01001399void cpu_stop_current(void)
Blue Swirl296af7c2010-03-29 19:23:50 +00001400{
Andreas Färber4917cf42013-05-27 05:17:50 +02001401 if (current_cpu) {
1402 current_cpu->stop = false;
1403 current_cpu->stopped = true;
1404 cpu_exit(current_cpu);
Dr. David Alan Gilbert96bce682016-01-25 10:08:18 +00001405 qemu_cond_broadcast(&qemu_pause_cond);
Jan Kiszkab4a3d962011-02-01 22:15:43 +01001406 }
Blue Swirl296af7c2010-03-29 19:23:50 +00001407}
1408
Kevin Wolf56983462013-07-05 13:49:54 +02001409int vm_stop(RunState state)
Blue Swirl296af7c2010-03-29 19:23:50 +00001410{
Juan Quintelaaa723c22012-09-18 16:30:11 +02001411 if (qemu_in_vcpu_thread()) {
Paolo Bonzini74892d22014-06-05 14:53:58 +02001412 qemu_system_vmstop_request_prepare();
Luiz Capitulino1dfb4dd2011-07-29 14:26:33 -03001413 qemu_system_vmstop_request(state);
Blue Swirl296af7c2010-03-29 19:23:50 +00001414 /*
1415 * FIXME: should not return to device code in case
1416 * vm_stop() has been requested.
1417 */
Jan Kiszkab4a3d962011-02-01 22:15:43 +01001418 cpu_stop_current();
Kevin Wolf56983462013-07-05 13:49:54 +02001419 return 0;
Blue Swirl296af7c2010-03-29 19:23:50 +00001420 }
Kevin Wolf56983462013-07-05 13:49:54 +02001421
1422 return do_vm_stop(state);
Blue Swirl296af7c2010-03-29 19:23:50 +00001423}
1424
Luiz Capitulino8a9236f2011-10-14 11:18:09 -03001425/* does a state transition even if the VM is already stopped,
1426 current state is forgotten forever */
Kevin Wolf56983462013-07-05 13:49:54 +02001427int vm_stop_force_state(RunState state)
Luiz Capitulino8a9236f2011-10-14 11:18:09 -03001428{
1429 if (runstate_is_running()) {
Kevin Wolf56983462013-07-05 13:49:54 +02001430 return vm_stop(state);
Luiz Capitulino8a9236f2011-10-14 11:18:09 -03001431 } else {
1432 runstate_set(state);
Wen Congyangb2780d32015-11-20 17:34:38 +08001433
1434 bdrv_drain_all();
Kevin Wolf594a45c2013-07-18 14:52:19 +02001435 /* Make sure to return an error if the flush in a previous vm_stop()
1436 * failed. */
Max Reitzda31d592016-03-16 19:54:32 +01001437 return blk_flush_all();
Luiz Capitulino8a9236f2011-10-14 11:18:09 -03001438 }
1439}
1440
Pavel Dovgalyuk8b427042015-09-17 19:24:05 +03001441static int64_t tcg_get_icount_limit(void)
1442{
1443 int64_t deadline;
1444
1445 if (replay_mode != REPLAY_MODE_PLAY) {
1446 deadline = qemu_clock_deadline_ns_all(QEMU_CLOCK_VIRTUAL);
1447
1448 /* Maintain prior (possibly buggy) behaviour where if no deadline
1449 * was set (as there is no QEMU_CLOCK_VIRTUAL timer) or it is more than
1450 * INT32_MAX nanoseconds ahead, we still use INT32_MAX
1451 * nanoseconds.
1452 */
1453 if ((deadline < 0) || (deadline > INT32_MAX)) {
1454 deadline = INT32_MAX;
1455 }
1456
1457 return qemu_icount_round(deadline);
1458 } else {
1459 return replay_get_instructions();
1460 }
1461}
1462
Peter Crosthwaite3d57f782015-06-23 19:31:17 -07001463static int tcg_cpu_exec(CPUState *cpu)
Blue Swirl296af7c2010-03-29 19:23:50 +00001464{
1465 int ret;
1466#ifdef CONFIG_PROFILER
1467 int64_t ti;
1468#endif
1469
1470#ifdef CONFIG_PROFILER
1471 ti = profile_getclock();
1472#endif
1473 if (use_icount) {
1474 int64_t count;
1475 int decr;
KONRAD Fredericc96778b2014-08-01 01:37:09 +02001476 timers_state.qemu_icount -= (cpu->icount_decr.u16.low
1477 + cpu->icount_extra);
Andreas Färber28ecfd72013-08-26 05:51:49 +02001478 cpu->icount_decr.u16.low = 0;
Andreas Färberefee7342013-08-26 05:39:29 +02001479 cpu->icount_extra = 0;
Pavel Dovgalyuk8b427042015-09-17 19:24:05 +03001480 count = tcg_get_icount_limit();
KONRAD Fredericc96778b2014-08-01 01:37:09 +02001481 timers_state.qemu_icount += count;
Blue Swirl296af7c2010-03-29 19:23:50 +00001482 decr = (count > 0xffff) ? 0xffff : count;
1483 count -= decr;
Andreas Färber28ecfd72013-08-26 05:51:49 +02001484 cpu->icount_decr.u16.low = decr;
Andreas Färberefee7342013-08-26 05:39:29 +02001485 cpu->icount_extra = count;
Blue Swirl296af7c2010-03-29 19:23:50 +00001486 }
Peter Crosthwaiteea3e9842015-06-18 10:24:55 -07001487 ret = cpu_exec(cpu);
Blue Swirl296af7c2010-03-29 19:23:50 +00001488#ifdef CONFIG_PROFILER
Alexey Kardashevskiy89d5cbd2015-03-16 14:57:38 +11001489 tcg_time += profile_getclock() - ti;
Blue Swirl296af7c2010-03-29 19:23:50 +00001490#endif
1491 if (use_icount) {
1492 /* Fold pending instructions back into the
1493 instruction counter, and clear the interrupt flag. */
KONRAD Fredericc96778b2014-08-01 01:37:09 +02001494 timers_state.qemu_icount -= (cpu->icount_decr.u16.low
1495 + cpu->icount_extra);
Andreas Färber28ecfd72013-08-26 05:51:49 +02001496 cpu->icount_decr.u32 = 0;
Andreas Färberefee7342013-08-26 05:39:29 +02001497 cpu->icount_extra = 0;
Pavel Dovgalyuk8b427042015-09-17 19:24:05 +03001498 replay_account_executed_instructions();
Blue Swirl296af7c2010-03-29 19:23:50 +00001499 }
1500 return ret;
1501}
1502
Jan Kiszkabdb7ca62011-09-26 09:40:39 +02001503static void tcg_exec_all(void)
Blue Swirl296af7c2010-03-29 19:23:50 +00001504{
Jan Kiszka9a360852011-02-01 22:15:55 +01001505 int r;
1506
Alex Bligh40daca52013-08-21 16:03:02 +01001507 /* Account partial waits to QEMU_CLOCK_VIRTUAL. */
Pavel Dovgalyuke76d1792016-03-10 14:56:09 +03001508 qemu_account_warp_timer();
Paolo Bonziniab33fcd2011-04-13 10:03:44 +02001509
Jan Kiszka0ab07c62011-02-07 12:19:14 +01001510 if (next_cpu == NULL) {
Blue Swirl296af7c2010-03-29 19:23:50 +00001511 next_cpu = first_cpu;
Jan Kiszka0ab07c62011-02-07 12:19:14 +01001512 }
Andreas Färberbdc44642013-06-24 23:50:24 +02001513 for (; next_cpu != NULL && !exit_request; next_cpu = CPU_NEXT(next_cpu)) {
Andreas Färber182735e2013-05-29 22:29:20 +02001514 CPUState *cpu = next_cpu;
Blue Swirl296af7c2010-03-29 19:23:50 +00001515
Alex Bligh40daca52013-08-21 16:03:02 +01001516 qemu_clock_enable(QEMU_CLOCK_VIRTUAL,
Andreas Färbered2803d2013-06-21 20:20:45 +02001517 (cpu->singlestep_enabled & SSTEP_NOTIMER) == 0);
Blue Swirl296af7c2010-03-29 19:23:50 +00001518
Andreas Färbera1fcaa72012-05-02 23:42:26 +02001519 if (cpu_can_run(cpu)) {
Peter Crosthwaite3d57f782015-06-23 19:31:17 -07001520 r = tcg_cpu_exec(cpu);
Jan Kiszka9a360852011-02-01 22:15:55 +01001521 if (r == EXCP_DEBUG) {
Andreas Färber91325042013-05-27 02:07:49 +02001522 cpu_handle_guest_debug(cpu);
Jan Kiszka3c638d02010-06-25 16:56:56 +02001523 break;
1524 }
Andreas Färberf324e762012-05-02 23:26:21 +02001525 } else if (cpu->stop || cpu->stopped) {
Blue Swirl296af7c2010-03-29 19:23:50 +00001526 break;
1527 }
1528 }
Paolo Bonziniaed807c2015-08-18 06:43:15 -07001529
1530 /* Pairs with smp_wmb in qemu_cpu_kick. */
1531 atomic_mb_set(&exit_request, 0);
Blue Swirl296af7c2010-03-29 19:23:50 +00001532}
1533
Stefan Weil9a78eea2010-10-22 23:03:33 +02001534void list_cpus(FILE *f, fprintf_function cpu_fprintf, const char *optarg)
Blue Swirl262353c2010-05-04 19:55:35 +00001535{
1536 /* XXX: implement xxx_cpu_list for targets that still miss it */
Peter Maydelle916cbf2012-09-05 17:41:08 -03001537#if defined(cpu_list)
1538 cpu_list(f, cpu_fprintf);
Blue Swirl262353c2010-05-04 19:55:35 +00001539#endif
1540}
Luiz Capitulinode0b36b2011-09-21 16:38:35 -03001541
1542CpuInfoList *qmp_query_cpus(Error **errp)
1543{
1544 CpuInfoList *head = NULL, *cur_item = NULL;
Andreas Färber182735e2013-05-29 22:29:20 +02001545 CPUState *cpu;
Luiz Capitulinode0b36b2011-09-21 16:38:35 -03001546
Andreas Färberbdc44642013-06-24 23:50:24 +02001547 CPU_FOREACH(cpu) {
Luiz Capitulinode0b36b2011-09-21 16:38:35 -03001548 CpuInfoList *info;
Andreas Färber182735e2013-05-29 22:29:20 +02001549#if defined(TARGET_I386)
1550 X86CPU *x86_cpu = X86_CPU(cpu);
1551 CPUX86State *env = &x86_cpu->env;
1552#elif defined(TARGET_PPC)
1553 PowerPCCPU *ppc_cpu = POWERPC_CPU(cpu);
1554 CPUPPCState *env = &ppc_cpu->env;
1555#elif defined(TARGET_SPARC)
1556 SPARCCPU *sparc_cpu = SPARC_CPU(cpu);
1557 CPUSPARCState *env = &sparc_cpu->env;
1558#elif defined(TARGET_MIPS)
1559 MIPSCPU *mips_cpu = MIPS_CPU(cpu);
1560 CPUMIPSState *env = &mips_cpu->env;
Bastian Koppelmann48e06fe2014-09-01 12:59:46 +01001561#elif defined(TARGET_TRICORE)
1562 TriCoreCPU *tricore_cpu = TRICORE_CPU(cpu);
1563 CPUTriCoreState *env = &tricore_cpu->env;
Andreas Färber182735e2013-05-29 22:29:20 +02001564#endif
Luiz Capitulinode0b36b2011-09-21 16:38:35 -03001565
Andreas Färbercb446ec2013-05-01 14:24:52 +02001566 cpu_synchronize_state(cpu);
Luiz Capitulinode0b36b2011-09-21 16:38:35 -03001567
1568 info = g_malloc0(sizeof(*info));
1569 info->value = g_malloc0(sizeof(*info->value));
Andreas Färber55e5c282012-12-17 06:18:02 +01001570 info->value->CPU = cpu->cpu_index;
Andreas Färber182735e2013-05-29 22:29:20 +02001571 info->value->current = (cpu == first_cpu);
Andreas Färber259186a2013-01-17 18:51:17 +01001572 info->value->halted = cpu->halted;
Eduardo Habkost58f88d42015-05-08 16:04:22 -03001573 info->value->qom_path = object_get_canonical_path(OBJECT(cpu));
Andreas Färber9f09e182012-05-03 06:59:07 +02001574 info->value->thread_id = cpu->thread_id;
Luiz Capitulinode0b36b2011-09-21 16:38:35 -03001575#if defined(TARGET_I386)
Eric Blake86f4b682015-11-18 01:52:59 -07001576 info->value->arch = CPU_INFO_ARCH_X86;
Eric Blake544a3732016-02-17 23:48:27 -07001577 info->value->u.x86.pc = env->eip + env->segs[R_CS].base;
Luiz Capitulinode0b36b2011-09-21 16:38:35 -03001578#elif defined(TARGET_PPC)
Eric Blake86f4b682015-11-18 01:52:59 -07001579 info->value->arch = CPU_INFO_ARCH_PPC;
Eric Blake544a3732016-02-17 23:48:27 -07001580 info->value->u.ppc.nip = env->nip;
Luiz Capitulinode0b36b2011-09-21 16:38:35 -03001581#elif defined(TARGET_SPARC)
Eric Blake86f4b682015-11-18 01:52:59 -07001582 info->value->arch = CPU_INFO_ARCH_SPARC;
Eric Blake544a3732016-02-17 23:48:27 -07001583 info->value->u.q_sparc.pc = env->pc;
1584 info->value->u.q_sparc.npc = env->npc;
Luiz Capitulinode0b36b2011-09-21 16:38:35 -03001585#elif defined(TARGET_MIPS)
Eric Blake86f4b682015-11-18 01:52:59 -07001586 info->value->arch = CPU_INFO_ARCH_MIPS;
Eric Blake544a3732016-02-17 23:48:27 -07001587 info->value->u.q_mips.PC = env->active_tc.PC;
Bastian Koppelmann48e06fe2014-09-01 12:59:46 +01001588#elif defined(TARGET_TRICORE)
Eric Blake86f4b682015-11-18 01:52:59 -07001589 info->value->arch = CPU_INFO_ARCH_TRICORE;
Eric Blake544a3732016-02-17 23:48:27 -07001590 info->value->u.tricore.PC = env->PC;
Eric Blake86f4b682015-11-18 01:52:59 -07001591#else
1592 info->value->arch = CPU_INFO_ARCH_OTHER;
Luiz Capitulinode0b36b2011-09-21 16:38:35 -03001593#endif
1594
1595 /* XXX: waiting for the qapi to support GSList */
1596 if (!cur_item) {
1597 head = cur_item = info;
1598 } else {
1599 cur_item->next = info;
1600 cur_item = info;
1601 }
1602 }
1603
1604 return head;
1605}
Luiz Capitulino0cfd6a92011-11-22 16:32:37 -02001606
1607void qmp_memsave(int64_t addr, int64_t size, const char *filename,
1608 bool has_cpu, int64_t cpu_index, Error **errp)
1609{
1610 FILE *f;
1611 uint32_t l;
Andreas Färber55e5c282012-12-17 06:18:02 +01001612 CPUState *cpu;
Luiz Capitulino0cfd6a92011-11-22 16:32:37 -02001613 uint8_t buf[1024];
Borislav Petkov0dc9daf2015-02-08 13:14:38 +01001614 int64_t orig_addr = addr, orig_size = size;
Luiz Capitulino0cfd6a92011-11-22 16:32:37 -02001615
1616 if (!has_cpu) {
1617 cpu_index = 0;
1618 }
1619
Andreas Färber151d1322013-02-15 15:41:49 +01001620 cpu = qemu_get_cpu(cpu_index);
1621 if (cpu == NULL) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001622 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "cpu-index",
1623 "a CPU number");
Luiz Capitulino0cfd6a92011-11-22 16:32:37 -02001624 return;
1625 }
1626
1627 f = fopen(filename, "wb");
1628 if (!f) {
Luiz Capitulino618da852013-06-07 14:35:06 -04001629 error_setg_file_open(errp, errno, filename);
Luiz Capitulino0cfd6a92011-11-22 16:32:37 -02001630 return;
1631 }
1632
1633 while (size != 0) {
1634 l = sizeof(buf);
1635 if (l > size)
1636 l = size;
Aneesh Kumar K.V2f4d0f52013-10-01 21:49:30 +05301637 if (cpu_memory_rw_debug(cpu, addr, buf, l, 0) != 0) {
Borislav Petkov0dc9daf2015-02-08 13:14:38 +01001638 error_setg(errp, "Invalid addr 0x%016" PRIx64 "/size %" PRId64
1639 " specified", orig_addr, orig_size);
Aneesh Kumar K.V2f4d0f52013-10-01 21:49:30 +05301640 goto exit;
1641 }
Luiz Capitulino0cfd6a92011-11-22 16:32:37 -02001642 if (fwrite(buf, 1, l, f) != l) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001643 error_setg(errp, QERR_IO_ERROR);
Luiz Capitulino0cfd6a92011-11-22 16:32:37 -02001644 goto exit;
1645 }
1646 addr += l;
1647 size -= l;
1648 }
1649
1650exit:
1651 fclose(f);
1652}
Luiz Capitulino6d3962b2011-11-22 17:26:46 -02001653
1654void qmp_pmemsave(int64_t addr, int64_t size, const char *filename,
1655 Error **errp)
1656{
1657 FILE *f;
1658 uint32_t l;
1659 uint8_t buf[1024];
1660
1661 f = fopen(filename, "wb");
1662 if (!f) {
Luiz Capitulino618da852013-06-07 14:35:06 -04001663 error_setg_file_open(errp, errno, filename);
Luiz Capitulino6d3962b2011-11-22 17:26:46 -02001664 return;
1665 }
1666
1667 while (size != 0) {
1668 l = sizeof(buf);
1669 if (l > size)
1670 l = size;
Stefan Weileb6282f2014-04-07 20:28:23 +02001671 cpu_physical_memory_read(addr, buf, l);
Luiz Capitulino6d3962b2011-11-22 17:26:46 -02001672 if (fwrite(buf, 1, l, f) != l) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001673 error_setg(errp, QERR_IO_ERROR);
Luiz Capitulino6d3962b2011-11-22 17:26:46 -02001674 goto exit;
1675 }
1676 addr += l;
1677 size -= l;
1678 }
1679
1680exit:
1681 fclose(f);
1682}
Luiz Capitulinoab49ab52011-11-23 12:55:53 -02001683
1684void qmp_inject_nmi(Error **errp)
1685{
1686#if defined(TARGET_I386)
Andreas Färber182735e2013-05-29 22:29:20 +02001687 CPUState *cs;
Luiz Capitulinoab49ab52011-11-23 12:55:53 -02001688
Andreas Färberbdc44642013-06-24 23:50:24 +02001689 CPU_FOREACH(cs) {
Andreas Färber182735e2013-05-29 22:29:20 +02001690 X86CPU *cpu = X86_CPU(cs);
Andreas Färber182735e2013-05-29 22:29:20 +02001691
Chen Fan02e51482013-12-23 17:04:02 +08001692 if (!cpu->apic_state) {
Andreas Färber182735e2013-05-29 22:29:20 +02001693 cpu_interrupt(cs, CPU_INTERRUPT_NMI);
Jan Kiszka02c09192011-10-18 00:00:06 +08001694 } else {
Chen Fan02e51482013-12-23 17:04:02 +08001695 apic_deliver_nmi(cpu->apic_state);
Jan Kiszka02c09192011-10-18 00:00:06 +08001696 }
Luiz Capitulinoab49ab52011-11-23 12:55:53 -02001697 }
1698#else
Alexey Kardashevskiy9cb805f2014-08-20 22:16:33 +10001699 nmi_monitor_handle(monitor_get_cpu_index(), errp);
Luiz Capitulinoab49ab52011-11-23 12:55:53 -02001700#endif
1701}
Sebastian Tanase27498be2014-07-25 11:56:33 +02001702
1703void dump_drift_info(FILE *f, fprintf_function cpu_fprintf)
1704{
1705 if (!use_icount) {
1706 return;
1707 }
1708
1709 cpu_fprintf(f, "Host - Guest clock %"PRIi64" ms\n",
1710 (cpu_get_clock() - cpu_get_icount())/SCALE_MS);
1711 if (icount_align_option) {
1712 cpu_fprintf(f, "Max guest delay %"PRIi64" ms\n", -max_delay/SCALE_MS);
1713 cpu_fprintf(f, "Max guest advance %"PRIi64" ms\n", max_advance/SCALE_MS);
1714 } else {
1715 cpu_fprintf(f, "Max guest delay NA\n");
1716 cpu_fprintf(f, "Max guest advance NA\n");
1717 }
1718}