Skip to content

Commit 225b7c1

Browse files
vittyvksean-jc
authored andcommitted
KVM: selftests: Fix vmxon_pa == vmcs12_pa == -1ull nVMX testcase for !eVMCS
The "vmxon_pa == vmcs12_pa == -1ull" test happens to work by accident: as Enlightened VMCS is always supported, set_default_vmx_state() adds 'KVM_STATE_NESTED_EVMCS' to 'flags' and the following branch of vmx_set_nested_state() is executed: if ((kvm_state->flags & KVM_STATE_NESTED_EVMCS) && (!guest_can_use(vcpu, X86_FEATURE_VMX) || !vmx->nested.enlightened_vmcs_enabled)) return -EINVAL; as 'enlightened_vmcs_enabled' is false. In fact, "vmxon_pa == vmcs12_pa == -1ull" is a valid state when not tainted by wrong flags so the test should aim for this branch: if (kvm_state->hdr.vmx.vmxon_pa == INVALID_GPA) return 0; Test all this properly: - Without KVM_STATE_NESTED_EVMCS in the flags, the expected return value is '0'. - With KVM_STATE_NESTED_EVMCS flag (when supported) set, the expected return value is '-EINVAL' prior to enabling eVMCS and '0' after. Reviewed-by: Maxim Levitsky <mlevitsk@redhat.com> Tested-by: Jeremi Piotrowski <jpiotrowski@linux.microsoft.com> Signed-off-by: Vitaly Kuznetsov <vkuznets@redhat.com> Link: https://lore.kernel.org/r/20231205103630.1391318-11-vkuznets@redhat.com Signed-off-by: Sean Christopherson <seanjc@google.com>
1 parent 6dac119 commit 225b7c1

1 file changed

Lines changed: 10 additions & 6 deletions

File tree

tools/testing/selftests/kvm/x86_64/vmx_set_nested_state_test.c

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -125,21 +125,25 @@ void test_vmx_nested_state(struct kvm_vcpu *vcpu)
125125

126126
/*
127127
* Setting vmxon_pa == -1ull and vmcs_pa == -1ull exits early without
128-
* setting the nested state but flags other than eVMCS must be clear.
129-
* The eVMCS flag can be set if the enlightened VMCS capability has
130-
* been enabled.
128+
* setting the nested state. When the eVMCS flag is not set, the
129+
* expected return value is '0'.
131130
*/
132131
set_default_vmx_state(state, state_sz);
132+
state->flags = 0;
133133
state->hdr.vmx.vmxon_pa = -1ull;
134134
state->hdr.vmx.vmcs12_pa = -1ull;
135-
test_nested_state_expect_einval(vcpu, state);
135+
test_nested_state(vcpu, state);
136136

137-
state->flags &= KVM_STATE_NESTED_EVMCS;
137+
/*
138+
* When eVMCS is supported, the eVMCS flag can only be set if the
139+
* enlightened VMCS capability has been enabled.
140+
*/
138141
if (have_evmcs) {
142+
state->flags = KVM_STATE_NESTED_EVMCS;
139143
test_nested_state_expect_einval(vcpu, state);
140144
vcpu_enable_evmcs(vcpu);
145+
test_nested_state(vcpu, state);
141146
}
142-
test_nested_state(vcpu, state);
143147

144148
/* It is invalid to have vmxon_pa == -1ull and SMM flags non-zero. */
145149
state->hdr.vmx.smm.flags = 1;

0 commit comments

Comments
 (0)