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