Commit | Line | Data |
---|---|---|
504214c4 | 1 | /** @file\r |
504214c4 LG |
2 | UEFI Memory page management functions.\r |
3 | \r | |
9a701955 | 4 | Copyright (c) 2007 - 2017, Intel Corporation. All rights reserved.<BR>\r |
cd5ebaa0 | 5 | This program and the accompanying materials\r |
23c98c94 | 6 | are licensed and made available under the terms and conditions of the BSD License\r |
7 | which accompanies this distribution. The full text of the license may be found at\r | |
8 | http://opensource.org/licenses/bsd-license.php\r | |
9 | \r | |
10 | THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r | |
11 | WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r | |
28a00297 | 12 | \r |
504214c4 | 13 | **/\r |
28a00297 | 14 | \r |
9c4ac31c | 15 | #include "DxeMain.h"\r |
ec90508b | 16 | #include "Imem.h"\r |
28a00297 | 17 | \r |
28a00297 | 18 | //\r |
d45fd260 | 19 | // Entry for tracking the memory regions for each memory type to coalesce similar memory types\r |
28a00297 | 20 | //\r |
21 | typedef struct {\r | |
22 | EFI_PHYSICAL_ADDRESS BaseAddress;\r | |
23 | EFI_PHYSICAL_ADDRESS MaximumAddress;\r | |
24 | UINT64 CurrentNumberOfPages;\r | |
b74350e9 | 25 | UINT64 NumberOfPages;\r |
28a00297 | 26 | UINTN InformationIndex;\r |
b74350e9 | 27 | BOOLEAN Special;\r |
28 | BOOLEAN Runtime;\r | |
d613c2a8 | 29 | } EFI_MEMORY_TYPE_STATISTICS;\r |
28a00297 | 30 | \r |
31 | //\r | |
32 | // MemoryMap - The current memory map\r | |
33 | //\r | |
34 | UINTN mMemoryMapKey = 0;\r | |
35 | \r | |
28a00297 | 36 | #define MAX_MAP_DEPTH 6\r |
dc8d93ca | 37 | \r |
38 | ///\r | |
39 | /// mMapDepth - depth of new descriptor stack\r | |
40 | ///\r | |
28a00297 | 41 | UINTN mMapDepth = 0;\r |
dc8d93ca | 42 | ///\r |
43 | /// mMapStack - space to use as temp storage to build new map descriptors\r | |
44 | ///\r | |
28a00297 | 45 | MEMORY_MAP mMapStack[MAX_MAP_DEPTH];\r |
46 | UINTN mFreeMapStack = 0;\r | |
dc8d93ca | 47 | ///\r |
48 | /// This list maintain the free memory map list\r | |
49 | ///\r | |
e94a9ff7 | 50 | LIST_ENTRY mFreeMemoryMapEntryList = INITIALIZE_LIST_HEAD_VARIABLE (mFreeMemoryMapEntryList);\r |
51 | BOOLEAN mMemoryTypeInformationInitialized = FALSE;\r | |
28a00297 | 52 | \r |
d613c2a8 | 53 | EFI_MEMORY_TYPE_STATISTICS mMemoryTypeStatistics[EfiMaxMemoryType + 1] = {\r |
f3f2e05d | 54 | { 0, MAX_ADDRESS, 0, 0, EfiMaxMemoryType, TRUE, FALSE }, // EfiReservedMemoryType\r |
55 | { 0, MAX_ADDRESS, 0, 0, EfiMaxMemoryType, FALSE, FALSE }, // EfiLoaderCode\r | |
56 | { 0, MAX_ADDRESS, 0, 0, EfiMaxMemoryType, FALSE, FALSE }, // EfiLoaderData\r | |
57 | { 0, MAX_ADDRESS, 0, 0, EfiMaxMemoryType, FALSE, FALSE }, // EfiBootServicesCode\r | |
58 | { 0, MAX_ADDRESS, 0, 0, EfiMaxMemoryType, FALSE, FALSE }, // EfiBootServicesData\r | |
59 | { 0, MAX_ADDRESS, 0, 0, EfiMaxMemoryType, TRUE, TRUE }, // EfiRuntimeServicesCode\r | |
60 | { 0, MAX_ADDRESS, 0, 0, EfiMaxMemoryType, TRUE, TRUE }, // EfiRuntimeServicesData\r | |
61 | { 0, MAX_ADDRESS, 0, 0, EfiMaxMemoryType, FALSE, FALSE }, // EfiConventionalMemory\r | |
62 | { 0, MAX_ADDRESS, 0, 0, EfiMaxMemoryType, FALSE, FALSE }, // EfiUnusableMemory\r | |
63 | { 0, MAX_ADDRESS, 0, 0, EfiMaxMemoryType, TRUE, FALSE }, // EfiACPIReclaimMemory\r | |
64 | { 0, MAX_ADDRESS, 0, 0, EfiMaxMemoryType, TRUE, FALSE }, // EfiACPIMemoryNVS\r | |
65 | { 0, MAX_ADDRESS, 0, 0, EfiMaxMemoryType, FALSE, FALSE }, // EfiMemoryMappedIO\r | |
66 | { 0, MAX_ADDRESS, 0, 0, EfiMaxMemoryType, FALSE, FALSE }, // EfiMemoryMappedIOPortSpace\r | |
67 | { 0, MAX_ADDRESS, 0, 0, EfiMaxMemoryType, TRUE, TRUE }, // EfiPalCode\r | |
a671a012 | 68 | { 0, MAX_ADDRESS, 0, 0, EfiMaxMemoryType, FALSE, FALSE }, // EfiPersistentMemory\r |
f3f2e05d | 69 | { 0, MAX_ADDRESS, 0, 0, EfiMaxMemoryType, FALSE, FALSE } // EfiMaxMemoryType\r |
28a00297 | 70 | };\r |
71 | \r | |
f3f2e05d | 72 | EFI_PHYSICAL_ADDRESS mDefaultMaximumAddress = MAX_ADDRESS;\r |
2345e7d4 | 73 | EFI_PHYSICAL_ADDRESS mDefaultBaseAddress = MAX_ADDRESS;\r |
28a00297 | 74 | \r |
75 | EFI_MEMORY_TYPE_INFORMATION gMemoryTypeInformation[EfiMaxMemoryType + 1] = {\r | |
76 | { EfiReservedMemoryType, 0 },\r | |
77 | { EfiLoaderCode, 0 },\r | |
78 | { EfiLoaderData, 0 },\r | |
79 | { EfiBootServicesCode, 0 },\r | |
80 | { EfiBootServicesData, 0 },\r | |
81 | { EfiRuntimeServicesCode, 0 },\r | |
82 | { EfiRuntimeServicesData, 0 },\r | |
83 | { EfiConventionalMemory, 0 },\r | |
84 | { EfiUnusableMemory, 0 },\r | |
85 | { EfiACPIReclaimMemory, 0 },\r | |
86 | { EfiACPIMemoryNVS, 0 },\r | |
87 | { EfiMemoryMappedIO, 0 },\r | |
88 | { EfiMemoryMappedIOPortSpace, 0 },\r | |
89 | { EfiPalCode, 0 },\r | |
a671a012 | 90 | { EfiPersistentMemory, 0 },\r |
28a00297 | 91 | { EfiMaxMemoryType, 0 }\r |
92 | };\r | |
54ea99a7 | 93 | //\r |
94 | // Only used when load module at fixed address feature is enabled. True means the memory is alreay successfully allocated\r | |
95 | // and ready to load the module in to specified address.or else, the memory is not ready and module will be loaded at a \r | |
96 | // address assigned by DXE core.\r | |
97 | //\r | |
98 | GLOBAL_REMOVE_IF_UNREFERENCED BOOLEAN gLoadFixedAddressCodeMemoryReady = FALSE;\r | |
d9177625 | 99 | \r |
162ed594 | 100 | /**\r |
d9177625 | 101 | Enter critical section by gaining lock on gMemoryLock.\r |
162ed594 | 102 | \r |
103 | **/\r | |
23c98c94 | 104 | VOID\r |
d9177625 | 105 | CoreAcquireMemoryLock (\r |
106 | VOID\r | |
107 | )\r | |
108 | {\r | |
109 | CoreAcquireLock (&gMemoryLock);\r | |
110 | }\r | |
111 | \r | |
112 | \r | |
113 | \r | |
114 | /**\r | |
115 | Exit critical section by releasing lock on gMemoryLock.\r | |
116 | \r | |
117 | **/\r | |
118 | VOID\r | |
119 | CoreReleaseMemoryLock (\r | |
28a00297 | 120 | VOID\r |
d9177625 | 121 | )\r |
122 | {\r | |
123 | CoreReleaseLock (&gMemoryLock);\r | |
124 | }\r | |
125 | \r | |
126 | \r | |
127 | \r | |
128 | \r | |
129 | /**\r | |
130 | Internal function. Removes a descriptor entry.\r | |
131 | \r | |
132 | @param Entry The entry to remove\r | |
133 | \r | |
134 | **/\r | |
135 | VOID\r | |
136 | RemoveMemoryMapEntry (\r | |
137 | IN OUT MEMORY_MAP *Entry\r | |
138 | )\r | |
139 | {\r | |
140 | RemoveEntryList (&Entry->Link);\r | |
141 | Entry->Link.ForwardLink = NULL;\r | |
142 | \r | |
143 | if (Entry->FromPages) {\r | |
144 | //\r | |
145 | // Insert the free memory map descriptor to the end of mFreeMemoryMapEntryList\r | |
146 | //\r | |
147 | InsertTailList (&mFreeMemoryMapEntryList, &Entry->Link);\r | |
148 | }\r | |
149 | }\r | |
162ed594 | 150 | \r |
151 | /**\r | |
152 | Internal function. Adds a ranges to the memory map.\r | |
153 | The range must not already exist in the map.\r | |
154 | \r | |
022c6d45 | 155 | @param Type The type of memory range to add\r |
156 | @param Start The starting address in the memory range Must be\r | |
157 | paged aligned\r | |
158 | @param End The last address in the range Must be the last\r | |
159 | byte of a page\r | |
160 | @param Attribute The attributes of the memory range to add\r | |
28a00297 | 161 | \r |
162ed594 | 162 | **/\r |
28a00297 | 163 | VOID\r |
164 | CoreAddRange (\r | |
165 | IN EFI_MEMORY_TYPE Type,\r | |
166 | IN EFI_PHYSICAL_ADDRESS Start,\r | |
167 | IN EFI_PHYSICAL_ADDRESS End,\r | |
168 | IN UINT64 Attribute\r | |
d9177625 | 169 | )\r |
170 | {\r | |
171 | LIST_ENTRY *Link;\r | |
172 | MEMORY_MAP *Entry;\r | |
28a00297 | 173 | \r |
d9177625 | 174 | ASSERT ((Start & EFI_PAGE_MASK) == 0);\r |
175 | ASSERT (End > Start) ;\r | |
162ed594 | 176 | \r |
d9177625 | 177 | ASSERT_LOCKED (&gMemoryLock);\r |
28a00297 | 178 | \r |
d9177625 | 179 | DEBUG ((DEBUG_PAGE, "AddRange: %lx-%lx to %d\n", Start, End, Type));\r |
d436d5ca | 180 | \r |
181 | //\r | |
182 | // If memory of type EfiConventionalMemory is being added that includes the page \r | |
183 | // starting at address 0, then zero the page starting at address 0. This has \r | |
184 | // two benifits. It helps find NULL pointer bugs and it also maximizes \r | |
185 | // compatibility with operating systems that may evaluate memory in this page \r | |
186 | // for legacy data structures. If memory of any other type is added starting \r | |
187 | // at address 0, then do not zero the page at address 0 because the page is being \r | |
188 | // used for other purposes.\r | |
189 | // \r | |
190 | if (Type == EfiConventionalMemory && Start == 0 && (End >= EFI_PAGE_SIZE - 1)) {\r | |
191 | SetMem ((VOID *)(UINTN)Start, EFI_PAGE_SIZE, 0);\r | |
192 | }\r | |
193 | \r | |
d9177625 | 194 | //\r |
195 | // Memory map being altered so updated key\r | |
196 | //\r | |
197 | mMemoryMapKey += 1;\r | |
162ed594 | 198 | \r |
d9177625 | 199 | //\r |
200 | // UEFI 2.0 added an event group for notificaiton on memory map changes.\r | |
201 | // So we need to signal this Event Group every time the memory map changes.\r | |
202 | // If we are in EFI 1.10 compatability mode no event groups will be\r | |
203 | // found and nothing will happen we we call this function. These events\r | |
204 | // will get signaled but since a lock is held around the call to this\r | |
6393d9c8 | 205 | // function the notificaiton events will only be called after this function\r |
d9177625 | 206 | // returns and the lock is released.\r |
207 | //\r | |
208 | CoreNotifySignalList (&gEfiEventMemoryMapChangeGuid);\r | |
162ed594 | 209 | \r |
d9177625 | 210 | //\r |
211 | // Look for adjoining memory descriptor\r | |
212 | //\r | |
28a00297 | 213 | \r |
d9177625 | 214 | // Two memory descriptors can only be merged if they have the same Type\r |
215 | // and the same Attribute\r | |
216 | //\r | |
162ed594 | 217 | \r |
d9177625 | 218 | Link = gMemoryMap.ForwardLink;\r |
219 | while (Link != &gMemoryMap) {\r | |
220 | Entry = CR (Link, MEMORY_MAP, Link, MEMORY_MAP_SIGNATURE);\r | |
221 | Link = Link->ForwardLink;\r | |
162ed594 | 222 | \r |
d9177625 | 223 | if (Entry->Type != Type) {\r |
224 | continue;\r | |
225 | }\r | |
226 | \r | |
227 | if (Entry->Attribute != Attribute) {\r | |
228 | continue;\r | |
229 | }\r | |
230 | \r | |
231 | if (Entry->End + 1 == Start) {\r | |
232 | \r | |
233 | Start = Entry->Start;\r | |
234 | RemoveMemoryMapEntry (Entry);\r | |
235 | \r | |
236 | } else if (Entry->Start == End + 1) {\r | |
237 | \r | |
238 | End = Entry->End;\r | |
239 | RemoveMemoryMapEntry (Entry);\r | |
240 | }\r | |
241 | }\r | |
242 | \r | |
243 | //\r | |
244 | // Add descriptor\r | |
245 | //\r | |
246 | \r | |
247 | mMapStack[mMapDepth].Signature = MEMORY_MAP_SIGNATURE;\r | |
248 | mMapStack[mMapDepth].FromPages = FALSE;\r | |
249 | mMapStack[mMapDepth].Type = Type;\r | |
250 | mMapStack[mMapDepth].Start = Start;\r | |
251 | mMapStack[mMapDepth].End = End;\r | |
252 | mMapStack[mMapDepth].VirtualStart = 0;\r | |
253 | mMapStack[mMapDepth].Attribute = Attribute;\r | |
254 | InsertTailList (&gMemoryMap, &mMapStack[mMapDepth].Link);\r | |
255 | \r | |
256 | mMapDepth += 1;\r | |
257 | ASSERT (mMapDepth < MAX_MAP_DEPTH);\r | |
258 | \r | |
259 | return ;\r | |
260 | }\r | |
022c6d45 | 261 | \r |
162ed594 | 262 | /**\r |
263 | Internal function. Deque a descriptor entry from the mFreeMemoryMapEntryList.\r | |
264 | If the list is emtry, then allocate a new page to refuel the list.\r | |
265 | Please Note this algorithm to allocate the memory map descriptor has a property\r | |
266 | that the memory allocated for memory entries always grows, and will never really be freed\r | |
267 | For example, if the current boot uses 2000 memory map entries at the maximum point, but\r | |
268 | ends up with only 50 at the time the OS is booted, then the memory associated with the 1950\r | |
269 | memory map entries is still allocated from EfiBootServicesMemory.\r | |
270 | \r | |
271 | \r | |
272 | @return The Memory map descriptor dequed from the mFreeMemoryMapEntryList\r | |
273 | \r | |
274 | **/\r | |
28a00297 | 275 | MEMORY_MAP *\r |
276 | AllocateMemoryMapEntry (\r | |
277 | VOID\r | |
d9177625 | 278 | )\r |
279 | {\r | |
280 | MEMORY_MAP* FreeDescriptorEntries;\r | |
281 | MEMORY_MAP* Entry;\r | |
282 | UINTN Index;\r | |
283 | \r | |
284 | if (IsListEmpty (&mFreeMemoryMapEntryList)) {\r | |
285 | //\r | |
286 | // The list is empty, to allocate one page to refuel the list\r | |
287 | //\r | |
d4731a98 AB |
288 | FreeDescriptorEntries = CoreAllocatePoolPages (EfiBootServicesData,\r |
289 | EFI_SIZE_TO_PAGES (DEFAULT_PAGE_ALLOCATION_GRANULARITY),\r | |
290 | DEFAULT_PAGE_ALLOCATION_GRANULARITY);\r | |
291 | if (FreeDescriptorEntries != NULL) {\r | |
d9177625 | 292 | //\r |
293 | // Enque the free memmory map entries into the list\r | |
294 | //\r | |
d4731a98 | 295 | for (Index = 0; Index < DEFAULT_PAGE_ALLOCATION_GRANULARITY / sizeof(MEMORY_MAP); Index++) {\r |
d9177625 | 296 | FreeDescriptorEntries[Index].Signature = MEMORY_MAP_SIGNATURE;\r |
297 | InsertTailList (&mFreeMemoryMapEntryList, &FreeDescriptorEntries[Index].Link);\r | |
298 | }\r | |
299 | } else {\r | |
300 | return NULL;\r | |
301 | }\r | |
302 | }\r | |
303 | //\r | |
304 | // dequeue the first descriptor from the list\r | |
305 | //\r | |
306 | Entry = CR (mFreeMemoryMapEntryList.ForwardLink, MEMORY_MAP, Link, MEMORY_MAP_SIGNATURE);\r | |
307 | RemoveEntryList (&Entry->Link);\r | |
308 | \r | |
309 | return Entry;\r | |
310 | }\r | |
022c6d45 | 311 | \r |
162ed594 | 312 | \r |
313 | /**\r | |
d9177625 | 314 | Internal function. Moves any memory descriptors that are on the\r |
315 | temporary descriptor stack to heap.\r | |
162ed594 | 316 | \r |
317 | **/\r | |
28a00297 | 318 | VOID\r |
d9177625 | 319 | CoreFreeMemoryMapStack (\r |
28a00297 | 320 | VOID\r |
321 | )\r | |
28a00297 | 322 | {\r |
d9177625 | 323 | MEMORY_MAP *Entry;\r |
324 | MEMORY_MAP *Entry2;\r | |
325 | LIST_ENTRY *Link2;\r | |
28a00297 | 326 | \r |
d9177625 | 327 | ASSERT_LOCKED (&gMemoryLock);\r |
28a00297 | 328 | \r |
d9177625 | 329 | //\r |
330 | // If already freeing the map stack, then return\r | |
331 | //\r | |
332 | if (mFreeMapStack != 0) {\r | |
333 | return ;\r | |
334 | }\r | |
162ed594 | 335 | \r |
d9177625 | 336 | //\r |
337 | // Move the temporary memory descriptor stack into pool\r | |
338 | //\r | |
339 | mFreeMapStack += 1;\r | |
162ed594 | 340 | \r |
d9177625 | 341 | while (mMapDepth != 0) {\r |
342 | //\r | |
343 | // Deque an memory map entry from mFreeMemoryMapEntryList\r | |
344 | //\r | |
345 | Entry = AllocateMemoryMapEntry ();\r | |
346 | \r | |
347 | ASSERT (Entry);\r | |
348 | \r | |
349 | //\r | |
350 | // Update to proper entry\r | |
351 | //\r | |
352 | mMapDepth -= 1;\r | |
353 | \r | |
354 | if (mMapStack[mMapDepth].Link.ForwardLink != NULL) {\r | |
355 | \r | |
356 | //\r | |
357 | // Move this entry to general memory\r | |
358 | //\r | |
359 | RemoveEntryList (&mMapStack[mMapDepth].Link);\r | |
360 | mMapStack[mMapDepth].Link.ForwardLink = NULL;\r | |
361 | \r | |
362 | CopyMem (Entry , &mMapStack[mMapDepth], sizeof (MEMORY_MAP));\r | |
363 | Entry->FromPages = TRUE;\r | |
364 | \r | |
365 | //\r | |
366 | // Find insertion location\r | |
367 | //\r | |
368 | for (Link2 = gMemoryMap.ForwardLink; Link2 != &gMemoryMap; Link2 = Link2->ForwardLink) {\r | |
369 | Entry2 = CR (Link2, MEMORY_MAP, Link, MEMORY_MAP_SIGNATURE);\r | |
370 | if (Entry2->FromPages && Entry2->Start > Entry->Start) {\r | |
371 | break;\r | |
372 | }\r | |
373 | }\r | |
374 | \r | |
375 | InsertTailList (Link2, &Entry->Link);\r | |
376 | \r | |
377 | } else {\r | |
378 | //\r | |
379 | // This item of mMapStack[mMapDepth] has already been dequeued from gMemoryMap list,\r | |
380 | // so here no need to move it to memory.\r | |
381 | //\r | |
382 | InsertTailList (&mFreeMemoryMapEntryList, &Entry->Link);\r | |
383 | }\r | |
384 | }\r | |
28a00297 | 385 | \r |
d9177625 | 386 | mFreeMapStack -= 1;\r |
387 | }\r | |
162ed594 | 388 | \r |
389 | /**\r | |
390 | Find untested but initialized memory regions in GCD map and convert them to be DXE allocatable.\r | |
391 | \r | |
392 | **/\r | |
2345e7d4 | 393 | BOOLEAN\r |
28a00297 | 394 | PromoteMemoryResource (\r |
395 | VOID\r | |
396 | )\r | |
28a00297 | 397 | {\r |
2345e7d4 | 398 | LIST_ENTRY *Link;\r |
399 | EFI_GCD_MAP_ENTRY *Entry;\r | |
400 | BOOLEAN Promoted;\r | |
28a00297 | 401 | \r |
d45fd260 | 402 | DEBUG ((DEBUG_PAGE, "Promote the memory resource\n"));\r |
022c6d45 | 403 | \r |
28a00297 | 404 | CoreAcquireGcdMemoryLock ();\r |
022c6d45 | 405 | \r |
2345e7d4 | 406 | Promoted = FALSE;\r |
28a00297 | 407 | Link = mGcdMemorySpaceMap.ForwardLink;\r |
408 | while (Link != &mGcdMemorySpaceMap) {\r | |
409 | \r | |
410 | Entry = CR (Link, EFI_GCD_MAP_ENTRY, Link, EFI_GCD_MAP_SIGNATURE);\r | |
411 | \r | |
412 | if (Entry->GcdMemoryType == EfiGcdMemoryTypeReserved &&\r | |
f3f2e05d | 413 | Entry->EndAddress < MAX_ADDRESS &&\r |
28a00297 | 414 | (Entry->Capabilities & (EFI_MEMORY_PRESENT | EFI_MEMORY_INITIALIZED | EFI_MEMORY_TESTED)) ==\r |
415 | (EFI_MEMORY_PRESENT | EFI_MEMORY_INITIALIZED)) {\r | |
416 | //\r | |
417 | // Update the GCD map\r | |
418 | //\r | |
74705ca5 SZ |
419 | if ((Entry->Capabilities & EFI_MEMORY_MORE_RELIABLE) == EFI_MEMORY_MORE_RELIABLE) {\r |
420 | Entry->GcdMemoryType = EfiGcdMemoryTypeMoreReliable;\r | |
421 | } else {\r | |
422 | Entry->GcdMemoryType = EfiGcdMemoryTypeSystemMemory;\r | |
423 | }\r | |
28a00297 | 424 | Entry->Capabilities |= EFI_MEMORY_TESTED;\r |
425 | Entry->ImageHandle = gDxeCoreImageHandle;\r | |
426 | Entry->DeviceHandle = NULL;\r | |
427 | \r | |
428 | //\r | |
429 | // Add to allocable system memory resource\r | |
022c6d45 | 430 | //\r |
28a00297 | 431 | \r |
432 | CoreAddRange (\r | |
022c6d45 | 433 | EfiConventionalMemory,\r |
434 | Entry->BaseAddress,\r | |
435 | Entry->EndAddress,\r | |
28a00297 | 436 | Entry->Capabilities & ~(EFI_MEMORY_PRESENT | EFI_MEMORY_INITIALIZED | EFI_MEMORY_TESTED | EFI_MEMORY_RUNTIME)\r |
437 | );\r | |
438 | CoreFreeMemoryMapStack ();\r | |
022c6d45 | 439 | \r |
2345e7d4 | 440 | Promoted = TRUE;\r |
28a00297 | 441 | }\r |
442 | \r | |
443 | Link = Link->ForwardLink;\r | |
444 | }\r | |
022c6d45 | 445 | \r |
28a00297 | 446 | CoreReleaseGcdMemoryLock ();\r |
022c6d45 | 447 | \r |
2345e7d4 | 448 | return Promoted;\r |
28a00297 | 449 | }\r |
54ea99a7 | 450 | /**\r |
451 | This function try to allocate Runtime code & Boot time code memory range. If LMFA enabled, 2 patchable PCD \r | |
452 | PcdLoadFixAddressRuntimeCodePageNumber & PcdLoadFixAddressBootTimeCodePageNumber which are set by tools will record the \r | |
453 | size of boot time and runtime code.\r | |
28a00297 | 454 | \r |
54ea99a7 | 455 | **/\r |
456 | VOID\r | |
457 | CoreLoadingFixedAddressHook (\r | |
458 | VOID\r | |
459 | )\r | |
460 | {\r | |
461 | UINT32 RuntimeCodePageNumber;\r | |
462 | UINT32 BootTimeCodePageNumber;\r | |
463 | EFI_PHYSICAL_ADDRESS RuntimeCodeBase;\r | |
464 | EFI_PHYSICAL_ADDRESS BootTimeCodeBase;\r | |
465 | EFI_STATUS Status;\r | |
466 | \r | |
467 | //\r | |
468 | // Make sure these 2 areas are not initialzied.\r | |
469 | //\r | |
470 | if (!gLoadFixedAddressCodeMemoryReady) { \r | |
471 | RuntimeCodePageNumber = PcdGet32(PcdLoadFixAddressRuntimeCodePageNumber);\r | |
472 | BootTimeCodePageNumber= PcdGet32(PcdLoadFixAddressBootTimeCodePageNumber);\r | |
473 | RuntimeCodeBase = (EFI_PHYSICAL_ADDRESS)(gLoadModuleAtFixAddressConfigurationTable.DxeCodeTopAddress - EFI_PAGES_TO_SIZE (RuntimeCodePageNumber));\r | |
474 | BootTimeCodeBase = (EFI_PHYSICAL_ADDRESS)(RuntimeCodeBase - EFI_PAGES_TO_SIZE (BootTimeCodePageNumber));\r | |
475 | //\r | |
476 | // Try to allocate runtime memory.\r | |
477 | //\r | |
478 | Status = CoreAllocatePages (\r | |
479 | AllocateAddress,\r | |
480 | EfiRuntimeServicesCode,\r | |
481 | RuntimeCodePageNumber,\r | |
482 | &RuntimeCodeBase\r | |
483 | );\r | |
484 | if (EFI_ERROR(Status)) {\r | |
485 | //\r | |
486 | // Runtime memory allocation failed \r | |
487 | //\r | |
488 | return;\r | |
489 | }\r | |
490 | //\r | |
491 | // Try to allocate boot memory.\r | |
492 | //\r | |
493 | Status = CoreAllocatePages (\r | |
494 | AllocateAddress,\r | |
495 | EfiBootServicesCode,\r | |
496 | BootTimeCodePageNumber,\r | |
497 | &BootTimeCodeBase\r | |
498 | );\r | |
499 | if (EFI_ERROR(Status)) {\r | |
500 | //\r | |
501 | // boot memory allocation failed. Free Runtime code range and will try the allocation again when \r | |
502 | // new memory range is installed.\r | |
503 | //\r | |
504 | CoreFreePages (\r | |
505 | RuntimeCodeBase,\r | |
506 | RuntimeCodePageNumber\r | |
507 | );\r | |
508 | return;\r | |
509 | }\r | |
510 | gLoadFixedAddressCodeMemoryReady = TRUE;\r | |
511 | } \r | |
512 | return;\r | |
513 | } \r | |
28a00297 | 514 | \r |
162ed594 | 515 | /**\r |
28a00297 | 516 | Called to initialize the memory map and add descriptors to\r |
517 | the current descriptor list.\r | |
28a00297 | 518 | The first descriptor that is added must be general usable\r |
519 | memory as the addition allocates heap.\r | |
520 | \r | |
022c6d45 | 521 | @param Type The type of memory to add\r |
522 | @param Start The starting address in the memory range Must be\r | |
523 | page aligned\r | |
524 | @param NumberOfPages The number of pages in the range\r | |
525 | @param Attribute Attributes of the memory to add\r | |
28a00297 | 526 | \r |
162ed594 | 527 | @return None. The range is added to the memory map\r |
28a00297 | 528 | \r |
162ed594 | 529 | **/\r |
530 | VOID\r | |
531 | CoreAddMemoryDescriptor (\r | |
532 | IN EFI_MEMORY_TYPE Type,\r | |
533 | IN EFI_PHYSICAL_ADDRESS Start,\r | |
534 | IN UINT64 NumberOfPages,\r | |
535 | IN UINT64 Attribute\r | |
536 | )\r | |
28a00297 | 537 | {\r |
538 | EFI_PHYSICAL_ADDRESS End;\r | |
539 | EFI_STATUS Status;\r | |
540 | UINTN Index;\r | |
541 | UINTN FreeIndex;\r | |
54ea99a7 | 542 | \r |
28a00297 | 543 | if ((Start & EFI_PAGE_MASK) != 0) {\r |
544 | return;\r | |
545 | }\r | |
546 | \r | |
8ee25f48 | 547 | if (Type >= EfiMaxMemoryType && Type < MEMORY_TYPE_OEM_RESERVED_MIN) {\r |
28a00297 | 548 | return;\r |
549 | }\r | |
28a00297 | 550 | CoreAcquireMemoryLock ();\r |
551 | End = Start + LShiftU64 (NumberOfPages, EFI_PAGE_SHIFT) - 1;\r | |
552 | CoreAddRange (Type, Start, End, Attribute);\r | |
553 | CoreFreeMemoryMapStack ();\r | |
554 | CoreReleaseMemoryLock ();\r | |
555 | \r | |
7eb927db | 556 | ApplyMemoryProtectionPolicy (EfiMaxMemoryType, Type, Start,\r |
4879e130 | 557 | LShiftU64 (NumberOfPages, EFI_PAGE_SHIFT));\r |
7eb927db | 558 | \r |
54ea99a7 | 559 | //\r |
560 | // If Loading Module At Fixed Address feature is enabled. try to allocate memory with Runtime code & Boot time code type\r | |
561 | //\r | |
852081fc | 562 | if (PcdGet64(PcdLoadModuleAtFixAddressEnable) != 0) {\r |
54ea99a7 | 563 | CoreLoadingFixedAddressHook();\r |
564 | }\r | |
565 | \r | |
28a00297 | 566 | //\r |
567 | // Check to see if the statistics for the different memory types have already been established\r | |
568 | //\r | |
569 | if (mMemoryTypeInformationInitialized) {\r | |
570 | return;\r | |
571 | }\r | |
572 | \r | |
54ea99a7 | 573 | \r |
28a00297 | 574 | //\r |
575 | // Loop through each memory type in the order specified by the gMemoryTypeInformation[] array\r | |
576 | //\r | |
577 | for (Index = 0; gMemoryTypeInformation[Index].Type != EfiMaxMemoryType; Index++) {\r | |
578 | //\r | |
579 | // Make sure the memory type in the gMemoryTypeInformation[] array is valid\r | |
580 | //\r | |
581 | Type = (EFI_MEMORY_TYPE) (gMemoryTypeInformation[Index].Type);\r | |
3d78c020 | 582 | if ((UINT32)Type > EfiMaxMemoryType) {\r |
28a00297 | 583 | continue;\r |
584 | }\r | |
28a00297 | 585 | if (gMemoryTypeInformation[Index].NumberOfPages != 0) {\r |
586 | //\r | |
587 | // Allocate pages for the current memory type from the top of available memory\r | |
588 | //\r | |
589 | Status = CoreAllocatePages (\r | |
590 | AllocateAnyPages,\r | |
591 | Type,\r | |
592 | gMemoryTypeInformation[Index].NumberOfPages,\r | |
593 | &mMemoryTypeStatistics[Type].BaseAddress\r | |
594 | );\r | |
595 | if (EFI_ERROR (Status)) {\r | |
596 | //\r | |
022c6d45 | 597 | // If an error occurs allocating the pages for the current memory type, then\r |
28a00297 | 598 | // free all the pages allocates for the previous memory types and return. This\r |
599 | // operation with be retied when/if more memory is added to the system\r | |
600 | //\r | |
601 | for (FreeIndex = 0; FreeIndex < Index; FreeIndex++) {\r | |
602 | //\r | |
603 | // Make sure the memory type in the gMemoryTypeInformation[] array is valid\r | |
604 | //\r | |
605 | Type = (EFI_MEMORY_TYPE) (gMemoryTypeInformation[FreeIndex].Type);\r | |
3d78c020 | 606 | if ((UINT32)Type > EfiMaxMemoryType) {\r |
28a00297 | 607 | continue;\r |
608 | }\r | |
609 | \r | |
610 | if (gMemoryTypeInformation[FreeIndex].NumberOfPages != 0) {\r | |
611 | CoreFreePages (\r | |
022c6d45 | 612 | mMemoryTypeStatistics[Type].BaseAddress,\r |
28a00297 | 613 | gMemoryTypeInformation[FreeIndex].NumberOfPages\r |
614 | );\r | |
615 | mMemoryTypeStatistics[Type].BaseAddress = 0;\r | |
f3f2e05d | 616 | mMemoryTypeStatistics[Type].MaximumAddress = MAX_ADDRESS;\r |
28a00297 | 617 | }\r |
618 | }\r | |
619 | return;\r | |
620 | }\r | |
621 | \r | |
622 | //\r | |
623 | // Compute the address at the top of the current statistics\r | |
624 | //\r | |
022c6d45 | 625 | mMemoryTypeStatistics[Type].MaximumAddress =\r |
626 | mMemoryTypeStatistics[Type].BaseAddress +\r | |
28a00297 | 627 | LShiftU64 (gMemoryTypeInformation[Index].NumberOfPages, EFI_PAGE_SHIFT) - 1;\r |
628 | \r | |
629 | //\r | |
022c6d45 | 630 | // If the current base address is the lowest address so far, then update the default\r |
28a00297 | 631 | // maximum address\r |
632 | //\r | |
633 | if (mMemoryTypeStatistics[Type].BaseAddress < mDefaultMaximumAddress) {\r | |
634 | mDefaultMaximumAddress = mMemoryTypeStatistics[Type].BaseAddress - 1;\r | |
635 | }\r | |
636 | }\r | |
637 | }\r | |
638 | \r | |
639 | //\r | |
640 | // There was enough system memory for all the the memory types were allocated. So,\r | |
641 | // those memory areas can be freed for future allocations, and all future memory\r | |
642 | // allocations can occur within their respective bins\r | |
643 | //\r | |
644 | for (Index = 0; gMemoryTypeInformation[Index].Type != EfiMaxMemoryType; Index++) {\r | |
645 | //\r | |
646 | // Make sure the memory type in the gMemoryTypeInformation[] array is valid\r | |
647 | //\r | |
648 | Type = (EFI_MEMORY_TYPE) (gMemoryTypeInformation[Index].Type);\r | |
3d78c020 | 649 | if ((UINT32)Type > EfiMaxMemoryType) {\r |
28a00297 | 650 | continue;\r |
651 | }\r | |
28a00297 | 652 | if (gMemoryTypeInformation[Index].NumberOfPages != 0) {\r |
653 | CoreFreePages (\r | |
022c6d45 | 654 | mMemoryTypeStatistics[Type].BaseAddress,\r |
28a00297 | 655 | gMemoryTypeInformation[Index].NumberOfPages\r |
656 | );\r | |
b74350e9 | 657 | mMemoryTypeStatistics[Type].NumberOfPages = gMemoryTypeInformation[Index].NumberOfPages;\r |
28a00297 | 658 | gMemoryTypeInformation[Index].NumberOfPages = 0;\r |
659 | }\r | |
660 | }\r | |
661 | \r | |
662 | //\r | |
663 | // If the number of pages reserved for a memory type is 0, then all allocations for that type\r | |
664 | // should be in the default range.\r | |
665 | //\r | |
666 | for (Type = (EFI_MEMORY_TYPE) 0; Type < EfiMaxMemoryType; Type++) {\r | |
667 | for (Index = 0; gMemoryTypeInformation[Index].Type != EfiMaxMemoryType; Index++) {\r | |
668 | if (Type == (EFI_MEMORY_TYPE)gMemoryTypeInformation[Index].Type) {\r | |
669 | mMemoryTypeStatistics[Type].InformationIndex = Index;\r | |
670 | }\r | |
671 | }\r | |
672 | mMemoryTypeStatistics[Type].CurrentNumberOfPages = 0;\r | |
f3f2e05d | 673 | if (mMemoryTypeStatistics[Type].MaximumAddress == MAX_ADDRESS) {\r |
28a00297 | 674 | mMemoryTypeStatistics[Type].MaximumAddress = mDefaultMaximumAddress;\r |
675 | }\r | |
676 | }\r | |
677 | \r | |
678 | mMemoryTypeInformationInitialized = TRUE;\r | |
679 | }\r | |
680 | \r | |
681 | \r | |
162ed594 | 682 | /**\r |
771ee501 EC |
683 | Internal function. Converts a memory range to the specified type or attributes.\r |
684 | The range must exist in the memory map. Either ChangingType or\r | |
685 | ChangingAttributes must be set, but not both.\r | |
162ed594 | 686 | \r |
022c6d45 | 687 | @param Start The first address of the range Must be page\r |
688 | aligned\r | |
689 | @param NumberOfPages The number of pages to convert\r | |
771ee501 | 690 | @param ChangingType Boolean indicating that type value should be changed\r |
022c6d45 | 691 | @param NewType The new type for the memory range\r |
771ee501 EC |
692 | @param ChangingAttributes Boolean indicating that attributes value should be changed\r |
693 | @param NewAttributes The new attributes for the memory range\r | |
162ed594 | 694 | \r |
022c6d45 | 695 | @retval EFI_INVALID_PARAMETER Invalid parameter\r |
696 | @retval EFI_NOT_FOUND Could not find a descriptor cover the specified\r | |
697 | range or convertion not allowed.\r | |
698 | @retval EFI_SUCCESS Successfully converts the memory range to the\r | |
162ed594 | 699 | specified type.\r |
700 | \r | |
701 | **/\r | |
28a00297 | 702 | EFI_STATUS\r |
771ee501 | 703 | CoreConvertPagesEx (\r |
28a00297 | 704 | IN UINT64 Start,\r |
705 | IN UINT64 NumberOfPages,\r | |
771ee501 EC |
706 | IN BOOLEAN ChangingType,\r |
707 | IN EFI_MEMORY_TYPE NewType,\r | |
708 | IN BOOLEAN ChangingAttributes,\r | |
709 | IN UINT64 NewAttributes\r | |
28a00297 | 710 | )\r |
28a00297 | 711 | {\r |
712 | \r | |
713 | UINT64 NumberOfBytes;\r | |
714 | UINT64 End;\r | |
715 | UINT64 RangeEnd;\r | |
716 | UINT64 Attribute;\r | |
771ee501 | 717 | EFI_MEMORY_TYPE MemType;\r |
28a00297 | 718 | LIST_ENTRY *Link;\r |
719 | MEMORY_MAP *Entry;\r | |
720 | \r | |
721 | Entry = NULL;\r | |
722 | NumberOfBytes = LShiftU64 (NumberOfPages, EFI_PAGE_SHIFT);\r | |
723 | End = Start + NumberOfBytes - 1;\r | |
724 | \r | |
725 | ASSERT (NumberOfPages);\r | |
726 | ASSERT ((Start & EFI_PAGE_MASK) == 0);\r | |
727 | ASSERT (End > Start) ;\r | |
728 | ASSERT_LOCKED (&gMemoryLock);\r | |
771ee501 | 729 | ASSERT ( (ChangingType == FALSE) || (ChangingAttributes == FALSE) );\r |
28a00297 | 730 | \r |
f0459afe | 731 | if (NumberOfPages == 0 || ((Start & EFI_PAGE_MASK) != 0) || (Start >= End)) {\r |
28a00297 | 732 | return EFI_INVALID_PARAMETER;\r |
733 | }\r | |
734 | \r | |
735 | //\r | |
736 | // Convert the entire range\r | |
737 | //\r | |
738 | \r | |
739 | while (Start < End) {\r | |
740 | \r | |
741 | //\r | |
742 | // Find the entry that the covers the range\r | |
743 | //\r | |
744 | for (Link = gMemoryMap.ForwardLink; Link != &gMemoryMap; Link = Link->ForwardLink) {\r | |
745 | Entry = CR (Link, MEMORY_MAP, Link, MEMORY_MAP_SIGNATURE);\r | |
746 | \r | |
747 | if (Entry->Start <= Start && Entry->End > Start) {\r | |
748 | break;\r | |
749 | }\r | |
750 | }\r | |
751 | \r | |
752 | if (Link == &gMemoryMap) {\r | |
162ed594 | 753 | DEBUG ((DEBUG_ERROR | DEBUG_PAGE, "ConvertPages: failed to find range %lx - %lx\n", Start, End));\r |
28a00297 | 754 | return EFI_NOT_FOUND;\r |
755 | }\r | |
756 | \r | |
3f2ae009 AB |
757 | //\r |
758 | // If we are converting the type of the range from EfiConventionalMemory to\r | |
759 | // another type, we have to ensure that the entire range is covered by a\r | |
760 | // single entry.\r | |
761 | //\r | |
762 | if (ChangingType && (NewType != EfiConventionalMemory)) {\r | |
763 | if (Entry->End < End) {\r | |
764 | DEBUG ((DEBUG_ERROR | DEBUG_PAGE, "ConvertPages: range %lx - %lx covers multiple entries\n", Start, End));\r | |
765 | return EFI_NOT_FOUND;\r | |
766 | }\r | |
767 | }\r | |
28a00297 | 768 | //\r |
769 | // Convert range to the end, or to the end of the descriptor\r | |
770 | // if that's all we've got\r | |
771 | //\r | |
772 | RangeEnd = End;\r | |
525aded9 | 773 | \r |
774 | ASSERT (Entry != NULL);\r | |
28a00297 | 775 | if (Entry->End < End) {\r |
776 | RangeEnd = Entry->End;\r | |
777 | }\r | |
778 | \r | |
771ee501 EC |
779 | if (ChangingType) {\r |
780 | DEBUG ((DEBUG_PAGE, "ConvertRange: %lx-%lx to type %d\n", Start, RangeEnd, NewType));\r | |
781 | }\r | |
782 | if (ChangingAttributes) {\r | |
783 | DEBUG ((DEBUG_PAGE, "ConvertRange: %lx-%lx to attr %lx\n", Start, RangeEnd, NewAttributes));\r | |
022c6d45 | 784 | }\r |
28a00297 | 785 | \r |
771ee501 EC |
786 | if (ChangingType) {\r |
787 | //\r | |
788 | // Debug code - verify conversion is allowed\r | |
789 | //\r | |
790 | if (!(NewType == EfiConventionalMemory ? 1 : 0) ^ (Entry->Type == EfiConventionalMemory ? 1 : 0)) {\r | |
9a701955 SZ |
791 | DEBUG ((DEBUG_ERROR | DEBUG_PAGE, "ConvertPages: Incompatible memory types, "));\r |
792 | if (Entry->Type == EfiConventionalMemory) {\r | |
793 | DEBUG ((DEBUG_ERROR | DEBUG_PAGE, "the pages to free have been freed\n"));\r | |
794 | } else {\r | |
795 | DEBUG ((DEBUG_ERROR | DEBUG_PAGE, "the pages to allocate have been allocated\n"));\r | |
796 | }\r | |
771ee501 EC |
797 | return EFI_NOT_FOUND;\r |
798 | }\r | |
799 | \r | |
800 | //\r | |
801 | // Update counters for the number of pages allocated to each memory type\r | |
802 | //\r | |
803 | if ((UINT32)Entry->Type < EfiMaxMemoryType) {\r | |
804 | if ((Start >= mMemoryTypeStatistics[Entry->Type].BaseAddress && Start <= mMemoryTypeStatistics[Entry->Type].MaximumAddress) ||\r | |
805 | (Start >= mDefaultBaseAddress && Start <= mDefaultMaximumAddress) ) {\r | |
806 | if (NumberOfPages > mMemoryTypeStatistics[Entry->Type].CurrentNumberOfPages) {\r | |
807 | mMemoryTypeStatistics[Entry->Type].CurrentNumberOfPages = 0;\r | |
808 | } else {\r | |
809 | mMemoryTypeStatistics[Entry->Type].CurrentNumberOfPages -= NumberOfPages;\r | |
810 | }\r | |
28a00297 | 811 | }\r |
812 | }\r | |
28a00297 | 813 | \r |
771ee501 EC |
814 | if ((UINT32)NewType < EfiMaxMemoryType) {\r |
815 | if ((Start >= mMemoryTypeStatistics[NewType].BaseAddress && Start <= mMemoryTypeStatistics[NewType].MaximumAddress) ||\r | |
816 | (Start >= mDefaultBaseAddress && Start <= mDefaultMaximumAddress) ) {\r | |
817 | mMemoryTypeStatistics[NewType].CurrentNumberOfPages += NumberOfPages;\r | |
818 | if (mMemoryTypeStatistics[NewType].CurrentNumberOfPages > gMemoryTypeInformation[mMemoryTypeStatistics[NewType].InformationIndex].NumberOfPages) {\r | |
819 | gMemoryTypeInformation[mMemoryTypeStatistics[NewType].InformationIndex].NumberOfPages = (UINT32)mMemoryTypeStatistics[NewType].CurrentNumberOfPages;\r | |
820 | }\r | |
28a00297 | 821 | }\r |
822 | }\r | |
823 | }\r | |
824 | \r | |
825 | //\r | |
826 | // Pull range out of descriptor\r | |
827 | //\r | |
828 | if (Entry->Start == Start) {\r | |
022c6d45 | 829 | \r |
28a00297 | 830 | //\r |
831 | // Clip start\r | |
832 | //\r | |
833 | Entry->Start = RangeEnd + 1;\r | |
834 | \r | |
835 | } else if (Entry->End == RangeEnd) {\r | |
022c6d45 | 836 | \r |
28a00297 | 837 | //\r |
838 | // Clip end\r | |
839 | //\r | |
840 | Entry->End = Start - 1;\r | |
841 | \r | |
842 | } else {\r | |
843 | \r | |
844 | //\r | |
845 | // Pull it out of the center, clip current\r | |
846 | //\r | |
022c6d45 | 847 | \r |
28a00297 | 848 | //\r |
849 | // Add a new one\r | |
850 | //\r | |
851 | mMapStack[mMapDepth].Signature = MEMORY_MAP_SIGNATURE;\r | |
852 | mMapStack[mMapDepth].FromPages = FALSE;\r | |
853 | mMapStack[mMapDepth].Type = Entry->Type;\r | |
854 | mMapStack[mMapDepth].Start = RangeEnd+1;\r | |
855 | mMapStack[mMapDepth].End = Entry->End;\r | |
856 | \r | |
857 | //\r | |
858 | // Inherit Attribute from the Memory Descriptor that is being clipped\r | |
859 | //\r | |
860 | mMapStack[mMapDepth].Attribute = Entry->Attribute;\r | |
861 | \r | |
862 | Entry->End = Start - 1;\r | |
863 | ASSERT (Entry->Start < Entry->End);\r | |
864 | \r | |
865 | Entry = &mMapStack[mMapDepth];\r | |
866 | InsertTailList (&gMemoryMap, &Entry->Link);\r | |
867 | \r | |
868 | mMapDepth += 1;\r | |
869 | ASSERT (mMapDepth < MAX_MAP_DEPTH);\r | |
870 | }\r | |
871 | \r | |
872 | //\r | |
022c6d45 | 873 | // The new range inherits the same Attribute as the Entry\r |
771ee501 | 874 | // it is being cut out of unless attributes are being changed\r |
28a00297 | 875 | //\r |
771ee501 EC |
876 | if (ChangingType) {\r |
877 | Attribute = Entry->Attribute;\r | |
878 | MemType = NewType;\r | |
879 | } else {\r | |
880 | Attribute = NewAttributes;\r | |
881 | MemType = Entry->Type;\r | |
882 | }\r | |
28a00297 | 883 | \r |
884 | //\r | |
885 | // If the descriptor is empty, then remove it from the map\r | |
886 | //\r | |
887 | if (Entry->Start == Entry->End + 1) {\r | |
888 | RemoveMemoryMapEntry (Entry);\r | |
889 | Entry = NULL;\r | |
890 | }\r | |
022c6d45 | 891 | \r |
28a00297 | 892 | //\r |
893 | // Add our new range in\r | |
894 | //\r | |
771ee501 EC |
895 | CoreAddRange (MemType, Start, RangeEnd, Attribute);\r |
896 | if (ChangingType && (MemType == EfiConventionalMemory)) {\r | |
9a340872 | 897 | //\r |
898 | // Avoid calling DEBUG_CLEAR_MEMORY() for an address of 0 because this\r | |
899 | // macro will ASSERT() if address is 0. Instead, CoreAddRange() guarantees\r | |
900 | // that the page starting at address 0 is always filled with zeros.\r | |
901 | //\r | |
902 | if (Start == 0) {\r | |
903 | if (RangeEnd > EFI_PAGE_SIZE) {\r | |
904 | DEBUG_CLEAR_MEMORY ((VOID *)(UINTN) EFI_PAGE_SIZE, (UINTN) (RangeEnd - EFI_PAGE_SIZE + 1));\r | |
905 | }\r | |
906 | } else {\r | |
907 | DEBUG_CLEAR_MEMORY ((VOID *)(UINTN) Start, (UINTN) (RangeEnd - Start + 1));\r | |
908 | }\r | |
38c7df98 | 909 | }\r |
28a00297 | 910 | \r |
911 | //\r | |
912 | // Move any map descriptor stack to general pool\r | |
913 | //\r | |
914 | CoreFreeMemoryMapStack ();\r | |
915 | \r | |
916 | //\r | |
917 | // Bump the starting address, and convert the next range\r | |
918 | //\r | |
919 | Start = RangeEnd + 1;\r | |
920 | }\r | |
921 | \r | |
922 | //\r | |
923 | // Converted the whole range, done\r | |
924 | //\r | |
925 | \r | |
926 | return EFI_SUCCESS;\r | |
927 | }\r | |
928 | \r | |
929 | \r | |
771ee501 EC |
930 | /**\r |
931 | Internal function. Converts a memory range to the specified type.\r | |
932 | The range must exist in the memory map.\r | |
933 | \r | |
934 | @param Start The first address of the range Must be page\r | |
935 | aligned\r | |
936 | @param NumberOfPages The number of pages to convert\r | |
937 | @param NewType The new type for the memory range\r | |
938 | \r | |
939 | @retval EFI_INVALID_PARAMETER Invalid parameter\r | |
940 | @retval EFI_NOT_FOUND Could not find a descriptor cover the specified\r | |
941 | range or convertion not allowed.\r | |
942 | @retval EFI_SUCCESS Successfully converts the memory range to the\r | |
943 | specified type.\r | |
944 | \r | |
945 | **/\r | |
946 | EFI_STATUS\r | |
947 | CoreConvertPages (\r | |
948 | IN UINT64 Start,\r | |
949 | IN UINT64 NumberOfPages,\r | |
950 | IN EFI_MEMORY_TYPE NewType\r | |
951 | )\r | |
952 | {\r | |
953 | return CoreConvertPagesEx(Start, NumberOfPages, TRUE, NewType, FALSE, 0);\r | |
954 | }\r | |
955 | \r | |
956 | \r | |
957 | /**\r | |
958 | Internal function. Converts a memory range to use new attributes.\r | |
959 | \r | |
960 | @param Start The first address of the range Must be page\r | |
961 | aligned\r | |
962 | @param NumberOfPages The number of pages to convert\r | |
963 | @param NewAttributes The new attributes value for the range.\r | |
964 | \r | |
771ee501 EC |
965 | **/\r |
966 | VOID\r | |
967 | CoreUpdateMemoryAttributes (\r | |
968 | IN EFI_PHYSICAL_ADDRESS Start,\r | |
969 | IN UINT64 NumberOfPages,\r | |
970 | IN UINT64 NewAttributes\r | |
971 | )\r | |
972 | {\r | |
973 | CoreAcquireMemoryLock ();\r | |
974 | \r | |
975 | //\r | |
976 | // Update the attributes to the new value\r | |
977 | //\r | |
978 | CoreConvertPagesEx(Start, NumberOfPages, FALSE, (EFI_MEMORY_TYPE)0, TRUE, NewAttributes);\r | |
979 | \r | |
980 | CoreReleaseMemoryLock ();\r | |
981 | }\r | |
982 | \r | |
162ed594 | 983 | \r |
984 | /**\r | |
985 | Internal function. Finds a consecutive free page range below\r | |
986 | the requested address.\r | |
987 | \r | |
022c6d45 | 988 | @param MaxAddress The address that the range must be below\r |
35f9e94e | 989 | @param MinAddress The address that the range must be above\r |
022c6d45 | 990 | @param NumberOfPages Number of pages needed\r |
991 | @param NewType The type of memory the range is going to be\r | |
992 | turned into\r | |
993 | @param Alignment Bits to align with\r | |
162ed594 | 994 | \r |
995 | @return The base address of the range, or 0 if the range was not found\r | |
996 | \r | |
997 | **/\r | |
28a00297 | 998 | UINT64\r |
999 | CoreFindFreePagesI (\r | |
1000 | IN UINT64 MaxAddress,\r | |
2345e7d4 | 1001 | IN UINT64 MinAddress,\r |
28a00297 | 1002 | IN UINT64 NumberOfPages,\r |
1003 | IN EFI_MEMORY_TYPE NewType,\r | |
1004 | IN UINTN Alignment\r | |
1005 | )\r | |
28a00297 | 1006 | {\r |
1007 | UINT64 NumberOfBytes;\r | |
1008 | UINT64 Target;\r | |
1009 | UINT64 DescStart;\r | |
1010 | UINT64 DescEnd;\r | |
1011 | UINT64 DescNumberOfBytes;\r | |
1012 | LIST_ENTRY *Link;\r | |
1013 | MEMORY_MAP *Entry;\r | |
1014 | \r | |
1015 | if ((MaxAddress < EFI_PAGE_MASK) ||(NumberOfPages == 0)) {\r | |
1016 | return 0;\r | |
1017 | }\r | |
1018 | \r | |
1019 | if ((MaxAddress & EFI_PAGE_MASK) != EFI_PAGE_MASK) {\r | |
022c6d45 | 1020 | \r |
28a00297 | 1021 | //\r |
1022 | // If MaxAddress is not aligned to the end of a page\r | |
1023 | //\r | |
022c6d45 | 1024 | \r |
28a00297 | 1025 | //\r |
1026 | // Change MaxAddress to be 1 page lower\r | |
1027 | //\r | |
1028 | MaxAddress -= (EFI_PAGE_MASK + 1);\r | |
022c6d45 | 1029 | \r |
28a00297 | 1030 | //\r |
1031 | // Set MaxAddress to a page boundary\r | |
1032 | //\r | |
6e1e5405 | 1033 | MaxAddress &= ~(UINT64)EFI_PAGE_MASK;\r |
022c6d45 | 1034 | \r |
28a00297 | 1035 | //\r |
1036 | // Set MaxAddress to end of the page\r | |
1037 | //\r | |
1038 | MaxAddress |= EFI_PAGE_MASK;\r | |
1039 | }\r | |
1040 | \r | |
1041 | NumberOfBytes = LShiftU64 (NumberOfPages, EFI_PAGE_SHIFT);\r | |
1042 | Target = 0;\r | |
1043 | \r | |
1044 | for (Link = gMemoryMap.ForwardLink; Link != &gMemoryMap; Link = Link->ForwardLink) {\r | |
1045 | Entry = CR (Link, MEMORY_MAP, Link, MEMORY_MAP_SIGNATURE);\r | |
022c6d45 | 1046 | \r |
28a00297 | 1047 | //\r |
1048 | // If it's not a free entry, don't bother with it\r | |
1049 | //\r | |
1050 | if (Entry->Type != EfiConventionalMemory) {\r | |
1051 | continue;\r | |
1052 | }\r | |
1053 | \r | |
1054 | DescStart = Entry->Start;\r | |
1055 | DescEnd = Entry->End;\r | |
1056 | \r | |
1057 | //\r | |
2345e7d4 | 1058 | // If desc is past max allowed address or below min allowed address, skip it\r |
28a00297 | 1059 | //\r |
2345e7d4 | 1060 | if ((DescStart >= MaxAddress) || (DescEnd < MinAddress)) {\r |
28a00297 | 1061 | continue;\r |
1062 | }\r | |
1063 | \r | |
1064 | //\r | |
1065 | // If desc ends past max allowed address, clip the end\r | |
1066 | //\r | |
1067 | if (DescEnd >= MaxAddress) {\r | |
1068 | DescEnd = MaxAddress;\r | |
1069 | }\r | |
1070 | \r | |
1071 | DescEnd = ((DescEnd + 1) & (~(Alignment - 1))) - 1;\r | |
1072 | \r | |
1365bedd HG |
1073 | // Skip if DescEnd is less than DescStart after alignment clipping\r |
1074 | if (DescEnd < DescStart) {\r | |
1075 | continue;\r | |
1076 | }\r | |
1077 | \r | |
28a00297 | 1078 | //\r |
022c6d45 | 1079 | // Compute the number of bytes we can used from this\r |
28a00297 | 1080 | // descriptor, and see it's enough to satisfy the request\r |
1081 | //\r | |
1082 | DescNumberOfBytes = DescEnd - DescStart + 1;\r | |
1083 | \r | |
1084 | if (DescNumberOfBytes >= NumberOfBytes) {\r | |
2345e7d4 | 1085 | //\r |
1086 | // If the start of the allocated range is below the min address allowed, skip it\r | |
1087 | //\r | |
1088 | if ((DescEnd - NumberOfBytes + 1) < MinAddress) {\r | |
1089 | continue;\r | |
1090 | }\r | |
28a00297 | 1091 | \r |
1092 | //\r | |
1093 | // If this is the best match so far remember it\r | |
1094 | //\r | |
1095 | if (DescEnd > Target) {\r | |
1096 | Target = DescEnd;\r | |
1097 | }\r | |
1098 | }\r | |
022c6d45 | 1099 | }\r |
28a00297 | 1100 | \r |
1101 | //\r | |
1102 | // If this is a grow down, adjust target to be the allocation base\r | |
1103 | //\r | |
1104 | Target -= NumberOfBytes - 1;\r | |
1105 | \r | |
1106 | //\r | |
1107 | // If we didn't find a match, return 0\r | |
1108 | //\r | |
1109 | if ((Target & EFI_PAGE_MASK) != 0) {\r | |
1110 | return 0;\r | |
1111 | }\r | |
1112 | \r | |
1113 | return Target;\r | |
1114 | }\r | |
1115 | \r | |
162ed594 | 1116 | \r |
1117 | /**\r | |
1118 | Internal function. Finds a consecutive free page range below\r | |
1119 | the requested address\r | |
1120 | \r | |
022c6d45 | 1121 | @param MaxAddress The address that the range must be below\r |
1122 | @param NoPages Number of pages needed\r | |
1123 | @param NewType The type of memory the range is going to be\r | |
1124 | turned into\r | |
1125 | @param Alignment Bits to align with\r | |
162ed594 | 1126 | \r |
1127 | @return The base address of the range, or 0 if the range was not found.\r | |
1128 | \r | |
1129 | **/\r | |
28a00297 | 1130 | UINT64\r |
1131 | FindFreePages (\r | |
1132 | IN UINT64 MaxAddress,\r | |
1133 | IN UINT64 NoPages,\r | |
1134 | IN EFI_MEMORY_TYPE NewType,\r | |
1135 | IN UINTN Alignment\r | |
1136 | )\r | |
28a00297 | 1137 | {\r |
2345e7d4 | 1138 | UINT64 Start;\r |
28a00297 | 1139 | \r |
2345e7d4 | 1140 | //\r |
1141 | // Attempt to find free pages in the preferred bin based on the requested memory type\r | |
1142 | //\r | |
3d78c020 | 1143 | if ((UINT32)NewType < EfiMaxMemoryType && MaxAddress >= mMemoryTypeStatistics[NewType].MaximumAddress) {\r |
2345e7d4 | 1144 | Start = CoreFindFreePagesI (\r |
1145 | mMemoryTypeStatistics[NewType].MaximumAddress, \r | |
1146 | mMemoryTypeStatistics[NewType].BaseAddress, \r | |
1147 | NoPages, \r | |
1148 | NewType, \r | |
1149 | Alignment\r | |
1150 | );\r | |
1151 | if (Start != 0) {\r | |
1152 | return Start;\r | |
1153 | }\r | |
1154 | }\r | |
28a00297 | 1155 | \r |
2345e7d4 | 1156 | //\r |
1157 | // Attempt to find free pages in the default allocation bin\r | |
1158 | //\r | |
1159 | if (MaxAddress >= mDefaultMaximumAddress) {\r | |
1160 | Start = CoreFindFreePagesI (mDefaultMaximumAddress, 0, NoPages, NewType, Alignment);\r | |
1161 | if (Start != 0) {\r | |
1162 | if (Start < mDefaultBaseAddress) {\r | |
1163 | mDefaultBaseAddress = Start;\r | |
1164 | }\r | |
1165 | return Start;\r | |
28a00297 | 1166 | }\r |
1167 | }\r | |
1168 | \r | |
2345e7d4 | 1169 | //\r |
1170 | // The allocation did not succeed in any of the prefered bins even after \r | |
1171 | // promoting resources. Attempt to find free pages anywhere is the requested \r | |
1172 | // address range. If this allocation fails, then there are not enough \r | |
1173 | // resources anywhere to satisfy the request.\r | |
1174 | //\r | |
1175 | Start = CoreFindFreePagesI (MaxAddress, 0, NoPages, NewType, Alignment);\r | |
1176 | if (Start != 0) {\r | |
1177 | return Start;\r | |
1178 | }\r | |
28a00297 | 1179 | \r |
2345e7d4 | 1180 | //\r |
1181 | // If allocations from the preferred bins fail, then attempt to promote memory resources.\r | |
1182 | //\r | |
1183 | if (!PromoteMemoryResource ()) {\r | |
1184 | return 0;\r | |
28a00297 | 1185 | }\r |
1186 | \r | |
2345e7d4 | 1187 | //\r |
1188 | // If any memory resources were promoted, then re-attempt the allocation\r | |
1189 | //\r | |
1190 | return FindFreePages (MaxAddress, NoPages, NewType, Alignment);\r | |
28a00297 | 1191 | }\r |
1192 | \r | |
1193 | \r | |
162ed594 | 1194 | /**\r |
1195 | Allocates pages from the memory map.\r | |
1196 | \r | |
022c6d45 | 1197 | @param Type The type of allocation to perform\r |
1198 | @param MemoryType The type of memory to turn the allocated pages\r | |
1199 | into\r | |
1200 | @param NumberOfPages The number of pages to allocate\r | |
1201 | @param Memory A pointer to receive the base allocated memory\r | |
1202 | address\r | |
162ed594 | 1203 | \r |
1204 | @return Status. On success, Memory is filled in with the base address allocated\r | |
022c6d45 | 1205 | @retval EFI_INVALID_PARAMETER Parameters violate checking rules defined in\r |
1206 | spec.\r | |
1207 | @retval EFI_NOT_FOUND Could not allocate pages match the requirement.\r | |
1208 | @retval EFI_OUT_OF_RESOURCES No enough pages to allocate.\r | |
162ed594 | 1209 | @retval EFI_SUCCESS Pages successfully allocated.\r |
1210 | \r | |
1211 | **/\r | |
28a00297 | 1212 | EFI_STATUS\r |
1213 | EFIAPI\r | |
84edd20b | 1214 | CoreInternalAllocatePages (\r |
28a00297 | 1215 | IN EFI_ALLOCATE_TYPE Type,\r |
1216 | IN EFI_MEMORY_TYPE MemoryType,\r | |
1217 | IN UINTN NumberOfPages,\r | |
1218 | IN OUT EFI_PHYSICAL_ADDRESS *Memory\r | |
1219 | )\r | |
28a00297 | 1220 | {\r |
1221 | EFI_STATUS Status;\r | |
1222 | UINT64 Start;\r | |
c2a07a10 SZ |
1223 | UINT64 NumberOfBytes;\r |
1224 | UINT64 End;\r | |
28a00297 | 1225 | UINT64 MaxAddress;\r |
1226 | UINTN Alignment;\r | |
1227 | \r | |
3d78c020 | 1228 | if ((UINT32)Type >= MaxAllocateType) {\r |
28a00297 | 1229 | return EFI_INVALID_PARAMETER;\r |
1230 | }\r | |
1231 | \r | |
8ee25f48 | 1232 | if ((MemoryType >= EfiMaxMemoryType && MemoryType < MEMORY_TYPE_OEM_RESERVED_MIN) ||\r |
a671a012 | 1233 | (MemoryType == EfiConventionalMemory) || (MemoryType == EfiPersistentMemory)) {\r |
28a00297 | 1234 | return EFI_INVALID_PARAMETER;\r |
1235 | }\r | |
1236 | \r | |
3e058701 ED |
1237 | if (Memory == NULL) {\r |
1238 | return EFI_INVALID_PARAMETER;\r | |
1239 | }\r | |
1240 | \r | |
d4731a98 | 1241 | Alignment = DEFAULT_PAGE_ALLOCATION_GRANULARITY;\r |
28a00297 | 1242 | \r |
1243 | if (MemoryType == EfiACPIReclaimMemory ||\r | |
1244 | MemoryType == EfiACPIMemoryNVS ||\r | |
1245 | MemoryType == EfiRuntimeServicesCode ||\r | |
1246 | MemoryType == EfiRuntimeServicesData) {\r | |
1247 | \r | |
d4731a98 | 1248 | Alignment = RUNTIME_PAGE_ALLOCATION_GRANULARITY;\r |
28a00297 | 1249 | }\r |
1250 | \r | |
1251 | if (Type == AllocateAddress) {\r | |
1252 | if ((*Memory & (Alignment - 1)) != 0) {\r | |
1253 | return EFI_NOT_FOUND;\r | |
1254 | }\r | |
1255 | }\r | |
1256 | \r | |
1257 | NumberOfPages += EFI_SIZE_TO_PAGES (Alignment) - 1;\r | |
1258 | NumberOfPages &= ~(EFI_SIZE_TO_PAGES (Alignment) - 1);\r | |
1259 | \r | |
1260 | //\r | |
022c6d45 | 1261 | // If this is for below a particular address, then\r |
28a00297 | 1262 | //\r |
1263 | Start = *Memory;\r | |
022c6d45 | 1264 | \r |
28a00297 | 1265 | //\r |
1266 | // The max address is the max natively addressable address for the processor\r | |
1267 | //\r | |
f3f2e05d | 1268 | MaxAddress = MAX_ADDRESS;\r |
022c6d45 | 1269 | \r |
c2a07a10 SZ |
1270 | //\r |
1271 | // Check for Type AllocateAddress,\r | |
1272 | // if NumberOfPages is 0 or\r | |
1273 | // if (NumberOfPages << EFI_PAGE_SHIFT) is above MAX_ADDRESS or\r | |
1274 | // if (Start + NumberOfBytes) rolls over 0 or\r | |
1275 | // if Start is above MAX_ADDRESS or\r | |
1276 | // if End is above MAX_ADDRESS,\r | |
1277 | // return EFI_NOT_FOUND.\r | |
1278 | //\r | |
1279 | if (Type == AllocateAddress) {\r | |
1280 | if ((NumberOfPages == 0) ||\r | |
1281 | (NumberOfPages > RShiftU64 (MaxAddress, EFI_PAGE_SHIFT))) {\r | |
1282 | return EFI_NOT_FOUND;\r | |
1283 | }\r | |
1284 | NumberOfBytes = LShiftU64 (NumberOfPages, EFI_PAGE_SHIFT);\r | |
1285 | End = Start + NumberOfBytes - 1;\r | |
1286 | \r | |
1287 | if ((Start >= End) ||\r | |
1288 | (Start > MaxAddress) || \r | |
1289 | (End > MaxAddress)) {\r | |
1290 | return EFI_NOT_FOUND;\r | |
1291 | }\r | |
1292 | }\r | |
1293 | \r | |
28a00297 | 1294 | if (Type == AllocateMaxAddress) {\r |
1295 | MaxAddress = Start;\r | |
1296 | }\r | |
1297 | \r | |
1298 | CoreAcquireMemoryLock ();\r | |
022c6d45 | 1299 | \r |
28a00297 | 1300 | //\r |
1301 | // If not a specific address, then find an address to allocate\r | |
1302 | //\r | |
1303 | if (Type != AllocateAddress) {\r | |
1304 | Start = FindFreePages (MaxAddress, NumberOfPages, MemoryType, Alignment);\r | |
1305 | if (Start == 0) {\r | |
1306 | Status = EFI_OUT_OF_RESOURCES;\r | |
1307 | goto Done;\r | |
1308 | }\r | |
1309 | }\r | |
1310 | \r | |
1311 | //\r | |
1312 | // Convert pages from FreeMemory to the requested type\r | |
1313 | //\r | |
1314 | Status = CoreConvertPages (Start, NumberOfPages, MemoryType);\r | |
1315 | \r | |
1316 | Done:\r | |
1317 | CoreReleaseMemoryLock ();\r | |
1318 | \r | |
1319 | if (!EFI_ERROR (Status)) {\r | |
1320 | *Memory = Start;\r | |
1321 | }\r | |
1322 | \r | |
1323 | return Status;\r | |
1324 | }\r | |
1325 | \r | |
84edd20b SZ |
1326 | /**\r |
1327 | Allocates pages from the memory map.\r | |
1328 | \r | |
1329 | @param Type The type of allocation to perform\r | |
1330 | @param MemoryType The type of memory to turn the allocated pages\r | |
1331 | into\r | |
1332 | @param NumberOfPages The number of pages to allocate\r | |
1333 | @param Memory A pointer to receive the base allocated memory\r | |
1334 | address\r | |
1335 | \r | |
1336 | @return Status. On success, Memory is filled in with the base address allocated\r | |
1337 | @retval EFI_INVALID_PARAMETER Parameters violate checking rules defined in\r | |
1338 | spec.\r | |
1339 | @retval EFI_NOT_FOUND Could not allocate pages match the requirement.\r | |
1340 | @retval EFI_OUT_OF_RESOURCES No enough pages to allocate.\r | |
1341 | @retval EFI_SUCCESS Pages successfully allocated.\r | |
1342 | \r | |
1343 | **/\r | |
1344 | EFI_STATUS\r | |
1345 | EFIAPI\r | |
1346 | CoreAllocatePages (\r | |
1347 | IN EFI_ALLOCATE_TYPE Type,\r | |
1348 | IN EFI_MEMORY_TYPE MemoryType,\r | |
1349 | IN UINTN NumberOfPages,\r | |
1350 | OUT EFI_PHYSICAL_ADDRESS *Memory\r | |
1351 | )\r | |
1352 | {\r | |
1353 | EFI_STATUS Status;\r | |
1354 | \r | |
1355 | Status = CoreInternalAllocatePages (Type, MemoryType, NumberOfPages, Memory);\r | |
1356 | if (!EFI_ERROR (Status)) {\r | |
1d60fe96 SZ |
1357 | CoreUpdateProfile (\r |
1358 | (EFI_PHYSICAL_ADDRESS) (UINTN) RETURN_ADDRESS (0),\r | |
1359 | MemoryProfileActionAllocatePages,\r | |
1360 | MemoryType,\r | |
1361 | EFI_PAGES_TO_SIZE (NumberOfPages),\r | |
1362 | (VOID *) (UINTN) *Memory,\r | |
1363 | NULL\r | |
1364 | );\r | |
74a88770 | 1365 | InstallMemoryAttributesTableOnMemoryAllocation (MemoryType);\r |
7eb927db AB |
1366 | ApplyMemoryProtectionPolicy (EfiConventionalMemory, MemoryType, *Memory,\r |
1367 | EFI_PAGES_TO_SIZE (NumberOfPages));\r | |
84edd20b SZ |
1368 | }\r |
1369 | return Status;\r | |
1370 | }\r | |
28a00297 | 1371 | \r |
162ed594 | 1372 | /**\r |
1373 | Frees previous allocated pages.\r | |
1374 | \r | |
022c6d45 | 1375 | @param Memory Base address of memory being freed\r |
1376 | @param NumberOfPages The number of pages to free\r | |
925f0d1a | 1377 | @param MemoryType Pointer to memory type\r |
162ed594 | 1378 | \r |
022c6d45 | 1379 | @retval EFI_NOT_FOUND Could not find the entry that covers the range\r |
1380 | @retval EFI_INVALID_PARAMETER Address not aligned\r | |
162ed594 | 1381 | @return EFI_SUCCESS -Pages successfully freed.\r |
1382 | \r | |
1383 | **/\r | |
022c6d45 | 1384 | EFI_STATUS\r |
28a00297 | 1385 | EFIAPI\r |
84edd20b | 1386 | CoreInternalFreePages (\r |
28a00297 | 1387 | IN EFI_PHYSICAL_ADDRESS Memory,\r |
925f0d1a SZ |
1388 | IN UINTN NumberOfPages,\r |
1389 | OUT EFI_MEMORY_TYPE *MemoryType OPTIONAL\r | |
28a00297 | 1390 | )\r |
28a00297 | 1391 | {\r |
1392 | EFI_STATUS Status;\r | |
1393 | LIST_ENTRY *Link;\r | |
1394 | MEMORY_MAP *Entry;\r | |
1395 | UINTN Alignment;\r | |
1396 | \r | |
1397 | //\r | |
1398 | // Free the range\r | |
1399 | //\r | |
1400 | CoreAcquireMemoryLock ();\r | |
1401 | \r | |
1402 | //\r | |
1403 | // Find the entry that the covers the range\r | |
1404 | //\r | |
1405 | Entry = NULL;\r | |
1406 | for (Link = gMemoryMap.ForwardLink; Link != &gMemoryMap; Link = Link->ForwardLink) {\r | |
1407 | Entry = CR(Link, MEMORY_MAP, Link, MEMORY_MAP_SIGNATURE);\r | |
1408 | if (Entry->Start <= Memory && Entry->End > Memory) {\r | |
1409 | break;\r | |
1410 | }\r | |
1411 | }\r | |
1412 | if (Link == &gMemoryMap) {\r | |
a5ca8fa7 | 1413 | Status = EFI_NOT_FOUND;\r |
1414 | goto Done;\r | |
28a00297 | 1415 | }\r |
1416 | \r | |
d4731a98 | 1417 | Alignment = DEFAULT_PAGE_ALLOCATION_GRANULARITY;\r |
28a00297 | 1418 | \r |
525aded9 | 1419 | ASSERT (Entry != NULL);\r |
28a00297 | 1420 | if (Entry->Type == EfiACPIReclaimMemory ||\r |
1421 | Entry->Type == EfiACPIMemoryNVS ||\r | |
1422 | Entry->Type == EfiRuntimeServicesCode ||\r | |
1423 | Entry->Type == EfiRuntimeServicesData) {\r | |
1424 | \r | |
d4731a98 | 1425 | Alignment = RUNTIME_PAGE_ALLOCATION_GRANULARITY;\r |
28a00297 | 1426 | \r |
1427 | }\r | |
1428 | \r | |
1429 | if ((Memory & (Alignment - 1)) != 0) {\r | |
a5ca8fa7 | 1430 | Status = EFI_INVALID_PARAMETER;\r |
1431 | goto Done;\r | |
28a00297 | 1432 | }\r |
1433 | \r | |
1434 | NumberOfPages += EFI_SIZE_TO_PAGES (Alignment) - 1;\r | |
1435 | NumberOfPages &= ~(EFI_SIZE_TO_PAGES (Alignment) - 1);\r | |
1436 | \r | |
925f0d1a SZ |
1437 | if (MemoryType != NULL) {\r |
1438 | *MemoryType = Entry->Type;\r | |
1439 | }\r | |
1440 | \r | |
28a00297 | 1441 | Status = CoreConvertPages (Memory, NumberOfPages, EfiConventionalMemory);\r |
1442 | \r | |
28a00297 | 1443 | if (EFI_ERROR (Status)) {\r |
a5ca8fa7 | 1444 | goto Done;\r |
28a00297 | 1445 | }\r |
1446 | \r | |
a5ca8fa7 | 1447 | Done:\r |
1448 | CoreReleaseMemoryLock ();\r | |
28a00297 | 1449 | return Status;\r |
1450 | }\r | |
1451 | \r | |
84edd20b SZ |
1452 | /**\r |
1453 | Frees previous allocated pages.\r | |
1454 | \r | |
1455 | @param Memory Base address of memory being freed\r | |
1456 | @param NumberOfPages The number of pages to free\r | |
1457 | \r | |
1458 | @retval EFI_NOT_FOUND Could not find the entry that covers the range\r | |
1459 | @retval EFI_INVALID_PARAMETER Address not aligned\r | |
1460 | @return EFI_SUCCESS -Pages successfully freed.\r | |
1461 | \r | |
1462 | **/\r | |
1463 | EFI_STATUS\r | |
1464 | EFIAPI\r | |
1465 | CoreFreePages (\r | |
1466 | IN EFI_PHYSICAL_ADDRESS Memory,\r | |
1467 | IN UINTN NumberOfPages\r | |
1468 | )\r | |
1469 | {\r | |
925f0d1a SZ |
1470 | EFI_STATUS Status;\r |
1471 | EFI_MEMORY_TYPE MemoryType;\r | |
736a692e | 1472 | \r |
925f0d1a | 1473 | Status = CoreInternalFreePages (Memory, NumberOfPages, &MemoryType);\r |
736a692e | 1474 | if (!EFI_ERROR (Status)) {\r |
1d60fe96 SZ |
1475 | CoreUpdateProfile (\r |
1476 | (EFI_PHYSICAL_ADDRESS) (UINTN) RETURN_ADDRESS (0),\r | |
1477 | MemoryProfileActionFreePages,\r | |
1478 | MemoryType,\r | |
1479 | EFI_PAGES_TO_SIZE (NumberOfPages),\r | |
1480 | (VOID *) (UINTN) Memory,\r | |
1481 | NULL\r | |
1482 | );\r | |
74a88770 | 1483 | InstallMemoryAttributesTableOnMemoryAllocation (MemoryType);\r |
7eb927db AB |
1484 | ApplyMemoryProtectionPolicy (MemoryType, EfiConventionalMemory, Memory,\r |
1485 | EFI_PAGES_TO_SIZE (NumberOfPages));\r | |
736a692e HT |
1486 | }\r |
1487 | return Status;\r | |
1488 | }\r | |
84edd20b | 1489 | \r |
2345e7d4 | 1490 | /**\r |
1491 | This function checks to see if the last memory map descriptor in a memory map\r | |
1492 | can be merged with any of the other memory map descriptors in a memorymap.\r | |
1493 | Memory descriptors may be merged if they are adjacent and have the same type\r | |
1494 | and attributes.\r | |
1495 | \r | |
1496 | @param MemoryMap A pointer to the start of the memory map.\r | |
1497 | @param MemoryMapDescriptor A pointer to the last descriptor in MemoryMap.\r | |
1498 | @param DescriptorSize The size, in bytes, of an individual\r | |
1499 | EFI_MEMORY_DESCRIPTOR.\r | |
1500 | \r | |
1501 | @return A pointer to the next available descriptor in MemoryMap\r | |
1502 | \r | |
1503 | **/\r | |
1504 | EFI_MEMORY_DESCRIPTOR *\r | |
1505 | MergeMemoryMapDescriptor (\r | |
1506 | IN EFI_MEMORY_DESCRIPTOR *MemoryMap,\r | |
1507 | IN EFI_MEMORY_DESCRIPTOR *MemoryMapDescriptor,\r | |
1508 | IN UINTN DescriptorSize\r | |
1509 | )\r | |
1510 | {\r | |
1511 | //\r | |
1512 | // Traverse the array of descriptors in MemoryMap\r | |
1513 | //\r | |
1514 | for (; MemoryMap != MemoryMapDescriptor; MemoryMap = NEXT_MEMORY_DESCRIPTOR (MemoryMap, DescriptorSize)) {\r | |
1515 | //\r | |
1516 | // Check to see if the Type fields are identical.\r | |
1517 | //\r | |
1518 | if (MemoryMap->Type != MemoryMapDescriptor->Type) {\r | |
1519 | continue;\r | |
1520 | }\r | |
1521 | \r | |
1522 | //\r | |
1523 | // Check to see if the Attribute fields are identical.\r | |
1524 | //\r | |
1525 | if (MemoryMap->Attribute != MemoryMapDescriptor->Attribute) {\r | |
1526 | continue;\r | |
1527 | }\r | |
1528 | \r | |
1529 | //\r | |
1530 | // Check to see if MemoryMapDescriptor is immediately above MemoryMap\r | |
1531 | //\r | |
1532 | if (MemoryMap->PhysicalStart + EFI_PAGES_TO_SIZE ((UINTN)MemoryMap->NumberOfPages) == MemoryMapDescriptor->PhysicalStart) { \r | |
1533 | //\r | |
1534 | // Merge MemoryMapDescriptor into MemoryMap\r | |
1535 | //\r | |
1536 | MemoryMap->NumberOfPages += MemoryMapDescriptor->NumberOfPages;\r | |
1537 | \r | |
1538 | //\r | |
1539 | // Return MemoryMapDescriptor as the next available slot int he MemoryMap array\r | |
1540 | //\r | |
1541 | return MemoryMapDescriptor;\r | |
1542 | }\r | |
1543 | \r | |
1544 | //\r | |
1545 | // Check to see if MemoryMapDescriptor is immediately below MemoryMap\r | |
1546 | //\r | |
1547 | if (MemoryMap->PhysicalStart - EFI_PAGES_TO_SIZE ((UINTN)MemoryMapDescriptor->NumberOfPages) == MemoryMapDescriptor->PhysicalStart) {\r | |
1548 | //\r | |
1549 | // Merge MemoryMapDescriptor into MemoryMap\r | |
1550 | //\r | |
1551 | MemoryMap->PhysicalStart = MemoryMapDescriptor->PhysicalStart;\r | |
1552 | MemoryMap->VirtualStart = MemoryMapDescriptor->VirtualStart;\r | |
1553 | MemoryMap->NumberOfPages += MemoryMapDescriptor->NumberOfPages;\r | |
1554 | \r | |
1555 | //\r | |
1556 | // Return MemoryMapDescriptor as the next available slot int he MemoryMap array\r | |
1557 | //\r | |
1558 | return MemoryMapDescriptor;\r | |
1559 | }\r | |
1560 | }\r | |
1561 | \r | |
1562 | //\r | |
1563 | // MemoryMapDescrtiptor could not be merged with any descriptors in MemoryMap.\r | |
1564 | //\r | |
1565 | // Return the slot immediately after MemoryMapDescriptor as the next available \r | |
1566 | // slot in the MemoryMap array\r | |
1567 | //\r | |
1568 | return NEXT_MEMORY_DESCRIPTOR (MemoryMapDescriptor, DescriptorSize);\r | |
1569 | }\r | |
28a00297 | 1570 | \r |
162ed594 | 1571 | /**\r |
1572 | This function returns a copy of the current memory map. The map is an array of\r | |
1573 | memory descriptors, each of which describes a contiguous block of memory.\r | |
1574 | \r | |
022c6d45 | 1575 | @param MemoryMapSize A pointer to the size, in bytes, of the\r |
1576 | MemoryMap buffer. On input, this is the size of\r | |
1577 | the buffer allocated by the caller. On output,\r | |
1578 | it is the size of the buffer returned by the\r | |
1579 | firmware if the buffer was large enough, or the\r | |
1580 | size of the buffer needed to contain the map if\r | |
1581 | the buffer was too small.\r | |
1582 | @param MemoryMap A pointer to the buffer in which firmware places\r | |
1583 | the current memory map.\r | |
1584 | @param MapKey A pointer to the location in which firmware\r | |
1585 | returns the key for the current memory map.\r | |
1586 | @param DescriptorSize A pointer to the location in which firmware\r | |
1587 | returns the size, in bytes, of an individual\r | |
1588 | EFI_MEMORY_DESCRIPTOR.\r | |
1589 | @param DescriptorVersion A pointer to the location in which firmware\r | |
1590 | returns the version number associated with the\r | |
1591 | EFI_MEMORY_DESCRIPTOR.\r | |
1592 | \r | |
1593 | @retval EFI_SUCCESS The memory map was returned in the MemoryMap\r | |
1594 | buffer.\r | |
1595 | @retval EFI_BUFFER_TOO_SMALL The MemoryMap buffer was too small. The current\r | |
1596 | buffer size needed to hold the memory map is\r | |
1597 | returned in MemoryMapSize.\r | |
162ed594 | 1598 | @retval EFI_INVALID_PARAMETER One of the parameters has an invalid value.\r |
1599 | \r | |
1600 | **/\r | |
28a00297 | 1601 | EFI_STATUS\r |
1602 | EFIAPI\r | |
1603 | CoreGetMemoryMap (\r | |
1604 | IN OUT UINTN *MemoryMapSize,\r | |
1605 | IN OUT EFI_MEMORY_DESCRIPTOR *MemoryMap,\r | |
1606 | OUT UINTN *MapKey,\r | |
1607 | OUT UINTN *DescriptorSize,\r | |
1608 | OUT UINT32 *DescriptorVersion\r | |
1609 | )\r | |
28a00297 | 1610 | {\r |
1611 | EFI_STATUS Status;\r | |
022c6d45 | 1612 | UINTN Size;\r |
1613 | UINTN BufferSize;\r | |
ba2c0527 | 1614 | UINTN NumberOfEntries;\r |
28a00297 | 1615 | LIST_ENTRY *Link;\r |
022c6d45 | 1616 | MEMORY_MAP *Entry;\r |
1617 | EFI_GCD_MAP_ENTRY *GcdMapEntry;\r | |
46a65f18 | 1618 | EFI_GCD_MAP_ENTRY MergeGcdMapEntry;\r |
b74350e9 | 1619 | EFI_MEMORY_TYPE Type;\r |
2345e7d4 | 1620 | EFI_MEMORY_DESCRIPTOR *MemoryMapStart;\r |
28a00297 | 1621 | \r |
1622 | //\r | |
1623 | // Make sure the parameters are valid\r | |
1624 | //\r | |
1625 | if (MemoryMapSize == NULL) {\r | |
1626 | return EFI_INVALID_PARAMETER;\r | |
1627 | }\r | |
022c6d45 | 1628 | \r |
28a00297 | 1629 | CoreAcquireGcdMemoryLock ();\r |
022c6d45 | 1630 | \r |
28a00297 | 1631 | //\r |
ba2c0527 | 1632 | // Count the number of Reserved and runtime MMIO entries\r |
a671a012 | 1633 | // And, count the number of Persistent entries.\r |
28a00297 | 1634 | //\r |
ba2c0527 | 1635 | NumberOfEntries = 0;\r |
28a00297 | 1636 | for (Link = mGcdMemorySpaceMap.ForwardLink; Link != &mGcdMemorySpaceMap; Link = Link->ForwardLink) {\r |
1637 | GcdMapEntry = CR (Link, EFI_GCD_MAP_ENTRY, Link, EFI_GCD_MAP_SIGNATURE);\r | |
ba2c0527 LG |
1638 | if ((GcdMapEntry->GcdMemoryType == EfiGcdMemoryTypePersistentMemory) || \r |
1639 | (GcdMapEntry->GcdMemoryType == EfiGcdMemoryTypeReserved) ||\r | |
1640 | ((GcdMapEntry->GcdMemoryType == EfiGcdMemoryTypeMemoryMappedIo) &&\r | |
1641 | ((GcdMapEntry->Attributes & EFI_MEMORY_RUNTIME) == EFI_MEMORY_RUNTIME))) {\r | |
1642 | NumberOfEntries ++;\r | |
a671a012 | 1643 | }\r |
28a00297 | 1644 | }\r |
1645 | \r | |
1646 | Size = sizeof (EFI_MEMORY_DESCRIPTOR);\r | |
1647 | \r | |
1648 | //\r | |
1649 | // Make sure Size != sizeof(EFI_MEMORY_DESCRIPTOR). This will\r | |
1650 | // prevent people from having pointer math bugs in their code.\r | |
1651 | // now you have to use *DescriptorSize to make things work.\r | |
1652 | //\r | |
1653 | Size += sizeof(UINT64) - (Size % sizeof (UINT64));\r | |
1654 | \r | |
1655 | if (DescriptorSize != NULL) {\r | |
1656 | *DescriptorSize = Size;\r | |
1657 | }\r | |
022c6d45 | 1658 | \r |
28a00297 | 1659 | if (DescriptorVersion != NULL) {\r |
1660 | *DescriptorVersion = EFI_MEMORY_DESCRIPTOR_VERSION;\r | |
1661 | }\r | |
1662 | \r | |
1663 | CoreAcquireMemoryLock ();\r | |
1664 | \r | |
1665 | //\r | |
1666 | // Compute the buffer size needed to fit the entire map\r | |
1667 | //\r | |
ba2c0527 | 1668 | BufferSize = Size * NumberOfEntries;\r |
28a00297 | 1669 | for (Link = gMemoryMap.ForwardLink; Link != &gMemoryMap; Link = Link->ForwardLink) {\r |
1670 | BufferSize += Size;\r | |
1671 | }\r | |
1672 | \r | |
1673 | if (*MemoryMapSize < BufferSize) {\r | |
1674 | Status = EFI_BUFFER_TOO_SMALL;\r | |
1675 | goto Done;\r | |
1676 | }\r | |
1677 | \r | |
1678 | if (MemoryMap == NULL) {\r | |
1679 | Status = EFI_INVALID_PARAMETER;\r | |
1680 | goto Done;\r | |
1681 | }\r | |
1682 | \r | |
1683 | //\r | |
1684 | // Build the map\r | |
1685 | //\r | |
383c303c | 1686 | ZeroMem (MemoryMap, BufferSize);\r |
2345e7d4 | 1687 | MemoryMapStart = MemoryMap;\r |
28a00297 | 1688 | for (Link = gMemoryMap.ForwardLink; Link != &gMemoryMap; Link = Link->ForwardLink) {\r |
1689 | Entry = CR (Link, MEMORY_MAP, Link, MEMORY_MAP_SIGNATURE);\r | |
1690 | ASSERT (Entry->VirtualStart == 0);\r | |
1691 | \r | |
b74350e9 | 1692 | //\r |
1693 | // Convert internal map into an EFI_MEMORY_DESCRIPTOR\r | |
1694 | //\r | |
28a00297 | 1695 | MemoryMap->Type = Entry->Type;\r |
1696 | MemoryMap->PhysicalStart = Entry->Start;\r | |
1697 | MemoryMap->VirtualStart = Entry->VirtualStart;\r | |
1698 | MemoryMap->NumberOfPages = RShiftU64 (Entry->End - Entry->Start + 1, EFI_PAGE_SHIFT);\r | |
b74350e9 | 1699 | //\r |
1700 | // If the memory type is EfiConventionalMemory, then determine if the range is part of a\r | |
022c6d45 | 1701 | // memory type bin and needs to be converted to the same memory type as the rest of the\r |
1702 | // memory type bin in order to minimize EFI Memory Map changes across reboots. This\r | |
b74350e9 | 1703 | // improves the chances for a successful S4 resume in the presence of minor page allocation\r |
1704 | // differences across reboots.\r | |
1705 | //\r | |
1706 | if (MemoryMap->Type == EfiConventionalMemory) {\r | |
1707 | for (Type = (EFI_MEMORY_TYPE) 0; Type < EfiMaxMemoryType; Type++) {\r | |
1708 | if (mMemoryTypeStatistics[Type].Special &&\r | |
1709 | mMemoryTypeStatistics[Type].NumberOfPages > 0 &&\r | |
1710 | Entry->Start >= mMemoryTypeStatistics[Type].BaseAddress &&\r | |
e94a9ff7 | 1711 | Entry->End <= mMemoryTypeStatistics[Type].MaximumAddress) {\r |
b74350e9 | 1712 | MemoryMap->Type = Type;\r |
1713 | }\r | |
1714 | }\r | |
1715 | }\r | |
1716 | MemoryMap->Attribute = Entry->Attribute;\r | |
10fe0d81 RN |
1717 | if (MemoryMap->Type < EfiMaxMemoryType) {\r |
1718 | if (mMemoryTypeStatistics[MemoryMap->Type].Runtime) {\r | |
1719 | MemoryMap->Attribute |= EFI_MEMORY_RUNTIME;\r | |
1720 | }\r | |
28a00297 | 1721 | }\r |
022c6d45 | 1722 | \r |
2345e7d4 | 1723 | //\r |
1724 | // Check to see if the new Memory Map Descriptor can be merged with an \r | |
1725 | // existing descriptor if they are adjacent and have the same attributes\r | |
1726 | //\r | |
1727 | MemoryMap = MergeMemoryMapDescriptor (MemoryMapStart, MemoryMap, Size);\r | |
28a00297 | 1728 | }\r |
1729 | \r | |
46a65f18 LG |
1730 | \r |
1731 | ZeroMem (&MergeGcdMapEntry, sizeof (MergeGcdMapEntry));\r | |
1732 | GcdMapEntry = NULL;\r | |
1733 | for (Link = mGcdMemorySpaceMap.ForwardLink; ; Link = Link->ForwardLink) {\r | |
1734 | if (Link != &mGcdMemorySpaceMap) {\r | |
1735 | //\r | |
1736 | // Merge adjacent same type and attribute GCD memory range\r | |
1737 | //\r | |
1738 | GcdMapEntry = CR (Link, EFI_GCD_MAP_ENTRY, Link, EFI_GCD_MAP_SIGNATURE);\r | |
1739 | \r | |
1740 | if ((MergeGcdMapEntry.Capabilities == GcdMapEntry->Capabilities) && \r | |
1741 | (MergeGcdMapEntry.Attributes == GcdMapEntry->Attributes) &&\r | |
1742 | (MergeGcdMapEntry.GcdMemoryType == GcdMapEntry->GcdMemoryType) &&\r | |
1743 | (MergeGcdMapEntry.GcdIoType == GcdMapEntry->GcdIoType)) {\r | |
1744 | MergeGcdMapEntry.EndAddress = GcdMapEntry->EndAddress;\r | |
1745 | continue;\r | |
1746 | }\r | |
1747 | }\r | |
1748 | \r | |
1749 | if ((MergeGcdMapEntry.GcdMemoryType == EfiGcdMemoryTypeReserved) ||\r | |
1750 | ((MergeGcdMapEntry.GcdMemoryType == EfiGcdMemoryTypeMemoryMappedIo) &&\r | |
1751 | ((MergeGcdMapEntry.Attributes & EFI_MEMORY_RUNTIME) == EFI_MEMORY_RUNTIME))) {\r | |
1752 | //\r | |
1753 | // Page Align GCD range is required. When it is converted to EFI_MEMORY_DESCRIPTOR, \r | |
1754 | // it will be recorded as page PhysicalStart and NumberOfPages. \r | |
1755 | //\r | |
1756 | ASSERT ((MergeGcdMapEntry.BaseAddress & EFI_PAGE_MASK) == 0);\r | |
1757 | ASSERT (((MergeGcdMapEntry.EndAddress - MergeGcdMapEntry.BaseAddress + 1) & EFI_PAGE_MASK) == 0);\r | |
1758 | \r | |
ba2c0527 LG |
1759 | // \r |
1760 | // Create EFI_MEMORY_DESCRIPTOR for every Reserved and runtime MMIO GCD entries\r | |
1761 | //\r | |
46a65f18 | 1762 | MemoryMap->PhysicalStart = MergeGcdMapEntry.BaseAddress;\r |
ba2c0527 | 1763 | MemoryMap->VirtualStart = 0;\r |
46a65f18 LG |
1764 | MemoryMap->NumberOfPages = RShiftU64 ((MergeGcdMapEntry.EndAddress - MergeGcdMapEntry.BaseAddress + 1), EFI_PAGE_SHIFT);\r |
1765 | MemoryMap->Attribute = (MergeGcdMapEntry.Attributes & ~EFI_MEMORY_PORT_IO) | \r | |
1766 | (MergeGcdMapEntry.Capabilities & (EFI_MEMORY_RP | EFI_MEMORY_WP | EFI_MEMORY_XP | EFI_MEMORY_RO |\r | |
ff0c6d66 | 1767 | EFI_MEMORY_UC | EFI_MEMORY_UCE | EFI_MEMORY_WC | EFI_MEMORY_WT | EFI_MEMORY_WB));\r |
ba2c0527 | 1768 | \r |
46a65f18 | 1769 | if (MergeGcdMapEntry.GcdMemoryType == EfiGcdMemoryTypeReserved) {\r |
ba2c0527 | 1770 | MemoryMap->Type = EfiReservedMemoryType;\r |
46a65f18 LG |
1771 | } else if (MergeGcdMapEntry.GcdMemoryType == EfiGcdMemoryTypeMemoryMappedIo) {\r |
1772 | if ((MergeGcdMapEntry.Attributes & EFI_MEMORY_PORT_IO) == EFI_MEMORY_PORT_IO) {\r | |
ba2c0527 LG |
1773 | MemoryMap->Type = EfiMemoryMappedIOPortSpace;\r |
1774 | } else {\r | |
1775 | MemoryMap->Type = EfiMemoryMappedIO;\r | |
28a00297 | 1776 | }\r |
28a00297 | 1777 | }\r |
ba2c0527 LG |
1778 | \r |
1779 | //\r | |
1780 | // Check to see if the new Memory Map Descriptor can be merged with an \r | |
1781 | // existing descriptor if they are adjacent and have the same attributes\r | |
1782 | //\r | |
1783 | MemoryMap = MergeMemoryMapDescriptor (MemoryMapStart, MemoryMap, Size);\r | |
28a00297 | 1784 | }\r |
a671a012 | 1785 | \r |
46a65f18 LG |
1786 | if (MergeGcdMapEntry.GcdMemoryType == EfiGcdMemoryTypePersistentMemory) {\r |
1787 | //\r | |
1788 | // Page Align GCD range is required. When it is converted to EFI_MEMORY_DESCRIPTOR, \r | |
1789 | // it will be recorded as page PhysicalStart and NumberOfPages. \r | |
1790 | //\r | |
1791 | ASSERT ((MergeGcdMapEntry.BaseAddress & EFI_PAGE_MASK) == 0);\r | |
1792 | ASSERT (((MergeGcdMapEntry.EndAddress - MergeGcdMapEntry.BaseAddress + 1) & EFI_PAGE_MASK) == 0);\r | |
1793 | \r | |
a671a012 LG |
1794 | // \r |
1795 | // Create EFI_MEMORY_DESCRIPTOR for every Persistent GCD entries\r | |
1796 | //\r | |
46a65f18 | 1797 | MemoryMap->PhysicalStart = MergeGcdMapEntry.BaseAddress;\r |
a671a012 | 1798 | MemoryMap->VirtualStart = 0;\r |
46a65f18 LG |
1799 | MemoryMap->NumberOfPages = RShiftU64 ((MergeGcdMapEntry.EndAddress - MergeGcdMapEntry.BaseAddress + 1), EFI_PAGE_SHIFT);\r |
1800 | MemoryMap->Attribute = MergeGcdMapEntry.Attributes | EFI_MEMORY_NV | \r | |
1801 | (MergeGcdMapEntry.Capabilities & (EFI_MEMORY_RP | EFI_MEMORY_WP | EFI_MEMORY_XP | EFI_MEMORY_RO |\r | |
ff0c6d66 | 1802 | EFI_MEMORY_UC | EFI_MEMORY_UCE | EFI_MEMORY_WC | EFI_MEMORY_WT | EFI_MEMORY_WB));\r |
a671a012 LG |
1803 | MemoryMap->Type = EfiPersistentMemory;\r |
1804 | \r | |
1805 | //\r | |
1806 | // Check to see if the new Memory Map Descriptor can be merged with an \r | |
1807 | // existing descriptor if they are adjacent and have the same attributes\r | |
1808 | //\r | |
1809 | MemoryMap = MergeMemoryMapDescriptor (MemoryMapStart, MemoryMap, Size);\r | |
1810 | }\r | |
46a65f18 LG |
1811 | if (Link == &mGcdMemorySpaceMap) {\r |
1812 | //\r | |
1813 | // break loop when arrive at head.\r | |
1814 | //\r | |
1815 | break;\r | |
1816 | }\r | |
1817 | if (GcdMapEntry != NULL) {\r | |
1818 | //\r | |
1819 | // Copy new GCD map entry for the following GCD range merge\r | |
1820 | //\r | |
1821 | CopyMem (&MergeGcdMapEntry, GcdMapEntry, sizeof (MergeGcdMapEntry));\r | |
1822 | }\r | |
28a00297 | 1823 | }\r |
022c6d45 | 1824 | \r |
2345e7d4 | 1825 | //\r |
1826 | // Compute the size of the buffer actually used after all memory map descriptor merge operations\r | |
1827 | //\r | |
1828 | BufferSize = ((UINT8 *)MemoryMap - (UINT8 *)MemoryMapStart);\r | |
1829 | \r | |
28a00297 | 1830 | Status = EFI_SUCCESS;\r |
1831 | \r | |
1832 | Done:\r | |
022c6d45 | 1833 | //\r |
1834 | // Update the map key finally\r | |
1835 | //\r | |
28a00297 | 1836 | if (MapKey != NULL) {\r |
1837 | *MapKey = mMemoryMapKey;\r | |
1838 | }\r | |
022c6d45 | 1839 | \r |
e439df50 | 1840 | CoreReleaseMemoryLock ();\r |
1841 | \r | |
1842 | CoreReleaseGcdMemoryLock ();\r | |
1843 | \r | |
28a00297 | 1844 | *MemoryMapSize = BufferSize;\r |
022c6d45 | 1845 | \r |
28a00297 | 1846 | return Status;\r |
1847 | }\r | |
1848 | \r | |
28a00297 | 1849 | \r |
162ed594 | 1850 | /**\r |
28a00297 | 1851 | Internal function. Used by the pool functions to allocate pages\r |
1852 | to back pool allocation requests.\r | |
1853 | \r | |
022c6d45 | 1854 | @param PoolType The type of memory for the new pool pages\r |
1855 | @param NumberOfPages No of pages to allocate\r | |
1856 | @param Alignment Bits to align.\r | |
28a00297 | 1857 | \r |
162ed594 | 1858 | @return The allocated memory, or NULL\r |
28a00297 | 1859 | \r |
162ed594 | 1860 | **/\r |
1861 | VOID *\r | |
1862 | CoreAllocatePoolPages (\r | |
1863 | IN EFI_MEMORY_TYPE PoolType,\r | |
1864 | IN UINTN NumberOfPages,\r | |
1865 | IN UINTN Alignment\r | |
1866 | )\r | |
28a00297 | 1867 | {\r |
1868 | UINT64 Start;\r | |
1869 | \r | |
1870 | //\r | |
1871 | // Find the pages to convert\r | |
1872 | //\r | |
f3f2e05d | 1873 | Start = FindFreePages (MAX_ADDRESS, NumberOfPages, PoolType, Alignment);\r |
28a00297 | 1874 | \r |
1875 | //\r | |
1876 | // Convert it to boot services data\r | |
1877 | //\r | |
1878 | if (Start == 0) {\r | |
7df7393f | 1879 | DEBUG ((DEBUG_ERROR | DEBUG_PAGE, "AllocatePoolPages: failed to allocate %d pages\n", (UINT32)NumberOfPages));\r |
28a00297 | 1880 | } else {\r |
1881 | CoreConvertPages (Start, NumberOfPages, PoolType);\r | |
1882 | }\r | |
1883 | \r | |
e94a9ff7 | 1884 | return (VOID *)(UINTN) Start;\r |
28a00297 | 1885 | }\r |
1886 | \r | |
162ed594 | 1887 | \r |
1888 | /**\r | |
1889 | Internal function. Frees pool pages allocated via AllocatePoolPages ()\r | |
1890 | \r | |
022c6d45 | 1891 | @param Memory The base address to free\r |
162ed594 | 1892 | @param NumberOfPages The number of pages to free\r |
1893 | \r | |
1894 | **/\r | |
28a00297 | 1895 | VOID\r |
1896 | CoreFreePoolPages (\r | |
1897 | IN EFI_PHYSICAL_ADDRESS Memory,\r | |
1898 | IN UINTN NumberOfPages\r | |
1899 | )\r | |
28a00297 | 1900 | {\r |
1901 | CoreConvertPages (Memory, NumberOfPages, EfiConventionalMemory);\r | |
1902 | }\r | |
1903 | \r | |
1904 | \r | |
28a00297 | 1905 | \r |
162ed594 | 1906 | /**\r |
1907 | Make sure the memory map is following all the construction rules,\r | |
28a00297 | 1908 | it is the last time to check memory map error before exit boot services.\r |
1909 | \r | |
022c6d45 | 1910 | @param MapKey Memory map key\r |
28a00297 | 1911 | \r |
022c6d45 | 1912 | @retval EFI_INVALID_PARAMETER Memory map not consistent with construction\r |
1913 | rules.\r | |
162ed594 | 1914 | @retval EFI_SUCCESS Valid memory map.\r |
28a00297 | 1915 | \r |
162ed594 | 1916 | **/\r |
1917 | EFI_STATUS\r | |
1918 | CoreTerminateMemoryMap (\r | |
1919 | IN UINTN MapKey\r | |
1920 | )\r | |
28a00297 | 1921 | {\r |
1922 | EFI_STATUS Status;\r | |
1923 | LIST_ENTRY *Link;\r | |
1924 | MEMORY_MAP *Entry;\r | |
1925 | \r | |
1926 | Status = EFI_SUCCESS;\r | |
1927 | \r | |
1928 | CoreAcquireMemoryLock ();\r | |
1929 | \r | |
1930 | if (MapKey == mMemoryMapKey) {\r | |
1931 | \r | |
1932 | //\r | |
1933 | // Make sure the memory map is following all the construction rules\r | |
1934 | // This is the last chance we will be able to display any messages on\r | |
1935 | // the console devices.\r | |
1936 | //\r | |
1937 | \r | |
1938 | for (Link = gMemoryMap.ForwardLink; Link != &gMemoryMap; Link = Link->ForwardLink) {\r | |
1939 | Entry = CR(Link, MEMORY_MAP, Link, MEMORY_MAP_SIGNATURE);\r | |
7d17a6a1 AB |
1940 | if (Entry->Type < EfiMaxMemoryType) {\r |
1941 | if (mMemoryTypeStatistics[Entry->Type].Runtime) {\r | |
1942 | ASSERT (Entry->Type != EfiACPIReclaimMemory);\r | |
1943 | ASSERT (Entry->Type != EfiACPIMemoryNVS);\r | |
d4731a98 | 1944 | if ((Entry->Start & (RUNTIME_PAGE_ALLOCATION_GRANULARITY - 1)) != 0) {\r |
7d17a6a1 AB |
1945 | DEBUG((DEBUG_ERROR | DEBUG_PAGE, "ExitBootServices: A RUNTIME memory entry is not on a proper alignment.\n"));\r |
1946 | Status = EFI_INVALID_PARAMETER;\r | |
1947 | goto Done;\r | |
1948 | }\r | |
d4731a98 | 1949 | if (((Entry->End + 1) & (RUNTIME_PAGE_ALLOCATION_GRANULARITY - 1)) != 0) {\r |
7d17a6a1 AB |
1950 | DEBUG((DEBUG_ERROR | DEBUG_PAGE, "ExitBootServices: A RUNTIME memory entry is not on a proper alignment.\n"));\r |
1951 | Status = EFI_INVALID_PARAMETER;\r | |
1952 | goto Done;\r | |
1953 | }\r | |
28a00297 | 1954 | }\r |
1955 | }\r | |
1956 | }\r | |
1957 | \r | |
1958 | //\r | |
1959 | // The map key they gave us matches what we expect. Fall through and\r | |
1960 | // return success. In an ideal world we would clear out all of\r | |
1961 | // EfiBootServicesCode and EfiBootServicesData. However this function\r | |
1962 | // is not the last one called by ExitBootServices(), so we have to\r | |
1963 | // preserve the memory contents.\r | |
1964 | //\r | |
1965 | } else {\r | |
1966 | Status = EFI_INVALID_PARAMETER;\r | |
1967 | }\r | |
1968 | \r | |
d45fd260 | 1969 | Done:\r |
28a00297 | 1970 | CoreReleaseMemoryLock ();\r |
1971 | \r | |
1972 | return Status;\r | |
1973 | }\r | |
1974 | \r | |
1975 | \r | |
1976 | \r | |
1977 | \r | |
1978 | \r | |
1979 | \r | |
1980 | \r | |
1981 | \r | |
162ed594 | 1982 | \r |