From patchwork Wed May 1 08:52:09 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Michael Roth X-Patchwork-Id: 13650343 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 kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 61234C4345F for ; Wed, 1 May 2024 09:06:21 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id E6A636B00AC; Wed, 1 May 2024 05:06:20 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id E19C46B00AD; Wed, 1 May 2024 05:06:20 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id C6BD76B00AE; Wed, 1 May 2024 05:06:20 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0015.hostedemail.com [216.40.44.15]) by kanga.kvack.org (Postfix) with ESMTP id A6D6D6B00AC for ; Wed, 1 May 2024 05:06:20 -0400 (EDT) Received: from smtpin25.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id 60986405D5 for ; Wed, 1 May 2024 09:06:20 +0000 (UTC) X-FDA: 82069245720.25.5F17D54 Received: from NAM11-DM6-obe.outbound.protection.outlook.com (mail-dm6nam11on2050.outbound.protection.outlook.com [40.107.223.50]) by imf10.hostedemail.com (Postfix) with ESMTP id 6BBCFC0013 for ; Wed, 1 May 2024 09:06:17 +0000 (UTC) Authentication-Results: imf10.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=hhdFfRMp; dmarc=pass (policy=quarantine) header.from=amd.com; spf=pass (imf10.hostedemail.com: domain of Michael.Roth@amd.com designates 40.107.223.50 as permitted sender) smtp.mailfrom=Michael.Roth@amd.com; arc=pass ("microsoft.com:s=arcselector9901:i=1") ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1714554377; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=bpKnivgAVH2H/wCIQAMeWVTFrkr7yKIT0Qi9SSKf8jk=; b=FrvpjvK5o8n8HjsnQUfu+GhApcQWvImQB1hnIvDbJ5yNGYHm4YeNfH99oCXVOApzXnGjNT TMFCgIlne/bo9xKeK4w9zd8tzH1By/jOvD5H2ZKmn0Cr/4aarAEn836qvJGt1gtnPCwyaX 37DZ2AmYmX4C5HTRTcxNECXDHEQ4xqo= ARC-Authentication-Results: i=2; imf10.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=hhdFfRMp; dmarc=pass (policy=quarantine) header.from=amd.com; spf=pass (imf10.hostedemail.com: domain of Michael.Roth@amd.com designates 40.107.223.50 as permitted sender) smtp.mailfrom=Michael.Roth@amd.com; arc=pass ("microsoft.com:s=arcselector9901:i=1") ARC-Seal: i=2; s=arc-20220608; d=hostedemail.com; t=1714554377; a=rsa-sha256; cv=pass; b=sSGjZ1UMsn7GiCspTAj5n/aTqFmmsL5Idx3FpdM0fFRG/f5T9HZcT25MuK5me1oFcWRsn5 7pQ76v8irAHHO3v/KoHcOaaLoJNn4TXnjNcnaQDY0Ht6ASnxkvcKrD0YwS0bGD1f8prlt4 cs8vMggT2yzI7i6ZiGyjsyc+aPoEDuA= ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=euz/LBgcQHSnCYvWGLTaXNiVQAPteE2Zn+NdyIeiXEGH0y3Ocg7tYlguLjhKiWoLTQOaO50CrvcRvIiOgc7S2O+sqJTF5OIT1epkZQU/qBatD4Gb+KElmPDo1I475UPa2RZz0fvHLc4m3H/QHhI6xAjYmW5hICovs0jdUXjxcEqw98FhjMg5NNgvmREKiQdtO0kUwCdb+1lkSESBbbA6ZIJHi5ZCFEuZt8bu1sBMV6UFA8NlPYDob9U9bz5KrDj3wfLbumJ8f+eWXzLrCP/By0U+54oRiXocfC/TRBzK+eFn9ZfUlhAfBdpoYZfdFa82qxGy5kmZkrZyqwBP0VD+2A== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=bpKnivgAVH2H/wCIQAMeWVTFrkr7yKIT0Qi9SSKf8jk=; b=au2DfYocEIGFA83z2eMYqy6TknYOXMwKGa5rcGrMTX+6JjBDWlk491bDr8CM/fdATMa/r0uVlLdvZpENKA+5gtl2LqyqBswiPgR/Ahvcji/lvy0fIMJp1YFSPM6XZwunAbuvx9OC45v/DqQ1KciYUfQZz3TPrj2U2nMDymDiUi5JFGlCgtWxA2BDPwbK4/WMKW+c0mWKmENNmEnE8sIqv7h79ziRKsZ1jN9fLAaGMv0Z5jDfYY7etbs8d6r+RNZMRA/hVL25W7rwz4o8ekSrh4fBVbeIsjKpfSV27aCkdr0nN0fpKuIEplmdCpj8A+St4GVvHmGTB/cimjrD4P+URA== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 165.204.84.17) smtp.rcpttodomain=vger.kernel.org smtp.mailfrom=amd.com; dmarc=pass (p=quarantine sp=quarantine pct=100) action=none header.from=amd.com; dkim=none (message not signed); arc=none (0) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amd.com; s=selector1; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=bpKnivgAVH2H/wCIQAMeWVTFrkr7yKIT0Qi9SSKf8jk=; b=hhdFfRMp3SwneAGvgueb/kBipUwfChQDdukYjL2PPWFqhxoTpQDrSlLW0ABiif0Fbt0xCOTk04eOBDC8mak5bohF9yNkE0UsNzokZSIAjwNbfNXK9nV/hGaeGggE4Cov8xP0cY7IKo7elTs4P9J5I4RcgJlT4tFywr+dN23c7mM= Received: from BN9PR03CA0523.namprd03.prod.outlook.com (2603:10b6:408:131::18) by SN7PR12MB7154.namprd12.prod.outlook.com (2603:10b6:806:2a5::16) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7519.34; Wed, 1 May 2024 09:06:12 +0000 Received: from BN2PEPF0000449F.namprd02.prod.outlook.com (2603:10b6:408:131:cafe::f7) by BN9PR03CA0523.outlook.office365.com (2603:10b6:408:131::18) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7519.36 via Frontend Transport; Wed, 1 May 2024 09:06:12 +0000 X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 165.204.84.17) smtp.mailfrom=amd.com; dkim=none (message not signed) header.d=none;dmarc=pass action=none header.from=amd.com; Received-SPF: Pass (protection.outlook.com: domain of amd.com designates 165.204.84.17 as permitted sender) receiver=protection.outlook.com; client-ip=165.204.84.17; helo=SATLEXMB04.amd.com; pr=C Received: from SATLEXMB04.amd.com (165.204.84.17) by BN2PEPF0000449F.mail.protection.outlook.com (10.167.243.150) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.7544.18 via Frontend Transport; Wed, 1 May 2024 09:06:12 +0000 Received: from localhost (10.180.168.240) by SATLEXMB04.amd.com (10.181.40.145) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2507.35; Wed, 1 May 2024 04:06:10 -0500 From: Michael Roth To: CC: , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , Subject: [PATCH v15 19/20] KVM: SEV: Provide support for SNP_EXTENDED_GUEST_REQUEST NAE event Date: Wed, 1 May 2024 03:52:09 -0500 Message-ID: <20240501085210.2213060-20-michael.roth@amd.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20240501085210.2213060-1-michael.roth@amd.com> References: <20240501085210.2213060-1-michael.roth@amd.com> MIME-Version: 1.0 X-Originating-IP: [10.180.168.240] X-ClientProxiedBy: SATLEXMB04.amd.com (10.181.40.145) To SATLEXMB04.amd.com (10.181.40.145) X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: BN2PEPF0000449F:EE_|SN7PR12MB7154:EE_ X-MS-Office365-Filtering-Correlation-Id: 445e63d8-c90d-4659-70d0-08dc69bdf2be X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0;ARA:13230031|36860700004|1800799015|82310400014|376005|7416005; X-Microsoft-Antispam-Message-Info: XHWBHG4RyxSyohYa7PC8PGj6bbR4/2e4nSNfl4gjBCRIpyb1FtiT2u2yYuroVM5KQm5pKQn7N4+R6xswO4oU6guC5pwebB4JDID0VlVlB/msUjKK8BW4AZj/iX1w9t0773870SSDHpOw/IWMgq0WlJ40TPZpH9ThQUBtJtA9Qgge/7QyM+SWdcEkqXHCMUluHhp63dYdf14vzKVe+77X4dZYKFbk5LRhPc2PmbKknyGre4AqEaJtfXiKvQVUzngcK3HQOmMPWHcPcUHIpU4gYOjkfDUTcb/lMc4ZXiJjCQF7zshr/puC6cOCBV3E87lfq1qhG8hLqcAScUA85JnGEc98bNGWydTmblgDcKOXHe2X9oc0S+88X0vB0D/rir+rKVO11HEL0c3NqJ07yL4QQX9HkjUSmMv8BkbDcfi4ETdHSc9rdufOu9ctYj+fGZuoTJYRNzciu1iRgCm9XumFdwpOZ3PCVLfHewzh9Ptwb/duzqo0OnwVXgDjy+NXIpHk9j3X0/IR4ss1JtZgWA3MBr9xqZ/00piCiSAFS22hecrO99t67JrU3CxFaVleaUhtdFr0OAZmxnMO1RCPaXtdB22kfdM1pjVMrq6/XsmFVEiPJNKSIzhyfF/6r8FcHFutawlBuGeqP/3NId4dQyi+aiQobzNTNzvww62Q5ktAR1bjh4KbNVfeqzVwaSs//YLbz3ypf5duT6KjDDeWFSCfu9QeDkTaTOjcB9TPaoNRkaXBmI5S/g5icspAA90VZcPZIDibMtmfJzU6b0akTMs+xuidicz3Yb0Dy5UlGTaUVStCaFMTRsy+n3bDTAnn09JrNFnw9LTjX62r0YMhX47m0Ix99go5Fgu+5C+4lZiAkeORzZUQs1LmJEuD0ErSxGL8pvLsSffdSh+Y1g6xTXhAeqS21d6AReJVATVe6TAoWCOJfppIT+sYsnnYxHd5SNvE5yokwJjewzcWFM7s/N+lSNO1EKO3MZuNRV2nsdG8s6nmi9ArMtVRmLvc9no3nSWwBgMnxukCvqmNEpHgyVvDnB3dTsN6Em6j6kilMwRRlJHnVkQDBiwsFZr5Wj36kPxXwQzeXp1lDFy9o9Ag7A2HCBXlH3ocmwgDTPNGynjJPEIxtPQRPcBWGxNaIiCHafz2Yf3sG28qq8IPN5WhNHwfEj9iEnv57wS8t290RgDuQuYa7dIuhQjS12WTNl93zxEDbeMIdueDRO5PtpPziLhng+Hqi4se4/WtOXsq9dMYJEeW8qX9ZQ81OfFHflh33APt87a7sWyoq1b3gou5GG+e7HkxOku7Cp/zHHekNbDHRVpKAtOom5vGx8gBowqQKOj3OGOijDETWRW9YJN0xZOnEYyXCxeWGedRPbJB6zMPfrJjaBrOSPvnKbI7P4QyeWls X-Forefront-Antispam-Report: CIP:165.204.84.17;CTRY:US;LANG:en;SCL:1;SRV:;IPV:CAL;SFV:NSPM;H:SATLEXMB04.amd.com;PTR:InfoDomainNonexistent;CAT:NONE;SFS:(13230031)(36860700004)(1800799015)(82310400014)(376005)(7416005);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 01 May 2024 09:06:12.1445 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 445e63d8-c90d-4659-70d0-08dc69bdf2be X-MS-Exchange-CrossTenant-Id: 3dd8961f-e488-4e60-8e11-a82d994e183d X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=3dd8961f-e488-4e60-8e11-a82d994e183d;Ip=[165.204.84.17];Helo=[SATLEXMB04.amd.com] X-MS-Exchange-CrossTenant-AuthSource: BN2PEPF0000449F.namprd02.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: SN7PR12MB7154 X-Stat-Signature: i1cdhnuwe7izx5gz4h3zji7mqwkianzy X-Rspamd-Queue-Id: 6BBCFC0013 X-Rspam-User: X-Rspamd-Server: rspam12 X-HE-Tag: 1714554377-335671 X-HE-Meta: U2FsdGVkX18enaE8XRaRHU6/LJXRB/fLQw+TXI2y2VfAHJAL0KrnY9Z10ptf/XNTuxlFaIlS9heGQqcICvAz9EuYJXEhBOVg2BxsxcEgseqhAfFlo5DPuIdphbn9xFDrHR5UJbjZaOvGzmjnAoJBTWPpX9lBeezasKGkbxHT7S/NYR7cLxf/S6V+P7kX4E7JKikUZCnpC6ULWeFJtXO/LIWUAEUN8v35DFMqDKeR7niTw9srmqrSk5nhbyi5Whu+v7SDagO3pJuhGQNqvVr7nxHiqMKXhZpGCXDYo2p5y8LDxJf3dlfQ/WfvEJcil1hWbd2oP2iz1Q+u/WhBD/vOEhCJQHC6AYx72D7zuwAuci9FJ0t+s59O3LW8lqHlmQAaQ59hBOOvCJYJiX+MtCqRq0inDqrpvI0iMW7XQz60YML0Db5Uk5qeZf4TV+4kRLFpbSOLFLR7DP2AmT3syAVaijZYHW/41GvbDbrRf1LG/5IY189UXpsa9TT+zg/QgJAnhkiuhyAC7qE/BZL3KqpN0eR3edVhoieKRCLZHj53j2nNtXQ2+mvVrHBc+zgHirnYzQ9yHqkqBq/v5mA4HMNmbKYH6da7C5784HskQsvSyifeSPji3baON52+Y/fYsOmlXAs7eHtTrt/H9kc1GlscAIvyrM9gC5tnuRmhCWSSmYiXZ9DpqYawb7gLhKoMjmjkcCzwhuulS85lY0EDw0SO63znxxY8+Mgd8gZdfnMTY6Yjq298agFSMwxUjI20A3UmFGLFkBCXDQVYTmLjGk5WWWSdTsWVRtggQX/Js88rWWrG0g8Bv/jKdMH9ArlZpOSGRF2svZ7nQ8ShHylZfLCUVnXSxikumTO5S9y1247qm8CHlX+Nsbs+C5MBOTYjE6nY5h+NoLIlq0RqvlkNCq6FluQOaiKVoK0H7kjd74mD8/KxXfXtFHZIscKJfJJv8Bli21kuj7WSOraj9d1DqA0 gxWHE+lK moISdpV7xZN0ZZtvbqfZ+l/2eGG1jgXleHwZfOEHmABJ4PrtXiAjHLtrDP6dRAChgqnLtgatGw9fMmJmnV4V7hRmq1ipBqqmLmGH9U2PoUhPrTfth0GI6Cl4w29XZaXQ/J0zVEEBu+ECGhcqAlb36k21HsI9PPnp+q8y+ks91TkNsjnUsdbm3un+aFpfarPxKm6uE2OSten0ALNw5qjFCvK9FSis3WEg/NJUT+cpVb0wA6jS84N+V+22WMB7Lij2NPlpeB+14nOGjH0o+kCgIRG9unEepo8qgypjXbNEHdNQQiVNjQYnYu/YjZryyfRPDAWfJSipuHtZmfPUigtlFxI1uPwjdP7Pw6pMRqpk+Jnd6lnemYlSNiPwXRJpP/00RBpaq X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: Version 2 of GHCB specification added support for the SNP Extended Guest Request Message NAE event. This event serves a nearly identical purpose to the previously-added SNP_GUEST_REQUEST event, but allows for additional certificate data to be supplied via an additional guest-supplied buffer to be used mainly for verifying the signature of an attestation report as returned by firmware. This certificate data is supplied by userspace, so unlike with SNP_GUEST_REQUEST events, SNP_EXTENDED_GUEST_REQUEST events are first forwarded to userspace via a KVM_EXIT_VMGEXIT exit structure, and then the firmware request is made after the certificate data has been fetched from userspace. Since there is a potential for race conditions where the userspace-supplied certificate data may be out-of-sync relative to the reported TCB or VLEK that firmware will use when signing attestation reports, a hook is also provided so that userspace can be informed once the attestation request is actually completed. See the updates to Documentation/ for more details on these aspects. Signed-off-by: Michael Roth --- Documentation/virt/kvm/api.rst | 87 ++++++++++++++++++++++++++++++++++ arch/x86/kvm/svm/sev.c | 86 +++++++++++++++++++++++++++++++++ arch/x86/kvm/svm/svm.h | 3 ++ include/uapi/linux/kvm.h | 23 +++++++++ 4 files changed, 199 insertions(+) diff --git a/Documentation/virt/kvm/api.rst b/Documentation/virt/kvm/api.rst index f0b76ff5030d..f3780ac98d56 100644 --- a/Documentation/virt/kvm/api.rst +++ b/Documentation/virt/kvm/api.rst @@ -7060,6 +7060,93 @@ Please note that the kernel is allowed to use the kvm_run structure as the primary storage for certain register types. Therefore, the kernel may use the values in kvm_run even if the corresponding bit in kvm_dirty_regs is not set. +:: + + /* KVM_EXIT_VMGEXIT */ + struct kvm_user_vmgexit { + #define KVM_USER_VMGEXIT_REQ_CERTS 1 + __u32 type; /* KVM_USER_VMGEXIT_* type */ + union { + struct { + __u64 data_gpa; + __u64 data_npages; + #define KVM_USER_VMGEXIT_REQ_CERTS_ERROR_INVALID_LEN 1 + #define KVM_USER_VMGEXIT_REQ_CERTS_ERROR_BUSY 2 + #define KVM_USER_VMGEXIT_REQ_CERTS_ERROR_GENERIC (1 << 31) + __u32 ret; + #define KVM_USER_VMGEXIT_REQ_CERTS_FLAGS_NOTIFY_DONE BIT(0) + __u8 flags; + #define KVM_USER_VMGEXIT_REQ_CERTS_STATUS_PENDING 0 + #define KVM_USER_VMGEXIT_REQ_CERTS_STATUS_DONE 1 + __u8 status; + } req_certs; + }; + }; + + +If exit reason is KVM_EXIT_VMGEXIT then it indicates that an SEV-SNP guest +has issued a VMGEXIT instruction (as documented by the AMD Architecture +Programmer's Manual (APM)) to the hypervisor that needs to be serviced by +userspace. These are generally handled by the host kernel, but in some +cases some aspects of handling a VMGEXIT are done in userspace. + +A kvm_user_vmgexit structure is defined to encapsulate the data to be +sent to or returned by userspace. The type field defines the specific type +of exit that needs to be serviced, and that type is used as a discriminator +to determine which union type should be used for input/output. + +KVM_USER_VMGEXIT_REQ_CERTS +-------------------------- + +When an SEV-SNP issues a guest request for an attestation report, it has the +option of issuing it in the form an *extended* guest request when a +certificate blob is returned alongside the attestation report so the guest +can validate the endorsement key used by SNP firmware to sign the report. +These certificates are managed by userspace and are requested via +KVM_EXIT_VMGEXITs using the KVM_USER_VMGEXIT_REQ_CERTS type. + +For the KVM_USER_VMGEXIT_REQ_CERTS type, the req_certs union type +is used. The kernel will supply in 'data_gpa' the value the guest supplies +via the RAX field of the GHCB when issuing extended guest requests. +'data_npages' will similarly contain the value the guest supplies in RBX +denoting the number of shared pages available to write the certificate +data into. + + - If the supplied number of pages is sufficient, userspace should write + the certificate data blob (in the format defined by the GHCB spec) in + the address indicated by 'data_gpa' and set 'ret' to 0. + + - If the number of pages supplied is not sufficient, userspace must write + the required number of pages in 'data_npages' and then set 'ret' to 1. + + - If userspace is temporarily unable to handle the request, 'ret' should + be set to 2 to inform the guest to retry later. + + - If some other error occurred, userspace should set 'ret' to a non-zero + value that is distinct from the specific return values mentioned above. + +Generally some care needs be taken to keep the returned certificate data in +sync with the actual endorsement key in use by firmware at the time the +attestation request is sent to SNP firmware. The recommended scheme to do +this is for the VMM to obtain a shared or exclusive lock on the path the +certificate blob file resides at before reading it and returning it to KVM, +and that it continues to hold the lock until the attestation request is +actually sent to firmware. To facilitate this, the VMM can set the +KVM_USER_VMGEXIT_REQ_CERTS_FLAGS_NOTIFY_DONE flag before returning the +certificate blob, in which case another KVM_EXIT_VMGEXIT of type +KVM_USER_VMGEXIT_REQ_CERTS will be sent to userspace with +KVM_USER_VMGEXIT_REQ_CERTS_STATUS_DONE being set in the status field to +indicate the request is fully-completed and that any associated locks can be +released. + +Tools/libraries that perform updates to SNP firmware TCB values or endorsement +keys (e.g. firmware interfaces such as SNP_COMMIT, SNP_SET_CONFIG, or +SNP_VLEK_LOAD, see Documentation/virt/coco/sev-guest.rst for more details) in +such a way that the certificate blob needs to be updated, should similarly +take an exclusive lock on the certificate blob for the duration of any updates +to firmware or the certificate blob contents to ensure that VMMs using the +above scheme will not return certificate blob data that is out of sync with +firmware. 6. Capabilities that can be enabled on vCPUs ============================================ diff --git a/arch/x86/kvm/svm/sev.c b/arch/x86/kvm/svm/sev.c index 5c6262f3232f..35f0bd91f92e 100644 --- a/arch/x86/kvm/svm/sev.c +++ b/arch/x86/kvm/svm/sev.c @@ -3297,6 +3297,11 @@ static int sev_es_validate_vmgexit(struct vcpu_svm *svm) if (!sev_snp_guest(vcpu->kvm)) goto vmgexit_err; break; + case SVM_VMGEXIT_EXT_GUEST_REQUEST: + if (!sev_snp_guest(vcpu->kvm) || !kvm_ghcb_rax_is_valid(svm) || + !kvm_ghcb_rbx_is_valid(svm)) + goto vmgexit_err; + break; default: reason = GHCB_ERR_INVALID_EVENT; goto vmgexit_err; @@ -3988,6 +3993,84 @@ static void snp_handle_guest_req(struct vcpu_svm *svm, gpa_t req_gpa, gpa_t resp ghcb_set_sw_exit_info_2(svm->sev_es.ghcb, SNP_GUEST_ERR(vmm_ret, fw_err)); } +static int snp_complete_ext_guest_req(struct kvm_vcpu *vcpu) +{ + struct vcpu_svm *svm = to_svm(vcpu); + struct vmcb_control_area *control; + struct kvm *kvm = vcpu->kvm; + sev_ret_code fw_err = 0; + int vmm_ret; + + vmm_ret = vcpu->run->vmgexit.req_certs.ret; + if (vmm_ret) { + if (vmm_ret == SNP_GUEST_VMM_ERR_INVALID_LEN) + vcpu->arch.regs[VCPU_REGS_RBX] = + vcpu->run->vmgexit.req_certs.data_npages; + goto out; + } + + /* + * The request was completed on the previous completion callback and + * this completion is only for the STATUS_DONE userspace notification. + */ + if (vcpu->run->vmgexit.req_certs.status == KVM_USER_VMGEXIT_REQ_CERTS_STATUS_DONE) + goto out_resume; + + control = &svm->vmcb->control; + + if (__snp_handle_guest_req(kvm, control->exit_info_1, + control->exit_info_2, &fw_err)) + vmm_ret = SNP_GUEST_VMM_ERR_GENERIC; + +out: + ghcb_set_sw_exit_info_2(svm->sev_es.ghcb, SNP_GUEST_ERR(vmm_ret, fw_err)); + + if (vcpu->run->vmgexit.req_certs.flags & KVM_USER_VMGEXIT_REQ_CERTS_FLAGS_NOTIFY_DONE) { + vcpu->run->vmgexit.req_certs.status = KVM_USER_VMGEXIT_REQ_CERTS_STATUS_DONE; + vcpu->run->vmgexit.req_certs.flags = 0; + return 0; /* notify userspace of completion */ + } + +out_resume: + return 1; /* resume guest */ +} + +static int snp_begin_ext_guest_req(struct kvm_vcpu *vcpu) +{ + int vmm_ret = SNP_GUEST_VMM_ERR_GENERIC; + struct vcpu_svm *svm = to_svm(vcpu); + unsigned long data_npages; + sev_ret_code fw_err; + gpa_t data_gpa; + + if (!sev_snp_guest(vcpu->kvm)) + goto abort_request; + + data_gpa = vcpu->arch.regs[VCPU_REGS_RAX]; + data_npages = vcpu->arch.regs[VCPU_REGS_RBX]; + + if (!IS_ALIGNED(data_gpa, PAGE_SIZE)) + goto abort_request; + + /* + * Grab the certificates from userspace so that can be bundled with + * attestation/guest requests. + */ + vcpu->run->exit_reason = KVM_EXIT_VMGEXIT; + vcpu->run->vmgexit.type = KVM_USER_VMGEXIT_REQ_CERTS; + vcpu->run->vmgexit.req_certs.data_gpa = data_gpa; + vcpu->run->vmgexit.req_certs.data_npages = data_npages; + vcpu->run->vmgexit.req_certs.flags = 0; + vcpu->run->vmgexit.req_certs.status = KVM_USER_VMGEXIT_REQ_CERTS_STATUS_PENDING; + vcpu->arch.complete_userspace_io = snp_complete_ext_guest_req; + + return 0; /* forward request to userspace */ + +abort_request: + ghcb_set_sw_exit_info_2(svm->sev_es.ghcb, SNP_GUEST_ERR(vmm_ret, fw_err)); + return 1; /* resume guest */ +} + static int sev_handle_vmgexit_msr_protocol(struct vcpu_svm *svm) { struct vmcb_control_area *control = &svm->vmcb->control; @@ -4266,6 +4349,9 @@ int sev_handle_vmgexit(struct kvm_vcpu *vcpu) snp_handle_guest_req(svm, control->exit_info_1, control->exit_info_2); ret = 1; break; + case SVM_VMGEXIT_EXT_GUEST_REQUEST: + ret = snp_begin_ext_guest_req(vcpu); + break; case SVM_VMGEXIT_UNSUPPORTED_EVENT: vcpu_unimpl(vcpu, "vmgexit: unsupported event - exit_info_1=%#llx, exit_info_2=%#llx\n", diff --git a/arch/x86/kvm/svm/svm.h b/arch/x86/kvm/svm/svm.h index e325ede0f463..83c562b4712a 100644 --- a/arch/x86/kvm/svm/svm.h +++ b/arch/x86/kvm/svm/svm.h @@ -309,6 +309,9 @@ struct vcpu_svm { /* Guest GIF value, used when vGIF is not enabled */ bool guest_gif; + + /* Transaction ID associated with SNP config updates */ + u64 snp_transaction_id; }; struct svm_cpu_data { diff --git a/include/uapi/linux/kvm.h b/include/uapi/linux/kvm.h index 2190adbe3002..106367d87189 100644 --- a/include/uapi/linux/kvm.h +++ b/include/uapi/linux/kvm.h @@ -135,6 +135,26 @@ struct kvm_xen_exit { } u; }; +struct kvm_user_vmgexit { +#define KVM_USER_VMGEXIT_REQ_CERTS 1 + __u32 type; /* KVM_USER_VMGEXIT_* type */ + union { + struct { + __u64 data_gpa; + __u64 data_npages; +#define KVM_USER_VMGEXIT_REQ_CERTS_ERROR_INVALID_LEN 1 +#define KVM_USER_VMGEXIT_REQ_CERTS_ERROR_BUSY 2 +#define KVM_USER_VMGEXIT_REQ_CERTS_ERROR_GENERIC (1 << 31) + __u32 ret; +#define KVM_USER_VMGEXIT_REQ_CERTS_FLAGS_NOTIFY_DONE BIT(0) + __u8 flags; +#define KVM_USER_VMGEXIT_REQ_CERTS_STATUS_PENDING 0 +#define KVM_USER_VMGEXIT_REQ_CERTS_STATUS_DONE 1 + __u8 status; + } req_certs; + }; +}; + #define KVM_S390_GET_SKEYS_NONE 1 #define KVM_S390_SKEYS_MAX 1048576 @@ -178,6 +198,7 @@ struct kvm_xen_exit { #define KVM_EXIT_NOTIFY 37 #define KVM_EXIT_LOONGARCH_IOCSR 38 #define KVM_EXIT_MEMORY_FAULT 39 +#define KVM_EXIT_VMGEXIT 40 /* For KVM_EXIT_INTERNAL_ERROR */ /* Emulate instruction failed. */ @@ -433,6 +454,8 @@ struct kvm_run { __u64 gpa; __u64 size; } memory_fault; + /* KVM_EXIT_VMGEXIT */ + struct kvm_user_vmgexit vmgexit; /* Fix the size of the union. */ char padding[256]; };