From nobody Mon Feb 9 10:27:32 2026 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id A6A2F130AC5 for ; Mon, 26 Feb 2024 19:03:53 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.129.124 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708974237; cv=none; b=di3KsmShmiYBUVQuGJZQnHGFFyX2sVJ+WO6zAbNC2pit5GBMKNN7OLn1J/0iuJ0OrWWK8EzdnZeCPzA4wEt5SEyneGVSe2cX3XpYN+BezD/r+X95QO0lUgshZyLAGUf6scf0bfnqads4uXp4znJnzsm/sDSGballnYck8bKa8y4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708974237; c=relaxed/simple; bh=u4vp1fQFXGsNmoUJGorwC2NA6a1G+j0i4NEF2HcCNMw=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version:Content-Type; b=HHKRz6MA2bepjp16tjtOUSNfRJST1yHSlUC7kcQnrZW6gxUSikS8vcuMnpoFXaCxFj2TIVOFVmjvapZ1Jd2ss536cFXTwQfmtR4sf+WsTOZ9T/J2X0jpAu9MfZCwBK2hHX6M2qlc4n2iZ29LoyUi4qPrn3dzzP3x52qMQFn2k2M= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=redhat.com; spf=pass smtp.mailfrom=redhat.com; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b=V1x7Ts4m; arc=none smtp.client-ip=170.10.129.124 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="V1x7Ts4m" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1708974232; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=umQkGU8rnhiSHX8hSfPIlFf/hoZK9Rh5A6sOzrKVDPU=; b=V1x7Ts4m+kuqKQ+KU5rhCX8nF/s8w+p33h6Gcq5q0LPXtHr9eif8p8ebuR+SU2WsCXuhLw t36LPUIN65A0mrZviUXIT2lw9Iy2xVhvtFmEz1at43a8zDr1X5s9XsSLzL0wzw1O/tYtWz mPO3OgBu34vD1q0I/UUMb5bcKCW2sf8= Received: from mimecast-mx02.redhat.com (mx-ext.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-75-qJ7u3lCoMSqU6s7iwjf6LQ-1; Mon, 26 Feb 2024 14:03:48 -0500 X-MC-Unique: qJ7u3lCoMSqU6s7iwjf6LQ-1 Received: from smtp.corp.redhat.com (int-mx02.intmail.prod.int.rdu2.redhat.com [10.11.54.2]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 59E1738212D4; Mon, 26 Feb 2024 19:03:48 +0000 (UTC) Received: from virtlab701.virt.lab.eng.bos.redhat.com (virtlab701.virt.lab.eng.bos.redhat.com [10.19.152.228]) by smtp.corp.redhat.com (Postfix) with ESMTP id 33BC340C1430; Mon, 26 Feb 2024 19:03:48 +0000 (UTC) From: Paolo Bonzini To: linux-kernel@vger.kernel.org, kvm@vger.kernel.org Cc: seanjc@google.com, michael.roth@amd.com, aik@amd.com Subject: [PATCH v3 13/15] KVM: SEV: define VM types for SEV and SEV-ES Date: Mon, 26 Feb 2024 14:03:42 -0500 Message-Id: <20240226190344.787149-14-pbonzini@redhat.com> In-Reply-To: <20240226190344.787149-1-pbonzini@redhat.com> References: <20240226190344.787149-1-pbonzini@redhat.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.4.1 on 10.11.54.2 Content-Type: text/plain; charset="utf-8" Signed-off-by: Paolo Bonzini --- Documentation/virt/kvm/api.rst | 2 ++ arch/x86/include/uapi/asm/kvm.h | 2 ++ arch/x86/kvm/svm/sev.c | 16 +++++++++++++--- arch/x86/kvm/svm/svm.c | 7 +++++++ arch/x86/kvm/svm/svm.h | 1 + arch/x86/kvm/x86.c | 2 ++ 6 files changed, 27 insertions(+), 3 deletions(-) diff --git a/Documentation/virt/kvm/api.rst b/Documentation/virt/kvm/api.rst index 0b5a33ee71ee..f0b76ff5030d 100644 --- a/Documentation/virt/kvm/api.rst +++ b/Documentation/virt/kvm/api.rst @@ -8819,6 +8819,8 @@ means the VM type with value @n is supported. Possib= le values of @n are:: =20 #define KVM_X86_DEFAULT_VM 0 #define KVM_X86_SW_PROTECTED_VM 1 + #define KVM_X86_SEV_VM 2 + #define KVM_X86_SEV_ES_VM 3 =20 Note, KVM_X86_SW_PROTECTED_VM is currently only for development and testin= g. Do not use KVM_X86_SW_PROTECTED_VM for "real" VMs, and especially not in diff --git a/arch/x86/include/uapi/asm/kvm.h b/arch/x86/include/uapi/asm/kv= m.h index d0c1b459f7e9..9d950b0b64c9 100644 --- a/arch/x86/include/uapi/asm/kvm.h +++ b/arch/x86/include/uapi/asm/kvm.h @@ -857,5 +857,7 @@ struct kvm_hyperv_eventfd { =20 #define KVM_X86_DEFAULT_VM 0 #define KVM_X86_SW_PROTECTED_VM 1 +#define KVM_X86_SEV_VM 2 +#define KVM_X86_SEV_ES_VM 3 =20 #endif /* _ASM_X86_KVM_H */ diff --git a/arch/x86/kvm/svm/sev.c b/arch/x86/kvm/svm/sev.c index 2549a539a686..1248ccf433e8 100644 --- a/arch/x86/kvm/svm/sev.c +++ b/arch/x86/kvm/svm/sev.c @@ -247,6 +247,9 @@ static int sev_guest_init(struct kvm *kvm, struct kvm_s= ev_cmd *argp) if (kvm->created_vcpus) return -EINVAL; =20 + if (kvm->arch.vm_type !=3D KVM_X86_DEFAULT_VM) + return -EINVAL; + if (unlikely(sev->active)) return -EINVAL; =20 @@ -264,6 +267,7 @@ static int sev_guest_init(struct kvm *kvm, struct kvm_s= ev_cmd *argp) =20 INIT_LIST_HEAD(&sev->regions_list); INIT_LIST_HEAD(&sev->mirror_vms); + sev->need_init =3D false; =20 kvm_set_apicv_inhibit(kvm, APICV_INHIBIT_REASON_SEV); =20 @@ -1799,7 +1803,8 @@ int sev_vm_move_enc_context_from(struct kvm *kvm, uns= igned int source_fd) if (ret) goto out_fput; =20 - if (sev_guest(kvm) || !sev_guest(source_kvm)) { + if (kvm->arch.vm_type !=3D source_kvm->arch.vm_type || + sev_guest(kvm) || !sev_guest(source_kvm)) { ret =3D -EINVAL; goto out_unlock; } @@ -2118,6 +2123,7 @@ int sev_vm_copy_enc_context_from(struct kvm *kvm, uns= igned int source_fd) mirror_sev->asid =3D source_sev->asid; mirror_sev->fd =3D source_sev->fd; mirror_sev->es_active =3D source_sev->es_active; + mirror_sev->need_init =3D false; mirror_sev->handle =3D source_sev->handle; INIT_LIST_HEAD(&mirror_sev->regions_list); INIT_LIST_HEAD(&mirror_sev->mirror_vms); @@ -2183,10 +2189,14 @@ void sev_vm_destroy(struct kvm *kvm) =20 void __init sev_set_cpu_caps(void) { - if (sev_enabled) + if (sev_enabled) { kvm_cpu_cap_set(X86_FEATURE_SEV); - if (sev_es_enabled) + kvm_caps.supported_vm_types |=3D BIT(KVM_X86_SEV_VM); + } + if (sev_es_enabled) { kvm_cpu_cap_set(X86_FEATURE_SEV_ES); + kvm_caps.supported_vm_types |=3D BIT(KVM_X86_SEV_ES_VM); + } } =20 void __init sev_hardware_setup(void) diff --git a/arch/x86/kvm/svm/svm.c b/arch/x86/kvm/svm/svm.c index 1cf9e5f1fd02..f4a750426b24 100644 --- a/arch/x86/kvm/svm/svm.c +++ b/arch/x86/kvm/svm/svm.c @@ -4089,6 +4089,9 @@ static void svm_cancel_injection(struct kvm_vcpu *vcp= u) =20 static int svm_vcpu_pre_run(struct kvm_vcpu *vcpu) { + if (to_kvm_sev_info(vcpu->kvm)->need_init) + return -EINVAL; + return 1; } =20 @@ -4890,6 +4893,10 @@ static void svm_vm_destroy(struct kvm *kvm) =20 static int svm_vm_init(struct kvm *kvm) { + if (kvm->arch.vm_type !=3D KVM_X86_DEFAULT_VM && + kvm->arch.vm_type !=3D KVM_X86_SW_PROTECTED_VM) + to_kvm_sev_info(kvm)->need_init =3D true; + if (!pause_filter_count || !pause_filter_thresh) kvm->arch.pause_in_guest =3D true; =20 diff --git a/arch/x86/kvm/svm/svm.h b/arch/x86/kvm/svm/svm.h index ebf2160bf0c6..7a921acc534f 100644 --- a/arch/x86/kvm/svm/svm.h +++ b/arch/x86/kvm/svm/svm.h @@ -79,6 +79,7 @@ enum { struct kvm_sev_info { bool active; /* SEV enabled guest */ bool es_active; /* SEV-ES enabled guest */ + bool need_init; /* waiting for SEV_INIT2 */ unsigned int asid; /* ASID used for this guest */ unsigned int handle; /* SEV firmware handle */ int fd; /* SEV device fd */ diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index 3b87e65904ae..b9dfe3179332 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -12576,6 +12576,8 @@ int kvm_arch_init_vm(struct kvm *kvm, unsigned long= type) kvm->arch.vm_type =3D type; kvm->arch.has_private_mem =3D (type =3D=3D KVM_X86_SW_PROTECTED_VM); + kvm->arch.has_protected_state =3D + (type =3D=3D KVM_X86_SEV_ES_VM); =20 ret =3D kvm_page_track_init(kvm); if (ret) --=20 2.39.1