Hyper-V enables CET for its guests when L1 is offered mode-based execute control. It then needs supervisor shadow-stack control for nested EPT and the VM-entry and VM-exit controls that load CET state, with and without the enlightened VMCS. Read the capabilities from L1, once without and once with eVMCS, since KVM filters the control MSRs for eVMCS only for reads by the guest. KVM can offer them only if the host has them, so take the expectations from the host's VMX capability MSRs through /dev/cpu/0/msr, and skip if they cannot be read. This keeps the test from failing where the host itself is a guest of a hypervisor that does not offer them. Without the fixes in this series the test fails: supervisor shadow-stack control is missing, and with eVMCS also the CET controls. Signed-off-by: Dennis Reußwig --- tools/testing/selftests/kvm/Makefile.kvm | 1 + .../selftests/kvm/x86/vmx_nested_cet_caps_test.c | 140 +++++++++++++++++++++ 2 files changed, 141 insertions(+) diff --git a/tools/testing/selftests/kvm/Makefile.kvm b/tools/testing/selftests/kvm/Makefile.kvm index 2b502c9ec7cb..9e62ed2bec1a 100644 --- a/tools/testing/selftests/kvm/Makefile.kvm +++ b/tools/testing/selftests/kvm/Makefile.kvm @@ -148,6 +148,7 @@ TEST_GEN_PROGS_x86 += x86/vmx_invalid_nested_guest_state TEST_GEN_PROGS_x86 += x86/vmx_nested_la57_state_test TEST_GEN_PROGS_x86 += x86/vmx_nested_mbec_test TEST_GEN_PROGS_x86 += x86/vmx_nested_sss_test +TEST_GEN_PROGS_x86 += x86/vmx_nested_cet_caps_test TEST_GEN_PROGS_x86 += x86/apic_bus_clock_test TEST_GEN_PROGS_x86 += x86/apic_id_routing_test TEST_GEN_PROGS_x86 += x86/xapic_ipi_test diff --git a/tools/testing/selftests/kvm/x86/vmx_nested_cet_caps_test.c b/tools/testing/selftests/kvm/x86/vmx_nested_cet_caps_test.c new file mode 100644 index 000000000000..67e6f8f6ba8d --- /dev/null +++ b/tools/testing/selftests/kvm/x86/vmx_nested_cet_caps_test.c @@ -0,0 +1,140 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* + * VMX capabilities that L1 needs to run L2 guests with CET. + * + * Hyper-V enables CET for its guests when L1 is offered mode-based execute + * control (MBEC). It then needs supervisor shadow-stack control for nested + * EPT (IA32_VMX_EPT_VPID_CAP bit 23) and the VM-entry/VM-exit controls that + * load CET state, with and without the enlightened VMCS. KVM can offer them + * only if the host has them, so the expectations come from the host's VMX + * capability MSRs, read through /dev/cpu/0/msr. KVM filters the control + * MSRs for eVMCS only when the guest reads them, so L1 reads them itself. + */ +#include +#include + +#include "test_util.h" +#include "kvm_util.h" +#include "processor.h" +#include "hyperv.h" +#include "vmx.h" + +static void guest_code(u64 expect_cet_ctls, u64 expect_sss) +{ + u64 ctls2 = rdmsr(MSR_IA32_VMX_PROCBASED_CTLS2); + u64 ept_cap = rdmsr(MSR_IA32_VMX_EPT_VPID_CAP); + u64 exit_ctls = rdmsr(MSR_IA32_VMX_TRUE_EXIT_CTLS); + u64 entry_ctls = rdmsr(MSR_IA32_VMX_TRUE_ENTRY_CTLS); + + /* Without MBEC, Hyper-V does not enable CET and nothing is required. */ + if (!((ctls2 >> 32) & SECONDARY_EXEC_MODE_BASED_EPT_EXEC)) + GUEST_DONE(); + + if (expect_cet_ctls) { + __GUEST_ASSERT((exit_ctls >> 32) & VM_EXIT_LOAD_CET_STATE, + "MBEC offered but the VM-exit control to load CET state is not, exit controls 0x%lx", + exit_ctls); + __GUEST_ASSERT((entry_ctls >> 32) & VM_ENTRY_LOAD_CET_STATE, + "MBEC offered but the VM-entry control to load CET state is not, entry controls 0x%lx", + entry_ctls); + } + + /* Supervisor shadow-stack control needs advanced VM-exit information. */ + if (expect_sss && (ept_cap & VMX_EPT_VPID_CAP_ADVANCED_VMEXIT_INFO)) + __GUEST_ASSERT(ept_cap & VMX_EPT_VPID_CAP_SSS, + "MBEC offered but supervisor shadow-stack control is not, EPT capabilities 0x%lx", + ept_cap); + + GUEST_DONE(); +} + +static bool read_host_msr(int fd, u32 msr, u64 *val) +{ + return pread(fd, val, sizeof(*val), msr) == sizeof(*val); +} + +/* Returns false if the host's VMX capability MSRs cannot be read. */ +static bool host_expectations(u64 *expect_cet_ctls, u64 *expect_sss) +{ + u64 ept_cap, exit_ctls, entry_ctls; + bool ok; + int fd; + + fd = open("/dev/cpu/0/msr", O_RDONLY); + if (fd < 0) + return false; + + ok = read_host_msr(fd, MSR_IA32_VMX_EPT_VPID_CAP, &ept_cap) && + read_host_msr(fd, MSR_IA32_VMX_TRUE_EXIT_CTLS, &exit_ctls) && + read_host_msr(fd, MSR_IA32_VMX_TRUE_ENTRY_CTLS, &entry_ctls); + close(fd); + if (!ok) + return false; + + *expect_cet_ctls = ((exit_ctls >> 32) & VM_EXIT_LOAD_CET_STATE) && + ((entry_ctls >> 32) & VM_ENTRY_LOAD_CET_STATE); + *expect_sss = (ept_cap & VMX_EPT_VPID_CAP_SSS) && + (ept_cap & VMX_EPT_VPID_CAP_ADVANCED_VMEXIT_INFO); + return true; +} + +static void run_test(bool evmcs, u64 expect_cet_ctls, u64 expect_sss) +{ + struct kvm_vcpu *vcpu; + struct kvm_vm *vm; + struct ucall uc; + + vm = vm_create_with_one_vcpu(&vcpu, guest_code); + if (evmcs) { + vcpu_set_hv_cpuid(vcpu); + vcpu_enable_evmcs(vcpu); + } + + vcpu_args_set(vcpu, 2, expect_cet_ctls, expect_sss); + vcpu_run(vcpu); + TEST_ASSERT_KVM_EXIT_REASON(vcpu, KVM_EXIT_IO); + + switch (get_ucall(vcpu, &uc)) { + case UCALL_ABORT: + REPORT_GUEST_ASSERT(uc); + break; + case UCALL_DONE: + break; + default: + TEST_FAIL("Unexpected ucall %lu", uc.cmd); + } + + kvm_vm_free(vm); +} + +int main(int argc, char *argv[]) +{ + const char *no_host_caps = "cannot read the host's VMX capabilities from /dev/cpu/0/msr"; + u64 expect_cet_ctls, expect_sss; + + TEST_REQUIRE(kvm_cpu_has(X86_FEATURE_VMX)); + TEST_REQUIRE(kvm_cpu_has_ept()); + TEST_REQUIRE(kvm_cpu_has(X86_FEATURE_SHSTK)); + TEST_REQUIRE(kvm_cpu_has_secondary_exec_control(SECONDARY_EXEC_MODE_BASED_EPT_EXEC)); + + ksft_print_header(); + ksft_set_plan(2); + + if (!host_expectations(&expect_cet_ctls, &expect_sss)) { + ksft_test_result_skip("without eVMCS: %s\n", no_host_caps); + ksft_test_result_skip("with eVMCS: %s\n", no_host_caps); + ksft_finished(); + } + + run_test(false, expect_cet_ctls, expect_sss); + ksft_test_result_pass("VMX capabilities for CET with MBEC, without eVMCS\n"); + + if (kvm_has_cap(KVM_CAP_HYPERV_ENLIGHTENED_VMCS)) { + run_test(true, expect_cet_ctls, expect_sss); + ksft_test_result_pass("VMX capabilities for CET with MBEC, with eVMCS\n"); + } else { + ksft_test_result_skip("with eVMCS: eVMCS not supported\n"); + } + + ksft_finished(); +} -- 2.53.0