From patchwork Fri May 10 21:10:06 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Michael Roth X-Patchwork-Id: 13661960 Received: from NAM10-DM6-obe.outbound.protection.outlook.com (mail-dm6nam10on2054.outbound.protection.outlook.com [40.107.93.54]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 388CA13B5A6; Fri, 10 May 2024 21:20:54 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=40.107.93.54 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1715376056; cv=fail; b=MZQDaWDwgxt6964Y4spq4k2wS36iLA1T5nUKvr47DsVJU5+Eu1JfNOC9TZOI6ixfSj5ayWud5u5TynIdGx+zl3kvoGqtiAXkhpzAv1Ijb9/xfbMUgpFP6h5jofVh6I21mtNJhVHFY7lUlpJm0E8MspQIDVgXkx/whK/09cnHLGY= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1715376056; c=relaxed/simple; bh=GkwynivJtpJ8XgE1UM4Ex0+ult6S8BjL8QUFmNQEwl4=; h=From:To:CC:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=ReOKoNKoHBoMQCzsqTzCaCCZGIVmN7QkbINPHwHj7Frb+LRCmPb1ba3/2frdJxVYe5VZR13cjvkCwaRBdWzj3hNsLnQkWkQvuY92/huGWj6ovzgEIrudlHoyTic5n4DDRZdASlD97bxAwjQ9X+TURLQYWX9YbZ1lnwI5Dk/1PG8= ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=amd.com; spf=fail smtp.mailfrom=amd.com; dkim=pass (1024-bit key) header.d=amd.com header.i=@amd.com header.b=cP2WSBG0; arc=fail smtp.client-ip=40.107.93.54 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=amd.com Authentication-Results: smtp.subspace.kernel.org; spf=fail smtp.mailfrom=amd.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=amd.com header.i=@amd.com header.b="cP2WSBG0" ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=i5B4L4Rx67GsfgzdHOdNjGaQxBCVXQTDS7zFB9XND3S4pUaVWmNVfRSmdRZ2OcpBZeqjqaSklE5NMGLuikChG/5gH7eVZ1V8+JTG0yvHLUr7BzT+gSip01xK2DcpB+XhZ4biUSDzqrlbHMWnr0zc098uJx+zkTfKteOANOG4AkDmSlvUivEuqJHzp3i5GXaywt0QKGXt2LfpVQ/AzGazS3fl4+2J4eqO2hS8/F8XTURFLgCDD1or4kxRNtJtliGvb8MNG8gLEfUSIFIwzN4oMLp3vGPYuaD1NUO6JknyRERSX6CwK9AKa2NwUSfZv3NxXrNAiTyVaGd9ppDeNssKbQ== 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=Ns2sQkcbmDWzhmrSPu1qFteenxs2iF4nPKD6ofsKTD0=; b=P5GmIhhEacD1Lnhv6DDRvHnMmKYCcO9iRO2ouRVciDAndv7ngEXuuvxY+0PRDWpvQONTE/qpUE8yfDKewcyb25tARBqy1mnGwhiFc4L2WLy0eN7MLOe6LKqI9bNPVj/WaAZaRm8iD3GjLNtoirI5txWZUREcI0i3p+KsiO8FHA9C8gXImAqyo8SP/bym0Ojfsb3BchMxnIBmLmrht7WgnLVdffDbPI2GXuV5Lu+0SkEgf9zY5nnjcWav3PLz9xBE3yPYqf3PCrWJU+1BVmM5/fU0oqdXo+gORx2ChWEQv//Zdz/D9MYPZVTE9VQmpRm2jh8dXcxdgviOkSKbevv7ig== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 165.204.84.17) smtp.rcpttodomain=redhat.com 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=Ns2sQkcbmDWzhmrSPu1qFteenxs2iF4nPKD6ofsKTD0=; b=cP2WSBG0YzTGElBPCgI0ASgwC7bmsfdX5Z42WZ3T9tMgvw10L7VhNHntZLGspOCjdq2RzYXsrDmu30HLZ/jyhv6oDZjrEwLZcSqI/Vzx6IvncTe54zD6Tc8QAHR0gaqqS7NB/IBteFZOOvOesiNFcf+QLvWexFz5/tUsP2VHxN8= Received: from BN9PR03CA0075.namprd03.prod.outlook.com (2603:10b6:408:fc::20) by LV2PR12MB5845.namprd12.prod.outlook.com (2603:10b6:408:176::13) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7544.48; Fri, 10 May 2024 21:20:52 +0000 Received: from BN1PEPF00004686.namprd03.prod.outlook.com (2603:10b6:408:fc:cafe::f8) by BN9PR03CA0075.outlook.office365.com (2603:10b6:408:fc::20) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7544.51 via Frontend Transport; Fri, 10 May 2024 21:20:52 +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 BN1PEPF00004686.mail.protection.outlook.com (10.167.243.91) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.7544.18 via Frontend Transport; Fri, 10 May 2024 21:20:52 +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; Fri, 10 May 2024 16:20:52 -0500 From: Michael Roth To: Paolo Bonzini CC: , , Sean Christopherson , Isaku Yamahata Subject: [PULL 01/19] KVM: MMU: Disable fast path if KVM_EXIT_MEMORY_FAULT is needed Date: Fri, 10 May 2024 16:10:06 -0500 Message-ID: <20240510211024.556136-2-michael.roth@amd.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20240510211024.556136-1-michael.roth@amd.com> References: <20240510211024.556136-1-michael.roth@amd.com> Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 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: BN1PEPF00004686:EE_|LV2PR12MB5845:EE_ X-MS-Office365-Filtering-Correlation-Id: a7f648a8-7d9d-42a7-0bd0-08dc713712a2 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0;ARA:13230031|1800799015|82310400017|376005|36860700004; X-Microsoft-Antispam-Message-Info: bUU1MP+oOHJSMFeL+c9sPmZ2O8N6M/PmudORzxvkYfiUK1haJsXfBcEDf5tVuzKCQV35pz1ygHnyE+lksPd5ZspDglN7bJtUOTDp9KdVEFUMvXTT8TxPbzgOmwvfDw6yLaNFUGCXKZkh9D0oUKgNRHildputvHZqgIfNe19K9OXjb6OA7MoBHsguMylVaIOgsz315asvzf7ItDfvyvXXFlSFgaUmR+aZddts6ebnfcG2f38RSF0HEVc/m3ENlzLDfTXc0HOf+cJfXYGrDF19nbz8e97AKSbF9oDwgMx02zdvv0/c1p1vzpBRDy0c3OmiHWJV/1eQdO64WebM0j/ugdVdK7FXpL1ZfMFegwIv2270z7QTTLuwRU+TjbQiiM6leXm1guh8SkQ05oYnVGiV/jbZUKiPVaqi5Y2oIb5x3tgPI/N2vyZGswBdopd1rheXqDzzDay/uQlWxyVTIqvg73XCveGfY9Snso+buS0wfR4QXDJVsQB2xOTzFAjVdBl091i7MgWFYNd+c18e6Uazm9ZDSzZvLKUM6TPDM92TxP2H43pr9qRM4mmVXjTRlsvpftdNZbA5Vu8yOnuhZgff0ZW/rWn+kjLVswai5UfP6UIcs6IYHi0Xt8ZqdEDLH3+h6sye/IU1runUeVbHPrwA+w4reEYVXMupA3Z0SByuLN8Je24Jq7Jq+RH90vJ+QSt182/pLh6GTbD5XbH76bL1vJFm1PCEKtTeO2zffXiwZA/tqfvUKKwZSROUk3LOFiGCO+Ip66PgCJ00j8zuu2/tsypevNM9bSEmoQAEfa2arp3rmPEU2OHaRrqsCuwRg903Czjuw8qvcwhuMeIvkHIfTSzyZzxvLTfRtB6b0WePwM3vOBZGMTMVKsXjd8sJjO6ZhWaWYoiktRF2nAtIWlqV5AB3KsQflRL0ymDX7Ff+UOf2KEGZ9afIA+KHq+OMamseN32aq6wZy4hcDjTMUQwdn3rr89ZiNu5S7E81J1bblEEYYzBGy5Cm9T0J4CHGZYXuG5wXAdDYP/1ND0GEvSNVhcn+5GvGziFZCcw6UXASmdpwzQDDnysQCrQowoXIveOuqucTjjluwfwK2/xa/T2PgG+WPSl5n0MqY/5tBQ2+YakcNIcfsHJusRI3klKpy42g9i8k/bR9AUNirKgvIMmkhjdsZTiFsfBOvGwGrMYVIzyQsz8GQIi/Ac/igkgHy17o/KuUUp69tRYlOLLJTGTsqeiYtvDjOPWADFX1zaXVhW3dEF3H0bIEyZ+V5FPShyUvqrUz5eLn444xtBZEB8u0lgEs6UjHZyJAD/mxP3cbq9cTKYFoHGHK8vkqDcaOQF0CBI8CbEwZvf4hjkF7fzUvKg95x6pN2+GB4OqTZRJHsTw= 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)(82310400017)(376005)(36860700004);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 10 May 2024 21:20:52.9048 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: a7f648a8-7d9d-42a7-0bd0-08dc713712a2 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: BN1PEPF00004686.namprd03.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: LV2PR12MB5845 For hardware-protected VMs like SEV-SNP guests, certain conditions like attempting to perform a write to a page which is not in the state that the guest expects it to be in can result in a nested/extended #PF which can only be satisfied by the host performing an implicit page state change to transition the page into the expected shared/private state. This is generally handled by generating a KVM_EXIT_MEMORY_FAULT event that gets forwarded to userspace to handle via KVM_SET_MEMORY_ATTRIBUTES. However, the fast_page_fault() code might misconstrue this situation as being the result of a write-protected access, and treat it as a spurious case when it sees that writes are already allowed for the sPTE. This results in the KVM MMU trying to resume the guest rather than taking any action to satisfy the real source of the #PF such as generating a KVM_EXIT_MEMORY_FAULT, resulting in the guest spinning on nested #PFs. Check for this condition and bail out of the fast path if it is detected. Suggested-by: Paolo Bonzini Suggested-by: Sean Christopherson Cc: Isaku Yamahata Reviewed-by: Isaku Yamahata Signed-off-by: Michael Roth --- arch/x86/kvm/mmu/mmu.c | 24 ++++++++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/arch/x86/kvm/mmu/mmu.c b/arch/x86/kvm/mmu/mmu.c index 4ec88a2a0061..9c7ab06ce454 100644 --- a/arch/x86/kvm/mmu/mmu.c +++ b/arch/x86/kvm/mmu/mmu.c @@ -3296,7 +3296,7 @@ static int kvm_handle_noslot_fault(struct kvm_vcpu *vcpu, return RET_PF_CONTINUE; } -static bool page_fault_can_be_fast(struct kvm_page_fault *fault) +static bool page_fault_can_be_fast(struct kvm *kvm, struct kvm_page_fault *fault) { /* * Page faults with reserved bits set, i.e. faults on MMIO SPTEs, only @@ -3307,6 +3307,26 @@ static bool page_fault_can_be_fast(struct kvm_page_fault *fault) if (fault->rsvd) return false; + /* + * For hardware-protected VMs, certain conditions like attempting to + * perform a write to a page which is not in the state that the guest + * expects it to be in can result in a nested/extended #PF. In this + * case, the below code might misconstrue this situation as being the + * result of a write-protected access, and treat it as a spurious case + * rather than taking any action to satisfy the real source of the #PF + * such as generating a KVM_EXIT_MEMORY_FAULT. This can lead to the + * guest spinning on a #PF indefinitely, so don't attempt the fast path + * in this case. + * + * Note that the kvm_mem_is_private() check might race with an + * attribute update, but this will either result in the guest spinning + * on RET_PF_SPURIOUS until the update completes, or an actual spurious + * case might go down the slow path. Either case will resolve itself. + */ + if (kvm->arch.has_private_mem && + fault->is_private != kvm_mem_is_private(kvm, fault->gfn)) + return false; + /* * #PF can be fast if: * @@ -3407,7 +3427,7 @@ static int fast_page_fault(struct kvm_vcpu *vcpu, struct kvm_page_fault *fault) u64 *sptep; uint retry_count = 0; - if (!page_fault_can_be_fast(fault)) + if (!page_fault_can_be_fast(vcpu->kvm, fault)) return ret; walk_shadow_page_lockless_begin(vcpu); From patchwork Fri May 10 21:10: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: 13661962 Received: from NAM12-MW2-obe.outbound.protection.outlook.com (mail-mw2nam12on2054.outbound.protection.outlook.com [40.107.244.54]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 227C513B5A4; Fri, 10 May 2024 21:21:23 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=40.107.244.54 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1715376085; cv=fail; b=Tn1HMszWMVwpUtdtezGbOMfuIjfsWO3PP73xB96+SGmRj5GSsXp6eK4N7Ev5lqXWpRUJiGuiw1g93vfMk9nTlV5qVm9/hcfWWJvc8RaiU3XYjCWo//bH5RTa3Gqt169YPXbnnq1TWL4AD7+2KAPLQ6BvAPAxN8rpO25enWOyTY4= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1715376085; c=relaxed/simple; bh=BcYxg/4Sp3zdr7LsH8asOGvULTnkzW+IZmjD4eZ0ayg=; h=From:To:CC:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=bFXqg0iCDFaJP204N/G5QYIkOFAps7BskT86TJlLs674ReC6JDbAASEHuYpS432fyHs1UJsvlplglOfBnBHjnx9bRBzhAlfinpPekjONsLsTS3mnS0KT/pBlu+m5ZbjyKi4B9pSP77U5bPytx3Vk/vlaVPjKWqvc/HR1y7wOiz0= ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=amd.com; spf=fail smtp.mailfrom=amd.com; dkim=pass (1024-bit key) header.d=amd.com header.i=@amd.com header.b=lT1MgQ8T; arc=fail smtp.client-ip=40.107.244.54 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=amd.com Authentication-Results: smtp.subspace.kernel.org; spf=fail smtp.mailfrom=amd.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=amd.com header.i=@amd.com header.b="lT1MgQ8T" ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=KoeiPWiNfCkFBHv2twTlpMldEaSYgX8ZxDc5Ph41ihobfEE4e8jkXlAh10dreIoqp+JzO7rmxauZo1Npm3m9LgCA+JGS7v0nUURWVxnc7JdV37fd361kX//GpTZW3P+n2OBSAPRXnHblzeZTREzd6YmeuBDr1mxo5r7M/KWr9KoQhQHf/FYEj/YnEAWdDrbw8RqNW2bM02U0pWfdGOFiDrKy7Z2uOdOF9lwlxXIzjDF51z9u5EYoWQrESaW2sHEIZZGy2GS37ec8z1WHimmj+rubuGCWJdWKUbfgGv1YP5miRW1g3vlJUnizC6UiebWGei3hVWwGY8hgbOJUECt7Gw== 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=/w7HkGwErEFXk5K+70K1QxZapZm3c5E5wfTsmBvpDxo=; b=nVxXK/SHNBhc8H0lEWFdjFEWh7PSxkeEDLxAW0Tsn0LS+gmyhK7zG28fm8qBEBx4ebK37ukWlKiiS4tnFVzRE6KYjxkFSxwtmHO4nCIgScncR75qPdIMEdXbPrzvzL/coQBfqurw5iJTdYS1kSN+EUjSzTC3r1t50jqMJs8c1XRCwwLr4Xm5Dsjat3eODnjMACL1NL7Q/5FoaCkPEKUynTNFP+AmPbZKdXS06lNlOxlFhGVYh5m2++bhjU/PUYXyRWFjefWhK9XObEnbBgot/vs2Oo36F8na031noySJ17DOR6nVMWrS+cLOCTCTlkhnPPNLKgM1JsRFpgPEjXmSYg== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 165.204.84.17) smtp.rcpttodomain=redhat.com 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=/w7HkGwErEFXk5K+70K1QxZapZm3c5E5wfTsmBvpDxo=; b=lT1MgQ8TVR1s4/0hVLaKLp6Y86rOCntedzpRS5l3pB7hALaF6Y+hkne3PLB1AWC5eL8xYJEIrNqsDU6oLccScLEFGZMLAvjysjJmKSJLDq4DPAR8SJoM6S30AoPTlgJNwD5cYZPZXUePoRKyfvcccWESFVN+K4lz7PPbnRmvfGE= Received: from BN9PR03CA0372.namprd03.prod.outlook.com (2603:10b6:408:f7::17) by DS0PR12MB6415.namprd12.prod.outlook.com (2603:10b6:8:cc::14) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7544.47; Fri, 10 May 2024 21:21:18 +0000 Received: from BN2PEPF000044A2.namprd02.prod.outlook.com (2603:10b6:408:f7:cafe::51) by BN9PR03CA0372.outlook.office365.com (2603:10b6:408:f7::17) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7544.45 via Frontend Transport; Fri, 10 May 2024 21:21:17 +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 BN2PEPF000044A2.mail.protection.outlook.com (10.167.243.153) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.7544.18 via Frontend Transport; Fri, 10 May 2024 21:21:17 +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; Fri, 10 May 2024 16:21:16 -0500 From: Michael Roth To: Paolo Bonzini CC: , , Sean Christopherson Subject: [PULL 02/19] KVM: SEV: Select KVM_GENERIC_PRIVATE_MEM when CONFIG_KVM_AMD_SEV=y Date: Fri, 10 May 2024 16:10:07 -0500 Message-ID: <20240510211024.556136-3-michael.roth@amd.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20240510211024.556136-1-michael.roth@amd.com> References: <20240510211024.556136-1-michael.roth@amd.com> Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 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: BN2PEPF000044A2:EE_|DS0PR12MB6415:EE_ X-MS-Office365-Filtering-Correlation-Id: 1dea16b3-0fea-4fc1-ed40-08dc7137216b X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0;ARA:13230031|1800799015|82310400017|376005|36860700004; X-Microsoft-Antispam-Message-Info: NJcvsrAOvIkoldWYRBV30y5h+noi04Es/ucism8btk+Xeh3rTacyPojc1bPxEGvs3bQleRO1RP21fr32E8AJjOVBhDPpM/TBs1vijv4lT8Z3DIoQOwyojfwZx3QkU3PE+WB7FKgzIskxGvdILt5nepxany1uprmrjjjZMaI2STlgG5Q3UGjKB3LN1fa3Dc8Fho9TLDgs+aNfZSmVc2cyUxF+6nCYL8XczHgD4239Yj3YgEGkSoTdzRNg5MU8zRYKEdPrIwi9sNyZ9bm6REUFahq5NUmZLAhiYzMkvGd+fXy7i/81HrYkZJPC/qLrU3VldVys3GAM0gAfIXpkTKJfZ2cGZCwdYzd52Edbkq1N2auh8Zs4a5tMh3ASSLkJMBQCgBjkzOUhQMET2AGuAkxfZkW5lbc7uDJ23xwtkzu8P3ooMMWgugmi/bGEQWyWYFPJO5Hjfmlv+VnORxdGw0rJXXliF4Q7ZkgIMbg61rSoG0c1tjt/erJFnrrLC/pLHPD5PexvY/wROQgTi7aYPHtNqOzrJuvl4M0e7pyriqXBXkWqY/AYKmz9IgGZihGQso4esN3+e9C+wEI5okxaE2ZmQFJlqNrFw6zJlv9GpuErKAfqj2hmiHLFLZkrdZA6a+ISfqHFssqM+t5MnZ1uFjNkwifqUMQirunGwX7fZJFDSUgw91r2p+F6lwGYZrqj1UnsQjc+46TcoE0Ahve8E2VURbKF0RHzxBkVl4lctX7bJeiZLwk3aS2bGyQDA3KOUn9dXazl4VIG+amC7IklXahhbx7CEJZItDWgn1iic/H8Qwm4ZnEFjEbVeY1NO1wn72VQ2NrXRBnJdTVgCSk7TPas2jfhGCvnUGV5CHruXrywaOQrLDiroCfnc1d7v7TJaq9W2ngPX9lCBCu/e94Tm6Q/IH7dA4dnnUCK3185WKcPdw0eFN1aWO2qedFcUDD7/25UXZfFXSzDErqT9wkhz8sT0g2+kLv808khhkl1MsEwbOUy9vtu7FH3hTbUH0bqaMn1jT//zGtgVvrgRQh0zvkoAd1w7gvoiHNEiwlpNT3q7JKb0x7/Z9gnPT7xJKrgkIuI0pLuv+sflIJ95m3KXDmCGTmSlndFUI5pVW8b+cMn+n+qEB5t/2eYJY1iso6CkFlzRy4A2RtZBUVjWKHrBGC9kxviiGw4tMeNQ7MqV7zQRLSM/zIeBhy4UPaa1I6blRQ5sbrbKs1IIK4VqRD6Y8NzZMqSPQW16jp+QwNrXUt3npyueJkiGHqrwk5yEzz4BLfwy+NO99BhCh4X3+q6/hU+COz0PPWxw5XBQH50+J3dGqCZ3lBDnT+7o8HwDmBVoG/GilB00pl7uCm2pzOo7CY0iDd8kCPajRoDta2A8HROjmmJeoNfuVCPimPZbynLLEaP 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)(82310400017)(376005)(36860700004);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 10 May 2024 21:21:17.7160 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 1dea16b3-0fea-4fc1-ed40-08dc7137216b 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: BN2PEPF000044A2.namprd02.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: DS0PR12MB6415 SEV-SNP relies on private memory support to run guests, so make sure to enable that support via the CONFIG_KVM_GENERIC_PRIVATE_MEM config option. Signed-off-by: Michael Roth Message-ID: <20240501085210.2213060-4-michael.roth@amd.com> Signed-off-by: Paolo Bonzini --- arch/x86/kvm/Kconfig | 1 + 1 file changed, 1 insertion(+) diff --git a/arch/x86/kvm/Kconfig b/arch/x86/kvm/Kconfig index d64fb2b3eb69..5e72faca4e8f 100644 --- a/arch/x86/kvm/Kconfig +++ b/arch/x86/kvm/Kconfig @@ -136,6 +136,7 @@ config KVM_AMD_SEV depends on KVM_AMD && X86_64 depends on CRYPTO_DEV_SP_PSP && !(KVM_AMD=y && CRYPTO_DEV_CCP_DD=m) select ARCH_HAS_CC_PLATFORM + select KVM_GENERIC_PRIVATE_MEM help Provides support for launching Encrypted VMs (SEV) and Encrypted VMs with Encrypted State (SEV-ES) on AMD processors. From patchwork Fri May 10 21:10:08 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Michael Roth X-Patchwork-Id: 13661963 Received: from NAM04-DM6-obe.outbound.protection.outlook.com (mail-dm6nam04on2062.outbound.protection.outlook.com [40.107.102.62]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id B730013BACA; Fri, 10 May 2024 21:21:43 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=40.107.102.62 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1715376105; cv=fail; b=Qp0bhrXimBsCO4IYaXH0YEEpp59+xdFTvdf/frpOg5L3zZOWYaOKy+K+zFm+AK3I8ZsdibGwSM3hOAmTobsq8rb2Cdv5CstNQg4NlJQttUTVw2O0//tG7S2HLRsI84ixjRx4A689QH/PDeimpyAHFGThGhZP7zaLbRR3lbRqWXk= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1715376105; c=relaxed/simple; bh=JEny4vs4b4p3nsrcI1mVhMv2rZOY5aB8o5erWsbR41o=; h=From:To:CC:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=qOe65ZU1N5WH/3nBYblZO0MUSBh51JuihGb2YfXayxZhfojrMeTUr8uQVOhuNKVvyuM/Fi3/+DphGhuiijUVYDNpRsxsKozkq+C0dlxkqJTEDD4kbeFcbB3U2crwXuR5nklHcTEIuBYS3yGYXKqwp9ZlhvleqZk129wBRAwFqM0= ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=amd.com; spf=fail smtp.mailfrom=amd.com; dkim=pass (1024-bit key) header.d=amd.com header.i=@amd.com header.b=Cw+sIMd0; arc=fail smtp.client-ip=40.107.102.62 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=amd.com Authentication-Results: smtp.subspace.kernel.org; spf=fail smtp.mailfrom=amd.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=amd.com header.i=@amd.com header.b="Cw+sIMd0" ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=Zsgjlm4AvVuB8kW6GA0ke5UPnPAnF3CaY9S3DvBARUjh1fVwCL6ZFjWRjLuBn9aN8wGERde3oe0MdjiO16v//WB508SXz5h4xRjW1ew6bG3XLg08LCz3FwEY7TQXwxtxi2Qfe4/ci/XQ6/Ggc12GqHJ0Bqq0Y5ExwpDl19ZlzUtO69WccRyVfkqGgXQ7FOpy/qEbbucS60uhcxrpnVslNc9fG1cmk5j+18HhgbT6jMkaPkfVNY6Z3kkX2LWGItvQe/5BSDcp0iqUCNOshaffMvhw4aYbLFzlyhmb8ncsmBGrJE+YF6t6rTBYRFf/effePNhuw6/nI5nSK7pdWttheQ== 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=2uC/eh7Tszyeo3AcJrOXq4KfmQHshjD/x6jIrrxrYco=; b=FXc1XLDhug/X9PR1XLASi7R4XOHUb+oNZqa1RFfnJZS+XlLk1k/0AGmZYp/geCnQ1rcUaFO4K2SPkN/kYKrS4HEnG14eWlNG2Ktak3FiYUzVkZVlT+Q2Fjf84f+tQgwKhrtlw3cq1hY5+UYAVSeWxI7+SD6HxERxhGH+9KLdJtH5Nvp2a9Y6UCAjo6AyBx+42wR/C2ektr2adbLBEDuhwTW/fAmP0pr5CjuXHD3ecqw6yc403SuSPZb9ZknBtL9siNslcaHi+cSPlGMWw3bKX8+bkSXXQfSI5JsJop14uM5T2Z7TBpbDZ8jXVu1amA4vGdLVTrWLL/qGvxjOIrMpUA== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 165.204.84.17) smtp.rcpttodomain=redhat.com 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=2uC/eh7Tszyeo3AcJrOXq4KfmQHshjD/x6jIrrxrYco=; b=Cw+sIMd0cAi61oQhd5jyyMfgscSF5fPVROn5Kr4YhLC2BPBFHLtPIPFkjiJqSyvJZwWEbFiIvGPVc6thFxpqdSwV2TsYX09v0Urhv/pMFeubAKgGlDC/de5hJQ6TW0AySM6HLSp1jGd3xZLj0/5RD1Mc8yZNofRYG8LgcY99JMU= Received: from BN9PR03CA0269.namprd03.prod.outlook.com (2603:10b6:408:ff::34) by CY5PR12MB6597.namprd12.prod.outlook.com (2603:10b6:930:43::18) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7544.48; Fri, 10 May 2024 21:21:40 +0000 Received: from BN2PEPF000044A4.namprd02.prod.outlook.com (2603:10b6:408:ff:cafe::f5) by BN9PR03CA0269.outlook.office365.com (2603:10b6:408:ff::34) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7544.46 via Frontend Transport; Fri, 10 May 2024 21:21:40 +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 BN2PEPF000044A4.mail.protection.outlook.com (10.167.243.155) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.7544.18 via Frontend Transport; Fri, 10 May 2024 21:21:39 +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; Fri, 10 May 2024 16:21:39 -0500 From: Michael Roth To: Paolo Bonzini CC: , , Sean Christopherson , Brijesh Singh , Ashish Kalra Subject: [PULL 03/19] KVM: SEV: Add initial SEV-SNP support Date: Fri, 10 May 2024 16:10:08 -0500 Message-ID: <20240510211024.556136-4-michael.roth@amd.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20240510211024.556136-1-michael.roth@amd.com> References: <20240510211024.556136-1-michael.roth@amd.com> Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 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: BN2PEPF000044A4:EE_|CY5PR12MB6597:EE_ X-MS-Office365-Filtering-Correlation-Id: 6e06e9c7-916e-430e-4fc4-08dc71372eb6 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0;ARA:13230031|1800799015|82310400017|36860700004|376005; X-Microsoft-Antispam-Message-Info: 5yhQscy9QfVqufj3gozZtmNYMbXU6yU0D9fgCk/TH/yaenGwicc0iJp52gZ5aMHkvhmsw9vBnesKDzJwadgyd29J/GkB2x2ydGKeC0ZMdUBmwE8vp03dEAjk5CCW4OWR0XBHBbchX9T2sYe+p/a4EGxscJLnKIBVWsHsR50HRQ7So12NyuJW4WqPOCuUnSgVx0v3ot06SsvHPJ43eHi310qP4cGzGs+54mXqsZX3QnIo3BFG3PWKWDLrx28Z9rhlG+FhnuYpXR7ioQxCi8AbtKHfnoleDvHBhYIYp4+xnG8oH76mC5vJyZe+t5SCmhoNLfqgd0PKsOnaE9E0kR8HIZZaoKzWMA0NM2mohtyhYsV0hya1pkwq9r+2M35ldCTKNV783k1xGyKShX6/aVsuzQjJIF/XNbgTzsjhRXXQ6EHtBBPqaDf2EfMISj0i9cyPi7hdJGawxO8GH6sbSKu+fBN1hFwldnwSO/MHxT5TeKgVuDn1SBtk37fTNnpvHo0scXLVOcfWLFhUhBEaovnj0De3JBF81PK4N6H4nHHhoHy1ar474K2AA42g4k4N/ikEz+Kt0lYMO8LJv+DM0Dm5oX1NqAsJ4hu4bq8s5yNqCj0gZu94BBJWliMkMa19Xo54d1DkbodhQdRAnQXfbvvKhpkIrhazYSneMWVC50maW5EwmnJ6DlRhjTuthFZusWyVVs/BBy5XV9jlbjAKg8xnWo8bF7HjrElHk3zYvDiMEyiSwfiWal5fLdm8AGZFZcZVTJk+vCG5A1rB/etJ9F9yXu9mXwmyB7GftRaQU1xNRuPNhO/5PS+lUdNUw24Yh5UozeB0LkOYjxrdtV8yO49g8Mz4yv7AF/0ps11H9/vlgOy0RMd5KyndxC4RFZ6VBmp3wZU8CWrAt5XxFq5WPwvpjdoZ6SJXsIX0vz8JilSgex3i8SPuofGRtzy3Tz15CpXWsYUL1+/2Ea2VGdpkjWMSygWfjiVP6izw1AU0HNd1tlPlRHO0jkaLHBD60jfdvNdzl3IiBhq+vgOtxNmnkJqmLO0QXwGRSZg09O+dAuPwU13S3q9s1Zt89kfIOgAnIKbyeb35ATJTsr0pJLt1SAWY73oMt4VkKkPe1eJ0Kw6NG9PZ87zUOHYoIgE1H3+T8XJRNob3hhzDluH9fUQksFVU6sXSr8HdRQ6aLziZkBnCwG7RVpnyPAh6z3VPGghTmgqYEFngNNFyuNem+Xx6ionYGaxBRa/PGDvHFtzZ+n/zVCvL0zgA6dzIBwrq6j6PVZyNgLu/8sbY2Cq2baLwu8egSHSRTCwkq8kfjfkjBFrQ7CBE7RwpU3Tt7SkZ/WG/gass8YuvvWu7bdVTKbr6UhM4nUrpdDyEDuJVFheE4chwU3c006eTYA5jdMYPLcpQxXKj 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)(82310400017)(36860700004)(376005);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 10 May 2024 21:21:39.9826 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 6e06e9c7-916e-430e-4fc4-08dc71372eb6 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: BN2PEPF000044A4.namprd02.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: CY5PR12MB6597 From: Brijesh Singh SEV-SNP builds upon existing SEV and SEV-ES functionality while adding new hardware-based security protection. SEV-SNP adds strong memory encryption and integrity protection to help prevent malicious hypervisor-based attacks such as data replay, memory re-mapping, and more, to create an isolated execution environment. Define a new KVM_X86_SNP_VM type which makes use of these capabilities and extend the KVM_SEV_INIT2 ioctl to support it. Also add a basic helper to check whether SNP is enabled and set PFERR_PRIVATE_ACCESS for private #NPFs so they are handled appropriately by KVM MMU. Signed-off-by: Brijesh Singh Co-developed-by: Michael Roth Signed-off-by: Michael Roth Signed-off-by: Ashish Kalra Reviewed-by: Paolo Bonzini Message-ID: <20240501085210.2213060-5-michael.roth@amd.com> Signed-off-by: Paolo Bonzini --- arch/x86/include/asm/svm.h | 3 ++- arch/x86/include/uapi/asm/kvm.h | 1 + arch/x86/kvm/svm/sev.c | 21 ++++++++++++++++++++- arch/x86/kvm/svm/svm.c | 8 +++++++- arch/x86/kvm/svm/svm.h | 12 ++++++++++++ 5 files changed, 42 insertions(+), 3 deletions(-) diff --git a/arch/x86/include/asm/svm.h b/arch/x86/include/asm/svm.h index 728c98175b9c..544a43c1cf11 100644 --- a/arch/x86/include/asm/svm.h +++ b/arch/x86/include/asm/svm.h @@ -285,7 +285,8 @@ static_assert((X2AVIC_MAX_PHYSICAL_ID & AVIC_PHYSICAL_MAX_INDEX_MASK) == X2AVIC_ #define AVIC_HPA_MASK ~((0xFFFULL << 52) | 0xFFF) -#define SVM_SEV_FEAT_DEBUG_SWAP BIT(5) +#define SVM_SEV_FEAT_SNP_ACTIVE BIT(0) +#define SVM_SEV_FEAT_DEBUG_SWAP BIT(5) struct vmcb_seg { u16 selector; diff --git a/arch/x86/include/uapi/asm/kvm.h b/arch/x86/include/uapi/asm/kvm.h index 9fae1b73b529..d2ae5fcc0275 100644 --- a/arch/x86/include/uapi/asm/kvm.h +++ b/arch/x86/include/uapi/asm/kvm.h @@ -874,5 +874,6 @@ struct kvm_hyperv_eventfd { #define KVM_X86_SW_PROTECTED_VM 1 #define KVM_X86_SEV_VM 2 #define KVM_X86_SEV_ES_VM 3 +#define KVM_X86_SNP_VM 4 #endif /* _ASM_X86_KVM_H */ diff --git a/arch/x86/kvm/svm/sev.c b/arch/x86/kvm/svm/sev.c index 0623cfaa7bb0..b3345d45b989 100644 --- a/arch/x86/kvm/svm/sev.c +++ b/arch/x86/kvm/svm/sev.c @@ -47,6 +47,9 @@ module_param_named(sev, sev_enabled, bool, 0444); static bool sev_es_enabled = true; module_param_named(sev_es, sev_es_enabled, bool, 0444); +/* enable/disable SEV-SNP support */ +static bool sev_snp_enabled; + /* enable/disable SEV-ES DebugSwap support */ static bool sev_es_debug_swap_enabled = true; module_param_named(debug_swap, sev_es_debug_swap_enabled, bool, 0444); @@ -288,6 +291,9 @@ static int __sev_guest_init(struct kvm *kvm, struct kvm_sev_cmd *argp, if (sev->es_active && !sev->ghcb_version) sev->ghcb_version = GHCB_VERSION_DEFAULT; + if (vm_type == KVM_X86_SNP_VM) + sev->vmsa_features |= SVM_SEV_FEAT_SNP_ACTIVE; + ret = sev_asid_new(sev); if (ret) goto e_no_asid; @@ -348,7 +354,8 @@ static int sev_guest_init2(struct kvm *kvm, struct kvm_sev_cmd *argp) return -EINVAL; if (kvm->arch.vm_type != KVM_X86_SEV_VM && - kvm->arch.vm_type != KVM_X86_SEV_ES_VM) + kvm->arch.vm_type != KVM_X86_SEV_ES_VM && + kvm->arch.vm_type != KVM_X86_SNP_VM) return -EINVAL; if (copy_from_user(&data, u64_to_user_ptr(argp->data), sizeof(data))) @@ -2328,11 +2335,16 @@ void __init sev_set_cpu_caps(void) kvm_cpu_cap_set(X86_FEATURE_SEV_ES); kvm_caps.supported_vm_types |= BIT(KVM_X86_SEV_ES_VM); } + if (sev_snp_enabled) { + kvm_cpu_cap_set(X86_FEATURE_SEV_SNP); + kvm_caps.supported_vm_types |= BIT(KVM_X86_SNP_VM); + } } void __init sev_hardware_setup(void) { unsigned int eax, ebx, ecx, edx, sev_asid_count, sev_es_asid_count; + bool sev_snp_supported = false; bool sev_es_supported = false; bool sev_supported = false; @@ -2413,6 +2425,7 @@ void __init sev_hardware_setup(void) sev_es_asid_count = min_sev_asid - 1; WARN_ON_ONCE(misc_cg_set_capacity(MISC_CG_RES_SEV_ES, sev_es_asid_count)); sev_es_supported = true; + sev_snp_supported = sev_snp_enabled && cc_platform_has(CC_ATTR_HOST_SEV_SNP); out: if (boot_cpu_has(X86_FEATURE_SEV)) @@ -2425,9 +2438,15 @@ void __init sev_hardware_setup(void) pr_info("SEV-ES %s (ASIDs %u - %u)\n", sev_es_supported ? "enabled" : "disabled", min_sev_asid > 1 ? 1 : 0, min_sev_asid - 1); + if (boot_cpu_has(X86_FEATURE_SEV_SNP)) + pr_info("SEV-SNP %s (ASIDs %u - %u)\n", + sev_snp_supported ? "enabled" : "disabled", + min_sev_asid > 1 ? 1 : 0, min_sev_asid - 1); sev_enabled = sev_supported; sev_es_enabled = sev_es_supported; + sev_snp_enabled = sev_snp_supported; + if (!sev_es_enabled || !cpu_feature_enabled(X86_FEATURE_DEBUG_SWAP) || !cpu_feature_enabled(X86_FEATURE_NO_NESTED_DATA_BP)) sev_es_debug_swap_enabled = false; diff --git a/arch/x86/kvm/svm/svm.c b/arch/x86/kvm/svm/svm.c index c8dc25886c16..66d5e2e46a66 100644 --- a/arch/x86/kvm/svm/svm.c +++ b/arch/x86/kvm/svm/svm.c @@ -2057,6 +2057,9 @@ static int npf_interception(struct kvm_vcpu *vcpu) if (WARN_ON_ONCE(error_code & PFERR_SYNTHETIC_MASK)) error_code &= ~PFERR_SYNTHETIC_MASK; + if (sev_snp_guest(vcpu->kvm) && (error_code & PFERR_GUEST_ENC_MASK)) + error_code |= PFERR_PRIVATE_ACCESS; + trace_kvm_page_fault(vcpu, fault_address, error_code); return kvm_mmu_page_fault(vcpu, fault_address, error_code, static_cpu_has(X86_FEATURE_DECODEASSISTS) ? @@ -4902,8 +4905,11 @@ static int svm_vm_init(struct kvm *kvm) if (type != KVM_X86_DEFAULT_VM && type != KVM_X86_SW_PROTECTED_VM) { - kvm->arch.has_protected_state = (type == KVM_X86_SEV_ES_VM); + kvm->arch.has_protected_state = + (type == KVM_X86_SEV_ES_VM || type == KVM_X86_SNP_VM); to_kvm_sev_info(kvm)->need_init = true; + + kvm->arch.has_private_mem = (type == KVM_X86_SNP_VM); } if (!pause_filter_count || !pause_filter_thresh) diff --git a/arch/x86/kvm/svm/svm.h b/arch/x86/kvm/svm/svm.h index be57213cd295..583e035d38f8 100644 --- a/arch/x86/kvm/svm/svm.h +++ b/arch/x86/kvm/svm/svm.h @@ -349,6 +349,18 @@ static __always_inline bool sev_es_guest(struct kvm *kvm) #endif } +static __always_inline bool sev_snp_guest(struct kvm *kvm) +{ +#ifdef CONFIG_KVM_AMD_SEV + struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info; + + return (sev->vmsa_features & SVM_SEV_FEAT_SNP_ACTIVE) && + !WARN_ON_ONCE(!sev_es_guest(kvm)); +#else + return false; +#endif +} + static inline void vmcb_mark_all_dirty(struct vmcb *vmcb) { vmcb->control.clean = 0; From patchwork Fri May 10 21:10: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: 13661964 Received: from NAM04-BN8-obe.outbound.protection.outlook.com (mail-bn8nam04on2059.outbound.protection.outlook.com [40.107.100.59]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id DAD0C13B590; Fri, 10 May 2024 21:22:07 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=40.107.100.59 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1715376129; cv=fail; b=GxdywOBGZ/utPhNAYrPGdWDbzbt0rMro2oOEhFW0ja+BwJdfMmV52nwbiK9jIWfQh77GjY5jLiIivtqEjrCdiUq8Ji7XHJd3VM7Pl0BzsUFVnXs97FmMmBXNm+oUEM+aboDOGfALXnHE2TDFIujiMim+VUl08m0Un/vcyICWPZc= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1715376129; c=relaxed/simple; bh=STMjBt7II5aOnU+44cO/x5WLmabjtUCTNFDO/iQNVPE=; h=From:To:CC:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=Y5FMGsSkx5WE/Ogpu6TFvq/x17QowuaBa8zAlI3eSeCsRK0OWC53c74yan+4g08H+tAuQXtvfwazVHCkeyyh46Ku0F7etnPpONGFiemKbr/87WqL86lLrZQTN5ksvhoDsrcrMZNH2ss3RdtnlSrwGo744kt+Op4U+RWKIYF1yE8= ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=amd.com; spf=fail smtp.mailfrom=amd.com; dkim=pass (1024-bit key) header.d=amd.com header.i=@amd.com header.b=Bjqk7uY0; arc=fail smtp.client-ip=40.107.100.59 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=amd.com Authentication-Results: smtp.subspace.kernel.org; spf=fail smtp.mailfrom=amd.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=amd.com header.i=@amd.com header.b="Bjqk7uY0" ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=jceqJhD8oLhQ7j8T+u1p8iW/kb9HFBXYShkPtbocL/jVWvG9sF3WnmiVMBKWMMHSZr+CGxUgUlFtPJrgY/A1j1c9jbHoebvnaqA6pNKMIyhFb9O4mOI02J6AaurQ+BGiENESUCmZKJ70e82gDTpJhU+gAAVvWuzA1G7ZljJDoTOYTkHPb5OlKE2xbmRECnKMLULSxROH80dK5G2xKT/FNPTS7PEeLiRXXz17WDZuSCAJP956j3R5gl8zmFfRH7ucfixY64h0OpC1qVpF2c9u9DpZtodYP7wtjCpUCikqkDn7ReRg4XDodgaC5fHfxhBuovDj+1tB3/w2VlkP46kEbg== 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=fKvVqd4yyJOwXD2ESQgT7LvEE6aJ85ssZ1I5Ppa11rY=; b=bDjlZbwJGQjf1Vk93g054Y6hyrYbOK43I9qDtYLJTc7Celc1uxjWRkHERXT/oYAlqCxAkRNEHq6xeWjcSgUTg+i/DA53J8q9gRBPfMGns8ZZaqxdTpEq7ng7bVMQhKKj966edQfa1lElRmUwV/chxTQzOggcYAZZoqEttLCMT7BePYZBpHu2L50aOFUlsl1lztjZ+M8F9vYBC7xfeFBdNcOhbOx1qPIlZHUJ5LuE9OAruzk90J1HPxDSvvifW6BZRtbuwS8Sn0tfJN1U4hHa0DagxwetDcMmkyeJDvLwPH/XAmcix3d492nXRGd961uht9BshjOcbSLOIOCEs9iJAg== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=temperror (sender ip is 165.204.84.17) smtp.rcpttodomain=redhat.com smtp.mailfrom=amd.com; dmarc=temperror 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=fKvVqd4yyJOwXD2ESQgT7LvEE6aJ85ssZ1I5Ppa11rY=; b=Bjqk7uY04q4HrhYnhMsT+Ty1CjqFbXBlhr+Ge4j8PWbjTVfYfkp1J6Pf3Sa6CXP2maJBM7Q51rYp/LdvZMyHmwhA5i8CCzv0AB+UOY3A4Z51bUgVtHxpYuYSgNX5OwSoOTPg7IVttRZGPTrS/ssc5zK7dBojkLCkVE/rp0Bni8Y= Received: from BN9PR03CA0794.namprd03.prod.outlook.com (2603:10b6:408:13f::19) by CY5PR12MB6225.namprd12.prod.outlook.com (2603:10b6:930:23::21) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7544.51; Fri, 10 May 2024 21:22:04 +0000 Received: from BN2PEPF0000449D.namprd02.prod.outlook.com (2603:10b6:408:13f:cafe::3b) by BN9PR03CA0794.outlook.office365.com (2603:10b6:408:13f::19) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7544.43 via Frontend Transport; Fri, 10 May 2024 21:22:04 +0000 X-MS-Exchange-Authentication-Results: spf=temperror (sender IP is 165.204.84.17) smtp.mailfrom=amd.com; dkim=none (message not signed) header.d=none;dmarc=temperror action=none header.from=amd.com; Received-SPF: TempError (protection.outlook.com: error in processing during lookup of amd.com: DNS Timeout) Received: from SATLEXMB04.amd.com (165.204.84.17) by BN2PEPF0000449D.mail.protection.outlook.com (10.167.243.148) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.7544.18 via Frontend Transport; Fri, 10 May 2024 21:22:03 +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; Fri, 10 May 2024 16:22:00 -0500 From: Michael Roth To: Paolo Bonzini CC: , , Sean Christopherson , Brijesh Singh , Ashish Kalra Subject: [PULL 04/19] KVM: SEV: Add KVM_SEV_SNP_LAUNCH_START command Date: Fri, 10 May 2024 16:10:09 -0500 Message-ID: <20240510211024.556136-5-michael.roth@amd.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20240510211024.556136-1-michael.roth@amd.com> References: <20240510211024.556136-1-michael.roth@amd.com> Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 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: BN2PEPF0000449D:EE_|CY5PR12MB6225:EE_ X-MS-Office365-Filtering-Correlation-Id: bc982f91-5807-4ab8-bb22-08dc71373cb9 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0;ARA:13230031|82310400017|36860700004|1800799015|376005; X-Microsoft-Antispam-Message-Info: euGl1uM5bx0y4rrhC3Wn9GLTydNsff8vlIb297fz/mA4zPAuJu6tgTmwRu6XC8EPEpGW1ahAm6Jr/6DnZNVpDFA8FHHru7VlMcPOpFZt4dw0D7KwZpvErp2c1GJ2d7dftcpDBRANQNWYEpQKHU8OXOS3cR7VCuRdqBZrP9yq4drao/VtKdW01QjWbE/xF9Jvst6FgyIXY2DEE5Hf8cTpMdsqwmthhAnmY9Z9J+Tzn+2ontmll/MTm7x9EHaVGoMd1D5Kmf2tyiIFhXfe54AgxfN1pI3BT3J7wAx3yF3AAkTjEePYGmb2hJq1Xlx9vRVUxVvVf1/yfkCm1isA2eRtdN+JY04O4hCVPfqpYO3Q4UKhEa1YMYEc+ndOkMoxjMD6iqrWoTA6E7zosQSfiZIm/nKHp+hmwHR6/0Gp3bAagHpKyFu08eSAI0jfIsTKN65v0z7iboD6OBOSjJYDPtlh68YR9/Rzq46k7G8i9g44VIGpe3WI8b163b5EkGiGWTcu6fM28IdDe18IRM4EfD08wlPiKLe0Bc3+UtFr4wm1e62Ri85O6rhuP5ENCF65qL0VGzV6VQfAh4nl45I6fPW60KNuRtt08HS/2oaOxXN73xP349NN7ytBPFi9Wn/bOwkVJ+6478kJaSJNsAgXkA8lipOUurgTsxtNL5eQY4kYfsj8RYpRpArOSZ04XBJbECbvPMA9kO4fQ0p6Fxf9WYDYWb8X0RySfB2kwrGBg8Bt0q8ZS6WmG5hRZSMPVIDf1DZ02gQs/AZCkgEvaLsttSQXlA6I9gssFGsKpdM7vusB2J2u4w17kd9E0k4fHrtSc+kKCIxSPi6zG6MYD2edbs2PCe3xKROHLcpXLrgwQX3+1+HtHxCCDNpHg+395gmyyNEhlyLMBHb7jTtp+R56FKoyFoRNxoP5R2jEUHh1uvrBWIFuG7AoLtPcCsuPHIPjrBQQE0aVs7PZlqNMj04cvN1z4vvrpr1A0Zee3LlW5rHObeSVK0A2jlGyR3ZjNN8WKWsqAB0vjgFqALfIzsotwmEDwpxpO60SciKB4oCX80Fg29x47eDezRMmz0N7tyOmHngGXG7A9VojRnfxwIp5zCqseroVBkl+b+xL7PRsm0oZxVwV+w5c09kKLNRwLzbJXCGICH+0UDXKjRmTQL3uuJieKcImYOGhmkQ+foqNIPcew2Ru6c5HHYnZN9XhCnajzVI97x9RK2zzD2S4WkoK5otiZZUWsFq2B9UkqEsC66dHCiUM14NM4KwhkbyOkDD+nKbUFSyyyqw6G9Mh+a11HqLvFZkYAywVS/vPeYjycwijhPFgnIDOqEBXjoqk8EmJD6Vp 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)(82310400017)(36860700004)(1800799015)(376005);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 10 May 2024 21:22:03.5244 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: bc982f91-5807-4ab8-bb22-08dc71373cb9 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: BN2PEPF0000449D.namprd02.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: CY5PR12MB6225 From: Brijesh Singh KVM_SEV_SNP_LAUNCH_START begins the launch process for an SEV-SNP guest. The command initializes a cryptographic digest context used to construct the measurement of the guest. Other commands can then at that point be used to load/encrypt data into the guest's initial launch image. For more information see the SEV-SNP specification. Signed-off-by: Brijesh Singh Co-developed-by: Michael Roth Signed-off-by: Michael Roth Signed-off-by: Ashish Kalra Message-ID: <20240501085210.2213060-6-michael.roth@amd.com> Signed-off-by: Paolo Bonzini --- .../virt/kvm/x86/amd-memory-encryption.rst | 28 ++- arch/x86/include/uapi/asm/kvm.h | 11 ++ arch/x86/kvm/svm/sev.c | 176 +++++++++++++++++- arch/x86/kvm/svm/svm.h | 1 + 4 files changed, 212 insertions(+), 4 deletions(-) diff --git a/Documentation/virt/kvm/x86/amd-memory-encryption.rst b/Documentation/virt/kvm/x86/amd-memory-encryption.rst index 9677a0714a39..dd179e162a87 100644 --- a/Documentation/virt/kvm/x86/amd-memory-encryption.rst +++ b/Documentation/virt/kvm/x86/amd-memory-encryption.rst @@ -466,6 +466,30 @@ issued by the hypervisor to make the guest ready for execution. Returns: 0 on success, -negative on error +18. KVM_SEV_SNP_LAUNCH_START +---------------------------- + +The KVM_SNP_LAUNCH_START command is used for creating the memory encryption +context for the SEV-SNP guest. It must be called prior to issuing +KVM_SEV_SNP_LAUNCH_UPDATE or KVM_SEV_SNP_LAUNCH_FINISH; + +Parameters (in): struct kvm_sev_snp_launch_start + +Returns: 0 on success, -negative on error + +:: + + struct kvm_sev_snp_launch_start { + __u64 policy; /* Guest policy to use. */ + __u8 gosvw[16]; /* Guest OS visible workarounds. */ + __u16 flags; /* Must be zero. */ + __u8 pad0[6]; + __u64 pad1[4]; + }; + +See SNP_LAUNCH_START in the SEV-SNP specification [snp-fw-abi]_ for further +details on the input parameters in ``struct kvm_sev_snp_launch_start``. + Device attribute API ==================== @@ -497,9 +521,11 @@ References ========== -See [white-paper]_, [api-spec]_, [amd-apm]_ and [kvm-forum]_ for more info. +See [white-paper]_, [api-spec]_, [amd-apm]_, [kvm-forum]_, and [snp-fw-abi]_ +for more info. .. [white-paper] https://developer.amd.com/wordpress/media/2013/12/AMD_Memory_Encryption_Whitepaper_v7-Public.pdf .. [api-spec] https://support.amd.com/TechDocs/55766_SEV-KM_API_Specification.pdf .. [amd-apm] https://support.amd.com/TechDocs/24593.pdf (section 15.34) .. [kvm-forum] https://www.linux-kvm.org/images/7/74/02x08A-Thomas_Lendacky-AMDs_Virtualizatoin_Memory_Encryption_Technology.pdf +.. [snp-fw-abi] https://www.amd.com/system/files/TechDocs/56860.pdf diff --git a/arch/x86/include/uapi/asm/kvm.h b/arch/x86/include/uapi/asm/kvm.h index d2ae5fcc0275..693a80ffe40a 100644 --- a/arch/x86/include/uapi/asm/kvm.h +++ b/arch/x86/include/uapi/asm/kvm.h @@ -697,6 +697,9 @@ enum sev_cmd_id { /* Second time is the charm; improved versions of the above ioctls. */ KVM_SEV_INIT2, + /* SNP-specific commands */ + KVM_SEV_SNP_LAUNCH_START = 100, + KVM_SEV_NR_MAX, }; @@ -824,6 +827,14 @@ struct kvm_sev_receive_update_data { __u32 pad2; }; +struct kvm_sev_snp_launch_start { + __u64 policy; + __u8 gosvw[16]; + __u16 flags; + __u8 pad0[6]; + __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 b3345d45b989..b372ae5c8c58 100644 --- a/arch/x86/kvm/svm/sev.c +++ b/arch/x86/kvm/svm/sev.c @@ -25,6 +25,7 @@ #include #include #include +#include #include "mmu.h" #include "x86.h" @@ -59,6 +60,21 @@ static u64 sev_supported_vmsa_features; #define AP_RESET_HOLD_NAE_EVENT 1 #define AP_RESET_HOLD_MSR_PROTO 2 +/* As defined by SEV-SNP Firmware ABI, under "Guest Policy". */ +#define SNP_POLICY_MASK_API_MINOR GENMASK_ULL(7, 0) +#define SNP_POLICY_MASK_API_MAJOR GENMASK_ULL(15, 8) +#define SNP_POLICY_MASK_SMT BIT_ULL(16) +#define SNP_POLICY_MASK_RSVD_MBO BIT_ULL(17) +#define SNP_POLICY_MASK_DEBUG BIT_ULL(19) +#define SNP_POLICY_MASK_SINGLE_SOCKET BIT_ULL(20) + +#define SNP_POLICY_MASK_VALID (SNP_POLICY_MASK_API_MINOR | \ + SNP_POLICY_MASK_API_MAJOR | \ + SNP_POLICY_MASK_SMT | \ + SNP_POLICY_MASK_RSVD_MBO | \ + SNP_POLICY_MASK_DEBUG | \ + SNP_POLICY_MASK_SINGLE_SOCKET) + static u8 sev_enc_bit; static DECLARE_RWSEM(sev_deactivate_lock); static DEFINE_MUTEX(sev_bitmap_lock); @@ -69,6 +85,8 @@ static unsigned int nr_asids; static unsigned long *sev_asid_bitmap; static unsigned long *sev_reclaim_asid_bitmap; +static int snp_decommission_context(struct kvm *kvm); + struct enc_region { struct list_head list; unsigned long npages; @@ -95,12 +113,17 @@ static int sev_flush_asids(unsigned int min_asid, unsigned int max_asid) down_write(&sev_deactivate_lock); wbinvd_on_all_cpus(); - ret = sev_guest_df_flush(&error); + + if (sev_snp_enabled) + ret = sev_do_cmd(SEV_CMD_SNP_DF_FLUSH, NULL, &error); + else + ret = sev_guest_df_flush(&error); up_write(&sev_deactivate_lock); if (ret) - pr_err("SEV: DF_FLUSH failed, ret=%d, error=%#x\n", ret, error); + pr_err("SEV%s: DF_FLUSH failed, ret=%d, error=%#x\n", + sev_snp_enabled ? "-SNP" : "", ret, error); return ret; } @@ -1998,6 +2021,106 @@ int sev_dev_get_attr(u32 group, u64 attr, u64 *val) } } +/* + * The guest context contains all the information, keys and metadata + * associated with the guest that the firmware tracks to implement SEV + * and SNP features. The firmware stores the guest context in hypervisor + * provide page via the SNP_GCTX_CREATE command. + */ +static void *snp_context_create(struct kvm *kvm, struct kvm_sev_cmd *argp) +{ + struct sev_data_snp_addr data = {}; + void *context; + int rc; + + /* Allocate memory for context page */ + context = snp_alloc_firmware_page(GFP_KERNEL_ACCOUNT); + if (!context) + return NULL; + + data.address = __psp_pa(context); + rc = __sev_issue_cmd(argp->sev_fd, SEV_CMD_SNP_GCTX_CREATE, &data, &argp->error); + if (rc) { + pr_warn("Failed to create SEV-SNP context, rc %d fw_error %d", + rc, argp->error); + snp_free_firmware_page(context); + return NULL; + } + + return context; +} + +static int snp_bind_asid(struct kvm *kvm, int *error) +{ + struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info; + struct sev_data_snp_activate data = {0}; + + data.gctx_paddr = __psp_pa(sev->snp_context); + data.asid = sev_get_asid(kvm); + return sev_issue_cmd(kvm, SEV_CMD_SNP_ACTIVATE, &data, error); +} + +static int snp_launch_start(struct kvm *kvm, struct kvm_sev_cmd *argp) +{ + struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info; + struct sev_data_snp_launch_start start = {0}; + struct kvm_sev_snp_launch_start params; + int rc; + + if (!sev_snp_guest(kvm)) + return -ENOTTY; + + if (copy_from_user(¶ms, u64_to_user_ptr(argp->data), sizeof(params))) + return -EFAULT; + + /* Don't allow userspace to allocate memory for more than 1 SNP context. */ + if (sev->snp_context) + return -EINVAL; + + sev->snp_context = snp_context_create(kvm, argp); + if (!sev->snp_context) + return -ENOTTY; + + if (params.flags) + return -EINVAL; + + if (params.policy & ~SNP_POLICY_MASK_VALID) + return -EINVAL; + + /* Check for policy bits that must be set */ + if (!(params.policy & SNP_POLICY_MASK_RSVD_MBO) || + !(params.policy & SNP_POLICY_MASK_SMT)) + return -EINVAL; + + if (params.policy & SNP_POLICY_MASK_SINGLE_SOCKET) + return -EINVAL; + + start.gctx_paddr = __psp_pa(sev->snp_context); + start.policy = params.policy; + memcpy(start.gosvw, params.gosvw, sizeof(params.gosvw)); + rc = __sev_issue_cmd(argp->sev_fd, SEV_CMD_SNP_LAUNCH_START, &start, &argp->error); + if (rc) { + pr_debug("%s: SEV_CMD_SNP_LAUNCH_START firmware command failed, rc %d\n", + __func__, rc); + goto e_free_context; + } + + sev->fd = argp->sev_fd; + rc = snp_bind_asid(kvm, &argp->error); + if (rc) { + pr_debug("%s: Failed to bind ASID to SEV-SNP context, rc %d\n", + __func__, rc); + goto e_free_context; + } + + return 0; + +e_free_context: + snp_decommission_context(kvm); + + return rc; +} + int sev_mem_enc_ioctl(struct kvm *kvm, void __user *argp) { struct kvm_sev_cmd sev_cmd; @@ -2021,6 +2144,15 @@ int sev_mem_enc_ioctl(struct kvm *kvm, void __user *argp) goto out; } + /* + * Once KVM_SEV_INIT2 initializes a KVM instance as an SNP guest, only + * allow the use of SNP-specific commands. + */ + if (sev_snp_guest(kvm) && sev_cmd.id < KVM_SEV_SNP_LAUNCH_START) { + r = -EPERM; + goto out; + } + switch (sev_cmd.id) { case KVM_SEV_ES_INIT: if (!sev_es_enabled) { @@ -2085,6 +2217,9 @@ int sev_mem_enc_ioctl(struct kvm *kvm, void __user *argp) case KVM_SEV_RECEIVE_FINISH: r = sev_receive_finish(kvm, &sev_cmd); break; + case KVM_SEV_SNP_LAUNCH_START: + r = snp_launch_start(kvm, &sev_cmd); + break; default: r = -EINVAL; goto out; @@ -2280,6 +2415,31 @@ int sev_vm_copy_enc_context_from(struct kvm *kvm, unsigned int source_fd) return ret; } +static int snp_decommission_context(struct kvm *kvm) +{ + struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info; + struct sev_data_snp_addr data = {}; + int ret; + + /* If context is not created then do nothing */ + if (!sev->snp_context) + return 0; + + /* Do the decommision, which will unbind the ASID from the SNP context */ + data.address = __sme_pa(sev->snp_context); + down_write(&sev_deactivate_lock); + ret = sev_do_cmd(SEV_CMD_SNP_DECOMMISSION, &data, NULL); + up_write(&sev_deactivate_lock); + + if (WARN_ONCE(ret, "Failed to release guest context, ret %d", ret)) + return ret; + + snp_free_firmware_page(sev->snp_context); + sev->snp_context = NULL; + + return 0; +} + void sev_vm_destroy(struct kvm *kvm) { struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info; @@ -2321,7 +2481,17 @@ void sev_vm_destroy(struct kvm *kvm) } } - sev_unbind_asid(kvm, sev->handle); + if (sev_snp_guest(kvm)) { + /* + * Decomission handles unbinding of the ASID. If it fails for + * some unexpected reason, just leak the ASID. + */ + if (snp_decommission_context(kvm)) + return; + } else { + sev_unbind_asid(kvm, sev->handle); + } + sev_asid_free(sev); } diff --git a/arch/x86/kvm/svm/svm.h b/arch/x86/kvm/svm/svm.h index 583e035d38f8..305772d36490 100644 --- a/arch/x86/kvm/svm/svm.h +++ b/arch/x86/kvm/svm/svm.h @@ -93,6 +93,7 @@ struct kvm_sev_info { struct list_head mirror_entry; /* Use as a list entry of mirrors */ struct misc_cg *misc_cg; /* For misc cgroup accounting */ atomic_t migration_in_progress; + void *snp_context; /* SNP guest context page */ }; struct kvm_svm { From patchwork Fri May 10 21:10:10 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Michael Roth X-Patchwork-Id: 13661965 Received: from NAM02-BN1-obe.outbound.protection.outlook.com (mail-bn1nam02on2082.outbound.protection.outlook.com [40.107.212.82]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 27D7113B5A6; Fri, 10 May 2024 21:22:30 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=40.107.212.82 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1715376153; cv=fail; b=TsdnZd7N9QGGKw9Z2ksQVygoObUDgy/2hQrA0ZR1EN23na76uZd6OmFU6XbkkEiLg01oDNUyJFjVTypL4vTIqdcea5oFTlKLnZlHg7MoNSCJJxXA4/C3PItfCTtNyoJTH23fzDJcvcnRMf65jO9oluthMHZ5VH086PH86Ql2qUI= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1715376153; c=relaxed/simple; bh=p44BqroeW6pOezWOiRrnuUvPH3JFTi7n8YdknN87I8A=; h=From:To:CC:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=Q0/TwwgfK96XVp7opnKas6HgegHpHHYGCr+6pp4ptipFIeUlJ3JFOg9QXjILS0xpPZo26eoTIPjhyLgUaHvZ/UyiYq7+rsHfJgzTQPQZAzZWYW1wavBtcf+3kFc8dXgAoFyTXCG3reuxlInUfFvn8d3zfTBvNjC+F0vQnYbZm/E= ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=amd.com; spf=fail smtp.mailfrom=amd.com; dkim=pass (1024-bit key) header.d=amd.com header.i=@amd.com header.b=jtey/G6z; arc=fail smtp.client-ip=40.107.212.82 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=amd.com Authentication-Results: smtp.subspace.kernel.org; spf=fail smtp.mailfrom=amd.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=amd.com header.i=@amd.com header.b="jtey/G6z" ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=c90VirFd9StW/ylxoxcTwLmUAICQ4Fp8cxqLxnRqFuY8c7lW+aBRpF4wGK7TMdc5peTeJqrlVQt7hXbd0+kJQDdyru23gk2mH38pyqbm9ZKOCgXXOM1gs9NWW2s9f+Dy3wW+QwYgySpf60+bT5cUX19bDVOh6B+J+ZhkqHdwhMXyqtPWubq8PSDgivMeIYhhk+8iaGsldGL2q0vFAHd0CF2owNA2gIC2sW9I6aqjrEbhbZptgyfQt3csKNhrxWjhsW2khRi/sT5kdAlolf9d1xA2TYFi08OG5OOnNKpIzBOD+248jBYaN/R4uN/Ri8Y0BbdyY7nE4JsVpsLZrXEPqw== 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=ZbkndvVL8wihOFSJUe/wyLeGwPISCDhOHPd14jZTmo4=; b=hNXIS2krRK6ACv+bBi2OpB0XzRROi6ic9NyYfzLnEIeQpmIE6up09XSyToLchhY6y8z5MtJpVVR47XnhByEDpKRL/3+RG9ayhaEQdgcF22jezjw6Ad6nZZV5pW3mu2CCZuOF/EfJfqzQZIeqQScnYO4Rc/boEgytGoo+t+JVFjfX7qZseUfD0nA8Dc0OV+QlPvvVQitUcrpS0XfzvdRp/daRVcPsHP9na5UUeBlf3i35EHdxVh7dW1Thge9QjkwQX0nhn2MFEOvEcxvLtPD4nQqykX4KaXP9yCmtYGJRCvx5eBTUf4yzPHdap6RIJ8XU2b+PKG+ky73/eplTqBEq2w== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 165.204.84.17) smtp.rcpttodomain=redhat.com 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=ZbkndvVL8wihOFSJUe/wyLeGwPISCDhOHPd14jZTmo4=; b=jtey/G6zXgDqbbN47obBX+LqanVjaL454rc3yWr8f5YMlaMb4imzt30ZwcaINbpKygmRDfd2+tGUIesOHTSO0sLphmNiDP0JaFAxyiSfOQS8zJQegKwImvJTRqjXXqot/J92YOFEpX15zBdiYN72E2LDl/FAPPo3mA79/lJP2lk= Received: from BN9PR03CA0368.namprd03.prod.outlook.com (2603:10b6:408:f7::13) by SA3PR12MB8809.namprd12.prod.outlook.com (2603:10b6:806:31f::20) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7544.49; Fri, 10 May 2024 21:22:27 +0000 Received: from BN2PEPF000044A2.namprd02.prod.outlook.com (2603:10b6:408:f7:cafe::b5) by BN9PR03CA0368.outlook.office365.com (2603:10b6:408:f7::13) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7544.48 via Frontend Transport; Fri, 10 May 2024 21:22:27 +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 BN2PEPF000044A2.mail.protection.outlook.com (10.167.243.153) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.7544.18 via Frontend Transport; Fri, 10 May 2024 21:22:26 +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; Fri, 10 May 2024 16:22:26 -0500 From: Michael Roth To: Paolo Bonzini CC: , , Sean Christopherson , Brijesh Singh , Ashish Kalra Subject: [PULL 05/19] KVM: SEV: Add KVM_SEV_SNP_LAUNCH_UPDATE command Date: Fri, 10 May 2024 16:10:10 -0500 Message-ID: <20240510211024.556136-6-michael.roth@amd.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20240510211024.556136-1-michael.roth@amd.com> References: <20240510211024.556136-1-michael.roth@amd.com> Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 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: BN2PEPF000044A2:EE_|SA3PR12MB8809:EE_ X-MS-Office365-Filtering-Correlation-Id: bcc6cb59-6f82-4de8-1bf3-08dc71374ab5 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0;ARA:13230031|376005|1800799015|36860700004|82310400017; X-Microsoft-Antispam-Message-Info: TAvdFzUsYbes9no45/CNTeTqrAPPygDTfN9AVwLxs4O3vlzf7GMXBX2XNj7oDQ/ZgTLOzuEb4zmWeiYtWtkMUqwRH6bUozCEry9NfROO2biGbKEQ/NPjcB5lu0RuaT/KgfWZAwbdHyTqF24ekXPFaIl9s6z3o1isD/mHO50n/Niqqz5pBOOINlVU5xLfLkPvKKNE1zIUr2lu7thTlbGw/5dV37FQrKlYRKurjFfq94C4AIJQIxIZj3MnOgn3OJNOXWfoLRP9u/8KAdk/xFViNXryLQLUZ54Th3RDPBnsn7E3o/wPELSLoxXxNKtpm2oQNpLPqv8L/VfimZTE6vipHWJ9buQgaE0yn6tzMaA9ESo/RGWYKx22L5FmIMkW9vt4GYI1GfF0jN3k/D1w6z2yjiwoig7EUbXJQKd6GYKe0XArL0sAC5ynBtv0wKZrTkJeIrpmqO8p2qhOri5056wzQ0z5B7hAdFEfqPvL5XQsCZ9RxX7qNud3kUioQ8obBNw38fR/mHBvXS9fkh2Npd3NcaMcfBftnHrSAyF4t4MUCfI3OIbmWkec6ICmkvG2Q7r1h8fCk/C+gWu1+2Fp0fceBAyU3t0ZLDulw3ApxqKUwBZItY2ifty5BwCxzA1JaGdkILAZSGhwIRU9yeHHvty0O/sbhLeRCOh9TWyV/FwlydIpICV4lCi0QO6d02u/AiUii9C7uFSCbmRcDRB4rVDRCYccklwOBXMaI+yeAS/M1EtC9FL7kh7NoxeYmrLBxgr91rEJhe8FY70GqRhiuxbFdGugz/6nnSYxQcss3c1GogBbdNKZTH6fzi6vEUEcMZ5LUV9kV97xCyKBcUKTBQ8Ln2dvwLXaCB/liK+baX7FB6bAA42xPak/j27dIJZuxW2+fAU8oYn/cIDyn+rR9pVp31YMJ30Qhm+1389BQxCyzMmIj1OkxEuf/jZXtYXi+EzRGsytlh64XsC/10M5xVAp2lQQBK9ZI1hcQlSr681xkPoSrpUlnVNtXJKl6W3YLBdpEO/LBNNk5JswMnzYqPcNDDO/HqwmGwuPC+q+rPKmHvZWIiALmelqRgBRsyrNNClkP99u2rBw9esWQ4H48ksOQU03GvFcqeizxjL7wb6Dz1A9VUYfWsjZBDoftjVmKmP01IiecxWyChqrUydtEyru153vPzJalQ+pmRT/lLU+3U+r5dLtTVC+ZvAhVQw1hBv/5ckG07g+PKkQNjtGpW47msPK5HkceBm01GIHBy6y5JeTHn+A6ydU+JunlLRe91qrAkBuTezYvqSM0ZOKcGfMzqVQDpm9hk6h3PAmqx61hnmFX/gtnCmRoWWSBopyfwSjUvH71xtEk8qD7BFFzGtlWPy0SzHBOcEQjqItZllJFLgW2sQ89xzkEHwz9o+F0Sz8 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)(36860700004)(82310400017);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 10 May 2024 21:22:26.9822 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: bcc6cb59-6f82-4de8-1bf3-08dc71374ab5 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: BN2PEPF000044A2.namprd02.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: SA3PR12MB8809 From: Brijesh Singh A key aspect of a launching an SNP guest is initializing it with a known/measured payload which is then encrypted into guest memory as pre-validated private pages and then measured into the cryptographic launch context created with KVM_SEV_SNP_LAUNCH_START so that the guest can attest itself after booting. Since all private pages are provided by guest_memfd, make use of the kvm_gmem_populate() interface to handle this. The general flow is that guest_memfd will handle allocating the pages associated with the GPA ranges being initialized by each particular call of KVM_SEV_SNP_LAUNCH_UPDATE, copying data from userspace into those pages, and then the post_populate callback will do the work of setting the RMP entries for these pages to private and issuing the SNP firmware calls to encrypt/measure them. For more information see the SEV-SNP specification. Signed-off-by: Brijesh Singh Co-developed-by: Michael Roth Signed-off-by: Michael Roth Signed-off-by: Ashish Kalra Message-ID: <20240501085210.2213060-7-michael.roth@amd.com> Signed-off-by: Paolo Bonzini --- .../virt/kvm/x86/amd-memory-encryption.rst | 54 ++++ arch/x86/include/uapi/asm/kvm.h | 19 ++ arch/x86/kvm/svm/sev.c | 230 ++++++++++++++++++ 3 files changed, 303 insertions(+) diff --git a/Documentation/virt/kvm/x86/amd-memory-encryption.rst b/Documentation/virt/kvm/x86/amd-memory-encryption.rst index dd179e162a87..cc16a7426d18 100644 --- a/Documentation/virt/kvm/x86/amd-memory-encryption.rst +++ b/Documentation/virt/kvm/x86/amd-memory-encryption.rst @@ -490,6 +490,60 @@ Returns: 0 on success, -negative on error See SNP_LAUNCH_START in the SEV-SNP specification [snp-fw-abi]_ for further details on the input parameters in ``struct kvm_sev_snp_launch_start``. +19. KVM_SEV_SNP_LAUNCH_UPDATE +----------------------------- + +The KVM_SEV_SNP_LAUNCH_UPDATE command is used for loading userspace-provided +data into a guest GPA range, measuring the contents into the SNP guest context +created by KVM_SEV_SNP_LAUNCH_START, and then encrypting/validating that GPA +range so that it will be immediately readable using the encryption key +associated with the guest context once it is booted, after which point it can +attest the measurement associated with its context before unlocking any +secrets. + +It is required that the GPA ranges initialized by this command have had the +KVM_MEMORY_ATTRIBUTE_PRIVATE attribute set in advance. See the documentation +for KVM_SET_MEMORY_ATTRIBUTES for more details on this aspect. + +Upon success, this command is not guaranteed to have processed the entire +range requested. Instead, the ``gfn_start``, ``uaddr``, and ``len`` fields of +``struct kvm_sev_snp_launch_update`` will be updated to correspond to the +remaining range that has yet to be processed. The caller should continue +calling this command until those fields indicate the entire range has been +processed, e.g. ``len`` is 0, ``gfn_start`` is equal to the last GFN in the +range plus 1, and ``uaddr`` is the last byte of the userspace-provided source +buffer address plus 1. In the case where ``type`` is KVM_SEV_SNP_PAGE_TYPE_ZERO, +``uaddr`` will be ignored completely. + +Parameters (in): struct kvm_sev_snp_launch_update + +Returns: 0 on success, < 0 on error, -EAGAIN if caller should retry + +:: + + struct kvm_sev_snp_launch_update { + __u64 gfn_start; /* Guest page number to load/encrypt data into. */ + __u64 uaddr; /* Userspace address of data to be loaded/encrypted. */ + __u64 len; /* 4k-aligned length in bytes to copy into guest memory.*/ + __u8 type; /* The type of the guest pages being initialized. */ + __u8 pad0; + __u16 flags; /* Must be zero. */ + __u32 pad1; + __u64 pad2[4]; + + }; + +where the allowed values for page_type are #define'd as:: + + KVM_SEV_SNP_PAGE_TYPE_NORMAL + KVM_SEV_SNP_PAGE_TYPE_ZERO + KVM_SEV_SNP_PAGE_TYPE_UNMEASURED + KVM_SEV_SNP_PAGE_TYPE_SECRETS + KVM_SEV_SNP_PAGE_TYPE_CPUID + +See the SEV-SNP spec [snp-fw-abi]_ for further details on how each page type is +used/measured. + Device attribute API ==================== diff --git a/arch/x86/include/uapi/asm/kvm.h b/arch/x86/include/uapi/asm/kvm.h index 693a80ffe40a..5935dc8a7e02 100644 --- a/arch/x86/include/uapi/asm/kvm.h +++ b/arch/x86/include/uapi/asm/kvm.h @@ -699,6 +699,7 @@ enum sev_cmd_id { /* SNP-specific commands */ KVM_SEV_SNP_LAUNCH_START = 100, + KVM_SEV_SNP_LAUNCH_UPDATE, KVM_SEV_NR_MAX, }; @@ -835,6 +836,24 @@ struct kvm_sev_snp_launch_start { __u64 pad1[4]; }; +/* Kept in sync with firmware values for simplicity. */ +#define KVM_SEV_SNP_PAGE_TYPE_NORMAL 0x1 +#define KVM_SEV_SNP_PAGE_TYPE_ZERO 0x3 +#define KVM_SEV_SNP_PAGE_TYPE_UNMEASURED 0x4 +#define KVM_SEV_SNP_PAGE_TYPE_SECRETS 0x5 +#define KVM_SEV_SNP_PAGE_TYPE_CPUID 0x6 + +struct kvm_sev_snp_launch_update { + __u64 gfn_start; + __u64 uaddr; + __u64 len; + __u8 type; + __u8 pad0; + __u16 flags; + __u32 pad1; + __u64 pad2[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 b372ae5c8c58..c966f2224624 100644 --- a/arch/x86/kvm/svm/sev.c +++ b/arch/x86/kvm/svm/sev.c @@ -259,6 +259,45 @@ static void sev_decommission(unsigned int handle) sev_guest_decommission(&decommission, NULL); } +/* + * Certain page-states, such as Pre-Guest and Firmware pages (as documented + * in Chapter 5 of the SEV-SNP Firmware ABI under "Page States") cannot be + * directly transitioned back to normal/hypervisor-owned state via RMPUPDATE + * unless they are reclaimed first. + * + * Until they are reclaimed and subsequently transitioned via RMPUPDATE, they + * might not be usable by the host due to being set as immutable or still + * being associated with a guest ASID. + */ +static int snp_page_reclaim(u64 pfn) +{ + struct sev_data_snp_page_reclaim data = {0}; + int err, rc; + + data.paddr = __sme_set(pfn << PAGE_SHIFT); + rc = sev_do_cmd(SEV_CMD_SNP_PAGE_RECLAIM, &data, &err); + if (WARN_ONCE(rc, "Failed to reclaim PFN %llx", pfn)) + snp_leak_pages(pfn, 1); + + return rc; +} + +/* + * Transition a page to hypervisor-owned/shared state in the RMP table. This + * should not fail under normal conditions, but leak the page should that + * happen since it will no longer be usable by the host due to RMP protections. + */ +static int host_rmp_make_shared(u64 pfn, enum pg_level level) +{ + int rc; + + rc = rmp_make_shared(pfn, level); + if (WARN_ON_ONCE(rc)) + snp_leak_pages(pfn, page_level_size(level) >> PAGE_SHIFT); + + return rc; +} + static void sev_unbind_asid(struct kvm *kvm, unsigned int handle) { struct sev_data_deactivate deactivate; @@ -2121,6 +2160,194 @@ static int snp_launch_start(struct kvm *kvm, struct kvm_sev_cmd *argp) return rc; } +struct sev_gmem_populate_args { + __u8 type; + int sev_fd; + int fw_error; +}; + +static int sev_gmem_post_populate(struct kvm *kvm, gfn_t gfn_start, kvm_pfn_t pfn, + void __user *src, int order, void *opaque) +{ + struct sev_gmem_populate_args *sev_populate_args = opaque; + struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info; + int n_private = 0, ret, i; + int npages = (1 << order); + gfn_t gfn; + + if (WARN_ON_ONCE(sev_populate_args->type != KVM_SEV_SNP_PAGE_TYPE_ZERO && !src)) + return -EINVAL; + + for (gfn = gfn_start, i = 0; gfn < gfn_start + npages; gfn++, i++) { + struct sev_data_snp_launch_update fw_args = {0}; + bool assigned; + int level; + + if (!kvm_mem_is_private(kvm, gfn)) { + pr_debug("%s: Failed to ensure GFN 0x%llx has private memory attribute set\n", + __func__, gfn); + ret = -EINVAL; + goto err; + } + + ret = snp_lookup_rmpentry((u64)pfn + i, &assigned, &level); + if (ret || assigned) { + pr_debug("%s: Failed to ensure GFN 0x%llx RMP entry is initial shared state, ret: %d assigned: %d\n", + __func__, gfn, ret, assigned); + ret = -EINVAL; + goto err; + } + + if (src) { + void *vaddr = kmap_local_pfn(pfn + i); + + ret = copy_from_user(vaddr, src + i * PAGE_SIZE, PAGE_SIZE); + if (ret) + goto err; + kunmap_local(vaddr); + } + + ret = rmp_make_private(pfn + i, gfn << PAGE_SHIFT, PG_LEVEL_4K, + sev_get_asid(kvm), true); + if (ret) + goto err; + + n_private++; + + fw_args.gctx_paddr = __psp_pa(sev->snp_context); + fw_args.address = __sme_set(pfn_to_hpa(pfn + i)); + fw_args.page_size = PG_LEVEL_TO_RMP(PG_LEVEL_4K); + fw_args.page_type = sev_populate_args->type; + + ret = __sev_issue_cmd(sev_populate_args->sev_fd, SEV_CMD_SNP_LAUNCH_UPDATE, + &fw_args, &sev_populate_args->fw_error); + if (ret) + goto fw_err; + } + + return 0; + +fw_err: + /* + * If the firmware command failed handle the reclaim and cleanup of that + * PFN specially vs. prior pages which can be cleaned up below without + * needing to reclaim in advance. + * + * Additionally, when invalid CPUID function entries are detected, + * firmware writes the expected values into the page and leaves it + * unencrypted so it can be used for debugging and error-reporting. + * + * Copy this page back into the source buffer so userspace can use this + * information to provide information on which CPUID leaves/fields + * failed CPUID validation. + */ + if (!snp_page_reclaim(pfn + i) && !host_rmp_make_shared(pfn + i, PG_LEVEL_4K) && + sev_populate_args->type == KVM_SEV_SNP_PAGE_TYPE_CPUID && + sev_populate_args->fw_error == SEV_RET_INVALID_PARAM) { + void *vaddr = kmap_local_pfn(pfn + i); + + if (copy_to_user(src + i * PAGE_SIZE, vaddr, PAGE_SIZE)) + pr_debug("Failed to write CPUID page back to userspace\n"); + + kunmap_local(vaddr); + } + + /* pfn + i is hypervisor-owned now, so skip below cleanup for it. */ + n_private--; + +err: + pr_debug("%s: exiting with error ret %d (fw_error %d), restoring %d gmem PFNs to shared.\n", + __func__, ret, sev_populate_args->fw_error, n_private); + for (i = 0; i < n_private; i++) + host_rmp_make_shared(pfn + i, PG_LEVEL_4K); + + return ret; +} + +static int snp_launch_update(struct kvm *kvm, struct kvm_sev_cmd *argp) +{ + struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info; + struct sev_gmem_populate_args sev_populate_args = {0}; + struct kvm_sev_snp_launch_update params; + struct kvm_memory_slot *memslot; + long npages, count; + void __user *src; + int ret = 0; + + if (!sev_snp_guest(kvm) || !sev->snp_context) + return -EINVAL; + + if (copy_from_user(¶ms, u64_to_user_ptr(argp->data), sizeof(params))) + return -EFAULT; + + pr_debug("%s: GFN start 0x%llx length 0x%llx type %d flags %d\n", __func__, + params.gfn_start, params.len, params.type, params.flags); + + if (!PAGE_ALIGNED(params.len) || params.flags || + (params.type != KVM_SEV_SNP_PAGE_TYPE_NORMAL && + params.type != KVM_SEV_SNP_PAGE_TYPE_ZERO && + params.type != KVM_SEV_SNP_PAGE_TYPE_UNMEASURED && + params.type != KVM_SEV_SNP_PAGE_TYPE_SECRETS && + params.type != KVM_SEV_SNP_PAGE_TYPE_CPUID)) + return -EINVAL; + + npages = params.len / PAGE_SIZE; + + /* + * For each GFN that's being prepared as part of the initial guest + * state, the following pre-conditions are verified: + * + * 1) The backing memslot is a valid private memslot. + * 2) The GFN has been set to private via KVM_SET_MEMORY_ATTRIBUTES + * beforehand. + * 3) The PFN of the guest_memfd has not already been set to private + * in the RMP table. + * + * The KVM MMU relies on kvm->mmu_invalidate_seq to retry nested page + * faults if there's a race between a fault and an attribute update via + * KVM_SET_MEMORY_ATTRIBUTES, and a similar approach could be utilized + * here. However, kvm->slots_lock guards against both this as well as + * concurrent memslot updates occurring while these checks are being + * performed, so use that here to make it easier to reason about the + * initial expected state and better guard against unexpected + * situations. + */ + mutex_lock(&kvm->slots_lock); + + memslot = gfn_to_memslot(kvm, params.gfn_start); + if (!kvm_slot_can_be_private(memslot)) { + ret = -EINVAL; + goto out; + } + + sev_populate_args.sev_fd = argp->sev_fd; + sev_populate_args.type = params.type; + src = params.type == KVM_SEV_SNP_PAGE_TYPE_ZERO ? NULL : u64_to_user_ptr(params.uaddr); + + count = kvm_gmem_populate(kvm, params.gfn_start, src, npages, + sev_gmem_post_populate, &sev_populate_args); + if (count < 0) { + argp->error = sev_populate_args.fw_error; + pr_debug("%s: kvm_gmem_populate failed, ret %ld (fw_error %d)\n", + __func__, count, argp->error); + ret = -EIO; + } else { + params.gfn_start += count; + params.len -= count * PAGE_SIZE; + if (params.type != KVM_SEV_SNP_PAGE_TYPE_ZERO) + params.uaddr += count * PAGE_SIZE; + + ret = 0; + if (copy_to_user(u64_to_user_ptr(argp->data), ¶ms, sizeof(params))) + ret = -EFAULT; + } + +out: + mutex_unlock(&kvm->slots_lock); + + return ret; +} + int sev_mem_enc_ioctl(struct kvm *kvm, void __user *argp) { struct kvm_sev_cmd sev_cmd; @@ -2220,6 +2447,9 @@ int sev_mem_enc_ioctl(struct kvm *kvm, void __user *argp) case KVM_SEV_SNP_LAUNCH_START: r = snp_launch_start(kvm, &sev_cmd); break; + case KVM_SEV_SNP_LAUNCH_UPDATE: + r = snp_launch_update(kvm, &sev_cmd); + break; default: r = -EINVAL; goto out; From patchwork Fri May 10 21:10:11 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Michael Roth X-Patchwork-Id: 13661966 Received: from NAM10-DM6-obe.outbound.protection.outlook.com (mail-dm6nam10on2075.outbound.protection.outlook.com [40.107.93.75]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 1BEFA101E3; Fri, 10 May 2024 21:22:52 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=40.107.93.75 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1715376174; cv=fail; b=pyyvEXLCaBJvFx2zMPlKfckR7fmrQZ2V+/D2/QF/OWIOVnFY6aLu3vQNQDZFPoTKea+aQ1az3xyR3hsqG9kQit5fr4j2VdSay4P7irk9+hhBm6rICNq29g12dIM77wLPy2Srrh5t7m37h6twErrZaHbRHe+kH9OdFdTYOX0eMk8= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1715376174; c=relaxed/simple; bh=kXuO2PguL+6y37tazI+DhqEvcLxVKuhQx+Bt4z7DLXE=; h=From:To:CC:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=iWf7dgj7crz+0hlYJXevsdL3ZQVIZXo+DW4xN8ifsrnZOgDRihH6jfIUecc3dr4BOIw+65WPZ0DIQtnJiSa65qflhSR17cgl1kUPsZirYauY2mrMfmZ9t4SbHbSBYgnY8rL2Odt48WP8w6m7qpQ3qwjrkWy9sXyKgak9QJDRx/w= ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=amd.com; spf=fail smtp.mailfrom=amd.com; dkim=pass (1024-bit key) header.d=amd.com header.i=@amd.com header.b=nNMKU5xG; arc=fail smtp.client-ip=40.107.93.75 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=amd.com Authentication-Results: smtp.subspace.kernel.org; spf=fail smtp.mailfrom=amd.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=amd.com header.i=@amd.com header.b="nNMKU5xG" ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=ko2a3UtbNhBPdaxaamhrCbwWGm3OoUihYSc8FZAItFvfCqV9JK1R1Rs+Kq9Q6e66MiZ0zRbzUZOF1AJG+fcHl0sQSCMvR/tnJrax74szTUGqx1WEvArqEtiEJlF2Psi4318sqBVyt+B856EPwShv2NuYXKxhF8wiZJAVRCVK1mi9Ln9Rrn41VZxsnVxglN/1MNdLNaeUB6vA6mb/In82yJ0QjUBLpJkwVBa3QepKlD5j+GfsjVfk9SF8eztElfm6PEPAIR8LFAyZY47GtJoWDnR9FzS0C3LdChsvZFq2UkJfVap0qW+Ie2HptRKuC6SwEcrtEHeCd6UjTUrqUoSeag== 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=2BalY2yO3hco544+u2I2ziT93lRp0HqnIZddf65pI6Q=; b=E4kW48e6eo6juTUfIQOKXmHNR8vlFQ4FUNkKFeZRQPFoW/Hobjnf3rnGeOFYKW2f5lJJy36KRGi809D9wMYLTczPOdcrKnH6vU3GDkPjLUd0jbhrOLVjN5AfzgPYuNYFSQmk0VSu2BN+I2Hh0+nCWO7VaVswrdpyxosKSqb4CEVA1i+pDVHIaYZwEQpRj0eDOeYsyfeGjf1ZeiIX0LqyhiOFZupZ71sd61tIKodQrCMAX2s9sumAUxlCZA7TEqDDm8GC2oFFPw5sDrYmqNpxbtlRbGJcmkjdOEWeP2QBIqyaejPl78MxY0psTevUu8BdNwB1GA/uG/i+wMmmq7Vkig== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 165.204.84.17) smtp.rcpttodomain=redhat.com 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=2BalY2yO3hco544+u2I2ziT93lRp0HqnIZddf65pI6Q=; b=nNMKU5xGC6KNR9EGXJLut08XcKvsjP3db1+Dmk2zzemzgJaqlxQriem3Sg4QHH7tsmlVfVMNdVFQCHdJEMXXDslCXjS/xo6m2xHY8/IUt8PjEvbIKJi0UGLMvs01RAnMFXfbXQjc0rkCZUh4yDFOLlxSIU4QeOz51dxOEQGZWLU= Received: from BN1PR12CA0022.namprd12.prod.outlook.com (2603:10b6:408:e1::27) by CH2PR12MB4167.namprd12.prod.outlook.com (2603:10b6:610:7a::7) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7544.49; Fri, 10 May 2024 21:22:50 +0000 Received: from BN2PEPF000044A2.namprd02.prod.outlook.com (2603:10b6:408:e1:cafe::ca) by BN1PR12CA0022.outlook.office365.com (2603:10b6:408:e1::27) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7544.45 via Frontend Transport; Fri, 10 May 2024 21:22: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 BN2PEPF000044A2.mail.protection.outlook.com (10.167.243.153) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.7544.18 via Frontend Transport; Fri, 10 May 2024 21:22: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; Fri, 10 May 2024 16:22:50 -0500 From: Michael Roth To: Paolo Bonzini CC: , , Sean Christopherson , Brijesh Singh , Harald Hoyer , Ashish Kalra Subject: [PULL 06/19] KVM: SEV: Add KVM_SEV_SNP_LAUNCH_FINISH command Date: Fri, 10 May 2024 16:10:11 -0500 Message-ID: <20240510211024.556136-7-michael.roth@amd.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20240510211024.556136-1-michael.roth@amd.com> References: <20240510211024.556136-1-michael.roth@amd.com> Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 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: BN2PEPF000044A2:EE_|CH2PR12MB4167:EE_ X-MS-Office365-Filtering-Correlation-Id: b1ada507-8570-4c84-2fa6-08dc713758b2 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0;ARA:13230031|36860700004|82310400017|376005|1800799015; X-Microsoft-Antispam-Message-Info: T43wZQ7VnwORwcMc+zd28TG+Q5n7J8yxAU1u4I+ceeMLWkk+nyIZwg88GEC4iAAHCPFuR56zOJTdIxWbUFs1VtKK3mE42P7iYUmql7XO5t9hwxrXfF4qgLDxJcaeMUOk5Rf1yUiueKe4YaxVib2UUrk3/Fs+kwGQxwA4HuF7Ms+30TE9sfdseeNmEDKLLFSaGWn2+zofA97I+4Au2iATV/TiMhbVPgENoqSydRpSXx83hrgG8CuALlvNmXuaL0SYjnsXdf7rq2lBvLtRVlw1ng0dy8OBu3+k/BR4hdyMe3lQC2gOtCtQo2lMJscjCOijX3o099nRNePUz9QKVKpfSpqP5gf+kBjgw6f3yRq+Whl2U3I+KvGVZzWCZ4hlAkekG0I5diTfaJ0Wfj0+U3c5pDL5L/ZrQNYpBjFFffPogGlIlXu+mjVB2bxY2I9xhCxlR5z1j6J9OIH0Q8JczX++5WefmFAHhyIa3Iukgqx4xW+2IPebVebXUth9UpO7mqygZXWSNL3gQ2FhjNQUtY3TKT87wCuqRYp++juf14yj/cW4gKMDgRH+b3qhgrfunLGnuluvf++fojpa+7YACjyoWT1T4x/U/GpkPV++kKUfI7+al3czAFQDnE23OCrrixuWGCn3wEZIoVLClZzhkrD39T99pf/2arwZeIWfp6HhNdsN+d9cQ14f7BaenBnyVJyAhErErwwhrflREBvM0hMIveQPBGqh4hoJa0sNADUOIISBo0oAEAeTYxvdloXWqptBG4NvsgPEnXiJNY8QScSMhZum/EfEB2G4ciamV3koXpI7S+YZythlxgI8+b++WnoIlUzRcMvdaJhsH89cneetVauE0Ea78bpD7UuJLfFU9V7vR181Z+iJUPyuVXKcZ1xudL87fUGXNC4VXe2cuJEJt/VVrCLZYOSNFtfxplWeGUWsLNVLOEUjvQQhXiTF6pP1Zk6zhEGruMCuSLO4t9TEZAENU9hDPDOnqda+M9z6MRT3x2ZYVUG0DLo/JpN2OsFNgWJj3VQLJ6+xdtWLcMbLEoRrAdk/Qxt8kP5sztE+eG0BiLCrmYqUqDnIeF5d6jlyq0+dAYHkC/O2qKUDdLGBhiAn8A1W03QJvHIYJuDcQvzjIdCxRn7JiAiClrID6eW+4y2uEDp7wFz718/BdqBI4FGc/ItNOxCcdP7zFscZJrf76ewu2UrPM1YSexsqckbXOdij1TkrXCijKLQpB4SYSfCV8rnhPhKRAZ55cpkA8gDAxHgvhQbbIGCSB5VcBBYbPsJW9LPj3RNF4Aputjz9h2PDjJOAU6raTWf6mpybOQO8ALgGdn694252RdgqlimSquclWZRABTs75GRDtY8TIVever3iskyTzQHoCCdrqEY= 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)(82310400017)(376005)(1800799015);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 10 May 2024 21:22:50.4667 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: b1ada507-8570-4c84-2fa6-08dc713758b2 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: BN2PEPF000044A2.namprd02.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: CH2PR12MB4167 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 Message-ID: <20240501085210.2213060-8-michael.roth@amd.com> Signed-off-by: Paolo Bonzini --- .../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 c966f2224624..208bb8170d3f 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; From patchwork Fri May 10 21:10:12 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Michael Roth X-Patchwork-Id: 13661967 Received: from NAM10-DM6-obe.outbound.protection.outlook.com (mail-dm6nam10on2087.outbound.protection.outlook.com [40.107.93.87]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 4EFB8495E5; Fri, 10 May 2024 21:23:19 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=40.107.93.87 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1715376201; cv=fail; b=ZnqD6kCInpwzWS+jzreEt1flNtdg37IwP6Pwc4s6nqXiK+sJfTu6oSdH3OMR4zQNADaKK10x261xpoxkCHRvG3ii2Nd3eX8ubI6e2uTjb6M4LxzltiOHWNXwZv2p8iXdoe+5Kh9u4gsGrSydx+eNzM6fPELq2WpqwnF7CTLl83s= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1715376201; c=relaxed/simple; bh=Gn/oiY0yOolQrRRDRdSHYpW4FdOjuCwG53+hfv1xhdc=; h=From:To:CC:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=bsJB/8aryCyr4l1JtmOVnL1nOW791JQj5rQjtL9u9utTQRjH4CiDotnenYlmlPP++4oiy7igD2Yu9iRIsb5K1Su5XTicV8Knr+qTEYdLtMkXsS5RFMgik1SE27owdHzfiiYe47YrGorjZeooqdPbkwwyReQRiaXsGjPi5UgNpv4= ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=amd.com; spf=fail smtp.mailfrom=amd.com; dkim=pass (1024-bit key) header.d=amd.com header.i=@amd.com header.b=rxHBbdFZ; arc=fail smtp.client-ip=40.107.93.87 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=amd.com Authentication-Results: smtp.subspace.kernel.org; spf=fail smtp.mailfrom=amd.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=amd.com header.i=@amd.com header.b="rxHBbdFZ" ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=MMMZ51K0pCoY8wC/D+TQp7ua+Lipsrx5/s6ReOGTGSAhZdfR5EVgYElvF+y0xqdvoFWCxGRZ+G5jEjVNPspLf8FEDfsdxmepd/1K5qTCtKUvoU2R/z+slHnovTX6pnLzOi/Bd2UET/bJh0wj6Nt0aEaEkAJ//58ezgyc+JSmlJyTXX33blmUsJuPJc7iG0E9tMgUHWHq6oNkF8g3L9e7Y6txEVJlmUamkJ4aQtabjp6h23hf2J23AxrF284Pg3GLZdKA5KVuqVOmZhQbPHOtGREmPsJzANz3jkusy2IglK4H3JbT2Avq3JeC7FUZIghEnEFY1nTqHeWICF5YPa/MCA== 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=q/2cwL3HB1cDl0yv5lORVTiUiHMVQkwQTdlawJgJkiw=; b=ZdjO/qpFvR23OmJZGsYAmNakjI75cJlGTyguYQ2NHNuUOIiT+5xGzklJiDlRVSWQ6Kx/54kFZLPTMEALfKlhhg27X5gxh4MQGK+YnhHty+e9ik2TLzG0eWXGF6MdKsbGjeIe61iwyokzu1xfxoKP10JcFPrQLNXoGPzm72kkN3adM0g4wQWe44D+3oZHT3WrslLc9sEHCTmdwJbYY9uhBDCh4zBxEgijzJjngNL59HCM/KvCWa5TRIHVM6oRCftzGmotOw8V16q0NZU/Uj/4EJQTDik8PraNJyvN5v9DM1CmpXx7SYJmkTnu7ekDz4WgIKmeJdKzAhI4CMX4GZBIPQ== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 165.204.84.17) smtp.rcpttodomain=redhat.com 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=q/2cwL3HB1cDl0yv5lORVTiUiHMVQkwQTdlawJgJkiw=; b=rxHBbdFZiiFD8bOQeb+mPQZFs/4WSX1LWJ0vOMTJZ6T0Zce9Ws9ElQqUqU83vLj1CBZUbLeqk7zr4AjiagGv82m7XbJqFoGw1yIH/pjqwQx9U6yRoLEGMS7u39UwI+PSsCZu29OS1PAvUTarD/l2TFZC3OmeA8f6T3Nzw5u4Vn8= Received: from BN9PR03CA0779.namprd03.prod.outlook.com (2603:10b6:408:13a::34) by PH7PR12MB5830.namprd12.prod.outlook.com (2603:10b6:510:1d5::15) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7544.47; Fri, 10 May 2024 21:23:12 +0000 Received: from BN2PEPF0000449F.namprd02.prod.outlook.com (2603:10b6:408:13a:cafe::e2) by BN9PR03CA0779.outlook.office365.com (2603:10b6:408:13a::34) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7544.46 via Frontend Transport; Fri, 10 May 2024 21:23: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; Fri, 10 May 2024 21:23:11 +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; Fri, 10 May 2024 16:23:10 -0500 From: Michael Roth To: Paolo Bonzini CC: , , Sean Christopherson , Brijesh Singh , Ashish Kalra Subject: [PULL 07/19] KVM: SEV: Add support to handle GHCB GPA register VMGEXIT Date: Fri, 10 May 2024 16:10:12 -0500 Message-ID: <20240510211024.556136-8-michael.roth@amd.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20240510211024.556136-1-michael.roth@amd.com> References: <20240510211024.556136-1-michael.roth@amd.com> Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 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_|PH7PR12MB5830:EE_ X-MS-Office365-Filtering-Correlation-Id: 44b2ecff-6638-4a61-7503-08dc7137658c X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0;ARA:13230031|82310400017|1800799015|376005|36860700004; X-Microsoft-Antispam-Message-Info: 2NLK+PiyGjdwGELDPcpbRESR8U5QPtQ9ZfmjaOcrLgOe004PUB6zuWWTBbVCYnaC9HlDNKCGFDTGgUlfMigLlARBZToCwU68+SDZe324oIlCQ2n68yRiIu11CQUGGprQCE4Bjqr258/2E0tOxKJUiPzozhaGG182aOaiLKRkElg3SXLqLLDbOkXBED79Jcr01MTTqRZ5ZyGF4E4kLHRApa5KgvH/DSBvuc4NcJUcdRqHR9ImjhaVlE0OLDxa8Sm+BNjUDDkXdlrNXw9KYLX8wWtHstYUOHxQmcld01CgzXwDhiEU1ZlRAMD3mtz8wSEoaJtXOHRAJ5hhbGaDFqJatGYSB3hdhxg893hrv/r5uLH9VLb8EzzXNsAbFhIQZpG63TYuPwJ7X+sMvA5hTgvOdHm/lyMAuYktu4vnfZEyNstmnHMVVI4JhFZu+ZM/qzm47VkxE0YgfIuyBPeEfLWI23OP6Hba4lUkAOvMPMPKjSw6/jLdvpEpqcZLSc0LBO87HKcBK4voA2ulAw1luLcg4H9tis6jh6WLWhRQd8ZkEs51s7tlH5+eVd2VsCb1ZBnRKXn8jFtLMrTRSPTBH979WvpIR6glheOHssJf+cxo2oj9K3Wi5znCiRUv+QqEMbtT8wQeMSi4dLyZu0SIh3G04pegAXpPS575AdW5mshgr66tmKhkcEI1PG0zEl40F26cRTDhXcxSg+jHs6M+rWzFEyaScWjfYCcZI2MeDuvDiXzwOlvi0/K544ky6CfPrdepKNoB4hMaCYKxdGd42rOIYdQbJx174wYGYIyKDPbwrWBP0PKZJl/vnBqt6EgyXnKrwqkr1ByRkORi/vrtukchtVehvfD+oyDzGh57TdWstivtw/IDcRRR+V/K4OnUrdoI7YLR93au9nkNjtQjFgdRakFYxyC0tnIOQKhK3IZwfspAid2oJWQ4VegptGGVTYecGE/onWIPHxKzg70UE1ZmPrS0xKaHvkgQVLYniGpRcExXvan0m4r+y89wm7XoUw+jN9fy8cGrZaPHQS+X3txHGegi3SlAvdL6fLC9PTLQFMCT/ctw08jHNNyQVXHv0Zx+yi8/C5ag9BDybpp+RN4v1DreWuC075U5YebuI4uPrDyS9/YzWQDnKRgM6VQbH+vtk2HYQPYubOCp3O8i1vsVOYXJeJIwl0yROpdHPmVg4Tzb4aRmyYC+QOcD1kAApc13+fKjutNvDyAov+UqUWtUBKw5b+5NV0i9R2vryvxgrwtP/5mW3yJd49JPlZrDfBlG/NEUusWZDJz3shfWT37O8+bZXOz3Qy3vMg6OTunD62pKyn4PZuZrIrboHB7vZANGSSrvTqO/nZgGMfYVhjY75VCY6+PnuXN+yGvkuJLUAM9ZImskj7lDZrJ5+eovtOCo 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)(82310400017)(1800799015)(376005)(36860700004);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 10 May 2024 21:23:11.9986 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 44b2ecff-6638-4a61-7503-08dc7137658c 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: PH7PR12MB5830 From: Brijesh Singh SEV-SNP guests are required to perform a GHCB GPA registration. Before using a GHCB GPA for a vCPU the first time, a guest must register the vCPU GHCB GPA. If hypervisor can work with the guest requested GPA then it must respond back with the same GPA otherwise return -1. On VMEXIT, verify that the GHCB GPA matches with the registered value. If a mismatch is detected, then abort the guest. Signed-off-by: Brijesh Singh Signed-off-by: Ashish Kalra Signed-off-by: Michael Roth Message-ID: <20240501085210.2213060-9-michael.roth@amd.com> Signed-off-by: Paolo Bonzini --- arch/x86/include/asm/sev-common.h | 8 ++++++ arch/x86/kvm/svm/sev.c | 48 +++++++++++++++++++++++++++---- arch/x86/kvm/svm/svm.h | 7 +++++ 3 files changed, 57 insertions(+), 6 deletions(-) diff --git a/arch/x86/include/asm/sev-common.h b/arch/x86/include/asm/sev-common.h index 5a8246dd532f..1006bfffe07a 100644 --- a/arch/x86/include/asm/sev-common.h +++ b/arch/x86/include/asm/sev-common.h @@ -59,6 +59,14 @@ #define GHCB_MSR_AP_RESET_HOLD_RESULT_POS 12 #define GHCB_MSR_AP_RESET_HOLD_RESULT_MASK GENMASK_ULL(51, 0) +/* Preferred GHCB GPA Request */ +#define GHCB_MSR_PREF_GPA_REQ 0x010 +#define GHCB_MSR_GPA_VALUE_POS 12 +#define GHCB_MSR_GPA_VALUE_MASK GENMASK_ULL(51, 0) + +#define GHCB_MSR_PREF_GPA_RESP 0x011 +#define GHCB_MSR_PREF_GPA_NONE 0xfffffffffffff + /* GHCB GPA Register */ #define GHCB_MSR_REG_GPA_REQ 0x012 #define GHCB_MSR_REG_GPA_REQ_VAL(v) \ diff --git a/arch/x86/kvm/svm/sev.c b/arch/x86/kvm/svm/sev.c index 208bb8170d3f..557f462fde04 100644 --- a/arch/x86/kvm/svm/sev.c +++ b/arch/x86/kvm/svm/sev.c @@ -3540,6 +3540,32 @@ static int sev_handle_vmgexit_msr_protocol(struct vcpu_svm *svm) set_ghcb_msr_bits(svm, GHCB_MSR_HV_FT_RESP, GHCB_MSR_INFO_MASK, GHCB_MSR_INFO_POS); break; + case GHCB_MSR_PREF_GPA_REQ: + if (!sev_snp_guest(vcpu->kvm)) + goto out_terminate; + + set_ghcb_msr_bits(svm, GHCB_MSR_PREF_GPA_NONE, GHCB_MSR_GPA_VALUE_MASK, + GHCB_MSR_GPA_VALUE_POS); + set_ghcb_msr_bits(svm, GHCB_MSR_PREF_GPA_RESP, GHCB_MSR_INFO_MASK, + GHCB_MSR_INFO_POS); + break; + case GHCB_MSR_REG_GPA_REQ: { + u64 gfn; + + if (!sev_snp_guest(vcpu->kvm)) + goto out_terminate; + + gfn = get_ghcb_msr_bits(svm, GHCB_MSR_GPA_VALUE_MASK, + GHCB_MSR_GPA_VALUE_POS); + + svm->sev_es.ghcb_registered_gpa = gfn_to_gpa(gfn); + + set_ghcb_msr_bits(svm, gfn, GHCB_MSR_GPA_VALUE_MASK, + GHCB_MSR_GPA_VALUE_POS); + set_ghcb_msr_bits(svm, GHCB_MSR_REG_GPA_RESP, GHCB_MSR_INFO_MASK, + GHCB_MSR_INFO_POS); + break; + } case GHCB_MSR_TERM_REQ: { u64 reason_set, reason_code; @@ -3552,12 +3578,7 @@ static int sev_handle_vmgexit_msr_protocol(struct vcpu_svm *svm) pr_info("SEV-ES guest requested termination: %#llx:%#llx\n", reason_set, reason_code); - vcpu->run->exit_reason = KVM_EXIT_SYSTEM_EVENT; - vcpu->run->system_event.type = KVM_SYSTEM_EVENT_SEV_TERM; - vcpu->run->system_event.ndata = 1; - vcpu->run->system_event.data[0] = control->ghcb_gpa; - - return 0; + goto out_terminate; } default: /* Error, keep GHCB MSR value as-is */ @@ -3568,6 +3589,14 @@ static int sev_handle_vmgexit_msr_protocol(struct vcpu_svm *svm) control->ghcb_gpa, ret); return ret; + +out_terminate: + vcpu->run->exit_reason = KVM_EXIT_SYSTEM_EVENT; + vcpu->run->system_event.type = KVM_SYSTEM_EVENT_SEV_TERM; + vcpu->run->system_event.ndata = 1; + vcpu->run->system_event.data[0] = control->ghcb_gpa; + + return 0; } int sev_handle_vmgexit(struct kvm_vcpu *vcpu) @@ -3603,6 +3632,13 @@ int sev_handle_vmgexit(struct kvm_vcpu *vcpu) trace_kvm_vmgexit_enter(vcpu->vcpu_id, svm->sev_es.ghcb); sev_es_sync_from_ghcb(svm); + + /* SEV-SNP guest requires that the GHCB GPA must be registered */ + if (sev_snp_guest(svm->vcpu.kvm) && !ghcb_gpa_is_registered(svm, ghcb_gpa)) { + vcpu_unimpl(&svm->vcpu, "vmgexit: GHCB GPA [%#llx] is not registered.\n", ghcb_gpa); + return -EINVAL; + } + ret = sev_es_validate_vmgexit(svm); if (ret) return ret; diff --git a/arch/x86/kvm/svm/svm.h b/arch/x86/kvm/svm/svm.h index 305772d36490..202ac5494c19 100644 --- a/arch/x86/kvm/svm/svm.h +++ b/arch/x86/kvm/svm/svm.h @@ -209,6 +209,8 @@ struct vcpu_sev_es_state { u32 ghcb_sa_len; bool ghcb_sa_sync; bool ghcb_sa_free; + + u64 ghcb_registered_gpa; }; struct vcpu_svm { @@ -362,6 +364,11 @@ static __always_inline bool sev_snp_guest(struct kvm *kvm) #endif } +static inline bool ghcb_gpa_is_registered(struct vcpu_svm *svm, u64 val) +{ + return svm->sev_es.ghcb_registered_gpa == val; +} + static inline void vmcb_mark_all_dirty(struct vmcb *vmcb) { vmcb->control.clean = 0; From patchwork Fri May 10 21:10:13 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Michael Roth X-Patchwork-Id: 13661968 Received: from NAM11-DM6-obe.outbound.protection.outlook.com (mail-dm6nam11on2077.outbound.protection.outlook.com [40.107.223.77]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id AF7FC47F73; Fri, 10 May 2024 21:23:38 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=40.107.223.77 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1715376220; cv=fail; b=jMUszuBqUnvT45V4kY3Gf8sdSGB2kUYg95NIXFLC1VXKr0RLK5cf4++uhwlXiKkw48yHAFksobEe8B6rihfUXzI967EolvPWxKJKwwfCe7vUbcVYB5+tXcEhvOZe0UbdlJzYuXfyhj5LxHPSI6FDyQ59G6s4lx7SjJSNsrCuZCM= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1715376220; c=relaxed/simple; bh=/Y+yuhBB4Lu35FxJwguWauoRHm4z+zPgmLPfYJFqTXw=; h=From:To:CC:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=m1llX0BXf6eU34bTr3N4Y7QuuDUoz9IUgjFdPAamA7ECqyP38C3Gp6geEkMf/QB8KgvLPodF5sGWwtMIjKFhaXlA7GJdEaCUzANANdPIRy08ObaNaXcrwdqWr2qKXsYsgJz3b36GGxvvz6oSV5+oMymMO85suCGZscMteyNQmCo= ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=amd.com; spf=fail smtp.mailfrom=amd.com; dkim=pass (1024-bit key) header.d=amd.com header.i=@amd.com header.b=By3jjYh4; arc=fail smtp.client-ip=40.107.223.77 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=amd.com Authentication-Results: smtp.subspace.kernel.org; spf=fail smtp.mailfrom=amd.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=amd.com header.i=@amd.com header.b="By3jjYh4" ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=IMOdz7r76reahdeUu+/M2f0GvolEA5H8Tx1Z61qmkyRgJDk3DnisXRt76cV8jX7J/9wqq8psAENP52Ga9xUaWgKb7JkgcbU85AO/9frpCAsHkAPeUgDc+AnGwCj0JCXuphXyh5Rqd5hldG0s7u1qJH7Gfasyq5BGSOLvE/v4bWGDZMx4ORJuj7iLUEJrceirimhDOjfBnxw4r0dYXERwubtwLcyDuAKkwTYx6pLYIJDjmlZ/jkIBMLJ07Y7AUvpO/epp/LHYrqegBKHThvBjT+f3zVwh6jAVlF9uAl7JbeSfDH4tQh2j+Brd41a0jKoPeTU6IjQrXb8CmO2k6KzvkA== 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=5D+oWe6D6ngXvlw2NKp1v0+rio+C79MB1L6SRyqdx0c=; b=n/CXSuQ+J5xmjR1ev/i6F/Xz6BC6OREMWi/Dl6t8TWEjzu75mFqiN+nssOJkaCXOXu7OHgRqMZ5c4v5NaFOqnxiPPipfe/NjMo2CvF8oR06izONzDe3MRwf08eGtBV2ZHlAs5n3tlTmkckoQ97pbDDQ64uV3RBvaO48MZj5OsQYnd2j+hHoyrty2djocw6vfIsNA43/rAwKRMtajMhxU5IiioHpxa3r9VHzh71i9oKBGiqzZ6PNlOCPa0SBh/dy3ImCjYgYFePsbDIhBb3/lPvRrb3UVOppRteFfDtWhBrrfJuPF5/W27FvFrx09E1jqD6nRu0Myrm8ljmMq1EcsnQ== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 165.204.84.17) smtp.rcpttodomain=redhat.com 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=5D+oWe6D6ngXvlw2NKp1v0+rio+C79MB1L6SRyqdx0c=; b=By3jjYh4o0s2i0v81IsB8gCv4mHKK++dr3YtG+eP2/LTMPMNffuy1vGQPjQlcvRlthMw5LK/Z/bkn1vcXIeW5iEfCoZpiyPhQPBXuct1LbkhwkG0n1EhSOX0xiHCkc9NyodyehDX5guA9o0fLcdeJOrToynkKbVZ6CwMt0M2CU4= Received: from BN9P223CA0029.NAMP223.PROD.OUTLOOK.COM (2603:10b6:408:10b::34) by PH8PR12MB6794.namprd12.prod.outlook.com (2603:10b6:510:1c5::17) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7544.49; Fri, 10 May 2024 21:23:32 +0000 Received: from BN2PEPF0000449E.namprd02.prod.outlook.com (2603:10b6:408:10b:cafe::49) by BN9P223CA0029.outlook.office365.com (2603:10b6:408:10b::34) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7544.48 via Frontend Transport; Fri, 10 May 2024 21:23:32 +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; Fri, 10 May 2024 21:23:31 +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; Fri, 10 May 2024 16:23:31 -0500 From: Michael Roth To: Paolo Bonzini CC: , , Sean Christopherson , Brijesh Singh , Ashish Kalra Subject: [PULL 08/19] KVM: SEV: Add support to handle MSR based Page State Change VMGEXIT Date: Fri, 10 May 2024 16:10:13 -0500 Message-ID: <20240510211024.556136-9-michael.roth@amd.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20240510211024.556136-1-michael.roth@amd.com> References: <20240510211024.556136-1-michael.roth@amd.com> Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 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_|PH8PR12MB6794:EE_ X-MS-Office365-Filtering-Correlation-Id: dc58ce26-4428-4294-0989-08dc7137715a X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0;ARA:13230031|376005|1800799015|36860700004|82310400017; X-Microsoft-Antispam-Message-Info: pOYScWR00cIW+iDNAFzeXRnNP9usBTLmnyYdau3x3j5oE3oz5QxTafHk3st+HtxMKxLtKlkPb2K3mZ/VIZaRi12fl7qjDgo0E+LXdP34bNMxKile35Ap48MuUMvgSyzfCnQ6gGkDeCjhcS2BkP8YzBcQrQgf+vlcpBd/f7ekV3xhtSKvplJmSxvMtlRBL2kXPcmHUC2I3WQ7IkI6i842zteoJD2dXzB0FQ+zGV9XnzEUIsepiGxGCDdroqDypNwm5oZ6Y8skzYFkFyuuGrwUtbAxX84Vy/8ir/2HDYQ+NBp5Wy7TT18YfSRF/mLshYNjP8nNxQQ8VL1jfGTwBLKYYIOTBrdCa+OxQRfNvjPSWQBnKjp4c1GUACFLERIKYDpOE81H3ra/BTKgHZ/oIV4bMeLV/XaSk2p/pkZk5HZWK96FHyEvz1BssqXYzW4tlsaSkB+67FZEmqva+/AxrT9x6jNSlR21WuGeIm1B1Nx6mqgheblxxo39DFvNR9t17OW1vqbRuss/BXILSKOFOLMrujeFagdIXLP6GjMZ1MKb3Ttkc8zkcTn6e6ncGPuDR3OptO2OeM0Gg3kGYW1b0mphpUFHRgTTqe/s8oS5DQg9qKbBqEsojdsuCVrNncFIgQVqUNWjXzeHxGDDEmf4gktZ/dVDpu5tYoCOQExx+LDTjNjt5WFrawtQ9FEZ2zqC+ssvBalFILmso+LPTdy4bxupe0EsxHCrbzZ/u6OcxWhGDEcVaTUSx6qoOjcQVkj3tLjb4TeUPkgzThi8nGXfikUyLzPVXFjTvEDhJ1cCRdnVF5hyh1mMrZhDQETPytOdi44MHoXdgSO8eOsEwNbc+qZqB4WTMnbpuyikOxOWPoqfQW1JeFsY8k4OPdZ79cDCT54mLrwZ9e7w6HZGD0qNGowtVog5yeJQRaq6kstQgKEFe8sXQ872muKZcCK26fpU9tRlJPD2szfQWzYk+QIgwdDupvj7Df7PeRJXTFfQoRw514Z5fl+htW9YbhERibKzmEYo+llsZPlaFXi4DzEOvbA6hUe+iqDZ+h18hW11Knb7A3GAy05j/5GF0ViokCGCLRWMsmg8bDuH2eGPvOIBLJ9tOLQsGYC6pPJtIDs2vMLzceFlHXjonJLBntGmUXxZswo/Tr4NNZCCZg8b6O5u5Pb0LPECsY9jRpPVstllPU8oRgewc4Y/rs5YM5kKGWiFhhYIpdhHxHG/IRrlVV9412WTRb7e73L3Upzn6bl7SvdYszMQHTEJge0t4Byw3Tw87vec4QCU03U2TKF8QWVzCOU5zlwxIblL+25si3+u5/RnQ2cR/OPyGk4FCsp78Ud0mOL8kf3/cebXCCqneQ1ebBjQ2+Nfd6nDCQSCPf3Tv5LZKZLeJ4CZ1bmVcvii+7Dbajc1 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)(36860700004)(82310400017);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 10 May 2024 21:23:31.8165 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: dc58ce26-4428-4294-0989-08dc7137715a 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: PH8PR12MB6794 SEV-SNP VMs can ask the hypervisor to change the page state in the RMP table to be private or shared using the Page State Change MSR protocol as defined in the GHCB specification. When using gmem, private/shared memory is allocated through separate pools, and KVM relies on userspace issuing a KVM_SET_MEMORY_ATTRIBUTES KVM ioctl to tell the KVM MMU whether or not a particular GFN should be backed by private memory or not. Forward these page state change requests to userspace so that it can issue the expected KVM ioctls. The KVM MMU will handle updating the RMP entries when it is ready to map a private page into a guest. Use the existing KVM_HC_MAP_GPA_RANGE hypercall format to deliver these requests to userspace via KVM_EXIT_HYPERCALL. Signed-off-by: Michael Roth Co-developed-by: Brijesh Singh Signed-off-by: Brijesh Singh Signed-off-by: Ashish Kalra Message-ID: <20240501085210.2213060-10-michael.roth@amd.com> Signed-off-by: Paolo Bonzini --- arch/x86/include/asm/sev-common.h | 6 ++++ arch/x86/kvm/svm/sev.c | 48 +++++++++++++++++++++++++++++++ 2 files changed, 54 insertions(+) diff --git a/arch/x86/include/asm/sev-common.h b/arch/x86/include/asm/sev-common.h index 1006bfffe07a..6d68db812de1 100644 --- a/arch/x86/include/asm/sev-common.h +++ b/arch/x86/include/asm/sev-common.h @@ -101,11 +101,17 @@ enum psc_op { /* GHCBData[11:0] */ \ GHCB_MSR_PSC_REQ) +#define GHCB_MSR_PSC_REQ_TO_GFN(msr) (((msr) & GENMASK_ULL(51, 12)) >> 12) +#define GHCB_MSR_PSC_REQ_TO_OP(msr) (((msr) & GENMASK_ULL(55, 52)) >> 52) + #define GHCB_MSR_PSC_RESP 0x015 #define GHCB_MSR_PSC_RESP_VAL(val) \ /* GHCBData[63:32] */ \ (((u64)(val) & GENMASK_ULL(63, 32)) >> 32) +/* Set highest bit as a generic error response */ +#define GHCB_MSR_PSC_RESP_ERROR (BIT_ULL(63) | GHCB_MSR_PSC_RESP) + /* GHCB Hypervisor Feature Request/Response */ #define GHCB_MSR_HV_FT_REQ 0x080 #define GHCB_MSR_HV_FT_RESP 0x081 diff --git a/arch/x86/kvm/svm/sev.c b/arch/x86/kvm/svm/sev.c index 557f462fde04..438f2e8b8152 100644 --- a/arch/x86/kvm/svm/sev.c +++ b/arch/x86/kvm/svm/sev.c @@ -3461,6 +3461,48 @@ static void set_ghcb_msr(struct vcpu_svm *svm, u64 value) svm->vmcb->control.ghcb_gpa = value; } +static int snp_complete_psc_msr(struct kvm_vcpu *vcpu) +{ + struct vcpu_svm *svm = to_svm(vcpu); + + if (vcpu->run->hypercall.ret) + set_ghcb_msr(svm, GHCB_MSR_PSC_RESP_ERROR); + else + set_ghcb_msr(svm, GHCB_MSR_PSC_RESP); + + return 1; /* resume guest */ +} + +static int snp_begin_psc_msr(struct vcpu_svm *svm, u64 ghcb_msr) +{ + u64 gpa = gfn_to_gpa(GHCB_MSR_PSC_REQ_TO_GFN(ghcb_msr)); + u8 op = GHCB_MSR_PSC_REQ_TO_OP(ghcb_msr); + struct kvm_vcpu *vcpu = &svm->vcpu; + + if (op != SNP_PAGE_STATE_PRIVATE && op != SNP_PAGE_STATE_SHARED) { + set_ghcb_msr(svm, GHCB_MSR_PSC_RESP_ERROR); + return 1; /* resume guest */ + } + + if (!(vcpu->kvm->arch.hypercall_exit_enabled & (1 << KVM_HC_MAP_GPA_RANGE))) { + set_ghcb_msr(svm, GHCB_MSR_PSC_RESP_ERROR); + return 1; /* resume guest */ + } + + vcpu->run->exit_reason = KVM_EXIT_HYPERCALL; + vcpu->run->hypercall.nr = KVM_HC_MAP_GPA_RANGE; + vcpu->run->hypercall.args[0] = gpa; + vcpu->run->hypercall.args[1] = 1; + vcpu->run->hypercall.args[2] = (op == SNP_PAGE_STATE_PRIVATE) + ? KVM_MAP_GPA_RANGE_ENCRYPTED + : KVM_MAP_GPA_RANGE_DECRYPTED; + vcpu->run->hypercall.args[2] |= KVM_MAP_GPA_RANGE_PAGE_SZ_4K; + + vcpu->arch.complete_userspace_io = snp_complete_psc_msr; + + return 0; /* forward request to userspace */ +} + static int sev_handle_vmgexit_msr_protocol(struct vcpu_svm *svm) { struct vmcb_control_area *control = &svm->vmcb->control; @@ -3566,6 +3608,12 @@ static int sev_handle_vmgexit_msr_protocol(struct vcpu_svm *svm) GHCB_MSR_INFO_POS); break; } + case GHCB_MSR_PSC_REQ: + if (!sev_snp_guest(vcpu->kvm)) + goto out_terminate; + + ret = snp_begin_psc_msr(svm, control->ghcb_gpa); + break; case GHCB_MSR_TERM_REQ: { u64 reason_set, reason_code; From patchwork Fri May 10 21:10:14 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Michael Roth X-Patchwork-Id: 13661948 Received: from NAM12-BN8-obe.outbound.protection.outlook.com (mail-bn8nam12on2080.outbound.protection.outlook.com [40.107.237.80]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id CCFB910965; Fri, 10 May 2024 21:16:48 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=40.107.237.80 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1715375810; cv=fail; b=tADiOXrGtHIUJyuLayQ4MPC+cXhwwxbMDumwLGacR8bcbZlDbpPR39+WaucEXud2EcrFMnLhpgb4KGRP5tyzMgcA6zNitAhXvI+s7+V/KuRlGc/hQruiSGwJVhDff8+ARvEkEugUV0yZ8SOiVvf/ruX2tIyFmHH6uSpsZd3Xoco= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1715375810; c=relaxed/simple; bh=D5/zhkzTJtnrrlQQgGLNxhsdc8j+a8i8OasY8I3hFlI=; h=From:To:CC:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=PZGIQfg9jTfkZo2Hq6rIEWMYTOPZTBXdwiPaBUom0vk4Msummfkpy9UYtKiQj+8fT/3CyIwDhLl5Ulp9Aeqbc8WSi3dUI02yRdB7PNl6SCI+s+DVlP4wbReflkazLbUf8fl0db4eueYBFiw7o1JzSQgNH8WuKIhbqRODKYOg7wI= ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=amd.com; spf=fail smtp.mailfrom=amd.com; dkim=pass (1024-bit key) header.d=amd.com header.i=@amd.com header.b=BnmIX+dG; arc=fail smtp.client-ip=40.107.237.80 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=amd.com Authentication-Results: smtp.subspace.kernel.org; spf=fail smtp.mailfrom=amd.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=amd.com header.i=@amd.com header.b="BnmIX+dG" ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=D93hesj+Xs73c+18Mv7G90Y88Vrfkhj8SGK+TqhIukhyeFjuot8GswIom0T752WCMbwBX1jD8r01MEKZ+CWWUFgmFOHAYxS56z8FMxs7F1MzUtMhC4SRYmRRJznFmyb0kQDv967ZKyXGjA6lyJDqliJJYoBtZQ4AN+aSe0YmiSkSPlVEvYzE3LSMrLMS2D7Eh2ruKQnRj2MugDe2XqLCZoaNNq7MVJQtMe0nxocwmXd4zW2AMHOLLjDsbQT1iddEYE0oxCszp20IfO4ZzpwXO+I8/6WPYO7/pyYtLTaGzbxGURGXrKN+C8j2gZbHXpQ4aHhX+P/wPz4zwjsAgnahBA== 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=UFEO5SmyF0oWrPZ7nUuIexGoqYZkyKvmdb92QQ4fnfQ=; b=lQWHKprjrg+2VB94wGl9ghGvUUCz0yLLnVsjypozJYdg29uX5cnVqkPUEwCseH/Ba6xEyJ2N3RAdQZyAGUly7GVSHe+iGLkZHrjXvDtLY4+Bh/KWhoGpaaKqfnSR2JZZXuRtrs0IwbJ+8X6YrGtB/DSgCEs0P7KYdkmxlP+idSZOLlRC3OhBtAJJfSptP3pWJ1mgA1PtuUR2CgfI7DHDzR5GhOAAQoXIGZHaHIxEcAWOOMmZpH9VPEdzHdwsjhk4RRlRq/Gt9VwbOKablJKLaz/EVma7iBAIO3U+E7s6yaBPlC3jC3XnMtT+SyHlKttFnnv4N2r/1bfGYaMXIFL7sA== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 165.204.84.17) smtp.rcpttodomain=redhat.com 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=UFEO5SmyF0oWrPZ7nUuIexGoqYZkyKvmdb92QQ4fnfQ=; b=BnmIX+dGqEKXpdxJHyy+PysrXTfUETn2PyuK/2InM4my0JF2ol//12gjZ9TSMRXg7nXh4eHLIJEnkmpFoV08UigNN1OBkbkC81T++GfzTQQuXVDm0D/viBn5Vx7RL3PLUw5NT///5wtOb4+NnnnJElM4jV2JSoQyZ9Z2gdj9y78= Received: from BN8PR12CA0023.namprd12.prod.outlook.com (2603:10b6:408:60::36) by SA1PR12MB8598.namprd12.prod.outlook.com (2603:10b6:806:253::21) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7544.48; Fri, 10 May 2024 21:16:42 +0000 Received: from BN1PEPF00004680.namprd03.prod.outlook.com (2603:10b6:408:60:cafe::61) by BN8PR12CA0023.outlook.office365.com (2603:10b6:408:60::36) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7544.51 via Frontend Transport; Fri, 10 May 2024 21:16:42 +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 BN1PEPF00004680.mail.protection.outlook.com (10.167.243.85) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.7544.18 via Frontend Transport; Fri, 10 May 2024 21:16:41 +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; Fri, 10 May 2024 16:16:41 -0500 From: Michael Roth To: Paolo Bonzini CC: , , Sean Christopherson , Brijesh Singh , Ashish Kalra Subject: [PULL 09/19] KVM: SEV: Add support to handle Page State Change VMGEXIT Date: Fri, 10 May 2024 16:10:14 -0500 Message-ID: <20240510211024.556136-10-michael.roth@amd.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20240510211024.556136-1-michael.roth@amd.com> References: <20240510211024.556136-1-michael.roth@amd.com> Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 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: BN1PEPF00004680:EE_|SA1PR12MB8598:EE_ X-MS-Office365-Filtering-Correlation-Id: e05c4757-553b-4eb7-d4bb-08dc71367cf4 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0;ARA:13230031|1800799015|82310400017|36860700004|376005; X-Microsoft-Antispam-Message-Info: nL/8m+dlrXevcz08fGkD7WwVmA5n3hKGAG8LVfzCB1jdcwaIRBzZzUpuacUTjl1PXIdtJudymilyQFBTOxo0vt51YzKD0oZsb6thtiTzcEW8yPHdge3ajtvwsfe1TiHLdA1ix6AkqNNjXT5iLzsYXLycZNfJXRWLzkcAUuQ5Mul1wBb98c7vgV3TqXHD/M5Ju0BnUHnXDZ0Hd2j86I2mHWChAsnQ4jwITJKGrLqjuj8zNZmzCuo2SAYek5ptnEj8WkMbEo/FidYTMyWShRATW2ZGvjndNx+IUAqX9s282KIiXC14SzS4lAw9J8WGbAq4Po3ntlPLTEJoEcUxHsZ1b4c643vKu/OAPiTnYVJV5K9p6DziSxOZYKwnqPn1Wa/GHHKlxT5+wkGxraL5D15N+zbDSF3D3qSRp/RgCGB1FwyXWeloExTIzjsBWSVa+PgjXapdcReMYOrY0TmC7OrxM3lKH03HKKhJd6BBGYvA+OfWyw1eHRc/Xio/TS+T5+c36sDlDqVB9u6dhZ93WaP3ZTtYFl9FdJacggn4mCD0R32MVdSjJDO3ByaRCzn3ZkbhOD9WeR9UhfnNFsH9nfQPdhExtW+KkHYSFlUY4bZu4HmlJQOyCj2T/Yznbn8viIXLk7wNA1/IGxKQbXiaPZMYtRLPQY0Nve9XW8/MiFGxnl4X68vpa0PRI8Q9Vy4bU0paySh1MN3cVXUXsepoBrjnjCn1X9oBxoKTQdDbbhbpb1uFEc8RBQb6DYDrjuof1Eg0X+SoyI4oJKwv1s+khdHAg0zhTrdLKQcU+KW66uYvhW0W858wJTyffks39VtyQZhf/UPRLxdkErtCZxJKDLo+9VOUlE8jUBk1G3+vPZ+ngXf4Rvk8y8wcQY8eaU8myERmEoMHbD0qePq2yZmRig+pR3/2xGXFqNfpkWcUg5pkBi/FclvWXWKGvQFbiqwZuTl48U8qvZQzRqeOwFOdNrjUIgSNAkvW/yXRccuWThBH1m5VFkMPJyCLYtYcpAtfMnrXtJT4BuO9XK7qw4ix8hMDeeqdHkmXhaTtq+judA51g+PvHfGmv6xxVjhqBGjWvlmjkmyYErpbZNFGNjij0Ac4eZbQgTMVboZWnDFU0Ioy/q69VpQBHPoLa7E2RW7pfGd137H9CESFsUnFc2wVrv5RS+mH8jvV3d6BG4OgTyFv/3MFn592p3EiCJI7mnq6BK1boVCAd+GKPHCM9PEsgCO7EWndfhNlEs2lXdFfGnVEcCtTzBEi24q8s37OoYXV8PO0WYjidjtChsrgB0ff3YCjnOJfTDTk7wNBkr+fFIw0T8VAVuwAaa02Qma8A/Bvx/oCS18RKQfNEai507bwSJC335yj4l4H5iui+070M/N/9mc= 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)(82310400017)(36860700004)(376005);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 10 May 2024 21:16:41.7891 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: e05c4757-553b-4eb7-d4bb-08dc71367cf4 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: BN1PEPF00004680.namprd03.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: SA1PR12MB8598 SEV-SNP VMs can ask the hypervisor to change the page state in the RMP table to be private or shared using the Page State Change NAE event as defined in the GHCB specification version 2. Forward these requests to userspace as KVM_EXIT_VMGEXITs, similar to how it is done for requests that don't use a GHCB page. As with the MSR-based page-state changes, use the existing KVM_HC_MAP_GPA_RANGE hypercall format to deliver these requests to userspace via KVM_EXIT_HYPERCALL. Signed-off-by: Michael Roth Co-developed-by: Brijesh Singh Signed-off-by: Brijesh Singh Signed-off-by: Ashish Kalra Message-ID: <20240501085210.2213060-11-michael.roth@amd.com> Co-developed-by: Paolo Bonzini Signed-off-by: Paolo Bonzini --- arch/x86/include/asm/sev-common.h | 11 ++ arch/x86/kvm/svm/sev.c | 188 ++++++++++++++++++++++++++++++ arch/x86/kvm/svm/svm.h | 5 + 3 files changed, 204 insertions(+) diff --git a/arch/x86/include/asm/sev-common.h b/arch/x86/include/asm/sev-common.h index 6d68db812de1..8647cc05e2f4 100644 --- a/arch/x86/include/asm/sev-common.h +++ b/arch/x86/include/asm/sev-common.h @@ -129,8 +129,19 @@ enum psc_op { * The VMGEXIT_PSC_MAX_ENTRY determines the size of the PSC structure, which * is a local stack variable in set_pages_state(). Do not increase this value * without evaluating the impact to stack usage. + * + * Use VMGEXIT_PSC_MAX_COUNT in cases where the actual GHCB-defined max value + * is needed, such as when processing GHCB requests on the hypervisor side. */ #define VMGEXIT_PSC_MAX_ENTRY 64 +#define VMGEXIT_PSC_MAX_COUNT 253 + +#define VMGEXIT_PSC_ERROR_GENERIC (0x100UL << 32) +#define VMGEXIT_PSC_ERROR_INVALID_HDR ((1UL << 32) | 1) +#define VMGEXIT_PSC_ERROR_INVALID_ENTRY ((1UL << 32) | 2) + +#define VMGEXIT_PSC_OP_PRIVATE 1 +#define VMGEXIT_PSC_OP_SHARED 2 struct psc_hdr { u16 cur_entry; diff --git a/arch/x86/kvm/svm/sev.c b/arch/x86/kvm/svm/sev.c index 438f2e8b8152..46669431b53d 100644 --- a/arch/x86/kvm/svm/sev.c +++ b/arch/x86/kvm/svm/sev.c @@ -3274,6 +3274,10 @@ static int sev_es_validate_vmgexit(struct vcpu_svm *svm) case SVM_VMGEXIT_HV_FEATURES: case SVM_VMGEXIT_TERM_REQUEST: break; + case SVM_VMGEXIT_PSC: + if (!sev_snp_guest(vcpu->kvm) || !kvm_ghcb_sw_scratch_is_valid(svm)) + goto vmgexit_err; + break; default: reason = GHCB_ERR_INVALID_EVENT; goto vmgexit_err; @@ -3503,6 +3507,183 @@ static int snp_begin_psc_msr(struct vcpu_svm *svm, u64 ghcb_msr) return 0; /* forward request to userspace */ } +struct psc_buffer { + struct psc_hdr hdr; + struct psc_entry entries[]; +} __packed; + +static int snp_begin_psc(struct vcpu_svm *svm, struct psc_buffer *psc); + +static void snp_complete_psc(struct vcpu_svm *svm, u64 psc_ret) +{ + svm->sev_es.psc_inflight = 0; + svm->sev_es.psc_idx = 0; + svm->sev_es.psc_2m = false; + ghcb_set_sw_exit_info_2(svm->sev_es.ghcb, VMGEXIT_PSC_ERROR_GENERIC); +} + +static void __snp_complete_one_psc(struct vcpu_svm *svm) +{ + struct psc_buffer *psc = svm->sev_es.ghcb_sa; + struct psc_entry *entries = psc->entries; + struct psc_hdr *hdr = &psc->hdr; + __u16 idx; + + /* + * Everything in-flight has been processed successfully. Update the + * corresponding entries in the guest's PSC buffer and zero out the + * count of in-flight PSC entries. + */ + for (idx = svm->sev_es.psc_idx; svm->sev_es.psc_inflight; + svm->sev_es.psc_inflight--, idx++) { + struct psc_entry *entry = &entries[idx]; + + entry->cur_page = entry->pagesize ? 512 : 1; + } + + hdr->cur_entry = idx; +} + +static int snp_complete_one_psc(struct kvm_vcpu *vcpu) +{ + struct vcpu_svm *svm = to_svm(vcpu); + struct psc_buffer *psc = svm->sev_es.ghcb_sa; + + if (vcpu->run->hypercall.ret) { + snp_complete_psc(svm, VMGEXIT_PSC_ERROR_GENERIC); + return 1; /* resume guest */ + } + + __snp_complete_one_psc(svm); + + /* Handle the next range (if any). */ + return snp_begin_psc(svm, psc); +} + +static int snp_begin_psc(struct vcpu_svm *svm, struct psc_buffer *psc) +{ + struct psc_entry *entries = psc->entries; + struct kvm_vcpu *vcpu = &svm->vcpu; + struct psc_hdr *hdr = &psc->hdr; + struct psc_entry entry_start; + u16 idx, idx_start, idx_end; + int npages; + bool huge; + u64 gfn; + + if (!(vcpu->kvm->arch.hypercall_exit_enabled & (1 << KVM_HC_MAP_GPA_RANGE))) { + snp_complete_psc(svm, VMGEXIT_PSC_ERROR_GENERIC); + return 1; + } + +next_range: + /* There should be no other PSCs in-flight at this point. */ + if (WARN_ON_ONCE(svm->sev_es.psc_inflight)) { + snp_complete_psc(svm, VMGEXIT_PSC_ERROR_GENERIC); + return 1; + } + + /* + * The PSC descriptor buffer can be modified by a misbehaved guest after + * validation, so take care to only use validated copies of values used + * for things like array indexing. + */ + idx_start = hdr->cur_entry; + idx_end = hdr->end_entry; + + if (idx_end >= VMGEXIT_PSC_MAX_COUNT) { + snp_complete_psc(svm, VMGEXIT_PSC_ERROR_INVALID_HDR); + return 1; + } + + /* Find the start of the next range which needs processing. */ + for (idx = idx_start; idx <= idx_end; idx++, hdr->cur_entry++) { + entry_start = entries[idx]; + + gfn = entry_start.gfn; + huge = entry_start.pagesize; + npages = huge ? 512 : 1; + + if (entry_start.cur_page > npages || !IS_ALIGNED(gfn, npages)) { + snp_complete_psc(svm, VMGEXIT_PSC_ERROR_INVALID_ENTRY); + return 1; + } + + if (entry_start.cur_page) { + /* + * If this is a partially-completed 2M range, force 4K handling + * for the remaining pages since they're effectively split at + * this point. Subsequent code should ensure this doesn't get + * combined with adjacent PSC entries where 2M handling is still + * possible. + */ + npages -= entry_start.cur_page; + gfn += entry_start.cur_page; + huge = false; + } + + if (npages) + break; + } + + if (idx > idx_end) { + /* Nothing more to process. */ + snp_complete_psc(svm, 0); + return 1; + } + + svm->sev_es.psc_2m = huge; + svm->sev_es.psc_idx = idx; + svm->sev_es.psc_inflight = 1; + + /* + * Find all subsequent PSC entries that contain adjacent GPA + * ranges/operations and can be combined into a single + * KVM_HC_MAP_GPA_RANGE exit. + */ + while (++idx <= idx_end) { + struct psc_entry entry = entries[idx]; + + if (entry.operation != entry_start.operation || + entry.gfn != entry_start.gfn + npages || + entry.cur_page || !!entry.pagesize != huge) + break; + + svm->sev_es.psc_inflight++; + npages += huge ? 512 : 1; + } + + switch (entry_start.operation) { + case VMGEXIT_PSC_OP_PRIVATE: + case VMGEXIT_PSC_OP_SHARED: + vcpu->run->exit_reason = KVM_EXIT_HYPERCALL; + vcpu->run->hypercall.nr = KVM_HC_MAP_GPA_RANGE; + vcpu->run->hypercall.args[0] = gfn_to_gpa(gfn); + vcpu->run->hypercall.args[1] = npages; + vcpu->run->hypercall.args[2] = entry_start.operation == VMGEXIT_PSC_OP_PRIVATE + ? KVM_MAP_GPA_RANGE_ENCRYPTED + : KVM_MAP_GPA_RANGE_DECRYPTED; + vcpu->run->hypercall.args[2] |= entry_start.pagesize + ? KVM_MAP_GPA_RANGE_PAGE_SZ_2M + : KVM_MAP_GPA_RANGE_PAGE_SZ_4K; + vcpu->arch.complete_userspace_io = snp_complete_one_psc; + return 0; /* forward request to userspace */ + default: + /* + * Only shared/private PSC operations are currently supported, so if the + * entire range consists of unsupported operations (e.g. SMASH/UNSMASH), + * then consider the entire range completed and avoid exiting to + * userspace. In theory snp_complete_psc() can always be called directly + * at this point to complete the current range and start the next one, + * but that could lead to unexpected levels of recursion. + */ + __snp_complete_one_psc(svm); + goto next_range; + } + + unreachable(); +} + static int sev_handle_vmgexit_msr_protocol(struct vcpu_svm *svm) { struct vmcb_control_area *control = &svm->vmcb->control; @@ -3761,6 +3942,13 @@ int sev_handle_vmgexit(struct kvm_vcpu *vcpu) vcpu->run->system_event.ndata = 1; vcpu->run->system_event.data[0] = control->ghcb_gpa; break; + case SVM_VMGEXIT_PSC: + ret = setup_vmgexit_scratch(svm, true, control->exit_info_2); + if (ret) + break; + + ret = snp_begin_psc(svm, svm->sev_es.ghcb_sa); + 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 202ac5494c19..32d37ef5f580 100644 --- a/arch/x86/kvm/svm/svm.h +++ b/arch/x86/kvm/svm/svm.h @@ -210,6 +210,11 @@ struct vcpu_sev_es_state { bool ghcb_sa_sync; bool ghcb_sa_free; + /* SNP Page-State-Change buffer entries currently being processed */ + u16 psc_idx; + u16 psc_inflight; + bool psc_2m; + u64 ghcb_registered_gpa; }; From patchwork Fri May 10 21:10:15 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Michael Roth X-Patchwork-Id: 13661949 Received: from NAM12-DM6-obe.outbound.protection.outlook.com (mail-dm6nam12on2083.outbound.protection.outlook.com [40.107.243.83]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 35CA510965; Fri, 10 May 2024 21:17:04 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=40.107.243.83 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1715375827; cv=fail; b=qUWVUCWB57i06F+pBVTeNRktsq69/p7Avqpg8OoiqGl9BTllzpdYjES8UFvz6Xjv5+DozMxQ1dC/pAJk7H65tycePQ3TjCRyRJoDYh/oDrCTAOppThUEGEoR487BtYrvU3ZcNkPigZ2EI+/RXP/3Gc2p0a1WbBwzIQ/d6OlNCVY= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1715375827; c=relaxed/simple; bh=TdNxHHKH2EbpTqFZiKuFqqUIp7P7PwKuvZEqXvI+yHc=; h=From:To:CC:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=hwJP3jRGN7/ZO1EXbYEr5j7SX4b+1J6spLqs3HxlxvO8Ywi1nCuXZzObtQwqSI61qeZ4TseEkqvGZdbTTmHAeQsXCbmufEVqHk/jKBSjwyR8/5A/UZnTevuA3kyV7e4k12favbDBNmCXkF8iyYRd4KhhaY/yGmWu2ND2gXbBix0= ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=amd.com; spf=fail smtp.mailfrom=amd.com; dkim=pass (1024-bit key) header.d=amd.com header.i=@amd.com header.b=YezYwwGh; arc=fail smtp.client-ip=40.107.243.83 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=amd.com Authentication-Results: smtp.subspace.kernel.org; spf=fail smtp.mailfrom=amd.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=amd.com header.i=@amd.com header.b="YezYwwGh" ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=XkhYAwo6YQbUdW4uIyVhL1Hv0aZBVL1XjWOifzq+Hao2XBaJB/wkgceCu3tB0SUyDx7NUHm/WpmNbKfjikeB7qHslErQ2OCPFwKTp5sEhBWChM8hVRdV/q9yciwAtqqLjxAKAS4kbBBQTu1OsP4p0cC1ScuOn5A0sOZcRA+2v1QcLteEezqRgXeM++yBUiw2Td4T53l/2rtnciRWNVr3UbLPdUqwrXUXx31YIJThEi1zcy8XA+7u+sBo/mlsxirw1AFCguxhmZxl0T13XTITeD0KslxPzjPlFDzNfjdinC+H7VlvReSlBojugGcnBcvpQ0PnP7yyfsG7jjCysbyc3g== 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=QlA6wwvx5NMnfKyPenKsCZ7bQrqbmcr4ATyGSvj81Es=; b=Eap0w3LQHi8wqa/S2sdJcxgaMyyXZ1vh6s1JmYNXuPpboBSwvvOCo6tZEYFS1LoBjjSJWAPZ+p2wAmfhpSQ4tgo1NzHBP90ruUEJWj0IlDg+gD0eGxUevDq+hRRQIFVj/jy2z8G1v0TZFE7ozAtEUXHiWWcbcLIVVfOjZXywA0UjWbw3dS7qbZ4d5+6gCx+uZMUurCMJLbCF+qWDlicop++LO4+BctTkgpOO9UcLv3M5sGFluLWO9YDzoiU0SBj5vyZfhhx6Dl/G0C9sfZk/Fke5ogteurvdiCc0O4BYz4yD67kN8Mv/WYyPgy7cMwffRUZFMa1S55u46mC5igQUxA== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 165.204.84.17) smtp.rcpttodomain=redhat.com 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=QlA6wwvx5NMnfKyPenKsCZ7bQrqbmcr4ATyGSvj81Es=; b=YezYwwGhRTTA17dcVnmr+lnuaykt1Q8a9lJDk85af1sFOLkIrzQ19sMKTn6h1kb/v3kZNjYuzciQ9sAza7t1DcolecJhaWM+LY2y1FEgOKMND3kt24EVzBKbAQGARkFtIZlosMU+vYdfnNb/U3x5j6XUZHvhlQhiiSx9Wzl4e/c= Received: from BN1PR10CA0016.namprd10.prod.outlook.com (2603:10b6:408:e0::21) by DS0PR12MB6605.namprd12.prod.outlook.com (2603:10b6:8:d3::11) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7544.48; Fri, 10 May 2024 21:17:02 +0000 Received: from BN1PEPF00004686.namprd03.prod.outlook.com (2603:10b6:408:e0:cafe::bb) by BN1PR10CA0016.outlook.office365.com (2603:10b6:408:e0::21) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7544.51 via Frontend Transport; Fri, 10 May 2024 21:17:02 +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 BN1PEPF00004686.mail.protection.outlook.com (10.167.243.91) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.7544.18 via Frontend Transport; Fri, 10 May 2024 21:17:02 +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; Fri, 10 May 2024 16:17:02 -0500 From: Michael Roth To: Paolo Bonzini CC: , , Sean Christopherson , Brijesh Singh , Ashish Kalra Subject: [PULL 10/19] KVM: SEV: Add support to handle RMP nested page faults Date: Fri, 10 May 2024 16:10:15 -0500 Message-ID: <20240510211024.556136-11-michael.roth@amd.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20240510211024.556136-1-michael.roth@amd.com> References: <20240510211024.556136-1-michael.roth@amd.com> Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 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: BN1PEPF00004686:EE_|DS0PR12MB6605:EE_ X-MS-Office365-Filtering-Correlation-Id: 45d6774b-6c35-464a-1407-08dc71368948 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0;ARA:13230031|376005|36860700004|82310400017|1800799015; X-Microsoft-Antispam-Message-Info: nlw3w5E3mPko//NAdL6IEkCqAh71tJQvSX1dq/SqDieDUgri+K6t6OL+7IcQUzUPgwQeVI+q9xliQmfZc3C2JxhZoxWLnrfyjXPtzfGcpoONE8yOKC1AZ0R8coPqUK9FMj+F7G6cFec25NcH10wen1GVlVd8W2bwsAU2JOiTCD7OMYJu2lYfO7mDgBqE+/Z8dyAyO2b5PB3BlTpryuL8xAnQ0piSGUrL4SE0X+6/tD7ANrVAwQuP+KXwA5PRJg5lxxZ4BNh3kQa9G/cDIfK1niSc6D7QxgnAenJLwSXLM8+d98cGHf2Aq8PuBISVHRHt8d4qtbe+YdXTYUaeFuouymYvzrZQ3dQ5FX74us61ZW0xleuRUhCx7JTl9HjkF4lDIu9UsdjER/EtYSSawH5Cq94uNxZYe43w8bcf5M+McXBmvCPXuhGSSPjfq4d2dDI1Vbuk21DoY7SxWopE0ogcoE23G0We5dk5jiZNuOUtMkne5FAdgOmpifDta2jyH2u9I53ornSCiAoH192AHgqLg+Tast6ENG1WFuvPtjbpmgp46O31V/qnHClHDH9hu7fXbpcW4pdXEkKeb6XTZfZPHxny2S4quRiGW4ysbkH4LfgaNxTE0upX10LJ4+e9I8gr8IP4WCHcnjGzdd4i2Qsi32EQSmAasrzZfVhBaYcXHkTJkmLM10VtM84YEMsldYVVUPwRjL/ALy2dKlrp3EBJxq5ZgLbOHwUW/AMgwAUdO8yekBde9nIaKjDHzwXZakgsjJnU5Z47KnB9kZd4BKpsY79cWXxYIggiVAEEi30bqopDN9qGuGFZLV7zHoG2INwyKTxraHgLm1l3gWdCmfqmn/i5x2T4L5ba1zbbUOblEe/TluyZTijrhgI/EeYpeps70crJ5NpqzxsarZ8DqlEE3YLW5RgUCsegE2wY/AjNQw7MkZyBOrhg+SGQbRiAyEQa2l1BtSDQnXDi5DANV5k70Bfmv3vEHmrnR+IB3wrvuSbeN9LRL4SPvkXL4AGkb6v/upv0wnvZsFOF4O+tDZXNcxiZSwlpeNduBkBb/V4AwuDtmLQn2rq76ImVQkn2OqOyYWCiEhyMG6G9Y81ihd357UHtExhCXB4L1CLe7apmH157OCNk9X4a145hErIp7mvBUBO2vsyTcZ/Zne64hG2U3zcasjEcWFr0rxT+HKik/JzqKJptbd1YZsrkyWJTmkodplG3rjYBhVlv1p/GPuJHgUMHgenDbbla/a4ONfiARQLi0q0ZDk3RuMkFWkrZp3+yqyOCzhBG2TTMppjEKoAkVTkSBMtUfnKp1xRySfhtqEqR5xBIfK15Sd7joyhINX+j0E0Np1sg2VgTKj2cKuT2SrdmYvg4IHxjvUxRXm+XutQ= 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)(36860700004)(82310400017)(1800799015);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 10 May 2024 21:17:02.4674 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 45d6774b-6c35-464a-1407-08dc71368948 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: BN1PEPF00004686.namprd03.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: DS0PR12MB6605 From: Brijesh Singh When SEV-SNP is enabled in the guest, the hardware places restrictions on all memory accesses based on the contents of the RMP table. When hardware encounters RMP check failure caused by the guest memory access it raises the #NPF. The error code contains additional information on the access type. See the APM volume 2 for additional information. When using gmem, RMP faults resulting from mismatches between the state in the RMP table vs. what the guest expects via its page table result in KVM_EXIT_MEMORY_FAULTs being forwarded to userspace to handle. This means the only expected case that needs to be handled in the kernel is when the page size of the entry in the RMP table is larger than the mapping in the nested page table, in which case a PSMASH instruction needs to be issued to split the large RMP entry into individual 4K entries so that subsequent accesses can succeed. Signed-off-by: Brijesh Singh Co-developed-by: Michael Roth Signed-off-by: Michael Roth Signed-off-by: Ashish Kalra Message-ID: <20240501085210.2213060-12-michael.roth@amd.com> Signed-off-by: Paolo Bonzini --- arch/x86/include/asm/kvm_host.h | 1 + arch/x86/include/asm/sev.h | 3 + arch/x86/kvm/mmu.h | 2 - arch/x86/kvm/mmu/mmu.c | 1 + arch/x86/kvm/svm/sev.c | 109 ++++++++++++++++++++++++++++++++ arch/x86/kvm/svm/svm.c | 14 ++-- arch/x86/kvm/svm/svm.h | 3 + arch/x86/kvm/trace.h | 31 +++++++++ arch/x86/kvm/x86.c | 1 + 9 files changed, 159 insertions(+), 6 deletions(-) diff --git a/arch/x86/include/asm/kvm_host.h b/arch/x86/include/asm/kvm_host.h index ea992f9b6e68..5830d42232da 100644 --- a/arch/x86/include/asm/kvm_host.h +++ b/arch/x86/include/asm/kvm_host.h @@ -1945,6 +1945,7 @@ void kvm_mmu_slot_leaf_clear_dirty(struct kvm *kvm, const struct kvm_memory_slot *memslot); void kvm_mmu_invalidate_mmio_sptes(struct kvm *kvm, u64 gen); void kvm_mmu_change_mmu_pages(struct kvm *kvm, unsigned long kvm_nr_mmu_pages); +void kvm_zap_gfn_range(struct kvm *kvm, gfn_t gfn_start, gfn_t gfn_end); int load_pdptrs(struct kvm_vcpu *vcpu, unsigned long cr3); diff --git a/arch/x86/include/asm/sev.h b/arch/x86/include/asm/sev.h index 93ed60080cfe..14394407245c 100644 --- a/arch/x86/include/asm/sev.h +++ b/arch/x86/include/asm/sev.h @@ -91,6 +91,9 @@ extern bool handle_vc_boot_ghcb(struct pt_regs *regs); /* RMUPDATE detected 4K page and 2MB page overlap. */ #define RMPUPDATE_FAIL_OVERLAP 4 +/* PSMASH failed due to concurrent access by another CPU */ +#define PSMASH_FAIL_INUSE 3 + /* RMP page size */ #define RMP_PG_SIZE_4K 0 #define RMP_PG_SIZE_2M 1 diff --git a/arch/x86/kvm/mmu.h b/arch/x86/kvm/mmu.h index 2343c9f00e31..e3cb35b9396d 100644 --- a/arch/x86/kvm/mmu.h +++ b/arch/x86/kvm/mmu.h @@ -251,8 +251,6 @@ static inline bool kvm_mmu_honors_guest_mtrrs(struct kvm *kvm) return __kvm_mmu_honors_guest_mtrrs(kvm_arch_has_noncoherent_dma(kvm)); } -void kvm_zap_gfn_range(struct kvm *kvm, gfn_t gfn_start, gfn_t gfn_end); - int kvm_arch_write_log_dirty(struct kvm_vcpu *vcpu); int kvm_mmu_post_init_vm(struct kvm *kvm); diff --git a/arch/x86/kvm/mmu/mmu.c b/arch/x86/kvm/mmu/mmu.c index 9c7ab06ce454..83025fe2d17d 100644 --- a/arch/x86/kvm/mmu/mmu.c +++ b/arch/x86/kvm/mmu/mmu.c @@ -6783,6 +6783,7 @@ static bool kvm_mmu_zap_collapsible_spte(struct kvm *kvm, return need_tlb_flush; } +EXPORT_SYMBOL_GPL(kvm_zap_gfn_range); static void kvm_rmap_zap_collapsible_sptes(struct kvm *kvm, const struct kvm_memory_slot *slot) diff --git a/arch/x86/kvm/svm/sev.c b/arch/x86/kvm/svm/sev.c index 46669431b53d..515bd6154a4b 100644 --- a/arch/x86/kvm/svm/sev.c +++ b/arch/x86/kvm/svm/sev.c @@ -3465,6 +3465,23 @@ static void set_ghcb_msr(struct vcpu_svm *svm, u64 value) svm->vmcb->control.ghcb_gpa = value; } +static int snp_rmptable_psmash(kvm_pfn_t pfn) +{ + int ret; + + pfn = pfn & ~(KVM_PAGES_PER_HPAGE(PG_LEVEL_2M) - 1); + + /* + * PSMASH_FAIL_INUSE indicates another processor is modifying the + * entry, so retry until that's no longer the case. + */ + do { + ret = psmash(pfn); + } while (ret == PSMASH_FAIL_INUSE); + + return ret; +} + static int snp_complete_psc_msr(struct kvm_vcpu *vcpu) { struct vcpu_svm *svm = to_svm(vcpu); @@ -4229,3 +4246,95 @@ struct page *snp_safe_alloc_page(struct kvm_vcpu *vcpu) return p; } + +void sev_handle_rmp_fault(struct kvm_vcpu *vcpu, gpa_t gpa, u64 error_code) +{ + struct kvm_memory_slot *slot; + struct kvm *kvm = vcpu->kvm; + int order, rmp_level, ret; + bool assigned; + kvm_pfn_t pfn; + gfn_t gfn; + + gfn = gpa >> PAGE_SHIFT; + + /* + * The only time RMP faults occur for shared pages is when the guest is + * triggering an RMP fault for an implicit page-state change from + * shared->private. Implicit page-state changes are forwarded to + * userspace via KVM_EXIT_MEMORY_FAULT events, however, so RMP faults + * for shared pages should not end up here. + */ + if (!kvm_mem_is_private(kvm, gfn)) { + pr_warn_ratelimited("SEV: Unexpected RMP fault for non-private GPA 0x%llx\n", + gpa); + return; + } + + slot = gfn_to_memslot(kvm, gfn); + if (!kvm_slot_can_be_private(slot)) { + pr_warn_ratelimited("SEV: Unexpected RMP fault, non-private slot for GPA 0x%llx\n", + gpa); + return; + } + + ret = kvm_gmem_get_pfn(kvm, slot, gfn, &pfn, &order); + if (ret) { + pr_warn_ratelimited("SEV: Unexpected RMP fault, no backing page for private GPA 0x%llx\n", + gpa); + return; + } + + ret = snp_lookup_rmpentry(pfn, &assigned, &rmp_level); + if (ret || !assigned) { + pr_warn_ratelimited("SEV: Unexpected RMP fault, no assigned RMP entry found for GPA 0x%llx PFN 0x%llx error %d\n", + gpa, pfn, ret); + goto out_no_trace; + } + + /* + * There are 2 cases where a PSMASH may be needed to resolve an #NPF + * with PFERR_GUEST_RMP_BIT set: + * + * 1) RMPADJUST/PVALIDATE can trigger an #NPF with PFERR_GUEST_SIZEM + * bit set if the guest issues them with a smaller granularity than + * what is indicated by the page-size bit in the 2MB RMP entry for + * the PFN that backs the GPA. + * + * 2) Guest access via NPT can trigger an #NPF if the NPT mapping is + * smaller than what is indicated by the 2MB RMP entry for the PFN + * that backs the GPA. + * + * In both these cases, the corresponding 2M RMP entry needs to + * be PSMASH'd to 512 4K RMP entries. If the RMP entry is already + * split into 4K RMP entries, then this is likely a spurious case which + * can occur when there are concurrent accesses by the guest to a 2MB + * GPA range that is backed by a 2MB-aligned PFN who's RMP entry is in + * the process of being PMASH'd into 4K entries. These cases should + * resolve automatically on subsequent accesses, so just ignore them + * here. + */ + if (rmp_level == PG_LEVEL_4K) + goto out; + + ret = snp_rmptable_psmash(pfn); + if (ret) { + /* + * Look it up again. If it's 4K now then the PSMASH may have + * raced with another process and the issue has already resolved + * itself. + */ + if (!snp_lookup_rmpentry(pfn, &assigned, &rmp_level) && + assigned && rmp_level == PG_LEVEL_4K) + goto out; + + pr_warn_ratelimited("SEV: Unable to split RMP entry for GPA 0x%llx PFN 0x%llx ret %d\n", + gpa, pfn, ret); + } + + kvm_zap_gfn_range(kvm, gfn, gfn + PTRS_PER_PMD); +out: + trace_kvm_rmp_fault(vcpu, gpa, pfn, error_code, rmp_level, ret); +out_no_trace: + put_page(pfn_to_page(pfn)); +} diff --git a/arch/x86/kvm/svm/svm.c b/arch/x86/kvm/svm/svm.c index 66d5e2e46a66..bdaf39571817 100644 --- a/arch/x86/kvm/svm/svm.c +++ b/arch/x86/kvm/svm/svm.c @@ -2044,6 +2044,7 @@ static int pf_interception(struct kvm_vcpu *vcpu) static int npf_interception(struct kvm_vcpu *vcpu) { struct vcpu_svm *svm = to_svm(vcpu); + int rc; u64 fault_address = svm->vmcb->control.exit_info_2; u64 error_code = svm->vmcb->control.exit_info_1; @@ -2061,10 +2062,15 @@ static int npf_interception(struct kvm_vcpu *vcpu) error_code |= PFERR_PRIVATE_ACCESS; trace_kvm_page_fault(vcpu, fault_address, error_code); - return kvm_mmu_page_fault(vcpu, fault_address, error_code, - static_cpu_has(X86_FEATURE_DECODEASSISTS) ? - svm->vmcb->control.insn_bytes : NULL, - svm->vmcb->control.insn_len); + rc = kvm_mmu_page_fault(vcpu, fault_address, error_code, + static_cpu_has(X86_FEATURE_DECODEASSISTS) ? + svm->vmcb->control.insn_bytes : NULL, + svm->vmcb->control.insn_len); + + if (rc > 0 && error_code & PFERR_GUEST_RMP_MASK) + sev_handle_rmp_fault(vcpu, fault_address, error_code); + + return rc; } static int db_interception(struct kvm_vcpu *vcpu) diff --git a/arch/x86/kvm/svm/svm.h b/arch/x86/kvm/svm/svm.h index 32d37ef5f580..36b573427b85 100644 --- a/arch/x86/kvm/svm/svm.h +++ b/arch/x86/kvm/svm/svm.h @@ -728,6 +728,7 @@ void sev_hardware_unsetup(void); int sev_cpu_init(struct svm_cpu_data *sd); int sev_dev_get_attr(u32 group, u64 attr, u64 *val); extern unsigned int max_sev_asid; +void sev_handle_rmp_fault(struct kvm_vcpu *vcpu, gpa_t gpa, u64 error_code); #else static inline struct page *snp_safe_alloc_page(struct kvm_vcpu *vcpu) { return alloc_page(GFP_KERNEL_ACCOUNT | __GFP_ZERO); @@ -741,6 +742,8 @@ static inline void sev_hardware_unsetup(void) {} static inline int sev_cpu_init(struct svm_cpu_data *sd) { return 0; } static inline int sev_dev_get_attr(u32 group, u64 attr, u64 *val) { return -ENXIO; } #define max_sev_asid 0 +static inline void sev_handle_rmp_fault(struct kvm_vcpu *vcpu, gpa_t gpa, u64 error_code) {} + #endif /* vmenter.S */ diff --git a/arch/x86/kvm/trace.h b/arch/x86/kvm/trace.h index c6b4b1728006..3531a187d5d9 100644 --- a/arch/x86/kvm/trace.h +++ b/arch/x86/kvm/trace.h @@ -1834,6 +1834,37 @@ TRACE_EVENT(kvm_vmgexit_msr_protocol_exit, __entry->vcpu_id, __entry->ghcb_gpa, __entry->result) ); +/* + * Tracepoint for #NPFs due to RMP faults. + */ +TRACE_EVENT(kvm_rmp_fault, + TP_PROTO(struct kvm_vcpu *vcpu, u64 gpa, u64 pfn, u64 error_code, + int rmp_level, int psmash_ret), + TP_ARGS(vcpu, gpa, pfn, error_code, rmp_level, psmash_ret), + + TP_STRUCT__entry( + __field(unsigned int, vcpu_id) + __field(u64, gpa) + __field(u64, pfn) + __field(u64, error_code) + __field(int, rmp_level) + __field(int, psmash_ret) + ), + + TP_fast_assign( + __entry->vcpu_id = vcpu->vcpu_id; + __entry->gpa = gpa; + __entry->pfn = pfn; + __entry->error_code = error_code; + __entry->rmp_level = rmp_level; + __entry->psmash_ret = psmash_ret; + ), + + TP_printk("vcpu %u gpa %016llx pfn 0x%llx error_code 0x%llx rmp_level %d psmash_ret %d", + __entry->vcpu_id, __entry->gpa, __entry->pfn, + __entry->error_code, __entry->rmp_level, __entry->psmash_ret) +); + #endif /* _TRACE_KVM_H */ #undef TRACE_INCLUDE_PATH diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index be43c77315ce..60ebe3ee9118 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -14003,6 +14003,7 @@ EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_vmgexit_enter); EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_vmgexit_exit); EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_vmgexit_msr_protocol_enter); EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_vmgexit_msr_protocol_exit); +EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_rmp_fault); static int __init kvm_x86_init(void) { From patchwork Fri May 10 21:10:16 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Michael Roth X-Patchwork-Id: 13661951 Received: from NAM11-CO1-obe.outbound.protection.outlook.com (mail-co1nam11on2078.outbound.protection.outlook.com [40.107.220.78]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 168B613B586; Fri, 10 May 2024 21:17:48 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=40.107.220.78 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1715375871; cv=fail; b=X8sjr0Psbq/vrK0tOVO3cb3RJuaDHx74JnAkTzLrNsUoyZwB4dkTFPpEdyNjv1KrIa3qH7T0JauByVdIZ9cOOF+NgS1Jt4Yyw+wn3h8DsowWnXibso2G9ChE9BKcVnnMoEPaXOc5vMELWnKtZJMgCHRK5s55em0ZkYBZx7KOzNI= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1715375871; c=relaxed/simple; bh=PENgeX+vrApi6IpkTW0KDgA7CblzLJUlO3kphnZQ5Fc=; h=From:To:CC:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=OjUeVeTY+FFXJa/2MtLyGJ3xRK/dyZlEzgGtNia0TpnWJ63bfPfqQwJy8UnsPmFuqi+1wVAphDk0QnkCXXDtqxq5HMfy2KoWWr0UlRyXouStn8Hkkf/v3Gc+hTzZC8bBIZrSLexH2IiAf4Ohmlgi+YLz69MgBXQqSWenyaaKw6c= ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=amd.com; spf=fail smtp.mailfrom=amd.com; dkim=pass (1024-bit key) header.d=amd.com header.i=@amd.com header.b=GQrmbXgR; arc=fail smtp.client-ip=40.107.220.78 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=amd.com Authentication-Results: smtp.subspace.kernel.org; spf=fail smtp.mailfrom=amd.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=amd.com header.i=@amd.com header.b="GQrmbXgR" ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=hhAmBaZQHCX6lCfPYl8yN42Mg+LJFqdlI8jwtnMKCtNSlyAPHebk2FGoLXvBYSP5PqRHsZnyv6WZJyWmiNBVsTZtlmB6HlFKBB42tP0XTAcAEXFoRVuci+zEYcswcc6h4Ahn1I1UnduCVFA2yVG9pu1ZLP5jTJ9NeftwGLdTvjZLCvDTEDR4hyn8RRb6tjy0oj+ookCIf5b4ihgySbytJ15w602iASj9IxMb+DOcCqfbQq2mihNwXT5vODlst0E3QofuWu1C09Te8OxSIyyc9pwP4+YETpYBDz0/EzCpMILprfRgo8gITy95Dt3H5ZzXHSZ44sL+/nor+sqzfGvnug== 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=lTxXD2A9DOFqRa/MSHJvs5BzzSriZQQApOCwI+ouCr0=; b=PYRYxLoFaLNLdD4zOUwHywFy0MIGFYAXZGygbJE2MFolNb8erVKK0yQfylD1pTkB7LeE3oapdg3ul/ZJjYK0rxme+ZVvKzheY9dEUXeGmG3lNuSWLAL1MoXQFao20Z3PzWx0A2p1PCltGbTTSZADWoFLigLqVA8R2RlabBDLzFsqjr1U5LH23z3H1OuxUTFy9oIjds7vk70Fm1XD1rKgDwSlG4e1u07+pqK5/6oVyJ1EtdIypWa+rOjUoxPr5BkSaPG455VMWpsjzySghFokjS6rLK/cfz9hU840fWdKqi7PiuQowuRj1bOns6D7D3cQwwMwoxJ0SxZbbMx08OlwpA== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 165.204.84.17) smtp.rcpttodomain=redhat.com 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=lTxXD2A9DOFqRa/MSHJvs5BzzSriZQQApOCwI+ouCr0=; b=GQrmbXgR8RojPeiDbdXm+kr0amdkaM1FHnAgPzAfdqH6mb0rRREPR0q1GzPyexIa0maWtdC2ZVd2bJdsgxMDRgMc4qhXeDSXlNg2zKL2xN4gk+orv8w4jRSNCNkhIQ+xvi5ORCIZX4vy/x9w75FjcC+/yiaPWxHzGX9MzlMvF+w= Received: from BN6PR17CA0037.namprd17.prod.outlook.com (2603:10b6:405:75::26) by IA0PR12MB7553.namprd12.prod.outlook.com (2603:10b6:208:43f::18) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7544.49; Fri, 10 May 2024 21:17:44 +0000 Received: from BN1PEPF00004684.namprd03.prod.outlook.com (2603:10b6:405:75:cafe::f0) by BN6PR17CA0037.outlook.office365.com (2603:10b6:405:75::26) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7544.49 via Frontend Transport; Fri, 10 May 2024 21:17:44 +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 BN1PEPF00004684.mail.protection.outlook.com (10.167.243.90) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.7544.18 via Frontend Transport; Fri, 10 May 2024 21:17:43 +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; Fri, 10 May 2024 16:17:43 -0500 From: Michael Roth To: Paolo Bonzini CC: , , Sean Christopherson , Tom Lendacky , Brijesh Singh , Ashish Kalra Subject: [PULL 11/19] KVM: SEV: Support SEV-SNP AP Creation NAE event Date: Fri, 10 May 2024 16:10:16 -0500 Message-ID: <20240510211024.556136-12-michael.roth@amd.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20240510211024.556136-1-michael.roth@amd.com> References: <20240510211024.556136-1-michael.roth@amd.com> Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 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: BN1PEPF00004684:EE_|IA0PR12MB7553:EE_ X-MS-Office365-Filtering-Correlation-Id: de4b3266-4a85-49af-ff50-08dc7136a204 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0;ARA:13230031|82310400017|376005|36860700004|1800799015; X-Microsoft-Antispam-Message-Info: RkQPs4g6oLn3fDmWKlPyULd5A3y0fMjeGk8UlQTfX+glaujfGRRK69BxPY7DQSVgt1ExxS7MQqfm7aZUYq4g3WVMspbTZDT2C1CuycH2ZPBJfENtUR8tFP6MOV0bcRscUdoPMlnKLcFOfwObZVLA4P2mGS9vtFY60hOGQm87SPWwHCvAUsRgHwMin9HQs/MvFAAnYV+J4sk5HYHMEwmBwQmWWQ2oADnTWK+ulNAYScEEuDyIRmISN+7JCouHEochyGlvSXAWg4Qbj1feC/EyAfd5jIbN9jGrTHJCdyFRc1HRWP0Ru/sqscU4Z+zNsUNvNvRqR2gNfooXS3Y3CQfDr5TcDx9MKRSGzg6RDVt59Qb7tTwfrVZGNnd9o+pYTdkY6/g26FJykPmIj0FoJAN1qtFqS/8rDTk8D7i/Cuha+HHosUps8UnmOyMmI+TlQzeZmdENB7QenzUL0XRMOe6ID8JJ56/NdsOIk+UjTUm1pATGrTeqIcEVR8oSTrJBRUaykaCdVr3AF+5wuRLjdKKweKKgQa2T5RaUWmMrskxYjoEGQITK3rVsHMKaEAt8Dtm3WNR2NKSO8jKRxE4r07WpyFF8t09LtF7PErdty9oyUFzMunIxx33yKBklm6T2p6lgu1swKYnPoySXNvFZ2RyhQi8R+sWz56usY4zUkMupxLhTDRlnRz5d28/Fkj1M5E8bGvworPkRS534+tXroMduX1sxFcoV5ROpH2NbGWSZ9sjKky/z7N++z/UztCDOpVZAf3QzWAhd+E/mHojdijrDdukrXCoZlBRCJHSKl/jZ9fifEE/c+hJLmhu06otAGLyIYi3soR2jK+RuQy/t85nxeTUjrG8QLzgnM2z9+WlOLvQqFn9gkHIO9LZ+C5YIy1+El5PI5JXOHHSKCAx7MIUZRX4ZnSLb1O1082PA/LDoJ4y22v1f0/J9hAY+PCYqyN7gN8xkMUJhRmQdKWX+yN36ZC4ffzl3o1xgI46iZbezUPedopmYza0dc2hdxhmkOGFBYhmgOA96dJJooAuE4i4BRahFQbjFFFzQDeJPG4tWaV1/bT9aD3YP/az6c54fXH0DHzahQ7w5wvZ7DwHHrDBcUaWHRgvfpwmrVy7GHmsGFQ5l4sb83iepO6oGEilr0z+y+XTi9pCdGPau2gVVzTRtg57Rn9l5lAL3vahunnVCBohcfGlaGMJimOa0tmwqRkh+Q722pyjp/uBexgAK5bpJ/asRijWnO6ZZzIAy9oosLu+QLcrblo3TZ1kKl29ZpUBOOTXq3tVrGcAj1J+/MrCvHQ0cUOt/dUSEll+MSoM1x7g3fB4OwLne7mvPg47MOm2EHGovEwMpAJKin07LUdCFPD+TxWW8Hc1E3hiD8KiI4wI= 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)(82310400017)(376005)(36860700004)(1800799015);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 10 May 2024 21:17:43.9708 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: de4b3266-4a85-49af-ff50-08dc7136a204 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: BN1PEPF00004684.namprd03.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: IA0PR12MB7553 From: Tom Lendacky Add support for the SEV-SNP AP Creation NAE event. This allows SEV-SNP guests to alter the register state of the APs on their own. This allows the guest a way of simulating INIT-SIPI. A new event, KVM_REQ_UPDATE_PROTECTED_GUEST_STATE, is created and used so as to avoid updating the VMSA pointer while the vCPU is running. For CREATE The guest supplies the GPA of the VMSA to be used for the vCPU with the specified APIC ID. The GPA is saved in the svm struct of the target vCPU, the KVM_REQ_UPDATE_PROTECTED_GUEST_STATE event is added to the vCPU and then the vCPU is kicked. For CREATE_ON_INIT: The guest supplies the GPA of the VMSA to be used for the vCPU with the specified APIC ID the next time an INIT is performed. The GPA is saved in the svm struct of the target vCPU. For DESTROY: The guest indicates it wishes to stop the vCPU. The GPA is cleared from the svm struct, the KVM_REQ_UPDATE_PROTECTED_GUEST_STATE event is added to vCPU and then the vCPU is kicked. The KVM_REQ_UPDATE_PROTECTED_GUEST_STATE event handler will be invoked as a result of the event or as a result of an INIT. If a new VMSA is to be installed, the VMSA guest page is set as the VMSA in the vCPU VMCB and the vCPU state is set to KVM_MP_STATE_RUNNABLE. If a new VMSA is not to be installed, the VMSA is cleared in the vCPU VMCB and the vCPU state is set to KVM_MP_STATE_HALTED to prevent it from being run. Signed-off-by: Tom Lendacky Co-developed-by: Michael Roth Signed-off-by: Michael Roth Signed-off-by: Brijesh Singh Signed-off-by: Ashish Kalra Message-ID: <20240501085210.2213060-13-michael.roth@amd.com> Signed-off-by: Paolo Bonzini --- arch/x86/include/asm/kvm_host.h | 1 + arch/x86/include/asm/svm.h | 6 + arch/x86/kvm/svm/sev.c | 231 +++++++++++++++++++++++++++++++- arch/x86/kvm/svm/svm.c | 11 +- arch/x86/kvm/svm/svm.h | 9 ++ arch/x86/kvm/x86.c | 11 ++ 6 files changed, 266 insertions(+), 3 deletions(-) diff --git a/arch/x86/include/asm/kvm_host.h b/arch/x86/include/asm/kvm_host.h index 5830d42232da..72f4ec1ee062 100644 --- a/arch/x86/include/asm/kvm_host.h +++ b/arch/x86/include/asm/kvm_host.h @@ -121,6 +121,7 @@ KVM_ARCH_REQ_FLAGS(31, KVM_REQUEST_WAIT | KVM_REQUEST_NO_WAKEUP) #define KVM_REQ_HV_TLB_FLUSH \ KVM_ARCH_REQ_FLAGS(32, KVM_REQUEST_WAIT | KVM_REQUEST_NO_WAKEUP) +#define KVM_REQ_UPDATE_PROTECTED_GUEST_STATE KVM_ARCH_REQ(34) #define CR0_RESERVED_BITS \ (~(unsigned long)(X86_CR0_PE | X86_CR0_MP | X86_CR0_EM | X86_CR0_TS \ diff --git a/arch/x86/include/asm/svm.h b/arch/x86/include/asm/svm.h index 544a43c1cf11..f0dea3750ca9 100644 --- a/arch/x86/include/asm/svm.h +++ b/arch/x86/include/asm/svm.h @@ -286,8 +286,14 @@ static_assert((X2AVIC_MAX_PHYSICAL_ID & AVIC_PHYSICAL_MAX_INDEX_MASK) == X2AVIC_ #define AVIC_HPA_MASK ~((0xFFFULL << 52) | 0xFFF) #define SVM_SEV_FEAT_SNP_ACTIVE BIT(0) +#define SVM_SEV_FEAT_RESTRICTED_INJECTION BIT(3) +#define SVM_SEV_FEAT_ALTERNATE_INJECTION BIT(4) #define SVM_SEV_FEAT_DEBUG_SWAP BIT(5) +#define SVM_SEV_FEAT_INT_INJ_MODES \ + (SVM_SEV_FEAT_RESTRICTED_INJECTION | \ + SVM_SEV_FEAT_ALTERNATE_INJECTION) + struct vmcb_seg { u16 selector; u16 attrib; diff --git a/arch/x86/kvm/svm/sev.c b/arch/x86/kvm/svm/sev.c index 515bd6154a4b..518c44296f8d 100644 --- a/arch/x86/kvm/svm/sev.c +++ b/arch/x86/kvm/svm/sev.c @@ -38,7 +38,7 @@ #define GHCB_VERSION_DEFAULT 2ULL #define GHCB_VERSION_MIN 1ULL -#define GHCB_HV_FT_SUPPORTED GHCB_HV_FT_SNP +#define GHCB_HV_FT_SUPPORTED (GHCB_HV_FT_SNP | GHCB_HV_FT_SNP_AP_CREATION) /* enable/disable SEV support */ static bool sev_enabled = true; @@ -3267,6 +3267,13 @@ static int sev_es_validate_vmgexit(struct vcpu_svm *svm) if (!kvm_ghcb_sw_scratch_is_valid(svm)) goto vmgexit_err; break; + case SVM_VMGEXIT_AP_CREATION: + if (!sev_snp_guest(vcpu->kvm)) + goto vmgexit_err; + if (lower_32_bits(control->exit_info_1) != SVM_VMGEXIT_AP_DESTROY) + if (!kvm_ghcb_rax_is_valid(svm)) + goto vmgexit_err; + break; case SVM_VMGEXIT_NMI_COMPLETE: case SVM_VMGEXIT_AP_HLT_LOOP: case SVM_VMGEXIT_AP_JUMP_TABLE: @@ -3701,6 +3708,205 @@ static int snp_begin_psc(struct vcpu_svm *svm, struct psc_buffer *psc) unreachable(); } +static int __sev_snp_update_protected_guest_state(struct kvm_vcpu *vcpu) +{ + struct vcpu_svm *svm = to_svm(vcpu); + + WARN_ON(!mutex_is_locked(&svm->sev_es.snp_vmsa_mutex)); + + /* Mark the vCPU as offline and not runnable */ + vcpu->arch.pv.pv_unhalted = false; + vcpu->arch.mp_state = KVM_MP_STATE_HALTED; + + /* Clear use of the VMSA */ + svm->vmcb->control.vmsa_pa = INVALID_PAGE; + + if (VALID_PAGE(svm->sev_es.snp_vmsa_gpa)) { + gfn_t gfn = gpa_to_gfn(svm->sev_es.snp_vmsa_gpa); + struct kvm_memory_slot *slot; + kvm_pfn_t pfn; + + slot = gfn_to_memslot(vcpu->kvm, gfn); + if (!slot) + return -EINVAL; + + /* + * The new VMSA will be private memory guest memory, so + * retrieve the PFN from the gmem backend. + */ + if (kvm_gmem_get_pfn(vcpu->kvm, slot, gfn, &pfn, NULL)) + return -EINVAL; + + /* + * From this point forward, the VMSA will always be a + * guest-mapped page rather than the initial one allocated + * by KVM in svm->sev_es.vmsa. In theory, svm->sev_es.vmsa + * could be free'd and cleaned up here, but that involves + * cleanups like wbinvd_on_all_cpus() which would ideally + * be handled during teardown rather than guest boot. + * Deferring that also allows the existing logic for SEV-ES + * VMSAs to be re-used with minimal SNP-specific changes. + */ + svm->sev_es.snp_has_guest_vmsa = true; + + /* Use the new VMSA */ + svm->vmcb->control.vmsa_pa = pfn_to_hpa(pfn); + + /* Mark the vCPU as runnable */ + vcpu->arch.pv.pv_unhalted = false; + vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE; + + svm->sev_es.snp_vmsa_gpa = INVALID_PAGE; + + /* + * gmem pages aren't currently migratable, but if this ever + * changes then care should be taken to ensure + * svm->sev_es.vmsa is pinned through some other means. + */ + kvm_release_pfn_clean(pfn); + } + + /* + * When replacing the VMSA during SEV-SNP AP creation, + * mark the VMCB dirty so that full state is always reloaded. + */ + vmcb_mark_all_dirty(svm->vmcb); + + return 0; +} + +/* + * Invoked as part of svm_vcpu_reset() processing of an init event. + */ +void sev_snp_init_protected_guest_state(struct kvm_vcpu *vcpu) +{ + struct vcpu_svm *svm = to_svm(vcpu); + int ret; + + if (!sev_snp_guest(vcpu->kvm)) + return; + + mutex_lock(&svm->sev_es.snp_vmsa_mutex); + + if (!svm->sev_es.snp_ap_waiting_for_reset) + goto unlock; + + svm->sev_es.snp_ap_waiting_for_reset = false; + + ret = __sev_snp_update_protected_guest_state(vcpu); + if (ret) + vcpu_unimpl(vcpu, "snp: AP state update on init failed\n"); + +unlock: + mutex_unlock(&svm->sev_es.snp_vmsa_mutex); +} + +static int sev_snp_ap_creation(struct vcpu_svm *svm) +{ + struct kvm_sev_info *sev = &to_kvm_svm(svm->vcpu.kvm)->sev_info; + struct kvm_vcpu *vcpu = &svm->vcpu; + struct kvm_vcpu *target_vcpu; + struct vcpu_svm *target_svm; + unsigned int request; + unsigned int apic_id; + bool kick; + int ret; + + request = lower_32_bits(svm->vmcb->control.exit_info_1); + apic_id = upper_32_bits(svm->vmcb->control.exit_info_1); + + /* Validate the APIC ID */ + target_vcpu = kvm_get_vcpu_by_id(vcpu->kvm, apic_id); + if (!target_vcpu) { + vcpu_unimpl(vcpu, "vmgexit: invalid AP APIC ID [%#x] from guest\n", + apic_id); + return -EINVAL; + } + + ret = 0; + + target_svm = to_svm(target_vcpu); + + /* + * The target vCPU is valid, so the vCPU will be kicked unless the + * request is for CREATE_ON_INIT. For any errors at this stage, the + * kick will place the vCPU in an non-runnable state. + */ + kick = true; + + mutex_lock(&target_svm->sev_es.snp_vmsa_mutex); + + target_svm->sev_es.snp_vmsa_gpa = INVALID_PAGE; + target_svm->sev_es.snp_ap_waiting_for_reset = true; + + /* Interrupt injection mode shouldn't change for AP creation */ + if (request < SVM_VMGEXIT_AP_DESTROY) { + u64 sev_features; + + sev_features = vcpu->arch.regs[VCPU_REGS_RAX]; + sev_features ^= sev->vmsa_features; + + if (sev_features & SVM_SEV_FEAT_INT_INJ_MODES) { + vcpu_unimpl(vcpu, "vmgexit: invalid AP injection mode [%#lx] from guest\n", + vcpu->arch.regs[VCPU_REGS_RAX]); + ret = -EINVAL; + goto out; + } + } + + switch (request) { + case SVM_VMGEXIT_AP_CREATE_ON_INIT: + kick = false; + fallthrough; + case SVM_VMGEXIT_AP_CREATE: + if (!page_address_valid(vcpu, svm->vmcb->control.exit_info_2)) { + vcpu_unimpl(vcpu, "vmgexit: invalid AP VMSA address [%#llx] from guest\n", + svm->vmcb->control.exit_info_2); + ret = -EINVAL; + goto out; + } + + /* + * Malicious guest can RMPADJUST a large page into VMSA which + * will hit the SNP erratum where the CPU will incorrectly signal + * an RMP violation #PF if a hugepage collides with the RMP entry + * of VMSA page, reject the AP CREATE request if VMSA address from + * guest is 2M aligned. + */ + if (IS_ALIGNED(svm->vmcb->control.exit_info_2, PMD_SIZE)) { + vcpu_unimpl(vcpu, + "vmgexit: AP VMSA address [%llx] from guest is unsafe as it is 2M aligned\n", + svm->vmcb->control.exit_info_2); + ret = -EINVAL; + goto out; + } + + target_svm->sev_es.snp_vmsa_gpa = svm->vmcb->control.exit_info_2; + break; + case SVM_VMGEXIT_AP_DESTROY: + break; + default: + vcpu_unimpl(vcpu, "vmgexit: invalid AP creation request [%#x] from guest\n", + request); + ret = -EINVAL; + break; + } + +out: + if (kick) { + kvm_make_request(KVM_REQ_UPDATE_PROTECTED_GUEST_STATE, target_vcpu); + + if (target_vcpu->arch.mp_state == KVM_MP_STATE_UNINITIALIZED) + kvm_make_request(KVM_REQ_UNBLOCK, target_vcpu); + + kvm_vcpu_kick(target_vcpu); + } + + mutex_unlock(&target_svm->sev_es.snp_vmsa_mutex); + + return ret; +} + static int sev_handle_vmgexit_msr_protocol(struct vcpu_svm *svm) { struct vmcb_control_area *control = &svm->vmcb->control; @@ -3966,6 +4172,15 @@ int sev_handle_vmgexit(struct kvm_vcpu *vcpu) ret = snp_begin_psc(svm, svm->sev_es.ghcb_sa); break; + case SVM_VMGEXIT_AP_CREATION: + ret = sev_snp_ap_creation(svm); + if (ret) { + ghcb_set_sw_exit_info_1(svm->sev_es.ghcb, 2); + ghcb_set_sw_exit_info_2(svm->sev_es.ghcb, GHCB_ERR_INVALID_INPUT); + } + + ret = 1; + break; case SVM_VMGEXIT_UNSUPPORTED_EVENT: vcpu_unimpl(vcpu, "vmgexit: unsupported event - exit_info_1=%#llx, exit_info_2=%#llx\n", @@ -4060,7 +4275,7 @@ static void sev_es_init_vmcb(struct vcpu_svm *svm) * the VMSA will be NULL if this vCPU is the destination for intrahost * migration, and will be copied later. */ - if (svm->sev_es.vmsa) + if (svm->sev_es.vmsa && !svm->sev_es.snp_has_guest_vmsa) svm->vmcb->control.vmsa_pa = __pa(svm->sev_es.vmsa); /* Can't intercept CR register access, HV can't modify CR registers */ @@ -4136,6 +4351,8 @@ void sev_es_vcpu_reset(struct vcpu_svm *svm) set_ghcb_msr(svm, GHCB_MSR_SEV_INFO((__u64)sev->ghcb_version, GHCB_VERSION_MIN, sev_enc_bit)); + + mutex_init(&svm->sev_es.snp_vmsa_mutex); } void sev_es_prepare_switch_to_guest(struct vcpu_svm *svm, struct sev_es_save_area *hostsa) @@ -4247,6 +4464,16 @@ struct page *snp_safe_alloc_page(struct kvm_vcpu *vcpu) return p; } +void sev_vcpu_unblocking(struct kvm_vcpu *vcpu) +{ + if (!sev_snp_guest(vcpu->kvm)) + return; + + if (kvm_test_request(KVM_REQ_UPDATE_PROTECTED_GUEST_STATE, vcpu) && + vcpu->arch.mp_state == KVM_MP_STATE_UNINITIALIZED) + vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE; +} + void sev_handle_rmp_fault(struct kvm_vcpu *vcpu, gpa_t gpa, u64 error_code) { struct kvm_memory_slot *slot; diff --git a/arch/x86/kvm/svm/svm.c b/arch/x86/kvm/svm/svm.c index bdaf39571817..546656606b44 100644 --- a/arch/x86/kvm/svm/svm.c +++ b/arch/x86/kvm/svm/svm.c @@ -1398,6 +1398,9 @@ static void svm_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event) svm->spec_ctrl = 0; svm->virt_spec_ctrl = 0; + if (init_event) + sev_snp_init_protected_guest_state(vcpu); + init_vmcb(vcpu); if (!init_event) @@ -4940,6 +4943,12 @@ static void *svm_alloc_apic_backing_page(struct kvm_vcpu *vcpu) return page_address(page); } +static void svm_vcpu_unblocking(struct kvm_vcpu *vcpu) +{ + sev_vcpu_unblocking(vcpu); + avic_vcpu_unblocking(vcpu); +} + static struct kvm_x86_ops svm_x86_ops __initdata = { .name = KBUILD_MODNAME, @@ -4962,7 +4971,7 @@ static struct kvm_x86_ops svm_x86_ops __initdata = { .vcpu_load = svm_vcpu_load, .vcpu_put = svm_vcpu_put, .vcpu_blocking = avic_vcpu_blocking, - .vcpu_unblocking = avic_vcpu_unblocking, + .vcpu_unblocking = svm_vcpu_unblocking, .update_exception_bitmap = svm_update_exception_bitmap, .get_msr_feature = svm_get_msr_feature, diff --git a/arch/x86/kvm/svm/svm.h b/arch/x86/kvm/svm/svm.h index 36b573427b85..926bfce571a6 100644 --- a/arch/x86/kvm/svm/svm.h +++ b/arch/x86/kvm/svm/svm.h @@ -216,6 +216,11 @@ struct vcpu_sev_es_state { bool psc_2m; u64 ghcb_registered_gpa; + + struct mutex snp_vmsa_mutex; /* Used to handle concurrent updates of VMSA. */ + gpa_t snp_vmsa_gpa; + bool snp_ap_waiting_for_reset; + bool snp_has_guest_vmsa; }; struct vcpu_svm { @@ -729,6 +734,8 @@ int sev_cpu_init(struct svm_cpu_data *sd); int sev_dev_get_attr(u32 group, u64 attr, u64 *val); extern unsigned int max_sev_asid; void sev_handle_rmp_fault(struct kvm_vcpu *vcpu, gpa_t gpa, u64 error_code); +void sev_vcpu_unblocking(struct kvm_vcpu *vcpu); +void sev_snp_init_protected_guest_state(struct kvm_vcpu *vcpu); #else static inline struct page *snp_safe_alloc_page(struct kvm_vcpu *vcpu) { return alloc_page(GFP_KERNEL_ACCOUNT | __GFP_ZERO); @@ -743,6 +750,8 @@ static inline int sev_cpu_init(struct svm_cpu_data *sd) { return 0; } static inline int sev_dev_get_attr(u32 group, u64 attr, u64 *val) { return -ENXIO; } #define max_sev_asid 0 static inline void sev_handle_rmp_fault(struct kvm_vcpu *vcpu, gpa_t gpa, u64 error_code) {} +static inline void sev_vcpu_unblocking(struct kvm_vcpu *vcpu) {} +static inline void sev_snp_init_protected_guest_state(struct kvm_vcpu *vcpu) {} #endif diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index 60ebe3ee9118..62a0474e1346 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -10945,6 +10945,14 @@ static int vcpu_enter_guest(struct kvm_vcpu *vcpu) if (kvm_check_request(KVM_REQ_UPDATE_CPU_DIRTY_LOGGING, vcpu)) static_call(kvm_x86_update_cpu_dirty_logging)(vcpu); + + if (kvm_check_request(KVM_REQ_UPDATE_PROTECTED_GUEST_STATE, vcpu)) { + kvm_vcpu_reset(vcpu, true); + if (vcpu->arch.mp_state != KVM_MP_STATE_RUNNABLE) { + r = 1; + goto out; + } + } } if (kvm_check_request(KVM_REQ_EVENT, vcpu) || req_int_win || @@ -13152,6 +13160,9 @@ static inline bool kvm_vcpu_has_events(struct kvm_vcpu *vcpu) if (kvm_test_request(KVM_REQ_PMI, vcpu)) return true; + if (kvm_test_request(KVM_REQ_UPDATE_PROTECTED_GUEST_STATE, vcpu)) + return true; + if (kvm_arch_interrupt_allowed(vcpu) && (kvm_cpu_has_interrupt(vcpu) || kvm_guest_apic_has_interrupt(vcpu))) From patchwork Fri May 10 21:10:17 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Michael Roth X-Patchwork-Id: 13661952 Received: from NAM04-BN8-obe.outbound.protection.outlook.com (mail-bn8nam04on2040.outbound.protection.outlook.com [40.107.100.40]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id F1CEE13BAED; Fri, 10 May 2024 21:18:08 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=40.107.100.40 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1715375893; cv=fail; b=rkNSdUsxZ9OcEDbMf55TfHdOlByU/6DY+cpKFFC44A8peuiQa9QqfOKc2NmCvJM9LFNRsfpovoM4sDi8YVTkK84v9QVovsVvut9P+hE4361uYGesmOhEXyuy7Yrh56IVPmbm2KGoX/AEp7J9qpoXFHBK6IOq+G6R2LJAOEALZl0= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1715375893; c=relaxed/simple; bh=2P4JTNbSFbBas9NBCzHcY/9llXZydk8porZdEY5wMxk=; h=From:To:CC:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=s+1kntxJJptNNHeYBV+Hhk0298bIe9nkxpONk8Rdx9bPgtBJUoZlMaiN4R8u28G/gfH6jvXs9PlJBhX4rVUGSv/GISFW4uUCmzEI3tAhJVFhZRN2LCfc2Z3kN/JUynZ5XO0WWKMsRMEWVKbN+MwYy9FktbYDhANCc7xLfQXzROU= ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=amd.com; spf=fail smtp.mailfrom=amd.com; dkim=pass (1024-bit key) header.d=amd.com header.i=@amd.com header.b=Zuj6y9k6; arc=fail smtp.client-ip=40.107.100.40 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=amd.com Authentication-Results: smtp.subspace.kernel.org; spf=fail smtp.mailfrom=amd.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=amd.com header.i=@amd.com header.b="Zuj6y9k6" ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=mnyl7BpamCJO+HblCggB8SNQpqmCWO7RTBrWW4EdYm4CpTjXqhW7uyRRMLBxdR+S8emHRKk4WF3J2LDbLGU6Dr9LHCVny/9+5roXwEYhBFJYHlLmvN1kWLkG4a3xLN4pcUK4NGoC0bUhzawLtJt0mR7qrAYFKpYSnScCshyUJ+ps5M+ZwQfiA81SRkWgd9FgpXv0tOgE1XPOr29y/HBFw/VX/gN5svPkXxnMTtftuERmw/1QHsTJawyFOrKut9w9qm2aCyBQtzG/+897RVzBvp8qhXVM/KGutG+dIbTPpp5NR2Y5bzfcIrMYSW61Mix7LCrEjh5dLOK1Cyf5/2x+aw== 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=vTsofH+TIDR97Ug9BLX4LJlsjJ32ksg/WW28J3TgUbM=; b=SL0SkXFrrPAjqaf8VaTraVZu4vEw0m8PkgKMFkZ0T3VB6USsSi+GMLtApsxDB2eMfV/3BaNHTUBVgce29sgYA99aSExFRqtS0GSxtq775BMPSc+GxOAvnEC9Ejixdia2D2ioiD4N3ysnMwE+CQxx84SoZH15de7N/4azhLCy9wPKCZ1V8aMfSRyGeL3PYn471hKVAsTeX+NB5Ibe5TwY+cB7d0baIVwa0R6A1ioOvEGmN0xm+sykmyq9vRs7krs8IDuVfNzaGMqdNR/5XNLjQJ+tl+ZXpJ4qlpDtARxfn3I7hYA6vS2pEsb1JCOT+Zf7pxgD2iVRleQABvReXmFG9w== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 165.204.84.17) smtp.rcpttodomain=redhat.com 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=vTsofH+TIDR97Ug9BLX4LJlsjJ32ksg/WW28J3TgUbM=; b=Zuj6y9k61O9sNuhwkNccGGmLMDPGUGWDFaDe3vTH9WwcUJJum9GmYtLspjSXpnOz6WOBsYHdmnc5MBRec6LI6QBI0jbS+JLZKAXUu+FFtRSUbuztISm9IqXaSEfolhrPtgC8lsiHopPRYeaKXFifaSP49ENVMQveYOXbHGZ7aMk= Received: from BN9PR03CA0909.namprd03.prod.outlook.com (2603:10b6:408:107::14) by IA1PR12MB8080.namprd12.prod.outlook.com (2603:10b6:208:3fd::6) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7544.47; Fri, 10 May 2024 21:18:04 +0000 Received: from BN1PEPF00004681.namprd03.prod.outlook.com (2603:10b6:408:107:cafe::e0) by BN9PR03CA0909.outlook.office365.com (2603:10b6:408:107::14) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7544.48 via Frontend Transport; Fri, 10 May 2024 21:18:04 +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 BN1PEPF00004681.mail.protection.outlook.com (10.167.243.87) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.7544.18 via Frontend Transport; Fri, 10 May 2024 21:18:04 +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; Fri, 10 May 2024 16:18:04 -0500 From: Michael Roth To: Paolo Bonzini CC: , , Sean Christopherson Subject: [PULL 12/19] KVM: SEV: Implement gmem hook for initializing private pages Date: Fri, 10 May 2024 16:10:17 -0500 Message-ID: <20240510211024.556136-13-michael.roth@amd.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20240510211024.556136-1-michael.roth@amd.com> References: <20240510211024.556136-1-michael.roth@amd.com> Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 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: BN1PEPF00004681:EE_|IA1PR12MB8080:EE_ X-MS-Office365-Filtering-Correlation-Id: c1defcc7-c915-403c-ea17-08dc7136ae4d X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0;ARA:13230031|36860700004|376005|1800799015|82310400017; X-Microsoft-Antispam-Message-Info: ZRn1BfNpzpweSNos5kRIZSevFjpnD5/bMDW8jo9JVOKZ+baqaHlnhgILMb6rkyZEEy2REqdYQFoU6b4zV+e3omfDkjWhJlPIcok0ZCNF6/nl71KpBAZgl3lJLgRoSInJbWGdS+BqTNRZ2qG4Gc5Loa5UT+il/tCYWDNIoQZQbbWCMFvBqmgYwh+rE4sQOwTNcS67EiUdDtSqstL1HYwQwx1iupS7uWoLll/pvb9GRUttKJ2Y1YQgs9+KQLk6CbcL+LH3SgMNuB7y8xqKUf4efSxlK1A9oOQKu9Qr8c3T0J1LQc/CoOH9FrH0GyO+qonR9tRjy5cgpIpB5NJ9eU9fgTCPGCFq4G6n/UtHdQ1y0jnontDe1NDtmVUnTx1sJ0uW1HH49+fOCANFKeTxNS3proacAJmvW5NTIKQ5HS72uBrcWnpxecupiMmTIuD0g1n+JlLRnbnlFPbo6IHFRNCvA8ky6YQ4M74fjukESN6ZYKZzxl3nOlA0yRYupElXqPZqyjE8B2ISdY4I4eSO02gLATPE9x04L7bAXIky8JyNdHzatzHXDrlW8hwc+jSWeojF8HjhSnDyxHPy8jFRdjache7QkKov82fEnGbxkg40D5o4fFGtDdl760NSdEmSfYXTmefJu5jVl8wbd6crRZRwfYRD8OaEUlUSaGLA7F+kUX/H5JqbhcHeUzGrH4E/73u2rFoX0zL8WmCN/jB8Wl/WksjM05LednckjrEIPeqxo0pdR+ydUDwfweDWgeKzkCGCA7A/p3rNZ2gD/qoE/FJEFqmvXQEATILR9AZb/DFte7Bh8FYsoOocmRDnKTOppseVGE3XYzg0dI1iH41Z1c246QK1szJcVYKiqGHWjQY3RQ8gi4x+2Xa/XR0qiLwRl+lhft/Rvd6/uavtzJ0Rd2JB6qfpPzKUpVZHjETWoIrTuw9BjWUOeEyhW45WJA1ViJlrLooLXVzpTWPC59bEpc+bHpi8auJPOf4fwSM2XLMHOL9r6H9TfeVLES7n6YPWNC24r741nK2hn1u+t9Dwoq6IcOGQC/YlKJl2JadmarBZDEtuqsgdkkWKmhMhyfwzcWPOs9c+tZ/mtJfZvkTjF5vxPNbDomUvEv7QJu61PhxJeq1VpM1YlE116LSclIlNiAGLrn3ep6YhU3fKXqfswnf336JR+Bxic+S1ug6XoSdZLwDcRmz1qOqdkjmH4BHBYLQez6TWUv7Tgn9DbbBn7o6dDUb8xqiWQ1SdK1hxR0gLDvFCGbO1X9PxtRyMeP89N0pqYsNMW1fXjzelCF0t3AcwPtLf5vlbTMs3dB9u/u1fm7mhIFs/4loS9QFSog0EJugWkuuXRTmzDpXoprKNFDUtCiHn7cjgoxYqqxxFTVeg1WP756RQixXW9oh475C6REbz 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)(376005)(1800799015)(82310400017);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 10 May 2024 21:18:04.5969 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: c1defcc7-c915-403c-ea17-08dc7136ae4d 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: BN1PEPF00004681.namprd03.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: IA1PR12MB8080 This will handle the RMP table updates needed to put a page into a private state before mapping it into an SEV-SNP guest. Reviewed-by: Paolo Bonzini Signed-off-by: Michael Roth Message-ID: <20240501085210.2213060-14-michael.roth@amd.com> Signed-off-by: Paolo Bonzini --- arch/x86/kvm/Kconfig | 1 + arch/x86/kvm/svm/sev.c | 98 ++++++++++++++++++++++++++++++++++++++++++ arch/x86/kvm/svm/svm.c | 2 + arch/x86/kvm/svm/svm.h | 5 +++ arch/x86/kvm/x86.c | 5 +++ virt/kvm/guest_memfd.c | 4 +- 6 files changed, 113 insertions(+), 2 deletions(-) diff --git a/arch/x86/kvm/Kconfig b/arch/x86/kvm/Kconfig index 5e72faca4e8f..10768f13b240 100644 --- a/arch/x86/kvm/Kconfig +++ b/arch/x86/kvm/Kconfig @@ -137,6 +137,7 @@ config KVM_AMD_SEV depends on CRYPTO_DEV_SP_PSP && !(KVM_AMD=y && CRYPTO_DEV_CCP_DD=m) select ARCH_HAS_CC_PLATFORM select KVM_GENERIC_PRIVATE_MEM + select HAVE_KVM_GMEM_PREPARE help Provides support for launching Encrypted VMs (SEV) and Encrypted VMs with Encrypted State (SEV-ES) on AMD processors. diff --git a/arch/x86/kvm/svm/sev.c b/arch/x86/kvm/svm/sev.c index 518c44296f8d..2bc4aa91cd31 100644 --- a/arch/x86/kvm/svm/sev.c +++ b/arch/x86/kvm/svm/sev.c @@ -4565,3 +4565,101 @@ void sev_handle_rmp_fault(struct kvm_vcpu *vcpu, gpa_t gpa, u64 error_code) out_no_trace: put_page(pfn_to_page(pfn)); } + +static bool is_pfn_range_shared(kvm_pfn_t start, kvm_pfn_t end) +{ + kvm_pfn_t pfn = start; + + while (pfn < end) { + int ret, rmp_level; + bool assigned; + + ret = snp_lookup_rmpentry(pfn, &assigned, &rmp_level); + if (ret) { + pr_warn_ratelimited("SEV: Failed to retrieve RMP entry: PFN 0x%llx GFN start 0x%llx GFN end 0x%llx RMP level %d error %d\n", + pfn, start, end, rmp_level, ret); + return false; + } + + if (assigned) { + pr_debug("%s: overlap detected, PFN 0x%llx start 0x%llx end 0x%llx RMP level %d\n", + __func__, pfn, start, end, rmp_level); + return false; + } + + pfn++; + } + + return true; +} + +static u8 max_level_for_order(int order) +{ + if (order >= KVM_HPAGE_GFN_SHIFT(PG_LEVEL_2M)) + return PG_LEVEL_2M; + + return PG_LEVEL_4K; +} + +static bool is_large_rmp_possible(struct kvm *kvm, kvm_pfn_t pfn, int order) +{ + kvm_pfn_t pfn_aligned = ALIGN_DOWN(pfn, PTRS_PER_PMD); + + /* + * If this is a large folio, and the entire 2M range containing the + * PFN is currently shared, then the entire 2M-aligned range can be + * set to private via a single 2M RMP entry. + */ + if (max_level_for_order(order) > PG_LEVEL_4K && + is_pfn_range_shared(pfn_aligned, pfn_aligned + PTRS_PER_PMD)) + return true; + + return false; +} + +int sev_gmem_prepare(struct kvm *kvm, kvm_pfn_t pfn, gfn_t gfn, int max_order) +{ + struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info; + kvm_pfn_t pfn_aligned; + gfn_t gfn_aligned; + int level, rc; + bool assigned; + + if (!sev_snp_guest(kvm)) + return 0; + + rc = snp_lookup_rmpentry(pfn, &assigned, &level); + if (rc) { + pr_err_ratelimited("SEV: Failed to look up RMP entry: GFN %llx PFN %llx error %d\n", + gfn, pfn, rc); + return -ENOENT; + } + + if (assigned) { + pr_debug("%s: already assigned: gfn %llx pfn %llx max_order %d level %d\n", + __func__, gfn, pfn, max_order, level); + return 0; + } + + if (is_large_rmp_possible(kvm, pfn, max_order)) { + level = PG_LEVEL_2M; + pfn_aligned = ALIGN_DOWN(pfn, PTRS_PER_PMD); + gfn_aligned = ALIGN_DOWN(gfn, PTRS_PER_PMD); + } else { + level = PG_LEVEL_4K; + pfn_aligned = pfn; + gfn_aligned = gfn; + } + + rc = rmp_make_private(pfn_aligned, gfn_to_gpa(gfn_aligned), level, sev->asid, false); + if (rc) { + pr_err_ratelimited("SEV: Failed to update RMP entry: GFN %llx PFN %llx level %d error %d\n", + gfn, pfn, level, rc); + return -EINVAL; + } + + pr_debug("%s: updated: gfn %llx pfn %llx pfn_aligned %llx max_order %d level %d\n", + __func__, gfn, pfn, pfn_aligned, max_order, level); + + return 0; +} diff --git a/arch/x86/kvm/svm/svm.c b/arch/x86/kvm/svm/svm.c index 546656606b44..b9ecc06f8934 100644 --- a/arch/x86/kvm/svm/svm.c +++ b/arch/x86/kvm/svm/svm.c @@ -5081,6 +5081,8 @@ static struct kvm_x86_ops svm_x86_ops __initdata = { .vcpu_deliver_sipi_vector = svm_vcpu_deliver_sipi_vector, .vcpu_get_apicv_inhibit_reasons = avic_vcpu_get_apicv_inhibit_reasons, .alloc_apic_backing_page = svm_alloc_apic_backing_page, + + .gmem_prepare = sev_gmem_prepare, }; /* diff --git a/arch/x86/kvm/svm/svm.h b/arch/x86/kvm/svm/svm.h index 926bfce571a6..4203bd9012e9 100644 --- a/arch/x86/kvm/svm/svm.h +++ b/arch/x86/kvm/svm/svm.h @@ -736,6 +736,7 @@ extern unsigned int max_sev_asid; void sev_handle_rmp_fault(struct kvm_vcpu *vcpu, gpa_t gpa, u64 error_code); void sev_vcpu_unblocking(struct kvm_vcpu *vcpu); void sev_snp_init_protected_guest_state(struct kvm_vcpu *vcpu); +int sev_gmem_prepare(struct kvm *kvm, kvm_pfn_t pfn, gfn_t gfn, int max_order); #else static inline struct page *snp_safe_alloc_page(struct kvm_vcpu *vcpu) { return alloc_page(GFP_KERNEL_ACCOUNT | __GFP_ZERO); @@ -752,6 +753,10 @@ static inline int sev_dev_get_attr(u32 group, u64 attr, u64 *val) { return -ENXI static inline void sev_handle_rmp_fault(struct kvm_vcpu *vcpu, gpa_t gpa, u64 error_code) {} static inline void sev_vcpu_unblocking(struct kvm_vcpu *vcpu) {} static inline void sev_snp_init_protected_guest_state(struct kvm_vcpu *vcpu) {} +static inline int sev_gmem_prepare(struct kvm *kvm, kvm_pfn_t pfn, gfn_t gfn, int max_order) +{ + return 0; +} #endif diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index 62a0474e1346..f82a137640d8 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -13617,6 +13617,11 @@ bool kvm_arch_no_poll(struct kvm_vcpu *vcpu) EXPORT_SYMBOL_GPL(kvm_arch_no_poll); #ifdef CONFIG_HAVE_KVM_GMEM_PREPARE +bool kvm_arch_gmem_prepare_needed(struct kvm *kvm) +{ + return kvm->arch.vm_type == KVM_X86_SNP_VM; +} + int kvm_arch_gmem_prepare(struct kvm *kvm, gfn_t gfn, kvm_pfn_t pfn, int max_order) { return static_call(kvm_x86_gmem_prepare)(kvm, pfn, gfn, max_order); diff --git a/virt/kvm/guest_memfd.c b/virt/kvm/guest_memfd.c index dfe50c64a552..9714add38852 100644 --- a/virt/kvm/guest_memfd.c +++ b/virt/kvm/guest_memfd.c @@ -39,8 +39,8 @@ static int kvm_gmem_prepare_folio(struct inode *inode, pgoff_t index, struct fol gfn = slot->base_gfn + index - slot->gmem.pgoff; rc = kvm_arch_gmem_prepare(kvm, gfn, pfn, compound_order(compound_head(page))); if (rc) { - pr_warn_ratelimited("gmem: Failed to prepare folio for index %lx, error %d.\n", - index, rc); + pr_warn_ratelimited("gmem: Failed to prepare folio for index %lx GFN %llx PFN %llx error %d.\n", + index, gfn, pfn, rc); return rc; } } From patchwork Fri May 10 21:10:18 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Michael Roth X-Patchwork-Id: 13661953 Received: from NAM12-MW2-obe.outbound.protection.outlook.com (mail-mw2nam12on2074.outbound.protection.outlook.com [40.107.244.74]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 7756613B789; Fri, 10 May 2024 21:18:28 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=40.107.244.74 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1715375915; cv=fail; b=s72yc4ub/FZLG0tEtXTrq/Skgi0A9YG8cxEEj8uE3CXx7+GvyLRtCCk/Z/CecdrTL4hdp4WbW0jYA0713JvBDAdCYC2oUZENRGUsRFEyqjCZ0gW02HI0Sa/92pZKo8awFhmJ4SJyYiu1YJZOogfKOhhkdTLtBBpq1gDH0vkCLb8= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1715375915; c=relaxed/simple; bh=1slvbwcBV1cu6GGG6vbFi13UFN+j12sU3GEU4fyumJQ=; h=From:To:CC:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=X7f1tHx1Ovf0uDC3s/kmjS/ma3AQlpny7tiS9YWHlh+F7IWOaZED/0qUm4f+8bRG4lQbKQTcQUwmouvZtoU2HR8Bg4Q8M5qKb1ONLfNWcF2tBIhYOSU2IrG29+PjlOPqoaJACsVT6VZFEvImR/P+96871GeX5wyzexdM2VXJDUg= ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=amd.com; spf=fail smtp.mailfrom=amd.com; dkim=pass (1024-bit key) header.d=amd.com header.i=@amd.com header.b=NPlrfPBF; arc=fail smtp.client-ip=40.107.244.74 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=amd.com Authentication-Results: smtp.subspace.kernel.org; spf=fail smtp.mailfrom=amd.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=amd.com header.i=@amd.com header.b="NPlrfPBF" ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=WxAqNcc1+Pwe4mmQatTsNj9LXUX1NaXHK+Y4t835ss2Yz/c53csC3wY8QjhyE0jTKMP94kBfRzrCHoSiLZ1iL7tcCzL0fFH8LYu18qMXikdnfpJRKJuJ0BBX155uTBXaRzl8Hi4hrHpPl6uXFHp6+/8n5tRaIlyGddenuLXvLViO2+hdGgRGTZPB420P1l60cfBzESC0Ygb1rzhrIdnDweVvpsW1toGhcEK7C0paO2/+OR2TWXDqnOPYZpPC1U8DPZYND2ma1e8SivwSVoPIwHcVwn0+jEA12rCpRyAwBimpzuMboFPVPLtgfi7l5waaSABapmbs0ZD6gH+z7taJkg== 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=U3RtZPSSBOzA9Vm6vLE98EaAy5syvEiaKlqSL7XgJ2Y=; b=EfXd+8nP6kICxfe2mZxXr2Eq6F8QEk62VwmQQwrJmsWaydSXc1/OO6sPO5dFGqxQbzw6HPDdJM5dKA7dKUDlFwWE0lM3CSaoO6KHOVxQI+7oWUdJ+hUyb0uKbmJzRs4HuffL5xDC+tpC0ARQjUG7+8C71XGTiyqisggOiWzDRwlhsrqEWWVbC3ig3ypgZ+AgmusJGksrrOJJ9FsQ/QiZCv7OyAD3in3vhjxNnqhY0WuvQB/lJlv5MWsmDztEou5tQJPpQ4gU/EfKXvkBkAg5058BjbpVgKRuTrrt8q8b+P5tw6rhz8F33oZMajQjPj3Ezq2v6cgqMoBNUDtrAEezZQ== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 165.204.84.17) smtp.rcpttodomain=redhat.com 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=U3RtZPSSBOzA9Vm6vLE98EaAy5syvEiaKlqSL7XgJ2Y=; b=NPlrfPBF2gINsJKjh1qJ0uHuBd1MP+Shay3okar3lxCKE+PBYIJSFJSo+/rRpoiRAlH/tCiQ7rvObMd3iKPGsCeqDtFqPS1d+igTpUqEPPFejvyxpbZug7Idhtim0xxlOJQ6Ijbixj2RC3XMKwHilfeTpP4U4V5a6mZzeqBCCuE= Received: from BN9P222CA0016.NAMP222.PROD.OUTLOOK.COM (2603:10b6:408:10c::21) by MW4PR12MB5603.namprd12.prod.outlook.com (2603:10b6:303:16a::17) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7544.48; Fri, 10 May 2024 21:18:26 +0000 Received: from BN1PEPF00004683.namprd03.prod.outlook.com (2603:10b6:408:10c:cafe::f7) by BN9P222CA0016.outlook.office365.com (2603:10b6:408:10c::21) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7544.51 via Frontend Transport; Fri, 10 May 2024 21:18:25 +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 BN1PEPF00004683.mail.protection.outlook.com (10.167.243.89) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.7544.18 via Frontend Transport; Fri, 10 May 2024 21:18:25 +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; Fri, 10 May 2024 16:18:24 -0500 From: Michael Roth To: Paolo Bonzini CC: , , Sean Christopherson Subject: [PULL 13/19] KVM: SEV: Implement gmem hook for invalidating private pages Date: Fri, 10 May 2024 16:10:18 -0500 Message-ID: <20240510211024.556136-14-michael.roth@amd.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20240510211024.556136-1-michael.roth@amd.com> References: <20240510211024.556136-1-michael.roth@amd.com> Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 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: BN1PEPF00004683:EE_|MW4PR12MB5603:EE_ X-MS-Office365-Filtering-Correlation-Id: 6d2a2326-e8d0-4866-2269-08dc7136baad X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0;ARA:13230031|1800799015|376005|36860700004|82310400017; X-Microsoft-Antispam-Message-Info: 9fEwvTNGb4jyf0xAQAd2kUZP/nBvJYm8dmJL4Z17GsLf9+DISbmrf1dxXqlNcWXYI+9iuNZj+fqKv8Ov+XiWK+tcc7XubJLd6p9f4fyFUswJYl5PvdJKOaj4fC0MTfF4kyI1AXLBScOdWyEpdXU69yhpd6J8vtWhNBToRW8ZgIOzujmu8z3JLO6sTWXqqXuRYgdMLeerT2HP2zEpwiW/LCwpRgKbkjZ7pvPMkANj7bp8aFMOzedboxJ5D83PUvMxIF/7/TnneCQtpSiS0pL/mYJLCH+fviSO+dHwc9lZBVVGRoXjuMjSa8lQ/y2sg8nMKjHF4vEBEbdt17Zb6yIkTFFbLsHub1biKDqfr4JL2Oq4iII8ORZDwz2oSxS6tv/4aRYYYQb+u5pHZhigj0bNKx8DKMKuwzHiN8uPEySEdmH6jd3ngbQugDtXHiU4Sq9UX+BomsdEqHnR0JUOKu+05OZXKIi36anu9kLMk3uXNo9IdfPXk7Hdjrfln59fJ1LjXi14ala7R2SbgQ+aJV0Xm+pjBGz9hupqYG/aY0O1sOyebGMVJC4e7yfF36NI1lFq+wOwF610FGJ8djNhZY2ogM3b6hEX7RKDDt5mG6P7fkRJWJZNNbIUqQ+RHaKsKt3Pa7Ut/Xceq7cjsrfMdxBc0KO5lK2tX682qYCkN8Wc9zmDr0b4cQLvOXjjYDfVJU70qq294jvOEH/zIjAyzWgg1ZLVlTzLNyI0hW5KaoYVyeZNXupGI7voczj91IrlzVE8C0IVNiXilbB/LOj7Gh4/V3Kc+BDxR/ZEUag0zqa3lFXY5K9Kd03WghMtkEqFs9BhuNswgNSB2RJab/s80bH1a2jnhfuJ2QVxecj1AdxK9o0tEGawy8CK8wtGNQJeHICYi2BZ5zIg5eoyGWO2GNTviOY+hV2ttBi7UIS7dwaXp04F7lZpoy1/w4yf7TmdtXvP0GVLxHYR94VOPKiXD4mivjlLOTfvahZCXMaf0rF3ss2l6allVgz5pN424EErNWT8m/kepbjLaCcGNfTHZ8lFBH5cEKbsx4g/N7xpL/t5LQs8f1Wn63SgtB1+9iYuIa5M5kCbm24fZtZo2pTHuIk43HtzU/iv/YtxohUY6y/A3hmD9vvh3QCmgzgAsivovsvivEuwcM+ItDBzb1Ttv7CsUgz0w/ECk6uZuUnrdRtKNDgl09cZ6bnP0JR/v7xTDCh3iPk/kSddYD/frjTOR/lBO+RBJ9YbqIvVeNgUp0UgyNe3ntlKuV986ei0GBS4LyeWqrFhUYqYn2L5FG3QTcgJPKzoxcmB0Pnwf2BG4234iNcmCQTsyGTgFdYxhGKiajE44BkxOSE6w98p+oKe9PXY7ptOBKWBKB8eB9Tv8IFpjB4= 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)(376005)(36860700004)(82310400017);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 10 May 2024 21:18:25.3536 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 6d2a2326-e8d0-4866-2269-08dc7136baad 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: BN1PEPF00004683.namprd03.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: MW4PR12MB5603 Implement a platform hook to do the work of restoring the direct map entries of gmem-managed pages and transitioning the corresponding RMP table entries back to the default shared/hypervisor-owned state. Signed-off-by: Michael Roth Message-ID: <20240501085210.2213060-15-michael.roth@amd.com> Signed-off-by: Paolo Bonzini --- arch/x86/kvm/Kconfig | 1 + arch/x86/kvm/svm/sev.c | 64 ++++++++++++++++++++++++++++++++++++++++++ arch/x86/kvm/svm/svm.c | 1 + arch/x86/kvm/svm/svm.h | 2 ++ 4 files changed, 68 insertions(+) diff --git a/arch/x86/kvm/Kconfig b/arch/x86/kvm/Kconfig index 10768f13b240..2a7f69abcac3 100644 --- a/arch/x86/kvm/Kconfig +++ b/arch/x86/kvm/Kconfig @@ -138,6 +138,7 @@ config KVM_AMD_SEV select ARCH_HAS_CC_PLATFORM select KVM_GENERIC_PRIVATE_MEM select HAVE_KVM_GMEM_PREPARE + select HAVE_KVM_GMEM_INVALIDATE help Provides support for launching Encrypted VMs (SEV) and Encrypted VMs with Encrypted State (SEV-ES) on AMD processors. diff --git a/arch/x86/kvm/svm/sev.c b/arch/x86/kvm/svm/sev.c index 2bc4aa91cd31..379ac6efd74e 100644 --- a/arch/x86/kvm/svm/sev.c +++ b/arch/x86/kvm/svm/sev.c @@ -4663,3 +4663,67 @@ int sev_gmem_prepare(struct kvm *kvm, kvm_pfn_t pfn, gfn_t gfn, int max_order) return 0; } + +void sev_gmem_invalidate(kvm_pfn_t start, kvm_pfn_t end) +{ + kvm_pfn_t pfn; + + pr_debug("%s: PFN start 0x%llx PFN end 0x%llx\n", __func__, start, end); + + for (pfn = start; pfn < end;) { + bool use_2m_update = false; + int rc, rmp_level; + bool assigned; + + rc = snp_lookup_rmpentry(pfn, &assigned, &rmp_level); + if (WARN_ONCE(rc, "SEV: Failed to retrieve RMP entry for PFN 0x%llx error %d\n", + pfn, rc)) + goto next_pfn; + + if (!assigned) + goto next_pfn; + + use_2m_update = IS_ALIGNED(pfn, PTRS_PER_PMD) && + end >= (pfn + PTRS_PER_PMD) && + rmp_level > PG_LEVEL_4K; + + /* + * If an unaligned PFN corresponds to a 2M region assigned as a + * large page in the RMP table, PSMASH the region into individual + * 4K RMP entries before attempting to convert a 4K sub-page. + */ + if (!use_2m_update && rmp_level > PG_LEVEL_4K) { + /* + * This shouldn't fail, but if it does, report it, but + * still try to update RMP entry to shared and pray this + * was a spurious error that can be addressed later. + */ + rc = snp_rmptable_psmash(pfn); + WARN_ONCE(rc, "SEV: Failed to PSMASH RMP entry for PFN 0x%llx error %d\n", + pfn, rc); + } + + rc = rmp_make_shared(pfn, use_2m_update ? PG_LEVEL_2M : PG_LEVEL_4K); + if (WARN_ONCE(rc, "SEV: Failed to update RMP entry for PFN 0x%llx error %d\n", + pfn, rc)) + goto next_pfn; + + /* + * SEV-ES avoids host/guest cache coherency issues through + * WBINVD hooks issued via MMU notifiers during run-time, and + * KVM's VM destroy path at shutdown. Those MMU notifier events + * don't cover gmem since there is no requirement to map pages + * to a HVA in order to use them for a running guest. While the + * shutdown path would still likely cover things for SNP guests, + * userspace may also free gmem pages during run-time via + * hole-punching operations on the guest_memfd, so flush the + * cache entries for these pages before free'ing them back to + * the host. + */ + clflush_cache_range(__va(pfn_to_hpa(pfn)), + use_2m_update ? PMD_SIZE : PAGE_SIZE); +next_pfn: + pfn += use_2m_update ? PTRS_PER_PMD : 1; + cond_resched(); + } +} diff --git a/arch/x86/kvm/svm/svm.c b/arch/x86/kvm/svm/svm.c index b9ecc06f8934..653cdb23a7d1 100644 --- a/arch/x86/kvm/svm/svm.c +++ b/arch/x86/kvm/svm/svm.c @@ -5083,6 +5083,7 @@ static struct kvm_x86_ops svm_x86_ops __initdata = { .alloc_apic_backing_page = svm_alloc_apic_backing_page, .gmem_prepare = sev_gmem_prepare, + .gmem_invalidate = sev_gmem_invalidate, }; /* diff --git a/arch/x86/kvm/svm/svm.h b/arch/x86/kvm/svm/svm.h index 4203bd9012e9..3cea024a7c18 100644 --- a/arch/x86/kvm/svm/svm.h +++ b/arch/x86/kvm/svm/svm.h @@ -737,6 +737,7 @@ void sev_handle_rmp_fault(struct kvm_vcpu *vcpu, gpa_t gpa, u64 error_code); void sev_vcpu_unblocking(struct kvm_vcpu *vcpu); void sev_snp_init_protected_guest_state(struct kvm_vcpu *vcpu); int sev_gmem_prepare(struct kvm *kvm, kvm_pfn_t pfn, gfn_t gfn, int max_order); +void sev_gmem_invalidate(kvm_pfn_t start, kvm_pfn_t end); #else static inline struct page *snp_safe_alloc_page(struct kvm_vcpu *vcpu) { return alloc_page(GFP_KERNEL_ACCOUNT | __GFP_ZERO); @@ -757,6 +758,7 @@ static inline int sev_gmem_prepare(struct kvm *kvm, kvm_pfn_t pfn, gfn_t gfn, in { return 0; } +static inline void sev_gmem_invalidate(kvm_pfn_t start, kvm_pfn_t end) {} #endif From patchwork Fri May 10 21:10:19 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Michael Roth X-Patchwork-Id: 13661954 Received: from NAM11-CO1-obe.outbound.protection.outlook.com (mail-co1nam11on2046.outbound.protection.outlook.com [40.107.220.46]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 9FDFE10965; Fri, 10 May 2024 21:18:48 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=40.107.220.46 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1715375930; cv=fail; b=D5niY4F1UfoSWYjZUFZ3T+Z+prhwwXXrkPDPrLTfBo33Ld959AG2YlLEh1dPUIjsx5qx7cWMx8eG+7zqy+Cypb3RCfoeOiGYGIveMwXHQSncej3pN0VwA9bWx6/eWPjCZ5WcNpM4ripf+e2Xh+DRHYrxXZa1Um2kwI7HoDdSxEY= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1715375930; c=relaxed/simple; bh=9JCtn8+S20SwpWMEhIghFBGaVob1SNk4SVHk5JxPUlg=; h=From:To:CC:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=VT7JzUDabuE7cUmL1FG1xoB6OZcw0kvzY+5VCIWk7ssryErgyeb0cRGlEchkknvlDot/gZ/jy+28pxaFYADQ98KSLj1HXmBXuNymRr103cQgOxvJPDV1rMFBoUQbdkNew7bsLn6+Cyc4kDB3wiAgCJTAKuc5Yc4g0gp4L0ZzwIc= ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=amd.com; spf=fail smtp.mailfrom=amd.com; dkim=pass (1024-bit key) header.d=amd.com header.i=@amd.com header.b=slJ9PMxf; arc=fail smtp.client-ip=40.107.220.46 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=amd.com Authentication-Results: smtp.subspace.kernel.org; spf=fail smtp.mailfrom=amd.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=amd.com header.i=@amd.com header.b="slJ9PMxf" ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=A0r1KWLwG7ZnpQmhXJ1xhWAKAdngetJRDPT/cfQBJkmRPfAjqs9pb01PycFr33bD02VXCagiTW6FQiwBKbwXo6MsP0XgxFhtzVVqISsE99eJFigkHCwzDv9AFG9D4nKYFIGbGwRLrIlp7DzPckQDLgUAOuuPRzv7rkGpSDcETSjIMGvLlSynaVYiLAXp1ixaBEZ15iek5majVQKkbONcJRN/Txsa0IrCd35hnduGsSg7WHrUS+rbOhPPkOVMCVQ/D6gWIubThDzy2kjHUyIMPH2xv0+LTMQBcC4ykCXQepQV8vUmtS916zO0zoPWgYOFR4szcXlSEYKDXNZ2bVkwOw== 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=cfJHXmG/cO0gjpyu0/O7QuOJmDVvYEZ92UUxOzehpUA=; b=FxdKV9xht3petyiRPmb1RIVr2He1aOp68bIjSkKO7evGmfQSOD+MYFmflj1gxKHae0slNuzleMTyDH4MCzmHEOewWfu55DdwK26NV9vo0I2f1+yPSPHgDXozetG3Qva/TdBVgVwq5fTiSRuc+5E7ry9QoFLGLbA73B9T1ib4js8qdtGLxfdb0OV4h0+OXQO17DbuxkWZHp8XYEELqoXGLlkiQYG90CB92EgZoFu8quc0ry4H58DN2JWjlj5cxR+Wn4nc4hh+ldkPmJJgiIt4ujBqmBIBVNTN8YsWTD9ODnYDH3bl7RDDCOE6kBASKFiAyQE3L+smLMi61TU1WpZeZg== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 165.204.84.17) smtp.rcpttodomain=redhat.com 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=cfJHXmG/cO0gjpyu0/O7QuOJmDVvYEZ92UUxOzehpUA=; b=slJ9PMxf9ZdXf/CdRus/tLKCFO7jb++QWjeA3UaMl10m/qX5pPDkm6toeJk7KTl5H7ZvCDv1XGbs3vOZfZRQT0BGvatWEO6/gW6XhPuq18AIjtI1b4eVXEXSW7FxIq1hEBIsjd25J1ad0QXjjDFRPtQ0kCmOJy9Wc8VWrpnPYh0= Received: from BN6PR17CA0060.namprd17.prod.outlook.com (2603:10b6:405:75::49) by CY8PR12MB7195.namprd12.prod.outlook.com (2603:10b6:930:59::11) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7544.42; Fri, 10 May 2024 21:18:46 +0000 Received: from BN1PEPF00004684.namprd03.prod.outlook.com (2603:10b6:405:75:cafe::e3) by BN6PR17CA0060.outlook.office365.com (2603:10b6:405:75::49) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7544.51 via Frontend Transport; Fri, 10 May 2024 21:18:46 +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 BN1PEPF00004684.mail.protection.outlook.com (10.167.243.90) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.7544.18 via Frontend Transport; Fri, 10 May 2024 21:18:46 +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; Fri, 10 May 2024 16:18:45 -0500 From: Michael Roth To: Paolo Bonzini CC: , , Sean Christopherson Subject: [PULL 14/19] KVM: x86: Implement hook for determining max NPT mapping level Date: Fri, 10 May 2024 16:10:19 -0500 Message-ID: <20240510211024.556136-15-michael.roth@amd.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20240510211024.556136-1-michael.roth@amd.com> References: <20240510211024.556136-1-michael.roth@amd.com> Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 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: BN1PEPF00004684:EE_|CY8PR12MB7195:EE_ X-MS-Office365-Filtering-Correlation-Id: b9554561-7609-487a-3ad5-08dc7136c6fe X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0;ARA:13230031|376005|36860700004|1800799015|82310400017; X-Microsoft-Antispam-Message-Info: 262MfFX+R1N7pxo6EyEa45/LqjiJ0SseMJZpXBHmh9D3ftAEURbMOS0TVE/AZ9YJtpxGv6Iqznri0XpYiXu6/EExQQFwCpKiE+Dqun+x9Kr42I2tsTumdBQPi2MG9EDrVdofQW7BtG4BxdivSfgh9NCuiiRQqKtdjwhJdEkXAtpLODzFAPCyDqJvbU0xFV7ZNqp3Tmix7dTS8Cs+fphhOcS/b/QaulvZpiwUdJ0lxgaD7jMP2Xod+d5ejeW9yKMuI9pPObAXEzEjcVmPU96HOyMaMLsItYvPZtRr4y8lkk42UOvWn45MhvOKH9vTuZbjUhkNqhrhVkurFwvdIzvE2ZOPwECEslDBY7n7FVKgMSPUr9CLEgFLJ0mJLWZ4TyiO8AC8csL1R7cuRSGg8OOS+ceRg9XpYjBUcIQUDozDtkchfaPD9OpWmDAGV3stx/pRS7PXPIRu/k01yRW9AI49j3Aq/eIjGxDMBwe21VUHXrkSEAxz9+6wQdCQxo20uvyaogBovdqpv8aL5lHCduTvhXAyz90UFjkB5wR0tJHaXBzx8j74otIYLZ7UnXLU7/fSbc8BJPAI0K5XQLM6gTAeiy3Slupl3JIJd7dpYUCUCjeO52sQ3NLyik6cGk3DrEKjHpzcitTEJYtYgizS7ssdDKGanwKZt2ZF951A3+fVK+3KG2+ZC4McWBc0aEXb3wY8cnc6eLi5FuwtPYRtT5f9t+M2f3mZ6lGoeLNCuqWPuMQifmuDeRQCdKZPgd2+Worp86sszyldWlF5TezD/b+AX9Bck398Cw7gEWBplmIMcSiYSE+3Y3YW8g9RJn+RT2+RIVyEfVSnUBnkLwZglnD0zI1sG1w5i4xNTjWb5qMLdLIdVfM2AeI6Wq1nC2Q1XW5U/lkUd5pe6U8Z6MDt4sApUClqNLq66ZNNHrODtOj7SDfixRqCTMj5Zd826ZgG5x4zGJwudRLLLea+Eo3/gGOixKZUZM5kGzch4/9F9kZ37rGqI0eGul6TJavJuxA6zpPJ0z2dxMLZotqUgX8khBDdmiLhw6WET63BzMY7qX+DvA9gMJgv0y/8piQybGul/bjiZ0IusdGTdu1EKDBskqaK9TvM0CJd56bxnuw3yZ6LRxrAP0204LelyTbrl9Ic3WhEnIv1yxB4S4Xolv2zW4zkh6zeoeVz53ToCy9jiMrzspQklDiD0AA3cWKxpWjsZkRRg1xS69vZywlev/11DlltQ3AaRW52UEqV01miZ739RJvxa8kGbXS7kVMSnrxVMD5aiowNtg4HWjYAcRazcUWA6E7zkvZP4FM6OhRY0bp2URRsqmERfGe63ABIMtZukqngdtjannNpEwQzpqjtvW6WUxw0l434RCDKivT2za1PMu2wLJMrMn1qfd5xcLEyplH+ 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)(36860700004)(1800799015)(82310400017);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 10 May 2024 21:18:46.0021 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: b9554561-7609-487a-3ad5-08dc7136c6fe 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: BN1PEPF00004684.namprd03.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: CY8PR12MB7195 In the case of SEV-SNP, whether or not a 2MB page can be mapped via a 2MB mapping in the guest's nested page table depends on whether or not any subpages within the range have already been initialized as private in the RMP table. The existing mixed-attribute tracking in KVM is insufficient here, for instance: - gmem allocates 2MB page - guest issues PVALIDATE on 2MB page - guest later converts a subpage to shared - SNP host code issues PSMASH to split 2MB RMP mapping to 4K - KVM MMU splits NPT mapping to 4K - guest later converts that shared page back to private At this point there are no mixed attributes, and KVM would normally allow for 2MB NPT mappings again, but this is actually not allowed because the RMP table mappings are 4K and cannot be promoted on the hypervisor side, so the NPT mappings must still be limited to 4K to match this. Implement a kvm_x86_ops.private_max_mapping_level() hook for SEV that checks for this condition and adjusts the mapping level accordingly. Reviewed-by: Paolo Bonzini Signed-off-by: Michael Roth Message-ID: <20240501085210.2213060-16-michael.roth@amd.com> Signed-off-by: Paolo Bonzini --- arch/x86/kvm/svm/sev.c | 15 +++++++++++++++ arch/x86/kvm/svm/svm.c | 1 + arch/x86/kvm/svm/svm.h | 5 +++++ 3 files changed, 21 insertions(+) diff --git a/arch/x86/kvm/svm/sev.c b/arch/x86/kvm/svm/sev.c index 379ac6efd74e..d603c97493b9 100644 --- a/arch/x86/kvm/svm/sev.c +++ b/arch/x86/kvm/svm/sev.c @@ -4727,3 +4727,18 @@ void sev_gmem_invalidate(kvm_pfn_t start, kvm_pfn_t end) cond_resched(); } } + +int sev_private_max_mapping_level(struct kvm *kvm, kvm_pfn_t pfn) +{ + int level, rc; + bool assigned; + + if (!sev_snp_guest(kvm)) + return 0; + + rc = snp_lookup_rmpentry(pfn, &assigned, &level); + if (rc || !assigned) + return PG_LEVEL_4K; + + return level; +} diff --git a/arch/x86/kvm/svm/svm.c b/arch/x86/kvm/svm/svm.c index 653cdb23a7d1..3d0549ca246f 100644 --- a/arch/x86/kvm/svm/svm.c +++ b/arch/x86/kvm/svm/svm.c @@ -5084,6 +5084,7 @@ static struct kvm_x86_ops svm_x86_ops __initdata = { .gmem_prepare = sev_gmem_prepare, .gmem_invalidate = sev_gmem_invalidate, + .private_max_mapping_level = sev_private_max_mapping_level, }; /* diff --git a/arch/x86/kvm/svm/svm.h b/arch/x86/kvm/svm/svm.h index 3cea024a7c18..555c55f50298 100644 --- a/arch/x86/kvm/svm/svm.h +++ b/arch/x86/kvm/svm/svm.h @@ -738,6 +738,7 @@ void sev_vcpu_unblocking(struct kvm_vcpu *vcpu); void sev_snp_init_protected_guest_state(struct kvm_vcpu *vcpu); int sev_gmem_prepare(struct kvm *kvm, kvm_pfn_t pfn, gfn_t gfn, int max_order); void sev_gmem_invalidate(kvm_pfn_t start, kvm_pfn_t end); +int sev_private_max_mapping_level(struct kvm *kvm, kvm_pfn_t pfn); #else static inline struct page *snp_safe_alloc_page(struct kvm_vcpu *vcpu) { return alloc_page(GFP_KERNEL_ACCOUNT | __GFP_ZERO); @@ -759,6 +760,10 @@ static inline int sev_gmem_prepare(struct kvm *kvm, kvm_pfn_t pfn, gfn_t gfn, in return 0; } static inline void sev_gmem_invalidate(kvm_pfn_t start, kvm_pfn_t end) {} +static inline int sev_private_max_mapping_level(struct kvm *kvm, kvm_pfn_t pfn) +{ + return 0; +} #endif From patchwork Fri May 10 21:10:20 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Michael Roth X-Patchwork-Id: 13661955 Received: from NAM12-BN8-obe.outbound.protection.outlook.com (mail-bn8nam12on2081.outbound.protection.outlook.com [40.107.237.81]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 0CA8013B5B0; Fri, 10 May 2024 21:19:14 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=40.107.237.81 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1715375956; cv=fail; b=brUnQiHzZigI7WIyY5afsc5+N8LxZwjoGhfCACCuBuoMuKFvmsxLCv4Eskhe6VYoft/vgxWHdLY9WrzDMxIRuEH8gEIJpFJ0cm0o5+NQ1oCXEVLK750Oem79eKwZLWFI9Tw3r+f2CggJttFoiAcH97inQMcZdrWhL1EEV1IfuLg= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1715375956; c=relaxed/simple; bh=94yTgcGA/ly16/FIngwjmMi5u6ODPosk4CvL8YUcyxE=; h=From:To:CC:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=Wknb8fCsrw0Eq4wEFRDqQD5ydV/56Q/JtKqQHvhBRIe1/V7qKblqzh3VocR+mU0YmDr9hSTw6LI0+7dUjzSWzP2EsM42yfIsIaFoUak4WnT4k6i2IxV/sgsbbsiRbX+HgPn69V0u8wbV/hV9LRg9p0G4jV9bo2c33LGB4KiG0B8= ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=amd.com; spf=fail smtp.mailfrom=amd.com; dkim=pass (1024-bit key) header.d=amd.com header.i=@amd.com header.b=L1CBDA1U; arc=fail smtp.client-ip=40.107.237.81 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=amd.com Authentication-Results: smtp.subspace.kernel.org; spf=fail smtp.mailfrom=amd.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=amd.com header.i=@amd.com header.b="L1CBDA1U" ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=jXVWcCpAtgG43RygsXvYPrF/iIenO6CZ5KUgpn6FFx9FhQrH+DI8npmWhLOCh4EUE9gARydHY+l76qtyQCwv58F3cFKwbroU6BW8lOryph2uK4Rws4g4IiqgnCzaxjQOLDSzw4wtc0KYq7By0y7rUUUQxv60J0XeKB5scVK+pTpRZ2SWxiHhcziYDTmoSawtDjFcQQBEzeYcHsaqZxKTpqYgyxpYMiqv+40OANkGCv3xCwdz491mw+BHlCqLpjQfESTccR8RJA2ItGG7lOKdMLvzs8GUkpGVDsUqv2Wy8WRZlS09n2n4cyIuhHCJNhS9qsAzxZqi7Tf1kWWEVRqldA== 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=1rthMVXzmZSrOVfdB/bmr414hVb92xCnklO7UjIWGDw=; b=gHpyQiKVsEmp6YFtYpGaKDlL2i726H/FzqfD9bQJf6XOQr3CXuWJOBoUn7DAL1FWhlMFxrXxHVwmwETWQlcyr5Hc58V812OH5O5AU9UTQLqOtCj4YeIisNRjdqbxZ6SwIEDmKU8TxXx6U9/JTpRcrLc5G08MDcbA8Uox0sbyXvf8ZfYYYZci4ldS8f5IlG8nL4qClp3t8izlII4b5mYjak5OSajkwPLMdPTemoRu1qdNTpy5Qgs4uDUCBE4C6/ZpW+mTDhB0dgDZKW8epgU6+q8qe2eXQO0zgZiC0uuthwVnS4Ue/csKU9MxAe8FObEC37YRNA7+KbT2JowErF3Zhg== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 165.204.84.17) smtp.rcpttodomain=redhat.com 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=1rthMVXzmZSrOVfdB/bmr414hVb92xCnklO7UjIWGDw=; b=L1CBDA1Uwz0LtpwduH1qUk4NaOpkbLsWDUTC6hXTTzgbYl0TsVWU3mkXWxv3vtceoUd9wb2r1tvmlryuja0PJJWq9ypqA2SgfM0aBoF3sXMNtqh/YJXqD6TM80Ls7t/m0h9aP7/Ruxv1Sn+lCTyPg/enkOwiUpmiWgyO4ai1r1k= Received: from BN1PR10CA0017.namprd10.prod.outlook.com (2603:10b6:408:e0::22) by IA1PR12MB6457.namprd12.prod.outlook.com (2603:10b6:208:3ab::17) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7544.51; Fri, 10 May 2024 21:19:09 +0000 Received: from BN1PEPF00004686.namprd03.prod.outlook.com (2603:10b6:408:e0:cafe::61) by BN1PR10CA0017.outlook.office365.com (2603:10b6:408:e0::22) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7544.51 via Frontend Transport; Fri, 10 May 2024 21:19:09 +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 BN1PEPF00004686.mail.protection.outlook.com (10.167.243.91) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.7544.18 via Frontend Transport; Fri, 10 May 2024 21:19:09 +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; Fri, 10 May 2024 16:19:09 -0500 From: Michael Roth To: Paolo Bonzini CC: , , Sean Christopherson , Ashish Kalra Subject: [PULL 15/19] KVM: SEV: Avoid WBINVD for HVA-based MMU notifications for SNP Date: Fri, 10 May 2024 16:10:20 -0500 Message-ID: <20240510211024.556136-16-michael.roth@amd.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20240510211024.556136-1-michael.roth@amd.com> References: <20240510211024.556136-1-michael.roth@amd.com> Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 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: BN1PEPF00004686:EE_|IA1PR12MB6457:EE_ X-MS-Office365-Filtering-Correlation-Id: 4f34a3a1-bcea-4765-bce1-08dc7136d4fa X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0;ARA:13230031|82310400017|376005|1800799015|36860700004; X-Microsoft-Antispam-Message-Info: W+OOe4yggnrTXdgFevbmTZ273Gvtq4w8EhYMhK/htKMGKRBNINQuITVroqObtjVifTh3a4e7e9j7E0wJBEHyNvD+Y1Rbu5rjn6wYxylH+6T/ktqbNwQ5A8eEFg1S4J4jO+QKWc9I1dpJSm+FITy1bb3swGf0nAtOaegOzUwP5BFgP6gE8mKvEL/KqEBAeTDpk46X51FEtKIpWh51gTvIsZDbtf/XFd6SllJ1Naa3Yp5iON7hSRW0LaC7bIeIWJ0HOo7Iss6R1DWnyN1CJXEdDkmUb8cMM68ufrKBCVQuW0O6rX0HYghS857Nirmqge4lAYBoDx3Fn+rHXV2nLrrqXhOKK3MlmBPFvAMGi3Z0TJR29le7ainq3M0IPJO6/BYQEIn9qtJSCxFZXO11cQFhsB2pPgNMPZPhiMaFiKAbb+81A36PQZqOHckmwrxDzl19NQsfAwgbv0vZoh7kDg0kl6awYEinc8zqbLSrHf4M7+jZ0ViDjvTDc+vDhwM9Z4fnitGWy1Ex469fBGHZt7oHGU6jHouqk2IyzvsPiz7rfA7SqOiu+l9aF+bcGJphH42qF4wyioRBY+rLF3Bs3y52JGP0pl5jKWHs0t+wRTO/mh867ZcqIOWEKCrtPBWYH8Q0w47WZ/Ewnt8gcVXNI+/e+pWNjzgA1o8Gbv0h0uOdhLehRrsvm7oN8ljq8Cu8jVWs0WmvuzhCzFpPoLBFX8XwGNGmDHjoqQ0yiZxzCaLUqN48+eCpuyamMDu/V5o/yfabSMBgvaVWZBUMidXOGwL4Qbg73293A+BpazhjEPZYL+s2QaHlF9y7LA1CJK7+lELrrXqCSbvplmtdtKnOeF16NmXOh1H6GNAwbXcQiwWWzOJa6cMOZ17PBTE4Z92CSCxiP2upcYCqbum9OHqZ9//vA7BKFAAAnHp1vA/8DBkwT6kNU6L0CKpvZH6/83wveKuRsqjkXwXrbpfXbi9QHgAVv2C2Lx0eQ+Z/kzrExh050+dZJ+pdnh85IZM5EHkZTcTei0UsZeH5+v4gy0GWmosDjJJ16LpsuhwoavNQU1fJjGj5cMY7fOS2uX30tD9Bto9QT3YImKThrnI7e3MLcok2oB7Kt8fxGtTp+hpNQqL5CUsRrvHUft3Pg7g3m4wBPFNdB2ikJi9p+nRwKSzv5leH7r55htQ3uTczOvwHvhMGx2NyRb5wPFxlmJmsACRLbl2lECs+ZTc5w/bLsqanJ7T6Lv6i0frTXfkFB+D2UbjEWZL1f/Yoc8HEX/UQEdN6SffDSJNbHROJFrV6EP0tofLfCoOAg621oku0VJWUS32NaMtjyxbbI7OcxurTWVp9QcEtByvfTDYuSAA54ARXXewY8IWZiHvJz2mG/WaoDOtfQd0= 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)(82310400017)(376005)(1800799015)(36860700004);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 10 May 2024 21:19:09.4673 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 4f34a3a1-bcea-4765-bce1-08dc7136d4fa 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: BN1PEPF00004686.namprd03.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: IA1PR12MB6457 From: Ashish Kalra With SNP/guest_memfd, private/encrypted memory should not be mappable, and MMU notifications for HVA-mapped memory will only be relevant to unencrypted guest memory. Therefore, the rationale behind issuing a wbinvd_on_all_cpus() in sev_guest_memory_reclaimed() should not apply for SNP guests and can be ignored. Signed-off-by: Ashish Kalra Reviewed-by: Paolo Bonzini [mdr: Add some clarifications in commit] Signed-off-by: Michael Roth Message-ID: <20240501085210.2213060-17-michael.roth@amd.com> Signed-off-by: Paolo Bonzini --- arch/x86/kvm/svm/sev.c | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/arch/x86/kvm/svm/sev.c b/arch/x86/kvm/svm/sev.c index d603c97493b9..2b88ae9a4f48 100644 --- a/arch/x86/kvm/svm/sev.c +++ b/arch/x86/kvm/svm/sev.c @@ -3039,7 +3039,13 @@ static void sev_flush_encrypted_page(struct kvm_vcpu *vcpu, void *va) void sev_guest_memory_reclaimed(struct kvm *kvm) { - if (!sev_guest(kvm)) + /* + * With SNP+gmem, private/encrypted memory is unreachable via the + * hva-based mmu notifiers, so these events are only actually + * pertaining to shared pages where there is no need to perform + * the WBINVD to flush associated caches. + */ + if (!sev_guest(kvm) || sev_snp_guest(kvm)) return; wbinvd_on_all_cpus(); From patchwork Fri May 10 21:10:21 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Michael Roth X-Patchwork-Id: 13661956 Received: from NAM12-BN8-obe.outbound.protection.outlook.com (mail-bn8nam12on2041.outbound.protection.outlook.com [40.107.237.41]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id DA20513BAF7; Fri, 10 May 2024 21:19:36 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=40.107.237.41 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1715375978; cv=fail; b=g/1pI3DAPHyU9l/6j1NTOmDazrIA3E9VauzrsCfRvXEIzKlz1szgcjIOXZwlsU/Nvvbgc0mtMMaZ92iAjoFlXzlA2wtildTAgBXe8F96fFswZzaXVzGy/Eopl3hjmt41CgUUKE0s61FcjMl0cJc20txfjxTU2/qoF64mQZu3784= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1715375978; c=relaxed/simple; bh=cA0BRdlMgj4mS21Y71WnWS8rynBMNHYJELrdO/3lfpI=; h=From:To:CC:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=r0O9V2Sas1n9WMsEUMe1TIVkc2nqRAcVvBdci+ln1P1IM6OjpN00dtTil9srHy+MS2wAqpjV05spyrEGPs/W4PDJJ8iLXDJ1vSs1JCDg3QgWOKiKiX8MjfOoyyvXWgbjn17B6j7nC5CQpUiTtBlNh58Iy+5LBw1qNIT3z5b2bnM= ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=amd.com; spf=fail smtp.mailfrom=amd.com; dkim=pass (1024-bit key) header.d=amd.com header.i=@amd.com header.b=YSE+iF23; arc=fail smtp.client-ip=40.107.237.41 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=amd.com Authentication-Results: smtp.subspace.kernel.org; spf=fail smtp.mailfrom=amd.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=amd.com header.i=@amd.com header.b="YSE+iF23" ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=O+sMXxe/YMdEc9NpQon3L5sxtaMv72WVWpnC+KTufHwPx5aeLnAyBrzkhQGKXf01RxOCjFFR4wnu6ZB7zM1NzUJHy7qceuUGuBvGph+ofk5IFxQiDYcwCQF1ARmllHWPIhEHrjn+YNvQ+hUoDNSFhLvk2/zdzTlDWzc8SNMj4fTI1J75jeXEnugNdHdIdr/XRNfjNNkTwRUaox5wO7lHOjx6P23XHsYoG1RSq5sbl1SRUTIUQE9REUYcT4YUrEd36vb7fAtTO5etdgILcwND4zJ/2T6taQVp+1ugUGRjo2Vi/cPTXKxecbCLUOtPXmwgVfMn42YD8jjA3dn6RZYw/A== 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=x19kI9HmknPHP8OG4YN8Ig8gnOdNgqW/ZC6ugVq9jYM=; b=D2I4oKtUWaXD27+VfkTF1G2b6QPQtaGI5GnX0Bi/PeHf2uQaUyww95mnjlitzcKBLubmycdWZV3euoVqNWF47+ItVknmykGDssovCLg7EmRfrc7HQbTpfGE9KYOj3Eb4uhdlphn2ZN9JRvAFH0vkYJ/rxad5f7U0J6jmzhzukkvw11uJuGNzOMkqTMPhVe59QTSAAzR0mnAzu//07OKwUBZgbCRYNMXDc+LfLUL3SZ/s+Y3t4FUVfRlOIlGKAEJou/6E4h8AcpxlwE+WVgLxZNIG9g60N8ztOIhJ3jq3cgPrCMx6Y545DmlpTuJ+cdtFskH+Z23YS+0eIZMAErVpaQ== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 165.204.84.17) smtp.rcpttodomain=redhat.com 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=x19kI9HmknPHP8OG4YN8Ig8gnOdNgqW/ZC6ugVq9jYM=; b=YSE+iF23+eIr3Dg/kOhpsAYfMqjwQxc+7qqRbERSIDzhY9tpRPxiu61Hn5FS6IBmlIGPKgHSgvcqzUYlPugcLBWuwqTcqzVXnQcjjSjuCtPPb+eTjzVNKMJH0cURX1BZRdM3O0vowbPtt3X5kFabpAj5cjJD/3Hi6EuhNQJ2emc= Received: from BN6PR17CA0060.namprd17.prod.outlook.com (2603:10b6:405:75::49) by BY5PR12MB4259.namprd12.prod.outlook.com (2603:10b6:a03:202::17) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7544.51; Fri, 10 May 2024 21:19:31 +0000 Received: from BN1PEPF00004684.namprd03.prod.outlook.com (2603:10b6:405:75:cafe::d3) by BN6PR17CA0060.outlook.office365.com (2603:10b6:405:75::49) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7544.51 via Frontend Transport; Fri, 10 May 2024 21:19:30 +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 BN1PEPF00004684.mail.protection.outlook.com (10.167.243.90) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.7544.18 via Frontend Transport; Fri, 10 May 2024 21:19:30 +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; Fri, 10 May 2024 16:19:29 -0500 From: Michael Roth To: Paolo Bonzini CC: , , Sean Christopherson , Brijesh Singh , Ashish Kalra Subject: [PULL 16/19] KVM: SVM: Add module parameter to enable SEV-SNP Date: Fri, 10 May 2024 16:10:21 -0500 Message-ID: <20240510211024.556136-17-michael.roth@amd.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20240510211024.556136-1-michael.roth@amd.com> References: <20240510211024.556136-1-michael.roth@amd.com> Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 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: BN1PEPF00004684:EE_|BY5PR12MB4259:EE_ X-MS-Office365-Filtering-Correlation-Id: b666e59a-0a8a-435d-4144-08dc7136e15e X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0;ARA:13230031|1800799015|376005|36860700004|82310400017; X-Microsoft-Antispam-Message-Info: gtv6qQTJseyvcCzV1KBCphe37jb/h3v9t+HwW4JTrNjqAPpPYuGpf6oJZEeEefvkaFo4CMoTaH4p2anhBaqz5RWhcd4xeqQIwof+j5YAFPv6J3NQkPVWM/O+4itfIVaTOfbXZfetpCO3pQJGAcIkMnobrZN4yv8CcCrtPeJPpWsfwmElTzE245xG/bIn6PjH2Adh61xug1vxzyNqQrhp9kZ3S1S+yCjbf0yvRTYbeMGyadi8FcP4sMW3uNQA+Z5GnXdMbyyYJvBEo7tnOpd5abz+nfJPOEDsvGwu3jQen2ZW9piiY4mmbv0Fk+FQvu7VBdLd/wSxA7fWIZRW2ZB+4Zu+mOUDHu/bgoMiD7A1Cbw/UE7YKkBCa5Z35rF5Bv1VwkyMQgGxSRSE7qcDnGy7vjlwNHf36GJVRrlM6VaCSCX4DGvl1dCyQq475vM2ms09pNdcWUj/T0jfKuGj0lIw6li2hUrTJ0lbS89Vt7cdaa9RX96yzMfpp4OmkZZb+LlKJPdp1ySKZCUidqV7AcDLFb7+9XkldDRGIkBkh+G1aRhvQ9FiGJxC4Wmkx7q0yKFFaw/NsYHsABPjKtIaHjgpqKO4WTP10BZHN5IelMRL2ygVKlY98sr+31XgbVIi+C4W6BF0obWz4ngOLG7InsDS2kESGo7CfPxLBITZIj8jnRpE3j0RW+mL+KvfuEVueNOkANZ23WRqHsTQCyj8EtFaP6Ikm09DsO+k2IsXqv+tqHBQNZSq3gxCxBfnF+YYIU2AskGO5UZmNxenBCvGdSS7ZzYJFIl1XL+/2vMk6WOeMUD37J2AZxas6NOLf8K3dZxLxpofLoHaVgDF5gVeMpvmN9d2TA/5bs/zvw60eTCd6AiaDesouzuXdg3UhKCvkbO3zg4QAAIp+qPA8DItuaV6HPqlFQG2dtjQYa93reguzOi9osKmSWl7dYZNvlAZH49MuX3SpGkuEUVPZqnSeVJWcCbJ1vidS8CbPqDVJBA5v1Mpxfz8KjE6HxoKmNj5GOysHhGJn3BhfSvjGHYT6ZbplE/Opq8pUN1Ytl4EEquu1YCYp8T832vJdcSFjjZa3iRehNYpF2tPk3FwoB+rLUFUSD2wylA+PbQjXYVA2v366nxiR6vWohXoG6tBUx+pSuOt+RcUsSlinkFid51q6JztyLyZ3UBOuZiEjAPO+vHQrYGKDyR5LjwNRuhMECpQgFrJGeYdlgSf2Kb2RUqYgipWrrvZ5fo773VJkPiWbDTSpcI6ZcwwjP3olV2GYCqCHHY6m38P9qOI8xR075ZJgF0G3kNQXJxVCjySBSMfBwrBXvGNxESBwz+oeTx6DTZ0H85O5GgQCtG3pmHwX9XOXKb0LtawRrXMYMU33LTi8CG97uc= 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)(376005)(36860700004)(82310400017);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 10 May 2024 21:19:30.2522 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: b666e59a-0a8a-435d-4144-08dc7136e15e 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: BN1PEPF00004684.namprd03.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: BY5PR12MB4259 From: Brijesh Singh Add a module parameter than can be used to enable or disable the SEV-SNP feature. Now that KVM contains the support for the SNP set the GHCB hypervisor feature flag to indicate that SNP is supported. Signed-off-by: Brijesh Singh Reviewed-by: Paolo Bonzini Signed-off-by: Ashish Kalra Message-ID: <20240501085210.2213060-18-michael.roth@amd.com> Signed-off-by: Paolo Bonzini --- arch/x86/kvm/svm/sev.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/arch/x86/kvm/svm/sev.c b/arch/x86/kvm/svm/sev.c index 2b88ae9a4f48..eb397ec22a47 100644 --- a/arch/x86/kvm/svm/sev.c +++ b/arch/x86/kvm/svm/sev.c @@ -49,7 +49,8 @@ static bool sev_es_enabled = true; module_param_named(sev_es, sev_es_enabled, bool, 0444); /* enable/disable SEV-SNP support */ -static bool sev_snp_enabled; +static bool sev_snp_enabled = true; +module_param_named(sev_snp, sev_snp_enabled, bool, 0444); /* enable/disable SEV-ES DebugSwap support */ static bool sev_es_debug_swap_enabled = true; From patchwork Fri May 10 21:10:22 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Michael Roth X-Patchwork-Id: 13661957 Received: from NAM11-BN8-obe.outbound.protection.outlook.com (mail-bn8nam11on2055.outbound.protection.outlook.com [40.107.236.55]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id A864B1E50A; Fri, 10 May 2024 21:19:54 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=40.107.236.55 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1715375996; cv=fail; b=a4ls7+DgxoYSALbLbtl23YcP13Dj0oPLxydDezuKLmh6FqRVd4sMmC3HUm+V+1On8Rk7/sFA/YmO/VbfXMYkk6n3BSGcn/WLIPPCSwEE0WxJEy6pQ22voIcAQqHBTm4tjTr1qT/GUcWEjKe8PQ+qKjcpEPHJ3pRcKauAf9+B9RA= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1715375996; c=relaxed/simple; bh=JixHu/uU3svMOWwF92FALmznoa5fkZ9Aw2T9/nNX3/4=; h=From:To:CC:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=PhI2Sq5Ox/8pk/RHAJnwpT65CqalU7L3baDHjOhoiPUtfv+W4ugZQo8nMiYYmcZBTJGrU+6C/WQg/7QoHGCTZ2N/xEAG50V9fVOntW6Q2yOmQaOHHGDAir4+fc+apYuekW0IXiEeP4zv+vXUafOxXwwj3+BkHGPFDwjO5WxAGro= ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=amd.com; spf=fail smtp.mailfrom=amd.com; dkim=pass (1024-bit key) header.d=amd.com header.i=@amd.com header.b=jHLT8x/R; arc=fail smtp.client-ip=40.107.236.55 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=amd.com Authentication-Results: smtp.subspace.kernel.org; spf=fail smtp.mailfrom=amd.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=amd.com header.i=@amd.com header.b="jHLT8x/R" ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=AAEnGlx37briypQHLckVUt94+kMAh/V0HH0rUfLeJodeuAyLZOTNWKqoTVqCrSqHAoxdZnr2HgzhXrmO3uQXAp0MerNcL4msk218q3nfUdVRY2PpbPOMeIBS5QLziDugXZMLEs2xOP4CAnYzDiY1pbm7wTli9DOYaBTKbmpHlp8icbAGPNTZqj4/6Sqg24oVyqnqE2pXDlKF4eZAnwkitDFPWMB4ipMerMQA5KwpsasXwAZD7/tQVMHFoeGSVkJCLh4BqSWvBIjxp2SB6oGRzEuTCSc3aave4gsp7Xz7kSzDio73dg31/i7uGZ9++5GM8H7vCFsjBMN6rI4yW+teEA== 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=jmEjFmuunxX261Btm41kuX2m+2eXSfxymRpN4QvQzkQ=; b=bkcTLM06yvxyZFl/BYoTuxB6KswyY8zQu/dC7PeqRwgsM75nR3GW0zVxYzKntj6HqeHuX4CCU6X3SYiZQMzNmlxu1dDMT4ZM7v9xS4ejogZYd9x9HUjaL4XRJm0Fq7sebvulp6GxBSAJeOUff+JbZcAKHnNDtBZX2qiptU/ZX1augVUO48irq5u1R8bbe40DPtTFxjOBHKDAXyKeBii0WpX3IkURCKbGjBoznbH/YxJA3WCRIn33EyMrIwdD8+NwAQb/r4tglP5yX9iieKwp/HFkYlSfW6sQRVEsLqdeFlJYsaQ+nPbowEeKRvGvb8orSSF33K40ZxWnYlEJAAnO+g== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 165.204.84.17) smtp.rcpttodomain=redhat.com 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=jmEjFmuunxX261Btm41kuX2m+2eXSfxymRpN4QvQzkQ=; b=jHLT8x/RavyR+7Zp9yfGIcUKESr1d9FN+G0+cNA3EY35kIXphPWGvN12aGG4ARl/jDTSxS/ub+VHMdfZIQE6NG8k1EUL/XAoQXxrXR0rsODDWrKlhxgSZdiNUEAhPdzcH89fwsRUJz2nGH7Q/g8zo1nEWLybZ1Hp3SqW6VURj6I= Received: from BN9P222CA0023.NAMP222.PROD.OUTLOOK.COM (2603:10b6:408:10c::28) by PH7PR12MB6763.namprd12.prod.outlook.com (2603:10b6:510:1ad::8) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7544.47; Fri, 10 May 2024 21:19:51 +0000 Received: from BN1PEPF00004683.namprd03.prod.outlook.com (2603:10b6:408:10c:cafe::e9) by BN9P222CA0023.outlook.office365.com (2603:10b6:408:10c::28) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7544.48 via Frontend Transport; Fri, 10 May 2024 21:19:51 +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 BN1PEPF00004683.mail.protection.outlook.com (10.167.243.89) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.7544.18 via Frontend Transport; Fri, 10 May 2024 21:19:51 +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; Fri, 10 May 2024 16:19:50 -0500 From: Michael Roth To: Paolo Bonzini CC: , , Sean Christopherson , Brijesh Singh , "Alexey Kardashevskiy" , Ashish Kalra , "Tom Lendacky" Subject: [PULL 17/19] KVM: SEV: Provide support for SNP_GUEST_REQUEST NAE event Date: Fri, 10 May 2024 16:10:22 -0500 Message-ID: <20240510211024.556136-18-michael.roth@amd.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20240510211024.556136-1-michael.roth@amd.com> References: <20240510211024.556136-1-michael.roth@amd.com> Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 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: BN1PEPF00004683:EE_|PH7PR12MB6763:EE_ X-MS-Office365-Filtering-Correlation-Id: 7aa00738-61c0-42a0-637a-08dc7136edbb X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0;ARA:13230031|82310400017|1800799015|376005|36860700004; X-Microsoft-Antispam-Message-Info: bSzWIYrLTV/7fUNlKc22lSUeh4/5hZfi+Mf9xQHzkT0OtAUR4q7THHFEO+8bCqjtxKxGwxFw9h8FeUN5oeiQp3Vudbd82NUEaX2f020HieGkQhRiWP21GyKJsWA611Gpdr/UlGRY8Z2pKZ0doZFUCwCdIsRlLL6I8vXgyS2icsdiFA8jMiWg20/malSpY/tzEvfRbexOyl7f/9FrxU18LcUv3MxHZavc0v/3yEI8HVx3KbUXSR2fAjCgeeAO0O1it1K2rJWSJGuMM+4Wo4O7k/TH4lYDPc3p02sdjHk1O+cJekMFSNzOiH5eQJMDoQ9+LYgm4f5TieyaAZncnDpxdtE18IGFAGB/owaIQwX3CEaDiAMilNtDxO9pk7f9eGlDxBzU+5gBWw5ZfRpaJmdceXLu8empbBGqiB8QIVn0KgW/4pQEzUrD5f0+fb2U6nKTs6++Mn61AzH6zHKRBAgP/sUxK8sTsKMUgCmfzK4BAIv+44JcyESB7oaeKhegejUxqhieVXJoXvrUJuAHEKJF1iBhH/RKUOAHQMPHTZPoiwOgbucqrfC3ojGjKYyMy+bs7/6ajyxvsQSlf3NdeChvEEytcy+PR2Tw9K7K9Op/rAPS4v9RIE1m6eqjJaAs22wfap8OfVaBdJPAtu+Ft4puPIb5VeOmwrRmTp7ScfMrnWZllkC8k4pOq/x/k4bS1L+zFWnjxMY0N2dIxwpGqlJg42kR+zCje8joNZJQm1i/yS3qflfRExK7aiRHg4SKyGYCFUaBRXg/E9NIL/uk5S1hk6YSgo7md1eF/ZWvvtd5tF+5Kc+zF+vUA+30IjtndE9ekHtZerenLGER01WIavKx31sqjpmZ49/I5wHapzYQYoVq0Hm+YdAJpSXxLoscYux7V/G81452BoM0MTOJR/3kM+xgva133OpVHArFpcj9RpWByoPf1zoFpBYu7MQIZ8lVxbm+VTM97KaUZjHZXHqF+iiGxgZlBvkvbJpwGPVj/cWh3i8RLxt9Z+wi/eL8gSOKSRciMp0/fYotnZXaO16bzQVg2Fyf51iS4osl7Mfms0VDqjUWJWjF86ePjDFVwf0cCnAKXYGCFrX0tr1Iv/uog2B2HdgbxEThODEun3zkrRQg7I7fenc+z8+bOeBgtlEEB0lquuXuGsPUTKabg9wvTW+dSN73xpkjP7N/d14yV92DpovxYiHnt67v+g/Es6aD1BigkfYrAEs8DTgCsNddnG8n3/ACkjJmY3h5C2k45dQj/LCBQHuVlyGBQsi5ZFc39fEAFzJnznpg8uto4XzfEuZMudrIi2JvVeR3VDLAJhmlcCyQpRKYeyi7gljc5CAnJ27Sw0y4GK+rZvQxNHMxQRHreDAg6QU010d1xRx73cyBzQXVOBdw6/iTcgaBfRdq 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)(82310400017)(1800799015)(376005)(36860700004);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 10 May 2024 21:19:51.0108 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 7aa00738-61c0-42a0-637a-08dc7136edbb 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: BN1PEPF00004683.namprd03.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: PH7PR12MB6763 From: Brijesh Singh Version 2 of GHCB specification added support for the SNP Guest Request Message NAE event. The event allows for an SEV-SNP guest to make requests to the SEV-SNP firmware through hypervisor using the SNP_GUEST_REQUEST API defined in the SEV-SNP firmware specification. This is used by guests primarily to request attestation reports from firmware. There are other request types are available as well, but the specifics of what guest requests are being made are opaque to the hypervisor, which only serves as a proxy for the guest requests and firmware responses. Implement handling for these events. Signed-off-by: Brijesh Singh Co-developed-by: Alexey Kardashevskiy Signed-off-by: Alexey Kardashevskiy Co-developed-by: Ashish Kalra Signed-off-by: Ashish Kalra Reviewed-by: Tom Lendacky [mdr: ensure FW command failures are indicated to guest, drop extended request handling to be re-written as separate patch, massage commit] Signed-off-by: Michael Roth Message-ID: <20240501085210.2213060-19-michael.roth@amd.com> Signed-off-by: Paolo Bonzini --- arch/x86/kvm/svm/sev.c | 86 ++++++++++++++++++++++++++++++++++ include/uapi/linux/sev-guest.h | 9 ++++ 2 files changed, 95 insertions(+) diff --git a/arch/x86/kvm/svm/sev.c b/arch/x86/kvm/svm/sev.c index eb397ec22a47..00d29d278f6e 100644 --- a/arch/x86/kvm/svm/sev.c +++ b/arch/x86/kvm/svm/sev.c @@ -19,6 +19,7 @@ #include #include #include +#include #include #include @@ -3292,6 +3293,10 @@ static int sev_es_validate_vmgexit(struct vcpu_svm *svm) if (!sev_snp_guest(vcpu->kvm) || !kvm_ghcb_sw_scratch_is_valid(svm)) goto vmgexit_err; break; + case SVM_VMGEXIT_GUEST_REQUEST: + if (!sev_snp_guest(vcpu->kvm)) + goto vmgexit_err; + break; default: reason = GHCB_ERR_INVALID_EVENT; goto vmgexit_err; @@ -3914,6 +3919,83 @@ static int sev_snp_ap_creation(struct vcpu_svm *svm) return ret; } +static int snp_setup_guest_buf(struct kvm *kvm, struct sev_data_snp_guest_request *data, + gpa_t req_gpa, gpa_t resp_gpa) +{ + struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info; + kvm_pfn_t req_pfn, resp_pfn; + + if (!PAGE_ALIGNED(req_gpa) || !PAGE_ALIGNED(resp_gpa)) + return -EINVAL; + + req_pfn = gfn_to_pfn(kvm, gpa_to_gfn(req_gpa)); + if (is_error_noslot_pfn(req_pfn)) + return -EINVAL; + + resp_pfn = gfn_to_pfn(kvm, gpa_to_gfn(resp_gpa)); + if (is_error_noslot_pfn(resp_pfn)) + return -EINVAL; + + if (rmp_make_private(resp_pfn, 0, PG_LEVEL_4K, 0, true)) + return -EINVAL; + + data->gctx_paddr = __psp_pa(sev->snp_context); + data->req_paddr = __sme_set(req_pfn << PAGE_SHIFT); + data->res_paddr = __sme_set(resp_pfn << PAGE_SHIFT); + + return 0; +} + +static int snp_cleanup_guest_buf(struct sev_data_snp_guest_request *data) +{ + u64 pfn = __sme_clr(data->res_paddr) >> PAGE_SHIFT; + + if (snp_page_reclaim(pfn) || rmp_make_shared(pfn, PG_LEVEL_4K)) + return -EINVAL; + + return 0; +} + +static int __snp_handle_guest_req(struct kvm *kvm, gpa_t req_gpa, gpa_t resp_gpa, + sev_ret_code *fw_err) +{ + struct sev_data_snp_guest_request data = {0}; + struct kvm_sev_info *sev; + int ret; + + if (!sev_snp_guest(kvm)) + return -EINVAL; + + sev = &to_kvm_svm(kvm)->sev_info; + + ret = snp_setup_guest_buf(kvm, &data, req_gpa, resp_gpa); + if (ret) + return ret; + + ret = sev_issue_cmd(kvm, SEV_CMD_SNP_GUEST_REQUEST, &data, fw_err); + if (ret) + return ret; + + ret = snp_cleanup_guest_buf(&data); + if (ret) + return ret; + + return 0; +} + +static void snp_handle_guest_req(struct vcpu_svm *svm, gpa_t req_gpa, gpa_t resp_gpa) +{ + struct kvm_vcpu *vcpu = &svm->vcpu; + struct kvm *kvm = vcpu->kvm; + sev_ret_code fw_err = 0; + int vmm_ret = 0; + + if (__snp_handle_guest_req(kvm, req_gpa, resp_gpa, &fw_err)) + vmm_ret = SNP_GUEST_VMM_ERR_GENERIC; + + ghcb_set_sw_exit_info_2(svm->sev_es.ghcb, SNP_GUEST_ERR(vmm_ret, fw_err)); +} + static int sev_handle_vmgexit_msr_protocol(struct vcpu_svm *svm) { struct vmcb_control_area *control = &svm->vmcb->control; @@ -4186,6 +4268,10 @@ int sev_handle_vmgexit(struct kvm_vcpu *vcpu) ghcb_set_sw_exit_info_2(svm->sev_es.ghcb, GHCB_ERR_INVALID_INPUT); } + ret = 1; + break; + case SVM_VMGEXIT_GUEST_REQUEST: + snp_handle_guest_req(svm, control->exit_info_1, control->exit_info_2); ret = 1; break; case SVM_VMGEXIT_UNSUPPORTED_EVENT: diff --git a/include/uapi/linux/sev-guest.h b/include/uapi/linux/sev-guest.h index 154a87a1eca9..7bd78e258569 100644 --- a/include/uapi/linux/sev-guest.h +++ b/include/uapi/linux/sev-guest.h @@ -89,8 +89,17 @@ struct snp_ext_report_req { #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_FW_ERR(x) ((x) & SNP_GUEST_FW_ERR_MASK) +#define SNP_GUEST_ERR(vmm_err, fw_err) (SNP_GUEST_VMM_ERR(vmm_err) | \ + SNP_GUEST_FW_ERR(fw_err)) +/* + * The GHCB spec only formally defines INVALID_LEN/BUSY VMM errors, but define + * a GENERIC error code such that it won't ever conflict with GHCB-defined + * errors if any get added in the future. + */ #define SNP_GUEST_VMM_ERR_INVALID_LEN 1 #define SNP_GUEST_VMM_ERR_BUSY 2 +#define SNP_GUEST_VMM_ERR_GENERIC BIT(31) #endif /* __UAPI_LINUX_SEV_GUEST_H_ */ From patchwork Fri May 10 21:10:23 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Michael Roth X-Patchwork-Id: 13661958 Received: from NAM02-DM3-obe.outbound.protection.outlook.com (mail-dm3nam02on2079.outbound.protection.outlook.com [40.107.95.79]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 7D8991E50A; Fri, 10 May 2024 21:20:21 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=40.107.95.79 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1715376023; cv=fail; b=shJ7pr8PSqJL4zjku0sut4px8Aj+6UTT77e0oSHGY75jgFwgd+LHUHO+HAPqEWQrUd10SwlpENP6tLZ40O5VKACIWoolULDdZUDKVotsUegEZLKd5fj1MKjpg67OpK6NbEsdIjwySes44jncvSe50LFWXyrItGYzwV1NI6NyyzQ= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1715376023; c=relaxed/simple; bh=qq+LzuA6/F/n9MAi1f1sSPIDhS1q+yKXp8gnxHfzTbY=; h=From:To:CC:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=qyCoC3wHILmCVKrPRrD/m3fjEM7KOqj+DvLHNIaPU/xBrO4+X6IlMJyPsKWgzuabXN5kPilYfGmOV2MtAEs9HSgkmek1GCkihxIyYowjK0TIu0LPC87sK5RXaH1Z6kP0T47U+zabjzfwZBLYsaXmW3XnRQNxQyhhA7OZCb6dY54= ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=amd.com; spf=fail smtp.mailfrom=amd.com; dkim=pass (1024-bit key) header.d=amd.com header.i=@amd.com header.b=BaW8GBDd; arc=fail smtp.client-ip=40.107.95.79 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=amd.com Authentication-Results: smtp.subspace.kernel.org; spf=fail smtp.mailfrom=amd.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=amd.com header.i=@amd.com header.b="BaW8GBDd" ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=VEpmL0ZJei7/fFt4WlsV5SuBkETED0srtBb0eKQn7wlbAslR0kKs9IyLqq/mqJ2G+HxEA0LA5c1HzWiixN7e+HOs6lgdlk+qMQy1NYIv6lrmF/iPIRWhIWUdJ65grU/047CSrwpdf05XlV4MZtnUYOGP0+r4Dg1GGV8sa/m6sMJrMrSdpuYw5pZBvJj1XkM/CLbUGusQkX4W6xfwIj4yGMqc26jWNdWq1nUXe7DCaD/5or0mBtKWrTAo5zC4xGz+yy+0+h1C7aUqJbSAmHdgKNa8rKgml1kr/0zjztdERBr0pVvMFwp1NvU2zjwfARG0+0M8wvkAM/23QcG4YJQeog== 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=GEPvc8sfs2dT4aUFDZf7AguOgqs8sZ4STptfn0aYLEA=; b=NuQxmz34+ccCdQtt59SAhdQhWl3l/7/s0GXYBTLBE3asDwf40YdUJ9lBTSGG5Xtak0f/wGbzlc9C8/vW/Ax4nP/K4ExGuRhm+suOyIr9Hi+TdpFrsVzA0GyAmCiofcYUkw/9/kmaWx3mD61ga7RYA/CiD9EYkznOALYIpdxCZVHc3y85IXD4lwUZF1wqpXFzeB4Py7dqQOygEHhdsotW6QrCXVqrDiN9ZoWqHEC9kiSy9M0ru4rqQEANyaTW9bkNFmNqHECOUTwcxl+TW1jdeWaCVCqcNX+Jb+o98spyeKsWHRBHL6OAJFsF/I9F+eVs6/pN38tI0mGQrLSYYS+Bag== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 165.204.84.17) smtp.rcpttodomain=redhat.com 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=GEPvc8sfs2dT4aUFDZf7AguOgqs8sZ4STptfn0aYLEA=; b=BaW8GBDdcDVq26+odrU7pIaIwf4F4ypyFQT+0s6M2uBmElgPwuIDe6hPofpvS0+mkEEUm5E/vFfeuTT3/33fhu/fvbIdn2i/NSGMGbwpFX6OWQ4lJL2RtsJ8Ndo6AVDOBJbdfKV/i3PPFp6+JDvnrmEQn1ZbKw32lNSRaHc3FlI= Received: from PH7P220CA0119.NAMP220.PROD.OUTLOOK.COM (2603:10b6:510:32d::6) by PH7PR12MB5903.namprd12.prod.outlook.com (2603:10b6:510:1d7::14) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7544.46; Fri, 10 May 2024 21:20:17 +0000 Received: from CY4PEPF0000FCC0.namprd03.prod.outlook.com (2603:10b6:510:32d:cafe::70) by PH7P220CA0119.outlook.office365.com (2603:10b6:510:32d::6) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7544.49 via Frontend Transport; Fri, 10 May 2024 21:20:13 +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.7544.18 via Frontend Transport; Fri, 10 May 2024 21:20:13 +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; Fri, 10 May 2024 16:20:11 -0500 From: Michael Roth To: Paolo Bonzini CC: , , Sean Christopherson Subject: [PULL 18/19] KVM: SEV: Provide support for SNP_EXTENDED_GUEST_REQUEST NAE event Date: Fri, 10 May 2024 16:10:23 -0500 Message-ID: <20240510211024.556136-19-michael.roth@amd.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20240510211024.556136-1-michael.roth@amd.com> References: <20240510211024.556136-1-michael.roth@amd.com> Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 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: CY4PEPF0000FCC0:EE_|PH7PR12MB5903:EE_ X-MS-Office365-Filtering-Correlation-Id: 74b7919a-fc21-47e1-2af9-08dc7136fafa X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0;ARA:13230031|36860700004|82310400017|376005|1800799015; X-Microsoft-Antispam-Message-Info: PWcG0fxGgbp8r3hPtH+DMT7t1cd2jL3wO636MHcfkjIYgzF6YWOIKgheOcbmnT9BMQzWcxKyRbC2Dn7iPzOfughy9JFeW9Zmne6Y7Jm3Vx7tWWtgt47xpZ5mXRex7ITYPukSs+q8KThrwJ5xglMm9SsJyw+XXjAXg0lWmiw30WuuCxdOWfEAUBFLEyOBRxCKhjEiG/7ythTKPZ2YebpVR0P5f/cFiJCeOMkkiOx+A8QOUgWy8DhwHakuhX3YiBUnv8c+LpmDm5Bi7y5OP54yiChTBJWjCoTyebW8phDwkJuxMjOTImk7WIfBgNDnd/+DRJmgqX1SWMeAvEtExWwol6i4UpOW5SRSuFe/K5xeG8MKF2ap4u6vHnyu6f9MZpEKqVYg3UmFUh9/JeVXPydE8Wd53BTUhCeKJ0aRU13/uIB9mmKj/SG8WyVfMIUSJw5STO/5/vWQAzFX8pj8JxQQITXqkIZnr+4QZMa+WVFoTRSTXWWbABuvWPMw9B788/7YXhlh4QEnAUfMZgqPYR5tsh/f9vix1Yh4mWt12usIUpZWgx2ybrqjZgwGejnUpHJaEcN2uaF+6l0JazORTRNF544iLAUL0EqITPoaz7k+fGYk8MS87kL2nx8HAFs9sDiu0uXTv4VAZsFlSjyh61GaGLdX/9CBSQrcToLquJ/FXNGbzgiA4ikgbgpl49huoUQiJDfpNCGvVop0D1rx+5Cs0p7kCDK05rC8gfCVjoKJLnTTFxW+fjRBdat3hzhVxNdJJr/e9CuvjziIS7gv+XuTMGjB7xjQMKtA6cXfnK15PJHgF791DerHFTcfAilj3NaN9xnKFRCxiP5DK2nR+3bosbE7heTF72UEW9/VDGV7fr1I2ze5hVFJqmGQXWijsZI2oQQJ7jGXoC1xwVhhdTHip4jK+XTosc66M6Bq6w8bmrLcdoVej5B9Z66rco94+EOngowbkjjMRbtRahy4pegbEFx08T6xJpEMwrcbc4+g7oDz3UHO4aRi78G2c1uPZxPXy3GZyCJ01k/SnjEGYMlQCwAclJE5glGmJu56RNVaYSplhmUfOLEt7GDDWJ0q+S6Mocv6EVmul2yueZRjJbYUTJV/c8EqQXzHM5k5BsSkOr7KYw88IFJDIORdy9wv37N7bEKT2h52MoGF+mZBw19ODsAyKTb6N/Byoc7wrDA6xVT3wwRkuNejmMrKSDZNnO1GhgSfQnUAWGwpH1E00HjIc0jQjeIPprXbROIJX0NGYDnbL5CEyTzhpje0YcRjpucgFmWvA7UTB4MqyhjqgsqLJwEyriNIvz5UkxfgUzrjn3Ajmvt2o/M6KXt2bSEJgP7r9CDt5fL29d4JWTN0XsygvzvCK0okk9TVFTYej2GHFTI= 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)(82310400017)(376005)(1800799015);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 10 May 2024 21:20:13.1545 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 74b7919a-fc21-47e1-2af9-08dc7136fafa 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: PH7PR12MB5903 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 Message-ID: <20240501085210.2213060-20-michael.roth@amd.com> Signed-off-by: Paolo Bonzini --- 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 00d29d278f6e..398266bef2ca 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; @@ -3996,6 +4001,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; @@ -4274,6 +4357,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 555c55f50298..97b3683ea324 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]; }; From patchwork Fri May 10 21:10:24 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Michael Roth X-Patchwork-Id: 13661959 Received: from NAM10-DM6-obe.outbound.protection.outlook.com (mail-dm6nam10on2067.outbound.protection.outlook.com [40.107.93.67]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 9EFF213B5A6; Fri, 10 May 2024 21:20:43 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=40.107.93.67 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1715376045; cv=fail; b=kOQYY+jKeq6tN3QLMJ9kLF15mOznYu36H9T5uXPBJSA3Aba4PkegrFzcEgqnUnX9W+iDeJxkYg00Fxfh4hs1oHbYGWAosFUpZp+1E/AfynJ1xsaz25i9D9VhKmIQOQRuz2QpDYtOAZ4VTrcDBnuXNg/jlpP0Rb+yJVPh4yW9ktU= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1715376045; c=relaxed/simple; bh=QAl8gOPZsQShplxaz0eaDzsWSDH7XmZwbTb8q2vOg24=; h=From:To:CC:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=rkZziiypvpu6Nfih1cESpyD9GAhBWSAiOIL/C8B01vs3U2VG+OPHeh90HlACAIaBWytb4aOk1MxPXejcCcVvx0VBsKUqAZqGp8R3ZFBsI6wkcZCcizUEQGBfNlUWkeTYmtPYp+V09MCdphc2HzleAoaIMOPUlEwBf+FvuaLFDTw= ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=amd.com; spf=fail smtp.mailfrom=amd.com; dkim=pass (1024-bit key) header.d=amd.com header.i=@amd.com header.b=cwhSQxC2; arc=fail smtp.client-ip=40.107.93.67 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=amd.com Authentication-Results: smtp.subspace.kernel.org; spf=fail smtp.mailfrom=amd.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=amd.com header.i=@amd.com header.b="cwhSQxC2" ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=PhVqqTzHAbmel2ThuxmxmnBb71Q8B5PuHI6xC8apFE2fj0JHEMlMLLVRm131TRYtb5JwJaRZvpwVQn53OVjdXMPRmac1cNV9/zQBXeKAaIrMXK7iOoSwcYIbyipyi7Tp62gVJEwasqJKRvCbekoHtSxb4r+f/eo6KobFnlbu5+5U88sWsdMgPdtRCWC65E6JPoBiZk/SmUxIlTl31KRJ/G8q6PTE9C5sEaaZG1D+ipSGXhFerIFG0Vykzw0tFIimn9tcDQHKODJZbMLma5KTq1RHb2AZ3EgDu6Lg3+yGcI0HekkPPvRCxHwry4+cNIBvuk8IrRvxUvQSP9xlDrnhFg== 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=RIMg7qa9m2Dc9naTEuqUQU5elOqtpvpKiHA3BPPrdY8=; b=aukKPq2tRStFz9kyF2RPj6pcWL+7F0O+nTbVb6L3MVrSDVdTjYslcjDbEilGPtQ/iDQeWw80OTdaGBIKpD3yUcs0GntBG61izTJlcUTRANnQyAXk7xMH2eO/C2Cj0Y+kVBmy0ZEzcPe6SZ07yQ0uXZ4qeGfjWEpepw7o+Xr0xQ94NNY8WLDyKvC2kZ4nrundQFGDQ9eornkhkcyMuiaKInKQftfqZYMqWz/4mULRa0RMWnM6jOLB7A/ZChHcdjDI+XNbsrE7nZcgrtEsQRcTzAdUWjl/6A6/FTKA96NYpjjFkbCLQGIk8zULtRIQJX+USQ4MgVxxmpMPi/NqV1O4Ug== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 165.204.84.17) smtp.rcpttodomain=redhat.com 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=RIMg7qa9m2Dc9naTEuqUQU5elOqtpvpKiHA3BPPrdY8=; b=cwhSQxC2aRhqdyfTw3srTcH5BSrL29A25d9EoXDoecMnPRAY8o+ivr7hVoRt/8bx9nMWwGnVqh5++iMRzKJ+2J/NzDOKPMr+7YC1SaHWd5eXvRWDcxcWkvJDiSpY+rHQDNQHZfKdKYBBG6wLSl+Of/RRT3NGtnEzalGmtFRw4JA= Received: from BN9PR03CA0080.namprd03.prod.outlook.com (2603:10b6:408:fc::25) by SN7PR12MB6744.namprd12.prod.outlook.com (2603:10b6:806:26c::13) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7544.46; Fri, 10 May 2024 21:20:32 +0000 Received: from BN1PEPF00004686.namprd03.prod.outlook.com (2603:10b6:408:fc:cafe::2f) by BN9PR03CA0080.outlook.office365.com (2603:10b6:408:fc::25) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7544.48 via Frontend Transport; Fri, 10 May 2024 21:20:32 +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 BN1PEPF00004686.mail.protection.outlook.com (10.167.243.91) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.7544.18 via Frontend Transport; Fri, 10 May 2024 21:20:32 +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; Fri, 10 May 2024 16:20:31 -0500 From: Michael Roth To: Paolo Bonzini CC: , , Sean Christopherson , Tom Lendacky Subject: [PULL 19/19] crypto: ccp: Add the SNP_VLEK_LOAD command Date: Fri, 10 May 2024 16:10:24 -0500 Message-ID: <20240510211024.556136-20-michael.roth@amd.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20240510211024.556136-1-michael.roth@amd.com> References: <20240510211024.556136-1-michael.roth@amd.com> Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 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: BN1PEPF00004686:EE_|SN7PR12MB6744:EE_ X-MS-Office365-Filtering-Correlation-Id: aab848e9-2ffe-48f2-7530-08dc71370660 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0;ARA:13230031|1800799015|376005|36860700004|82310400017; X-Microsoft-Antispam-Message-Info: iw/HmcLXimbimK5dCeCwPtUEAn9jsfXTsdplwgLRpdOyfLIO3JF1OH0A58nGaJdXHbrExEXFzdY2oZg8oKnquvkUfubnjFbjBKCqJSuZpyHIcyI6eR2IoxgDt37b3ROzglXkLOPYU9POX69GIrq3CriBhbbdjXF8QzHCQ2D+XASYnculv/2Z+IDjAzPYy8ocKb4JBBg5xQm9cMOjhvsAwWsdVWdEDyAROmh0AmCHZPoSvcxHPVaEWGV5Mvi5Xkl6MinqdGz0EY6cF42ipfAscQtSxo/6GGIlvDLPXEiOCN0+AL9Ne/Um8iNiH2URQCRjJveKitUR52UnQLcsJHWfqzRkJDT9XopdBEqMGjWQlutSuI/dItDRmW03ZMCb7rjVUlMmYvISY6cyX1DW7RILLWq/vpQxS5y7esWCqPsaN7iOg6X1GOBKOKwrQW1P4SY8EKyqooES8OU2G6wtzEUOPH2eXelWjtD3T1xQCUBG9bgInqoF/ABPcqE5TwhvPB/t2EIi/cvuvJgI9OAsFIRYEjVdYY0CyIFy2qtSVuEugNn/zrvjCyVa0s9YW0OKxk0arm7u2B5+KnClsTaBv3Cvsq2E5TcIGfkAoJYKvH7UKg823bI9xBdNQCCphGdfZ566mPWUs+toOwLkC/xZMDNoyTdVT93mkErp3pcPG0iJL290cUP6sabePzobgFFlXQmO0p65i6yyu8+jddMObN7TPZE0Kh1YVwP4AGEsoORmin/SGgZ9pUYHH65rcmVQSkQdY9tUA9OynJZuP8N4lV95MqKNzubzv4DlS8/OUKek+6uMjkuxHOh5Gk5VUFgQzhrgmLDjYshb9f1Ud2k7vmGK5A44FCfXYAGxFrNFY+W0cHcAdSjqmzGZibZ///tfu7c7/WwuCAvNfn4jPmHyQM8zAWA0P0W7lC3iTtXkrVfV1VW/fIXAa6WtklZpsj8wP7nXMcCrcy7GKG1ykZVQJa0vJR+SUGgnzbdHZDMt7fLa7ccqlrMwzCONbopzF3xbxsYFykS9EguQkga389mNoPKGeqin3LvMu3Tz8qYGQviN9cnaAAO3vSoDF4XzxQ3ec6uYdvCtwCIIrSV/VN735IjcGnuN5bdOd2+vFFmMLHXgAMFV+jHMBv17tTtEL3R5d8Q89nFPJt3Mtt2QoeqDxj719C5qYyGx44a1uatXahUwOOEd/d1rVUEF1ZUhAbAudx67ObF8H6cM4p1Xdp47iEjjuqE/z+tQvepSyRUWfJrDMfIpH6CRzPz+OLSYyQmq9Ry3Qxhgn18NZLEtTTlUCJNZi5qmeNMur+zHn22U0tV9objzvOGOFnhy0D88YHALO+N7eYVmAHqirKz5KyUwLgD/2xaWPjU9Vb9L+T2VF1he5iM11DmsWaOVjGhEacmFWPzY 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)(376005)(36860700004)(82310400017);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 10 May 2024 21:20:32.3423 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: aab848e9-2ffe-48f2-7530-08dc71370660 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: BN1PEPF00004686.namprd03.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: SN7PR12MB6744 When requesting an attestation report a guest is able to specify whether it wants SNP firmware to sign the report using either a Versioned Chip Endorsement Key (VCEK), which is derived from chip-unique secrets, or a Versioned Loaded Endorsement Key (VLEK) which is obtained from an AMD Key Derivation Service (KDS) and derived from seeds allocated to enrolled cloud service providers (CSPs). For VLEK keys, an SNP_VLEK_LOAD SNP firmware command is used to load them into the system after obtaining them from the KDS. Add a corresponding userspace interface so to allow the loading of VLEK keys into the system. See SEV-SNP Firmware ABI 1.54, SNP_VLEK_LOAD for more details. Reviewed-by: Tom Lendacky Signed-off-by: Michael Roth Message-ID: <20240501085210.2213060-21-michael.roth@amd.com> Signed-off-by: Paolo Bonzini --- Documentation/virt/coco/sev-guest.rst | 19 ++++++++++++++ drivers/crypto/ccp/sev-dev.c | 36 +++++++++++++++++++++++++++ include/uapi/linux/psp-sev.h | 27 ++++++++++++++++++++ 3 files changed, 82 insertions(+) diff --git a/Documentation/virt/coco/sev-guest.rst b/Documentation/virt/coco/sev-guest.rst index e1eaf6a830ce..de68d3a4b540 100644 --- a/Documentation/virt/coco/sev-guest.rst +++ b/Documentation/virt/coco/sev-guest.rst @@ -176,6 +176,25 @@ to SNP_CONFIG command defined in the SEV-SNP spec. The current values of the firmware parameters affected by this command can be queried via SNP_PLATFORM_STATUS. +2.7 SNP_VLEK_LOAD +----------------- +:Technology: sev-snp +:Type: hypervisor ioctl cmd +:Parameters (in): struct sev_user_data_snp_vlek_load +:Returns (out): 0 on success, -negative on error + +When requesting an attestation report a guest is able to specify whether +it wants SNP firmware to sign the report using either a Versioned Chip +Endorsement Key (VCEK), which is derived from chip-unique secrets, or a +Versioned Loaded Endorsement Key (VLEK) which is obtained from an AMD +Key Derivation Service (KDS) and derived from seeds allocated to +enrolled cloud service providers. + +In the case of VLEK keys, the SNP_VLEK_LOAD SNP command is used to load +them into the system after obtaining them from the KDS, and corresponds +closely to the SNP_VLEK_LOAD firmware command specified in the SEV-SNP +spec. + 3. SEV-SNP CPUID Enforcement ============================ diff --git a/drivers/crypto/ccp/sev-dev.c b/drivers/crypto/ccp/sev-dev.c index 2102377f727b..97a7959406ee 100644 --- a/drivers/crypto/ccp/sev-dev.c +++ b/drivers/crypto/ccp/sev-dev.c @@ -2027,6 +2027,39 @@ static int sev_ioctl_do_snp_set_config(struct sev_issue_cmd *argp, bool writable return __sev_do_cmd_locked(SEV_CMD_SNP_CONFIG, &config, &argp->error); } +static int sev_ioctl_do_snp_vlek_load(struct sev_issue_cmd *argp, bool writable) +{ + struct sev_device *sev = psp_master->sev_data; + struct sev_user_data_snp_vlek_load input; + void *blob; + int ret; + + if (!sev->snp_initialized || !argp->data) + return -EINVAL; + + if (!writable) + return -EPERM; + + if (copy_from_user(&input, u64_to_user_ptr(argp->data), sizeof(input))) + return -EFAULT; + + if (input.len != sizeof(input) || input.vlek_wrapped_version != 0) + return -EINVAL; + + blob = psp_copy_user_blob(input.vlek_wrapped_address, + sizeof(struct sev_user_data_snp_wrapped_vlek_hashstick)); + if (IS_ERR(blob)) + return PTR_ERR(blob); + + input.vlek_wrapped_address = __psp_pa(blob); + + ret = __sev_do_cmd_locked(SEV_CMD_SNP_VLEK_LOAD, &input, &argp->error); + + kfree(blob); + + return ret; +} + static long sev_ioctl(struct file *file, unsigned int ioctl, unsigned long arg) { void __user *argp = (void __user *)arg; @@ -2087,6 +2120,9 @@ static long sev_ioctl(struct file *file, unsigned int ioctl, unsigned long arg) case SNP_SET_CONFIG: ret = sev_ioctl_do_snp_set_config(&input, writable); break; + case SNP_VLEK_LOAD: + ret = sev_ioctl_do_snp_vlek_load(&input, writable); + break; default: ret = -EINVAL; goto out; diff --git a/include/uapi/linux/psp-sev.h b/include/uapi/linux/psp-sev.h index b7a2c2ee35b7..2289b7c76c59 100644 --- a/include/uapi/linux/psp-sev.h +++ b/include/uapi/linux/psp-sev.h @@ -31,6 +31,7 @@ enum { SNP_PLATFORM_STATUS, SNP_COMMIT, SNP_SET_CONFIG, + SNP_VLEK_LOAD, SEV_MAX, }; @@ -214,6 +215,32 @@ struct sev_user_data_snp_config { __u8 rsvd1[52]; } __packed; +/** + * struct sev_data_snp_vlek_load - SNP_VLEK_LOAD structure + * + * @len: length of the command buffer read by the PSP + * @vlek_wrapped_version: version of wrapped VLEK hashstick (Must be 0h) + * @rsvd: reserved + * @vlek_wrapped_address: address of a wrapped VLEK hashstick + * (struct sev_user_data_snp_wrapped_vlek_hashstick) + */ +struct sev_user_data_snp_vlek_load { + __u32 len; /* In */ + __u8 vlek_wrapped_version; /* In */ + __u8 rsvd[3]; /* In */ + __u64 vlek_wrapped_address; /* In */ +} __packed; + +/** + * struct sev_user_data_snp_vlek_wrapped_vlek_hashstick - Wrapped VLEK data + * + * @data: Opaque data provided by AMD KDS (as described in SEV-SNP Firmware ABI + * 1.54, SNP_VLEK_LOAD) + */ +struct sev_user_data_snp_wrapped_vlek_hashstick { + __u8 data[432]; /* In */ +} __packed; + /** * struct sev_issue_cmd - SEV ioctl parameters *