CpuMpPei.c 28 KB

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