Merge tag 'trace-printf-v6.13' of git://git.kernel.org/pub/scm/linux/kernel/git/trace...
[drm/drm-misc.git] / tools / testing / selftests / kvm / x86_64 / feature_msrs_test.c
bloba72f13ae2edbb05900ea4893437e70b0949f87c0
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * Copyright (C) 2020, Red Hat, Inc.
4 */
5 #include <fcntl.h>
6 #include <stdio.h>
7 #include <stdlib.h>
8 #include <string.h>
9 #include <sys/ioctl.h>
11 #include "test_util.h"
12 #include "kvm_util.h"
13 #include "processor.h"
15 static bool is_kvm_controlled_msr(uint32_t msr)
17 return msr == MSR_IA32_VMX_CR0_FIXED1 || msr == MSR_IA32_VMX_CR4_FIXED1;
21 * For VMX MSRs with a "true" variant, KVM requires userspace to set the "true"
22 * MSR, and doesn't allow setting the hidden version.
24 static bool is_hidden_vmx_msr(uint32_t msr)
26 switch (msr) {
27 case MSR_IA32_VMX_PINBASED_CTLS:
28 case MSR_IA32_VMX_PROCBASED_CTLS:
29 case MSR_IA32_VMX_EXIT_CTLS:
30 case MSR_IA32_VMX_ENTRY_CTLS:
31 return true;
32 default:
33 return false;
37 static bool is_quirked_msr(uint32_t msr)
39 return msr != MSR_AMD64_DE_CFG;
42 static void test_feature_msr(uint32_t msr)
44 const uint64_t supported_mask = kvm_get_feature_msr(msr);
45 uint64_t reset_value = is_quirked_msr(msr) ? supported_mask : 0;
46 struct kvm_vcpu *vcpu;
47 struct kvm_vm *vm;
50 * Don't bother testing KVM-controlled MSRs beyond verifying that the
51 * MSR can be read from userspace. Any value is effectively legal, as
52 * KVM is bound by x86 architecture, not by ABI.
54 if (is_kvm_controlled_msr(msr))
55 return;
58 * More goofy behavior. KVM reports the host CPU's actual revision ID,
59 * but initializes the vCPU's revision ID to an arbitrary value.
61 if (msr == MSR_IA32_UCODE_REV)
62 reset_value = host_cpu_is_intel ? 0x100000000ULL : 0x01000065;
65 * For quirked MSRs, KVM's ABI is to initialize the vCPU's value to the
66 * full set of features supported by KVM. For non-quirked MSRs, and
67 * when the quirk is disabled, KVM must zero-initialize the MSR and let
68 * userspace do the configuration.
70 vm = vm_create_with_one_vcpu(&vcpu, NULL);
71 TEST_ASSERT(vcpu_get_msr(vcpu, msr) == reset_value,
72 "Wanted 0x%lx for %squirked MSR 0x%x, got 0x%lx",
73 reset_value, is_quirked_msr(msr) ? "" : "non-", msr,
74 vcpu_get_msr(vcpu, msr));
75 if (!is_hidden_vmx_msr(msr))
76 vcpu_set_msr(vcpu, msr, supported_mask);
77 kvm_vm_free(vm);
79 if (is_hidden_vmx_msr(msr))
80 return;
82 if (!kvm_has_cap(KVM_CAP_DISABLE_QUIRKS2) ||
83 !(kvm_check_cap(KVM_CAP_DISABLE_QUIRKS2) & KVM_X86_QUIRK_STUFF_FEATURE_MSRS))
84 return;
86 vm = vm_create(1);
87 vm_enable_cap(vm, KVM_CAP_DISABLE_QUIRKS2, KVM_X86_QUIRK_STUFF_FEATURE_MSRS);
89 vcpu = vm_vcpu_add(vm, 0, NULL);
90 TEST_ASSERT(!vcpu_get_msr(vcpu, msr),
91 "Quirk disabled, wanted '0' for MSR 0x%x, got 0x%lx",
92 msr, vcpu_get_msr(vcpu, msr));
93 kvm_vm_free(vm);
96 int main(int argc, char *argv[])
98 const struct kvm_msr_list *feature_list;
99 int i;
102 * Skip the entire test if MSR_FEATURES isn't supported, other tests
103 * will cover the "regular" list of MSRs, the coverage here is purely
104 * opportunistic and not interesting on its own.
106 TEST_REQUIRE(kvm_has_cap(KVM_CAP_GET_MSR_FEATURES));
108 (void)kvm_get_msr_index_list();
110 feature_list = kvm_get_feature_msr_index_list();
111 for (i = 0; i < feature_list->nmsrs; i++)
112 test_feature_msr(feature_list->indices[i]);