blob: a2b33ccb293e50bd977d700e32c4a8e09f61e1f3 [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
Peter Maydell7b31bbc2016-01-26 18:16:56 +000025#include "qemu/osdep.h"
KONRAD Frederic8d4e9142017-02-23 18:29:08 +000026#include "qemu/config-file.h"
Paolo Bonzini33c11872016-03-15 16:58:45 +010027#include "cpu.h"
Paolo Bonzini83c90892012-12-17 18:19:49 +010028#include "monitor/monitor.h"
Markus Armbrustere688df62018-02-01 12:18:31 +010029#include "qapi/error.h"
Markus Armbruster112ed242018-02-26 17:13:27 -060030#include "qapi/qapi-commands-misc.h"
Markus Armbruster9af23982018-02-11 10:36:01 +010031#include "qapi/qapi-events-run-state.h"
Wenchao Xiaa4e15de2014-06-18 08:43:36 +020032#include "qapi/qmp/qerror.h"
Markus Armbrusterd49b6832015-03-17 18:29:20 +010033#include "qemu/error-report.h"
Paolo Bonzini9c17d612012-12-17 18:20:04 +010034#include "sysemu/sysemu.h"
Max Reitzda31d592016-03-16 19:54:32 +010035#include "sysemu/block-backend.h"
Paolo Bonzini022c62c2012-12-17 18:19:49 +010036#include "exec/gdbstub.h"
Paolo Bonzini9c17d612012-12-17 18:20:04 +010037#include "sysemu/dma.h"
Vincent Palatinb3946622017-01-10 11:59:55 +010038#include "sysemu/hw_accel.h"
Paolo Bonzini9c17d612012-12-17 18:20:04 +010039#include "sysemu/kvm.h"
Vincent Palatinb0cb0a62017-01-10 11:59:57 +010040#include "sysemu/hax.h"
Sergio Andres Gomez Del Realc97d6d22017-09-13 04:05:09 -050041#include "sysemu/hvf.h"
Justin Terry (VM)19306802018-01-22 13:07:49 -080042#include "sysemu/whpx.h"
Paolo Bonzini63c91552016-03-15 13:18:37 +010043#include "exec/exec-all.h"
Blue Swirl296af7c2010-03-29 19:23:50 +000044
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010045#include "qemu/thread.h"
Paolo Bonzini9c17d612012-12-17 18:20:04 +010046#include "sysemu/cpus.h"
47#include "sysemu/qtest.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010048#include "qemu/main-loop.h"
Markus Armbruster922a01a2018-02-01 12:18:46 +010049#include "qemu/option.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010050#include "qemu/bitmap.h"
Liu Ping Fancb365642013-09-25 14:20:58 +080051#include "qemu/seqlock.h"
KONRAD Frederic8d4e9142017-02-23 18:29:08 +000052#include "tcg.h"
Alexey Kardashevskiy9cb805f2014-08-20 22:16:33 +100053#include "hw/nmi.h"
Pavel Dovgalyuk8b427042015-09-17 19:24:05 +030054#include "sysemu/replay.h"
Igor Mammedovafed5a52017-05-10 13:29:55 +020055#include "hw/boards.h"
Jan Kiszka0ff0fc12011-06-23 10:15:55 +020056
Jan Kiszka6d9cb732011-02-01 22:15:58 +010057#ifdef CONFIG_LINUX
58
59#include <sys/prctl.h>
60
Marcelo Tosattic0532a72010-10-11 15:31:21 -030061#ifndef PR_MCE_KILL
62#define PR_MCE_KILL 33
63#endif
64
Jan Kiszka6d9cb732011-02-01 22:15:58 +010065#ifndef PR_MCE_KILL_SET
66#define PR_MCE_KILL_SET 1
67#endif
68
69#ifndef PR_MCE_KILL_EARLY
70#define PR_MCE_KILL_EARLY 1
71#endif
72
73#endif /* CONFIG_LINUX */
74
Sebastian Tanase27498be2014-07-25 11:56:33 +020075int64_t max_delay;
76int64_t max_advance;
Blue Swirl296af7c2010-03-29 19:23:50 +000077
Jason J. Herne2adcc852015-09-08 13:12:33 -040078/* vcpu throttling controls */
79static QEMUTimer *throttle_timer;
80static unsigned int throttle_percentage;
81
82#define CPU_THROTTLE_PCT_MIN 1
83#define CPU_THROTTLE_PCT_MAX 99
84#define CPU_THROTTLE_TIMESLICE_NS 10000000
85
Tiejun Chen321bc0b2013-08-02 09:43:09 +080086bool cpu_is_stopped(CPUState *cpu)
87{
88 return cpu->stopped || !runstate_is_running();
89}
90
Andreas Färbera98ae1d2013-05-26 23:21:08 +020091static bool cpu_thread_is_idle(CPUState *cpu)
Peter Maydellac873f12012-07-19 16:52:27 +010092{
Andreas Färberc64ca812012-05-03 02:11:45 +020093 if (cpu->stop || cpu->queued_work_first) {
Peter Maydellac873f12012-07-19 16:52:27 +010094 return false;
95 }
Tiejun Chen321bc0b2013-08-02 09:43:09 +080096 if (cpu_is_stopped(cpu)) {
Peter Maydellac873f12012-07-19 16:52:27 +010097 return true;
98 }
Andreas Färber8c2e1b02013-08-25 18:53:55 +020099 if (!cpu->halted || cpu_has_work(cpu) ||
Alexander Graf215e79c2013-04-24 22:24:12 +0200100 kvm_halt_in_kernel()) {
Peter Maydellac873f12012-07-19 16:52:27 +0100101 return false;
102 }
103 return true;
104}
105
106static bool all_cpu_threads_idle(void)
107{
Andreas Färber182735e2013-05-29 22:29:20 +0200108 CPUState *cpu;
Peter Maydellac873f12012-07-19 16:52:27 +0100109
Andreas Färberbdc44642013-06-24 23:50:24 +0200110 CPU_FOREACH(cpu) {
Andreas Färber182735e2013-05-29 22:29:20 +0200111 if (!cpu_thread_is_idle(cpu)) {
Peter Maydellac873f12012-07-19 16:52:27 +0100112 return false;
113 }
114 }
115 return true;
116}
117
Blue Swirl296af7c2010-03-29 19:23:50 +0000118/***********************************************************/
Paolo Bonzini946fb272011-09-12 13:57:37 +0200119/* guest cycle counter */
120
Paolo Bonzinia3270e12013-10-07 17:18:15 +0200121/* Protected by TimersState seqlock */
122
Victor CLEMENT5045e9d92015-05-29 17:14:04 +0200123static bool icount_sleep = true;
Paolo Bonzini946fb272011-09-12 13:57:37 +0200124/* 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 +0200127typedef struct TimersState {
Liu Ping Fancb365642013-09-25 14:20:58 +0800128 /* Protected by BQL. */
Paolo Bonzini946fb272011-09-12 13:57:37 +0200129 int64_t cpu_ticks_prev;
130 int64_t cpu_ticks_offset;
Liu Ping Fancb365642013-09-25 14:20:58 +0800131
Paolo Bonzini94377112018-08-14 09:57:16 +0200132 /* Protect fields that can be respectively read outside the
133 * BQL, and written from multiple threads.
Liu Ping Fancb365642013-09-25 14:20:58 +0800134 */
135 QemuSeqLock vm_clock_seqlock;
Paolo Bonzini94377112018-08-14 09:57:16 +0200136 QemuSpin vm_clock_lock;
137
138 int16_t cpu_ticks_enabled;
KONRAD Fredericc96778b2014-08-01 01:37:09 +0200139
Paolo Bonzinic1ff0732018-08-14 09:31:58 +0200140 /* Conversion factor from emulated instructions to virtual clock ticks. */
Paolo Bonzini94377112018-08-14 09:57:16 +0200141 int16_t icount_time_shift;
142
KONRAD Fredericc96778b2014-08-01 01:37:09 +0200143 /* Compensate for varying guest execution speed. */
144 int64_t qemu_icount_bias;
Paolo Bonzini94377112018-08-14 09:57:16 +0200145
146 int64_t vm_clock_warp_start;
147 int64_t cpu_clock_offset;
148
KONRAD Fredericc96778b2014-08-01 01:37:09 +0200149 /* Only written by TCG thread */
150 int64_t qemu_icount;
Paolo Bonzini94377112018-08-14 09:57:16 +0200151
Pavel Dovgalyukb39e3f32018-01-11 11:26:10 +0300152 /* for adjusting icount */
Pavel Dovgalyukb39e3f32018-01-11 11:26:10 +0300153 QEMUTimer *icount_rt_timer;
154 QEMUTimer *icount_vm_timer;
155 QEMUTimer *icount_warp_timer;
Paolo Bonzini946fb272011-09-12 13:57:37 +0200156} TimersState;
157
Liu Ping Fand9cd4002013-07-21 08:43:00 +0000158static TimersState timers_state;
KONRAD Frederic8d4e9142017-02-23 18:29:08 +0000159bool mttcg_enabled;
160
161/*
162 * We default to false if we know other options have been enabled
163 * which are currently incompatible with MTTCG. Otherwise when each
164 * guest (target) has been updated to support:
165 * - atomic instructions
166 * - memory ordering primitives (barriers)
167 * they can set the appropriate CONFIG flags in ${target}-softmmu.mak
168 *
169 * Once a guest architecture has been converted to the new primitives
170 * there are two remaining limitations to check.
171 *
172 * - The guest can't be oversized (e.g. 64 bit guest on 32 bit host)
173 * - The host must have a stronger memory order than the guest
174 *
175 * It may be possible in future to support strong guests on weak hosts
176 * but that will require tagging all load/stores in a guest with their
177 * implicit memory order requirements which would likely slow things
178 * down a lot.
179 */
180
181static bool check_tcg_memory_orders_compatible(void)
182{
183#if defined(TCG_GUEST_DEFAULT_MO) && defined(TCG_TARGET_DEFAULT_MO)
184 return (TCG_GUEST_DEFAULT_MO & ~TCG_TARGET_DEFAULT_MO) == 0;
185#else
186 return false;
187#endif
188}
189
190static bool default_mttcg_enabled(void)
191{
Alex Bennée83fd9622017-02-27 17:09:01 +0000192 if (use_icount || TCG_OVERSIZED_GUEST) {
KONRAD Frederic8d4e9142017-02-23 18:29:08 +0000193 return false;
194 } else {
195#ifdef TARGET_SUPPORTS_MTTCG
196 return check_tcg_memory_orders_compatible();
197#else
198 return false;
199#endif
200 }
201}
202
203void qemu_tcg_configure(QemuOpts *opts, Error **errp)
204{
205 const char *t = qemu_opt_get(opts, "thread");
206 if (t) {
207 if (strcmp(t, "multi") == 0) {
208 if (TCG_OVERSIZED_GUEST) {
209 error_setg(errp, "No MTTCG when guest word size > hosts");
Alex Bennée83fd9622017-02-27 17:09:01 +0000210 } else if (use_icount) {
211 error_setg(errp, "No MTTCG when icount is enabled");
KONRAD Frederic8d4e9142017-02-23 18:29:08 +0000212 } else {
Nikunj A Dadhania86953502017-04-10 11:36:55 +0530213#ifndef TARGET_SUPPORTS_MTTCG
Markus Armbruster07656912018-10-17 10:26:28 +0200214 warn_report("Guest not yet converted to MTTCG - "
215 "you may get unexpected results");
Alex Bennéec34c7622017-02-28 14:40:17 +0000216#endif
KONRAD Frederic8d4e9142017-02-23 18:29:08 +0000217 if (!check_tcg_memory_orders_compatible()) {
Markus Armbruster07656912018-10-17 10:26:28 +0200218 warn_report("Guest expects a stronger memory ordering "
219 "than the host provides");
Pranith Kumar8cfef892017-03-25 16:19:23 -0400220 error_printf("This may cause strange/hard to debug errors\n");
KONRAD Frederic8d4e9142017-02-23 18:29:08 +0000221 }
222 mttcg_enabled = true;
223 }
224 } else if (strcmp(t, "single") == 0) {
225 mttcg_enabled = false;
226 } else {
227 error_setg(errp, "Invalid 'thread' setting %s", t);
228 }
229 } else {
230 mttcg_enabled = default_mttcg_enabled();
231 }
232}
Paolo Bonzini946fb272011-09-12 13:57:37 +0200233
Alex Bennéee4cd9652017-03-31 16:09:42 +0100234/* The current number of executed instructions is based on what we
235 * originally budgeted minus the current state of the decrementing
236 * icount counters in extra/u16.low.
237 */
238static int64_t cpu_get_icount_executed(CPUState *cpu)
239{
240 return cpu->icount_budget - (cpu->icount_decr.u16.low + cpu->icount_extra);
241}
242
Alex Bennée512d3c82017-04-05 12:32:37 +0100243/*
244 * Update the global shared timer_state.qemu_icount to take into
245 * account executed instructions. This is done by the TCG vCPU
246 * thread so the main-loop can see time has moved forward.
247 */
Paolo Bonzini9b4e6f42018-09-11 13:15:32 +0200248static void cpu_update_icount_locked(CPUState *cpu)
Alex Bennée512d3c82017-04-05 12:32:37 +0100249{
250 int64_t executed = cpu_get_icount_executed(cpu);
251 cpu->icount_budget -= executed;
252
Emilio G. Cota38adcb62018-09-10 19:27:49 -0400253 atomic_set_i64(&timers_state.qemu_icount,
254 timers_state.qemu_icount + executed);
Paolo Bonzini9b4e6f42018-09-11 13:15:32 +0200255}
256
257/*
258 * Update the global shared timer_state.qemu_icount to take into
259 * account executed instructions. This is done by the TCG vCPU
260 * thread so the main-loop can see time has moved forward.
261 */
262void cpu_update_icount(CPUState *cpu)
263{
264 seqlock_write_lock(&timers_state.vm_clock_seqlock,
265 &timers_state.vm_clock_lock);
266 cpu_update_icount_locked(cpu);
Paolo Bonzini94377112018-08-14 09:57:16 +0200267 seqlock_write_unlock(&timers_state.vm_clock_seqlock,
268 &timers_state.vm_clock_lock);
Alex Bennée512d3c82017-04-05 12:32:37 +0100269}
270
Paolo Bonzinic1ff0732018-08-14 09:31:58 +0200271static int64_t cpu_get_icount_raw_locked(void)
Paolo Bonzini946fb272011-09-12 13:57:37 +0200272{
Andreas Färber4917cf42013-05-27 05:17:50 +0200273 CPUState *cpu = current_cpu;
Paolo Bonzini946fb272011-09-12 13:57:37 +0200274
Alex Bennée243c5f72017-03-30 18:49:22 +0100275 if (cpu && cpu->running) {
Paolo Bonzini414b15c2015-06-24 14:16:26 +0200276 if (!cpu->can_do_io) {
Alistair Francis493d89b2018-02-03 09:43:14 +0100277 error_report("Bad icount read");
Pavel Dovgalyuk2a629142014-12-08 10:53:45 +0300278 exit(1);
Paolo Bonzini946fb272011-09-12 13:57:37 +0200279 }
Alex Bennéee4cd9652017-03-31 16:09:42 +0100280 /* Take into account what has run */
Paolo Bonzini9b4e6f42018-09-11 13:15:32 +0200281 cpu_update_icount_locked(cpu);
Paolo Bonzini946fb272011-09-12 13:57:37 +0200282 }
Emilio G. Cota38adcb62018-09-10 19:27:49 -0400283 /* The read is protected by the seqlock, but needs atomic64 to avoid UB */
284 return atomic_read_i64(&timers_state.qemu_icount);
Paolo Bonzinic1ff0732018-08-14 09:31:58 +0200285}
286
287static int64_t cpu_get_icount_locked(void)
288{
289 int64_t icount = cpu_get_icount_raw_locked();
Emilio G. Cotac97595d2018-09-10 19:27:50 -0400290 return atomic_read_i64(&timers_state.qemu_icount_bias) +
291 cpu_icount_to_ns(icount);
Paolo Bonzinic1ff0732018-08-14 09:31:58 +0200292}
293
294int64_t cpu_get_icount_raw(void)
295{
296 int64_t icount;
297 unsigned start;
298
299 do {
300 start = seqlock_read_begin(&timers_state.vm_clock_seqlock);
301 icount = cpu_get_icount_raw_locked();
302 } while (seqlock_read_retry(&timers_state.vm_clock_seqlock, start));
303
304 return icount;
Pavel Dovgalyuk2a629142014-12-08 10:53:45 +0300305}
306
307/* Return the virtual CPU time, based on the instruction counter. */
Paolo Bonzini17a15f12013-10-03 15:17:25 +0200308int64_t cpu_get_icount(void)
309{
310 int64_t icount;
311 unsigned start;
312
313 do {
314 start = seqlock_read_begin(&timers_state.vm_clock_seqlock);
315 icount = cpu_get_icount_locked();
316 } while (seqlock_read_retry(&timers_state.vm_clock_seqlock, start));
317
318 return icount;
319}
320
KONRAD Frederic3f031312014-08-01 01:37:15 +0200321int64_t cpu_icount_to_ns(int64_t icount)
322{
Paolo Bonzinic1ff0732018-08-14 09:31:58 +0200323 return icount << atomic_read(&timers_state.icount_time_shift);
KONRAD Frederic3f031312014-08-01 01:37:15 +0200324}
325
Paolo Bonzinif2a4ad62018-08-18 09:36:16 +0200326static int64_t cpu_get_ticks_locked(void)
327{
328 int64_t ticks = timers_state.cpu_ticks_offset;
329 if (timers_state.cpu_ticks_enabled) {
330 ticks += cpu_get_host_ticks();
331 }
332
333 if (timers_state.cpu_ticks_prev > ticks) {
334 /* Non increasing ticks may happen if the host uses software suspend. */
335 timers_state.cpu_ticks_offset += timers_state.cpu_ticks_prev - ticks;
336 ticks = timers_state.cpu_ticks_prev;
337 }
338
339 timers_state.cpu_ticks_prev = ticks;
340 return ticks;
341}
342
Cao jind90f3cc2016-07-29 19:05:38 +0800343/* return the time elapsed in VM between vm_start and vm_stop. Unless
344 * icount is active, cpu_get_ticks() uses units of the host CPU cycle
345 * counter.
Cao jind90f3cc2016-07-29 19:05:38 +0800346 */
Paolo Bonzini946fb272011-09-12 13:57:37 +0200347int64_t cpu_get_ticks(void)
348{
Paolo Bonzini5f3e3102013-10-28 17:32:18 +0100349 int64_t ticks;
350
Paolo Bonzini946fb272011-09-12 13:57:37 +0200351 if (use_icount) {
352 return cpu_get_icount();
353 }
Paolo Bonzini5f3e3102013-10-28 17:32:18 +0100354
Paolo Bonzinif2a4ad62018-08-18 09:36:16 +0200355 qemu_spin_lock(&timers_state.vm_clock_lock);
356 ticks = cpu_get_ticks_locked();
357 qemu_spin_unlock(&timers_state.vm_clock_lock);
Paolo Bonzini5f3e3102013-10-28 17:32:18 +0100358 return ticks;
Paolo Bonzini946fb272011-09-12 13:57:37 +0200359}
360
Liu Ping Fancb365642013-09-25 14:20:58 +0800361static int64_t cpu_get_clock_locked(void)
362{
Cao jin1d45cea2016-07-29 19:05:37 +0800363 int64_t time;
Liu Ping Fancb365642013-09-25 14:20:58 +0800364
Cao jin1d45cea2016-07-29 19:05:37 +0800365 time = timers_state.cpu_clock_offset;
Paolo Bonzini5f3e3102013-10-28 17:32:18 +0100366 if (timers_state.cpu_ticks_enabled) {
Cao jin1d45cea2016-07-29 19:05:37 +0800367 time += get_clock();
Liu Ping Fancb365642013-09-25 14:20:58 +0800368 }
369
Cao jin1d45cea2016-07-29 19:05:37 +0800370 return time;
Liu Ping Fancb365642013-09-25 14:20:58 +0800371}
372
Cao jind90f3cc2016-07-29 19:05:38 +0800373/* Return the monotonic time elapsed in VM, i.e.,
Peter Maydell8212ff82016-09-15 10:24:22 +0100374 * the time between vm_start and vm_stop
375 */
Paolo Bonzini946fb272011-09-12 13:57:37 +0200376int64_t cpu_get_clock(void)
377{
378 int64_t ti;
Liu Ping Fancb365642013-09-25 14:20:58 +0800379 unsigned start;
380
381 do {
382 start = seqlock_read_begin(&timers_state.vm_clock_seqlock);
383 ti = cpu_get_clock_locked();
384 } while (seqlock_read_retry(&timers_state.vm_clock_seqlock, start));
385
386 return ti;
Paolo Bonzini946fb272011-09-12 13:57:37 +0200387}
388
Liu Ping Fancb365642013-09-25 14:20:58 +0800389/* enable cpu_get_ticks()
Cao jin3224e872016-07-08 18:31:37 +0800390 * Caller must hold BQL which serves as mutex for vm_clock_seqlock.
Liu Ping Fancb365642013-09-25 14:20:58 +0800391 */
Paolo Bonzini946fb272011-09-12 13:57:37 +0200392void cpu_enable_ticks(void)
393{
Paolo Bonzini94377112018-08-14 09:57:16 +0200394 seqlock_write_lock(&timers_state.vm_clock_seqlock,
395 &timers_state.vm_clock_lock);
Paolo Bonzini946fb272011-09-12 13:57:37 +0200396 if (!timers_state.cpu_ticks_enabled) {
Christopher Covington4a7428c2015-09-25 10:42:21 -0400397 timers_state.cpu_ticks_offset -= cpu_get_host_ticks();
Paolo Bonzini946fb272011-09-12 13:57:37 +0200398 timers_state.cpu_clock_offset -= get_clock();
399 timers_state.cpu_ticks_enabled = 1;
400 }
Paolo Bonzini94377112018-08-14 09:57:16 +0200401 seqlock_write_unlock(&timers_state.vm_clock_seqlock,
402 &timers_state.vm_clock_lock);
Paolo Bonzini946fb272011-09-12 13:57:37 +0200403}
404
405/* disable cpu_get_ticks() : the clock is stopped. You must not call
Liu Ping Fancb365642013-09-25 14:20:58 +0800406 * cpu_get_ticks() after that.
Cao jin3224e872016-07-08 18:31:37 +0800407 * Caller must hold BQL which serves as mutex for vm_clock_seqlock.
Liu Ping Fancb365642013-09-25 14:20:58 +0800408 */
Paolo Bonzini946fb272011-09-12 13:57:37 +0200409void cpu_disable_ticks(void)
410{
Paolo Bonzini94377112018-08-14 09:57:16 +0200411 seqlock_write_lock(&timers_state.vm_clock_seqlock,
412 &timers_state.vm_clock_lock);
Paolo Bonzini946fb272011-09-12 13:57:37 +0200413 if (timers_state.cpu_ticks_enabled) {
Christopher Covington4a7428c2015-09-25 10:42:21 -0400414 timers_state.cpu_ticks_offset += cpu_get_host_ticks();
Liu Ping Fancb365642013-09-25 14:20:58 +0800415 timers_state.cpu_clock_offset = cpu_get_clock_locked();
Paolo Bonzini946fb272011-09-12 13:57:37 +0200416 timers_state.cpu_ticks_enabled = 0;
417 }
Paolo Bonzini94377112018-08-14 09:57:16 +0200418 seqlock_write_unlock(&timers_state.vm_clock_seqlock,
419 &timers_state.vm_clock_lock);
Paolo Bonzini946fb272011-09-12 13:57:37 +0200420}
421
422/* Correlation between real and virtual time is always going to be
423 fairly approximate, so ignore small variation.
424 When the guest is idle real and virtual time will be aligned in
425 the IO wait loop. */
Rutuja Shah73bcb242016-03-21 21:32:30 +0530426#define ICOUNT_WOBBLE (NANOSECONDS_PER_SECOND / 10)
Paolo Bonzini946fb272011-09-12 13:57:37 +0200427
428static void icount_adjust(void)
429{
430 int64_t cur_time;
431 int64_t cur_icount;
432 int64_t delta;
Paolo Bonzinia3270e12013-10-07 17:18:15 +0200433
434 /* Protected by TimersState mutex. */
Paolo Bonzini946fb272011-09-12 13:57:37 +0200435 static int64_t last_delta;
Paolo Bonzini468cc7c2013-10-07 17:21:51 +0200436
Paolo Bonzini946fb272011-09-12 13:57:37 +0200437 /* If the VM is not running, then do nothing. */
438 if (!runstate_is_running()) {
439 return;
440 }
Paolo Bonzini468cc7c2013-10-07 17:21:51 +0200441
Paolo Bonzini94377112018-08-14 09:57:16 +0200442 seqlock_write_lock(&timers_state.vm_clock_seqlock,
443 &timers_state.vm_clock_lock);
Paolo Bonzini17a15f12013-10-03 15:17:25 +0200444 cur_time = cpu_get_clock_locked();
445 cur_icount = cpu_get_icount_locked();
Paolo Bonzini468cc7c2013-10-07 17:21:51 +0200446
Paolo Bonzini946fb272011-09-12 13:57:37 +0200447 delta = cur_icount - cur_time;
448 /* FIXME: This is a very crude algorithm, somewhat prone to oscillation. */
449 if (delta > 0
450 && last_delta + ICOUNT_WOBBLE < delta * 2
Paolo Bonzinic1ff0732018-08-14 09:31:58 +0200451 && timers_state.icount_time_shift > 0) {
Paolo Bonzini946fb272011-09-12 13:57:37 +0200452 /* The guest is getting too far ahead. Slow time down. */
Paolo Bonzinic1ff0732018-08-14 09:31:58 +0200453 atomic_set(&timers_state.icount_time_shift,
454 timers_state.icount_time_shift - 1);
Paolo Bonzini946fb272011-09-12 13:57:37 +0200455 }
456 if (delta < 0
457 && last_delta - ICOUNT_WOBBLE > delta * 2
Paolo Bonzinic1ff0732018-08-14 09:31:58 +0200458 && timers_state.icount_time_shift < MAX_ICOUNT_SHIFT) {
Paolo Bonzini946fb272011-09-12 13:57:37 +0200459 /* The guest is getting too far behind. Speed time up. */
Paolo Bonzinic1ff0732018-08-14 09:31:58 +0200460 atomic_set(&timers_state.icount_time_shift,
461 timers_state.icount_time_shift + 1);
Paolo Bonzini946fb272011-09-12 13:57:37 +0200462 }
463 last_delta = delta;
Emilio G. Cotac97595d2018-09-10 19:27:50 -0400464 atomic_set_i64(&timers_state.qemu_icount_bias,
465 cur_icount - (timers_state.qemu_icount
466 << timers_state.icount_time_shift));
Paolo Bonzini94377112018-08-14 09:57:16 +0200467 seqlock_write_unlock(&timers_state.vm_clock_seqlock,
468 &timers_state.vm_clock_lock);
Paolo Bonzini946fb272011-09-12 13:57:37 +0200469}
470
471static void icount_adjust_rt(void *opaque)
472{
Pavel Dovgalyukb39e3f32018-01-11 11:26:10 +0300473 timer_mod(timers_state.icount_rt_timer,
Pavel Dovgalyuk1979b902015-01-12 15:00:43 +0300474 qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL_RT) + 1000);
Paolo Bonzini946fb272011-09-12 13:57:37 +0200475 icount_adjust();
476}
477
478static void icount_adjust_vm(void *opaque)
479{
Pavel Dovgalyukb39e3f32018-01-11 11:26:10 +0300480 timer_mod(timers_state.icount_vm_timer,
Alex Bligh40daca52013-08-21 16:03:02 +0100481 qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) +
Rutuja Shah73bcb242016-03-21 21:32:30 +0530482 NANOSECONDS_PER_SECOND / 10);
Paolo Bonzini946fb272011-09-12 13:57:37 +0200483 icount_adjust();
484}
485
486static int64_t qemu_icount_round(int64_t count)
487{
Paolo Bonzinic1ff0732018-08-14 09:31:58 +0200488 int shift = atomic_read(&timers_state.icount_time_shift);
489 return (count + (1 << shift) - 1) >> shift;
Paolo Bonzini946fb272011-09-12 13:57:37 +0200490}
491
Pavel Dovgalyukefab87c2015-09-17 19:24:39 +0300492static void icount_warp_rt(void)
Paolo Bonzini946fb272011-09-12 13:57:37 +0200493{
Alex Bennéeccffff42016-04-04 15:35:48 +0100494 unsigned seq;
495 int64_t warp_start;
496
Paolo Bonzini17a15f12013-10-03 15:17:25 +0200497 /* The icount_warp_timer is rescheduled soon after vm_clock_warp_start
498 * changes from -1 to another value, so the race here is okay.
499 */
Alex Bennéeccffff42016-04-04 15:35:48 +0100500 do {
501 seq = seqlock_read_begin(&timers_state.vm_clock_seqlock);
Pavel Dovgalyukb39e3f32018-01-11 11:26:10 +0300502 warp_start = timers_state.vm_clock_warp_start;
Alex Bennéeccffff42016-04-04 15:35:48 +0100503 } while (seqlock_read_retry(&timers_state.vm_clock_seqlock, seq));
504
505 if (warp_start == -1) {
Paolo Bonzini946fb272011-09-12 13:57:37 +0200506 return;
507 }
508
Paolo Bonzini94377112018-08-14 09:57:16 +0200509 seqlock_write_lock(&timers_state.vm_clock_seqlock,
510 &timers_state.vm_clock_lock);
Paolo Bonzini946fb272011-09-12 13:57:37 +0200511 if (runstate_is_running()) {
Paolo Bonzini74c0b812018-10-08 13:24:14 +0200512 int64_t clock = REPLAY_CLOCK_LOCKED(REPLAY_CLOCK_VIRTUAL_RT,
513 cpu_get_clock_locked());
Paolo Bonzini8ed961d2013-10-07 17:26:07 +0200514 int64_t warp_delta;
515
Pavel Dovgalyukb39e3f32018-01-11 11:26:10 +0300516 warp_delta = clock - timers_state.vm_clock_warp_start;
Paolo Bonzini8ed961d2013-10-07 17:26:07 +0200517 if (use_icount == 2) {
Paolo Bonzini946fb272011-09-12 13:57:37 +0200518 /*
Alex Bligh40daca52013-08-21 16:03:02 +0100519 * In adaptive mode, do not let QEMU_CLOCK_VIRTUAL run too
Paolo Bonzini946fb272011-09-12 13:57:37 +0200520 * far ahead of real time.
521 */
Paolo Bonzini17a15f12013-10-03 15:17:25 +0200522 int64_t cur_icount = cpu_get_icount_locked();
Pavel Dovgalyukbf2a7dd2014-11-26 13:40:55 +0300523 int64_t delta = clock - cur_icount;
Paolo Bonzini8ed961d2013-10-07 17:26:07 +0200524 warp_delta = MIN(warp_delta, delta);
Paolo Bonzini946fb272011-09-12 13:57:37 +0200525 }
Emilio G. Cotac97595d2018-09-10 19:27:50 -0400526 atomic_set_i64(&timers_state.qemu_icount_bias,
527 timers_state.qemu_icount_bias + warp_delta);
Paolo Bonzini946fb272011-09-12 13:57:37 +0200528 }
Pavel Dovgalyukb39e3f32018-01-11 11:26:10 +0300529 timers_state.vm_clock_warp_start = -1;
Paolo Bonzini94377112018-08-14 09:57:16 +0200530 seqlock_write_unlock(&timers_state.vm_clock_seqlock,
531 &timers_state.vm_clock_lock);
Paolo Bonzini8ed961d2013-10-07 17:26:07 +0200532
533 if (qemu_clock_expired(QEMU_CLOCK_VIRTUAL)) {
534 qemu_clock_notify(QEMU_CLOCK_VIRTUAL);
535 }
Paolo Bonzini946fb272011-09-12 13:57:37 +0200536}
537
Pavel Dovgalyuke76d1792016-03-10 14:56:09 +0300538static void icount_timer_cb(void *opaque)
Pavel Dovgalyukefab87c2015-09-17 19:24:39 +0300539{
Pavel Dovgalyuke76d1792016-03-10 14:56:09 +0300540 /* No need for a checkpoint because the timer already synchronizes
541 * with CHECKPOINT_CLOCK_VIRTUAL_RT.
542 */
543 icount_warp_rt();
Pavel Dovgalyukefab87c2015-09-17 19:24:39 +0300544}
545
Paolo Bonzini8156be52012-03-28 15:42:04 +0200546void qtest_clock_warp(int64_t dest)
547{
Alex Bligh40daca52013-08-21 16:03:02 +0100548 int64_t clock = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
Fam Zhengefef88b2015-01-19 17:51:43 +0800549 AioContext *aio_context;
Paolo Bonzini8156be52012-03-28 15:42:04 +0200550 assert(qtest_enabled());
Fam Zhengefef88b2015-01-19 17:51:43 +0800551 aio_context = qemu_get_aio_context();
Paolo Bonzini8156be52012-03-28 15:42:04 +0200552 while (clock < dest) {
Alex Bligh40daca52013-08-21 16:03:02 +0100553 int64_t deadline = qemu_clock_deadline_ns_all(QEMU_CLOCK_VIRTUAL);
Sergey Fedorovc9299e22014-06-10 13:10:28 +0400554 int64_t warp = qemu_soonest_timeout(dest - clock, deadline);
Fam Zhengefef88b2015-01-19 17:51:43 +0800555
Paolo Bonzini94377112018-08-14 09:57:16 +0200556 seqlock_write_lock(&timers_state.vm_clock_seqlock,
557 &timers_state.vm_clock_lock);
Emilio G. Cotac97595d2018-09-10 19:27:50 -0400558 atomic_set_i64(&timers_state.qemu_icount_bias,
559 timers_state.qemu_icount_bias + warp);
Paolo Bonzini94377112018-08-14 09:57:16 +0200560 seqlock_write_unlock(&timers_state.vm_clock_seqlock,
561 &timers_state.vm_clock_lock);
Paolo Bonzini17a15f12013-10-03 15:17:25 +0200562
Alex Bligh40daca52013-08-21 16:03:02 +0100563 qemu_clock_run_timers(QEMU_CLOCK_VIRTUAL);
Fam Zhengefef88b2015-01-19 17:51:43 +0800564 timerlist_run_timers(aio_context->tlg.tl[QEMU_CLOCK_VIRTUAL]);
Alex Bligh40daca52013-08-21 16:03:02 +0100565 clock = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
Paolo Bonzini8156be52012-03-28 15:42:04 +0200566 }
Alex Bligh40daca52013-08-21 16:03:02 +0100567 qemu_clock_notify(QEMU_CLOCK_VIRTUAL);
Paolo Bonzini8156be52012-03-28 15:42:04 +0200568}
569
Pavel Dovgalyuke76d1792016-03-10 14:56:09 +0300570void qemu_start_warp_timer(void)
Paolo Bonzini946fb272011-09-12 13:57:37 +0200571{
Paolo Bonzinice78d182013-10-07 17:30:02 +0200572 int64_t clock;
Paolo Bonzini946fb272011-09-12 13:57:37 +0200573 int64_t deadline;
574
Pavel Dovgalyuke76d1792016-03-10 14:56:09 +0300575 if (!use_icount) {
Paolo Bonzini946fb272011-09-12 13:57:37 +0200576 return;
577 }
578
Pavel Dovgalyuk8bd7f712015-09-17 19:24:44 +0300579 /* Nothing to do if the VM is stopped: QEMU_CLOCK_VIRTUAL timers
580 * do not fire, so computing the deadline does not make sense.
581 */
582 if (!runstate_is_running()) {
583 return;
584 }
585
Pavel Dovgalyuk0c081852018-09-12 11:19:45 +0300586 if (replay_mode != REPLAY_MODE_PLAY) {
587 if (!all_cpu_threads_idle()) {
588 return;
589 }
Pavel Dovgalyuk8bd7f712015-09-17 19:24:44 +0300590
Pavel Dovgalyuk0c081852018-09-12 11:19:45 +0300591 if (qtest_enabled()) {
592 /* When testing, qtest commands advance icount. */
593 return;
594 }
Paolo Bonzini946fb272011-09-12 13:57:37 +0200595
Pavel Dovgalyuk0c081852018-09-12 11:19:45 +0300596 replay_checkpoint(CHECKPOINT_CLOCK_WARP_START);
597 } else {
598 /* warp clock deterministically in record/replay mode */
599 if (!replay_checkpoint(CHECKPOINT_CLOCK_WARP_START)) {
600 /* vCPU is sleeping and warp can't be started.
601 It is probably a race condition: notification sent
602 to vCPU was processed in advance and vCPU went to sleep.
603 Therefore we have to wake it up for doing someting. */
604 if (replay_has_checkpoint()) {
605 qemu_clock_notify(QEMU_CLOCK_VIRTUAL);
606 }
607 return;
608 }
Paolo Bonzini8156be52012-03-28 15:42:04 +0200609 }
610
Alex Blighac70aaf2013-08-21 16:02:57 +0100611 /* We want to use the earliest deadline from ALL vm_clocks */
Pavel Dovgalyukbf2a7dd2014-11-26 13:40:55 +0300612 clock = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL_RT);
Alex Bligh40daca52013-08-21 16:03:02 +0100613 deadline = qemu_clock_deadline_ns_all(QEMU_CLOCK_VIRTUAL);
Paolo Bonzinice78d182013-10-07 17:30:02 +0200614 if (deadline < 0) {
Victor CLEMENTd7a0f712015-05-29 17:14:06 +0200615 static bool notified;
616 if (!icount_sleep && !notified) {
Alistair Francis3dc6f862017-07-12 06:57:41 -0700617 warn_report("icount sleep disabled and no active timers");
Victor CLEMENTd7a0f712015-05-29 17:14:06 +0200618 notified = true;
619 }
Paolo Bonzinice78d182013-10-07 17:30:02 +0200620 return;
Alex Blighac70aaf2013-08-21 16:02:57 +0100621 }
622
Paolo Bonzini946fb272011-09-12 13:57:37 +0200623 if (deadline > 0) {
624 /*
Alex Bligh40daca52013-08-21 16:03:02 +0100625 * Ensure QEMU_CLOCK_VIRTUAL proceeds even when the virtual CPU goes to
Paolo Bonzini946fb272011-09-12 13:57:37 +0200626 * sleep. Otherwise, the CPU might be waiting for a future timer
627 * interrupt to wake it up, but the interrupt never comes because
628 * the vCPU isn't running any insns and thus doesn't advance the
Alex Bligh40daca52013-08-21 16:03:02 +0100629 * QEMU_CLOCK_VIRTUAL.
Paolo Bonzini946fb272011-09-12 13:57:37 +0200630 */
Victor CLEMENT5045e9d92015-05-29 17:14:04 +0200631 if (!icount_sleep) {
632 /*
633 * We never let VCPUs sleep in no sleep icount mode.
634 * If there is a pending QEMU_CLOCK_VIRTUAL timer we just advance
635 * to the next QEMU_CLOCK_VIRTUAL event and notify it.
636 * It is useful when we want a deterministic execution time,
637 * isolated from host latencies.
638 */
Paolo Bonzini94377112018-08-14 09:57:16 +0200639 seqlock_write_lock(&timers_state.vm_clock_seqlock,
640 &timers_state.vm_clock_lock);
Emilio G. Cotac97595d2018-09-10 19:27:50 -0400641 atomic_set_i64(&timers_state.qemu_icount_bias,
642 timers_state.qemu_icount_bias + deadline);
Paolo Bonzini94377112018-08-14 09:57:16 +0200643 seqlock_write_unlock(&timers_state.vm_clock_seqlock,
644 &timers_state.vm_clock_lock);
Victor CLEMENT5045e9d92015-05-29 17:14:04 +0200645 qemu_clock_notify(QEMU_CLOCK_VIRTUAL);
646 } else {
647 /*
648 * We do stop VCPUs and only advance QEMU_CLOCK_VIRTUAL after some
649 * "real" time, (related to the time left until the next event) has
650 * passed. The QEMU_CLOCK_VIRTUAL_RT clock will do this.
651 * This avoids that the warps are visible externally; for example,
652 * you will not be sending network packets continuously instead of
653 * every 100ms.
654 */
Paolo Bonzini94377112018-08-14 09:57:16 +0200655 seqlock_write_lock(&timers_state.vm_clock_seqlock,
656 &timers_state.vm_clock_lock);
Pavel Dovgalyukb39e3f32018-01-11 11:26:10 +0300657 if (timers_state.vm_clock_warp_start == -1
658 || timers_state.vm_clock_warp_start > clock) {
659 timers_state.vm_clock_warp_start = clock;
Victor CLEMENT5045e9d92015-05-29 17:14:04 +0200660 }
Paolo Bonzini94377112018-08-14 09:57:16 +0200661 seqlock_write_unlock(&timers_state.vm_clock_seqlock,
662 &timers_state.vm_clock_lock);
Pavel Dovgalyukb39e3f32018-01-11 11:26:10 +0300663 timer_mod_anticipate(timers_state.icount_warp_timer,
664 clock + deadline);
Paolo Bonzinice78d182013-10-07 17:30:02 +0200665 }
Alex Blighac70aaf2013-08-21 16:02:57 +0100666 } else if (deadline == 0) {
Alex Bligh40daca52013-08-21 16:03:02 +0100667 qemu_clock_notify(QEMU_CLOCK_VIRTUAL);
Paolo Bonzini946fb272011-09-12 13:57:37 +0200668 }
669}
670
Pavel Dovgalyuke76d1792016-03-10 14:56:09 +0300671static void qemu_account_warp_timer(void)
672{
673 if (!use_icount || !icount_sleep) {
674 return;
675 }
676
677 /* Nothing to do if the VM is stopped: QEMU_CLOCK_VIRTUAL timers
678 * do not fire, so computing the deadline does not make sense.
679 */
680 if (!runstate_is_running()) {
681 return;
682 }
683
684 /* warp clock deterministically in record/replay mode */
685 if (!replay_checkpoint(CHECKPOINT_CLOCK_WARP_ACCOUNT)) {
686 return;
687 }
688
Pavel Dovgalyukb39e3f32018-01-11 11:26:10 +0300689 timer_del(timers_state.icount_warp_timer);
Pavel Dovgalyuke76d1792016-03-10 14:56:09 +0300690 icount_warp_rt();
691}
692
KONRAD Fredericd09eae32014-08-01 01:37:10 +0200693static bool icount_state_needed(void *opaque)
694{
695 return use_icount;
696}
697
Pavel Dovgalyukb39e3f32018-01-11 11:26:10 +0300698static bool warp_timer_state_needed(void *opaque)
699{
700 TimersState *s = opaque;
701 return s->icount_warp_timer != NULL;
702}
703
704static bool adjust_timers_state_needed(void *opaque)
705{
706 TimersState *s = opaque;
707 return s->icount_rt_timer != NULL;
708}
709
710/*
711 * Subsection for warp timer migration is optional, because may not be created
712 */
713static const VMStateDescription icount_vmstate_warp_timer = {
714 .name = "timer/icount/warp_timer",
715 .version_id = 1,
716 .minimum_version_id = 1,
717 .needed = warp_timer_state_needed,
718 .fields = (VMStateField[]) {
719 VMSTATE_INT64(vm_clock_warp_start, TimersState),
720 VMSTATE_TIMER_PTR(icount_warp_timer, TimersState),
721 VMSTATE_END_OF_LIST()
722 }
723};
724
725static const VMStateDescription icount_vmstate_adjust_timers = {
726 .name = "timer/icount/timers",
727 .version_id = 1,
728 .minimum_version_id = 1,
729 .needed = adjust_timers_state_needed,
730 .fields = (VMStateField[]) {
731 VMSTATE_TIMER_PTR(icount_rt_timer, TimersState),
732 VMSTATE_TIMER_PTR(icount_vm_timer, TimersState),
733 VMSTATE_END_OF_LIST()
734 }
735};
736
KONRAD Fredericd09eae32014-08-01 01:37:10 +0200737/*
738 * This is a subsection for icount migration.
739 */
740static const VMStateDescription icount_vmstate_timers = {
741 .name = "timer/icount",
742 .version_id = 1,
743 .minimum_version_id = 1,
Juan Quintela5cd8cad2014-09-23 14:09:54 +0200744 .needed = icount_state_needed,
KONRAD Fredericd09eae32014-08-01 01:37:10 +0200745 .fields = (VMStateField[]) {
746 VMSTATE_INT64(qemu_icount_bias, TimersState),
747 VMSTATE_INT64(qemu_icount, TimersState),
748 VMSTATE_END_OF_LIST()
Pavel Dovgalyukb39e3f32018-01-11 11:26:10 +0300749 },
750 .subsections = (const VMStateDescription*[]) {
751 &icount_vmstate_warp_timer,
752 &icount_vmstate_adjust_timers,
753 NULL
KONRAD Fredericd09eae32014-08-01 01:37:10 +0200754 }
755};
756
Paolo Bonzini946fb272011-09-12 13:57:37 +0200757static const VMStateDescription vmstate_timers = {
758 .name = "timer",
759 .version_id = 2,
760 .minimum_version_id = 1,
Juan Quintela35d08452014-04-16 16:01:33 +0200761 .fields = (VMStateField[]) {
Paolo Bonzini946fb272011-09-12 13:57:37 +0200762 VMSTATE_INT64(cpu_ticks_offset, TimersState),
Paolo Bonzinic1ff0732018-08-14 09:31:58 +0200763 VMSTATE_UNUSED(8),
Paolo Bonzini946fb272011-09-12 13:57:37 +0200764 VMSTATE_INT64_V(cpu_clock_offset, TimersState, 2),
765 VMSTATE_END_OF_LIST()
KONRAD Fredericd09eae32014-08-01 01:37:10 +0200766 },
Juan Quintela5cd8cad2014-09-23 14:09:54 +0200767 .subsections = (const VMStateDescription*[]) {
768 &icount_vmstate_timers,
769 NULL
Paolo Bonzini946fb272011-09-12 13:57:37 +0200770 }
771};
772
Paolo Bonzini14e6fe12016-10-31 10:36:08 +0100773static void cpu_throttle_thread(CPUState *cpu, run_on_cpu_data opaque)
Jason J. Herne2adcc852015-09-08 13:12:33 -0400774{
Jason J. Herne2adcc852015-09-08 13:12:33 -0400775 double pct;
776 double throttle_ratio;
777 long sleeptime_ns;
778
779 if (!cpu_throttle_get_percentage()) {
780 return;
781 }
782
783 pct = (double)cpu_throttle_get_percentage()/100;
784 throttle_ratio = pct / (1 - pct);
785 sleeptime_ns = (long)(throttle_ratio * CPU_THROTTLE_TIMESLICE_NS);
786
787 qemu_mutex_unlock_iothread();
Jason J. Herne2adcc852015-09-08 13:12:33 -0400788 g_usleep(sleeptime_ns / 1000); /* Convert ns to us for usleep call */
789 qemu_mutex_lock_iothread();
Felipe Franciosi90bb0c02017-05-19 22:29:50 +0100790 atomic_set(&cpu->throttle_thread_scheduled, 0);
Jason J. Herne2adcc852015-09-08 13:12:33 -0400791}
792
793static void cpu_throttle_timer_tick(void *opaque)
794{
795 CPUState *cpu;
796 double pct;
797
798 /* Stop the timer if needed */
799 if (!cpu_throttle_get_percentage()) {
800 return;
801 }
802 CPU_FOREACH(cpu) {
803 if (!atomic_xchg(&cpu->throttle_thread_scheduled, 1)) {
Paolo Bonzini14e6fe12016-10-31 10:36:08 +0100804 async_run_on_cpu(cpu, cpu_throttle_thread,
805 RUN_ON_CPU_NULL);
Jason J. Herne2adcc852015-09-08 13:12:33 -0400806 }
807 }
808
809 pct = (double)cpu_throttle_get_percentage()/100;
810 timer_mod(throttle_timer, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL_RT) +
811 CPU_THROTTLE_TIMESLICE_NS / (1-pct));
812}
813
814void cpu_throttle_set(int new_throttle_pct)
815{
816 /* Ensure throttle percentage is within valid range */
817 new_throttle_pct = MIN(new_throttle_pct, CPU_THROTTLE_PCT_MAX);
818 new_throttle_pct = MAX(new_throttle_pct, CPU_THROTTLE_PCT_MIN);
819
820 atomic_set(&throttle_percentage, new_throttle_pct);
821
822 timer_mod(throttle_timer, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL_RT) +
823 CPU_THROTTLE_TIMESLICE_NS);
824}
825
826void cpu_throttle_stop(void)
827{
828 atomic_set(&throttle_percentage, 0);
829}
830
831bool cpu_throttle_active(void)
832{
833 return (cpu_throttle_get_percentage() != 0);
834}
835
836int cpu_throttle_get_percentage(void)
837{
838 return atomic_read(&throttle_percentage);
839}
840
Pavel Dovgalyuk4603ea02014-09-01 09:34:49 +0400841void cpu_ticks_init(void)
842{
Emilio G. Cotaccdb3c12016-06-08 14:55:20 -0400843 seqlock_init(&timers_state.vm_clock_seqlock);
Emilio G. Cota87a09cd2018-09-03 13:18:29 -0400844 qemu_spin_init(&timers_state.vm_clock_lock);
Pavel Dovgalyuk4603ea02014-09-01 09:34:49 +0400845 vmstate_register(NULL, 0, &vmstate_timers, &timers_state);
Jason J. Herne2adcc852015-09-08 13:12:33 -0400846 throttle_timer = timer_new_ns(QEMU_CLOCK_VIRTUAL_RT,
847 cpu_throttle_timer_tick, NULL);
Pavel Dovgalyuk4603ea02014-09-01 09:34:49 +0400848}
849
Sebastian Tanase1ad95802014-07-25 11:56:28 +0200850void configure_icount(QemuOpts *opts, Error **errp)
Paolo Bonzini946fb272011-09-12 13:57:37 +0200851{
Sebastian Tanase1ad95802014-07-25 11:56:28 +0200852 const char *option;
Sebastian Tanasea8bfac32014-07-25 11:56:29 +0200853 char *rem_str = NULL;
Sebastian Tanase1ad95802014-07-25 11:56:28 +0200854
Sebastian Tanase1ad95802014-07-25 11:56:28 +0200855 option = qemu_opt_get(opts, "shift");
Paolo Bonzini946fb272011-09-12 13:57:37 +0200856 if (!option) {
Sebastian Tanasea8bfac32014-07-25 11:56:29 +0200857 if (qemu_opt_get(opts, "align") != NULL) {
858 error_setg(errp, "Please specify shift option when using align");
859 }
Paolo Bonzini946fb272011-09-12 13:57:37 +0200860 return;
861 }
Victor CLEMENTf1f4b572015-05-29 17:14:05 +0200862
863 icount_sleep = qemu_opt_get_bool(opts, "sleep", true);
Victor CLEMENT5045e9d92015-05-29 17:14:04 +0200864 if (icount_sleep) {
Pavel Dovgalyukb39e3f32018-01-11 11:26:10 +0300865 timers_state.icount_warp_timer = timer_new_ns(QEMU_CLOCK_VIRTUAL_RT,
Pavel Dovgalyuke76d1792016-03-10 14:56:09 +0300866 icount_timer_cb, NULL);
Victor CLEMENT5045e9d92015-05-29 17:14:04 +0200867 }
Victor CLEMENTf1f4b572015-05-29 17:14:05 +0200868
Sebastian Tanasea8bfac32014-07-25 11:56:29 +0200869 icount_align_option = qemu_opt_get_bool(opts, "align", false);
Victor CLEMENTf1f4b572015-05-29 17:14:05 +0200870
871 if (icount_align_option && !icount_sleep) {
Pranith Kumar778d9f92016-02-26 10:16:51 -0500872 error_setg(errp, "align=on and sleep=off are incompatible");
Victor CLEMENTf1f4b572015-05-29 17:14:05 +0200873 }
Paolo Bonzini946fb272011-09-12 13:57:37 +0200874 if (strcmp(option, "auto") != 0) {
Sebastian Tanasea8bfac32014-07-25 11:56:29 +0200875 errno = 0;
Paolo Bonzinic1ff0732018-08-14 09:31:58 +0200876 timers_state.icount_time_shift = strtol(option, &rem_str, 0);
Sebastian Tanasea8bfac32014-07-25 11:56:29 +0200877 if (errno != 0 || *rem_str != '\0' || !strlen(option)) {
878 error_setg(errp, "icount: Invalid shift value");
879 }
Paolo Bonzini946fb272011-09-12 13:57:37 +0200880 use_icount = 1;
881 return;
Sebastian Tanasea8bfac32014-07-25 11:56:29 +0200882 } else if (icount_align_option) {
883 error_setg(errp, "shift=auto and align=on are incompatible");
Victor CLEMENTf1f4b572015-05-29 17:14:05 +0200884 } else if (!icount_sleep) {
Pranith Kumar778d9f92016-02-26 10:16:51 -0500885 error_setg(errp, "shift=auto and sleep=off are incompatible");
Paolo Bonzini946fb272011-09-12 13:57:37 +0200886 }
887
888 use_icount = 2;
889
890 /* 125MIPS seems a reasonable initial guess at the guest speed.
891 It will be corrected fairly quickly anyway. */
Paolo Bonzinic1ff0732018-08-14 09:31:58 +0200892 timers_state.icount_time_shift = 3;
Paolo Bonzini946fb272011-09-12 13:57:37 +0200893
894 /* Have both realtime and virtual time triggers for speed adjustment.
895 The realtime trigger catches emulated time passing too slowly,
896 the virtual time trigger catches emulated time passing too fast.
897 Realtime triggers occur even when idle, so use them less frequently
898 than VM triggers. */
Pavel Dovgalyukb39e3f32018-01-11 11:26:10 +0300899 timers_state.vm_clock_warp_start = -1;
900 timers_state.icount_rt_timer = timer_new_ms(QEMU_CLOCK_VIRTUAL_RT,
Pavel Dovgalyukbf2a7dd2014-11-26 13:40:55 +0300901 icount_adjust_rt, NULL);
Pavel Dovgalyukb39e3f32018-01-11 11:26:10 +0300902 timer_mod(timers_state.icount_rt_timer,
Pavel Dovgalyukbf2a7dd2014-11-26 13:40:55 +0300903 qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL_RT) + 1000);
Pavel Dovgalyukb39e3f32018-01-11 11:26:10 +0300904 timers_state.icount_vm_timer = timer_new_ns(QEMU_CLOCK_VIRTUAL,
Alex Bligh40daca52013-08-21 16:03:02 +0100905 icount_adjust_vm, NULL);
Pavel Dovgalyukb39e3f32018-01-11 11:26:10 +0300906 timer_mod(timers_state.icount_vm_timer,
Alex Bligh40daca52013-08-21 16:03:02 +0100907 qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) +
Rutuja Shah73bcb242016-03-21 21:32:30 +0530908 NANOSECONDS_PER_SECOND / 10);
Paolo Bonzini946fb272011-09-12 13:57:37 +0200909}
910
911/***********************************************************/
Alex Bennée65467062017-02-23 18:29:09 +0000912/* TCG vCPU kick timer
913 *
914 * The kick timer is responsible for moving single threaded vCPU
915 * emulation on to the next vCPU. If more than one vCPU is running a
916 * timer event with force a cpu->exit so the next vCPU can get
917 * scheduled.
918 *
919 * The timer is removed if all vCPUs are idle and restarted again once
920 * idleness is complete.
921 */
922
923static QEMUTimer *tcg_kick_vcpu_timer;
Alex Bennée791158d2017-02-23 18:29:10 +0000924static CPUState *tcg_current_rr_cpu;
Alex Bennée65467062017-02-23 18:29:09 +0000925
926#define TCG_KICK_PERIOD (NANOSECONDS_PER_SECOND / 10)
927
928static inline int64_t qemu_tcg_next_kick(void)
929{
930 return qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) + TCG_KICK_PERIOD;
931}
932
Alex Bennée791158d2017-02-23 18:29:10 +0000933/* Kick the currently round-robin scheduled vCPU */
934static void qemu_cpu_kick_rr_cpu(void)
935{
936 CPUState *cpu;
Alex Bennée791158d2017-02-23 18:29:10 +0000937 do {
938 cpu = atomic_mb_read(&tcg_current_rr_cpu);
939 if (cpu) {
940 cpu_exit(cpu);
941 }
942 } while (cpu != atomic_mb_read(&tcg_current_rr_cpu));
943}
944
Paolo Bonzini6b8f0182017-03-02 19:56:40 +0100945static void do_nothing(CPUState *cpu, run_on_cpu_data unused)
946{
947}
948
Paolo Bonzini3f53bc62017-03-03 11:50:29 +0100949void qemu_timer_notify_cb(void *opaque, QEMUClockType type)
950{
Paolo Bonzini6b8f0182017-03-02 19:56:40 +0100951 if (!use_icount || type != QEMU_CLOCK_VIRTUAL) {
952 qemu_notify_event();
953 return;
954 }
955
Peter Maydellc52e7132018-04-10 13:02:25 +0100956 if (qemu_in_vcpu_thread()) {
957 /* A CPU is currently running; kick it back out to the
958 * tcg_cpu_exec() loop so it will recalculate its
959 * icount deadline immediately.
960 */
961 qemu_cpu_kick(current_cpu);
962 } else if (first_cpu) {
Paolo Bonzini6b8f0182017-03-02 19:56:40 +0100963 /* qemu_cpu_kick is not enough to kick a halted CPU out of
964 * qemu_tcg_wait_io_event. async_run_on_cpu, instead,
965 * causes cpu_thread_is_idle to return false. This way,
966 * handle_icount_deadline can run.
Peter Maydellc52e7132018-04-10 13:02:25 +0100967 * If we have no CPUs at all for some reason, we don't
968 * need to do anything.
Paolo Bonzini6b8f0182017-03-02 19:56:40 +0100969 */
970 async_run_on_cpu(first_cpu, do_nothing, RUN_ON_CPU_NULL);
971 }
Paolo Bonzini3f53bc62017-03-03 11:50:29 +0100972}
973
Alex Bennée65467062017-02-23 18:29:09 +0000974static void kick_tcg_thread(void *opaque)
975{
976 timer_mod(tcg_kick_vcpu_timer, qemu_tcg_next_kick());
Alex Bennée791158d2017-02-23 18:29:10 +0000977 qemu_cpu_kick_rr_cpu();
Alex Bennée65467062017-02-23 18:29:09 +0000978}
979
980static void start_tcg_kick_timer(void)
981{
Paolo Bonzinidb08b682018-01-11 13:53:12 +0100982 assert(!mttcg_enabled);
983 if (!tcg_kick_vcpu_timer && CPU_NEXT(first_cpu)) {
Alex Bennée65467062017-02-23 18:29:09 +0000984 tcg_kick_vcpu_timer = timer_new_ns(QEMU_CLOCK_VIRTUAL,
985 kick_tcg_thread, NULL);
Alex Bennée1926ab22018-09-27 18:17:24 +0100986 }
987 if (tcg_kick_vcpu_timer && !timer_pending(tcg_kick_vcpu_timer)) {
Alex Bennée65467062017-02-23 18:29:09 +0000988 timer_mod(tcg_kick_vcpu_timer, qemu_tcg_next_kick());
989 }
990}
991
992static void stop_tcg_kick_timer(void)
993{
Paolo Bonzinidb08b682018-01-11 13:53:12 +0100994 assert(!mttcg_enabled);
Alex Bennée1926ab22018-09-27 18:17:24 +0100995 if (tcg_kick_vcpu_timer && timer_pending(tcg_kick_vcpu_timer)) {
Alex Bennée65467062017-02-23 18:29:09 +0000996 timer_del(tcg_kick_vcpu_timer);
Alex Bennée65467062017-02-23 18:29:09 +0000997 }
998}
999
Alex Bennée65467062017-02-23 18:29:09 +00001000/***********************************************************/
Blue Swirl296af7c2010-03-29 19:23:50 +00001001void hw_error(const char *fmt, ...)
1002{
1003 va_list ap;
Andreas Färber55e5c282012-12-17 06:18:02 +01001004 CPUState *cpu;
Blue Swirl296af7c2010-03-29 19:23:50 +00001005
1006 va_start(ap, fmt);
1007 fprintf(stderr, "qemu: hardware error: ");
1008 vfprintf(stderr, fmt, ap);
1009 fprintf(stderr, "\n");
Andreas Färberbdc44642013-06-24 23:50:24 +02001010 CPU_FOREACH(cpu) {
Andreas Färber55e5c282012-12-17 06:18:02 +01001011 fprintf(stderr, "CPU #%d:\n", cpu->cpu_index);
Andreas Färber878096e2013-05-27 01:33:50 +02001012 cpu_dump_state(cpu, stderr, fprintf, CPU_DUMP_FPU);
Blue Swirl296af7c2010-03-29 19:23:50 +00001013 }
1014 va_end(ap);
1015 abort();
1016}
1017
1018void cpu_synchronize_all_states(void)
1019{
Andreas Färber182735e2013-05-29 22:29:20 +02001020 CPUState *cpu;
Blue Swirl296af7c2010-03-29 19:23:50 +00001021
Andreas Färberbdc44642013-06-24 23:50:24 +02001022 CPU_FOREACH(cpu) {
Andreas Färber182735e2013-05-29 22:29:20 +02001023 cpu_synchronize_state(cpu);
Sergio Andres Gomez Del Realc97d6d22017-09-13 04:05:09 -05001024 /* TODO: move to cpu_synchronize_state() */
1025 if (hvf_enabled()) {
1026 hvf_cpu_synchronize_state(cpu);
1027 }
Blue Swirl296af7c2010-03-29 19:23:50 +00001028 }
1029}
1030
1031void cpu_synchronize_all_post_reset(void)
1032{
Andreas Färber182735e2013-05-29 22:29:20 +02001033 CPUState *cpu;
Blue Swirl296af7c2010-03-29 19:23:50 +00001034
Andreas Färberbdc44642013-06-24 23:50:24 +02001035 CPU_FOREACH(cpu) {
Andreas Färber182735e2013-05-29 22:29:20 +02001036 cpu_synchronize_post_reset(cpu);
Sergio Andres Gomez Del Realc97d6d22017-09-13 04:05:09 -05001037 /* TODO: move to cpu_synchronize_post_reset() */
1038 if (hvf_enabled()) {
1039 hvf_cpu_synchronize_post_reset(cpu);
1040 }
Blue Swirl296af7c2010-03-29 19:23:50 +00001041 }
1042}
1043
1044void cpu_synchronize_all_post_init(void)
1045{
Andreas Färber182735e2013-05-29 22:29:20 +02001046 CPUState *cpu;
Blue Swirl296af7c2010-03-29 19:23:50 +00001047
Andreas Färberbdc44642013-06-24 23:50:24 +02001048 CPU_FOREACH(cpu) {
Andreas Färber182735e2013-05-29 22:29:20 +02001049 cpu_synchronize_post_init(cpu);
Sergio Andres Gomez Del Realc97d6d22017-09-13 04:05:09 -05001050 /* TODO: move to cpu_synchronize_post_init() */
1051 if (hvf_enabled()) {
1052 hvf_cpu_synchronize_post_init(cpu);
1053 }
Blue Swirl296af7c2010-03-29 19:23:50 +00001054 }
1055}
1056
David Gibson75e972d2017-05-26 14:46:28 +10001057void cpu_synchronize_all_pre_loadvm(void)
1058{
1059 CPUState *cpu;
1060
1061 CPU_FOREACH(cpu) {
1062 cpu_synchronize_pre_loadvm(cpu);
1063 }
1064}
1065
Stefan Hajnoczi4486e892018-03-07 14:42:05 +00001066static int do_vm_stop(RunState state, bool send_stop)
Blue Swirl296af7c2010-03-29 19:23:50 +00001067{
Kevin Wolf56983462013-07-05 13:49:54 +02001068 int ret = 0;
1069
Luiz Capitulino13548692011-07-29 15:36:43 -03001070 if (runstate_is_running()) {
Blue Swirl296af7c2010-03-29 19:23:50 +00001071 cpu_disable_ticks();
Blue Swirl296af7c2010-03-29 19:23:50 +00001072 pause_all_vcpus();
Luiz Capitulinof5bbfba2011-07-29 15:04:45 -03001073 runstate_set(state);
Luiz Capitulino1dfb4dd2011-07-29 14:26:33 -03001074 vm_state_notify(0, state);
Stefan Hajnoczi4486e892018-03-07 14:42:05 +00001075 if (send_stop) {
Peter Xu3ab72382018-08-15 21:37:37 +08001076 qapi_event_send_stop();
Stefan Hajnoczi4486e892018-03-07 14:42:05 +00001077 }
Blue Swirl296af7c2010-03-29 19:23:50 +00001078 }
Kevin Wolf56983462013-07-05 13:49:54 +02001079
Kevin Wolf594a45c2013-07-18 14:52:19 +02001080 bdrv_drain_all();
Pavel Dovgalyuk6d0ceb82016-09-26 11:08:16 +03001081 replay_disable_events();
John Snow22af08e2016-09-22 21:45:51 -04001082 ret = bdrv_flush_all();
Kevin Wolf594a45c2013-07-18 14:52:19 +02001083
Kevin Wolf56983462013-07-05 13:49:54 +02001084 return ret;
Blue Swirl296af7c2010-03-29 19:23:50 +00001085}
1086
Stefan Hajnoczi4486e892018-03-07 14:42:05 +00001087/* Special vm_stop() variant for terminating the process. Historically clients
1088 * did not expect a QMP STOP event and so we need to retain compatibility.
1089 */
1090int vm_shutdown(void)
1091{
1092 return do_vm_stop(RUN_STATE_SHUTDOWN, false);
1093}
1094
Andreas Färbera1fcaa72012-05-02 23:42:26 +02001095static bool cpu_can_run(CPUState *cpu)
Blue Swirl296af7c2010-03-29 19:23:50 +00001096{
Andreas Färber4fdeee72012-05-02 23:10:09 +02001097 if (cpu->stop) {
Andreas Färbera1fcaa72012-05-02 23:42:26 +02001098 return false;
Jan Kiszka0ab07c62011-02-07 12:19:14 +01001099 }
Tiejun Chen321bc0b2013-08-02 09:43:09 +08001100 if (cpu_is_stopped(cpu)) {
Andreas Färbera1fcaa72012-05-02 23:42:26 +02001101 return false;
Jan Kiszka0ab07c62011-02-07 12:19:14 +01001102 }
Andreas Färbera1fcaa72012-05-02 23:42:26 +02001103 return true;
Blue Swirl296af7c2010-03-29 19:23:50 +00001104}
1105
Andreas Färber91325042013-05-27 02:07:49 +02001106static void cpu_handle_guest_debug(CPUState *cpu)
Jan Kiszka3c638d02010-06-25 16:56:56 +02001107{
Andreas Färber64f6b342013-05-27 02:06:09 +02001108 gdb_set_stop_cpu(cpu);
Jan Kiszka8cf71712011-02-07 12:19:16 +01001109 qemu_system_debug_request();
Andreas Färberf324e762012-05-02 23:26:21 +02001110 cpu->stopped = true;
Jan Kiszka3c638d02010-06-25 16:56:56 +02001111}
1112
Jan Kiszka6d9cb732011-02-01 22:15:58 +01001113#ifdef CONFIG_LINUX
1114static void sigbus_reraise(void)
1115{
1116 sigset_t set;
1117 struct sigaction action;
1118
1119 memset(&action, 0, sizeof(action));
1120 action.sa_handler = SIG_DFL;
1121 if (!sigaction(SIGBUS, &action, NULL)) {
1122 raise(SIGBUS);
1123 sigemptyset(&set);
1124 sigaddset(&set, SIGBUS);
Peter Maydella2d17612016-05-16 18:33:59 +01001125 pthread_sigmask(SIG_UNBLOCK, &set, NULL);
Jan Kiszka6d9cb732011-02-01 22:15:58 +01001126 }
1127 perror("Failed to re-raise SIGBUS!\n");
1128 abort();
1129}
1130
Paolo Bonzinid98d4072017-02-08 13:22:12 +01001131static void sigbus_handler(int n, siginfo_t *siginfo, void *ctx)
Jan Kiszka6d9cb732011-02-01 22:15:58 +01001132{
Paolo Bonzinia16fc072017-02-09 09:50:02 +01001133 if (siginfo->si_code != BUS_MCEERR_AO && siginfo->si_code != BUS_MCEERR_AR) {
1134 sigbus_reraise();
1135 }
1136
Paolo Bonzini2ae41db2017-02-08 12:48:54 +01001137 if (current_cpu) {
1138 /* Called asynchronously in VCPU thread. */
1139 if (kvm_on_sigbus_vcpu(current_cpu, siginfo->si_code, siginfo->si_addr)) {
1140 sigbus_reraise();
1141 }
1142 } else {
1143 /* Called synchronously (via signalfd) in main thread. */
1144 if (kvm_on_sigbus(siginfo->si_code, siginfo->si_addr)) {
1145 sigbus_reraise();
1146 }
Jan Kiszka6d9cb732011-02-01 22:15:58 +01001147 }
1148}
1149
1150static void qemu_init_sigbus(void)
1151{
1152 struct sigaction action;
1153
1154 memset(&action, 0, sizeof(action));
1155 action.sa_flags = SA_SIGINFO;
Paolo Bonzinid98d4072017-02-08 13:22:12 +01001156 action.sa_sigaction = sigbus_handler;
Jan Kiszka6d9cb732011-02-01 22:15:58 +01001157 sigaction(SIGBUS, &action, NULL);
1158
1159 prctl(PR_MCE_KILL, PR_MCE_KILL_SET, PR_MCE_KILL_EARLY, 0, 0);
1160}
Paolo Bonzinia16fc072017-02-09 09:50:02 +01001161#else /* !CONFIG_LINUX */
1162static void qemu_init_sigbus(void)
1163{
1164}
Paolo Bonzinia16fc072017-02-09 09:50:02 +01001165#endif /* !CONFIG_LINUX */
Blue Swirl296af7c2010-03-29 19:23:50 +00001166
Stefan Weilb2532d82012-09-27 07:41:42 +02001167static QemuMutex qemu_global_mutex;
Blue Swirl296af7c2010-03-29 19:23:50 +00001168
1169static QemuThread io_thread;
1170
Blue Swirl296af7c2010-03-29 19:23:50 +00001171/* cpu creation */
1172static QemuCond qemu_cpu_cond;
1173/* system init */
Blue Swirl296af7c2010-03-29 19:23:50 +00001174static QemuCond qemu_pause_cond;
1175
Paolo Bonzinid3b12f52011-09-13 10:30:52 +02001176void qemu_init_cpu_loop(void)
Blue Swirl296af7c2010-03-29 19:23:50 +00001177{
Jan Kiszka6d9cb732011-02-01 22:15:58 +01001178 qemu_init_sigbus();
Anthony Liguoried945922011-02-08 18:18:18 +01001179 qemu_cond_init(&qemu_cpu_cond);
Anthony Liguoried945922011-02-08 18:18:18 +01001180 qemu_cond_init(&qemu_pause_cond);
Blue Swirl296af7c2010-03-29 19:23:50 +00001181 qemu_mutex_init(&qemu_global_mutex);
Blue Swirl296af7c2010-03-29 19:23:50 +00001182
Jan Kiszkab7680cb2011-03-12 17:43:51 +01001183 qemu_thread_get_self(&io_thread);
Blue Swirl296af7c2010-03-29 19:23:50 +00001184}
1185
Paolo Bonzini14e6fe12016-10-31 10:36:08 +01001186void run_on_cpu(CPUState *cpu, run_on_cpu_func func, run_on_cpu_data data)
Marcelo Tosattie82bcec2010-05-04 09:45:22 -03001187{
Sergey Fedorovd148d902016-08-29 09:51:00 +02001188 do_run_on_cpu(cpu, func, data, &qemu_global_mutex);
Chegu Vinod3c022702013-06-24 03:49:41 -06001189}
1190
Gu Zheng4c055ab2016-05-12 09:18:13 +05301191static void qemu_kvm_destroy_vcpu(CPUState *cpu)
1192{
1193 if (kvm_destroy_vcpu(cpu) < 0) {
1194 error_report("kvm_destroy_vcpu failed");
1195 exit(EXIT_FAILURE);
1196 }
1197}
1198
1199static void qemu_tcg_destroy_vcpu(CPUState *cpu)
1200{
1201}
1202
David Hildenbrandebd05fe2017-11-29 20:12:15 +01001203static void qemu_cpu_stop(CPUState *cpu, bool exit)
1204{
1205 g_assert(qemu_cpu_is_self(cpu));
1206 cpu->stop = false;
1207 cpu->stopped = true;
1208 if (exit) {
1209 cpu_exit(cpu);
1210 }
1211 qemu_cond_broadcast(&qemu_pause_cond);
1212}
1213
Andreas Färber509a0d72012-05-03 02:18:09 +02001214static void qemu_wait_io_event_common(CPUState *cpu)
Blue Swirl296af7c2010-03-29 19:23:50 +00001215{
Alex Bennée37257942017-02-23 18:29:14 +00001216 atomic_mb_set(&cpu->thread_kicked, false);
Andreas Färber4fdeee72012-05-02 23:10:09 +02001217 if (cpu->stop) {
David Hildenbrandebd05fe2017-11-29 20:12:15 +01001218 qemu_cpu_stop(cpu, false);
Blue Swirl296af7c2010-03-29 19:23:50 +00001219 }
Sergey Fedorova5403c62016-08-02 18:27:36 +01001220 process_queued_cpu_work(cpu);
Alex Bennée37257942017-02-23 18:29:14 +00001221}
1222
Paolo Bonzinidb08b682018-01-11 13:53:12 +01001223static void qemu_tcg_rr_wait_io_event(CPUState *cpu)
Alex Bennée37257942017-02-23 18:29:14 +00001224{
Paolo Bonzinidb08b682018-01-11 13:53:12 +01001225 while (all_cpu_threads_idle()) {
Alex Bennée65467062017-02-23 18:29:09 +00001226 stop_tcg_kick_timer();
KONRAD Fredericd5f8d612015-08-10 17:27:06 +02001227 qemu_cond_wait(cpu->halt_cond, &qemu_global_mutex);
Jan Kiszka16400322011-02-09 16:29:37 +01001228 }
Blue Swirl296af7c2010-03-29 19:23:50 +00001229
Alex Bennée65467062017-02-23 18:29:09 +00001230 start_tcg_kick_timer();
1231
Alex Bennée37257942017-02-23 18:29:14 +00001232 qemu_wait_io_event_common(cpu);
Blue Swirl296af7c2010-03-29 19:23:50 +00001233}
1234
Paolo Bonzinidb08b682018-01-11 13:53:12 +01001235static void qemu_wait_io_event(CPUState *cpu)
Blue Swirl296af7c2010-03-29 19:23:50 +00001236{
Andreas Färbera98ae1d2013-05-26 23:21:08 +02001237 while (cpu_thread_is_idle(cpu)) {
Andreas Färberf5c121b2012-05-03 01:22:49 +02001238 qemu_cond_wait(cpu->halt_cond, &qemu_global_mutex);
Jan Kiszka16400322011-02-09 16:29:37 +01001239 }
Blue Swirl296af7c2010-03-29 19:23:50 +00001240
Paolo Bonzinidb08b682018-01-11 13:53:12 +01001241#ifdef _WIN32
1242 /* Eat dummy APC queued by qemu_cpu_kick_thread. */
1243 if (!tcg_enabled()) {
1244 SleepEx(0, TRUE);
Sergio Andres Gomez Del Realc97d6d22017-09-13 04:05:09 -05001245 }
Paolo Bonzinidb08b682018-01-11 13:53:12 +01001246#endif
Sergio Andres Gomez Del Realc97d6d22017-09-13 04:05:09 -05001247 qemu_wait_io_event_common(cpu);
1248}
1249
Jan Kiszka7e97cd82011-02-07 12:19:12 +01001250static void *qemu_kvm_cpu_thread_fn(void *arg)
Blue Swirl296af7c2010-03-29 19:23:50 +00001251{
Andreas Färber48a106b2013-05-27 02:20:39 +02001252 CPUState *cpu = arg;
Jan Kiszka84b49152011-02-01 22:15:50 +01001253 int r;
Blue Swirl296af7c2010-03-29 19:23:50 +00001254
Paolo Bonziniab28bd22015-07-09 08:55:38 +02001255 rcu_register_thread();
1256
Paolo Bonzini2e7f7a32015-06-18 18:47:18 +02001257 qemu_mutex_lock_iothread();
Andreas Färber814e6122012-05-02 17:00:37 +02001258 qemu_thread_get_self(cpu->thread);
Andreas Färber9f09e182012-05-03 06:59:07 +02001259 cpu->thread_id = qemu_get_thread_id();
Pavel Dovgalyuk626cf8f2014-12-08 10:53:17 +03001260 cpu->can_do_io = 1;
Andreas Färber4917cf42013-05-27 05:17:50 +02001261 current_cpu = cpu;
Blue Swirl296af7c2010-03-29 19:23:50 +00001262
Andreas Färber504134d2012-12-17 06:38:45 +01001263 r = kvm_init_vcpu(cpu);
Jan Kiszka84b49152011-02-01 22:15:50 +01001264 if (r < 0) {
Alistair Francis493d89b2018-02-03 09:43:14 +01001265 error_report("kvm_init_vcpu failed: %s", strerror(-r));
Jan Kiszka84b49152011-02-01 22:15:50 +01001266 exit(1);
1267 }
Blue Swirl296af7c2010-03-29 19:23:50 +00001268
Paolo Bonzini18268b62017-02-09 09:41:14 +01001269 kvm_init_cpu_signals(cpu);
Blue Swirl296af7c2010-03-29 19:23:50 +00001270
1271 /* signal CPU creation */
Andreas Färber61a46212012-05-02 22:49:36 +02001272 cpu->created = true;
Blue Swirl296af7c2010-03-29 19:23:50 +00001273 qemu_cond_signal(&qemu_cpu_cond);
1274
Gu Zheng4c055ab2016-05-12 09:18:13 +05301275 do {
Andreas Färbera1fcaa72012-05-02 23:42:26 +02001276 if (cpu_can_run(cpu)) {
Andreas Färber1458c362013-05-26 23:46:55 +02001277 r = kvm_cpu_exec(cpu);
Jan Kiszka83f338f2011-02-07 12:19:17 +01001278 if (r == EXCP_DEBUG) {
Andreas Färber91325042013-05-27 02:07:49 +02001279 cpu_handle_guest_debug(cpu);
Jan Kiszka83f338f2011-02-07 12:19:17 +01001280 }
Jan Kiszka0ab07c62011-02-07 12:19:14 +01001281 }
Paolo Bonzinidb08b682018-01-11 13:53:12 +01001282 qemu_wait_io_event(cpu);
Gu Zheng4c055ab2016-05-12 09:18:13 +05301283 } while (!cpu->unplug || cpu_can_run(cpu));
Blue Swirl296af7c2010-03-29 19:23:50 +00001284
Gu Zheng4c055ab2016-05-12 09:18:13 +05301285 qemu_kvm_destroy_vcpu(cpu);
Bharata B Rao2c579042016-05-12 09:18:14 +05301286 cpu->created = false;
1287 qemu_cond_signal(&qemu_cpu_cond);
Gu Zheng4c055ab2016-05-12 09:18:13 +05301288 qemu_mutex_unlock_iothread();
Paolo Bonzini57615ed2018-01-30 11:04:36 -05001289 rcu_unregister_thread();
Blue Swirl296af7c2010-03-29 19:23:50 +00001290 return NULL;
1291}
1292
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +02001293static void *qemu_dummy_cpu_thread_fn(void *arg)
1294{
1295#ifdef _WIN32
Alistair Francis493d89b2018-02-03 09:43:14 +01001296 error_report("qtest is not supported under Windows");
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +02001297 exit(1);
1298#else
Andreas Färber10a90212013-05-27 02:24:35 +02001299 CPUState *cpu = arg;
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +02001300 sigset_t waitset;
1301 int r;
1302
Paolo Bonziniab28bd22015-07-09 08:55:38 +02001303 rcu_register_thread();
1304
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +02001305 qemu_mutex_lock_iothread();
Andreas Färber814e6122012-05-02 17:00:37 +02001306 qemu_thread_get_self(cpu->thread);
Andreas Färber9f09e182012-05-03 06:59:07 +02001307 cpu->thread_id = qemu_get_thread_id();
Pavel Dovgalyuk626cf8f2014-12-08 10:53:17 +03001308 cpu->can_do_io = 1;
Alex Bennée37257942017-02-23 18:29:14 +00001309 current_cpu = cpu;
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +02001310
1311 sigemptyset(&waitset);
1312 sigaddset(&waitset, SIG_IPI);
1313
1314 /* signal CPU creation */
Andreas Färber61a46212012-05-02 22:49:36 +02001315 cpu->created = true;
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +02001316 qemu_cond_signal(&qemu_cpu_cond);
1317
Paolo Bonzinid2831ab2018-01-30 11:04:53 -05001318 do {
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +02001319 qemu_mutex_unlock_iothread();
1320 do {
1321 int sig;
1322 r = sigwait(&waitset, &sig);
1323 } while (r == -1 && (errno == EAGAIN || errno == EINTR));
1324 if (r == -1) {
1325 perror("sigwait");
1326 exit(1);
1327 }
1328 qemu_mutex_lock_iothread();
Paolo Bonzinidb08b682018-01-11 13:53:12 +01001329 qemu_wait_io_event(cpu);
Paolo Bonzinid2831ab2018-01-30 11:04:53 -05001330 } while (!cpu->unplug);
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +02001331
Paolo Bonzinid2831ab2018-01-30 11:04:53 -05001332 rcu_unregister_thread();
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +02001333 return NULL;
1334#endif
1335}
1336
Alex Bennée1be7fcb2016-10-27 16:10:08 +01001337static int64_t tcg_get_icount_limit(void)
1338{
1339 int64_t deadline;
1340
1341 if (replay_mode != REPLAY_MODE_PLAY) {
1342 deadline = qemu_clock_deadline_ns_all(QEMU_CLOCK_VIRTUAL);
1343
1344 /* Maintain prior (possibly buggy) behaviour where if no deadline
1345 * was set (as there is no QEMU_CLOCK_VIRTUAL timer) or it is more than
1346 * INT32_MAX nanoseconds ahead, we still use INT32_MAX
1347 * nanoseconds.
1348 */
1349 if ((deadline < 0) || (deadline > INT32_MAX)) {
1350 deadline = INT32_MAX;
1351 }
1352
1353 return qemu_icount_round(deadline);
1354 } else {
1355 return replay_get_instructions();
1356 }
1357}
1358
Alex Bennée12e97002016-10-27 16:10:14 +01001359static void handle_icount_deadline(void)
1360{
Paolo Bonzini6b8f0182017-03-02 19:56:40 +01001361 assert(qemu_in_vcpu_thread());
Alex Bennée12e97002016-10-27 16:10:14 +01001362 if (use_icount) {
1363 int64_t deadline =
1364 qemu_clock_deadline_ns_all(QEMU_CLOCK_VIRTUAL);
1365
1366 if (deadline == 0) {
Paolo Bonzini6b8f0182017-03-02 19:56:40 +01001367 /* Wake up other AioContexts. */
Alex Bennée12e97002016-10-27 16:10:14 +01001368 qemu_clock_notify(QEMU_CLOCK_VIRTUAL);
Paolo Bonzini6b8f0182017-03-02 19:56:40 +01001369 qemu_clock_run_timers(QEMU_CLOCK_VIRTUAL);
Alex Bennée12e97002016-10-27 16:10:14 +01001370 }
1371 }
1372}
1373
Alex Bennée05248382017-03-29 16:46:59 +01001374static void prepare_icount_for_run(CPUState *cpu)
1375{
1376 if (use_icount) {
Alex Bennéeeda5f7c2017-04-05 12:35:48 +01001377 int insns_left;
Alex Bennée05248382017-03-29 16:46:59 +01001378
1379 /* These should always be cleared by process_icount_data after
1380 * each vCPU execution. However u16.high can be raised
1381 * asynchronously by cpu_exit/cpu_interrupt/tcg_handle_interrupt
1382 */
1383 g_assert(cpu->icount_decr.u16.low == 0);
1384 g_assert(cpu->icount_extra == 0);
1385
Alex Bennéeeda5f7c2017-04-05 12:35:48 +01001386 cpu->icount_budget = tcg_get_icount_limit();
1387 insns_left = MIN(0xffff, cpu->icount_budget);
1388 cpu->icount_decr.u16.low = insns_left;
1389 cpu->icount_extra = cpu->icount_budget - insns_left;
Alex Bennéed759c952018-02-27 12:52:48 +03001390
1391 replay_mutex_lock();
Alex Bennée05248382017-03-29 16:46:59 +01001392 }
1393}
1394
1395static void process_icount_data(CPUState *cpu)
1396{
1397 if (use_icount) {
Alex Bennéee4cd9652017-03-31 16:09:42 +01001398 /* Account for executed instructions */
Alex Bennée512d3c82017-04-05 12:32:37 +01001399 cpu_update_icount(cpu);
Alex Bennée05248382017-03-29 16:46:59 +01001400
1401 /* Reset the counters */
1402 cpu->icount_decr.u16.low = 0;
1403 cpu->icount_extra = 0;
Alex Bennéee4cd9652017-03-31 16:09:42 +01001404 cpu->icount_budget = 0;
1405
Alex Bennée05248382017-03-29 16:46:59 +01001406 replay_account_executed_instructions();
Alex Bennéed759c952018-02-27 12:52:48 +03001407
1408 replay_mutex_unlock();
Alex Bennée05248382017-03-29 16:46:59 +01001409 }
1410}
1411
1412
Alex Bennée1be7fcb2016-10-27 16:10:08 +01001413static int tcg_cpu_exec(CPUState *cpu)
1414{
1415 int ret;
1416#ifdef CONFIG_PROFILER
1417 int64_t ti;
1418#endif
1419
Emilio G. Cotaf28d0df2018-06-22 13:45:31 -04001420 assert(tcg_enabled());
Alex Bennée1be7fcb2016-10-27 16:10:08 +01001421#ifdef CONFIG_PROFILER
1422 ti = profile_getclock();
1423#endif
Alex Bennée1be7fcb2016-10-27 16:10:08 +01001424 cpu_exec_start(cpu);
1425 ret = cpu_exec(cpu);
1426 cpu_exec_end(cpu);
1427#ifdef CONFIG_PROFILER
Emilio G. Cota72fd2ef2018-10-10 10:48:53 -04001428 atomic_set(&tcg_ctx->prof.cpu_exec_time,
1429 tcg_ctx->prof.cpu_exec_time + profile_getclock() - ti);
Alex Bennée1be7fcb2016-10-27 16:10:08 +01001430#endif
Alex Bennée1be7fcb2016-10-27 16:10:08 +01001431 return ret;
1432}
1433
Alex Bennéec93bbbe2016-10-27 16:10:09 +01001434/* Destroy any remaining vCPUs which have been unplugged and have
1435 * finished running
1436 */
1437static void deal_with_unplugged_cpus(void)
Alex Bennée1be7fcb2016-10-27 16:10:08 +01001438{
Alex Bennéec93bbbe2016-10-27 16:10:09 +01001439 CPUState *cpu;
Alex Bennée1be7fcb2016-10-27 16:10:08 +01001440
Alex Bennéec93bbbe2016-10-27 16:10:09 +01001441 CPU_FOREACH(cpu) {
1442 if (cpu->unplug && !cpu_can_run(cpu)) {
1443 qemu_tcg_destroy_vcpu(cpu);
1444 cpu->created = false;
1445 qemu_cond_signal(&qemu_cpu_cond);
Alex Bennée1be7fcb2016-10-27 16:10:08 +01001446 break;
1447 }
1448 }
Alex Bennée1be7fcb2016-10-27 16:10:08 +01001449}
Jan Kiszkabdb7ca62011-09-26 09:40:39 +02001450
Alex Bennée65467062017-02-23 18:29:09 +00001451/* Single-threaded TCG
1452 *
1453 * In the single-threaded case each vCPU is simulated in turn. If
1454 * there is more than a single vCPU we create a simple timer to kick
1455 * the vCPU and ensure we don't get stuck in a tight loop in one vCPU.
1456 * This is done explicitly rather than relying on side-effects
1457 * elsewhere.
1458 */
1459
Alex Bennée37257942017-02-23 18:29:14 +00001460static void *qemu_tcg_rr_cpu_thread_fn(void *arg)
Blue Swirl296af7c2010-03-29 19:23:50 +00001461{
Andreas Färberc3586ba2012-05-03 01:41:24 +02001462 CPUState *cpu = arg;
Blue Swirl296af7c2010-03-29 19:23:50 +00001463
Emilio G. Cotaf28d0df2018-06-22 13:45:31 -04001464 assert(tcg_enabled());
Paolo Bonziniab28bd22015-07-09 08:55:38 +02001465 rcu_register_thread();
Emilio G. Cota3468b592017-07-19 18:57:58 -04001466 tcg_register_thread();
Paolo Bonziniab28bd22015-07-09 08:55:38 +02001467
Paolo Bonzini2e7f7a32015-06-18 18:47:18 +02001468 qemu_mutex_lock_iothread();
Andreas Färber814e6122012-05-02 17:00:37 +02001469 qemu_thread_get_self(cpu->thread);
Blue Swirl296af7c2010-03-29 19:23:50 +00001470
David Hildenbrand5a9c9732018-02-09 20:52:39 +01001471 cpu->thread_id = qemu_get_thread_id();
1472 cpu->created = true;
1473 cpu->can_do_io = 1;
Blue Swirl296af7c2010-03-29 19:23:50 +00001474 qemu_cond_signal(&qemu_cpu_cond);
1475
Jan Kiszkafa7d1862011-08-22 18:35:25 +02001476 /* wait for initial kick-off after machine start */
Emilio G. Cotac28e3992015-04-27 12:45:28 -04001477 while (first_cpu->stopped) {
KONRAD Fredericd5f8d612015-08-10 17:27:06 +02001478 qemu_cond_wait(first_cpu->halt_cond, &qemu_global_mutex);
Jan Kiszka8e564b42012-02-17 18:31:15 +01001479
1480 /* process any pending work */
Andreas Färberbdc44642013-06-24 23:50:24 +02001481 CPU_FOREACH(cpu) {
Alex Bennée37257942017-02-23 18:29:14 +00001482 current_cpu = cpu;
Andreas Färber182735e2013-05-29 22:29:20 +02001483 qemu_wait_io_event_common(cpu);
Jan Kiszka8e564b42012-02-17 18:31:15 +01001484 }
Jan Kiszka0ab07c62011-02-07 12:19:14 +01001485 }
Blue Swirl296af7c2010-03-29 19:23:50 +00001486
Alex Bennée65467062017-02-23 18:29:09 +00001487 start_tcg_kick_timer();
1488
Alex Bennéec93bbbe2016-10-27 16:10:09 +01001489 cpu = first_cpu;
1490
Alex Bennéee5143e32017-02-23 18:29:12 +00001491 /* process any pending work */
1492 cpu->exit_request = 1;
1493
Blue Swirl296af7c2010-03-29 19:23:50 +00001494 while (1) {
Alex Bennéed759c952018-02-27 12:52:48 +03001495 qemu_mutex_unlock_iothread();
1496 replay_mutex_lock();
1497 qemu_mutex_lock_iothread();
Alex Bennéec93bbbe2016-10-27 16:10:09 +01001498 /* Account partial waits to QEMU_CLOCK_VIRTUAL. */
1499 qemu_account_warp_timer();
1500
Paolo Bonzini6b8f0182017-03-02 19:56:40 +01001501 /* Run the timers here. This is much more efficient than
1502 * waking up the I/O thread and waiting for completion.
1503 */
1504 handle_icount_deadline();
1505
Alex Bennéed759c952018-02-27 12:52:48 +03001506 replay_mutex_unlock();
1507
Alex Bennéec93bbbe2016-10-27 16:10:09 +01001508 if (!cpu) {
1509 cpu = first_cpu;
1510 }
1511
Alex Bennéee5143e32017-02-23 18:29:12 +00001512 while (cpu && !cpu->queued_work_first && !cpu->exit_request) {
1513
Alex Bennée791158d2017-02-23 18:29:10 +00001514 atomic_mb_set(&tcg_current_rr_cpu, cpu);
Alex Bennée37257942017-02-23 18:29:14 +00001515 current_cpu = cpu;
Alex Bennéec93bbbe2016-10-27 16:10:09 +01001516
1517 qemu_clock_enable(QEMU_CLOCK_VIRTUAL,
1518 (cpu->singlestep_enabled & SSTEP_NOTIMER) == 0);
1519
1520 if (cpu_can_run(cpu)) {
1521 int r;
Alex Bennée05248382017-03-29 16:46:59 +01001522
Alex Bennéed759c952018-02-27 12:52:48 +03001523 qemu_mutex_unlock_iothread();
Alex Bennée05248382017-03-29 16:46:59 +01001524 prepare_icount_for_run(cpu);
1525
Alex Bennéec93bbbe2016-10-27 16:10:09 +01001526 r = tcg_cpu_exec(cpu);
Alex Bennée05248382017-03-29 16:46:59 +01001527
1528 process_icount_data(cpu);
Alex Bennéed759c952018-02-27 12:52:48 +03001529 qemu_mutex_lock_iothread();
Alex Bennée05248382017-03-29 16:46:59 +01001530
Alex Bennéec93bbbe2016-10-27 16:10:09 +01001531 if (r == EXCP_DEBUG) {
1532 cpu_handle_guest_debug(cpu);
1533 break;
Pranith Kumar08e73c42017-02-23 18:29:15 +00001534 } else if (r == EXCP_ATOMIC) {
1535 qemu_mutex_unlock_iothread();
1536 cpu_exec_step_atomic(cpu);
1537 qemu_mutex_lock_iothread();
1538 break;
Alex Bennéec93bbbe2016-10-27 16:10:09 +01001539 }
Alex Bennée37257942017-02-23 18:29:14 +00001540 } else if (cpu->stop) {
Alex Bennéec93bbbe2016-10-27 16:10:09 +01001541 if (cpu->unplug) {
1542 cpu = CPU_NEXT(cpu);
1543 }
1544 break;
1545 }
1546
Alex Bennéee5143e32017-02-23 18:29:12 +00001547 cpu = CPU_NEXT(cpu);
1548 } /* while (cpu && !cpu->exit_request).. */
1549
Alex Bennée791158d2017-02-23 18:29:10 +00001550 /* Does not need atomic_mb_set because a spurious wakeup is okay. */
1551 atomic_set(&tcg_current_rr_cpu, NULL);
Alex Bennéec93bbbe2016-10-27 16:10:09 +01001552
Alex Bennéee5143e32017-02-23 18:29:12 +00001553 if (cpu && cpu->exit_request) {
1554 atomic_mb_set(&cpu->exit_request, 0);
1555 }
Alex Blighac70aaf2013-08-21 16:02:57 +01001556
Clement Deschamps013aabd2018-10-21 16:21:03 +02001557 if (use_icount && all_cpu_threads_idle()) {
1558 /*
1559 * When all cpus are sleeping (e.g in WFI), to avoid a deadlock
1560 * in the main_loop, wake it up in order to start the warp timer.
1561 */
1562 qemu_notify_event();
1563 }
1564
Emilio G. Cota068a5ea2018-08-19 05:13:35 -04001565 qemu_tcg_rr_wait_io_event(cpu ? cpu : first_cpu);
Alex Bennéec93bbbe2016-10-27 16:10:09 +01001566 deal_with_unplugged_cpus();
Blue Swirl296af7c2010-03-29 19:23:50 +00001567 }
1568
Paolo Bonzini9b0605f2018-01-30 11:05:06 -05001569 rcu_unregister_thread();
Blue Swirl296af7c2010-03-29 19:23:50 +00001570 return NULL;
1571}
1572
Vincent Palatinb0cb0a62017-01-10 11:59:57 +01001573static void *qemu_hax_cpu_thread_fn(void *arg)
1574{
1575 CPUState *cpu = arg;
1576 int r;
Vincent Palatinb3d3a422017-03-20 11:15:49 +01001577
Paolo Bonzini9857c2d2018-01-30 16:28:49 +01001578 rcu_register_thread();
Vincent Palatinb3d3a422017-03-20 11:15:49 +01001579 qemu_mutex_lock_iothread();
Vincent Palatinb0cb0a62017-01-10 11:59:57 +01001580 qemu_thread_get_self(cpu->thread);
Vincent Palatinb0cb0a62017-01-10 11:59:57 +01001581
1582 cpu->thread_id = qemu_get_thread_id();
1583 cpu->created = true;
1584 cpu->halted = 0;
1585 current_cpu = cpu;
1586
1587 hax_init_vcpu(cpu);
1588 qemu_cond_signal(&qemu_cpu_cond);
1589
Paolo Bonzini9857c2d2018-01-30 16:28:49 +01001590 do {
Vincent Palatinb0cb0a62017-01-10 11:59:57 +01001591 if (cpu_can_run(cpu)) {
1592 r = hax_smp_cpu_exec(cpu);
1593 if (r == EXCP_DEBUG) {
1594 cpu_handle_guest_debug(cpu);
1595 }
1596 }
1597
Paolo Bonzinidb08b682018-01-11 13:53:12 +01001598 qemu_wait_io_event(cpu);
Paolo Bonzini9857c2d2018-01-30 16:28:49 +01001599 } while (!cpu->unplug || cpu_can_run(cpu));
1600 rcu_unregister_thread();
Vincent Palatinb0cb0a62017-01-10 11:59:57 +01001601 return NULL;
1602}
1603
Sergio Andres Gomez Del Realc97d6d22017-09-13 04:05:09 -05001604/* The HVF-specific vCPU thread function. This one should only run when the host
1605 * CPU supports the VMX "unrestricted guest" feature. */
1606static void *qemu_hvf_cpu_thread_fn(void *arg)
1607{
1608 CPUState *cpu = arg;
1609
1610 int r;
1611
1612 assert(hvf_enabled());
1613
1614 rcu_register_thread();
1615
1616 qemu_mutex_lock_iothread();
1617 qemu_thread_get_self(cpu->thread);
1618
1619 cpu->thread_id = qemu_get_thread_id();
1620 cpu->can_do_io = 1;
1621 current_cpu = cpu;
1622
1623 hvf_init_vcpu(cpu);
1624
1625 /* signal CPU creation */
1626 cpu->created = true;
1627 qemu_cond_signal(&qemu_cpu_cond);
1628
1629 do {
1630 if (cpu_can_run(cpu)) {
1631 r = hvf_vcpu_exec(cpu);
1632 if (r == EXCP_DEBUG) {
1633 cpu_handle_guest_debug(cpu);
1634 }
1635 }
Paolo Bonzinidb08b682018-01-11 13:53:12 +01001636 qemu_wait_io_event(cpu);
Sergio Andres Gomez Del Realc97d6d22017-09-13 04:05:09 -05001637 } while (!cpu->unplug || cpu_can_run(cpu));
1638
1639 hvf_vcpu_destroy(cpu);
1640 cpu->created = false;
1641 qemu_cond_signal(&qemu_cpu_cond);
1642 qemu_mutex_unlock_iothread();
Paolo Bonzini8178e632018-01-30 11:05:21 -05001643 rcu_unregister_thread();
Sergio Andres Gomez Del Realc97d6d22017-09-13 04:05:09 -05001644 return NULL;
1645}
1646
Justin Terry (VM)19306802018-01-22 13:07:49 -08001647static void *qemu_whpx_cpu_thread_fn(void *arg)
1648{
1649 CPUState *cpu = arg;
1650 int r;
1651
1652 rcu_register_thread();
1653
1654 qemu_mutex_lock_iothread();
1655 qemu_thread_get_self(cpu->thread);
1656 cpu->thread_id = qemu_get_thread_id();
1657 current_cpu = cpu;
1658
1659 r = whpx_init_vcpu(cpu);
1660 if (r < 0) {
1661 fprintf(stderr, "whpx_init_vcpu failed: %s\n", strerror(-r));
1662 exit(1);
1663 }
1664
1665 /* signal CPU creation */
1666 cpu->created = true;
1667 qemu_cond_signal(&qemu_cpu_cond);
1668
1669 do {
1670 if (cpu_can_run(cpu)) {
1671 r = whpx_vcpu_exec(cpu);
1672 if (r == EXCP_DEBUG) {
1673 cpu_handle_guest_debug(cpu);
1674 }
1675 }
1676 while (cpu_thread_is_idle(cpu)) {
1677 qemu_cond_wait(cpu->halt_cond, &qemu_global_mutex);
1678 }
1679 qemu_wait_io_event_common(cpu);
1680 } while (!cpu->unplug || cpu_can_run(cpu));
1681
1682 whpx_destroy_vcpu(cpu);
1683 cpu->created = false;
1684 qemu_cond_signal(&qemu_cpu_cond);
1685 qemu_mutex_unlock_iothread();
1686 rcu_unregister_thread();
Vincent Palatinb0cb0a62017-01-10 11:59:57 +01001687 return NULL;
1688}
1689
1690#ifdef _WIN32
1691static void CALLBACK dummy_apc_func(ULONG_PTR unused)
1692{
1693}
1694#endif
1695
Alex Bennée37257942017-02-23 18:29:14 +00001696/* Multi-threaded TCG
1697 *
1698 * In the multi-threaded case each vCPU has its own thread. The TLS
1699 * variable current_cpu can be used deep in the code to find the
1700 * current CPUState for a given thread.
1701 */
1702
1703static void *qemu_tcg_cpu_thread_fn(void *arg)
1704{
1705 CPUState *cpu = arg;
1706
Emilio G. Cotaf28d0df2018-06-22 13:45:31 -04001707 assert(tcg_enabled());
Alex Bennéebf51c722017-03-30 18:32:29 +01001708 g_assert(!use_icount);
1709
Alex Bennée37257942017-02-23 18:29:14 +00001710 rcu_register_thread();
Emilio G. Cota3468b592017-07-19 18:57:58 -04001711 tcg_register_thread();
Alex Bennée37257942017-02-23 18:29:14 +00001712
1713 qemu_mutex_lock_iothread();
1714 qemu_thread_get_self(cpu->thread);
1715
1716 cpu->thread_id = qemu_get_thread_id();
1717 cpu->created = true;
1718 cpu->can_do_io = 1;
1719 current_cpu = cpu;
1720 qemu_cond_signal(&qemu_cpu_cond);
1721
1722 /* process any pending work */
1723 cpu->exit_request = 1;
1724
Cédric Le Goater54961aa2018-04-25 15:18:28 +02001725 do {
Alex Bennée37257942017-02-23 18:29:14 +00001726 if (cpu_can_run(cpu)) {
1727 int r;
Alex Bennéed759c952018-02-27 12:52:48 +03001728 qemu_mutex_unlock_iothread();
Alex Bennée37257942017-02-23 18:29:14 +00001729 r = tcg_cpu_exec(cpu);
Alex Bennéed759c952018-02-27 12:52:48 +03001730 qemu_mutex_lock_iothread();
Alex Bennée37257942017-02-23 18:29:14 +00001731 switch (r) {
1732 case EXCP_DEBUG:
1733 cpu_handle_guest_debug(cpu);
1734 break;
1735 case EXCP_HALTED:
1736 /* during start-up the vCPU is reset and the thread is
1737 * kicked several times. If we don't ensure we go back
1738 * to sleep in the halted state we won't cleanly
1739 * start-up when the vCPU is enabled.
1740 *
1741 * cpu->halted should ensure we sleep in wait_io_event
1742 */
1743 g_assert(cpu->halted);
1744 break;
Pranith Kumar08e73c42017-02-23 18:29:15 +00001745 case EXCP_ATOMIC:
1746 qemu_mutex_unlock_iothread();
1747 cpu_exec_step_atomic(cpu);
1748 qemu_mutex_lock_iothread();
Alex Bennée37257942017-02-23 18:29:14 +00001749 default:
1750 /* Ignore everything else? */
1751 break;
1752 }
1753 }
1754
Alex Bennée37257942017-02-23 18:29:14 +00001755 atomic_mb_set(&cpu->exit_request, 0);
Paolo Bonzinidb08b682018-01-11 13:53:12 +01001756 qemu_wait_io_event(cpu);
Paolo Bonzini9b0605f2018-01-30 11:05:06 -05001757 } while (!cpu->unplug || cpu_can_run(cpu));
Alex Bennée37257942017-02-23 18:29:14 +00001758
Paolo Bonzini9b0605f2018-01-30 11:05:06 -05001759 qemu_tcg_destroy_vcpu(cpu);
1760 cpu->created = false;
1761 qemu_cond_signal(&qemu_cpu_cond);
1762 qemu_mutex_unlock_iothread();
1763 rcu_unregister_thread();
Alex Bennée37257942017-02-23 18:29:14 +00001764 return NULL;
1765}
1766
Andreas Färber2ff09a42012-05-03 00:23:30 +02001767static void qemu_cpu_kick_thread(CPUState *cpu)
Paolo Bonzinicc015e92011-03-12 17:44:08 +01001768{
1769#ifndef _WIN32
1770 int err;
1771
Paolo Bonzinie0c38212015-08-26 00:19:19 +02001772 if (cpu->thread_kicked) {
1773 return;
Paolo Bonzini9102ded2015-08-18 06:52:09 -07001774 }
Paolo Bonzinie0c38212015-08-26 00:19:19 +02001775 cpu->thread_kicked = true;
Andreas Färber814e6122012-05-02 17:00:37 +02001776 err = pthread_kill(cpu->thread->thread, SIG_IPI);
Paolo Bonzinicc015e92011-03-12 17:44:08 +01001777 if (err) {
1778 fprintf(stderr, "qemu:%s: %s", __func__, strerror(err));
1779 exit(1);
1780 }
1781#else /* _WIN32 */
Vincent Palatinb0cb0a62017-01-10 11:59:57 +01001782 if (!qemu_cpu_is_self(cpu)) {
Justin Terry (VM)19306802018-01-22 13:07:49 -08001783 if (whpx_enabled()) {
1784 whpx_vcpu_kick(cpu);
1785 } else if (!QueueUserAPC(dummy_apc_func, cpu->hThread, 0)) {
Vincent Palatinb0cb0a62017-01-10 11:59:57 +01001786 fprintf(stderr, "%s: QueueUserAPC failed with error %lu\n",
1787 __func__, GetLastError());
1788 exit(1);
1789 }
1790 }
Paolo Bonzinicc015e92011-03-12 17:44:08 +01001791#endif
1792}
1793
Andreas Färberc08d7422012-05-03 04:34:15 +02001794void qemu_cpu_kick(CPUState *cpu)
Blue Swirl296af7c2010-03-29 19:23:50 +00001795{
Andreas Färberf5c121b2012-05-03 01:22:49 +02001796 qemu_cond_broadcast(cpu->halt_cond);
Paolo Bonzinie0c38212015-08-26 00:19:19 +02001797 if (tcg_enabled()) {
Alex Bennée791158d2017-02-23 18:29:10 +00001798 cpu_exit(cpu);
Alex Bennée37257942017-02-23 18:29:14 +00001799 /* NOP unless doing single-thread RR */
Alex Bennée791158d2017-02-23 18:29:10 +00001800 qemu_cpu_kick_rr_cpu();
Paolo Bonzinie0c38212015-08-26 00:19:19 +02001801 } else {
Vincent Palatinb0cb0a62017-01-10 11:59:57 +01001802 if (hax_enabled()) {
1803 /*
1804 * FIXME: race condition with the exit_request check in
1805 * hax_vcpu_hax_exec
1806 */
1807 cpu->exit_request = 1;
1808 }
Paolo Bonzinie0c38212015-08-26 00:19:19 +02001809 qemu_cpu_kick_thread(cpu);
1810 }
Blue Swirl296af7c2010-03-29 19:23:50 +00001811}
1812
Jan Kiszka46d62fa2011-02-01 22:15:59 +01001813void qemu_cpu_kick_self(void)
1814{
Andreas Färber4917cf42013-05-27 05:17:50 +02001815 assert(current_cpu);
Paolo Bonzini9102ded2015-08-18 06:52:09 -07001816 qemu_cpu_kick_thread(current_cpu);
Blue Swirl296af7c2010-03-29 19:23:50 +00001817}
1818
Andreas Färber60e82572012-05-02 22:23:49 +02001819bool qemu_cpu_is_self(CPUState *cpu)
Blue Swirl296af7c2010-03-29 19:23:50 +00001820{
Andreas Färber814e6122012-05-02 17:00:37 +02001821 return qemu_thread_is_self(cpu->thread);
Blue Swirl296af7c2010-03-29 19:23:50 +00001822}
1823
Paolo Bonzini79e2b9a2015-01-21 12:09:14 +01001824bool qemu_in_vcpu_thread(void)
Juan Quintelaaa723c22012-09-18 16:30:11 +02001825{
Andreas Färber4917cf42013-05-27 05:17:50 +02001826 return current_cpu && qemu_cpu_is_self(current_cpu);
Juan Quintelaaa723c22012-09-18 16:30:11 +02001827}
1828
Paolo Bonziniafbe7052015-06-18 18:47:19 +02001829static __thread bool iothread_locked = false;
1830
1831bool qemu_mutex_iothread_locked(void)
1832{
1833 return iothread_locked;
1834}
1835
Emilio G. Cotacb764d02017-10-28 02:16:41 -04001836/*
1837 * The BQL is taken from so many places that it is worth profiling the
1838 * callers directly, instead of funneling them all through a single function.
1839 */
1840void qemu_mutex_lock_iothread_impl(const char *file, int line)
Blue Swirl296af7c2010-03-29 19:23:50 +00001841{
Emilio G. Cotacb764d02017-10-28 02:16:41 -04001842 QemuMutexLockFunc bql_lock = atomic_read(&qemu_bql_mutex_lock_func);
1843
Jan Kiszka8d04fb52017-02-23 18:29:11 +00001844 g_assert(!qemu_mutex_iothread_locked());
Emilio G. Cotacb764d02017-10-28 02:16:41 -04001845 bql_lock(&qemu_global_mutex, file, line);
Paolo Bonziniafbe7052015-06-18 18:47:19 +02001846 iothread_locked = true;
Blue Swirl296af7c2010-03-29 19:23:50 +00001847}
1848
1849void qemu_mutex_unlock_iothread(void)
1850{
Jan Kiszka8d04fb52017-02-23 18:29:11 +00001851 g_assert(qemu_mutex_iothread_locked());
Paolo Bonziniafbe7052015-06-18 18:47:19 +02001852 iothread_locked = false;
Blue Swirl296af7c2010-03-29 19:23:50 +00001853 qemu_mutex_unlock(&qemu_global_mutex);
1854}
1855
Alex Bennéee8faee02016-10-27 16:09:58 +01001856static bool all_vcpus_paused(void)
Blue Swirl296af7c2010-03-29 19:23:50 +00001857{
Andreas Färberbdc44642013-06-24 23:50:24 +02001858 CPUState *cpu;
Blue Swirl296af7c2010-03-29 19:23:50 +00001859
Andreas Färberbdc44642013-06-24 23:50:24 +02001860 CPU_FOREACH(cpu) {
Andreas Färber182735e2013-05-29 22:29:20 +02001861 if (!cpu->stopped) {
Alex Bennéee8faee02016-10-27 16:09:58 +01001862 return false;
Jan Kiszka0ab07c62011-02-07 12:19:14 +01001863 }
Blue Swirl296af7c2010-03-29 19:23:50 +00001864 }
1865
Alex Bennéee8faee02016-10-27 16:09:58 +01001866 return true;
Blue Swirl296af7c2010-03-29 19:23:50 +00001867}
1868
1869void pause_all_vcpus(void)
1870{
Andreas Färberbdc44642013-06-24 23:50:24 +02001871 CPUState *cpu;
Blue Swirl296af7c2010-03-29 19:23:50 +00001872
Alex Bligh40daca52013-08-21 16:03:02 +01001873 qemu_clock_enable(QEMU_CLOCK_VIRTUAL, false);
Andreas Färberbdc44642013-06-24 23:50:24 +02001874 CPU_FOREACH(cpu) {
David Hildenbrandebd05fe2017-11-29 20:12:15 +01001875 if (qemu_cpu_is_self(cpu)) {
1876 qemu_cpu_stop(cpu, true);
1877 } else {
1878 cpu->stop = true;
1879 qemu_cpu_kick(cpu);
1880 }
Jan Kiszkad798e972012-02-17 18:31:16 +01001881 }
1882
Alex Bennéed759c952018-02-27 12:52:48 +03001883 /* We need to drop the replay_lock so any vCPU threads woken up
1884 * can finish their replay tasks
1885 */
1886 replay_mutex_unlock();
1887
Blue Swirl296af7c2010-03-29 19:23:50 +00001888 while (!all_vcpus_paused()) {
Paolo Bonzinibe7d6c52011-03-12 17:44:02 +01001889 qemu_cond_wait(&qemu_pause_cond, &qemu_global_mutex);
Andreas Färberbdc44642013-06-24 23:50:24 +02001890 CPU_FOREACH(cpu) {
Andreas Färber182735e2013-05-29 22:29:20 +02001891 qemu_cpu_kick(cpu);
Blue Swirl296af7c2010-03-29 19:23:50 +00001892 }
1893 }
Alex Bennéed759c952018-02-27 12:52:48 +03001894
1895 qemu_mutex_unlock_iothread();
1896 replay_mutex_lock();
1897 qemu_mutex_lock_iothread();
Blue Swirl296af7c2010-03-29 19:23:50 +00001898}
1899
Igor Mammedov29936832013-04-23 10:29:37 +02001900void cpu_resume(CPUState *cpu)
1901{
1902 cpu->stop = false;
1903 cpu->stopped = false;
1904 qemu_cpu_kick(cpu);
1905}
1906
Blue Swirl296af7c2010-03-29 19:23:50 +00001907void resume_all_vcpus(void)
1908{
Andreas Färberbdc44642013-06-24 23:50:24 +02001909 CPUState *cpu;
Blue Swirl296af7c2010-03-29 19:23:50 +00001910
Alex Bligh40daca52013-08-21 16:03:02 +01001911 qemu_clock_enable(QEMU_CLOCK_VIRTUAL, true);
Andreas Färberbdc44642013-06-24 23:50:24 +02001912 CPU_FOREACH(cpu) {
Andreas Färber182735e2013-05-29 22:29:20 +02001913 cpu_resume(cpu);
Blue Swirl296af7c2010-03-29 19:23:50 +00001914 }
1915}
1916
Paolo Bonzinidbadee42018-01-30 16:40:12 +01001917void cpu_remove_sync(CPUState *cpu)
Gu Zheng4c055ab2016-05-12 09:18:13 +05301918{
1919 cpu->stop = true;
1920 cpu->unplug = true;
1921 qemu_cpu_kick(cpu);
Paolo Bonzinidbadee42018-01-30 16:40:12 +01001922 qemu_mutex_unlock_iothread();
1923 qemu_thread_join(cpu->thread);
1924 qemu_mutex_lock_iothread();
Bharata B Rao2c579042016-05-12 09:18:14 +05301925}
1926
Dr. David Alan Gilbert49001162014-01-30 10:20:32 +00001927/* For temporary buffers for forming a name */
1928#define VCPU_THREAD_NAME_SIZE 16
1929
Andreas Färbere5ab30a2012-05-03 01:50:44 +02001930static void qemu_tcg_init_vcpu(CPUState *cpu)
Blue Swirl296af7c2010-03-29 19:23:50 +00001931{
Dr. David Alan Gilbert49001162014-01-30 10:20:32 +00001932 char thread_name[VCPU_THREAD_NAME_SIZE];
Alex Bennée37257942017-02-23 18:29:14 +00001933 static QemuCond *single_tcg_halt_cond;
1934 static QemuThread *single_tcg_cpu_thread;
Emilio G. Cotae8feb962017-07-07 19:24:20 -04001935 static int tcg_region_inited;
1936
Emilio G. Cotaf28d0df2018-06-22 13:45:31 -04001937 assert(tcg_enabled());
Emilio G. Cotae8feb962017-07-07 19:24:20 -04001938 /*
1939 * Initialize TCG regions--once. Now is a good time, because:
1940 * (1) TCG's init context, prologue and target globals have been set up.
1941 * (2) qemu_tcg_mttcg_enabled() works now (TCG init code runs before the
1942 * -accel flag is processed, so the check doesn't work then).
1943 */
1944 if (!tcg_region_inited) {
1945 tcg_region_inited = 1;
1946 tcg_region_init();
1947 }
Dr. David Alan Gilbert49001162014-01-30 10:20:32 +00001948
Alex Bennée37257942017-02-23 18:29:14 +00001949 if (qemu_tcg_mttcg_enabled() || !single_tcg_cpu_thread) {
Andreas Färber814e6122012-05-02 17:00:37 +02001950 cpu->thread = g_malloc0(sizeof(QemuThread));
Andreas Färberf5c121b2012-05-03 01:22:49 +02001951 cpu->halt_cond = g_malloc0(sizeof(QemuCond));
1952 qemu_cond_init(cpu->halt_cond);
Alex Bennée37257942017-02-23 18:29:14 +00001953
1954 if (qemu_tcg_mttcg_enabled()) {
1955 /* create a thread per vCPU with TCG (MTTCG) */
1956 parallel_cpus = true;
1957 snprintf(thread_name, VCPU_THREAD_NAME_SIZE, "CPU %d/TCG",
Dr. David Alan Gilbert49001162014-01-30 10:20:32 +00001958 cpu->cpu_index);
Alex Bennée37257942017-02-23 18:29:14 +00001959
1960 qemu_thread_create(cpu->thread, thread_name, qemu_tcg_cpu_thread_fn,
1961 cpu, QEMU_THREAD_JOINABLE);
1962
1963 } else {
1964 /* share a single thread for all cpus with TCG */
1965 snprintf(thread_name, VCPU_THREAD_NAME_SIZE, "ALL CPUs/TCG");
1966 qemu_thread_create(cpu->thread, thread_name,
1967 qemu_tcg_rr_cpu_thread_fn,
1968 cpu, QEMU_THREAD_JOINABLE);
1969
1970 single_tcg_halt_cond = cpu->halt_cond;
1971 single_tcg_cpu_thread = cpu->thread;
1972 }
Paolo Bonzini1ecf47b2011-12-13 13:43:52 +01001973#ifdef _WIN32
Andreas Färber814e6122012-05-02 17:00:37 +02001974 cpu->hThread = qemu_thread_get_handle(cpu->thread);
Paolo Bonzini1ecf47b2011-12-13 13:43:52 +01001975#endif
Blue Swirl296af7c2010-03-29 19:23:50 +00001976 } else {
Alex Bennée37257942017-02-23 18:29:14 +00001977 /* For non-MTTCG cases we share the thread */
1978 cpu->thread = single_tcg_cpu_thread;
1979 cpu->halt_cond = single_tcg_halt_cond;
David Hildenbranda3421732018-02-09 20:52:37 +01001980 cpu->thread_id = first_cpu->thread_id;
1981 cpu->can_do_io = 1;
1982 cpu->created = true;
Blue Swirl296af7c2010-03-29 19:23:50 +00001983 }
1984}
1985
Vincent Palatinb0cb0a62017-01-10 11:59:57 +01001986static void qemu_hax_start_vcpu(CPUState *cpu)
1987{
1988 char thread_name[VCPU_THREAD_NAME_SIZE];
1989
1990 cpu->thread = g_malloc0(sizeof(QemuThread));
1991 cpu->halt_cond = g_malloc0(sizeof(QemuCond));
1992 qemu_cond_init(cpu->halt_cond);
1993
1994 snprintf(thread_name, VCPU_THREAD_NAME_SIZE, "CPU %d/HAX",
1995 cpu->cpu_index);
1996 qemu_thread_create(cpu->thread, thread_name, qemu_hax_cpu_thread_fn,
1997 cpu, QEMU_THREAD_JOINABLE);
1998#ifdef _WIN32
1999 cpu->hThread = qemu_thread_get_handle(cpu->thread);
2000#endif
Vincent Palatinb0cb0a62017-01-10 11:59:57 +01002001}
2002
Andreas Färber48a106b2013-05-27 02:20:39 +02002003static void qemu_kvm_start_vcpu(CPUState *cpu)
Blue Swirl296af7c2010-03-29 19:23:50 +00002004{
Dr. David Alan Gilbert49001162014-01-30 10:20:32 +00002005 char thread_name[VCPU_THREAD_NAME_SIZE];
2006
Andreas Färber814e6122012-05-02 17:00:37 +02002007 cpu->thread = g_malloc0(sizeof(QemuThread));
Andreas Färberf5c121b2012-05-03 01:22:49 +02002008 cpu->halt_cond = g_malloc0(sizeof(QemuCond));
2009 qemu_cond_init(cpu->halt_cond);
Dr. David Alan Gilbert49001162014-01-30 10:20:32 +00002010 snprintf(thread_name, VCPU_THREAD_NAME_SIZE, "CPU %d/KVM",
2011 cpu->cpu_index);
2012 qemu_thread_create(cpu->thread, thread_name, qemu_kvm_cpu_thread_fn,
2013 cpu, QEMU_THREAD_JOINABLE);
Blue Swirl296af7c2010-03-29 19:23:50 +00002014}
2015
Sergio Andres Gomez Del Realc97d6d22017-09-13 04:05:09 -05002016static void qemu_hvf_start_vcpu(CPUState *cpu)
2017{
2018 char thread_name[VCPU_THREAD_NAME_SIZE];
2019
2020 /* HVF currently does not support TCG, and only runs in
2021 * unrestricted-guest mode. */
2022 assert(hvf_enabled());
2023
2024 cpu->thread = g_malloc0(sizeof(QemuThread));
2025 cpu->halt_cond = g_malloc0(sizeof(QemuCond));
2026 qemu_cond_init(cpu->halt_cond);
2027
2028 snprintf(thread_name, VCPU_THREAD_NAME_SIZE, "CPU %d/HVF",
2029 cpu->cpu_index);
2030 qemu_thread_create(cpu->thread, thread_name, qemu_hvf_cpu_thread_fn,
2031 cpu, QEMU_THREAD_JOINABLE);
Sergio Andres Gomez Del Realc97d6d22017-09-13 04:05:09 -05002032}
2033
Justin Terry (VM)19306802018-01-22 13:07:49 -08002034static void qemu_whpx_start_vcpu(CPUState *cpu)
2035{
2036 char thread_name[VCPU_THREAD_NAME_SIZE];
2037
2038 cpu->thread = g_malloc0(sizeof(QemuThread));
2039 cpu->halt_cond = g_malloc0(sizeof(QemuCond));
2040 qemu_cond_init(cpu->halt_cond);
2041 snprintf(thread_name, VCPU_THREAD_NAME_SIZE, "CPU %d/WHPX",
2042 cpu->cpu_index);
2043 qemu_thread_create(cpu->thread, thread_name, qemu_whpx_cpu_thread_fn,
2044 cpu, QEMU_THREAD_JOINABLE);
2045#ifdef _WIN32
2046 cpu->hThread = qemu_thread_get_handle(cpu->thread);
2047#endif
Justin Terry (VM)19306802018-01-22 13:07:49 -08002048}
2049
Andreas Färber10a90212013-05-27 02:24:35 +02002050static void qemu_dummy_start_vcpu(CPUState *cpu)
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +02002051{
Dr. David Alan Gilbert49001162014-01-30 10:20:32 +00002052 char thread_name[VCPU_THREAD_NAME_SIZE];
2053
Andreas Färber814e6122012-05-02 17:00:37 +02002054 cpu->thread = g_malloc0(sizeof(QemuThread));
Andreas Färberf5c121b2012-05-03 01:22:49 +02002055 cpu->halt_cond = g_malloc0(sizeof(QemuCond));
2056 qemu_cond_init(cpu->halt_cond);
Dr. David Alan Gilbert49001162014-01-30 10:20:32 +00002057 snprintf(thread_name, VCPU_THREAD_NAME_SIZE, "CPU %d/DUMMY",
2058 cpu->cpu_index);
2059 qemu_thread_create(cpu->thread, thread_name, qemu_dummy_cpu_thread_fn, cpu,
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +02002060 QEMU_THREAD_JOINABLE);
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +02002061}
2062
Andreas Färberc643bed2013-05-27 03:23:24 +02002063void qemu_init_vcpu(CPUState *cpu)
Blue Swirl296af7c2010-03-29 19:23:50 +00002064{
Andreas Färberce3960e2012-12-17 03:27:07 +01002065 cpu->nr_cores = smp_cores;
2066 cpu->nr_threads = smp_threads;
Andreas Färberf324e762012-05-02 23:26:21 +02002067 cpu->stopped = true;
Peter Maydell56943e82016-01-21 14:15:04 +00002068
2069 if (!cpu->as) {
2070 /* If the target cpu hasn't set up any address spaces itself,
2071 * give it the default one.
2072 */
Peter Maydell12ebc9a2016-01-21 14:15:04 +00002073 cpu->num_ases = 1;
Peter Xu80ceb072017-11-23 17:23:32 +08002074 cpu_address_space_init(cpu, 0, "cpu-memory", cpu->memory);
Peter Maydell56943e82016-01-21 14:15:04 +00002075 }
2076
Jan Kiszka0ab07c62011-02-07 12:19:14 +01002077 if (kvm_enabled()) {
Andreas Färber48a106b2013-05-27 02:20:39 +02002078 qemu_kvm_start_vcpu(cpu);
Vincent Palatinb0cb0a62017-01-10 11:59:57 +01002079 } else if (hax_enabled()) {
2080 qemu_hax_start_vcpu(cpu);
Sergio Andres Gomez Del Realc97d6d22017-09-13 04:05:09 -05002081 } else if (hvf_enabled()) {
2082 qemu_hvf_start_vcpu(cpu);
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +02002083 } else if (tcg_enabled()) {
Andreas Färbere5ab30a2012-05-03 01:50:44 +02002084 qemu_tcg_init_vcpu(cpu);
Justin Terry (VM)19306802018-01-22 13:07:49 -08002085 } else if (whpx_enabled()) {
2086 qemu_whpx_start_vcpu(cpu);
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +02002087 } else {
Andreas Färber10a90212013-05-27 02:24:35 +02002088 qemu_dummy_start_vcpu(cpu);
Jan Kiszka0ab07c62011-02-07 12:19:14 +01002089 }
David Hildenbrand81e96312018-02-09 20:52:38 +01002090
2091 while (!cpu->created) {
2092 qemu_cond_wait(&qemu_cpu_cond, &qemu_global_mutex);
2093 }
Blue Swirl296af7c2010-03-29 19:23:50 +00002094}
2095
Jan Kiszkab4a3d962011-02-01 22:15:43 +01002096void cpu_stop_current(void)
Blue Swirl296af7c2010-03-29 19:23:50 +00002097{
Andreas Färber4917cf42013-05-27 05:17:50 +02002098 if (current_cpu) {
David Hildenbrandebd05fe2017-11-29 20:12:15 +01002099 qemu_cpu_stop(current_cpu, true);
Jan Kiszkab4a3d962011-02-01 22:15:43 +01002100 }
Blue Swirl296af7c2010-03-29 19:23:50 +00002101}
2102
Kevin Wolf56983462013-07-05 13:49:54 +02002103int vm_stop(RunState state)
Blue Swirl296af7c2010-03-29 19:23:50 +00002104{
Juan Quintelaaa723c22012-09-18 16:30:11 +02002105 if (qemu_in_vcpu_thread()) {
Paolo Bonzini74892d22014-06-05 14:53:58 +02002106 qemu_system_vmstop_request_prepare();
Luiz Capitulino1dfb4dd2011-07-29 14:26:33 -03002107 qemu_system_vmstop_request(state);
Blue Swirl296af7c2010-03-29 19:23:50 +00002108 /*
2109 * FIXME: should not return to device code in case
2110 * vm_stop() has been requested.
2111 */
Jan Kiszkab4a3d962011-02-01 22:15:43 +01002112 cpu_stop_current();
Kevin Wolf56983462013-07-05 13:49:54 +02002113 return 0;
Blue Swirl296af7c2010-03-29 19:23:50 +00002114 }
Kevin Wolf56983462013-07-05 13:49:54 +02002115
Stefan Hajnoczi4486e892018-03-07 14:42:05 +00002116 return do_vm_stop(state, true);
Blue Swirl296af7c2010-03-29 19:23:50 +00002117}
2118
Claudio Imbrenda2d76e822017-02-14 18:07:47 +01002119/**
2120 * Prepare for (re)starting the VM.
2121 * Returns -1 if the vCPUs are not to be restarted (e.g. if they are already
2122 * running or in case of an error condition), 0 otherwise.
2123 */
2124int vm_prepare_start(void)
2125{
2126 RunState requested;
Claudio Imbrenda2d76e822017-02-14 18:07:47 +01002127
2128 qemu_vmstop_requested(&requested);
2129 if (runstate_is_running() && requested == RUN_STATE__MAX) {
2130 return -1;
2131 }
2132
2133 /* Ensure that a STOP/RESUME pair of events is emitted if a
2134 * vmstop request was pending. The BLOCK_IO_ERROR event, for
2135 * example, according to documentation is always followed by
2136 * the STOP event.
2137 */
2138 if (runstate_is_running()) {
Peter Xu3ab72382018-08-15 21:37:37 +08002139 qapi_event_send_stop();
2140 qapi_event_send_resume();
Markus Armbrusterf0561582018-04-23 10:45:18 +02002141 return -1;
Claudio Imbrenda2d76e822017-02-14 18:07:47 +01002142 }
2143
2144 /* We are sending this now, but the CPUs will be resumed shortly later */
Peter Xu3ab72382018-08-15 21:37:37 +08002145 qapi_event_send_resume();
Markus Armbrusterf0561582018-04-23 10:45:18 +02002146
2147 replay_enable_events();
2148 cpu_enable_ticks();
2149 runstate_set(RUN_STATE_RUNNING);
2150 vm_state_notify(1, RUN_STATE_RUNNING);
2151 return 0;
Claudio Imbrenda2d76e822017-02-14 18:07:47 +01002152}
2153
2154void vm_start(void)
2155{
2156 if (!vm_prepare_start()) {
2157 resume_all_vcpus();
2158 }
2159}
2160
Luiz Capitulino8a9236f2011-10-14 11:18:09 -03002161/* does a state transition even if the VM is already stopped,
2162 current state is forgotten forever */
Kevin Wolf56983462013-07-05 13:49:54 +02002163int vm_stop_force_state(RunState state)
Luiz Capitulino8a9236f2011-10-14 11:18:09 -03002164{
2165 if (runstate_is_running()) {
Kevin Wolf56983462013-07-05 13:49:54 +02002166 return vm_stop(state);
Luiz Capitulino8a9236f2011-10-14 11:18:09 -03002167 } else {
2168 runstate_set(state);
Wen Congyangb2780d32015-11-20 17:34:38 +08002169
2170 bdrv_drain_all();
Kevin Wolf594a45c2013-07-18 14:52:19 +02002171 /* Make sure to return an error if the flush in a previous vm_stop()
2172 * failed. */
John Snow22af08e2016-09-22 21:45:51 -04002173 return bdrv_flush_all();
Luiz Capitulino8a9236f2011-10-14 11:18:09 -03002174 }
2175}
2176
Stefan Weil9a78eea2010-10-22 23:03:33 +02002177void list_cpus(FILE *f, fprintf_function cpu_fprintf, const char *optarg)
Blue Swirl262353c2010-05-04 19:55:35 +00002178{
2179 /* XXX: implement xxx_cpu_list for targets that still miss it */
Peter Maydelle916cbf2012-09-05 17:41:08 -03002180#if defined(cpu_list)
2181 cpu_list(f, cpu_fprintf);
Blue Swirl262353c2010-05-04 19:55:35 +00002182#endif
2183}
Luiz Capitulinode0b36b2011-09-21 16:38:35 -03002184
2185CpuInfoList *qmp_query_cpus(Error **errp)
2186{
Igor Mammedovafed5a52017-05-10 13:29:55 +02002187 MachineState *ms = MACHINE(qdev_get_machine());
2188 MachineClass *mc = MACHINE_GET_CLASS(ms);
Luiz Capitulinode0b36b2011-09-21 16:38:35 -03002189 CpuInfoList *head = NULL, *cur_item = NULL;
Andreas Färber182735e2013-05-29 22:29:20 +02002190 CPUState *cpu;
Luiz Capitulinode0b36b2011-09-21 16:38:35 -03002191
Andreas Färberbdc44642013-06-24 23:50:24 +02002192 CPU_FOREACH(cpu) {
Luiz Capitulinode0b36b2011-09-21 16:38:35 -03002193 CpuInfoList *info;
Andreas Färber182735e2013-05-29 22:29:20 +02002194#if defined(TARGET_I386)
2195 X86CPU *x86_cpu = X86_CPU(cpu);
2196 CPUX86State *env = &x86_cpu->env;
2197#elif defined(TARGET_PPC)
2198 PowerPCCPU *ppc_cpu = POWERPC_CPU(cpu);
2199 CPUPPCState *env = &ppc_cpu->env;
2200#elif defined(TARGET_SPARC)
2201 SPARCCPU *sparc_cpu = SPARC_CPU(cpu);
2202 CPUSPARCState *env = &sparc_cpu->env;
Michael Clark25fa1942018-03-03 01:32:59 +13002203#elif defined(TARGET_RISCV)
2204 RISCVCPU *riscv_cpu = RISCV_CPU(cpu);
2205 CPURISCVState *env = &riscv_cpu->env;
Andreas Färber182735e2013-05-29 22:29:20 +02002206#elif defined(TARGET_MIPS)
2207 MIPSCPU *mips_cpu = MIPS_CPU(cpu);
2208 CPUMIPSState *env = &mips_cpu->env;
Bastian Koppelmann48e06fe2014-09-01 12:59:46 +01002209#elif defined(TARGET_TRICORE)
2210 TriCoreCPU *tricore_cpu = TRICORE_CPU(cpu);
2211 CPUTriCoreState *env = &tricore_cpu->env;
Viktor Mihajlovski9d0306d2018-02-16 17:08:37 +01002212#elif defined(TARGET_S390X)
2213 S390CPU *s390_cpu = S390_CPU(cpu);
2214 CPUS390XState *env = &s390_cpu->env;
Andreas Färber182735e2013-05-29 22:29:20 +02002215#endif
Luiz Capitulinode0b36b2011-09-21 16:38:35 -03002216
Andreas Färbercb446ec2013-05-01 14:24:52 +02002217 cpu_synchronize_state(cpu);
Luiz Capitulinode0b36b2011-09-21 16:38:35 -03002218
2219 info = g_malloc0(sizeof(*info));
2220 info->value = g_malloc0(sizeof(*info->value));
Andreas Färber55e5c282012-12-17 06:18:02 +01002221 info->value->CPU = cpu->cpu_index;
Andreas Färber182735e2013-05-29 22:29:20 +02002222 info->value->current = (cpu == first_cpu);
Andreas Färber259186a2013-01-17 18:51:17 +01002223 info->value->halted = cpu->halted;
Eduardo Habkost58f88d42015-05-08 16:04:22 -03002224 info->value->qom_path = object_get_canonical_path(OBJECT(cpu));
Andreas Färber9f09e182012-05-03 06:59:07 +02002225 info->value->thread_id = cpu->thread_id;
Luiz Capitulinode0b36b2011-09-21 16:38:35 -03002226#if defined(TARGET_I386)
Eric Blake86f4b682015-11-18 01:52:59 -07002227 info->value->arch = CPU_INFO_ARCH_X86;
Eric Blake544a3732016-02-17 23:48:27 -07002228 info->value->u.x86.pc = env->eip + env->segs[R_CS].base;
Luiz Capitulinode0b36b2011-09-21 16:38:35 -03002229#elif defined(TARGET_PPC)
Eric Blake86f4b682015-11-18 01:52:59 -07002230 info->value->arch = CPU_INFO_ARCH_PPC;
Eric Blake544a3732016-02-17 23:48:27 -07002231 info->value->u.ppc.nip = env->nip;
Luiz Capitulinode0b36b2011-09-21 16:38:35 -03002232#elif defined(TARGET_SPARC)
Eric Blake86f4b682015-11-18 01:52:59 -07002233 info->value->arch = CPU_INFO_ARCH_SPARC;
Eric Blake544a3732016-02-17 23:48:27 -07002234 info->value->u.q_sparc.pc = env->pc;
2235 info->value->u.q_sparc.npc = env->npc;
Luiz Capitulinode0b36b2011-09-21 16:38:35 -03002236#elif defined(TARGET_MIPS)
Eric Blake86f4b682015-11-18 01:52:59 -07002237 info->value->arch = CPU_INFO_ARCH_MIPS;
Eric Blake544a3732016-02-17 23:48:27 -07002238 info->value->u.q_mips.PC = env->active_tc.PC;
Bastian Koppelmann48e06fe2014-09-01 12:59:46 +01002239#elif defined(TARGET_TRICORE)
Eric Blake86f4b682015-11-18 01:52:59 -07002240 info->value->arch = CPU_INFO_ARCH_TRICORE;
Eric Blake544a3732016-02-17 23:48:27 -07002241 info->value->u.tricore.PC = env->PC;
Viktor Mihajlovski9d0306d2018-02-16 17:08:37 +01002242#elif defined(TARGET_S390X)
2243 info->value->arch = CPU_INFO_ARCH_S390;
2244 info->value->u.s390.cpu_state = env->cpu_state;
Michael Clark25fa1942018-03-03 01:32:59 +13002245#elif defined(TARGET_RISCV)
2246 info->value->arch = CPU_INFO_ARCH_RISCV;
2247 info->value->u.riscv.pc = env->pc;
Eric Blake86f4b682015-11-18 01:52:59 -07002248#else
2249 info->value->arch = CPU_INFO_ARCH_OTHER;
Luiz Capitulinode0b36b2011-09-21 16:38:35 -03002250#endif
Igor Mammedovafed5a52017-05-10 13:29:55 +02002251 info->value->has_props = !!mc->cpu_index_to_instance_props;
2252 if (info->value->has_props) {
2253 CpuInstanceProperties *props;
2254 props = g_malloc0(sizeof(*props));
2255 *props = mc->cpu_index_to_instance_props(ms, cpu->cpu_index);
2256 info->value->props = props;
2257 }
Luiz Capitulinode0b36b2011-09-21 16:38:35 -03002258
2259 /* XXX: waiting for the qapi to support GSList */
2260 if (!cur_item) {
2261 head = cur_item = info;
2262 } else {
2263 cur_item->next = info;
2264 cur_item = info;
2265 }
2266 }
2267
2268 return head;
2269}
Luiz Capitulino0cfd6a92011-11-22 16:32:37 -02002270
Laszlo Ersekdaa9d2b2018-04-27 21:28:51 +02002271static CpuInfoArch sysemu_target_to_cpuinfo_arch(SysEmuTarget target)
2272{
2273 /*
2274 * The @SysEmuTarget -> @CpuInfoArch mapping below is based on the
2275 * TARGET_ARCH -> TARGET_BASE_ARCH mapping in the "configure" script.
2276 */
2277 switch (target) {
2278 case SYS_EMU_TARGET_I386:
2279 case SYS_EMU_TARGET_X86_64:
2280 return CPU_INFO_ARCH_X86;
2281
2282 case SYS_EMU_TARGET_PPC:
Laszlo Ersekdaa9d2b2018-04-27 21:28:51 +02002283 case SYS_EMU_TARGET_PPC64:
2284 return CPU_INFO_ARCH_PPC;
2285
2286 case SYS_EMU_TARGET_SPARC:
2287 case SYS_EMU_TARGET_SPARC64:
2288 return CPU_INFO_ARCH_SPARC;
2289
2290 case SYS_EMU_TARGET_MIPS:
2291 case SYS_EMU_TARGET_MIPSEL:
2292 case SYS_EMU_TARGET_MIPS64:
2293 case SYS_EMU_TARGET_MIPS64EL:
2294 return CPU_INFO_ARCH_MIPS;
2295
2296 case SYS_EMU_TARGET_TRICORE:
2297 return CPU_INFO_ARCH_TRICORE;
2298
2299 case SYS_EMU_TARGET_S390X:
2300 return CPU_INFO_ARCH_S390;
2301
2302 case SYS_EMU_TARGET_RISCV32:
2303 case SYS_EMU_TARGET_RISCV64:
2304 return CPU_INFO_ARCH_RISCV;
2305
2306 default:
2307 return CPU_INFO_ARCH_OTHER;
2308 }
2309}
2310
2311static void cpustate_to_cpuinfo_s390(CpuInfoS390 *info, const CPUState *cpu)
2312{
2313#ifdef TARGET_S390X
2314 S390CPU *s390_cpu = S390_CPU(cpu);
2315 CPUS390XState *env = &s390_cpu->env;
2316
2317 info->cpu_state = env->cpu_state;
2318#else
2319 abort();
2320#endif
2321}
2322
Luiz Capitulinoce74ee32018-02-16 17:08:38 +01002323/*
2324 * fast means: we NEVER interrupt vCPU threads to retrieve
2325 * information from KVM.
2326 */
2327CpuInfoFastList *qmp_query_cpus_fast(Error **errp)
2328{
2329 MachineState *ms = MACHINE(qdev_get_machine());
2330 MachineClass *mc = MACHINE_GET_CLASS(ms);
2331 CpuInfoFastList *head = NULL, *cur_item = NULL;
Laszlo Ersekdaa9d2b2018-04-27 21:28:51 +02002332 SysEmuTarget target = qapi_enum_parse(&SysEmuTarget_lookup, TARGET_NAME,
2333 -1, &error_abort);
Luiz Capitulinoce74ee32018-02-16 17:08:38 +01002334 CPUState *cpu;
2335
2336 CPU_FOREACH(cpu) {
2337 CpuInfoFastList *info = g_malloc0(sizeof(*info));
2338 info->value = g_malloc0(sizeof(*info->value));
2339
2340 info->value->cpu_index = cpu->cpu_index;
2341 info->value->qom_path = object_get_canonical_path(OBJECT(cpu));
2342 info->value->thread_id = cpu->thread_id;
2343
2344 info->value->has_props = !!mc->cpu_index_to_instance_props;
2345 if (info->value->has_props) {
2346 CpuInstanceProperties *props;
2347 props = g_malloc0(sizeof(*props));
2348 *props = mc->cpu_index_to_instance_props(ms, cpu->cpu_index);
2349 info->value->props = props;
2350 }
2351
Laszlo Ersekdaa9d2b2018-04-27 21:28:51 +02002352 info->value->arch = sysemu_target_to_cpuinfo_arch(target);
2353 info->value->target = target;
2354 if (target == SYS_EMU_TARGET_S390X) {
2355 cpustate_to_cpuinfo_s390(&info->value->u.s390x, cpu);
Laszlo Ersekdaa9d2b2018-04-27 21:28:51 +02002356 }
2357
Luiz Capitulinoce74ee32018-02-16 17:08:38 +01002358 if (!cur_item) {
2359 head = cur_item = info;
2360 } else {
2361 cur_item->next = info;
2362 cur_item = info;
2363 }
2364 }
2365
2366 return head;
2367}
2368
Luiz Capitulino0cfd6a92011-11-22 16:32:37 -02002369void qmp_memsave(int64_t addr, int64_t size, const char *filename,
2370 bool has_cpu, int64_t cpu_index, Error **errp)
2371{
2372 FILE *f;
2373 uint32_t l;
Andreas Färber55e5c282012-12-17 06:18:02 +01002374 CPUState *cpu;
Luiz Capitulino0cfd6a92011-11-22 16:32:37 -02002375 uint8_t buf[1024];
Borislav Petkov0dc9daf2015-02-08 13:14:38 +01002376 int64_t orig_addr = addr, orig_size = size;
Luiz Capitulino0cfd6a92011-11-22 16:32:37 -02002377
2378 if (!has_cpu) {
2379 cpu_index = 0;
2380 }
2381
Andreas Färber151d1322013-02-15 15:41:49 +01002382 cpu = qemu_get_cpu(cpu_index);
2383 if (cpu == NULL) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01002384 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "cpu-index",
2385 "a CPU number");
Luiz Capitulino0cfd6a92011-11-22 16:32:37 -02002386 return;
2387 }
2388
2389 f = fopen(filename, "wb");
2390 if (!f) {
Luiz Capitulino618da852013-06-07 14:35:06 -04002391 error_setg_file_open(errp, errno, filename);
Luiz Capitulino0cfd6a92011-11-22 16:32:37 -02002392 return;
2393 }
2394
2395 while (size != 0) {
2396 l = sizeof(buf);
2397 if (l > size)
2398 l = size;
Aneesh Kumar K.V2f4d0f52013-10-01 21:49:30 +05302399 if (cpu_memory_rw_debug(cpu, addr, buf, l, 0) != 0) {
Borislav Petkov0dc9daf2015-02-08 13:14:38 +01002400 error_setg(errp, "Invalid addr 0x%016" PRIx64 "/size %" PRId64
2401 " specified", orig_addr, orig_size);
Aneesh Kumar K.V2f4d0f52013-10-01 21:49:30 +05302402 goto exit;
2403 }
Luiz Capitulino0cfd6a92011-11-22 16:32:37 -02002404 if (fwrite(buf, 1, l, f) != l) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01002405 error_setg(errp, QERR_IO_ERROR);
Luiz Capitulino0cfd6a92011-11-22 16:32:37 -02002406 goto exit;
2407 }
2408 addr += l;
2409 size -= l;
2410 }
2411
2412exit:
2413 fclose(f);
2414}
Luiz Capitulino6d3962b2011-11-22 17:26:46 -02002415
2416void qmp_pmemsave(int64_t addr, int64_t size, const char *filename,
2417 Error **errp)
2418{
2419 FILE *f;
2420 uint32_t l;
2421 uint8_t buf[1024];
2422
2423 f = fopen(filename, "wb");
2424 if (!f) {
Luiz Capitulino618da852013-06-07 14:35:06 -04002425 error_setg_file_open(errp, errno, filename);
Luiz Capitulino6d3962b2011-11-22 17:26:46 -02002426 return;
2427 }
2428
2429 while (size != 0) {
2430 l = sizeof(buf);
2431 if (l > size)
2432 l = size;
Stefan Weileb6282f2014-04-07 20:28:23 +02002433 cpu_physical_memory_read(addr, buf, l);
Luiz Capitulino6d3962b2011-11-22 17:26:46 -02002434 if (fwrite(buf, 1, l, f) != l) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01002435 error_setg(errp, QERR_IO_ERROR);
Luiz Capitulino6d3962b2011-11-22 17:26:46 -02002436 goto exit;
2437 }
2438 addr += l;
2439 size -= l;
2440 }
2441
2442exit:
2443 fclose(f);
2444}
Luiz Capitulinoab49ab52011-11-23 12:55:53 -02002445
2446void qmp_inject_nmi(Error **errp)
2447{
Alexey Kardashevskiy9cb805f2014-08-20 22:16:33 +10002448 nmi_monitor_handle(monitor_get_cpu_index(), errp);
Luiz Capitulinoab49ab52011-11-23 12:55:53 -02002449}
Sebastian Tanase27498be2014-07-25 11:56:33 +02002450
2451void dump_drift_info(FILE *f, fprintf_function cpu_fprintf)
2452{
2453 if (!use_icount) {
2454 return;
2455 }
2456
2457 cpu_fprintf(f, "Host - Guest clock %"PRIi64" ms\n",
2458 (cpu_get_clock() - cpu_get_icount())/SCALE_MS);
2459 if (icount_align_option) {
2460 cpu_fprintf(f, "Max guest delay %"PRIi64" ms\n", -max_delay/SCALE_MS);
2461 cpu_fprintf(f, "Max guest advance %"PRIi64" ms\n", max_advance/SCALE_MS);
2462 } else {
2463 cpu_fprintf(f, "Max guest delay NA\n");
2464 cpu_fprintf(f, "Max guest advance NA\n");
2465 }
2466}