pmu_counters_test.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (C) 2023, Tencent, Inc.
  4. */
  5. #include <x86intrin.h>
  6. #include "pmu.h"
  7. #include "processor.h"
  8. /* Number of iterations of the loop for the guest measurement payload. */
  9. #define NUM_LOOPS 10
  10. /* Each iteration of the loop retires one branch instruction. */
  11. #define NUM_BRANCH_INSNS_RETIRED (NUM_LOOPS)
  12. /*
  13. * Number of instructions in each loop. 1 ENTER, 1 CLFLUSH/CLFLUSHOPT/NOP,
  14. * 1 MFENCE, 1 MOV, 1 LEAVE, 1 LOOP.
  15. */
  16. #define NUM_INSNS_PER_LOOP 6
  17. /*
  18. * Number of "extra" instructions that will be counted, i.e. the number of
  19. * instructions that are needed to set up the loop and then disable the
  20. * counter. 2 MOV, 2 XOR, 1 WRMSR.
  21. */
  22. #define NUM_EXTRA_INSNS 5
  23. /* Total number of instructions retired within the measured section. */
  24. #define NUM_INSNS_RETIRED (NUM_LOOPS * NUM_INSNS_PER_LOOP + NUM_EXTRA_INSNS)
  25. /* Track which architectural events are supported by hardware. */
  26. static uint32_t hardware_pmu_arch_events;
  27. static uint8_t kvm_pmu_version;
  28. static bool kvm_has_perf_caps;
  29. #define X86_PMU_FEATURE_NULL \
  30. ({ \
  31. struct kvm_x86_pmu_feature feature = {}; \
  32. \
  33. feature; \
  34. })
  35. static bool pmu_is_null_feature(struct kvm_x86_pmu_feature event)
  36. {
  37. return !(*(u64 *)&event);
  38. }
  39. struct kvm_intel_pmu_event {
  40. struct kvm_x86_pmu_feature gp_event;
  41. struct kvm_x86_pmu_feature fixed_event;
  42. };
  43. /*
  44. * Wrap the array to appease the compiler, as the macros used to construct each
  45. * kvm_x86_pmu_feature use syntax that's only valid in function scope, and the
  46. * compiler often thinks the feature definitions aren't compile-time constants.
  47. */
  48. static struct kvm_intel_pmu_event intel_event_to_feature(uint8_t idx)
  49. {
  50. const struct kvm_intel_pmu_event __intel_event_to_feature[] = {
  51. [INTEL_ARCH_CPU_CYCLES_INDEX] = { X86_PMU_FEATURE_CPU_CYCLES, X86_PMU_FEATURE_CPU_CYCLES_FIXED },
  52. [INTEL_ARCH_INSTRUCTIONS_RETIRED_INDEX] = { X86_PMU_FEATURE_INSNS_RETIRED, X86_PMU_FEATURE_INSNS_RETIRED_FIXED },
  53. /*
  54. * Note, the fixed counter for reference cycles is NOT the same as the
  55. * general purpose architectural event. The fixed counter explicitly
  56. * counts at the same frequency as the TSC, whereas the GP event counts
  57. * at a fixed, but uarch specific, frequency. Bundle them here for
  58. * simplicity.
  59. */
  60. [INTEL_ARCH_REFERENCE_CYCLES_INDEX] = { X86_PMU_FEATURE_REFERENCE_CYCLES, X86_PMU_FEATURE_REFERENCE_TSC_CYCLES_FIXED },
  61. [INTEL_ARCH_LLC_REFERENCES_INDEX] = { X86_PMU_FEATURE_LLC_REFERENCES, X86_PMU_FEATURE_NULL },
  62. [INTEL_ARCH_LLC_MISSES_INDEX] = { X86_PMU_FEATURE_LLC_MISSES, X86_PMU_FEATURE_NULL },
  63. [INTEL_ARCH_BRANCHES_RETIRED_INDEX] = { X86_PMU_FEATURE_BRANCH_INSNS_RETIRED, X86_PMU_FEATURE_NULL },
  64. [INTEL_ARCH_BRANCHES_MISPREDICTED_INDEX] = { X86_PMU_FEATURE_BRANCHES_MISPREDICTED, X86_PMU_FEATURE_NULL },
  65. [INTEL_ARCH_TOPDOWN_SLOTS_INDEX] = { X86_PMU_FEATURE_TOPDOWN_SLOTS, X86_PMU_FEATURE_TOPDOWN_SLOTS_FIXED },
  66. [INTEL_ARCH_TOPDOWN_BE_BOUND_INDEX] = { X86_PMU_FEATURE_TOPDOWN_BE_BOUND, X86_PMU_FEATURE_NULL },
  67. [INTEL_ARCH_TOPDOWN_BAD_SPEC_INDEX] = { X86_PMU_FEATURE_TOPDOWN_BAD_SPEC, X86_PMU_FEATURE_NULL },
  68. [INTEL_ARCH_TOPDOWN_FE_BOUND_INDEX] = { X86_PMU_FEATURE_TOPDOWN_FE_BOUND, X86_PMU_FEATURE_NULL },
  69. [INTEL_ARCH_TOPDOWN_RETIRING_INDEX] = { X86_PMU_FEATURE_TOPDOWN_RETIRING, X86_PMU_FEATURE_NULL },
  70. [INTEL_ARCH_LBR_INSERTS_INDEX] = { X86_PMU_FEATURE_LBR_INSERTS, X86_PMU_FEATURE_NULL },
  71. };
  72. kvm_static_assert(ARRAY_SIZE(__intel_event_to_feature) == NR_INTEL_ARCH_EVENTS);
  73. return __intel_event_to_feature[idx];
  74. }
  75. static struct kvm_vm *pmu_vm_create_with_one_vcpu(struct kvm_vcpu **vcpu,
  76. void *guest_code,
  77. uint8_t pmu_version,
  78. uint64_t perf_capabilities)
  79. {
  80. struct kvm_vm *vm;
  81. vm = vm_create_with_one_vcpu(vcpu, guest_code);
  82. sync_global_to_guest(vm, kvm_pmu_version);
  83. sync_global_to_guest(vm, hardware_pmu_arch_events);
  84. /*
  85. * Set PERF_CAPABILITIES before PMU version as KVM disallows enabling
  86. * features via PERF_CAPABILITIES if the guest doesn't have a vPMU.
  87. */
  88. if (kvm_has_perf_caps)
  89. vcpu_set_msr(*vcpu, MSR_IA32_PERF_CAPABILITIES, perf_capabilities);
  90. vcpu_set_cpuid_property(*vcpu, X86_PROPERTY_PMU_VERSION, pmu_version);
  91. return vm;
  92. }
  93. static void run_vcpu(struct kvm_vcpu *vcpu)
  94. {
  95. struct ucall uc;
  96. do {
  97. vcpu_run(vcpu);
  98. switch (get_ucall(vcpu, &uc)) {
  99. case UCALL_SYNC:
  100. break;
  101. case UCALL_ABORT:
  102. REPORT_GUEST_ASSERT(uc);
  103. break;
  104. case UCALL_PRINTF:
  105. pr_info("%s", uc.buffer);
  106. break;
  107. case UCALL_DONE:
  108. break;
  109. default:
  110. TEST_FAIL("Unexpected ucall: %lu", uc.cmd);
  111. }
  112. } while (uc.cmd != UCALL_DONE);
  113. }
  114. static uint8_t guest_get_pmu_version(void)
  115. {
  116. /*
  117. * Return the effective PMU version, i.e. the minimum between what KVM
  118. * supports and what is enumerated to the guest. The host deliberately
  119. * advertises a PMU version to the guest beyond what is actually
  120. * supported by KVM to verify KVM doesn't freak out and do something
  121. * bizarre with an architecturally valid, but unsupported, version.
  122. */
  123. return min_t(uint8_t, kvm_pmu_version, this_cpu_property(X86_PROPERTY_PMU_VERSION));
  124. }
  125. /*
  126. * If an architectural event is supported and guaranteed to generate at least
  127. * one "hit, assert that its count is non-zero. If an event isn't supported or
  128. * the test can't guarantee the associated action will occur, then all bets are
  129. * off regarding the count, i.e. no checks can be done.
  130. *
  131. * Sanity check that in all cases, the event doesn't count when it's disabled,
  132. * and that KVM correctly emulates the write of an arbitrary value.
  133. */
  134. static void guest_assert_event_count(uint8_t idx, uint32_t pmc, uint32_t pmc_msr)
  135. {
  136. uint64_t count;
  137. count = _rdpmc(pmc);
  138. if (!(hardware_pmu_arch_events & BIT(idx)))
  139. goto sanity_checks;
  140. switch (idx) {
  141. case INTEL_ARCH_INSTRUCTIONS_RETIRED_INDEX:
  142. /* Relax precise count check due to VM-EXIT/VM-ENTRY overcount issue */
  143. if (this_pmu_has_errata(INSTRUCTIONS_RETIRED_OVERCOUNT))
  144. GUEST_ASSERT(count >= NUM_INSNS_RETIRED);
  145. else
  146. GUEST_ASSERT_EQ(count, NUM_INSNS_RETIRED);
  147. break;
  148. case INTEL_ARCH_BRANCHES_RETIRED_INDEX:
  149. /* Relax precise count check due to VM-EXIT/VM-ENTRY overcount issue */
  150. if (this_pmu_has_errata(BRANCHES_RETIRED_OVERCOUNT))
  151. GUEST_ASSERT(count >= NUM_BRANCH_INSNS_RETIRED);
  152. else
  153. GUEST_ASSERT_EQ(count, NUM_BRANCH_INSNS_RETIRED);
  154. break;
  155. case INTEL_ARCH_LLC_REFERENCES_INDEX:
  156. case INTEL_ARCH_LLC_MISSES_INDEX:
  157. if (!this_cpu_has(X86_FEATURE_CLFLUSHOPT) &&
  158. !this_cpu_has(X86_FEATURE_CLFLUSH))
  159. break;
  160. fallthrough;
  161. case INTEL_ARCH_CPU_CYCLES_INDEX:
  162. case INTEL_ARCH_REFERENCE_CYCLES_INDEX:
  163. case INTEL_ARCH_TOPDOWN_BE_BOUND_INDEX:
  164. case INTEL_ARCH_TOPDOWN_FE_BOUND_INDEX:
  165. GUEST_ASSERT_NE(count, 0);
  166. break;
  167. case INTEL_ARCH_TOPDOWN_SLOTS_INDEX:
  168. case INTEL_ARCH_TOPDOWN_RETIRING_INDEX:
  169. __GUEST_ASSERT(count >= NUM_INSNS_RETIRED,
  170. "Expected top-down slots >= %u, got count = %lu",
  171. NUM_INSNS_RETIRED, count);
  172. break;
  173. default:
  174. break;
  175. }
  176. sanity_checks:
  177. __asm__ __volatile__("loop ." : "+c"((int){NUM_LOOPS}));
  178. GUEST_ASSERT_EQ(_rdpmc(pmc), count);
  179. wrmsr(pmc_msr, 0xdead);
  180. GUEST_ASSERT_EQ(_rdpmc(pmc), 0xdead);
  181. }
  182. /*
  183. * Enable and disable the PMC in a monolithic asm blob to ensure that the
  184. * compiler can't insert _any_ code into the measured sequence. Note, ECX
  185. * doesn't need to be clobbered as the input value, @pmc_msr, is restored
  186. * before the end of the sequence.
  187. *
  188. * If CLFUSH{,OPT} is supported, flush the cacheline containing (at least) the
  189. * CLFUSH{,OPT} instruction on each loop iteration to force LLC references and
  190. * misses, i.e. to allow testing that those events actually count.
  191. *
  192. * If forced emulation is enabled (and specified), force emulation on a subset
  193. * of the measured code to verify that KVM correctly emulates instructions and
  194. * branches retired events in conjunction with hardware also counting said
  195. * events.
  196. */
  197. #define GUEST_MEASURE_EVENT(_msr, _value, clflush, FEP) \
  198. do { \
  199. __asm__ __volatile__("wrmsr\n\t" \
  200. " mov $" __stringify(NUM_LOOPS) ", %%ecx\n\t" \
  201. "1:\n\t" \
  202. FEP "enter $0, $0\n\t" \
  203. clflush "\n\t" \
  204. "mfence\n\t" \
  205. "mov %[m], %%eax\n\t" \
  206. FEP "leave\n\t" \
  207. FEP "loop 1b\n\t" \
  208. FEP "mov %%edi, %%ecx\n\t" \
  209. FEP "xor %%eax, %%eax\n\t" \
  210. FEP "xor %%edx, %%edx\n\t" \
  211. "wrmsr\n\t" \
  212. :: "a"((uint32_t)_value), "d"(_value >> 32), \
  213. "c"(_msr), "D"(_msr), [m]"m"(kvm_pmu_version) \
  214. ); \
  215. } while (0)
  216. #define GUEST_TEST_EVENT(_idx, _pmc, _pmc_msr, _ctrl_msr, _value, FEP) \
  217. do { \
  218. wrmsr(_pmc_msr, 0); \
  219. \
  220. if (this_cpu_has(X86_FEATURE_CLFLUSHOPT)) \
  221. GUEST_MEASURE_EVENT(_ctrl_msr, _value, "clflushopt %[m]", FEP); \
  222. else if (this_cpu_has(X86_FEATURE_CLFLUSH)) \
  223. GUEST_MEASURE_EVENT(_ctrl_msr, _value, "clflush %[m]", FEP); \
  224. else \
  225. GUEST_MEASURE_EVENT(_ctrl_msr, _value, "nop", FEP); \
  226. \
  227. guest_assert_event_count(_idx, _pmc, _pmc_msr); \
  228. } while (0)
  229. static void __guest_test_arch_event(uint8_t idx, uint32_t pmc, uint32_t pmc_msr,
  230. uint32_t ctrl_msr, uint64_t ctrl_msr_value)
  231. {
  232. GUEST_TEST_EVENT(idx, pmc, pmc_msr, ctrl_msr, ctrl_msr_value, "");
  233. if (is_forced_emulation_enabled)
  234. GUEST_TEST_EVENT(idx, pmc, pmc_msr, ctrl_msr, ctrl_msr_value, KVM_FEP);
  235. }
  236. static void guest_test_arch_event(uint8_t idx)
  237. {
  238. uint32_t nr_gp_counters = this_cpu_property(X86_PROPERTY_PMU_NR_GP_COUNTERS);
  239. uint32_t pmu_version = guest_get_pmu_version();
  240. /* PERF_GLOBAL_CTRL exists only for Architectural PMU Version 2+. */
  241. bool guest_has_perf_global_ctrl = pmu_version >= 2;
  242. struct kvm_x86_pmu_feature gp_event, fixed_event;
  243. uint32_t base_pmc_msr;
  244. unsigned int i;
  245. /* The host side shouldn't invoke this without a guest PMU. */
  246. GUEST_ASSERT(pmu_version);
  247. if (this_cpu_has(X86_FEATURE_PDCM) &&
  248. rdmsr(MSR_IA32_PERF_CAPABILITIES) & PMU_CAP_FW_WRITES)
  249. base_pmc_msr = MSR_IA32_PMC0;
  250. else
  251. base_pmc_msr = MSR_IA32_PERFCTR0;
  252. gp_event = intel_event_to_feature(idx).gp_event;
  253. GUEST_ASSERT_EQ(idx, gp_event.f.bit);
  254. GUEST_ASSERT(nr_gp_counters);
  255. for (i = 0; i < nr_gp_counters; i++) {
  256. uint64_t eventsel = ARCH_PERFMON_EVENTSEL_OS |
  257. ARCH_PERFMON_EVENTSEL_ENABLE |
  258. intel_pmu_arch_events[idx];
  259. wrmsr(MSR_P6_EVNTSEL0 + i, 0);
  260. if (guest_has_perf_global_ctrl)
  261. wrmsr(MSR_CORE_PERF_GLOBAL_CTRL, BIT_ULL(i));
  262. __guest_test_arch_event(idx, i, base_pmc_msr + i,
  263. MSR_P6_EVNTSEL0 + i, eventsel);
  264. }
  265. if (!guest_has_perf_global_ctrl)
  266. return;
  267. fixed_event = intel_event_to_feature(idx).fixed_event;
  268. if (pmu_is_null_feature(fixed_event) || !this_pmu_has(fixed_event))
  269. return;
  270. i = fixed_event.f.bit;
  271. wrmsr(MSR_CORE_PERF_FIXED_CTR_CTRL, FIXED_PMC_CTRL(i, FIXED_PMC_KERNEL));
  272. __guest_test_arch_event(idx, i | INTEL_RDPMC_FIXED,
  273. MSR_CORE_PERF_FIXED_CTR0 + i,
  274. MSR_CORE_PERF_GLOBAL_CTRL,
  275. FIXED_PMC_GLOBAL_CTRL_ENABLE(i));
  276. }
  277. static void guest_test_arch_events(void)
  278. {
  279. uint8_t i;
  280. for (i = 0; i < NR_INTEL_ARCH_EVENTS; i++)
  281. guest_test_arch_event(i);
  282. GUEST_DONE();
  283. }
  284. static void test_arch_events(uint8_t pmu_version, uint64_t perf_capabilities,
  285. uint8_t length, uint32_t unavailable_mask)
  286. {
  287. struct kvm_vcpu *vcpu;
  288. struct kvm_vm *vm;
  289. /* Testing arch events requires a vPMU (there are no negative tests). */
  290. if (!pmu_version)
  291. return;
  292. unavailable_mask &= GENMASK(X86_PROPERTY_PMU_EVENTS_MASK.hi_bit,
  293. X86_PROPERTY_PMU_EVENTS_MASK.lo_bit);
  294. vm = pmu_vm_create_with_one_vcpu(&vcpu, guest_test_arch_events,
  295. pmu_version, perf_capabilities);
  296. vcpu_set_cpuid_property(vcpu, X86_PROPERTY_PMU_EBX_BIT_VECTOR_LENGTH,
  297. length);
  298. vcpu_set_cpuid_property(vcpu, X86_PROPERTY_PMU_EVENTS_MASK,
  299. unavailable_mask);
  300. run_vcpu(vcpu);
  301. kvm_vm_free(vm);
  302. }
  303. /*
  304. * Limit testing to MSRs that are actually defined by Intel (in the SDM). MSRs
  305. * that aren't defined counter MSRs *probably* don't exist, but there's no
  306. * guarantee that currently undefined MSR indices won't be used for something
  307. * other than PMCs in the future.
  308. */
  309. #define MAX_NR_GP_COUNTERS 8
  310. #define MAX_NR_FIXED_COUNTERS 3
  311. #define GUEST_ASSERT_PMC_MSR_ACCESS(insn, msr, expect_gp, vector) \
  312. __GUEST_ASSERT(expect_gp ? vector == GP_VECTOR : !vector, \
  313. "Expected %s on " #insn "(0x%x), got %s", \
  314. expect_gp ? "#GP" : "no fault", msr, ex_str(vector)) \
  315. #define GUEST_ASSERT_PMC_VALUE(insn, msr, val, expected) \
  316. __GUEST_ASSERT(val == expected, \
  317. "Expected " #insn "(0x%x) to yield 0x%lx, got 0x%lx", \
  318. msr, expected, val);
  319. static void guest_test_rdpmc(uint32_t rdpmc_idx, bool expect_success,
  320. uint64_t expected_val)
  321. {
  322. uint8_t vector;
  323. uint64_t val;
  324. vector = rdpmc_safe(rdpmc_idx, &val);
  325. GUEST_ASSERT_PMC_MSR_ACCESS(RDPMC, rdpmc_idx, !expect_success, vector);
  326. if (expect_success)
  327. GUEST_ASSERT_PMC_VALUE(RDPMC, rdpmc_idx, val, expected_val);
  328. if (!is_forced_emulation_enabled)
  329. return;
  330. vector = rdpmc_safe_fep(rdpmc_idx, &val);
  331. GUEST_ASSERT_PMC_MSR_ACCESS(RDPMC, rdpmc_idx, !expect_success, vector);
  332. if (expect_success)
  333. GUEST_ASSERT_PMC_VALUE(RDPMC, rdpmc_idx, val, expected_val);
  334. }
  335. static void guest_rd_wr_counters(uint32_t base_msr, uint8_t nr_possible_counters,
  336. uint8_t nr_counters, uint32_t or_mask)
  337. {
  338. const bool pmu_has_fast_mode = !guest_get_pmu_version();
  339. uint8_t i;
  340. for (i = 0; i < nr_possible_counters; i++) {
  341. /*
  342. * TODO: Test a value that validates full-width writes and the
  343. * width of the counters.
  344. */
  345. const uint64_t test_val = 0xffff;
  346. const uint32_t msr = base_msr + i;
  347. /*
  348. * Fixed counters are supported if the counter is less than the
  349. * number of enumerated contiguous counters *or* the counter is
  350. * explicitly enumerated in the supported counters mask.
  351. */
  352. const bool expect_success = i < nr_counters || (or_mask & BIT(i));
  353. /*
  354. * KVM drops writes to MSR_P6_PERFCTR[0|1] if the counters are
  355. * unsupported, i.e. doesn't #GP and reads back '0'.
  356. */
  357. const uint64_t expected_val = expect_success ? test_val : 0;
  358. const bool expect_gp = !expect_success && msr != MSR_P6_PERFCTR0 &&
  359. msr != MSR_P6_PERFCTR1;
  360. uint32_t rdpmc_idx;
  361. uint8_t vector;
  362. uint64_t val;
  363. vector = wrmsr_safe(msr, test_val);
  364. GUEST_ASSERT_PMC_MSR_ACCESS(WRMSR, msr, expect_gp, vector);
  365. vector = rdmsr_safe(msr, &val);
  366. GUEST_ASSERT_PMC_MSR_ACCESS(RDMSR, msr, expect_gp, vector);
  367. /* On #GP, the result of RDMSR is undefined. */
  368. if (!expect_gp)
  369. GUEST_ASSERT_PMC_VALUE(RDMSR, msr, val, expected_val);
  370. /*
  371. * Redo the read tests with RDPMC, which has different indexing
  372. * semantics and additional capabilities.
  373. */
  374. rdpmc_idx = i;
  375. if (base_msr == MSR_CORE_PERF_FIXED_CTR0)
  376. rdpmc_idx |= INTEL_RDPMC_FIXED;
  377. guest_test_rdpmc(rdpmc_idx, expect_success, expected_val);
  378. /*
  379. * KVM doesn't support non-architectural PMUs, i.e. it should
  380. * impossible to have fast mode RDPMC. Verify that attempting
  381. * to use fast RDPMC always #GPs.
  382. */
  383. GUEST_ASSERT(!expect_success || !pmu_has_fast_mode);
  384. rdpmc_idx |= INTEL_RDPMC_FAST;
  385. guest_test_rdpmc(rdpmc_idx, false, -1ull);
  386. vector = wrmsr_safe(msr, 0);
  387. GUEST_ASSERT_PMC_MSR_ACCESS(WRMSR, msr, expect_gp, vector);
  388. }
  389. }
  390. static void guest_test_gp_counters(void)
  391. {
  392. uint8_t pmu_version = guest_get_pmu_version();
  393. uint8_t nr_gp_counters = 0;
  394. uint32_t base_msr;
  395. if (pmu_version)
  396. nr_gp_counters = this_cpu_property(X86_PROPERTY_PMU_NR_GP_COUNTERS);
  397. /*
  398. * For v2+ PMUs, PERF_GLOBAL_CTRL's architectural post-RESET value is
  399. * "Sets bits n-1:0 and clears the upper bits", where 'n' is the number
  400. * of GP counters. If there are no GP counters, require KVM to leave
  401. * PERF_GLOBAL_CTRL '0'. This edge case isn't covered by the SDM, but
  402. * follow the spirit of the architecture and only globally enable GP
  403. * counters, of which there are none.
  404. */
  405. if (pmu_version > 1) {
  406. uint64_t global_ctrl = rdmsr(MSR_CORE_PERF_GLOBAL_CTRL);
  407. if (nr_gp_counters)
  408. GUEST_ASSERT_EQ(global_ctrl, GENMASK_ULL(nr_gp_counters - 1, 0));
  409. else
  410. GUEST_ASSERT_EQ(global_ctrl, 0);
  411. }
  412. if (this_cpu_has(X86_FEATURE_PDCM) &&
  413. rdmsr(MSR_IA32_PERF_CAPABILITIES) & PMU_CAP_FW_WRITES)
  414. base_msr = MSR_IA32_PMC0;
  415. else
  416. base_msr = MSR_IA32_PERFCTR0;
  417. guest_rd_wr_counters(base_msr, MAX_NR_GP_COUNTERS, nr_gp_counters, 0);
  418. GUEST_DONE();
  419. }
  420. static void test_gp_counters(uint8_t pmu_version, uint64_t perf_capabilities,
  421. uint8_t nr_gp_counters)
  422. {
  423. struct kvm_vcpu *vcpu;
  424. struct kvm_vm *vm;
  425. vm = pmu_vm_create_with_one_vcpu(&vcpu, guest_test_gp_counters,
  426. pmu_version, perf_capabilities);
  427. vcpu_set_cpuid_property(vcpu, X86_PROPERTY_PMU_NR_GP_COUNTERS,
  428. nr_gp_counters);
  429. run_vcpu(vcpu);
  430. kvm_vm_free(vm);
  431. }
  432. static void guest_test_fixed_counters(void)
  433. {
  434. uint64_t supported_bitmask = 0;
  435. uint8_t nr_fixed_counters = 0;
  436. uint8_t i;
  437. /* Fixed counters require Architectural vPMU Version 2+. */
  438. if (guest_get_pmu_version() >= 2)
  439. nr_fixed_counters = this_cpu_property(X86_PROPERTY_PMU_NR_FIXED_COUNTERS);
  440. /*
  441. * The supported bitmask for fixed counters was introduced in PMU
  442. * version 5.
  443. */
  444. if (guest_get_pmu_version() >= 5)
  445. supported_bitmask = this_cpu_property(X86_PROPERTY_PMU_FIXED_COUNTERS_BITMASK);
  446. guest_rd_wr_counters(MSR_CORE_PERF_FIXED_CTR0, MAX_NR_FIXED_COUNTERS,
  447. nr_fixed_counters, supported_bitmask);
  448. for (i = 0; i < MAX_NR_FIXED_COUNTERS; i++) {
  449. uint8_t vector;
  450. uint64_t val;
  451. if (i >= nr_fixed_counters && !(supported_bitmask & BIT_ULL(i))) {
  452. vector = wrmsr_safe(MSR_CORE_PERF_FIXED_CTR_CTRL,
  453. FIXED_PMC_CTRL(i, FIXED_PMC_KERNEL));
  454. __GUEST_ASSERT(vector == GP_VECTOR,
  455. "Expected #GP for counter %u in FIXED_CTR_CTRL", i);
  456. vector = wrmsr_safe(MSR_CORE_PERF_GLOBAL_CTRL,
  457. FIXED_PMC_GLOBAL_CTRL_ENABLE(i));
  458. __GUEST_ASSERT(vector == GP_VECTOR,
  459. "Expected #GP for counter %u in PERF_GLOBAL_CTRL", i);
  460. continue;
  461. }
  462. wrmsr(MSR_CORE_PERF_FIXED_CTR0 + i, 0);
  463. wrmsr(MSR_CORE_PERF_FIXED_CTR_CTRL, FIXED_PMC_CTRL(i, FIXED_PMC_KERNEL));
  464. wrmsr(MSR_CORE_PERF_GLOBAL_CTRL, FIXED_PMC_GLOBAL_CTRL_ENABLE(i));
  465. __asm__ __volatile__("loop ." : "+c"((int){NUM_LOOPS}));
  466. wrmsr(MSR_CORE_PERF_GLOBAL_CTRL, 0);
  467. val = rdmsr(MSR_CORE_PERF_FIXED_CTR0 + i);
  468. GUEST_ASSERT_NE(val, 0);
  469. }
  470. GUEST_DONE();
  471. }
  472. static void test_fixed_counters(uint8_t pmu_version, uint64_t perf_capabilities,
  473. uint8_t nr_fixed_counters,
  474. uint32_t supported_bitmask)
  475. {
  476. struct kvm_vcpu *vcpu;
  477. struct kvm_vm *vm;
  478. vm = pmu_vm_create_with_one_vcpu(&vcpu, guest_test_fixed_counters,
  479. pmu_version, perf_capabilities);
  480. vcpu_set_cpuid_property(vcpu, X86_PROPERTY_PMU_FIXED_COUNTERS_BITMASK,
  481. supported_bitmask);
  482. vcpu_set_cpuid_property(vcpu, X86_PROPERTY_PMU_NR_FIXED_COUNTERS,
  483. nr_fixed_counters);
  484. run_vcpu(vcpu);
  485. kvm_vm_free(vm);
  486. }
  487. static void test_intel_counters(void)
  488. {
  489. uint8_t nr_fixed_counters = kvm_cpu_property(X86_PROPERTY_PMU_NR_FIXED_COUNTERS);
  490. uint8_t nr_gp_counters = kvm_cpu_property(X86_PROPERTY_PMU_NR_GP_COUNTERS);
  491. uint8_t pmu_version = kvm_cpu_property(X86_PROPERTY_PMU_VERSION);
  492. unsigned int i;
  493. uint8_t v, j;
  494. uint32_t k;
  495. const uint64_t perf_caps[] = {
  496. 0,
  497. PMU_CAP_FW_WRITES,
  498. };
  499. /*
  500. * To keep the total runtime reasonable, test only a handful of select,
  501. * semi-arbitrary values for the mask of unavailable PMU events. Test
  502. * 0 (all events available) and all ones (no events available) as well
  503. * as alternating bit sequencues, e.g. to detect if KVM is checking the
  504. * wrong bit(s).
  505. */
  506. const uint32_t unavailable_masks[] = {
  507. 0x0,
  508. 0xffffffffu,
  509. 0xaaaaaaaau,
  510. 0x55555555u,
  511. 0xf0f0f0f0u,
  512. 0x0f0f0f0fu,
  513. 0xa0a0a0a0u,
  514. 0x0a0a0a0au,
  515. 0x50505050u,
  516. 0x05050505u,
  517. };
  518. /*
  519. * Test up to PMU v5, which is the current maximum version defined by
  520. * Intel, i.e. is the last version that is guaranteed to be backwards
  521. * compatible with KVM's existing behavior.
  522. */
  523. uint8_t max_pmu_version = max_t(typeof(pmu_version), pmu_version, 5);
  524. /*
  525. * Detect the existence of events that aren't supported by selftests.
  526. * This will (obviously) fail any time hardware adds support for a new
  527. * event, but it's worth paying that price to keep the test fresh.
  528. */
  529. TEST_ASSERT(this_cpu_property(X86_PROPERTY_PMU_EBX_BIT_VECTOR_LENGTH) <= NR_INTEL_ARCH_EVENTS,
  530. "New architectural event(s) detected; please update this test (length = %u, mask = %x)",
  531. this_cpu_property(X86_PROPERTY_PMU_EBX_BIT_VECTOR_LENGTH),
  532. this_cpu_property(X86_PROPERTY_PMU_EVENTS_MASK));
  533. /*
  534. * Iterate over known arch events irrespective of KVM/hardware support
  535. * to verify that KVM doesn't reject programming of events just because
  536. * the *architectural* encoding is unsupported. Track which events are
  537. * supported in hardware; the guest side will validate supported events
  538. * count correctly, even if *enumeration* of the event is unsupported
  539. * by KVM and/or isn't exposed to the guest.
  540. */
  541. for (i = 0; i < NR_INTEL_ARCH_EVENTS; i++) {
  542. if (this_pmu_has(intel_event_to_feature(i).gp_event))
  543. hardware_pmu_arch_events |= BIT(i);
  544. }
  545. for (v = 0; v <= max_pmu_version; v++) {
  546. for (i = 0; i < ARRAY_SIZE(perf_caps); i++) {
  547. if (!kvm_has_perf_caps && perf_caps[i])
  548. continue;
  549. pr_info("Testing arch events, PMU version %u, perf_caps = %lx\n",
  550. v, perf_caps[i]);
  551. /*
  552. * Test single bits for all PMU version and lengths up
  553. * the number of events +1 (to verify KVM doesn't do
  554. * weird things if the guest length is greater than the
  555. * host length). Explicitly test a mask of '0' and all
  556. * ones i.e. all events being available and unavailable.
  557. */
  558. for (j = 0; j <= NR_INTEL_ARCH_EVENTS + 1; j++) {
  559. for (k = 1; k < ARRAY_SIZE(unavailable_masks); k++)
  560. test_arch_events(v, perf_caps[i], j, unavailable_masks[k]);
  561. }
  562. pr_info("Testing GP counters, PMU version %u, perf_caps = %lx\n",
  563. v, perf_caps[i]);
  564. for (j = 0; j <= nr_gp_counters; j++)
  565. test_gp_counters(v, perf_caps[i], j);
  566. pr_info("Testing fixed counters, PMU version %u, perf_caps = %lx\n",
  567. v, perf_caps[i]);
  568. for (j = 0; j <= nr_fixed_counters; j++) {
  569. for (k = 0; k <= (BIT(nr_fixed_counters) - 1); k++)
  570. test_fixed_counters(v, perf_caps[i], j, k);
  571. }
  572. }
  573. }
  574. }
  575. int main(int argc, char *argv[])
  576. {
  577. TEST_REQUIRE(kvm_is_pmu_enabled());
  578. TEST_REQUIRE(host_cpu_is_intel);
  579. TEST_REQUIRE(kvm_cpu_has_p(X86_PROPERTY_PMU_VERSION));
  580. TEST_REQUIRE(kvm_cpu_property(X86_PROPERTY_PMU_VERSION) > 0);
  581. kvm_pmu_version = kvm_cpu_property(X86_PROPERTY_PMU_VERSION);
  582. kvm_has_perf_caps = kvm_cpu_has(X86_FEATURE_PDCM);
  583. test_intel_counters();
  584. return 0;
  585. }