UefiCpuPkg/CpuDxe: Enable protection for newly added page table
[mirror_edk2.git] / UefiCpuPkg / CpuS3DataDxe / CpuS3Data.c
CommitLineData
bfec5efa
MK
1/** @file\r
2ACPI CPU Data initialization module\r
3\r
4This module initializes the ACPI_CPU_DATA structure and registers the address\r
5of this structure in the PcdCpuS3DataAddress PCD. This is a generic/simple\r
6version of this module. It does not provide a machine check handler or CPU\r
7register initialization tables for ACPI S3 resume. It also only supports the\r
8number of CPUs reported by the MP Services Protocol, so this module does not\r
9support hot plug CPUs. This module can be copied into a CPU specific package\r
10and customized if these additional features are required.\r
11\r
9cc45009 12Copyright (c) 2013 - 2017, Intel Corporation. All rights reserved.<BR>\r
bfec5efa
MK
13Copyright (c) 2015, Red Hat, Inc.\r
14\r
15This program and the accompanying materials\r
16are licensed and made available under the terms and conditions of the BSD License\r
17which accompanies this distribution. The full text of the license may be found at\r
18http://opensource.org/licenses/bsd-license.php\r
19\r
20THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
21WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
22\r
23**/\r
24\r
25#include <PiDxe.h>\r
26\r
27#include <AcpiCpuData.h>\r
28\r
29#include <Library/BaseLib.h>\r
30#include <Library/BaseMemoryLib.h>\r
31#include <Library/UefiBootServicesTableLib.h>\r
32#include <Library/DebugLib.h>\r
33#include <Library/MtrrLib.h>\r
34\r
35#include <Protocol/MpService.h>\r
36#include <Guid/EventGroup.h>\r
37\r
38//\r
39// Data structure used to allocate ACPI_CPU_DATA and its supporting structures\r
40//\r
41typedef struct {\r
42 ACPI_CPU_DATA AcpiCpuData;\r
43 MTRR_SETTINGS MtrrTable;\r
44 IA32_DESCRIPTOR GdtrProfile;\r
45 IA32_DESCRIPTOR IdtrProfile;\r
46} ACPI_CPU_DATA_EX;\r
47\r
48/**\r
49 Allocate EfiACPIMemoryNVS below 4G memory address.\r
50\r
51 This function allocates EfiACPIMemoryNVS below 4G memory address.\r
52\r
53 @param[in] Size Size of memory to allocate.\r
54\r
55 @return Allocated address for output.\r
56\r
57**/\r
58VOID *\r
59AllocateAcpiNvsMemoryBelow4G (\r
60 IN UINTN Size\r
61 )\r
62{\r
63 EFI_PHYSICAL_ADDRESS Address;\r
64 EFI_STATUS Status;\r
65 VOID *Buffer;\r
66\r
67 Address = BASE_4GB - 1;\r
68 Status = gBS->AllocatePages (\r
69 AllocateMaxAddress,\r
70 EfiACPIMemoryNVS,\r
71 EFI_SIZE_TO_PAGES (Size),\r
72 &Address\r
73 );\r
74 if (EFI_ERROR (Status)) {\r
75 return NULL;\r
76 }\r
77\r
78 Buffer = (VOID *)(UINTN)Address;\r
79 ZeroMem (Buffer, Size);\r
80\r
81 return Buffer;\r
82}\r
83\r
84/**\r
85 Callback function executed when the EndOfDxe event group is signaled.\r
86\r
65b24ada 87 We delay allocating StartupVector and saving the MTRR settings until BDS signals EndOfDxe.\r
bfec5efa
MK
88\r
89 @param[in] Event Event whose notification function is being invoked.\r
90 @param[out] Context Pointer to the MTRR_SETTINGS buffer to fill in.\r
91**/\r
92VOID\r
93EFIAPI\r
65b24ada 94CpuS3DataOnEndOfDxe (\r
bfec5efa
MK
95 IN EFI_EVENT Event,\r
96 OUT VOID *Context\r
97 )\r
98{\r
65b24ada
JF
99 EFI_STATUS Status;\r
100 ACPI_CPU_DATA_EX *AcpiCpuDataEx;\r
101\r
102 AcpiCpuDataEx = (ACPI_CPU_DATA_EX *) Context;\r
103 //\r
104 // Allocate a 4KB reserved page below 1MB\r
105 //\r
106 AcpiCpuDataEx->AcpiCpuData.StartupVector = BASE_1MB - 1;\r
107 Status = gBS->AllocatePages (\r
108 AllocateMaxAddress,\r
109 EfiReservedMemoryType,\r
110 1,\r
111 &AcpiCpuDataEx->AcpiCpuData.StartupVector\r
112 );\r
113 ASSERT_EFI_ERROR (Status);\r
114\r
bfec5efa 115 DEBUG ((EFI_D_VERBOSE, "%a\n", __FUNCTION__));\r
65b24ada 116 MtrrGetAllMtrrs (&AcpiCpuDataEx->MtrrTable);\r
bfec5efa
MK
117\r
118 //\r
119 // Close event, so it will not be invoked again.\r
120 //\r
121 gBS->CloseEvent (Event);\r
122}\r
123\r
124/**\r
125 The entry function of the CpuS3Data driver.\r
126\r
127 Allocate and initialize all fields of the ACPI_CPU_DATA structure except the\r
128 MTRR settings. Register an event notification on gEfiEndOfDxeEventGroupGuid\r
129 to capture the ACPI_CPU_DATA MTRR settings. The PcdCpuS3DataAddress is set\r
130 to the address that ACPI_CPU_DATA is allocated at.\r
131\r
132 @param[in] ImageHandle The firmware allocated handle for the EFI image.\r
133 @param[in] SystemTable A pointer to the EFI System Table.\r
134\r
135 @retval EFI_SUCCESS The entry point is executed successfully.\r
ca98f603 136 @retval EFI_UNSUPPORTED Do not support ACPI S3.\r
bfec5efa
MK
137 @retval other Some error occurs when executing this entry point.\r
138\r
139**/\r
140EFI_STATUS\r
141EFIAPI\r
142CpuS3DataInitialize (\r
143 IN EFI_HANDLE ImageHandle,\r
144 IN EFI_SYSTEM_TABLE *SystemTable\r
145 )\r
146{\r
147 EFI_STATUS Status;\r
148 ACPI_CPU_DATA_EX *AcpiCpuDataEx;\r
149 ACPI_CPU_DATA *AcpiCpuData;\r
150 EFI_MP_SERVICES_PROTOCOL *MpServices;\r
151 UINTN NumberOfCpus;\r
152 UINTN NumberOfEnabledProcessors;\r
153 VOID *Stack;\r
154 UINTN TableSize;\r
155 CPU_REGISTER_TABLE *RegisterTable;\r
156 UINTN Index;\r
157 EFI_PROCESSOR_INFORMATION ProcessorInfoBuffer;\r
158 UINTN GdtSize;\r
159 UINTN IdtSize;\r
160 VOID *Gdt;\r
161 VOID *Idt;\r
162 EFI_EVENT Event;\r
8b371e93 163 ACPI_CPU_DATA *OldAcpiCpuData;\r
bfec5efa 164\r
ca98f603
SZ
165 if (!PcdGetBool (PcdAcpiS3Enable)) {\r
166 return EFI_UNSUPPORTED;\r
167 }\r
168\r
8b371e93
JF
169 //\r
170 // Set PcdCpuS3DataAddress to the base address of the ACPI_CPU_DATA structure\r
171 //\r
172 OldAcpiCpuData = (ACPI_CPU_DATA *) (UINTN) PcdGet64 (PcdCpuS3DataAddress);\r
173\r
bfec5efa
MK
174 //\r
175 // Allocate ACPI NVS memory below 4G memory for use on ACPI S3 resume.\r
176 //\r
177 AcpiCpuDataEx = AllocateAcpiNvsMemoryBelow4G (sizeof (ACPI_CPU_DATA_EX));\r
178 ASSERT (AcpiCpuDataEx != NULL);\r
179 AcpiCpuData = &AcpiCpuDataEx->AcpiCpuData;\r
180\r
181 //\r
182 // Get MP Services Protocol\r
183 //\r
184 Status = gBS->LocateProtocol (\r
185 &gEfiMpServiceProtocolGuid,\r
186 NULL,\r
187 (VOID **)&MpServices\r
188 );\r
189 ASSERT_EFI_ERROR (Status);\r
190\r
bfec5efa
MK
191 //\r
192 // Get the number of CPUs\r
193 //\r
194 Status = MpServices->GetNumberOfProcessors (\r
195 MpServices,\r
196 &NumberOfCpus,\r
197 &NumberOfEnabledProcessors\r
198 );\r
199 ASSERT_EFI_ERROR (Status);\r
200 AcpiCpuData->NumberOfCpus = (UINT32)NumberOfCpus;\r
201\r
202 //\r
203 // Initialize ACPI_CPU_DATA fields\r
204 //\r
205 AcpiCpuData->StackSize = PcdGet32 (PcdCpuApStackSize);\r
206 AcpiCpuData->ApMachineCheckHandlerBase = 0;\r
207 AcpiCpuData->ApMachineCheckHandlerSize = 0;\r
208 AcpiCpuData->GdtrProfile = (EFI_PHYSICAL_ADDRESS)(UINTN)&AcpiCpuDataEx->GdtrProfile;\r
209 AcpiCpuData->IdtrProfile = (EFI_PHYSICAL_ADDRESS)(UINTN)&AcpiCpuDataEx->IdtrProfile;\r
210 AcpiCpuData->MtrrTable = (EFI_PHYSICAL_ADDRESS)(UINTN)&AcpiCpuDataEx->MtrrTable;\r
211\r
212 //\r
213 // Allocate stack space for all CPUs\r
214 //\r
215 Stack = AllocateAcpiNvsMemoryBelow4G (NumberOfCpus * AcpiCpuData->StackSize);\r
216 ASSERT (Stack != NULL);\r
217 AcpiCpuData->StackAddress = (EFI_PHYSICAL_ADDRESS)(UINTN)Stack;\r
218\r
219 //\r
220 // Get the boot processor's GDT and IDT\r
221 //\r
222 AsmReadGdtr (&AcpiCpuDataEx->GdtrProfile);\r
223 AsmReadIdtr (&AcpiCpuDataEx->IdtrProfile);\r
224\r
225 //\r
226 // Allocate GDT and IDT in ACPI NVS and copy current GDT and IDT contents\r
227 //\r
228 GdtSize = AcpiCpuDataEx->GdtrProfile.Limit + 1;\r
229 IdtSize = AcpiCpuDataEx->IdtrProfile.Limit + 1;\r
230 Gdt = AllocateAcpiNvsMemoryBelow4G (GdtSize + IdtSize);\r
231 ASSERT (Gdt != NULL);\r
232 Idt = (VOID *)((UINTN)Gdt + GdtSize);\r
233 CopyMem (Gdt, (VOID *)AcpiCpuDataEx->GdtrProfile.Base, GdtSize);\r
234 CopyMem (Idt, (VOID *)AcpiCpuDataEx->IdtrProfile.Base, IdtSize);\r
235 AcpiCpuDataEx->GdtrProfile.Base = (UINTN)Gdt;\r
236 AcpiCpuDataEx->IdtrProfile.Base = (UINTN)Idt;\r
237\r
8b371e93
JF
238 if (OldAcpiCpuData != NULL) {\r
239 AcpiCpuData->RegisterTable = OldAcpiCpuData->RegisterTable;\r
240 AcpiCpuData->PreSmmInitRegisterTable = OldAcpiCpuData->PreSmmInitRegisterTable;\r
241 } else {\r
242 //\r
243 // Allocate buffer for empty RegisterTable and PreSmmInitRegisterTable for all CPUs\r
244 //\r
245 TableSize = 2 * NumberOfCpus * sizeof (CPU_REGISTER_TABLE);\r
246 RegisterTable = (CPU_REGISTER_TABLE *)AllocateAcpiNvsMemoryBelow4G (TableSize);\r
247 ASSERT (RegisterTable != NULL);\r
248\r
249 for (Index = 0; Index < NumberOfCpus; Index++) {\r
250 Status = MpServices->GetProcessorInfo (\r
bfec5efa
MK
251 MpServices,\r
252 Index,\r
253 &ProcessorInfoBuffer\r
254 );\r
8b371e93
JF
255 ASSERT_EFI_ERROR (Status);\r
256\r
257 RegisterTable[Index].InitialApicId = (UINT32)ProcessorInfoBuffer.ProcessorId;\r
258 RegisterTable[Index].TableLength = 0;\r
259 RegisterTable[Index].AllocatedSize = 0;\r
260 RegisterTable[Index].RegisterTableEntry = 0;\r
261\r
262 RegisterTable[NumberOfCpus + Index].InitialApicId = (UINT32)ProcessorInfoBuffer.ProcessorId;\r
263 RegisterTable[NumberOfCpus + Index].TableLength = 0;\r
264 RegisterTable[NumberOfCpus + Index].AllocatedSize = 0;\r
265 RegisterTable[NumberOfCpus + Index].RegisterTableEntry = 0;\r
266 }\r
267 AcpiCpuData->RegisterTable = (EFI_PHYSICAL_ADDRESS)(UINTN)RegisterTable;\r
268 AcpiCpuData->PreSmmInitRegisterTable = (EFI_PHYSICAL_ADDRESS)(UINTN)(RegisterTable + NumberOfCpus);\r
bfec5efa 269 }\r
bfec5efa
MK
270\r
271 //\r
272 // Set PcdCpuS3DataAddress to the base address of the ACPI_CPU_DATA structure\r
273 //\r
274 Status = PcdSet64S (PcdCpuS3DataAddress, (UINT64)(UINTN)AcpiCpuData);\r
275 ASSERT_EFI_ERROR (Status);\r
276\r
277 //\r
278 // Register EFI_END_OF_DXE_EVENT_GROUP_GUID event.\r
65b24ada 279 // The notification function allocates StartupVector and saves MTRRs for ACPI_CPU_DATA\r
bfec5efa
MK
280 //\r
281 Status = gBS->CreateEventEx (\r
282 EVT_NOTIFY_SIGNAL,\r
283 TPL_CALLBACK,\r
65b24ada
JF
284 CpuS3DataOnEndOfDxe,\r
285 AcpiCpuData,\r
bfec5efa
MK
286 &gEfiEndOfDxeEventGroupGuid,\r
287 &Event\r
288 );\r
289 ASSERT_EFI_ERROR (Status);\r
290\r
291 return EFI_SUCCESS;\r
292}\r