]> git.proxmox.com Git - mirror_edk2.git/blame - UefiCpuPkg/CpuMpPei/CpuMpPei.c
UefiCpuPkg: Replace BSD License with BSD+Patent License
[mirror_edk2.git] / UefiCpuPkg / CpuMpPei / CpuMpPei.c
CommitLineData
ea0f431c
JF
1/** @file\r
2 CPU PEI Module installs CPU Multiple Processor PPI.\r
3\r
7367cc6c 4 Copyright (c) 2015 - 2018, Intel Corporation. All rights reserved.<BR>\r
0acd8697 5 SPDX-License-Identifier: BSD-2-Clause-Patent\r
ea0f431c
JF
6\r
7**/\r
8\r
9#include "CpuMpPei.h"\r
89fa1bf2
JF
10\r
11//\r
12// CPU MP PPI to be installed\r
13//\r
14EFI_PEI_MP_SERVICES_PPI mMpServicesPpi = {\r
15 PeiGetNumberOfProcessors,\r
16 PeiGetProcessorInfo,\r
17 PeiStartupAllAPs,\r
18 PeiStartupThisAP,\r
19 PeiSwitchBSP,\r
20 PeiEnableDisableAP,\r
21 PeiWhoAmI,\r
22};\r
23\r
24EFI_PEI_PPI_DESCRIPTOR mPeiCpuMpPpiDesc = {\r
25 (EFI_PEI_PPI_DESCRIPTOR_PPI | EFI_PEI_PPI_DESCRIPTOR_TERMINATE_LIST),\r
26 &gEfiPeiMpServicesPpiGuid,\r
27 &mMpServicesPpi\r
28};\r
29\r
30/**\r
31 This service retrieves the number of logical processor in the platform\r
32 and the number of those logical processors that are enabled on this boot.\r
33 This service may only be called from the BSP.\r
34\r
35 This function is used to retrieve the following information:\r
36 - The number of logical processors that are present in the system.\r
37 - The number of enabled logical processors in the system at the instant\r
38 this call is made.\r
39\r
40 Because MP Service Ppi provides services to enable and disable processors\r
41 dynamically, the number of enabled logical processors may vary during the\r
42 course of a boot session.\r
43\r
44 If this service is called from an AP, then EFI_DEVICE_ERROR is returned.\r
45 If NumberOfProcessors or NumberOfEnabledProcessors is NULL, then\r
46 EFI_INVALID_PARAMETER is returned. Otherwise, the total number of processors\r
47 is returned in NumberOfProcessors, the number of currently enabled processor\r
48 is returned in NumberOfEnabledProcessors, and EFI_SUCCESS is returned.\r
49\r
50 @param[in] PeiServices An indirect pointer to the PEI Services Table\r
51 published by the PEI Foundation.\r
52 @param[in] This Pointer to this instance of the PPI.\r
53 @param[out] NumberOfProcessors Pointer to the total number of logical processors in\r
54 the system, including the BSP and disabled APs.\r
55 @param[out] NumberOfEnabledProcessors\r
56 Number of processors in the system that are enabled.\r
57\r
58 @retval EFI_SUCCESS The number of logical processors and enabled\r
59 logical processors was retrieved.\r
60 @retval EFI_DEVICE_ERROR The calling processor is an AP.\r
61 @retval EFI_INVALID_PARAMETER NumberOfProcessors is NULL.\r
62 NumberOfEnabledProcessors is NULL.\r
63**/\r
64EFI_STATUS\r
65EFIAPI\r
66PeiGetNumberOfProcessors (\r
67 IN CONST EFI_PEI_SERVICES **PeiServices,\r
68 IN EFI_PEI_MP_SERVICES_PPI *This,\r
69 OUT UINTN *NumberOfProcessors,\r
70 OUT UINTN *NumberOfEnabledProcessors\r
71 )\r
72{\r
73 if ((NumberOfProcessors == NULL) || (NumberOfEnabledProcessors == NULL)) {\r
74 return EFI_INVALID_PARAMETER;\r
75 }\r
76\r
77 return MpInitLibGetNumberOfProcessors (\r
78 NumberOfProcessors,\r
79 NumberOfEnabledProcessors\r
80 );\r
81}\r
82\r
83/**\r
84 Gets detailed MP-related information on the requested processor at the\r
85 instant this call is made. This service may only be called from the BSP.\r
86\r
87 This service retrieves detailed MP-related information about any processor\r
88 on the platform. Note the following:\r
89 - The processor information may change during the course of a boot session.\r
90 - The information presented here is entirely MP related.\r
91\r
92 Information regarding the number of caches and their sizes, frequency of operation,\r
93 slot numbers is all considered platform-related information and is not provided\r
94 by this service.\r
95\r
96 @param[in] PeiServices An indirect pointer to the PEI Services Table\r
97 published by the PEI Foundation.\r
98 @param[in] This Pointer to this instance of the PPI.\r
99 @param[in] ProcessorNumber Pointer to the total number of logical processors in\r
100 the system, including the BSP and disabled APs.\r
101 @param[out] ProcessorInfoBuffer Number of processors in the system that are enabled.\r
102\r
103 @retval EFI_SUCCESS Processor information was returned.\r
104 @retval EFI_DEVICE_ERROR The calling processor is an AP.\r
105 @retval EFI_INVALID_PARAMETER ProcessorInfoBuffer is NULL.\r
106 @retval EFI_NOT_FOUND The processor with the handle specified by\r
107 ProcessorNumber does not exist in the platform.\r
108**/\r
109EFI_STATUS\r
110EFIAPI\r
111PeiGetProcessorInfo (\r
112 IN CONST EFI_PEI_SERVICES **PeiServices,\r
113 IN EFI_PEI_MP_SERVICES_PPI *This,\r
114 IN UINTN ProcessorNumber,\r
115 OUT EFI_PROCESSOR_INFORMATION *ProcessorInfoBuffer\r
116 )\r
117{\r
118 return MpInitLibGetProcessorInfo (ProcessorNumber, ProcessorInfoBuffer, NULL);\r
119}\r
120\r
121/**\r
122 This service executes a caller provided function on all enabled APs. APs can\r
123 run either simultaneously or one at a time in sequence. This service supports\r
124 both blocking requests only. This service may only\r
125 be called from the BSP.\r
126\r
127 This function is used to dispatch all the enabled APs to the function specified\r
128 by Procedure. If any enabled AP is busy, then EFI_NOT_READY is returned\r
129 immediately and Procedure is not started on any AP.\r
130\r
131 If SingleThread is TRUE, all the enabled APs execute the function specified by\r
132 Procedure one by one, in ascending order of processor handle number. Otherwise,\r
133 all the enabled APs execute the function specified by Procedure simultaneously.\r
134\r
135 If the timeout specified by TimeoutInMicroSeconds expires before all APs return\r
136 from Procedure, then Procedure on the failed APs is terminated. All enabled APs\r
137 are always available for further calls to EFI_PEI_MP_SERVICES_PPI.StartupAllAPs()\r
138 and EFI_PEI_MP_SERVICES_PPI.StartupThisAP(). If FailedCpuList is not NULL, its\r
139 content points to the list of processor handle numbers in which Procedure was\r
140 terminated.\r
141\r
142 Note: It is the responsibility of the consumer of the EFI_PEI_MP_SERVICES_PPI.StartupAllAPs()\r
143 to make sure that the nature of the code that is executed on the BSP and the\r
144 dispatched APs is well controlled. The MP Services Ppi does not guarantee\r
145 that the Procedure function is MP-safe. Hence, the tasks that can be run in\r
146 parallel are limited to certain independent tasks and well-controlled exclusive\r
147 code. PEI services and Ppis may not be called by APs unless otherwise\r
148 specified.\r
149\r
150 In blocking execution mode, BSP waits until all APs finish or\r
151 TimeoutInMicroSeconds expires.\r
152\r
153 @param[in] PeiServices An indirect pointer to the PEI Services Table\r
154 published by the PEI Foundation.\r
155 @param[in] This A pointer to the EFI_PEI_MP_SERVICES_PPI instance.\r
156 @param[in] Procedure A pointer to the function to be run on enabled APs of\r
157 the system.\r
158 @param[in] SingleThread If TRUE, then all the enabled APs execute the function\r
159 specified by Procedure one by one, in ascending order\r
160 of processor handle number. If FALSE, then all the\r
161 enabled APs execute the function specified by Procedure\r
162 simultaneously.\r
163 @param[in] TimeoutInMicroSeconds\r
164 Indicates the time limit in microseconds for APs to\r
165 return from Procedure, for blocking mode only. Zero\r
166 means infinity. If the timeout expires before all APs\r
167 return from Procedure, then Procedure on the failed APs\r
168 is terminated. All enabled APs are available for next\r
169 function assigned by EFI_PEI_MP_SERVICES_PPI.StartupAllAPs()\r
170 or EFI_PEI_MP_SERVICES_PPI.StartupThisAP(). If the\r
171 timeout expires in blocking mode, BSP returns\r
172 EFI_TIMEOUT.\r
173 @param[in] ProcedureArgument The parameter passed into Procedure for all APs.\r
174\r
175 @retval EFI_SUCCESS In blocking mode, all APs have finished before the\r
176 timeout expired.\r
177 @retval EFI_DEVICE_ERROR Caller processor is AP.\r
178 @retval EFI_NOT_STARTED No enabled APs exist in the system.\r
179 @retval EFI_NOT_READY Any enabled APs are busy.\r
180 @retval EFI_TIMEOUT In blocking mode, the timeout expired before all\r
181 enabled APs have finished.\r
182 @retval EFI_INVALID_PARAMETER Procedure is NULL.\r
183**/\r
184EFI_STATUS\r
185EFIAPI\r
186PeiStartupAllAPs (\r
187 IN CONST EFI_PEI_SERVICES **PeiServices,\r
188 IN EFI_PEI_MP_SERVICES_PPI *This,\r
189 IN EFI_AP_PROCEDURE Procedure,\r
190 IN BOOLEAN SingleThread,\r
191 IN UINTN TimeoutInMicroSeconds,\r
192 IN VOID *ProcedureArgument OPTIONAL\r
193 )\r
194{\r
195 return MpInitLibStartupAllAPs (\r
196 Procedure,\r
197 SingleThread,\r
198 NULL,\r
199 TimeoutInMicroSeconds,\r
200 ProcedureArgument,\r
201 NULL\r
202 );\r
203}\r
204\r
205/**\r
206 This service lets the caller get one enabled AP to execute a caller-provided\r
207 function. The caller can request the BSP to wait for the completion\r
208 of the AP. This service may only be called from the BSP.\r
209\r
210 This function is used to dispatch one enabled AP to the function specified by\r
211 Procedure passing in the argument specified by ProcedureArgument.\r
212 The execution is in blocking mode. The BSP waits until the AP finishes or\r
213 TimeoutInMicroSecondss expires.\r
214\r
215 If the timeout specified by TimeoutInMicroseconds expires before the AP returns\r
216 from Procedure, then execution of Procedure by the AP is terminated. The AP is\r
217 available for subsequent calls to EFI_PEI_MP_SERVICES_PPI.StartupAllAPs() and\r
218 EFI_PEI_MP_SERVICES_PPI.StartupThisAP().\r
219\r
220 @param[in] PeiServices An indirect pointer to the PEI Services Table\r
221 published by the PEI Foundation.\r
222 @param[in] This A pointer to the EFI_PEI_MP_SERVICES_PPI instance.\r
223 @param[in] Procedure A pointer to the function to be run on enabled APs of\r
224 the system.\r
225 @param[in] ProcessorNumber The handle number of the AP. The range is from 0 to the\r
226 total number of logical processors minus 1. The total\r
227 number of logical processors can be retrieved by\r
228 EFI_PEI_MP_SERVICES_PPI.GetNumberOfProcessors().\r
229 @param[in] TimeoutInMicroseconds\r
230 Indicates the time limit in microseconds for APs to\r
231 return from Procedure, for blocking mode only. Zero\r
232 means infinity. If the timeout expires before all APs\r
233 return from Procedure, then Procedure on the failed APs\r
234 is terminated. All enabled APs are available for next\r
235 function assigned by EFI_PEI_MP_SERVICES_PPI.StartupAllAPs()\r
236 or EFI_PEI_MP_SERVICES_PPI.StartupThisAP(). If the\r
237 timeout expires in blocking mode, BSP returns\r
238 EFI_TIMEOUT.\r
239 @param[in] ProcedureArgument The parameter passed into Procedure for all APs.\r
240\r
241 @retval EFI_SUCCESS In blocking mode, specified AP finished before the\r
242 timeout expires.\r
243 @retval EFI_DEVICE_ERROR The calling processor is an AP.\r
244 @retval EFI_TIMEOUT In blocking mode, the timeout expired before the\r
245 specified AP has finished.\r
246 @retval EFI_NOT_FOUND The processor with the handle specified by\r
247 ProcessorNumber does not exist.\r
248 @retval EFI_INVALID_PARAMETER ProcessorNumber specifies the BSP or disabled AP.\r
249 @retval EFI_INVALID_PARAMETER Procedure is NULL.\r
250**/\r
251EFI_STATUS\r
252EFIAPI\r
253PeiStartupThisAP (\r
254 IN CONST EFI_PEI_SERVICES **PeiServices,\r
255 IN EFI_PEI_MP_SERVICES_PPI *This,\r
256 IN EFI_AP_PROCEDURE Procedure,\r
257 IN UINTN ProcessorNumber,\r
258 IN UINTN TimeoutInMicroseconds,\r
259 IN VOID *ProcedureArgument OPTIONAL\r
260 )\r
261{\r
262 return MpInitLibStartupThisAP (\r
263 Procedure,\r
264 ProcessorNumber,\r
265 NULL,\r
266 TimeoutInMicroseconds,\r
267 ProcedureArgument,\r
268 NULL\r
269 );\r
270}\r
271\r
272/**\r
273 This service switches the requested AP to be the BSP from that point onward.\r
274 This service changes the BSP for all purposes. This call can only be performed\r
275 by the current BSP.\r
276\r
277 This service switches the requested AP to be the BSP from that point onward.\r
278 This service changes the BSP for all purposes. The new BSP can take over the\r
279 execution of the old BSP and continue seamlessly from where the old one left\r
280 off.\r
281\r
282 If the BSP cannot be switched prior to the return from this service, then\r
283 EFI_UNSUPPORTED must be returned.\r
284\r
285 @param[in] PeiServices An indirect pointer to the PEI Services Table\r
286 published by the PEI Foundation.\r
287 @param[in] This A pointer to the EFI_PEI_MP_SERVICES_PPI instance.\r
288 @param[in] ProcessorNumber The handle number of the AP. The range is from 0 to the\r
289 total number of logical processors minus 1. The total\r
290 number of logical processors can be retrieved by\r
291 EFI_PEI_MP_SERVICES_PPI.GetNumberOfProcessors().\r
292 @param[in] EnableOldBSP If TRUE, then the old BSP will be listed as an enabled\r
293 AP. Otherwise, it will be disabled.\r
294\r
295 @retval EFI_SUCCESS BSP successfully switched.\r
296 @retval EFI_UNSUPPORTED Switching the BSP cannot be completed prior to this\r
297 service returning.\r
298 @retval EFI_UNSUPPORTED Switching the BSP is not supported.\r
e3ae7f52 299 @retval EFI_DEVICE_ERROR The calling processor is an AP.\r
89fa1bf2
JF
300 @retval EFI_NOT_FOUND The processor with the handle specified by\r
301 ProcessorNumber does not exist.\r
302 @retval EFI_INVALID_PARAMETER ProcessorNumber specifies the current BSP or a disabled\r
303 AP.\r
304 @retval EFI_NOT_READY The specified AP is busy.\r
305**/\r
306EFI_STATUS\r
307EFIAPI\r
308PeiSwitchBSP (\r
309 IN CONST EFI_PEI_SERVICES **PeiServices,\r
310 IN EFI_PEI_MP_SERVICES_PPI *This,\r
311 IN UINTN ProcessorNumber,\r
312 IN BOOLEAN EnableOldBSP\r
313 )\r
314{\r
315 return MpInitLibSwitchBSP (ProcessorNumber, EnableOldBSP);\r
316}\r
317\r
318/**\r
319 This service lets the caller enable or disable an AP from this point onward.\r
320 This service may only be called from the BSP.\r
321\r
322 This service allows the caller enable or disable an AP from this point onward.\r
323 The caller can optionally specify the health status of the AP by Health. If\r
324 an AP is being disabled, then the state of the disabled AP is implementation\r
325 dependent. If an AP is enabled, then the implementation must guarantee that a\r
326 complete initialization sequence is performed on the AP, so the AP is in a state\r
327 that is compatible with an MP operating system.\r
328\r
329 If the enable or disable AP operation cannot be completed prior to the return\r
330 from this service, then EFI_UNSUPPORTED must be returned.\r
331\r
332 @param[in] PeiServices An indirect pointer to the PEI Services Table\r
333 published by the PEI Foundation.\r
334 @param[in] This A pointer to the EFI_PEI_MP_SERVICES_PPI instance.\r
335 @param[in] ProcessorNumber The handle number of the AP. The range is from 0 to the\r
336 total number of logical processors minus 1. The total\r
337 number of logical processors can be retrieved by\r
338 EFI_PEI_MP_SERVICES_PPI.GetNumberOfProcessors().\r
339 @param[in] EnableAP Specifies the new state for the processor for enabled,\r
340 FALSE for disabled.\r
341 @param[in] HealthFlag If not NULL, a pointer to a value that specifies the\r
342 new health status of the AP. This flag corresponds to\r
343 StatusFlag defined in EFI_PEI_MP_SERVICES_PPI.GetProcessorInfo().\r
344 Only the PROCESSOR_HEALTH_STATUS_BIT is used. All other\r
345 bits are ignored. If it is NULL, this parameter is\r
346 ignored.\r
347\r
348 @retval EFI_SUCCESS The specified AP was enabled or disabled successfully.\r
349 @retval EFI_UNSUPPORTED Enabling or disabling an AP cannot be completed prior\r
350 to this service returning.\r
351 @retval EFI_UNSUPPORTED Enabling or disabling an AP is not supported.\r
352 @retval EFI_DEVICE_ERROR The calling processor is an AP.\r
353 @retval EFI_NOT_FOUND Processor with the handle specified by ProcessorNumber\r
354 does not exist.\r
355 @retval EFI_INVALID_PARAMETER ProcessorNumber specifies the BSP.\r
356**/\r
357EFI_STATUS\r
358EFIAPI\r
359PeiEnableDisableAP (\r
360 IN CONST EFI_PEI_SERVICES **PeiServices,\r
361 IN EFI_PEI_MP_SERVICES_PPI *This,\r
362 IN UINTN ProcessorNumber,\r
363 IN BOOLEAN EnableAP,\r
364 IN UINT32 *HealthFlag OPTIONAL\r
365 )\r
366{\r
367 return MpInitLibEnableDisableAP (ProcessorNumber, EnableAP, HealthFlag);\r
368}\r
369\r
370/**\r
371 This return the handle number for the calling processor. This service may be\r
372 called from the BSP and APs.\r
373\r
374 This service returns the processor handle number for the calling processor.\r
375 The returned value is in the range from 0 to the total number of logical\r
376 processors minus 1. The total number of logical processors can be retrieved\r
377 with EFI_PEI_MP_SERVICES_PPI.GetNumberOfProcessors(). This service may be\r
378 called from the BSP and APs. If ProcessorNumber is NULL, then EFI_INVALID_PARAMETER\r
379 is returned. Otherwise, the current processors handle number is returned in\r
380 ProcessorNumber, and EFI_SUCCESS is returned.\r
381\r
382 @param[in] PeiServices An indirect pointer to the PEI Services Table\r
383 published by the PEI Foundation.\r
384 @param[in] This A pointer to the EFI_PEI_MP_SERVICES_PPI instance.\r
385 @param[out] ProcessorNumber The handle number of the AP. The range is from 0 to the\r
386 total number of logical processors minus 1. The total\r
387 number of logical processors can be retrieved by\r
388 EFI_PEI_MP_SERVICES_PPI.GetNumberOfProcessors().\r
389\r
390 @retval EFI_SUCCESS The current processor handle number was returned in\r
391 ProcessorNumber.\r
392 @retval EFI_INVALID_PARAMETER ProcessorNumber is NULL.\r
393**/\r
394EFI_STATUS\r
395EFIAPI\r
396PeiWhoAmI (\r
397 IN CONST EFI_PEI_SERVICES **PeiServices,\r
398 IN EFI_PEI_MP_SERVICES_PPI *This,\r
399 OUT UINTN *ProcessorNumber\r
400 )\r
401{\r
402 return MpInitLibWhoAmI (ProcessorNumber);\r
403}\r
404\r
ea0f431c 405/**\r
0a0d5296 406 Get GDT register value.\r
ea0f431c 407\r
0a0d5296
JW
408 This function is mainly for AP purpose because AP may have different GDT\r
409 table than BSP.\r
ea0f431c 410\r
0a0d5296 411 @param[in,out] Buffer The pointer to private data buffer.\r
ea0f431c 412\r
0a0d5296
JW
413**/\r
414VOID\r
415EFIAPI\r
416GetGdtr (\r
417 IN OUT VOID *Buffer\r
418 )\r
419{\r
420 AsmReadGdtr ((IA32_DESCRIPTOR *)Buffer);\r
421}\r
422\r
423/**\r
424 Initializes CPU exceptions handlers for the sake of stack switch requirement.\r
425\r
426 This function is a wrapper of InitializeCpuExceptionHandlersEx. It's mainly\r
427 for the sake of AP's init because of EFI_AP_PROCEDURE API requirement.\r
428\r
429 @param[in,out] Buffer The pointer to private data buffer.\r
ea0f431c
JF
430\r
431**/\r
0a0d5296 432VOID\r
ea0f431c 433EFIAPI\r
0a0d5296
JW
434InitializeExceptionStackSwitchHandlers (\r
435 IN OUT VOID *Buffer\r
436 )\r
437{\r
438 CPU_EXCEPTION_INIT_DATA *EssData;\r
439 IA32_DESCRIPTOR Idtr;\r
440 EFI_STATUS Status;\r
441\r
442 EssData = Buffer;\r
443 //\r
444 // We don't plan to replace IDT table with a new one, but we should not assume\r
445 // the AP's IDT is the same as BSP's IDT either.\r
446 //\r
447 AsmReadIdtr (&Idtr);\r
448 EssData->Ia32.IdtTable = (VOID *)Idtr.Base;\r
449 EssData->Ia32.IdtTableSize = Idtr.Limit + 1;\r
450 Status = InitializeCpuExceptionHandlersEx (NULL, EssData);\r
451 ASSERT_EFI_ERROR (Status);\r
452}\r
453\r
454/**\r
455 Initializes MP exceptions handlers for the sake of stack switch requirement.\r
456\r
457 This function will allocate required resources required to setup stack switch\r
458 and pass them through CPU_EXCEPTION_INIT_DATA to each logic processor.\r
459\r
460**/\r
461VOID\r
462InitializeMpExceptionStackSwitchHandlers (\r
463 VOID\r
464 )\r
465{\r
466 EFI_STATUS Status;\r
467 UINTN Index;\r
468 UINTN Bsp;\r
469 UINTN ExceptionNumber;\r
470 UINTN OldGdtSize;\r
471 UINTN NewGdtSize;\r
472 UINTN NewStackSize;\r
473 IA32_DESCRIPTOR Gdtr;\r
474 CPU_EXCEPTION_INIT_DATA EssData;\r
475 UINT8 *GdtBuffer;\r
476 UINT8 *StackTop;\r
477 UINTN NumberOfProcessors;\r
478\r
479 if (!PcdGetBool (PcdCpuStackGuard)) {\r
480 return;\r
481 }\r
482\r
483 MpInitLibGetNumberOfProcessors(&NumberOfProcessors, NULL);\r
484 MpInitLibWhoAmI (&Bsp);\r
485\r
486 ExceptionNumber = FixedPcdGetSize (PcdCpuStackSwitchExceptionList);\r
487 NewStackSize = FixedPcdGet32 (PcdCpuKnownGoodStackSize) * ExceptionNumber;\r
488\r
489 Status = PeiServicesAllocatePool (\r
490 NewStackSize * NumberOfProcessors,\r
491 (VOID **)&StackTop\r
492 );\r
493 ASSERT(StackTop != NULL);\r
494 if (EFI_ERROR (Status)) {\r
495 ASSERT_EFI_ERROR (Status);\r
496 return;\r
497 }\r
498 StackTop += NewStackSize * NumberOfProcessors;\r
499\r
500 //\r
501 // The default exception handlers must have been initialized. Let's just skip\r
502 // it in this method.\r
503 //\r
504 EssData.Ia32.Revision = CPU_EXCEPTION_INIT_DATA_REV;\r
505 EssData.Ia32.InitDefaultHandlers = FALSE;\r
506\r
507 EssData.Ia32.StackSwitchExceptions = FixedPcdGetPtr(PcdCpuStackSwitchExceptionList);\r
508 EssData.Ia32.StackSwitchExceptionNumber = ExceptionNumber;\r
509 EssData.Ia32.KnownGoodStackSize = FixedPcdGet32(PcdCpuKnownGoodStackSize);\r
510\r
511 //\r
512 // Initialize Gdtr to suppress incorrect compiler/analyzer warnings.\r
513 //\r
514 Gdtr.Base = 0;\r
515 Gdtr.Limit = 0;\r
516 for (Index = 0; Index < NumberOfProcessors; ++Index) {\r
517 //\r
518 // To support stack switch, we need to re-construct GDT but not IDT.\r
519 //\r
520 if (Index == Bsp) {\r
521 GetGdtr(&Gdtr);\r
522 } else {\r
523 //\r
524 // AP might have different size of GDT from BSP.\r
525 //\r
526 MpInitLibStartupThisAP (GetGdtr, Index, NULL, 0, (VOID *)&Gdtr, NULL);\r
527 }\r
528\r
529 //\r
530 // X64 needs only one TSS of current task working for all exceptions\r
531 // because of its IST feature. IA32 needs one TSS for each exception\r
532 // in addition to current task. Since AP is not supposed to allocate\r
533 // memory, we have to do it in BSP. To simplify the code, we allocate\r
534 // memory for IA32 case to cover both IA32 and X64 exception stack\r
535 // switch.\r
536 //\r
537 // Layout of memory to allocate for each processor:\r
538 // --------------------------------\r
539 // | Alignment | (just in case)\r
540 // --------------------------------\r
541 // | |\r
542 // | Original GDT |\r
543 // | |\r
544 // --------------------------------\r
545 // | Current task descriptor |\r
546 // --------------------------------\r
547 // | |\r
548 // | Exception task descriptors | X ExceptionNumber\r
549 // | |\r
550 // --------------------------------\r
551 // | Current task-state segment |\r
552 // --------------------------------\r
553 // | |\r
554 // | Exception task-state segment | X ExceptionNumber\r
555 // | |\r
556 // --------------------------------\r
557 //\r
558 OldGdtSize = Gdtr.Limit + 1;\r
559 EssData.Ia32.ExceptionTssDescSize = sizeof (IA32_TSS_DESCRIPTOR) *\r
560 (ExceptionNumber + 1);\r
561 EssData.Ia32.ExceptionTssSize = sizeof (IA32_TASK_STATE_SEGMENT) *\r
562 (ExceptionNumber + 1);\r
563 NewGdtSize = sizeof (IA32_TSS_DESCRIPTOR) +\r
564 OldGdtSize +\r
565 EssData.Ia32.ExceptionTssDescSize +\r
566 EssData.Ia32.ExceptionTssSize;\r
567\r
568 Status = PeiServicesAllocatePool (\r
569 NewGdtSize,\r
570 (VOID **)&GdtBuffer\r
571 );\r
572 ASSERT (GdtBuffer != NULL);\r
573 if (EFI_ERROR (Status)) {\r
574 ASSERT_EFI_ERROR (Status);\r
575 return;\r
576 }\r
577\r
578 //\r
579 // Make sure GDT table alignment\r
580 //\r
581 EssData.Ia32.GdtTable = ALIGN_POINTER(GdtBuffer, sizeof (IA32_TSS_DESCRIPTOR));\r
582 NewGdtSize -= ((UINT8 *)EssData.Ia32.GdtTable - GdtBuffer);\r
583 EssData.Ia32.GdtTableSize = NewGdtSize;\r
584\r
585 EssData.Ia32.ExceptionTssDesc = ((UINT8 *)EssData.Ia32.GdtTable + OldGdtSize);\r
586 EssData.Ia32.ExceptionTss = ((UINT8 *)EssData.Ia32.GdtTable + OldGdtSize +\r
587 EssData.Ia32.ExceptionTssDescSize);\r
588\r
589 EssData.Ia32.KnownGoodStackTop = (UINTN)StackTop;\r
590 DEBUG ((DEBUG_INFO,\r
591 "Exception stack top[cpu%lu]: 0x%lX\n",\r
592 (UINT64)(UINTN)Index,\r
593 (UINT64)(UINTN)StackTop));\r
594\r
595 if (Index == Bsp) {\r
596 InitializeExceptionStackSwitchHandlers (&EssData);\r
597 } else {\r
598 MpInitLibStartupThisAP (\r
599 InitializeExceptionStackSwitchHandlers,\r
600 Index,\r
601 NULL,\r
602 0,\r
603 (VOID *)&EssData,\r
604 NULL\r
605 );\r
606 }\r
607\r
608 StackTop -= NewStackSize;\r
609 }\r
610}\r
611\r
612/**\r
613 Initializes MP and exceptions handlers.\r
614\r
615 @param PeiServices The pointer to the PEI Services Table.\r
616\r
617 @retval EFI_SUCCESS MP was successfully initialized.\r
618 @retval others Error occurred in MP initialization.\r
619\r
620**/\r
621EFI_STATUS\r
622InitializeCpuMpWorker (\r
ea0f431c
JF
623 IN CONST EFI_PEI_SERVICES **PeiServices\r
624 )\r
625{\r
0a0d5296 626 EFI_STATUS Status;\r
9bedfb2f
JF
627 EFI_VECTOR_HANDOFF_INFO *VectorInfo;\r
628 EFI_PEI_VECTOR_HANDOFF_INFO_PPI *VectorHandoffInfoPpi;\r
ea0f431c 629\r
ea0f431c 630 //\r
9bedfb2f
JF
631 // Get Vector Hand-off Info PPI\r
632 //\r
633 VectorInfo = NULL;\r
634 Status = PeiServicesLocatePpi (\r
635 &gEfiVectorHandoffInfoPpiGuid,\r
636 0,\r
637 NULL,\r
638 (VOID **)&VectorHandoffInfoPpi\r
639 );\r
640 if (Status == EFI_SUCCESS) {\r
641 VectorInfo = VectorHandoffInfoPpi->Info;\r
642 }\r
7367cc6c 643\r
9bedfb2f 644 //\r
0a0d5296 645 // Initialize default handlers\r
ea0f431c 646 //\r
0a0d5296
JW
647 Status = InitializeCpuExceptionHandlers (VectorInfo);\r
648 if (EFI_ERROR (Status)) {\r
649 return Status;\r
650 }\r
651\r
a1a4c7a4 652 Status = MpInitLibInitialize ();\r
0a0d5296
JW
653 if (EFI_ERROR (Status)) {\r
654 return Status;\r
655 }\r
656\r
657 //\r
658 // Special initialization for the sake of Stack Guard\r
659 //\r
660 InitializeMpExceptionStackSwitchHandlers ();\r
a1a4c7a4 661\r
ea0f431c
JF
662 //\r
663 // Update and publish CPU BIST information\r
664 //\r
a1a4c7a4
JF
665 CollectBistDataFromPpi (PeiServices);\r
666\r
ea0f431c
JF
667 //\r
668 // Install CPU MP PPI\r
669 //\r
670 Status = PeiServicesInstallPpi(&mPeiCpuMpPpiDesc);\r
671 ASSERT_EFI_ERROR (Status);\r
672\r
673 return Status;\r
674}\r
0a0d5296
JW
675\r
676/**\r
677 The Entry point of the MP CPU PEIM.\r
678\r
679 This function will wakeup APs and collect CPU AP count and install the\r
680 Mp Service Ppi.\r
681\r
682 @param FileHandle Handle of the file being invoked.\r
683 @param PeiServices Describes the list of possible PEI Services.\r
684\r
685 @retval EFI_SUCCESS MpServicePpi is installed successfully.\r
686\r
687**/\r
688EFI_STATUS\r
689EFIAPI\r
690CpuMpPeimInit (\r
691 IN EFI_PEI_FILE_HANDLE FileHandle,\r
692 IN CONST EFI_PEI_SERVICES **PeiServices\r
693 )\r
694{\r
695 EFI_STATUS Status;\r
696\r
697 //\r
698 // For the sake of special initialization needing to be done right after\r
699 // memory discovery.\r
700 //\r
701 Status = PeiServicesNotifyPpi (&mPostMemNotifyList[0]);\r
702 ASSERT_EFI_ERROR (Status);\r
703\r
704 return Status;\r
705}\r