From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from NAM10-BN7-obe.outbound.protection.outlook.com (NAM10-BN7-obe.outbound.protection.outlook.com []) by mx.groups.io with SMTP id smtpd.web12.10214.1619783548337545009 for ; Fri, 30 Apr 2021 04:52:34 -0700 Authentication-Results: mx.groups.io; dkim=pass header.i=@amd.com header.s=selector1 header.b=ZpbgqjmH; 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=az75ZLrTO1DmRmjQQ5DnA8MjtPrVG7C4gj/d476+FjrJkYY/uXlrNqiTZ5wA2EozH02a3qer3SZ8t/NRw1boYNEd8xAn7q+eOSo6qajURNGfgNM0K+Ahv5RYVTlbFY3vGZimqsqpOdYNCfwHbLvG7T5W7AubY7PmopFE9d+xNdDQtBNC3SH/UeyhTohnNF60bgEDdTY08OceRQ8YKa8LJ3fLnJtOEqu2cONBBtvanWKBEvFSIS6+HlW+voWoO3t+phkZRUP97ZEmafHwCZWQE5Zw10zQBOo/AomFoIIUhxpBLC8cPZ6EqIGK4tfd8d7MX/sXIkDC7w57x5eedKnNBg== 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=RFODU3l7Yydv8RQ8TUN20YOP3Hvnu7ckfSWlCwcS7bA=; b=ElGKU/ge5JVbhAGxIvfcerBSHC4ys78NgU+ucEaUbBdEQVoyYZwxFxVS7pPHyz5UgtZG2qxJdt5mSZ+7mn19T2ojg6WhCvkOPtwZKCo+X6zo2OUcFI7a5jtjGqlHiQJrNBbClPwbuKTXYwLK4jYHawE+guYy53Ny99B7C/7b2NGFWxwceF8g5ZVIddQPvvn0I0q1x8H15uuteUu4F4Kze5HRp6xaXTdg3D8Ndc048oQH4/KbH9jo5J06K5q/CT34O26EAVZ8+xHq8u3l/ZCFTAj9qRgDECua4GSx1Ja8NPOzLIPK5Hb1ArMGLUogVlq9dVs5XLvRjGiTtVaXxp3k1w== 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=RFODU3l7Yydv8RQ8TUN20YOP3Hvnu7ckfSWlCwcS7bA=; b=ZpbgqjmHQuhAOQw4OZm3MZ593Cy8j90gTlRH9QwWLZzEMrsxEMFE294QIvacdWma57ss8PUGFrJScFvyAdh/CGHB+fikbtM/yrkviLb0+E2hII4jE14oNdC7oVlGnAvud6ep+vFDEWJVyL9t9vAjfJcGv3+KnX59c2caB8/CY74= 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 SN6PR12MB2783.namprd12.prod.outlook.com (2603:10b6:805:78::18) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4065.25; Fri, 30 Apr 2021 11:52:32 +0000 Received: from SN6PR12MB2718.namprd12.prod.outlook.com ([fe80::9898:5b48:a062:db94]) by SN6PR12MB2718.namprd12.prod.outlook.com ([fe80::9898:5b48:a062:db94%6]) with mapi id 15.20.4065.027; Fri, 30 Apr 2021 11:52:32 +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 , Erdem Aktas Subject: [PATCH RFC v2 23/28] OvmfPkg/MemEncryptSevLib: Add support to validate > 4GB memory in PEI phase Date: Fri, 30 Apr 2021 06:51:43 -0500 Message-Id: <20210430115148.22267-24-brijesh.singh@amd.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20210430115148.22267-1-brijesh.singh@amd.com> References: <20210430115148.22267-1-brijesh.singh@amd.com> X-Originating-IP: [165.204.77.1] X-ClientProxiedBy: SA0PR12CA0006.namprd12.prod.outlook.com (2603:10b6:806:6f::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 SA0PR12CA0006.namprd12.prod.outlook.com (2603:10b6:806:6f::11) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4087.27 via Frontend Transport; Fri, 30 Apr 2021 11:52:29 +0000 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: 88fc6cac-4d94-4123-10ec-08d90bce6eed X-MS-TrafficTypeDiagnostic: SN6PR12MB2783: X-MS-Exchange-Transport-Forked: True X-Microsoft-Antispam-PRVS: X-MS-Oob-TLC-OOBClassifiers: OLM:8273; X-MS-Exchange-SenderADCheck: 1 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: P55PHuqt0Sm26IxI/WYHNJ+ufQtwWYZpuknqNxq7yjiBQd0l4a0zgNTUZ/MzKlbkqBTF/wC8dyUQARCEL6/fHcMFHqjS4N1B4iosJvWb9rpmAO+VQ8a/ADy3Is1R8wVNyoCouzd0zdV6w01HPgR7qPaFTvhsZ48ejnVTjPZYygmkQAZN2jESzHqScpEXWQnkGh6vuaEiUoppFK8rsTSHaK+ItojyNXqSKdnQtcLV/2IFOEWHFuadLRUX/U9n13BR60zDpS5OIEn/hPpjGMvDMcrYBSS7QAsRhdzK7q/0T142LzmZHqPnRcTUaLzChZc6PP+iWyXdpBHUuZ1ix6F9nH3TSWKa0OTx/MimeOeTwMtPJ0sf40rt5rmi+3pRtRKcFfUx+CwbIhFDSdLXF2ReV0u4cJWlh8leHJoYCtU/nn7J4JtOU1p+z9yBP6hbv3a0F/SZNh8OtbUo+9NatbtsXaiCaTJq4S2KDewW+UMNBieqIeSB0H5QT/XZFKr3XdLwbYgKZvxFjQtLIF1DlJI18dHdGdwz/gaw/m60Gvjnli8p1QNQdAFEfpKQkRfXeI65Sn5O2RpQ5o9nqYeHMXPNGknglBm5zm5K482Z3zbf7tLWtUiFHyGG28TBEgRxoJXFNLkegyHhZMnCc1Ny6bub567uxREGsW0oD+4P48zBN+EypLzLfVSyzIuFUPPpCFZ5jkfWOCMdSbrMHHwfocCepr04w4TFT9wALu9J1lQPFgM= 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)(39860400002)(396003)(136003)(366004)(376002)(346002)(36756003)(44832011)(16526019)(52116002)(2616005)(956004)(66556008)(26005)(38100700002)(54906003)(966005)(1076003)(66476007)(186003)(7696005)(66946007)(5660300002)(15650500001)(6486002)(38350700002)(6916009)(86362001)(19627235002)(316002)(83380400001)(2906002)(6666004)(8936002)(478600001)(8676002)(4326008);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData: =?us-ascii?Q?tRd6V6no77OsPkuT7UfCMWCNN44NOLpZwFCL8jVu6zDFsTe8VTSnt9tOFrD7?= =?us-ascii?Q?lD0dp67xOzm4ePtDsVvxFTbAGz5cqpp3a4qGtJoSRqkhu8sJ+5wFWe6GGy7W?= =?us-ascii?Q?nsbM0D94cy+fTrgEquOoFuc7rje2b1R5p2C7bHSxaH+gXq78XXPJxflLbslS?= =?us-ascii?Q?+wAoXGXwlx7vIetblF4YqhX7Ws46jVL8GtgB245xNN9nPTuQbbSD9n1EIAic?= =?us-ascii?Q?M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0lBWBsQ9/cnl9h?= =?us-ascii?Q?l6Urkg79fHoqlS360K2D/gtDeCkJ8jY9grj7HoTij3tnYk+VHOx3SMLrysKy?= =?us-ascii?Q?3IaFedlfXxAh4dQIC8e/A6AKYR/9AOjy1lWReFbsYLx2SCYeNJbdUZw0MKjK?= =?us-ascii?Q?Wj+qEgMheIcbLMzJ/gY89/GVUBMdCRL9JaeKjbsZgTESImfC7N1doriK1jD4?= =?us-ascii?Q?Bz7+34AYaF5qFUCRYVOHwyB8?= X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-Network-Message-Id: 88fc6cac-4d94-4123-10ec-08d90bce6eed X-MS-Exchange-CrossTenant-AuthSource: SN6PR12MB2718.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 30 Apr 2021 11:52:30.3283 (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: bRvE2NXalrIqMQm4c07/phRv5tazs/2FIL+kCnO9QRhjEJmE7N+93+ZfZbGJ7bk5TlILOKPQTmESfLwj0OokrQ== X-MS-Exchange-Transport-CrossTenantHeadersStamped: SN6PR12MB2783 Content-Type: text/plain BZ: https://bugzilla.tianocore.org/show_bug.cgi?id=3275 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: James Bottomley Cc: Min Xu Cc: Jiewen Yao Cc: Tom Lendacky Cc: Jordan Justen Cc: Ard Biesheuvel Cc: Laszlo Ersek Cc: Erdem Aktas Signed-off-by: Brijesh Singh --- OvmfPkg/Library/BaseMemEncryptSevLib/X64/PeiDxeVirtualMemory.c | 115 ++++++++++++++++++++ OvmfPkg/Library/BaseMemEncryptSevLib/X64/PeiSnpSystemRamValidate.c | 22 ++++ OvmfPkg/Library/BaseMemEncryptSevLib/X64/VirtualMemory.h | 19 ++++ 3 files changed, 156 insertions(+) diff --git a/OvmfPkg/Library/BaseMemEncryptSevLib/X64/PeiDxeVirtualMemory.c b/OvmfPkg/Library/BaseMemEncryptSevLib/X64/PeiDxeVirtualMemory.c index 707db5a74a..996a319b26 100644 --- a/OvmfPkg/Library/BaseMemEncryptSevLib/X64/PeiDxeVirtualMemory.c +++ b/OvmfPkg/Library/BaseMemEncryptSevLib/X64/PeiDxeVirtualMemory.c @@ -536,6 +536,121 @@ EnableReadOnlyPageWriteProtect ( AsmWriteCr0 (AsmReadCr0() | BIT16); } +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 warnings. + // + PageMapLevel4Entry = NULL; + + DEBUG (( + DEBUG_VERBOSE, + "%a:%a: Cr3Base=0x%Lx Physical=0x%Lx Length=0x%Lx\n", + gEfiCallerBaseName, + __FUNCTION__, + Cr3BaseAddress, + PhysicalAddress, + (UINT64)Length + )); + + if (Length == 0) { + return RETURN_INVALID_PARAMETER; + } + + // + // Check if we have a valid memory encryption mask + // + AddressEncMask = InternalGetMemEncryptionAddressMask (); + if (!AddressEncMask) { + return RETURN_ACCESS_DENIED; + } + + PgTableMask = AddressEncMask | EFI_PAGE_MASK; + + + // + // Make sure that the page table is changeable. + // + IsWpEnabled = IsReadOnlyPageWriteProtected (); + if (IsWpEnabled) { + DisableReadOnlyPageWriteProtect (); + } + + Status = EFI_SUCCESS; + + while (Length) + { + // + // If Cr3BaseAddress is not specified then read the current CR3 + // + if (Cr3BaseAddress == 0) { + Cr3BaseAddress = AsmReadCr3(); + } + + PageMapLevel4Entry = (VOID*) (Cr3BaseAddress & ~PgTableMask); + PageMapLevel4Entry += PML4_OFFSET(PhysicalAddress); + if (!PageMapLevel4Entry->Bits.Present) { + DEBUG (( + DEBUG_ERROR, + "%a:%a: bad PML4 for Physical=0x%Lx\n", + gEfiCallerBaseName, + __FUNCTION__, + PhysicalAddress + )); + Status = RETURN_NO_MAPPING; + goto Done; + } + + PageDirectory1GEntry = (VOID *)( + (PageMapLevel4Entry->Bits.PageTableBaseAddress << + 12) & ~PgTableMask + ); + PageDirectory1GEntry += PDP_OFFSET(PhysicalAddress); + if (!PageDirectory1GEntry->Bits.Present) { + PageDirectory1GEntry->Bits.Present = 1; + PageDirectory1GEntry->Bits.MustBe1 = 1; + PageDirectory1GEntry->Bits.MustBeZero = 0; + PageDirectory1GEntry->Bits.ReadWrite = 1; + PageDirectory1GEntry->Uint64 |= (UINT64)PhysicalAddress | AddressEncMask; + } + + if (Length <= BIT30) { + Length = 0; + } else { + Length -= BIT30; + } + + PhysicalAddress += BIT30; + } + + // + // Flush TLB + // + CpuFlushTlb(); + +Done: + // + // Restore page table write protection, if any. + // + if (IsWpEnabled) { + EnableReadOnlyPageWriteProtect (); + } + + return Status; +} /** This function either sets or clears memory encryption bit for the memory diff --git a/OvmfPkg/Library/BaseMemEncryptSevLib/X64/PeiSnpSystemRamValidate.c b/OvmfPkg/Library/BaseMemEncryptSevLib/X64/PeiSnpSystemRamValidate.c index 1fc3337f7c..ff4c0742ed 100644 --- a/OvmfPkg/Library/BaseMemEncryptSevLib/X64/PeiSnpSystemRamValidate.c +++ b/OvmfPkg/Library/BaseMemEncryptSevLib/X64/PeiSnpSystemRamValidate.c @@ -10,9 +10,12 @@ #include #include +#include +#include #include #include "SnpPageStateChange.h" +#include "VirtualMemory.h" typedef struct { UINT64 StartAddress; @@ -68,6 +71,7 @@ MemEncryptSevSnpPreValidateSystemRam ( { PHYSICAL_ADDRESS EndAddress; SNP_PRE_VALIDATED_RANGE OverlapRange; + EFI_STATUS Status; if (!MemEncryptSevSnpIsEnabled ()) { return; @@ -75,6 +79,24 @@ MemEncryptSevSnpPreValidateSystemRam ( EndAddress = BaseAddress + EFI_PAGES_TO_SIZE (NumPages); + // + // The page table used in PEI can address up to 4GB memory. If we are asked to + // validate a range above the 4GB, then create an identity mapping so that the + // PVALIDATE instruction can execute correctly. If the page table entry is not + // present then PVALIDATE will #GP. + // + if (BaseAddress >= SIZE_4GB) { + Status = 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. diff --git a/OvmfPkg/Library/BaseMemEncryptSevLib/X64/VirtualMemory.h b/OvmfPkg/Library/BaseMemEncryptSevLib/X64/VirtualMemory.h index 832ff10a33..25de939797 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 -- 2.17.1