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