2 UEFI MemoryAttributesTable support
4 Copyright (c) 2016, Intel Corporation. All rights reserved.<BR>
5 This program and the accompanying materials
6 are licensed and made available under the terms and conditions of the BSD License
7 which accompanies this distribution. The full text of the license may be found at
8 http://opensource.org/licenses/bsd-license.php
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
16 #include <Library/BaseLib.h>
17 #include <Library/BaseMemoryLib.h>
18 #include <Library/MemoryAllocationLib.h>
19 #include <Library/UefiBootServicesTableLib.h>
20 #include <Library/DxeServicesTableLib.h>
21 #include <Library/DebugLib.h>
22 #include <Library/UefiLib.h>
24 #include <Guid/EventGroup.h>
26 #include <Guid/MemoryAttributesTable.h>
27 #include <Guid/PropertiesTable.h>
32 This function for GetMemoryMap() with properties table capability.
34 It calls original GetMemoryMap() to get the original memory map information. Then
35 plus the additional memory map entries for PE Code/Data seperation.
37 @param MemoryMapSize A pointer to the size, in bytes, of the
38 MemoryMap buffer. On input, this is the size of
39 the buffer allocated by the caller. On output,
40 it is the size of the buffer returned by the
41 firmware if the buffer was large enough, or the
42 size of the buffer needed to contain the map if
43 the buffer was too small.
44 @param MemoryMap A pointer to the buffer in which firmware places
45 the current memory map.
46 @param MapKey A pointer to the location in which firmware
47 returns the key for the current memory map.
48 @param DescriptorSize A pointer to the location in which firmware
49 returns the size, in bytes, of an individual
50 EFI_MEMORY_DESCRIPTOR.
51 @param DescriptorVersion A pointer to the location in which firmware
52 returns the version number associated with the
53 EFI_MEMORY_DESCRIPTOR.
55 @retval EFI_SUCCESS The memory map was returned in the MemoryMap
57 @retval EFI_BUFFER_TOO_SMALL The MemoryMap buffer was too small. The current
58 buffer size needed to hold the memory map is
59 returned in MemoryMapSize.
60 @retval EFI_INVALID_PARAMETER One of the parameters has an invalid value.
65 CoreGetMemoryMapPropertiesTable (
66 IN OUT UINTN
*MemoryMapSize
,
67 IN OUT EFI_MEMORY_DESCRIPTOR
*MemoryMap
,
69 OUT UINTN
*DescriptorSize
,
70 OUT UINT32
*DescriptorVersion
73 extern EFI_PROPERTIES_TABLE mPropertiesTable
;
76 Install MemoryAttributesTable.
78 @param[in] Event The Event this notify function registered to.
79 @param[in] Context Pointer to the context data registered to the Event.
83 InstallMemoryAttributesTable (
89 EFI_MEMORY_DESCRIPTOR
*MemoryMap
;
90 EFI_MEMORY_DESCRIPTOR
*MemoryMapStart
;
93 UINT32 DescriptorVersion
;
96 UINT32 RuntimeEntryCount
;
97 EFI_MEMORY_ATTRIBUTES_TABLE
*MemoryAttributesTable
;
98 EFI_MEMORY_DESCRIPTOR
*MemoryAttributesEntry
;
100 if ((mPropertiesTable
.MemoryProtectionAttribute
& EFI_PROPERTIES_RUNTIME_MEMORY_PROTECTION_NON_EXECUTABLE_PE_DATA
) == 0) {
101 DEBUG ((EFI_D_VERBOSE
, "MemoryProtectionAttribute NON_EXECUTABLE_PE_DATA is not set, "));
102 DEBUG ((EFI_D_VERBOSE
, "because Runtime Driver Section Alignment is not %dK.\n", EFI_ACPI_RUNTIME_PAGE_ALLOCATION_ALIGNMENT
>> 10));
108 Status
= CoreGetMemoryMapPropertiesTable (
115 ASSERT (Status
== EFI_BUFFER_TOO_SMALL
);
118 MemoryMap
= AllocatePool (MemoryMapSize
);
119 ASSERT (MemoryMap
!= NULL
);
121 Status
= CoreGetMemoryMapPropertiesTable (
128 if (EFI_ERROR (Status
)) {
129 FreePool (MemoryMap
);
131 } while (Status
== EFI_BUFFER_TOO_SMALL
);
133 MemoryMapStart
= MemoryMap
;
134 RuntimeEntryCount
= 0;
135 for (Index
= 0; Index
< MemoryMapSize
/DescriptorSize
; Index
++) {
136 switch (MemoryMap
->Type
) {
137 case EfiRuntimeServicesCode
:
138 case EfiRuntimeServicesData
:
139 RuntimeEntryCount
++;
142 MemoryMap
= NEXT_MEMORY_DESCRIPTOR(MemoryMap
, DescriptorSize
);
146 // Allocate MemoryAttributesTable
148 MemoryAttributesTable
= AllocatePool (sizeof(EFI_MEMORY_ATTRIBUTES_TABLE
) + DescriptorSize
* RuntimeEntryCount
);
149 ASSERT (MemoryAttributesTable
!= NULL
);
150 MemoryAttributesTable
->Version
= EFI_MEMORY_ATTRIBUTES_TABLE_VERSION
;
151 MemoryAttributesTable
->NumberOfEntries
= RuntimeEntryCount
;
152 MemoryAttributesTable
->DescriptorSize
= (UINT32
)DescriptorSize
;
153 MemoryAttributesTable
->Reserved
= 0;
154 DEBUG ((EFI_D_VERBOSE
, "MemoryAttributesTable:\n"));
155 DEBUG ((EFI_D_VERBOSE
, " Version - 0x%08x\n", MemoryAttributesTable
->Version
));
156 DEBUG ((EFI_D_VERBOSE
, " NumberOfEntries - 0x%08x\n", MemoryAttributesTable
->NumberOfEntries
));
157 DEBUG ((EFI_D_VERBOSE
, " DescriptorSize - 0x%08x\n", MemoryAttributesTable
->DescriptorSize
));
158 MemoryAttributesEntry
= (EFI_MEMORY_DESCRIPTOR
*)(MemoryAttributesTable
+ 1);
159 MemoryMap
= MemoryMapStart
;
160 for (Index
= 0; Index
< MemoryMapSize
/DescriptorSize
; Index
++) {
161 switch (MemoryMap
->Type
) {
162 case EfiRuntimeServicesCode
:
163 case EfiRuntimeServicesData
:
164 CopyMem (MemoryAttributesEntry
, MemoryMap
, DescriptorSize
);
165 MemoryAttributesEntry
->Attribute
&= (EFI_MEMORY_RO
|EFI_MEMORY_XP
|EFI_MEMORY_RUNTIME
);
166 DEBUG ((EFI_D_VERBOSE
, "Entry (0x%x)\n", MemoryAttributesEntry
));
167 DEBUG ((EFI_D_VERBOSE
, " Type - 0x%x\n", MemoryAttributesEntry
->Type
));
168 DEBUG ((EFI_D_VERBOSE
, " PhysicalStart - 0x%016lx\n", MemoryAttributesEntry
->PhysicalStart
));
169 DEBUG ((EFI_D_VERBOSE
, " VirtualStart - 0x%016lx\n", MemoryAttributesEntry
->VirtualStart
));
170 DEBUG ((EFI_D_VERBOSE
, " NumberOfPages - 0x%016lx\n", MemoryAttributesEntry
->NumberOfPages
));
171 DEBUG ((EFI_D_VERBOSE
, " Attribute - 0x%016lx\n", MemoryAttributesEntry
->Attribute
));
172 MemoryAttributesEntry
= NEXT_MEMORY_DESCRIPTOR(MemoryAttributesEntry
, DescriptorSize
);
175 MemoryMap
= NEXT_MEMORY_DESCRIPTOR(MemoryMap
, DescriptorSize
);
177 MemoryMap
= MemoryMapStart
;
178 FreePool (MemoryMap
);
180 Status
= gBS
->InstallConfigurationTable (&gEfiMemoryAttributesTableGuid
, MemoryAttributesTable
);
181 ASSERT_EFI_ERROR (Status
);
185 Initialize MemoryAttrubutesTable support.
189 CoreInitializeMemoryAttributesTable (
194 EFI_EVENT ReadyToBootEvent
;
197 // Construct the table at ReadyToBoot, because this should be
198 // last point to allocate RuntimeCode/RuntimeData.
200 Status
= gBS
->CreateEventEx (
203 InstallMemoryAttributesTable
,
205 &gEfiEventReadyToBootGuid
,
208 ASSERT_EFI_ERROR (Status
);