KVM: x86: Add supported_vm_types to kvm_caps
authorPaolo Bonzini <pbonzini@redhat.com>
Thu, 4 Apr 2024 12:13:18 +0000 (08:13 -0400)
committerPaolo Bonzini <pbonzini@redhat.com>
Thu, 11 Apr 2024 17:08:24 +0000 (13:08 -0400)
This simplifies the implementation of KVM_CHECK_EXTENSION(KVM_CAP_VM_TYPES),
and also allows the vendor module to specify which VM types are supported.

Suggested-by: Sean Christopherson <seanjc@google.com>
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
Message-ID: <20240404121327.3107131-9-pbonzini@redhat.com>
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
arch/x86/kvm/x86.c
arch/x86/kvm/x86.h

index d4a8d896798f5258e48f7dbdf0e48d1eaf3ca4e2..d584f5739402aa281ca4a9ab471b3d9039628065 100644 (file)
@@ -94,6 +94,7 @@
 
 struct kvm_caps kvm_caps __read_mostly = {
        .supported_mce_cap = MCG_CTL_P | MCG_SER_P,
+       .supported_vm_types = BIT(KVM_X86_DEFAULT_VM),
 };
 EXPORT_SYMBOL_GPL(kvm_caps);
 
@@ -4629,9 +4630,7 @@ static int kvm_ioctl_get_supported_hv_cpuid(struct kvm_vcpu *vcpu,
 
 static bool kvm_is_vm_type_supported(unsigned long type)
 {
-       return type == KVM_X86_DEFAULT_VM ||
-              (type == KVM_X86_SW_PROTECTED_VM &&
-               IS_ENABLED(CONFIG_KVM_SW_PROTECTED_VM) && tdp_mmu_enabled);
+       return type < 32 && (kvm_caps.supported_vm_types & BIT(type));
 }
 
 int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext)
@@ -4832,9 +4831,7 @@ int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext)
                r = kvm_caps.has_notify_vmexit;
                break;
        case KVM_CAP_VM_TYPES:
-               r = BIT(KVM_X86_DEFAULT_VM);
-               if (kvm_is_vm_type_supported(KVM_X86_SW_PROTECTED_VM))
-                       r |= BIT(KVM_X86_SW_PROTECTED_VM);
+               r = kvm_caps.supported_vm_types;
                break;
        default:
                break;
@@ -9824,6 +9821,9 @@ int kvm_x86_vendor_init(struct kvm_x86_init_ops *ops)
 
        kvm_register_perf_callbacks(ops->handle_intel_pt_intr);
 
+       if (IS_ENABLED(CONFIG_KVM_SW_PROTECTED_VM) && tdp_mmu_enabled)
+               kvm_caps.supported_vm_types |= BIT(KVM_X86_SW_PROTECTED_VM);
+
        if (!kvm_cpu_cap_has(X86_FEATURE_XSAVES))
                kvm_caps.supported_xss = 0;
 
index a8b71803777baa13060b8051cc2b9aa3ce7287fd..d80a4c6b5a3882a26a87ef8249c56a7ece4d179b 100644 (file)
@@ -24,6 +24,8 @@ struct kvm_caps {
        bool has_bus_lock_exit;
        /* notify VM exit supported? */
        bool has_notify_vmexit;
+       /* bit mask of VM types */
+       u32 supported_vm_types;
 
        u64 supported_mce_cap;
        u64 supported_xcr0;