OcBootManagementLib: Remove AvoidHighAlloc

This commit is contained in:
vit9696 2020-02-06 19:29:32 +03:00
parent 4ca2dd0185
commit e2ee015647
15 changed files with 25 additions and 273 deletions

View File

@ -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

View File

@ -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

View File

@ -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
);
/**

View File

@ -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;

View File

@ -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 , ()) \

View File

@ -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

View File

@ -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;
}

View File

@ -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
);
}

View File

@ -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;

View File

@ -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;

View File

@ -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) {

View File

@ -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

View File

@ -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"<null>",
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;

View File

@ -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);

View File

@ -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),