]> git.proxmox.com Git - mirror_edk2.git/blame - MdeModulePkg/Core/Dxe/Mem/Page.c
Add comments and DoxyGen format for these files.
[mirror_edk2.git] / MdeModulePkg / Core / Dxe / Mem / Page.c
CommitLineData
28a00297 1/*++\r
2\r
3Copyright (c) 2007, Intel Corporation \r
4All rights reserved. This program and the accompanying materials \r
5are licensed and made available under the terms and conditions of the BSD License \r
6which accompanies this distribution. The full text of the license may be found at \r
7http://opensource.org/licenses/bsd-license.php \r
8 \r
9THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS, \r
10WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. \r
11\r
12Module Name:\r
13\r
14 page.c\r
15\r
16Abstract:\r
17\r
18 EFI Memory page management\r
19\r
20\r
21Revision History\r
22\r
23--*/\r
24\r
25#include <DxeMain.h>\r
26\r
27#define EFI_DEFAULT_PAGE_ALLOCATION_ALIGNMENT (EFI_PAGE_SIZE)\r
28\r
29//\r
30// Entry for tracking the memory regions for each memory type to help cooalese like memory types\r
31//\r
32typedef struct {\r
33 EFI_PHYSICAL_ADDRESS BaseAddress;\r
34 EFI_PHYSICAL_ADDRESS MaximumAddress;\r
35 UINT64 CurrentNumberOfPages;\r
b74350e9 36 UINT64 NumberOfPages;\r
28a00297 37 UINTN InformationIndex;\r
b74350e9 38 BOOLEAN Special;\r
39 BOOLEAN Runtime;\r
28a00297 40} EFI_MEMORY_TYPE_STAISTICS;\r
41\r
42//\r
43// MemoryMap - The current memory map\r
44//\r
45UINTN mMemoryMapKey = 0;\r
46\r
47//\r
48// mMapStack - space to use as temp storage to build new map descriptors\r
49// mMapDepth - depth of new descriptor stack\r
50//\r
51\r
52#define MAX_MAP_DEPTH 6\r
53UINTN mMapDepth = 0;\r
54MEMORY_MAP mMapStack[MAX_MAP_DEPTH];\r
55UINTN mFreeMapStack = 0;\r
56//\r
57// This list maintain the free memory map list\r
58//\r
59LIST_ENTRY mFreeMemoryMapEntryList = INITIALIZE_LIST_HEAD_VARIABLE (mFreeMemoryMapEntryList);\r
60BOOLEAN mMemoryTypeInformationInitialized = FALSE;\r
61\r
62EFI_MEMORY_TYPE_STAISTICS mMemoryTypeStatistics[EfiMaxMemoryType + 1] = {\r
b74350e9 63 { 0, EFI_MAX_ADDRESS, 0, 0, EfiMaxMemoryType, TRUE, FALSE }, // EfiReservedMemoryType\r
64 { 0, EFI_MAX_ADDRESS, 0, 0, EfiMaxMemoryType, FALSE, FALSE }, // EfiLoaderCode\r
65 { 0, EFI_MAX_ADDRESS, 0, 0, EfiMaxMemoryType, FALSE, FALSE }, // EfiLoaderData\r
66 { 0, EFI_MAX_ADDRESS, 0, 0, EfiMaxMemoryType, FALSE, FALSE }, // EfiBootServicesCode\r
67 { 0, EFI_MAX_ADDRESS, 0, 0, EfiMaxMemoryType, FALSE, FALSE }, // EfiBootServicesData\r
68 { 0, EFI_MAX_ADDRESS, 0, 0, EfiMaxMemoryType, TRUE, TRUE }, // EfiRuntimeServicesCode\r
69 { 0, EFI_MAX_ADDRESS, 0, 0, EfiMaxMemoryType, TRUE, TRUE }, // EfiRuntimeServicesData\r
70 { 0, EFI_MAX_ADDRESS, 0, 0, EfiMaxMemoryType, FALSE, FALSE }, // EfiConventionalMemory\r
71 { 0, EFI_MAX_ADDRESS, 0, 0, EfiMaxMemoryType, FALSE, FALSE }, // EfiUnusableMemory\r
72 { 0, EFI_MAX_ADDRESS, 0, 0, EfiMaxMemoryType, TRUE, FALSE }, // EfiACPIReclaimMemory\r
73 { 0, EFI_MAX_ADDRESS, 0, 0, EfiMaxMemoryType, TRUE, FALSE }, // EfiACPIMemoryNVS\r
74 { 0, EFI_MAX_ADDRESS, 0, 0, EfiMaxMemoryType, FALSE, FALSE }, // EfiMemoryMappedIO\r
75 { 0, EFI_MAX_ADDRESS, 0, 0, EfiMaxMemoryType, FALSE, FALSE }, // EfiMemoryMappedIOPortSpace\r
76 { 0, EFI_MAX_ADDRESS, 0, 0, EfiMaxMemoryType, TRUE, TRUE }, // EfiPalCode\r
77 { 0, EFI_MAX_ADDRESS, 0, 0, EfiMaxMemoryType, FALSE, FALSE } // EfiMaxMemoryType\r
28a00297 78};\r
79\r
80EFI_PHYSICAL_ADDRESS mDefaultMaximumAddress = EFI_MAX_ADDRESS;\r
81\r
82EFI_MEMORY_TYPE_INFORMATION gMemoryTypeInformation[EfiMaxMemoryType + 1] = {\r
83 { EfiReservedMemoryType, 0 },\r
84 { EfiLoaderCode, 0 },\r
85 { EfiLoaderData, 0 },\r
86 { EfiBootServicesCode, 0 },\r
87 { EfiBootServicesData, 0 },\r
88 { EfiRuntimeServicesCode, 0 },\r
89 { EfiRuntimeServicesData, 0 },\r
90 { EfiConventionalMemory, 0 },\r
91 { EfiUnusableMemory, 0 },\r
92 { EfiACPIReclaimMemory, 0 },\r
93 { EfiACPIMemoryNVS, 0 },\r
94 { EfiMemoryMappedIO, 0 },\r
95 { EfiMemoryMappedIOPortSpace, 0 },\r
96 { EfiPalCode, 0 },\r
97 { EfiMaxMemoryType, 0 }\r
98};\r
99\r
100//\r
101// Internal prototypes\r
102//\r
103STATIC\r
104VOID \r
105PromoteMemoryResource (\r
106 VOID\r
107);\r
108\r
109STATIC\r
110VOID\r
111CoreAddRange (\r
112 IN EFI_MEMORY_TYPE Type,\r
113 IN EFI_PHYSICAL_ADDRESS Start,\r
114 IN EFI_PHYSICAL_ADDRESS End,\r
115 IN UINT64 Attribute\r
116 );\r
117\r
118STATIC\r
119VOID\r
120CoreFreeMemoryMapStack (\r
121 VOID\r
122 );\r
123\r
124STATIC\r
125EFI_STATUS\r
126CoreConvertPages (\r
127 IN UINT64 Start,\r
128 IN UINT64 NumberOfPages,\r
129 IN EFI_MEMORY_TYPE NewType\r
130 );\r
131\r
132STATIC\r
133VOID\r
134RemoveMemoryMapEntry (\r
135 MEMORY_MAP *Entry\r
136 );\r
137 \r
138STATIC\r
139MEMORY_MAP *\r
140AllocateMemoryMapEntry (\r
141 VOID\r
142 );\r
143 \r
144VOID\r
145CoreAcquireMemoryLock (\r
146 VOID\r
147 )\r
148/*++\r
149\r
150Routine Description:\r
151\r
152 Enter critical section by gaining lock on gMemoryLock\r
153\r
154Arguments:\r
155\r
156 None\r
157\r
158Returns:\r
159\r
160 None\r
161\r
162--*/\r
163{\r
164 CoreAcquireLock (&gMemoryLock);\r
165}\r
166\r
167\r
168VOID\r
169CoreReleaseMemoryLock (\r
170 VOID\r
171 )\r
172/*++\r
173\r
174Routine Description:\r
175\r
176 Exit critical section by releasing lock on gMemoryLock\r
177\r
178Arguments:\r
179\r
180 None\r
181\r
182Returns:\r
183\r
184 None\r
185\r
186--*/\r
187{\r
188 CoreReleaseLock (&gMemoryLock);\r
189}\r
190\r
191STATIC\r
192VOID\r
193PromoteMemoryResource (\r
194 VOID\r
195 )\r
196/*++\r
197\r
198Routine Description:\r
199\r
200 Find untested but initialized memory regions in GCD map and convert them to be DXE allocatable.\r
201\r
202Arguments:\r
203\r
204 None\r
205\r
206Returns:\r
207\r
208 None\r
209\r
210--*/\r
211{\r
212 LIST_ENTRY *Link;\r
213 EFI_GCD_MAP_ENTRY *Entry;\r
214\r
215 DEBUG ((EFI_D_ERROR | EFI_D_PAGE, "Promote the memory resource\n"));\r
216 \r
217 CoreAcquireGcdMemoryLock ();\r
218 \r
219 Link = mGcdMemorySpaceMap.ForwardLink;\r
220 while (Link != &mGcdMemorySpaceMap) {\r
221\r
222 Entry = CR (Link, EFI_GCD_MAP_ENTRY, Link, EFI_GCD_MAP_SIGNATURE);\r
223\r
224 if (Entry->GcdMemoryType == EfiGcdMemoryTypeReserved &&\r
225 Entry->EndAddress < EFI_MAX_ADDRESS &&\r
226 (Entry->Capabilities & (EFI_MEMORY_PRESENT | EFI_MEMORY_INITIALIZED | EFI_MEMORY_TESTED)) ==\r
227 (EFI_MEMORY_PRESENT | EFI_MEMORY_INITIALIZED)) {\r
228 //\r
229 // Update the GCD map\r
230 //\r
231 Entry->GcdMemoryType = EfiGcdMemoryTypeSystemMemory;\r
232 Entry->Capabilities |= EFI_MEMORY_TESTED;\r
233 Entry->ImageHandle = gDxeCoreImageHandle;\r
234 Entry->DeviceHandle = NULL;\r
235\r
236 //\r
237 // Add to allocable system memory resource\r
238 // \r
239\r
240 CoreAddRange (\r
241 EfiConventionalMemory, \r
242 Entry->BaseAddress, \r
243 Entry->EndAddress, \r
244 Entry->Capabilities & ~(EFI_MEMORY_PRESENT | EFI_MEMORY_INITIALIZED | EFI_MEMORY_TESTED | EFI_MEMORY_RUNTIME)\r
245 );\r
246 CoreFreeMemoryMapStack ();\r
247 \r
248 }\r
249\r
250 Link = Link->ForwardLink;\r
251 }\r
252 \r
253 CoreReleaseGcdMemoryLock ();\r
254 \r
255 return;\r
256}\r
257\r
258VOID\r
259CoreAddMemoryDescriptor (\r
260 IN EFI_MEMORY_TYPE Type,\r
261 IN EFI_PHYSICAL_ADDRESS Start,\r
262 IN UINT64 NumberOfPages,\r
263 IN UINT64 Attribute\r
264 )\r
265/*++\r
266\r
267Routine Description:\r
268\r
269 Called to initialize the memory map and add descriptors to\r
270 the current descriptor list.\r
271\r
272 The first descriptor that is added must be general usable\r
273 memory as the addition allocates heap.\r
274\r
275Arguments:\r
276\r
277 Type - The type of memory to add\r
278\r
279 Start - The starting address in the memory range\r
280 Must be page aligned\r
281\r
282 NumberOfPages - The number of pages in the range\r
283\r
284 Attribute - Attributes of the memory to add\r
285\r
286Returns:\r
287\r
288 None. The range is added to the memory map\r
289\r
290--*/\r
291{\r
292 EFI_PHYSICAL_ADDRESS End;\r
293 EFI_STATUS Status;\r
294 UINTN Index;\r
295 UINTN FreeIndex;\r
296\r
297 if ((Start & EFI_PAGE_MASK) != 0) {\r
298 return;\r
299 }\r
300\r
301 if (Type >= EfiMaxMemoryType && Type <= 0x7fffffff) {\r
302 return;\r
303 }\r
304 \r
305 CoreAcquireMemoryLock ();\r
306 End = Start + LShiftU64 (NumberOfPages, EFI_PAGE_SHIFT) - 1;\r
307 CoreAddRange (Type, Start, End, Attribute);\r
308 CoreFreeMemoryMapStack ();\r
309 CoreReleaseMemoryLock ();\r
310\r
311 //\r
312 // Check to see if the statistics for the different memory types have already been established\r
313 //\r
314 if (mMemoryTypeInformationInitialized) {\r
315 return;\r
316 }\r
317\r
318 //\r
319 // Loop through each memory type in the order specified by the gMemoryTypeInformation[] array\r
320 //\r
321 for (Index = 0; gMemoryTypeInformation[Index].Type != EfiMaxMemoryType; Index++) {\r
322 //\r
323 // Make sure the memory type in the gMemoryTypeInformation[] array is valid\r
324 //\r
325 Type = (EFI_MEMORY_TYPE) (gMemoryTypeInformation[Index].Type);\r
326 if (Type < 0 || Type > EfiMaxMemoryType) {\r
327 continue;\r
328 }\r
329\r
330 if (gMemoryTypeInformation[Index].NumberOfPages != 0) {\r
331 //\r
332 // Allocate pages for the current memory type from the top of available memory\r
333 //\r
334 Status = CoreAllocatePages (\r
335 AllocateAnyPages,\r
336 Type,\r
337 gMemoryTypeInformation[Index].NumberOfPages,\r
338 &mMemoryTypeStatistics[Type].BaseAddress\r
339 );\r
340 if (EFI_ERROR (Status)) {\r
341 //\r
342 // If an error occurs allocating the pages for the current memory type, then \r
343 // free all the pages allocates for the previous memory types and return. This\r
344 // operation with be retied when/if more memory is added to the system\r
345 //\r
346 for (FreeIndex = 0; FreeIndex < Index; FreeIndex++) {\r
347 //\r
348 // Make sure the memory type in the gMemoryTypeInformation[] array is valid\r
349 //\r
350 Type = (EFI_MEMORY_TYPE) (gMemoryTypeInformation[FreeIndex].Type);\r
351 if (Type < 0 || Type > EfiMaxMemoryType) {\r
352 continue;\r
353 }\r
354\r
355 if (gMemoryTypeInformation[FreeIndex].NumberOfPages != 0) {\r
356 CoreFreePages (\r
357 mMemoryTypeStatistics[Type].BaseAddress, \r
358 gMemoryTypeInformation[FreeIndex].NumberOfPages\r
359 );\r
360 mMemoryTypeStatistics[Type].BaseAddress = 0;\r
361 mMemoryTypeStatistics[Type].MaximumAddress = EFI_MAX_ADDRESS;\r
362 }\r
363 }\r
364 return;\r
365 }\r
366\r
367 //\r
368 // Compute the address at the top of the current statistics\r
369 //\r
370 mMemoryTypeStatistics[Type].MaximumAddress = \r
371 mMemoryTypeStatistics[Type].BaseAddress + \r
372 LShiftU64 (gMemoryTypeInformation[Index].NumberOfPages, EFI_PAGE_SHIFT) - 1;\r
373\r
374 //\r
375 // If the current base address is the lowest address so far, then update the default \r
376 // maximum address\r
377 //\r
378 if (mMemoryTypeStatistics[Type].BaseAddress < mDefaultMaximumAddress) {\r
379 mDefaultMaximumAddress = mMemoryTypeStatistics[Type].BaseAddress - 1;\r
380 }\r
381 }\r
382 }\r
383\r
384 //\r
385 // There was enough system memory for all the the memory types were allocated. So,\r
386 // those memory areas can be freed for future allocations, and all future memory\r
387 // allocations can occur within their respective bins\r
388 //\r
389 for (Index = 0; gMemoryTypeInformation[Index].Type != EfiMaxMemoryType; Index++) {\r
390 //\r
391 // Make sure the memory type in the gMemoryTypeInformation[] array is valid\r
392 //\r
393 Type = (EFI_MEMORY_TYPE) (gMemoryTypeInformation[Index].Type);\r
394 if (Type < 0 || Type > EfiMaxMemoryType) {\r
395 continue;\r
396 }\r
397\r
398 if (gMemoryTypeInformation[Index].NumberOfPages != 0) {\r
399 CoreFreePages (\r
400 mMemoryTypeStatistics[Type].BaseAddress, \r
401 gMemoryTypeInformation[Index].NumberOfPages\r
402 );\r
b74350e9 403 mMemoryTypeStatistics[Type].NumberOfPages = gMemoryTypeInformation[Index].NumberOfPages;\r
28a00297 404 gMemoryTypeInformation[Index].NumberOfPages = 0;\r
405 }\r
406 }\r
407\r
408 //\r
409 // If the number of pages reserved for a memory type is 0, then all allocations for that type\r
410 // should be in the default range.\r
411 //\r
412 for (Type = (EFI_MEMORY_TYPE) 0; Type < EfiMaxMemoryType; Type++) {\r
413 for (Index = 0; gMemoryTypeInformation[Index].Type != EfiMaxMemoryType; Index++) {\r
414 if (Type == (EFI_MEMORY_TYPE)gMemoryTypeInformation[Index].Type) {\r
415 mMemoryTypeStatistics[Type].InformationIndex = Index;\r
416 }\r
417 }\r
418 mMemoryTypeStatistics[Type].CurrentNumberOfPages = 0;\r
419 if (mMemoryTypeStatistics[Type].MaximumAddress == EFI_MAX_ADDRESS) {\r
420 mMemoryTypeStatistics[Type].MaximumAddress = mDefaultMaximumAddress;\r
421 }\r
422 }\r
423\r
424 mMemoryTypeInformationInitialized = TRUE;\r
425}\r
426\r
427\r
428STATIC\r
429VOID\r
430CoreAddRange (\r
431 IN EFI_MEMORY_TYPE Type,\r
432 IN EFI_PHYSICAL_ADDRESS Start,\r
433 IN EFI_PHYSICAL_ADDRESS End,\r
434 IN UINT64 Attribute\r
435 )\r
436/*++\r
437\r
438Routine Description:\r
439\r
440 Internal function. Adds a ranges to the memory map.\r
441 The range must not already exist in the map.\r
442\r
443Arguments:\r
444\r
445 Type - The type of memory range to add\r
446\r
447 Start - The starting address in the memory range\r
448 Must be paged aligned\r
449\r
450 End - The last address in the range\r
451 Must be the last byte of a page\r
452\r
453 Attribute - The attributes of the memory range to add\r
454\r
455Returns:\r
456\r
457 None. The range is added to the memory map\r
458\r
459--*/\r
460{\r
461 LIST_ENTRY *Link;\r
462 MEMORY_MAP *Entry;\r
463\r
464 ASSERT ((Start & EFI_PAGE_MASK) == 0);\r
465 ASSERT (End > Start) ;\r
466\r
467 ASSERT_LOCKED (&gMemoryLock);\r
468 \r
469 DEBUG ((EFI_D_PAGE, "AddRange: %lx-%lx to %d\n", Start, End, Type));\r
470\r
471 //\r
472 // Memory map being altered so updated key\r
473 //\r
474 mMemoryMapKey += 1;\r
475\r
476 //\r
477 // UEFI 2.0 added an event group for notificaiton on memory map changes.\r
478 // So we need to signal this Event Group every time the memory map changes.\r
479 // If we are in EFI 1.10 compatability mode no event groups will be \r
480 // found and nothing will happen we we call this function. These events\r
481 // will get signaled but since a lock is held around the call to this \r
482 // function the notificaiton events will only be called after this funciton\r
483 // returns and the lock is released.\r
484 //\r
485 CoreNotifySignalList (&gEfiEventMemoryMapChangeGuid);\r
486\r
487 //\r
488 // Look for adjoining memory descriptor\r
489 //\r
490 \r
491 // Two memory descriptors can only be merged if they have the same Type\r
492 // and the same Attribute\r
493 //\r
494\r
495 Link = gMemoryMap.ForwardLink;\r
496 while (Link != &gMemoryMap) {\r
497 Entry = CR (Link, MEMORY_MAP, Link, MEMORY_MAP_SIGNATURE);\r
498 Link = Link->ForwardLink;\r
499\r
500 if (Entry->Type != Type) {\r
501 continue;\r
502 }\r
503\r
504 if (Entry->Attribute != Attribute) {\r
505 continue;\r
506 }\r
507\r
508 if (Entry->End + 1 == Start) {\r
509 \r
510 Start = Entry->Start;\r
511 RemoveMemoryMapEntry (Entry);\r
512\r
513 } else if (Entry->Start == End + 1) {\r
514 \r
515 End = Entry->End;\r
516 RemoveMemoryMapEntry (Entry);\r
517 }\r
518 }\r
519\r
520 //\r
521 // Add descriptor \r
522 //\r
523\r
524 mMapStack[mMapDepth].Signature = MEMORY_MAP_SIGNATURE;\r
525 mMapStack[mMapDepth].FromPages = FALSE;\r
526 mMapStack[mMapDepth].Type = Type;\r
527 mMapStack[mMapDepth].Start = Start;\r
528 mMapStack[mMapDepth].End = End;\r
529 mMapStack[mMapDepth].VirtualStart = 0;\r
530 mMapStack[mMapDepth].Attribute = Attribute;\r
531 InsertTailList (&gMemoryMap, &mMapStack[mMapDepth].Link);\r
532\r
533 mMapDepth += 1;\r
534 ASSERT (mMapDepth < MAX_MAP_DEPTH);\r
535\r
536 return ;\r
537}\r
538\r
539STATIC\r
540VOID\r
541CoreFreeMemoryMapStack (\r
542 VOID\r
543 )\r
544/*++\r
545\r
546Routine Description:\r
547\r
548 Internal function. Moves any memory descriptors that are on the\r
549 temporary descriptor stack to heap.\r
550\r
551Arguments:\r
552\r
553 None\r
554\r
555Returns:\r
556\r
557 None\r
558\r
559--*/\r
560{\r
561 MEMORY_MAP *Entry;\r
562 MEMORY_MAP *Entry2;\r
563 LIST_ENTRY *Link2;\r
564\r
565 ASSERT_LOCKED (&gMemoryLock);\r
566\r
567 //\r
568 // If already freeing the map stack, then return\r
569 //\r
570 if (mFreeMapStack) {\r
571 return ;\r
572 }\r
573\r
574 //\r
575 // Move the temporary memory descriptor stack into pool\r
576 //\r
577 mFreeMapStack += 1;\r
578\r
579 while (mMapDepth) {\r
580 //\r
581 // Deque an memory map entry from mFreeMemoryMapEntryList \r
582 //\r
583 Entry = AllocateMemoryMapEntry ();\r
584 \r
585 ASSERT (Entry);\r
586\r
587 //\r
588 // Update to proper entry\r
589 //\r
590 mMapDepth -= 1;\r
591\r
592 if (mMapStack[mMapDepth].Link.ForwardLink != NULL) {\r
593\r
594 //\r
595 // Move this entry to general memory\r
596 //\r
597 RemoveEntryList (&mMapStack[mMapDepth].Link);\r
598 mMapStack[mMapDepth].Link.ForwardLink = NULL;\r
599\r
600 CopyMem (Entry , &mMapStack[mMapDepth], sizeof (MEMORY_MAP));\r
601 Entry->FromPages = TRUE;\r
602\r
603 //\r
604 // Find insertion location\r
605 //\r
606 for (Link2 = gMemoryMap.ForwardLink; Link2 != &gMemoryMap; Link2 = Link2->ForwardLink) {\r
607 Entry2 = CR (Link2, MEMORY_MAP, Link, MEMORY_MAP_SIGNATURE);\r
608 if (Entry2->FromPages && Entry2->Start > Entry->Start) {\r
609 break;\r
610 }\r
611 }\r
612\r
613 InsertTailList (Link2, &Entry->Link);\r
614\r
615 } else {\r
616 // \r
617 // This item of mMapStack[mMapDepth] has already been dequeued from gMemoryMap list,\r
618 // so here no need to move it to memory.\r
619 //\r
620 InsertTailList (&mFreeMemoryMapEntryList, &Entry->Link);\r
621 }\r
622 }\r
623\r
624 mFreeMapStack -= 1;\r
625}\r
626\r
627STATIC\r
628VOID\r
629RemoveMemoryMapEntry (\r
630 MEMORY_MAP *Entry\r
631 )\r
632/*++\r
633\r
634Routine Description:\r
635\r
636 Internal function. Removes a descriptor entry.\r
637\r
638Arguments:\r
639\r
640 Entry - The entry to remove\r
641\r
642Returns:\r
643\r
644 None\r
645\r
646--*/\r
647{\r
648 RemoveEntryList (&Entry->Link);\r
649 Entry->Link.ForwardLink = NULL;\r
650\r
651 if (Entry->FromPages) {\r
652 //\r
653 // Insert the free memory map descriptor to the end of mFreeMemoryMapEntryList\r
654 //\r
655 InsertTailList (&mFreeMemoryMapEntryList, &Entry->Link);\r
656 }\r
657}\r
658\r
659STATIC\r
660MEMORY_MAP *\r
661AllocateMemoryMapEntry (\r
662 VOID\r
663 )\r
664/*++\r
665\r
666Routine Description:\r
667\r
668 Internal function. Deque a descriptor entry from the mFreeMemoryMapEntryList.\r
669 If the list is emtry, then allocate a new page to refuel the list. \r
670 Please Note this algorithm to allocate the memory map descriptor has a property\r
671 that the memory allocated for memory entries always grows, and will never really be freed \r
672 For example, if the current boot uses 2000 memory map entries at the maximum point, but\r
673 ends up with only 50 at the time the OS is booted, then the memory associated with the 1950 \r
674 memory map entries is still allocated from EfiBootServicesMemory. \r
675\r
676Arguments:\r
677\r
678 NONE\r
679\r
680Returns:\r
681\r
682 The Memory map descriptor dequed from the mFreeMemoryMapEntryList\r
683\r
684--*/ \r
685{\r
686 MEMORY_MAP* FreeDescriptorEntries;\r
687 MEMORY_MAP* Entry;\r
688 UINTN Index;\r
689 \r
690 if (IsListEmpty (&mFreeMemoryMapEntryList)) {\r
691 // \r
692 // The list is empty, to allocate one page to refuel the list\r
693 //\r
694 FreeDescriptorEntries = CoreAllocatePoolPages (EfiBootServicesData, EFI_SIZE_TO_PAGES(DEFAULT_PAGE_ALLOCATION), DEFAULT_PAGE_ALLOCATION);\r
695 if(FreeDescriptorEntries != NULL) {\r
696 //\r
697 // Enque the free memmory map entries into the list\r
698 //\r
699 for (Index = 0; Index< DEFAULT_PAGE_ALLOCATION / sizeof(MEMORY_MAP); Index++) {\r
700 FreeDescriptorEntries[Index].Signature = MEMORY_MAP_SIGNATURE;\r
701 InsertTailList (&mFreeMemoryMapEntryList, &FreeDescriptorEntries[Index].Link);\r
702 } \r
703 } else {\r
704 return NULL;\r
705 }\r
706 }\r
707 //\r
708 // dequeue the first descriptor from the list\r
709 //\r
710 Entry = CR (mFreeMemoryMapEntryList.ForwardLink, MEMORY_MAP, Link, MEMORY_MAP_SIGNATURE);\r
711 RemoveEntryList (&Entry->Link);\r
712 \r
713 return Entry;\r
714} \r
715\r
716STATIC\r
717EFI_STATUS\r
718CoreConvertPages (\r
719 IN UINT64 Start,\r
720 IN UINT64 NumberOfPages,\r
721 IN EFI_MEMORY_TYPE NewType\r
722 )\r
723/*++\r
724\r
725Routine Description:\r
726\r
727 Internal function. Converts a memory range to the specified type.\r
728 The range must exist in the memory map.\r
729\r
730Arguments:\r
731\r
732 Start - The first address of the range\r
733 Must be page aligned\r
734\r
735 NumberOfPages - The number of pages to convert\r
736\r
737 NewType - The new type for the memory range\r
738\r
739Returns:\r
740\r
741 EFI_INVALID_PARAMETER - Invalid parameter\r
742 \r
743 EFI_NOT_FOUND - Could not find a descriptor cover the specified range \r
744 or convertion not allowed.\r
745 \r
746 EFI_SUCCESS - Successfully converts the memory range to the specified type.\r
747\r
748--*/\r
749{\r
750\r
751 UINT64 NumberOfBytes;\r
752 UINT64 End;\r
753 UINT64 RangeEnd;\r
754 UINT64 Attribute;\r
755 LIST_ENTRY *Link;\r
756 MEMORY_MAP *Entry;\r
757\r
758 Entry = NULL;\r
759 NumberOfBytes = LShiftU64 (NumberOfPages, EFI_PAGE_SHIFT);\r
760 End = Start + NumberOfBytes - 1;\r
761\r
762 ASSERT (NumberOfPages);\r
763 ASSERT ((Start & EFI_PAGE_MASK) == 0);\r
764 ASSERT (End > Start) ;\r
765 ASSERT_LOCKED (&gMemoryLock);\r
766\r
767 if (NumberOfPages == 0 || (Start & EFI_PAGE_MASK ) || (Start > (Start + NumberOfBytes))) {\r
768 return EFI_INVALID_PARAMETER;\r
769 }\r
770\r
771 //\r
772 // Convert the entire range\r
773 //\r
774\r
775 while (Start < End) {\r
776\r
777 //\r
778 // Find the entry that the covers the range\r
779 //\r
780 for (Link = gMemoryMap.ForwardLink; Link != &gMemoryMap; Link = Link->ForwardLink) {\r
781 Entry = CR (Link, MEMORY_MAP, Link, MEMORY_MAP_SIGNATURE);\r
782\r
783 if (Entry->Start <= Start && Entry->End > Start) {\r
784 break;\r
785 }\r
786 }\r
787\r
788 if (Link == &gMemoryMap) {\r
789 DEBUG ((EFI_D_ERROR | EFI_D_PAGE, "ConvertPages: failed to find range %lx - %lx\n", Start, End));\r
790 return EFI_NOT_FOUND;\r
791 }\r
792\r
793 //\r
794 // Convert range to the end, or to the end of the descriptor\r
795 // if that's all we've got\r
796 //\r
797 RangeEnd = End;\r
798 if (Entry->End < End) {\r
799 RangeEnd = Entry->End;\r
800 }\r
801\r
802 DEBUG ((EFI_D_PAGE, "ConvertRange: %lx-%lx to %d\n", Start, RangeEnd, NewType));\r
803\r
804 //\r
805 // Debug code - verify conversion is allowed\r
806 //\r
807 if (!(NewType == EfiConventionalMemory ? 1 : 0) ^ (Entry->Type == EfiConventionalMemory ? 1 : 0)) {\r
808 DEBUG ((EFI_D_ERROR , "ConvertPages: Incompatible memory types\n"));\r
809 return EFI_NOT_FOUND;\r
810 } \r
811\r
812 //\r
813 // Update counters for the number of pages allocated to each memory type\r
814 //\r
815 if (Entry->Type >= 0 && Entry->Type < EfiMaxMemoryType) {\r
816 if (Start >= mMemoryTypeStatistics[Entry->Type].BaseAddress && \r
817 Start <= mMemoryTypeStatistics[Entry->Type].MaximumAddress) {\r
818 if (NumberOfPages > mMemoryTypeStatistics[Entry->Type].CurrentNumberOfPages) {\r
819 mMemoryTypeStatistics[Entry->Type].CurrentNumberOfPages = 0;\r
820 } else {\r
821 mMemoryTypeStatistics[Entry->Type].CurrentNumberOfPages -= NumberOfPages;\r
822 }\r
823 }\r
824 }\r
825\r
826 if (NewType >= 0 && NewType < EfiMaxMemoryType) {\r
827 if (Start >= mMemoryTypeStatistics[NewType].BaseAddress && Start <= mMemoryTypeStatistics[NewType].MaximumAddress) {\r
828 mMemoryTypeStatistics[NewType].CurrentNumberOfPages += NumberOfPages;\r
829 if (mMemoryTypeStatistics[NewType].CurrentNumberOfPages > \r
830 gMemoryTypeInformation[mMemoryTypeStatistics[NewType].InformationIndex].NumberOfPages) {\r
831 gMemoryTypeInformation[mMemoryTypeStatistics[NewType].InformationIndex].NumberOfPages = (UINT32)mMemoryTypeStatistics[NewType].CurrentNumberOfPages;\r
832 }\r
833 }\r
834 }\r
835\r
836 //\r
837 // Pull range out of descriptor\r
838 //\r
839 if (Entry->Start == Start) {\r
840 \r
841 //\r
842 // Clip start\r
843 //\r
844 Entry->Start = RangeEnd + 1;\r
845\r
846 } else if (Entry->End == RangeEnd) {\r
847 \r
848 //\r
849 // Clip end\r
850 //\r
851 Entry->End = Start - 1;\r
852\r
853 } else {\r
854\r
855 //\r
856 // Pull it out of the center, clip current\r
857 //\r
858 \r
859 //\r
860 // Add a new one\r
861 //\r
862 mMapStack[mMapDepth].Signature = MEMORY_MAP_SIGNATURE;\r
863 mMapStack[mMapDepth].FromPages = FALSE;\r
864 mMapStack[mMapDepth].Type = Entry->Type;\r
865 mMapStack[mMapDepth].Start = RangeEnd+1;\r
866 mMapStack[mMapDepth].End = Entry->End;\r
867\r
868 //\r
869 // Inherit Attribute from the Memory Descriptor that is being clipped\r
870 //\r
871 mMapStack[mMapDepth].Attribute = Entry->Attribute;\r
872\r
873 Entry->End = Start - 1;\r
874 ASSERT (Entry->Start < Entry->End);\r
875\r
876 Entry = &mMapStack[mMapDepth];\r
877 InsertTailList (&gMemoryMap, &Entry->Link);\r
878\r
879 mMapDepth += 1;\r
880 ASSERT (mMapDepth < MAX_MAP_DEPTH);\r
881 }\r
882\r
883 //\r
884 // The new range inherits the same Attribute as the Entry \r
885 //it is being cut out of\r
886 //\r
887 Attribute = Entry->Attribute;\r
888\r
889 //\r
890 // If the descriptor is empty, then remove it from the map\r
891 //\r
892 if (Entry->Start == Entry->End + 1) {\r
893 RemoveMemoryMapEntry (Entry);\r
894 Entry = NULL;\r
895 }\r
896 \r
897 //\r
898 // Add our new range in\r
899 //\r
900 CoreAddRange (NewType, Start, RangeEnd, Attribute);\r
901\r
902 //\r
903 // Move any map descriptor stack to general pool\r
904 //\r
905 CoreFreeMemoryMapStack ();\r
906\r
907 //\r
908 // Bump the starting address, and convert the next range\r
909 //\r
910 Start = RangeEnd + 1;\r
911 }\r
912\r
913 //\r
914 // Converted the whole range, done\r
915 //\r
916\r
917 return EFI_SUCCESS;\r
918}\r
919\r
920\r
921STATIC\r
922UINT64\r
923CoreFindFreePagesI (\r
924 IN UINT64 MaxAddress,\r
925 IN UINT64 NumberOfPages,\r
926 IN EFI_MEMORY_TYPE NewType,\r
927 IN UINTN Alignment\r
928 )\r
929/*++\r
930\r
931Routine Description:\r
932\r
933 Internal function. Finds a consecutive free page range below\r
934 the requested address.\r
935\r
936Arguments:\r
937\r
938 MaxAddress - The address that the range must be below\r
939\r
940 NumberOfPages - Number of pages needed\r
941\r
942 NewType - The type of memory the range is going to be turned into\r
943\r
944 Alignment - Bits to align with\r
945\r
946Returns:\r
947\r
948 The base address of the range, or 0 if the range was not found\r
949\r
950--*/\r
951{\r
952 UINT64 NumberOfBytes;\r
953 UINT64 Target;\r
954 UINT64 DescStart;\r
955 UINT64 DescEnd;\r
956 UINT64 DescNumberOfBytes;\r
957 LIST_ENTRY *Link;\r
958 MEMORY_MAP *Entry;\r
959\r
960 if ((MaxAddress < EFI_PAGE_MASK) ||(NumberOfPages == 0)) {\r
961 return 0;\r
962 }\r
963\r
964 if ((MaxAddress & EFI_PAGE_MASK) != EFI_PAGE_MASK) {\r
965 \r
966 //\r
967 // If MaxAddress is not aligned to the end of a page\r
968 //\r
969 \r
970 //\r
971 // Change MaxAddress to be 1 page lower\r
972 //\r
973 MaxAddress -= (EFI_PAGE_MASK + 1);\r
974 \r
975 //\r
976 // Set MaxAddress to a page boundary\r
977 //\r
978 MaxAddress &= ~EFI_PAGE_MASK;\r
979 \r
980 //\r
981 // Set MaxAddress to end of the page\r
982 //\r
983 MaxAddress |= EFI_PAGE_MASK;\r
984 }\r
985\r
986 NumberOfBytes = LShiftU64 (NumberOfPages, EFI_PAGE_SHIFT);\r
987 Target = 0;\r
988\r
989 for (Link = gMemoryMap.ForwardLink; Link != &gMemoryMap; Link = Link->ForwardLink) {\r
990 Entry = CR (Link, MEMORY_MAP, Link, MEMORY_MAP_SIGNATURE);\r
991 \r
992 //\r
993 // If it's not a free entry, don't bother with it\r
994 //\r
995 if (Entry->Type != EfiConventionalMemory) {\r
996 continue;\r
997 }\r
998\r
999 DescStart = Entry->Start;\r
1000 DescEnd = Entry->End;\r
1001\r
1002 //\r
1003 // If desc is past max allowed address, skip it\r
1004 //\r
1005 if (DescStart >= MaxAddress) {\r
1006 continue;\r
1007 }\r
1008\r
1009 //\r
1010 // If desc ends past max allowed address, clip the end\r
1011 //\r
1012 if (DescEnd >= MaxAddress) {\r
1013 DescEnd = MaxAddress;\r
1014 }\r
1015\r
1016 DescEnd = ((DescEnd + 1) & (~(Alignment - 1))) - 1;\r
1017\r
1018 //\r
1019 // Compute the number of bytes we can used from this \r
1020 // descriptor, and see it's enough to satisfy the request\r
1021 //\r
1022 DescNumberOfBytes = DescEnd - DescStart + 1;\r
1023\r
1024 if (DescNumberOfBytes >= NumberOfBytes) {\r
1025\r
1026 //\r
1027 // If this is the best match so far remember it\r
1028 //\r
1029 if (DescEnd > Target) {\r
1030 Target = DescEnd;\r
1031 }\r
1032 }\r
1033 } \r
1034\r
1035 //\r
1036 // If this is a grow down, adjust target to be the allocation base\r
1037 //\r
1038 Target -= NumberOfBytes - 1;\r
1039\r
1040 //\r
1041 // If we didn't find a match, return 0\r
1042 //\r
1043 if ((Target & EFI_PAGE_MASK) != 0) {\r
1044 return 0;\r
1045 }\r
1046\r
1047 return Target;\r
1048}\r
1049\r
1050STATIC\r
1051UINT64\r
1052FindFreePages (\r
1053 IN UINT64 MaxAddress,\r
1054 IN UINT64 NoPages,\r
1055 IN EFI_MEMORY_TYPE NewType,\r
1056 IN UINTN Alignment\r
1057 )\r
1058/*++\r
1059\r
1060Routine Description:\r
1061\r
1062 Internal function. Finds a consecutive free page range below\r
1063 the requested address\r
1064\r
1065Arguments:\r
1066\r
1067 MaxAddress - The address that the range must be below\r
1068\r
1069 NoPages - Number of pages needed\r
1070\r
1071 NewType - The type of memory the range is going to be turned into\r
1072\r
1073 Alignment - Bits to align with\r
1074\r
1075Returns:\r
1076\r
1077 The base address of the range, or 0 if the range was not found.\r
1078\r
1079--*/\r
1080{\r
1081 UINT64 NewMaxAddress;\r
1082 UINT64 Start;\r
1083\r
1084 NewMaxAddress = MaxAddress;\r
1085\r
1086 if (NewType >= 0 && NewType < EfiMaxMemoryType && NewMaxAddress >= mMemoryTypeStatistics[NewType].MaximumAddress) {\r
1087 NewMaxAddress = mMemoryTypeStatistics[NewType].MaximumAddress;\r
1088 } else {\r
1089 if (NewMaxAddress > mDefaultMaximumAddress) {\r
1090 NewMaxAddress = mDefaultMaximumAddress;\r
1091 }\r
1092 }\r
1093\r
1094 Start = CoreFindFreePagesI (NewMaxAddress, NoPages, NewType, Alignment);\r
1095 if (!Start) {\r
1096 Start = CoreFindFreePagesI (MaxAddress, NoPages, NewType, Alignment);\r
1097 if (!Start) {\r
1098 //\r
1099 // Here means there may be no enough memory to use, so try to go through\r
1100 // all the memory descript to promote the untested memory directly\r
1101 //\r
1102 PromoteMemoryResource ();\r
1103\r
1104 //\r
1105 // Allocate memory again after the memory resource re-arranged\r
1106 //\r
1107 Start = CoreFindFreePagesI (MaxAddress, NoPages, NewType, Alignment);\r
1108 }\r
1109 }\r
1110\r
1111 return Start;\r
1112}\r
1113\r
1114\r
1115EFI_STATUS\r
1116EFIAPI\r
1117CoreAllocatePages (\r
1118 IN EFI_ALLOCATE_TYPE Type,\r
1119 IN EFI_MEMORY_TYPE MemoryType,\r
1120 IN UINTN NumberOfPages,\r
1121 IN OUT EFI_PHYSICAL_ADDRESS *Memory\r
1122 )\r
1123/*++\r
1124\r
1125Routine Description:\r
1126\r
1127 Allocates pages from the memory map.\r
1128\r
1129Arguments:\r
1130\r
1131 Type - The type of allocation to perform\r
1132\r
1133 MemoryType - The type of memory to turn the allocated pages into\r
1134\r
1135 NumberOfPages - The number of pages to allocate\r
1136\r
1137 Memory - A pointer to receive the base allocated memory address\r
1138\r
1139Returns:\r
1140\r
1141 Status. On success, Memory is filled in with the base address allocated\r
1142\r
1143 EFI_INVALID_PARAMETER - Parameters violate checking rules defined in spec.\r
1144 \r
1145 EFI_NOT_FOUND - Could not allocate pages match the requirement.\r
1146 \r
1147 EFI_OUT_OF_RESOURCES - No enough pages to allocate.\r
1148 \r
1149 EFI_SUCCESS - Pages successfully allocated.\r
1150\r
1151--*/\r
1152{\r
1153 EFI_STATUS Status;\r
1154 UINT64 Start;\r
1155 UINT64 MaxAddress;\r
1156 UINTN Alignment;\r
1157\r
1158 if (Type < AllocateAnyPages || Type >= (UINTN) MaxAllocateType) {\r
1159 return EFI_INVALID_PARAMETER;\r
1160 }\r
1161\r
1162 if ((MemoryType >= EfiMaxMemoryType && MemoryType <= 0x7fffffff) ||\r
1163 MemoryType == EfiConventionalMemory) {\r
1164 return EFI_INVALID_PARAMETER;\r
1165 }\r
1166\r
1167 Alignment = EFI_DEFAULT_PAGE_ALLOCATION_ALIGNMENT;\r
1168\r
1169 if (MemoryType == EfiACPIReclaimMemory ||\r
1170 MemoryType == EfiACPIMemoryNVS ||\r
1171 MemoryType == EfiRuntimeServicesCode ||\r
1172 MemoryType == EfiRuntimeServicesData) {\r
1173\r
1174 Alignment = EFI_ACPI_RUNTIME_PAGE_ALLOCATION_ALIGNMENT;\r
1175 }\r
1176\r
1177 if (Type == AllocateAddress) {\r
1178 if ((*Memory & (Alignment - 1)) != 0) {\r
1179 return EFI_NOT_FOUND;\r
1180 }\r
1181 }\r
1182\r
1183 NumberOfPages += EFI_SIZE_TO_PAGES (Alignment) - 1;\r
1184 NumberOfPages &= ~(EFI_SIZE_TO_PAGES (Alignment) - 1);\r
1185\r
1186 //\r
1187 // If this is for below a particular address, then \r
1188 //\r
1189 Start = *Memory;\r
1190 \r
1191 //\r
1192 // The max address is the max natively addressable address for the processor\r
1193 //\r
1194 MaxAddress = EFI_MAX_ADDRESS;\r
1195 \r
1196 if (Type == AllocateMaxAddress) {\r
1197 MaxAddress = Start;\r
1198 }\r
1199\r
1200 CoreAcquireMemoryLock ();\r
1201 \r
1202 //\r
1203 // If not a specific address, then find an address to allocate\r
1204 //\r
1205 if (Type != AllocateAddress) {\r
1206 Start = FindFreePages (MaxAddress, NumberOfPages, MemoryType, Alignment);\r
1207 if (Start == 0) {\r
1208 Status = EFI_OUT_OF_RESOURCES;\r
1209 goto Done;\r
1210 }\r
1211 }\r
1212\r
1213 //\r
1214 // Convert pages from FreeMemory to the requested type\r
1215 //\r
1216 Status = CoreConvertPages (Start, NumberOfPages, MemoryType);\r
1217\r
1218Done:\r
1219 CoreReleaseMemoryLock ();\r
1220\r
1221 if (!EFI_ERROR (Status)) {\r
1222 *Memory = Start;\r
1223 }\r
1224\r
1225 return Status;\r
1226}\r
1227\r
1228\r
1229\r
1230\r
1231EFI_STATUS \r
1232EFIAPI\r
1233CoreFreePages (\r
1234 IN EFI_PHYSICAL_ADDRESS Memory,\r
1235 IN UINTN NumberOfPages\r
1236 )\r
1237/*++\r
1238\r
1239Routine Description:\r
1240\r
1241 Frees previous allocated pages.\r
1242\r
1243Arguments:\r
1244\r
1245 Memory - Base address of memory being freed\r
1246\r
1247 NumberOfPages - The number of pages to free\r
1248\r
1249Returns:\r
1250\r
1251 EFI_NOT_FOUND - Could not find the entry that covers the range\r
1252 \r
1253 EFI_INVALID_PARAMETER - Address not aligned\r
1254 \r
1255 EFI_SUCCESS -Pages successfully freed.\r
1256\r
1257--*/\r
1258{\r
1259 EFI_STATUS Status;\r
1260 LIST_ENTRY *Link;\r
1261 MEMORY_MAP *Entry;\r
1262 UINTN Alignment;\r
1263\r
1264 //\r
1265 // Free the range\r
1266 //\r
1267 CoreAcquireMemoryLock ();\r
1268\r
1269 //\r
1270 // Find the entry that the covers the range\r
1271 //\r
1272 Entry = NULL;\r
1273 for (Link = gMemoryMap.ForwardLink; Link != &gMemoryMap; Link = Link->ForwardLink) {\r
1274 Entry = CR(Link, MEMORY_MAP, Link, MEMORY_MAP_SIGNATURE);\r
1275 if (Entry->Start <= Memory && Entry->End > Memory) {\r
1276 break;\r
1277 }\r
1278 }\r
1279 if (Link == &gMemoryMap) {\r
1280 CoreReleaseMemoryLock ();\r
1281 return EFI_NOT_FOUND;\r
1282 }\r
1283\r
1284 Alignment = EFI_DEFAULT_PAGE_ALLOCATION_ALIGNMENT;\r
1285\r
1286 if (Entry->Type == EfiACPIReclaimMemory ||\r
1287 Entry->Type == EfiACPIMemoryNVS ||\r
1288 Entry->Type == EfiRuntimeServicesCode ||\r
1289 Entry->Type == EfiRuntimeServicesData) {\r
1290\r
1291 Alignment = EFI_ACPI_RUNTIME_PAGE_ALLOCATION_ALIGNMENT;\r
1292\r
1293 }\r
1294\r
1295 if ((Memory & (Alignment - 1)) != 0) {\r
1296 CoreReleaseMemoryLock ();\r
1297 return EFI_INVALID_PARAMETER;\r
1298 }\r
1299\r
1300 NumberOfPages += EFI_SIZE_TO_PAGES (Alignment) - 1;\r
1301 NumberOfPages &= ~(EFI_SIZE_TO_PAGES (Alignment) - 1);\r
1302\r
1303 Status = CoreConvertPages (Memory, NumberOfPages, EfiConventionalMemory);\r
1304\r
1305 CoreReleaseMemoryLock ();\r
1306\r
1307 if (EFI_ERROR (Status)) {\r
1308 return Status;\r
1309 }\r
1310\r
1311 //\r
1312 // Destroy the contents\r
1313 //\r
1314 if (Memory < EFI_MAX_ADDRESS) {\r
1315 DEBUG_CLEAR_MEMORY ((VOID *)(UINTN)Memory, NumberOfPages << EFI_PAGE_SHIFT);\r
1316 }\r
1317 \r
1318 return Status;\r
1319}\r
1320\r
1321\r
1322\r
1323EFI_STATUS\r
1324EFIAPI\r
1325CoreGetMemoryMap (\r
1326 IN OUT UINTN *MemoryMapSize,\r
1327 IN OUT EFI_MEMORY_DESCRIPTOR *MemoryMap,\r
1328 OUT UINTN *MapKey,\r
1329 OUT UINTN *DescriptorSize,\r
1330 OUT UINT32 *DescriptorVersion\r
1331 )\r
1332/*++\r
1333\r
1334Routine Description:\r
1335\r
1336 This function returns a copy of the current memory map. The map is an array of \r
1337 memory descriptors, each of which describes a contiguous block of memory.\r
1338\r
1339Arguments:\r
1340\r
1341 MemoryMapSize - A pointer to the size, in bytes, of the MemoryMap buffer. On\r
1342 input, this is the size of the buffer allocated by the caller. \r
1343 On output, it is the size of the buffer returned by the firmware \r
1344 if the buffer was large enough, or the size of the buffer needed \r
1345 to contain the map if the buffer was too small.\r
1346 MemoryMap - A pointer to the buffer in which firmware places the current memory map.\r
1347 MapKey - A pointer to the location in which firmware returns the key for the\r
1348 current memory map.\r
1349 DescriptorSize - A pointer to the location in which firmware returns the size, in\r
1350 bytes, of an individual EFI_MEMORY_DESCRIPTOR.\r
1351 DescriptorVersion - A pointer to the location in which firmware returns the version\r
1352 number associated with the EFI_MEMORY_DESCRIPTOR.\r
1353\r
1354Returns:\r
1355\r
1356 EFI_SUCCESS - The memory map was returned in the MemoryMap buffer. \r
1357 EFI_BUFFER_TOO_SMALL - The MemoryMap buffer was too small. The current buffer size\r
1358 needed to hold the memory map is returned in MemoryMapSize.\r
1359 EFI_INVALID_PARAMETER - One of the parameters has an invalid value. \r
1360\r
1361--*/\r
1362{\r
1363 EFI_STATUS Status;\r
1364 UINTN Size; \r
1365 UINTN BufferSize; \r
1366 UINTN NumberOfRuntimeEntries;\r
1367 LIST_ENTRY *Link;\r
1368 MEMORY_MAP *Entry; \r
1369 EFI_GCD_MAP_ENTRY *GcdMapEntry; \r
b74350e9 1370 EFI_MEMORY_TYPE Type;\r
28a00297 1371\r
1372 //\r
1373 // Make sure the parameters are valid\r
1374 //\r
1375 if (MemoryMapSize == NULL) {\r
1376 return EFI_INVALID_PARAMETER;\r
1377 }\r
1378 \r
1379 CoreAcquireGcdMemoryLock ();\r
1380 \r
1381 //\r
1382 // Count the number of Reserved and MMIO entries that are marked for runtime use\r
1383 //\r
1384 NumberOfRuntimeEntries = 0;\r
1385 for (Link = mGcdMemorySpaceMap.ForwardLink; Link != &mGcdMemorySpaceMap; Link = Link->ForwardLink) {\r
1386 GcdMapEntry = CR (Link, EFI_GCD_MAP_ENTRY, Link, EFI_GCD_MAP_SIGNATURE);\r
1387 if ((GcdMapEntry->GcdMemoryType == EfiGcdMemoryTypeReserved) ||\r
1388 (GcdMapEntry->GcdMemoryType == EfiGcdMemoryTypeMemoryMappedIo)) {\r
1389 if ((GcdMapEntry->Attributes & EFI_MEMORY_RUNTIME) == EFI_MEMORY_RUNTIME) {\r
1390 NumberOfRuntimeEntries++;\r
1391 }\r
1392 }\r
1393 }\r
1394\r
1395 Size = sizeof (EFI_MEMORY_DESCRIPTOR);\r
1396\r
1397 //\r
1398 // Make sure Size != sizeof(EFI_MEMORY_DESCRIPTOR). This will\r
1399 // prevent people from having pointer math bugs in their code.\r
1400 // now you have to use *DescriptorSize to make things work.\r
1401 //\r
1402 Size += sizeof(UINT64) - (Size % sizeof (UINT64));\r
1403\r
1404 if (DescriptorSize != NULL) {\r
1405 *DescriptorSize = Size;\r
1406 }\r
1407 \r
1408 if (DescriptorVersion != NULL) {\r
1409 *DescriptorVersion = EFI_MEMORY_DESCRIPTOR_VERSION;\r
1410 }\r
1411\r
1412 CoreAcquireMemoryLock ();\r
1413\r
1414 //\r
1415 // Compute the buffer size needed to fit the entire map\r
1416 //\r
1417 BufferSize = Size * NumberOfRuntimeEntries;\r
1418 for (Link = gMemoryMap.ForwardLink; Link != &gMemoryMap; Link = Link->ForwardLink) {\r
1419 BufferSize += Size;\r
1420 }\r
1421\r
1422 if (*MemoryMapSize < BufferSize) {\r
1423 Status = EFI_BUFFER_TOO_SMALL;\r
1424 goto Done;\r
1425 }\r
1426\r
1427 if (MemoryMap == NULL) {\r
1428 Status = EFI_INVALID_PARAMETER;\r
1429 goto Done;\r
1430 }\r
1431\r
1432 //\r
1433 // Build the map\r
1434 //\r
383c303c 1435 ZeroMem (MemoryMap, BufferSize);\r
28a00297 1436 for (Link = gMemoryMap.ForwardLink; Link != &gMemoryMap; Link = Link->ForwardLink) {\r
1437 Entry = CR (Link, MEMORY_MAP, Link, MEMORY_MAP_SIGNATURE);\r
1438 ASSERT (Entry->VirtualStart == 0);\r
1439\r
b74350e9 1440 //\r
1441 // Convert internal map into an EFI_MEMORY_DESCRIPTOR\r
1442 //\r
28a00297 1443 MemoryMap->Type = Entry->Type;\r
1444 MemoryMap->PhysicalStart = Entry->Start;\r
1445 MemoryMap->VirtualStart = Entry->VirtualStart;\r
1446 MemoryMap->NumberOfPages = RShiftU64 (Entry->End - Entry->Start + 1, EFI_PAGE_SHIFT);\r
b74350e9 1447 //\r
1448 // If the memory type is EfiConventionalMemory, then determine if the range is part of a\r
1449 // memory type bin and needs to be converted to the same memory type as the rest of the \r
1450 // memory type bin in order to minimize EFI Memory Map changes across reboots. This \r
1451 // improves the chances for a successful S4 resume in the presence of minor page allocation\r
1452 // differences across reboots.\r
1453 //\r
1454 if (MemoryMap->Type == EfiConventionalMemory) {\r
1455 for (Type = (EFI_MEMORY_TYPE) 0; Type < EfiMaxMemoryType; Type++) {\r
1456 if (mMemoryTypeStatistics[Type].Special &&\r
1457 mMemoryTypeStatistics[Type].NumberOfPages > 0 &&\r
1458 Entry->Start >= mMemoryTypeStatistics[Type].BaseAddress &&\r
1459 Entry->End <= mMemoryTypeStatistics[Type].MaximumAddress ) {\r
1460 MemoryMap->Type = Type;\r
1461 }\r
1462 }\r
1463 }\r
1464 MemoryMap->Attribute = Entry->Attribute;\r
1465 if (mMemoryTypeStatistics[MemoryMap->Type].Runtime) {\r
1466 MemoryMap->Attribute |= EFI_MEMORY_RUNTIME;\r
28a00297 1467 }\r
1468 \r
1469 MemoryMap = NextMemoryDescriptor (MemoryMap, Size);\r
1470 }\r
1471\r
1472 for (Link = mGcdMemorySpaceMap.ForwardLink; Link != &mGcdMemorySpaceMap; Link = Link->ForwardLink) {\r
1473 GcdMapEntry = CR (Link, EFI_GCD_MAP_ENTRY, Link, EFI_GCD_MAP_SIGNATURE);\r
1474 if ((GcdMapEntry->GcdMemoryType == EfiGcdMemoryTypeReserved) ||\r
1475 (GcdMapEntry->GcdMemoryType == EfiGcdMemoryTypeMemoryMappedIo)) {\r
1476 if ((GcdMapEntry->Attributes & EFI_MEMORY_RUNTIME) == EFI_MEMORY_RUNTIME) {\r
1477 \r
1478 MemoryMap->PhysicalStart = GcdMapEntry->BaseAddress;\r
1479 MemoryMap->VirtualStart = 0;\r
1480 MemoryMap->NumberOfPages = RShiftU64 ((GcdMapEntry->EndAddress - GcdMapEntry->BaseAddress + 1), EFI_PAGE_SHIFT);\r
1481 MemoryMap->Attribute = GcdMapEntry->Attributes & ~EFI_MEMORY_PORT_IO;\r
1482\r
1483 if (GcdMapEntry->GcdMemoryType == EfiGcdMemoryTypeReserved) {\r
1484 MemoryMap->Type = EfiReservedMemoryType;\r
1485 } else if (GcdMapEntry->GcdMemoryType == EfiGcdMemoryTypeMemoryMappedIo) {\r
1486 if ((GcdMapEntry->Attributes & EFI_MEMORY_PORT_IO) == EFI_MEMORY_PORT_IO) {\r
1487 MemoryMap->Type = EfiMemoryMappedIOPortSpace;\r
1488 } else {\r
1489 MemoryMap->Type = EfiMemoryMappedIO;\r
1490 }\r
1491 }\r
1492\r
1493 MemoryMap = NextMemoryDescriptor (MemoryMap, Size);\r
1494 }\r
1495 }\r
1496 }\r
1497 \r
1498 Status = EFI_SUCCESS;\r
1499\r
1500Done:\r
1501\r
1502 CoreReleaseMemoryLock ();\r
1503 \r
1504 CoreReleaseGcdMemoryLock ();\r
1505 \r
1506 // \r
1507 // Update the map key finally \r
1508 // \r
1509 if (MapKey != NULL) {\r
1510 *MapKey = mMemoryMapKey;\r
1511 }\r
1512 \r
1513 *MemoryMapSize = BufferSize;\r
1514 \r
1515 return Status;\r
1516}\r
1517\r
1518VOID *\r
1519CoreAllocatePoolPages (\r
1520 IN EFI_MEMORY_TYPE PoolType,\r
1521 IN UINTN NumberOfPages,\r
1522 IN UINTN Alignment\r
1523 )\r
1524/*++\r
1525\r
1526Routine Description:\r
1527\r
1528 Internal function. Used by the pool functions to allocate pages\r
1529 to back pool allocation requests.\r
1530\r
1531Arguments:\r
1532\r
1533 PoolType - The type of memory for the new pool pages\r
1534\r
1535 NumberOfPages - No of pages to allocate\r
1536\r
1537 Alignment - Bits to align.\r
1538\r
1539Returns:\r
1540\r
1541 The allocated memory, or NULL\r
1542\r
1543--*/\r
1544{\r
1545 UINT64 Start;\r
1546\r
1547 //\r
1548 // Find the pages to convert\r
1549 //\r
1550 Start = FindFreePages (EFI_MAX_ADDRESS, NumberOfPages, PoolType, Alignment);\r
1551\r
1552 //\r
1553 // Convert it to boot services data\r
1554 //\r
1555 if (Start == 0) {\r
1556 DEBUG ((EFI_D_ERROR | EFI_D_PAGE, "AllocatePoolPages: failed to allocate %d pages\n", NumberOfPages));\r
1557 } else {\r
1558 CoreConvertPages (Start, NumberOfPages, PoolType);\r
1559 }\r
1560\r
1561 return (VOID *)(UINTN)Start;\r
1562}\r
1563\r
1564VOID\r
1565CoreFreePoolPages (\r
1566 IN EFI_PHYSICAL_ADDRESS Memory,\r
1567 IN UINTN NumberOfPages\r
1568 )\r
1569/*++\r
1570\r
1571Routine Description:\r
1572\r
1573 Internal function. Frees pool pages allocated via AllocatePoolPages ()\r
1574\r
1575Arguments:\r
1576\r
1577 Memory - The base address to free\r
1578\r
1579 NumberOfPages - The number of pages to free\r
1580\r
1581Returns:\r
1582\r
1583 None\r
1584\r
1585--*/\r
1586{\r
1587 CoreConvertPages (Memory, NumberOfPages, EfiConventionalMemory);\r
1588}\r
1589\r
1590\r
1591EFI_STATUS\r
1592CoreTerminateMemoryMap (\r
1593 IN UINTN MapKey\r
1594 )\r
1595/*++\r
1596\r
1597Routine Description:\r
1598\r
1599 Make sure the memory map is following all the construction rules, \r
1600 it is the last time to check memory map error before exit boot services.\r
1601\r
1602Arguments:\r
1603\r
1604 MapKey - Memory map key\r
1605\r
1606Returns:\r
1607\r
1608 EFI_INVALID_PARAMETER - Memory map not consistent with construction rules.\r
1609 \r
1610 EFI_SUCCESS - Valid memory map.\r
1611\r
1612--*/\r
1613{\r
1614 EFI_STATUS Status;\r
1615 LIST_ENTRY *Link;\r
1616 MEMORY_MAP *Entry;\r
1617\r
1618 Status = EFI_SUCCESS;\r
1619\r
1620 CoreAcquireMemoryLock ();\r
1621\r
1622 if (MapKey == mMemoryMapKey) {\r
1623\r
1624 //\r
1625 // Make sure the memory map is following all the construction rules\r
1626 // This is the last chance we will be able to display any messages on\r
1627 // the console devices.\r
1628 //\r
1629\r
1630 for (Link = gMemoryMap.ForwardLink; Link != &gMemoryMap; Link = Link->ForwardLink) {\r
1631 Entry = CR(Link, MEMORY_MAP, Link, MEMORY_MAP_SIGNATURE);\r
1632 if (Entry->Attribute & EFI_MEMORY_RUNTIME) { \r
1633 if (Entry->Type == EfiACPIReclaimMemory || Entry->Type == EfiACPIMemoryNVS) {\r
1634 DEBUG((EFI_D_ERROR, "ExitBootServices: ACPI memory entry has RUNTIME attribute set.\n"));\r
1635 CoreReleaseMemoryLock ();\r
1636 return EFI_INVALID_PARAMETER;\r
1637 }\r
1638 if (Entry->Start & (EFI_ACPI_RUNTIME_PAGE_ALLOCATION_ALIGNMENT - 1)) {\r
1639 DEBUG((EFI_D_ERROR, "ExitBootServices: A RUNTIME memory entry is not on a proper alignment.\n"));\r
1640 CoreReleaseMemoryLock ();\r
1641 return EFI_INVALID_PARAMETER;\r
1642 }\r
1643 if ((Entry->End + 1) & (EFI_ACPI_RUNTIME_PAGE_ALLOCATION_ALIGNMENT - 1)) {\r
1644 DEBUG((EFI_D_ERROR, "ExitBootServices: A RUNTIME memory entry is not on a proper alignment.\n"));\r
1645 CoreReleaseMemoryLock ();\r
1646 return EFI_INVALID_PARAMETER;\r
1647 }\r
1648 }\r
1649 }\r
1650\r
1651 //\r
1652 // The map key they gave us matches what we expect. Fall through and\r
1653 // return success. In an ideal world we would clear out all of\r
1654 // EfiBootServicesCode and EfiBootServicesData. However this function\r
1655 // is not the last one called by ExitBootServices(), so we have to\r
1656 // preserve the memory contents.\r
1657 //\r
1658 } else {\r
1659 Status = EFI_INVALID_PARAMETER;\r
1660 }\r
1661\r
1662 CoreReleaseMemoryLock ();\r
1663\r
1664 return Status;\r
1665}\r
1666\r
1667\r
1668\r
1669\r
1670\r
1671\r
1672\r
1673\r