blob: bbb8d4e8beadf97179570b039fbb27417a912e13 [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;
Blue Swirl296af7c2010-03-29 19:23:50 +000067
Tiejun Chen321bc0b2013-08-02 09:43:09 +080068bool cpu_is_stopped(CPUState *cpu)
69{
70 return cpu->stopped || !runstate_is_running();
71}
72
Andreas Färbera98ae1d2013-05-26 23:21:08 +020073static bool cpu_thread_is_idle(CPUState *cpu)
Peter Maydellac873f12012-07-19 16:52:27 +010074{
Andreas Färberc64ca812012-05-03 02:11:45 +020075 if (cpu->stop || cpu->queued_work_first) {
Peter Maydellac873f12012-07-19 16:52:27 +010076 return false;
77 }
Tiejun Chen321bc0b2013-08-02 09:43:09 +080078 if (cpu_is_stopped(cpu)) {
Peter Maydellac873f12012-07-19 16:52:27 +010079 return true;
80 }
Andreas Färber8c2e1b02013-08-25 18:53:55 +020081 if (!cpu->halted || cpu_has_work(cpu) ||
Alexander Graf215e79c2013-04-24 22:24:12 +020082 kvm_halt_in_kernel()) {
Peter Maydellac873f12012-07-19 16:52:27 +010083 return false;
84 }
85 return true;
86}
87
88static bool all_cpu_threads_idle(void)
89{
Andreas Färber182735e2013-05-29 22:29:20 +020090 CPUState *cpu;
Peter Maydellac873f12012-07-19 16:52:27 +010091
Andreas Färberbdc44642013-06-24 23:50:24 +020092 CPU_FOREACH(cpu) {
Andreas Färber182735e2013-05-29 22:29:20 +020093 if (!cpu_thread_is_idle(cpu)) {
Peter Maydellac873f12012-07-19 16:52:27 +010094 return false;
95 }
96 }
97 return true;
98}
99
Blue Swirl296af7c2010-03-29 19:23:50 +0000100/***********************************************************/
Paolo Bonzini946fb272011-09-12 13:57:37 +0200101/* guest cycle counter */
102
Paolo Bonzinia3270e12013-10-07 17:18:15 +0200103/* Protected by TimersState seqlock */
104
Sebastian Tanase71468392014-07-23 11:47:50 +0200105static int64_t vm_clock_warp_start = -1;
Paolo Bonzini946fb272011-09-12 13:57:37 +0200106/* Conversion factor from emulated instructions to virtual clock ticks. */
107static int icount_time_shift;
108/* Arbitrarily pick 1MIPS as the minimum allowable speed. */
109#define MAX_ICOUNT_SHIFT 10
Paolo Bonzinia3270e12013-10-07 17:18:15 +0200110
Paolo Bonzini946fb272011-09-12 13:57:37 +0200111static QEMUTimer *icount_rt_timer;
112static QEMUTimer *icount_vm_timer;
113static QEMUTimer *icount_warp_timer;
Paolo Bonzini946fb272011-09-12 13:57:37 +0200114
115typedef struct TimersState {
Liu Ping Fancb365642013-09-25 14:20:58 +0800116 /* Protected by BQL. */
Paolo Bonzini946fb272011-09-12 13:57:37 +0200117 int64_t cpu_ticks_prev;
118 int64_t cpu_ticks_offset;
Liu Ping Fancb365642013-09-25 14:20:58 +0800119
120 /* cpu_clock_offset can be read out of BQL, so protect it with
121 * this lock.
122 */
123 QemuSeqLock vm_clock_seqlock;
Paolo Bonzini946fb272011-09-12 13:57:37 +0200124 int64_t cpu_clock_offset;
125 int32_t cpu_ticks_enabled;
126 int64_t dummy;
KONRAD Fredericc96778b2014-08-01 01:37:09 +0200127
128 /* Compensate for varying guest execution speed. */
129 int64_t qemu_icount_bias;
130 /* Only written by TCG thread */
131 int64_t qemu_icount;
Paolo Bonzini946fb272011-09-12 13:57:37 +0200132} TimersState;
133
Liu Ping Fand9cd4002013-07-21 08:43:00 +0000134static TimersState timers_state;
Paolo Bonzini946fb272011-09-12 13:57:37 +0200135
136/* Return the virtual CPU time, based on the instruction counter. */
Paolo Bonzini17a15f12013-10-03 15:17:25 +0200137static int64_t cpu_get_icount_locked(void)
Paolo Bonzini946fb272011-09-12 13:57:37 +0200138{
139 int64_t icount;
Andreas Färber4917cf42013-05-27 05:17:50 +0200140 CPUState *cpu = current_cpu;
Paolo Bonzini946fb272011-09-12 13:57:37 +0200141
KONRAD Fredericc96778b2014-08-01 01:37:09 +0200142 icount = timers_state.qemu_icount;
Andreas Färber4917cf42013-05-27 05:17:50 +0200143 if (cpu) {
Andreas Färber99df7dc2013-08-26 05:15:23 +0200144 if (!cpu_can_do_io(cpu)) {
Paolo Bonzini946fb272011-09-12 13:57:37 +0200145 fprintf(stderr, "Bad clock read\n");
146 }
Andreas Färber28ecfd72013-08-26 05:51:49 +0200147 icount -= (cpu->icount_decr.u16.low + cpu->icount_extra);
Paolo Bonzini946fb272011-09-12 13:57:37 +0200148 }
KONRAD Frederic3f031312014-08-01 01:37:15 +0200149 return timers_state.qemu_icount_bias + cpu_icount_to_ns(icount);
Paolo Bonzini946fb272011-09-12 13:57:37 +0200150}
151
Paolo Bonzini17a15f12013-10-03 15:17:25 +0200152int64_t cpu_get_icount(void)
153{
154 int64_t icount;
155 unsigned start;
156
157 do {
158 start = seqlock_read_begin(&timers_state.vm_clock_seqlock);
159 icount = cpu_get_icount_locked();
160 } while (seqlock_read_retry(&timers_state.vm_clock_seqlock, start));
161
162 return icount;
163}
164
KONRAD Frederic3f031312014-08-01 01:37:15 +0200165int64_t cpu_icount_to_ns(int64_t icount)
166{
167 return icount << icount_time_shift;
168}
169
Paolo Bonzini946fb272011-09-12 13:57:37 +0200170/* return the host CPU cycle counter and handle stop/restart */
Liu Ping Fancb365642013-09-25 14:20:58 +0800171/* Caller must hold the BQL */
Paolo Bonzini946fb272011-09-12 13:57:37 +0200172int64_t cpu_get_ticks(void)
173{
Paolo Bonzini5f3e3102013-10-28 17:32:18 +0100174 int64_t ticks;
175
Paolo Bonzini946fb272011-09-12 13:57:37 +0200176 if (use_icount) {
177 return cpu_get_icount();
178 }
Paolo Bonzini5f3e3102013-10-28 17:32:18 +0100179
180 ticks = timers_state.cpu_ticks_offset;
181 if (timers_state.cpu_ticks_enabled) {
182 ticks += cpu_get_real_ticks();
Paolo Bonzini946fb272011-09-12 13:57:37 +0200183 }
Paolo Bonzini5f3e3102013-10-28 17:32:18 +0100184
185 if (timers_state.cpu_ticks_prev > ticks) {
186 /* Note: non increasing ticks may happen if the host uses
187 software suspend */
188 timers_state.cpu_ticks_offset += timers_state.cpu_ticks_prev - ticks;
189 ticks = timers_state.cpu_ticks_prev;
190 }
191
192 timers_state.cpu_ticks_prev = ticks;
193 return ticks;
Paolo Bonzini946fb272011-09-12 13:57:37 +0200194}
195
Liu Ping Fancb365642013-09-25 14:20:58 +0800196static int64_t cpu_get_clock_locked(void)
197{
Paolo Bonzini5f3e3102013-10-28 17:32:18 +0100198 int64_t ticks;
Liu Ping Fancb365642013-09-25 14:20:58 +0800199
Paolo Bonzini5f3e3102013-10-28 17:32:18 +0100200 ticks = timers_state.cpu_clock_offset;
201 if (timers_state.cpu_ticks_enabled) {
202 ticks += get_clock();
Liu Ping Fancb365642013-09-25 14:20:58 +0800203 }
204
Paolo Bonzini5f3e3102013-10-28 17:32:18 +0100205 return ticks;
Liu Ping Fancb365642013-09-25 14:20:58 +0800206}
207
Paolo Bonzini946fb272011-09-12 13:57:37 +0200208/* return the host CPU monotonic timer and handle stop/restart */
209int64_t cpu_get_clock(void)
210{
211 int64_t ti;
Liu Ping Fancb365642013-09-25 14:20:58 +0800212 unsigned start;
213
214 do {
215 start = seqlock_read_begin(&timers_state.vm_clock_seqlock);
216 ti = cpu_get_clock_locked();
217 } while (seqlock_read_retry(&timers_state.vm_clock_seqlock, start));
218
219 return ti;
Paolo Bonzini946fb272011-09-12 13:57:37 +0200220}
221
Liu Ping Fancb365642013-09-25 14:20:58 +0800222/* enable cpu_get_ticks()
223 * Caller must hold BQL which server as mutex for vm_clock_seqlock.
224 */
Paolo Bonzini946fb272011-09-12 13:57:37 +0200225void cpu_enable_ticks(void)
226{
Liu Ping Fancb365642013-09-25 14:20:58 +0800227 /* Here, the really thing protected by seqlock is cpu_clock_offset. */
228 seqlock_write_lock(&timers_state.vm_clock_seqlock);
Paolo Bonzini946fb272011-09-12 13:57:37 +0200229 if (!timers_state.cpu_ticks_enabled) {
230 timers_state.cpu_ticks_offset -= cpu_get_real_ticks();
231 timers_state.cpu_clock_offset -= get_clock();
232 timers_state.cpu_ticks_enabled = 1;
233 }
Liu Ping Fancb365642013-09-25 14:20:58 +0800234 seqlock_write_unlock(&timers_state.vm_clock_seqlock);
Paolo Bonzini946fb272011-09-12 13:57:37 +0200235}
236
237/* disable cpu_get_ticks() : the clock is stopped. You must not call
Liu Ping Fancb365642013-09-25 14:20:58 +0800238 * cpu_get_ticks() after that.
239 * Caller must hold BQL which server as mutex for vm_clock_seqlock.
240 */
Paolo Bonzini946fb272011-09-12 13:57:37 +0200241void cpu_disable_ticks(void)
242{
Liu Ping Fancb365642013-09-25 14:20:58 +0800243 /* Here, the really thing protected by seqlock is cpu_clock_offset. */
244 seqlock_write_lock(&timers_state.vm_clock_seqlock);
Paolo Bonzini946fb272011-09-12 13:57:37 +0200245 if (timers_state.cpu_ticks_enabled) {
Paolo Bonzini5f3e3102013-10-28 17:32:18 +0100246 timers_state.cpu_ticks_offset += cpu_get_real_ticks();
Liu Ping Fancb365642013-09-25 14:20:58 +0800247 timers_state.cpu_clock_offset = cpu_get_clock_locked();
Paolo Bonzini946fb272011-09-12 13:57:37 +0200248 timers_state.cpu_ticks_enabled = 0;
249 }
Liu Ping Fancb365642013-09-25 14:20:58 +0800250 seqlock_write_unlock(&timers_state.vm_clock_seqlock);
Paolo Bonzini946fb272011-09-12 13:57:37 +0200251}
252
253/* Correlation between real and virtual time is always going to be
254 fairly approximate, so ignore small variation.
255 When the guest is idle real and virtual time will be aligned in
256 the IO wait loop. */
257#define ICOUNT_WOBBLE (get_ticks_per_sec() / 10)
258
259static void icount_adjust(void)
260{
261 int64_t cur_time;
262 int64_t cur_icount;
263 int64_t delta;
Paolo Bonzinia3270e12013-10-07 17:18:15 +0200264
265 /* Protected by TimersState mutex. */
Paolo Bonzini946fb272011-09-12 13:57:37 +0200266 static int64_t last_delta;
Paolo Bonzini468cc7c2013-10-07 17:21:51 +0200267
Paolo Bonzini946fb272011-09-12 13:57:37 +0200268 /* If the VM is not running, then do nothing. */
269 if (!runstate_is_running()) {
270 return;
271 }
Paolo Bonzini468cc7c2013-10-07 17:21:51 +0200272
Paolo Bonzini17a15f12013-10-03 15:17:25 +0200273 seqlock_write_lock(&timers_state.vm_clock_seqlock);
274 cur_time = cpu_get_clock_locked();
275 cur_icount = cpu_get_icount_locked();
Paolo Bonzini468cc7c2013-10-07 17:21:51 +0200276
Paolo Bonzini946fb272011-09-12 13:57:37 +0200277 delta = cur_icount - cur_time;
278 /* FIXME: This is a very crude algorithm, somewhat prone to oscillation. */
279 if (delta > 0
280 && last_delta + ICOUNT_WOBBLE < delta * 2
281 && icount_time_shift > 0) {
282 /* The guest is getting too far ahead. Slow time down. */
283 icount_time_shift--;
284 }
285 if (delta < 0
286 && last_delta - ICOUNT_WOBBLE > delta * 2
287 && icount_time_shift < MAX_ICOUNT_SHIFT) {
288 /* The guest is getting too far behind. Speed time up. */
289 icount_time_shift++;
290 }
291 last_delta = delta;
KONRAD Fredericc96778b2014-08-01 01:37:09 +0200292 timers_state.qemu_icount_bias = cur_icount
293 - (timers_state.qemu_icount << icount_time_shift);
Paolo Bonzini17a15f12013-10-03 15:17:25 +0200294 seqlock_write_unlock(&timers_state.vm_clock_seqlock);
Paolo Bonzini946fb272011-09-12 13:57:37 +0200295}
296
297static void icount_adjust_rt(void *opaque)
298{
Alex Bligh40daca52013-08-21 16:03:02 +0100299 timer_mod(icount_rt_timer,
300 qemu_clock_get_ms(QEMU_CLOCK_REALTIME) + 1000);
Paolo Bonzini946fb272011-09-12 13:57:37 +0200301 icount_adjust();
302}
303
304static void icount_adjust_vm(void *opaque)
305{
Alex Bligh40daca52013-08-21 16:03:02 +0100306 timer_mod(icount_vm_timer,
307 qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) +
308 get_ticks_per_sec() / 10);
Paolo Bonzini946fb272011-09-12 13:57:37 +0200309 icount_adjust();
310}
311
312static int64_t qemu_icount_round(int64_t count)
313{
314 return (count + (1 << icount_time_shift) - 1) >> icount_time_shift;
315}
316
317static void icount_warp_rt(void *opaque)
318{
Paolo Bonzini17a15f12013-10-03 15:17:25 +0200319 /* The icount_warp_timer is rescheduled soon after vm_clock_warp_start
320 * changes from -1 to another value, so the race here is okay.
321 */
322 if (atomic_read(&vm_clock_warp_start) == -1) {
Paolo Bonzini946fb272011-09-12 13:57:37 +0200323 return;
324 }
325
Paolo Bonzini17a15f12013-10-03 15:17:25 +0200326 seqlock_write_lock(&timers_state.vm_clock_seqlock);
Paolo Bonzini946fb272011-09-12 13:57:37 +0200327 if (runstate_is_running()) {
Alex Bligh40daca52013-08-21 16:03:02 +0100328 int64_t clock = qemu_clock_get_ns(QEMU_CLOCK_REALTIME);
Paolo Bonzini8ed961d2013-10-07 17:26:07 +0200329 int64_t warp_delta;
330
331 warp_delta = clock - vm_clock_warp_start;
332 if (use_icount == 2) {
Paolo Bonzini946fb272011-09-12 13:57:37 +0200333 /*
Alex Bligh40daca52013-08-21 16:03:02 +0100334 * In adaptive mode, do not let QEMU_CLOCK_VIRTUAL run too
Paolo Bonzini946fb272011-09-12 13:57:37 +0200335 * far ahead of real time.
336 */
Paolo Bonzini17a15f12013-10-03 15:17:25 +0200337 int64_t cur_time = cpu_get_clock_locked();
338 int64_t cur_icount = cpu_get_icount_locked();
Paolo Bonzini946fb272011-09-12 13:57:37 +0200339 int64_t delta = cur_time - cur_icount;
Paolo Bonzini8ed961d2013-10-07 17:26:07 +0200340 warp_delta = MIN(warp_delta, delta);
Paolo Bonzini946fb272011-09-12 13:57:37 +0200341 }
KONRAD Fredericc96778b2014-08-01 01:37:09 +0200342 timers_state.qemu_icount_bias += warp_delta;
Paolo Bonzini946fb272011-09-12 13:57:37 +0200343 }
344 vm_clock_warp_start = -1;
Paolo Bonzini17a15f12013-10-03 15:17:25 +0200345 seqlock_write_unlock(&timers_state.vm_clock_seqlock);
Paolo Bonzini8ed961d2013-10-07 17:26:07 +0200346
347 if (qemu_clock_expired(QEMU_CLOCK_VIRTUAL)) {
348 qemu_clock_notify(QEMU_CLOCK_VIRTUAL);
349 }
Paolo Bonzini946fb272011-09-12 13:57:37 +0200350}
351
Paolo Bonzini8156be52012-03-28 15:42:04 +0200352void qtest_clock_warp(int64_t dest)
353{
Alex Bligh40daca52013-08-21 16:03:02 +0100354 int64_t clock = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
Paolo Bonzini8156be52012-03-28 15:42:04 +0200355 assert(qtest_enabled());
356 while (clock < dest) {
Alex Bligh40daca52013-08-21 16:03:02 +0100357 int64_t deadline = qemu_clock_deadline_ns_all(QEMU_CLOCK_VIRTUAL);
Sergey Fedorovc9299e22014-06-10 13:10:28 +0400358 int64_t warp = qemu_soonest_timeout(dest - clock, deadline);
Paolo Bonzini17a15f12013-10-03 15:17:25 +0200359 seqlock_write_lock(&timers_state.vm_clock_seqlock);
KONRAD Fredericc96778b2014-08-01 01:37:09 +0200360 timers_state.qemu_icount_bias += warp;
Paolo Bonzini17a15f12013-10-03 15:17:25 +0200361 seqlock_write_unlock(&timers_state.vm_clock_seqlock);
362
Alex Bligh40daca52013-08-21 16:03:02 +0100363 qemu_clock_run_timers(QEMU_CLOCK_VIRTUAL);
364 clock = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
Paolo Bonzini8156be52012-03-28 15:42:04 +0200365 }
Alex Bligh40daca52013-08-21 16:03:02 +0100366 qemu_clock_notify(QEMU_CLOCK_VIRTUAL);
Paolo Bonzini8156be52012-03-28 15:42:04 +0200367}
368
Alex Bligh40daca52013-08-21 16:03:02 +0100369void qemu_clock_warp(QEMUClockType type)
Paolo Bonzini946fb272011-09-12 13:57:37 +0200370{
Paolo Bonzinice78d182013-10-07 17:30:02 +0200371 int64_t clock;
Paolo Bonzini946fb272011-09-12 13:57:37 +0200372 int64_t deadline;
373
374 /*
375 * There are too many global variables to make the "warp" behavior
376 * applicable to other clocks. But a clock argument removes the
377 * need for if statements all over the place.
378 */
Alex Bligh40daca52013-08-21 16:03:02 +0100379 if (type != QEMU_CLOCK_VIRTUAL || !use_icount) {
Paolo Bonzini946fb272011-09-12 13:57:37 +0200380 return;
381 }
382
383 /*
Alex Bligh40daca52013-08-21 16:03:02 +0100384 * If the CPUs have been sleeping, advance QEMU_CLOCK_VIRTUAL timer now.
385 * This ensures that the deadline for the timer is computed correctly below.
Paolo Bonzini946fb272011-09-12 13:57:37 +0200386 * This also makes sure that the insn counter is synchronized before the
387 * CPU starts running, in case the CPU is woken by an event other than
Alex Bligh40daca52013-08-21 16:03:02 +0100388 * the earliest QEMU_CLOCK_VIRTUAL timer.
Paolo Bonzini946fb272011-09-12 13:57:37 +0200389 */
390 icount_warp_rt(NULL);
Paolo Bonzinice78d182013-10-07 17:30:02 +0200391 timer_del(icount_warp_timer);
392 if (!all_cpu_threads_idle()) {
Paolo Bonzini946fb272011-09-12 13:57:37 +0200393 return;
394 }
395
Paolo Bonzini8156be52012-03-28 15:42:04 +0200396 if (qtest_enabled()) {
397 /* When testing, qtest commands advance icount. */
398 return;
399 }
400
Alex Blighac70aaf2013-08-21 16:02:57 +0100401 /* We want to use the earliest deadline from ALL vm_clocks */
Paolo Bonzinice78d182013-10-07 17:30:02 +0200402 clock = qemu_clock_get_ns(QEMU_CLOCK_REALTIME);
Alex Bligh40daca52013-08-21 16:03:02 +0100403 deadline = qemu_clock_deadline_ns_all(QEMU_CLOCK_VIRTUAL);
Paolo Bonzinice78d182013-10-07 17:30:02 +0200404 if (deadline < 0) {
405 return;
Alex Blighac70aaf2013-08-21 16:02:57 +0100406 }
407
Paolo Bonzini946fb272011-09-12 13:57:37 +0200408 if (deadline > 0) {
409 /*
Alex Bligh40daca52013-08-21 16:03:02 +0100410 * Ensure QEMU_CLOCK_VIRTUAL proceeds even when the virtual CPU goes to
Paolo Bonzini946fb272011-09-12 13:57:37 +0200411 * sleep. Otherwise, the CPU might be waiting for a future timer
412 * interrupt to wake it up, but the interrupt never comes because
413 * the vCPU isn't running any insns and thus doesn't advance the
Alex Bligh40daca52013-08-21 16:03:02 +0100414 * QEMU_CLOCK_VIRTUAL.
Paolo Bonzini946fb272011-09-12 13:57:37 +0200415 *
416 * An extreme solution for this problem would be to never let VCPUs
Alex Bligh40daca52013-08-21 16:03:02 +0100417 * sleep in icount mode if there is a pending QEMU_CLOCK_VIRTUAL
418 * timer; rather time could just advance to the next QEMU_CLOCK_VIRTUAL
419 * event. Instead, we do stop VCPUs and only advance QEMU_CLOCK_VIRTUAL
420 * after some e"real" time, (related to the time left until the next
421 * event) has passed. The QEMU_CLOCK_REALTIME timer will do this.
422 * This avoids that the warps are visible externally; for example,
423 * you will not be sending network packets continuously instead of
424 * every 100ms.
Paolo Bonzini946fb272011-09-12 13:57:37 +0200425 */
Paolo Bonzini17a15f12013-10-03 15:17:25 +0200426 seqlock_write_lock(&timers_state.vm_clock_seqlock);
Paolo Bonzinice78d182013-10-07 17:30:02 +0200427 if (vm_clock_warp_start == -1 || vm_clock_warp_start > clock) {
428 vm_clock_warp_start = clock;
429 }
Paolo Bonzini17a15f12013-10-03 15:17:25 +0200430 seqlock_write_unlock(&timers_state.vm_clock_seqlock);
Paolo Bonzinice78d182013-10-07 17:30:02 +0200431 timer_mod_anticipate(icount_warp_timer, clock + deadline);
Alex Blighac70aaf2013-08-21 16:02:57 +0100432 } else if (deadline == 0) {
Alex Bligh40daca52013-08-21 16:03:02 +0100433 qemu_clock_notify(QEMU_CLOCK_VIRTUAL);
Paolo Bonzini946fb272011-09-12 13:57:37 +0200434 }
435}
436
KONRAD Fredericd09eae32014-08-01 01:37:10 +0200437static bool icount_state_needed(void *opaque)
438{
439 return use_icount;
440}
441
442/*
443 * This is a subsection for icount migration.
444 */
445static const VMStateDescription icount_vmstate_timers = {
446 .name = "timer/icount",
447 .version_id = 1,
448 .minimum_version_id = 1,
449 .fields = (VMStateField[]) {
450 VMSTATE_INT64(qemu_icount_bias, TimersState),
451 VMSTATE_INT64(qemu_icount, TimersState),
452 VMSTATE_END_OF_LIST()
453 }
454};
455
Paolo Bonzini946fb272011-09-12 13:57:37 +0200456static const VMStateDescription vmstate_timers = {
457 .name = "timer",
458 .version_id = 2,
459 .minimum_version_id = 1,
Juan Quintela35d08452014-04-16 16:01:33 +0200460 .fields = (VMStateField[]) {
Paolo Bonzini946fb272011-09-12 13:57:37 +0200461 VMSTATE_INT64(cpu_ticks_offset, TimersState),
462 VMSTATE_INT64(dummy, TimersState),
463 VMSTATE_INT64_V(cpu_clock_offset, TimersState, 2),
464 VMSTATE_END_OF_LIST()
KONRAD Fredericd09eae32014-08-01 01:37:10 +0200465 },
466 .subsections = (VMStateSubsection[]) {
467 {
468 .vmsd = &icount_vmstate_timers,
469 .needed = icount_state_needed,
470 }, {
471 /* empty */
472 }
Paolo Bonzini946fb272011-09-12 13:57:37 +0200473 }
474};
475
476void configure_icount(const char *option)
477{
Liu Ping Fancb365642013-09-25 14:20:58 +0800478 seqlock_init(&timers_state.vm_clock_seqlock, NULL);
Paolo Bonzini946fb272011-09-12 13:57:37 +0200479 vmstate_register(NULL, 0, &vmstate_timers, &timers_state);
480 if (!option) {
481 return;
482 }
483
Alex Bligh40daca52013-08-21 16:03:02 +0100484 icount_warp_timer = timer_new_ns(QEMU_CLOCK_REALTIME,
485 icount_warp_rt, NULL);
Paolo Bonzini946fb272011-09-12 13:57:37 +0200486 if (strcmp(option, "auto") != 0) {
487 icount_time_shift = strtol(option, NULL, 0);
488 use_icount = 1;
489 return;
490 }
491
492 use_icount = 2;
493
494 /* 125MIPS seems a reasonable initial guess at the guest speed.
495 It will be corrected fairly quickly anyway. */
496 icount_time_shift = 3;
497
498 /* Have both realtime and virtual time triggers for speed adjustment.
499 The realtime trigger catches emulated time passing too slowly,
500 the virtual time trigger catches emulated time passing too fast.
501 Realtime triggers occur even when idle, so use them less frequently
502 than VM triggers. */
Alex Bligh40daca52013-08-21 16:03:02 +0100503 icount_rt_timer = timer_new_ms(QEMU_CLOCK_REALTIME,
504 icount_adjust_rt, NULL);
505 timer_mod(icount_rt_timer,
506 qemu_clock_get_ms(QEMU_CLOCK_REALTIME) + 1000);
507 icount_vm_timer = timer_new_ns(QEMU_CLOCK_VIRTUAL,
508 icount_adjust_vm, NULL);
509 timer_mod(icount_vm_timer,
510 qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) +
511 get_ticks_per_sec() / 10);
Paolo Bonzini946fb272011-09-12 13:57:37 +0200512}
513
514/***********************************************************/
Blue Swirl296af7c2010-03-29 19:23:50 +0000515void hw_error(const char *fmt, ...)
516{
517 va_list ap;
Andreas Färber55e5c282012-12-17 06:18:02 +0100518 CPUState *cpu;
Blue Swirl296af7c2010-03-29 19:23:50 +0000519
520 va_start(ap, fmt);
521 fprintf(stderr, "qemu: hardware error: ");
522 vfprintf(stderr, fmt, ap);
523 fprintf(stderr, "\n");
Andreas Färberbdc44642013-06-24 23:50:24 +0200524 CPU_FOREACH(cpu) {
Andreas Färber55e5c282012-12-17 06:18:02 +0100525 fprintf(stderr, "CPU #%d:\n", cpu->cpu_index);
Andreas Färber878096e2013-05-27 01:33:50 +0200526 cpu_dump_state(cpu, stderr, fprintf, CPU_DUMP_FPU);
Blue Swirl296af7c2010-03-29 19:23:50 +0000527 }
528 va_end(ap);
529 abort();
530}
531
532void cpu_synchronize_all_states(void)
533{
Andreas Färber182735e2013-05-29 22:29:20 +0200534 CPUState *cpu;
Blue Swirl296af7c2010-03-29 19:23:50 +0000535
Andreas Färberbdc44642013-06-24 23:50:24 +0200536 CPU_FOREACH(cpu) {
Andreas Färber182735e2013-05-29 22:29:20 +0200537 cpu_synchronize_state(cpu);
Blue Swirl296af7c2010-03-29 19:23:50 +0000538 }
539}
540
541void cpu_synchronize_all_post_reset(void)
542{
Andreas Färber182735e2013-05-29 22:29:20 +0200543 CPUState *cpu;
Blue Swirl296af7c2010-03-29 19:23:50 +0000544
Andreas Färberbdc44642013-06-24 23:50:24 +0200545 CPU_FOREACH(cpu) {
Andreas Färber182735e2013-05-29 22:29:20 +0200546 cpu_synchronize_post_reset(cpu);
Blue Swirl296af7c2010-03-29 19:23:50 +0000547 }
548}
549
550void cpu_synchronize_all_post_init(void)
551{
Andreas Färber182735e2013-05-29 22:29:20 +0200552 CPUState *cpu;
Blue Swirl296af7c2010-03-29 19:23:50 +0000553
Andreas Färberbdc44642013-06-24 23:50:24 +0200554 CPU_FOREACH(cpu) {
Andreas Färber182735e2013-05-29 22:29:20 +0200555 cpu_synchronize_post_init(cpu);
Blue Swirl296af7c2010-03-29 19:23:50 +0000556 }
557}
558
Kevin Wolf56983462013-07-05 13:49:54 +0200559static int do_vm_stop(RunState state)
Blue Swirl296af7c2010-03-29 19:23:50 +0000560{
Kevin Wolf56983462013-07-05 13:49:54 +0200561 int ret = 0;
562
Luiz Capitulino13548692011-07-29 15:36:43 -0300563 if (runstate_is_running()) {
Blue Swirl296af7c2010-03-29 19:23:50 +0000564 cpu_disable_ticks();
Blue Swirl296af7c2010-03-29 19:23:50 +0000565 pause_all_vcpus();
Luiz Capitulinof5bbfba2011-07-29 15:04:45 -0300566 runstate_set(state);
Luiz Capitulino1dfb4dd2011-07-29 14:26:33 -0300567 vm_state_notify(0, state);
Wenchao Xiaa4e15de2014-06-18 08:43:36 +0200568 qapi_event_send_stop(&error_abort);
Blue Swirl296af7c2010-03-29 19:23:50 +0000569 }
Kevin Wolf56983462013-07-05 13:49:54 +0200570
Kevin Wolf594a45c2013-07-18 14:52:19 +0200571 bdrv_drain_all();
572 ret = bdrv_flush_all();
573
Kevin Wolf56983462013-07-05 13:49:54 +0200574 return ret;
Blue Swirl296af7c2010-03-29 19:23:50 +0000575}
576
Andreas Färbera1fcaa72012-05-02 23:42:26 +0200577static bool cpu_can_run(CPUState *cpu)
Blue Swirl296af7c2010-03-29 19:23:50 +0000578{
Andreas Färber4fdeee72012-05-02 23:10:09 +0200579 if (cpu->stop) {
Andreas Färbera1fcaa72012-05-02 23:42:26 +0200580 return false;
Jan Kiszka0ab07c62011-02-07 12:19:14 +0100581 }
Tiejun Chen321bc0b2013-08-02 09:43:09 +0800582 if (cpu_is_stopped(cpu)) {
Andreas Färbera1fcaa72012-05-02 23:42:26 +0200583 return false;
Jan Kiszka0ab07c62011-02-07 12:19:14 +0100584 }
Andreas Färbera1fcaa72012-05-02 23:42:26 +0200585 return true;
Blue Swirl296af7c2010-03-29 19:23:50 +0000586}
587
Andreas Färber91325042013-05-27 02:07:49 +0200588static void cpu_handle_guest_debug(CPUState *cpu)
Jan Kiszka3c638d02010-06-25 16:56:56 +0200589{
Andreas Färber64f6b342013-05-27 02:06:09 +0200590 gdb_set_stop_cpu(cpu);
Jan Kiszka8cf71712011-02-07 12:19:16 +0100591 qemu_system_debug_request();
Andreas Färberf324e762012-05-02 23:26:21 +0200592 cpu->stopped = true;
Jan Kiszka3c638d02010-06-25 16:56:56 +0200593}
594
Paolo Bonzini714bd042011-03-12 17:44:06 +0100595static void cpu_signal(int sig)
596{
Andreas Färber4917cf42013-05-27 05:17:50 +0200597 if (current_cpu) {
598 cpu_exit(current_cpu);
Paolo Bonzini714bd042011-03-12 17:44:06 +0100599 }
600 exit_request = 1;
601}
Paolo Bonzini714bd042011-03-12 17:44:06 +0100602
Jan Kiszka6d9cb732011-02-01 22:15:58 +0100603#ifdef CONFIG_LINUX
604static void sigbus_reraise(void)
605{
606 sigset_t set;
607 struct sigaction action;
608
609 memset(&action, 0, sizeof(action));
610 action.sa_handler = SIG_DFL;
611 if (!sigaction(SIGBUS, &action, NULL)) {
612 raise(SIGBUS);
613 sigemptyset(&set);
614 sigaddset(&set, SIGBUS);
615 sigprocmask(SIG_UNBLOCK, &set, NULL);
616 }
617 perror("Failed to re-raise SIGBUS!\n");
618 abort();
619}
620
621static void sigbus_handler(int n, struct qemu_signalfd_siginfo *siginfo,
622 void *ctx)
623{
624 if (kvm_on_sigbus(siginfo->ssi_code,
625 (void *)(intptr_t)siginfo->ssi_addr)) {
626 sigbus_reraise();
627 }
628}
629
630static void qemu_init_sigbus(void)
631{
632 struct sigaction action;
633
634 memset(&action, 0, sizeof(action));
635 action.sa_flags = SA_SIGINFO;
636 action.sa_sigaction = (void (*)(int, siginfo_t*, void*))sigbus_handler;
637 sigaction(SIGBUS, &action, NULL);
638
639 prctl(PR_MCE_KILL, PR_MCE_KILL_SET, PR_MCE_KILL_EARLY, 0, 0);
640}
641
Andreas Färber290adf32013-01-17 09:30:27 +0100642static void qemu_kvm_eat_signals(CPUState *cpu)
Jan Kiszka1ab3c6c2011-03-15 12:26:12 +0100643{
644 struct timespec ts = { 0, 0 };
645 siginfo_t siginfo;
646 sigset_t waitset;
647 sigset_t chkset;
648 int r;
649
650 sigemptyset(&waitset);
651 sigaddset(&waitset, SIG_IPI);
652 sigaddset(&waitset, SIGBUS);
653
654 do {
655 r = sigtimedwait(&waitset, &siginfo, &ts);
656 if (r == -1 && !(errno == EAGAIN || errno == EINTR)) {
657 perror("sigtimedwait");
658 exit(1);
659 }
660
661 switch (r) {
662 case SIGBUS:
Andreas Färber290adf32013-01-17 09:30:27 +0100663 if (kvm_on_sigbus_vcpu(cpu, siginfo.si_code, siginfo.si_addr)) {
Jan Kiszka1ab3c6c2011-03-15 12:26:12 +0100664 sigbus_reraise();
665 }
666 break;
667 default:
668 break;
669 }
670
671 r = sigpending(&chkset);
672 if (r == -1) {
673 perror("sigpending");
674 exit(1);
675 }
676 } while (sigismember(&chkset, SIG_IPI) || sigismember(&chkset, SIGBUS));
Jan Kiszka1ab3c6c2011-03-15 12:26:12 +0100677}
678
Jan Kiszka6d9cb732011-02-01 22:15:58 +0100679#else /* !CONFIG_LINUX */
680
681static void qemu_init_sigbus(void)
682{
683}
Jan Kiszka1ab3c6c2011-03-15 12:26:12 +0100684
Andreas Färber290adf32013-01-17 09:30:27 +0100685static void qemu_kvm_eat_signals(CPUState *cpu)
Jan Kiszka1ab3c6c2011-03-15 12:26:12 +0100686{
687}
Jan Kiszka6d9cb732011-02-01 22:15:58 +0100688#endif /* !CONFIG_LINUX */
689
Blue Swirl296af7c2010-03-29 19:23:50 +0000690#ifndef _WIN32
Jan Kiszka55f8d6a2011-02-01 22:15:52 +0100691static void dummy_signal(int sig)
Blue Swirl296af7c2010-03-29 19:23:50 +0000692{
693}
694
Andreas Färber13618e02013-05-26 23:41:00 +0200695static void qemu_kvm_init_cpu_signals(CPUState *cpu)
Paolo Bonzini714bd042011-03-12 17:44:06 +0100696{
697 int r;
698 sigset_t set;
699 struct sigaction sigact;
700
701 memset(&sigact, 0, sizeof(sigact));
702 sigact.sa_handler = dummy_signal;
703 sigaction(SIG_IPI, &sigact, NULL);
704
Paolo Bonzini714bd042011-03-12 17:44:06 +0100705 pthread_sigmask(SIG_BLOCK, NULL, &set);
706 sigdelset(&set, SIG_IPI);
707 sigdelset(&set, SIGBUS);
Andreas Färber491d6e82013-05-26 23:38:10 +0200708 r = kvm_set_signal_mask(cpu, &set);
Paolo Bonzini714bd042011-03-12 17:44:06 +0100709 if (r) {
710 fprintf(stderr, "kvm_set_signal_mask: %s\n", strerror(-r));
711 exit(1);
712 }
Paolo Bonzini714bd042011-03-12 17:44:06 +0100713}
714
715static void qemu_tcg_init_cpu_signals(void)
716{
Paolo Bonzini714bd042011-03-12 17:44:06 +0100717 sigset_t set;
718 struct sigaction sigact;
719
720 memset(&sigact, 0, sizeof(sigact));
721 sigact.sa_handler = cpu_signal;
722 sigaction(SIG_IPI, &sigact, NULL);
723
724 sigemptyset(&set);
725 sigaddset(&set, SIG_IPI);
726 pthread_sigmask(SIG_UNBLOCK, &set, NULL);
Paolo Bonzini714bd042011-03-12 17:44:06 +0100727}
728
Jan Kiszka55f8d6a2011-02-01 22:15:52 +0100729#else /* _WIN32 */
Andreas Färber13618e02013-05-26 23:41:00 +0200730static void qemu_kvm_init_cpu_signals(CPUState *cpu)
Paolo Bonzini714bd042011-03-12 17:44:06 +0100731{
732 abort();
733}
734
735static void qemu_tcg_init_cpu_signals(void)
736{
737}
Jan Kiszka55f8d6a2011-02-01 22:15:52 +0100738#endif /* _WIN32 */
Blue Swirl296af7c2010-03-29 19:23:50 +0000739
Stefan Weilb2532d82012-09-27 07:41:42 +0200740static QemuMutex qemu_global_mutex;
Paolo Bonzini46daff12011-06-09 13:10:24 +0200741static QemuCond qemu_io_proceeded_cond;
742static bool iothread_requesting_mutex;
Blue Swirl296af7c2010-03-29 19:23:50 +0000743
744static QemuThread io_thread;
745
746static QemuThread *tcg_cpu_thread;
747static QemuCond *tcg_halt_cond;
748
Blue Swirl296af7c2010-03-29 19:23:50 +0000749/* cpu creation */
750static QemuCond qemu_cpu_cond;
751/* system init */
Blue Swirl296af7c2010-03-29 19:23:50 +0000752static QemuCond qemu_pause_cond;
Marcelo Tosattie82bcec2010-05-04 09:45:22 -0300753static QemuCond qemu_work_cond;
Blue Swirl296af7c2010-03-29 19:23:50 +0000754
Paolo Bonzinid3b12f52011-09-13 10:30:52 +0200755void qemu_init_cpu_loop(void)
Blue Swirl296af7c2010-03-29 19:23:50 +0000756{
Jan Kiszka6d9cb732011-02-01 22:15:58 +0100757 qemu_init_sigbus();
Anthony Liguoried945922011-02-08 18:18:18 +0100758 qemu_cond_init(&qemu_cpu_cond);
Anthony Liguoried945922011-02-08 18:18:18 +0100759 qemu_cond_init(&qemu_pause_cond);
760 qemu_cond_init(&qemu_work_cond);
Paolo Bonzini46daff12011-06-09 13:10:24 +0200761 qemu_cond_init(&qemu_io_proceeded_cond);
Blue Swirl296af7c2010-03-29 19:23:50 +0000762 qemu_mutex_init(&qemu_global_mutex);
Blue Swirl296af7c2010-03-29 19:23:50 +0000763
Jan Kiszkab7680cb2011-03-12 17:43:51 +0100764 qemu_thread_get_self(&io_thread);
Blue Swirl296af7c2010-03-29 19:23:50 +0000765}
766
Andreas Färberf100f0b2012-05-03 14:58:47 +0200767void run_on_cpu(CPUState *cpu, void (*func)(void *data), void *data)
Marcelo Tosattie82bcec2010-05-04 09:45:22 -0300768{
769 struct qemu_work_item wi;
770
Andreas Färber60e82572012-05-02 22:23:49 +0200771 if (qemu_cpu_is_self(cpu)) {
Marcelo Tosattie82bcec2010-05-04 09:45:22 -0300772 func(data);
773 return;
774 }
775
776 wi.func = func;
777 wi.data = data;
Chegu Vinod3c022702013-06-24 03:49:41 -0600778 wi.free = false;
Andreas Färberc64ca812012-05-03 02:11:45 +0200779 if (cpu->queued_work_first == NULL) {
780 cpu->queued_work_first = &wi;
Jan Kiszka0ab07c62011-02-07 12:19:14 +0100781 } else {
Andreas Färberc64ca812012-05-03 02:11:45 +0200782 cpu->queued_work_last->next = &wi;
Jan Kiszka0ab07c62011-02-07 12:19:14 +0100783 }
Andreas Färberc64ca812012-05-03 02:11:45 +0200784 cpu->queued_work_last = &wi;
Marcelo Tosattie82bcec2010-05-04 09:45:22 -0300785 wi.next = NULL;
786 wi.done = false;
787
Andreas Färberc08d7422012-05-03 04:34:15 +0200788 qemu_cpu_kick(cpu);
Marcelo Tosattie82bcec2010-05-04 09:45:22 -0300789 while (!wi.done) {
Andreas Färber4917cf42013-05-27 05:17:50 +0200790 CPUState *self_cpu = current_cpu;
Marcelo Tosattie82bcec2010-05-04 09:45:22 -0300791
792 qemu_cond_wait(&qemu_work_cond, &qemu_global_mutex);
Andreas Färber4917cf42013-05-27 05:17:50 +0200793 current_cpu = self_cpu;
Marcelo Tosattie82bcec2010-05-04 09:45:22 -0300794 }
795}
796
Chegu Vinod3c022702013-06-24 03:49:41 -0600797void async_run_on_cpu(CPUState *cpu, void (*func)(void *data), void *data)
798{
799 struct qemu_work_item *wi;
800
801 if (qemu_cpu_is_self(cpu)) {
802 func(data);
803 return;
804 }
805
806 wi = g_malloc0(sizeof(struct qemu_work_item));
807 wi->func = func;
808 wi->data = data;
809 wi->free = true;
810 if (cpu->queued_work_first == NULL) {
811 cpu->queued_work_first = wi;
812 } else {
813 cpu->queued_work_last->next = wi;
814 }
815 cpu->queued_work_last = wi;
816 wi->next = NULL;
817 wi->done = false;
818
819 qemu_cpu_kick(cpu);
820}
821
Andreas Färber6d45b102012-05-03 02:13:22 +0200822static void flush_queued_work(CPUState *cpu)
Marcelo Tosattie82bcec2010-05-04 09:45:22 -0300823{
824 struct qemu_work_item *wi;
825
Andreas Färberc64ca812012-05-03 02:11:45 +0200826 if (cpu->queued_work_first == NULL) {
Marcelo Tosattie82bcec2010-05-04 09:45:22 -0300827 return;
Jan Kiszka0ab07c62011-02-07 12:19:14 +0100828 }
Marcelo Tosattie82bcec2010-05-04 09:45:22 -0300829
Andreas Färberc64ca812012-05-03 02:11:45 +0200830 while ((wi = cpu->queued_work_first)) {
831 cpu->queued_work_first = wi->next;
Marcelo Tosattie82bcec2010-05-04 09:45:22 -0300832 wi->func(wi->data);
833 wi->done = true;
Chegu Vinod3c022702013-06-24 03:49:41 -0600834 if (wi->free) {
835 g_free(wi);
836 }
Marcelo Tosattie82bcec2010-05-04 09:45:22 -0300837 }
Andreas Färberc64ca812012-05-03 02:11:45 +0200838 cpu->queued_work_last = NULL;
Marcelo Tosattie82bcec2010-05-04 09:45:22 -0300839 qemu_cond_broadcast(&qemu_work_cond);
840}
841
Andreas Färber509a0d72012-05-03 02:18:09 +0200842static void qemu_wait_io_event_common(CPUState *cpu)
Blue Swirl296af7c2010-03-29 19:23:50 +0000843{
Andreas Färber4fdeee72012-05-02 23:10:09 +0200844 if (cpu->stop) {
845 cpu->stop = false;
Andreas Färberf324e762012-05-02 23:26:21 +0200846 cpu->stopped = true;
Blue Swirl296af7c2010-03-29 19:23:50 +0000847 qemu_cond_signal(&qemu_pause_cond);
848 }
Andreas Färber6d45b102012-05-03 02:13:22 +0200849 flush_queued_work(cpu);
Andreas Färber216fc9a2012-05-02 17:49:49 +0200850 cpu->thread_kicked = false;
Blue Swirl296af7c2010-03-29 19:23:50 +0000851}
852
Jan Kiszka6cabe1f2010-06-25 16:56:53 +0200853static void qemu_tcg_wait_io_event(void)
Blue Swirl296af7c2010-03-29 19:23:50 +0000854{
Andreas Färber182735e2013-05-29 22:29:20 +0200855 CPUState *cpu;
Jan Kiszka6cabe1f2010-06-25 16:56:53 +0200856
Jan Kiszka16400322011-02-09 16:29:37 +0100857 while (all_cpu_threads_idle()) {
Paolo Bonziniab33fcd2011-04-13 10:03:44 +0200858 /* Start accounting real time to the virtual clock if the CPUs
859 are idle. */
Alex Bligh40daca52013-08-21 16:03:02 +0100860 qemu_clock_warp(QEMU_CLOCK_VIRTUAL);
Paolo Bonzini9705fbb2011-03-12 17:44:00 +0100861 qemu_cond_wait(tcg_halt_cond, &qemu_global_mutex);
Jan Kiszka16400322011-02-09 16:29:37 +0100862 }
Blue Swirl296af7c2010-03-29 19:23:50 +0000863
Paolo Bonzini46daff12011-06-09 13:10:24 +0200864 while (iothread_requesting_mutex) {
865 qemu_cond_wait(&qemu_io_proceeded_cond, &qemu_global_mutex);
866 }
Jan Kiszka6cabe1f2010-06-25 16:56:53 +0200867
Andreas Färberbdc44642013-06-24 23:50:24 +0200868 CPU_FOREACH(cpu) {
Andreas Färber182735e2013-05-29 22:29:20 +0200869 qemu_wait_io_event_common(cpu);
Jan Kiszka6cabe1f2010-06-25 16:56:53 +0200870 }
Blue Swirl296af7c2010-03-29 19:23:50 +0000871}
872
Andreas Färberfd529e82013-05-26 23:24:55 +0200873static void qemu_kvm_wait_io_event(CPUState *cpu)
Blue Swirl296af7c2010-03-29 19:23:50 +0000874{
Andreas Färbera98ae1d2013-05-26 23:21:08 +0200875 while (cpu_thread_is_idle(cpu)) {
Andreas Färberf5c121b2012-05-03 01:22:49 +0200876 qemu_cond_wait(cpu->halt_cond, &qemu_global_mutex);
Jan Kiszka16400322011-02-09 16:29:37 +0100877 }
Blue Swirl296af7c2010-03-29 19:23:50 +0000878
Andreas Färber290adf32013-01-17 09:30:27 +0100879 qemu_kvm_eat_signals(cpu);
Andreas Färber509a0d72012-05-03 02:18:09 +0200880 qemu_wait_io_event_common(cpu);
Blue Swirl296af7c2010-03-29 19:23:50 +0000881}
882
Jan Kiszka7e97cd82011-02-07 12:19:12 +0100883static void *qemu_kvm_cpu_thread_fn(void *arg)
Blue Swirl296af7c2010-03-29 19:23:50 +0000884{
Andreas Färber48a106b2013-05-27 02:20:39 +0200885 CPUState *cpu = arg;
Jan Kiszka84b49152011-02-01 22:15:50 +0100886 int r;
Blue Swirl296af7c2010-03-29 19:23:50 +0000887
Marcelo Tosatti6164e6d2010-03-23 13:37:13 -0300888 qemu_mutex_lock(&qemu_global_mutex);
Andreas Färber814e6122012-05-02 17:00:37 +0200889 qemu_thread_get_self(cpu->thread);
Andreas Färber9f09e182012-05-03 06:59:07 +0200890 cpu->thread_id = qemu_get_thread_id();
Andreas Färber4917cf42013-05-27 05:17:50 +0200891 current_cpu = cpu;
Blue Swirl296af7c2010-03-29 19:23:50 +0000892
Andreas Färber504134d2012-12-17 06:38:45 +0100893 r = kvm_init_vcpu(cpu);
Jan Kiszka84b49152011-02-01 22:15:50 +0100894 if (r < 0) {
895 fprintf(stderr, "kvm_init_vcpu failed: %s\n", strerror(-r));
896 exit(1);
897 }
Blue Swirl296af7c2010-03-29 19:23:50 +0000898
Andreas Färber13618e02013-05-26 23:41:00 +0200899 qemu_kvm_init_cpu_signals(cpu);
Blue Swirl296af7c2010-03-29 19:23:50 +0000900
901 /* signal CPU creation */
Andreas Färber61a46212012-05-02 22:49:36 +0200902 cpu->created = true;
Blue Swirl296af7c2010-03-29 19:23:50 +0000903 qemu_cond_signal(&qemu_cpu_cond);
904
Blue Swirl296af7c2010-03-29 19:23:50 +0000905 while (1) {
Andreas Färbera1fcaa72012-05-02 23:42:26 +0200906 if (cpu_can_run(cpu)) {
Andreas Färber1458c362013-05-26 23:46:55 +0200907 r = kvm_cpu_exec(cpu);
Jan Kiszka83f338f2011-02-07 12:19:17 +0100908 if (r == EXCP_DEBUG) {
Andreas Färber91325042013-05-27 02:07:49 +0200909 cpu_handle_guest_debug(cpu);
Jan Kiszka83f338f2011-02-07 12:19:17 +0100910 }
Jan Kiszka0ab07c62011-02-07 12:19:14 +0100911 }
Andreas Färberfd529e82013-05-26 23:24:55 +0200912 qemu_kvm_wait_io_event(cpu);
Blue Swirl296af7c2010-03-29 19:23:50 +0000913 }
914
915 return NULL;
916}
917
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +0200918static void *qemu_dummy_cpu_thread_fn(void *arg)
919{
920#ifdef _WIN32
921 fprintf(stderr, "qtest is not supported under Windows\n");
922 exit(1);
923#else
Andreas Färber10a90212013-05-27 02:24:35 +0200924 CPUState *cpu = arg;
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +0200925 sigset_t waitset;
926 int r;
927
928 qemu_mutex_lock_iothread();
Andreas Färber814e6122012-05-02 17:00:37 +0200929 qemu_thread_get_self(cpu->thread);
Andreas Färber9f09e182012-05-03 06:59:07 +0200930 cpu->thread_id = qemu_get_thread_id();
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +0200931
932 sigemptyset(&waitset);
933 sigaddset(&waitset, SIG_IPI);
934
935 /* signal CPU creation */
Andreas Färber61a46212012-05-02 22:49:36 +0200936 cpu->created = true;
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +0200937 qemu_cond_signal(&qemu_cpu_cond);
938
Andreas Färber4917cf42013-05-27 05:17:50 +0200939 current_cpu = cpu;
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +0200940 while (1) {
Andreas Färber4917cf42013-05-27 05:17:50 +0200941 current_cpu = NULL;
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +0200942 qemu_mutex_unlock_iothread();
943 do {
944 int sig;
945 r = sigwait(&waitset, &sig);
946 } while (r == -1 && (errno == EAGAIN || errno == EINTR));
947 if (r == -1) {
948 perror("sigwait");
949 exit(1);
950 }
951 qemu_mutex_lock_iothread();
Andreas Färber4917cf42013-05-27 05:17:50 +0200952 current_cpu = cpu;
Andreas Färber509a0d72012-05-03 02:18:09 +0200953 qemu_wait_io_event_common(cpu);
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +0200954 }
955
956 return NULL;
957#endif
958}
959
Jan Kiszkabdb7ca62011-09-26 09:40:39 +0200960static void tcg_exec_all(void);
961
Jan Kiszka7e97cd82011-02-07 12:19:12 +0100962static void *qemu_tcg_cpu_thread_fn(void *arg)
Blue Swirl296af7c2010-03-29 19:23:50 +0000963{
Andreas Färberc3586ba2012-05-03 01:41:24 +0200964 CPUState *cpu = arg;
Blue Swirl296af7c2010-03-29 19:23:50 +0000965
Jan Kiszka55f8d6a2011-02-01 22:15:52 +0100966 qemu_tcg_init_cpu_signals();
Andreas Färber814e6122012-05-02 17:00:37 +0200967 qemu_thread_get_self(cpu->thread);
Blue Swirl296af7c2010-03-29 19:23:50 +0000968
Blue Swirl296af7c2010-03-29 19:23:50 +0000969 qemu_mutex_lock(&qemu_global_mutex);
Andreas Färber38fcbd32013-07-07 19:50:23 +0200970 CPU_FOREACH(cpu) {
971 cpu->thread_id = qemu_get_thread_id();
972 cpu->created = true;
973 }
Blue Swirl296af7c2010-03-29 19:23:50 +0000974 qemu_cond_signal(&qemu_cpu_cond);
975
Jan Kiszkafa7d1862011-08-22 18:35:25 +0200976 /* wait for initial kick-off after machine start */
Andreas Färberbdc44642013-06-24 23:50:24 +0200977 while (QTAILQ_FIRST(&cpus)->stopped) {
Jan Kiszkafa7d1862011-08-22 18:35:25 +0200978 qemu_cond_wait(tcg_halt_cond, &qemu_global_mutex);
Jan Kiszka8e564b42012-02-17 18:31:15 +0100979
980 /* process any pending work */
Andreas Färberbdc44642013-06-24 23:50:24 +0200981 CPU_FOREACH(cpu) {
Andreas Färber182735e2013-05-29 22:29:20 +0200982 qemu_wait_io_event_common(cpu);
Jan Kiszka8e564b42012-02-17 18:31:15 +0100983 }
Jan Kiszka0ab07c62011-02-07 12:19:14 +0100984 }
Blue Swirl296af7c2010-03-29 19:23:50 +0000985
986 while (1) {
Jan Kiszkabdb7ca62011-09-26 09:40:39 +0200987 tcg_exec_all();
Alex Blighac70aaf2013-08-21 16:02:57 +0100988
989 if (use_icount) {
Alex Bligh40daca52013-08-21 16:03:02 +0100990 int64_t deadline = qemu_clock_deadline_ns_all(QEMU_CLOCK_VIRTUAL);
Alex Blighac70aaf2013-08-21 16:02:57 +0100991
992 if (deadline == 0) {
Alex Bligh40daca52013-08-21 16:03:02 +0100993 qemu_clock_notify(QEMU_CLOCK_VIRTUAL);
Alex Blighac70aaf2013-08-21 16:02:57 +0100994 }
Paolo Bonzini3b2319a2011-04-13 10:03:43 +0200995 }
Jan Kiszka6cabe1f2010-06-25 16:56:53 +0200996 qemu_tcg_wait_io_event();
Blue Swirl296af7c2010-03-29 19:23:50 +0000997 }
998
999 return NULL;
1000}
1001
Andreas Färber2ff09a42012-05-03 00:23:30 +02001002static void qemu_cpu_kick_thread(CPUState *cpu)
Paolo Bonzinicc015e92011-03-12 17:44:08 +01001003{
1004#ifndef _WIN32
1005 int err;
1006
Andreas Färber814e6122012-05-02 17:00:37 +02001007 err = pthread_kill(cpu->thread->thread, SIG_IPI);
Paolo Bonzinicc015e92011-03-12 17:44:08 +01001008 if (err) {
1009 fprintf(stderr, "qemu:%s: %s", __func__, strerror(err));
1010 exit(1);
1011 }
1012#else /* _WIN32 */
Andreas Färber60e82572012-05-02 22:23:49 +02001013 if (!qemu_cpu_is_self(cpu)) {
Olivier Hainqueed9164a2013-04-09 18:06:53 +02001014 CONTEXT tcgContext;
1015
1016 if (SuspendThread(cpu->hThread) == (DWORD)-1) {
Stefan Weil7f1721d2013-04-13 22:45:50 +02001017 fprintf(stderr, "qemu:%s: GetLastError:%lu\n", __func__,
Olivier Hainqueed9164a2013-04-09 18:06:53 +02001018 GetLastError());
1019 exit(1);
1020 }
1021
1022 /* On multi-core systems, we are not sure that the thread is actually
1023 * suspended until we can get the context.
1024 */
1025 tcgContext.ContextFlags = CONTEXT_CONTROL;
1026 while (GetThreadContext(cpu->hThread, &tcgContext) != 0) {
1027 continue;
1028 }
1029
Paolo Bonzinicc015e92011-03-12 17:44:08 +01001030 cpu_signal(0);
Olivier Hainqueed9164a2013-04-09 18:06:53 +02001031
1032 if (ResumeThread(cpu->hThread) == (DWORD)-1) {
Stefan Weil7f1721d2013-04-13 22:45:50 +02001033 fprintf(stderr, "qemu:%s: GetLastError:%lu\n", __func__,
Olivier Hainqueed9164a2013-04-09 18:06:53 +02001034 GetLastError());
1035 exit(1);
1036 }
Paolo Bonzinicc015e92011-03-12 17:44:08 +01001037 }
1038#endif
1039}
1040
Andreas Färberc08d7422012-05-03 04:34:15 +02001041void qemu_cpu_kick(CPUState *cpu)
Blue Swirl296af7c2010-03-29 19:23:50 +00001042{
Andreas Färberf5c121b2012-05-03 01:22:49 +02001043 qemu_cond_broadcast(cpu->halt_cond);
Andreas Färber216fc9a2012-05-02 17:49:49 +02001044 if (!tcg_enabled() && !cpu->thread_kicked) {
Andreas Färber2ff09a42012-05-03 00:23:30 +02001045 qemu_cpu_kick_thread(cpu);
Andreas Färber216fc9a2012-05-02 17:49:49 +02001046 cpu->thread_kicked = true;
Jan Kiszkaaa2c3642011-02-01 22:15:42 +01001047 }
Blue Swirl296af7c2010-03-29 19:23:50 +00001048}
1049
Jan Kiszka46d62fa2011-02-01 22:15:59 +01001050void qemu_cpu_kick_self(void)
1051{
Paolo Bonzinib55c22c2011-03-12 17:44:07 +01001052#ifndef _WIN32
Andreas Färber4917cf42013-05-27 05:17:50 +02001053 assert(current_cpu);
Jan Kiszka46d62fa2011-02-01 22:15:59 +01001054
Andreas Färber4917cf42013-05-27 05:17:50 +02001055 if (!current_cpu->thread_kicked) {
1056 qemu_cpu_kick_thread(current_cpu);
1057 current_cpu->thread_kicked = true;
Jan Kiszka46d62fa2011-02-01 22:15:59 +01001058 }
Paolo Bonzinib55c22c2011-03-12 17:44:07 +01001059#else
1060 abort();
1061#endif
Blue Swirl296af7c2010-03-29 19:23:50 +00001062}
1063
Andreas Färber60e82572012-05-02 22:23:49 +02001064bool qemu_cpu_is_self(CPUState *cpu)
Blue Swirl296af7c2010-03-29 19:23:50 +00001065{
Andreas Färber814e6122012-05-02 17:00:37 +02001066 return qemu_thread_is_self(cpu->thread);
Blue Swirl296af7c2010-03-29 19:23:50 +00001067}
1068
Juan Quintelaaa723c22012-09-18 16:30:11 +02001069static bool qemu_in_vcpu_thread(void)
1070{
Andreas Färber4917cf42013-05-27 05:17:50 +02001071 return current_cpu && qemu_cpu_is_self(current_cpu);
Juan Quintelaaa723c22012-09-18 16:30:11 +02001072}
1073
Blue Swirl296af7c2010-03-29 19:23:50 +00001074void qemu_mutex_lock_iothread(void)
1075{
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +02001076 if (!tcg_enabled()) {
Blue Swirl296af7c2010-03-29 19:23:50 +00001077 qemu_mutex_lock(&qemu_global_mutex);
Marcelo Tosatti1a28cac2010-05-04 09:45:20 -03001078 } else {
Paolo Bonzini46daff12011-06-09 13:10:24 +02001079 iothread_requesting_mutex = true;
Marcelo Tosatti1a28cac2010-05-04 09:45:20 -03001080 if (qemu_mutex_trylock(&qemu_global_mutex)) {
Andreas Färber182735e2013-05-29 22:29:20 +02001081 qemu_cpu_kick_thread(first_cpu);
Marcelo Tosatti1a28cac2010-05-04 09:45:20 -03001082 qemu_mutex_lock(&qemu_global_mutex);
1083 }
Paolo Bonzini46daff12011-06-09 13:10:24 +02001084 iothread_requesting_mutex = false;
1085 qemu_cond_broadcast(&qemu_io_proceeded_cond);
Marcelo Tosatti1a28cac2010-05-04 09:45:20 -03001086 }
Blue Swirl296af7c2010-03-29 19:23:50 +00001087}
1088
1089void qemu_mutex_unlock_iothread(void)
1090{
1091 qemu_mutex_unlock(&qemu_global_mutex);
1092}
1093
1094static int all_vcpus_paused(void)
1095{
Andreas Färberbdc44642013-06-24 23:50:24 +02001096 CPUState *cpu;
Blue Swirl296af7c2010-03-29 19:23:50 +00001097
Andreas Färberbdc44642013-06-24 23:50:24 +02001098 CPU_FOREACH(cpu) {
Andreas Färber182735e2013-05-29 22:29:20 +02001099 if (!cpu->stopped) {
Blue Swirl296af7c2010-03-29 19:23:50 +00001100 return 0;
Jan Kiszka0ab07c62011-02-07 12:19:14 +01001101 }
Blue Swirl296af7c2010-03-29 19:23:50 +00001102 }
1103
1104 return 1;
1105}
1106
1107void pause_all_vcpus(void)
1108{
Andreas Färberbdc44642013-06-24 23:50:24 +02001109 CPUState *cpu;
Blue Swirl296af7c2010-03-29 19:23:50 +00001110
Alex Bligh40daca52013-08-21 16:03:02 +01001111 qemu_clock_enable(QEMU_CLOCK_VIRTUAL, false);
Andreas Färberbdc44642013-06-24 23:50:24 +02001112 CPU_FOREACH(cpu) {
Andreas Färber182735e2013-05-29 22:29:20 +02001113 cpu->stop = true;
1114 qemu_cpu_kick(cpu);
Blue Swirl296af7c2010-03-29 19:23:50 +00001115 }
1116
Juan Quintelaaa723c22012-09-18 16:30:11 +02001117 if (qemu_in_vcpu_thread()) {
Jan Kiszkad798e972012-02-17 18:31:16 +01001118 cpu_stop_current();
1119 if (!kvm_enabled()) {
Andreas Färberbdc44642013-06-24 23:50:24 +02001120 CPU_FOREACH(cpu) {
Andreas Färber182735e2013-05-29 22:29:20 +02001121 cpu->stop = false;
1122 cpu->stopped = true;
Jan Kiszkad798e972012-02-17 18:31:16 +01001123 }
1124 return;
1125 }
1126 }
1127
Blue Swirl296af7c2010-03-29 19:23:50 +00001128 while (!all_vcpus_paused()) {
Paolo Bonzinibe7d6c52011-03-12 17:44:02 +01001129 qemu_cond_wait(&qemu_pause_cond, &qemu_global_mutex);
Andreas Färberbdc44642013-06-24 23:50:24 +02001130 CPU_FOREACH(cpu) {
Andreas Färber182735e2013-05-29 22:29:20 +02001131 qemu_cpu_kick(cpu);
Blue Swirl296af7c2010-03-29 19:23:50 +00001132 }
1133 }
1134}
1135
Igor Mammedov29936832013-04-23 10:29:37 +02001136void cpu_resume(CPUState *cpu)
1137{
1138 cpu->stop = false;
1139 cpu->stopped = false;
1140 qemu_cpu_kick(cpu);
1141}
1142
Blue Swirl296af7c2010-03-29 19:23:50 +00001143void resume_all_vcpus(void)
1144{
Andreas Färberbdc44642013-06-24 23:50:24 +02001145 CPUState *cpu;
Blue Swirl296af7c2010-03-29 19:23:50 +00001146
Alex Bligh40daca52013-08-21 16:03:02 +01001147 qemu_clock_enable(QEMU_CLOCK_VIRTUAL, true);
Andreas Färberbdc44642013-06-24 23:50:24 +02001148 CPU_FOREACH(cpu) {
Andreas Färber182735e2013-05-29 22:29:20 +02001149 cpu_resume(cpu);
Blue Swirl296af7c2010-03-29 19:23:50 +00001150 }
1151}
1152
Dr. David Alan Gilbert49001162014-01-30 10:20:32 +00001153/* For temporary buffers for forming a name */
1154#define VCPU_THREAD_NAME_SIZE 16
1155
Andreas Färbere5ab30a2012-05-03 01:50:44 +02001156static void qemu_tcg_init_vcpu(CPUState *cpu)
Blue Swirl296af7c2010-03-29 19:23:50 +00001157{
Dr. David Alan Gilbert49001162014-01-30 10:20:32 +00001158 char thread_name[VCPU_THREAD_NAME_SIZE];
1159
Edgar E. Iglesias09daed82013-12-17 13:06:51 +10001160 tcg_cpu_address_space_init(cpu, cpu->as);
1161
Blue Swirl296af7c2010-03-29 19:23:50 +00001162 /* share a single thread for all cpus with TCG */
1163 if (!tcg_cpu_thread) {
Andreas Färber814e6122012-05-02 17:00:37 +02001164 cpu->thread = g_malloc0(sizeof(QemuThread));
Andreas Färberf5c121b2012-05-03 01:22:49 +02001165 cpu->halt_cond = g_malloc0(sizeof(QemuCond));
1166 qemu_cond_init(cpu->halt_cond);
1167 tcg_halt_cond = cpu->halt_cond;
Dr. David Alan Gilbert49001162014-01-30 10:20:32 +00001168 snprintf(thread_name, VCPU_THREAD_NAME_SIZE, "CPU %d/TCG",
1169 cpu->cpu_index);
1170 qemu_thread_create(cpu->thread, thread_name, qemu_tcg_cpu_thread_fn,
1171 cpu, QEMU_THREAD_JOINABLE);
Paolo Bonzini1ecf47b2011-12-13 13:43:52 +01001172#ifdef _WIN32
Andreas Färber814e6122012-05-02 17:00:37 +02001173 cpu->hThread = qemu_thread_get_handle(cpu->thread);
Paolo Bonzini1ecf47b2011-12-13 13:43:52 +01001174#endif
Andreas Färber61a46212012-05-02 22:49:36 +02001175 while (!cpu->created) {
Paolo Bonzini18a85722011-03-12 17:44:03 +01001176 qemu_cond_wait(&qemu_cpu_cond, &qemu_global_mutex);
Jan Kiszka0ab07c62011-02-07 12:19:14 +01001177 }
Andreas Färber814e6122012-05-02 17:00:37 +02001178 tcg_cpu_thread = cpu->thread;
Blue Swirl296af7c2010-03-29 19:23:50 +00001179 } else {
Andreas Färber814e6122012-05-02 17:00:37 +02001180 cpu->thread = tcg_cpu_thread;
Andreas Färberf5c121b2012-05-03 01:22:49 +02001181 cpu->halt_cond = tcg_halt_cond;
Blue Swirl296af7c2010-03-29 19:23:50 +00001182 }
1183}
1184
Andreas Färber48a106b2013-05-27 02:20:39 +02001185static void qemu_kvm_start_vcpu(CPUState *cpu)
Blue Swirl296af7c2010-03-29 19:23:50 +00001186{
Dr. David Alan Gilbert49001162014-01-30 10:20:32 +00001187 char thread_name[VCPU_THREAD_NAME_SIZE];
1188
Andreas Färber814e6122012-05-02 17:00:37 +02001189 cpu->thread = g_malloc0(sizeof(QemuThread));
Andreas Färberf5c121b2012-05-03 01:22:49 +02001190 cpu->halt_cond = g_malloc0(sizeof(QemuCond));
1191 qemu_cond_init(cpu->halt_cond);
Dr. David Alan Gilbert49001162014-01-30 10:20:32 +00001192 snprintf(thread_name, VCPU_THREAD_NAME_SIZE, "CPU %d/KVM",
1193 cpu->cpu_index);
1194 qemu_thread_create(cpu->thread, thread_name, qemu_kvm_cpu_thread_fn,
1195 cpu, QEMU_THREAD_JOINABLE);
Andreas Färber61a46212012-05-02 22:49:36 +02001196 while (!cpu->created) {
Paolo Bonzini18a85722011-03-12 17:44:03 +01001197 qemu_cond_wait(&qemu_cpu_cond, &qemu_global_mutex);
Jan Kiszka0ab07c62011-02-07 12:19:14 +01001198 }
Blue Swirl296af7c2010-03-29 19:23:50 +00001199}
1200
Andreas Färber10a90212013-05-27 02:24:35 +02001201static void qemu_dummy_start_vcpu(CPUState *cpu)
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +02001202{
Dr. David Alan Gilbert49001162014-01-30 10:20:32 +00001203 char thread_name[VCPU_THREAD_NAME_SIZE];
1204
Andreas Färber814e6122012-05-02 17:00:37 +02001205 cpu->thread = g_malloc0(sizeof(QemuThread));
Andreas Färberf5c121b2012-05-03 01:22:49 +02001206 cpu->halt_cond = g_malloc0(sizeof(QemuCond));
1207 qemu_cond_init(cpu->halt_cond);
Dr. David Alan Gilbert49001162014-01-30 10:20:32 +00001208 snprintf(thread_name, VCPU_THREAD_NAME_SIZE, "CPU %d/DUMMY",
1209 cpu->cpu_index);
1210 qemu_thread_create(cpu->thread, thread_name, qemu_dummy_cpu_thread_fn, cpu,
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +02001211 QEMU_THREAD_JOINABLE);
Andreas Färber61a46212012-05-02 22:49:36 +02001212 while (!cpu->created) {
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +02001213 qemu_cond_wait(&qemu_cpu_cond, &qemu_global_mutex);
1214 }
1215}
1216
Andreas Färberc643bed2013-05-27 03:23:24 +02001217void qemu_init_vcpu(CPUState *cpu)
Blue Swirl296af7c2010-03-29 19:23:50 +00001218{
Andreas Färberce3960e2012-12-17 03:27:07 +01001219 cpu->nr_cores = smp_cores;
1220 cpu->nr_threads = smp_threads;
Andreas Färberf324e762012-05-02 23:26:21 +02001221 cpu->stopped = true;
Jan Kiszka0ab07c62011-02-07 12:19:14 +01001222 if (kvm_enabled()) {
Andreas Färber48a106b2013-05-27 02:20:39 +02001223 qemu_kvm_start_vcpu(cpu);
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +02001224 } else if (tcg_enabled()) {
Andreas Färbere5ab30a2012-05-03 01:50:44 +02001225 qemu_tcg_init_vcpu(cpu);
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +02001226 } else {
Andreas Färber10a90212013-05-27 02:24:35 +02001227 qemu_dummy_start_vcpu(cpu);
Jan Kiszka0ab07c62011-02-07 12:19:14 +01001228 }
Blue Swirl296af7c2010-03-29 19:23:50 +00001229}
1230
Jan Kiszkab4a3d962011-02-01 22:15:43 +01001231void cpu_stop_current(void)
Blue Swirl296af7c2010-03-29 19:23:50 +00001232{
Andreas Färber4917cf42013-05-27 05:17:50 +02001233 if (current_cpu) {
1234 current_cpu->stop = false;
1235 current_cpu->stopped = true;
1236 cpu_exit(current_cpu);
Paolo Bonzini67bb1722011-03-12 17:43:59 +01001237 qemu_cond_signal(&qemu_pause_cond);
Jan Kiszkab4a3d962011-02-01 22:15:43 +01001238 }
Blue Swirl296af7c2010-03-29 19:23:50 +00001239}
1240
Kevin Wolf56983462013-07-05 13:49:54 +02001241int vm_stop(RunState state)
Blue Swirl296af7c2010-03-29 19:23:50 +00001242{
Juan Quintelaaa723c22012-09-18 16:30:11 +02001243 if (qemu_in_vcpu_thread()) {
Paolo Bonzini74892d22014-06-05 14:53:58 +02001244 qemu_system_vmstop_request_prepare();
Luiz Capitulino1dfb4dd2011-07-29 14:26:33 -03001245 qemu_system_vmstop_request(state);
Blue Swirl296af7c2010-03-29 19:23:50 +00001246 /*
1247 * FIXME: should not return to device code in case
1248 * vm_stop() has been requested.
1249 */
Jan Kiszkab4a3d962011-02-01 22:15:43 +01001250 cpu_stop_current();
Kevin Wolf56983462013-07-05 13:49:54 +02001251 return 0;
Blue Swirl296af7c2010-03-29 19:23:50 +00001252 }
Kevin Wolf56983462013-07-05 13:49:54 +02001253
1254 return do_vm_stop(state);
Blue Swirl296af7c2010-03-29 19:23:50 +00001255}
1256
Luiz Capitulino8a9236f2011-10-14 11:18:09 -03001257/* does a state transition even if the VM is already stopped,
1258 current state is forgotten forever */
Kevin Wolf56983462013-07-05 13:49:54 +02001259int vm_stop_force_state(RunState state)
Luiz Capitulino8a9236f2011-10-14 11:18:09 -03001260{
1261 if (runstate_is_running()) {
Kevin Wolf56983462013-07-05 13:49:54 +02001262 return vm_stop(state);
Luiz Capitulino8a9236f2011-10-14 11:18:09 -03001263 } else {
1264 runstate_set(state);
Kevin Wolf594a45c2013-07-18 14:52:19 +02001265 /* Make sure to return an error if the flush in a previous vm_stop()
1266 * failed. */
1267 return bdrv_flush_all();
Luiz Capitulino8a9236f2011-10-14 11:18:09 -03001268 }
1269}
1270
Andreas Färber9349b4f2012-03-14 01:38:32 +01001271static int tcg_cpu_exec(CPUArchState *env)
Blue Swirl296af7c2010-03-29 19:23:50 +00001272{
Andreas Färberefee7342013-08-26 05:39:29 +02001273 CPUState *cpu = ENV_GET_CPU(env);
Blue Swirl296af7c2010-03-29 19:23:50 +00001274 int ret;
1275#ifdef CONFIG_PROFILER
1276 int64_t ti;
1277#endif
1278
1279#ifdef CONFIG_PROFILER
1280 ti = profile_getclock();
1281#endif
1282 if (use_icount) {
1283 int64_t count;
Alex Blighac70aaf2013-08-21 16:02:57 +01001284 int64_t deadline;
Blue Swirl296af7c2010-03-29 19:23:50 +00001285 int decr;
KONRAD Fredericc96778b2014-08-01 01:37:09 +02001286 timers_state.qemu_icount -= (cpu->icount_decr.u16.low
1287 + cpu->icount_extra);
Andreas Färber28ecfd72013-08-26 05:51:49 +02001288 cpu->icount_decr.u16.low = 0;
Andreas Färberefee7342013-08-26 05:39:29 +02001289 cpu->icount_extra = 0;
Alex Bligh40daca52013-08-21 16:03:02 +01001290 deadline = qemu_clock_deadline_ns_all(QEMU_CLOCK_VIRTUAL);
Alex Blighac70aaf2013-08-21 16:02:57 +01001291
1292 /* Maintain prior (possibly buggy) behaviour where if no deadline
Alex Bligh40daca52013-08-21 16:03:02 +01001293 * was set (as there is no QEMU_CLOCK_VIRTUAL timer) or it is more than
Alex Blighac70aaf2013-08-21 16:02:57 +01001294 * INT32_MAX nanoseconds ahead, we still use INT32_MAX
1295 * nanoseconds.
1296 */
1297 if ((deadline < 0) || (deadline > INT32_MAX)) {
1298 deadline = INT32_MAX;
1299 }
1300
1301 count = qemu_icount_round(deadline);
KONRAD Fredericc96778b2014-08-01 01:37:09 +02001302 timers_state.qemu_icount += count;
Blue Swirl296af7c2010-03-29 19:23:50 +00001303 decr = (count > 0xffff) ? 0xffff : count;
1304 count -= decr;
Andreas Färber28ecfd72013-08-26 05:51:49 +02001305 cpu->icount_decr.u16.low = decr;
Andreas Färberefee7342013-08-26 05:39:29 +02001306 cpu->icount_extra = count;
Blue Swirl296af7c2010-03-29 19:23:50 +00001307 }
1308 ret = cpu_exec(env);
1309#ifdef CONFIG_PROFILER
1310 qemu_time += profile_getclock() - ti;
1311#endif
1312 if (use_icount) {
1313 /* Fold pending instructions back into the
1314 instruction counter, and clear the interrupt flag. */
KONRAD Fredericc96778b2014-08-01 01:37:09 +02001315 timers_state.qemu_icount -= (cpu->icount_decr.u16.low
1316 + cpu->icount_extra);
Andreas Färber28ecfd72013-08-26 05:51:49 +02001317 cpu->icount_decr.u32 = 0;
Andreas Färberefee7342013-08-26 05:39:29 +02001318 cpu->icount_extra = 0;
Blue Swirl296af7c2010-03-29 19:23:50 +00001319 }
1320 return ret;
1321}
1322
Jan Kiszkabdb7ca62011-09-26 09:40:39 +02001323static void tcg_exec_all(void)
Blue Swirl296af7c2010-03-29 19:23:50 +00001324{
Jan Kiszka9a360852011-02-01 22:15:55 +01001325 int r;
1326
Alex Bligh40daca52013-08-21 16:03:02 +01001327 /* Account partial waits to QEMU_CLOCK_VIRTUAL. */
1328 qemu_clock_warp(QEMU_CLOCK_VIRTUAL);
Paolo Bonziniab33fcd2011-04-13 10:03:44 +02001329
Jan Kiszka0ab07c62011-02-07 12:19:14 +01001330 if (next_cpu == NULL) {
Blue Swirl296af7c2010-03-29 19:23:50 +00001331 next_cpu = first_cpu;
Jan Kiszka0ab07c62011-02-07 12:19:14 +01001332 }
Andreas Färberbdc44642013-06-24 23:50:24 +02001333 for (; next_cpu != NULL && !exit_request; next_cpu = CPU_NEXT(next_cpu)) {
Andreas Färber182735e2013-05-29 22:29:20 +02001334 CPUState *cpu = next_cpu;
1335 CPUArchState *env = cpu->env_ptr;
Blue Swirl296af7c2010-03-29 19:23:50 +00001336
Alex Bligh40daca52013-08-21 16:03:02 +01001337 qemu_clock_enable(QEMU_CLOCK_VIRTUAL,
Andreas Färbered2803d2013-06-21 20:20:45 +02001338 (cpu->singlestep_enabled & SSTEP_NOTIMER) == 0);
Blue Swirl296af7c2010-03-29 19:23:50 +00001339
Andreas Färbera1fcaa72012-05-02 23:42:26 +02001340 if (cpu_can_run(cpu)) {
Jan Kiszkabdb7ca62011-09-26 09:40:39 +02001341 r = tcg_cpu_exec(env);
Jan Kiszka9a360852011-02-01 22:15:55 +01001342 if (r == EXCP_DEBUG) {
Andreas Färber91325042013-05-27 02:07:49 +02001343 cpu_handle_guest_debug(cpu);
Jan Kiszka3c638d02010-06-25 16:56:56 +02001344 break;
1345 }
Andreas Färberf324e762012-05-02 23:26:21 +02001346 } else if (cpu->stop || cpu->stopped) {
Blue Swirl296af7c2010-03-29 19:23:50 +00001347 break;
1348 }
1349 }
Jan Kiszkac629a4b2010-06-25 16:56:52 +02001350 exit_request = 0;
Blue Swirl296af7c2010-03-29 19:23:50 +00001351}
1352
Stefan Weil9a78eea2010-10-22 23:03:33 +02001353void list_cpus(FILE *f, fprintf_function cpu_fprintf, const char *optarg)
Blue Swirl262353c2010-05-04 19:55:35 +00001354{
1355 /* XXX: implement xxx_cpu_list for targets that still miss it */
Peter Maydelle916cbf2012-09-05 17:41:08 -03001356#if defined(cpu_list)
1357 cpu_list(f, cpu_fprintf);
Blue Swirl262353c2010-05-04 19:55:35 +00001358#endif
1359}
Luiz Capitulinode0b36b2011-09-21 16:38:35 -03001360
1361CpuInfoList *qmp_query_cpus(Error **errp)
1362{
1363 CpuInfoList *head = NULL, *cur_item = NULL;
Andreas Färber182735e2013-05-29 22:29:20 +02001364 CPUState *cpu;
Luiz Capitulinode0b36b2011-09-21 16:38:35 -03001365
Andreas Färberbdc44642013-06-24 23:50:24 +02001366 CPU_FOREACH(cpu) {
Luiz Capitulinode0b36b2011-09-21 16:38:35 -03001367 CpuInfoList *info;
Andreas Färber182735e2013-05-29 22:29:20 +02001368#if defined(TARGET_I386)
1369 X86CPU *x86_cpu = X86_CPU(cpu);
1370 CPUX86State *env = &x86_cpu->env;
1371#elif defined(TARGET_PPC)
1372 PowerPCCPU *ppc_cpu = POWERPC_CPU(cpu);
1373 CPUPPCState *env = &ppc_cpu->env;
1374#elif defined(TARGET_SPARC)
1375 SPARCCPU *sparc_cpu = SPARC_CPU(cpu);
1376 CPUSPARCState *env = &sparc_cpu->env;
1377#elif defined(TARGET_MIPS)
1378 MIPSCPU *mips_cpu = MIPS_CPU(cpu);
1379 CPUMIPSState *env = &mips_cpu->env;
1380#endif
Luiz Capitulinode0b36b2011-09-21 16:38:35 -03001381
Andreas Färbercb446ec2013-05-01 14:24:52 +02001382 cpu_synchronize_state(cpu);
Luiz Capitulinode0b36b2011-09-21 16:38:35 -03001383
1384 info = g_malloc0(sizeof(*info));
1385 info->value = g_malloc0(sizeof(*info->value));
Andreas Färber55e5c282012-12-17 06:18:02 +01001386 info->value->CPU = cpu->cpu_index;
Andreas Färber182735e2013-05-29 22:29:20 +02001387 info->value->current = (cpu == first_cpu);
Andreas Färber259186a2013-01-17 18:51:17 +01001388 info->value->halted = cpu->halted;
Andreas Färber9f09e182012-05-03 06:59:07 +02001389 info->value->thread_id = cpu->thread_id;
Luiz Capitulinode0b36b2011-09-21 16:38:35 -03001390#if defined(TARGET_I386)
1391 info->value->has_pc = true;
1392 info->value->pc = env->eip + env->segs[R_CS].base;
1393#elif defined(TARGET_PPC)
1394 info->value->has_nip = true;
1395 info->value->nip = env->nip;
1396#elif defined(TARGET_SPARC)
1397 info->value->has_pc = true;
1398 info->value->pc = env->pc;
1399 info->value->has_npc = true;
1400 info->value->npc = env->npc;
1401#elif defined(TARGET_MIPS)
1402 info->value->has_PC = true;
1403 info->value->PC = env->active_tc.PC;
1404#endif
1405
1406 /* XXX: waiting for the qapi to support GSList */
1407 if (!cur_item) {
1408 head = cur_item = info;
1409 } else {
1410 cur_item->next = info;
1411 cur_item = info;
1412 }
1413 }
1414
1415 return head;
1416}
Luiz Capitulino0cfd6a92011-11-22 16:32:37 -02001417
1418void qmp_memsave(int64_t addr, int64_t size, const char *filename,
1419 bool has_cpu, int64_t cpu_index, Error **errp)
1420{
1421 FILE *f;
1422 uint32_t l;
Andreas Färber55e5c282012-12-17 06:18:02 +01001423 CPUState *cpu;
Luiz Capitulino0cfd6a92011-11-22 16:32:37 -02001424 uint8_t buf[1024];
1425
1426 if (!has_cpu) {
1427 cpu_index = 0;
1428 }
1429
Andreas Färber151d1322013-02-15 15:41:49 +01001430 cpu = qemu_get_cpu(cpu_index);
1431 if (cpu == NULL) {
Luiz Capitulino0cfd6a92011-11-22 16:32:37 -02001432 error_set(errp, QERR_INVALID_PARAMETER_VALUE, "cpu-index",
1433 "a CPU number");
1434 return;
1435 }
1436
1437 f = fopen(filename, "wb");
1438 if (!f) {
Luiz Capitulino618da852013-06-07 14:35:06 -04001439 error_setg_file_open(errp, errno, filename);
Luiz Capitulino0cfd6a92011-11-22 16:32:37 -02001440 return;
1441 }
1442
1443 while (size != 0) {
1444 l = sizeof(buf);
1445 if (l > size)
1446 l = size;
Aneesh Kumar K.V2f4d0f52013-10-01 21:49:30 +05301447 if (cpu_memory_rw_debug(cpu, addr, buf, l, 0) != 0) {
1448 error_setg(errp, "Invalid addr 0x%016" PRIx64 "specified", addr);
1449 goto exit;
1450 }
Luiz Capitulino0cfd6a92011-11-22 16:32:37 -02001451 if (fwrite(buf, 1, l, f) != l) {
1452 error_set(errp, QERR_IO_ERROR);
1453 goto exit;
1454 }
1455 addr += l;
1456 size -= l;
1457 }
1458
1459exit:
1460 fclose(f);
1461}
Luiz Capitulino6d3962b2011-11-22 17:26:46 -02001462
1463void qmp_pmemsave(int64_t addr, int64_t size, const char *filename,
1464 Error **errp)
1465{
1466 FILE *f;
1467 uint32_t l;
1468 uint8_t buf[1024];
1469
1470 f = fopen(filename, "wb");
1471 if (!f) {
Luiz Capitulino618da852013-06-07 14:35:06 -04001472 error_setg_file_open(errp, errno, filename);
Luiz Capitulino6d3962b2011-11-22 17:26:46 -02001473 return;
1474 }
1475
1476 while (size != 0) {
1477 l = sizeof(buf);
1478 if (l > size)
1479 l = size;
Stefan Weileb6282f2014-04-07 20:28:23 +02001480 cpu_physical_memory_read(addr, buf, l);
Luiz Capitulino6d3962b2011-11-22 17:26:46 -02001481 if (fwrite(buf, 1, l, f) != l) {
1482 error_set(errp, QERR_IO_ERROR);
1483 goto exit;
1484 }
1485 addr += l;
1486 size -= l;
1487 }
1488
1489exit:
1490 fclose(f);
1491}
Luiz Capitulinoab49ab52011-11-23 12:55:53 -02001492
1493void qmp_inject_nmi(Error **errp)
1494{
1495#if defined(TARGET_I386)
Andreas Färber182735e2013-05-29 22:29:20 +02001496 CPUState *cs;
Luiz Capitulinoab49ab52011-11-23 12:55:53 -02001497
Andreas Färberbdc44642013-06-24 23:50:24 +02001498 CPU_FOREACH(cs) {
Andreas Färber182735e2013-05-29 22:29:20 +02001499 X86CPU *cpu = X86_CPU(cs);
Andreas Färber182735e2013-05-29 22:29:20 +02001500
Chen Fan02e51482013-12-23 17:04:02 +08001501 if (!cpu->apic_state) {
Andreas Färber182735e2013-05-29 22:29:20 +02001502 cpu_interrupt(cs, CPU_INTERRUPT_NMI);
Jan Kiszka02c09192011-10-18 00:00:06 +08001503 } else {
Chen Fan02e51482013-12-23 17:04:02 +08001504 apic_deliver_nmi(cpu->apic_state);
Jan Kiszka02c09192011-10-18 00:00:06 +08001505 }
Luiz Capitulinoab49ab52011-11-23 12:55:53 -02001506 }
Eugene (jno) Dvurechenski7f7f9752012-12-05 15:50:07 +01001507#elif defined(TARGET_S390X)
1508 CPUState *cs;
1509 S390CPU *cpu;
1510
Andreas Färberbdc44642013-06-24 23:50:24 +02001511 CPU_FOREACH(cs) {
Eugene (jno) Dvurechenski7f7f9752012-12-05 15:50:07 +01001512 cpu = S390_CPU(cs);
1513 if (cpu->env.cpu_num == monitor_get_cpu_index()) {
1514 if (s390_cpu_restart(S390_CPU(cs)) == -1) {
1515 error_set(errp, QERR_UNSUPPORTED);
1516 return;
1517 }
1518 break;
1519 }
1520 }
Luiz Capitulinoab49ab52011-11-23 12:55:53 -02001521#else
1522 error_set(errp, QERR_UNSUPPORTED);
1523#endif
1524}