From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from NAM10-MW2-obe.outbound.protection.outlook.com (NAM10-MW2-obe.outbound.protection.outlook.com [40.107.94.49]) by mx.groups.io with SMTP id smtpd.web11.861.1631557222462732596 for ; Mon, 13 Sep 2021 11:20:23 -0700 Authentication-Results: mx.groups.io; dkim=fail reason="body hash did not verify" header.i=@amd.com header.s=selector1 header.b=DJGFB6Gd; spf=permerror, err=parse error for token &{10 18 %{i}._ip.%{h}._ehlo.%{d}._spf.vali.email}: invalid domain name (domain: amd.com, ip: 40.107.94.49, mailfrom: brijesh.singh@amd.com) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=XMkwNlwKZLChcDzSgGJY1SHU/CkY5KPpVNaK8b8oLVk1snqQ/zvRXYC+uOkBPGQH+wE9kTAFHhV/ex+RavHTzwH0YnaJpkD6zULB2B7DRcWufTNY882+U+VhurXg7kmC9FpPPUosPbFqryzpI51j2yxg0jTh7/v4nuAG/v4NsB8QwnjcRTzytJSNQTXi6KNAZEvFkfP7DjW2w77/ICWJ59PeiOEKYU75kWv8pURniCrjIRofPVELGh53FTiwjJKaDHq1ophkZI6uM6aDpyMcrCsyAZRF0UPwP17eYaupwSd5R1nSJ1LRu4eGyS4nwMvfbkmFnz04b5DPdoXmC76ivw== 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; bh=+eVMhvvLwWwbEq/R0XntdiqkpFr2+d6VcbLQLl/qvdw=; b=c1fbOGnGeMLk5Le0UfCXd0xn1gQD6VRg79AbLTefx9tmJLkkLz4J7YqG5Z9HujPGOjvaZ44DsM5EOIdUiRoPbZbdHUfHXccEBuS0vhknA8YsdjImj/FjCUkxmtLcOJp8TRrjw2CgorwwwjXuAPQVSRd4lcdxT7nmmeiQMsuhLZ6bWq9TD/0PclapBsfFymvLEmPGURhtO5n3PnZYdtxFW2z11j4Gib1NFcf0ASFbpmrAYwYxASQYSz5QtqUq91i0XddvMsn9UV6mLQPLK1AJ6PfJ9tSlDZTKpcCbmgsYyMWytAmqDj+BBJkjnrVFqj7VzMfo5sIgBVIaTW78ynyjPQ== 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=+eVMhvvLwWwbEq/R0XntdiqkpFr2+d6VcbLQLl/qvdw=; b=DJGFB6GdVa/eWnu3JgceGOy7mQw7uON2NvtgqUcD1KjouXq8KxVd1hTE+MGjMYgf1zU0EXI2jXPdXeApNg8/A9X4+y72sb0+izOzfzmFP4LYrbwhAiClpEe2Twcs12v8WIq30k5mCGFKzFBoUUhq75YV3nj4On7TosHX8sFqrTw= 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 SN6PR12MB2830.namprd12.prod.outlook.com (2603:10b6:805:e0::27) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4500.14; Mon, 13 Sep 2021 18:20:21 +0000 Received: from SN6PR12MB2718.namprd12.prod.outlook.com ([fe80::78b7:7336:d363:9be3]) by SN6PR12MB2718.namprd12.prod.outlook.com ([fe80::78b7:7336:d363:9be3%6]) with mapi id 15.20.4500.019; Mon, 13 Sep 2021 18:20:21 +0000 From: "Brijesh Singh" To: devel@edk2.groups.io CC: James Bottomley , Min Xu , Jiewen Yao , Tom Lendacky , Jordan Justen , Ard Biesheuvel , Erdem Aktas , Michael Roth , Gerd Hoffmann , Brijesh Singh , Michael Roth Subject: [PATCH v7 27/31] OvmfPkg/MemEncryptSevLib: change the page state in the RMP table Date: Mon, 13 Sep 2021 13:19:37 -0500 Message-ID: <20210913181941.23405-28-brijesh.singh@amd.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20210913181941.23405-1-brijesh.singh@amd.com> References: <20210913181941.23405-1-brijesh.singh@amd.com> X-ClientProxiedBy: SA9P221CA0012.NAMP221.PROD.OUTLOOK.COM (2603:10b6:806:25::17) To SN6PR12MB2718.namprd12.prod.outlook.com (2603:10b6:805:6f::22) Return-Path: brijesh.singh@amd.com MIME-Version: 1.0 Received: from sbrijesh-desktop.amd.com (165.204.77.1) by SA9P221CA0012.NAMP221.PROD.OUTLOOK.COM (2603:10b6:806:25::17) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4500.14 via Frontend Transport; Mon, 13 Sep 2021 18:20:19 +0000 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: 9ed2b22e-0d2e-421c-8d94-08d976e32515 X-MS-TrafficTypeDiagnostic: SN6PR12MB2830: X-MS-Exchange-Transport-Forked: True X-Microsoft-Antispam-PRVS: X-MS-Oob-TLC-OOBClassifiers: OLM:8273; X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: FCkPqnj3HE2/whu98kb9Ho4EMjBcshoRNCGdNSbURbgwg+meo73nB0r0fz5X322/qgbvPiWOxICdfZUvHy/pD+83B5lTSr7J5TeIQ6TIgfMhHVsDQYGptG7hrIqfAYYGunRUtVojz3KwjTDdrrdZL44p+1bHnS8mkBYGnQkDAjam8Jcnf/bZ9muYqtEKbIAIYCHarCoEnBEQLSWPkPqDEdSVXUBk8X489CF4iZDsPjzil1iI3wviPEBr4IYJpKUw6DP+bvKXejxPKA4IMSBnnrIxq7pnhAg5bof2bAZbAyinkzwSB9af7h9RPqMht+5jIndbT3kKWI21Aig5IC+8gyxrvVO1uFU1tGM9Wt4PX/0mrsIwo3XmRXyI1koowb9JBRm8JTF6jqLzBDmRMQCU7WTiv5J0Kq0diCwNX0GglRDBKwNLC0NorFP5ASu0dEUGDw+I06zik3RjFHCF94Lkm2aOCl86pfckcvYXaDkvPvTm/XP+i159DdlPeyPIlAmCq17CY6odhKQQvOIeix4oiNrc7c51v22pib8pN0bBeuRL0MPYZ4JAjl1eGN/YjPfZgWp0hwb3DXx3FngvRybD7lJh+fbsgVkKcCaP0j/cJIg/F+pfhhfepSnsNX6s3KgG/oK/oXBxclIcRklpvGnGyhkzcCEGxmFO5nwFENuVm41CdwZnW/J44b6Y3c4ssCcRR6n2Dcl1itudGplu15Ghc3YrXHmUrfUAUCn+Aho+A3ZNibKHcB6eIvPmM0JsdRZx6rApCRE4LP5exG7JvewwBA== 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)(346002)(39860400002)(366004)(136003)(396003)(376002)(38100700002)(38350700002)(54906003)(4326008)(1076003)(186003)(6916009)(66476007)(956004)(19627235002)(2906002)(6666004)(66946007)(66556008)(2616005)(8936002)(83380400001)(478600001)(966005)(44832011)(8676002)(5660300002)(26005)(86362001)(316002)(6486002)(36756003)(52116002)(7696005);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: =?us-ascii?Q?pqZBDDvZpyC7Mwu2yKwXtEO0/4j3ZOH0A4mEapsSswJOW6Gjr9oT07PC4k07?= =?us-ascii?Q?a0AIuDE7rQHyoRV39KtbPT5c8gBAwr4W3zltSrUMUNPWf4W964E3h3fXnvcE?= =?us-ascii?Q?u7uOESBHMHHzsdjIsqLpoNCteGbgdMrWWmfH8hjejkcjDqAsixSw2tQ1shxa?= =?us-ascii?Q?bCywbZ3CehFj8gMS/Mpp6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ii?Q?bevycAbCgnx1Y11Ct+c8LMFM5bgtdkAIaLmtH/c3UVXk3jF+bO7LgIUEH+KO?= =?us-ascii?Q?9AmMnCu/xH67ARdm0HX90mTqW5HtdFPWEz6LD6+VCXCTWaXE2gbe6uyyjNhC?= =?us-ascii?Q?Zm1HFs5q0XurNvd6Ag3kQ32/3QrTjNO+KpSlKEaynH+rTnfFovwHIUYU0LWo?= =?us-ascii?Q?RgIoAfSOQkhDF7AKjATE9tKhSFlMf31KUbUIHuhG6dhII3IZpJxZ1BcfjQg+?= =?us-ascii?Q?fGnmGuEVz9PluCyATua2IOi/?= X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-Network-Message-Id: 9ed2b22e-0d2e-421c-8d94-08d976e32515 X-MS-Exchange-CrossTenant-AuthSource: SN6PR12MB2718.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 13 Sep 2021 18:20:20.3328 (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: 2sBxlKQXwzI+F3b2TobwwS8tptk6PxXh/FiCz5cq7bvaGlP+T2YNhjW7wFQJP3bxbm0J8vk/mMIk+iZh/YLKTA== X-MS-Exchange-Transport-CrossTenantHeadersStamped: SN6PR12MB2830 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain BZ: https://bugzilla.tianocore.org/show_bug.cgi?id=3D3275 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 change the page state in the RMP table so that it is in sync with the memory encryption attribute change. Cc: Michael Roth Cc: James Bottomley Cc: Min Xu Cc: Jiewen Yao Cc: Tom Lendacky Cc: Jordan Justen Cc: Ard Biesheuvel Cc: Erdem Aktas Cc: Gerd Hoffmann Acked-by: Jiewen Yao Signed-off-by: Brijesh Singh --- .../X64/PeiDxeVirtualMemory.c | 34 +++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/OvmfPkg/Library/BaseMemEncryptSevLib/X64/PeiDxeVirtualMemory.c= b/OvmfPkg/Library/BaseMemEncryptSevLib/X64/PeiDxeVirtualMemory.c index f146f6d61cc5..56db1e4b6ecf 100644 --- a/OvmfPkg/Library/BaseMemEncryptSevLib/X64/PeiDxeVirtualMemory.c +++ b/OvmfPkg/Library/BaseMemEncryptSevLib/X64/PeiDxeVirtualMemory.c @@ -17,6 +17,7 @@ #include =20 #include "VirtualMemory.h" +#include "SnpPageStateChange.h" =20 STATIC BOOLEAN mAddressEncMaskChecked =3D FALSE; STATIC UINT64 mAddressEncMask; @@ -695,10 +696,12 @@ SetMemoryEncDec ( PAGE_MAP_AND_DIRECTORY_POINTER *PageDirectoryPointerEntry; PAGE_TABLE_1G_ENTRY *PageDirectory1GEntry; PAGE_TABLE_ENTRY *PageDirectory2MEntry; + PHYSICAL_ADDRESS OrigPhysicalAddress; PAGE_TABLE_4K_ENTRY *PageTableEntry; UINT64 PgTableMask; UINT64 AddressEncMask; BOOLEAN IsWpEnabled; + UINTN OrigLength; RETURN_STATUS Status; =20 // @@ -751,6 +754,22 @@ SetMemoryEncDec ( =20 Status =3D EFI_SUCCESS; =20 + // + // To maintain the security gurantees we must set the page to shared in = the RMP + // table before clearing the memory encryption mask from the current pag= e table. + // + // The InternalSetPageState() is used for setting the page state in the = RMP table. + // + if ((Mode =3D=3D ClearCBit) && MemEncryptSevSnpIsEnabled ()) { + InternalSetPageState (PhysicalAddress, EFI_SIZE_TO_PAGES (Length), Sev= SnpPageShared, FALSE); + } + + // + // Save the specified length and physical address (we need it later). + // + OrigLength =3D Length; + OrigPhysicalAddress =3D PhysicalAddress; + while (Length !=3D 0) { // @@ -923,6 +942,21 @@ SetMemoryEncDec ( // CpuFlushTlb(); =20 + // + // SEV-SNP requires that all the private pages (i.e pages mapped encrypt= ed) must be + // added in the RMP table before the access. + // + // The InternalSetPageState() is used for setting the page state in the = RMP table. + // + if ((Mode =3D=3D SetCBit) && MemEncryptSevSnpIsEnabled ()) { + InternalSetPageState ( + OrigPhysicalAddress, + EFI_SIZE_TO_PAGES (OrigLength), + SevSnpPagePrivate, + FALSE + ); + } + Done: // // Restore page table write protection, if any. --=20 2.17.1