Path: blob/master/tools/testing/selftests/kvm/arm64/vpmu_counter_access.c
38237 views
// SPDX-License-Identifier: GPL-2.0-only1/*2* vpmu_counter_access - Test vPMU event counter access3*4* Copyright (c) 2023 Google LLC.5*6* This test checks if the guest can see the same number of the PMU event7* counters (PMCR_EL0.N) that userspace sets, if the guest can access8* those counters, and if the guest is prevented from accessing any9* other counters.10* It also checks if the userspace accesses to the PMU regsisters honor the11* PMCR.N value that's set for the guest.12* This test runs only when KVM_CAP_ARM_PMU_V3 is supported on the host.13*/14#include <kvm_util.h>15#include <processor.h>16#include <test_util.h>17#include <vgic.h>18#include <perf/arm_pmuv3.h>19#include <linux/bitfield.h>2021/* The max number of the PMU event counters (excluding the cycle counter) */22#define ARMV8_PMU_MAX_GENERAL_COUNTERS (ARMV8_PMU_MAX_COUNTERS - 1)2324/* The cycle counter bit position that's common among the PMU registers */25#define ARMV8_PMU_CYCLE_IDX 312627struct vpmu_vm {28struct kvm_vm *vm;29struct kvm_vcpu *vcpu;30};3132static struct vpmu_vm vpmu_vm;3334struct pmreg_sets {35uint64_t set_reg_id;36uint64_t clr_reg_id;37};3839#define PMREG_SET(set, clr) {.set_reg_id = set, .clr_reg_id = clr}4041static uint64_t get_pmcr_n(uint64_t pmcr)42{43return FIELD_GET(ARMV8_PMU_PMCR_N, pmcr);44}4546static uint64_t get_counters_mask(uint64_t n)47{48uint64_t mask = BIT(ARMV8_PMU_CYCLE_IDX);4950if (n)51mask |= GENMASK(n - 1, 0);52return mask;53}5455/* Read PMEVTCNTR<n>_EL0 through PMXEVCNTR_EL0 */56static inline unsigned long read_sel_evcntr(int sel)57{58write_sysreg(sel, pmselr_el0);59isb();60return read_sysreg(pmxevcntr_el0);61}6263/* Write PMEVTCNTR<n>_EL0 through PMXEVCNTR_EL0 */64static inline void write_sel_evcntr(int sel, unsigned long val)65{66write_sysreg(sel, pmselr_el0);67isb();68write_sysreg(val, pmxevcntr_el0);69isb();70}7172/* Read PMEVTYPER<n>_EL0 through PMXEVTYPER_EL0 */73static inline unsigned long read_sel_evtyper(int sel)74{75write_sysreg(sel, pmselr_el0);76isb();77return read_sysreg(pmxevtyper_el0);78}7980/* Write PMEVTYPER<n>_EL0 through PMXEVTYPER_EL0 */81static inline void write_sel_evtyper(int sel, unsigned long val)82{83write_sysreg(sel, pmselr_el0);84isb();85write_sysreg(val, pmxevtyper_el0);86isb();87}8889static void pmu_disable_reset(void)90{91uint64_t pmcr = read_sysreg(pmcr_el0);9293/* Reset all counters, disabling them */94pmcr &= ~ARMV8_PMU_PMCR_E;95write_sysreg(pmcr | ARMV8_PMU_PMCR_P, pmcr_el0);96isb();97}9899#define RETURN_READ_PMEVCNTRN(n) \100return read_sysreg(pmevcntr##n##_el0)101static unsigned long read_pmevcntrn(int n)102{103PMEVN_SWITCH(n, RETURN_READ_PMEVCNTRN);104return 0;105}106107#define WRITE_PMEVCNTRN(n) \108write_sysreg(val, pmevcntr##n##_el0)109static void write_pmevcntrn(int n, unsigned long val)110{111PMEVN_SWITCH(n, WRITE_PMEVCNTRN);112isb();113}114115#define READ_PMEVTYPERN(n) \116return read_sysreg(pmevtyper##n##_el0)117static unsigned long read_pmevtypern(int n)118{119PMEVN_SWITCH(n, READ_PMEVTYPERN);120return 0;121}122123#define WRITE_PMEVTYPERN(n) \124write_sysreg(val, pmevtyper##n##_el0)125static void write_pmevtypern(int n, unsigned long val)126{127PMEVN_SWITCH(n, WRITE_PMEVTYPERN);128isb();129}130131/*132* The pmc_accessor structure has pointers to PMEV{CNTR,TYPER}<n>_EL0133* accessors that test cases will use. Each of the accessors will134* either directly reads/writes PMEV{CNTR,TYPER}<n>_EL0135* (i.e. {read,write}_pmev{cnt,type}rn()), or reads/writes them through136* PMXEV{CNTR,TYPER}_EL0 (i.e. {read,write}_sel_ev{cnt,type}r()).137*138* This is used to test that combinations of those accessors provide139* the consistent behavior.140*/141struct pmc_accessor {142/* A function to be used to read PMEVTCNTR<n>_EL0 */143unsigned long (*read_cntr)(int idx);144/* A function to be used to write PMEVTCNTR<n>_EL0 */145void (*write_cntr)(int idx, unsigned long val);146/* A function to be used to read PMEVTYPER<n>_EL0 */147unsigned long (*read_typer)(int idx);148/* A function to be used to write PMEVTYPER<n>_EL0 */149void (*write_typer)(int idx, unsigned long val);150};151152struct pmc_accessor pmc_accessors[] = {153/* test with all direct accesses */154{ read_pmevcntrn, write_pmevcntrn, read_pmevtypern, write_pmevtypern },155/* test with all indirect accesses */156{ read_sel_evcntr, write_sel_evcntr, read_sel_evtyper, write_sel_evtyper },157/* read with direct accesses, and write with indirect accesses */158{ read_pmevcntrn, write_sel_evcntr, read_pmevtypern, write_sel_evtyper },159/* read with indirect accesses, and write with direct accesses */160{ read_sel_evcntr, write_pmevcntrn, read_sel_evtyper, write_pmevtypern },161};162163/*164* Convert a pointer of pmc_accessor to an index in pmc_accessors[],165* assuming that the pointer is one of the entries in pmc_accessors[].166*/167#define PMC_ACC_TO_IDX(acc) (acc - &pmc_accessors[0])168169#define GUEST_ASSERT_BITMAP_REG(regname, mask, set_expected) \170{ \171uint64_t _tval = read_sysreg(regname); \172\173if (set_expected) \174__GUEST_ASSERT((_tval & mask), \175"tval: 0x%lx; mask: 0x%lx; set_expected: %u", \176_tval, mask, set_expected); \177else \178__GUEST_ASSERT(!(_tval & mask), \179"tval: 0x%lx; mask: 0x%lx; set_expected: %u", \180_tval, mask, set_expected); \181}182183/*184* Check if @mask bits in {PMCNTEN,PMINTEN,PMOVS}{SET,CLR} registers185* are set or cleared as specified in @set_expected.186*/187static void check_bitmap_pmu_regs(uint64_t mask, bool set_expected)188{189GUEST_ASSERT_BITMAP_REG(pmcntenset_el0, mask, set_expected);190GUEST_ASSERT_BITMAP_REG(pmcntenclr_el0, mask, set_expected);191GUEST_ASSERT_BITMAP_REG(pmintenset_el1, mask, set_expected);192GUEST_ASSERT_BITMAP_REG(pmintenclr_el1, mask, set_expected);193GUEST_ASSERT_BITMAP_REG(pmovsset_el0, mask, set_expected);194GUEST_ASSERT_BITMAP_REG(pmovsclr_el0, mask, set_expected);195}196197/*198* Check if the bit in {PMCNTEN,PMINTEN,PMOVS}{SET,CLR} registers corresponding199* to the specified counter (@pmc_idx) can be read/written as expected.200* When @set_op is true, it tries to set the bit for the counter in201* those registers by writing the SET registers (the bit won't be set202* if the counter is not implemented though).203* Otherwise, it tries to clear the bits in the registers by writing204* the CLR registers.205* Then, it checks if the values indicated in the registers are as expected.206*/207static void test_bitmap_pmu_regs(int pmc_idx, bool set_op)208{209uint64_t pmcr_n, test_bit = BIT(pmc_idx);210bool set_expected = false;211212if (set_op) {213write_sysreg(test_bit, pmcntenset_el0);214write_sysreg(test_bit, pmintenset_el1);215write_sysreg(test_bit, pmovsset_el0);216217/* The bit will be set only if the counter is implemented */218pmcr_n = get_pmcr_n(read_sysreg(pmcr_el0));219set_expected = (pmc_idx < pmcr_n) ? true : false;220} else {221write_sysreg(test_bit, pmcntenclr_el0);222write_sysreg(test_bit, pmintenclr_el1);223write_sysreg(test_bit, pmovsclr_el0);224}225check_bitmap_pmu_regs(test_bit, set_expected);226}227228/*229* Tests for reading/writing registers for the (implemented) event counter230* specified by @pmc_idx.231*/232static void test_access_pmc_regs(struct pmc_accessor *acc, int pmc_idx)233{234uint64_t write_data, read_data;235236/* Disable all PMCs and reset all PMCs to zero. */237pmu_disable_reset();238239/*240* Tests for reading/writing {PMCNTEN,PMINTEN,PMOVS}{SET,CLR}_EL1.241*/242243/* Make sure that the bit in those registers are set to 0 */244test_bitmap_pmu_regs(pmc_idx, false);245/* Test if setting the bit in those registers works */246test_bitmap_pmu_regs(pmc_idx, true);247/* Test if clearing the bit in those registers works */248test_bitmap_pmu_regs(pmc_idx, false);249250/*251* Tests for reading/writing the event type register.252*/253254/*255* Set the event type register to an arbitrary value just for testing256* of reading/writing the register.257* Arm ARM says that for the event from 0x0000 to 0x003F,258* the value indicated in the PMEVTYPER<n>_EL0.evtCount field is259* the value written to the field even when the specified event260* is not supported.261*/262write_data = (ARMV8_PMU_EXCLUDE_EL1 | ARMV8_PMUV3_PERFCTR_INST_RETIRED);263acc->write_typer(pmc_idx, write_data);264read_data = acc->read_typer(pmc_idx);265__GUEST_ASSERT(read_data == write_data,266"pmc_idx: 0x%x; acc_idx: 0x%lx; read_data: 0x%lx; write_data: 0x%lx",267pmc_idx, PMC_ACC_TO_IDX(acc), read_data, write_data);268269/*270* Tests for reading/writing the event count register.271*/272273read_data = acc->read_cntr(pmc_idx);274275/* The count value must be 0, as it is disabled and reset */276__GUEST_ASSERT(read_data == 0,277"pmc_idx: 0x%x; acc_idx: 0x%lx; read_data: 0x%lx",278pmc_idx, PMC_ACC_TO_IDX(acc), read_data);279280write_data = read_data + pmc_idx + 0x12345;281acc->write_cntr(pmc_idx, write_data);282read_data = acc->read_cntr(pmc_idx);283__GUEST_ASSERT(read_data == write_data,284"pmc_idx: 0x%x; acc_idx: 0x%lx; read_data: 0x%lx; write_data: 0x%lx",285pmc_idx, PMC_ACC_TO_IDX(acc), read_data, write_data);286}287288#define INVALID_EC (-1ul)289uint64_t expected_ec = INVALID_EC;290291static void guest_sync_handler(struct ex_regs *regs)292{293uint64_t esr, ec;294295esr = read_sysreg(esr_el1);296ec = ESR_ELx_EC(esr);297298__GUEST_ASSERT(expected_ec == ec,299"PC: 0x%lx; ESR: 0x%lx; EC: 0x%lx; EC expected: 0x%lx",300regs->pc, esr, ec, expected_ec);301302/* skip the trapping instruction */303regs->pc += 4;304305/* Use INVALID_EC to indicate an exception occurred */306expected_ec = INVALID_EC;307}308309/*310* Run the given operation that should trigger an exception with the311* given exception class. The exception handler (guest_sync_handler)312* will reset op_end_addr to 0, expected_ec to INVALID_EC, and skip313* the instruction that trapped.314*/315#define TEST_EXCEPTION(ec, ops) \316({ \317GUEST_ASSERT(ec != INVALID_EC); \318WRITE_ONCE(expected_ec, ec); \319dsb(ish); \320ops; \321GUEST_ASSERT(expected_ec == INVALID_EC); \322})323324/*325* Tests for reading/writing registers for the unimplemented event counter326* specified by @pmc_idx (>= PMCR_EL0.N).327*/328static void test_access_invalid_pmc_regs(struct pmc_accessor *acc, int pmc_idx)329{330/*331* Reading/writing the event count/type registers should cause332* an UNDEFINED exception.333*/334TEST_EXCEPTION(ESR_ELx_EC_UNKNOWN, acc->read_cntr(pmc_idx));335TEST_EXCEPTION(ESR_ELx_EC_UNKNOWN, acc->write_cntr(pmc_idx, 0));336TEST_EXCEPTION(ESR_ELx_EC_UNKNOWN, acc->read_typer(pmc_idx));337TEST_EXCEPTION(ESR_ELx_EC_UNKNOWN, acc->write_typer(pmc_idx, 0));338/*339* The bit corresponding to the (unimplemented) counter in340* {PMCNTEN,PMINTEN,PMOVS}{SET,CLR} registers should be RAZ.341*/342test_bitmap_pmu_regs(pmc_idx, 1);343test_bitmap_pmu_regs(pmc_idx, 0);344}345346/*347* The guest is configured with PMUv3 with @expected_pmcr_n number of348* event counters.349* Check if @expected_pmcr_n is consistent with PMCR_EL0.N, and350* if reading/writing PMU registers for implemented or unimplemented351* counters works as expected.352*/353static void guest_code(uint64_t expected_pmcr_n)354{355uint64_t pmcr, pmcr_n, unimp_mask;356int i, pmc;357358__GUEST_ASSERT(expected_pmcr_n <= ARMV8_PMU_MAX_GENERAL_COUNTERS,359"Expected PMCR.N: 0x%lx; ARMv8 general counters: 0x%x",360expected_pmcr_n, ARMV8_PMU_MAX_GENERAL_COUNTERS);361362pmcr = read_sysreg(pmcr_el0);363pmcr_n = get_pmcr_n(pmcr);364365/* Make sure that PMCR_EL0.N indicates the value userspace set */366__GUEST_ASSERT(pmcr_n == expected_pmcr_n,367"Expected PMCR.N: 0x%lx, PMCR.N: 0x%lx",368expected_pmcr_n, pmcr_n);369370/*371* Make sure that (RAZ) bits corresponding to unimplemented event372* counters in {PMCNTEN,PMINTEN,PMOVS}{SET,CLR} registers are reset373* to zero.374* (NOTE: bits for implemented event counters are reset to UNKNOWN)375*/376unimp_mask = GENMASK_ULL(ARMV8_PMU_MAX_GENERAL_COUNTERS - 1, pmcr_n);377check_bitmap_pmu_regs(unimp_mask, false);378379/*380* Tests for reading/writing PMU registers for implemented counters.381* Use each combination of PMEV{CNTR,TYPER}<n>_EL0 accessor functions.382*/383for (i = 0; i < ARRAY_SIZE(pmc_accessors); i++) {384for (pmc = 0; pmc < pmcr_n; pmc++)385test_access_pmc_regs(&pmc_accessors[i], pmc);386}387388/*389* Tests for reading/writing PMU registers for unimplemented counters.390* Use each combination of PMEV{CNTR,TYPER}<n>_EL0 accessor functions.391*/392for (i = 0; i < ARRAY_SIZE(pmc_accessors); i++) {393for (pmc = pmcr_n; pmc < ARMV8_PMU_MAX_GENERAL_COUNTERS; pmc++)394test_access_invalid_pmc_regs(&pmc_accessors[i], pmc);395}396397GUEST_DONE();398}399400/* Create a VM that has one vCPU with PMUv3 configured. */401static void create_vpmu_vm(void *guest_code)402{403struct kvm_vcpu_init init;404uint8_t pmuver, ec;405uint64_t dfr0, irq = 23;406struct kvm_device_attr irq_attr = {407.group = KVM_ARM_VCPU_PMU_V3_CTRL,408.attr = KVM_ARM_VCPU_PMU_V3_IRQ,409.addr = (uint64_t)&irq,410};411412/* The test creates the vpmu_vm multiple times. Ensure a clean state */413memset(&vpmu_vm, 0, sizeof(vpmu_vm));414415vpmu_vm.vm = vm_create(1);416vm_init_descriptor_tables(vpmu_vm.vm);417for (ec = 0; ec < ESR_ELx_EC_MAX + 1; ec++) {418vm_install_sync_handler(vpmu_vm.vm, VECTOR_SYNC_CURRENT, ec,419guest_sync_handler);420}421422/* Create vCPU with PMUv3 */423kvm_get_default_vcpu_target(vpmu_vm.vm, &init);424init.features[0] |= (1 << KVM_ARM_VCPU_PMU_V3);425vpmu_vm.vcpu = aarch64_vcpu_add(vpmu_vm.vm, 0, &init, guest_code);426vcpu_init_descriptor_tables(vpmu_vm.vcpu);427428kvm_arch_vm_finalize_vcpus(vpmu_vm.vm);429430/* Make sure that PMUv3 support is indicated in the ID register */431dfr0 = vcpu_get_reg(vpmu_vm.vcpu, KVM_ARM64_SYS_REG(SYS_ID_AA64DFR0_EL1));432pmuver = FIELD_GET(ID_AA64DFR0_EL1_PMUVer, dfr0);433TEST_ASSERT(pmuver != ID_AA64DFR0_EL1_PMUVer_IMP_DEF &&434pmuver >= ID_AA64DFR0_EL1_PMUVer_IMP,435"Unexpected PMUVER (0x%x) on the vCPU with PMUv3", pmuver);436437vcpu_ioctl(vpmu_vm.vcpu, KVM_SET_DEVICE_ATTR, &irq_attr);438}439440static void destroy_vpmu_vm(void)441{442kvm_vm_free(vpmu_vm.vm);443}444445static void run_vcpu(struct kvm_vcpu *vcpu, uint64_t pmcr_n)446{447struct ucall uc;448449vcpu_args_set(vcpu, 1, pmcr_n);450vcpu_run(vcpu);451switch (get_ucall(vcpu, &uc)) {452case UCALL_ABORT:453REPORT_GUEST_ASSERT(uc);454break;455case UCALL_DONE:456break;457default:458TEST_FAIL("Unknown ucall %lu", uc.cmd);459break;460}461}462463static void test_create_vpmu_vm_with_nr_counters(unsigned int nr_counters, bool expect_fail)464{465struct kvm_vcpu *vcpu;466unsigned int prev;467int ret;468469create_vpmu_vm(guest_code);470vcpu = vpmu_vm.vcpu;471472prev = get_pmcr_n(vcpu_get_reg(vcpu, KVM_ARM64_SYS_REG(SYS_PMCR_EL0)));473474ret = __vcpu_device_attr_set(vcpu, KVM_ARM_VCPU_PMU_V3_CTRL,475KVM_ARM_VCPU_PMU_V3_SET_NR_COUNTERS, &nr_counters);476477if (expect_fail)478TEST_ASSERT(ret && errno == EINVAL,479"Setting more PMU counters (%u) than available (%u) unexpectedly succeeded",480nr_counters, prev);481else482TEST_ASSERT(!ret, KVM_IOCTL_ERROR(KVM_SET_DEVICE_ATTR, ret));483484vcpu_device_attr_set(vcpu, KVM_ARM_VCPU_PMU_V3_CTRL, KVM_ARM_VCPU_PMU_V3_INIT, NULL);485}486487/*488* Create a guest with one vCPU, set the PMCR_EL0.N for the vCPU to @pmcr_n,489* and run the test.490*/491static void run_access_test(uint64_t pmcr_n)492{493uint64_t sp;494struct kvm_vcpu *vcpu;495struct kvm_vcpu_init init;496497pr_debug("Test with pmcr_n %lu\n", pmcr_n);498499test_create_vpmu_vm_with_nr_counters(pmcr_n, false);500vcpu = vpmu_vm.vcpu;501502/* Save the initial sp to restore them later to run the guest again */503sp = vcpu_get_reg(vcpu, ctxt_reg_alias(vcpu, SYS_SP_EL1));504505run_vcpu(vcpu, pmcr_n);506507/*508* Reset and re-initialize the vCPU, and run the guest code again to509* check if PMCR_EL0.N is preserved.510*/511kvm_get_default_vcpu_target(vpmu_vm.vm, &init);512init.features[0] |= (1 << KVM_ARM_VCPU_PMU_V3);513aarch64_vcpu_setup(vcpu, &init);514vcpu_init_descriptor_tables(vcpu);515vcpu_set_reg(vcpu, ctxt_reg_alias(vcpu, SYS_SP_EL1), sp);516vcpu_set_reg(vcpu, ARM64_CORE_REG(regs.pc), (uint64_t)guest_code);517518run_vcpu(vcpu, pmcr_n);519520destroy_vpmu_vm();521}522523static struct pmreg_sets validity_check_reg_sets[] = {524PMREG_SET(SYS_PMCNTENSET_EL0, SYS_PMCNTENCLR_EL0),525PMREG_SET(SYS_PMINTENSET_EL1, SYS_PMINTENCLR_EL1),526PMREG_SET(SYS_PMOVSSET_EL0, SYS_PMOVSCLR_EL0),527};528529/*530* Create a VM, and check if KVM handles the userspace accesses of531* the PMU register sets in @validity_check_reg_sets[] correctly.532*/533static void run_pmregs_validity_test(uint64_t pmcr_n)534{535int i;536struct kvm_vcpu *vcpu;537uint64_t set_reg_id, clr_reg_id, reg_val;538uint64_t valid_counters_mask, max_counters_mask;539540test_create_vpmu_vm_with_nr_counters(pmcr_n, false);541vcpu = vpmu_vm.vcpu;542543valid_counters_mask = get_counters_mask(pmcr_n);544max_counters_mask = get_counters_mask(ARMV8_PMU_MAX_COUNTERS);545546for (i = 0; i < ARRAY_SIZE(validity_check_reg_sets); i++) {547set_reg_id = validity_check_reg_sets[i].set_reg_id;548clr_reg_id = validity_check_reg_sets[i].clr_reg_id;549550/*551* Test if the 'set' and 'clr' variants of the registers552* are initialized based on the number of valid counters.553*/554reg_val = vcpu_get_reg(vcpu, KVM_ARM64_SYS_REG(set_reg_id));555TEST_ASSERT((reg_val & (~valid_counters_mask)) == 0,556"Initial read of set_reg: 0x%llx has unimplemented counters enabled: 0x%lx",557KVM_ARM64_SYS_REG(set_reg_id), reg_val);558559reg_val = vcpu_get_reg(vcpu, KVM_ARM64_SYS_REG(clr_reg_id));560TEST_ASSERT((reg_val & (~valid_counters_mask)) == 0,561"Initial read of clr_reg: 0x%llx has unimplemented counters enabled: 0x%lx",562KVM_ARM64_SYS_REG(clr_reg_id), reg_val);563564/*565* Using the 'set' variant, force-set the register to the566* max number of possible counters and test if KVM discards567* the bits for unimplemented counters as it should.568*/569vcpu_set_reg(vcpu, KVM_ARM64_SYS_REG(set_reg_id), max_counters_mask);570571reg_val = vcpu_get_reg(vcpu, KVM_ARM64_SYS_REG(set_reg_id));572TEST_ASSERT((reg_val & (~valid_counters_mask)) == 0,573"Read of set_reg: 0x%llx has unimplemented counters enabled: 0x%lx",574KVM_ARM64_SYS_REG(set_reg_id), reg_val);575576reg_val = vcpu_get_reg(vcpu, KVM_ARM64_SYS_REG(clr_reg_id));577TEST_ASSERT((reg_val & (~valid_counters_mask)) == 0,578"Read of clr_reg: 0x%llx has unimplemented counters enabled: 0x%lx",579KVM_ARM64_SYS_REG(clr_reg_id), reg_val);580}581582destroy_vpmu_vm();583}584585/*586* Create a guest with one vCPU, and attempt to set the PMCR_EL0.N for587* the vCPU to @pmcr_n, which is larger than the host value.588* The attempt should fail as @pmcr_n is too big to set for the vCPU.589*/590static void run_error_test(uint64_t pmcr_n)591{592pr_debug("Error test with pmcr_n %lu (larger than the host)\n", pmcr_n);593594test_create_vpmu_vm_with_nr_counters(pmcr_n, true);595destroy_vpmu_vm();596}597598/*599* Return the default number of implemented PMU event counters excluding600* the cycle counter (i.e. PMCR_EL0.N value) for the guest.601*/602static uint64_t get_pmcr_n_limit(void)603{604uint64_t pmcr;605606create_vpmu_vm(guest_code);607pmcr = vcpu_get_reg(vpmu_vm.vcpu, KVM_ARM64_SYS_REG(SYS_PMCR_EL0));608destroy_vpmu_vm();609return get_pmcr_n(pmcr);610}611612static bool kvm_supports_nr_counters_attr(void)613{614bool supported;615616create_vpmu_vm(NULL);617supported = !__vcpu_has_device_attr(vpmu_vm.vcpu, KVM_ARM_VCPU_PMU_V3_CTRL,618KVM_ARM_VCPU_PMU_V3_SET_NR_COUNTERS);619destroy_vpmu_vm();620621return supported;622}623624int main(void)625{626uint64_t i, pmcr_n;627628TEST_REQUIRE(kvm_has_cap(KVM_CAP_ARM_PMU_V3));629TEST_REQUIRE(kvm_supports_vgic_v3());630TEST_REQUIRE(kvm_supports_nr_counters_attr());631632pmcr_n = get_pmcr_n_limit();633for (i = 0; i <= pmcr_n; i++) {634run_access_test(i);635run_pmregs_validity_test(i);636}637638for (i = pmcr_n + 1; i < ARMV8_PMU_MAX_COUNTERS; i++)639run_error_test(i);640641return 0;642}643644645