From nobody Fri Sep 12 01:04:11 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 EFBE9C61DA4 for ; Tue, 14 Feb 2023 16:49:01 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232628AbjBNQtA (ORCPT ); Tue, 14 Feb 2023 11:49:00 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:41870 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232289AbjBNQsw (ORCPT ); Tue, 14 Feb 2023 11:48:52 -0500 Received: from mail-pj1-x104a.google.com (mail-pj1-x104a.google.com [IPv6:2607:f8b0:4864:20::104a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 8DD8B2BED4 for ; Tue, 14 Feb 2023 08:48:48 -0800 (PST) Received: by mail-pj1-x104a.google.com with SMTP id ip9-20020a17090b314900b0023445cc3e08so410947pjb.1 for ; Tue, 14 Feb 2023 08:48:48 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=t23AMd5aqhUaIAzpimbSpgnbzTEYRxbMZhOXOMJqf1M=; b=Av62xu801emyTC/zrSTG4it6ti4mHUPuqVwvq8NXIy8HMdaqX6kctA2lkwYbkKfcQE 3RLGX+mSz8wqr3nSnPpP2DupddyPx8d28c/Zx4rDz9I37lRSe76FLb9lLm/Oq4yzN+v4 dkV6vpZLv51pG4xXMl0nu0WyEIRmO/taaaqHs0JS9ZIAJyG8PBJiXJLZfy9VXQZD9Qnn wvX4EN9BkqEilZ+glI4XNoN7wA02tLkjLMUZ394ccE3cOZOvqjNIBrMGO7M81u7sg67r 014pP69MiBRug5pyDB1tf4Kjge519iDILcWtNOPXOzgV/V2u8vJogGq1cmKaNUNE4/zA EUJw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=t23AMd5aqhUaIAzpimbSpgnbzTEYRxbMZhOXOMJqf1M=; b=OWEqfTn7g2u9yDboSklXsGBxjcwwKmmigslmSL5mFYyqXO2Mtz0/ARs5zVu69gPezv Vep9uI8mhVE3OemQpwfyowP73Snp7J0ufw8Rd3GV1kxfT9zyfk0uPgMRFJzaYXNCSwRy CQZqoUUCYmkCvI+JUbWnIzpGHTl5yVkgA2Rj9vcDoCn5972yeiceMWLsKgkQVRMTMoAW LS9CjI+/cCdhRU65D2CI7z0iD4xbH3Ej4J9uyX0v+b1N3QfteZ8aZdmP5p+dbWgRScsp WrISmoGMzWqJWtKKQPmwsYEL6shGP57LAIQTTT2Unk28cj3mXeSCFf6lWtt7XrdnrRN+ IU+A== X-Gm-Message-State: AO0yUKXH2+F0HiCbPElXzXS+/TZL+2374lpi1U5vppdKFWqs+2y4b56x 4nCFy2/Qx71QH+wOBrF5L1LuGVEGO8hFf+ETCZhcRHSByropIGl1VWG9qVPAPyDVhNd1HdFzk6J DL3PjHbpAX9Xfj78EHokqtiGxD7r74upj2W9JDMxs2iDel8asKB4zDoZghpx2XdX9btjSATTy4P x0q6bV8zs= X-Google-Smtp-Source: AK7set9TMtTubIo/r5gUBCy+Bqjv//+yICE90pVRl5n3U3MscUN8a0zmLwyTTeH3VPEUH4YmGGlXNLjkzsINH+uLQQ== X-Received: from dionnaglaze.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:2ee6]) (user=dionnaglaze job=sendgmr) by 2002:a17:90a:4e48:b0:233:aed3:b20 with SMTP id t8-20020a17090a4e4800b00233aed30b20mr42249pjl.12.1676393327892; Tue, 14 Feb 2023 08:48:47 -0800 (PST) Date: Tue, 14 Feb 2023 16:46:35 +0000 In-Reply-To: <20230214164638.1189804-1-dionnaglaze@google.com> Mime-Version: 1.0 References: <20230214164638.1189804-1-dionnaglaze@google.com> X-Mailer: git-send-email 2.39.1.637.g21b0678d19-goog Message-ID: <20230214164638.1189804-2-dionnaglaze@google.com> Subject: [PATCH v15 1/4] virt/coco/sev-guest: Add throttling awareness From: Dionna Glaze To: linux-kernel@vger.kernel.org, x86@kernel.org Cc: Dionna Glaze , Tom Lendacky , Paolo Bonzini , Joerg Roedel , Peter Gonda , Thomas Gleixner , Dave Hansen , Ingo Molnar , Borislav Petkov , "H. Peter Anvin" , Venu Busireddy , Michael Roth , "Kirill A. Shutemov" , Michael Sterritt Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" The host is permitted and encouraged to throttle guest requests to the AMD-SP since it is a shared resource across all VMs. Without throttling-awareness, the host returning an error will immediately lock out access to the VMPCK, which makes the VM less useful as it can't attest itself. Since throttling is expected for a host to protect itself from an uncooperative guest, a cooperative host can return a VMM error code that the request was throttled. The driver interprets the upper 32 bits of exitinfo2 as a VMM error code. 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. A quick fix is to retry for a while and then disable the VMPCK and return to user space. A guest request may not make it to the AMD-SP before the host returns to the guest, so the err local variable in handle_guest_request must be initialized the same way fw_err is. snp_issue_guest_request similarly should set fw_err whether or not the value is non-zero, in order to appropriately clear the error value when zero. The IV reuse fix for invalid certs_len needs modification to work with throttling, since a single retry with a modified exit_code may be throttled without retry and result in a locked-out VMPCK. Instead, change the exit_code as before and jump to the same retry label, and deal with the error code fixup by checking if the exit_code had to be changed. Cc: Tom Lendacky Cc: Paolo Bonzini Cc: Joerg Roedel Cc: Peter Gonda Cc: Thomas Gleixner Cc: Dave Hansen Cc: Ingo Molnar Cc: Borislav Petkov Cc: "H. Peter Anvin" Cc: Venu Busireddy Cc: Michael Roth Cc: "Kirill A. Shutemov" Cc: Michael Sterritt Fixes: d5af44dde546 ("x86/sev: Provide support for SNP guest request NAEs") Signed-off-by: Dionna Glaze --- arch/x86/include/asm/sev-common.h | 3 ++- arch/x86/kernel/sev.c | 3 +-- drivers/virt/coco/sev-guest/sev-guest.c | 34 ++++++++++++++++++++++--- 3 files changed, 34 insertions(+), 6 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 679026a640ef..a908ffc2dfba 100644 --- a/arch/x86/kernel/sev.c +++ b/arch/x86/kernel/sev.c @@ -2212,14 +2212,13 @@ 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) input->data_npages =3D ghcb_get_rbx(ghcb); =20 - *fw_err =3D ghcb->save.sw_exit_info_2; - ret =3D -EIO; } =20 diff --git a/drivers/virt/coco/sev-guest/sev-guest.c b/drivers/virt/coco/se= v-guest/sev-guest.c index 4ec4174e05a3..dc75f11c086e 100644 --- a/drivers/virt/coco/sev-guest/sev-guest.c +++ b/drivers/virt/coco/sev-guest/sev-guest.c @@ -30,6 +30,8 @@ #define DEVICE_NAME "sev-guest" #define AAD_LEN 48 #define MSG_HDR_VER 1 +#define ACCEPTABLE_REQUEST_RETRY_DURATION (60*HZ) +#define REQUEST_RETRY_DELAY (2*HZ) =20 struct snp_guest_crypto { struct crypto_aead *tfm; @@ -322,9 +324,12 @@ static int handle_guest_request(struct snp_guest_dev *= snp_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 =3D 0xff; + unsigned long start_time =3D jiffies; + u64 orig_exit_code =3D exit_code; u64 seqno; int rc; + unsigned int certs_npages =3D 0; =20 /* Get message sequence and verify that its a non-zero */ seqno =3D snp_get_msg_seqno(snp_dev); @@ -338,6 +343,7 @@ static int handle_guest_request(struct snp_guest_dev *s= np_dev, u64 exit_code, in if (rc) return rc; =20 +retry: /* * Call firmware to process the request. In this function the encrypted * message enters shared memory with the host. So after this call the @@ -346,6 +352,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 + /* + * 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. + */ + if (err =3D=3D SNP_GUEST_REQ_ERR_BUSY) { + if (jiffies - start_time > ACCEPTABLE_REQUEST_RETRY_DURATION) { + rc =3D -ETIMEDOUT; + /* + * Must disable VMPCK since it's not the user's + * responsibility to avoid IV reuse. + */ + goto disable_vmpck; + } + schedule_timeout_killable(REQUEST_RETRY_DELAY); + goto retry; + } + /* * If the extended guest request fails due to having too small of a * certificate data buffer, retry the same guest request without the @@ -354,7 +378,7 @@ static int handle_guest_request(struct snp_guest_dev *s= np_dev, u64 exit_code, in */ 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; + certs_npages =3D snp_dev->input.data_npages; =20 exit_code =3D SVM_VMGEXIT_GUEST_REQUEST; =20 @@ -366,8 +390,12 @@ 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); + schedule_timeout_killable(REQUEST_RETRY_DELAY); + goto retry; + } =20 + if (orig_exit_code =3D=3D SVM_VMGEXIT_EXT_GUEST_REQUEST && + exit_code !=3D orig_exit_code) { /* * Override the error to inform callers the given extended * request buffer size was too small and give the caller the --=20 2.39.1.637.g21b0678d19-goog From nobody Fri Sep 12 01:04:11 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 15F50C64EC7 for ; Tue, 14 Feb 2023 16:49:05 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232513AbjBNQtD (ORCPT ); Tue, 14 Feb 2023 11:49:03 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:41880 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232447AbjBNQsw (ORCPT ); Tue, 14 Feb 2023 11:48:52 -0500 Received: from mail-yw1-x114a.google.com (mail-yw1-x114a.google.com [IPv6:2607:f8b0:4864:20::114a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id BC2AD83E4 for ; Tue, 14 Feb 2023 08:48:50 -0800 (PST) Received: by mail-yw1-x114a.google.com with SMTP id 00721157ae682-52ec8c88d75so139470927b3.12 for ; Tue, 14 Feb 2023 08:48:50 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=s0nfdnvfldsrO3Wlbi7UXxbxGh5v/Cc3vMzKji0VJCw=; b=WBceX564EWms8L5oRwFo5LqR4t0pF7MQ1SN05LYidS09ZBWhB1oM/T2CpKL25862NX X6KUSF1RvcvkVsqnto+XKdP/3EInT0Y7ReNwzpqOIN0Uni33pIw95AFs6yXSBfETo4Iu v6rB1dtqqHuLFqrCBe626m8fUBt8u3lhffJaxAH2FN2JWGXldf3rHOts6+ngQXm18W0H gA2M8EH/FRHWQNDsids5N0OZNJ6WI6fzvaKB25/gb7kOFiCyGIJ9hM7JrSUpGfiibp4n +hXrRtjEdWdA1SFYFDihgThU7Don6bky/waq5m8tDNU9F/QjdMDfdmvChL4w9BS7B/3j 5POQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=s0nfdnvfldsrO3Wlbi7UXxbxGh5v/Cc3vMzKji0VJCw=; b=dgD6SdrgW4QhOHLdUHOyYSAv73cDUFtSD+pMmQ1Hr2FmgCj+SSOsKOz+yonmM45siL uTw6HH7o+ogvJ5qnZacVeDMEucoxOBGyYyjAiLWlpJX1NHrOakYDCw0HJF4ttkrC15kI H+d1JiLucG6P6XtXetuWXn5fP0dGh1oiniG2clrc5GPTrYy61VlHHwJOjPoPgQ5vdROC a+rQvwbtfpG+BRt5fIpQSMZH2x1fpVZ1LMKM37W6XuuTOPwECMa3h3AcYb4D+oCVwQVR o/w/mhEssyeGZ199qZek9VjGPFpVh8HJ6LAQk2/NPaATgONjXLdC1l89KxdwlpYniTr3 gviQ== X-Gm-Message-State: AO0yUKXUszwqVnn9gdXKCi4k2JPCxWizgUWzkiqmmopRUQeGZsOtjzFt +62+8HjjfysYI9xIfvM/uJfBJOYMCe0SXhYdVC1itdc/4gfdqhVKa+WsOr9ciRHdly0+R5hWoYl j/4hvbPAi/DjLd4VDi5B9hxq2oKwVxdaq2riHx2CB8R+/kMFFtJUQkzHBSU6JRJmCf6oihDM1s5 XDHWqr650= X-Google-Smtp-Source: AK7set9U2gh1E4Zu3sxIanWgg2VqTuBpapDNzMznUIzHuOOEowrzNDDxq8w3+Dnl27mY4U0RliJWWMNgjm7ejB9HOw== X-Received: from dionnaglaze.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:2ee6]) (user=dionnaglaze job=sendgmr) by 2002:a81:9c08:0:b0:4ff:95a:1aa with SMTP id m8-20020a819c08000000b004ff095a01aamr369220ywa.364.1676393329914; Tue, 14 Feb 2023 08:48:49 -0800 (PST) Date: Tue, 14 Feb 2023 16:46:36 +0000 In-Reply-To: <20230214164638.1189804-1-dionnaglaze@google.com> Mime-Version: 1.0 References: <20230214164638.1189804-1-dionnaglaze@google.com> X-Mailer: git-send-email 2.39.1.637.g21b0678d19-goog Message-ID: <20230214164638.1189804-3-dionnaglaze@google.com> Subject: [PATCH v15 2/4] virt/coco/sev-guest: Double-buffer messages From: Dionna Glaze To: linux-kernel@vger.kernel.org, x86@kernel.org Cc: Dionna Glaze , Tom Lendacky , Paolo Bonzini , Joerg Roedel , Peter Gonda , Thomas Gleixner , Dave Hansen , Ingo Molnar , Borislav Petkov , "H. Peter Anvin" , Venu Busireddy , Michael Roth , "Kirill A. Shutemov" , Michael Sterritt Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" 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") Cc: Tom Lendacky Cc: Paolo Bonzini Cc: Joerg Roedel Cc: Peter Gonda Cc: Thomas Gleixner Cc: Dave Hansen Cc: Ingo Molnar Cc: Borislav Petkov Cc: "H. Peter Anvin" Cc: Venu Busireddy Cc: Michael Roth Cc: "Kirill A. Shutemov" Cc: Michael Sterritt Signed-off-by: Dionna Glaze --- drivers/virt/coco/sev-guest/sev-guest.c | 25 +++++++++++++++++++++---- 1 file changed, 21 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 dc75f11c086e..9f6c5ca40d3b 100644 --- a/drivers/virt/coco/sev-guest/sev-guest.c +++ b/drivers/virt/coco/sev-guest/sev-guest.c @@ -45,7 +45,13 @@ 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; @@ -265,14 +271,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; @@ -296,7 +305,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)); @@ -336,13 +345,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)); + retry: /* * Call firmware to process the request. In this function the encrypted --=20 2.39.1.637.g21b0678d19-goog From nobody Fri Sep 12 01:04:11 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 015E8C05027 for ; Tue, 14 Feb 2023 16:49:08 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232631AbjBNQtG (ORCPT ); Tue, 14 Feb 2023 11:49:06 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:41866 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232597AbjBNQsx (ORCPT ); Tue, 14 Feb 2023 11:48:53 -0500 Received: from mail-pl1-x64a.google.com (mail-pl1-x64a.google.com [IPv6:2607:f8b0:4864:20::64a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C521C976F for ; Tue, 14 Feb 2023 08:48:52 -0800 (PST) Received: by mail-pl1-x64a.google.com with SMTP id d21-20020a170902cb9500b0019ab46166a3so1863846ply.5 for ; Tue, 14 Feb 2023 08:48:52 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=TrJQ2wDw2LHf9efJZF0CvF6CmGL/52JTDCIgIS/uJjo=; b=IKFwvbb3dinO6WmpEYEZwVado7b2ZjuvmuubAEs104eU9ZB+6TV1svRGGeN7EPzrT8 tSekFngm37JSLBSmqfU3ibD7OwxY98scdplDVEvZp8xzXfYccp5//WbeV14uaWqRp9CA xMAUrk81o6xUduIE0ewZRtMiVhGBndweBM322wcvZm47+N0A0U1T2WouX/YEOd+0U/yP Rg06nO/0x8Ac7MEWOIgVOg4tLQuhgHjf+JbYUMUM0lqk9MXix53e0YdWRy8MbPq6SWm9 G+RnkKQ/W013ZJ3l4Awo+2ih+6lxSf/A7OgiH59Otuf5ye/jJkOKiy83OTedQZ1GTu9a 1gPQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=TrJQ2wDw2LHf9efJZF0CvF6CmGL/52JTDCIgIS/uJjo=; b=NMWRdA/aLfbs4hb7qiFyy4ypNa6n5uc/xIBD1bwmHVzsDqnI4NsvhqSku6swf4mr0o zCoWuDUjLF3vGeMYIYPCKBrCLtdnLh5xBlyeQoxaDA8CIwAjrD5RdUOu0O+KKr+zi7S9 UpFvoc5Z7MDtq/MoKCEUIv5iTXtBTmivEIZHw6gsqD4dcJB+BnooqoQZz+PATX7krd0S Sa4NEUFU3hIOPfDN149oveAQ9jXGm8TpWYbsBrM2zj1bhuuYhxHzyMYmfRM6MoObUe7z 9df6fodw8QOg4/QF1wawkXcU51NfS4TpPXwIPObJR6pqM/uwyv2e3cSbtt65kI177aDu cXEg== X-Gm-Message-State: AO0yUKVf7Kqy23PHplomP3z3bWX1+ka/r0pKjbkLGzb77kVi9A5l/rad saFNdOEvJh6ywZ78fElYKYQ7NPMGpjB4kFOdFe1kUUaEvoP7YntBMQPaoiASTsiPNHtSYBNga8L ck68njKZBC8hJpuMdibgg3R8qkIHmztAE3C4a1VOQH3bVbJYfHv0aPXKur9owcRYxGI+kSmy7w7 pKZRit+fk= X-Google-Smtp-Source: AK7set9axnAnXYz0NGmRmX1hq+CElbkNVSC7MbnHJPt46WHf6/z5W5PU9VcDtl7dwbTWJoA+XKit+e1L/0MLhnjmCQ== X-Received: from dionnaglaze.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:2ee6]) (user=dionnaglaze job=sendgmr) by 2002:a63:3502:0:b0:4fb:98ef:e33 with SMTP id c2-20020a633502000000b004fb98ef0e33mr4474pga.12.1676393331947; Tue, 14 Feb 2023 08:48:51 -0800 (PST) Date: Tue, 14 Feb 2023 16:46:37 +0000 In-Reply-To: <20230214164638.1189804-1-dionnaglaze@google.com> Mime-Version: 1.0 References: <20230214164638.1189804-1-dionnaglaze@google.com> X-Mailer: git-send-email 2.39.1.637.g21b0678d19-goog Message-ID: <20230214164638.1189804-4-dionnaglaze@google.com> Subject: [PATCH v15 3/4] crypto: ccp - Name -1 return value as SEV_RET_NO_FW_CALL From: Dionna Glaze To: linux-kernel@vger.kernel.org, x86@kernel.org Cc: Peter Gonda , Thomas Lendacky , Paolo Bonzini , Joerg Roedel , Ingo Molnar , Andy Lutomirsky , John Allen , Herbert Xu , "David S. Miller" , Borislav Petkov , Dionna Glaze Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable 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 is not actually called. To make this protocol unambiguous, the value is named SEV_RET_NO_FW_CALL. Cc: Thomas Lendacky Cc: Paolo Bonzini Cc: Joerg Roedel Cc: Ingo Molnar Cc: Andy Lutomirsky Cc: John Allen Cc: Herbert Xu Cc: "David S. Miller" Cc: Borislav Petkov Signed-off-by: Peter Gonda Signed-off-by: Dionna Glaze --- Documentation/virt/coco/sev-guest.rst | 2 +- drivers/crypto/ccp/sev-dev.c | 22 ++++++++++++++-------- include/uapi/linux/psp-sev.h | 7 +++++++ 3 files changed, 22 insertions(+), 9 deletions(-) diff --git a/Documentation/virt/coco/sev-guest.rst b/Documentation/virt/coc= o/sev-guest.rst index bf593e88cfd9..e76393e389eb 100644 --- a/Documentation/virt/coco/sev-guest.rst +++ b/Documentation/virt/coco/sev-guest.rst @@ -41,7 +41,7 @@ The guest ioctl should be issued on a file descriptor of = the /dev/sev-guest devi 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. +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..ac205f78a595 100644 --- a/drivers/crypto/ccp/sev-dev.c +++ b/drivers/crypto/ccp/sev-dev.c @@ -440,12 +440,19 @@ 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) { struct psp_device *psp =3D psp_master; struct sev_device *sev; - int rc =3D 0, psp_ret =3D -1; - int (*init_function)(int *error); + int rc =3D 0, psp_ret =3D SEV_RET_NO_FW_CALL; =20 if (!psp || !psp->sev_data) return -ENODEV; @@ -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 @@ -473,9 +477,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."); - rc =3D init_function(&psp_ret); + dev_err(sev->dev, +"SEV: retrying INIT command because of SECURE_DATA_INVALID error. Retrying= once to reset PSP SEV state."); + rc =3D __sev_do_init_locked(&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..e8cfb8bde0d7 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 but is added 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.39.1.637.g21b0678d19-goog From nobody Fri Sep 12 01:04:11 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 77AF4C61DA4 for ; Tue, 14 Feb 2023 16:49:13 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232723AbjBNQtM (ORCPT ); Tue, 14 Feb 2023 11:49:12 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:41826 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232482AbjBNQs4 (ORCPT ); Tue, 14 Feb 2023 11:48:56 -0500 Received: from mail-pj1-x1049.google.com (mail-pj1-x1049.google.com [IPv6:2607:f8b0:4864:20::1049]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 63AE3868A for ; Tue, 14 Feb 2023 08:48:54 -0800 (PST) Received: by mail-pj1-x1049.google.com with SMTP id kw4-20020a17090b220400b00233d236c7b4so3594434pjb.1 for ; Tue, 14 Feb 2023 08:48:54 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=U2LFbXrVP9Dj/xYoFXQbnLbIxOP+xAIoRkXou7v2xxU=; b=ColOKHmzMbt7S8y1t6aIYYaRqHap+hjj4p10jQEDh/HKAzDbcd2DVw1qkVT0tnedmM mcwIY47+JarUIUsSICtduL8igFVNBnA/tUv+Zaq6USD2B53RIaj30/mn0LAlBKyqw1eD DMJYn4s2fPx7Av4YMbKcwVfYZnhcx7FMIZ3e0GxHlvyjBrvZIwQhw7Z6gZ3mE04rY76B T4dVLeFVaU0x1Poh5TU6CkcOJX1Qo6j0wwQwPw0hHiWGPVc3+WDae7xynl4byRlPYaEt 0+u5Xroto5qP2iMfVoRuzSzl66MAFxm6rRyauBKheoVCxlfVB36+P3eYeA5jYdkxf+Tt 7V8A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=U2LFbXrVP9Dj/xYoFXQbnLbIxOP+xAIoRkXou7v2xxU=; b=0XAmCHMWLofUG3QJGO68hGkWVV+AfuwdjoB/AceAFtdT4a2tqO4GFDEIonBXSloK0D b85ObST83ZT6mvOByq5OQRGNuM36AgHJ8H6Waigd/9Dq5yqALPGBBLGjdqR9bs2Z6Gej 3z6jdoLdx1kFfGU7UYnH2LDAhG0Ri02Wl/5C67aXwxV7wiCdG09bJgrpfQaCp5KfDwVY dulWQxHUJGsTCfqEL56Q+EwXuB7B14O4e1+Z/1mWkofWl+DdhylPTDE8kLMVNOqICTXc iwpSxUYq63KGfBCTTo6aQqU4EMLYqo/AMHIT/APAMJtnTNiNCUaoJCeydu5o1Ay6/HC3 CTYw== X-Gm-Message-State: AO0yUKXdGexzRnuDqhW71ztkv5PKcb6vtohj9Rv7iD2kzX3kwn3qW6JY 5v5nPWiaaLF9G2AW+AxT8bfeniiJnj7aBoYbqvySQb9D+TiWggb6NtG5YpE968AKUujRwH/fIm5 6caHyVpNOd5Oiul2/GSWx5zyMGnG9STV86F+bRDbmTOwJmO03KuFTrY4afGPb1VX5VSIxdlqIp6 YCSzKvUbc= X-Google-Smtp-Source: AK7set+6qEJB647Zln1jr2hm300knwZrdL4JildH4P/tNsQN5eXyRNmjeiJUZSgbrdI6MpDAHB/B9HgLkm4irfpHzg== X-Received: from dionnaglaze.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:2ee6]) (user=dionnaglaze job=sendgmr) by 2002:a17:90a:64c1:b0:233:d985:1eae with SMTP id i1-20020a17090a64c100b00233d9851eaemr47238pjm.10.1676393333824; Tue, 14 Feb 2023 08:48:53 -0800 (PST) Date: Tue, 14 Feb 2023 16:46:38 +0000 In-Reply-To: <20230214164638.1189804-1-dionnaglaze@google.com> Mime-Version: 1.0 References: <20230214164638.1189804-1-dionnaglaze@google.com> X-Mailer: git-send-email 2.39.1.637.g21b0678d19-goog Message-ID: <20230214164638.1189804-5-dionnaglaze@google.com> Subject: [PATCH v15 4/4] x86/sev: Change snp_guest_issue_request's fw_err From: Dionna Glaze To: linux-kernel@vger.kernel.org, x86@kernel.org Cc: Dionna Glaze , Tom Lendacky , Paolo Bonzini , Joerg Roedel , Peter Gonda , Thomas Gleixner , Dave Hansen , Ingo Molnar , Borislav Petkov , "H. Peter Anvin" , Venu Busireddy , Michael Roth , "Kirill A. Shutemov" , Michael Sterritt Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" 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 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*. This type is changed for issuing the guest request. The ioctl command struct's error field is passed directly instead of 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 the call is bookended with the no firmware call error and clearing the error. Since the "fw_err" field is really exitinfo2 split into the upper bits' vmm error code and lower bits' firmware error code, sev-guest.h is updated to represent the 64 bit value as a union. Cc: Tom Lendacky Cc: Paolo Bonzini Cc: Joerg Roedel Cc: Peter Gonda Cc: Thomas Gleixner Cc: Dave Hansen Cc: Ingo Molnar Cc: Borislav Petkov Cc: "H. Peter Anvin" Cc: Venu Busireddy Cc: Michael Roth Cc: "Kirill A. Shutemov" Cc: Michael Sterritt Signed-off-by: Dionna Glaze --- Documentation/virt/coco/sev-guest.rst | 21 +++++++---- arch/x86/include/asm/sev-common.h | 4 --- arch/x86/include/asm/sev.h | 4 +-- arch/x86/kernel/sev.c | 12 ++++--- drivers/virt/coco/sev-guest/sev-guest.c | 47 ++++++++++++------------- include/uapi/linux/sev-guest.h | 18 ++++++++-- 6 files changed, 62 insertions(+), 44 deletions(-) diff --git a/Documentation/virt/coco/sev-guest.rst b/Documentation/virt/coc= o/sev-guest.rst index e76393e389eb..48e89ea2a618 100644 --- a/Documentation/virt/coco/sev-guest.rst +++ b/Documentation/virt/coco/sev-guest.rst @@ -37,11 +37,12 @@ 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 otherwise= the -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 +58,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..05de34d10d89 100644 --- a/arch/x86/include/asm/sev.h +++ b/arch/x86/include/asm/sev.h @@ -196,7 +196,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, u64= *exitinfo2); #else static inline void sev_es_ist_enter(struct pt_regs *regs) { } static inline void sev_es_ist_exit(void) { } @@ -217,7 +217,7 @@ 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) + u64 *exitinfo2) { return -ENOTTY; } diff --git a/arch/x86/kernel/sev.c b/arch/x86/kernel/sev.c index a908ffc2dfba..82aa81fafb71 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, u64= *exitinfo2) { struct ghcb_state state; struct es_em_ctxt ctxt; @@ -2186,9 +2188,11 @@ int snp_issue_guest_request(u64 exit_code, struct sn= p_req_data *input, unsigned if (!cc_platform_has(CC_ATTR_GUEST_SEV_SNP)) return -ENODEV; =20 - if (!fw_err) + if (!exitinfo2) return -EINVAL; =20 + *exitinfo2 =3D SEV_RET_NO_FW_CALL; + /* * __sev_get_ghcb() needs to run with IRQs disabled because it is using * a per-CPU GHCB. @@ -2212,11 +2216,11 @@ 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; + *exitinfo2 =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_VMM_ERR(SNP_GUEST_VMM_ERR_IN= VALID_LEN)) input->data_npages =3D ghcb_get_rbx(ghcb); =20 ret =3D -EIO; diff --git a/drivers/virt/coco/sev-guest/sev-guest.c b/drivers/virt/coco/se= v-guest/sev-guest.c index 9f6c5ca40d3b..04d71bb67975 100644 --- a/drivers/virt/coco/sev-guest/sev-guest.c +++ b/drivers/virt/coco/sev-guest/sev-guest.c @@ -329,11 +329,11 @@ 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_co= de, + struct snp_guest_request_ioctl *arg, + u8 type, void *req_buf, size_t req_sz, + void *resp_buf, u32 resp_sz) { - unsigned long err =3D 0xff; unsigned long start_time =3D jiffies; u64 orig_exit_code =3D exit_code; u64 seqno; @@ -349,7 +349,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, arg->msg_version, type, req_buf, req_s= z); if (rc) return rc; =20 @@ -367,14 +367,15 @@ static int handle_guest_request(struct snp_guest_dev = *snp_dev, u64 exit_code, in * 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, + &arg->exitinfo2); =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. */ - if (err =3D=3D SNP_GUEST_REQ_ERR_BUSY) { + if (arg->vmm_error =3D=3D SNP_GUEST_VMM_ERR_BUSY) { if (jiffies - start_time > ACCEPTABLE_REQUEST_RETRY_DURATION) { rc =3D -ETIMEDOUT; /* @@ -394,7 +395,7 @@ static int handle_guest_request(struct snp_guest_dev *s= np_dev, u64 exit_code, in * and thus avoid IV reuse. */ if (exit_code =3D=3D SVM_VMGEXIT_EXT_GUEST_REQUEST && - err =3D=3D SNP_GUEST_REQ_INVALID_LEN) { + arg->vmm_error =3D=3D SNP_GUEST_VMM_ERR_INVALID_LEN) { certs_npages =3D snp_dev->input.data_npages; =20 exit_code =3D SVM_VMGEXIT_GUEST_REQUEST; @@ -418,17 +419,14 @@ static int handle_guest_request(struct snp_guest_dev = *snp_dev, u64 exit_code, in * request buffer size was too small and give the caller the * required buffer size. */ - err =3D SNP_GUEST_REQ_INVALID_LEN; + arg->vmm_error =3D SNP_GUEST_VMM_ERR_INVALID_LEN; snp_dev->input.data_npages =3D certs_npages; } =20 - if (fw_err) - *fw_err =3D err; - if (rc) { dev_alert(snp_dev->dev, - "Detected error from ASP request. rc: %d, fw_err: %llu\n", - rc, *fw_err); + "Detected error from ASP request. rc: %d, exitinfo2: %llx\n", + rc, arg->exitinfo2); goto disable_vmpck; } =20 @@ -475,9 +473,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 @@ -515,9 +513,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 @@ -577,12 +574,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))) @@ -617,7 +614,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) @@ -648,7 +645,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..2aa39112cf8d 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 1 +#define SNP_GUEST_VMM_ERR_BUSY 2 + #endif /* __UAPI_LINUX_SEV_GUEST_H_ */ --=20 2.39.1.637.g21b0678d19-goog