2 Basic paging support for the CPU to enable Stack Guard.
4 Copyright (c) 2018 - 2019, Intel Corporation. All rights reserved.<BR>
6 SPDX-License-Identifier: BSD-2-Clause-Patent
10 #include <Register/Intel/Cpuid.h>
11 #include <Register/Intel/Msr.h>
12 #include <Library/MemoryAllocationLib.h>
13 #include <Library/CpuLib.h>
14 #include <Library/BaseLib.h>
18 #define IA32_PG_P BIT0
19 #define IA32_PG_RW BIT1
20 #define IA32_PG_U BIT2
21 #define IA32_PG_A BIT5
22 #define IA32_PG_D BIT6
23 #define IA32_PG_PS BIT7
24 #define IA32_PG_NX BIT63
26 #define PAGE_ATTRIBUTE_BITS (IA32_PG_RW | IA32_PG_P)
27 #define PAGE_PROGATE_BITS (IA32_PG_D | IA32_PG_A | IA32_PG_NX | IA32_PG_U |\
30 #define PAGING_PAE_INDEX_MASK 0x1FF
31 #define PAGING_4K_ADDRESS_MASK_64 0x000FFFFFFFFFF000ull
32 #define PAGING_2M_ADDRESS_MASK_64 0x000FFFFFFFE00000ull
33 #define PAGING_1G_ADDRESS_MASK_64 0x000FFFFFC0000000ull
34 #define PAGING_512G_ADDRESS_MASK_64 0x000FFF8000000000ull
47 PAGE_ATTRIBUTE Attribute
;
50 UINTN AddressBitOffset
;
51 UINTN AddressBitLength
;
52 } PAGE_ATTRIBUTE_TABLE
;
54 PAGE_ATTRIBUTE_TABLE mPageAttributeTable
[] = {
55 {PageNone
, 0, 0, 0, 0},
56 {Page4K
, SIZE_4KB
, PAGING_4K_ADDRESS_MASK_64
, 12, 9},
57 {Page2M
, SIZE_2MB
, PAGING_2M_ADDRESS_MASK_64
, 21, 9},
58 {Page1G
, SIZE_1GB
, PAGING_1G_ADDRESS_MASK_64
, 30, 9},
59 {Page512G
, SIZE_512GB
, PAGING_512G_ADDRESS_MASK_64
, 39, 9},
62 EFI_PEI_NOTIFY_DESCRIPTOR mPostMemNotifyList
[] = {
64 (EFI_PEI_PPI_DESCRIPTOR_NOTIFY_CALLBACK
| EFI_PEI_PPI_DESCRIPTOR_TERMINATE_LIST
),
65 &gEfiPeiMemoryDiscoveredPpiGuid
,
66 MemoryDiscoveredPpiNotifyCallback
71 The function will check if IA32 PAE is supported.
73 @retval TRUE IA32 PAE is supported.
74 @retval FALSE IA32 PAE is not supported.
83 CPUID_VERSION_INFO_EDX RegEdx
;
85 AsmCpuid (CPUID_SIGNATURE
, &RegEax
, NULL
, NULL
, NULL
);
86 if (RegEax
>= CPUID_VERSION_INFO
) {
87 AsmCpuid (CPUID_VERSION_INFO
, NULL
, NULL
, NULL
, &RegEdx
.Uint32
);
88 if (RegEdx
.Bits
.PAE
!= 0) {
97 This API provides a way to allocate memory for page table.
99 @param Pages The number of 4 KB pages to allocate.
101 @return A pointer to the allocated buffer or NULL if allocation fails.
105 AllocatePageTableMemory (
111 Address
= AllocatePages(Pages
);
112 if (Address
!= NULL
) {
113 ZeroMem(Address
, EFI_PAGES_TO_SIZE (Pages
));
120 Get the address width supported by current processor.
122 @retval 32 If processor is in 32-bit mode.
123 @retval 36-48 If processor is in 64-bit mode.
127 GetPhysicalAddressWidth (
133 if (sizeof(UINTN
) == 4) {
137 AsmCpuid(CPUID_EXTENDED_FUNCTION
, &RegEax
, NULL
, NULL
, NULL
);
138 if (RegEax
>= CPUID_VIR_PHY_ADDRESS_SIZE
) {
139 AsmCpuid (CPUID_VIR_PHY_ADDRESS_SIZE
, &RegEax
, NULL
, NULL
, NULL
);
145 return (UINTN
)RegEax
;
152 Get the type of top level page table.
154 @retval Page512G PML4 paging.
155 @retval Page1G PAE paing.
159 GetPageTableTopLevelType (
163 MSR_IA32_EFER_REGISTER MsrEfer
;
165 MsrEfer
.Uint64
= AsmReadMsr64 (MSR_CORE_IA32_EFER
);
167 return (MsrEfer
.Bits
.LMA
== 1) ? Page512G
: Page1G
;
171 Return page table entry matching the address.
173 @param[in] Address The address to be checked.
174 @param[out] PageAttributes The page attribute of the page entry.
176 @return The page entry.
180 IN PHYSICAL_ADDRESS Address
,
181 OUT PAGE_ATTRIBUTE
*PageAttribute
187 UINT64 AddressEncMask
;
189 AddressEncMask
= PcdGet64 (PcdPteMemoryEncryptionAddressOrMask
);
190 PageTable
= (UINT64
*)(UINTN
)(AsmReadCr3 () & PAGING_4K_ADDRESS_MASK_64
);
191 for (Level
= (INTN
)GetPageTableTopLevelType (); Level
> 0; --Level
) {
192 Index
= (UINTN
)RShiftU64 (Address
, mPageAttributeTable
[Level
].AddressBitOffset
);
193 Index
&= PAGING_PAE_INDEX_MASK
;
198 if (PageTable
[Index
] == 0) {
199 *PageAttribute
= PageNone
;
206 if ((PageTable
[Index
] & IA32_PG_PS
) != 0 || Level
== PageMin
) {
207 *PageAttribute
= (PAGE_ATTRIBUTE
)Level
;
208 return &PageTable
[Index
];
212 // Page directory or table
214 PageTable
= (UINT64
*)(UINTN
)(PageTable
[Index
] &
216 PAGING_4K_ADDRESS_MASK_64
);
219 *PageAttribute
= PageNone
;
224 This function splits one page entry to smaller page entries.
226 @param[in] PageEntry The page entry to be splitted.
227 @param[in] PageAttribute The page attribute of the page entry.
228 @param[in] SplitAttribute How to split the page entry.
229 @param[in] Recursively Do the split recursively or not.
231 @retval RETURN_SUCCESS The page entry is splitted.
232 @retval RETURN_INVALID_PARAMETER If target page attribute is invalid
233 @retval RETURN_OUT_OF_RESOURCES No resource to split page entry.
237 IN UINT64
*PageEntry
,
238 IN PAGE_ATTRIBUTE PageAttribute
,
239 IN PAGE_ATTRIBUTE SplitAttribute
,
240 IN BOOLEAN Recursively
244 UINT64
*NewPageEntry
;
246 UINT64 AddressEncMask
;
247 PAGE_ATTRIBUTE SplitTo
;
249 if (SplitAttribute
== PageNone
|| SplitAttribute
>= PageAttribute
) {
250 ASSERT (SplitAttribute
!= PageNone
);
251 ASSERT (SplitAttribute
< PageAttribute
);
252 return RETURN_INVALID_PARAMETER
;
255 NewPageEntry
= AllocatePageTableMemory (1);
256 if (NewPageEntry
== NULL
) {
257 ASSERT (NewPageEntry
!= NULL
);
258 return RETURN_OUT_OF_RESOURCES
;
262 // One level down each step to achieve more compact page table.
264 SplitTo
= PageAttribute
- 1;
265 AddressEncMask
= PcdGet64 (PcdPteMemoryEncryptionAddressOrMask
) &
266 mPageAttributeTable
[SplitTo
].AddressMask
;
267 BaseAddress
= *PageEntry
&
268 ~PcdGet64 (PcdPteMemoryEncryptionAddressOrMask
) &
269 mPageAttributeTable
[PageAttribute
].AddressMask
;
270 for (Index
= 0; Index
< SIZE_4KB
/ sizeof(UINT64
); Index
++) {
271 NewPageEntry
[Index
] = BaseAddress
| AddressEncMask
|
272 ((*PageEntry
) & PAGE_PROGATE_BITS
);
274 if (SplitTo
!= PageMin
) {
275 NewPageEntry
[Index
] |= IA32_PG_PS
;
278 if (Recursively
&& SplitTo
> SplitAttribute
) {
279 SplitPage (&NewPageEntry
[Index
], SplitTo
, SplitAttribute
, Recursively
);
282 BaseAddress
+= mPageAttributeTable
[SplitTo
].Length
;
285 (*PageEntry
) = (UINT64
)(UINTN
)NewPageEntry
| AddressEncMask
| PAGE_ATTRIBUTE_BITS
;
287 return RETURN_SUCCESS
;
291 This function modifies the page attributes for the memory region specified
292 by BaseAddress and Length from their current attributes to the attributes
293 specified by Attributes.
295 Caller should make sure BaseAddress and Length is at page boundary.
297 @param[in] BaseAddress Start address of a memory region.
298 @param[in] Length Size in bytes of the memory region.
299 @param[in] Attributes Bit mask of attributes to modify.
301 @retval RETURN_SUCCESS The attributes were modified for the memory
303 @retval RETURN_INVALID_PARAMETER Length is zero; or,
304 Attributes specified an illegal combination
305 of attributes that cannot be set together; or
306 Addressis not 4KB aligned.
307 @retval RETURN_OUT_OF_RESOURCES There are not enough system resources to modify
309 @retval RETURN_UNSUPPORTED Cannot modify the attributes of given memory.
314 ConvertMemoryPageAttributes (
315 IN PHYSICAL_ADDRESS BaseAddress
,
321 PAGE_ATTRIBUTE PageAttribute
;
322 RETURN_STATUS Status
;
323 EFI_PHYSICAL_ADDRESS MaximumAddress
;
326 (BaseAddress
& (SIZE_4KB
- 1)) != 0 ||
327 (Length
& (SIZE_4KB
- 1)) != 0) {
330 ASSERT ((BaseAddress
& (SIZE_4KB
- 1)) == 0);
331 ASSERT ((Length
& (SIZE_4KB
- 1)) == 0);
333 return RETURN_INVALID_PARAMETER
;
336 MaximumAddress
= (EFI_PHYSICAL_ADDRESS
)MAX_UINT32
;
337 if (BaseAddress
> MaximumAddress
||
338 Length
> MaximumAddress
||
339 (BaseAddress
> MaximumAddress
- (Length
- 1))) {
340 return RETURN_UNSUPPORTED
;
344 // Below logic is to check 2M/4K page to make sure we do not waste memory.
346 while (Length
!= 0) {
347 PageEntry
= GetPageTableEntry (BaseAddress
, &PageAttribute
);
348 if (PageEntry
== NULL
) {
349 return RETURN_UNSUPPORTED
;
352 if (PageAttribute
!= Page4K
) {
353 Status
= SplitPage (PageEntry
, PageAttribute
, Page4K
, FALSE
);
354 if (RETURN_ERROR (Status
)) {
358 // Do it again until the page is 4K.
364 // Just take care of 'present' bit for Stack Guard.
366 if ((Attributes
& IA32_PG_P
) != 0) {
367 *PageEntry
|= (UINT64
)IA32_PG_P
;
369 *PageEntry
&= ~((UINT64
)IA32_PG_P
);
373 // Convert success, move to next
375 BaseAddress
+= SIZE_4KB
;
379 return RETURN_SUCCESS
;
383 Get maximum size of page memory supported by current processor.
385 @param[in] TopLevelType The type of top level page entry.
387 @retval Page1G If processor supports 1G page and PML4.
388 @retval Page2M For all other situations.
393 IN PAGE_ATTRIBUTE TopLevelType
399 if (TopLevelType
== Page512G
) {
400 AsmCpuid (CPUID_EXTENDED_FUNCTION
, &RegEax
, NULL
, NULL
, NULL
);
401 if (RegEax
>= CPUID_EXTENDED_CPU_SIG
) {
402 AsmCpuid (CPUID_EXTENDED_CPU_SIG
, NULL
, NULL
, NULL
, &RegEdx
);
403 if ((RegEdx
& BIT26
) != 0) {
413 Create PML4 or PAE page table.
415 @return The address of page table.
423 RETURN_STATUS Status
;
424 UINTN PhysicalAddressBits
;
425 UINTN NumberOfEntries
;
426 PAGE_ATTRIBUTE TopLevelPageAttr
;
428 PAGE_ATTRIBUTE MaxMemoryPage
;
430 UINT64 AddressEncMask
;
432 EFI_PHYSICAL_ADDRESS PhysicalAddress
;
434 TopLevelPageAttr
= (PAGE_ATTRIBUTE
)GetPageTableTopLevelType ();
435 PhysicalAddressBits
= GetPhysicalAddressWidth ();
436 NumberOfEntries
= (UINTN
)1 << (PhysicalAddressBits
-
437 mPageAttributeTable
[TopLevelPageAttr
].AddressBitOffset
);
439 PageTable
= (UINTN
) AllocatePageTableMemory (1);
440 if (PageTable
== 0) {
444 AddressEncMask
= PcdGet64 (PcdPteMemoryEncryptionAddressOrMask
);
445 AddressEncMask
&= mPageAttributeTable
[TopLevelPageAttr
].AddressMask
;
446 MaxMemoryPage
= GetMaxMemoryPage (TopLevelPageAttr
);
447 PageEntry
= (UINT64
*)PageTable
;
450 for (Index
= 0; Index
< NumberOfEntries
; ++Index
) {
451 *PageEntry
= PhysicalAddress
| AddressEncMask
| PAGE_ATTRIBUTE_BITS
;
454 // Split the top page table down to the maximum page size supported
456 if (MaxMemoryPage
< TopLevelPageAttr
) {
457 Status
= SplitPage(PageEntry
, TopLevelPageAttr
, MaxMemoryPage
, TRUE
);
458 ASSERT_EFI_ERROR (Status
);
461 if (TopLevelPageAttr
== Page1G
) {
463 // PDPTE[2:1] (PAE Paging) must be 0. SplitPage() might change them to 1.
465 *PageEntry
&= ~(UINT64
)(IA32_PG_RW
| IA32_PG_U
);
469 PhysicalAddress
+= mPageAttributeTable
[TopLevelPageAttr
].Length
;
477 Setup page tables and make them work.
487 PageTable
= CreatePageTable ();
488 ASSERT (PageTable
!= 0);
489 if (PageTable
!= 0) {
490 AsmWriteCr3(PageTable
);
491 AsmWriteCr4 (AsmReadCr4 () | BIT5
); // CR4.PAE
492 AsmWriteCr0 (AsmReadCr0 () | BIT31
); // CR0.PG
497 Get the base address of current AP's stack.
499 This function is called in AP's context and assumes that whole calling stacks
500 (till this function) consumed by AP's wakeup procedure will not exceed 4KB.
502 PcdCpuApStackSize must be configured with value taking the Guard page into
505 @param[in,out] Buffer The pointer to private data buffer.
514 EFI_PHYSICAL_ADDRESS StackBase
;
516 StackBase
= (EFI_PHYSICAL_ADDRESS
)(UINTN
)&StackBase
;
517 StackBase
+= BASE_4KB
;
518 StackBase
&= ~((EFI_PHYSICAL_ADDRESS
)BASE_4KB
- 1);
519 StackBase
-= PcdGet32(PcdCpuApStackSize
);
521 *(EFI_PHYSICAL_ADDRESS
*)Buffer
= StackBase
;
525 Setup stack Guard page at the stack base of each processor. BSP and APs have
526 different way to get stack base address.
530 SetupStackGuardPage (
534 EFI_PEI_HOB_POINTERS Hob
;
535 EFI_PHYSICAL_ADDRESS StackBase
;
536 UINTN NumberOfProcessors
;
541 // One extra page at the bottom of the stack is needed for Guard page.
543 if (PcdGet32(PcdCpuApStackSize
) <= EFI_PAGE_SIZE
) {
544 DEBUG ((DEBUG_ERROR
, "PcdCpuApStackSize is not big enough for Stack Guard!\n"));
548 MpInitLibGetNumberOfProcessors(&NumberOfProcessors
, NULL
);
549 MpInitLibWhoAmI (&Bsp
);
550 for (Index
= 0; Index
< NumberOfProcessors
; ++Index
) {
554 Hob
.Raw
= GetHobList ();
555 while ((Hob
.Raw
= GetNextHob (EFI_HOB_TYPE_MEMORY_ALLOCATION
, Hob
.Raw
)) != NULL
) {
556 if (CompareGuid (&gEfiHobMemoryAllocStackGuid
,
557 &(Hob
.MemoryAllocationStack
->AllocDescriptor
.Name
))) {
558 StackBase
= Hob
.MemoryAllocationStack
->AllocDescriptor
.MemoryBaseAddress
;
561 Hob
.Raw
= GET_NEXT_HOB (Hob
);
565 // Ask AP to return is stack base address.
567 MpInitLibStartupThisAP(GetStackBase
, Index
, NULL
, 0, (VOID
*)&StackBase
, NULL
);
569 ASSERT (StackBase
!= 0);
571 // Set Guard page at stack base address.
573 ConvertMemoryPageAttributes(StackBase
, EFI_PAGE_SIZE
, 0);
574 DEBUG ((DEBUG_INFO
, "Stack Guard set at %lx [cpu%lu]!\n",
575 (UINT64
)StackBase
, (UINT64
)Index
));
579 // Publish the changes of page table.
585 Enabl/setup stack guard for each processor if PcdCpuStackGuard is set to TRUE.
587 Doing this in the memory-discovered callback is to make sure the Stack Guard
588 feature to cover as most PEI code as possible.
590 @param[in] PeiServices General purpose services available to every PEIM.
591 @param[in] NotifyDescriptor The notification structure this PEIM registered on install.
592 @param[in] Ppi The memory discovered PPI. Not used.
594 @retval EFI_SUCCESS The function completed successfully.
595 @retval others There's error in MP initialization.
599 MemoryDiscoveredPpiNotifyCallback (
600 IN EFI_PEI_SERVICES
**PeiServices
,
601 IN EFI_PEI_NOTIFY_DESCRIPTOR
*NotifyDescriptor
,
606 BOOLEAN InitStackGuard
;
609 // Paging must be setup first. Otherwise the exception TSS setup during MP
610 // initialization later will not contain paging information and then fail
611 // the task switch (for the sake of stack switch).
613 InitStackGuard
= FALSE
;
614 if (IsIa32PaeSupported () && PcdGetBool (PcdCpuStackGuard
)) {
616 InitStackGuard
= TRUE
;
619 Status
= InitializeCpuMpWorker ((CONST EFI_PEI_SERVICES
**)PeiServices
);
620 ASSERT_EFI_ERROR (Status
);
622 if (InitStackGuard
) {
623 SetupStackGuardPage ();