From e2ee015647cfdc49409ee996f0bf70f7248d35e5 Mon Sep 17 00:00:00 2001 From: vit9696 Date: Thu, 6 Feb 2020 19:29:32 +0300 Subject: [PATCH] OcBootManagementLib: Remove AvoidHighAlloc --- Include/Library/OcAppleBootCompatLib.h | 16 --- Include/Library/OcAppleDiskImageLib.h | 3 +- Include/Library/OcAppleRamDiskLib.h | 4 +- Include/Library/OcBootManagementLib.h | 19 --- Include/Library/OcConfigurationLib.h | 1 - .../OcAppleBootCompatLib/BootCompatInternal.h | 25 ---- Library/OcAppleBootCompatLib/CustomSlide.c | 128 ------------------ .../OcAppleBootCompatLib.c | 16 --- .../OcAppleBootCompatLib/ServiceOverrides.c | 4 - .../OcAppleDiskImageLib/OcAppleDiskImageLib.c | 5 +- Library/OcAppleRamDiskLib/OcAppleRamDiskLib.c | 36 ++--- .../BootManagementInternal.h | 3 +- .../OcBootManagementLib/DefaultEntryChoice.c | 32 +---- Library/OcBootManagementLib/DmgBootSupport.c | 5 +- .../OcConfigurationLib/OcConfigurationLib.c | 1 - 15 files changed, 25 insertions(+), 273 deletions(-) diff --git a/Include/Library/OcAppleBootCompatLib.h b/Include/Library/OcAppleBootCompatLib.h index 48bc6906..d1b74869 100644 --- a/Include/Library/OcAppleBootCompatLib.h +++ b/Include/Library/OcAppleBootCompatLib.h @@ -105,20 +105,4 @@ OcAbcInitialize ( IN OC_ABC_SETTINGS *Settings ); -/** - Allocate or deallocate kernel protection zone: a number of pages that reserve the memory - later used by kernel loader. This may also hardcode an ASLR slide, use with extra care. - - @param[in] Allocate Pass TRUE to perform allocation protection and FALSE to release it. - You should pass TRUE as many times as you need and passing FALSE is optional, - as kernel protection zone is automatically released prior to image start. - - @retval EFI_SUCCESS on success. -**/ -EFI_STATUS -EFIAPI -OcHandleKernelProtectionZone ( - IN BOOLEAN Allocate - ); - #endif // OC_APPLE_BOOT_COMPAT_LIB_H diff --git a/Include/Library/OcAppleDiskImageLib.h b/Include/Library/OcAppleDiskImageLib.h index 1b55403d..bc5f0aa4 100644 --- a/Include/Library/OcAppleDiskImageLib.h +++ b/Include/Library/OcAppleDiskImageLib.h @@ -42,8 +42,7 @@ OcAppleDiskImageInitializeContext ( BOOLEAN OcAppleDiskImageInitializeFromFile ( OUT OC_APPLE_DISK_IMAGE_CONTEXT *Context, - IN EFI_FILE_PROTOCOL *File, - IN BOOLEAN AvoidHighMem + IN EFI_FILE_PROTOCOL *File ); VOID diff --git a/Include/Library/OcAppleRamDiskLib.h b/Include/Library/OcAppleRamDiskLib.h index de4d932a..62dbbff4 100644 --- a/Include/Library/OcAppleRamDiskLib.h +++ b/Include/Library/OcAppleRamDiskLib.h @@ -23,15 +23,13 @@ WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. @param[in] Size Requested memory size. @param[in] MemoryType Requested memory type. - @param[in] AvoidHighMem Allocate only in lower 4 GBs of memory. @retval Allocated extent table. **/ CONST APPLE_RAM_DISK_EXTENT_TABLE * OcAppleRamDiskAllocate ( IN UINTN Size, - IN EFI_MEMORY_TYPE MemoryType, - IN BOOLEAN AvoidHighMem + IN EFI_MEMORY_TYPE MemoryType ); /** diff --git a/Include/Library/OcBootManagementLib.h b/Include/Library/OcBootManagementLib.h index 6b4d90cd..75bbcfaa 100755 --- a/Include/Library/OcBootManagementLib.h +++ b/Include/Library/OcBootManagementLib.h @@ -294,17 +294,6 @@ EFI_STATUS OUT EFI_DEVICE_PATH_PROTOCOL **DevicePath OPTIONAL ); -/** - Exposed allocation protector. This function is called when large memory allocations - need to happen on platforms where this logic is specialised. - Pass Allocate = TRUE when allocating and optionally Allocate = FALSE when failed to boot. -**/ -typedef -EFI_STATUS -(EFIAPI *OC_BALLOON_ALLOC) ( - IN BOOLEAN Allocate - ); - /** Custom picker entry. **/ @@ -406,14 +395,6 @@ typedef struct { // VOID *PrivilegeContext; // - // Balloon allocator. On some firmwares (e.g. GA Z68/Z77) memory layout can be problematic: - // - They may only have lower 4 gigabytes useable. - // - They may have issues protecting kernel memory in these lower 4 gigabytes. - // When this function is set, you are required to allocate only in lower 4 gigabytes, - // and each attempt to allocate more than 100 MBs must invoke BalloonAllocator. - // - OC_BALLOON_ALLOC BalloonAllocator; - // // Additional suffix to include by the interface. // CONST CHAR8 *TitleSuffix; diff --git a/Include/Library/OcConfigurationLib.h b/Include/Library/OcConfigurationLib.h index 33f19b72..76c920ac 100644 --- a/Include/Library/OcConfigurationLib.h +++ b/Include/Library/OcConfigurationLib.h @@ -497,7 +497,6 @@ OC_DECLARE (OC_UEFI_INPUT) /// #define OC_UEFI_QUIRKS_FIELDS(_, __) \ _(UINT32 , ExitBootServicesDelay , , 0 , ()) \ - _(BOOLEAN , AvoidHighAlloc , , FALSE , ()) \ _(BOOLEAN , IgnoreInvalidFlexRatio , , FALSE , ()) \ _(BOOLEAN , IgnoreTextInGraphics , , FALSE , ()) \ _(BOOLEAN , ReleaseUsbOwnership , , FALSE , ()) \ diff --git a/Library/OcAppleBootCompatLib/BootCompatInternal.h b/Library/OcAppleBootCompatLib/BootCompatInternal.h index f574f6cf..7d14d833 100644 --- a/Library/OcAppleBootCompatLib/BootCompatInternal.h +++ b/Library/OcAppleBootCompatLib/BootCompatInternal.h @@ -301,14 +301,6 @@ typedef struct SLIDE_SUPPORT_STATE_ { /// Estimated size for kernel itself, device tree, memory map, and rt pages. /// UINTN EstimatedKernelArea; - /// - /// Future kernel area protecting it from other allocations by UEFI. - /// - EFI_PHYSICAL_ADDRESS BalloonArea; - /// - /// Future kernel area size in pages. - /// - UINTN BalloonAreaPages; } SLIDE_SUPPORT_STATE; /** @@ -504,21 +496,4 @@ AppleSlideRestore ( IN OUT OC_BOOT_ARGUMENTS *BootArgs ); -/** - Allocate or deallocate kernel protection zone: a number of pages that reserve the memory - later used by kernel loader. This may also hardcode an ASLR slide, use with extra care. - - @param[in,out] BootCompat Boot compatibility context. - @param[in] Allocate Pass TRUE to perform allocation protection and FALSE to release it. - You should pass TRUE as many times as you need and passing FALSE is optional, - as kernel protection zone is automatically released prior to image start. - - @retval EFI_SUCCESS on success. -**/ -EFI_STATUS -AppleSlideHandleBalloonState ( - IN OUT BOOT_COMPAT_CONTEXT *BootCompat, - IN BOOLEAN Allocate - ); - #endif // BOOT_COMPAT_INTERNAL_H diff --git a/Library/OcAppleBootCompatLib/CustomSlide.c b/Library/OcAppleBootCompatLib/CustomSlide.c index 56e3f508..351c60c2 100644 --- a/Library/OcAppleBootCompatLib/CustomSlide.c +++ b/Library/OcAppleBootCompatLib/CustomSlide.c @@ -809,131 +809,3 @@ AppleSlideRestore ( // HideSlideFromOs (SlideSupport, BootArgs); } - -EFI_STATUS -AppleSlideHandleBalloonState ( - IN OUT BOOT_COMPAT_CONTEXT *BootCompat, - IN BOOLEAN Allocate - ) -{ - EFI_PHYSICAL_ADDRESS AllocatedMapPages; - UINTN MemoryMapSize; - EFI_MEMORY_DESCRIPTOR *MemoryMap; - UINTN MapKey; - EFI_STATUS Status; - UINTN Slide; - UINTN SlidesToReserve; - UINTN DescriptorSize; - UINTN StartAddr; - UINTN StartAddrTmp; - UINTN EndAddr; - UINT32 DescriptorVersion; - OC_CPU_GENERATION CpuGeneration; - BOOLEAN HasSandyOrIvy; - UINTN EstimatedKernelArea; - - if (!Allocate) { - DEBUG (( - DEBUG_INFO, - "OCABC: Freeing balloon area: 0x%Lx (%Lu pages)\n", - (UINT64) BootCompat->SlideSupport.BalloonArea, - (UINT64) BootCompat->SlideSupport.BalloonAreaPages - )); - - if (BootCompat->SlideSupport.BalloonArea != 0) { - gBS->FreePages ( - BootCompat->SlideSupport.BalloonArea, - BootCompat->SlideSupport.BalloonAreaPages - ); - BootCompat->SlideSupport.BalloonArea = 0; - BootCompat->SlideSupport.BalloonAreaPages = 0; - return EFI_SUCCESS; - } - - return EFI_NOT_FOUND; - } - - AllocatedMapPages = BASE_4GB; - Status = GetCurrentMemoryMapAlloc ( - &MemoryMapSize, - &MemoryMap, - &MapKey, - &DescriptorSize, - &DescriptorVersion, - BootCompat->ServicePtrs.GetMemoryMap, ///< I think it is not required... - &AllocatedMapPages - ); - - if (EFI_ERROR (Status)) { - DEBUG ((DEBUG_WARN, "OCABC: Failed to obtain memory map for balloon - %r\n", Status)); - return EFI_OUT_OF_RESOURCES; - } - - CpuGeneration = OcCpuGetGeneration (); - HasSandyOrIvy = CpuGeneration == OcCpuGenerationSandyBridge || - CpuGeneration == OcCpuGenerationIvyBridge; - - EstimatedKernelArea = (UINTN) EFI_PAGES_TO_SIZE ( - CountRuntimePages (MemoryMapSize, MemoryMap, DescriptorSize, NULL) - ) + ESTIMATED_KERNEL_SIZE; - - gBS->FreePages ( - (EFI_PHYSICAL_ADDRESS)(UINTN) MemoryMap, - (UINTN) AllocatedMapPages - ); - - // - // Reserve 5 contiguous slides for now. - // - SlidesToReserve = 5; - - for (Slide = 0; Slide < TOTAL_SLIDE_NUM - SlidesToReserve; Slide += SlidesToReserve) { - GetSlideRangeForValue ( - EstimatedKernelArea, - HasSandyOrIvy, - (UINT8) Slide, - &StartAddr, - &EndAddr - ); - - GetSlideRangeForValue ( - EstimatedKernelArea, - HasSandyOrIvy, - (UINT8) (Slide + SlidesToReserve), - &StartAddrTmp, - &EndAddr - ); - - BootCompat->SlideSupport.BalloonArea = StartAddr; - BootCompat->SlideSupport.BalloonAreaPages = (UINTN) EFI_SIZE_TO_PAGES (EndAddr - StartAddr); - - Status = gBS->AllocatePages ( - AllocateAddress, - EfiBootServicesData, - BootCompat->SlideSupport.BalloonAreaPages, - &BootCompat->SlideSupport.BalloonArea - ); - - if (!EFI_ERROR (Status)) { - DEBUG (( - DEBUG_INFO, - "OCABC: Created balloon at 0x%Lx (%Lu pages)\n", - (UINT64) BootCompat->SlideSupport.BalloonArea, - (UINT64) BootCompat->SlideSupport.BalloonAreaPages - )); - - BootCompat->SlideSupport.HasMemoryMapAnalysis = TRUE; - BootCompat->SlideSupport.ValidSlideCount = 1; - BootCompat->SlideSupport.ValidSlides[0] = (UINT8) Slide; - - return EFI_SUCCESS; - } - - BootCompat->SlideSupport.BalloonArea = 0; - BootCompat->SlideSupport.BalloonAreaPages = 0; - } - - DEBUG ((DEBUG_WARN, "OCABC: Failed to find memory for balloon\n")); - - return EFI_NOT_FOUND; -} diff --git a/Library/OcAppleBootCompatLib/OcAppleBootCompatLib.c b/Library/OcAppleBootCompatLib/OcAppleBootCompatLib.c index 0048f0a6..b1aab67c 100644 --- a/Library/OcAppleBootCompatLib/OcAppleBootCompatLib.c +++ b/Library/OcAppleBootCompatLib/OcAppleBootCompatLib.c @@ -131,19 +131,3 @@ OcAbcInitialize ( return EFI_SUCCESS; } - -EFI_STATUS -EFIAPI -OcHandleKernelProtectionZone ( - IN BOOLEAN Allocate - ) -{ - BOOT_COMPAT_CONTEXT *BootCompat; - - BootCompat = GetBootCompatContext (); - - return AppleSlideHandleBalloonState ( - BootCompat, - Allocate - ); -} diff --git a/Library/OcAppleBootCompatLib/ServiceOverrides.c b/Library/OcAppleBootCompatLib/ServiceOverrides.c index 01928f3e..fb13cd63 100644 --- a/Library/OcAppleBootCompatLib/ServiceOverrides.c +++ b/Library/OcAppleBootCompatLib/ServiceOverrides.c @@ -387,10 +387,6 @@ OcStartImage ( // We failed but other operating systems should be loadable. // --BootCompat->ServiceState.AppleBootNestedCount; - - if (BootCompat->ServiceState.AppleBootNestedCount == 0) { - AppleSlideHandleBalloonState (BootCompat, FALSE); - } } return Status; diff --git a/Library/OcAppleDiskImageLib/OcAppleDiskImageLib.c b/Library/OcAppleDiskImageLib/OcAppleDiskImageLib.c index 4ce57773..7cad6f56 100644 --- a/Library/OcAppleDiskImageLib/OcAppleDiskImageLib.c +++ b/Library/OcAppleDiskImageLib/OcAppleDiskImageLib.c @@ -195,8 +195,7 @@ OcAppleDiskImageInitializeContext ( BOOLEAN OcAppleDiskImageInitializeFromFile ( OUT OC_APPLE_DISK_IMAGE_CONTEXT *Context, - IN EFI_FILE_PROTOCOL *File, - IN BOOLEAN AvoidHighMem + IN EFI_FILE_PROTOCOL *File ) { EFI_STATUS Status; @@ -214,7 +213,7 @@ OcAppleDiskImageInitializeFromFile ( return FALSE; } - ExtentTable = OcAppleRamDiskAllocate (FileSize, EfiACPIMemoryNVS, AvoidHighMem); + ExtentTable = OcAppleRamDiskAllocate (FileSize, EfiACPIMemoryNVS); if (ExtentTable == NULL) { DEBUG ((DEBUG_INFO, "OCBD: Failed to allocate DMG data\n")); return FALSE; diff --git a/Library/OcAppleRamDiskLib/OcAppleRamDiskLib.c b/Library/OcAppleRamDiskLib/OcAppleRamDiskLib.c index a5ba8ea5..1bd3402c 100644 --- a/Library/OcAppleRamDiskLib/OcAppleRamDiskLib.c +++ b/Library/OcAppleRamDiskLib/OcAppleRamDiskLib.c @@ -236,8 +236,7 @@ CONST APPLE_RAM_DISK_EXTENT_TABLE * InternalAppleRamDiskAllocate ( IN UINTN Size, IN EFI_MEMORY_TYPE MemoryType, - IN BOOLEAN PreferHighMem, - IN BOOLEAN AvoidHighMem + IN BOOLEAN PreferHighMem ) { BOOLEAN Result; @@ -269,7 +268,7 @@ InternalAppleRamDiskAllocate ( // in the lower addresses (see more detail in AptioMemoryFix) depending on // KASLR offset generated randomly or with slide boot argument. // - if (PreferHighMem && !AvoidHighMem) { + if (PreferHighMem) { RemainingSize = InternalAllocateRemainingSize ( BASE_4GB, BASE_8EB, @@ -300,7 +299,7 @@ InternalAppleRamDiskAllocate ( // RemainingSize = InternalAllocateRemainingSize ( 0, - AvoidHighMem ? BASE_4GB : BASE_8EB, + BASE_8EB, MemoryType, MemoryMap, MemoryMapSize, @@ -321,36 +320,30 @@ InternalAppleRamDiskAllocate ( CONST APPLE_RAM_DISK_EXTENT_TABLE * OcAppleRamDiskAllocate ( IN UINTN Size, - IN EFI_MEMORY_TYPE MemoryType, - IN BOOLEAN AvoidHighMem + IN EFI_MEMORY_TYPE MemoryType ) { CONST APPLE_RAM_DISK_EXTENT_TABLE *ExtentTable; - if (!AvoidHighMem) { - // - // Try to allocate preferrably above BASE_4GB to avoid colliding with the kernel. - // - ExtentTable = InternalAppleRamDiskAllocate (Size, MemoryType, TRUE, FALSE); - } else { - ExtentTable = NULL; - } + // + // Try to allocate preferrably above BASE_4GB to avoid colliding with the kernel. + // + ExtentTable = InternalAppleRamDiskAllocate (Size, MemoryType, TRUE); if (ExtentTable == NULL) { // // Being here means that we exceeded entry amount in the extent table. // Retry with any addresses. Should never happen in reality. // - ExtentTable = InternalAppleRamDiskAllocate (Size, MemoryType, FALSE, AvoidHighMem); + ExtentTable = InternalAppleRamDiskAllocate (Size, MemoryType, FALSE); } DEBUG (( DEBUG_BULK_INFO, - "OCRAM: Extent allocation of %u bytes (%x) gave %p (avoid high %d)\n", + "OCRAM: Extent allocation of %u bytes (%x) gave %p\n", (UINT32) Size, (UINT32) MemoryType, - ExtentTable, - AvoidHighMem + ExtentTable )); return ExtentTable; @@ -496,9 +489,10 @@ OcAppleRamDiskLoadFile ( ASSERT (FileSize > 0); // - // We need a temporary buffer in lower addresses as several motherboards on APTIO IV - // (e.g. GA-Z87X-UD4H) fail to read directly to high addresses when using FAT filesystem. - // The issue is likely a continuation of AvoidHighAlloc bugs. + // We need a temporary buffer in lower addresses as several motherboards on APTIO IV, + // e.g. GA-Z77P-D3 (rev. 1.1), GA-Z87X-UD4H, etc. fail to read directly to high addresses + // when using FAT filesystem. The original workaround to this was AvoidHighAlloc quirk. + // REF: https://github.com/acidanthera/bugtracker/issues/449 // TmpBuffer = AllocatePool (BASE_4MB); if (TmpBuffer == NULL) { diff --git a/Library/OcBootManagementLib/BootManagementInternal.h b/Library/OcBootManagementLib/BootManagementInternal.h index 54e47dc6..4cefb236 100644 --- a/Library/OcBootManagementLib/BootManagementInternal.h +++ b/Library/OcBootManagementLib/BootManagementInternal.h @@ -51,8 +51,7 @@ EFI_DEVICE_PATH_PROTOCOL * InternalLoadDmg ( IN OUT INTERNAL_DMG_LOAD_CONTEXT *Context, IN APPLE_BOOT_POLICY_PROTOCOL *BootPolicy, - IN UINT32 Policy, - IN BOOLEAN AvoidHighMem + IN UINT32 Policy ); VOID diff --git a/Library/OcBootManagementLib/DefaultEntryChoice.c b/Library/OcBootManagementLib/DefaultEntryChoice.c index 90a380d3..b9cc5a63 100644 --- a/Library/OcBootManagementLib/DefaultEntryChoice.c +++ b/Library/OcBootManagementLib/DefaultEntryChoice.c @@ -1105,7 +1105,6 @@ InternalLoadBootEntry ( UINT32 EntryDataSize; CONST CHAR8 *Args; UINT32 ArgsLen; - BOOLEAN UseBallooning; ASSERT (BootPolicy != NULL); ASSERT (BootEntry != NULL); @@ -1122,8 +1121,6 @@ InternalLoadBootEntry ( ZeroMem (DmgLoadContext, sizeof (*DmgLoadContext)); - UseBallooning = FALSE; - EntryData = NULL; EntryDataSize = 0; @@ -1132,30 +1129,13 @@ InternalLoadBootEntry ( return EFI_SECURITY_VIOLATION; } - // - // Assume that DMG load requires a lot of memory. - // - UseBallooning = Context->BalloonAllocator != NULL; - -#ifdef OC_ENABLE_BALLOONING - if (UseBallooning) { - Context->BalloonAllocator (TRUE); - } -#endif - DmgLoadContext->DevicePath = BootEntry->DevicePath; DevicePath = InternalLoadDmg ( DmgLoadContext, BootPolicy, - Context->LoadPolicy, - UseBallooning + Context->LoadPolicy ); if (DevicePath == NULL) { -#ifdef OC_ENABLE_BALLOONING - if (UseBallooning) { - Context->BalloonAllocator (FALSE); - } -#endif return EFI_UNSUPPORTED; } } else if (BootEntry->Type == OcBootCustom && BootEntry->DevicePath == NULL) { @@ -1181,12 +1161,11 @@ InternalLoadBootEntry ( UnicodeDevicePath = ConvertDevicePathToText (DevicePath, FALSE, FALSE); DEBUG (( DEBUG_INFO, - "OCB: Perform boot %s to dp %s (%p/%u), balloon %d\n", + "OCB: Perform boot %s to dp %s (%p/%u)\n", BootEntry->Name, UnicodeDevicePath != NULL ? UnicodeDevicePath : L"", EntryData, - EntryDataSize, - UseBallooning + EntryDataSize )); if (UnicodeDevicePath != NULL) { FreePool (UnicodeDevicePath); @@ -1259,11 +1238,6 @@ InternalLoadBootEntry ( } } else { InternalUnloadDmg (DmgLoadContext); -#ifdef OC_ENABLE_BALLOONING - if (UseBallooning) { - Context->BalloonAllocator (FALSE); - } -#endif } return Status; diff --git a/Library/OcBootManagementLib/DmgBootSupport.c b/Library/OcBootManagementLib/DmgBootSupport.c index 1c44a816..66160f11 100644 --- a/Library/OcBootManagementLib/DmgBootSupport.c +++ b/Library/OcBootManagementLib/DmgBootSupport.c @@ -320,8 +320,7 @@ EFI_DEVICE_PATH_PROTOCOL * InternalLoadDmg ( IN OUT INTERNAL_DMG_LOAD_CONTEXT *Context, IN APPLE_BOOT_POLICY_PROTOCOL *BootPolicy, - IN UINT32 Policy, - IN BOOLEAN AvoidHighMem + IN UINT32 Policy ) { EFI_DEVICE_PATH_PROTOCOL *DevPath; @@ -415,7 +414,7 @@ InternalLoadDmg ( return NULL; } - Result = OcAppleDiskImageInitializeFromFile (Context->DmgContext, DmgFile, AvoidHighMem); + Result = OcAppleDiskImageInitializeFromFile (Context->DmgContext, DmgFile); DmgFile->Close (DmgFile); diff --git a/Library/OcConfigurationLib/OcConfigurationLib.c b/Library/OcConfigurationLib/OcConfigurationLib.c index 1e77278c..db1ca562 100644 --- a/Library/OcConfigurationLib/OcConfigurationLib.c +++ b/Library/OcConfigurationLib/OcConfigurationLib.c @@ -521,7 +521,6 @@ mUefiDriversSchema = OC_SCHEMA_STRING (NULL); STATIC OC_SCHEMA mUefiQuirksSchema[] = { - OC_SCHEMA_BOOLEAN_IN ("AvoidHighAlloc", OC_GLOBAL_CONFIG, Uefi.Quirks.AvoidHighAlloc), OC_SCHEMA_BOOLEAN_IN ("ClearScreenOnModeSwitch",OC_GLOBAL_CONFIG, Uefi.Quirks.ClearScreenOnModeSwitch), OC_SCHEMA_INTEGER_IN ("ExitBootServicesDelay", OC_GLOBAL_CONFIG, Uefi.Quirks.ExitBootServicesDelay), OC_SCHEMA_BOOLEAN_IN ("IgnoreInvalidFlexRatio", OC_GLOBAL_CONFIG, Uefi.Quirks.IgnoreInvalidFlexRatio),