]> www.infradead.org Git - users/hch/block.git/commitdiff
KVM: SEV: publish supported VMSA features
authorPaolo Bonzini <pbonzini@redhat.com>
Thu, 4 Apr 2024 12:13:15 +0000 (08:13 -0400)
committerPaolo Bonzini <pbonzini@redhat.com>
Thu, 11 Apr 2024 17:08:22 +0000 (13:08 -0400)
Compute the set of features to be stored in the VMSA when KVM is
initialized; move it from there into kvm_sev_info when SEV is initialized,
and then into the initial VMSA.

The new variable can then be used to return the set of supported features
to userspace, via the KVM_GET_DEVICE_ATTR ioctl.

Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
Reviewed-by: Isaku Yamahata <isaku.yamahata@intel.com>
Message-ID: <20240404121327.3107131-6-pbonzini@redhat.com>
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
Documentation/virt/kvm/x86/amd-memory-encryption.rst
arch/x86/include/uapi/asm/kvm.h
arch/x86/kvm/svm/sev.c
arch/x86/kvm/svm/svm.c
arch/x86/kvm/svm/svm.h

index 84335d119ff136a0236594b43ef5fc21126d69e3..2ea648e4c97af1b6d644e21cb5c67fb0eda06276 100644 (file)
@@ -425,6 +425,18 @@ issued by the hypervisor to make the guest ready for execution.
 
 Returns: 0 on success, -negative on error
 
+Device attribute API
+====================
+
+Attributes of the SEV implementation can be retrieved through the
+``KVM_HAS_DEVICE_ATTR`` and ``KVM_GET_DEVICE_ATTR`` ioctls on the ``/dev/kvm``
+device node, using group ``KVM_X86_GRP_SEV``.
+
+Currently only one attribute is implemented:
+
+* ``KVM_X86_SEV_VMSA_FEATURES``: return the set of all bits that
+  are accepted in the ``vmsa_features`` of ``KVM_SEV_INIT2``.
+
 Firmware Management
 ===================
 
index ef11aa4cab42536cf1773bf5b787bcf715bd76e9..b7dc515f4c27a17c070f6bca5f8a5f13b5844489 100644 (file)
@@ -457,8 +457,13 @@ struct kvm_sync_regs {
 
 #define KVM_STATE_VMX_PREEMPTION_TIMER_DEADLINE        0x00000001
 
-/* attributes for system fd (group 0) */
-#define KVM_X86_XCOMP_GUEST_SUPP       0
+/* vendor-independent attributes for system fd (group 0) */
+#define KVM_X86_GRP_SYSTEM             0
+#  define KVM_X86_XCOMP_GUEST_SUPP     0
+
+/* vendor-specific groups and attributes for system fd */
+#define KVM_X86_GRP_SEV                        1
+#  define KVM_X86_SEV_VMSA_FEATURES    0
 
 struct kvm_vmx_nested_state_data {
        __u8 vmcs12[KVM_STATE_NESTED_VMX_VMCS_SIZE];
index 5d41f27a8af57329e90cfadbeed4bb180efa50ec..5055935dfd1d3a9252eedb21a464ae5546cf2630 100644 (file)
@@ -46,6 +46,7 @@ module_param_named(sev_es, sev_es_enabled, bool, 0444);
 /* enable/disable SEV-ES DebugSwap support */
 static bool sev_es_debug_swap_enabled = false;
 module_param_named(debug_swap, sev_es_debug_swap_enabled, bool, 0444);
+static u64 sev_supported_vmsa_features;
 
 static u8 sev_enc_bit;
 static DECLARE_RWSEM(sev_deactivate_lock);
@@ -603,8 +604,8 @@ static int sev_es_sync_vmsa(struct vcpu_svm *svm)
        save->xss  = svm->vcpu.arch.ia32_xss;
        save->dr6  = svm->vcpu.arch.dr6;
 
-       if (sev_es_debug_swap_enabled) {
-               save->sev_features |= SVM_SEV_FEAT_DEBUG_SWAP;
+       if (sev_supported_vmsa_features) {
+               save->sev_features = sev_supported_vmsa_features;
                pr_warn_once("Enabling DebugSwap with KVM_SEV_ES_INIT. "
                             "This will not work starting with Linux 6.10\n");
        }
@@ -1843,6 +1844,21 @@ out_fput:
        return ret;
 }
 
+int sev_dev_get_attr(u32 group, u64 attr, u64 *val)
+{
+       if (group != KVM_X86_GRP_SEV)
+               return -ENXIO;
+
+       switch (attr) {
+       case KVM_X86_SEV_VMSA_FEATURES:
+               *val = sev_supported_vmsa_features;
+               return 0;
+
+       default:
+               return -ENXIO;
+       }
+}
+
 int sev_mem_enc_ioctl(struct kvm *kvm, void __user *argp)
 {
        struct kvm_sev_cmd sev_cmd;
@@ -2275,6 +2291,10 @@ out:
        if (!sev_es_enabled || !cpu_feature_enabled(X86_FEATURE_DEBUG_SWAP) ||
            !cpu_feature_enabled(X86_FEATURE_NO_NESTED_DATA_BP))
                sev_es_debug_swap_enabled = false;
+
+       sev_supported_vmsa_features = 0;
+       if (sev_es_debug_swap_enabled)
+               sev_supported_vmsa_features |= SVM_SEV_FEAT_DEBUG_SWAP;
 }
 
 void sev_hardware_unsetup(void)
index e7f47a1f3eb1b6ebbf10e160ecd22f3e3aa6cd1e..450535d6757f74d6af173ae9702bee783da818b0 100644 (file)
@@ -5026,6 +5026,7 @@ static struct kvm_x86_ops svm_x86_ops __initdata = {
 #endif
 
 #ifdef CONFIG_KVM_AMD_SEV
+       .dev_get_attr = sev_dev_get_attr,
        .mem_enc_ioctl = sev_mem_enc_ioctl,
        .mem_enc_register_region = sev_mem_enc_register_region,
        .mem_enc_unregister_region = sev_mem_enc_unregister_region,
index ec8ca7d92cf160cdc9be5e866b34cb1358eea663..1c6601a9cbbfa8780fa6778facab15ed2afc8aea 100644 (file)
@@ -692,6 +692,7 @@ void __init sev_set_cpu_caps(void);
 void __init sev_hardware_setup(void);
 void sev_hardware_unsetup(void);
 int sev_cpu_init(struct svm_cpu_data *sd);
+int sev_dev_get_attr(u32 group, u64 attr, u64 *val);
 extern unsigned int max_sev_asid;
 #else
 static inline struct page *snp_safe_alloc_page(struct kvm_vcpu *vcpu) {
@@ -704,6 +705,7 @@ static inline void __init sev_set_cpu_caps(void) {}
 static inline void __init sev_hardware_setup(void) {}
 static inline void sev_hardware_unsetup(void) {}
 static inline int sev_cpu_init(struct svm_cpu_data *sd) { return 0; }
+static inline int sev_dev_get_attr(u32 group, u64 attr, u64 *val) { return -ENXIO; }
 #define max_sev_asid 0
 #endif