From nobody Mon Feb 9 12:25:35 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 17B225F85B for ; Mon, 18 Mar 2024 23:33:57 +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=1710804841; cv=none; b=m8Vb6pEsh89tW7q47X3Wp7D4AG2FoIo96wej6/vUmAfkKf32d+EetwkKDHjJjO+Hs0t+etIk6A4eBiO2KcBPPHM36f9Hc6rHBtjUWXnskVV8X455qHzLU37YVquxQxhhGZbIOOaHfz13+sIdg5k+oo1+jfBh9AAWlzp8NJZF77s= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1710804841; c=relaxed/simple; bh=cjIU+B6BLsuurs7zM20656DcF0/I2SGoWy906srd+qs=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=AmKPhzL8rfIv71Xrr1F77mScFndtJB1hcOtTTX1KRRBOnSgz7h+xHXSAQJnQ/ZKEeTN4x5//UV4mBURM1TkCkdgk7SZhQzKV5UsLTQ/cfMQSVFYRkMTUXiGwBF9qLgGcvYHy70wIyKbx/+xVjhLqlwxy9pLoPt9UJZmzf2eOrW8= 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=QqyzF95M; 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="QqyzF95M" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1710804837; 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=3qB+9WOwj6VlTsZBkG/PbOw2H1v6jsG7recEA+AUYPU=; b=QqyzF95MNTa3oy6tTxkHEmwpa1rUJJx1+RHgvhfVSyeJCvY7QpHZpBs1tZdjW+ZkXPvQvb hujJ3p9oSz47ATGlIaDjsX04JickRzDMnOq/9Z9lzBsXgUYVKIQmQJblgRg6wbFnmWcGs6 zofVM9E7zHJh8AV3U97YJ3+XUnK/bF4= Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-495-4ZzVU31FNsKti-upSKoqsg-1; Mon, 18 Mar 2024 19:33:55 -0400 X-MC-Unique: 4ZzVU31FNsKti-upSKoqsg-1 Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.rdu2.redhat.com [10.11.54.3]) (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 080AC85A58B; Mon, 18 Mar 2024 23:33:55 +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 D57E71121312; Mon, 18 Mar 2024 23:33:54 +0000 (UTC) From: Paolo Bonzini To: linux-kernel@vger.kernel.org, kvm@vger.kernel.org Cc: michael.roth@amd.com, isaku.yamahata@intel.com, seanjc@google.com Subject: [PATCH v4 06/15] KVM: SEV: store VMSA features in kvm_sev_info Date: Mon, 18 Mar 2024 19:33:43 -0400 Message-ID: <20240318233352.2728327-7-pbonzini@redhat.com> In-Reply-To: <20240318233352.2728327-1-pbonzini@redhat.com> References: <20240318233352.2728327-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.3 Content-Type: text/plain; charset="utf-8" Right now, the set of features that are stored in the VMSA upon initialization is fixed and depends on the module parameters for kvm-amd.ko. However, the hypervisor cannot really change it at will because the feature word has to match between the hypervisor and whatever computes a measurement of the VMSA for attestation purposes. Add a field to kvm_sev_info that holds the set of features to be stored in the VMSA; and query it instead of referring to the module parameters. Because KVM_SEV_INIT and KVM_SEV_ES_INIT accept no parameters, this does not yet introduce any functional change, but it paves the way for an API that allows customization of the features per-VM. Signed-off-by: Paolo Bonzini Message-Id: <20240209183743.22030-6-pbonzini@redhat.com> Reviewed-by: Michael Roth Signed-off-by: Paolo Bonzini --- arch/x86/kvm/svm/sev.c | 29 +++++++++++++++++++++-------- arch/x86/kvm/svm/svm.c | 2 +- arch/x86/kvm/svm/svm.h | 3 ++- 3 files changed, 24 insertions(+), 10 deletions(-) diff --git a/arch/x86/kvm/svm/sev.c b/arch/x86/kvm/svm/sev.c index 22c35a39c25f..a8300646a280 100644 --- a/arch/x86/kvm/svm/sev.c +++ b/arch/x86/kvm/svm/sev.c @@ -96,6 +96,14 @@ static inline bool is_mirroring_enc_context(struct kvm *= kvm) return !!to_kvm_svm(kvm)->sev_info.enc_context_owner; } =20 +static bool sev_vcpu_has_debug_swap(struct vcpu_svm *svm) +{ + struct kvm_vcpu *vcpu =3D &svm->vcpu; + struct kvm_sev_info *sev =3D &to_kvm_svm(vcpu->kvm)->sev_info; + + return sev->vmsa_features & SVM_SEV_FEAT_DEBUG_SWAP; +} + /* Must be called with the sev_bitmap_lock held */ static bool __sev_recycle_asids(unsigned int min_asid, unsigned int max_as= id) { @@ -245,6 +253,11 @@ static int sev_guest_init(struct kvm *kvm, struct kvm_= sev_cmd *argp) =20 sev->active =3D true; sev->es_active =3D argp->id =3D=3D KVM_SEV_ES_INIT; + sev->vmsa_features =3D sev_supported_vmsa_features; + if (sev_supported_vmsa_features) + pr_warn_once("Enabling DebugSwap with KVM_SEV_ES_INIT. " + "This will not work starting with Linux 6.10\n"); + ret =3D sev_asid_new(sev); if (ret) goto e_no_asid; @@ -266,6 +279,7 @@ static int sev_guest_init(struct kvm *kvm, struct kvm_s= ev_cmd *argp) sev_asid_free(sev); sev->asid =3D 0; e_no_asid: + sev->vmsa_features =3D 0; sev->es_active =3D false; sev->active =3D false; return ret; @@ -560,6 +574,8 @@ static int sev_launch_update_data(struct kvm *kvm, stru= ct kvm_sev_cmd *argp) =20 static int sev_es_sync_vmsa(struct vcpu_svm *svm) { + struct kvm_vcpu *vcpu =3D &svm->vcpu; + struct kvm_sev_info *sev =3D &to_kvm_svm(vcpu->kvm)->sev_info; struct sev_es_save_area *save =3D svm->sev_es.vmsa; =20 /* Check some debug related fields before encrypting the VMSA */ @@ -601,11 +617,7 @@ static int sev_es_sync_vmsa(struct vcpu_svm *svm) save->xss =3D svm->vcpu.arch.ia32_xss; save->dr6 =3D svm->vcpu.arch.dr6; =20 - if (sev_supported_vmsa_features) { - save->sev_features =3D sev_supported_vmsa_features; - pr_warn_once("Enabling DebugSwap with KVM_SEV_ES_INIT. " - "This will not work starting with Linux 6.10\n"); - } + save->sev_features =3D sev->vmsa_features; =20 pr_debug("Virtual Machine Save Area (VMSA):\n"); print_hex_dump_debug("", DUMP_PREFIX_NONE, 16, 1, save, sizeof(*save), fa= lse); @@ -1685,6 +1697,7 @@ static void sev_migrate_from(struct kvm *dst_kvm, str= uct kvm *src_kvm) dst->pages_locked =3D src->pages_locked; dst->enc_context_owner =3D src->enc_context_owner; dst->es_active =3D src->es_active; + dst->vmsa_features =3D src->vmsa_features; =20 src->asid =3D 0; src->active =3D false; @@ -3057,7 +3070,7 @@ static void sev_es_init_vmcb(struct vcpu_svm *svm) svm_set_intercept(svm, TRAP_CR8_WRITE); =20 vmcb->control.intercepts[INTERCEPT_DR] =3D 0; - if (!sev_es_debug_swap_enabled) { + if (!sev_vcpu_has_debug_swap(svm)) { vmcb_set_intercept(&vmcb->control, INTERCEPT_DR7_READ); vmcb_set_intercept(&vmcb->control, INTERCEPT_DR7_WRITE); recalc_intercepts(svm); @@ -3112,7 +3125,7 @@ void sev_es_vcpu_reset(struct vcpu_svm *svm) sev_enc_bit)); } =20 -void sev_es_prepare_switch_to_guest(struct sev_es_save_area *hostsa) +void sev_es_prepare_switch_to_guest(struct vcpu_svm *svm, struct sev_es_sa= ve_area *hostsa) { /* * All host state for SEV-ES guests is categorized into three swap types @@ -3140,7 +3153,7 @@ void sev_es_prepare_switch_to_guest(struct sev_es_sav= e_area *hostsa) * the CPU (Type-B). If DebugSwap is disabled/unsupported, the CPU both * saves and loads debug registers (Type-A). */ - if (sev_es_debug_swap_enabled) { + if (sev_vcpu_has_debug_swap(svm)) { hostsa->dr0 =3D native_get_debugreg(0); hostsa->dr1 =3D native_get_debugreg(1); hostsa->dr2 =3D native_get_debugreg(2); diff --git a/arch/x86/kvm/svm/svm.c b/arch/x86/kvm/svm/svm.c index 450535d6757f..c22e87ebf0de 100644 --- a/arch/x86/kvm/svm/svm.c +++ b/arch/x86/kvm/svm/svm.c @@ -1523,7 +1523,7 @@ static void svm_prepare_switch_to_guest(struct kvm_vc= pu *vcpu) struct sev_es_save_area *hostsa; hostsa =3D (struct sev_es_save_area *)(page_address(sd->save_area) + 0x4= 00); =20 - sev_es_prepare_switch_to_guest(hostsa); + sev_es_prepare_switch_to_guest(svm, hostsa); } =20 if (tsc_scaling) diff --git a/arch/x86/kvm/svm/svm.h b/arch/x86/kvm/svm/svm.h index 864fac367424..b7707514d042 100644 --- a/arch/x86/kvm/svm/svm.h +++ b/arch/x86/kvm/svm/svm.h @@ -85,6 +85,7 @@ struct kvm_sev_info { unsigned long pages_locked; /* Number of pages locked */ struct list_head regions_list; /* List of registered regions */ u64 ap_jump_table; /* SEV-ES AP Jump Table address */ + u64 vmsa_features; struct kvm *enc_context_owner; /* Owner of copied encryption context */ struct list_head mirror_vms; /* List of VMs mirroring */ struct list_head mirror_entry; /* Use as a list entry of mirrors */ @@ -684,7 +685,7 @@ int sev_handle_vmgexit(struct kvm_vcpu *vcpu); int sev_es_string_io(struct vcpu_svm *svm, int size, unsigned int port, in= t in); void sev_es_vcpu_reset(struct vcpu_svm *svm); void sev_vcpu_deliver_sipi_vector(struct kvm_vcpu *vcpu, u8 vector); -void sev_es_prepare_switch_to_guest(struct sev_es_save_area *hostsa); +void sev_es_prepare_switch_to_guest(struct vcpu_svm *svm, struct sev_es_sa= ve_area *hostsa); void sev_es_unmap_ghcb(struct vcpu_svm *svm); =20 /* These symbols are used in common code and are stubbed below. */ --=20 2.43.0