EdkiiPeiMpServices2PpiUnitTest.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477
  1. /** @file
  2. PEI Module to test APIs defined in EdkiiPeiMpServices2Ppi.
  3. Copyright (c) 2022, Intel Corporation. All rights reserved.<BR>
  4. SPDX-License-Identifier: BSD-2-Clause-Patent
  5. **/
  6. #include <Library/PeimEntryPoint.h>
  7. #include <Library/PeiServicesLib.h>
  8. #include "EfiMpServicesUnitTestCommom.h"
  9. #define UNIT_TEST_NAME "EdkiiPeiMpServices2Ppi Unit Test"
  10. #define UNIT_TEST_VERSION "0.1"
  11. /**
  12. Get EDKII_PEI_MP_SERVICES2_PPI pointer.
  13. @param[out] MpServices Pointer to the buffer where EDKII_PEI_MP_SERVICES2_PPI is stored.
  14. @retval EFI_SUCCESS EDKII_PEI_MP_SERVICES2_PPI interface is returned
  15. @retval EFI_NOT_FOUND EDKII_PEI_MP_SERVICES2_PPI interface is not found
  16. **/
  17. EFI_STATUS
  18. MpServicesUnitTestGetMpServices (
  19. OUT MP_SERVICES *MpServices
  20. )
  21. {
  22. return PeiServicesLocatePpi (&gEdkiiPeiMpServices2PpiGuid, 0, NULL, (VOID **)&MpServices->Ppi);
  23. }
  24. /**
  25. Retrieve the number of logical processor in the platform and the number of those logical processors that
  26. are enabled on this boot.
  27. @param[in] MpServices MP_SERVICES structure.
  28. @param[out] NumberOfProcessors Pointer to the total number of logical processors in the system, including
  29. the BSP and disabled APs.
  30. @param[out] NumberOfEnabledProcessors Pointer to the number of processors in the system that are enabled.
  31. @retval EFI_SUCCESS Retrieve the number of logical processor successfully
  32. @retval Others Retrieve the number of logical processor unsuccessfully
  33. **/
  34. EFI_STATUS
  35. MpServicesUnitTestGetNumberOfProcessors (
  36. IN MP_SERVICES MpServices,
  37. OUT UINTN *NumberOfProcessors,
  38. OUT UINTN *NumberOfEnabledProcessors
  39. )
  40. {
  41. return MpServices.Ppi->GetNumberOfProcessors (MpServices.Ppi, NumberOfProcessors, NumberOfEnabledProcessors);
  42. }
  43. /**
  44. Get detailed information on the requested logical processor.
  45. @param[in] MpServices MP_SERVICES structure.
  46. @param[in] ProcessorNumber The handle number of the processor.
  47. @param[out] ProcessorInfoBuffer Pointer to the buffer where the processor information is stored.
  48. @retval EFI_SUCCESS Get information on the requested logical processor successfully
  49. @retval Others Get information on the requested logical processor unsuccessfully
  50. **/
  51. EFI_STATUS
  52. MpServicesUnitTestGetProcessorInfo (
  53. IN MP_SERVICES MpServices,
  54. IN UINTN ProcessorNumber,
  55. OUT EFI_PROCESSOR_INFORMATION *ProcessorInfoBuffer
  56. )
  57. {
  58. return MpServices.Ppi->GetProcessorInfo (MpServices.Ppi, ProcessorNumber, ProcessorInfoBuffer);
  59. }
  60. /**
  61. Execute a caller provided function on all enabled APs.
  62. @param[in] MpServices MP_SERVICES structure.
  63. @param[in] Procedure Pointer to the function to be run on enabled APs of the system.
  64. @param[in] SingleThread If TRUE, then all the enabled APs execute the function specified by Procedure
  65. one by one, in ascending order of processor handle number.
  66. If FALSE, then all the enabled APs execute the function specified by Procedure
  67. simultaneously.
  68. @param[in] TimeoutInMicroSeconds Indicates the time limit in microseconds for APs to return from Procedure,
  69. for blocking mode only. Zero means infinity.
  70. @param[in] ProcedureArgument The parameter passed into Procedure for all APs.
  71. @retval EFI_SUCCESS Execute a caller provided function on all enabled APs successfully
  72. @retval Others Execute a caller provided function on all enabled APs unsuccessfully
  73. **/
  74. EFI_STATUS
  75. MpServicesUnitTestStartupAllAPs (
  76. IN MP_SERVICES MpServices,
  77. IN EFI_AP_PROCEDURE Procedure,
  78. IN BOOLEAN SingleThread,
  79. IN UINTN TimeoutInMicroSeconds,
  80. IN VOID *ProcedureArgument
  81. )
  82. {
  83. return MpServices.Ppi->StartupAllAPs (MpServices.Ppi, Procedure, SingleThread, TimeoutInMicroSeconds, ProcedureArgument);
  84. }
  85. /**
  86. Caller gets one enabled AP to execute a caller-provided function.
  87. @param[in] MpServices MP_SERVICES structure.
  88. @param[in] Procedure Pointer to the function to be run on enabled APs of the system.
  89. @param[in] ProcessorNumber The handle number of the AP.
  90. @param[in] TimeoutInMicroSeconds Indicates the time limit in microseconds for APs to return from Procedure,
  91. for blocking mode only. Zero means infinity.
  92. @param[in] ProcedureArgument The parameter passed into Procedure for all APs.
  93. @retval EFI_SUCCESS Caller gets one enabled AP to execute a caller-provided function successfully
  94. @retval Others Caller gets one enabled AP to execute a caller-provided function unsuccessfully
  95. **/
  96. EFI_STATUS
  97. MpServicesUnitTestStartupThisAP (
  98. IN MP_SERVICES MpServices,
  99. IN EFI_AP_PROCEDURE Procedure,
  100. IN UINTN ProcessorNumber,
  101. IN UINTN TimeoutInMicroSeconds,
  102. IN VOID *ProcedureArgument
  103. )
  104. {
  105. return MpServices.Ppi->StartupThisAP (MpServices.Ppi, Procedure, ProcessorNumber, TimeoutInMicroSeconds, ProcedureArgument);
  106. }
  107. /**
  108. Switch the requested AP to be the BSP from that point onward.
  109. @param[in] MpServices MP_SERVICES structure.
  110. @param[in] ProcessorNumber The handle number of AP that is to become the new BSP.
  111. @param[in] EnableOldBSP If TRUE, the old BSP will be listed as an enabled AP. Otherwise, it will be disabled.
  112. @retval EFI_SUCCESS Switch the requested AP to be the BSP successfully
  113. @retval Others Switch the requested AP to be the BSP unsuccessfully
  114. **/
  115. EFI_STATUS
  116. MpServicesUnitTestSwitchBSP (
  117. IN MP_SERVICES MpServices,
  118. IN UINTN ProcessorNumber,
  119. IN BOOLEAN EnableOldBSP
  120. )
  121. {
  122. return MpServices.Ppi->SwitchBSP (MpServices.Ppi, ProcessorNumber, EnableOldBSP);
  123. }
  124. /**
  125. Caller enables or disables an AP from this point onward.
  126. @param[in] MpServices MP_SERVICES structure.
  127. @param[in] ProcessorNumber The handle number of the AP.
  128. @param[in] EnableAP Specifies the new state for the processor for enabled, FALSE for disabled.
  129. @param[in] HealthFlag If not NULL, a pointer to a value that specifies the new health status of the AP.
  130. @retval EFI_SUCCESS Caller enables or disables an AP successfully.
  131. @retval Others Caller enables or disables an AP unsuccessfully.
  132. **/
  133. EFI_STATUS
  134. MpServicesUnitTestEnableDisableAP (
  135. IN MP_SERVICES MpServices,
  136. IN UINTN ProcessorNumber,
  137. IN BOOLEAN EnableAP,
  138. IN UINT32 *HealthFlag
  139. )
  140. {
  141. return MpServices.Ppi->EnableDisableAP (MpServices.Ppi, ProcessorNumber, EnableAP, HealthFlag);
  142. }
  143. /**
  144. Get the handle number for the calling processor.
  145. @param[in] MpServices MP_SERVICES structure.
  146. @param[out] ProcessorNumber The handle number for the calling processor.
  147. @retval EFI_SUCCESS Get the handle number for the calling processor successfully.
  148. @retval Others Get the handle number for the calling processor unsuccessfully.
  149. **/
  150. EFI_STATUS
  151. MpServicesUnitTestWhoAmI (
  152. IN MP_SERVICES MpServices,
  153. OUT UINTN *ProcessorNumber
  154. )
  155. {
  156. return MpServices.Ppi->WhoAmI (MpServices.Ppi, ProcessorNumber);
  157. }
  158. /**
  159. Execute a caller provided function on all enabled CPUs.
  160. @param[in] MpServices MP_SERVICES structure.
  161. @param[in] Procedure Pointer to the function to be run on enabled CPUs of the system.
  162. @param[in] TimeoutInMicroSeconds Indicates the time limit in microseconds for APs to return from Procedure,
  163. for blocking mode only. Zero means infinity.
  164. @param[in] ProcedureArgument The parameter passed into Procedure for all enabled CPUs.
  165. @retval EFI_SUCCESS Execute a caller provided function on all enabled CPUs successfully
  166. @retval Others Execute a caller provided function on all enabled CPUs unsuccessfully
  167. **/
  168. EFI_STATUS
  169. MpServicesUnitTestStartupAllCPUs (
  170. IN MP_SERVICES MpServices,
  171. IN EFI_AP_PROCEDURE Procedure,
  172. IN UINTN TimeoutInMicroSeconds,
  173. IN VOID *ProcedureArgument
  174. )
  175. {
  176. return MpServices.Ppi->StartupAllCPUs (MpServices.Ppi, Procedure, TimeoutInMicroSeconds, ProcedureArgument);
  177. }
  178. /**
  179. Infinite loop procedure to be run on specified AP.
  180. @param[in,out] Buffer The pointer to private data buffer.
  181. **/
  182. VOID
  183. ApInfiniteLoopProcedure (
  184. IN OUT VOID *Buffer
  185. )
  186. {
  187. EFI_STATUS Status;
  188. UINTN ProcessorNumber;
  189. volatile BOOLEAN InfiniteLoop;
  190. MP_SERVICE_UT_CONTEXT *LocalContext;
  191. LocalContext = (MP_SERVICE_UT_CONTEXT *)Buffer;
  192. Status = MpServicesUnitTestWhoAmI (LocalContext->MpServices, &ProcessorNumber);
  193. ASSERT_EFI_ERROR (Status);
  194. if (ProcessorNumber == LocalContext->BspNumber) {
  195. InfiniteLoop = FALSE;
  196. } else {
  197. InfiniteLoop = TRUE;
  198. }
  199. while (InfiniteLoop) {
  200. }
  201. }
  202. /**
  203. Procedure to run MP service StartupAllCPUs on AP.
  204. @param[in,out] Buffer The pointer to private data buffer.
  205. **/
  206. VOID
  207. RunMpServiceStartupAllCPUsOnAp (
  208. IN OUT VOID *Buffer
  209. )
  210. {
  211. MP_SERVICE_UT_CONTEXT *LocalContext;
  212. LocalContext = (MP_SERVICE_UT_CONTEXT *)Buffer;
  213. LocalContext->ApProcedureReturnStatus = MpServicesUnitTestStartupAllCPUs (
  214. LocalContext->MpServices,
  215. (EFI_AP_PROCEDURE)EmptyProcedure,
  216. 0,
  217. NULL
  218. );
  219. }
  220. /**
  221. Unit test of PEI MP service StartupAllCPU.
  222. All CPUs should execute the Procedure.
  223. @param[in] Context Context pointer for this test.
  224. @retval UNIT_TEST_PASSED The Unit test has completed and the test
  225. case was successful.
  226. @retval UNIT_TEST_ERROR_TEST_FAILED A test case assertion has failed.
  227. **/
  228. UNIT_TEST_STATUS
  229. EFIAPI
  230. TestStartupAllCPUs1 (
  231. IN UNIT_TEST_CONTEXT Context
  232. )
  233. {
  234. EFI_STATUS Status;
  235. UINTN ProcessorIndex;
  236. MP_SERVICE_UT_CONTEXT *LocalContext;
  237. LocalContext = (MP_SERVICE_UT_CONTEXT *)Context;
  238. SetMem (LocalContext->CommonBuffer, LocalContext->NumberOfProcessors * sizeof (*LocalContext->CommonBuffer), 0xFF);
  239. Status = MpServicesUnitTestStartupAllCPUs (
  240. LocalContext->MpServices,
  241. (EFI_AP_PROCEDURE)StoreCpuNumbers,
  242. 0,
  243. (VOID *)LocalContext
  244. );
  245. UT_ASSERT_NOT_EFI_ERROR (Status);
  246. for (ProcessorIndex = 0; ProcessorIndex < LocalContext->NumberOfProcessors; ProcessorIndex++) {
  247. UT_ASSERT_TRUE (LocalContext->CommonBuffer[ProcessorIndex] == ProcessorIndex);
  248. }
  249. return UNIT_TEST_PASSED;
  250. }
  251. /**
  252. Unit test of PEI MP service StartupAllCPU.
  253. When this service is called from an AP, the return status should be EFI_DEVICE_ERROR.
  254. @param[in] Context Context pointer for this test.
  255. @retval UNIT_TEST_PASSED The Unit test has completed and the test
  256. case was successful.
  257. @retval UNIT_TEST_ERROR_TEST_FAILED A test case assertion has failed.
  258. **/
  259. UNIT_TEST_STATUS
  260. EFIAPI
  261. TestStartupAllCPUs2 (
  262. IN UNIT_TEST_CONTEXT Context
  263. )
  264. {
  265. EFI_STATUS Status;
  266. UINTN ApNumber;
  267. MP_SERVICE_UT_CONTEXT *LocalContext;
  268. LocalContext = (MP_SERVICE_UT_CONTEXT *)Context;
  269. for (ApNumber = 0; ApNumber < LocalContext->NumberOfProcessors; ApNumber++) {
  270. LocalContext->ApNumber = ApNumber;
  271. Status = MpServicesUnitTestStartupThisAP (
  272. LocalContext->MpServices,
  273. (EFI_AP_PROCEDURE)RunMpServiceStartupAllCPUsOnAp,
  274. ApNumber,
  275. 0,
  276. (VOID *)LocalContext
  277. );
  278. if (ApNumber == LocalContext->BspNumber) {
  279. UT_ASSERT_STATUS_EQUAL (Status, EFI_INVALID_PARAMETER);
  280. } else {
  281. UT_ASSERT_NOT_EFI_ERROR (Status);
  282. UT_ASSERT_STATUS_EQUAL (LocalContext->ApProcedureReturnStatus, EFI_DEVICE_ERROR);
  283. }
  284. }
  285. return UNIT_TEST_PASSED;
  286. }
  287. /**
  288. Unit test of PEI MP service StartupAllCPU.
  289. When called with all CPUs timeout, the return status should be EFI_TIMEOUT.
  290. @param[in] Context Context pointer for this test.
  291. @retval UNIT_TEST_PASSED The Unit test has completed and the test
  292. case was successful.
  293. @retval UNIT_TEST_ERROR_TEST_FAILED A test case assertion has failed.
  294. **/
  295. UNIT_TEST_STATUS
  296. EFIAPI
  297. TestStartupAllCPUs3 (
  298. IN UNIT_TEST_CONTEXT Context
  299. )
  300. {
  301. EFI_STATUS Status;
  302. MP_SERVICE_UT_CONTEXT *LocalContext;
  303. LocalContext = (MP_SERVICE_UT_CONTEXT *)Context;
  304. Status = MpServicesUnitTestStartupAllCPUs (
  305. LocalContext->MpServices,
  306. (EFI_AP_PROCEDURE)ApInfiniteLoopProcedure,
  307. RUN_PROCEDURE_TIMEOUT_VALUE,
  308. (VOID *)LocalContext
  309. );
  310. UT_ASSERT_STATUS_EQUAL (Status, EFI_TIMEOUT);
  311. return UNIT_TEST_PASSED;
  312. }
  313. /**
  314. Create test suite and unit tests only for EdkiiPeiMpServices2Ppi.
  315. @param[in] Framework A pointer to the framework that is being persisted.
  316. @param[in] Context A pointer to the private data buffer.
  317. @retval EFI_SUCCESS Create test suite and unit tests successfully.
  318. @retval Others Create test suite and unit tests unsuccessfully.
  319. **/
  320. EFI_STATUS
  321. AddTestCaseOnlyForEdkiiPeiMpServices2Ppi (
  322. IN UNIT_TEST_FRAMEWORK_HANDLE Framework,
  323. IN MP_SERVICE_UT_CONTEXT *Context
  324. )
  325. {
  326. EFI_STATUS Status;
  327. UNIT_TEST_SUITE_HANDLE MpServiceStartupAllCPUsTestSuite;
  328. MpServiceStartupAllCPUsTestSuite = NULL;
  329. //
  330. // Test StartupAllCPUs function
  331. //
  332. Status = CreateUnitTestSuite (&MpServiceStartupAllCPUsTestSuite, Framework, "Execute a caller provided function on all enabled CPUs", "MpServices.StartupAllCPUs", NULL, NULL);
  333. if (EFI_ERROR (Status)) {
  334. DEBUG ((DEBUG_ERROR, "Failed in CreateUnitTestSuite for MpServiceStartupAllCPUs Test Suite\n"));
  335. return Status;
  336. }
  337. AddTestCase (MpServiceStartupAllCPUsTestSuite, "Test StartupAllCPUs 1", "TestStartupAllCPUs1", TestStartupAllCPUs1, InitUTContext, CheckUTContext, Context);
  338. AddTestCase (MpServiceStartupAllCPUsTestSuite, "Test StartupAllCPUs 2", "TestStartupAllCPUs2", TestStartupAllCPUs2, InitUTContext, CheckUTContext, Context);
  339. AddTestCase (MpServiceStartupAllCPUsTestSuite, "Test StartupAllCPUs 3", "TestStartupAllCPUs3", TestStartupAllCPUs3, InitUTContext, CheckUTContext, Context);
  340. return EFI_SUCCESS;
  341. }
  342. /**
  343. Standard PEIM entry point for unit test execution from PEI.
  344. Initialize the unit test framework, suite, and unit tests for the EdkiiPeiMpServices2Ppi and run the unit test.
  345. @param[in] FileHandle Handle of the file being invoked.
  346. @param[in] PeiServices Pointer to PEI Services table.
  347. **/
  348. EFI_STATUS
  349. EFIAPI
  350. PeiEntryPoint (
  351. IN EFI_PEI_FILE_HANDLE FileHandle,
  352. IN CONST EFI_PEI_SERVICES **PeiServices
  353. )
  354. {
  355. EFI_STATUS Status;
  356. UNIT_TEST_FRAMEWORK_HANDLE Framework;
  357. MP_SERVICE_UT_CONTEXT Context;
  358. Framework = NULL;
  359. Context.MpServices.Ppi = NULL;
  360. Context.CommonBuffer = NULL;
  361. Context.DisabledApNumber = NULL;
  362. DEBUG ((DEBUG_INFO, "%a v%a\n", UNIT_TEST_NAME, UNIT_TEST_VERSION));
  363. //
  364. // Start setting up the test framework for running the tests.
  365. //
  366. Status = InitUnitTestFramework (&Framework, UNIT_TEST_NAME, gEfiCallerBaseName, UNIT_TEST_VERSION);
  367. if (EFI_ERROR (Status)) {
  368. DEBUG ((DEBUG_ERROR, "Failed in InitUnitTestFramework. Status = %r\n", Status));
  369. goto EXIT;
  370. }
  371. //
  372. // Create test suite and unit tests only for EdkiiPeiMpServices2Ppi.
  373. //
  374. Status = AddTestCaseOnlyForEdkiiPeiMpServices2Ppi (Framework, &Context);
  375. if (EFI_ERROR (Status)) {
  376. DEBUG ((DEBUG_ERROR, "Failed in AddTestCaseOnlyForEdkiiPeiMpServices2Ppi. Status = %r\n", Status));
  377. goto EXIT;
  378. }
  379. //
  380. // Create test suite and unit tests for both EdkiiPeiMpServices2Ppi and EfiMpServiceProtocol.
  381. //
  382. Status = AddCommonTestCase (Framework, &Context);
  383. if (EFI_ERROR (Status)) {
  384. DEBUG ((DEBUG_ERROR, "Failed in AddCommonTestCase. Status = %r\n", Status));
  385. goto EXIT;
  386. }
  387. //
  388. // Execute the tests.
  389. //
  390. Status = RunAllTestSuites (Framework);
  391. EXIT:
  392. if (Framework != NULL) {
  393. FreeUnitTestFramework (Framework);
  394. }
  395. return Status;
  396. }