Skip to content

Commit

Permalink
Create memory bin override library to allow for more extensive platfo…
Browse files Browse the repository at this point in the history
…rm customization (#194)

Adds a memory bin override library that allows a platform to customize
logic around memory bin locations in DXE.

- [ ] Impacts functionality?
- [ ] Impacts security?
- [x] Breaking change?
- [ ] Includes tests?
- [ ] Includes documentation?

Tested on the Q35 platform with a custom memory bin override library

Add the new NULL version of the library to the platform DSC file
```
MemoryBinOverrideLib|MdeModulePkg/Library/MemoryBinOverrideLibNull/MemoryBinOverrideLibNull.inf
```
  • Loading branch information
cfernald authored and kenlautner committed May 9, 2023
1 parent 9a7c897 commit 76b86ea
Show file tree
Hide file tree
Showing 9 changed files with 172 additions and 5 deletions.
1 change: 1 addition & 0 deletions MdeModulePkg/Core/Dxe/DxeMain.inf
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@
PcdLib
BaseBinSecurityLib ## MS_CHANGE_?
DxeMemoryProtectionHobLib ## MU_CHANGE
MemoryBinOverrideLib ## MU_CHANGE

[Guids]
gEfiEventMemoryMapChangeGuid ## PRODUCES ## Event
Expand Down
28 changes: 26 additions & 2 deletions MdeModulePkg/Core/Dxe/Mem/Page.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ SPDX-License-Identifier: BSD-2-Clause-Patent
#include "Imem.h"
#include "HeapGuard.h"
#include <Pi/PrePiDxeCis.h>
#include "MemoryProtectionSupport.h" // MU_CHANGE
#include "MemoryProtectionSupport.h" // MU_CHANGE
#include <Library/MemoryBinOverrideLib.h> // MU_CHANGE

//
// Entry for tracking the memory regions for each memory type to coalesce similar memory types
Expand Down Expand Up @@ -570,6 +571,7 @@ CoreAddMemoryDescriptor (
EFI_STATUS Status;
UINTN Index;
UINTN FreeIndex;
EFI_ALLOCATE_TYPE AllocationType; // MU_CHANGE

if ((Start & EFI_PAGE_MASK) != 0) {
return;
Expand Down Expand Up @@ -619,16 +621,28 @@ CoreAddMemoryDescriptor (
}

if (gMemoryTypeInformation[Index].NumberOfPages != 0) {
// MU_CHANGE START Allow overriding of bin locations.
AllocationType = AllocateAnyPages;
GetMemoryBinOverride (
Type,
&mMemoryTypeStatistics[Type].BaseAddress,
&gMemoryTypeInformation[Index].NumberOfPages,
&AllocationType
);
// MU_CHANGE END

//
// Allocate pages for the current memory type from the top of available memory
//
Status = CoreAllocatePages (
AllocateAnyPages,
AllocationType, // MU_CHANGE
Type,
gMemoryTypeInformation[Index].NumberOfPages,
&mMemoryTypeStatistics[Type].BaseAddress
);
if (EFI_ERROR (Status)) {
mMemoryTypeStatistics[Type].BaseAddress = 0; // MU_CHANGE

//
// If an error occurs allocating the pages for the current memory type, then
// free all the pages allocates for the previous memory types and return. This
Expand Down Expand Up @@ -695,6 +709,15 @@ CoreAddMemoryDescriptor (
mMemoryTypeStatistics[Type].NumberOfPages = gMemoryTypeInformation[Index].NumberOfPages;
gMemoryTypeInformation[Index].NumberOfPages = 0;
}

// MU_CHANGE START
ReportMemoryBinLocation (
Type,
mMemoryTypeStatistics[Type].BaseAddress,
mMemoryTypeStatistics[Type].NumberOfPages
);

// MU_CHANGE END
}

//
Expand Down Expand Up @@ -1545,6 +1568,7 @@ CoreInternalFreePages (
MEMORY_MAP *Entry;
UINTN Alignment;
BOOLEAN IsGuarded;

// MU_CHANGE Start: Unprotect page(s) before free if the memory will be cleared on free
UINT64 Attributes;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@
PcdLib
BaseBinSecurityLib
DxeMemoryProtectionHobLib
MemoryBinOverrideLib

[Protocols]
gEfiDecompressProtocolGuid
Expand Down Expand Up @@ -176,6 +177,7 @@
gMuEventPreExitBootServicesGuid
gDxeMemoryProtectionSettingsGuid
gMemoryProtectionSpecialRegionHobGuid
gEfiEventBeforeExitBootServicesGuid

[BuildOptions.Common]
MSFT:*_*_*_CC_FLAGS = -I$(WORKSPACE)/MdeModulePkg/Core/Dxe
Expand Down
48 changes: 48 additions & 0 deletions MdeModulePkg/Include/Library/MemoryBinOverrideLib.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/** @file
Header definitions for the memory bin override library. This library allows
a platform to override the location or size of the memory type bins.
Copyright (c) Microsoft Corporation
SPDX-License-Identifier: BSD-2-Clause-Patent
**/

#ifndef MEMORY_BIN_OVERRIDE_LIB_H__
#define MEMORY_BIN_OVERRIDE_LIB_H__

/**
Reports a runtime memory bin location to the override library.
@param[in] Type The memory type for the reported bin.
@param[in] BaseAddress The base physical address of the reported bin.
@param[in] NumberOfPages The number of pages in the reported bin.
**/
VOID
EFIAPI
ReportMemoryBinLocation (
IN EFI_MEMORY_TYPE Type,
IN EFI_PHYSICAL_ADDRESS BaseAddress,
IN UINT64 NumberOfPages
);

/**
Checks if the library needs to override the given memory bin allocation type,
location, and size. If this function encounters internal errors, the
parameters should remain unchanged.
@param[in] Type The memory type of the bin.
@param[out] BaseAddress The base address of the bin override on return.
@param[out] NumberOfPages The number of pages of the bin override on return.
@param[out] AllocationType The allocation type for the bin, AllocateAddress
if an override was provided.
**/
VOID
EFIAPI
GetMemoryBinOverride (
IN EFI_MEMORY_TYPE Type,
OUT EFI_PHYSICAL_ADDRESS *BaseAddress,
OUT UINT32 *NumberOfPages,
OUT EFI_ALLOCATE_TYPE *AllocationType
);

#endif
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/**
NULL implementation of the memory bin override lib.
Copyright (c) Microsoft Corporation. All rights reserved.
SPDX-License-Identifier: BSD-2-Clause-Patent
**/

#include <Uefi.h>
#include <Library/BaseLib.h>
#include <Library/MemoryBinOverrideLib.h>

/**
Reports a runtime memory bin location to the override library.
@param[in] Type The memory type for the reported bin.
@param[in] BaseAddress The base physical address of the reported bin.
@param[in] NumberOfPages The number of pages in the reported bin.
**/
VOID
EFIAPI
ReportMemoryBinLocation (
IN EFI_MEMORY_TYPE Type,
IN EFI_PHYSICAL_ADDRESS BaseAddress,
IN UINT64 NumberOfPages
)
{
return;
}

/**
Checks if the library needs to override the given memory bin allocation type,
location, and size. If this function encounters internal errors, the
parameters should remain unchanged.
@param[in] Type The memory type of the bin.
@param[out] BaseAddress The base address of the bin override on return.
@param[out] NumberOfPages The number of pages of the bin override on return.
@param[out] AllocationType The allocation type for the bin, AllocateAddress
if an override was provided.
**/
VOID
EFIAPI
GetMemoryBinOverride (
IN EFI_MEMORY_TYPE Type,
OUT EFI_PHYSICAL_ADDRESS *BaseAddress,
OUT UINT32 *NumberOfPages,
OUT EFI_ALLOCATE_TYPE *AllocationType
)
{
return;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
## @file
# NULL implementation of the memory bin override lib.
#
# Copyright (c) Microsoft Corporation. All rights reserved.
# SPDX-License-Identifier: BSD-2-Clause-Patent
#
##

[Defines]
INF_VERSION = 1.27
BASE_NAME = MemoryOverrideLibNull
FILE_GUID = 368687CE-3189-4C26-A6EB-615B64CAA911
MODULE_TYPE = DXE_CORE
VERSION_STRING = 1.0
LIBRARY_CLASS = MemoryBinOverrideLib

#
# The following information is for reference only and not required by the build tools.
#
# VALID_ARCHITECTURES = IA32 X64 AARCH64
#

[Sources]
MemoryBinOverrideLibNull.c

[Packages]
MdePkg/MdePkg.dec
MdeModulePkg/MdeModulePkg.dec

[LibraryClasses]
BaseLib
12 changes: 9 additions & 3 deletions MdeModulePkg/MdeModulePkg.dec
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,12 @@
#
ExceptionPersistenceLib|Include/Library/ExceptionPersistenceLib.h

# MU_CHANGE - Add MemoryBinOverrideLib to MdeModulePkg
## @libraryclass Provides a way to override memory bin locations and sizes
# dynamically.
#
MemoryBinOverrideLib|Include/Library/MemoryBinOverrideLib.h

[Guids]
## MdeModule package token space guid
# Include/Guid/MdeModulePkgTokenSpace.h
Expand Down Expand Up @@ -469,7 +475,7 @@

## Include/Guid/EndofS3Resume.h
gEdkiiEndOfS3ResumeGuid = { 0x96f5296d, 0x05f7, 0x4f3c, {0x84, 0x67, 0xe4, 0x56, 0x89, 0x0e, 0x0c, 0xb5 } }

## MSCHANGE BEGIN
#
# Guids for NVMe Timeout Events
Expand Down Expand Up @@ -656,7 +662,7 @@
# MU_CHANGE
# This protocol provides access to Internal Event services that do not need TPL_APPLICATION
gInternalEventServicesProtocolGuid = {0x7ecd162a, 0xc664, 0x11ec, { 0x9d, 0x64, 0x02, 0x42, 0xac, 0x12, 0x00, 0x02 } }

## This protocol defines the generic memory test interfaces in Dxe phase.
# Include/Protocol/GenericMemoryTest.h
gEfiGenericMemTestProtocolGuid = { 0x309DE7F1, 0x7F5E, 0x4ACE, { 0xB4, 0x9C, 0x53, 0x1B, 0xE5, 0xAA, 0x95, 0xEF }}
Expand Down Expand Up @@ -2382,7 +2388,7 @@
## This dynamic PCD holds the device state bitmap as described in Include/Library/DeviceStateLib.h
# @Prompt Describes device state
gEfiMdeModulePkgTokenSpaceGuid.PcdDeviceStateBitmask|0x00000000|UINT32|0x00030009

[PcdsDynamicEx]
## This dynamic PCD enables the default variable setting.
# Its value is the default store ID value. The default value is zero as Standard default.
Expand Down
2 changes: 2 additions & 0 deletions MdeModulePkg/MdeModulePkg.dsc
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,7 @@
HobLib|MdePkg/Library/DxeCoreHobLib/DxeCoreHobLib.inf
MemoryAllocationLib|MdeModulePkg/Library/DxeCoreMemoryAllocationLib/DxeCoreMemoryAllocationLib.inf
ExtractGuidedSectionLib|MdePkg/Library/DxeExtractGuidedSectionLib/DxeExtractGuidedSectionLib.inf
MemoryBinOverrideLib|MdeModulePkg/Library/MemoryBinOverrideLibNull/MemoryBinOverrideLibNull.inf # MU_CHANGE

[LibraryClasses.common.DXE_DRIVER]
HobLib|MdePkg/Library/DxeHobLib/DxeHobLib.inf
Expand Down Expand Up @@ -531,6 +532,7 @@
MdeModulePkg/Library/BaseVariableFlashInfoLib/BaseVariableFlashInfoLib.inf
MdeModulePkg/Library/PcdDatabaseLoaderLib/Pei/PcdDatabaseLoaderLibPei.inf # MU_CHANGE
MdeModulePkg/Library/PcdDatabaseLoaderLib/Dxe/PcdDatabaseLoaderLibDxe.inf # MU_CHANGE
MdeModulePkg/Library/MemoryBinOverrideLibNull/MemoryBinOverrideLibNull.inf # MU_CHANGE

# MU_CHANGE START
!if $(TOOLCHAIN) != VS2017 and $(TOOLCHAIN) != VS2019
Expand Down
1 change: 1 addition & 0 deletions MdeModulePkg/Test/MdeModulePkgHostTest.dsc
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@
CpuExceptionHandlerLib|MdeModulePkg/Library/CpuExceptionHandlerLibNull/CpuExceptionHandlerLibNull.inf
BaseBinSecurityLib|MdePkg/Library/BaseBinSecurityLibNull/BaseBinSecurityLibNull.inf
UefiRuntimeServicesTableLib|MdePkg/Library/UefiRuntimeServicesTableLib/UefiRuntimeServicesTableLib.inf
MemoryBinOverrideLib|MdeModulePkg/Library/MemoryBinOverrideLibNull/MemoryBinOverrideLibNull.inf # MU_CHANGE

<PcdsFixedAtBuild>
gEfiMdeModulePkgTokenSpaceGuid.PcdLoadModuleAtFixAddressEnable|0
Expand Down

0 comments on commit 76b86ea

Please sign in to comment.