Skip to content

Commit

Permalink
Clear Access Attributes Before Page Free
Browse files Browse the repository at this point in the history
  • Loading branch information
TaylorBeebe authored and kenlautner committed May 4, 2023
1 parent a521e4f commit 0df0cca
Showing 1 changed file with 18 additions and 2 deletions.
20 changes: 18 additions & 2 deletions MdeModulePkg/Core/Dxe/Mem/Page.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ SPDX-License-Identifier: BSD-2-Clause-Patent
#include "Imem.h"
#include "HeapGuard.h"
#include <Pi/PrePiDxeCis.h>
#include "MemoryProtectionSupport.h" // MU_CHANGE

//
// Entry for tracking the memory regions for each memory type to coalesce similar memory types
Expand Down Expand Up @@ -1101,7 +1102,7 @@ CoreFindFreePagesI (
DescEnd = MaxAddress;
}

DescEnd = ((DescEnd + 1) & (~((UINT64)Alignment - 1))) - 1;
DescEnd = ((DescEnd + 1) & (~(Alignment - 1))) - 1;

// Skip if DescEnd is less than DescStart after alignment clipping
if (DescEnd < DescStart) {
Expand Down Expand Up @@ -1536,6 +1537,22 @@ 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;

if (DebugClearMemoryEnabled () && (MemoryAttributeProtocol != NULL)) {
Status = MemoryAttributeProtocol->GetMemoryAttributes (MemoryAttributeProtocol, Memory, EFI_PAGES_TO_SIZE (NumberOfPages), &Attributes);

if ((Attributes & EFI_MEMORY_RO) || (Attributes & EFI_MEMORY_RP) || (Status == EFI_NO_MAPPING)) {
Status = ClearAccessAttributesFromMemoryRange (Memory, EFI_PAGES_TO_SIZE (NumberOfPages));

if (EFI_ERROR (Status) && (Status != EFI_NOT_READY)) {
DEBUG ((DEBUG_WARN, "%a - Unable to clear attributes from memory at base: 0x%llx\n", __FUNCTION__, Memory));
}
}
}

// MU_CHANGE End

//
// Free the range
Expand Down Expand Up @@ -1960,7 +1977,6 @@ CoreGetMemoryMap (
MemoryMap->Attribute = MergeGcdMapEntry.Attributes | EFI_MEMORY_NV |
(MergeGcdMapEntry.Capabilities & (EFI_CACHE_ATTRIBUTE_MASK | EFI_MEMORY_ATTRIBUTE_MASK));
MemoryMap->Type = EfiPersistentMemory;

//
// Check to see if the new Memory Map Descriptor can be merged with an
// existing descriptor if they are adjacent and have the same attributes
Expand Down

0 comments on commit 0df0cca

Please sign in to comment.