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