]> git.proxmox.com Git - mirror_edk2.git/blame - UefiCpuPkg/PiSmmCpuDxeSmm/Ia32/PageTbl.c
UefiCpuPkg/PiSmmCpuDxeSmm: Add paging protection.
[mirror_edk2.git] / UefiCpuPkg / PiSmmCpuDxeSmm / Ia32 / PageTbl.c
CommitLineData
7947da3c
MK
1/** @file\r
2Page table manipulation functions for IA-32 processors\r
3\r
fe3a75bc 4Copyright (c) 2009 - 2016, Intel Corporation. All rights reserved.<BR>\r
7947da3c
MK
5This program and the accompanying materials\r
6are licensed and made available under the terms and conditions of the BSD License\r
7which accompanies this distribution. The full text of the license may be found at\r
8http://opensource.org/licenses/bsd-license.php\r
9\r
10THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
11WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
12\r
13**/\r
14\r
15#include "PiSmmCpuDxeSmm.h"\r
16\r
7947da3c
MK
17/**\r
18 Create PageTable for SMM use.\r
19\r
20 @return PageTable Address\r
21\r
22**/\r
23UINT32\r
24SmmInitPageTable (\r
25 VOID\r
26 )\r
27{\r
28 UINTN PageFaultHandlerHookAddress;\r
29 IA32_IDT_GATE_DESCRIPTOR *IdtEntry;\r
30\r
31 //\r
32 // Initialize spin lock\r
33 //\r
fe3a75bc 34 InitializeSpinLock (mPFLock);\r
7947da3c
MK
35\r
36 if (FeaturePcdGet (PcdCpuSmmProfileEnable)) {\r
37 //\r
38 // Set own Page Fault entry instead of the default one, because SMM Profile\r
39 // feature depends on IRET instruction to do Single Step\r
40 //\r
41 PageFaultHandlerHookAddress = (UINTN)PageFaultIdtHandlerSmmProfile;\r
42 IdtEntry = (IA32_IDT_GATE_DESCRIPTOR *) gcSmiIdtr.Base;\r
43 IdtEntry += EXCEPT_IA32_PAGE_FAULT;\r
44 IdtEntry->Bits.OffsetLow = (UINT16)PageFaultHandlerHookAddress;\r
45 IdtEntry->Bits.Reserved_0 = 0;\r
46 IdtEntry->Bits.GateType = IA32_IDT_GATE_TYPE_INTERRUPT_32;\r
47 IdtEntry->Bits.OffsetHigh = (UINT16)(PageFaultHandlerHookAddress >> 16);\r
48 } else {\r
49 //\r
50 // Register SMM Page Fault Handler\r
51 //\r
52 SmmRegisterExceptionHandler (&mSmmCpuService, EXCEPT_IA32_PAGE_FAULT, SmiPFHandler);\r
53 }\r
54\r
55 //\r
56 // Additional SMM IDT initialization for SMM stack guard\r
57 //\r
58 if (FeaturePcdGet (PcdCpuSmmStackGuard)) {\r
59 InitializeIDTSmmStackGuard ();\r
60 }\r
717fb604 61 return Gen4GPageTable (TRUE);\r
7947da3c
MK
62}\r
63\r
64/**\r
65 Page Fault handler for SMM use.\r
66\r
67**/\r
68VOID\r
69SmiDefaultPFHandler (\r
70 VOID\r
71 )\r
72{\r
73 CpuDeadLoop ();\r
74}\r
75\r
76/**\r
77 ThePage Fault handler wrapper for SMM use.\r
78\r
79 @param InterruptType Defines the type of interrupt or exception that\r
80 occurred on the processor.This parameter is processor architecture specific.\r
81 @param SystemContext A pointer to the processor context when\r
82 the interrupt occurred on the processor.\r
83**/\r
84VOID\r
85EFIAPI\r
86SmiPFHandler (\r
87 IN EFI_EXCEPTION_TYPE InterruptType,\r
88 IN EFI_SYSTEM_CONTEXT SystemContext\r
89 )\r
90{\r
91 UINTN PFAddress;\r
92\r
93 ASSERT (InterruptType == EXCEPT_IA32_PAGE_FAULT);\r
94\r
fe3a75bc 95 AcquireSpinLock (mPFLock);\r
7947da3c
MK
96\r
97 PFAddress = AsmReadCr2 ();\r
98\r
99 if ((FeaturePcdGet (PcdCpuSmmStackGuard)) &&\r
100 (PFAddress >= mCpuHotPlugData.SmrrBase) &&\r
101 (PFAddress < (mCpuHotPlugData.SmrrBase + mCpuHotPlugData.SmrrSize))) {\r
717fb604 102 DEBUG ((DEBUG_ERROR, "SMM stack overflow!\n"));\r
7947da3c
MK
103 CpuDeadLoop ();\r
104 }\r
105\r
106 //\r
107 // If a page fault occurs in SMM range\r
108 //\r
109 if ((PFAddress < mCpuHotPlugData.SmrrBase) ||\r
110 (PFAddress >= mCpuHotPlugData.SmrrBase + mCpuHotPlugData.SmrrSize)) {\r
111 if ((SystemContext.SystemContextIa32->ExceptionData & IA32_PF_EC_ID) != 0) {\r
717fb604 112 DEBUG ((DEBUG_ERROR, "Code executed on IP(0x%x) out of SMM range after SMM is locked!\n", PFAddress));\r
7947da3c
MK
113 DEBUG_CODE (\r
114 DumpModuleInfoByIp (*(UINTN *)(UINTN)SystemContext.SystemContextIa32->Esp);\r
115 );\r
116 CpuDeadLoop ();\r
117 }\r
118 }\r
119\r
120 if (FeaturePcdGet (PcdCpuSmmProfileEnable)) {\r
121 SmmProfilePFHandler (\r
122 SystemContext.SystemContextIa32->Eip,\r
123 SystemContext.SystemContextIa32->ExceptionData\r
124 );\r
125 } else {\r
126 SmiDefaultPFHandler ();\r
127 }\r
128\r
fe3a75bc 129 ReleaseSpinLock (mPFLock);\r
7947da3c 130}\r
717fb604
JY
131\r
132/**\r
133 This function sets memory attribute for page table.\r
134**/\r
135VOID\r
136SetPageTableAttributes (\r
137 VOID\r
138 )\r
139{\r
140 UINTN Index2;\r
141 UINTN Index3;\r
142 UINT64 *L1PageTable;\r
143 UINT64 *L2PageTable;\r
144 UINT64 *L3PageTable;\r
145 BOOLEAN IsSplitted;\r
146 BOOLEAN PageTableSplitted;\r
147\r
148 DEBUG ((DEBUG_INFO, "SetPageTableAttributes\n"));\r
149\r
150 //\r
151 // Disable write protection, because we need mark page table to be write protected.\r
152 // We need *write* page table memory, to mark itself to be *read only*.\r
153 //\r
154 AsmWriteCr0 (AsmReadCr0() & ~CR0_WP);\r
155\r
156 do {\r
157 DEBUG ((DEBUG_INFO, "Start...\n"));\r
158 PageTableSplitted = FALSE;\r
159\r
160 L3PageTable = (UINT64 *)GetPageTableBase ();\r
161\r
162 SmmSetMemoryAttributesEx ((EFI_PHYSICAL_ADDRESS)(UINTN)L3PageTable, SIZE_4KB, EFI_MEMORY_RO, &IsSplitted);\r
163 PageTableSplitted = (PageTableSplitted || IsSplitted);\r
164\r
165 for (Index3 = 0; Index3 < 4; Index3++) {\r
166 L2PageTable = (UINT64 *)(UINTN)(L3PageTable[Index3] & PAGING_4K_ADDRESS_MASK_64);\r
167 if (L2PageTable == NULL) {\r
168 continue;\r
169 }\r
170\r
171 SmmSetMemoryAttributesEx ((EFI_PHYSICAL_ADDRESS)(UINTN)L2PageTable, SIZE_4KB, EFI_MEMORY_RO, &IsSplitted);\r
172 PageTableSplitted = (PageTableSplitted || IsSplitted);\r
173\r
174 for (Index2 = 0; Index2 < SIZE_4KB/sizeof(UINT64); Index2++) {\r
175 if ((L2PageTable[Index2] & IA32_PG_PS) != 0) {\r
176 // 2M\r
177 continue;\r
178 }\r
179 L1PageTable = (UINT64 *)(UINTN)(L2PageTable[Index2] & PAGING_4K_ADDRESS_MASK_64);\r
180 if (L1PageTable == NULL) {\r
181 continue;\r
182 }\r
183 SmmSetMemoryAttributesEx ((EFI_PHYSICAL_ADDRESS)(UINTN)L1PageTable, SIZE_4KB, EFI_MEMORY_RO, &IsSplitted);\r
184 PageTableSplitted = (PageTableSplitted || IsSplitted);\r
185 }\r
186 }\r
187 } while (PageTableSplitted);\r
188\r
189 //\r
190 // Enable write protection, after page table updated.\r
191 //\r
192 AsmWriteCr0 (AsmReadCr0() | CR0_WP);\r
193\r
194 return ;\r
195}\r