]> git.proxmox.com Git - mirror_edk2.git/blob - MdeModulePkg/Core/Dxe/Misc/MemoryAttributesTable.c
MdeModulePkg: Fix Memory Attributes table type issue
[mirror_edk2.git] / MdeModulePkg / Core / Dxe / Misc / MemoryAttributesTable.c
1 /** @file
2 UEFI MemoryAttributesTable support
3
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
9
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.
12
13 **/
14
15 #include <PiDxe.h>
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>
23
24 #include <Guid/EventGroup.h>
25
26 #include <Guid/MemoryAttributesTable.h>
27 #include <Guid/PropertiesTable.h>
28
29 #include "DxeMain.h"
30
31 /**
32 This function for GetMemoryMap() with properties table capability.
33
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.
36
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.
54
55 @retval EFI_SUCCESS The memory map was returned in the MemoryMap
56 buffer.
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.
61
62 **/
63 EFI_STATUS
64 EFIAPI
65 CoreGetMemoryMapPropertiesTable (
66 IN OUT UINTN *MemoryMapSize,
67 IN OUT EFI_MEMORY_DESCRIPTOR *MemoryMap,
68 OUT UINTN *MapKey,
69 OUT UINTN *DescriptorSize,
70 OUT UINT32 *DescriptorVersion
71 );
72
73 extern EFI_PROPERTIES_TABLE mPropertiesTable;
74
75 /**
76 Install MemoryAttributesTable.
77
78 @param[in] Event The Event this notify function registered to.
79 @param[in] Context Pointer to the context data registered to the Event.
80 **/
81 VOID
82 EFIAPI
83 InstallMemoryAttributesTable (
84 EFI_EVENT Event,
85 VOID *Context
86 )
87 {
88 UINTN MemoryMapSize;
89 EFI_MEMORY_DESCRIPTOR *MemoryMap;
90 EFI_MEMORY_DESCRIPTOR *MemoryMapStart;
91 UINTN MapKey;
92 UINTN DescriptorSize;
93 UINT32 DescriptorVersion;
94 UINTN Index;
95 EFI_STATUS Status;
96 UINT32 RuntimeEntryCount;
97 EFI_MEMORY_ATTRIBUTES_TABLE *MemoryAttributesTable;
98 EFI_MEMORY_DESCRIPTOR *MemoryAttributesEntry;
99
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));
103 return ;
104 }
105
106 MemoryMapSize = 0;
107 MemoryMap = NULL;
108 Status = CoreGetMemoryMapPropertiesTable (
109 &MemoryMapSize,
110 MemoryMap,
111 &MapKey,
112 &DescriptorSize,
113 &DescriptorVersion
114 );
115 ASSERT (Status == EFI_BUFFER_TOO_SMALL);
116
117 do {
118 MemoryMap = AllocatePool (MemoryMapSize);
119 ASSERT (MemoryMap != NULL);
120
121 Status = CoreGetMemoryMapPropertiesTable (
122 &MemoryMapSize,
123 MemoryMap,
124 &MapKey,
125 &DescriptorSize,
126 &DescriptorVersion
127 );
128 if (EFI_ERROR (Status)) {
129 FreePool (MemoryMap);
130 }
131 } while (Status == EFI_BUFFER_TOO_SMALL);
132
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 ++;
140 break;
141 }
142 MemoryMap = NEXT_MEMORY_DESCRIPTOR(MemoryMap, DescriptorSize);
143 }
144
145 //
146 // Allocate MemoryAttributesTable
147 //
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);
173 break;
174 }
175 MemoryMap = NEXT_MEMORY_DESCRIPTOR(MemoryMap, DescriptorSize);
176 }
177
178 Status = gBS->InstallConfigurationTable (&gEfiMemoryAttributesTableGuid, MemoryAttributesTable);
179 ASSERT_EFI_ERROR (Status);
180 }
181
182 /**
183 Initialize MemoryAttrubutesTable support.
184 **/
185 VOID
186 EFIAPI
187 CoreInitializeMemoryAttributesTable (
188 VOID
189 )
190 {
191 EFI_STATUS Status;
192 EFI_EVENT ReadyToBootEvent;
193
194 //
195 // Construct the table at ReadyToBoot, because this should be
196 // last point to allocate RuntimeCode/RuntimeData.
197 //
198 Status = gBS->CreateEventEx (
199 EVT_NOTIFY_SIGNAL,
200 TPL_NOTIFY,
201 InstallMemoryAttributesTable,
202 NULL,
203 &gEfiEventReadyToBootGuid,
204 &ReadyToBootEvent
205 );
206 ASSERT_EFI_ERROR (Status);
207 return ;
208 }