From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from NAM04-MW2-obe.outbound.protection.outlook.com (NAM04-MW2-obe.outbound.protection.outlook.com [40.107.101.84]) by mx.groups.io with SMTP id smtpd.web08.1330.1632163593166794776 for ; Mon, 20 Sep 2021 11:46:34 -0700 Authentication-Results: mx.groups.io; dkim=fail reason="body hash did not verify" header.i=@amd.com header.s=selector1 header.b=yTyuWzDo; 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.101.84, mailfrom: brijesh.singh@amd.com) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=O6WrIZ3u5yOVEFqMEjOISZiFPq9KR2zUUtH1Ikc96ssFUMZ++LmDnv4vOtKGFADs+AsbF4SOpFflcRYqpuVjPCcJL0MpTOd7/DScnQYFA3FM4PNWKTXYvC6cIVXD5Lb7+j1EuWRDiVyea8cggnKuxqkfpwKgMga9AgNqjwaVizo1sUiY9oM/tdxBg6gel8iDZAc+MlUcKfs/fmD2020dI/XjRxUMJsp/EdFr8hA7JK44+76oNN1bPIdl8lY9dior2BIlaNh41Jl2uCEAYNFi03fi2t9ipWhP6sPtvKdGaQleJw8GYFqrQ9XxhJuzmLLytIv8Q4R06mYYl6/X2yYACw== 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=K3VIIQSIJOMLaVtWWurntkLkZQP2vS9rXGJJFdhze1s=; b=d5tlSiGS2UbyTSOlTeY99tbBuMn86OcDUIWorDheAt6y64uRE6eADkv3f5obW4N1nvku2ZJ5C/qmjupXngV1m5Qm1DP6j2feYzwmdCxzIatkwpV8KCOQNkIEVc6GKGGLq2uqPrnrC3yGEirEY/jSaTTNzLRlzqyt4Fn7e4ZSdyBN4ksYQTYi/5MaVsbQamjdeczXLEaHzAT0y7waNe4rtf6gDJTPZIPI9ZDjm5Lx+Hz7pB9WZ+deEFJNDiaweZJT+AB6t0Zo7lxK/+wyeA7DQ0oWyGACE+53jzvHlyH6YN6/8/NJwqbs89tkS41kqksPGpx3Nx515ehaqcl+Mzg0FA== 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=K3VIIQSIJOMLaVtWWurntkLkZQP2vS9rXGJJFdhze1s=; b=yTyuWzDoWfLWPR5SKIvtjTvSADTx+uKIfllKoqA0ZDnKBmeDk/MhQwEhRUIGT7us94H7ArwipEagpNFYzduPg3vpuRSMMW2fUAxs6QwgkO21PySg0nRYSUOrRg1JWEnkG46Z12aleZci9GYGq3vUHaZ1KtG6/yhFygHHXzipN/E= 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 SN6PR12MB2768.namprd12.prod.outlook.com (2603:10b6:805:72::11) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4523.14; Mon, 20 Sep 2021 18:46:32 +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.4523.018; Mon, 20 Sep 2021 18:46:32 +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 , Jiewen Yao Subject: [PATCH v8 17/32] OvmfPkg/MemEncryptSevLib: add support to validate > 4GB memory in PEI phase Date: Mon, 20 Sep 2021 13:45:49 -0500 Message-ID: <20210920184604.31590-18-brijesh.singh@amd.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20210920184604.31590-1-brijesh.singh@amd.com> References: <20210920184604.31590-1-brijesh.singh@amd.com> X-ClientProxiedBy: SN4PR0201CA0034.namprd02.prod.outlook.com (2603:10b6:803:2e::20) 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 SN4PR0201CA0034.namprd02.prod.outlook.com (2603:10b6:803:2e::20) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4523.14 via Frontend Transport; Mon, 20 Sep 2021 18:46:32 +0000 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: fa0db08b-81ae-415f-bf1b-08d97c66f712 X-MS-TrafficTypeDiagnostic: SN6PR12MB2768: 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: IRi9Bq0UYJDY/1GFof7wFIgiKmlk4FSMqg+6d5re5IjdllB6Y9G6qOlNN8UGeKefzegABHW3yiZzUGrHCgghdSxeobAzgyleiGdQ3ta/yKNqrhm1SSJJgIBEYgSon1CAdxnmWchYbJEBCYWdvdkl3XqgENK8NFR9EmoHzz5TTvt5wvFOjD0fJMtSrDqdIoS2ZIJSkZR8o5TWlzxeUpNHOtWwTCVzpGpLH9MpkdMevgJGPiuSQ6/Fxkn98zs0qy18FHjCKsUQp+Zp2g6BmN5IFOAfq6yliQ/9wLc0kLndQLcb5n9FrT22iOJO+2kLL22K/9Y9Tma2SgnLFB3bBigjFmy1WIlP9h63VNgfV6PAGWn6Yhg5tfQPr7dFODL0Vz91LXUTvAFnQ4IN42eQfXiyDtHcU4a74jHVCkDjTeR8LEjzmVd/khl8snTTK+/ZY6yprLhf2OdcvjbE4UswJwPTFPwSSRSn3sqiQJzPW7kLZ0TzzSb58cPKoGbR8rpUIvExrUjdcPWidw16d8uK8hiIj3TAOf8mTvnGX+hcfLDjB1yC8hHuZ0rEsGyN24Z6UE5nvEAVkSbdS1T9uX7WEuuuiIyGdcyXJINbB4QZ7z0FBqAU1R5vmdKbI+6Jx8ceYLJ2UFgrJGnU7Z2ZU8F149yTdhOGOnC0SDXcPN9rlRiaKYBPSn3ztVMzXd4/CotrrNgsQ3JXGhfYQ7T8eRWGTHiilUE186FQc9tbxNPBO4ILQJxF0bRXor57MlWG4f4YryvLTCLogpO90KhsBb9OuDRWEg== 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)(376002)(366004)(136003)(39860400002)(396003)(6916009)(186003)(86362001)(2616005)(956004)(6486002)(19627235002)(6666004)(44832011)(15650500001)(26005)(38350700002)(5660300002)(8676002)(36756003)(478600001)(66946007)(1076003)(7696005)(54906003)(2906002)(316002)(966005)(66476007)(66556008)(4326008)(8936002)(38100700002)(52116002)(83380400001);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: =?us-ascii?Q?UWnvdNxp3eZvfYuPpDhgr97716jgbCAtl6BE/2I6Q+zKq/uJ8uXXLippeu1J?= =?us-ascii?Q?Vw7sjbF+oEH3WIhwxxIUnX3hsZhqtVFykkBfYnLe9kb4iuu29uNJsGo2Gxks?= =?us-ascii?Q?D8q5KNfk+hYtBLQPiWcsjaBqiOhtn0PKj5rKFT5uQCMtTdx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YTVYyq9Oq/lag5ltOYNTPoFtjAXy3IoMXH07dzcg6A?= =?us-ascii?Q?JqqBNJHO487YK3EGtMUZrYvmdMo6SZYdY/kMLi7+1lvqDmAmVDln1CQekdBh?= =?us-ascii?Q?9hxkfGDXOdcyqEuirPm7hGieQIRlTNRUAwfs7FJZDGHbFeyt8H2oNyQvnnPX?= =?us-ascii?Q?ZkUJrYI8Lk/3CFNCpSu6tHhhvlbEh9WV8ceLdiK0XYl1i+35+llCePgGzm4C?= =?us-ascii?Q?gk4m1OOCxnLfOBkq8XNlD2KVh6wluOYgU0KNsm7bAwh1iz/vhh0+2dWmeddO?= =?us-ascii?Q?xdgNDrxTgcq5L5cKK+ZtTa4F?= X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-Network-Message-Id: fa0db08b-81ae-415f-bf1b-08d97c66f712 X-MS-Exchange-CrossTenant-AuthSource: SN6PR12MB2718.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 20 Sep 2021 18:46:32.4964 (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: toHXJCA3oDf8Ea+7tpCBVwk8Y13q+z2Tz0Dq/cMmBRUHvwFJ0gnRFzmpY87pITmwjcyk2Oy7ZO3e+9p+rmglGg== X-MS-Exchange-Transport-CrossTenantHeadersStamped: SN6PR12MB2768 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain BZ: https://bugzilla.tianocore.org/show_bug.cgi?id=3D3275 The initial page built during the SEC phase is used by the MemEncryptSevSnpValidateSystemRam() for the system RAM validation. The page validation process requires using the PVALIDATE instruction; the instruction accepts a virtual address of the memory region that needs to be validated. If hardware encounters a page table walk failure (due to page-not-present) then it raises #GP. The initial page table built in SEC phase address up to 4GB. Add an internal function to extend the page table to cover > 4GB. The function builds 1GB entries in the page table for access > 4GB. This will provide the support to call PVALIDATE instruction for the virtual address > 4GB in PEI phase. 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 --- .../BaseMemEncryptSevLib/X64/VirtualMemory.h | 19 +++ .../X64/PeiDxeVirtualMemory.c | 115 ++++++++++++++++++ .../X64/PeiSnpSystemRamValidate.c | 22 ++++ 3 files changed, 156 insertions(+) diff --git a/OvmfPkg/Library/BaseMemEncryptSevLib/X64/VirtualMemory.h b/Ovm= fPkg/Library/BaseMemEncryptSevLib/X64/VirtualMemory.h index 21bbbd1c4f9c..aefef68c30c0 100644 --- a/OvmfPkg/Library/BaseMemEncryptSevLib/X64/VirtualMemory.h +++ b/OvmfPkg/Library/BaseMemEncryptSevLib/X64/VirtualMemory.h @@ -143,4 +143,23 @@ InternalMemEncryptSevClearMmioPageEncMask ( IN PHYSICAL_ADDRESS PhysicalAddress, IN UINTN Length ); + +/** + Create 1GB identity mapping for the specified virtual address range. + + @param[in] Cr3BaseAddress Cr3 Base Address (if zero then use + current CR3) + @param[in] VirtualAddress Virtual address + @param[in] Length Length of virtual address range + + @retval RETURN_INVALID_PARAMETER Number of pages is zero. + +**/ +RETURN_STATUS +EFIAPI +InternalMemEncryptSevCreateIdentityMap1G ( + IN PHYSICAL_ADDRESS Cr3BaseAddress, + IN PHYSICAL_ADDRESS PhysicalAddress, + IN UINTN Length + ); #endif diff --git a/OvmfPkg/Library/BaseMemEncryptSevLib/X64/PeiDxeVirtualMemory.c= b/OvmfPkg/Library/BaseMemEncryptSevLib/X64/PeiDxeVirtualMemory.c index c696745f9d26..f146f6d61cc5 100644 --- a/OvmfPkg/Library/BaseMemEncryptSevLib/X64/PeiDxeVirtualMemory.c +++ b/OvmfPkg/Library/BaseMemEncryptSevLib/X64/PeiDxeVirtualMemory.c @@ -536,6 +536,121 @@ EnableReadOnlyPageWriteProtect ( AsmWriteCr0 (AsmReadCr0() | BIT16); } =20 +RETURN_STATUS +EFIAPI +InternalMemEncryptSevCreateIdentityMap1G ( + IN PHYSICAL_ADDRESS Cr3BaseAddress, + IN PHYSICAL_ADDRESS PhysicalAddress, + IN UINTN Length + ) +{ + PAGE_MAP_AND_DIRECTORY_POINTER *PageMapLevel4Entry; + PAGE_TABLE_1G_ENTRY *PageDirectory1GEntry; + UINT64 PgTableMask; + UINT64 AddressEncMask; + BOOLEAN IsWpEnabled; + RETURN_STATUS Status; + + // + // Set PageMapLevel4Entry to suppress incorrect compiler/analyzer warnin= gs. + // + PageMapLevel4Entry =3D NULL; + + DEBUG (( + DEBUG_VERBOSE, + "%a:%a: Cr3Base=3D0x%Lx Physical=3D0x%Lx Length=3D0x%Lx\n", + gEfiCallerBaseName, + __FUNCTION__, + Cr3BaseAddress, + PhysicalAddress, + (UINT64)Length + )); + + if (Length =3D=3D 0) { + return RETURN_INVALID_PARAMETER; + } + + // + // Check if we have a valid memory encryption mask + // + AddressEncMask =3D InternalGetMemEncryptionAddressMask (); + if (!AddressEncMask) { + return RETURN_ACCESS_DENIED; + } + + PgTableMask =3D AddressEncMask | EFI_PAGE_MASK; + + + // + // Make sure that the page table is changeable. + // + IsWpEnabled =3D IsReadOnlyPageWriteProtected (); + if (IsWpEnabled) { + DisableReadOnlyPageWriteProtect (); + } + + Status =3D EFI_SUCCESS; + + while (Length) + { + // + // If Cr3BaseAddress is not specified then read the current CR3 + // + if (Cr3BaseAddress =3D=3D 0) { + Cr3BaseAddress =3D AsmReadCr3(); + } + + PageMapLevel4Entry =3D (VOID*) (Cr3BaseAddress & ~PgTableMask); + PageMapLevel4Entry +=3D PML4_OFFSET(PhysicalAddress); + if (!PageMapLevel4Entry->Bits.Present) { + DEBUG (( + DEBUG_ERROR, + "%a:%a: bad PML4 for Physical=3D0x%Lx\n", + gEfiCallerBaseName, + __FUNCTION__, + PhysicalAddress + )); + Status =3D RETURN_NO_MAPPING; + goto Done; + } + + PageDirectory1GEntry =3D (VOID *)( + (PageMapLevel4Entry->Bits.PageTableBaseAddres= s << + 12) & ~PgTableMask + ); + PageDirectory1GEntry +=3D PDP_OFFSET(PhysicalAddress); + if (!PageDirectory1GEntry->Bits.Present) { + PageDirectory1GEntry->Bits.Present =3D 1; + PageDirectory1GEntry->Bits.MustBe1 =3D 1; + PageDirectory1GEntry->Bits.MustBeZero =3D 0; + PageDirectory1GEntry->Bits.ReadWrite =3D 1; + PageDirectory1GEntry->Uint64 |=3D (UINT64)PhysicalAddress | AddressE= ncMask; + } + + if (Length <=3D BIT30) { + Length =3D 0; + } else { + Length -=3D BIT30; + } + + PhysicalAddress +=3D BIT30; + } + + // + // Flush TLB + // + CpuFlushTlb(); + +Done: + // + // Restore page table write protection, if any. + // + if (IsWpEnabled) { + EnableReadOnlyPageWriteProtect (); + } + + return Status; +} =20 /** This function either sets or clears memory encryption bit for the memory diff --git a/OvmfPkg/Library/BaseMemEncryptSevLib/X64/PeiSnpSystemRamValida= te.c b/OvmfPkg/Library/BaseMemEncryptSevLib/X64/PeiSnpSystemRamValidate.c index eae7a31773a4..a0803e1255dc 100644 --- a/OvmfPkg/Library/BaseMemEncryptSevLib/X64/PeiSnpSystemRamValidate.c +++ b/OvmfPkg/Library/BaseMemEncryptSevLib/X64/PeiSnpSystemRamValidate.c @@ -10,9 +10,12 @@ =20 #include #include +#include +#include #include =20 #include "SnpPageStateChange.h" +#include "VirtualMemory.h" =20 typedef struct { UINT64 StartAddress; @@ -105,6 +108,7 @@ MemEncryptSevSnpPreValidateSystemRam ( { PHYSICAL_ADDRESS EndAddress; SNP_PRE_VALIDATED_RANGE OverlapRange; + EFI_STATUS Status; =20 if (!MemEncryptSevSnpIsEnabled ()) { return; @@ -112,6 +116,24 @@ MemEncryptSevSnpPreValidateSystemRam ( =20 EndAddress =3D BaseAddress + EFI_PAGES_TO_SIZE (NumPages); =20 + // + // The page table used in PEI can address up to 4GB memory. If we are as= ked to + // validate a range above the 4GB, then create an identity mapping so th= at the + // PVALIDATE instruction can execute correctly. If the page table entry = is not + // present then PVALIDATE will #GP. + // + if (BaseAddress >=3D SIZE_4GB) { + Status =3D InternalMemEncryptSevCreateIdentityMap1G ( + 0, + BaseAddress, + EFI_PAGES_TO_SIZE (NumPages) + ); + if (EFI_ERROR (Status)) { + ASSERT (FALSE); + CpuDeadLoop (); + } + } + while (BaseAddress < EndAddress) { // // Check if the range overlaps with the pre-validated ranges. --=20 2.25.1