From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mga07.intel.com (mga07.intel.com [134.134.136.100]) by mx.groups.io with SMTP id smtpd.web12.6754.1594971260853327084 for ; Fri, 17 Jul 2020 00:34:21 -0700 Authentication-Results: mx.groups.io; dkim=missing; spf=pass (domain: intel.com, ip: 134.134.136.100, mailfrom: ray.ni@intel.com) IronPort-SDR: wC15pYN2JaeDmOypUesEPoKz6n2mBLP2OpqufR4c7lXbIFNWHtcxFTTmROLwNTM+CecgjMVsNJ 7q2TpRJ18Kig== X-IronPort-AV: E=McAfee;i="6000,8403,9684"; a="214270332" X-IronPort-AV: E=Sophos;i="5.75,362,1589266800"; d="scan'208";a="214270332" X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False Received: from orsmga005.jf.intel.com ([10.7.209.41]) by orsmga105.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 17 Jul 2020 00:34:17 -0700 IronPort-SDR: lm/ng2d2KyTnQbYz1QQIdmpuuhdPTr7eVj8SsbDt7/dO0/3Eu96Lo1zctH+2rFnrm9NTWa45U0 OJoLiVLNMdGw== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.75,362,1589266800"; d="scan'208";a="460764967" Received: from ray-dev.ccr.corp.intel.com ([10.239.158.87]) by orsmga005.jf.intel.com with ESMTP; 17 Jul 2020 00:34:13 -0700 From: "Ni, Ray" To: devel@edk2.groups.io Cc: Michael D Kinney , Ming Shao , Eric Dong , Laszlo Ersek , Sean Brogan , Bret Barkelew , Jiewen Yao Subject: [PATCH] UefiCpuPkg/MtrrLib/UnitTest: Add host based unit test Date: Fri, 17 Jul 2020 15:33:18 +0800 Message-Id: <20200717073318.877-1-ray.ni@intel.com> X-Mailer: git-send-email 2.27.0.windows.1 MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Add host based unit tests for the MtrrLib services. The BaseLib services AsmCpuid(), AsmReadMsr64(), and AsmWriteMsr64() are hooked and provide simple emulation of the CPUID leafs and MSRs required by the MtrrLib to run as a host based unit test. Test cases are developed for each of the API. For the most important APIs MtrrSetMemoryAttributesInMtrrSettings() and MtrrSetMemoryAttributeInMtrrSettings(), random inputs are generated and fed to the APIs to make sure the implementation is good. The test application accepts an optional parameter which specifies how many iterations of feeding random inputs to the two APIs. The overall number of test cases increases when the iteration increases. Default iteration is 10 when no parameter is specified. Signed-off-by: Ray Ni Signed-off-by: Michael D Kinney Signed-off-by: Ming Shao Cc: Michael D Kinney Cc: Ming Shao Cc: Eric Dong Cc: Ray Ni Cc: Laszlo Ersek Cc: Sean Brogan Cc: Bret Barkelew Cc: Jiewen Yao --- .../MtrrLib/UnitTest/MtrrLibUnitTest.c | 1094 +++++++++++++++++ .../MtrrLib/UnitTest/MtrrLibUnitTest.h | 171 +++ .../MtrrLib/UnitTest/MtrrLibUnitTestHost.inf | 39 + UefiCpuPkg/Library/MtrrLib/UnitTest/Support.c | 913 ++++++++++++++ UefiCpuPkg/Test/UefiCpuPkgHostTest.dsc | 31 + 5 files changed, 2248 insertions(+) create mode 100644 UefiCpuPkg/Library/MtrrLib/UnitTest/MtrrLibUnitTest.c create mode 100644 UefiCpuPkg/Library/MtrrLib/UnitTest/MtrrLibUnitTest.h create mode 100644 UefiCpuPkg/Library/MtrrLib/UnitTest/MtrrLibUnitTestHost= .inf create mode 100644 UefiCpuPkg/Library/MtrrLib/UnitTest/Support.c create mode 100644 UefiCpuPkg/Test/UefiCpuPkgHostTest.dsc diff --git a/UefiCpuPkg/Library/MtrrLib/UnitTest/MtrrLibUnitTest.c b/UefiCp= uPkg/Library/MtrrLib/UnitTest/MtrrLibUnitTest.c new file mode 100644 index 0000000000..b0623ddea7 --- /dev/null +++ b/UefiCpuPkg/Library/MtrrLib/UnitTest/MtrrLibUnitTest.c @@ -0,0 +1,1094 @@ +/** @file=0D + Unit tests of the MtrrLib instance of the MtrrLib class=0D +=0D + Copyright (c) 2020, Intel Corporation. All rights reserved.
=0D + SPDX-License-Identifier: BSD-2-Clause-Patent=0D +=0D +**/=0D +=0D +#include "MtrrLibUnitTest.h"=0D +=0D +CONST MTRR_LIB_SYSTEM_PARAMETER mDefaultSystemParameter =3D {=0D + 42, TRUE, TRUE, CacheUncacheable, 12=0D + };=0D +=0D +MTRR_LIB_SYSTEM_PARAMETER mSystemParameters[] =3D {=0D + { 38, TRUE, TRUE, CacheUncacheable, 12 },=0D + { 38, TRUE, TRUE, CacheWriteBack, 12 },=0D + { 38, TRUE, TRUE, CacheWriteThrough, 12 },=0D + { 38, TRUE, TRUE, CacheWriteProtected, 12 },=0D + { 38, TRUE, TRUE, CacheWriteCombining, 12 },=0D +=0D + { 42, TRUE, TRUE, CacheUncacheable, 12 },=0D + { 42, TRUE, TRUE, CacheWriteBack, 12 },=0D + { 42, TRUE, TRUE, CacheWriteThrough, 12 },=0D + { 42, TRUE, TRUE, CacheWriteProtected, 12 },=0D + { 42, TRUE, TRUE, CacheWriteCombining, 12 },=0D +=0D + { 48, TRUE, TRUE, CacheUncacheable, 12 },=0D + { 48, TRUE, TRUE, CacheWriteBack, 12 },=0D + { 48, TRUE, TRUE, CacheWriteThrough, 12 },=0D + { 48, TRUE, TRUE, CacheWriteProtected, 12 },=0D + { 48, TRUE, TRUE, CacheWriteCombining, 12 },=0D +};=0D +=0D +UINT32 mFixedMtrrsIndex[] =3D {=0D + MSR_IA32_MTRR_FIX64K_00000,=0D + MSR_IA32_MTRR_FIX16K_80000,=0D + MSR_IA32_MTRR_FIX16K_A0000,=0D + MSR_IA32_MTRR_FIX4K_C0000,=0D + MSR_IA32_MTRR_FIX4K_C8000,=0D + MSR_IA32_MTRR_FIX4K_D0000,=0D + MSR_IA32_MTRR_FIX4K_D8000,=0D + MSR_IA32_MTRR_FIX4K_E0000,=0D + MSR_IA32_MTRR_FIX4K_E8000,=0D + MSR_IA32_MTRR_FIX4K_F0000,=0D + MSR_IA32_MTRR_FIX4K_F8000=0D +};=0D +STATIC_ASSERT (=0D + (ARRAY_SIZE (mFixedMtrrsIndex) =3D=3D MTRR_NUMBER_OF_FIXED_MTRR),=0D + "gFixedMtrrIndex does NOT contain all the fixed MTRRs!"=0D + );=0D +=0D +=0D +CHAR8 *mCacheDescription[] =3D { "UC", "WC", "N/A", "N/A", "WT", "WP", "WB= " };=0D +=0D +/**=0D + Compare the actual memory ranges against expected memory ranges and retu= rn PASS when they match.=0D +=0D + @param ExpectedMemoryRanges Expected memory ranges.=0D + @param ExpectedMemoryRangeCount Count of expected memory ranges.=0D + @param ActualRanges Actual memory ranges.=0D + @param ActualRangeCount Count of actual memory ranges.=0D +=0D + @retval UNIT_TEST_PASSED Test passed.=0D + @retval others Test failed.=0D +**/=0D +UNIT_TEST_STATUS=0D +VerifyMemoryRanges (=0D + IN MTRR_MEMORY_RANGE *ExpectedMemoryRanges,=0D + IN UINTN ExpectedMemoryRangeCount,=0D + IN MTRR_MEMORY_RANGE *ActualRanges,=0D + IN UINTN ActualRangeCount=0D + )=0D +{=0D + UINTN Index;=0D + UT_ASSERT_EQUAL (ExpectedMemoryRangeCount, ActualRangeCount);=0D + for (Index =3D 0; Index < ExpectedMemoryRangeCount; Index++) {=0D + UT_ASSERT_EQUAL (ExpectedMemoryRanges[Index].BaseAddress, ActualRanges= [Index].BaseAddress);=0D + UT_ASSERT_EQUAL (ExpectedMemoryRanges[Index].Length, ActualRanges[Inde= x].Length);=0D + UT_ASSERT_EQUAL (ExpectedMemoryRanges[Index].Type, ActualRanges[Index]= .Type);=0D + }=0D +=0D + return UNIT_TEST_PASSED;=0D +}=0D +=0D +/**=0D + Dump the memory ranges.=0D +=0D + @param Ranges Memory ranges to dump.=0D + @param RangeCount Count of memory ranges.=0D +**/=0D +VOID=0D +DumpMemoryRanges (=0D + MTRR_MEMORY_RANGE *Ranges,=0D + UINTN RangeCount=0D + )=0D +{=0D + UINTN Index;=0D + for (Index =3D 0; Index < RangeCount; Index++) {=0D + UT_LOG_INFO ("\t{ 0x%016llx, 0x%016llx, %a },\n", Ranges[Index].BaseAd= dress, Ranges[Index].Length, mCacheDescription[Ranges[Index].Type]);=0D + }=0D +}=0D +=0D +/**=0D +**/=0D +=0D +/**=0D + Generate random count of MTRRs for each cache type.=0D +=0D + @param TotalCount Total MTRR count.=0D + @param UcCount Return count of Uncacheable type.=0D + @param WtCount Return count of Write Through type.=0D + @param WbCount Return count of Write Back type.=0D + @param WpCount Return count of Write Protected type.=0D + @param WcCount Return count of Write Combining type.=0D +**/=0D +VOID=0D +GenerateRandomMemoryTypeCombination (=0D + IN UINT32 TotalCount,=0D + OUT UINT32 *UcCount,=0D + OUT UINT32 *WtCount,=0D + OUT UINT32 *WbCount,=0D + OUT UINT32 *WpCount,=0D + OUT UINT32 *WcCount=0D + )=0D +{=0D + UINTN Index;=0D + UINT32 TotalMtrrCount;=0D + UINT32 *CountPerType[5];=0D +=0D + CountPerType[0] =3D UcCount;=0D + CountPerType[1] =3D WtCount;=0D + CountPerType[2] =3D WbCount;=0D + CountPerType[3] =3D WpCount;=0D + CountPerType[4] =3D WcCount;=0D +=0D + //=0D + // Initialize the count of each cache type to 0.=0D + //=0D + for (Index =3D 0; Index < ARRAY_SIZE (CountPerType); Index++) {=0D + *(CountPerType[Index]) =3D 0;=0D + }=0D +=0D + //=0D + // Pick a random count of MTRRs=0D + //=0D + TotalMtrrCount =3D Random32 (1, TotalCount);=0D + for (Index =3D 0; Index < TotalMtrrCount; Index++) {=0D + //=0D + // For each of them, pick a random cache type.=0D + //=0D + (*(CountPerType[Random32 (0, ARRAY_SIZE (CountPerType) - 1)]))++;=0D + }=0D +}=0D +=0D +/**=0D + Unit test of MtrrLib service MtrrSetMemoryAttribute()=0D +=0D + @param[in] Context Ignored=0D +=0D + @retval UNIT_TEST_PASSED The Unit test has completed and th= e test=0D + case was successful.=0D + @retval UNIT_TEST_ERROR_TEST_FAILED A test case assertion has failed.= =0D +=0D +**/=0D +UNIT_TEST_STATUS=0D +EFIAPI=0D +UnitTestMtrrSetMemoryAttributesInMtrrSettings (=0D + IN UNIT_TEST_CONTEXT Context=0D + )=0D +{=0D + MTRR_LIB_SYSTEM_PARAMETER *SystemParameter;=0D + RETURN_STATUS Status;=0D + UINT32 UcCount;=0D + UINT32 WtCount;=0D + UINT32 WbCount;=0D + UINT32 WpCount;=0D + UINT32 WcCount;=0D +=0D + UINT32 MtrrIndex;=0D + UINT8 *Scratch;=0D + UINTN ScratchSize;=0D + MTRR_SETTINGS LocalMtrrs;=0D +=0D + MTRR_MEMORY_RANGE RawMtrrRange[MTRR_NUMBER_OF_VARIABLE_MTRR];=0D + MTRR_MEMORY_RANGE ExpectedMemoryRanges[MTRR_NUMBER_OF_FIXED_MTRR= * sizeof (UINT64) + 2 * MTRR_NUMBER_OF_VARIABLE_MTRR + 1];=0D + UINT32 ExpectedVariableMtrrUsage;=0D + UINTN ExpectedMemoryRangesCount;=0D +=0D + MTRR_MEMORY_RANGE ActualMemoryRanges[MTRR_NUMBER_OF_FIXED_MTRR = * sizeof (UINT64) + 2 * MTRR_NUMBER_OF_VARIABLE_MTRR + 1];=0D + UINT32 ActualVariableMtrrUsage;=0D + UINTN ActualMemoryRangesCount;=0D +=0D + MTRR_SETTINGS *Mtrrs[2];=0D +=0D + SystemParameter =3D (MTRR_LIB_SYSTEM_PARAMETER *) Context;=0D + GenerateRandomMemoryTypeCombination (=0D + SystemParameter->VariableMtrrCount - PatchPcdGet32 (PcdCpuNumberOfRese= rvedVariableMtrrs),=0D + &UcCount, &WtCount, &WbCount, &WpCount, &WcCount=0D + );=0D + GenerateValidAndConfigurableMtrrPairs (=0D + SystemParameter->PhysicalAddressBits, RawMtrrRange,=0D + UcCount, WtCount, WbCount, WpCount, WcCount=0D + );=0D +=0D + ExpectedVariableMtrrUsage =3D UcCount + WtCount + WbCount + WpCount + Wc= Count;=0D + ExpectedMemoryRangesCount =3D ARRAY_SIZE (ExpectedMemoryRanges);=0D + GetEffectiveMemoryRanges (=0D + SystemParameter->DefaultCacheType,=0D + SystemParameter->PhysicalAddressBits,=0D + RawMtrrRange, ExpectedVariableMtrrUsage,=0D + ExpectedMemoryRanges, &ExpectedMemoryRangesCount=0D + );=0D +=0D + UT_LOG_INFO (=0D + "Total MTRR [%d]: UC=3D%d, WT=3D%d, WB=3D%d, WP=3D%d, WC=3D%d\n",=0D + ExpectedVariableMtrrUsage, UcCount, WtCount, WbCount, WpCount, WcCount= =0D + );=0D + UT_LOG_INFO ("--- Expected Memory Ranges [%d] ---\n", ExpectedMemoryRang= esCount);=0D + DumpMemoryRanges (ExpectedMemoryRanges, ExpectedMemoryRangesCount);=0D +=0D + //=0D + // Default cache type is always an INPUT=0D + //=0D + ZeroMem (&LocalMtrrs, sizeof (LocalMtrrs));=0D + LocalMtrrs.MtrrDefType =3D MtrrGetDefaultMemoryType ();=0D + ScratchSize =3D SCRATCH_BUFFER_SIZE;=0D + Mtrrs[0] =3D &LocalMtrrs;=0D + Mtrrs[1] =3D NULL;=0D +=0D + for (MtrrIndex =3D 0; MtrrIndex < ARRAY_SIZE (Mtrrs); MtrrIndex++) {=0D + Scratch =3D calloc (ScratchSize, sizeof (UINT8));=0D + Status =3D MtrrSetMemoryAttributesInMtrrSettings (Mtrrs[MtrrIndex], Sc= ratch, &ScratchSize, ExpectedMemoryRanges, ExpectedMemoryRangesCount);=0D + if (Status =3D=3D RETURN_BUFFER_TOO_SMALL) {=0D + Scratch =3D realloc (Scratch, ScratchSize);=0D + Status =3D MtrrSetMemoryAttributesInMtrrSettings (Mtrrs[MtrrIndex], = Scratch, &ScratchSize, ExpectedMemoryRanges, ExpectedMemoryRangesCount);=0D + }=0D + UT_ASSERT_STATUS_EQUAL (Status, RETURN_SUCCESS);=0D +=0D + if (Mtrrs[MtrrIndex] =3D=3D NULL) {=0D + ZeroMem (&LocalMtrrs, sizeof (LocalMtrrs));=0D + MtrrGetAllMtrrs (&LocalMtrrs);=0D + }=0D + ActualMemoryRangesCount =3D ARRAY_SIZE (ActualMemoryRanges);=0D + CollectTestResult (=0D + SystemParameter->DefaultCacheType, SystemParameter->PhysicalAddressB= its, SystemParameter->VariableMtrrCount,=0D + &LocalMtrrs, ActualMemoryRanges, &ActualMemoryRangesCount, &ActualVa= riableMtrrUsage=0D + );=0D +=0D + UT_LOG_INFO ("--- Actual Memory Ranges [%d] ---\n", ActualMemoryRanges= Count);=0D + DumpMemoryRanges (ActualMemoryRanges, ActualMemoryRangesCount);=0D + VerifyMemoryRanges (ExpectedMemoryRanges, ExpectedMemoryRangesCount, A= ctualMemoryRanges, ActualMemoryRangesCount);=0D + UT_ASSERT_TRUE (ExpectedVariableMtrrUsage >=3D ActualVariableMtrrUsage= );=0D +=0D + ZeroMem (&LocalMtrrs, sizeof (LocalMtrrs));=0D + }=0D +=0D + free (Scratch);=0D +=0D + return UNIT_TEST_PASSED;=0D +}=0D +=0D +/**=0D + Test routine to check whether invalid base/size can be rejected.=0D +=0D + @param Context Pointer to MTRR_LIB_SYSTEM_PARAMETER.=0D +=0D + @return Test status.=0D +**/=0D +UNIT_TEST_STATUS=0D +EFIAPI=0D +UnitTestInvalidMemoryLayouts (=0D + IN UNIT_TEST_CONTEXT Context=0D + )=0D +{=0D + MTRR_LIB_SYSTEM_PARAMETER *SystemParameter;=0D + MTRR_MEMORY_RANGE Ranges[MTRR_NUMBER_OF_VARIABLE_MTRR * 2 + = 1];=0D + UINTN RangeCount;=0D + UINT64 MaxAddress;=0D + UINT32 Index;=0D + UINT64 BaseAddress;=0D + UINT64 Length;=0D + RETURN_STATUS Status;=0D + UINTN ScratchSize;=0D +=0D + SystemParameter =3D (MTRR_LIB_SYSTEM_PARAMETER *) Context;=0D +=0D + RangeCount =3D Random32 (1, ARRAY_SIZE (Ranges));=0D + MaxAddress =3D 1ull << SystemParameter->PhysicalAddressBits;=0D +=0D + for (Index =3D 0; Index < RangeCount; Index++) {=0D + do {=0D + BaseAddress =3D Random64 (0, MaxAddress);=0D + Length =3D Random64 (1, MaxAddress - BaseAddress);=0D + } while (((BaseAddress & 0xFFF) =3D=3D 0) || ((Length & 0xFFF) =3D=3D = 0));=0D +=0D + Ranges[Index].BaseAddress =3D BaseAddress;=0D + Ranges[Index].Length =3D Length;=0D + Ranges[Index].Type =3D GenerateRandomCacheType ();=0D +=0D + Status =3D MtrrSetMemoryAttribute (=0D + Ranges[Index].BaseAddress, Ranges[Index].Length, Ranges[Index].Type= =0D + );=0D + UT_ASSERT_TRUE (RETURN_ERROR (Status));=0D + }=0D +=0D + ScratchSize =3D 0;=0D + Status =3D MtrrSetMemoryAttributesInMtrrSettings (NULL, NULL, &ScratchSi= ze, Ranges, RangeCount);=0D + UT_ASSERT_TRUE (RETURN_ERROR (Status));=0D +=0D + return UNIT_TEST_PASSED;=0D +}=0D +=0D +/**=0D + Unit test of MtrrLib service IsMtrrSupported()=0D +=0D + @param[in] Context Ignored=0D +=0D + @retval UNIT_TEST_PASSED The Unit test has completed and th= e test=0D + case was successful.=0D + @retval UNIT_TEST_ERROR_TEST_FAILED A test case assertion has failed.= =0D +=0D +**/=0D +UNIT_TEST_STATUS=0D +EFIAPI=0D +UnitTestIsMtrrSupported (=0D + IN UNIT_TEST_CONTEXT Context=0D + )=0D +{=0D + MTRR_LIB_SYSTEM_PARAMETER SystemParameter;=0D +=0D + CopyMem (&SystemParameter, &mDefaultSystemParameter, sizeof (SystemParam= eter));=0D + //=0D + // MTRR capability off in CPUID leaf.=0D + //=0D + SystemParameter.MtrrSupported =3D FALSE;=0D + InitializeMtrrRegs (&SystemParameter);=0D + UT_ASSERT_FALSE (IsMtrrSupported ());=0D +=0D + //=0D + // MTRR capability on in CPUID leaf, but no variable or fixed MTRRs.=0D + //=0D + SystemParameter.MtrrSupported =3D TRUE;=0D + SystemParameter.VariableMtrrCount =3D 0;=0D + SystemParameter.FixedMtrrSupported =3D FALSE;=0D + InitializeMtrrRegs (&SystemParameter);=0D + UT_ASSERT_FALSE (IsMtrrSupported ());=0D +=0D + //=0D + // MTRR capability on in CPUID leaf, but no variable MTRRs.=0D + //=0D + SystemParameter.MtrrSupported =3D TRUE;=0D + SystemParameter.VariableMtrrCount =3D 0;=0D + SystemParameter.FixedMtrrSupported =3D TRUE;=0D + InitializeMtrrRegs (&SystemParameter);=0D + UT_ASSERT_FALSE (IsMtrrSupported ());=0D +=0D + //=0D + // MTRR capability on in CPUID leaf, but no fixed MTRRs.=0D + //=0D + SystemParameter.MtrrSupported =3D TRUE;=0D + SystemParameter.VariableMtrrCount =3D 7;=0D + SystemParameter.FixedMtrrSupported =3D FALSE;=0D + InitializeMtrrRegs (&SystemParameter);=0D + UT_ASSERT_FALSE (IsMtrrSupported ());=0D +=0D + //=0D + // MTRR capability on in CPUID leaf with both variable and fixed MTRRs.= =0D + //=0D + SystemParameter.MtrrSupported =3D TRUE;=0D + SystemParameter.VariableMtrrCount =3D 7;=0D + SystemParameter.FixedMtrrSupported =3D TRUE;=0D + InitializeMtrrRegs (&SystemParameter);=0D + UT_ASSERT_TRUE (IsMtrrSupported ());=0D +=0D + return UNIT_TEST_PASSED;=0D +}=0D +=0D +/**=0D + Unit test of MtrrLib service GetVariableMtrrCount()=0D +=0D + @param[in] Context Ignored=0D +=0D + @retval UNIT_TEST_PASSED The Unit test has completed and th= e test=0D + case was successful.=0D + @retval UNIT_TEST_ERROR_TEST_FAILED A test case assertion has failed.= =0D +=0D +**/=0D +UNIT_TEST_STATUS=0D +EFIAPI=0D +UnitTestGetVariableMtrrCount (=0D + IN UNIT_TEST_CONTEXT Context=0D + )=0D +{=0D + UINT32 Result;=0D + MTRR_LIB_SYSTEM_PARAMETER SystemParameter;=0D +=0D + CopyMem (&SystemParameter, &mDefaultSystemParameter, sizeof (SystemParam= eter));=0D + //=0D + // If MTRR capability off in CPUID leaf, then the count is always 0.=0D + //=0D + SystemParameter.MtrrSupported =3D FALSE;=0D + for (SystemParameter.VariableMtrrCount =3D 1; SystemParameter.VariableMt= rrCount <=3D MTRR_NUMBER_OF_VARIABLE_MTRR; SystemParameter.VariableMtrrCoun= t++) {=0D + InitializeMtrrRegs (&SystemParameter);=0D + Result =3D GetVariableMtrrCount ();=0D + UT_ASSERT_EQUAL (Result, 0);=0D + }=0D +=0D + //=0D + // Try all supported variable MTRR counts.=0D + // If variable MTRR count is > MTRR_NUMBER_OF_VARIABLE_MTRR, then an ASS= ERT()=0D + // is generated.=0D + //=0D + SystemParameter.MtrrSupported =3D TRUE;=0D + for (SystemParameter.VariableMtrrCount =3D 1; SystemParameter.VariableMt= rrCount <=3D MTRR_NUMBER_OF_VARIABLE_MTRR; SystemParameter.VariableMtrrCoun= t++) {=0D + InitializeMtrrRegs (&SystemParameter);=0D + Result =3D GetVariableMtrrCount ();=0D + UT_ASSERT_EQUAL (Result, SystemParameter.VariableMtrrCount);=0D + }=0D +=0D + //=0D + // Expect ASSERT() if variable MTRR count is > MTRR_NUMBER_OF_VARIABLE_M= TRR=0D + //=0D + SystemParameter.VariableMtrrCount =3D MTRR_NUMBER_OF_VARIABLE_MTRR + 1;= =0D + InitializeMtrrRegs (&SystemParameter);=0D + UT_EXPECT_ASSERT_FAILURE (GetVariableMtrrCount (), NULL);=0D +=0D + SystemParameter.MtrrSupported =3D TRUE;=0D + SystemParameter.VariableMtrrCount =3D MAX_UINT8;=0D + InitializeMtrrRegs (&SystemParameter);=0D + UT_EXPECT_ASSERT_FAILURE (GetVariableMtrrCount (), NULL);=0D +=0D + return UNIT_TEST_PASSED;=0D +}=0D +=0D +/**=0D + Unit test of MtrrLib service GetFirmwareVariableMtrrCount()=0D +=0D + @param[in] Context Ignored=0D +=0D + @retval UNIT_TEST_PASSED The Unit test has completed and th= e test=0D + case was successful.=0D + @retval UNIT_TEST_ERROR_TEST_FAILED A test case assertion has failed.= =0D +=0D +**/=0D +UNIT_TEST_STATUS=0D +EFIAPI=0D +UnitTestGetFirmwareVariableMtrrCount (=0D + IN UNIT_TEST_CONTEXT Context=0D + )=0D +{=0D + UINT32 Result;=0D + UINT32 ReservedMtrrs;=0D + MTRR_LIB_SYSTEM_PARAMETER SystemParameter;=0D +=0D + CopyMem (&SystemParameter, &mDefaultSystemParameter, sizeof (SystemParam= eter));=0D +=0D + InitializeMtrrRegs (&SystemParameter);=0D + //=0D + // Positive test cases for VCNT =3D 10 and Reserved PCD in range 0..10=0D + //=0D + for (ReservedMtrrs =3D 0; ReservedMtrrs <=3D SystemParameter.VariableMtr= rCount; ReservedMtrrs++) {=0D + PatchPcdSet32 (PcdCpuNumberOfReservedVariableMtrrs, ReservedMtrrs);=0D + Result =3D GetFirmwareVariableMtrrCount ();=0D + UT_ASSERT_EQUAL (Result, SystemParameter.VariableMtrrCount - ReservedM= trrs);=0D + }=0D +=0D + //=0D + // Negative test cases when Reserved PCD is larger than VCNT=0D + //=0D + for (ReservedMtrrs =3D SystemParameter.VariableMtrrCount + 1; ReservedMt= rrs <=3D 255; ReservedMtrrs++) {=0D + PatchPcdSet32 (PcdCpuNumberOfReservedVariableMtrrs, ReservedMtrrs);=0D + Result =3D GetFirmwareVariableMtrrCount ();=0D + UT_ASSERT_EQUAL (Result, 0);=0D + }=0D +=0D + //=0D + // Negative test cases when Reserved PCD is larger than VCNT=0D + //=0D + PatchPcdSet32 (PcdCpuNumberOfReservedVariableMtrrs, MAX_UINT32);=0D + Result =3D GetFirmwareVariableMtrrCount ();=0D + UT_ASSERT_EQUAL (Result, 0);=0D +=0D + //=0D + // Negative test case when MTRRs are not supported=0D + //=0D + SystemParameter.MtrrSupported =3D FALSE;=0D + InitializeMtrrRegs (&SystemParameter);=0D + PatchPcdSet32 (PcdCpuNumberOfReservedVariableMtrrs, 2);=0D + Result =3D GetFirmwareVariableMtrrCount ();=0D + UT_ASSERT_EQUAL (Result, 0);=0D +=0D + //=0D + // Negative test case when Fixed MTRRs are not supported=0D + //=0D + SystemParameter.MtrrSupported =3D TRUE;=0D + SystemParameter.FixedMtrrSupported =3D FALSE;=0D + InitializeMtrrRegs (&SystemParameter);=0D + PatchPcdSet32 (PcdCpuNumberOfReservedVariableMtrrs, 2);=0D + Result =3D GetFirmwareVariableMtrrCount ();=0D + UT_ASSERT_EQUAL (Result, 0);=0D +=0D + //=0D + // Expect ASSERT() if variable MTRR count is > MTRR_NUMBER_OF_VARIABLE_M= TRR=0D + //=0D + SystemParameter.FixedMtrrSupported =3D TRUE;=0D + SystemParameter.VariableMtrrCount =3D MTRR_NUMBER_OF_VARIABLE_MTRR + 1;= =0D + InitializeMtrrRegs (&SystemParameter);=0D + UT_EXPECT_ASSERT_FAILURE (GetFirmwareVariableMtrrCount (), NULL);=0D +=0D + return UNIT_TEST_PASSED;=0D +}=0D +=0D +/**=0D + Unit test of MtrrLib service MtrrGetMemoryAttribute()=0D +=0D + @param[in] Context Ignored=0D +=0D + @retval UNIT_TEST_PASSED The Unit test has completed and th= e test=0D + case was successful.=0D + @retval UNIT_TEST_ERROR_TEST_FAILED A test case assertion has failed.= =0D +=0D +**/=0D +UNIT_TEST_STATUS=0D +EFIAPI=0D +UnitTestMtrrGetMemoryAttribute (=0D + IN UNIT_TEST_CONTEXT Context=0D + )=0D +{=0D + return UNIT_TEST_PASSED;=0D +}=0D +=0D +/**=0D + Unit test of MtrrLib service MtrrGetFixedMtrr()=0D +=0D + @param[in] Context Ignored=0D +=0D + @retval UNIT_TEST_PASSED The Unit test has completed and th= e test=0D + case was successful.=0D + @retval UNIT_TEST_ERROR_TEST_FAILED A test case assertion has failed.= =0D +=0D +**/=0D +UNIT_TEST_STATUS=0D +EFIAPI=0D +UnitTestMtrrGetFixedMtrr (=0D + IN UNIT_TEST_CONTEXT Context=0D + )=0D +{=0D + MTRR_FIXED_SETTINGS *Result;=0D + MTRR_FIXED_SETTINGS ExpectedFixedSettings;=0D + MTRR_FIXED_SETTINGS FixedSettings;=0D + UINTN Index;=0D + UINTN MsrIndex;=0D + UINTN ByteIndex;=0D + UINT64 MsrValue;=0D + MTRR_LIB_SYSTEM_PARAMETER SystemParameter;=0D +=0D + CopyMem (&SystemParameter, &mDefaultSystemParameter, sizeof (SystemParam= eter));=0D + InitializeMtrrRegs (&SystemParameter);=0D + //=0D + // Set random cache type to different ranges under 1MB and make sure=0D + // the fixed MTRR settings are expected.=0D + // Try 100 times.=0D + //=0D + for (Index =3D 0; Index < 100; Index++) {=0D + for (MsrIndex =3D 0; MsrIndex < ARRAY_SIZE (mFixedMtrrsIndex); MsrInde= x++) {=0D + MsrValue =3D 0;=0D + for (ByteIndex =3D 0; ByteIndex < sizeof (UINT64); ByteIndex++) {=0D + MsrValue =3D MsrValue | LShiftU64 (GenerateRandomCacheType (), Byt= eIndex * 8);=0D + }=0D + ExpectedFixedSettings.Mtrr[MsrIndex] =3D MsrValue;=0D + AsmWriteMsr64 (mFixedMtrrsIndex[MsrIndex], MsrValue);=0D + }=0D +=0D + Result =3D MtrrGetFixedMtrr (&FixedSettings);=0D + UT_ASSERT_EQUAL (Result, &FixedSettings);=0D + UT_ASSERT_MEM_EQUAL (&FixedSettings, &ExpectedFixedSettings, sizeof (F= ixedSettings));=0D + }=0D +=0D + //=0D + // Negative test case when MTRRs are not supported=0D + //=0D + SystemParameter.MtrrSupported =3D FALSE;=0D + InitializeMtrrRegs (&SystemParameter);=0D +=0D + ZeroMem (&FixedSettings, sizeof (FixedSettings));=0D + ZeroMem (&ExpectedFixedSettings, sizeof (ExpectedFixedSettings));=0D + Result =3D MtrrGetFixedMtrr (&FixedSettings);=0D + UT_ASSERT_EQUAL (Result, &FixedSettings);=0D + UT_ASSERT_MEM_EQUAL (&ExpectedFixedSettings, &FixedSettings, sizeof (Exp= ectedFixedSettings));=0D +=0D + return UNIT_TEST_PASSED;=0D +}=0D +=0D +/**=0D + Unit test of MtrrLib service MtrrGetAllMtrrs()=0D +=0D + @param[in] Context Ignored=0D +=0D + @retval UNIT_TEST_PASSED The Unit test has completed and th= e test=0D + case was successful.=0D + @retval UNIT_TEST_ERROR_TEST_FAILED A test case assertion has failed.= =0D +=0D +**/=0D +UNIT_TEST_STATUS=0D +EFIAPI=0D +UnitTestMtrrGetAllMtrrs (=0D + IN UNIT_TEST_CONTEXT Context=0D + )=0D +{=0D + MTRR_SETTINGS *Result;=0D + MTRR_SETTINGS Mtrrs;=0D + MTRR_SETTINGS ExpectedMtrrs;=0D + MTRR_VARIABLE_SETTING VariableMtrr[MTRR_NUMBER_OF_VARIABLE_MTRR];=0D + UINT32 Index;=0D +=0D + MTRR_LIB_SYSTEM_PARAMETER SystemParameter;=0D +=0D + CopyMem (&SystemParameter, &mDefaultSystemParameter, sizeof (SystemParam= eter));=0D + InitializeMtrrRegs (&SystemParameter);=0D +=0D + for (Index =3D 0; Index < SystemParameter.VariableMtrrCount; Index++) {= =0D + GenerateRandomMtrrPair (SystemParameter.PhysicalAddressBits, GenerateR= andomCacheType (), &VariableMtrr[Index], NULL);=0D + AsmWriteMsr64 (MSR_IA32_MTRR_PHYSBASE0 + (Index << 1), VariableMtrr[In= dex].Base);=0D + AsmWriteMsr64 (MSR_IA32_MTRR_PHYSMASK0 + (Index << 1), VariableMtrr[In= dex].Mask);=0D + }=0D + Result =3D MtrrGetAllMtrrs (&Mtrrs);=0D + UT_ASSERT_EQUAL (Result, &Mtrrs);=0D + UT_ASSERT_MEM_EQUAL (Mtrrs.Variables.Mtrr, VariableMtrr, sizeof (MTRR_VA= RIABLE_SETTING) * SystemParameter.VariableMtrrCount);=0D +=0D + //=0D + // Negative test case when MTRRs are not supported=0D + //=0D + ZeroMem (&ExpectedMtrrs, sizeof (ExpectedMtrrs));=0D + ZeroMem (&Mtrrs, sizeof (Mtrrs));=0D +=0D + SystemParameter.MtrrSupported =3D FALSE;=0D + InitializeMtrrRegs (&SystemParameter);=0D + Result =3D MtrrGetAllMtrrs (&Mtrrs);=0D + UT_ASSERT_EQUAL (Result, &Mtrrs);=0D + UT_ASSERT_MEM_EQUAL (&ExpectedMtrrs, &Mtrrs, sizeof (ExpectedMtrrs));=0D +=0D + //=0D + // Expect ASSERT() if variable MTRR count is > MTRR_NUMBER_OF_VARIABLE_M= TRR=0D + //=0D + SystemParameter.MtrrSupported =3D TRUE;=0D + SystemParameter.VariableMtrrCount =3D MTRR_NUMBER_OF_VARIABLE_MTRR + 1;= =0D + InitializeMtrrRegs (&SystemParameter);=0D + UT_EXPECT_ASSERT_FAILURE (MtrrGetAllMtrrs (&Mtrrs), NULL);=0D +=0D + return UNIT_TEST_PASSED;=0D +}=0D +=0D +/**=0D + Unit test of MtrrLib service MtrrSetAllMtrrs()=0D +=0D + @param[in] Context Ignored=0D +=0D + @retval UNIT_TEST_PASSED The Unit test has completed and th= e test=0D + case was successful.=0D + @retval UNIT_TEST_ERROR_TEST_FAILED A test case assertion has failed.= =0D +=0D +**/=0D +UNIT_TEST_STATUS=0D +EFIAPI=0D +UnitTestMtrrSetAllMtrrs (=0D + IN UNIT_TEST_CONTEXT Context=0D + )=0D +{=0D + MTRR_SETTINGS *Result;=0D + MTRR_SETTINGS Mtrrs;=0D + UINT32 Index;=0D + MSR_IA32_MTRR_DEF_TYPE_REGISTER Default;=0D +=0D + MTRR_LIB_SYSTEM_PARAMETER SystemParameter;=0D +=0D + CopyMem (&SystemParameter, &mDefaultSystemParameter, sizeof (SystemParam= eter));=0D + InitializeMtrrRegs (&SystemParameter);=0D +=0D + Default.Uint64 =3D 0;=0D + Default.Bits.E =3D 1;=0D + Default.Bits.FE =3D 1;=0D + Default.Bits.Type =3D GenerateRandomCacheType ();=0D +=0D + ZeroMem (&Mtrrs, sizeof (Mtrrs));=0D + Mtrrs.MtrrDefType =3D Default.Uint64;=0D + for (Index =3D 0; Index < SystemParameter.VariableMtrrCount; Index++) {= =0D + GenerateRandomMtrrPair (SystemParameter.PhysicalAddressBits, GenerateR= andomCacheType (), &Mtrrs.Variables.Mtrr[Index], NULL);=0D + }=0D + Result =3D MtrrSetAllMtrrs (&Mtrrs);=0D + UT_ASSERT_EQUAL (Result, &Mtrrs);=0D +=0D + UT_ASSERT_EQUAL (AsmReadMsr64 (MSR_IA32_MTRR_DEF_TYPE), Mtrrs.MtrrDefTyp= e);=0D + for (Index =3D 0; Index < SystemParameter.VariableMtrrCount; Index++) {= =0D + UT_ASSERT_EQUAL (AsmReadMsr64 (MSR_IA32_MTRR_PHYSBASE0 + (Index << 1))= , Mtrrs.Variables.Mtrr[Index].Base);=0D + UT_ASSERT_EQUAL (AsmReadMsr64 (MSR_IA32_MTRR_PHYSMASK0 + (Index << 1))= , Mtrrs.Variables.Mtrr[Index].Mask);=0D + }=0D +=0D + return UNIT_TEST_PASSED;=0D +}=0D +=0D +/**=0D + Unit test of MtrrLib service MtrrGetMemoryAttributeInVariableMtrr()=0D +=0D + @param[in] Context Ignored=0D +=0D + @retval UNIT_TEST_PASSED The Unit test has completed and th= e test=0D + case was successful.=0D + @retval UNIT_TEST_ERROR_TEST_FAILED A test case assertion has failed.= =0D +=0D +**/=0D +UNIT_TEST_STATUS=0D +EFIAPI=0D +UnitTestMtrrGetMemoryAttributeInVariableMtrr (=0D + IN UNIT_TEST_CONTEXT Context=0D + )=0D +{=0D + MTRR_LIB_SYSTEM_PARAMETER SystemParameter;=0D + UINT32 Result;=0D + MTRR_VARIABLE_SETTING VariableSetting[MTRR_NUMBER_OF_VARIABLE_= MTRR];=0D + VARIABLE_MTRR VariableMtrr[MTRR_NUMBER_OF_VARIABLE_MTR= R];=0D + UINT64 ValidMtrrBitsMask;=0D + UINT64 ValidMtrrAddressMask;=0D + UINT32 Index;=0D + MSR_IA32_MTRR_PHYSBASE_REGISTER Base;=0D + MSR_IA32_MTRR_PHYSMASK_REGISTER Mask;=0D +=0D + CopyMem (&SystemParameter, &mDefaultSystemParameter, sizeof (SystemParam= eter));=0D +=0D + InitializeMtrrRegs (&SystemParameter);=0D +=0D + ValidMtrrBitsMask =3D (1ull << SystemParameter.PhysicalAddressBits) -= 1;=0D + ValidMtrrAddressMask =3D ValidMtrrBitsMask & 0xfffffffffffff000ULL;=0D +=0D + for (Index =3D 0; Index < SystemParameter.VariableMtrrCount; Index++) {= =0D + GenerateRandomMtrrPair (SystemParameter.PhysicalAddressBits, GenerateR= andomCacheType (), &VariableSetting[Index], NULL);=0D + AsmWriteMsr64 (MSR_IA32_MTRR_PHYSBASE0 + (Index << 1), VariableSetting= [Index].Base);=0D + AsmWriteMsr64 (MSR_IA32_MTRR_PHYSMASK0 + (Index << 1), VariableSetting= [Index].Mask);=0D + }=0D + Result =3D MtrrGetMemoryAttributeInVariableMtrr (ValidMtrrBitsMask, Vali= dMtrrAddressMask, VariableMtrr);=0D + UT_ASSERT_EQUAL (Result, SystemParameter.VariableMtrrCount);=0D +=0D + for (Index =3D 0; Index < SystemParameter.VariableMtrrCount; Index++) {= =0D + Base.Uint64 =3D VariableMtrr[Index].BaseAddress;=0D + Base.Bits.Type =3D (UINT32) VariableMtrr[Index].Type;=0D + UT_ASSERT_EQUAL (Base.Uint64, VariableSetting[Index].Base);=0D +=0D + Mask.Uint64 =3D ~(VariableMtrr[Index].Length - 1) & ValidMtrrBitsMa= sk;=0D + Mask.Bits.V =3D 1;=0D + UT_ASSERT_EQUAL (Mask.Uint64, VariableSetting[Index].Mask);=0D + }=0D +=0D + //=0D + // Negative test case when MTRRs are not supported=0D + //=0D + SystemParameter.MtrrSupported =3D FALSE;=0D + InitializeMtrrRegs (&SystemParameter);=0D + Result =3D MtrrGetMemoryAttributeInVariableMtrr (ValidMtrrBitsMask, Vali= dMtrrAddressMask, VariableMtrr);=0D + UT_ASSERT_EQUAL (Result, 0);=0D +=0D + //=0D + // Expect ASSERT() if variable MTRR count is > MTRR_NUMBER_OF_VARIABLE_M= TRR=0D + //=0D + SystemParameter.MtrrSupported =3D TRUE;=0D + SystemParameter.VariableMtrrCount =3D MTRR_NUMBER_OF_VARIABLE_MTRR + 1;= =0D + InitializeMtrrRegs (&SystemParameter);=0D + UT_EXPECT_ASSERT_FAILURE (MtrrGetMemoryAttributeInVariableMtrr (ValidMtr= rBitsMask, ValidMtrrAddressMask, VariableMtrr), NULL);=0D +=0D + return UNIT_TEST_PASSED;=0D +}=0D +=0D +/**=0D + Unit test of MtrrLib service MtrrDebugPrintAllMtrrs()=0D +=0D + @param[in] Context Ignored=0D +=0D + @retval UNIT_TEST_PASSED The Unit test has completed and th= e test=0D + case was successful.=0D + @retval UNIT_TEST_ERROR_TEST_FAILED A test case assertion has failed.= =0D +=0D +**/=0D +UNIT_TEST_STATUS=0D +EFIAPI=0D +UnitTestMtrrDebugPrintAllMtrrs (=0D + IN UNIT_TEST_CONTEXT Context=0D + )=0D +{=0D + return UNIT_TEST_PASSED;=0D +}=0D +=0D +/**=0D + Unit test of MtrrLib service MtrrGetDefaultMemoryType().=0D +=0D + @param[in] Context Ignored=0D +=0D + @retval UNIT_TEST_PASSED The Unit test has completed and th= e test=0D + case was successful.=0D + @retval UNIT_TEST_ERROR_TEST_FAILED A test case assertion has failed.= =0D +=0D +**/=0D +UNIT_TEST_STATUS=0D +EFIAPI=0D +UnitTestMtrrGetDefaultMemoryType (=0D + IN UNIT_TEST_CONTEXT Context=0D + )=0D +{=0D + UINTN Index;=0D + MTRR_MEMORY_CACHE_TYPE Result;=0D + MTRR_LIB_SYSTEM_PARAMETER SystemParameter;=0D + MTRR_MEMORY_CACHE_TYPE CacheType[5];=0D +=0D + CacheType[0] =3D CacheUncacheable;=0D + CacheType[1] =3D CacheWriteCombining;=0D + CacheType[2] =3D CacheWriteThrough;=0D + CacheType[3] =3D CacheWriteProtected;=0D + CacheType[4] =3D CacheWriteBack;=0D +=0D + CopyMem (&SystemParameter, &mDefaultSystemParameter, sizeof (SystemParam= eter));=0D + //=0D + // If MTRRs are supported, then always return the cache type in the MSR= =0D + // MSR_IA32_MTRR_DEF_TYPE=0D + //=0D + for (Index =3D 0; Index < ARRAY_SIZE (CacheType); Index++) {=0D + SystemParameter.DefaultCacheType =3D CacheType[Index];=0D + InitializeMtrrRegs (&SystemParameter);=0D + Result =3D MtrrGetDefaultMemoryType ();=0D + UT_ASSERT_EQUAL (Result, SystemParameter.DefaultCacheType);=0D + }=0D +=0D + //=0D + // If MTRRs are not supported, then always return CacheUncacheable=0D + //=0D + SystemParameter.MtrrSupported =3D FALSE;=0D + InitializeMtrrRegs (&SystemParameter);=0D + Result =3D MtrrGetDefaultMemoryType ();=0D + UT_ASSERT_EQUAL (Result, CacheUncacheable);=0D +=0D + SystemParameter.MtrrSupported =3D TRUE;=0D + SystemParameter.FixedMtrrSupported =3D FALSE;=0D + InitializeMtrrRegs (&SystemParameter);=0D + Result =3D MtrrGetDefaultMemoryType ();=0D + UT_ASSERT_EQUAL (Result, CacheUncacheable);=0D +=0D + SystemParameter.MtrrSupported =3D TRUE;=0D + SystemParameter.FixedMtrrSupported =3D TRUE;=0D + SystemParameter.VariableMtrrCount =3D 0;=0D + InitializeMtrrRegs (&SystemParameter);=0D + Result =3D MtrrGetDefaultMemoryType ();=0D + UT_ASSERT_EQUAL (Result, CacheUncacheable);=0D +=0D + return UNIT_TEST_PASSED;=0D +}=0D +=0D +/**=0D + Unit test of MtrrLib service MtrrSetMemoryAttributeInMtrrSettings().=0D +=0D + @param[in] Context Ignored=0D +=0D + @retval UNIT_TEST_PASSED The Unit test has completed and th= e test=0D + case was successful.=0D + @retval UNIT_TEST_ERROR_TEST_FAILED A test case assertion has failed.= =0D +=0D +**/=0D +UNIT_TEST_STATUS=0D +EFIAPI=0D +UnitTestMtrrSetMemoryAttributeInMtrrSettings (=0D + IN UNIT_TEST_CONTEXT Context=0D + )=0D +{=0D + MTRR_LIB_SYSTEM_PARAMETER *SystemParameter;=0D + RETURN_STATUS Status;=0D + UINT32 UcCount;=0D + UINT32 WtCount;=0D + UINT32 WbCount;=0D + UINT32 WpCount;=0D + UINT32 WcCount;=0D +=0D + UINTN MtrrIndex;=0D + UINTN Index;=0D + MTRR_SETTINGS LocalMtrrs;=0D +=0D + MTRR_MEMORY_RANGE RawMtrrRange[MTRR_NUMBER_OF_VARIABLE_MTRR];=0D + MTRR_MEMORY_RANGE ExpectedMemoryRanges[MTRR_NUMBER_OF_FIXED_MTRR= * sizeof (UINT64) + 2 * MTRR_NUMBER_OF_VARIABLE_MTRR + 1];=0D + UINT32 ExpectedVariableMtrrUsage;=0D + UINTN ExpectedMemoryRangesCount;=0D +=0D + MTRR_MEMORY_RANGE ActualMemoryRanges[MTRR_NUMBER_OF_FIXED_MTRR *= sizeof (UINT64) + 2 * MTRR_NUMBER_OF_VARIABLE_MTRR + 1];=0D + UINT32 ActualVariableMtrrUsage;=0D + UINTN ActualMemoryRangesCount;=0D +=0D + MTRR_SETTINGS *Mtrrs[2];=0D +=0D + SystemParameter =3D (MTRR_LIB_SYSTEM_PARAMETER *) Context;=0D + GenerateRandomMemoryTypeCombination (=0D + SystemParameter->VariableMtrrCount - PatchPcdGet32 (PcdCpuNumberOfRese= rvedVariableMtrrs),=0D + &UcCount, &WtCount, &WbCount, &WpCount, &WcCount=0D + );=0D + GenerateValidAndConfigurableMtrrPairs (=0D + SystemParameter->PhysicalAddressBits, RawMtrrRange,=0D + UcCount, WtCount, WbCount, WpCount, WcCount=0D + );=0D +=0D + ExpectedVariableMtrrUsage =3D UcCount + WtCount + WbCount + WpCount + Wc= Count;=0D + ExpectedMemoryRangesCount =3D ARRAY_SIZE (ExpectedMemoryRanges);=0D + GetEffectiveMemoryRanges (=0D + SystemParameter->DefaultCacheType,=0D + SystemParameter->PhysicalAddressBits,=0D + RawMtrrRange, ExpectedVariableMtrrUsage,=0D + ExpectedMemoryRanges, &ExpectedMemoryRangesCount=0D + );=0D +=0D + UT_LOG_INFO ("--- Expected Memory Ranges [%d] ---\n", ExpectedMemoryRang= esCount);=0D + DumpMemoryRanges (ExpectedMemoryRanges, ExpectedMemoryRangesCount);=0D + //=0D + // Default cache type is always an INPUT=0D + //=0D + ZeroMem (&LocalMtrrs, sizeof (LocalMtrrs));=0D + LocalMtrrs.MtrrDefType =3D MtrrGetDefaultMemoryType ();=0D + Mtrrs[0] =3D &LocalMtrrs;=0D + Mtrrs[1] =3D NULL;=0D +=0D + for (MtrrIndex =3D 0; MtrrIndex < ARRAY_SIZE (Mtrrs); MtrrIndex++) {=0D + for (Index =3D 0; Index < ExpectedMemoryRangesCount; Index++) {=0D + Status =3D MtrrSetMemoryAttributeInMtrrSettings (=0D + Mtrrs[MtrrIndex],=0D + ExpectedMemoryRanges[Index].BaseAddress,=0D + ExpectedMemoryRanges[Index].Length,=0D + ExpectedMemoryRanges[Index].Type=0D + );=0D + UT_ASSERT_TRUE (Status =3D=3D RETURN_SUCCESS || Status =3D=3D RETURN= _OUT_OF_RESOURCES || Status =3D=3D RETURN_BUFFER_TOO_SMALL);=0D + if (Status =3D=3D RETURN_OUT_OF_RESOURCES || Status =3D=3D RETURN_BU= FFER_TOO_SMALL) {=0D + return UNIT_TEST_SKIPPED;=0D + }=0D + }=0D +=0D + if (Mtrrs[MtrrIndex] =3D=3D NULL) {=0D + ZeroMem (&LocalMtrrs, sizeof (LocalMtrrs));=0D + MtrrGetAllMtrrs (&LocalMtrrs);=0D + }=0D + ActualMemoryRangesCount =3D ARRAY_SIZE (ActualMemoryRanges);=0D + CollectTestResult (=0D + SystemParameter->DefaultCacheType, SystemParameter->PhysicalAddressB= its, SystemParameter->VariableMtrrCount,=0D + &LocalMtrrs, ActualMemoryRanges, &ActualMemoryRangesCount, &ActualVa= riableMtrrUsage=0D + );=0D + UT_LOG_INFO ("--- Actual Memory Ranges [%d] ---\n", ActualMemoryRanges= Count);=0D + DumpMemoryRanges (ActualMemoryRanges, ActualMemoryRangesCount);=0D + VerifyMemoryRanges (ExpectedMemoryRanges, ExpectedMemoryRangesCount, A= ctualMemoryRanges, ActualMemoryRangesCount);=0D + UT_ASSERT_TRUE (ExpectedVariableMtrrUsage >=3D ActualVariableMtrrUsage= );=0D +=0D + ZeroMem (&LocalMtrrs, sizeof (LocalMtrrs));=0D + }=0D +=0D + return UNIT_TEST_PASSED;=0D +}=0D +=0D +/**=0D + Prep routine for UnitTestGetFirmwareVariableMtrrCount().=0D +=0D + @param Context Point to a UINT32 data to save the PcdCpuNumberOfReserve= dVariableMtrrs.=0D +**/=0D +UNIT_TEST_STATUS=0D +EFIAPI=0D +SavePcdValue (=0D + UNIT_TEST_CONTEXT Context=0D + )=0D +{=0D + *(UINT32 *) Context =3D PatchPcdGet32 (PcdCpuNumberOfReservedVariableMtr= rs);=0D + return UNIT_TEST_PASSED;=0D +}=0D +=0D +/**=0D + Clean up routine for UnitTestGetFirmwareVariableMtrrCount().=0D +=0D + @param Context Point to a UINT32 data to save the PcdCpuNumberOfReserve= dVariableMtrrs.=0D +**/=0D +UNIT_TEST_STATUS=0D +EFIAPI=0D +RestorePcdValue (=0D + UNIT_TEST_CONTEXT Context=0D + )=0D +{=0D + PatchPcdSet32 (PcdCpuNumberOfReservedVariableMtrrs, *(UINT32 *) Context)= ;=0D + return UNIT_TEST_PASSED;=0D +}=0D +=0D +/**=0D + Initialize the unit test framework, suite, and unit tests for the=0D + ResetSystemLib and run the ResetSystemLib unit test.=0D +=0D + @param Iteration Iteration of testing MtrrSetMemoryAttribu= teInMtrrSettings=0D + and MtrrSetMemoryAttributesInMtrrSettings= using random inputs.=0D +=0D + @retval EFI_SUCCESS All test cases were dispatched.=0D + @retval EFI_OUT_OF_RESOURCES There are not enough resources available = to=0D + initialize the unit tests.=0D +**/=0D +STATIC=0D +EFI_STATUS=0D +EFIAPI=0D +UnitTestingEntry (=0D + UINTN Iteration=0D + )=0D +{=0D + EFI_STATUS Status;=0D + UNIT_TEST_FRAMEWORK_HANDLE Framework;=0D + UNIT_TEST_SUITE_HANDLE MtrrApiTests;=0D + UINTN Index;=0D + UINTN SystemIndex;=0D + UINT32 OriginalPcdValue;=0D +=0D + Framework =3D NULL;=0D +=0D + DEBUG ((DEBUG_INFO, "%a v%a\n", UNIT_TEST_APP_NAME, UNIT_TEST_APP_VERSIO= N));=0D +=0D + //=0D + // Setup the test framework for running the tests.=0D + //=0D + Status =3D InitUnitTestFramework (&Framework, UNIT_TEST_APP_NAME, gEfiCa= llerBaseName, UNIT_TEST_APP_VERSION);=0D + if (EFI_ERROR (Status)) {=0D + DEBUG ((DEBUG_ERROR, "Failed in InitUnitTestFramework. Status =3D %r\n= ", Status));=0D + goto EXIT;=0D + }=0D +=0D + //=0D + // --------------Suite-----------Description--------------Name----------= Function--------Pre---Post-------------------Context-----------=0D + //=0D +=0D + //=0D + // Populate the MtrrLib API Unit Test Suite.=0D + //=0D + Status =3D CreateUnitTestSuite (&MtrrApiTests, Framework, "MtrrLib API T= ests", "MtrrLib.MtrrLib", NULL, NULL);=0D + if (EFI_ERROR (Status)) {=0D + DEBUG ((DEBUG_ERROR, "Failed in CreateUnitTestSuite for MtrrLib API Te= sts\n"));=0D + Status =3D EFI_OUT_OF_RESOURCES;=0D + goto EXIT;=0D + }=0D + AddTestCase (MtrrApiTests, "Test IsMtrrSupported", = "MtrrSupported", UnitTestIsMtrrSupported, = NULL, NULL, NULL);=0D + AddTestCase (MtrrApiTests, "Test GetVariableMtrrCount", = "GetVariableMtrrCount", UnitTestGetVariableMtrrCount, = NULL, NULL, NULL);=0D + AddTestCase (MtrrApiTests, "Test GetFirmwareVariableMtrrCount", = "GetFirmwareVariableMtrrCount", UnitTestGetFirmwareVariableMtrrCoun= t, SavePcdValue, RestorePcdValue, &OriginalPcdValue);=0D + AddTestCase (MtrrApiTests, "Test MtrrGetMemoryAttribute", = "MtrrGetMemoryAttribute", UnitTestMtrrGetMemoryAttribute, = NULL, NULL, NULL);=0D + AddTestCase (MtrrApiTests, "Test MtrrGetFixedMtrr", = "MtrrGetFixedMtrr", UnitTestMtrrGetFixedMtrr, = NULL, NULL, NULL);=0D + AddTestCase (MtrrApiTests, "Test MtrrGetAllMtrrs", = "MtrrGetAllMtrrs", UnitTestMtrrGetAllMtrrs, = NULL, NULL, NULL);=0D + AddTestCase (MtrrApiTests, "Test MtrrSetAllMtrrs", = "MtrrSetAllMtrrs", UnitTestMtrrSetAllMtrrs, = NULL, NULL, NULL);=0D + AddTestCase (MtrrApiTests, "Test MtrrGetMemoryAttributeInVariableMtrr", = "MtrrGetMemoryAttributeInVariableMtrr", UnitTestMtrrGetMemoryAttributeInVar= iableMtrr, NULL, NULL, NULL);=0D + AddTestCase (MtrrApiTests, "Test MtrrDebugPrintAllMtrrs", = "MtrrDebugPrintAllMtrrs", UnitTestMtrrDebugPrintAllMtrrs, = NULL, NULL, NULL);=0D + AddTestCase (MtrrApiTests, "Test MtrrGetDefaultMemoryType", = "MtrrGetDefaultMemoryType", UnitTestMtrrGetDefaultMemoryType, = NULL, NULL, NULL);=0D +=0D + for (SystemIndex =3D 0; SystemIndex < ARRAY_SIZE (mSystemParameters); Sy= stemIndex++) {=0D + for (Index =3D 0; Index < Iteration; Index++) {=0D + AddTestCase (MtrrApiTests, "Test InvalidMemoryLayouts", = "InvalidMemoryLayouts", UnitTestInvalidMemoryLayouts,= InitializeMtrrRegs, NULL, &mSystemParameters[SystemIndex]= );=0D + AddTestCase (MtrrApiTests, "Test MtrrSetMemoryAttributeInMtrrSetting= s", "MtrrSetMemoryAttributeInMtrrSettings", UnitTestMtrrSetMemoryAttribut= eInMtrrSettings, InitializeMtrrRegs, NULL, &mSystemParameters[SystemIndex]= );=0D + AddTestCase (MtrrApiTests, "Test MtrrSetMemoryAttributesInMtrrSettin= gs", "MtrrSetMemoryAttributesInMtrrSettings", UnitTestMtrrSetMemoryAttribut= esInMtrrSettings, InitializeMtrrRegs, NULL, &mSystemParameters[SystemIndex]= );=0D + }=0D + }=0D + //=0D + // Execute the tests.=0D + //=0D + srand ((unsigned int) time (NULL));=0D + Status =3D RunAllTestSuites (Framework);=0D +=0D +EXIT:=0D + if (Framework !=3D NULL) {=0D + FreeUnitTestFramework (Framework);=0D + }=0D +=0D + return Status;=0D +}=0D +=0D +/**=0D + Standard POSIX C entry point for host based unit test execution.=0D +=0D + @param Argc Number of arguments.=0D + @param Argv Array of arguments.=0D +=0D + @return Test application exit code.=0D +**/=0D +INT32=0D +main (=0D + INT32 Argc,=0D + CHAR8 *Argv[]=0D + )=0D +{=0D + UINTN Iteration;=0D +=0D + //=0D + // First parameter specifies the test iterations.=0D + // Default is 10.=0D + //=0D + Iteration =3D 10;=0D + if (Argc =3D=3D 2) {=0D + Iteration =3D atoi (Argv[1]);=0D + }=0D + return UnitTestingEntry (Iteration);=0D +}=0D diff --git a/UefiCpuPkg/Library/MtrrLib/UnitTest/MtrrLibUnitTest.h b/UefiCp= uPkg/Library/MtrrLib/UnitTest/MtrrLibUnitTest.h new file mode 100644 index 0000000000..25d4269589 --- /dev/null +++ b/UefiCpuPkg/Library/MtrrLib/UnitTest/MtrrLibUnitTest.h @@ -0,0 +1,171 @@ +/** @file=0D +=0D + Copyright (c) 2020, Intel Corporation. All rights reserved.
=0D + SPDX-License-Identifier: BSD-2-Clause-Patent=0D +=0D +**/=0D +=0D +#ifndef _MTRR_SUPPORT_H_=0D +#define _MTRR_SUPPORT_H_=0D +=0D +#include =0D +#include =0D +#include =0D +#include =0D +#include =0D +#include =0D +#include =0D +#include =0D +=0D +#include =0D +#include =0D +#include =0D +#include =0D +#include =0D +#include =0D +#include =0D +=0D +#include =0D +#include =0D +#include =0D +=0D +#define UNIT_TEST_APP_NAME "MtrrLib Unit Tests"=0D +#define UNIT_TEST_APP_VERSION "1.0"=0D +=0D +#define SCRATCH_BUFFER_SIZE SIZE_16KB=0D +=0D +typedef struct {=0D + UINT8 PhysicalAddressBits;=0D + BOOLEAN MtrrSupported;=0D + BOOLEAN FixedMtrrSupported;=0D + MTRR_MEMORY_CACHE_TYPE DefaultCacheType;=0D + UINT32 VariableMtrrCount;=0D +} MTRR_LIB_SYSTEM_PARAMETER;=0D +=0D +extern UINT32 mFixedMtrrsIndex[];=0D +=0D +/**=0D + Initialize the MTRR registers.=0D +=0D + @param SystemParameter System parameter that controls the MTRR registers= initialization.=0D +**/=0D +UNIT_TEST_STATUS=0D +EFIAPI=0D +InitializeMtrrRegs (=0D + IN MTRR_LIB_SYSTEM_PARAMETER *SystemParameter=0D + );=0D +=0D +/**=0D + Return a random memory cache type.=0D +**/=0D +MTRR_MEMORY_CACHE_TYPE=0D +GenerateRandomCacheType (=0D + VOID=0D + );=0D +=0D +/**=0D + Generate random MTRRs.=0D +=0D + @param PhysicalAddressBits Physical address bits.=0D + @param RawMemoryRanges Return the randomly generated MTRRs.=0D + @param UcCount Count of Uncacheable MTRRs.=0D + @param WtCount Count of Write Through MTRRs.=0D + @param WbCount Count of Write Back MTRRs.=0D + @param WpCount Count of Write Protected MTRRs.=0D + @param WcCount Count of Write Combining MTRRs.=0D +**/=0D +VOID=0D +GenerateValidAndConfigurableMtrrPairs (=0D + IN UINT32 PhysicalAddressBits,=0D + IN OUT MTRR_MEMORY_RANGE *RawMemoryRanges,=0D + IN UINT32 UcCount,=0D + IN UINT32 WtCount,=0D + IN UINT32 WbCount,=0D + IN UINT32 WpCount,=0D + IN UINT32 WcCount=0D + );=0D +=0D +/**=0D + Convert the MTRR BASE/MASK array to memory ranges.=0D +=0D + @param DefaultType Default memory type.=0D + @param PhysicalAddressBits Physical address bits.=0D + @param RawMemoryRanges Raw memory ranges.=0D + @param RawMemoryRangeCount Count of raw memory ranges.=0D + @param MemoryRanges Memory ranges.=0D + @param MemoryRangeCount Count of memory ranges.=0D +**/=0D +VOID=0D +GetEffectiveMemoryRanges (=0D + IN MTRR_MEMORY_CACHE_TYPE DefaultType,=0D + IN UINT32 PhysicalAddressBits,=0D + IN MTRR_MEMORY_RANGE *RawMemoryRanges,=0D + IN UINT32 RawMemoryRangeCount,=0D + OUT MTRR_MEMORY_RANGE *MemoryRanges,=0D + OUT UINTN *MemoryRangeCount=0D + );=0D +=0D +/**=0D + Generate random MTRR BASE/MASK for a specified type.=0D +=0D + @param PhysicalAddressBits Physical address bits.=0D + @param CacheType Cache type.=0D + @param MtrrPair Return the random MTRR.=0D + @param MtrrMemoryRange Return the random memory range.=0D +**/=0D +VOID=0D +GenerateRandomMtrrPair (=0D + IN UINT32 PhysicalAddressBits,=0D + IN MTRR_MEMORY_CACHE_TYPE CacheType,=0D + OUT MTRR_VARIABLE_SETTING *MtrrPair, OPTIONAL=0D + OUT MTRR_MEMORY_RANGE *MtrrMemoryRange OPTIONAL=0D + );=0D +=0D +/**=0D + Collect the test result.=0D +=0D + @param DefaultType Default memory type.=0D + @param PhysicalAddressBits Physical address bits.=0D + @param VariableMtrrCount Count of variable MTRRs.=0D + @param Mtrrs MTRR settings to collect from.=0D + @param Ranges Return the memory ranges.=0D + @param RangeCount Return the count of memory ranges.=0D + @param MtrrCount Return the count of variable MTRRs being use= d.=0D +**/=0D +VOID=0D +CollectTestResult (=0D + IN MTRR_MEMORY_CACHE_TYPE DefaultType,=0D + IN UINT32 PhysicalAddressBits,=0D + IN UINT32 VariableMtrrCount,=0D + IN MTRR_SETTINGS *Mtrrs,=0D + OUT MTRR_MEMORY_RANGE *Ranges,=0D + IN OUT UINTN *RangeCount,=0D + OUT UINT32 *MtrrCount=0D + );=0D +=0D +/**=0D + Return a 64bit random number.=0D +=0D + @param Start Start of the random number range.=0D + @param Limit Limit of the random number range.=0D + @return 64bit random number=0D +**/=0D +UINT64=0D +Random64 (=0D + UINT64 Start,=0D + UINT64 Limit=0D + );=0D +=0D +/**=0D + Return a 32bit random number.=0D +=0D + @param Start Start of the random number range.=0D + @param Limit Limit of the random number range.=0D + @return 32bit random number=0D +**/=0D +UINT32=0D +Random32 (=0D + UINT32 Start,=0D + UINT32 Limit=0D + );=0D +#endif=0D diff --git a/UefiCpuPkg/Library/MtrrLib/UnitTest/MtrrLibUnitTestHost.inf b/= UefiCpuPkg/Library/MtrrLib/UnitTest/MtrrLibUnitTestHost.inf new file mode 100644 index 0000000000..447238dc81 --- /dev/null +++ b/UefiCpuPkg/Library/MtrrLib/UnitTest/MtrrLibUnitTestHost.inf @@ -0,0 +1,39 @@ +## @file=0D +# Unit tests of the MtrrLib instance of the MtrrLib class=0D +#=0D +# Copyright (c) 2020, Intel Corporation. All rights reserved.
=0D +# SPDX-License-Identifier: BSD-2-Clause-Patent=0D +##=0D +=0D +[Defines]=0D + INF_VERSION =3D 0x00010006=0D + BASE_NAME =3D MtrrLibUnitTestHost=0D + FILE_GUID =3D A1542D84-B64D-4847-885E-0509084376AB= =0D + MODULE_TYPE =3D HOST_APPLICATION=0D + VERSION_STRING =3D 1.0=0D +=0D +#=0D +# The following information is for reference only and not required by the = build tools.=0D +#=0D +# VALID_ARCHITECTURES =3D IA32 X64=0D +#=0D +=0D +[Sources]=0D + MtrrLibUnitTest.c=0D + MtrrLibUnitTest.h=0D + Support.c=0D +=0D +[Packages]=0D + MdePkg/MdePkg.dec=0D + UefiCpuPkg/UefiCpuPkg.dec=0D + UnitTestFrameworkPkg/UnitTestFrameworkPkg.dec=0D +=0D +[LibraryClasses]=0D + BaseLib=0D + BaseMemoryLib=0D + DebugLib=0D + MtrrLib=0D + UnitTestLib=0D +=0D +[Pcd]=0D + gUefiCpuPkgTokenSpaceGuid.PcdCpuNumberOfReservedVariableMtrrs ## SOMET= IMES_CONSUMES=0D diff --git a/UefiCpuPkg/Library/MtrrLib/UnitTest/Support.c b/UefiCpuPkg/Lib= rary/MtrrLib/UnitTest/Support.c new file mode 100644 index 0000000000..9fe4b0278e --- /dev/null +++ b/UefiCpuPkg/Library/MtrrLib/UnitTest/Support.c @@ -0,0 +1,913 @@ +/** @file=0D + Unit tests of the MtrrLib instance of the MtrrLib class=0D +=0D + Copyright (c) 2018 - 2020, Intel Corporation. All rights reserved.
=0D + SPDX-License-Identifier: BSD-2-Clause-Patent=0D +=0D +**/=0D +=0D +#include "MtrrLibUnitTest.h"=0D +=0D +MTRR_MEMORY_CACHE_TYPE mMemoryCacheTypes[] =3D {=0D + CacheUncacheable, CacheWriteCombining, CacheWriteThrough, CacheWriteProt= ected, CacheWriteBack=0D + };=0D +=0D +UINT64 mFixedMtrrsValue[MTRR_NUMBER_OF_FIXED_MTR= R];=0D +MSR_IA32_MTRR_PHYSBASE_REGISTER mVariableMtrrsPhysBase[MTRR_NUMBER_OF_VAR= IABLE_MTRR];=0D +MSR_IA32_MTRR_PHYSMASK_REGISTER mVariableMtrrsPhysMask[MTRR_NUMBER_OF_VAR= IABLE_MTRR];=0D +MSR_IA32_MTRR_DEF_TYPE_REGISTER mDefTypeMsr;=0D +MSR_IA32_MTRRCAP_REGISTER mMtrrCapMsr;=0D +CPUID_VERSION_INFO_EDX mCpuidVersionInfoEdx;=0D +CPUID_VIR_PHY_ADDRESS_SIZE_EAX mCpuidVirPhyAddressSizeEax;=0D +=0D +/**=0D + Retrieves CPUID information.=0D +=0D + Executes the CPUID instruction with EAX set to the value specified by In= dex.=0D + This function always returns Index.=0D + If Eax is not NULL, then the value of EAX after CPUID is returned in Eax= .=0D + If Ebx is not NULL, then the value of EBX after CPUID is returned in Ebx= .=0D + If Ecx is not NULL, then the value of ECX after CPUID is returned in Ecx= .=0D + If Edx is not NULL, then the value of EDX after CPUID is returned in Edx= .=0D + This function is only available on IA-32 and x64.=0D +=0D + @param Index The 32-bit value to load into EAX prior to invoking the CP= UID=0D + instruction.=0D + @param Eax The pointer to the 32-bit EAX value returned by the CPUID= =0D + instruction. This is an optional parameter that may be NUL= L.=0D + @param Ebx The pointer to the 32-bit EBX value returned by the CPUID= =0D + instruction. This is an optional parameter that may be NUL= L.=0D + @param Ecx The pointer to the 32-bit ECX value returned by the CPUID= =0D + instruction. This is an optional parameter that may be NUL= L.=0D + @param Edx The pointer to the 32-bit EDX value returned by the CPUID= =0D + instruction. This is an optional parameter that may be NUL= L.=0D +=0D + @return Index.=0D +=0D +**/=0D +UINT32=0D +EFIAPI=0D +UnitTestMtrrLibAsmCpuid (=0D + IN UINT32 Index,=0D + OUT UINT32 *Eax, OPTIONAL=0D + OUT UINT32 *Ebx, OPTIONAL=0D + OUT UINT32 *Ecx, OPTIONAL=0D + OUT UINT32 *Edx OPTIONAL=0D + )=0D +{=0D + switch (Index) {=0D + case CPUID_VERSION_INFO:=0D + if (Edx !=3D NULL) {=0D + *Edx =3D mCpuidVersionInfoEdx.Uint32;=0D + }=0D + return Index;=0D + break;=0D + case CPUID_EXTENDED_FUNCTION:=0D + if (Eax !=3D NULL) {=0D + *Eax =3D CPUID_VIR_PHY_ADDRESS_SIZE;=0D + }=0D + return Index;=0D + break;=0D + case CPUID_VIR_PHY_ADDRESS_SIZE:=0D + if (Eax !=3D NULL) {=0D + *Eax =3D mCpuidVirPhyAddressSizeEax.Uint32;=0D + }=0D + return Index;=0D + break;=0D + }=0D +=0D + //=0D + // Should never fall through to here=0D + //=0D + ASSERT(FALSE);=0D + return Index;=0D +}=0D +=0D +/**=0D + Returns a 64-bit Machine Specific Register(MSR).=0D +=0D + Reads and returns the 64-bit MSR specified by Index. No parameter checki= ng is=0D + performed on Index, and some Index values may cause CPU exceptions. The= =0D + caller must either guarantee that Index is valid, or the caller must set= up=0D + exception handlers to catch the exceptions. This function is only availa= ble=0D + on IA-32 and x64.=0D +=0D + @param MsrIndex The 32-bit MSR index to read.=0D +=0D + @return The value of the MSR identified by MsrIndex.=0D +=0D +**/=0D +UINT64=0D +EFIAPI=0D +UnitTestMtrrLibAsmReadMsr64(=0D + IN UINT32 MsrIndex=0D + )=0D +{=0D + UINT32 Index;=0D +=0D + for (Index =3D 0; Index < ARRAY_SIZE (mFixedMtrrsValue); Index++) {=0D + if (MsrIndex =3D=3D mFixedMtrrsIndex[Index]) {=0D + return mFixedMtrrsValue[Index];=0D + }=0D + }=0D +=0D + if ((MsrIndex >=3D MSR_IA32_MTRR_PHYSBASE0) &&=0D + (MsrIndex <=3D MSR_IA32_MTRR_PHYSMASK0 + (MTRR_NUMBER_OF_VARIABLE_MT= RR << 1))) {=0D + if (MsrIndex % 2 =3D=3D 0) {=0D + Index =3D (MsrIndex - MSR_IA32_MTRR_PHYSBASE0) >> 1;=0D + return mVariableMtrrsPhysBase[Index].Uint64;=0D + } else {=0D + Index =3D (MsrIndex - MSR_IA32_MTRR_PHYSMASK0) >> 1;=0D + return mVariableMtrrsPhysMask[Index].Uint64;=0D + }=0D + }=0D +=0D + if (MsrIndex =3D=3D MSR_IA32_MTRR_DEF_TYPE) {=0D + return mDefTypeMsr.Uint64;=0D + }=0D +=0D + if (MsrIndex =3D=3D MSR_IA32_MTRRCAP) {=0D + return mMtrrCapMsr.Uint64;=0D + }=0D +=0D + //=0D + // Should never fall through to here=0D + //=0D + ASSERT(FALSE);=0D + return 0;=0D +}=0D +=0D +/**=0D + Writes a 64-bit value to a Machine Specific Register(MSR), and returns t= he=0D + value.=0D +=0D + Writes the 64-bit value specified by Value to the MSR specified by Index= . The=0D + 64-bit value written to the MSR is returned. No parameter checking is=0D + performed on Index or Value, and some of these may cause CPU exceptions.= The=0D + caller must either guarantee that Index and Value are valid, or the call= er=0D + must establish proper exception handlers. This function is only availabl= e on=0D + IA-32 and x64.=0D +=0D + @param MsrIndex The 32-bit MSR index to write.=0D + @param Value The 64-bit value to write to the MSR.=0D +=0D + @return Value=0D +=0D +**/=0D +UINT64=0D +EFIAPI=0D +UnitTestMtrrLibAsmWriteMsr64(=0D + IN UINT32 MsrIndex,=0D + IN UINT64 Value=0D + )=0D +{=0D + UINT32 Index;=0D +=0D + for (Index =3D 0; Index < ARRAY_SIZE (mFixedMtrrsValue); Index++) {=0D + if (MsrIndex =3D=3D mFixedMtrrsIndex[Index]) {=0D + mFixedMtrrsValue[Index] =3D Value;=0D + return Value;=0D + }=0D + }=0D +=0D + if ((MsrIndex >=3D MSR_IA32_MTRR_PHYSBASE0) &&=0D + (MsrIndex <=3D MSR_IA32_MTRR_PHYSMASK0 + (MTRR_NUMBER_OF_VARIABLE_MT= RR << 1))) {=0D + if (MsrIndex % 2 =3D=3D 0) {=0D + Index =3D (MsrIndex - MSR_IA32_MTRR_PHYSBASE0) >> 1;=0D + mVariableMtrrsPhysBase[Index].Uint64 =3D Value;=0D + return Value;=0D + } else {=0D + Index =3D (MsrIndex - MSR_IA32_MTRR_PHYSMASK0) >> 1;=0D + mVariableMtrrsPhysMask[Index].Uint64 =3D Value;=0D + return Value;=0D + }=0D + }=0D +=0D + if (MsrIndex =3D=3D MSR_IA32_MTRR_DEF_TYPE) {=0D + mDefTypeMsr.Uint64 =3D Value;=0D + return Value;=0D + }=0D +=0D + if (MsrIndex =3D=3D MSR_IA32_MTRRCAP) {=0D + mMtrrCapMsr.Uint64 =3D Value;=0D + return Value;=0D + }=0D +=0D + //=0D + // Should never fall through to here=0D + //=0D + ASSERT(FALSE);=0D + return 0;=0D +}=0D +=0D +/**=0D + Initialize MTRR registers.=0D +**/=0D +=0D +/**=0D + Initialize the MTRR registers.=0D +=0D + @param SystemParameter System parameter that controls the MTRR registers= initialization.=0D +**/=0D +UNIT_TEST_STATUS=0D +EFIAPI=0D +InitializeMtrrRegs (=0D + IN MTRR_LIB_SYSTEM_PARAMETER *SystemParameter=0D + )=0D +{=0D + UINT32 Index;=0D +=0D + SetMem (mFixedMtrrsValue, sizeof (mFixedMtrrsValue), SystemParameter->De= faultCacheType);=0D +=0D + for (Index =3D 0; Index < ARRAY_SIZE (mVariableMtrrsPhysBase); Index++) = {=0D + mVariableMtrrsPhysBase[Index].Uint64 =3D 0;=0D + mVariableMtrrsPhysBase[Index].Bits.Type =3D SystemParameter->Defa= ultCacheType;=0D + mVariableMtrrsPhysBase[Index].Bits.Reserved1 =3D 0;=0D +=0D + mVariableMtrrsPhysMask[Index].Uint64 =3D 0;=0D + mVariableMtrrsPhysMask[Index].Bits.V =3D 0;=0D + mVariableMtrrsPhysMask[Index].Bits.Reserved1 =3D 0;=0D + }=0D +=0D + mDefTypeMsr.Bits.E =3D 1;=0D + mDefTypeMsr.Bits.FE =3D 1;=0D + mDefTypeMsr.Bits.Type =3D SystemParameter->DefaultCacheType;=0D + mDefTypeMsr.Bits.Reserved1 =3D 0;=0D + mDefTypeMsr.Bits.Reserved2 =3D 0;=0D + mDefTypeMsr.Bits.Reserved3 =3D 0;=0D +=0D + mMtrrCapMsr.Bits.SMRR =3D 0;=0D + mMtrrCapMsr.Bits.WC =3D 0;=0D + mMtrrCapMsr.Bits.VCNT =3D SystemParameter->VariableMtrrCount;=0D + mMtrrCapMsr.Bits.FIX =3D SystemParameter->FixedMtrrSupported;=0D + mMtrrCapMsr.Bits.Reserved1 =3D 0;=0D + mMtrrCapMsr.Bits.Reserved2 =3D 0;=0D + mMtrrCapMsr.Bits.Reserved3 =3D 0;=0D +=0D + mCpuidVersionInfoEdx.Bits.MTRR =3D SystemParameter-= >MtrrSupported;=0D + mCpuidVirPhyAddressSizeEax.Bits.PhysicalAddressBits =3D SystemParameter-= >PhysicalAddressBits;=0D +=0D + //=0D + // Hook BaseLib functions used by MtrrLib that require some emulation.=0D + //=0D + gUnitTestHostBaseLib.X86->AsmCpuid =3D UnitTestMtrrLibAsmCpuid;=0D + gUnitTestHostBaseLib.X86->AsmReadMsr64 =3D UnitTestMtrrLibAsmReadMsr64;= =0D + gUnitTestHostBaseLib.X86->AsmWriteMsr64 =3D UnitTestMtrrLibAsmWriteMsr64= ;=0D +=0D + return UNIT_TEST_PASSED;=0D +}=0D +=0D +/**=0D + Collect the test result.=0D +=0D + @param DefaultType Default memory type.=0D + @param PhysicalAddressBits Physical address bits.=0D + @param VariableMtrrCount Count of variable MTRRs.=0D + @param Mtrrs MTRR settings to collect from.=0D + @param Ranges Return the memory ranges.=0D + @param RangeCount Return the count of memory ranges.=0D + @param MtrrCount Return the count of variable MTRRs being use= d.=0D +**/=0D +VOID=0D +CollectTestResult (=0D + IN MTRR_MEMORY_CACHE_TYPE DefaultType,=0D + IN UINT32 PhysicalAddressBits,=0D + IN UINT32 VariableMtrrCount,=0D + IN MTRR_SETTINGS *Mtrrs,=0D + OUT MTRR_MEMORY_RANGE *Ranges,=0D + IN OUT UINTN *RangeCount,=0D + OUT UINT32 *MtrrCount=0D + )=0D +{=0D + UINTN Index;=0D + UINT64 MtrrValidBitsMask;=0D + UINT64 MtrrValidAddressMask;=0D + MTRR_MEMORY_RANGE RawMemoryRanges[ARRAY_SIZE (Mtrrs->Variables.Mtrr)];=0D +=0D + ASSERT (Mtrrs !=3D NULL);=0D + ASSERT (VariableMtrrCount <=3D ARRAY_SIZE (Mtrrs->Variables.Mtrr));=0D +=0D + MtrrValidBitsMask =3D (1ull << PhysicalAddressBits) - 1;=0D + MtrrValidAddressMask =3D MtrrValidBitsMask & ~0xFFFull;=0D +=0D + *MtrrCount =3D 0;=0D + for (Index =3D 0; Index < VariableMtrrCount; Index++) {=0D + if (((MSR_IA32_MTRR_PHYSMASK_REGISTER *) &Mtrrs->Variables.Mtrr[Index]= .Mask)->Bits.V =3D=3D 1) {=0D + RawMemoryRanges[*MtrrCount].BaseAddress =3D Mtrrs->Variables.Mtrr[In= dex].Base & MtrrValidAddressMask;=0D + RawMemoryRanges[*MtrrCount].Type =3D=0D + ((MSR_IA32_MTRR_PHYSBASE_REGISTER *) &Mtrrs->Variables.Mtrr[Index]= .Base)->Bits.Type;=0D + RawMemoryRanges[*MtrrCount].Length =3D=0D + ((~(Mtrrs->Variables.Mtrr[Index].Mask & MtrrValidAddressMask)) &= MtrrValidBitsMask) + 1;=0D + (*MtrrCount)++;=0D + }=0D + }=0D +=0D + GetEffectiveMemoryRanges (DefaultType, PhysicalAddressBits, RawMemoryRan= ges, *MtrrCount, Ranges, RangeCount);=0D +}=0D +=0D +/**=0D + Return a 32bit random number.=0D +=0D + @param Start Start of the random number range.=0D + @param Limit Limit of the random number range.=0D + @return 32bit random number=0D +**/=0D +UINT32=0D +Random32 (=0D + UINT32 Start,=0D + UINT32 Limit=0D + )=0D +{=0D + return (UINT32) (((double) rand () / RAND_MAX) * (Limit - Start)) + Star= t;=0D +}=0D +=0D +/**=0D + Return a 64bit random number.=0D +=0D + @param Start Start of the random number range.=0D + @param Limit Limit of the random number range.=0D + @return 64bit random number=0D +**/=0D +UINT64=0D +Random64 (=0D + UINT64 Start,=0D + UINT64 Limit=0D + )=0D +{=0D + return (UINT64) (((double) rand () / RAND_MAX) * (Limit - Start)) + Star= t;=0D +}=0D +=0D +/**=0D + Generate random MTRR BASE/MASK for a specified type.=0D +=0D + @param PhysicalAddressBits Physical address bits.=0D + @param CacheType Cache type.=0D + @param MtrrPair Return the random MTRR.=0D + @param MtrrMemoryRange Return the random memory range.=0D +**/=0D +VOID=0D +GenerateRandomMtrrPair (=0D + IN UINT32 PhysicalAddressBits,=0D + IN MTRR_MEMORY_CACHE_TYPE CacheType,=0D + OUT MTRR_VARIABLE_SETTING *MtrrPair, OPTIONAL=0D + OUT MTRR_MEMORY_RANGE *MtrrMemoryRange OPTIONAL=0D + )=0D +{=0D + MSR_IA32_MTRR_PHYSBASE_REGISTER PhysBase;=0D + MSR_IA32_MTRR_PHYSMASK_REGISTER PhysMask;=0D + UINT32 SizeShift;=0D + UINT32 BaseShift;=0D + UINT64 RandomBoundary;=0D + UINT64 MaxPhysicalAddress;=0D + UINT64 RangeSize;=0D + UINT64 RangeBase;=0D + UINT64 PhysBasePhyMaskValidBitsMask;=0D +=0D + MaxPhysicalAddress =3D 1ull << PhysicalAddressBits;=0D + do {=0D + SizeShift =3D Random32 (12, PhysicalAddressBits - 1);=0D + RangeSize =3D 1ull << SizeShift;=0D +=0D + BaseShift =3D Random32 (SizeShift, PhysicalAddressBits - 1);=0D + RandomBoundary =3D Random64 (0, 1ull << (PhysicalAddressBits - BaseShi= ft));=0D + RangeBase =3D RandomBoundary << BaseShift;=0D + } while (RangeBase < SIZE_1MB || RangeBase > MaxPhysicalAddress - 1);=0D +=0D + PhysBasePhyMaskValidBitsMask =3D (MaxPhysicalAddress - 1) & 0xffffffffff= fff000ULL;=0D +=0D + PhysBase.Uint64 =3D 0;=0D + PhysBase.Bits.Type =3D CacheType;=0D + PhysBase.Uint64 |=3D RangeBase & PhysBasePhyMaskValidBitsMask;=0D + PhysMask.Uint64 =3D 0;=0D + PhysMask.Bits.V =3D 1;=0D + PhysMask.Uint64 |=3D ((~RangeSize) + 1) & PhysBasePhyMaskValidBitsMask= ;=0D +=0D + if (MtrrPair !=3D NULL) {=0D + MtrrPair->Base =3D PhysBase.Uint64;=0D + MtrrPair->Mask =3D PhysMask.Uint64;=0D + }=0D +=0D + if (MtrrMemoryRange !=3D NULL) {=0D + MtrrMemoryRange->BaseAddress =3D RangeBase;=0D + MtrrMemoryRange->Length =3D RangeSize;=0D + MtrrMemoryRange->Type =3D CacheType;=0D + }=0D +}=0D +=0D +=0D +/**=0D + Check whether the Range overlaps with any one in Ranges.=0D +=0D + @param Range The memory range to check.=0D + @param Ranges The memory ranges.=0D + @param Count Count of memory ranges.=0D +=0D + @return TRUE when overlap exists.=0D +**/=0D +BOOLEAN=0D +RangesOverlap (=0D + IN MTRR_MEMORY_RANGE *Range,=0D + IN MTRR_MEMORY_RANGE *Ranges,=0D + IN UINTN Count=0D + )=0D +{=0D + while (Count-- !=3D 0) {=0D + //=0D + // Two ranges overlap when:=0D + // 1. range#2.base is in the middle of range#1=0D + // 2. range#1.base is in the middle of range#2=0D + //=0D + if ((Range->BaseAddress <=3D Ranges[Count].BaseAddress && Ranges[Count= ].BaseAddress < Range->BaseAddress + Range->Length)=0D + || (Ranges[Count].BaseAddress <=3D Range->BaseAddress && Range->BaseA= ddress < Ranges[Count].BaseAddress + Ranges[Count].Length)) {=0D + return TRUE;=0D + }=0D + }=0D + return FALSE;=0D +}=0D +=0D +/**=0D + Generate random MTRRs.=0D +=0D + @param PhysicalAddressBits Physical address bits.=0D + @param RawMemoryRanges Return the randomly generated MTRRs.=0D + @param UcCount Count of Uncacheable MTRRs.=0D + @param WtCount Count of Write Through MTRRs.=0D + @param WbCount Count of Write Back MTRRs.=0D + @param WpCount Count of Write Protected MTRRs.=0D + @param WcCount Count of Write Combine MTRRs.=0D +**/=0D +VOID=0D +GenerateValidAndConfigurableMtrrPairs (=0D + IN UINT32 PhysicalAddressBits,=0D + IN OUT MTRR_MEMORY_RANGE *RawMemoryRanges,=0D + IN UINT32 UcCount,=0D + IN UINT32 WtCount,=0D + IN UINT32 WbCount,=0D + IN UINT32 WpCount,=0D + IN UINT32 WcCount=0D + )=0D +{=0D + UINT32 Index;=0D +=0D + //=0D + // 1. Generate UC, WT, WB in order.=0D + //=0D + for (Index =3D 0; Index < UcCount; Index++) {=0D + GenerateRandomMtrrPair (PhysicalAddressBits, CacheUncacheable, NULL, &= RawMemoryRanges[Index]);=0D + }=0D +=0D + for (Index =3D UcCount; Index < UcCount + WtCount; Index++) {=0D + GenerateRandomMtrrPair (PhysicalAddressBits, CacheWriteThrough, NULL, = &RawMemoryRanges[Index]);=0D + }=0D +=0D + for (Index =3D UcCount + WtCount; Index < UcCount + WtCount + WbCount; I= ndex++) {=0D + GenerateRandomMtrrPair (PhysicalAddressBits, CacheWriteBack, NULL, &Ra= wMemoryRanges[Index]);=0D + }=0D +=0D + //=0D + // 2. Generate WP MTRR and DO NOT overlap with WT, WB.=0D + //=0D + for (Index =3D UcCount + WtCount + WbCount; Index < UcCount + WtCount + = WbCount + WpCount; Index++) {=0D + GenerateRandomMtrrPair (PhysicalAddressBits, CacheWriteProtected, NULL= , &RawMemoryRanges[Index]);=0D + while (RangesOverlap (&RawMemoryRanges[Index], &RawMemoryRanges[UcCoun= t], WtCount + WbCount)) {=0D + GenerateRandomMtrrPair (PhysicalAddressBits, CacheWriteProtected, NU= LL, &RawMemoryRanges[Index]);=0D + }=0D + }=0D +=0D + //=0D + // 3. Generate WC MTRR and DO NOT overlap with WT, WB, WP.=0D + //=0D + for (Index =3D UcCount + WtCount + WbCount + WpCount; Index < UcCount + = WtCount + WbCount + WpCount + WcCount; Index++) {=0D + GenerateRandomMtrrPair (PhysicalAddressBits, CacheWriteCombining, NULL= , &RawMemoryRanges[Index]);=0D + while (RangesOverlap (&RawMemoryRanges[Index], &RawMemoryRanges[UcCoun= t], WtCount + WbCount + WpCount)) {=0D + GenerateRandomMtrrPair (PhysicalAddressBits, CacheWriteCombining, NU= LL, &RawMemoryRanges[Index]);=0D + }=0D + }=0D +}=0D +=0D +/**=0D + Return a random memory cache type.=0D +**/=0D +MTRR_MEMORY_CACHE_TYPE=0D +GenerateRandomCacheType (=0D + VOID=0D + )=0D +{=0D + return mMemoryCacheTypes[Random32 (0, ARRAY_SIZE (mMemoryCacheTypes) -= 1)];=0D +}=0D +=0D +/**=0D + Compare function used by qsort().=0D +**/=0D +=0D +/**=0D + Compare function used by qsort().=0D +=0D + @param Left Left operand to compare.=0D + @param Right Right operand to compare.=0D +=0D + @retval 0 Left =3D=3D Right=0D + @retval -1 Left < Right=0D + @retval 1 Left > Right=0D +**/=0D +INT32=0D +CompareFuncUint64 (=0D + CONST VOID * Left,=0D + CONST VOID * Right=0D + )=0D +{=0D + INT64 Delta;=0D + Delta =3D (*(UINT64*)Left - *(UINT64*)Right);=0D + if (Delta > 0) {=0D + return 1;=0D + } else if (Delta =3D=3D 0) {=0D + return 0;=0D + } else {=0D + return -1;=0D + }=0D +}=0D +=0D +/**=0D + Determin the memory cache type for the Range.=0D +=0D + @param DefaultType Default cache type.=0D + @param Range The memory range to determin the cache type.=0D + @param Ranges The entire memory ranges.=0D + @param RangeCount Count of the entire memory ranges.=0D +**/=0D +VOID=0D +DetermineMemoryCacheType (=0D + IN MTRR_MEMORY_CACHE_TYPE DefaultType,=0D + IN OUT MTRR_MEMORY_RANGE *Range,=0D + IN MTRR_MEMORY_RANGE *Ranges,=0D + IN UINT32 RangeCount=0D + )=0D +{=0D + UINT32 Index;=0D + Range->Type =3D CacheInvalid;=0D + for (Index =3D 0; Index < RangeCount; Index++) {=0D + if (RangesOverlap (Range, &Ranges[Index], 1)) {=0D + if (Ranges[Index].Type < Range->Type) {=0D + Range->Type =3D Ranges[Index].Type;=0D + }=0D + }=0D + }=0D +=0D + if (Range->Type =3D=3D CacheInvalid) {=0D + Range->Type =3D DefaultType;=0D + }=0D +}=0D +=0D +/**=0D + Get the index of the element that does NOT equals to Array[Index].=0D +=0D + @param Index Current element.=0D + @param Array Array to scan.=0D + @param Count Count of the array.=0D +=0D + @return Next element that doesn't equal to current one.=0D +**/=0D +UINT32=0D +GetNextDifferentElementInSortedArray (=0D + IN UINT32 Index,=0D + IN UINT64 *Array,=0D + IN UINT32 Count=0D + )=0D +{=0D + UINT64 CurrentElement;=0D + CurrentElement =3D Array[Index];=0D + while (CurrentElement =3D=3D Array[Index] && Index < Count) {=0D + Index++;=0D + }=0D + return Index;=0D +}=0D +=0D +/**=0D + Remove the duplicates from the array.=0D +=0D + @param Array The array to operate on.=0D + @param Count Count of the array.=0D +**/=0D +VOID=0D +RemoveDuplicatesInSortedArray (=0D + IN OUT UINT64 *Array,=0D + IN OUT UINT32 *Count=0D + )=0D +{=0D + UINT32 Index;=0D + UINT32 NewCount;=0D +=0D + Index =3D 0;=0D + NewCount =3D 0;=0D + while (Index < *Count) {=0D + Array[NewCount] =3D Array[Index];=0D + NewCount++;=0D + Index =3D GetNextDifferentElementInSortedArray (Index, Array, *Count);= =0D + }=0D + *Count =3D NewCount;=0D +}=0D +=0D +/**=0D + Return TRUE when Address is in the Range.=0D +=0D + @param Address The address to check.=0D + @param Range The range to check.=0D + @return TRUE when Address is in the Range.=0D +**/=0D +BOOLEAN=0D +AddressInRange (=0D + IN UINT64 Address,=0D + IN MTRR_MEMORY_RANGE Range=0D + )=0D +{=0D + return (Address >=3D Range.BaseAddress) && (Address <=3D Range.BaseAdd= ress + Range.Length - 1);=0D +}=0D +=0D +/**=0D + Get the overlap bit flag.=0D +=0D + @param RawMemoryRanges Raw memory ranges.=0D + @param RawMemoryRangeCount Count of raw memory ranges.=0D + @param Address The address to check.=0D +**/=0D +UINT64=0D +GetOverlapBitFlag (=0D + IN MTRR_MEMORY_RANGE *RawMemoryRanges,=0D + IN UINT32 RawMemoryRangeCount,=0D + IN UINT64 Address=0D + )=0D +{=0D + UINT64 OverlapBitFlag;=0D + UINT32 Index;=0D + OverlapBitFlag =3D 0;=0D + for (Index =3D 0; Index < RawMemoryRangeCount; Index++) {=0D + if (AddressInRange (Address, RawMemoryRanges[Index])) {=0D + OverlapBitFlag |=3D (1ull << Index);=0D + }=0D + }=0D +=0D + return OverlapBitFlag;=0D +}=0D +=0D +/**=0D + Return the relationship between flags.=0D +=0D + @param Flag1 Flag 1=0D + @param Flag2 Flag 2=0D +=0D + @retval 0 Flag1 =3D=3D Flag2=0D + @retval 1 Flag1 is a subset of Flag2=0D + @retval 2 Flag2 is a subset of Flag1=0D + @retval 3 No subset relations between Flag1 and Flag2.=0D +**/=0D +UINT32=0D +CheckOverlapBitFlagsRelation (=0D + IN UINT64 Flag1,=0D + IN UINT64 Flag2=0D + )=0D +{=0D + if (Flag1 =3D=3D Flag2) return 0;=0D + if ((Flag1 | Flag2) =3D=3D Flag2) return 1;=0D + if ((Flag1 | Flag2) =3D=3D Flag1) return 2;=0D + return 3;=0D +}=0D +=0D +/**=0D + Return TRUE when the Endpoint is in any of the Ranges.=0D +=0D + @param Endpoint The endpoint to check.=0D + @param Ranges The memory ranges.=0D + @param RangeCount Count of memory ranges.=0D +=0D + @retval TRUE Endpoint is in one of the range.=0D + @retval FALSE Endpoint is not in any of the ranges.=0D +**/=0D +BOOLEAN=0D +IsEndpointInRanges (=0D + IN UINT64 Endpoint,=0D + IN MTRR_MEMORY_RANGE *Ranges,=0D + IN UINTN RangeCount=0D + )=0D +{=0D + UINT32 Index;=0D + for (Index =3D 0; Index < RangeCount; Index++) {=0D + if (AddressInRange (Endpoint, Ranges[Index])) {=0D + return TRUE;=0D + }=0D + }=0D + return FALSE;=0D +}=0D +=0D +=0D +/**=0D + Compact adjacent ranges of the same type.=0D +=0D + @param DefaultType Default memory type.=0D + @param PhysicalAddressBits Physical address bits.=0D + @param EffectiveMtrrMemoryRanges Memory ranges to compact.=0D + @param EffectiveMtrrMemoryRangesCount Return the new count of memory ran= ges.=0D +**/=0D +VOID=0D +CompactAndExtendEffectiveMtrrMemoryRanges (=0D + IN MTRR_MEMORY_CACHE_TYPE DefaultType,=0D + IN UINT32 PhysicalAddressBits,=0D + IN OUT MTRR_MEMORY_RANGE **EffectiveMtrrMemoryRanges,=0D + IN OUT UINTN *EffectiveMtrrMemoryRangesCount=0D + )=0D +{=0D + UINT64 MaxAddress;=0D + UINTN NewRangesCountAtMost;=0D + MTRR_MEMORY_RANGE *NewRanges;=0D + UINTN NewRangesCountActual;=0D + MTRR_MEMORY_RANGE *CurrentRangeInNewRanges;=0D + MTRR_MEMORY_CACHE_TYPE CurrentRangeTypeInOldRanges;=0D +=0D + MTRR_MEMORY_RANGE *OldRanges;=0D + MTRR_MEMORY_RANGE OldLastRange;=0D + UINTN OldRangesIndex;=0D +=0D + NewRangesCountActual =3D 0;=0D + NewRangesCountAtMost =3D *EffectiveMtrrMemoryRangesCount + 2; // At mo= st with 2 more range entries.=0D + NewRanges =3D (MTRR_MEMORY_RANGE *) calloc (NewRangesCountAtM= ost, sizeof (MTRR_MEMORY_RANGE));=0D + OldRanges =3D *EffectiveMtrrMemoryRanges;=0D + if (OldRanges[0].BaseAddress > 0) {=0D + NewRanges[NewRangesCountActual].BaseAddress =3D 0;=0D + NewRanges[NewRangesCountActual].Length =3D OldRanges[0].BaseAddre= ss;=0D + NewRanges[NewRangesCountActual].Type =3D DefaultType;=0D + NewRangesCountActual++;=0D + }=0D +=0D + OldRangesIndex =3D 0;=0D + while (OldRangesIndex < *EffectiveMtrrMemoryRangesCount) {=0D + CurrentRangeTypeInOldRanges =3D OldRanges[OldRangesIndex].Type;=0D + CurrentRangeInNewRanges =3D NULL;=0D + if (NewRangesCountActual > 0) // We need to check CurrentNewRange fi= rst before generate a new NewRange.=0D + {=0D + CurrentRangeInNewRanges =3D &NewRanges[NewRangesCountActual - 1];=0D + }=0D + if (CurrentRangeInNewRanges !=3D NULL && CurrentRangeInNewRanges->Type= =3D=3D CurrentRangeTypeInOldRanges) {=0D + CurrentRangeInNewRanges->Length +=3D OldRanges[OldRangesIndex].Lengt= h;=0D + } else {=0D + NewRanges[NewRangesCountActual].BaseAddress =3D OldRanges[OldRangesI= ndex].BaseAddress;=0D + NewRanges[NewRangesCountActual].Length +=3D OldRanges[OldRangesI= ndex].Length;=0D + NewRanges[NewRangesCountActual].Type =3D CurrentRangeTypeInOl= dRanges;=0D + while (OldRangesIndex + 1 < *EffectiveMtrrMemoryRangesCount && OldRa= nges[OldRangesIndex + 1].Type =3D=3D CurrentRangeTypeInOldRanges)=0D + {=0D + OldRangesIndex++;=0D + NewRanges[NewRangesCountActual].Length +=3D OldRanges[OldRangesInd= ex].Length;=0D + }=0D + NewRangesCountActual++;=0D + }=0D +=0D + OldRangesIndex++;=0D + }=0D +=0D + MaxAddress =3D (1ull << PhysicalAddressBits) - 1;=0D + OldLastRange =3D OldRanges[(*EffectiveMtrrMemoryRangesCount) - 1];=0D + CurrentRangeInNewRanges =3D &NewRanges[NewRangesCountActual - 1];=0D + if (OldLastRange.BaseAddress + OldLastRange.Length - 1 < MaxAddress) {=0D + if (CurrentRangeInNewRanges->Type =3D=3D DefaultType) {=0D + CurrentRangeInNewRanges->Length =3D MaxAddress - CurrentRangeInNewRa= nges->BaseAddress + 1;=0D + } else {=0D + NewRanges[NewRangesCountActual].BaseAddress =3D OldLastRange.BaseAdd= ress + OldLastRange.Length;=0D + NewRanges[NewRangesCountActual].Length =3D MaxAddress - NewRanges[Ne= wRangesCountActual].BaseAddress + 1;=0D + NewRanges[NewRangesCountActual].Type =3D DefaultType;=0D + NewRangesCountActual++;=0D + }=0D + }=0D +=0D + free (*EffectiveMtrrMemoryRanges);=0D + *EffectiveMtrrMemoryRanges =3D NewRanges;=0D + *EffectiveMtrrMemoryRangesCount =3D NewRangesCountActual;=0D +}=0D +=0D +/**=0D + Collect all the endpoints in the raw memory ranges.=0D +=0D + @param Endpoints Return the collected endpoints.=0D + @param EndPointCount Return the count of endpoints.=0D + @param RawMemoryRanges Raw memory ranges.=0D + @param RawMemoryRangeCount Count of raw memory ranges.=0D +**/=0D +VOID=0D +CollectEndpoints (=0D + IN OUT UINT64 *Endpoints,=0D + IN OUT UINT32 *EndPointCount,=0D + IN MTRR_MEMORY_RANGE *RawMemoryRanges,=0D + IN UINT32 RawMemoryRangeCount=0D + )=0D +{=0D + UINT32 Index;=0D + UINT32 RawRangeIndex;=0D +=0D + ASSERT ((RawMemoryRangeCount << 1) =3D=3D *EndPointCount);=0D +=0D + for (Index =3D 0; Index < *EndPointCount; Index +=3D 2) {=0D + RawRangeIndex =3D Index >> 1;=0D + Endpoints[Index] =3D RawMemoryRanges[RawRangeIndex].BaseAddress;=0D + Endpoints[Index + 1] =3D RawMemoryRanges[RawRangeIndex].BaseAddress + = RawMemoryRanges[RawRangeIndex].Length - 1;=0D + }=0D +=0D + qsort (Endpoints, *EndPointCount, sizeof (UINT64), CompareFuncUint64);=0D + RemoveDuplicatesInSortedArray (Endpoints, EndPointCount);=0D +}=0D +=0D +/**=0D + Convert the MTRR BASE/MASK array to memory ranges.=0D +=0D + @param DefaultType Default memory type.=0D + @param PhysicalAddressBits Physical address bits.=0D + @param RawMemoryRanges Raw memory ranges.=0D + @param RawMemoryRangeCount Count of raw memory ranges.=0D + @param MemoryRanges Memory ranges.=0D + @param MemoryRangeCount Count of memory ranges.=0D +**/=0D +VOID=0D +GetEffectiveMemoryRanges (=0D + IN MTRR_MEMORY_CACHE_TYPE DefaultType,=0D + IN UINT32 PhysicalAddressBits,=0D + IN MTRR_MEMORY_RANGE *RawMemoryRanges,=0D + IN UINT32 RawMemoryRangeCount,=0D + OUT MTRR_MEMORY_RANGE *MemoryRanges,=0D + OUT UINTN *MemoryRangeCount=0D + )=0D +{=0D + UINTN Index;=0D + UINT32 AllEndPointsCount;=0D + UINT64 *AllEndPointsInclusive;=0D + UINT32 AllRangePiecesCountMax;=0D + MTRR_MEMORY_RANGE *AllRangePieces;=0D + UINTN AllRangePiecesCountActual;=0D + UINT64 OverlapBitFlag1;=0D + UINT64 OverlapBitFlag2;=0D + INT32 OverlapFlagRelation;=0D +=0D + if (RawMemoryRangeCount =3D=3D 0) {=0D + MemoryRanges[0].BaseAddress =3D 0;=0D + MemoryRanges[0].Length =3D (1ull << PhysicalAddressBits);=0D + MemoryRanges[0].Type =3D DefaultType;=0D + *MemoryRangeCount =3D 1;=0D + return;=0D + }=0D +=0D + AllEndPointsCount =3D RawMemoryRangeCount << 1;=0D + AllEndPointsInclusive =3D calloc (AllEndPointsCount, sizeof (UINT64)= );=0D + AllRangePiecesCountMax =3D RawMemoryRangeCount * 3 + 1;=0D + AllRangePieces =3D calloc (AllRangePiecesCountMax, sizeof (MT= RR_MEMORY_RANGE));=0D + CollectEndpoints (AllEndPointsInclusive, &AllEndPointsCount, RawMemoryRa= nges, RawMemoryRangeCount);=0D +=0D + for (Index =3D 0, AllRangePiecesCountActual =3D 0; Index < AllEndPointsC= ount - 1; Index++) {=0D + OverlapBitFlag1 =3D GetOverlapBitFlag (RawMemoryRanges, RawMemoryRange= Count, AllEndPointsInclusive[Index]);=0D + OverlapBitFlag2 =3D GetOverlapBitFlag (RawMemoryRanges, RawMemoryRange= Count, AllEndPointsInclusive[Index + 1]);=0D + OverlapFlagRelation =3D CheckOverlapBitFlagsRelation (OverlapBitFlag1,= OverlapBitFlag2);=0D + switch (OverlapFlagRelation) {=0D + case 0: // [1, 2]=0D + AllRangePieces[AllRangePiecesCountActual].BaseAddress =3D AllEndPo= intsInclusive[Index];=0D + AllRangePieces[AllRangePiecesCountActual].Length =3D AllEndPo= intsInclusive[Index + 1] - AllEndPointsInclusive[Index] + 1;=0D + AllRangePiecesCountActual++;=0D + break;=0D + case 1: // [1, 2)=0D + AllRangePieces[AllRangePiecesCountActual].BaseAddress =3D AllEndPo= intsInclusive[Index];=0D + AllRangePieces[AllRangePiecesCountActual].Length =3D (AllEndP= ointsInclusive[Index + 1] - 1) - AllEndPointsInclusive[Index] + 1;=0D + AllRangePiecesCountActual++;=0D + break;=0D + case 2: // (1, 2]=0D + AllRangePieces[AllRangePiecesCountActual].BaseAddress =3D AllEndPo= intsInclusive[Index] + 1;=0D + AllRangePieces[AllRangePiecesCountActual].Length =3D AllEndPo= intsInclusive[Index + 1] - (AllEndPointsInclusive[Index] + 1) + 1;=0D + AllRangePiecesCountActual++;=0D +=0D + if (!IsEndpointInRanges (AllEndPointsInclusive[Index], AllRangePie= ces, AllRangePiecesCountActual)) {=0D + AllRangePieces[AllRangePiecesCountActual].BaseAddress =3D AllEnd= PointsInclusive[Index];=0D + AllRangePieces[AllRangePiecesCountActual].Length =3D 1;=0D + AllRangePiecesCountActual++;=0D + }=0D + break;=0D + case 3: // (1, 2)=0D + AllRangePieces[AllRangePiecesCountActual].BaseAddress =3D AllEndPo= intsInclusive[Index] + 1;=0D + AllRangePieces[AllRangePiecesCountActual].Length =3D (AllEndP= ointsInclusive[Index + 1] - 1) - (AllEndPointsInclusive[Index] + 1) + 1;=0D + if (AllRangePieces[AllRangePiecesCountActual].Length =3D=3D 0) /= / Only in case 3 can exists Length=3D0, we should skip such "segment".=0D + break;=0D + AllRangePiecesCountActual++;=0D + if (!IsEndpointInRanges (AllEndPointsInclusive[Index], AllRangePie= ces, AllRangePiecesCountActual)) {=0D + AllRangePieces[AllRangePiecesCountActual].BaseAddress =3D AllEnd= PointsInclusive[Index];=0D + AllRangePieces[AllRangePiecesCountActual].Length =3D 1;=0D + AllRangePiecesCountActual++;=0D + }=0D + break;=0D + default:=0D + ASSERT (FALSE);=0D + }=0D + }=0D +=0D + for (Index =3D 0; Index < AllRangePiecesCountActual; Index++) {=0D + DetermineMemoryCacheType (DefaultType, &AllRangePieces[Index], RawMemo= ryRanges, RawMemoryRangeCount);=0D + }=0D +=0D + CompactAndExtendEffectiveMtrrMemoryRanges (DefaultType, PhysicalAddressB= its, &AllRangePieces, &AllRangePiecesCountActual);=0D + ASSERT (*MemoryRangeCount >=3D AllRangePiecesCountActual);=0D + memcpy (MemoryRanges, AllRangePieces, AllRangePiecesCountActual * sizeof= (MTRR_MEMORY_RANGE));=0D + *MemoryRangeCount =3D AllRangePiecesCountActual;=0D +=0D + free (AllEndPointsInclusive);=0D + free (AllRangePieces);=0D +}=0D diff --git a/UefiCpuPkg/Test/UefiCpuPkgHostTest.dsc b/UefiCpuPkg/Test/UefiC= puPkgHostTest.dsc new file mode 100644 index 0000000000..8a5c456830 --- /dev/null +++ b/UefiCpuPkg/Test/UefiCpuPkgHostTest.dsc @@ -0,0 +1,31 @@ +## @file=0D +# UefiCpuPkg DSC file used to build host-based unit tests.=0D +#=0D +# Copyright (c) 2020, Intel Corporation. All rights reserved.
=0D +# SPDX-License-Identifier: BSD-2-Clause-Patent=0D +#=0D +##=0D +=0D +[Defines]=0D + PLATFORM_NAME =3D UefiCpuPkgHostTest=0D + PLATFORM_GUID =3D E00B9599-5B74-4FF7-AB9F-8183FB13B2F9=0D + PLATFORM_VERSION =3D 0.1=0D + DSC_SPECIFICATION =3D 0x00010005=0D + OUTPUT_DIRECTORY =3D Build/UefiCpuPkg/HostTest=0D + SUPPORTED_ARCHITECTURES =3D IA32|X64=0D + BUILD_TARGETS =3D NOOPT=0D + SKUID_IDENTIFIER =3D DEFAULT=0D +=0D +!include UnitTestFrameworkPkg/UnitTestFrameworkPkgHost.dsc.inc=0D +=0D +[LibraryClasses]=0D + MtrrLib|UefiCpuPkg/Library/MtrrLib/MtrrLib.inf=0D +=0D +[PcdsPatchableInModule]=0D + gUefiCpuPkgTokenSpaceGuid.PcdCpuNumberOfReservedVariableMtrrs|0=0D +=0D +[Components]=0D + #=0D + # Build HOST_APPLICATION that tests the MtrrLib=0D + #=0D + UefiCpuPkg/Library/MtrrLib/UnitTest/MtrrLibUnitTestHost.inf=0D --=20 2.27.0.windows.1