From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 3B089606A32D for ; Fri, 4 Dec 2026 10:49:33 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=lists.infradead.org header.i=@lists.infradead.org header.a=rsa-sha256 header.s=bombadil.20210309 header.b=FB5Zd9G5; dkim=pass (1024-bit key, unprotected) header.d=linux.dev header.i=@linux.dev header.a=rsa-sha256 header.s=key1 header.b=dFMzFZos DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender:List-Subscribe:List-Help :List-Post:List-Archive:List-Unsubscribe:List-Id:Content-Transfer-Encoding: MIME-Version:References:In-Reply-To:Message-ID:Date:Subject:Cc:To:From: Reply-To:Content-Type:Content-ID:Content-Description:Resent-Date:Resent-From: Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Unsubscribe-Post: List-Owner; bh=N3ZsOwOZRUBhP41WGsK1mRE2o+8kAOeRn5DcVV2byk4=; b=FB5Zd9G5vRofoi 529p7BTahkjRqBPJc3FomLt0aAiNGQIeH+rzqFZcaQElyiX/3tGumaqroamiEZ0TabgTFKuHCMl0I h41NzcWNHMCLZXw20WuH7ovdQf5suqg1kfJEpDwMyEB7ncrxgcJW+7tgl/1OSxCr7olk6+kM17TbP XNik61T2vhHjKKNheCIcFmBT6td52VvSOhR/izNUIvauw1FCwTpDn1BjEUOmSQCB4AT0XnbTXip4S erk8p2bKqsPLx3FaaEvrf9QJwRs+kTSkqjew3IBKW9jscG56tfdTXPxC5Qn7hYXziidriMd2m4r98 jx65Ff0SAb92C3HAdEwA==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.100.1 #2 (Red Hat Linux)) id 1xZQr1-000000002Mh-0lRO; Fri, 04 Dec 2026 10:49:27 +0000 Received: from out-171.mta1.migadu.com ([2001:41d0:203:375::ab] helo=mta1.migadu.com) by bombadil.infradead.org with esmtps (Exim 4.100.1 #2 (Red Hat Linux)) id 1xZQqw-000000002Kj-3uiW for linux-arm-kernel@lists.infradead.org; Fri, 04 Dec 2026 10:49:24 +0000 X-Envelope-To: linux-arm-kernel@lists.infradead.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: linux-arm-kernel@lists.infradead.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-Archived-By: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.9.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20261204_024923_116962_709691B9 X-CRM114-Status: GOOD ( 25.54 ) X-Spam-Score: -2.1 (--) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-arm-kernel" X-Spam-Status: No, score=-19.3 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,USER_AGENT_GIT autolearn=ham autolearn_force=no version=4.0.1 X-Spam-Checker-Version: SpamAssassin 4.0.1 (2024-03-26) on aws-us-west-2-korg-listserv-alma10-1 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