123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212 |
- /** @file
- Unit tests of the MtrrLib instance of the MtrrLib class
- Copyright (c) 2020, Intel Corporation. All rights reserved.<BR>
- SPDX-License-Identifier: BSD-2-Clause-Patent
- **/
- #include "MtrrLibUnitTest.h"
- STATIC CONST MTRR_LIB_SYSTEM_PARAMETER mDefaultSystemParameter = {
- 42, TRUE, TRUE, CacheUncacheable, 12
- };
- STATIC MTRR_LIB_SYSTEM_PARAMETER mSystemParameters[] = {
- { 38, TRUE, TRUE, CacheUncacheable, 12 },
- { 38, TRUE, TRUE, CacheWriteBack, 12 },
- { 38, TRUE, TRUE, CacheWriteThrough, 12 },
- { 38, TRUE, TRUE, CacheWriteProtected, 12 },
- { 38, TRUE, TRUE, CacheWriteCombining, 12 },
- { 42, TRUE, TRUE, CacheUncacheable, 12 },
- { 42, TRUE, TRUE, CacheWriteBack, 12 },
- { 42, TRUE, TRUE, CacheWriteThrough, 12 },
- { 42, TRUE, TRUE, CacheWriteProtected, 12 },
- { 42, TRUE, TRUE, CacheWriteCombining, 12 },
- { 48, TRUE, TRUE, CacheUncacheable, 12 },
- { 48, TRUE, TRUE, CacheWriteBack, 12 },
- { 48, TRUE, TRUE, CacheWriteThrough, 12 },
- { 48, TRUE, TRUE, CacheWriteProtected, 12 },
- { 48, TRUE, TRUE, CacheWriteCombining, 12 },
- };
- UINT32 mFixedMtrrsIndex[] = {
- MSR_IA32_MTRR_FIX64K_00000,
- MSR_IA32_MTRR_FIX16K_80000,
- MSR_IA32_MTRR_FIX16K_A0000,
- MSR_IA32_MTRR_FIX4K_C0000,
- MSR_IA32_MTRR_FIX4K_C8000,
- MSR_IA32_MTRR_FIX4K_D0000,
- MSR_IA32_MTRR_FIX4K_D8000,
- MSR_IA32_MTRR_FIX4K_E0000,
- MSR_IA32_MTRR_FIX4K_E8000,
- MSR_IA32_MTRR_FIX4K_F0000,
- MSR_IA32_MTRR_FIX4K_F8000
- };
- STATIC_ASSERT (
- (ARRAY_SIZE (mFixedMtrrsIndex) == MTRR_NUMBER_OF_FIXED_MTRR),
- "gFixedMtrrIndex does NOT contain all the fixed MTRRs!"
- );
- //
- // Context structure to be used for most of the test cases.
- //
- typedef struct {
- CONST MTRR_LIB_SYSTEM_PARAMETER *SystemParameter;
- } MTRR_LIB_TEST_CONTEXT;
- //
- // Context structure to be used for GetFirmwareVariableMtrrCount() test.
- //
- typedef struct {
- UINT32 NumberOfReservedVariableMtrrs;
- CONST MTRR_LIB_SYSTEM_PARAMETER *SystemParameter;
- } MTRR_LIB_GET_FIRMWARE_VARIABLE_MTRR_COUNT_CONTEXT;
- STATIC CHAR8 *mCacheDescription[] = { "UC", "WC", "N/A", "N/A", "WT", "WP", "WB" };
- /**
- Compare the actual memory ranges against expected memory ranges and return PASS when they match.
- @param ExpectedMemoryRanges Expected memory ranges.
- @param ExpectedMemoryRangeCount Count of expected memory ranges.
- @param ActualRanges Actual memory ranges.
- @param ActualRangeCount Count of actual memory ranges.
- @retval UNIT_TEST_PASSED Test passed.
- @retval others Test failed.
- **/
- UNIT_TEST_STATUS
- VerifyMemoryRanges (
- IN MTRR_MEMORY_RANGE *ExpectedMemoryRanges,
- IN UINTN ExpectedMemoryRangeCount,
- IN MTRR_MEMORY_RANGE *ActualRanges,
- IN UINTN ActualRangeCount
- )
- {
- UINTN Index;
- UT_ASSERT_EQUAL (ExpectedMemoryRangeCount, ActualRangeCount);
- for (Index = 0; Index < ExpectedMemoryRangeCount; Index++) {
- UT_ASSERT_EQUAL (ExpectedMemoryRanges[Index].BaseAddress, ActualRanges[Index].BaseAddress);
- UT_ASSERT_EQUAL (ExpectedMemoryRanges[Index].Length, ActualRanges[Index].Length);
- UT_ASSERT_EQUAL (ExpectedMemoryRanges[Index].Type, ActualRanges[Index].Type);
- }
- return UNIT_TEST_PASSED;
- }
- /**
- Dump the memory ranges.
- @param Ranges Memory ranges to dump.
- @param RangeCount Count of memory ranges.
- **/
- VOID
- DumpMemoryRanges (
- MTRR_MEMORY_RANGE *Ranges,
- UINTN RangeCount
- )
- {
- UINTN Index;
- for (Index = 0; Index < RangeCount; Index++) {
- UT_LOG_INFO ("\t{ 0x%016llx, 0x%016llx, %a },\n", Ranges[Index].BaseAddress, Ranges[Index].Length, mCacheDescription[Ranges[Index].Type]);
- }
- }
- /**
- **/
- /**
- Generate random count of MTRRs for each cache type.
- @param TotalCount Total MTRR count.
- @param UcCount Return count of Uncacheable type.
- @param WtCount Return count of Write Through type.
- @param WbCount Return count of Write Back type.
- @param WpCount Return count of Write Protected type.
- @param WcCount Return count of Write Combining type.
- **/
- VOID
- GenerateRandomMemoryTypeCombination (
- IN UINT32 TotalCount,
- OUT UINT32 *UcCount,
- OUT UINT32 *WtCount,
- OUT UINT32 *WbCount,
- OUT UINT32 *WpCount,
- OUT UINT32 *WcCount
- )
- {
- UINTN Index;
- UINT32 TotalMtrrCount;
- UINT32 *CountPerType[5];
- CountPerType[0] = UcCount;
- CountPerType[1] = WtCount;
- CountPerType[2] = WbCount;
- CountPerType[3] = WpCount;
- CountPerType[4] = WcCount;
- //
- // Initialize the count of each cache type to 0.
- //
- for (Index = 0; Index < ARRAY_SIZE (CountPerType); Index++) {
- *(CountPerType[Index]) = 0;
- }
- //
- // Pick a random count of MTRRs
- //
- TotalMtrrCount = Random32 (1, TotalCount);
- for (Index = 0; Index < TotalMtrrCount; Index++) {
- //
- // For each of them, pick a random cache type.
- //
- (*(CountPerType[Random32 (0, ARRAY_SIZE (CountPerType) - 1)]))++;
- }
- }
- /**
- Unit test of MtrrLib service MtrrSetMemoryAttribute()
- @param[in] Context Ignored
- @retval UNIT_TEST_PASSED The Unit test has completed and the test
- case was successful.
- @retval UNIT_TEST_ERROR_TEST_FAILED A test case assertion has failed.
- **/
- UNIT_TEST_STATUS
- EFIAPI
- UnitTestMtrrSetMemoryAttributesInMtrrSettings (
- IN UNIT_TEST_CONTEXT Context
- )
- {
- CONST MTRR_LIB_SYSTEM_PARAMETER *SystemParameter;
- RETURN_STATUS Status;
- UINT32 UcCount;
- UINT32 WtCount;
- UINT32 WbCount;
- UINT32 WpCount;
- UINT32 WcCount;
- UINT32 MtrrIndex;
- UINT8 *Scratch;
- UINTN ScratchSize;
- MTRR_SETTINGS LocalMtrrs;
- MTRR_MEMORY_RANGE RawMtrrRange[MTRR_NUMBER_OF_VARIABLE_MTRR];
- MTRR_MEMORY_RANGE ExpectedMemoryRanges[MTRR_NUMBER_OF_FIXED_MTRR * sizeof (UINT64) + 2 * MTRR_NUMBER_OF_VARIABLE_MTRR + 1];
- UINT32 ExpectedVariableMtrrUsage;
- UINTN ExpectedMemoryRangesCount;
- MTRR_MEMORY_RANGE ActualMemoryRanges[MTRR_NUMBER_OF_FIXED_MTRR * sizeof (UINT64) + 2 * MTRR_NUMBER_OF_VARIABLE_MTRR + 1];
- UINT32 ActualVariableMtrrUsage;
- UINTN ActualMemoryRangesCount;
- MTRR_SETTINGS *Mtrrs[2];
- SystemParameter = (MTRR_LIB_SYSTEM_PARAMETER *)Context;
- GenerateRandomMemoryTypeCombination (
- SystemParameter->VariableMtrrCount - PatchPcdGet32 (PcdCpuNumberOfReservedVariableMtrrs),
- &UcCount,
- &WtCount,
- &WbCount,
- &WpCount,
- &WcCount
- );
- GenerateValidAndConfigurableMtrrPairs (
- SystemParameter->PhysicalAddressBits,
- RawMtrrRange,
- UcCount,
- WtCount,
- WbCount,
- WpCount,
- WcCount
- );
- ExpectedVariableMtrrUsage = UcCount + WtCount + WbCount + WpCount + WcCount;
- ExpectedMemoryRangesCount = ARRAY_SIZE (ExpectedMemoryRanges);
- GetEffectiveMemoryRanges (
- SystemParameter->DefaultCacheType,
- SystemParameter->PhysicalAddressBits,
- RawMtrrRange,
- ExpectedVariableMtrrUsage,
- ExpectedMemoryRanges,
- &ExpectedMemoryRangesCount
- );
- UT_LOG_INFO (
- "Total MTRR [%d]: UC=%d, WT=%d, WB=%d, WP=%d, WC=%d\n",
- ExpectedVariableMtrrUsage,
- UcCount,
- WtCount,
- WbCount,
- WpCount,
- WcCount
- );
- UT_LOG_INFO ("--- Expected Memory Ranges [%d] ---\n", ExpectedMemoryRangesCount);
- DumpMemoryRanges (ExpectedMemoryRanges, ExpectedMemoryRangesCount);
- //
- // Default cache type is always an INPUT
- //
- ZeroMem (&LocalMtrrs, sizeof (LocalMtrrs));
- LocalMtrrs.MtrrDefType = MtrrGetDefaultMemoryType ();
- ScratchSize = SCRATCH_BUFFER_SIZE;
- Mtrrs[0] = &LocalMtrrs;
- Mtrrs[1] = NULL;
- for (MtrrIndex = 0; MtrrIndex < ARRAY_SIZE (Mtrrs); MtrrIndex++) {
- Scratch = calloc (ScratchSize, sizeof (UINT8));
- Status = MtrrSetMemoryAttributesInMtrrSettings (Mtrrs[MtrrIndex], Scratch, &ScratchSize, ExpectedMemoryRanges, ExpectedMemoryRangesCount);
- if (Status == RETURN_BUFFER_TOO_SMALL) {
- Scratch = realloc (Scratch, ScratchSize);
- Status = MtrrSetMemoryAttributesInMtrrSettings (Mtrrs[MtrrIndex], Scratch, &ScratchSize, ExpectedMemoryRanges, ExpectedMemoryRangesCount);
- }
- UT_ASSERT_STATUS_EQUAL (Status, RETURN_SUCCESS);
- if (Mtrrs[MtrrIndex] == NULL) {
- ZeroMem (&LocalMtrrs, sizeof (LocalMtrrs));
- MtrrGetAllMtrrs (&LocalMtrrs);
- }
- ActualMemoryRangesCount = ARRAY_SIZE (ActualMemoryRanges);
- CollectTestResult (
- SystemParameter->DefaultCacheType,
- SystemParameter->PhysicalAddressBits,
- SystemParameter->VariableMtrrCount,
- &LocalMtrrs,
- ActualMemoryRanges,
- &ActualMemoryRangesCount,
- &ActualVariableMtrrUsage
- );
- UT_LOG_INFO ("--- Actual Memory Ranges [%d] ---\n", ActualMemoryRangesCount);
- DumpMemoryRanges (ActualMemoryRanges, ActualMemoryRangesCount);
- VerifyMemoryRanges (ExpectedMemoryRanges, ExpectedMemoryRangesCount, ActualMemoryRanges, ActualMemoryRangesCount);
- UT_ASSERT_TRUE (ExpectedVariableMtrrUsage >= ActualVariableMtrrUsage);
- ZeroMem (&LocalMtrrs, sizeof (LocalMtrrs));
- }
- free (Scratch);
- return UNIT_TEST_PASSED;
- }
- /**
- Test routine to check whether invalid base/size can be rejected.
- @param Context Pointer to MTRR_LIB_SYSTEM_PARAMETER.
- @return Test status.
- **/
- UNIT_TEST_STATUS
- EFIAPI
- UnitTestInvalidMemoryLayouts (
- IN UNIT_TEST_CONTEXT Context
- )
- {
- CONST MTRR_LIB_SYSTEM_PARAMETER *SystemParameter;
- MTRR_MEMORY_RANGE Ranges[MTRR_NUMBER_OF_VARIABLE_MTRR * 2 + 1];
- UINTN RangeCount;
- UINT64 MaxAddress;
- UINT32 Index;
- UINT64 BaseAddress;
- UINT64 Length;
- RETURN_STATUS Status;
- UINTN ScratchSize;
- SystemParameter = (MTRR_LIB_SYSTEM_PARAMETER *)Context;
- RangeCount = Random32 (1, ARRAY_SIZE (Ranges));
- MaxAddress = 1ull << SystemParameter->PhysicalAddressBits;
- for (Index = 0; Index < RangeCount; Index++) {
- do {
- BaseAddress = Random64 (0, MaxAddress);
- Length = Random64 (1, MaxAddress - BaseAddress);
- } while (((BaseAddress & 0xFFF) == 0) || ((Length & 0xFFF) == 0));
- Ranges[Index].BaseAddress = BaseAddress;
- Ranges[Index].Length = Length;
- Ranges[Index].Type = GenerateRandomCacheType ();
- Status = MtrrSetMemoryAttribute (
- Ranges[Index].BaseAddress,
- Ranges[Index].Length,
- Ranges[Index].Type
- );
- UT_ASSERT_TRUE (RETURN_ERROR (Status));
- }
- ScratchSize = 0;
- Status = MtrrSetMemoryAttributesInMtrrSettings (NULL, NULL, &ScratchSize, Ranges, RangeCount);
- UT_ASSERT_TRUE (RETURN_ERROR (Status));
- return UNIT_TEST_PASSED;
- }
- /**
- Unit test of MtrrLib service IsMtrrSupported()
- @param[in] Context Ignored
- @retval UNIT_TEST_PASSED The Unit test has completed and the test
- case was successful.
- @retval UNIT_TEST_ERROR_TEST_FAILED A test case assertion has failed.
- **/
- UNIT_TEST_STATUS
- EFIAPI
- UnitTestIsMtrrSupported (
- IN UNIT_TEST_CONTEXT Context
- )
- {
- MTRR_LIB_SYSTEM_PARAMETER SystemParameter;
- MTRR_LIB_TEST_CONTEXT *LocalContext;
- LocalContext = (MTRR_LIB_TEST_CONTEXT *)Context;
- CopyMem (&SystemParameter, LocalContext->SystemParameter, sizeof (SystemParameter));
- //
- // MTRR capability off in CPUID leaf.
- //
- SystemParameter.MtrrSupported = FALSE;
- InitializeMtrrRegs (&SystemParameter);
- UT_ASSERT_FALSE (IsMtrrSupported ());
- //
- // MTRR capability on in CPUID leaf, but no variable or fixed MTRRs.
- //
- SystemParameter.MtrrSupported = TRUE;
- SystemParameter.VariableMtrrCount = 0;
- SystemParameter.FixedMtrrSupported = FALSE;
- InitializeMtrrRegs (&SystemParameter);
- UT_ASSERT_FALSE (IsMtrrSupported ());
- //
- // MTRR capability on in CPUID leaf, but no variable MTRRs.
- //
- SystemParameter.MtrrSupported = TRUE;
- SystemParameter.VariableMtrrCount = 0;
- SystemParameter.FixedMtrrSupported = TRUE;
- InitializeMtrrRegs (&SystemParameter);
- UT_ASSERT_FALSE (IsMtrrSupported ());
- //
- // MTRR capability on in CPUID leaf, but no fixed MTRRs.
- //
- SystemParameter.MtrrSupported = TRUE;
- SystemParameter.VariableMtrrCount = 7;
- SystemParameter.FixedMtrrSupported = FALSE;
- InitializeMtrrRegs (&SystemParameter);
- UT_ASSERT_FALSE (IsMtrrSupported ());
- //
- // MTRR capability on in CPUID leaf with both variable and fixed MTRRs.
- //
- SystemParameter.MtrrSupported = TRUE;
- SystemParameter.VariableMtrrCount = 7;
- SystemParameter.FixedMtrrSupported = TRUE;
- InitializeMtrrRegs (&SystemParameter);
- UT_ASSERT_TRUE (IsMtrrSupported ());
- return UNIT_TEST_PASSED;
- }
- /**
- Unit test of MtrrLib service GetVariableMtrrCount()
- @param[in] Context Ignored
- @retval UNIT_TEST_PASSED The Unit test has completed and the test
- case was successful.
- @retval UNIT_TEST_ERROR_TEST_FAILED A test case assertion has failed.
- **/
- UNIT_TEST_STATUS
- EFIAPI
- UnitTestGetVariableMtrrCount (
- IN UNIT_TEST_CONTEXT Context
- )
- {
- UINT32 Result;
- MTRR_LIB_SYSTEM_PARAMETER SystemParameter;
- MTRR_LIB_TEST_CONTEXT *LocalContext;
- LocalContext = (MTRR_LIB_TEST_CONTEXT *)Context;
- CopyMem (&SystemParameter, LocalContext->SystemParameter, sizeof (SystemParameter));
- //
- // If MTRR capability off in CPUID leaf, then the count is always 0.
- //
- SystemParameter.MtrrSupported = FALSE;
- for (SystemParameter.VariableMtrrCount = 1; SystemParameter.VariableMtrrCount <= MTRR_NUMBER_OF_VARIABLE_MTRR; SystemParameter.VariableMtrrCount++) {
- InitializeMtrrRegs (&SystemParameter);
- Result = GetVariableMtrrCount ();
- UT_ASSERT_EQUAL (Result, 0);
- }
- //
- // Try all supported variable MTRR counts.
- // If variable MTRR count is > MTRR_NUMBER_OF_VARIABLE_MTRR, then an ASSERT()
- // is generated.
- //
- SystemParameter.MtrrSupported = TRUE;
- for (SystemParameter.VariableMtrrCount = 1; SystemParameter.VariableMtrrCount <= MTRR_NUMBER_OF_VARIABLE_MTRR; SystemParameter.VariableMtrrCount++) {
- InitializeMtrrRegs (&SystemParameter);
- Result = GetVariableMtrrCount ();
- UT_ASSERT_EQUAL (Result, SystemParameter.VariableMtrrCount);
- }
- //
- // Expect ASSERT() if variable MTRR count is > MTRR_NUMBER_OF_VARIABLE_MTRR
- //
- SystemParameter.VariableMtrrCount = MTRR_NUMBER_OF_VARIABLE_MTRR + 1;
- InitializeMtrrRegs (&SystemParameter);
- UT_EXPECT_ASSERT_FAILURE (GetVariableMtrrCount (), NULL);
- SystemParameter.MtrrSupported = TRUE;
- SystemParameter.VariableMtrrCount = MAX_UINT8;
- InitializeMtrrRegs (&SystemParameter);
- UT_EXPECT_ASSERT_FAILURE (GetVariableMtrrCount (), NULL);
- return UNIT_TEST_PASSED;
- }
- /**
- Unit test of MtrrLib service GetFirmwareVariableMtrrCount()
- @param[in] Context Ignored
- @retval UNIT_TEST_PASSED The Unit test has completed and the test
- case was successful.
- @retval UNIT_TEST_ERROR_TEST_FAILED A test case assertion has failed.
- **/
- UNIT_TEST_STATUS
- EFIAPI
- UnitTestGetFirmwareVariableMtrrCount (
- IN UNIT_TEST_CONTEXT Context
- )
- {
- UINT32 Result;
- UINT32 ReservedMtrrs;
- MTRR_LIB_SYSTEM_PARAMETER SystemParameter;
- MTRR_LIB_GET_FIRMWARE_VARIABLE_MTRR_COUNT_CONTEXT *LocalContext;
- LocalContext = (MTRR_LIB_GET_FIRMWARE_VARIABLE_MTRR_COUNT_CONTEXT *)Context;
- CopyMem (&SystemParameter, LocalContext->SystemParameter, sizeof (SystemParameter));
- InitializeMtrrRegs (&SystemParameter);
- //
- // Positive test cases for VCNT = 10 and Reserved PCD in range 0..10
- //
- for (ReservedMtrrs = 0; ReservedMtrrs <= SystemParameter.VariableMtrrCount; ReservedMtrrs++) {
- PatchPcdSet32 (PcdCpuNumberOfReservedVariableMtrrs, ReservedMtrrs);
- Result = GetFirmwareVariableMtrrCount ();
- UT_ASSERT_EQUAL (Result, SystemParameter.VariableMtrrCount - ReservedMtrrs);
- }
- //
- // Negative test cases when Reserved PCD is larger than VCNT
- //
- for (ReservedMtrrs = SystemParameter.VariableMtrrCount + 1; ReservedMtrrs <= 255; ReservedMtrrs++) {
- PatchPcdSet32 (PcdCpuNumberOfReservedVariableMtrrs, ReservedMtrrs);
- Result = GetFirmwareVariableMtrrCount ();
- UT_ASSERT_EQUAL (Result, 0);
- }
- //
- // Negative test cases when Reserved PCD is larger than VCNT
- //
- PatchPcdSet32 (PcdCpuNumberOfReservedVariableMtrrs, MAX_UINT32);
- Result = GetFirmwareVariableMtrrCount ();
- UT_ASSERT_EQUAL (Result, 0);
- //
- // Negative test case when MTRRs are not supported
- //
- SystemParameter.MtrrSupported = FALSE;
- InitializeMtrrRegs (&SystemParameter);
- PatchPcdSet32 (PcdCpuNumberOfReservedVariableMtrrs, 2);
- Result = GetFirmwareVariableMtrrCount ();
- UT_ASSERT_EQUAL (Result, 0);
- //
- // Negative test case when Fixed MTRRs are not supported
- //
- SystemParameter.MtrrSupported = TRUE;
- SystemParameter.FixedMtrrSupported = FALSE;
- InitializeMtrrRegs (&SystemParameter);
- PatchPcdSet32 (PcdCpuNumberOfReservedVariableMtrrs, 2);
- Result = GetFirmwareVariableMtrrCount ();
- UT_ASSERT_EQUAL (Result, 0);
- //
- // Expect ASSERT() if variable MTRR count is > MTRR_NUMBER_OF_VARIABLE_MTRR
- //
- SystemParameter.FixedMtrrSupported = TRUE;
- SystemParameter.VariableMtrrCount = MTRR_NUMBER_OF_VARIABLE_MTRR + 1;
- InitializeMtrrRegs (&SystemParameter);
- UT_EXPECT_ASSERT_FAILURE (GetFirmwareVariableMtrrCount (), NULL);
- return UNIT_TEST_PASSED;
- }
- /**
- Unit test of MtrrLib service MtrrGetMemoryAttribute()
- @param[in] Context Ignored
- @retval UNIT_TEST_PASSED The Unit test has completed and the test
- case was successful.
- @retval UNIT_TEST_ERROR_TEST_FAILED A test case assertion has failed.
- **/
- UNIT_TEST_STATUS
- EFIAPI
- UnitTestMtrrGetMemoryAttribute (
- IN UNIT_TEST_CONTEXT Context
- )
- {
- return UNIT_TEST_PASSED;
- }
- /**
- Unit test of MtrrLib service MtrrGetFixedMtrr()
- @param[in] Context Ignored
- @retval UNIT_TEST_PASSED The Unit test has completed and the test
- case was successful.
- @retval UNIT_TEST_ERROR_TEST_FAILED A test case assertion has failed.
- **/
- UNIT_TEST_STATUS
- EFIAPI
- UnitTestMtrrGetFixedMtrr (
- IN UNIT_TEST_CONTEXT Context
- )
- {
- MTRR_FIXED_SETTINGS *Result;
- MTRR_FIXED_SETTINGS ExpectedFixedSettings;
- MTRR_FIXED_SETTINGS FixedSettings;
- UINTN Index;
- UINTN MsrIndex;
- UINTN ByteIndex;
- UINT64 MsrValue;
- MTRR_LIB_SYSTEM_PARAMETER SystemParameter;
- MTRR_LIB_TEST_CONTEXT *LocalContext;
- LocalContext = (MTRR_LIB_TEST_CONTEXT *)Context;
- CopyMem (&SystemParameter, LocalContext->SystemParameter, sizeof (SystemParameter));
- InitializeMtrrRegs (&SystemParameter);
- //
- // Set random cache type to different ranges under 1MB and make sure
- // the fixed MTRR settings are expected.
- // Try 100 times.
- //
- for (Index = 0; Index < 100; Index++) {
- for (MsrIndex = 0; MsrIndex < ARRAY_SIZE (mFixedMtrrsIndex); MsrIndex++) {
- MsrValue = 0;
- for (ByteIndex = 0; ByteIndex < sizeof (UINT64); ByteIndex++) {
- MsrValue = MsrValue | LShiftU64 (GenerateRandomCacheType (), ByteIndex * 8);
- }
- ExpectedFixedSettings.Mtrr[MsrIndex] = MsrValue;
- AsmWriteMsr64 (mFixedMtrrsIndex[MsrIndex], MsrValue);
- }
- Result = MtrrGetFixedMtrr (&FixedSettings);
- UT_ASSERT_EQUAL ((UINTN)Result, (UINTN)&FixedSettings);
- UT_ASSERT_MEM_EQUAL (&FixedSettings, &ExpectedFixedSettings, sizeof (FixedSettings));
- }
- //
- // Negative test case when MTRRs are not supported
- //
- SystemParameter.MtrrSupported = FALSE;
- InitializeMtrrRegs (&SystemParameter);
- ZeroMem (&FixedSettings, sizeof (FixedSettings));
- ZeroMem (&ExpectedFixedSettings, sizeof (ExpectedFixedSettings));
- Result = MtrrGetFixedMtrr (&FixedSettings);
- UT_ASSERT_EQUAL ((UINTN)Result, (UINTN)&FixedSettings);
- UT_ASSERT_MEM_EQUAL (&ExpectedFixedSettings, &FixedSettings, sizeof (ExpectedFixedSettings));
- return UNIT_TEST_PASSED;
- }
- /**
- Unit test of MtrrLib service MtrrGetAllMtrrs()
- @param[in] Context Ignored
- @retval UNIT_TEST_PASSED The Unit test has completed and the test
- case was successful.
- @retval UNIT_TEST_ERROR_TEST_FAILED A test case assertion has failed.
- **/
- UNIT_TEST_STATUS
- EFIAPI
- UnitTestMtrrGetAllMtrrs (
- IN UNIT_TEST_CONTEXT Context
- )
- {
- MTRR_SETTINGS *Result;
- MTRR_SETTINGS Mtrrs;
- MTRR_SETTINGS ExpectedMtrrs;
- MTRR_VARIABLE_SETTING VariableMtrr[MTRR_NUMBER_OF_VARIABLE_MTRR];
- UINT32 Index;
- MTRR_LIB_SYSTEM_PARAMETER SystemParameter;
- MTRR_LIB_TEST_CONTEXT *LocalContext;
- LocalContext = (MTRR_LIB_TEST_CONTEXT *)Context;
- CopyMem (&SystemParameter, LocalContext->SystemParameter, sizeof (SystemParameter));
- InitializeMtrrRegs (&SystemParameter);
- for (Index = 0; Index < SystemParameter.VariableMtrrCount; Index++) {
- GenerateRandomMtrrPair (SystemParameter.PhysicalAddressBits, GenerateRandomCacheType (), &VariableMtrr[Index], NULL);
- AsmWriteMsr64 (MSR_IA32_MTRR_PHYSBASE0 + (Index << 1), VariableMtrr[Index].Base);
- AsmWriteMsr64 (MSR_IA32_MTRR_PHYSMASK0 + (Index << 1), VariableMtrr[Index].Mask);
- }
- Result = MtrrGetAllMtrrs (&Mtrrs);
- UT_ASSERT_EQUAL ((UINTN)Result, (UINTN)&Mtrrs);
- UT_ASSERT_MEM_EQUAL (Mtrrs.Variables.Mtrr, VariableMtrr, sizeof (MTRR_VARIABLE_SETTING) * SystemParameter.VariableMtrrCount);
- //
- // Negative test case when MTRRs are not supported
- //
- ZeroMem (&ExpectedMtrrs, sizeof (ExpectedMtrrs));
- ZeroMem (&Mtrrs, sizeof (Mtrrs));
- SystemParameter.MtrrSupported = FALSE;
- InitializeMtrrRegs (&SystemParameter);
- Result = MtrrGetAllMtrrs (&Mtrrs);
- UT_ASSERT_EQUAL ((UINTN)Result, (UINTN)&Mtrrs);
- UT_ASSERT_MEM_EQUAL (&ExpectedMtrrs, &Mtrrs, sizeof (ExpectedMtrrs));
- //
- // Expect ASSERT() if variable MTRR count is > MTRR_NUMBER_OF_VARIABLE_MTRR
- //
- SystemParameter.MtrrSupported = TRUE;
- SystemParameter.VariableMtrrCount = MTRR_NUMBER_OF_VARIABLE_MTRR + 1;
- InitializeMtrrRegs (&SystemParameter);
- UT_EXPECT_ASSERT_FAILURE (MtrrGetAllMtrrs (&Mtrrs), NULL);
- return UNIT_TEST_PASSED;
- }
- /**
- Unit test of MtrrLib service MtrrSetAllMtrrs()
- @param[in] Context Ignored
- @retval UNIT_TEST_PASSED The Unit test has completed and the test
- case was successful.
- @retval UNIT_TEST_ERROR_TEST_FAILED A test case assertion has failed.
- **/
- UNIT_TEST_STATUS
- EFIAPI
- UnitTestMtrrSetAllMtrrs (
- IN UNIT_TEST_CONTEXT Context
- )
- {
- MTRR_SETTINGS *Result;
- MTRR_SETTINGS Mtrrs;
- UINT32 Index;
- MSR_IA32_MTRR_DEF_TYPE_REGISTER Default;
- MTRR_LIB_SYSTEM_PARAMETER SystemParameter;
- MTRR_LIB_TEST_CONTEXT *LocalContext;
- LocalContext = (MTRR_LIB_TEST_CONTEXT *)Context;
- CopyMem (&SystemParameter, LocalContext->SystemParameter, sizeof (SystemParameter));
- InitializeMtrrRegs (&SystemParameter);
- Default.Uint64 = 0;
- Default.Bits.E = 1;
- Default.Bits.FE = 1;
- Default.Bits.Type = GenerateRandomCacheType ();
- ZeroMem (&Mtrrs, sizeof (Mtrrs));
- Mtrrs.MtrrDefType = Default.Uint64;
- for (Index = 0; Index < SystemParameter.VariableMtrrCount; Index++) {
- GenerateRandomMtrrPair (SystemParameter.PhysicalAddressBits, GenerateRandomCacheType (), &Mtrrs.Variables.Mtrr[Index], NULL);
- }
- Result = MtrrSetAllMtrrs (&Mtrrs);
- UT_ASSERT_EQUAL ((UINTN)Result, (UINTN)&Mtrrs);
- UT_ASSERT_EQUAL (AsmReadMsr64 (MSR_IA32_MTRR_DEF_TYPE), Mtrrs.MtrrDefType);
- for (Index = 0; Index < SystemParameter.VariableMtrrCount; Index++) {
- UT_ASSERT_EQUAL (AsmReadMsr64 (MSR_IA32_MTRR_PHYSBASE0 + (Index << 1)), Mtrrs.Variables.Mtrr[Index].Base);
- UT_ASSERT_EQUAL (AsmReadMsr64 (MSR_IA32_MTRR_PHYSMASK0 + (Index << 1)), Mtrrs.Variables.Mtrr[Index].Mask);
- }
- return UNIT_TEST_PASSED;
- }
- /**
- Unit test of MtrrLib service MtrrGetMemoryAttributeInVariableMtrr()
- @param[in] Context Ignored
- @retval UNIT_TEST_PASSED The Unit test has completed and the test
- case was successful.
- @retval UNIT_TEST_ERROR_TEST_FAILED A test case assertion has failed.
- **/
- UNIT_TEST_STATUS
- EFIAPI
- UnitTestMtrrGetMemoryAttributeInVariableMtrr (
- IN UNIT_TEST_CONTEXT Context
- )
- {
- MTRR_LIB_TEST_CONTEXT *LocalContext;
- MTRR_LIB_SYSTEM_PARAMETER SystemParameter;
- UINT32 Result;
- MTRR_VARIABLE_SETTING VariableSetting[MTRR_NUMBER_OF_VARIABLE_MTRR];
- VARIABLE_MTRR VariableMtrr[MTRR_NUMBER_OF_VARIABLE_MTRR];
- UINT64 ValidMtrrBitsMask;
- UINT64 ValidMtrrAddressMask;
- UINT32 Index;
- MSR_IA32_MTRR_PHYSBASE_REGISTER Base;
- MSR_IA32_MTRR_PHYSMASK_REGISTER Mask;
- LocalContext = (MTRR_LIB_TEST_CONTEXT *)Context;
- CopyMem (&SystemParameter, LocalContext->SystemParameter, sizeof (SystemParameter));
- InitializeMtrrRegs (&SystemParameter);
- ValidMtrrBitsMask = (1ull << SystemParameter.PhysicalAddressBits) - 1;
- ValidMtrrAddressMask = ValidMtrrBitsMask & 0xfffffffffffff000ULL;
- for (Index = 0; Index < SystemParameter.VariableMtrrCount; Index++) {
- GenerateRandomMtrrPair (SystemParameter.PhysicalAddressBits, GenerateRandomCacheType (), &VariableSetting[Index], NULL);
- AsmWriteMsr64 (MSR_IA32_MTRR_PHYSBASE0 + (Index << 1), VariableSetting[Index].Base);
- AsmWriteMsr64 (MSR_IA32_MTRR_PHYSMASK0 + (Index << 1), VariableSetting[Index].Mask);
- }
- Result = MtrrGetMemoryAttributeInVariableMtrr (ValidMtrrBitsMask, ValidMtrrAddressMask, VariableMtrr);
- UT_ASSERT_EQUAL (Result, SystemParameter.VariableMtrrCount);
- for (Index = 0; Index < SystemParameter.VariableMtrrCount; Index++) {
- Base.Uint64 = VariableMtrr[Index].BaseAddress;
- Base.Bits.Type = (UINT32)VariableMtrr[Index].Type;
- UT_ASSERT_EQUAL (Base.Uint64, VariableSetting[Index].Base);
- Mask.Uint64 = ~(VariableMtrr[Index].Length - 1) & ValidMtrrBitsMask;
- Mask.Bits.V = 1;
- UT_ASSERT_EQUAL (Mask.Uint64, VariableSetting[Index].Mask);
- }
- //
- // Negative test case when MTRRs are not supported
- //
- SystemParameter.MtrrSupported = FALSE;
- InitializeMtrrRegs (&SystemParameter);
- Result = MtrrGetMemoryAttributeInVariableMtrr (ValidMtrrBitsMask, ValidMtrrAddressMask, VariableMtrr);
- UT_ASSERT_EQUAL (Result, 0);
- //
- // Expect ASSERT() if variable MTRR count is > MTRR_NUMBER_OF_VARIABLE_MTRR
- //
- SystemParameter.MtrrSupported = TRUE;
- SystemParameter.VariableMtrrCount = MTRR_NUMBER_OF_VARIABLE_MTRR + 1;
- InitializeMtrrRegs (&SystemParameter);
- UT_EXPECT_ASSERT_FAILURE (MtrrGetMemoryAttributeInVariableMtrr (ValidMtrrBitsMask, ValidMtrrAddressMask, VariableMtrr), NULL);
- return UNIT_TEST_PASSED;
- }
- /**
- Unit test of MtrrLib service MtrrDebugPrintAllMtrrs()
- @param[in] Context Ignored
- @retval UNIT_TEST_PASSED The Unit test has completed and the test
- case was successful.
- @retval UNIT_TEST_ERROR_TEST_FAILED A test case assertion has failed.
- **/
- UNIT_TEST_STATUS
- EFIAPI
- UnitTestMtrrDebugPrintAllMtrrs (
- IN UNIT_TEST_CONTEXT Context
- )
- {
- return UNIT_TEST_PASSED;
- }
- /**
- Unit test of MtrrLib service MtrrGetDefaultMemoryType().
- @param[in] Context Ignored
- @retval UNIT_TEST_PASSED The Unit test has completed and the test
- case was successful.
- @retval UNIT_TEST_ERROR_TEST_FAILED A test case assertion has failed.
- **/
- UNIT_TEST_STATUS
- EFIAPI
- UnitTestMtrrGetDefaultMemoryType (
- IN UNIT_TEST_CONTEXT Context
- )
- {
- MTRR_LIB_TEST_CONTEXT *LocalContext;
- UINTN Index;
- MTRR_MEMORY_CACHE_TYPE Result;
- MTRR_LIB_SYSTEM_PARAMETER SystemParameter;
- MTRR_MEMORY_CACHE_TYPE CacheType[5];
- CacheType[0] = CacheUncacheable;
- CacheType[1] = CacheWriteCombining;
- CacheType[2] = CacheWriteThrough;
- CacheType[3] = CacheWriteProtected;
- CacheType[4] = CacheWriteBack;
- LocalContext = (MTRR_LIB_TEST_CONTEXT *)Context;
- CopyMem (&SystemParameter, LocalContext->SystemParameter, sizeof (SystemParameter));
- //
- // If MTRRs are supported, then always return the cache type in the MSR
- // MSR_IA32_MTRR_DEF_TYPE
- //
- for (Index = 0; Index < ARRAY_SIZE (CacheType); Index++) {
- SystemParameter.DefaultCacheType = CacheType[Index];
- InitializeMtrrRegs (&SystemParameter);
- Result = MtrrGetDefaultMemoryType ();
- UT_ASSERT_EQUAL (Result, SystemParameter.DefaultCacheType);
- }
- //
- // If MTRRs are not supported, then always return CacheUncacheable
- //
- SystemParameter.MtrrSupported = FALSE;
- InitializeMtrrRegs (&SystemParameter);
- Result = MtrrGetDefaultMemoryType ();
- UT_ASSERT_EQUAL (Result, CacheUncacheable);
- SystemParameter.MtrrSupported = TRUE;
- SystemParameter.FixedMtrrSupported = FALSE;
- InitializeMtrrRegs (&SystemParameter);
- Result = MtrrGetDefaultMemoryType ();
- UT_ASSERT_EQUAL (Result, CacheUncacheable);
- SystemParameter.MtrrSupported = TRUE;
- SystemParameter.FixedMtrrSupported = TRUE;
- SystemParameter.VariableMtrrCount = 0;
- InitializeMtrrRegs (&SystemParameter);
- Result = MtrrGetDefaultMemoryType ();
- UT_ASSERT_EQUAL (Result, CacheUncacheable);
- return UNIT_TEST_PASSED;
- }
- /**
- Unit test of MtrrLib service MtrrSetMemoryAttributeInMtrrSettings().
- @param[in] Context Ignored
- @retval UNIT_TEST_PASSED The Unit test has completed and the test
- case was successful.
- @retval UNIT_TEST_ERROR_TEST_FAILED A test case assertion has failed.
- **/
- UNIT_TEST_STATUS
- EFIAPI
- UnitTestMtrrSetMemoryAttributeInMtrrSettings (
- IN UNIT_TEST_CONTEXT Context
- )
- {
- CONST MTRR_LIB_SYSTEM_PARAMETER *SystemParameter;
- RETURN_STATUS Status;
- UINT32 UcCount;
- UINT32 WtCount;
- UINT32 WbCount;
- UINT32 WpCount;
- UINT32 WcCount;
- UINTN MtrrIndex;
- UINTN Index;
- MTRR_SETTINGS LocalMtrrs;
- MTRR_MEMORY_RANGE RawMtrrRange[MTRR_NUMBER_OF_VARIABLE_MTRR];
- MTRR_MEMORY_RANGE ExpectedMemoryRanges[MTRR_NUMBER_OF_FIXED_MTRR * sizeof (UINT64) + 2 * MTRR_NUMBER_OF_VARIABLE_MTRR + 1];
- UINT32 ExpectedVariableMtrrUsage;
- UINTN ExpectedMemoryRangesCount;
- MTRR_MEMORY_RANGE ActualMemoryRanges[MTRR_NUMBER_OF_FIXED_MTRR * sizeof (UINT64) + 2 * MTRR_NUMBER_OF_VARIABLE_MTRR + 1];
- UINT32 ActualVariableMtrrUsage;
- UINTN ActualMemoryRangesCount;
- MTRR_SETTINGS *Mtrrs[2];
- SystemParameter = (MTRR_LIB_SYSTEM_PARAMETER *)Context;
- GenerateRandomMemoryTypeCombination (
- SystemParameter->VariableMtrrCount - PatchPcdGet32 (PcdCpuNumberOfReservedVariableMtrrs),
- &UcCount,
- &WtCount,
- &WbCount,
- &WpCount,
- &WcCount
- );
- GenerateValidAndConfigurableMtrrPairs (
- SystemParameter->PhysicalAddressBits,
- RawMtrrRange,
- UcCount,
- WtCount,
- WbCount,
- WpCount,
- WcCount
- );
- ExpectedVariableMtrrUsage = UcCount + WtCount + WbCount + WpCount + WcCount;
- ExpectedMemoryRangesCount = ARRAY_SIZE (ExpectedMemoryRanges);
- GetEffectiveMemoryRanges (
- SystemParameter->DefaultCacheType,
- SystemParameter->PhysicalAddressBits,
- RawMtrrRange,
- ExpectedVariableMtrrUsage,
- ExpectedMemoryRanges,
- &ExpectedMemoryRangesCount
- );
- UT_LOG_INFO ("--- Expected Memory Ranges [%d] ---\n", ExpectedMemoryRangesCount);
- DumpMemoryRanges (ExpectedMemoryRanges, ExpectedMemoryRangesCount);
- //
- // Default cache type is always an INPUT
- //
- ZeroMem (&LocalMtrrs, sizeof (LocalMtrrs));
- LocalMtrrs.MtrrDefType = MtrrGetDefaultMemoryType ();
- Mtrrs[0] = &LocalMtrrs;
- Mtrrs[1] = NULL;
- for (MtrrIndex = 0; MtrrIndex < ARRAY_SIZE (Mtrrs); MtrrIndex++) {
- for (Index = 0; Index < ExpectedMemoryRangesCount; Index++) {
- Status = MtrrSetMemoryAttributeInMtrrSettings (
- Mtrrs[MtrrIndex],
- ExpectedMemoryRanges[Index].BaseAddress,
- ExpectedMemoryRanges[Index].Length,
- ExpectedMemoryRanges[Index].Type
- );
- UT_ASSERT_TRUE (Status == RETURN_SUCCESS || Status == RETURN_OUT_OF_RESOURCES || Status == RETURN_BUFFER_TOO_SMALL);
- if ((Status == RETURN_OUT_OF_RESOURCES) || (Status == RETURN_BUFFER_TOO_SMALL)) {
- return UNIT_TEST_SKIPPED;
- }
- }
- if (Mtrrs[MtrrIndex] == NULL) {
- ZeroMem (&LocalMtrrs, sizeof (LocalMtrrs));
- MtrrGetAllMtrrs (&LocalMtrrs);
- }
- ActualMemoryRangesCount = ARRAY_SIZE (ActualMemoryRanges);
- CollectTestResult (
- SystemParameter->DefaultCacheType,
- SystemParameter->PhysicalAddressBits,
- SystemParameter->VariableMtrrCount,
- &LocalMtrrs,
- ActualMemoryRanges,
- &ActualMemoryRangesCount,
- &ActualVariableMtrrUsage
- );
- UT_LOG_INFO ("--- Actual Memory Ranges [%d] ---\n", ActualMemoryRangesCount);
- DumpMemoryRanges (ActualMemoryRanges, ActualMemoryRangesCount);
- VerifyMemoryRanges (ExpectedMemoryRanges, ExpectedMemoryRangesCount, ActualMemoryRanges, ActualMemoryRangesCount);
- UT_ASSERT_TRUE (ExpectedVariableMtrrUsage >= ActualVariableMtrrUsage);
- ZeroMem (&LocalMtrrs, sizeof (LocalMtrrs));
- }
- return UNIT_TEST_PASSED;
- }
- /**
- Prep routine for UnitTestGetFirmwareVariableMtrrCount().
- @param Context Point to a UINT32 data to save the PcdCpuNumberOfReservedVariableMtrrs.
- **/
- UNIT_TEST_STATUS
- EFIAPI
- SavePcdValue (
- UNIT_TEST_CONTEXT Context
- )
- {
- MTRR_LIB_GET_FIRMWARE_VARIABLE_MTRR_COUNT_CONTEXT *LocalContext;
- LocalContext = (MTRR_LIB_GET_FIRMWARE_VARIABLE_MTRR_COUNT_CONTEXT *)Context;
- LocalContext->NumberOfReservedVariableMtrrs = PatchPcdGet32 (PcdCpuNumberOfReservedVariableMtrrs);
- return UNIT_TEST_PASSED;
- }
- /**
- Clean up routine for UnitTestGetFirmwareVariableMtrrCount().
- @param Context Point to a UINT32 data to save the PcdCpuNumberOfReservedVariableMtrrs.
- **/
- VOID
- EFIAPI
- RestorePcdValue (
- UNIT_TEST_CONTEXT Context
- )
- {
- MTRR_LIB_GET_FIRMWARE_VARIABLE_MTRR_COUNT_CONTEXT *LocalContext;
- LocalContext = (MTRR_LIB_GET_FIRMWARE_VARIABLE_MTRR_COUNT_CONTEXT *)Context;
- PatchPcdSet32 (PcdCpuNumberOfReservedVariableMtrrs, LocalContext->NumberOfReservedVariableMtrrs);
- }
- /**
- Initialize the unit test framework, suite, and unit tests for the
- ResetSystemLib and run the ResetSystemLib unit test.
- @param Iteration Iteration of testing MtrrSetMemoryAttributeInMtrrSettings
- and MtrrSetMemoryAttributesInMtrrSettings using random inputs.
- @retval EFI_SUCCESS All test cases were dispatched.
- @retval EFI_OUT_OF_RESOURCES There are not enough resources available to
- initialize the unit tests.
- **/
- STATIC
- EFI_STATUS
- EFIAPI
- UnitTestingEntry (
- UINTN Iteration
- )
- {
- EFI_STATUS Status;
- UNIT_TEST_FRAMEWORK_HANDLE Framework;
- UNIT_TEST_SUITE_HANDLE MtrrApiTests;
- UINTN Index;
- UINTN SystemIndex;
- MTRR_LIB_TEST_CONTEXT Context;
- MTRR_LIB_GET_FIRMWARE_VARIABLE_MTRR_COUNT_CONTEXT GetFirmwareVariableMtrrCountContext;
- Context.SystemParameter = &mDefaultSystemParameter;
- GetFirmwareVariableMtrrCountContext.SystemParameter = &mDefaultSystemParameter;
- Framework = NULL;
- //
- // Setup the test framework for running the tests.
- //
- Status = InitUnitTestFramework (&Framework, UNIT_TEST_APP_NAME, gEfiCallerBaseName, UNIT_TEST_APP_VERSION);
- if (EFI_ERROR (Status)) {
- DEBUG ((DEBUG_ERROR, "Failed in InitUnitTestFramework. Status = %r\n", Status));
- goto EXIT;
- }
- //
- // --------------Suite-----------Description--------------Name----------Function--------Pre---Post-------------------Context-----------
- //
- //
- // Populate the MtrrLib API Unit Test Suite.
- //
- Status = CreateUnitTestSuite (&MtrrApiTests, Framework, "MtrrLib API Tests", "MtrrLib.MtrrLib", NULL, NULL);
- if (EFI_ERROR (Status)) {
- DEBUG ((DEBUG_ERROR, "Failed in CreateUnitTestSuite for MtrrLib API Tests\n"));
- Status = EFI_OUT_OF_RESOURCES;
- goto EXIT;
- }
- AddTestCase (MtrrApiTests, "Test IsMtrrSupported", "MtrrSupported", UnitTestIsMtrrSupported, NULL, NULL, &Context);
- AddTestCase (MtrrApiTests, "Test GetVariableMtrrCount", "GetVariableMtrrCount", UnitTestGetVariableMtrrCount, NULL, NULL, &Context);
- AddTestCase (MtrrApiTests, "Test GetFirmwareVariableMtrrCount", "GetFirmwareVariableMtrrCount", UnitTestGetFirmwareVariableMtrrCount, SavePcdValue, RestorePcdValue, &GetFirmwareVariableMtrrCountContext);
- AddTestCase (MtrrApiTests, "Test MtrrGetMemoryAttribute", "MtrrGetMemoryAttribute", UnitTestMtrrGetMemoryAttribute, NULL, NULL, &Context);
- AddTestCase (MtrrApiTests, "Test MtrrGetFixedMtrr", "MtrrGetFixedMtrr", UnitTestMtrrGetFixedMtrr, NULL, NULL, &Context);
- AddTestCase (MtrrApiTests, "Test MtrrGetAllMtrrs", "MtrrGetAllMtrrs", UnitTestMtrrGetAllMtrrs, NULL, NULL, &Context);
- AddTestCase (MtrrApiTests, "Test MtrrSetAllMtrrs", "MtrrSetAllMtrrs", UnitTestMtrrSetAllMtrrs, NULL, NULL, &Context);
- AddTestCase (MtrrApiTests, "Test MtrrGetMemoryAttributeInVariableMtrr", "MtrrGetMemoryAttributeInVariableMtrr", UnitTestMtrrGetMemoryAttributeInVariableMtrr, NULL, NULL, &Context);
- AddTestCase (MtrrApiTests, "Test MtrrDebugPrintAllMtrrs", "MtrrDebugPrintAllMtrrs", UnitTestMtrrDebugPrintAllMtrrs, NULL, NULL, &Context);
- AddTestCase (MtrrApiTests, "Test MtrrGetDefaultMemoryType", "MtrrGetDefaultMemoryType", UnitTestMtrrGetDefaultMemoryType, NULL, NULL, &Context);
- for (SystemIndex = 0; SystemIndex < ARRAY_SIZE (mSystemParameters); SystemIndex++) {
- for (Index = 0; Index < Iteration; Index++) {
- AddTestCase (MtrrApiTests, "Test InvalidMemoryLayouts", "InvalidMemoryLayouts", UnitTestInvalidMemoryLayouts, InitializeSystem, NULL, &mSystemParameters[SystemIndex]);
- AddTestCase (MtrrApiTests, "Test MtrrSetMemoryAttributeInMtrrSettings", "MtrrSetMemoryAttributeInMtrrSettings", UnitTestMtrrSetMemoryAttributeInMtrrSettings, InitializeSystem, NULL, &mSystemParameters[SystemIndex]);
- AddTestCase (MtrrApiTests, "Test MtrrSetMemoryAttributesInMtrrSettings", "MtrrSetMemoryAttributesInMtrrSettings", UnitTestMtrrSetMemoryAttributesInMtrrSettings, InitializeSystem, NULL, &mSystemParameters[SystemIndex]);
- }
- }
- //
- // Execute the tests.
- //
- Status = RunAllTestSuites (Framework);
- EXIT:
- if (Framework != NULL) {
- FreeUnitTestFramework (Framework);
- }
- return Status;
- }
- /**
- Standard POSIX C entry point for host based unit test execution.
- @param Argc Number of arguments.
- @param Argv Array of arguments.
- @return Test application exit code.
- **/
- INT32
- main (
- INT32 Argc,
- CHAR8 *Argv[]
- )
- {
- UINTN Count;
- DEBUG ((DEBUG_INFO, "%a v%a\n", UNIT_TEST_APP_NAME, UNIT_TEST_APP_VERSION));
- srand ((unsigned int)time (NULL));
- //
- // MtrrLibUnitTest generate-random-numbers <path to MtrrLib/UnitTest/RandomNumber.c> <random-number count>
- //
- if ((Argc == 4) && (AsciiStriCmp ("generate-random-numbers", Argv[1]) == 0)) {
- Count = atoi (Argv[3]);
- DEBUG ((DEBUG_INFO, "Generate %d random numbers to %a.\n", Count, Argv[2]));
- GenerateRandomNumbers (Argv[2], Count);
- return 0;
- }
- //
- // MtrrLibUnitTest [<iterations>]
- // <iterations> [fixed|random]
- // Default <iterations> is 10.
- // Default uses fixed inputs.
- //
- Count = 10;
- mRandomInput = FALSE;
- if ((Argc == 2) || (Argc == 3)) {
- Count = atoi (Argv[1]);
- if (Argc == 3) {
- if (AsciiStriCmp ("fixed", Argv[2]) == 0) {
- mRandomInput = FALSE;
- } else if (AsciiStriCmp ("random", Argv[2]) == 0) {
- mRandomInput = TRUE;
- }
- }
- }
- DEBUG ((DEBUG_INFO, "Iterations = %d\n", Count));
- DEBUG ((DEBUG_INFO, "Input = %a\n", mRandomInput ? "random" : "fixed"));
- return UnitTestingEntry (Count);
- }
|