From nobody Tue Sep 9 17:29:09 2025 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 1C16EC61DA3 for ; Tue, 21 Feb 2023 11:34:48 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234053AbjBULeq (ORCPT ); Tue, 21 Feb 2023 06:34:46 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:57978 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233605AbjBULek (ORCPT ); Tue, 21 Feb 2023 06:34:40 -0500 Received: from mail.skyhub.de (mail.skyhub.de [IPv6:2a01:4f8:190:11c2::b:1457]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 5C8B7233E9 for ; Tue, 21 Feb 2023 03:34:38 -0800 (PST) Received: from zn.tnic (p5de8e9fe.dip0.t-ipconnect.de [93.232.233.254]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.skyhub.de (SuperMail on ZX Spectrum 128k) with ESMTPSA id 517DA1EC064C; Tue, 21 Feb 2023 12:34:36 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=alien8.de; s=dkim; t=1676979276; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=xAFRY6UWQLF1FP7olpHuR0CBZw+9W4Mq3Tn1rtwpBJM=; b=qZK1Zd9ewhHGtj5llNFPOJ/ooJVzt5ZkJGOPofc6h/kDGBH04EZ6Tdc/ON7u7ANmGAOqEG a5SeFooneVntrBGVqiEA7LXRYimNcXgyt/Ulb4k1ftCFbeZf9BuMbtIvdFVOAfsz5DwUbW erynl47FPXz5h9Kadnn9/t7eqpvg/2w= From: Borislav Petkov To: LKML Cc: stable@kernel.org, Dionna Glaze , Joerg Roedel , Michael Roth , Nikunj A Dadhania , Peter Gonda , Tom Lendacky , linux-coco@lists.linux.dev, x86@kernel.org Subject: [PATCH -v2 01/11] crypto: ccp - Name -1 return value as SEV_RET_NO_FW_CALL Date: Tue, 21 Feb 2023 12:34:18 +0100 Message-Id: <20230221113428.19324-2-bp@alien8.de> X-Mailer: git-send-email 2.35.1 In-Reply-To: <20230221113428.19324-1-bp@alien8.de> References: <20230221113428.19324-1-bp@alien8.de> 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" From: Peter Gonda The PSP can return a "firmware error" code of -1 in circumstances where the PSP has not actually been called. To make this protocol unambiguous, name the value SEV_RET_NO_FW_CALL. [ bp: Massage a bit. ] Signed-off-by: Peter Gonda Signed-off-by: Dionna Glaze Signed-off-by: Borislav Petkov (AMD) Cc: Link: https://lore.kernel.org/r/20221207010210.2563293-2-dionnaglaze@google= .com --- Documentation/virt/coco/sev-guest.rst | 4 ++-- drivers/crypto/ccp/sev-dev.c | 8 +++++--- include/uapi/linux/psp-sev.h | 7 +++++++ 3 files changed, 14 insertions(+), 5 deletions(-) diff --git a/Documentation/virt/coco/sev-guest.rst b/Documentation/virt/coc= o/sev-guest.rst index bf593e88cfd9..aa3e4c6a1f90 100644 --- a/Documentation/virt/coco/sev-guest.rst +++ b/Documentation/virt/coco/sev-guest.rst @@ -40,8 +40,8 @@ along with a description: The guest ioctl should be issued on a file descriptor of the /dev/sev-gues= t device. The ioctl accepts struct snp_user_guest_request. The input and output stru= cture is specified through the req_data and resp_data field respectively. If the io= ctl fails -to execute due to a firmware error, then fw_err code will be set otherwise= the -fw_err will be set to 0x00000000000000ff. +to execute due to a firmware error, then fw_err code will be set. Otherwis= e, fw_err +will be set to 0x00000000ffffffff, i.e., the lower 32-bits are -1. =20 The firmware checks that the message sequence counter is one greater than the guests message sequence counter. If guest driver fails to increment me= ssage diff --git a/drivers/crypto/ccp/sev-dev.c b/drivers/crypto/ccp/sev-dev.c index 06fc7156c04f..f60bb73edfda 100644 --- a/drivers/crypto/ccp/sev-dev.c +++ b/drivers/crypto/ccp/sev-dev.c @@ -442,10 +442,10 @@ static int __sev_init_ex_locked(int *error) =20 static int __sev_platform_init_locked(int *error) { + int rc =3D 0, psp_ret =3D SEV_RET_NO_FW_CALL; struct psp_device *psp =3D psp_master; - struct sev_device *sev; - int rc =3D 0, psp_ret =3D -1; int (*init_function)(int *error); + struct sev_device *sev; =20 if (!psp || !psp->sev_data) return -ENODEV; @@ -473,9 +473,11 @@ static int __sev_platform_init_locked(int *error) * initialization function should succeed by replacing the state * with a reset state. */ - dev_err(sev->dev, "SEV: retrying INIT command because of SECURE_DATA_INV= ALID error. Retrying once to reset PSP SEV state."); + dev_err(sev->dev, +"SEV: retrying INIT command because of SECURE_DATA_INVALID error. Retrying= once to reset PSP SEV state."); rc =3D init_function(&psp_ret); } + if (error) *error =3D psp_ret; =20 diff --git a/include/uapi/linux/psp-sev.h b/include/uapi/linux/psp-sev.h index 91b4c63d5cbf..1c9da485318f 100644 --- a/include/uapi/linux/psp-sev.h +++ b/include/uapi/linux/psp-sev.h @@ -36,6 +36,13 @@ enum { * SEV Firmware status code */ typedef enum { + /* + * This error code is not in the SEV spec. Its purpose is to convey that + * there was an error that prevented the SEV firmware from being called. + * The SEV API error codes are 16 bits, so the -1 value will not overlap + * with possible values from the specification. + */ + SEV_RET_NO_FW_CALL =3D -1, SEV_RET_SUCCESS =3D 0, SEV_RET_INVALID_PLATFORM_STATE, SEV_RET_INVALID_GUEST_STATE, --=20 2.35.1 From nobody Tue Sep 9 17:29:09 2025 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 5B636C6379F for ; Tue, 21 Feb 2023 11:34:44 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233812AbjBULen (ORCPT ); Tue, 21 Feb 2023 06:34:43 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:57976 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233677AbjBULek (ORCPT ); Tue, 21 Feb 2023 06:34:40 -0500 Received: from mail.skyhub.de (mail.skyhub.de [5.9.137.197]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 5F844234CA for ; Tue, 21 Feb 2023 03:34:38 -0800 (PST) Received: from zn.tnic (p5de8e9fe.dip0.t-ipconnect.de [93.232.233.254]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.skyhub.de (SuperMail on ZX Spectrum 128k) with ESMTPSA id B7E561EC064D; Tue, 21 Feb 2023 12:34:36 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=alien8.de; s=dkim; t=1676979276; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=fmJP6KJzP645UaTSE0lRF5lUFM34wl43txCaH4JG/ds=; b=gr+Ft10dPKgV49IOliEOEH6OOeK1mkIT1loIM1M/fMuVqQ3tJZgbmQy7PpsirIEuEwMHOf JOXiXKY+LNCKKaxs6n5oY8RWbpub0Vft/lwwfzMbCv7vQU/PuvN6mX5I69/r/VZbAbYQWu T1bhTRA4CSVexbPlRIP74fjb9yyI29k= From: Borislav Petkov To: LKML Cc: Dionna Glaze , Joerg Roedel , Michael Roth , Nikunj A Dadhania , Peter Gonda , Tom Lendacky , linux-coco@lists.linux.dev, x86@kernel.org Subject: [PATCH -v2 02/11] virt/coco/sev-guest: Check SEV_SNP attribute at probe time Date: Tue, 21 Feb 2023 12:34:19 +0100 Message-Id: <20230221113428.19324-3-bp@alien8.de> X-Mailer: git-send-email 2.35.1 In-Reply-To: <20230221113428.19324-1-bp@alien8.de> References: <20230221113428.19324-1-bp@alien8.de> 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" From: "Borislav Petkov (AMD)" No need to check it on every ioctl. And yes, this is a common SEV driver but it does only SNP-specific operations currently. This can be revisited later, when more use cases appear. No functional changes. Signed-off-by: Borislav Petkov (AMD) --- arch/x86/kernel/sev.c | 3 --- drivers/virt/coco/sev-guest/sev-guest.c | 3 +++ 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/arch/x86/kernel/sev.c b/arch/x86/kernel/sev.c index 679026a640ef..c644c34372e8 100644 --- a/arch/x86/kernel/sev.c +++ b/arch/x86/kernel/sev.c @@ -2183,9 +2183,6 @@ int snp_issue_guest_request(u64 exit_code, struct snp= _req_data *input, unsigned struct ghcb *ghcb; int ret; =20 - if (!cc_platform_has(CC_ATTR_GUEST_SEV_SNP)) - return -ENODEV; - if (!fw_err) return -EINVAL; =20 diff --git a/drivers/virt/coco/sev-guest/sev-guest.c b/drivers/virt/coco/se= v-guest/sev-guest.c index 4ec4174e05a3..edaf6031c6d9 100644 --- a/drivers/virt/coco/sev-guest/sev-guest.c +++ b/drivers/virt/coco/sev-guest/sev-guest.c @@ -689,6 +689,9 @@ static int __init sev_guest_probe(struct platform_devic= e *pdev) void __iomem *mapping; int ret; =20 + if (!cc_platform_has(CC_ATTR_GUEST_SEV_SNP)) + return -ENODEV; + if (!dev->platform_data) return -ENODEV; =20 --=20 2.35.1 From nobody Tue Sep 9 17:29:09 2025 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 4AFD7C61DA3 for ; Tue, 21 Feb 2023 11:35:04 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234094AbjBULet (ORCPT ); Tue, 21 Feb 2023 06:34:49 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58010 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233706AbjBULek (ORCPT ); Tue, 21 Feb 2023 06:34:40 -0500 Received: from mail.skyhub.de (mail.skyhub.de [IPv6:2a01:4f8:190:11c2::b:1457]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D994B234DB for ; Tue, 21 Feb 2023 03:34:38 -0800 (PST) Received: from zn.tnic (p5de8e9fe.dip0.t-ipconnect.de [93.232.233.254]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.skyhub.de (SuperMail on ZX Spectrum 128k) with ESMTPSA id 33AC71EC064E; Tue, 21 Feb 2023 12:34:37 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=alien8.de; s=dkim; t=1676979277; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=ksRalnveoGheILhfPrdSCwL5dhzTJiCmiR2fo+V5RHY=; b=E1ZqyQ7CvzM2ZM9uGSEnSE2bhwBpOF0WxTjYdYzoBZwvQswxl15MXwB8jTAPLKGM7YCLuH kfkXc+VhCOVOy6vK5YMAy7lXUi6YsF3PTIY3mD5wxp1RYM9044VpbLXBGH2hZuxLdM5ciy xWoA405WdEpn6QB/PdUd8bzyJOaOjoY= From: Borislav Petkov To: LKML Cc: Dionna Glaze , Joerg Roedel , Michael Roth , Nikunj A Dadhania , Peter Gonda , Tom Lendacky , linux-coco@lists.linux.dev, x86@kernel.org Subject: [PATCH -v2 03/11] virt/coco/sev-guest: Simplify extended guest request handling Date: Tue, 21 Feb 2023 12:34:20 +0100 Message-Id: <20230221113428.19324-4-bp@alien8.de> X-Mailer: git-send-email 2.35.1 In-Reply-To: <20230221113428.19324-1-bp@alien8.de> References: <20230221113428.19324-1-bp@alien8.de> 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" From: "Borislav Petkov (AMD)" Return a specific error code - -ENOSPC - to signal the too small cert data buffer instead of checking exit code and exitinfo2. While at it, hoist the *fw_err assignment in snp_issue_guest_request() so that a proper error value is returned to the callers. Signed-off-by: Borislav Petkov (AMD) --- arch/x86/kernel/sev.c | 11 +++--- drivers/virt/coco/sev-guest/sev-guest.c | 46 ++++++++++++++----------- 2 files changed, 31 insertions(+), 26 deletions(-) diff --git a/arch/x86/kernel/sev.c b/arch/x86/kernel/sev.c index c644c34372e8..6a3e1425ba17 100644 --- a/arch/x86/kernel/sev.c +++ b/arch/x86/kernel/sev.c @@ -2209,15 +2209,16 @@ int snp_issue_guest_request(u64 exit_code, struct s= np_req_data *input, unsigned if (ret) goto e_put; =20 + *fw_err =3D ghcb->save.sw_exit_info_2; if (ghcb->save.sw_exit_info_2) { /* Number of expected pages are returned in RBX */ if (exit_code =3D=3D SVM_VMGEXIT_EXT_GUEST_REQUEST && - ghcb->save.sw_exit_info_2 =3D=3D SNP_GUEST_REQ_INVALID_LEN) + ghcb->save.sw_exit_info_2 =3D=3D SNP_GUEST_REQ_INVALID_LEN) { input->data_npages =3D ghcb_get_rbx(ghcb); - - *fw_err =3D ghcb->save.sw_exit_info_2; - - ret =3D -EIO; + ret =3D -ENOSPC; + } else { + ret =3D -EIO; + } } =20 e_put: diff --git a/drivers/virt/coco/sev-guest/sev-guest.c b/drivers/virt/coco/se= v-guest/sev-guest.c index edaf6031c6d9..5b4cddf44a3a 100644 --- a/drivers/virt/coco/sev-guest/sev-guest.c +++ b/drivers/virt/coco/sev-guest/sev-guest.c @@ -322,7 +322,8 @@ static int handle_guest_request(struct snp_guest_dev *s= np_dev, u64 exit_code, in u8 type, void *req_buf, size_t req_sz, void *resp_buf, u32 resp_sz, __u64 *fw_err) { - unsigned long err; + unsigned long err, override_err =3D 0; + unsigned int override_npages =3D 0; u64 seqno; int rc; =20 @@ -338,6 +339,7 @@ static int handle_guest_request(struct snp_guest_dev *s= np_dev, u64 exit_code, in if (rc) return rc; =20 +retry_request: /* * Call firmware to process the request. In this function the encrypted * message enters shared memory with the host. So after this call the @@ -346,17 +348,24 @@ static int handle_guest_request(struct snp_guest_dev = *snp_dev, u64 exit_code, in */ rc =3D snp_issue_guest_request(exit_code, &snp_dev->input, &err); =20 - /* - * If the extended guest request fails due to having too small of a - * certificate data buffer, retry the same guest request without the - * extended data request in order to increment the sequence number - * and thus avoid IV reuse. - */ - if (exit_code =3D=3D SVM_VMGEXIT_EXT_GUEST_REQUEST && - err =3D=3D SNP_GUEST_REQ_INVALID_LEN) { - const unsigned int certs_npages =3D snp_dev->input.data_npages; + switch (rc) { + case -ENOSPC: + /* + * If the extended guest request fails due to having too + * small of a certificate data buffer, retry the same + * guest request without the extended data request in + * order to increment the sequence number and thus avoid + * IV reuse. + */ + override_npages =3D snp_dev->input.data_npages; + exit_code =3D SVM_VMGEXIT_GUEST_REQUEST; =20 - exit_code =3D SVM_VMGEXIT_GUEST_REQUEST; + /* + * Override the error to inform callers the given extended + * request buffer size was too small and give the caller the + * required buffer size. + */ + override_err =3D SNP_GUEST_REQ_INVALID_LEN; =20 /* * If this call to the firmware succeeds, the sequence number can @@ -366,19 +375,14 @@ static int handle_guest_request(struct snp_guest_dev = *snp_dev, u64 exit_code, in * of the VMPCK and the error code being propagated back to the * user as an ioctl() return code. */ - rc =3D snp_issue_guest_request(exit_code, &snp_dev->input, &err); - - /* - * Override the error to inform callers the given extended - * request buffer size was too small and give the caller the - * required buffer size. - */ - err =3D SNP_GUEST_REQ_INVALID_LEN; - snp_dev->input.data_npages =3D certs_npages; + goto retry_request; } =20 if (fw_err) - *fw_err =3D err; + *fw_err =3D override_err ?: err; + + if (override_npages) + snp_dev->input.data_npages =3D override_npages; =20 if (rc) { dev_alert(snp_dev->dev, --=20 2.35.1 From nobody Tue Sep 9 17:29:09 2025 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 03982C6379F for ; Tue, 21 Feb 2023 11:35:05 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234200AbjBULex (ORCPT ); Tue, 21 Feb 2023 06:34:53 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58028 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233151AbjBULel (ORCPT ); Tue, 21 Feb 2023 06:34:41 -0500 Received: from mail.skyhub.de (mail.skyhub.de [5.9.137.197]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 7249F234E0 for ; Tue, 21 Feb 2023 03:34:39 -0800 (PST) Received: from zn.tnic (p5de8e9fe.dip0.t-ipconnect.de [93.232.233.254]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.skyhub.de (SuperMail on ZX Spectrum 128k) with ESMTPSA id C8C8F1EC0662; Tue, 21 Feb 2023 12:34:37 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=alien8.de; s=dkim; t=1676979277; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=eIxV1gF+0xxMeTGWWux4B6qDSXGFkU3ZGwakcGga7O0=; b=ZpmskPJims9Hyc9dnmgeiYuGxje5mn0CO+IORb6lkVE++tmyaqBr+fekPt4jmfGXeUcGPu zLDVvfAhZZ/TR3k89W4U7PKjtyjOQMfQy8+ZXxp7Tc6VPmKeOOpLJxJxpWIvy4p3ibOT1K acb4yQU4Ykknp8PzRcHsdMYUx1ARNL8= From: Borislav Petkov To: LKML Cc: Dionna Glaze , Joerg Roedel , Michael Roth , Nikunj A Dadhania , Peter Gonda , Tom Lendacky , linux-coco@lists.linux.dev, x86@kernel.org Subject: [PATCH -v2 04/11] virt/coco/sev-guest: Remove the disable_vmpck label in handle_guest_request() Date: Tue, 21 Feb 2023 12:34:21 +0100 Message-Id: <20230221113428.19324-5-bp@alien8.de> X-Mailer: git-send-email 2.35.1 In-Reply-To: <20230221113428.19324-1-bp@alien8.de> References: <20230221113428.19324-1-bp@alien8.de> 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" From: "Borislav Petkov (AMD)" Call the function directly instead. No functional changes. Signed-off-by: Borislav Petkov (AMD) --- drivers/virt/coco/sev-guest/sev-guest.c | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/drivers/virt/coco/sev-guest/sev-guest.c b/drivers/virt/coco/se= v-guest/sev-guest.c index 5b4cddf44a3a..c0ecc5885573 100644 --- a/drivers/virt/coco/sev-guest/sev-guest.c +++ b/drivers/virt/coco/sev-guest/sev-guest.c @@ -388,7 +388,8 @@ static int handle_guest_request(struct snp_guest_dev *s= np_dev, u64 exit_code, in dev_alert(snp_dev->dev, "Detected error from ASP request. rc: %d, fw_err: %llu\n", rc, *fw_err); - goto disable_vmpck; + snp_disable_vmpck(snp_dev); + return rc; } =20 rc =3D verify_and_dec_payload(snp_dev, resp_buf, resp_sz); @@ -396,17 +397,14 @@ static int handle_guest_request(struct snp_guest_dev = *snp_dev, u64 exit_code, in dev_alert(snp_dev->dev, "Detected unexpected decode failure from ASP. rc: %d\n", rc); - goto disable_vmpck; + snp_disable_vmpck(snp_dev); + return rc; } =20 /* Increment to new message sequence after payload decryption was success= ful. */ snp_inc_msg_seqno(snp_dev); =20 return 0; - -disable_vmpck: - snp_disable_vmpck(snp_dev); - return rc; } =20 static int get_report(struct snp_guest_dev *snp_dev, struct snp_guest_requ= est_ioctl *arg) --=20 2.35.1 From nobody Tue Sep 9 17:29:09 2025 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 238B3C64ED6 for ; Tue, 21 Feb 2023 11:35:05 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234223AbjBULe5 (ORCPT ); Tue, 21 Feb 2023 06:34:57 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58010 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233751AbjBULel (ORCPT ); Tue, 21 Feb 2023 06:34:41 -0500 Received: from mail.skyhub.de (mail.skyhub.de [IPv6:2a01:4f8:190:11c2::b:1457]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 0010123858 for ; Tue, 21 Feb 2023 03:34:39 -0800 (PST) Received: from zn.tnic (p5de8e9fe.dip0.t-ipconnect.de [93.232.233.254]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.skyhub.de (SuperMail on ZX Spectrum 128k) with ESMTPSA id 64B5F1EC0666; Tue, 21 Feb 2023 12:34:38 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=alien8.de; s=dkim; t=1676979278; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=cwRPhPVa7VbVWxOqB+Oos/acf4MTp3gp6dUcgjiwjrA=; b=beu/SnEpU6k01R9hmAUwPqllKFAUqkW0jJEU87dx7kdHVXy5avM1YPz+3XpcsqPnxeANif Dl6+r9bAuxzk5CT4P1jlE4fqMMb8ewBai1pI4bvAcdGfwFJ8yk6p8Ekc8sPpalw33WLysY lUGkjCzK9EKp7bu6FQ0Z9kkypY2bggQ= From: Borislav Petkov To: LKML Cc: Dionna Glaze , Joerg Roedel , Michael Roth , Nikunj A Dadhania , Peter Gonda , Tom Lendacky , linux-coco@lists.linux.dev, x86@kernel.org Subject: [PATCH -v2 05/11] virt/coco/sev-guest: Carve out the request issuing logic into a helper Date: Tue, 21 Feb 2023 12:34:22 +0100 Message-Id: <20230221113428.19324-6-bp@alien8.de> X-Mailer: git-send-email 2.35.1 In-Reply-To: <20230221113428.19324-1-bp@alien8.de> References: <20230221113428.19324-1-bp@alien8.de> 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" From: "Borislav Petkov (AMD)" This makes the code flow a lot easier to follow. No functional changes. Signed-off-by: Borislav Petkov (AMD) --- drivers/virt/coco/sev-guest/sev-guest.c | 41 +++++++++++++++---------- 1 file changed, 24 insertions(+), 17 deletions(-) diff --git a/drivers/virt/coco/sev-guest/sev-guest.c b/drivers/virt/coco/se= v-guest/sev-guest.c index c0ecc5885573..e72289de2b28 100644 --- a/drivers/virt/coco/sev-guest/sev-guest.c +++ b/drivers/virt/coco/sev-guest/sev-guest.c @@ -318,27 +318,12 @@ static int enc_payload(struct snp_guest_dev *snp_dev,= u64 seqno, int version, u8 return __enc_payload(snp_dev, req, payload, sz); } =20 -static int handle_guest_request(struct snp_guest_dev *snp_dev, u64 exit_co= de, int msg_ver, - u8 type, void *req_buf, size_t req_sz, void *resp_buf, - u32 resp_sz, __u64 *fw_err) +static int __handle_guest_request(struct snp_guest_dev *snp_dev, u64 exit_= code, __u64 *fw_err) { unsigned long err, override_err =3D 0; unsigned int override_npages =3D 0; - u64 seqno; int rc; =20 - /* Get message sequence and verify that its a non-zero */ - seqno =3D snp_get_msg_seqno(snp_dev); - if (!seqno) - return -EIO; - - memset(snp_dev->response, 0, sizeof(struct snp_guest_msg)); - - /* Encrypt the userspace provided payload */ - rc =3D enc_payload(snp_dev, seqno, msg_ver, type, req_buf, req_sz); - if (rc) - return rc; - retry_request: /* * Call firmware to process the request. In this function the encrypted @@ -347,7 +332,6 @@ static int handle_guest_request(struct snp_guest_dev *s= np_dev, u64 exit_code, in * prevent reuse of the IV. */ rc =3D snp_issue_guest_request(exit_code, &snp_dev->input, &err); - switch (rc) { case -ENOSPC: /* @@ -384,6 +368,29 @@ static int handle_guest_request(struct snp_guest_dev *= snp_dev, u64 exit_code, in if (override_npages) snp_dev->input.data_npages =3D override_npages; =20 + return rc; +} + +static int handle_guest_request(struct snp_guest_dev *snp_dev, u64 exit_co= de, int msg_ver, + u8 type, void *req_buf, size_t req_sz, void *resp_buf, + u32 resp_sz, __u64 *fw_err) +{ + u64 seqno; + int rc; + + /* Get message sequence and verify that its a non-zero */ + seqno =3D snp_get_msg_seqno(snp_dev); + if (!seqno) + return -EIO; + + memset(snp_dev->response, 0, sizeof(struct snp_guest_msg)); + + /* Encrypt the userspace provided payload */ + rc =3D enc_payload(snp_dev, seqno, msg_ver, type, req_buf, req_sz); + if (rc) + return rc; + + rc =3D __handle_guest_request(snp_dev, exit_code, fw_err); if (rc) { dev_alert(snp_dev->dev, "Detected error from ASP request. rc: %d, fw_err: %llu\n", --=20 2.35.1 From nobody Tue Sep 9 17:29:09 2025 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 3280FC64EC4 for ; Tue, 21 Feb 2023 11:35:05 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234249AbjBULfC (ORCPT ); Tue, 21 Feb 2023 06:35:02 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58064 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233777AbjBULem (ORCPT ); Tue, 21 Feb 2023 06:34:42 -0500 Received: from mail.skyhub.de (mail.skyhub.de [5.9.137.197]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 6CBE523840 for ; Tue, 21 Feb 2023 03:34:40 -0800 (PST) Received: from zn.tnic (p5de8e9fe.dip0.t-ipconnect.de [93.232.233.254]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.skyhub.de (SuperMail on ZX Spectrum 128k) with ESMTPSA id F3B2E1EC0674; Tue, 21 Feb 2023 12:34:38 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=alien8.de; s=dkim; t=1676979279; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=9ZqBaTyUq+/8HiR92mwl2yPYyLN2fWif2NArmQh+IzA=; b=IzN/qDVrRHW2mngsE32WaSNfsn2pPg52BPW85dqrC2L2pviR1URMIq/88KYpwdXfST6D2E gQIx4wbc+zd31yLMgJfKKJej618u0FO9TVjaMWUNuUo4NK2J9G8SA/iPDXVRE9OyP8QjLd OZ150zmTo4MnzgrOXeUA5lrUSagDdyc= From: Borislav Petkov To: LKML Cc: Dionna Glaze , Joerg Roedel , Michael Roth , Nikunj A Dadhania , Peter Gonda , Tom Lendacky , linux-coco@lists.linux.dev, x86@kernel.org Subject: [PATCH -v2 06/11] virt/coco/sev-guest: Do some code style cleanups Date: Tue, 21 Feb 2023 12:34:23 +0100 Message-Id: <20230221113428.19324-7-bp@alien8.de> X-Mailer: git-send-email 2.35.1 In-Reply-To: <20230221113428.19324-1-bp@alien8.de> References: <20230221113428.19324-1-bp@alien8.de> 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" From: "Borislav Petkov (AMD)" Remove unnecessary linebreaks, make the code more compact. No functional changes. Signed-off-by: Borislav Petkov (AMD) --- drivers/virt/coco/sev-guest/sev-guest.c | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/drivers/virt/coco/sev-guest/sev-guest.c b/drivers/virt/coco/se= v-guest/sev-guest.c index e72289de2b28..d430e2be2a22 100644 --- a/drivers/virt/coco/sev-guest/sev-guest.c +++ b/drivers/virt/coco/sev-guest/sev-guest.c @@ -392,18 +392,14 @@ static int handle_guest_request(struct snp_guest_dev = *snp_dev, u64 exit_code, in =20 rc =3D __handle_guest_request(snp_dev, exit_code, fw_err); if (rc) { - dev_alert(snp_dev->dev, - "Detected error from ASP request. rc: %d, fw_err: %llu\n", - rc, *fw_err); + dev_alert(snp_dev->dev, "Detected error from ASP request. rc: %d, fw_err= : %llu\n", rc, *fw_err); snp_disable_vmpck(snp_dev); return rc; } =20 rc =3D verify_and_dec_payload(snp_dev, resp_buf, resp_sz); if (rc) { - dev_alert(snp_dev->dev, - "Detected unexpected decode failure from ASP. rc: %d\n", - rc); + dev_alert(snp_dev->dev, "Detected unexpected decode failure from ASP. rc= : %d\n", rc); snp_disable_vmpck(snp_dev); return rc; } --=20 2.35.1 From nobody Tue Sep 9 17:29:09 2025 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 4FC86C61DA3 for ; Tue, 21 Feb 2023 11:35:10 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233533AbjBULfI (ORCPT ); Tue, 21 Feb 2023 06:35:08 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58142 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233826AbjBULeo (ORCPT ); Tue, 21 Feb 2023 06:34:44 -0500 Received: from mail.skyhub.de (mail.skyhub.de [IPv6:2a01:4f8:190:11c2::b:1457]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 34D142594D for ; Tue, 21 Feb 2023 03:34:41 -0800 (PST) Received: from zn.tnic (p5de8e9fe.dip0.t-ipconnect.de [93.232.233.254]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.skyhub.de (SuperMail on ZX Spectrum 128k) with ESMTPSA id 622981EC067C; Tue, 21 Feb 2023 12:34:39 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=alien8.de; s=dkim; t=1676979279; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=KR4vVsJESSCg86kIVXeeVz75YYrz3rzwdB6btBRiLEY=; b=K1QiusbpWj/Qs2KDQUM2zv48odDuqatgdvqb+53IWyQa4LC5CALTvIvubvy2eo9E9G6rZU eYWVG4ZIm6Y+K0DYbPI2ORGO2krxBpnOV/ypXVV1Cpzzi5yWN7xVeMBMQX2YEcK8r9NLO/ 2vFZEvidXnah5e+KwhGGTDoWCQkvolY= From: Borislav Petkov To: LKML Cc: Dionna Glaze , Joerg Roedel , Michael Roth , Nikunj A Dadhania , Peter Gonda , Tom Lendacky , linux-coco@lists.linux.dev, x86@kernel.org Subject: [PATCH -v2 07/11] virt/coco/sev-guest: Convert the sw_exit_info_2 checking to a switch-case Date: Tue, 21 Feb 2023 12:34:24 +0100 Message-Id: <20230221113428.19324-8-bp@alien8.de> X-Mailer: git-send-email 2.35.1 In-Reply-To: <20230221113428.19324-1-bp@alien8.de> References: <20230221113428.19324-1-bp@alien8.de> 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" From: "Borislav Petkov (AMD)" snp_issue_guest_request() checks the value returned by the hypervisor in sw_exit_info_2 and returns a different error depending on it. Convert those checks into a switch-case to make it more readable when more error values are going to be checked in the future. No functional changes. Signed-off-by: Borislav Petkov (AMD) --- arch/x86/kernel/sev.c | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/arch/x86/kernel/sev.c b/arch/x86/kernel/sev.c index 6a3e1425ba17..d67884fb38c1 100644 --- a/arch/x86/kernel/sev.c +++ b/arch/x86/kernel/sev.c @@ -2210,15 +2210,21 @@ int snp_issue_guest_request(u64 exit_code, struct s= np_req_data *input, unsigned goto e_put; =20 *fw_err =3D ghcb->save.sw_exit_info_2; - if (ghcb->save.sw_exit_info_2) { + switch (*fw_err) { + case 0: + break; + + case SNP_GUEST_REQ_INVALID_LEN: /* Number of expected pages are returned in RBX */ - if (exit_code =3D=3D SVM_VMGEXIT_EXT_GUEST_REQUEST && - ghcb->save.sw_exit_info_2 =3D=3D SNP_GUEST_REQ_INVALID_LEN) { + if (exit_code =3D=3D SVM_VMGEXIT_EXT_GUEST_REQUEST) { input->data_npages =3D ghcb_get_rbx(ghcb); ret =3D -ENOSPC; - } else { - ret =3D -EIO; + break; } + fallthrough; + default: + ret =3D -EIO; + break; } =20 e_put: --=20 2.35.1 From nobody Tue Sep 9 17:29:09 2025 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 3BEA1C64ED9 for ; Tue, 21 Feb 2023 11:35:07 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233151AbjBULfF (ORCPT ); Tue, 21 Feb 2023 06:35:05 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58092 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233794AbjBULem (ORCPT ); Tue, 21 Feb 2023 06:34:42 -0500 Received: from mail.skyhub.de (mail.skyhub.de [5.9.137.197]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 3781925B93 for ; Tue, 21 Feb 2023 03:34:41 -0800 (PST) Received: from zn.tnic (p5de8e9fe.dip0.t-ipconnect.de [93.232.233.254]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.skyhub.de (SuperMail on ZX Spectrum 128k) with ESMTPSA id D64C31EC067E; Tue, 21 Feb 2023 12:34:39 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=alien8.de; s=dkim; t=1676979279; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=IYyR0+BccWAZu5naODrqJjalGeVpYFKs8LsqRBbFls0=; b=V/S64JpQrPEnqV250KVXVQRNMRPqMIqPonbf6qQxcaH8JO7DLiKIsClOy29TDN/QmOOIox dA5CXj+2i+Q0g6if//P0LcCDz/y2m//YkAP2uGlgN+Kyk+FSgtHWjuib8dSB7QRuTqiPYU oyRua31voVHEt79/UQyFo1pyY4gVakE= From: Borislav Petkov To: LKML Cc: Dionna Glaze , Joerg Roedel , Michael Roth , Nikunj A Dadhania , Peter Gonda , Tom Lendacky , linux-coco@lists.linux.dev, x86@kernel.org Subject: [PATCH -v2 08/11] crypto: ccp: Get rid of __sev_platform_init_locked()'s local function pointer Date: Tue, 21 Feb 2023 12:34:25 +0100 Message-Id: <20230221113428.19324-9-bp@alien8.de> X-Mailer: git-send-email 2.35.1 In-Reply-To: <20230221113428.19324-1-bp@alien8.de> References: <20230221113428.19324-1-bp@alien8.de> 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" From: "Borislav Petkov (AMD)" Add a wrapper instead. No functional changes. Signed-off-by: Borislav Petkov (AMD) --- drivers/crypto/ccp/sev-dev.c | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/drivers/crypto/ccp/sev-dev.c b/drivers/crypto/ccp/sev-dev.c index f60bb73edfda..c54cc8f9a284 100644 --- a/drivers/crypto/ccp/sev-dev.c +++ b/drivers/crypto/ccp/sev-dev.c @@ -440,11 +440,18 @@ static int __sev_init_ex_locked(int *error) return __sev_do_cmd_locked(SEV_CMD_INIT_EX, &data, error); } =20 +static inline int __sev_do_init_locked(int *psp_ret) +{ + if (sev_init_ex_buffer) + return __sev_init_ex_locked(psp_ret); + else + return __sev_init_locked(psp_ret); +} + static int __sev_platform_init_locked(int *error) { int rc =3D 0, psp_ret =3D SEV_RET_NO_FW_CALL; struct psp_device *psp =3D psp_master; - int (*init_function)(int *error); struct sev_device *sev; =20 if (!psp || !psp->sev_data) @@ -456,15 +463,12 @@ static int __sev_platform_init_locked(int *error) return 0; =20 if (sev_init_ex_buffer) { - init_function =3D __sev_init_ex_locked; rc =3D sev_read_init_ex_file(); if (rc) return rc; - } else { - init_function =3D __sev_init_locked; } =20 - rc =3D init_function(&psp_ret); + rc =3D __sev_do_init_locked(&psp_ret); if (rc && psp_ret =3D=3D SEV_RET_SECURE_DATA_INVALID) { /* * Initialization command returned an integrity check failure @@ -475,7 +479,7 @@ static int __sev_platform_init_locked(int *error) */ dev_err(sev->dev, "SEV: retrying INIT command because of SECURE_DATA_INVALID error. Retrying= once to reset PSP SEV state."); - rc =3D init_function(&psp_ret); + rc =3D __sev_do_init_locked(&psp_ret); } =20 if (error) --=20 2.35.1 From nobody Tue Sep 9 17:29:09 2025 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 3FA93C64EC4 for ; Tue, 21 Feb 2023 11:35:12 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234059AbjBULfK (ORCPT ); Tue, 21 Feb 2023 06:35:10 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58152 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233830AbjBULeo (ORCPT ); Tue, 21 Feb 2023 06:34:44 -0500 Received: from mail.skyhub.de (mail.skyhub.de [5.9.137.197]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 17A012686F for ; Tue, 21 Feb 2023 03:34:41 -0800 (PST) Received: from zn.tnic (p5de8e9fe.dip0.t-ipconnect.de [93.232.233.254]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.skyhub.de (SuperMail on ZX Spectrum 128k) with ESMTPSA id 5D0451EC0681; Tue, 21 Feb 2023 12:34:40 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=alien8.de; s=dkim; t=1676979280; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=4IxoB32WLSIy6eV1NxkSejZghgVhKqORRCJKoMxnv/Y=; b=EQmnfjWnpopJUzOwY9r1pP0A3wFv+y11v8GCohH+bsoxt5SLtvGLAT/k7wXTEH4xhgILVO Vm/Z7MhxgfkbtpYoIM0X4Gh09S83Tja++c1nPixw+JcCERtjmKFiWdjJI+TiinWv2dQjBF xHVpVl80ErSEKJFyVCHTVHK9zRCeoVo= From: Borislav Petkov To: LKML Cc: Borislav Petkov , Dionna Glaze , Joerg Roedel , Michael Roth , Nikunj A Dadhania , Peter Gonda , Tom Lendacky , linux-coco@lists.linux.dev, x86@kernel.org Subject: [PATCH -v2 09/11] virt/coco/sev-guest: Add throttling awareness Date: Tue, 21 Feb 2023 12:34:26 +0100 Message-Id: <20230221113428.19324-10-bp@alien8.de> X-Mailer: git-send-email 2.35.1 In-Reply-To: <20230221113428.19324-1-bp@alien8.de> References: <20230221113428.19324-1-bp@alien8.de> 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" From: Dionna Glaze A potentially malicious SEV guest can constantly hammer the hypervisor using this driver to send down requests and thus prevent or at least considerably hinder other guests from issuing requests to the secure processor which is a shared platform resource. Therefore, the host is permitted and encouraged to throttle such guest requests. Add the capability to handle the case when the hypervisor throttles excessive numbers of requests issued by the guest. Otherwise, the VM platform communication key will be disabled, preventing the guest from attesting itself. Realistically speaking, a well-behaved guest should not even care about throttling. During its lifetime, it would end up issuing a handful of requests which the hardware can easily handle. This is more to address the case of a malicious guest. Such guest should get throttled and if its VMPCK gets disabled, then that's its own wrongdoing and perhaps that guest even deserves it. To the implementation: the hypervisor signals with SNP_GUEST_REQ_ERR_BUSY that the guest requests should be throttled. That error code is returned in the upper 32-bit half of exitinfo2 and this is part of the GHCB spec v2. So the guest is given a throttling period of 1 minute in which it retries the request every 2 seconds. This is a good default but if it turns out to not pan out in practice, it can be tweaked later. For safety, since the encryption algorithm in GHCBv2 is AES_GCM, control must remain in the kernel to complete the request with the current sequence number. Returning without finishing the request allows the guest to make another request but with different message contents. This is IV reuse, and breaks cryptographic protections. [ bp: Rewrite commit message and do a simplified version. ] Fixes: d5af44dde546 ("x86/sev: Provide support for SNP guest request NAEs") Signed-off-by: Dionna Glaze Co-developed-by: Borislav Petkov (AMD) Signed-off-by: Borislav Petkov (AMD) Link: https://lore.kernel.org/r/20230214164638.1189804-2-dionnaglaze@google= .com --- arch/x86/include/asm/sev-common.h | 3 ++- arch/x86/kernel/sev.c | 4 ++++ drivers/virt/coco/sev-guest/sev-guest.c | 19 ++++++++++++++++++- 3 files changed, 24 insertions(+), 2 deletions(-) diff --git a/arch/x86/include/asm/sev-common.h b/arch/x86/include/asm/sev-c= ommon.h index b8357d6ecd47..b63be696b776 100644 --- a/arch/x86/include/asm/sev-common.h +++ b/arch/x86/include/asm/sev-common.h @@ -128,8 +128,9 @@ struct snp_psc_desc { struct psc_entry entries[VMGEXIT_PSC_MAX_ENTRY]; } __packed; =20 -/* Guest message request error code */ +/* Guest message request error codes */ #define SNP_GUEST_REQ_INVALID_LEN BIT_ULL(32) +#define SNP_GUEST_REQ_ERR_BUSY BIT_ULL(33) =20 #define GHCB_MSR_TERM_REQ 0x100 #define GHCB_MSR_TERM_REASON_SET_POS 12 diff --git a/arch/x86/kernel/sev.c b/arch/x86/kernel/sev.c index d67884fb38c1..3f664ab277c4 100644 --- a/arch/x86/kernel/sev.c +++ b/arch/x86/kernel/sev.c @@ -2214,6 +2214,10 @@ int snp_issue_guest_request(u64 exit_code, struct sn= p_req_data *input, unsigned case 0: break; =20 + case SNP_GUEST_REQ_ERR_BUSY: + ret =3D -EAGAIN; + break; + case SNP_GUEST_REQ_INVALID_LEN: /* Number of expected pages are returned in RBX */ if (exit_code =3D=3D SVM_VMGEXIT_EXT_GUEST_REQUEST) { diff --git a/drivers/virt/coco/sev-guest/sev-guest.c b/drivers/virt/coco/se= v-guest/sev-guest.c index d430e2be2a22..da4f6267baad 100644 --- a/drivers/virt/coco/sev-guest/sev-guest.c +++ b/drivers/virt/coco/sev-guest/sev-guest.c @@ -31,6 +31,9 @@ #define AAD_LEN 48 #define MSG_HDR_VER 1 =20 +#define SNP_REQ_MAX_RETRY_DURATION (60*HZ) +#define SNP_REQ_RETRY_DELAY (2*HZ) + struct snp_guest_crypto { struct crypto_aead *tfm; u8 *iv, *authtag; @@ -320,7 +323,8 @@ static int enc_payload(struct snp_guest_dev *snp_dev, u= 64 seqno, int version, u8 =20 static int __handle_guest_request(struct snp_guest_dev *snp_dev, u64 exit_= code, __u64 *fw_err) { - unsigned long err, override_err =3D 0; + unsigned long err =3D 0xff, override_err =3D 0; + unsigned long req_start =3D jiffies; unsigned int override_npages =3D 0; int rc; =20 @@ -360,6 +364,19 @@ static int __handle_guest_request(struct snp_guest_dev= *snp_dev, u64 exit_code, * user as an ioctl() return code. */ goto retry_request; + + /* + * The host may return SNP_GUEST_REQ_ERR_EBUSY if the request has been + * throttled. Retry in the driver to avoid returning and reusing the + * message sequence number on a different message. + */ + case -EAGAIN: + if (jiffies - req_start > SNP_REQ_MAX_RETRY_DURATION) { + rc =3D -ETIMEDOUT; + break; + } + schedule_timeout_killable(SNP_REQ_RETRY_DELAY); + goto retry_request; } =20 if (fw_err) --=20 2.35.1 From nobody Tue Sep 9 17:29:09 2025 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 76403C61DA3 for ; Tue, 21 Feb 2023 11:35:15 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234109AbjBULfO (ORCPT ); Tue, 21 Feb 2023 06:35:14 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58154 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233832AbjBULeo (ORCPT ); Tue, 21 Feb 2023 06:34:44 -0500 Received: from mail.skyhub.de (mail.skyhub.de [5.9.137.197]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 7757526CEC for ; Tue, 21 Feb 2023 03:34:42 -0800 (PST) Received: from zn.tnic (p5de8e9fe.dip0.t-ipconnect.de [93.232.233.254]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.skyhub.de (SuperMail on ZX Spectrum 128k) with ESMTPSA id CF1FA1EC0682; Tue, 21 Feb 2023 12:34:40 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=alien8.de; s=dkim; t=1676979280; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=dWHBIgi4/PPHoVD+CngkvEQZ9VAcHNq+ytmZl53I++c=; b=Pa3GQzNpCEz5RIlXXbe112ECZiXJnWwMhmAnOK0IZbYyETMd8xNqbPoBdNZ7z7ZtQpZci4 xu457d3A+pT/QFBpwL3XB/XLxKeBhMGkAQdnSTIu9tV0VcMvYZZhhKDHbSkDF0CbU6GS5f ikY0Z0bUIxYDKFvwapn2qeTqIOZqvLc= From: Borislav Petkov To: LKML Cc: Dionna Glaze , Joerg Roedel , Michael Roth , Nikunj A Dadhania , Peter Gonda , Tom Lendacky , linux-coco@lists.linux.dev, x86@kernel.org Subject: [PATCH -v2 10/11] virt/coco/sev-guest: Double-buffer messages Date: Tue, 21 Feb 2023 12:34:27 +0100 Message-Id: <20230221113428.19324-11-bp@alien8.de> X-Mailer: git-send-email 2.35.1 In-Reply-To: <20230221113428.19324-1-bp@alien8.de> References: <20230221113428.19324-1-bp@alien8.de> 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" From: Dionna Glaze The encryption algorithms read and write directly to shared unencrypted memory, which may leak information as well as permit the host to tamper with the message integrity. Instead, copy whole messages in or out as needed before doing any computation on them. Fixes: d5af44dde546 ("x86/sev: Provide support for SNP guest request NAEs") Signed-off-by: Dionna Glaze Signed-off-by: Borislav Petkov (AMD) Link: https://lore.kernel.org/r/20230214164638.1189804-3-dionnaglaze@google= .com --- drivers/virt/coco/sev-guest/sev-guest.c | 27 +++++++++++++++++++++---- 1 file changed, 23 insertions(+), 4 deletions(-) diff --git a/drivers/virt/coco/sev-guest/sev-guest.c b/drivers/virt/coco/se= v-guest/sev-guest.c index da4f6267baad..6bc1390b54e8 100644 --- a/drivers/virt/coco/sev-guest/sev-guest.c +++ b/drivers/virt/coco/sev-guest/sev-guest.c @@ -46,7 +46,15 @@ struct snp_guest_dev { =20 void *certs_data; struct snp_guest_crypto *crypto; + /* request and response are in unencrypted memory */ struct snp_guest_msg *request, *response; + + /* + * Avoid information leakage by double-buffering shared messages + * in fields that are in regular encrypted memory. + */ + struct snp_guest_msg secret_request, secret_response; + struct snp_secrets_page_layout *layout; struct snp_req_data input; u32 *os_area_msg_seqno; @@ -266,14 +274,17 @@ static int dec_payload(struct snp_guest_dev *snp_dev,= struct snp_guest_msg *msg, static int verify_and_dec_payload(struct snp_guest_dev *snp_dev, void *pay= load, u32 sz) { struct snp_guest_crypto *crypto =3D snp_dev->crypto; - struct snp_guest_msg *resp =3D snp_dev->response; - struct snp_guest_msg *req =3D snp_dev->request; + struct snp_guest_msg *resp =3D &snp_dev->secret_response; + struct snp_guest_msg *req =3D &snp_dev->secret_request; struct snp_guest_msg_hdr *req_hdr =3D &req->hdr; struct snp_guest_msg_hdr *resp_hdr =3D &resp->hdr; =20 dev_dbg(snp_dev->dev, "response [seqno %lld type %d version %d sz %d]\n", resp_hdr->msg_seqno, resp_hdr->msg_type, resp_hdr->msg_version, resp_hdr= ->msg_sz); =20 + /* Copy response from shared memory to encrypted memory. */ + memcpy(resp, snp_dev->response, sizeof(*resp)); + /* Verify that the sequence counter is incremented by 1 */ if (unlikely(resp_hdr->msg_seqno !=3D (req_hdr->msg_seqno + 1))) return -EBADMSG; @@ -297,7 +308,7 @@ static int verify_and_dec_payload(struct snp_guest_dev = *snp_dev, void *payload, static int enc_payload(struct snp_guest_dev *snp_dev, u64 seqno, int versi= on, u8 type, void *payload, size_t sz) { - struct snp_guest_msg *req =3D snp_dev->request; + struct snp_guest_msg *req =3D &snp_dev->secret_request; struct snp_guest_msg_hdr *hdr =3D &req->hdr; =20 memset(req, 0, sizeof(*req)); @@ -400,13 +411,21 @@ static int handle_guest_request(struct snp_guest_dev = *snp_dev, u64 exit_code, in if (!seqno) return -EIO; =20 + /* Clear shared memory's response for the host to populate. */ memset(snp_dev->response, 0, sizeof(struct snp_guest_msg)); =20 - /* Encrypt the userspace provided payload */ + /* Encrypt the userspace provided payload in snp_dev->secret_request. */ rc =3D enc_payload(snp_dev, seqno, msg_ver, type, req_buf, req_sz); if (rc) return rc; =20 + /* + * Write the fully encrypted request to the shared unencrypted + * request page. + */ + memcpy(snp_dev->request, &snp_dev->secret_request, + sizeof(snp_dev->secret_request)); + rc =3D __handle_guest_request(snp_dev, exit_code, fw_err); if (rc) { dev_alert(snp_dev->dev, "Detected error from ASP request. rc: %d, fw_err= : %llu\n", rc, *fw_err); --=20 2.35.1 From nobody Tue Sep 9 17:29:09 2025 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 21978C61DA3 for ; Tue, 21 Feb 2023 11:35:19 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234164AbjBULfR (ORCPT ); Tue, 21 Feb 2023 06:35:17 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58178 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233945AbjBULep (ORCPT ); Tue, 21 Feb 2023 06:34:45 -0500 Received: from mail.skyhub.de (mail.skyhub.de [IPv6:2a01:4f8:190:11c2::b:1457]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 2658026CFC for ; Tue, 21 Feb 2023 03:34:43 -0800 (PST) Received: from zn.tnic (p5de8e9fe.dip0.t-ipconnect.de [93.232.233.254]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.skyhub.de (SuperMail on ZX Spectrum 128k) with ESMTPSA id 4C3CB1EC059E; Tue, 21 Feb 2023 12:34:41 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=alien8.de; s=dkim; t=1676979281; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=SmUs8DwPiZRjavZ2XyRnu2OzNFkfeY5x0Zeji6c7auE=; b=KAcDfO5bVsWA81G4my6293EtVm+6osi6XDe8fp7DvS531tp16csV6lvkP/F1zTWovqiOlm SXpUHSqw/XIehQtB6a4B+BBeVQHdgTJOHive1o5Qtk4Pd25IqJUbzDQhDiK+c4jfwoLJtb YREjDN380RjxORzPJL5TcFZzTt5Chys= From: Borislav Petkov To: LKML Cc: Dionna Glaze , Joerg Roedel , Michael Roth , Nikunj A Dadhania , Peter Gonda , Tom Lendacky , linux-coco@lists.linux.dev, x86@kernel.org Subject: [PATCH -v2 11/11] x86/sev: Change snp_guest_issue_request()'s fw_err argument Date: Tue, 21 Feb 2023 12:34:28 +0100 Message-Id: <20230221113428.19324-12-bp@alien8.de> X-Mailer: git-send-email 2.35.1 In-Reply-To: <20230221113428.19324-1-bp@alien8.de> References: <20230221113428.19324-1-bp@alien8.de> 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" From: Dionna Glaze The GHCB specification declares that the firmware error value for a guest request will be stored in the lower 32 bits of EXIT_INFO_2. The upper 32 bits are for the VMM's own error code. The fw_err argument to snp_guest_issue_request() is thus a misnomer, and callers will need access to all 64 bits. The type of unsigned long also causes problems, since sw_exit_info2 is u64 (unsigned long long) vs the argument's unsigned long*. Change this type for issuing the guest request. Pass the ioctl command struct's error field directly instead of in a local variable, since an incomplete guest request may not set the error code, and uninitialized stack memory would be written back to user space. The firmware might not even be called, so bookend the call with the no firmware call error and clear the error. Since the "fw_err" field is really exitinfo2 split into the upper bits' vmm error code and lower bits' firmware error code, convert the 64 bit value to a union. [ bp: Massage commit message, adjust code. ] Signed-off-by: Dionna Glaze Signed-off-by: Borislav Petkov (AMD) Link: https://lore.kernel.org/r/20230214164638.1189804-5-dionnaglaze@google= .com Tested-by: Dionna Glaze --- Documentation/virt/coco/sev-guest.rst | 20 ++++++---- arch/x86/include/asm/sev-common.h | 4 -- arch/x86/include/asm/sev.h | 8 ++-- arch/x86/kernel/sev.c | 15 ++++---- drivers/virt/coco/sev-guest/sev-guest.c | 49 +++++++++++++------------ include/uapi/linux/sev-guest.h | 18 ++++++++- 6 files changed, 68 insertions(+), 46 deletions(-) diff --git a/Documentation/virt/coco/sev-guest.rst b/Documentation/virt/coc= o/sev-guest.rst index aa3e4c6a1f90..68b0d2363af8 100644 --- a/Documentation/virt/coco/sev-guest.rst +++ b/Documentation/virt/coco/sev-guest.rst @@ -37,11 +37,11 @@ along with a description: the return value. General error numbers (-ENOMEM, -EINVAL) are not detailed, but errors with specific meanings are. =20 -The guest ioctl should be issued on a file descriptor of the /dev/sev-gues= t device. -The ioctl accepts struct snp_user_guest_request. The input and output stru= cture is -specified through the req_data and resp_data field respectively. If the io= ctl fails -to execute due to a firmware error, then fw_err code will be set. Otherwis= e, fw_err -will be set to 0x00000000ffffffff, i.e., the lower 32-bits are -1. +The guest ioctl should be issued on a file descriptor of the /dev/sev-guest +device. The ioctl accepts struct snp_user_guest_request. The input and +output structure is specified through the req_data and resp_data field +respectively. If the ioctl fails to execute due to a firmware error, then +the fw_error code will be set, otherwise fw_error will be set to -1. =20 The firmware checks that the message sequence counter is one greater than the guests message sequence counter. If guest driver fails to increment me= ssage @@ -57,8 +57,14 @@ counter (e.g. counter overflow), then -EIO will be retur= ned. __u64 req_data; __u64 resp_data; =20 - /* firmware error code on failure (see psp-sev.h) */ - __u64 fw_err; + /* bits[63:32]: VMM error code, bits[31:0] firmware error = code (see psp-sev.h) */ + union { + __u64 exitinfo2; + struct { + __u32 fw_error; + __u32 vmm_error; + }; + }; }; =20 2.1 SNP_GET_REPORT diff --git a/arch/x86/include/asm/sev-common.h b/arch/x86/include/asm/sev-c= ommon.h index b63be696b776..0759af9b1acf 100644 --- a/arch/x86/include/asm/sev-common.h +++ b/arch/x86/include/asm/sev-common.h @@ -128,10 +128,6 @@ struct snp_psc_desc { struct psc_entry entries[VMGEXIT_PSC_MAX_ENTRY]; } __packed; =20 -/* Guest message request error codes */ -#define SNP_GUEST_REQ_INVALID_LEN BIT_ULL(32) -#define SNP_GUEST_REQ_ERR_BUSY BIT_ULL(33) - #define GHCB_MSR_TERM_REQ 0x100 #define GHCB_MSR_TERM_REASON_SET_POS 12 #define GHCB_MSR_TERM_REASON_SET_MASK 0xf diff --git a/arch/x86/include/asm/sev.h b/arch/x86/include/asm/sev.h index ebc271bb6d8e..30567c72cbd9 100644 --- a/arch/x86/include/asm/sev.h +++ b/arch/x86/include/asm/sev.h @@ -185,6 +185,9 @@ static inline int pvalidate(unsigned long vaddr, bool r= mp_psize, bool validate) =20 return rc; } + +struct snp_guest_request_ioctl; + void setup_ghcb(void); void __init early_snp_set_memory_private(unsigned long vaddr, unsigned lon= g paddr, unsigned int npages); @@ -196,7 +199,7 @@ void snp_set_memory_private(unsigned long vaddr, unsign= ed int npages); void snp_set_wakeup_secondary_cpu(void); bool snp_init(struct boot_params *bp); void __init __noreturn snp_abort(void); -int snp_issue_guest_request(u64 exit_code, struct snp_req_data *input, uns= igned long *fw_err); +int snp_issue_guest_request(u64 exit_code, struct snp_req_data *input, str= uct snp_guest_request_ioctl *rio); #else static inline void sev_es_ist_enter(struct pt_regs *regs) { } static inline void sev_es_ist_exit(void) { } @@ -216,8 +219,7 @@ static inline void snp_set_memory_private(unsigned long= vaddr, unsigned int npag static inline void snp_set_wakeup_secondary_cpu(void) { } static inline bool snp_init(struct boot_params *bp) { return false; } static inline void snp_abort(void) { } -static inline int snp_issue_guest_request(u64 exit_code, struct snp_req_da= ta *input, - unsigned long *fw_err) +static inline int snp_issue_guest_request(u64 exit_code, struct snp_req_da= ta *input, struct snp_guest_request_ioctl *rio) { return -ENOTTY; } diff --git a/arch/x86/kernel/sev.c b/arch/x86/kernel/sev.c index 3f664ab277c4..b031244d6d2d 100644 --- a/arch/x86/kernel/sev.c +++ b/arch/x86/kernel/sev.c @@ -22,6 +22,8 @@ #include #include #include +#include +#include =20 #include #include @@ -2175,7 +2177,7 @@ static int __init init_sev_config(char *str) } __setup("sev=3D", init_sev_config); =20 -int snp_issue_guest_request(u64 exit_code, struct snp_req_data *input, uns= igned long *fw_err) +int snp_issue_guest_request(u64 exit_code, struct snp_req_data *input, str= uct snp_guest_request_ioctl *rio) { struct ghcb_state state; struct es_em_ctxt ctxt; @@ -2183,8 +2185,7 @@ int snp_issue_guest_request(u64 exit_code, struct snp= _req_data *input, unsigned struct ghcb *ghcb; int ret; =20 - if (!fw_err) - return -EINVAL; + rio->exitinfo2 =3D SEV_RET_NO_FW_CALL; =20 /* * __sev_get_ghcb() needs to run with IRQs disabled because it is using @@ -2209,16 +2210,16 @@ int snp_issue_guest_request(u64 exit_code, struct s= np_req_data *input, unsigned if (ret) goto e_put; =20 - *fw_err =3D ghcb->save.sw_exit_info_2; - switch (*fw_err) { + rio->exitinfo2 =3D ghcb->save.sw_exit_info_2; + switch (rio->exitinfo2) { case 0: break; =20 - case SNP_GUEST_REQ_ERR_BUSY: + case SNP_GUEST_VMM_ERR(SNP_GUEST_VMM_ERR_BUSY): ret =3D -EAGAIN; break; =20 - case SNP_GUEST_REQ_INVALID_LEN: + case SNP_GUEST_VMM_ERR(SNP_GUEST_VMM_ERR_INVALID_LEN): /* Number of expected pages are returned in RBX */ if (exit_code =3D=3D SVM_VMGEXIT_EXT_GUEST_REQUEST) { input->data_npages =3D ghcb_get_rbx(ghcb); diff --git a/drivers/virt/coco/sev-guest/sev-guest.c b/drivers/virt/coco/se= v-guest/sev-guest.c index 6bc1390b54e8..6b3c1d308353 100644 --- a/drivers/virt/coco/sev-guest/sev-guest.c +++ b/drivers/virt/coco/sev-guest/sev-guest.c @@ -332,11 +332,12 @@ static int enc_payload(struct snp_guest_dev *snp_dev,= u64 seqno, int version, u8 return __enc_payload(snp_dev, req, payload, sz); } =20 -static int __handle_guest_request(struct snp_guest_dev *snp_dev, u64 exit_= code, __u64 *fw_err) +static int __handle_guest_request(struct snp_guest_dev *snp_dev, u64 exit_= code, + struct snp_guest_request_ioctl *rio) { - unsigned long err =3D 0xff, override_err =3D 0; unsigned long req_start =3D jiffies; unsigned int override_npages =3D 0; + u64 override_err =3D 0; int rc; =20 retry_request: @@ -346,7 +347,7 @@ static int __handle_guest_request(struct snp_guest_dev = *snp_dev, u64 exit_code, * sequence number must be incremented or the VMPCK must be deleted to * prevent reuse of the IV. */ - rc =3D snp_issue_guest_request(exit_code, &snp_dev->input, &err); + rc =3D snp_issue_guest_request(exit_code, &snp_dev->input, rio); switch (rc) { case -ENOSPC: /* @@ -364,7 +365,7 @@ static int __handle_guest_request(struct snp_guest_dev = *snp_dev, u64 exit_code, * request buffer size was too small and give the caller the * required buffer size. */ - override_err =3D SNP_GUEST_REQ_INVALID_LEN; + override_err =3D SNP_GUEST_VMM_ERR_INVALID_LEN << SNP_GUEST_VMM_ERR_SHIF= T; =20 /* * If this call to the firmware succeeds, the sequence number can @@ -377,7 +378,7 @@ static int __handle_guest_request(struct snp_guest_dev = *snp_dev, u64 exit_code, goto retry_request; =20 /* - * The host may return SNP_GUEST_REQ_ERR_EBUSY if the request has been + * The host may return SNP_GUEST_VMM_ERR_BUSY if the request has been * throttled. Retry in the driver to avoid returning and reusing the * message sequence number on a different message. */ @@ -390,8 +391,8 @@ static int __handle_guest_request(struct snp_guest_dev = *snp_dev, u64 exit_code, goto retry_request; } =20 - if (fw_err) - *fw_err =3D override_err ?: err; + if (override_err) + rio->exitinfo2 =3D override_err; =20 if (override_npages) snp_dev->input.data_npages =3D override_npages; @@ -399,9 +400,10 @@ static int __handle_guest_request(struct snp_guest_dev= *snp_dev, u64 exit_code, return rc; } =20 -static int handle_guest_request(struct snp_guest_dev *snp_dev, u64 exit_co= de, int msg_ver, - u8 type, void *req_buf, size_t req_sz, void *resp_buf, - u32 resp_sz, __u64 *fw_err) +static int handle_guest_request(struct snp_guest_dev *snp_dev, u64 exit_co= de, + struct snp_guest_request_ioctl *rio, u8 type, + void *req_buf, size_t req_sz, void *resp_buf, + u32 resp_sz) { u64 seqno; int rc; @@ -415,7 +417,7 @@ static int handle_guest_request(struct snp_guest_dev *s= np_dev, u64 exit_code, in memset(snp_dev->response, 0, sizeof(struct snp_guest_msg)); =20 /* Encrypt the userspace provided payload in snp_dev->secret_request. */ - rc =3D enc_payload(snp_dev, seqno, msg_ver, type, req_buf, req_sz); + rc =3D enc_payload(snp_dev, seqno, rio->msg_version, type, req_buf, req_s= z); if (rc) return rc; =20 @@ -426,9 +428,11 @@ static int handle_guest_request(struct snp_guest_dev *= snp_dev, u64 exit_code, in memcpy(snp_dev->request, &snp_dev->secret_request, sizeof(snp_dev->secret_request)); =20 - rc =3D __handle_guest_request(snp_dev, exit_code, fw_err); + rc =3D __handle_guest_request(snp_dev, exit_code, rio); if (rc) { - dev_alert(snp_dev->dev, "Detected error from ASP request. rc: %d, fw_err= : %llu\n", rc, *fw_err); + dev_alert(snp_dev->dev, + "Detected error from ASP request. rc: %d, exitinfo2: %llu\n", + rc, rio->exitinfo2); snp_disable_vmpck(snp_dev); return rc; } @@ -471,9 +475,9 @@ static int get_report(struct snp_guest_dev *snp_dev, st= ruct snp_guest_request_io if (!resp) return -ENOMEM; =20 - rc =3D handle_guest_request(snp_dev, SVM_VMGEXIT_GUEST_REQUEST, arg->msg_= version, + rc =3D handle_guest_request(snp_dev, SVM_VMGEXIT_GUEST_REQUEST, arg, SNP_MSG_REPORT_REQ, &req, sizeof(req), resp->data, - resp_len, &arg->fw_err); + resp_len); if (rc) goto e_free; =20 @@ -511,9 +515,8 @@ static int get_derived_key(struct snp_guest_dev *snp_de= v, struct snp_guest_reque if (copy_from_user(&req, (void __user *)arg->req_data, sizeof(req))) return -EFAULT; =20 - rc =3D handle_guest_request(snp_dev, SVM_VMGEXIT_GUEST_REQUEST, arg->msg_= version, - SNP_MSG_KEY_REQ, &req, sizeof(req), buf, resp_len, - &arg->fw_err); + rc =3D handle_guest_request(snp_dev, SVM_VMGEXIT_GUEST_REQUEST, arg, + SNP_MSG_KEY_REQ, &req, sizeof(req), buf, resp_len); if (rc) return rc; =20 @@ -573,12 +576,12 @@ static int get_ext_report(struct snp_guest_dev *snp_d= ev, struct snp_guest_reques return -ENOMEM; =20 snp_dev->input.data_npages =3D npages; - ret =3D handle_guest_request(snp_dev, SVM_VMGEXIT_EXT_GUEST_REQUEST, arg-= >msg_version, + ret =3D handle_guest_request(snp_dev, SVM_VMGEXIT_EXT_GUEST_REQUEST, arg, SNP_MSG_REPORT_REQ, &req.data, - sizeof(req.data), resp->data, resp_len, &arg->fw_err); + sizeof(req.data), resp->data, resp_len); =20 /* If certs length is invalid then copy the returned length */ - if (arg->fw_err =3D=3D SNP_GUEST_REQ_INVALID_LEN) { + if (arg->vmm_error =3D=3D SNP_GUEST_VMM_ERR_INVALID_LEN) { req.certs_len =3D snp_dev->input.data_npages << PAGE_SHIFT; =20 if (copy_to_user((void __user *)arg->req_data, &req, sizeof(req))) @@ -613,7 +616,7 @@ static long snp_guest_ioctl(struct file *file, unsigned= int ioctl, unsigned long if (copy_from_user(&input, argp, sizeof(input))) return -EFAULT; =20 - input.fw_err =3D 0xff; + input.exitinfo2 =3D 0xff; =20 /* Message version must be non-zero */ if (!input.msg_version) @@ -644,7 +647,7 @@ static long snp_guest_ioctl(struct file *file, unsigned= int ioctl, unsigned long =20 mutex_unlock(&snp_cmd_mutex); =20 - if (input.fw_err && copy_to_user(argp, &input, sizeof(input))) + if (input.exitinfo2 && copy_to_user(argp, &input, sizeof(input))) return -EFAULT; =20 return ret; diff --git a/include/uapi/linux/sev-guest.h b/include/uapi/linux/sev-guest.h index 256aaeff7e65..b25b728281ca 100644 --- a/include/uapi/linux/sev-guest.h +++ b/include/uapi/linux/sev-guest.h @@ -52,8 +52,14 @@ struct snp_guest_request_ioctl { __u64 req_data; __u64 resp_data; =20 - /* firmware error code on failure (see psp-sev.h) */ - __u64 fw_err; + /* bits[63:32]: VMM error code, bits[31:0] firmware error code (see psp-s= ev.h) */ + union { + __u64 exitinfo2; + struct { + __u32 fw_error; + __u32 vmm_error; + }; + }; }; =20 struct snp_ext_report_req { @@ -77,4 +83,12 @@ struct snp_ext_report_req { /* Get SNP extended report as defined in the GHCB specification version 2.= */ #define SNP_GET_EXT_REPORT _IOWR(SNP_GUEST_REQ_IOC_TYPE, 0x2, struct snp_g= uest_request_ioctl) =20 +/* Guest message request EXIT_INFO_2 constants */ +#define SNP_GUEST_FW_ERR_MASK GENMASK_ULL(31, 0) +#define SNP_GUEST_VMM_ERR_SHIFT 32 +#define SNP_GUEST_VMM_ERR(x) (((u64)x) << SNP_GUEST_VMM_ERR_SHIFT) + +#define SNP_GUEST_VMM_ERR_INVALID_LEN BIT(0) +#define SNP_GUEST_VMM_ERR_BUSY BIT(1) + #endif /* __UAPI_LINUX_SEV_GUEST_H_ */ --=20 2.35.1