KVM: selftests: Add module param API to check if nested virtualization is enabled

Add an API to check if the nested virtualization module param is enabled,
e.g. so that tests can verify KVM's own enumeration of the corresponding
CPU feature.  Provide a big warning above the API to make it as obvious as
possible clear that tests should use "kvm_cpu_has(X86_FEATURE_VMX) ||
kvm_cpu_has(X86_FEATURE_SVM)" when checking for nested virtualization.  It
doesn't really matter which method tests use, as KVM is supposed to keep
"nested" synchronized with VMX/SVM enumeration, but make the comment super
scary to try and prevent KVM selftests from having two different ways of
doing the same thing.

Note, the bool vs. int mess is intentional, because KVM's params are
terrible.  kvm_amd.ko provides an "int", while kvm_intel.ko provides a
"bool".

Link: https://patch.msgid.link/20260930212114.3500045-5-seanjc@google.com
Signed-off-by: Sean Christopherson <seanjc@google.com>
This commit is contained in:
Sean Christopherson 2026-09-30 14:21:13 -07:00
parent cc2b9cc15a
commit 58f4dc83da

View File

@ -1472,6 +1472,19 @@ static inline bool kvm_is_lbrv_enabled(void)
return !!get_kvm_amd_param_integer("lbrv");
}
/*
* Do NOT use this to check for nVMX or nSVM support. Querying kvm_cpu_has()
* for either of X86_FEATURE_{VMX,SVM} is the idiomatic way to check for nested
* virtualization support. Use this *only* to validate KVM's own enumeration.
*/
static inline bool kvm_is_nested_virtualization_enabled(void)
{
if (host_cpu_is_intel)
return get_kvm_intel_param_bool("nested");
return get_kvm_amd_param_integer("nested");
}
u64 *vm_get_pte(struct kvm_vm *vm, gva_t gva);
u64 kvm_hypercall(u64 nr, u64 a0, u64 a1, u64 a2, u64 a3);