blob: 3f8fae43ba628374c50ca292c8d81c3d3c50b5b7 [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"
Stefan Hajnoczid5d2b152022-02-22 14:01:50 +000028#include "qemu/coroutine-tls.h"
Markus Armbrustere688df62018-02-01 12:18:31 +010029#include "qapi/error.h"
Philippe Mathieu-Daudédf7a1f482020-10-12 14:15:32 +020030#include "qapi/qapi-commands-machine.h"
Markus Armbruster112ed242018-02-26 17:13:27 -060031#include "qapi/qapi-commands-misc.h"
Markus Armbruster9af23982018-02-11 10:36:01 +010032#include "qapi/qapi-events-run-state.h"
Wenchao Xiaa4e15de2014-06-18 08:43:36 +020033#include "qapi/qmp/qerror.h"
Paolo Bonzini022c62c2012-12-17 18:19:49 +010034#include "exec/gdbstub.h"
Vincent Palatinb3946622017-01-10 11:59:55 +010035#include "sysemu/hw_accel.h"
Philippe Mathieu-Daudé73842ef2022-02-03 02:13:28 +010036#include "exec/cpu-common.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010037#include "qemu/thread.h"
Emilio G. Cota30865f32018-10-21 13:30:35 -040038#include "qemu/plugin.h"
Paolo Bonzini9c17d612012-12-17 18:20:04 +010039#include "sysemu/cpus.h"
Richard Henderson9c09a252019-03-14 13:06:29 -070040#include "qemu/guest-random.h"
Alexey Kardashevskiy9cb805f2014-08-20 22:16:33 +100041#include "hw/nmi.h"
Pavel Dovgalyuk8b427042015-09-17 19:24:05 +030042#include "sysemu/replay.h"
Markus Armbruster54d31232019-08-12 07:23:59 +020043#include "sysemu/runstate.h"
Claudio Fontana740b1752020-08-19 13:17:19 +020044#include "sysemu/cpu-timers.h"
Sunil Muthuswamyfaf20792020-10-28 02:23:19 +000045#include "sysemu/whpx.h"
Like Xu5cc87672019-05-19 04:54:21 +080046#include "hw/boards.h"
Markus Armbruster650d1032019-08-12 07:23:48 +020047#include "hw/hw.h"
Daniel P. Berrangé8af3f5c2021-04-15 14:33:51 +010048#include "trace.h"
Jan Kiszka0ff0fc12011-06-23 10:15:55 +020049
Jan Kiszka6d9cb732011-02-01 22:15:58 +010050#ifdef CONFIG_LINUX
51
52#include <sys/prctl.h>
53
Marcelo Tosattic0532a72010-10-11 15:31:21 -030054#ifndef PR_MCE_KILL
55#define PR_MCE_KILL 33
56#endif
57
Jan Kiszka6d9cb732011-02-01 22:15:58 +010058#ifndef PR_MCE_KILL_SET
59#define PR_MCE_KILL_SET 1
60#endif
61
62#ifndef PR_MCE_KILL_EARLY
63#define PR_MCE_KILL_EARLY 1
64#endif
65
66#endif /* CONFIG_LINUX */
67
Yury Kotovbd1f7ff2019-09-09 16:13:34 +030068static QemuMutex qemu_global_mutex;
69
Tiejun Chen321bc0b2013-08-02 09:43:09 +080070bool cpu_is_stopped(CPUState *cpu)
71{
72 return cpu->stopped || !runstate_is_running();
73}
74
Claudio Fontana430065d2020-07-31 12:23:42 +020075bool cpu_work_list_empty(CPUState *cpu)
Emilio G. Cota0c0fcc22020-06-12 20:02:24 +010076{
Idan Horowitz25e82fb2022-01-14 02:43:57 +020077 return QSIMPLEQ_EMPTY_ATOMIC(&cpu->work_list);
Emilio G. Cota0c0fcc22020-06-12 20:02:24 +010078}
79
Claudio Fontana430065d2020-07-31 12:23:42 +020080bool cpu_thread_is_idle(CPUState *cpu)
Peter Maydellac873f12012-07-19 16:52:27 +010081{
Emilio G. Cota0c0fcc22020-06-12 20:02:24 +010082 if (cpu->stop || !cpu_work_list_empty(cpu)) {
Peter Maydellac873f12012-07-19 16:52:27 +010083 return false;
84 }
Tiejun Chen321bc0b2013-08-02 09:43:09 +080085 if (cpu_is_stopped(cpu)) {
Peter Maydellac873f12012-07-19 16:52:27 +010086 return true;
87 }
Andreas Färber8c2e1b02013-08-25 18:53:55 +020088 if (!cpu->halted || cpu_has_work(cpu) ||
Sunil Muthuswamyfaf20792020-10-28 02:23:19 +000089 kvm_halt_in_kernel() || whpx_apic_in_platform()) {
Peter Maydellac873f12012-07-19 16:52:27 +010090 return false;
91 }
92 return true;
93}
94
Claudio Fontana740b1752020-08-19 13:17:19 +020095bool all_cpu_threads_idle(void)
Peter Maydellac873f12012-07-19 16:52:27 +010096{
Andreas Färber182735e2013-05-29 22:29:20 +020097 CPUState *cpu;
Peter Maydellac873f12012-07-19 16:52:27 +010098
Andreas Färberbdc44642013-06-24 23:50:24 +020099 CPU_FOREACH(cpu) {
Andreas Färber182735e2013-05-29 22:29:20 +0200100 if (!cpu_thread_is_idle(cpu)) {
Peter Maydellac873f12012-07-19 16:52:27 +0100101 return false;
102 }
103 }
104 return true;
105}
106
Alex Bennée65467062017-02-23 18:29:09 +0000107/***********************************************************/
Blue Swirl296af7c2010-03-29 19:23:50 +0000108void hw_error(const char *fmt, ...)
109{
110 va_list ap;
Andreas Färber55e5c282012-12-17 06:18:02 +0100111 CPUState *cpu;
Blue Swirl296af7c2010-03-29 19:23:50 +0000112
113 va_start(ap, fmt);
114 fprintf(stderr, "qemu: hardware error: ");
115 vfprintf(stderr, fmt, ap);
116 fprintf(stderr, "\n");
Andreas Färberbdc44642013-06-24 23:50:24 +0200117 CPU_FOREACH(cpu) {
Andreas Färber55e5c282012-12-17 06:18:02 +0100118 fprintf(stderr, "CPU #%d:\n", cpu->cpu_index);
Markus Armbruster90c84c52019-04-17 21:18:02 +0200119 cpu_dump_state(cpu, stderr, CPU_DUMP_FPU);
Blue Swirl296af7c2010-03-29 19:23:50 +0000120 }
121 va_end(ap);
122 abort();
123}
124
Claudio Fontana430065d2020-07-31 12:23:42 +0200125/*
126 * The chosen accelerator is supposed to register this.
127 */
Claudio Fontanab86f59c2021-02-04 17:39:25 +0100128static const AccelOpsClass *cpus_accel;
Claudio Fontana430065d2020-07-31 12:23:42 +0200129
Blue Swirl296af7c2010-03-29 19:23:50 +0000130void cpu_synchronize_all_states(void)
131{
Andreas Färber182735e2013-05-29 22:29:20 +0200132 CPUState *cpu;
Blue Swirl296af7c2010-03-29 19:23:50 +0000133
Andreas Färberbdc44642013-06-24 23:50:24 +0200134 CPU_FOREACH(cpu) {
Andreas Färber182735e2013-05-29 22:29:20 +0200135 cpu_synchronize_state(cpu);
Blue Swirl296af7c2010-03-29 19:23:50 +0000136 }
137}
138
139void cpu_synchronize_all_post_reset(void)
140{
Andreas Färber182735e2013-05-29 22:29:20 +0200141 CPUState *cpu;
Blue Swirl296af7c2010-03-29 19:23:50 +0000142
Andreas Färberbdc44642013-06-24 23:50:24 +0200143 CPU_FOREACH(cpu) {
Andreas Färber182735e2013-05-29 22:29:20 +0200144 cpu_synchronize_post_reset(cpu);
Blue Swirl296af7c2010-03-29 19:23:50 +0000145 }
146}
147
148void cpu_synchronize_all_post_init(void)
149{
Andreas Färber182735e2013-05-29 22:29:20 +0200150 CPUState *cpu;
Blue Swirl296af7c2010-03-29 19:23:50 +0000151
Andreas Färberbdc44642013-06-24 23:50:24 +0200152 CPU_FOREACH(cpu) {
Andreas Färber182735e2013-05-29 22:29:20 +0200153 cpu_synchronize_post_init(cpu);
Blue Swirl296af7c2010-03-29 19:23:50 +0000154 }
155}
156
David Gibson75e972d2017-05-26 14:46:28 +1000157void cpu_synchronize_all_pre_loadvm(void)
158{
159 CPUState *cpu;
160
161 CPU_FOREACH(cpu) {
162 cpu_synchronize_pre_loadvm(cpu);
163 }
164}
165
Claudio Fontana430065d2020-07-31 12:23:42 +0200166void cpu_synchronize_state(CPUState *cpu)
167{
Claudio Fontana994aa172020-08-19 16:01:03 +0200168 if (cpus_accel->synchronize_state) {
Claudio Fontana430065d2020-07-31 12:23:42 +0200169 cpus_accel->synchronize_state(cpu);
170 }
Claudio Fontana430065d2020-07-31 12:23:42 +0200171}
172
173void cpu_synchronize_post_reset(CPUState *cpu)
174{
Claudio Fontana994aa172020-08-19 16:01:03 +0200175 if (cpus_accel->synchronize_post_reset) {
Claudio Fontana430065d2020-07-31 12:23:42 +0200176 cpus_accel->synchronize_post_reset(cpu);
177 }
Claudio Fontana430065d2020-07-31 12:23:42 +0200178}
179
180void cpu_synchronize_post_init(CPUState *cpu)
181{
Claudio Fontana994aa172020-08-19 16:01:03 +0200182 if (cpus_accel->synchronize_post_init) {
Claudio Fontana430065d2020-07-31 12:23:42 +0200183 cpus_accel->synchronize_post_init(cpu);
184 }
Claudio Fontana430065d2020-07-31 12:23:42 +0200185}
186
187void cpu_synchronize_pre_loadvm(CPUState *cpu)
188{
Claudio Fontana994aa172020-08-19 16:01:03 +0200189 if (cpus_accel->synchronize_pre_loadvm) {
Claudio Fontana430065d2020-07-31 12:23:42 +0200190 cpus_accel->synchronize_pre_loadvm(cpu);
191 }
Claudio Fontana430065d2020-07-31 12:23:42 +0200192}
193
Tom Lendacky92a51992021-01-26 11:36:47 -0600194bool cpus_are_resettable(void)
195{
196 return cpu_check_are_resettable();
197}
198
Claudio Fontana430065d2020-07-31 12:23:42 +0200199int64_t cpus_get_virtual_clock(void)
200{
Claudio Fontana994aa172020-08-19 16:01:03 +0200201 /*
202 * XXX
203 *
204 * need to check that cpus_accel is not NULL, because qcow2 calls
205 * qemu_get_clock_ns(CLOCK_VIRTUAL) without any accel initialized and
206 * with ticks disabled in some io-tests:
207 * 030 040 041 060 099 120 127 140 156 161 172 181 191 192 195 203 229 249 256 267
208 *
209 * is this expected?
210 *
211 * XXX
212 */
Claudio Fontana430065d2020-07-31 12:23:42 +0200213 if (cpus_accel && cpus_accel->get_virtual_clock) {
214 return cpus_accel->get_virtual_clock();
215 }
Claudio Fontana430065d2020-07-31 12:23:42 +0200216 return cpu_get_clock();
217}
218
219/*
220 * return the time elapsed in VM between vm_start and vm_stop. Unless
221 * icount is active, cpus_get_elapsed_ticks() uses units of the host CPU cycle
222 * counter.
223 */
224int64_t cpus_get_elapsed_ticks(void)
225{
Claudio Fontana994aa172020-08-19 16:01:03 +0200226 if (cpus_accel->get_elapsed_ticks) {
Claudio Fontana430065d2020-07-31 12:23:42 +0200227 return cpus_accel->get_elapsed_ticks();
228 }
Claudio Fontana430065d2020-07-31 12:23:42 +0200229 return cpu_get_ticks();
230}
231
Claudio Fontanabb4776b2020-08-11 15:16:33 +0200232static void generic_handle_interrupt(CPUState *cpu, int mask)
233{
234 cpu->interrupt_request |= mask;
235
236 if (!qemu_cpu_is_self(cpu)) {
237 qemu_cpu_kick(cpu);
238 }
239}
240
241void cpu_interrupt(CPUState *cpu, int mask)
242{
243 if (cpus_accel->handle_interrupt) {
244 cpus_accel->handle_interrupt(cpu, mask);
245 } else {
246 generic_handle_interrupt(cpu, mask);
247 }
248}
249
Stefan Hajnoczi4486e892018-03-07 14:42:05 +0000250static int do_vm_stop(RunState state, bool send_stop)
Blue Swirl296af7c2010-03-29 19:23:50 +0000251{
Kevin Wolf56983462013-07-05 13:49:54 +0200252 int ret = 0;
253
Luiz Capitulino13548692011-07-29 15:36:43 -0300254 if (runstate_is_running()) {
Longpengf962cac2020-03-16 16:37:32 +0800255 runstate_set(state);
Blue Swirl296af7c2010-03-29 19:23:50 +0000256 cpu_disable_ticks();
Blue Swirl296af7c2010-03-29 19:23:50 +0000257 pause_all_vcpus();
Luiz Capitulino1dfb4dd2011-07-29 14:26:33 -0300258 vm_state_notify(0, state);
Stefan Hajnoczi4486e892018-03-07 14:42:05 +0000259 if (send_stop) {
Peter Xu3ab72382018-08-15 21:37:37 +0800260 qapi_event_send_stop();
Stefan Hajnoczi4486e892018-03-07 14:42:05 +0000261 }
Blue Swirl296af7c2010-03-29 19:23:50 +0000262 }
Kevin Wolf56983462013-07-05 13:49:54 +0200263
Kevin Wolf594a45c2013-07-18 14:52:19 +0200264 bdrv_drain_all();
John Snow22af08e2016-09-22 21:45:51 -0400265 ret = bdrv_flush_all();
Daniel P. Berrangé8af3f5c2021-04-15 14:33:51 +0100266 trace_vm_stop_flush_all(ret);
Kevin Wolf594a45c2013-07-18 14:52:19 +0200267
Kevin Wolf56983462013-07-05 13:49:54 +0200268 return ret;
Blue Swirl296af7c2010-03-29 19:23:50 +0000269}
270
Stefan Hajnoczi4486e892018-03-07 14:42:05 +0000271/* Special vm_stop() variant for terminating the process. Historically clients
272 * did not expect a QMP STOP event and so we need to retain compatibility.
273 */
274int vm_shutdown(void)
275{
276 return do_vm_stop(RUN_STATE_SHUTDOWN, false);
277}
278
Claudio Fontana430065d2020-07-31 12:23:42 +0200279bool cpu_can_run(CPUState *cpu)
Blue Swirl296af7c2010-03-29 19:23:50 +0000280{
Andreas Färber4fdeee72012-05-02 23:10:09 +0200281 if (cpu->stop) {
Andreas Färbera1fcaa72012-05-02 23:42:26 +0200282 return false;
Jan Kiszka0ab07c62011-02-07 12:19:14 +0100283 }
Tiejun Chen321bc0b2013-08-02 09:43:09 +0800284 if (cpu_is_stopped(cpu)) {
Andreas Färbera1fcaa72012-05-02 23:42:26 +0200285 return false;
Jan Kiszka0ab07c62011-02-07 12:19:14 +0100286 }
Andreas Färbera1fcaa72012-05-02 23:42:26 +0200287 return true;
Blue Swirl296af7c2010-03-29 19:23:50 +0000288}
289
Claudio Fontana430065d2020-07-31 12:23:42 +0200290void cpu_handle_guest_debug(CPUState *cpu)
Jan Kiszka3c638d02010-06-25 16:56:56 +0200291{
Pavel Dovgalyukfda84582020-10-03 20:13:43 +0300292 if (replay_running_debug()) {
293 if (!cpu->singlestep_enabled) {
Pavel Dovgalyukcda38252020-10-03 20:13:49 +0300294 /*
295 * Report about the breakpoint and
296 * make a single step to skip it
297 */
298 replay_breakpoint();
Pavel Dovgalyukfda84582020-10-03 20:13:43 +0300299 cpu_single_step(cpu, SSTEP_ENABLE);
300 } else {
301 cpu_single_step(cpu, 0);
302 }
303 } else {
304 gdb_set_stop_cpu(cpu);
305 qemu_system_debug_request();
306 cpu->stopped = true;
307 }
Jan Kiszka3c638d02010-06-25 16:56:56 +0200308}
309
Jan Kiszka6d9cb732011-02-01 22:15:58 +0100310#ifdef CONFIG_LINUX
311static void sigbus_reraise(void)
312{
313 sigset_t set;
314 struct sigaction action;
315
316 memset(&action, 0, sizeof(action));
317 action.sa_handler = SIG_DFL;
318 if (!sigaction(SIGBUS, &action, NULL)) {
319 raise(SIGBUS);
320 sigemptyset(&set);
321 sigaddset(&set, SIGBUS);
Peter Maydella2d17612016-05-16 18:33:59 +0100322 pthread_sigmask(SIG_UNBLOCK, &set, NULL);
Jan Kiszka6d9cb732011-02-01 22:15:58 +0100323 }
Li Zhijianeb1960a2021-07-06 17:44:33 +0800324 perror("Failed to re-raise SIGBUS!");
Jan Kiszka6d9cb732011-02-01 22:15:58 +0100325 abort();
326}
327
Paolo Bonzinid98d4072017-02-08 13:22:12 +0100328static void sigbus_handler(int n, siginfo_t *siginfo, void *ctx)
Jan Kiszka6d9cb732011-02-01 22:15:58 +0100329{
Paolo Bonzinia16fc072017-02-09 09:50:02 +0100330 if (siginfo->si_code != BUS_MCEERR_AO && siginfo->si_code != BUS_MCEERR_AR) {
331 sigbus_reraise();
332 }
333
Paolo Bonzini2ae41db2017-02-08 12:48:54 +0100334 if (current_cpu) {
335 /* Called asynchronously in VCPU thread. */
336 if (kvm_on_sigbus_vcpu(current_cpu, siginfo->si_code, siginfo->si_addr)) {
337 sigbus_reraise();
338 }
339 } else {
340 /* Called synchronously (via signalfd) in main thread. */
341 if (kvm_on_sigbus(siginfo->si_code, siginfo->si_addr)) {
342 sigbus_reraise();
343 }
Jan Kiszka6d9cb732011-02-01 22:15:58 +0100344 }
345}
346
347static void qemu_init_sigbus(void)
348{
349 struct sigaction action;
350
David Hildenbrand29b838c2021-12-17 14:46:10 +0100351 /*
352 * ALERT: when modifying this, take care that SIGBUS forwarding in
353 * os_mem_prealloc() will continue working as expected.
354 */
Jan Kiszka6d9cb732011-02-01 22:15:58 +0100355 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
Stefan Hajnoczid5d2b152022-02-22 14:01:50 +0000477QEMU_DEFINE_STATIC_CO_TLS(bool, iothread_locked)
Paolo Bonziniafbe7052015-06-18 18:47:19 +0200478
479bool qemu_mutex_iothread_locked(void)
480{
Stefan Hajnoczid5d2b152022-02-22 14:01:50 +0000481 return get_iothread_locked();
Paolo Bonziniafbe7052015-06-18 18:47:19 +0200482}
483
Emanuele Giuseppe Esposito65386922022-03-03 10:15:46 -0500484bool qemu_in_main_thread(void)
485{
486 return qemu_mutex_iothread_locked();
487}
488
Emilio G. Cotacb764d02017-10-28 02:16:41 -0400489/*
490 * The BQL is taken from so many places that it is worth profiling the
491 * callers directly, instead of funneling them all through a single function.
492 */
493void qemu_mutex_lock_iothread_impl(const char *file, int line)
Blue Swirl296af7c2010-03-29 19:23:50 +0000494{
Stefan Hajnoczid73415a2020-09-23 11:56:46 +0100495 QemuMutexLockFunc bql_lock = qatomic_read(&qemu_bql_mutex_lock_func);
Emilio G. Cotacb764d02017-10-28 02:16:41 -0400496
Jan Kiszka8d04fb52017-02-23 18:29:11 +0000497 g_assert(!qemu_mutex_iothread_locked());
Emilio G. Cotacb764d02017-10-28 02:16:41 -0400498 bql_lock(&qemu_global_mutex, file, line);
Stefan Hajnoczid5d2b152022-02-22 14:01:50 +0000499 set_iothread_locked(true);
Blue Swirl296af7c2010-03-29 19:23:50 +0000500}
501
502void qemu_mutex_unlock_iothread(void)
503{
Jan Kiszka8d04fb52017-02-23 18:29:11 +0000504 g_assert(qemu_mutex_iothread_locked());
Stefan Hajnoczid5d2b152022-02-22 14:01:50 +0000505 set_iothread_locked(false);
Blue Swirl296af7c2010-03-29 19:23:50 +0000506 qemu_mutex_unlock(&qemu_global_mutex);
507}
508
Aravinda Prasad19e067e2020-01-31 00:14:17 +0530509void qemu_cond_wait_iothread(QemuCond *cond)
510{
511 qemu_cond_wait(cond, &qemu_global_mutex);
512}
513
Claudio Fontanab0c3cf92020-06-29 11:35:03 +0200514void qemu_cond_timedwait_iothread(QemuCond *cond, int ms)
515{
516 qemu_cond_timedwait(cond, &qemu_global_mutex, ms);
517}
518
Claudio Fontana430065d2020-07-31 12:23:42 +0200519/* signal CPU creation */
520void cpu_thread_signal_created(CPUState *cpu)
521{
522 cpu->created = true;
523 qemu_cond_signal(&qemu_cpu_cond);
524}
525
526/* signal CPU destruction */
527void cpu_thread_signal_destroyed(CPUState *cpu)
528{
529 cpu->created = false;
530 qemu_cond_signal(&qemu_cpu_cond);
531}
532
533
Alex Bennéee8faee02016-10-27 16:09:58 +0100534static bool all_vcpus_paused(void)
Blue Swirl296af7c2010-03-29 19:23:50 +0000535{
Andreas Färberbdc44642013-06-24 23:50:24 +0200536 CPUState *cpu;
Blue Swirl296af7c2010-03-29 19:23:50 +0000537
Andreas Färberbdc44642013-06-24 23:50:24 +0200538 CPU_FOREACH(cpu) {
Andreas Färber182735e2013-05-29 22:29:20 +0200539 if (!cpu->stopped) {
Alex Bennéee8faee02016-10-27 16:09:58 +0100540 return false;
Jan Kiszka0ab07c62011-02-07 12:19:14 +0100541 }
Blue Swirl296af7c2010-03-29 19:23:50 +0000542 }
543
Alex Bennéee8faee02016-10-27 16:09:58 +0100544 return true;
Blue Swirl296af7c2010-03-29 19:23:50 +0000545}
546
547void pause_all_vcpus(void)
548{
Andreas Färberbdc44642013-06-24 23:50:24 +0200549 CPUState *cpu;
Blue Swirl296af7c2010-03-29 19:23:50 +0000550
Alex Bligh40daca52013-08-21 16:03:02 +0100551 qemu_clock_enable(QEMU_CLOCK_VIRTUAL, false);
Andreas Färberbdc44642013-06-24 23:50:24 +0200552 CPU_FOREACH(cpu) {
David Hildenbrandebd05fe2017-11-29 20:12:15 +0100553 if (qemu_cpu_is_self(cpu)) {
554 qemu_cpu_stop(cpu, true);
555 } else {
556 cpu->stop = true;
557 qemu_cpu_kick(cpu);
558 }
Jan Kiszkad798e972012-02-17 18:31:16 +0100559 }
560
Alex Bennéed759c952018-02-27 12:52:48 +0300561 /* We need to drop the replay_lock so any vCPU threads woken up
562 * can finish their replay tasks
563 */
564 replay_mutex_unlock();
565
Blue Swirl296af7c2010-03-29 19:23:50 +0000566 while (!all_vcpus_paused()) {
Paolo Bonzinibe7d6c52011-03-12 17:44:02 +0100567 qemu_cond_wait(&qemu_pause_cond, &qemu_global_mutex);
Andreas Färberbdc44642013-06-24 23:50:24 +0200568 CPU_FOREACH(cpu) {
Andreas Färber182735e2013-05-29 22:29:20 +0200569 qemu_cpu_kick(cpu);
Blue Swirl296af7c2010-03-29 19:23:50 +0000570 }
571 }
Alex Bennéed759c952018-02-27 12:52:48 +0300572
573 qemu_mutex_unlock_iothread();
574 replay_mutex_lock();
575 qemu_mutex_lock_iothread();
Blue Swirl296af7c2010-03-29 19:23:50 +0000576}
577
Igor Mammedov29936832013-04-23 10:29:37 +0200578void cpu_resume(CPUState *cpu)
579{
580 cpu->stop = false;
581 cpu->stopped = false;
582 qemu_cpu_kick(cpu);
583}
584
Blue Swirl296af7c2010-03-29 19:23:50 +0000585void resume_all_vcpus(void)
586{
Andreas Färberbdc44642013-06-24 23:50:24 +0200587 CPUState *cpu;
Blue Swirl296af7c2010-03-29 19:23:50 +0000588
Longpengf962cac2020-03-16 16:37:32 +0800589 if (!runstate_is_running()) {
590 return;
591 }
592
Alex Bligh40daca52013-08-21 16:03:02 +0100593 qemu_clock_enable(QEMU_CLOCK_VIRTUAL, true);
Andreas Färberbdc44642013-06-24 23:50:24 +0200594 CPU_FOREACH(cpu) {
Andreas Färber182735e2013-05-29 22:29:20 +0200595 cpu_resume(cpu);
Blue Swirl296af7c2010-03-29 19:23:50 +0000596 }
597}
598
Paolo Bonzinidbadee42018-01-30 16:40:12 +0100599void cpu_remove_sync(CPUState *cpu)
Gu Zheng4c055ab2016-05-12 09:18:13 +0530600{
601 cpu->stop = true;
602 cpu->unplug = true;
603 qemu_cpu_kick(cpu);
Paolo Bonzinidbadee42018-01-30 16:40:12 +0100604 qemu_mutex_unlock_iothread();
605 qemu_thread_join(cpu->thread);
606 qemu_mutex_lock_iothread();
Bharata B Rao2c579042016-05-12 09:18:14 +0530607}
608
Claudio Fontanab86f59c2021-02-04 17:39:25 +0100609void cpus_register_accel(const AccelOpsClass *ops)
Claudio Fontana430065d2020-07-31 12:23:42 +0200610{
Claudio Fontanab86f59c2021-02-04 17:39:25 +0100611 assert(ops != NULL);
612 assert(ops->create_vcpu_thread != NULL); /* mandatory */
613 cpus_accel = ops;
Claudio Fontana430065d2020-07-31 12:23:42 +0200614}
615
Andreas Färberc643bed2013-05-27 03:23:24 +0200616void qemu_init_vcpu(CPUState *cpu)
Blue Swirl296af7c2010-03-29 19:23:50 +0000617{
Like Xu5cc87672019-05-19 04:54:21 +0800618 MachineState *ms = MACHINE(qdev_get_machine());
619
620 cpu->nr_cores = ms->smp.cores;
621 cpu->nr_threads = ms->smp.threads;
Andreas Färberf324e762012-05-02 23:26:21 +0200622 cpu->stopped = true;
Richard Henderson9c09a252019-03-14 13:06:29 -0700623 cpu->random_seed = qemu_guest_random_seed_thread_part1();
Peter Maydell56943e82016-01-21 14:15:04 +0000624
625 if (!cpu->as) {
626 /* If the target cpu hasn't set up any address spaces itself,
627 * give it the default one.
628 */
Peter Maydell12ebc9a2016-01-21 14:15:04 +0000629 cpu->num_ases = 1;
Peter Xu80ceb072017-11-23 17:23:32 +0800630 cpu_address_space_init(cpu, 0, "cpu-memory", cpu->memory);
Peter Maydell56943e82016-01-21 14:15:04 +0000631 }
632
Claudio Fontanab86f59c2021-02-04 17:39:25 +0100633 /* accelerators all implement the AccelOpsClass */
Claudio Fontana994aa172020-08-19 16:01:03 +0200634 g_assert(cpus_accel != NULL && cpus_accel->create_vcpu_thread != NULL);
635 cpus_accel->create_vcpu_thread(cpu);
David Hildenbrand81e96312018-02-09 20:52:38 +0100636
637 while (!cpu->created) {
638 qemu_cond_wait(&qemu_cpu_cond, &qemu_global_mutex);
639 }
Blue Swirl296af7c2010-03-29 19:23:50 +0000640}
641
Jan Kiszkab4a3d962011-02-01 22:15:43 +0100642void cpu_stop_current(void)
Blue Swirl296af7c2010-03-29 19:23:50 +0000643{
Andreas Färber4917cf42013-05-27 05:17:50 +0200644 if (current_cpu) {
Peter Maydell0ec7e672019-01-07 15:23:47 +0000645 current_cpu->stop = true;
646 cpu_exit(current_cpu);
Jan Kiszkab4a3d962011-02-01 22:15:43 +0100647 }
Blue Swirl296af7c2010-03-29 19:23:50 +0000648}
649
Kevin Wolf56983462013-07-05 13:49:54 +0200650int vm_stop(RunState state)
Blue Swirl296af7c2010-03-29 19:23:50 +0000651{
Juan Quintelaaa723c22012-09-18 16:30:11 +0200652 if (qemu_in_vcpu_thread()) {
Paolo Bonzini74892d22014-06-05 14:53:58 +0200653 qemu_system_vmstop_request_prepare();
Luiz Capitulino1dfb4dd2011-07-29 14:26:33 -0300654 qemu_system_vmstop_request(state);
Blue Swirl296af7c2010-03-29 19:23:50 +0000655 /*
656 * FIXME: should not return to device code in case
657 * vm_stop() has been requested.
658 */
Jan Kiszkab4a3d962011-02-01 22:15:43 +0100659 cpu_stop_current();
Kevin Wolf56983462013-07-05 13:49:54 +0200660 return 0;
Blue Swirl296af7c2010-03-29 19:23:50 +0000661 }
Kevin Wolf56983462013-07-05 13:49:54 +0200662
Stefan Hajnoczi4486e892018-03-07 14:42:05 +0000663 return do_vm_stop(state, true);
Blue Swirl296af7c2010-03-29 19:23:50 +0000664}
665
Claudio Imbrenda2d76e822017-02-14 18:07:47 +0100666/**
667 * Prepare for (re)starting the VM.
668 * Returns -1 if the vCPUs are not to be restarted (e.g. if they are already
669 * running or in case of an error condition), 0 otherwise.
670 */
671int vm_prepare_start(void)
672{
673 RunState requested;
Claudio Imbrenda2d76e822017-02-14 18:07:47 +0100674
675 qemu_vmstop_requested(&requested);
676 if (runstate_is_running() && requested == RUN_STATE__MAX) {
677 return -1;
678 }
679
680 /* Ensure that a STOP/RESUME pair of events is emitted if a
681 * vmstop request was pending. The BLOCK_IO_ERROR event, for
682 * example, according to documentation is always followed by
683 * the STOP event.
684 */
685 if (runstate_is_running()) {
Peter Xu3ab72382018-08-15 21:37:37 +0800686 qapi_event_send_stop();
687 qapi_event_send_resume();
Markus Armbrusterf0561582018-04-23 10:45:18 +0200688 return -1;
Claudio Imbrenda2d76e822017-02-14 18:07:47 +0100689 }
690
691 /* We are sending this now, but the CPUs will be resumed shortly later */
Peter Xu3ab72382018-08-15 21:37:37 +0800692 qapi_event_send_resume();
Markus Armbrusterf0561582018-04-23 10:45:18 +0200693
Markus Armbrusterf0561582018-04-23 10:45:18 +0200694 cpu_enable_ticks();
695 runstate_set(RUN_STATE_RUNNING);
696 vm_state_notify(1, RUN_STATE_RUNNING);
697 return 0;
Claudio Imbrenda2d76e822017-02-14 18:07:47 +0100698}
699
700void vm_start(void)
701{
702 if (!vm_prepare_start()) {
703 resume_all_vcpus();
704 }
705}
706
Luiz Capitulino8a9236f2011-10-14 11:18:09 -0300707/* does a state transition even if the VM is already stopped,
708 current state is forgotten forever */
Kevin Wolf56983462013-07-05 13:49:54 +0200709int vm_stop_force_state(RunState state)
Luiz Capitulino8a9236f2011-10-14 11:18:09 -0300710{
711 if (runstate_is_running()) {
Kevin Wolf56983462013-07-05 13:49:54 +0200712 return vm_stop(state);
Luiz Capitulino8a9236f2011-10-14 11:18:09 -0300713 } else {
Daniel P. Berrangé8af3f5c2021-04-15 14:33:51 +0100714 int ret;
Luiz Capitulino8a9236f2011-10-14 11:18:09 -0300715 runstate_set(state);
Wen Congyangb2780d32015-11-20 17:34:38 +0800716
717 bdrv_drain_all();
Kevin Wolf594a45c2013-07-18 14:52:19 +0200718 /* Make sure to return an error if the flush in a previous vm_stop()
719 * failed. */
Daniel P. Berrangé8af3f5c2021-04-15 14:33:51 +0100720 ret = bdrv_flush_all();
721 trace_vm_stop_flush_all(ret);
722 return ret;
Luiz Capitulino8a9236f2011-10-14 11:18:09 -0300723 }
724}
725
Markus Armbruster04424282019-04-17 21:17:57 +0200726void list_cpus(const char *optarg)
Blue Swirl262353c2010-05-04 19:55:35 +0000727{
728 /* XXX: implement xxx_cpu_list for targets that still miss it */
Peter Maydelle916cbf2012-09-05 17:41:08 -0300729#if defined(cpu_list)
Markus Armbruster04424282019-04-17 21:17:57 +0200730 cpu_list();
Blue Swirl262353c2010-05-04 19:55:35 +0000731#endif
732}
Luiz Capitulinode0b36b2011-09-21 16:38:35 -0300733
Luiz Capitulino0cfd6a92011-11-22 16:32:37 -0200734void qmp_memsave(int64_t addr, int64_t size, const char *filename,
735 bool has_cpu, int64_t cpu_index, Error **errp)
736{
737 FILE *f;
738 uint32_t l;
Andreas Färber55e5c282012-12-17 06:18:02 +0100739 CPUState *cpu;
Luiz Capitulino0cfd6a92011-11-22 16:32:37 -0200740 uint8_t buf[1024];
Borislav Petkov0dc9daf2015-02-08 13:14:38 +0100741 int64_t orig_addr = addr, orig_size = size;
Luiz Capitulino0cfd6a92011-11-22 16:32:37 -0200742
743 if (!has_cpu) {
744 cpu_index = 0;
745 }
746
Andreas Färber151d1322013-02-15 15:41:49 +0100747 cpu = qemu_get_cpu(cpu_index);
748 if (cpu == NULL) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100749 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "cpu-index",
750 "a CPU number");
Luiz Capitulino0cfd6a92011-11-22 16:32:37 -0200751 return;
752 }
753
754 f = fopen(filename, "wb");
755 if (!f) {
Luiz Capitulino618da852013-06-07 14:35:06 -0400756 error_setg_file_open(errp, errno, filename);
Luiz Capitulino0cfd6a92011-11-22 16:32:37 -0200757 return;
758 }
759
760 while (size != 0) {
761 l = sizeof(buf);
762 if (l > size)
763 l = size;
Aneesh Kumar K.V2f4d0f52013-10-01 21:49:30 +0530764 if (cpu_memory_rw_debug(cpu, addr, buf, l, 0) != 0) {
Borislav Petkov0dc9daf2015-02-08 13:14:38 +0100765 error_setg(errp, "Invalid addr 0x%016" PRIx64 "/size %" PRId64
766 " specified", orig_addr, orig_size);
Aneesh Kumar K.V2f4d0f52013-10-01 21:49:30 +0530767 goto exit;
768 }
Luiz Capitulino0cfd6a92011-11-22 16:32:37 -0200769 if (fwrite(buf, 1, l, f) != l) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100770 error_setg(errp, QERR_IO_ERROR);
Luiz Capitulino0cfd6a92011-11-22 16:32:37 -0200771 goto exit;
772 }
773 addr += l;
774 size -= l;
775 }
776
777exit:
778 fclose(f);
779}
Luiz Capitulino6d3962b2011-11-22 17:26:46 -0200780
781void qmp_pmemsave(int64_t addr, int64_t size, const char *filename,
782 Error **errp)
783{
784 FILE *f;
785 uint32_t l;
786 uint8_t buf[1024];
787
788 f = fopen(filename, "wb");
789 if (!f) {
Luiz Capitulino618da852013-06-07 14:35:06 -0400790 error_setg_file_open(errp, errno, filename);
Luiz Capitulino6d3962b2011-11-22 17:26:46 -0200791 return;
792 }
793
794 while (size != 0) {
795 l = sizeof(buf);
796 if (l > size)
797 l = size;
Stefan Weileb6282f2014-04-07 20:28:23 +0200798 cpu_physical_memory_read(addr, buf, l);
Luiz Capitulino6d3962b2011-11-22 17:26:46 -0200799 if (fwrite(buf, 1, l, f) != l) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100800 error_setg(errp, QERR_IO_ERROR);
Luiz Capitulino6d3962b2011-11-22 17:26:46 -0200801 goto exit;
802 }
803 addr += l;
804 size -= l;
805 }
806
807exit:
808 fclose(f);
809}
Luiz Capitulinoab49ab52011-11-23 12:55:53 -0200810
811void qmp_inject_nmi(Error **errp)
812{
Kevin Wolf947e4742020-10-05 17:58:44 +0200813 nmi_monitor_handle(monitor_get_cpu_index(monitor_cur()), errp);
Luiz Capitulinoab49ab52011-11-23 12:55:53 -0200814}
Sebastian Tanase27498be2014-07-25 11:56:33 +0200815