Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=91.218.175.175 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796381363; b=PnQagULoLNbPvxN0VJtVGbOyHcZLxwapAd/Q8h0Z/oYekHvpxJ9G1ft+HvwzRxMfEs1P DfrFwpB6eMt1kTXNnnQbmcwe4EdnIkawUzVf7Cum/Fea2g4b1fjFgOY5JNx6F/E15per/ QJW1rIIVaEVauRKdnzIuQRkOmRWrqT4JWCrhE/JE0SvX3HYonkFzUyWszP+bIg6dRcgOX YMKfyWWFDUHrM+Au5iw/g+7me7QKMilesNlRgpH+YCcWhtaFXipvExVMxkbrbf6iqsDLn P9LMsv01qETQQobpeYkpGBEsojWPxVpZumWtasnm7gJkiXDfDjzoxskT47yJYuTt0eA== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796381363; h=DMARC-Filter:X-Envelope-To:DKIM-Signature:X-Envelope-To:Received: X-Mizu-Trace-ID:X-Migadu-Flow:From:To:Cc:Subject:Date:Message-ID: X-Mailer:In-Reply-To:References:MIME-Version: Content-Transfer-Encoding; bh=N3ZsOwOZRUBhP41WGsK1mRE2o+8kAOeRn5DcVV2byk4=; b=tvK4VdpjzN32fPZvZIYk6HvI2wVOxtDPYSncJVmooH6pT73LUOyeLEdfUzFQxtYS5tBZ NNKDs4hcOU7Lotx6BYuQPT//bFF/n/e44u++dF8n63W7pa8FBMx3MkPYpTYI7U4RgcbAb EJGCE0Qse2HCmA2PQa6UyFC+sJ+NScybfswLM1okCynk2O475X3FqXPGOoBrD1jKjDijo 89TCDR4UW2fEgJBWNuAu1Ec9hQVZoaoyKLQcHk4ifq1whocJPopG6ANAa1ToNJ9qVeGK6 CGpllRnjIyCFwRHjep79z7Rlahxg26RQfPL2PuvG21c15Xlhx4Ej6AEVua8f61FevUQ== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=linux.dev; spf=pass smtp.mailfrom=linux.dev; dkim=pass header.d=linux.dev header.i=@linux.dev header.a=rsa-sha256 header.s=key1 header.b=dFMzFZos; arc=none smtp.remote-ip=91.218.175.175 Received: from mta0.migadu.com (out-175.mta0.migadu.com [91.218.175.175]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id C2A22606A33D for ; Fri, 4 Dec 2026 10:49:22 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org C2A22606A33D Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linux.dev Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key, unprotected) header.d=linux.dev header.i=@linux.dev header.a=rsa-sha256 header.s=key1 header.b=dFMzFZos X-Envelope-To: kvm@vger.kernel.org DKIM-Signature: a=rsa-sha256; bh=SN2zqarCNRMKSmPpveDMGcZLof9ja1hF9EfJsKYxAbM=; c=simple/simple; d=linux.dev; h=from:to:subject:date:message-id:mime-version:content-type; s=key1; t=1796381361; v=1; x=1796986161; b=dFMzFZosrKARSE90o834jBAGUO9isUAPznmrj5asbBQNkaAICkUkGBxd9eQd+yc3wCBH7XOh 3Ac8FGS694363wE2qiYohHcGwYoSlUstJWLl0PZ3Mk5J+8Vha5VHxG8ufstx7EsAHTLybM2iYsR lov1U+Y6AAUxJRhBEz4IkJcU= X-Envelope-To: kvm@vger.kernel.org Received: by smtp.migadu.com with ESMTPS id 13292667ff45b602; Fri, 04 Dec 2026 10:49:20 +0000 X-Mizu-Trace-ID: 13292667ff45b602 X-Migadu-Flow: FLOW_OUT From: Fuad Tabba To: Marc Zyngier , Oliver Upton Cc: kvmarm@lists.linux.dev, linux-arm-kernel@lists.infradead.org, kvm@vger.kernel.org, linux-kselftest@vger.kernel.org, Joey Gouly , Steffen Eiden , Suzuki K Poulose , Zenghui Yu , Catalin Marinas , Will Deacon , Mark Rutland , Vincent Donnefort , Paolo Bonzini , Sean Christopherson , Shuah Khan , James Clark , Fuad Tabba Subject: [PATCH 2/2] KVM: arm64: selftests: Test PMU counter overflow in vpmu_counter_access Date: Fri, 4 Dec 2026 10:49:11 +0000 Message-ID: <20261204104911.2797512-3-fuad.tabba@linux.dev> X-Mailer: git-send-email 2.47.3 In-Reply-To: <20261204104911.2797512-1-fuad.tabba@linux.dev> References: <20261204104911.2797512-1-fuad.tabba@linux.dev> X-Mailing-List: kvm@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: 8bit The vpmu_counter_access test accesses the guest's PMU registers, but doesn't check what happens when a counter overflows. Add a case where the guest lets its cycle counter overflow, and check that it takes the PMU interrupt with the counter's bit set in PMOVSSET_EL0, whether it waits without trapping or traps in a loop. Do the same for a chained pair of event counters, where the high counter must also go up by one. With the cycle counter's interrupt disabled, check that the guest reads the bit as set and doesn't take the interrupt. Add a define for the PMU's interrupt number now that the guest uses it. Signed-off-by: Fuad Tabba --- .../selftests/kvm/arm64/vpmu_counter_access.c | 214 +++++++++++++++++- 1 file changed, 213 insertions(+), 1 deletion(-) diff --git a/tools/testing/selftests/kvm/arm64/vpmu_counter_access.c b/tools/testing/selftests/kvm/arm64/vpmu_counter_access.c index 22223395969e0..57e2f39863767 100644 --- a/tools/testing/selftests/kvm/arm64/vpmu_counter_access.c +++ b/tools/testing/selftests/kvm/arm64/vpmu_counter_access.c @@ -10,14 +10,18 @@ * other counters. * It also checks if the userspace accesses to the PMU regsisters honor the * PMCR.N value that's set for the guest. + * Finally, it checks the overflow status and interrupt on a counter overflow. * This test runs only when KVM_CAP_ARM_PMU_V3 is supported on the host. */ +#include +#include #include #include #include #include #include #include +#include /* The max number of the PMU event counters (excluding the cycle counter) */ #define ARMV8_PMU_MAX_GENERAL_COUNTERS (ARMV8_PMU_MAX_COUNTERS - 1) @@ -25,6 +29,9 @@ /* The cycle counter bit position that's common among the PMU registers */ #define ARMV8_PMU_CYCLE_IDX 31 +/* The PPI that the PMU interrupt is delivered on */ +#define PMU_IRQ 23 + struct vpmu_vm { struct kvm_vm *vm; struct kvm_vcpu *vcpu; @@ -403,7 +410,7 @@ static void create_vpmu_vm(void *guest_code) { struct kvm_vcpu_init init; u8 pmuver, ec; - u64 dfr0, irq = 23; + u64 dfr0, irq = PMU_IRQ; struct kvm_device_attr irq_attr = { .group = KVM_ARM_VCPU_PMU_V3_CTRL, .attr = KVM_ARM_VCPU_PMU_V3_IRQ, @@ -596,6 +603,209 @@ static void run_error_test(u64 pmcr_n) destroy_vpmu_vm(); } +#define CYCLE_COUNTER BIT(ARMV8_PMU_CYCLE_IDX) +#define CHAIN_LOW BIT(0) +#define CHAIN_HIGH BIT(1) + +#define NR_OVERFLOWS 8 +#define CYCLES_TO_OVERFLOW 100000 +#define OVERFLOW_TIMEOUT_MS 2000 + +enum overflow_wait { + /* The guest doesn't trap, so it exits on the overflow interrupt. */ + WAIT_QUIET, + WAIT_TRAPPING, +}; + +static u64 irq_count, irq_ovs; + +static void guest_irq_handler(struct ex_regs *regs) +{ + unsigned int intid = gic_get_and_ack_irq(); + + if (intid == PMU_IRQ) { + WRITE_ONCE(irq_ovs, read_sysreg(pmovsclr_el0)); + WRITE_ONCE(irq_count, READ_ONCE(irq_count) + 1); + + write_sysreg(CYCLE_COUNTER | CHAIN_LOW | CHAIN_HIGH, pmovsclr_el0); + isb(); + } + + gic_set_eoi(intid); +} + +static void overflow_stop(u64 counters) +{ + write_sysreg(counters, pmcntenclr_el0); + write_sysreg(counters, pmintenclr_el1); + write_sysreg(counters, pmovsclr_el0); + isb(); +} + +static void overflow_start(u64 counters, u64 irq_counter) +{ + WRITE_ONCE(irq_count, 0); + WRITE_ONCE(irq_ovs, 0); + + write_sysreg(irq_counter, pmintenset_el1); + write_sysreg(counters, pmcntenset_el0); + isb(); +} + +static void wait_for_overflow(enum overflow_wait wait, u64 counter) +{ + u64 deadline = timer_get_cntct(VIRTUAL) + msec_to_cycles(OVERFLOW_TIMEOUT_MS); + struct arm_smccc_res res; + + while (!READ_ONCE(irq_count) && timer_get_cntct(VIRTUAL) < deadline) { + if (wait == WAIT_TRAPPING) + do_smccc(PSCI_0_2_FN_PSCI_VERSION, 0, 0, 0, 0, 0, 0, 0, &res); + } + + __GUEST_ASSERT(READ_ONCE(irq_count), + "No interrupt for the overflow of counter 0x%lx", counter); + __GUEST_ASSERT(READ_ONCE(irq_ovs) & counter, + "Overflow interrupt for counter 0x%lx with PMOVSCLR_EL0 = 0x%lx", + counter, READ_ONCE(irq_ovs)); +} + +static void test_cycle_counter_overflow(enum overflow_wait wait) +{ + int i; + + for (i = 0; i < NR_OVERFLOWS; i++) { + overflow_stop(CYCLE_COUNTER); + write_sysreg(-CYCLES_TO_OVERFLOW, pmccntr_el0); + overflow_start(CYCLE_COUNTER, CYCLE_COUNTER); + + wait_for_overflow(wait, CYCLE_COUNTER); + } + + overflow_stop(CYCLE_COUNTER); +} + +static void test_overflow_irq_disabled(void) +{ + u64 deadline, ovs; + int i; + + for (i = 0; i < NR_OVERFLOWS; i++) { + overflow_stop(CYCLE_COUNTER); + write_sysreg(-CYCLES_TO_OVERFLOW, pmccntr_el0); + overflow_start(CYCLE_COUNTER, 0); + + deadline = timer_get_cntct(VIRTUAL) + msec_to_cycles(OVERFLOW_TIMEOUT_MS); + do { + ovs = read_sysreg(pmovsclr_el0); + } while (!(ovs & CYCLE_COUNTER) && timer_get_cntct(VIRTUAL) < deadline); + + /* The handler clears the status bit, so check for an interrupt first. */ + __GUEST_ASSERT(!READ_ONCE(irq_count), + "Overflow interrupt for the cycle counter with its interrupt disabled"); + __GUEST_ASSERT(ovs & CYCLE_COUNTER, + "No overflow status for the cycle counter with its interrupt disabled"); + } + + overflow_stop(CYCLE_COUNTER); +} + +static void test_chained_counter_overflow(enum overflow_wait wait) +{ + u64 high; + int i; + + write_pmevtypern(0, ARMV8_PMU_INCLUDE_EL2 | ARMV8_PMUV3_PERFCTR_CPU_CYCLES); + write_pmevtypern(1, ARMV8_PMUV3_PERFCTR_CHAIN); + + for (i = 0; i < NR_OVERFLOWS; i++) { + overflow_stop(CHAIN_LOW | CHAIN_HIGH); + write_pmevcntrn(0, (u32)-CYCLES_TO_OVERFLOW); + write_pmevcntrn(1, 0); + overflow_start(CHAIN_LOW | CHAIN_HIGH, CHAIN_LOW); + + wait_for_overflow(wait, CHAIN_LOW); + + write_sysreg(CHAIN_LOW | CHAIN_HIGH, pmcntenclr_el0); + isb(); + high = read_pmevcntrn(1); + __GUEST_ASSERT(high == 1, + "Chained counter at %lu after one overflow of the counter below it", + high); + } + + overflow_stop(CHAIN_LOW | CHAIN_HIGH); +} + +static void guest_overflow_code(void) +{ + local_irq_disable(); + gic_init(GIC_V3, 1); + gic_irq_enable(PMU_IRQ); + + /* The bits for implemented counters are reset to UNKNOWN. */ + write_sysreg(~0ul, pmcntenclr_el0); + write_sysreg(~0ul, pmintenclr_el1); + write_sysreg(~0ul, pmovsclr_el0); + + /* On a host with NV, the guest runs at EL2, where a counter doesn't count by default. */ + write_sysreg(ARMV8_PMU_INCLUDE_EL2, pmccfiltr_el0); + + /* Leave PMCR_EL0.LP clear, since a counter chains only if it overflows at 32 bits. */ + write_sysreg(ARMV8_PMU_PMCR_E | ARMV8_PMU_PMCR_LC, pmcr_el0); + isb(); + local_irq_enable(); + + test_cycle_counter_overflow(WAIT_QUIET); + test_cycle_counter_overflow(WAIT_TRAPPING); + test_overflow_irq_disabled(); + + if (get_pmcr_n(read_sysreg(pmcr_el0)) >= 2 && + (read_sysreg(pmceid0_el0) & BIT(ARMV8_PMUV3_PERFCTR_CPU_CYCLES))) { + test_chained_counter_overflow(WAIT_QUIET); + test_chained_counter_overflow(WAIT_TRAPPING); + } else { + /* Tell the host that the chained counters weren't tested. */ + GUEST_SYNC(0); + } + + GUEST_DONE(); +} + +/* + * Create a guest with one vCPU, and check that it sees the overflow status for + * each counter overflow, and takes the PMU interrupt only if it's enabled. + */ +static void run_overflow_test(void) +{ + struct kvm_vcpu *vcpu; + struct ucall uc; + + create_vpmu_vm(guest_overflow_code); + vcpu = vpmu_vm.vcpu; + vm_install_exception_handler(vpmu_vm.vm, VECTOR_IRQ_CURRENT, guest_irq_handler); + vcpu_device_attr_set(vcpu, KVM_ARM_VCPU_PMU_V3_CTRL, KVM_ARM_VCPU_PMU_V3_INIT, NULL); + + do { + vcpu_run(vcpu); + + switch (get_ucall(vcpu, &uc)) { + case UCALL_ABORT: + REPORT_GUEST_ASSERT(uc); + break; + case UCALL_SYNC: + pr_info("Chained counter overflow not tested: needs two event counters and CPU_CYCLES\n"); + break; + case UCALL_DONE: + break; + default: + TEST_FAIL("Unknown ucall %lu", uc.cmd); + break; + } + } while (uc.cmd != UCALL_DONE); + + destroy_vpmu_vm(); +} + /* * Return the default number of implemented PMU event counters excluding * the cycle counter (i.e. PMCR_EL0.N value) for the guest. @@ -639,5 +849,7 @@ int main(void) for (i = pmcr_n + 1; i < ARMV8_PMU_MAX_COUNTERS; i++) run_error_test(i); + run_overflow_test(); + return 0; } -- 2.47.3