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