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