blob: 071085f840b02ce35327031386aec911639f2de6 [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"
Markus Armbrustera8d25322019-05-23 16:35:08 +020026#include "qemu-common.h"
Paolo Bonzini83c90892012-12-17 18:19:49 +010027#include "monitor/monitor.h"
Markus Armbrustere688df62018-02-01 12:18:31 +010028#include "qapi/error.h"
Philippe Mathieu-Daudédf7a1f482020-10-12 14:15:32 +020029#include "qapi/qapi-commands-machine.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"
Paolo Bonzini022c62c2012-12-17 18:19:49 +010033#include "exec/gdbstub.h"
Vincent Palatinb3946622017-01-10 11:59:55 +010034#include "sysemu/hw_accel.h"
Paolo Bonzini63c91552016-03-15 13:18:37 +010035#include "exec/exec-all.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010036#include "qemu/thread.h"
Emilio G. Cota30865f32018-10-21 13:30:35 -040037#include "qemu/plugin.h"
Paolo Bonzini9c17d612012-12-17 18:20:04 +010038#include "sysemu/cpus.h"
Richard Henderson9c09a252019-03-14 13:06:29 -070039#include "qemu/guest-random.h"
Alexey Kardashevskiy9cb805f2014-08-20 22:16:33 +100040#include "hw/nmi.h"
Pavel Dovgalyuk8b427042015-09-17 19:24:05 +030041#include "sysemu/replay.h"
Markus Armbruster54d31232019-08-12 07:23:59 +020042#include "sysemu/runstate.h"
Claudio Fontana740b1752020-08-19 13:17:19 +020043#include "sysemu/cpu-timers.h"
Sunil Muthuswamyfaf20792020-10-28 02:23:19 +000044#include "sysemu/whpx.h"
Like Xu5cc87672019-05-19 04:54:21 +080045#include "hw/boards.h"
Markus Armbruster650d1032019-08-12 07:23:48 +020046#include "hw/hw.h"
Daniel P. Berrangé8af3f5c2021-04-15 14:33:51 +010047#include "trace.h"
Jan Kiszka0ff0fc12011-06-23 10:15:55 +020048
Jan Kiszka6d9cb732011-02-01 22:15:58 +010049#ifdef CONFIG_LINUX
50
51#include <sys/prctl.h>
52
Marcelo Tosattic0532a72010-10-11 15:31:21 -030053#ifndef PR_MCE_KILL
54#define PR_MCE_KILL 33
55#endif
56
Jan Kiszka6d9cb732011-02-01 22:15:58 +010057#ifndef PR_MCE_KILL_SET
58#define PR_MCE_KILL_SET 1
59#endif
60
61#ifndef PR_MCE_KILL_EARLY
62#define PR_MCE_KILL_EARLY 1
63#endif
64
65#endif /* CONFIG_LINUX */
66
Yury Kotovbd1f7ff2019-09-09 16:13:34 +030067static QemuMutex qemu_global_mutex;
68
Tiejun Chen321bc0b2013-08-02 09:43:09 +080069bool cpu_is_stopped(CPUState *cpu)
70{
71 return cpu->stopped || !runstate_is_running();
72}
73
Claudio Fontana430065d2020-07-31 12:23:42 +020074bool cpu_work_list_empty(CPUState *cpu)
Emilio G. Cota0c0fcc22020-06-12 20:02:24 +010075{
76 bool ret;
77
78 qemu_mutex_lock(&cpu->work_mutex);
79 ret = QSIMPLEQ_EMPTY(&cpu->work_list);
80 qemu_mutex_unlock(&cpu->work_mutex);
81 return ret;
82}
83
Claudio Fontana430065d2020-07-31 12:23:42 +020084bool cpu_thread_is_idle(CPUState *cpu)
Peter Maydellac873f12012-07-19 16:52:27 +010085{
Emilio G. Cota0c0fcc22020-06-12 20:02:24 +010086 if (cpu->stop || !cpu_work_list_empty(cpu)) {
Peter Maydellac873f12012-07-19 16:52:27 +010087 return false;
88 }
Tiejun Chen321bc0b2013-08-02 09:43:09 +080089 if (cpu_is_stopped(cpu)) {
Peter Maydellac873f12012-07-19 16:52:27 +010090 return true;
91 }
Andreas Färber8c2e1b02013-08-25 18:53:55 +020092 if (!cpu->halted || cpu_has_work(cpu) ||
Sunil Muthuswamyfaf20792020-10-28 02:23:19 +000093 kvm_halt_in_kernel() || whpx_apic_in_platform()) {
Peter Maydellac873f12012-07-19 16:52:27 +010094 return false;
95 }
96 return true;
97}
98
Claudio Fontana740b1752020-08-19 13:17:19 +020099bool all_cpu_threads_idle(void)
Peter Maydellac873f12012-07-19 16:52:27 +0100100{
Andreas Färber182735e2013-05-29 22:29:20 +0200101 CPUState *cpu;
Peter Maydellac873f12012-07-19 16:52:27 +0100102
Andreas Färberbdc44642013-06-24 23:50:24 +0200103 CPU_FOREACH(cpu) {
Andreas Färber182735e2013-05-29 22:29:20 +0200104 if (!cpu_thread_is_idle(cpu)) {
Peter Maydellac873f12012-07-19 16:52:27 +0100105 return false;
106 }
107 }
108 return true;
109}
110
Alex Bennée65467062017-02-23 18:29:09 +0000111/***********************************************************/
Blue Swirl296af7c2010-03-29 19:23:50 +0000112void hw_error(const char *fmt, ...)
113{
114 va_list ap;
Andreas Färber55e5c282012-12-17 06:18:02 +0100115 CPUState *cpu;
Blue Swirl296af7c2010-03-29 19:23:50 +0000116
117 va_start(ap, fmt);
118 fprintf(stderr, "qemu: hardware error: ");
119 vfprintf(stderr, fmt, ap);
120 fprintf(stderr, "\n");
Andreas Färberbdc44642013-06-24 23:50:24 +0200121 CPU_FOREACH(cpu) {
Andreas Färber55e5c282012-12-17 06:18:02 +0100122 fprintf(stderr, "CPU #%d:\n", cpu->cpu_index);
Markus Armbruster90c84c52019-04-17 21:18:02 +0200123 cpu_dump_state(cpu, stderr, CPU_DUMP_FPU);
Blue Swirl296af7c2010-03-29 19:23:50 +0000124 }
125 va_end(ap);
126 abort();
127}
128
Claudio Fontana430065d2020-07-31 12:23:42 +0200129/*
130 * The chosen accelerator is supposed to register this.
131 */
Claudio Fontanab86f59c2021-02-04 17:39:25 +0100132static const AccelOpsClass *cpus_accel;
Claudio Fontana430065d2020-07-31 12:23:42 +0200133
Blue Swirl296af7c2010-03-29 19:23:50 +0000134void cpu_synchronize_all_states(void)
135{
Andreas Färber182735e2013-05-29 22:29:20 +0200136 CPUState *cpu;
Blue Swirl296af7c2010-03-29 19:23:50 +0000137
Andreas Färberbdc44642013-06-24 23:50:24 +0200138 CPU_FOREACH(cpu) {
Andreas Färber182735e2013-05-29 22:29:20 +0200139 cpu_synchronize_state(cpu);
Blue Swirl296af7c2010-03-29 19:23:50 +0000140 }
141}
142
143void cpu_synchronize_all_post_reset(void)
144{
Andreas Färber182735e2013-05-29 22:29:20 +0200145 CPUState *cpu;
Blue Swirl296af7c2010-03-29 19:23:50 +0000146
Andreas Färberbdc44642013-06-24 23:50:24 +0200147 CPU_FOREACH(cpu) {
Andreas Färber182735e2013-05-29 22:29:20 +0200148 cpu_synchronize_post_reset(cpu);
Blue Swirl296af7c2010-03-29 19:23:50 +0000149 }
150}
151
152void cpu_synchronize_all_post_init(void)
153{
Andreas Färber182735e2013-05-29 22:29:20 +0200154 CPUState *cpu;
Blue Swirl296af7c2010-03-29 19:23:50 +0000155
Andreas Färberbdc44642013-06-24 23:50:24 +0200156 CPU_FOREACH(cpu) {
Andreas Färber182735e2013-05-29 22:29:20 +0200157 cpu_synchronize_post_init(cpu);
Blue Swirl296af7c2010-03-29 19:23:50 +0000158 }
159}
160
David Gibson75e972d2017-05-26 14:46:28 +1000161void cpu_synchronize_all_pre_loadvm(void)
162{
163 CPUState *cpu;
164
165 CPU_FOREACH(cpu) {
166 cpu_synchronize_pre_loadvm(cpu);
167 }
168}
169
Claudio Fontana430065d2020-07-31 12:23:42 +0200170void cpu_synchronize_state(CPUState *cpu)
171{
Claudio Fontana994aa172020-08-19 16:01:03 +0200172 if (cpus_accel->synchronize_state) {
Claudio Fontana430065d2020-07-31 12:23:42 +0200173 cpus_accel->synchronize_state(cpu);
174 }
Claudio Fontana430065d2020-07-31 12:23:42 +0200175}
176
177void cpu_synchronize_post_reset(CPUState *cpu)
178{
Claudio Fontana994aa172020-08-19 16:01:03 +0200179 if (cpus_accel->synchronize_post_reset) {
Claudio Fontana430065d2020-07-31 12:23:42 +0200180 cpus_accel->synchronize_post_reset(cpu);
181 }
Claudio Fontana430065d2020-07-31 12:23:42 +0200182}
183
184void cpu_synchronize_post_init(CPUState *cpu)
185{
Claudio Fontana994aa172020-08-19 16:01:03 +0200186 if (cpus_accel->synchronize_post_init) {
Claudio Fontana430065d2020-07-31 12:23:42 +0200187 cpus_accel->synchronize_post_init(cpu);
188 }
Claudio Fontana430065d2020-07-31 12:23:42 +0200189}
190
191void cpu_synchronize_pre_loadvm(CPUState *cpu)
192{
Claudio Fontana994aa172020-08-19 16:01:03 +0200193 if (cpus_accel->synchronize_pre_loadvm) {
Claudio Fontana430065d2020-07-31 12:23:42 +0200194 cpus_accel->synchronize_pre_loadvm(cpu);
195 }
Claudio Fontana430065d2020-07-31 12:23:42 +0200196}
197
Tom Lendacky92a51992021-01-26 11:36:47 -0600198bool cpus_are_resettable(void)
199{
200 return cpu_check_are_resettable();
201}
202
Claudio Fontana430065d2020-07-31 12:23:42 +0200203int64_t cpus_get_virtual_clock(void)
204{
Claudio Fontana994aa172020-08-19 16:01:03 +0200205 /*
206 * XXX
207 *
208 * need to check that cpus_accel is not NULL, because qcow2 calls
209 * qemu_get_clock_ns(CLOCK_VIRTUAL) without any accel initialized and
210 * with ticks disabled in some io-tests:
211 * 030 040 041 060 099 120 127 140 156 161 172 181 191 192 195 203 229 249 256 267
212 *
213 * is this expected?
214 *
215 * XXX
216 */
Claudio Fontana430065d2020-07-31 12:23:42 +0200217 if (cpus_accel && cpus_accel->get_virtual_clock) {
218 return cpus_accel->get_virtual_clock();
219 }
Claudio Fontana430065d2020-07-31 12:23:42 +0200220 return cpu_get_clock();
221}
222
223/*
224 * return the time elapsed in VM between vm_start and vm_stop. Unless
225 * icount is active, cpus_get_elapsed_ticks() uses units of the host CPU cycle
226 * counter.
227 */
228int64_t cpus_get_elapsed_ticks(void)
229{
Claudio Fontana994aa172020-08-19 16:01:03 +0200230 if (cpus_accel->get_elapsed_ticks) {
Claudio Fontana430065d2020-07-31 12:23:42 +0200231 return cpus_accel->get_elapsed_ticks();
232 }
Claudio Fontana430065d2020-07-31 12:23:42 +0200233 return cpu_get_ticks();
234}
235
Claudio Fontanabb4776b2020-08-11 15:16:33 +0200236static void generic_handle_interrupt(CPUState *cpu, int mask)
237{
238 cpu->interrupt_request |= mask;
239
240 if (!qemu_cpu_is_self(cpu)) {
241 qemu_cpu_kick(cpu);
242 }
243}
244
245void cpu_interrupt(CPUState *cpu, int mask)
246{
247 if (cpus_accel->handle_interrupt) {
248 cpus_accel->handle_interrupt(cpu, mask);
249 } else {
250 generic_handle_interrupt(cpu, mask);
251 }
252}
253
Stefan Hajnoczi4486e892018-03-07 14:42:05 +0000254static int do_vm_stop(RunState state, bool send_stop)
Blue Swirl296af7c2010-03-29 19:23:50 +0000255{
Kevin Wolf56983462013-07-05 13:49:54 +0200256 int ret = 0;
257
Luiz Capitulino13548692011-07-29 15:36:43 -0300258 if (runstate_is_running()) {
Longpengf962cac2020-03-16 16:37:32 +0800259 runstate_set(state);
Blue Swirl296af7c2010-03-29 19:23:50 +0000260 cpu_disable_ticks();
Blue Swirl296af7c2010-03-29 19:23:50 +0000261 pause_all_vcpus();
Luiz Capitulino1dfb4dd2011-07-29 14:26:33 -0300262 vm_state_notify(0, state);
Stefan Hajnoczi4486e892018-03-07 14:42:05 +0000263 if (send_stop) {
Peter Xu3ab72382018-08-15 21:37:37 +0800264 qapi_event_send_stop();
Stefan Hajnoczi4486e892018-03-07 14:42:05 +0000265 }
Blue Swirl296af7c2010-03-29 19:23:50 +0000266 }
Kevin Wolf56983462013-07-05 13:49:54 +0200267
Kevin Wolf594a45c2013-07-18 14:52:19 +0200268 bdrv_drain_all();
John Snow22af08e2016-09-22 21:45:51 -0400269 ret = bdrv_flush_all();
Daniel P. Berrangé8af3f5c2021-04-15 14:33:51 +0100270 trace_vm_stop_flush_all(ret);
Kevin Wolf594a45c2013-07-18 14:52:19 +0200271
Kevin Wolf56983462013-07-05 13:49:54 +0200272 return ret;
Blue Swirl296af7c2010-03-29 19:23:50 +0000273}
274
Stefan Hajnoczi4486e892018-03-07 14:42:05 +0000275/* Special vm_stop() variant for terminating the process. Historically clients
276 * did not expect a QMP STOP event and so we need to retain compatibility.
277 */
278int vm_shutdown(void)
279{
280 return do_vm_stop(RUN_STATE_SHUTDOWN, false);
281}
282
Claudio Fontana430065d2020-07-31 12:23:42 +0200283bool cpu_can_run(CPUState *cpu)
Blue Swirl296af7c2010-03-29 19:23:50 +0000284{
Andreas Färber4fdeee72012-05-02 23:10:09 +0200285 if (cpu->stop) {
Andreas Färbera1fcaa72012-05-02 23:42:26 +0200286 return false;
Jan Kiszka0ab07c62011-02-07 12:19:14 +0100287 }
Tiejun Chen321bc0b2013-08-02 09:43:09 +0800288 if (cpu_is_stopped(cpu)) {
Andreas Färbera1fcaa72012-05-02 23:42:26 +0200289 return false;
Jan Kiszka0ab07c62011-02-07 12:19:14 +0100290 }
Andreas Färbera1fcaa72012-05-02 23:42:26 +0200291 return true;
Blue Swirl296af7c2010-03-29 19:23:50 +0000292}
293
Claudio Fontana430065d2020-07-31 12:23:42 +0200294void cpu_handle_guest_debug(CPUState *cpu)
Jan Kiszka3c638d02010-06-25 16:56:56 +0200295{
Pavel Dovgalyukfda84582020-10-03 20:13:43 +0300296 if (replay_running_debug()) {
297 if (!cpu->singlestep_enabled) {
Pavel Dovgalyukcda38252020-10-03 20:13:49 +0300298 /*
299 * Report about the breakpoint and
300 * make a single step to skip it
301 */
302 replay_breakpoint();
Pavel Dovgalyukfda84582020-10-03 20:13:43 +0300303 cpu_single_step(cpu, SSTEP_ENABLE);
304 } else {
305 cpu_single_step(cpu, 0);
306 }
307 } else {
308 gdb_set_stop_cpu(cpu);
309 qemu_system_debug_request();
310 cpu->stopped = true;
311 }
Jan Kiszka3c638d02010-06-25 16:56:56 +0200312}
313
Jan Kiszka6d9cb732011-02-01 22:15:58 +0100314#ifdef CONFIG_LINUX
315static void sigbus_reraise(void)
316{
317 sigset_t set;
318 struct sigaction action;
319
320 memset(&action, 0, sizeof(action));
321 action.sa_handler = SIG_DFL;
322 if (!sigaction(SIGBUS, &action, NULL)) {
323 raise(SIGBUS);
324 sigemptyset(&set);
325 sigaddset(&set, SIGBUS);
Peter Maydella2d17612016-05-16 18:33:59 +0100326 pthread_sigmask(SIG_UNBLOCK, &set, NULL);
Jan Kiszka6d9cb732011-02-01 22:15:58 +0100327 }
Li Zhijianeb1960a2021-07-06 17:44:33 +0800328 perror("Failed to re-raise SIGBUS!");
Jan Kiszka6d9cb732011-02-01 22:15:58 +0100329 abort();
330}
331
Paolo Bonzinid98d4072017-02-08 13:22:12 +0100332static void sigbus_handler(int n, siginfo_t *siginfo, void *ctx)
Jan Kiszka6d9cb732011-02-01 22:15:58 +0100333{
Paolo Bonzinia16fc072017-02-09 09:50:02 +0100334 if (siginfo->si_code != BUS_MCEERR_AO && siginfo->si_code != BUS_MCEERR_AR) {
335 sigbus_reraise();
336 }
337
Paolo Bonzini2ae41db2017-02-08 12:48:54 +0100338 if (current_cpu) {
339 /* Called asynchronously in VCPU thread. */
340 if (kvm_on_sigbus_vcpu(current_cpu, siginfo->si_code, siginfo->si_addr)) {
341 sigbus_reraise();
342 }
343 } else {
344 /* Called synchronously (via signalfd) in main thread. */
345 if (kvm_on_sigbus(siginfo->si_code, siginfo->si_addr)) {
346 sigbus_reraise();
347 }
Jan Kiszka6d9cb732011-02-01 22:15:58 +0100348 }
349}
350
351static void qemu_init_sigbus(void)
352{
353 struct sigaction action;
354
355 memset(&action, 0, sizeof(action));
356 action.sa_flags = SA_SIGINFO;
Paolo Bonzinid98d4072017-02-08 13:22:12 +0100357 action.sa_sigaction = sigbus_handler;
Jan Kiszka6d9cb732011-02-01 22:15:58 +0100358 sigaction(SIGBUS, &action, NULL);
359
360 prctl(PR_MCE_KILL, PR_MCE_KILL_SET, PR_MCE_KILL_EARLY, 0, 0);
361}
Paolo Bonzinia16fc072017-02-09 09:50:02 +0100362#else /* !CONFIG_LINUX */
363static void qemu_init_sigbus(void)
364{
365}
Paolo Bonzinia16fc072017-02-09 09:50:02 +0100366#endif /* !CONFIG_LINUX */
Blue Swirl296af7c2010-03-29 19:23:50 +0000367
Blue Swirl296af7c2010-03-29 19:23:50 +0000368static QemuThread io_thread;
369
Blue Swirl296af7c2010-03-29 19:23:50 +0000370/* cpu creation */
371static QemuCond qemu_cpu_cond;
372/* system init */
Blue Swirl296af7c2010-03-29 19:23:50 +0000373static QemuCond qemu_pause_cond;
374
Paolo Bonzinid3b12f52011-09-13 10:30:52 +0200375void qemu_init_cpu_loop(void)
Blue Swirl296af7c2010-03-29 19:23:50 +0000376{
Jan Kiszka6d9cb732011-02-01 22:15:58 +0100377 qemu_init_sigbus();
Anthony Liguoried945922011-02-08 18:18:18 +0100378 qemu_cond_init(&qemu_cpu_cond);
Anthony Liguoried945922011-02-08 18:18:18 +0100379 qemu_cond_init(&qemu_pause_cond);
Blue Swirl296af7c2010-03-29 19:23:50 +0000380 qemu_mutex_init(&qemu_global_mutex);
Blue Swirl296af7c2010-03-29 19:23:50 +0000381
Jan Kiszkab7680cb2011-03-12 17:43:51 +0100382 qemu_thread_get_self(&io_thread);
Blue Swirl296af7c2010-03-29 19:23:50 +0000383}
384
Paolo Bonzini14e6fe12016-10-31 10:36:08 +0100385void run_on_cpu(CPUState *cpu, run_on_cpu_func func, run_on_cpu_data data)
Marcelo Tosattie82bcec2010-05-04 09:45:22 -0300386{
Sergey Fedorovd148d902016-08-29 09:51:00 +0200387 do_run_on_cpu(cpu, func, data, &qemu_global_mutex);
Chegu Vinod3c022702013-06-24 03:49:41 -0600388}
389
David Hildenbrandebd05fe2017-11-29 20:12:15 +0100390static void qemu_cpu_stop(CPUState *cpu, bool exit)
391{
392 g_assert(qemu_cpu_is_self(cpu));
393 cpu->stop = false;
394 cpu->stopped = true;
395 if (exit) {
396 cpu_exit(cpu);
397 }
398 qemu_cond_broadcast(&qemu_pause_cond);
399}
400
Claudio Fontana430065d2020-07-31 12:23:42 +0200401void qemu_wait_io_event_common(CPUState *cpu)
Blue Swirl296af7c2010-03-29 19:23:50 +0000402{
Stefan Hajnoczid73415a2020-09-23 11:56:46 +0100403 qatomic_mb_set(&cpu->thread_kicked, false);
Andreas Färber4fdeee72012-05-02 23:10:09 +0200404 if (cpu->stop) {
David Hildenbrandebd05fe2017-11-29 20:12:15 +0100405 qemu_cpu_stop(cpu, false);
Blue Swirl296af7c2010-03-29 19:23:50 +0000406 }
Sergey Fedorova5403c62016-08-02 18:27:36 +0100407 process_queued_cpu_work(cpu);
Alex Bennée37257942017-02-23 18:29:14 +0000408}
409
Claudio Fontana430065d2020-07-31 12:23:42 +0200410void qemu_wait_io_event(CPUState *cpu)
Blue Swirl296af7c2010-03-29 19:23:50 +0000411{
Emilio G. Cota30865f32018-10-21 13:30:35 -0400412 bool slept = false;
413
Andreas Färbera98ae1d2013-05-26 23:21:08 +0200414 while (cpu_thread_is_idle(cpu)) {
Emilio G. Cota30865f32018-10-21 13:30:35 -0400415 if (!slept) {
416 slept = true;
417 qemu_plugin_vcpu_idle_cb(cpu);
418 }
Andreas Färberf5c121b2012-05-03 01:22:49 +0200419 qemu_cond_wait(cpu->halt_cond, &qemu_global_mutex);
Jan Kiszka16400322011-02-09 16:29:37 +0100420 }
Emilio G. Cota30865f32018-10-21 13:30:35 -0400421 if (slept) {
422 qemu_plugin_vcpu_resume_cb(cpu);
423 }
Blue Swirl296af7c2010-03-29 19:23:50 +0000424
Paolo Bonzinidb08b682018-01-11 13:53:12 +0100425#ifdef _WIN32
Claudio Fontana430065d2020-07-31 12:23:42 +0200426 /* Eat dummy APC queued by cpus_kick_thread. */
427 if (hax_enabled()) {
Paolo Bonzinidb08b682018-01-11 13:53:12 +0100428 SleepEx(0, TRUE);
Sergio Andres Gomez Del Realc97d6d22017-09-13 04:05:09 -0500429 }
Paolo Bonzinidb08b682018-01-11 13:53:12 +0100430#endif
Sergio Andres Gomez Del Realc97d6d22017-09-13 04:05:09 -0500431 qemu_wait_io_event_common(cpu);
432}
433
Claudio Fontana430065d2020-07-31 12:23:42 +0200434void cpus_kick_thread(CPUState *cpu)
Paolo Bonzinicc015e92011-03-12 17:44:08 +0100435{
436#ifndef _WIN32
437 int err;
438
Paolo Bonzinie0c38212015-08-26 00:19:19 +0200439 if (cpu->thread_kicked) {
440 return;
Paolo Bonzini9102ded2015-08-18 06:52:09 -0700441 }
Paolo Bonzinie0c38212015-08-26 00:19:19 +0200442 cpu->thread_kicked = true;
Andreas Färber814e6122012-05-02 17:00:37 +0200443 err = pthread_kill(cpu->thread->thread, SIG_IPI);
Laurent Vivierd455ebc2019-01-02 15:16:03 +0100444 if (err && err != ESRCH) {
Paolo Bonzinicc015e92011-03-12 17:44:08 +0100445 fprintf(stderr, "qemu:%s: %s", __func__, strerror(err));
446 exit(1);
447 }
Paolo Bonzinicc015e92011-03-12 17:44:08 +0100448#endif
449}
450
Andreas Färberc08d7422012-05-03 04:34:15 +0200451void qemu_cpu_kick(CPUState *cpu)
Blue Swirl296af7c2010-03-29 19:23:50 +0000452{
Andreas Färberf5c121b2012-05-03 01:22:49 +0200453 qemu_cond_broadcast(cpu->halt_cond);
Claudio Fontana994aa172020-08-19 16:01:03 +0200454 if (cpus_accel->kick_vcpu_thread) {
Claudio Fontana430065d2020-07-31 12:23:42 +0200455 cpus_accel->kick_vcpu_thread(cpu);
Claudio Fontanae92558e2020-07-07 11:18:49 +0200456 } else { /* default */
Claudio Fontana430065d2020-07-31 12:23:42 +0200457 cpus_kick_thread(cpu);
Paolo Bonzinie0c38212015-08-26 00:19:19 +0200458 }
Blue Swirl296af7c2010-03-29 19:23:50 +0000459}
460
Jan Kiszka46d62fa2011-02-01 22:15:59 +0100461void qemu_cpu_kick_self(void)
462{
Andreas Färber4917cf42013-05-27 05:17:50 +0200463 assert(current_cpu);
Claudio Fontana430065d2020-07-31 12:23:42 +0200464 cpus_kick_thread(current_cpu);
Blue Swirl296af7c2010-03-29 19:23:50 +0000465}
466
Andreas Färber60e82572012-05-02 22:23:49 +0200467bool qemu_cpu_is_self(CPUState *cpu)
Blue Swirl296af7c2010-03-29 19:23:50 +0000468{
Andreas Färber814e6122012-05-02 17:00:37 +0200469 return qemu_thread_is_self(cpu->thread);
Blue Swirl296af7c2010-03-29 19:23:50 +0000470}
471
Paolo Bonzini79e2b9a2015-01-21 12:09:14 +0100472bool qemu_in_vcpu_thread(void)
Juan Quintelaaa723c22012-09-18 16:30:11 +0200473{
Andreas Färber4917cf42013-05-27 05:17:50 +0200474 return current_cpu && qemu_cpu_is_self(current_cpu);
Juan Quintelaaa723c22012-09-18 16:30:11 +0200475}
476
Paolo Bonziniafbe7052015-06-18 18:47:19 +0200477static __thread bool iothread_locked = false;
478
479bool qemu_mutex_iothread_locked(void)
480{
481 return iothread_locked;
482}
483
Emilio G. Cotacb764d02017-10-28 02:16:41 -0400484/*
485 * The BQL is taken from so many places that it is worth profiling the
486 * callers directly, instead of funneling them all through a single function.
487 */
488void qemu_mutex_lock_iothread_impl(const char *file, int line)
Blue Swirl296af7c2010-03-29 19:23:50 +0000489{
Stefan Hajnoczid73415a2020-09-23 11:56:46 +0100490 QemuMutexLockFunc bql_lock = qatomic_read(&qemu_bql_mutex_lock_func);
Emilio G. Cotacb764d02017-10-28 02:16:41 -0400491
Jan Kiszka8d04fb52017-02-23 18:29:11 +0000492 g_assert(!qemu_mutex_iothread_locked());
Emilio G. Cotacb764d02017-10-28 02:16:41 -0400493 bql_lock(&qemu_global_mutex, file, line);
Paolo Bonziniafbe7052015-06-18 18:47:19 +0200494 iothread_locked = true;
Blue Swirl296af7c2010-03-29 19:23:50 +0000495}
496
497void qemu_mutex_unlock_iothread(void)
498{
Jan Kiszka8d04fb52017-02-23 18:29:11 +0000499 g_assert(qemu_mutex_iothread_locked());
Paolo Bonziniafbe7052015-06-18 18:47:19 +0200500 iothread_locked = false;
Blue Swirl296af7c2010-03-29 19:23:50 +0000501 qemu_mutex_unlock(&qemu_global_mutex);
502}
503
Aravinda Prasad19e067e2020-01-31 00:14:17 +0530504void qemu_cond_wait_iothread(QemuCond *cond)
505{
506 qemu_cond_wait(cond, &qemu_global_mutex);
507}
508
Claudio Fontanab0c3cf92020-06-29 11:35:03 +0200509void qemu_cond_timedwait_iothread(QemuCond *cond, int ms)
510{
511 qemu_cond_timedwait(cond, &qemu_global_mutex, ms);
512}
513
Claudio Fontana430065d2020-07-31 12:23:42 +0200514/* signal CPU creation */
515void cpu_thread_signal_created(CPUState *cpu)
516{
517 cpu->created = true;
518 qemu_cond_signal(&qemu_cpu_cond);
519}
520
521/* signal CPU destruction */
522void cpu_thread_signal_destroyed(CPUState *cpu)
523{
524 cpu->created = false;
525 qemu_cond_signal(&qemu_cpu_cond);
526}
527
528
Alex Bennéee8faee02016-10-27 16:09:58 +0100529static bool all_vcpus_paused(void)
Blue Swirl296af7c2010-03-29 19:23:50 +0000530{
Andreas Färberbdc44642013-06-24 23:50:24 +0200531 CPUState *cpu;
Blue Swirl296af7c2010-03-29 19:23:50 +0000532
Andreas Färberbdc44642013-06-24 23:50:24 +0200533 CPU_FOREACH(cpu) {
Andreas Färber182735e2013-05-29 22:29:20 +0200534 if (!cpu->stopped) {
Alex Bennéee8faee02016-10-27 16:09:58 +0100535 return false;
Jan Kiszka0ab07c62011-02-07 12:19:14 +0100536 }
Blue Swirl296af7c2010-03-29 19:23:50 +0000537 }
538
Alex Bennéee8faee02016-10-27 16:09:58 +0100539 return true;
Blue Swirl296af7c2010-03-29 19:23:50 +0000540}
541
542void pause_all_vcpus(void)
543{
Andreas Färberbdc44642013-06-24 23:50:24 +0200544 CPUState *cpu;
Blue Swirl296af7c2010-03-29 19:23:50 +0000545
Alex Bligh40daca52013-08-21 16:03:02 +0100546 qemu_clock_enable(QEMU_CLOCK_VIRTUAL, false);
Andreas Färberbdc44642013-06-24 23:50:24 +0200547 CPU_FOREACH(cpu) {
David Hildenbrandebd05fe2017-11-29 20:12:15 +0100548 if (qemu_cpu_is_self(cpu)) {
549 qemu_cpu_stop(cpu, true);
550 } else {
551 cpu->stop = true;
552 qemu_cpu_kick(cpu);
553 }
Jan Kiszkad798e972012-02-17 18:31:16 +0100554 }
555
Alex Bennéed759c952018-02-27 12:52:48 +0300556 /* We need to drop the replay_lock so any vCPU threads woken up
557 * can finish their replay tasks
558 */
559 replay_mutex_unlock();
560
Blue Swirl296af7c2010-03-29 19:23:50 +0000561 while (!all_vcpus_paused()) {
Paolo Bonzinibe7d6c52011-03-12 17:44:02 +0100562 qemu_cond_wait(&qemu_pause_cond, &qemu_global_mutex);
Andreas Färberbdc44642013-06-24 23:50:24 +0200563 CPU_FOREACH(cpu) {
Andreas Färber182735e2013-05-29 22:29:20 +0200564 qemu_cpu_kick(cpu);
Blue Swirl296af7c2010-03-29 19:23:50 +0000565 }
566 }
Alex Bennéed759c952018-02-27 12:52:48 +0300567
568 qemu_mutex_unlock_iothread();
569 replay_mutex_lock();
570 qemu_mutex_lock_iothread();
Blue Swirl296af7c2010-03-29 19:23:50 +0000571}
572
Igor Mammedov29936832013-04-23 10:29:37 +0200573void cpu_resume(CPUState *cpu)
574{
575 cpu->stop = false;
576 cpu->stopped = false;
577 qemu_cpu_kick(cpu);
578}
579
Blue Swirl296af7c2010-03-29 19:23:50 +0000580void resume_all_vcpus(void)
581{
Andreas Färberbdc44642013-06-24 23:50:24 +0200582 CPUState *cpu;
Blue Swirl296af7c2010-03-29 19:23:50 +0000583
Longpengf962cac2020-03-16 16:37:32 +0800584 if (!runstate_is_running()) {
585 return;
586 }
587
Alex Bligh40daca52013-08-21 16:03:02 +0100588 qemu_clock_enable(QEMU_CLOCK_VIRTUAL, true);
Andreas Färberbdc44642013-06-24 23:50:24 +0200589 CPU_FOREACH(cpu) {
Andreas Färber182735e2013-05-29 22:29:20 +0200590 cpu_resume(cpu);
Blue Swirl296af7c2010-03-29 19:23:50 +0000591 }
592}
593
Paolo Bonzinidbadee42018-01-30 16:40:12 +0100594void cpu_remove_sync(CPUState *cpu)
Gu Zheng4c055ab2016-05-12 09:18:13 +0530595{
596 cpu->stop = true;
597 cpu->unplug = true;
598 qemu_cpu_kick(cpu);
Paolo Bonzinidbadee42018-01-30 16:40:12 +0100599 qemu_mutex_unlock_iothread();
600 qemu_thread_join(cpu->thread);
601 qemu_mutex_lock_iothread();
Bharata B Rao2c579042016-05-12 09:18:14 +0530602}
603
Claudio Fontanab86f59c2021-02-04 17:39:25 +0100604void cpus_register_accel(const AccelOpsClass *ops)
Claudio Fontana430065d2020-07-31 12:23:42 +0200605{
Claudio Fontanab86f59c2021-02-04 17:39:25 +0100606 assert(ops != NULL);
607 assert(ops->create_vcpu_thread != NULL); /* mandatory */
608 cpus_accel = ops;
Claudio Fontana430065d2020-07-31 12:23:42 +0200609}
610
Andreas Färberc643bed2013-05-27 03:23:24 +0200611void qemu_init_vcpu(CPUState *cpu)
Blue Swirl296af7c2010-03-29 19:23:50 +0000612{
Like Xu5cc87672019-05-19 04:54:21 +0800613 MachineState *ms = MACHINE(qdev_get_machine());
614
615 cpu->nr_cores = ms->smp.cores;
616 cpu->nr_threads = ms->smp.threads;
Andreas Färberf324e762012-05-02 23:26:21 +0200617 cpu->stopped = true;
Richard Henderson9c09a252019-03-14 13:06:29 -0700618 cpu->random_seed = qemu_guest_random_seed_thread_part1();
Peter Maydell56943e82016-01-21 14:15:04 +0000619
620 if (!cpu->as) {
621 /* If the target cpu hasn't set up any address spaces itself,
622 * give it the default one.
623 */
Peter Maydell12ebc9a2016-01-21 14:15:04 +0000624 cpu->num_ases = 1;
Peter Xu80ceb072017-11-23 17:23:32 +0800625 cpu_address_space_init(cpu, 0, "cpu-memory", cpu->memory);
Peter Maydell56943e82016-01-21 14:15:04 +0000626 }
627
Claudio Fontanab86f59c2021-02-04 17:39:25 +0100628 /* accelerators all implement the AccelOpsClass */
Claudio Fontana994aa172020-08-19 16:01:03 +0200629 g_assert(cpus_accel != NULL && cpus_accel->create_vcpu_thread != NULL);
630 cpus_accel->create_vcpu_thread(cpu);
David Hildenbrand81e96312018-02-09 20:52:38 +0100631
632 while (!cpu->created) {
633 qemu_cond_wait(&qemu_cpu_cond, &qemu_global_mutex);
634 }
Blue Swirl296af7c2010-03-29 19:23:50 +0000635}
636
Jan Kiszkab4a3d962011-02-01 22:15:43 +0100637void cpu_stop_current(void)
Blue Swirl296af7c2010-03-29 19:23:50 +0000638{
Andreas Färber4917cf42013-05-27 05:17:50 +0200639 if (current_cpu) {
Peter Maydell0ec7e672019-01-07 15:23:47 +0000640 current_cpu->stop = true;
641 cpu_exit(current_cpu);
Jan Kiszkab4a3d962011-02-01 22:15:43 +0100642 }
Blue Swirl296af7c2010-03-29 19:23:50 +0000643}
644
Kevin Wolf56983462013-07-05 13:49:54 +0200645int vm_stop(RunState state)
Blue Swirl296af7c2010-03-29 19:23:50 +0000646{
Juan Quintelaaa723c22012-09-18 16:30:11 +0200647 if (qemu_in_vcpu_thread()) {
Paolo Bonzini74892d22014-06-05 14:53:58 +0200648 qemu_system_vmstop_request_prepare();
Luiz Capitulino1dfb4dd2011-07-29 14:26:33 -0300649 qemu_system_vmstop_request(state);
Blue Swirl296af7c2010-03-29 19:23:50 +0000650 /*
651 * FIXME: should not return to device code in case
652 * vm_stop() has been requested.
653 */
Jan Kiszkab4a3d962011-02-01 22:15:43 +0100654 cpu_stop_current();
Kevin Wolf56983462013-07-05 13:49:54 +0200655 return 0;
Blue Swirl296af7c2010-03-29 19:23:50 +0000656 }
Kevin Wolf56983462013-07-05 13:49:54 +0200657
Stefan Hajnoczi4486e892018-03-07 14:42:05 +0000658 return do_vm_stop(state, true);
Blue Swirl296af7c2010-03-29 19:23:50 +0000659}
660
Claudio Imbrenda2d76e822017-02-14 18:07:47 +0100661/**
662 * Prepare for (re)starting the VM.
663 * Returns -1 if the vCPUs are not to be restarted (e.g. if they are already
664 * running or in case of an error condition), 0 otherwise.
665 */
666int vm_prepare_start(void)
667{
668 RunState requested;
Claudio Imbrenda2d76e822017-02-14 18:07:47 +0100669
670 qemu_vmstop_requested(&requested);
671 if (runstate_is_running() && requested == RUN_STATE__MAX) {
672 return -1;
673 }
674
675 /* Ensure that a STOP/RESUME pair of events is emitted if a
676 * vmstop request was pending. The BLOCK_IO_ERROR event, for
677 * example, according to documentation is always followed by
678 * the STOP event.
679 */
680 if (runstate_is_running()) {
Peter Xu3ab72382018-08-15 21:37:37 +0800681 qapi_event_send_stop();
682 qapi_event_send_resume();
Markus Armbrusterf0561582018-04-23 10:45:18 +0200683 return -1;
Claudio Imbrenda2d76e822017-02-14 18:07:47 +0100684 }
685
686 /* We are sending this now, but the CPUs will be resumed shortly later */
Peter Xu3ab72382018-08-15 21:37:37 +0800687 qapi_event_send_resume();
Markus Armbrusterf0561582018-04-23 10:45:18 +0200688
Markus Armbrusterf0561582018-04-23 10:45:18 +0200689 cpu_enable_ticks();
690 runstate_set(RUN_STATE_RUNNING);
691 vm_state_notify(1, RUN_STATE_RUNNING);
692 return 0;
Claudio Imbrenda2d76e822017-02-14 18:07:47 +0100693}
694
695void vm_start(void)
696{
697 if (!vm_prepare_start()) {
698 resume_all_vcpus();
699 }
700}
701
Luiz Capitulino8a9236f2011-10-14 11:18:09 -0300702/* does a state transition even if the VM is already stopped,
703 current state is forgotten forever */
Kevin Wolf56983462013-07-05 13:49:54 +0200704int vm_stop_force_state(RunState state)
Luiz Capitulino8a9236f2011-10-14 11:18:09 -0300705{
706 if (runstate_is_running()) {
Kevin Wolf56983462013-07-05 13:49:54 +0200707 return vm_stop(state);
Luiz Capitulino8a9236f2011-10-14 11:18:09 -0300708 } else {
Daniel P. Berrangé8af3f5c2021-04-15 14:33:51 +0100709 int ret;
Luiz Capitulino8a9236f2011-10-14 11:18:09 -0300710 runstate_set(state);
Wen Congyangb2780d32015-11-20 17:34:38 +0800711
712 bdrv_drain_all();
Kevin Wolf594a45c2013-07-18 14:52:19 +0200713 /* Make sure to return an error if the flush in a previous vm_stop()
714 * failed. */
Daniel P. Berrangé8af3f5c2021-04-15 14:33:51 +0100715 ret = bdrv_flush_all();
716 trace_vm_stop_flush_all(ret);
717 return ret;
Luiz Capitulino8a9236f2011-10-14 11:18:09 -0300718 }
719}
720
Markus Armbruster04424282019-04-17 21:17:57 +0200721void list_cpus(const char *optarg)
Blue Swirl262353c2010-05-04 19:55:35 +0000722{
723 /* XXX: implement xxx_cpu_list for targets that still miss it */
Peter Maydelle916cbf2012-09-05 17:41:08 -0300724#if defined(cpu_list)
Markus Armbruster04424282019-04-17 21:17:57 +0200725 cpu_list();
Blue Swirl262353c2010-05-04 19:55:35 +0000726#endif
727}
Luiz Capitulinode0b36b2011-09-21 16:38:35 -0300728
Luiz Capitulino0cfd6a92011-11-22 16:32:37 -0200729void qmp_memsave(int64_t addr, int64_t size, const char *filename,
730 bool has_cpu, int64_t cpu_index, Error **errp)
731{
732 FILE *f;
733 uint32_t l;
Andreas Färber55e5c282012-12-17 06:18:02 +0100734 CPUState *cpu;
Luiz Capitulino0cfd6a92011-11-22 16:32:37 -0200735 uint8_t buf[1024];
Borislav Petkov0dc9daf2015-02-08 13:14:38 +0100736 int64_t orig_addr = addr, orig_size = size;
Luiz Capitulino0cfd6a92011-11-22 16:32:37 -0200737
738 if (!has_cpu) {
739 cpu_index = 0;
740 }
741
Andreas Färber151d1322013-02-15 15:41:49 +0100742 cpu = qemu_get_cpu(cpu_index);
743 if (cpu == NULL) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100744 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "cpu-index",
745 "a CPU number");
Luiz Capitulino0cfd6a92011-11-22 16:32:37 -0200746 return;
747 }
748
749 f = fopen(filename, "wb");
750 if (!f) {
Luiz Capitulino618da852013-06-07 14:35:06 -0400751 error_setg_file_open(errp, errno, filename);
Luiz Capitulino0cfd6a92011-11-22 16:32:37 -0200752 return;
753 }
754
755 while (size != 0) {
756 l = sizeof(buf);
757 if (l > size)
758 l = size;
Aneesh Kumar K.V2f4d0f52013-10-01 21:49:30 +0530759 if (cpu_memory_rw_debug(cpu, addr, buf, l, 0) != 0) {
Borislav Petkov0dc9daf2015-02-08 13:14:38 +0100760 error_setg(errp, "Invalid addr 0x%016" PRIx64 "/size %" PRId64
761 " specified", orig_addr, orig_size);
Aneesh Kumar K.V2f4d0f52013-10-01 21:49:30 +0530762 goto exit;
763 }
Luiz Capitulino0cfd6a92011-11-22 16:32:37 -0200764 if (fwrite(buf, 1, l, f) != l) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100765 error_setg(errp, QERR_IO_ERROR);
Luiz Capitulino0cfd6a92011-11-22 16:32:37 -0200766 goto exit;
767 }
768 addr += l;
769 size -= l;
770 }
771
772exit:
773 fclose(f);
774}
Luiz Capitulino6d3962b2011-11-22 17:26:46 -0200775
776void qmp_pmemsave(int64_t addr, int64_t size, const char *filename,
777 Error **errp)
778{
779 FILE *f;
780 uint32_t l;
781 uint8_t buf[1024];
782
783 f = fopen(filename, "wb");
784 if (!f) {
Luiz Capitulino618da852013-06-07 14:35:06 -0400785 error_setg_file_open(errp, errno, filename);
Luiz Capitulino6d3962b2011-11-22 17:26:46 -0200786 return;
787 }
788
789 while (size != 0) {
790 l = sizeof(buf);
791 if (l > size)
792 l = size;
Stefan Weileb6282f2014-04-07 20:28:23 +0200793 cpu_physical_memory_read(addr, buf, l);
Luiz Capitulino6d3962b2011-11-22 17:26:46 -0200794 if (fwrite(buf, 1, l, f) != l) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100795 error_setg(errp, QERR_IO_ERROR);
Luiz Capitulino6d3962b2011-11-22 17:26:46 -0200796 goto exit;
797 }
798 addr += l;
799 size -= l;
800 }
801
802exit:
803 fclose(f);
804}
Luiz Capitulinoab49ab52011-11-23 12:55:53 -0200805
806void qmp_inject_nmi(Error **errp)
807{
Kevin Wolf947e4742020-10-05 17:58:44 +0200808 nmi_monitor_handle(monitor_get_cpu_index(monitor_cur()), errp);
Luiz Capitulinoab49ab52011-11-23 12:55:53 -0200809}
Sebastian Tanase27498be2014-07-25 11:56:33 +0200810