blob: 43676fa1f31622b002387e7da02cfbcecdc8e85a [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. */
26#include "config-host.h"
27
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"
Paolo Bonzini022c62c2012-12-17 18:19:49 +010032#include "exec/gdbstub.h"
Paolo Bonzini9c17d612012-12-17 18:20:04 +010033#include "sysemu/dma.h"
34#include "sysemu/kvm.h"
Luiz Capitulinode0b36b2011-09-21 16:38:35 -030035#include "qmp-commands.h"
Blue Swirl296af7c2010-03-29 19:23:50 +000036
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010037#include "qemu/thread.h"
Paolo Bonzini9c17d612012-12-17 18:20:04 +010038#include "sysemu/cpus.h"
39#include "sysemu/qtest.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010040#include "qemu/main-loop.h"
41#include "qemu/bitmap.h"
Liu Ping Fancb365642013-09-25 14:20:58 +080042#include "qemu/seqlock.h"
Wenchao Xiaa4e15de2014-06-18 08:43:36 +020043#include "qapi-event.h"
Alexey Kardashevskiy9cb805f2014-08-20 22:16:33 +100044#include "hw/nmi.h"
Pavel Dovgalyuk8b427042015-09-17 19:24:05 +030045#include "sysemu/replay.h"
Jan Kiszka0ff0fc12011-06-23 10:15:55 +020046
47#ifndef _WIN32
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010048#include "qemu/compatfd.h"
Jan Kiszka0ff0fc12011-06-23 10:15:55 +020049#endif
Blue Swirl296af7c2010-03-29 19:23:50 +000050
Jan Kiszka6d9cb732011-02-01 22:15:58 +010051#ifdef CONFIG_LINUX
52
53#include <sys/prctl.h>
54
Marcelo Tosattic0532a72010-10-11 15:31:21 -030055#ifndef PR_MCE_KILL
56#define PR_MCE_KILL 33
57#endif
58
Jan Kiszka6d9cb732011-02-01 22:15:58 +010059#ifndef PR_MCE_KILL_SET
60#define PR_MCE_KILL_SET 1
61#endif
62
63#ifndef PR_MCE_KILL_EARLY
64#define PR_MCE_KILL_EARLY 1
65#endif
66
67#endif /* CONFIG_LINUX */
68
Andreas Färber182735e2013-05-29 22:29:20 +020069static CPUState *next_cpu;
Sebastian Tanase27498be2014-07-25 11:56:33 +020070int64_t max_delay;
71int64_t max_advance;
Blue Swirl296af7c2010-03-29 19:23:50 +000072
Jason J. Herne2adcc852015-09-08 13:12:33 -040073/* vcpu throttling controls */
74static QEMUTimer *throttle_timer;
75static unsigned int throttle_percentage;
76
77#define CPU_THROTTLE_PCT_MIN 1
78#define CPU_THROTTLE_PCT_MAX 99
79#define CPU_THROTTLE_TIMESLICE_NS 10000000
80
Tiejun Chen321bc0b2013-08-02 09:43:09 +080081bool cpu_is_stopped(CPUState *cpu)
82{
83 return cpu->stopped || !runstate_is_running();
84}
85
Andreas Färbera98ae1d2013-05-26 23:21:08 +020086static bool cpu_thread_is_idle(CPUState *cpu)
Peter Maydellac873f12012-07-19 16:52:27 +010087{
Andreas Färberc64ca812012-05-03 02:11:45 +020088 if (cpu->stop || cpu->queued_work_first) {
Peter Maydellac873f12012-07-19 16:52:27 +010089 return false;
90 }
Tiejun Chen321bc0b2013-08-02 09:43:09 +080091 if (cpu_is_stopped(cpu)) {
Peter Maydellac873f12012-07-19 16:52:27 +010092 return true;
93 }
Andreas Färber8c2e1b02013-08-25 18:53:55 +020094 if (!cpu->halted || cpu_has_work(cpu) ||
Alexander Graf215e79c2013-04-24 22:24:12 +020095 kvm_halt_in_kernel()) {
Peter Maydellac873f12012-07-19 16:52:27 +010096 return false;
97 }
98 return true;
99}
100
101static bool all_cpu_threads_idle(void)
102{
Andreas Färber182735e2013-05-29 22:29:20 +0200103 CPUState *cpu;
Peter Maydellac873f12012-07-19 16:52:27 +0100104
Andreas Färberbdc44642013-06-24 23:50:24 +0200105 CPU_FOREACH(cpu) {
Andreas Färber182735e2013-05-29 22:29:20 +0200106 if (!cpu_thread_is_idle(cpu)) {
Peter Maydellac873f12012-07-19 16:52:27 +0100107 return false;
108 }
109 }
110 return true;
111}
112
Blue Swirl296af7c2010-03-29 19:23:50 +0000113/***********************************************************/
Paolo Bonzini946fb272011-09-12 13:57:37 +0200114/* guest cycle counter */
115
Paolo Bonzinia3270e12013-10-07 17:18:15 +0200116/* Protected by TimersState seqlock */
117
Victor CLEMENT5045e9d92015-05-29 17:14:04 +0200118static bool icount_sleep = true;
Sebastian Tanase71468392014-07-23 11:47:50 +0200119static int64_t vm_clock_warp_start = -1;
Paolo Bonzini946fb272011-09-12 13:57:37 +0200120/* Conversion factor from emulated instructions to virtual clock ticks. */
121static int icount_time_shift;
122/* Arbitrarily pick 1MIPS as the minimum allowable speed. */
123#define MAX_ICOUNT_SHIFT 10
Paolo Bonzinia3270e12013-10-07 17:18:15 +0200124
Paolo Bonzini946fb272011-09-12 13:57:37 +0200125static QEMUTimer *icount_rt_timer;
126static QEMUTimer *icount_vm_timer;
127static QEMUTimer *icount_warp_timer;
Paolo Bonzini946fb272011-09-12 13:57:37 +0200128
129typedef struct TimersState {
Liu Ping Fancb365642013-09-25 14:20:58 +0800130 /* Protected by BQL. */
Paolo Bonzini946fb272011-09-12 13:57:37 +0200131 int64_t cpu_ticks_prev;
132 int64_t cpu_ticks_offset;
Liu Ping Fancb365642013-09-25 14:20:58 +0800133
134 /* cpu_clock_offset can be read out of BQL, so protect it with
135 * this lock.
136 */
137 QemuSeqLock vm_clock_seqlock;
Paolo Bonzini946fb272011-09-12 13:57:37 +0200138 int64_t cpu_clock_offset;
139 int32_t cpu_ticks_enabled;
140 int64_t dummy;
KONRAD Fredericc96778b2014-08-01 01:37:09 +0200141
142 /* Compensate for varying guest execution speed. */
143 int64_t qemu_icount_bias;
144 /* Only written by TCG thread */
145 int64_t qemu_icount;
Paolo Bonzini946fb272011-09-12 13:57:37 +0200146} TimersState;
147
Liu Ping Fand9cd4002013-07-21 08:43:00 +0000148static TimersState timers_state;
Paolo Bonzini946fb272011-09-12 13:57:37 +0200149
Pavel Dovgalyuk2a629142014-12-08 10:53:45 +0300150int64_t cpu_get_icount_raw(void)
Paolo Bonzini946fb272011-09-12 13:57:37 +0200151{
152 int64_t icount;
Andreas Färber4917cf42013-05-27 05:17:50 +0200153 CPUState *cpu = current_cpu;
Paolo Bonzini946fb272011-09-12 13:57:37 +0200154
KONRAD Fredericc96778b2014-08-01 01:37:09 +0200155 icount = timers_state.qemu_icount;
Andreas Färber4917cf42013-05-27 05:17:50 +0200156 if (cpu) {
Paolo Bonzini414b15c2015-06-24 14:16:26 +0200157 if (!cpu->can_do_io) {
Pavel Dovgalyuk2a629142014-12-08 10:53:45 +0300158 fprintf(stderr, "Bad icount read\n");
159 exit(1);
Paolo Bonzini946fb272011-09-12 13:57:37 +0200160 }
Andreas Färber28ecfd72013-08-26 05:51:49 +0200161 icount -= (cpu->icount_decr.u16.low + cpu->icount_extra);
Paolo Bonzini946fb272011-09-12 13:57:37 +0200162 }
Pavel Dovgalyuk2a629142014-12-08 10:53:45 +0300163 return icount;
164}
165
166/* Return the virtual CPU time, based on the instruction counter. */
167static int64_t cpu_get_icount_locked(void)
168{
169 int64_t icount = cpu_get_icount_raw();
KONRAD Frederic3f031312014-08-01 01:37:15 +0200170 return timers_state.qemu_icount_bias + cpu_icount_to_ns(icount);
Paolo Bonzini946fb272011-09-12 13:57:37 +0200171}
172
Paolo Bonzini17a15f12013-10-03 15:17:25 +0200173int64_t cpu_get_icount(void)
174{
175 int64_t icount;
176 unsigned start;
177
178 do {
179 start = seqlock_read_begin(&timers_state.vm_clock_seqlock);
180 icount = cpu_get_icount_locked();
181 } while (seqlock_read_retry(&timers_state.vm_clock_seqlock, start));
182
183 return icount;
184}
185
KONRAD Frederic3f031312014-08-01 01:37:15 +0200186int64_t cpu_icount_to_ns(int64_t icount)
187{
188 return icount << icount_time_shift;
189}
190
Paolo Bonzini946fb272011-09-12 13:57:37 +0200191/* return the host CPU cycle counter and handle stop/restart */
Liu Ping Fancb365642013-09-25 14:20:58 +0800192/* Caller must hold the BQL */
Paolo Bonzini946fb272011-09-12 13:57:37 +0200193int64_t cpu_get_ticks(void)
194{
Paolo Bonzini5f3e3102013-10-28 17:32:18 +0100195 int64_t ticks;
196
Paolo Bonzini946fb272011-09-12 13:57:37 +0200197 if (use_icount) {
198 return cpu_get_icount();
199 }
Paolo Bonzini5f3e3102013-10-28 17:32:18 +0100200
201 ticks = timers_state.cpu_ticks_offset;
202 if (timers_state.cpu_ticks_enabled) {
Christopher Covington4a7428c2015-09-25 10:42:21 -0400203 ticks += cpu_get_host_ticks();
Paolo Bonzini946fb272011-09-12 13:57:37 +0200204 }
Paolo Bonzini5f3e3102013-10-28 17:32:18 +0100205
206 if (timers_state.cpu_ticks_prev > ticks) {
207 /* Note: non increasing ticks may happen if the host uses
208 software suspend */
209 timers_state.cpu_ticks_offset += timers_state.cpu_ticks_prev - ticks;
210 ticks = timers_state.cpu_ticks_prev;
211 }
212
213 timers_state.cpu_ticks_prev = ticks;
214 return ticks;
Paolo Bonzini946fb272011-09-12 13:57:37 +0200215}
216
Liu Ping Fancb365642013-09-25 14:20:58 +0800217static int64_t cpu_get_clock_locked(void)
218{
Paolo Bonzini5f3e3102013-10-28 17:32:18 +0100219 int64_t ticks;
Liu Ping Fancb365642013-09-25 14:20:58 +0800220
Paolo Bonzini5f3e3102013-10-28 17:32:18 +0100221 ticks = timers_state.cpu_clock_offset;
222 if (timers_state.cpu_ticks_enabled) {
223 ticks += get_clock();
Liu Ping Fancb365642013-09-25 14:20:58 +0800224 }
225
Paolo Bonzini5f3e3102013-10-28 17:32:18 +0100226 return ticks;
Liu Ping Fancb365642013-09-25 14:20:58 +0800227}
228
Paolo Bonzini946fb272011-09-12 13:57:37 +0200229/* return the host CPU monotonic timer and handle stop/restart */
230int64_t cpu_get_clock(void)
231{
232 int64_t ti;
Liu Ping Fancb365642013-09-25 14:20:58 +0800233 unsigned start;
234
235 do {
236 start = seqlock_read_begin(&timers_state.vm_clock_seqlock);
237 ti = cpu_get_clock_locked();
238 } while (seqlock_read_retry(&timers_state.vm_clock_seqlock, start));
239
240 return ti;
Paolo Bonzini946fb272011-09-12 13:57:37 +0200241}
242
Liu Ping Fancb365642013-09-25 14:20:58 +0800243/* enable cpu_get_ticks()
244 * Caller must hold BQL which server as mutex for vm_clock_seqlock.
245 */
Paolo Bonzini946fb272011-09-12 13:57:37 +0200246void cpu_enable_ticks(void)
247{
Liu Ping Fancb365642013-09-25 14:20:58 +0800248 /* Here, the really thing protected by seqlock is cpu_clock_offset. */
249 seqlock_write_lock(&timers_state.vm_clock_seqlock);
Paolo Bonzini946fb272011-09-12 13:57:37 +0200250 if (!timers_state.cpu_ticks_enabled) {
Christopher Covington4a7428c2015-09-25 10:42:21 -0400251 timers_state.cpu_ticks_offset -= cpu_get_host_ticks();
Paolo Bonzini946fb272011-09-12 13:57:37 +0200252 timers_state.cpu_clock_offset -= get_clock();
253 timers_state.cpu_ticks_enabled = 1;
254 }
Liu Ping Fancb365642013-09-25 14:20:58 +0800255 seqlock_write_unlock(&timers_state.vm_clock_seqlock);
Paolo Bonzini946fb272011-09-12 13:57:37 +0200256}
257
258/* disable cpu_get_ticks() : the clock is stopped. You must not call
Liu Ping Fancb365642013-09-25 14:20:58 +0800259 * cpu_get_ticks() after that.
260 * Caller must hold BQL which server as mutex for vm_clock_seqlock.
261 */
Paolo Bonzini946fb272011-09-12 13:57:37 +0200262void cpu_disable_ticks(void)
263{
Liu Ping Fancb365642013-09-25 14:20:58 +0800264 /* Here, the really thing protected by seqlock is cpu_clock_offset. */
265 seqlock_write_lock(&timers_state.vm_clock_seqlock);
Paolo Bonzini946fb272011-09-12 13:57:37 +0200266 if (timers_state.cpu_ticks_enabled) {
Christopher Covington4a7428c2015-09-25 10:42:21 -0400267 timers_state.cpu_ticks_offset += cpu_get_host_ticks();
Liu Ping Fancb365642013-09-25 14:20:58 +0800268 timers_state.cpu_clock_offset = cpu_get_clock_locked();
Paolo Bonzini946fb272011-09-12 13:57:37 +0200269 timers_state.cpu_ticks_enabled = 0;
270 }
Liu Ping Fancb365642013-09-25 14:20:58 +0800271 seqlock_write_unlock(&timers_state.vm_clock_seqlock);
Paolo Bonzini946fb272011-09-12 13:57:37 +0200272}
273
274/* Correlation between real and virtual time is always going to be
275 fairly approximate, so ignore small variation.
276 When the guest is idle real and virtual time will be aligned in
277 the IO wait loop. */
278#define ICOUNT_WOBBLE (get_ticks_per_sec() / 10)
279
280static void icount_adjust(void)
281{
282 int64_t cur_time;
283 int64_t cur_icount;
284 int64_t delta;
Paolo Bonzinia3270e12013-10-07 17:18:15 +0200285
286 /* Protected by TimersState mutex. */
Paolo Bonzini946fb272011-09-12 13:57:37 +0200287 static int64_t last_delta;
Paolo Bonzini468cc7c2013-10-07 17:21:51 +0200288
Paolo Bonzini946fb272011-09-12 13:57:37 +0200289 /* If the VM is not running, then do nothing. */
290 if (!runstate_is_running()) {
291 return;
292 }
Paolo Bonzini468cc7c2013-10-07 17:21:51 +0200293
Paolo Bonzini17a15f12013-10-03 15:17:25 +0200294 seqlock_write_lock(&timers_state.vm_clock_seqlock);
295 cur_time = cpu_get_clock_locked();
296 cur_icount = cpu_get_icount_locked();
Paolo Bonzini468cc7c2013-10-07 17:21:51 +0200297
Paolo Bonzini946fb272011-09-12 13:57:37 +0200298 delta = cur_icount - cur_time;
299 /* FIXME: This is a very crude algorithm, somewhat prone to oscillation. */
300 if (delta > 0
301 && last_delta + ICOUNT_WOBBLE < delta * 2
302 && icount_time_shift > 0) {
303 /* The guest is getting too far ahead. Slow time down. */
304 icount_time_shift--;
305 }
306 if (delta < 0
307 && last_delta - ICOUNT_WOBBLE > delta * 2
308 && icount_time_shift < MAX_ICOUNT_SHIFT) {
309 /* The guest is getting too far behind. Speed time up. */
310 icount_time_shift++;
311 }
312 last_delta = delta;
KONRAD Fredericc96778b2014-08-01 01:37:09 +0200313 timers_state.qemu_icount_bias = cur_icount
314 - (timers_state.qemu_icount << icount_time_shift);
Paolo Bonzini17a15f12013-10-03 15:17:25 +0200315 seqlock_write_unlock(&timers_state.vm_clock_seqlock);
Paolo Bonzini946fb272011-09-12 13:57:37 +0200316}
317
318static void icount_adjust_rt(void *opaque)
319{
Alex Bligh40daca52013-08-21 16:03:02 +0100320 timer_mod(icount_rt_timer,
Pavel Dovgalyuk1979b902015-01-12 15:00:43 +0300321 qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL_RT) + 1000);
Paolo Bonzini946fb272011-09-12 13:57:37 +0200322 icount_adjust();
323}
324
325static void icount_adjust_vm(void *opaque)
326{
Alex Bligh40daca52013-08-21 16:03:02 +0100327 timer_mod(icount_vm_timer,
328 qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) +
329 get_ticks_per_sec() / 10);
Paolo Bonzini946fb272011-09-12 13:57:37 +0200330 icount_adjust();
331}
332
333static int64_t qemu_icount_round(int64_t count)
334{
335 return (count + (1 << icount_time_shift) - 1) >> icount_time_shift;
336}
337
Pavel Dovgalyukefab87c2015-09-17 19:24:39 +0300338static void icount_warp_rt(void)
Paolo Bonzini946fb272011-09-12 13:57:37 +0200339{
Paolo Bonzini17a15f12013-10-03 15:17:25 +0200340 /* The icount_warp_timer is rescheduled soon after vm_clock_warp_start
341 * changes from -1 to another value, so the race here is okay.
342 */
343 if (atomic_read(&vm_clock_warp_start) == -1) {
Paolo Bonzini946fb272011-09-12 13:57:37 +0200344 return;
345 }
346
Paolo Bonzini17a15f12013-10-03 15:17:25 +0200347 seqlock_write_lock(&timers_state.vm_clock_seqlock);
Paolo Bonzini946fb272011-09-12 13:57:37 +0200348 if (runstate_is_running()) {
Pavel Dovgalyuk8eda2062015-09-17 19:24:28 +0300349 int64_t clock = REPLAY_CLOCK(REPLAY_CLOCK_VIRTUAL_RT,
350 cpu_get_clock_locked());
Paolo Bonzini8ed961d2013-10-07 17:26:07 +0200351 int64_t warp_delta;
352
353 warp_delta = clock - vm_clock_warp_start;
354 if (use_icount == 2) {
Paolo Bonzini946fb272011-09-12 13:57:37 +0200355 /*
Alex Bligh40daca52013-08-21 16:03:02 +0100356 * In adaptive mode, do not let QEMU_CLOCK_VIRTUAL run too
Paolo Bonzini946fb272011-09-12 13:57:37 +0200357 * far ahead of real time.
358 */
Paolo Bonzini17a15f12013-10-03 15:17:25 +0200359 int64_t cur_icount = cpu_get_icount_locked();
Pavel Dovgalyukbf2a7dd2014-11-26 13:40:55 +0300360 int64_t delta = clock - cur_icount;
Paolo Bonzini8ed961d2013-10-07 17:26:07 +0200361 warp_delta = MIN(warp_delta, delta);
Paolo Bonzini946fb272011-09-12 13:57:37 +0200362 }
KONRAD Fredericc96778b2014-08-01 01:37:09 +0200363 timers_state.qemu_icount_bias += warp_delta;
Paolo Bonzini946fb272011-09-12 13:57:37 +0200364 }
365 vm_clock_warp_start = -1;
Paolo Bonzini17a15f12013-10-03 15:17:25 +0200366 seqlock_write_unlock(&timers_state.vm_clock_seqlock);
Paolo Bonzini8ed961d2013-10-07 17:26:07 +0200367
368 if (qemu_clock_expired(QEMU_CLOCK_VIRTUAL)) {
369 qemu_clock_notify(QEMU_CLOCK_VIRTUAL);
370 }
Paolo Bonzini946fb272011-09-12 13:57:37 +0200371}
372
Pavel Dovgalyukefab87c2015-09-17 19:24:39 +0300373static void icount_dummy_timer(void *opaque)
374{
375 (void)opaque;
376}
377
Paolo Bonzini8156be52012-03-28 15:42:04 +0200378void qtest_clock_warp(int64_t dest)
379{
Alex Bligh40daca52013-08-21 16:03:02 +0100380 int64_t clock = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
Fam Zhengefef88b2015-01-19 17:51:43 +0800381 AioContext *aio_context;
Paolo Bonzini8156be52012-03-28 15:42:04 +0200382 assert(qtest_enabled());
Fam Zhengefef88b2015-01-19 17:51:43 +0800383 aio_context = qemu_get_aio_context();
Paolo Bonzini8156be52012-03-28 15:42:04 +0200384 while (clock < dest) {
Alex Bligh40daca52013-08-21 16:03:02 +0100385 int64_t deadline = qemu_clock_deadline_ns_all(QEMU_CLOCK_VIRTUAL);
Sergey Fedorovc9299e22014-06-10 13:10:28 +0400386 int64_t warp = qemu_soonest_timeout(dest - clock, deadline);
Fam Zhengefef88b2015-01-19 17:51:43 +0800387
Paolo Bonzini17a15f12013-10-03 15:17:25 +0200388 seqlock_write_lock(&timers_state.vm_clock_seqlock);
KONRAD Fredericc96778b2014-08-01 01:37:09 +0200389 timers_state.qemu_icount_bias += warp;
Paolo Bonzini17a15f12013-10-03 15:17:25 +0200390 seqlock_write_unlock(&timers_state.vm_clock_seqlock);
391
Alex Bligh40daca52013-08-21 16:03:02 +0100392 qemu_clock_run_timers(QEMU_CLOCK_VIRTUAL);
Fam Zhengefef88b2015-01-19 17:51:43 +0800393 timerlist_run_timers(aio_context->tlg.tl[QEMU_CLOCK_VIRTUAL]);
Alex Bligh40daca52013-08-21 16:03:02 +0100394 clock = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
Paolo Bonzini8156be52012-03-28 15:42:04 +0200395 }
Alex Bligh40daca52013-08-21 16:03:02 +0100396 qemu_clock_notify(QEMU_CLOCK_VIRTUAL);
Paolo Bonzini8156be52012-03-28 15:42:04 +0200397}
398
Alex Bligh40daca52013-08-21 16:03:02 +0100399void qemu_clock_warp(QEMUClockType type)
Paolo Bonzini946fb272011-09-12 13:57:37 +0200400{
Paolo Bonzinice78d182013-10-07 17:30:02 +0200401 int64_t clock;
Paolo Bonzini946fb272011-09-12 13:57:37 +0200402 int64_t deadline;
403
404 /*
405 * There are too many global variables to make the "warp" behavior
406 * applicable to other clocks. But a clock argument removes the
407 * need for if statements all over the place.
408 */
Alex Bligh40daca52013-08-21 16:03:02 +0100409 if (type != QEMU_CLOCK_VIRTUAL || !use_icount) {
Paolo Bonzini946fb272011-09-12 13:57:37 +0200410 return;
411 }
412
Pavel Dovgalyuk8bd7f712015-09-17 19:24:44 +0300413 /* Nothing to do if the VM is stopped: QEMU_CLOCK_VIRTUAL timers
414 * do not fire, so computing the deadline does not make sense.
415 */
416 if (!runstate_is_running()) {
417 return;
418 }
419
420 /* warp clock deterministically in record/replay mode */
421 if (!replay_checkpoint(CHECKPOINT_CLOCK_WARP)) {
422 return;
423 }
424
Victor CLEMENT5045e9d92015-05-29 17:14:04 +0200425 if (icount_sleep) {
426 /*
427 * If the CPUs have been sleeping, advance QEMU_CLOCK_VIRTUAL timer now.
428 * This ensures that the deadline for the timer is computed correctly
429 * below.
430 * This also makes sure that the insn counter is synchronized before
431 * the CPU starts running, in case the CPU is woken by an event other
432 * than the earliest QEMU_CLOCK_VIRTUAL timer.
433 */
Pavel Dovgalyukefab87c2015-09-17 19:24:39 +0300434 icount_warp_rt();
Victor CLEMENT5045e9d92015-05-29 17:14:04 +0200435 timer_del(icount_warp_timer);
436 }
Paolo Bonzinice78d182013-10-07 17:30:02 +0200437 if (!all_cpu_threads_idle()) {
Paolo Bonzini946fb272011-09-12 13:57:37 +0200438 return;
439 }
440
Paolo Bonzini8156be52012-03-28 15:42:04 +0200441 if (qtest_enabled()) {
442 /* When testing, qtest commands advance icount. */
443 return;
444 }
445
Alex Blighac70aaf2013-08-21 16:02:57 +0100446 /* We want to use the earliest deadline from ALL vm_clocks */
Pavel Dovgalyukbf2a7dd2014-11-26 13:40:55 +0300447 clock = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL_RT);
Alex Bligh40daca52013-08-21 16:03:02 +0100448 deadline = qemu_clock_deadline_ns_all(QEMU_CLOCK_VIRTUAL);
Paolo Bonzinice78d182013-10-07 17:30:02 +0200449 if (deadline < 0) {
Victor CLEMENTd7a0f712015-05-29 17:14:06 +0200450 static bool notified;
451 if (!icount_sleep && !notified) {
452 error_report("WARNING: icount sleep disabled and no active timers");
453 notified = true;
454 }
Paolo Bonzinice78d182013-10-07 17:30:02 +0200455 return;
Alex Blighac70aaf2013-08-21 16:02:57 +0100456 }
457
Paolo Bonzini946fb272011-09-12 13:57:37 +0200458 if (deadline > 0) {
459 /*
Alex Bligh40daca52013-08-21 16:03:02 +0100460 * Ensure QEMU_CLOCK_VIRTUAL proceeds even when the virtual CPU goes to
Paolo Bonzini946fb272011-09-12 13:57:37 +0200461 * sleep. Otherwise, the CPU might be waiting for a future timer
462 * interrupt to wake it up, but the interrupt never comes because
463 * the vCPU isn't running any insns and thus doesn't advance the
Alex Bligh40daca52013-08-21 16:03:02 +0100464 * QEMU_CLOCK_VIRTUAL.
Paolo Bonzini946fb272011-09-12 13:57:37 +0200465 */
Victor CLEMENT5045e9d92015-05-29 17:14:04 +0200466 if (!icount_sleep) {
467 /*
468 * We never let VCPUs sleep in no sleep icount mode.
469 * If there is a pending QEMU_CLOCK_VIRTUAL timer we just advance
470 * to the next QEMU_CLOCK_VIRTUAL event and notify it.
471 * It is useful when we want a deterministic execution time,
472 * isolated from host latencies.
473 */
474 seqlock_write_lock(&timers_state.vm_clock_seqlock);
475 timers_state.qemu_icount_bias += deadline;
476 seqlock_write_unlock(&timers_state.vm_clock_seqlock);
477 qemu_clock_notify(QEMU_CLOCK_VIRTUAL);
478 } else {
479 /*
480 * We do stop VCPUs and only advance QEMU_CLOCK_VIRTUAL after some
481 * "real" time, (related to the time left until the next event) has
482 * passed. The QEMU_CLOCK_VIRTUAL_RT clock will do this.
483 * This avoids that the warps are visible externally; for example,
484 * you will not be sending network packets continuously instead of
485 * every 100ms.
486 */
487 seqlock_write_lock(&timers_state.vm_clock_seqlock);
488 if (vm_clock_warp_start == -1 || vm_clock_warp_start > clock) {
489 vm_clock_warp_start = clock;
490 }
491 seqlock_write_unlock(&timers_state.vm_clock_seqlock);
492 timer_mod_anticipate(icount_warp_timer, clock + deadline);
Paolo Bonzinice78d182013-10-07 17:30:02 +0200493 }
Alex Blighac70aaf2013-08-21 16:02:57 +0100494 } else if (deadline == 0) {
Alex Bligh40daca52013-08-21 16:03:02 +0100495 qemu_clock_notify(QEMU_CLOCK_VIRTUAL);
Paolo Bonzini946fb272011-09-12 13:57:37 +0200496 }
497}
498
KONRAD Fredericd09eae32014-08-01 01:37:10 +0200499static bool icount_state_needed(void *opaque)
500{
501 return use_icount;
502}
503
504/*
505 * This is a subsection for icount migration.
506 */
507static const VMStateDescription icount_vmstate_timers = {
508 .name = "timer/icount",
509 .version_id = 1,
510 .minimum_version_id = 1,
Juan Quintela5cd8cad2014-09-23 14:09:54 +0200511 .needed = icount_state_needed,
KONRAD Fredericd09eae32014-08-01 01:37:10 +0200512 .fields = (VMStateField[]) {
513 VMSTATE_INT64(qemu_icount_bias, TimersState),
514 VMSTATE_INT64(qemu_icount, TimersState),
515 VMSTATE_END_OF_LIST()
516 }
517};
518
Paolo Bonzini946fb272011-09-12 13:57:37 +0200519static const VMStateDescription vmstate_timers = {
520 .name = "timer",
521 .version_id = 2,
522 .minimum_version_id = 1,
Juan Quintela35d08452014-04-16 16:01:33 +0200523 .fields = (VMStateField[]) {
Paolo Bonzini946fb272011-09-12 13:57:37 +0200524 VMSTATE_INT64(cpu_ticks_offset, TimersState),
525 VMSTATE_INT64(dummy, TimersState),
526 VMSTATE_INT64_V(cpu_clock_offset, TimersState, 2),
527 VMSTATE_END_OF_LIST()
KONRAD Fredericd09eae32014-08-01 01:37:10 +0200528 },
Juan Quintela5cd8cad2014-09-23 14:09:54 +0200529 .subsections = (const VMStateDescription*[]) {
530 &icount_vmstate_timers,
531 NULL
Paolo Bonzini946fb272011-09-12 13:57:37 +0200532 }
533};
534
Jason J. Herne2adcc852015-09-08 13:12:33 -0400535static void cpu_throttle_thread(void *opaque)
536{
537 CPUState *cpu = opaque;
538 double pct;
539 double throttle_ratio;
540 long sleeptime_ns;
541
542 if (!cpu_throttle_get_percentage()) {
543 return;
544 }
545
546 pct = (double)cpu_throttle_get_percentage()/100;
547 throttle_ratio = pct / (1 - pct);
548 sleeptime_ns = (long)(throttle_ratio * CPU_THROTTLE_TIMESLICE_NS);
549
550 qemu_mutex_unlock_iothread();
551 atomic_set(&cpu->throttle_thread_scheduled, 0);
552 g_usleep(sleeptime_ns / 1000); /* Convert ns to us for usleep call */
553 qemu_mutex_lock_iothread();
554}
555
556static void cpu_throttle_timer_tick(void *opaque)
557{
558 CPUState *cpu;
559 double pct;
560
561 /* Stop the timer if needed */
562 if (!cpu_throttle_get_percentage()) {
563 return;
564 }
565 CPU_FOREACH(cpu) {
566 if (!atomic_xchg(&cpu->throttle_thread_scheduled, 1)) {
567 async_run_on_cpu(cpu, cpu_throttle_thread, cpu);
568 }
569 }
570
571 pct = (double)cpu_throttle_get_percentage()/100;
572 timer_mod(throttle_timer, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL_RT) +
573 CPU_THROTTLE_TIMESLICE_NS / (1-pct));
574}
575
576void cpu_throttle_set(int new_throttle_pct)
577{
578 /* Ensure throttle percentage is within valid range */
579 new_throttle_pct = MIN(new_throttle_pct, CPU_THROTTLE_PCT_MAX);
580 new_throttle_pct = MAX(new_throttle_pct, CPU_THROTTLE_PCT_MIN);
581
582 atomic_set(&throttle_percentage, new_throttle_pct);
583
584 timer_mod(throttle_timer, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL_RT) +
585 CPU_THROTTLE_TIMESLICE_NS);
586}
587
588void cpu_throttle_stop(void)
589{
590 atomic_set(&throttle_percentage, 0);
591}
592
593bool cpu_throttle_active(void)
594{
595 return (cpu_throttle_get_percentage() != 0);
596}
597
598int cpu_throttle_get_percentage(void)
599{
600 return atomic_read(&throttle_percentage);
601}
602
Pavel Dovgalyuk4603ea02014-09-01 09:34:49 +0400603void cpu_ticks_init(void)
604{
605 seqlock_init(&timers_state.vm_clock_seqlock, NULL);
606 vmstate_register(NULL, 0, &vmstate_timers, &timers_state);
Jason J. Herne2adcc852015-09-08 13:12:33 -0400607 throttle_timer = timer_new_ns(QEMU_CLOCK_VIRTUAL_RT,
608 cpu_throttle_timer_tick, NULL);
Pavel Dovgalyuk4603ea02014-09-01 09:34:49 +0400609}
610
Sebastian Tanase1ad95802014-07-25 11:56:28 +0200611void configure_icount(QemuOpts *opts, Error **errp)
Paolo Bonzini946fb272011-09-12 13:57:37 +0200612{
Sebastian Tanase1ad95802014-07-25 11:56:28 +0200613 const char *option;
Sebastian Tanasea8bfac32014-07-25 11:56:29 +0200614 char *rem_str = NULL;
Sebastian Tanase1ad95802014-07-25 11:56:28 +0200615
Sebastian Tanase1ad95802014-07-25 11:56:28 +0200616 option = qemu_opt_get(opts, "shift");
Paolo Bonzini946fb272011-09-12 13:57:37 +0200617 if (!option) {
Sebastian Tanasea8bfac32014-07-25 11:56:29 +0200618 if (qemu_opt_get(opts, "align") != NULL) {
619 error_setg(errp, "Please specify shift option when using align");
620 }
Paolo Bonzini946fb272011-09-12 13:57:37 +0200621 return;
622 }
Victor CLEMENTf1f4b572015-05-29 17:14:05 +0200623
624 icount_sleep = qemu_opt_get_bool(opts, "sleep", true);
Victor CLEMENT5045e9d92015-05-29 17:14:04 +0200625 if (icount_sleep) {
626 icount_warp_timer = timer_new_ns(QEMU_CLOCK_VIRTUAL_RT,
Pavel Dovgalyukefab87c2015-09-17 19:24:39 +0300627 icount_dummy_timer, NULL);
Victor CLEMENT5045e9d92015-05-29 17:14:04 +0200628 }
Victor CLEMENTf1f4b572015-05-29 17:14:05 +0200629
Sebastian Tanasea8bfac32014-07-25 11:56:29 +0200630 icount_align_option = qemu_opt_get_bool(opts, "align", false);
Victor CLEMENTf1f4b572015-05-29 17:14:05 +0200631
632 if (icount_align_option && !icount_sleep) {
633 error_setg(errp, "align=on and sleep=no are incompatible");
634 }
Paolo Bonzini946fb272011-09-12 13:57:37 +0200635 if (strcmp(option, "auto") != 0) {
Sebastian Tanasea8bfac32014-07-25 11:56:29 +0200636 errno = 0;
637 icount_time_shift = strtol(option, &rem_str, 0);
638 if (errno != 0 || *rem_str != '\0' || !strlen(option)) {
639 error_setg(errp, "icount: Invalid shift value");
640 }
Paolo Bonzini946fb272011-09-12 13:57:37 +0200641 use_icount = 1;
642 return;
Sebastian Tanasea8bfac32014-07-25 11:56:29 +0200643 } else if (icount_align_option) {
644 error_setg(errp, "shift=auto and align=on are incompatible");
Victor CLEMENTf1f4b572015-05-29 17:14:05 +0200645 } else if (!icount_sleep) {
646 error_setg(errp, "shift=auto and sleep=no are incompatible");
Paolo Bonzini946fb272011-09-12 13:57:37 +0200647 }
648
649 use_icount = 2;
650
651 /* 125MIPS seems a reasonable initial guess at the guest speed.
652 It will be corrected fairly quickly anyway. */
653 icount_time_shift = 3;
654
655 /* Have both realtime and virtual time triggers for speed adjustment.
656 The realtime trigger catches emulated time passing too slowly,
657 the virtual time trigger catches emulated time passing too fast.
658 Realtime triggers occur even when idle, so use them less frequently
659 than VM triggers. */
Pavel Dovgalyukbf2a7dd2014-11-26 13:40:55 +0300660 icount_rt_timer = timer_new_ms(QEMU_CLOCK_VIRTUAL_RT,
661 icount_adjust_rt, NULL);
Alex Bligh40daca52013-08-21 16:03:02 +0100662 timer_mod(icount_rt_timer,
Pavel Dovgalyukbf2a7dd2014-11-26 13:40:55 +0300663 qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL_RT) + 1000);
Alex Bligh40daca52013-08-21 16:03:02 +0100664 icount_vm_timer = timer_new_ns(QEMU_CLOCK_VIRTUAL,
665 icount_adjust_vm, NULL);
666 timer_mod(icount_vm_timer,
667 qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) +
668 get_ticks_per_sec() / 10);
Paolo Bonzini946fb272011-09-12 13:57:37 +0200669}
670
671/***********************************************************/
Blue Swirl296af7c2010-03-29 19:23:50 +0000672void hw_error(const char *fmt, ...)
673{
674 va_list ap;
Andreas Färber55e5c282012-12-17 06:18:02 +0100675 CPUState *cpu;
Blue Swirl296af7c2010-03-29 19:23:50 +0000676
677 va_start(ap, fmt);
678 fprintf(stderr, "qemu: hardware error: ");
679 vfprintf(stderr, fmt, ap);
680 fprintf(stderr, "\n");
Andreas Färberbdc44642013-06-24 23:50:24 +0200681 CPU_FOREACH(cpu) {
Andreas Färber55e5c282012-12-17 06:18:02 +0100682 fprintf(stderr, "CPU #%d:\n", cpu->cpu_index);
Andreas Färber878096e2013-05-27 01:33:50 +0200683 cpu_dump_state(cpu, stderr, fprintf, CPU_DUMP_FPU);
Blue Swirl296af7c2010-03-29 19:23:50 +0000684 }
685 va_end(ap);
686 abort();
687}
688
689void cpu_synchronize_all_states(void)
690{
Andreas Färber182735e2013-05-29 22:29:20 +0200691 CPUState *cpu;
Blue Swirl296af7c2010-03-29 19:23:50 +0000692
Andreas Färberbdc44642013-06-24 23:50:24 +0200693 CPU_FOREACH(cpu) {
Andreas Färber182735e2013-05-29 22:29:20 +0200694 cpu_synchronize_state(cpu);
Blue Swirl296af7c2010-03-29 19:23:50 +0000695 }
696}
697
698void cpu_synchronize_all_post_reset(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_post_reset(cpu);
Blue Swirl296af7c2010-03-29 19:23:50 +0000704 }
705}
706
707void cpu_synchronize_all_post_init(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_init(cpu);
Blue Swirl296af7c2010-03-29 19:23:50 +0000713 }
714}
715
Kevin Wolf56983462013-07-05 13:49:54 +0200716static int do_vm_stop(RunState state)
Blue Swirl296af7c2010-03-29 19:23:50 +0000717{
Kevin Wolf56983462013-07-05 13:49:54 +0200718 int ret = 0;
719
Luiz Capitulino13548692011-07-29 15:36:43 -0300720 if (runstate_is_running()) {
Blue Swirl296af7c2010-03-29 19:23:50 +0000721 cpu_disable_ticks();
Blue Swirl296af7c2010-03-29 19:23:50 +0000722 pause_all_vcpus();
Luiz Capitulinof5bbfba2011-07-29 15:04:45 -0300723 runstate_set(state);
Luiz Capitulino1dfb4dd2011-07-29 14:26:33 -0300724 vm_state_notify(0, state);
Wenchao Xiaa4e15de2014-06-18 08:43:36 +0200725 qapi_event_send_stop(&error_abort);
Blue Swirl296af7c2010-03-29 19:23:50 +0000726 }
Kevin Wolf56983462013-07-05 13:49:54 +0200727
Kevin Wolf594a45c2013-07-18 14:52:19 +0200728 bdrv_drain_all();
729 ret = bdrv_flush_all();
730
Kevin Wolf56983462013-07-05 13:49:54 +0200731 return ret;
Blue Swirl296af7c2010-03-29 19:23:50 +0000732}
733
Andreas Färbera1fcaa72012-05-02 23:42:26 +0200734static bool cpu_can_run(CPUState *cpu)
Blue Swirl296af7c2010-03-29 19:23:50 +0000735{
Andreas Färber4fdeee72012-05-02 23:10:09 +0200736 if (cpu->stop) {
Andreas Färbera1fcaa72012-05-02 23:42:26 +0200737 return false;
Jan Kiszka0ab07c62011-02-07 12:19:14 +0100738 }
Tiejun Chen321bc0b2013-08-02 09:43:09 +0800739 if (cpu_is_stopped(cpu)) {
Andreas Färbera1fcaa72012-05-02 23:42:26 +0200740 return false;
Jan Kiszka0ab07c62011-02-07 12:19:14 +0100741 }
Andreas Färbera1fcaa72012-05-02 23:42:26 +0200742 return true;
Blue Swirl296af7c2010-03-29 19:23:50 +0000743}
744
Andreas Färber91325042013-05-27 02:07:49 +0200745static void cpu_handle_guest_debug(CPUState *cpu)
Jan Kiszka3c638d02010-06-25 16:56:56 +0200746{
Andreas Färber64f6b342013-05-27 02:06:09 +0200747 gdb_set_stop_cpu(cpu);
Jan Kiszka8cf71712011-02-07 12:19:16 +0100748 qemu_system_debug_request();
Andreas Färberf324e762012-05-02 23:26:21 +0200749 cpu->stopped = true;
Jan Kiszka3c638d02010-06-25 16:56:56 +0200750}
751
Jan Kiszka6d9cb732011-02-01 22:15:58 +0100752#ifdef CONFIG_LINUX
753static void sigbus_reraise(void)
754{
755 sigset_t set;
756 struct sigaction action;
757
758 memset(&action, 0, sizeof(action));
759 action.sa_handler = SIG_DFL;
760 if (!sigaction(SIGBUS, &action, NULL)) {
761 raise(SIGBUS);
762 sigemptyset(&set);
763 sigaddset(&set, SIGBUS);
764 sigprocmask(SIG_UNBLOCK, &set, NULL);
765 }
766 perror("Failed to re-raise SIGBUS!\n");
767 abort();
768}
769
770static void sigbus_handler(int n, struct qemu_signalfd_siginfo *siginfo,
771 void *ctx)
772{
773 if (kvm_on_sigbus(siginfo->ssi_code,
774 (void *)(intptr_t)siginfo->ssi_addr)) {
775 sigbus_reraise();
776 }
777}
778
779static void qemu_init_sigbus(void)
780{
781 struct sigaction action;
782
783 memset(&action, 0, sizeof(action));
784 action.sa_flags = SA_SIGINFO;
785 action.sa_sigaction = (void (*)(int, siginfo_t*, void*))sigbus_handler;
786 sigaction(SIGBUS, &action, NULL);
787
788 prctl(PR_MCE_KILL, PR_MCE_KILL_SET, PR_MCE_KILL_EARLY, 0, 0);
789}
790
Andreas Färber290adf32013-01-17 09:30:27 +0100791static void qemu_kvm_eat_signals(CPUState *cpu)
Jan Kiszka1ab3c6c2011-03-15 12:26:12 +0100792{
793 struct timespec ts = { 0, 0 };
794 siginfo_t siginfo;
795 sigset_t waitset;
796 sigset_t chkset;
797 int r;
798
799 sigemptyset(&waitset);
800 sigaddset(&waitset, SIG_IPI);
801 sigaddset(&waitset, SIGBUS);
802
803 do {
804 r = sigtimedwait(&waitset, &siginfo, &ts);
805 if (r == -1 && !(errno == EAGAIN || errno == EINTR)) {
806 perror("sigtimedwait");
807 exit(1);
808 }
809
810 switch (r) {
811 case SIGBUS:
Andreas Färber290adf32013-01-17 09:30:27 +0100812 if (kvm_on_sigbus_vcpu(cpu, siginfo.si_code, siginfo.si_addr)) {
Jan Kiszka1ab3c6c2011-03-15 12:26:12 +0100813 sigbus_reraise();
814 }
815 break;
816 default:
817 break;
818 }
819
820 r = sigpending(&chkset);
821 if (r == -1) {
822 perror("sigpending");
823 exit(1);
824 }
825 } while (sigismember(&chkset, SIG_IPI) || sigismember(&chkset, SIGBUS));
Jan Kiszka1ab3c6c2011-03-15 12:26:12 +0100826}
827
Jan Kiszka6d9cb732011-02-01 22:15:58 +0100828#else /* !CONFIG_LINUX */
829
830static void qemu_init_sigbus(void)
831{
832}
Jan Kiszka1ab3c6c2011-03-15 12:26:12 +0100833
Andreas Färber290adf32013-01-17 09:30:27 +0100834static void qemu_kvm_eat_signals(CPUState *cpu)
Jan Kiszka1ab3c6c2011-03-15 12:26:12 +0100835{
836}
Jan Kiszka6d9cb732011-02-01 22:15:58 +0100837#endif /* !CONFIG_LINUX */
838
Blue Swirl296af7c2010-03-29 19:23:50 +0000839#ifndef _WIN32
Jan Kiszka55f8d6a2011-02-01 22:15:52 +0100840static void dummy_signal(int sig)
Blue Swirl296af7c2010-03-29 19:23:50 +0000841{
842}
843
Andreas Färber13618e02013-05-26 23:41:00 +0200844static void qemu_kvm_init_cpu_signals(CPUState *cpu)
Paolo Bonzini714bd042011-03-12 17:44:06 +0100845{
846 int r;
847 sigset_t set;
848 struct sigaction sigact;
849
850 memset(&sigact, 0, sizeof(sigact));
851 sigact.sa_handler = dummy_signal;
852 sigaction(SIG_IPI, &sigact, NULL);
853
Paolo Bonzini714bd042011-03-12 17:44:06 +0100854 pthread_sigmask(SIG_BLOCK, NULL, &set);
855 sigdelset(&set, SIG_IPI);
856 sigdelset(&set, SIGBUS);
Andreas Färber491d6e82013-05-26 23:38:10 +0200857 r = kvm_set_signal_mask(cpu, &set);
Paolo Bonzini714bd042011-03-12 17:44:06 +0100858 if (r) {
859 fprintf(stderr, "kvm_set_signal_mask: %s\n", strerror(-r));
860 exit(1);
861 }
Paolo Bonzini714bd042011-03-12 17:44:06 +0100862}
863
Jan Kiszka55f8d6a2011-02-01 22:15:52 +0100864#else /* _WIN32 */
Andreas Färber13618e02013-05-26 23:41:00 +0200865static void qemu_kvm_init_cpu_signals(CPUState *cpu)
Paolo Bonzini714bd042011-03-12 17:44:06 +0100866{
867 abort();
868}
Jan Kiszka55f8d6a2011-02-01 22:15:52 +0100869#endif /* _WIN32 */
Blue Swirl296af7c2010-03-29 19:23:50 +0000870
Stefan Weilb2532d82012-09-27 07:41:42 +0200871static QemuMutex qemu_global_mutex;
Paolo Bonzini46daff12011-06-09 13:10:24 +0200872static QemuCond qemu_io_proceeded_cond;
Paolo Bonzini6b498092015-02-27 19:58:23 +0100873static unsigned iothread_requesting_mutex;
Blue Swirl296af7c2010-03-29 19:23:50 +0000874
875static QemuThread io_thread;
876
Blue Swirl296af7c2010-03-29 19:23:50 +0000877/* cpu creation */
878static QemuCond qemu_cpu_cond;
879/* system init */
Blue Swirl296af7c2010-03-29 19:23:50 +0000880static QemuCond qemu_pause_cond;
Marcelo Tosattie82bcec2010-05-04 09:45:22 -0300881static QemuCond qemu_work_cond;
Blue Swirl296af7c2010-03-29 19:23:50 +0000882
Paolo Bonzinid3b12f52011-09-13 10:30:52 +0200883void qemu_init_cpu_loop(void)
Blue Swirl296af7c2010-03-29 19:23:50 +0000884{
Jan Kiszka6d9cb732011-02-01 22:15:58 +0100885 qemu_init_sigbus();
Anthony Liguoried945922011-02-08 18:18:18 +0100886 qemu_cond_init(&qemu_cpu_cond);
Anthony Liguoried945922011-02-08 18:18:18 +0100887 qemu_cond_init(&qemu_pause_cond);
888 qemu_cond_init(&qemu_work_cond);
Paolo Bonzini46daff12011-06-09 13:10:24 +0200889 qemu_cond_init(&qemu_io_proceeded_cond);
Blue Swirl296af7c2010-03-29 19:23:50 +0000890 qemu_mutex_init(&qemu_global_mutex);
Blue Swirl296af7c2010-03-29 19:23:50 +0000891
Jan Kiszkab7680cb2011-03-12 17:43:51 +0100892 qemu_thread_get_self(&io_thread);
Blue Swirl296af7c2010-03-29 19:23:50 +0000893}
894
Andreas Färberf100f0b2012-05-03 14:58:47 +0200895void run_on_cpu(CPUState *cpu, void (*func)(void *data), void *data)
Marcelo Tosattie82bcec2010-05-04 09:45:22 -0300896{
897 struct qemu_work_item wi;
898
Andreas Färber60e82572012-05-02 22:23:49 +0200899 if (qemu_cpu_is_self(cpu)) {
Marcelo Tosattie82bcec2010-05-04 09:45:22 -0300900 func(data);
901 return;
902 }
903
904 wi.func = func;
905 wi.data = data;
Chegu Vinod3c022702013-06-24 03:49:41 -0600906 wi.free = false;
Paolo Bonzini376692b2015-07-10 12:32:32 +0200907
908 qemu_mutex_lock(&cpu->work_mutex);
Andreas Färberc64ca812012-05-03 02:11:45 +0200909 if (cpu->queued_work_first == NULL) {
910 cpu->queued_work_first = &wi;
Jan Kiszka0ab07c62011-02-07 12:19:14 +0100911 } else {
Andreas Färberc64ca812012-05-03 02:11:45 +0200912 cpu->queued_work_last->next = &wi;
Jan Kiszka0ab07c62011-02-07 12:19:14 +0100913 }
Andreas Färberc64ca812012-05-03 02:11:45 +0200914 cpu->queued_work_last = &wi;
Marcelo Tosattie82bcec2010-05-04 09:45:22 -0300915 wi.next = NULL;
916 wi.done = false;
Paolo Bonzini376692b2015-07-10 12:32:32 +0200917 qemu_mutex_unlock(&cpu->work_mutex);
Marcelo Tosattie82bcec2010-05-04 09:45:22 -0300918
Andreas Färberc08d7422012-05-03 04:34:15 +0200919 qemu_cpu_kick(cpu);
Paolo Bonzini376692b2015-07-10 12:32:32 +0200920 while (!atomic_mb_read(&wi.done)) {
Andreas Färber4917cf42013-05-27 05:17:50 +0200921 CPUState *self_cpu = current_cpu;
Marcelo Tosattie82bcec2010-05-04 09:45:22 -0300922
923 qemu_cond_wait(&qemu_work_cond, &qemu_global_mutex);
Andreas Färber4917cf42013-05-27 05:17:50 +0200924 current_cpu = self_cpu;
Marcelo Tosattie82bcec2010-05-04 09:45:22 -0300925 }
926}
927
Chegu Vinod3c022702013-06-24 03:49:41 -0600928void async_run_on_cpu(CPUState *cpu, void (*func)(void *data), void *data)
929{
930 struct qemu_work_item *wi;
931
932 if (qemu_cpu_is_self(cpu)) {
933 func(data);
934 return;
935 }
936
937 wi = g_malloc0(sizeof(struct qemu_work_item));
938 wi->func = func;
939 wi->data = data;
940 wi->free = true;
Paolo Bonzini376692b2015-07-10 12:32:32 +0200941
942 qemu_mutex_lock(&cpu->work_mutex);
Chegu Vinod3c022702013-06-24 03:49:41 -0600943 if (cpu->queued_work_first == NULL) {
944 cpu->queued_work_first = wi;
945 } else {
946 cpu->queued_work_last->next = wi;
947 }
948 cpu->queued_work_last = wi;
949 wi->next = NULL;
950 wi->done = false;
Paolo Bonzini376692b2015-07-10 12:32:32 +0200951 qemu_mutex_unlock(&cpu->work_mutex);
Chegu Vinod3c022702013-06-24 03:49:41 -0600952
953 qemu_cpu_kick(cpu);
954}
955
Andreas Färber6d45b102012-05-03 02:13:22 +0200956static void flush_queued_work(CPUState *cpu)
Marcelo Tosattie82bcec2010-05-04 09:45:22 -0300957{
958 struct qemu_work_item *wi;
959
Andreas Färberc64ca812012-05-03 02:11:45 +0200960 if (cpu->queued_work_first == NULL) {
Marcelo Tosattie82bcec2010-05-04 09:45:22 -0300961 return;
Jan Kiszka0ab07c62011-02-07 12:19:14 +0100962 }
Marcelo Tosattie82bcec2010-05-04 09:45:22 -0300963
Paolo Bonzini376692b2015-07-10 12:32:32 +0200964 qemu_mutex_lock(&cpu->work_mutex);
965 while (cpu->queued_work_first != NULL) {
966 wi = cpu->queued_work_first;
Andreas Färberc64ca812012-05-03 02:11:45 +0200967 cpu->queued_work_first = wi->next;
Paolo Bonzini376692b2015-07-10 12:32:32 +0200968 if (!cpu->queued_work_first) {
969 cpu->queued_work_last = NULL;
970 }
971 qemu_mutex_unlock(&cpu->work_mutex);
Marcelo Tosattie82bcec2010-05-04 09:45:22 -0300972 wi->func(wi->data);
Paolo Bonzini376692b2015-07-10 12:32:32 +0200973 qemu_mutex_lock(&cpu->work_mutex);
Chegu Vinod3c022702013-06-24 03:49:41 -0600974 if (wi->free) {
975 g_free(wi);
Paolo Bonzini376692b2015-07-10 12:32:32 +0200976 } else {
977 atomic_mb_set(&wi->done, true);
Chegu Vinod3c022702013-06-24 03:49:41 -0600978 }
Marcelo Tosattie82bcec2010-05-04 09:45:22 -0300979 }
Paolo Bonzini376692b2015-07-10 12:32:32 +0200980 qemu_mutex_unlock(&cpu->work_mutex);
Marcelo Tosattie82bcec2010-05-04 09:45:22 -0300981 qemu_cond_broadcast(&qemu_work_cond);
982}
983
Andreas Färber509a0d72012-05-03 02:18:09 +0200984static void qemu_wait_io_event_common(CPUState *cpu)
Blue Swirl296af7c2010-03-29 19:23:50 +0000985{
Andreas Färber4fdeee72012-05-02 23:10:09 +0200986 if (cpu->stop) {
987 cpu->stop = false;
Andreas Färberf324e762012-05-02 23:26:21 +0200988 cpu->stopped = true;
Blue Swirl296af7c2010-03-29 19:23:50 +0000989 qemu_cond_signal(&qemu_pause_cond);
990 }
Andreas Färber6d45b102012-05-03 02:13:22 +0200991 flush_queued_work(cpu);
Andreas Färber216fc9a2012-05-02 17:49:49 +0200992 cpu->thread_kicked = false;
Blue Swirl296af7c2010-03-29 19:23:50 +0000993}
994
KONRAD Fredericd5f8d612015-08-10 17:27:06 +0200995static void qemu_tcg_wait_io_event(CPUState *cpu)
Blue Swirl296af7c2010-03-29 19:23:50 +0000996{
Jan Kiszka16400322011-02-09 16:29:37 +0100997 while (all_cpu_threads_idle()) {
Paolo Bonziniab33fcd2011-04-13 10:03:44 +0200998 /* Start accounting real time to the virtual clock if the CPUs
999 are idle. */
Alex Bligh40daca52013-08-21 16:03:02 +01001000 qemu_clock_warp(QEMU_CLOCK_VIRTUAL);
KONRAD Fredericd5f8d612015-08-10 17:27:06 +02001001 qemu_cond_wait(cpu->halt_cond, &qemu_global_mutex);
Jan Kiszka16400322011-02-09 16:29:37 +01001002 }
Blue Swirl296af7c2010-03-29 19:23:50 +00001003
Paolo Bonzini46daff12011-06-09 13:10:24 +02001004 while (iothread_requesting_mutex) {
1005 qemu_cond_wait(&qemu_io_proceeded_cond, &qemu_global_mutex);
1006 }
Jan Kiszka6cabe1f2010-06-25 16:56:53 +02001007
Andreas Färberbdc44642013-06-24 23:50:24 +02001008 CPU_FOREACH(cpu) {
Andreas Färber182735e2013-05-29 22:29:20 +02001009 qemu_wait_io_event_common(cpu);
Jan Kiszka6cabe1f2010-06-25 16:56:53 +02001010 }
Blue Swirl296af7c2010-03-29 19:23:50 +00001011}
1012
Andreas Färberfd529e82013-05-26 23:24:55 +02001013static void qemu_kvm_wait_io_event(CPUState *cpu)
Blue Swirl296af7c2010-03-29 19:23:50 +00001014{
Andreas Färbera98ae1d2013-05-26 23:21:08 +02001015 while (cpu_thread_is_idle(cpu)) {
Andreas Färberf5c121b2012-05-03 01:22:49 +02001016 qemu_cond_wait(cpu->halt_cond, &qemu_global_mutex);
Jan Kiszka16400322011-02-09 16:29:37 +01001017 }
Blue Swirl296af7c2010-03-29 19:23:50 +00001018
Andreas Färber290adf32013-01-17 09:30:27 +01001019 qemu_kvm_eat_signals(cpu);
Andreas Färber509a0d72012-05-03 02:18:09 +02001020 qemu_wait_io_event_common(cpu);
Blue Swirl296af7c2010-03-29 19:23:50 +00001021}
1022
Jan Kiszka7e97cd82011-02-07 12:19:12 +01001023static void *qemu_kvm_cpu_thread_fn(void *arg)
Blue Swirl296af7c2010-03-29 19:23:50 +00001024{
Andreas Färber48a106b2013-05-27 02:20:39 +02001025 CPUState *cpu = arg;
Jan Kiszka84b49152011-02-01 22:15:50 +01001026 int r;
Blue Swirl296af7c2010-03-29 19:23:50 +00001027
Paolo Bonziniab28bd22015-07-09 08:55:38 +02001028 rcu_register_thread();
1029
Paolo Bonzini2e7f7a32015-06-18 18:47:18 +02001030 qemu_mutex_lock_iothread();
Andreas Färber814e6122012-05-02 17:00:37 +02001031 qemu_thread_get_self(cpu->thread);
Andreas Färber9f09e182012-05-03 06:59:07 +02001032 cpu->thread_id = qemu_get_thread_id();
Pavel Dovgalyuk626cf8f2014-12-08 10:53:17 +03001033 cpu->can_do_io = 1;
Andreas Färber4917cf42013-05-27 05:17:50 +02001034 current_cpu = cpu;
Blue Swirl296af7c2010-03-29 19:23:50 +00001035
Andreas Färber504134d2012-12-17 06:38:45 +01001036 r = kvm_init_vcpu(cpu);
Jan Kiszka84b49152011-02-01 22:15:50 +01001037 if (r < 0) {
1038 fprintf(stderr, "kvm_init_vcpu failed: %s\n", strerror(-r));
1039 exit(1);
1040 }
Blue Swirl296af7c2010-03-29 19:23:50 +00001041
Andreas Färber13618e02013-05-26 23:41:00 +02001042 qemu_kvm_init_cpu_signals(cpu);
Blue Swirl296af7c2010-03-29 19:23:50 +00001043
1044 /* signal CPU creation */
Andreas Färber61a46212012-05-02 22:49:36 +02001045 cpu->created = true;
Blue Swirl296af7c2010-03-29 19:23:50 +00001046 qemu_cond_signal(&qemu_cpu_cond);
1047
Blue Swirl296af7c2010-03-29 19:23:50 +00001048 while (1) {
Andreas Färbera1fcaa72012-05-02 23:42:26 +02001049 if (cpu_can_run(cpu)) {
Andreas Färber1458c362013-05-26 23:46:55 +02001050 r = kvm_cpu_exec(cpu);
Jan Kiszka83f338f2011-02-07 12:19:17 +01001051 if (r == EXCP_DEBUG) {
Andreas Färber91325042013-05-27 02:07:49 +02001052 cpu_handle_guest_debug(cpu);
Jan Kiszka83f338f2011-02-07 12:19:17 +01001053 }
Jan Kiszka0ab07c62011-02-07 12:19:14 +01001054 }
Andreas Färberfd529e82013-05-26 23:24:55 +02001055 qemu_kvm_wait_io_event(cpu);
Blue Swirl296af7c2010-03-29 19:23:50 +00001056 }
1057
1058 return NULL;
1059}
1060
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +02001061static void *qemu_dummy_cpu_thread_fn(void *arg)
1062{
1063#ifdef _WIN32
1064 fprintf(stderr, "qtest is not supported under Windows\n");
1065 exit(1);
1066#else
Andreas Färber10a90212013-05-27 02:24:35 +02001067 CPUState *cpu = arg;
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +02001068 sigset_t waitset;
1069 int r;
1070
Paolo Bonziniab28bd22015-07-09 08:55:38 +02001071 rcu_register_thread();
1072
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +02001073 qemu_mutex_lock_iothread();
Andreas Färber814e6122012-05-02 17:00:37 +02001074 qemu_thread_get_self(cpu->thread);
Andreas Färber9f09e182012-05-03 06:59:07 +02001075 cpu->thread_id = qemu_get_thread_id();
Pavel Dovgalyuk626cf8f2014-12-08 10:53:17 +03001076 cpu->can_do_io = 1;
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +02001077
1078 sigemptyset(&waitset);
1079 sigaddset(&waitset, SIG_IPI);
1080
1081 /* signal CPU creation */
Andreas Färber61a46212012-05-02 22:49:36 +02001082 cpu->created = true;
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +02001083 qemu_cond_signal(&qemu_cpu_cond);
1084
Andreas Färber4917cf42013-05-27 05:17:50 +02001085 current_cpu = cpu;
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +02001086 while (1) {
Andreas Färber4917cf42013-05-27 05:17:50 +02001087 current_cpu = NULL;
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +02001088 qemu_mutex_unlock_iothread();
1089 do {
1090 int sig;
1091 r = sigwait(&waitset, &sig);
1092 } while (r == -1 && (errno == EAGAIN || errno == EINTR));
1093 if (r == -1) {
1094 perror("sigwait");
1095 exit(1);
1096 }
1097 qemu_mutex_lock_iothread();
Andreas Färber4917cf42013-05-27 05:17:50 +02001098 current_cpu = cpu;
Andreas Färber509a0d72012-05-03 02:18:09 +02001099 qemu_wait_io_event_common(cpu);
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +02001100 }
1101
1102 return NULL;
1103#endif
1104}
1105
Jan Kiszkabdb7ca62011-09-26 09:40:39 +02001106static void tcg_exec_all(void);
1107
Jan Kiszka7e97cd82011-02-07 12:19:12 +01001108static void *qemu_tcg_cpu_thread_fn(void *arg)
Blue Swirl296af7c2010-03-29 19:23:50 +00001109{
Andreas Färberc3586ba2012-05-03 01:41:24 +02001110 CPUState *cpu = arg;
Blue Swirl296af7c2010-03-29 19:23:50 +00001111
Paolo Bonziniab28bd22015-07-09 08:55:38 +02001112 rcu_register_thread();
1113
Paolo Bonzini2e7f7a32015-06-18 18:47:18 +02001114 qemu_mutex_lock_iothread();
Andreas Färber814e6122012-05-02 17:00:37 +02001115 qemu_thread_get_self(cpu->thread);
Blue Swirl296af7c2010-03-29 19:23:50 +00001116
Andreas Färber38fcbd32013-07-07 19:50:23 +02001117 CPU_FOREACH(cpu) {
1118 cpu->thread_id = qemu_get_thread_id();
1119 cpu->created = true;
Pavel Dovgalyuk626cf8f2014-12-08 10:53:17 +03001120 cpu->can_do_io = 1;
Andreas Färber38fcbd32013-07-07 19:50:23 +02001121 }
Blue Swirl296af7c2010-03-29 19:23:50 +00001122 qemu_cond_signal(&qemu_cpu_cond);
1123
Jan Kiszkafa7d1862011-08-22 18:35:25 +02001124 /* wait for initial kick-off after machine start */
Emilio G. Cotac28e3992015-04-27 12:45:28 -04001125 while (first_cpu->stopped) {
KONRAD Fredericd5f8d612015-08-10 17:27:06 +02001126 qemu_cond_wait(first_cpu->halt_cond, &qemu_global_mutex);
Jan Kiszka8e564b42012-02-17 18:31:15 +01001127
1128 /* process any pending work */
Andreas Färberbdc44642013-06-24 23:50:24 +02001129 CPU_FOREACH(cpu) {
Andreas Färber182735e2013-05-29 22:29:20 +02001130 qemu_wait_io_event_common(cpu);
Jan Kiszka8e564b42012-02-17 18:31:15 +01001131 }
Jan Kiszka0ab07c62011-02-07 12:19:14 +01001132 }
Blue Swirl296af7c2010-03-29 19:23:50 +00001133
Paolo Bonzini21618b32015-02-27 20:01:03 +01001134 /* process any pending work */
Paolo Bonziniaed807c2015-08-18 06:43:15 -07001135 atomic_mb_set(&exit_request, 1);
Paolo Bonzini21618b32015-02-27 20:01:03 +01001136
Blue Swirl296af7c2010-03-29 19:23:50 +00001137 while (1) {
Jan Kiszkabdb7ca62011-09-26 09:40:39 +02001138 tcg_exec_all();
Alex Blighac70aaf2013-08-21 16:02:57 +01001139
1140 if (use_icount) {
Alex Bligh40daca52013-08-21 16:03:02 +01001141 int64_t deadline = qemu_clock_deadline_ns_all(QEMU_CLOCK_VIRTUAL);
Alex Blighac70aaf2013-08-21 16:02:57 +01001142
1143 if (deadline == 0) {
Alex Bligh40daca52013-08-21 16:03:02 +01001144 qemu_clock_notify(QEMU_CLOCK_VIRTUAL);
Alex Blighac70aaf2013-08-21 16:02:57 +01001145 }
Paolo Bonzini3b2319a2011-04-13 10:03:43 +02001146 }
KONRAD Fredericd5f8d612015-08-10 17:27:06 +02001147 qemu_tcg_wait_io_event(QTAILQ_FIRST(&cpus));
Blue Swirl296af7c2010-03-29 19:23:50 +00001148 }
1149
1150 return NULL;
1151}
1152
Andreas Färber2ff09a42012-05-03 00:23:30 +02001153static void qemu_cpu_kick_thread(CPUState *cpu)
Paolo Bonzinicc015e92011-03-12 17:44:08 +01001154{
1155#ifndef _WIN32
1156 int err;
1157
Paolo Bonzinie0c38212015-08-26 00:19:19 +02001158 if (cpu->thread_kicked) {
1159 return;
Paolo Bonzini9102ded2015-08-18 06:52:09 -07001160 }
Paolo Bonzinie0c38212015-08-26 00:19:19 +02001161 cpu->thread_kicked = true;
Andreas Färber814e6122012-05-02 17:00:37 +02001162 err = pthread_kill(cpu->thread->thread, SIG_IPI);
Paolo Bonzinicc015e92011-03-12 17:44:08 +01001163 if (err) {
1164 fprintf(stderr, "qemu:%s: %s", __func__, strerror(err));
1165 exit(1);
1166 }
1167#else /* _WIN32 */
Paolo Bonzinie0c38212015-08-26 00:19:19 +02001168 abort();
Paolo Bonzinicc015e92011-03-12 17:44:08 +01001169#endif
1170}
1171
Paolo Bonzinie0c38212015-08-26 00:19:19 +02001172static void qemu_cpu_kick_no_halt(void)
1173{
1174 CPUState *cpu;
1175 /* Ensure whatever caused the exit has reached the CPU threads before
1176 * writing exit_request.
1177 */
1178 atomic_mb_set(&exit_request, 1);
1179 cpu = atomic_mb_read(&tcg_current_cpu);
1180 if (cpu) {
1181 cpu_exit(cpu);
1182 }
1183}
1184
Andreas Färberc08d7422012-05-03 04:34:15 +02001185void qemu_cpu_kick(CPUState *cpu)
Blue Swirl296af7c2010-03-29 19:23:50 +00001186{
Andreas Färberf5c121b2012-05-03 01:22:49 +02001187 qemu_cond_broadcast(cpu->halt_cond);
Paolo Bonzinie0c38212015-08-26 00:19:19 +02001188 if (tcg_enabled()) {
1189 qemu_cpu_kick_no_halt();
1190 } else {
1191 qemu_cpu_kick_thread(cpu);
1192 }
Blue Swirl296af7c2010-03-29 19:23:50 +00001193}
1194
Jan Kiszka46d62fa2011-02-01 22:15:59 +01001195void qemu_cpu_kick_self(void)
1196{
Andreas Färber4917cf42013-05-27 05:17:50 +02001197 assert(current_cpu);
Paolo Bonzini9102ded2015-08-18 06:52:09 -07001198 qemu_cpu_kick_thread(current_cpu);
Blue Swirl296af7c2010-03-29 19:23:50 +00001199}
1200
Andreas Färber60e82572012-05-02 22:23:49 +02001201bool qemu_cpu_is_self(CPUState *cpu)
Blue Swirl296af7c2010-03-29 19:23:50 +00001202{
Andreas Färber814e6122012-05-02 17:00:37 +02001203 return qemu_thread_is_self(cpu->thread);
Blue Swirl296af7c2010-03-29 19:23:50 +00001204}
1205
Paolo Bonzini79e2b9a2015-01-21 12:09:14 +01001206bool qemu_in_vcpu_thread(void)
Juan Quintelaaa723c22012-09-18 16:30:11 +02001207{
Andreas Färber4917cf42013-05-27 05:17:50 +02001208 return current_cpu && qemu_cpu_is_self(current_cpu);
Juan Quintelaaa723c22012-09-18 16:30:11 +02001209}
1210
Paolo Bonziniafbe7052015-06-18 18:47:19 +02001211static __thread bool iothread_locked = false;
1212
1213bool qemu_mutex_iothread_locked(void)
1214{
1215 return iothread_locked;
1216}
1217
Blue Swirl296af7c2010-03-29 19:23:50 +00001218void qemu_mutex_lock_iothread(void)
1219{
Paolo Bonzini21618b32015-02-27 20:01:03 +01001220 atomic_inc(&iothread_requesting_mutex);
Paolo Bonzini2e7f7a32015-06-18 18:47:18 +02001221 /* In the simple case there is no need to bump the VCPU thread out of
1222 * TCG code execution.
1223 */
1224 if (!tcg_enabled() || qemu_in_vcpu_thread() ||
Aníbal Limón46036b22015-09-03 15:48:33 -05001225 !first_cpu || !first_cpu->created) {
Blue Swirl296af7c2010-03-29 19:23:50 +00001226 qemu_mutex_lock(&qemu_global_mutex);
Paolo Bonzini21618b32015-02-27 20:01:03 +01001227 atomic_dec(&iothread_requesting_mutex);
Marcelo Tosatti1a28cac2010-05-04 09:45:20 -03001228 } else {
Marcelo Tosatti1a28cac2010-05-04 09:45:20 -03001229 if (qemu_mutex_trylock(&qemu_global_mutex)) {
Paolo Bonzinie0c38212015-08-26 00:19:19 +02001230 qemu_cpu_kick_no_halt();
Marcelo Tosatti1a28cac2010-05-04 09:45:20 -03001231 qemu_mutex_lock(&qemu_global_mutex);
1232 }
Paolo Bonzini6b498092015-02-27 19:58:23 +01001233 atomic_dec(&iothread_requesting_mutex);
Paolo Bonzini46daff12011-06-09 13:10:24 +02001234 qemu_cond_broadcast(&qemu_io_proceeded_cond);
Marcelo Tosatti1a28cac2010-05-04 09:45:20 -03001235 }
Paolo Bonziniafbe7052015-06-18 18:47:19 +02001236 iothread_locked = true;
Blue Swirl296af7c2010-03-29 19:23:50 +00001237}
1238
1239void qemu_mutex_unlock_iothread(void)
1240{
Paolo Bonziniafbe7052015-06-18 18:47:19 +02001241 iothread_locked = false;
Blue Swirl296af7c2010-03-29 19:23:50 +00001242 qemu_mutex_unlock(&qemu_global_mutex);
1243}
1244
1245static int all_vcpus_paused(void)
1246{
Andreas Färberbdc44642013-06-24 23:50:24 +02001247 CPUState *cpu;
Blue Swirl296af7c2010-03-29 19:23:50 +00001248
Andreas Färberbdc44642013-06-24 23:50:24 +02001249 CPU_FOREACH(cpu) {
Andreas Färber182735e2013-05-29 22:29:20 +02001250 if (!cpu->stopped) {
Blue Swirl296af7c2010-03-29 19:23:50 +00001251 return 0;
Jan Kiszka0ab07c62011-02-07 12:19:14 +01001252 }
Blue Swirl296af7c2010-03-29 19:23:50 +00001253 }
1254
1255 return 1;
1256}
1257
1258void pause_all_vcpus(void)
1259{
Andreas Färberbdc44642013-06-24 23:50:24 +02001260 CPUState *cpu;
Blue Swirl296af7c2010-03-29 19:23:50 +00001261
Alex Bligh40daca52013-08-21 16:03:02 +01001262 qemu_clock_enable(QEMU_CLOCK_VIRTUAL, false);
Andreas Färberbdc44642013-06-24 23:50:24 +02001263 CPU_FOREACH(cpu) {
Andreas Färber182735e2013-05-29 22:29:20 +02001264 cpu->stop = true;
1265 qemu_cpu_kick(cpu);
Blue Swirl296af7c2010-03-29 19:23:50 +00001266 }
1267
Juan Quintelaaa723c22012-09-18 16:30:11 +02001268 if (qemu_in_vcpu_thread()) {
Jan Kiszkad798e972012-02-17 18:31:16 +01001269 cpu_stop_current();
1270 if (!kvm_enabled()) {
Andreas Färberbdc44642013-06-24 23:50:24 +02001271 CPU_FOREACH(cpu) {
Andreas Färber182735e2013-05-29 22:29:20 +02001272 cpu->stop = false;
1273 cpu->stopped = true;
Jan Kiszkad798e972012-02-17 18:31:16 +01001274 }
1275 return;
1276 }
1277 }
1278
Blue Swirl296af7c2010-03-29 19:23:50 +00001279 while (!all_vcpus_paused()) {
Paolo Bonzinibe7d6c52011-03-12 17:44:02 +01001280 qemu_cond_wait(&qemu_pause_cond, &qemu_global_mutex);
Andreas Färberbdc44642013-06-24 23:50:24 +02001281 CPU_FOREACH(cpu) {
Andreas Färber182735e2013-05-29 22:29:20 +02001282 qemu_cpu_kick(cpu);
Blue Swirl296af7c2010-03-29 19:23:50 +00001283 }
1284 }
1285}
1286
Igor Mammedov29936832013-04-23 10:29:37 +02001287void cpu_resume(CPUState *cpu)
1288{
1289 cpu->stop = false;
1290 cpu->stopped = false;
1291 qemu_cpu_kick(cpu);
1292}
1293
Blue Swirl296af7c2010-03-29 19:23:50 +00001294void resume_all_vcpus(void)
1295{
Andreas Färberbdc44642013-06-24 23:50:24 +02001296 CPUState *cpu;
Blue Swirl296af7c2010-03-29 19:23:50 +00001297
Alex Bligh40daca52013-08-21 16:03:02 +01001298 qemu_clock_enable(QEMU_CLOCK_VIRTUAL, true);
Andreas Färberbdc44642013-06-24 23:50:24 +02001299 CPU_FOREACH(cpu) {
Andreas Färber182735e2013-05-29 22:29:20 +02001300 cpu_resume(cpu);
Blue Swirl296af7c2010-03-29 19:23:50 +00001301 }
1302}
1303
Dr. David Alan Gilbert49001162014-01-30 10:20:32 +00001304/* For temporary buffers for forming a name */
1305#define VCPU_THREAD_NAME_SIZE 16
1306
Andreas Färbere5ab30a2012-05-03 01:50:44 +02001307static void qemu_tcg_init_vcpu(CPUState *cpu)
Blue Swirl296af7c2010-03-29 19:23:50 +00001308{
Dr. David Alan Gilbert49001162014-01-30 10:20:32 +00001309 char thread_name[VCPU_THREAD_NAME_SIZE];
KONRAD Fredericd5f8d612015-08-10 17:27:06 +02001310 static QemuCond *tcg_halt_cond;
1311 static QemuThread *tcg_cpu_thread;
Dr. David Alan Gilbert49001162014-01-30 10:20:32 +00001312
Edgar E. Iglesias09daed82013-12-17 13:06:51 +10001313 tcg_cpu_address_space_init(cpu, cpu->as);
1314
Blue Swirl296af7c2010-03-29 19:23:50 +00001315 /* share a single thread for all cpus with TCG */
1316 if (!tcg_cpu_thread) {
Andreas Färber814e6122012-05-02 17:00:37 +02001317 cpu->thread = g_malloc0(sizeof(QemuThread));
Andreas Färberf5c121b2012-05-03 01:22:49 +02001318 cpu->halt_cond = g_malloc0(sizeof(QemuCond));
1319 qemu_cond_init(cpu->halt_cond);
1320 tcg_halt_cond = cpu->halt_cond;
Dr. David Alan Gilbert49001162014-01-30 10:20:32 +00001321 snprintf(thread_name, VCPU_THREAD_NAME_SIZE, "CPU %d/TCG",
1322 cpu->cpu_index);
1323 qemu_thread_create(cpu->thread, thread_name, qemu_tcg_cpu_thread_fn,
1324 cpu, QEMU_THREAD_JOINABLE);
Paolo Bonzini1ecf47b2011-12-13 13:43:52 +01001325#ifdef _WIN32
Andreas Färber814e6122012-05-02 17:00:37 +02001326 cpu->hThread = qemu_thread_get_handle(cpu->thread);
Paolo Bonzini1ecf47b2011-12-13 13:43:52 +01001327#endif
Andreas Färber61a46212012-05-02 22:49:36 +02001328 while (!cpu->created) {
Paolo Bonzini18a85722011-03-12 17:44:03 +01001329 qemu_cond_wait(&qemu_cpu_cond, &qemu_global_mutex);
Jan Kiszka0ab07c62011-02-07 12:19:14 +01001330 }
Andreas Färber814e6122012-05-02 17:00:37 +02001331 tcg_cpu_thread = cpu->thread;
Blue Swirl296af7c2010-03-29 19:23:50 +00001332 } else {
Andreas Färber814e6122012-05-02 17:00:37 +02001333 cpu->thread = tcg_cpu_thread;
Andreas Färberf5c121b2012-05-03 01:22:49 +02001334 cpu->halt_cond = tcg_halt_cond;
Blue Swirl296af7c2010-03-29 19:23:50 +00001335 }
1336}
1337
Andreas Färber48a106b2013-05-27 02:20:39 +02001338static void qemu_kvm_start_vcpu(CPUState *cpu)
Blue Swirl296af7c2010-03-29 19:23:50 +00001339{
Dr. David Alan Gilbert49001162014-01-30 10:20:32 +00001340 char thread_name[VCPU_THREAD_NAME_SIZE];
1341
Andreas Färber814e6122012-05-02 17:00:37 +02001342 cpu->thread = g_malloc0(sizeof(QemuThread));
Andreas Färberf5c121b2012-05-03 01:22:49 +02001343 cpu->halt_cond = g_malloc0(sizeof(QemuCond));
1344 qemu_cond_init(cpu->halt_cond);
Dr. David Alan Gilbert49001162014-01-30 10:20:32 +00001345 snprintf(thread_name, VCPU_THREAD_NAME_SIZE, "CPU %d/KVM",
1346 cpu->cpu_index);
1347 qemu_thread_create(cpu->thread, thread_name, qemu_kvm_cpu_thread_fn,
1348 cpu, QEMU_THREAD_JOINABLE);
Andreas Färber61a46212012-05-02 22:49:36 +02001349 while (!cpu->created) {
Paolo Bonzini18a85722011-03-12 17:44:03 +01001350 qemu_cond_wait(&qemu_cpu_cond, &qemu_global_mutex);
Jan Kiszka0ab07c62011-02-07 12:19:14 +01001351 }
Blue Swirl296af7c2010-03-29 19:23:50 +00001352}
1353
Andreas Färber10a90212013-05-27 02:24:35 +02001354static void qemu_dummy_start_vcpu(CPUState *cpu)
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +02001355{
Dr. David Alan Gilbert49001162014-01-30 10:20:32 +00001356 char thread_name[VCPU_THREAD_NAME_SIZE];
1357
Andreas Färber814e6122012-05-02 17:00:37 +02001358 cpu->thread = g_malloc0(sizeof(QemuThread));
Andreas Färberf5c121b2012-05-03 01:22:49 +02001359 cpu->halt_cond = g_malloc0(sizeof(QemuCond));
1360 qemu_cond_init(cpu->halt_cond);
Dr. David Alan Gilbert49001162014-01-30 10:20:32 +00001361 snprintf(thread_name, VCPU_THREAD_NAME_SIZE, "CPU %d/DUMMY",
1362 cpu->cpu_index);
1363 qemu_thread_create(cpu->thread, thread_name, qemu_dummy_cpu_thread_fn, cpu,
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +02001364 QEMU_THREAD_JOINABLE);
Andreas Färber61a46212012-05-02 22:49:36 +02001365 while (!cpu->created) {
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +02001366 qemu_cond_wait(&qemu_cpu_cond, &qemu_global_mutex);
1367 }
1368}
1369
Andreas Färberc643bed2013-05-27 03:23:24 +02001370void qemu_init_vcpu(CPUState *cpu)
Blue Swirl296af7c2010-03-29 19:23:50 +00001371{
Andreas Färberce3960e2012-12-17 03:27:07 +01001372 cpu->nr_cores = smp_cores;
1373 cpu->nr_threads = smp_threads;
Andreas Färberf324e762012-05-02 23:26:21 +02001374 cpu->stopped = true;
Jan Kiszka0ab07c62011-02-07 12:19:14 +01001375 if (kvm_enabled()) {
Andreas Färber48a106b2013-05-27 02:20:39 +02001376 qemu_kvm_start_vcpu(cpu);
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +02001377 } else if (tcg_enabled()) {
Andreas Färbere5ab30a2012-05-03 01:50:44 +02001378 qemu_tcg_init_vcpu(cpu);
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +02001379 } else {
Andreas Färber10a90212013-05-27 02:24:35 +02001380 qemu_dummy_start_vcpu(cpu);
Jan Kiszka0ab07c62011-02-07 12:19:14 +01001381 }
Blue Swirl296af7c2010-03-29 19:23:50 +00001382}
1383
Jan Kiszkab4a3d962011-02-01 22:15:43 +01001384void cpu_stop_current(void)
Blue Swirl296af7c2010-03-29 19:23:50 +00001385{
Andreas Färber4917cf42013-05-27 05:17:50 +02001386 if (current_cpu) {
1387 current_cpu->stop = false;
1388 current_cpu->stopped = true;
1389 cpu_exit(current_cpu);
Paolo Bonzini67bb1722011-03-12 17:43:59 +01001390 qemu_cond_signal(&qemu_pause_cond);
Jan Kiszkab4a3d962011-02-01 22:15:43 +01001391 }
Blue Swirl296af7c2010-03-29 19:23:50 +00001392}
1393
Kevin Wolf56983462013-07-05 13:49:54 +02001394int vm_stop(RunState state)
Blue Swirl296af7c2010-03-29 19:23:50 +00001395{
Juan Quintelaaa723c22012-09-18 16:30:11 +02001396 if (qemu_in_vcpu_thread()) {
Paolo Bonzini74892d22014-06-05 14:53:58 +02001397 qemu_system_vmstop_request_prepare();
Luiz Capitulino1dfb4dd2011-07-29 14:26:33 -03001398 qemu_system_vmstop_request(state);
Blue Swirl296af7c2010-03-29 19:23:50 +00001399 /*
1400 * FIXME: should not return to device code in case
1401 * vm_stop() has been requested.
1402 */
Jan Kiszkab4a3d962011-02-01 22:15:43 +01001403 cpu_stop_current();
Kevin Wolf56983462013-07-05 13:49:54 +02001404 return 0;
Blue Swirl296af7c2010-03-29 19:23:50 +00001405 }
Kevin Wolf56983462013-07-05 13:49:54 +02001406
1407 return do_vm_stop(state);
Blue Swirl296af7c2010-03-29 19:23:50 +00001408}
1409
Luiz Capitulino8a9236f2011-10-14 11:18:09 -03001410/* does a state transition even if the VM is already stopped,
1411 current state is forgotten forever */
Kevin Wolf56983462013-07-05 13:49:54 +02001412int vm_stop_force_state(RunState state)
Luiz Capitulino8a9236f2011-10-14 11:18:09 -03001413{
1414 if (runstate_is_running()) {
Kevin Wolf56983462013-07-05 13:49:54 +02001415 return vm_stop(state);
Luiz Capitulino8a9236f2011-10-14 11:18:09 -03001416 } else {
1417 runstate_set(state);
Wen Congyangb2780d32015-11-20 17:34:38 +08001418
1419 bdrv_drain_all();
Kevin Wolf594a45c2013-07-18 14:52:19 +02001420 /* Make sure to return an error if the flush in a previous vm_stop()
1421 * failed. */
1422 return bdrv_flush_all();
Luiz Capitulino8a9236f2011-10-14 11:18:09 -03001423 }
1424}
1425
Pavel Dovgalyuk8b427042015-09-17 19:24:05 +03001426static int64_t tcg_get_icount_limit(void)
1427{
1428 int64_t deadline;
1429
1430 if (replay_mode != REPLAY_MODE_PLAY) {
1431 deadline = qemu_clock_deadline_ns_all(QEMU_CLOCK_VIRTUAL);
1432
1433 /* Maintain prior (possibly buggy) behaviour where if no deadline
1434 * was set (as there is no QEMU_CLOCK_VIRTUAL timer) or it is more than
1435 * INT32_MAX nanoseconds ahead, we still use INT32_MAX
1436 * nanoseconds.
1437 */
1438 if ((deadline < 0) || (deadline > INT32_MAX)) {
1439 deadline = INT32_MAX;
1440 }
1441
1442 return qemu_icount_round(deadline);
1443 } else {
1444 return replay_get_instructions();
1445 }
1446}
1447
Peter Crosthwaite3d57f782015-06-23 19:31:17 -07001448static int tcg_cpu_exec(CPUState *cpu)
Blue Swirl296af7c2010-03-29 19:23:50 +00001449{
1450 int ret;
1451#ifdef CONFIG_PROFILER
1452 int64_t ti;
1453#endif
1454
1455#ifdef CONFIG_PROFILER
1456 ti = profile_getclock();
1457#endif
1458 if (use_icount) {
1459 int64_t count;
1460 int decr;
KONRAD Fredericc96778b2014-08-01 01:37:09 +02001461 timers_state.qemu_icount -= (cpu->icount_decr.u16.low
1462 + cpu->icount_extra);
Andreas Färber28ecfd72013-08-26 05:51:49 +02001463 cpu->icount_decr.u16.low = 0;
Andreas Färberefee7342013-08-26 05:39:29 +02001464 cpu->icount_extra = 0;
Pavel Dovgalyuk8b427042015-09-17 19:24:05 +03001465 count = tcg_get_icount_limit();
KONRAD Fredericc96778b2014-08-01 01:37:09 +02001466 timers_state.qemu_icount += count;
Blue Swirl296af7c2010-03-29 19:23:50 +00001467 decr = (count > 0xffff) ? 0xffff : count;
1468 count -= decr;
Andreas Färber28ecfd72013-08-26 05:51:49 +02001469 cpu->icount_decr.u16.low = decr;
Andreas Färberefee7342013-08-26 05:39:29 +02001470 cpu->icount_extra = count;
Blue Swirl296af7c2010-03-29 19:23:50 +00001471 }
Peter Crosthwaiteea3e9842015-06-18 10:24:55 -07001472 ret = cpu_exec(cpu);
Blue Swirl296af7c2010-03-29 19:23:50 +00001473#ifdef CONFIG_PROFILER
Alexey Kardashevskiy89d5cbd2015-03-16 14:57:38 +11001474 tcg_time += profile_getclock() - ti;
Blue Swirl296af7c2010-03-29 19:23:50 +00001475#endif
1476 if (use_icount) {
1477 /* Fold pending instructions back into the
1478 instruction counter, and clear the interrupt flag. */
KONRAD Fredericc96778b2014-08-01 01:37:09 +02001479 timers_state.qemu_icount -= (cpu->icount_decr.u16.low
1480 + cpu->icount_extra);
Andreas Färber28ecfd72013-08-26 05:51:49 +02001481 cpu->icount_decr.u32 = 0;
Andreas Färberefee7342013-08-26 05:39:29 +02001482 cpu->icount_extra = 0;
Pavel Dovgalyuk8b427042015-09-17 19:24:05 +03001483 replay_account_executed_instructions();
Blue Swirl296af7c2010-03-29 19:23:50 +00001484 }
1485 return ret;
1486}
1487
Jan Kiszkabdb7ca62011-09-26 09:40:39 +02001488static void tcg_exec_all(void)
Blue Swirl296af7c2010-03-29 19:23:50 +00001489{
Jan Kiszka9a360852011-02-01 22:15:55 +01001490 int r;
1491
Alex Bligh40daca52013-08-21 16:03:02 +01001492 /* Account partial waits to QEMU_CLOCK_VIRTUAL. */
1493 qemu_clock_warp(QEMU_CLOCK_VIRTUAL);
Paolo Bonziniab33fcd2011-04-13 10:03:44 +02001494
Jan Kiszka0ab07c62011-02-07 12:19:14 +01001495 if (next_cpu == NULL) {
Blue Swirl296af7c2010-03-29 19:23:50 +00001496 next_cpu = first_cpu;
Jan Kiszka0ab07c62011-02-07 12:19:14 +01001497 }
Andreas Färberbdc44642013-06-24 23:50:24 +02001498 for (; next_cpu != NULL && !exit_request; next_cpu = CPU_NEXT(next_cpu)) {
Andreas Färber182735e2013-05-29 22:29:20 +02001499 CPUState *cpu = next_cpu;
Blue Swirl296af7c2010-03-29 19:23:50 +00001500
Alex Bligh40daca52013-08-21 16:03:02 +01001501 qemu_clock_enable(QEMU_CLOCK_VIRTUAL,
Andreas Färbered2803d2013-06-21 20:20:45 +02001502 (cpu->singlestep_enabled & SSTEP_NOTIMER) == 0);
Blue Swirl296af7c2010-03-29 19:23:50 +00001503
Andreas Färbera1fcaa72012-05-02 23:42:26 +02001504 if (cpu_can_run(cpu)) {
Peter Crosthwaite3d57f782015-06-23 19:31:17 -07001505 r = tcg_cpu_exec(cpu);
Jan Kiszka9a360852011-02-01 22:15:55 +01001506 if (r == EXCP_DEBUG) {
Andreas Färber91325042013-05-27 02:07:49 +02001507 cpu_handle_guest_debug(cpu);
Jan Kiszka3c638d02010-06-25 16:56:56 +02001508 break;
1509 }
Andreas Färberf324e762012-05-02 23:26:21 +02001510 } else if (cpu->stop || cpu->stopped) {
Blue Swirl296af7c2010-03-29 19:23:50 +00001511 break;
1512 }
1513 }
Paolo Bonziniaed807c2015-08-18 06:43:15 -07001514
1515 /* Pairs with smp_wmb in qemu_cpu_kick. */
1516 atomic_mb_set(&exit_request, 0);
Blue Swirl296af7c2010-03-29 19:23:50 +00001517}
1518
Stefan Weil9a78eea2010-10-22 23:03:33 +02001519void list_cpus(FILE *f, fprintf_function cpu_fprintf, const char *optarg)
Blue Swirl262353c2010-05-04 19:55:35 +00001520{
1521 /* XXX: implement xxx_cpu_list for targets that still miss it */
Peter Maydelle916cbf2012-09-05 17:41:08 -03001522#if defined(cpu_list)
1523 cpu_list(f, cpu_fprintf);
Blue Swirl262353c2010-05-04 19:55:35 +00001524#endif
1525}
Luiz Capitulinode0b36b2011-09-21 16:38:35 -03001526
1527CpuInfoList *qmp_query_cpus(Error **errp)
1528{
1529 CpuInfoList *head = NULL, *cur_item = NULL;
Andreas Färber182735e2013-05-29 22:29:20 +02001530 CPUState *cpu;
Luiz Capitulinode0b36b2011-09-21 16:38:35 -03001531
Andreas Färberbdc44642013-06-24 23:50:24 +02001532 CPU_FOREACH(cpu) {
Luiz Capitulinode0b36b2011-09-21 16:38:35 -03001533 CpuInfoList *info;
Andreas Färber182735e2013-05-29 22:29:20 +02001534#if defined(TARGET_I386)
1535 X86CPU *x86_cpu = X86_CPU(cpu);
1536 CPUX86State *env = &x86_cpu->env;
1537#elif defined(TARGET_PPC)
1538 PowerPCCPU *ppc_cpu = POWERPC_CPU(cpu);
1539 CPUPPCState *env = &ppc_cpu->env;
1540#elif defined(TARGET_SPARC)
1541 SPARCCPU *sparc_cpu = SPARC_CPU(cpu);
1542 CPUSPARCState *env = &sparc_cpu->env;
1543#elif defined(TARGET_MIPS)
1544 MIPSCPU *mips_cpu = MIPS_CPU(cpu);
1545 CPUMIPSState *env = &mips_cpu->env;
Bastian Koppelmann48e06fe2014-09-01 12:59:46 +01001546#elif defined(TARGET_TRICORE)
1547 TriCoreCPU *tricore_cpu = TRICORE_CPU(cpu);
1548 CPUTriCoreState *env = &tricore_cpu->env;
Andreas Färber182735e2013-05-29 22:29:20 +02001549#endif
Luiz Capitulinode0b36b2011-09-21 16:38:35 -03001550
Andreas Färbercb446ec2013-05-01 14:24:52 +02001551 cpu_synchronize_state(cpu);
Luiz Capitulinode0b36b2011-09-21 16:38:35 -03001552
1553 info = g_malloc0(sizeof(*info));
1554 info->value = g_malloc0(sizeof(*info->value));
Andreas Färber55e5c282012-12-17 06:18:02 +01001555 info->value->CPU = cpu->cpu_index;
Andreas Färber182735e2013-05-29 22:29:20 +02001556 info->value->current = (cpu == first_cpu);
Andreas Färber259186a2013-01-17 18:51:17 +01001557 info->value->halted = cpu->halted;
Eduardo Habkost58f88d42015-05-08 16:04:22 -03001558 info->value->qom_path = object_get_canonical_path(OBJECT(cpu));
Andreas Färber9f09e182012-05-03 06:59:07 +02001559 info->value->thread_id = cpu->thread_id;
Luiz Capitulinode0b36b2011-09-21 16:38:35 -03001560#if defined(TARGET_I386)
1561 info->value->has_pc = true;
1562 info->value->pc = env->eip + env->segs[R_CS].base;
1563#elif defined(TARGET_PPC)
1564 info->value->has_nip = true;
1565 info->value->nip = env->nip;
1566#elif defined(TARGET_SPARC)
1567 info->value->has_pc = true;
1568 info->value->pc = env->pc;
1569 info->value->has_npc = true;
1570 info->value->npc = env->npc;
1571#elif defined(TARGET_MIPS)
1572 info->value->has_PC = true;
1573 info->value->PC = env->active_tc.PC;
Bastian Koppelmann48e06fe2014-09-01 12:59:46 +01001574#elif defined(TARGET_TRICORE)
1575 info->value->has_PC = true;
1576 info->value->PC = env->PC;
Luiz Capitulinode0b36b2011-09-21 16:38:35 -03001577#endif
1578
1579 /* XXX: waiting for the qapi to support GSList */
1580 if (!cur_item) {
1581 head = cur_item = info;
1582 } else {
1583 cur_item->next = info;
1584 cur_item = info;
1585 }
1586 }
1587
1588 return head;
1589}
Luiz Capitulino0cfd6a92011-11-22 16:32:37 -02001590
1591void qmp_memsave(int64_t addr, int64_t size, const char *filename,
1592 bool has_cpu, int64_t cpu_index, Error **errp)
1593{
1594 FILE *f;
1595 uint32_t l;
Andreas Färber55e5c282012-12-17 06:18:02 +01001596 CPUState *cpu;
Luiz Capitulino0cfd6a92011-11-22 16:32:37 -02001597 uint8_t buf[1024];
Borislav Petkov0dc9daf2015-02-08 13:14:38 +01001598 int64_t orig_addr = addr, orig_size = size;
Luiz Capitulino0cfd6a92011-11-22 16:32:37 -02001599
1600 if (!has_cpu) {
1601 cpu_index = 0;
1602 }
1603
Andreas Färber151d1322013-02-15 15:41:49 +01001604 cpu = qemu_get_cpu(cpu_index);
1605 if (cpu == NULL) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001606 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "cpu-index",
1607 "a CPU number");
Luiz Capitulino0cfd6a92011-11-22 16:32:37 -02001608 return;
1609 }
1610
1611 f = fopen(filename, "wb");
1612 if (!f) {
Luiz Capitulino618da852013-06-07 14:35:06 -04001613 error_setg_file_open(errp, errno, filename);
Luiz Capitulino0cfd6a92011-11-22 16:32:37 -02001614 return;
1615 }
1616
1617 while (size != 0) {
1618 l = sizeof(buf);
1619 if (l > size)
1620 l = size;
Aneesh Kumar K.V2f4d0f52013-10-01 21:49:30 +05301621 if (cpu_memory_rw_debug(cpu, addr, buf, l, 0) != 0) {
Borislav Petkov0dc9daf2015-02-08 13:14:38 +01001622 error_setg(errp, "Invalid addr 0x%016" PRIx64 "/size %" PRId64
1623 " specified", orig_addr, orig_size);
Aneesh Kumar K.V2f4d0f52013-10-01 21:49:30 +05301624 goto exit;
1625 }
Luiz Capitulino0cfd6a92011-11-22 16:32:37 -02001626 if (fwrite(buf, 1, l, f) != l) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001627 error_setg(errp, QERR_IO_ERROR);
Luiz Capitulino0cfd6a92011-11-22 16:32:37 -02001628 goto exit;
1629 }
1630 addr += l;
1631 size -= l;
1632 }
1633
1634exit:
1635 fclose(f);
1636}
Luiz Capitulino6d3962b2011-11-22 17:26:46 -02001637
1638void qmp_pmemsave(int64_t addr, int64_t size, const char *filename,
1639 Error **errp)
1640{
1641 FILE *f;
1642 uint32_t l;
1643 uint8_t buf[1024];
1644
1645 f = fopen(filename, "wb");
1646 if (!f) {
Luiz Capitulino618da852013-06-07 14:35:06 -04001647 error_setg_file_open(errp, errno, filename);
Luiz Capitulino6d3962b2011-11-22 17:26:46 -02001648 return;
1649 }
1650
1651 while (size != 0) {
1652 l = sizeof(buf);
1653 if (l > size)
1654 l = size;
Stefan Weileb6282f2014-04-07 20:28:23 +02001655 cpu_physical_memory_read(addr, buf, l);
Luiz Capitulino6d3962b2011-11-22 17:26:46 -02001656 if (fwrite(buf, 1, l, f) != l) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001657 error_setg(errp, QERR_IO_ERROR);
Luiz Capitulino6d3962b2011-11-22 17:26:46 -02001658 goto exit;
1659 }
1660 addr += l;
1661 size -= l;
1662 }
1663
1664exit:
1665 fclose(f);
1666}
Luiz Capitulinoab49ab52011-11-23 12:55:53 -02001667
1668void qmp_inject_nmi(Error **errp)
1669{
1670#if defined(TARGET_I386)
Andreas Färber182735e2013-05-29 22:29:20 +02001671 CPUState *cs;
Luiz Capitulinoab49ab52011-11-23 12:55:53 -02001672
Andreas Färberbdc44642013-06-24 23:50:24 +02001673 CPU_FOREACH(cs) {
Andreas Färber182735e2013-05-29 22:29:20 +02001674 X86CPU *cpu = X86_CPU(cs);
Andreas Färber182735e2013-05-29 22:29:20 +02001675
Chen Fan02e51482013-12-23 17:04:02 +08001676 if (!cpu->apic_state) {
Andreas Färber182735e2013-05-29 22:29:20 +02001677 cpu_interrupt(cs, CPU_INTERRUPT_NMI);
Jan Kiszka02c09192011-10-18 00:00:06 +08001678 } else {
Chen Fan02e51482013-12-23 17:04:02 +08001679 apic_deliver_nmi(cpu->apic_state);
Jan Kiszka02c09192011-10-18 00:00:06 +08001680 }
Luiz Capitulinoab49ab52011-11-23 12:55:53 -02001681 }
1682#else
Alexey Kardashevskiy9cb805f2014-08-20 22:16:33 +10001683 nmi_monitor_handle(monitor_get_cpu_index(), errp);
Luiz Capitulinoab49ab52011-11-23 12:55:53 -02001684#endif
1685}
Sebastian Tanase27498be2014-07-25 11:56:33 +02001686
1687void dump_drift_info(FILE *f, fprintf_function cpu_fprintf)
1688{
1689 if (!use_icount) {
1690 return;
1691 }
1692
1693 cpu_fprintf(f, "Host - Guest clock %"PRIi64" ms\n",
1694 (cpu_get_clock() - cpu_get_icount())/SCALE_MS);
1695 if (icount_align_option) {
1696 cpu_fprintf(f, "Max guest delay %"PRIi64" ms\n", -max_delay/SCALE_MS);
1697 cpu_fprintf(f, "Max guest advance %"PRIi64" ms\n", max_advance/SCALE_MS);
1698 } else {
1699 cpu_fprintf(f, "Max guest delay NA\n");
1700 cpu_fprintf(f, "Max guest advance NA\n");
1701 }
1702}