From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from NAM04-DM6-obe.outbound.protection.outlook.com (NAM04-DM6-obe.outbound.protection.outlook.com []) by mx.groups.io with SMTP id smtpd.web08.27.1616599961841997394 for ; Wed, 24 Mar 2021 08:32:42 -0700 Authentication-Results: mx.groups.io; dkim=pass header.i=@amd.com header.s=selector1 header.b=s4Nq9o3L; spf=permerror, err=parse error for token &{10 18 %{i}._ip.%{h}._ehlo.%{d}._spf.vali.email}: invalid domain name (domain: amd.com, ip: , mailfrom: brijesh.singh@amd.com) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=h3ijvSav+0K/VqcyHZ78eMY99axw1Q8vZQKGeAB45xLPMWHYpzTkhSmI56OTbSGYqZG7dNxtKXmpfbOsBtd7wUULM4MwYW2EidMfdN6h1rSelvrvMWcokQg/8xybITnHHIncIYq+7B8iLVrGHZ8fUvuj66rjyaKlk4/VS7THDR2jbK4Gd7+yfJIgTyMV2x5hADGOdugungOgxRjUUtdjITy3FHMirKDaTTJMLHsRezXLfUPQwHI3NnWEP8qOKSKVzBgM+PItzWYveSuy4vX1PTAMW56ZeIpsB7LfBMuD9tMVhqMpEsMCngPbxTYfBPovLogG8pjBDpTYbj8d7RgcWA== 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-SenderADCheck; bh=6F6xqtUvD5aKBKTRD49fjpHIm7MAlZx0ZNQOr9/UsSY=; b=hwx4Kkna4QjEhxP0YMOm5K3MQ13kKuyhEvob7RYd5h784GnMYJ2dYBgxCjGNgD7ZlLTslsgSIqau4KzSEXRgIbLtdqI1d+v/V7MiEzDirhAwgwFwJ9KrYGI+AMxF1iYOpbfE3FExSdPDBbxCfsnu6f3ve5JA9DMHVEvENBpFpxj2xZlZgNwXRJDuj+oZfcusSCMcI4IyMnG6csc2TYz8ytkN1nelhA1zIoL7oH43JoilOACVcCkOw3MVUBnsuwQ6U14ocTVLAGfsfTVv8F3w9P40cYemEUV1FNcwKiYmVw3Juma1AxUHuA5llI8CFiX0lBiH+0hkxjceraEiqVzYzA== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=amd.com; dmarc=pass action=none header.from=amd.com; dkim=pass header.d=amd.com; arc=none 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=6F6xqtUvD5aKBKTRD49fjpHIm7MAlZx0ZNQOr9/UsSY=; b=s4Nq9o3LcH5wiYpPMbHPZWgRb7bYHHAxlRZV3i7/JCc4VWhCpfNcax/qV9cBqr5aKnOLTcztDv5dbPczWlbEv8PMKixPgP7Mz2HU8MY7mMdEuYSG5SLZIQTCkjeTioTIzfrdqq9PND9RI5dvSo1lFbmPSCS/B5V5py3CMoVFLLY= Authentication-Results: edk2.groups.io; dkim=none (message not signed) header.d=none;edk2.groups.io; dmarc=none action=none header.from=amd.com; Received: from SN6PR12MB2718.namprd12.prod.outlook.com (2603:10b6:805:6f::22) by SA0PR12MB4430.namprd12.prod.outlook.com (2603:10b6:806:70::20) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.3955.18; Wed, 24 Mar 2021 15:32:40 +0000 Received: from SN6PR12MB2718.namprd12.prod.outlook.com ([fe80::30fb:2d6c:a0bf:2f1d]) by SN6PR12MB2718.namprd12.prod.outlook.com ([fe80::30fb:2d6c:a0bf:2f1d%3]) with mapi id 15.20.3955.027; Wed, 24 Mar 2021 15:32:40 +0000 From: "Brijesh Singh" To: devel@edk2.groups.io Cc: Brijesh Singh , James Bottomley , Min Xu , Jiewen Yao , Tom Lendacky , Jordan Justen , Ard Biesheuvel , Laszlo Ersek Subject: [RFC PATCH 18/19] OvmfPkg/MemEncryptSevLib: Validate the memory during set or clear enc attribute Date: Wed, 24 Mar 2021 10:32:14 -0500 Message-Id: <20210324153215.17971-19-brijesh.singh@amd.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20210324153215.17971-1-brijesh.singh@amd.com> References: <20210324153215.17971-1-brijesh.singh@amd.com> X-Originating-IP: [165.204.77.1] X-ClientProxiedBy: SA9PR11CA0006.namprd11.prod.outlook.com (2603:10b6:806:6e::11) To SN6PR12MB2718.namprd12.prod.outlook.com (2603:10b6:805:6f::22) Return-Path: brijesh.singh@amd.com MIME-Version: 1.0 X-MS-Exchange-MessageSentRepresentingType: 1 Received: from sbrijesh-desktop.amd.com (165.204.77.1) by SA9PR11CA0006.namprd11.prod.outlook.com (2603:10b6:806:6e::11) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.3955.18 via Frontend Transport; Wed, 24 Mar 2021 15:32:40 +0000 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-HT: Tenant X-MS-Office365-Filtering-Correlation-Id: 8d851e7b-abdf-48eb-91be-08d8eeda0f8c X-MS-TrafficTypeDiagnostic: SA0PR12MB4430: X-MS-Exchange-Transport-Forked: True X-Microsoft-Antispam-PRVS: X-MS-Oob-TLC-OOBClassifiers: OLM:7219; X-MS-Exchange-SenderADCheck: 1 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: Y+7bvsY9YkVcWnxO4RcHoAH+JGvP6RQvEn0vWPmHK9cjVifC8Oxx+eTO9e7qRWjJX+wWqzsXkYfe9RE7Wk3BEF7RczOMbZ2v/wb9C4Eei1dysNe1DaL5VUZpkHkGmAdYqzqSjbcrEgZSaqZpq8ZegiNNBUbMg7jILcLe1u0Oy+7McZp13/W4Y41JxuQdYrOh+FAOMRSNEmhqMu1ALw34Vub89QXpyzGK7ZqZMn5sywVXkK62K6Dn889sdu1svDIaDJz572aIUkKitAEMggLj6vfX3hO99IC3GqzEXQes2F69tCpBnekpEnbNFWEPGOA2d+Tsl7O8d6kleyQQh+A1lHQS7L9W8J+42J7+5QjqWLv63ZGZxwmCFodMhLqwSkELSeCKY+BNZZbtb6THiZv0oinT0VKawBuucFVUFqyKKdjy12JFdbWP38biZF7MTUqf4kt+LM9Y+aNav8uzZCDOEfiiHqz8y2KzuYHA5PIskUCJ5hVBwupsAHNX8cMH46vmt5all/vuMLalX/DOh8xNJglgeyk7aR+7CovG8jaPB0/V7yhjX6pjD+QTjjl/PM6WLxhgm02MGMJk81L1h8HinyIUS4U5NUjpE0veUlws2+2go8rPyDHf4EYQGbPkQi20FF1YhrrFj3x/ST6KBeQeVXwy7ws/YXhoB4Sj8gpTVCCPpKoo551iypu8PIulUidcj/hbSaudM1iAkojzf/kxVLLjHGpChnoRlsH02nRdMACsdLME4E4MAWVL1cQcNbGJ X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:SN6PR12MB2718.namprd12.prod.outlook.com;PTR:;CAT:NONE;SFS:(4636009)(366004)(39860400002)(136003)(376002)(346002)(396003)(2616005)(83380400001)(66476007)(15650500001)(4326008)(44832011)(1076003)(6486002)(2906002)(86362001)(66946007)(478600001)(316002)(66556008)(966005)(16526019)(6666004)(26005)(54906003)(19627235002)(6916009)(186003)(5660300002)(8676002)(7696005)(956004)(8936002)(38100700001)(36756003)(52116002);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData: =?us-ascii?Q?psFdAIy25mNFDKjv4O/7vpelSMsm0AufoXxFXN2m0WVsG1JXUUUIXTMmhrt7?= =?us-ascii?Q?WErAWP/ilJkNIFW1frUVbiHI1IlzrM9Ne65+Y643M8DOtANU+Ro+lG8CZydX?= =?us-ascii?Q?2RKmQCqIn6mf6X87tE2MVFiPt0uOCPLKahzzjpgTsJnQr/byMlY+PEJpckw1?= =?us-ascii?Q?5eyy3p0P9obko5OVCN0nIxN+GMbwzE2DEKzBS36oijoQev//wW22r9JLMsq/?=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n/m8NLHUasm2M8ioDXQ29TrUVeYqZDHZ7hhHKHeLSKZ?= =?us-ascii?Q?RVVh0T828f+xxBjNvM44v+fWvctcJ7+9CLshAntMkkPDhpQB0PHcU5MXecoi?= =?us-ascii?Q?i8bQDo/i9tbho0Pnn7MvV+3EOhxtuYRb0tBqPGJi78DcyAETaz+G3K1QxIu4?= =?us-ascii?Q?Kd8wXNLDm8kFnFRWVRF0ZJcJ9o2s3mXf7pKfw1jhsunvL7NOEuagUm8z16ww?= =?us-ascii?Q?HyRfvRgpXxmkvHBfeOLqeJ44?= X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-Network-Message-Id: 8d851e7b-abdf-48eb-91be-08d8eeda0f8c X-MS-Exchange-CrossTenant-AuthSource: SN6PR12MB2718.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 24 Mar 2021 15:32:40.6085 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 3dd8961f-e488-4e60-8e11-a82d994e183d X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: DY3bzdvpdAkYEF7LqK1ZfP38BNGKa19UuOF1mCIapFsOCdRDFiwDML4ezVMxwoFgT4yT+xzj+R+hQNcVkw6P/w== X-MS-Exchange-Transport-CrossTenantHeadersStamped: SA0PR12MB4430 Content-Type: text/plain BZ: https://bugzilla.tianocore.org/show_bug.cgi?id=3275 The MemEncryptSev{Set,Clear}PageEncMask() functions are used to set or clear the memory encryption attribute in the page table. When SEV-SNP is active, we also need to validate or invalidate the pages and update the RMP entry. Before clearing the encryption attribute we need to invalidate the memory, and then make the page shared in the RMP entry. Similarly, after setting the encryption attribute in the page table, we add the memory as private in the RMP entry and validate it. Cc: James Bottomley Cc: Min Xu Cc: Jiewen Yao Cc: Tom Lendacky Cc: Jordan Justen Cc: Ard Biesheuvel Cc: Laszlo Ersek Signed-off-by: Brijesh Singh --- OvmfPkg/Library/BaseMemEncryptSevLib/DxeMemEncryptSevLib.inf | 3 ++ OvmfPkg/Library/BaseMemEncryptSevLib/PeiMemEncryptSevLib.inf | 1 + OvmfPkg/Library/BaseMemEncryptSevLib/X64/PeiDxeSnpSetPageState.c | 32 +++++++++++++++++ OvmfPkg/Library/BaseMemEncryptSevLib/X64/PeiDxeVirtualMemory.c | 36 ++++++++++++++++++-- OvmfPkg/Library/BaseMemEncryptSevLib/X64/SnpSetPageState.h | 27 +++++++++++++++ 5 files changed, 97 insertions(+), 2 deletions(-) diff --git a/OvmfPkg/Library/BaseMemEncryptSevLib/DxeMemEncryptSevLib.inf b/OvmfPkg/Library/BaseMemEncryptSevLib/DxeMemEncryptSevLib.inf index f2e162d680..fa8f7719a7 100644 --- a/OvmfPkg/Library/BaseMemEncryptSevLib/DxeMemEncryptSevLib.inf +++ b/OvmfPkg/Library/BaseMemEncryptSevLib/DxeMemEncryptSevLib.inf @@ -36,6 +36,8 @@ [Sources.X64] X64/MemEncryptSevLib.c X64/PeiDxeVirtualMemory.c + X64/SnpPageStateChangeInternal.c + X64/PeiDxeSnpSetPageState.c X64/VirtualMemory.c X64/VirtualMemory.h @@ -49,6 +51,7 @@ DebugLib MemoryAllocationLib PcdLib + VmgExitLib [FeaturePcd] gUefiOvmfPkgTokenSpaceGuid.PcdSmmSmramRequire diff --git a/OvmfPkg/Library/BaseMemEncryptSevLib/PeiMemEncryptSevLib.inf b/OvmfPkg/Library/BaseMemEncryptSevLib/PeiMemEncryptSevLib.inf index cb9dd2bb21..d16ec44954 100644 --- a/OvmfPkg/Library/BaseMemEncryptSevLib/PeiMemEncryptSevLib.inf +++ b/OvmfPkg/Library/BaseMemEncryptSevLib/PeiMemEncryptSevLib.inf @@ -37,6 +37,7 @@ [Sources.X64] X64/MemEncryptSevLib.c X64/PeiDxeVirtualMemory.c + X64/PeiDxeSnpSetPageState.c X64/PeiSnpSystemRamValidate.c X64/SnpPageStateTrack.c X64/SnpPageStateChangeInternal.c diff --git a/OvmfPkg/Library/BaseMemEncryptSevLib/X64/PeiDxeSnpSetPageState.c b/OvmfPkg/Library/BaseMemEncryptSevLib/X64/PeiDxeSnpSetPageState.c new file mode 100644 index 0000000000..0a3d58ac22 --- /dev/null +++ b/OvmfPkg/Library/BaseMemEncryptSevLib/X64/PeiDxeSnpSetPageState.c @@ -0,0 +1,32 @@ +/** @file + + SEV-SNP Page Validation functions. + + Copyright (c) 2020 - 2021, AMD Incorporated. All rights reserved.
+ + SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#include +#include + +#include "PeiSnpPageStateChange.h" + +VOID +SnpSetMemoryPrivate ( + IN PHYSICAL_ADDRESS PhysicalAddress, + IN UINTN Length + ) +{ + SetPageStateInternal (PhysicalAddress, EFI_SIZE_TO_PAGES (Length), SevSnpPagePrivate, FALSE); +} + +VOID +SnpSetMemoryShared ( + IN PHYSICAL_ADDRESS PhysicalAddress, + IN UINTN Length + ) +{ + SetPageStateInternal (PhysicalAddress, EFI_SIZE_TO_PAGES (Length), SevSnpPageShared, FALSE); +} diff --git a/OvmfPkg/Library/BaseMemEncryptSevLib/X64/PeiDxeVirtualMemory.c b/OvmfPkg/Library/BaseMemEncryptSevLib/X64/PeiDxeVirtualMemory.c index 33d9bafe9f..26d363d427 100644 --- a/OvmfPkg/Library/BaseMemEncryptSevLib/X64/PeiDxeVirtualMemory.c +++ b/OvmfPkg/Library/BaseMemEncryptSevLib/X64/PeiDxeVirtualMemory.c @@ -17,6 +17,7 @@ #include #include "VirtualMemory.h" +#include "SnpSetPageState.h" STATIC BOOLEAN mAddressEncMaskChecked = FALSE; STATIC UINT64 mAddressEncMask; @@ -700,22 +701,34 @@ SetMemoryEncDec ( UINT64 AddressEncMask; BOOLEAN IsWpEnabled; RETURN_STATUS Status; + BOOLEAN NeedPageStateChange; + PHYSICAL_ADDRESS OrigPhysicalAddress; + UINTN OrigLength; // // Set PageMapLevel4Entry to suppress incorrect compiler/analyzer warnings. // PageMapLevel4Entry = NULL; + // + // When SEV-SNP is active, before clearing the encryption attribute from + // the page table we also need to update the RMP entry for the memory + // region to make the region shared. And after setting the encryption + // attribute, the region must be made private in the RMP table. + // + NeedPageStateChange = MemEncryptSevSnpIsEnabled (); + DEBUG (( DEBUG_VERBOSE, - "%a:%a: Cr3Base=0x%Lx Physical=0x%Lx Length=0x%Lx Mode=%a CacheFlush=%u\n", + "%a:%a: Cr3Base=0x%Lx Physical=0x%Lx Length=0x%Lx Mode=%a CacheFlush=%u Rmpupdate=%u\n", gEfiCallerBaseName, __FUNCTION__, Cr3BaseAddress, PhysicalAddress, (UINT64)Length, (Mode == SetCBit) ? "Encrypt" : "Decrypt", - (UINT32)CacheFlush + (UINT32)CacheFlush, + (UINT32)NeedPageStateChange )); // @@ -749,6 +762,18 @@ SetMemoryEncDec ( DisableReadOnlyPageWriteProtect (); } + // + // Make the RMP updates before clearing the encryption attribute in the page table. + // + if (NeedPageStateChange && (Mode == ClearCBit)) { + SnpSetMemoryShared (PhysicalAddress, Length); + } + + // + // Save the values, we need it later during the Page state change. + // + OrigPhysicalAddress = PhysicalAddress; + OrigLength = Length; Status = EFI_SUCCESS; while (Length != 0) @@ -923,6 +948,13 @@ SetMemoryEncDec ( // CpuFlushTlb(); + // + // Make the RMP updates after setting the encryption attribute in the page table. + // + if (NeedPageStateChange && (Mode == SetCBit)) { + SnpSetMemoryPrivate (OrigPhysicalAddress, OrigLength); + } + Done: // // Restore page table write protection, if any. diff --git a/OvmfPkg/Library/BaseMemEncryptSevLib/X64/SnpSetPageState.h b/OvmfPkg/Library/BaseMemEncryptSevLib/X64/SnpSetPageState.h new file mode 100644 index 0000000000..0b29bad612 --- /dev/null +++ b/OvmfPkg/Library/BaseMemEncryptSevLib/X64/SnpSetPageState.h @@ -0,0 +1,27 @@ +/** @file + + SEV-SNP Page Validation functions. + + Copyright (c) 2020 - 2021, AMD Incorporated. All rights reserved.
+ + SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#ifndef PEI_DXE_SNP_PAGE_STATE_INTERNAL_H_ +#define PEI_DXE_SNP_PAGE_STATE_INTERNAL_H_ + +#include "../SnpPageStateChange.h" + +VOID +SnpSetMemoryPrivate ( + IN PHYSICAL_ADDRESS PhysicalAddress, + IN UINTN Length + ); + +VOID +SnpSetMemoryShared ( + IN PHYSICAL_ADDRESS PhysicalAddress, + IN UINTN Length + ); +#endif -- 2.17.1