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