2016-01-29 09:52:14 +01:00
|
|
|
/** @file
|
|
|
|
UEFI MemoryAttributesTable support
|
|
|
|
|
2018-06-27 15:08:52 +02:00
|
|
|
Copyright (c) 2016 - 2018, Intel Corporation. All rights reserved.<BR>
|
2019-04-04 01:05:13 +02:00
|
|
|
SPDX-License-Identifier: BSD-2-Clause-Patent
|
2016-01-29 09:52:14 +01:00
|
|
|
|
|
|
|
**/
|
|
|
|
|
|
|
|
#include <PiDxe.h>
|
|
|
|
#include <Library/BaseLib.h>
|
|
|
|
#include <Library/BaseMemoryLib.h>
|
|
|
|
#include <Library/MemoryAllocationLib.h>
|
|
|
|
#include <Library/UefiBootServicesTableLib.h>
|
|
|
|
#include <Library/DxeServicesTableLib.h>
|
|
|
|
#include <Library/DebugLib.h>
|
|
|
|
#include <Library/UefiLib.h>
|
2023-11-03 16:29:43 +01:00
|
|
|
#include <Library/ImagePropertiesRecordLib.h>
|
2016-01-29 09:52:14 +01:00
|
|
|
|
|
|
|
#include <Guid/EventGroup.h>
|
|
|
|
|
|
|
|
#include <Guid/MemoryAttributesTable.h>
|
|
|
|
|
|
|
|
#include "DxeMain.h"
|
2020-04-07 09:48:30 +02:00
|
|
|
#include "HeapGuard.h"
|
2016-01-29 09:52:14 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
This function for GetMemoryMap() with properties table capability.
|
|
|
|
|
|
|
|
It calls original GetMemoryMap() to get the original memory map information. Then
|
|
|
|
plus the additional memory map entries for PE Code/Data seperation.
|
|
|
|
|
|
|
|
@param MemoryMapSize A pointer to the size, in bytes, of the
|
|
|
|
MemoryMap buffer. On input, this is the size of
|
|
|
|
the buffer allocated by the caller. On output,
|
|
|
|
it is the size of the buffer returned by the
|
|
|
|
firmware if the buffer was large enough, or the
|
|
|
|
size of the buffer needed to contain the map if
|
|
|
|
the buffer was too small.
|
|
|
|
@param MemoryMap A pointer to the buffer in which firmware places
|
|
|
|
the current memory map.
|
|
|
|
@param MapKey A pointer to the location in which firmware
|
|
|
|
returns the key for the current memory map.
|
|
|
|
@param DescriptorSize A pointer to the location in which firmware
|
|
|
|
returns the size, in bytes, of an individual
|
|
|
|
EFI_MEMORY_DESCRIPTOR.
|
|
|
|
@param DescriptorVersion A pointer to the location in which firmware
|
|
|
|
returns the version number associated with the
|
|
|
|
EFI_MEMORY_DESCRIPTOR.
|
|
|
|
|
|
|
|
@retval EFI_SUCCESS The memory map was returned in the MemoryMap
|
|
|
|
buffer.
|
|
|
|
@retval EFI_BUFFER_TOO_SMALL The MemoryMap buffer was too small. The current
|
|
|
|
buffer size needed to hold the memory map is
|
|
|
|
returned in MemoryMapSize.
|
|
|
|
@retval EFI_INVALID_PARAMETER One of the parameters has an invalid value.
|
|
|
|
|
|
|
|
**/
|
|
|
|
EFI_STATUS
|
|
|
|
EFIAPI
|
2016-05-16 02:53:37 +02:00
|
|
|
CoreGetMemoryMapWithSeparatedImageSection (
|
2016-01-29 09:52:14 +01:00
|
|
|
IN OUT UINTN *MemoryMapSize,
|
|
|
|
IN OUT EFI_MEMORY_DESCRIPTOR *MemoryMap,
|
|
|
|
OUT UINTN *MapKey,
|
|
|
|
OUT UINTN *DescriptorSize,
|
|
|
|
OUT UINT32 *DescriptorVersion
|
|
|
|
);
|
|
|
|
|
2020-04-07 09:48:30 +02:00
|
|
|
#define PREVIOUS_MEMORY_DESCRIPTOR(MemoryDescriptor, Size) \
|
|
|
|
((EFI_MEMORY_DESCRIPTOR *)((UINT8 *)(MemoryDescriptor) - (Size)))
|
|
|
|
|
|
|
|
#define IMAGE_PROPERTIES_PRIVATE_DATA_SIGNATURE SIGNATURE_32 ('I','P','P','D')
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
UINT32 Signature;
|
|
|
|
UINTN ImageRecordCount;
|
|
|
|
UINTN CodeSegmentCountMax;
|
|
|
|
LIST_ENTRY ImageRecordList;
|
|
|
|
} IMAGE_PROPERTIES_PRIVATE_DATA;
|
|
|
|
|
|
|
|
STATIC IMAGE_PROPERTIES_PRIVATE_DATA mImagePropertiesPrivateData = {
|
|
|
|
IMAGE_PROPERTIES_PRIVATE_DATA_SIGNATURE,
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
INITIALIZE_LIST_HEAD_VARIABLE (mImagePropertiesPrivateData.ImageRecordList)
|
|
|
|
};
|
|
|
|
|
|
|
|
STATIC EFI_LOCK mMemoryAttributesTableLock = EFI_INITIALIZE_LOCK_VARIABLE (TPL_NOTIFY);
|
|
|
|
|
2020-04-07 09:48:19 +02:00
|
|
|
BOOLEAN mMemoryAttributesTableEnable = TRUE;
|
2020-04-07 09:48:30 +02:00
|
|
|
BOOLEAN mMemoryAttributesTableEndOfDxe = FALSE;
|
2016-04-20 11:27:40 +02:00
|
|
|
EFI_MEMORY_ATTRIBUTES_TABLE *mMemoryAttributesTable = NULL;
|
|
|
|
BOOLEAN mMemoryAttributesTableReadyToBoot = FALSE;
|
2023-02-02 19:03:34 +01:00
|
|
|
BOOLEAN gMemoryAttributesTableForwardCfi = TRUE;
|
2016-01-29 09:52:14 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
Install MemoryAttributesTable.
|
|
|
|
|
|
|
|
**/
|
|
|
|
VOID
|
|
|
|
InstallMemoryAttributesTable (
|
2016-04-20 11:27:40 +02:00
|
|
|
VOID
|
2016-01-29 09:52:14 +01:00
|
|
|
)
|
|
|
|
{
|
|
|
|
UINTN MemoryMapSize;
|
|
|
|
EFI_MEMORY_DESCRIPTOR *MemoryMap;
|
|
|
|
EFI_MEMORY_DESCRIPTOR *MemoryMapStart;
|
|
|
|
UINTN MapKey;
|
|
|
|
UINTN DescriptorSize;
|
|
|
|
UINT32 DescriptorVersion;
|
|
|
|
UINTN Index;
|
|
|
|
EFI_STATUS Status;
|
|
|
|
UINT32 RuntimeEntryCount;
|
|
|
|
EFI_MEMORY_ATTRIBUTES_TABLE *MemoryAttributesTable;
|
|
|
|
EFI_MEMORY_DESCRIPTOR *MemoryAttributesEntry;
|
|
|
|
|
2016-04-20 11:27:40 +02:00
|
|
|
if (gMemoryMapTerminated) {
|
|
|
|
//
|
|
|
|
// Directly return after MemoryMap terminated.
|
|
|
|
//
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-04-07 09:48:19 +02:00
|
|
|
if (!mMemoryAttributesTableEnable) {
|
|
|
|
DEBUG ((DEBUG_VERBOSE, "Cannot install Memory Attributes Table "));
|
2021-11-17 04:21:29 +01:00
|
|
|
DEBUG ((DEBUG_VERBOSE, "because Runtime Driver Section Alignment is not %dK.\n", RUNTIME_PAGE_ALLOCATION_GRANULARITY >> 10));
|
2016-01-29 09:52:14 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-04-20 11:27:40 +02:00
|
|
|
if (mMemoryAttributesTable == NULL) {
|
|
|
|
//
|
|
|
|
// InstallConfigurationTable here to occupy one entry for MemoryAttributesTable
|
|
|
|
// before GetMemoryMap below, as InstallConfigurationTable may allocate runtime
|
|
|
|
// memory for the new entry.
|
|
|
|
//
|
|
|
|
Status = gBS->InstallConfigurationTable (&gEfiMemoryAttributesTableGuid, (VOID *)(UINTN)MAX_ADDRESS);
|
|
|
|
ASSERT_EFI_ERROR (Status);
|
|
|
|
}
|
|
|
|
|
2016-01-29 09:52:14 +01:00
|
|
|
MemoryMapSize = 0;
|
|
|
|
MemoryMap = NULL;
|
2016-05-16 02:53:37 +02:00
|
|
|
Status = CoreGetMemoryMapWithSeparatedImageSection (
|
2016-01-29 09:52:14 +01:00
|
|
|
&MemoryMapSize,
|
|
|
|
MemoryMap,
|
|
|
|
&MapKey,
|
|
|
|
&DescriptorSize,
|
|
|
|
&DescriptorVersion
|
|
|
|
);
|
|
|
|
ASSERT (Status == EFI_BUFFER_TOO_SMALL);
|
|
|
|
|
|
|
|
do {
|
|
|
|
MemoryMap = AllocatePool (MemoryMapSize);
|
|
|
|
ASSERT (MemoryMap != NULL);
|
|
|
|
|
2016-05-16 02:53:37 +02:00
|
|
|
Status = CoreGetMemoryMapWithSeparatedImageSection (
|
2016-01-29 09:52:14 +01:00
|
|
|
&MemoryMapSize,
|
|
|
|
MemoryMap,
|
|
|
|
&MapKey,
|
|
|
|
&DescriptorSize,
|
|
|
|
&DescriptorVersion
|
|
|
|
);
|
|
|
|
if (EFI_ERROR (Status)) {
|
|
|
|
FreePool (MemoryMap);
|
|
|
|
}
|
|
|
|
} while (Status == EFI_BUFFER_TOO_SMALL);
|
|
|
|
|
|
|
|
MemoryMapStart = MemoryMap;
|
|
|
|
RuntimeEntryCount = 0;
|
|
|
|
for (Index = 0; Index < MemoryMapSize/DescriptorSize; Index++) {
|
|
|
|
switch (MemoryMap->Type) {
|
|
|
|
case EfiRuntimeServicesCode:
|
|
|
|
case EfiRuntimeServicesData:
|
|
|
|
RuntimeEntryCount++;
|
|
|
|
break;
|
|
|
|
}
|
2021-12-05 23:54:02 +01:00
|
|
|
|
2016-01-29 09:52:14 +01:00
|
|
|
MemoryMap = NEXT_MEMORY_DESCRIPTOR (MemoryMap, DescriptorSize);
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// Allocate MemoryAttributesTable
|
|
|
|
//
|
|
|
|
MemoryAttributesTable = AllocatePool (sizeof (EFI_MEMORY_ATTRIBUTES_TABLE) + DescriptorSize * RuntimeEntryCount);
|
|
|
|
ASSERT (MemoryAttributesTable != NULL);
|
|
|
|
MemoryAttributesTable->Version = EFI_MEMORY_ATTRIBUTES_TABLE_VERSION;
|
|
|
|
MemoryAttributesTable->NumberOfEntries = RuntimeEntryCount;
|
|
|
|
MemoryAttributesTable->DescriptorSize = (UINT32)DescriptorSize;
|
2023-02-02 19:03:34 +01:00
|
|
|
if (gMemoryAttributesTableForwardCfi) {
|
|
|
|
MemoryAttributesTable->Flags = EFI_MEMORY_ATTRIBUTES_FLAGS_RT_FORWARD_CONTROL_FLOW_GUARD;
|
|
|
|
} else {
|
|
|
|
MemoryAttributesTable->Flags = 0;
|
|
|
|
}
|
|
|
|
|
2021-11-17 04:21:29 +01:00
|
|
|
DEBUG ((DEBUG_VERBOSE, "MemoryAttributesTable:\n"));
|
|
|
|
DEBUG ((DEBUG_VERBOSE, " Version - 0x%08x\n", MemoryAttributesTable->Version));
|
|
|
|
DEBUG ((DEBUG_VERBOSE, " NumberOfEntries - 0x%08x\n", MemoryAttributesTable->NumberOfEntries));
|
|
|
|
DEBUG ((DEBUG_VERBOSE, " DescriptorSize - 0x%08x\n", MemoryAttributesTable->DescriptorSize));
|
2016-01-29 09:52:14 +01:00
|
|
|
MemoryAttributesEntry = (EFI_MEMORY_DESCRIPTOR *)(MemoryAttributesTable + 1);
|
|
|
|
MemoryMap = MemoryMapStart;
|
|
|
|
for (Index = 0; Index < MemoryMapSize/DescriptorSize; Index++) {
|
|
|
|
switch (MemoryMap->Type) {
|
|
|
|
case EfiRuntimeServicesCode:
|
|
|
|
case EfiRuntimeServicesData:
|
|
|
|
CopyMem (MemoryAttributesEntry, MemoryMap, DescriptorSize);
|
|
|
|
MemoryAttributesEntry->Attribute &= (EFI_MEMORY_RO|EFI_MEMORY_XP|EFI_MEMORY_RUNTIME);
|
2021-11-17 04:21:29 +01:00
|
|
|
DEBUG ((DEBUG_VERBOSE, "Entry (0x%x)\n", MemoryAttributesEntry));
|
|
|
|
DEBUG ((DEBUG_VERBOSE, " Type - 0x%x\n", MemoryAttributesEntry->Type));
|
|
|
|
DEBUG ((DEBUG_VERBOSE, " PhysicalStart - 0x%016lx\n", MemoryAttributesEntry->PhysicalStart));
|
|
|
|
DEBUG ((DEBUG_VERBOSE, " VirtualStart - 0x%016lx\n", MemoryAttributesEntry->VirtualStart));
|
|
|
|
DEBUG ((DEBUG_VERBOSE, " NumberOfPages - 0x%016lx\n", MemoryAttributesEntry->NumberOfPages));
|
|
|
|
DEBUG ((DEBUG_VERBOSE, " Attribute - 0x%016lx\n", MemoryAttributesEntry->Attribute));
|
2016-01-29 09:52:14 +01:00
|
|
|
MemoryAttributesEntry = NEXT_MEMORY_DESCRIPTOR (MemoryAttributesEntry, DescriptorSize);
|
|
|
|
break;
|
|
|
|
}
|
2021-12-05 23:54:02 +01:00
|
|
|
|
2016-01-29 09:52:14 +01:00
|
|
|
MemoryMap = NEXT_MEMORY_DESCRIPTOR (MemoryMap, DescriptorSize);
|
|
|
|
}
|
2021-12-05 23:54:02 +01:00
|
|
|
|
2016-04-20 10:19:01 +02:00
|
|
|
MemoryMap = MemoryMapStart;
|
|
|
|
FreePool (MemoryMap);
|
2016-01-29 09:52:14 +01:00
|
|
|
|
2016-04-20 11:27:40 +02:00
|
|
|
//
|
|
|
|
// Update configuratoin table for MemoryAttributesTable.
|
|
|
|
//
|
2016-01-29 09:52:14 +01:00
|
|
|
Status = gBS->InstallConfigurationTable (&gEfiMemoryAttributesTableGuid, MemoryAttributesTable);
|
|
|
|
ASSERT_EFI_ERROR (Status);
|
2016-04-20 11:27:40 +02:00
|
|
|
|
|
|
|
if (mMemoryAttributesTable != NULL) {
|
|
|
|
FreePool (mMemoryAttributesTable);
|
|
|
|
}
|
2021-12-05 23:54:02 +01:00
|
|
|
|
2018-06-27 15:08:52 +02:00
|
|
|
mMemoryAttributesTable = MemoryAttributesTable;
|
2016-04-20 11:27:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
Install MemoryAttributesTable on memory allocation.
|
|
|
|
|
|
|
|
@param[in] MemoryType EFI memory type.
|
|
|
|
**/
|
|
|
|
VOID
|
|
|
|
InstallMemoryAttributesTableOnMemoryAllocation (
|
|
|
|
IN EFI_MEMORY_TYPE MemoryType
|
|
|
|
)
|
|
|
|
{
|
|
|
|
//
|
|
|
|
// Install MemoryAttributesTable after ReadyToBoot on runtime memory allocation.
|
|
|
|
//
|
|
|
|
if (mMemoryAttributesTableReadyToBoot &&
|
|
|
|
((MemoryType == EfiRuntimeServicesCode) || (MemoryType == EfiRuntimeServicesData)))
|
|
|
|
{
|
|
|
|
InstallMemoryAttributesTable ();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
Install MemoryAttributesTable on ReadyToBoot.
|
|
|
|
|
|
|
|
@param[in] Event The Event this notify function registered to.
|
|
|
|
@param[in] Context Pointer to the context data registered to the Event.
|
|
|
|
**/
|
|
|
|
VOID
|
|
|
|
EFIAPI
|
|
|
|
InstallMemoryAttributesTableOnReadyToBoot (
|
|
|
|
IN EFI_EVENT Event,
|
|
|
|
IN VOID *Context
|
|
|
|
)
|
|
|
|
{
|
|
|
|
InstallMemoryAttributesTable ();
|
2018-06-27 15:08:52 +02:00
|
|
|
mMemoryAttributesTableReadyToBoot = TRUE;
|
2016-01-29 09:52:14 +01:00
|
|
|
}
|
|
|
|
|
2017-11-22 15:05:07 +01:00
|
|
|
/**
|
|
|
|
Install initial MemoryAttributesTable on EndOfDxe.
|
|
|
|
Then SMM can consume this information.
|
|
|
|
|
|
|
|
@param[in] Event The Event this notify function registered to.
|
|
|
|
@param[in] Context Pointer to the context data registered to the Event.
|
|
|
|
**/
|
|
|
|
VOID
|
|
|
|
EFIAPI
|
|
|
|
InstallMemoryAttributesTableOnEndOfDxe (
|
|
|
|
IN EFI_EVENT Event,
|
|
|
|
IN VOID *Context
|
|
|
|
)
|
|
|
|
{
|
2020-04-07 09:48:30 +02:00
|
|
|
mMemoryAttributesTableEndOfDxe = TRUE;
|
2017-11-22 15:05:07 +01:00
|
|
|
InstallMemoryAttributesTable ();
|
2023-11-03 16:29:44 +01:00
|
|
|
|
|
|
|
DEBUG_CODE_BEGIN ();
|
|
|
|
if ( mImagePropertiesPrivateData.ImageRecordCount > 0) {
|
|
|
|
DEBUG ((DEBUG_INFO, "DXE - Total Runtime Image Count: 0x%x\n", mImagePropertiesPrivateData.ImageRecordCount));
|
|
|
|
DEBUG ((DEBUG_INFO, "DXE - Dump Runtime Image Records:\n"));
|
|
|
|
DumpImageRecords (&mImagePropertiesPrivateData.ImageRecordList);
|
|
|
|
}
|
|
|
|
|
|
|
|
DEBUG_CODE_END ();
|
2017-11-22 15:05:07 +01:00
|
|
|
}
|
|
|
|
|
2016-01-29 09:52:14 +01:00
|
|
|
/**
|
|
|
|
Initialize MemoryAttrubutesTable support.
|
|
|
|
**/
|
|
|
|
VOID
|
|
|
|
EFIAPI
|
|
|
|
CoreInitializeMemoryAttributesTable (
|
|
|
|
VOID
|
|
|
|
)
|
|
|
|
{
|
|
|
|
EFI_STATUS Status;
|
|
|
|
EFI_EVENT ReadyToBootEvent;
|
2017-11-22 15:05:07 +01:00
|
|
|
EFI_EVENT EndOfDxeEvent;
|
2016-01-29 09:52:14 +01:00
|
|
|
|
|
|
|
//
|
2016-04-20 11:27:40 +02:00
|
|
|
// Construct the table at ReadyToBoot.
|
2016-01-29 09:52:14 +01:00
|
|
|
//
|
2016-04-20 11:27:40 +02:00
|
|
|
Status = CoreCreateEventInternal (
|
|
|
|
EVT_NOTIFY_SIGNAL,
|
2017-11-22 15:05:07 +01:00
|
|
|
TPL_CALLBACK,
|
2016-04-20 11:27:40 +02:00
|
|
|
InstallMemoryAttributesTableOnReadyToBoot,
|
|
|
|
NULL,
|
|
|
|
&gEfiEventReadyToBootGuid,
|
|
|
|
&ReadyToBootEvent
|
|
|
|
);
|
2016-01-29 09:52:14 +01:00
|
|
|
ASSERT_EFI_ERROR (Status);
|
2017-11-22 15:05:07 +01:00
|
|
|
|
|
|
|
//
|
|
|
|
// Construct the initial table at EndOfDxe,
|
|
|
|
// then SMM can consume this information.
|
|
|
|
// Use TPL_NOTIFY here, as such SMM code (TPL_CALLBACK)
|
|
|
|
// can run after it.
|
|
|
|
//
|
|
|
|
Status = CoreCreateEventInternal (
|
|
|
|
EVT_NOTIFY_SIGNAL,
|
|
|
|
TPL_NOTIFY,
|
|
|
|
InstallMemoryAttributesTableOnEndOfDxe,
|
|
|
|
NULL,
|
|
|
|
&gEfiEndOfDxeEventGroupGuid,
|
|
|
|
&EndOfDxeEvent
|
|
|
|
);
|
|
|
|
ASSERT_EFI_ERROR (Status);
|
2016-01-29 09:52:14 +01:00
|
|
|
return;
|
|
|
|
}
|
2020-04-07 09:48:30 +02:00
|
|
|
|
|
|
|
//
|
|
|
|
// Below functions are for MemoryMap
|
|
|
|
//
|
|
|
|
|
|
|
|
/**
|
|
|
|
Acquire memory lock on mMemoryAttributesTableLock.
|
|
|
|
**/
|
|
|
|
STATIC
|
|
|
|
VOID
|
|
|
|
CoreAcquiremMemoryAttributesTableLock (
|
|
|
|
VOID
|
|
|
|
)
|
|
|
|
{
|
|
|
|
CoreAcquireLock (&mMemoryAttributesTableLock);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
Release memory lock on mMemoryAttributesTableLock.
|
|
|
|
**/
|
|
|
|
STATIC
|
|
|
|
VOID
|
|
|
|
CoreReleasemMemoryAttributesTableLock (
|
|
|
|
VOID
|
|
|
|
)
|
|
|
|
{
|
|
|
|
CoreReleaseLock (&mMemoryAttributesTableLock);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
Merge continous memory map entries whose have same attributes.
|
|
|
|
|
|
|
|
@param MemoryMap A pointer to the buffer in which firmware places
|
|
|
|
the current memory map.
|
|
|
|
@param MemoryMapSize A pointer to the size, in bytes, of the
|
|
|
|
MemoryMap buffer. On input, this is the size of
|
|
|
|
the current memory map. On output,
|
|
|
|
it is the size of new memory map after merge.
|
|
|
|
@param DescriptorSize Size, in bytes, of an individual EFI_MEMORY_DESCRIPTOR.
|
|
|
|
**/
|
|
|
|
VOID
|
|
|
|
MergeMemoryMap (
|
|
|
|
IN OUT EFI_MEMORY_DESCRIPTOR *MemoryMap,
|
|
|
|
IN OUT UINTN *MemoryMapSize,
|
|
|
|
IN UINTN DescriptorSize
|
|
|
|
)
|
|
|
|
{
|
|
|
|
EFI_MEMORY_DESCRIPTOR *MemoryMapEntry;
|
|
|
|
EFI_MEMORY_DESCRIPTOR *MemoryMapEnd;
|
|
|
|
UINT64 MemoryBlockLength;
|
|
|
|
EFI_MEMORY_DESCRIPTOR *NewMemoryMapEntry;
|
|
|
|
EFI_MEMORY_DESCRIPTOR *NextMemoryMapEntry;
|
|
|
|
|
|
|
|
MemoryMapEntry = MemoryMap;
|
|
|
|
NewMemoryMapEntry = MemoryMap;
|
|
|
|
MemoryMapEnd = (EFI_MEMORY_DESCRIPTOR *)((UINT8 *)MemoryMap + *MemoryMapSize);
|
|
|
|
while ((UINTN)MemoryMapEntry < (UINTN)MemoryMapEnd) {
|
2024-08-28 19:55:09 +02:00
|
|
|
CopyMem (NewMemoryMapEntry, MemoryMapEntry, DescriptorSize);
|
2020-04-07 09:48:30 +02:00
|
|
|
NextMemoryMapEntry = NEXT_MEMORY_DESCRIPTOR (MemoryMapEntry, DescriptorSize);
|
|
|
|
|
|
|
|
do {
|
2024-08-28 19:55:09 +02:00
|
|
|
if ((UINTN)NextMemoryMapEntry < (UINTN)MemoryMapEnd) {
|
|
|
|
MergeGuardPages (NewMemoryMapEntry, NextMemoryMapEntry->PhysicalStart);
|
|
|
|
}
|
|
|
|
|
2023-11-03 16:29:43 +01:00
|
|
|
MemoryBlockLength = LShiftU64 (NewMemoryMapEntry->NumberOfPages, EFI_PAGE_SHIFT);
|
2020-04-07 09:48:30 +02:00
|
|
|
if (((UINTN)NextMemoryMapEntry < (UINTN)MemoryMapEnd) &&
|
|
|
|
(NewMemoryMapEntry->Type == NextMemoryMapEntry->Type) &&
|
|
|
|
(NewMemoryMapEntry->Attribute == NextMemoryMapEntry->Attribute) &&
|
|
|
|
((NewMemoryMapEntry->PhysicalStart + MemoryBlockLength) == NextMemoryMapEntry->PhysicalStart))
|
|
|
|
{
|
|
|
|
NewMemoryMapEntry->NumberOfPages += NextMemoryMapEntry->NumberOfPages;
|
|
|
|
NextMemoryMapEntry = NEXT_MEMORY_DESCRIPTOR (NextMemoryMapEntry, DescriptorSize);
|
|
|
|
continue;
|
|
|
|
} else {
|
|
|
|
MemoryMapEntry = PREVIOUS_MEMORY_DESCRIPTOR (NextMemoryMapEntry, DescriptorSize);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} while (TRUE);
|
|
|
|
|
|
|
|
MemoryMapEntry = NEXT_MEMORY_DESCRIPTOR (MemoryMapEntry, DescriptorSize);
|
|
|
|
NewMemoryMapEntry = NEXT_MEMORY_DESCRIPTOR (NewMemoryMapEntry, DescriptorSize);
|
|
|
|
}
|
|
|
|
|
|
|
|
*MemoryMapSize = (UINTN)NewMemoryMapEntry - (UINTN)MemoryMap;
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
Enforce memory map attributes.
|
MdeModulePkg: MAT: Do Not Set EfiMemoryMappedIo[PortSpace] Attrs
Per UEFI spec 2.10 section 4.6.3 EFI_MEMORY_ATTRIBUTES_TABLE,
"The Memory Attributes Table is currently used to describe memory
protections that may be applied to the EFI Runtime code and data
by an operating system or hypervisor. Consumers of this table must
currently ignore entries containing any values for Type except for
EfiRuntimeServicesData and EfiRuntimeServicesCode to ensure
compatibility with future uses of this table."
However, the current MAT code also enforces attributes for
EfiMemoryMappedIo and EfiMemoryMappedIoPortSpace, which it should
not be. Per
https://edk2.groups.io/g/devel/topic/patch_v1_mdemodulepkg/105570114?p=,,,20,0,0,0::recentpostdate/sticky,,,20,2,0,105570114,
it was suggested to remove these types from the MAT logic.
This patch removes EfiMemoryMappedIo and EfiMemoryMappedIoPortSpace
from the MAT logic in accordance with the UEFI spec.
Signed-off-by: Oliver Smith-Denny <osde@linux.microsoft.com>
2024-08-26 19:18:04 +02:00
|
|
|
This function will set EfiRuntimeServicesData to be EFI_MEMORY_XP.
|
2020-04-07 09:48:30 +02:00
|
|
|
|
|
|
|
@param MemoryMap A pointer to the buffer in which firmware places
|
|
|
|
the current memory map.
|
|
|
|
@param MemoryMapSize Size, in bytes, of the MemoryMap buffer.
|
|
|
|
@param DescriptorSize Size, in bytes, of an individual EFI_MEMORY_DESCRIPTOR.
|
|
|
|
**/
|
|
|
|
STATIC
|
|
|
|
VOID
|
|
|
|
EnforceMemoryMapAttribute (
|
|
|
|
IN OUT EFI_MEMORY_DESCRIPTOR *MemoryMap,
|
|
|
|
IN UINTN MemoryMapSize,
|
|
|
|
IN UINTN DescriptorSize
|
|
|
|
)
|
|
|
|
{
|
|
|
|
EFI_MEMORY_DESCRIPTOR *MemoryMapEntry;
|
|
|
|
EFI_MEMORY_DESCRIPTOR *MemoryMapEnd;
|
|
|
|
|
|
|
|
MemoryMapEntry = MemoryMap;
|
|
|
|
MemoryMapEnd = (EFI_MEMORY_DESCRIPTOR *)((UINT8 *)MemoryMap + MemoryMapSize);
|
|
|
|
while ((UINTN)MemoryMapEntry < (UINTN)MemoryMapEnd) {
|
MdeModulePkg: MAT Set RO/XP on Code/Data Sections Outside Image Memory
The Memory Attributes Table is generated by fetching the EFI memory map
and splitting entries which contain loaded images so DATA and CODE
sections have separate descriptors. The splitting is done via a call to
SplitTable() which
marks image DATA sections with the EFI_MEMORY_XP attribute and CODE
sections with the EFI_MEMORY_RO attribute when
splitting. After this process, there may still be EfiRuntimeServicesCode
regions which did not have their attributes set because they are not
part of loaded images.
This patch updates the MAT EnforceMemoryMapAttribute logic to set the
access attributes of runtime memory regions which are not part of loaded
images (have not had their access attributes set). The attributes of the
code regions will be read-only and no-execute because the UEFI spec
dictates that runtime code regions should only contain loaded EFI
modules.
BZ: https://bugzilla.tianocore.org/show_bug.cgi?id=4832
Refs:
1.
https://edk2.groups.io/g/devel/topic/patch_v1_mdemodulepkg/105570114?p=,,,20,0,0,0::recentpostdate/sticky,,,20,2,0,105570114
2.
https://edk2.groups.io/g/devel/topic/mdemodulepkg_fix_mat/105477564?p=,,,20,0,0,0::recentpostdate/sticky,,,20,2,0,105477564
Signed-off-by: Oliver Smith-Denny <osde@linux.microsoft.com>
2024-08-26 19:23:14 +02:00
|
|
|
if ((MemoryMapEntry->Attribute & EFI_MEMORY_ACCESS_MASK) == 0) {
|
|
|
|
switch (MemoryMapEntry->Type) {
|
|
|
|
case EfiRuntimeServicesCode:
|
|
|
|
// If at this point the attributes have not been set on an EfiRuntimeServicesCode
|
|
|
|
// region, the memory range must not contain a loaded image. It's possible these
|
|
|
|
// non-image EfiRuntimeServicesCode regions are part of the unused memory bucket.
|
|
|
|
// It could also be that this region was explicitly allocated outside of the PE
|
|
|
|
// loader but the UEFI spec requires that all EfiRuntimeServicesCode regions contain
|
|
|
|
// EFI modules. In either case, set the attributes to RO and XP.
|
|
|
|
MemoryMapEntry->Attribute |= (EFI_MEMORY_RO | EFI_MEMORY_XP);
|
|
|
|
break;
|
|
|
|
case EfiRuntimeServicesData:
|
|
|
|
MemoryMapEntry->Attribute |= EFI_MEMORY_XP;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2020-04-07 09:48:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
MemoryMapEntry = NEXT_MEMORY_DESCRIPTOR (MemoryMapEntry, DescriptorSize);
|
|
|
|
}
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
This function for GetMemoryMap() with properties table capability.
|
|
|
|
|
|
|
|
It calls original GetMemoryMap() to get the original memory map information. Then
|
|
|
|
plus the additional memory map entries for PE Code/Data seperation.
|
|
|
|
|
|
|
|
@param MemoryMapSize A pointer to the size, in bytes, of the
|
|
|
|
MemoryMap buffer. On input, this is the size of
|
|
|
|
the buffer allocated by the caller. On output,
|
|
|
|
it is the size of the buffer returned by the
|
|
|
|
firmware if the buffer was large enough, or the
|
|
|
|
size of the buffer needed to contain the map if
|
|
|
|
the buffer was too small.
|
|
|
|
@param MemoryMap A pointer to the buffer in which firmware places
|
|
|
|
the current memory map.
|
|
|
|
@param MapKey A pointer to the location in which firmware
|
|
|
|
returns the key for the current memory map.
|
|
|
|
@param DescriptorSize A pointer to the location in which firmware
|
|
|
|
returns the size, in bytes, of an individual
|
|
|
|
EFI_MEMORY_DESCRIPTOR.
|
|
|
|
@param DescriptorVersion A pointer to the location in which firmware
|
|
|
|
returns the version number associated with the
|
|
|
|
EFI_MEMORY_DESCRIPTOR.
|
|
|
|
|
|
|
|
@retval EFI_SUCCESS The memory map was returned in the MemoryMap
|
|
|
|
buffer.
|
|
|
|
@retval EFI_BUFFER_TOO_SMALL The MemoryMap buffer was too small. The current
|
|
|
|
buffer size needed to hold the memory map is
|
|
|
|
returned in MemoryMapSize.
|
|
|
|
@retval EFI_INVALID_PARAMETER One of the parameters has an invalid value.
|
|
|
|
|
|
|
|
**/
|
|
|
|
EFI_STATUS
|
|
|
|
EFIAPI
|
|
|
|
CoreGetMemoryMapWithSeparatedImageSection (
|
|
|
|
IN OUT UINTN *MemoryMapSize,
|
|
|
|
IN OUT EFI_MEMORY_DESCRIPTOR *MemoryMap,
|
|
|
|
OUT UINTN *MapKey,
|
|
|
|
OUT UINTN *DescriptorSize,
|
|
|
|
OUT UINT32 *DescriptorVersion
|
|
|
|
)
|
|
|
|
{
|
|
|
|
EFI_STATUS Status;
|
|
|
|
UINTN OldMemoryMapSize;
|
|
|
|
UINTN AdditionalRecordCount;
|
|
|
|
|
|
|
|
//
|
|
|
|
// If PE code/data is not aligned, just return.
|
|
|
|
//
|
|
|
|
if (!mMemoryAttributesTableEnable) {
|
|
|
|
return CoreGetMemoryMap (MemoryMapSize, MemoryMap, MapKey, DescriptorSize, DescriptorVersion);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (MemoryMapSize == NULL) {
|
|
|
|
return EFI_INVALID_PARAMETER;
|
|
|
|
}
|
|
|
|
|
|
|
|
CoreAcquiremMemoryAttributesTableLock ();
|
|
|
|
|
2023-11-20 21:07:02 +01:00
|
|
|
AdditionalRecordCount = (2 * mImagePropertiesPrivateData.CodeSegmentCountMax + 3) * mImagePropertiesPrivateData.ImageRecordCount;
|
2020-04-07 09:48:30 +02:00
|
|
|
|
|
|
|
OldMemoryMapSize = *MemoryMapSize;
|
|
|
|
Status = CoreGetMemoryMap (MemoryMapSize, MemoryMap, MapKey, DescriptorSize, DescriptorVersion);
|
|
|
|
if (Status == EFI_BUFFER_TOO_SMALL) {
|
|
|
|
*MemoryMapSize = *MemoryMapSize + (*DescriptorSize) * AdditionalRecordCount;
|
|
|
|
} else if (Status == EFI_SUCCESS) {
|
|
|
|
ASSERT (MemoryMap != NULL);
|
|
|
|
if (OldMemoryMapSize - *MemoryMapSize < (*DescriptorSize) * AdditionalRecordCount) {
|
|
|
|
*MemoryMapSize = *MemoryMapSize + (*DescriptorSize) * AdditionalRecordCount;
|
|
|
|
//
|
|
|
|
// Need update status to buffer too small
|
|
|
|
//
|
|
|
|
Status = EFI_BUFFER_TOO_SMALL;
|
|
|
|
} else {
|
|
|
|
//
|
|
|
|
// Split PE code/data
|
|
|
|
//
|
2023-11-03 16:29:42 +01:00
|
|
|
SplitTable (MemoryMapSize, MemoryMap, *DescriptorSize, &mImagePropertiesPrivateData.ImageRecordList, AdditionalRecordCount);
|
2023-11-03 16:29:43 +01:00
|
|
|
|
|
|
|
//
|
|
|
|
// Set RuntimeData to XP
|
|
|
|
//
|
|
|
|
EnforceMemoryMapAttribute (MemoryMap, *MemoryMapSize, *DescriptorSize);
|
|
|
|
|
|
|
|
//
|
|
|
|
// Merge same type to save entry size
|
|
|
|
//
|
|
|
|
MergeMemoryMap (MemoryMap, MemoryMapSize, *DescriptorSize);
|
2020-04-07 09:48:30 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
CoreReleasemMemoryAttributesTableLock ();
|
|
|
|
return Status;
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// Below functions are for ImageRecord
|
|
|
|
//
|
|
|
|
|
|
|
|
/**
|
|
|
|
Insert image record.
|
|
|
|
|
|
|
|
@param RuntimeImage Runtime image information
|
|
|
|
**/
|
|
|
|
VOID
|
|
|
|
InsertImageRecord (
|
|
|
|
IN EFI_RUNTIME_IMAGE_ENTRY *RuntimeImage
|
|
|
|
)
|
|
|
|
{
|
2023-11-03 16:29:44 +01:00
|
|
|
EFI_STATUS Status;
|
|
|
|
IMAGE_PROPERTIES_RECORD *ImageRecord;
|
|
|
|
CHAR8 *PdbPointer;
|
|
|
|
UINT32 RequiredAlignment;
|
2020-04-07 09:48:30 +02:00
|
|
|
|
|
|
|
DEBUG ((DEBUG_VERBOSE, "InsertImageRecord - 0x%x\n", RuntimeImage));
|
|
|
|
|
|
|
|
if (mMemoryAttributesTableEndOfDxe) {
|
|
|
|
DEBUG ((DEBUG_INFO, "Do not insert runtime image record after EndOfDxe\n"));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
ImageRecord = AllocatePool (sizeof (*ImageRecord));
|
|
|
|
if (ImageRecord == NULL) {
|
|
|
|
return;
|
|
|
|
}
|
2021-12-05 23:54:02 +01:00
|
|
|
|
2023-11-03 16:29:44 +01:00
|
|
|
InitializeListHead (&ImageRecord->Link);
|
|
|
|
InitializeListHead (&ImageRecord->CodeSegmentList);
|
2020-04-07 09:48:30 +02:00
|
|
|
|
2023-11-03 16:29:44 +01:00
|
|
|
PdbPointer = PeCoffLoaderGetPdbPointer ((VOID *)(UINTN)RuntimeImage->ImageBase);
|
2020-04-07 09:48:30 +02:00
|
|
|
if (PdbPointer != NULL) {
|
|
|
|
DEBUG ((DEBUG_VERBOSE, " Image - %a\n", PdbPointer));
|
|
|
|
}
|
|
|
|
|
2023-11-03 16:29:44 +01:00
|
|
|
RequiredAlignment = RUNTIME_PAGE_ALLOCATION_GRANULARITY;
|
|
|
|
Status = CreateImagePropertiesRecord (
|
|
|
|
RuntimeImage->ImageBase,
|
|
|
|
RuntimeImage->ImageSize,
|
|
|
|
&RequiredAlignment,
|
|
|
|
ImageRecord
|
|
|
|
);
|
|
|
|
|
|
|
|
if (EFI_ERROR (Status)) {
|
|
|
|
if (Status == EFI_ABORTED) {
|
|
|
|
mMemoryAttributesTableEnable = FALSE;
|
2020-04-07 09:48:30 +02:00
|
|
|
}
|
2021-12-05 23:54:02 +01:00
|
|
|
|
2023-11-03 16:29:44 +01:00
|
|
|
Status = EFI_ABORTED;
|
2020-04-07 09:48:30 +02:00
|
|
|
goto Finish;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ImageRecord->CodeSegmentCount == 0) {
|
2023-11-03 16:29:44 +01:00
|
|
|
mMemoryAttributesTableEnable = FALSE;
|
2020-04-07 09:48:30 +02:00
|
|
|
DEBUG ((DEBUG_ERROR, "!!!!!!!! InsertImageRecord - CodeSegmentCount is 0 !!!!!!!!\n"));
|
|
|
|
if (PdbPointer != NULL) {
|
|
|
|
DEBUG ((DEBUG_ERROR, "!!!!!!!! Image - %a !!!!!!!!\n", PdbPointer));
|
|
|
|
}
|
2021-12-05 23:54:02 +01:00
|
|
|
|
2023-11-03 16:29:44 +01:00
|
|
|
Status = EFI_ABORTED;
|
2020-04-07 09:48:30 +02:00
|
|
|
goto Finish;
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// Check overlap all section in ImageBase/Size
|
|
|
|
//
|
|
|
|
if (!IsImageRecordCodeSectionValid (ImageRecord)) {
|
|
|
|
DEBUG ((DEBUG_ERROR, "IsImageRecordCodeSectionValid - FAIL\n"));
|
2023-11-03 16:29:44 +01:00
|
|
|
Status = EFI_ABORTED;
|
2020-04-07 09:48:30 +02:00
|
|
|
goto Finish;
|
|
|
|
}
|
|
|
|
|
|
|
|
InsertTailList (&mImagePropertiesPrivateData.ImageRecordList, &ImageRecord->Link);
|
|
|
|
mImagePropertiesPrivateData.ImageRecordCount++;
|
|
|
|
|
|
|
|
if (mImagePropertiesPrivateData.CodeSegmentCountMax < ImageRecord->CodeSegmentCount) {
|
|
|
|
mImagePropertiesPrivateData.CodeSegmentCountMax = ImageRecord->CodeSegmentCount;
|
|
|
|
}
|
|
|
|
|
2023-11-03 16:29:42 +01:00
|
|
|
SortImageRecord (&mImagePropertiesPrivateData.ImageRecordList);
|
2020-04-07 09:48:30 +02:00
|
|
|
|
|
|
|
Finish:
|
2023-11-03 16:29:44 +01:00
|
|
|
if (EFI_ERROR (Status) && (ImageRecord != NULL)) {
|
|
|
|
DeleteImagePropertiesRecord (ImageRecord);
|
|
|
|
}
|
|
|
|
|
2020-04-07 09:48:30 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
Remove Image record.
|
|
|
|
|
|
|
|
@param RuntimeImage Runtime image information
|
|
|
|
**/
|
|
|
|
VOID
|
|
|
|
RemoveImageRecord (
|
|
|
|
IN EFI_RUNTIME_IMAGE_ENTRY *RuntimeImage
|
|
|
|
)
|
|
|
|
{
|
2023-11-03 16:29:44 +01:00
|
|
|
IMAGE_PROPERTIES_RECORD *ImageRecord;
|
2020-04-07 09:48:30 +02:00
|
|
|
|
|
|
|
DEBUG ((DEBUG_VERBOSE, "RemoveImageRecord - 0x%x\n", RuntimeImage));
|
|
|
|
DEBUG ((DEBUG_VERBOSE, "RemoveImageRecord - 0x%016lx - 0x%016lx\n", (EFI_PHYSICAL_ADDRESS)(UINTN)RuntimeImage->ImageBase, RuntimeImage->ImageSize));
|
|
|
|
|
|
|
|
if (mMemoryAttributesTableEndOfDxe) {
|
|
|
|
DEBUG ((DEBUG_INFO, "Do not remove runtime image record after EndOfDxe\n"));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-11-03 16:29:42 +01:00
|
|
|
ImageRecord = FindImageRecord ((EFI_PHYSICAL_ADDRESS)(UINTN)RuntimeImage->ImageBase, RuntimeImage->ImageSize, &mImagePropertiesPrivateData.ImageRecordList);
|
2020-04-07 09:48:30 +02:00
|
|
|
if (ImageRecord == NULL) {
|
|
|
|
DEBUG ((DEBUG_ERROR, "!!!!!!!! ImageRecord not found !!!!!!!!\n"));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-11-03 16:29:44 +01:00
|
|
|
DeleteImagePropertiesRecord (ImageRecord);
|
2020-04-07 09:48:30 +02:00
|
|
|
|
|
|
|
mImagePropertiesPrivateData.ImageRecordCount--;
|
|
|
|
}
|