CpuMp.c 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852
  1. /** @file
  2. CPU DXE Module to produce CPU MP Protocol.
  3. Copyright (c) 2008 - 2017, Intel Corporation. All rights reserved.<BR>
  4. SPDX-License-Identifier: BSD-2-Clause-Patent
  5. **/
  6. #include "CpuDxe.h"
  7. #include "CpuMp.h"
  8. EFI_HANDLE mMpServiceHandle = NULL;
  9. UINTN mNumberOfProcessors = 1;
  10. EFI_MP_SERVICES_PROTOCOL mMpServicesTemplate = {
  11. GetNumberOfProcessors,
  12. GetProcessorInfo,
  13. StartupAllAPs,
  14. StartupThisAP,
  15. SwitchBSP,
  16. EnableDisableAP,
  17. WhoAmI
  18. };
  19. /**
  20. This service retrieves the number of logical processor in the platform
  21. and the number of those logical processors that are enabled on this boot.
  22. This service may only be called from the BSP.
  23. This function is used to retrieve the following information:
  24. - The number of logical processors that are present in the system.
  25. - The number of enabled logical processors in the system at the instant
  26. this call is made.
  27. Because MP Service Protocol provides services to enable and disable processors
  28. dynamically, the number of enabled logical processors may vary during the
  29. course of a boot session.
  30. If this service is called from an AP, then EFI_DEVICE_ERROR is returned.
  31. If NumberOfProcessors or NumberOfEnabledProcessors is NULL, then
  32. EFI_INVALID_PARAMETER is returned. Otherwise, the total number of processors
  33. is returned in NumberOfProcessors, the number of currently enabled processor
  34. is returned in NumberOfEnabledProcessors, and EFI_SUCCESS is returned.
  35. @param[in] This A pointer to the EFI_MP_SERVICES_PROTOCOL
  36. instance.
  37. @param[out] NumberOfProcessors Pointer to the total number of logical
  38. processors in the system, including the BSP
  39. and disabled APs.
  40. @param[out] NumberOfEnabledProcessors Pointer to the number of enabled logical
  41. processors that exist in system, including
  42. the BSP.
  43. @retval EFI_SUCCESS The number of logical processors and enabled
  44. logical processors was retrieved.
  45. @retval EFI_DEVICE_ERROR The calling processor is an AP.
  46. @retval EFI_INVALID_PARAMETER NumberOfProcessors is NULL.
  47. @retval EFI_INVALID_PARAMETER NumberOfEnabledProcessors is NULL.
  48. **/
  49. EFI_STATUS
  50. EFIAPI
  51. GetNumberOfProcessors (
  52. IN EFI_MP_SERVICES_PROTOCOL *This,
  53. OUT UINTN *NumberOfProcessors,
  54. OUT UINTN *NumberOfEnabledProcessors
  55. )
  56. {
  57. if ((NumberOfProcessors == NULL) || (NumberOfEnabledProcessors == NULL)) {
  58. return EFI_INVALID_PARAMETER;
  59. }
  60. return MpInitLibGetNumberOfProcessors (
  61. NumberOfProcessors,
  62. NumberOfEnabledProcessors
  63. );
  64. }
  65. /**
  66. Gets detailed MP-related information on the requested processor at the
  67. instant this call is made. This service may only be called from the BSP.
  68. This service retrieves detailed MP-related information about any processor
  69. on the platform. Note the following:
  70. - The processor information may change during the course of a boot session.
  71. - The information presented here is entirely MP related.
  72. Information regarding the number of caches and their sizes, frequency of operation,
  73. slot numbers is all considered platform-related information and is not provided
  74. by this service.
  75. @param[in] This A pointer to the EFI_MP_SERVICES_PROTOCOL
  76. instance.
  77. @param[in] ProcessorNumber The handle number of processor.
  78. @param[out] ProcessorInfoBuffer A pointer to the buffer where information for
  79. the requested processor is deposited.
  80. @retval EFI_SUCCESS Processor information was returned.
  81. @retval EFI_DEVICE_ERROR The calling processor is an AP.
  82. @retval EFI_INVALID_PARAMETER ProcessorInfoBuffer is NULL.
  83. @retval EFI_NOT_FOUND The processor with the handle specified by
  84. ProcessorNumber does not exist in the platform.
  85. **/
  86. EFI_STATUS
  87. EFIAPI
  88. GetProcessorInfo (
  89. IN EFI_MP_SERVICES_PROTOCOL *This,
  90. IN UINTN ProcessorNumber,
  91. OUT EFI_PROCESSOR_INFORMATION *ProcessorInfoBuffer
  92. )
  93. {
  94. return MpInitLibGetProcessorInfo (ProcessorNumber, ProcessorInfoBuffer, NULL);
  95. }
  96. /**
  97. This service executes a caller provided function on all enabled APs. APs can
  98. run either simultaneously or one at a time in sequence. This service supports
  99. both blocking and non-blocking requests. The non-blocking requests use EFI
  100. events so the BSP can detect when the APs have finished. This service may only
  101. be called from the BSP.
  102. This function is used to dispatch all the enabled APs to the function specified
  103. by Procedure. If any enabled AP is busy, then EFI_NOT_READY is returned
  104. immediately and Procedure is not started on any AP.
  105. If SingleThread is TRUE, all the enabled APs execute the function specified by
  106. Procedure one by one, in ascending order of processor handle number. Otherwise,
  107. all the enabled APs execute the function specified by Procedure simultaneously.
  108. If WaitEvent is NULL, execution is in blocking mode. The BSP waits until all
  109. APs finish or TimeoutInMicroseconds expires. Otherwise, execution is in non-blocking
  110. mode, and the BSP returns from this service without waiting for APs. If a
  111. non-blocking mode is requested after the UEFI Event EFI_EVENT_GROUP_READY_TO_BOOT
  112. is signaled, then EFI_UNSUPPORTED must be returned.
  113. If the timeout specified by TimeoutInMicroseconds expires before all APs return
  114. from Procedure, then Procedure on the failed APs is terminated. All enabled APs
  115. are always available for further calls to EFI_MP_SERVICES_PROTOCOL.StartupAllAPs()
  116. and EFI_MP_SERVICES_PROTOCOL.StartupThisAP(). If FailedCpuList is not NULL, its
  117. content points to the list of processor handle numbers in which Procedure was
  118. terminated.
  119. Note: It is the responsibility of the consumer of the EFI_MP_SERVICES_PROTOCOL.StartupAllAPs()
  120. to make sure that the nature of the code that is executed on the BSP and the
  121. dispatched APs is well controlled. The MP Services Protocol does not guarantee
  122. that the Procedure function is MP-safe. Hence, the tasks that can be run in
  123. parallel are limited to certain independent tasks and well-controlled exclusive
  124. code. EFI services and protocols may not be called by APs unless otherwise
  125. specified.
  126. In blocking execution mode, BSP waits until all APs finish or
  127. TimeoutInMicroseconds expires.
  128. In non-blocking execution mode, BSP is freed to return to the caller and then
  129. proceed to the next task without having to wait for APs. The following
  130. sequence needs to occur in a non-blocking execution mode:
  131. -# The caller that intends to use this MP Services Protocol in non-blocking
  132. mode creates WaitEvent by calling the EFI CreateEvent() service. The caller
  133. invokes EFI_MP_SERVICES_PROTOCOL.StartupAllAPs(). If the parameter WaitEvent
  134. is not NULL, then StartupAllAPs() executes in non-blocking mode. It requests
  135. the function specified by Procedure to be started on all the enabled APs,
  136. and releases the BSP to continue with other tasks.
  137. -# The caller can use the CheckEvent() and WaitForEvent() services to check
  138. the state of the WaitEvent created in step 1.
  139. -# When the APs complete their task or TimeoutInMicroSeconds expires, the MP
  140. Service signals WaitEvent by calling the EFI SignalEvent() function. If
  141. FailedCpuList is not NULL, its content is available when WaitEvent is
  142. signaled. If all APs returned from Procedure prior to the timeout, then
  143. FailedCpuList is set to NULL. If not all APs return from Procedure before
  144. the timeout, then FailedCpuList is filled in with the list of the failed
  145. APs. The buffer is allocated by MP Service Protocol using AllocatePool().
  146. It is the caller's responsibility to free the buffer with FreePool() service.
  147. -# This invocation of SignalEvent() function informs the caller that invoked
  148. EFI_MP_SERVICES_PROTOCOL.StartupAllAPs() that either all the APs completed
  149. the specified task or a timeout occurred. The contents of FailedCpuList
  150. can be examined to determine which APs did not complete the specified task
  151. prior to the timeout.
  152. @param[in] This A pointer to the EFI_MP_SERVICES_PROTOCOL
  153. instance.
  154. @param[in] Procedure A pointer to the function to be run on
  155. enabled APs of the system. See type
  156. EFI_AP_PROCEDURE.
  157. @param[in] SingleThread If TRUE, then all the enabled APs execute
  158. the function specified by Procedure one by
  159. one, in ascending order of processor handle
  160. number. If FALSE, then all the enabled APs
  161. execute the function specified by Procedure
  162. simultaneously.
  163. @param[in] WaitEvent The event created by the caller with CreateEvent()
  164. service. If it is NULL, then execute in
  165. blocking mode. BSP waits until all APs finish
  166. or TimeoutInMicroseconds expires. If it's
  167. not NULL, then execute in non-blocking mode.
  168. BSP requests the function specified by
  169. Procedure to be started on all the enabled
  170. APs, and go on executing immediately. If
  171. all return from Procedure, or TimeoutInMicroseconds
  172. expires, this event is signaled. The BSP
  173. can use the CheckEvent() or WaitForEvent()
  174. services to check the state of event. Type
  175. EFI_EVENT is defined in CreateEvent() in
  176. the Unified Extensible Firmware Interface
  177. Specification.
  178. @param[in] TimeoutInMicroseconds Indicates the time limit in microseconds for
  179. APs to return from Procedure, either for
  180. blocking or non-blocking mode. Zero means
  181. infinity. If the timeout expires before
  182. all APs return from Procedure, then Procedure
  183. on the failed APs is terminated. All enabled
  184. APs are available for next function assigned
  185. by EFI_MP_SERVICES_PROTOCOL.StartupAllAPs()
  186. or EFI_MP_SERVICES_PROTOCOL.StartupThisAP().
  187. If the timeout expires in blocking mode,
  188. BSP returns EFI_TIMEOUT. If the timeout
  189. expires in non-blocking mode, WaitEvent
  190. is signaled with SignalEvent().
  191. @param[in] ProcedureArgument The parameter passed into Procedure for
  192. all APs.
  193. @param[out] FailedCpuList If NULL, this parameter is ignored. Otherwise,
  194. if all APs finish successfully, then its
  195. content is set to NULL. If not all APs
  196. finish before timeout expires, then its
  197. content is set to address of the buffer
  198. holding handle numbers of the failed APs.
  199. The buffer is allocated by MP Service Protocol,
  200. and it's the caller's responsibility to
  201. free the buffer with FreePool() service.
  202. In blocking mode, it is ready for consumption
  203. when the call returns. In non-blocking mode,
  204. it is ready when WaitEvent is signaled. The
  205. list of failed CPU is terminated by
  206. END_OF_CPU_LIST.
  207. @retval EFI_SUCCESS In blocking mode, all APs have finished before
  208. the timeout expired.
  209. @retval EFI_SUCCESS In non-blocking mode, function has been dispatched
  210. to all enabled APs.
  211. @retval EFI_UNSUPPORTED A non-blocking mode request was made after the
  212. UEFI event EFI_EVENT_GROUP_READY_TO_BOOT was
  213. signaled.
  214. @retval EFI_DEVICE_ERROR Caller processor is AP.
  215. @retval EFI_NOT_STARTED No enabled APs exist in the system.
  216. @retval EFI_NOT_READY Any enabled APs are busy.
  217. @retval EFI_TIMEOUT In blocking mode, the timeout expired before
  218. all enabled APs have finished.
  219. @retval EFI_INVALID_PARAMETER Procedure is NULL.
  220. **/
  221. EFI_STATUS
  222. EFIAPI
  223. StartupAllAPs (
  224. IN EFI_MP_SERVICES_PROTOCOL *This,
  225. IN EFI_AP_PROCEDURE Procedure,
  226. IN BOOLEAN SingleThread,
  227. IN EFI_EVENT WaitEvent OPTIONAL,
  228. IN UINTN TimeoutInMicroseconds,
  229. IN VOID *ProcedureArgument OPTIONAL,
  230. OUT UINTN **FailedCpuList OPTIONAL
  231. )
  232. {
  233. return MpInitLibStartupAllAPs (
  234. Procedure,
  235. SingleThread,
  236. WaitEvent,
  237. TimeoutInMicroseconds,
  238. ProcedureArgument,
  239. FailedCpuList
  240. );
  241. }
  242. /**
  243. This service lets the caller get one enabled AP to execute a caller-provided
  244. function. The caller can request the BSP to either wait for the completion
  245. of the AP or just proceed with the next task by using the EFI event mechanism.
  246. See EFI_MP_SERVICES_PROTOCOL.StartupAllAPs() for more details on non-blocking
  247. execution support. This service may only be called from the BSP.
  248. This function is used to dispatch one enabled AP to the function specified by
  249. Procedure passing in the argument specified by ProcedureArgument. If WaitEvent
  250. is NULL, execution is in blocking mode. The BSP waits until the AP finishes or
  251. TimeoutInMicroSeconds expires. Otherwise, execution is in non-blocking mode.
  252. BSP proceeds to the next task without waiting for the AP. If a non-blocking mode
  253. is requested after the UEFI Event EFI_EVENT_GROUP_READY_TO_BOOT is signaled,
  254. then EFI_UNSUPPORTED must be returned.
  255. If the timeout specified by TimeoutInMicroseconds expires before the AP returns
  256. from Procedure, then execution of Procedure by the AP is terminated. The AP is
  257. available for subsequent calls to EFI_MP_SERVICES_PROTOCOL.StartupAllAPs() and
  258. EFI_MP_SERVICES_PROTOCOL.StartupThisAP().
  259. @param[in] This A pointer to the EFI_MP_SERVICES_PROTOCOL
  260. instance.
  261. @param[in] Procedure A pointer to the function to be run on the
  262. designated AP of the system. See type
  263. EFI_AP_PROCEDURE.
  264. @param[in] ProcessorNumber The handle number of the AP. The range is
  265. from 0 to the total number of logical
  266. processors minus 1. The total number of
  267. logical processors can be retrieved by
  268. EFI_MP_SERVICES_PROTOCOL.GetNumberOfProcessors().
  269. @param[in] WaitEvent The event created by the caller with CreateEvent()
  270. service. If it is NULL, then execute in
  271. blocking mode. BSP waits until this AP finish
  272. or TimeoutInMicroSeconds expires. If it's
  273. not NULL, then execute in non-blocking mode.
  274. BSP requests the function specified by
  275. Procedure to be started on this AP,
  276. and go on executing immediately. If this AP
  277. return from Procedure or TimeoutInMicroSeconds
  278. expires, this event is signaled. The BSP
  279. can use the CheckEvent() or WaitForEvent()
  280. services to check the state of event. Type
  281. EFI_EVENT is defined in CreateEvent() in
  282. the Unified Extensible Firmware Interface
  283. Specification.
  284. @param[in] TimeoutInMicroseconds Indicates the time limit in microseconds for
  285. this AP to finish this Procedure, either for
  286. blocking or non-blocking mode. Zero means
  287. infinity. If the timeout expires before
  288. this AP returns from Procedure, then Procedure
  289. on the AP is terminated. The
  290. AP is available for next function assigned
  291. by EFI_MP_SERVICES_PROTOCOL.StartupAllAPs()
  292. or EFI_MP_SERVICES_PROTOCOL.StartupThisAP().
  293. If the timeout expires in blocking mode,
  294. BSP returns EFI_TIMEOUT. If the timeout
  295. expires in non-blocking mode, WaitEvent
  296. is signaled with SignalEvent().
  297. @param[in] ProcedureArgument The parameter passed into Procedure on the
  298. specified AP.
  299. @param[out] Finished If NULL, this parameter is ignored. In
  300. blocking mode, this parameter is ignored.
  301. In non-blocking mode, if AP returns from
  302. Procedure before the timeout expires, its
  303. content is set to TRUE. Otherwise, the
  304. value is set to FALSE. The caller can
  305. determine if the AP returned from Procedure
  306. by evaluating this value.
  307. @retval EFI_SUCCESS In blocking mode, specified AP finished before
  308. the timeout expires.
  309. @retval EFI_SUCCESS In non-blocking mode, the function has been
  310. dispatched to specified AP.
  311. @retval EFI_UNSUPPORTED A non-blocking mode request was made after the
  312. UEFI event EFI_EVENT_GROUP_READY_TO_BOOT was
  313. signaled.
  314. @retval EFI_DEVICE_ERROR The calling processor is an AP.
  315. @retval EFI_TIMEOUT In blocking mode, the timeout expired before
  316. the specified AP has finished.
  317. @retval EFI_NOT_READY The specified AP is busy.
  318. @retval EFI_NOT_FOUND The processor with the handle specified by
  319. ProcessorNumber does not exist.
  320. @retval EFI_INVALID_PARAMETER ProcessorNumber specifies the BSP or disabled AP.
  321. @retval EFI_INVALID_PARAMETER Procedure is NULL.
  322. **/
  323. EFI_STATUS
  324. EFIAPI
  325. StartupThisAP (
  326. IN EFI_MP_SERVICES_PROTOCOL *This,
  327. IN EFI_AP_PROCEDURE Procedure,
  328. IN UINTN ProcessorNumber,
  329. IN EFI_EVENT WaitEvent OPTIONAL,
  330. IN UINTN TimeoutInMicroseconds,
  331. IN VOID *ProcedureArgument OPTIONAL,
  332. OUT BOOLEAN *Finished OPTIONAL
  333. )
  334. {
  335. return MpInitLibStartupThisAP (
  336. Procedure,
  337. ProcessorNumber,
  338. WaitEvent,
  339. TimeoutInMicroseconds,
  340. ProcedureArgument,
  341. Finished
  342. );
  343. }
  344. /**
  345. This service switches the requested AP to be the BSP from that point onward.
  346. This service changes the BSP for all purposes. This call can only be performed
  347. by the current BSP.
  348. This service switches the requested AP to be the BSP from that point onward.
  349. This service changes the BSP for all purposes. The new BSP can take over the
  350. execution of the old BSP and continue seamlessly from where the old one left
  351. off. This service may not be supported after the UEFI Event EFI_EVENT_GROUP_READY_TO_BOOT
  352. is signaled.
  353. If the BSP cannot be switched prior to the return from this service, then
  354. EFI_UNSUPPORTED must be returned.
  355. @param[in] This A pointer to the EFI_MP_SERVICES_PROTOCOL instance.
  356. @param[in] ProcessorNumber The handle number of AP that is to become the new
  357. BSP. The range is from 0 to the total number of
  358. logical processors minus 1. The total number of
  359. logical processors can be retrieved by
  360. EFI_MP_SERVICES_PROTOCOL.GetNumberOfProcessors().
  361. @param[in] EnableOldBSP If TRUE, then the old BSP will be listed as an
  362. enabled AP. Otherwise, it will be disabled.
  363. @retval EFI_SUCCESS BSP successfully switched.
  364. @retval EFI_UNSUPPORTED Switching the BSP cannot be completed prior to
  365. this service returning.
  366. @retval EFI_UNSUPPORTED Switching the BSP is not supported.
  367. @retval EFI_DEVICE_ERROR The calling processor is an AP.
  368. @retval EFI_NOT_FOUND The processor with the handle specified by
  369. ProcessorNumber does not exist.
  370. @retval EFI_INVALID_PARAMETER ProcessorNumber specifies the current BSP or
  371. a disabled AP.
  372. @retval EFI_NOT_READY The specified AP is busy.
  373. **/
  374. EFI_STATUS
  375. EFIAPI
  376. SwitchBSP (
  377. IN EFI_MP_SERVICES_PROTOCOL *This,
  378. IN UINTN ProcessorNumber,
  379. IN BOOLEAN EnableOldBSP
  380. )
  381. {
  382. return MpInitLibSwitchBSP (ProcessorNumber, EnableOldBSP);
  383. }
  384. /**
  385. This service lets the caller enable or disable an AP from this point onward.
  386. This service may only be called from the BSP.
  387. This service allows the caller enable or disable an AP from this point onward.
  388. The caller can optionally specify the health status of the AP by Health. If
  389. an AP is being disabled, then the state of the disabled AP is implementation
  390. dependent. If an AP is enabled, then the implementation must guarantee that a
  391. complete initialization sequence is performed on the AP, so the AP is in a state
  392. that is compatible with an MP operating system. This service may not be supported
  393. after the UEFI Event EFI_EVENT_GROUP_READY_TO_BOOT is signaled.
  394. If the enable or disable AP operation cannot be completed prior to the return
  395. from this service, then EFI_UNSUPPORTED must be returned.
  396. @param[in] This A pointer to the EFI_MP_SERVICES_PROTOCOL instance.
  397. @param[in] ProcessorNumber The handle number of AP.
  398. The range is from 0 to the total number of
  399. logical processors minus 1. The total number of
  400. logical processors can be retrieved by
  401. EFI_MP_SERVICES_PROTOCOL.GetNumberOfProcessors().
  402. @param[in] EnableAP Specifies the new state for the processor for
  403. enabled, FALSE for disabled.
  404. @param[in] HealthFlag If not NULL, a pointer to a value that specifies
  405. the new health status of the AP. This flag
  406. corresponds to StatusFlag defined in
  407. EFI_MP_SERVICES_PROTOCOL.GetProcessorInfo(). Only
  408. the PROCESSOR_HEALTH_STATUS_BIT is used. All other
  409. bits are ignored. If it is NULL, this parameter
  410. is ignored.
  411. @retval EFI_SUCCESS The specified AP was enabled or disabled successfully.
  412. @retval EFI_UNSUPPORTED Enabling or disabling an AP cannot be completed
  413. prior to this service returning.
  414. @retval EFI_UNSUPPORTED Enabling or disabling an AP is not supported.
  415. @retval EFI_DEVICE_ERROR The calling processor is an AP.
  416. @retval EFI_NOT_FOUND Processor with the handle specified by ProcessorNumber
  417. does not exist.
  418. @retval EFI_INVALID_PARAMETER ProcessorNumber specifies the BSP.
  419. **/
  420. EFI_STATUS
  421. EFIAPI
  422. EnableDisableAP (
  423. IN EFI_MP_SERVICES_PROTOCOL *This,
  424. IN UINTN ProcessorNumber,
  425. IN BOOLEAN EnableAP,
  426. IN UINT32 *HealthFlag OPTIONAL
  427. )
  428. {
  429. return MpInitLibEnableDisableAP (ProcessorNumber, EnableAP, HealthFlag);
  430. }
  431. /**
  432. This return the handle number for the calling processor. This service may be
  433. called from the BSP and APs.
  434. This service returns the processor handle number for the calling processor.
  435. The returned value is in the range from 0 to the total number of logical
  436. processors minus 1. The total number of logical processors can be retrieved
  437. with EFI_MP_SERVICES_PROTOCOL.GetNumberOfProcessors(). This service may be
  438. called from the BSP and APs. If ProcessorNumber is NULL, then EFI_INVALID_PARAMETER
  439. is returned. Otherwise, the current processors handle number is returned in
  440. ProcessorNumber, and EFI_SUCCESS is returned.
  441. @param[in] This A pointer to the EFI_MP_SERVICES_PROTOCOL instance.
  442. @param[out] ProcessorNumber Pointer to the handle number of AP.
  443. The range is from 0 to the total number of
  444. logical processors minus 1. The total number of
  445. logical processors can be retrieved by
  446. EFI_MP_SERVICES_PROTOCOL.GetNumberOfProcessors().
  447. @retval EFI_SUCCESS The current processor handle number was returned
  448. in ProcessorNumber.
  449. @retval EFI_INVALID_PARAMETER ProcessorNumber is NULL.
  450. **/
  451. EFI_STATUS
  452. EFIAPI
  453. WhoAmI (
  454. IN EFI_MP_SERVICES_PROTOCOL *This,
  455. OUT UINTN *ProcessorNumber
  456. )
  457. {
  458. return MpInitLibWhoAmI (ProcessorNumber);
  459. }
  460. /**
  461. Collects BIST data from HOB.
  462. This function collects BIST data from HOB built from Sec Platform Information
  463. PPI or SEC Platform Information2 PPI.
  464. **/
  465. VOID
  466. CollectBistDataFromHob (
  467. VOID
  468. )
  469. {
  470. EFI_HOB_GUID_TYPE *GuidHob;
  471. EFI_SEC_PLATFORM_INFORMATION_RECORD2 *SecPlatformInformation2;
  472. EFI_SEC_PLATFORM_INFORMATION_RECORD *SecPlatformInformation;
  473. UINTN NumberOfData;
  474. EFI_SEC_PLATFORM_INFORMATION_CPU *CpuInstance;
  475. EFI_SEC_PLATFORM_INFORMATION_CPU BspCpuInstance;
  476. UINTN ProcessorNumber;
  477. EFI_PROCESSOR_INFORMATION ProcessorInfo;
  478. EFI_HEALTH_FLAGS BistData;
  479. UINTN CpuInstanceNumber;
  480. SecPlatformInformation2 = NULL;
  481. SecPlatformInformation = NULL;
  482. //
  483. // Get gEfiSecPlatformInformation2PpiGuid Guided HOB firstly
  484. //
  485. GuidHob = GetFirstGuidHob (&gEfiSecPlatformInformation2PpiGuid);
  486. if (GuidHob != NULL) {
  487. //
  488. // Sec Platform Information2 PPI includes BSP/APs' BIST information
  489. //
  490. SecPlatformInformation2 = GET_GUID_HOB_DATA (GuidHob);
  491. NumberOfData = SecPlatformInformation2->NumberOfCpus;
  492. CpuInstance = SecPlatformInformation2->CpuInstance;
  493. } else {
  494. //
  495. // Otherwise, get gEfiSecPlatformInformationPpiGuid Guided HOB
  496. //
  497. GuidHob = GetFirstGuidHob (&gEfiSecPlatformInformationPpiGuid);
  498. if (GuidHob != NULL) {
  499. SecPlatformInformation = GET_GUID_HOB_DATA (GuidHob);
  500. NumberOfData = 1;
  501. //
  502. // SEC Platform Information only includes BSP's BIST information
  503. // does not have BSP's APIC ID
  504. //
  505. BspCpuInstance.CpuLocation = GetApicId ();
  506. BspCpuInstance.InfoRecord.IA32HealthFlags.Uint32 = SecPlatformInformation->IA32HealthFlags.Uint32;
  507. CpuInstance = &BspCpuInstance;
  508. } else {
  509. DEBUG ((DEBUG_INFO, "Does not find any HOB stored CPU BIST information!\n"));
  510. //
  511. // Does not find any HOB stored BIST information
  512. //
  513. return;
  514. }
  515. }
  516. for (ProcessorNumber = 0; ProcessorNumber < mNumberOfProcessors; ProcessorNumber++) {
  517. MpInitLibGetProcessorInfo (ProcessorNumber, &ProcessorInfo, &BistData);
  518. for (CpuInstanceNumber = 0; CpuInstanceNumber < NumberOfData; CpuInstanceNumber++) {
  519. if (ProcessorInfo.ProcessorId == CpuInstance[CpuInstanceNumber].CpuLocation) {
  520. //
  521. // Update CPU health status for MP Services Protocol according to BIST data.
  522. //
  523. BistData = CpuInstance[CpuInstanceNumber].InfoRecord.IA32HealthFlags;
  524. }
  525. }
  526. if (BistData.Uint32 != 0) {
  527. //
  528. // Report Status Code that self test is failed
  529. //
  530. REPORT_STATUS_CODE (
  531. EFI_ERROR_CODE | EFI_ERROR_MAJOR,
  532. (EFI_COMPUTING_UNIT_HOST_PROCESSOR | EFI_CU_HP_EC_SELF_TEST)
  533. );
  534. }
  535. }
  536. }
  537. /**
  538. Get GDT register value.
  539. This function is mainly for AP purpose because AP may have different GDT
  540. table than BSP.
  541. @param[in,out] Buffer The pointer to private data buffer.
  542. **/
  543. VOID
  544. EFIAPI
  545. GetGdtr (
  546. IN OUT VOID *Buffer
  547. )
  548. {
  549. AsmReadGdtr ((IA32_DESCRIPTOR *)Buffer);
  550. }
  551. /**
  552. Initializes CPU exceptions handlers for the sake of stack switch requirement.
  553. This function is a wrapper of InitializeCpuExceptionHandlersEx. It's mainly
  554. for the sake of AP's init because of EFI_AP_PROCEDURE API requirement.
  555. @param[in,out] Buffer The pointer to private data buffer.
  556. **/
  557. VOID
  558. EFIAPI
  559. InitializeExceptionStackSwitchHandlers (
  560. IN OUT VOID *Buffer
  561. )
  562. {
  563. CPU_EXCEPTION_INIT_DATA *EssData;
  564. IA32_DESCRIPTOR Idtr;
  565. EFI_STATUS Status;
  566. EssData = Buffer;
  567. //
  568. // We don't plan to replace IDT table with a new one, but we should not assume
  569. // the AP's IDT is the same as BSP's IDT either.
  570. //
  571. AsmReadIdtr (&Idtr);
  572. EssData->Ia32.IdtTable = (VOID *)Idtr.Base;
  573. EssData->Ia32.IdtTableSize = Idtr.Limit + 1;
  574. Status = InitializeCpuExceptionHandlersEx (NULL, EssData);
  575. ASSERT_EFI_ERROR (Status);
  576. }
  577. /**
  578. Initializes MP exceptions handlers for the sake of stack switch requirement.
  579. This function will allocate required resources required to setup stack switch
  580. and pass them through CPU_EXCEPTION_INIT_DATA to each logic processor.
  581. **/
  582. VOID
  583. InitializeMpExceptionStackSwitchHandlers (
  584. VOID
  585. )
  586. {
  587. UINTN Index;
  588. UINTN Bsp;
  589. UINTN ExceptionNumber;
  590. UINTN OldGdtSize;
  591. UINTN NewGdtSize;
  592. UINTN NewStackSize;
  593. IA32_DESCRIPTOR Gdtr;
  594. CPU_EXCEPTION_INIT_DATA EssData;
  595. UINT8 *GdtBuffer;
  596. UINT8 *StackTop;
  597. ExceptionNumber = FixedPcdGetSize (PcdCpuStackSwitchExceptionList);
  598. NewStackSize = FixedPcdGet32 (PcdCpuKnownGoodStackSize) * ExceptionNumber;
  599. StackTop = AllocateRuntimeZeroPool (NewStackSize * mNumberOfProcessors);
  600. ASSERT (StackTop != NULL);
  601. StackTop += NewStackSize * mNumberOfProcessors;
  602. //
  603. // The default exception handlers must have been initialized. Let's just skip
  604. // it in this method.
  605. //
  606. EssData.Ia32.Revision = CPU_EXCEPTION_INIT_DATA_REV;
  607. EssData.Ia32.InitDefaultHandlers = FALSE;
  608. EssData.Ia32.StackSwitchExceptions = FixedPcdGetPtr (PcdCpuStackSwitchExceptionList);
  609. EssData.Ia32.StackSwitchExceptionNumber = ExceptionNumber;
  610. EssData.Ia32.KnownGoodStackSize = FixedPcdGet32 (PcdCpuKnownGoodStackSize);
  611. //
  612. // Initialize Gdtr to suppress incorrect compiler/analyzer warnings.
  613. //
  614. Gdtr.Base = 0;
  615. Gdtr.Limit = 0;
  616. MpInitLibWhoAmI (&Bsp);
  617. for (Index = 0; Index < mNumberOfProcessors; ++Index) {
  618. //
  619. // To support stack switch, we need to re-construct GDT but not IDT.
  620. //
  621. if (Index == Bsp) {
  622. GetGdtr (&Gdtr);
  623. } else {
  624. //
  625. // AP might have different size of GDT from BSP.
  626. //
  627. MpInitLibStartupThisAP (GetGdtr, Index, NULL, 0, (VOID *)&Gdtr, NULL);
  628. }
  629. //
  630. // X64 needs only one TSS of current task working for all exceptions
  631. // because of its IST feature. IA32 needs one TSS for each exception
  632. // in addition to current task. Since AP is not supposed to allocate
  633. // memory, we have to do it in BSP. To simplify the code, we allocate
  634. // memory for IA32 case to cover both IA32 and X64 exception stack
  635. // switch.
  636. //
  637. // Layout of memory to allocate for each processor:
  638. // --------------------------------
  639. // | Alignment | (just in case)
  640. // --------------------------------
  641. // | |
  642. // | Original GDT |
  643. // | |
  644. // --------------------------------
  645. // | Current task descriptor |
  646. // --------------------------------
  647. // | |
  648. // | Exception task descriptors | X ExceptionNumber
  649. // | |
  650. // --------------------------------
  651. // | Current task-state segment |
  652. // --------------------------------
  653. // | |
  654. // | Exception task-state segment | X ExceptionNumber
  655. // | |
  656. // --------------------------------
  657. //
  658. OldGdtSize = Gdtr.Limit + 1;
  659. EssData.Ia32.ExceptionTssDescSize = sizeof (IA32_TSS_DESCRIPTOR) *
  660. (ExceptionNumber + 1);
  661. EssData.Ia32.ExceptionTssSize = sizeof (IA32_TASK_STATE_SEGMENT) *
  662. (ExceptionNumber + 1);
  663. NewGdtSize = sizeof (IA32_TSS_DESCRIPTOR) +
  664. OldGdtSize +
  665. EssData.Ia32.ExceptionTssDescSize +
  666. EssData.Ia32.ExceptionTssSize;
  667. GdtBuffer = AllocateRuntimeZeroPool (NewGdtSize);
  668. ASSERT (GdtBuffer != NULL);
  669. //
  670. // Make sure GDT table alignment
  671. //
  672. EssData.Ia32.GdtTable = ALIGN_POINTER (GdtBuffer, sizeof (IA32_TSS_DESCRIPTOR));
  673. NewGdtSize -= ((UINT8 *)EssData.Ia32.GdtTable - GdtBuffer);
  674. EssData.Ia32.GdtTableSize = NewGdtSize;
  675. EssData.Ia32.ExceptionTssDesc = ((UINT8 *)EssData.Ia32.GdtTable + OldGdtSize);
  676. EssData.Ia32.ExceptionTss = ((UINT8 *)EssData.Ia32.GdtTable + OldGdtSize +
  677. EssData.Ia32.ExceptionTssDescSize);
  678. EssData.Ia32.KnownGoodStackTop = (UINTN)StackTop;
  679. DEBUG ((
  680. DEBUG_INFO,
  681. "Exception stack top[cpu%lu]: 0x%lX\n",
  682. (UINT64)(UINTN)Index,
  683. (UINT64)(UINTN)StackTop
  684. ));
  685. if (Index == Bsp) {
  686. InitializeExceptionStackSwitchHandlers (&EssData);
  687. } else {
  688. MpInitLibStartupThisAP (
  689. InitializeExceptionStackSwitchHandlers,
  690. Index,
  691. NULL,
  692. 0,
  693. (VOID *)&EssData,
  694. NULL
  695. );
  696. }
  697. StackTop -= NewStackSize;
  698. }
  699. }
  700. /**
  701. Initializes MP exceptions handlers for special features, such as Heap Guard
  702. and Stack Guard.
  703. **/
  704. VOID
  705. InitializeMpExceptionHandlers (
  706. VOID
  707. )
  708. {
  709. //
  710. // Enable non-stop mode for #PF triggered by Heap Guard or NULL Pointer
  711. // Detection.
  712. //
  713. if (HEAP_GUARD_NONSTOP_MODE || NULL_DETECTION_NONSTOP_MODE) {
  714. RegisterCpuInterruptHandler (EXCEPT_IA32_DEBUG, DebugExceptionHandler);
  715. RegisterCpuInterruptHandler (EXCEPT_IA32_PAGE_FAULT, PageFaultExceptionHandler);
  716. }
  717. //
  718. // Setup stack switch for Stack Guard feature.
  719. //
  720. if (PcdGetBool (PcdCpuStackGuard)) {
  721. InitializeMpExceptionStackSwitchHandlers ();
  722. }
  723. }
  724. /**
  725. Initialize Multi-processor support.
  726. **/
  727. VOID
  728. InitializeMpSupport (
  729. VOID
  730. )
  731. {
  732. EFI_STATUS Status;
  733. UINTN NumberOfProcessors;
  734. UINTN NumberOfEnabledProcessors;
  735. //
  736. // Wakeup APs to do initialization
  737. //
  738. Status = MpInitLibInitialize ();
  739. ASSERT_EFI_ERROR (Status);
  740. MpInitLibGetNumberOfProcessors (&NumberOfProcessors, &NumberOfEnabledProcessors);
  741. mNumberOfProcessors = NumberOfProcessors;
  742. DEBUG ((DEBUG_INFO, "Detect CPU count: %d\n", mNumberOfProcessors));
  743. //
  744. // Initialize special exception handlers for each logic processor.
  745. //
  746. InitializeMpExceptionHandlers ();
  747. //
  748. // Update CPU healthy information from Guided HOB
  749. //
  750. CollectBistDataFromHob ();
  751. Status = gBS->InstallMultipleProtocolInterfaces (
  752. &mMpServiceHandle,
  753. &gEfiMpServiceProtocolGuid,
  754. &mMpServicesTemplate,
  755. NULL
  756. );
  757. ASSERT_EFI_ERROR (Status);
  758. }