From: "Philippe Mathieu-Daudé" <philmd@oss.qualcomm.com>
To: qemu-devel@nongnu.org
Cc: "Dr. David Alan Gilbert" <dave@treblig.org>,
"Markus Armbruster" <armbru@redhat.com>,
"Marc-André Lureau" <marcandre.lureau@redhat.com>,
"Richard Henderson" <richard.henderson@linaro.org>,
"Philippe Mathieu-Daudé" <philmd@oss.qualcomm.com>,
"Paolo Bonzini" <pbonzini@redhat.com>,
"Philippe Mathieu-Daudé" <philmd@mailo.com>
Subject: [PATCH 13/14] system: Move runstate-related code from cpus.c to runstate.c
Date: Wed, 12 Aug 2026 14:23:30 +0200 [thread overview]
Message-ID: <20260812122332.72975-14-philmd@oss.qualcomm.com> (raw)
In-Reply-To: <20260812122332.72975-1-philmd@oss.qualcomm.com>
Signed-off-by: Philippe Mathieu-Daudé <philmd@oss.qualcomm.com>
---
system/cpus.c | 146 ---------------------------------------------
system/runstate.c | 148 ++++++++++++++++++++++++++++++++++++++++++++++
2 files changed, 148 insertions(+), 146 deletions(-)
diff --git a/system/cpus.c b/system/cpus.c
index 9758cda4636..f9f1b297d1c 100644
--- a/system/cpus.c
+++ b/system/cpus.c
@@ -277,58 +277,6 @@ void cpu_interrupt(CPUState *cpu, int mask)
cpus_accel->handle_interrupt(cpu, mask);
}
-/*
- * True if the vm was previously suspended, and has not been woken or reset.
- */
-static int vm_was_suspended;
-
-void vm_set_suspended(bool suspended)
-{
- vm_was_suspended = suspended;
-}
-
-bool vm_get_suspended(void)
-{
- return vm_was_suspended;
-}
-
-static int do_vm_stop(RunState state, bool send_stop)
-{
- int ret = 0;
- RunState oldstate = runstate_get();
-
- if (runstate_is_live(oldstate)) {
- vm_was_suspended = (oldstate == RUN_STATE_SUSPENDED);
- runstate_set(state);
- cpu_disable_ticks();
- if (oldstate == RUN_STATE_RUNNING) {
- pause_all_vcpus();
- }
- ret = vm_state_notify(0, state);
- if (send_stop) {
- qapi_event_send_stop();
- }
- }
-
- bdrv_drain_all();
- /*
- * Even if vm_state_notify() return failure,
- * it would be better to flush as before.
- */
- ret |= bdrv_flush_all();
- trace_vm_stop_flush_all(ret);
-
- return ret;
-}
-
-/* Special vm_stop() variant for terminating the process. Historically clients
- * did not expect a QMP STOP event and so we need to retain compatibility.
- */
-int vm_shutdown(void)
-{
- return do_vm_stop(RUN_STATE_SHUTDOWN, false);
-}
-
bool cpu_can_run(CPUState *cpu)
{
if (cpu->stop) {
@@ -740,100 +688,6 @@ void cpu_stop_current(void)
}
}
-int vm_stop(RunState state)
-{
- if (qemu_in_vcpu_thread()) {
- qemu_system_vmstop_request_prepare();
- qemu_system_vmstop_request(state);
- /*
- * FIXME: should not return to device code in case
- * vm_stop() has been requested.
- */
- cpu_stop_current();
- return 0;
- }
-
- return do_vm_stop(state, true);
-}
-
-/**
- * Prepare for (re)starting the VM.
- * Returns 0 if the vCPUs should be restarted, -1 on an error condition,
- * and 1 otherwise.
- */
-int vm_prepare_start(bool step_pending)
-{
- int ret = vm_was_suspended ? 1 : 0;
- RunState state = vm_was_suspended ? RUN_STATE_SUSPENDED : RUN_STATE_RUNNING;
- RunState requested;
-
- qemu_vmstop_requested(&requested);
- if (runstate_is_running() && requested == RUN_STATE__MAX) {
- return -1;
- }
-
- /* Ensure that a STOP/RESUME pair of events is emitted if a
- * vmstop request was pending. The BLOCK_IO_ERROR event, for
- * example, according to documentation is always followed by
- * the STOP event.
- */
- if (runstate_is_running()) {
- qapi_event_send_stop();
- qapi_event_send_resume();
- return -1;
- }
-
- /*
- * WHPX accelerator needs to know whether we are going to step
- * any CPUs, before starting the first one.
- */
- accel_pre_resume(MACHINE(qdev_get_machine()), step_pending);
-
- /* We are sending this now, but the CPUs will be resumed shortly later */
- qapi_event_send_resume();
-
- cpu_enable_ticks();
- runstate_set(state);
- vm_state_notify(1, state);
- vm_was_suspended = false;
- return ret;
-}
-
-void vm_start(void)
-{
- if (!vm_prepare_start(false)) {
- resume_all_vcpus();
- }
-}
-
-void vm_resume(RunState state)
-{
- if (runstate_is_live(state)) {
- vm_start();
- } else {
- runstate_set(state);
- }
-}
-
-/* does a state transition even if the VM is already stopped,
- current state is forgotten forever */
-int vm_stop_force_state(RunState state)
-{
- if (runstate_is_live(runstate_get())) {
- return vm_stop(state);
- } else {
- int ret;
- runstate_set(state);
-
- bdrv_drain_all();
- /* Make sure to return an error if the flush in a previous vm_stop()
- * failed. */
- ret = bdrv_flush_all();
- trace_vm_stop_flush_all(ret);
- return ret;
- }
-}
-
void qmp_memsave(uint64_t addr, uint64_t size, const char *filename,
bool has_cpu, int64_t cpu_index, Error **errp)
{
diff --git a/system/runstate.c b/system/runstate.c
index 08acf801b0e..79bf21d93e2 100644
--- a/system/runstate.c
+++ b/system/runstate.c
@@ -52,6 +52,7 @@
#include "qemu/thread.h"
#include "qom/object.h"
#include "qom/object_interfaces.h"
+#include "system/cpu-timers.h"
#include "system/cpus.h"
#include "system/qtest.h"
#include "system/replay.h"
@@ -408,6 +409,153 @@ int vm_state_notify(bool running, RunState state)
return ret;
}
+/*
+ * True if the vm was previously suspended, and has not been woken or reset.
+ */
+static int vm_was_suspended;
+
+void vm_set_suspended(bool suspended)
+{
+ vm_was_suspended = suspended;
+}
+
+bool vm_get_suspended(void)
+{
+ return vm_was_suspended;
+}
+
+static int do_vm_stop(RunState state, bool send_stop)
+{
+ int ret = 0;
+ RunState oldstate = runstate_get();
+
+ if (runstate_is_live(oldstate)) {
+ vm_was_suspended = (oldstate == RUN_STATE_SUSPENDED);
+ runstate_set(state);
+ cpu_disable_ticks();
+ if (oldstate == RUN_STATE_RUNNING) {
+ pause_all_vcpus();
+ }
+ ret = vm_state_notify(0, state);
+ if (send_stop) {
+ qapi_event_send_stop();
+ }
+ }
+
+ bdrv_drain_all();
+ /*
+ * Even if vm_state_notify() return failure,
+ * it would be better to flush as before.
+ */
+ ret |= bdrv_flush_all();
+ trace_vm_stop_flush_all(ret);
+
+ return ret;
+}
+
+/* Special vm_stop() variant for terminating the process. Historically clients
+ * did not expect a QMP STOP event and so we need to retain compatibility.
+ */
+int vm_shutdown(void)
+{
+ return do_vm_stop(RUN_STATE_SHUTDOWN, false);
+}
+
+
+int vm_stop(RunState state)
+{
+ if (qemu_in_vcpu_thread()) {
+ qemu_system_vmstop_request_prepare();
+ qemu_system_vmstop_request(state);
+ /*
+ * FIXME: should not return to device code in case
+ * vm_stop() has been requested.
+ */
+ cpu_stop_current();
+ return 0;
+ }
+
+ return do_vm_stop(state, true);
+}
+
+/**
+ * Prepare for (re)starting the VM.
+ * Returns 0 if the vCPUs should be restarted, -1 on an error condition,
+ * and 1 otherwise.
+ */
+int vm_prepare_start(bool step_pending)
+{
+ int ret = vm_was_suspended ? 1 : 0;
+ RunState state = vm_was_suspended ? RUN_STATE_SUSPENDED : RUN_STATE_RUNNING;
+ RunState requested;
+
+ qemu_vmstop_requested(&requested);
+ if (runstate_is_running() && requested == RUN_STATE__MAX) {
+ return -1;
+ }
+
+ /* Ensure that a STOP/RESUME pair of events is emitted if a
+ * vmstop request was pending. The BLOCK_IO_ERROR event, for
+ * example, according to documentation is always followed by
+ * the STOP event.
+ */
+ if (runstate_is_running()) {
+ qapi_event_send_stop();
+ qapi_event_send_resume();
+ return -1;
+ }
+
+ /*
+ * WHPX accelerator needs to know whether we are going to step
+ * any CPUs, before starting the first one.
+ */
+ accel_pre_resume(MACHINE(qdev_get_machine()), step_pending);
+
+ /* We are sending this now, but the CPUs will be resumed shortly later */
+ qapi_event_send_resume();
+
+ cpu_enable_ticks();
+ runstate_set(state);
+ vm_state_notify(1, state);
+ vm_was_suspended = false;
+ return ret;
+}
+
+void vm_start(void)
+{
+ if (!vm_prepare_start(false)) {
+ resume_all_vcpus();
+ }
+}
+
+void vm_resume(RunState state)
+{
+ if (runstate_is_live(state)) {
+ vm_start();
+ } else {
+ runstate_set(state);
+ }
+}
+
+/* does a state transition even if the VM is already stopped,
+ current state is forgotten forever */
+int vm_stop_force_state(RunState state)
+{
+ if (runstate_is_live(runstate_get())) {
+ return vm_stop(state);
+ } else {
+ int ret;
+ runstate_set(state);
+
+ bdrv_drain_all();
+ /* Make sure to return an error if the flush in a previous vm_stop()
+ * failed. */
+ ret = bdrv_flush_all();
+ trace_vm_stop_flush_all(ret);
+ return ret;
+ }
+}
+
static ShutdownCause reset_requested;
static ShutdownCause shutdown_requested;
static int shutdown_exit_code = EXIT_SUCCESS;
--
2.53.0
next prev parent reply other threads:[~2026-08-12 12:25 UTC|newest]
Thread overview: 16+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-08-12 12:23 [PATCH 00/14] monitor: Reduce headers included in 'monitor/monitor.h' Philippe Mathieu-Daudé
2026-08-12 12:23 ` [PATCH 01/14] hexagon: Remove unnecessary 'monitor/monitor.h' header Philippe Mathieu-Daudé
2026-08-12 12:23 ` [PATCH 02/14] tests/unit: Include 'qemu/main-loop.h' header in test-util-sockets.c Philippe Mathieu-Daudé
2026-08-12 12:23 ` [PATCH 03/14] qapi/qmp-dispatch: Include 'qemu/aio-wait.h' and 'monitor/monitor.h' Philippe Mathieu-Daudé
2026-08-12 12:23 ` [PATCH 04/14] qapi/qmp-registry: Remove unnecessary 'monitor/monitor.h' header Philippe Mathieu-Daudé
2026-08-12 12:23 ` [PATCH 05/14] migration/hmp-cmds: Include 'block/block-global-state.h' header Philippe Mathieu-Daudé
2026-08-12 12:23 ` [PATCH 06/14] monitor: Include missing 'qemu/aio-wait.h' header Philippe Mathieu-Daudé
2026-08-12 12:23 ` [PATCH 07/14] monitor: Include missing 'qemu/lockable.h' header Philippe Mathieu-Daudé
2026-08-12 12:23 ` [PATCH 08/14] monitor: Include missing 'qemu/coroutine-core.h' header Philippe Mathieu-Daudé
2026-08-12 12:23 ` [PATCH 09/14] monitor: Reduce inclusion of 'qapi/qapi-emit-events.h' header Philippe Mathieu-Daudé
2026-08-12 12:23 ` [PATCH 10/14] monitor: Remove unnecessary 'block/block.h' header Philippe Mathieu-Daudé
2026-08-12 12:23 ` [PATCH 11/14] monitor/hmp: Remove unnecessary 'monitor/monitor.h' header Philippe Mathieu-Daudé
2026-08-12 12:23 ` [PATCH 12/14] system: " Philippe Mathieu-Daudé
2026-08-12 12:23 ` Philippe Mathieu-Daudé [this message]
2026-08-12 13:09 ` [PATCH 13/14] system: Move runstate-related code from cpus.c to runstate.c Philippe Mathieu-Daudé
2026-08-12 12:23 ` [PATCH 14/14] system/dirtylimit: Extract HMP code to dirtylimit-hmp-cmds.c Philippe Mathieu-Daudé
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20260812122332.72975-14-philmd@oss.qualcomm.com \
--to=philmd@oss.qualcomm.com \
--cc=armbru@redhat.com \
--cc=dave@treblig.org \
--cc=marcandre.lureau@redhat.com \
--cc=pbonzini@redhat.com \
--cc=philmd@mailo.com \
--cc=qemu-devel@nongnu.org \
--cc=richard.henderson@linaro.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.