blob: 4abc3b3dda2d3d0f76e008c4bd894e723c7c5139 [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
Alex Bennéec34c7622017-02-28 14:40:17 +0000214 error_report("Guest not yet converted to MTTCG - "
215 "you may get unexpected results");
216#endif
KONRAD Frederic8d4e9142017-02-23 18:29:08 +0000217 if (!check_tcg_memory_orders_compatible()) {
218 error_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 */
248void cpu_update_icount(CPUState *cpu)
249{
250 int64_t executed = cpu_get_icount_executed(cpu);
251 cpu->icount_budget -= executed;
252
Paolo Bonzini94377112018-08-14 09:57:16 +0200253#ifndef CONFIG_ATOMIC64
254 seqlock_write_lock(&timers_state.vm_clock_seqlock,
255 &timers_state.vm_clock_lock);
256#endif
Alex Bennée512d3c82017-04-05 12:32:37 +0100257 atomic_set__nocheck(&timers_state.qemu_icount,
Paolo Bonzinic1ff0732018-08-14 09:31:58 +0200258 timers_state.qemu_icount + executed);
Paolo Bonzini94377112018-08-14 09:57:16 +0200259#ifndef CONFIG_ATOMIC64
260 seqlock_write_unlock(&timers_state.vm_clock_seqlock,
261 &timers_state.vm_clock_lock);
Alex Bennée512d3c82017-04-05 12:32:37 +0100262#endif
263}
264
Paolo Bonzinic1ff0732018-08-14 09:31:58 +0200265static int64_t cpu_get_icount_raw_locked(void)
Paolo Bonzini946fb272011-09-12 13:57:37 +0200266{
Andreas Färber4917cf42013-05-27 05:17:50 +0200267 CPUState *cpu = current_cpu;
Paolo Bonzini946fb272011-09-12 13:57:37 +0200268
Alex Bennée243c5f72017-03-30 18:49:22 +0100269 if (cpu && cpu->running) {
Paolo Bonzini414b15c2015-06-24 14:16:26 +0200270 if (!cpu->can_do_io) {
Alistair Francis493d89b2018-02-03 09:43:14 +0100271 error_report("Bad icount read");
Pavel Dovgalyuk2a629142014-12-08 10:53:45 +0300272 exit(1);
Paolo Bonzini946fb272011-09-12 13:57:37 +0200273 }
Alex Bennéee4cd9652017-03-31 16:09:42 +0100274 /* Take into account what has run */
Alex Bennée1d059062017-04-05 10:53:47 +0100275 cpu_update_icount(cpu);
Paolo Bonzini946fb272011-09-12 13:57:37 +0200276 }
Paolo Bonzinic1ff0732018-08-14 09:31:58 +0200277 /* The read is protected by the seqlock, so __nocheck is okay. */
Alex Bennée1d059062017-04-05 10:53:47 +0100278 return atomic_read__nocheck(&timers_state.qemu_icount);
Paolo Bonzinic1ff0732018-08-14 09:31:58 +0200279}
280
281static int64_t cpu_get_icount_locked(void)
282{
283 int64_t icount = cpu_get_icount_raw_locked();
284 return atomic_read__nocheck(&timers_state.qemu_icount_bias) + cpu_icount_to_ns(icount);
285}
286
287int64_t cpu_get_icount_raw(void)
288{
289 int64_t icount;
290 unsigned start;
291
292 do {
293 start = seqlock_read_begin(&timers_state.vm_clock_seqlock);
294 icount = cpu_get_icount_raw_locked();
295 } while (seqlock_read_retry(&timers_state.vm_clock_seqlock, start));
296
297 return icount;
Pavel Dovgalyuk2a629142014-12-08 10:53:45 +0300298}
299
300/* Return the virtual CPU time, based on the instruction counter. */
Paolo Bonzini17a15f12013-10-03 15:17:25 +0200301int64_t cpu_get_icount(void)
302{
303 int64_t icount;
304 unsigned start;
305
306 do {
307 start = seqlock_read_begin(&timers_state.vm_clock_seqlock);
308 icount = cpu_get_icount_locked();
309 } while (seqlock_read_retry(&timers_state.vm_clock_seqlock, start));
310
311 return icount;
312}
313
KONRAD Frederic3f031312014-08-01 01:37:15 +0200314int64_t cpu_icount_to_ns(int64_t icount)
315{
Paolo Bonzinic1ff0732018-08-14 09:31:58 +0200316 return icount << atomic_read(&timers_state.icount_time_shift);
KONRAD Frederic3f031312014-08-01 01:37:15 +0200317}
318
Paolo Bonzinif2a4ad62018-08-18 09:36:16 +0200319static int64_t cpu_get_ticks_locked(void)
320{
321 int64_t ticks = timers_state.cpu_ticks_offset;
322 if (timers_state.cpu_ticks_enabled) {
323 ticks += cpu_get_host_ticks();
324 }
325
326 if (timers_state.cpu_ticks_prev > ticks) {
327 /* Non increasing ticks may happen if the host uses software suspend. */
328 timers_state.cpu_ticks_offset += timers_state.cpu_ticks_prev - ticks;
329 ticks = timers_state.cpu_ticks_prev;
330 }
331
332 timers_state.cpu_ticks_prev = ticks;
333 return ticks;
334}
335
Cao jind90f3cc2016-07-29 19:05:38 +0800336/* return the time elapsed in VM between vm_start and vm_stop. Unless
337 * icount is active, cpu_get_ticks() uses units of the host CPU cycle
338 * counter.
Cao jind90f3cc2016-07-29 19:05:38 +0800339 */
Paolo Bonzini946fb272011-09-12 13:57:37 +0200340int64_t cpu_get_ticks(void)
341{
Paolo Bonzini5f3e3102013-10-28 17:32:18 +0100342 int64_t ticks;
343
Paolo Bonzini946fb272011-09-12 13:57:37 +0200344 if (use_icount) {
345 return cpu_get_icount();
346 }
Paolo Bonzini5f3e3102013-10-28 17:32:18 +0100347
Paolo Bonzinif2a4ad62018-08-18 09:36:16 +0200348 qemu_spin_lock(&timers_state.vm_clock_lock);
349 ticks = cpu_get_ticks_locked();
350 qemu_spin_unlock(&timers_state.vm_clock_lock);
Paolo Bonzini5f3e3102013-10-28 17:32:18 +0100351 return ticks;
Paolo Bonzini946fb272011-09-12 13:57:37 +0200352}
353
Liu Ping Fancb365642013-09-25 14:20:58 +0800354static int64_t cpu_get_clock_locked(void)
355{
Cao jin1d45cea2016-07-29 19:05:37 +0800356 int64_t time;
Liu Ping Fancb365642013-09-25 14:20:58 +0800357
Cao jin1d45cea2016-07-29 19:05:37 +0800358 time = timers_state.cpu_clock_offset;
Paolo Bonzini5f3e3102013-10-28 17:32:18 +0100359 if (timers_state.cpu_ticks_enabled) {
Cao jin1d45cea2016-07-29 19:05:37 +0800360 time += get_clock();
Liu Ping Fancb365642013-09-25 14:20:58 +0800361 }
362
Cao jin1d45cea2016-07-29 19:05:37 +0800363 return time;
Liu Ping Fancb365642013-09-25 14:20:58 +0800364}
365
Cao jind90f3cc2016-07-29 19:05:38 +0800366/* Return the monotonic time elapsed in VM, i.e.,
Peter Maydell8212ff82016-09-15 10:24:22 +0100367 * the time between vm_start and vm_stop
368 */
Paolo Bonzini946fb272011-09-12 13:57:37 +0200369int64_t cpu_get_clock(void)
370{
371 int64_t ti;
Liu Ping Fancb365642013-09-25 14:20:58 +0800372 unsigned start;
373
374 do {
375 start = seqlock_read_begin(&timers_state.vm_clock_seqlock);
376 ti = cpu_get_clock_locked();
377 } while (seqlock_read_retry(&timers_state.vm_clock_seqlock, start));
378
379 return ti;
Paolo Bonzini946fb272011-09-12 13:57:37 +0200380}
381
Liu Ping Fancb365642013-09-25 14:20:58 +0800382/* enable cpu_get_ticks()
Cao jin3224e872016-07-08 18:31:37 +0800383 * Caller must hold BQL which serves as mutex for vm_clock_seqlock.
Liu Ping Fancb365642013-09-25 14:20:58 +0800384 */
Paolo Bonzini946fb272011-09-12 13:57:37 +0200385void cpu_enable_ticks(void)
386{
Paolo Bonzini94377112018-08-14 09:57:16 +0200387 seqlock_write_lock(&timers_state.vm_clock_seqlock,
388 &timers_state.vm_clock_lock);
Paolo Bonzini946fb272011-09-12 13:57:37 +0200389 if (!timers_state.cpu_ticks_enabled) {
Christopher Covington4a7428c2015-09-25 10:42:21 -0400390 timers_state.cpu_ticks_offset -= cpu_get_host_ticks();
Paolo Bonzini946fb272011-09-12 13:57:37 +0200391 timers_state.cpu_clock_offset -= get_clock();
392 timers_state.cpu_ticks_enabled = 1;
393 }
Paolo Bonzini94377112018-08-14 09:57:16 +0200394 seqlock_write_unlock(&timers_state.vm_clock_seqlock,
395 &timers_state.vm_clock_lock);
Paolo Bonzini946fb272011-09-12 13:57:37 +0200396}
397
398/* disable cpu_get_ticks() : the clock is stopped. You must not call
Liu Ping Fancb365642013-09-25 14:20:58 +0800399 * cpu_get_ticks() after that.
Cao jin3224e872016-07-08 18:31:37 +0800400 * Caller must hold BQL which serves as mutex for vm_clock_seqlock.
Liu Ping Fancb365642013-09-25 14:20:58 +0800401 */
Paolo Bonzini946fb272011-09-12 13:57:37 +0200402void cpu_disable_ticks(void)
403{
Paolo Bonzini94377112018-08-14 09:57:16 +0200404 seqlock_write_lock(&timers_state.vm_clock_seqlock,
405 &timers_state.vm_clock_lock);
Paolo Bonzini946fb272011-09-12 13:57:37 +0200406 if (timers_state.cpu_ticks_enabled) {
Christopher Covington4a7428c2015-09-25 10:42:21 -0400407 timers_state.cpu_ticks_offset += cpu_get_host_ticks();
Liu Ping Fancb365642013-09-25 14:20:58 +0800408 timers_state.cpu_clock_offset = cpu_get_clock_locked();
Paolo Bonzini946fb272011-09-12 13:57:37 +0200409 timers_state.cpu_ticks_enabled = 0;
410 }
Paolo Bonzini94377112018-08-14 09:57:16 +0200411 seqlock_write_unlock(&timers_state.vm_clock_seqlock,
412 &timers_state.vm_clock_lock);
Paolo Bonzini946fb272011-09-12 13:57:37 +0200413}
414
415/* Correlation between real and virtual time is always going to be
416 fairly approximate, so ignore small variation.
417 When the guest is idle real and virtual time will be aligned in
418 the IO wait loop. */
Rutuja Shah73bcb242016-03-21 21:32:30 +0530419#define ICOUNT_WOBBLE (NANOSECONDS_PER_SECOND / 10)
Paolo Bonzini946fb272011-09-12 13:57:37 +0200420
421static void icount_adjust(void)
422{
423 int64_t cur_time;
424 int64_t cur_icount;
425 int64_t delta;
Paolo Bonzinia3270e12013-10-07 17:18:15 +0200426
427 /* Protected by TimersState mutex. */
Paolo Bonzini946fb272011-09-12 13:57:37 +0200428 static int64_t last_delta;
Paolo Bonzini468cc7c2013-10-07 17:21:51 +0200429
Paolo Bonzini946fb272011-09-12 13:57:37 +0200430 /* If the VM is not running, then do nothing. */
431 if (!runstate_is_running()) {
432 return;
433 }
Paolo Bonzini468cc7c2013-10-07 17:21:51 +0200434
Paolo Bonzini94377112018-08-14 09:57:16 +0200435 seqlock_write_lock(&timers_state.vm_clock_seqlock,
436 &timers_state.vm_clock_lock);
Paolo Bonzini17a15f12013-10-03 15:17:25 +0200437 cur_time = cpu_get_clock_locked();
438 cur_icount = cpu_get_icount_locked();
Paolo Bonzini468cc7c2013-10-07 17:21:51 +0200439
Paolo Bonzini946fb272011-09-12 13:57:37 +0200440 delta = cur_icount - cur_time;
441 /* FIXME: This is a very crude algorithm, somewhat prone to oscillation. */
442 if (delta > 0
443 && last_delta + ICOUNT_WOBBLE < delta * 2
Paolo Bonzinic1ff0732018-08-14 09:31:58 +0200444 && timers_state.icount_time_shift > 0) {
Paolo Bonzini946fb272011-09-12 13:57:37 +0200445 /* The guest is getting too far ahead. Slow time down. */
Paolo Bonzinic1ff0732018-08-14 09:31:58 +0200446 atomic_set(&timers_state.icount_time_shift,
447 timers_state.icount_time_shift - 1);
Paolo Bonzini946fb272011-09-12 13:57:37 +0200448 }
449 if (delta < 0
450 && last_delta - ICOUNT_WOBBLE > delta * 2
Paolo Bonzinic1ff0732018-08-14 09:31:58 +0200451 && timers_state.icount_time_shift < MAX_ICOUNT_SHIFT) {
Paolo Bonzini946fb272011-09-12 13:57:37 +0200452 /* The guest is getting too far behind. Speed time up. */
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 last_delta = delta;
Paolo Bonzinic1ff0732018-08-14 09:31:58 +0200457 atomic_set__nocheck(&timers_state.qemu_icount_bias,
458 cur_icount - (timers_state.qemu_icount
459 << timers_state.icount_time_shift));
Paolo Bonzini94377112018-08-14 09:57:16 +0200460 seqlock_write_unlock(&timers_state.vm_clock_seqlock,
461 &timers_state.vm_clock_lock);
Paolo Bonzini946fb272011-09-12 13:57:37 +0200462}
463
464static void icount_adjust_rt(void *opaque)
465{
Pavel Dovgalyukb39e3f32018-01-11 11:26:10 +0300466 timer_mod(timers_state.icount_rt_timer,
Pavel Dovgalyuk1979b902015-01-12 15:00:43 +0300467 qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL_RT) + 1000);
Paolo Bonzini946fb272011-09-12 13:57:37 +0200468 icount_adjust();
469}
470
471static void icount_adjust_vm(void *opaque)
472{
Pavel Dovgalyukb39e3f32018-01-11 11:26:10 +0300473 timer_mod(timers_state.icount_vm_timer,
Alex Bligh40daca52013-08-21 16:03:02 +0100474 qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) +
Rutuja Shah73bcb242016-03-21 21:32:30 +0530475 NANOSECONDS_PER_SECOND / 10);
Paolo Bonzini946fb272011-09-12 13:57:37 +0200476 icount_adjust();
477}
478
479static int64_t qemu_icount_round(int64_t count)
480{
Paolo Bonzinic1ff0732018-08-14 09:31:58 +0200481 int shift = atomic_read(&timers_state.icount_time_shift);
482 return (count + (1 << shift) - 1) >> shift;
Paolo Bonzini946fb272011-09-12 13:57:37 +0200483}
484
Pavel Dovgalyukefab87c2015-09-17 19:24:39 +0300485static void icount_warp_rt(void)
Paolo Bonzini946fb272011-09-12 13:57:37 +0200486{
Alex Bennéeccffff42016-04-04 15:35:48 +0100487 unsigned seq;
488 int64_t warp_start;
489
Paolo Bonzini17a15f12013-10-03 15:17:25 +0200490 /* The icount_warp_timer is rescheduled soon after vm_clock_warp_start
491 * changes from -1 to another value, so the race here is okay.
492 */
Alex Bennéeccffff42016-04-04 15:35:48 +0100493 do {
494 seq = seqlock_read_begin(&timers_state.vm_clock_seqlock);
Pavel Dovgalyukb39e3f32018-01-11 11:26:10 +0300495 warp_start = timers_state.vm_clock_warp_start;
Alex Bennéeccffff42016-04-04 15:35:48 +0100496 } while (seqlock_read_retry(&timers_state.vm_clock_seqlock, seq));
497
498 if (warp_start == -1) {
Paolo Bonzini946fb272011-09-12 13:57:37 +0200499 return;
500 }
501
Paolo Bonzini94377112018-08-14 09:57:16 +0200502 seqlock_write_lock(&timers_state.vm_clock_seqlock,
503 &timers_state.vm_clock_lock);
Paolo Bonzini946fb272011-09-12 13:57:37 +0200504 if (runstate_is_running()) {
Pavel Dovgalyuk8eda2062015-09-17 19:24:28 +0300505 int64_t clock = REPLAY_CLOCK(REPLAY_CLOCK_VIRTUAL_RT,
506 cpu_get_clock_locked());
Paolo Bonzini8ed961d2013-10-07 17:26:07 +0200507 int64_t warp_delta;
508
Pavel Dovgalyukb39e3f32018-01-11 11:26:10 +0300509 warp_delta = clock - timers_state.vm_clock_warp_start;
Paolo Bonzini8ed961d2013-10-07 17:26:07 +0200510 if (use_icount == 2) {
Paolo Bonzini946fb272011-09-12 13:57:37 +0200511 /*
Alex Bligh40daca52013-08-21 16:03:02 +0100512 * In adaptive mode, do not let QEMU_CLOCK_VIRTUAL run too
Paolo Bonzini946fb272011-09-12 13:57:37 +0200513 * far ahead of real time.
514 */
Paolo Bonzini17a15f12013-10-03 15:17:25 +0200515 int64_t cur_icount = cpu_get_icount_locked();
Pavel Dovgalyukbf2a7dd2014-11-26 13:40:55 +0300516 int64_t delta = clock - cur_icount;
Paolo Bonzini8ed961d2013-10-07 17:26:07 +0200517 warp_delta = MIN(warp_delta, delta);
Paolo Bonzini946fb272011-09-12 13:57:37 +0200518 }
Paolo Bonzinic1ff0732018-08-14 09:31:58 +0200519 atomic_set__nocheck(&timers_state.qemu_icount_bias,
520 timers_state.qemu_icount_bias + warp_delta);
Paolo Bonzini946fb272011-09-12 13:57:37 +0200521 }
Pavel Dovgalyukb39e3f32018-01-11 11:26:10 +0300522 timers_state.vm_clock_warp_start = -1;
Paolo Bonzini94377112018-08-14 09:57:16 +0200523 seqlock_write_unlock(&timers_state.vm_clock_seqlock,
524 &timers_state.vm_clock_lock);
Paolo Bonzini8ed961d2013-10-07 17:26:07 +0200525
526 if (qemu_clock_expired(QEMU_CLOCK_VIRTUAL)) {
527 qemu_clock_notify(QEMU_CLOCK_VIRTUAL);
528 }
Paolo Bonzini946fb272011-09-12 13:57:37 +0200529}
530
Pavel Dovgalyuke76d1792016-03-10 14:56:09 +0300531static void icount_timer_cb(void *opaque)
Pavel Dovgalyukefab87c2015-09-17 19:24:39 +0300532{
Pavel Dovgalyuke76d1792016-03-10 14:56:09 +0300533 /* No need for a checkpoint because the timer already synchronizes
534 * with CHECKPOINT_CLOCK_VIRTUAL_RT.
535 */
536 icount_warp_rt();
Pavel Dovgalyukefab87c2015-09-17 19:24:39 +0300537}
538
Paolo Bonzini8156be52012-03-28 15:42:04 +0200539void qtest_clock_warp(int64_t dest)
540{
Alex Bligh40daca52013-08-21 16:03:02 +0100541 int64_t clock = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
Fam Zhengefef88b2015-01-19 17:51:43 +0800542 AioContext *aio_context;
Paolo Bonzini8156be52012-03-28 15:42:04 +0200543 assert(qtest_enabled());
Fam Zhengefef88b2015-01-19 17:51:43 +0800544 aio_context = qemu_get_aio_context();
Paolo Bonzini8156be52012-03-28 15:42:04 +0200545 while (clock < dest) {
Alex Bligh40daca52013-08-21 16:03:02 +0100546 int64_t deadline = qemu_clock_deadline_ns_all(QEMU_CLOCK_VIRTUAL);
Sergey Fedorovc9299e22014-06-10 13:10:28 +0400547 int64_t warp = qemu_soonest_timeout(dest - clock, deadline);
Fam Zhengefef88b2015-01-19 17:51:43 +0800548
Paolo Bonzini94377112018-08-14 09:57:16 +0200549 seqlock_write_lock(&timers_state.vm_clock_seqlock,
550 &timers_state.vm_clock_lock);
Paolo Bonzinic1ff0732018-08-14 09:31:58 +0200551 atomic_set__nocheck(&timers_state.qemu_icount_bias,
552 timers_state.qemu_icount_bias + warp);
Paolo Bonzini94377112018-08-14 09:57:16 +0200553 seqlock_write_unlock(&timers_state.vm_clock_seqlock,
554 &timers_state.vm_clock_lock);
Paolo Bonzini17a15f12013-10-03 15:17:25 +0200555
Alex Bligh40daca52013-08-21 16:03:02 +0100556 qemu_clock_run_timers(QEMU_CLOCK_VIRTUAL);
Fam Zhengefef88b2015-01-19 17:51:43 +0800557 timerlist_run_timers(aio_context->tlg.tl[QEMU_CLOCK_VIRTUAL]);
Alex Bligh40daca52013-08-21 16:03:02 +0100558 clock = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
Paolo Bonzini8156be52012-03-28 15:42:04 +0200559 }
Alex Bligh40daca52013-08-21 16:03:02 +0100560 qemu_clock_notify(QEMU_CLOCK_VIRTUAL);
Paolo Bonzini8156be52012-03-28 15:42:04 +0200561}
562
Pavel Dovgalyuke76d1792016-03-10 14:56:09 +0300563void qemu_start_warp_timer(void)
Paolo Bonzini946fb272011-09-12 13:57:37 +0200564{
Paolo Bonzinice78d182013-10-07 17:30:02 +0200565 int64_t clock;
Paolo Bonzini946fb272011-09-12 13:57:37 +0200566 int64_t deadline;
567
Pavel Dovgalyuke76d1792016-03-10 14:56:09 +0300568 if (!use_icount) {
Paolo Bonzini946fb272011-09-12 13:57:37 +0200569 return;
570 }
571
Pavel Dovgalyuk8bd7f712015-09-17 19:24:44 +0300572 /* Nothing to do if the VM is stopped: QEMU_CLOCK_VIRTUAL timers
573 * do not fire, so computing the deadline does not make sense.
574 */
575 if (!runstate_is_running()) {
576 return;
577 }
578
579 /* warp clock deterministically in record/replay mode */
Pavel Dovgalyuke76d1792016-03-10 14:56:09 +0300580 if (!replay_checkpoint(CHECKPOINT_CLOCK_WARP_START)) {
Pavel Dovgalyuk8bd7f712015-09-17 19:24:44 +0300581 return;
582 }
583
Paolo Bonzinice78d182013-10-07 17:30:02 +0200584 if (!all_cpu_threads_idle()) {
Paolo Bonzini946fb272011-09-12 13:57:37 +0200585 return;
586 }
587
Paolo Bonzini8156be52012-03-28 15:42:04 +0200588 if (qtest_enabled()) {
589 /* When testing, qtest commands advance icount. */
Pavel Dovgalyuke76d1792016-03-10 14:56:09 +0300590 return;
Paolo Bonzini8156be52012-03-28 15:42:04 +0200591 }
592
Alex Blighac70aaf2013-08-21 16:02:57 +0100593 /* We want to use the earliest deadline from ALL vm_clocks */
Pavel Dovgalyukbf2a7dd2014-11-26 13:40:55 +0300594 clock = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL_RT);
Alex Bligh40daca52013-08-21 16:03:02 +0100595 deadline = qemu_clock_deadline_ns_all(QEMU_CLOCK_VIRTUAL);
Paolo Bonzinice78d182013-10-07 17:30:02 +0200596 if (deadline < 0) {
Victor CLEMENTd7a0f712015-05-29 17:14:06 +0200597 static bool notified;
598 if (!icount_sleep && !notified) {
Alistair Francis3dc6f862017-07-12 06:57:41 -0700599 warn_report("icount sleep disabled and no active timers");
Victor CLEMENTd7a0f712015-05-29 17:14:06 +0200600 notified = true;
601 }
Paolo Bonzinice78d182013-10-07 17:30:02 +0200602 return;
Alex Blighac70aaf2013-08-21 16:02:57 +0100603 }
604
Paolo Bonzini946fb272011-09-12 13:57:37 +0200605 if (deadline > 0) {
606 /*
Alex Bligh40daca52013-08-21 16:03:02 +0100607 * Ensure QEMU_CLOCK_VIRTUAL proceeds even when the virtual CPU goes to
Paolo Bonzini946fb272011-09-12 13:57:37 +0200608 * sleep. Otherwise, the CPU might be waiting for a future timer
609 * interrupt to wake it up, but the interrupt never comes because
610 * the vCPU isn't running any insns and thus doesn't advance the
Alex Bligh40daca52013-08-21 16:03:02 +0100611 * QEMU_CLOCK_VIRTUAL.
Paolo Bonzini946fb272011-09-12 13:57:37 +0200612 */
Victor CLEMENT5045e9d92015-05-29 17:14:04 +0200613 if (!icount_sleep) {
614 /*
615 * We never let VCPUs sleep in no sleep icount mode.
616 * If there is a pending QEMU_CLOCK_VIRTUAL timer we just advance
617 * to the next QEMU_CLOCK_VIRTUAL event and notify it.
618 * It is useful when we want a deterministic execution time,
619 * isolated from host latencies.
620 */
Paolo Bonzini94377112018-08-14 09:57:16 +0200621 seqlock_write_lock(&timers_state.vm_clock_seqlock,
622 &timers_state.vm_clock_lock);
Paolo Bonzinic1ff0732018-08-14 09:31:58 +0200623 atomic_set__nocheck(&timers_state.qemu_icount_bias,
624 timers_state.qemu_icount_bias + deadline);
Paolo Bonzini94377112018-08-14 09:57:16 +0200625 seqlock_write_unlock(&timers_state.vm_clock_seqlock,
626 &timers_state.vm_clock_lock);
Victor CLEMENT5045e9d92015-05-29 17:14:04 +0200627 qemu_clock_notify(QEMU_CLOCK_VIRTUAL);
628 } else {
629 /*
630 * We do stop VCPUs and only advance QEMU_CLOCK_VIRTUAL after some
631 * "real" time, (related to the time left until the next event) has
632 * passed. The QEMU_CLOCK_VIRTUAL_RT clock will do this.
633 * This avoids that the warps are visible externally; for example,
634 * you will not be sending network packets continuously instead of
635 * every 100ms.
636 */
Paolo Bonzini94377112018-08-14 09:57:16 +0200637 seqlock_write_lock(&timers_state.vm_clock_seqlock,
638 &timers_state.vm_clock_lock);
Pavel Dovgalyukb39e3f32018-01-11 11:26:10 +0300639 if (timers_state.vm_clock_warp_start == -1
640 || timers_state.vm_clock_warp_start > clock) {
641 timers_state.vm_clock_warp_start = clock;
Victor CLEMENT5045e9d92015-05-29 17:14:04 +0200642 }
Paolo Bonzini94377112018-08-14 09:57:16 +0200643 seqlock_write_unlock(&timers_state.vm_clock_seqlock,
644 &timers_state.vm_clock_lock);
Pavel Dovgalyukb39e3f32018-01-11 11:26:10 +0300645 timer_mod_anticipate(timers_state.icount_warp_timer,
646 clock + deadline);
Paolo Bonzinice78d182013-10-07 17:30:02 +0200647 }
Alex Blighac70aaf2013-08-21 16:02:57 +0100648 } else if (deadline == 0) {
Alex Bligh40daca52013-08-21 16:03:02 +0100649 qemu_clock_notify(QEMU_CLOCK_VIRTUAL);
Paolo Bonzini946fb272011-09-12 13:57:37 +0200650 }
651}
652
Pavel Dovgalyuke76d1792016-03-10 14:56:09 +0300653static void qemu_account_warp_timer(void)
654{
655 if (!use_icount || !icount_sleep) {
656 return;
657 }
658
659 /* Nothing to do if the VM is stopped: QEMU_CLOCK_VIRTUAL timers
660 * do not fire, so computing the deadline does not make sense.
661 */
662 if (!runstate_is_running()) {
663 return;
664 }
665
666 /* warp clock deterministically in record/replay mode */
667 if (!replay_checkpoint(CHECKPOINT_CLOCK_WARP_ACCOUNT)) {
668 return;
669 }
670
Pavel Dovgalyukb39e3f32018-01-11 11:26:10 +0300671 timer_del(timers_state.icount_warp_timer);
Pavel Dovgalyuke76d1792016-03-10 14:56:09 +0300672 icount_warp_rt();
673}
674
KONRAD Fredericd09eae32014-08-01 01:37:10 +0200675static bool icount_state_needed(void *opaque)
676{
677 return use_icount;
678}
679
Pavel Dovgalyukb39e3f32018-01-11 11:26:10 +0300680static bool warp_timer_state_needed(void *opaque)
681{
682 TimersState *s = opaque;
683 return s->icount_warp_timer != NULL;
684}
685
686static bool adjust_timers_state_needed(void *opaque)
687{
688 TimersState *s = opaque;
689 return s->icount_rt_timer != NULL;
690}
691
692/*
693 * Subsection for warp timer migration is optional, because may not be created
694 */
695static const VMStateDescription icount_vmstate_warp_timer = {
696 .name = "timer/icount/warp_timer",
697 .version_id = 1,
698 .minimum_version_id = 1,
699 .needed = warp_timer_state_needed,
700 .fields = (VMStateField[]) {
701 VMSTATE_INT64(vm_clock_warp_start, TimersState),
702 VMSTATE_TIMER_PTR(icount_warp_timer, TimersState),
703 VMSTATE_END_OF_LIST()
704 }
705};
706
707static const VMStateDescription icount_vmstate_adjust_timers = {
708 .name = "timer/icount/timers",
709 .version_id = 1,
710 .minimum_version_id = 1,
711 .needed = adjust_timers_state_needed,
712 .fields = (VMStateField[]) {
713 VMSTATE_TIMER_PTR(icount_rt_timer, TimersState),
714 VMSTATE_TIMER_PTR(icount_vm_timer, TimersState),
715 VMSTATE_END_OF_LIST()
716 }
717};
718
KONRAD Fredericd09eae32014-08-01 01:37:10 +0200719/*
720 * This is a subsection for icount migration.
721 */
722static const VMStateDescription icount_vmstate_timers = {
723 .name = "timer/icount",
724 .version_id = 1,
725 .minimum_version_id = 1,
Juan Quintela5cd8cad2014-09-23 14:09:54 +0200726 .needed = icount_state_needed,
KONRAD Fredericd09eae32014-08-01 01:37:10 +0200727 .fields = (VMStateField[]) {
728 VMSTATE_INT64(qemu_icount_bias, TimersState),
729 VMSTATE_INT64(qemu_icount, TimersState),
730 VMSTATE_END_OF_LIST()
Pavel Dovgalyukb39e3f32018-01-11 11:26:10 +0300731 },
732 .subsections = (const VMStateDescription*[]) {
733 &icount_vmstate_warp_timer,
734 &icount_vmstate_adjust_timers,
735 NULL
KONRAD Fredericd09eae32014-08-01 01:37:10 +0200736 }
737};
738
Paolo Bonzini946fb272011-09-12 13:57:37 +0200739static const VMStateDescription vmstate_timers = {
740 .name = "timer",
741 .version_id = 2,
742 .minimum_version_id = 1,
Juan Quintela35d08452014-04-16 16:01:33 +0200743 .fields = (VMStateField[]) {
Paolo Bonzini946fb272011-09-12 13:57:37 +0200744 VMSTATE_INT64(cpu_ticks_offset, TimersState),
Paolo Bonzinic1ff0732018-08-14 09:31:58 +0200745 VMSTATE_UNUSED(8),
Paolo Bonzini946fb272011-09-12 13:57:37 +0200746 VMSTATE_INT64_V(cpu_clock_offset, TimersState, 2),
747 VMSTATE_END_OF_LIST()
KONRAD Fredericd09eae32014-08-01 01:37:10 +0200748 },
Juan Quintela5cd8cad2014-09-23 14:09:54 +0200749 .subsections = (const VMStateDescription*[]) {
750 &icount_vmstate_timers,
751 NULL
Paolo Bonzini946fb272011-09-12 13:57:37 +0200752 }
753};
754
Paolo Bonzini14e6fe12016-10-31 10:36:08 +0100755static void cpu_throttle_thread(CPUState *cpu, run_on_cpu_data opaque)
Jason J. Herne2adcc852015-09-08 13:12:33 -0400756{
Jason J. Herne2adcc852015-09-08 13:12:33 -0400757 double pct;
758 double throttle_ratio;
759 long sleeptime_ns;
760
761 if (!cpu_throttle_get_percentage()) {
762 return;
763 }
764
765 pct = (double)cpu_throttle_get_percentage()/100;
766 throttle_ratio = pct / (1 - pct);
767 sleeptime_ns = (long)(throttle_ratio * CPU_THROTTLE_TIMESLICE_NS);
768
769 qemu_mutex_unlock_iothread();
Jason J. Herne2adcc852015-09-08 13:12:33 -0400770 g_usleep(sleeptime_ns / 1000); /* Convert ns to us for usleep call */
771 qemu_mutex_lock_iothread();
Felipe Franciosi90bb0c02017-05-19 22:29:50 +0100772 atomic_set(&cpu->throttle_thread_scheduled, 0);
Jason J. Herne2adcc852015-09-08 13:12:33 -0400773}
774
775static void cpu_throttle_timer_tick(void *opaque)
776{
777 CPUState *cpu;
778 double pct;
779
780 /* Stop the timer if needed */
781 if (!cpu_throttle_get_percentage()) {
782 return;
783 }
784 CPU_FOREACH(cpu) {
785 if (!atomic_xchg(&cpu->throttle_thread_scheduled, 1)) {
Paolo Bonzini14e6fe12016-10-31 10:36:08 +0100786 async_run_on_cpu(cpu, cpu_throttle_thread,
787 RUN_ON_CPU_NULL);
Jason J. Herne2adcc852015-09-08 13:12:33 -0400788 }
789 }
790
791 pct = (double)cpu_throttle_get_percentage()/100;
792 timer_mod(throttle_timer, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL_RT) +
793 CPU_THROTTLE_TIMESLICE_NS / (1-pct));
794}
795
796void cpu_throttle_set(int new_throttle_pct)
797{
798 /* Ensure throttle percentage is within valid range */
799 new_throttle_pct = MIN(new_throttle_pct, CPU_THROTTLE_PCT_MAX);
800 new_throttle_pct = MAX(new_throttle_pct, CPU_THROTTLE_PCT_MIN);
801
802 atomic_set(&throttle_percentage, new_throttle_pct);
803
804 timer_mod(throttle_timer, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL_RT) +
805 CPU_THROTTLE_TIMESLICE_NS);
806}
807
808void cpu_throttle_stop(void)
809{
810 atomic_set(&throttle_percentage, 0);
811}
812
813bool cpu_throttle_active(void)
814{
815 return (cpu_throttle_get_percentage() != 0);
816}
817
818int cpu_throttle_get_percentage(void)
819{
820 return atomic_read(&throttle_percentage);
821}
822
Pavel Dovgalyuk4603ea02014-09-01 09:34:49 +0400823void cpu_ticks_init(void)
824{
Emilio G. Cotaccdb3c12016-06-08 14:55:20 -0400825 seqlock_init(&timers_state.vm_clock_seqlock);
Emilio G. Cota87a09cd2018-09-03 13:18:29 -0400826 qemu_spin_init(&timers_state.vm_clock_lock);
Pavel Dovgalyuk4603ea02014-09-01 09:34:49 +0400827 vmstate_register(NULL, 0, &vmstate_timers, &timers_state);
Jason J. Herne2adcc852015-09-08 13:12:33 -0400828 throttle_timer = timer_new_ns(QEMU_CLOCK_VIRTUAL_RT,
829 cpu_throttle_timer_tick, NULL);
Pavel Dovgalyuk4603ea02014-09-01 09:34:49 +0400830}
831
Sebastian Tanase1ad95802014-07-25 11:56:28 +0200832void configure_icount(QemuOpts *opts, Error **errp)
Paolo Bonzini946fb272011-09-12 13:57:37 +0200833{
Sebastian Tanase1ad95802014-07-25 11:56:28 +0200834 const char *option;
Sebastian Tanasea8bfac32014-07-25 11:56:29 +0200835 char *rem_str = NULL;
Sebastian Tanase1ad95802014-07-25 11:56:28 +0200836
Sebastian Tanase1ad95802014-07-25 11:56:28 +0200837 option = qemu_opt_get(opts, "shift");
Paolo Bonzini946fb272011-09-12 13:57:37 +0200838 if (!option) {
Sebastian Tanasea8bfac32014-07-25 11:56:29 +0200839 if (qemu_opt_get(opts, "align") != NULL) {
840 error_setg(errp, "Please specify shift option when using align");
841 }
Paolo Bonzini946fb272011-09-12 13:57:37 +0200842 return;
843 }
Victor CLEMENTf1f4b572015-05-29 17:14:05 +0200844
845 icount_sleep = qemu_opt_get_bool(opts, "sleep", true);
Victor CLEMENT5045e9d92015-05-29 17:14:04 +0200846 if (icount_sleep) {
Pavel Dovgalyukb39e3f32018-01-11 11:26:10 +0300847 timers_state.icount_warp_timer = timer_new_ns(QEMU_CLOCK_VIRTUAL_RT,
Pavel Dovgalyuke76d1792016-03-10 14:56:09 +0300848 icount_timer_cb, NULL);
Victor CLEMENT5045e9d92015-05-29 17:14:04 +0200849 }
Victor CLEMENTf1f4b572015-05-29 17:14:05 +0200850
Sebastian Tanasea8bfac32014-07-25 11:56:29 +0200851 icount_align_option = qemu_opt_get_bool(opts, "align", false);
Victor CLEMENTf1f4b572015-05-29 17:14:05 +0200852
853 if (icount_align_option && !icount_sleep) {
Pranith Kumar778d9f92016-02-26 10:16:51 -0500854 error_setg(errp, "align=on and sleep=off are incompatible");
Victor CLEMENTf1f4b572015-05-29 17:14:05 +0200855 }
Paolo Bonzini946fb272011-09-12 13:57:37 +0200856 if (strcmp(option, "auto") != 0) {
Sebastian Tanasea8bfac32014-07-25 11:56:29 +0200857 errno = 0;
Paolo Bonzinic1ff0732018-08-14 09:31:58 +0200858 timers_state.icount_time_shift = strtol(option, &rem_str, 0);
Sebastian Tanasea8bfac32014-07-25 11:56:29 +0200859 if (errno != 0 || *rem_str != '\0' || !strlen(option)) {
860 error_setg(errp, "icount: Invalid shift value");
861 }
Paolo Bonzini946fb272011-09-12 13:57:37 +0200862 use_icount = 1;
863 return;
Sebastian Tanasea8bfac32014-07-25 11:56:29 +0200864 } else if (icount_align_option) {
865 error_setg(errp, "shift=auto and align=on are incompatible");
Victor CLEMENTf1f4b572015-05-29 17:14:05 +0200866 } else if (!icount_sleep) {
Pranith Kumar778d9f92016-02-26 10:16:51 -0500867 error_setg(errp, "shift=auto and sleep=off are incompatible");
Paolo Bonzini946fb272011-09-12 13:57:37 +0200868 }
869
870 use_icount = 2;
871
872 /* 125MIPS seems a reasonable initial guess at the guest speed.
873 It will be corrected fairly quickly anyway. */
Paolo Bonzinic1ff0732018-08-14 09:31:58 +0200874 timers_state.icount_time_shift = 3;
Paolo Bonzini946fb272011-09-12 13:57:37 +0200875
876 /* Have both realtime and virtual time triggers for speed adjustment.
877 The realtime trigger catches emulated time passing too slowly,
878 the virtual time trigger catches emulated time passing too fast.
879 Realtime triggers occur even when idle, so use them less frequently
880 than VM triggers. */
Pavel Dovgalyukb39e3f32018-01-11 11:26:10 +0300881 timers_state.vm_clock_warp_start = -1;
882 timers_state.icount_rt_timer = timer_new_ms(QEMU_CLOCK_VIRTUAL_RT,
Pavel Dovgalyukbf2a7dd2014-11-26 13:40:55 +0300883 icount_adjust_rt, NULL);
Pavel Dovgalyukb39e3f32018-01-11 11:26:10 +0300884 timer_mod(timers_state.icount_rt_timer,
Pavel Dovgalyukbf2a7dd2014-11-26 13:40:55 +0300885 qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL_RT) + 1000);
Pavel Dovgalyukb39e3f32018-01-11 11:26:10 +0300886 timers_state.icount_vm_timer = timer_new_ns(QEMU_CLOCK_VIRTUAL,
Alex Bligh40daca52013-08-21 16:03:02 +0100887 icount_adjust_vm, NULL);
Pavel Dovgalyukb39e3f32018-01-11 11:26:10 +0300888 timer_mod(timers_state.icount_vm_timer,
Alex Bligh40daca52013-08-21 16:03:02 +0100889 qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) +
Rutuja Shah73bcb242016-03-21 21:32:30 +0530890 NANOSECONDS_PER_SECOND / 10);
Paolo Bonzini946fb272011-09-12 13:57:37 +0200891}
892
893/***********************************************************/
Alex Bennée65467062017-02-23 18:29:09 +0000894/* TCG vCPU kick timer
895 *
896 * The kick timer is responsible for moving single threaded vCPU
897 * emulation on to the next vCPU. If more than one vCPU is running a
898 * timer event with force a cpu->exit so the next vCPU can get
899 * scheduled.
900 *
901 * The timer is removed if all vCPUs are idle and restarted again once
902 * idleness is complete.
903 */
904
905static QEMUTimer *tcg_kick_vcpu_timer;
Alex Bennée791158d2017-02-23 18:29:10 +0000906static CPUState *tcg_current_rr_cpu;
Alex Bennée65467062017-02-23 18:29:09 +0000907
908#define TCG_KICK_PERIOD (NANOSECONDS_PER_SECOND / 10)
909
910static inline int64_t qemu_tcg_next_kick(void)
911{
912 return qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) + TCG_KICK_PERIOD;
913}
914
Alex Bennée791158d2017-02-23 18:29:10 +0000915/* Kick the currently round-robin scheduled vCPU */
916static void qemu_cpu_kick_rr_cpu(void)
917{
918 CPUState *cpu;
Alex Bennée791158d2017-02-23 18:29:10 +0000919 do {
920 cpu = atomic_mb_read(&tcg_current_rr_cpu);
921 if (cpu) {
922 cpu_exit(cpu);
923 }
924 } while (cpu != atomic_mb_read(&tcg_current_rr_cpu));
925}
926
Paolo Bonzini6b8f0182017-03-02 19:56:40 +0100927static void do_nothing(CPUState *cpu, run_on_cpu_data unused)
928{
929}
930
Paolo Bonzini3f53bc62017-03-03 11:50:29 +0100931void qemu_timer_notify_cb(void *opaque, QEMUClockType type)
932{
Paolo Bonzini6b8f0182017-03-02 19:56:40 +0100933 if (!use_icount || type != QEMU_CLOCK_VIRTUAL) {
934 qemu_notify_event();
935 return;
936 }
937
Peter Maydellc52e7132018-04-10 13:02:25 +0100938 if (qemu_in_vcpu_thread()) {
939 /* A CPU is currently running; kick it back out to the
940 * tcg_cpu_exec() loop so it will recalculate its
941 * icount deadline immediately.
942 */
943 qemu_cpu_kick(current_cpu);
944 } else if (first_cpu) {
Paolo Bonzini6b8f0182017-03-02 19:56:40 +0100945 /* qemu_cpu_kick is not enough to kick a halted CPU out of
946 * qemu_tcg_wait_io_event. async_run_on_cpu, instead,
947 * causes cpu_thread_is_idle to return false. This way,
948 * handle_icount_deadline can run.
Peter Maydellc52e7132018-04-10 13:02:25 +0100949 * If we have no CPUs at all for some reason, we don't
950 * need to do anything.
Paolo Bonzini6b8f0182017-03-02 19:56:40 +0100951 */
952 async_run_on_cpu(first_cpu, do_nothing, RUN_ON_CPU_NULL);
953 }
Paolo Bonzini3f53bc62017-03-03 11:50:29 +0100954}
955
Alex Bennée65467062017-02-23 18:29:09 +0000956static void kick_tcg_thread(void *opaque)
957{
958 timer_mod(tcg_kick_vcpu_timer, qemu_tcg_next_kick());
Alex Bennée791158d2017-02-23 18:29:10 +0000959 qemu_cpu_kick_rr_cpu();
Alex Bennée65467062017-02-23 18:29:09 +0000960}
961
962static void start_tcg_kick_timer(void)
963{
Paolo Bonzinidb08b682018-01-11 13:53:12 +0100964 assert(!mttcg_enabled);
965 if (!tcg_kick_vcpu_timer && CPU_NEXT(first_cpu)) {
Alex Bennée65467062017-02-23 18:29:09 +0000966 tcg_kick_vcpu_timer = timer_new_ns(QEMU_CLOCK_VIRTUAL,
967 kick_tcg_thread, NULL);
968 timer_mod(tcg_kick_vcpu_timer, qemu_tcg_next_kick());
969 }
970}
971
972static void stop_tcg_kick_timer(void)
973{
Paolo Bonzinidb08b682018-01-11 13:53:12 +0100974 assert(!mttcg_enabled);
Alex Bennée65467062017-02-23 18:29:09 +0000975 if (tcg_kick_vcpu_timer) {
976 timer_del(tcg_kick_vcpu_timer);
977 tcg_kick_vcpu_timer = NULL;
978 }
979}
980
Alex Bennée65467062017-02-23 18:29:09 +0000981/***********************************************************/
Blue Swirl296af7c2010-03-29 19:23:50 +0000982void hw_error(const char *fmt, ...)
983{
984 va_list ap;
Andreas Färber55e5c282012-12-17 06:18:02 +0100985 CPUState *cpu;
Blue Swirl296af7c2010-03-29 19:23:50 +0000986
987 va_start(ap, fmt);
988 fprintf(stderr, "qemu: hardware error: ");
989 vfprintf(stderr, fmt, ap);
990 fprintf(stderr, "\n");
Andreas Färberbdc44642013-06-24 23:50:24 +0200991 CPU_FOREACH(cpu) {
Andreas Färber55e5c282012-12-17 06:18:02 +0100992 fprintf(stderr, "CPU #%d:\n", cpu->cpu_index);
Andreas Färber878096e2013-05-27 01:33:50 +0200993 cpu_dump_state(cpu, stderr, fprintf, CPU_DUMP_FPU);
Blue Swirl296af7c2010-03-29 19:23:50 +0000994 }
995 va_end(ap);
996 abort();
997}
998
999void cpu_synchronize_all_states(void)
1000{
Andreas Färber182735e2013-05-29 22:29:20 +02001001 CPUState *cpu;
Blue Swirl296af7c2010-03-29 19:23:50 +00001002
Andreas Färberbdc44642013-06-24 23:50:24 +02001003 CPU_FOREACH(cpu) {
Andreas Färber182735e2013-05-29 22:29:20 +02001004 cpu_synchronize_state(cpu);
Sergio Andres Gomez Del Realc97d6d22017-09-13 04:05:09 -05001005 /* TODO: move to cpu_synchronize_state() */
1006 if (hvf_enabled()) {
1007 hvf_cpu_synchronize_state(cpu);
1008 }
Blue Swirl296af7c2010-03-29 19:23:50 +00001009 }
1010}
1011
1012void cpu_synchronize_all_post_reset(void)
1013{
Andreas Färber182735e2013-05-29 22:29:20 +02001014 CPUState *cpu;
Blue Swirl296af7c2010-03-29 19:23:50 +00001015
Andreas Färberbdc44642013-06-24 23:50:24 +02001016 CPU_FOREACH(cpu) {
Andreas Färber182735e2013-05-29 22:29:20 +02001017 cpu_synchronize_post_reset(cpu);
Sergio Andres Gomez Del Realc97d6d22017-09-13 04:05:09 -05001018 /* TODO: move to cpu_synchronize_post_reset() */
1019 if (hvf_enabled()) {
1020 hvf_cpu_synchronize_post_reset(cpu);
1021 }
Blue Swirl296af7c2010-03-29 19:23:50 +00001022 }
1023}
1024
1025void cpu_synchronize_all_post_init(void)
1026{
Andreas Färber182735e2013-05-29 22:29:20 +02001027 CPUState *cpu;
Blue Swirl296af7c2010-03-29 19:23:50 +00001028
Andreas Färberbdc44642013-06-24 23:50:24 +02001029 CPU_FOREACH(cpu) {
Andreas Färber182735e2013-05-29 22:29:20 +02001030 cpu_synchronize_post_init(cpu);
Sergio Andres Gomez Del Realc97d6d22017-09-13 04:05:09 -05001031 /* TODO: move to cpu_synchronize_post_init() */
1032 if (hvf_enabled()) {
1033 hvf_cpu_synchronize_post_init(cpu);
1034 }
Blue Swirl296af7c2010-03-29 19:23:50 +00001035 }
1036}
1037
David Gibson75e972d2017-05-26 14:46:28 +10001038void cpu_synchronize_all_pre_loadvm(void)
1039{
1040 CPUState *cpu;
1041
1042 CPU_FOREACH(cpu) {
1043 cpu_synchronize_pre_loadvm(cpu);
1044 }
1045}
1046
Stefan Hajnoczi4486e892018-03-07 14:42:05 +00001047static int do_vm_stop(RunState state, bool send_stop)
Blue Swirl296af7c2010-03-29 19:23:50 +00001048{
Kevin Wolf56983462013-07-05 13:49:54 +02001049 int ret = 0;
1050
Luiz Capitulino13548692011-07-29 15:36:43 -03001051 if (runstate_is_running()) {
Blue Swirl296af7c2010-03-29 19:23:50 +00001052 cpu_disable_ticks();
Blue Swirl296af7c2010-03-29 19:23:50 +00001053 pause_all_vcpus();
Luiz Capitulinof5bbfba2011-07-29 15:04:45 -03001054 runstate_set(state);
Luiz Capitulino1dfb4dd2011-07-29 14:26:33 -03001055 vm_state_notify(0, state);
Stefan Hajnoczi4486e892018-03-07 14:42:05 +00001056 if (send_stop) {
Peter Xu3ab72382018-08-15 21:37:37 +08001057 qapi_event_send_stop();
Stefan Hajnoczi4486e892018-03-07 14:42:05 +00001058 }
Blue Swirl296af7c2010-03-29 19:23:50 +00001059 }
Kevin Wolf56983462013-07-05 13:49:54 +02001060
Kevin Wolf594a45c2013-07-18 14:52:19 +02001061 bdrv_drain_all();
Pavel Dovgalyuk6d0ceb82016-09-26 11:08:16 +03001062 replay_disable_events();
John Snow22af08e2016-09-22 21:45:51 -04001063 ret = bdrv_flush_all();
Kevin Wolf594a45c2013-07-18 14:52:19 +02001064
Kevin Wolf56983462013-07-05 13:49:54 +02001065 return ret;
Blue Swirl296af7c2010-03-29 19:23:50 +00001066}
1067
Stefan Hajnoczi4486e892018-03-07 14:42:05 +00001068/* Special vm_stop() variant for terminating the process. Historically clients
1069 * did not expect a QMP STOP event and so we need to retain compatibility.
1070 */
1071int vm_shutdown(void)
1072{
1073 return do_vm_stop(RUN_STATE_SHUTDOWN, false);
1074}
1075
Andreas Färbera1fcaa72012-05-02 23:42:26 +02001076static bool cpu_can_run(CPUState *cpu)
Blue Swirl296af7c2010-03-29 19:23:50 +00001077{
Andreas Färber4fdeee72012-05-02 23:10:09 +02001078 if (cpu->stop) {
Andreas Färbera1fcaa72012-05-02 23:42:26 +02001079 return false;
Jan Kiszka0ab07c62011-02-07 12:19:14 +01001080 }
Tiejun Chen321bc0b2013-08-02 09:43:09 +08001081 if (cpu_is_stopped(cpu)) {
Andreas Färbera1fcaa72012-05-02 23:42:26 +02001082 return false;
Jan Kiszka0ab07c62011-02-07 12:19:14 +01001083 }
Andreas Färbera1fcaa72012-05-02 23:42:26 +02001084 return true;
Blue Swirl296af7c2010-03-29 19:23:50 +00001085}
1086
Andreas Färber91325042013-05-27 02:07:49 +02001087static void cpu_handle_guest_debug(CPUState *cpu)
Jan Kiszka3c638d02010-06-25 16:56:56 +02001088{
Andreas Färber64f6b342013-05-27 02:06:09 +02001089 gdb_set_stop_cpu(cpu);
Jan Kiszka8cf71712011-02-07 12:19:16 +01001090 qemu_system_debug_request();
Andreas Färberf324e762012-05-02 23:26:21 +02001091 cpu->stopped = true;
Jan Kiszka3c638d02010-06-25 16:56:56 +02001092}
1093
Jan Kiszka6d9cb732011-02-01 22:15:58 +01001094#ifdef CONFIG_LINUX
1095static void sigbus_reraise(void)
1096{
1097 sigset_t set;
1098 struct sigaction action;
1099
1100 memset(&action, 0, sizeof(action));
1101 action.sa_handler = SIG_DFL;
1102 if (!sigaction(SIGBUS, &action, NULL)) {
1103 raise(SIGBUS);
1104 sigemptyset(&set);
1105 sigaddset(&set, SIGBUS);
Peter Maydella2d17612016-05-16 18:33:59 +01001106 pthread_sigmask(SIG_UNBLOCK, &set, NULL);
Jan Kiszka6d9cb732011-02-01 22:15:58 +01001107 }
1108 perror("Failed to re-raise SIGBUS!\n");
1109 abort();
1110}
1111
Paolo Bonzinid98d4072017-02-08 13:22:12 +01001112static void sigbus_handler(int n, siginfo_t *siginfo, void *ctx)
Jan Kiszka6d9cb732011-02-01 22:15:58 +01001113{
Paolo Bonzinia16fc072017-02-09 09:50:02 +01001114 if (siginfo->si_code != BUS_MCEERR_AO && siginfo->si_code != BUS_MCEERR_AR) {
1115 sigbus_reraise();
1116 }
1117
Paolo Bonzini2ae41db2017-02-08 12:48:54 +01001118 if (current_cpu) {
1119 /* Called asynchronously in VCPU thread. */
1120 if (kvm_on_sigbus_vcpu(current_cpu, siginfo->si_code, siginfo->si_addr)) {
1121 sigbus_reraise();
1122 }
1123 } else {
1124 /* Called synchronously (via signalfd) in main thread. */
1125 if (kvm_on_sigbus(siginfo->si_code, siginfo->si_addr)) {
1126 sigbus_reraise();
1127 }
Jan Kiszka6d9cb732011-02-01 22:15:58 +01001128 }
1129}
1130
1131static void qemu_init_sigbus(void)
1132{
1133 struct sigaction action;
1134
1135 memset(&action, 0, sizeof(action));
1136 action.sa_flags = SA_SIGINFO;
Paolo Bonzinid98d4072017-02-08 13:22:12 +01001137 action.sa_sigaction = sigbus_handler;
Jan Kiszka6d9cb732011-02-01 22:15:58 +01001138 sigaction(SIGBUS, &action, NULL);
1139
1140 prctl(PR_MCE_KILL, PR_MCE_KILL_SET, PR_MCE_KILL_EARLY, 0, 0);
1141}
Paolo Bonzinia16fc072017-02-09 09:50:02 +01001142#else /* !CONFIG_LINUX */
1143static void qemu_init_sigbus(void)
1144{
1145}
Paolo Bonzinia16fc072017-02-09 09:50:02 +01001146#endif /* !CONFIG_LINUX */
Blue Swirl296af7c2010-03-29 19:23:50 +00001147
Stefan Weilb2532d82012-09-27 07:41:42 +02001148static QemuMutex qemu_global_mutex;
Blue Swirl296af7c2010-03-29 19:23:50 +00001149
1150static QemuThread io_thread;
1151
Blue Swirl296af7c2010-03-29 19:23:50 +00001152/* cpu creation */
1153static QemuCond qemu_cpu_cond;
1154/* system init */
Blue Swirl296af7c2010-03-29 19:23:50 +00001155static QemuCond qemu_pause_cond;
1156
Paolo Bonzinid3b12f52011-09-13 10:30:52 +02001157void qemu_init_cpu_loop(void)
Blue Swirl296af7c2010-03-29 19:23:50 +00001158{
Jan Kiszka6d9cb732011-02-01 22:15:58 +01001159 qemu_init_sigbus();
Anthony Liguoried945922011-02-08 18:18:18 +01001160 qemu_cond_init(&qemu_cpu_cond);
Anthony Liguoried945922011-02-08 18:18:18 +01001161 qemu_cond_init(&qemu_pause_cond);
Blue Swirl296af7c2010-03-29 19:23:50 +00001162 qemu_mutex_init(&qemu_global_mutex);
Blue Swirl296af7c2010-03-29 19:23:50 +00001163
Jan Kiszkab7680cb2011-03-12 17:43:51 +01001164 qemu_thread_get_self(&io_thread);
Blue Swirl296af7c2010-03-29 19:23:50 +00001165}
1166
Paolo Bonzini14e6fe12016-10-31 10:36:08 +01001167void run_on_cpu(CPUState *cpu, run_on_cpu_func func, run_on_cpu_data data)
Marcelo Tosattie82bcec2010-05-04 09:45:22 -03001168{
Sergey Fedorovd148d902016-08-29 09:51:00 +02001169 do_run_on_cpu(cpu, func, data, &qemu_global_mutex);
Chegu Vinod3c022702013-06-24 03:49:41 -06001170}
1171
Gu Zheng4c055ab2016-05-12 09:18:13 +05301172static void qemu_kvm_destroy_vcpu(CPUState *cpu)
1173{
1174 if (kvm_destroy_vcpu(cpu) < 0) {
1175 error_report("kvm_destroy_vcpu failed");
1176 exit(EXIT_FAILURE);
1177 }
1178}
1179
1180static void qemu_tcg_destroy_vcpu(CPUState *cpu)
1181{
1182}
1183
David Hildenbrandebd05fe2017-11-29 20:12:15 +01001184static void qemu_cpu_stop(CPUState *cpu, bool exit)
1185{
1186 g_assert(qemu_cpu_is_self(cpu));
1187 cpu->stop = false;
1188 cpu->stopped = true;
1189 if (exit) {
1190 cpu_exit(cpu);
1191 }
1192 qemu_cond_broadcast(&qemu_pause_cond);
1193}
1194
Andreas Färber509a0d72012-05-03 02:18:09 +02001195static void qemu_wait_io_event_common(CPUState *cpu)
Blue Swirl296af7c2010-03-29 19:23:50 +00001196{
Alex Bennée37257942017-02-23 18:29:14 +00001197 atomic_mb_set(&cpu->thread_kicked, false);
Andreas Färber4fdeee72012-05-02 23:10:09 +02001198 if (cpu->stop) {
David Hildenbrandebd05fe2017-11-29 20:12:15 +01001199 qemu_cpu_stop(cpu, false);
Blue Swirl296af7c2010-03-29 19:23:50 +00001200 }
Sergey Fedorova5403c62016-08-02 18:27:36 +01001201 process_queued_cpu_work(cpu);
Alex Bennée37257942017-02-23 18:29:14 +00001202}
1203
Paolo Bonzinidb08b682018-01-11 13:53:12 +01001204static void qemu_tcg_rr_wait_io_event(CPUState *cpu)
Alex Bennée37257942017-02-23 18:29:14 +00001205{
Paolo Bonzinidb08b682018-01-11 13:53:12 +01001206 while (all_cpu_threads_idle()) {
Alex Bennée65467062017-02-23 18:29:09 +00001207 stop_tcg_kick_timer();
KONRAD Fredericd5f8d612015-08-10 17:27:06 +02001208 qemu_cond_wait(cpu->halt_cond, &qemu_global_mutex);
Jan Kiszka16400322011-02-09 16:29:37 +01001209 }
Blue Swirl296af7c2010-03-29 19:23:50 +00001210
Alex Bennée65467062017-02-23 18:29:09 +00001211 start_tcg_kick_timer();
1212
Alex Bennée37257942017-02-23 18:29:14 +00001213 qemu_wait_io_event_common(cpu);
Blue Swirl296af7c2010-03-29 19:23:50 +00001214}
1215
Paolo Bonzinidb08b682018-01-11 13:53:12 +01001216static void qemu_wait_io_event(CPUState *cpu)
Blue Swirl296af7c2010-03-29 19:23:50 +00001217{
Andreas Färbera98ae1d2013-05-26 23:21:08 +02001218 while (cpu_thread_is_idle(cpu)) {
Andreas Färberf5c121b2012-05-03 01:22:49 +02001219 qemu_cond_wait(cpu->halt_cond, &qemu_global_mutex);
Jan Kiszka16400322011-02-09 16:29:37 +01001220 }
Blue Swirl296af7c2010-03-29 19:23:50 +00001221
Paolo Bonzinidb08b682018-01-11 13:53:12 +01001222#ifdef _WIN32
1223 /* Eat dummy APC queued by qemu_cpu_kick_thread. */
1224 if (!tcg_enabled()) {
1225 SleepEx(0, TRUE);
Sergio Andres Gomez Del Realc97d6d22017-09-13 04:05:09 -05001226 }
Paolo Bonzinidb08b682018-01-11 13:53:12 +01001227#endif
Sergio Andres Gomez Del Realc97d6d22017-09-13 04:05:09 -05001228 qemu_wait_io_event_common(cpu);
1229}
1230
Jan Kiszka7e97cd82011-02-07 12:19:12 +01001231static void *qemu_kvm_cpu_thread_fn(void *arg)
Blue Swirl296af7c2010-03-29 19:23:50 +00001232{
Andreas Färber48a106b2013-05-27 02:20:39 +02001233 CPUState *cpu = arg;
Jan Kiszka84b49152011-02-01 22:15:50 +01001234 int r;
Blue Swirl296af7c2010-03-29 19:23:50 +00001235
Paolo Bonziniab28bd22015-07-09 08:55:38 +02001236 rcu_register_thread();
1237
Paolo Bonzini2e7f7a32015-06-18 18:47:18 +02001238 qemu_mutex_lock_iothread();
Andreas Färber814e6122012-05-02 17:00:37 +02001239 qemu_thread_get_self(cpu->thread);
Andreas Färber9f09e182012-05-03 06:59:07 +02001240 cpu->thread_id = qemu_get_thread_id();
Pavel Dovgalyuk626cf8f2014-12-08 10:53:17 +03001241 cpu->can_do_io = 1;
Andreas Färber4917cf42013-05-27 05:17:50 +02001242 current_cpu = cpu;
Blue Swirl296af7c2010-03-29 19:23:50 +00001243
Andreas Färber504134d2012-12-17 06:38:45 +01001244 r = kvm_init_vcpu(cpu);
Jan Kiszka84b49152011-02-01 22:15:50 +01001245 if (r < 0) {
Alistair Francis493d89b2018-02-03 09:43:14 +01001246 error_report("kvm_init_vcpu failed: %s", strerror(-r));
Jan Kiszka84b49152011-02-01 22:15:50 +01001247 exit(1);
1248 }
Blue Swirl296af7c2010-03-29 19:23:50 +00001249
Paolo Bonzini18268b62017-02-09 09:41:14 +01001250 kvm_init_cpu_signals(cpu);
Blue Swirl296af7c2010-03-29 19:23:50 +00001251
1252 /* signal CPU creation */
Andreas Färber61a46212012-05-02 22:49:36 +02001253 cpu->created = true;
Blue Swirl296af7c2010-03-29 19:23:50 +00001254 qemu_cond_signal(&qemu_cpu_cond);
1255
Gu Zheng4c055ab2016-05-12 09:18:13 +05301256 do {
Andreas Färbera1fcaa72012-05-02 23:42:26 +02001257 if (cpu_can_run(cpu)) {
Andreas Färber1458c362013-05-26 23:46:55 +02001258 r = kvm_cpu_exec(cpu);
Jan Kiszka83f338f2011-02-07 12:19:17 +01001259 if (r == EXCP_DEBUG) {
Andreas Färber91325042013-05-27 02:07:49 +02001260 cpu_handle_guest_debug(cpu);
Jan Kiszka83f338f2011-02-07 12:19:17 +01001261 }
Jan Kiszka0ab07c62011-02-07 12:19:14 +01001262 }
Paolo Bonzinidb08b682018-01-11 13:53:12 +01001263 qemu_wait_io_event(cpu);
Gu Zheng4c055ab2016-05-12 09:18:13 +05301264 } while (!cpu->unplug || cpu_can_run(cpu));
Blue Swirl296af7c2010-03-29 19:23:50 +00001265
Gu Zheng4c055ab2016-05-12 09:18:13 +05301266 qemu_kvm_destroy_vcpu(cpu);
Bharata B Rao2c579042016-05-12 09:18:14 +05301267 cpu->created = false;
1268 qemu_cond_signal(&qemu_cpu_cond);
Gu Zheng4c055ab2016-05-12 09:18:13 +05301269 qemu_mutex_unlock_iothread();
Paolo Bonzini57615ed2018-01-30 11:04:36 -05001270 rcu_unregister_thread();
Blue Swirl296af7c2010-03-29 19:23:50 +00001271 return NULL;
1272}
1273
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +02001274static void *qemu_dummy_cpu_thread_fn(void *arg)
1275{
1276#ifdef _WIN32
Alistair Francis493d89b2018-02-03 09:43:14 +01001277 error_report("qtest is not supported under Windows");
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +02001278 exit(1);
1279#else
Andreas Färber10a90212013-05-27 02:24:35 +02001280 CPUState *cpu = arg;
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +02001281 sigset_t waitset;
1282 int r;
1283
Paolo Bonziniab28bd22015-07-09 08:55:38 +02001284 rcu_register_thread();
1285
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +02001286 qemu_mutex_lock_iothread();
Andreas Färber814e6122012-05-02 17:00:37 +02001287 qemu_thread_get_self(cpu->thread);
Andreas Färber9f09e182012-05-03 06:59:07 +02001288 cpu->thread_id = qemu_get_thread_id();
Pavel Dovgalyuk626cf8f2014-12-08 10:53:17 +03001289 cpu->can_do_io = 1;
Alex Bennée37257942017-02-23 18:29:14 +00001290 current_cpu = cpu;
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +02001291
1292 sigemptyset(&waitset);
1293 sigaddset(&waitset, SIG_IPI);
1294
1295 /* signal CPU creation */
Andreas Färber61a46212012-05-02 22:49:36 +02001296 cpu->created = true;
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +02001297 qemu_cond_signal(&qemu_cpu_cond);
1298
Paolo Bonzinid2831ab2018-01-30 11:04:53 -05001299 do {
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +02001300 qemu_mutex_unlock_iothread();
1301 do {
1302 int sig;
1303 r = sigwait(&waitset, &sig);
1304 } while (r == -1 && (errno == EAGAIN || errno == EINTR));
1305 if (r == -1) {
1306 perror("sigwait");
1307 exit(1);
1308 }
1309 qemu_mutex_lock_iothread();
Paolo Bonzinidb08b682018-01-11 13:53:12 +01001310 qemu_wait_io_event(cpu);
Paolo Bonzinid2831ab2018-01-30 11:04:53 -05001311 } while (!cpu->unplug);
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +02001312
Paolo Bonzinid2831ab2018-01-30 11:04:53 -05001313 rcu_unregister_thread();
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +02001314 return NULL;
1315#endif
1316}
1317
Alex Bennée1be7fcb2016-10-27 16:10:08 +01001318static int64_t tcg_get_icount_limit(void)
1319{
1320 int64_t deadline;
1321
1322 if (replay_mode != REPLAY_MODE_PLAY) {
1323 deadline = qemu_clock_deadline_ns_all(QEMU_CLOCK_VIRTUAL);
1324
1325 /* Maintain prior (possibly buggy) behaviour where if no deadline
1326 * was set (as there is no QEMU_CLOCK_VIRTUAL timer) or it is more than
1327 * INT32_MAX nanoseconds ahead, we still use INT32_MAX
1328 * nanoseconds.
1329 */
1330 if ((deadline < 0) || (deadline > INT32_MAX)) {
1331 deadline = INT32_MAX;
1332 }
1333
1334 return qemu_icount_round(deadline);
1335 } else {
1336 return replay_get_instructions();
1337 }
1338}
1339
Alex Bennée12e97002016-10-27 16:10:14 +01001340static void handle_icount_deadline(void)
1341{
Paolo Bonzini6b8f0182017-03-02 19:56:40 +01001342 assert(qemu_in_vcpu_thread());
Alex Bennée12e97002016-10-27 16:10:14 +01001343 if (use_icount) {
1344 int64_t deadline =
1345 qemu_clock_deadline_ns_all(QEMU_CLOCK_VIRTUAL);
1346
1347 if (deadline == 0) {
Paolo Bonzini6b8f0182017-03-02 19:56:40 +01001348 /* Wake up other AioContexts. */
Alex Bennée12e97002016-10-27 16:10:14 +01001349 qemu_clock_notify(QEMU_CLOCK_VIRTUAL);
Paolo Bonzini6b8f0182017-03-02 19:56:40 +01001350 qemu_clock_run_timers(QEMU_CLOCK_VIRTUAL);
Alex Bennée12e97002016-10-27 16:10:14 +01001351 }
1352 }
1353}
1354
Alex Bennée05248382017-03-29 16:46:59 +01001355static void prepare_icount_for_run(CPUState *cpu)
1356{
1357 if (use_icount) {
Alex Bennéeeda5f7c2017-04-05 12:35:48 +01001358 int insns_left;
Alex Bennée05248382017-03-29 16:46:59 +01001359
1360 /* These should always be cleared by process_icount_data after
1361 * each vCPU execution. However u16.high can be raised
1362 * asynchronously by cpu_exit/cpu_interrupt/tcg_handle_interrupt
1363 */
1364 g_assert(cpu->icount_decr.u16.low == 0);
1365 g_assert(cpu->icount_extra == 0);
1366
Alex Bennéeeda5f7c2017-04-05 12:35:48 +01001367 cpu->icount_budget = tcg_get_icount_limit();
1368 insns_left = MIN(0xffff, cpu->icount_budget);
1369 cpu->icount_decr.u16.low = insns_left;
1370 cpu->icount_extra = cpu->icount_budget - insns_left;
Alex Bennéed759c952018-02-27 12:52:48 +03001371
1372 replay_mutex_lock();
Alex Bennée05248382017-03-29 16:46:59 +01001373 }
1374}
1375
1376static void process_icount_data(CPUState *cpu)
1377{
1378 if (use_icount) {
Alex Bennéee4cd9652017-03-31 16:09:42 +01001379 /* Account for executed instructions */
Alex Bennée512d3c82017-04-05 12:32:37 +01001380 cpu_update_icount(cpu);
Alex Bennée05248382017-03-29 16:46:59 +01001381
1382 /* Reset the counters */
1383 cpu->icount_decr.u16.low = 0;
1384 cpu->icount_extra = 0;
Alex Bennéee4cd9652017-03-31 16:09:42 +01001385 cpu->icount_budget = 0;
1386
Alex Bennée05248382017-03-29 16:46:59 +01001387 replay_account_executed_instructions();
Alex Bennéed759c952018-02-27 12:52:48 +03001388
1389 replay_mutex_unlock();
Alex Bennée05248382017-03-29 16:46:59 +01001390 }
1391}
1392
1393
Alex Bennée1be7fcb2016-10-27 16:10:08 +01001394static int tcg_cpu_exec(CPUState *cpu)
1395{
1396 int ret;
1397#ifdef CONFIG_PROFILER
1398 int64_t ti;
1399#endif
1400
Emilio G. Cotaf28d0df2018-06-22 13:45:31 -04001401 assert(tcg_enabled());
Alex Bennée1be7fcb2016-10-27 16:10:08 +01001402#ifdef CONFIG_PROFILER
1403 ti = profile_getclock();
1404#endif
Alex Bennée1be7fcb2016-10-27 16:10:08 +01001405 cpu_exec_start(cpu);
1406 ret = cpu_exec(cpu);
1407 cpu_exec_end(cpu);
1408#ifdef CONFIG_PROFILER
1409 tcg_time += profile_getclock() - ti;
1410#endif
Alex Bennée1be7fcb2016-10-27 16:10:08 +01001411 return ret;
1412}
1413
Alex Bennéec93bbbe2016-10-27 16:10:09 +01001414/* Destroy any remaining vCPUs which have been unplugged and have
1415 * finished running
1416 */
1417static void deal_with_unplugged_cpus(void)
Alex Bennée1be7fcb2016-10-27 16:10:08 +01001418{
Alex Bennéec93bbbe2016-10-27 16:10:09 +01001419 CPUState *cpu;
Alex Bennée1be7fcb2016-10-27 16:10:08 +01001420
Alex Bennéec93bbbe2016-10-27 16:10:09 +01001421 CPU_FOREACH(cpu) {
1422 if (cpu->unplug && !cpu_can_run(cpu)) {
1423 qemu_tcg_destroy_vcpu(cpu);
1424 cpu->created = false;
1425 qemu_cond_signal(&qemu_cpu_cond);
Alex Bennée1be7fcb2016-10-27 16:10:08 +01001426 break;
1427 }
1428 }
Alex Bennée1be7fcb2016-10-27 16:10:08 +01001429}
Jan Kiszkabdb7ca62011-09-26 09:40:39 +02001430
Alex Bennée65467062017-02-23 18:29:09 +00001431/* Single-threaded TCG
1432 *
1433 * In the single-threaded case each vCPU is simulated in turn. If
1434 * there is more than a single vCPU we create a simple timer to kick
1435 * the vCPU and ensure we don't get stuck in a tight loop in one vCPU.
1436 * This is done explicitly rather than relying on side-effects
1437 * elsewhere.
1438 */
1439
Alex Bennée37257942017-02-23 18:29:14 +00001440static void *qemu_tcg_rr_cpu_thread_fn(void *arg)
Blue Swirl296af7c2010-03-29 19:23:50 +00001441{
Andreas Färberc3586ba2012-05-03 01:41:24 +02001442 CPUState *cpu = arg;
Blue Swirl296af7c2010-03-29 19:23:50 +00001443
Emilio G. Cotaf28d0df2018-06-22 13:45:31 -04001444 assert(tcg_enabled());
Paolo Bonziniab28bd22015-07-09 08:55:38 +02001445 rcu_register_thread();
Emilio G. Cota3468b592017-07-19 18:57:58 -04001446 tcg_register_thread();
Paolo Bonziniab28bd22015-07-09 08:55:38 +02001447
Paolo Bonzini2e7f7a32015-06-18 18:47:18 +02001448 qemu_mutex_lock_iothread();
Andreas Färber814e6122012-05-02 17:00:37 +02001449 qemu_thread_get_self(cpu->thread);
Blue Swirl296af7c2010-03-29 19:23:50 +00001450
David Hildenbrand5a9c9732018-02-09 20:52:39 +01001451 cpu->thread_id = qemu_get_thread_id();
1452 cpu->created = true;
1453 cpu->can_do_io = 1;
Blue Swirl296af7c2010-03-29 19:23:50 +00001454 qemu_cond_signal(&qemu_cpu_cond);
1455
Jan Kiszkafa7d1862011-08-22 18:35:25 +02001456 /* wait for initial kick-off after machine start */
Emilio G. Cotac28e3992015-04-27 12:45:28 -04001457 while (first_cpu->stopped) {
KONRAD Fredericd5f8d612015-08-10 17:27:06 +02001458 qemu_cond_wait(first_cpu->halt_cond, &qemu_global_mutex);
Jan Kiszka8e564b42012-02-17 18:31:15 +01001459
1460 /* process any pending work */
Andreas Färberbdc44642013-06-24 23:50:24 +02001461 CPU_FOREACH(cpu) {
Alex Bennée37257942017-02-23 18:29:14 +00001462 current_cpu = cpu;
Andreas Färber182735e2013-05-29 22:29:20 +02001463 qemu_wait_io_event_common(cpu);
Jan Kiszka8e564b42012-02-17 18:31:15 +01001464 }
Jan Kiszka0ab07c62011-02-07 12:19:14 +01001465 }
Blue Swirl296af7c2010-03-29 19:23:50 +00001466
Alex Bennée65467062017-02-23 18:29:09 +00001467 start_tcg_kick_timer();
1468
Alex Bennéec93bbbe2016-10-27 16:10:09 +01001469 cpu = first_cpu;
1470
Alex Bennéee5143e32017-02-23 18:29:12 +00001471 /* process any pending work */
1472 cpu->exit_request = 1;
1473
Blue Swirl296af7c2010-03-29 19:23:50 +00001474 while (1) {
Alex Bennéed759c952018-02-27 12:52:48 +03001475 qemu_mutex_unlock_iothread();
1476 replay_mutex_lock();
1477 qemu_mutex_lock_iothread();
Alex Bennéec93bbbe2016-10-27 16:10:09 +01001478 /* Account partial waits to QEMU_CLOCK_VIRTUAL. */
1479 qemu_account_warp_timer();
1480
Paolo Bonzini6b8f0182017-03-02 19:56:40 +01001481 /* Run the timers here. This is much more efficient than
1482 * waking up the I/O thread and waiting for completion.
1483 */
1484 handle_icount_deadline();
1485
Alex Bennéed759c952018-02-27 12:52:48 +03001486 replay_mutex_unlock();
1487
Alex Bennéec93bbbe2016-10-27 16:10:09 +01001488 if (!cpu) {
1489 cpu = first_cpu;
1490 }
1491
Alex Bennéee5143e32017-02-23 18:29:12 +00001492 while (cpu && !cpu->queued_work_first && !cpu->exit_request) {
1493
Alex Bennée791158d2017-02-23 18:29:10 +00001494 atomic_mb_set(&tcg_current_rr_cpu, cpu);
Alex Bennée37257942017-02-23 18:29:14 +00001495 current_cpu = cpu;
Alex Bennéec93bbbe2016-10-27 16:10:09 +01001496
1497 qemu_clock_enable(QEMU_CLOCK_VIRTUAL,
1498 (cpu->singlestep_enabled & SSTEP_NOTIMER) == 0);
1499
1500 if (cpu_can_run(cpu)) {
1501 int r;
Alex Bennée05248382017-03-29 16:46:59 +01001502
Alex Bennéed759c952018-02-27 12:52:48 +03001503 qemu_mutex_unlock_iothread();
Alex Bennée05248382017-03-29 16:46:59 +01001504 prepare_icount_for_run(cpu);
1505
Alex Bennéec93bbbe2016-10-27 16:10:09 +01001506 r = tcg_cpu_exec(cpu);
Alex Bennée05248382017-03-29 16:46:59 +01001507
1508 process_icount_data(cpu);
Alex Bennéed759c952018-02-27 12:52:48 +03001509 qemu_mutex_lock_iothread();
Alex Bennée05248382017-03-29 16:46:59 +01001510
Alex Bennéec93bbbe2016-10-27 16:10:09 +01001511 if (r == EXCP_DEBUG) {
1512 cpu_handle_guest_debug(cpu);
1513 break;
Pranith Kumar08e73c42017-02-23 18:29:15 +00001514 } else if (r == EXCP_ATOMIC) {
1515 qemu_mutex_unlock_iothread();
1516 cpu_exec_step_atomic(cpu);
1517 qemu_mutex_lock_iothread();
1518 break;
Alex Bennéec93bbbe2016-10-27 16:10:09 +01001519 }
Alex Bennée37257942017-02-23 18:29:14 +00001520 } else if (cpu->stop) {
Alex Bennéec93bbbe2016-10-27 16:10:09 +01001521 if (cpu->unplug) {
1522 cpu = CPU_NEXT(cpu);
1523 }
1524 break;
1525 }
1526
Alex Bennéee5143e32017-02-23 18:29:12 +00001527 cpu = CPU_NEXT(cpu);
1528 } /* while (cpu && !cpu->exit_request).. */
1529
Alex Bennée791158d2017-02-23 18:29:10 +00001530 /* Does not need atomic_mb_set because a spurious wakeup is okay. */
1531 atomic_set(&tcg_current_rr_cpu, NULL);
Alex Bennéec93bbbe2016-10-27 16:10:09 +01001532
Alex Bennéee5143e32017-02-23 18:29:12 +00001533 if (cpu && cpu->exit_request) {
1534 atomic_mb_set(&cpu->exit_request, 0);
1535 }
Alex Blighac70aaf2013-08-21 16:02:57 +01001536
Emilio G. Cota068a5ea2018-08-19 05:13:35 -04001537 qemu_tcg_rr_wait_io_event(cpu ? cpu : first_cpu);
Alex Bennéec93bbbe2016-10-27 16:10:09 +01001538 deal_with_unplugged_cpus();
Blue Swirl296af7c2010-03-29 19:23:50 +00001539 }
1540
Paolo Bonzini9b0605f2018-01-30 11:05:06 -05001541 rcu_unregister_thread();
Blue Swirl296af7c2010-03-29 19:23:50 +00001542 return NULL;
1543}
1544
Vincent Palatinb0cb0a62017-01-10 11:59:57 +01001545static void *qemu_hax_cpu_thread_fn(void *arg)
1546{
1547 CPUState *cpu = arg;
1548 int r;
Vincent Palatinb3d3a422017-03-20 11:15:49 +01001549
Paolo Bonzini9857c2d2018-01-30 16:28:49 +01001550 rcu_register_thread();
Vincent Palatinb3d3a422017-03-20 11:15:49 +01001551 qemu_mutex_lock_iothread();
Vincent Palatinb0cb0a62017-01-10 11:59:57 +01001552 qemu_thread_get_self(cpu->thread);
Vincent Palatinb0cb0a62017-01-10 11:59:57 +01001553
1554 cpu->thread_id = qemu_get_thread_id();
1555 cpu->created = true;
1556 cpu->halted = 0;
1557 current_cpu = cpu;
1558
1559 hax_init_vcpu(cpu);
1560 qemu_cond_signal(&qemu_cpu_cond);
1561
Paolo Bonzini9857c2d2018-01-30 16:28:49 +01001562 do {
Vincent Palatinb0cb0a62017-01-10 11:59:57 +01001563 if (cpu_can_run(cpu)) {
1564 r = hax_smp_cpu_exec(cpu);
1565 if (r == EXCP_DEBUG) {
1566 cpu_handle_guest_debug(cpu);
1567 }
1568 }
1569
Paolo Bonzinidb08b682018-01-11 13:53:12 +01001570 qemu_wait_io_event(cpu);
Paolo Bonzini9857c2d2018-01-30 16:28:49 +01001571 } while (!cpu->unplug || cpu_can_run(cpu));
1572 rcu_unregister_thread();
Vincent Palatinb0cb0a62017-01-10 11:59:57 +01001573 return NULL;
1574}
1575
Sergio Andres Gomez Del Realc97d6d22017-09-13 04:05:09 -05001576/* The HVF-specific vCPU thread function. This one should only run when the host
1577 * CPU supports the VMX "unrestricted guest" feature. */
1578static void *qemu_hvf_cpu_thread_fn(void *arg)
1579{
1580 CPUState *cpu = arg;
1581
1582 int r;
1583
1584 assert(hvf_enabled());
1585
1586 rcu_register_thread();
1587
1588 qemu_mutex_lock_iothread();
1589 qemu_thread_get_self(cpu->thread);
1590
1591 cpu->thread_id = qemu_get_thread_id();
1592 cpu->can_do_io = 1;
1593 current_cpu = cpu;
1594
1595 hvf_init_vcpu(cpu);
1596
1597 /* signal CPU creation */
1598 cpu->created = true;
1599 qemu_cond_signal(&qemu_cpu_cond);
1600
1601 do {
1602 if (cpu_can_run(cpu)) {
1603 r = hvf_vcpu_exec(cpu);
1604 if (r == EXCP_DEBUG) {
1605 cpu_handle_guest_debug(cpu);
1606 }
1607 }
Paolo Bonzinidb08b682018-01-11 13:53:12 +01001608 qemu_wait_io_event(cpu);
Sergio Andres Gomez Del Realc97d6d22017-09-13 04:05:09 -05001609 } while (!cpu->unplug || cpu_can_run(cpu));
1610
1611 hvf_vcpu_destroy(cpu);
1612 cpu->created = false;
1613 qemu_cond_signal(&qemu_cpu_cond);
1614 qemu_mutex_unlock_iothread();
Paolo Bonzini8178e632018-01-30 11:05:21 -05001615 rcu_unregister_thread();
Sergio Andres Gomez Del Realc97d6d22017-09-13 04:05:09 -05001616 return NULL;
1617}
1618
Justin Terry (VM)19306802018-01-22 13:07:49 -08001619static void *qemu_whpx_cpu_thread_fn(void *arg)
1620{
1621 CPUState *cpu = arg;
1622 int r;
1623
1624 rcu_register_thread();
1625
1626 qemu_mutex_lock_iothread();
1627 qemu_thread_get_self(cpu->thread);
1628 cpu->thread_id = qemu_get_thread_id();
1629 current_cpu = cpu;
1630
1631 r = whpx_init_vcpu(cpu);
1632 if (r < 0) {
1633 fprintf(stderr, "whpx_init_vcpu failed: %s\n", strerror(-r));
1634 exit(1);
1635 }
1636
1637 /* signal CPU creation */
1638 cpu->created = true;
1639 qemu_cond_signal(&qemu_cpu_cond);
1640
1641 do {
1642 if (cpu_can_run(cpu)) {
1643 r = whpx_vcpu_exec(cpu);
1644 if (r == EXCP_DEBUG) {
1645 cpu_handle_guest_debug(cpu);
1646 }
1647 }
1648 while (cpu_thread_is_idle(cpu)) {
1649 qemu_cond_wait(cpu->halt_cond, &qemu_global_mutex);
1650 }
1651 qemu_wait_io_event_common(cpu);
1652 } while (!cpu->unplug || cpu_can_run(cpu));
1653
1654 whpx_destroy_vcpu(cpu);
1655 cpu->created = false;
1656 qemu_cond_signal(&qemu_cpu_cond);
1657 qemu_mutex_unlock_iothread();
1658 rcu_unregister_thread();
Vincent Palatinb0cb0a62017-01-10 11:59:57 +01001659 return NULL;
1660}
1661
1662#ifdef _WIN32
1663static void CALLBACK dummy_apc_func(ULONG_PTR unused)
1664{
1665}
1666#endif
1667
Alex Bennée37257942017-02-23 18:29:14 +00001668/* Multi-threaded TCG
1669 *
1670 * In the multi-threaded case each vCPU has its own thread. The TLS
1671 * variable current_cpu can be used deep in the code to find the
1672 * current CPUState for a given thread.
1673 */
1674
1675static void *qemu_tcg_cpu_thread_fn(void *arg)
1676{
1677 CPUState *cpu = arg;
1678
Emilio G. Cotaf28d0df2018-06-22 13:45:31 -04001679 assert(tcg_enabled());
Alex Bennéebf51c722017-03-30 18:32:29 +01001680 g_assert(!use_icount);
1681
Alex Bennée37257942017-02-23 18:29:14 +00001682 rcu_register_thread();
Emilio G. Cota3468b592017-07-19 18:57:58 -04001683 tcg_register_thread();
Alex Bennée37257942017-02-23 18:29:14 +00001684
1685 qemu_mutex_lock_iothread();
1686 qemu_thread_get_self(cpu->thread);
1687
1688 cpu->thread_id = qemu_get_thread_id();
1689 cpu->created = true;
1690 cpu->can_do_io = 1;
1691 current_cpu = cpu;
1692 qemu_cond_signal(&qemu_cpu_cond);
1693
1694 /* process any pending work */
1695 cpu->exit_request = 1;
1696
Cédric Le Goater54961aa2018-04-25 15:18:28 +02001697 do {
Alex Bennée37257942017-02-23 18:29:14 +00001698 if (cpu_can_run(cpu)) {
1699 int r;
Alex Bennéed759c952018-02-27 12:52:48 +03001700 qemu_mutex_unlock_iothread();
Alex Bennée37257942017-02-23 18:29:14 +00001701 r = tcg_cpu_exec(cpu);
Alex Bennéed759c952018-02-27 12:52:48 +03001702 qemu_mutex_lock_iothread();
Alex Bennée37257942017-02-23 18:29:14 +00001703 switch (r) {
1704 case EXCP_DEBUG:
1705 cpu_handle_guest_debug(cpu);
1706 break;
1707 case EXCP_HALTED:
1708 /* during start-up the vCPU is reset and the thread is
1709 * kicked several times. If we don't ensure we go back
1710 * to sleep in the halted state we won't cleanly
1711 * start-up when the vCPU is enabled.
1712 *
1713 * cpu->halted should ensure we sleep in wait_io_event
1714 */
1715 g_assert(cpu->halted);
1716 break;
Pranith Kumar08e73c42017-02-23 18:29:15 +00001717 case EXCP_ATOMIC:
1718 qemu_mutex_unlock_iothread();
1719 cpu_exec_step_atomic(cpu);
1720 qemu_mutex_lock_iothread();
Alex Bennée37257942017-02-23 18:29:14 +00001721 default:
1722 /* Ignore everything else? */
1723 break;
1724 }
1725 }
1726
Alex Bennée37257942017-02-23 18:29:14 +00001727 atomic_mb_set(&cpu->exit_request, 0);
Paolo Bonzinidb08b682018-01-11 13:53:12 +01001728 qemu_wait_io_event(cpu);
Paolo Bonzini9b0605f2018-01-30 11:05:06 -05001729 } while (!cpu->unplug || cpu_can_run(cpu));
Alex Bennée37257942017-02-23 18:29:14 +00001730
Paolo Bonzini9b0605f2018-01-30 11:05:06 -05001731 qemu_tcg_destroy_vcpu(cpu);
1732 cpu->created = false;
1733 qemu_cond_signal(&qemu_cpu_cond);
1734 qemu_mutex_unlock_iothread();
1735 rcu_unregister_thread();
Alex Bennée37257942017-02-23 18:29:14 +00001736 return NULL;
1737}
1738
Andreas Färber2ff09a42012-05-03 00:23:30 +02001739static void qemu_cpu_kick_thread(CPUState *cpu)
Paolo Bonzinicc015e92011-03-12 17:44:08 +01001740{
1741#ifndef _WIN32
1742 int err;
1743
Paolo Bonzinie0c38212015-08-26 00:19:19 +02001744 if (cpu->thread_kicked) {
1745 return;
Paolo Bonzini9102ded2015-08-18 06:52:09 -07001746 }
Paolo Bonzinie0c38212015-08-26 00:19:19 +02001747 cpu->thread_kicked = true;
Andreas Färber814e6122012-05-02 17:00:37 +02001748 err = pthread_kill(cpu->thread->thread, SIG_IPI);
Paolo Bonzinicc015e92011-03-12 17:44:08 +01001749 if (err) {
1750 fprintf(stderr, "qemu:%s: %s", __func__, strerror(err));
1751 exit(1);
1752 }
1753#else /* _WIN32 */
Vincent Palatinb0cb0a62017-01-10 11:59:57 +01001754 if (!qemu_cpu_is_self(cpu)) {
Justin Terry (VM)19306802018-01-22 13:07:49 -08001755 if (whpx_enabled()) {
1756 whpx_vcpu_kick(cpu);
1757 } else if (!QueueUserAPC(dummy_apc_func, cpu->hThread, 0)) {
Vincent Palatinb0cb0a62017-01-10 11:59:57 +01001758 fprintf(stderr, "%s: QueueUserAPC failed with error %lu\n",
1759 __func__, GetLastError());
1760 exit(1);
1761 }
1762 }
Paolo Bonzinicc015e92011-03-12 17:44:08 +01001763#endif
1764}
1765
Andreas Färberc08d7422012-05-03 04:34:15 +02001766void qemu_cpu_kick(CPUState *cpu)
Blue Swirl296af7c2010-03-29 19:23:50 +00001767{
Andreas Färberf5c121b2012-05-03 01:22:49 +02001768 qemu_cond_broadcast(cpu->halt_cond);
Paolo Bonzinie0c38212015-08-26 00:19:19 +02001769 if (tcg_enabled()) {
Alex Bennée791158d2017-02-23 18:29:10 +00001770 cpu_exit(cpu);
Alex Bennée37257942017-02-23 18:29:14 +00001771 /* NOP unless doing single-thread RR */
Alex Bennée791158d2017-02-23 18:29:10 +00001772 qemu_cpu_kick_rr_cpu();
Paolo Bonzinie0c38212015-08-26 00:19:19 +02001773 } else {
Vincent Palatinb0cb0a62017-01-10 11:59:57 +01001774 if (hax_enabled()) {
1775 /*
1776 * FIXME: race condition with the exit_request check in
1777 * hax_vcpu_hax_exec
1778 */
1779 cpu->exit_request = 1;
1780 }
Paolo Bonzinie0c38212015-08-26 00:19:19 +02001781 qemu_cpu_kick_thread(cpu);
1782 }
Blue Swirl296af7c2010-03-29 19:23:50 +00001783}
1784
Jan Kiszka46d62fa2011-02-01 22:15:59 +01001785void qemu_cpu_kick_self(void)
1786{
Andreas Färber4917cf42013-05-27 05:17:50 +02001787 assert(current_cpu);
Paolo Bonzini9102ded2015-08-18 06:52:09 -07001788 qemu_cpu_kick_thread(current_cpu);
Blue Swirl296af7c2010-03-29 19:23:50 +00001789}
1790
Andreas Färber60e82572012-05-02 22:23:49 +02001791bool qemu_cpu_is_self(CPUState *cpu)
Blue Swirl296af7c2010-03-29 19:23:50 +00001792{
Andreas Färber814e6122012-05-02 17:00:37 +02001793 return qemu_thread_is_self(cpu->thread);
Blue Swirl296af7c2010-03-29 19:23:50 +00001794}
1795
Paolo Bonzini79e2b9a2015-01-21 12:09:14 +01001796bool qemu_in_vcpu_thread(void)
Juan Quintelaaa723c22012-09-18 16:30:11 +02001797{
Andreas Färber4917cf42013-05-27 05:17:50 +02001798 return current_cpu && qemu_cpu_is_self(current_cpu);
Juan Quintelaaa723c22012-09-18 16:30:11 +02001799}
1800
Paolo Bonziniafbe7052015-06-18 18:47:19 +02001801static __thread bool iothread_locked = false;
1802
1803bool qemu_mutex_iothread_locked(void)
1804{
1805 return iothread_locked;
1806}
1807
Emilio G. Cotacb764d02017-10-28 02:16:41 -04001808/*
1809 * The BQL is taken from so many places that it is worth profiling the
1810 * callers directly, instead of funneling them all through a single function.
1811 */
1812void qemu_mutex_lock_iothread_impl(const char *file, int line)
Blue Swirl296af7c2010-03-29 19:23:50 +00001813{
Emilio G. Cotacb764d02017-10-28 02:16:41 -04001814 QemuMutexLockFunc bql_lock = atomic_read(&qemu_bql_mutex_lock_func);
1815
Jan Kiszka8d04fb52017-02-23 18:29:11 +00001816 g_assert(!qemu_mutex_iothread_locked());
Emilio G. Cotacb764d02017-10-28 02:16:41 -04001817 bql_lock(&qemu_global_mutex, file, line);
Paolo Bonziniafbe7052015-06-18 18:47:19 +02001818 iothread_locked = true;
Blue Swirl296af7c2010-03-29 19:23:50 +00001819}
1820
1821void qemu_mutex_unlock_iothread(void)
1822{
Jan Kiszka8d04fb52017-02-23 18:29:11 +00001823 g_assert(qemu_mutex_iothread_locked());
Paolo Bonziniafbe7052015-06-18 18:47:19 +02001824 iothread_locked = false;
Blue Swirl296af7c2010-03-29 19:23:50 +00001825 qemu_mutex_unlock(&qemu_global_mutex);
1826}
1827
Alex Bennéee8faee02016-10-27 16:09:58 +01001828static bool all_vcpus_paused(void)
Blue Swirl296af7c2010-03-29 19:23:50 +00001829{
Andreas Färberbdc44642013-06-24 23:50:24 +02001830 CPUState *cpu;
Blue Swirl296af7c2010-03-29 19:23:50 +00001831
Andreas Färberbdc44642013-06-24 23:50:24 +02001832 CPU_FOREACH(cpu) {
Andreas Färber182735e2013-05-29 22:29:20 +02001833 if (!cpu->stopped) {
Alex Bennéee8faee02016-10-27 16:09:58 +01001834 return false;
Jan Kiszka0ab07c62011-02-07 12:19:14 +01001835 }
Blue Swirl296af7c2010-03-29 19:23:50 +00001836 }
1837
Alex Bennéee8faee02016-10-27 16:09:58 +01001838 return true;
Blue Swirl296af7c2010-03-29 19:23:50 +00001839}
1840
1841void pause_all_vcpus(void)
1842{
Andreas Färberbdc44642013-06-24 23:50:24 +02001843 CPUState *cpu;
Blue Swirl296af7c2010-03-29 19:23:50 +00001844
Alex Bligh40daca52013-08-21 16:03:02 +01001845 qemu_clock_enable(QEMU_CLOCK_VIRTUAL, false);
Andreas Färberbdc44642013-06-24 23:50:24 +02001846 CPU_FOREACH(cpu) {
David Hildenbrandebd05fe2017-11-29 20:12:15 +01001847 if (qemu_cpu_is_self(cpu)) {
1848 qemu_cpu_stop(cpu, true);
1849 } else {
1850 cpu->stop = true;
1851 qemu_cpu_kick(cpu);
1852 }
Jan Kiszkad798e972012-02-17 18:31:16 +01001853 }
1854
Alex Bennéed759c952018-02-27 12:52:48 +03001855 /* We need to drop the replay_lock so any vCPU threads woken up
1856 * can finish their replay tasks
1857 */
1858 replay_mutex_unlock();
1859
Blue Swirl296af7c2010-03-29 19:23:50 +00001860 while (!all_vcpus_paused()) {
Paolo Bonzinibe7d6c52011-03-12 17:44:02 +01001861 qemu_cond_wait(&qemu_pause_cond, &qemu_global_mutex);
Andreas Färberbdc44642013-06-24 23:50:24 +02001862 CPU_FOREACH(cpu) {
Andreas Färber182735e2013-05-29 22:29:20 +02001863 qemu_cpu_kick(cpu);
Blue Swirl296af7c2010-03-29 19:23:50 +00001864 }
1865 }
Alex Bennéed759c952018-02-27 12:52:48 +03001866
1867 qemu_mutex_unlock_iothread();
1868 replay_mutex_lock();
1869 qemu_mutex_lock_iothread();
Blue Swirl296af7c2010-03-29 19:23:50 +00001870}
1871
Igor Mammedov29936832013-04-23 10:29:37 +02001872void cpu_resume(CPUState *cpu)
1873{
1874 cpu->stop = false;
1875 cpu->stopped = false;
1876 qemu_cpu_kick(cpu);
1877}
1878
Blue Swirl296af7c2010-03-29 19:23:50 +00001879void resume_all_vcpus(void)
1880{
Andreas Färberbdc44642013-06-24 23:50:24 +02001881 CPUState *cpu;
Blue Swirl296af7c2010-03-29 19:23:50 +00001882
Alex Bligh40daca52013-08-21 16:03:02 +01001883 qemu_clock_enable(QEMU_CLOCK_VIRTUAL, true);
Andreas Färberbdc44642013-06-24 23:50:24 +02001884 CPU_FOREACH(cpu) {
Andreas Färber182735e2013-05-29 22:29:20 +02001885 cpu_resume(cpu);
Blue Swirl296af7c2010-03-29 19:23:50 +00001886 }
1887}
1888
Paolo Bonzinidbadee42018-01-30 16:40:12 +01001889void cpu_remove_sync(CPUState *cpu)
Gu Zheng4c055ab2016-05-12 09:18:13 +05301890{
1891 cpu->stop = true;
1892 cpu->unplug = true;
1893 qemu_cpu_kick(cpu);
Paolo Bonzinidbadee42018-01-30 16:40:12 +01001894 qemu_mutex_unlock_iothread();
1895 qemu_thread_join(cpu->thread);
1896 qemu_mutex_lock_iothread();
Bharata B Rao2c579042016-05-12 09:18:14 +05301897}
1898
Dr. David Alan Gilbert49001162014-01-30 10:20:32 +00001899/* For temporary buffers for forming a name */
1900#define VCPU_THREAD_NAME_SIZE 16
1901
Andreas Färbere5ab30a2012-05-03 01:50:44 +02001902static void qemu_tcg_init_vcpu(CPUState *cpu)
Blue Swirl296af7c2010-03-29 19:23:50 +00001903{
Dr. David Alan Gilbert49001162014-01-30 10:20:32 +00001904 char thread_name[VCPU_THREAD_NAME_SIZE];
Alex Bennée37257942017-02-23 18:29:14 +00001905 static QemuCond *single_tcg_halt_cond;
1906 static QemuThread *single_tcg_cpu_thread;
Emilio G. Cotae8feb962017-07-07 19:24:20 -04001907 static int tcg_region_inited;
1908
Emilio G. Cotaf28d0df2018-06-22 13:45:31 -04001909 assert(tcg_enabled());
Emilio G. Cotae8feb962017-07-07 19:24:20 -04001910 /*
1911 * Initialize TCG regions--once. Now is a good time, because:
1912 * (1) TCG's init context, prologue and target globals have been set up.
1913 * (2) qemu_tcg_mttcg_enabled() works now (TCG init code runs before the
1914 * -accel flag is processed, so the check doesn't work then).
1915 */
1916 if (!tcg_region_inited) {
1917 tcg_region_inited = 1;
1918 tcg_region_init();
1919 }
Dr. David Alan Gilbert49001162014-01-30 10:20:32 +00001920
Alex Bennée37257942017-02-23 18:29:14 +00001921 if (qemu_tcg_mttcg_enabled() || !single_tcg_cpu_thread) {
Andreas Färber814e6122012-05-02 17:00:37 +02001922 cpu->thread = g_malloc0(sizeof(QemuThread));
Andreas Färberf5c121b2012-05-03 01:22:49 +02001923 cpu->halt_cond = g_malloc0(sizeof(QemuCond));
1924 qemu_cond_init(cpu->halt_cond);
Alex Bennée37257942017-02-23 18:29:14 +00001925
1926 if (qemu_tcg_mttcg_enabled()) {
1927 /* create a thread per vCPU with TCG (MTTCG) */
1928 parallel_cpus = true;
1929 snprintf(thread_name, VCPU_THREAD_NAME_SIZE, "CPU %d/TCG",
Dr. David Alan Gilbert49001162014-01-30 10:20:32 +00001930 cpu->cpu_index);
Alex Bennée37257942017-02-23 18:29:14 +00001931
1932 qemu_thread_create(cpu->thread, thread_name, qemu_tcg_cpu_thread_fn,
1933 cpu, QEMU_THREAD_JOINABLE);
1934
1935 } else {
1936 /* share a single thread for all cpus with TCG */
1937 snprintf(thread_name, VCPU_THREAD_NAME_SIZE, "ALL CPUs/TCG");
1938 qemu_thread_create(cpu->thread, thread_name,
1939 qemu_tcg_rr_cpu_thread_fn,
1940 cpu, QEMU_THREAD_JOINABLE);
1941
1942 single_tcg_halt_cond = cpu->halt_cond;
1943 single_tcg_cpu_thread = cpu->thread;
1944 }
Paolo Bonzini1ecf47b2011-12-13 13:43:52 +01001945#ifdef _WIN32
Andreas Färber814e6122012-05-02 17:00:37 +02001946 cpu->hThread = qemu_thread_get_handle(cpu->thread);
Paolo Bonzini1ecf47b2011-12-13 13:43:52 +01001947#endif
Blue Swirl296af7c2010-03-29 19:23:50 +00001948 } else {
Alex Bennée37257942017-02-23 18:29:14 +00001949 /* For non-MTTCG cases we share the thread */
1950 cpu->thread = single_tcg_cpu_thread;
1951 cpu->halt_cond = single_tcg_halt_cond;
David Hildenbranda3421732018-02-09 20:52:37 +01001952 cpu->thread_id = first_cpu->thread_id;
1953 cpu->can_do_io = 1;
1954 cpu->created = true;
Blue Swirl296af7c2010-03-29 19:23:50 +00001955 }
1956}
1957
Vincent Palatinb0cb0a62017-01-10 11:59:57 +01001958static void qemu_hax_start_vcpu(CPUState *cpu)
1959{
1960 char thread_name[VCPU_THREAD_NAME_SIZE];
1961
1962 cpu->thread = g_malloc0(sizeof(QemuThread));
1963 cpu->halt_cond = g_malloc0(sizeof(QemuCond));
1964 qemu_cond_init(cpu->halt_cond);
1965
1966 snprintf(thread_name, VCPU_THREAD_NAME_SIZE, "CPU %d/HAX",
1967 cpu->cpu_index);
1968 qemu_thread_create(cpu->thread, thread_name, qemu_hax_cpu_thread_fn,
1969 cpu, QEMU_THREAD_JOINABLE);
1970#ifdef _WIN32
1971 cpu->hThread = qemu_thread_get_handle(cpu->thread);
1972#endif
Vincent Palatinb0cb0a62017-01-10 11:59:57 +01001973}
1974
Andreas Färber48a106b2013-05-27 02:20:39 +02001975static void qemu_kvm_start_vcpu(CPUState *cpu)
Blue Swirl296af7c2010-03-29 19:23:50 +00001976{
Dr. David Alan Gilbert49001162014-01-30 10:20:32 +00001977 char thread_name[VCPU_THREAD_NAME_SIZE];
1978
Andreas Färber814e6122012-05-02 17:00:37 +02001979 cpu->thread = g_malloc0(sizeof(QemuThread));
Andreas Färberf5c121b2012-05-03 01:22:49 +02001980 cpu->halt_cond = g_malloc0(sizeof(QemuCond));
1981 qemu_cond_init(cpu->halt_cond);
Dr. David Alan Gilbert49001162014-01-30 10:20:32 +00001982 snprintf(thread_name, VCPU_THREAD_NAME_SIZE, "CPU %d/KVM",
1983 cpu->cpu_index);
1984 qemu_thread_create(cpu->thread, thread_name, qemu_kvm_cpu_thread_fn,
1985 cpu, QEMU_THREAD_JOINABLE);
Blue Swirl296af7c2010-03-29 19:23:50 +00001986}
1987
Sergio Andres Gomez Del Realc97d6d22017-09-13 04:05:09 -05001988static void qemu_hvf_start_vcpu(CPUState *cpu)
1989{
1990 char thread_name[VCPU_THREAD_NAME_SIZE];
1991
1992 /* HVF currently does not support TCG, and only runs in
1993 * unrestricted-guest mode. */
1994 assert(hvf_enabled());
1995
1996 cpu->thread = g_malloc0(sizeof(QemuThread));
1997 cpu->halt_cond = g_malloc0(sizeof(QemuCond));
1998 qemu_cond_init(cpu->halt_cond);
1999
2000 snprintf(thread_name, VCPU_THREAD_NAME_SIZE, "CPU %d/HVF",
2001 cpu->cpu_index);
2002 qemu_thread_create(cpu->thread, thread_name, qemu_hvf_cpu_thread_fn,
2003 cpu, QEMU_THREAD_JOINABLE);
Sergio Andres Gomez Del Realc97d6d22017-09-13 04:05:09 -05002004}
2005
Justin Terry (VM)19306802018-01-22 13:07:49 -08002006static void qemu_whpx_start_vcpu(CPUState *cpu)
2007{
2008 char thread_name[VCPU_THREAD_NAME_SIZE];
2009
2010 cpu->thread = g_malloc0(sizeof(QemuThread));
2011 cpu->halt_cond = g_malloc0(sizeof(QemuCond));
2012 qemu_cond_init(cpu->halt_cond);
2013 snprintf(thread_name, VCPU_THREAD_NAME_SIZE, "CPU %d/WHPX",
2014 cpu->cpu_index);
2015 qemu_thread_create(cpu->thread, thread_name, qemu_whpx_cpu_thread_fn,
2016 cpu, QEMU_THREAD_JOINABLE);
2017#ifdef _WIN32
2018 cpu->hThread = qemu_thread_get_handle(cpu->thread);
2019#endif
Justin Terry (VM)19306802018-01-22 13:07:49 -08002020}
2021
Andreas Färber10a90212013-05-27 02:24:35 +02002022static void qemu_dummy_start_vcpu(CPUState *cpu)
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +02002023{
Dr. David Alan Gilbert49001162014-01-30 10:20:32 +00002024 char thread_name[VCPU_THREAD_NAME_SIZE];
2025
Andreas Färber814e6122012-05-02 17:00:37 +02002026 cpu->thread = g_malloc0(sizeof(QemuThread));
Andreas Färberf5c121b2012-05-03 01:22:49 +02002027 cpu->halt_cond = g_malloc0(sizeof(QemuCond));
2028 qemu_cond_init(cpu->halt_cond);
Dr. David Alan Gilbert49001162014-01-30 10:20:32 +00002029 snprintf(thread_name, VCPU_THREAD_NAME_SIZE, "CPU %d/DUMMY",
2030 cpu->cpu_index);
2031 qemu_thread_create(cpu->thread, thread_name, qemu_dummy_cpu_thread_fn, cpu,
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +02002032 QEMU_THREAD_JOINABLE);
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +02002033}
2034
Andreas Färberc643bed2013-05-27 03:23:24 +02002035void qemu_init_vcpu(CPUState *cpu)
Blue Swirl296af7c2010-03-29 19:23:50 +00002036{
Andreas Färberce3960e2012-12-17 03:27:07 +01002037 cpu->nr_cores = smp_cores;
2038 cpu->nr_threads = smp_threads;
Andreas Färberf324e762012-05-02 23:26:21 +02002039 cpu->stopped = true;
Peter Maydell56943e82016-01-21 14:15:04 +00002040
2041 if (!cpu->as) {
2042 /* If the target cpu hasn't set up any address spaces itself,
2043 * give it the default one.
2044 */
Peter Maydell12ebc9a2016-01-21 14:15:04 +00002045 cpu->num_ases = 1;
Peter Xu80ceb072017-11-23 17:23:32 +08002046 cpu_address_space_init(cpu, 0, "cpu-memory", cpu->memory);
Peter Maydell56943e82016-01-21 14:15:04 +00002047 }
2048
Jan Kiszka0ab07c62011-02-07 12:19:14 +01002049 if (kvm_enabled()) {
Andreas Färber48a106b2013-05-27 02:20:39 +02002050 qemu_kvm_start_vcpu(cpu);
Vincent Palatinb0cb0a62017-01-10 11:59:57 +01002051 } else if (hax_enabled()) {
2052 qemu_hax_start_vcpu(cpu);
Sergio Andres Gomez Del Realc97d6d22017-09-13 04:05:09 -05002053 } else if (hvf_enabled()) {
2054 qemu_hvf_start_vcpu(cpu);
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +02002055 } else if (tcg_enabled()) {
Andreas Färbere5ab30a2012-05-03 01:50:44 +02002056 qemu_tcg_init_vcpu(cpu);
Justin Terry (VM)19306802018-01-22 13:07:49 -08002057 } else if (whpx_enabled()) {
2058 qemu_whpx_start_vcpu(cpu);
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +02002059 } else {
Andreas Färber10a90212013-05-27 02:24:35 +02002060 qemu_dummy_start_vcpu(cpu);
Jan Kiszka0ab07c62011-02-07 12:19:14 +01002061 }
David Hildenbrand81e96312018-02-09 20:52:38 +01002062
2063 while (!cpu->created) {
2064 qemu_cond_wait(&qemu_cpu_cond, &qemu_global_mutex);
2065 }
Blue Swirl296af7c2010-03-29 19:23:50 +00002066}
2067
Jan Kiszkab4a3d962011-02-01 22:15:43 +01002068void cpu_stop_current(void)
Blue Swirl296af7c2010-03-29 19:23:50 +00002069{
Andreas Färber4917cf42013-05-27 05:17:50 +02002070 if (current_cpu) {
David Hildenbrandebd05fe2017-11-29 20:12:15 +01002071 qemu_cpu_stop(current_cpu, true);
Jan Kiszkab4a3d962011-02-01 22:15:43 +01002072 }
Blue Swirl296af7c2010-03-29 19:23:50 +00002073}
2074
Kevin Wolf56983462013-07-05 13:49:54 +02002075int vm_stop(RunState state)
Blue Swirl296af7c2010-03-29 19:23:50 +00002076{
Juan Quintelaaa723c22012-09-18 16:30:11 +02002077 if (qemu_in_vcpu_thread()) {
Paolo Bonzini74892d22014-06-05 14:53:58 +02002078 qemu_system_vmstop_request_prepare();
Luiz Capitulino1dfb4dd2011-07-29 14:26:33 -03002079 qemu_system_vmstop_request(state);
Blue Swirl296af7c2010-03-29 19:23:50 +00002080 /*
2081 * FIXME: should not return to device code in case
2082 * vm_stop() has been requested.
2083 */
Jan Kiszkab4a3d962011-02-01 22:15:43 +01002084 cpu_stop_current();
Kevin Wolf56983462013-07-05 13:49:54 +02002085 return 0;
Blue Swirl296af7c2010-03-29 19:23:50 +00002086 }
Kevin Wolf56983462013-07-05 13:49:54 +02002087
Stefan Hajnoczi4486e892018-03-07 14:42:05 +00002088 return do_vm_stop(state, true);
Blue Swirl296af7c2010-03-29 19:23:50 +00002089}
2090
Claudio Imbrenda2d76e822017-02-14 18:07:47 +01002091/**
2092 * Prepare for (re)starting the VM.
2093 * Returns -1 if the vCPUs are not to be restarted (e.g. if they are already
2094 * running or in case of an error condition), 0 otherwise.
2095 */
2096int vm_prepare_start(void)
2097{
2098 RunState requested;
Claudio Imbrenda2d76e822017-02-14 18:07:47 +01002099
2100 qemu_vmstop_requested(&requested);
2101 if (runstate_is_running() && requested == RUN_STATE__MAX) {
2102 return -1;
2103 }
2104
2105 /* Ensure that a STOP/RESUME pair of events is emitted if a
2106 * vmstop request was pending. The BLOCK_IO_ERROR event, for
2107 * example, according to documentation is always followed by
2108 * the STOP event.
2109 */
2110 if (runstate_is_running()) {
Peter Xu3ab72382018-08-15 21:37:37 +08002111 qapi_event_send_stop();
2112 qapi_event_send_resume();
Markus Armbrusterf0561582018-04-23 10:45:18 +02002113 return -1;
Claudio Imbrenda2d76e822017-02-14 18:07:47 +01002114 }
2115
2116 /* We are sending this now, but the CPUs will be resumed shortly later */
Peter Xu3ab72382018-08-15 21:37:37 +08002117 qapi_event_send_resume();
Markus Armbrusterf0561582018-04-23 10:45:18 +02002118
2119 replay_enable_events();
2120 cpu_enable_ticks();
2121 runstate_set(RUN_STATE_RUNNING);
2122 vm_state_notify(1, RUN_STATE_RUNNING);
2123 return 0;
Claudio Imbrenda2d76e822017-02-14 18:07:47 +01002124}
2125
2126void vm_start(void)
2127{
2128 if (!vm_prepare_start()) {
2129 resume_all_vcpus();
2130 }
2131}
2132
Luiz Capitulino8a9236f2011-10-14 11:18:09 -03002133/* does a state transition even if the VM is already stopped,
2134 current state is forgotten forever */
Kevin Wolf56983462013-07-05 13:49:54 +02002135int vm_stop_force_state(RunState state)
Luiz Capitulino8a9236f2011-10-14 11:18:09 -03002136{
2137 if (runstate_is_running()) {
Kevin Wolf56983462013-07-05 13:49:54 +02002138 return vm_stop(state);
Luiz Capitulino8a9236f2011-10-14 11:18:09 -03002139 } else {
2140 runstate_set(state);
Wen Congyangb2780d32015-11-20 17:34:38 +08002141
2142 bdrv_drain_all();
Kevin Wolf594a45c2013-07-18 14:52:19 +02002143 /* Make sure to return an error if the flush in a previous vm_stop()
2144 * failed. */
John Snow22af08e2016-09-22 21:45:51 -04002145 return bdrv_flush_all();
Luiz Capitulino8a9236f2011-10-14 11:18:09 -03002146 }
2147}
2148
Stefan Weil9a78eea2010-10-22 23:03:33 +02002149void list_cpus(FILE *f, fprintf_function cpu_fprintf, const char *optarg)
Blue Swirl262353c2010-05-04 19:55:35 +00002150{
2151 /* XXX: implement xxx_cpu_list for targets that still miss it */
Peter Maydelle916cbf2012-09-05 17:41:08 -03002152#if defined(cpu_list)
2153 cpu_list(f, cpu_fprintf);
Blue Swirl262353c2010-05-04 19:55:35 +00002154#endif
2155}
Luiz Capitulinode0b36b2011-09-21 16:38:35 -03002156
2157CpuInfoList *qmp_query_cpus(Error **errp)
2158{
Igor Mammedovafed5a52017-05-10 13:29:55 +02002159 MachineState *ms = MACHINE(qdev_get_machine());
2160 MachineClass *mc = MACHINE_GET_CLASS(ms);
Luiz Capitulinode0b36b2011-09-21 16:38:35 -03002161 CpuInfoList *head = NULL, *cur_item = NULL;
Andreas Färber182735e2013-05-29 22:29:20 +02002162 CPUState *cpu;
Luiz Capitulinode0b36b2011-09-21 16:38:35 -03002163
Andreas Färberbdc44642013-06-24 23:50:24 +02002164 CPU_FOREACH(cpu) {
Luiz Capitulinode0b36b2011-09-21 16:38:35 -03002165 CpuInfoList *info;
Andreas Färber182735e2013-05-29 22:29:20 +02002166#if defined(TARGET_I386)
2167 X86CPU *x86_cpu = X86_CPU(cpu);
2168 CPUX86State *env = &x86_cpu->env;
2169#elif defined(TARGET_PPC)
2170 PowerPCCPU *ppc_cpu = POWERPC_CPU(cpu);
2171 CPUPPCState *env = &ppc_cpu->env;
2172#elif defined(TARGET_SPARC)
2173 SPARCCPU *sparc_cpu = SPARC_CPU(cpu);
2174 CPUSPARCState *env = &sparc_cpu->env;
Michael Clark25fa1942018-03-03 01:32:59 +13002175#elif defined(TARGET_RISCV)
2176 RISCVCPU *riscv_cpu = RISCV_CPU(cpu);
2177 CPURISCVState *env = &riscv_cpu->env;
Andreas Färber182735e2013-05-29 22:29:20 +02002178#elif defined(TARGET_MIPS)
2179 MIPSCPU *mips_cpu = MIPS_CPU(cpu);
2180 CPUMIPSState *env = &mips_cpu->env;
Bastian Koppelmann48e06fe2014-09-01 12:59:46 +01002181#elif defined(TARGET_TRICORE)
2182 TriCoreCPU *tricore_cpu = TRICORE_CPU(cpu);
2183 CPUTriCoreState *env = &tricore_cpu->env;
Viktor Mihajlovski9d0306d2018-02-16 17:08:37 +01002184#elif defined(TARGET_S390X)
2185 S390CPU *s390_cpu = S390_CPU(cpu);
2186 CPUS390XState *env = &s390_cpu->env;
Andreas Färber182735e2013-05-29 22:29:20 +02002187#endif
Luiz Capitulinode0b36b2011-09-21 16:38:35 -03002188
Andreas Färbercb446ec2013-05-01 14:24:52 +02002189 cpu_synchronize_state(cpu);
Luiz Capitulinode0b36b2011-09-21 16:38:35 -03002190
2191 info = g_malloc0(sizeof(*info));
2192 info->value = g_malloc0(sizeof(*info->value));
Andreas Färber55e5c282012-12-17 06:18:02 +01002193 info->value->CPU = cpu->cpu_index;
Andreas Färber182735e2013-05-29 22:29:20 +02002194 info->value->current = (cpu == first_cpu);
Andreas Färber259186a2013-01-17 18:51:17 +01002195 info->value->halted = cpu->halted;
Eduardo Habkost58f88d42015-05-08 16:04:22 -03002196 info->value->qom_path = object_get_canonical_path(OBJECT(cpu));
Andreas Färber9f09e182012-05-03 06:59:07 +02002197 info->value->thread_id = cpu->thread_id;
Luiz Capitulinode0b36b2011-09-21 16:38:35 -03002198#if defined(TARGET_I386)
Eric Blake86f4b682015-11-18 01:52:59 -07002199 info->value->arch = CPU_INFO_ARCH_X86;
Eric Blake544a3732016-02-17 23:48:27 -07002200 info->value->u.x86.pc = env->eip + env->segs[R_CS].base;
Luiz Capitulinode0b36b2011-09-21 16:38:35 -03002201#elif defined(TARGET_PPC)
Eric Blake86f4b682015-11-18 01:52:59 -07002202 info->value->arch = CPU_INFO_ARCH_PPC;
Eric Blake544a3732016-02-17 23:48:27 -07002203 info->value->u.ppc.nip = env->nip;
Luiz Capitulinode0b36b2011-09-21 16:38:35 -03002204#elif defined(TARGET_SPARC)
Eric Blake86f4b682015-11-18 01:52:59 -07002205 info->value->arch = CPU_INFO_ARCH_SPARC;
Eric Blake544a3732016-02-17 23:48:27 -07002206 info->value->u.q_sparc.pc = env->pc;
2207 info->value->u.q_sparc.npc = env->npc;
Luiz Capitulinode0b36b2011-09-21 16:38:35 -03002208#elif defined(TARGET_MIPS)
Eric Blake86f4b682015-11-18 01:52:59 -07002209 info->value->arch = CPU_INFO_ARCH_MIPS;
Eric Blake544a3732016-02-17 23:48:27 -07002210 info->value->u.q_mips.PC = env->active_tc.PC;
Bastian Koppelmann48e06fe2014-09-01 12:59:46 +01002211#elif defined(TARGET_TRICORE)
Eric Blake86f4b682015-11-18 01:52:59 -07002212 info->value->arch = CPU_INFO_ARCH_TRICORE;
Eric Blake544a3732016-02-17 23:48:27 -07002213 info->value->u.tricore.PC = env->PC;
Viktor Mihajlovski9d0306d2018-02-16 17:08:37 +01002214#elif defined(TARGET_S390X)
2215 info->value->arch = CPU_INFO_ARCH_S390;
2216 info->value->u.s390.cpu_state = env->cpu_state;
Michael Clark25fa1942018-03-03 01:32:59 +13002217#elif defined(TARGET_RISCV)
2218 info->value->arch = CPU_INFO_ARCH_RISCV;
2219 info->value->u.riscv.pc = env->pc;
Eric Blake86f4b682015-11-18 01:52:59 -07002220#else
2221 info->value->arch = CPU_INFO_ARCH_OTHER;
Luiz Capitulinode0b36b2011-09-21 16:38:35 -03002222#endif
Igor Mammedovafed5a52017-05-10 13:29:55 +02002223 info->value->has_props = !!mc->cpu_index_to_instance_props;
2224 if (info->value->has_props) {
2225 CpuInstanceProperties *props;
2226 props = g_malloc0(sizeof(*props));
2227 *props = mc->cpu_index_to_instance_props(ms, cpu->cpu_index);
2228 info->value->props = props;
2229 }
Luiz Capitulinode0b36b2011-09-21 16:38:35 -03002230
2231 /* XXX: waiting for the qapi to support GSList */
2232 if (!cur_item) {
2233 head = cur_item = info;
2234 } else {
2235 cur_item->next = info;
2236 cur_item = info;
2237 }
2238 }
2239
2240 return head;
2241}
Luiz Capitulino0cfd6a92011-11-22 16:32:37 -02002242
Laszlo Ersekdaa9d2b2018-04-27 21:28:51 +02002243static CpuInfoArch sysemu_target_to_cpuinfo_arch(SysEmuTarget target)
2244{
2245 /*
2246 * The @SysEmuTarget -> @CpuInfoArch mapping below is based on the
2247 * TARGET_ARCH -> TARGET_BASE_ARCH mapping in the "configure" script.
2248 */
2249 switch (target) {
2250 case SYS_EMU_TARGET_I386:
2251 case SYS_EMU_TARGET_X86_64:
2252 return CPU_INFO_ARCH_X86;
2253
2254 case SYS_EMU_TARGET_PPC:
Laszlo Ersekdaa9d2b2018-04-27 21:28:51 +02002255 case SYS_EMU_TARGET_PPC64:
2256 return CPU_INFO_ARCH_PPC;
2257
2258 case SYS_EMU_TARGET_SPARC:
2259 case SYS_EMU_TARGET_SPARC64:
2260 return CPU_INFO_ARCH_SPARC;
2261
2262 case SYS_EMU_TARGET_MIPS:
2263 case SYS_EMU_TARGET_MIPSEL:
2264 case SYS_EMU_TARGET_MIPS64:
2265 case SYS_EMU_TARGET_MIPS64EL:
2266 return CPU_INFO_ARCH_MIPS;
2267
2268 case SYS_EMU_TARGET_TRICORE:
2269 return CPU_INFO_ARCH_TRICORE;
2270
2271 case SYS_EMU_TARGET_S390X:
2272 return CPU_INFO_ARCH_S390;
2273
2274 case SYS_EMU_TARGET_RISCV32:
2275 case SYS_EMU_TARGET_RISCV64:
2276 return CPU_INFO_ARCH_RISCV;
2277
2278 default:
2279 return CPU_INFO_ARCH_OTHER;
2280 }
2281}
2282
2283static void cpustate_to_cpuinfo_s390(CpuInfoS390 *info, const CPUState *cpu)
2284{
2285#ifdef TARGET_S390X
2286 S390CPU *s390_cpu = S390_CPU(cpu);
2287 CPUS390XState *env = &s390_cpu->env;
2288
2289 info->cpu_state = env->cpu_state;
2290#else
2291 abort();
2292#endif
2293}
2294
Luiz Capitulinoce74ee32018-02-16 17:08:38 +01002295/*
2296 * fast means: we NEVER interrupt vCPU threads to retrieve
2297 * information from KVM.
2298 */
2299CpuInfoFastList *qmp_query_cpus_fast(Error **errp)
2300{
2301 MachineState *ms = MACHINE(qdev_get_machine());
2302 MachineClass *mc = MACHINE_GET_CLASS(ms);
2303 CpuInfoFastList *head = NULL, *cur_item = NULL;
Laszlo Ersekdaa9d2b2018-04-27 21:28:51 +02002304 SysEmuTarget target = qapi_enum_parse(&SysEmuTarget_lookup, TARGET_NAME,
2305 -1, &error_abort);
Luiz Capitulinoce74ee32018-02-16 17:08:38 +01002306 CPUState *cpu;
2307
2308 CPU_FOREACH(cpu) {
2309 CpuInfoFastList *info = g_malloc0(sizeof(*info));
2310 info->value = g_malloc0(sizeof(*info->value));
2311
2312 info->value->cpu_index = cpu->cpu_index;
2313 info->value->qom_path = object_get_canonical_path(OBJECT(cpu));
2314 info->value->thread_id = cpu->thread_id;
2315
2316 info->value->has_props = !!mc->cpu_index_to_instance_props;
2317 if (info->value->has_props) {
2318 CpuInstanceProperties *props;
2319 props = g_malloc0(sizeof(*props));
2320 *props = mc->cpu_index_to_instance_props(ms, cpu->cpu_index);
2321 info->value->props = props;
2322 }
2323
Laszlo Ersekdaa9d2b2018-04-27 21:28:51 +02002324 info->value->arch = sysemu_target_to_cpuinfo_arch(target);
2325 info->value->target = target;
2326 if (target == SYS_EMU_TARGET_S390X) {
2327 cpustate_to_cpuinfo_s390(&info->value->u.s390x, cpu);
Laszlo Ersekdaa9d2b2018-04-27 21:28:51 +02002328 }
2329
Luiz Capitulinoce74ee32018-02-16 17:08:38 +01002330 if (!cur_item) {
2331 head = cur_item = info;
2332 } else {
2333 cur_item->next = info;
2334 cur_item = info;
2335 }
2336 }
2337
2338 return head;
2339}
2340
Luiz Capitulino0cfd6a92011-11-22 16:32:37 -02002341void qmp_memsave(int64_t addr, int64_t size, const char *filename,
2342 bool has_cpu, int64_t cpu_index, Error **errp)
2343{
2344 FILE *f;
2345 uint32_t l;
Andreas Färber55e5c282012-12-17 06:18:02 +01002346 CPUState *cpu;
Luiz Capitulino0cfd6a92011-11-22 16:32:37 -02002347 uint8_t buf[1024];
Borislav Petkov0dc9daf2015-02-08 13:14:38 +01002348 int64_t orig_addr = addr, orig_size = size;
Luiz Capitulino0cfd6a92011-11-22 16:32:37 -02002349
2350 if (!has_cpu) {
2351 cpu_index = 0;
2352 }
2353
Andreas Färber151d1322013-02-15 15:41:49 +01002354 cpu = qemu_get_cpu(cpu_index);
2355 if (cpu == NULL) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01002356 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "cpu-index",
2357 "a CPU number");
Luiz Capitulino0cfd6a92011-11-22 16:32:37 -02002358 return;
2359 }
2360
2361 f = fopen(filename, "wb");
2362 if (!f) {
Luiz Capitulino618da852013-06-07 14:35:06 -04002363 error_setg_file_open(errp, errno, filename);
Luiz Capitulino0cfd6a92011-11-22 16:32:37 -02002364 return;
2365 }
2366
2367 while (size != 0) {
2368 l = sizeof(buf);
2369 if (l > size)
2370 l = size;
Aneesh Kumar K.V2f4d0f52013-10-01 21:49:30 +05302371 if (cpu_memory_rw_debug(cpu, addr, buf, l, 0) != 0) {
Borislav Petkov0dc9daf2015-02-08 13:14:38 +01002372 error_setg(errp, "Invalid addr 0x%016" PRIx64 "/size %" PRId64
2373 " specified", orig_addr, orig_size);
Aneesh Kumar K.V2f4d0f52013-10-01 21:49:30 +05302374 goto exit;
2375 }
Luiz Capitulino0cfd6a92011-11-22 16:32:37 -02002376 if (fwrite(buf, 1, l, f) != l) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01002377 error_setg(errp, QERR_IO_ERROR);
Luiz Capitulino0cfd6a92011-11-22 16:32:37 -02002378 goto exit;
2379 }
2380 addr += l;
2381 size -= l;
2382 }
2383
2384exit:
2385 fclose(f);
2386}
Luiz Capitulino6d3962b2011-11-22 17:26:46 -02002387
2388void qmp_pmemsave(int64_t addr, int64_t size, const char *filename,
2389 Error **errp)
2390{
2391 FILE *f;
2392 uint32_t l;
2393 uint8_t buf[1024];
2394
2395 f = fopen(filename, "wb");
2396 if (!f) {
Luiz Capitulino618da852013-06-07 14:35:06 -04002397 error_setg_file_open(errp, errno, filename);
Luiz Capitulino6d3962b2011-11-22 17:26:46 -02002398 return;
2399 }
2400
2401 while (size != 0) {
2402 l = sizeof(buf);
2403 if (l > size)
2404 l = size;
Stefan Weileb6282f2014-04-07 20:28:23 +02002405 cpu_physical_memory_read(addr, buf, l);
Luiz Capitulino6d3962b2011-11-22 17:26:46 -02002406 if (fwrite(buf, 1, l, f) != l) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01002407 error_setg(errp, QERR_IO_ERROR);
Luiz Capitulino6d3962b2011-11-22 17:26:46 -02002408 goto exit;
2409 }
2410 addr += l;
2411 size -= l;
2412 }
2413
2414exit:
2415 fclose(f);
2416}
Luiz Capitulinoab49ab52011-11-23 12:55:53 -02002417
2418void qmp_inject_nmi(Error **errp)
2419{
Alexey Kardashevskiy9cb805f2014-08-20 22:16:33 +10002420 nmi_monitor_handle(monitor_get_cpu_index(), errp);
Luiz Capitulinoab49ab52011-11-23 12:55:53 -02002421}
Sebastian Tanase27498be2014-07-25 11:56:33 +02002422
2423void dump_drift_info(FILE *f, fprintf_function cpu_fprintf)
2424{
2425 if (!use_icount) {
2426 return;
2427 }
2428
2429 cpu_fprintf(f, "Host - Guest clock %"PRIi64" ms\n",
2430 (cpu_get_clock() - cpu_get_icount())/SCALE_MS);
2431 if (icount_align_option) {
2432 cpu_fprintf(f, "Max guest delay %"PRIi64" ms\n", -max_delay/SCALE_MS);
2433 cpu_fprintf(f, "Max guest advance %"PRIi64" ms\n", max_advance/SCALE_MS);
2434 } else {
2435 cpu_fprintf(f, "Max guest delay NA\n");
2436 cpu_fprintf(f, "Max guest advance NA\n");
2437 }
2438}