Skip to content

Commit df3b847

Browse files
cfernaldProjectMuBot
authored andcommitted
Create memory bin override library to allow for more extensive platform 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 ```
1 parent 96e8877 commit df3b847

File tree

7 files changed

+165
-1
lines changed

7 files changed

+165
-1
lines changed

MdeModulePkg/Core/Dxe/DxeMain.inf

+1
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@
9999
ImagePropertiesRecordLib
100100
DxeMemoryProtectionHobLib ## MU_CHANGE
101101
SafeIntLib ## MU_CHANGE
102+
MemoryBinOverrideLib ## MU_CHANGE
102103

103104
[Guids]
104105
gEfiEventMemoryMapChangeGuid ## PRODUCES ## Event

MdeModulePkg/Core/Dxe/Mem/Page.c

+25-1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ SPDX-License-Identifier: BSD-2-Clause-Patent
1010
#include "Imem.h"
1111
#include "HeapGuard.h"
1212
#include <Pi/PiDxeCis.h>
13+
#include <Library/MemoryBinOverrideLib.h> // MU_CHANGE
1314

1415
//
1516
// Entry for tracking the memory regions for each memory type to coalesce similar memory types
@@ -681,6 +682,7 @@ CoreAddMemoryDescriptor (
681682
EFI_STATUS Status;
682683
UINTN Index;
683684
UINTN FreeIndex;
685+
EFI_ALLOCATE_TYPE AllocationType; // MU_CHANGE
684686

685687
if ((Start & EFI_PAGE_MASK) != 0) {
686688
return;
@@ -730,16 +732,29 @@ CoreAddMemoryDescriptor (
730732
}
731733

732734
if (gMemoryTypeInformation[Index].NumberOfPages != 0) {
735+
// MU_CHANGE START Allow overriding of bin locations.
736+
AllocationType = AllocateAnyPages;
737+
GetMemoryBinOverride (
738+
Type,
739+
&mMemoryTypeStatistics[Type].BaseAddress,
740+
&gMemoryTypeInformation[Index].NumberOfPages,
741+
&AllocationType
742+
);
743+
// MU_CHANGE END
744+
733745
//
734746
// Allocate pages for the current memory type from the top of available memory
735747
//
748+
736749
Status = CoreAllocatePages (
737-
AllocateAnyPages,
750+
AllocationType, // MU_CHANGE
738751
Type,
739752
gMemoryTypeInformation[Index].NumberOfPages,
740753
&mMemoryTypeStatistics[Type].BaseAddress
741754
);
742755
if (EFI_ERROR (Status)) {
756+
mMemoryTypeStatistics[Type].BaseAddress = 0; // MU_CHANGE
757+
743758
//
744759
// If an error occurs allocating the pages for the current memory type, then
745760
// free all the pages allocates for the previous memory types and return. This
@@ -806,6 +821,15 @@ CoreAddMemoryDescriptor (
806821
mMemoryTypeStatistics[Type].NumberOfPages = gMemoryTypeInformation[Index].NumberOfPages;
807822
gMemoryTypeInformation[Index].NumberOfPages = 0;
808823
}
824+
825+
// MU_CHANGE START
826+
ReportMemoryBinLocation (
827+
Type,
828+
mMemoryTypeStatistics[Type].BaseAddress,
829+
mMemoryTypeStatistics[Type].NumberOfPages
830+
);
831+
832+
// MU_CHANGE END
809833
}
810834

811835
//
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/** @file
2+
Header definitions for the memory bin override library. This library allows
3+
a platform to override the location or size of the memory type bins.
4+
5+
Copyright (c) Microsoft Corporation
6+
SPDX-License-Identifier: BSD-2-Clause-Patent
7+
8+
**/
9+
10+
#ifndef MEMORY_BIN_OVERRIDE_LIB_H__
11+
#define MEMORY_BIN_OVERRIDE_LIB_H__
12+
13+
/**
14+
Reports a runtime memory bin location to the override library.
15+
16+
@param[in] Type The memory type for the reported bin.
17+
@param[in] BaseAddress The base physical address of the reported bin.
18+
@param[in] NumberOfPages The number of pages in the reported bin.
19+
**/
20+
VOID
21+
EFIAPI
22+
ReportMemoryBinLocation (
23+
IN EFI_MEMORY_TYPE Type,
24+
IN EFI_PHYSICAL_ADDRESS BaseAddress,
25+
IN UINT64 NumberOfPages
26+
);
27+
28+
/**
29+
Checks if the library needs to override the given memory bin allocation type,
30+
location, and size. If this function encounters internal errors, the
31+
parameters should remain unchanged.
32+
33+
@param[in] Type The memory type of the bin.
34+
@param[out] BaseAddress The base address of the bin override on return.
35+
@param[out] NumberOfPages The number of pages of the bin override on return.
36+
@param[out] AllocationType The allocation type for the bin, AllocateAddress
37+
if an override was provided.
38+
**/
39+
VOID
40+
EFIAPI
41+
GetMemoryBinOverride (
42+
IN EFI_MEMORY_TYPE Type,
43+
OUT EFI_PHYSICAL_ADDRESS *BaseAddress,
44+
OUT UINT32 *NumberOfPages,
45+
OUT EFI_ALLOCATE_TYPE *AllocationType
46+
);
47+
48+
#endif
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/**
2+
NULL implementation of the memory bin override lib.
3+
4+
Copyright (c) Microsoft Corporation. All rights reserved.
5+
SPDX-License-Identifier: BSD-2-Clause-Patent
6+
7+
**/
8+
9+
#include <Uefi.h>
10+
#include <Library/BaseLib.h>
11+
#include <Library/MemoryBinOverrideLib.h>
12+
13+
/**
14+
Reports a runtime memory bin location to the override library.
15+
16+
@param[in] Type The memory type for the reported bin.
17+
@param[in] BaseAddress The base physical address of the reported bin.
18+
@param[in] NumberOfPages The number of pages in the reported bin.
19+
**/
20+
VOID
21+
EFIAPI
22+
ReportMemoryBinLocation (
23+
IN EFI_MEMORY_TYPE Type,
24+
IN EFI_PHYSICAL_ADDRESS BaseAddress,
25+
IN UINT64 NumberOfPages
26+
)
27+
{
28+
return;
29+
}
30+
31+
/**
32+
Checks if the library needs to override the given memory bin allocation type,
33+
location, and size. If this function encounters internal errors, the
34+
parameters should remain unchanged.
35+
36+
@param[in] Type The memory type of the bin.
37+
@param[out] BaseAddress The base address of the bin override on return.
38+
@param[out] NumberOfPages The number of pages of the bin override on return.
39+
@param[out] AllocationType The allocation type for the bin, AllocateAddress
40+
if an override was provided.
41+
**/
42+
VOID
43+
EFIAPI
44+
GetMemoryBinOverride (
45+
IN EFI_MEMORY_TYPE Type,
46+
OUT EFI_PHYSICAL_ADDRESS *BaseAddress,
47+
OUT UINT32 *NumberOfPages,
48+
OUT EFI_ALLOCATE_TYPE *AllocationType
49+
)
50+
{
51+
return;
52+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
## @file
2+
# NULL implementation of the memory bin override lib.
3+
#
4+
# Copyright (c) Microsoft Corporation. All rights reserved.
5+
# SPDX-License-Identifier: BSD-2-Clause-Patent
6+
#
7+
##
8+
9+
[Defines]
10+
INF_VERSION = 1.27
11+
BASE_NAME = MemoryOverrideLibNull
12+
FILE_GUID = 368687CE-3189-4C26-A6EB-615B64CAA911
13+
MODULE_TYPE = DXE_CORE
14+
VERSION_STRING = 1.0
15+
LIBRARY_CLASS = MemoryBinOverrideLib
16+
17+
#
18+
# The following information is for reference only and not required by the build tools.
19+
#
20+
# VALID_ARCHITECTURES = IA32 X64 AARCH64
21+
#
22+
23+
[Sources]
24+
MemoryBinOverrideLibNull.c
25+
26+
[Packages]
27+
MdePkg/MdePkg.dec
28+
MdeModulePkg/MdeModulePkg.dec
29+
30+
[LibraryClasses]
31+
BaseLib

MdeModulePkg/MdeModulePkg.dec

+6
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,12 @@
232232
#
233233
MmMemoryProtectionHobLib|Include/Library/MmMemoryProtectionHobLib.h
234234

235+
# MU_CHANGE - Add MemoryBinOverrideLib to MdeModulePkg
236+
## @libraryclass Provides a way to override memory bin locations and sizes
237+
# dynamically.
238+
#
239+
MemoryBinOverrideLib|Include/Library/MemoryBinOverrideLib.h
240+
235241
[Guids]
236242
## MdeModule package token space guid
237243
# Include/Guid/MdeModulePkgTokenSpace.h

MdeModulePkg/MdeModulePkg.dsc

+2
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,7 @@
152152
HobLib|MdePkg/Library/DxeCoreHobLib/DxeCoreHobLib.inf
153153
MemoryAllocationLib|MdeModulePkg/Library/DxeCoreMemoryAllocationLib/DxeCoreMemoryAllocationLib.inf
154154
ExtractGuidedSectionLib|MdePkg/Library/DxeExtractGuidedSectionLib/DxeExtractGuidedSectionLib.inf
155+
MemoryBinOverrideLib|MdeModulePkg/Library/MemoryBinOverrideLibNull/MemoryBinOverrideLibNull.inf # MU_CHANGE
155156

156157
[LibraryClasses.common.DXE_DRIVER]
157158
HobLib|MdePkg/Library/DxeHobLib/DxeHobLib.inf
@@ -514,6 +515,7 @@
514515
MdeModulePkg/Library/BaseVariableFlashInfoLib/BaseVariableFlashInfoLib.inf
515516
MdeModulePkg/Library/PcdDatabaseLoaderLib/Pei/PcdDatabaseLoaderLibPei.inf # MU_CHANGE
516517
MdeModulePkg/Library/PcdDatabaseLoaderLib/Dxe/PcdDatabaseLoaderLibDxe.inf # MU_CHANGE
518+
MdeModulePkg/Library/MemoryBinOverrideLibNull/MemoryBinOverrideLibNull.inf # MU_CHANGE
517519

518520
# MU_CHANGE START
519521
!if $(TOOLCHAIN) != VS2017 and $(TOOLCHAIN) != VS2019

0 commit comments

Comments
 (0)