MdeModulePkg: Update MemoryAttributesTable.c to Reduce Global Variable Use

This patch updates MemoryAttributesTable.c to reduce reliance on global
variables and allow some logic to move to a library.

Cc: Jian J Wang <jian.j.wang@intel.com>
Cc: Liming Gao <gaoliming@byosoft.com.cn>
Cc: Dandan Bi <dandan.bi@intel.com>
Signed-off-by: Taylor Beebe <taylor.d.beebe@gmail.com>
Reviewed-by: Liming Gao <gaoliming@byosoft.com.cn>
This commit is contained in:
Taylor Beebe 2023-11-03 08:29:42 -07:00 committed by mergify[bot]
parent 26460342d0
commit 561362368b
1 changed files with 54 additions and 48 deletions

View File

@ -541,8 +541,9 @@ EnforceMemoryMapAttribute (
/** /**
Return the first image record, whose [ImageBase, ImageSize] covered by [Buffer, Length]. Return the first image record, whose [ImageBase, ImageSize] covered by [Buffer, Length].
@param Buffer Start Address @param Buffer Start Address
@param Length Address length @param Length Address length
@param ImageRecordList Image record list
@return first image record covered by [buffer, length] @return first image record covered by [buffer, length]
**/ **/
@ -550,14 +551,12 @@ STATIC
IMAGE_PROPERTIES_RECORD * IMAGE_PROPERTIES_RECORD *
GetImageRecordByAddress ( GetImageRecordByAddress (
IN EFI_PHYSICAL_ADDRESS Buffer, IN EFI_PHYSICAL_ADDRESS Buffer,
IN UINT64 Length IN UINT64 Length,
IN LIST_ENTRY *ImageRecordList
) )
{ {
IMAGE_PROPERTIES_RECORD *ImageRecord; IMAGE_PROPERTIES_RECORD *ImageRecord;
LIST_ENTRY *ImageRecordLink; LIST_ENTRY *ImageRecordLink;
LIST_ENTRY *ImageRecordList;
ImageRecordList = &mImagePropertiesPrivateData.ImageRecordList;
for (ImageRecordLink = ImageRecordList->ForwardLink; for (ImageRecordLink = ImageRecordList->ForwardLink;
ImageRecordLink != ImageRecordList; ImageRecordLink != ImageRecordList;
@ -692,7 +691,8 @@ SetNewRecord (
STATIC STATIC
UINTN UINTN
GetMaxSplitRecordCount ( GetMaxSplitRecordCount (
IN EFI_MEMORY_DESCRIPTOR *OldRecord IN EFI_MEMORY_DESCRIPTOR *OldRecord,
IN LIST_ENTRY *ImageRecordList
) )
{ {
IMAGE_PROPERTIES_RECORD *ImageRecord; IMAGE_PROPERTIES_RECORD *ImageRecord;
@ -705,7 +705,7 @@ GetMaxSplitRecordCount (
PhysicalEnd = OldRecord->PhysicalStart + EfiPagesToSize (OldRecord->NumberOfPages); PhysicalEnd = OldRecord->PhysicalStart + EfiPagesToSize (OldRecord->NumberOfPages);
do { do {
ImageRecord = GetImageRecordByAddress (PhysicalStart, PhysicalEnd - PhysicalStart); ImageRecord = GetImageRecordByAddress (PhysicalStart, PhysicalEnd - PhysicalStart, ImageRecordList);
if (ImageRecord == NULL) { if (ImageRecord == NULL) {
break; break;
} }
@ -725,13 +725,16 @@ GetMaxSplitRecordCount (
Split the memory map to new entries, according to one old entry, Split the memory map to new entries, according to one old entry,
based upon PE code section and data section. based upon PE code section and data section.
@param OldRecord A pointer to one old memory map entry. @param OldRecord A pointer to one old memory map entry.
@param NewRecord A pointer to several new memory map entries. @param NewRecord A pointer to several new memory map entries.
The caller gurantee the buffer size be 1 + The caller gurantee the buffer size be 1 +
(SplitRecordCount * DescriptorSize) calculated (SplitRecordCount * DescriptorSize) calculated
below. below.
@param MaxSplitRecordCount The max number of splitted entries @param MaxSplitRecordCount The max number of splitted entries
@param DescriptorSize Size, in bytes, of an individual EFI_MEMORY_DESCRIPTOR. @param DescriptorSize Size, in bytes, of an individual EFI_MEMORY_DESCRIPTOR.
@param ImageRecordList A list of IMAGE_PROPERTIES_RECORD entries used when searching
for an image record contained by the memory range described in
the existing EFI memory map descriptor OldRecord
@retval 0 no entry is splitted. @retval 0 no entry is splitted.
@return the real number of splitted record. @return the real number of splitted record.
@ -742,7 +745,8 @@ SplitRecord (
IN EFI_MEMORY_DESCRIPTOR *OldRecord, IN EFI_MEMORY_DESCRIPTOR *OldRecord,
IN OUT EFI_MEMORY_DESCRIPTOR *NewRecord, IN OUT EFI_MEMORY_DESCRIPTOR *NewRecord,
IN UINTN MaxSplitRecordCount, IN UINTN MaxSplitRecordCount,
IN UINTN DescriptorSize IN UINTN DescriptorSize,
IN LIST_ENTRY *ImageRecordList
) )
{ {
EFI_MEMORY_DESCRIPTOR TempRecord; EFI_MEMORY_DESCRIPTOR TempRecord;
@ -770,7 +774,7 @@ SplitRecord (
ImageRecord = NULL; ImageRecord = NULL;
do { do {
NewImageRecord = GetImageRecordByAddress (PhysicalStart, PhysicalEnd - PhysicalStart); NewImageRecord = GetImageRecordByAddress (PhysicalStart, PhysicalEnd - PhysicalStart, ImageRecordList);
if (NewImageRecord == NULL) { if (NewImageRecord == NULL) {
// //
// No more image covered by this range, stop // No more image covered by this range, stop
@ -867,23 +871,29 @@ SplitRecord (
| Record Y | | Record Y |
+---------------+ +---------------+
@param MemoryMapSize A pointer to the size, in bytes, of the @param MemoryMapSize A pointer to the size, in bytes, of the
MemoryMap buffer. On input, this is the size of MemoryMap buffer. On input, this is the size of
old MemoryMap before split. The actual buffer old MemoryMap before split. The actual buffer
size of MemoryMap is MemoryMapSize + size of MemoryMap is MemoryMapSize +
(AdditionalRecordCount * DescriptorSize) calculated (AdditionalRecordCount * DescriptorSize) calculated
below. On output, it is the size of new MemoryMap below. On output, it is the size of new MemoryMap
after split. after split.
@param MemoryMap A pointer to the buffer in which firmware places @param MemoryMap A pointer to the buffer in which firmware places
the current memory map. the current memory map.
@param DescriptorSize Size, in bytes, of an individual EFI_MEMORY_DESCRIPTOR. @param DescriptorSize Size, in bytes, of an individual EFI_MEMORY_DESCRIPTOR.
@param ImageRecordList A list of IMAGE_PROPERTIES_RECORD entries used when searching
for an image record contained by the memory range described in
EFI memory map descriptors.
@param NumberOfAdditionalDescriptors The number of unused descriptors at the end of the input MemoryMap.
**/ **/
STATIC STATIC
VOID VOID
SplitTable ( SplitTable (
IN OUT UINTN *MemoryMapSize, IN OUT UINTN *MemoryMapSize,
IN OUT EFI_MEMORY_DESCRIPTOR *MemoryMap, IN OUT EFI_MEMORY_DESCRIPTOR *MemoryMap,
IN UINTN DescriptorSize IN UINTN DescriptorSize,
IN LIST_ENTRY *ImageRecordList,
IN UINTN NumberOfAdditionalDescriptors
) )
{ {
INTN IndexOld; INTN IndexOld;
@ -891,9 +901,6 @@ SplitTable (
UINTN MaxSplitRecordCount; UINTN MaxSplitRecordCount;
UINTN RealSplitRecordCount; UINTN RealSplitRecordCount;
UINTN TotalSplitRecordCount; UINTN TotalSplitRecordCount;
UINTN AdditionalRecordCount;
AdditionalRecordCount = (2 * mImagePropertiesPrivateData.CodeSegmentCountMax + 1) * mImagePropertiesPrivateData.ImageRecordCount;
TotalSplitRecordCount = 0; TotalSplitRecordCount = 0;
// //
@ -903,9 +910,9 @@ SplitTable (
// //
// Let new record point to end of full MemoryMap buffer. // Let new record point to end of full MemoryMap buffer.
// //
IndexNew = ((*MemoryMapSize) / DescriptorSize) - 1 + AdditionalRecordCount; IndexNew = ((*MemoryMapSize) / DescriptorSize) - 1 + NumberOfAdditionalDescriptors;
for ( ; IndexOld >= 0; IndexOld--) { for ( ; IndexOld >= 0; IndexOld--) {
MaxSplitRecordCount = GetMaxSplitRecordCount ((EFI_MEMORY_DESCRIPTOR *)((UINT8 *)MemoryMap + IndexOld * DescriptorSize)); MaxSplitRecordCount = GetMaxSplitRecordCount ((EFI_MEMORY_DESCRIPTOR *)((UINT8 *)MemoryMap + IndexOld * DescriptorSize), ImageRecordList);
// //
// Split this MemoryMap record // Split this MemoryMap record
// //
@ -914,7 +921,8 @@ SplitTable (
(EFI_MEMORY_DESCRIPTOR *)((UINT8 *)MemoryMap + IndexOld * DescriptorSize), (EFI_MEMORY_DESCRIPTOR *)((UINT8 *)MemoryMap + IndexOld * DescriptorSize),
(EFI_MEMORY_DESCRIPTOR *)((UINT8 *)MemoryMap + IndexNew * DescriptorSize), (EFI_MEMORY_DESCRIPTOR *)((UINT8 *)MemoryMap + IndexNew * DescriptorSize),
MaxSplitRecordCount, MaxSplitRecordCount,
DescriptorSize DescriptorSize,
ImageRecordList
); );
// //
// Adjust IndexNew according to real split. // Adjust IndexNew according to real split.
@ -934,7 +942,7 @@ SplitTable (
// //
CopyMem ( CopyMem (
MemoryMap, MemoryMap,
(UINT8 *)MemoryMap + (AdditionalRecordCount - TotalSplitRecordCount) * DescriptorSize, (UINT8 *)MemoryMap + (NumberOfAdditionalDescriptors - TotalSplitRecordCount) * DescriptorSize,
(*MemoryMapSize) + TotalSplitRecordCount * DescriptorSize (*MemoryMapSize) + TotalSplitRecordCount * DescriptorSize
); );
@ -1035,7 +1043,7 @@ CoreGetMemoryMapWithSeparatedImageSection (
// //
// Split PE code/data // Split PE code/data
// //
SplitTable (MemoryMapSize, MemoryMap, *DescriptorSize); SplitTable (MemoryMapSize, MemoryMap, *DescriptorSize, &mImagePropertiesPrivateData.ImageRecordList, AdditionalRecordCount);
} }
} }
@ -1233,11 +1241,13 @@ SwapImageRecord (
/** /**
Sort image record based upon the ImageBase from low to high. Sort image record based upon the ImageBase from low to high.
@param ImageRecordList Image record list to be sorted
**/ **/
STATIC STATIC
VOID VOID
SortImageRecord ( SortImageRecord (
VOID IN LIST_ENTRY *ImageRecordList
) )
{ {
IMAGE_PROPERTIES_RECORD *ImageRecord; IMAGE_PROPERTIES_RECORD *ImageRecord;
@ -1245,9 +1255,6 @@ SortImageRecord (
LIST_ENTRY *ImageRecordLink; LIST_ENTRY *ImageRecordLink;
LIST_ENTRY *NextImageRecordLink; LIST_ENTRY *NextImageRecordLink;
LIST_ENTRY *ImageRecordEndLink; LIST_ENTRY *ImageRecordEndLink;
LIST_ENTRY *ImageRecordList;
ImageRecordList = &mImagePropertiesPrivateData.ImageRecordList;
ImageRecordLink = ImageRecordList->ForwardLink; ImageRecordLink = ImageRecordList->ForwardLink;
NextImageRecordLink = ImageRecordLink->ForwardLink; NextImageRecordLink = ImageRecordLink->ForwardLink;
@ -1456,7 +1463,7 @@ InsertImageRecord (
mImagePropertiesPrivateData.CodeSegmentCountMax = ImageRecord->CodeSegmentCount; mImagePropertiesPrivateData.CodeSegmentCountMax = ImageRecord->CodeSegmentCount;
} }
SortImageRecord (); SortImageRecord (&mImagePropertiesPrivateData.ImageRecordList);
Finish: Finish:
return; return;
@ -1465,8 +1472,9 @@ Finish:
/** /**
Find image record according to image base and size. Find image record according to image base and size.
@param ImageBase Base of PE image @param ImageBase Base of PE image
@param ImageSize Size of PE image @param ImageSize Size of PE image
@param ImageRecordList Image record list to be searched
@return image record @return image record
**/ **/
@ -1474,14 +1482,12 @@ STATIC
IMAGE_PROPERTIES_RECORD * IMAGE_PROPERTIES_RECORD *
FindImageRecord ( FindImageRecord (
IN EFI_PHYSICAL_ADDRESS ImageBase, IN EFI_PHYSICAL_ADDRESS ImageBase,
IN UINT64 ImageSize IN UINT64 ImageSize,
IN LIST_ENTRY *ImageRecordList
) )
{ {
IMAGE_PROPERTIES_RECORD *ImageRecord; IMAGE_PROPERTIES_RECORD *ImageRecord;
LIST_ENTRY *ImageRecordLink; LIST_ENTRY *ImageRecordLink;
LIST_ENTRY *ImageRecordList;
ImageRecordList = &mImagePropertiesPrivateData.ImageRecordList;
for (ImageRecordLink = ImageRecordList->ForwardLink; for (ImageRecordLink = ImageRecordList->ForwardLink;
ImageRecordLink != ImageRecordList; ImageRecordLink != ImageRecordList;
@ -1526,7 +1532,7 @@ RemoveImageRecord (
return; return;
} }
ImageRecord = FindImageRecord ((EFI_PHYSICAL_ADDRESS)(UINTN)RuntimeImage->ImageBase, RuntimeImage->ImageSize); ImageRecord = FindImageRecord ((EFI_PHYSICAL_ADDRESS)(UINTN)RuntimeImage->ImageBase, RuntimeImage->ImageSize, &mImagePropertiesPrivateData.ImageRecordList);
if (ImageRecord == NULL) { if (ImageRecord == NULL) {
DEBUG ((DEBUG_ERROR, "!!!!!!!! ImageRecord not found !!!!!!!!\n")); DEBUG ((DEBUG_ERROR, "!!!!!!!! ImageRecord not found !!!!!!!!\n"));
return; return;