vpmu_counter_access.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * vpmu_counter_access - Test vPMU event counter access
  4. *
  5. * Copyright (c) 2023 Google LLC.
  6. *
  7. * This test checks if the guest can see the same number of the PMU event
  8. * counters (PMCR_EL0.N) that userspace sets, if the guest can access
  9. * those counters, and if the guest is prevented from accessing any
  10. * other counters.
  11. * It also checks if the userspace accesses to the PMU regsisters honor the
  12. * PMCR.N value that's set for the guest.
  13. * This test runs only when KVM_CAP_ARM_PMU_V3 is supported on the host.
  14. */
  15. #include <kvm_util.h>
  16. #include <processor.h>
  17. #include <test_util.h>
  18. #include <vgic.h>
  19. #include <perf/arm_pmuv3.h>
  20. #include <linux/bitfield.h>
  21. /* The max number of the PMU event counters (excluding the cycle counter) */
  22. #define ARMV8_PMU_MAX_GENERAL_COUNTERS (ARMV8_PMU_MAX_COUNTERS - 1)
  23. /* The cycle counter bit position that's common among the PMU registers */
  24. #define ARMV8_PMU_CYCLE_IDX 31
  25. struct vpmu_vm {
  26. struct kvm_vm *vm;
  27. struct kvm_vcpu *vcpu;
  28. };
  29. static struct vpmu_vm vpmu_vm;
  30. struct pmreg_sets {
  31. uint64_t set_reg_id;
  32. uint64_t clr_reg_id;
  33. };
  34. #define PMREG_SET(set, clr) {.set_reg_id = set, .clr_reg_id = clr}
  35. static uint64_t get_pmcr_n(uint64_t pmcr)
  36. {
  37. return FIELD_GET(ARMV8_PMU_PMCR_N, pmcr);
  38. }
  39. static uint64_t get_counters_mask(uint64_t n)
  40. {
  41. uint64_t mask = BIT(ARMV8_PMU_CYCLE_IDX);
  42. if (n)
  43. mask |= GENMASK(n - 1, 0);
  44. return mask;
  45. }
  46. /* Read PMEVTCNTR<n>_EL0 through PMXEVCNTR_EL0 */
  47. static inline unsigned long read_sel_evcntr(int sel)
  48. {
  49. write_sysreg(sel, pmselr_el0);
  50. isb();
  51. return read_sysreg(pmxevcntr_el0);
  52. }
  53. /* Write PMEVTCNTR<n>_EL0 through PMXEVCNTR_EL0 */
  54. static inline void write_sel_evcntr(int sel, unsigned long val)
  55. {
  56. write_sysreg(sel, pmselr_el0);
  57. isb();
  58. write_sysreg(val, pmxevcntr_el0);
  59. isb();
  60. }
  61. /* Read PMEVTYPER<n>_EL0 through PMXEVTYPER_EL0 */
  62. static inline unsigned long read_sel_evtyper(int sel)
  63. {
  64. write_sysreg(sel, pmselr_el0);
  65. isb();
  66. return read_sysreg(pmxevtyper_el0);
  67. }
  68. /* Write PMEVTYPER<n>_EL0 through PMXEVTYPER_EL0 */
  69. static inline void write_sel_evtyper(int sel, unsigned long val)
  70. {
  71. write_sysreg(sel, pmselr_el0);
  72. isb();
  73. write_sysreg(val, pmxevtyper_el0);
  74. isb();
  75. }
  76. static void pmu_disable_reset(void)
  77. {
  78. uint64_t pmcr = read_sysreg(pmcr_el0);
  79. /* Reset all counters, disabling them */
  80. pmcr &= ~ARMV8_PMU_PMCR_E;
  81. write_sysreg(pmcr | ARMV8_PMU_PMCR_P, pmcr_el0);
  82. isb();
  83. }
  84. #define RETURN_READ_PMEVCNTRN(n) \
  85. return read_sysreg(pmevcntr##n##_el0)
  86. static unsigned long read_pmevcntrn(int n)
  87. {
  88. PMEVN_SWITCH(n, RETURN_READ_PMEVCNTRN);
  89. return 0;
  90. }
  91. #define WRITE_PMEVCNTRN(n) \
  92. write_sysreg(val, pmevcntr##n##_el0)
  93. static void write_pmevcntrn(int n, unsigned long val)
  94. {
  95. PMEVN_SWITCH(n, WRITE_PMEVCNTRN);
  96. isb();
  97. }
  98. #define READ_PMEVTYPERN(n) \
  99. return read_sysreg(pmevtyper##n##_el0)
  100. static unsigned long read_pmevtypern(int n)
  101. {
  102. PMEVN_SWITCH(n, READ_PMEVTYPERN);
  103. return 0;
  104. }
  105. #define WRITE_PMEVTYPERN(n) \
  106. write_sysreg(val, pmevtyper##n##_el0)
  107. static void write_pmevtypern(int n, unsigned long val)
  108. {
  109. PMEVN_SWITCH(n, WRITE_PMEVTYPERN);
  110. isb();
  111. }
  112. /*
  113. * The pmc_accessor structure has pointers to PMEV{CNTR,TYPER}<n>_EL0
  114. * accessors that test cases will use. Each of the accessors will
  115. * either directly reads/writes PMEV{CNTR,TYPER}<n>_EL0
  116. * (i.e. {read,write}_pmev{cnt,type}rn()), or reads/writes them through
  117. * PMXEV{CNTR,TYPER}_EL0 (i.e. {read,write}_sel_ev{cnt,type}r()).
  118. *
  119. * This is used to test that combinations of those accessors provide
  120. * the consistent behavior.
  121. */
  122. struct pmc_accessor {
  123. /* A function to be used to read PMEVTCNTR<n>_EL0 */
  124. unsigned long (*read_cntr)(int idx);
  125. /* A function to be used to write PMEVTCNTR<n>_EL0 */
  126. void (*write_cntr)(int idx, unsigned long val);
  127. /* A function to be used to read PMEVTYPER<n>_EL0 */
  128. unsigned long (*read_typer)(int idx);
  129. /* A function to be used to write PMEVTYPER<n>_EL0 */
  130. void (*write_typer)(int idx, unsigned long val);
  131. };
  132. struct pmc_accessor pmc_accessors[] = {
  133. /* test with all direct accesses */
  134. { read_pmevcntrn, write_pmevcntrn, read_pmevtypern, write_pmevtypern },
  135. /* test with all indirect accesses */
  136. { read_sel_evcntr, write_sel_evcntr, read_sel_evtyper, write_sel_evtyper },
  137. /* read with direct accesses, and write with indirect accesses */
  138. { read_pmevcntrn, write_sel_evcntr, read_pmevtypern, write_sel_evtyper },
  139. /* read with indirect accesses, and write with direct accesses */
  140. { read_sel_evcntr, write_pmevcntrn, read_sel_evtyper, write_pmevtypern },
  141. };
  142. /*
  143. * Convert a pointer of pmc_accessor to an index in pmc_accessors[],
  144. * assuming that the pointer is one of the entries in pmc_accessors[].
  145. */
  146. #define PMC_ACC_TO_IDX(acc) (acc - &pmc_accessors[0])
  147. #define GUEST_ASSERT_BITMAP_REG(regname, mask, set_expected) \
  148. { \
  149. uint64_t _tval = read_sysreg(regname); \
  150. \
  151. if (set_expected) \
  152. __GUEST_ASSERT((_tval & mask), \
  153. "tval: 0x%lx; mask: 0x%lx; set_expected: %u", \
  154. _tval, mask, set_expected); \
  155. else \
  156. __GUEST_ASSERT(!(_tval & mask), \
  157. "tval: 0x%lx; mask: 0x%lx; set_expected: %u", \
  158. _tval, mask, set_expected); \
  159. }
  160. /*
  161. * Check if @mask bits in {PMCNTEN,PMINTEN,PMOVS}{SET,CLR} registers
  162. * are set or cleared as specified in @set_expected.
  163. */
  164. static void check_bitmap_pmu_regs(uint64_t mask, bool set_expected)
  165. {
  166. GUEST_ASSERT_BITMAP_REG(pmcntenset_el0, mask, set_expected);
  167. GUEST_ASSERT_BITMAP_REG(pmcntenclr_el0, mask, set_expected);
  168. GUEST_ASSERT_BITMAP_REG(pmintenset_el1, mask, set_expected);
  169. GUEST_ASSERT_BITMAP_REG(pmintenclr_el1, mask, set_expected);
  170. GUEST_ASSERT_BITMAP_REG(pmovsset_el0, mask, set_expected);
  171. GUEST_ASSERT_BITMAP_REG(pmovsclr_el0, mask, set_expected);
  172. }
  173. /*
  174. * Check if the bit in {PMCNTEN,PMINTEN,PMOVS}{SET,CLR} registers corresponding
  175. * to the specified counter (@pmc_idx) can be read/written as expected.
  176. * When @set_op is true, it tries to set the bit for the counter in
  177. * those registers by writing the SET registers (the bit won't be set
  178. * if the counter is not implemented though).
  179. * Otherwise, it tries to clear the bits in the registers by writing
  180. * the CLR registers.
  181. * Then, it checks if the values indicated in the registers are as expected.
  182. */
  183. static void test_bitmap_pmu_regs(int pmc_idx, bool set_op)
  184. {
  185. uint64_t pmcr_n, test_bit = BIT(pmc_idx);
  186. bool set_expected = false;
  187. if (set_op) {
  188. write_sysreg(test_bit, pmcntenset_el0);
  189. write_sysreg(test_bit, pmintenset_el1);
  190. write_sysreg(test_bit, pmovsset_el0);
  191. /* The bit will be set only if the counter is implemented */
  192. pmcr_n = get_pmcr_n(read_sysreg(pmcr_el0));
  193. set_expected = (pmc_idx < pmcr_n) ? true : false;
  194. } else {
  195. write_sysreg(test_bit, pmcntenclr_el0);
  196. write_sysreg(test_bit, pmintenclr_el1);
  197. write_sysreg(test_bit, pmovsclr_el0);
  198. }
  199. check_bitmap_pmu_regs(test_bit, set_expected);
  200. }
  201. /*
  202. * Tests for reading/writing registers for the (implemented) event counter
  203. * specified by @pmc_idx.
  204. */
  205. static void test_access_pmc_regs(struct pmc_accessor *acc, int pmc_idx)
  206. {
  207. uint64_t write_data, read_data;
  208. /* Disable all PMCs and reset all PMCs to zero. */
  209. pmu_disable_reset();
  210. /*
  211. * Tests for reading/writing {PMCNTEN,PMINTEN,PMOVS}{SET,CLR}_EL1.
  212. */
  213. /* Make sure that the bit in those registers are set to 0 */
  214. test_bitmap_pmu_regs(pmc_idx, false);
  215. /* Test if setting the bit in those registers works */
  216. test_bitmap_pmu_regs(pmc_idx, true);
  217. /* Test if clearing the bit in those registers works */
  218. test_bitmap_pmu_regs(pmc_idx, false);
  219. /*
  220. * Tests for reading/writing the event type register.
  221. */
  222. /*
  223. * Set the event type register to an arbitrary value just for testing
  224. * of reading/writing the register.
  225. * Arm ARM says that for the event from 0x0000 to 0x003F,
  226. * the value indicated in the PMEVTYPER<n>_EL0.evtCount field is
  227. * the value written to the field even when the specified event
  228. * is not supported.
  229. */
  230. write_data = (ARMV8_PMU_EXCLUDE_EL1 | ARMV8_PMUV3_PERFCTR_INST_RETIRED);
  231. acc->write_typer(pmc_idx, write_data);
  232. read_data = acc->read_typer(pmc_idx);
  233. __GUEST_ASSERT(read_data == write_data,
  234. "pmc_idx: 0x%x; acc_idx: 0x%lx; read_data: 0x%lx; write_data: 0x%lx",
  235. pmc_idx, PMC_ACC_TO_IDX(acc), read_data, write_data);
  236. /*
  237. * Tests for reading/writing the event count register.
  238. */
  239. read_data = acc->read_cntr(pmc_idx);
  240. /* The count value must be 0, as it is disabled and reset */
  241. __GUEST_ASSERT(read_data == 0,
  242. "pmc_idx: 0x%x; acc_idx: 0x%lx; read_data: 0x%lx",
  243. pmc_idx, PMC_ACC_TO_IDX(acc), read_data);
  244. write_data = read_data + pmc_idx + 0x12345;
  245. acc->write_cntr(pmc_idx, write_data);
  246. read_data = acc->read_cntr(pmc_idx);
  247. __GUEST_ASSERT(read_data == write_data,
  248. "pmc_idx: 0x%x; acc_idx: 0x%lx; read_data: 0x%lx; write_data: 0x%lx",
  249. pmc_idx, PMC_ACC_TO_IDX(acc), read_data, write_data);
  250. }
  251. #define INVALID_EC (-1ul)
  252. uint64_t expected_ec = INVALID_EC;
  253. static void guest_sync_handler(struct ex_regs *regs)
  254. {
  255. uint64_t esr, ec;
  256. esr = read_sysreg(esr_el1);
  257. ec = ESR_ELx_EC(esr);
  258. __GUEST_ASSERT(expected_ec == ec,
  259. "PC: 0x%lx; ESR: 0x%lx; EC: 0x%lx; EC expected: 0x%lx",
  260. regs->pc, esr, ec, expected_ec);
  261. /* skip the trapping instruction */
  262. regs->pc += 4;
  263. /* Use INVALID_EC to indicate an exception occurred */
  264. expected_ec = INVALID_EC;
  265. }
  266. /*
  267. * Run the given operation that should trigger an exception with the
  268. * given exception class. The exception handler (guest_sync_handler)
  269. * will reset op_end_addr to 0, expected_ec to INVALID_EC, and skip
  270. * the instruction that trapped.
  271. */
  272. #define TEST_EXCEPTION(ec, ops) \
  273. ({ \
  274. GUEST_ASSERT(ec != INVALID_EC); \
  275. WRITE_ONCE(expected_ec, ec); \
  276. dsb(ish); \
  277. ops; \
  278. GUEST_ASSERT(expected_ec == INVALID_EC); \
  279. })
  280. /*
  281. * Tests for reading/writing registers for the unimplemented event counter
  282. * specified by @pmc_idx (>= PMCR_EL0.N).
  283. */
  284. static void test_access_invalid_pmc_regs(struct pmc_accessor *acc, int pmc_idx)
  285. {
  286. /*
  287. * Reading/writing the event count/type registers should cause
  288. * an UNDEFINED exception.
  289. */
  290. TEST_EXCEPTION(ESR_ELx_EC_UNKNOWN, acc->read_cntr(pmc_idx));
  291. TEST_EXCEPTION(ESR_ELx_EC_UNKNOWN, acc->write_cntr(pmc_idx, 0));
  292. TEST_EXCEPTION(ESR_ELx_EC_UNKNOWN, acc->read_typer(pmc_idx));
  293. TEST_EXCEPTION(ESR_ELx_EC_UNKNOWN, acc->write_typer(pmc_idx, 0));
  294. /*
  295. * The bit corresponding to the (unimplemented) counter in
  296. * {PMCNTEN,PMINTEN,PMOVS}{SET,CLR} registers should be RAZ.
  297. */
  298. test_bitmap_pmu_regs(pmc_idx, 1);
  299. test_bitmap_pmu_regs(pmc_idx, 0);
  300. }
  301. /*
  302. * The guest is configured with PMUv3 with @expected_pmcr_n number of
  303. * event counters.
  304. * Check if @expected_pmcr_n is consistent with PMCR_EL0.N, and
  305. * if reading/writing PMU registers for implemented or unimplemented
  306. * counters works as expected.
  307. */
  308. static void guest_code(uint64_t expected_pmcr_n)
  309. {
  310. uint64_t pmcr, pmcr_n, unimp_mask;
  311. int i, pmc;
  312. __GUEST_ASSERT(expected_pmcr_n <= ARMV8_PMU_MAX_GENERAL_COUNTERS,
  313. "Expected PMCR.N: 0x%lx; ARMv8 general counters: 0x%x",
  314. expected_pmcr_n, ARMV8_PMU_MAX_GENERAL_COUNTERS);
  315. pmcr = read_sysreg(pmcr_el0);
  316. pmcr_n = get_pmcr_n(pmcr);
  317. /* Make sure that PMCR_EL0.N indicates the value userspace set */
  318. __GUEST_ASSERT(pmcr_n == expected_pmcr_n,
  319. "Expected PMCR.N: 0x%lx, PMCR.N: 0x%lx",
  320. expected_pmcr_n, pmcr_n);
  321. /*
  322. * Make sure that (RAZ) bits corresponding to unimplemented event
  323. * counters in {PMCNTEN,PMINTEN,PMOVS}{SET,CLR} registers are reset
  324. * to zero.
  325. * (NOTE: bits for implemented event counters are reset to UNKNOWN)
  326. */
  327. unimp_mask = GENMASK_ULL(ARMV8_PMU_MAX_GENERAL_COUNTERS - 1, pmcr_n);
  328. check_bitmap_pmu_regs(unimp_mask, false);
  329. /*
  330. * Tests for reading/writing PMU registers for implemented counters.
  331. * Use each combination of PMEV{CNTR,TYPER}<n>_EL0 accessor functions.
  332. */
  333. for (i = 0; i < ARRAY_SIZE(pmc_accessors); i++) {
  334. for (pmc = 0; pmc < pmcr_n; pmc++)
  335. test_access_pmc_regs(&pmc_accessors[i], pmc);
  336. }
  337. /*
  338. * Tests for reading/writing PMU registers for unimplemented counters.
  339. * Use each combination of PMEV{CNTR,TYPER}<n>_EL0 accessor functions.
  340. */
  341. for (i = 0; i < ARRAY_SIZE(pmc_accessors); i++) {
  342. for (pmc = pmcr_n; pmc < ARMV8_PMU_MAX_GENERAL_COUNTERS; pmc++)
  343. test_access_invalid_pmc_regs(&pmc_accessors[i], pmc);
  344. }
  345. GUEST_DONE();
  346. }
  347. /* Create a VM that has one vCPU with PMUv3 configured. */
  348. static void create_vpmu_vm(void *guest_code)
  349. {
  350. struct kvm_vcpu_init init;
  351. uint8_t pmuver, ec;
  352. uint64_t dfr0, irq = 23;
  353. struct kvm_device_attr irq_attr = {
  354. .group = KVM_ARM_VCPU_PMU_V3_CTRL,
  355. .attr = KVM_ARM_VCPU_PMU_V3_IRQ,
  356. .addr = (uint64_t)&irq,
  357. };
  358. /* The test creates the vpmu_vm multiple times. Ensure a clean state */
  359. memset(&vpmu_vm, 0, sizeof(vpmu_vm));
  360. vpmu_vm.vm = vm_create(1);
  361. vm_init_descriptor_tables(vpmu_vm.vm);
  362. for (ec = 0; ec < ESR_ELx_EC_MAX + 1; ec++) {
  363. vm_install_sync_handler(vpmu_vm.vm, VECTOR_SYNC_CURRENT, ec,
  364. guest_sync_handler);
  365. }
  366. /* Create vCPU with PMUv3 */
  367. kvm_get_default_vcpu_target(vpmu_vm.vm, &init);
  368. init.features[0] |= (1 << KVM_ARM_VCPU_PMU_V3);
  369. vpmu_vm.vcpu = aarch64_vcpu_add(vpmu_vm.vm, 0, &init, guest_code);
  370. vcpu_init_descriptor_tables(vpmu_vm.vcpu);
  371. kvm_arch_vm_finalize_vcpus(vpmu_vm.vm);
  372. /* Make sure that PMUv3 support is indicated in the ID register */
  373. dfr0 = vcpu_get_reg(vpmu_vm.vcpu, KVM_ARM64_SYS_REG(SYS_ID_AA64DFR0_EL1));
  374. pmuver = FIELD_GET(ID_AA64DFR0_EL1_PMUVer, dfr0);
  375. TEST_ASSERT(pmuver != ID_AA64DFR0_EL1_PMUVer_IMP_DEF &&
  376. pmuver >= ID_AA64DFR0_EL1_PMUVer_IMP,
  377. "Unexpected PMUVER (0x%x) on the vCPU with PMUv3", pmuver);
  378. vcpu_ioctl(vpmu_vm.vcpu, KVM_SET_DEVICE_ATTR, &irq_attr);
  379. }
  380. static void destroy_vpmu_vm(void)
  381. {
  382. kvm_vm_free(vpmu_vm.vm);
  383. }
  384. static void run_vcpu(struct kvm_vcpu *vcpu, uint64_t pmcr_n)
  385. {
  386. struct ucall uc;
  387. vcpu_args_set(vcpu, 1, pmcr_n);
  388. vcpu_run(vcpu);
  389. switch (get_ucall(vcpu, &uc)) {
  390. case UCALL_ABORT:
  391. REPORT_GUEST_ASSERT(uc);
  392. break;
  393. case UCALL_DONE:
  394. break;
  395. default:
  396. TEST_FAIL("Unknown ucall %lu", uc.cmd);
  397. break;
  398. }
  399. }
  400. static void test_create_vpmu_vm_with_nr_counters(unsigned int nr_counters, bool expect_fail)
  401. {
  402. struct kvm_vcpu *vcpu;
  403. unsigned int prev;
  404. int ret;
  405. create_vpmu_vm(guest_code);
  406. vcpu = vpmu_vm.vcpu;
  407. prev = get_pmcr_n(vcpu_get_reg(vcpu, KVM_ARM64_SYS_REG(SYS_PMCR_EL0)));
  408. ret = __vcpu_device_attr_set(vcpu, KVM_ARM_VCPU_PMU_V3_CTRL,
  409. KVM_ARM_VCPU_PMU_V3_SET_NR_COUNTERS, &nr_counters);
  410. if (expect_fail)
  411. TEST_ASSERT(ret && errno == EINVAL,
  412. "Setting more PMU counters (%u) than available (%u) unexpectedly succeeded",
  413. nr_counters, prev);
  414. else
  415. TEST_ASSERT(!ret, KVM_IOCTL_ERROR(KVM_SET_DEVICE_ATTR, ret));
  416. vcpu_device_attr_set(vcpu, KVM_ARM_VCPU_PMU_V3_CTRL, KVM_ARM_VCPU_PMU_V3_INIT, NULL);
  417. }
  418. /*
  419. * Create a guest with one vCPU, set the PMCR_EL0.N for the vCPU to @pmcr_n,
  420. * and run the test.
  421. */
  422. static void run_access_test(uint64_t pmcr_n)
  423. {
  424. uint64_t sp;
  425. struct kvm_vcpu *vcpu;
  426. struct kvm_vcpu_init init;
  427. pr_debug("Test with pmcr_n %lu\n", pmcr_n);
  428. test_create_vpmu_vm_with_nr_counters(pmcr_n, false);
  429. vcpu = vpmu_vm.vcpu;
  430. /* Save the initial sp to restore them later to run the guest again */
  431. sp = vcpu_get_reg(vcpu, ctxt_reg_alias(vcpu, SYS_SP_EL1));
  432. run_vcpu(vcpu, pmcr_n);
  433. /*
  434. * Reset and re-initialize the vCPU, and run the guest code again to
  435. * check if PMCR_EL0.N is preserved.
  436. */
  437. kvm_get_default_vcpu_target(vpmu_vm.vm, &init);
  438. init.features[0] |= (1 << KVM_ARM_VCPU_PMU_V3);
  439. aarch64_vcpu_setup(vcpu, &init);
  440. vcpu_init_descriptor_tables(vcpu);
  441. vcpu_set_reg(vcpu, ctxt_reg_alias(vcpu, SYS_SP_EL1), sp);
  442. vcpu_set_reg(vcpu, ARM64_CORE_REG(regs.pc), (uint64_t)guest_code);
  443. run_vcpu(vcpu, pmcr_n);
  444. destroy_vpmu_vm();
  445. }
  446. static struct pmreg_sets validity_check_reg_sets[] = {
  447. PMREG_SET(SYS_PMCNTENSET_EL0, SYS_PMCNTENCLR_EL0),
  448. PMREG_SET(SYS_PMINTENSET_EL1, SYS_PMINTENCLR_EL1),
  449. PMREG_SET(SYS_PMOVSSET_EL0, SYS_PMOVSCLR_EL0),
  450. };
  451. /*
  452. * Create a VM, and check if KVM handles the userspace accesses of
  453. * the PMU register sets in @validity_check_reg_sets[] correctly.
  454. */
  455. static void run_pmregs_validity_test(uint64_t pmcr_n)
  456. {
  457. int i;
  458. struct kvm_vcpu *vcpu;
  459. uint64_t set_reg_id, clr_reg_id, reg_val;
  460. uint64_t valid_counters_mask, max_counters_mask;
  461. test_create_vpmu_vm_with_nr_counters(pmcr_n, false);
  462. vcpu = vpmu_vm.vcpu;
  463. valid_counters_mask = get_counters_mask(pmcr_n);
  464. max_counters_mask = get_counters_mask(ARMV8_PMU_MAX_COUNTERS);
  465. for (i = 0; i < ARRAY_SIZE(validity_check_reg_sets); i++) {
  466. set_reg_id = validity_check_reg_sets[i].set_reg_id;
  467. clr_reg_id = validity_check_reg_sets[i].clr_reg_id;
  468. /*
  469. * Test if the 'set' and 'clr' variants of the registers
  470. * are initialized based on the number of valid counters.
  471. */
  472. reg_val = vcpu_get_reg(vcpu, KVM_ARM64_SYS_REG(set_reg_id));
  473. TEST_ASSERT((reg_val & (~valid_counters_mask)) == 0,
  474. "Initial read of set_reg: 0x%llx has unimplemented counters enabled: 0x%lx",
  475. KVM_ARM64_SYS_REG(set_reg_id), reg_val);
  476. reg_val = vcpu_get_reg(vcpu, KVM_ARM64_SYS_REG(clr_reg_id));
  477. TEST_ASSERT((reg_val & (~valid_counters_mask)) == 0,
  478. "Initial read of clr_reg: 0x%llx has unimplemented counters enabled: 0x%lx",
  479. KVM_ARM64_SYS_REG(clr_reg_id), reg_val);
  480. /*
  481. * Using the 'set' variant, force-set the register to the
  482. * max number of possible counters and test if KVM discards
  483. * the bits for unimplemented counters as it should.
  484. */
  485. vcpu_set_reg(vcpu, KVM_ARM64_SYS_REG(set_reg_id), max_counters_mask);
  486. reg_val = vcpu_get_reg(vcpu, KVM_ARM64_SYS_REG(set_reg_id));
  487. TEST_ASSERT((reg_val & (~valid_counters_mask)) == 0,
  488. "Read of set_reg: 0x%llx has unimplemented counters enabled: 0x%lx",
  489. KVM_ARM64_SYS_REG(set_reg_id), reg_val);
  490. reg_val = vcpu_get_reg(vcpu, KVM_ARM64_SYS_REG(clr_reg_id));
  491. TEST_ASSERT((reg_val & (~valid_counters_mask)) == 0,
  492. "Read of clr_reg: 0x%llx has unimplemented counters enabled: 0x%lx",
  493. KVM_ARM64_SYS_REG(clr_reg_id), reg_val);
  494. }
  495. destroy_vpmu_vm();
  496. }
  497. /*
  498. * Create a guest with one vCPU, and attempt to set the PMCR_EL0.N for
  499. * the vCPU to @pmcr_n, which is larger than the host value.
  500. * The attempt should fail as @pmcr_n is too big to set for the vCPU.
  501. */
  502. static void run_error_test(uint64_t pmcr_n)
  503. {
  504. pr_debug("Error test with pmcr_n %lu (larger than the host)\n", pmcr_n);
  505. test_create_vpmu_vm_with_nr_counters(pmcr_n, true);
  506. destroy_vpmu_vm();
  507. }
  508. /*
  509. * Return the default number of implemented PMU event counters excluding
  510. * the cycle counter (i.e. PMCR_EL0.N value) for the guest.
  511. */
  512. static uint64_t get_pmcr_n_limit(void)
  513. {
  514. uint64_t pmcr;
  515. create_vpmu_vm(guest_code);
  516. pmcr = vcpu_get_reg(vpmu_vm.vcpu, KVM_ARM64_SYS_REG(SYS_PMCR_EL0));
  517. destroy_vpmu_vm();
  518. return get_pmcr_n(pmcr);
  519. }
  520. static bool kvm_supports_nr_counters_attr(void)
  521. {
  522. bool supported;
  523. create_vpmu_vm(NULL);
  524. supported = !__vcpu_has_device_attr(vpmu_vm.vcpu, KVM_ARM_VCPU_PMU_V3_CTRL,
  525. KVM_ARM_VCPU_PMU_V3_SET_NR_COUNTERS);
  526. destroy_vpmu_vm();
  527. return supported;
  528. }
  529. int main(void)
  530. {
  531. uint64_t i, pmcr_n;
  532. TEST_REQUIRE(kvm_has_cap(KVM_CAP_ARM_PMU_V3));
  533. TEST_REQUIRE(kvm_supports_vgic_v3());
  534. TEST_REQUIRE(kvm_supports_nr_counters_attr());
  535. pmcr_n = get_pmcr_n_limit();
  536. for (i = 0; i <= pmcr_n; i++) {
  537. run_access_test(i);
  538. run_pmregs_validity_test(i);
  539. }
  540. for (i = pmcr_n + 1; i < ARMV8_PMU_MAX_COUNTERS; i++)
  541. run_error_test(i);
  542. return 0;
  543. }