2 Page table management support.
4 Copyright (c) 2017, Intel Corporation. All rights reserved.<BR>
5 Copyright (c) 2017, AMD Incorporated. All rights reserved.<BR>
7 This program and the accompanying materials
8 are licensed and made available under the terms and conditions of the BSD License
9 which accompanies this distribution. The full text of the license may be found at
10 http://opensource.org/licenses/bsd-license.php
12 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
13 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
19 #include <Library/BaseLib.h>
20 #include <Library/CpuLib.h>
21 #include <Library/BaseMemoryLib.h>
22 #include <Library/MemoryAllocationLib.h>
23 #include <Library/DebugLib.h>
24 #include <Library/UefiBootServicesTableLib.h>
25 #include <Protocol/MpService.h>
26 #include <Protocol/SmmBase2.h>
27 #include <Register/Cpuid.h>
28 #include <Register/Msr.h>
31 #include "CpuPageTable.h"
44 #define IA32_PG_P BIT0
45 #define IA32_PG_RW BIT1
46 #define IA32_PG_U BIT2
47 #define IA32_PG_WT BIT3
48 #define IA32_PG_CD BIT4
49 #define IA32_PG_A BIT5
50 #define IA32_PG_D BIT6
51 #define IA32_PG_PS BIT7
52 #define IA32_PG_PAT_2M BIT12
53 #define IA32_PG_PAT_4K IA32_PG_PS
54 #define IA32_PG_PMNT BIT62
55 #define IA32_PG_NX BIT63
57 #define PAGE_ATTRIBUTE_BITS (IA32_PG_D | IA32_PG_A | IA32_PG_U | IA32_PG_RW | IA32_PG_P)
59 // Bits 1, 2, 5, 6 are reserved in the IA32 PAE PDPTE
60 // X64 PAE PDPTE does not have such restriction
62 #define IA32_PAE_PDPTE_ATTRIBUTE_BITS (IA32_PG_P)
64 #define PAGE_PROGATE_BITS (IA32_PG_NX | PAGE_ATTRIBUTE_BITS)
66 #define PAGING_4K_MASK 0xFFF
67 #define PAGING_2M_MASK 0x1FFFFF
68 #define PAGING_1G_MASK 0x3FFFFFFF
70 #define PAGING_PAE_INDEX_MASK 0x1FF
72 #define PAGING_4K_ADDRESS_MASK_64 0x000FFFFFFFFFF000ull
73 #define PAGING_2M_ADDRESS_MASK_64 0x000FFFFFFFE00000ull
74 #define PAGING_1G_ADDRESS_MASK_64 0x000FFFFFC0000000ull
84 PAGE_ATTRIBUTE Attribute
;
87 } PAGE_ATTRIBUTE_TABLE
;
95 PAGE_ATTRIBUTE_TABLE mPageAttributeTable
[] = {
96 {Page4K
, SIZE_4KB
, PAGING_4K_ADDRESS_MASK_64
},
97 {Page2M
, SIZE_2MB
, PAGING_2M_ADDRESS_MASK_64
},
98 {Page1G
, SIZE_1GB
, PAGING_1G_ADDRESS_MASK_64
},
101 PAGE_TABLE_POOL
*mPageTablePool
= NULL
;
102 PAGE_TABLE_LIB_PAGING_CONTEXT mPagingContext
;
103 EFI_SMM_BASE2_PROTOCOL
*mSmmBase2
= NULL
;
106 Check if current execution environment is in SMM mode or not, via
107 EFI_SMM_BASE2_PROTOCOL.
109 This is necessary because of the fact that MdePkg\Library\SmmMemoryAllocationLib
110 supports to free memory outside SMRAM. The library will call gBS->FreePool() or
111 gBS->FreePages() and then SetMemorySpaceAttributes interface in turn to change
112 memory paging attributes during free operation, if some memory related features
113 are enabled (like Heap Guard).
115 This means that SetMemorySpaceAttributes() has chance to run in SMM mode. This
116 will cause incorrect result because SMM mode always loads its own page tables,
117 which are usually different from DXE. This function can be used to detect such
118 situation and help to avoid further misoperations.
120 @retval TRUE In SMM mode.
121 @retval FALSE Not in SMM mode.
131 if (mSmmBase2
== NULL
) {
132 gBS
->LocateProtocol (&gEfiSmmBase2ProtocolGuid
, NULL
, (VOID
**)&mSmmBase2
);
135 if (mSmmBase2
!= NULL
) {
136 mSmmBase2
->InSmm (mSmmBase2
, &InSmm
);
140 // mSmmBase2->InSmm() can only detect if the caller is running in SMRAM
141 // or from SMM driver. It cannot tell if the caller is running in SMM mode.
142 // Check page table base address to guarantee that because SMM mode willl
143 // load its own page table.
146 mPagingContext
.ContextData
.X64
.PageTableBase
!= (UINT64
)AsmReadCr3());
150 Return current paging context.
152 @param[in,out] PagingContext The paging context.
155 GetCurrentPagingContext (
156 IN OUT PAGE_TABLE_LIB_PAGING_CONTEXT
*PagingContext
160 CPUID_EXTENDED_CPU_SIG_EDX RegEdx
;
161 MSR_IA32_EFER_REGISTER MsrEfer
;
164 // Don't retrieve current paging context from processor if in SMM mode.
167 ZeroMem (&mPagingContext
, sizeof(mPagingContext
));
168 if (sizeof(UINTN
) == sizeof(UINT64
)) {
169 mPagingContext
.MachineType
= IMAGE_FILE_MACHINE_X64
;
171 mPagingContext
.MachineType
= IMAGE_FILE_MACHINE_I386
;
173 if ((AsmReadCr0 () & CR0_PG
) != 0) {
174 mPagingContext
.ContextData
.X64
.PageTableBase
= (AsmReadCr3 () & PAGING_4K_ADDRESS_MASK_64
);
176 mPagingContext
.ContextData
.X64
.PageTableBase
= 0;
179 if ((AsmReadCr4 () & CR4_PSE
) != 0) {
180 mPagingContext
.ContextData
.Ia32
.Attributes
|= PAGE_TABLE_LIB_PAGING_CONTEXT_IA32_X64_ATTRIBUTES_PSE
;
182 if ((AsmReadCr4 () & CR4_PAE
) != 0) {
183 mPagingContext
.ContextData
.Ia32
.Attributes
|= PAGE_TABLE_LIB_PAGING_CONTEXT_IA32_X64_ATTRIBUTES_PAE
;
185 if ((AsmReadCr0 () & CR0_WP
) != 0) {
186 mPagingContext
.ContextData
.Ia32
.Attributes
|= PAGE_TABLE_LIB_PAGING_CONTEXT_IA32_X64_ATTRIBUTES_WP_ENABLE
;
189 AsmCpuid (CPUID_EXTENDED_FUNCTION
, &RegEax
, NULL
, NULL
, NULL
);
190 if (RegEax
>= CPUID_EXTENDED_CPU_SIG
) {
191 AsmCpuid (CPUID_EXTENDED_CPU_SIG
, NULL
, NULL
, NULL
, &RegEdx
.Uint32
);
193 if (RegEdx
.Bits
.NX
!= 0) {
195 MsrEfer
.Uint64
= AsmReadMsr64(MSR_CORE_IA32_EFER
);
196 if (MsrEfer
.Bits
.NXE
!= 0) {
198 mPagingContext
.ContextData
.Ia32
.Attributes
|= PAGE_TABLE_LIB_PAGING_CONTEXT_IA32_X64_ATTRIBUTES_XD_ACTIVATED
;
202 if (RegEdx
.Bits
.Page1GB
!= 0) {
203 mPagingContext
.ContextData
.Ia32
.Attributes
|= PAGE_TABLE_LIB_PAGING_CONTEXT_IA32_X64_ATTRIBUTES_PAGE_1G_SUPPORT
;
209 // This can avoid getting SMM paging context if in SMM mode. We cannot assume
210 // SMM mode shares the same paging context as DXE.
212 CopyMem (PagingContext
, &mPagingContext
, sizeof (mPagingContext
));
216 Return length according to page attributes.
218 @param[in] PageAttributes The page attribute of the page entry.
220 @return The length of page entry.
223 PageAttributeToLength (
224 IN PAGE_ATTRIBUTE PageAttribute
228 for (Index
= 0; Index
< sizeof(mPageAttributeTable
)/sizeof(mPageAttributeTable
[0]); Index
++) {
229 if (PageAttribute
== mPageAttributeTable
[Index
].Attribute
) {
230 return (UINTN
)mPageAttributeTable
[Index
].Length
;
237 Return address mask according to page attributes.
239 @param[in] PageAttributes The page attribute of the page entry.
241 @return The address mask of page entry.
244 PageAttributeToMask (
245 IN PAGE_ATTRIBUTE PageAttribute
249 for (Index
= 0; Index
< sizeof(mPageAttributeTable
)/sizeof(mPageAttributeTable
[0]); Index
++) {
250 if (PageAttribute
== mPageAttributeTable
[Index
].Attribute
) {
251 return (UINTN
)mPageAttributeTable
[Index
].AddressMask
;
258 Return page table entry to match the address.
260 @param[in] PagingContext The paging context.
261 @param[in] Address The address to be checked.
262 @param[out] PageAttributes The page attribute of the page entry.
264 @return The page entry.
268 IN PAGE_TABLE_LIB_PAGING_CONTEXT
*PagingContext
,
269 IN PHYSICAL_ADDRESS Address
,
270 OUT PAGE_ATTRIBUTE
*PageAttribute
281 UINT64 AddressEncMask
;
283 ASSERT (PagingContext
!= NULL
);
285 Index4
= ((UINTN
)RShiftU64 (Address
, 39)) & PAGING_PAE_INDEX_MASK
;
286 Index3
= ((UINTN
)Address
>> 30) & PAGING_PAE_INDEX_MASK
;
287 Index2
= ((UINTN
)Address
>> 21) & PAGING_PAE_INDEX_MASK
;
288 Index1
= ((UINTN
)Address
>> 12) & PAGING_PAE_INDEX_MASK
;
290 // Make sure AddressEncMask is contained to smallest supported address field.
292 AddressEncMask
= PcdGet64 (PcdPteMemoryEncryptionAddressOrMask
) & PAGING_1G_ADDRESS_MASK_64
;
294 if (PagingContext
->MachineType
== IMAGE_FILE_MACHINE_X64
) {
295 L4PageTable
= (UINT64
*)(UINTN
)PagingContext
->ContextData
.X64
.PageTableBase
;
296 if (L4PageTable
[Index4
] == 0) {
297 *PageAttribute
= PageNone
;
301 L3PageTable
= (UINT64
*)(UINTN
)(L4PageTable
[Index4
] & ~AddressEncMask
& PAGING_4K_ADDRESS_MASK_64
);
303 ASSERT((PagingContext
->ContextData
.Ia32
.Attributes
& PAGE_TABLE_LIB_PAGING_CONTEXT_IA32_X64_ATTRIBUTES_PAE
) != 0);
304 L3PageTable
= (UINT64
*)(UINTN
)PagingContext
->ContextData
.Ia32
.PageTableBase
;
306 if (L3PageTable
[Index3
] == 0) {
307 *PageAttribute
= PageNone
;
310 if ((L3PageTable
[Index3
] & IA32_PG_PS
) != 0) {
312 *PageAttribute
= Page1G
;
313 return &L3PageTable
[Index3
];
316 L2PageTable
= (UINT64
*)(UINTN
)(L3PageTable
[Index3
] & ~AddressEncMask
& PAGING_4K_ADDRESS_MASK_64
);
317 if (L2PageTable
[Index2
] == 0) {
318 *PageAttribute
= PageNone
;
321 if ((L2PageTable
[Index2
] & IA32_PG_PS
) != 0) {
323 *PageAttribute
= Page2M
;
324 return &L2PageTable
[Index2
];
328 L1PageTable
= (UINT64
*)(UINTN
)(L2PageTable
[Index2
] & ~AddressEncMask
& PAGING_4K_ADDRESS_MASK_64
);
329 if ((L1PageTable
[Index1
] == 0) && (Address
!= 0)) {
330 *PageAttribute
= PageNone
;
333 *PageAttribute
= Page4K
;
334 return &L1PageTable
[Index1
];
338 Return memory attributes of page entry.
340 @param[in] PageEntry The page entry.
342 @return Memory attributes of page entry.
345 GetAttributesFromPageEntry (
351 if ((*PageEntry
& IA32_PG_P
) == 0) {
352 Attributes
|= EFI_MEMORY_RP
;
354 if ((*PageEntry
& IA32_PG_RW
) == 0) {
355 Attributes
|= EFI_MEMORY_RO
;
357 if ((*PageEntry
& IA32_PG_NX
) != 0) {
358 Attributes
|= EFI_MEMORY_XP
;
364 Modify memory attributes of page entry.
366 @param[in] PagingContext The paging context.
367 @param[in] PageEntry The page entry.
368 @param[in] Attributes The bit mask of attributes to modify for the memory region.
369 @param[in] PageAction The page action.
370 @param[out] IsModified TRUE means page table modified. FALSE means page table not modified.
373 ConvertPageEntryAttribute (
374 IN PAGE_TABLE_LIB_PAGING_CONTEXT
*PagingContext
,
375 IN UINT64
*PageEntry
,
376 IN UINT64 Attributes
,
377 IN PAGE_ACTION PageAction
,
378 OUT BOOLEAN
*IsModified
381 UINT64 CurrentPageEntry
;
384 CurrentPageEntry
= *PageEntry
;
385 NewPageEntry
= CurrentPageEntry
;
386 if ((Attributes
& EFI_MEMORY_RP
) != 0) {
387 switch (PageAction
) {
388 case PageActionAssign
:
390 NewPageEntry
&= ~(UINT64
)IA32_PG_P
;
392 case PageActionClear
:
393 NewPageEntry
|= IA32_PG_P
;
397 switch (PageAction
) {
398 case PageActionAssign
:
399 NewPageEntry
|= IA32_PG_P
;
402 case PageActionClear
:
406 if ((Attributes
& EFI_MEMORY_RO
) != 0) {
407 switch (PageAction
) {
408 case PageActionAssign
:
410 NewPageEntry
&= ~(UINT64
)IA32_PG_RW
;
412 case PageActionClear
:
413 NewPageEntry
|= IA32_PG_RW
;
417 switch (PageAction
) {
418 case PageActionAssign
:
419 NewPageEntry
|= IA32_PG_RW
;
422 case PageActionClear
:
426 if ((PagingContext
->ContextData
.Ia32
.Attributes
& PAGE_TABLE_LIB_PAGING_CONTEXT_IA32_X64_ATTRIBUTES_XD_ACTIVATED
) != 0) {
427 if ((Attributes
& EFI_MEMORY_XP
) != 0) {
428 switch (PageAction
) {
429 case PageActionAssign
:
431 NewPageEntry
|= IA32_PG_NX
;
433 case PageActionClear
:
434 NewPageEntry
&= ~IA32_PG_NX
;
438 switch (PageAction
) {
439 case PageActionAssign
:
440 NewPageEntry
&= ~IA32_PG_NX
;
443 case PageActionClear
:
448 *PageEntry
= NewPageEntry
;
449 if (CurrentPageEntry
!= NewPageEntry
) {
451 DEBUG ((DEBUG_VERBOSE
, "ConvertPageEntryAttribute 0x%lx", CurrentPageEntry
));
452 DEBUG ((DEBUG_VERBOSE
, "->0x%lx\n", NewPageEntry
));
459 This function returns if there is need to split page entry.
461 @param[in] BaseAddress The base address to be checked.
462 @param[in] Length The length to be checked.
463 @param[in] PageEntry The page entry to be checked.
464 @param[in] PageAttribute The page attribute of the page entry.
466 @retval SplitAttributes on if there is need to split page entry.
470 IN PHYSICAL_ADDRESS BaseAddress
,
472 IN UINT64
*PageEntry
,
473 IN PAGE_ATTRIBUTE PageAttribute
476 UINT64 PageEntryLength
;
478 PageEntryLength
= PageAttributeToLength (PageAttribute
);
480 if (((BaseAddress
& (PageEntryLength
- 1)) == 0) && (Length
>= PageEntryLength
)) {
484 if (((BaseAddress
& PAGING_2M_MASK
) != 0) || (Length
< SIZE_2MB
)) {
492 This function splits one page entry to small page entries.
494 @param[in] PageEntry The page entry to be splitted.
495 @param[in] PageAttribute The page attribute of the page entry.
496 @param[in] SplitAttribute How to split the page entry.
497 @param[in] AllocatePagesFunc If page split is needed, this function is used to allocate more pages.
499 @retval RETURN_SUCCESS The page entry is splitted.
500 @retval RETURN_UNSUPPORTED The page entry does not support to be splitted.
501 @retval RETURN_OUT_OF_RESOURCES No resource to split page entry.
505 IN UINT64
*PageEntry
,
506 IN PAGE_ATTRIBUTE PageAttribute
,
507 IN PAGE_ATTRIBUTE SplitAttribute
,
508 IN PAGE_TABLE_LIB_ALLOCATE_PAGES AllocatePagesFunc
512 UINT64
*NewPageEntry
;
514 UINT64 AddressEncMask
;
516 ASSERT (PageAttribute
== Page2M
|| PageAttribute
== Page1G
);
518 ASSERT (AllocatePagesFunc
!= NULL
);
520 // Make sure AddressEncMask is contained to smallest supported address field.
522 AddressEncMask
= PcdGet64 (PcdPteMemoryEncryptionAddressOrMask
) & PAGING_1G_ADDRESS_MASK_64
;
524 if (PageAttribute
== Page2M
) {
528 ASSERT (SplitAttribute
== Page4K
);
529 if (SplitAttribute
== Page4K
) {
530 NewPageEntry
= AllocatePagesFunc (1);
531 DEBUG ((DEBUG_INFO
, "Split - 0x%x\n", NewPageEntry
));
532 if (NewPageEntry
== NULL
) {
533 return RETURN_OUT_OF_RESOURCES
;
535 BaseAddress
= *PageEntry
& ~AddressEncMask
& PAGING_2M_ADDRESS_MASK_64
;
536 for (Index
= 0; Index
< SIZE_4KB
/ sizeof(UINT64
); Index
++) {
537 NewPageEntry
[Index
] = (BaseAddress
+ SIZE_4KB
* Index
) | AddressEncMask
| ((*PageEntry
) & PAGE_PROGATE_BITS
);
539 (*PageEntry
) = (UINT64
)(UINTN
)NewPageEntry
| AddressEncMask
| ((*PageEntry
) & PAGE_ATTRIBUTE_BITS
);
540 return RETURN_SUCCESS
;
542 return RETURN_UNSUPPORTED
;
544 } else if (PageAttribute
== Page1G
) {
547 // No need support 1G->4K directly, we should use 1G->2M, then 2M->4K to get more compact page table.
549 ASSERT (SplitAttribute
== Page2M
|| SplitAttribute
== Page4K
);
550 if ((SplitAttribute
== Page2M
|| SplitAttribute
== Page4K
)) {
551 NewPageEntry
= AllocatePagesFunc (1);
552 DEBUG ((DEBUG_INFO
, "Split - 0x%x\n", NewPageEntry
));
553 if (NewPageEntry
== NULL
) {
554 return RETURN_OUT_OF_RESOURCES
;
556 BaseAddress
= *PageEntry
& ~AddressEncMask
& PAGING_1G_ADDRESS_MASK_64
;
557 for (Index
= 0; Index
< SIZE_4KB
/ sizeof(UINT64
); Index
++) {
558 NewPageEntry
[Index
] = (BaseAddress
+ SIZE_2MB
* Index
) | AddressEncMask
| IA32_PG_PS
| ((*PageEntry
) & PAGE_PROGATE_BITS
);
560 (*PageEntry
) = (UINT64
)(UINTN
)NewPageEntry
| AddressEncMask
| ((*PageEntry
) & PAGE_ATTRIBUTE_BITS
);
561 return RETURN_SUCCESS
;
563 return RETURN_UNSUPPORTED
;
566 return RETURN_UNSUPPORTED
;
571 Check the WP status in CR0 register. This bit is used to lock or unlock write
572 access to pages marked as read-only.
574 @retval TRUE Write protection is enabled.
575 @retval FALSE Write protection is disabled.
578 IsReadOnlyPageWriteProtected (
583 // To avoid unforseen consequences, don't touch paging settings in SMM mode
587 return ((AsmReadCr0 () & CR0_WP
) != 0);
593 Disable Write Protect on pages marked as read-only.
596 DisableReadOnlyPageWriteProtect (
601 // To avoid unforseen consequences, don't touch paging settings in SMM mode
605 AsmWriteCr0 (AsmReadCr0 () & ~CR0_WP
);
610 Enable Write Protect on pages marked as read-only.
613 EnableReadOnlyPageWriteProtect (
618 // To avoid unforseen consequences, don't touch paging settings in SMM mode
622 AsmWriteCr0 (AsmReadCr0 () | CR0_WP
);
627 This function modifies the page attributes for the memory region specified by BaseAddress and
628 Length from their current attributes to the attributes specified by Attributes.
630 Caller should make sure BaseAddress and Length is at page boundary.
632 @param[in] PagingContext The paging context. NULL means get page table from current CPU context.
633 @param[in] BaseAddress The physical address that is the start address of a memory region.
634 @param[in] Length The size in bytes of the memory region.
635 @param[in] Attributes The bit mask of attributes to modify for the memory region.
636 @param[in] PageAction The page action.
637 @param[in] AllocatePagesFunc If page split is needed, this function is used to allocate more pages.
638 NULL mean page split is unsupported.
639 @param[out] IsSplitted TRUE means page table splitted. FALSE means page table not splitted.
640 @param[out] IsModified TRUE means page table modified. FALSE means page table not modified.
642 @retval RETURN_SUCCESS The attributes were modified for the memory region.
643 @retval RETURN_ACCESS_DENIED The attributes for the memory resource range specified by
644 BaseAddress and Length cannot be modified.
645 @retval RETURN_INVALID_PARAMETER Length is zero.
646 Attributes specified an illegal combination of attributes that
647 cannot be set together.
648 @retval RETURN_OUT_OF_RESOURCES There are not enough system resources to modify the attributes of
649 the memory resource range.
650 @retval RETURN_UNSUPPORTED The processor does not support one or more bytes of the memory
651 resource range specified by BaseAddress and Length.
652 The bit mask of attributes is not support for the memory resource
653 range specified by BaseAddress and Length.
656 ConvertMemoryPageAttributes (
657 IN PAGE_TABLE_LIB_PAGING_CONTEXT
*PagingContext OPTIONAL
,
658 IN PHYSICAL_ADDRESS BaseAddress
,
660 IN UINT64 Attributes
,
661 IN PAGE_ACTION PageAction
,
662 IN PAGE_TABLE_LIB_ALLOCATE_PAGES AllocatePagesFunc OPTIONAL
,
663 OUT BOOLEAN
*IsSplitted
, OPTIONAL
664 OUT BOOLEAN
*IsModified OPTIONAL
667 PAGE_TABLE_LIB_PAGING_CONTEXT CurrentPagingContext
;
669 PAGE_ATTRIBUTE PageAttribute
;
670 UINTN PageEntryLength
;
671 PAGE_ATTRIBUTE SplitAttribute
;
672 RETURN_STATUS Status
;
673 BOOLEAN IsEntryModified
;
676 if ((BaseAddress
& (SIZE_4KB
- 1)) != 0) {
677 DEBUG ((DEBUG_ERROR
, "BaseAddress(0x%lx) is not aligned!\n", BaseAddress
));
678 return EFI_UNSUPPORTED
;
680 if ((Length
& (SIZE_4KB
- 1)) != 0) {
681 DEBUG ((DEBUG_ERROR
, "Length(0x%lx) is not aligned!\n", Length
));
682 return EFI_UNSUPPORTED
;
685 DEBUG ((DEBUG_ERROR
, "Length is 0!\n"));
686 return RETURN_INVALID_PARAMETER
;
689 if ((Attributes
& ~(EFI_MEMORY_RP
| EFI_MEMORY_RO
| EFI_MEMORY_XP
)) != 0) {
690 DEBUG ((DEBUG_ERROR
, "Attributes(0x%lx) has unsupported bit\n", Attributes
));
691 return EFI_UNSUPPORTED
;
694 if (PagingContext
== NULL
) {
695 GetCurrentPagingContext (&CurrentPagingContext
);
697 CopyMem (&CurrentPagingContext
, PagingContext
, sizeof(CurrentPagingContext
));
699 switch(CurrentPagingContext
.MachineType
) {
700 case IMAGE_FILE_MACHINE_I386
:
701 if (CurrentPagingContext
.ContextData
.Ia32
.PageTableBase
== 0) {
702 if (Attributes
== 0) {
705 DEBUG ((DEBUG_ERROR
, "PageTable is 0!\n"));
706 return EFI_UNSUPPORTED
;
709 if ((CurrentPagingContext
.ContextData
.Ia32
.Attributes
& PAGE_TABLE_LIB_PAGING_CONTEXT_IA32_X64_ATTRIBUTES_PAE
) == 0) {
710 DEBUG ((DEBUG_ERROR
, "Non-PAE Paging!\n"));
711 return EFI_UNSUPPORTED
;
713 if ((BaseAddress
+ Length
) > BASE_4GB
) {
714 DEBUG ((DEBUG_ERROR
, "Beyond 4GB memory in 32-bit mode!\n"));
715 return EFI_UNSUPPORTED
;
718 case IMAGE_FILE_MACHINE_X64
:
719 ASSERT (CurrentPagingContext
.ContextData
.X64
.PageTableBase
!= 0);
723 return EFI_UNSUPPORTED
;
727 // DEBUG ((DEBUG_ERROR, "ConvertMemoryPageAttributes(%x) - %016lx, %016lx, %02lx\n", IsSet, BaseAddress, Length, Attributes));
729 if (IsSplitted
!= NULL
) {
732 if (IsModified
!= NULL
) {
735 if (AllocatePagesFunc
== NULL
) {
736 AllocatePagesFunc
= AllocatePageTableMemory
;
740 // Make sure that the page table is changeable.
742 IsWpEnabled
= IsReadOnlyPageWriteProtected ();
744 DisableReadOnlyPageWriteProtect ();
748 // Below logic is to check 2M/4K page to make sure we donot waist memory.
750 Status
= EFI_SUCCESS
;
751 while (Length
!= 0) {
752 PageEntry
= GetPageTableEntry (&CurrentPagingContext
, BaseAddress
, &PageAttribute
);
753 if (PageEntry
== NULL
) {
754 Status
= RETURN_UNSUPPORTED
;
757 PageEntryLength
= PageAttributeToLength (PageAttribute
);
758 SplitAttribute
= NeedSplitPage (BaseAddress
, Length
, PageEntry
, PageAttribute
);
759 if (SplitAttribute
== PageNone
) {
760 ConvertPageEntryAttribute (&CurrentPagingContext
, PageEntry
, Attributes
, PageAction
, &IsEntryModified
);
761 if (IsEntryModified
) {
762 if (IsModified
!= NULL
) {
767 // Convert success, move to next
769 BaseAddress
+= PageEntryLength
;
770 Length
-= PageEntryLength
;
772 if (AllocatePagesFunc
== NULL
) {
773 Status
= RETURN_UNSUPPORTED
;
776 Status
= SplitPage (PageEntry
, PageAttribute
, SplitAttribute
, AllocatePagesFunc
);
777 if (RETURN_ERROR (Status
)) {
778 Status
= RETURN_UNSUPPORTED
;
781 if (IsSplitted
!= NULL
) {
784 if (IsModified
!= NULL
) {
788 // Just split current page
789 // Convert success in next around
796 // Restore page table write protection, if any.
799 EnableReadOnlyPageWriteProtect ();
805 This function assigns the page attributes for the memory region specified by BaseAddress and
806 Length from their current attributes to the attributes specified by Attributes.
808 Caller should make sure BaseAddress and Length is at page boundary.
810 Caller need guarentee the TPL <= TPL_NOTIFY, if there is split page request.
812 @param[in] PagingContext The paging context. NULL means get page table from current CPU context.
813 @param[in] BaseAddress The physical address that is the start address of a memory region.
814 @param[in] Length The size in bytes of the memory region.
815 @param[in] Attributes The bit mask of attributes to set for the memory region.
816 @param[in] AllocatePagesFunc If page split is needed, this function is used to allocate more pages.
817 NULL mean page split is unsupported.
819 @retval RETURN_SUCCESS The attributes were cleared for the memory region.
820 @retval RETURN_ACCESS_DENIED The attributes for the memory resource range specified by
821 BaseAddress and Length cannot be modified.
822 @retval RETURN_INVALID_PARAMETER Length is zero.
823 Attributes specified an illegal combination of attributes that
824 cannot be set together.
825 @retval RETURN_OUT_OF_RESOURCES There are not enough system resources to modify the attributes of
826 the memory resource range.
827 @retval RETURN_UNSUPPORTED The processor does not support one or more bytes of the memory
828 resource range specified by BaseAddress and Length.
829 The bit mask of attributes is not support for the memory resource
830 range specified by BaseAddress and Length.
834 AssignMemoryPageAttributes (
835 IN PAGE_TABLE_LIB_PAGING_CONTEXT
*PagingContext OPTIONAL
,
836 IN PHYSICAL_ADDRESS BaseAddress
,
838 IN UINT64 Attributes
,
839 IN PAGE_TABLE_LIB_ALLOCATE_PAGES AllocatePagesFunc OPTIONAL
842 RETURN_STATUS Status
;
846 // DEBUG((DEBUG_INFO, "AssignMemoryPageAttributes: 0x%lx - 0x%lx (0x%lx)\n", BaseAddress, Length, Attributes));
847 Status
= ConvertMemoryPageAttributes (PagingContext
, BaseAddress
, Length
, Attributes
, PageActionAssign
, AllocatePagesFunc
, &IsSplitted
, &IsModified
);
848 if (!EFI_ERROR(Status
)) {
849 if ((PagingContext
== NULL
) && IsModified
) {
851 // Flush TLB as last step.
853 // Note: Since APs will always init CR3 register in HLT loop mode or do
854 // TLB flush in MWAIT loop mode, there's no need to flush TLB for them
865 Check if Execute Disable feature is enabled or not.
868 IsExecuteDisableEnabled (
872 MSR_CORE_IA32_EFER_REGISTER MsrEfer
;
874 MsrEfer
.Uint64
= AsmReadMsr64 (MSR_IA32_EFER
);
875 return (MsrEfer
.Bits
.NXE
== 1);
879 Update GCD memory space attributes according to current page table setup.
882 RefreshGcdMemoryAttributesFromPaging (
887 UINTN NumberOfDescriptors
;
888 EFI_GCD_MEMORY_SPACE_DESCRIPTOR
*MemorySpaceMap
;
889 PAGE_TABLE_LIB_PAGING_CONTEXT PagingContext
;
890 PAGE_ATTRIBUTE PageAttribute
;
893 UINT64 MemorySpaceLength
;
896 UINT64 PageStartAddress
;
899 UINT64 NewAttributes
;
903 // Assuming that memory space map returned is sorted already; otherwise sort
904 // them in the order of lowest address to highest address.
906 Status
= gDS
->GetMemorySpaceMap (&NumberOfDescriptors
, &MemorySpaceMap
);
907 ASSERT_EFI_ERROR (Status
);
909 GetCurrentPagingContext (&PagingContext
);
916 if (IsExecuteDisableEnabled ()) {
917 Capabilities
= EFI_MEMORY_RO
| EFI_MEMORY_RP
| EFI_MEMORY_XP
;
919 Capabilities
= EFI_MEMORY_RO
| EFI_MEMORY_RP
;
922 for (Index
= 0; Index
< NumberOfDescriptors
; Index
++) {
923 if (MemorySpaceMap
[Index
].GcdMemoryType
== EfiGcdMemoryTypeNonExistent
) {
928 // Sync the actual paging related capabilities back to GCD service first.
929 // As a side effect (good one), this can also help to avoid unnecessary
930 // memory map entries due to the different capabilities of the same type
931 // memory, such as multiple RT_CODE and RT_DATA entries in memory map,
932 // which could cause boot failure of some old Linux distro (before v4.3).
934 Status
= gDS
->SetMemorySpaceCapabilities (
935 MemorySpaceMap
[Index
].BaseAddress
,
936 MemorySpaceMap
[Index
].Length
,
937 MemorySpaceMap
[Index
].Capabilities
| Capabilities
939 if (EFI_ERROR (Status
)) {
941 // If we cannot udpate the capabilities, we cannot update its
942 // attributes either. So just simply skip current block of memory.
946 "Failed to update capability: [%lu] %016lx - %016lx (%016lx -> %016lx)\r\n",
947 (UINT64
)Index
, MemorySpaceMap
[Index
].BaseAddress
,
948 MemorySpaceMap
[Index
].BaseAddress
+ MemorySpaceMap
[Index
].Length
- 1,
949 MemorySpaceMap
[Index
].Capabilities
,
950 MemorySpaceMap
[Index
].Capabilities
| Capabilities
955 if (MemorySpaceMap
[Index
].BaseAddress
>= (BaseAddress
+ PageLength
)) {
957 // Current memory space starts at a new page. Resetting PageLength will
958 // trigger a retrieval of page attributes at new address.
963 // In case current memory space is not adjacent to last one
965 PageLength
-= (MemorySpaceMap
[Index
].BaseAddress
- BaseAddress
);
969 // Sync actual page attributes to GCD
971 BaseAddress
= MemorySpaceMap
[Index
].BaseAddress
;
972 MemorySpaceLength
= MemorySpaceMap
[Index
].Length
;
973 while (MemorySpaceLength
> 0) {
974 if (PageLength
== 0) {
975 PageEntry
= GetPageTableEntry (&PagingContext
, BaseAddress
, &PageAttribute
);
976 if (PageEntry
== NULL
) {
981 // Note current memory space might start in the middle of a page
983 PageStartAddress
= (*PageEntry
) & (UINT64
)PageAttributeToMask(PageAttribute
);
984 PageLength
= PageAttributeToLength (PageAttribute
) - (BaseAddress
- PageStartAddress
);
985 Attributes
= GetAttributesFromPageEntry (PageEntry
);
988 Length
= MIN (PageLength
, MemorySpaceLength
);
989 if (Attributes
!= (MemorySpaceMap
[Index
].Attributes
&
990 EFI_MEMORY_PAGETYPE_MASK
)) {
991 NewAttributes
= (MemorySpaceMap
[Index
].Attributes
&
992 ~EFI_MEMORY_PAGETYPE_MASK
) | Attributes
;
993 Status
= gDS
->SetMemorySpaceAttributes (
998 ASSERT_EFI_ERROR (Status
);
1001 "Updated memory space attribute: [%lu] %016lx - %016lx (%016lx -> %016lx)\r\n",
1002 (UINT64
)Index
, BaseAddress
, BaseAddress
+ Length
- 1,
1003 MemorySpaceMap
[Index
].Attributes
,
1008 PageLength
-= Length
;
1009 MemorySpaceLength
-= Length
;
1010 BaseAddress
+= Length
;
1014 FreePool (MemorySpaceMap
);
1018 Initialize a buffer pool for page table use only.
1020 To reduce the potential split operation on page table, the pages reserved for
1021 page table should be allocated in the times of PAGE_TABLE_POOL_UNIT_PAGES and
1022 at the boundary of PAGE_TABLE_POOL_ALIGNMENT. So the page pool is always
1023 initialized with number of pages greater than or equal to the given PoolPages.
1025 Once the pages in the pool are used up, this method should be called again to
1026 reserve at least another PAGE_TABLE_POOL_UNIT_PAGES. Usually this won't happen
1029 @param[in] PoolPages The least page number of the pool to be created.
1031 @retval TRUE The pool is initialized successfully.
1032 @retval FALSE The memory is out of resource.
1035 InitializePageTablePool (
1043 // Always reserve at least PAGE_TABLE_POOL_UNIT_PAGES, including one page for
1046 PoolPages
+= 1; // Add one page for header.
1047 PoolPages
= ((PoolPages
- 1) / PAGE_TABLE_POOL_UNIT_PAGES
+ 1) *
1048 PAGE_TABLE_POOL_UNIT_PAGES
;
1049 Buffer
= AllocateAlignedPages (PoolPages
, PAGE_TABLE_POOL_ALIGNMENT
);
1050 if (Buffer
== NULL
) {
1051 DEBUG ((DEBUG_ERROR
, "ERROR: Out of aligned pages\r\n"));
1056 // Link all pools into a list for easier track later.
1058 if (mPageTablePool
== NULL
) {
1059 mPageTablePool
= Buffer
;
1060 mPageTablePool
->NextPool
= mPageTablePool
;
1062 ((PAGE_TABLE_POOL
*)Buffer
)->NextPool
= mPageTablePool
->NextPool
;
1063 mPageTablePool
->NextPool
= Buffer
;
1064 mPageTablePool
= Buffer
;
1068 // Reserve one page for pool header.
1070 mPageTablePool
->FreePages
= PoolPages
- 1;
1071 mPageTablePool
->Offset
= EFI_PAGES_TO_SIZE (1);
1074 // Mark the whole pool pages as read-only.
1076 ConvertMemoryPageAttributes (
1078 (PHYSICAL_ADDRESS
)(UINTN
)Buffer
,
1079 EFI_PAGES_TO_SIZE (PoolPages
),
1082 AllocatePageTableMemory
,
1086 ASSERT (IsModified
== TRUE
);
1092 This API provides a way to allocate memory for page table.
1094 This API can be called more than once to allocate memory for page tables.
1096 Allocates the number of 4KB pages and returns a pointer to the allocated
1097 buffer. The buffer returned is aligned on a 4KB boundary.
1099 If Pages is 0, then NULL is returned.
1100 If there is not enough memory remaining to satisfy the request, then NULL is
1103 @param Pages The number of 4 KB pages to allocate.
1105 @return A pointer to the allocated buffer or NULL if allocation fails.
1110 AllocatePageTableMemory (
1121 // Renew the pool if necessary.
1123 if (mPageTablePool
== NULL
||
1124 Pages
> mPageTablePool
->FreePages
) {
1125 if (!InitializePageTablePool (Pages
)) {
1130 Buffer
= (UINT8
*)mPageTablePool
+ mPageTablePool
->Offset
;
1132 mPageTablePool
->Offset
+= EFI_PAGES_TO_SIZE (Pages
);
1133 mPageTablePool
->FreePages
-= Pages
;
1139 Initialize the Page Table lib.
1142 InitializePageTableLib (
1146 PAGE_TABLE_LIB_PAGING_CONTEXT CurrentPagingContext
;
1148 GetCurrentPagingContext (&CurrentPagingContext
);
1151 // Reserve memory of page tables for future uses, if paging is enabled.
1153 if (CurrentPagingContext
.ContextData
.X64
.PageTableBase
!= 0 &&
1154 (CurrentPagingContext
.ContextData
.Ia32
.Attributes
&
1155 PAGE_TABLE_LIB_PAGING_CONTEXT_IA32_X64_ATTRIBUTES_PAE
) != 0) {
1156 DisableReadOnlyPageWriteProtect ();
1157 InitializePageTablePool (1);
1158 EnableReadOnlyPageWriteProtect ();
1161 DEBUG ((DEBUG_INFO
, "CurrentPagingContext:\n", CurrentPagingContext
.MachineType
));
1162 DEBUG ((DEBUG_INFO
, " MachineType - 0x%x\n", CurrentPagingContext
.MachineType
));
1163 DEBUG ((DEBUG_INFO
, " PageTableBase - 0x%x\n", CurrentPagingContext
.ContextData
.X64
.PageTableBase
));
1164 DEBUG ((DEBUG_INFO
, " Attributes - 0x%x\n", CurrentPagingContext
.ContextData
.X64
.Attributes
));