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 "CpuPageTable.h"
31 #define IA32_PG_P BIT0
32 #define IA32_PG_RW BIT1
33 #define IA32_PG_U BIT2
34 #define IA32_PG_WT BIT3
35 #define IA32_PG_CD BIT4
36 #define IA32_PG_A BIT5
37 #define IA32_PG_D BIT6
38 #define IA32_PG_PS BIT7
39 #define IA32_PG_PAT_2M BIT12
40 #define IA32_PG_PAT_4K IA32_PG_PS
41 #define IA32_PG_PMNT BIT62
42 #define IA32_PG_NX BIT63
44 #define PAGE_ATTRIBUTE_BITS (IA32_PG_D | IA32_PG_A | IA32_PG_U | IA32_PG_RW | IA32_PG_P)
46 // Bits 1, 2, 5, 6 are reserved in the IA32 PAE PDPTE
47 // X64 PAE PDPTE does not have such restriction
49 #define IA32_PAE_PDPTE_ATTRIBUTE_BITS (IA32_PG_P)
51 #define PAGE_PROGATE_BITS (IA32_PG_NX | PAGE_ATTRIBUTE_BITS)
53 #define PAGING_4K_MASK 0xFFF
54 #define PAGING_2M_MASK 0x1FFFFF
55 #define PAGING_1G_MASK 0x3FFFFFFF
57 #define PAGING_PAE_INDEX_MASK 0x1FF
59 #define PAGING_4K_ADDRESS_MASK_64 0x000FFFFFFFFFF000ull
60 #define PAGING_2M_ADDRESS_MASK_64 0x000FFFFFFFE00000ull
61 #define PAGING_1G_ADDRESS_MASK_64 0x000FFFFFC0000000ull
71 PAGE_ATTRIBUTE Attribute
;
74 } PAGE_ATTRIBUTE_TABLE
;
82 PAGE_ATTRIBUTE_TABLE mPageAttributeTable
[] = {
83 {Page4K
, SIZE_4KB
, PAGING_4K_ADDRESS_MASK_64
},
84 {Page2M
, SIZE_2MB
, PAGING_2M_ADDRESS_MASK_64
},
85 {Page1G
, SIZE_1GB
, PAGING_1G_ADDRESS_MASK_64
},
89 Enable write protection function for AP.
91 @param[in,out] Buffer The pointer to private data buffer.
95 SyncCpuEnableWriteProtection (
99 AsmWriteCr0 (AsmReadCr0 () | BIT16
);
103 CpuFlushTlb function for AP.
105 @param[in,out] Buffer The pointer to private data buffer.
117 Sync memory page attributes for AP.
119 @param[in] Procedure A pointer to the function to be run on enabled APs of
123 SyncMemoryPageAttributesAp (
124 IN EFI_AP_PROCEDURE Procedure
128 EFI_MP_SERVICES_PROTOCOL
*MpService
;
130 Status
= gBS
->LocateProtocol (
131 &gEfiMpServiceProtocolGuid
,
136 // Synchronize the update with all APs
138 if (!EFI_ERROR (Status
)) {
139 Status
= MpService
->StartupAllAPs (
141 Procedure
, // Procedure
142 FALSE
, // SingleThread
144 0, // TimeoutInMicrosecsond
145 NULL
, // ProcedureArgument
146 NULL
// FailedCpuList
148 ASSERT (Status
== EFI_SUCCESS
|| Status
== EFI_NOT_STARTED
|| Status
== EFI_NOT_READY
);
153 Return current paging context.
155 @param[in,out] PagingContext The paging context.
158 GetCurrentPagingContext (
159 IN OUT PAGE_TABLE_LIB_PAGING_CONTEXT
*PagingContext
165 ZeroMem(PagingContext
, sizeof(*PagingContext
));
166 if (sizeof(UINTN
) == sizeof(UINT64
)) {
167 PagingContext
->MachineType
= IMAGE_FILE_MACHINE_X64
;
169 PagingContext
->MachineType
= IMAGE_FILE_MACHINE_I386
;
171 if ((AsmReadCr0 () & BIT31
) != 0) {
172 PagingContext
->ContextData
.X64
.PageTableBase
= (AsmReadCr3 () & PAGING_4K_ADDRESS_MASK_64
);
173 if ((AsmReadCr0 () & BIT16
) == 0) {
174 AsmWriteCr0 (AsmReadCr0 () | BIT16
);
175 SyncMemoryPageAttributesAp (SyncCpuEnableWriteProtection
);
178 PagingContext
->ContextData
.X64
.PageTableBase
= 0;
181 if ((AsmReadCr4 () & BIT4
) != 0) {
182 PagingContext
->ContextData
.Ia32
.Attributes
|= PAGE_TABLE_LIB_PAGING_CONTEXT_IA32_X64_ATTRIBUTES_PSE
;
184 if ((AsmReadCr4 () & BIT5
) != 0) {
185 PagingContext
->ContextData
.Ia32
.Attributes
|= PAGE_TABLE_LIB_PAGING_CONTEXT_IA32_X64_ATTRIBUTES_PAE
;
187 if ((AsmReadCr0 () & BIT16
) != 0) {
188 PagingContext
->ContextData
.Ia32
.Attributes
|= PAGE_TABLE_LIB_PAGING_CONTEXT_IA32_X64_ATTRIBUTES_WP_ENABLE
;
191 AsmCpuid (0x80000000, &RegEax
, NULL
, NULL
, NULL
);
192 if (RegEax
> 0x80000000) {
193 AsmCpuid (0x80000001, NULL
, NULL
, NULL
, &RegEdx
);
194 if ((RegEdx
& BIT20
) != 0) {
196 if ((AsmReadMsr64 (0x000001A0) & BIT34
) == 0) {
198 if ((AsmReadMsr64 (0xC0000080) & BIT11
) != 0) {
200 PagingContext
->ContextData
.Ia32
.Attributes
|= PAGE_TABLE_LIB_PAGING_CONTEXT_IA32_X64_ATTRIBUTES_XD_ACTIVATED
;
204 if ((RegEdx
& BIT26
) != 0) {
205 PagingContext
->ContextData
.Ia32
.Attributes
|= PAGE_TABLE_LIB_PAGING_CONTEXT_IA32_X64_ATTRIBUTES_PAGE_1G_SUPPORT
;
211 Return length according to page attributes.
213 @param[in] PageAttributes The page attribute of the page entry.
215 @return The length of page entry.
218 PageAttributeToLength (
219 IN PAGE_ATTRIBUTE PageAttribute
223 for (Index
= 0; Index
< sizeof(mPageAttributeTable
)/sizeof(mPageAttributeTable
[0]); Index
++) {
224 if (PageAttribute
== mPageAttributeTable
[Index
].Attribute
) {
225 return (UINTN
)mPageAttributeTable
[Index
].Length
;
232 Return address mask according to page attributes.
234 @param[in] PageAttributes The page attribute of the page entry.
236 @return The address mask of page entry.
239 PageAttributeToMask (
240 IN PAGE_ATTRIBUTE PageAttribute
244 for (Index
= 0; Index
< sizeof(mPageAttributeTable
)/sizeof(mPageAttributeTable
[0]); Index
++) {
245 if (PageAttribute
== mPageAttributeTable
[Index
].Attribute
) {
246 return (UINTN
)mPageAttributeTable
[Index
].AddressMask
;
253 Return page table entry to match the address.
255 @param[in] PagingContext The paging context.
256 @param[in] Address The address to be checked.
257 @param[out] PageAttributes The page attribute of the page entry.
259 @return The page entry.
263 IN PAGE_TABLE_LIB_PAGING_CONTEXT
*PagingContext
,
264 IN PHYSICAL_ADDRESS Address
,
265 OUT PAGE_ATTRIBUTE
*PageAttribute
276 UINT64 AddressEncMask
;
278 ASSERT (PagingContext
!= NULL
);
280 Index4
= ((UINTN
)RShiftU64 (Address
, 39)) & PAGING_PAE_INDEX_MASK
;
281 Index3
= ((UINTN
)Address
>> 30) & PAGING_PAE_INDEX_MASK
;
282 Index2
= ((UINTN
)Address
>> 21) & PAGING_PAE_INDEX_MASK
;
283 Index1
= ((UINTN
)Address
>> 12) & PAGING_PAE_INDEX_MASK
;
285 // Make sure AddressEncMask is contained to smallest supported address field.
287 AddressEncMask
= PcdGet64 (PcdPteMemoryEncryptionAddressOrMask
) & PAGING_1G_ADDRESS_MASK_64
;
289 if (PagingContext
->MachineType
== IMAGE_FILE_MACHINE_X64
) {
290 L4PageTable
= (UINT64
*)(UINTN
)PagingContext
->ContextData
.X64
.PageTableBase
;
291 if (L4PageTable
[Index4
] == 0) {
292 *PageAttribute
= PageNone
;
296 L3PageTable
= (UINT64
*)(UINTN
)(L4PageTable
[Index4
] & ~AddressEncMask
& PAGING_4K_ADDRESS_MASK_64
);
298 ASSERT((PagingContext
->ContextData
.Ia32
.Attributes
& PAGE_TABLE_LIB_PAGING_CONTEXT_IA32_X64_ATTRIBUTES_PAE
) != 0);
299 L3PageTable
= (UINT64
*)(UINTN
)PagingContext
->ContextData
.Ia32
.PageTableBase
;
301 if (L3PageTable
[Index3
] == 0) {
302 *PageAttribute
= PageNone
;
305 if ((L3PageTable
[Index3
] & IA32_PG_PS
) != 0) {
307 *PageAttribute
= Page1G
;
308 return &L3PageTable
[Index3
];
311 L2PageTable
= (UINT64
*)(UINTN
)(L3PageTable
[Index3
] & ~AddressEncMask
& PAGING_4K_ADDRESS_MASK_64
);
312 if (L2PageTable
[Index2
] == 0) {
313 *PageAttribute
= PageNone
;
316 if ((L2PageTable
[Index2
] & IA32_PG_PS
) != 0) {
318 *PageAttribute
= Page2M
;
319 return &L2PageTable
[Index2
];
323 L1PageTable
= (UINT64
*)(UINTN
)(L2PageTable
[Index2
] & ~AddressEncMask
& PAGING_4K_ADDRESS_MASK_64
);
324 if ((L1PageTable
[Index1
] == 0) && (Address
!= 0)) {
325 *PageAttribute
= PageNone
;
328 *PageAttribute
= Page4K
;
329 return &L1PageTable
[Index1
];
333 Return memory attributes of page entry.
335 @param[in] PageEntry The page entry.
337 @return Memory attributes of page entry.
340 GetAttributesFromPageEntry (
346 if ((*PageEntry
& IA32_PG_P
) == 0) {
347 Attributes
|= EFI_MEMORY_RP
;
349 if ((*PageEntry
& IA32_PG_RW
) == 0) {
350 Attributes
|= EFI_MEMORY_RO
;
352 if ((*PageEntry
& IA32_PG_NX
) != 0) {
353 Attributes
|= EFI_MEMORY_XP
;
359 Modify memory attributes of page entry.
361 @param[in] PagingContext The paging context.
362 @param[in] PageEntry The page entry.
363 @param[in] Attributes The bit mask of attributes to modify for the memory region.
364 @param[in] PageAction The page action.
365 @param[out] IsModified TRUE means page table modified. FALSE means page table not modified.
368 ConvertPageEntryAttribute (
369 IN PAGE_TABLE_LIB_PAGING_CONTEXT
*PagingContext
,
370 IN UINT64
*PageEntry
,
371 IN UINT64 Attributes
,
372 IN PAGE_ACTION PageAction
,
373 OUT BOOLEAN
*IsModified
376 UINT64 CurrentPageEntry
;
379 CurrentPageEntry
= *PageEntry
;
380 NewPageEntry
= CurrentPageEntry
;
381 if ((Attributes
& EFI_MEMORY_RP
) != 0) {
382 switch (PageAction
) {
383 case PageActionAssign
:
385 NewPageEntry
&= ~(UINT64
)IA32_PG_P
;
387 case PageActionClear
:
388 NewPageEntry
|= IA32_PG_P
;
392 switch (PageAction
) {
393 case PageActionAssign
:
394 NewPageEntry
|= IA32_PG_P
;
397 case PageActionClear
:
401 if ((Attributes
& EFI_MEMORY_RO
) != 0) {
402 switch (PageAction
) {
403 case PageActionAssign
:
405 NewPageEntry
&= ~(UINT64
)IA32_PG_RW
;
407 case PageActionClear
:
408 NewPageEntry
|= IA32_PG_RW
;
412 switch (PageAction
) {
413 case PageActionAssign
:
414 NewPageEntry
|= IA32_PG_RW
;
417 case PageActionClear
:
421 if ((PagingContext
->ContextData
.Ia32
.Attributes
& PAGE_TABLE_LIB_PAGING_CONTEXT_IA32_X64_ATTRIBUTES_XD_ACTIVATED
) != 0) {
422 if ((Attributes
& EFI_MEMORY_XP
) != 0) {
423 switch (PageAction
) {
424 case PageActionAssign
:
426 NewPageEntry
|= IA32_PG_NX
;
428 case PageActionClear
:
429 NewPageEntry
&= ~IA32_PG_NX
;
433 switch (PageAction
) {
434 case PageActionAssign
:
435 NewPageEntry
&= ~IA32_PG_NX
;
438 case PageActionClear
:
443 *PageEntry
= NewPageEntry
;
444 if (CurrentPageEntry
!= NewPageEntry
) {
446 DEBUG ((DEBUG_INFO
, "ConvertPageEntryAttribute 0x%lx", CurrentPageEntry
));
447 DEBUG ((DEBUG_INFO
, "->0x%lx\n", NewPageEntry
));
454 This function returns if there is need to split page entry.
456 @param[in] BaseAddress The base address to be checked.
457 @param[in] Length The length to be checked.
458 @param[in] PageEntry The page entry to be checked.
459 @param[in] PageAttribute The page attribute of the page entry.
461 @retval SplitAttributes on if there is need to split page entry.
465 IN PHYSICAL_ADDRESS BaseAddress
,
467 IN UINT64
*PageEntry
,
468 IN PAGE_ATTRIBUTE PageAttribute
471 UINT64 PageEntryLength
;
473 PageEntryLength
= PageAttributeToLength (PageAttribute
);
475 if (((BaseAddress
& (PageEntryLength
- 1)) == 0) && (Length
>= PageEntryLength
)) {
479 if (((BaseAddress
& PAGING_2M_MASK
) != 0) || (Length
< SIZE_2MB
)) {
487 This function splits one page entry to small page entries.
489 @param[in] PageEntry The page entry to be splitted.
490 @param[in] PageAttribute The page attribute of the page entry.
491 @param[in] SplitAttribute How to split the page entry.
492 @param[in] AllocatePagesFunc If page split is needed, this function is used to allocate more pages.
494 @retval RETURN_SUCCESS The page entry is splitted.
495 @retval RETURN_UNSUPPORTED The page entry does not support to be splitted.
496 @retval RETURN_OUT_OF_RESOURCES No resource to split page entry.
500 IN UINT64
*PageEntry
,
501 IN PAGE_ATTRIBUTE PageAttribute
,
502 IN PAGE_ATTRIBUTE SplitAttribute
,
503 IN PAGE_TABLE_LIB_ALLOCATE_PAGES AllocatePagesFunc
507 UINT64
*NewPageEntry
;
509 UINT64 AddressEncMask
;
511 ASSERT (PageAttribute
== Page2M
|| PageAttribute
== Page1G
);
513 ASSERT (AllocatePagesFunc
!= NULL
);
515 // Make sure AddressEncMask is contained to smallest supported address field.
517 AddressEncMask
= PcdGet64 (PcdPteMemoryEncryptionAddressOrMask
) & PAGING_1G_ADDRESS_MASK_64
;
519 if (PageAttribute
== Page2M
) {
523 ASSERT (SplitAttribute
== Page4K
);
524 if (SplitAttribute
== Page4K
) {
525 NewPageEntry
= AllocatePagesFunc (1);
526 DEBUG ((DEBUG_INFO
, "Split - 0x%x\n", NewPageEntry
));
527 if (NewPageEntry
== NULL
) {
528 return RETURN_OUT_OF_RESOURCES
;
530 BaseAddress
= *PageEntry
& ~AddressEncMask
& PAGING_2M_ADDRESS_MASK_64
;
531 for (Index
= 0; Index
< SIZE_4KB
/ sizeof(UINT64
); Index
++) {
532 NewPageEntry
[Index
] = (BaseAddress
+ SIZE_4KB
* Index
) | AddressEncMask
| ((*PageEntry
) & PAGE_PROGATE_BITS
);
534 (*PageEntry
) = (UINT64
)(UINTN
)NewPageEntry
| AddressEncMask
| ((*PageEntry
) & PAGE_PROGATE_BITS
);
535 return RETURN_SUCCESS
;
537 return RETURN_UNSUPPORTED
;
539 } else if (PageAttribute
== Page1G
) {
542 // No need support 1G->4K directly, we should use 1G->2M, then 2M->4K to get more compact page table.
544 ASSERT (SplitAttribute
== Page2M
|| SplitAttribute
== Page4K
);
545 if ((SplitAttribute
== Page2M
|| SplitAttribute
== Page4K
)) {
546 NewPageEntry
= AllocatePagesFunc (1);
547 DEBUG ((DEBUG_INFO
, "Split - 0x%x\n", NewPageEntry
));
548 if (NewPageEntry
== NULL
) {
549 return RETURN_OUT_OF_RESOURCES
;
551 BaseAddress
= *PageEntry
& ~AddressEncMask
& PAGING_1G_ADDRESS_MASK_64
;
552 for (Index
= 0; Index
< SIZE_4KB
/ sizeof(UINT64
); Index
++) {
553 NewPageEntry
[Index
] = (BaseAddress
+ SIZE_2MB
* Index
) | AddressEncMask
| IA32_PG_PS
| ((*PageEntry
) & PAGE_PROGATE_BITS
);
555 (*PageEntry
) = (UINT64
)(UINTN
)NewPageEntry
| AddressEncMask
| ((*PageEntry
) & PAGE_PROGATE_BITS
);
556 return RETURN_SUCCESS
;
558 return RETURN_UNSUPPORTED
;
561 return RETURN_UNSUPPORTED
;
566 This function modifies the page attributes for the memory region specified by BaseAddress and
567 Length from their current attributes to the attributes specified by Attributes.
569 Caller should make sure BaseAddress and Length is at page boundary.
571 @param[in] PagingContext The paging context. NULL means get page table from current CPU context.
572 @param[in] BaseAddress The physical address that is the start address of a memory region.
573 @param[in] Length The size in bytes of the memory region.
574 @param[in] Attributes The bit mask of attributes to modify for the memory region.
575 @param[in] PageAction The page action.
576 @param[in] AllocatePagesFunc If page split is needed, this function is used to allocate more pages.
577 NULL mean page split is unsupported.
578 @param[out] IsSplitted TRUE means page table splitted. FALSE means page table not splitted.
579 @param[out] IsModified TRUE means page table modified. FALSE means page table not modified.
581 @retval RETURN_SUCCESS The attributes were modified for the memory region.
582 @retval RETURN_ACCESS_DENIED The attributes for the memory resource range specified by
583 BaseAddress and Length cannot be modified.
584 @retval RETURN_INVALID_PARAMETER Length is zero.
585 Attributes specified an illegal combination of attributes that
586 cannot be set together.
587 @retval RETURN_OUT_OF_RESOURCES There are not enough system resources to modify the attributes of
588 the memory resource range.
589 @retval RETURN_UNSUPPORTED The processor does not support one or more bytes of the memory
590 resource range specified by BaseAddress and Length.
591 The bit mask of attributes is not support for the memory resource
592 range specified by BaseAddress and Length.
595 ConvertMemoryPageAttributes (
596 IN PAGE_TABLE_LIB_PAGING_CONTEXT
*PagingContext OPTIONAL
,
597 IN PHYSICAL_ADDRESS BaseAddress
,
599 IN UINT64 Attributes
,
600 IN PAGE_ACTION PageAction
,
601 IN PAGE_TABLE_LIB_ALLOCATE_PAGES AllocatePagesFunc OPTIONAL
,
602 OUT BOOLEAN
*IsSplitted
, OPTIONAL
603 OUT BOOLEAN
*IsModified OPTIONAL
606 PAGE_TABLE_LIB_PAGING_CONTEXT CurrentPagingContext
;
608 PAGE_ATTRIBUTE PageAttribute
;
609 UINTN PageEntryLength
;
610 PAGE_ATTRIBUTE SplitAttribute
;
611 RETURN_STATUS Status
;
612 BOOLEAN IsEntryModified
;
614 if ((BaseAddress
& (SIZE_4KB
- 1)) != 0) {
615 DEBUG ((DEBUG_ERROR
, "BaseAddress(0x%lx) is not aligned!\n", BaseAddress
));
616 return EFI_UNSUPPORTED
;
618 if ((Length
& (SIZE_4KB
- 1)) != 0) {
619 DEBUG ((DEBUG_ERROR
, "Length(0x%lx) is not aligned!\n", Length
));
620 return EFI_UNSUPPORTED
;
623 DEBUG ((DEBUG_ERROR
, "Length is 0!\n"));
624 return RETURN_INVALID_PARAMETER
;
627 if ((Attributes
& ~(EFI_MEMORY_RP
| EFI_MEMORY_RO
| EFI_MEMORY_XP
)) != 0) {
628 DEBUG ((DEBUG_ERROR
, "Attributes(0x%lx) has unsupported bit\n", Attributes
));
629 return EFI_UNSUPPORTED
;
632 if (PagingContext
== NULL
) {
633 GetCurrentPagingContext (&CurrentPagingContext
);
635 CopyMem (&CurrentPagingContext
, PagingContext
, sizeof(CurrentPagingContext
));
637 switch(CurrentPagingContext
.MachineType
) {
638 case IMAGE_FILE_MACHINE_I386
:
639 if (CurrentPagingContext
.ContextData
.Ia32
.PageTableBase
== 0) {
640 DEBUG ((DEBUG_ERROR
, "PageTable is 0!\n"));
641 if (Attributes
== 0) {
644 return EFI_UNSUPPORTED
;
647 if ((CurrentPagingContext
.ContextData
.Ia32
.Attributes
& PAGE_TABLE_LIB_PAGING_CONTEXT_IA32_X64_ATTRIBUTES_PAE
) == 0) {
648 DEBUG ((DEBUG_ERROR
, "Non-PAE Paging!\n"));
649 return EFI_UNSUPPORTED
;
652 case IMAGE_FILE_MACHINE_X64
:
653 ASSERT (CurrentPagingContext
.ContextData
.X64
.PageTableBase
!= 0);
657 return EFI_UNSUPPORTED
;
661 // DEBUG ((DEBUG_ERROR, "ConvertMemoryPageAttributes(%x) - %016lx, %016lx, %02lx\n", IsSet, BaseAddress, Length, Attributes));
663 if (IsSplitted
!= NULL
) {
666 if (IsModified
!= NULL
) {
671 // Below logic is to check 2M/4K page to make sure we donot waist memory.
673 while (Length
!= 0) {
674 PageEntry
= GetPageTableEntry (&CurrentPagingContext
, BaseAddress
, &PageAttribute
);
675 if (PageEntry
== NULL
) {
676 return RETURN_UNSUPPORTED
;
678 PageEntryLength
= PageAttributeToLength (PageAttribute
);
679 SplitAttribute
= NeedSplitPage (BaseAddress
, Length
, PageEntry
, PageAttribute
);
680 if (SplitAttribute
== PageNone
) {
681 ConvertPageEntryAttribute (&CurrentPagingContext
, PageEntry
, Attributes
, PageAction
, &IsEntryModified
);
682 if (IsEntryModified
) {
683 if (IsModified
!= NULL
) {
688 // Convert success, move to next
690 BaseAddress
+= PageEntryLength
;
691 Length
-= PageEntryLength
;
693 if (AllocatePagesFunc
== NULL
) {
694 return RETURN_UNSUPPORTED
;
696 Status
= SplitPage (PageEntry
, PageAttribute
, SplitAttribute
, AllocatePagesFunc
);
697 if (RETURN_ERROR (Status
)) {
698 return RETURN_UNSUPPORTED
;
700 if (IsSplitted
!= NULL
) {
703 if (IsModified
!= NULL
) {
707 // Just split current page
708 // Convert success in next around
713 return RETURN_SUCCESS
;
717 This function assigns the page attributes for the memory region specified by BaseAddress and
718 Length from their current attributes to the attributes specified by Attributes.
720 Caller should make sure BaseAddress and Length is at page boundary.
722 Caller need guarentee the TPL <= TPL_NOTIFY, if there is split page request.
724 @param[in] PagingContext The paging context. NULL means get page table from current CPU context.
725 @param[in] BaseAddress The physical address that is the start address of a memory region.
726 @param[in] Length The size in bytes of the memory region.
727 @param[in] Attributes The bit mask of attributes to set for the memory region.
728 @param[in] AllocatePagesFunc If page split is needed, this function is used to allocate more pages.
729 NULL mean page split is unsupported.
731 @retval RETURN_SUCCESS The attributes were cleared for the memory region.
732 @retval RETURN_ACCESS_DENIED The attributes for the memory resource range specified by
733 BaseAddress and Length cannot be modified.
734 @retval RETURN_INVALID_PARAMETER Length is zero.
735 Attributes specified an illegal combination of attributes that
736 cannot be set together.
737 @retval RETURN_OUT_OF_RESOURCES There are not enough system resources to modify the attributes of
738 the memory resource range.
739 @retval RETURN_UNSUPPORTED The processor does not support one or more bytes of the memory
740 resource range specified by BaseAddress and Length.
741 The bit mask of attributes is not support for the memory resource
742 range specified by BaseAddress and Length.
746 AssignMemoryPageAttributes (
747 IN PAGE_TABLE_LIB_PAGING_CONTEXT
*PagingContext OPTIONAL
,
748 IN PHYSICAL_ADDRESS BaseAddress
,
750 IN UINT64 Attributes
,
751 IN PAGE_TABLE_LIB_ALLOCATE_PAGES AllocatePagesFunc OPTIONAL
754 RETURN_STATUS Status
;
758 // DEBUG((DEBUG_INFO, "AssignMemoryPageAttributes: 0x%lx - 0x%lx (0x%lx)\n", BaseAddress, Length, Attributes));
759 Status
= ConvertMemoryPageAttributes (PagingContext
, BaseAddress
, Length
, Attributes
, PageActionAssign
, AllocatePagesFunc
, &IsSplitted
, &IsModified
);
760 if (!EFI_ERROR(Status
)) {
761 if ((PagingContext
== NULL
) && IsModified
) {
763 // Flush TLB as last step
766 SyncMemoryPageAttributesAp (SyncCpuFlushTlb
);
774 Initialize the Page Table lib.
777 InitializePageTableLib (
781 PAGE_TABLE_LIB_PAGING_CONTEXT CurrentPagingContext
;
783 GetCurrentPagingContext (&CurrentPagingContext
);
784 DEBUG ((DEBUG_INFO
, "CurrentPagingContext:\n", CurrentPagingContext
.MachineType
));
785 DEBUG ((DEBUG_INFO
, " MachineType - 0x%x\n", CurrentPagingContext
.MachineType
));
786 DEBUG ((DEBUG_INFO
, " PageTableBase - 0x%x\n", CurrentPagingContext
.ContextData
.X64
.PageTableBase
));
787 DEBUG ((DEBUG_INFO
, " Attributes - 0x%x\n", CurrentPagingContext
.ContextData
.X64
.Attributes
));