]> git.proxmox.com Git - mirror_edk2.git/blob - UefiCpuPkg/CpuDxe/CpuMp.c
UefiCpuPkg: Move AsmRelocateApLoopStart from Mpfuncs.nasm to AmdSev.nasm
[mirror_edk2.git] / UefiCpuPkg / CpuDxe / CpuMp.c
1 /** @file
2 CPU DXE Module to produce CPU MP Protocol.
3
4 Copyright (c) 2008 - 2022, Intel Corporation. All rights reserved.<BR>
5 SPDX-License-Identifier: BSD-2-Clause-Patent
6
7 **/
8
9 #include "CpuDxe.h"
10 #include "CpuMp.h"
11
12 EFI_HANDLE mMpServiceHandle = NULL;
13 UINTN mNumberOfProcessors = 1;
14
15 EFI_MP_SERVICES_PROTOCOL mMpServicesTemplate = {
16 GetNumberOfProcessors,
17 GetProcessorInfo,
18 StartupAllAPs,
19 StartupThisAP,
20 SwitchBSP,
21 EnableDisableAP,
22 WhoAmI
23 };
24
25 /**
26 This service retrieves the number of logical processor in the platform
27 and the number of those logical processors that are enabled on this boot.
28 This service may only be called from the BSP.
29
30 This function is used to retrieve the following information:
31 - The number of logical processors that are present in the system.
32 - The number of enabled logical processors in the system at the instant
33 this call is made.
34
35 Because MP Service Protocol provides services to enable and disable processors
36 dynamically, the number of enabled logical processors may vary during the
37 course of a boot session.
38
39 If this service is called from an AP, then EFI_DEVICE_ERROR is returned.
40 If NumberOfProcessors or NumberOfEnabledProcessors is NULL, then
41 EFI_INVALID_PARAMETER is returned. Otherwise, the total number of processors
42 is returned in NumberOfProcessors, the number of currently enabled processor
43 is returned in NumberOfEnabledProcessors, and EFI_SUCCESS is returned.
44
45 @param[in] This A pointer to the EFI_MP_SERVICES_PROTOCOL
46 instance.
47 @param[out] NumberOfProcessors Pointer to the total number of logical
48 processors in the system, including the BSP
49 and disabled APs.
50 @param[out] NumberOfEnabledProcessors Pointer to the number of enabled logical
51 processors that exist in system, including
52 the BSP.
53
54 @retval EFI_SUCCESS The number of logical processors and enabled
55 logical processors was retrieved.
56 @retval EFI_DEVICE_ERROR The calling processor is an AP.
57 @retval EFI_INVALID_PARAMETER NumberOfProcessors is NULL.
58 @retval EFI_INVALID_PARAMETER NumberOfEnabledProcessors is NULL.
59
60 **/
61 EFI_STATUS
62 EFIAPI
63 GetNumberOfProcessors (
64 IN EFI_MP_SERVICES_PROTOCOL *This,
65 OUT UINTN *NumberOfProcessors,
66 OUT UINTN *NumberOfEnabledProcessors
67 )
68 {
69 if ((NumberOfProcessors == NULL) || (NumberOfEnabledProcessors == NULL)) {
70 return EFI_INVALID_PARAMETER;
71 }
72
73 return MpInitLibGetNumberOfProcessors (
74 NumberOfProcessors,
75 NumberOfEnabledProcessors
76 );
77 }
78
79 /**
80 Gets detailed MP-related information on the requested processor at the
81 instant this call is made. This service may only be called from the BSP.
82
83 This service retrieves detailed MP-related information about any processor
84 on the platform. Note the following:
85 - The processor information may change during the course of a boot session.
86 - The information presented here is entirely MP related.
87
88 Information regarding the number of caches and their sizes, frequency of operation,
89 slot numbers is all considered platform-related information and is not provided
90 by this service.
91
92 @param[in] This A pointer to the EFI_MP_SERVICES_PROTOCOL
93 instance.
94 @param[in] ProcessorNumber The handle number of processor.
95 @param[out] ProcessorInfoBuffer A pointer to the buffer where information for
96 the requested processor is deposited.
97
98 @retval EFI_SUCCESS Processor information was returned.
99 @retval EFI_DEVICE_ERROR The calling processor is an AP.
100 @retval EFI_INVALID_PARAMETER ProcessorInfoBuffer is NULL.
101 @retval EFI_NOT_FOUND The processor with the handle specified by
102 ProcessorNumber does not exist in the platform.
103
104 **/
105 EFI_STATUS
106 EFIAPI
107 GetProcessorInfo (
108 IN EFI_MP_SERVICES_PROTOCOL *This,
109 IN UINTN ProcessorNumber,
110 OUT EFI_PROCESSOR_INFORMATION *ProcessorInfoBuffer
111 )
112 {
113 return MpInitLibGetProcessorInfo (ProcessorNumber, ProcessorInfoBuffer, NULL);
114 }
115
116 /**
117 This service executes a caller provided function on all enabled APs. APs can
118 run either simultaneously or one at a time in sequence. This service supports
119 both blocking and non-blocking requests. The non-blocking requests use EFI
120 events so the BSP can detect when the APs have finished. This service may only
121 be called from the BSP.
122
123 This function is used to dispatch all the enabled APs to the function specified
124 by Procedure. If any enabled AP is busy, then EFI_NOT_READY is returned
125 immediately and Procedure is not started on any AP.
126
127 If SingleThread is TRUE, all the enabled APs execute the function specified by
128 Procedure one by one, in ascending order of processor handle number. Otherwise,
129 all the enabled APs execute the function specified by Procedure simultaneously.
130
131 If WaitEvent is NULL, execution is in blocking mode. The BSP waits until all
132 APs finish or TimeoutInMicroseconds expires. Otherwise, execution is in non-blocking
133 mode, and the BSP returns from this service without waiting for APs. If a
134 non-blocking mode is requested after the UEFI Event EFI_EVENT_GROUP_READY_TO_BOOT
135 is signaled, then EFI_UNSUPPORTED must be returned.
136
137 If the timeout specified by TimeoutInMicroseconds expires before all APs return
138 from Procedure, then Procedure on the failed APs is terminated. All enabled APs
139 are always available for further calls to EFI_MP_SERVICES_PROTOCOL.StartupAllAPs()
140 and EFI_MP_SERVICES_PROTOCOL.StartupThisAP(). If FailedCpuList is not NULL, its
141 content points to the list of processor handle numbers in which Procedure was
142 terminated.
143
144 Note: It is the responsibility of the consumer of the EFI_MP_SERVICES_PROTOCOL.StartupAllAPs()
145 to make sure that the nature of the code that is executed on the BSP and the
146 dispatched APs is well controlled. The MP Services Protocol does not guarantee
147 that the Procedure function is MP-safe. Hence, the tasks that can be run in
148 parallel are limited to certain independent tasks and well-controlled exclusive
149 code. EFI services and protocols may not be called by APs unless otherwise
150 specified.
151
152 In blocking execution mode, BSP waits until all APs finish or
153 TimeoutInMicroseconds expires.
154
155 In non-blocking execution mode, BSP is freed to return to the caller and then
156 proceed to the next task without having to wait for APs. The following
157 sequence needs to occur in a non-blocking execution mode:
158
159 -# The caller that intends to use this MP Services Protocol in non-blocking
160 mode creates WaitEvent by calling the EFI CreateEvent() service. The caller
161 invokes EFI_MP_SERVICES_PROTOCOL.StartupAllAPs(). If the parameter WaitEvent
162 is not NULL, then StartupAllAPs() executes in non-blocking mode. It requests
163 the function specified by Procedure to be started on all the enabled APs,
164 and releases the BSP to continue with other tasks.
165 -# The caller can use the CheckEvent() and WaitForEvent() services to check
166 the state of the WaitEvent created in step 1.
167 -# When the APs complete their task or TimeoutInMicroSeconds expires, the MP
168 Service signals WaitEvent by calling the EFI SignalEvent() function. If
169 FailedCpuList is not NULL, its content is available when WaitEvent is
170 signaled. If all APs returned from Procedure prior to the timeout, then
171 FailedCpuList is set to NULL. If not all APs return from Procedure before
172 the timeout, then FailedCpuList is filled in with the list of the failed
173 APs. The buffer is allocated by MP Service Protocol using AllocatePool().
174 It is the caller's responsibility to free the buffer with FreePool() service.
175 -# This invocation of SignalEvent() function informs the caller that invoked
176 EFI_MP_SERVICES_PROTOCOL.StartupAllAPs() that either all the APs completed
177 the specified task or a timeout occurred. The contents of FailedCpuList
178 can be examined to determine which APs did not complete the specified task
179 prior to the timeout.
180
181 @param[in] This A pointer to the EFI_MP_SERVICES_PROTOCOL
182 instance.
183 @param[in] Procedure A pointer to the function to be run on
184 enabled APs of the system. See type
185 EFI_AP_PROCEDURE.
186 @param[in] SingleThread If TRUE, then all the enabled APs execute
187 the function specified by Procedure one by
188 one, in ascending order of processor handle
189 number. If FALSE, then all the enabled APs
190 execute the function specified by Procedure
191 simultaneously.
192 @param[in] WaitEvent The event created by the caller with CreateEvent()
193 service. If it is NULL, then execute in
194 blocking mode. BSP waits until all APs finish
195 or TimeoutInMicroseconds expires. If it's
196 not NULL, then execute in non-blocking mode.
197 BSP requests the function specified by
198 Procedure to be started on all the enabled
199 APs, and go on executing immediately. If
200 all return from Procedure, or TimeoutInMicroseconds
201 expires, this event is signaled. The BSP
202 can use the CheckEvent() or WaitForEvent()
203 services to check the state of event. Type
204 EFI_EVENT is defined in CreateEvent() in
205 the Unified Extensible Firmware Interface
206 Specification.
207 @param[in] TimeoutInMicroseconds Indicates the time limit in microseconds for
208 APs to return from Procedure, either for
209 blocking or non-blocking mode. Zero means
210 infinity. If the timeout expires before
211 all APs return from Procedure, then Procedure
212 on the failed APs is terminated. All enabled
213 APs are available for next function assigned
214 by EFI_MP_SERVICES_PROTOCOL.StartupAllAPs()
215 or EFI_MP_SERVICES_PROTOCOL.StartupThisAP().
216 If the timeout expires in blocking mode,
217 BSP returns EFI_TIMEOUT. If the timeout
218 expires in non-blocking mode, WaitEvent
219 is signaled with SignalEvent().
220 @param[in] ProcedureArgument The parameter passed into Procedure for
221 all APs.
222 @param[out] FailedCpuList If NULL, this parameter is ignored. Otherwise,
223 if all APs finish successfully, then its
224 content is set to NULL. If not all APs
225 finish before timeout expires, then its
226 content is set to address of the buffer
227 holding handle numbers of the failed APs.
228 The buffer is allocated by MP Service Protocol,
229 and it's the caller's responsibility to
230 free the buffer with FreePool() service.
231 In blocking mode, it is ready for consumption
232 when the call returns. In non-blocking mode,
233 it is ready when WaitEvent is signaled. The
234 list of failed CPU is terminated by
235 END_OF_CPU_LIST.
236
237 @retval EFI_SUCCESS In blocking mode, all APs have finished before
238 the timeout expired.
239 @retval EFI_SUCCESS In non-blocking mode, function has been dispatched
240 to all enabled APs.
241 @retval EFI_UNSUPPORTED A non-blocking mode request was made after the
242 UEFI event EFI_EVENT_GROUP_READY_TO_BOOT was
243 signaled.
244 @retval EFI_DEVICE_ERROR Caller processor is AP.
245 @retval EFI_NOT_STARTED No enabled APs exist in the system.
246 @retval EFI_NOT_READY Any enabled APs are busy.
247 @retval EFI_TIMEOUT In blocking mode, the timeout expired before
248 all enabled APs have finished.
249 @retval EFI_INVALID_PARAMETER Procedure is NULL.
250
251 **/
252 EFI_STATUS
253 EFIAPI
254 StartupAllAPs (
255 IN EFI_MP_SERVICES_PROTOCOL *This,
256 IN EFI_AP_PROCEDURE Procedure,
257 IN BOOLEAN SingleThread,
258 IN EFI_EVENT WaitEvent OPTIONAL,
259 IN UINTN TimeoutInMicroseconds,
260 IN VOID *ProcedureArgument OPTIONAL,
261 OUT UINTN **FailedCpuList OPTIONAL
262 )
263 {
264 return MpInitLibStartupAllAPs (
265 Procedure,
266 SingleThread,
267 WaitEvent,
268 TimeoutInMicroseconds,
269 ProcedureArgument,
270 FailedCpuList
271 );
272 }
273
274 /**
275 This service lets the caller get one enabled AP to execute a caller-provided
276 function. The caller can request the BSP to either wait for the completion
277 of the AP or just proceed with the next task by using the EFI event mechanism.
278 See EFI_MP_SERVICES_PROTOCOL.StartupAllAPs() for more details on non-blocking
279 execution support. This service may only be called from the BSP.
280
281 This function is used to dispatch one enabled AP to the function specified by
282 Procedure passing in the argument specified by ProcedureArgument. If WaitEvent
283 is NULL, execution is in blocking mode. The BSP waits until the AP finishes or
284 TimeoutInMicroSeconds expires. Otherwise, execution is in non-blocking mode.
285 BSP proceeds to the next task without waiting for the AP. If a non-blocking mode
286 is requested after the UEFI Event EFI_EVENT_GROUP_READY_TO_BOOT is signaled,
287 then EFI_UNSUPPORTED must be returned.
288
289 If the timeout specified by TimeoutInMicroseconds expires before the AP returns
290 from Procedure, then execution of Procedure by the AP is terminated. The AP is
291 available for subsequent calls to EFI_MP_SERVICES_PROTOCOL.StartupAllAPs() and
292 EFI_MP_SERVICES_PROTOCOL.StartupThisAP().
293
294 @param[in] This A pointer to the EFI_MP_SERVICES_PROTOCOL
295 instance.
296 @param[in] Procedure A pointer to the function to be run on the
297 designated AP of the system. See type
298 EFI_AP_PROCEDURE.
299 @param[in] ProcessorNumber The handle number of the AP. The range is
300 from 0 to the total number of logical
301 processors minus 1. The total number of
302 logical processors can be retrieved by
303 EFI_MP_SERVICES_PROTOCOL.GetNumberOfProcessors().
304 @param[in] WaitEvent The event created by the caller with CreateEvent()
305 service. If it is NULL, then execute in
306 blocking mode. BSP waits until this AP finish
307 or TimeoutInMicroSeconds expires. If it's
308 not NULL, then execute in non-blocking mode.
309 BSP requests the function specified by
310 Procedure to be started on this AP,
311 and go on executing immediately. If this AP
312 return from Procedure or TimeoutInMicroSeconds
313 expires, this event is signaled. The BSP
314 can use the CheckEvent() or WaitForEvent()
315 services to check the state of event. Type
316 EFI_EVENT is defined in CreateEvent() in
317 the Unified Extensible Firmware Interface
318 Specification.
319 @param[in] TimeoutInMicroseconds Indicates the time limit in microseconds for
320 this AP to finish this Procedure, either for
321 blocking or non-blocking mode. Zero means
322 infinity. If the timeout expires before
323 this AP returns from Procedure, then Procedure
324 on the AP is terminated. The
325 AP is available for next function assigned
326 by EFI_MP_SERVICES_PROTOCOL.StartupAllAPs()
327 or EFI_MP_SERVICES_PROTOCOL.StartupThisAP().
328 If the timeout expires in blocking mode,
329 BSP returns EFI_TIMEOUT. If the timeout
330 expires in non-blocking mode, WaitEvent
331 is signaled with SignalEvent().
332 @param[in] ProcedureArgument The parameter passed into Procedure on the
333 specified AP.
334 @param[out] Finished If NULL, this parameter is ignored. In
335 blocking mode, this parameter is ignored.
336 In non-blocking mode, if AP returns from
337 Procedure before the timeout expires, its
338 content is set to TRUE. Otherwise, the
339 value is set to FALSE. The caller can
340 determine if the AP returned from Procedure
341 by evaluating this value.
342
343 @retval EFI_SUCCESS In blocking mode, specified AP finished before
344 the timeout expires.
345 @retval EFI_SUCCESS In non-blocking mode, the function has been
346 dispatched to specified AP.
347 @retval EFI_UNSUPPORTED A non-blocking mode request was made after the
348 UEFI event EFI_EVENT_GROUP_READY_TO_BOOT was
349 signaled.
350 @retval EFI_DEVICE_ERROR The calling processor is an AP.
351 @retval EFI_TIMEOUT In blocking mode, the timeout expired before
352 the specified AP has finished.
353 @retval EFI_NOT_READY The specified AP is busy.
354 @retval EFI_NOT_FOUND The processor with the handle specified by
355 ProcessorNumber does not exist.
356 @retval EFI_INVALID_PARAMETER ProcessorNumber specifies the BSP or disabled AP.
357 @retval EFI_INVALID_PARAMETER Procedure is NULL.
358
359 **/
360 EFI_STATUS
361 EFIAPI
362 StartupThisAP (
363 IN EFI_MP_SERVICES_PROTOCOL *This,
364 IN EFI_AP_PROCEDURE Procedure,
365 IN UINTN ProcessorNumber,
366 IN EFI_EVENT WaitEvent OPTIONAL,
367 IN UINTN TimeoutInMicroseconds,
368 IN VOID *ProcedureArgument OPTIONAL,
369 OUT BOOLEAN *Finished OPTIONAL
370 )
371 {
372 return MpInitLibStartupThisAP (
373 Procedure,
374 ProcessorNumber,
375 WaitEvent,
376 TimeoutInMicroseconds,
377 ProcedureArgument,
378 Finished
379 );
380 }
381
382 /**
383 This service switches the requested AP to be the BSP from that point onward.
384 This service changes the BSP for all purposes. This call can only be performed
385 by the current BSP.
386
387 This service switches the requested AP to be the BSP from that point onward.
388 This service changes the BSP for all purposes. The new BSP can take over the
389 execution of the old BSP and continue seamlessly from where the old one left
390 off. This service may not be supported after the UEFI Event EFI_EVENT_GROUP_READY_TO_BOOT
391 is signaled.
392
393 If the BSP cannot be switched prior to the return from this service, then
394 EFI_UNSUPPORTED must be returned.
395
396 @param[in] This A pointer to the EFI_MP_SERVICES_PROTOCOL instance.
397 @param[in] ProcessorNumber The handle number of AP that is to become the new
398 BSP. The range is from 0 to the total number of
399 logical processors minus 1. The total number of
400 logical processors can be retrieved by
401 EFI_MP_SERVICES_PROTOCOL.GetNumberOfProcessors().
402 @param[in] EnableOldBSP If TRUE, then the old BSP will be listed as an
403 enabled AP. Otherwise, it will be disabled.
404
405 @retval EFI_SUCCESS BSP successfully switched.
406 @retval EFI_UNSUPPORTED Switching the BSP cannot be completed prior to
407 this service returning.
408 @retval EFI_UNSUPPORTED Switching the BSP is not supported.
409 @retval EFI_DEVICE_ERROR The calling processor is an AP.
410 @retval EFI_NOT_FOUND The processor with the handle specified by
411 ProcessorNumber does not exist.
412 @retval EFI_INVALID_PARAMETER ProcessorNumber specifies the current BSP or
413 a disabled AP.
414 @retval EFI_NOT_READY The specified AP is busy.
415
416 **/
417 EFI_STATUS
418 EFIAPI
419 SwitchBSP (
420 IN EFI_MP_SERVICES_PROTOCOL *This,
421 IN UINTN ProcessorNumber,
422 IN BOOLEAN EnableOldBSP
423 )
424 {
425 return MpInitLibSwitchBSP (ProcessorNumber, EnableOldBSP);
426 }
427
428 /**
429 This service lets the caller enable or disable an AP from this point onward.
430 This service may only be called from the BSP.
431
432 This service allows the caller enable or disable an AP from this point onward.
433 The caller can optionally specify the health status of the AP by Health. If
434 an AP is being disabled, then the state of the disabled AP is implementation
435 dependent. If an AP is enabled, then the implementation must guarantee that a
436 complete initialization sequence is performed on the AP, so the AP is in a state
437 that is compatible with an MP operating system. This service may not be supported
438 after the UEFI Event EFI_EVENT_GROUP_READY_TO_BOOT is signaled.
439
440 If the enable or disable AP operation cannot be completed prior to the return
441 from this service, then EFI_UNSUPPORTED must be returned.
442
443 @param[in] This A pointer to the EFI_MP_SERVICES_PROTOCOL instance.
444 @param[in] ProcessorNumber The handle number of AP.
445 The range is from 0 to the total number of
446 logical processors minus 1. The total number of
447 logical processors can be retrieved by
448 EFI_MP_SERVICES_PROTOCOL.GetNumberOfProcessors().
449 @param[in] EnableAP Specifies the new state for the processor for
450 enabled, FALSE for disabled.
451 @param[in] HealthFlag If not NULL, a pointer to a value that specifies
452 the new health status of the AP. This flag
453 corresponds to StatusFlag defined in
454 EFI_MP_SERVICES_PROTOCOL.GetProcessorInfo(). Only
455 the PROCESSOR_HEALTH_STATUS_BIT is used. All other
456 bits are ignored. If it is NULL, this parameter
457 is ignored.
458
459 @retval EFI_SUCCESS The specified AP was enabled or disabled successfully.
460 @retval EFI_UNSUPPORTED Enabling or disabling an AP cannot be completed
461 prior to this service returning.
462 @retval EFI_UNSUPPORTED Enabling or disabling an AP is not supported.
463 @retval EFI_DEVICE_ERROR The calling processor is an AP.
464 @retval EFI_NOT_FOUND Processor with the handle specified by ProcessorNumber
465 does not exist.
466 @retval EFI_INVALID_PARAMETER ProcessorNumber specifies the BSP.
467
468 **/
469 EFI_STATUS
470 EFIAPI
471 EnableDisableAP (
472 IN EFI_MP_SERVICES_PROTOCOL *This,
473 IN UINTN ProcessorNumber,
474 IN BOOLEAN EnableAP,
475 IN UINT32 *HealthFlag OPTIONAL
476 )
477 {
478 return MpInitLibEnableDisableAP (ProcessorNumber, EnableAP, HealthFlag);
479 }
480
481 /**
482 This return the handle number for the calling processor. This service may be
483 called from the BSP and APs.
484
485 This service returns the processor handle number for the calling processor.
486 The returned value is in the range from 0 to the total number of logical
487 processors minus 1. The total number of logical processors can be retrieved
488 with EFI_MP_SERVICES_PROTOCOL.GetNumberOfProcessors(). This service may be
489 called from the BSP and APs. If ProcessorNumber is NULL, then EFI_INVALID_PARAMETER
490 is returned. Otherwise, the current processors handle number is returned in
491 ProcessorNumber, and EFI_SUCCESS is returned.
492
493 @param[in] This A pointer to the EFI_MP_SERVICES_PROTOCOL instance.
494 @param[out] ProcessorNumber Pointer to the handle number of AP.
495 The range is from 0 to the total number of
496 logical processors minus 1. The total number of
497 logical processors can be retrieved by
498 EFI_MP_SERVICES_PROTOCOL.GetNumberOfProcessors().
499
500 @retval EFI_SUCCESS The current processor handle number was returned
501 in ProcessorNumber.
502 @retval EFI_INVALID_PARAMETER ProcessorNumber is NULL.
503
504 **/
505 EFI_STATUS
506 EFIAPI
507 WhoAmI (
508 IN EFI_MP_SERVICES_PROTOCOL *This,
509 OUT UINTN *ProcessorNumber
510 )
511 {
512 return MpInitLibWhoAmI (ProcessorNumber);
513 }
514
515 /**
516 Collects BIST data from HOB.
517
518 This function collects BIST data from HOB built from Sec Platform Information
519 PPI or SEC Platform Information2 PPI.
520
521 **/
522 VOID
523 CollectBistDataFromHob (
524 VOID
525 )
526 {
527 EFI_HOB_GUID_TYPE *GuidHob;
528 EFI_SEC_PLATFORM_INFORMATION_RECORD2 *SecPlatformInformation2;
529 EFI_SEC_PLATFORM_INFORMATION_RECORD *SecPlatformInformation;
530 UINTN NumberOfData;
531 EFI_SEC_PLATFORM_INFORMATION_CPU *CpuInstance;
532 EFI_SEC_PLATFORM_INFORMATION_CPU BspCpuInstance;
533 UINTN ProcessorNumber;
534 EFI_PROCESSOR_INFORMATION ProcessorInfo;
535 EFI_HEALTH_FLAGS BistData;
536 UINTN CpuInstanceNumber;
537
538 SecPlatformInformation2 = NULL;
539 SecPlatformInformation = NULL;
540
541 //
542 // Get gEfiSecPlatformInformation2PpiGuid Guided HOB firstly
543 //
544 GuidHob = GetFirstGuidHob (&gEfiSecPlatformInformation2PpiGuid);
545 if (GuidHob != NULL) {
546 //
547 // Sec Platform Information2 PPI includes BSP/APs' BIST information
548 //
549 SecPlatformInformation2 = GET_GUID_HOB_DATA (GuidHob);
550 NumberOfData = SecPlatformInformation2->NumberOfCpus;
551 CpuInstance = SecPlatformInformation2->CpuInstance;
552 } else {
553 //
554 // Otherwise, get gEfiSecPlatformInformationPpiGuid Guided HOB
555 //
556 GuidHob = GetFirstGuidHob (&gEfiSecPlatformInformationPpiGuid);
557 if (GuidHob != NULL) {
558 SecPlatformInformation = GET_GUID_HOB_DATA (GuidHob);
559 NumberOfData = 1;
560 //
561 // SEC Platform Information only includes BSP's BIST information
562 // does not have BSP's APIC ID
563 //
564 BspCpuInstance.CpuLocation = GetApicId ();
565 BspCpuInstance.InfoRecord.IA32HealthFlags.Uint32 = SecPlatformInformation->IA32HealthFlags.Uint32;
566 CpuInstance = &BspCpuInstance;
567 } else {
568 DEBUG ((DEBUG_INFO, "Does not find any HOB stored CPU BIST information!\n"));
569 //
570 // Does not find any HOB stored BIST information
571 //
572 return;
573 }
574 }
575
576 for (ProcessorNumber = 0; ProcessorNumber < mNumberOfProcessors; ProcessorNumber++) {
577 MpInitLibGetProcessorInfo (ProcessorNumber, &ProcessorInfo, &BistData);
578 for (CpuInstanceNumber = 0; CpuInstanceNumber < NumberOfData; CpuInstanceNumber++) {
579 if (ProcessorInfo.ProcessorId == CpuInstance[CpuInstanceNumber].CpuLocation) {
580 //
581 // Update CPU health status for MP Services Protocol according to BIST data.
582 //
583 BistData = CpuInstance[CpuInstanceNumber].InfoRecord.IA32HealthFlags;
584 }
585 }
586
587 if (BistData.Uint32 != 0) {
588 //
589 // Report Status Code that self test is failed
590 //
591 REPORT_STATUS_CODE (
592 EFI_ERROR_CODE | EFI_ERROR_MAJOR,
593 (EFI_COMPUTING_UNIT_HOST_PROCESSOR | EFI_CU_HP_EC_SELF_TEST)
594 );
595 }
596 }
597 }
598
599 /**
600 Get GDT register value.
601
602 This function is mainly for AP purpose because AP may have different GDT
603 table than BSP.
604
605 @param[in,out] Buffer The pointer to private data buffer.
606
607 **/
608 VOID
609 EFIAPI
610 GetGdtr (
611 IN OUT VOID *Buffer
612 )
613 {
614 AsmReadGdtr ((IA32_DESCRIPTOR *)Buffer);
615 }
616
617 /**
618 Initializes CPU exceptions handlers for the sake of stack switch requirement.
619
620 This function is a wrapper of InitializeSeparateExceptionStacks. It's mainly
621 for the sake of AP's init because of EFI_AP_PROCEDURE API requirement.
622
623 @param[in,out] Buffer The pointer to private data buffer.
624
625 **/
626 VOID
627 EFIAPI
628 InitializeExceptionStackSwitchHandlers (
629 IN OUT VOID *Buffer
630 )
631 {
632 CPU_EXCEPTION_INIT_DATA *EssData;
633 IA32_DESCRIPTOR Idtr;
634 EFI_STATUS Status;
635
636 EssData = Buffer;
637 //
638 // We don't plan to replace IDT table with a new one, but we should not assume
639 // the AP's IDT is the same as BSP's IDT either.
640 //
641 AsmReadIdtr (&Idtr);
642 EssData->Ia32.IdtTable = (VOID *)Idtr.Base;
643 EssData->Ia32.IdtTableSize = Idtr.Limit + 1;
644 Status = InitializeSeparateExceptionStacks (EssData);
645 ASSERT_EFI_ERROR (Status);
646 }
647
648 /**
649 Initializes MP exceptions handlers for the sake of stack switch requirement.
650
651 This function will allocate required resources required to setup stack switch
652 and pass them through CPU_EXCEPTION_INIT_DATA to each logic processor.
653
654 **/
655 VOID
656 InitializeMpExceptionStackSwitchHandlers (
657 VOID
658 )
659 {
660 UINTN Index;
661 UINTN Bsp;
662 UINTN ExceptionNumber;
663 UINTN OldGdtSize;
664 UINTN NewGdtSize;
665 UINTN NewStackSize;
666 IA32_DESCRIPTOR Gdtr;
667 CPU_EXCEPTION_INIT_DATA EssData;
668 UINT8 *GdtBuffer;
669 UINT8 *StackTop;
670
671 ExceptionNumber = FixedPcdGetSize (PcdCpuStackSwitchExceptionList);
672 NewStackSize = FixedPcdGet32 (PcdCpuKnownGoodStackSize) * ExceptionNumber;
673
674 StackTop = AllocateRuntimeZeroPool (NewStackSize * mNumberOfProcessors);
675 ASSERT (StackTop != NULL);
676 StackTop += NewStackSize * mNumberOfProcessors;
677
678 //
679 // The default exception handlers must have been initialized. Let's just skip
680 // it in this method.
681 //
682 EssData.Ia32.Revision = CPU_EXCEPTION_INIT_DATA_REV;
683 EssData.Ia32.InitDefaultHandlers = FALSE;
684
685 EssData.Ia32.StackSwitchExceptions = FixedPcdGetPtr (PcdCpuStackSwitchExceptionList);
686 EssData.Ia32.StackSwitchExceptionNumber = ExceptionNumber;
687 EssData.Ia32.KnownGoodStackSize = FixedPcdGet32 (PcdCpuKnownGoodStackSize);
688
689 //
690 // Initialize Gdtr to suppress incorrect compiler/analyzer warnings.
691 //
692 Gdtr.Base = 0;
693 Gdtr.Limit = 0;
694 MpInitLibWhoAmI (&Bsp);
695 for (Index = 0; Index < mNumberOfProcessors; ++Index) {
696 //
697 // To support stack switch, we need to re-construct GDT but not IDT.
698 //
699 if (Index == Bsp) {
700 GetGdtr (&Gdtr);
701 } else {
702 //
703 // AP might have different size of GDT from BSP.
704 //
705 MpInitLibStartupThisAP (GetGdtr, Index, NULL, 0, (VOID *)&Gdtr, NULL);
706 }
707
708 //
709 // X64 needs only one TSS of current task working for all exceptions
710 // because of its IST feature. IA32 needs one TSS for each exception
711 // in addition to current task. Since AP is not supposed to allocate
712 // memory, we have to do it in BSP. To simplify the code, we allocate
713 // memory for IA32 case to cover both IA32 and X64 exception stack
714 // switch.
715 //
716 // Layout of memory to allocate for each processor:
717 // --------------------------------
718 // | Alignment | (just in case)
719 // --------------------------------
720 // | |
721 // | Original GDT |
722 // | |
723 // --------------------------------
724 // | Current task descriptor |
725 // --------------------------------
726 // | |
727 // | Exception task descriptors | X ExceptionNumber
728 // | |
729 // --------------------------------
730 // | Current task-state segment |
731 // --------------------------------
732 // | |
733 // | Exception task-state segment | X ExceptionNumber
734 // | |
735 // --------------------------------
736 //
737 OldGdtSize = Gdtr.Limit + 1;
738 EssData.Ia32.ExceptionTssDescSize = sizeof (IA32_TSS_DESCRIPTOR) *
739 (ExceptionNumber + 1);
740 EssData.Ia32.ExceptionTssSize = sizeof (IA32_TASK_STATE_SEGMENT) *
741 (ExceptionNumber + 1);
742 NewGdtSize = sizeof (IA32_TSS_DESCRIPTOR) +
743 OldGdtSize +
744 EssData.Ia32.ExceptionTssDescSize +
745 EssData.Ia32.ExceptionTssSize;
746
747 GdtBuffer = AllocateRuntimeZeroPool (NewGdtSize);
748 ASSERT (GdtBuffer != NULL);
749
750 //
751 // Make sure GDT table alignment
752 //
753 EssData.Ia32.GdtTable = ALIGN_POINTER (GdtBuffer, sizeof (IA32_TSS_DESCRIPTOR));
754 NewGdtSize -= ((UINT8 *)EssData.Ia32.GdtTable - GdtBuffer);
755 EssData.Ia32.GdtTableSize = NewGdtSize;
756
757 EssData.Ia32.ExceptionTssDesc = ((UINT8 *)EssData.Ia32.GdtTable + OldGdtSize);
758 EssData.Ia32.ExceptionTss = ((UINT8 *)EssData.Ia32.GdtTable + OldGdtSize +
759 EssData.Ia32.ExceptionTssDescSize);
760
761 EssData.Ia32.KnownGoodStackTop = (UINTN)StackTop;
762 DEBUG ((
763 DEBUG_INFO,
764 "Exception stack top[cpu%lu]: 0x%lX\n",
765 (UINT64)(UINTN)Index,
766 (UINT64)(UINTN)StackTop
767 ));
768
769 if (Index == Bsp) {
770 InitializeExceptionStackSwitchHandlers (&EssData);
771 } else {
772 MpInitLibStartupThisAP (
773 InitializeExceptionStackSwitchHandlers,
774 Index,
775 NULL,
776 0,
777 (VOID *)&EssData,
778 NULL
779 );
780 }
781
782 StackTop -= NewStackSize;
783 }
784 }
785
786 /**
787 Initializes MP exceptions handlers for special features, such as Heap Guard
788 and Stack Guard.
789 **/
790 VOID
791 InitializeMpExceptionHandlers (
792 VOID
793 )
794 {
795 //
796 // Enable non-stop mode for #PF triggered by Heap Guard or NULL Pointer
797 // Detection.
798 //
799 if (HEAP_GUARD_NONSTOP_MODE || NULL_DETECTION_NONSTOP_MODE) {
800 RegisterCpuInterruptHandler (EXCEPT_IA32_DEBUG, DebugExceptionHandler);
801 RegisterCpuInterruptHandler (EXCEPT_IA32_PAGE_FAULT, PageFaultExceptionHandler);
802 }
803
804 //
805 // Setup stack switch for Stack Guard feature.
806 //
807 if (PcdGetBool (PcdCpuStackGuard)) {
808 InitializeMpExceptionStackSwitchHandlers ();
809 }
810 }
811
812 /**
813 Initialize Multi-processor support.
814
815 **/
816 VOID
817 InitializeMpSupport (
818 VOID
819 )
820 {
821 EFI_STATUS Status;
822 UINTN NumberOfProcessors;
823 UINTN NumberOfEnabledProcessors;
824
825 //
826 // Wakeup APs to do initialization
827 //
828 Status = MpInitLibInitialize ();
829 ASSERT_EFI_ERROR (Status);
830
831 MpInitLibGetNumberOfProcessors (&NumberOfProcessors, &NumberOfEnabledProcessors);
832 mNumberOfProcessors = NumberOfProcessors;
833 DEBUG ((DEBUG_INFO, "Detect CPU count: %d\n", mNumberOfProcessors));
834
835 //
836 // Initialize special exception handlers for each logic processor.
837 //
838 InitializeMpExceptionHandlers ();
839
840 //
841 // Update CPU healthy information from Guided HOB
842 //
843 CollectBistDataFromHob ();
844
845 Status = gBS->InstallMultipleProtocolInterfaces (
846 &mMpServiceHandle,
847 &gEfiMpServiceProtocolGuid,
848 &mMpServicesTemplate,
849 NULL
850 );
851 ASSERT_EFI_ERROR (Status);
852 }