From nobody Sun Feb 8 14:56:49 2026 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.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 442AC82869 for ; Fri, 23 Feb 2024 10:40:31 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.133.124 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708684833; cv=none; b=dMcvvPqT40AR8Yh7QDQEE383kOKoKsvDL6bjaBuIJuo0gERemulG719f1fB7iLSlteYeiGTryPXCIUpJtEoWeh5NhLobfwv894dVG9lekYri887RXag/TVwa2Ya+C9Aq9Iv8sSaNexazK6+fYMxk8ySK67HKvDGpghY2KZmpDzs= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708684833; c=relaxed/simple; bh=DX9YcffDBaz6DU6P4zQRuDxKywNhABwdJPiW+EG6rz4=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version:Content-Type; b=o9eC6snf86Q+fF3f91rYKqAzy+TLCgmFCvP9r7SG8/NzWCeLQydu7ZsUcta1yMhGu385JpUkdgaqMQA+4UUjjDv3+8Fr8CNVIc+tzvjd1CkZ1p6hU0/0LyhMwJpVBx+TZgxX/Ak0JLMNQQC1pSIs98nBMhpDBqB8JO6epVfhqd8= 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=i54Cjzsa; arc=none smtp.client-ip=170.10.133.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="i54Cjzsa" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1708684831; 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=LVP8O1T68Ud+akMCY43T5a2gER3eDvB9zlOBSBJIRR0=; b=i54Cjzsa4ziw4JfKmXaMcQEbi8tXLSPLJPAbGfdfPBkjieF6/Tz19mMJlgRyaeleRIhroj D3/hZEPdxAY8zSJ+o6W9d6Cg39iYCwmVmDBNJyOZnEhzrdpynm1Z9D9B/nBppO/y2vUpH2 Ua8YL6hS8BdDEfgMsnFi+051LXG1wIA= 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-424-bpr3kyCZO3quMyRLFlPPbg-1; Fri, 23 Feb 2024 05:40:26 -0500 X-MC-Unique: bpr3kyCZO3quMyRLFlPPbg-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 9BA6F88F4C6; Fri, 23 Feb 2024 10:40:10 +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 758C9112132A; Fri, 23 Feb 2024 10:40:10 +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 v2 01/11] KVM: SEV: fix compat ABI for KVM_MEMORY_ENCRYPT_OP Date: Fri, 23 Feb 2024 05:39:59 -0500 Message-Id: <20240223104009.632194-2-pbonzini@redhat.com> In-Reply-To: <20240223104009.632194-1-pbonzini@redhat.com> References: <20240223104009.632194-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" The data structs for KVM_MEMORY_ENCRYPT_OP have different sizes for 32- and= 64-bit kernels, but they do not make any attempt to convert from one ABI to the ot= her. Fix this by adding the appropriate padding. No functional change intended for 64-bit userspace. Signed-off-by: Paolo Bonzini Message-Id: <20240209183743.22030-2-pbonzini@redhat.com> Reviewed-by: Michael Roth Signed-off-by: Paolo Bonzini Reviewed-by: Sean Christopherson --- arch/x86/include/uapi/asm/kvm.h | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/arch/x86/include/uapi/asm/kvm.h b/arch/x86/include/uapi/asm/kv= m.h index 0ad6bda1fc39..b305daff056e 100644 --- a/arch/x86/include/uapi/asm/kvm.h +++ b/arch/x86/include/uapi/asm/kvm.h @@ -687,6 +687,7 @@ enum sev_cmd_id { =20 struct kvm_sev_cmd { __u32 id; + __u32 pad0; __u64 data; __u32 error; __u32 sev_fd; @@ -697,28 +698,35 @@ struct kvm_sev_launch_start { __u32 policy; __u64 dh_uaddr; __u32 dh_len; + __u32 pad0; __u64 session_uaddr; __u32 session_len; + __u32 pad1; }; =20 struct kvm_sev_launch_update_data { __u64 uaddr; __u32 len; + __u32 pad0; }; =20 =20 struct kvm_sev_launch_secret { __u64 hdr_uaddr; __u32 hdr_len; + __u32 pad0; __u64 guest_uaddr; __u32 guest_len; + __u32 pad1; __u64 trans_uaddr; __u32 trans_len; + __u32 pad2; }; =20 struct kvm_sev_launch_measure { __u64 uaddr; __u32 len; + __u32 pad0; }; =20 struct kvm_sev_guest_status { @@ -731,33 +739,43 @@ struct kvm_sev_dbg { __u64 src_uaddr; __u64 dst_uaddr; __u32 len; + __u32 pad0; }; =20 struct kvm_sev_attestation_report { __u8 mnonce[16]; __u64 uaddr; __u32 len; + __u32 pad0; }; =20 struct kvm_sev_send_start { __u32 policy; + __u32 pad0; __u64 pdh_cert_uaddr; __u32 pdh_cert_len; + __u32 pad1; __u64 plat_certs_uaddr; __u32 plat_certs_len; + __u32 pad2; __u64 amd_certs_uaddr; __u32 amd_certs_len; + __u32 pad3; __u64 session_uaddr; __u32 session_len; + __u32 pad4; }; =20 struct kvm_sev_send_update_data { __u64 hdr_uaddr; __u32 hdr_len; + __u32 pad0; __u64 guest_uaddr; __u32 guest_len; + __u32 pad1; __u64 trans_uaddr; __u32 trans_len; + __u32 pad2; }; =20 struct kvm_sev_receive_start { @@ -765,17 +783,22 @@ struct kvm_sev_receive_start { __u32 policy; __u64 pdh_uaddr; __u32 pdh_len; + __u32 pad0; __u64 session_uaddr; __u32 session_len; + __u32 pad1; }; =20 struct kvm_sev_receive_update_data { __u64 hdr_uaddr; __u32 hdr_len; + __u32 pad0; __u64 guest_uaddr; __u32 guest_len; + __u32 pad1; __u64 trans_uaddr; __u32 trans_len; + __u32 pad2; }; =20 #define KVM_X2APIC_API_USE_32BIT_IDS (1ULL << 0) --=20 2.39.1 From nobody Sun Feb 8 14:56:49 2026 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.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 8EC7467A01 for ; Fri, 23 Feb 2024 10:41:04 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.133.124 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708684866; cv=none; b=pOSn/AtjJl9C7zWZrin598FTtuGz/Ruin23cAyb7YTIuBngdHeaI1Mr5mXXp+YCgtNysv6269bMg4x2mtYD1Rssp59sotRva48DRMexIUguUW7CPl1mjqZyy+jZ4YsMNcLpGk7na1Lmi0Q13iGvi7iZuaDooxNSet368vrIQ8VM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708684866; c=relaxed/simple; bh=G5tKkyjprqLewL3O1LDTvZquCuMpAlrU/ub17lN3RL0=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version:Content-Type; b=BWvIExB/2eO4u1hJSt4xkmDPi159eF2aSLFGp3ZU0C2Ig6FK8RHlR9AezaEbYIsybx/s9/nYAd34H102YawiRsuioowfGHr1NEnYtxMDtEAnHcl8IXqf+2croLmUjxU154sFvN963ResLoHkm98xOg0ajFKQlAMH3AikLhvJhZY= 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=GxFC1CYp; arc=none smtp.client-ip=170.10.133.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="GxFC1CYp" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1708684863; 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=LiCqbXFLA/UyGnlYUq9cP+Y4tIkdjEj7HNX9/TX+2pE=; b=GxFC1CYpyvXbSpcz2LksCXlH9ieG6yLz/8cPp5476qIRsp0FpBCHt/GgyuESt6uSW1Ah0T vs2lemsYHGrr+kNNVfMJ4seM8XDEkr5ucvfM3BTLr7YT02Lkn7I223OiKkiMOqAABH8aE6 ag9eWPLDfZ3r3/T2c5tHlFzG8G1kClU= Received: from mimecast-mx02.redhat.com (66.187.233.88 [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-607-2Tu4Y4_uNRWlVLT0SWqalQ-1; Fri, 23 Feb 2024 05:40:11 -0500 X-MC-Unique: 2Tu4Y4_uNRWlVLT0SWqalQ-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 CA4F6185A784; Fri, 23 Feb 2024 10:40:10 +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 A3C27112132A; Fri, 23 Feb 2024 10:40:10 +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 v2 02/11] KVM: introduce new vendor op for KVM_GET_DEVICE_ATTR Date: Fri, 23 Feb 2024 05:40:00 -0500 Message-Id: <20240223104009.632194-3-pbonzini@redhat.com> In-Reply-To: <20240223104009.632194-1-pbonzini@redhat.com> References: <20240223104009.632194-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" Allow vendor modules to provide their own attributes on /dev/kvm. To avoid proliferation of vendor ops, implement KVM_HAS_DEVICE_ATTR and KVM_GET_DEVICE_ATTR in terms of the same function. You're not supposed to use KVM_GET_DEVICE_ATTR to do complicated computations, especially on /dev/kvm. Signed-off-by: Paolo Bonzini Message-Id: <20240209183743.22030-3-pbonzini@redhat.com> Reviewed-by: Michael Roth Signed-off-by: Paolo Bonzini --- arch/x86/include/asm/kvm-x86-ops.h | 1 + arch/x86/include/asm/kvm_host.h | 1 + arch/x86/kvm/x86.c | 52 +++++++++++++++++++----------- 3 files changed, 36 insertions(+), 18 deletions(-) diff --git a/arch/x86/include/asm/kvm-x86-ops.h b/arch/x86/include/asm/kvm-= x86-ops.h index 378ed944b849..ac8b7614e79d 100644 --- a/arch/x86/include/asm/kvm-x86-ops.h +++ b/arch/x86/include/asm/kvm-x86-ops.h @@ -122,6 +122,7 @@ KVM_X86_OP(enter_smm) KVM_X86_OP(leave_smm) KVM_X86_OP(enable_smi_window) #endif +KVM_X86_OP_OPTIONAL(dev_get_attr) KVM_X86_OP_OPTIONAL(mem_enc_ioctl) KVM_X86_OP_OPTIONAL(mem_enc_register_region) KVM_X86_OP_OPTIONAL(mem_enc_unregister_region) diff --git a/arch/x86/include/asm/kvm_host.h b/arch/x86/include/asm/kvm_hos= t.h index d271ba20a0b2..0bcd9ae16097 100644 --- a/arch/x86/include/asm/kvm_host.h +++ b/arch/x86/include/asm/kvm_host.h @@ -1769,6 +1769,7 @@ struct kvm_x86_ops { void (*enable_smi_window)(struct kvm_vcpu *vcpu); #endif =20 + int (*dev_get_attr)(u64 attr, u64 *val); int (*mem_enc_ioctl)(struct kvm *kvm, void __user *argp); int (*mem_enc_register_region)(struct kvm *kvm, struct kvm_enc_region *ar= gp); int (*mem_enc_unregister_region)(struct kvm *kvm, struct kvm_enc_region *= argp); diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index bf10a9073a09..8746530930d5 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -4804,37 +4804,53 @@ static inline void __user *kvm_get_attr_addr(struct= kvm_device_attr *attr) return uaddr; } =20 -static int kvm_x86_dev_get_attr(struct kvm_device_attr *attr) +static int __kvm_x86_dev_get_attr(struct kvm_device_attr *attr, u64 *val) { - u64 __user *uaddr =3D kvm_get_attr_addr(attr); + int r; =20 if (attr->group) return -ENXIO; =20 + switch (attr->attr) { + case KVM_X86_XCOMP_GUEST_SUPP: + r =3D 0; + *val =3D kvm_caps.supported_xcr0; + break; + default: + r =3D -ENXIO; + if (kvm_x86_ops.dev_get_attr) + r =3D kvm_x86_ops.dev_get_attr(attr->attr, val); + break; + } + + return r; +} + +static int kvm_x86_dev_get_attr(struct kvm_device_attr *attr) +{ + u64 __user *uaddr; + int r; + u64 val; + + r =3D __kvm_x86_dev_get_attr(attr, &val); + if (r < 0) + return r; + + uaddr =3D kvm_get_attr_addr(attr); if (IS_ERR(uaddr)) return PTR_ERR(uaddr); =20 - switch (attr->attr) { - case KVM_X86_XCOMP_GUEST_SUPP: - if (put_user(kvm_caps.supported_xcr0, uaddr)) - return -EFAULT; - return 0; - default: - return -ENXIO; - } + if (put_user(val, uaddr)) + return -EFAULT; + + return 0; } =20 static int kvm_x86_dev_has_attr(struct kvm_device_attr *attr) { - if (attr->group) - return -ENXIO; + u64 val; =20 - switch (attr->attr) { - case KVM_X86_XCOMP_GUEST_SUPP: - return 0; - default: - return -ENXIO; - } + return __kvm_x86_dev_get_attr(attr, &val); } =20 long kvm_arch_dev_ioctl(struct file *filp, --=20 2.39.1 From nobody Sun Feb 8 14:56:49 2026 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.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 854F078B7E for ; Fri, 23 Feb 2024 10:40:17 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.133.124 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708684819; cv=none; b=VHrw7iUmuROQa2irzTEBaiIVY+6oTx540u5DMUDMXpM7CTCfpwgVCSeZXz8gJy9TmNcv+5678TIAaP6aVPWvNvPaemELhs4WsAA5iKxmi97uJuApeYB/09IzNaBZc/0Fk/H3ouxdmSZDOriqt1yF57fSOjLx2U2z2iIOkLVzMRU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708684819; c=relaxed/simple; bh=mdeBdatCKk8Xzj5nvfbu+cmRuDDMrfl3wd3KLsml9bc=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version:Content-Type; b=eu7BXyH4wPra3S3oWHN/JijHV2UTZsC03YLvYYhRYe14HqObcb+qyrdgvhhXsrLEehSbHAbDU8sAJVbUkc0id7xfZWGyBM+TwXF3ok107l+t3hFcrCblSMN+FMW/ufWzLTDR/fcTRbZ08fNZBRpQIDUg43VHmOQmxaQvW6p12Fo= 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=TEJo/+xj; arc=none smtp.client-ip=170.10.133.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="TEJo/+xj" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1708684816; 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=HNd1RWFfxP99GP5p/jumFBlLTjGZdI0OBikpCrKysX4=; b=TEJo/+xjlz3F4obG2Ic25FS16NYtOiFQMSTD8W93C+bc4ugwCDF5wVdPGzh18AkqsVGbIM hv6g7ipQXnHv1C8azOuOOMmmaQW6phx1p5nDfMInlmzVxZ+LxKL8iqEioD5YYN0V0/c0wH 995r6SzGeWQcjG2lCbY7f03stn4KdYA= 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-650-UfgFwcO_Nsuq-cLNup0YLg-1; Fri, 23 Feb 2024 05:40:11 -0500 X-MC-Unique: UfgFwcO_Nsuq-cLNup0YLg-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 061A21C068D1; Fri, 23 Feb 2024 10:40:11 +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 D292F112132A; Fri, 23 Feb 2024 10:40:10 +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 v2 03/11] Documentation: kvm/sev: separate description of firmware Date: Fri, 23 Feb 2024 05:40:01 -0500 Message-Id: <20240223104009.632194-4-pbonzini@redhat.com> In-Reply-To: <20240223104009.632194-1-pbonzini@redhat.com> References: <20240223104009.632194-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" The description of firmware is included part under the "SEV Key Management" header, part under the KVM_SEV_INIT ioctl. Put these two bits together and and rename "SEV Key Management" to what it actually is, namely a description of the KVM_MEMORY_ENCRYPT_OP API. Signed-off-by: Paolo Bonzini Message-Id: <20240209183743.22030-4-pbonzini@redhat.com> Reviewed-by: Michael Roth Signed-off-by: Paolo Bonzini --- .../virt/kvm/x86/amd-memory-encryption.rst | 29 +++++++++++-------- 1 file changed, 17 insertions(+), 12 deletions(-) diff --git a/Documentation/virt/kvm/x86/amd-memory-encryption.rst b/Documen= tation/virt/kvm/x86/amd-memory-encryption.rst index 995780088eb2..37c5c37f4f6e 100644 --- a/Documentation/virt/kvm/x86/amd-memory-encryption.rst +++ b/Documentation/virt/kvm/x86/amd-memory-encryption.rst @@ -46,14 +46,8 @@ SEV hardware uses ASIDs to associate a memory encryption= key with a VM. Hence, the ASID for the SEV-enabled guests must be from 1 to a maximum val= ue defined in the CPUID 0x8000001f[ecx] field. =20 -SEV Key Management -=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D - -The SEV guest key management is handled by a separate processor called the= AMD -Secure Processor (AMD-SP). Firmware running inside the AMD-SP provides a s= ecure -key management interface to perform common hypervisor activities such as -encrypting bootstrap code, snapshot, migrating and debugging the guest. Fo= r more -information, see the SEV Key Management spec [api-spec]_ +``KVM_MEMORY_ENCRYPT_OP`` API +=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D =20 The main ioctl to access SEV is KVM_MEMORY_ENCRYPT_OP. If the argument to KVM_MEMORY_ENCRYPT_OP is NULL, the ioctl returns 0 if SEV is enabled @@ -87,10 +81,6 @@ guests, such as launching, running, snapshotting, migrat= ing and decommissioning. The KVM_SEV_INIT command is used by the hypervisor to initialize the SEV p= latform context. In a typical workflow, this command should be the first command i= ssued. =20 -The firmware can be initialized either by using its own non-volatile stora= ge or -the OS can manage the NV storage for the firmware using the module paramet= er -``init_ex_path``. If the file specified by ``init_ex_path`` does not exist= or -is invalid, the OS will create or override the file with output from PSP. =20 Returns: 0 on success, -negative on error =20 @@ -434,6 +424,21 @@ issued by the hypervisor to make the guest ready for e= xecution. =20 Returns: 0 on success, -negative on error =20 +Firmware Management +=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D + +The SEV guest key management is handled by a separate processor called the= AMD +Secure Processor (AMD-SP). Firmware running inside the AMD-SP provides a s= ecure +key management interface to perform common hypervisor activities such as +encrypting bootstrap code, snapshot, migrating and debugging the guest. Fo= r more +information, see the SEV Key Management spec [api-spec]_ + +The AMD-SP firmware can be initialized either by using its own non-volatile +storage or the OS can manage the NV storage for the firmware using +parameter ``init_ex_path`` of the ``ccp`` module. If the file specified +by ``init_ex_path`` does not exist or is invalid, the OS will create or +override the file with PSP non-volatile storage. + References =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D =20 --=20 2.39.1 From nobody Sun Feb 8 14:56:49 2026 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.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 C38FD5D47A for ; Fri, 23 Feb 2024 10:40:14 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.133.124 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708684817; cv=none; b=ZMjin5MOZw1jaPmwk1YSkdgNDtTN42GIGaZzppQML1MdVTK8RFPsasNnkVDWnTuGEiaykZUXfF8gl+/grUAPqzIjIPwnsIBG90ijLMNVpxrku7OiXDjPXgrFnMXkL/TMy2Ma0aIplutpk47wGl5IZwK4fR2SgevRNOVNdRAPb4Q= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708684817; c=relaxed/simple; bh=WiHo26olXym8uZAe6XD29P5g1iEmbJr12ucwrhJHnGo=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version:Content-Type; b=AUmyNV4VAyX6Klp4bjiWSNXu4KMWaK4v70jZWy+PZdX71Jk4zWqN24qAaee5Kk8Fhha3RnT/M8CXqcP05YHnS0D63YulzTOKLvqtRmSmFt++6vNT8fmBBahnJscv01N8qO5Kjq3u+nO81v3X7+hxsRlJXFfhlIFQ93HKz2HUJdw= 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=R4tFFrdB; arc=none smtp.client-ip=170.10.133.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="R4tFFrdB" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1708684813; 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=uGLY1gjVWOR89SFnq4m2u7ro5SjWOGyLr82g9TmbgOU=; b=R4tFFrdB+1km0CgLD4nUhDqt1+A6PlPgGy/fmieSBVcgmP75dkhkwKSXOVGE0WYr/16VAh 8rwe3rGENU4ix9vh06M1p88Zs1RPD0fnR99ElDXflOnIUF5vynt+R5KYYFUAPQgTb1d1yz BpeGUHO5umnB4hQXQa+VHxuqBAcNo6c= 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-696-hrKTSp14MeSYYUCq4hVc5w-1; Fri, 23 Feb 2024 05:40:11 -0500 X-MC-Unique: hrKTSp14MeSYYUCq4hVc5w-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 342E63806720; Fri, 23 Feb 2024 10:40:11 +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 0DBEB112132A; Fri, 23 Feb 2024 10:40:11 +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 v2 04/11] KVM: SEV: publish supported VMSA features Date: Fri, 23 Feb 2024 05:40:02 -0500 Message-Id: <20240223104009.632194-5-pbonzini@redhat.com> In-Reply-To: <20240223104009.632194-1-pbonzini@redhat.com> References: <20240223104009.632194-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" 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 Message-Id: <20240209183743.22030-5-pbonzini@redhat.com> Signed-off-by: Paolo Bonzini --- .../virt/kvm/x86/amd-memory-encryption.rst | 12 ++++++++++ arch/x86/include/uapi/asm/kvm.h | 1 + arch/x86/kvm/svm/sev.c | 23 +++++++++++++++++-- arch/x86/kvm/svm/svm.c | 1 + arch/x86/kvm/svm/svm.h | 1 + 5 files changed, 36 insertions(+), 2 deletions(-) diff --git a/Documentation/virt/kvm/x86/amd-memory-encryption.rst b/Documen= tation/virt/kvm/x86/amd-memory-encryption.rst index 37c5c37f4f6e..5ed11bc16b96 100644 --- a/Documentation/virt/kvm/x86/amd-memory-encryption.rst +++ b/Documentation/virt/kvm/x86/amd-memory-encryption.rst @@ -424,6 +424,18 @@ issued by the hypervisor to make the guest ready for e= xecution. =20 Returns: 0 on success, -negative on error =20 +Device attribute API +=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D + +Attributes of the SEV implementation can be retrieved through the +``KVM_HAS_DEVICE_ATTR`` and ``KVM_GET_DEVICE_ATTR`` ioctls on the ``/dev/k= vm`` +device node. + +Currently only one attribute is implemented: + +* group 0, attribute ``KVM_X86_SEV_VMSA_FEATURES``: return the set of all + bits that are accepted in the ``vmsa_features`` of ``KVM_SEV_INIT2``. + Firmware Management =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D =20 diff --git a/arch/x86/include/uapi/asm/kvm.h b/arch/x86/include/uapi/asm/kv= m.h index b305daff056e..cccaa5ff6d01 100644 --- a/arch/x86/include/uapi/asm/kvm.h +++ b/arch/x86/include/uapi/asm/kvm.h @@ -459,6 +459,7 @@ struct kvm_sync_regs { =20 /* attributes for system fd (group 0) */ #define KVM_X86_XCOMP_GUEST_SUPP 0 +#define KVM_X86_SEV_VMSA_FEATURES 1 =20 struct kvm_vmx_nested_state_data { __u8 vmcs12[KVM_STATE_NESTED_VMX_VMCS_SIZE]; diff --git a/arch/x86/kvm/svm/sev.c b/arch/x86/kvm/svm/sev.c index f760106c31f8..53e958805ab9 100644 --- a/arch/x86/kvm/svm/sev.c +++ b/arch/x86/kvm/svm/sev.c @@ -59,10 +59,12 @@ module_param_named(sev_es, sev_es_enabled, bool, 0444); /* enable/disable SEV-ES DebugSwap support */ static bool sev_es_debug_swap_enabled =3D true; module_param_named(debug_swap, sev_es_debug_swap_enabled, bool, 0444); +static u64 sev_supported_vmsa_features; #else #define sev_enabled false #define sev_es_enabled false #define sev_es_debug_swap_enabled false +#define sev_supported_vmsa_features 0 #endif /* CONFIG_KVM_AMD_SEV */ =20 static u8 sev_enc_bit; @@ -612,8 +614,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_es_debug_swap_enabled) - save->sev_features |=3D SVM_SEV_FEAT_DEBUG_SWAP; + save->sev_features =3D sev_supported_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); @@ -1849,6 +1850,20 @@ int sev_vm_move_enc_context_from(struct kvm *kvm, un= signed int source_fd) return ret; } =20 +int sev_dev_get_attr(u64 attr, u64 *val) +{ + switch (attr) { +#ifdef CONFIG_KVM_AMD_SEV + case KVM_X86_SEV_VMSA_FEATURES: + *val =3D sev_supported_vmsa_features; + return 0; +#endif + + default: + return -ENXIO; + } +} + int sev_mem_enc_ioctl(struct kvm *kvm, void __user *argp) { struct kvm_sev_cmd sev_cmd; @@ -2276,6 +2291,10 @@ void __init sev_hardware_setup(void) 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 =3D false; + + sev_supported_vmsa_features =3D 0; + if (sev_es_debug_swap_enabled) + sev_supported_vmsa_features |=3D SVM_SEV_FEAT_DEBUG_SWAP; #endif } =20 diff --git a/arch/x86/kvm/svm/svm.c b/arch/x86/kvm/svm/svm.c index e90b429c84f1..aa1792f402ab 100644 --- a/arch/x86/kvm/svm/svm.c +++ b/arch/x86/kvm/svm/svm.c @@ -5014,6 +5014,7 @@ static struct kvm_x86_ops svm_x86_ops __initdata =3D { .enable_smi_window =3D svm_enable_smi_window, #endif =20 + .dev_get_attr =3D sev_dev_get_attr, .mem_enc_ioctl =3D sev_mem_enc_ioctl, .mem_enc_register_region =3D sev_mem_enc_register_region, .mem_enc_unregister_region =3D sev_mem_enc_unregister_region, diff --git a/arch/x86/kvm/svm/svm.h b/arch/x86/kvm/svm/svm.h index 8ef95139cd24..d630026b23b0 100644 --- a/arch/x86/kvm/svm/svm.h +++ b/arch/x86/kvm/svm/svm.h @@ -671,6 +671,7 @@ void avic_refresh_virtual_apic_mode(struct kvm_vcpu *vc= pu); extern unsigned int max_sev_asid; =20 void sev_vm_destroy(struct kvm *kvm); +int sev_dev_get_attr(u64 attr, u64 *val); int sev_mem_enc_ioctl(struct kvm *kvm, void __user *argp); int sev_mem_enc_register_region(struct kvm *kvm, struct kvm_enc_region *range); --=20 2.39.1 From nobody Sun Feb 8 14:56:49 2026 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.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 6660B6311D for ; Fri, 23 Feb 2024 10:40:16 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.133.124 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708684820; cv=none; b=dXPU4XMjHP7R9wxDGIiWLrN4FAN7SoQDlxrgtR2pQYREzZZW5MGjHnwE8Wd/gRuCF15DA+8sQia+sirvGMwvQ9koARK6peK5VElqUenZ/U4Y2ng8stChHJAhW5SMdKTL4UTwJdouJVrjQ4R2VPoVE4fvuUt2hlySD0pqUGKljRU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708684820; c=relaxed/simple; bh=LmL/QgvlxJDbXIsEvOpKded4ruGPZfrgdFOV8JxYsHI=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version:Content-Type; b=ZhmDuQOq4aP9k/uj0WovEkMinhjYdH7hDBWTc2Erd3M4ahLjF3suJboZJ4vZrBnmeJjZHPKKJPkwihNqCowgrCzvrmJS8EUhlgV+QVYQRpy0bD9ZMVEV1jkZm+ygnQjPWWErGAwXl7K2LV/C+Lb3E9uzQPlxvBPLslNKfkuTbA4= 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=elzG7JAT; arc=none smtp.client-ip=170.10.133.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="elzG7JAT" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1708684815; 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=X3aSkSt963si5xzBcpANvQTm6vbv5JXkZquYFhpdO8o=; b=elzG7JATg99gaKaNZ4hWTPzZE2T5eCIo2cz6/R5qQwwUEZmd7Xqw1agEFMF4ptPBv7l/6c E4Ch29bQuDnbuVJpVSpriHqRj5b2fclOi7vR4w0n3lJRP3x3CaXVEnpUZ60+UZd1N2500o bWQmJ5SShJwG3KPYRl93WFe0h2V0sNI= 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-680-hFLhs9FhNrSnghNDvpaDpw-1; Fri, 23 Feb 2024 05:40:11 -0500 X-MC-Unique: hFLhs9FhNrSnghNDvpaDpw-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 63084108BE95; Fri, 23 Feb 2024 10:40:11 +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 3C76F112132A; Fri, 23 Feb 2024 10:40:11 +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 v2 05/11] KVM: SEV: store VMSA features in kvm_sev_info Date: Fri, 23 Feb 2024 05:40:03 -0500 Message-Id: <20240223104009.632194-6-pbonzini@redhat.com> In-Reply-To: <20240223104009.632194-1-pbonzini@redhat.com> References: <20240223104009.632194-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 | 22 ++++++++++++++++++---- arch/x86/kvm/svm/svm.c | 2 +- arch/x86/kvm/svm/svm.h | 3 ++- 3 files changed, 21 insertions(+), 6 deletions(-) diff --git a/arch/x86/kvm/svm/sev.c b/arch/x86/kvm/svm/sev.c index 53e958805ab9..b0e97f9617e3 100644 --- a/arch/x86/kvm/svm/sev.c +++ b/arch/x86/kvm/svm/sev.c @@ -117,6 +117,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(int min_asid, int max_asid) { @@ -259,6 +267,8 @@ static int sev_guest_init(struct kvm *kvm, struct kvm_s= ev_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; + asid =3D sev_asid_new(sev); if (asid < 0) goto e_no_asid; @@ -279,6 +289,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; @@ -573,6 +584,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 */ @@ -614,7 +627,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 - save->sev_features =3D sev_supported_vmsa_features; + 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); @@ -1694,6 +1707,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; @@ -3064,7 +3078,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); @@ -3119,7 +3133,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 @@ -3147,7 +3161,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 aa1792f402ab..392b9c2e2ce1 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 d630026b23b0..864c782eaa58 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 */ @@ -693,7 +694,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 /* vmenter.S */ --=20 2.39.1 From nobody Sun Feb 8 14:56:49 2026 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.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 BD0497AE45 for ; Fri, 23 Feb 2024 10:40:18 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.133.124 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708684820; cv=none; b=FdRsl4DLEHSbsOi/dv8iBpfjDbtLA66yn3myHfjFlqoXtYi/EJKSE25Zutkbm2o5af3F2JwmWF35GTcTknLyQc3ICK/YBWhoC/zKvjUuYTGg04wmTOQ68QEhB3AZ38jbIrPritE811y/d2+0rR2vYcJh2sfuiAOr9d95ihS1tXk= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708684820; c=relaxed/simple; bh=pU+I1UNsFigwwYduAuVyFUMAum80+uoBu3TqnVl6ud4=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version:Content-Type; b=byDBgTHZJwSfHQMGsNcpF4UD9juu8UtW/aTp7Fuia9lNqsgUdoqJxtwI+LFBv5F1RJZ6S350dkAAGj3qlzAjYMBaaeeAO8VRA75f6W5VlTCcthwBvhXC8x1jFyDPYF3xgnoniUfOT8DQDDJAAzDV+q2eVt0CP3ZYD8UhVeEPlM4= 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=WVBmRb48; arc=none smtp.client-ip=170.10.133.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="WVBmRb48" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1708684817; 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=/d+3soBxX8Esy8j47TVH8Z004bmpwg8FksG075eEEEk=; b=WVBmRb48GRqajvfTbDMi+UVbZRLzePhP3ojg8v4Vfzof5zsN7qCzPg6IKO1nt/LScB/0uU awXyyfCf8P0T/VWU0YdfHdxPDvXTrpCbrzgDbVuUqlqfgTa6AIAYNjulkw5qrPgKW2vijd 4zNIXdsW+g+TbGR10AJA71SKHid86Is= 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-56-2OYUTg_bN8qaFMvQcjEpww-1; Fri, 23 Feb 2024 05:40:12 -0500 X-MC-Unique: 2OYUTg_bN8qaFMvQcjEpww-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 8FE1729AA2CF; Fri, 23 Feb 2024 10:40:11 +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 6A420112132A; Fri, 23 Feb 2024 10:40:11 +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 v2 06/11] KVM: SEV: disable DEBUG_SWAP by default Date: Fri, 23 Feb 2024 05:40:04 -0500 Message-Id: <20240223104009.632194-7-pbonzini@redhat.com> In-Reply-To: <20240223104009.632194-1-pbonzini@redhat.com> References: <20240223104009.632194-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" Disable all VMSA features in KVM_SEV_INIT and KVM_SEV_ES_INIT. They are not actually supported by SEV (a SEV guest does not have a VMSA to which you can apply features) and they cause unexpected changes in measurement for SEV-ES. Going on, the way to enable them will be to use a new initialization ioctl that takes the VMSA features as a parameter. Signed-off-by: Paolo Bonzini --- arch/x86/kvm/svm/sev.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/arch/x86/kvm/svm/sev.c b/arch/x86/kvm/svm/sev.c index b0e97f9617e3..06e03a6fe7e4 100644 --- a/arch/x86/kvm/svm/sev.c +++ b/arch/x86/kvm/svm/sev.c @@ -267,7 +267,7 @@ static int sev_guest_init(struct kvm *kvm, struct kvm_s= ev_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; + sev->vmsa_features =3D 0; =20 asid =3D sev_asid_new(sev); if (asid < 0) --=20 2.39.1 From nobody Sun Feb 8 14:56:49 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 780E078B76 for ; Fri, 23 Feb 2024 10:40:17 +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=1708684820; cv=none; b=m4Uim+03JhS9Dl58n0pZ6dcTQQ2P/3uQf8/o1YWDvdPmx5cRsR8SFWnLh+fRSefUfFThYFJCTMo1idJmFH2BmP5tBmp/RgnUF5ckfrvY8mBAJWEWfWFeWwcvcj24eULgPL0z4BMNtpw9Iv3emtFmLeb+dpWoam1jTXCXUGQ7GhA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708684820; c=relaxed/simple; bh=nm6HmMAMwcxtL6M/ucjCqqkXa6rYzYNN1kjIvE8f8Nc=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version:Content-Type; b=clXXmcdf+rf4nPpJ50g974jXsuRaBa/lvy5TWcZnDcJW+eTAG36KCjLluR+85mfDn+Vjr+zhak+Lvhxo8mOFZ4lUoW2Y0fL8bIrpFYeM4rhuefNjpT2qPewxCIoMZq8vIA4ZLmN4klw3zkHmPAhl9rK5A6s6/uPFYmqLptfjgcE= 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=A59uDTyz; 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="A59uDTyz" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1708684816; 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=XBUurJHQMU7qtqFTOu3PnVuwnrzZsBu/4n1xTWVo+Pw=; b=A59uDTyzd8N2BxH9Hqh4G/X/yonXnwPqvUhCKoYZ7k6A5HxJAJzUpNQZpCYDdLbIou5DSy he9+pfJYmvLoeIjX3i19gubG42sVMuwpiBJzTnx9fIlQRlbsisxX467CFLUOIadLLQyLua QXogwSu/Br7XVI6/l1WRMKtKF8gSFDo= 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-5-AyKdURBfOxCjSi5iXyl_iw-1; Fri, 23 Feb 2024 05:40:12 -0500 X-MC-Unique: AyKdURBfOxCjSi5iXyl_iw-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 C3E8888CF74; Fri, 23 Feb 2024 10:40:11 +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 9908D112132A; Fri, 23 Feb 2024 10:40:11 +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 v2 07/11] KVM: x86: define standard behavior for bits 0/1 of VM type Date: Fri, 23 Feb 2024 05:40:05 -0500 Message-Id: <20240223104009.632194-8-pbonzini@redhat.com> In-Reply-To: <20240223104009.632194-1-pbonzini@redhat.com> References: <20240223104009.632194-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" Some VM types have characteristics in common; in fact, the only use of VM types right now is kvm_arch_has_private_mem and it assumes that _all_ VM types have private memory. So, let the low bits specify the characteristics of the VM type. As of we have two special things: whether memory is private, and whether guest state is protected. The latter is similar to kvm->arch.guest_state_protected, but the latter is only set on a fully initialized VM. If both are set, ioctls to set registers will cause an error---SEV-ES did not do so, which is a problematic API. Signed-off-by: Paolo Bonzini Message-Id: <20240209183743.22030-7-pbonzini@redhat.com> Signed-off-by: Paolo Bonzini --- arch/x86/include/asm/kvm_host.h | 9 +++- arch/x86/kvm/x86.c | 93 +++++++++++++++++++++++++++------ 2 files changed, 85 insertions(+), 17 deletions(-) diff --git a/arch/x86/include/asm/kvm_host.h b/arch/x86/include/asm/kvm_hos= t.h index 0bcd9ae16097..15db2697863c 100644 --- a/arch/x86/include/asm/kvm_host.h +++ b/arch/x86/include/asm/kvm_host.h @@ -2135,8 +2135,15 @@ void kvm_mmu_new_pgd(struct kvm_vcpu *vcpu, gpa_t ne= w_pgd); void kvm_configure_mmu(bool enable_tdp, int tdp_forced_root_level, int tdp_max_root_level, int tdp_huge_page_level); =20 + +/* Low bits of VM types provide confidential computing capabilities. */ +#define __KVM_X86_PRIVATE_MEM_TYPE 1 +#define __KVM_X86_PROTECTED_STATE_TYPE 2 +#define __KVM_X86_VM_TYPE_FEATURES 3 +static_assert((KVM_X86_SW_PROTECTED_VM & __KVM_X86_VM_TYPE_FEATURES) =3D= =3D __KVM_X86_PRIVATE_MEM_TYPE); + #ifdef CONFIG_KVM_PRIVATE_MEM -#define kvm_arch_has_private_mem(kvm) ((kvm)->arch.vm_type !=3D KVM_X86_DE= FAULT_VM) +#define kvm_arch_has_private_mem(kvm) ((kvm)->arch.vm_type & __KVM_X86_PRI= VATE_MEM_TYPE) #else #define kvm_arch_has_private_mem(kvm) false #endif diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index 8746530930d5..e634e5b67516 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -5526,21 +5526,30 @@ static int kvm_vcpu_ioctl_x86_set_vcpu_events(struc= t kvm_vcpu *vcpu, return 0; } =20 -static void kvm_vcpu_ioctl_x86_get_debugregs(struct kvm_vcpu *vcpu, - struct kvm_debugregs *dbgregs) +static int kvm_vcpu_ioctl_x86_get_debugregs(struct kvm_vcpu *vcpu, + struct kvm_debugregs *dbgregs) { unsigned long val; =20 + if ((vcpu->kvm->arch.vm_type & __KVM_X86_PROTECTED_STATE_TYPE) && + vcpu->arch.guest_state_protected) + return -EINVAL; + memset(dbgregs, 0, sizeof(*dbgregs)); memcpy(dbgregs->db, vcpu->arch.db, sizeof(vcpu->arch.db)); kvm_get_dr(vcpu, 6, &val); dbgregs->dr6 =3D val; dbgregs->dr7 =3D vcpu->arch.dr7; + return 0; } =20 static int kvm_vcpu_ioctl_x86_set_debugregs(struct kvm_vcpu *vcpu, struct kvm_debugregs *dbgregs) { + if ((vcpu->kvm->arch.vm_type & __KVM_X86_PROTECTED_STATE_TYPE) && + vcpu->arch.guest_state_protected) + return -EINVAL; + if (dbgregs->flags) return -EINVAL; =20 @@ -5559,9 +5568,13 @@ static int kvm_vcpu_ioctl_x86_set_debugregs(struct k= vm_vcpu *vcpu, } =20 =20 -static void kvm_vcpu_ioctl_x86_get_xsave2(struct kvm_vcpu *vcpu, - u8 *state, unsigned int size) +static int kvm_vcpu_ioctl_x86_get_xsave2(struct kvm_vcpu *vcpu, + u8 *state, unsigned int size) { + if ((vcpu->kvm->arch.vm_type & __KVM_X86_PROTECTED_STATE_TYPE) && + fpstate_is_confidential(&vcpu->arch.guest_fpu)) + return -EINVAL; + /* * Only copy state for features that are enabled for the guest. The * state itself isn't problematic, but setting bits in the header for @@ -5578,22 +5591,27 @@ static void kvm_vcpu_ioctl_x86_get_xsave2(struct kv= m_vcpu *vcpu, XFEATURE_MASK_FPSSE; =20 if (fpstate_is_confidential(&vcpu->arch.guest_fpu)) - return; + return 0; =20 fpu_copy_guest_fpstate_to_uabi(&vcpu->arch.guest_fpu, state, size, supported_xcr0, vcpu->arch.pkru); + return 0; } =20 -static void kvm_vcpu_ioctl_x86_get_xsave(struct kvm_vcpu *vcpu, - struct kvm_xsave *guest_xsave) +static int kvm_vcpu_ioctl_x86_get_xsave(struct kvm_vcpu *vcpu, + struct kvm_xsave *guest_xsave) { - kvm_vcpu_ioctl_x86_get_xsave2(vcpu, (void *)guest_xsave->region, - sizeof(guest_xsave->region)); + return kvm_vcpu_ioctl_x86_get_xsave2(vcpu, (void *)guest_xsave->region, + sizeof(guest_xsave->region)); } =20 static int kvm_vcpu_ioctl_x86_set_xsave(struct kvm_vcpu *vcpu, struct kvm_xsave *guest_xsave) { + if ((vcpu->kvm->arch.vm_type & __KVM_X86_PROTECTED_STATE_TYPE) && + fpstate_is_confidential(&vcpu->arch.guest_fpu)) + return -EINVAL; + if (fpstate_is_confidential(&vcpu->arch.guest_fpu)) return 0; =20 @@ -5603,18 +5621,23 @@ static int kvm_vcpu_ioctl_x86_set_xsave(struct kvm_= vcpu *vcpu, &vcpu->arch.pkru); } =20 -static void kvm_vcpu_ioctl_x86_get_xcrs(struct kvm_vcpu *vcpu, - struct kvm_xcrs *guest_xcrs) +static int kvm_vcpu_ioctl_x86_get_xcrs(struct kvm_vcpu *vcpu, + struct kvm_xcrs *guest_xcrs) { + if ((vcpu->kvm->arch.vm_type & __KVM_X86_PROTECTED_STATE_TYPE) && + vcpu->arch.guest_state_protected) + return -EINVAL; + if (!boot_cpu_has(X86_FEATURE_XSAVE)) { guest_xcrs->nr_xcrs =3D 0; - return; + return 0; } =20 guest_xcrs->nr_xcrs =3D 1; guest_xcrs->flags =3D 0; guest_xcrs->xcrs[0].xcr =3D XCR_XFEATURE_ENABLED_MASK; guest_xcrs->xcrs[0].value =3D vcpu->arch.xcr0; + return 0; } =20 static int kvm_vcpu_ioctl_x86_set_xcrs(struct kvm_vcpu *vcpu, @@ -5622,6 +5645,10 @@ static int kvm_vcpu_ioctl_x86_set_xcrs(struct kvm_vc= pu *vcpu, { int i, r =3D 0; =20 + if ((vcpu->kvm->arch.vm_type & __KVM_X86_PROTECTED_STATE_TYPE) && + vcpu->arch.guest_state_protected) + return -EINVAL; + if (!boot_cpu_has(X86_FEATURE_XSAVE)) return -EINVAL; =20 @@ -6010,7 +6037,9 @@ long kvm_arch_vcpu_ioctl(struct file *filp, case KVM_GET_DEBUGREGS: { struct kvm_debugregs dbgregs; =20 - kvm_vcpu_ioctl_x86_get_debugregs(vcpu, &dbgregs); + r =3D kvm_vcpu_ioctl_x86_get_debugregs(vcpu, &dbgregs); + if (r < 0) + break; =20 r =3D -EFAULT; if (copy_to_user(argp, &dbgregs, @@ -6040,7 +6069,9 @@ long kvm_arch_vcpu_ioctl(struct file *filp, if (!u.xsave) break; =20 - kvm_vcpu_ioctl_x86_get_xsave(vcpu, u.xsave); + r =3D kvm_vcpu_ioctl_x86_get_xsave(vcpu, u.xsave); + if (r < 0) + break; =20 r =3D -EFAULT; if (copy_to_user(argp, u.xsave, sizeof(struct kvm_xsave))) @@ -6069,7 +6100,9 @@ long kvm_arch_vcpu_ioctl(struct file *filp, if (!u.xsave) break; =20 - kvm_vcpu_ioctl_x86_get_xsave2(vcpu, u.buffer, size); + r =3D kvm_vcpu_ioctl_x86_get_xsave2(vcpu, u.buffer, size); + if (r < 0) + break; =20 r =3D -EFAULT; if (copy_to_user(argp, u.xsave, size)) @@ -6085,7 +6118,9 @@ long kvm_arch_vcpu_ioctl(struct file *filp, if (!u.xcrs) break; =20 - kvm_vcpu_ioctl_x86_get_xcrs(vcpu, u.xcrs); + r =3D kvm_vcpu_ioctl_x86_get_xcrs(vcpu, u.xcrs); + if (r < 0) + break; =20 r =3D -EFAULT; if (copy_to_user(argp, u.xcrs, @@ -6229,6 +6264,11 @@ long kvm_arch_vcpu_ioctl(struct file *filp, } #endif case KVM_GET_SREGS2: { + r =3D -EINVAL; + if ((vcpu->kvm->arch.vm_type & __KVM_X86_PROTECTED_STATE_TYPE) && + vcpu->arch.guest_state_protected) + goto out; + u.sregs2 =3D kzalloc(sizeof(struct kvm_sregs2), GFP_KERNEL); r =3D -ENOMEM; if (!u.sregs2) @@ -6241,6 +6281,11 @@ long kvm_arch_vcpu_ioctl(struct file *filp, break; } case KVM_SET_SREGS2: { + r =3D -EINVAL; + if ((vcpu->kvm->arch.vm_type & __KVM_X86_PROTECTED_STATE_TYPE) && + vcpu->arch.guest_state_protected) + goto out; + u.sregs2 =3D memdup_user(argp, sizeof(struct kvm_sregs2)); if (IS_ERR(u.sregs2)) { r =3D PTR_ERR(u.sregs2); @@ -11466,6 +11511,10 @@ static void __get_regs(struct kvm_vcpu *vcpu, stru= ct kvm_regs *regs) =20 int kvm_arch_vcpu_ioctl_get_regs(struct kvm_vcpu *vcpu, struct kvm_regs *r= egs) { + if ((vcpu->kvm->arch.vm_type & __KVM_X86_PROTECTED_STATE_TYPE) && + vcpu->arch.guest_state_protected) + return -EINVAL; + vcpu_load(vcpu); __get_regs(vcpu, regs); vcpu_put(vcpu); @@ -11507,6 +11556,10 @@ static void __set_regs(struct kvm_vcpu *vcpu, stru= ct kvm_regs *regs) =20 int kvm_arch_vcpu_ioctl_set_regs(struct kvm_vcpu *vcpu, struct kvm_regs *r= egs) { + if ((vcpu->kvm->arch.vm_type & __KVM_X86_PROTECTED_STATE_TYPE) && + vcpu->arch.guest_state_protected) + return -EINVAL; + vcpu_load(vcpu); __set_regs(vcpu, regs); vcpu_put(vcpu); @@ -11579,6 +11632,10 @@ static void __get_sregs2(struct kvm_vcpu *vcpu, st= ruct kvm_sregs2 *sregs2) int kvm_arch_vcpu_ioctl_get_sregs(struct kvm_vcpu *vcpu, struct kvm_sregs *sregs) { + if ((vcpu->kvm->arch.vm_type & __KVM_X86_PROTECTED_STATE_TYPE) && + vcpu->arch.guest_state_protected) + return -EINVAL; + vcpu_load(vcpu); __get_sregs(vcpu, sregs); vcpu_put(vcpu); @@ -11846,6 +11903,10 @@ int kvm_arch_vcpu_ioctl_set_sregs(struct kvm_vcpu = *vcpu, { int ret; =20 + if ((vcpu->kvm->arch.vm_type & __KVM_X86_PROTECTED_STATE_TYPE) && + vcpu->arch.guest_state_protected) + return -EINVAL; + vcpu_load(vcpu); ret =3D __set_sregs(vcpu, sregs); vcpu_put(vcpu); --=20 2.39.1 From nobody Sun Feb 8 14:56:49 2026 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.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 787486280B for ; Fri, 23 Feb 2024 10:40:18 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.133.124 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708684820; cv=none; b=CXJXIGwJtGi7BEIBJMH75hdAjziD5F0wxfKuFB5w6zrlglk0G6jBul+AUlV4i0WLWpMibc7rV9SIuIUPp3nEdEQI/QQu7BJqOvME+zZe5DqrT4PprXm9Z6xqfKG600X+Eqag4JTwO68B7OMD0bZZDp8SwwJFz61IHOUPaLyCZ9E= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708684820; c=relaxed/simple; bh=yJOTF4Fo3Zuv/OxFDFVsXOE/9tKTgWyjyBG3UlU2g6g=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version:Content-Type; b=jjIsWJF4iJ34n2z0xPrYeEu30YRwoIc1wCfa2zZo1xxdhpZ0CmjxTjqg50gLsCRw+/ZxGkH7GqsxiOTzaAcPto7nSTDpHoqTgagZ+fOYGk5aX68Tmz4BBZMdTTbZb0ycSlYmRRe799CN0EWbdcGi3zyVDmpYYYJlwYxE3IklC5Y= 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=VKWapa0I; arc=none smtp.client-ip=170.10.133.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="VKWapa0I" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1708684816; 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=P+nR0TAnkSpDrfkTZplFwZ3L3OjoWb5NbbRwlVY9jbI=; b=VKWapa0I6y/e767b6CDMA97799tUOi7Gr0b22TLI6UoG9PrtPmvfxysKjEDITEqhKqiTcl FMpqfH9aEWTXUK1s0jp768IIEhCG9MAFBNLO0YBNdFvnouFDXUloOC/g8cqy7q3Y4sLeRA 1SCmav9pzlk+to1H2Gvt/cNIApolytc= 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-56-C_SQYLXlNTCSs8ELV03X0g-1; Fri, 23 Feb 2024 05:40:12 -0500 X-MC-Unique: C_SQYLXlNTCSs8ELV03X0g-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 F3539185A781; Fri, 23 Feb 2024 10:40:11 +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 CC3C3112132A; Fri, 23 Feb 2024 10:40:11 +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 v2 08/11] KVM: x86: Add is_vm_type_supported callback Date: Fri, 23 Feb 2024 05:40:06 -0500 Message-Id: <20240223104009.632194-9-pbonzini@redhat.com> In-Reply-To: <20240223104009.632194-1-pbonzini@redhat.com> References: <20240223104009.632194-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" Allow the backend to specify which VM types are supported. Based on a patch by Isaku Yamahata . Signed-off-by: Paolo Bonzini --- arch/x86/include/asm/kvm-x86-ops.h | 1 + arch/x86/include/asm/kvm_host.h | 1 + arch/x86/kvm/x86.c | 8 +++++++- 3 files changed, 9 insertions(+), 1 deletion(-) diff --git a/arch/x86/include/asm/kvm-x86-ops.h b/arch/x86/include/asm/kvm-= x86-ops.h index ac8b7614e79d..8694a6e3d012 100644 --- a/arch/x86/include/asm/kvm-x86-ops.h +++ b/arch/x86/include/asm/kvm-x86-ops.h @@ -20,6 +20,7 @@ KVM_X86_OP(hardware_disable) KVM_X86_OP(hardware_unsetup) KVM_X86_OP(has_emulated_msr) KVM_X86_OP(vcpu_after_set_cpuid) +KVM_X86_OP_OPTIONAL_RET0(is_vm_type_supported) KVM_X86_OP(vm_init) KVM_X86_OP_OPTIONAL(vm_destroy) KVM_X86_OP_OPTIONAL_RET0(vcpu_precreate) diff --git a/arch/x86/include/asm/kvm_host.h b/arch/x86/include/asm/kvm_hos= t.h index 15db2697863c..9cf588db55e9 100644 --- a/arch/x86/include/asm/kvm_host.h +++ b/arch/x86/include/asm/kvm_host.h @@ -1601,6 +1601,7 @@ struct kvm_x86_ops { bool (*has_emulated_msr)(struct kvm *kvm, u32 index); void (*vcpu_after_set_cpuid)(struct kvm_vcpu *vcpu); =20 + bool (*is_vm_type_supported)(unsigned long vm_type); unsigned int vm_size; int (*vm_init)(struct kvm *kvm); void (*vm_destroy)(struct kvm *kvm); diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index e634e5b67516..d4029053b6d8 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -4581,13 +4581,19 @@ static int kvm_ioctl_get_supported_hv_cpuid(struct = kvm_vcpu *vcpu, } #endif =20 -static bool kvm_is_vm_type_supported(unsigned long type) +static inline bool __kvm_is_vm_type_supported(unsigned long type) { return type =3D=3D KVM_X86_DEFAULT_VM || (type =3D=3D KVM_X86_SW_PROTECTED_VM && IS_ENABLED(CONFIG_KVM_SW_PROTECTED_VM) && tdp_enabled); } =20 +static bool kvm_is_vm_type_supported(unsigned long type) +{ + return __kvm_is_vm_type_supported(type) || + static_call(kvm_x86_is_vm_type_supported)(type); +} + int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext) { int r =3D 0; --=20 2.39.1 From nobody Sun Feb 8 14:56:49 2026 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.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 99B386313D for ; Fri, 23 Feb 2024 10:40:16 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.133.124 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708684819; cv=none; b=DYDRwKRkNV08Kk6XsViOgtYs8UhSKIjaMh0eiNLwKfAaEq1Hl6BzDKABLUmRJlVUVxjYZCL/w/NS15Gulg5gy7S8olBhpj/ihRR1//zeuIdg1CHjrVkEPVI9jsSYn9T5X9Zt9q3Sr64rMix9uX2vnArxu07tLBHIDTbINgQHVT4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708684819; c=relaxed/simple; bh=DhCTZsK0IpfHWKIUtMPDNMHLet4Gw2VOAQsCh9OviDY=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version:Content-Type; b=ZQUwIcDHVbKWJ5EQ8Xif6vEbz9+qCDXRBjj5v0B0B8djII5Oy5+hz8jbftXuV81km59K+HDiW8HjXf5nL2xH/Gp6Ed4p7FSfDLm+I55+KLxPwo5vyH5pAG7iM7upvEAzb9N01jQ4LNTb+dRIqGCtfCwM8faHnGOkU0kbsM1spWU= 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=HjwGaG1G; arc=none smtp.client-ip=170.10.133.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="HjwGaG1G" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1708684815; 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=6Dxr8YfHFuXWumS+2ASTMr73iq7XV3ma9qDcIY901Hw=; b=HjwGaG1G1rftnoQcWBKD4HRO3D6p7lAPkpFS9LXle5Ggj2N9HuL5muhdBE9siMjpEoHHFs MjWdLNYl1R4g2uUjEJQKOZDYfxKT8uUxSvMJR5kP/vZb6iHMVKkfyWM5OObk4I4roXodPX 7xDqCt7CetF2Zcxkso1EOKnc8y9AC3U= 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-620-le6W4mtaN4WXA0AU7VNi3g-1; Fri, 23 Feb 2024 05:40:12 -0500 X-MC-Unique: le6W4mtaN4WXA0AU7VNi3g-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 2C7F388CF76; Fri, 23 Feb 2024 10:40:12 +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 06B86112132A; Fri, 23 Feb 2024 10:40:12 +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 v2 09/11] KVM: SEV: define VM types for SEV and SEV-ES Date: Fri, 23 Feb 2024 05:40:07 -0500 Message-Id: <20240223104009.632194-10-pbonzini@redhat.com> In-Reply-To: <20240223104009.632194-1-pbonzini@redhat.com> References: <20240223104009.632194-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" Signed-off-by: Paolo Bonzini Message-Id: <20240209183743.22030-9-pbonzini@redhat.com> 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 | 21 ++++++++++++++++++++- arch/x86/kvm/svm/svm.c | 11 +++++++++++ arch/x86/kvm/svm/svm.h | 2 ++ arch/x86/kvm/x86.c | 4 ++++ 6 files changed, 41 insertions(+), 1 deletion(-) diff --git a/Documentation/virt/kvm/api.rst b/Documentation/virt/kvm/api.rst index 3ec0b7a455a0..bf957bb70e4b 100644 --- a/Documentation/virt/kvm/api.rst +++ b/Documentation/virt/kvm/api.rst @@ -8790,6 +8790,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 8 + #define KVM_X86_SEV_ES_VM 10 =20 9. Known KVM API problems =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D diff --git a/arch/x86/include/uapi/asm/kvm.h b/arch/x86/include/uapi/asm/kv= m.h index cccaa5ff6d01..12cf6f3b367e 100644 --- a/arch/x86/include/uapi/asm/kvm.h +++ b/arch/x86/include/uapi/asm/kvm.h @@ -850,5 +850,7 @@ struct kvm_hyperv_eventfd { =20 #define KVM_X86_DEFAULT_VM 0 #define KVM_X86_SW_PROTECTED_VM 1 +#define KVM_X86_SEV_VM 8 +#define KVM_X86_SEV_ES_VM 10 =20 #endif /* _ASM_X86_KVM_H */ diff --git a/arch/x86/kvm/svm/sev.c b/arch/x86/kvm/svm/sev.c index 06e03a6fe7e4..f859ea270fcc 100644 --- a/arch/x86/kvm/svm/sev.c +++ b/arch/x86/kvm/svm/sev.c @@ -261,6 +261,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; + ret =3D -EBUSY; if (unlikely(sev->active)) return ret; @@ -280,6 +283,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 @@ -1815,7 +1819,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; } @@ -2136,6 +2141,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); @@ -3193,3 +3199,16 @@ void sev_vcpu_deliver_sipi_vector(struct kvm_vcpu *v= cpu, u8 vector) =20 ghcb_set_sw_exit_info_2(svm->sev_es.ghcb, 1); } + +bool sev_is_vm_type_supported(unsigned long type) +{ + if (type =3D=3D KVM_X86_SEV_VM) + return sev_enabled; + if (type =3D=3D KVM_X86_SEV_ES_VM) + return sev_es_enabled; + + return false; +} + +static_assert((KVM_X86_SEV_VM & __KVM_X86_VM_TYPE_FEATURES) =3D=3D 0); +static_assert((KVM_X86_SEV_ES_VM & __KVM_X86_VM_TYPE_FEATURES) =3D=3D __KV= M_X86_PROTECTED_STATE_TYPE); diff --git a/arch/x86/kvm/svm/svm.c b/arch/x86/kvm/svm/svm.c index 392b9c2e2ce1..87541c84d07e 100644 --- a/arch/x86/kvm/svm/svm.c +++ b/arch/x86/kvm/svm/svm.c @@ -4087,6 +4087,11 @@ static void svm_cancel_injection(struct kvm_vcpu *vc= pu) =20 static int svm_vcpu_pre_run(struct kvm_vcpu *vcpu) { + struct kvm_sev_info *sev =3D &to_kvm_svm(vcpu->kvm)->sev_info; + + if (sev->need_init) + return -EINVAL; + return 1; } =20 @@ -4888,6 +4893,11 @@ static void svm_vm_destroy(struct kvm *kvm) =20 static int svm_vm_init(struct kvm *kvm) { + if (kvm->arch.vm_type) { + struct kvm_sev_info *sev =3D &to_kvm_svm(kvm)->sev_info; + sev->need_init =3D true; + } + if (!pause_filter_count || !pause_filter_thresh) kvm->arch.pause_in_guest =3D true; =20 @@ -4914,6 +4924,7 @@ static struct kvm_x86_ops svm_x86_ops __initdata =3D { .vcpu_free =3D svm_vcpu_free, .vcpu_reset =3D svm_vcpu_reset, =20 + .is_vm_type_supported =3D sev_is_vm_type_supported, .vm_size =3D sizeof(struct kvm_svm), .vm_init =3D svm_vm_init, .vm_destroy =3D svm_vm_destroy, diff --git a/arch/x86/kvm/svm/svm.h b/arch/x86/kvm/svm/svm.h index 864c782eaa58..63be26d4a024 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 */ @@ -696,6 +697,7 @@ 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 vcpu_svm *svm, struct sev_es_sa= ve_area *hostsa); void sev_es_unmap_ghcb(struct vcpu_svm *svm); +bool sev_is_vm_type_supported(unsigned long type); =20 /* vmenter.S */ =20 diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index d4029053b6d8..4b26d84bc836 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -4794,6 +4794,10 @@ int kvm_vm_ioctl_check_extension(struct kvm *kvm, lo= ng ext) r =3D BIT(KVM_X86_DEFAULT_VM); if (kvm_is_vm_type_supported(KVM_X86_SW_PROTECTED_VM)) r |=3D BIT(KVM_X86_SW_PROTECTED_VM); + if (kvm_is_vm_type_supported(KVM_X86_SEV_VM)) + r |=3D BIT(KVM_X86_SEV_VM); + if (kvm_is_vm_type_supported(KVM_X86_SEV_ES_VM)) + r |=3D BIT(KVM_X86_SEV_ES_VM); break; default: break; --=20 2.39.1 From nobody Sun Feb 8 14:56:49 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 0045E79DDC for ; Fri, 23 Feb 2024 10:40:17 +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=1708684820; cv=none; b=rJczpzTLQ8DGXD5jGilWr0BpXcc1BoSvt5ueGcruDMzt5EneqsW8/TzvVPMqXXufc5chduCqnmPcNSijSRI0jn9kP3alNzOkLd8L3B/qk0JrWzxylv0GQW4fFo+SgB/ZGMp+5yVjMNWF5CGj7Yz3TV8/iK1btt4/hH50gdYDYac= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708684820; c=relaxed/simple; bh=M8+jMVrbf9+TNAxkvhCf/2NGKdpuW9aOBvuO2qAM52k=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version:Content-Type; b=nJdqlgOz4eyjIGEDMAOAintRKZ3+5qmlQ5mW27/ivdD8g8yaRm7DZmXWv7SsodlZ4VcjqODG0+FDseLTlRkUU4hmlm1XUbIn1o7vj9a3e8xubl4juuT4vidfmB7bZvmogw6xlrXTqrhP4+0LauAGgxeDle6tto5caFpX9PdmrtU= 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=iVWwKLzS; 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="iVWwKLzS" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1708684816; 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=P1Q176J8wJxhXIOqS0QqB+TG9NN0auQY50DshlVCsQ4=; b=iVWwKLzSM4mEZTANTpbLunZ7/q9xQ7t2w0Eluz1Kf9N6LwFXigNhmiOfbFxw1IWJ3Bvgn8 ZhKEnzYvbm9VICKKxqgJh2utkXpNeESsr/k+HwIR08+lGOCGgtLdC3BFGhOKKu4AbOQBx7 k1CCfGG5hqUJqKsMEgFL9qixbPFNvtg= 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-547-W3p5ds6BPMW25KxXcMwS4g-1; Fri, 23 Feb 2024 05:40:13 -0500 X-MC-Unique: W3p5ds6BPMW25KxXcMwS4g-1 Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.rdu2.redhat.com [10.11.54.5]) (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 780FA887E46; Fri, 23 Feb 2024 10:40:12 +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 5064310DD4; Fri, 23 Feb 2024 10:40:12 +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 v2 10/11] KVM: SEV: introduce KVM_SEV_INIT2 operation Date: Fri, 23 Feb 2024 05:40:08 -0500 Message-Id: <20240223104009.632194-11-pbonzini@redhat.com> In-Reply-To: <20240223104009.632194-1-pbonzini@redhat.com> References: <20240223104009.632194-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.5 Content-Type: text/plain; charset="utf-8" The idea that no parameter would ever be necessary when enabling SEV or SEV-ES for a VM was decidedly optimistic. In fact, in some sense it's already a parameter whether SEV or SEV-ES is desired. Another possible source of variability is the desired set of VMSA features, as that affects the measurement of the VM's initial state and cannot be changed arbitrarily by the hypervisor. Create a new sub-operation for KVM_MEMORY_ENCRYPT_OP that can take a struct, and put the new op to work by including the VMSA features as a field of the struct. The existing KVM_SEV_INIT and KVM_SEV_ES_INIT use the full set of supported VMSA features for backwards compatibility. The struct also includes the usual bells and whistles for future extensibility: a flags field that must be zero for now, and some padding at the end. Signed-off-by: Paolo Bonzini Message-Id: <20240209183743.22030-10-pbonzini@redhat.com> Signed-off-by: Paolo Bonzini --- .../virt/kvm/x86/amd-memory-encryption.rst | 40 +++++++++++++-- arch/x86/include/uapi/asm/kvm.h | 9 ++++ arch/x86/kvm/svm/sev.c | 50 +++++++++++++++++-- 3 files changed, 92 insertions(+), 7 deletions(-) diff --git a/Documentation/virt/kvm/x86/amd-memory-encryption.rst b/Documen= tation/virt/kvm/x86/amd-memory-encryption.rst index 5ed11bc16b96..b951d82af26c 100644 --- a/Documentation/virt/kvm/x86/amd-memory-encryption.rst +++ b/Documentation/virt/kvm/x86/amd-memory-encryption.rst @@ -75,15 +75,49 @@ are defined in ````. KVM implements the following commands to support common lifecycle events o= f SEV guests, such as launching, running, snapshotting, migrating and decommissi= oning. =20 -1. KVM_SEV_INIT ---------------- +1. KVM_SEV_INIT2 +---------------- =20 -The KVM_SEV_INIT command is used by the hypervisor to initialize the SEV p= latform +The KVM_SEV_INIT2 command is used by the hypervisor to initialize the SEV = platform context. In a typical workflow, this command should be the first command i= ssued. =20 +For this command to be accepted, either KVM_X86_SEV_VM or KVM_X86_SEV_ES_VM +must have been passed to the KVM_CREATE_VM ioctl. A virtual machine creat= ed +with those machine types in turn cannot be run until KVM_SEV_INIT2 is invo= ked. + +Parameters: struct kvm_sev_init (in) =20 Returns: 0 on success, -negative on error =20 +:: + + struct struct kvm_sev_init { + __u64 vmsa_features; /* initial value of features field i= n VMSA */ + __u32 flags; /* must be 0 */ + __u32 pad[9]; + }; + +It is an error if the hypervisor does not support any of the bits that +are set in ``flags`` or ``vmsa_features``. ``vmsa_features`` must be +0 for SEV virtual machines, as they do not have a VMSA. + +This command replaces the deprecated KVM_SEV_INIT and KVM_SEV_ES_INIT comm= ands. +The commands did not have any parameters (the ```data``` field was unused)= and +only work for the KVM_X86_DEFAULT_VM machine type (0). + +They behave as if: + +* the VM type is KVM_X86_SEV_VM for KVM_SEV_INIT, or KVM_X86_SEV_ES_VM for + KVM_SEV_ES_INIT + +* the ``flags`` and ``vmsa_features`` fields of ``struct kvm_sev_init`` are + set to zero + +If the ``KVM_X86_SEV_VMSA_FEATURES`` attribute does not exist, the hypervi= sor only +supports KVM_SEV_INIT and KVM_SEV_ES_INIT. In that case, note that KVM_SE= V_ES_INIT +might set the debug swap VMSA feature (bit 5) depending on the value of the +``debug_swap`` parameter of ``kvm-amd.ko``. + 2. KVM_SEV_LAUNCH_START ----------------------- =20 diff --git a/arch/x86/include/uapi/asm/kvm.h b/arch/x86/include/uapi/asm/kv= m.h index 12cf6f3b367e..f6c13434fa31 100644 --- a/arch/x86/include/uapi/asm/kvm.h +++ b/arch/x86/include/uapi/asm/kvm.h @@ -683,6 +683,9 @@ enum sev_cmd_id { /* Guest Migration Extension */ KVM_SEV_SEND_CANCEL, =20 + /* Second time is the charm; improved versions of the above ioctls. */ + KVM_SEV_INIT2, + KVM_SEV_NR_MAX, }; =20 @@ -694,6 +697,12 @@ struct kvm_sev_cmd { __u32 sev_fd; }; =20 +struct kvm_sev_init { + __u64 vmsa_features; + __u32 flags; + __u32 pad[9]; +}; + struct kvm_sev_launch_start { __u32 handle; __u32 policy; diff --git a/arch/x86/kvm/svm/sev.c b/arch/x86/kvm/svm/sev.c index f859ea270fcc..6df058369433 100644 --- a/arch/x86/kvm/svm/sev.c +++ b/arch/x86/kvm/svm/sev.c @@ -253,15 +253,22 @@ static void sev_unbind_asid(struct kvm *kvm, unsigned= int handle) sev_decommission(handle); } =20 -static int sev_guest_init(struct kvm *kvm, struct kvm_sev_cmd *argp) +static int __sev_guest_init(struct kvm *kvm, struct kvm_sev_cmd *argp, + struct kvm_sev_init *data, + unsigned long vm_type) { struct kvm_sev_info *sev =3D &to_kvm_svm(kvm)->sev_info; + bool es_active =3D (vm_type & __KVM_X86_PROTECTED_STATE_TYPE) !=3D 0; + u64 valid_vmsa_features =3D es_active ? sev_supported_vmsa_features : 0; int asid, ret; =20 if (kvm->created_vcpus) return -EINVAL; =20 - if (kvm->arch.vm_type !=3D KVM_X86_DEFAULT_VM) + if (data->flags) + return -EINVAL; + + if (data->vmsa_features & ~valid_vmsa_features) return -EINVAL; =20 ret =3D -EBUSY; @@ -269,8 +276,8 @@ static int sev_guest_init(struct kvm *kvm, struct kvm_s= ev_cmd *argp) return ret; =20 sev->active =3D true; - sev->es_active =3D argp->id =3D=3D KVM_SEV_ES_INIT; - sev->vmsa_features =3D 0; + sev->es_active =3D es_active; + sev->vmsa_features =3D data->vmsa_features; =20 asid =3D sev_asid_new(sev); if (asid < 0) @@ -299,6 +306,38 @@ static int sev_guest_init(struct kvm *kvm, struct kvm_= sev_cmd *argp) return ret; } =20 +static int sev_guest_init(struct kvm *kvm, struct kvm_sev_cmd *argp) +{ + struct kvm_sev_init data =3D { + .vmsa_features =3D 0, + }; + unsigned long vm_type; + + if (kvm->arch.vm_type !=3D KVM_X86_DEFAULT_VM) + return -EINVAL; + + vm_type =3D (argp->id =3D=3D KVM_SEV_INIT ? KVM_X86_SEV_VM : KVM_X86_SEV_= ES_VM); + return __sev_guest_init(kvm, argp, &data, vm_type); +} + +static int sev_guest_init2(struct kvm *kvm, struct kvm_sev_cmd *argp) +{ + struct kvm_sev_info *sev =3D &to_kvm_svm(kvm)->sev_info; + struct kvm_sev_init data; + + if (!sev->need_init) + return -EINVAL; + + if (kvm->arch.vm_type !=3D KVM_X86_SEV_VM && + kvm->arch.vm_type !=3D KVM_X86_SEV_ES_VM) + return -EINVAL; + + if (copy_from_user(&data, (void __user *)(uintptr_t)argp->data, sizeof(da= ta))) + return -EFAULT; + + return __sev_guest_init(kvm, argp, &data, kvm->arch.vm_type); +} + static int sev_bind_asid(struct kvm *kvm, unsigned int handle, int *error) { struct sev_data_activate activate; @@ -1916,6 +1955,9 @@ int sev_mem_enc_ioctl(struct kvm *kvm, void __user *a= rgp) case KVM_SEV_INIT: r =3D sev_guest_init(kvm, &sev_cmd); break; + case KVM_SEV_INIT2: + r =3D sev_guest_init2(kvm, &sev_cmd); + break; case KVM_SEV_LAUNCH_START: r =3D sev_launch_start(kvm, &sev_cmd); break; --=20 2.39.1 From nobody Sun Feb 8 14:56:49 2026 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.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 C49737AE50 for ; Fri, 23 Feb 2024 10:40:18 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.133.124 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708684821; cv=none; b=mhOoQ7awoSzX7Euc5RY5dWSokBjTsXKKQZSz0+spWF1BBqhz1/cIa6ktJV/P5jF+3w7OVCiT/7mCyeRfmKcrw+KtTfSVIrOqJcJPvMbLjOyCha99k5uQvShs3dM7PBPkUHIoc4stJEroEgtfxU+M81eQIVHBoY5H5pXEy7vOYRA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708684821; c=relaxed/simple; bh=MxHen5gZ4mUabNWwbFOU550bpEpTeJP+x6FtutXKCWU=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version:Content-Type; b=hvJiqmtGH8FvU+neXv7TGt6nX29Ev52EBcrDjOTjh0JfmMXzm+WF8dFrTt2t+MCe25KpXuAqlPSgvPSEHscCT+aWtB8zxxiwD3J53P2dHRExt29vsILWfxm4L7nPYu5ytFKdg3o3ekISrLE+wV+efLSv3xmtHOzLLb+NhsLbbfk= 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=TnTyJlD4; arc=none smtp.client-ip=170.10.133.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="TnTyJlD4" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1708684817; 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=W1kbnyH9FICEHgN6ZoJfgIOpveSUVH1COmtrpN3QbUA=; b=TnTyJlD4XGvZuZNWfW1SCeTzSs0LGeXaqb0aHbPhrT2rLSpGQS5+5t7eYJkwkgcFgNPRTA 6ndHLsn70OdfZ9xx1R6d6q/yep8+d6dJd1WjQN6ya52KCd1ViUidVAe8BhFFXrYDWHzNTG uexTys6ZjLMlw67NpO4A8eOItJGzSy8= 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-620-9M76F_CEM-qdjABZYmJWLw-1; Fri, 23 Feb 2024 05:40:13 -0500 X-MC-Unique: 9M76F_CEM-qdjABZYmJWLw-1 Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.rdu2.redhat.com [10.11.54.5]) (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 A7D1A3806723; Fri, 23 Feb 2024 10:40:12 +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 80E6A8CE8; Fri, 23 Feb 2024 10:40:12 +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 v2 11/11] selftests: kvm: add tests for KVM_SEV_INIT2 Date: Fri, 23 Feb 2024 05:40:09 -0500 Message-Id: <20240223104009.632194-12-pbonzini@redhat.com> In-Reply-To: <20240223104009.632194-1-pbonzini@redhat.com> References: <20240223104009.632194-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.5 Content-Type: text/plain; charset="utf-8" Signed-off-by: Paolo Bonzini Message-Id: <20240209183743.22030-11-pbonzini@redhat.com> Signed-off-by: Paolo Bonzini --- tools/testing/selftests/kvm/Makefile | 1 + .../selftests/kvm/include/kvm_util_base.h | 6 +- .../selftests/kvm/set_memory_region_test.c | 8 +- .../selftests/kvm/x86_64/sev_init2_tests.c | 146 ++++++++++++++++++ 4 files changed, 153 insertions(+), 8 deletions(-) create mode 100644 tools/testing/selftests/kvm/x86_64/sev_init2_tests.c diff --git a/tools/testing/selftests/kvm/Makefile b/tools/testing/selftests= /kvm/Makefile index 492e937fab00..a81a89b1dc2a 100644 --- a/tools/testing/selftests/kvm/Makefile +++ b/tools/testing/selftests/kvm/Makefile @@ -116,6 +116,7 @@ TEST_GEN_PROGS_x86_64 +=3D x86_64/tsc_msrs_test TEST_GEN_PROGS_x86_64 +=3D x86_64/vmx_pmu_caps_test TEST_GEN_PROGS_x86_64 +=3D x86_64/xen_shinfo_test TEST_GEN_PROGS_x86_64 +=3D x86_64/xen_vmcall_test +TEST_GEN_PROGS_x86_64 +=3D x86_64/sev_init2_tests TEST_GEN_PROGS_x86_64 +=3D x86_64/sev_migrate_tests TEST_GEN_PROGS_x86_64 +=3D x86_64/amx_test TEST_GEN_PROGS_x86_64 +=3D x86_64/max_vcpuid_cap_test diff --git a/tools/testing/selftests/kvm/include/kvm_util_base.h b/tools/te= sting/selftests/kvm/include/kvm_util_base.h index 9e5afc472c14..44b6ea56a205 100644 --- a/tools/testing/selftests/kvm/include/kvm_util_base.h +++ b/tools/testing/selftests/kvm/include/kvm_util_base.h @@ -846,17 +846,15 @@ static inline struct kvm_vm *vm_create_barebones(void) return ____vm_create(VM_SHAPE_DEFAULT); } =20 -#ifdef __x86_64__ -static inline struct kvm_vm *vm_create_barebones_protected_vm(void) +static inline struct kvm_vm *vm_create_barebones_type(unsigned long type) { const struct vm_shape shape =3D { .mode =3D VM_MODE_DEFAULT, - .type =3D KVM_X86_SW_PROTECTED_VM, + .type =3D type, }; =20 return ____vm_create(shape); } -#endif =20 static inline struct kvm_vm *vm_create(uint32_t nr_runnable_vcpus) { diff --git a/tools/testing/selftests/kvm/set_memory_region_test.c b/tools/t= esting/selftests/kvm/set_memory_region_test.c index 075b80dbe237..0ac6c21d7251 100644 --- a/tools/testing/selftests/kvm/set_memory_region_test.c +++ b/tools/testing/selftests/kvm/set_memory_region_test.c @@ -339,7 +339,7 @@ static void test_invalid_memory_region_flags(void) =20 #ifdef __x86_64__ if (kvm_check_cap(KVM_CAP_VM_TYPES) & BIT(KVM_X86_SW_PROTECTED_VM)) - vm =3D vm_create_barebones_protected_vm(); + vm =3D vm_create_barebones_type(KVM_X86_SW_PROTECTED_VM); else #endif vm =3D vm_create_barebones(); @@ -452,7 +452,7 @@ static void test_add_private_memory_region(void) =20 pr_info("Testing ADD of KVM_MEM_GUEST_MEMFD memory regions\n"); =20 - vm =3D vm_create_barebones_protected_vm(); + vm =3D vm_create_barebones_type(KVM_X86_SW_PROTECTED_VM); =20 test_invalid_guest_memfd(vm, vm->kvm_fd, 0, "KVM fd should fail"); test_invalid_guest_memfd(vm, vm->fd, 0, "VM's fd should fail"); @@ -461,7 +461,7 @@ static void test_add_private_memory_region(void) test_invalid_guest_memfd(vm, memfd, 0, "Regular memfd() should fail"); close(memfd); =20 - vm2 =3D vm_create_barebones_protected_vm(); + vm2 =3D vm_create_barebones_type(KVM_X86_SW_PROTECTED_VM); memfd =3D vm_create_guest_memfd(vm2, MEM_REGION_SIZE, 0); test_invalid_guest_memfd(vm, memfd, 0, "Other VM's guest_memfd() should f= ail"); =20 @@ -489,7 +489,7 @@ static void test_add_overlapping_private_memory_regions= (void) =20 pr_info("Testing ADD of overlapping KVM_MEM_GUEST_MEMFD memory regions\n"= ); =20 - vm =3D vm_create_barebones_protected_vm(); + vm =3D vm_create_barebones_type(KVM_X86_SW_PROTECTED_VM); =20 memfd =3D vm_create_guest_memfd(vm, MEM_REGION_SIZE * 4, 0); =20 diff --git a/tools/testing/selftests/kvm/x86_64/sev_init2_tests.c b/tools/t= esting/selftests/kvm/x86_64/sev_init2_tests.c new file mode 100644 index 000000000000..644fd5757041 --- /dev/null +++ b/tools/testing/selftests/kvm/x86_64/sev_init2_tests.c @@ -0,0 +1,146 @@ +// SPDX-License-Identifier: GPL-2.0-only +#include +#include +#include +#include +#include +#include +#include + +#include "test_util.h" +#include "kvm_util.h" +#include "processor.h" +#include "svm_util.h" +#include "kselftest.h" + +#define SVM_SEV_FEAT_DEBUG_SWAP 32u + +/* + * Some features may have hidden dependencies, or may only work + * for certain VM types. Err on the side of safety and don't + * expect that all supported features can be passed one by one + * to KVM_SEV_INIT2. + * + * (Well, right now there's only one...) + */ +#define KNOWN_FEATURES SVM_SEV_FEAT_DEBUG_SWAP + +int kvm_fd; +u64 supported_vmsa_features; +bool have_sev_es; + +static int __sev_ioctl(int vm_fd, int cmd_id, void *data) +{ + struct kvm_sev_cmd cmd =3D { + .id =3D cmd_id, + .data =3D (uint64_t)data, + .sev_fd =3D open_sev_dev_path_or_exit(), + }; + int ret; + + ret =3D ioctl(vm_fd, KVM_MEMORY_ENCRYPT_OP, &cmd); + TEST_ASSERT(ret < 0 || cmd.error =3D=3D SEV_RET_SUCCESS, + "%d failed: fw error: %d\n", + cmd_id, cmd.error); + + return ret; +} + +static void test_init2(unsigned long vm_type, struct kvm_sev_init *init) +{ + struct kvm_vm *vm; + int ret; + + vm =3D vm_create_barebones_type(vm_type); + ret =3D __sev_ioctl(vm->fd, KVM_SEV_INIT2, init); + TEST_ASSERT(ret =3D=3D 0, + "KVM_SEV_INIT2 return code is %d (expected 0), errno: %d", + ret, errno); + kvm_vm_free(vm); +} + +static void test_init2_invalid(unsigned long vm_type, struct kvm_sev_init = *init) +{ + struct kvm_vm *vm; + int ret; + + vm =3D vm_create_barebones_type(vm_type); + ret =3D __sev_ioctl(vm->fd, KVM_SEV_INIT2, init); + TEST_ASSERT(ret =3D=3D -1 && errno =3D=3D EINVAL, + "KVM_SEV_INIT2 return code %d, errno: %d (expected EINVAL)", + ret, errno); + kvm_vm_free(vm); +} + +void test_vm_types(void) +{ + test_init2(KVM_X86_SEV_VM, &(struct kvm_sev_init){}); + + if (have_sev_es) + test_init2(KVM_X86_SEV_ES_VM, &(struct kvm_sev_init){}); + else + test_init2_invalid(KVM_X86_SEV_ES_VM, &(struct kvm_sev_init){}); + + test_init2_invalid(0, &(struct kvm_sev_init){}); + if (kvm_check_cap(KVM_CAP_VM_TYPES) & BIT(KVM_X86_SW_PROTECTED_VM)) + test_init2_invalid(KVM_X86_SW_PROTECTED_VM, &(struct kvm_sev_init){}); +} + +void test_flags(uint32_t vm_type) +{ + int i; + + for (i =3D 0; i < 32; i++) + test_init2_invalid(vm_type, &(struct kvm_sev_init){ + .flags =3D 1u << i, + }); +} + +void test_features(uint32_t vm_type, uint64_t supported_features) +{ + int i; + + for (i =3D 0; i < 64; i++) { + if (!(supported_features & (1u << i))) + test_init2_invalid(vm_type, &(struct kvm_sev_init){ + .vmsa_features =3D 1u << i, + }); + else if (KNOWN_FEATURES & (1u << i)) + test_init2(vm_type, &(struct kvm_sev_init){ + .vmsa_features =3D 1u << i, + }); + } +} + +int main(int argc, char *argv[]) +{ + int kvm_fd =3D open_kvm_dev_path_or_exit(); + bool have_sev; + + TEST_REQUIRE(__kvm_has_device_attr(kvm_fd, 0, KVM_X86_SEV_VMSA_FEATURES) = =3D=3D 0); + kvm_device_attr_get(kvm_fd, 0, KVM_X86_SEV_VMSA_FEATURES, &supported_vmsa= _features); + + have_sev =3D kvm_cpu_has(X86_FEATURE_SEV); + TEST_ASSERT(have_sev =3D=3D !!(kvm_check_cap(KVM_CAP_VM_TYPES) & BIT(KVM_= X86_SEV_VM)), + "sev: KVM_CAP_VM_TYPES (%x) does not match cpuid (checking %x)", + kvm_check_cap(KVM_CAP_VM_TYPES), 1 << KVM_X86_SEV_VM); + + TEST_REQUIRE(kvm_check_cap(KVM_CAP_VM_TYPES) & BIT(KVM_X86_SEV_VM)); + have_sev_es =3D kvm_cpu_has(X86_FEATURE_SEV_ES); + + TEST_ASSERT(have_sev_es =3D=3D !!(kvm_check_cap(KVM_CAP_VM_TYPES) & BIT(K= VM_X86_SEV_ES_VM)), + "sev-es: KVM_CAP_VM_TYPES (%x) does not match cpuid (checking %x)", + kvm_check_cap(KVM_CAP_VM_TYPES), 1 << KVM_X86_SEV_ES_VM); + + test_vm_types(); + + test_flags(KVM_X86_SEV_VM); + if (have_sev_es) + test_flags(KVM_X86_SEV_ES_VM); + + test_features(KVM_X86_SEV_VM, 0); + if (have_sev_es) + test_features(KVM_X86_SEV_ES_VM, supported_vmsa_features); + + return 0; +} --=20 2.39.1