diff --git a/MdeModulePkg/Core/Dxe/Mem/Page.c b/MdeModulePkg/Core/Dxe/Mem/Page.c index e4daa741b9..9df64cc105 100644 --- a/MdeModulePkg/Core/Dxe/Mem/Page.c +++ b/MdeModulePkg/Core/Dxe/Mem/Page.c @@ -1459,6 +1459,11 @@ CoreInternalAllocatePages ( // return EFI_NOT_FOUND. // if (Type == AllocateAddress) { + // Page 0 is not allowed to be allocated as it is reserved for null pointer detection + if (Start == 0) { + return EFI_NOT_FOUND; + } + if ((NumberOfPages == 0) || (NumberOfPages > RShiftU64 (MaxAddress, EFI_PAGE_SHIFT))) { diff --git a/MdeModulePkg/Core/DxeIplPeim/Ia32/DxeLoadFunc.c b/MdeModulePkg/Core/DxeIplPeim/Ia32/DxeLoadFunc.c index 60878b4c1a..98aae5a96e 100644 --- a/MdeModulePkg/Core/DxeIplPeim/Ia32/DxeLoadFunc.c +++ b/MdeModulePkg/Core/DxeIplPeim/Ia32/DxeLoadFunc.c @@ -265,14 +265,6 @@ HandOffToDxeCore ( EFI_PEI_VECTOR_HANDOFF_INFO_PPI *VectorHandoffInfoPpi; BOOLEAN BuildPageTablesIa32Pae; - // - // Clear page 0 and mark it as allocated if NULL pointer detection is enabled. - // - if (IsNullDetectionEnabled ()) { - ClearFirst4KPage (HobList.Raw); - BuildMemoryAllocationHob (0, EFI_PAGES_TO_SIZE (1), EfiBootServicesData); - } - Status = PeiServicesAllocatePages (EfiBootServicesData, EFI_SIZE_TO_PAGES (STACK_SIZE), &BaseOfStack); ASSERT_EFI_ERROR (Status); diff --git a/MdeModulePkg/Core/DxeIplPeim/X64/DxeLoadFunc.c b/MdeModulePkg/Core/DxeIplPeim/X64/DxeLoadFunc.c index fa2050cf02..034254baea 100644 --- a/MdeModulePkg/Core/DxeIplPeim/X64/DxeLoadFunc.c +++ b/MdeModulePkg/Core/DxeIplPeim/X64/DxeLoadFunc.c @@ -36,14 +36,6 @@ HandOffToDxeCore ( VOID *GhcbBase; UINTN GhcbSize; - // - // Clear page 0 and mark it as allocated if NULL pointer detection is enabled. - // - if (IsNullDetectionEnabled ()) { - ClearFirst4KPage (HobList.Raw); - BuildMemoryAllocationHob (0, EFI_PAGES_TO_SIZE (1), EfiBootServicesData); - } - // // Get Vector Hand-off Info PPI and build Guided HOB // diff --git a/MdeModulePkg/Core/DxeIplPeim/X64/VirtualMemory.c b/MdeModulePkg/Core/DxeIplPeim/X64/VirtualMemory.c index 1fb2a3b2ea..ee85b311e9 100644 --- a/MdeModulePkg/Core/DxeIplPeim/X64/VirtualMemory.c +++ b/MdeModulePkg/Core/DxeIplPeim/X64/VirtualMemory.c @@ -31,72 +31,6 @@ SPDX-License-Identifier: BSD-2-Clause-Patent // PAGE_TABLE_POOL *mPageTablePool = NULL; -/** - Clear legacy memory located at the first 4K-page, if available. - - This function traverses the whole HOB list to check if memory from 0 to 4095 - exists and has not been allocated, and then clear it if so. - - @param HobStart The start of HobList passed to DxeCore. - -**/ -VOID -ClearFirst4KPage ( - IN VOID *HobStart - ) -{ - EFI_PEI_HOB_POINTERS RscHob; - EFI_PEI_HOB_POINTERS MemHob; - BOOLEAN DoClear; - - RscHob.Raw = HobStart; - MemHob.Raw = HobStart; - DoClear = FALSE; - - // - // Check if page 0 exists and free - // - while ((RscHob.Raw = GetNextHob ( - EFI_HOB_TYPE_RESOURCE_DESCRIPTOR, - RscHob.Raw - )) != NULL) - { - if ((RscHob.ResourceDescriptor->ResourceType == EFI_RESOURCE_SYSTEM_MEMORY) && - (RscHob.ResourceDescriptor->PhysicalStart == 0)) - { - DoClear = TRUE; - // - // Make sure memory at 0-4095 has not been allocated. - // - while ((MemHob.Raw = GetNextHob ( - EFI_HOB_TYPE_MEMORY_ALLOCATION, - MemHob.Raw - )) != NULL) - { - if (MemHob.MemoryAllocation->AllocDescriptor.MemoryBaseAddress - < EFI_PAGE_SIZE) - { - DoClear = FALSE; - break; - } - - MemHob.Raw = GET_NEXT_HOB (MemHob); - } - - break; - } - - RscHob.Raw = GET_NEXT_HOB (RscHob); - } - - if (DoClear) { - DEBUG ((DEBUG_INFO, "Clearing first 4K-page!\r\n")); - SetMem (NULL, EFI_PAGE_SIZE, 0); - } - - return; -} - /** Return configure status of NULL pointer detection feature. diff --git a/MdeModulePkg/Core/DxeIplPeim/X64/VirtualMemory.h b/MdeModulePkg/Core/DxeIplPeim/X64/VirtualMemory.h index e0352844d7..95e88a9c4f 100644 --- a/MdeModulePkg/Core/DxeIplPeim/X64/VirtualMemory.h +++ b/MdeModulePkg/Core/DxeIplPeim/X64/VirtualMemory.h @@ -267,20 +267,6 @@ AsmGetVectorTemplatInfo ( OUT VOID **TemplateBase ); -/** - Clear legacy memory located at the first 4K-page. - - This function traverses the whole HOB list to check if memory from 0 to 4095 - exists and has not been allocated, and then clear it if so. - - @param HobStart The start of HobList passed to DxeCore. - -**/ -VOID -ClearFirst4KPage ( - IN VOID *HobStart - ); - /** Return configure status of NULL pointer detection feature. diff --git a/MdeModulePkg/Core/Pei/Memory/MemoryServices.c b/MdeModulePkg/Core/Pei/Memory/MemoryServices.c index 562092df70..80b2f898b1 100644 --- a/MdeModulePkg/Core/Pei/Memory/MemoryServices.c +++ b/MdeModulePkg/Core/Pei/Memory/MemoryServices.c @@ -613,10 +613,11 @@ PeiAllocatePages ( // // Check to see if on correct boundary for the memory type. - // If not aligned, make the allocation aligned. + // If not aligned, make the allocation aligned and that we are not trying to allocate page 0, which is used for + // null detection. // Padding = *(FreeMemoryTop) & (Granularity - 1); - if ((UINTN)(*FreeMemoryTop - *FreeMemoryBottom) < Padding) { + if (((UINTN)(*FreeMemoryTop - *FreeMemoryBottom) < Padding) || (*(FreeMemoryTop) - Padding == 0)) { DEBUG ((DEBUG_ERROR, "AllocatePages failed: Out of space after padding.\n")); return EFI_OUT_OF_RESOURCES; }