]> git.proxmox.com Git - mirror_edk2.git/blame - UefiCpuPkg/CpuMpPei/CpuMpPei.c
CpuException: Add InitializeSeparateExceptionStacks
[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
053e878b 11extern EDKII_PEI_MP_SERVICES2_PPI mMpServices2Ppi;\r
a430589c 12\r
89fa1bf2
JF
13//\r
14// CPU MP PPI to be installed\r
15//\r
053e878b 16EFI_PEI_MP_SERVICES_PPI mMpServicesPpi = {\r
89fa1bf2
JF
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
053e878b 26EFI_PEI_PPI_DESCRIPTOR mPeiCpuMpPpiList[] = {\r
a430589c
ED
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
053e878b
MK
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
89fa1bf2
JF
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
053e878b
MK
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
89fa1bf2
JF
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
053e878b
MK
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
89fa1bf2
JF
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
053e878b
MK
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
89fa1bf2
JF
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
053e878b 426 IN OUT VOID *Buffer\r
0a0d5296
JW
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
e7abb94d 435 This function is a wrapper of InitializeSeparateExceptionStacks. It's mainly\r
0a0d5296
JW
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 443InitializeExceptionStackSwitchHandlers (\r
053e878b 444 IN OUT VOID *Buffer\r
0a0d5296
JW
445 )\r
446{\r
053e878b
MK
447 CPU_EXCEPTION_INIT_DATA *EssData;\r
448 IA32_DESCRIPTOR Idtr;\r
449 EFI_STATUS Status;\r
0a0d5296
JW
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
053e878b 457 EssData->Ia32.IdtTable = (VOID *)Idtr.Base;\r
0a0d5296 458 EssData->Ia32.IdtTableSize = Idtr.Limit + 1;\r
e7abb94d 459 Status = InitializeSeparateExceptionStacks (EssData);\r
0a0d5296
JW
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
053e878b
MK
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
0a0d5296
JW
487\r
488 if (!PcdGetBool (PcdCpuStackGuard)) {\r
489 return;\r
490 }\r
491\r
053e878b 492 MpInitLibGetNumberOfProcessors (&NumberOfProcessors, NULL);\r
0a0d5296
JW
493 MpInitLibWhoAmI (&Bsp);\r
494\r
495 ExceptionNumber = FixedPcdGetSize (PcdCpuStackSwitchExceptionList);\r
053e878b 496 NewStackSize = FixedPcdGet32 (PcdCpuKnownGoodStackSize) * ExceptionNumber;\r
0a0d5296 497\r
3b769c51 498 StackTop = AllocatePages (EFI_SIZE_TO_PAGES (NewStackSize * NumberOfProcessors));\r
053e878b 499 ASSERT (StackTop != NULL);\r
3b769c51 500 if (StackTop == NULL) {\r
0a0d5296
JW
501 return;\r
502 }\r
053e878b 503\r
0a0d5296
JW
504 StackTop += NewStackSize * NumberOfProcessors;\r
505\r
506 //\r
507 // The default exception handlers must have been initialized. Let's just skip\r
508 // it in this method.\r
509 //\r
053e878b 510 EssData.Ia32.Revision = CPU_EXCEPTION_INIT_DATA_REV;\r
0a0d5296
JW
511 EssData.Ia32.InitDefaultHandlers = FALSE;\r
512\r
053e878b 513 EssData.Ia32.StackSwitchExceptions = FixedPcdGetPtr (PcdCpuStackSwitchExceptionList);\r
0a0d5296 514 EssData.Ia32.StackSwitchExceptionNumber = ExceptionNumber;\r
053e878b 515 EssData.Ia32.KnownGoodStackSize = FixedPcdGet32 (PcdCpuKnownGoodStackSize);\r
0a0d5296
JW
516\r
517 //\r
518 // Initialize Gdtr to suppress incorrect compiler/analyzer warnings.\r
519 //\r
053e878b 520 Gdtr.Base = 0;\r
0a0d5296
JW
521 Gdtr.Limit = 0;\r
522 for (Index = 0; Index < NumberOfProcessors; ++Index) {\r
523 //\r
524 // To support stack switch, we need to re-construct GDT but not IDT.\r
525 //\r
526 if (Index == Bsp) {\r
053e878b 527 GetGdtr (&Gdtr);\r
0a0d5296
JW
528 } else {\r
529 //\r
530 // AP might have different size of GDT from BSP.\r
531 //\r
532 MpInitLibStartupThisAP (GetGdtr, Index, NULL, 0, (VOID *)&Gdtr, NULL);\r
533 }\r
534\r
535 //\r
536 // X64 needs only one TSS of current task working for all exceptions\r
537 // because of its IST feature. IA32 needs one TSS for each exception\r
538 // in addition to current task. Since AP is not supposed to allocate\r
539 // memory, we have to do it in BSP. To simplify the code, we allocate\r
540 // memory for IA32 case to cover both IA32 and X64 exception stack\r
541 // switch.\r
542 //\r
543 // Layout of memory to allocate for each processor:\r
544 // --------------------------------\r
545 // | Alignment | (just in case)\r
546 // --------------------------------\r
547 // | |\r
548 // | Original GDT |\r
549 // | |\r
550 // --------------------------------\r
551 // | Current task descriptor |\r
552 // --------------------------------\r
553 // | |\r
554 // | Exception task descriptors | X ExceptionNumber\r
555 // | |\r
556 // --------------------------------\r
557 // | Current task-state segment |\r
558 // --------------------------------\r
559 // | |\r
560 // | Exception task-state segment | X ExceptionNumber\r
561 // | |\r
562 // --------------------------------\r
563 //\r
053e878b 564 OldGdtSize = Gdtr.Limit + 1;\r
0a0d5296
JW
565 EssData.Ia32.ExceptionTssDescSize = sizeof (IA32_TSS_DESCRIPTOR) *\r
566 (ExceptionNumber + 1);\r
567 EssData.Ia32.ExceptionTssSize = sizeof (IA32_TASK_STATE_SEGMENT) *\r
568 (ExceptionNumber + 1);\r
569 NewGdtSize = sizeof (IA32_TSS_DESCRIPTOR) +\r
570 OldGdtSize +\r
571 EssData.Ia32.ExceptionTssDescSize +\r
572 EssData.Ia32.ExceptionTssSize;\r
573\r
574 Status = PeiServicesAllocatePool (\r
575 NewGdtSize,\r
576 (VOID **)&GdtBuffer\r
577 );\r
578 ASSERT (GdtBuffer != NULL);\r
579 if (EFI_ERROR (Status)) {\r
580 ASSERT_EFI_ERROR (Status);\r
581 return;\r
582 }\r
583\r
584 //\r
585 // Make sure GDT table alignment\r
586 //\r
053e878b
MK
587 EssData.Ia32.GdtTable = ALIGN_POINTER (GdtBuffer, sizeof (IA32_TSS_DESCRIPTOR));\r
588 NewGdtSize -= ((UINT8 *)EssData.Ia32.GdtTable - GdtBuffer);\r
0a0d5296
JW
589 EssData.Ia32.GdtTableSize = NewGdtSize;\r
590\r
591 EssData.Ia32.ExceptionTssDesc = ((UINT8 *)EssData.Ia32.GdtTable + OldGdtSize);\r
053e878b
MK
592 EssData.Ia32.ExceptionTss = ((UINT8 *)EssData.Ia32.GdtTable + OldGdtSize +\r
593 EssData.Ia32.ExceptionTssDescSize);\r
0a0d5296
JW
594\r
595 EssData.Ia32.KnownGoodStackTop = (UINTN)StackTop;\r
053e878b
MK
596 DEBUG ((\r
597 DEBUG_INFO,\r
598 "Exception stack top[cpu%lu]: 0x%lX\n",\r
599 (UINT64)(UINTN)Index,\r
600 (UINT64)(UINTN)StackTop\r
601 ));\r
0a0d5296
JW
602\r
603 if (Index == Bsp) {\r
604 InitializeExceptionStackSwitchHandlers (&EssData);\r
605 } else {\r
606 MpInitLibStartupThisAP (\r
607 InitializeExceptionStackSwitchHandlers,\r
608 Index,\r
609 NULL,\r
610 0,\r
611 (VOID *)&EssData,\r
612 NULL\r
613 );\r
614 }\r
615\r
053e878b 616 StackTop -= NewStackSize;\r
0a0d5296
JW
617 }\r
618}\r
619\r
620/**\r
621 Initializes MP and exceptions handlers.\r
622\r
623 @param PeiServices The pointer to the PEI Services Table.\r
624\r
625 @retval EFI_SUCCESS MP was successfully initialized.\r
626 @retval others Error occurred in MP initialization.\r
627\r
628**/\r
629EFI_STATUS\r
630InitializeCpuMpWorker (\r
053e878b 631 IN CONST EFI_PEI_SERVICES **PeiServices\r
ea0f431c
JF
632 )\r
633{\r
053e878b
MK
634 EFI_STATUS Status;\r
635 EFI_VECTOR_HANDOFF_INFO *VectorInfo;\r
636 EFI_PEI_VECTOR_HANDOFF_INFO_PPI *VectorHandoffInfoPpi;\r
ea0f431c 637\r
ea0f431c 638 //\r
9bedfb2f
JF
639 // Get Vector Hand-off Info PPI\r
640 //\r
641 VectorInfo = NULL;\r
053e878b
MK
642 Status = PeiServicesLocatePpi (\r
643 &gEfiVectorHandoffInfoPpiGuid,\r
644 0,\r
645 NULL,\r
646 (VOID **)&VectorHandoffInfoPpi\r
647 );\r
9bedfb2f
JF
648 if (Status == EFI_SUCCESS) {\r
649 VectorInfo = VectorHandoffInfoPpi->Info;\r
650 }\r
7367cc6c 651\r
9bedfb2f 652 //\r
0a0d5296 653 // Initialize default handlers\r
ea0f431c 654 //\r
0a0d5296
JW
655 Status = InitializeCpuExceptionHandlers (VectorInfo);\r
656 if (EFI_ERROR (Status)) {\r
657 return Status;\r
658 }\r
659\r
a1a4c7a4 660 Status = MpInitLibInitialize ();\r
0a0d5296
JW
661 if (EFI_ERROR (Status)) {\r
662 return Status;\r
663 }\r
664\r
665 //\r
666 // Special initialization for the sake of Stack Guard\r
667 //\r
668 InitializeMpExceptionStackSwitchHandlers ();\r
a1a4c7a4 669\r
ea0f431c
JF
670 //\r
671 // Update and publish CPU BIST information\r
672 //\r
a1a4c7a4
JF
673 CollectBistDataFromPpi (PeiServices);\r
674\r
ea0f431c
JF
675 //\r
676 // Install CPU MP PPI\r
677 //\r
053e878b 678 Status = PeiServicesInstallPpi (mPeiCpuMpPpiList);\r
ea0f431c
JF
679 ASSERT_EFI_ERROR (Status);\r
680\r
681 return Status;\r
682}\r
0a0d5296
JW
683\r
684/**\r
685 The Entry point of the MP CPU PEIM.\r
686\r
687 This function will wakeup APs and collect CPU AP count and install the\r
688 Mp Service Ppi.\r
689\r
690 @param FileHandle Handle of the file being invoked.\r
691 @param PeiServices Describes the list of possible PEI Services.\r
692\r
693 @retval EFI_SUCCESS MpServicePpi is installed successfully.\r
694\r
695**/\r
696EFI_STATUS\r
697EFIAPI\r
698CpuMpPeimInit (\r
699 IN EFI_PEI_FILE_HANDLE FileHandle,\r
700 IN CONST EFI_PEI_SERVICES **PeiServices\r
701 )\r
702{\r
053e878b 703 EFI_STATUS Status;\r
0a0d5296
JW
704\r
705 //\r
706 // For the sake of special initialization needing to be done right after\r
707 // memory discovery.\r
708 //\r
709 Status = PeiServicesNotifyPpi (&mPostMemNotifyList[0]);\r
710 ASSERT_EFI_ERROR (Status);\r
711\r
712 return Status;\r
713}\r