2 Common header file for MP Initialize Library.
4 Copyright (c) 2016 - 2023, Intel Corporation. All rights reserved.<BR>
5 Copyright (c) 2020, AMD Inc. All rights reserved.<BR>
7 SPDX-License-Identifier: BSD-2-Clause-Patent
16 #include <Register/Intel/Cpuid.h>
17 #include <Register/Amd/Cpuid.h>
18 #include <Register/Amd/Ghcb.h>
19 #include <Register/Intel/Msr.h>
20 #include <Register/Intel/LocalApic.h>
21 #include <Register/Intel/Microcode.h>
23 #include <Library/MpInitLib.h>
24 #include <Library/BaseLib.h>
25 #include <Library/BaseMemoryLib.h>
26 #include <Library/MemoryAllocationLib.h>
27 #include <Library/DebugLib.h>
28 #include <Library/LocalApicLib.h>
29 #include <Library/CpuLib.h>
30 #include <Library/UefiCpuLib.h>
31 #include <Library/TimerLib.h>
32 #include <Library/SynchronizationLib.h>
33 #include <Library/MtrrLib.h>
34 #include <Library/HobLib.h>
35 #include <Library/PcdLib.h>
36 #include <Library/MicrocodeLib.h>
37 #include <ConfidentialComputingGuestAttr.h>
39 #include <Register/Amd/Fam17Msr.h>
40 #include <Register/Amd/Ghcb.h>
42 #include <Guid/MicrocodePatchHob.h>
44 #define WAKEUP_AP_SIGNAL SIGNATURE_32 ('S', 'T', 'A', 'P')
46 #define CPU_INIT_MP_LIB_HOB_GUID \
48 0x58eb6a19, 0x3699, 0x4c68, { 0xa8, 0x36, 0xda, 0xcd, 0x8e, 0xdc, 0xad, 0x4a } \
52 // The MP data for switch BSP
54 #define CPU_SWITCH_STATE_IDLE 0
55 #define CPU_SWITCH_STATE_STORED 1
56 #define CPU_SWITCH_STATE_LOADED 2
59 // Default maximum number of entries to store the microcode patches information
61 #define DEFAULT_MAX_MICROCODE_PATCH_NUM 8
64 // Data structure for microcode patch information
69 } MICROCODE_PATCH_INFO
;
72 // CPU volatile registers around INIT-SIPI-SIPI
87 } CPU_VOLATILE_REGISTERS
;
90 // CPU exchange information for switch BSP
93 UINT8 State
; // offset 0
94 UINTN StackPointer
; // offset 4 / 8
95 CPU_VOLATILE_REGISTERS VolatileRegisters
; // offset 8 / 16
96 } CPU_EXCHANGE_ROLE_INFO
;
99 // AP loop state when APs are in idle state
100 // It's value is the same with PcdCpuApLoopMode
109 // AP initialization state during APs wakeup
120 // The state transitions for an AP when it process a procedure are:
121 // Idle ----> Ready ----> Busy ----> Idle
137 volatile UINT32
*StartupApSignal
;
138 volatile UINTN ApFunction
;
139 volatile UINTN ApFunctionArgument
;
141 volatile CPU_STATE State
;
142 CPU_VOLATILE_REGISTERS VolatileRegisters
;
149 UINT32 ProcessorSignature
;
151 UINT64 MicrocodeEntryAddr
;
152 UINT32 MicrocodeRevision
;
153 SEV_ES_SAVE_AREA
*SevEsSaveArea
;
157 // Basic CPU information saved in Guided HOB.
158 // Because the contents will be shard between PEI and DXE,
159 // we need to make sure the each fields offset same in different
164 UINT32 InitialApicId
;
172 // AP reset code information including code address and size,
173 // this structure will be shared be C code and assembly code.
174 // It is natural aligned by design.
177 UINT8
*RendezvousFunnelAddress
;
178 UINTN ModeEntryOffset
;
179 UINTN RendezvousFunnelSize
;
180 UINT8
*RelocateApLoopFuncAddress
;
181 UINTN RelocateApLoopFuncSize
;
182 UINTN ModeTransitionOffset
;
183 UINTN SwitchToRealNoNxOffset
;
184 UINTN SwitchToRealPM16ModeOffset
;
185 UINTN SwitchToRealPM16ModeSize
;
186 } MP_ASSEMBLY_ADDRESS_MAP
;
188 typedef struct _CPU_MP_DATA CPU_MP_DATA
;
193 // MP CPU exchange information for AP reset code
194 // This structure is required to be packed because fixed field offsets
195 // into this structure are used in assembly code in this module
201 IA32_DESCRIPTOR GdtrProfile
;
202 IA32_DESCRIPTOR IdtrProfile
;
208 UINTN EnableExecuteDisable
;
211 CPU_INFO_IN_HOB
*CpuInfo
;
212 UINTN NumApsExecuting
;
213 CPU_MP_DATA
*CpuMpData
;
214 UINTN InitializeFloatingPointUnitsAddress
;
215 UINT32 ModeTransitionMemory
;
216 UINT16 ModeTransitionSegment
;
217 UINT32 ModeHighMemory
;
218 UINT16 ModeHighSegment
;
220 // Enable5LevelPaging indicates whether 5-level paging is enabled in long mode.
222 BOOLEAN Enable5LevelPaging
;
223 BOOLEAN SevEsIsEnabled
;
224 BOOLEAN SevSnpIsEnabled
;
226 BOOLEAN ExtTopoAvail
;
227 } MP_CPU_EXCHANGE_INFO
;
232 // CPU MP Data save in memory
234 struct _CPU_MP_DATA
{
239 // The above fields data will be passed from PEI to DXE
240 // Please make sure the fields offset same in the different
245 UINTN CpuApStackSize
;
246 MP_ASSEMBLY_ADDRESS_MAP AddressMap
;
248 UINTN WakeupBufferHigh
;
250 UINTN BackupBufferSize
;
252 volatile UINT32 FinishedCount
;
254 BOOLEAN SingleThread
;
255 EFI_AP_PROCEDURE Procedure
;
262 UINTN
**FailedCpuList
;
264 AP_INIT_STATE InitFlag
;
265 BOOLEAN SwitchBspFlag
;
267 CPU_EXCHANGE_ROLE_INFO BSPInfo
;
268 CPU_EXCHANGE_ROLE_INFO APInfo
;
269 MTRR_SETTINGS MtrrTable
;
271 UINT8 ApTargetCState
;
272 UINT16 PmCodeSegment
;
273 UINT16 Pm16CodeSegment
;
274 CPU_AP_DATA
*CpuData
;
275 volatile MP_CPU_EXCHANGE_INFO
*MpCpuExchangeInfo
;
277 UINT32 CurrentTimerCount
;
280 BOOLEAN PeriodicMode
;
281 BOOLEAN TimerInterruptState
;
282 UINT64 MicrocodePatchAddress
;
283 UINT64 MicrocodePatchRegionSize
;
286 // Whether need to use Init-Sipi-Sipi to wake up the APs.
287 // Two cases need to set this value to TRUE. One is in HLT
288 // loop mode, the other is resume from S3 which loop mode
289 // will be hardcode change to HLT mode by PiSmmCpuDxeSmm
292 BOOLEAN WakeUpByInitSipiSipi
;
294 BOOLEAN SevEsIsEnabled
;
295 BOOLEAN SevSnpIsEnabled
;
296 BOOLEAN UseSevEsAPMethod
;
298 UINTN SevEsAPResetStackStart
;
299 CPU_MP_DATA
*NewCpuMpData
;
305 // AP_STACK_DATA is stored at the top of each AP stack.
312 #define AP_SAFE_STACK_SIZE 128
313 #define AP_RESET_STACK_SIZE AP_SAFE_STACK_SIZE
314 STATIC_ASSERT ((AP_SAFE_STACK_SIZE
& (CPU_STACK_ALIGNMENT
- 1)) == 0, "AP_SAFE_STACK_SIZE is not aligned with CPU_STACK_ALIGNMENT");
327 Assembly code to move an AP from long mode to real mode.
329 Move an AP from long mode to real mode in preparation to invoking
330 the reset vector. This is used for SEV-ES guests where a hypervisor
331 is not allowed to set the CS and RIP to point to the reset vector.
333 @param[in] BufferStart The reset vector target.
334 @param[in] Code16 16-bit protected mode code segment value.
335 @param[in] Code32 32-bit protected mode code segment value.
336 @param[in] StackStart The start of a stack to be used for transitioning
337 from long mode to real mode.
342 IN UINTN BufferStart
,
348 extern EFI_GUID mCpuInitMpLibHobGuid
;
351 Assembly code to place AP into safe loop mode.
353 Place AP into targeted C-State if MONITOR is supported, otherwise
354 place AP into hlt state.
355 Place AP in protected mode if the current is long mode. Due to AP maybe
356 wakeup by some hardware event. It could avoid accessing page table that
357 may not available during booting to OS.
359 @param[in] MwaitSupport TRUE indicates MONITOR is supported.
360 FALSE indicates MONITOR is not supported.
361 @param[in] ApTargetCState Target C-State value.
362 @param[in] PmCodeSegment Protected mode code segment value.
366 (EFIAPI
*ASM_RELOCATE_AP_LOOP
)(
367 IN BOOLEAN MwaitSupport
,
368 IN UINTN ApTargetCState
,
369 IN UINTN PmCodeSegment
,
370 IN UINTN TopOfApStack
,
371 IN UINTN NumberToFinish
,
372 IN UINTN Pm16CodeSegment
,
373 IN UINTN SevEsAPJumpTable
,
374 IN UINTN WakeupBuffer
378 Assembly code to get starting address and size of the rendezvous entry for APs.
379 Information for fixing a jump instruction in the code is also returned.
381 @param[out] AddressMap Output buffer for address map information.
386 OUT MP_ASSEMBLY_ADDRESS_MAP
*AddressMap
390 This function is called by both the BSP and the AP which is to become the BSP to
391 Exchange execution context including stack between them. After return from this
392 function, the BSP becomes AP and the AP becomes the BSP.
394 @param[in] MyInfo Pointer to buffer holding the exchanging information for the executing processor.
395 @param[in] OthersInfo Pointer to buffer holding the exchanging information for the peer.
401 IN CPU_EXCHANGE_ROLE_INFO
*MyInfo
,
402 IN CPU_EXCHANGE_ROLE_INFO
*OthersInfo
407 ASM_RELOCATE_AP_LOOP AmdSevEntry
; // 64-bit AMD Sev processors
408 ASM_RELOCATE_AP_LOOP GenericEntry
; // Intel processors (32-bit or 64-bit), 32-bit AMD processors, or AMD non-Sev processors
409 } RELOCATE_AP_LOOP_ENTRY
;
412 Get the pointer to CPU MP Data structure.
414 @return The pointer to CPU MP Data structure.
422 Save the pointer to CPU MP Data structure.
424 @param[in] CpuMpData The pointer to CPU MP Data structure will be saved.
428 IN CPU_MP_DATA
*CpuMpData
432 Get available system memory below 1MB by specified size.
434 @param[in] WakeupBufferSize Wakeup buffer size required
436 @retval other Return wakeup buffer address below 1MB.
437 @retval -1 Cannot find free memory below 1MB.
441 IN UINTN WakeupBufferSize
445 Get available EfiBootServicesCode memory below 4GB by specified size.
447 This buffer is required to safely transfer AP from real address mode to
448 protected mode or long mode, due to the fact that the buffer returned by
449 GetWakeupBuffer() may be marked as non-executable.
451 @param[in] BufferSize Wakeup transition buffer size.
453 @retval other Return wakeup transition buffer address below 4GB.
454 @retval 0 Cannot find free memory below 4GB.
462 Return the address of the SEV-ES AP jump table.
464 This buffer is required in order for an SEV-ES guest to transition from
467 @return Return SEV-ES AP jump table buffer
475 This function will be called by BSP to wakeup AP.
477 @param[in] CpuMpData Pointer to CPU MP Data
478 @param[in] Broadcast TRUE: Send broadcast IPI to all APs
479 FALSE: Send IPI to AP by ApicId
480 @param[in] ProcessorNumber The handle number of specified processor
481 @param[in] Procedure The function to be invoked by AP
482 @param[in] ProcedureArgument The argument to be passed into AP function
483 @param[in] WakeUpDisabledAps Whether need to wake up disabled APs in broadcast mode.
487 IN CPU_MP_DATA
*CpuMpData
,
488 IN BOOLEAN Broadcast
,
489 IN UINTN ProcessorNumber
,
490 IN EFI_AP_PROCEDURE Procedure OPTIONAL
,
491 IN VOID
*ProcedureArgument OPTIONAL
,
492 IN BOOLEAN WakeUpDisabledAps
496 Initialize global data for MP support.
498 @param[in] CpuMpData The pointer to CPU MP Data structure.
502 IN CPU_MP_DATA
*CpuMpData
506 Worker function to execute a caller provided function on all enabled APs.
508 @param[in] Procedure A pointer to the function to be run on
509 enabled APs of the system.
510 @param[in] SingleThread If TRUE, then all the enabled APs execute
511 the function specified by Procedure one by
512 one, in ascending order of processor handle
513 number. If FALSE, then all the enabled APs
514 execute the function specified by Procedure
516 @param[in] ExcludeBsp Whether let BSP also trig this task.
517 @param[in] WaitEvent The event created by the caller with CreateEvent()
519 @param[in] TimeoutInMicroseconds Indicates the time limit in microseconds for
520 APs to return from Procedure, either for
521 blocking or non-blocking mode.
522 @param[in] ProcedureArgument The parameter passed into Procedure for
524 @param[out] FailedCpuList If all APs finish successfully, then its
525 content is set to NULL. If not all APs
526 finish before timeout expires, then its
527 content is set to address of the buffer
528 holding handle numbers of the failed APs.
530 @retval EFI_SUCCESS In blocking mode, all APs have finished before
532 @retval EFI_SUCCESS In non-blocking mode, function has been dispatched
534 @retval others Failed to Startup all APs.
538 StartupAllCPUsWorker (
539 IN EFI_AP_PROCEDURE Procedure
,
540 IN BOOLEAN SingleThread
,
541 IN BOOLEAN ExcludeBsp
,
542 IN EFI_EVENT WaitEvent OPTIONAL
,
543 IN UINTN TimeoutInMicroseconds
,
544 IN VOID
*ProcedureArgument OPTIONAL
,
545 OUT UINTN
**FailedCpuList OPTIONAL
549 Worker function to let the caller get one enabled AP to execute a caller-provided
552 @param[in] Procedure A pointer to the function to be run on
553 enabled APs of the system.
554 @param[in] ProcessorNumber The handle number of the AP.
555 @param[in] WaitEvent The event created by the caller with CreateEvent()
557 @param[in] TimeoutInMicroseconds Indicates the time limit in microseconds for
558 APs to return from Procedure, either for
559 blocking or non-blocking mode.
560 @param[in] ProcedureArgument The parameter passed into Procedure for
562 @param[out] Finished If AP returns from Procedure before the
563 timeout expires, its content is set to TRUE.
564 Otherwise, the value is set to FALSE.
566 @retval EFI_SUCCESS In blocking mode, specified AP finished before
568 @retval others Failed to Startup AP.
572 StartupThisAPWorker (
573 IN EFI_AP_PROCEDURE Procedure
,
574 IN UINTN ProcessorNumber
,
575 IN EFI_EVENT WaitEvent OPTIONAL
,
576 IN UINTN TimeoutInMicroseconds
,
577 IN VOID
*ProcedureArgument OPTIONAL
,
578 OUT BOOLEAN
*Finished OPTIONAL
582 Worker function to switch the requested AP to be the BSP from that point onward.
584 @param[in] ProcessorNumber The handle number of AP that is to become the new BSP.
585 @param[in] EnableOldBSP If TRUE, then the old BSP will be listed as an
586 enabled AP. Otherwise, it will be disabled.
588 @retval EFI_SUCCESS BSP successfully switched.
589 @retval others Failed to switch BSP.
594 IN UINTN ProcessorNumber
,
595 IN BOOLEAN EnableOldBSP
599 Worker function to let the caller enable or disable an AP from this point onward.
600 This service may only be called from the BSP.
602 @param[in] ProcessorNumber The handle number of AP.
603 @param[in] EnableAP Specifies the new state for the processor for
604 enabled, FALSE for disabled.
605 @param[in] HealthFlag If not NULL, a pointer to a value that specifies
606 the new health status of the AP.
608 @retval EFI_SUCCESS The specified AP was enabled or disabled successfully.
609 @retval others Failed to Enable/Disable AP.
613 EnableDisableApWorker (
614 IN UINTN ProcessorNumber
,
616 IN UINT32
*HealthFlag OPTIONAL
620 Get pointer to CPU MP Data structure from GUIDed HOB.
622 @return The pointer to CPU MP Data structure.
625 GetCpuMpDataFromGuidedHob (
629 /** Checks status of specified AP.
631 This function checks whether the specified AP has finished the task assigned
632 by StartupThisAP(), and whether timeout expires.
634 @param[in] ProcessorNumber The handle number of processor.
636 @retval EFI_SUCCESS Specified AP has finished task assigned by StartupThisAPs().
637 @retval EFI_TIMEOUT The timeout expires.
638 @retval EFI_NOT_READY Specified AP has not finished task and timeout has not expired.
642 IN UINTN ProcessorNumber
646 Checks status of all APs.
648 This function checks whether all APs have finished task assigned by StartupAllAPs(),
649 and whether timeout expires.
651 @retval EFI_SUCCESS All APs have finished task assigned by StartupAllAPs().
652 @retval EFI_TIMEOUT The timeout expires.
653 @retval EFI_NOT_READY APs have not finished task and timeout has not expired.
661 Checks APs status and updates APs status if needed.
665 CheckAndUpdateApsStatus (
670 Detect whether specified processor can find matching microcode patch and load it.
672 @param[in] CpuMpData The pointer to CPU MP Data structure.
673 @param[in] ProcessorNumber The handle number of the processor. The range is
674 from 0 to the total number of logical processors
679 IN CPU_MP_DATA
*CpuMpData
,
680 IN UINTN ProcessorNumber
684 Shadow the required microcode patches data into memory.
686 @param[in, out] CpuMpData The pointer to CPU MP Data structure.
689 ShadowMicrocodeUpdatePatch (
690 IN OUT CPU_MP_DATA
*CpuMpData
694 Get the cached microcode patch base address and size from the microcode patch
695 information cache HOB.
697 @param[out] Address Base address of the microcode patches data.
698 It will be updated if the microcode patch
699 information cache HOB is found.
700 @param[out] RegionSize Size of the microcode patches data.
701 It will be updated if the microcode patch
702 information cache HOB is found.
704 @retval TRUE The microcode patch information cache HOB is found.
705 @retval FALSE The microcode patch information cache HOB is not found.
709 GetMicrocodePatchInfoFromHob (
715 Detect whether Mwait-monitor feature is supported.
717 @retval TRUE Mwait-monitor feature is supported.
718 @retval FALSE Mwait-monitor feature is not supported.
726 Enable Debug Agent to support source debugging on AP function.
735 Find the current Processor number by APIC ID.
737 @param[in] CpuMpData Pointer to PEI CPU MP Data
738 @param[out] ProcessorNumber Return the pocessor number found
740 @retval EFI_SUCCESS ProcessorNumber is found and returned.
741 @retval EFI_NOT_FOUND ProcessorNumber is not found.
745 IN CPU_MP_DATA
*CpuMpData
,
746 OUT UINTN
*ProcessorNumber
750 This funtion will try to invoke platform specific microcode shadow logic to
751 relocate microcode update patches into memory.
753 @param[in, out] CpuMpData The pointer to CPU MP Data structure.
755 @retval EFI_SUCCESS Shadow microcode success.
756 @retval EFI_OUT_OF_RESOURCES No enough resource to complete the operation.
757 @retval EFI_UNSUPPORTED Can't find platform specific microcode shadow
761 PlatformShadowMicrocode (
762 IN OUT CPU_MP_DATA
*CpuMpData
766 Allocate the SEV-ES AP jump table buffer.
768 @param[in, out] CpuMpData The pointer to CPU MP Data structure.
771 AllocateSevEsAPMemory (
772 IN OUT CPU_MP_DATA
*CpuMpData
776 Program the SEV-ES AP jump table buffer.
778 @param[in] SipiVector The SIPI vector used for the AP Reset
786 The function puts the AP in halt loop.
788 @param[in] CpuMpData The pointer to CPU MP Data structure.
792 CPU_MP_DATA
*CpuMpData
796 Check if the specified confidential computing attribute is active.
798 @retval TRUE The specified Attr is active.
799 @retval FALSE The specified Attr is not active.
803 ConfidentialComputingGuestHas (
804 CONFIDENTIAL_COMPUTING_GUEST_ATTR Attr
808 The function fills the exchange data for the AP.
810 @param[in] ExchangeInfo The pointer to CPU Exchange Data structure
813 FillExchangeInfoDataSevEs (
814 IN
volatile MP_CPU_EXCHANGE_INFO
*ExchangeInfo
818 Issue RMPADJUST to adjust the VMSA attribute of an SEV-SNP page.
820 @param[in] PageAddress
823 @return RMPADJUST return value
827 IN EFI_PHYSICAL_ADDRESS PageAddress
,
832 Create an SEV-SNP AP save area (VMSA) for use in running the vCPU.
834 @param[in] CpuMpData Pointer to CPU MP Data
835 @param[in] CpuData Pointer to CPU AP Data
836 @param[in] ApicId APIC ID of the vCPU
839 SevSnpCreateSaveArea (
840 IN CPU_MP_DATA
*CpuMpData
,
841 IN CPU_AP_DATA
*CpuData
,
848 @param[in] CpuMpData Pointer to CPU MP Data
849 @param[in] ProcessorNumber The handle number of specified processor
854 IN CPU_MP_DATA
*CpuMpData
,
855 IN INTN ProcessorNumber