From: Dan Carpenter <dan@pereq.eu>
To: Marc Zyngier <maz@kernel.org>, Oliver Upton <oupton@kernel.org>,
Fuad Tabba <fuad.tabba@linux.dev>,
Joey Gouly <joey.gouly@arm.com>,
Steffen Eiden <seiden@linux.ibm.com>,
Suzuki K Poulose <suzuki.poulose@arm.com>,
Zenghui Yu <yuzenghui@huawei.com>,
Catalin Marinas <catalin.marinas@arm.com>,
Will Deacon <will@kernel.org>,
Mark Rutland <mark.rutland@arm.com>,
Christoffer Dall <c.dall@virtualopensystems.com>,
Rusty Russell <rusty@rustcorp.com.au>,
Marcelo Tosatti <mtosatti@redhat.com>,
Sebastian Ott <sebott@redhat.com>,
Eric Auger <eric.auger@redhat.com>,
James Morse <james.morse@arm.com>,
Paolo Bonzini <pbonzini@redhat.com>,
Sean Christopherson <seanjc@google.com>,
Shuah Khan <shuah@kernel.org>
Cc: "Karthik Bhargavan" <karthik@pereq.eu>,
"Clément Blaudeau" <clement@cryspen.com>,
linux-arm-kernel@lists.infradead.org, kvmarm@lists.linux.dev,
"Dan Carpenter" <dan@pereq.eu>,
linux-kernel@vger.kernel.org, kvm@vger.kernel.org,
linux-kselftest@vger.kernel.org
Subject: [PATCH v2 8/8] KVM: arm64: selftests: Test that a vcpu reset clears the vcpu's state
Date: Wed, 25 Nov 2026 09:56:23 +0300 [thread overview]
Message-ID: <20261125-double_exception-v2-8-224689d8a764@pereq.eu> (raw)
In-Reply-To: <20261125-double_exception-v2-0-224689d8a764@pereq.eu>
Add a test to check that KVM_ARM_VCPU_INIT (which resets a vcpu) has
reset all the things it was supposed to.
The test dirties as much of the vcpu state as it can, resets the vcpu,
and compares every register in KVM_GET_REG_LIST with a freshly
initialised vcpu. The are a few things which we must not clear such as
MPIDR_EL1, which userspace configures and the reset keeps, and the
CLIDR_EL1 and CCSIDR counters which keep counting.
The test runs with and without EL2.
Signed-off-by: Dan Carpenter <dan@pereq.eu>
---
tools/testing/selftests/kvm/Makefile.kvm | 1 +
tools/testing/selftests/kvm/arm64/vcpu_reset.c | 210 +++++++++++++++++++++++++
2 files changed, 211 insertions(+)
diff --git a/tools/testing/selftests/kvm/Makefile.kvm b/tools/testing/selftests/kvm/Makefile.kvm
index 95867977beb3..cbd05070a2e3 100644
--- a/tools/testing/selftests/kvm/Makefile.kvm
+++ b/tools/testing/selftests/kvm/Makefile.kvm
@@ -201,6 +201,7 @@ TEST_GEN_PROGS_arm64 += arm64/sea_to_user
TEST_GEN_PROGS_arm64 += arm64/set_id_regs
TEST_GEN_PROGS_arm64 += arm64/smccc_filter
TEST_GEN_PROGS_arm64 += arm64/stage2_block_transitions
+TEST_GEN_PROGS_arm64 += arm64/vcpu_reset
TEST_GEN_PROGS_arm64 += arm64/vcpu_width_config
TEST_GEN_PROGS_arm64 += arm64/vgic_init
TEST_GEN_PROGS_arm64 += arm64/vgic_irq
diff --git a/tools/testing/selftests/kvm/arm64/vcpu_reset.c b/tools/testing/selftests/kvm/arm64/vcpu_reset.c
new file mode 100644
index 000000000000..cdc051e4b897
--- /dev/null
+++ b/tools/testing/selftests/kvm/arm64/vcpu_reset.c
@@ -0,0 +1,210 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/*
+ * vcpu_reset - Test that resetting a vcpu that has run with KVM_ARM_VCPU_INIT
+ * puts it back in the state of a new vcpu.
+ *
+ * Before the reset, the vcpu has an MMIO read waiting for userspace and a
+ * pending SError, and userspace has written to every register it can. After
+ * the reset, every register must match a new vcpu's, except the ones in
+ * preserved_regs[], which must keep the value userspace wrote, and the
+ * counters, which keep counting. No SError may be pending, and the next
+ * KVM_RUN must not complete the MMIO read.
+ */
+#include "kvm_util.h"
+#include "processor.h"
+#include "test_util.h"
+
+#define MMIO_ADDR 0x8000000ULL
+#define MAX_REG_SIZE 256
+
+/*
+ * Registers that a reset keeps: the configuration that userspace sets for the
+ * vcpu, and the counters, which are VM-wide and change on their own.
+ */
+#define DEMUX_CCSIDR (KVM_REG_ARM64 | KVM_REG_SIZE_U32 | KVM_REG_ARM_DEMUX | \
+ KVM_REG_ARM_DEMUX_ID_CCSIDR)
+
+static bool is_preserved(u64 id)
+{
+ if ((id & ~(u64)KVM_REG_ARM_DEMUX_VAL_MASK) == DEMUX_CCSIDR)
+ return true;
+
+ switch (id) {
+ case KVM_ARM64_SYS_REG(SYS_MPIDR_EL1):
+ case KVM_ARM64_SYS_REG(SYS_CLIDR_EL1):
+ return true;
+ }
+ return false;
+}
+
+static bool is_counter(u64 id)
+{
+ return id == KVM_REG_ARM_TIMER_CNT || id == KVM_REG_ARM_PTIMER_CNT;
+}
+
+static void guest_code(void)
+{
+ READ_ONCE(*(u64 *)MMIO_ADDR);
+ GUEST_FAIL("The MMIO read completed");
+}
+
+struct regs {
+ struct kvm_reg_list *list;
+ u8 (*val)[MAX_REG_SIZE];
+};
+
+static void get_regs(struct kvm_vcpu *vcpu, struct regs *r)
+{
+ u64 i;
+
+ r->list = vcpu_get_reg_list(vcpu);
+ r->val = calloc(r->list->n, MAX_REG_SIZE);
+ TEST_ASSERT(r->val, "calloc() failed");
+ for (i = 0; i < r->list->n; i++) {
+ TEST_ASSERT(KVM_REG_SIZE(r->list->reg[i]) <= MAX_REG_SIZE,
+ "Register 0x%llx is too big", r->list->reg[i]);
+ TEST_ASSERT(!__vcpu_get_reg(vcpu, r->list->reg[i], r->val[i]),
+ "Can't read register 0x%llx", r->list->reg[i]);
+ }
+}
+
+static void free_regs(struct regs *r)
+{
+ free(r->list);
+ free(r->val);
+}
+
+/* Write the inverse of each register, or failing that, flip its bit 0 */
+static void dirty_regs(struct kvm_vcpu *vcpu)
+{
+ struct kvm_reg_list *list = vcpu_get_reg_list(vcpu);
+ u8 val[MAX_REG_SIZE];
+ struct kvm_one_reg reg = { .addr = (u64)val };
+ u64 i, j, size;
+
+ for (i = 0; i < list->n; i++) {
+ reg.id = list->reg[i];
+ size = KVM_REG_SIZE(reg.id);
+ if (__vcpu_get_reg(vcpu, reg.id, val))
+ continue;
+ for (j = 0; j < size; j++)
+ val[j] = ~val[j];
+ if (!__vcpu_ioctl(vcpu, KVM_SET_ONE_REG, ®))
+ continue;
+ for (j = 0; j < size; j++)
+ val[j] = ~val[j];
+ val[0] ^= 1;
+ __vcpu_ioctl(vcpu, KVM_SET_ONE_REG, ®);
+ }
+ free(list);
+}
+
+static void assert_reg_eq(u64 id, const u8 *got, const u8 *want, const char *what)
+{
+ u64 size = KVM_REG_SIZE(id);
+ u64 g = 0, w = 0;
+
+ if (!memcmp(got, want, size))
+ return;
+
+ memcpy(&g, got, size < 8 ? size : 8);
+ memcpy(&w, want, size < 8 ? size : 8);
+ TEST_FAIL("Register 0x%llx is 0x%lx after the reset, %s 0x%lx",
+ (unsigned long long)id, (unsigned long)g, what, (unsigned long)w);
+}
+
+static struct kvm_vm *create_vcpu(bool el2, struct kvm_vcpu **vcpu)
+{
+ struct kvm_vcpu_init init;
+ struct kvm_vm *vm;
+
+ vm = vm_create(1);
+ kvm_get_default_vcpu_target(vm, &init);
+ if (el2)
+ init.features[0] |= BIT(KVM_ARM_VCPU_HAS_EL2);
+ else
+ init.features[0] &= ~BIT(KVM_ARM_VCPU_HAS_EL2);
+ *vcpu = aarch64_vcpu_add(vm, 0, &init, guest_code);
+ kvm_arch_vm_finalize_vcpus(vm);
+ return vm;
+}
+
+static void test_reset(bool el2)
+{
+ struct kvm_vcpu_events events = {};
+ struct regs new, before, after;
+ struct kvm_vcpu *vcpu, *ref;
+ struct kvm_vm *vm, *ref_vm;
+ u64 i, id;
+
+ /*
+ * A new vcpu: initialised again to undo what the library set up, and
+ * through its first KVM_RUN without running any code.
+ */
+ ref_vm = create_vcpu(el2, &ref);
+ vcpu_ioctl(ref, KVM_ARM_VCPU_INIT, &ref->init);
+ vcpu_run_complete_io(ref);
+ get_regs(ref, &new);
+
+ vm = create_vcpu(el2, &vcpu);
+ virt_map(vm, MMIO_ADDR, MMIO_ADDR, 1);
+
+ vcpu_run(vcpu);
+ TEST_ASSERT_KVM_EXIT_REASON(vcpu, KVM_EXIT_MMIO);
+ TEST_ASSERT(!vcpu->run->mmio.is_write, "Expected an MMIO read");
+ memset(vcpu->run->mmio.data, 0x5a, sizeof(vcpu->run->mmio.data));
+
+ events.exception.serror_pending = true;
+ vcpu_events_set(vcpu, &events);
+
+ dirty_regs(vcpu);
+ get_regs(vcpu, &before);
+
+ vcpu_ioctl(vcpu, KVM_ARM_VCPU_INIT, &vcpu->init);
+
+ vcpu_events_get(vcpu, &events);
+ TEST_ASSERT(!events.exception.serror_pending, "An SError is still pending");
+
+ /* Completes the MMIO read if it is still pending */
+ vcpu_run_complete_io(vcpu);
+ get_regs(vcpu, &after);
+
+ TEST_ASSERT_EQ(new.list->n, after.list->n);
+ TEST_ASSERT_EQ(before.list->n, after.list->n);
+ for (i = 0; i < after.list->n; i++) {
+ id = after.list->reg[i];
+ TEST_ASSERT_EQ(id, new.list->reg[i]);
+ TEST_ASSERT_EQ(id, before.list->reg[i]);
+
+ if (is_counter(id))
+ continue;
+ if (is_preserved(id))
+ assert_reg_eq(id, after.val[i], before.val[i], "userspace wrote");
+ else
+ assert_reg_eq(id, after.val[i], new.val[i], "a new vcpu has");
+ }
+
+ free_regs(&new);
+ free_regs(&before);
+ free_regs(&after);
+ kvm_vm_free(vm);
+ kvm_vm_free(ref_vm);
+}
+
+int main(void)
+{
+ bool el2 = test_supports_el2();
+
+ ksft_print_header();
+ ksft_set_plan(el2 ? 2 : 1);
+
+ test_reset(false);
+ ksft_test_result_pass("reset without EL2\n");
+
+ if (el2) {
+ test_reset(true);
+ ksft_test_result_pass("reset with EL2\n");
+ }
+
+ ksft_finished();
+}
--
2.53.0
prev parent reply other threads:[~2026-11-25 6:56 UTC|newest]
Thread overview: 22+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-25 6:56 [PATCH v2 0/8] KVM: arm64: Fix kvm_reset_vcpu() and SError handling for nested virt Dan Carpenter
2026-11-25 6:56 ` [PATCH v2 1/8] KVM: arm64: Drop an incomplete MMIO access when resetting a vcpu Dan Carpenter
2026-11-25 6:56 ` [PATCH v2 2/8] KVM: arm64: Reset SP_EL1, ELR_EL1 and SPSR_EL1 " Dan Carpenter
2026-11-25 7:15 ` sashiko-bot
2026-11-25 9:14 ` Dan Carpenter
2026-11-25 9:18 ` Dan Carpenter
2026-11-26 13:37 ` Steffen Eiden
2026-11-26 14:04 ` Dan Carpenter
2026-11-25 9:08 ` Suzuki K Poulose
2026-11-25 9:32 ` Dan Carpenter
2026-11-25 6:56 ` [PATCH v2 3/8] KVM: arm64: Discard a pending SError " Dan Carpenter
2026-11-25 6:56 ` [PATCH v2 4/8] KVM: arm64: nv: Discard nested SError in kvm_reset_vcpu() Dan Carpenter
2026-11-25 6:56 ` [PATCH v2 5/8] KVM: arm64: nv: Replace a pending SError when injecting a new one Dan Carpenter
2026-11-25 8:00 ` Oliver Upton
2026-11-25 9:51 ` Dan Carpenter
2026-11-25 6:56 ` [PATCH v2 6/8] KVM: arm64: nv: Don't deliver a pending SError to the wrong guest Dan Carpenter
2026-11-25 7:24 ` sashiko-bot
2026-11-25 8:04 ` Oliver Upton
2026-11-25 11:11 ` Dan Carpenter
2026-11-25 11:05 ` Dan Carpenter
2026-11-25 6:56 ` [PATCH v2 7/8] KVM: arm64: nv: Re-inject a stored SError after the exit is handled Dan Carpenter
2026-11-25 6:56 ` Dan Carpenter [this message]
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=20261125-double_exception-v2-8-224689d8a764@pereq.eu \
--to=dan@pereq.eu \
--cc=c.dall@virtualopensystems.com \
--cc=catalin.marinas@arm.com \
--cc=clement@cryspen.com \
--cc=eric.auger@redhat.com \
--cc=fuad.tabba@linux.dev \
--cc=james.morse@arm.com \
--cc=joey.gouly@arm.com \
--cc=karthik@pereq.eu \
--cc=kvm@vger.kernel.org \
--cc=kvmarm@lists.linux.dev \
--cc=linux-arm-kernel@lists.infradead.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-kselftest@vger.kernel.org \
--cc=mark.rutland@arm.com \
--cc=maz@kernel.org \
--cc=mtosatti@redhat.com \
--cc=oupton@kernel.org \
--cc=pbonzini@redhat.com \
--cc=rusty@rustcorp.com.au \
--cc=seanjc@google.com \
--cc=sebott@redhat.com \
--cc=seiden@linux.ibm.com \
--cc=shuah@kernel.org \
--cc=suzuki.poulose@arm.com \
--cc=will@kernel.org \
--cc=yuzenghui@huawei.com \
/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.