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