]> git.proxmox.com Git - mirror_edk2.git/blame - UefiCpuPkg/CpuMpPei/CpuMpPei.c
UefiCpuPkg/CpuMp: Fix hang when StackGuard is enabled in 16-core cpu
[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
60b12e69
MK
432/**\r
433 Migrates the Global Descriptor Table (GDT) to permanent memory.\r
434\r
435 @retval EFI_SUCCESS The GDT was migrated successfully.\r
436 @retval EFI_OUT_OF_RESOURCES The GDT could not be migrated due to lack of available memory.\r
437\r
438**/\r
439EFI_STATUS\r
440MigrateGdt (\r
441 VOID\r
442 )\r
443{\r
444 EFI_STATUS Status;\r
445 UINTN GdtBufferSize;\r
446 IA32_DESCRIPTOR Gdtr;\r
447 VOID *GdtBuffer;\r
448\r
449 AsmReadGdtr ((IA32_DESCRIPTOR *) &Gdtr);\r
450 GdtBufferSize = sizeof (IA32_SEGMENT_DESCRIPTOR) -1 + Gdtr.Limit + 1;\r
451\r
452 Status = PeiServicesAllocatePool (\r
453 GdtBufferSize,\r
454 &GdtBuffer\r
455 );\r
456 ASSERT (GdtBuffer != NULL);\r
457 if (EFI_ERROR (Status)) {\r
458 return EFI_OUT_OF_RESOURCES;\r
459 }\r
460\r
461 GdtBuffer = ALIGN_POINTER (GdtBuffer, sizeof (IA32_SEGMENT_DESCRIPTOR));\r
462 CopyMem (GdtBuffer, (VOID *) Gdtr.Base, Gdtr.Limit + 1);\r
463 Gdtr.Base = (UINTN) GdtBuffer;\r
464 AsmWriteGdtr (&Gdtr);\r
465\r
466 return EFI_SUCCESS;\r
467}\r
468\r
0a0d5296
JW
469/**\r
470 Initializes CPU exceptions handlers for the sake of stack switch requirement.\r
471\r
472 This function is a wrapper of InitializeCpuExceptionHandlersEx. It's mainly\r
473 for the sake of AP's init because of EFI_AP_PROCEDURE API requirement.\r
474\r
475 @param[in,out] Buffer The pointer to private data buffer.\r
ea0f431c
JF
476\r
477**/\r
0a0d5296 478VOID\r
ea0f431c 479EFIAPI\r
0a0d5296
JW
480InitializeExceptionStackSwitchHandlers (\r
481 IN OUT VOID *Buffer\r
482 )\r
483{\r
484 CPU_EXCEPTION_INIT_DATA *EssData;\r
485 IA32_DESCRIPTOR Idtr;\r
486 EFI_STATUS Status;\r
487\r
488 EssData = Buffer;\r
489 //\r
490 // We don't plan to replace IDT table with a new one, but we should not assume\r
491 // the AP's IDT is the same as BSP's IDT either.\r
492 //\r
493 AsmReadIdtr (&Idtr);\r
494 EssData->Ia32.IdtTable = (VOID *)Idtr.Base;\r
495 EssData->Ia32.IdtTableSize = Idtr.Limit + 1;\r
496 Status = InitializeCpuExceptionHandlersEx (NULL, EssData);\r
497 ASSERT_EFI_ERROR (Status);\r
498}\r
499\r
500/**\r
501 Initializes MP exceptions handlers for the sake of stack switch requirement.\r
502\r
503 This function will allocate required resources required to setup stack switch\r
504 and pass them through CPU_EXCEPTION_INIT_DATA to each logic processor.\r
505\r
506**/\r
507VOID\r
508InitializeMpExceptionStackSwitchHandlers (\r
509 VOID\r
510 )\r
511{\r
512 EFI_STATUS Status;\r
513 UINTN Index;\r
514 UINTN Bsp;\r
515 UINTN ExceptionNumber;\r
516 UINTN OldGdtSize;\r
517 UINTN NewGdtSize;\r
518 UINTN NewStackSize;\r
519 IA32_DESCRIPTOR Gdtr;\r
520 CPU_EXCEPTION_INIT_DATA EssData;\r
521 UINT8 *GdtBuffer;\r
522 UINT8 *StackTop;\r
523 UINTN NumberOfProcessors;\r
524\r
525 if (!PcdGetBool (PcdCpuStackGuard)) {\r
526 return;\r
527 }\r
528\r
529 MpInitLibGetNumberOfProcessors(&NumberOfProcessors, NULL);\r
530 MpInitLibWhoAmI (&Bsp);\r
531\r
532 ExceptionNumber = FixedPcdGetSize (PcdCpuStackSwitchExceptionList);\r
533 NewStackSize = FixedPcdGet32 (PcdCpuKnownGoodStackSize) * ExceptionNumber;\r
534\r
3b769c51 535 StackTop = AllocatePages (EFI_SIZE_TO_PAGES (NewStackSize * NumberOfProcessors));\r
0a0d5296 536 ASSERT(StackTop != NULL);\r
3b769c51 537 if (StackTop == NULL) {\r
0a0d5296
JW
538 return;\r
539 }\r
540 StackTop += NewStackSize * NumberOfProcessors;\r
541\r
542 //\r
543 // The default exception handlers must have been initialized. Let's just skip\r
544 // it in this method.\r
545 //\r
546 EssData.Ia32.Revision = CPU_EXCEPTION_INIT_DATA_REV;\r
547 EssData.Ia32.InitDefaultHandlers = FALSE;\r
548\r
549 EssData.Ia32.StackSwitchExceptions = FixedPcdGetPtr(PcdCpuStackSwitchExceptionList);\r
550 EssData.Ia32.StackSwitchExceptionNumber = ExceptionNumber;\r
551 EssData.Ia32.KnownGoodStackSize = FixedPcdGet32(PcdCpuKnownGoodStackSize);\r
552\r
553 //\r
554 // Initialize Gdtr to suppress incorrect compiler/analyzer warnings.\r
555 //\r
556 Gdtr.Base = 0;\r
557 Gdtr.Limit = 0;\r
558 for (Index = 0; Index < NumberOfProcessors; ++Index) {\r
559 //\r
560 // To support stack switch, we need to re-construct GDT but not IDT.\r
561 //\r
562 if (Index == Bsp) {\r
563 GetGdtr(&Gdtr);\r
564 } else {\r
565 //\r
566 // AP might have different size of GDT from BSP.\r
567 //\r
568 MpInitLibStartupThisAP (GetGdtr, Index, NULL, 0, (VOID *)&Gdtr, NULL);\r
569 }\r
570\r
571 //\r
572 // X64 needs only one TSS of current task working for all exceptions\r
573 // because of its IST feature. IA32 needs one TSS for each exception\r
574 // in addition to current task. Since AP is not supposed to allocate\r
575 // memory, we have to do it in BSP. To simplify the code, we allocate\r
576 // memory for IA32 case to cover both IA32 and X64 exception stack\r
577 // switch.\r
578 //\r
579 // Layout of memory to allocate for each processor:\r
580 // --------------------------------\r
581 // | Alignment | (just in case)\r
582 // --------------------------------\r
583 // | |\r
584 // | Original GDT |\r
585 // | |\r
586 // --------------------------------\r
587 // | Current task descriptor |\r
588 // --------------------------------\r
589 // | |\r
590 // | Exception task descriptors | X ExceptionNumber\r
591 // | |\r
592 // --------------------------------\r
593 // | Current task-state segment |\r
594 // --------------------------------\r
595 // | |\r
596 // | Exception task-state segment | X ExceptionNumber\r
597 // | |\r
598 // --------------------------------\r
599 //\r
600 OldGdtSize = Gdtr.Limit + 1;\r
601 EssData.Ia32.ExceptionTssDescSize = sizeof (IA32_TSS_DESCRIPTOR) *\r
602 (ExceptionNumber + 1);\r
603 EssData.Ia32.ExceptionTssSize = sizeof (IA32_TASK_STATE_SEGMENT) *\r
604 (ExceptionNumber + 1);\r
605 NewGdtSize = sizeof (IA32_TSS_DESCRIPTOR) +\r
606 OldGdtSize +\r
607 EssData.Ia32.ExceptionTssDescSize +\r
608 EssData.Ia32.ExceptionTssSize;\r
609\r
610 Status = PeiServicesAllocatePool (\r
611 NewGdtSize,\r
612 (VOID **)&GdtBuffer\r
613 );\r
614 ASSERT (GdtBuffer != NULL);\r
615 if (EFI_ERROR (Status)) {\r
616 ASSERT_EFI_ERROR (Status);\r
617 return;\r
618 }\r
619\r
620 //\r
621 // Make sure GDT table alignment\r
622 //\r
623 EssData.Ia32.GdtTable = ALIGN_POINTER(GdtBuffer, sizeof (IA32_TSS_DESCRIPTOR));\r
624 NewGdtSize -= ((UINT8 *)EssData.Ia32.GdtTable - GdtBuffer);\r
625 EssData.Ia32.GdtTableSize = NewGdtSize;\r
626\r
627 EssData.Ia32.ExceptionTssDesc = ((UINT8 *)EssData.Ia32.GdtTable + OldGdtSize);\r
628 EssData.Ia32.ExceptionTss = ((UINT8 *)EssData.Ia32.GdtTable + OldGdtSize +\r
629 EssData.Ia32.ExceptionTssDescSize);\r
630\r
631 EssData.Ia32.KnownGoodStackTop = (UINTN)StackTop;\r
632 DEBUG ((DEBUG_INFO,\r
633 "Exception stack top[cpu%lu]: 0x%lX\n",\r
634 (UINT64)(UINTN)Index,\r
635 (UINT64)(UINTN)StackTop));\r
636\r
637 if (Index == Bsp) {\r
638 InitializeExceptionStackSwitchHandlers (&EssData);\r
639 } else {\r
640 MpInitLibStartupThisAP (\r
641 InitializeExceptionStackSwitchHandlers,\r
642 Index,\r
643 NULL,\r
644 0,\r
645 (VOID *)&EssData,\r
646 NULL\r
647 );\r
648 }\r
649\r
650 StackTop -= NewStackSize;\r
651 }\r
652}\r
653\r
654/**\r
655 Initializes MP and exceptions handlers.\r
656\r
657 @param PeiServices The pointer to the PEI Services Table.\r
658\r
659 @retval EFI_SUCCESS MP was successfully initialized.\r
660 @retval others Error occurred in MP initialization.\r
661\r
662**/\r
663EFI_STATUS\r
664InitializeCpuMpWorker (\r
ea0f431c
JF
665 IN CONST EFI_PEI_SERVICES **PeiServices\r
666 )\r
667{\r
0a0d5296 668 EFI_STATUS Status;\r
9bedfb2f
JF
669 EFI_VECTOR_HANDOFF_INFO *VectorInfo;\r
670 EFI_PEI_VECTOR_HANDOFF_INFO_PPI *VectorHandoffInfoPpi;\r
ea0f431c 671\r
ea0f431c 672 //\r
9bedfb2f
JF
673 // Get Vector Hand-off Info PPI\r
674 //\r
675 VectorInfo = NULL;\r
676 Status = PeiServicesLocatePpi (\r
677 &gEfiVectorHandoffInfoPpiGuid,\r
678 0,\r
679 NULL,\r
680 (VOID **)&VectorHandoffInfoPpi\r
681 );\r
682 if (Status == EFI_SUCCESS) {\r
683 VectorInfo = VectorHandoffInfoPpi->Info;\r
684 }\r
7367cc6c 685\r
9bedfb2f 686 //\r
0a0d5296 687 // Initialize default handlers\r
ea0f431c 688 //\r
0a0d5296
JW
689 Status = InitializeCpuExceptionHandlers (VectorInfo);\r
690 if (EFI_ERROR (Status)) {\r
691 return Status;\r
692 }\r
693\r
a1a4c7a4 694 Status = MpInitLibInitialize ();\r
0a0d5296
JW
695 if (EFI_ERROR (Status)) {\r
696 return Status;\r
697 }\r
698\r
699 //\r
700 // Special initialization for the sake of Stack Guard\r
701 //\r
702 InitializeMpExceptionStackSwitchHandlers ();\r
a1a4c7a4 703\r
ea0f431c
JF
704 //\r
705 // Update and publish CPU BIST information\r
706 //\r
a1a4c7a4
JF
707 CollectBistDataFromPpi (PeiServices);\r
708\r
ea0f431c
JF
709 //\r
710 // Install CPU MP PPI\r
711 //\r
a430589c 712 Status = PeiServicesInstallPpi(mPeiCpuMpPpiList);\r
ea0f431c
JF
713 ASSERT_EFI_ERROR (Status);\r
714\r
715 return Status;\r
716}\r
0a0d5296
JW
717\r
718/**\r
719 The Entry point of the MP CPU PEIM.\r
720\r
721 This function will wakeup APs and collect CPU AP count and install the\r
722 Mp Service Ppi.\r
723\r
724 @param FileHandle Handle of the file being invoked.\r
725 @param PeiServices Describes the list of possible PEI Services.\r
726\r
727 @retval EFI_SUCCESS MpServicePpi is installed successfully.\r
728\r
729**/\r
730EFI_STATUS\r
731EFIAPI\r
732CpuMpPeimInit (\r
733 IN EFI_PEI_FILE_HANDLE FileHandle,\r
734 IN CONST EFI_PEI_SERVICES **PeiServices\r
735 )\r
736{\r
737 EFI_STATUS Status;\r
738\r
739 //\r
740 // For the sake of special initialization needing to be done right after\r
741 // memory discovery.\r
742 //\r
743 Status = PeiServicesNotifyPpi (&mPostMemNotifyList[0]);\r
744 ASSERT_EFI_ERROR (Status);\r
745\r
746 return Status;\r
747}\r