]> git.proxmox.com Git - mirror_edk2.git/blob - MdeModulePkg/Core/DxeIplPeim/X64/VirtualMemory.c
MdeModulePkg/DxeIpl: Mark page table as read-only
[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 - 2016, 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 @retval TRUE Page table should be split.
130 @retval FALSE Page table should not be split.
131 **/
132 BOOLEAN
133 ToSplitPageTable (
134 IN EFI_PHYSICAL_ADDRESS Address,
135 IN UINTN Size,
136 IN EFI_PHYSICAL_ADDRESS StackBase,
137 IN UINTN StackSize
138 )
139 {
140 if (IsNullDetectionEnabled () && Address == 0) {
141 return TRUE;
142 }
143
144 if (PcdGetBool (PcdCpuStackGuard)) {
145 if (StackBase >= Address && StackBase < (Address + Size)) {
146 return TRUE;
147 }
148 }
149
150 if (PcdGetBool (PcdSetNxForStack)) {
151 if ((Address < StackBase + StackSize) && ((Address + Size) > StackBase)) {
152 return TRUE;
153 }
154 }
155
156 return FALSE;
157 }
158 /**
159 Initialize a buffer pool for page table use only.
160
161 To reduce the potential split operation on page table, the pages reserved for
162 page table should be allocated in the times of PAGE_TABLE_POOL_UNIT_PAGES and
163 at the boundary of PAGE_TABLE_POOL_ALIGNMENT. So the page pool is always
164 initialized with number of pages greater than or equal to the given PoolPages.
165
166 Once the pages in the pool are used up, this method should be called again to
167 reserve at least another PAGE_TABLE_POOL_UNIT_PAGES. But usually this won't
168 happen in practice.
169
170 @param PoolPages The least page number of the pool to be created.
171
172 @retval TRUE The pool is initialized successfully.
173 @retval FALSE The memory is out of resource.
174 **/
175 BOOLEAN
176 InitializePageTablePool (
177 IN UINTN PoolPages
178 )
179 {
180 VOID *Buffer;
181
182 //
183 // Always reserve at least PAGE_TABLE_POOL_UNIT_PAGES, including one page for
184 // header.
185 //
186 PoolPages += 1; // Add one page for header.
187 PoolPages = ((PoolPages - 1) / PAGE_TABLE_POOL_UNIT_PAGES + 1) *
188 PAGE_TABLE_POOL_UNIT_PAGES;
189 Buffer = AllocateAlignedPages (PoolPages, PAGE_TABLE_POOL_ALIGNMENT);
190 if (Buffer == NULL) {
191 DEBUG ((DEBUG_ERROR, "ERROR: Out of aligned pages\r\n"));
192 return FALSE;
193 }
194
195 //
196 // Link all pools into a list for easier track later.
197 //
198 if (mPageTablePool == NULL) {
199 mPageTablePool = Buffer;
200 mPageTablePool->NextPool = mPageTablePool;
201 } else {
202 ((PAGE_TABLE_POOL *)Buffer)->NextPool = mPageTablePool->NextPool;
203 mPageTablePool->NextPool = Buffer;
204 mPageTablePool = Buffer;
205 }
206
207 //
208 // Reserve one page for pool header.
209 //
210 mPageTablePool->FreePages = PoolPages - 1;
211 mPageTablePool->Offset = EFI_PAGES_TO_SIZE (1);
212
213 return TRUE;
214 }
215
216 /**
217 This API provides a way to allocate memory for page table.
218
219 This API can be called more than once to allocate memory for page tables.
220
221 Allocates the number of 4KB pages and returns a pointer to the allocated
222 buffer. The buffer returned is aligned on a 4KB boundary.
223
224 If Pages is 0, then NULL is returned.
225 If there is not enough memory remaining to satisfy the request, then NULL is
226 returned.
227
228 @param Pages The number of 4 KB pages to allocate.
229
230 @return A pointer to the allocated buffer or NULL if allocation fails.
231
232 **/
233 VOID *
234 AllocatePageTableMemory (
235 IN UINTN Pages
236 )
237 {
238 VOID *Buffer;
239
240 if (Pages == 0) {
241 return NULL;
242 }
243
244 //
245 // Renew the pool if necessary.
246 //
247 if (mPageTablePool == NULL ||
248 Pages > mPageTablePool->FreePages) {
249 if (!InitializePageTablePool (Pages)) {
250 return NULL;
251 }
252 }
253
254 Buffer = (UINT8 *)mPageTablePool + mPageTablePool->Offset;
255
256 mPageTablePool->Offset += EFI_PAGES_TO_SIZE (Pages);
257 mPageTablePool->FreePages -= Pages;
258
259 return Buffer;
260 }
261
262 /**
263 Split 2M page to 4K.
264
265 @param[in] PhysicalAddress Start physical address the 2M page covered.
266 @param[in, out] PageEntry2M Pointer to 2M page entry.
267 @param[in] StackBase Stack base address.
268 @param[in] StackSize Stack size.
269
270 **/
271 VOID
272 Split2MPageTo4K (
273 IN EFI_PHYSICAL_ADDRESS PhysicalAddress,
274 IN OUT UINT64 *PageEntry2M,
275 IN EFI_PHYSICAL_ADDRESS StackBase,
276 IN UINTN StackSize
277 )
278 {
279 EFI_PHYSICAL_ADDRESS PhysicalAddress4K;
280 UINTN IndexOfPageTableEntries;
281 PAGE_TABLE_4K_ENTRY *PageTableEntry;
282 UINT64 AddressEncMask;
283
284 //
285 // Make sure AddressEncMask is contained to smallest supported address field
286 //
287 AddressEncMask = PcdGet64 (PcdPteMemoryEncryptionAddressOrMask) & PAGING_1G_ADDRESS_MASK_64;
288
289 PageTableEntry = AllocatePageTableMemory (1);
290 ASSERT (PageTableEntry != NULL);
291
292 //
293 // Fill in 2M page entry.
294 //
295 *PageEntry2M = (UINT64) (UINTN) PageTableEntry | AddressEncMask | IA32_PG_P | IA32_PG_RW;
296
297 PhysicalAddress4K = PhysicalAddress;
298 for (IndexOfPageTableEntries = 0; IndexOfPageTableEntries < 512; IndexOfPageTableEntries++, PageTableEntry++, PhysicalAddress4K += SIZE_4KB) {
299 //
300 // Fill in the Page Table entries
301 //
302 PageTableEntry->Uint64 = (UINT64) PhysicalAddress4K | AddressEncMask;
303 PageTableEntry->Bits.ReadWrite = 1;
304
305 if ((IsNullDetectionEnabled () && PhysicalAddress4K == 0) ||
306 (PcdGetBool (PcdCpuStackGuard) && PhysicalAddress4K == StackBase)) {
307 PageTableEntry->Bits.Present = 0;
308 } else {
309 PageTableEntry->Bits.Present = 1;
310 }
311
312 if (PcdGetBool (PcdSetNxForStack)
313 && (PhysicalAddress4K >= StackBase)
314 && (PhysicalAddress4K < StackBase + StackSize)) {
315 //
316 // Set Nx bit for stack.
317 //
318 PageTableEntry->Bits.Nx = 1;
319 }
320 }
321 }
322
323 /**
324 Split 1G page to 2M.
325
326 @param[in] PhysicalAddress Start physical address the 1G page covered.
327 @param[in, out] PageEntry1G Pointer to 1G page entry.
328 @param[in] StackBase Stack base address.
329 @param[in] StackSize Stack size.
330
331 **/
332 VOID
333 Split1GPageTo2M (
334 IN EFI_PHYSICAL_ADDRESS PhysicalAddress,
335 IN OUT UINT64 *PageEntry1G,
336 IN EFI_PHYSICAL_ADDRESS StackBase,
337 IN UINTN StackSize
338 )
339 {
340 EFI_PHYSICAL_ADDRESS PhysicalAddress2M;
341 UINTN IndexOfPageDirectoryEntries;
342 PAGE_TABLE_ENTRY *PageDirectoryEntry;
343 UINT64 AddressEncMask;
344
345 //
346 // Make sure AddressEncMask is contained to smallest supported address field
347 //
348 AddressEncMask = PcdGet64 (PcdPteMemoryEncryptionAddressOrMask) & PAGING_1G_ADDRESS_MASK_64;
349
350 PageDirectoryEntry = AllocatePageTableMemory (1);
351 ASSERT (PageDirectoryEntry != NULL);
352
353 //
354 // Fill in 1G page entry.
355 //
356 *PageEntry1G = (UINT64) (UINTN) PageDirectoryEntry | AddressEncMask | IA32_PG_P | IA32_PG_RW;
357
358 PhysicalAddress2M = PhysicalAddress;
359 for (IndexOfPageDirectoryEntries = 0; IndexOfPageDirectoryEntries < 512; IndexOfPageDirectoryEntries++, PageDirectoryEntry++, PhysicalAddress2M += SIZE_2MB) {
360 if (ToSplitPageTable (PhysicalAddress2M, SIZE_2MB, StackBase, StackSize)) {
361 //
362 // Need to split this 2M page that covers NULL or stack range.
363 //
364 Split2MPageTo4K (PhysicalAddress2M, (UINT64 *) PageDirectoryEntry, StackBase, StackSize);
365 } else {
366 //
367 // Fill in the Page Directory entries
368 //
369 PageDirectoryEntry->Uint64 = (UINT64) PhysicalAddress2M | AddressEncMask;
370 PageDirectoryEntry->Bits.ReadWrite = 1;
371 PageDirectoryEntry->Bits.Present = 1;
372 PageDirectoryEntry->Bits.MustBe1 = 1;
373 }
374 }
375 }
376
377 /**
378 Set one page of page table pool memory to be read-only.
379
380 @param[in] PageTableBase Base address of page table (CR3).
381 @param[in] Address Start address of a page to be set as read-only.
382 @param[in] Level4Paging Level 4 paging flag.
383
384 **/
385 VOID
386 SetPageTablePoolReadOnly (
387 IN UINTN PageTableBase,
388 IN EFI_PHYSICAL_ADDRESS Address,
389 IN BOOLEAN Level4Paging
390 )
391 {
392 UINTN Index;
393 UINTN EntryIndex;
394 UINT64 AddressEncMask;
395 EFI_PHYSICAL_ADDRESS PhysicalAddress;
396 UINT64 *PageTable;
397 UINT64 *NewPageTable;
398 UINT64 PageAttr;
399 UINT64 LevelSize[5];
400 UINT64 LevelMask[5];
401 UINTN LevelShift[5];
402 UINTN Level;
403 UINT64 PoolUnitSize;
404
405 ASSERT (PageTableBase != 0);
406
407 //
408 // Since the page table is always from page table pool, which is always
409 // located at the boundary of PcdPageTablePoolAlignment, we just need to
410 // set the whole pool unit to be read-only.
411 //
412 Address = Address & PAGE_TABLE_POOL_ALIGN_MASK;
413
414 LevelShift[1] = PAGING_L1_ADDRESS_SHIFT;
415 LevelShift[2] = PAGING_L2_ADDRESS_SHIFT;
416 LevelShift[3] = PAGING_L3_ADDRESS_SHIFT;
417 LevelShift[4] = PAGING_L4_ADDRESS_SHIFT;
418
419 LevelMask[1] = PAGING_4K_ADDRESS_MASK_64;
420 LevelMask[2] = PAGING_2M_ADDRESS_MASK_64;
421 LevelMask[3] = PAGING_1G_ADDRESS_MASK_64;
422 LevelMask[4] = PAGING_1G_ADDRESS_MASK_64;
423
424 LevelSize[1] = SIZE_4KB;
425 LevelSize[2] = SIZE_2MB;
426 LevelSize[3] = SIZE_1GB;
427 LevelSize[4] = SIZE_512GB;
428
429 AddressEncMask = PcdGet64 (PcdPteMemoryEncryptionAddressOrMask) &
430 PAGING_1G_ADDRESS_MASK_64;
431 PageTable = (UINT64 *)(UINTN)PageTableBase;
432 PoolUnitSize = PAGE_TABLE_POOL_UNIT_SIZE;
433
434 for (Level = (Level4Paging) ? 4 : 3; Level > 0; --Level) {
435 Index = ((UINTN)RShiftU64 (Address, LevelShift[Level]));
436 Index &= PAGING_PAE_INDEX_MASK;
437
438 PageAttr = PageTable[Index];
439 if ((PageAttr & IA32_PG_PS) == 0) {
440 //
441 // Go to next level of table.
442 //
443 PageTable = (UINT64 *)(UINTN)(PageAttr & ~AddressEncMask &
444 PAGING_4K_ADDRESS_MASK_64);
445 continue;
446 }
447
448 if (PoolUnitSize >= LevelSize[Level]) {
449 //
450 // Clear R/W bit if current page granularity is not larger than pool unit
451 // size.
452 //
453 if ((PageAttr & IA32_PG_RW) != 0) {
454 while (PoolUnitSize > 0) {
455 //
456 // PAGE_TABLE_POOL_UNIT_SIZE and PAGE_TABLE_POOL_ALIGNMENT are fit in
457 // one page (2MB). Then we don't need to update attributes for pages
458 // crossing page directory. ASSERT below is for that purpose.
459 //
460 ASSERT (Index < EFI_PAGE_SIZE/sizeof (UINT64));
461
462 PageTable[Index] &= ~(UINT64)IA32_PG_RW;
463 PoolUnitSize -= LevelSize[Level];
464
465 ++Index;
466 }
467 }
468
469 break;
470
471 } else {
472 //
473 // The smaller granularity of page must be needed.
474 //
475 NewPageTable = AllocatePageTableMemory (1);
476 ASSERT (NewPageTable != NULL);
477
478 PhysicalAddress = PageAttr & LevelMask[Level];
479 for (EntryIndex = 0;
480 EntryIndex < EFI_PAGE_SIZE/sizeof (UINT64);
481 ++EntryIndex) {
482 NewPageTable[EntryIndex] = PhysicalAddress | AddressEncMask |
483 IA32_PG_P | IA32_PG_RW;
484 if (Level > 1) {
485 NewPageTable[EntryIndex] |= IA32_PG_PS;
486 }
487 PhysicalAddress += LevelSize[Level];
488 }
489
490 PageTable[Index] = (UINT64)(UINTN)NewPageTable | AddressEncMask |
491 IA32_PG_P | IA32_PG_RW;
492 PageTable = NewPageTable;
493 }
494 }
495 }
496
497 /**
498 Prevent the memory pages used for page table from been overwritten.
499
500 @param[in] PageTableBase Base address of page table (CR3).
501 @param[in] Level4Paging Level 4 paging flag.
502
503 **/
504 VOID
505 EnablePageTableProtection (
506 IN UINTN PageTableBase,
507 IN BOOLEAN Level4Paging
508 )
509 {
510 PAGE_TABLE_POOL *HeadPool;
511 PAGE_TABLE_POOL *Pool;
512 UINT64 PoolSize;
513 EFI_PHYSICAL_ADDRESS Address;
514
515 if (mPageTablePool == NULL) {
516 return;
517 }
518
519 //
520 // Disable write protection, because we need to mark page table to be write
521 // protected.
522 //
523 AsmWriteCr0 (AsmReadCr0() & ~CR0_WP);
524
525 //
526 // SetPageTablePoolReadOnly might update mPageTablePool. It's safer to
527 // remember original one in advance.
528 //
529 HeadPool = mPageTablePool;
530 Pool = HeadPool;
531 do {
532 Address = (EFI_PHYSICAL_ADDRESS)(UINTN)Pool;
533 PoolSize = Pool->Offset + EFI_PAGES_TO_SIZE (Pool->FreePages);
534
535 //
536 // The size of one pool must be multiple of PAGE_TABLE_POOL_UNIT_SIZE, which
537 // is one of page size of the processor (2MB by default). Let's apply the
538 // protection to them one by one.
539 //
540 while (PoolSize > 0) {
541 SetPageTablePoolReadOnly(PageTableBase, Address, Level4Paging);
542 Address += PAGE_TABLE_POOL_UNIT_SIZE;
543 PoolSize -= PAGE_TABLE_POOL_UNIT_SIZE;
544 }
545
546 Pool = Pool->NextPool;
547 } while (Pool != HeadPool);
548
549 //
550 // Enable write protection, after page table attribute updated.
551 //
552 AsmWriteCr0 (AsmReadCr0() | CR0_WP);
553 }
554
555 /**
556 Allocates and fills in the Page Directory and Page Table Entries to
557 establish a 1:1 Virtual to Physical mapping.
558
559 @param[in] StackBase Stack base address.
560 @param[in] StackSize Stack size.
561
562 @return The address of 4 level page map.
563
564 **/
565 UINTN
566 CreateIdentityMappingPageTables (
567 IN EFI_PHYSICAL_ADDRESS StackBase,
568 IN UINTN StackSize
569 )
570 {
571 UINT32 RegEax;
572 UINT32 RegEdx;
573 UINT8 PhysicalAddressBits;
574 EFI_PHYSICAL_ADDRESS PageAddress;
575 UINTN IndexOfPml4Entries;
576 UINTN IndexOfPdpEntries;
577 UINTN IndexOfPageDirectoryEntries;
578 UINT32 NumberOfPml4EntriesNeeded;
579 UINT32 NumberOfPdpEntriesNeeded;
580 PAGE_MAP_AND_DIRECTORY_POINTER *PageMapLevel4Entry;
581 PAGE_MAP_AND_DIRECTORY_POINTER *PageMap;
582 PAGE_MAP_AND_DIRECTORY_POINTER *PageDirectoryPointerEntry;
583 PAGE_TABLE_ENTRY *PageDirectoryEntry;
584 UINTN TotalPagesNum;
585 UINTN BigPageAddress;
586 VOID *Hob;
587 BOOLEAN Page1GSupport;
588 PAGE_TABLE_1G_ENTRY *PageDirectory1GEntry;
589 UINT64 AddressEncMask;
590
591 //
592 // Make sure AddressEncMask is contained to smallest supported address field
593 //
594 AddressEncMask = PcdGet64 (PcdPteMemoryEncryptionAddressOrMask) & PAGING_1G_ADDRESS_MASK_64;
595
596 Page1GSupport = FALSE;
597 if (PcdGetBool(PcdUse1GPageTable)) {
598 AsmCpuid (0x80000000, &RegEax, NULL, NULL, NULL);
599 if (RegEax >= 0x80000001) {
600 AsmCpuid (0x80000001, NULL, NULL, NULL, &RegEdx);
601 if ((RegEdx & BIT26) != 0) {
602 Page1GSupport = TRUE;
603 }
604 }
605 }
606
607 //
608 // Get physical address bits supported.
609 //
610 Hob = GetFirstHob (EFI_HOB_TYPE_CPU);
611 if (Hob != NULL) {
612 PhysicalAddressBits = ((EFI_HOB_CPU *) Hob)->SizeOfMemorySpace;
613 } else {
614 AsmCpuid (0x80000000, &RegEax, NULL, NULL, NULL);
615 if (RegEax >= 0x80000008) {
616 AsmCpuid (0x80000008, &RegEax, NULL, NULL, NULL);
617 PhysicalAddressBits = (UINT8) RegEax;
618 } else {
619 PhysicalAddressBits = 36;
620 }
621 }
622
623 //
624 // IA-32e paging translates 48-bit linear addresses to 52-bit physical addresses.
625 //
626 ASSERT (PhysicalAddressBits <= 52);
627 if (PhysicalAddressBits > 48) {
628 PhysicalAddressBits = 48;
629 }
630
631 //
632 // Calculate the table entries needed.
633 //
634 if (PhysicalAddressBits <= 39 ) {
635 NumberOfPml4EntriesNeeded = 1;
636 NumberOfPdpEntriesNeeded = (UINT32)LShiftU64 (1, (PhysicalAddressBits - 30));
637 } else {
638 NumberOfPml4EntriesNeeded = (UINT32)LShiftU64 (1, (PhysicalAddressBits - 39));
639 NumberOfPdpEntriesNeeded = 512;
640 }
641
642 //
643 // Pre-allocate big pages to avoid later allocations.
644 //
645 if (!Page1GSupport) {
646 TotalPagesNum = (NumberOfPdpEntriesNeeded + 1) * NumberOfPml4EntriesNeeded + 1;
647 } else {
648 TotalPagesNum = NumberOfPml4EntriesNeeded + 1;
649 }
650 BigPageAddress = (UINTN) AllocatePageTableMemory (TotalPagesNum);
651 ASSERT (BigPageAddress != 0);
652
653 //
654 // By architecture only one PageMapLevel4 exists - so lets allocate storage for it.
655 //
656 PageMap = (VOID *) BigPageAddress;
657 BigPageAddress += SIZE_4KB;
658
659 PageMapLevel4Entry = PageMap;
660 PageAddress = 0;
661 for (IndexOfPml4Entries = 0; IndexOfPml4Entries < NumberOfPml4EntriesNeeded; IndexOfPml4Entries++, PageMapLevel4Entry++) {
662 //
663 // Each PML4 entry points to a page of Page Directory Pointer entires.
664 // So lets allocate space for them and fill them in in the IndexOfPdpEntries loop.
665 //
666 PageDirectoryPointerEntry = (VOID *) BigPageAddress;
667 BigPageAddress += SIZE_4KB;
668
669 //
670 // Make a PML4 Entry
671 //
672 PageMapLevel4Entry->Uint64 = (UINT64)(UINTN)PageDirectoryPointerEntry | AddressEncMask;
673 PageMapLevel4Entry->Bits.ReadWrite = 1;
674 PageMapLevel4Entry->Bits.Present = 1;
675
676 if (Page1GSupport) {
677 PageDirectory1GEntry = (VOID *) PageDirectoryPointerEntry;
678
679 for (IndexOfPageDirectoryEntries = 0; IndexOfPageDirectoryEntries < 512; IndexOfPageDirectoryEntries++, PageDirectory1GEntry++, PageAddress += SIZE_1GB) {
680 if (ToSplitPageTable (PageAddress, SIZE_1GB, StackBase, StackSize)) {
681 Split1GPageTo2M (PageAddress, (UINT64 *) PageDirectory1GEntry, StackBase, StackSize);
682 } else {
683 //
684 // Fill in the Page Directory entries
685 //
686 PageDirectory1GEntry->Uint64 = (UINT64)PageAddress | AddressEncMask;
687 PageDirectory1GEntry->Bits.ReadWrite = 1;
688 PageDirectory1GEntry->Bits.Present = 1;
689 PageDirectory1GEntry->Bits.MustBe1 = 1;
690 }
691 }
692 } else {
693 for (IndexOfPdpEntries = 0; IndexOfPdpEntries < NumberOfPdpEntriesNeeded; IndexOfPdpEntries++, PageDirectoryPointerEntry++) {
694 //
695 // Each Directory Pointer entries points to a page of Page Directory entires.
696 // So allocate space for them and fill them in in the IndexOfPageDirectoryEntries loop.
697 //
698 PageDirectoryEntry = (VOID *) BigPageAddress;
699 BigPageAddress += SIZE_4KB;
700
701 //
702 // Fill in a Page Directory Pointer Entries
703 //
704 PageDirectoryPointerEntry->Uint64 = (UINT64)(UINTN)PageDirectoryEntry | AddressEncMask;
705 PageDirectoryPointerEntry->Bits.ReadWrite = 1;
706 PageDirectoryPointerEntry->Bits.Present = 1;
707
708 for (IndexOfPageDirectoryEntries = 0; IndexOfPageDirectoryEntries < 512; IndexOfPageDirectoryEntries++, PageDirectoryEntry++, PageAddress += SIZE_2MB) {
709 if (ToSplitPageTable (PageAddress, SIZE_2MB, StackBase, StackSize)) {
710 //
711 // Need to split this 2M page that covers NULL or stack range.
712 //
713 Split2MPageTo4K (PageAddress, (UINT64 *) PageDirectoryEntry, StackBase, StackSize);
714 } else {
715 //
716 // Fill in the Page Directory entries
717 //
718 PageDirectoryEntry->Uint64 = (UINT64)PageAddress | AddressEncMask;
719 PageDirectoryEntry->Bits.ReadWrite = 1;
720 PageDirectoryEntry->Bits.Present = 1;
721 PageDirectoryEntry->Bits.MustBe1 = 1;
722 }
723 }
724 }
725
726 for (; IndexOfPdpEntries < 512; IndexOfPdpEntries++, PageDirectoryPointerEntry++) {
727 ZeroMem (
728 PageDirectoryPointerEntry,
729 sizeof(PAGE_MAP_AND_DIRECTORY_POINTER)
730 );
731 }
732 }
733 }
734
735 //
736 // For the PML4 entries we are not using fill in a null entry.
737 //
738 for (; IndexOfPml4Entries < 512; IndexOfPml4Entries++, PageMapLevel4Entry++) {
739 ZeroMem (
740 PageMapLevel4Entry,
741 sizeof (PAGE_MAP_AND_DIRECTORY_POINTER)
742 );
743 }
744
745 //
746 // Protect the page table by marking the memory used for page table to be
747 // read-only.
748 //
749 EnablePageTableProtection ((UINTN)PageMap, TRUE);
750
751 if (PcdGetBool (PcdSetNxForStack)) {
752 EnableExecuteDisableBit ();
753 }
754
755 return (UINTN)PageMap;
756 }
757