Path: blob/master/tools/testing/selftests/kvm/x86/aperfmperf_test.c
38237 views
// SPDX-License-Identifier: GPL-2.0-only1/*2* Test for KVM_X86_DISABLE_EXITS_APERFMPERF3*4* Copyright (C) 2025, Google LLC.5*6* Test the ability to disable VM-exits for rdmsr of IA32_APERF and7* IA32_MPERF. When these VM-exits are disabled, reads of these MSRs8* return the host's values.9*10* Note: Requires read access to /dev/cpu/<lpu>/msr to read host MSRs.11*/1213#include <fcntl.h>14#include <limits.h>15#include <stdbool.h>16#include <stdio.h>17#include <stdint.h>18#include <unistd.h>19#include <asm/msr-index.h>2021#include "kvm_util.h"22#include "processor.h"23#include "svm_util.h"24#include "test_util.h"25#include "vmx.h"2627#define NUM_ITERATIONS 100002829static int open_dev_msr(int cpu)30{31char path[PATH_MAX];3233snprintf(path, sizeof(path), "/dev/cpu/%d/msr", cpu);34return open_path_or_exit(path, O_RDONLY);35}3637static uint64_t read_dev_msr(int msr_fd, uint32_t msr)38{39uint64_t data;40ssize_t rc;4142rc = pread(msr_fd, &data, sizeof(data), msr);43TEST_ASSERT(rc == sizeof(data), "Read of MSR 0x%x failed", msr);4445return data;46}4748static void guest_read_aperf_mperf(void)49{50int i;5152for (i = 0; i < NUM_ITERATIONS; i++)53GUEST_SYNC2(rdmsr(MSR_IA32_APERF), rdmsr(MSR_IA32_MPERF));54}5556#define L2_GUEST_STACK_SIZE 645758static void l2_guest_code(void)59{60guest_read_aperf_mperf();61GUEST_DONE();62}6364static void l1_svm_code(struct svm_test_data *svm)65{66unsigned long l2_guest_stack[L2_GUEST_STACK_SIZE];67struct vmcb *vmcb = svm->vmcb;6869generic_svm_setup(svm, l2_guest_code, &l2_guest_stack[L2_GUEST_STACK_SIZE]);70run_guest(vmcb, svm->vmcb_gpa);71}7273static void l1_vmx_code(struct vmx_pages *vmx)74{75unsigned long l2_guest_stack[L2_GUEST_STACK_SIZE];7677GUEST_ASSERT_EQ(prepare_for_vmx_operation(vmx), true);78GUEST_ASSERT_EQ(load_vmcs(vmx), true);7980prepare_vmcs(vmx, NULL, &l2_guest_stack[L2_GUEST_STACK_SIZE]);8182/*83* Enable MSR bitmaps (the bitmap itself is allocated, zeroed, and set84* in the VMCS by prepare_vmcs()), as MSR exiting mandatory on Intel.85*/86vmwrite(CPU_BASED_VM_EXEC_CONTROL,87vmreadz(CPU_BASED_VM_EXEC_CONTROL) | CPU_BASED_USE_MSR_BITMAPS);8889GUEST_ASSERT(!vmwrite(GUEST_RIP, (u64)l2_guest_code));90GUEST_ASSERT(!vmlaunch());91}9293static void guest_code(void *nested_test_data)94{95guest_read_aperf_mperf();9697if (this_cpu_has(X86_FEATURE_SVM))98l1_svm_code(nested_test_data);99else if (this_cpu_has(X86_FEATURE_VMX))100l1_vmx_code(nested_test_data);101else102GUEST_DONE();103104TEST_FAIL("L2 should have signaled 'done'");105}106107static void guest_no_aperfmperf(void)108{109uint64_t msr_val;110uint8_t vector;111112vector = rdmsr_safe(MSR_IA32_APERF, &msr_val);113GUEST_ASSERT(vector == GP_VECTOR);114115vector = rdmsr_safe(MSR_IA32_APERF, &msr_val);116GUEST_ASSERT(vector == GP_VECTOR);117118GUEST_DONE();119}120121int main(int argc, char *argv[])122{123const bool has_nested = kvm_cpu_has(X86_FEATURE_SVM) || kvm_cpu_has(X86_FEATURE_VMX);124uint64_t host_aperf_before, host_mperf_before;125vm_vaddr_t nested_test_data_gva;126struct kvm_vcpu *vcpu;127struct kvm_vm *vm;128int msr_fd, cpu, i;129130/* Sanity check that APERF/MPERF are unsupported by default. */131vm = vm_create_with_one_vcpu(&vcpu, guest_no_aperfmperf);132vcpu_run(vcpu);133TEST_ASSERT_EQ(get_ucall(vcpu, NULL), UCALL_DONE);134kvm_vm_free(vm);135136cpu = pin_self_to_any_cpu();137138msr_fd = open_dev_msr(cpu);139140/*141* This test requires a non-standard VM initialization, because142* KVM_ENABLE_CAP cannot be used on a VM file descriptor after143* a VCPU has been created.144*/145vm = vm_create(1);146147TEST_REQUIRE(vm_check_cap(vm, KVM_CAP_X86_DISABLE_EXITS) &148KVM_X86_DISABLE_EXITS_APERFMPERF);149150vm_enable_cap(vm, KVM_CAP_X86_DISABLE_EXITS,151KVM_X86_DISABLE_EXITS_APERFMPERF);152153vcpu = vm_vcpu_add(vm, 0, guest_code);154155if (!has_nested)156nested_test_data_gva = NONCANONICAL;157else if (kvm_cpu_has(X86_FEATURE_SVM))158vcpu_alloc_svm(vm, &nested_test_data_gva);159else160vcpu_alloc_vmx(vm, &nested_test_data_gva);161162vcpu_args_set(vcpu, 1, nested_test_data_gva);163164host_aperf_before = read_dev_msr(msr_fd, MSR_IA32_APERF);165host_mperf_before = read_dev_msr(msr_fd, MSR_IA32_MPERF);166167for (i = 0; i <= NUM_ITERATIONS * (1 + has_nested); i++) {168uint64_t host_aperf_after, host_mperf_after;169uint64_t guest_aperf, guest_mperf;170struct ucall uc;171172vcpu_run(vcpu);173TEST_ASSERT_KVM_EXIT_REASON(vcpu, KVM_EXIT_IO);174175switch (get_ucall(vcpu, &uc)) {176case UCALL_DONE:177goto done;178case UCALL_ABORT:179REPORT_GUEST_ASSERT(uc);180case UCALL_SYNC:181guest_aperf = uc.args[0];182guest_mperf = uc.args[1];183184host_aperf_after = read_dev_msr(msr_fd, MSR_IA32_APERF);185host_mperf_after = read_dev_msr(msr_fd, MSR_IA32_MPERF);186187TEST_ASSERT(host_aperf_before < guest_aperf,188"APERF: host_before (0x%" PRIx64 ") >= guest (0x%" PRIx64 ")",189host_aperf_before, guest_aperf);190TEST_ASSERT(guest_aperf < host_aperf_after,191"APERF: guest (0x%" PRIx64 ") >= host_after (0x%" PRIx64 ")",192guest_aperf, host_aperf_after);193TEST_ASSERT(host_mperf_before < guest_mperf,194"MPERF: host_before (0x%" PRIx64 ") >= guest (0x%" PRIx64 ")",195host_mperf_before, guest_mperf);196TEST_ASSERT(guest_mperf < host_mperf_after,197"MPERF: guest (0x%" PRIx64 ") >= host_after (0x%" PRIx64 ")",198guest_mperf, host_mperf_after);199200host_aperf_before = host_aperf_after;201host_mperf_before = host_mperf_after;202203break;204}205}206TEST_FAIL("Didn't receive UCALL_DONE\n");207done:208kvm_vm_free(vm);209close(msr_fd);210211return 0;212}213214215