From patchwork Wed May 1 08:51:57 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Michael Roth X-Patchwork-Id: 13650351 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 B0929C4345F for ; Wed, 1 May 2024 09:09:16 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 47F376B00BC; Wed, 1 May 2024 05:09:16 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 456BD6B00BD; Wed, 1 May 2024 05:09:16 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 2D0946B00BE; Wed, 1 May 2024 05:09:16 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0010.hostedemail.com [216.40.44.10]) by kanga.kvack.org (Postfix) with ESMTP id 0BDA86B00BC for ; Wed, 1 May 2024 05:09:16 -0400 (EDT) Received: from smtpin07.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id B6C37A167E for ; Wed, 1 May 2024 09:09:15 +0000 (UTC) X-FDA: 82069253070.07.A53AEB4 Received: from NAM12-BN8-obe.outbound.protection.outlook.com (mail-bn8nam12on2077.outbound.protection.outlook.com [40.107.237.77]) by imf15.hostedemail.com (Postfix) with ESMTP id CEB31A0017 for ; Wed, 1 May 2024 09:09:11 +0000 (UTC) Authentication-Results: imf15.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=dtwkBlX+; spf=pass (imf15.hostedemail.com: domain of Michael.Roth@amd.com designates 40.107.237.77 as permitted sender) smtp.mailfrom=Michael.Roth@amd.com; arc=pass ("microsoft.com:s=arcselector9901:i=1"); dmarc=pass (policy=quarantine) header.from=amd.com ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1714554551; 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=dYQYofULTsvq+RGAK48Bsi2yQW2HzXb5HOdHe6aXetE=; b=huRyapUCU1hQ0iq+dlSlOmZaLTsaOG2osNg2rqy9F2lBhjT8kFScS9TOhoC9cBRQaIlf1+ soMWd9zaYpLkpV6XikzZrzKzwhmIVy0Yjz/xuhC5O8Ev/ni1EcbimQEMk+MX7VGnXr2wVB gyL0ag/opEgWsP+iyKH10gbD4h5EBdw= ARC-Authentication-Results: i=2; imf15.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=dtwkBlX+; spf=pass (imf15.hostedemail.com: domain of Michael.Roth@amd.com designates 40.107.237.77 as permitted sender) smtp.mailfrom=Michael.Roth@amd.com; arc=pass ("microsoft.com:s=arcselector9901:i=1"); dmarc=pass (policy=quarantine) header.from=amd.com ARC-Seal: i=2; s=arc-20220608; d=hostedemail.com; t=1714554551; a=rsa-sha256; cv=pass; b=nU49hnms76JXTxaHr6JbiRxs4lLzDZkhm4n4X2HpgVyyjXctx6sAAHyAdr//5asb+QI57d xNBUWnDmNpPt2nEfpdZOmWiBnUCvmVh8y09MvUEYB0XU6X+9btUsJlQdERSuxc8a2quxyY mFETqeQXuYeHXgV1QdQRQ6M3wWNGSYc= ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=SAl0Q8JY0GCH40JK+81sA+6jxeK3gsIJoUi4qed+Ee0lz1c65tF+VbDNbUNHtHjL7ijD0SbH3w54opLxXF+pisYC7u2eSZpAdgRG20+mRmT58LztSfwMncLJSUdqWvuTV6pSa3M7cplbOIoBGhehEKOjneKMDjwBXHeteasSg1LsMUaJrOd9AM0esbe2FegUmbM+DyUbekjRuJkg/Cw8y2f9Q132kkgIEuTBtx8jkoDR+AuLDg5lyyU/yzCKmFqS083Ds/MQUF5gTWYmplII0EVSEjm0Hc4JnR+/balXzZfcmBtqCTPAqg8IT+rvkldgBFWLYzeVWe2/ebzcEYh7Yg== 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=dYQYofULTsvq+RGAK48Bsi2yQW2HzXb5HOdHe6aXetE=; b=J8L/pyVOK9vKZGde06f3kT1ftJhwPTyA2f27OhMxB1p5/pEgACbegtmDa9po8RrB4Cgf3taA6y58xQ5ar1ZIEroFuPCsEACAdHMmUd7A7YQCxYYFVJYrClTvZy2qMn4jhD81I400K11Xj60HoATOXZYsUFTBmZKQh4Hhys2Q5baAWxg0xBY8QOFk8V7GyvmT1KdETmGRTpdlPbNjZnWhppk3Ok8NMIeBD/Do/tFHLtU0RZSKZ6bkTV7QilMN4f+/TJLS+bwCcmhu3NpmSRwBaoRrdRwP+xBqO8j6aYk+Dhduf9yps2Tep5tXeoiXb5KK3Xbqc7EG6iwG0/ra5g8Gng== 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=dYQYofULTsvq+RGAK48Bsi2yQW2HzXb5HOdHe6aXetE=; b=dtwkBlX+5bkBElTr6Wj+jvmJyoNUiRLK89bfKXhM2BwnzaYpzjqSpuY0j4/znIPrakRd0AgkAdegUTNbi4AXzh2HG1eU8cPiWH+oNu3mvsefgHZtsbBTYxxTOP4CoX70RzokWWlEJRzlAityLiKrFADB7rmHFF2IPzA+focIMig= Received: from BN9PR03CA0601.namprd03.prod.outlook.com (2603:10b6:408:106::6) by IA1PR12MB7733.namprd12.prod.outlook.com (2603:10b6:208:423::17) 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:09:08 +0000 Received: from BN2PEPF0000449E.namprd02.prod.outlook.com (2603:10b6:408:106:cafe::8c) by BN9PR03CA0601.outlook.office365.com (2603:10b6:408:106::6) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7544.29 via Frontend Transport; Wed, 1 May 2024 09:09:08 +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 BN2PEPF0000449E.mail.protection.outlook.com (10.167.243.149) 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:09:08 +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:09:07 -0500 From: Michael Roth To: CC: , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , Brijesh Singh , Harald Hoyer Subject: [PATCH v15 07/20] KVM: SEV: Add KVM_SEV_SNP_LAUNCH_FINISH command Date: Wed, 1 May 2024 03:51:57 -0500 Message-ID: <20240501085210.2213060-8-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: BN2PEPF0000449E:EE_|IA1PR12MB7733:EE_ X-MS-Office365-Filtering-Correlation-Id: 794f380b-3a05-4235-0915-08dc69be5bc1 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0;ARA:13230031|1800799015|36860700004|82310400014|376005|7416005; X-Microsoft-Antispam-Message-Info: sWCowbIWfbJmJGNSiizYeGpGUwCk0iftfK0FREu4oVMlqcd6nYhes/xdE4XF+dduVXbj8oiLce1A83aG1MMufiQFoM3//CxkhkjrzbIcxPuze2qRgSGC2d5NWbaPCnhOLtWetburlDY8OGuygESDcABKmu7nIGQvgI32b8vkIUyPXeBEUJWgrfQpb3h1r8iH7WevHzM+YhWgriCei8ajLXFQo5q0R4ELG6Hd93Yhxkxo2DwbqzDNabPx6bYdEDSSrj7VpcQu7YwBCYvUSClNb60J1ae2hOowaW6kYmvK9UA+F5a1YwVEWBofwStwtW7cCf1a/MqGDPTaNPoSlHnPQ6k6Vr5EtA6+gCoPDpDqN5tdVs8YcnH3/N342bDJDhV81NvbaC5yWmJV0Cj1zmdmVY+RjGPbh7uLyjUDv/WBtoI4e5tqq26XJgpP+su5kweo5LDEYZZJsIrZcNNTPjzuf8t6u4JQgkVyn0rLUH4yVE3dF7hqMpf4tF+PfgppZuWlBV2D/afKvaDwKaMgbwCYPWUk/AF3soyV395E11N8G5HQeylkKSqm6KLj9q/ZytSxCOBrVNxJIu6/HZF9RgX14EuaRIir7r4vVrBNQySJbMWjIUsMMHzZxPr78w08y5FeAgGt4hPvPfLk/7RovsKLgGpxPcGKcCjgV1lcVfkpyez7jyS1s0bM0ul9NwzCsSFQ0abW/iqh4zMelS3FpwgGx9B705zmuTCRsr5sW+RlHPIFLfD1fe4ZptzqTA1SGSPw1nJURL7+GK9l1Li9B1oVQTMteHW2ufTjaPTySduepKIZS9v91ZPPAQKHCi96iWo0iLWv5b6USgzgZpVeJbYflBY5MtaxZ2wdxmIrigyX4+kpiagt8NvnJ288biRONW2SUtk80CxjXJUaexPzMP55pCs0y08XstQ6WJmLPCZw6GCr2uYHADdXOj26SkLQeWEhbS2oIG5BC5/udDmDgpz+PRdIrYRdM9+X9zy4JtT6jHubaWTS0DhrYYQXbnmjHWVmYKKphRKoj2+bL4/MVmmPnHuz3ujgG4PEK3td33d44JrAQYP0WVJdXozFwY9RXdTQNxw5uj72lxP+b6cQX5R1kKwoNPISn0NyUNKliUMZAZNjE0n0cpcE30+uAOT80Ioeo6jwBNS0n+B9Ph0ZcKxn56nKfHRlfF2OAwdeuPZTPrh6y2+zVM0D+pmJOoImO9y4SFE9Gmay6GrnH6JOZrc3hqoULPNTH0ehVPC26RLguKknnpmBG4hF2zeaWQsdfbvMXRrLw5T9ofh9SDFiCtiMlQQebufowdzfw8iPcYrLuI7wrLj85GUJ2/6eei1VxDv1zR0QJX8n4IKtJypK2houSs8QbDcGxoL963LcQK6CIBWgCA28s4cXHevzu9Oib/WG 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)(1800799015)(36860700004)(82310400014)(376005)(7416005);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 01 May 2024 09:09:08.3066 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 794f380b-3a05-4235-0915-08dc69be5bc1 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: BN2PEPF0000449E.namprd02.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: IA1PR12MB7733 X-Rspam-User: X-Rspamd-Queue-Id: CEB31A0017 X-Rspamd-Server: rspam06 X-Stat-Signature: zuapgktye6cxzehm6ssx4a8t3bay8a4x X-HE-Tag: 1714554551-380450 X-HE-Meta: U2FsdGVkX18XrWykJmGtBCpzFzASd4wUjhqyWFVsAOjPKVn1cGVS1wxTvHWpxwr9yDtfuD1IU/sqtyjHtvUgIWnPqNJIwxhv2o3PiDIsZ5Jojg79ayVRJOf9wf0zKFSH7QZZNMe8fymw4nz//qbJkKuznONmXRYdiwpq6mGQGXyn2ZHoLEYIU5CqFUt1swF1Li690I4KSnCwQnAaN0a338Gf/0Acr8SB+nagpJ8USOxB73PPv1HX3erOiFi7lNS3ocockgPSDMZPtf7EFOOMCXCQf82xRs/DvwxTry/jF0sNnnZmXK1/e4R2JPJh+ixYNyytIkpgECvhXNLSnB6XfVcQeO1LYZafz2cpVrI3v5NfdG9HYP4GMB+khAAuuaf3PY3rTzV+r+/rxhcwpHBohqD/61/nh2zIy3dt+tMy0pczrJdyXMy7ZWI/3INHVfx4aRXDMeR5XN/brjUtqxds6dkIFCOU8gjGVaGC2DBz94ZNDdZH/qsf2Qox1nVzhdpg3RoEx5W2g70tI0/X1tj1c1PnK/mg+LFewUNCfvh5N2bdzZRdswHx6QNLLbuZeWXG9mUaQMKclymtUC5nqag068gJai738eInPvpRrDqc4YtVxIfGuG6XpDt408koARObkV94kT5bSOUrlVqLi/hPHOboZFMaDavbcLRPbs+9NdiK/HR0J9/LNuIXryQSXXbP+LO7Y5Mq+M2fIuIHdpzqkxjvUPGA9p3N69ljFUae8BZBLOyj87cD9OJGMb0Z7zkL6P1j0QuBkiVjDmNETddGDrwEA0Kt1099FuqNCU6BJCs83RV2AbOcbqhTEd1qJxBWxfvLPfW7cae3bEfF3h4tQWIeVu+sxvnRx9wT6nfBuz6FBP5KFJaXLw/9XrdeFREbhfn+E3sf2WNGOU8FNUkqR+s1KRqmZQHcBgkZRPe0QcCPx88CbPxQb0QkEAZjJppZ9oiSDmvi2Mz5dn2n/te ucgYyMDL Soitv8zjjtLzQq/4bnbHSaopd5WjYF69jaCSLxbaR+Dr7lXdVThxV4Hk8EBZCgg+taxnBWjf2QKW31oDZGzoZmlFqZRcKXA7R989s/6pdFTbes21bZUv/x2Nn2OwMFIIZ9GdMlVyXSl0we4DsMTCG1HiSIxjpKwY2EDErcNadPtlLvswxYgzxxE4qk3a21f3bxC5wo4Aj8NqGbe2GovJigdwehMmA2JM6L3fTAZ/AWOXIx4W6vSxp4jAQT5af0ZZzqYLHJn6BYAvBhJPeZW0DRCPJWuGijx4ngGtCpWvkniFDVEsk0pD/jkm/ZEVW15sAqWninLzw8Zg0mT9sAoP0c796Y6H76Diqh3QptD/C0dGjHOrpC1tRou5+FHBcmEPiQpoBrHtik9MTAQCxtAuXSORpBLWjYEuezBBo4rFlfrRQffpwallyihPI6yolv+hpWeMe 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: From: Brijesh Singh Add a KVM_SEV_SNP_LAUNCH_FINISH command to finalize the cryptographic launch digest which stores the measurement of the guest at launch time. Also extend the existing SNP firmware data structures to support disabling the use of Versioned Chip Endorsement Keys (VCEK) by guests as part of this command. While finalizing the launch flow, the code also issues the LAUNCH_UPDATE SNP firmware commands to encrypt/measure the initial VMSA pages for each configured vCPU, which requires setting the RMP entries for those pages to private, so also add handling to clean up the RMP entries for these pages whening freeing vCPUs during shutdown. Signed-off-by: Brijesh Singh Co-developed-by: Michael Roth Signed-off-by: Michael Roth Signed-off-by: Harald Hoyer Signed-off-by: Ashish Kalra --- .../virt/kvm/x86/amd-memory-encryption.rst | 28 ++++ arch/x86/include/uapi/asm/kvm.h | 17 +++ arch/x86/kvm/svm/sev.c | 127 ++++++++++++++++++ include/linux/psp-sev.h | 4 +- 4 files changed, 175 insertions(+), 1 deletion(-) diff --git a/Documentation/virt/kvm/x86/amd-memory-encryption.rst b/Documentation/virt/kvm/x86/amd-memory-encryption.rst index cc16a7426d18..1ddb6a86ce7f 100644 --- a/Documentation/virt/kvm/x86/amd-memory-encryption.rst +++ b/Documentation/virt/kvm/x86/amd-memory-encryption.rst @@ -544,6 +544,34 @@ where the allowed values for page_type are #define'd as:: See the SEV-SNP spec [snp-fw-abi]_ for further details on how each page type is used/measured. +20. KVM_SEV_SNP_LAUNCH_FINISH +----------------------------- + +After completion of the SNP guest launch flow, the KVM_SEV_SNP_LAUNCH_FINISH +command can be issued to make the guest ready for execution. + +Parameters (in): struct kvm_sev_snp_launch_finish + +Returns: 0 on success, -negative on error + +:: + + struct kvm_sev_snp_launch_finish { + __u64 id_block_uaddr; + __u64 id_auth_uaddr; + __u8 id_block_en; + __u8 auth_key_en; + __u8 vcek_disabled; + __u8 host_data[32]; + __u8 pad0[3]; + __u16 flags; /* Must be zero */ + __u64 pad1[4]; + }; + + +See SNP_LAUNCH_FINISH in the SEV-SNP specification [snp-fw-abi]_ for further +details on the input parameters in ``struct kvm_sev_snp_launch_finish``. + Device attribute API ==================== diff --git a/arch/x86/include/uapi/asm/kvm.h b/arch/x86/include/uapi/asm/kvm.h index 5935dc8a7e02..988b5204d636 100644 --- a/arch/x86/include/uapi/asm/kvm.h +++ b/arch/x86/include/uapi/asm/kvm.h @@ -700,6 +700,7 @@ enum sev_cmd_id { /* SNP-specific commands */ KVM_SEV_SNP_LAUNCH_START = 100, KVM_SEV_SNP_LAUNCH_UPDATE, + KVM_SEV_SNP_LAUNCH_FINISH, KVM_SEV_NR_MAX, }; @@ -854,6 +855,22 @@ struct kvm_sev_snp_launch_update { __u64 pad2[4]; }; +#define KVM_SEV_SNP_ID_BLOCK_SIZE 96 +#define KVM_SEV_SNP_ID_AUTH_SIZE 4096 +#define KVM_SEV_SNP_FINISH_DATA_SIZE 32 + +struct kvm_sev_snp_launch_finish { + __u64 id_block_uaddr; + __u64 id_auth_uaddr; + __u8 id_block_en; + __u8 auth_key_en; + __u8 vcek_disabled; + __u8 host_data[KVM_SEV_SNP_FINISH_DATA_SIZE]; + __u8 pad0[3]; + __u16 flags; + __u64 pad1[4]; +}; + #define KVM_X2APIC_API_USE_32BIT_IDS (1ULL << 0) #define KVM_X2APIC_API_DISABLE_BROADCAST_QUIRK (1ULL << 1) diff --git a/arch/x86/kvm/svm/sev.c b/arch/x86/kvm/svm/sev.c index f31f87655a67..797230f810f8 100644 --- a/arch/x86/kvm/svm/sev.c +++ b/arch/x86/kvm/svm/sev.c @@ -75,6 +75,8 @@ static u64 sev_supported_vmsa_features; SNP_POLICY_MASK_DEBUG | \ SNP_POLICY_MASK_SINGLE_SOCKET) +#define INITIAL_VMSA_GPA 0xFFFFFFFFF000 + static u8 sev_enc_bit; static DECLARE_RWSEM(sev_deactivate_lock); static DEFINE_MUTEX(sev_bitmap_lock); @@ -2348,6 +2350,115 @@ static int snp_launch_update(struct kvm *kvm, struct kvm_sev_cmd *argp) return ret; } +static int snp_launch_update_vmsa(struct kvm *kvm, struct kvm_sev_cmd *argp) +{ + struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info; + struct sev_data_snp_launch_update data = {}; + struct kvm_vcpu *vcpu; + unsigned long i; + int ret; + + data.gctx_paddr = __psp_pa(sev->snp_context); + data.page_type = SNP_PAGE_TYPE_VMSA; + + kvm_for_each_vcpu(i, vcpu, kvm) { + struct vcpu_svm *svm = to_svm(vcpu); + u64 pfn = __pa(svm->sev_es.vmsa) >> PAGE_SHIFT; + + ret = sev_es_sync_vmsa(svm); + if (ret) + return ret; + + /* Transition the VMSA page to a firmware state. */ + ret = rmp_make_private(pfn, INITIAL_VMSA_GPA, PG_LEVEL_4K, sev->asid, true); + if (ret) + return ret; + + /* Issue the SNP command to encrypt the VMSA */ + data.address = __sme_pa(svm->sev_es.vmsa); + ret = __sev_issue_cmd(argp->sev_fd, SEV_CMD_SNP_LAUNCH_UPDATE, + &data, &argp->error); + if (ret) { + if (!snp_page_reclaim(pfn)) + host_rmp_make_shared(pfn, PG_LEVEL_4K); + + return ret; + } + + svm->vcpu.arch.guest_state_protected = true; + } + + return 0; +} + +static int snp_launch_finish(struct kvm *kvm, struct kvm_sev_cmd *argp) +{ + struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info; + struct kvm_sev_snp_launch_finish params; + struct sev_data_snp_launch_finish *data; + void *id_block = NULL, *id_auth = NULL; + int ret; + + if (!sev_snp_guest(kvm)) + return -ENOTTY; + + if (!sev->snp_context) + return -EINVAL; + + if (copy_from_user(¶ms, u64_to_user_ptr(argp->data), sizeof(params))) + return -EFAULT; + + if (params.flags) + return -EINVAL; + + /* Measure all vCPUs using LAUNCH_UPDATE before finalizing the launch flow. */ + ret = snp_launch_update_vmsa(kvm, argp); + if (ret) + return ret; + + data = kzalloc(sizeof(*data), GFP_KERNEL_ACCOUNT); + if (!data) + return -ENOMEM; + + if (params.id_block_en) { + id_block = psp_copy_user_blob(params.id_block_uaddr, KVM_SEV_SNP_ID_BLOCK_SIZE); + if (IS_ERR(id_block)) { + ret = PTR_ERR(id_block); + goto e_free; + } + + data->id_block_en = 1; + data->id_block_paddr = __sme_pa(id_block); + + id_auth = psp_copy_user_blob(params.id_auth_uaddr, KVM_SEV_SNP_ID_AUTH_SIZE); + if (IS_ERR(id_auth)) { + ret = PTR_ERR(id_auth); + goto e_free_id_block; + } + + data->id_auth_paddr = __sme_pa(id_auth); + + if (params.auth_key_en) + data->auth_key_en = 1; + } + + data->vcek_disabled = params.vcek_disabled; + + memcpy(data->host_data, params.host_data, KVM_SEV_SNP_FINISH_DATA_SIZE); + data->gctx_paddr = __psp_pa(sev->snp_context); + ret = sev_issue_cmd(kvm, SEV_CMD_SNP_LAUNCH_FINISH, data, &argp->error); + + kfree(id_auth); + +e_free_id_block: + kfree(id_block); + +e_free: + kfree(data); + + return ret; +} + int sev_mem_enc_ioctl(struct kvm *kvm, void __user *argp) { struct kvm_sev_cmd sev_cmd; @@ -2450,6 +2561,9 @@ int sev_mem_enc_ioctl(struct kvm *kvm, void __user *argp) case KVM_SEV_SNP_LAUNCH_UPDATE: r = snp_launch_update(kvm, &sev_cmd); break; + case KVM_SEV_SNP_LAUNCH_FINISH: + r = snp_launch_finish(kvm, &sev_cmd); + break; default: r = -EINVAL; goto out; @@ -2940,11 +3054,24 @@ void sev_free_vcpu(struct kvm_vcpu *vcpu) svm = to_svm(vcpu); + /* + * If it's an SNP guest, then the VMSA was marked in the RMP table as + * a guest-owned page. Transition the page to hypervisor state before + * releasing it back to the system. + */ + if (sev_snp_guest(vcpu->kvm)) { + u64 pfn = __pa(svm->sev_es.vmsa) >> PAGE_SHIFT; + + if (host_rmp_make_shared(pfn, PG_LEVEL_4K)) + goto skip_vmsa_free; + } + if (vcpu->arch.guest_state_protected) sev_flush_encrypted_page(vcpu, svm->sev_es.vmsa); __free_page(virt_to_page(svm->sev_es.vmsa)); +skip_vmsa_free: if (svm->sev_es.ghcb_sa_free) kvfree(svm->sev_es.ghcb_sa); } diff --git a/include/linux/psp-sev.h b/include/linux/psp-sev.h index 3705c2044fc0..903ddfea8585 100644 --- a/include/linux/psp-sev.h +++ b/include/linux/psp-sev.h @@ -658,6 +658,7 @@ struct sev_data_snp_launch_update { * @id_auth_paddr: system physical address of ID block authentication structure * @id_block_en: indicates whether ID block is present * @auth_key_en: indicates whether author key is present in authentication structure + * @vcek_disabled: indicates whether use of VCEK is allowed for attestation reports * @rsvd: reserved * @host_data: host-supplied data for guest, not interpreted by firmware */ @@ -667,7 +668,8 @@ struct sev_data_snp_launch_finish { u64 id_auth_paddr; u8 id_block_en:1; u8 auth_key_en:1; - u64 rsvd:62; + u8 vcek_disabled:1; + u64 rsvd:61; u8 host_data[32]; } __packed;