blob: 2b5c0bd7c74c07a85ddaf8eda145ef4ad99c003c [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"
Paolo Bonzini9c17d612012-12-17 18:20:04 +010030#include "sysemu/sysemu.h"
Paolo Bonzini022c62c2012-12-17 18:19:49 +010031#include "exec/gdbstub.h"
Paolo Bonzini9c17d612012-12-17 18:20:04 +010032#include "sysemu/dma.h"
33#include "sysemu/kvm.h"
Luiz Capitulinode0b36b2011-09-21 16:38:35 -030034#include "qmp-commands.h"
Blue Swirl296af7c2010-03-29 19:23:50 +000035
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010036#include "qemu/thread.h"
Paolo Bonzini9c17d612012-12-17 18:20:04 +010037#include "sysemu/cpus.h"
38#include "sysemu/qtest.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010039#include "qemu/main-loop.h"
40#include "qemu/bitmap.h"
Liu Ping Fancb365642013-09-25 14:20:58 +080041#include "qemu/seqlock.h"
Wenchao Xiaa4e15de2014-06-18 08:43:36 +020042#include "qapi-event.h"
Jan Kiszka0ff0fc12011-06-23 10:15:55 +020043
44#ifndef _WIN32
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010045#include "qemu/compatfd.h"
Jan Kiszka0ff0fc12011-06-23 10:15:55 +020046#endif
Blue Swirl296af7c2010-03-29 19:23:50 +000047
Jan Kiszka6d9cb732011-02-01 22:15:58 +010048#ifdef CONFIG_LINUX
49
50#include <sys/prctl.h>
51
Marcelo Tosattic0532a72010-10-11 15:31:21 -030052#ifndef PR_MCE_KILL
53#define PR_MCE_KILL 33
54#endif
55
Jan Kiszka6d9cb732011-02-01 22:15:58 +010056#ifndef PR_MCE_KILL_SET
57#define PR_MCE_KILL_SET 1
58#endif
59
60#ifndef PR_MCE_KILL_EARLY
61#define PR_MCE_KILL_EARLY 1
62#endif
63
64#endif /* CONFIG_LINUX */
65
Andreas Färber182735e2013-05-29 22:29:20 +020066static CPUState *next_cpu;
Sebastian Tanase27498be2014-07-25 11:56:33 +020067int64_t max_delay;
68int64_t max_advance;
Blue Swirl296af7c2010-03-29 19:23:50 +000069
Tiejun Chen321bc0b2013-08-02 09:43:09 +080070bool cpu_is_stopped(CPUState *cpu)
71{
72 return cpu->stopped || !runstate_is_running();
73}
74
Andreas Färbera98ae1d2013-05-26 23:21:08 +020075static bool cpu_thread_is_idle(CPUState *cpu)
Peter Maydellac873f12012-07-19 16:52:27 +010076{
Andreas Färberc64ca812012-05-03 02:11:45 +020077 if (cpu->stop || cpu->queued_work_first) {
Peter Maydellac873f12012-07-19 16:52:27 +010078 return false;
79 }
Tiejun Chen321bc0b2013-08-02 09:43:09 +080080 if (cpu_is_stopped(cpu)) {
Peter Maydellac873f12012-07-19 16:52:27 +010081 return true;
82 }
Andreas Färber8c2e1b02013-08-25 18:53:55 +020083 if (!cpu->halted || cpu_has_work(cpu) ||
Alexander Graf215e79c2013-04-24 22:24:12 +020084 kvm_halt_in_kernel()) {
Peter Maydellac873f12012-07-19 16:52:27 +010085 return false;
86 }
87 return true;
88}
89
90static bool all_cpu_threads_idle(void)
91{
Andreas Färber182735e2013-05-29 22:29:20 +020092 CPUState *cpu;
Peter Maydellac873f12012-07-19 16:52:27 +010093
Andreas Färberbdc44642013-06-24 23:50:24 +020094 CPU_FOREACH(cpu) {
Andreas Färber182735e2013-05-29 22:29:20 +020095 if (!cpu_thread_is_idle(cpu)) {
Peter Maydellac873f12012-07-19 16:52:27 +010096 return false;
97 }
98 }
99 return true;
100}
101
Blue Swirl296af7c2010-03-29 19:23:50 +0000102/***********************************************************/
Paolo Bonzini946fb272011-09-12 13:57:37 +0200103/* guest cycle counter */
104
Paolo Bonzinia3270e12013-10-07 17:18:15 +0200105/* Protected by TimersState seqlock */
106
Sebastian Tanase71468392014-07-23 11:47:50 +0200107static int64_t vm_clock_warp_start = -1;
Paolo Bonzini946fb272011-09-12 13:57:37 +0200108/* Conversion factor from emulated instructions to virtual clock ticks. */
109static int icount_time_shift;
110/* Arbitrarily pick 1MIPS as the minimum allowable speed. */
111#define MAX_ICOUNT_SHIFT 10
Paolo Bonzinia3270e12013-10-07 17:18:15 +0200112
Paolo Bonzini946fb272011-09-12 13:57:37 +0200113static QEMUTimer *icount_rt_timer;
114static QEMUTimer *icount_vm_timer;
115static QEMUTimer *icount_warp_timer;
Paolo Bonzini946fb272011-09-12 13:57:37 +0200116
117typedef struct TimersState {
Liu Ping Fancb365642013-09-25 14:20:58 +0800118 /* Protected by BQL. */
Paolo Bonzini946fb272011-09-12 13:57:37 +0200119 int64_t cpu_ticks_prev;
120 int64_t cpu_ticks_offset;
Liu Ping Fancb365642013-09-25 14:20:58 +0800121
122 /* cpu_clock_offset can be read out of BQL, so protect it with
123 * this lock.
124 */
125 QemuSeqLock vm_clock_seqlock;
Paolo Bonzini946fb272011-09-12 13:57:37 +0200126 int64_t cpu_clock_offset;
127 int32_t cpu_ticks_enabled;
128 int64_t dummy;
KONRAD Fredericc96778b2014-08-01 01:37:09 +0200129
130 /* Compensate for varying guest execution speed. */
131 int64_t qemu_icount_bias;
132 /* Only written by TCG thread */
133 int64_t qemu_icount;
Paolo Bonzini946fb272011-09-12 13:57:37 +0200134} TimersState;
135
Liu Ping Fand9cd4002013-07-21 08:43:00 +0000136static TimersState timers_state;
Paolo Bonzini946fb272011-09-12 13:57:37 +0200137
138/* Return the virtual CPU time, based on the instruction counter. */
Paolo Bonzini17a15f12013-10-03 15:17:25 +0200139static int64_t cpu_get_icount_locked(void)
Paolo Bonzini946fb272011-09-12 13:57:37 +0200140{
141 int64_t icount;
Andreas Färber4917cf42013-05-27 05:17:50 +0200142 CPUState *cpu = current_cpu;
Paolo Bonzini946fb272011-09-12 13:57:37 +0200143
KONRAD Fredericc96778b2014-08-01 01:37:09 +0200144 icount = timers_state.qemu_icount;
Andreas Färber4917cf42013-05-27 05:17:50 +0200145 if (cpu) {
Andreas Färber99df7dc2013-08-26 05:15:23 +0200146 if (!cpu_can_do_io(cpu)) {
Paolo Bonzini946fb272011-09-12 13:57:37 +0200147 fprintf(stderr, "Bad clock read\n");
148 }
Andreas Färber28ecfd72013-08-26 05:51:49 +0200149 icount -= (cpu->icount_decr.u16.low + cpu->icount_extra);
Paolo Bonzini946fb272011-09-12 13:57:37 +0200150 }
KONRAD Frederic3f031312014-08-01 01:37:15 +0200151 return timers_state.qemu_icount_bias + cpu_icount_to_ns(icount);
Paolo Bonzini946fb272011-09-12 13:57:37 +0200152}
153
Paolo Bonzini17a15f12013-10-03 15:17:25 +0200154int64_t cpu_get_icount(void)
155{
156 int64_t icount;
157 unsigned start;
158
159 do {
160 start = seqlock_read_begin(&timers_state.vm_clock_seqlock);
161 icount = cpu_get_icount_locked();
162 } while (seqlock_read_retry(&timers_state.vm_clock_seqlock, start));
163
164 return icount;
165}
166
KONRAD Frederic3f031312014-08-01 01:37:15 +0200167int64_t cpu_icount_to_ns(int64_t icount)
168{
169 return icount << icount_time_shift;
170}
171
Paolo Bonzini946fb272011-09-12 13:57:37 +0200172/* return the host CPU cycle counter and handle stop/restart */
Liu Ping Fancb365642013-09-25 14:20:58 +0800173/* Caller must hold the BQL */
Paolo Bonzini946fb272011-09-12 13:57:37 +0200174int64_t cpu_get_ticks(void)
175{
Paolo Bonzini5f3e3102013-10-28 17:32:18 +0100176 int64_t ticks;
177
Paolo Bonzini946fb272011-09-12 13:57:37 +0200178 if (use_icount) {
179 return cpu_get_icount();
180 }
Paolo Bonzini5f3e3102013-10-28 17:32:18 +0100181
182 ticks = timers_state.cpu_ticks_offset;
183 if (timers_state.cpu_ticks_enabled) {
184 ticks += cpu_get_real_ticks();
Paolo Bonzini946fb272011-09-12 13:57:37 +0200185 }
Paolo Bonzini5f3e3102013-10-28 17:32:18 +0100186
187 if (timers_state.cpu_ticks_prev > ticks) {
188 /* Note: non increasing ticks may happen if the host uses
189 software suspend */
190 timers_state.cpu_ticks_offset += timers_state.cpu_ticks_prev - ticks;
191 ticks = timers_state.cpu_ticks_prev;
192 }
193
194 timers_state.cpu_ticks_prev = ticks;
195 return ticks;
Paolo Bonzini946fb272011-09-12 13:57:37 +0200196}
197
Liu Ping Fancb365642013-09-25 14:20:58 +0800198static int64_t cpu_get_clock_locked(void)
199{
Paolo Bonzini5f3e3102013-10-28 17:32:18 +0100200 int64_t ticks;
Liu Ping Fancb365642013-09-25 14:20:58 +0800201
Paolo Bonzini5f3e3102013-10-28 17:32:18 +0100202 ticks = timers_state.cpu_clock_offset;
203 if (timers_state.cpu_ticks_enabled) {
204 ticks += get_clock();
Liu Ping Fancb365642013-09-25 14:20:58 +0800205 }
206
Paolo Bonzini5f3e3102013-10-28 17:32:18 +0100207 return ticks;
Liu Ping Fancb365642013-09-25 14:20:58 +0800208}
209
Paolo Bonzini946fb272011-09-12 13:57:37 +0200210/* return the host CPU monotonic timer and handle stop/restart */
211int64_t cpu_get_clock(void)
212{
213 int64_t ti;
Liu Ping Fancb365642013-09-25 14:20:58 +0800214 unsigned start;
215
216 do {
217 start = seqlock_read_begin(&timers_state.vm_clock_seqlock);
218 ti = cpu_get_clock_locked();
219 } while (seqlock_read_retry(&timers_state.vm_clock_seqlock, start));
220
221 return ti;
Paolo Bonzini946fb272011-09-12 13:57:37 +0200222}
223
Sebastian Tanasec2aa5f82014-07-25 11:56:31 +0200224/* return the offset between the host clock and virtual CPU clock */
225int64_t cpu_get_clock_offset(void)
226{
227 int64_t ti;
228 unsigned start;
229
230 do {
231 start = seqlock_read_begin(&timers_state.vm_clock_seqlock);
232 ti = timers_state.cpu_clock_offset;
233 if (!timers_state.cpu_ticks_enabled) {
234 ti -= get_clock();
235 }
236 } while (seqlock_read_retry(&timers_state.vm_clock_seqlock, start));
237
238 return -ti;
239}
240
Liu Ping Fancb365642013-09-25 14:20:58 +0800241/* enable cpu_get_ticks()
242 * Caller must hold BQL which server as mutex for vm_clock_seqlock.
243 */
Paolo Bonzini946fb272011-09-12 13:57:37 +0200244void cpu_enable_ticks(void)
245{
Liu Ping Fancb365642013-09-25 14:20:58 +0800246 /* Here, the really thing protected by seqlock is cpu_clock_offset. */
247 seqlock_write_lock(&timers_state.vm_clock_seqlock);
Paolo Bonzini946fb272011-09-12 13:57:37 +0200248 if (!timers_state.cpu_ticks_enabled) {
249 timers_state.cpu_ticks_offset -= cpu_get_real_ticks();
250 timers_state.cpu_clock_offset -= get_clock();
251 timers_state.cpu_ticks_enabled = 1;
252 }
Liu Ping Fancb365642013-09-25 14:20:58 +0800253 seqlock_write_unlock(&timers_state.vm_clock_seqlock);
Paolo Bonzini946fb272011-09-12 13:57:37 +0200254}
255
256/* disable cpu_get_ticks() : the clock is stopped. You must not call
Liu Ping Fancb365642013-09-25 14:20:58 +0800257 * cpu_get_ticks() after that.
258 * Caller must hold BQL which server as mutex for vm_clock_seqlock.
259 */
Paolo Bonzini946fb272011-09-12 13:57:37 +0200260void cpu_disable_ticks(void)
261{
Liu Ping Fancb365642013-09-25 14:20:58 +0800262 /* Here, the really thing protected by seqlock is cpu_clock_offset. */
263 seqlock_write_lock(&timers_state.vm_clock_seqlock);
Paolo Bonzini946fb272011-09-12 13:57:37 +0200264 if (timers_state.cpu_ticks_enabled) {
Paolo Bonzini5f3e3102013-10-28 17:32:18 +0100265 timers_state.cpu_ticks_offset += cpu_get_real_ticks();
Liu Ping Fancb365642013-09-25 14:20:58 +0800266 timers_state.cpu_clock_offset = cpu_get_clock_locked();
Paolo Bonzini946fb272011-09-12 13:57:37 +0200267 timers_state.cpu_ticks_enabled = 0;
268 }
Liu Ping Fancb365642013-09-25 14:20:58 +0800269 seqlock_write_unlock(&timers_state.vm_clock_seqlock);
Paolo Bonzini946fb272011-09-12 13:57:37 +0200270}
271
272/* Correlation between real and virtual time is always going to be
273 fairly approximate, so ignore small variation.
274 When the guest is idle real and virtual time will be aligned in
275 the IO wait loop. */
276#define ICOUNT_WOBBLE (get_ticks_per_sec() / 10)
277
278static void icount_adjust(void)
279{
280 int64_t cur_time;
281 int64_t cur_icount;
282 int64_t delta;
Paolo Bonzinia3270e12013-10-07 17:18:15 +0200283
284 /* Protected by TimersState mutex. */
Paolo Bonzini946fb272011-09-12 13:57:37 +0200285 static int64_t last_delta;
Paolo Bonzini468cc7c2013-10-07 17:21:51 +0200286
Paolo Bonzini946fb272011-09-12 13:57:37 +0200287 /* If the VM is not running, then do nothing. */
288 if (!runstate_is_running()) {
289 return;
290 }
Paolo Bonzini468cc7c2013-10-07 17:21:51 +0200291
Paolo Bonzini17a15f12013-10-03 15:17:25 +0200292 seqlock_write_lock(&timers_state.vm_clock_seqlock);
293 cur_time = cpu_get_clock_locked();
294 cur_icount = cpu_get_icount_locked();
Paolo Bonzini468cc7c2013-10-07 17:21:51 +0200295
Paolo Bonzini946fb272011-09-12 13:57:37 +0200296 delta = cur_icount - cur_time;
297 /* FIXME: This is a very crude algorithm, somewhat prone to oscillation. */
298 if (delta > 0
299 && last_delta + ICOUNT_WOBBLE < delta * 2
300 && icount_time_shift > 0) {
301 /* The guest is getting too far ahead. Slow time down. */
302 icount_time_shift--;
303 }
304 if (delta < 0
305 && last_delta - ICOUNT_WOBBLE > delta * 2
306 && icount_time_shift < MAX_ICOUNT_SHIFT) {
307 /* The guest is getting too far behind. Speed time up. */
308 icount_time_shift++;
309 }
310 last_delta = delta;
KONRAD Fredericc96778b2014-08-01 01:37:09 +0200311 timers_state.qemu_icount_bias = cur_icount
312 - (timers_state.qemu_icount << icount_time_shift);
Paolo Bonzini17a15f12013-10-03 15:17:25 +0200313 seqlock_write_unlock(&timers_state.vm_clock_seqlock);
Paolo Bonzini946fb272011-09-12 13:57:37 +0200314}
315
316static void icount_adjust_rt(void *opaque)
317{
Alex Bligh40daca52013-08-21 16:03:02 +0100318 timer_mod(icount_rt_timer,
319 qemu_clock_get_ms(QEMU_CLOCK_REALTIME) + 1000);
Paolo Bonzini946fb272011-09-12 13:57:37 +0200320 icount_adjust();
321}
322
323static void icount_adjust_vm(void *opaque)
324{
Alex Bligh40daca52013-08-21 16:03:02 +0100325 timer_mod(icount_vm_timer,
326 qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) +
327 get_ticks_per_sec() / 10);
Paolo Bonzini946fb272011-09-12 13:57:37 +0200328 icount_adjust();
329}
330
331static int64_t qemu_icount_round(int64_t count)
332{
333 return (count + (1 << icount_time_shift) - 1) >> icount_time_shift;
334}
335
336static void icount_warp_rt(void *opaque)
337{
Paolo Bonzini17a15f12013-10-03 15:17:25 +0200338 /* The icount_warp_timer is rescheduled soon after vm_clock_warp_start
339 * changes from -1 to another value, so the race here is okay.
340 */
341 if (atomic_read(&vm_clock_warp_start) == -1) {
Paolo Bonzini946fb272011-09-12 13:57:37 +0200342 return;
343 }
344
Paolo Bonzini17a15f12013-10-03 15:17:25 +0200345 seqlock_write_lock(&timers_state.vm_clock_seqlock);
Paolo Bonzini946fb272011-09-12 13:57:37 +0200346 if (runstate_is_running()) {
Alex Bligh40daca52013-08-21 16:03:02 +0100347 int64_t clock = qemu_clock_get_ns(QEMU_CLOCK_REALTIME);
Paolo Bonzini8ed961d2013-10-07 17:26:07 +0200348 int64_t warp_delta;
349
350 warp_delta = clock - vm_clock_warp_start;
351 if (use_icount == 2) {
Paolo Bonzini946fb272011-09-12 13:57:37 +0200352 /*
Alex Bligh40daca52013-08-21 16:03:02 +0100353 * In adaptive mode, do not let QEMU_CLOCK_VIRTUAL run too
Paolo Bonzini946fb272011-09-12 13:57:37 +0200354 * far ahead of real time.
355 */
Paolo Bonzini17a15f12013-10-03 15:17:25 +0200356 int64_t cur_time = cpu_get_clock_locked();
357 int64_t cur_icount = cpu_get_icount_locked();
Paolo Bonzini946fb272011-09-12 13:57:37 +0200358 int64_t delta = cur_time - cur_icount;
Paolo Bonzini8ed961d2013-10-07 17:26:07 +0200359 warp_delta = MIN(warp_delta, delta);
Paolo Bonzini946fb272011-09-12 13:57:37 +0200360 }
KONRAD Fredericc96778b2014-08-01 01:37:09 +0200361 timers_state.qemu_icount_bias += warp_delta;
Paolo Bonzini946fb272011-09-12 13:57:37 +0200362 }
363 vm_clock_warp_start = -1;
Paolo Bonzini17a15f12013-10-03 15:17:25 +0200364 seqlock_write_unlock(&timers_state.vm_clock_seqlock);
Paolo Bonzini8ed961d2013-10-07 17:26:07 +0200365
366 if (qemu_clock_expired(QEMU_CLOCK_VIRTUAL)) {
367 qemu_clock_notify(QEMU_CLOCK_VIRTUAL);
368 }
Paolo Bonzini946fb272011-09-12 13:57:37 +0200369}
370
Paolo Bonzini8156be52012-03-28 15:42:04 +0200371void qtest_clock_warp(int64_t dest)
372{
Alex Bligh40daca52013-08-21 16:03:02 +0100373 int64_t clock = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
Paolo Bonzini8156be52012-03-28 15:42:04 +0200374 assert(qtest_enabled());
375 while (clock < dest) {
Alex Bligh40daca52013-08-21 16:03:02 +0100376 int64_t deadline = qemu_clock_deadline_ns_all(QEMU_CLOCK_VIRTUAL);
Sergey Fedorovc9299e22014-06-10 13:10:28 +0400377 int64_t warp = qemu_soonest_timeout(dest - clock, deadline);
Paolo Bonzini17a15f12013-10-03 15:17:25 +0200378 seqlock_write_lock(&timers_state.vm_clock_seqlock);
KONRAD Fredericc96778b2014-08-01 01:37:09 +0200379 timers_state.qemu_icount_bias += warp;
Paolo Bonzini17a15f12013-10-03 15:17:25 +0200380 seqlock_write_unlock(&timers_state.vm_clock_seqlock);
381
Alex Bligh40daca52013-08-21 16:03:02 +0100382 qemu_clock_run_timers(QEMU_CLOCK_VIRTUAL);
383 clock = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
Paolo Bonzini8156be52012-03-28 15:42:04 +0200384 }
Alex Bligh40daca52013-08-21 16:03:02 +0100385 qemu_clock_notify(QEMU_CLOCK_VIRTUAL);
Paolo Bonzini8156be52012-03-28 15:42:04 +0200386}
387
Alex Bligh40daca52013-08-21 16:03:02 +0100388void qemu_clock_warp(QEMUClockType type)
Paolo Bonzini946fb272011-09-12 13:57:37 +0200389{
Paolo Bonzinice78d182013-10-07 17:30:02 +0200390 int64_t clock;
Paolo Bonzini946fb272011-09-12 13:57:37 +0200391 int64_t deadline;
392
393 /*
394 * There are too many global variables to make the "warp" behavior
395 * applicable to other clocks. But a clock argument removes the
396 * need for if statements all over the place.
397 */
Alex Bligh40daca52013-08-21 16:03:02 +0100398 if (type != QEMU_CLOCK_VIRTUAL || !use_icount) {
Paolo Bonzini946fb272011-09-12 13:57:37 +0200399 return;
400 }
401
402 /*
Alex Bligh40daca52013-08-21 16:03:02 +0100403 * If the CPUs have been sleeping, advance QEMU_CLOCK_VIRTUAL timer now.
404 * This ensures that the deadline for the timer is computed correctly below.
Paolo Bonzini946fb272011-09-12 13:57:37 +0200405 * This also makes sure that the insn counter is synchronized before the
406 * CPU starts running, in case the CPU is woken by an event other than
Alex Bligh40daca52013-08-21 16:03:02 +0100407 * the earliest QEMU_CLOCK_VIRTUAL timer.
Paolo Bonzini946fb272011-09-12 13:57:37 +0200408 */
409 icount_warp_rt(NULL);
Paolo Bonzinice78d182013-10-07 17:30:02 +0200410 timer_del(icount_warp_timer);
411 if (!all_cpu_threads_idle()) {
Paolo Bonzini946fb272011-09-12 13:57:37 +0200412 return;
413 }
414
Paolo Bonzini8156be52012-03-28 15:42:04 +0200415 if (qtest_enabled()) {
416 /* When testing, qtest commands advance icount. */
417 return;
418 }
419
Alex Blighac70aaf2013-08-21 16:02:57 +0100420 /* We want to use the earliest deadline from ALL vm_clocks */
Paolo Bonzinice78d182013-10-07 17:30:02 +0200421 clock = qemu_clock_get_ns(QEMU_CLOCK_REALTIME);
Alex Bligh40daca52013-08-21 16:03:02 +0100422 deadline = qemu_clock_deadline_ns_all(QEMU_CLOCK_VIRTUAL);
Paolo Bonzinice78d182013-10-07 17:30:02 +0200423 if (deadline < 0) {
424 return;
Alex Blighac70aaf2013-08-21 16:02:57 +0100425 }
426
Paolo Bonzini946fb272011-09-12 13:57:37 +0200427 if (deadline > 0) {
428 /*
Alex Bligh40daca52013-08-21 16:03:02 +0100429 * Ensure QEMU_CLOCK_VIRTUAL proceeds even when the virtual CPU goes to
Paolo Bonzini946fb272011-09-12 13:57:37 +0200430 * sleep. Otherwise, the CPU might be waiting for a future timer
431 * interrupt to wake it up, but the interrupt never comes because
432 * the vCPU isn't running any insns and thus doesn't advance the
Alex Bligh40daca52013-08-21 16:03:02 +0100433 * QEMU_CLOCK_VIRTUAL.
Paolo Bonzini946fb272011-09-12 13:57:37 +0200434 *
435 * An extreme solution for this problem would be to never let VCPUs
Alex Bligh40daca52013-08-21 16:03:02 +0100436 * sleep in icount mode if there is a pending QEMU_CLOCK_VIRTUAL
437 * timer; rather time could just advance to the next QEMU_CLOCK_VIRTUAL
438 * event. Instead, we do stop VCPUs and only advance QEMU_CLOCK_VIRTUAL
439 * after some e"real" time, (related to the time left until the next
440 * event) has passed. The QEMU_CLOCK_REALTIME timer will do this.
441 * This avoids that the warps are visible externally; for example,
442 * you will not be sending network packets continuously instead of
443 * every 100ms.
Paolo Bonzini946fb272011-09-12 13:57:37 +0200444 */
Paolo Bonzini17a15f12013-10-03 15:17:25 +0200445 seqlock_write_lock(&timers_state.vm_clock_seqlock);
Paolo Bonzinice78d182013-10-07 17:30:02 +0200446 if (vm_clock_warp_start == -1 || vm_clock_warp_start > clock) {
447 vm_clock_warp_start = clock;
448 }
Paolo Bonzini17a15f12013-10-03 15:17:25 +0200449 seqlock_write_unlock(&timers_state.vm_clock_seqlock);
Paolo Bonzinice78d182013-10-07 17:30:02 +0200450 timer_mod_anticipate(icount_warp_timer, clock + deadline);
Alex Blighac70aaf2013-08-21 16:02:57 +0100451 } else if (deadline == 0) {
Alex Bligh40daca52013-08-21 16:03:02 +0100452 qemu_clock_notify(QEMU_CLOCK_VIRTUAL);
Paolo Bonzini946fb272011-09-12 13:57:37 +0200453 }
454}
455
KONRAD Fredericd09eae32014-08-01 01:37:10 +0200456static bool icount_state_needed(void *opaque)
457{
458 return use_icount;
459}
460
461/*
462 * This is a subsection for icount migration.
463 */
464static const VMStateDescription icount_vmstate_timers = {
465 .name = "timer/icount",
466 .version_id = 1,
467 .minimum_version_id = 1,
468 .fields = (VMStateField[]) {
469 VMSTATE_INT64(qemu_icount_bias, TimersState),
470 VMSTATE_INT64(qemu_icount, TimersState),
471 VMSTATE_END_OF_LIST()
472 }
473};
474
Paolo Bonzini946fb272011-09-12 13:57:37 +0200475static const VMStateDescription vmstate_timers = {
476 .name = "timer",
477 .version_id = 2,
478 .minimum_version_id = 1,
Juan Quintela35d08452014-04-16 16:01:33 +0200479 .fields = (VMStateField[]) {
Paolo Bonzini946fb272011-09-12 13:57:37 +0200480 VMSTATE_INT64(cpu_ticks_offset, TimersState),
481 VMSTATE_INT64(dummy, TimersState),
482 VMSTATE_INT64_V(cpu_clock_offset, TimersState, 2),
483 VMSTATE_END_OF_LIST()
KONRAD Fredericd09eae32014-08-01 01:37:10 +0200484 },
485 .subsections = (VMStateSubsection[]) {
486 {
487 .vmsd = &icount_vmstate_timers,
488 .needed = icount_state_needed,
489 }, {
490 /* empty */
491 }
Paolo Bonzini946fb272011-09-12 13:57:37 +0200492 }
493};
494
Sebastian Tanase1ad95802014-07-25 11:56:28 +0200495void configure_icount(QemuOpts *opts, Error **errp)
Paolo Bonzini946fb272011-09-12 13:57:37 +0200496{
Sebastian Tanase1ad95802014-07-25 11:56:28 +0200497 const char *option;
Sebastian Tanasea8bfac32014-07-25 11:56:29 +0200498 char *rem_str = NULL;
Sebastian Tanase1ad95802014-07-25 11:56:28 +0200499
Liu Ping Fancb365642013-09-25 14:20:58 +0800500 seqlock_init(&timers_state.vm_clock_seqlock, NULL);
Paolo Bonzini946fb272011-09-12 13:57:37 +0200501 vmstate_register(NULL, 0, &vmstate_timers, &timers_state);
Sebastian Tanase1ad95802014-07-25 11:56:28 +0200502 option = qemu_opt_get(opts, "shift");
Paolo Bonzini946fb272011-09-12 13:57:37 +0200503 if (!option) {
Sebastian Tanasea8bfac32014-07-25 11:56:29 +0200504 if (qemu_opt_get(opts, "align") != NULL) {
505 error_setg(errp, "Please specify shift option when using align");
506 }
Paolo Bonzini946fb272011-09-12 13:57:37 +0200507 return;
508 }
Sebastian Tanasea8bfac32014-07-25 11:56:29 +0200509 icount_align_option = qemu_opt_get_bool(opts, "align", false);
Alex Bligh40daca52013-08-21 16:03:02 +0100510 icount_warp_timer = timer_new_ns(QEMU_CLOCK_REALTIME,
511 icount_warp_rt, NULL);
Paolo Bonzini946fb272011-09-12 13:57:37 +0200512 if (strcmp(option, "auto") != 0) {
Sebastian Tanasea8bfac32014-07-25 11:56:29 +0200513 errno = 0;
514 icount_time_shift = strtol(option, &rem_str, 0);
515 if (errno != 0 || *rem_str != '\0' || !strlen(option)) {
516 error_setg(errp, "icount: Invalid shift value");
517 }
Paolo Bonzini946fb272011-09-12 13:57:37 +0200518 use_icount = 1;
519 return;
Sebastian Tanasea8bfac32014-07-25 11:56:29 +0200520 } else if (icount_align_option) {
521 error_setg(errp, "shift=auto and align=on are incompatible");
Paolo Bonzini946fb272011-09-12 13:57:37 +0200522 }
523
524 use_icount = 2;
525
526 /* 125MIPS seems a reasonable initial guess at the guest speed.
527 It will be corrected fairly quickly anyway. */
528 icount_time_shift = 3;
529
530 /* Have both realtime and virtual time triggers for speed adjustment.
531 The realtime trigger catches emulated time passing too slowly,
532 the virtual time trigger catches emulated time passing too fast.
533 Realtime triggers occur even when idle, so use them less frequently
534 than VM triggers. */
Alex Bligh40daca52013-08-21 16:03:02 +0100535 icount_rt_timer = timer_new_ms(QEMU_CLOCK_REALTIME,
536 icount_adjust_rt, NULL);
537 timer_mod(icount_rt_timer,
538 qemu_clock_get_ms(QEMU_CLOCK_REALTIME) + 1000);
539 icount_vm_timer = timer_new_ns(QEMU_CLOCK_VIRTUAL,
540 icount_adjust_vm, NULL);
541 timer_mod(icount_vm_timer,
542 qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) +
543 get_ticks_per_sec() / 10);
Paolo Bonzini946fb272011-09-12 13:57:37 +0200544}
545
546/***********************************************************/
Blue Swirl296af7c2010-03-29 19:23:50 +0000547void hw_error(const char *fmt, ...)
548{
549 va_list ap;
Andreas Färber55e5c282012-12-17 06:18:02 +0100550 CPUState *cpu;
Blue Swirl296af7c2010-03-29 19:23:50 +0000551
552 va_start(ap, fmt);
553 fprintf(stderr, "qemu: hardware error: ");
554 vfprintf(stderr, fmt, ap);
555 fprintf(stderr, "\n");
Andreas Färberbdc44642013-06-24 23:50:24 +0200556 CPU_FOREACH(cpu) {
Andreas Färber55e5c282012-12-17 06:18:02 +0100557 fprintf(stderr, "CPU #%d:\n", cpu->cpu_index);
Andreas Färber878096e2013-05-27 01:33:50 +0200558 cpu_dump_state(cpu, stderr, fprintf, CPU_DUMP_FPU);
Blue Swirl296af7c2010-03-29 19:23:50 +0000559 }
560 va_end(ap);
561 abort();
562}
563
564void cpu_synchronize_all_states(void)
565{
Andreas Färber182735e2013-05-29 22:29:20 +0200566 CPUState *cpu;
Blue Swirl296af7c2010-03-29 19:23:50 +0000567
Andreas Färberbdc44642013-06-24 23:50:24 +0200568 CPU_FOREACH(cpu) {
Andreas Färber182735e2013-05-29 22:29:20 +0200569 cpu_synchronize_state(cpu);
Blue Swirl296af7c2010-03-29 19:23:50 +0000570 }
571}
572
573void cpu_synchronize_all_post_reset(void)
574{
Andreas Färber182735e2013-05-29 22:29:20 +0200575 CPUState *cpu;
Blue Swirl296af7c2010-03-29 19:23:50 +0000576
Andreas Färberbdc44642013-06-24 23:50:24 +0200577 CPU_FOREACH(cpu) {
Andreas Färber182735e2013-05-29 22:29:20 +0200578 cpu_synchronize_post_reset(cpu);
Blue Swirl296af7c2010-03-29 19:23:50 +0000579 }
580}
581
582void cpu_synchronize_all_post_init(void)
583{
Andreas Färber182735e2013-05-29 22:29:20 +0200584 CPUState *cpu;
Blue Swirl296af7c2010-03-29 19:23:50 +0000585
Andreas Färberbdc44642013-06-24 23:50:24 +0200586 CPU_FOREACH(cpu) {
Andreas Färber182735e2013-05-29 22:29:20 +0200587 cpu_synchronize_post_init(cpu);
Blue Swirl296af7c2010-03-29 19:23:50 +0000588 }
589}
590
Kevin Wolf56983462013-07-05 13:49:54 +0200591static int do_vm_stop(RunState state)
Blue Swirl296af7c2010-03-29 19:23:50 +0000592{
Kevin Wolf56983462013-07-05 13:49:54 +0200593 int ret = 0;
594
Luiz Capitulino13548692011-07-29 15:36:43 -0300595 if (runstate_is_running()) {
Blue Swirl296af7c2010-03-29 19:23:50 +0000596 cpu_disable_ticks();
Blue Swirl296af7c2010-03-29 19:23:50 +0000597 pause_all_vcpus();
Luiz Capitulinof5bbfba2011-07-29 15:04:45 -0300598 runstate_set(state);
Luiz Capitulino1dfb4dd2011-07-29 14:26:33 -0300599 vm_state_notify(0, state);
Wenchao Xiaa4e15de2014-06-18 08:43:36 +0200600 qapi_event_send_stop(&error_abort);
Blue Swirl296af7c2010-03-29 19:23:50 +0000601 }
Kevin Wolf56983462013-07-05 13:49:54 +0200602
Kevin Wolf594a45c2013-07-18 14:52:19 +0200603 bdrv_drain_all();
604 ret = bdrv_flush_all();
605
Kevin Wolf56983462013-07-05 13:49:54 +0200606 return ret;
Blue Swirl296af7c2010-03-29 19:23:50 +0000607}
608
Andreas Färbera1fcaa72012-05-02 23:42:26 +0200609static bool cpu_can_run(CPUState *cpu)
Blue Swirl296af7c2010-03-29 19:23:50 +0000610{
Andreas Färber4fdeee72012-05-02 23:10:09 +0200611 if (cpu->stop) {
Andreas Färbera1fcaa72012-05-02 23:42:26 +0200612 return false;
Jan Kiszka0ab07c62011-02-07 12:19:14 +0100613 }
Tiejun Chen321bc0b2013-08-02 09:43:09 +0800614 if (cpu_is_stopped(cpu)) {
Andreas Färbera1fcaa72012-05-02 23:42:26 +0200615 return false;
Jan Kiszka0ab07c62011-02-07 12:19:14 +0100616 }
Andreas Färbera1fcaa72012-05-02 23:42:26 +0200617 return true;
Blue Swirl296af7c2010-03-29 19:23:50 +0000618}
619
Andreas Färber91325042013-05-27 02:07:49 +0200620static void cpu_handle_guest_debug(CPUState *cpu)
Jan Kiszka3c638d02010-06-25 16:56:56 +0200621{
Andreas Färber64f6b342013-05-27 02:06:09 +0200622 gdb_set_stop_cpu(cpu);
Jan Kiszka8cf71712011-02-07 12:19:16 +0100623 qemu_system_debug_request();
Andreas Färberf324e762012-05-02 23:26:21 +0200624 cpu->stopped = true;
Jan Kiszka3c638d02010-06-25 16:56:56 +0200625}
626
Paolo Bonzini714bd042011-03-12 17:44:06 +0100627static void cpu_signal(int sig)
628{
Andreas Färber4917cf42013-05-27 05:17:50 +0200629 if (current_cpu) {
630 cpu_exit(current_cpu);
Paolo Bonzini714bd042011-03-12 17:44:06 +0100631 }
632 exit_request = 1;
633}
Paolo Bonzini714bd042011-03-12 17:44:06 +0100634
Jan Kiszka6d9cb732011-02-01 22:15:58 +0100635#ifdef CONFIG_LINUX
636static void sigbus_reraise(void)
637{
638 sigset_t set;
639 struct sigaction action;
640
641 memset(&action, 0, sizeof(action));
642 action.sa_handler = SIG_DFL;
643 if (!sigaction(SIGBUS, &action, NULL)) {
644 raise(SIGBUS);
645 sigemptyset(&set);
646 sigaddset(&set, SIGBUS);
647 sigprocmask(SIG_UNBLOCK, &set, NULL);
648 }
649 perror("Failed to re-raise SIGBUS!\n");
650 abort();
651}
652
653static void sigbus_handler(int n, struct qemu_signalfd_siginfo *siginfo,
654 void *ctx)
655{
656 if (kvm_on_sigbus(siginfo->ssi_code,
657 (void *)(intptr_t)siginfo->ssi_addr)) {
658 sigbus_reraise();
659 }
660}
661
662static void qemu_init_sigbus(void)
663{
664 struct sigaction action;
665
666 memset(&action, 0, sizeof(action));
667 action.sa_flags = SA_SIGINFO;
668 action.sa_sigaction = (void (*)(int, siginfo_t*, void*))sigbus_handler;
669 sigaction(SIGBUS, &action, NULL);
670
671 prctl(PR_MCE_KILL, PR_MCE_KILL_SET, PR_MCE_KILL_EARLY, 0, 0);
672}
673
Andreas Färber290adf32013-01-17 09:30:27 +0100674static void qemu_kvm_eat_signals(CPUState *cpu)
Jan Kiszka1ab3c6c2011-03-15 12:26:12 +0100675{
676 struct timespec ts = { 0, 0 };
677 siginfo_t siginfo;
678 sigset_t waitset;
679 sigset_t chkset;
680 int r;
681
682 sigemptyset(&waitset);
683 sigaddset(&waitset, SIG_IPI);
684 sigaddset(&waitset, SIGBUS);
685
686 do {
687 r = sigtimedwait(&waitset, &siginfo, &ts);
688 if (r == -1 && !(errno == EAGAIN || errno == EINTR)) {
689 perror("sigtimedwait");
690 exit(1);
691 }
692
693 switch (r) {
694 case SIGBUS:
Andreas Färber290adf32013-01-17 09:30:27 +0100695 if (kvm_on_sigbus_vcpu(cpu, siginfo.si_code, siginfo.si_addr)) {
Jan Kiszka1ab3c6c2011-03-15 12:26:12 +0100696 sigbus_reraise();
697 }
698 break;
699 default:
700 break;
701 }
702
703 r = sigpending(&chkset);
704 if (r == -1) {
705 perror("sigpending");
706 exit(1);
707 }
708 } while (sigismember(&chkset, SIG_IPI) || sigismember(&chkset, SIGBUS));
Jan Kiszka1ab3c6c2011-03-15 12:26:12 +0100709}
710
Jan Kiszka6d9cb732011-02-01 22:15:58 +0100711#else /* !CONFIG_LINUX */
712
713static void qemu_init_sigbus(void)
714{
715}
Jan Kiszka1ab3c6c2011-03-15 12:26:12 +0100716
Andreas Färber290adf32013-01-17 09:30:27 +0100717static void qemu_kvm_eat_signals(CPUState *cpu)
Jan Kiszka1ab3c6c2011-03-15 12:26:12 +0100718{
719}
Jan Kiszka6d9cb732011-02-01 22:15:58 +0100720#endif /* !CONFIG_LINUX */
721
Blue Swirl296af7c2010-03-29 19:23:50 +0000722#ifndef _WIN32
Jan Kiszka55f8d6a2011-02-01 22:15:52 +0100723static void dummy_signal(int sig)
Blue Swirl296af7c2010-03-29 19:23:50 +0000724{
725}
726
Andreas Färber13618e02013-05-26 23:41:00 +0200727static void qemu_kvm_init_cpu_signals(CPUState *cpu)
Paolo Bonzini714bd042011-03-12 17:44:06 +0100728{
729 int r;
730 sigset_t set;
731 struct sigaction sigact;
732
733 memset(&sigact, 0, sizeof(sigact));
734 sigact.sa_handler = dummy_signal;
735 sigaction(SIG_IPI, &sigact, NULL);
736
Paolo Bonzini714bd042011-03-12 17:44:06 +0100737 pthread_sigmask(SIG_BLOCK, NULL, &set);
738 sigdelset(&set, SIG_IPI);
739 sigdelset(&set, SIGBUS);
Andreas Färber491d6e82013-05-26 23:38:10 +0200740 r = kvm_set_signal_mask(cpu, &set);
Paolo Bonzini714bd042011-03-12 17:44:06 +0100741 if (r) {
742 fprintf(stderr, "kvm_set_signal_mask: %s\n", strerror(-r));
743 exit(1);
744 }
Paolo Bonzini714bd042011-03-12 17:44:06 +0100745}
746
747static void qemu_tcg_init_cpu_signals(void)
748{
Paolo Bonzini714bd042011-03-12 17:44:06 +0100749 sigset_t set;
750 struct sigaction sigact;
751
752 memset(&sigact, 0, sizeof(sigact));
753 sigact.sa_handler = cpu_signal;
754 sigaction(SIG_IPI, &sigact, NULL);
755
756 sigemptyset(&set);
757 sigaddset(&set, SIG_IPI);
758 pthread_sigmask(SIG_UNBLOCK, &set, NULL);
Paolo Bonzini714bd042011-03-12 17:44:06 +0100759}
760
Jan Kiszka55f8d6a2011-02-01 22:15:52 +0100761#else /* _WIN32 */
Andreas Färber13618e02013-05-26 23:41:00 +0200762static void qemu_kvm_init_cpu_signals(CPUState *cpu)
Paolo Bonzini714bd042011-03-12 17:44:06 +0100763{
764 abort();
765}
766
767static void qemu_tcg_init_cpu_signals(void)
768{
769}
Jan Kiszka55f8d6a2011-02-01 22:15:52 +0100770#endif /* _WIN32 */
Blue Swirl296af7c2010-03-29 19:23:50 +0000771
Stefan Weilb2532d82012-09-27 07:41:42 +0200772static QemuMutex qemu_global_mutex;
Paolo Bonzini46daff12011-06-09 13:10:24 +0200773static QemuCond qemu_io_proceeded_cond;
774static bool iothread_requesting_mutex;
Blue Swirl296af7c2010-03-29 19:23:50 +0000775
776static QemuThread io_thread;
777
778static QemuThread *tcg_cpu_thread;
779static QemuCond *tcg_halt_cond;
780
Blue Swirl296af7c2010-03-29 19:23:50 +0000781/* cpu creation */
782static QemuCond qemu_cpu_cond;
783/* system init */
Blue Swirl296af7c2010-03-29 19:23:50 +0000784static QemuCond qemu_pause_cond;
Marcelo Tosattie82bcec2010-05-04 09:45:22 -0300785static QemuCond qemu_work_cond;
Blue Swirl296af7c2010-03-29 19:23:50 +0000786
Paolo Bonzinid3b12f52011-09-13 10:30:52 +0200787void qemu_init_cpu_loop(void)
Blue Swirl296af7c2010-03-29 19:23:50 +0000788{
Jan Kiszka6d9cb732011-02-01 22:15:58 +0100789 qemu_init_sigbus();
Anthony Liguoried945922011-02-08 18:18:18 +0100790 qemu_cond_init(&qemu_cpu_cond);
Anthony Liguoried945922011-02-08 18:18:18 +0100791 qemu_cond_init(&qemu_pause_cond);
792 qemu_cond_init(&qemu_work_cond);
Paolo Bonzini46daff12011-06-09 13:10:24 +0200793 qemu_cond_init(&qemu_io_proceeded_cond);
Blue Swirl296af7c2010-03-29 19:23:50 +0000794 qemu_mutex_init(&qemu_global_mutex);
Blue Swirl296af7c2010-03-29 19:23:50 +0000795
Jan Kiszkab7680cb2011-03-12 17:43:51 +0100796 qemu_thread_get_self(&io_thread);
Blue Swirl296af7c2010-03-29 19:23:50 +0000797}
798
Andreas Färberf100f0b2012-05-03 14:58:47 +0200799void run_on_cpu(CPUState *cpu, void (*func)(void *data), void *data)
Marcelo Tosattie82bcec2010-05-04 09:45:22 -0300800{
801 struct qemu_work_item wi;
802
Andreas Färber60e82572012-05-02 22:23:49 +0200803 if (qemu_cpu_is_self(cpu)) {
Marcelo Tosattie82bcec2010-05-04 09:45:22 -0300804 func(data);
805 return;
806 }
807
808 wi.func = func;
809 wi.data = data;
Chegu Vinod3c022702013-06-24 03:49:41 -0600810 wi.free = false;
Andreas Färberc64ca812012-05-03 02:11:45 +0200811 if (cpu->queued_work_first == NULL) {
812 cpu->queued_work_first = &wi;
Jan Kiszka0ab07c62011-02-07 12:19:14 +0100813 } else {
Andreas Färberc64ca812012-05-03 02:11:45 +0200814 cpu->queued_work_last->next = &wi;
Jan Kiszka0ab07c62011-02-07 12:19:14 +0100815 }
Andreas Färberc64ca812012-05-03 02:11:45 +0200816 cpu->queued_work_last = &wi;
Marcelo Tosattie82bcec2010-05-04 09:45:22 -0300817 wi.next = NULL;
818 wi.done = false;
819
Andreas Färberc08d7422012-05-03 04:34:15 +0200820 qemu_cpu_kick(cpu);
Marcelo Tosattie82bcec2010-05-04 09:45:22 -0300821 while (!wi.done) {
Andreas Färber4917cf42013-05-27 05:17:50 +0200822 CPUState *self_cpu = current_cpu;
Marcelo Tosattie82bcec2010-05-04 09:45:22 -0300823
824 qemu_cond_wait(&qemu_work_cond, &qemu_global_mutex);
Andreas Färber4917cf42013-05-27 05:17:50 +0200825 current_cpu = self_cpu;
Marcelo Tosattie82bcec2010-05-04 09:45:22 -0300826 }
827}
828
Chegu Vinod3c022702013-06-24 03:49:41 -0600829void async_run_on_cpu(CPUState *cpu, void (*func)(void *data), void *data)
830{
831 struct qemu_work_item *wi;
832
833 if (qemu_cpu_is_self(cpu)) {
834 func(data);
835 return;
836 }
837
838 wi = g_malloc0(sizeof(struct qemu_work_item));
839 wi->func = func;
840 wi->data = data;
841 wi->free = true;
842 if (cpu->queued_work_first == NULL) {
843 cpu->queued_work_first = wi;
844 } else {
845 cpu->queued_work_last->next = wi;
846 }
847 cpu->queued_work_last = wi;
848 wi->next = NULL;
849 wi->done = false;
850
851 qemu_cpu_kick(cpu);
852}
853
Andreas Färber6d45b102012-05-03 02:13:22 +0200854static void flush_queued_work(CPUState *cpu)
Marcelo Tosattie82bcec2010-05-04 09:45:22 -0300855{
856 struct qemu_work_item *wi;
857
Andreas Färberc64ca812012-05-03 02:11:45 +0200858 if (cpu->queued_work_first == NULL) {
Marcelo Tosattie82bcec2010-05-04 09:45:22 -0300859 return;
Jan Kiszka0ab07c62011-02-07 12:19:14 +0100860 }
Marcelo Tosattie82bcec2010-05-04 09:45:22 -0300861
Andreas Färberc64ca812012-05-03 02:11:45 +0200862 while ((wi = cpu->queued_work_first)) {
863 cpu->queued_work_first = wi->next;
Marcelo Tosattie82bcec2010-05-04 09:45:22 -0300864 wi->func(wi->data);
865 wi->done = true;
Chegu Vinod3c022702013-06-24 03:49:41 -0600866 if (wi->free) {
867 g_free(wi);
868 }
Marcelo Tosattie82bcec2010-05-04 09:45:22 -0300869 }
Andreas Färberc64ca812012-05-03 02:11:45 +0200870 cpu->queued_work_last = NULL;
Marcelo Tosattie82bcec2010-05-04 09:45:22 -0300871 qemu_cond_broadcast(&qemu_work_cond);
872}
873
Andreas Färber509a0d72012-05-03 02:18:09 +0200874static void qemu_wait_io_event_common(CPUState *cpu)
Blue Swirl296af7c2010-03-29 19:23:50 +0000875{
Andreas Färber4fdeee72012-05-02 23:10:09 +0200876 if (cpu->stop) {
877 cpu->stop = false;
Andreas Färberf324e762012-05-02 23:26:21 +0200878 cpu->stopped = true;
Blue Swirl296af7c2010-03-29 19:23:50 +0000879 qemu_cond_signal(&qemu_pause_cond);
880 }
Andreas Färber6d45b102012-05-03 02:13:22 +0200881 flush_queued_work(cpu);
Andreas Färber216fc9a2012-05-02 17:49:49 +0200882 cpu->thread_kicked = false;
Blue Swirl296af7c2010-03-29 19:23:50 +0000883}
884
Jan Kiszka6cabe1f2010-06-25 16:56:53 +0200885static void qemu_tcg_wait_io_event(void)
Blue Swirl296af7c2010-03-29 19:23:50 +0000886{
Andreas Färber182735e2013-05-29 22:29:20 +0200887 CPUState *cpu;
Jan Kiszka6cabe1f2010-06-25 16:56:53 +0200888
Jan Kiszka16400322011-02-09 16:29:37 +0100889 while (all_cpu_threads_idle()) {
Paolo Bonziniab33fcd2011-04-13 10:03:44 +0200890 /* Start accounting real time to the virtual clock if the CPUs
891 are idle. */
Alex Bligh40daca52013-08-21 16:03:02 +0100892 qemu_clock_warp(QEMU_CLOCK_VIRTUAL);
Paolo Bonzini9705fbb2011-03-12 17:44:00 +0100893 qemu_cond_wait(tcg_halt_cond, &qemu_global_mutex);
Jan Kiszka16400322011-02-09 16:29:37 +0100894 }
Blue Swirl296af7c2010-03-29 19:23:50 +0000895
Paolo Bonzini46daff12011-06-09 13:10:24 +0200896 while (iothread_requesting_mutex) {
897 qemu_cond_wait(&qemu_io_proceeded_cond, &qemu_global_mutex);
898 }
Jan Kiszka6cabe1f2010-06-25 16:56:53 +0200899
Andreas Färberbdc44642013-06-24 23:50:24 +0200900 CPU_FOREACH(cpu) {
Andreas Färber182735e2013-05-29 22:29:20 +0200901 qemu_wait_io_event_common(cpu);
Jan Kiszka6cabe1f2010-06-25 16:56:53 +0200902 }
Blue Swirl296af7c2010-03-29 19:23:50 +0000903}
904
Andreas Färberfd529e82013-05-26 23:24:55 +0200905static void qemu_kvm_wait_io_event(CPUState *cpu)
Blue Swirl296af7c2010-03-29 19:23:50 +0000906{
Andreas Färbera98ae1d2013-05-26 23:21:08 +0200907 while (cpu_thread_is_idle(cpu)) {
Andreas Färberf5c121b2012-05-03 01:22:49 +0200908 qemu_cond_wait(cpu->halt_cond, &qemu_global_mutex);
Jan Kiszka16400322011-02-09 16:29:37 +0100909 }
Blue Swirl296af7c2010-03-29 19:23:50 +0000910
Andreas Färber290adf32013-01-17 09:30:27 +0100911 qemu_kvm_eat_signals(cpu);
Andreas Färber509a0d72012-05-03 02:18:09 +0200912 qemu_wait_io_event_common(cpu);
Blue Swirl296af7c2010-03-29 19:23:50 +0000913}
914
Jan Kiszka7e97cd82011-02-07 12:19:12 +0100915static void *qemu_kvm_cpu_thread_fn(void *arg)
Blue Swirl296af7c2010-03-29 19:23:50 +0000916{
Andreas Färber48a106b2013-05-27 02:20:39 +0200917 CPUState *cpu = arg;
Jan Kiszka84b49152011-02-01 22:15:50 +0100918 int r;
Blue Swirl296af7c2010-03-29 19:23:50 +0000919
Marcelo Tosatti6164e6d2010-03-23 13:37:13 -0300920 qemu_mutex_lock(&qemu_global_mutex);
Andreas Färber814e6122012-05-02 17:00:37 +0200921 qemu_thread_get_self(cpu->thread);
Andreas Färber9f09e182012-05-03 06:59:07 +0200922 cpu->thread_id = qemu_get_thread_id();
Andreas Färber4917cf42013-05-27 05:17:50 +0200923 current_cpu = cpu;
Blue Swirl296af7c2010-03-29 19:23:50 +0000924
Andreas Färber504134d2012-12-17 06:38:45 +0100925 r = kvm_init_vcpu(cpu);
Jan Kiszka84b49152011-02-01 22:15:50 +0100926 if (r < 0) {
927 fprintf(stderr, "kvm_init_vcpu failed: %s\n", strerror(-r));
928 exit(1);
929 }
Blue Swirl296af7c2010-03-29 19:23:50 +0000930
Andreas Färber13618e02013-05-26 23:41:00 +0200931 qemu_kvm_init_cpu_signals(cpu);
Blue Swirl296af7c2010-03-29 19:23:50 +0000932
933 /* signal CPU creation */
Andreas Färber61a46212012-05-02 22:49:36 +0200934 cpu->created = true;
Blue Swirl296af7c2010-03-29 19:23:50 +0000935 qemu_cond_signal(&qemu_cpu_cond);
936
Blue Swirl296af7c2010-03-29 19:23:50 +0000937 while (1) {
Andreas Färbera1fcaa72012-05-02 23:42:26 +0200938 if (cpu_can_run(cpu)) {
Andreas Färber1458c362013-05-26 23:46:55 +0200939 r = kvm_cpu_exec(cpu);
Jan Kiszka83f338f2011-02-07 12:19:17 +0100940 if (r == EXCP_DEBUG) {
Andreas Färber91325042013-05-27 02:07:49 +0200941 cpu_handle_guest_debug(cpu);
Jan Kiszka83f338f2011-02-07 12:19:17 +0100942 }
Jan Kiszka0ab07c62011-02-07 12:19:14 +0100943 }
Andreas Färberfd529e82013-05-26 23:24:55 +0200944 qemu_kvm_wait_io_event(cpu);
Blue Swirl296af7c2010-03-29 19:23:50 +0000945 }
946
947 return NULL;
948}
949
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +0200950static void *qemu_dummy_cpu_thread_fn(void *arg)
951{
952#ifdef _WIN32
953 fprintf(stderr, "qtest is not supported under Windows\n");
954 exit(1);
955#else
Andreas Färber10a90212013-05-27 02:24:35 +0200956 CPUState *cpu = arg;
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +0200957 sigset_t waitset;
958 int r;
959
960 qemu_mutex_lock_iothread();
Andreas Färber814e6122012-05-02 17:00:37 +0200961 qemu_thread_get_self(cpu->thread);
Andreas Färber9f09e182012-05-03 06:59:07 +0200962 cpu->thread_id = qemu_get_thread_id();
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +0200963
964 sigemptyset(&waitset);
965 sigaddset(&waitset, SIG_IPI);
966
967 /* signal CPU creation */
Andreas Färber61a46212012-05-02 22:49:36 +0200968 cpu->created = true;
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +0200969 qemu_cond_signal(&qemu_cpu_cond);
970
Andreas Färber4917cf42013-05-27 05:17:50 +0200971 current_cpu = cpu;
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +0200972 while (1) {
Andreas Färber4917cf42013-05-27 05:17:50 +0200973 current_cpu = NULL;
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +0200974 qemu_mutex_unlock_iothread();
975 do {
976 int sig;
977 r = sigwait(&waitset, &sig);
978 } while (r == -1 && (errno == EAGAIN || errno == EINTR));
979 if (r == -1) {
980 perror("sigwait");
981 exit(1);
982 }
983 qemu_mutex_lock_iothread();
Andreas Färber4917cf42013-05-27 05:17:50 +0200984 current_cpu = cpu;
Andreas Färber509a0d72012-05-03 02:18:09 +0200985 qemu_wait_io_event_common(cpu);
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +0200986 }
987
988 return NULL;
989#endif
990}
991
Jan Kiszkabdb7ca62011-09-26 09:40:39 +0200992static void tcg_exec_all(void);
993
Jan Kiszka7e97cd82011-02-07 12:19:12 +0100994static void *qemu_tcg_cpu_thread_fn(void *arg)
Blue Swirl296af7c2010-03-29 19:23:50 +0000995{
Andreas Färberc3586ba2012-05-03 01:41:24 +0200996 CPUState *cpu = arg;
Blue Swirl296af7c2010-03-29 19:23:50 +0000997
Jan Kiszka55f8d6a2011-02-01 22:15:52 +0100998 qemu_tcg_init_cpu_signals();
Andreas Färber814e6122012-05-02 17:00:37 +0200999 qemu_thread_get_self(cpu->thread);
Blue Swirl296af7c2010-03-29 19:23:50 +00001000
Blue Swirl296af7c2010-03-29 19:23:50 +00001001 qemu_mutex_lock(&qemu_global_mutex);
Andreas Färber38fcbd32013-07-07 19:50:23 +02001002 CPU_FOREACH(cpu) {
1003 cpu->thread_id = qemu_get_thread_id();
1004 cpu->created = true;
1005 }
Blue Swirl296af7c2010-03-29 19:23:50 +00001006 qemu_cond_signal(&qemu_cpu_cond);
1007
Jan Kiszkafa7d1862011-08-22 18:35:25 +02001008 /* wait for initial kick-off after machine start */
Andreas Färberbdc44642013-06-24 23:50:24 +02001009 while (QTAILQ_FIRST(&cpus)->stopped) {
Jan Kiszkafa7d1862011-08-22 18:35:25 +02001010 qemu_cond_wait(tcg_halt_cond, &qemu_global_mutex);
Jan Kiszka8e564b42012-02-17 18:31:15 +01001011
1012 /* process any pending work */
Andreas Färberbdc44642013-06-24 23:50:24 +02001013 CPU_FOREACH(cpu) {
Andreas Färber182735e2013-05-29 22:29:20 +02001014 qemu_wait_io_event_common(cpu);
Jan Kiszka8e564b42012-02-17 18:31:15 +01001015 }
Jan Kiszka0ab07c62011-02-07 12:19:14 +01001016 }
Blue Swirl296af7c2010-03-29 19:23:50 +00001017
1018 while (1) {
Jan Kiszkabdb7ca62011-09-26 09:40:39 +02001019 tcg_exec_all();
Alex Blighac70aaf2013-08-21 16:02:57 +01001020
1021 if (use_icount) {
Alex Bligh40daca52013-08-21 16:03:02 +01001022 int64_t deadline = qemu_clock_deadline_ns_all(QEMU_CLOCK_VIRTUAL);
Alex Blighac70aaf2013-08-21 16:02:57 +01001023
1024 if (deadline == 0) {
Alex Bligh40daca52013-08-21 16:03:02 +01001025 qemu_clock_notify(QEMU_CLOCK_VIRTUAL);
Alex Blighac70aaf2013-08-21 16:02:57 +01001026 }
Paolo Bonzini3b2319a2011-04-13 10:03:43 +02001027 }
Jan Kiszka6cabe1f2010-06-25 16:56:53 +02001028 qemu_tcg_wait_io_event();
Blue Swirl296af7c2010-03-29 19:23:50 +00001029 }
1030
1031 return NULL;
1032}
1033
Andreas Färber2ff09a42012-05-03 00:23:30 +02001034static void qemu_cpu_kick_thread(CPUState *cpu)
Paolo Bonzinicc015e92011-03-12 17:44:08 +01001035{
1036#ifndef _WIN32
1037 int err;
1038
Andreas Färber814e6122012-05-02 17:00:37 +02001039 err = pthread_kill(cpu->thread->thread, SIG_IPI);
Paolo Bonzinicc015e92011-03-12 17:44:08 +01001040 if (err) {
1041 fprintf(stderr, "qemu:%s: %s", __func__, strerror(err));
1042 exit(1);
1043 }
1044#else /* _WIN32 */
Andreas Färber60e82572012-05-02 22:23:49 +02001045 if (!qemu_cpu_is_self(cpu)) {
Olivier Hainqueed9164a2013-04-09 18:06:53 +02001046 CONTEXT tcgContext;
1047
1048 if (SuspendThread(cpu->hThread) == (DWORD)-1) {
Stefan Weil7f1721d2013-04-13 22:45:50 +02001049 fprintf(stderr, "qemu:%s: GetLastError:%lu\n", __func__,
Olivier Hainqueed9164a2013-04-09 18:06:53 +02001050 GetLastError());
1051 exit(1);
1052 }
1053
1054 /* On multi-core systems, we are not sure that the thread is actually
1055 * suspended until we can get the context.
1056 */
1057 tcgContext.ContextFlags = CONTEXT_CONTROL;
1058 while (GetThreadContext(cpu->hThread, &tcgContext) != 0) {
1059 continue;
1060 }
1061
Paolo Bonzinicc015e92011-03-12 17:44:08 +01001062 cpu_signal(0);
Olivier Hainqueed9164a2013-04-09 18:06:53 +02001063
1064 if (ResumeThread(cpu->hThread) == (DWORD)-1) {
Stefan Weil7f1721d2013-04-13 22:45:50 +02001065 fprintf(stderr, "qemu:%s: GetLastError:%lu\n", __func__,
Olivier Hainqueed9164a2013-04-09 18:06:53 +02001066 GetLastError());
1067 exit(1);
1068 }
Paolo Bonzinicc015e92011-03-12 17:44:08 +01001069 }
1070#endif
1071}
1072
Andreas Färberc08d7422012-05-03 04:34:15 +02001073void qemu_cpu_kick(CPUState *cpu)
Blue Swirl296af7c2010-03-29 19:23:50 +00001074{
Andreas Färberf5c121b2012-05-03 01:22:49 +02001075 qemu_cond_broadcast(cpu->halt_cond);
Andreas Färber216fc9a2012-05-02 17:49:49 +02001076 if (!tcg_enabled() && !cpu->thread_kicked) {
Andreas Färber2ff09a42012-05-03 00:23:30 +02001077 qemu_cpu_kick_thread(cpu);
Andreas Färber216fc9a2012-05-02 17:49:49 +02001078 cpu->thread_kicked = true;
Jan Kiszkaaa2c3642011-02-01 22:15:42 +01001079 }
Blue Swirl296af7c2010-03-29 19:23:50 +00001080}
1081
Jan Kiszka46d62fa2011-02-01 22:15:59 +01001082void qemu_cpu_kick_self(void)
1083{
Paolo Bonzinib55c22c2011-03-12 17:44:07 +01001084#ifndef _WIN32
Andreas Färber4917cf42013-05-27 05:17:50 +02001085 assert(current_cpu);
Jan Kiszka46d62fa2011-02-01 22:15:59 +01001086
Andreas Färber4917cf42013-05-27 05:17:50 +02001087 if (!current_cpu->thread_kicked) {
1088 qemu_cpu_kick_thread(current_cpu);
1089 current_cpu->thread_kicked = true;
Jan Kiszka46d62fa2011-02-01 22:15:59 +01001090 }
Paolo Bonzinib55c22c2011-03-12 17:44:07 +01001091#else
1092 abort();
1093#endif
Blue Swirl296af7c2010-03-29 19:23:50 +00001094}
1095
Andreas Färber60e82572012-05-02 22:23:49 +02001096bool qemu_cpu_is_self(CPUState *cpu)
Blue Swirl296af7c2010-03-29 19:23:50 +00001097{
Andreas Färber814e6122012-05-02 17:00:37 +02001098 return qemu_thread_is_self(cpu->thread);
Blue Swirl296af7c2010-03-29 19:23:50 +00001099}
1100
Juan Quintelaaa723c22012-09-18 16:30:11 +02001101static bool qemu_in_vcpu_thread(void)
1102{
Andreas Färber4917cf42013-05-27 05:17:50 +02001103 return current_cpu && qemu_cpu_is_self(current_cpu);
Juan Quintelaaa723c22012-09-18 16:30:11 +02001104}
1105
Blue Swirl296af7c2010-03-29 19:23:50 +00001106void qemu_mutex_lock_iothread(void)
1107{
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +02001108 if (!tcg_enabled()) {
Blue Swirl296af7c2010-03-29 19:23:50 +00001109 qemu_mutex_lock(&qemu_global_mutex);
Marcelo Tosatti1a28cac2010-05-04 09:45:20 -03001110 } else {
Paolo Bonzini46daff12011-06-09 13:10:24 +02001111 iothread_requesting_mutex = true;
Marcelo Tosatti1a28cac2010-05-04 09:45:20 -03001112 if (qemu_mutex_trylock(&qemu_global_mutex)) {
Andreas Färber182735e2013-05-29 22:29:20 +02001113 qemu_cpu_kick_thread(first_cpu);
Marcelo Tosatti1a28cac2010-05-04 09:45:20 -03001114 qemu_mutex_lock(&qemu_global_mutex);
1115 }
Paolo Bonzini46daff12011-06-09 13:10:24 +02001116 iothread_requesting_mutex = false;
1117 qemu_cond_broadcast(&qemu_io_proceeded_cond);
Marcelo Tosatti1a28cac2010-05-04 09:45:20 -03001118 }
Blue Swirl296af7c2010-03-29 19:23:50 +00001119}
1120
1121void qemu_mutex_unlock_iothread(void)
1122{
1123 qemu_mutex_unlock(&qemu_global_mutex);
1124}
1125
1126static int all_vcpus_paused(void)
1127{
Andreas Färberbdc44642013-06-24 23:50:24 +02001128 CPUState *cpu;
Blue Swirl296af7c2010-03-29 19:23:50 +00001129
Andreas Färberbdc44642013-06-24 23:50:24 +02001130 CPU_FOREACH(cpu) {
Andreas Färber182735e2013-05-29 22:29:20 +02001131 if (!cpu->stopped) {
Blue Swirl296af7c2010-03-29 19:23:50 +00001132 return 0;
Jan Kiszka0ab07c62011-02-07 12:19:14 +01001133 }
Blue Swirl296af7c2010-03-29 19:23:50 +00001134 }
1135
1136 return 1;
1137}
1138
1139void pause_all_vcpus(void)
1140{
Andreas Färberbdc44642013-06-24 23:50:24 +02001141 CPUState *cpu;
Blue Swirl296af7c2010-03-29 19:23:50 +00001142
Alex Bligh40daca52013-08-21 16:03:02 +01001143 qemu_clock_enable(QEMU_CLOCK_VIRTUAL, false);
Andreas Färberbdc44642013-06-24 23:50:24 +02001144 CPU_FOREACH(cpu) {
Andreas Färber182735e2013-05-29 22:29:20 +02001145 cpu->stop = true;
1146 qemu_cpu_kick(cpu);
Blue Swirl296af7c2010-03-29 19:23:50 +00001147 }
1148
Juan Quintelaaa723c22012-09-18 16:30:11 +02001149 if (qemu_in_vcpu_thread()) {
Jan Kiszkad798e972012-02-17 18:31:16 +01001150 cpu_stop_current();
1151 if (!kvm_enabled()) {
Andreas Färberbdc44642013-06-24 23:50:24 +02001152 CPU_FOREACH(cpu) {
Andreas Färber182735e2013-05-29 22:29:20 +02001153 cpu->stop = false;
1154 cpu->stopped = true;
Jan Kiszkad798e972012-02-17 18:31:16 +01001155 }
1156 return;
1157 }
1158 }
1159
Blue Swirl296af7c2010-03-29 19:23:50 +00001160 while (!all_vcpus_paused()) {
Paolo Bonzinibe7d6c52011-03-12 17:44:02 +01001161 qemu_cond_wait(&qemu_pause_cond, &qemu_global_mutex);
Andreas Färberbdc44642013-06-24 23:50:24 +02001162 CPU_FOREACH(cpu) {
Andreas Färber182735e2013-05-29 22:29:20 +02001163 qemu_cpu_kick(cpu);
Blue Swirl296af7c2010-03-29 19:23:50 +00001164 }
1165 }
1166}
1167
Igor Mammedov29936832013-04-23 10:29:37 +02001168void cpu_resume(CPUState *cpu)
1169{
1170 cpu->stop = false;
1171 cpu->stopped = false;
1172 qemu_cpu_kick(cpu);
1173}
1174
Blue Swirl296af7c2010-03-29 19:23:50 +00001175void resume_all_vcpus(void)
1176{
Andreas Färberbdc44642013-06-24 23:50:24 +02001177 CPUState *cpu;
Blue Swirl296af7c2010-03-29 19:23:50 +00001178
Alex Bligh40daca52013-08-21 16:03:02 +01001179 qemu_clock_enable(QEMU_CLOCK_VIRTUAL, true);
Andreas Färberbdc44642013-06-24 23:50:24 +02001180 CPU_FOREACH(cpu) {
Andreas Färber182735e2013-05-29 22:29:20 +02001181 cpu_resume(cpu);
Blue Swirl296af7c2010-03-29 19:23:50 +00001182 }
1183}
1184
Dr. David Alan Gilbert49001162014-01-30 10:20:32 +00001185/* For temporary buffers for forming a name */
1186#define VCPU_THREAD_NAME_SIZE 16
1187
Andreas Färbere5ab30a2012-05-03 01:50:44 +02001188static void qemu_tcg_init_vcpu(CPUState *cpu)
Blue Swirl296af7c2010-03-29 19:23:50 +00001189{
Dr. David Alan Gilbert49001162014-01-30 10:20:32 +00001190 char thread_name[VCPU_THREAD_NAME_SIZE];
1191
Edgar E. Iglesias09daed82013-12-17 13:06:51 +10001192 tcg_cpu_address_space_init(cpu, cpu->as);
1193
Blue Swirl296af7c2010-03-29 19:23:50 +00001194 /* share a single thread for all cpus with TCG */
1195 if (!tcg_cpu_thread) {
Andreas Färber814e6122012-05-02 17:00:37 +02001196 cpu->thread = g_malloc0(sizeof(QemuThread));
Andreas Färberf5c121b2012-05-03 01:22:49 +02001197 cpu->halt_cond = g_malloc0(sizeof(QemuCond));
1198 qemu_cond_init(cpu->halt_cond);
1199 tcg_halt_cond = cpu->halt_cond;
Dr. David Alan Gilbert49001162014-01-30 10:20:32 +00001200 snprintf(thread_name, VCPU_THREAD_NAME_SIZE, "CPU %d/TCG",
1201 cpu->cpu_index);
1202 qemu_thread_create(cpu->thread, thread_name, qemu_tcg_cpu_thread_fn,
1203 cpu, QEMU_THREAD_JOINABLE);
Paolo Bonzini1ecf47b2011-12-13 13:43:52 +01001204#ifdef _WIN32
Andreas Färber814e6122012-05-02 17:00:37 +02001205 cpu->hThread = qemu_thread_get_handle(cpu->thread);
Paolo Bonzini1ecf47b2011-12-13 13:43:52 +01001206#endif
Andreas Färber61a46212012-05-02 22:49:36 +02001207 while (!cpu->created) {
Paolo Bonzini18a85722011-03-12 17:44:03 +01001208 qemu_cond_wait(&qemu_cpu_cond, &qemu_global_mutex);
Jan Kiszka0ab07c62011-02-07 12:19:14 +01001209 }
Andreas Färber814e6122012-05-02 17:00:37 +02001210 tcg_cpu_thread = cpu->thread;
Blue Swirl296af7c2010-03-29 19:23:50 +00001211 } else {
Andreas Färber814e6122012-05-02 17:00:37 +02001212 cpu->thread = tcg_cpu_thread;
Andreas Färberf5c121b2012-05-03 01:22:49 +02001213 cpu->halt_cond = tcg_halt_cond;
Blue Swirl296af7c2010-03-29 19:23:50 +00001214 }
1215}
1216
Andreas Färber48a106b2013-05-27 02:20:39 +02001217static void qemu_kvm_start_vcpu(CPUState *cpu)
Blue Swirl296af7c2010-03-29 19:23:50 +00001218{
Dr. David Alan Gilbert49001162014-01-30 10:20:32 +00001219 char thread_name[VCPU_THREAD_NAME_SIZE];
1220
Andreas Färber814e6122012-05-02 17:00:37 +02001221 cpu->thread = g_malloc0(sizeof(QemuThread));
Andreas Färberf5c121b2012-05-03 01:22:49 +02001222 cpu->halt_cond = g_malloc0(sizeof(QemuCond));
1223 qemu_cond_init(cpu->halt_cond);
Dr. David Alan Gilbert49001162014-01-30 10:20:32 +00001224 snprintf(thread_name, VCPU_THREAD_NAME_SIZE, "CPU %d/KVM",
1225 cpu->cpu_index);
1226 qemu_thread_create(cpu->thread, thread_name, qemu_kvm_cpu_thread_fn,
1227 cpu, QEMU_THREAD_JOINABLE);
Andreas Färber61a46212012-05-02 22:49:36 +02001228 while (!cpu->created) {
Paolo Bonzini18a85722011-03-12 17:44:03 +01001229 qemu_cond_wait(&qemu_cpu_cond, &qemu_global_mutex);
Jan Kiszka0ab07c62011-02-07 12:19:14 +01001230 }
Blue Swirl296af7c2010-03-29 19:23:50 +00001231}
1232
Andreas Färber10a90212013-05-27 02:24:35 +02001233static void qemu_dummy_start_vcpu(CPUState *cpu)
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +02001234{
Dr. David Alan Gilbert49001162014-01-30 10:20:32 +00001235 char thread_name[VCPU_THREAD_NAME_SIZE];
1236
Andreas Färber814e6122012-05-02 17:00:37 +02001237 cpu->thread = g_malloc0(sizeof(QemuThread));
Andreas Färberf5c121b2012-05-03 01:22:49 +02001238 cpu->halt_cond = g_malloc0(sizeof(QemuCond));
1239 qemu_cond_init(cpu->halt_cond);
Dr. David Alan Gilbert49001162014-01-30 10:20:32 +00001240 snprintf(thread_name, VCPU_THREAD_NAME_SIZE, "CPU %d/DUMMY",
1241 cpu->cpu_index);
1242 qemu_thread_create(cpu->thread, thread_name, qemu_dummy_cpu_thread_fn, cpu,
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +02001243 QEMU_THREAD_JOINABLE);
Andreas Färber61a46212012-05-02 22:49:36 +02001244 while (!cpu->created) {
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +02001245 qemu_cond_wait(&qemu_cpu_cond, &qemu_global_mutex);
1246 }
1247}
1248
Andreas Färberc643bed2013-05-27 03:23:24 +02001249void qemu_init_vcpu(CPUState *cpu)
Blue Swirl296af7c2010-03-29 19:23:50 +00001250{
Andreas Färberce3960e2012-12-17 03:27:07 +01001251 cpu->nr_cores = smp_cores;
1252 cpu->nr_threads = smp_threads;
Andreas Färberf324e762012-05-02 23:26:21 +02001253 cpu->stopped = true;
Jan Kiszka0ab07c62011-02-07 12:19:14 +01001254 if (kvm_enabled()) {
Andreas Färber48a106b2013-05-27 02:20:39 +02001255 qemu_kvm_start_vcpu(cpu);
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +02001256 } else if (tcg_enabled()) {
Andreas Färbere5ab30a2012-05-03 01:50:44 +02001257 qemu_tcg_init_vcpu(cpu);
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +02001258 } else {
Andreas Färber10a90212013-05-27 02:24:35 +02001259 qemu_dummy_start_vcpu(cpu);
Jan Kiszka0ab07c62011-02-07 12:19:14 +01001260 }
Blue Swirl296af7c2010-03-29 19:23:50 +00001261}
1262
Jan Kiszkab4a3d962011-02-01 22:15:43 +01001263void cpu_stop_current(void)
Blue Swirl296af7c2010-03-29 19:23:50 +00001264{
Andreas Färber4917cf42013-05-27 05:17:50 +02001265 if (current_cpu) {
1266 current_cpu->stop = false;
1267 current_cpu->stopped = true;
1268 cpu_exit(current_cpu);
Paolo Bonzini67bb1722011-03-12 17:43:59 +01001269 qemu_cond_signal(&qemu_pause_cond);
Jan Kiszkab4a3d962011-02-01 22:15:43 +01001270 }
Blue Swirl296af7c2010-03-29 19:23:50 +00001271}
1272
Kevin Wolf56983462013-07-05 13:49:54 +02001273int vm_stop(RunState state)
Blue Swirl296af7c2010-03-29 19:23:50 +00001274{
Juan Quintelaaa723c22012-09-18 16:30:11 +02001275 if (qemu_in_vcpu_thread()) {
Paolo Bonzini74892d22014-06-05 14:53:58 +02001276 qemu_system_vmstop_request_prepare();
Luiz Capitulino1dfb4dd2011-07-29 14:26:33 -03001277 qemu_system_vmstop_request(state);
Blue Swirl296af7c2010-03-29 19:23:50 +00001278 /*
1279 * FIXME: should not return to device code in case
1280 * vm_stop() has been requested.
1281 */
Jan Kiszkab4a3d962011-02-01 22:15:43 +01001282 cpu_stop_current();
Kevin Wolf56983462013-07-05 13:49:54 +02001283 return 0;
Blue Swirl296af7c2010-03-29 19:23:50 +00001284 }
Kevin Wolf56983462013-07-05 13:49:54 +02001285
1286 return do_vm_stop(state);
Blue Swirl296af7c2010-03-29 19:23:50 +00001287}
1288
Luiz Capitulino8a9236f2011-10-14 11:18:09 -03001289/* does a state transition even if the VM is already stopped,
1290 current state is forgotten forever */
Kevin Wolf56983462013-07-05 13:49:54 +02001291int vm_stop_force_state(RunState state)
Luiz Capitulino8a9236f2011-10-14 11:18:09 -03001292{
1293 if (runstate_is_running()) {
Kevin Wolf56983462013-07-05 13:49:54 +02001294 return vm_stop(state);
Luiz Capitulino8a9236f2011-10-14 11:18:09 -03001295 } else {
1296 runstate_set(state);
Kevin Wolf594a45c2013-07-18 14:52:19 +02001297 /* Make sure to return an error if the flush in a previous vm_stop()
1298 * failed. */
1299 return bdrv_flush_all();
Luiz Capitulino8a9236f2011-10-14 11:18:09 -03001300 }
1301}
1302
Andreas Färber9349b4f2012-03-14 01:38:32 +01001303static int tcg_cpu_exec(CPUArchState *env)
Blue Swirl296af7c2010-03-29 19:23:50 +00001304{
Andreas Färberefee7342013-08-26 05:39:29 +02001305 CPUState *cpu = ENV_GET_CPU(env);
Blue Swirl296af7c2010-03-29 19:23:50 +00001306 int ret;
1307#ifdef CONFIG_PROFILER
1308 int64_t ti;
1309#endif
1310
1311#ifdef CONFIG_PROFILER
1312 ti = profile_getclock();
1313#endif
1314 if (use_icount) {
1315 int64_t count;
Alex Blighac70aaf2013-08-21 16:02:57 +01001316 int64_t deadline;
Blue Swirl296af7c2010-03-29 19:23:50 +00001317 int decr;
KONRAD Fredericc96778b2014-08-01 01:37:09 +02001318 timers_state.qemu_icount -= (cpu->icount_decr.u16.low
1319 + cpu->icount_extra);
Andreas Färber28ecfd72013-08-26 05:51:49 +02001320 cpu->icount_decr.u16.low = 0;
Andreas Färberefee7342013-08-26 05:39:29 +02001321 cpu->icount_extra = 0;
Alex Bligh40daca52013-08-21 16:03:02 +01001322 deadline = qemu_clock_deadline_ns_all(QEMU_CLOCK_VIRTUAL);
Alex Blighac70aaf2013-08-21 16:02:57 +01001323
1324 /* Maintain prior (possibly buggy) behaviour where if no deadline
Alex Bligh40daca52013-08-21 16:03:02 +01001325 * was set (as there is no QEMU_CLOCK_VIRTUAL timer) or it is more than
Alex Blighac70aaf2013-08-21 16:02:57 +01001326 * INT32_MAX nanoseconds ahead, we still use INT32_MAX
1327 * nanoseconds.
1328 */
1329 if ((deadline < 0) || (deadline > INT32_MAX)) {
1330 deadline = INT32_MAX;
1331 }
1332
1333 count = qemu_icount_round(deadline);
KONRAD Fredericc96778b2014-08-01 01:37:09 +02001334 timers_state.qemu_icount += count;
Blue Swirl296af7c2010-03-29 19:23:50 +00001335 decr = (count > 0xffff) ? 0xffff : count;
1336 count -= decr;
Andreas Färber28ecfd72013-08-26 05:51:49 +02001337 cpu->icount_decr.u16.low = decr;
Andreas Färberefee7342013-08-26 05:39:29 +02001338 cpu->icount_extra = count;
Blue Swirl296af7c2010-03-29 19:23:50 +00001339 }
1340 ret = cpu_exec(env);
1341#ifdef CONFIG_PROFILER
1342 qemu_time += profile_getclock() - ti;
1343#endif
1344 if (use_icount) {
1345 /* Fold pending instructions back into the
1346 instruction counter, and clear the interrupt flag. */
KONRAD Fredericc96778b2014-08-01 01:37:09 +02001347 timers_state.qemu_icount -= (cpu->icount_decr.u16.low
1348 + cpu->icount_extra);
Andreas Färber28ecfd72013-08-26 05:51:49 +02001349 cpu->icount_decr.u32 = 0;
Andreas Färberefee7342013-08-26 05:39:29 +02001350 cpu->icount_extra = 0;
Blue Swirl296af7c2010-03-29 19:23:50 +00001351 }
1352 return ret;
1353}
1354
Jan Kiszkabdb7ca62011-09-26 09:40:39 +02001355static void tcg_exec_all(void)
Blue Swirl296af7c2010-03-29 19:23:50 +00001356{
Jan Kiszka9a360852011-02-01 22:15:55 +01001357 int r;
1358
Alex Bligh40daca52013-08-21 16:03:02 +01001359 /* Account partial waits to QEMU_CLOCK_VIRTUAL. */
1360 qemu_clock_warp(QEMU_CLOCK_VIRTUAL);
Paolo Bonziniab33fcd2011-04-13 10:03:44 +02001361
Jan Kiszka0ab07c62011-02-07 12:19:14 +01001362 if (next_cpu == NULL) {
Blue Swirl296af7c2010-03-29 19:23:50 +00001363 next_cpu = first_cpu;
Jan Kiszka0ab07c62011-02-07 12:19:14 +01001364 }
Andreas Färberbdc44642013-06-24 23:50:24 +02001365 for (; next_cpu != NULL && !exit_request; next_cpu = CPU_NEXT(next_cpu)) {
Andreas Färber182735e2013-05-29 22:29:20 +02001366 CPUState *cpu = next_cpu;
1367 CPUArchState *env = cpu->env_ptr;
Blue Swirl296af7c2010-03-29 19:23:50 +00001368
Alex Bligh40daca52013-08-21 16:03:02 +01001369 qemu_clock_enable(QEMU_CLOCK_VIRTUAL,
Andreas Färbered2803d2013-06-21 20:20:45 +02001370 (cpu->singlestep_enabled & SSTEP_NOTIMER) == 0);
Blue Swirl296af7c2010-03-29 19:23:50 +00001371
Andreas Färbera1fcaa72012-05-02 23:42:26 +02001372 if (cpu_can_run(cpu)) {
Jan Kiszkabdb7ca62011-09-26 09:40:39 +02001373 r = tcg_cpu_exec(env);
Jan Kiszka9a360852011-02-01 22:15:55 +01001374 if (r == EXCP_DEBUG) {
Andreas Färber91325042013-05-27 02:07:49 +02001375 cpu_handle_guest_debug(cpu);
Jan Kiszka3c638d02010-06-25 16:56:56 +02001376 break;
1377 }
Andreas Färberf324e762012-05-02 23:26:21 +02001378 } else if (cpu->stop || cpu->stopped) {
Blue Swirl296af7c2010-03-29 19:23:50 +00001379 break;
1380 }
1381 }
Jan Kiszkac629a4b2010-06-25 16:56:52 +02001382 exit_request = 0;
Blue Swirl296af7c2010-03-29 19:23:50 +00001383}
1384
Stefan Weil9a78eea2010-10-22 23:03:33 +02001385void list_cpus(FILE *f, fprintf_function cpu_fprintf, const char *optarg)
Blue Swirl262353c2010-05-04 19:55:35 +00001386{
1387 /* XXX: implement xxx_cpu_list for targets that still miss it */
Peter Maydelle916cbf2012-09-05 17:41:08 -03001388#if defined(cpu_list)
1389 cpu_list(f, cpu_fprintf);
Blue Swirl262353c2010-05-04 19:55:35 +00001390#endif
1391}
Luiz Capitulinode0b36b2011-09-21 16:38:35 -03001392
1393CpuInfoList *qmp_query_cpus(Error **errp)
1394{
1395 CpuInfoList *head = NULL, *cur_item = NULL;
Andreas Färber182735e2013-05-29 22:29:20 +02001396 CPUState *cpu;
Luiz Capitulinode0b36b2011-09-21 16:38:35 -03001397
Andreas Färberbdc44642013-06-24 23:50:24 +02001398 CPU_FOREACH(cpu) {
Luiz Capitulinode0b36b2011-09-21 16:38:35 -03001399 CpuInfoList *info;
Andreas Färber182735e2013-05-29 22:29:20 +02001400#if defined(TARGET_I386)
1401 X86CPU *x86_cpu = X86_CPU(cpu);
1402 CPUX86State *env = &x86_cpu->env;
1403#elif defined(TARGET_PPC)
1404 PowerPCCPU *ppc_cpu = POWERPC_CPU(cpu);
1405 CPUPPCState *env = &ppc_cpu->env;
1406#elif defined(TARGET_SPARC)
1407 SPARCCPU *sparc_cpu = SPARC_CPU(cpu);
1408 CPUSPARCState *env = &sparc_cpu->env;
1409#elif defined(TARGET_MIPS)
1410 MIPSCPU *mips_cpu = MIPS_CPU(cpu);
1411 CPUMIPSState *env = &mips_cpu->env;
1412#endif
Luiz Capitulinode0b36b2011-09-21 16:38:35 -03001413
Andreas Färbercb446ec2013-05-01 14:24:52 +02001414 cpu_synchronize_state(cpu);
Luiz Capitulinode0b36b2011-09-21 16:38:35 -03001415
1416 info = g_malloc0(sizeof(*info));
1417 info->value = g_malloc0(sizeof(*info->value));
Andreas Färber55e5c282012-12-17 06:18:02 +01001418 info->value->CPU = cpu->cpu_index;
Andreas Färber182735e2013-05-29 22:29:20 +02001419 info->value->current = (cpu == first_cpu);
Andreas Färber259186a2013-01-17 18:51:17 +01001420 info->value->halted = cpu->halted;
Andreas Färber9f09e182012-05-03 06:59:07 +02001421 info->value->thread_id = cpu->thread_id;
Luiz Capitulinode0b36b2011-09-21 16:38:35 -03001422#if defined(TARGET_I386)
1423 info->value->has_pc = true;
1424 info->value->pc = env->eip + env->segs[R_CS].base;
1425#elif defined(TARGET_PPC)
1426 info->value->has_nip = true;
1427 info->value->nip = env->nip;
1428#elif defined(TARGET_SPARC)
1429 info->value->has_pc = true;
1430 info->value->pc = env->pc;
1431 info->value->has_npc = true;
1432 info->value->npc = env->npc;
1433#elif defined(TARGET_MIPS)
1434 info->value->has_PC = true;
1435 info->value->PC = env->active_tc.PC;
1436#endif
1437
1438 /* XXX: waiting for the qapi to support GSList */
1439 if (!cur_item) {
1440 head = cur_item = info;
1441 } else {
1442 cur_item->next = info;
1443 cur_item = info;
1444 }
1445 }
1446
1447 return head;
1448}
Luiz Capitulino0cfd6a92011-11-22 16:32:37 -02001449
1450void qmp_memsave(int64_t addr, int64_t size, const char *filename,
1451 bool has_cpu, int64_t cpu_index, Error **errp)
1452{
1453 FILE *f;
1454 uint32_t l;
Andreas Färber55e5c282012-12-17 06:18:02 +01001455 CPUState *cpu;
Luiz Capitulino0cfd6a92011-11-22 16:32:37 -02001456 uint8_t buf[1024];
1457
1458 if (!has_cpu) {
1459 cpu_index = 0;
1460 }
1461
Andreas Färber151d1322013-02-15 15:41:49 +01001462 cpu = qemu_get_cpu(cpu_index);
1463 if (cpu == NULL) {
Luiz Capitulino0cfd6a92011-11-22 16:32:37 -02001464 error_set(errp, QERR_INVALID_PARAMETER_VALUE, "cpu-index",
1465 "a CPU number");
1466 return;
1467 }
1468
1469 f = fopen(filename, "wb");
1470 if (!f) {
Luiz Capitulino618da852013-06-07 14:35:06 -04001471 error_setg_file_open(errp, errno, filename);
Luiz Capitulino0cfd6a92011-11-22 16:32:37 -02001472 return;
1473 }
1474
1475 while (size != 0) {
1476 l = sizeof(buf);
1477 if (l > size)
1478 l = size;
Aneesh Kumar K.V2f4d0f52013-10-01 21:49:30 +05301479 if (cpu_memory_rw_debug(cpu, addr, buf, l, 0) != 0) {
1480 error_setg(errp, "Invalid addr 0x%016" PRIx64 "specified", addr);
1481 goto exit;
1482 }
Luiz Capitulino0cfd6a92011-11-22 16:32:37 -02001483 if (fwrite(buf, 1, l, f) != l) {
1484 error_set(errp, QERR_IO_ERROR);
1485 goto exit;
1486 }
1487 addr += l;
1488 size -= l;
1489 }
1490
1491exit:
1492 fclose(f);
1493}
Luiz Capitulino6d3962b2011-11-22 17:26:46 -02001494
1495void qmp_pmemsave(int64_t addr, int64_t size, const char *filename,
1496 Error **errp)
1497{
1498 FILE *f;
1499 uint32_t l;
1500 uint8_t buf[1024];
1501
1502 f = fopen(filename, "wb");
1503 if (!f) {
Luiz Capitulino618da852013-06-07 14:35:06 -04001504 error_setg_file_open(errp, errno, filename);
Luiz Capitulino6d3962b2011-11-22 17:26:46 -02001505 return;
1506 }
1507
1508 while (size != 0) {
1509 l = sizeof(buf);
1510 if (l > size)
1511 l = size;
Stefan Weileb6282f2014-04-07 20:28:23 +02001512 cpu_physical_memory_read(addr, buf, l);
Luiz Capitulino6d3962b2011-11-22 17:26:46 -02001513 if (fwrite(buf, 1, l, f) != l) {
1514 error_set(errp, QERR_IO_ERROR);
1515 goto exit;
1516 }
1517 addr += l;
1518 size -= l;
1519 }
1520
1521exit:
1522 fclose(f);
1523}
Luiz Capitulinoab49ab52011-11-23 12:55:53 -02001524
1525void qmp_inject_nmi(Error **errp)
1526{
1527#if defined(TARGET_I386)
Andreas Färber182735e2013-05-29 22:29:20 +02001528 CPUState *cs;
Luiz Capitulinoab49ab52011-11-23 12:55:53 -02001529
Andreas Färberbdc44642013-06-24 23:50:24 +02001530 CPU_FOREACH(cs) {
Andreas Färber182735e2013-05-29 22:29:20 +02001531 X86CPU *cpu = X86_CPU(cs);
Andreas Färber182735e2013-05-29 22:29:20 +02001532
Chen Fan02e51482013-12-23 17:04:02 +08001533 if (!cpu->apic_state) {
Andreas Färber182735e2013-05-29 22:29:20 +02001534 cpu_interrupt(cs, CPU_INTERRUPT_NMI);
Jan Kiszka02c09192011-10-18 00:00:06 +08001535 } else {
Chen Fan02e51482013-12-23 17:04:02 +08001536 apic_deliver_nmi(cpu->apic_state);
Jan Kiszka02c09192011-10-18 00:00:06 +08001537 }
Luiz Capitulinoab49ab52011-11-23 12:55:53 -02001538 }
Eugene (jno) Dvurechenski7f7f9752012-12-05 15:50:07 +01001539#elif defined(TARGET_S390X)
1540 CPUState *cs;
1541 S390CPU *cpu;
1542
Andreas Färberbdc44642013-06-24 23:50:24 +02001543 CPU_FOREACH(cs) {
Eugene (jno) Dvurechenski7f7f9752012-12-05 15:50:07 +01001544 cpu = S390_CPU(cs);
1545 if (cpu->env.cpu_num == monitor_get_cpu_index()) {
1546 if (s390_cpu_restart(S390_CPU(cs)) == -1) {
1547 error_set(errp, QERR_UNSUPPORTED);
1548 return;
1549 }
1550 break;
1551 }
1552 }
Luiz Capitulinoab49ab52011-11-23 12:55:53 -02001553#else
1554 error_set(errp, QERR_UNSUPPORTED);
1555#endif
1556}
Sebastian Tanase27498be2014-07-25 11:56:33 +02001557
1558void dump_drift_info(FILE *f, fprintf_function cpu_fprintf)
1559{
1560 if (!use_icount) {
1561 return;
1562 }
1563
1564 cpu_fprintf(f, "Host - Guest clock %"PRIi64" ms\n",
1565 (cpu_get_clock() - cpu_get_icount())/SCALE_MS);
1566 if (icount_align_option) {
1567 cpu_fprintf(f, "Max guest delay %"PRIi64" ms\n", -max_delay/SCALE_MS);
1568 cpu_fprintf(f, "Max guest advance %"PRIi64" ms\n", max_advance/SCALE_MS);
1569 } else {
1570 cpu_fprintf(f, "Max guest delay NA\n");
1571 cpu_fprintf(f, "Max guest advance NA\n");
1572 }
1573}