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