]> git.proxmox.com Git - mirror_edk2.git/blob - UefiCpuPkg/Library/MpInitLib/DxeMpLib.c
UefiCpuPkg/MpInitLib: use PcdConfidentialComputingAttr to check SEV status
[mirror_edk2.git] / UefiCpuPkg / Library / MpInitLib / DxeMpLib.c
1 /** @file
2 MP initialize support functions for DXE phase.
3
4 Copyright (c) 2016 - 2020, Intel Corporation. All rights reserved.<BR>
5 SPDX-License-Identifier: BSD-2-Clause-Patent
6
7 **/
8
9 #include "MpLib.h"
10
11 #include <Library/UefiLib.h>
12 #include <Library/UefiBootServicesTableLib.h>
13 #include <Library/DebugAgentLib.h>
14 #include <Library/DxeServicesTableLib.h>
15 #include <Library/VmgExitLib.h>
16 #include <Register/Amd/Fam17Msr.h>
17 #include <Register/Amd/Ghcb.h>
18
19 #include <Protocol/Timer.h>
20
21 #define AP_SAFE_STACK_SIZE 128
22
23 CPU_MP_DATA *mCpuMpData = NULL;
24 EFI_EVENT mCheckAllApsEvent = NULL;
25 EFI_EVENT mMpInitExitBootServicesEvent = NULL;
26 EFI_EVENT mLegacyBootEvent = NULL;
27 volatile BOOLEAN mStopCheckAllApsStatus = TRUE;
28 VOID *mReservedApLoopFunc = NULL;
29 UINTN mReservedTopOfApStack;
30 volatile UINT32 mNumberToFinish = 0;
31
32 //
33 // Begin wakeup buffer allocation below 0x88000
34 //
35 STATIC EFI_PHYSICAL_ADDRESS mSevEsDxeWakeupBuffer = 0x88000;
36
37 /**
38 Enable Debug Agent to support source debugging on AP function.
39
40 **/
41 VOID
42 EnableDebugAgent (
43 VOID
44 )
45 {
46 //
47 // Initialize Debug Agent to support source level debug in DXE phase
48 //
49 InitializeDebugAgent (DEBUG_AGENT_INIT_DXE_AP, NULL, NULL);
50 }
51
52 /**
53 Get the pointer to CPU MP Data structure.
54
55 @return The pointer to CPU MP Data structure.
56 **/
57 CPU_MP_DATA *
58 GetCpuMpData (
59 VOID
60 )
61 {
62 ASSERT (mCpuMpData != NULL);
63 return mCpuMpData;
64 }
65
66 /**
67 Save the pointer to CPU MP Data structure.
68
69 @param[in] CpuMpData The pointer to CPU MP Data structure will be saved.
70 **/
71 VOID
72 SaveCpuMpData (
73 IN CPU_MP_DATA *CpuMpData
74 )
75 {
76 mCpuMpData = CpuMpData;
77 }
78
79 /**
80 Get available system memory below 0x88000 by specified size.
81
82 @param[in] WakeupBufferSize Wakeup buffer size required
83
84 @retval other Return wakeup buffer address below 1MB.
85 @retval -1 Cannot find free memory below 1MB.
86 **/
87 UINTN
88 GetWakeupBuffer (
89 IN UINTN WakeupBufferSize
90 )
91 {
92 EFI_STATUS Status;
93 EFI_PHYSICAL_ADDRESS StartAddress;
94 EFI_MEMORY_TYPE MemoryType;
95
96 if (ConfidentialComputingGuestHas (CCAttrAmdSevEs)) {
97 MemoryType = EfiReservedMemoryType;
98 } else {
99 MemoryType = EfiBootServicesData;
100 }
101
102 //
103 // Try to allocate buffer below 1M for waking vector.
104 // LegacyBios driver only reports warning when page allocation in range
105 // [0x60000, 0x88000) fails.
106 // This library is consumed by CpuDxe driver to produce CPU Arch protocol.
107 // LagacyBios driver depends on CPU Arch protocol which guarantees below
108 // allocation runs earlier than LegacyBios driver.
109 //
110 if (ConfidentialComputingGuestHas (CCAttrAmdSevEs)) {
111 //
112 // SEV-ES Wakeup buffer should be under 0x88000 and under any previous one
113 //
114 StartAddress = mSevEsDxeWakeupBuffer;
115 } else {
116 StartAddress = 0x88000;
117 }
118
119 Status = gBS->AllocatePages (
120 AllocateMaxAddress,
121 MemoryType,
122 EFI_SIZE_TO_PAGES (WakeupBufferSize),
123 &StartAddress
124 );
125 ASSERT_EFI_ERROR (Status);
126 if (EFI_ERROR (Status)) {
127 StartAddress = (EFI_PHYSICAL_ADDRESS)-1;
128 } else if (ConfidentialComputingGuestHas (CCAttrAmdSevEs)) {
129 //
130 // Next SEV-ES wakeup buffer allocation must be below this allocation
131 //
132 mSevEsDxeWakeupBuffer = StartAddress;
133 }
134
135 DEBUG ((
136 DEBUG_INFO,
137 "WakeupBufferStart = %x, WakeupBufferSize = %x\n",
138 (UINTN)StartAddress,
139 WakeupBufferSize
140 ));
141
142 return (UINTN)StartAddress;
143 }
144
145 /**
146 Get available EfiBootServicesCode memory below 4GB by specified size.
147
148 This buffer is required to safely transfer AP from real address mode to
149 protected mode or long mode, due to the fact that the buffer returned by
150 GetWakeupBuffer() may be marked as non-executable.
151
152 @param[in] BufferSize Wakeup transition buffer size.
153
154 @retval other Return wakeup transition buffer address below 4GB.
155 @retval 0 Cannot find free memory below 4GB.
156 **/
157 UINTN
158 GetModeTransitionBuffer (
159 IN UINTN BufferSize
160 )
161 {
162 EFI_STATUS Status;
163 EFI_PHYSICAL_ADDRESS StartAddress;
164
165 StartAddress = BASE_4GB - 1;
166 Status = gBS->AllocatePages (
167 AllocateMaxAddress,
168 EfiBootServicesCode,
169 EFI_SIZE_TO_PAGES (BufferSize),
170 &StartAddress
171 );
172 if (EFI_ERROR (Status)) {
173 StartAddress = 0;
174 }
175
176 return (UINTN)StartAddress;
177 }
178
179 /**
180 Return the address of the SEV-ES AP jump table.
181
182 This buffer is required in order for an SEV-ES guest to transition from
183 UEFI into an OS.
184
185 @return Return SEV-ES AP jump table buffer
186 **/
187 UINTN
188 GetSevEsAPMemory (
189 VOID
190 )
191 {
192 EFI_STATUS Status;
193 EFI_PHYSICAL_ADDRESS StartAddress;
194 MSR_SEV_ES_GHCB_REGISTER Msr;
195 GHCB *Ghcb;
196 BOOLEAN InterruptState;
197
198 //
199 // Allocate 1 page for AP jump table page
200 //
201 StartAddress = BASE_4GB - 1;
202 Status = gBS->AllocatePages (
203 AllocateMaxAddress,
204 EfiReservedMemoryType,
205 1,
206 &StartAddress
207 );
208 ASSERT_EFI_ERROR (Status);
209
210 DEBUG ((DEBUG_INFO, "Dxe: SevEsAPMemory = %lx\n", (UINTN)StartAddress));
211
212 //
213 // Save the SevEsAPMemory as the AP jump table.
214 //
215 Msr.GhcbPhysicalAddress = AsmReadMsr64 (MSR_SEV_ES_GHCB);
216 Ghcb = Msr.Ghcb;
217
218 VmgInit (Ghcb, &InterruptState);
219 VmgExit (Ghcb, SVM_EXIT_AP_JUMP_TABLE, 0, (UINT64)(UINTN)StartAddress);
220 VmgDone (Ghcb, InterruptState);
221
222 return (UINTN)StartAddress;
223 }
224
225 /**
226 Checks APs status and updates APs status if needed.
227
228 **/
229 VOID
230 CheckAndUpdateApsStatus (
231 VOID
232 )
233 {
234 UINTN ProcessorNumber;
235 EFI_STATUS Status;
236 CPU_MP_DATA *CpuMpData;
237
238 CpuMpData = GetCpuMpData ();
239
240 //
241 // First, check whether pending StartupAllAPs() exists.
242 //
243 if (CpuMpData->WaitEvent != NULL) {
244 Status = CheckAllAPs ();
245 //
246 // If all APs finish for StartupAllAPs(), signal the WaitEvent for it.
247 //
248 if (Status != EFI_NOT_READY) {
249 Status = gBS->SignalEvent (CpuMpData->WaitEvent);
250 CpuMpData->WaitEvent = NULL;
251 }
252 }
253
254 //
255 // Second, check whether pending StartupThisAPs() callings exist.
256 //
257 for (ProcessorNumber = 0; ProcessorNumber < CpuMpData->CpuCount; ProcessorNumber++) {
258 if (CpuMpData->CpuData[ProcessorNumber].WaitEvent == NULL) {
259 continue;
260 }
261
262 Status = CheckThisAP (ProcessorNumber);
263
264 if (Status != EFI_NOT_READY) {
265 gBS->SignalEvent (CpuMpData->CpuData[ProcessorNumber].WaitEvent);
266 CpuMpData->CpuData[ProcessorNumber].WaitEvent = NULL;
267 }
268 }
269 }
270
271 /**
272 Checks APs' status periodically.
273
274 This function is triggered by timer periodically to check the
275 state of APs for StartupAllAPs() and StartupThisAP() executed
276 in non-blocking mode.
277
278 @param[in] Event Event triggered.
279 @param[in] Context Parameter passed with the event.
280
281 **/
282 VOID
283 EFIAPI
284 CheckApsStatus (
285 IN EFI_EVENT Event,
286 IN VOID *Context
287 )
288 {
289 //
290 // If CheckApsStatus() is not stopped, otherwise return immediately.
291 //
292 if (!mStopCheckAllApsStatus) {
293 CheckAndUpdateApsStatus ();
294 }
295 }
296
297 /**
298 Get Protected mode code segment with 16-bit default addressing
299 from current GDT table.
300
301 @return Protected mode 16-bit code segment value.
302 **/
303 UINT16
304 GetProtectedMode16CS (
305 VOID
306 )
307 {
308 IA32_DESCRIPTOR GdtrDesc;
309 IA32_SEGMENT_DESCRIPTOR *GdtEntry;
310 UINTN GdtEntryCount;
311 UINT16 Index;
312
313 Index = (UINT16)-1;
314 AsmReadGdtr (&GdtrDesc);
315 GdtEntryCount = (GdtrDesc.Limit + 1) / sizeof (IA32_SEGMENT_DESCRIPTOR);
316 GdtEntry = (IA32_SEGMENT_DESCRIPTOR *)GdtrDesc.Base;
317 for (Index = 0; Index < GdtEntryCount; Index++) {
318 if (GdtEntry->Bits.L == 0) {
319 if ((GdtEntry->Bits.Type > 8) && (GdtEntry->Bits.DB == 0)) {
320 break;
321 }
322 }
323
324 GdtEntry++;
325 }
326
327 ASSERT (Index != GdtEntryCount);
328 return Index * 8;
329 }
330
331 /**
332 Get Protected mode code segment from current GDT table.
333
334 @return Protected mode code segment value.
335 **/
336 UINT16
337 GetProtectedModeCS (
338 VOID
339 )
340 {
341 IA32_DESCRIPTOR GdtrDesc;
342 IA32_SEGMENT_DESCRIPTOR *GdtEntry;
343 UINTN GdtEntryCount;
344 UINT16 Index;
345
346 AsmReadGdtr (&GdtrDesc);
347 GdtEntryCount = (GdtrDesc.Limit + 1) / sizeof (IA32_SEGMENT_DESCRIPTOR);
348 GdtEntry = (IA32_SEGMENT_DESCRIPTOR *)GdtrDesc.Base;
349 for (Index = 0; Index < GdtEntryCount; Index++) {
350 if (GdtEntry->Bits.L == 0) {
351 if ((GdtEntry->Bits.Type > 8) && (GdtEntry->Bits.DB == 1)) {
352 break;
353 }
354 }
355
356 GdtEntry++;
357 }
358
359 ASSERT (Index != GdtEntryCount);
360 return Index * 8;
361 }
362
363 /**
364 Do sync on APs.
365
366 @param[in, out] Buffer Pointer to private data buffer.
367 **/
368 VOID
369 EFIAPI
370 RelocateApLoop (
371 IN OUT VOID *Buffer
372 )
373 {
374 CPU_MP_DATA *CpuMpData;
375 BOOLEAN MwaitSupport;
376 ASM_RELOCATE_AP_LOOP AsmRelocateApLoopFunc;
377 UINTN ProcessorNumber;
378 UINTN StackStart;
379
380 MpInitLibWhoAmI (&ProcessorNumber);
381 CpuMpData = GetCpuMpData ();
382 MwaitSupport = IsMwaitSupport ();
383 if (CpuMpData->SevEsIsEnabled) {
384 StackStart = CpuMpData->SevEsAPResetStackStart;
385 } else {
386 StackStart = mReservedTopOfApStack;
387 }
388
389 AsmRelocateApLoopFunc = (ASM_RELOCATE_AP_LOOP)(UINTN)mReservedApLoopFunc;
390 AsmRelocateApLoopFunc (
391 MwaitSupport,
392 CpuMpData->ApTargetCState,
393 CpuMpData->PmCodeSegment,
394 StackStart - ProcessorNumber * AP_SAFE_STACK_SIZE,
395 (UINTN)&mNumberToFinish,
396 CpuMpData->Pm16CodeSegment,
397 CpuMpData->SevEsAPBuffer,
398 CpuMpData->WakeupBuffer
399 );
400 //
401 // It should never reach here
402 //
403 ASSERT (FALSE);
404 }
405
406 /**
407 Callback function for ExitBootServices.
408
409 @param[in] Event Event whose notification function is being invoked.
410 @param[in] Context The pointer to the notification function's context,
411 which is implementation-dependent.
412
413 **/
414 VOID
415 EFIAPI
416 MpInitChangeApLoopCallback (
417 IN EFI_EVENT Event,
418 IN VOID *Context
419 )
420 {
421 CPU_MP_DATA *CpuMpData;
422
423 CpuMpData = GetCpuMpData ();
424 CpuMpData->PmCodeSegment = GetProtectedModeCS ();
425 CpuMpData->Pm16CodeSegment = GetProtectedMode16CS ();
426 CpuMpData->ApLoopMode = PcdGet8 (PcdCpuApLoopMode);
427 mNumberToFinish = CpuMpData->CpuCount - 1;
428 WakeUpAP (CpuMpData, TRUE, 0, RelocateApLoop, NULL, TRUE);
429 while (mNumberToFinish > 0) {
430 CpuPause ();
431 }
432
433 if (CpuMpData->SevEsIsEnabled && (CpuMpData->WakeupBuffer != (UINTN)-1)) {
434 //
435 // There are APs present. Re-use reserved memory area below 1MB from
436 // WakeupBuffer as the area to be used for transitioning to 16-bit mode
437 // in support of booting of the AP by an OS.
438 //
439 CopyMem (
440 (VOID *)CpuMpData->WakeupBuffer,
441 (VOID *)(CpuMpData->AddressMap.RendezvousFunnelAddress +
442 CpuMpData->AddressMap.SwitchToRealPM16ModeOffset),
443 CpuMpData->AddressMap.SwitchToRealPM16ModeSize
444 );
445 }
446
447 DEBUG ((DEBUG_INFO, "%a() done!\n", __FUNCTION__));
448 }
449
450 /**
451 Initialize global data for MP support.
452
453 @param[in] CpuMpData The pointer to CPU MP Data structure.
454 **/
455 VOID
456 InitMpGlobalData (
457 IN CPU_MP_DATA *CpuMpData
458 )
459 {
460 EFI_STATUS Status;
461 EFI_PHYSICAL_ADDRESS Address;
462 UINTN ApSafeBufferSize;
463 UINTN Index;
464 EFI_GCD_MEMORY_SPACE_DESCRIPTOR MemDesc;
465 UINTN StackBase;
466 CPU_INFO_IN_HOB *CpuInfoInHob;
467
468 SaveCpuMpData (CpuMpData);
469
470 if (CpuMpData->CpuCount == 1) {
471 //
472 // If only BSP exists, return
473 //
474 return;
475 }
476
477 if (PcdGetBool (PcdCpuStackGuard)) {
478 //
479 // One extra page at the bottom of the stack is needed for Guard page.
480 //
481 if (CpuMpData->CpuApStackSize <= EFI_PAGE_SIZE) {
482 DEBUG ((DEBUG_ERROR, "PcdCpuApStackSize is not big enough for Stack Guard!\n"));
483 ASSERT (FALSE);
484 }
485
486 //
487 // DXE will reuse stack allocated for APs at PEI phase if it's available.
488 // Let's check it here.
489 //
490 // Note: BSP's stack guard is set at DxeIpl phase. But for the sake of
491 // BSP/AP exchange, stack guard for ApTopOfStack of cpu 0 will still be
492 // set here.
493 //
494 CpuInfoInHob = (CPU_INFO_IN_HOB *)(UINTN)CpuMpData->CpuInfoInHob;
495 for (Index = 0; Index < CpuMpData->CpuCount; ++Index) {
496 if ((CpuInfoInHob != NULL) && (CpuInfoInHob[Index].ApTopOfStack != 0)) {
497 StackBase = (UINTN)CpuInfoInHob[Index].ApTopOfStack - CpuMpData->CpuApStackSize;
498 } else {
499 StackBase = CpuMpData->Buffer + Index * CpuMpData->CpuApStackSize;
500 }
501
502 Status = gDS->GetMemorySpaceDescriptor (StackBase, &MemDesc);
503 ASSERT_EFI_ERROR (Status);
504
505 Status = gDS->SetMemorySpaceAttributes (
506 StackBase,
507 EFI_PAGES_TO_SIZE (1),
508 MemDesc.Attributes | EFI_MEMORY_RP
509 );
510 ASSERT_EFI_ERROR (Status);
511
512 DEBUG ((
513 DEBUG_INFO,
514 "Stack Guard set at %lx [cpu%lu]!\n",
515 (UINT64)StackBase,
516 (UINT64)Index
517 ));
518 }
519 }
520
521 //
522 // Avoid APs access invalid buffer data which allocated by BootServices,
523 // so we will allocate reserved data for AP loop code. We also need to
524 // allocate this buffer below 4GB due to APs may be transferred to 32bit
525 // protected mode on long mode DXE.
526 // Allocating it in advance since memory services are not available in
527 // Exit Boot Services callback function.
528 //
529 ApSafeBufferSize = EFI_PAGES_TO_SIZE (
530 EFI_SIZE_TO_PAGES (
531 CpuMpData->AddressMap.RelocateApLoopFuncSize
532 )
533 );
534 Address = BASE_4GB - 1;
535 Status = gBS->AllocatePages (
536 AllocateMaxAddress,
537 EfiReservedMemoryType,
538 EFI_SIZE_TO_PAGES (ApSafeBufferSize),
539 &Address
540 );
541 ASSERT_EFI_ERROR (Status);
542
543 mReservedApLoopFunc = (VOID *)(UINTN)Address;
544 ASSERT (mReservedApLoopFunc != NULL);
545
546 //
547 // Make sure that the buffer memory is executable if NX protection is enabled
548 // for EfiReservedMemoryType.
549 //
550 // TODO: Check EFI_MEMORY_XP bit set or not once it's available in DXE GCD
551 // service.
552 //
553 Status = gDS->GetMemorySpaceDescriptor (Address, &MemDesc);
554 if (!EFI_ERROR (Status)) {
555 gDS->SetMemorySpaceAttributes (
556 Address,
557 ApSafeBufferSize,
558 MemDesc.Attributes & (~EFI_MEMORY_XP)
559 );
560 }
561
562 ApSafeBufferSize = EFI_PAGES_TO_SIZE (
563 EFI_SIZE_TO_PAGES (
564 CpuMpData->CpuCount * AP_SAFE_STACK_SIZE
565 )
566 );
567 Address = BASE_4GB - 1;
568 Status = gBS->AllocatePages (
569 AllocateMaxAddress,
570 EfiReservedMemoryType,
571 EFI_SIZE_TO_PAGES (ApSafeBufferSize),
572 &Address
573 );
574 ASSERT_EFI_ERROR (Status);
575
576 mReservedTopOfApStack = (UINTN)Address + ApSafeBufferSize;
577 ASSERT ((mReservedTopOfApStack & (UINTN)(CPU_STACK_ALIGNMENT - 1)) == 0);
578 CopyMem (
579 mReservedApLoopFunc,
580 CpuMpData->AddressMap.RelocateApLoopFuncAddress,
581 CpuMpData->AddressMap.RelocateApLoopFuncSize
582 );
583
584 Status = gBS->CreateEvent (
585 EVT_TIMER | EVT_NOTIFY_SIGNAL,
586 TPL_NOTIFY,
587 CheckApsStatus,
588 NULL,
589 &mCheckAllApsEvent
590 );
591 ASSERT_EFI_ERROR (Status);
592
593 //
594 // Set timer to check all APs status.
595 //
596 Status = gBS->SetTimer (
597 mCheckAllApsEvent,
598 TimerPeriodic,
599 EFI_TIMER_PERIOD_MICROSECONDS (
600 PcdGet32 (PcdCpuApStatusCheckIntervalInMicroSeconds)
601 )
602 );
603 ASSERT_EFI_ERROR (Status);
604
605 Status = gBS->CreateEvent (
606 EVT_SIGNAL_EXIT_BOOT_SERVICES,
607 TPL_CALLBACK,
608 MpInitChangeApLoopCallback,
609 NULL,
610 &mMpInitExitBootServicesEvent
611 );
612 ASSERT_EFI_ERROR (Status);
613
614 Status = gBS->CreateEventEx (
615 EVT_NOTIFY_SIGNAL,
616 TPL_CALLBACK,
617 MpInitChangeApLoopCallback,
618 NULL,
619 &gEfiEventLegacyBootGuid,
620 &mLegacyBootEvent
621 );
622 ASSERT_EFI_ERROR (Status);
623 }
624
625 /**
626 This service executes a caller provided function on all enabled APs.
627
628 @param[in] Procedure A pointer to the function to be run on
629 enabled APs of the system. See type
630 EFI_AP_PROCEDURE.
631 @param[in] SingleThread If TRUE, then all the enabled APs execute
632 the function specified by Procedure one by
633 one, in ascending order of processor handle
634 number. If FALSE, then all the enabled APs
635 execute the function specified by Procedure
636 simultaneously.
637 @param[in] WaitEvent The event created by the caller with CreateEvent()
638 service. If it is NULL, then execute in
639 blocking mode. BSP waits until all APs finish
640 or TimeoutInMicroSeconds expires. If it's
641 not NULL, then execute in non-blocking mode.
642 BSP requests the function specified by
643 Procedure to be started on all the enabled
644 APs, and go on executing immediately. If
645 all return from Procedure, or TimeoutInMicroSeconds
646 expires, this event is signaled. The BSP
647 can use the CheckEvent() or WaitForEvent()
648 services to check the state of event. Type
649 EFI_EVENT is defined in CreateEvent() in
650 the Unified Extensible Firmware Interface
651 Specification.
652 @param[in] TimeoutInMicroseconds Indicates the time limit in microseconds for
653 APs to return from Procedure, either for
654 blocking or non-blocking mode. Zero means
655 infinity. If the timeout expires before
656 all APs return from Procedure, then Procedure
657 on the failed APs is terminated. All enabled
658 APs are available for next function assigned
659 by MpInitLibStartupAllAPs() or
660 MPInitLibStartupThisAP().
661 If the timeout expires in blocking mode,
662 BSP returns EFI_TIMEOUT. If the timeout
663 expires in non-blocking mode, WaitEvent
664 is signaled with SignalEvent().
665 @param[in] ProcedureArgument The parameter passed into Procedure for
666 all APs.
667 @param[out] FailedCpuList If NULL, this parameter is ignored. Otherwise,
668 if all APs finish successfully, then its
669 content is set to NULL. If not all APs
670 finish before timeout expires, then its
671 content is set to address of the buffer
672 holding handle numbers of the failed APs.
673 The buffer is allocated by MP Initialization
674 library, and it's the caller's responsibility to
675 free the buffer with FreePool() service.
676 In blocking mode, it is ready for consumption
677 when the call returns. In non-blocking mode,
678 it is ready when WaitEvent is signaled. The
679 list of failed CPU is terminated by
680 END_OF_CPU_LIST.
681
682 @retval EFI_SUCCESS In blocking mode, all APs have finished before
683 the timeout expired.
684 @retval EFI_SUCCESS In non-blocking mode, function has been dispatched
685 to all enabled APs.
686 @retval EFI_UNSUPPORTED A non-blocking mode request was made after the
687 UEFI event EFI_EVENT_GROUP_READY_TO_BOOT was
688 signaled.
689 @retval EFI_UNSUPPORTED WaitEvent is not NULL if non-blocking mode is not
690 supported.
691 @retval EFI_DEVICE_ERROR Caller processor is AP.
692 @retval EFI_NOT_STARTED No enabled APs exist in the system.
693 @retval EFI_NOT_READY Any enabled APs are busy.
694 @retval EFI_NOT_READY MP Initialize Library is not initialized.
695 @retval EFI_TIMEOUT In blocking mode, the timeout expired before
696 all enabled APs have finished.
697 @retval EFI_INVALID_PARAMETER Procedure is NULL.
698
699 **/
700 EFI_STATUS
701 EFIAPI
702 MpInitLibStartupAllAPs (
703 IN EFI_AP_PROCEDURE Procedure,
704 IN BOOLEAN SingleThread,
705 IN EFI_EVENT WaitEvent OPTIONAL,
706 IN UINTN TimeoutInMicroseconds,
707 IN VOID *ProcedureArgument OPTIONAL,
708 OUT UINTN **FailedCpuList OPTIONAL
709 )
710 {
711 EFI_STATUS Status;
712
713 //
714 // Temporarily stop checkAllApsStatus for avoid resource dead-lock.
715 //
716 mStopCheckAllApsStatus = TRUE;
717
718 Status = StartupAllCPUsWorker (
719 Procedure,
720 SingleThread,
721 TRUE,
722 WaitEvent,
723 TimeoutInMicroseconds,
724 ProcedureArgument,
725 FailedCpuList
726 );
727
728 //
729 // Start checkAllApsStatus
730 //
731 mStopCheckAllApsStatus = FALSE;
732
733 return Status;
734 }
735
736 /**
737 This service lets the caller get one enabled AP to execute a caller-provided
738 function.
739
740 @param[in] Procedure A pointer to the function to be run on the
741 designated AP of the system. See type
742 EFI_AP_PROCEDURE.
743 @param[in] ProcessorNumber The handle number of the AP. The range is
744 from 0 to the total number of logical
745 processors minus 1. The total number of
746 logical processors can be retrieved by
747 MpInitLibGetNumberOfProcessors().
748 @param[in] WaitEvent The event created by the caller with CreateEvent()
749 service. If it is NULL, then execute in
750 blocking mode. BSP waits until this AP finish
751 or TimeoutInMicroSeconds expires. If it's
752 not NULL, then execute in non-blocking mode.
753 BSP requests the function specified by
754 Procedure to be started on this AP,
755 and go on executing immediately. If this AP
756 return from Procedure or TimeoutInMicroSeconds
757 expires, this event is signaled. The BSP
758 can use the CheckEvent() or WaitForEvent()
759 services to check the state of event. Type
760 EFI_EVENT is defined in CreateEvent() in
761 the Unified Extensible Firmware Interface
762 Specification.
763 @param[in] TimeoutInMicroseconds Indicates the time limit in microseconds for
764 this AP to finish this Procedure, either for
765 blocking or non-blocking mode. Zero means
766 infinity. If the timeout expires before
767 this AP returns from Procedure, then Procedure
768 on the AP is terminated. The
769 AP is available for next function assigned
770 by MpInitLibStartupAllAPs() or
771 MpInitLibStartupThisAP().
772 If the timeout expires in blocking mode,
773 BSP returns EFI_TIMEOUT. If the timeout
774 expires in non-blocking mode, WaitEvent
775 is signaled with SignalEvent().
776 @param[in] ProcedureArgument The parameter passed into Procedure on the
777 specified AP.
778 @param[out] Finished If NULL, this parameter is ignored. In
779 blocking mode, this parameter is ignored.
780 In non-blocking mode, if AP returns from
781 Procedure before the timeout expires, its
782 content is set to TRUE. Otherwise, the
783 value is set to FALSE. The caller can
784 determine if the AP returned from Procedure
785 by evaluating this value.
786
787 @retval EFI_SUCCESS In blocking mode, specified AP finished before
788 the timeout expires.
789 @retval EFI_SUCCESS In non-blocking mode, the function has been
790 dispatched to specified AP.
791 @retval EFI_UNSUPPORTED A non-blocking mode request was made after the
792 UEFI event EFI_EVENT_GROUP_READY_TO_BOOT was
793 signaled.
794 @retval EFI_UNSUPPORTED WaitEvent is not NULL if non-blocking mode is not
795 supported.
796 @retval EFI_DEVICE_ERROR The calling processor is an AP.
797 @retval EFI_TIMEOUT In blocking mode, the timeout expired before
798 the specified AP has finished.
799 @retval EFI_NOT_READY The specified AP is busy.
800 @retval EFI_NOT_READY MP Initialize Library is not initialized.
801 @retval EFI_NOT_FOUND The processor with the handle specified by
802 ProcessorNumber does not exist.
803 @retval EFI_INVALID_PARAMETER ProcessorNumber specifies the BSP or disabled AP.
804 @retval EFI_INVALID_PARAMETER Procedure is NULL.
805
806 **/
807 EFI_STATUS
808 EFIAPI
809 MpInitLibStartupThisAP (
810 IN EFI_AP_PROCEDURE Procedure,
811 IN UINTN ProcessorNumber,
812 IN EFI_EVENT WaitEvent OPTIONAL,
813 IN UINTN TimeoutInMicroseconds,
814 IN VOID *ProcedureArgument OPTIONAL,
815 OUT BOOLEAN *Finished OPTIONAL
816 )
817 {
818 EFI_STATUS Status;
819
820 //
821 // temporarily stop checkAllApsStatus for avoid resource dead-lock.
822 //
823 mStopCheckAllApsStatus = TRUE;
824
825 Status = StartupThisAPWorker (
826 Procedure,
827 ProcessorNumber,
828 WaitEvent,
829 TimeoutInMicroseconds,
830 ProcedureArgument,
831 Finished
832 );
833
834 mStopCheckAllApsStatus = FALSE;
835
836 return Status;
837 }
838
839 /**
840 This service switches the requested AP to be the BSP from that point onward.
841 This service changes the BSP for all purposes. This call can only be performed
842 by the current BSP.
843
844 @param[in] ProcessorNumber The handle number of AP that is to become the new
845 BSP. The range is from 0 to the total number of
846 logical processors minus 1. The total number of
847 logical processors can be retrieved by
848 MpInitLibGetNumberOfProcessors().
849 @param[in] EnableOldBSP If TRUE, then the old BSP will be listed as an
850 enabled AP. Otherwise, it will be disabled.
851
852 @retval EFI_SUCCESS BSP successfully switched.
853 @retval EFI_UNSUPPORTED Switching the BSP cannot be completed prior to
854 this service returning.
855 @retval EFI_UNSUPPORTED Switching the BSP is not supported.
856 @retval EFI_DEVICE_ERROR The calling processor is an AP.
857 @retval EFI_NOT_FOUND The processor with the handle specified by
858 ProcessorNumber does not exist.
859 @retval EFI_INVALID_PARAMETER ProcessorNumber specifies the current BSP or
860 a disabled AP.
861 @retval EFI_NOT_READY The specified AP is busy.
862 @retval EFI_NOT_READY MP Initialize Library is not initialized.
863
864 **/
865 EFI_STATUS
866 EFIAPI
867 MpInitLibSwitchBSP (
868 IN UINTN ProcessorNumber,
869 IN BOOLEAN EnableOldBSP
870 )
871 {
872 EFI_STATUS Status;
873 EFI_TIMER_ARCH_PROTOCOL *Timer;
874 UINT64 TimerPeriod;
875
876 TimerPeriod = 0;
877 //
878 // Locate Timer Arch Protocol
879 //
880 Status = gBS->LocateProtocol (&gEfiTimerArchProtocolGuid, NULL, (VOID **)&Timer);
881 if (EFI_ERROR (Status)) {
882 Timer = NULL;
883 }
884
885 if (Timer != NULL) {
886 //
887 // Save current rate of DXE Timer
888 //
889 Timer->GetTimerPeriod (Timer, &TimerPeriod);
890 //
891 // Disable DXE Timer and drain pending interrupts
892 //
893 Timer->SetTimerPeriod (Timer, 0);
894 }
895
896 Status = SwitchBSPWorker (ProcessorNumber, EnableOldBSP);
897
898 if (Timer != NULL) {
899 //
900 // Enable and restore rate of DXE Timer
901 //
902 Timer->SetTimerPeriod (Timer, TimerPeriod);
903 }
904
905 return Status;
906 }
907
908 /**
909 This service lets the caller enable or disable an AP from this point onward.
910 This service may only be called from the BSP.
911
912 @param[in] ProcessorNumber The handle number of AP.
913 The range is from 0 to the total number of
914 logical processors minus 1. The total number of
915 logical processors can be retrieved by
916 MpInitLibGetNumberOfProcessors().
917 @param[in] EnableAP Specifies the new state for the processor for
918 enabled, FALSE for disabled.
919 @param[in] HealthFlag If not NULL, a pointer to a value that specifies
920 the new health status of the AP. This flag
921 corresponds to StatusFlag defined in
922 EFI_MP_SERVICES_PROTOCOL.GetProcessorInfo(). Only
923 the PROCESSOR_HEALTH_STATUS_BIT is used. All other
924 bits are ignored. If it is NULL, this parameter
925 is ignored.
926
927 @retval EFI_SUCCESS The specified AP was enabled or disabled successfully.
928 @retval EFI_UNSUPPORTED Enabling or disabling an AP cannot be completed
929 prior to this service returning.
930 @retval EFI_UNSUPPORTED Enabling or disabling an AP is not supported.
931 @retval EFI_DEVICE_ERROR The calling processor is an AP.
932 @retval EFI_NOT_FOUND Processor with the handle specified by ProcessorNumber
933 does not exist.
934 @retval EFI_INVALID_PARAMETER ProcessorNumber specifies the BSP.
935 @retval EFI_NOT_READY MP Initialize Library is not initialized.
936
937 **/
938 EFI_STATUS
939 EFIAPI
940 MpInitLibEnableDisableAP (
941 IN UINTN ProcessorNumber,
942 IN BOOLEAN EnableAP,
943 IN UINT32 *HealthFlag OPTIONAL
944 )
945 {
946 EFI_STATUS Status;
947 BOOLEAN TempStopCheckState;
948
949 TempStopCheckState = FALSE;
950 //
951 // temporarily stop checkAllAPsStatus for initialize parameters.
952 //
953 if (!mStopCheckAllApsStatus) {
954 mStopCheckAllApsStatus = TRUE;
955 TempStopCheckState = TRUE;
956 }
957
958 Status = EnableDisableApWorker (ProcessorNumber, EnableAP, HealthFlag);
959
960 if (TempStopCheckState) {
961 mStopCheckAllApsStatus = FALSE;
962 }
963
964 return Status;
965 }
966
967 /**
968 This funtion will try to invoke platform specific microcode shadow logic to
969 relocate microcode update patches into memory.
970
971 @param[in, out] CpuMpData The pointer to CPU MP Data structure.
972
973 @retval EFI_SUCCESS Shadow microcode success.
974 @retval EFI_OUT_OF_RESOURCES No enough resource to complete the operation.
975 @retval EFI_UNSUPPORTED Can't find platform specific microcode shadow
976 PPI/Protocol.
977 **/
978 EFI_STATUS
979 PlatformShadowMicrocode (
980 IN OUT CPU_MP_DATA *CpuMpData
981 )
982 {
983 //
984 // There is no DXE version of platform shadow microcode protocol so far.
985 // A platform which only uses DxeMpInitLib instance could only supports
986 // the PCD based microcode shadowing.
987 //
988 return EFI_UNSUPPORTED;
989 }