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 [40.107.102.72]) by mx.groups.io with SMTP id smtpd.web11.37.1616599960815499093 for ; Wed, 24 Mar 2021 08:32:41 -0700 Authentication-Results: mx.groups.io; dkim=pass header.i=@amd.com header.s=selector1 header.b=CEBPJu/J; 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.102.72, mailfrom: brijesh.singh@amd.com) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=dZCynGqvNKdjrvMCyUwBMvfERgCV8+XGb+rjB+KMl+0U5mlViNg9OA6RcPDG3ijEX0AcKqIym/ON3+4BTPTiKpaiBAfWUFWZfm5QVGKZIKENLQ92dQnQMbNCIZSFOv5CJ+daqieWdrpe40YCjjLMrapDagPl8HhZzrh5YbrgNOMyCcye3NUdy1nWJYchCYv5OdQ9yR8ZjFjzPNXgX/InTkzk2qMwyEIz3HoyVnyKYk+JvRYMTNLzFgRlo/gszNsdb3oxpxxxHpGTSLC+xH72C6ayJKLsBrRdA1l9wb83VsKFX5UMBO/R4REXOV1WezkoNDZhrcjpoQpQUjDNfrZ+kQ== 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=JxKo4fWiK5f+dT2xW4yvj9XOa9amCCb9V5ASX+hi5a8=; b=SUpnZ5qZ65B9RpzvHPPZPMaQDBBWsGer0h3oiBEhPUhdL+5vXkEHZ+fSjC9t9ulRXlIe9MB/cRv98H18v84CdLf2NDi4p33/jEYdLcAMS3rqpS8tLagIHLssGRQBUu5wwe6i9aLzh0k1B2L9yFthIahjEPSpiXZu3YpLUgRfo+ZOqhtp66CHWglto+aHr2HxqTBkvFq4puPW21knIgSZBWGfRFBX8F+87m4roBu7jJxPXYZ19I9MZupQCout5WeKhOrw0toCLIBVWYNizBaTzITziD3b4aQ5wUosO15P8viX5SwbiGxl36nvQh+JbdChSzyJ6u1EXLdu5Mi2ucgrtg== 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=JxKo4fWiK5f+dT2xW4yvj9XOa9amCCb9V5ASX+hi5a8=; b=CEBPJu/Jy3dcnt/6pbhuUEgMzwStV55RLba2gCAR8ZG5TlkhT+jOvZF7egmT5pEMxTyYZHwkLOy6fy9T5addLjQ/JP9mftC8J51H8cfGZn7jDBnoygG6AEvw3zRNmNHbcPKbLbN0lTTL2w0SW4Zldq4zG76YDCcE6pxgKCv3pbI= 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:39 +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:39 +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 16/19] OvmfPkg/MemEncryptSevLib: Add support to validate > 4GB memory in PEI phase Date: Wed, 24 Mar 2021 10:32:12 -0500 Message-Id: <20210324153215.17971-17-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:38 +0000 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-HT: Tenant X-MS-Office365-Filtering-Correlation-Id: 3e2df773-70ec-42d5-dc76-08d8eeda0ec5 X-MS-TrafficTypeDiagnostic: SA0PR12MB4430: 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: mway/qBG0U86eEzhy5ubAG+E8qq0nm6rxoqoZCVCj0CMrgydktxTKhwqbL0Pa4mzB9lp50pIvSUJXQvn4P7NmUFSSgQ4GGHDJZEX1L8KEUly0fSweHI/tjnP2lBcGLK0rZD+f2ZXPCAFvK6egrWXFX7XxYXIyqDWXySxsB6RBopUUyRtdsexD5d9F4SOVVBjgfjUoHkm6Na+8tudThzW+BkNI9GlwOmlKwp76wUKdQ9K62qswwEFQD5kgPzFZqEm8ApcyTEj6ezWIlzmVx/S4+FrwCdFmrHwllHG68MCJGmxZqzBN2xdjJziYLLV7W7hF012t07jVo30LMnpJ6IjRf9vd0d8hMaZ1oW3Hh9+QqeJXhjLSO+OElcDD06POWMk5DDzFhwBX8BldfM6+oDg4da/KS+XBcBgYavqQnthLREIiur5orXcFjBPpIOVPPRBrTVw6CNHJ9+S1fUdin5EUrP7I7vg7d/2hig/M3krtqWJRCCJU9qZZY+c1AostBylJhfMotBvUUJSuSvw94gN8Z1QO8xymHGDyY3LvD733MH/Ogox8oAl0Kbcy7YqszRM6jJa8mtvrQ0cq2DkGPiACDXh8LroryY35VgMtg0YsjeDI9sDJM97+jzcILRa3hmvLkmbMhwKg45UM7pRTA9fhpg8lX9ufl38QToVGVIl4Zel5K/bT2M0AtJStW7fl8jmnmF62A3sNYeQ3qHOGO25Eecr1En9JhGYwl2IWd0jT0g= 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?59WSLA6Rti1wDTUhQ2rNp+ggUIQE/CwGgzfbSfqnQNGh5svWY8YpMBXPWtd3?= =?us-ascii?Q?cbUxtyr+qDp49DoPLLKDLfpOc/r1FDUX5C1Ibtt7cRKXYkIulVgSRA/Ns072?= =?us-ascii?Q?QjShV9an/urwxXABNq8fSjClpL+hFNnvARBhW3zCExeyfPCd2UVjBzzEeNCM?= =?us-ascii?Q?p1bBNgWVmko4rFDK2MLjAMdG1iYOi1sebDoISw4Euh5Dun8SpjQlqtA4v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/HwW8PbT81FpxJqmjCxEuzVuBE4I+k4?= =?us-ascii?Q?duDULqqnsQpvZG0IHvzjzGkgMm1H8sCiKfZ+JtNh4aDi+1jaj1e3af0dRYft?= =?us-ascii?Q?zwZ9b+uEyEEEL7Kr+AL8mi20JigAK0mjBaa9hlipDpLqU1HZEAgSxin5b1wl?= =?us-ascii?Q?qvVaI6P3QSSRWcCjRZpm6k8mXRTlM8YwsXUiDRgRBtSjtMYr9QzvRPYJibTR?= =?us-ascii?Q?zVTwPE+wJNQHZt/Xi0Jjrn1W?= X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-Network-Message-Id: 3e2df773-70ec-42d5-dc76-08d8eeda0ec5 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:39.4892 (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: fVBiZjWf7Y/SEVmG6fGqM8aCzOwhND/mjqXNG8hhY9m07ecFY5x2i06NwMUjwFaffj0guLqLyYDnsXSsoZvjQA== X-MS-Exchange-Transport-CrossTenantHeadersStamped: SA0PR12MB4430 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 Signed-off-by: Brijesh Singh --- OvmfPkg/Library/BaseMemEncryptSevLib/X64/PeiDxeVirtualMemory.c | 115 ++++++++++++++++++++ OvmfPkg/Library/BaseMemEncryptSevLib/X64/PeiSnpSystemRamValidate.c | 16 +++ OvmfPkg/Library/BaseMemEncryptSevLib/X64/VirtualMemory.h | 19 ++++ 3 files changed, 150 insertions(+) diff --git a/OvmfPkg/Library/BaseMemEncryptSevLib/X64/PeiDxeVirtualMemory.c b/OvmfPkg/Library/BaseMemEncryptSevLib/X64/PeiDxeVirtualMemory.c index d3455e812b..33d9bafe9f 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 ce8a05bb1f..41bf301efe 100644 --- a/OvmfPkg/Library/BaseMemEncryptSevLib/X64/PeiSnpSystemRamValidate.c +++ b/OvmfPkg/Library/BaseMemEncryptSevLib/X64/PeiSnpSystemRamValidate.c @@ -16,6 +16,7 @@ #include "../SnpPageStateChange.h" #include "SnpPageStateTrack.h" +#include "VirtualMemory.h" STATIC SNP_VALIDATED_RANGE *mRootNode; @@ -62,9 +63,24 @@ SevSnpValidateSystemRam ( { UINTN EndAddress; SNP_VALIDATED_RANGE *Range; + EFI_STATUS Status; 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 + // cause the #GP. + // + if (BaseAddress >= SIZE_4GB) { + Status = InternalMemEncryptSevCreateIdentityMap1G (0, BaseAddress, + EFI_PAGES_TO_SIZE (NumPages)); + if (EFI_ERROR (Status)) { + ASSERT (FALSE); + } + } + // // If the Root is NULL then its the first call. Lets initialize the List before // we process the request. diff --git a/OvmfPkg/Library/BaseMemEncryptSevLib/X64/VirtualMemory.h b/OvmfPkg/Library/BaseMemEncryptSevLib/X64/VirtualMemory.h index 996f94f07e..829dc96a1d 100644 --- a/OvmfPkg/Library/BaseMemEncryptSevLib/X64/VirtualMemory.h +++ b/OvmfPkg/Library/BaseMemEncryptSevLib/X64/VirtualMemory.h @@ -267,4 +267,23 @@ InternalMemEncryptSevGetAddressRangeState ( 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 to check + @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