2 Page table manipulation functions for IA-32 processors
4 Copyright (c) 2009 - 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.
17 #include "PiSmmCpuDxeSmm.h"
19 UINT8 mPhysicalAddressBits
;
22 Create PageTable for SMM use.
24 @return PageTable Address
32 UINTN PageFaultHandlerHookAddress
;
33 IA32_IDT_GATE_DESCRIPTOR
*IdtEntry
;
37 // Initialize spin lock
39 InitializeSpinLock (mPFLock
);
41 mPhysicalAddressBits
= 32;
43 if (FeaturePcdGet (PcdCpuSmmProfileEnable
)) {
45 // Set own Page Fault entry instead of the default one, because SMM Profile
46 // feature depends on IRET instruction to do Single Step
48 PageFaultHandlerHookAddress
= (UINTN
)PageFaultIdtHandlerSmmProfile
;
49 IdtEntry
= (IA32_IDT_GATE_DESCRIPTOR
*) gcSmiIdtr
.Base
;
50 IdtEntry
+= EXCEPT_IA32_PAGE_FAULT
;
51 IdtEntry
->Bits
.OffsetLow
= (UINT16
)PageFaultHandlerHookAddress
;
52 IdtEntry
->Bits
.Reserved_0
= 0;
53 IdtEntry
->Bits
.GateType
= IA32_IDT_GATE_TYPE_INTERRUPT_32
;
54 IdtEntry
->Bits
.OffsetHigh
= (UINT16
)(PageFaultHandlerHookAddress
>> 16);
57 // Register SMM Page Fault Handler
59 Status
= SmmRegisterExceptionHandler (&mSmmCpuService
, EXCEPT_IA32_PAGE_FAULT
, SmiPFHandler
);
60 ASSERT_EFI_ERROR (Status
);
64 // Additional SMM IDT initialization for SMM stack guard
66 if (FeaturePcdGet (PcdCpuSmmStackGuard
)) {
67 InitializeIDTSmmStackGuard ();
69 return Gen4GPageTable (TRUE
);
73 Page Fault handler for SMM use.
85 ThePage Fault handler wrapper for SMM use.
87 @param InterruptType Defines the type of interrupt or exception that
88 occurred on the processor.This parameter is processor architecture specific.
89 @param SystemContext A pointer to the processor context when
90 the interrupt occurred on the processor.
95 IN EFI_EXCEPTION_TYPE InterruptType
,
96 IN EFI_SYSTEM_CONTEXT SystemContext
100 UINTN GuardPageAddress
;
103 ASSERT (InterruptType
== EXCEPT_IA32_PAGE_FAULT
);
105 AcquireSpinLock (mPFLock
);
107 PFAddress
= AsmReadCr2 ();
110 // If a page fault occurs in SMRAM range, it might be in a SMM stack guard page,
111 // or SMM page protection violation.
113 if ((PFAddress
>= mCpuHotPlugData
.SmrrBase
) &&
114 (PFAddress
< (mCpuHotPlugData
.SmrrBase
+ mCpuHotPlugData
.SmrrSize
))) {
115 DumpCpuContext (InterruptType
, SystemContext
);
116 CpuIndex
= GetCpuIndex ();
117 GuardPageAddress
= (mSmmStackArrayBase
+ EFI_PAGE_SIZE
+ CpuIndex
* mSmmStackSize
);
118 if ((FeaturePcdGet (PcdCpuSmmStackGuard
)) &&
119 (PFAddress
>= GuardPageAddress
) &&
120 (PFAddress
< (GuardPageAddress
+ EFI_PAGE_SIZE
))) {
121 DEBUG ((DEBUG_ERROR
, "SMM stack overflow!\n"));
123 if ((SystemContext
.SystemContextIa32
->ExceptionData
& IA32_PF_EC_ID
) != 0) {
124 DEBUG ((DEBUG_ERROR
, "SMM exception at execution (0x%x)\n", PFAddress
));
126 DumpModuleInfoByIp (*(UINTN
*)(UINTN
)SystemContext
.SystemContextIa32
->Esp
);
129 DEBUG ((DEBUG_ERROR
, "SMM exception at access (0x%x)\n", PFAddress
));
131 DumpModuleInfoByIp ((UINTN
)SystemContext
.SystemContextIa32
->Eip
);
139 // If a page fault occurs in SMM range
141 if ((PFAddress
< mCpuHotPlugData
.SmrrBase
) ||
142 (PFAddress
>= mCpuHotPlugData
.SmrrBase
+ mCpuHotPlugData
.SmrrSize
)) {
143 DumpCpuContext (InterruptType
, SystemContext
);
144 if ((SystemContext
.SystemContextIa32
->ExceptionData
& IA32_PF_EC_ID
) != 0) {
145 DEBUG ((DEBUG_ERROR
, "Code executed on IP(0x%x) out of SMM range after SMM is locked!\n", PFAddress
));
147 DumpModuleInfoByIp (*(UINTN
*)(UINTN
)SystemContext
.SystemContextIa32
->Esp
);
151 if (IsSmmCommBufferForbiddenAddress (PFAddress
)) {
152 DEBUG ((DEBUG_ERROR
, "Access SMM communication forbidden address (0x%x)!\n", PFAddress
));
154 DumpModuleInfoByIp ((UINTN
)SystemContext
.SystemContextIa32
->Eip
);
160 if (FeaturePcdGet (PcdCpuSmmProfileEnable
)) {
161 SmmProfilePFHandler (
162 SystemContext
.SystemContextIa32
->Eip
,
163 SystemContext
.SystemContextIa32
->ExceptionData
166 DumpCpuContext (InterruptType
, SystemContext
);
167 SmiDefaultPFHandler ();
170 ReleaseSpinLock (mPFLock
);
174 This function sets memory attribute for page table.
177 SetPageTableAttributes (
187 BOOLEAN PageTableSplitted
;
189 DEBUG ((DEBUG_INFO
, "SetPageTableAttributes\n"));
192 // Disable write protection, because we need mark page table to be write protected.
193 // We need *write* page table memory, to mark itself to be *read only*.
195 AsmWriteCr0 (AsmReadCr0() & ~CR0_WP
);
198 DEBUG ((DEBUG_INFO
, "Start...\n"));
199 PageTableSplitted
= FALSE
;
201 L3PageTable
= (UINT64
*)GetPageTableBase ();
203 SmmSetMemoryAttributesEx ((EFI_PHYSICAL_ADDRESS
)(UINTN
)L3PageTable
, SIZE_4KB
, EFI_MEMORY_RO
, &IsSplitted
);
204 PageTableSplitted
= (PageTableSplitted
|| IsSplitted
);
206 for (Index3
= 0; Index3
< 4; Index3
++) {
207 L2PageTable
= (UINT64
*)(UINTN
)(L3PageTable
[Index3
] & ~mAddressEncMask
& PAGING_4K_ADDRESS_MASK_64
);
208 if (L2PageTable
== NULL
) {
212 SmmSetMemoryAttributesEx ((EFI_PHYSICAL_ADDRESS
)(UINTN
)L2PageTable
, SIZE_4KB
, EFI_MEMORY_RO
, &IsSplitted
);
213 PageTableSplitted
= (PageTableSplitted
|| IsSplitted
);
215 for (Index2
= 0; Index2
< SIZE_4KB
/sizeof(UINT64
); Index2
++) {
216 if ((L2PageTable
[Index2
] & IA32_PG_PS
) != 0) {
220 L1PageTable
= (UINT64
*)(UINTN
)(L2PageTable
[Index2
] & ~mAddressEncMask
& PAGING_4K_ADDRESS_MASK_64
);
221 if (L1PageTable
== NULL
) {
224 SmmSetMemoryAttributesEx ((EFI_PHYSICAL_ADDRESS
)(UINTN
)L1PageTable
, SIZE_4KB
, EFI_MEMORY_RO
, &IsSplitted
);
225 PageTableSplitted
= (PageTableSplitted
|| IsSplitted
);
228 } while (PageTableSplitted
);
231 // Enable write protection, after page table updated.
233 AsmWriteCr0 (AsmReadCr0() | CR0_WP
);