]> git.proxmox.com Git - mirror_edk2.git/blame - UefiCpuPkg/PiSmmCpuDxeSmm/PiSmmCpuDxeSmm.c
UefiCpuPkg/MpLib.c: Set AP state after X2APIC mode enabled
[mirror_edk2.git] / UefiCpuPkg / PiSmmCpuDxeSmm / PiSmmCpuDxeSmm.c
CommitLineData
529a5a86
MK
1/** @file\r
2Agent Module to load other modules to deploy SMM Entry Vector for X86 CPU.\r
3\r
8491e302 4Copyright (c) 2009 - 2017, Intel Corporation. All rights reserved.<BR>\r
241f9149
LD
5Copyright (c) 2017, AMD Incorporated. All rights reserved.<BR>\r
6\r
529a5a86
MK
7This program and the accompanying materials\r
8are licensed and made available under the terms and conditions of the BSD License\r
9which accompanies this distribution. The full text of the license may be found at\r
10http://opensource.org/licenses/bsd-license.php\r
11\r
12THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
13WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
14\r
15**/\r
16\r
17#include "PiSmmCpuDxeSmm.h"\r
18\r
19//\r
20// SMM CPU Private Data structure that contains SMM Configuration Protocol\r
21// along its supporting fields.\r
22//\r
23SMM_CPU_PRIVATE_DATA mSmmCpuPrivateData = {\r
24 SMM_CPU_PRIVATE_DATA_SIGNATURE, // Signature\r
25 NULL, // SmmCpuHandle\r
26 NULL, // Pointer to ProcessorInfo array\r
27 NULL, // Pointer to Operation array\r
28 NULL, // Pointer to CpuSaveStateSize array\r
29 NULL, // Pointer to CpuSaveState array\r
30 { {0} }, // SmmReservedSmramRegion\r
31 {\r
32 SmmStartupThisAp, // SmmCoreEntryContext.SmmStartupThisAp\r
33 0, // SmmCoreEntryContext.CurrentlyExecutingCpu\r
34 0, // SmmCoreEntryContext.NumberOfCpus\r
35 NULL, // SmmCoreEntryContext.CpuSaveStateSize\r
36 NULL // SmmCoreEntryContext.CpuSaveState\r
37 },\r
38 NULL, // SmmCoreEntry\r
39 {\r
40 mSmmCpuPrivateData.SmmReservedSmramRegion, // SmmConfiguration.SmramReservedRegions\r
41 RegisterSmmEntry // SmmConfiguration.RegisterSmmEntry\r
42 },\r
43};\r
44\r
45CPU_HOT_PLUG_DATA mCpuHotPlugData = {\r
46 CPU_HOT_PLUG_DATA_REVISION_1, // Revision\r
47 0, // Array Length of SmBase and APIC ID\r
48 NULL, // Pointer to APIC ID array\r
49 NULL, // Pointer to SMBASE array\r
50 0, // Reserved\r
51 0, // SmrrBase\r
52 0 // SmrrSize\r
53};\r
54\r
55//\r
56// Global pointer used to access mSmmCpuPrivateData from outside and inside SMM\r
57//\r
58SMM_CPU_PRIVATE_DATA *gSmmCpuPrivate = &mSmmCpuPrivateData;\r
59\r
60//\r
61// SMM Relocation variables\r
62//\r
63volatile BOOLEAN *mRebased;\r
64volatile BOOLEAN mIsBsp;\r
65\r
66///\r
67/// Handle for the SMM CPU Protocol\r
68///\r
69EFI_HANDLE mSmmCpuHandle = NULL;\r
70\r
71///\r
72/// SMM CPU Protocol instance\r
73///\r
74EFI_SMM_CPU_PROTOCOL mSmmCpu = {\r
75 SmmReadSaveState,\r
76 SmmWriteSaveState\r
77};\r
78\r
79EFI_CPU_INTERRUPT_HANDLER mExternalVectorTable[EXCEPTION_VECTOR_NUMBER];\r
80\r
529a5a86
MK
81//\r
82// SMM stack information\r
83//\r
84UINTN mSmmStackArrayBase;\r
85UINTN mSmmStackArrayEnd;\r
86UINTN mSmmStackSize;\r
87\r
529a5a86
MK
88UINTN mMaxNumberOfCpus = 1;\r
89UINTN mNumberOfCpus = 1;\r
90\r
91//\r
92// SMM ready to lock flag\r
93//\r
94BOOLEAN mSmmReadyToLock = FALSE;\r
95\r
96//\r
97// Global used to cache PCD for SMM Code Access Check enable\r
98//\r
99BOOLEAN mSmmCodeAccessCheckEnable = FALSE;\r
100\r
241f9149
LD
101//\r
102// Global copy of the PcdPteMemoryEncryptionAddressOrMask\r
103//\r
104UINT64 mAddressEncMask = 0;\r
105\r
529a5a86
MK
106//\r
107// Spin lock used to serialize setting of SMM Code Access Check feature\r
108//\r
fe3a75bc 109SPIN_LOCK *mConfigSmmCodeAccessCheckLock = NULL;\r
529a5a86 110\r
7ed6f781
JF
111//\r
112// Saved SMM ranges information\r
113//\r
114EFI_SMRAM_DESCRIPTOR *mSmmCpuSmramRanges;\r
115UINTN mSmmCpuSmramRangeCount;\r
116\r
529a5a86
MK
117/**\r
118 Initialize IDT to setup exception handlers for SMM.\r
119\r
120**/\r
121VOID\r
122InitializeSmmIdt (\r
123 VOID\r
124 )\r
125{\r
126 EFI_STATUS Status;\r
127 BOOLEAN InterruptState;\r
128 IA32_DESCRIPTOR DxeIdtr;\r
717fb604
JY
129\r
130 //\r
131 // There are 32 (not 255) entries in it since only processor\r
132 // generated exceptions will be handled.\r
133 //\r
134 gcSmiIdtr.Limit = (sizeof(IA32_IDT_GATE_DESCRIPTOR) * 32) - 1;\r
135 //\r
136 // Allocate page aligned IDT, because it might be set as read only.\r
137 //\r
138 gcSmiIdtr.Base = (UINTN)AllocateCodePages (EFI_SIZE_TO_PAGES(gcSmiIdtr.Limit + 1));\r
139 ASSERT (gcSmiIdtr.Base != 0);\r
140 ZeroMem ((VOID *)gcSmiIdtr.Base, gcSmiIdtr.Limit + 1);\r
141\r
529a5a86
MK
142 //\r
143 // Disable Interrupt and save DXE IDT table\r
144 //\r
145 InterruptState = SaveAndDisableInterrupts ();\r
146 AsmReadIdtr (&DxeIdtr);\r
147 //\r
148 // Load SMM temporary IDT table\r
149 //\r
150 AsmWriteIdtr (&gcSmiIdtr);\r
151 //\r
152 // Setup SMM default exception handlers, SMM IDT table\r
153 // will be updated and saved in gcSmiIdtr\r
154 //\r
155 Status = InitializeCpuExceptionHandlers (NULL);\r
156 ASSERT_EFI_ERROR (Status);\r
157 //\r
158 // Restore DXE IDT table and CPU interrupt\r
159 //\r
160 AsmWriteIdtr ((IA32_DESCRIPTOR *) &DxeIdtr);\r
161 SetInterruptState (InterruptState);\r
162}\r
163\r
164/**\r
165 Search module name by input IP address and output it.\r
166\r
167 @param CallerIpAddress Caller instruction pointer.\r
168\r
169**/\r
170VOID\r
171DumpModuleInfoByIp (\r
172 IN UINTN CallerIpAddress\r
173 )\r
174{\r
175 UINTN Pe32Data;\r
529a5a86 176 VOID *PdbPointer;\r
529a5a86
MK
177\r
178 //\r
179 // Find Image Base\r
180 //\r
b8caae19 181 Pe32Data = PeCoffSerachImageBase (CallerIpAddress);\r
529a5a86 182 if (Pe32Data != 0) {\r
b8caae19 183 DEBUG ((DEBUG_ERROR, "It is invoked from the instruction before IP(0x%p)", (VOID *) CallerIpAddress));\r
529a5a86
MK
184 PdbPointer = PeCoffLoaderGetPdbPointer ((VOID *) Pe32Data);\r
185 if (PdbPointer != NULL) {\r
b8caae19 186 DEBUG ((DEBUG_ERROR, " in module (%a)\n", PdbPointer));\r
529a5a86
MK
187 }\r
188 }\r
189}\r
190\r
191/**\r
192 Read information from the CPU save state.\r
193\r
194 @param This EFI_SMM_CPU_PROTOCOL instance\r
195 @param Width The number of bytes to read from the CPU save state.\r
196 @param Register Specifies the CPU register to read form the save state.\r
197 @param CpuIndex Specifies the zero-based index of the CPU save state.\r
198 @param Buffer Upon return, this holds the CPU register value read from the save state.\r
199\r
200 @retval EFI_SUCCESS The register was read from Save State\r
201 @retval EFI_NOT_FOUND The register is not defined for the Save State of Processor\r
202 @retval EFI_INVALID_PARAMTER This or Buffer is NULL.\r
203\r
204**/\r
205EFI_STATUS\r
206EFIAPI\r
207SmmReadSaveState (\r
208 IN CONST EFI_SMM_CPU_PROTOCOL *This,\r
209 IN UINTN Width,\r
210 IN EFI_SMM_SAVE_STATE_REGISTER Register,\r
211 IN UINTN CpuIndex,\r
212 OUT VOID *Buffer\r
213 )\r
214{\r
215 EFI_STATUS Status;\r
216\r
217 //\r
218 // Retrieve pointer to the specified CPU's SMM Save State buffer\r
219 //\r
220 if ((CpuIndex >= gSmst->NumberOfCpus) || (Buffer == NULL)) {\r
221 return EFI_INVALID_PARAMETER;\r
222 }\r
223\r
224 //\r
225 // Check for special EFI_SMM_SAVE_STATE_REGISTER_PROCESSOR_ID\r
226 //\r
227 if (Register == EFI_SMM_SAVE_STATE_REGISTER_PROCESSOR_ID) {\r
228 //\r
229 // The pseudo-register only supports the 64-bit size specified by Width.\r
230 //\r
231 if (Width != sizeof (UINT64)) {\r
232 return EFI_INVALID_PARAMETER;\r
233 }\r
234 //\r
235 // If the processor is in SMM at the time the SMI occurred,\r
236 // the pseudo register value for EFI_SMM_SAVE_STATE_REGISTER_PROCESSOR_ID is returned in Buffer.\r
237 // Otherwise, EFI_NOT_FOUND is returned.\r
238 //\r
ed3d5ecb 239 if (*(mSmmMpSyncData->CpuData[CpuIndex].Present)) {\r
529a5a86
MK
240 *(UINT64 *)Buffer = gSmmCpuPrivate->ProcessorInfo[CpuIndex].ProcessorId;\r
241 return EFI_SUCCESS;\r
242 } else {\r
243 return EFI_NOT_FOUND;\r
244 }\r
245 }\r
246\r
ed3d5ecb 247 if (!(*(mSmmMpSyncData->CpuData[CpuIndex].Present))) {\r
529a5a86
MK
248 return EFI_INVALID_PARAMETER;\r
249 }\r
250\r
251 Status = SmmCpuFeaturesReadSaveStateRegister (CpuIndex, Register, Width, Buffer);\r
252 if (Status == EFI_UNSUPPORTED) {\r
253 Status = ReadSaveStateRegister (CpuIndex, Register, Width, Buffer);\r
254 }\r
255 return Status;\r
256}\r
257\r
258/**\r
259 Write data to the CPU save state.\r
260\r
261 @param This EFI_SMM_CPU_PROTOCOL instance\r
262 @param Width The number of bytes to read from the CPU save state.\r
263 @param Register Specifies the CPU register to write to the save state.\r
264 @param CpuIndex Specifies the zero-based index of the CPU save state\r
265 @param Buffer Upon entry, this holds the new CPU register value.\r
266\r
267 @retval EFI_SUCCESS The register was written from Save State\r
268 @retval EFI_NOT_FOUND The register is not defined for the Save State of Processor\r
269 @retval EFI_INVALID_PARAMTER ProcessorIndex or Width is not correct\r
270\r
271**/\r
272EFI_STATUS\r
273EFIAPI\r
274SmmWriteSaveState (\r
275 IN CONST EFI_SMM_CPU_PROTOCOL *This,\r
276 IN UINTN Width,\r
277 IN EFI_SMM_SAVE_STATE_REGISTER Register,\r
278 IN UINTN CpuIndex,\r
279 IN CONST VOID *Buffer\r
280 )\r
281{\r
282 EFI_STATUS Status;\r
283\r
284 //\r
285 // Retrieve pointer to the specified CPU's SMM Save State buffer\r
286 //\r
287 if ((CpuIndex >= gSmst->NumberOfCpus) || (Buffer == NULL)) {\r
288 return EFI_INVALID_PARAMETER;\r
289 }\r
290\r
291 //\r
292 // Writes to EFI_SMM_SAVE_STATE_REGISTER_PROCESSOR_ID are ignored\r
293 //\r
294 if (Register == EFI_SMM_SAVE_STATE_REGISTER_PROCESSOR_ID) {\r
295 return EFI_SUCCESS;\r
296 }\r
297\r
298 if (!mSmmMpSyncData->CpuData[CpuIndex].Present) {\r
299 return EFI_INVALID_PARAMETER;\r
300 }\r
301\r
302 Status = SmmCpuFeaturesWriteSaveStateRegister (CpuIndex, Register, Width, Buffer);\r
303 if (Status == EFI_UNSUPPORTED) {\r
304 Status = WriteSaveStateRegister (CpuIndex, Register, Width, Buffer);\r
305 }\r
306 return Status;\r
307}\r
308\r
309\r
310/**\r
311 C function for SMI handler. To change all processor's SMMBase Register.\r
312\r
313**/\r
314VOID\r
315EFIAPI\r
316SmmInitHandler (\r
317 VOID\r
318 )\r
319{\r
320 UINT32 ApicId;\r
321 UINTN Index;\r
322\r
323 //\r
324 // Update SMM IDT entries' code segment and load IDT\r
325 //\r
326 AsmWriteIdtr (&gcSmiIdtr);\r
327 ApicId = GetApicId ();\r
328\r
bb767506 329 ASSERT (mNumberOfCpus <= mMaxNumberOfCpus);\r
529a5a86
MK
330\r
331 for (Index = 0; Index < mNumberOfCpus; Index++) {\r
332 if (ApicId == (UINT32)gSmmCpuPrivate->ProcessorInfo[Index].ProcessorId) {\r
333 //\r
334 // Initialize SMM specific features on the currently executing CPU\r
335 //\r
336 SmmCpuFeaturesInitializeProcessor (\r
337 Index,\r
338 mIsBsp,\r
339 gSmmCpuPrivate->ProcessorInfo,\r
340 &mCpuHotPlugData\r
341 );\r
342\r
a46a4c90
JF
343 if (!mSmmS3Flag) {\r
344 //\r
345 // Check XD and BTS features on each processor on normal boot\r
346 //\r
51773d49 347 CheckFeatureSupported ();\r
a46a4c90
JF
348 }\r
349\r
529a5a86
MK
350 if (mIsBsp) {\r
351 //\r
352 // BSP rebase is already done above.\r
353 // Initialize private data during S3 resume\r
354 //\r
355 InitializeMpSyncData ();\r
356 }\r
357\r
358 //\r
359 // Hook return after RSM to set SMM re-based flag\r
360 //\r
361 SemaphoreHook (Index, &mRebased[Index]);\r
362\r
363 return;\r
364 }\r
365 }\r
366 ASSERT (FALSE);\r
367}\r
368\r
369/**\r
370 Relocate SmmBases for each processor.\r
371\r
372 Execute on first boot and all S3 resumes\r
373\r
374**/\r
375VOID\r
376EFIAPI\r
377SmmRelocateBases (\r
378 VOID\r
379 )\r
380{\r
381 UINT8 BakBuf[BACK_BUF_SIZE];\r
382 SMRAM_SAVE_STATE_MAP BakBuf2;\r
383 SMRAM_SAVE_STATE_MAP *CpuStatePtr;\r
384 UINT8 *U8Ptr;\r
385 UINT32 ApicId;\r
386 UINTN Index;\r
387 UINTN BspIndex;\r
388\r
389 //\r
390 // Make sure the reserved size is large enough for procedure SmmInitTemplate.\r
391 //\r
392 ASSERT (sizeof (BakBuf) >= gcSmmInitSize);\r
393\r
394 //\r
395 // Patch ASM code template with current CR0, CR3, and CR4 values\r
396 //\r
397 gSmmCr0 = (UINT32)AsmReadCr0 ();\r
398 gSmmCr3 = (UINT32)AsmReadCr3 ();\r
399 gSmmCr4 = (UINT32)AsmReadCr4 ();\r
400\r
401 //\r
402 // Patch GDTR for SMM base relocation\r
403 //\r
404 gcSmiInitGdtr.Base = gcSmiGdtr.Base;\r
405 gcSmiInitGdtr.Limit = gcSmiGdtr.Limit;\r
406\r
407 U8Ptr = (UINT8*)(UINTN)(SMM_DEFAULT_SMBASE + SMM_HANDLER_OFFSET);\r
408 CpuStatePtr = (SMRAM_SAVE_STATE_MAP *)(UINTN)(SMM_DEFAULT_SMBASE + SMRAM_SAVE_STATE_MAP_OFFSET);\r
409\r
410 //\r
411 // Backup original contents at address 0x38000\r
412 //\r
413 CopyMem (BakBuf, U8Ptr, sizeof (BakBuf));\r
414 CopyMem (&BakBuf2, CpuStatePtr, sizeof (BakBuf2));\r
415\r
416 //\r
417 // Load image for relocation\r
418 //\r
419 CopyMem (U8Ptr, gcSmmInitTemplate, gcSmmInitSize);\r
420\r
421 //\r
422 // Retrieve the local APIC ID of current processor\r
423 //\r
424 ApicId = GetApicId ();\r
425\r
426 //\r
427 // Relocate SM bases for all APs\r
428 // This is APs' 1st SMI - rebase will be done here, and APs' default SMI handler will be overridden by gcSmmInitTemplate\r
429 //\r
430 mIsBsp = FALSE;\r
431 BspIndex = (UINTN)-1;\r
432 for (Index = 0; Index < mNumberOfCpus; Index++) {\r
433 mRebased[Index] = FALSE;\r
434 if (ApicId != (UINT32)gSmmCpuPrivate->ProcessorInfo[Index].ProcessorId) {\r
435 SendSmiIpi ((UINT32)gSmmCpuPrivate->ProcessorInfo[Index].ProcessorId);\r
436 //\r
437 // Wait for this AP to finish its 1st SMI\r
438 //\r
439 while (!mRebased[Index]);\r
440 } else {\r
441 //\r
442 // BSP will be Relocated later\r
443 //\r
444 BspIndex = Index;\r
445 }\r
446 }\r
447\r
448 //\r
449 // Relocate BSP's SMM base\r
450 //\r
451 ASSERT (BspIndex != (UINTN)-1);\r
452 mIsBsp = TRUE;\r
453 SendSmiIpi (ApicId);\r
454 //\r
455 // Wait for the BSP to finish its 1st SMI\r
456 //\r
457 while (!mRebased[BspIndex]);\r
458\r
459 //\r
460 // Restore contents at address 0x38000\r
461 //\r
462 CopyMem (CpuStatePtr, &BakBuf2, sizeof (BakBuf2));\r
463 CopyMem (U8Ptr, BakBuf, sizeof (BakBuf));\r
464}\r
465\r
529a5a86
MK
466/**\r
467 SMM Ready To Lock event notification handler.\r
468\r
469 The CPU S3 data is copied to SMRAM for security and mSmmReadyToLock is set to\r
470 perform additional lock actions that must be performed from SMM on the next SMI.\r
471\r
472 @param[in] Protocol Points to the protocol's unique identifier.\r
473 @param[in] Interface Points to the interface instance.\r
474 @param[in] Handle The handle on which the interface was installed.\r
475\r
476 @retval EFI_SUCCESS Notification handler runs successfully.\r
477 **/\r
478EFI_STATUS\r
479EFIAPI\r
480SmmReadyToLockEventNotify (\r
481 IN CONST EFI_GUID *Protocol,\r
482 IN VOID *Interface,\r
483 IN EFI_HANDLE Handle\r
484 )\r
485{\r
0bdc9e75 486 GetAcpiCpuData ();\r
529a5a86 487\r
d2fc7711
JY
488 //\r
489 // Cache a copy of UEFI memory map before we start profiling feature.\r
490 //\r
491 GetUefiMemoryMap ();\r
492\r
529a5a86
MK
493 //\r
494 // Set SMM ready to lock flag and return\r
495 //\r
496 mSmmReadyToLock = TRUE;\r
497 return EFI_SUCCESS;\r
498}\r
499\r
500/**\r
501 The module Entry Point of the CPU SMM driver.\r
502\r
503 @param ImageHandle The firmware allocated handle for the EFI image.\r
504 @param SystemTable A pointer to the EFI System Table.\r
505\r
506 @retval EFI_SUCCESS The entry point is executed successfully.\r
507 @retval Other Some error occurs when executing this entry point.\r
508\r
509**/\r
510EFI_STATUS\r
511EFIAPI\r
512PiCpuSmmEntry (\r
513 IN EFI_HANDLE ImageHandle,\r
514 IN EFI_SYSTEM_TABLE *SystemTable\r
515 )\r
516{\r
517 EFI_STATUS Status;\r
518 EFI_MP_SERVICES_PROTOCOL *MpServices;\r
519 UINTN NumberOfEnabledProcessors;\r
520 UINTN Index;\r
521 VOID *Buffer;\r
ae82a30b
JY
522 UINTN BufferPages;\r
523 UINTN TileCodeSize;\r
524 UINTN TileDataSize;\r
529a5a86 525 UINTN TileSize;\r
529a5a86
MK
526 UINT8 *Stacks;\r
527 VOID *Registration;\r
528 UINT32 RegEax;\r
529 UINT32 RegEdx;\r
530 UINTN FamilyId;\r
531 UINTN ModelId;\r
532 UINT32 Cr3;\r
533\r
534 //\r
535 // Initialize Debug Agent to support source level debug in SMM code\r
536 //\r
537 InitializeDebugAgent (DEBUG_AGENT_INIT_SMM, NULL, NULL);\r
538\r
539 //\r
540 // Report the start of CPU SMM initialization.\r
541 //\r
542 REPORT_STATUS_CODE (\r
543 EFI_PROGRESS_CODE,\r
544 EFI_COMPUTING_UNIT_HOST_PROCESSOR | EFI_CU_HP_PC_SMM_INIT\r
545 );\r
546\r
547 //\r
548 // Fix segment address of the long-mode-switch jump\r
549 //\r
550 if (sizeof (UINTN) == sizeof (UINT64)) {\r
551 gSmmJmpAddr.Segment = LONG_MODE_CODE_SEGMENT;\r
552 }\r
553\r
554 //\r
555 // Find out SMRR Base and SMRR Size\r
556 //\r
557 FindSmramInfo (&mCpuHotPlugData.SmrrBase, &mCpuHotPlugData.SmrrSize);\r
558\r
559 //\r
560 // Get MP Services Protocol\r
561 //\r
562 Status = SystemTable->BootServices->LocateProtocol (&gEfiMpServiceProtocolGuid, NULL, (VOID **)&MpServices);\r
563 ASSERT_EFI_ERROR (Status);\r
564\r
565 //\r
566 // Use MP Services Protocol to retrieve the number of processors and number of enabled processors\r
567 //\r
568 Status = MpServices->GetNumberOfProcessors (MpServices, &mNumberOfCpus, &NumberOfEnabledProcessors);\r
569 ASSERT_EFI_ERROR (Status);\r
570 ASSERT (mNumberOfCpus <= PcdGet32 (PcdCpuMaxLogicalProcessorNumber));\r
571\r
572 //\r
573 // If support CPU hot plug, PcdCpuSmmEnableBspElection should be set to TRUE.\r
574 // A constant BSP index makes no sense because it may be hot removed.\r
575 //\r
576 DEBUG_CODE (\r
577 if (FeaturePcdGet (PcdCpuHotPlugSupport)) {\r
578\r
579 ASSERT (FeaturePcdGet (PcdCpuSmmEnableBspElection));\r
580 }\r
581 );\r
582\r
583 //\r
584 // Save the PcdCpuSmmCodeAccessCheckEnable value into a global variable.\r
585 //\r
586 mSmmCodeAccessCheckEnable = PcdGetBool (PcdCpuSmmCodeAccessCheckEnable);\r
587 DEBUG ((EFI_D_INFO, "PcdCpuSmmCodeAccessCheckEnable = %d\n", mSmmCodeAccessCheckEnable));\r
588\r
241f9149
LD
589 //\r
590 // Save the PcdPteMemoryEncryptionAddressOrMask value into a global variable.\r
591 // Make sure AddressEncMask is contained to smallest supported address field.\r
592 //\r
593 mAddressEncMask = PcdGet64 (PcdPteMemoryEncryptionAddressOrMask) & PAGING_1G_ADDRESS_MASK_64;\r
594 DEBUG ((EFI_D_INFO, "mAddressEncMask = 0x%lx\n", mAddressEncMask));\r
595\r
529a5a86
MK
596 //\r
597 // If support CPU hot plug, we need to allocate resources for possibly hot-added processors\r
598 //\r
599 if (FeaturePcdGet (PcdCpuHotPlugSupport)) {\r
600 mMaxNumberOfCpus = PcdGet32 (PcdCpuMaxLogicalProcessorNumber);\r
601 } else {\r
602 mMaxNumberOfCpus = mNumberOfCpus;\r
603 }\r
604 gSmmCpuPrivate->SmmCoreEntryContext.NumberOfCpus = mMaxNumberOfCpus;\r
605\r
606 //\r
607 // The CPU save state and code for the SMI entry point are tiled within an SMRAM\r
608 // allocated buffer. The minimum size of this buffer for a uniprocessor system\r
609 // is 32 KB, because the entry point is SMBASE + 32KB, and CPU save state area\r
610 // just below SMBASE + 64KB. If more than one CPU is present in the platform,\r
611 // then the SMI entry point and the CPU save state areas can be tiles to minimize\r
612 // the total amount SMRAM required for all the CPUs. The tile size can be computed\r
613 // by adding the // CPU save state size, any extra CPU specific context, and\r
614 // the size of code that must be placed at the SMI entry point to transfer\r
615 // control to a C function in the native SMM execution mode. This size is\r
616 // rounded up to the nearest power of 2 to give the tile size for a each CPU.\r
617 // The total amount of memory required is the maximum number of CPUs that\r
618 // platform supports times the tile size. The picture below shows the tiling,\r
619 // where m is the number of tiles that fit in 32KB.\r
620 //\r
621 // +-----------------------------+ <-- 2^n offset from Base of allocated buffer\r
622 // | CPU m+1 Save State |\r
623 // +-----------------------------+\r
624 // | CPU m+1 Extra Data |\r
625 // +-----------------------------+\r
626 // | Padding |\r
627 // +-----------------------------+\r
628 // | CPU 2m SMI Entry |\r
629 // +#############################+ <-- Base of allocated buffer + 64 KB\r
630 // | CPU m-1 Save State |\r
631 // +-----------------------------+\r
632 // | CPU m-1 Extra Data |\r
633 // +-----------------------------+\r
634 // | Padding |\r
635 // +-----------------------------+\r
636 // | CPU 2m-1 SMI Entry |\r
637 // +=============================+ <-- 2^n offset from Base of allocated buffer\r
638 // | . . . . . . . . . . . . |\r
639 // +=============================+ <-- 2^n offset from Base of allocated buffer\r
640 // | CPU 2 Save State |\r
641 // +-----------------------------+\r
642 // | CPU 2 Extra Data |\r
643 // +-----------------------------+\r
644 // | Padding |\r
645 // +-----------------------------+\r
646 // | CPU m+1 SMI Entry |\r
647 // +=============================+ <-- Base of allocated buffer + 32 KB\r
648 // | CPU 1 Save State |\r
649 // +-----------------------------+\r
650 // | CPU 1 Extra Data |\r
651 // +-----------------------------+\r
652 // | Padding |\r
653 // +-----------------------------+\r
654 // | CPU m SMI Entry |\r
655 // +#############################+ <-- Base of allocated buffer + 32 KB == CPU 0 SMBASE + 64 KB\r
656 // | CPU 0 Save State |\r
657 // +-----------------------------+\r
658 // | CPU 0 Extra Data |\r
659 // +-----------------------------+\r
660 // | Padding |\r
661 // +-----------------------------+\r
662 // | CPU m-1 SMI Entry |\r
663 // +=============================+ <-- 2^n offset from Base of allocated buffer\r
664 // | . . . . . . . . . . . . |\r
665 // +=============================+ <-- 2^n offset from Base of allocated buffer\r
666 // | Padding |\r
667 // +-----------------------------+\r
668 // | CPU 1 SMI Entry |\r
669 // +=============================+ <-- 2^n offset from Base of allocated buffer\r
670 // | Padding |\r
671 // +-----------------------------+\r
672 // | CPU 0 SMI Entry |\r
673 // +#############################+ <-- Base of allocated buffer == CPU 0 SMBASE + 32 KB\r
674 //\r
675\r
676 //\r
677 // Retrieve CPU Family\r
678 //\r
e9b3a6c9 679 AsmCpuid (CPUID_VERSION_INFO, &RegEax, NULL, NULL, NULL);\r
529a5a86
MK
680 FamilyId = (RegEax >> 8) & 0xf;\r
681 ModelId = (RegEax >> 4) & 0xf;\r
682 if (FamilyId == 0x06 || FamilyId == 0x0f) {\r
683 ModelId = ModelId | ((RegEax >> 12) & 0xf0);\r
684 }\r
685\r
e9b3a6c9
MK
686 RegEdx = 0;\r
687 AsmCpuid (CPUID_EXTENDED_FUNCTION, &RegEax, NULL, NULL, NULL);\r
688 if (RegEax >= CPUID_EXTENDED_CPU_SIG) {\r
689 AsmCpuid (CPUID_EXTENDED_CPU_SIG, NULL, NULL, NULL, &RegEdx);\r
690 }\r
529a5a86
MK
691 //\r
692 // Determine the mode of the CPU at the time an SMI occurs\r
693 // Intel(R) 64 and IA-32 Architectures Software Developer's Manual\r
694 // Volume 3C, Section 34.4.1.1\r
695 //\r
696 mSmmSaveStateRegisterLma = EFI_SMM_SAVE_STATE_REGISTER_LMA_32BIT;\r
697 if ((RegEdx & BIT29) != 0) {\r
698 mSmmSaveStateRegisterLma = EFI_SMM_SAVE_STATE_REGISTER_LMA_64BIT;\r
699 }\r
700 if (FamilyId == 0x06) {\r
701 if (ModelId == 0x17 || ModelId == 0x0f || ModelId == 0x1c) {\r
702 mSmmSaveStateRegisterLma = EFI_SMM_SAVE_STATE_REGISTER_LMA_64BIT;\r
703 }\r
704 }\r
705\r
706 //\r
707 // Compute tile size of buffer required to hold the CPU SMRAM Save State Map, extra CPU\r
f12367a0
MK
708 // specific context start starts at SMBASE + SMM_PSD_OFFSET, and the SMI entry point.\r
709 // This size is rounded up to nearest power of 2.\r
529a5a86 710 //\r
ae82a30b
JY
711 TileCodeSize = GetSmiHandlerSize ();\r
712 TileCodeSize = ALIGN_VALUE(TileCodeSize, SIZE_4KB);\r
f12367a0 713 TileDataSize = (SMRAM_SAVE_STATE_MAP_OFFSET - SMM_PSD_OFFSET) + sizeof (SMRAM_SAVE_STATE_MAP);\r
ae82a30b
JY
714 TileDataSize = ALIGN_VALUE(TileDataSize, SIZE_4KB);\r
715 TileSize = TileDataSize + TileCodeSize - 1;\r
529a5a86 716 TileSize = 2 * GetPowerOfTwo32 ((UINT32)TileSize);\r
ae82a30b 717 DEBUG ((EFI_D_INFO, "SMRAM TileSize = 0x%08x (0x%08x, 0x%08x)\n", TileSize, TileCodeSize, TileDataSize));\r
529a5a86
MK
718\r
719 //\r
f12367a0
MK
720 // If the TileSize is larger than space available for the SMI Handler of\r
721 // CPU[i], the extra CPU specific context of CPU[i+1], and the SMRAM Save\r
722 // State Map of CPU[i+1], then ASSERT(). If this ASSERT() is triggered, then\r
723 // the SMI Handler size must be reduced or the size of the extra CPU specific\r
724 // context must be reduced.\r
529a5a86
MK
725 //\r
726 ASSERT (TileSize <= (SMRAM_SAVE_STATE_MAP_OFFSET + sizeof (SMRAM_SAVE_STATE_MAP) - SMM_HANDLER_OFFSET));\r
727\r
728 //\r
729 // Allocate buffer for all of the tiles.\r
730 //\r
731 // Intel(R) 64 and IA-32 Architectures Software Developer's Manual\r
732 // Volume 3C, Section 34.11 SMBASE Relocation\r
733 // For Pentium and Intel486 processors, the SMBASE values must be\r
734 // aligned on a 32-KByte boundary or the processor will enter shutdown\r
735 // state during the execution of a RSM instruction.\r
736 //\r
737 // Intel486 processors: FamilyId is 4\r
738 // Pentium processors : FamilyId is 5\r
739 //\r
ae82a30b 740 BufferPages = EFI_SIZE_TO_PAGES (SIZE_32KB + TileSize * (mMaxNumberOfCpus - 1));\r
529a5a86 741 if ((FamilyId == 4) || (FamilyId == 5)) {\r
717fb604 742 Buffer = AllocateAlignedCodePages (BufferPages, SIZE_32KB);\r
529a5a86 743 } else {\r
717fb604 744 Buffer = AllocateAlignedCodePages (BufferPages, SIZE_4KB);\r
529a5a86
MK
745 }\r
746 ASSERT (Buffer != NULL);\r
ae82a30b 747 DEBUG ((EFI_D_INFO, "SMRAM SaveState Buffer (0x%08x, 0x%08x)\n", Buffer, EFI_PAGES_TO_SIZE(BufferPages)));\r
529a5a86
MK
748\r
749 //\r
750 // Allocate buffer for pointers to array in SMM_CPU_PRIVATE_DATA.\r
751 //\r
752 gSmmCpuPrivate->ProcessorInfo = (EFI_PROCESSOR_INFORMATION *)AllocatePool (sizeof (EFI_PROCESSOR_INFORMATION) * mMaxNumberOfCpus);\r
753 ASSERT (gSmmCpuPrivate->ProcessorInfo != NULL);\r
754\r
755 gSmmCpuPrivate->Operation = (SMM_CPU_OPERATION *)AllocatePool (sizeof (SMM_CPU_OPERATION) * mMaxNumberOfCpus);\r
756 ASSERT (gSmmCpuPrivate->Operation != NULL);\r
757\r
758 gSmmCpuPrivate->CpuSaveStateSize = (UINTN *)AllocatePool (sizeof (UINTN) * mMaxNumberOfCpus);\r
759 ASSERT (gSmmCpuPrivate->CpuSaveStateSize != NULL);\r
760\r
761 gSmmCpuPrivate->CpuSaveState = (VOID **)AllocatePool (sizeof (VOID *) * mMaxNumberOfCpus);\r
762 ASSERT (gSmmCpuPrivate->CpuSaveState != NULL);\r
763\r
764 mSmmCpuPrivateData.SmmCoreEntryContext.CpuSaveStateSize = gSmmCpuPrivate->CpuSaveStateSize;\r
765 mSmmCpuPrivateData.SmmCoreEntryContext.CpuSaveState = gSmmCpuPrivate->CpuSaveState;\r
529a5a86
MK
766\r
767 //\r
768 // Allocate buffer for pointers to array in CPU_HOT_PLUG_DATA.\r
769 //\r
770 mCpuHotPlugData.ApicId = (UINT64 *)AllocatePool (sizeof (UINT64) * mMaxNumberOfCpus);\r
771 ASSERT (mCpuHotPlugData.ApicId != NULL);\r
772 mCpuHotPlugData.SmBase = (UINTN *)AllocatePool (sizeof (UINTN) * mMaxNumberOfCpus);\r
773 ASSERT (mCpuHotPlugData.SmBase != NULL);\r
774 mCpuHotPlugData.ArrayLength = (UINT32)mMaxNumberOfCpus;\r
775\r
776 //\r
777 // Retrieve APIC ID of each enabled processor from the MP Services protocol.\r
778 // Also compute the SMBASE address, CPU Save State address, and CPU Save state\r
779 // size for each CPU in the platform\r
780 //\r
781 for (Index = 0; Index < mMaxNumberOfCpus; Index++) {\r
782 mCpuHotPlugData.SmBase[Index] = (UINTN)Buffer + Index * TileSize - SMM_HANDLER_OFFSET;\r
783 gSmmCpuPrivate->CpuSaveStateSize[Index] = sizeof(SMRAM_SAVE_STATE_MAP);\r
784 gSmmCpuPrivate->CpuSaveState[Index] = (VOID *)(mCpuHotPlugData.SmBase[Index] + SMRAM_SAVE_STATE_MAP_OFFSET);\r
785 gSmmCpuPrivate->Operation[Index] = SmmCpuNone;\r
786\r
787 if (Index < mNumberOfCpus) {\r
788 Status = MpServices->GetProcessorInfo (MpServices, Index, &gSmmCpuPrivate->ProcessorInfo[Index]);\r
789 ASSERT_EFI_ERROR (Status);\r
790 mCpuHotPlugData.ApicId[Index] = gSmmCpuPrivate->ProcessorInfo[Index].ProcessorId;\r
791\r
792 DEBUG ((EFI_D_INFO, "CPU[%03x] APIC ID=%04x SMBASE=%08x SaveState=%08x Size=%08x\n",\r
793 Index,\r
794 (UINT32)gSmmCpuPrivate->ProcessorInfo[Index].ProcessorId,\r
795 mCpuHotPlugData.SmBase[Index],\r
796 gSmmCpuPrivate->CpuSaveState[Index],\r
797 gSmmCpuPrivate->CpuSaveStateSize[Index]\r
798 ));\r
799 } else {\r
800 gSmmCpuPrivate->ProcessorInfo[Index].ProcessorId = INVALID_APIC_ID;\r
801 mCpuHotPlugData.ApicId[Index] = INVALID_APIC_ID;\r
802 }\r
803 }\r
804\r
805 //\r
806 // Allocate SMI stacks for all processors.\r
807 //\r
808 if (FeaturePcdGet (PcdCpuSmmStackGuard)) {\r
809 //\r
810 // 2 more pages is allocated for each processor.\r
811 // one is guard page and the other is known good stack.\r
812 //\r
813 // +-------------------------------------------+-----+-------------------------------------------+\r
814 // | Known Good Stack | Guard Page | SMM Stack | ... | Known Good Stack | Guard Page | SMM Stack |\r
815 // +-------------------------------------------+-----+-------------------------------------------+\r
816 // | | | |\r
817 // |<-------------- Processor 0 -------------->| |<-------------- Processor n -------------->|\r
818 //\r
819 mSmmStackSize = EFI_PAGES_TO_SIZE (EFI_SIZE_TO_PAGES (PcdGet32 (PcdCpuSmmStackSize)) + 2);\r
820 Stacks = (UINT8 *) AllocatePages (gSmmCpuPrivate->SmmCoreEntryContext.NumberOfCpus * (EFI_SIZE_TO_PAGES (PcdGet32 (PcdCpuSmmStackSize)) + 2));\r
821 ASSERT (Stacks != NULL);\r
822 mSmmStackArrayBase = (UINTN)Stacks;\r
823 mSmmStackArrayEnd = mSmmStackArrayBase + gSmmCpuPrivate->SmmCoreEntryContext.NumberOfCpus * mSmmStackSize - 1;\r
824 } else {\r
825 mSmmStackSize = PcdGet32 (PcdCpuSmmStackSize);\r
826 Stacks = (UINT8 *) AllocatePages (EFI_SIZE_TO_PAGES (gSmmCpuPrivate->SmmCoreEntryContext.NumberOfCpus * mSmmStackSize));\r
827 ASSERT (Stacks != NULL);\r
828 }\r
829\r
830 //\r
831 // Set SMI stack for SMM base relocation\r
832 //\r
833 gSmmInitStack = (UINTN) (Stacks + mSmmStackSize - sizeof (UINTN));\r
834\r
835 //\r
836 // Initialize IDT\r
837 //\r
838 InitializeSmmIdt ();\r
839\r
840 //\r
841 // Relocate SMM Base addresses to the ones allocated from SMRAM\r
842 //\r
843 mRebased = (BOOLEAN *)AllocateZeroPool (sizeof (BOOLEAN) * mMaxNumberOfCpus);\r
844 ASSERT (mRebased != NULL);\r
845 SmmRelocateBases ();\r
846\r
847 //\r
848 // Call hook for BSP to perform extra actions in normal mode after all\r
849 // SMM base addresses have been relocated on all CPUs\r
850 //\r
851 SmmCpuFeaturesSmmRelocationComplete ();\r
852\r
717fb604
JY
853 DEBUG ((DEBUG_INFO, "mXdSupported - 0x%x\n", mXdSupported));\r
854\r
529a5a86
MK
855 //\r
856 // SMM Time initialization\r
857 //\r
858 InitializeSmmTimer ();\r
859\r
860 //\r
861 // Initialize MP globals\r
862 //\r
863 Cr3 = InitializeMpServiceData (Stacks, mSmmStackSize);\r
864\r
865 //\r
866 // Fill in SMM Reserved Regions\r
867 //\r
868 gSmmCpuPrivate->SmmReservedSmramRegion[0].SmramReservedStart = 0;\r
869 gSmmCpuPrivate->SmmReservedSmramRegion[0].SmramReservedSize = 0;\r
870\r
871 //\r
872 // Install the SMM Configuration Protocol onto a new handle on the handle database.\r
873 // The entire SMM Configuration Protocol is allocated from SMRAM, so only a pointer\r
874 // to an SMRAM address will be present in the handle database\r
875 //\r
876 Status = SystemTable->BootServices->InstallMultipleProtocolInterfaces (\r
877 &gSmmCpuPrivate->SmmCpuHandle,\r
878 &gEfiSmmConfigurationProtocolGuid, &gSmmCpuPrivate->SmmConfiguration,\r
879 NULL\r
880 );\r
881 ASSERT_EFI_ERROR (Status);\r
882\r
883 //\r
884 // Install the SMM CPU Protocol into SMM protocol database\r
885 //\r
886 Status = gSmst->SmmInstallProtocolInterface (\r
887 &mSmmCpuHandle,\r
888 &gEfiSmmCpuProtocolGuid,\r
889 EFI_NATIVE_INTERFACE,\r
890 &mSmmCpu\r
891 );\r
892 ASSERT_EFI_ERROR (Status);\r
893\r
894 //\r
895 // Expose address of CPU Hot Plug Data structure if CPU hot plug is supported.\r
896 //\r
897 if (FeaturePcdGet (PcdCpuHotPlugSupport)) {\r
9838b016
MK
898 Status = PcdSet64S (PcdCpuHotPlugDataAddress, (UINT64)(UINTN)&mCpuHotPlugData);\r
899 ASSERT_EFI_ERROR (Status);\r
529a5a86
MK
900 }\r
901\r
902 //\r
903 // Initialize SMM CPU Services Support\r
904 //\r
905 Status = InitializeSmmCpuServices (mSmmCpuHandle);\r
906 ASSERT_EFI_ERROR (Status);\r
907\r
529a5a86
MK
908 //\r
909 // register SMM Ready To Lock Protocol notification\r
910 //\r
911 Status = gSmst->SmmRegisterProtocolNotify (\r
912 &gEfiSmmReadyToLockProtocolGuid,\r
913 SmmReadyToLockEventNotify,\r
914 &Registration\r
915 );\r
916 ASSERT_EFI_ERROR (Status);\r
917\r
529a5a86
MK
918 //\r
919 // Initialize SMM Profile feature\r
920 //\r
921 InitSmmProfile (Cr3);\r
922\r
b10d5ddc 923 GetAcpiS3EnableFlag ();\r
0bdc9e75 924 InitSmmS3ResumeState (Cr3);\r
529a5a86
MK
925\r
926 DEBUG ((EFI_D_INFO, "SMM CPU Module exit from SMRAM with EFI_SUCCESS\n"));\r
927\r
928 return EFI_SUCCESS;\r
929}\r
930\r
931/**\r
932\r
933 Find out SMRAM information including SMRR base and SMRR size.\r
934\r
935 @param SmrrBase SMRR base\r
936 @param SmrrSize SMRR size\r
937\r
938**/\r
939VOID\r
940FindSmramInfo (\r
941 OUT UINT32 *SmrrBase,\r
942 OUT UINT32 *SmrrSize\r
943 )\r
944{\r
945 EFI_STATUS Status;\r
946 UINTN Size;\r
947 EFI_SMM_ACCESS2_PROTOCOL *SmmAccess;\r
948 EFI_SMRAM_DESCRIPTOR *CurrentSmramRange;\r
529a5a86
MK
949 UINTN Index;\r
950 UINT64 MaxSize;\r
951 BOOLEAN Found;\r
952\r
953 //\r
954 // Get SMM Access Protocol\r
955 //\r
956 Status = gBS->LocateProtocol (&gEfiSmmAccess2ProtocolGuid, NULL, (VOID **)&SmmAccess);\r
957 ASSERT_EFI_ERROR (Status);\r
958\r
959 //\r
960 // Get SMRAM information\r
961 //\r
962 Size = 0;\r
963 Status = SmmAccess->GetCapabilities (SmmAccess, &Size, NULL);\r
964 ASSERT (Status == EFI_BUFFER_TOO_SMALL);\r
965\r
7ed6f781
JF
966 mSmmCpuSmramRanges = (EFI_SMRAM_DESCRIPTOR *)AllocatePool (Size);\r
967 ASSERT (mSmmCpuSmramRanges != NULL);\r
529a5a86 968\r
7ed6f781 969 Status = SmmAccess->GetCapabilities (SmmAccess, &Size, mSmmCpuSmramRanges);\r
529a5a86
MK
970 ASSERT_EFI_ERROR (Status);\r
971\r
7ed6f781 972 mSmmCpuSmramRangeCount = Size / sizeof (EFI_SMRAM_DESCRIPTOR);\r
529a5a86
MK
973\r
974 //\r
975 // Find the largest SMRAM range between 1MB and 4GB that is at least 256K - 4K in size\r
976 //\r
977 CurrentSmramRange = NULL;\r
7ed6f781 978 for (Index = 0, MaxSize = SIZE_256KB - EFI_PAGE_SIZE; Index < mSmmCpuSmramRangeCount; Index++) {\r
529a5a86
MK
979 //\r
980 // Skip any SMRAM region that is already allocated, needs testing, or needs ECC initialization\r
981 //\r
7ed6f781 982 if ((mSmmCpuSmramRanges[Index].RegionState & (EFI_ALLOCATED | EFI_NEEDS_TESTING | EFI_NEEDS_ECC_INITIALIZATION)) != 0) {\r
529a5a86
MK
983 continue;\r
984 }\r
985\r
7ed6f781
JF
986 if (mSmmCpuSmramRanges[Index].CpuStart >= BASE_1MB) {\r
987 if ((mSmmCpuSmramRanges[Index].CpuStart + mSmmCpuSmramRanges[Index].PhysicalSize) <= SMRR_MAX_ADDRESS) {\r
988 if (mSmmCpuSmramRanges[Index].PhysicalSize >= MaxSize) {\r
989 MaxSize = mSmmCpuSmramRanges[Index].PhysicalSize;\r
990 CurrentSmramRange = &mSmmCpuSmramRanges[Index];\r
529a5a86
MK
991 }\r
992 }\r
993 }\r
994 }\r
995\r
996 ASSERT (CurrentSmramRange != NULL);\r
997\r
998 *SmrrBase = (UINT32)CurrentSmramRange->CpuStart;\r
999 *SmrrSize = (UINT32)CurrentSmramRange->PhysicalSize;\r
1000\r
1001 do {\r
1002 Found = FALSE;\r
7ed6f781
JF
1003 for (Index = 0; Index < mSmmCpuSmramRangeCount; Index++) {\r
1004 if (mSmmCpuSmramRanges[Index].CpuStart < *SmrrBase &&\r
1005 *SmrrBase == (mSmmCpuSmramRanges[Index].CpuStart + mSmmCpuSmramRanges[Index].PhysicalSize)) {\r
1006 *SmrrBase = (UINT32)mSmmCpuSmramRanges[Index].CpuStart;\r
1007 *SmrrSize = (UINT32)(*SmrrSize + mSmmCpuSmramRanges[Index].PhysicalSize);\r
529a5a86 1008 Found = TRUE;\r
7ed6f781
JF
1009 } else if ((*SmrrBase + *SmrrSize) == mSmmCpuSmramRanges[Index].CpuStart && mSmmCpuSmramRanges[Index].PhysicalSize > 0) {\r
1010 *SmrrSize = (UINT32)(*SmrrSize + mSmmCpuSmramRanges[Index].PhysicalSize);\r
529a5a86
MK
1011 Found = TRUE;\r
1012 }\r
1013 }\r
1014 } while (Found);\r
1015\r
1016 DEBUG ((EFI_D_INFO, "SMRR Base: 0x%x, SMRR Size: 0x%x\n", *SmrrBase, *SmrrSize));\r
1017}\r
1018\r
1019/**\r
1020Configure SMM Code Access Check feature on an AP.\r
1021SMM Feature Control MSR will be locked after configuration.\r
1022\r
1023@param[in,out] Buffer Pointer to private data buffer.\r
1024**/\r
1025VOID\r
1026EFIAPI\r
1027ConfigSmmCodeAccessCheckOnCurrentProcessor (\r
1028 IN OUT VOID *Buffer\r
1029 )\r
1030{\r
1031 UINTN CpuIndex;\r
1032 UINT64 SmmFeatureControlMsr;\r
1033 UINT64 NewSmmFeatureControlMsr;\r
1034\r
1035 //\r
1036 // Retrieve the CPU Index from the context passed in\r
1037 //\r
1038 CpuIndex = *(UINTN *)Buffer;\r
1039\r
1040 //\r
1041 // Get the current SMM Feature Control MSR value\r
1042 //\r
1043 SmmFeatureControlMsr = SmmCpuFeaturesGetSmmRegister (CpuIndex, SmmRegFeatureControl);\r
1044\r
1045 //\r
1046 // Compute the new SMM Feature Control MSR value\r
1047 //\r
1048 NewSmmFeatureControlMsr = SmmFeatureControlMsr;\r
1049 if (mSmmCodeAccessCheckEnable) {\r
1050 NewSmmFeatureControlMsr |= SMM_CODE_CHK_EN_BIT;\r
f6bc3a6d
JF
1051 if (FeaturePcdGet (PcdCpuSmmFeatureControlMsrLock)) {\r
1052 NewSmmFeatureControlMsr |= SMM_FEATURE_CONTROL_LOCK_BIT;\r
1053 }\r
529a5a86
MK
1054 }\r
1055\r
1056 //\r
1057 // Only set the SMM Feature Control MSR value if the new value is different than the current value\r
1058 //\r
1059 if (NewSmmFeatureControlMsr != SmmFeatureControlMsr) {\r
1060 SmmCpuFeaturesSetSmmRegister (CpuIndex, SmmRegFeatureControl, NewSmmFeatureControlMsr);\r
1061 }\r
1062\r
1063 //\r
1064 // Release the spin lock user to serialize the updates to the SMM Feature Control MSR\r
1065 //\r
fe3a75bc 1066 ReleaseSpinLock (mConfigSmmCodeAccessCheckLock);\r
529a5a86
MK
1067}\r
1068\r
1069/**\r
1070Configure SMM Code Access Check feature for all processors.\r
1071SMM Feature Control MSR will be locked after configuration.\r
1072**/\r
1073VOID\r
1074ConfigSmmCodeAccessCheck (\r
1075 VOID\r
1076 )\r
1077{\r
1078 UINTN Index;\r
1079 EFI_STATUS Status;\r
1080\r
1081 //\r
1082 // Check to see if the Feature Control MSR is supported on this CPU\r
1083 //\r
f6b0cb17 1084 Index = gSmmCpuPrivate->SmmCoreEntryContext.CurrentlyExecutingCpu;\r
529a5a86
MK
1085 if (!SmmCpuFeaturesIsSmmRegisterSupported (Index, SmmRegFeatureControl)) {\r
1086 mSmmCodeAccessCheckEnable = FALSE;\r
1087 return;\r
1088 }\r
1089\r
1090 //\r
1091 // Check to see if the CPU supports the SMM Code Access Check feature\r
1092 // Do not access this MSR unless the CPU supports the SmmRegFeatureControl\r
1093 //\r
1094 if ((AsmReadMsr64 (EFI_MSR_SMM_MCA_CAP) & SMM_CODE_ACCESS_CHK_BIT) == 0) {\r
1095 mSmmCodeAccessCheckEnable = FALSE;\r
529a5a86
MK
1096 return;\r
1097 }\r
1098\r
1099 //\r
1100 // Initialize the lock used to serialize the MSR programming in BSP and all APs\r
1101 //\r
fe3a75bc 1102 InitializeSpinLock (mConfigSmmCodeAccessCheckLock);\r
529a5a86
MK
1103\r
1104 //\r
1105 // Acquire Config SMM Code Access Check spin lock. The BSP will release the\r
1106 // spin lock when it is done executing ConfigSmmCodeAccessCheckOnCurrentProcessor().\r
1107 //\r
fe3a75bc 1108 AcquireSpinLock (mConfigSmmCodeAccessCheckLock);\r
529a5a86
MK
1109\r
1110 //\r
1111 // Enable SMM Code Access Check feature on the BSP.\r
1112 //\r
1113 ConfigSmmCodeAccessCheckOnCurrentProcessor (&Index);\r
1114\r
1115 //\r
1116 // Enable SMM Code Access Check feature for the APs.\r
1117 //\r
1118 for (Index = 0; Index < gSmst->NumberOfCpus; Index++) {\r
f6b0cb17 1119 if (Index != gSmmCpuPrivate->SmmCoreEntryContext.CurrentlyExecutingCpu) {\r
529a5a86
MK
1120\r
1121 //\r
1122 // Acquire Config SMM Code Access Check spin lock. The AP will release the\r
1123 // spin lock when it is done executing ConfigSmmCodeAccessCheckOnCurrentProcessor().\r
1124 //\r
fe3a75bc 1125 AcquireSpinLock (mConfigSmmCodeAccessCheckLock);\r
529a5a86
MK
1126\r
1127 //\r
1128 // Call SmmStartupThisAp() to enable SMM Code Access Check on an AP.\r
1129 //\r
1130 Status = gSmst->SmmStartupThisAp (ConfigSmmCodeAccessCheckOnCurrentProcessor, Index, &Index);\r
1131 ASSERT_EFI_ERROR (Status);\r
1132\r
1133 //\r
1134 // Wait for the AP to release the Config SMM Code Access Check spin lock.\r
1135 //\r
fe3a75bc 1136 while (!AcquireSpinLockOrFail (mConfigSmmCodeAccessCheckLock)) {\r
529a5a86
MK
1137 CpuPause ();\r
1138 }\r
1139\r
1140 //\r
1141 // Release the Config SMM Code Access Check spin lock.\r
1142 //\r
fe3a75bc 1143 ReleaseSpinLock (mConfigSmmCodeAccessCheckLock);\r
529a5a86
MK
1144 }\r
1145 }\r
1146}\r
1147\r
21c17193
JY
1148/**\r
1149 This API provides a way to allocate memory for page table.\r
1150\r
1151 This API can be called more once to allocate memory for page tables.\r
1152\r
1153 Allocates the number of 4KB pages of type EfiRuntimeServicesData and returns a pointer to the\r
1154 allocated buffer. The buffer returned is aligned on a 4KB boundary. If Pages is 0, then NULL\r
1155 is returned. If there is not enough memory remaining to satisfy the request, then NULL is\r
1156 returned.\r
1157\r
1158 @param Pages The number of 4 KB pages to allocate.\r
1159\r
1160 @return A pointer to the allocated buffer or NULL if allocation fails.\r
1161\r
1162**/\r
1163VOID *\r
1164AllocatePageTableMemory (\r
1165 IN UINTN Pages\r
1166 )\r
1167{\r
1168 VOID *Buffer;\r
1169\r
1170 Buffer = SmmCpuFeaturesAllocatePageTableMemory (Pages);\r
1171 if (Buffer != NULL) {\r
1172 return Buffer;\r
1173 }\r
1174 return AllocatePages (Pages);\r
1175}\r
1176\r
717fb604
JY
1177/**\r
1178 Allocate pages for code.\r
1179\r
1180 @param[in] Pages Number of pages to be allocated.\r
1181\r
1182 @return Allocated memory.\r
1183**/\r
1184VOID *\r
1185AllocateCodePages (\r
1186 IN UINTN Pages\r
1187 )\r
1188{\r
1189 EFI_STATUS Status;\r
1190 EFI_PHYSICAL_ADDRESS Memory;\r
1191\r
1192 if (Pages == 0) {\r
1193 return NULL;\r
1194 }\r
1195\r
1196 Status = gSmst->SmmAllocatePages (AllocateAnyPages, EfiRuntimeServicesCode, Pages, &Memory);\r
1197 if (EFI_ERROR (Status)) {\r
1198 return NULL;\r
1199 }\r
1200 return (VOID *) (UINTN) Memory;\r
1201}\r
1202\r
1203/**\r
1204 Allocate aligned pages for code.\r
1205\r
1206 @param[in] Pages Number of pages to be allocated.\r
1207 @param[in] Alignment The requested alignment of the allocation.\r
1208 Must be a power of two.\r
1209 If Alignment is zero, then byte alignment is used.\r
1210\r
1211 @return Allocated memory.\r
1212**/\r
1213VOID *\r
1214AllocateAlignedCodePages (\r
1215 IN UINTN Pages,\r
1216 IN UINTN Alignment\r
1217 )\r
1218{\r
1219 EFI_STATUS Status;\r
1220 EFI_PHYSICAL_ADDRESS Memory;\r
1221 UINTN AlignedMemory;\r
1222 UINTN AlignmentMask;\r
1223 UINTN UnalignedPages;\r
1224 UINTN RealPages;\r
1225\r
1226 //\r
1227 // Alignment must be a power of two or zero.\r
1228 //\r
1229 ASSERT ((Alignment & (Alignment - 1)) == 0);\r
1230\r
1231 if (Pages == 0) {\r
1232 return NULL;\r
1233 }\r
1234 if (Alignment > EFI_PAGE_SIZE) {\r
1235 //\r
1236 // Calculate the total number of pages since alignment is larger than page size.\r
1237 //\r
1238 AlignmentMask = Alignment - 1;\r
1239 RealPages = Pages + EFI_SIZE_TO_PAGES (Alignment);\r
1240 //\r
1241 // Make sure that Pages plus EFI_SIZE_TO_PAGES (Alignment) does not overflow.\r
1242 //\r
1243 ASSERT (RealPages > Pages);\r
1244\r
1245 Status = gSmst->SmmAllocatePages (AllocateAnyPages, EfiRuntimeServicesCode, RealPages, &Memory);\r
1246 if (EFI_ERROR (Status)) {\r
1247 return NULL;\r
1248 }\r
1249 AlignedMemory = ((UINTN) Memory + AlignmentMask) & ~AlignmentMask;\r
1250 UnalignedPages = EFI_SIZE_TO_PAGES (AlignedMemory - (UINTN) Memory);\r
1251 if (UnalignedPages > 0) {\r
1252 //\r
1253 // Free first unaligned page(s).\r
1254 //\r
1255 Status = gSmst->SmmFreePages (Memory, UnalignedPages);\r
1256 ASSERT_EFI_ERROR (Status);\r
1257 }\r
8491e302 1258 Memory = AlignedMemory + EFI_PAGES_TO_SIZE (Pages);\r
717fb604
JY
1259 UnalignedPages = RealPages - Pages - UnalignedPages;\r
1260 if (UnalignedPages > 0) {\r
1261 //\r
1262 // Free last unaligned page(s).\r
1263 //\r
1264 Status = gSmst->SmmFreePages (Memory, UnalignedPages);\r
1265 ASSERT_EFI_ERROR (Status);\r
1266 }\r
1267 } else {\r
1268 //\r
1269 // Do not over-allocate pages in this case.\r
1270 //\r
1271 Status = gSmst->SmmAllocatePages (AllocateAnyPages, EfiRuntimeServicesCode, Pages, &Memory);\r
1272 if (EFI_ERROR (Status)) {\r
1273 return NULL;\r
1274 }\r
1275 AlignedMemory = (UINTN) Memory;\r
1276 }\r
1277 return (VOID *) AlignedMemory;\r
1278}\r
1279\r
529a5a86
MK
1280/**\r
1281 Perform the remaining tasks.\r
1282\r
1283**/\r
1284VOID\r
1285PerformRemainingTasks (\r
1286 VOID\r
1287 )\r
1288{\r
1289 if (mSmmReadyToLock) {\r
1290 //\r
1291 // Start SMM Profile feature\r
1292 //\r
1293 if (FeaturePcdGet (PcdCpuSmmProfileEnable)) {\r
1294 SmmProfileStart ();\r
1295 }\r
1296 //\r
1297 // Create a mix of 2MB and 4KB page table. Update some memory ranges absent and execute-disable.\r
1298 //\r
1299 InitPaging ();\r
717fb604
JY
1300\r
1301 //\r
1302 // Mark critical region to be read-only in page table\r
1303 //\r
d2fc7711
JY
1304 SetMemMapAttributes ();\r
1305\r
1306 //\r
1307 // For outside SMRAM, we only map SMM communication buffer or MMIO.\r
1308 //\r
1309 SetUefiMemMapAttributes ();\r
717fb604
JY
1310\r
1311 //\r
1312 // Set page table itself to be read-only\r
1313 //\r
1314 SetPageTableAttributes ();\r
1315\r
529a5a86
MK
1316 //\r
1317 // Configure SMM Code Access Check feature if available.\r
1318 //\r
1319 ConfigSmmCodeAccessCheck ();\r
1320\r
21c17193
JY
1321 SmmCpuFeaturesCompleteSmmReadyToLock ();\r
1322\r
529a5a86
MK
1323 //\r
1324 // Clean SMM ready to lock flag\r
1325 //\r
1326 mSmmReadyToLock = FALSE;\r
1327 }\r
1328}\r
9f419739
JY
1329\r
1330/**\r
1331 Perform the pre tasks.\r
1332\r
1333**/\r
1334VOID\r
1335PerformPreTasks (\r
1336 VOID\r
1337 )\r
1338{\r
0bdc9e75 1339 RestoreSmmConfigurationInS3 ();\r
9f419739 1340}\r