Path: blob/master/tools/testing/selftests/kvm/x86/feature_msrs_test.c
38237 views
// SPDX-License-Identifier: GPL-2.01/*2* Copyright (C) 2020, Red Hat, Inc.3*/4#include <fcntl.h>5#include <stdio.h>6#include <stdlib.h>7#include <string.h>8#include <sys/ioctl.h>910#include "test_util.h"11#include "kvm_util.h"12#include "processor.h"1314static bool is_kvm_controlled_msr(uint32_t msr)15{16return msr == MSR_IA32_VMX_CR0_FIXED1 || msr == MSR_IA32_VMX_CR4_FIXED1;17}1819/*20* For VMX MSRs with a "true" variant, KVM requires userspace to set the "true"21* MSR, and doesn't allow setting the hidden version.22*/23static bool is_hidden_vmx_msr(uint32_t msr)24{25switch (msr) {26case MSR_IA32_VMX_PINBASED_CTLS:27case MSR_IA32_VMX_PROCBASED_CTLS:28case MSR_IA32_VMX_EXIT_CTLS:29case MSR_IA32_VMX_ENTRY_CTLS:30return true;31default:32return false;33}34}3536static bool is_quirked_msr(uint32_t msr)37{38return msr != MSR_AMD64_DE_CFG;39}4041static void test_feature_msr(uint32_t msr)42{43const uint64_t supported_mask = kvm_get_feature_msr(msr);44uint64_t reset_value = is_quirked_msr(msr) ? supported_mask : 0;45struct kvm_vcpu *vcpu;46struct kvm_vm *vm;4748/*49* Don't bother testing KVM-controlled MSRs beyond verifying that the50* MSR can be read from userspace. Any value is effectively legal, as51* KVM is bound by x86 architecture, not by ABI.52*/53if (is_kvm_controlled_msr(msr))54return;5556/*57* More goofy behavior. KVM reports the host CPU's actual revision ID,58* but initializes the vCPU's revision ID to an arbitrary value.59*/60if (msr == MSR_IA32_UCODE_REV)61reset_value = host_cpu_is_intel ? 0x100000000ULL : 0x01000065;6263/*64* For quirked MSRs, KVM's ABI is to initialize the vCPU's value to the65* full set of features supported by KVM. For non-quirked MSRs, and66* when the quirk is disabled, KVM must zero-initialize the MSR and let67* userspace do the configuration.68*/69vm = vm_create_with_one_vcpu(&vcpu, NULL);70TEST_ASSERT(vcpu_get_msr(vcpu, msr) == reset_value,71"Wanted 0x%lx for %squirked MSR 0x%x, got 0x%lx",72reset_value, is_quirked_msr(msr) ? "" : "non-", msr,73vcpu_get_msr(vcpu, msr));74if (!is_hidden_vmx_msr(msr))75vcpu_set_msr(vcpu, msr, supported_mask);76kvm_vm_free(vm);7778if (is_hidden_vmx_msr(msr))79return;8081if (!kvm_has_cap(KVM_CAP_DISABLE_QUIRKS2) ||82!(kvm_check_cap(KVM_CAP_DISABLE_QUIRKS2) & KVM_X86_QUIRK_STUFF_FEATURE_MSRS))83return;8485vm = vm_create(1);86vm_enable_cap(vm, KVM_CAP_DISABLE_QUIRKS2, KVM_X86_QUIRK_STUFF_FEATURE_MSRS);8788vcpu = vm_vcpu_add(vm, 0, NULL);89TEST_ASSERT(!vcpu_get_msr(vcpu, msr),90"Quirk disabled, wanted '0' for MSR 0x%x, got 0x%lx",91msr, vcpu_get_msr(vcpu, msr));92kvm_vm_free(vm);93}9495int main(int argc, char *argv[])96{97const struct kvm_msr_list *feature_list;98int i;99100/*101* Skip the entire test if MSR_FEATURES isn't supported, other tests102* will cover the "regular" list of MSRs, the coverage here is purely103* opportunistic and not interesting on its own.104*/105TEST_REQUIRE(kvm_has_cap(KVM_CAP_GET_MSR_FEATURES));106107(void)kvm_get_msr_index_list();108109feature_list = kvm_get_feature_msr_index_list();110for (i = 0; i < feature_list->nmsrs; i++)111test_feature_msr(feature_list->indices[i]);112}113114115