From nobody Tue Jun 30 20:09:23 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 424EEC4332F for ; Mon, 10 Jan 2022 06:05:13 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S238895AbiAJGFK (ORCPT ); Mon, 10 Jan 2022 01:05:10 -0500 Received: from out30-57.freemail.mail.aliyun.com ([115.124.30.57]:37454 "EHLO out30-57.freemail.mail.aliyun.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S238871AbiAJGFI (ORCPT ); Mon, 10 Jan 2022 01:05:08 -0500 X-Alimail-AntiSpam: AC=PASS;BC=-1|-1;BR=01201311R291e4;CH=green;DM=||false|;DS=||;FP=0|-1|-1|-1|0|-1|-1|-1;HT=e01e04395;MF=shirong@linux.alibaba.com;NM=1;PH=DS;RN=25;SR=0;TI=SMTPD_---0V1LvBNz_1641794698; Received: from localhost.localdomain(mailfrom:shirong@linux.alibaba.com fp:SMTPD_---0V1LvBNz_1641794698) by smtp.aliyun-inc.com(127.0.0.1); Mon, 10 Jan 2022 14:05:04 +0800 From: Shirong Hao To: pbonzini@redhat.com, seanjc@google.com, vkuznets@redhat.com, wanpengli@tencent.com, jmattson@google.com, joro@8bytes.org, tglx@linutronix.de, mingo@redhat.co, bp@alien8.de, dave.hansen@linux.intel.com, x86@kernel.org, hpa@zytor.com, brijesh.singh@amd.com, thomas.lendacky@amd.com, john.allen@amd.com, herbert@gondor.apana.org.au, davem@davemloft.net, srutherford@google.com, ashish.kalra@amd.com, natet@google.com Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org, linux-crypto@vger.kernel.org, zhang.jia@linux.alibaba.com, Shirong Hao Subject: [PATCH 1/3] KVM: X86: Introduce KVM_HC_VM_HANDLE hypercall Date: Mon, 10 Jan 2022 14:04:43 +0800 Message-Id: <20220110060445.549800-2-shirong@linux.alibaba.com> X-Mailer: git-send-email 2.27.0 In-Reply-To: <20220110060445.549800-1-shirong@linux.alibaba.com> References: <20220110060445.549800-1-shirong@linux.alibaba.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" This hypercall is used by the SEV guest to get the firmware handle. Signed-off-by: Shirong Hao --- arch/x86/include/asm/kvm_host.h | 1 + arch/x86/kvm/svm/svm.c | 11 +++++++++++ arch/x86/kvm/x86.c | 7 ++++++- include/uapi/linux/kvm_para.h | 1 + 4 files changed, 19 insertions(+), 1 deletion(-) diff --git a/arch/x86/include/asm/kvm_host.h b/arch/x86/include/asm/kvm_hos= t.h index 2164b9f4c7b0..fe745f4e6954 100644 --- a/arch/x86/include/asm/kvm_host.h +++ b/arch/x86/include/asm/kvm_host.h @@ -1493,6 +1493,7 @@ struct kvm_x86_ops { int (*complete_emulated_msr)(struct kvm_vcpu *vcpu, int err); =20 void (*vcpu_deliver_sipi_vector)(struct kvm_vcpu *vcpu, u8 vector); + int (*vm_handle)(struct kvm *kvm); }; =20 struct kvm_x86_nested_ops { diff --git a/arch/x86/kvm/svm/svm.c b/arch/x86/kvm/svm/svm.c index d0f68d11ec70..c0eb310cb4c3 100644 --- a/arch/x86/kvm/svm/svm.c +++ b/arch/x86/kvm/svm/svm.c @@ -4576,6 +4576,16 @@ static int svm_vm_init(struct kvm *kvm) return 0; } =20 +static int sev_vm_handle(struct kvm *kvm) +{ + struct kvm_sev_info *sev =3D &to_kvm_svm(kvm)->sev_info; + + if (!sev_guest(kvm)) + return -ENOTTY; + + return sev->handle; +} + static struct kvm_x86_ops svm_x86_ops __initdata =3D { .name =3D "kvm_amd", =20 @@ -4705,6 +4715,7 @@ static struct kvm_x86_ops svm_x86_ops __initdata =3D { .complete_emulated_msr =3D svm_complete_emulated_msr, =20 .vcpu_deliver_sipi_vector =3D svm_vcpu_deliver_sipi_vector, + .vm_handle =3D sev_vm_handle, }; =20 static struct kvm_x86_init_ops svm_init_ops __initdata =3D { diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index 0cf1082455df..24acf0f2a539 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -8906,7 +8906,7 @@ int kvm_emulate_hypercall(struct kvm_vcpu *vcpu) a3 &=3D 0xFFFFFFFF; } =20 - if (static_call(kvm_x86_get_cpl)(vcpu) !=3D 0) { + if (static_call(kvm_x86_get_cpl)(vcpu) !=3D 0 && nr !=3D KVM_HC_VM_HANDLE= ) { ret =3D -KVM_EPERM; goto out; } @@ -8965,6 +8965,11 @@ int kvm_emulate_hypercall(struct kvm_vcpu *vcpu) vcpu->arch.complete_userspace_io =3D complete_hypercall_exit; return 0; } + case KVM_HC_VM_HANDLE: + ret =3D -KVM_ENOSYS; + if (kvm_x86_ops.vm_handle) + ret =3D kvm_x86_ops.vm_handle(vcpu->kvm); + break; default: ret =3D -KVM_ENOSYS; break; diff --git a/include/uapi/linux/kvm_para.h b/include/uapi/linux/kvm_para.h index 960c7e93d1a9..b64469a12707 100644 --- a/include/uapi/linux/kvm_para.h +++ b/include/uapi/linux/kvm_para.h @@ -30,6 +30,7 @@ #define KVM_HC_SEND_IPI 10 #define KVM_HC_SCHED_YIELD 11 #define KVM_HC_MAP_GPA_RANGE 12 +#define KVM_HC_VM_HANDLE 13 =20 /* * hypercalls use architecture specific --=20 2.27.0 From nobody Tue Jun 30 20:09:23 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 61B4FC433FE for ; Mon, 10 Jan 2022 06:05:23 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S238935AbiAJGFU (ORCPT ); Mon, 10 Jan 2022 01:05:20 -0500 Received: from out30-42.freemail.mail.aliyun.com ([115.124.30.42]:57473 "EHLO out30-42.freemail.mail.aliyun.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S238891AbiAJGFL (ORCPT ); Mon, 10 Jan 2022 01:05:11 -0500 X-Alimail-AntiSpam: AC=PASS;BC=-1|-1;BR=01201311R111e4;CH=green;DM=||false|;DS=||;FP=0|-1|-1|-1|0|-1|-1|-1;HT=e01e04394;MF=shirong@linux.alibaba.com;NM=1;PH=DS;RN=25;SR=0;TI=SMTPD_---0V1LvBNz_1641794698; Received: from localhost.localdomain(mailfrom:shirong@linux.alibaba.com fp:SMTPD_---0V1LvBNz_1641794698) by smtp.aliyun-inc.com(127.0.0.1); Mon, 10 Jan 2022 14:05:07 +0800 From: Shirong Hao To: pbonzini@redhat.com, seanjc@google.com, vkuznets@redhat.com, wanpengli@tencent.com, jmattson@google.com, joro@8bytes.org, tglx@linutronix.de, mingo@redhat.co, bp@alien8.de, dave.hansen@linux.intel.com, x86@kernel.org, hpa@zytor.com, brijesh.singh@amd.com, thomas.lendacky@amd.com, john.allen@amd.com, herbert@gondor.apana.org.au, davem@davemloft.net, srutherford@google.com, ashish.kalra@amd.com, natet@google.com Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org, linux-crypto@vger.kernel.org, zhang.jia@linux.alibaba.com, Shirong Hao Subject: [PATCH 2/3] KVM/SVM: move the implementation of sev_get_attestation_report to ccp driver Date: Mon, 10 Jan 2022 14:04:44 +0800 Message-Id: <20220110060445.549800-3-shirong@linux.alibaba.com> X-Mailer: git-send-email 2.27.0 In-Reply-To: <20220110060445.549800-1-shirong@linux.alibaba.com> References: <20220110060445.549800-1-shirong@linux.alibaba.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" The sev_get_attestation_report is called by qemu-kvm to get sev attestation report. However, getting the sev attestation report is a general function, the need for host program to get the sev attestation report by interacting with /dev/sev device exists. Considering this need, it is more reasonable to move the main implementation of sev_get_attestation_report into sev_do_get_report defined in the ccp driver. Any host program getting the guest firmware handle can directly interact with the sev device based on sev_do_get_report to get the sev attestation report. In addition, after moving the general code of sev_get_attestation_report to the ccp, move the check for sev fd in __sev_issue_cmd to the sev_get_attestation_report function is necessary. Signed-off-by: Shirong Hao --- arch/x86/kvm/svm/sev.c | 49 ++++--------------------------- drivers/crypto/ccp/sev-dev.c | 56 ++++++++++++++++++++++++++++++++++++ include/linux/psp-sev.h | 7 +++++ 3 files changed, 69 insertions(+), 43 deletions(-) diff --git a/arch/x86/kvm/svm/sev.c b/arch/x86/kvm/svm/sev.c index 7656a2c5662a..016acc96c5dc 100644 --- a/arch/x86/kvm/svm/sev.c +++ b/arch/x86/kvm/svm/sev.c @@ -1066,10 +1066,8 @@ static int sev_get_attestation_report(struct kvm *kv= m, struct kvm_sev_cmd *argp) { void __user *report =3D (void __user *)(uintptr_t)argp->data; struct kvm_sev_info *sev =3D &to_kvm_svm(kvm)->sev_info; - struct sev_data_attestation_report data; struct kvm_sev_attestation_report params; - void __user *p; - void *blob =3D NULL; + struct fd f; int ret; =20 if (!sev_guest(kvm)) @@ -1078,48 +1076,13 @@ static int sev_get_attestation_report(struct kvm *k= vm, struct kvm_sev_cmd *argp) if (copy_from_user(¶ms, (void __user *)(uintptr_t)argp->data, sizeof(= params))) return -EFAULT; =20 - memset(&data, 0, sizeof(data)); - - /* User wants to query the blob length */ - if (!params.len) - goto cmd; - - p =3D (void __user *)(uintptr_t)params.uaddr; - if (p) { - if (params.len > SEV_FW_BLOB_MAX_SIZE) - return -EINVAL; - - blob =3D kmalloc(params.len, GFP_KERNEL_ACCOUNT); - if (!blob) - return -ENOMEM; - - data.address =3D __psp_pa(blob); - data.len =3D params.len; - memcpy(data.mnonce, params.mnonce, sizeof(params.mnonce)); - } -cmd: - data.handle =3D sev->handle; - ret =3D sev_issue_cmd(kvm, SEV_CMD_ATTESTATION_REPORT, &data, &argp->erro= r); - /* - * If we query the session length, FW responded with expected data. - */ - if (!params.len) - goto done; - - if (ret) - goto e_free_blob; + f =3D fdget(sev->fd); + if (!f.file) + return -EBADF; =20 - if (blob) { - if (copy_to_user(p, blob, params.len)) - ret =3D -EFAULT; - } + ret =3D sev_do_get_report(report, ¶ms, f.file, sev->handle, &argp->er= ror); =20 -done: - params.len =3D data.len; - if (copy_to_user(report, ¶ms, sizeof(params))) - ret =3D -EFAULT; -e_free_blob: - kfree(blob); + fdput(f); return ret; } =20 diff --git a/drivers/crypto/ccp/sev-dev.c b/drivers/crypto/ccp/sev-dev.c index e09925d86bf3..2f6b81742d28 100644 --- a/drivers/crypto/ccp/sev-dev.c +++ b/drivers/crypto/ccp/sev-dev.c @@ -22,6 +22,7 @@ #include #include #include +#include =20 #include =20 @@ -384,6 +385,61 @@ static int sev_ioctl_do_platform_status(struct sev_iss= ue_cmd *argp) return ret; } =20 +int sev_do_get_report(void __user *report, struct kvm_sev_attestation_repo= rt *input, + struct file *filep, u32 handle, u32 *error) +{ + struct sev_data_attestation_report data; + void __user *p; + void *blob =3D NULL; + int ret; + + memset(&data, 0, sizeof(data)); + + /* User wants to query the blob length */ + if (!input->len) + goto cmd; + + p =3D (void __user *)(uintptr_t)input->uaddr; + if (p) { + if (input->len > SEV_FW_BLOB_MAX_SIZE) + return -EINVAL; + + blob =3D kmalloc(input->len, GFP_KERNEL_ACCOUNT); + if (!blob) + return -ENOMEM; + + data.address =3D __psp_pa(blob); + data.len =3D input->len; + memcpy(data.mnonce, input->mnonce, sizeof(input->mnonce)); + } +cmd: + data.handle =3D handle; + ret =3D sev_issue_cmd_external_user(filep, SEV_CMD_ATTESTATION_REPORT, &d= ata, error); + + /* + * If we query the session length, FW responded with expected data. + */ + if (!input->len) + goto done; + + if (ret) + goto e_free_blob; + + if (blob) { + if (copy_to_user(p, blob, input->len)) + ret =3D -EFAULT; + } + +done: + input->len =3D data.len; + if (copy_to_user(report, input, sizeof(*input))) + ret =3D -EFAULT; +e_free_blob: + kfree(blob); + return ret; +} +EXPORT_SYMBOL_GPL(sev_do_get_report); + static int sev_ioctl_do_pek_pdh_gen(int cmd, struct sev_issue_cmd *argp, b= ool writable) { struct sev_device *sev =3D psp_master->sev_data; diff --git a/include/linux/psp-sev.h b/include/linux/psp-sev.h index d48a7192e881..0cbf39a7d116 100644 --- a/include/linux/psp-sev.h +++ b/include/linux/psp-sev.h @@ -541,6 +541,9 @@ int sev_platform_init(int *error); */ int sev_platform_status(struct sev_user_data_status *status, int *error); =20 +int sev_do_get_report(void __user *report, struct kvm_sev_attestation_repo= rt *input, + struct file *filep, u32 handle, u32 *error); + /** * sev_issue_cmd_external_user - issue SEV command by other driver with a = file * handle. @@ -649,6 +652,10 @@ sev_issue_cmd_external_user(struct file *filep, unsign= ed int id, void *data, int =20 static inline void *psp_copy_user_blob(u64 __user uaddr, u32 len) { return= ERR_PTR(-EINVAL); } =20 +static inline int +sev_do_get_report(void __user *report, struct kvm_sev_attestation_report *= input, + struct file *filep, u32 handle, u32 *error) { return -ENODEV; } + #endif /* CONFIG_CRYPTO_DEV_SP_PSP */ =20 #endif /* __PSP_SEV_H__ */ --=20 2.27.0 From nobody Tue Jun 30 20:09:23 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id EC3F4C433EF for ; Mon, 10 Jan 2022 06:05:28 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S239000AbiAJGF1 (ORCPT ); Mon, 10 Jan 2022 01:05:27 -0500 Received: from out30-132.freemail.mail.aliyun.com ([115.124.30.132]:42379 "EHLO out30-132.freemail.mail.aliyun.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S238912AbiAJGFQ (ORCPT ); Mon, 10 Jan 2022 01:05:16 -0500 X-Alimail-AntiSpam: AC=PASS;BC=-1|-1;BR=01201311R171e4;CH=green;DM=||false|;DS=||;FP=0|-1|-1|-1|0|-1|-1|-1;HT=e01e04426;MF=shirong@linux.alibaba.com;NM=1;PH=DS;RN=25;SR=0;TI=SMTPD_---0V1LvBNz_1641794698; Received: from localhost.localdomain(mailfrom:shirong@linux.alibaba.com fp:SMTPD_---0V1LvBNz_1641794698) by smtp.aliyun-inc.com(127.0.0.1); Mon, 10 Jan 2022 14:05:11 +0800 From: Shirong Hao To: pbonzini@redhat.com, seanjc@google.com, vkuznets@redhat.com, wanpengli@tencent.com, jmattson@google.com, joro@8bytes.org, tglx@linutronix.de, mingo@redhat.co, bp@alien8.de, dave.hansen@linux.intel.com, x86@kernel.org, hpa@zytor.com, brijesh.singh@amd.com, thomas.lendacky@amd.com, john.allen@amd.com, herbert@gondor.apana.org.au, davem@davemloft.net, srutherford@google.com, ashish.kalra@amd.com, natet@google.com Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org, linux-crypto@vger.kernel.org, zhang.jia@linux.alibaba.com, Shirong Hao Subject: [PATCH 3/3] crypto: ccp: Implement SEV_GET_REPORT ioctl command Date: Mon, 10 Jan 2022 14:04:45 +0800 Message-Id: <20220110060445.549800-4-shirong@linux.alibaba.com> X-Mailer: git-send-email 2.27.0 In-Reply-To: <20220110060445.549800-1-shirong@linux.alibaba.com> References: <20220110060445.549800-1-shirong@linux.alibaba.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" The SEV_GET_REPORT command can be used by host service with guest firmware handle to query the attestation report. Signed-off-by: Shirong Hao --- drivers/crypto/ccp/sev-dev.c | 20 +++++++++++++++++++- include/uapi/linux/psp-sev.h | 17 +++++++++++++++++ 2 files changed, 36 insertions(+), 1 deletion(-) diff --git a/drivers/crypto/ccp/sev-dev.c b/drivers/crypto/ccp/sev-dev.c index 2f6b81742d28..2e479b88aa29 100644 --- a/drivers/crypto/ccp/sev-dev.c +++ b/drivers/crypto/ccp/sev-dev.c @@ -414,7 +414,10 @@ int sev_do_get_report(void __user *report, struct kvm_= sev_attestation_report *in } cmd: data.handle =3D handle; - ret =3D sev_issue_cmd_external_user(filep, SEV_CMD_ATTESTATION_REPORT, &d= ata, error); + if (!filep) + ret =3D __sev_do_cmd_locked(SEV_CMD_ATTESTATION_REPORT, &data, error); + else + ret =3D sev_issue_cmd_external_user(filep, SEV_CMD_ATTESTATION_REPORT, &= data, error); =20 /* * If we query the session length, FW responded with expected data. @@ -440,6 +443,18 @@ int sev_do_get_report(void __user *report, struct kvm_= sev_attestation_report *in } EXPORT_SYMBOL_GPL(sev_do_get_report); =20 +static int sev_ioctl_do_get_report(struct sev_issue_cmd *argp) +{ + void __user *report =3D (void __user *)(uintptr_t)argp->data; + struct sev_user_data_attestation_report input; + + if (copy_from_user(&input, (void __user *)argp->data, sizeof(input))) + return -EFAULT; + + return sev_do_get_report(report, (struct kvm_sev_attestation_report *)&in= put, + NULL, input.handle, &argp->error); +} + static int sev_ioctl_do_pek_pdh_gen(int cmd, struct sev_issue_cmd *argp, b= ool writable) { struct sev_device *sev =3D psp_master->sev_data; @@ -926,6 +941,9 @@ static long sev_ioctl(struct file *file, unsigned int i= octl, unsigned long arg) case SEV_GET_ID2: ret =3D sev_ioctl_do_get_id2(&input); break; + case SEV_GET_REPORT: + ret =3D sev_ioctl_do_get_report(&input); + break; default: ret =3D -EINVAL; goto out; diff --git a/include/uapi/linux/psp-sev.h b/include/uapi/linux/psp-sev.h index 91b4c63d5cbf..c7d70fc0ac1e 100644 --- a/include/uapi/linux/psp-sev.h +++ b/include/uapi/linux/psp-sev.h @@ -28,6 +28,7 @@ enum { SEV_PEK_CERT_IMPORT, SEV_GET_ID, /* This command is deprecated, use SEV_GET_ID2 */ SEV_GET_ID2, + SEV_GET_REPORT, =20 SEV_MAX, }; @@ -147,6 +148,22 @@ struct sev_user_data_get_id2 { __u32 length; /* In/Out */ } __packed; =20 +/** + * struct sev_user_data_attestation_report - ATTESTATION command parameters + * + * @mnonce: mnonce to compute HMAC + * @uaddr: physical address containing the attestation report + * @len: length of attestation report + * @handle: handle of the VM to process + */ + +struct sev_user_data_attestation_report { + __u8 mnonce[16]; /* In */ + __u64 uaddr; /* In */ + __u32 len; /* In/Out */ + __u32 handle; /* In */ +}; + /** * struct sev_issue_cmd - SEV ioctl parameters * --=20 2.27.0