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