]> git.proxmox.com Git - mirror_edk2.git/blob - MdeModulePkg/Core/DxeIplPeim/X64/VirtualMemory.c
MdeModulePkg: Clean up source files
[mirror_edk2.git] / MdeModulePkg / Core / DxeIplPeim / X64 / VirtualMemory.c
1 /** @file
2 x64 Virtual Memory Management Services in the form of an IA-32 driver.
3 Used to establish a 1:1 Virtual to Physical Mapping that is required to
4 enter Long Mode (x64 64-bit mode).
5
6 While we make a 1:1 mapping (identity mapping) for all physical pages
7 we still need to use the MTRR's to ensure that the cachability attributes
8 for all memory regions is correct.
9
10 The basic idea is to use 2MB page table entries where ever possible. If
11 more granularity of cachability is required then 4K page tables are used.
12
13 References:
14 1) IA-32 Intel(R) Architecture Software Developer's Manual Volume 1:Basic Architecture, Intel
15 2) IA-32 Intel(R) Architecture Software Developer's Manual Volume 2:Instruction Set Reference, Intel
16 3) IA-32 Intel(R) Architecture Software Developer's Manual Volume 3:System Programmer's Guide, Intel
17
18 Copyright (c) 2006 - 2018, Intel Corporation. All rights reserved.<BR>
19 Copyright (c) 2017, AMD Incorporated. All rights reserved.<BR>
20
21 This program and the accompanying materials
22 are licensed and made available under the terms and conditions of the BSD License
23 which accompanies this distribution. The full text of the license may be found at
24 http://opensource.org/licenses/bsd-license.php
25
26 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
27 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
28
29 **/
30
31 #include "DxeIpl.h"
32 #include "VirtualMemory.h"
33
34 //
35 // Global variable to keep track current available memory used as page table.
36 //
37 PAGE_TABLE_POOL *mPageTablePool = NULL;
38
39 /**
40 Clear legacy memory located at the first 4K-page, if available.
41
42 This function traverses the whole HOB list to check if memory from 0 to 4095
43 exists and has not been allocated, and then clear it if so.
44
45 @param HobStart The start of HobList passed to DxeCore.
46
47 **/
48 VOID
49 ClearFirst4KPage (
50 IN VOID *HobStart
51 )
52 {
53 EFI_PEI_HOB_POINTERS RscHob;
54 EFI_PEI_HOB_POINTERS MemHob;
55 BOOLEAN DoClear;
56
57 RscHob.Raw = HobStart;
58 MemHob.Raw = HobStart;
59 DoClear = FALSE;
60
61 //
62 // Check if page 0 exists and free
63 //
64 while ((RscHob.Raw = GetNextHob (EFI_HOB_TYPE_RESOURCE_DESCRIPTOR,
65 RscHob.Raw)) != NULL) {
66 if (RscHob.ResourceDescriptor->ResourceType == EFI_RESOURCE_SYSTEM_MEMORY &&
67 RscHob.ResourceDescriptor->PhysicalStart == 0) {
68 DoClear = TRUE;
69 //
70 // Make sure memory at 0-4095 has not been allocated.
71 //
72 while ((MemHob.Raw = GetNextHob (EFI_HOB_TYPE_MEMORY_ALLOCATION,
73 MemHob.Raw)) != NULL) {
74 if (MemHob.MemoryAllocation->AllocDescriptor.MemoryBaseAddress
75 < EFI_PAGE_SIZE) {
76 DoClear = FALSE;
77 break;
78 }
79 MemHob.Raw = GET_NEXT_HOB (MemHob);
80 }
81 break;
82 }
83 RscHob.Raw = GET_NEXT_HOB (RscHob);
84 }
85
86 if (DoClear) {
87 DEBUG ((DEBUG_INFO, "Clearing first 4K-page!\r\n"));
88 SetMem (NULL, EFI_PAGE_SIZE, 0);
89 }
90
91 return;
92 }
93
94 /**
95 Return configure status of NULL pointer detection feature.
96
97 @return TRUE NULL pointer detection feature is enabled
98 @return FALSE NULL pointer detection feature is disabled
99
100 **/
101 BOOLEAN
102 IsNullDetectionEnabled (
103 VOID
104 )
105 {
106 return ((PcdGet8 (PcdNullPointerDetectionPropertyMask) & BIT0) != 0);
107 }
108
109 /**
110 Enable Execute Disable Bit.
111
112 **/
113 VOID
114 EnableExecuteDisableBit (
115 VOID
116 )
117 {
118 UINT64 MsrRegisters;
119
120 MsrRegisters = AsmReadMsr64 (0xC0000080);
121 MsrRegisters |= BIT11;
122 AsmWriteMsr64 (0xC0000080, MsrRegisters);
123 }
124
125 /**
126 The function will check if page table entry should be splitted to smaller
127 granularity.
128
129 @param Address Physical memory address.
130 @param Size Size of the given physical memory.
131 @param StackBase Base address of stack.
132 @param StackSize Size of stack.
133
134 @retval TRUE Page table should be split.
135 @retval FALSE Page table should not be split.
136 **/
137 BOOLEAN
138 ToSplitPageTable (
139 IN EFI_PHYSICAL_ADDRESS Address,
140 IN UINTN Size,
141 IN EFI_PHYSICAL_ADDRESS StackBase,
142 IN UINTN StackSize
143 )
144 {
145 if (IsNullDetectionEnabled () && Address == 0) {
146 return TRUE;
147 }
148
149 if (PcdGetBool (PcdCpuStackGuard)) {
150 if (StackBase >= Address && StackBase < (Address + Size)) {
151 return TRUE;
152 }
153 }
154
155 if (PcdGetBool (PcdSetNxForStack)) {
156 if ((Address < StackBase + StackSize) && ((Address + Size) > StackBase)) {
157 return TRUE;
158 }
159 }
160
161 return FALSE;
162 }
163 /**
164 Initialize a buffer pool for page table use only.
165
166 To reduce the potential split operation on page table, the pages reserved for
167 page table should be allocated in the times of PAGE_TABLE_POOL_UNIT_PAGES and
168 at the boundary of PAGE_TABLE_POOL_ALIGNMENT. So the page pool is always
169 initialized with number of pages greater than or equal to the given PoolPages.
170
171 Once the pages in the pool are used up, this method should be called again to
172 reserve at least another PAGE_TABLE_POOL_UNIT_PAGES. But usually this won't
173 happen in practice.
174
175 @param PoolPages The least page number of the pool to be created.
176
177 @retval TRUE The pool is initialized successfully.
178 @retval FALSE The memory is out of resource.
179 **/
180 BOOLEAN
181 InitializePageTablePool (
182 IN UINTN PoolPages
183 )
184 {
185 VOID *Buffer;
186
187 //
188 // Always reserve at least PAGE_TABLE_POOL_UNIT_PAGES, including one page for
189 // header.
190 //
191 PoolPages += 1; // Add one page for header.
192 PoolPages = ((PoolPages - 1) / PAGE_TABLE_POOL_UNIT_PAGES + 1) *
193 PAGE_TABLE_POOL_UNIT_PAGES;
194 Buffer = AllocateAlignedPages (PoolPages, PAGE_TABLE_POOL_ALIGNMENT);
195 if (Buffer == NULL) {
196 DEBUG ((DEBUG_ERROR, "ERROR: Out of aligned pages\r\n"));
197 return FALSE;
198 }
199
200 //
201 // Link all pools into a list for easier track later.
202 //
203 if (mPageTablePool == NULL) {
204 mPageTablePool = Buffer;
205 mPageTablePool->NextPool = mPageTablePool;
206 } else {
207 ((PAGE_TABLE_POOL *)Buffer)->NextPool = mPageTablePool->NextPool;
208 mPageTablePool->NextPool = Buffer;
209 mPageTablePool = Buffer;
210 }
211
212 //
213 // Reserve one page for pool header.
214 //
215 mPageTablePool->FreePages = PoolPages - 1;
216 mPageTablePool->Offset = EFI_PAGES_TO_SIZE (1);
217
218 return TRUE;
219 }
220
221 /**
222 This API provides a way to allocate memory for page table.
223
224 This API can be called more than once to allocate memory for page tables.
225
226 Allocates the number of 4KB pages and returns a pointer to the allocated
227 buffer. The buffer returned is aligned on a 4KB boundary.
228
229 If Pages is 0, then NULL is returned.
230 If there is not enough memory remaining to satisfy the request, then NULL is
231 returned.
232
233 @param Pages The number of 4 KB pages to allocate.
234
235 @return A pointer to the allocated buffer or NULL if allocation fails.
236
237 **/
238 VOID *
239 AllocatePageTableMemory (
240 IN UINTN Pages
241 )
242 {
243 VOID *Buffer;
244
245 if (Pages == 0) {
246 return NULL;
247 }
248
249 //
250 // Renew the pool if necessary.
251 //
252 if (mPageTablePool == NULL ||
253 Pages > mPageTablePool->FreePages) {
254 if (!InitializePageTablePool (Pages)) {
255 return NULL;
256 }
257 }
258
259 Buffer = (UINT8 *)mPageTablePool + mPageTablePool->Offset;
260
261 mPageTablePool->Offset += EFI_PAGES_TO_SIZE (Pages);
262 mPageTablePool->FreePages -= Pages;
263
264 return Buffer;
265 }
266
267 /**
268 Split 2M page to 4K.
269
270 @param[in] PhysicalAddress Start physical address the 2M page covered.
271 @param[in, out] PageEntry2M Pointer to 2M page entry.
272 @param[in] StackBase Stack base address.
273 @param[in] StackSize Stack size.
274
275 **/
276 VOID
277 Split2MPageTo4K (
278 IN EFI_PHYSICAL_ADDRESS PhysicalAddress,
279 IN OUT UINT64 *PageEntry2M,
280 IN EFI_PHYSICAL_ADDRESS StackBase,
281 IN UINTN StackSize
282 )
283 {
284 EFI_PHYSICAL_ADDRESS PhysicalAddress4K;
285 UINTN IndexOfPageTableEntries;
286 PAGE_TABLE_4K_ENTRY *PageTableEntry;
287 UINT64 AddressEncMask;
288
289 //
290 // Make sure AddressEncMask is contained to smallest supported address field
291 //
292 AddressEncMask = PcdGet64 (PcdPteMemoryEncryptionAddressOrMask) & PAGING_1G_ADDRESS_MASK_64;
293
294 PageTableEntry = AllocatePageTableMemory (1);
295 ASSERT (PageTableEntry != NULL);
296
297 //
298 // Fill in 2M page entry.
299 //
300 *PageEntry2M = (UINT64) (UINTN) PageTableEntry | AddressEncMask | IA32_PG_P | IA32_PG_RW;
301
302 PhysicalAddress4K = PhysicalAddress;
303 for (IndexOfPageTableEntries = 0; IndexOfPageTableEntries < 512; IndexOfPageTableEntries++, PageTableEntry++, PhysicalAddress4K += SIZE_4KB) {
304 //
305 // Fill in the Page Table entries
306 //
307 PageTableEntry->Uint64 = (UINT64) PhysicalAddress4K | AddressEncMask;
308 PageTableEntry->Bits.ReadWrite = 1;
309
310 if ((IsNullDetectionEnabled () && PhysicalAddress4K == 0) ||
311 (PcdGetBool (PcdCpuStackGuard) && PhysicalAddress4K == StackBase)) {
312 PageTableEntry->Bits.Present = 0;
313 } else {
314 PageTableEntry->Bits.Present = 1;
315 }
316
317 if (PcdGetBool (PcdSetNxForStack)
318 && (PhysicalAddress4K >= StackBase)
319 && (PhysicalAddress4K < StackBase + StackSize)) {
320 //
321 // Set Nx bit for stack.
322 //
323 PageTableEntry->Bits.Nx = 1;
324 }
325 }
326 }
327
328 /**
329 Split 1G page to 2M.
330
331 @param[in] PhysicalAddress Start physical address the 1G page covered.
332 @param[in, out] PageEntry1G Pointer to 1G page entry.
333 @param[in] StackBase Stack base address.
334 @param[in] StackSize Stack size.
335
336 **/
337 VOID
338 Split1GPageTo2M (
339 IN EFI_PHYSICAL_ADDRESS PhysicalAddress,
340 IN OUT UINT64 *PageEntry1G,
341 IN EFI_PHYSICAL_ADDRESS StackBase,
342 IN UINTN StackSize
343 )
344 {
345 EFI_PHYSICAL_ADDRESS PhysicalAddress2M;
346 UINTN IndexOfPageDirectoryEntries;
347 PAGE_TABLE_ENTRY *PageDirectoryEntry;
348 UINT64 AddressEncMask;
349
350 //
351 // Make sure AddressEncMask is contained to smallest supported address field
352 //
353 AddressEncMask = PcdGet64 (PcdPteMemoryEncryptionAddressOrMask) & PAGING_1G_ADDRESS_MASK_64;
354
355 PageDirectoryEntry = AllocatePageTableMemory (1);
356 ASSERT (PageDirectoryEntry != NULL);
357
358 //
359 // Fill in 1G page entry.
360 //
361 *PageEntry1G = (UINT64) (UINTN) PageDirectoryEntry | AddressEncMask | IA32_PG_P | IA32_PG_RW;
362
363 PhysicalAddress2M = PhysicalAddress;
364 for (IndexOfPageDirectoryEntries = 0; IndexOfPageDirectoryEntries < 512; IndexOfPageDirectoryEntries++, PageDirectoryEntry++, PhysicalAddress2M += SIZE_2MB) {
365 if (ToSplitPageTable (PhysicalAddress2M, SIZE_2MB, StackBase, StackSize)) {
366 //
367 // Need to split this 2M page that covers NULL or stack range.
368 //
369 Split2MPageTo4K (PhysicalAddress2M, (UINT64 *) PageDirectoryEntry, StackBase, StackSize);
370 } else {
371 //
372 // Fill in the Page Directory entries
373 //
374 PageDirectoryEntry->Uint64 = (UINT64) PhysicalAddress2M | AddressEncMask;
375 PageDirectoryEntry->Bits.ReadWrite = 1;
376 PageDirectoryEntry->Bits.Present = 1;
377 PageDirectoryEntry->Bits.MustBe1 = 1;
378 }
379 }
380 }
381
382 /**
383 Set one page of page table pool memory to be read-only.
384
385 @param[in] PageTableBase Base address of page table (CR3).
386 @param[in] Address Start address of a page to be set as read-only.
387 @param[in] Level4Paging Level 4 paging flag.
388
389 **/
390 VOID
391 SetPageTablePoolReadOnly (
392 IN UINTN PageTableBase,
393 IN EFI_PHYSICAL_ADDRESS Address,
394 IN BOOLEAN Level4Paging
395 )
396 {
397 UINTN Index;
398 UINTN EntryIndex;
399 UINT64 AddressEncMask;
400 EFI_PHYSICAL_ADDRESS PhysicalAddress;
401 UINT64 *PageTable;
402 UINT64 *NewPageTable;
403 UINT64 PageAttr;
404 UINT64 LevelSize[5];
405 UINT64 LevelMask[5];
406 UINTN LevelShift[5];
407 UINTN Level;
408 UINT64 PoolUnitSize;
409
410 ASSERT (PageTableBase != 0);
411
412 //
413 // Since the page table is always from page table pool, which is always
414 // located at the boundary of PcdPageTablePoolAlignment, we just need to
415 // set the whole pool unit to be read-only.
416 //
417 Address = Address & PAGE_TABLE_POOL_ALIGN_MASK;
418
419 LevelShift[1] = PAGING_L1_ADDRESS_SHIFT;
420 LevelShift[2] = PAGING_L2_ADDRESS_SHIFT;
421 LevelShift[3] = PAGING_L3_ADDRESS_SHIFT;
422 LevelShift[4] = PAGING_L4_ADDRESS_SHIFT;
423
424 LevelMask[1] = PAGING_4K_ADDRESS_MASK_64;
425 LevelMask[2] = PAGING_2M_ADDRESS_MASK_64;
426 LevelMask[3] = PAGING_1G_ADDRESS_MASK_64;
427 LevelMask[4] = PAGING_1G_ADDRESS_MASK_64;
428
429 LevelSize[1] = SIZE_4KB;
430 LevelSize[2] = SIZE_2MB;
431 LevelSize[3] = SIZE_1GB;
432 LevelSize[4] = SIZE_512GB;
433
434 AddressEncMask = PcdGet64 (PcdPteMemoryEncryptionAddressOrMask) &
435 PAGING_1G_ADDRESS_MASK_64;
436 PageTable = (UINT64 *)(UINTN)PageTableBase;
437 PoolUnitSize = PAGE_TABLE_POOL_UNIT_SIZE;
438
439 for (Level = (Level4Paging) ? 4 : 3; Level > 0; --Level) {
440 Index = ((UINTN)RShiftU64 (Address, LevelShift[Level]));
441 Index &= PAGING_PAE_INDEX_MASK;
442
443 PageAttr = PageTable[Index];
444 if ((PageAttr & IA32_PG_PS) == 0) {
445 //
446 // Go to next level of table.
447 //
448 PageTable = (UINT64 *)(UINTN)(PageAttr & ~AddressEncMask &
449 PAGING_4K_ADDRESS_MASK_64);
450 continue;
451 }
452
453 if (PoolUnitSize >= LevelSize[Level]) {
454 //
455 // Clear R/W bit if current page granularity is not larger than pool unit
456 // size.
457 //
458 if ((PageAttr & IA32_PG_RW) != 0) {
459 while (PoolUnitSize > 0) {
460 //
461 // PAGE_TABLE_POOL_UNIT_SIZE and PAGE_TABLE_POOL_ALIGNMENT are fit in
462 // one page (2MB). Then we don't need to update attributes for pages
463 // crossing page directory. ASSERT below is for that purpose.
464 //
465 ASSERT (Index < EFI_PAGE_SIZE/sizeof (UINT64));
466
467 PageTable[Index] &= ~(UINT64)IA32_PG_RW;
468 PoolUnitSize -= LevelSize[Level];
469
470 ++Index;
471 }
472 }
473
474 break;
475
476 } else {
477 //
478 // The smaller granularity of page must be needed.
479 //
480 ASSERT (Level > 1);
481
482 NewPageTable = AllocatePageTableMemory (1);
483 ASSERT (NewPageTable != NULL);
484
485 PhysicalAddress = PageAttr & LevelMask[Level];
486 for (EntryIndex = 0;
487 EntryIndex < EFI_PAGE_SIZE/sizeof (UINT64);
488 ++EntryIndex) {
489 NewPageTable[EntryIndex] = PhysicalAddress | AddressEncMask |
490 IA32_PG_P | IA32_PG_RW;
491 if (Level > 2) {
492 NewPageTable[EntryIndex] |= IA32_PG_PS;
493 }
494 PhysicalAddress += LevelSize[Level - 1];
495 }
496
497 PageTable[Index] = (UINT64)(UINTN)NewPageTable | AddressEncMask |
498 IA32_PG_P | IA32_PG_RW;
499 PageTable = NewPageTable;
500 }
501 }
502 }
503
504 /**
505 Prevent the memory pages used for page table from been overwritten.
506
507 @param[in] PageTableBase Base address of page table (CR3).
508 @param[in] Level4Paging Level 4 paging flag.
509
510 **/
511 VOID
512 EnablePageTableProtection (
513 IN UINTN PageTableBase,
514 IN BOOLEAN Level4Paging
515 )
516 {
517 PAGE_TABLE_POOL *HeadPool;
518 PAGE_TABLE_POOL *Pool;
519 UINT64 PoolSize;
520 EFI_PHYSICAL_ADDRESS Address;
521
522 if (mPageTablePool == NULL) {
523 return;
524 }
525
526 //
527 // Disable write protection, because we need to mark page table to be write
528 // protected.
529 //
530 AsmWriteCr0 (AsmReadCr0() & ~CR0_WP);
531
532 //
533 // SetPageTablePoolReadOnly might update mPageTablePool. It's safer to
534 // remember original one in advance.
535 //
536 HeadPool = mPageTablePool;
537 Pool = HeadPool;
538 do {
539 Address = (EFI_PHYSICAL_ADDRESS)(UINTN)Pool;
540 PoolSize = Pool->Offset + EFI_PAGES_TO_SIZE (Pool->FreePages);
541
542 //
543 // The size of one pool must be multiple of PAGE_TABLE_POOL_UNIT_SIZE, which
544 // is one of page size of the processor (2MB by default). Let's apply the
545 // protection to them one by one.
546 //
547 while (PoolSize > 0) {
548 SetPageTablePoolReadOnly(PageTableBase, Address, Level4Paging);
549 Address += PAGE_TABLE_POOL_UNIT_SIZE;
550 PoolSize -= PAGE_TABLE_POOL_UNIT_SIZE;
551 }
552
553 Pool = Pool->NextPool;
554 } while (Pool != HeadPool);
555
556 //
557 // Enable write protection, after page table attribute updated.
558 //
559 AsmWriteCr0 (AsmReadCr0() | CR0_WP);
560 }
561
562 /**
563 Allocates and fills in the Page Directory and Page Table Entries to
564 establish a 1:1 Virtual to Physical mapping.
565
566 @param[in] StackBase Stack base address.
567 @param[in] StackSize Stack size.
568
569 @return The address of 4 level page map.
570
571 **/
572 UINTN
573 CreateIdentityMappingPageTables (
574 IN EFI_PHYSICAL_ADDRESS StackBase,
575 IN UINTN StackSize
576 )
577 {
578 UINT32 RegEax;
579 UINT32 RegEdx;
580 UINT8 PhysicalAddressBits;
581 EFI_PHYSICAL_ADDRESS PageAddress;
582 UINTN IndexOfPml4Entries;
583 UINTN IndexOfPdpEntries;
584 UINTN IndexOfPageDirectoryEntries;
585 UINT32 NumberOfPml4EntriesNeeded;
586 UINT32 NumberOfPdpEntriesNeeded;
587 PAGE_MAP_AND_DIRECTORY_POINTER *PageMapLevel4Entry;
588 PAGE_MAP_AND_DIRECTORY_POINTER *PageMap;
589 PAGE_MAP_AND_DIRECTORY_POINTER *PageDirectoryPointerEntry;
590 PAGE_TABLE_ENTRY *PageDirectoryEntry;
591 UINTN TotalPagesNum;
592 UINTN BigPageAddress;
593 VOID *Hob;
594 BOOLEAN Page1GSupport;
595 PAGE_TABLE_1G_ENTRY *PageDirectory1GEntry;
596 UINT64 AddressEncMask;
597
598 //
599 // Make sure AddressEncMask is contained to smallest supported address field
600 //
601 AddressEncMask = PcdGet64 (PcdPteMemoryEncryptionAddressOrMask) & PAGING_1G_ADDRESS_MASK_64;
602
603 Page1GSupport = FALSE;
604 if (PcdGetBool(PcdUse1GPageTable)) {
605 AsmCpuid (0x80000000, &RegEax, NULL, NULL, NULL);
606 if (RegEax >= 0x80000001) {
607 AsmCpuid (0x80000001, NULL, NULL, NULL, &RegEdx);
608 if ((RegEdx & BIT26) != 0) {
609 Page1GSupport = TRUE;
610 }
611 }
612 }
613
614 //
615 // Get physical address bits supported.
616 //
617 Hob = GetFirstHob (EFI_HOB_TYPE_CPU);
618 if (Hob != NULL) {
619 PhysicalAddressBits = ((EFI_HOB_CPU *) Hob)->SizeOfMemorySpace;
620 } else {
621 AsmCpuid (0x80000000, &RegEax, NULL, NULL, NULL);
622 if (RegEax >= 0x80000008) {
623 AsmCpuid (0x80000008, &RegEax, NULL, NULL, NULL);
624 PhysicalAddressBits = (UINT8) RegEax;
625 } else {
626 PhysicalAddressBits = 36;
627 }
628 }
629
630 //
631 // IA-32e paging translates 48-bit linear addresses to 52-bit physical addresses.
632 //
633 ASSERT (PhysicalAddressBits <= 52);
634 if (PhysicalAddressBits > 48) {
635 PhysicalAddressBits = 48;
636 }
637
638 //
639 // Calculate the table entries needed.
640 //
641 if (PhysicalAddressBits <= 39 ) {
642 NumberOfPml4EntriesNeeded = 1;
643 NumberOfPdpEntriesNeeded = (UINT32)LShiftU64 (1, (PhysicalAddressBits - 30));
644 } else {
645 NumberOfPml4EntriesNeeded = (UINT32)LShiftU64 (1, (PhysicalAddressBits - 39));
646 NumberOfPdpEntriesNeeded = 512;
647 }
648
649 //
650 // Pre-allocate big pages to avoid later allocations.
651 //
652 if (!Page1GSupport) {
653 TotalPagesNum = (NumberOfPdpEntriesNeeded + 1) * NumberOfPml4EntriesNeeded + 1;
654 } else {
655 TotalPagesNum = NumberOfPml4EntriesNeeded + 1;
656 }
657 BigPageAddress = (UINTN) AllocatePageTableMemory (TotalPagesNum);
658 ASSERT (BigPageAddress != 0);
659
660 //
661 // By architecture only one PageMapLevel4 exists - so lets allocate storage for it.
662 //
663 PageMap = (VOID *) BigPageAddress;
664 BigPageAddress += SIZE_4KB;
665
666 PageMapLevel4Entry = PageMap;
667 PageAddress = 0;
668 for (IndexOfPml4Entries = 0; IndexOfPml4Entries < NumberOfPml4EntriesNeeded; IndexOfPml4Entries++, PageMapLevel4Entry++) {
669 //
670 // Each PML4 entry points to a page of Page Directory Pointer entires.
671 // So lets allocate space for them and fill them in in the IndexOfPdpEntries loop.
672 //
673 PageDirectoryPointerEntry = (VOID *) BigPageAddress;
674 BigPageAddress += SIZE_4KB;
675
676 //
677 // Make a PML4 Entry
678 //
679 PageMapLevel4Entry->Uint64 = (UINT64)(UINTN)PageDirectoryPointerEntry | AddressEncMask;
680 PageMapLevel4Entry->Bits.ReadWrite = 1;
681 PageMapLevel4Entry->Bits.Present = 1;
682
683 if (Page1GSupport) {
684 PageDirectory1GEntry = (VOID *) PageDirectoryPointerEntry;
685
686 for (IndexOfPageDirectoryEntries = 0; IndexOfPageDirectoryEntries < 512; IndexOfPageDirectoryEntries++, PageDirectory1GEntry++, PageAddress += SIZE_1GB) {
687 if (ToSplitPageTable (PageAddress, SIZE_1GB, StackBase, StackSize)) {
688 Split1GPageTo2M (PageAddress, (UINT64 *) PageDirectory1GEntry, StackBase, StackSize);
689 } else {
690 //
691 // Fill in the Page Directory entries
692 //
693 PageDirectory1GEntry->Uint64 = (UINT64)PageAddress | AddressEncMask;
694 PageDirectory1GEntry->Bits.ReadWrite = 1;
695 PageDirectory1GEntry->Bits.Present = 1;
696 PageDirectory1GEntry->Bits.MustBe1 = 1;
697 }
698 }
699 } else {
700 for (IndexOfPdpEntries = 0; IndexOfPdpEntries < NumberOfPdpEntriesNeeded; IndexOfPdpEntries++, PageDirectoryPointerEntry++) {
701 //
702 // Each Directory Pointer entries points to a page of Page Directory entires.
703 // So allocate space for them and fill them in in the IndexOfPageDirectoryEntries loop.
704 //
705 PageDirectoryEntry = (VOID *) BigPageAddress;
706 BigPageAddress += SIZE_4KB;
707
708 //
709 // Fill in a Page Directory Pointer Entries
710 //
711 PageDirectoryPointerEntry->Uint64 = (UINT64)(UINTN)PageDirectoryEntry | AddressEncMask;
712 PageDirectoryPointerEntry->Bits.ReadWrite = 1;
713 PageDirectoryPointerEntry->Bits.Present = 1;
714
715 for (IndexOfPageDirectoryEntries = 0; IndexOfPageDirectoryEntries < 512; IndexOfPageDirectoryEntries++, PageDirectoryEntry++, PageAddress += SIZE_2MB) {
716 if (ToSplitPageTable (PageAddress, SIZE_2MB, StackBase, StackSize)) {
717 //
718 // Need to split this 2M page that covers NULL or stack range.
719 //
720 Split2MPageTo4K (PageAddress, (UINT64 *) PageDirectoryEntry, StackBase, StackSize);
721 } else {
722 //
723 // Fill in the Page Directory entries
724 //
725 PageDirectoryEntry->Uint64 = (UINT64)PageAddress | AddressEncMask;
726 PageDirectoryEntry->Bits.ReadWrite = 1;
727 PageDirectoryEntry->Bits.Present = 1;
728 PageDirectoryEntry->Bits.MustBe1 = 1;
729 }
730 }
731 }
732
733 for (; IndexOfPdpEntries < 512; IndexOfPdpEntries++, PageDirectoryPointerEntry++) {
734 ZeroMem (
735 PageDirectoryPointerEntry,
736 sizeof(PAGE_MAP_AND_DIRECTORY_POINTER)
737 );
738 }
739 }
740 }
741
742 //
743 // For the PML4 entries we are not using fill in a null entry.
744 //
745 for (; IndexOfPml4Entries < 512; IndexOfPml4Entries++, PageMapLevel4Entry++) {
746 ZeroMem (
747 PageMapLevel4Entry,
748 sizeof (PAGE_MAP_AND_DIRECTORY_POINTER)
749 );
750 }
751
752 //
753 // Protect the page table by marking the memory used for page table to be
754 // read-only.
755 //
756 EnablePageTableProtection ((UINTN)PageMap, TRUE);
757
758 if (PcdGetBool (PcdSetNxForStack)) {
759 EnableExecuteDisableBit ();
760 }
761
762 return (UINTN)PageMap;
763 }
764