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. 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 | 90 ++++++++++++++++++++++ 2 files changed, 91 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..cb9135df5d13 --- /dev/null +++ b/tools/testing/selftests/kvm/x86/vmx_nested_cet_caps_test.c @@ -0,0 +1,90 @@ +// 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 filters the + * control MSRs for eVMCS only when the guest reads them, so L1 reads them + * itself. + */ +#include "test_util.h" +#include "kvm_util.h" +#include "processor.h" +#include "hyperv.h" +#include "vmx.h" + +static void guest_code(void) +{ + 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(); + + __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 (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 void run_test(bool evmcs) +{ + struct kvm_vcpu *vcpu; + struct kvm_vm *vm; + struct ucall uc; + + pr_info("Checking the VMX capabilities L1 sees %s eVMCS\n", evmcs ? "with" : "without"); + + vm = vm_create_with_one_vcpu(&vcpu, guest_code); + if (evmcs) { + vcpu_set_hv_cpuid(vcpu); + vcpu_enable_evmcs(vcpu); + } + + 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[]) +{ + 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)); + + run_test(false); + + if (kvm_has_cap(KVM_CAP_HYPERV_ENLIGHTENED_VMCS)) + run_test(true); + else + pr_info("eVMCS not supported, skipping the eVMCS case\n"); + + return 0; +} -- 2.53.0