From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from smtp-fw-4101.amazon.com (smtp-fw-4101.amazon.com [72.21.198.25]) by mx.groups.io with SMTP id smtpd.web11.216.1613679016987245757 for ; Thu, 18 Feb 2021 12:10:17 -0800 Authentication-Results: mx.groups.io; dkim=pass header.i=@amazon.com header.s=amazon201209 header.b=bgCMBZto; spf=pass (domain: amazon.de, ip: 72.21.198.25, mailfrom: prvs=6763d3f84=graf@amazon.de) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amazon.com; i=@amazon.com; q=dns/txt; s=amazon201209; t=1613679017; x=1645215017; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=tDEHyCv5pwicavV8+0lDD4xz+qrbaQw7JxvuMF74MCU=; b=bgCMBZtoqt1vkjYV/rJvQ2J78x9OzkwnI+hrptlpVwFR5hhRqQHDdYr8 6BipLL1weVeNcIY24ka3I3OzyU5YjwCgTN9luf/rGSazdWS0qC5WD+zUk li61jHa2RIx7OH/bdJA4OtPd0lLS1J6q9LVl9NSG55JPLl4B2KEnrM2DB g=; X-IronPort-AV: E=Sophos;i="5.81,187,1610409600"; d="scan'208";a="85162076" Received: from iad12-co-svc-p1-lb1-vlan2.amazon.com (HELO email-inbound-relay-1e-303d0b0e.us-east-1.amazon.com) ([10.43.8.2]) by smtp-border-fw-out-4101.iad4.amazon.com with ESMTP; 18 Feb 2021 20:10:07 +0000 Received: from EX13MTAUWC002.ant.amazon.com (iad12-ws-svc-p26-lb9-vlan3.iad.amazon.com [10.40.163.38]) by email-inbound-relay-1e-303d0b0e.us-east-1.amazon.com (Postfix) with ESMTPS id 2BC7CA2466; Thu, 18 Feb 2021 20:10:04 +0000 (UTC) Received: from EX13D20UWC001.ant.amazon.com (10.43.162.244) by EX13MTAUWC002.ant.amazon.com (10.43.162.240) with Microsoft SMTP Server (TLS) id 15.0.1497.2; Thu, 18 Feb 2021 20:10:04 +0000 Received: from u79c5a0a55de558.ant.amazon.com (10.43.162.213) by EX13D20UWC001.ant.amazon.com (10.43.162.244) with Microsoft SMTP Server (TLS) id 15.0.1497.2; Thu, 18 Feb 2021 20:10:02 +0000 From: "Alexander Graf" To: CC: Leif Lindholm , Laszlo Ersek , "Ard Biesheuvel" , Jordan Justen , David Woodhouse , "Hendrik Borghorst" Subject: [PATCH 1/2] MdeModulePkg/Core/Dxe: Allow to force runtime allocations at separate range Date: Thu, 18 Feb 2021 21:09:52 +0100 Message-ID: <20210218200953.20943-2-graf@amazon.com> X-Mailer: git-send-email 2.28.0.394.ge197136389 In-Reply-To: <20210218200953.20943-1-graf@amazon.com> References: <20210218200953.20943-1-graf@amazon.com> MIME-Version: 1.0 X-Originating-IP: [10.43.162.213] X-ClientProxiedBy: EX13D13UWB004.ant.amazon.com (10.43.161.218) To EX13D20UWC001.ant.amazon.com (10.43.162.244) Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: quoted-printable Operating Systems that get hibernated expect all non-boot-time allocations to be identical before and after hibernation. In edk2, we create pools and allocate pages starting from the highest allowed address for the allocation, usually 0xFFFFFFFF. Typically, that means we allocate a few pages of boot time data, then a few pages of runtime data, then another few pages of boot time data and again runtime data. Every allocation has direct impact on the following allocations. The problem with this scheme is that small code changes in boot time code already can have significant impact on runtime allocations, which then break hibernation. This patch adds a mechanism to override the MaxAddress for runtime allocations with a target defined Pcd value. With this feature enabled, we can have different allocation ranges for runtime and boot time allocations. This allows us to determine at boot time whether to load an old, hibernation compatible runtime allocation path or a new, hibernation unsafe runtime allocation. All within the same edk2 target binary. It also allows us to modify boot time behavior, such as modifying buffer allocation mechanisms without compromising on hibernation safety. Signed-off-by: Alexander Graf --- MdeModulePkg/Core/Dxe/DxeMain.inf | 4 +++ MdeModulePkg/Core/Dxe/Mem/Page.c | 70 +++++++++++++++++++++++++++++++++++= ++++ MdeModulePkg/MdeModulePkg.dec | 16 +++++++++ MdeModulePkg/MdeModulePkg.uni | 12 +++++++ 4 files changed, 102 insertions(+) diff --git a/MdeModulePkg/Core/Dxe/DxeMain.inf b/MdeModulePkg/Core/Dxe/DxeM= ain.inf index e4bca89577..0696246970 100644 --- a/MdeModulePkg/Core/Dxe/DxeMain.inf +++ b/MdeModulePkg/Core/Dxe/DxeMain.inf @@ -186,6 +186,10 @@ gEfiMdeModulePkgTokenSpaceGuid.PcdHeapGuardPropertyMask = ## CONSUMES gEfiMdeModulePkgTokenSpaceGuid.PcdCpuStackGuard = ## CONSUMES gEfiMdeModulePkgTokenSpaceGuid.PcdFwVolDxeMaxEncapsulationDepth = ## CONSUMES + gEfiMdeModulePkgTokenSpaceGuid.PcdEnforceMaxACPIReclaimMemory = ## CONSUMES + gEfiMdeModulePkgTokenSpaceGuid.PcdEnforceMaxACPIMemoryNVS = ## CONSUMES + gEfiMdeModulePkgTokenSpaceGuid.PcdEnforceMaxEfiRuntimeServicesCode = ## CONSUMES + gEfiMdeModulePkgTokenSpaceGuid.PcdEnforceMaxEfiRuntimeServicesData = ## CONSUMES = # [Hob] # RESOURCE_DESCRIPTOR ## CONSUMES diff --git a/MdeModulePkg/Core/Dxe/Mem/Page.c b/MdeModulePkg/Core/Dxe/Mem/P= age.c index 731bf08bc9..91599adccb 100644 --- a/MdeModulePkg/Core/Dxe/Mem/Page.c +++ b/MdeModulePkg/Core/Dxe/Mem/Page.c @@ -1007,6 +1007,74 @@ CoreUpdateMemoryAttributes ( CoreReleaseMemoryLock (); } = +UINT64 +EnforceMaxAddress ( + IN UINT64 MaxAddress, + IN EFI_MEMORY_TYPE NewType, + IN UINT64 NumberOfPages + ) +{ + UINT64 NumberOfBytes =3D LShiftU64 (NumberOfPages, EFI_PAGE_SHIFT); + UINT64 LowestPossible =3D MaxAddress; + UINT64 ForceMaxAddress; + LIST_ENTRY *Link; + MEMORY_MAP *Entry; + + switch (NewType) { + case EfiACPIReclaimMemory: + ForceMaxAddress =3D PcdGet64(PcdEnforceMaxACPIReclaimMemory); + break; + case EfiACPIMemoryNVS: + ForceMaxAddress =3D PcdGet64(PcdEnforceMaxACPIMemoryNVS); + break; + case EfiRuntimeServicesCode: + ForceMaxAddress =3D PcdGet64(PcdEnforceMaxEfiRuntimeServicesCode); + break; + case EfiRuntimeServicesData: + ForceMaxAddress =3D PcdGet64(PcdEnforceMaxEfiRuntimeServicesData); + break; + default: + ForceMaxAddress =3D MaxAddress; + break; + } + + // + // The currently requested address already fits our forced max constrain= t? + // Great, let's use that then. + // + if (ForceMaxAddress >=3D MaxAddress) { + return MaxAddress; + } + + // + // Check if the allocation would fit. If not, don't force it. + // + for (Link =3D gMemoryMap.ForwardLink; Link !=3D &gMemoryMap; Link =3D Li= nk->ForwardLink) { + Entry =3D CR (Link, MEMORY_MAP, Link, MEMORY_MAP_SIGNATURE); + + // + // If it's not a free entry, don't bother with it + // + if (Entry->Type !=3D EfiConventionalMemory) { + continue; + } + + if ((Entry->Start < LowestPossible) && + ((Entry->End - Entry->Start) >=3D NumberOfBytes)) { + LowestPossible =3D Entry->End; + } + } + DEBUG ((DEBUG_ERROR | DEBUG_PAGE, "Force=3D%lx Lowest=3D%lx Max=3D%lx\n"= , ForceMaxAddress, LowestPossible, MaxAddress)); + + // + // We don't have free RAM available in the desired target area? Bail out! + // + if (ForceMaxAddress < LowestPossible) { + return MaxAddress; + } + + return ForceMaxAddress; +} = /** Internal function. Finds a consecutive free page range below @@ -1041,6 +1109,8 @@ CoreFindFreePagesI ( LIST_ENTRY *Link; MEMORY_MAP *Entry; = + MaxAddress =3D EnforceMaxAddress(MaxAddress, NewType, NumberOfPages); + if ((MaxAddress < EFI_PAGE_MASK) ||(NumberOfPages =3D=3D 0)) { return 0; } diff --git a/MdeModulePkg/MdeModulePkg.dec b/MdeModulePkg/MdeModulePkg.dec index 1483955110..cbad48af5e 100644 --- a/MdeModulePkg/MdeModulePkg.dec +++ b/MdeModulePkg/MdeModulePkg.dec @@ -1535,6 +1535,22 @@ # @Prompt Maximum permitted FwVol section nesting depth (exclusive). gEfiMdeModulePkgTokenSpaceGuid.PcdFwVolDxeMaxEncapsulationDepth|0x10|UIN= T32|0x00000030 = + ## Maximum address that a dynamic EfiACPIReclaimMemory allocation can be= requested at + # @Prompt Maximum address for EfiACPIReclaimMemory allocations + gEfiMdeModulePkgTokenSpaceGuid.PcdEnforceMaxACPIReclaimMemory|0xFFFFFFFF= FFFFFFFF|UINT64|0x30001016 + + ## Maximum address that a dynamic EfiACPIMemoryNVS allocation can be req= uested at + # @Prompt Maximum address for EfiACPIMemoryNVS allocations + gEfiMdeModulePkgTokenSpaceGuid.PcdEnforceMaxACPIMemoryNVS|0xFFFFFFFFFFFF= FFFF|UINT64|0x30001017 + + ## Maximum address that a dynamic EfiRuntimeServicesCode allocation can = be requested at + # @Prompt Maximum address for EfiRuntimeServicesCode allocations + gEfiMdeModulePkgTokenSpaceGuid.PcdEnforceMaxEfiRuntimeServicesCode|0xFFF= FFFFFFFFFFFFF|UINT64|0x30001018 + + ## Maximum address that a dynamic EfiRuntimeServicesData allocation can = be requested at + # @Prompt Maximum address for EfiRuntimeServicesData allocations + gEfiMdeModulePkgTokenSpaceGuid.PcdEnforceMaxEfiRuntimeServicesData|0xFFF= FFFFFFFFFFFFF|UINT64|0x30001019 + [PcdsPatchableInModule, PcdsDynamic, PcdsDynamicEx] ## This PCD defines the Console output row. The default value is 25 acco= rding to UEFI spec. # This PCD could be set to 0 then console output would be at max column= and max row. diff --git a/MdeModulePkg/MdeModulePkg.uni b/MdeModulePkg/MdeModulePkg.uni index ef9f4d97b9..0dc5c1960b 100644 --- a/MdeModulePkg/MdeModulePkg.uni +++ b/MdeModulePkg/MdeModulePkg.uni @@ -1330,3 +1330,15 @@ #string STR_gEfiMdeModulePkgTokenSpaceGuid_PcdPcieResizableBarSupport_HELP= #language en-US "Indicates if the PCIe Resizable BAR Capability Supported.=

\n" = "TRUE - PCIe Resizable BAR Capability is supported.
\= n" = "FALSE - PCIe Resizable BAR Capability is not supported.<= BR>" + +#string STR_gEfiMdeModulePkgTokenSpaceGuid.PcdEnforceMaxACPIReclaimMemory_= PROMPT #language en-US "Maximum address for EfiACPIReclaimMemory allocation= s" +#string STR_gEfiMdeModulePkgTokenSpaceGuid.PcdEnforceMaxACPIReclaimMemory_= HELP #language en-US "Maximum address that a dynamic EfiACPIReclaimMemory a= llocation can be requested at" + +#string STR_gEfiMdeModulePkgTokenSpaceGuid.PcdEnforceMaxACPIMemoryNVS_PROM= PT #language en-US "Maximum address for EfiACPIMemoryNVS allocations" +#string STR_gEfiMdeModulePkgTokenSpaceGuid.PcdEnforceMaxACPIMemoryNVS_HELP= #language en-US "Maximum address that a dynamic EfiACPIMemoryNVS allocatio= n can be requested at" + +#string STR_gEfiMdeModulePkgTokenSpaceGuid.PcdEnforceMaxEfiRuntimeServices= Code_PROMPT #language en-US "Maximum address for EfiRuntimeServicesCode all= ocations" +#string STR_gEfiMdeModulePkgTokenSpaceGuid.PcdEnforceMaxEfiRuntimeServices= Code_HELP #language en-US "Maximum address that a dynamic EfiRuntimeService= sCode allocation can be requested at" + +#string STR_gEfiMdeModulePkgTokenSpaceGuid.PcdEnforceMaxEfiRuntimeServices= Data_PROMPT #language en-US "Maximum address for EfiRuntimeServicesData all= ocations" +#string STR_gEfiMdeModulePkgTokenSpaceGuid.PcdEnforceMaxEfiRuntimeServices= Data_HELP #language en-US "Maximum address that a dynamic EfiRuntimeService= sData allocation can be requested at" -- = 2.16.4 Amazon Development Center Germany GmbH Krausenstr. 38 10117 Berlin Geschaeftsfuehrung: Christian Schlaeger, Jonathan Weiss Eingetragen am Amtsgericht Charlottenburg unter HRB 149173 B Sitz: Berlin Ust-ID: DE 289 237 879