From nobody Wed May 1 22:04:29 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of groups.io designates 66.175.222.12 as permitted sender) client-ip=66.175.222.12; envelope-from=bounce+27952+56388+1787277+3901457@groups.io; helo=web01.groups.io; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of groups.io designates 66.175.222.12 as permitted sender) smtp.mailfrom=bounce+27952+56388+1787277+3901457@groups.io; dmarc=fail(p=none dis=none) header.from=linaro.org ARC-Seal: i=1; a=rsa-sha256; t=1585218291; cv=none; d=zohomail.com; s=zohoarc; b=Sx0LN2mt24uL1bkN0ZuuU/AQU0e9AX3NNHpEYj8Fh/XJ0Af2G9MQJkXsQrN09Zro0x8W3UJe3GB6vMagalM99p3oNUqhTTYpPNV0z8S+W5Y9es67QobmufnnZmBTT3Pt34GzNPOuroZvTHm3nkOso31gCdoTsb1qfRwd6viGpfM= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1585218291; h=Cc:Date:From:In-Reply-To:List-Id:List-Unsubscribe:Message-ID:Reply-To:References:Sender:Subject:To; bh=IBTUJ1BSOB9WWIvDauemRIqTTp3lnEqqOFAt8Bxpbnc=; b=f4CVIL+95YYEdncggHAgCA1hb5JESZs5/AdRIbfTqF5IrJIxluCgGsaz9oM93RXb2d6pOEQPdBeNETxPIbAOiqfD75ku8vfFn5YDxql+SjCzMz5iPIWZB9xK/z+osRyujA+/QhPvqyFCmeLWQqFF03w18rYf6YL9WKDotYlIQaE= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of groups.io designates 66.175.222.12 as permitted sender) smtp.mailfrom=bounce+27952+56388+1787277+3901457@groups.io; dmarc=fail header.from= (p=none dis=none) header.from= Received: from web01.groups.io (web01.groups.io [66.175.222.12]) by mx.zohomail.com with SMTPS id 1585218291393520.5576556872634; Thu, 26 Mar 2020 03:24:51 -0700 (PDT) Return-Path: X-Received: by 127.0.0.2 with SMTP id PoLjYY1788612xqcPDIbULNs; Thu, 26 Mar 2020 03:24:50 -0700 X-Received: from mail-wr1-f44.google.com (mail-wr1-f44.google.com [209.85.221.44]) by mx.groups.io with SMTP id smtpd.web11.34021.1585218290124241579 for ; Thu, 26 Mar 2020 03:24:50 -0700 X-Received: by mail-wr1-f44.google.com with SMTP id t7so6980570wrw.12 for ; Thu, 26 Mar 2020 03:24:49 -0700 (PDT) X-Gm-Message-State: dFKBeQJHSGDLgVmWzjjKmcvux1787277AA= X-Google-Smtp-Source: ADFU+vu23Ou/4v7kqx5fEeHnnXpUgFP87N/uDQT2EMqS7TBoXnBhiaKGZSQd9eCNxmQYe6ZJcD5hPA== X-Received: by 2002:adf:eed1:: with SMTP id a17mr8269708wrp.287.1585218288359; Thu, 26 Mar 2020 03:24:48 -0700 (PDT) X-Received: from e123331-lin.home (amontpellier-657-1-18-247.w109-210.abo.wanadoo.fr. [109.210.65.247]) by smtp.gmail.com with ESMTPSA id d5sm3124511wrh.40.2020.03.26.03.24.47 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 26 Mar 2020 03:24:47 -0700 (PDT) From: "Ard Biesheuvel" To: devel@edk2.groups.io Cc: Ard Biesheuvel , Laszlo Ersek , Leif Lindholm , Michael D Kinney , Ray Ni , Jiewen Yao , Bret Barkelew Subject: [edk2-devel] [PATCH 1/4] OvmfPkg: remove handling of properties table Date: Thu, 26 Mar 2020 11:24:40 +0100 Message-Id: <20200326102443.748-2-ard.biesheuvel@linaro.org> In-Reply-To: <20200326102443.748-1-ard.biesheuvel@linaro.org> References: <20200326102443.748-1-ard.biesheuvel@linaro.org> Precedence: Bulk List-Unsubscribe: Sender: devel@edk2.groups.io List-Id: Mailing-List: list devel@edk2.groups.io; contact devel+owner@edk2.groups.io Reply-To: devel@edk2.groups.io,ard.biesheuvel@linaro.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=groups.io; q=dns/txt; s=20140610; t=1585218290; bh=0tx2Sd0uiYHeFHyzsQBAw5TrijKd9Vw84Gj65RPTkcw=; h=Cc:Date:From:Reply-To:Subject:To; b=dmSWQFgOzb8KhFSEdzNF79+NqcSmZO8TLPCZh9VcOm3js1sv1jeNeoBTkZeYEvngrxx F5MzvEJxZjY+pohFV/BYYeBC2aXyqzlE+Tp+JeB4kvjjvuCBuJOtzwxS/JMQAVy9x+B4m 7ki3OevQjLKjplbNAfSzPiz+KS8YjpC3zJg= X-ZohoMail-DKIM: pass (identity @groups.io) Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" The UEFI properties table and the associated memory protection feature was severely broken from the start, and has been deprecated for a while. Let's drop all references to it from OVMF so we can safely remove it from the DXE core as well. Link: https://bugzilla.tianocore.org/show_bug.cgi?id=3D2633 Signed-off-by: Ard Biesheuvel Acked-by: Jiewen Yao Reviewed-by: Dandan Bi for patch [PATCH 2/4] [PATCH 3= /4] [PATCH 4/4]. Reviewed-by: Jian J Wang Reviewed-by: Laszlo Ersek --- OvmfPkg/OvmfPkgIa32.dsc | 1 - OvmfPkg/OvmfPkgIa32X64.dsc | 1 - OvmfPkg/OvmfPkgX64.dsc | 1 - OvmfPkg/OvmfXen.dsc | 1 - OvmfPkg/PlatformPei/Platform.c | 1 - OvmfPkg/PlatformPei/PlatformPei.inf | 1 - 6 files changed, 6 deletions(-) diff --git a/OvmfPkg/OvmfPkgIa32.dsc b/OvmfPkg/OvmfPkgIa32.dsc index 02ca17db8b2a..47926ac9e235 100644 --- a/OvmfPkg/OvmfPkgIa32.dsc +++ b/OvmfPkg/OvmfPkgIa32.dsc @@ -570,7 +570,6 @@ [PcdsDynamicDefault] =20 # Noexec settings for DXE. gEfiMdeModulePkgTokenSpaceGuid.PcdSetNxForStack|FALSE - gEfiMdeModulePkgTokenSpaceGuid.PcdPropertiesTableEnable|FALSE =20 # UefiCpuPkg PCDs related to initial AP bringup and general AP managemen= t. gUefiCpuPkgTokenSpaceGuid.PcdCpuMaxLogicalProcessorNumber|64 diff --git a/OvmfPkg/OvmfPkgIa32X64.dsc b/OvmfPkg/OvmfPkgIa32X64.dsc index d08cf558c6aa..37943624dc72 100644 --- a/OvmfPkg/OvmfPkgIa32X64.dsc +++ b/OvmfPkg/OvmfPkgIa32X64.dsc @@ -581,7 +581,6 @@ [PcdsDynamicDefault] =20 # Noexec settings for DXE. gEfiMdeModulePkgTokenSpaceGuid.PcdSetNxForStack|FALSE - gEfiMdeModulePkgTokenSpaceGuid.PcdPropertiesTableEnable|FALSE =20 # UefiCpuPkg PCDs related to initial AP bringup and general AP managemen= t. gUefiCpuPkgTokenSpaceGuid.PcdCpuMaxLogicalProcessorNumber|64 diff --git a/OvmfPkg/OvmfPkgX64.dsc b/OvmfPkg/OvmfPkgX64.dsc index b2dccc40a865..f41ebb95d717 100644 --- a/OvmfPkg/OvmfPkgX64.dsc +++ b/OvmfPkg/OvmfPkgX64.dsc @@ -580,7 +580,6 @@ [PcdsDynamicDefault] =20 # Noexec settings for DXE. gEfiMdeModulePkgTokenSpaceGuid.PcdSetNxForStack|FALSE - gEfiMdeModulePkgTokenSpaceGuid.PcdPropertiesTableEnable|FALSE =20 # UefiCpuPkg PCDs related to initial AP bringup and general AP managemen= t. gUefiCpuPkgTokenSpaceGuid.PcdCpuMaxLogicalProcessorNumber|64 diff --git a/OvmfPkg/OvmfXen.dsc b/OvmfPkg/OvmfXen.dsc index 85fe39f7896c..67b95d7b788c 100644 --- a/OvmfPkg/OvmfXen.dsc +++ b/OvmfPkg/OvmfXen.dsc @@ -483,7 +483,6 @@ [PcdsDynamicDefault] =20 # Noexec settings for DXE. gEfiMdeModulePkgTokenSpaceGuid.PcdSetNxForStack|FALSE - gEfiMdeModulePkgTokenSpaceGuid.PcdPropertiesTableEnable|FALSE =20 # Set memory encryption mask gEfiMdeModulePkgTokenSpaceGuid.PcdPteMemoryEncryptionAddressOrMask|0x0 diff --git a/OvmfPkg/PlatformPei/Platform.c b/OvmfPkg/PlatformPei/Platform.c index 587ca68fc210..088e616a980c 100644 --- a/OvmfPkg/PlatformPei/Platform.c +++ b/OvmfPkg/PlatformPei/Platform.c @@ -315,7 +315,6 @@ NoexecDxeInitialization ( VOID ) { - UPDATE_BOOLEAN_PCD_FROM_FW_CFG (PcdPropertiesTableEnable); UPDATE_BOOLEAN_PCD_FROM_FW_CFG (PcdSetNxForStack); } =20 diff --git a/OvmfPkg/PlatformPei/PlatformPei.inf b/OvmfPkg/PlatformPei/Plat= formPei.inf index 8531c63995c1..19f2424981bc 100644 --- a/OvmfPkg/PlatformPei/PlatformPei.inf +++ b/OvmfPkg/PlatformPei/PlatformPei.inf @@ -94,7 +94,6 @@ [Pcd] gEfiMdeModulePkgTokenSpaceGuid.PcdDxeIplSwitchToLongMode gEfiMdeModulePkgTokenSpaceGuid.PcdUse1GPageTable gEfiMdeModulePkgTokenSpaceGuid.PcdSetNxForStack - gEfiMdeModulePkgTokenSpaceGuid.PcdPropertiesTableEnable gEfiMdeModulePkgTokenSpaceGuid.PcdAcpiS3Enable gEfiMdeModulePkgTokenSpaceGuid.PcdPteMemoryEncryptionAddressOrMask gEfiSecurityPkgTokenSpaceGuid.PcdOptionRomImageVerificationPolicy --=20 2.17.1 -=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D- Groups.io Links: You receive all messages sent to this group. View/Reply Online (#56388): https://edk2.groups.io/g/devel/message/56388 Mute This Topic: https://groups.io/mt/72560882/1787277 Group Owner: devel+owner@edk2.groups.io Unsubscribe: https://edk2.groups.io/g/devel/unsub [importer@patchew.org] -=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D- From nobody Wed May 1 22:04:29 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of groups.io designates 66.175.222.12 as permitted sender) client-ip=66.175.222.12; envelope-from=bounce+27952+56389+1787277+3901457@groups.io; helo=web01.groups.io; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of groups.io designates 66.175.222.12 as permitted sender) smtp.mailfrom=bounce+27952+56389+1787277+3901457@groups.io; dmarc=fail(p=none dis=none) header.from=linaro.org ARC-Seal: i=1; a=rsa-sha256; t=1585218292; cv=none; d=zohomail.com; s=zohoarc; b=ZEao0y709eTvTebS/OpA+WTR9l9y2jydEDWqE2QOyyU2KRYHzCXkNUsKDYgY5+fFiP9QKDhvyMjXPo9Vl90cRAUxisDoo8u0EWVIPcROnQQBRyt2yMjHqIRvCS5xrUfIfgVWORkL1fHx8KD0829SbpeRpc1fUIyUdsGWs+JmfWg= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1585218292; h=Cc:Date:From:In-Reply-To:List-Id:List-Unsubscribe:Message-ID:Reply-To:References:Sender:Subject:To; bh=/Kd+kSo+vgkDy2Usagl+gZhR6RE7l95Dkw25MSwP0S4=; b=hzpCwx7mWhVGgSXyiM+r52KfTTh8g67+lMcmyq3MFiLOdjSNhQM/ECRSUHoiHIDpNQB8Wsk7U7C3c5TeTxB1jZkFmH6xRuXK0JcW8MKyvGbg13HxXM4P4hUCcxRBUjqaLSHpOa0eVf95eSfnLDcx1P5qoTE7i4baOm3MVqlHAYM= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of groups.io designates 66.175.222.12 as permitted sender) smtp.mailfrom=bounce+27952+56389+1787277+3901457@groups.io; dmarc=fail header.from= (p=none dis=none) header.from= Received: from web01.groups.io (web01.groups.io [66.175.222.12]) by mx.zohomail.com with SMTPS id 158521829263729.35019127702776; Thu, 26 Mar 2020 03:24:52 -0700 (PDT) Return-Path: X-Received: by 127.0.0.2 with SMTP id 775yYY1788612xBOX0OgEUS0; Thu, 26 Mar 2020 03:24:52 -0700 X-Received: from mail-wr1-f50.google.com (mail-wr1-f50.google.com [209.85.221.50]) by mx.groups.io with SMTP id smtpd.web12.33982.1585218291521608925 for ; Thu, 26 Mar 2020 03:24:51 -0700 X-Received: by mail-wr1-f50.google.com with SMTP id 31so7048070wrs.3 for ; Thu, 26 Mar 2020 03:24:51 -0700 (PDT) X-Gm-Message-State: Fgv4YDYrSJhRXHW6Hxo5Ltoqx1787277AA= X-Google-Smtp-Source: ADFU+vsN+sjSi8WrYHngzXfLH/O/XSxmrErKOHRwdR1w3Bm0uAsvQtQCFm+vxC+9nJI9bJYCsDOAoA== X-Received: by 2002:adf:e304:: with SMTP id b4mr8655332wrj.352.1585218289583; Thu, 26 Mar 2020 03:24:49 -0700 (PDT) X-Received: from e123331-lin.home (amontpellier-657-1-18-247.w109-210.abo.wanadoo.fr. [109.210.65.247]) by smtp.gmail.com with ESMTPSA id d5sm3124511wrh.40.2020.03.26.03.24.48 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 26 Mar 2020 03:24:48 -0700 (PDT) From: "Ard Biesheuvel" To: devel@edk2.groups.io Cc: Ard Biesheuvel , Laszlo Ersek , Leif Lindholm , Michael D Kinney , Ray Ni , Jiewen Yao , Bret Barkelew Subject: [edk2-devel] [PATCH 2/4] MdeModulePkg: disable properties table generation but retain the code Date: Thu, 26 Mar 2020 11:24:41 +0100 Message-Id: <20200326102443.748-3-ard.biesheuvel@linaro.org> In-Reply-To: <20200326102443.748-1-ard.biesheuvel@linaro.org> References: <20200326102443.748-1-ard.biesheuvel@linaro.org> Precedence: Bulk List-Unsubscribe: Sender: devel@edk2.groups.io List-Id: Mailing-List: list devel@edk2.groups.io; contact devel+owner@edk2.groups.io Reply-To: devel@edk2.groups.io,ard.biesheuvel@linaro.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=groups.io; q=dns/txt; s=20140610; t=1585218292; bh=HKXzAYadlCD5Mh+APyo6XcGXA7MGZatMtMPYXYAzlCw=; h=Cc:Date:From:Reply-To:Subject:To; b=s9EpnHpKjHf6LBmJxaKcpNJ86cnict12NjgZyzKzQzlLme6JUVE/JdzClh7DYF0Ufjg jy/dx5i2E0EeuBfC6ewX4KSsModbYexd2nYSCWotBRHylpjMgQ9xwhNgu1g+rhrh3fOYa fO2zjskbCUWjIQo9Cm4v9uSq7pKKvBJl7Ns= X-ZohoMail-DKIM: pass (identity @groups.io) Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" This is the minimal change required to stop exposing the EFI properties table, which is deprecated. Given how the implementation is entangled with the code that exposes the related memory attributes table, most of the code is retained, and further cleanups are relegated to subsequent patches. Link: https://bugzilla.tianocore.org/show_bug.cgi?id=3D2633 Signed-off-by: Ard Biesheuvel Acked-by: Jiewen Yao Reviewed-by: Dandan Bi for patch [PATCH 2/4] [PATCH 3= /4] [PATCH 4/4]. Reviewed-by: Jian J Wang --- MdeModulePkg/Core/Dxe/DxeMain.inf | 2 - MdeModulePkg/Core/Dxe/Misc/MemoryAttributesTable.c | 7 +- MdeModulePkg/Core/Dxe/Misc/MemoryProtection.c | 1 - MdeModulePkg/Core/Dxe/Misc/PropertiesTable.c | 107 ++---------------= --- MdeModulePkg/MdeModulePkg.dec | 24 ----- MdeModulePkg/MdeModulePkg.uni | 21 ---- 6 files changed, 14 insertions(+), 148 deletions(-) diff --git a/MdeModulePkg/Core/Dxe/DxeMain.inf b/MdeModulePkg/Core/Dxe/DxeM= ain.inf index 61161bee28e1..75e0a968f0cf 100644 --- a/MdeModulePkg/Core/Dxe/DxeMain.inf +++ b/MdeModulePkg/Core/Dxe/DxeMain.inf @@ -120,7 +120,6 @@ [Guids] gEventExitBootServicesFailedGuid ## SOMETIMES_PRODUCES ##= Event gEfiVectorHandoffTableGuid ## SOMETIMES_PRODUCES ##= SystemTable gEdkiiMemoryProfileGuid ## SOMETIMES_PRODUCES ##= GUID # Install protocol - gEfiPropertiesTableGuid ## SOMETIMES_PRODUCES ##= SystemTable gEfiMemoryAttributesTableGuid ## SOMETIMES_PRODUCES ##= SystemTable gEfiEndOfDxeEventGroupGuid ## SOMETIMES_CONSUMES ##= Event gEfiHobMemoryAllocStackGuid ## SOMETIMES_CONSUMES ##= SystemTable @@ -180,7 +179,6 @@ [Pcd] gEfiMdeModulePkgTokenSpaceGuid.PcdMemoryProfileMemoryType = ## CONSUMES gEfiMdeModulePkgTokenSpaceGuid.PcdMemoryProfilePropertyMask = ## CONSUMES gEfiMdeModulePkgTokenSpaceGuid.PcdMemoryProfileDriverPath = ## CONSUMES - gEfiMdeModulePkgTokenSpaceGuid.PcdPropertiesTableEnable = ## CONSUMES gEfiMdeModulePkgTokenSpaceGuid.PcdImageProtectionPolicy = ## CONSUMES gEfiMdeModulePkgTokenSpaceGuid.PcdDxeNxMemoryProtectionPolicy = ## CONSUMES gEfiMdeModulePkgTokenSpaceGuid.PcdNullPointerDetectionPropertyMask = ## CONSUMES diff --git a/MdeModulePkg/Core/Dxe/Misc/MemoryAttributesTable.c b/MdeModule= Pkg/Core/Dxe/Misc/MemoryAttributesTable.c index ebdeb35079a2..0b0ed4413c28 100644 --- a/MdeModulePkg/Core/Dxe/Misc/MemoryAttributesTable.c +++ b/MdeModulePkg/Core/Dxe/Misc/MemoryAttributesTable.c @@ -18,7 +18,6 @@ SPDX-License-Identifier: BSD-2-Clause-Patent #include =20 #include -#include =20 #include "DxeMain.h" =20 @@ -64,7 +63,7 @@ CoreGetMemoryMapWithSeparatedImageSection ( OUT UINT32 *DescriptorVersion ); =20 -extern EFI_PROPERTIES_TABLE mPropertiesTable; +BOOLEAN mMemoryAttributesTableEnable =3D TRUE; EFI_MEMORY_ATTRIBUTES_TABLE *mMemoryAttributesTable =3D NULL; BOOLEAN mMemoryAttributesTableReadyToBoot =3D FALSE; =20 @@ -96,8 +95,8 @@ InstallMemoryAttributesTable ( return; } =20 - if ((mPropertiesTable.MemoryProtectionAttribute & EFI_PROPERTIES_RUNTIME= _MEMORY_PROTECTION_NON_EXECUTABLE_PE_DATA) =3D=3D 0) { - DEBUG ((EFI_D_VERBOSE, "MemoryProtectionAttribute NON_EXECUTABLE_PE_DA= TA is not set, ")); + if (!mMemoryAttributesTableEnable) { + DEBUG ((EFI_D_VERBOSE, "Cannot install Memory Attributes Table ")); DEBUG ((EFI_D_VERBOSE, "because Runtime Driver Section Alignment is no= t %dK.\n", RUNTIME_PAGE_ALLOCATION_GRANULARITY >> 10)); return ; } diff --git a/MdeModulePkg/Core/Dxe/Misc/MemoryProtection.c b/MdeModulePkg/C= ore/Dxe/Misc/MemoryProtection.c index 47edf86dfbf3..92a442f517b2 100644 --- a/MdeModulePkg/Core/Dxe/Misc/MemoryProtection.c +++ b/MdeModulePkg/Core/Dxe/Misc/MemoryProtection.c @@ -35,7 +35,6 @@ SPDX-License-Identifier: BSD-2-Clause-Patent =20 #include #include -#include =20 #include #include diff --git a/MdeModulePkg/Core/Dxe/Misc/PropertiesTable.c b/MdeModulePkg/Co= re/Dxe/Misc/PropertiesTable.c index 53bb6b7c912c..6ee8a8af9098 100644 --- a/MdeModulePkg/Core/Dxe/Misc/PropertiesTable.c +++ b/MdeModulePkg/Core/Dxe/Misc/PropertiesTable.c @@ -23,8 +23,6 @@ SPDX-License-Identifier: BSD-2-Clause-Patent #include #include =20 -#include - #include "DxeMain.h" #include "HeapGuard.h" =20 @@ -47,18 +45,12 @@ IMAGE_PROPERTIES_PRIVATE_DATA mImagePropertiesPrivateD= ata =3D { INITIALIZE_LIST_HEAD_VARIABLE (mImagePropertiesPrivateData.ImageRecordLi= st) }; =20 -EFI_PROPERTIES_TABLE mPropertiesTable =3D { - EFI_PROPERTIES_TABLE_VERSION, - sizeof(EFI_PROPERTIES_TABLE), - EFI_PROPERTIES_RUNTIME_MEMORY_PROTECTION_NON_EXECUTABLE_PE_DATA -}; - EFI_LOCK mPropertiesTableLock =3D EFI_INITIALIZE_LOCK_VARIABLE (= TPL_NOTIFY); =20 -BOOLEAN mPropertiesTableEnable; - BOOLEAN mPropertiesTableEndOfDxe =3D FALSE; =20 +extern BOOLEAN mMemoryAttributesTableEnable; + // // Below functions are for MemoryMap // @@ -359,11 +351,7 @@ SetNewRecord ( // // DATA // - if (!mPropertiesTableEnable) { - NewRecord->Type =3D TempRecord.Type; - } else { - NewRecord->Type =3D EfiRuntimeServicesData; - } + NewRecord->Type =3D TempRecord.Type; NewRecord->PhysicalStart =3D TempRecord.PhysicalStart; NewRecord->VirtualStart =3D 0; NewRecord->NumberOfPages =3D EfiSizeToPages(ImageRecordCodeSection->= CodeSegmentBase - NewRecord->PhysicalStart); @@ -376,11 +364,7 @@ SetNewRecord ( // // CODE // - if (!mPropertiesTableEnable) { - NewRecord->Type =3D TempRecord.Type; - } else { - NewRecord->Type =3D EfiRuntimeServicesCode; - } + NewRecord->Type =3D TempRecord.Type; NewRecord->PhysicalStart =3D ImageRecordCodeSection->CodeSegmentBase; NewRecord->VirtualStart =3D 0; NewRecord->NumberOfPages =3D EfiSizeToPages(ImageRecordCodeSection->= CodeSegmentSize); @@ -404,11 +388,7 @@ SetNewRecord ( // Final DATA // if (TempRecord.PhysicalStart < ImageEnd) { - if (!mPropertiesTableEnable) { - NewRecord->Type =3D TempRecord.Type; - } else { - NewRecord->Type =3D EfiRuntimeServicesData; - } + NewRecord->Type =3D TempRecord.Type; NewRecord->PhysicalStart =3D TempRecord.PhysicalStart; NewRecord->VirtualStart =3D 0; NewRecord->NumberOfPages =3D EfiSizeToPages (ImageEnd - TempRecord.Phy= sicalStart); @@ -519,14 +499,8 @@ SplitRecord ( // NewRecord =3D PREVIOUS_MEMORY_DESCRIPTOR (NewRecord, DescriptorSiz= e); IsLastRecordData =3D FALSE; - if (!mPropertiesTableEnable) { - if ((NewRecord->Attribute & EFI_MEMORY_XP) !=3D 0) { - IsLastRecordData =3D TRUE; - } - } else { - if (NewRecord->Type =3D=3D EfiRuntimeServicesData) { - IsLastRecordData =3D TRUE; - } + if ((NewRecord->Attribute & EFI_MEMORY_XP) !=3D 0) { + IsLastRecordData =3D TRUE; } if (IsLastRecordData) { // @@ -538,11 +512,7 @@ SplitRecord ( // Last record is CODE, create a new DATA entry. // NewRecord =3D NEXT_MEMORY_DESCRIPTOR (NewRecord, DescriptorSize); - if (!mPropertiesTableEnable) { - NewRecord->Type =3D TempRecord.Type; - } else { - NewRecord->Type =3D EfiRuntimeServicesData; - } + NewRecord->Type =3D TempRecord.Type; NewRecord->PhysicalStart =3D TempRecord.PhysicalStart; NewRecord->VirtualStart =3D 0; NewRecord->NumberOfPages =3D TempRecord.NumberOfPages; @@ -751,7 +721,7 @@ CoreGetMemoryMapWithSeparatedImageSection ( // // If PE code/data is not aligned, just return. // - if ((mPropertiesTable.MemoryProtectionAttribute & EFI_PROPERTIES_RUNTIME= _MEMORY_PROTECTION_NON_EXECUTABLE_PE_DATA) =3D=3D 0) { + if (!mMemoryAttributesTableEnable) { return CoreGetMemoryMap (MemoryMapSize, MemoryMap, MapKey, DescriptorS= ize, DescriptorVersion); } =20 @@ -803,12 +773,9 @@ SetPropertiesTableSectionAlignment ( ) { if (((SectionAlignment & (RUNTIME_PAGE_ALLOCATION_GRANULARITY - 1)) !=3D= 0) && - ((mPropertiesTable.MemoryProtectionAttribute & EFI_PROPERTIES_RUNTIM= E_MEMORY_PROTECTION_NON_EXECUTABLE_PE_DATA) !=3D 0)) { + mMemoryAttributesTableEnable) { DEBUG ((EFI_D_VERBOSE, "SetPropertiesTableSectionAlignment - Clear\n")= ); - mPropertiesTable.MemoryProtectionAttribute &=3D ~((UINT64)EFI_PROPERTI= ES_RUNTIME_MEMORY_PROTECTION_NON_EXECUTABLE_PE_DATA); - gBS->GetMemoryMap =3D CoreGetMemoryMap; - gBS->Hdr.CRC32 =3D 0; - gBS->CalculateCrc32 ((UINT8 *)gBS, gBS->Hdr.HeaderSize, &gBS->Hdr.CRC3= 2); + mMemoryAttributesTableEnable =3D FALSE; } } =20 @@ -1018,35 +985,6 @@ SortImageRecord ( } } =20 -/** - Dump image record. -**/ -STATIC -VOID -DumpImageRecord ( - VOID - ) -{ - IMAGE_PROPERTIES_RECORD *ImageRecord; - LIST_ENTRY *ImageRecordLink; - LIST_ENTRY *ImageRecordList; - UINTN Index; - - ImageRecordList =3D &mImagePropertiesPrivateData.ImageRecordList; - - for (ImageRecordLink =3D ImageRecordList->ForwardLink, Index=3D 0; - ImageRecordLink !=3D ImageRecordList; - ImageRecordLink =3D ImageRecordLink->ForwardLink, Index++) { - ImageRecord =3D CR ( - ImageRecordLink, - IMAGE_PROPERTIES_RECORD, - Link, - IMAGE_PROPERTIES_RECORD_SIGNATURE - ); - DEBUG ((EFI_D_VERBOSE, " Image[%d]: 0x%016lx - 0x%016lx\n", Index, Im= ageRecord->ImageBase, ImageRecord->ImageSize)); - } -} - /** Insert image record. =20 @@ -1323,29 +1261,6 @@ InstallPropertiesTable ( ) { mPropertiesTableEndOfDxe =3D TRUE; - if (PcdGetBool (PcdPropertiesTableEnable)) { - EFI_STATUS Status; - - Status =3D gBS->InstallConfigurationTable (&gEfiPropertiesTableGuid, &= mPropertiesTable); - ASSERT_EFI_ERROR (Status); - - DEBUG ((EFI_D_INFO, "MemoryProtectionAttribute - 0x%016lx\n", mPropert= iesTable.MemoryProtectionAttribute)); - if ((mPropertiesTable.MemoryProtectionAttribute & EFI_PROPERTIES_RUNTI= ME_MEMORY_PROTECTION_NON_EXECUTABLE_PE_DATA) =3D=3D 0) { - DEBUG ((EFI_D_ERROR, "MemoryProtectionAttribute NON_EXECUTABLE_PE_DA= TA is not set, ")); - DEBUG ((EFI_D_ERROR, "because Runtime Driver Section Alignment is no= t %dK.\n", RUNTIME_PAGE_ALLOCATION_GRANULARITY >> 10)); - return ; - } - - gBS->GetMemoryMap =3D CoreGetMemoryMapWithSeparatedImageSection; - gBS->Hdr.CRC32 =3D 0; - gBS->CalculateCrc32 ((UINT8 *)gBS, gBS->Hdr.HeaderSize, &gBS->Hdr.CRC3= 2); - - DEBUG ((EFI_D_VERBOSE, "Total Image Count - 0x%x\n", mImagePropertiesP= rivateData.ImageRecordCount)); - DEBUG ((EFI_D_VERBOSE, "Dump ImageRecord:\n")); - DumpImageRecord (); - - mPropertiesTableEnable =3D TRUE; - } } =20 /** diff --git a/MdeModulePkg/MdeModulePkg.dec b/MdeModulePkg/MdeModulePkg.dec index 91a3c608231c..aeb8b820db1b 100644 --- a/MdeModulePkg/MdeModulePkg.dec +++ b/MdeModulePkg/MdeModulePkg.dec @@ -1866,30 +1866,6 @@ [PcdsFixedAtBuild, PcdsPatchableInModule, PcdsDynami= c, PcdsDynamicEx] # @Prompt Flag to request system reboot after processing capsule. gEfiMdeModulePkgTokenSpaceGuid.PcdSystemRebootAfterCapsuleProcessFlag|0x= 0001|UINT16|0x0000006d =20 - ## Publish PropertiesTable or not. - # - # If this PCD is TRUE, DxeCore publishs PropertiesTable. - # DxeCore evaluates if all runtime drivers has 4K aligned PE sections. I= f all - # PE sections in runtime drivers are 4K aligned, DxeCore sets BIT0 in - # PropertiesTable. Or DxeCore clears BIT0 in PropertiesTable. - # If this PCD is FALSE, DxeCore does not publish PropertiesTable. - # - # If PropertiesTable has BIT0 set, DxeCore uses below policy in UEFI mem= ory map: - # 1) Use EfiRuntimeServicesCode for runtime driver PE image code secti= on and - # use EfiRuntimeServicesData for runtime driver PE image header and= other section. - # 2) Set EfiRuntimeServicesCode to be EFI_MEMORY_RO. - # 3) Set EfiRuntimeServicesData to be EFI_MEMORY_XP. - # 4) Set EfiMemoryMappedIO and EfiMemoryMappedIOPortSpace to be EFI_ME= MORY_XP. - # - # NOTE: Platform need gurantee this PCD is set correctly. Platform shoul= d set - # this PCD to be TURE if and only if all runtime driver has seperated Co= de/Data - # section. If PE code/data sections are merged, the result is unpredicta= ble. - # - # UEFI 2.6 specification does not recommend to use this BIT0 attribute. - # - # @Prompt Publish UEFI PropertiesTable. - gEfiMdeModulePkgTokenSpaceGuid.PcdPropertiesTableEnable|FALSE|BOOLEAN|0x= 0000006e - ## Default OEM ID for ACPI table creation, its length must be 0x6 bytes = to follow ACPI specification. # @Prompt Default OEM ID for ACPI table creation. gEfiMdeModulePkgTokenSpaceGuid.PcdAcpiDefaultOemId|"INTEL "|VOID*|0x3000= 1034 diff --git a/MdeModulePkg/MdeModulePkg.uni b/MdeModulePkg/MdeModulePkg.uni index 2c856ed07333..2007e0596c4f 100644 --- a/MdeModulePkg/MdeModulePkg.uni +++ b/MdeModulePkg/MdeModulePkg.uni @@ -891,27 +891,6 @@ #string STR_gEfiMdeModulePkgTokenSpaceGuid_PcdFrontPageFormSetGuid_HELP #= language en-US "This PCD points to the front page formset GUID\n" = "Compare the FormsetGuid or ClassGuid with this PCD value ca= n detect whether in front page" =20 -#string STR_gEfiMdeModulePkgTokenSpaceGuid_PcdPropertiesTableEnable_PROMPT= #language en-US "Publish UEFI PropertiesTable." - -#string STR_gEfiMdeModulePkgTokenSpaceGuid_PcdPropertiesTableEnable_HELP = #language en-US "Publish PropertiesTable or not.\n" - = "\n" - = "If this PCD is TRUE, DxeCore publishs PropertiesTable.\n" - = "DxeCore evaluates if all runtime drivers has 4K aligned PE= sections. If all\n" - = "PE sections in runtime drivers are 4K aligned, DxeCore set= s BIT0 in\n" - = "PropertiesTable. Or DxeCore clears BIT0 in PropertiesTable= .\n" - = "If this PCD is FALSE, DxeCore does not publish PropertiesT= able.\n" - = "\n" - = "If PropertiesTable has BIT0 set, DxeCore uses below policy= in UEFI memory map:\n" - = "1) Use EfiRuntimeServicesCode for runtime driver PE image = code section and\n" - = "use EfiRuntimeServicesData for runtime driver PE image hea= der and other section.\n" - = "2) Set EfiRuntimeServicesCode to be EFI_MEMORY_RO.\n" - = "3) Set EfiRuntimeServicesData to be EFI_MEMORY_XP.\n" - = "4) Set EfiMemoryMappedIO and EfiMemoryMappedIOPortSpace to= be EFI_MEMORY_XP.\n" - = "\n" - = "NOTE: Platform need gurantee this PCD is set correctly. Pl= atform should set\n" - = "this PCD to be TURE if and only if all runtime driver has = seperated Code/Data\n" - = "section. If PE code/data sections are merged, the result i= s unpredictable.\n" - #string STR_gEfiMdeModulePkgTokenSpaceGuid_PcdShadowPeimOnBoot_HELP #lang= uage en-US "Indicates if to shadow PEIM and PeiCore after memory is ready.<= BR>
\n" = "This PCD is used on other boot path except for S3 boot.\n" = "TRUE - Shadow PEIM and PeiCore after memory is ready.
\n" --=20 2.17.1 -=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D- Groups.io Links: You receive all messages sent to this group. View/Reply Online (#56389): https://edk2.groups.io/g/devel/message/56389 Mute This Topic: https://groups.io/mt/72560883/1787277 Group Owner: devel+owner@edk2.groups.io Unsubscribe: https://edk2.groups.io/g/devel/unsub [importer@patchew.org] -=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D- From nobody Wed May 1 22:04:29 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of groups.io designates 66.175.222.12 as permitted sender) client-ip=66.175.222.12; envelope-from=bounce+27952+56390+1787277+3901457@groups.io; helo=web01.groups.io; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of groups.io designates 66.175.222.12 as permitted sender) smtp.mailfrom=bounce+27952+56390+1787277+3901457@groups.io; dmarc=fail(p=none dis=none) header.from=linaro.org ARC-Seal: i=1; a=rsa-sha256; t=1585218295; cv=none; d=zohomail.com; s=zohoarc; b=aT7lTl9hl4Nd0IYohCyYgyejIYKy825xXkQ7cUihGm/ofnS9kys4LrP7hiPuE5FwcCBN9sZ+M5/oCa3trnUGrIYy2I43S1oddi8QxTG/AX0DsKSw8L7xNaTTF3Zqa19KOY4gck1hBV4yTusvtx3uPDmu3l8YmzPYOWemwoKZcck= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1585218295; h=Cc:Date:From:In-Reply-To:List-Id:List-Unsubscribe:Message-ID:Reply-To:References:Sender:Subject:To; bh=OrMlqW2e1U8++7oLJnNKei8NFWcLj1smhCrADwQxHqk=; b=etCNCkrWTfe+wPEXI2uPMbaJ778YtPkrEBjzZctGuZ9i5fX/4lMj2nX3YZgYoKpPjNYxcpYMK8K+3yEiEprPgyuv9to4/ckUAKi2JgLC0VQc0s1645MXKsFVmjQlh0G11ZxhxZ2He7iuvKjR23x4VGNlmLWuFp4lVKo46p0cFvo= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of groups.io designates 66.175.222.12 as permitted sender) smtp.mailfrom=bounce+27952+56390+1787277+3901457@groups.io; dmarc=fail header.from= (p=none dis=none) header.from= Received: from web01.groups.io (web01.groups.io [66.175.222.12]) by mx.zohomail.com with SMTPS id 1585218295210618.302964064764; Thu, 26 Mar 2020 03:24:55 -0700 (PDT) Return-Path: X-Received: by 127.0.0.2 with SMTP id NxKeYY1788612xNId0EL1XGb; Thu, 26 Mar 2020 03:24:54 -0700 X-Received: from mail-wr1-f68.google.com (mail-wr1-f68.google.com [209.85.221.68]) by mx.groups.io with SMTP id smtpd.web10.33958.1585218292635113719 for ; Thu, 26 Mar 2020 03:24:52 -0700 X-Received: by mail-wr1-f68.google.com with SMTP id m11so1136481wrx.10 for ; Thu, 26 Mar 2020 03:24:52 -0700 (PDT) X-Gm-Message-State: LMi2SaUzQ1mbq0X9LqHGV2zzx1787277AA= X-Google-Smtp-Source: ADFU+vuVi3pwQLrbYJ4hbJJqvqvkMNknRFALBGuBZIpOL1Pz3NOwyyQKS4kRvbhAjw94p6HEoPeSwQ== X-Received: by 2002:adf:df04:: with SMTP id y4mr8412547wrl.318.1585218290990; Thu, 26 Mar 2020 03:24:50 -0700 (PDT) X-Received: from e123331-lin.home (amontpellier-657-1-18-247.w109-210.abo.wanadoo.fr. [109.210.65.247]) by smtp.gmail.com with ESMTPSA id d5sm3124511wrh.40.2020.03.26.03.24.49 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 26 Mar 2020 03:24:50 -0700 (PDT) From: "Ard Biesheuvel" To: devel@edk2.groups.io Cc: Ard Biesheuvel , Laszlo Ersek , Leif Lindholm , Michael D Kinney , Ray Ni , Jiewen Yao , Bret Barkelew Subject: [edk2-devel] [PATCH 3/4] MdePkg: remove PropertiesTable GUID Date: Thu, 26 Mar 2020 11:24:42 +0100 Message-Id: <20200326102443.748-4-ard.biesheuvel@linaro.org> In-Reply-To: <20200326102443.748-1-ard.biesheuvel@linaro.org> References: <20200326102443.748-1-ard.biesheuvel@linaro.org> Precedence: Bulk List-Unsubscribe: Sender: devel@edk2.groups.io List-Id: Mailing-List: list devel@edk2.groups.io; contact devel+owner@edk2.groups.io Reply-To: devel@edk2.groups.io,ard.biesheuvel@linaro.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=groups.io; q=dns/txt; s=20140610; t=1585218294; bh=6LXV6SxsKp5pNi1fAVOunxZgNkbX1wkvX0H8ciHcoSY=; h=Cc:Date:From:Reply-To:Subject:To; b=YZcQQ+hVEpaPFGY8sEiVwI8M0fbMVBFtO4qYUczH/PUiUbU8ElNcsBTv/DDA+m7InxK dW9bJ5XPm5yqniC4OLXS+KaLYQ8XCtJQNLpVdqyE7afbo0K04qMtuhSARV9AQgvUaDFXh lMFjdC/r0uXUDfBEfGkTJ0yvK+2rymXi3cE= X-ZohoMail-DKIM: pass (identity @groups.io) Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Remove the GUID definition that describes the deprecated properties table, which is no longer generated by the DXE core. Link: https://bugzilla.tianocore.org/show_bug.cgi?id=3D2633 Signed-off-by: Ard Biesheuvel Acked-by: Jiewen Yao Reviewed-by: Bret Barkelew Reviewed-by: Dandan Bi for patch [PATCH 2/4] [PATCH 3= /4] [PATCH 4/4]. Reviewed-by: Jian J Wang --- MdePkg/Include/Guid/PropertiesTable.h | 31 -------------------- MdePkg/MdePkg.dec | 3 -- 2 files changed, 34 deletions(-) diff --git a/MdePkg/Include/Guid/PropertiesTable.h b/MdePkg/Include/Guid/Pr= opertiesTable.h deleted file mode 100644 index 56dbc7f96138..000000000000 --- a/MdePkg/Include/Guid/PropertiesTable.h +++ /dev/null @@ -1,31 +0,0 @@ -/** @file - GUIDs used for UEFI Properties Table in the UEFI 2.5 specification. - - Copyright (c) 2015, Intel Corporation. All rights reserved.
- SPDX-License-Identifier: BSD-2-Clause-Patent - -**/ - -#ifndef __EFI_PROPERTIES_TABLE_H__ -#define __EFI_PROPERTIES_TABLE_H__ - -#define EFI_PROPERTIES_TABLE_GUID {\ - 0x880aaca3, 0x4adc, 0x4a04, {0x90, 0x79, 0xb7, 0x47, 0x34, 0x8, 0x25, 0x= e5} \ -} - -typedef struct { - UINT32 Version; - UINT32 Length; - UINT64 MemoryProtectionAttribute; -} EFI_PROPERTIES_TABLE; - -#define EFI_PROPERTIES_TABLE_VERSION 0x00010000 - -// -// Memory attribute (Not defined bit is reserved) -// -#define EFI_PROPERTIES_RUNTIME_MEMORY_PROTECTION_NON_EXECUTABLE_PE_DATA = 0x1 - -extern EFI_GUID gEfiPropertiesTableGuid; - -#endif diff --git a/MdePkg/MdePkg.dec b/MdePkg/MdePkg.dec index ac1f5339af06..6c37c2181c62 100644 --- a/MdePkg/MdePkg.dec +++ b/MdePkg/MdePkg.dec @@ -578,9 +578,6 @@ [Guids] # GUIDs defined in UEFI2.5 # =20 - ## Include/Guid/PropertiesTable.h - gEfiPropertiesTableGuid =3D { 0x880aaca3, 0x4adc, 0x4a04, {0x90, = 0x79, 0xb7, 0x47, 0x34, 0x8, 0x25, 0xe5 }} - ## Include/Guid/SystemResourceTable.h gEfiSystemResourceTableGuid =3D { 0xb122a263, 0x3661, 0x4f68, {0x99, = 0x29, 0x78, 0xf8, 0xb0, 0xd6, 0x21, 0x80 }} =20 --=20 2.17.1 -=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D- Groups.io Links: You receive all messages sent to this group. View/Reply Online (#56390): https://edk2.groups.io/g/devel/message/56390 Mute This Topic: https://groups.io/mt/72560884/1787277 Group Owner: devel+owner@edk2.groups.io Unsubscribe: https://edk2.groups.io/g/devel/unsub [importer@patchew.org] -=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D- From nobody Wed May 1 22:04:29 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of groups.io designates 66.175.222.12 as permitted sender) client-ip=66.175.222.12; envelope-from=bounce+27952+56391+1787277+3901457@groups.io; helo=web01.groups.io; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of groups.io designates 66.175.222.12 as permitted sender) smtp.mailfrom=bounce+27952+56391+1787277+3901457@groups.io; dmarc=fail(p=none dis=none) header.from=linaro.org ARC-Seal: i=1; a=rsa-sha256; t=1585218298; cv=none; d=zohomail.com; s=zohoarc; b=KcUetUgSuLfaalMSPr55jILRKeO1Jatxf9yN9hphghRrgZ8qsCKPWtYnGAOSaB/jhhAJtFY0GpjqrOL5Wu2GyZoSZwL7LRnQTWWe0ZcbiOiMECRGLXiTJ5sQ0OMN0eddoye8s483qYy1PVXPoJFPHEN0eaf8lpRV+2MBxya015w= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1585218298; h=Cc:Date:From:In-Reply-To:List-Id:List-Unsubscribe:Message-ID:Reply-To:References:Sender:Subject:To; bh=eSDtgGqmNzHpVYUF+yPKmMfM45GlZaGFhtULwyxdOsU=; b=hm6PPIZWN4BKLRnrKrVSoQRy4UFSwpn10Y8v0o1wtZNxcJpVZFyr11gq593+wJ0jdg388I6HfTOv2mNDs1YF2pVjBN51MdxujCB0t9tJminQucTM8PEYmglp23UfE6eVzy3XwMwvHxmreGkSeoj6Q1GCx961YwX313SdnV/T2A0= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of groups.io designates 66.175.222.12 as permitted sender) smtp.mailfrom=bounce+27952+56391+1787277+3901457@groups.io; dmarc=fail header.from= (p=none dis=none) header.from= Received: from web01.groups.io (web01.groups.io [66.175.222.12]) by mx.zohomail.com with SMTPS id 1585218298501827.4461471105799; Thu, 26 Mar 2020 03:24:58 -0700 (PDT) Return-Path: X-Received: by 127.0.0.2 with SMTP id VE0IYY1788612xuUh8eu7g76; Thu, 26 Mar 2020 03:24:58 -0700 X-Received: from mail-wm1-f65.google.com (mail-wm1-f65.google.com [209.85.128.65]) by mx.groups.io with SMTP id smtpd.web10.33959.1585218296853976238 for ; Thu, 26 Mar 2020 03:24:57 -0700 X-Received: by mail-wm1-f65.google.com with SMTP id m3so6357089wmi.0 for ; Thu, 26 Mar 2020 03:24:56 -0700 (PDT) X-Gm-Message-State: g0KKRfBU1F78se8CuatvAYNlx1787277AA= X-Google-Smtp-Source: ADFU+vthCA7DNVhYFAV65xPgcyJlKjLH/R95mfsmXiPa2wICBbGYEpFqyUW2hq7B7jKd2zoTmAhZfA== X-Received: by 2002:a1c:4486:: with SMTP id r128mr2491277wma.32.1585218292241; Thu, 26 Mar 2020 03:24:52 -0700 (PDT) X-Received: from e123331-lin.home (amontpellier-657-1-18-247.w109-210.abo.wanadoo.fr. [109.210.65.247]) by smtp.gmail.com with ESMTPSA id d5sm3124511wrh.40.2020.03.26.03.24.51 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 26 Mar 2020 03:24:51 -0700 (PDT) From: "Ard Biesheuvel" To: devel@edk2.groups.io Cc: Ard Biesheuvel , Laszlo Ersek , Leif Lindholm , Michael D Kinney , Ray Ni , Jiewen Yao , Bret Barkelew Subject: [edk2-devel] [PATCH 4/4] MdeModulePkg/DxeCore: merge properties table routines into MAT handling Date: Thu, 26 Mar 2020 11:24:43 +0100 Message-Id: <20200326102443.748-5-ard.biesheuvel@linaro.org> In-Reply-To: <20200326102443.748-1-ard.biesheuvel@linaro.org> References: <20200326102443.748-1-ard.biesheuvel@linaro.org> Precedence: Bulk List-Unsubscribe: Sender: devel@edk2.groups.io List-Id: Mailing-List: list devel@edk2.groups.io; contact devel+owner@edk2.groups.io Reply-To: devel@edk2.groups.io,ard.biesheuvel@linaro.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=groups.io; q=dns/txt; s=20140610; t=1585218298; bh=wZXOxi7vuxO9FsWRb+NRRC5rrHfekc5ksnPgr9tX3RI=; h=Cc:Date:From:Reply-To:Subject:To; b=VxGmRovuvfD6IpkLCAshE5dLpxQEJXfWYW0IDYEw2hmfiesi5SuA3j3REIm1u3Gw7Wn sYWXvXeiiMF5m0aMusDpYgSnSb/kNmSZI73inckYIYkGM5SQjZLcpcAK0Vvt6iy48yuAq dxLSpx7loritTrXG1l4ImrR+poUMr50PNfg= X-ZohoMail-DKIM: pass (identity @groups.io) Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" The memory attributes table handling code shared some parts of the properties table handling code when it was introduced. The latter has been dropped, and so we can merge the remaining pieces into the former. Link: https://bugzilla.tianocore.org/show_bug.cgi?id=3D2633 Signed-off-by: Ard Biesheuvel Acked-by: Jiewen Yao Reviewed-by: Dandan Bi for patch [PATCH 2/4] [PATCH 3= /4] [PATCH 4/4]. Reviewed-by: Jian J Wang --- MdeModulePkg/Core/Dxe/DxeMain.h | 9 - MdeModulePkg/Core/Dxe/DxeMain.inf | 1 - MdeModulePkg/Core/Dxe/DxeMain/DxeMain.c | 1 - MdeModulePkg/Core/Dxe/Misc/MemoryAttributesTable.c | 1219 ++++++++++++++++= ++ MdeModulePkg/Core/Dxe/Misc/PropertiesTable.c | 1288 ----------------= ---- 5 files changed, 1219 insertions(+), 1299 deletions(-) diff --git a/MdeModulePkg/Core/Dxe/DxeMain.h b/MdeModulePkg/Core/Dxe/DxeMai= n.h index 6a64852730fb..9bd3c0d08411 100644 --- a/MdeModulePkg/Core/Dxe/DxeMain.h +++ b/MdeModulePkg/Core/Dxe/DxeMain.h @@ -2815,15 +2815,6 @@ CoreUpdateMemoryAttributes ( IN UINT64 NewAttributes ); =20 -/** - Initialize PropertiesTable support. -**/ -VOID -EFIAPI -CoreInitializePropertiesTable ( - VOID - ); - /** Initialize MemoryAttrubutesTable support. **/ diff --git a/MdeModulePkg/Core/Dxe/DxeMain.inf b/MdeModulePkg/Core/Dxe/DxeM= ain.inf index 75e0a968f0cf..1d4b11dc7318 100644 --- a/MdeModulePkg/Core/Dxe/DxeMain.inf +++ b/MdeModulePkg/Core/Dxe/DxeMain.inf @@ -34,7 +34,6 @@ [Sources] Misc/Stall.c Misc/SetWatchdogTimer.c Misc/InstallConfigurationTable.c - Misc/PropertiesTable.c Misc/MemoryAttributesTable.c Misc/MemoryProtection.c Library/Library.c diff --git a/MdeModulePkg/Core/Dxe/DxeMain/DxeMain.c b/MdeModulePkg/Core/Dx= e/DxeMain/DxeMain.c index 514d1aa75ada..5ee4cd10b436 100644 --- a/MdeModulePkg/Core/Dxe/DxeMain/DxeMain.c +++ b/MdeModulePkg/Core/Dxe/DxeMain/DxeMain.c @@ -423,7 +423,6 @@ DxeMain ( =20 MemoryProfileInstallProtocol (); =20 - CoreInitializePropertiesTable (); CoreInitializeMemoryAttributesTable (); CoreInitializeMemoryProtection (); =20 diff --git a/MdeModulePkg/Core/Dxe/Misc/MemoryAttributesTable.c b/MdeModule= Pkg/Core/Dxe/Misc/MemoryAttributesTable.c index 0b0ed4413c28..bb1c48ab6c4e 100644 --- a/MdeModulePkg/Core/Dxe/Misc/MemoryAttributesTable.c +++ b/MdeModulePkg/Core/Dxe/Misc/MemoryAttributesTable.c @@ -20,6 +20,7 @@ SPDX-License-Identifier: BSD-2-Clause-Patent #include =20 #include "DxeMain.h" +#include "HeapGuard.h" =20 /** This function for GetMemoryMap() with properties table capability. @@ -63,7 +64,29 @@ CoreGetMemoryMapWithSeparatedImageSection ( OUT UINT32 *DescriptorVersion ); =20 +#define PREVIOUS_MEMORY_DESCRIPTOR(MemoryDescriptor, Size) \ + ((EFI_MEMORY_DESCRIPTOR *)((UINT8 *)(MemoryDescriptor) - (Size))) + +#define IMAGE_PROPERTIES_PRIVATE_DATA_SIGNATURE SIGNATURE_32 ('I','P','P',= 'D') + +typedef struct { + UINT32 Signature; + UINTN ImageRecordCount; + UINTN CodeSegmentCountMax; + LIST_ENTRY ImageRecordList; +} IMAGE_PROPERTIES_PRIVATE_DATA; + +STATIC IMAGE_PROPERTIES_PRIVATE_DATA mImagePropertiesPrivateData =3D { + IMAGE_PROPERTIES_PRIVATE_DATA_SIGNATURE, + 0, + 0, + INITIALIZE_LIST_HEAD_VARIABLE (mImagePropertiesPrivateData.ImageRecordLi= st) +}; + +STATIC EFI_LOCK mMemoryAttributesTableLock =3D EFI_INITIALIZE= _LOCK_VARIABLE (TPL_NOTIFY); + BOOLEAN mMemoryAttributesTableEnable =3D TRUE; +BOOLEAN mMemoryAttributesTableEndOfDxe =3D FALSE; EFI_MEMORY_ATTRIBUTES_TABLE *mMemoryAttributesTable =3D NULL; BOOLEAN mMemoryAttributesTableReadyToBoot =3D FALSE; =20 @@ -247,6 +270,7 @@ InstallMemoryAttributesTableOnEndOfDxe ( IN VOID *Context ) { + mMemoryAttributesTableEndOfDxe =3D TRUE; InstallMemoryAttributesTable (); } =20 @@ -293,3 +317,1198 @@ CoreInitializeMemoryAttributesTable ( ASSERT_EFI_ERROR (Status); return ; } + +// +// Below functions are for MemoryMap +// + +/** + Converts a number of EFI_PAGEs to a size in bytes. + + NOTE: Do not use EFI_PAGES_TO_SIZE because it handles UINTN only. + + @param Pages The number of EFI_PAGES. + + @return The number of bytes associated with the number of EFI_PAGEs spe= cified + by Pages. +**/ +STATIC +UINT64 +EfiPagesToSize ( + IN UINT64 Pages + ) +{ + return LShiftU64 (Pages, EFI_PAGE_SHIFT); +} + +/** + Converts a size, in bytes, to a number of EFI_PAGESs. + + NOTE: Do not use EFI_SIZE_TO_PAGES because it handles UINTN only. + + @param Size A size in bytes. + + @return The number of EFI_PAGESs associated with the number of bytes sp= ecified + by Size. + +**/ +STATIC +UINT64 +EfiSizeToPages ( + IN UINT64 Size + ) +{ + return RShiftU64 (Size, EFI_PAGE_SHIFT) + ((((UINTN)Size) & EFI_PAGE_MAS= K) ? 1 : 0); +} + +/** + Acquire memory lock on mMemoryAttributesTableLock. +**/ +STATIC +VOID +CoreAcquiremMemoryAttributesTableLock ( + VOID + ) +{ + CoreAcquireLock (&mMemoryAttributesTableLock); +} + +/** + Release memory lock on mMemoryAttributesTableLock. +**/ +STATIC +VOID +CoreReleasemMemoryAttributesTableLock ( + VOID + ) +{ + CoreReleaseLock (&mMemoryAttributesTableLock); +} + +/** + Sort memory map entries based upon PhysicalStart, from low to high. + + @param MemoryMap A pointer to the buffer in which firmware= places + the current memory map. + @param MemoryMapSize Size, in bytes, of the MemoryMap buffer. + @param DescriptorSize Size, in bytes, of an individual EFI_MEMO= RY_DESCRIPTOR. +**/ +STATIC +VOID +SortMemoryMap ( + IN OUT EFI_MEMORY_DESCRIPTOR *MemoryMap, + IN UINTN MemoryMapSize, + IN UINTN DescriptorSize + ) +{ + EFI_MEMORY_DESCRIPTOR *MemoryMapEntry; + EFI_MEMORY_DESCRIPTOR *NextMemoryMapEntry; + EFI_MEMORY_DESCRIPTOR *MemoryMapEnd; + EFI_MEMORY_DESCRIPTOR TempMemoryMap; + + MemoryMapEntry =3D MemoryMap; + NextMemoryMapEntry =3D NEXT_MEMORY_DESCRIPTOR (MemoryMapEntry, Descripto= rSize); + MemoryMapEnd =3D (EFI_MEMORY_DESCRIPTOR *) ((UINT8 *) MemoryMap + Memory= MapSize); + while (MemoryMapEntry < MemoryMapEnd) { + while (NextMemoryMapEntry < MemoryMapEnd) { + if (MemoryMapEntry->PhysicalStart > NextMemoryMapEntry->PhysicalStar= t) { + CopyMem (&TempMemoryMap, MemoryMapEntry, sizeof(EFI_MEMORY_DESCRIP= TOR)); + CopyMem (MemoryMapEntry, NextMemoryMapEntry, sizeof(EFI_MEMORY_DES= CRIPTOR)); + CopyMem (NextMemoryMapEntry, &TempMemoryMap, sizeof(EFI_MEMORY_DES= CRIPTOR)); + } + + NextMemoryMapEntry =3D NEXT_MEMORY_DESCRIPTOR (NextMemoryMapEntry, D= escriptorSize); + } + + MemoryMapEntry =3D NEXT_MEMORY_DESCRIPTOR (MemoryMapEntry, Descri= ptorSize); + NextMemoryMapEntry =3D NEXT_MEMORY_DESCRIPTOR (MemoryMapEntry, Descri= ptorSize); + } + + return ; +} + +/** + Merge continous memory map entries whose have same attributes. + + @param MemoryMap A pointer to the buffer in which firmware= places + the current memory map. + @param MemoryMapSize A pointer to the size, in bytes, of the + MemoryMap buffer. On input, this is the s= ize of + the current memory map. On output, + it is the size of new memory map after me= rge. + @param DescriptorSize Size, in bytes, of an individual EFI_MEMO= RY_DESCRIPTOR. +**/ +VOID +MergeMemoryMap ( + IN OUT EFI_MEMORY_DESCRIPTOR *MemoryMap, + IN OUT UINTN *MemoryMapSize, + IN UINTN DescriptorSize + ) +{ + EFI_MEMORY_DESCRIPTOR *MemoryMapEntry; + EFI_MEMORY_DESCRIPTOR *MemoryMapEnd; + UINT64 MemoryBlockLength; + EFI_MEMORY_DESCRIPTOR *NewMemoryMapEntry; + EFI_MEMORY_DESCRIPTOR *NextMemoryMapEntry; + + MemoryMapEntry =3D MemoryMap; + NewMemoryMapEntry =3D MemoryMap; + MemoryMapEnd =3D (EFI_MEMORY_DESCRIPTOR *) ((UINT8 *) MemoryMap + *Memor= yMapSize); + while ((UINTN)MemoryMapEntry < (UINTN)MemoryMapEnd) { + CopyMem (NewMemoryMapEntry, MemoryMapEntry, sizeof(EFI_MEMORY_DESCRIPT= OR)); + NextMemoryMapEntry =3D NEXT_MEMORY_DESCRIPTOR (MemoryMapEntry, Descrip= torSize); + + do { + MergeGuardPages (NewMemoryMapEntry, NextMemoryMapEntry->PhysicalStar= t); + MemoryBlockLength =3D (UINT64) (EfiPagesToSize (NewMemoryMapEntry->N= umberOfPages)); + if (((UINTN)NextMemoryMapEntry < (UINTN)MemoryMapEnd) && + (NewMemoryMapEntry->Type =3D=3D NextMemoryMapEntry->Type) && + (NewMemoryMapEntry->Attribute =3D=3D NextMemoryMapEntry->Attribu= te) && + ((NewMemoryMapEntry->PhysicalStart + MemoryBlockLength) =3D=3D N= extMemoryMapEntry->PhysicalStart)) { + NewMemoryMapEntry->NumberOfPages +=3D NextMemoryMapEntry->NumberOf= Pages; + NextMemoryMapEntry =3D NEXT_MEMORY_DESCRIPTOR (NextMemoryMapEntry,= DescriptorSize); + continue; + } else { + MemoryMapEntry =3D PREVIOUS_MEMORY_DESCRIPTOR (NextMemoryMapEntry,= DescriptorSize); + break; + } + } while (TRUE); + + MemoryMapEntry =3D NEXT_MEMORY_DESCRIPTOR (MemoryMapEntry, DescriptorS= ize); + NewMemoryMapEntry =3D NEXT_MEMORY_DESCRIPTOR (NewMemoryMapEntry, Descr= iptorSize); + } + + *MemoryMapSize =3D (UINTN)NewMemoryMapEntry - (UINTN)MemoryMap; + + return ; +} + +/** + Enforce memory map attributes. + This function will set EfiRuntimeServicesData/EfiMemoryMappedIO/EfiMemor= yMappedIOPortSpace to be EFI_MEMORY_XP. + + @param MemoryMap A pointer to the buffer in which firmware= places + the current memory map. + @param MemoryMapSize Size, in bytes, of the MemoryMap buffer. + @param DescriptorSize Size, in bytes, of an individual EFI_MEMO= RY_DESCRIPTOR. +**/ +STATIC +VOID +EnforceMemoryMapAttribute ( + IN OUT EFI_MEMORY_DESCRIPTOR *MemoryMap, + IN UINTN MemoryMapSize, + IN UINTN DescriptorSize + ) +{ + EFI_MEMORY_DESCRIPTOR *MemoryMapEntry; + EFI_MEMORY_DESCRIPTOR *MemoryMapEnd; + + MemoryMapEntry =3D MemoryMap; + MemoryMapEnd =3D (EFI_MEMORY_DESCRIPTOR *) ((UINT8 *) MemoryMap + Memo= ryMapSize); + while ((UINTN)MemoryMapEntry < (UINTN)MemoryMapEnd) { + switch (MemoryMapEntry->Type) { + case EfiRuntimeServicesCode: + // do nothing + break; + case EfiRuntimeServicesData: + case EfiMemoryMappedIO: + case EfiMemoryMappedIOPortSpace: + MemoryMapEntry->Attribute |=3D EFI_MEMORY_XP; + break; + case EfiReservedMemoryType: + case EfiACPIMemoryNVS: + break; + } + + MemoryMapEntry =3D NEXT_MEMORY_DESCRIPTOR (MemoryMapEntry, DescriptorS= ize); + } + + return ; +} + +/** + Return the first image record, whose [ImageBase, ImageSize] covered by [= Buffer, Length]. + + @param Buffer Start Address + @param Length Address length + + @return first image record covered by [buffer, length] +**/ +STATIC +IMAGE_PROPERTIES_RECORD * +GetImageRecordByAddress ( + IN EFI_PHYSICAL_ADDRESS Buffer, + IN UINT64 Length + ) +{ + IMAGE_PROPERTIES_RECORD *ImageRecord; + LIST_ENTRY *ImageRecordLink; + LIST_ENTRY *ImageRecordList; + + ImageRecordList =3D &mImagePropertiesPrivateData.ImageRecordList; + + for (ImageRecordLink =3D ImageRecordList->ForwardLink; + ImageRecordLink !=3D ImageRecordList; + ImageRecordLink =3D ImageRecordLink->ForwardLink) { + ImageRecord =3D CR ( + ImageRecordLink, + IMAGE_PROPERTIES_RECORD, + Link, + IMAGE_PROPERTIES_RECORD_SIGNATURE + ); + + if ((Buffer <=3D ImageRecord->ImageBase) && + (Buffer + Length >=3D ImageRecord->ImageBase + ImageRecord->ImageS= ize)) { + return ImageRecord; + } + } + + return NULL; +} + +/** + Set the memory map to new entries, according to one old entry, + based upon PE code section and data section in image record + + @param ImageRecord An image record whose [ImageBase, ImageSi= ze] covered + by old memory map entry. + @param NewRecord A pointer to several new memory map entri= es. + The caller gurantee the buffer size be 1 + + (SplitRecordCount * DescriptorSize) calcu= lated + below. + @param OldRecord A pointer to one old memory map entry. + @param DescriptorSize Size, in bytes, of an individual EFI_MEMO= RY_DESCRIPTOR. +**/ +STATIC +UINTN +SetNewRecord ( + IN IMAGE_PROPERTIES_RECORD *ImageRecord, + IN OUT EFI_MEMORY_DESCRIPTOR *NewRecord, + IN EFI_MEMORY_DESCRIPTOR *OldRecord, + IN UINTN DescriptorSize + ) +{ + EFI_MEMORY_DESCRIPTOR TempRecord; + IMAGE_PROPERTIES_RECORD_CODE_SECTION *ImageRecordCodeSection; + LIST_ENTRY *ImageRecordCodeSectionLink; + LIST_ENTRY *ImageRecordCodeSectionEndLink; + LIST_ENTRY *ImageRecordCodeSectionList; + UINTN NewRecordCount; + UINT64 PhysicalEnd; + UINT64 ImageEnd; + + CopyMem (&TempRecord, OldRecord, sizeof(EFI_MEMORY_DESCRIPTOR)); + PhysicalEnd =3D TempRecord.PhysicalStart + EfiPagesToSize(TempRecord.Num= berOfPages); + NewRecordCount =3D 0; + + ImageRecordCodeSectionList =3D &ImageRecord->CodeSegmentList; + + ImageRecordCodeSectionLink =3D ImageRecordCodeSectionList->ForwardLink; + ImageRecordCodeSectionEndLink =3D ImageRecordCodeSectionList; + while (ImageRecordCodeSectionLink !=3D ImageRecordCodeSectionEndLink) { + ImageRecordCodeSection =3D CR ( + ImageRecordCodeSectionLink, + IMAGE_PROPERTIES_RECORD_CODE_SECTION, + Link, + IMAGE_PROPERTIES_RECORD_CODE_SECTION_SIGNAT= URE + ); + ImageRecordCodeSectionLink =3D ImageRecordCodeSectionLink->ForwardLink; + + if (TempRecord.PhysicalStart <=3D ImageRecordCodeSection->CodeSegmentB= ase) { + // + // DATA + // + NewRecord->Type =3D TempRecord.Type; + NewRecord->PhysicalStart =3D TempRecord.PhysicalStart; + NewRecord->VirtualStart =3D 0; + NewRecord->NumberOfPages =3D EfiSizeToPages(ImageRecordCodeSection->= CodeSegmentBase - NewRecord->PhysicalStart); + NewRecord->Attribute =3D TempRecord.Attribute | EFI_MEMORY_XP; + if (NewRecord->NumberOfPages !=3D 0) { + NewRecord =3D NEXT_MEMORY_DESCRIPTOR (NewRecord, DescriptorSize); + NewRecordCount ++; + } + + // + // CODE + // + NewRecord->Type =3D TempRecord.Type; + NewRecord->PhysicalStart =3D ImageRecordCodeSection->CodeSegmentBase; + NewRecord->VirtualStart =3D 0; + NewRecord->NumberOfPages =3D EfiSizeToPages(ImageRecordCodeSection->= CodeSegmentSize); + NewRecord->Attribute =3D (TempRecord.Attribute & (~EFI_MEMORY_XP= )) | EFI_MEMORY_RO; + if (NewRecord->NumberOfPages !=3D 0) { + NewRecord =3D NEXT_MEMORY_DESCRIPTOR (NewRecord, DescriptorSize); + NewRecordCount ++; + } + + TempRecord.PhysicalStart =3D ImageRecordCodeSection->CodeSegmentBase= + EfiPagesToSize (EfiSizeToPages(ImageRecordCodeSection->CodeSegmentSize)); + TempRecord.NumberOfPages =3D EfiSizeToPages(PhysicalEnd - TempRecord= .PhysicalStart); + if (TempRecord.NumberOfPages =3D=3D 0) { + break; + } + } + } + + ImageEnd =3D ImageRecord->ImageBase + ImageRecord->ImageSize; + + // + // Final DATA + // + if (TempRecord.PhysicalStart < ImageEnd) { + NewRecord->Type =3D TempRecord.Type; + NewRecord->PhysicalStart =3D TempRecord.PhysicalStart; + NewRecord->VirtualStart =3D 0; + NewRecord->NumberOfPages =3D EfiSizeToPages (ImageEnd - TempRecord.Phy= sicalStart); + NewRecord->Attribute =3D TempRecord.Attribute | EFI_MEMORY_XP; + NewRecordCount ++; + } + + return NewRecordCount; +} + +/** + Return the max number of new splitted entries, according to one old entr= y, + based upon PE code section and data section. + + @param OldRecord A pointer to one old memory map entry. + + @retval 0 no entry need to be splitted. + @return the max number of new splitted entries +**/ +STATIC +UINTN +GetMaxSplitRecordCount ( + IN EFI_MEMORY_DESCRIPTOR *OldRecord + ) +{ + IMAGE_PROPERTIES_RECORD *ImageRecord; + UINTN SplitRecordCount; + UINT64 PhysicalStart; + UINT64 PhysicalEnd; + + SplitRecordCount =3D 0; + PhysicalStart =3D OldRecord->PhysicalStart; + PhysicalEnd =3D OldRecord->PhysicalStart + EfiPagesToSize(OldRecord->Num= berOfPages); + + do { + ImageRecord =3D GetImageRecordByAddress (PhysicalStart, PhysicalEnd - = PhysicalStart); + if (ImageRecord =3D=3D NULL) { + break; + } + SplitRecordCount +=3D (2 * ImageRecord->CodeSegmentCount + 1); + PhysicalStart =3D ImageRecord->ImageBase + ImageRecord->ImageSize; + } while ((ImageRecord !=3D NULL) && (PhysicalStart < PhysicalEnd)); + + if (SplitRecordCount !=3D 0) { + SplitRecordCount--; + } + + return SplitRecordCount; +} + +/** + Split the memory map to new entries, according to one old entry, + based upon PE code section and data section. + + @param OldRecord A pointer to one old memory map entry. + @param NewRecord A pointer to several new memory map entri= es. + The caller gurantee the buffer size be 1 + + (SplitRecordCount * DescriptorSize) calcu= lated + below. + @param MaxSplitRecordCount The max number of splitted entries + @param DescriptorSize Size, in bytes, of an individual EFI_MEMO= RY_DESCRIPTOR. + + @retval 0 no entry is splitted. + @return the real number of splitted record. +**/ +STATIC +UINTN +SplitRecord ( + IN EFI_MEMORY_DESCRIPTOR *OldRecord, + IN OUT EFI_MEMORY_DESCRIPTOR *NewRecord, + IN UINTN MaxSplitRecordCount, + IN UINTN DescriptorSize + ) +{ + EFI_MEMORY_DESCRIPTOR TempRecord; + IMAGE_PROPERTIES_RECORD *ImageRecord; + IMAGE_PROPERTIES_RECORD *NewImageRecord; + UINT64 PhysicalStart; + UINT64 PhysicalEnd; + UINTN NewRecordCount; + UINTN TotalNewRecordCount; + BOOLEAN IsLastRecordData; + + if (MaxSplitRecordCount =3D=3D 0) { + CopyMem (NewRecord, OldRecord, DescriptorSize); + return 0; + } + + TotalNewRecordCount =3D 0; + + // + // Override previous record + // + CopyMem (&TempRecord, OldRecord, sizeof(EFI_MEMORY_DESCRIPTOR)); + PhysicalStart =3D TempRecord.PhysicalStart; + PhysicalEnd =3D TempRecord.PhysicalStart + EfiPagesToSize(TempRecord.Num= berOfPages); + + ImageRecord =3D NULL; + do { + NewImageRecord =3D GetImageRecordByAddress (PhysicalStart, PhysicalEnd= - PhysicalStart); + if (NewImageRecord =3D=3D NULL) { + // + // No more image covered by this range, stop + // + if ((PhysicalEnd > PhysicalStart) && (ImageRecord !=3D NULL)) { + // + // If this is still address in this record, need record. + // + NewRecord =3D PREVIOUS_MEMORY_DESCRIPTOR (NewRecord, DescriptorSiz= e); + IsLastRecordData =3D FALSE; + if ((NewRecord->Attribute & EFI_MEMORY_XP) !=3D 0) { + IsLastRecordData =3D TRUE; + } + if (IsLastRecordData) { + // + // Last record is DATA, just merge it. + // + NewRecord->NumberOfPages =3D EfiSizeToPages(PhysicalEnd - NewRec= ord->PhysicalStart); + } else { + // + // Last record is CODE, create a new DATA entry. + // + NewRecord =3D NEXT_MEMORY_DESCRIPTOR (NewRecord, DescriptorSize); + NewRecord->Type =3D TempRecord.Type; + NewRecord->PhysicalStart =3D TempRecord.PhysicalStart; + NewRecord->VirtualStart =3D 0; + NewRecord->NumberOfPages =3D TempRecord.NumberOfPages; + NewRecord->Attribute =3D TempRecord.Attribute | EFI_MEMORY_X= P; + TotalNewRecordCount ++; + } + } + break; + } + ImageRecord =3D NewImageRecord; + + // + // Set new record + // + NewRecordCount =3D SetNewRecord (ImageRecord, NewRecord, &TempRecord, = DescriptorSize); + TotalNewRecordCount +=3D NewRecordCount; + NewRecord =3D (EFI_MEMORY_DESCRIPTOR *)((UINT8 *)NewRecord + NewRecord= Count * DescriptorSize); + + // + // Update PhysicalStart, in order to exclude the image buffer already = splitted. + // + PhysicalStart =3D ImageRecord->ImageBase + ImageRecord->ImageSize; + TempRecord.PhysicalStart =3D PhysicalStart; + TempRecord.NumberOfPages =3D EfiSizeToPages (PhysicalEnd - PhysicalSta= rt); + } while ((ImageRecord !=3D NULL) && (PhysicalStart < PhysicalEnd)); + + // + // The logic in function SplitTable() ensures that TotalNewRecordCount w= ill not be zero if the + // code reaches here. + // + ASSERT (TotalNewRecordCount !=3D 0); + return TotalNewRecordCount - 1; +} + +/** + Split the original memory map, and add more entries to describe PE code = section and data section. + This function will set EfiRuntimeServicesData to be EFI_MEMORY_XP. + This function will merge entries with same attributes finally. + + NOTE: It assumes PE code/data section are page aligned. + NOTE: It assumes enough entry is prepared for new memory map. + + Split table: + +---------------+ + | Record X | + +---------------+ + | Record RtCode | + +---------------+ + | Record Y | + +---------------+ + =3D=3D> + +---------------+ + | Record X | + +---------------+ ---- + | Record RtData | | + +---------------+ | + | Record RtCode | |-> PE/COFF1 + +---------------+ | + | Record RtData | | + +---------------+ ---- + | Record RtData | | + +---------------+ | + | Record RtCode | |-> PE/COFF2 + +---------------+ | + | Record RtData | | + +---------------+ ---- + | Record Y | + +---------------+ + + @param MemoryMapSize A pointer to the size, in bytes, of the + MemoryMap buffer. On input, this is the s= ize of + old MemoryMap before split. The actual bu= ffer + size of MemoryMap is MemoryMapSize + + (AdditionalRecordCount * DescriptorSize) = calculated + below. On output, it is the size of new M= emoryMap + after split. + @param MemoryMap A pointer to the buffer in which firmware= places + the current memory map. + @param DescriptorSize Size, in bytes, of an individual EFI_MEMO= RY_DESCRIPTOR. +**/ +STATIC +VOID +SplitTable ( + IN OUT UINTN *MemoryMapSize, + IN OUT EFI_MEMORY_DESCRIPTOR *MemoryMap, + IN UINTN DescriptorSize + ) +{ + INTN IndexOld; + INTN IndexNew; + UINTN MaxSplitRecordCount; + UINTN RealSplitRecordCount; + UINTN TotalSplitRecordCount; + UINTN AdditionalRecordCount; + + AdditionalRecordCount =3D (2 * mImagePropertiesPrivateData.CodeSegmentCo= untMax + 1) * mImagePropertiesPrivateData.ImageRecordCount; + + TotalSplitRecordCount =3D 0; + // + // Let old record point to end of valid MemoryMap buffer. + // + IndexOld =3D ((*MemoryMapSize) / DescriptorSize) - 1; + // + // Let new record point to end of full MemoryMap buffer. + // + IndexNew =3D ((*MemoryMapSize) / DescriptorSize) - 1 + AdditionalRecordC= ount; + for (; IndexOld >=3D 0; IndexOld--) { + MaxSplitRecordCount =3D GetMaxSplitRecordCount ((EFI_MEMORY_DESCRIPTOR= *)((UINT8 *)MemoryMap + IndexOld * DescriptorSize)); + // + // Split this MemoryMap record + // + IndexNew -=3D MaxSplitRecordCount; + RealSplitRecordCount =3D SplitRecord ( + (EFI_MEMORY_DESCRIPTOR *)((UINT8 *)MemoryMap = + IndexOld * DescriptorSize), + (EFI_MEMORY_DESCRIPTOR *)((UINT8 *)MemoryMap = + IndexNew * DescriptorSize), + MaxSplitRecordCount, + DescriptorSize + ); + // + // Adjust IndexNew according to real split. + // + CopyMem ( + ((UINT8 *)MemoryMap + (IndexNew + MaxSplitRecordCount - RealSplitRec= ordCount) * DescriptorSize), + ((UINT8 *)MemoryMap + IndexNew * DescriptorSize), + RealSplitRecordCount * DescriptorSize + ); + IndexNew =3D IndexNew + MaxSplitRecordCount - RealSplitRecordCount; + TotalSplitRecordCount +=3D RealSplitRecordCount; + IndexNew --; + } + // + // Move all records to the beginning. + // + CopyMem ( + MemoryMap, + (UINT8 *)MemoryMap + (AdditionalRecordCount - TotalSplitRecordCount) *= DescriptorSize, + (*MemoryMapSize) + TotalSplitRecordCount * DescriptorSize + ); + + *MemoryMapSize =3D (*MemoryMapSize) + DescriptorSize * TotalSplitRecordC= ount; + + // + // Sort from low to high (Just in case) + // + SortMemoryMap (MemoryMap, *MemoryMapSize, DescriptorSize); + + // + // Set RuntimeData to XP + // + EnforceMemoryMapAttribute (MemoryMap, *MemoryMapSize, DescriptorSize); + + // + // Merge same type to save entry size + // + MergeMemoryMap (MemoryMap, MemoryMapSize, DescriptorSize); + + return ; +} + +/** + This function for GetMemoryMap() with properties table capability. + + It calls original GetMemoryMap() to get the original memory map informat= ion. Then + plus the additional memory map entries for PE Code/Data seperation. + + @param MemoryMapSize A pointer to the size, in bytes, of the + MemoryMap buffer. On input, this is the s= ize of + the buffer allocated by the caller. On o= utput, + it is the size of the buffer returned by = the + firmware if the buffer was large enough,= or the + size of the buffer needed to contain the= map if + the buffer was too small. + @param MemoryMap A pointer to the buffer in which firmware= places + the current memory map. + @param MapKey A pointer to the location in which firmwa= re + returns the key for the current memory ma= p. + @param DescriptorSize A pointer to the location in which firmwa= re + returns the size, in bytes, of an individ= ual + EFI_MEMORY_DESCRIPTOR. + @param DescriptorVersion A pointer to the location in which firmwa= re + returns the version number associated wit= h the + EFI_MEMORY_DESCRIPTOR. + + @retval EFI_SUCCESS The memory map was returned in the Memory= Map + buffer. + @retval EFI_BUFFER_TOO_SMALL The MemoryMap buffer was too small. The c= urrent + buffer size needed to hold the memory map= is + returned in MemoryMapSize. + @retval EFI_INVALID_PARAMETER One of the parameters has an invalid valu= e. + +**/ +EFI_STATUS +EFIAPI +CoreGetMemoryMapWithSeparatedImageSection ( + IN OUT UINTN *MemoryMapSize, + IN OUT EFI_MEMORY_DESCRIPTOR *MemoryMap, + OUT UINTN *MapKey, + OUT UINTN *DescriptorSize, + OUT UINT32 *DescriptorVersion + ) +{ + EFI_STATUS Status; + UINTN OldMemoryMapSize; + UINTN AdditionalRecordCount; + + // + // If PE code/data is not aligned, just return. + // + if (!mMemoryAttributesTableEnable) { + return CoreGetMemoryMap (MemoryMapSize, MemoryMap, MapKey, DescriptorS= ize, DescriptorVersion); + } + + if (MemoryMapSize =3D=3D NULL) { + return EFI_INVALID_PARAMETER; + } + + CoreAcquiremMemoryAttributesTableLock (); + + AdditionalRecordCount =3D (2 * mImagePropertiesPrivateData.CodeSegmentCo= untMax + 1) * mImagePropertiesPrivateData.ImageRecordCount; + + OldMemoryMapSize =3D *MemoryMapSize; + Status =3D CoreGetMemoryMap (MemoryMapSize, MemoryMap, MapKey, Descripto= rSize, DescriptorVersion); + if (Status =3D=3D EFI_BUFFER_TOO_SMALL) { + *MemoryMapSize =3D *MemoryMapSize + (*DescriptorSize) * AdditionalReco= rdCount; + } else if (Status =3D=3D EFI_SUCCESS) { + ASSERT (MemoryMap !=3D NULL); + if (OldMemoryMapSize - *MemoryMapSize < (*DescriptorSize) * Additional= RecordCount) { + *MemoryMapSize =3D *MemoryMapSize + (*DescriptorSize) * AdditionalRe= cordCount; + // + // Need update status to buffer too small + // + Status =3D EFI_BUFFER_TOO_SMALL; + } else { + // + // Split PE code/data + // + SplitTable (MemoryMapSize, MemoryMap, *DescriptorSize); + } + } + + CoreReleasemMemoryAttributesTableLock (); + return Status; +} + +// +// Below functions are for ImageRecord +// + +/** + Set MemoryAttributesTable according to PE/COFF image section alignment. + + @param SectionAlignment PE/COFF section alignment +**/ +STATIC +VOID +SetMemoryAttributesTableSectionAlignment ( + IN UINT32 SectionAlignment + ) +{ + if (((SectionAlignment & (RUNTIME_PAGE_ALLOCATION_GRANULARITY - 1)) !=3D= 0) && + mMemoryAttributesTableEnable) { + DEBUG ((EFI_D_VERBOSE, "SetMemoryAttributesTableSectionAlignment - Cle= ar\n")); + mMemoryAttributesTableEnable =3D FALSE; + } +} + +/** + Swap two code sections in image record. + + @param FirstImageRecordCodeSection first code section in image record + @param SecondImageRecordCodeSection second code section in image reco= rd +**/ +STATIC +VOID +SwapImageRecordCodeSection ( + IN IMAGE_PROPERTIES_RECORD_CODE_SECTION *FirstImageRecordCodeSectio= n, + IN IMAGE_PROPERTIES_RECORD_CODE_SECTION *SecondImageRecordCodeSecti= on + ) +{ + IMAGE_PROPERTIES_RECORD_CODE_SECTION TempImageRecordCodeSection; + + TempImageRecordCodeSection.CodeSegmentBase =3D FirstImageRecordCodeSecti= on->CodeSegmentBase; + TempImageRecordCodeSection.CodeSegmentSize =3D FirstImageRecordCodeSecti= on->CodeSegmentSize; + + FirstImageRecordCodeSection->CodeSegmentBase =3D SecondImageRecordCodeSe= ction->CodeSegmentBase; + FirstImageRecordCodeSection->CodeSegmentSize =3D SecondImageRecordCodeSe= ction->CodeSegmentSize; + + SecondImageRecordCodeSection->CodeSegmentBase =3D TempImageRecordCodeSec= tion.CodeSegmentBase; + SecondImageRecordCodeSection->CodeSegmentSize =3D TempImageRecordCodeSec= tion.CodeSegmentSize; +} + +/** + Sort code section in image record, based upon CodeSegmentBase from low t= o high. + + @param ImageRecord image record to be sorted +**/ +VOID +SortImageRecordCodeSection ( + IN IMAGE_PROPERTIES_RECORD *ImageRecord + ) +{ + IMAGE_PROPERTIES_RECORD_CODE_SECTION *ImageRecordCodeSection; + IMAGE_PROPERTIES_RECORD_CODE_SECTION *NextImageRecordCodeSection; + LIST_ENTRY *ImageRecordCodeSectionLink; + LIST_ENTRY *NextImageRecordCodeSectionLin= k; + LIST_ENTRY *ImageRecordCodeSectionEndLink; + LIST_ENTRY *ImageRecordCodeSectionList; + + ImageRecordCodeSectionList =3D &ImageRecord->CodeSegmentList; + + ImageRecordCodeSectionLink =3D ImageRecordCodeSectionList->ForwardLink; + NextImageRecordCodeSectionLink =3D ImageRecordCodeSectionLink->ForwardLi= nk; + ImageRecordCodeSectionEndLink =3D ImageRecordCodeSectionList; + while (ImageRecordCodeSectionLink !=3D ImageRecordCodeSectionEndLink) { + ImageRecordCodeSection =3D CR ( + ImageRecordCodeSectionLink, + IMAGE_PROPERTIES_RECORD_CODE_SECTION, + Link, + IMAGE_PROPERTIES_RECORD_CODE_SECTION_SIGNAT= URE + ); + while (NextImageRecordCodeSectionLink !=3D ImageRecordCodeSectionEndLi= nk) { + NextImageRecordCodeSection =3D CR ( + NextImageRecordCodeSectionLink, + IMAGE_PROPERTIES_RECORD_CODE_SECTION, + Link, + IMAGE_PROPERTIES_RECORD_CODE_SECTION_= SIGNATURE + ); + if (ImageRecordCodeSection->CodeSegmentBase > NextImageRecordCodeSec= tion->CodeSegmentBase) { + SwapImageRecordCodeSection (ImageRecordCodeSection, NextImageRecor= dCodeSection); + } + NextImageRecordCodeSectionLink =3D NextImageRecordCodeSectionLink->F= orwardLink; + } + + ImageRecordCodeSectionLink =3D ImageRecordCodeSectionLink->ForwardLink; + NextImageRecordCodeSectionLink =3D ImageRecordCodeSectionLink->Forward= Link; + } +} + +/** + Check if code section in image record is valid. + + @param ImageRecord image record to be checked + + @retval TRUE image record is valid + @retval FALSE image record is invalid +**/ +BOOLEAN +IsImageRecordCodeSectionValid ( + IN IMAGE_PROPERTIES_RECORD *ImageRecord + ) +{ + IMAGE_PROPERTIES_RECORD_CODE_SECTION *ImageRecordCodeSection; + IMAGE_PROPERTIES_RECORD_CODE_SECTION *LastImageRecordCodeSection; + LIST_ENTRY *ImageRecordCodeSectionLink; + LIST_ENTRY *ImageRecordCodeSectionEndLink; + LIST_ENTRY *ImageRecordCodeSectionList; + + DEBUG ((EFI_D_VERBOSE, "ImageCode SegmentCount - 0x%x\n", ImageRecord->C= odeSegmentCount)); + + ImageRecordCodeSectionList =3D &ImageRecord->CodeSegmentList; + + ImageRecordCodeSectionLink =3D ImageRecordCodeSectionList->ForwardLink; + ImageRecordCodeSectionEndLink =3D ImageRecordCodeSectionList; + LastImageRecordCodeSection =3D NULL; + while (ImageRecordCodeSectionLink !=3D ImageRecordCodeSectionEndLink) { + ImageRecordCodeSection =3D CR ( + ImageRecordCodeSectionLink, + IMAGE_PROPERTIES_RECORD_CODE_SECTION, + Link, + IMAGE_PROPERTIES_RECORD_CODE_SECTION_SIGNAT= URE + ); + if (ImageRecordCodeSection->CodeSegmentSize =3D=3D 0) { + return FALSE; + } + if (ImageRecordCodeSection->CodeSegmentBase < ImageRecord->ImageBase) { + return FALSE; + } + if (ImageRecordCodeSection->CodeSegmentBase >=3D MAX_ADDRESS - ImageRe= cordCodeSection->CodeSegmentSize) { + return FALSE; + } + if ((ImageRecordCodeSection->CodeSegmentBase + ImageRecordCodeSection-= >CodeSegmentSize) > (ImageRecord->ImageBase + ImageRecord->ImageSize)) { + return FALSE; + } + if (LastImageRecordCodeSection !=3D NULL) { + if ((LastImageRecordCodeSection->CodeSegmentBase + LastImageRecordCo= deSection->CodeSegmentSize) > ImageRecordCodeSection->CodeSegmentBase) { + return FALSE; + } + } + + LastImageRecordCodeSection =3D ImageRecordCodeSection; + ImageRecordCodeSectionLink =3D ImageRecordCodeSectionLink->ForwardLink; + } + + return TRUE; +} + +/** + Swap two image records. + + @param FirstImageRecord first image record. + @param SecondImageRecord second image record. +**/ +STATIC +VOID +SwapImageRecord ( + IN IMAGE_PROPERTIES_RECORD *FirstImageRecord, + IN IMAGE_PROPERTIES_RECORD *SecondImageRecord + ) +{ + IMAGE_PROPERTIES_RECORD TempImageRecord; + + TempImageRecord.ImageBase =3D FirstImageRecord->ImageBase; + TempImageRecord.ImageSize =3D FirstImageRecord->ImageSize; + TempImageRecord.CodeSegmentCount =3D FirstImageRecord->CodeSegmentCount; + + FirstImageRecord->ImageBase =3D SecondImageRecord->ImageBase; + FirstImageRecord->ImageSize =3D SecondImageRecord->ImageSize; + FirstImageRecord->CodeSegmentCount =3D SecondImageRecord->CodeSegmentCou= nt; + + SecondImageRecord->ImageBase =3D TempImageRecord.ImageBase; + SecondImageRecord->ImageSize =3D TempImageRecord.ImageSize; + SecondImageRecord->CodeSegmentCount =3D TempImageRecord.CodeSegmentCount; + + SwapListEntries (&FirstImageRecord->CodeSegmentList, &SecondImageRecord-= >CodeSegmentList); +} + +/** + Sort image record based upon the ImageBase from low to high. +**/ +STATIC +VOID +SortImageRecord ( + VOID + ) +{ + IMAGE_PROPERTIES_RECORD *ImageRecord; + IMAGE_PROPERTIES_RECORD *NextImageRecord; + LIST_ENTRY *ImageRecordLink; + LIST_ENTRY *NextImageRecordLink; + LIST_ENTRY *ImageRecordEndLink; + LIST_ENTRY *ImageRecordList; + + ImageRecordList =3D &mImagePropertiesPrivateData.ImageRecordList; + + ImageRecordLink =3D ImageRecordList->ForwardLink; + NextImageRecordLink =3D ImageRecordLink->ForwardLink; + ImageRecordEndLink =3D ImageRecordList; + while (ImageRecordLink !=3D ImageRecordEndLink) { + ImageRecord =3D CR ( + ImageRecordLink, + IMAGE_PROPERTIES_RECORD, + Link, + IMAGE_PROPERTIES_RECORD_SIGNATURE + ); + while (NextImageRecordLink !=3D ImageRecordEndLink) { + NextImageRecord =3D CR ( + NextImageRecordLink, + IMAGE_PROPERTIES_RECORD, + Link, + IMAGE_PROPERTIES_RECORD_SIGNATURE + ); + if (ImageRecord->ImageBase > NextImageRecord->ImageBase) { + SwapImageRecord (ImageRecord, NextImageRecord); + } + NextImageRecordLink =3D NextImageRecordLink->ForwardLink; + } + + ImageRecordLink =3D ImageRecordLink->ForwardLink; + NextImageRecordLink =3D ImageRecordLink->ForwardLink; + } +} + +/** + Insert image record. + + @param RuntimeImage Runtime image information +**/ +VOID +InsertImageRecord ( + IN EFI_RUNTIME_IMAGE_ENTRY *RuntimeImage + ) +{ + VOID *ImageAddress; + EFI_IMAGE_DOS_HEADER *DosHdr; + UINT32 PeCoffHeaderOffset; + UINT32 SectionAlignment; + EFI_IMAGE_SECTION_HEADER *Section; + EFI_IMAGE_OPTIONAL_HEADER_PTR_UNION Hdr; + UINT8 *Name; + UINTN Index; + IMAGE_PROPERTIES_RECORD *ImageRecord; + CHAR8 *PdbPointer; + IMAGE_PROPERTIES_RECORD_CODE_SECTION *ImageRecordCodeSection; + + DEBUG ((EFI_D_VERBOSE, "InsertImageRecord - 0x%x\n", RuntimeImage)); + DEBUG ((EFI_D_VERBOSE, "InsertImageRecord - 0x%016lx - 0x%016lx\n", (EFI= _PHYSICAL_ADDRESS)(UINTN)RuntimeImage->ImageBase, RuntimeImage->ImageSize)); + + if (mMemoryAttributesTableEndOfDxe) { + DEBUG ((DEBUG_INFO, "Do not insert runtime image record after EndOfDxe= \n")); + return ; + } + + ImageRecord =3D AllocatePool (sizeof(*ImageRecord)); + if (ImageRecord =3D=3D NULL) { + return ; + } + ImageRecord->Signature =3D IMAGE_PROPERTIES_RECORD_SIGNATURE; + + DEBUG ((EFI_D_VERBOSE, "ImageRecordCount - 0x%x\n", mImagePropertiesPriv= ateData.ImageRecordCount)); + + // + // Step 1: record whole region + // + ImageRecord->ImageBase =3D (EFI_PHYSICAL_ADDRESS)(UINTN)RuntimeImage->Im= ageBase; + ImageRecord->ImageSize =3D RuntimeImage->ImageSize; + + ImageAddress =3D RuntimeImage->ImageBase; + + PdbPointer =3D PeCoffLoaderGetPdbPointer ((VOID*) (UINTN) ImageAddress); + if (PdbPointer !=3D NULL) { + DEBUG ((EFI_D_VERBOSE, " Image - %a\n", PdbPointer)); + } + + // + // Check PE/COFF image + // + DosHdr =3D (EFI_IMAGE_DOS_HEADER *) (UINTN) ImageAddress; + PeCoffHeaderOffset =3D 0; + if (DosHdr->e_magic =3D=3D EFI_IMAGE_DOS_SIGNATURE) { + PeCoffHeaderOffset =3D DosHdr->e_lfanew; + } + + Hdr.Pe32 =3D (EFI_IMAGE_NT_HEADERS32 *)((UINT8 *) (UINTN) ImageAddress += PeCoffHeaderOffset); + if (Hdr.Pe32->Signature !=3D EFI_IMAGE_NT_SIGNATURE) { + DEBUG ((EFI_D_VERBOSE, "Hdr.Pe32->Signature invalid - 0x%x\n", Hdr.Pe3= 2->Signature)); + // It might be image in SMM. + goto Finish; + } + + // + // Get SectionAlignment + // + if (Hdr.Pe32->OptionalHeader.Magic =3D=3D EFI_IMAGE_NT_OPTIONAL_HDR32_MA= GIC) { + SectionAlignment =3D Hdr.Pe32->OptionalHeader.SectionAlignment; + } else { + SectionAlignment =3D Hdr.Pe32Plus->OptionalHeader.SectionAlignment; + } + + SetMemoryAttributesTableSectionAlignment (SectionAlignment); + if ((SectionAlignment & (RUNTIME_PAGE_ALLOCATION_GRANULARITY - 1)) !=3D = 0) { + DEBUG ((EFI_D_WARN, "!!!!!!!! InsertImageRecord - Section Alignment(0= x%x) is not %dK !!!!!!!!\n", + SectionAlignment, RUNTIME_PAGE_ALLOCATION_GRANULARITY >> 10)); + PdbPointer =3D PeCoffLoaderGetPdbPointer ((VOID*) (UINTN) ImageAddress= ); + if (PdbPointer !=3D NULL) { + DEBUG ((EFI_D_WARN, "!!!!!!!! Image - %a !!!!!!!!\n", PdbPointer)); + } + goto Finish; + } + + Section =3D (EFI_IMAGE_SECTION_HEADER *) ( + (UINT8 *) (UINTN) ImageAddress + + PeCoffHeaderOffset + + sizeof(UINT32) + + sizeof(EFI_IMAGE_FILE_HEADER) + + Hdr.Pe32->FileHeader.SizeOfOptionalHeader + ); + ImageRecord->CodeSegmentCount =3D 0; + InitializeListHead (&ImageRecord->CodeSegmentList); + for (Index =3D 0; Index < Hdr.Pe32->FileHeader.NumberOfSections; Index++= ) { + Name =3D Section[Index].Name; + DEBUG (( + EFI_D_VERBOSE, + " Section - '%c%c%c%c%c%c%c%c'\n", + Name[0], + Name[1], + Name[2], + Name[3], + Name[4], + Name[5], + Name[6], + Name[7] + )); + + if ((Section[Index].Characteristics & EFI_IMAGE_SCN_CNT_CODE) !=3D 0) { + DEBUG ((EFI_D_VERBOSE, " VirtualSize - 0x%08x\n", Section[= Index].Misc.VirtualSize)); + DEBUG ((EFI_D_VERBOSE, " VirtualAddress - 0x%08x\n", Section[= Index].VirtualAddress)); + DEBUG ((EFI_D_VERBOSE, " SizeOfRawData - 0x%08x\n", Section[= Index].SizeOfRawData)); + DEBUG ((EFI_D_VERBOSE, " PointerToRawData - 0x%08x\n", Section[= Index].PointerToRawData)); + DEBUG ((EFI_D_VERBOSE, " PointerToRelocations - 0x%08x\n", Section[= Index].PointerToRelocations)); + DEBUG ((EFI_D_VERBOSE, " PointerToLinenumbers - 0x%08x\n", Section[= Index].PointerToLinenumbers)); + DEBUG ((EFI_D_VERBOSE, " NumberOfRelocations - 0x%08x\n", Section[= Index].NumberOfRelocations)); + DEBUG ((EFI_D_VERBOSE, " NumberOfLinenumbers - 0x%08x\n", Section[= Index].NumberOfLinenumbers)); + DEBUG ((EFI_D_VERBOSE, " Characteristics - 0x%08x\n", Section[= Index].Characteristics)); + + // + // Step 2: record code section + // + ImageRecordCodeSection =3D AllocatePool (sizeof(*ImageRecordCodeSect= ion)); + if (ImageRecordCodeSection =3D=3D NULL) { + return ; + } + ImageRecordCodeSection->Signature =3D IMAGE_PROPERTIES_RECORD_CODE_S= ECTION_SIGNATURE; + + ImageRecordCodeSection->CodeSegmentBase =3D (UINTN)ImageAddress + Se= ction[Index].VirtualAddress; + ImageRecordCodeSection->CodeSegmentSize =3D Section[Index].SizeOfRaw= Data; + + DEBUG ((EFI_D_VERBOSE, "ImageCode: 0x%016lx - 0x%016lx\n", ImageReco= rdCodeSection->CodeSegmentBase, ImageRecordCodeSection->CodeSegmentSize)); + + InsertTailList (&ImageRecord->CodeSegmentList, &ImageRecordCodeSecti= on->Link); + ImageRecord->CodeSegmentCount++; + } + } + + if (ImageRecord->CodeSegmentCount =3D=3D 0) { + SetMemoryAttributesTableSectionAlignment (1); + DEBUG ((EFI_D_ERROR, "!!!!!!!! InsertImageRecord - CodeSegmentCount i= s 0 !!!!!!!!\n")); + PdbPointer =3D PeCoffLoaderGetPdbPointer ((VOID*) (UINTN) ImageAddress= ); + if (PdbPointer !=3D NULL) { + DEBUG ((EFI_D_ERROR, "!!!!!!!! Image - %a !!!!!!!!\n", PdbPointer)= ); + } + goto Finish; + } + + // + // Final + // + SortImageRecordCodeSection (ImageRecord); + // + // Check overlap all section in ImageBase/Size + // + if (!IsImageRecordCodeSectionValid (ImageRecord)) { + DEBUG ((EFI_D_ERROR, "IsImageRecordCodeSectionValid - FAIL\n")); + goto Finish; + } + + InsertTailList (&mImagePropertiesPrivateData.ImageRecordList, &ImageReco= rd->Link); + mImagePropertiesPrivateData.ImageRecordCount++; + + if (mImagePropertiesPrivateData.CodeSegmentCountMax < ImageRecord->CodeS= egmentCount) { + mImagePropertiesPrivateData.CodeSegmentCountMax =3D ImageRecord->CodeS= egmentCount; + } + + SortImageRecord (); + +Finish: + return ; +} + +/** + Find image record according to image base and size. + + @param ImageBase Base of PE image + @param ImageSize Size of PE image + + @return image record +**/ +STATIC +IMAGE_PROPERTIES_RECORD * +FindImageRecord ( + IN EFI_PHYSICAL_ADDRESS ImageBase, + IN UINT64 ImageSize + ) +{ + IMAGE_PROPERTIES_RECORD *ImageRecord; + LIST_ENTRY *ImageRecordLink; + LIST_ENTRY *ImageRecordList; + + ImageRecordList =3D &mImagePropertiesPrivateData.ImageRecordList; + + for (ImageRecordLink =3D ImageRecordList->ForwardLink; + ImageRecordLink !=3D ImageRecordList; + ImageRecordLink =3D ImageRecordLink->ForwardLink) { + ImageRecord =3D CR ( + ImageRecordLink, + IMAGE_PROPERTIES_RECORD, + Link, + IMAGE_PROPERTIES_RECORD_SIGNATURE + ); + + if ((ImageBase =3D=3D ImageRecord->ImageBase) && + (ImageSize =3D=3D ImageRecord->ImageSize)) { + return ImageRecord; + } + } + + return NULL; +} + +/** + Remove Image record. + + @param RuntimeImage Runtime image information +**/ +VOID +RemoveImageRecord ( + IN EFI_RUNTIME_IMAGE_ENTRY *RuntimeImage + ) +{ + IMAGE_PROPERTIES_RECORD *ImageRecord; + LIST_ENTRY *CodeSegmentListHead; + IMAGE_PROPERTIES_RECORD_CODE_SECTION *ImageRecordCodeSection; + + DEBUG ((EFI_D_VERBOSE, "RemoveImageRecord - 0x%x\n", RuntimeImage)); + DEBUG ((EFI_D_VERBOSE, "RemoveImageRecord - 0x%016lx - 0x%016lx\n", (EFI= _PHYSICAL_ADDRESS)(UINTN)RuntimeImage->ImageBase, RuntimeImage->ImageSize)); + + if (mMemoryAttributesTableEndOfDxe) { + DEBUG ((DEBUG_INFO, "Do not remove runtime image record after EndOfDxe= \n")); + return ; + } + + ImageRecord =3D FindImageRecord ((EFI_PHYSICAL_ADDRESS)(UINTN)RuntimeIma= ge->ImageBase, RuntimeImage->ImageSize); + if (ImageRecord =3D=3D NULL) { + DEBUG ((EFI_D_ERROR, "!!!!!!!! ImageRecord not found !!!!!!!!\n")); + return ; + } + + CodeSegmentListHead =3D &ImageRecord->CodeSegmentList; + while (!IsListEmpty (CodeSegmentListHead)) { + ImageRecordCodeSection =3D CR ( + CodeSegmentListHead->ForwardLink, + IMAGE_PROPERTIES_RECORD_CODE_SECTION, + Link, + IMAGE_PROPERTIES_RECORD_CODE_SECTION_SIGNAT= URE + ); + RemoveEntryList (&ImageRecordCodeSection->Link); + FreePool (ImageRecordCodeSection); + } + + RemoveEntryList (&ImageRecord->Link); + FreePool (ImageRecord); + mImagePropertiesPrivateData.ImageRecordCount--; +} diff --git a/MdeModulePkg/Core/Dxe/Misc/PropertiesTable.c b/MdeModulePkg/Co= re/Dxe/Misc/PropertiesTable.c deleted file mode 100644 index 6ee8a8af9098..000000000000 --- a/MdeModulePkg/Core/Dxe/Misc/PropertiesTable.c +++ /dev/null @@ -1,1288 +0,0 @@ -/** @file - UEFI PropertiesTable support - -Copyright (c) 2015 - 2018, Intel Corporation. All rights reserved.
-SPDX-License-Identifier: BSD-2-Clause-Patent - -**/ - -#include -#include -#include -#include -#include -#include -#include -#include -#include - -#include -#include - -#include -#include -#include - -#include "DxeMain.h" -#include "HeapGuard.h" - -#define PREVIOUS_MEMORY_DESCRIPTOR(MemoryDescriptor, Size) \ - ((EFI_MEMORY_DESCRIPTOR *)((UINT8 *)(MemoryDescriptor) - (Size))) - -#define IMAGE_PROPERTIES_PRIVATE_DATA_SIGNATURE SIGNATURE_32 ('I','P','P',= 'D') - -typedef struct { - UINT32 Signature; - UINTN ImageRecordCount; - UINTN CodeSegmentCountMax; - LIST_ENTRY ImageRecordList; -} IMAGE_PROPERTIES_PRIVATE_DATA; - -IMAGE_PROPERTIES_PRIVATE_DATA mImagePropertiesPrivateData =3D { - IMAGE_PROPERTIES_PRIVATE_DATA_SIGNATURE, - 0, - 0, - INITIALIZE_LIST_HEAD_VARIABLE (mImagePropertiesPrivateData.ImageRecordLi= st) -}; - -EFI_LOCK mPropertiesTableLock =3D EFI_INITIALIZE_LOCK_VARIABLE (= TPL_NOTIFY); - -BOOLEAN mPropertiesTableEndOfDxe =3D FALSE; - -extern BOOLEAN mMemoryAttributesTableEnable; - -// -// Below functions are for MemoryMap -// - -/** - Converts a number of EFI_PAGEs to a size in bytes. - - NOTE: Do not use EFI_PAGES_TO_SIZE because it handles UINTN only. - - @param Pages The number of EFI_PAGES. - - @return The number of bytes associated with the number of EFI_PAGEs spe= cified - by Pages. -**/ -STATIC -UINT64 -EfiPagesToSize ( - IN UINT64 Pages - ) -{ - return LShiftU64 (Pages, EFI_PAGE_SHIFT); -} - -/** - Converts a size, in bytes, to a number of EFI_PAGESs. - - NOTE: Do not use EFI_SIZE_TO_PAGES because it handles UINTN only. - - @param Size A size in bytes. - - @return The number of EFI_PAGESs associated with the number of bytes sp= ecified - by Size. - -**/ -STATIC -UINT64 -EfiSizeToPages ( - IN UINT64 Size - ) -{ - return RShiftU64 (Size, EFI_PAGE_SHIFT) + ((((UINTN)Size) & EFI_PAGE_MAS= K) ? 1 : 0); -} - -/** - Acquire memory lock on mPropertiesTableLock. -**/ -STATIC -VOID -CoreAcquirePropertiesTableLock ( - VOID - ) -{ - CoreAcquireLock (&mPropertiesTableLock); -} - -/** - Release memory lock on mPropertiesTableLock. -**/ -STATIC -VOID -CoreReleasePropertiesTableLock ( - VOID - ) -{ - CoreReleaseLock (&mPropertiesTableLock); -} - -/** - Sort memory map entries based upon PhysicalStart, from low to high. - - @param MemoryMap A pointer to the buffer in which firmware= places - the current memory map. - @param MemoryMapSize Size, in bytes, of the MemoryMap buffer. - @param DescriptorSize Size, in bytes, of an individual EFI_MEMO= RY_DESCRIPTOR. -**/ -STATIC -VOID -SortMemoryMap ( - IN OUT EFI_MEMORY_DESCRIPTOR *MemoryMap, - IN UINTN MemoryMapSize, - IN UINTN DescriptorSize - ) -{ - EFI_MEMORY_DESCRIPTOR *MemoryMapEntry; - EFI_MEMORY_DESCRIPTOR *NextMemoryMapEntry; - EFI_MEMORY_DESCRIPTOR *MemoryMapEnd; - EFI_MEMORY_DESCRIPTOR TempMemoryMap; - - MemoryMapEntry =3D MemoryMap; - NextMemoryMapEntry =3D NEXT_MEMORY_DESCRIPTOR (MemoryMapEntry, Descripto= rSize); - MemoryMapEnd =3D (EFI_MEMORY_DESCRIPTOR *) ((UINT8 *) MemoryMap + Memory= MapSize); - while (MemoryMapEntry < MemoryMapEnd) { - while (NextMemoryMapEntry < MemoryMapEnd) { - if (MemoryMapEntry->PhysicalStart > NextMemoryMapEntry->PhysicalStar= t) { - CopyMem (&TempMemoryMap, MemoryMapEntry, sizeof(EFI_MEMORY_DESCRIP= TOR)); - CopyMem (MemoryMapEntry, NextMemoryMapEntry, sizeof(EFI_MEMORY_DES= CRIPTOR)); - CopyMem (NextMemoryMapEntry, &TempMemoryMap, sizeof(EFI_MEMORY_DES= CRIPTOR)); - } - - NextMemoryMapEntry =3D NEXT_MEMORY_DESCRIPTOR (NextMemoryMapEntry, D= escriptorSize); - } - - MemoryMapEntry =3D NEXT_MEMORY_DESCRIPTOR (MemoryMapEntry, Descri= ptorSize); - NextMemoryMapEntry =3D NEXT_MEMORY_DESCRIPTOR (MemoryMapEntry, Descri= ptorSize); - } - - return ; -} - -/** - Merge continous memory map entries whose have same attributes. - - @param MemoryMap A pointer to the buffer in which firmware= places - the current memory map. - @param MemoryMapSize A pointer to the size, in bytes, of the - MemoryMap buffer. On input, this is the s= ize of - the current memory map. On output, - it is the size of new memory map after me= rge. - @param DescriptorSize Size, in bytes, of an individual EFI_MEMO= RY_DESCRIPTOR. -**/ -VOID -MergeMemoryMap ( - IN OUT EFI_MEMORY_DESCRIPTOR *MemoryMap, - IN OUT UINTN *MemoryMapSize, - IN UINTN DescriptorSize - ) -{ - EFI_MEMORY_DESCRIPTOR *MemoryMapEntry; - EFI_MEMORY_DESCRIPTOR *MemoryMapEnd; - UINT64 MemoryBlockLength; - EFI_MEMORY_DESCRIPTOR *NewMemoryMapEntry; - EFI_MEMORY_DESCRIPTOR *NextMemoryMapEntry; - - MemoryMapEntry =3D MemoryMap; - NewMemoryMapEntry =3D MemoryMap; - MemoryMapEnd =3D (EFI_MEMORY_DESCRIPTOR *) ((UINT8 *) MemoryMap + *Memor= yMapSize); - while ((UINTN)MemoryMapEntry < (UINTN)MemoryMapEnd) { - CopyMem (NewMemoryMapEntry, MemoryMapEntry, sizeof(EFI_MEMORY_DESCRIPT= OR)); - NextMemoryMapEntry =3D NEXT_MEMORY_DESCRIPTOR (MemoryMapEntry, Descrip= torSize); - - do { - MergeGuardPages (NewMemoryMapEntry, NextMemoryMapEntry->PhysicalStar= t); - MemoryBlockLength =3D (UINT64) (EfiPagesToSize (NewMemoryMapEntry->N= umberOfPages)); - if (((UINTN)NextMemoryMapEntry < (UINTN)MemoryMapEnd) && - (NewMemoryMapEntry->Type =3D=3D NextMemoryMapEntry->Type) && - (NewMemoryMapEntry->Attribute =3D=3D NextMemoryMapEntry->Attribu= te) && - ((NewMemoryMapEntry->PhysicalStart + MemoryBlockLength) =3D=3D N= extMemoryMapEntry->PhysicalStart)) { - NewMemoryMapEntry->NumberOfPages +=3D NextMemoryMapEntry->NumberOf= Pages; - NextMemoryMapEntry =3D NEXT_MEMORY_DESCRIPTOR (NextMemoryMapEntry,= DescriptorSize); - continue; - } else { - MemoryMapEntry =3D PREVIOUS_MEMORY_DESCRIPTOR (NextMemoryMapEntry,= DescriptorSize); - break; - } - } while (TRUE); - - MemoryMapEntry =3D NEXT_MEMORY_DESCRIPTOR (MemoryMapEntry, DescriptorS= ize); - NewMemoryMapEntry =3D NEXT_MEMORY_DESCRIPTOR (NewMemoryMapEntry, Descr= iptorSize); - } - - *MemoryMapSize =3D (UINTN)NewMemoryMapEntry - (UINTN)MemoryMap; - - return ; -} - -/** - Enforce memory map attributes. - This function will set EfiRuntimeServicesData/EfiMemoryMappedIO/EfiMemor= yMappedIOPortSpace to be EFI_MEMORY_XP. - - @param MemoryMap A pointer to the buffer in which firmware= places - the current memory map. - @param MemoryMapSize Size, in bytes, of the MemoryMap buffer. - @param DescriptorSize Size, in bytes, of an individual EFI_MEMO= RY_DESCRIPTOR. -**/ -STATIC -VOID -EnforceMemoryMapAttribute ( - IN OUT EFI_MEMORY_DESCRIPTOR *MemoryMap, - IN UINTN MemoryMapSize, - IN UINTN DescriptorSize - ) -{ - EFI_MEMORY_DESCRIPTOR *MemoryMapEntry; - EFI_MEMORY_DESCRIPTOR *MemoryMapEnd; - - MemoryMapEntry =3D MemoryMap; - MemoryMapEnd =3D (EFI_MEMORY_DESCRIPTOR *) ((UINT8 *) MemoryMap + Memo= ryMapSize); - while ((UINTN)MemoryMapEntry < (UINTN)MemoryMapEnd) { - switch (MemoryMapEntry->Type) { - case EfiRuntimeServicesCode: - // do nothing - break; - case EfiRuntimeServicesData: - case EfiMemoryMappedIO: - case EfiMemoryMappedIOPortSpace: - MemoryMapEntry->Attribute |=3D EFI_MEMORY_XP; - break; - case EfiReservedMemoryType: - case EfiACPIMemoryNVS: - break; - } - - MemoryMapEntry =3D NEXT_MEMORY_DESCRIPTOR (MemoryMapEntry, DescriptorS= ize); - } - - return ; -} - -/** - Return the first image record, whose [ImageBase, ImageSize] covered by [= Buffer, Length]. - - @param Buffer Start Address - @param Length Address length - - @return first image record covered by [buffer, length] -**/ -STATIC -IMAGE_PROPERTIES_RECORD * -GetImageRecordByAddress ( - IN EFI_PHYSICAL_ADDRESS Buffer, - IN UINT64 Length - ) -{ - IMAGE_PROPERTIES_RECORD *ImageRecord; - LIST_ENTRY *ImageRecordLink; - LIST_ENTRY *ImageRecordList; - - ImageRecordList =3D &mImagePropertiesPrivateData.ImageRecordList; - - for (ImageRecordLink =3D ImageRecordList->ForwardLink; - ImageRecordLink !=3D ImageRecordList; - ImageRecordLink =3D ImageRecordLink->ForwardLink) { - ImageRecord =3D CR ( - ImageRecordLink, - IMAGE_PROPERTIES_RECORD, - Link, - IMAGE_PROPERTIES_RECORD_SIGNATURE - ); - - if ((Buffer <=3D ImageRecord->ImageBase) && - (Buffer + Length >=3D ImageRecord->ImageBase + ImageRecord->ImageS= ize)) { - return ImageRecord; - } - } - - return NULL; -} - -/** - Set the memory map to new entries, according to one old entry, - based upon PE code section and data section in image record - - @param ImageRecord An image record whose [ImageBase, ImageSi= ze] covered - by old memory map entry. - @param NewRecord A pointer to several new memory map entri= es. - The caller gurantee the buffer size be 1 + - (SplitRecordCount * DescriptorSize) calcu= lated - below. - @param OldRecord A pointer to one old memory map entry. - @param DescriptorSize Size, in bytes, of an individual EFI_MEMO= RY_DESCRIPTOR. -**/ -STATIC -UINTN -SetNewRecord ( - IN IMAGE_PROPERTIES_RECORD *ImageRecord, - IN OUT EFI_MEMORY_DESCRIPTOR *NewRecord, - IN EFI_MEMORY_DESCRIPTOR *OldRecord, - IN UINTN DescriptorSize - ) -{ - EFI_MEMORY_DESCRIPTOR TempRecord; - IMAGE_PROPERTIES_RECORD_CODE_SECTION *ImageRecordCodeSection; - LIST_ENTRY *ImageRecordCodeSectionLink; - LIST_ENTRY *ImageRecordCodeSectionEndLink; - LIST_ENTRY *ImageRecordCodeSectionList; - UINTN NewRecordCount; - UINT64 PhysicalEnd; - UINT64 ImageEnd; - - CopyMem (&TempRecord, OldRecord, sizeof(EFI_MEMORY_DESCRIPTOR)); - PhysicalEnd =3D TempRecord.PhysicalStart + EfiPagesToSize(TempRecord.Num= berOfPages); - NewRecordCount =3D 0; - - ImageRecordCodeSectionList =3D &ImageRecord->CodeSegmentList; - - ImageRecordCodeSectionLink =3D ImageRecordCodeSectionList->ForwardLink; - ImageRecordCodeSectionEndLink =3D ImageRecordCodeSectionList; - while (ImageRecordCodeSectionLink !=3D ImageRecordCodeSectionEndLink) { - ImageRecordCodeSection =3D CR ( - ImageRecordCodeSectionLink, - IMAGE_PROPERTIES_RECORD_CODE_SECTION, - Link, - IMAGE_PROPERTIES_RECORD_CODE_SECTION_SIGNAT= URE - ); - ImageRecordCodeSectionLink =3D ImageRecordCodeSectionLink->ForwardLink; - - if (TempRecord.PhysicalStart <=3D ImageRecordCodeSection->CodeSegmentB= ase) { - // - // DATA - // - NewRecord->Type =3D TempRecord.Type; - NewRecord->PhysicalStart =3D TempRecord.PhysicalStart; - NewRecord->VirtualStart =3D 0; - NewRecord->NumberOfPages =3D EfiSizeToPages(ImageRecordCodeSection->= CodeSegmentBase - NewRecord->PhysicalStart); - NewRecord->Attribute =3D TempRecord.Attribute | EFI_MEMORY_XP; - if (NewRecord->NumberOfPages !=3D 0) { - NewRecord =3D NEXT_MEMORY_DESCRIPTOR (NewRecord, DescriptorSize); - NewRecordCount ++; - } - - // - // CODE - // - NewRecord->Type =3D TempRecord.Type; - NewRecord->PhysicalStart =3D ImageRecordCodeSection->CodeSegmentBase; - NewRecord->VirtualStart =3D 0; - NewRecord->NumberOfPages =3D EfiSizeToPages(ImageRecordCodeSection->= CodeSegmentSize); - NewRecord->Attribute =3D (TempRecord.Attribute & (~EFI_MEMORY_XP= )) | EFI_MEMORY_RO; - if (NewRecord->NumberOfPages !=3D 0) { - NewRecord =3D NEXT_MEMORY_DESCRIPTOR (NewRecord, DescriptorSize); - NewRecordCount ++; - } - - TempRecord.PhysicalStart =3D ImageRecordCodeSection->CodeSegmentBase= + EfiPagesToSize (EfiSizeToPages(ImageRecordCodeSection->CodeSegmentSize)); - TempRecord.NumberOfPages =3D EfiSizeToPages(PhysicalEnd - TempRecord= .PhysicalStart); - if (TempRecord.NumberOfPages =3D=3D 0) { - break; - } - } - } - - ImageEnd =3D ImageRecord->ImageBase + ImageRecord->ImageSize; - - // - // Final DATA - // - if (TempRecord.PhysicalStart < ImageEnd) { - NewRecord->Type =3D TempRecord.Type; - NewRecord->PhysicalStart =3D TempRecord.PhysicalStart; - NewRecord->VirtualStart =3D 0; - NewRecord->NumberOfPages =3D EfiSizeToPages (ImageEnd - TempRecord.Phy= sicalStart); - NewRecord->Attribute =3D TempRecord.Attribute | EFI_MEMORY_XP; - NewRecordCount ++; - } - - return NewRecordCount; -} - -/** - Return the max number of new splitted entries, according to one old entr= y, - based upon PE code section and data section. - - @param OldRecord A pointer to one old memory map entry. - - @retval 0 no entry need to be splitted. - @return the max number of new splitted entries -**/ -STATIC -UINTN -GetMaxSplitRecordCount ( - IN EFI_MEMORY_DESCRIPTOR *OldRecord - ) -{ - IMAGE_PROPERTIES_RECORD *ImageRecord; - UINTN SplitRecordCount; - UINT64 PhysicalStart; - UINT64 PhysicalEnd; - - SplitRecordCount =3D 0; - PhysicalStart =3D OldRecord->PhysicalStart; - PhysicalEnd =3D OldRecord->PhysicalStart + EfiPagesToSize(OldRecord->Num= berOfPages); - - do { - ImageRecord =3D GetImageRecordByAddress (PhysicalStart, PhysicalEnd - = PhysicalStart); - if (ImageRecord =3D=3D NULL) { - break; - } - SplitRecordCount +=3D (2 * ImageRecord->CodeSegmentCount + 1); - PhysicalStart =3D ImageRecord->ImageBase + ImageRecord->ImageSize; - } while ((ImageRecord !=3D NULL) && (PhysicalStart < PhysicalEnd)); - - if (SplitRecordCount !=3D 0) { - SplitRecordCount--; - } - - return SplitRecordCount; -} - -/** - Split the memory map to new entries, according to one old entry, - based upon PE code section and data section. - - @param OldRecord A pointer to one old memory map entry. - @param NewRecord A pointer to several new memory map entri= es. - The caller gurantee the buffer size be 1 + - (SplitRecordCount * DescriptorSize) calcu= lated - below. - @param MaxSplitRecordCount The max number of splitted entries - @param DescriptorSize Size, in bytes, of an individual EFI_MEMO= RY_DESCRIPTOR. - - @retval 0 no entry is splitted. - @return the real number of splitted record. -**/ -STATIC -UINTN -SplitRecord ( - IN EFI_MEMORY_DESCRIPTOR *OldRecord, - IN OUT EFI_MEMORY_DESCRIPTOR *NewRecord, - IN UINTN MaxSplitRecordCount, - IN UINTN DescriptorSize - ) -{ - EFI_MEMORY_DESCRIPTOR TempRecord; - IMAGE_PROPERTIES_RECORD *ImageRecord; - IMAGE_PROPERTIES_RECORD *NewImageRecord; - UINT64 PhysicalStart; - UINT64 PhysicalEnd; - UINTN NewRecordCount; - UINTN TotalNewRecordCount; - BOOLEAN IsLastRecordData; - - if (MaxSplitRecordCount =3D=3D 0) { - CopyMem (NewRecord, OldRecord, DescriptorSize); - return 0; - } - - TotalNewRecordCount =3D 0; - - // - // Override previous record - // - CopyMem (&TempRecord, OldRecord, sizeof(EFI_MEMORY_DESCRIPTOR)); - PhysicalStart =3D TempRecord.PhysicalStart; - PhysicalEnd =3D TempRecord.PhysicalStart + EfiPagesToSize(TempRecord.Num= berOfPages); - - ImageRecord =3D NULL; - do { - NewImageRecord =3D GetImageRecordByAddress (PhysicalStart, PhysicalEnd= - PhysicalStart); - if (NewImageRecord =3D=3D NULL) { - // - // No more image covered by this range, stop - // - if ((PhysicalEnd > PhysicalStart) && (ImageRecord !=3D NULL)) { - // - // If this is still address in this record, need record. - // - NewRecord =3D PREVIOUS_MEMORY_DESCRIPTOR (NewRecord, DescriptorSiz= e); - IsLastRecordData =3D FALSE; - if ((NewRecord->Attribute & EFI_MEMORY_XP) !=3D 0) { - IsLastRecordData =3D TRUE; - } - if (IsLastRecordData) { - // - // Last record is DATA, just merge it. - // - NewRecord->NumberOfPages =3D EfiSizeToPages(PhysicalEnd - NewRec= ord->PhysicalStart); - } else { - // - // Last record is CODE, create a new DATA entry. - // - NewRecord =3D NEXT_MEMORY_DESCRIPTOR (NewRecord, DescriptorSize); - NewRecord->Type =3D TempRecord.Type; - NewRecord->PhysicalStart =3D TempRecord.PhysicalStart; - NewRecord->VirtualStart =3D 0; - NewRecord->NumberOfPages =3D TempRecord.NumberOfPages; - NewRecord->Attribute =3D TempRecord.Attribute | EFI_MEMORY_X= P; - TotalNewRecordCount ++; - } - } - break; - } - ImageRecord =3D NewImageRecord; - - // - // Set new record - // - NewRecordCount =3D SetNewRecord (ImageRecord, NewRecord, &TempRecord, = DescriptorSize); - TotalNewRecordCount +=3D NewRecordCount; - NewRecord =3D (EFI_MEMORY_DESCRIPTOR *)((UINT8 *)NewRecord + NewRecord= Count * DescriptorSize); - - // - // Update PhysicalStart, in order to exclude the image buffer already = splitted. - // - PhysicalStart =3D ImageRecord->ImageBase + ImageRecord->ImageSize; - TempRecord.PhysicalStart =3D PhysicalStart; - TempRecord.NumberOfPages =3D EfiSizeToPages (PhysicalEnd - PhysicalSta= rt); - } while ((ImageRecord !=3D NULL) && (PhysicalStart < PhysicalEnd)); - - // - // The logic in function SplitTable() ensures that TotalNewRecordCount w= ill not be zero if the - // code reaches here. - // - ASSERT (TotalNewRecordCount !=3D 0); - return TotalNewRecordCount - 1; -} - -/** - Split the original memory map, and add more entries to describe PE code = section and data section. - This function will set EfiRuntimeServicesData to be EFI_MEMORY_XP. - This function will merge entries with same attributes finally. - - NOTE: It assumes PE code/data section are page aligned. - NOTE: It assumes enough entry is prepared for new memory map. - - Split table: - +---------------+ - | Record X | - +---------------+ - | Record RtCode | - +---------------+ - | Record Y | - +---------------+ - =3D=3D> - +---------------+ - | Record X | - +---------------+ ---- - | Record RtData | | - +---------------+ | - | Record RtCode | |-> PE/COFF1 - +---------------+ | - | Record RtData | | - +---------------+ ---- - | Record RtData | | - +---------------+ | - | Record RtCode | |-> PE/COFF2 - +---------------+ | - | Record RtData | | - +---------------+ ---- - | Record Y | - +---------------+ - - @param MemoryMapSize A pointer to the size, in bytes, of the - MemoryMap buffer. On input, this is the s= ize of - old MemoryMap before split. The actual bu= ffer - size of MemoryMap is MemoryMapSize + - (AdditionalRecordCount * DescriptorSize) = calculated - below. On output, it is the size of new M= emoryMap - after split. - @param MemoryMap A pointer to the buffer in which firmware= places - the current memory map. - @param DescriptorSize Size, in bytes, of an individual EFI_MEMO= RY_DESCRIPTOR. -**/ -STATIC -VOID -SplitTable ( - IN OUT UINTN *MemoryMapSize, - IN OUT EFI_MEMORY_DESCRIPTOR *MemoryMap, - IN UINTN DescriptorSize - ) -{ - INTN IndexOld; - INTN IndexNew; - UINTN MaxSplitRecordCount; - UINTN RealSplitRecordCount; - UINTN TotalSplitRecordCount; - UINTN AdditionalRecordCount; - - AdditionalRecordCount =3D (2 * mImagePropertiesPrivateData.CodeSegmentCo= untMax + 1) * mImagePropertiesPrivateData.ImageRecordCount; - - TotalSplitRecordCount =3D 0; - // - // Let old record point to end of valid MemoryMap buffer. - // - IndexOld =3D ((*MemoryMapSize) / DescriptorSize) - 1; - // - // Let new record point to end of full MemoryMap buffer. - // - IndexNew =3D ((*MemoryMapSize) / DescriptorSize) - 1 + AdditionalRecordC= ount; - for (; IndexOld >=3D 0; IndexOld--) { - MaxSplitRecordCount =3D GetMaxSplitRecordCount ((EFI_MEMORY_DESCRIPTOR= *)((UINT8 *)MemoryMap + IndexOld * DescriptorSize)); - // - // Split this MemoryMap record - // - IndexNew -=3D MaxSplitRecordCount; - RealSplitRecordCount =3D SplitRecord ( - (EFI_MEMORY_DESCRIPTOR *)((UINT8 *)MemoryMap = + IndexOld * DescriptorSize), - (EFI_MEMORY_DESCRIPTOR *)((UINT8 *)MemoryMap = + IndexNew * DescriptorSize), - MaxSplitRecordCount, - DescriptorSize - ); - // - // Adjust IndexNew according to real split. - // - CopyMem ( - ((UINT8 *)MemoryMap + (IndexNew + MaxSplitRecordCount - RealSplitRec= ordCount) * DescriptorSize), - ((UINT8 *)MemoryMap + IndexNew * DescriptorSize), - RealSplitRecordCount * DescriptorSize - ); - IndexNew =3D IndexNew + MaxSplitRecordCount - RealSplitRecordCount; - TotalSplitRecordCount +=3D RealSplitRecordCount; - IndexNew --; - } - // - // Move all records to the beginning. - // - CopyMem ( - MemoryMap, - (UINT8 *)MemoryMap + (AdditionalRecordCount - TotalSplitRecordCount) *= DescriptorSize, - (*MemoryMapSize) + TotalSplitRecordCount * DescriptorSize - ); - - *MemoryMapSize =3D (*MemoryMapSize) + DescriptorSize * TotalSplitRecordC= ount; - - // - // Sort from low to high (Just in case) - // - SortMemoryMap (MemoryMap, *MemoryMapSize, DescriptorSize); - - // - // Set RuntimeData to XP - // - EnforceMemoryMapAttribute (MemoryMap, *MemoryMapSize, DescriptorSize); - - // - // Merge same type to save entry size - // - MergeMemoryMap (MemoryMap, MemoryMapSize, DescriptorSize); - - return ; -} - -/** - This function for GetMemoryMap() with properties table capability. - - It calls original GetMemoryMap() to get the original memory map informat= ion. Then - plus the additional memory map entries for PE Code/Data seperation. - - @param MemoryMapSize A pointer to the size, in bytes, of the - MemoryMap buffer. On input, this is the s= ize of - the buffer allocated by the caller. On o= utput, - it is the size of the buffer returned by = the - firmware if the buffer was large enough,= or the - size of the buffer needed to contain the= map if - the buffer was too small. - @param MemoryMap A pointer to the buffer in which firmware= places - the current memory map. - @param MapKey A pointer to the location in which firmwa= re - returns the key for the current memory ma= p. - @param DescriptorSize A pointer to the location in which firmwa= re - returns the size, in bytes, of an individ= ual - EFI_MEMORY_DESCRIPTOR. - @param DescriptorVersion A pointer to the location in which firmwa= re - returns the version number associated wit= h the - EFI_MEMORY_DESCRIPTOR. - - @retval EFI_SUCCESS The memory map was returned in the Memory= Map - buffer. - @retval EFI_BUFFER_TOO_SMALL The MemoryMap buffer was too small. The c= urrent - buffer size needed to hold the memory map= is - returned in MemoryMapSize. - @retval EFI_INVALID_PARAMETER One of the parameters has an invalid valu= e. - -**/ -EFI_STATUS -EFIAPI -CoreGetMemoryMapWithSeparatedImageSection ( - IN OUT UINTN *MemoryMapSize, - IN OUT EFI_MEMORY_DESCRIPTOR *MemoryMap, - OUT UINTN *MapKey, - OUT UINTN *DescriptorSize, - OUT UINT32 *DescriptorVersion - ) -{ - EFI_STATUS Status; - UINTN OldMemoryMapSize; - UINTN AdditionalRecordCount; - - // - // If PE code/data is not aligned, just return. - // - if (!mMemoryAttributesTableEnable) { - return CoreGetMemoryMap (MemoryMapSize, MemoryMap, MapKey, DescriptorS= ize, DescriptorVersion); - } - - if (MemoryMapSize =3D=3D NULL) { - return EFI_INVALID_PARAMETER; - } - - CoreAcquirePropertiesTableLock (); - - AdditionalRecordCount =3D (2 * mImagePropertiesPrivateData.CodeSegmentCo= untMax + 1) * mImagePropertiesPrivateData.ImageRecordCount; - - OldMemoryMapSize =3D *MemoryMapSize; - Status =3D CoreGetMemoryMap (MemoryMapSize, MemoryMap, MapKey, Descripto= rSize, DescriptorVersion); - if (Status =3D=3D EFI_BUFFER_TOO_SMALL) { - *MemoryMapSize =3D *MemoryMapSize + (*DescriptorSize) * AdditionalReco= rdCount; - } else if (Status =3D=3D EFI_SUCCESS) { - ASSERT (MemoryMap !=3D NULL); - if (OldMemoryMapSize - *MemoryMapSize < (*DescriptorSize) * Additional= RecordCount) { - *MemoryMapSize =3D *MemoryMapSize + (*DescriptorSize) * AdditionalRe= cordCount; - // - // Need update status to buffer too small - // - Status =3D EFI_BUFFER_TOO_SMALL; - } else { - // - // Split PE code/data - // - SplitTable (MemoryMapSize, MemoryMap, *DescriptorSize); - } - } - - CoreReleasePropertiesTableLock (); - return Status; -} - -// -// Below functions are for ImageRecord -// - -/** - Set PropertiesTable according to PE/COFF image section alignment. - - @param SectionAlignment PE/COFF section alignment -**/ -STATIC -VOID -SetPropertiesTableSectionAlignment ( - IN UINT32 SectionAlignment - ) -{ - if (((SectionAlignment & (RUNTIME_PAGE_ALLOCATION_GRANULARITY - 1)) !=3D= 0) && - mMemoryAttributesTableEnable) { - DEBUG ((EFI_D_VERBOSE, "SetPropertiesTableSectionAlignment - Clear\n")= ); - mMemoryAttributesTableEnable =3D FALSE; - } -} - -/** - Swap two code sections in image record. - - @param FirstImageRecordCodeSection first code section in image record - @param SecondImageRecordCodeSection second code section in image reco= rd -**/ -STATIC -VOID -SwapImageRecordCodeSection ( - IN IMAGE_PROPERTIES_RECORD_CODE_SECTION *FirstImageRecordCodeSectio= n, - IN IMAGE_PROPERTIES_RECORD_CODE_SECTION *SecondImageRecordCodeSecti= on - ) -{ - IMAGE_PROPERTIES_RECORD_CODE_SECTION TempImageRecordCodeSection; - - TempImageRecordCodeSection.CodeSegmentBase =3D FirstImageRecordCodeSecti= on->CodeSegmentBase; - TempImageRecordCodeSection.CodeSegmentSize =3D FirstImageRecordCodeSecti= on->CodeSegmentSize; - - FirstImageRecordCodeSection->CodeSegmentBase =3D SecondImageRecordCodeSe= ction->CodeSegmentBase; - FirstImageRecordCodeSection->CodeSegmentSize =3D SecondImageRecordCodeSe= ction->CodeSegmentSize; - - SecondImageRecordCodeSection->CodeSegmentBase =3D TempImageRecordCodeSec= tion.CodeSegmentBase; - SecondImageRecordCodeSection->CodeSegmentSize =3D TempImageRecordCodeSec= tion.CodeSegmentSize; -} - -/** - Sort code section in image record, based upon CodeSegmentBase from low t= o high. - - @param ImageRecord image record to be sorted -**/ -VOID -SortImageRecordCodeSection ( - IN IMAGE_PROPERTIES_RECORD *ImageRecord - ) -{ - IMAGE_PROPERTIES_RECORD_CODE_SECTION *ImageRecordCodeSection; - IMAGE_PROPERTIES_RECORD_CODE_SECTION *NextImageRecordCodeSection; - LIST_ENTRY *ImageRecordCodeSectionLink; - LIST_ENTRY *NextImageRecordCodeSectionLin= k; - LIST_ENTRY *ImageRecordCodeSectionEndLink; - LIST_ENTRY *ImageRecordCodeSectionList; - - ImageRecordCodeSectionList =3D &ImageRecord->CodeSegmentList; - - ImageRecordCodeSectionLink =3D ImageRecordCodeSectionList->ForwardLink; - NextImageRecordCodeSectionLink =3D ImageRecordCodeSectionLink->ForwardLi= nk; - ImageRecordCodeSectionEndLink =3D ImageRecordCodeSectionList; - while (ImageRecordCodeSectionLink !=3D ImageRecordCodeSectionEndLink) { - ImageRecordCodeSection =3D CR ( - ImageRecordCodeSectionLink, - IMAGE_PROPERTIES_RECORD_CODE_SECTION, - Link, - IMAGE_PROPERTIES_RECORD_CODE_SECTION_SIGNAT= URE - ); - while (NextImageRecordCodeSectionLink !=3D ImageRecordCodeSectionEndLi= nk) { - NextImageRecordCodeSection =3D CR ( - NextImageRecordCodeSectionLink, - IMAGE_PROPERTIES_RECORD_CODE_SECTION, - Link, - IMAGE_PROPERTIES_RECORD_CODE_SECTION_= SIGNATURE - ); - if (ImageRecordCodeSection->CodeSegmentBase > NextImageRecordCodeSec= tion->CodeSegmentBase) { - SwapImageRecordCodeSection (ImageRecordCodeSection, NextImageRecor= dCodeSection); - } - NextImageRecordCodeSectionLink =3D NextImageRecordCodeSectionLink->F= orwardLink; - } - - ImageRecordCodeSectionLink =3D ImageRecordCodeSectionLink->ForwardLink; - NextImageRecordCodeSectionLink =3D ImageRecordCodeSectionLink->Forward= Link; - } -} - -/** - Check if code section in image record is valid. - - @param ImageRecord image record to be checked - - @retval TRUE image record is valid - @retval FALSE image record is invalid -**/ -BOOLEAN -IsImageRecordCodeSectionValid ( - IN IMAGE_PROPERTIES_RECORD *ImageRecord - ) -{ - IMAGE_PROPERTIES_RECORD_CODE_SECTION *ImageRecordCodeSection; - IMAGE_PROPERTIES_RECORD_CODE_SECTION *LastImageRecordCodeSection; - LIST_ENTRY *ImageRecordCodeSectionLink; - LIST_ENTRY *ImageRecordCodeSectionEndLink; - LIST_ENTRY *ImageRecordCodeSectionList; - - DEBUG ((EFI_D_VERBOSE, "ImageCode SegmentCount - 0x%x\n", ImageRecord->C= odeSegmentCount)); - - ImageRecordCodeSectionList =3D &ImageRecord->CodeSegmentList; - - ImageRecordCodeSectionLink =3D ImageRecordCodeSectionList->ForwardLink; - ImageRecordCodeSectionEndLink =3D ImageRecordCodeSectionList; - LastImageRecordCodeSection =3D NULL; - while (ImageRecordCodeSectionLink !=3D ImageRecordCodeSectionEndLink) { - ImageRecordCodeSection =3D CR ( - ImageRecordCodeSectionLink, - IMAGE_PROPERTIES_RECORD_CODE_SECTION, - Link, - IMAGE_PROPERTIES_RECORD_CODE_SECTION_SIGNAT= URE - ); - if (ImageRecordCodeSection->CodeSegmentSize =3D=3D 0) { - return FALSE; - } - if (ImageRecordCodeSection->CodeSegmentBase < ImageRecord->ImageBase) { - return FALSE; - } - if (ImageRecordCodeSection->CodeSegmentBase >=3D MAX_ADDRESS - ImageRe= cordCodeSection->CodeSegmentSize) { - return FALSE; - } - if ((ImageRecordCodeSection->CodeSegmentBase + ImageRecordCodeSection-= >CodeSegmentSize) > (ImageRecord->ImageBase + ImageRecord->ImageSize)) { - return FALSE; - } - if (LastImageRecordCodeSection !=3D NULL) { - if ((LastImageRecordCodeSection->CodeSegmentBase + LastImageRecordCo= deSection->CodeSegmentSize) > ImageRecordCodeSection->CodeSegmentBase) { - return FALSE; - } - } - - LastImageRecordCodeSection =3D ImageRecordCodeSection; - ImageRecordCodeSectionLink =3D ImageRecordCodeSectionLink->ForwardLink; - } - - return TRUE; -} - -/** - Swap two image records. - - @param FirstImageRecord first image record. - @param SecondImageRecord second image record. -**/ -STATIC -VOID -SwapImageRecord ( - IN IMAGE_PROPERTIES_RECORD *FirstImageRecord, - IN IMAGE_PROPERTIES_RECORD *SecondImageRecord - ) -{ - IMAGE_PROPERTIES_RECORD TempImageRecord; - - TempImageRecord.ImageBase =3D FirstImageRecord->ImageBase; - TempImageRecord.ImageSize =3D FirstImageRecord->ImageSize; - TempImageRecord.CodeSegmentCount =3D FirstImageRecord->CodeSegmentCount; - - FirstImageRecord->ImageBase =3D SecondImageRecord->ImageBase; - FirstImageRecord->ImageSize =3D SecondImageRecord->ImageSize; - FirstImageRecord->CodeSegmentCount =3D SecondImageRecord->CodeSegmentCou= nt; - - SecondImageRecord->ImageBase =3D TempImageRecord.ImageBase; - SecondImageRecord->ImageSize =3D TempImageRecord.ImageSize; - SecondImageRecord->CodeSegmentCount =3D TempImageRecord.CodeSegmentCount; - - SwapListEntries (&FirstImageRecord->CodeSegmentList, &SecondImageRecord-= >CodeSegmentList); -} - -/** - Sort image record based upon the ImageBase from low to high. -**/ -STATIC -VOID -SortImageRecord ( - VOID - ) -{ - IMAGE_PROPERTIES_RECORD *ImageRecord; - IMAGE_PROPERTIES_RECORD *NextImageRecord; - LIST_ENTRY *ImageRecordLink; - LIST_ENTRY *NextImageRecordLink; - LIST_ENTRY *ImageRecordEndLink; - LIST_ENTRY *ImageRecordList; - - ImageRecordList =3D &mImagePropertiesPrivateData.ImageRecordList; - - ImageRecordLink =3D ImageRecordList->ForwardLink; - NextImageRecordLink =3D ImageRecordLink->ForwardLink; - ImageRecordEndLink =3D ImageRecordList; - while (ImageRecordLink !=3D ImageRecordEndLink) { - ImageRecord =3D CR ( - ImageRecordLink, - IMAGE_PROPERTIES_RECORD, - Link, - IMAGE_PROPERTIES_RECORD_SIGNATURE - ); - while (NextImageRecordLink !=3D ImageRecordEndLink) { - NextImageRecord =3D CR ( - NextImageRecordLink, - IMAGE_PROPERTIES_RECORD, - Link, - IMAGE_PROPERTIES_RECORD_SIGNATURE - ); - if (ImageRecord->ImageBase > NextImageRecord->ImageBase) { - SwapImageRecord (ImageRecord, NextImageRecord); - } - NextImageRecordLink =3D NextImageRecordLink->ForwardLink; - } - - ImageRecordLink =3D ImageRecordLink->ForwardLink; - NextImageRecordLink =3D ImageRecordLink->ForwardLink; - } -} - -/** - Insert image record. - - @param RuntimeImage Runtime image information -**/ -VOID -InsertImageRecord ( - IN EFI_RUNTIME_IMAGE_ENTRY *RuntimeImage - ) -{ - VOID *ImageAddress; - EFI_IMAGE_DOS_HEADER *DosHdr; - UINT32 PeCoffHeaderOffset; - UINT32 SectionAlignment; - EFI_IMAGE_SECTION_HEADER *Section; - EFI_IMAGE_OPTIONAL_HEADER_PTR_UNION Hdr; - UINT8 *Name; - UINTN Index; - IMAGE_PROPERTIES_RECORD *ImageRecord; - CHAR8 *PdbPointer; - IMAGE_PROPERTIES_RECORD_CODE_SECTION *ImageRecordCodeSection; - - DEBUG ((EFI_D_VERBOSE, "InsertImageRecord - 0x%x\n", RuntimeImage)); - DEBUG ((EFI_D_VERBOSE, "InsertImageRecord - 0x%016lx - 0x%016lx\n", (EFI= _PHYSICAL_ADDRESS)(UINTN)RuntimeImage->ImageBase, RuntimeImage->ImageSize)); - - if (mPropertiesTableEndOfDxe) { - DEBUG ((DEBUG_INFO, "Do not insert runtime image record after EndOfDxe= \n")); - return ; - } - - ImageRecord =3D AllocatePool (sizeof(*ImageRecord)); - if (ImageRecord =3D=3D NULL) { - return ; - } - ImageRecord->Signature =3D IMAGE_PROPERTIES_RECORD_SIGNATURE; - - DEBUG ((EFI_D_VERBOSE, "ImageRecordCount - 0x%x\n", mImagePropertiesPriv= ateData.ImageRecordCount)); - - // - // Step 1: record whole region - // - ImageRecord->ImageBase =3D (EFI_PHYSICAL_ADDRESS)(UINTN)RuntimeImage->Im= ageBase; - ImageRecord->ImageSize =3D RuntimeImage->ImageSize; - - ImageAddress =3D RuntimeImage->ImageBase; - - PdbPointer =3D PeCoffLoaderGetPdbPointer ((VOID*) (UINTN) ImageAddress); - if (PdbPointer !=3D NULL) { - DEBUG ((EFI_D_VERBOSE, " Image - %a\n", PdbPointer)); - } - - // - // Check PE/COFF image - // - DosHdr =3D (EFI_IMAGE_DOS_HEADER *) (UINTN) ImageAddress; - PeCoffHeaderOffset =3D 0; - if (DosHdr->e_magic =3D=3D EFI_IMAGE_DOS_SIGNATURE) { - PeCoffHeaderOffset =3D DosHdr->e_lfanew; - } - - Hdr.Pe32 =3D (EFI_IMAGE_NT_HEADERS32 *)((UINT8 *) (UINTN) ImageAddress += PeCoffHeaderOffset); - if (Hdr.Pe32->Signature !=3D EFI_IMAGE_NT_SIGNATURE) { - DEBUG ((EFI_D_VERBOSE, "Hdr.Pe32->Signature invalid - 0x%x\n", Hdr.Pe3= 2->Signature)); - // It might be image in SMM. - goto Finish; - } - - // - // Get SectionAlignment - // - if (Hdr.Pe32->OptionalHeader.Magic =3D=3D EFI_IMAGE_NT_OPTIONAL_HDR32_MA= GIC) { - SectionAlignment =3D Hdr.Pe32->OptionalHeader.SectionAlignment; - } else { - SectionAlignment =3D Hdr.Pe32Plus->OptionalHeader.SectionAlignment; - } - - SetPropertiesTableSectionAlignment (SectionAlignment); - if ((SectionAlignment & (RUNTIME_PAGE_ALLOCATION_GRANULARITY - 1)) !=3D = 0) { - DEBUG ((EFI_D_WARN, "!!!!!!!! InsertImageRecord - Section Alignment(0= x%x) is not %dK !!!!!!!!\n", - SectionAlignment, RUNTIME_PAGE_ALLOCATION_GRANULARITY >> 10)); - PdbPointer =3D PeCoffLoaderGetPdbPointer ((VOID*) (UINTN) ImageAddress= ); - if (PdbPointer !=3D NULL) { - DEBUG ((EFI_D_WARN, "!!!!!!!! Image - %a !!!!!!!!\n", PdbPointer)); - } - goto Finish; - } - - Section =3D (EFI_IMAGE_SECTION_HEADER *) ( - (UINT8 *) (UINTN) ImageAddress + - PeCoffHeaderOffset + - sizeof(UINT32) + - sizeof(EFI_IMAGE_FILE_HEADER) + - Hdr.Pe32->FileHeader.SizeOfOptionalHeader - ); - ImageRecord->CodeSegmentCount =3D 0; - InitializeListHead (&ImageRecord->CodeSegmentList); - for (Index =3D 0; Index < Hdr.Pe32->FileHeader.NumberOfSections; Index++= ) { - Name =3D Section[Index].Name; - DEBUG (( - EFI_D_VERBOSE, - " Section - '%c%c%c%c%c%c%c%c'\n", - Name[0], - Name[1], - Name[2], - Name[3], - Name[4], - Name[5], - Name[6], - Name[7] - )); - - if ((Section[Index].Characteristics & EFI_IMAGE_SCN_CNT_CODE) !=3D 0) { - DEBUG ((EFI_D_VERBOSE, " VirtualSize - 0x%08x\n", Section[= Index].Misc.VirtualSize)); - DEBUG ((EFI_D_VERBOSE, " VirtualAddress - 0x%08x\n", Section[= Index].VirtualAddress)); - DEBUG ((EFI_D_VERBOSE, " SizeOfRawData - 0x%08x\n", Section[= Index].SizeOfRawData)); - DEBUG ((EFI_D_VERBOSE, " PointerToRawData - 0x%08x\n", Section[= Index].PointerToRawData)); - DEBUG ((EFI_D_VERBOSE, " PointerToRelocations - 0x%08x\n", Section[= Index].PointerToRelocations)); - DEBUG ((EFI_D_VERBOSE, " PointerToLinenumbers - 0x%08x\n", Section[= Index].PointerToLinenumbers)); - DEBUG ((EFI_D_VERBOSE, " NumberOfRelocations - 0x%08x\n", Section[= Index].NumberOfRelocations)); - DEBUG ((EFI_D_VERBOSE, " NumberOfLinenumbers - 0x%08x\n", Section[= Index].NumberOfLinenumbers)); - DEBUG ((EFI_D_VERBOSE, " Characteristics - 0x%08x\n", Section[= Index].Characteristics)); - - // - // Step 2: record code section - // - ImageRecordCodeSection =3D AllocatePool (sizeof(*ImageRecordCodeSect= ion)); - if (ImageRecordCodeSection =3D=3D NULL) { - return ; - } - ImageRecordCodeSection->Signature =3D IMAGE_PROPERTIES_RECORD_CODE_S= ECTION_SIGNATURE; - - ImageRecordCodeSection->CodeSegmentBase =3D (UINTN)ImageAddress + Se= ction[Index].VirtualAddress; - ImageRecordCodeSection->CodeSegmentSize =3D Section[Index].SizeOfRaw= Data; - - DEBUG ((EFI_D_VERBOSE, "ImageCode: 0x%016lx - 0x%016lx\n", ImageReco= rdCodeSection->CodeSegmentBase, ImageRecordCodeSection->CodeSegmentSize)); - - InsertTailList (&ImageRecord->CodeSegmentList, &ImageRecordCodeSecti= on->Link); - ImageRecord->CodeSegmentCount++; - } - } - - if (ImageRecord->CodeSegmentCount =3D=3D 0) { - SetPropertiesTableSectionAlignment (1); - DEBUG ((EFI_D_ERROR, "!!!!!!!! InsertImageRecord - CodeSegmentCount i= s 0 !!!!!!!!\n")); - PdbPointer =3D PeCoffLoaderGetPdbPointer ((VOID*) (UINTN) ImageAddress= ); - if (PdbPointer !=3D NULL) { - DEBUG ((EFI_D_ERROR, "!!!!!!!! Image - %a !!!!!!!!\n", PdbPointer)= ); - } - goto Finish; - } - - // - // Final - // - SortImageRecordCodeSection (ImageRecord); - // - // Check overlap all section in ImageBase/Size - // - if (!IsImageRecordCodeSectionValid (ImageRecord)) { - DEBUG ((EFI_D_ERROR, "IsImageRecordCodeSectionValid - FAIL\n")); - goto Finish; - } - - InsertTailList (&mImagePropertiesPrivateData.ImageRecordList, &ImageReco= rd->Link); - mImagePropertiesPrivateData.ImageRecordCount++; - - if (mImagePropertiesPrivateData.CodeSegmentCountMax < ImageRecord->CodeS= egmentCount) { - mImagePropertiesPrivateData.CodeSegmentCountMax =3D ImageRecord->CodeS= egmentCount; - } - - SortImageRecord (); - -Finish: - return ; -} - -/** - Find image record according to image base and size. - - @param ImageBase Base of PE image - @param ImageSize Size of PE image - - @return image record -**/ -STATIC -IMAGE_PROPERTIES_RECORD * -FindImageRecord ( - IN EFI_PHYSICAL_ADDRESS ImageBase, - IN UINT64 ImageSize - ) -{ - IMAGE_PROPERTIES_RECORD *ImageRecord; - LIST_ENTRY *ImageRecordLink; - LIST_ENTRY *ImageRecordList; - - ImageRecordList =3D &mImagePropertiesPrivateData.ImageRecordList; - - for (ImageRecordLink =3D ImageRecordList->ForwardLink; - ImageRecordLink !=3D ImageRecordList; - ImageRecordLink =3D ImageRecordLink->ForwardLink) { - ImageRecord =3D CR ( - ImageRecordLink, - IMAGE_PROPERTIES_RECORD, - Link, - IMAGE_PROPERTIES_RECORD_SIGNATURE - ); - - if ((ImageBase =3D=3D ImageRecord->ImageBase) && - (ImageSize =3D=3D ImageRecord->ImageSize)) { - return ImageRecord; - } - } - - return NULL; -} - -/** - Remove Image record. - - @param RuntimeImage Runtime image information -**/ -VOID -RemoveImageRecord ( - IN EFI_RUNTIME_IMAGE_ENTRY *RuntimeImage - ) -{ - IMAGE_PROPERTIES_RECORD *ImageRecord; - LIST_ENTRY *CodeSegmentListHead; - IMAGE_PROPERTIES_RECORD_CODE_SECTION *ImageRecordCodeSection; - - DEBUG ((EFI_D_VERBOSE, "RemoveImageRecord - 0x%x\n", RuntimeImage)); - DEBUG ((EFI_D_VERBOSE, "RemoveImageRecord - 0x%016lx - 0x%016lx\n", (EFI= _PHYSICAL_ADDRESS)(UINTN)RuntimeImage->ImageBase, RuntimeImage->ImageSize)); - - if (mPropertiesTableEndOfDxe) { - DEBUG ((DEBUG_INFO, "Do not remove runtime image record after EndOfDxe= \n")); - return ; - } - - ImageRecord =3D FindImageRecord ((EFI_PHYSICAL_ADDRESS)(UINTN)RuntimeIma= ge->ImageBase, RuntimeImage->ImageSize); - if (ImageRecord =3D=3D NULL) { - DEBUG ((EFI_D_ERROR, "!!!!!!!! ImageRecord not found !!!!!!!!\n")); - return ; - } - - CodeSegmentListHead =3D &ImageRecord->CodeSegmentList; - while (!IsListEmpty (CodeSegmentListHead)) { - ImageRecordCodeSection =3D CR ( - CodeSegmentListHead->ForwardLink, - IMAGE_PROPERTIES_RECORD_CODE_SECTION, - Link, - IMAGE_PROPERTIES_RECORD_CODE_SECTION_SIGNAT= URE - ); - RemoveEntryList (&ImageRecordCodeSection->Link); - FreePool (ImageRecordCodeSection); - } - - RemoveEntryList (&ImageRecord->Link); - FreePool (ImageRecord); - mImagePropertiesPrivateData.ImageRecordCount--; -} - - -/** - Install PropertiesTable. - - @param[in] Event The Event this notify function registered to. - @param[in] Context Pointer to the context data registered to the Even= t. -**/ -VOID -EFIAPI -InstallPropertiesTable ( - EFI_EVENT Event, - VOID *Context - ) -{ - mPropertiesTableEndOfDxe =3D TRUE; -} - -/** - Initialize PropertiesTable support. -**/ -VOID -EFIAPI -CoreInitializePropertiesTable ( - VOID - ) -{ - EFI_STATUS Status; - EFI_EVENT EndOfDxeEvent; - - Status =3D gBS->CreateEventEx ( - EVT_NOTIFY_SIGNAL, - TPL_NOTIFY, - InstallPropertiesTable, - NULL, - &gEfiEndOfDxeEventGroupGuid, - &EndOfDxeEvent - ); - ASSERT_EFI_ERROR (Status); - return ; -} --=20 2.17.1 -=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D- Groups.io Links: You receive all messages sent to this group. View/Reply Online (#56391): https://edk2.groups.io/g/devel/message/56391 Mute This Topic: https://groups.io/mt/72560885/1787277 Group Owner: devel+owner@edk2.groups.io Unsubscribe: https://edk2.groups.io/g/devel/unsub [importer@patchew.org] -=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-