From patchwork Sun Apr 21 18:01:07 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Michael Roth X-Patchwork-Id: 13637458 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 99A67C4345F for ; Sun, 21 Apr 2024 18:10:59 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 2DA856B00B6; Sun, 21 Apr 2024 14:10:59 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 28A136B00BB; Sun, 21 Apr 2024 14:10:59 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 0DCDD6B00BC; Sun, 21 Apr 2024 14:10:59 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0012.hostedemail.com [216.40.44.12]) by kanga.kvack.org (Postfix) with ESMTP id E2A186B00B6 for ; Sun, 21 Apr 2024 14:10:58 -0400 (EDT) Received: from smtpin18.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id 9D06416014F for ; Sun, 21 Apr 2024 18:10:58 +0000 (UTC) X-FDA: 82034330196.18.F994DB7 Received: from NAM11-DM6-obe.outbound.protection.outlook.com (mail-dm6nam11on2084.outbound.protection.outlook.com [40.107.223.84]) by imf18.hostedemail.com (Postfix) with ESMTP id 99E571C0008 for ; Sun, 21 Apr 2024 18:10:55 +0000 (UTC) Authentication-Results: imf18.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=LpfFNfWi; dmarc=pass (policy=quarantine) header.from=amd.com; spf=pass (imf18.hostedemail.com: domain of Michael.Roth@amd.com designates 40.107.223.84 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=1713723055; 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=bMLcSqMLx+WnMuvKssjd3QjQtrtM9T0ZYvsTn0pvMl8=; b=aVW46PjyEI2EiUXtJFJxAotJv/VmUlQh/FU9HZhLYfUsCzNlI166peKOg8Y78gyAK7J+2m xxOQNRdQJgL7pEWrfU7XnmAYWge10khhS/euybIII4nzgIo2QvRwP0fawjrQ0DvSuShIvo d8DqwyPA6Y2qW1mgBXnp3XWaI+6Jgzc= ARC-Authentication-Results: i=2; imf18.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=LpfFNfWi; dmarc=pass (policy=quarantine) header.from=amd.com; spf=pass (imf18.hostedemail.com: domain of Michael.Roth@amd.com designates 40.107.223.84 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=1713723055; a=rsa-sha256; cv=pass; b=Hc/Xzbl3iN8yso2sKMSbbOM1W6Yaer/SRKBMIfJkSZCQOYEQKTbb4bsu5yKqiaiWScpE3O vu2bf0jWhGqoP+9zZXO9q6mQQImAfdExVQPe5A0OSEZiF2j06a8yL7fK94L3zkO4BuNl3y 37o+emzCi+wcE0aT6m4f4EWQU+aKoVc= ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=VO8Z4KACOlo7i10oZJnVx72lCiMcuCyIzAF9a6Epd3Xs0WQ+f1M6Mmz71sqJijM8p6zUeXuyJp4xjH04QYxWstri4AFVMPO5IlTVgeytHRv68n82d9MfBjZsw30xHorDlloGzfLalyE1hkp54a3l7wnxMI+dJi34M4XEFhERMnJZM0i64fZPHud8Apb8QOqcS8QOnVfsubw1lZapJQQs1b889XQSFzf33PTkri/MFGWewnEYf31nvf3LL9sWQK4EqH+JPJTMmQyqFxZjLKZcaizW4ZiWdFBuTGW7dTf3K/Mhcv9Cc1JnDNbCRnmlyTbyPt+c1hOLP3g5OScKAbiHLg== 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=bMLcSqMLx+WnMuvKssjd3QjQtrtM9T0ZYvsTn0pvMl8=; b=MJi1V/hUTwPAQ9wshO7bI3bxBpGwP3qO6McNSeDxMuiDulw+ghyn/DO/KfFfxdkggK2b1qhfu4rHvh9qQXo5P4AJHNIfAozUkq5hvnZSPX5HuF8w16Wu9J0+H0aZLs/EexAOlKuvNWYVLXzgzHpPM1KLZfHG8XUh2gmm9julIrCeBCMATqKX+y0YYxmzoI94xY9o6PK4Yres4tg/YRBbgra0haQRK8rDKz5DDQvWs3YMYTPyexaW6YNaL12qz2PAgX81gvY3nv7r/xE8c6pMseVJllzWrSPOEIbFDZLtifqjNmhiDJK32SR3r4fEBRHWI2Ed0QhiGBcADpJ4J82cMw== 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=bMLcSqMLx+WnMuvKssjd3QjQtrtM9T0ZYvsTn0pvMl8=; b=LpfFNfWiKv37Tzasc1Mc8lcpSJpLcfNfn27yREUr7dUAn/dSuc1/kp6MxSDiUTbBGbQ6svqGfBoGP9G2JuiP1ancqxTiTp9izKXkJgut3W6OzrboC8ajaHArlJeqajBuI9NRE8TlBe4s/u7u03PNeMXt1tR/qm/aos0rIil+sMs= Received: from CY5PR15CA0010.namprd15.prod.outlook.com (2603:10b6:930:14::15) by PH7PR12MB9127.namprd12.prod.outlook.com (2603:10b6:510:2f6::17) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7472.44; Sun, 21 Apr 2024 18:10:50 +0000 Received: from CY4PEPF0000FCC0.namprd03.prod.outlook.com (2603:10b6:930:14:cafe::42) by CY5PR15CA0010.outlook.office365.com (2603:10b6:930:14::15) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7495.33 via Frontend Transport; Sun, 21 Apr 2024 18:10:50 +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 CY4PEPF0000FCC0.mail.protection.outlook.com (10.167.242.102) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.7519.19 via Frontend Transport; Sun, 21 Apr 2024 18:10:50 +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; Sun, 21 Apr 2024 13:10:49 -0500 From: Michael Roth To: CC: , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , Brijesh Singh , Harald Hoyer Subject: [PATCH v14 07/22] KVM: SEV: Add KVM_SEV_SNP_LAUNCH_FINISH command Date: Sun, 21 Apr 2024 13:01:07 -0500 Message-ID: <20240421180122.1650812-8-michael.roth@amd.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20240421180122.1650812-1-michael.roth@amd.com> References: <20240421180122.1650812-1-michael.roth@amd.com> MIME-Version: 1.0 X-Originating-IP: [10.180.168.240] X-ClientProxiedBy: SATLEXMB03.amd.com (10.181.40.144) To SATLEXMB04.amd.com (10.181.40.145) X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: CY4PEPF0000FCC0:EE_|PH7PR12MB9127:EE_ X-MS-Office365-Filtering-Correlation-Id: fbfdbd03-fb5d-41ed-9a5f-08dc622e604f X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: 6Kb8w0IroJPUKsvnn8rC69EBDDxQU1KmmBIrQpnQuVNfV+be5u1wsjWDpeMMvtIXhGsQbcWR0XyP1ujDYL3Xx1PSP/KLsvB0w/BF0qdi3nBnTA8EqUzEAMn3lXQmwS2McksfALN11AQmUQqNW3XZu5HIRL8L67m58NIVkhLZe7YQ/ZJCBqrTHWPqxy/bMtCcezmFuOIyF9SwBEK0VUc3eEJ7L8eSiJkOFyBL+uxPU+64PvH80IaFk8TZlByOtcfLOJQ7OQkoSaI/oGKYpfsLQI52ImSjBKNmbq/hCRgETPEJ20y11qwlv0/XDTpjTHkY809Aq9FNaZNDE73z9iTB6gIu5/K7kJoXdi8vz6yThSDKqoNxul6utfhvIezHubh0xuryTM+cHJA/oKarCF8FW8pENEc07GmqGmPq9QPMKvKmgGKvgNdqaiB/ZJ18ZMLw0MwATxXtoJ04vrcmQdmboG4qlp6algyWjMWtQRfJfVanXZMQH35qwkCXDp7n3yS3zrdIDn6FrZsa+S6kXDur1Eb49z98cGCNA5NTyBtVIjNFZC+kDokfZLZCSE12HyrKhXIMGsXpt5PBh2q9J4W9o+Fe+nqlGqLtV2zQpDcl7MlqffoqwqBYx03QQ6ZqMtaw7jM+6Ob4saexxvaHEiIxYiiPSYehHL+iIiOarqu7abY5AZBsPnBQyMfGDN3ErOYrsSjjvS08uM7tnK1YX36k9BWIkCDmMlsuNCFSeg72MWSHmyh+qVDr3295N7OoAC+rBlHvz6N2dM7YpKAump/RvKjUWbjGdNRYG2iyRz9d9Whdvj1k6kavs3/yd5lRPmRh9rTYoFbWHQGD9ZBHeqt5gqrPkDDxkbzCWE7WQcKtjrYu2SlauW4fdDTot9zdbgChpeB56yPLAN+TpYj/x0u1XHFE9Yobku2Fw7XPboQtyV0D0LjAwbTjABb0+xOx9qLiqi4XVUHNmdorQTBxbBWYdDGoenTwxrEGdL8TQCvGs6iGx0awVfMIk73yOO4CSdjOUV42ImR6eoHkgvSm8//WZpTYMhEmk740dzl+KZlMvQnHiKVW9VFsTY3SsJBAG0Bys+tsJ7J91Uhk1wZYkjrAMP+5xk9e1r/Eb2qVIqFYwlxAzDjHrWqZvcBsk82aIFkRchcbQtbssgBBgvAYPmaNAW6UMkCVE6FOJLdZ10SYlTSSRkAfx2kQhgrFy0IT75OfxUy6RoCoO8Z8li5mG9gJkAainjMGw5KnW2k2T+N8P9BtpbmgtETmW6E3vVn33mFgMjBsWMf4zMHVR1fEIDT+53DPUvk1KsZiUIrSw6YViJ65I6eYWVMXgLUgzLBowaZqW9Y+sA+1Qz8xTMkiCerswrFzKGuzAYpt7Ah57GPtiYSeJaS8o5ACxPYBlEoUlxcG 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)(376005)(1800799015)(7416005)(36860700004)(82310400014);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 21 Apr 2024 18:10:50.2677 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: fbfdbd03-fb5d-41ed-9a5f-08dc622e604f 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: CY4PEPF0000FCC0.namprd03.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: PH7PR12MB9127 X-Rspam-User: X-Rspamd-Server: rspam11 X-Rspamd-Queue-Id: 99E571C0008 X-Stat-Signature: kchdxqt5ydfwkj3o661j9ysbn1pudfjg X-HE-Tag: 1713723055-242197 X-HE-Meta: U2FsdGVkX19hld5myOFDREb/H/pzJQ7Mh5KK9DT+Y7QoraK1jOWVctj6Fx9p2FNtFXORbDxdNY5N3GEdQFtkIa0/iWYlSeHc9bzMH2B9Hw/XH2MGikyr6e9Z9gAn3p6xSUFaaijwSgUdqYBw68qRxkKXBQ5/oAP5U+nZwg6SPCmjB7pZNM9YeDEUPokhkBuO8VUMUSQdTJduVNFC/10ALBUijWzBTSlog60Y4oEcIR5VlizasjSTMeEWB9aj6HcWUP9dqnUP5SJHzm3Tn9t58V5pzjankimPaZSskKSZHkBtsKrDnuaktLcIclykcB8yW+L6Xw2b9RuZEc+a3zGDXL/Y8I3BLZCDhnn9sdbmmdNp35nlbmQED8RRKiv0qcFfCcx0lrpCiQdU0QndU5x0XuITwfTzvIb27ulEhuHumZkVDUHwV/jTxk1Av3WEd4BjS0vK8MdVJulsBztdKSpUyuUAW7QNQF8dorAdZ+y+C1BQM6PYtcPVAp9AqVplPFw1+rRDyfSI/vT8C4t2mUjAloEqUrdwiN05LHrRvhIOeKtup5R8BPezDlwIdN/O8NeQ506LSjG5tH0GnOwiu3e8/0k1BF8Kn7d2mPu2If7U8Erv0SI3OcJjQJUA9fQP2mTmTtnHJMS4FbIXehqNMEvxdAghedZCPMN+CI7YrIRtZlaU1fvH55e9oJq05nOo009WswGM63qv+QyhpWgiKPelbWlHwjtuMbdjDhk+TaCFDu4gKUiSMS/VKG5ksYZMD6ZPQ5uCzIPQUqqFIdKCkUH+0VeVK4Kh+ywe4rxYaAh6HRSFHTFm0TxBaKg+Jcw0jWWk+Qh6HvCFyC7qDh8DvPYqA60Lbynm0hhO3syRiBtZqLphS3sgPhZK54dWWLNOdwLUzhXJ6LPerEYXlIQ1Tof6+vTScOpwUKm7Axaz0co7MICXiWym7vPsDlhQBy19ec/vem39YX4lKk8zGpptpCu +DK3clJq euJwB1VimxuFemLS7Dp8ZGYPItZwQYZH6ENTAYB9Qm7f8o99g+ihcOz74zGDJUHZgxcdMlWv3RFedhWHcukjO/zZEW+79UpbhTmxmLjaOAMHUUyu8jeuuAE/C12mwBycCgQT6oviGBLDZUw7Y2VABy9jBVJd8Y/ldfl048vg377CqeJ06FTXAyVV8sllBKrCIK3hs+29CtyTmb8KE9UVlcq+CeianxTtsVtIroCp+FsXw5HAjuiG5syzt68yyvosVu0ykr5sjqHjVcs46D6+hH5V292CsnJyBexCRQ7/QL49+RmBHTerk0Qtqap6xzCpBtU73uib2LcEcTUSCw/MRTnb6shuJVUe9A/8B5fZxEP31XpNkgba8m4hWq9TBSSFPQLgBnX0SERaPu3GjX1bU/RnecsChZRyp1kQ4+gaZsUr7z6UKaQcQDXQ1sS5v78XkAwm2 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 | 126 ++++++++++++++++++ include/linux/psp-sev.h | 4 +- 4 files changed, 174 insertions(+), 1 deletion(-) diff --git a/Documentation/virt/kvm/x86/amd-memory-encryption.rst b/Documentation/virt/kvm/x86/amd-memory-encryption.rst index 60728868c5c6..67bcede94bb5 100644 --- a/Documentation/virt/kvm/x86/amd-memory-encryption.rst +++ b/Documentation/virt/kvm/x86/amd-memory-encryption.rst @@ -537,6 +537,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 3c9255de76db..8007fbfe0160 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, }; @@ -852,6 +853,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 d3ae4ded91df..6ca1b13c9beb 100644 --- a/arch/x86/kvm/svm/sev.c +++ b/arch/x86/kvm/svm/sev.c @@ -74,6 +74,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); @@ -2352,6 +2354,114 @@ 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; + + /* Perform some pre-encryption checks against the VMSA */ + 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) { + snp_page_reclaim(pfn); + 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; @@ -2454,6 +2564,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; @@ -2944,11 +3057,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;