blob: 69e21858b8090bee35fa52ee4ea39efc20929816 [file] [log] [blame]
Blue Swirl296af7c2010-03-29 19:23:50 +00001/*
2 * QEMU System Emulator
3 *
4 * Copyright (c) 2003-2008 Fabrice Bellard
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
23 */
24
25/* Needed early for CONFIG_BSD etc. */
Peter Maydell7b31bbc2016-01-26 18:16:56 +000026#include "qemu/osdep.h"
Paolo Bonzini33c11872016-03-15 16:58:45 +010027#include "qemu-common.h"
KONRAD Frederic8d4e9142017-02-23 18:29:08 +000028#include "qemu/config-file.h"
Paolo Bonzini33c11872016-03-15 16:58:45 +010029#include "cpu.h"
Paolo Bonzini83c90892012-12-17 18:19:49 +010030#include "monitor/monitor.h"
Wenchao Xiaa4e15de2014-06-18 08:43:36 +020031#include "qapi/qmp/qerror.h"
Markus Armbrusterd49b6832015-03-17 18:29:20 +010032#include "qemu/error-report.h"
Paolo Bonzini9c17d612012-12-17 18:20:04 +010033#include "sysemu/sysemu.h"
Max Reitzda31d592016-03-16 19:54:32 +010034#include "sysemu/block-backend.h"
Paolo Bonzini022c62c2012-12-17 18:19:49 +010035#include "exec/gdbstub.h"
Paolo Bonzini9c17d612012-12-17 18:20:04 +010036#include "sysemu/dma.h"
Vincent Palatinb3946622017-01-10 11:59:55 +010037#include "sysemu/hw_accel.h"
Paolo Bonzini9c17d612012-12-17 18:20:04 +010038#include "sysemu/kvm.h"
Vincent Palatinb0cb0a62017-01-10 11:59:57 +010039#include "sysemu/hax.h"
Luiz Capitulinode0b36b2011-09-21 16:38:35 -030040#include "qmp-commands.h"
Paolo Bonzini63c91552016-03-15 13:18:37 +010041#include "exec/exec-all.h"
Blue Swirl296af7c2010-03-29 19:23:50 +000042
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010043#include "qemu/thread.h"
Paolo Bonzini9c17d612012-12-17 18:20:04 +010044#include "sysemu/cpus.h"
45#include "sysemu/qtest.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010046#include "qemu/main-loop.h"
47#include "qemu/bitmap.h"
Liu Ping Fancb365642013-09-25 14:20:58 +080048#include "qemu/seqlock.h"
KONRAD Frederic8d4e9142017-02-23 18:29:08 +000049#include "tcg.h"
Wenchao Xiaa4e15de2014-06-18 08:43:36 +020050#include "qapi-event.h"
Alexey Kardashevskiy9cb805f2014-08-20 22:16:33 +100051#include "hw/nmi.h"
Pavel Dovgalyuk8b427042015-09-17 19:24:05 +030052#include "sysemu/replay.h"
Jan Kiszka0ff0fc12011-06-23 10:15:55 +020053
Jan Kiszka6d9cb732011-02-01 22:15:58 +010054#ifdef CONFIG_LINUX
55
56#include <sys/prctl.h>
57
Marcelo Tosattic0532a72010-10-11 15:31:21 -030058#ifndef PR_MCE_KILL
59#define PR_MCE_KILL 33
60#endif
61
Jan Kiszka6d9cb732011-02-01 22:15:58 +010062#ifndef PR_MCE_KILL_SET
63#define PR_MCE_KILL_SET 1
64#endif
65
66#ifndef PR_MCE_KILL_EARLY
67#define PR_MCE_KILL_EARLY 1
68#endif
69
70#endif /* CONFIG_LINUX */
71
Sebastian Tanase27498be2014-07-25 11:56:33 +020072int64_t max_delay;
73int64_t max_advance;
Blue Swirl296af7c2010-03-29 19:23:50 +000074
Jason J. Herne2adcc852015-09-08 13:12:33 -040075/* vcpu throttling controls */
76static QEMUTimer *throttle_timer;
77static unsigned int throttle_percentage;
78
79#define CPU_THROTTLE_PCT_MIN 1
80#define CPU_THROTTLE_PCT_MAX 99
81#define CPU_THROTTLE_TIMESLICE_NS 10000000
82
Tiejun Chen321bc0b2013-08-02 09:43:09 +080083bool cpu_is_stopped(CPUState *cpu)
84{
85 return cpu->stopped || !runstate_is_running();
86}
87
Andreas Färbera98ae1d2013-05-26 23:21:08 +020088static bool cpu_thread_is_idle(CPUState *cpu)
Peter Maydellac873f12012-07-19 16:52:27 +010089{
Andreas Färberc64ca812012-05-03 02:11:45 +020090 if (cpu->stop || cpu->queued_work_first) {
Peter Maydellac873f12012-07-19 16:52:27 +010091 return false;
92 }
Tiejun Chen321bc0b2013-08-02 09:43:09 +080093 if (cpu_is_stopped(cpu)) {
Peter Maydellac873f12012-07-19 16:52:27 +010094 return true;
95 }
Andreas Färber8c2e1b02013-08-25 18:53:55 +020096 if (!cpu->halted || cpu_has_work(cpu) ||
Alexander Graf215e79c2013-04-24 22:24:12 +020097 kvm_halt_in_kernel()) {
Peter Maydellac873f12012-07-19 16:52:27 +010098 return false;
99 }
100 return true;
101}
102
103static bool all_cpu_threads_idle(void)
104{
Andreas Färber182735e2013-05-29 22:29:20 +0200105 CPUState *cpu;
Peter Maydellac873f12012-07-19 16:52:27 +0100106
Andreas Färberbdc44642013-06-24 23:50:24 +0200107 CPU_FOREACH(cpu) {
Andreas Färber182735e2013-05-29 22:29:20 +0200108 if (!cpu_thread_is_idle(cpu)) {
Peter Maydellac873f12012-07-19 16:52:27 +0100109 return false;
110 }
111 }
112 return true;
113}
114
Blue Swirl296af7c2010-03-29 19:23:50 +0000115/***********************************************************/
Paolo Bonzini946fb272011-09-12 13:57:37 +0200116/* guest cycle counter */
117
Paolo Bonzinia3270e12013-10-07 17:18:15 +0200118/* Protected by TimersState seqlock */
119
Victor CLEMENT5045e9d92015-05-29 17:14:04 +0200120static bool icount_sleep = true;
Sebastian Tanase71468392014-07-23 11:47:50 +0200121static int64_t vm_clock_warp_start = -1;
Paolo Bonzini946fb272011-09-12 13:57:37 +0200122/* Conversion factor from emulated instructions to virtual clock ticks. */
123static int icount_time_shift;
124/* Arbitrarily pick 1MIPS as the minimum allowable speed. */
125#define MAX_ICOUNT_SHIFT 10
Paolo Bonzinia3270e12013-10-07 17:18:15 +0200126
Paolo Bonzini946fb272011-09-12 13:57:37 +0200127static QEMUTimer *icount_rt_timer;
128static QEMUTimer *icount_vm_timer;
129static QEMUTimer *icount_warp_timer;
Paolo Bonzini946fb272011-09-12 13:57:37 +0200130
131typedef struct TimersState {
Liu Ping Fancb365642013-09-25 14:20:58 +0800132 /* Protected by BQL. */
Paolo Bonzini946fb272011-09-12 13:57:37 +0200133 int64_t cpu_ticks_prev;
134 int64_t cpu_ticks_offset;
Liu Ping Fancb365642013-09-25 14:20:58 +0800135
136 /* cpu_clock_offset can be read out of BQL, so protect it with
137 * this lock.
138 */
139 QemuSeqLock vm_clock_seqlock;
Paolo Bonzini946fb272011-09-12 13:57:37 +0200140 int64_t cpu_clock_offset;
141 int32_t cpu_ticks_enabled;
142 int64_t dummy;
KONRAD Fredericc96778b2014-08-01 01:37:09 +0200143
144 /* Compensate for varying guest execution speed. */
145 int64_t qemu_icount_bias;
146 /* Only written by TCG thread */
147 int64_t qemu_icount;
Paolo Bonzini946fb272011-09-12 13:57:37 +0200148} TimersState;
149
Liu Ping Fand9cd4002013-07-21 08:43:00 +0000150static TimersState timers_state;
KONRAD Frederic8d4e9142017-02-23 18:29:08 +0000151bool mttcg_enabled;
152
153/*
154 * We default to false if we know other options have been enabled
155 * which are currently incompatible with MTTCG. Otherwise when each
156 * guest (target) has been updated to support:
157 * - atomic instructions
158 * - memory ordering primitives (barriers)
159 * they can set the appropriate CONFIG flags in ${target}-softmmu.mak
160 *
161 * Once a guest architecture has been converted to the new primitives
162 * there are two remaining limitations to check.
163 *
164 * - The guest can't be oversized (e.g. 64 bit guest on 32 bit host)
165 * - The host must have a stronger memory order than the guest
166 *
167 * It may be possible in future to support strong guests on weak hosts
168 * but that will require tagging all load/stores in a guest with their
169 * implicit memory order requirements which would likely slow things
170 * down a lot.
171 */
172
173static bool check_tcg_memory_orders_compatible(void)
174{
175#if defined(TCG_GUEST_DEFAULT_MO) && defined(TCG_TARGET_DEFAULT_MO)
176 return (TCG_GUEST_DEFAULT_MO & ~TCG_TARGET_DEFAULT_MO) == 0;
177#else
178 return false;
179#endif
180}
181
182static bool default_mttcg_enabled(void)
183{
Alex Bennée83fd9622017-02-27 17:09:01 +0000184 if (use_icount || TCG_OVERSIZED_GUEST) {
KONRAD Frederic8d4e9142017-02-23 18:29:08 +0000185 return false;
186 } else {
187#ifdef TARGET_SUPPORTS_MTTCG
188 return check_tcg_memory_orders_compatible();
189#else
190 return false;
191#endif
192 }
193}
194
195void qemu_tcg_configure(QemuOpts *opts, Error **errp)
196{
197 const char *t = qemu_opt_get(opts, "thread");
198 if (t) {
199 if (strcmp(t, "multi") == 0) {
200 if (TCG_OVERSIZED_GUEST) {
201 error_setg(errp, "No MTTCG when guest word size > hosts");
Alex Bennée83fd9622017-02-27 17:09:01 +0000202 } else if (use_icount) {
203 error_setg(errp, "No MTTCG when icount is enabled");
KONRAD Frederic8d4e9142017-02-23 18:29:08 +0000204 } else {
Alex Bennéec34c7622017-02-28 14:40:17 +0000205#ifndef TARGET_SUPPORT_MTTCG
206 error_report("Guest not yet converted to MTTCG - "
207 "you may get unexpected results");
208#endif
KONRAD Frederic8d4e9142017-02-23 18:29:08 +0000209 if (!check_tcg_memory_orders_compatible()) {
210 error_report("Guest expects a stronger memory ordering "
211 "than the host provides");
212 error_printf("This may cause strange/hard to debug errors");
213 }
214 mttcg_enabled = true;
215 }
216 } else if (strcmp(t, "single") == 0) {
217 mttcg_enabled = false;
218 } else {
219 error_setg(errp, "Invalid 'thread' setting %s", t);
220 }
221 } else {
222 mttcg_enabled = default_mttcg_enabled();
223 }
224}
Paolo Bonzini946fb272011-09-12 13:57:37 +0200225
Pavel Dovgalyuk2a629142014-12-08 10:53:45 +0300226int64_t cpu_get_icount_raw(void)
Paolo Bonzini946fb272011-09-12 13:57:37 +0200227{
228 int64_t icount;
Andreas Färber4917cf42013-05-27 05:17:50 +0200229 CPUState *cpu = current_cpu;
Paolo Bonzini946fb272011-09-12 13:57:37 +0200230
KONRAD Fredericc96778b2014-08-01 01:37:09 +0200231 icount = timers_state.qemu_icount;
Andreas Färber4917cf42013-05-27 05:17:50 +0200232 if (cpu) {
Paolo Bonzini414b15c2015-06-24 14:16:26 +0200233 if (!cpu->can_do_io) {
Pavel Dovgalyuk2a629142014-12-08 10:53:45 +0300234 fprintf(stderr, "Bad icount read\n");
235 exit(1);
Paolo Bonzini946fb272011-09-12 13:57:37 +0200236 }
Andreas Färber28ecfd72013-08-26 05:51:49 +0200237 icount -= (cpu->icount_decr.u16.low + cpu->icount_extra);
Paolo Bonzini946fb272011-09-12 13:57:37 +0200238 }
Pavel Dovgalyuk2a629142014-12-08 10:53:45 +0300239 return icount;
240}
241
242/* Return the virtual CPU time, based on the instruction counter. */
243static int64_t cpu_get_icount_locked(void)
244{
245 int64_t icount = cpu_get_icount_raw();
KONRAD Frederic3f031312014-08-01 01:37:15 +0200246 return timers_state.qemu_icount_bias + cpu_icount_to_ns(icount);
Paolo Bonzini946fb272011-09-12 13:57:37 +0200247}
248
Paolo Bonzini17a15f12013-10-03 15:17:25 +0200249int64_t cpu_get_icount(void)
250{
251 int64_t icount;
252 unsigned start;
253
254 do {
255 start = seqlock_read_begin(&timers_state.vm_clock_seqlock);
256 icount = cpu_get_icount_locked();
257 } while (seqlock_read_retry(&timers_state.vm_clock_seqlock, start));
258
259 return icount;
260}
261
KONRAD Frederic3f031312014-08-01 01:37:15 +0200262int64_t cpu_icount_to_ns(int64_t icount)
263{
264 return icount << icount_time_shift;
265}
266
Cao jind90f3cc2016-07-29 19:05:38 +0800267/* return the time elapsed in VM between vm_start and vm_stop. Unless
268 * icount is active, cpu_get_ticks() uses units of the host CPU cycle
269 * counter.
270 *
271 * Caller must hold the BQL
272 */
Paolo Bonzini946fb272011-09-12 13:57:37 +0200273int64_t cpu_get_ticks(void)
274{
Paolo Bonzini5f3e3102013-10-28 17:32:18 +0100275 int64_t ticks;
276
Paolo Bonzini946fb272011-09-12 13:57:37 +0200277 if (use_icount) {
278 return cpu_get_icount();
279 }
Paolo Bonzini5f3e3102013-10-28 17:32:18 +0100280
281 ticks = timers_state.cpu_ticks_offset;
282 if (timers_state.cpu_ticks_enabled) {
Christopher Covington4a7428c2015-09-25 10:42:21 -0400283 ticks += cpu_get_host_ticks();
Paolo Bonzini946fb272011-09-12 13:57:37 +0200284 }
Paolo Bonzini5f3e3102013-10-28 17:32:18 +0100285
286 if (timers_state.cpu_ticks_prev > ticks) {
287 /* Note: non increasing ticks may happen if the host uses
288 software suspend */
289 timers_state.cpu_ticks_offset += timers_state.cpu_ticks_prev - ticks;
290 ticks = timers_state.cpu_ticks_prev;
291 }
292
293 timers_state.cpu_ticks_prev = ticks;
294 return ticks;
Paolo Bonzini946fb272011-09-12 13:57:37 +0200295}
296
Liu Ping Fancb365642013-09-25 14:20:58 +0800297static int64_t cpu_get_clock_locked(void)
298{
Cao jin1d45cea2016-07-29 19:05:37 +0800299 int64_t time;
Liu Ping Fancb365642013-09-25 14:20:58 +0800300
Cao jin1d45cea2016-07-29 19:05:37 +0800301 time = timers_state.cpu_clock_offset;
Paolo Bonzini5f3e3102013-10-28 17:32:18 +0100302 if (timers_state.cpu_ticks_enabled) {
Cao jin1d45cea2016-07-29 19:05:37 +0800303 time += get_clock();
Liu Ping Fancb365642013-09-25 14:20:58 +0800304 }
305
Cao jin1d45cea2016-07-29 19:05:37 +0800306 return time;
Liu Ping Fancb365642013-09-25 14:20:58 +0800307}
308
Cao jind90f3cc2016-07-29 19:05:38 +0800309/* Return the monotonic time elapsed in VM, i.e.,
Peter Maydell8212ff82016-09-15 10:24:22 +0100310 * the time between vm_start and vm_stop
311 */
Paolo Bonzini946fb272011-09-12 13:57:37 +0200312int64_t cpu_get_clock(void)
313{
314 int64_t ti;
Liu Ping Fancb365642013-09-25 14:20:58 +0800315 unsigned start;
316
317 do {
318 start = seqlock_read_begin(&timers_state.vm_clock_seqlock);
319 ti = cpu_get_clock_locked();
320 } while (seqlock_read_retry(&timers_state.vm_clock_seqlock, start));
321
322 return ti;
Paolo Bonzini946fb272011-09-12 13:57:37 +0200323}
324
Liu Ping Fancb365642013-09-25 14:20:58 +0800325/* enable cpu_get_ticks()
Cao jin3224e872016-07-08 18:31:37 +0800326 * Caller must hold BQL which serves as mutex for vm_clock_seqlock.
Liu Ping Fancb365642013-09-25 14:20:58 +0800327 */
Paolo Bonzini946fb272011-09-12 13:57:37 +0200328void cpu_enable_ticks(void)
329{
Liu Ping Fancb365642013-09-25 14:20:58 +0800330 /* Here, the really thing protected by seqlock is cpu_clock_offset. */
Emilio G. Cota03719e42016-06-08 14:55:21 -0400331 seqlock_write_begin(&timers_state.vm_clock_seqlock);
Paolo Bonzini946fb272011-09-12 13:57:37 +0200332 if (!timers_state.cpu_ticks_enabled) {
Christopher Covington4a7428c2015-09-25 10:42:21 -0400333 timers_state.cpu_ticks_offset -= cpu_get_host_ticks();
Paolo Bonzini946fb272011-09-12 13:57:37 +0200334 timers_state.cpu_clock_offset -= get_clock();
335 timers_state.cpu_ticks_enabled = 1;
336 }
Emilio G. Cota03719e42016-06-08 14:55:21 -0400337 seqlock_write_end(&timers_state.vm_clock_seqlock);
Paolo Bonzini946fb272011-09-12 13:57:37 +0200338}
339
340/* disable cpu_get_ticks() : the clock is stopped. You must not call
Liu Ping Fancb365642013-09-25 14:20:58 +0800341 * cpu_get_ticks() after that.
Cao jin3224e872016-07-08 18:31:37 +0800342 * Caller must hold BQL which serves as mutex for vm_clock_seqlock.
Liu Ping Fancb365642013-09-25 14:20:58 +0800343 */
Paolo Bonzini946fb272011-09-12 13:57:37 +0200344void cpu_disable_ticks(void)
345{
Liu Ping Fancb365642013-09-25 14:20:58 +0800346 /* Here, the really thing protected by seqlock is cpu_clock_offset. */
Emilio G. Cota03719e42016-06-08 14:55:21 -0400347 seqlock_write_begin(&timers_state.vm_clock_seqlock);
Paolo Bonzini946fb272011-09-12 13:57:37 +0200348 if (timers_state.cpu_ticks_enabled) {
Christopher Covington4a7428c2015-09-25 10:42:21 -0400349 timers_state.cpu_ticks_offset += cpu_get_host_ticks();
Liu Ping Fancb365642013-09-25 14:20:58 +0800350 timers_state.cpu_clock_offset = cpu_get_clock_locked();
Paolo Bonzini946fb272011-09-12 13:57:37 +0200351 timers_state.cpu_ticks_enabled = 0;
352 }
Emilio G. Cota03719e42016-06-08 14:55:21 -0400353 seqlock_write_end(&timers_state.vm_clock_seqlock);
Paolo Bonzini946fb272011-09-12 13:57:37 +0200354}
355
356/* Correlation between real and virtual time is always going to be
357 fairly approximate, so ignore small variation.
358 When the guest is idle real and virtual time will be aligned in
359 the IO wait loop. */
Rutuja Shah73bcb242016-03-21 21:32:30 +0530360#define ICOUNT_WOBBLE (NANOSECONDS_PER_SECOND / 10)
Paolo Bonzini946fb272011-09-12 13:57:37 +0200361
362static void icount_adjust(void)
363{
364 int64_t cur_time;
365 int64_t cur_icount;
366 int64_t delta;
Paolo Bonzinia3270e12013-10-07 17:18:15 +0200367
368 /* Protected by TimersState mutex. */
Paolo Bonzini946fb272011-09-12 13:57:37 +0200369 static int64_t last_delta;
Paolo Bonzini468cc7c2013-10-07 17:21:51 +0200370
Paolo Bonzini946fb272011-09-12 13:57:37 +0200371 /* If the VM is not running, then do nothing. */
372 if (!runstate_is_running()) {
373 return;
374 }
Paolo Bonzini468cc7c2013-10-07 17:21:51 +0200375
Emilio G. Cota03719e42016-06-08 14:55:21 -0400376 seqlock_write_begin(&timers_state.vm_clock_seqlock);
Paolo Bonzini17a15f12013-10-03 15:17:25 +0200377 cur_time = cpu_get_clock_locked();
378 cur_icount = cpu_get_icount_locked();
Paolo Bonzini468cc7c2013-10-07 17:21:51 +0200379
Paolo Bonzini946fb272011-09-12 13:57:37 +0200380 delta = cur_icount - cur_time;
381 /* FIXME: This is a very crude algorithm, somewhat prone to oscillation. */
382 if (delta > 0
383 && last_delta + ICOUNT_WOBBLE < delta * 2
384 && icount_time_shift > 0) {
385 /* The guest is getting too far ahead. Slow time down. */
386 icount_time_shift--;
387 }
388 if (delta < 0
389 && last_delta - ICOUNT_WOBBLE > delta * 2
390 && icount_time_shift < MAX_ICOUNT_SHIFT) {
391 /* The guest is getting too far behind. Speed time up. */
392 icount_time_shift++;
393 }
394 last_delta = delta;
KONRAD Fredericc96778b2014-08-01 01:37:09 +0200395 timers_state.qemu_icount_bias = cur_icount
396 - (timers_state.qemu_icount << icount_time_shift);
Emilio G. Cota03719e42016-06-08 14:55:21 -0400397 seqlock_write_end(&timers_state.vm_clock_seqlock);
Paolo Bonzini946fb272011-09-12 13:57:37 +0200398}
399
400static void icount_adjust_rt(void *opaque)
401{
Alex Bligh40daca52013-08-21 16:03:02 +0100402 timer_mod(icount_rt_timer,
Pavel Dovgalyuk1979b902015-01-12 15:00:43 +0300403 qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL_RT) + 1000);
Paolo Bonzini946fb272011-09-12 13:57:37 +0200404 icount_adjust();
405}
406
407static void icount_adjust_vm(void *opaque)
408{
Alex Bligh40daca52013-08-21 16:03:02 +0100409 timer_mod(icount_vm_timer,
410 qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) +
Rutuja Shah73bcb242016-03-21 21:32:30 +0530411 NANOSECONDS_PER_SECOND / 10);
Paolo Bonzini946fb272011-09-12 13:57:37 +0200412 icount_adjust();
413}
414
415static int64_t qemu_icount_round(int64_t count)
416{
417 return (count + (1 << icount_time_shift) - 1) >> icount_time_shift;
418}
419
Pavel Dovgalyukefab87c2015-09-17 19:24:39 +0300420static void icount_warp_rt(void)
Paolo Bonzini946fb272011-09-12 13:57:37 +0200421{
Alex Bennéeccffff42016-04-04 15:35:48 +0100422 unsigned seq;
423 int64_t warp_start;
424
Paolo Bonzini17a15f12013-10-03 15:17:25 +0200425 /* The icount_warp_timer is rescheduled soon after vm_clock_warp_start
426 * changes from -1 to another value, so the race here is okay.
427 */
Alex Bennéeccffff42016-04-04 15:35:48 +0100428 do {
429 seq = seqlock_read_begin(&timers_state.vm_clock_seqlock);
430 warp_start = vm_clock_warp_start;
431 } while (seqlock_read_retry(&timers_state.vm_clock_seqlock, seq));
432
433 if (warp_start == -1) {
Paolo Bonzini946fb272011-09-12 13:57:37 +0200434 return;
435 }
436
Emilio G. Cota03719e42016-06-08 14:55:21 -0400437 seqlock_write_begin(&timers_state.vm_clock_seqlock);
Paolo Bonzini946fb272011-09-12 13:57:37 +0200438 if (runstate_is_running()) {
Pavel Dovgalyuk8eda2062015-09-17 19:24:28 +0300439 int64_t clock = REPLAY_CLOCK(REPLAY_CLOCK_VIRTUAL_RT,
440 cpu_get_clock_locked());
Paolo Bonzini8ed961d2013-10-07 17:26:07 +0200441 int64_t warp_delta;
442
443 warp_delta = clock - vm_clock_warp_start;
444 if (use_icount == 2) {
Paolo Bonzini946fb272011-09-12 13:57:37 +0200445 /*
Alex Bligh40daca52013-08-21 16:03:02 +0100446 * In adaptive mode, do not let QEMU_CLOCK_VIRTUAL run too
Paolo Bonzini946fb272011-09-12 13:57:37 +0200447 * far ahead of real time.
448 */
Paolo Bonzini17a15f12013-10-03 15:17:25 +0200449 int64_t cur_icount = cpu_get_icount_locked();
Pavel Dovgalyukbf2a7dd2014-11-26 13:40:55 +0300450 int64_t delta = clock - cur_icount;
Paolo Bonzini8ed961d2013-10-07 17:26:07 +0200451 warp_delta = MIN(warp_delta, delta);
Paolo Bonzini946fb272011-09-12 13:57:37 +0200452 }
KONRAD Fredericc96778b2014-08-01 01:37:09 +0200453 timers_state.qemu_icount_bias += warp_delta;
Paolo Bonzini946fb272011-09-12 13:57:37 +0200454 }
455 vm_clock_warp_start = -1;
Emilio G. Cota03719e42016-06-08 14:55:21 -0400456 seqlock_write_end(&timers_state.vm_clock_seqlock);
Paolo Bonzini8ed961d2013-10-07 17:26:07 +0200457
458 if (qemu_clock_expired(QEMU_CLOCK_VIRTUAL)) {
459 qemu_clock_notify(QEMU_CLOCK_VIRTUAL);
460 }
Paolo Bonzini946fb272011-09-12 13:57:37 +0200461}
462
Pavel Dovgalyuke76d1792016-03-10 14:56:09 +0300463static void icount_timer_cb(void *opaque)
Pavel Dovgalyukefab87c2015-09-17 19:24:39 +0300464{
Pavel Dovgalyuke76d1792016-03-10 14:56:09 +0300465 /* No need for a checkpoint because the timer already synchronizes
466 * with CHECKPOINT_CLOCK_VIRTUAL_RT.
467 */
468 icount_warp_rt();
Pavel Dovgalyukefab87c2015-09-17 19:24:39 +0300469}
470
Paolo Bonzini8156be52012-03-28 15:42:04 +0200471void qtest_clock_warp(int64_t dest)
472{
Alex Bligh40daca52013-08-21 16:03:02 +0100473 int64_t clock = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
Fam Zhengefef88b2015-01-19 17:51:43 +0800474 AioContext *aio_context;
Paolo Bonzini8156be52012-03-28 15:42:04 +0200475 assert(qtest_enabled());
Fam Zhengefef88b2015-01-19 17:51:43 +0800476 aio_context = qemu_get_aio_context();
Paolo Bonzini8156be52012-03-28 15:42:04 +0200477 while (clock < dest) {
Alex Bligh40daca52013-08-21 16:03:02 +0100478 int64_t deadline = qemu_clock_deadline_ns_all(QEMU_CLOCK_VIRTUAL);
Sergey Fedorovc9299e22014-06-10 13:10:28 +0400479 int64_t warp = qemu_soonest_timeout(dest - clock, deadline);
Fam Zhengefef88b2015-01-19 17:51:43 +0800480
Emilio G. Cota03719e42016-06-08 14:55:21 -0400481 seqlock_write_begin(&timers_state.vm_clock_seqlock);
KONRAD Fredericc96778b2014-08-01 01:37:09 +0200482 timers_state.qemu_icount_bias += warp;
Emilio G. Cota03719e42016-06-08 14:55:21 -0400483 seqlock_write_end(&timers_state.vm_clock_seqlock);
Paolo Bonzini17a15f12013-10-03 15:17:25 +0200484
Alex Bligh40daca52013-08-21 16:03:02 +0100485 qemu_clock_run_timers(QEMU_CLOCK_VIRTUAL);
Fam Zhengefef88b2015-01-19 17:51:43 +0800486 timerlist_run_timers(aio_context->tlg.tl[QEMU_CLOCK_VIRTUAL]);
Alex Bligh40daca52013-08-21 16:03:02 +0100487 clock = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
Paolo Bonzini8156be52012-03-28 15:42:04 +0200488 }
Alex Bligh40daca52013-08-21 16:03:02 +0100489 qemu_clock_notify(QEMU_CLOCK_VIRTUAL);
Paolo Bonzini8156be52012-03-28 15:42:04 +0200490}
491
Pavel Dovgalyuke76d1792016-03-10 14:56:09 +0300492void qemu_start_warp_timer(void)
Paolo Bonzini946fb272011-09-12 13:57:37 +0200493{
Paolo Bonzinice78d182013-10-07 17:30:02 +0200494 int64_t clock;
Paolo Bonzini946fb272011-09-12 13:57:37 +0200495 int64_t deadline;
496
Pavel Dovgalyuke76d1792016-03-10 14:56:09 +0300497 if (!use_icount) {
Paolo Bonzini946fb272011-09-12 13:57:37 +0200498 return;
499 }
500
Pavel Dovgalyuk8bd7f712015-09-17 19:24:44 +0300501 /* Nothing to do if the VM is stopped: QEMU_CLOCK_VIRTUAL timers
502 * do not fire, so computing the deadline does not make sense.
503 */
504 if (!runstate_is_running()) {
505 return;
506 }
507
508 /* warp clock deterministically in record/replay mode */
Pavel Dovgalyuke76d1792016-03-10 14:56:09 +0300509 if (!replay_checkpoint(CHECKPOINT_CLOCK_WARP_START)) {
Pavel Dovgalyuk8bd7f712015-09-17 19:24:44 +0300510 return;
511 }
512
Paolo Bonzinice78d182013-10-07 17:30:02 +0200513 if (!all_cpu_threads_idle()) {
Paolo Bonzini946fb272011-09-12 13:57:37 +0200514 return;
515 }
516
Paolo Bonzini8156be52012-03-28 15:42:04 +0200517 if (qtest_enabled()) {
518 /* When testing, qtest commands advance icount. */
Pavel Dovgalyuke76d1792016-03-10 14:56:09 +0300519 return;
Paolo Bonzini8156be52012-03-28 15:42:04 +0200520 }
521
Alex Blighac70aaf2013-08-21 16:02:57 +0100522 /* We want to use the earliest deadline from ALL vm_clocks */
Pavel Dovgalyukbf2a7dd2014-11-26 13:40:55 +0300523 clock = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL_RT);
Alex Bligh40daca52013-08-21 16:03:02 +0100524 deadline = qemu_clock_deadline_ns_all(QEMU_CLOCK_VIRTUAL);
Paolo Bonzinice78d182013-10-07 17:30:02 +0200525 if (deadline < 0) {
Victor CLEMENTd7a0f712015-05-29 17:14:06 +0200526 static bool notified;
527 if (!icount_sleep && !notified) {
528 error_report("WARNING: icount sleep disabled and no active timers");
529 notified = true;
530 }
Paolo Bonzinice78d182013-10-07 17:30:02 +0200531 return;
Alex Blighac70aaf2013-08-21 16:02:57 +0100532 }
533
Paolo Bonzini946fb272011-09-12 13:57:37 +0200534 if (deadline > 0) {
535 /*
Alex Bligh40daca52013-08-21 16:03:02 +0100536 * Ensure QEMU_CLOCK_VIRTUAL proceeds even when the virtual CPU goes to
Paolo Bonzini946fb272011-09-12 13:57:37 +0200537 * sleep. Otherwise, the CPU might be waiting for a future timer
538 * interrupt to wake it up, but the interrupt never comes because
539 * the vCPU isn't running any insns and thus doesn't advance the
Alex Bligh40daca52013-08-21 16:03:02 +0100540 * QEMU_CLOCK_VIRTUAL.
Paolo Bonzini946fb272011-09-12 13:57:37 +0200541 */
Victor CLEMENT5045e9d92015-05-29 17:14:04 +0200542 if (!icount_sleep) {
543 /*
544 * We never let VCPUs sleep in no sleep icount mode.
545 * If there is a pending QEMU_CLOCK_VIRTUAL timer we just advance
546 * to the next QEMU_CLOCK_VIRTUAL event and notify it.
547 * It is useful when we want a deterministic execution time,
548 * isolated from host latencies.
549 */
Emilio G. Cota03719e42016-06-08 14:55:21 -0400550 seqlock_write_begin(&timers_state.vm_clock_seqlock);
Victor CLEMENT5045e9d92015-05-29 17:14:04 +0200551 timers_state.qemu_icount_bias += deadline;
Emilio G. Cota03719e42016-06-08 14:55:21 -0400552 seqlock_write_end(&timers_state.vm_clock_seqlock);
Victor CLEMENT5045e9d92015-05-29 17:14:04 +0200553 qemu_clock_notify(QEMU_CLOCK_VIRTUAL);
554 } else {
555 /*
556 * We do stop VCPUs and only advance QEMU_CLOCK_VIRTUAL after some
557 * "real" time, (related to the time left until the next event) has
558 * passed. The QEMU_CLOCK_VIRTUAL_RT clock will do this.
559 * This avoids that the warps are visible externally; for example,
560 * you will not be sending network packets continuously instead of
561 * every 100ms.
562 */
Emilio G. Cota03719e42016-06-08 14:55:21 -0400563 seqlock_write_begin(&timers_state.vm_clock_seqlock);
Victor CLEMENT5045e9d92015-05-29 17:14:04 +0200564 if (vm_clock_warp_start == -1 || vm_clock_warp_start > clock) {
565 vm_clock_warp_start = clock;
566 }
Emilio G. Cota03719e42016-06-08 14:55:21 -0400567 seqlock_write_end(&timers_state.vm_clock_seqlock);
Victor CLEMENT5045e9d92015-05-29 17:14:04 +0200568 timer_mod_anticipate(icount_warp_timer, clock + deadline);
Paolo Bonzinice78d182013-10-07 17:30:02 +0200569 }
Alex Blighac70aaf2013-08-21 16:02:57 +0100570 } else if (deadline == 0) {
Alex Bligh40daca52013-08-21 16:03:02 +0100571 qemu_clock_notify(QEMU_CLOCK_VIRTUAL);
Paolo Bonzini946fb272011-09-12 13:57:37 +0200572 }
573}
574
Pavel Dovgalyuke76d1792016-03-10 14:56:09 +0300575static void qemu_account_warp_timer(void)
576{
577 if (!use_icount || !icount_sleep) {
578 return;
579 }
580
581 /* Nothing to do if the VM is stopped: QEMU_CLOCK_VIRTUAL timers
582 * do not fire, so computing the deadline does not make sense.
583 */
584 if (!runstate_is_running()) {
585 return;
586 }
587
588 /* warp clock deterministically in record/replay mode */
589 if (!replay_checkpoint(CHECKPOINT_CLOCK_WARP_ACCOUNT)) {
590 return;
591 }
592
593 timer_del(icount_warp_timer);
594 icount_warp_rt();
595}
596
KONRAD Fredericd09eae32014-08-01 01:37:10 +0200597static bool icount_state_needed(void *opaque)
598{
599 return use_icount;
600}
601
602/*
603 * This is a subsection for icount migration.
604 */
605static const VMStateDescription icount_vmstate_timers = {
606 .name = "timer/icount",
607 .version_id = 1,
608 .minimum_version_id = 1,
Juan Quintela5cd8cad2014-09-23 14:09:54 +0200609 .needed = icount_state_needed,
KONRAD Fredericd09eae32014-08-01 01:37:10 +0200610 .fields = (VMStateField[]) {
611 VMSTATE_INT64(qemu_icount_bias, TimersState),
612 VMSTATE_INT64(qemu_icount, TimersState),
613 VMSTATE_END_OF_LIST()
614 }
615};
616
Paolo Bonzini946fb272011-09-12 13:57:37 +0200617static const VMStateDescription vmstate_timers = {
618 .name = "timer",
619 .version_id = 2,
620 .minimum_version_id = 1,
Juan Quintela35d08452014-04-16 16:01:33 +0200621 .fields = (VMStateField[]) {
Paolo Bonzini946fb272011-09-12 13:57:37 +0200622 VMSTATE_INT64(cpu_ticks_offset, TimersState),
623 VMSTATE_INT64(dummy, TimersState),
624 VMSTATE_INT64_V(cpu_clock_offset, TimersState, 2),
625 VMSTATE_END_OF_LIST()
KONRAD Fredericd09eae32014-08-01 01:37:10 +0200626 },
Juan Quintela5cd8cad2014-09-23 14:09:54 +0200627 .subsections = (const VMStateDescription*[]) {
628 &icount_vmstate_timers,
629 NULL
Paolo Bonzini946fb272011-09-12 13:57:37 +0200630 }
631};
632
Paolo Bonzini14e6fe12016-10-31 10:36:08 +0100633static void cpu_throttle_thread(CPUState *cpu, run_on_cpu_data opaque)
Jason J. Herne2adcc852015-09-08 13:12:33 -0400634{
Jason J. Herne2adcc852015-09-08 13:12:33 -0400635 double pct;
636 double throttle_ratio;
637 long sleeptime_ns;
638
639 if (!cpu_throttle_get_percentage()) {
640 return;
641 }
642
643 pct = (double)cpu_throttle_get_percentage()/100;
644 throttle_ratio = pct / (1 - pct);
645 sleeptime_ns = (long)(throttle_ratio * CPU_THROTTLE_TIMESLICE_NS);
646
647 qemu_mutex_unlock_iothread();
648 atomic_set(&cpu->throttle_thread_scheduled, 0);
649 g_usleep(sleeptime_ns / 1000); /* Convert ns to us for usleep call */
650 qemu_mutex_lock_iothread();
651}
652
653static void cpu_throttle_timer_tick(void *opaque)
654{
655 CPUState *cpu;
656 double pct;
657
658 /* Stop the timer if needed */
659 if (!cpu_throttle_get_percentage()) {
660 return;
661 }
662 CPU_FOREACH(cpu) {
663 if (!atomic_xchg(&cpu->throttle_thread_scheduled, 1)) {
Paolo Bonzini14e6fe12016-10-31 10:36:08 +0100664 async_run_on_cpu(cpu, cpu_throttle_thread,
665 RUN_ON_CPU_NULL);
Jason J. Herne2adcc852015-09-08 13:12:33 -0400666 }
667 }
668
669 pct = (double)cpu_throttle_get_percentage()/100;
670 timer_mod(throttle_timer, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL_RT) +
671 CPU_THROTTLE_TIMESLICE_NS / (1-pct));
672}
673
674void cpu_throttle_set(int new_throttle_pct)
675{
676 /* Ensure throttle percentage is within valid range */
677 new_throttle_pct = MIN(new_throttle_pct, CPU_THROTTLE_PCT_MAX);
678 new_throttle_pct = MAX(new_throttle_pct, CPU_THROTTLE_PCT_MIN);
679
680 atomic_set(&throttle_percentage, new_throttle_pct);
681
682 timer_mod(throttle_timer, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL_RT) +
683 CPU_THROTTLE_TIMESLICE_NS);
684}
685
686void cpu_throttle_stop(void)
687{
688 atomic_set(&throttle_percentage, 0);
689}
690
691bool cpu_throttle_active(void)
692{
693 return (cpu_throttle_get_percentage() != 0);
694}
695
696int cpu_throttle_get_percentage(void)
697{
698 return atomic_read(&throttle_percentage);
699}
700
Pavel Dovgalyuk4603ea02014-09-01 09:34:49 +0400701void cpu_ticks_init(void)
702{
Emilio G. Cotaccdb3c12016-06-08 14:55:20 -0400703 seqlock_init(&timers_state.vm_clock_seqlock);
Pavel Dovgalyuk4603ea02014-09-01 09:34:49 +0400704 vmstate_register(NULL, 0, &vmstate_timers, &timers_state);
Jason J. Herne2adcc852015-09-08 13:12:33 -0400705 throttle_timer = timer_new_ns(QEMU_CLOCK_VIRTUAL_RT,
706 cpu_throttle_timer_tick, NULL);
Pavel Dovgalyuk4603ea02014-09-01 09:34:49 +0400707}
708
Sebastian Tanase1ad95802014-07-25 11:56:28 +0200709void configure_icount(QemuOpts *opts, Error **errp)
Paolo Bonzini946fb272011-09-12 13:57:37 +0200710{
Sebastian Tanase1ad95802014-07-25 11:56:28 +0200711 const char *option;
Sebastian Tanasea8bfac32014-07-25 11:56:29 +0200712 char *rem_str = NULL;
Sebastian Tanase1ad95802014-07-25 11:56:28 +0200713
Sebastian Tanase1ad95802014-07-25 11:56:28 +0200714 option = qemu_opt_get(opts, "shift");
Paolo Bonzini946fb272011-09-12 13:57:37 +0200715 if (!option) {
Sebastian Tanasea8bfac32014-07-25 11:56:29 +0200716 if (qemu_opt_get(opts, "align") != NULL) {
717 error_setg(errp, "Please specify shift option when using align");
718 }
Paolo Bonzini946fb272011-09-12 13:57:37 +0200719 return;
720 }
Victor CLEMENTf1f4b572015-05-29 17:14:05 +0200721
722 icount_sleep = qemu_opt_get_bool(opts, "sleep", true);
Victor CLEMENT5045e9d92015-05-29 17:14:04 +0200723 if (icount_sleep) {
724 icount_warp_timer = timer_new_ns(QEMU_CLOCK_VIRTUAL_RT,
Pavel Dovgalyuke76d1792016-03-10 14:56:09 +0300725 icount_timer_cb, NULL);
Victor CLEMENT5045e9d92015-05-29 17:14:04 +0200726 }
Victor CLEMENTf1f4b572015-05-29 17:14:05 +0200727
Sebastian Tanasea8bfac32014-07-25 11:56:29 +0200728 icount_align_option = qemu_opt_get_bool(opts, "align", false);
Victor CLEMENTf1f4b572015-05-29 17:14:05 +0200729
730 if (icount_align_option && !icount_sleep) {
Pranith Kumar778d9f92016-02-26 10:16:51 -0500731 error_setg(errp, "align=on and sleep=off are incompatible");
Victor CLEMENTf1f4b572015-05-29 17:14:05 +0200732 }
Paolo Bonzini946fb272011-09-12 13:57:37 +0200733 if (strcmp(option, "auto") != 0) {
Sebastian Tanasea8bfac32014-07-25 11:56:29 +0200734 errno = 0;
735 icount_time_shift = strtol(option, &rem_str, 0);
736 if (errno != 0 || *rem_str != '\0' || !strlen(option)) {
737 error_setg(errp, "icount: Invalid shift value");
738 }
Paolo Bonzini946fb272011-09-12 13:57:37 +0200739 use_icount = 1;
740 return;
Sebastian Tanasea8bfac32014-07-25 11:56:29 +0200741 } else if (icount_align_option) {
742 error_setg(errp, "shift=auto and align=on are incompatible");
Victor CLEMENTf1f4b572015-05-29 17:14:05 +0200743 } else if (!icount_sleep) {
Pranith Kumar778d9f92016-02-26 10:16:51 -0500744 error_setg(errp, "shift=auto and sleep=off are incompatible");
Paolo Bonzini946fb272011-09-12 13:57:37 +0200745 }
746
747 use_icount = 2;
748
749 /* 125MIPS seems a reasonable initial guess at the guest speed.
750 It will be corrected fairly quickly anyway. */
751 icount_time_shift = 3;
752
753 /* Have both realtime and virtual time triggers for speed adjustment.
754 The realtime trigger catches emulated time passing too slowly,
755 the virtual time trigger catches emulated time passing too fast.
756 Realtime triggers occur even when idle, so use them less frequently
757 than VM triggers. */
Pavel Dovgalyukbf2a7dd2014-11-26 13:40:55 +0300758 icount_rt_timer = timer_new_ms(QEMU_CLOCK_VIRTUAL_RT,
759 icount_adjust_rt, NULL);
Alex Bligh40daca52013-08-21 16:03:02 +0100760 timer_mod(icount_rt_timer,
Pavel Dovgalyukbf2a7dd2014-11-26 13:40:55 +0300761 qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL_RT) + 1000);
Alex Bligh40daca52013-08-21 16:03:02 +0100762 icount_vm_timer = timer_new_ns(QEMU_CLOCK_VIRTUAL,
763 icount_adjust_vm, NULL);
764 timer_mod(icount_vm_timer,
765 qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) +
Rutuja Shah73bcb242016-03-21 21:32:30 +0530766 NANOSECONDS_PER_SECOND / 10);
Paolo Bonzini946fb272011-09-12 13:57:37 +0200767}
768
769/***********************************************************/
Alex Bennée65467062017-02-23 18:29:09 +0000770/* TCG vCPU kick timer
771 *
772 * The kick timer is responsible for moving single threaded vCPU
773 * emulation on to the next vCPU. If more than one vCPU is running a
774 * timer event with force a cpu->exit so the next vCPU can get
775 * scheduled.
776 *
777 * The timer is removed if all vCPUs are idle and restarted again once
778 * idleness is complete.
779 */
780
781static QEMUTimer *tcg_kick_vcpu_timer;
Alex Bennée791158d2017-02-23 18:29:10 +0000782static CPUState *tcg_current_rr_cpu;
Alex Bennée65467062017-02-23 18:29:09 +0000783
784#define TCG_KICK_PERIOD (NANOSECONDS_PER_SECOND / 10)
785
786static inline int64_t qemu_tcg_next_kick(void)
787{
788 return qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) + TCG_KICK_PERIOD;
789}
790
Alex Bennée791158d2017-02-23 18:29:10 +0000791/* Kick the currently round-robin scheduled vCPU */
792static void qemu_cpu_kick_rr_cpu(void)
793{
794 CPUState *cpu;
Alex Bennée791158d2017-02-23 18:29:10 +0000795 do {
796 cpu = atomic_mb_read(&tcg_current_rr_cpu);
797 if (cpu) {
798 cpu_exit(cpu);
799 }
800 } while (cpu != atomic_mb_read(&tcg_current_rr_cpu));
801}
802
Alex Bennée65467062017-02-23 18:29:09 +0000803static void kick_tcg_thread(void *opaque)
804{
805 timer_mod(tcg_kick_vcpu_timer, qemu_tcg_next_kick());
Alex Bennée791158d2017-02-23 18:29:10 +0000806 qemu_cpu_kick_rr_cpu();
Alex Bennée65467062017-02-23 18:29:09 +0000807}
808
809static void start_tcg_kick_timer(void)
810{
Alex Bennée37257942017-02-23 18:29:14 +0000811 if (!mttcg_enabled && !tcg_kick_vcpu_timer && CPU_NEXT(first_cpu)) {
Alex Bennée65467062017-02-23 18:29:09 +0000812 tcg_kick_vcpu_timer = timer_new_ns(QEMU_CLOCK_VIRTUAL,
813 kick_tcg_thread, NULL);
814 timer_mod(tcg_kick_vcpu_timer, qemu_tcg_next_kick());
815 }
816}
817
818static void stop_tcg_kick_timer(void)
819{
820 if (tcg_kick_vcpu_timer) {
821 timer_del(tcg_kick_vcpu_timer);
822 tcg_kick_vcpu_timer = NULL;
823 }
824}
825
Alex Bennée65467062017-02-23 18:29:09 +0000826/***********************************************************/
Blue Swirl296af7c2010-03-29 19:23:50 +0000827void hw_error(const char *fmt, ...)
828{
829 va_list ap;
Andreas Färber55e5c282012-12-17 06:18:02 +0100830 CPUState *cpu;
Blue Swirl296af7c2010-03-29 19:23:50 +0000831
832 va_start(ap, fmt);
833 fprintf(stderr, "qemu: hardware error: ");
834 vfprintf(stderr, fmt, ap);
835 fprintf(stderr, "\n");
Andreas Färberbdc44642013-06-24 23:50:24 +0200836 CPU_FOREACH(cpu) {
Andreas Färber55e5c282012-12-17 06:18:02 +0100837 fprintf(stderr, "CPU #%d:\n", cpu->cpu_index);
Andreas Färber878096e2013-05-27 01:33:50 +0200838 cpu_dump_state(cpu, stderr, fprintf, CPU_DUMP_FPU);
Blue Swirl296af7c2010-03-29 19:23:50 +0000839 }
840 va_end(ap);
841 abort();
842}
843
844void cpu_synchronize_all_states(void)
845{
Andreas Färber182735e2013-05-29 22:29:20 +0200846 CPUState *cpu;
Blue Swirl296af7c2010-03-29 19:23:50 +0000847
Andreas Färberbdc44642013-06-24 23:50:24 +0200848 CPU_FOREACH(cpu) {
Andreas Färber182735e2013-05-29 22:29:20 +0200849 cpu_synchronize_state(cpu);
Blue Swirl296af7c2010-03-29 19:23:50 +0000850 }
851}
852
853void cpu_synchronize_all_post_reset(void)
854{
Andreas Färber182735e2013-05-29 22:29:20 +0200855 CPUState *cpu;
Blue Swirl296af7c2010-03-29 19:23:50 +0000856
Andreas Färberbdc44642013-06-24 23:50:24 +0200857 CPU_FOREACH(cpu) {
Andreas Färber182735e2013-05-29 22:29:20 +0200858 cpu_synchronize_post_reset(cpu);
Blue Swirl296af7c2010-03-29 19:23:50 +0000859 }
860}
861
862void cpu_synchronize_all_post_init(void)
863{
Andreas Färber182735e2013-05-29 22:29:20 +0200864 CPUState *cpu;
Blue Swirl296af7c2010-03-29 19:23:50 +0000865
Andreas Färberbdc44642013-06-24 23:50:24 +0200866 CPU_FOREACH(cpu) {
Andreas Färber182735e2013-05-29 22:29:20 +0200867 cpu_synchronize_post_init(cpu);
Blue Swirl296af7c2010-03-29 19:23:50 +0000868 }
869}
870
Kevin Wolf56983462013-07-05 13:49:54 +0200871static int do_vm_stop(RunState state)
Blue Swirl296af7c2010-03-29 19:23:50 +0000872{
Kevin Wolf56983462013-07-05 13:49:54 +0200873 int ret = 0;
874
Luiz Capitulino13548692011-07-29 15:36:43 -0300875 if (runstate_is_running()) {
Blue Swirl296af7c2010-03-29 19:23:50 +0000876 cpu_disable_ticks();
Blue Swirl296af7c2010-03-29 19:23:50 +0000877 pause_all_vcpus();
Luiz Capitulinof5bbfba2011-07-29 15:04:45 -0300878 runstate_set(state);
Luiz Capitulino1dfb4dd2011-07-29 14:26:33 -0300879 vm_state_notify(0, state);
Wenchao Xiaa4e15de2014-06-18 08:43:36 +0200880 qapi_event_send_stop(&error_abort);
Blue Swirl296af7c2010-03-29 19:23:50 +0000881 }
Kevin Wolf56983462013-07-05 13:49:54 +0200882
Kevin Wolf594a45c2013-07-18 14:52:19 +0200883 bdrv_drain_all();
Pavel Dovgalyuk6d0ceb82016-09-26 11:08:16 +0300884 replay_disable_events();
John Snow22af08e2016-09-22 21:45:51 -0400885 ret = bdrv_flush_all();
Kevin Wolf594a45c2013-07-18 14:52:19 +0200886
Kevin Wolf56983462013-07-05 13:49:54 +0200887 return ret;
Blue Swirl296af7c2010-03-29 19:23:50 +0000888}
889
Andreas Färbera1fcaa72012-05-02 23:42:26 +0200890static bool cpu_can_run(CPUState *cpu)
Blue Swirl296af7c2010-03-29 19:23:50 +0000891{
Andreas Färber4fdeee72012-05-02 23:10:09 +0200892 if (cpu->stop) {
Andreas Färbera1fcaa72012-05-02 23:42:26 +0200893 return false;
Jan Kiszka0ab07c62011-02-07 12:19:14 +0100894 }
Tiejun Chen321bc0b2013-08-02 09:43:09 +0800895 if (cpu_is_stopped(cpu)) {
Andreas Färbera1fcaa72012-05-02 23:42:26 +0200896 return false;
Jan Kiszka0ab07c62011-02-07 12:19:14 +0100897 }
Andreas Färbera1fcaa72012-05-02 23:42:26 +0200898 return true;
Blue Swirl296af7c2010-03-29 19:23:50 +0000899}
900
Andreas Färber91325042013-05-27 02:07:49 +0200901static void cpu_handle_guest_debug(CPUState *cpu)
Jan Kiszka3c638d02010-06-25 16:56:56 +0200902{
Andreas Färber64f6b342013-05-27 02:06:09 +0200903 gdb_set_stop_cpu(cpu);
Jan Kiszka8cf71712011-02-07 12:19:16 +0100904 qemu_system_debug_request();
Andreas Färberf324e762012-05-02 23:26:21 +0200905 cpu->stopped = true;
Jan Kiszka3c638d02010-06-25 16:56:56 +0200906}
907
Jan Kiszka6d9cb732011-02-01 22:15:58 +0100908#ifdef CONFIG_LINUX
909static void sigbus_reraise(void)
910{
911 sigset_t set;
912 struct sigaction action;
913
914 memset(&action, 0, sizeof(action));
915 action.sa_handler = SIG_DFL;
916 if (!sigaction(SIGBUS, &action, NULL)) {
917 raise(SIGBUS);
918 sigemptyset(&set);
919 sigaddset(&set, SIGBUS);
Peter Maydella2d17612016-05-16 18:33:59 +0100920 pthread_sigmask(SIG_UNBLOCK, &set, NULL);
Jan Kiszka6d9cb732011-02-01 22:15:58 +0100921 }
922 perror("Failed to re-raise SIGBUS!\n");
923 abort();
924}
925
Paolo Bonzinid98d4072017-02-08 13:22:12 +0100926static void sigbus_handler(int n, siginfo_t *siginfo, void *ctx)
Jan Kiszka6d9cb732011-02-01 22:15:58 +0100927{
Paolo Bonzinia16fc072017-02-09 09:50:02 +0100928 if (siginfo->si_code != BUS_MCEERR_AO && siginfo->si_code != BUS_MCEERR_AR) {
929 sigbus_reraise();
930 }
931
Paolo Bonzini2ae41db2017-02-08 12:48:54 +0100932 if (current_cpu) {
933 /* Called asynchronously in VCPU thread. */
934 if (kvm_on_sigbus_vcpu(current_cpu, siginfo->si_code, siginfo->si_addr)) {
935 sigbus_reraise();
936 }
937 } else {
938 /* Called synchronously (via signalfd) in main thread. */
939 if (kvm_on_sigbus(siginfo->si_code, siginfo->si_addr)) {
940 sigbus_reraise();
941 }
Jan Kiszka6d9cb732011-02-01 22:15:58 +0100942 }
943}
944
945static void qemu_init_sigbus(void)
946{
947 struct sigaction action;
948
949 memset(&action, 0, sizeof(action));
950 action.sa_flags = SA_SIGINFO;
Paolo Bonzinid98d4072017-02-08 13:22:12 +0100951 action.sa_sigaction = sigbus_handler;
Jan Kiszka6d9cb732011-02-01 22:15:58 +0100952 sigaction(SIGBUS, &action, NULL);
953
954 prctl(PR_MCE_KILL, PR_MCE_KILL_SET, PR_MCE_KILL_EARLY, 0, 0);
955}
Paolo Bonzinia16fc072017-02-09 09:50:02 +0100956#else /* !CONFIG_LINUX */
957static void qemu_init_sigbus(void)
958{
959}
Paolo Bonzinia16fc072017-02-09 09:50:02 +0100960#endif /* !CONFIG_LINUX */
Blue Swirl296af7c2010-03-29 19:23:50 +0000961
Stefan Weilb2532d82012-09-27 07:41:42 +0200962static QemuMutex qemu_global_mutex;
Blue Swirl296af7c2010-03-29 19:23:50 +0000963
964static QemuThread io_thread;
965
Blue Swirl296af7c2010-03-29 19:23:50 +0000966/* cpu creation */
967static QemuCond qemu_cpu_cond;
968/* system init */
Blue Swirl296af7c2010-03-29 19:23:50 +0000969static QemuCond qemu_pause_cond;
970
Paolo Bonzinid3b12f52011-09-13 10:30:52 +0200971void qemu_init_cpu_loop(void)
Blue Swirl296af7c2010-03-29 19:23:50 +0000972{
Jan Kiszka6d9cb732011-02-01 22:15:58 +0100973 qemu_init_sigbus();
Anthony Liguoried945922011-02-08 18:18:18 +0100974 qemu_cond_init(&qemu_cpu_cond);
Anthony Liguoried945922011-02-08 18:18:18 +0100975 qemu_cond_init(&qemu_pause_cond);
Blue Swirl296af7c2010-03-29 19:23:50 +0000976 qemu_mutex_init(&qemu_global_mutex);
Blue Swirl296af7c2010-03-29 19:23:50 +0000977
Jan Kiszkab7680cb2011-03-12 17:43:51 +0100978 qemu_thread_get_self(&io_thread);
Blue Swirl296af7c2010-03-29 19:23:50 +0000979}
980
Paolo Bonzini14e6fe12016-10-31 10:36:08 +0100981void run_on_cpu(CPUState *cpu, run_on_cpu_func func, run_on_cpu_data data)
Marcelo Tosattie82bcec2010-05-04 09:45:22 -0300982{
Sergey Fedorovd148d902016-08-29 09:51:00 +0200983 do_run_on_cpu(cpu, func, data, &qemu_global_mutex);
Chegu Vinod3c022702013-06-24 03:49:41 -0600984}
985
Gu Zheng4c055ab2016-05-12 09:18:13 +0530986static void qemu_kvm_destroy_vcpu(CPUState *cpu)
987{
988 if (kvm_destroy_vcpu(cpu) < 0) {
989 error_report("kvm_destroy_vcpu failed");
990 exit(EXIT_FAILURE);
991 }
992}
993
994static void qemu_tcg_destroy_vcpu(CPUState *cpu)
995{
996}
997
Andreas Färber509a0d72012-05-03 02:18:09 +0200998static void qemu_wait_io_event_common(CPUState *cpu)
Blue Swirl296af7c2010-03-29 19:23:50 +0000999{
Alex Bennée37257942017-02-23 18:29:14 +00001000 atomic_mb_set(&cpu->thread_kicked, false);
Andreas Färber4fdeee72012-05-02 23:10:09 +02001001 if (cpu->stop) {
1002 cpu->stop = false;
Andreas Färberf324e762012-05-02 23:26:21 +02001003 cpu->stopped = true;
Dr. David Alan Gilbert96bce682016-01-25 10:08:18 +00001004 qemu_cond_broadcast(&qemu_pause_cond);
Blue Swirl296af7c2010-03-29 19:23:50 +00001005 }
Sergey Fedorova5403c62016-08-02 18:27:36 +01001006 process_queued_cpu_work(cpu);
Alex Bennée37257942017-02-23 18:29:14 +00001007}
1008
1009static bool qemu_tcg_should_sleep(CPUState *cpu)
1010{
1011 if (mttcg_enabled) {
1012 return cpu_thread_is_idle(cpu);
1013 } else {
1014 return all_cpu_threads_idle();
1015 }
Blue Swirl296af7c2010-03-29 19:23:50 +00001016}
1017
KONRAD Fredericd5f8d612015-08-10 17:27:06 +02001018static void qemu_tcg_wait_io_event(CPUState *cpu)
Blue Swirl296af7c2010-03-29 19:23:50 +00001019{
Alex Bennée37257942017-02-23 18:29:14 +00001020 while (qemu_tcg_should_sleep(cpu)) {
Alex Bennée65467062017-02-23 18:29:09 +00001021 stop_tcg_kick_timer();
KONRAD Fredericd5f8d612015-08-10 17:27:06 +02001022 qemu_cond_wait(cpu->halt_cond, &qemu_global_mutex);
Jan Kiszka16400322011-02-09 16:29:37 +01001023 }
Blue Swirl296af7c2010-03-29 19:23:50 +00001024
Alex Bennée65467062017-02-23 18:29:09 +00001025 start_tcg_kick_timer();
1026
Alex Bennée37257942017-02-23 18:29:14 +00001027 qemu_wait_io_event_common(cpu);
Blue Swirl296af7c2010-03-29 19:23:50 +00001028}
1029
Andreas Färberfd529e82013-05-26 23:24:55 +02001030static void qemu_kvm_wait_io_event(CPUState *cpu)
Blue Swirl296af7c2010-03-29 19:23:50 +00001031{
Andreas Färbera98ae1d2013-05-26 23:21:08 +02001032 while (cpu_thread_is_idle(cpu)) {
Andreas Färberf5c121b2012-05-03 01:22:49 +02001033 qemu_cond_wait(cpu->halt_cond, &qemu_global_mutex);
Jan Kiszka16400322011-02-09 16:29:37 +01001034 }
Blue Swirl296af7c2010-03-29 19:23:50 +00001035
Andreas Färber509a0d72012-05-03 02:18:09 +02001036 qemu_wait_io_event_common(cpu);
Blue Swirl296af7c2010-03-29 19:23:50 +00001037}
1038
Jan Kiszka7e97cd82011-02-07 12:19:12 +01001039static void *qemu_kvm_cpu_thread_fn(void *arg)
Blue Swirl296af7c2010-03-29 19:23:50 +00001040{
Andreas Färber48a106b2013-05-27 02:20:39 +02001041 CPUState *cpu = arg;
Jan Kiszka84b49152011-02-01 22:15:50 +01001042 int r;
Blue Swirl296af7c2010-03-29 19:23:50 +00001043
Paolo Bonziniab28bd22015-07-09 08:55:38 +02001044 rcu_register_thread();
1045
Paolo Bonzini2e7f7a32015-06-18 18:47:18 +02001046 qemu_mutex_lock_iothread();
Andreas Färber814e6122012-05-02 17:00:37 +02001047 qemu_thread_get_self(cpu->thread);
Andreas Färber9f09e182012-05-03 06:59:07 +02001048 cpu->thread_id = qemu_get_thread_id();
Pavel Dovgalyuk626cf8f2014-12-08 10:53:17 +03001049 cpu->can_do_io = 1;
Andreas Färber4917cf42013-05-27 05:17:50 +02001050 current_cpu = cpu;
Blue Swirl296af7c2010-03-29 19:23:50 +00001051
Andreas Färber504134d2012-12-17 06:38:45 +01001052 r = kvm_init_vcpu(cpu);
Jan Kiszka84b49152011-02-01 22:15:50 +01001053 if (r < 0) {
1054 fprintf(stderr, "kvm_init_vcpu failed: %s\n", strerror(-r));
1055 exit(1);
1056 }
Blue Swirl296af7c2010-03-29 19:23:50 +00001057
Paolo Bonzini18268b62017-02-09 09:41:14 +01001058 kvm_init_cpu_signals(cpu);
Blue Swirl296af7c2010-03-29 19:23:50 +00001059
1060 /* signal CPU creation */
Andreas Färber61a46212012-05-02 22:49:36 +02001061 cpu->created = true;
Blue Swirl296af7c2010-03-29 19:23:50 +00001062 qemu_cond_signal(&qemu_cpu_cond);
1063
Gu Zheng4c055ab2016-05-12 09:18:13 +05301064 do {
Andreas Färbera1fcaa72012-05-02 23:42:26 +02001065 if (cpu_can_run(cpu)) {
Andreas Färber1458c362013-05-26 23:46:55 +02001066 r = kvm_cpu_exec(cpu);
Jan Kiszka83f338f2011-02-07 12:19:17 +01001067 if (r == EXCP_DEBUG) {
Andreas Färber91325042013-05-27 02:07:49 +02001068 cpu_handle_guest_debug(cpu);
Jan Kiszka83f338f2011-02-07 12:19:17 +01001069 }
Jan Kiszka0ab07c62011-02-07 12:19:14 +01001070 }
Andreas Färberfd529e82013-05-26 23:24:55 +02001071 qemu_kvm_wait_io_event(cpu);
Gu Zheng4c055ab2016-05-12 09:18:13 +05301072 } while (!cpu->unplug || cpu_can_run(cpu));
Blue Swirl296af7c2010-03-29 19:23:50 +00001073
Gu Zheng4c055ab2016-05-12 09:18:13 +05301074 qemu_kvm_destroy_vcpu(cpu);
Bharata B Rao2c579042016-05-12 09:18:14 +05301075 cpu->created = false;
1076 qemu_cond_signal(&qemu_cpu_cond);
Gu Zheng4c055ab2016-05-12 09:18:13 +05301077 qemu_mutex_unlock_iothread();
Blue Swirl296af7c2010-03-29 19:23:50 +00001078 return NULL;
1079}
1080
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +02001081static void *qemu_dummy_cpu_thread_fn(void *arg)
1082{
1083#ifdef _WIN32
1084 fprintf(stderr, "qtest is not supported under Windows\n");
1085 exit(1);
1086#else
Andreas Färber10a90212013-05-27 02:24:35 +02001087 CPUState *cpu = arg;
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +02001088 sigset_t waitset;
1089 int r;
1090
Paolo Bonziniab28bd22015-07-09 08:55:38 +02001091 rcu_register_thread();
1092
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +02001093 qemu_mutex_lock_iothread();
Andreas Färber814e6122012-05-02 17:00:37 +02001094 qemu_thread_get_self(cpu->thread);
Andreas Färber9f09e182012-05-03 06:59:07 +02001095 cpu->thread_id = qemu_get_thread_id();
Pavel Dovgalyuk626cf8f2014-12-08 10:53:17 +03001096 cpu->can_do_io = 1;
Alex Bennée37257942017-02-23 18:29:14 +00001097 current_cpu = cpu;
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +02001098
1099 sigemptyset(&waitset);
1100 sigaddset(&waitset, SIG_IPI);
1101
1102 /* signal CPU creation */
Andreas Färber61a46212012-05-02 22:49:36 +02001103 cpu->created = true;
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +02001104 qemu_cond_signal(&qemu_cpu_cond);
1105
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +02001106 while (1) {
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +02001107 qemu_mutex_unlock_iothread();
1108 do {
1109 int sig;
1110 r = sigwait(&waitset, &sig);
1111 } while (r == -1 && (errno == EAGAIN || errno == EINTR));
1112 if (r == -1) {
1113 perror("sigwait");
1114 exit(1);
1115 }
1116 qemu_mutex_lock_iothread();
Andreas Färber509a0d72012-05-03 02:18:09 +02001117 qemu_wait_io_event_common(cpu);
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +02001118 }
1119
1120 return NULL;
1121#endif
1122}
1123
Alex Bennée1be7fcb2016-10-27 16:10:08 +01001124static int64_t tcg_get_icount_limit(void)
1125{
1126 int64_t deadline;
1127
1128 if (replay_mode != REPLAY_MODE_PLAY) {
1129 deadline = qemu_clock_deadline_ns_all(QEMU_CLOCK_VIRTUAL);
1130
1131 /* Maintain prior (possibly buggy) behaviour where if no deadline
1132 * was set (as there is no QEMU_CLOCK_VIRTUAL timer) or it is more than
1133 * INT32_MAX nanoseconds ahead, we still use INT32_MAX
1134 * nanoseconds.
1135 */
1136 if ((deadline < 0) || (deadline > INT32_MAX)) {
1137 deadline = INT32_MAX;
1138 }
1139
1140 return qemu_icount_round(deadline);
1141 } else {
1142 return replay_get_instructions();
1143 }
1144}
1145
Alex Bennée12e97002016-10-27 16:10:14 +01001146static void handle_icount_deadline(void)
1147{
1148 if (use_icount) {
1149 int64_t deadline =
1150 qemu_clock_deadline_ns_all(QEMU_CLOCK_VIRTUAL);
1151
1152 if (deadline == 0) {
1153 qemu_clock_notify(QEMU_CLOCK_VIRTUAL);
1154 }
1155 }
1156}
1157
Alex Bennée1be7fcb2016-10-27 16:10:08 +01001158static int tcg_cpu_exec(CPUState *cpu)
1159{
1160 int ret;
1161#ifdef CONFIG_PROFILER
1162 int64_t ti;
1163#endif
1164
1165#ifdef CONFIG_PROFILER
1166 ti = profile_getclock();
1167#endif
1168 if (use_icount) {
1169 int64_t count;
1170 int decr;
1171 timers_state.qemu_icount -= (cpu->icount_decr.u16.low
1172 + cpu->icount_extra);
1173 cpu->icount_decr.u16.low = 0;
1174 cpu->icount_extra = 0;
1175 count = tcg_get_icount_limit();
1176 timers_state.qemu_icount += count;
1177 decr = (count > 0xffff) ? 0xffff : count;
1178 count -= decr;
1179 cpu->icount_decr.u16.low = decr;
1180 cpu->icount_extra = count;
1181 }
Jan Kiszka8d04fb52017-02-23 18:29:11 +00001182 qemu_mutex_unlock_iothread();
Alex Bennée1be7fcb2016-10-27 16:10:08 +01001183 cpu_exec_start(cpu);
1184 ret = cpu_exec(cpu);
1185 cpu_exec_end(cpu);
Jan Kiszka8d04fb52017-02-23 18:29:11 +00001186 qemu_mutex_lock_iothread();
Alex Bennée1be7fcb2016-10-27 16:10:08 +01001187#ifdef CONFIG_PROFILER
1188 tcg_time += profile_getclock() - ti;
1189#endif
1190 if (use_icount) {
1191 /* Fold pending instructions back into the
1192 instruction counter, and clear the interrupt flag. */
1193 timers_state.qemu_icount -= (cpu->icount_decr.u16.low
1194 + cpu->icount_extra);
1195 cpu->icount_decr.u32 = 0;
1196 cpu->icount_extra = 0;
1197 replay_account_executed_instructions();
1198 }
1199 return ret;
1200}
1201
Alex Bennéec93bbbe2016-10-27 16:10:09 +01001202/* Destroy any remaining vCPUs which have been unplugged and have
1203 * finished running
1204 */
1205static void deal_with_unplugged_cpus(void)
Alex Bennée1be7fcb2016-10-27 16:10:08 +01001206{
Alex Bennéec93bbbe2016-10-27 16:10:09 +01001207 CPUState *cpu;
Alex Bennée1be7fcb2016-10-27 16:10:08 +01001208
Alex Bennéec93bbbe2016-10-27 16:10:09 +01001209 CPU_FOREACH(cpu) {
1210 if (cpu->unplug && !cpu_can_run(cpu)) {
1211 qemu_tcg_destroy_vcpu(cpu);
1212 cpu->created = false;
1213 qemu_cond_signal(&qemu_cpu_cond);
Alex Bennée1be7fcb2016-10-27 16:10:08 +01001214 break;
1215 }
1216 }
Alex Bennée1be7fcb2016-10-27 16:10:08 +01001217}
Jan Kiszkabdb7ca62011-09-26 09:40:39 +02001218
Alex Bennée65467062017-02-23 18:29:09 +00001219/* Single-threaded TCG
1220 *
1221 * In the single-threaded case each vCPU is simulated in turn. If
1222 * there is more than a single vCPU we create a simple timer to kick
1223 * the vCPU and ensure we don't get stuck in a tight loop in one vCPU.
1224 * This is done explicitly rather than relying on side-effects
1225 * elsewhere.
1226 */
1227
Alex Bennée37257942017-02-23 18:29:14 +00001228static void *qemu_tcg_rr_cpu_thread_fn(void *arg)
Blue Swirl296af7c2010-03-29 19:23:50 +00001229{
Andreas Färberc3586ba2012-05-03 01:41:24 +02001230 CPUState *cpu = arg;
Blue Swirl296af7c2010-03-29 19:23:50 +00001231
Paolo Bonziniab28bd22015-07-09 08:55:38 +02001232 rcu_register_thread();
1233
Paolo Bonzini2e7f7a32015-06-18 18:47:18 +02001234 qemu_mutex_lock_iothread();
Andreas Färber814e6122012-05-02 17:00:37 +02001235 qemu_thread_get_self(cpu->thread);
Blue Swirl296af7c2010-03-29 19:23:50 +00001236
Andreas Färber38fcbd32013-07-07 19:50:23 +02001237 CPU_FOREACH(cpu) {
1238 cpu->thread_id = qemu_get_thread_id();
1239 cpu->created = true;
Pavel Dovgalyuk626cf8f2014-12-08 10:53:17 +03001240 cpu->can_do_io = 1;
Andreas Färber38fcbd32013-07-07 19:50:23 +02001241 }
Blue Swirl296af7c2010-03-29 19:23:50 +00001242 qemu_cond_signal(&qemu_cpu_cond);
1243
Jan Kiszkafa7d1862011-08-22 18:35:25 +02001244 /* wait for initial kick-off after machine start */
Emilio G. Cotac28e3992015-04-27 12:45:28 -04001245 while (first_cpu->stopped) {
KONRAD Fredericd5f8d612015-08-10 17:27:06 +02001246 qemu_cond_wait(first_cpu->halt_cond, &qemu_global_mutex);
Jan Kiszka8e564b42012-02-17 18:31:15 +01001247
1248 /* process any pending work */
Andreas Färberbdc44642013-06-24 23:50:24 +02001249 CPU_FOREACH(cpu) {
Alex Bennée37257942017-02-23 18:29:14 +00001250 current_cpu = cpu;
Andreas Färber182735e2013-05-29 22:29:20 +02001251 qemu_wait_io_event_common(cpu);
Jan Kiszka8e564b42012-02-17 18:31:15 +01001252 }
Jan Kiszka0ab07c62011-02-07 12:19:14 +01001253 }
Blue Swirl296af7c2010-03-29 19:23:50 +00001254
Alex Bennée65467062017-02-23 18:29:09 +00001255 start_tcg_kick_timer();
1256
Alex Bennéec93bbbe2016-10-27 16:10:09 +01001257 cpu = first_cpu;
1258
Alex Bennéee5143e32017-02-23 18:29:12 +00001259 /* process any pending work */
1260 cpu->exit_request = 1;
1261
Blue Swirl296af7c2010-03-29 19:23:50 +00001262 while (1) {
Alex Bennéec93bbbe2016-10-27 16:10:09 +01001263 /* Account partial waits to QEMU_CLOCK_VIRTUAL. */
1264 qemu_account_warp_timer();
1265
1266 if (!cpu) {
1267 cpu = first_cpu;
1268 }
1269
Alex Bennéee5143e32017-02-23 18:29:12 +00001270 while (cpu && !cpu->queued_work_first && !cpu->exit_request) {
1271
Alex Bennée791158d2017-02-23 18:29:10 +00001272 atomic_mb_set(&tcg_current_rr_cpu, cpu);
Alex Bennée37257942017-02-23 18:29:14 +00001273 current_cpu = cpu;
Alex Bennéec93bbbe2016-10-27 16:10:09 +01001274
1275 qemu_clock_enable(QEMU_CLOCK_VIRTUAL,
1276 (cpu->singlestep_enabled & SSTEP_NOTIMER) == 0);
1277
1278 if (cpu_can_run(cpu)) {
1279 int r;
1280 r = tcg_cpu_exec(cpu);
1281 if (r == EXCP_DEBUG) {
1282 cpu_handle_guest_debug(cpu);
1283 break;
Pranith Kumar08e73c42017-02-23 18:29:15 +00001284 } else if (r == EXCP_ATOMIC) {
1285 qemu_mutex_unlock_iothread();
1286 cpu_exec_step_atomic(cpu);
1287 qemu_mutex_lock_iothread();
1288 break;
Alex Bennéec93bbbe2016-10-27 16:10:09 +01001289 }
Alex Bennée37257942017-02-23 18:29:14 +00001290 } else if (cpu->stop) {
Alex Bennéec93bbbe2016-10-27 16:10:09 +01001291 if (cpu->unplug) {
1292 cpu = CPU_NEXT(cpu);
1293 }
1294 break;
1295 }
1296
Alex Bennéee5143e32017-02-23 18:29:12 +00001297 cpu = CPU_NEXT(cpu);
1298 } /* while (cpu && !cpu->exit_request).. */
1299
Alex Bennée791158d2017-02-23 18:29:10 +00001300 /* Does not need atomic_mb_set because a spurious wakeup is okay. */
1301 atomic_set(&tcg_current_rr_cpu, NULL);
Alex Bennéec93bbbe2016-10-27 16:10:09 +01001302
Alex Bennéee5143e32017-02-23 18:29:12 +00001303 if (cpu && cpu->exit_request) {
1304 atomic_mb_set(&cpu->exit_request, 0);
1305 }
Alex Blighac70aaf2013-08-21 16:02:57 +01001306
Alex Bennée12e97002016-10-27 16:10:14 +01001307 handle_icount_deadline();
Alex Blighac70aaf2013-08-21 16:02:57 +01001308
Alex Bennée37257942017-02-23 18:29:14 +00001309 qemu_tcg_wait_io_event(cpu ? cpu : QTAILQ_FIRST(&cpus));
Alex Bennéec93bbbe2016-10-27 16:10:09 +01001310 deal_with_unplugged_cpus();
Blue Swirl296af7c2010-03-29 19:23:50 +00001311 }
1312
1313 return NULL;
1314}
1315
Vincent Palatinb0cb0a62017-01-10 11:59:57 +01001316static void *qemu_hax_cpu_thread_fn(void *arg)
1317{
1318 CPUState *cpu = arg;
1319 int r;
1320 qemu_thread_get_self(cpu->thread);
1321 qemu_mutex_lock(&qemu_global_mutex);
1322
1323 cpu->thread_id = qemu_get_thread_id();
1324 cpu->created = true;
1325 cpu->halted = 0;
1326 current_cpu = cpu;
1327
1328 hax_init_vcpu(cpu);
1329 qemu_cond_signal(&qemu_cpu_cond);
1330
1331 while (1) {
1332 if (cpu_can_run(cpu)) {
1333 r = hax_smp_cpu_exec(cpu);
1334 if (r == EXCP_DEBUG) {
1335 cpu_handle_guest_debug(cpu);
1336 }
1337 }
1338
1339 while (cpu_thread_is_idle(cpu)) {
1340 qemu_cond_wait(cpu->halt_cond, &qemu_global_mutex);
1341 }
1342#ifdef _WIN32
1343 SleepEx(0, TRUE);
1344#endif
1345 qemu_wait_io_event_common(cpu);
1346 }
1347 return NULL;
1348}
1349
1350#ifdef _WIN32
1351static void CALLBACK dummy_apc_func(ULONG_PTR unused)
1352{
1353}
1354#endif
1355
Alex Bennée37257942017-02-23 18:29:14 +00001356/* Multi-threaded TCG
1357 *
1358 * In the multi-threaded case each vCPU has its own thread. The TLS
1359 * variable current_cpu can be used deep in the code to find the
1360 * current CPUState for a given thread.
1361 */
1362
1363static void *qemu_tcg_cpu_thread_fn(void *arg)
1364{
1365 CPUState *cpu = arg;
1366
1367 rcu_register_thread();
1368
1369 qemu_mutex_lock_iothread();
1370 qemu_thread_get_self(cpu->thread);
1371
1372 cpu->thread_id = qemu_get_thread_id();
1373 cpu->created = true;
1374 cpu->can_do_io = 1;
1375 current_cpu = cpu;
1376 qemu_cond_signal(&qemu_cpu_cond);
1377
1378 /* process any pending work */
1379 cpu->exit_request = 1;
1380
1381 while (1) {
1382 if (cpu_can_run(cpu)) {
1383 int r;
1384 r = tcg_cpu_exec(cpu);
1385 switch (r) {
1386 case EXCP_DEBUG:
1387 cpu_handle_guest_debug(cpu);
1388 break;
1389 case EXCP_HALTED:
1390 /* during start-up the vCPU is reset and the thread is
1391 * kicked several times. If we don't ensure we go back
1392 * to sleep in the halted state we won't cleanly
1393 * start-up when the vCPU is enabled.
1394 *
1395 * cpu->halted should ensure we sleep in wait_io_event
1396 */
1397 g_assert(cpu->halted);
1398 break;
Pranith Kumar08e73c42017-02-23 18:29:15 +00001399 case EXCP_ATOMIC:
1400 qemu_mutex_unlock_iothread();
1401 cpu_exec_step_atomic(cpu);
1402 qemu_mutex_lock_iothread();
Alex Bennée37257942017-02-23 18:29:14 +00001403 default:
1404 /* Ignore everything else? */
1405 break;
1406 }
1407 }
1408
1409 handle_icount_deadline();
1410
1411 atomic_mb_set(&cpu->exit_request, 0);
1412 qemu_tcg_wait_io_event(cpu);
1413 }
1414
1415 return NULL;
1416}
1417
Andreas Färber2ff09a42012-05-03 00:23:30 +02001418static void qemu_cpu_kick_thread(CPUState *cpu)
Paolo Bonzinicc015e92011-03-12 17:44:08 +01001419{
1420#ifndef _WIN32
1421 int err;
1422
Paolo Bonzinie0c38212015-08-26 00:19:19 +02001423 if (cpu->thread_kicked) {
1424 return;
Paolo Bonzini9102ded2015-08-18 06:52:09 -07001425 }
Paolo Bonzinie0c38212015-08-26 00:19:19 +02001426 cpu->thread_kicked = true;
Andreas Färber814e6122012-05-02 17:00:37 +02001427 err = pthread_kill(cpu->thread->thread, SIG_IPI);
Paolo Bonzinicc015e92011-03-12 17:44:08 +01001428 if (err) {
1429 fprintf(stderr, "qemu:%s: %s", __func__, strerror(err));
1430 exit(1);
1431 }
1432#else /* _WIN32 */
Vincent Palatinb0cb0a62017-01-10 11:59:57 +01001433 if (!qemu_cpu_is_self(cpu)) {
1434 if (!QueueUserAPC(dummy_apc_func, cpu->hThread, 0)) {
1435 fprintf(stderr, "%s: QueueUserAPC failed with error %lu\n",
1436 __func__, GetLastError());
1437 exit(1);
1438 }
1439 }
Paolo Bonzinicc015e92011-03-12 17:44:08 +01001440#endif
1441}
1442
Andreas Färberc08d7422012-05-03 04:34:15 +02001443void qemu_cpu_kick(CPUState *cpu)
Blue Swirl296af7c2010-03-29 19:23:50 +00001444{
Andreas Färberf5c121b2012-05-03 01:22:49 +02001445 qemu_cond_broadcast(cpu->halt_cond);
Paolo Bonzinie0c38212015-08-26 00:19:19 +02001446 if (tcg_enabled()) {
Alex Bennée791158d2017-02-23 18:29:10 +00001447 cpu_exit(cpu);
Alex Bennée37257942017-02-23 18:29:14 +00001448 /* NOP unless doing single-thread RR */
Alex Bennée791158d2017-02-23 18:29:10 +00001449 qemu_cpu_kick_rr_cpu();
Paolo Bonzinie0c38212015-08-26 00:19:19 +02001450 } else {
Vincent Palatinb0cb0a62017-01-10 11:59:57 +01001451 if (hax_enabled()) {
1452 /*
1453 * FIXME: race condition with the exit_request check in
1454 * hax_vcpu_hax_exec
1455 */
1456 cpu->exit_request = 1;
1457 }
Paolo Bonzinie0c38212015-08-26 00:19:19 +02001458 qemu_cpu_kick_thread(cpu);
1459 }
Blue Swirl296af7c2010-03-29 19:23:50 +00001460}
1461
Jan Kiszka46d62fa2011-02-01 22:15:59 +01001462void qemu_cpu_kick_self(void)
1463{
Andreas Färber4917cf42013-05-27 05:17:50 +02001464 assert(current_cpu);
Paolo Bonzini9102ded2015-08-18 06:52:09 -07001465 qemu_cpu_kick_thread(current_cpu);
Blue Swirl296af7c2010-03-29 19:23:50 +00001466}
1467
Andreas Färber60e82572012-05-02 22:23:49 +02001468bool qemu_cpu_is_self(CPUState *cpu)
Blue Swirl296af7c2010-03-29 19:23:50 +00001469{
Andreas Färber814e6122012-05-02 17:00:37 +02001470 return qemu_thread_is_self(cpu->thread);
Blue Swirl296af7c2010-03-29 19:23:50 +00001471}
1472
Paolo Bonzini79e2b9a2015-01-21 12:09:14 +01001473bool qemu_in_vcpu_thread(void)
Juan Quintelaaa723c22012-09-18 16:30:11 +02001474{
Andreas Färber4917cf42013-05-27 05:17:50 +02001475 return current_cpu && qemu_cpu_is_self(current_cpu);
Juan Quintelaaa723c22012-09-18 16:30:11 +02001476}
1477
Paolo Bonziniafbe7052015-06-18 18:47:19 +02001478static __thread bool iothread_locked = false;
1479
1480bool qemu_mutex_iothread_locked(void)
1481{
1482 return iothread_locked;
1483}
1484
Blue Swirl296af7c2010-03-29 19:23:50 +00001485void qemu_mutex_lock_iothread(void)
1486{
Jan Kiszka8d04fb52017-02-23 18:29:11 +00001487 g_assert(!qemu_mutex_iothread_locked());
1488 qemu_mutex_lock(&qemu_global_mutex);
Paolo Bonziniafbe7052015-06-18 18:47:19 +02001489 iothread_locked = true;
Blue Swirl296af7c2010-03-29 19:23:50 +00001490}
1491
1492void qemu_mutex_unlock_iothread(void)
1493{
Jan Kiszka8d04fb52017-02-23 18:29:11 +00001494 g_assert(qemu_mutex_iothread_locked());
Paolo Bonziniafbe7052015-06-18 18:47:19 +02001495 iothread_locked = false;
Blue Swirl296af7c2010-03-29 19:23:50 +00001496 qemu_mutex_unlock(&qemu_global_mutex);
1497}
1498
Alex Bennéee8faee02016-10-27 16:09:58 +01001499static bool all_vcpus_paused(void)
Blue Swirl296af7c2010-03-29 19:23:50 +00001500{
Andreas Färberbdc44642013-06-24 23:50:24 +02001501 CPUState *cpu;
Blue Swirl296af7c2010-03-29 19:23:50 +00001502
Andreas Färberbdc44642013-06-24 23:50:24 +02001503 CPU_FOREACH(cpu) {
Andreas Färber182735e2013-05-29 22:29:20 +02001504 if (!cpu->stopped) {
Alex Bennéee8faee02016-10-27 16:09:58 +01001505 return false;
Jan Kiszka0ab07c62011-02-07 12:19:14 +01001506 }
Blue Swirl296af7c2010-03-29 19:23:50 +00001507 }
1508
Alex Bennéee8faee02016-10-27 16:09:58 +01001509 return true;
Blue Swirl296af7c2010-03-29 19:23:50 +00001510}
1511
1512void pause_all_vcpus(void)
1513{
Andreas Färberbdc44642013-06-24 23:50:24 +02001514 CPUState *cpu;
Blue Swirl296af7c2010-03-29 19:23:50 +00001515
Alex Bligh40daca52013-08-21 16:03:02 +01001516 qemu_clock_enable(QEMU_CLOCK_VIRTUAL, false);
Andreas Färberbdc44642013-06-24 23:50:24 +02001517 CPU_FOREACH(cpu) {
Andreas Färber182735e2013-05-29 22:29:20 +02001518 cpu->stop = true;
1519 qemu_cpu_kick(cpu);
Blue Swirl296af7c2010-03-29 19:23:50 +00001520 }
1521
Juan Quintelaaa723c22012-09-18 16:30:11 +02001522 if (qemu_in_vcpu_thread()) {
Jan Kiszkad798e972012-02-17 18:31:16 +01001523 cpu_stop_current();
Jan Kiszkad798e972012-02-17 18:31:16 +01001524 }
1525
Blue Swirl296af7c2010-03-29 19:23:50 +00001526 while (!all_vcpus_paused()) {
Paolo Bonzinibe7d6c52011-03-12 17:44:02 +01001527 qemu_cond_wait(&qemu_pause_cond, &qemu_global_mutex);
Andreas Färberbdc44642013-06-24 23:50:24 +02001528 CPU_FOREACH(cpu) {
Andreas Färber182735e2013-05-29 22:29:20 +02001529 qemu_cpu_kick(cpu);
Blue Swirl296af7c2010-03-29 19:23:50 +00001530 }
1531 }
1532}
1533
Igor Mammedov29936832013-04-23 10:29:37 +02001534void cpu_resume(CPUState *cpu)
1535{
1536 cpu->stop = false;
1537 cpu->stopped = false;
1538 qemu_cpu_kick(cpu);
1539}
1540
Blue Swirl296af7c2010-03-29 19:23:50 +00001541void resume_all_vcpus(void)
1542{
Andreas Färberbdc44642013-06-24 23:50:24 +02001543 CPUState *cpu;
Blue Swirl296af7c2010-03-29 19:23:50 +00001544
Alex Bligh40daca52013-08-21 16:03:02 +01001545 qemu_clock_enable(QEMU_CLOCK_VIRTUAL, true);
Andreas Färberbdc44642013-06-24 23:50:24 +02001546 CPU_FOREACH(cpu) {
Andreas Färber182735e2013-05-29 22:29:20 +02001547 cpu_resume(cpu);
Blue Swirl296af7c2010-03-29 19:23:50 +00001548 }
1549}
1550
Gu Zheng4c055ab2016-05-12 09:18:13 +05301551void cpu_remove(CPUState *cpu)
1552{
1553 cpu->stop = true;
1554 cpu->unplug = true;
1555 qemu_cpu_kick(cpu);
1556}
1557
Bharata B Rao2c579042016-05-12 09:18:14 +05301558void cpu_remove_sync(CPUState *cpu)
1559{
1560 cpu_remove(cpu);
1561 while (cpu->created) {
1562 qemu_cond_wait(&qemu_cpu_cond, &qemu_global_mutex);
1563 }
1564}
1565
Dr. David Alan Gilbert49001162014-01-30 10:20:32 +00001566/* For temporary buffers for forming a name */
1567#define VCPU_THREAD_NAME_SIZE 16
1568
Andreas Färbere5ab30a2012-05-03 01:50:44 +02001569static void qemu_tcg_init_vcpu(CPUState *cpu)
Blue Swirl296af7c2010-03-29 19:23:50 +00001570{
Dr. David Alan Gilbert49001162014-01-30 10:20:32 +00001571 char thread_name[VCPU_THREAD_NAME_SIZE];
Alex Bennée37257942017-02-23 18:29:14 +00001572 static QemuCond *single_tcg_halt_cond;
1573 static QemuThread *single_tcg_cpu_thread;
Dr. David Alan Gilbert49001162014-01-30 10:20:32 +00001574
Alex Bennée37257942017-02-23 18:29:14 +00001575 if (qemu_tcg_mttcg_enabled() || !single_tcg_cpu_thread) {
Andreas Färber814e6122012-05-02 17:00:37 +02001576 cpu->thread = g_malloc0(sizeof(QemuThread));
Andreas Färberf5c121b2012-05-03 01:22:49 +02001577 cpu->halt_cond = g_malloc0(sizeof(QemuCond));
1578 qemu_cond_init(cpu->halt_cond);
Alex Bennée37257942017-02-23 18:29:14 +00001579
1580 if (qemu_tcg_mttcg_enabled()) {
1581 /* create a thread per vCPU with TCG (MTTCG) */
1582 parallel_cpus = true;
1583 snprintf(thread_name, VCPU_THREAD_NAME_SIZE, "CPU %d/TCG",
Dr. David Alan Gilbert49001162014-01-30 10:20:32 +00001584 cpu->cpu_index);
Alex Bennée37257942017-02-23 18:29:14 +00001585
1586 qemu_thread_create(cpu->thread, thread_name, qemu_tcg_cpu_thread_fn,
1587 cpu, QEMU_THREAD_JOINABLE);
1588
1589 } else {
1590 /* share a single thread for all cpus with TCG */
1591 snprintf(thread_name, VCPU_THREAD_NAME_SIZE, "ALL CPUs/TCG");
1592 qemu_thread_create(cpu->thread, thread_name,
1593 qemu_tcg_rr_cpu_thread_fn,
1594 cpu, QEMU_THREAD_JOINABLE);
1595
1596 single_tcg_halt_cond = cpu->halt_cond;
1597 single_tcg_cpu_thread = cpu->thread;
1598 }
Paolo Bonzini1ecf47b2011-12-13 13:43:52 +01001599#ifdef _WIN32
Andreas Färber814e6122012-05-02 17:00:37 +02001600 cpu->hThread = qemu_thread_get_handle(cpu->thread);
Paolo Bonzini1ecf47b2011-12-13 13:43:52 +01001601#endif
Andreas Färber61a46212012-05-02 22:49:36 +02001602 while (!cpu->created) {
Paolo Bonzini18a85722011-03-12 17:44:03 +01001603 qemu_cond_wait(&qemu_cpu_cond, &qemu_global_mutex);
Jan Kiszka0ab07c62011-02-07 12:19:14 +01001604 }
Blue Swirl296af7c2010-03-29 19:23:50 +00001605 } else {
Alex Bennée37257942017-02-23 18:29:14 +00001606 /* For non-MTTCG cases we share the thread */
1607 cpu->thread = single_tcg_cpu_thread;
1608 cpu->halt_cond = single_tcg_halt_cond;
Blue Swirl296af7c2010-03-29 19:23:50 +00001609 }
1610}
1611
Vincent Palatinb0cb0a62017-01-10 11:59:57 +01001612static void qemu_hax_start_vcpu(CPUState *cpu)
1613{
1614 char thread_name[VCPU_THREAD_NAME_SIZE];
1615
1616 cpu->thread = g_malloc0(sizeof(QemuThread));
1617 cpu->halt_cond = g_malloc0(sizeof(QemuCond));
1618 qemu_cond_init(cpu->halt_cond);
1619
1620 snprintf(thread_name, VCPU_THREAD_NAME_SIZE, "CPU %d/HAX",
1621 cpu->cpu_index);
1622 qemu_thread_create(cpu->thread, thread_name, qemu_hax_cpu_thread_fn,
1623 cpu, QEMU_THREAD_JOINABLE);
1624#ifdef _WIN32
1625 cpu->hThread = qemu_thread_get_handle(cpu->thread);
1626#endif
1627 while (!cpu->created) {
1628 qemu_cond_wait(&qemu_cpu_cond, &qemu_global_mutex);
1629 }
1630}
1631
Andreas Färber48a106b2013-05-27 02:20:39 +02001632static void qemu_kvm_start_vcpu(CPUState *cpu)
Blue Swirl296af7c2010-03-29 19:23:50 +00001633{
Dr. David Alan Gilbert49001162014-01-30 10:20:32 +00001634 char thread_name[VCPU_THREAD_NAME_SIZE];
1635
Andreas Färber814e6122012-05-02 17:00:37 +02001636 cpu->thread = g_malloc0(sizeof(QemuThread));
Andreas Färberf5c121b2012-05-03 01:22:49 +02001637 cpu->halt_cond = g_malloc0(sizeof(QemuCond));
1638 qemu_cond_init(cpu->halt_cond);
Dr. David Alan Gilbert49001162014-01-30 10:20:32 +00001639 snprintf(thread_name, VCPU_THREAD_NAME_SIZE, "CPU %d/KVM",
1640 cpu->cpu_index);
1641 qemu_thread_create(cpu->thread, thread_name, qemu_kvm_cpu_thread_fn,
1642 cpu, QEMU_THREAD_JOINABLE);
Andreas Färber61a46212012-05-02 22:49:36 +02001643 while (!cpu->created) {
Paolo Bonzini18a85722011-03-12 17:44:03 +01001644 qemu_cond_wait(&qemu_cpu_cond, &qemu_global_mutex);
Jan Kiszka0ab07c62011-02-07 12:19:14 +01001645 }
Blue Swirl296af7c2010-03-29 19:23:50 +00001646}
1647
Andreas Färber10a90212013-05-27 02:24:35 +02001648static void qemu_dummy_start_vcpu(CPUState *cpu)
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +02001649{
Dr. David Alan Gilbert49001162014-01-30 10:20:32 +00001650 char thread_name[VCPU_THREAD_NAME_SIZE];
1651
Andreas Färber814e6122012-05-02 17:00:37 +02001652 cpu->thread = g_malloc0(sizeof(QemuThread));
Andreas Färberf5c121b2012-05-03 01:22:49 +02001653 cpu->halt_cond = g_malloc0(sizeof(QemuCond));
1654 qemu_cond_init(cpu->halt_cond);
Dr. David Alan Gilbert49001162014-01-30 10:20:32 +00001655 snprintf(thread_name, VCPU_THREAD_NAME_SIZE, "CPU %d/DUMMY",
1656 cpu->cpu_index);
1657 qemu_thread_create(cpu->thread, thread_name, qemu_dummy_cpu_thread_fn, cpu,
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +02001658 QEMU_THREAD_JOINABLE);
Andreas Färber61a46212012-05-02 22:49:36 +02001659 while (!cpu->created) {
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +02001660 qemu_cond_wait(&qemu_cpu_cond, &qemu_global_mutex);
1661 }
1662}
1663
Andreas Färberc643bed2013-05-27 03:23:24 +02001664void qemu_init_vcpu(CPUState *cpu)
Blue Swirl296af7c2010-03-29 19:23:50 +00001665{
Andreas Färberce3960e2012-12-17 03:27:07 +01001666 cpu->nr_cores = smp_cores;
1667 cpu->nr_threads = smp_threads;
Andreas Färberf324e762012-05-02 23:26:21 +02001668 cpu->stopped = true;
Peter Maydell56943e82016-01-21 14:15:04 +00001669
1670 if (!cpu->as) {
1671 /* If the target cpu hasn't set up any address spaces itself,
1672 * give it the default one.
1673 */
Peter Crosthwaite6731d862016-01-21 14:15:06 +00001674 AddressSpace *as = address_space_init_shareable(cpu->memory,
1675 "cpu-memory");
Peter Maydell12ebc9a2016-01-21 14:15:04 +00001676 cpu->num_ases = 1;
Peter Crosthwaite6731d862016-01-21 14:15:06 +00001677 cpu_address_space_init(cpu, as, 0);
Peter Maydell56943e82016-01-21 14:15:04 +00001678 }
1679
Jan Kiszka0ab07c62011-02-07 12:19:14 +01001680 if (kvm_enabled()) {
Andreas Färber48a106b2013-05-27 02:20:39 +02001681 qemu_kvm_start_vcpu(cpu);
Vincent Palatinb0cb0a62017-01-10 11:59:57 +01001682 } else if (hax_enabled()) {
1683 qemu_hax_start_vcpu(cpu);
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +02001684 } else if (tcg_enabled()) {
Andreas Färbere5ab30a2012-05-03 01:50:44 +02001685 qemu_tcg_init_vcpu(cpu);
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +02001686 } else {
Andreas Färber10a90212013-05-27 02:24:35 +02001687 qemu_dummy_start_vcpu(cpu);
Jan Kiszka0ab07c62011-02-07 12:19:14 +01001688 }
Blue Swirl296af7c2010-03-29 19:23:50 +00001689}
1690
Jan Kiszkab4a3d962011-02-01 22:15:43 +01001691void cpu_stop_current(void)
Blue Swirl296af7c2010-03-29 19:23:50 +00001692{
Andreas Färber4917cf42013-05-27 05:17:50 +02001693 if (current_cpu) {
1694 current_cpu->stop = false;
1695 current_cpu->stopped = true;
1696 cpu_exit(current_cpu);
Dr. David Alan Gilbert96bce682016-01-25 10:08:18 +00001697 qemu_cond_broadcast(&qemu_pause_cond);
Jan Kiszkab4a3d962011-02-01 22:15:43 +01001698 }
Blue Swirl296af7c2010-03-29 19:23:50 +00001699}
1700
Kevin Wolf56983462013-07-05 13:49:54 +02001701int vm_stop(RunState state)
Blue Swirl296af7c2010-03-29 19:23:50 +00001702{
Juan Quintelaaa723c22012-09-18 16:30:11 +02001703 if (qemu_in_vcpu_thread()) {
Paolo Bonzini74892d22014-06-05 14:53:58 +02001704 qemu_system_vmstop_request_prepare();
Luiz Capitulino1dfb4dd2011-07-29 14:26:33 -03001705 qemu_system_vmstop_request(state);
Blue Swirl296af7c2010-03-29 19:23:50 +00001706 /*
1707 * FIXME: should not return to device code in case
1708 * vm_stop() has been requested.
1709 */
Jan Kiszkab4a3d962011-02-01 22:15:43 +01001710 cpu_stop_current();
Kevin Wolf56983462013-07-05 13:49:54 +02001711 return 0;
Blue Swirl296af7c2010-03-29 19:23:50 +00001712 }
Kevin Wolf56983462013-07-05 13:49:54 +02001713
1714 return do_vm_stop(state);
Blue Swirl296af7c2010-03-29 19:23:50 +00001715}
1716
Claudio Imbrenda2d76e822017-02-14 18:07:47 +01001717/**
1718 * Prepare for (re)starting the VM.
1719 * Returns -1 if the vCPUs are not to be restarted (e.g. if they are already
1720 * running or in case of an error condition), 0 otherwise.
1721 */
1722int vm_prepare_start(void)
1723{
1724 RunState requested;
1725 int res = 0;
1726
1727 qemu_vmstop_requested(&requested);
1728 if (runstate_is_running() && requested == RUN_STATE__MAX) {
1729 return -1;
1730 }
1731
1732 /* Ensure that a STOP/RESUME pair of events is emitted if a
1733 * vmstop request was pending. The BLOCK_IO_ERROR event, for
1734 * example, according to documentation is always followed by
1735 * the STOP event.
1736 */
1737 if (runstate_is_running()) {
1738 qapi_event_send_stop(&error_abort);
1739 res = -1;
1740 } else {
1741 replay_enable_events();
1742 cpu_enable_ticks();
1743 runstate_set(RUN_STATE_RUNNING);
1744 vm_state_notify(1, RUN_STATE_RUNNING);
1745 }
1746
1747 /* We are sending this now, but the CPUs will be resumed shortly later */
1748 qapi_event_send_resume(&error_abort);
1749 return res;
1750}
1751
1752void vm_start(void)
1753{
1754 if (!vm_prepare_start()) {
1755 resume_all_vcpus();
1756 }
1757}
1758
Luiz Capitulino8a9236f2011-10-14 11:18:09 -03001759/* does a state transition even if the VM is already stopped,
1760 current state is forgotten forever */
Kevin Wolf56983462013-07-05 13:49:54 +02001761int vm_stop_force_state(RunState state)
Luiz Capitulino8a9236f2011-10-14 11:18:09 -03001762{
1763 if (runstate_is_running()) {
Kevin Wolf56983462013-07-05 13:49:54 +02001764 return vm_stop(state);
Luiz Capitulino8a9236f2011-10-14 11:18:09 -03001765 } else {
1766 runstate_set(state);
Wen Congyangb2780d32015-11-20 17:34:38 +08001767
1768 bdrv_drain_all();
Kevin Wolf594a45c2013-07-18 14:52:19 +02001769 /* Make sure to return an error if the flush in a previous vm_stop()
1770 * failed. */
John Snow22af08e2016-09-22 21:45:51 -04001771 return bdrv_flush_all();
Luiz Capitulino8a9236f2011-10-14 11:18:09 -03001772 }
1773}
1774
Stefan Weil9a78eea2010-10-22 23:03:33 +02001775void list_cpus(FILE *f, fprintf_function cpu_fprintf, const char *optarg)
Blue Swirl262353c2010-05-04 19:55:35 +00001776{
1777 /* XXX: implement xxx_cpu_list for targets that still miss it */
Peter Maydelle916cbf2012-09-05 17:41:08 -03001778#if defined(cpu_list)
1779 cpu_list(f, cpu_fprintf);
Blue Swirl262353c2010-05-04 19:55:35 +00001780#endif
1781}
Luiz Capitulinode0b36b2011-09-21 16:38:35 -03001782
1783CpuInfoList *qmp_query_cpus(Error **errp)
1784{
1785 CpuInfoList *head = NULL, *cur_item = NULL;
Andreas Färber182735e2013-05-29 22:29:20 +02001786 CPUState *cpu;
Luiz Capitulinode0b36b2011-09-21 16:38:35 -03001787
Andreas Färberbdc44642013-06-24 23:50:24 +02001788 CPU_FOREACH(cpu) {
Luiz Capitulinode0b36b2011-09-21 16:38:35 -03001789 CpuInfoList *info;
Andreas Färber182735e2013-05-29 22:29:20 +02001790#if defined(TARGET_I386)
1791 X86CPU *x86_cpu = X86_CPU(cpu);
1792 CPUX86State *env = &x86_cpu->env;
1793#elif defined(TARGET_PPC)
1794 PowerPCCPU *ppc_cpu = POWERPC_CPU(cpu);
1795 CPUPPCState *env = &ppc_cpu->env;
1796#elif defined(TARGET_SPARC)
1797 SPARCCPU *sparc_cpu = SPARC_CPU(cpu);
1798 CPUSPARCState *env = &sparc_cpu->env;
1799#elif defined(TARGET_MIPS)
1800 MIPSCPU *mips_cpu = MIPS_CPU(cpu);
1801 CPUMIPSState *env = &mips_cpu->env;
Bastian Koppelmann48e06fe2014-09-01 12:59:46 +01001802#elif defined(TARGET_TRICORE)
1803 TriCoreCPU *tricore_cpu = TRICORE_CPU(cpu);
1804 CPUTriCoreState *env = &tricore_cpu->env;
Andreas Färber182735e2013-05-29 22:29:20 +02001805#endif
Luiz Capitulinode0b36b2011-09-21 16:38:35 -03001806
Andreas Färbercb446ec2013-05-01 14:24:52 +02001807 cpu_synchronize_state(cpu);
Luiz Capitulinode0b36b2011-09-21 16:38:35 -03001808
1809 info = g_malloc0(sizeof(*info));
1810 info->value = g_malloc0(sizeof(*info->value));
Andreas Färber55e5c282012-12-17 06:18:02 +01001811 info->value->CPU = cpu->cpu_index;
Andreas Färber182735e2013-05-29 22:29:20 +02001812 info->value->current = (cpu == first_cpu);
Andreas Färber259186a2013-01-17 18:51:17 +01001813 info->value->halted = cpu->halted;
Eduardo Habkost58f88d42015-05-08 16:04:22 -03001814 info->value->qom_path = object_get_canonical_path(OBJECT(cpu));
Andreas Färber9f09e182012-05-03 06:59:07 +02001815 info->value->thread_id = cpu->thread_id;
Luiz Capitulinode0b36b2011-09-21 16:38:35 -03001816#if defined(TARGET_I386)
Eric Blake86f4b682015-11-18 01:52:59 -07001817 info->value->arch = CPU_INFO_ARCH_X86;
Eric Blake544a3732016-02-17 23:48:27 -07001818 info->value->u.x86.pc = env->eip + env->segs[R_CS].base;
Luiz Capitulinode0b36b2011-09-21 16:38:35 -03001819#elif defined(TARGET_PPC)
Eric Blake86f4b682015-11-18 01:52:59 -07001820 info->value->arch = CPU_INFO_ARCH_PPC;
Eric Blake544a3732016-02-17 23:48:27 -07001821 info->value->u.ppc.nip = env->nip;
Luiz Capitulinode0b36b2011-09-21 16:38:35 -03001822#elif defined(TARGET_SPARC)
Eric Blake86f4b682015-11-18 01:52:59 -07001823 info->value->arch = CPU_INFO_ARCH_SPARC;
Eric Blake544a3732016-02-17 23:48:27 -07001824 info->value->u.q_sparc.pc = env->pc;
1825 info->value->u.q_sparc.npc = env->npc;
Luiz Capitulinode0b36b2011-09-21 16:38:35 -03001826#elif defined(TARGET_MIPS)
Eric Blake86f4b682015-11-18 01:52:59 -07001827 info->value->arch = CPU_INFO_ARCH_MIPS;
Eric Blake544a3732016-02-17 23:48:27 -07001828 info->value->u.q_mips.PC = env->active_tc.PC;
Bastian Koppelmann48e06fe2014-09-01 12:59:46 +01001829#elif defined(TARGET_TRICORE)
Eric Blake86f4b682015-11-18 01:52:59 -07001830 info->value->arch = CPU_INFO_ARCH_TRICORE;
Eric Blake544a3732016-02-17 23:48:27 -07001831 info->value->u.tricore.PC = env->PC;
Eric Blake86f4b682015-11-18 01:52:59 -07001832#else
1833 info->value->arch = CPU_INFO_ARCH_OTHER;
Luiz Capitulinode0b36b2011-09-21 16:38:35 -03001834#endif
1835
1836 /* XXX: waiting for the qapi to support GSList */
1837 if (!cur_item) {
1838 head = cur_item = info;
1839 } else {
1840 cur_item->next = info;
1841 cur_item = info;
1842 }
1843 }
1844
1845 return head;
1846}
Luiz Capitulino0cfd6a92011-11-22 16:32:37 -02001847
1848void qmp_memsave(int64_t addr, int64_t size, const char *filename,
1849 bool has_cpu, int64_t cpu_index, Error **errp)
1850{
1851 FILE *f;
1852 uint32_t l;
Andreas Färber55e5c282012-12-17 06:18:02 +01001853 CPUState *cpu;
Luiz Capitulino0cfd6a92011-11-22 16:32:37 -02001854 uint8_t buf[1024];
Borislav Petkov0dc9daf2015-02-08 13:14:38 +01001855 int64_t orig_addr = addr, orig_size = size;
Luiz Capitulino0cfd6a92011-11-22 16:32:37 -02001856
1857 if (!has_cpu) {
1858 cpu_index = 0;
1859 }
1860
Andreas Färber151d1322013-02-15 15:41:49 +01001861 cpu = qemu_get_cpu(cpu_index);
1862 if (cpu == NULL) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001863 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "cpu-index",
1864 "a CPU number");
Luiz Capitulino0cfd6a92011-11-22 16:32:37 -02001865 return;
1866 }
1867
1868 f = fopen(filename, "wb");
1869 if (!f) {
Luiz Capitulino618da852013-06-07 14:35:06 -04001870 error_setg_file_open(errp, errno, filename);
Luiz Capitulino0cfd6a92011-11-22 16:32:37 -02001871 return;
1872 }
1873
1874 while (size != 0) {
1875 l = sizeof(buf);
1876 if (l > size)
1877 l = size;
Aneesh Kumar K.V2f4d0f52013-10-01 21:49:30 +05301878 if (cpu_memory_rw_debug(cpu, addr, buf, l, 0) != 0) {
Borislav Petkov0dc9daf2015-02-08 13:14:38 +01001879 error_setg(errp, "Invalid addr 0x%016" PRIx64 "/size %" PRId64
1880 " specified", orig_addr, orig_size);
Aneesh Kumar K.V2f4d0f52013-10-01 21:49:30 +05301881 goto exit;
1882 }
Luiz Capitulino0cfd6a92011-11-22 16:32:37 -02001883 if (fwrite(buf, 1, l, f) != l) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001884 error_setg(errp, QERR_IO_ERROR);
Luiz Capitulino0cfd6a92011-11-22 16:32:37 -02001885 goto exit;
1886 }
1887 addr += l;
1888 size -= l;
1889 }
1890
1891exit:
1892 fclose(f);
1893}
Luiz Capitulino6d3962b2011-11-22 17:26:46 -02001894
1895void qmp_pmemsave(int64_t addr, int64_t size, const char *filename,
1896 Error **errp)
1897{
1898 FILE *f;
1899 uint32_t l;
1900 uint8_t buf[1024];
1901
1902 f = fopen(filename, "wb");
1903 if (!f) {
Luiz Capitulino618da852013-06-07 14:35:06 -04001904 error_setg_file_open(errp, errno, filename);
Luiz Capitulino6d3962b2011-11-22 17:26:46 -02001905 return;
1906 }
1907
1908 while (size != 0) {
1909 l = sizeof(buf);
1910 if (l > size)
1911 l = size;
Stefan Weileb6282f2014-04-07 20:28:23 +02001912 cpu_physical_memory_read(addr, buf, l);
Luiz Capitulino6d3962b2011-11-22 17:26:46 -02001913 if (fwrite(buf, 1, l, f) != l) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001914 error_setg(errp, QERR_IO_ERROR);
Luiz Capitulino6d3962b2011-11-22 17:26:46 -02001915 goto exit;
1916 }
1917 addr += l;
1918 size -= l;
1919 }
1920
1921exit:
1922 fclose(f);
1923}
Luiz Capitulinoab49ab52011-11-23 12:55:53 -02001924
1925void qmp_inject_nmi(Error **errp)
1926{
Alexey Kardashevskiy9cb805f2014-08-20 22:16:33 +10001927 nmi_monitor_handle(monitor_get_cpu_index(), errp);
Luiz Capitulinoab49ab52011-11-23 12:55:53 -02001928}
Sebastian Tanase27498be2014-07-25 11:56:33 +02001929
1930void dump_drift_info(FILE *f, fprintf_function cpu_fprintf)
1931{
1932 if (!use_icount) {
1933 return;
1934 }
1935
1936 cpu_fprintf(f, "Host - Guest clock %"PRIi64" ms\n",
1937 (cpu_get_clock() - cpu_get_icount())/SCALE_MS);
1938 if (icount_align_option) {
1939 cpu_fprintf(f, "Max guest delay %"PRIi64" ms\n", -max_delay/SCALE_MS);
1940 cpu_fprintf(f, "Max guest advance %"PRIi64" ms\n", max_advance/SCALE_MS);
1941 } else {
1942 cpu_fprintf(f, "Max guest delay NA\n");
1943 cpu_fprintf(f, "Max guest advance NA\n");
1944 }
1945}