Skip to content

Commit

Permalink
TCBZ 4492: Update SplitTable() Logic to Correctly Break Up Memory Map…
Browse files Browse the repository at this point in the history
… Descriptors (#476)

## Description

Fixes #475

Fix the SplitTable() logic to correctly break up the memory map and fix
errors in the Memory Attributes Table.

- [x] Impacts functionality?
- **Functionality** - Does the change ultimately impact how firmware
functions?
- Examples: Add a new library, publish a new PPI, update an algorithm,
...
- [ ] Impacts security?
- **Security** - Does the change have a direct security impact on an
application,
    flow, or firmware?
  - Examples: Crypto algorithm change, buffer overflow fix, parameter
    validation improvement, ...
- [ ] Breaking change?
- **Breaking change** - Will anyone consuming this change experience a
break
    in build or boot behavior?
- Examples: Add a new library class, move a module to a different repo,
call
    a function in a new library class in a pre-existing module, ...
- [ ] Includes tests?
  - **Tests** - Does the change include any explicit test code?
  - Examples: Unit tests, integration tests, robot tests, ...
- [ ] Includes documentation?
- **Documentation** - Does the change contain explicit documentation
additions
    outside direct code modifications (and comments)?
- Examples: Update readme file, add feature readme file, link to
documentation
    on an a separate Web page, ...

## How This Was Tested

Tested on Q35

## Integration Instructions

N/A
  • Loading branch information
TaylorBeebe authored Jun 28, 2023
1 parent 72aab6e commit eb15af8
Showing 1 changed file with 19 additions and 18 deletions.
37 changes: 19 additions & 18 deletions MdeModulePkg/Core/Dxe/Misc/MemoryAttributesTable.c
Original file line number Diff line number Diff line change
Expand Up @@ -899,24 +899,27 @@ SplitTable (
IN UINTN DescriptorSize
)
{
// MU_CHANGE START: TCBZ 4492 - Fix SplitTable() Logic
INTN IndexOld;
INTN IndexNew;
INTN IndexNewStarting;
UINTN MaxSplitRecordCount;
UINTN RealSplitRecordCount;
UINTN TotalSplitRecordCount;
UINTN AdditionalRecordCount;
UINTN TotalSkippedRecords;

AdditionalRecordCount = (2 * mImagePropertiesPrivateData.CodeSegmentCountMax + 1) * mImagePropertiesPrivateData.ImageRecordCount;

TotalSplitRecordCount = 0;
TotalSkippedRecords = 0;
//
// Let old record point to end of valid MemoryMap buffer.
//
IndexOld = ((*MemoryMapSize) / DescriptorSize) - 1;
//
// Let new record point to end of full MemoryMap buffer.
//
IndexNew = ((*MemoryMapSize) / DescriptorSize) - 1 + AdditionalRecordCount;
IndexNew = ((*MemoryMapSize) / DescriptorSize) - 1 + AdditionalRecordCount;
IndexNewStarting = IndexNew;
for ( ; IndexOld >= 0; IndexOld--) {
MaxSplitRecordCount = GetMaxSplitRecordCount ((EFI_MEMORY_DESCRIPTOR *)((UINT8 *)MemoryMap + IndexOld * DescriptorSize));
//
Expand All @@ -929,16 +932,14 @@ SplitTable (
MaxSplitRecordCount,
DescriptorSize
);
//
// Adjust IndexNew according to real split.
//
CopyMem (
((UINT8 *)MemoryMap + (IndexNew + MaxSplitRecordCount - RealSplitRecordCount) * DescriptorSize),
((UINT8 *)MemoryMap + IndexNew * DescriptorSize),
RealSplitRecordCount * DescriptorSize
);
IndexNew = IndexNew + MaxSplitRecordCount - RealSplitRecordCount;
TotalSplitRecordCount += RealSplitRecordCount;

// If we didn't utilize all the extra allocated descriptor slots, set the physical address of the unused slots
// to MAX_ADDRESS so they are moved to the bottom of the list when sorting.
for ( ; RealSplitRecordCount < MaxSplitRecordCount; RealSplitRecordCount++) {
((EFI_MEMORY_DESCRIPTOR *)((UINT8 *)MemoryMap + ((IndexNew + RealSplitRecordCount + 1) * DescriptorSize)))->PhysicalStart = MAX_ADDRESS;
TotalSkippedRecords++;
}

IndexNew--;
}

Expand All @@ -947,17 +948,17 @@ SplitTable (
//
CopyMem (
MemoryMap,
(UINT8 *)MemoryMap + (AdditionalRecordCount - TotalSplitRecordCount) * DescriptorSize,
(*MemoryMapSize) + TotalSplitRecordCount * DescriptorSize
(UINT8 *)MemoryMap + ((IndexNew + 1) * DescriptorSize),
(IndexNewStarting - IndexNew) * DescriptorSize
);

*MemoryMapSize = (*MemoryMapSize) + DescriptorSize * TotalSplitRecordCount;

//
// Sort from low to high (Just in case)
//
SortMemoryMap (MemoryMap, *MemoryMapSize, DescriptorSize);
SortMemoryMap (MemoryMap, (IndexNewStarting - IndexNew) * DescriptorSize, DescriptorSize);

*MemoryMapSize = (IndexNewStarting - IndexNew - TotalSkippedRecords) * DescriptorSize;
// MU_CHANGE END
//
// Set RuntimeData to XP
//
Expand Down

0 comments on commit eb15af8

Please sign in to comment.