65 OUT UINT32 *DescriptorVersion
68#define PREVIOUS_MEMORY_DESCRIPTOR(MemoryDescriptor, Size) \
69 ((EFI_MEMORY_DESCRIPTOR *)((UINT8 *)(MemoryDescriptor) - (Size)))
71#define IMAGE_PROPERTIES_PRIVATE_DATA_SIGNATURE SIGNATURE_32 ('I','P','P','D')
75 UINTN ImageRecordCount;
76 UINTN CodeSegmentCountMax;
81 IMAGE_PROPERTIES_PRIVATE_DATA_SIGNATURE,
89BOOLEAN mMemoryAttributesTableEnable =
TRUE;
91BOOLEAN mMemoryAttributesTableReadyToBoot =
FALSE;
92BOOLEAN gMemoryAttributesTableForwardCfi =
TRUE;
107 UINTN DescriptorSize;
108 UINT32 DescriptorVersion;
111 UINT32 RuntimeEntryCount;
115 if (gMemoryMapTerminated) {
122 if (!mMemoryAttributesTableEnable) {
123 DEBUG ((DEBUG_VERBOSE,
"Cannot install Memory Attributes Table "));
124 DEBUG ((DEBUG_VERBOSE,
"because Runtime Driver Section Alignment is not %dK.\n", RUNTIME_PAGE_ALLOCATION_GRANULARITY >> 10));
128 if (mMemoryAttributesTable ==
NULL) {
147 ASSERT (Status == EFI_BUFFER_TOO_SMALL);
151 if (MemoryMap ==
NULL) {
152 ASSERT (MemoryMap !=
NULL);
163 if (EFI_ERROR (Status)) {
166 }
while (Status == EFI_BUFFER_TOO_SMALL);
168 MemoryMapStart = MemoryMap;
169 RuntimeEntryCount = 0;
170 for (Index = 0; Index < MemoryMapSize/DescriptorSize; Index++) {
171 switch (MemoryMap->
Type) {
178 MemoryMap = NEXT_MEMORY_DESCRIPTOR (MemoryMap, DescriptorSize);
185 if (MemoryAttributesTable ==
NULL) {
186 ASSERT (MemoryAttributesTable !=
NULL);
191 MemoryAttributesTable->Version = EFI_MEMORY_ATTRIBUTES_TABLE_VERSION;
192 MemoryAttributesTable->NumberOfEntries = RuntimeEntryCount;
193 MemoryAttributesTable->DescriptorSize = (UINT32)DescriptorSize;
194 if (gMemoryAttributesTableForwardCfi) {
195 MemoryAttributesTable->Flags = EFI_MEMORY_ATTRIBUTES_FLAGS_RT_FORWARD_CONTROL_FLOW_GUARD;
197 MemoryAttributesTable->Flags = 0;
200 DEBUG ((DEBUG_VERBOSE,
"MemoryAttributesTable:\n"));
201 DEBUG ((DEBUG_VERBOSE,
" Version - 0x%08x\n", MemoryAttributesTable->Version));
202 DEBUG ((DEBUG_VERBOSE,
" NumberOfEntries - 0x%08x\n", MemoryAttributesTable->NumberOfEntries));
203 DEBUG ((DEBUG_VERBOSE,
" DescriptorSize - 0x%08x\n", MemoryAttributesTable->DescriptorSize));
205 MemoryMap = MemoryMapStart;
206 for (Index = 0; Index < MemoryMapSize/DescriptorSize; Index++) {
207 switch (MemoryMap->
Type) {
210 CopyMem (MemoryAttributesEntry, MemoryMap, DescriptorSize);
211 MemoryAttributesEntry->
Attribute &= (EFI_MEMORY_RO|EFI_MEMORY_XP|EFI_MEMORY_RUNTIME);
212 DEBUG ((DEBUG_VERBOSE,
"Entry (0x%x)\n", MemoryAttributesEntry));
213 DEBUG ((DEBUG_VERBOSE,
" Type - 0x%x\n", MemoryAttributesEntry->
Type));
214 DEBUG ((DEBUG_VERBOSE,
" PhysicalStart - 0x%016lx\n", MemoryAttributesEntry->
PhysicalStart));
215 DEBUG ((DEBUG_VERBOSE,
" VirtualStart - 0x%016lx\n", MemoryAttributesEntry->
VirtualStart));
216 DEBUG ((DEBUG_VERBOSE,
" NumberOfPages - 0x%016lx\n", MemoryAttributesEntry->
NumberOfPages));
217 DEBUG ((DEBUG_VERBOSE,
" Attribute - 0x%016lx\n", MemoryAttributesEntry->
Attribute));
218 MemoryAttributesEntry = NEXT_MEMORY_DESCRIPTOR (MemoryAttributesEntry, DescriptorSize);
222 MemoryMap = NEXT_MEMORY_DESCRIPTOR (MemoryMap, DescriptorSize);
225 MemoryMap = MemoryMapStart;
231 Status =
gBS->InstallConfigurationTable (&gEfiMemoryAttributesTableGuid, MemoryAttributesTable);
234 if (mMemoryAttributesTable !=
NULL) {
238 mMemoryAttributesTable = MemoryAttributesTable;
254 if (mMemoryAttributesTableReadyToBoot &&
275 mMemoryAttributesTableReadyToBoot =
TRUE;
295 if ( mImagePropertiesPrivateData.ImageRecordCount > 0) {
296 DEBUG ((DEBUG_INFO,
"DXE - Total Runtime Image Count: 0x%x\n", mImagePropertiesPrivateData.ImageRecordCount));
297 DEBUG ((DEBUG_INFO,
"DXE - Dump Runtime Image Records:\n"));
325 &gEfiEventReadyToBootGuid,
341 &gEfiEndOfDxeEventGroupGuid,
396 UINT64 MemoryBlockLength;
400 MemoryMapEntry = MemoryMap;
401 NewMemoryMapEntry = MemoryMap;
403 while ((
UINTN)MemoryMapEntry < (
UINTN)MemoryMapEnd) {
404 CopyMem (NewMemoryMapEntry, MemoryMapEntry, DescriptorSize);
405 NextMemoryMapEntry = NEXT_MEMORY_DESCRIPTOR (MemoryMapEntry, DescriptorSize);
408 if ((
UINTN)NextMemoryMapEntry < (
UINTN)MemoryMapEnd) {
413 if (((
UINTN)NextMemoryMapEntry < (
UINTN)MemoryMapEnd) &&
414 (NewMemoryMapEntry->
Type == NextMemoryMapEntry->
Type) &&
419 NextMemoryMapEntry = NEXT_MEMORY_DESCRIPTOR (NextMemoryMapEntry, DescriptorSize);
422 MemoryMapEntry = PREVIOUS_MEMORY_DESCRIPTOR (NextMemoryMapEntry, DescriptorSize);
427 MemoryMapEntry = NEXT_MEMORY_DESCRIPTOR (MemoryMapEntry, DescriptorSize);
428 NewMemoryMapEntry = NEXT_MEMORY_DESCRIPTOR (NewMemoryMapEntry, DescriptorSize);
431 *MemoryMapSize = (
UINTN)NewMemoryMapEntry - (
UINTN)MemoryMap;
456 MemoryMapEntry = MemoryMap;
458 while ((
UINTN)MemoryMapEntry < (
UINTN)MemoryMapEnd) {
459 if ((MemoryMapEntry->
Attribute & EFI_MEMORY_ACCESS_MASK) == 0) {
460 switch (MemoryMapEntry->
Type) {
468 MemoryMapEntry->
Attribute |= (EFI_MEMORY_RO | EFI_MEMORY_XP);
471 MemoryMapEntry->
Attribute |= EFI_MEMORY_XP;
478 MemoryMapEntry = NEXT_MEMORY_DESCRIPTOR (MemoryMapEntry, DescriptorSize);
523 OUT UINT32 *DescriptorVersion
527 UINTN OldMemoryMapSize;
528 UINTN AdditionalRecordCount;
533 if (!mMemoryAttributesTableEnable) {
534 return CoreGetMemoryMap (MemoryMapSize, MemoryMap, MapKey, DescriptorSize, DescriptorVersion);
537 if (MemoryMapSize ==
NULL) {
538 return EFI_INVALID_PARAMETER;
543 AdditionalRecordCount = (2 * mImagePropertiesPrivateData.CodeSegmentCountMax + 3) * mImagePropertiesPrivateData.ImageRecordCount;
545 OldMemoryMapSize = *MemoryMapSize;
546 Status =
CoreGetMemoryMap (MemoryMapSize, MemoryMap, MapKey, DescriptorSize, DescriptorVersion);
547 if (Status == EFI_BUFFER_TOO_SMALL) {
548 *MemoryMapSize = *MemoryMapSize + (*DescriptorSize) * AdditionalRecordCount;
550 ASSERT (MemoryMap !=
NULL);
551 if (OldMemoryMapSize - *MemoryMapSize < (*DescriptorSize) * AdditionalRecordCount) {
552 *MemoryMapSize = *MemoryMapSize + (*DescriptorSize) * AdditionalRecordCount;
556 Status = EFI_BUFFER_TOO_SMALL;
561 SplitTable (MemoryMapSize, MemoryMap, *DescriptorSize, &mImagePropertiesPrivateData.ImageRecordList, AdditionalRecordCount);
596 UINT32 RequiredAlignment;
598 DEBUG ((DEBUG_VERBOSE,
"InsertImageRecord - 0x%x\n", RuntimeImage));
601 if (ImageRecord ==
NULL) {
608 PdbPointer = PeCoffLoaderGetPdbPointer ((
VOID *)(
UINTN)RuntimeImage->ImageBase);
609 if (PdbPointer !=
NULL) {
610 DEBUG ((DEBUG_VERBOSE,
" Image - %a\n", PdbPointer));
613 RequiredAlignment = RUNTIME_PAGE_ALLOCATION_GRANULARITY;
615 RuntimeImage->ImageBase,
616 RuntimeImage->ImageSize,
621 if (EFI_ERROR (Status)) {
622 if (Status == EFI_ABORTED) {
623 mMemoryAttributesTableEnable =
FALSE;
626 Status = EFI_ABORTED;
630 if (ImageRecord->CodeSegmentCount == 0) {
631 mMemoryAttributesTableEnable =
FALSE;
632 DEBUG ((DEBUG_ERROR,
"!!!!!!!! InsertImageRecord - CodeSegmentCount is 0 !!!!!!!!\n"));
633 if (PdbPointer !=
NULL) {
634 DEBUG ((DEBUG_ERROR,
"!!!!!!!! Image - %a !!!!!!!!\n", PdbPointer));
637 Status = EFI_ABORTED;
645 DEBUG ((DEBUG_ERROR,
"IsImageRecordCodeSectionValid - FAIL\n"));
646 Status = EFI_ABORTED;
650 InsertTailList (&mImagePropertiesPrivateData.ImageRecordList, &ImageRecord->Link);
651 mImagePropertiesPrivateData.ImageRecordCount++;
653 if (mImagePropertiesPrivateData.CodeSegmentCountMax < ImageRecord->CodeSegmentCount) {
654 mImagePropertiesPrivateData.CodeSegmentCountMax = ImageRecord->CodeSegmentCount;
660 if (EFI_ERROR (Status) && (ImageRecord !=
NULL)) {
679 DEBUG ((DEBUG_VERBOSE,
"RemoveImageRecord - 0x%x\n", RuntimeImage));
680 DEBUG ((DEBUG_VERBOSE,
"RemoveImageRecord - 0x%016lx - 0x%016lx\n", (
EFI_PHYSICAL_ADDRESS)(
UINTN)RuntimeImage->ImageBase, RuntimeImage->ImageSize));
683 if (ImageRecord ==
NULL) {
684 DEBUG ((DEBUG_ERROR,
"!!!!!!!! ImageRecord not found !!!!!!!!\n"));
690 mImagePropertiesPrivateData.ImageRecordCount--;
LIST_ENTRY *EFIAPI InitializeListHead(IN OUT LIST_ENTRY *ListHead)
#define INITIALIZE_LIST_HEAD_VARIABLE(ListHead)
UINT64 EFIAPI LShiftU64(IN UINT64 Operand, IN UINTN Count)
LIST_ENTRY *EFIAPI InsertTailList(IN OUT LIST_ENTRY *ListHead, IN OUT LIST_ENTRY *Entry)
VOID *EFIAPI CopyMem(OUT VOID *DestinationBuffer, IN CONST VOID *SourceBuffer, IN UINTN Length)
VOID MergeGuardPages(IN EFI_MEMORY_DESCRIPTOR *MemoryMapEntry, IN EFI_PHYSICAL_ADDRESS MaxAddress)
EFI_STATUS EFIAPI CoreCreateEventInternal(IN UINT32 Type, IN EFI_TPL NotifyTpl, IN EFI_EVENT_NOTIFY NotifyFunction OPTIONAL, IN CONST VOID *NotifyContext OPTIONAL, IN CONST EFI_GUID *EventGroup OPTIONAL, OUT EFI_EVENT *Event)
VOID CoreAcquireLock(IN EFI_LOCK *Lock)
VOID CoreReleaseLock(IN EFI_LOCK *Lock)
EFI_STATUS EFIAPI CoreGetMemoryMap(IN OUT UINTN *MemoryMapSize, IN OUT EFI_MEMORY_DESCRIPTOR *MemoryMap, OUT UINTN *MapKey, OUT UINTN *DescriptorSize, OUT UINT32 *DescriptorVersion)
VOID EFIAPI FreePool(IN VOID *Buffer)
EFI_STATUS EFIAPI SplitTable(IN OUT UINTN *MemoryMapSize, IN OUT EFI_MEMORY_DESCRIPTOR *MemoryMap, IN UINTN DescriptorSize, IN LIST_ENTRY *ImageRecordList, IN UINTN NumberOfAdditionalDescriptors)
VOID EFIAPI DeleteImagePropertiesRecord(IN IMAGE_PROPERTIES_RECORD *ImageRecord)
BOOLEAN EFIAPI IsImageRecordCodeSectionValid(IN IMAGE_PROPERTIES_RECORD *ImageRecord)
IMAGE_PROPERTIES_RECORD *EFIAPI FindImageRecord(IN EFI_PHYSICAL_ADDRESS ImageBase, IN UINT64 ImageSize, IN LIST_ENTRY *ImageRecordList)
EFI_STATUS EFIAPI CreateImagePropertiesRecord(IN CONST VOID *ImageBase, IN CONST UINT64 ImageSize, IN CONST UINT32 *Alignment OPTIONAL, OUT IMAGE_PROPERTIES_RECORD *ImageRecord)
VOID EFIAPI DumpImageRecords(IN LIST_ENTRY *ImageRecordList)
EFI_STATUS EFIAPI SortImageRecord(IN LIST_ENTRY *ImageRecordList)
VOID InsertImageRecord(IN EFI_RUNTIME_IMAGE_ENTRY *RuntimeImage)
VOID EFIAPI CoreInitializeMemoryAttributesTable(VOID)
EFI_STATUS EFIAPI CoreGetMemoryMapWithSeparatedImageSection(IN OUT UINTN *MemoryMapSize, IN OUT EFI_MEMORY_DESCRIPTOR *MemoryMap, OUT UINTN *MapKey, OUT UINTN *DescriptorSize, OUT UINT32 *DescriptorVersion)
VOID MergeMemoryMap(IN OUT EFI_MEMORY_DESCRIPTOR *MemoryMap, IN OUT UINTN *MemoryMapSize, IN UINTN DescriptorSize)
VOID EFIAPI InstallMemoryAttributesTableOnEndOfDxe(IN EFI_EVENT Event, IN VOID *Context)
STATIC VOID CoreAcquiremMemoryAttributesTableLock(VOID)
STATIC VOID CoreReleasemMemoryAttributesTableLock(VOID)
STATIC VOID EnforceMemoryMapAttribute(IN OUT EFI_MEMORY_DESCRIPTOR *MemoryMap, IN UINTN MemoryMapSize, IN UINTN DescriptorSize)
VOID EFIAPI InstallMemoryAttributesTableOnReadyToBoot(IN EFI_EVENT Event, IN VOID *Context)
VOID InstallMemoryAttributesTable(VOID)
VOID InstallMemoryAttributesTableOnMemoryAllocation(IN EFI_MEMORY_TYPE MemoryType)
VOID RemoveImageRecord(IN EFI_RUNTIME_IMAGE_ENTRY *RuntimeImage)
#define ASSERT_EFI_ERROR(StatusParameter)
#define DEBUG_CODE_BEGIN()
#define DEBUG(Expression)
VOID *EFIAPI AllocatePool(IN UINTN AllocationSize)
UINT64 EFI_PHYSICAL_ADDRESS
#define EFI_INITIALIZE_LOCK_VARIABLE(Priority)
EFI_VIRTUAL_ADDRESS VirtualStart
EFI_PHYSICAL_ADDRESS PhysicalStart