From: Joerg Roedel Support setting a VMSA in guest physical memory during the SEV-SNP launch process. Only one VMSA can be provided which will then be used for the BSP. All of the APs will not have a VMSA allocated or assigned when this feature is used. This ensures stable launch measurements on SEV-SNP which are independent of the number of VCPUs the VM is launched with. Signed-off-by: Joerg Roedel --- arch/x86/include/uapi/asm/kvm.h | 1 + arch/x86/kvm/svm/sev.c | 44 ++++++++++++++++++++++++++++++++- arch/x86/kvm/x86.c | 1 + include/uapi/linux/kvm.h | 1 + 4 files changed, 46 insertions(+), 1 deletion(-) diff --git a/arch/x86/include/uapi/asm/kvm.h b/arch/x86/include/uapi/asm/kvm.h index 5f2b30d0405c..fc87a5ba295b 100644 --- a/arch/x86/include/uapi/asm/kvm.h +++ b/arch/x86/include/uapi/asm/kvm.h @@ -885,6 +885,7 @@ struct kvm_sev_snp_launch_start { /* Kept in sync with firmware values for simplicity. */ #define KVM_SEV_PAGE_TYPE_INVALID 0x0 #define KVM_SEV_SNP_PAGE_TYPE_NORMAL 0x1 +#define KVM_SEV_SNP_PAGE_TYPE_VMSA 0x2 #define KVM_SEV_SNP_PAGE_TYPE_ZERO 0x3 #define KVM_SEV_SNP_PAGE_TYPE_UNMEASURED 0x4 #define KVM_SEV_SNP_PAGE_TYPE_SECRETS 0x5 diff --git a/arch/x86/kvm/svm/sev.c b/arch/x86/kvm/svm/sev.c index 88db83b3ff8e..90399d5d0358 100644 --- a/arch/x86/kvm/svm/sev.c +++ b/arch/x86/kvm/svm/sev.c @@ -2520,6 +2520,20 @@ static int snp_launch_start(struct kvm *kvm, struct kvm_sev_cmd *argp) return rc; } +static bool snp_check_launch_vmsa(struct kvm_sev_info *sev, + struct sev_es_save_area *vmsa) +{ + /* VMSA sev_features must match VMs vmsa_features */ + if (vmsa->sev_features != sev->vmsa_features) + return false; + + /* Must always boot from VMPL0 */ + if (vmsa->vmpl != 0) + return false; + + return true; +} + struct sev_gmem_populate_args { __u8 type; int sev_fd; @@ -2532,7 +2546,9 @@ static int sev_gmem_post_populate(struct kvm *kvm, gfn_t gfn, kvm_pfn_t pfn, struct sev_gmem_populate_args *sev_populate_args = opaque; struct sev_data_snp_launch_update fw_args = {0}; struct kvm_sev_info *sev = to_kvm_sev_info(kvm); + gpa_t gpa = gfn << PAGE_SHIFT; bool assigned = false; + u64 sev_features = 0; int level; int ret; @@ -2550,14 +2566,27 @@ static int sev_gmem_post_populate(struct kvm *kvm, gfn_t gfn, kvm_pfn_t pfn, if (src_page) { void *src_vaddr = kmap_local_page(src_page); void *dst_vaddr = kmap_local_pfn(pfn); + struct sev_es_save_area *vmsa = dst_vaddr; + bool accept_page = true; memcpy(dst_vaddr, src_vaddr, PAGE_SIZE); + if (sev_populate_args->type == KVM_SEV_SNP_PAGE_TYPE_VMSA) { + accept_page = snp_check_launch_vmsa(sev, vmsa); + if (accept_page) + sev_features = vmsa->sev_features; + } + kunmap_local(src_vaddr); kunmap_local(dst_vaddr); + + if (!accept_page) { + ret = -EINVAL; + goto out; + } } - ret = rmp_make_private(pfn, gfn << PAGE_SHIFT, PG_LEVEL_4K, + ret = rmp_make_private(pfn, gpa, PG_LEVEL_4K, sev_get_asid(kvm), true); if (ret) goto out; @@ -2593,6 +2622,9 @@ static int sev_gmem_post_populate(struct kvm *kvm, gfn_t gfn, kvm_pfn_t pfn, kunmap_local(dst_vaddr); } + if (ret == 0 && sev_populate_args->type == KVM_SEV_SNP_PAGE_TYPE_VMSA) + sev->initial_vmsa_gpa = gpa; + out: if (ret) pr_debug("%s: error updating GFN %llx, return code %d (fw_error %d)\n", @@ -2620,12 +2652,22 @@ static int snp_launch_update(struct kvm *kvm, struct kvm_sev_cmd *argp) if (!params.len || !PAGE_ALIGNED(params.len) || params.flags || (params.type != KVM_SEV_SNP_PAGE_TYPE_NORMAL && + params.type != KVM_SEV_SNP_PAGE_TYPE_VMSA && params.type != KVM_SEV_SNP_PAGE_TYPE_ZERO && params.type != KVM_SEV_SNP_PAGE_TYPE_UNMEASURED && params.type != KVM_SEV_SNP_PAGE_TYPE_SECRETS && params.type != KVM_SEV_SNP_PAGE_TYPE_CPUID)) return -EINVAL; + if (params.type == KVM_SEV_SNP_PAGE_TYPE_VMSA) { + /* VMSA page are allowed only once */ + if (sev->initial_vmsa_gpa != INVALID_PAGE) + return -EBUSY; + /* Can only deploy a single page as VMSA */ + if (params.len != PAGE_SIZE) + return -EINVAL; + } + src = params.type == KVM_SEV_SNP_PAGE_TYPE_ZERO ? NULL : u64_to_user_ptr(params.uaddr); if (!PAGE_ALIGNED(src)) diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index 0550359ed798..dc9abe62476e 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -4870,6 +4870,7 @@ int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext) case KVM_CAP_MEMORY_FAULT_INFO: case KVM_CAP_X86_GUEST_MODE: case KVM_CAP_ONE_REG: + case KVM_CAP_SNP_DIRECT_VMSA: r = 1; break; case KVM_CAP_PRE_FAULT_MEMORY: diff --git a/include/uapi/linux/kvm.h b/include/uapi/linux/kvm.h index 6c8afa2047bf..bf034435f98c 100644 --- a/include/uapi/linux/kvm.h +++ b/include/uapi/linux/kvm.h @@ -996,6 +996,7 @@ struct kvm_enable_cap { #define KVM_CAP_S390_USER_OPEREXEC 246 #define KVM_CAP_S390_KEYOP 247 #define KVM_CAP_S390_VSIE_ESAMODE 248 +#define KVM_CAP_SNP_DIRECT_VMSA 249 struct kvm_irq_routing_irqchip { __u32 irqchip; -- 2.53.0