1 // SPDX-License-Identifier: GPL-2.0
3 * Test for x86 KVM_SET_PMU_EVENT_FILTER.
5 * Copyright (C) 2022, Google LLC.
7 * This work is licensed under the terms of the GNU GPL, version 2.
9 * Verifies the expected behavior of allow lists and deny lists for
13 #define _GNU_SOURCE /* for program_invocation_short_name */
14 #include "test_util.h"
16 #include "processor.h"
19 * In lieu of copying perf_event.h into tools...
21 #define ARCH_PERFMON_EVENTSEL_OS (1ULL << 17)
22 #define ARCH_PERFMON_EVENTSEL_ENABLE (1ULL << 22)
26 unsigned int version_id:8;
27 unsigned int num_counters:8;
28 unsigned int bit_width:8;
29 unsigned int mask_length:8;
36 unsigned int no_unhalted_core_cycles:1;
37 unsigned int no_instructions_retired:1;
38 unsigned int no_unhalted_reference_cycles:1;
39 unsigned int no_llc_reference:1;
40 unsigned int no_llc_misses:1;
41 unsigned int no_branch_instruction_retired:1;
42 unsigned int no_branch_misses_retired:1;
47 /* End of stuff taken from perf_event.h. */
49 /* Oddly, this isn't in perf_event.h. */
50 #define ARCH_PERFMON_BRANCHES_RETIRED 5
53 #define NUM_BRANCHES 42
56 * This is how the event selector and unit mask are stored in an AMD
57 * core performance event-select register. Intel's format is similar,
58 * but the event selector is only 8 bits.
60 #define EVENT(select, umask) ((select & 0xf00UL) << 24 | (select & 0xff) | \
64 * "Branch instructions retired", from the Intel SDM, volume 3,
65 * "Pre-defined Architectural Performance Events."
68 #define INTEL_BR_RETIRED EVENT(0xc4, 0)
71 * "Retired branch instructions", from Processor Programming Reference
72 * (PPR) for AMD Family 17h Model 01h, Revision B1 Processors,
73 * Preliminary Processor Programming Reference (PPR) for AMD Family
74 * 17h Model 31h, Revision B0 Processors, and Preliminary Processor
75 * Programming Reference (PPR) for AMD Family 19h Model 01h, Revision
76 * B1 Processors Volume 1 of 2.
79 #define AMD_ZEN_BR_RETIRED EVENT(0xc2, 0)
82 * This event list comprises Intel's eight architectural events plus
83 * AMD's "retired branch instructions" for Zen[123] (and possibly
86 static const uint64_t event_list[] = {
99 * If we encounter a #GP during the guest PMU sanity check, then the guest
100 * PMU is not functional. Inform the hypervisor via GUEST_SYNC(0).
102 static void guest_gp_handler(struct ex_regs *regs)
108 * Check that we can write a new value to the given MSR and read it back.
109 * The caller should provide a non-empty set of bits that are safe to flip.
111 * Return on success. GUEST_SYNC(0) on error.
113 static void check_msr(uint32_t msr, uint64_t bits_to_flip)
115 uint64_t v = rdmsr(msr) ^ bits_to_flip;
127 static void intel_guest_code(void)
129 check_msr(MSR_CORE_PERF_GLOBAL_CTRL, 1);
130 check_msr(MSR_P6_EVNTSEL0, 0xffff);
131 check_msr(MSR_IA32_PMC0, 0xffff);
137 wrmsr(MSR_CORE_PERF_GLOBAL_CTRL, 0);
138 wrmsr(MSR_P6_EVNTSEL0, ARCH_PERFMON_EVENTSEL_ENABLE |
139 ARCH_PERFMON_EVENTSEL_OS | INTEL_BR_RETIRED);
140 wrmsr(MSR_CORE_PERF_GLOBAL_CTRL, 1);
141 br0 = rdmsr(MSR_IA32_PMC0);
142 __asm__ __volatile__("loop ." : "+c"((int){NUM_BRANCHES}));
143 br1 = rdmsr(MSR_IA32_PMC0);
144 GUEST_SYNC(br1 - br0);
149 * To avoid needing a check for CPUID.80000001:ECX.PerfCtrExtCore[bit 23],
150 * this code uses the always-available, legacy K7 PMU MSRs, which alias to
151 * the first four of the six extended core PMU MSRs.
153 static void amd_guest_code(void)
155 check_msr(MSR_K7_EVNTSEL0, 0xffff);
156 check_msr(MSR_K7_PERFCTR0, 0xffff);
162 wrmsr(MSR_K7_EVNTSEL0, 0);
163 wrmsr(MSR_K7_EVNTSEL0, ARCH_PERFMON_EVENTSEL_ENABLE |
164 ARCH_PERFMON_EVENTSEL_OS | AMD_ZEN_BR_RETIRED);
165 br0 = rdmsr(MSR_K7_PERFCTR0);
166 __asm__ __volatile__("loop ." : "+c"((int){NUM_BRANCHES}));
167 br1 = rdmsr(MSR_K7_PERFCTR0);
168 GUEST_SYNC(br1 - br0);
173 * Run the VM to the next GUEST_SYNC(value), and return the value passed
174 * to the sync. Any other exit from the guest is fatal.
176 static uint64_t run_vm_to_sync(struct kvm_vm *vm)
178 struct kvm_run *run = vcpu_state(vm, VCPU_ID);
181 vcpu_run(vm, VCPU_ID);
182 TEST_ASSERT(run->exit_reason == KVM_EXIT_IO,
183 "Exit_reason other than KVM_EXIT_IO: %u (%s)\n",
185 exit_reason_str(run->exit_reason));
186 get_ucall(vm, VCPU_ID, &uc);
187 TEST_ASSERT(uc.cmd == UCALL_SYNC,
188 "Received ucall other than UCALL_SYNC: %lu", uc.cmd);
193 * In a nested environment or if the vPMU is disabled, the guest PMU
194 * might not work as architected (accessing the PMU MSRs may raise
195 * #GP, or writes could simply be discarded). In those situations,
196 * there is no point in running these tests. The guest code will perform
197 * a sanity check and then GUEST_SYNC(success). In the case of failure,
198 * the behavior of the guest on resumption is undefined.
200 static bool sanity_check_pmu(struct kvm_vm *vm)
204 vm_install_exception_handler(vm, GP_VECTOR, guest_gp_handler);
205 success = run_vm_to_sync(vm);
206 vm_install_exception_handler(vm, GP_VECTOR, NULL);
211 static struct kvm_pmu_event_filter *make_pmu_event_filter(uint32_t nevents)
213 struct kvm_pmu_event_filter *f;
214 int size = sizeof(*f) + nevents * sizeof(f->events[0]);
217 TEST_ASSERT(f, "Out of memory");
219 f->nevents = nevents;
223 static struct kvm_pmu_event_filter *event_filter(uint32_t action)
225 struct kvm_pmu_event_filter *f;
228 f = make_pmu_event_filter(ARRAY_SIZE(event_list));
230 for (i = 0; i < ARRAY_SIZE(event_list); i++)
231 f->events[i] = event_list[i];
237 * Remove the first occurrence of 'event' (if any) from the filter's
240 static struct kvm_pmu_event_filter *remove_event(struct kvm_pmu_event_filter *f,
246 for (i = 0; i < f->nevents; i++) {
248 f->events[i - 1] = f->events[i];
250 found = f->events[i] == event;
257 static void test_without_filter(struct kvm_vm *vm)
259 uint64_t count = run_vm_to_sync(vm);
261 if (count != NUM_BRANCHES)
262 pr_info("%s: Branch instructions retired = %lu (expected %u)\n",
263 __func__, count, NUM_BRANCHES);
264 TEST_ASSERT(count, "Allowed PMU event is not counting");
267 static uint64_t test_with_filter(struct kvm_vm *vm,
268 struct kvm_pmu_event_filter *f)
270 vm_ioctl(vm, KVM_SET_PMU_EVENT_FILTER, (void *)f);
271 return run_vm_to_sync(vm);
274 static void test_member_deny_list(struct kvm_vm *vm)
276 struct kvm_pmu_event_filter *f = event_filter(KVM_PMU_EVENT_DENY);
277 uint64_t count = test_with_filter(vm, f);
281 pr_info("%s: Branch instructions retired = %lu (expected 0)\n",
283 TEST_ASSERT(!count, "Disallowed PMU Event is counting");
286 static void test_member_allow_list(struct kvm_vm *vm)
288 struct kvm_pmu_event_filter *f = event_filter(KVM_PMU_EVENT_ALLOW);
289 uint64_t count = test_with_filter(vm, f);
292 if (count != NUM_BRANCHES)
293 pr_info("%s: Branch instructions retired = %lu (expected %u)\n",
294 __func__, count, NUM_BRANCHES);
295 TEST_ASSERT(count, "Allowed PMU event is not counting");
298 static void test_not_member_deny_list(struct kvm_vm *vm)
300 struct kvm_pmu_event_filter *f = event_filter(KVM_PMU_EVENT_DENY);
303 remove_event(f, INTEL_BR_RETIRED);
304 remove_event(f, AMD_ZEN_BR_RETIRED);
305 count = test_with_filter(vm, f);
307 if (count != NUM_BRANCHES)
308 pr_info("%s: Branch instructions retired = %lu (expected %u)\n",
309 __func__, count, NUM_BRANCHES);
310 TEST_ASSERT(count, "Allowed PMU event is not counting");
313 static void test_not_member_allow_list(struct kvm_vm *vm)
315 struct kvm_pmu_event_filter *f = event_filter(KVM_PMU_EVENT_ALLOW);
318 remove_event(f, INTEL_BR_RETIRED);
319 remove_event(f, AMD_ZEN_BR_RETIRED);
320 count = test_with_filter(vm, f);
323 pr_info("%s: Branch instructions retired = %lu (expected 0)\n",
325 TEST_ASSERT(!count, "Disallowed PMU Event is counting");
329 * Check for a non-zero PMU version, at least one general-purpose
330 * counter per logical processor, an EBX bit vector of length greater
331 * than 5, and EBX[5] clear.
333 static bool check_intel_pmu_leaf(struct kvm_cpuid_entry2 *entry)
335 union cpuid10_eax eax = { .full = entry->eax };
336 union cpuid10_ebx ebx = { .full = entry->ebx };
338 return eax.split.version_id && eax.split.num_counters > 0 &&
339 eax.split.mask_length > ARCH_PERFMON_BRANCHES_RETIRED &&
340 !ebx.split.no_branch_instruction_retired;
344 * Note that CPUID leaf 0xa is Intel-specific. This leaf should be
345 * clear on AMD hardware.
347 static bool use_intel_pmu(void)
349 struct kvm_cpuid_entry2 *entry;
351 entry = kvm_get_supported_cpuid_index(0xa, 0);
352 return is_intel_cpu() && entry && check_intel_pmu_leaf(entry);
355 static bool is_zen1(uint32_t eax)
357 return x86_family(eax) == 0x17 && x86_model(eax) <= 0x0f;
360 static bool is_zen2(uint32_t eax)
362 return x86_family(eax) == 0x17 &&
363 x86_model(eax) >= 0x30 && x86_model(eax) <= 0x3f;
366 static bool is_zen3(uint32_t eax)
368 return x86_family(eax) == 0x19 && x86_model(eax) <= 0x0f;
372 * Determining AMD support for a PMU event requires consulting the AMD
373 * PPR for the CPU or reference material derived therefrom. The AMD
374 * test code herein has been verified to work on Zen1, Zen2, and Zen3.
376 * Feel free to add more AMD CPUs that are documented to support event
377 * select 0xc2 umask 0 as "retired branch instructions."
379 static bool use_amd_pmu(void)
381 struct kvm_cpuid_entry2 *entry;
383 entry = kvm_get_supported_cpuid_index(1, 0);
384 return is_amd_cpu() && entry &&
385 (is_zen1(entry->eax) ||
386 is_zen2(entry->eax) ||
387 is_zen3(entry->eax));
390 int main(int argc, char *argv[])
392 void (*guest_code)(void) = NULL;
396 /* Tell stdout not to buffer its content */
397 setbuf(stdout, NULL);
399 r = kvm_check_cap(KVM_CAP_PMU_EVENT_FILTER);
401 print_skip("KVM_CAP_PMU_EVENT_FILTER not supported");
406 guest_code = intel_guest_code;
407 else if (use_amd_pmu())
408 guest_code = amd_guest_code;
411 print_skip("Don't know how to test this guest PMU");
415 vm = vm_create_default(VCPU_ID, 0, guest_code);
417 vm_init_descriptor_tables(vm);
418 vcpu_init_descriptor_tables(vm, VCPU_ID);
420 if (!sanity_check_pmu(vm)) {
421 print_skip("Guest PMU is not functional");
425 test_without_filter(vm);
426 test_member_deny_list(vm);
427 test_member_allow_list(vm);
428 test_not_member_deny_list(vm);
429 test_not_member_allow_list(vm);