TianoCore EDK2 master
Loading...
Searching...
No Matches
Support.c File Reference
#include "MtrrLibUnitTest.h"

Go to the source code of this file.

Functions

UINTN Rand (VOID)
 
VOID GenerateRandomNumbers (CHAR8 *FilePath, UINTN Count)
 
UINT32 EFIAPI UnitTestMtrrLibAsmCpuidEx (IN UINT32 Index, IN UINT32 SubIndex, OUT UINT32 *Eax OPTIONAL, OUT UINT32 *Ebx OPTIONAL, OUT UINT32 *Ecx OPTIONAL, OUT UINT32 *Edx OPTIONAL)
 
UINT32 EFIAPI UnitTestMtrrLibAsmCpuid (IN UINT32 Index, OUT UINT32 *Eax OPTIONAL, OUT UINT32 *Ebx OPTIONAL, OUT UINT32 *Ecx OPTIONAL, OUT UINT32 *Edx OPTIONAL)
 
UINT64 EFIAPI UnitTestMtrrLibAsmReadMsr64 (IN UINT32 MsrIndex)
 
UINT64 EFIAPI UnitTestMtrrLibAsmWriteMsr64 (IN UINT32 MsrIndex, IN UINT64 Value)
 
UNIT_TEST_STATUS EFIAPI InitializeMtrrRegs (IN MTRR_LIB_SYSTEM_PARAMETER *SystemParameter)
 
UNIT_TEST_STATUS EFIAPI InitializeSystem (IN UNIT_TEST_CONTEXT Context)
 
VOID CollectTestResult (IN MTRR_MEMORY_CACHE_TYPE DefaultType, IN UINT32 PhysicalAddressBits, IN UINT32 VariableMtrrCount, IN MTRR_SETTINGS *Mtrrs, OUT MTRR_MEMORY_RANGE *Ranges, IN OUT UINTN *RangeCount, OUT UINT32 *MtrrCount)
 
UINT32 Random32 (UINT32 Start, UINT32 Limit)
 
UINT64 Random64 (UINT64 Start, UINT64 Limit)
 
VOID GenerateRandomMtrrPair (IN UINT32 PhysicalAddressBits, IN MTRR_MEMORY_CACHE_TYPE CacheType, OUT MTRR_VARIABLE_SETTING *MtrrPair OPTIONAL, OUT MTRR_MEMORY_RANGE *MtrrMemoryRange OPTIONAL)
 
BOOLEAN RangesOverlap (IN MTRR_MEMORY_RANGE *Range, IN MTRR_MEMORY_RANGE *Ranges, IN UINTN Count)
 
VOID GenerateValidAndConfigurableMtrrPairs (IN UINT32 PhysicalAddressBits, IN OUT MTRR_MEMORY_RANGE *RawMemoryRanges, IN UINT32 UcCount, IN UINT32 WtCount, IN UINT32 WbCount, IN UINT32 WpCount, IN UINT32 WcCount)
 
MTRR_MEMORY_CACHE_TYPE GenerateRandomCacheType (VOID)
 
INT32 CompareFuncUint64 (CONST VOID *Left, CONST VOID *Right)
 
VOID DetermineMemoryCacheType (IN MTRR_MEMORY_CACHE_TYPE DefaultType, IN OUT MTRR_MEMORY_RANGE *Range, IN MTRR_MEMORY_RANGE *Ranges, IN UINT32 RangeCount)
 
UINT32 GetNextDifferentElementInSortedArray (IN UINT32 Index, IN UINT64 *Array, IN UINT32 Count)
 
VOID RemoveDuplicatesInSortedArray (IN OUT UINT64 *Array, IN OUT UINT32 *Count)
 
BOOLEAN AddressInRange (IN UINT64 Address, IN MTRR_MEMORY_RANGE Range)
 
UINT64 GetOverlapBitFlag (IN MTRR_MEMORY_RANGE *RawMemoryRanges, IN UINT32 RawMemoryRangeCount, IN UINT64 Address)
 
UINT32 CheckOverlapBitFlagsRelation (IN UINT64 Flag1, IN UINT64 Flag2)
 
BOOLEAN IsEndpointInRanges (IN UINT64 Endpoint, IN MTRR_MEMORY_RANGE *Ranges, IN UINTN RangeCount)
 
VOID CompactAndExtendEffectiveMtrrMemoryRanges (IN MTRR_MEMORY_CACHE_TYPE DefaultType, IN UINT32 PhysicalAddressBits, IN OUT MTRR_MEMORY_RANGE **EffectiveMtrrMemoryRanges, IN OUT UINTN *EffectiveMtrrMemoryRangesCount)
 
VOID CollectEndpoints (IN OUT UINT64 *Endpoints, IN OUT UINT32 *EndPointCount, IN MTRR_MEMORY_RANGE *RawMemoryRanges, IN UINT32 RawMemoryRangeCount)
 
VOID GetEffectiveMemoryRanges (IN MTRR_MEMORY_CACHE_TYPE DefaultType, IN UINT32 PhysicalAddressBits, IN MTRR_MEMORY_RANGE *RawMemoryRanges, IN UINT32 RawMemoryRangeCount, OUT MTRR_MEMORY_RANGE *MemoryRanges, OUT UINTN *MemoryRangeCount)
 

Variables

MTRR_MEMORY_CACHE_TYPE mMemoryCacheTypes []
 
UINT64 mFixedMtrrsValue [MTRR_NUMBER_OF_FIXED_MTRR]
 
MSR_IA32_MTRR_PHYSBASE_REGISTER mVariableMtrrsPhysBase [MTRR_NUMBER_OF_VARIABLE_MTRR]
 
MSR_IA32_MTRR_PHYSMASK_REGISTER mVariableMtrrsPhysMask [MTRR_NUMBER_OF_VARIABLE_MTRR]
 
MSR_IA32_MTRR_DEF_TYPE_REGISTER mDefTypeMsr
 
MSR_IA32_MTRRCAP_REGISTER mMtrrCapMsr
 
MSR_IA32_TME_ACTIVATE_REGISTER mTmeActivateMsr
 
CPUID_VERSION_INFO_EDX mCpuidVersionInfoEdx
 
CPUID_STRUCTURED_EXTENDED_FEATURE_FLAGS_ECX mCpuidExtendedFeatureFlagsEcx
 
CPUID_VIR_PHY_ADDRESS_SIZE_EAX mCpuidVirPhyAddressSizeEax
 
BOOLEAN mRandomInput
 
UINTN mNumberIndex = 0
 
UINTN mNumbers []
 
UINTN mNumberCount
 
CHAR8 mContentTemplate []
 

Detailed Description

Unit tests of the MtrrLib instance of the MtrrLib class

Copyright (c) 2018 - 2023, Intel Corporation. All rights reserved.
SPDX-License-Identifier: BSD-2-Clause-Patent

Definition in file Support.c.

Function Documentation

◆ AddressInRange()

BOOLEAN AddressInRange ( IN UINT64  Address,
IN MTRR_MEMORY_RANGE  Range 
)

Return TRUE when Address is in the Range.

Parameters
AddressThe address to check.
RangeThe range to check.
Returns
TRUE when Address is in the Range.

Definition at line 789 of file Support.c.

◆ CheckOverlapBitFlagsRelation()

UINT32 CheckOverlapBitFlagsRelation ( IN UINT64  Flag1,
IN UINT64  Flag2 
)

Return the relationship between flags.

Parameters
Flag1Flag 1
Flag2Flag 2
Return values
0Flag1 == Flag2
1Flag1 is a subset of Flag2
2Flag2 is a subset of Flag1
3No subset relations between Flag1 and Flag2.

Definition at line 836 of file Support.c.

◆ CollectEndpoints()

VOID CollectEndpoints ( IN OUT UINT64 *  Endpoints,
IN OUT UINT32 *  EndPointCount,
IN MTRR_MEMORY_RANGE RawMemoryRanges,
IN UINT32  RawMemoryRangeCount 
)

Collect all the endpoints in the raw memory ranges.

Parameters
EndpointsReturn the collected endpoints.
EndPointCountReturn the count of endpoints.
RawMemoryRangesRaw memory ranges.
RawMemoryRangeCountCount of raw memory ranges.

Definition at line 976 of file Support.c.

◆ CollectTestResult()

VOID CollectTestResult ( IN MTRR_MEMORY_CACHE_TYPE  DefaultType,
IN UINT32  PhysicalAddressBits,
IN UINT32  VariableMtrrCount,
IN MTRR_SETTINGS Mtrrs,
OUT MTRR_MEMORY_RANGE Ranges,
IN OUT UINTN RangeCount,
OUT UINT32 *  MtrrCount 
)

Collect the test result.

Parameters
DefaultTypeDefault memory type.
PhysicalAddressBitsPhysical address bits.
VariableMtrrCountCount of variable MTRRs.
MtrrsMTRR settings to collect from.
RangesReturn the memory ranges.
RangeCountReturn the count of memory ranges.
MtrrCountReturn the count of variable MTRRs being used.

Definition at line 437 of file Support.c.

◆ CompactAndExtendEffectiveMtrrMemoryRanges()

VOID CompactAndExtendEffectiveMtrrMemoryRanges ( IN MTRR_MEMORY_CACHE_TYPE  DefaultType,
IN UINT32  PhysicalAddressBits,
IN OUT MTRR_MEMORY_RANGE **  EffectiveMtrrMemoryRanges,
IN OUT UINTN EffectiveMtrrMemoryRangesCount 
)

Compact adjacent ranges of the same type.

Parameters
DefaultTypeDefault memory type.
PhysicalAddressBitsPhysical address bits.
EffectiveMtrrMemoryRangesMemory ranges to compact.
EffectiveMtrrMemoryRangesCountReturn the new count of memory ranges.

Definition at line 893 of file Support.c.

◆ CompareFuncUint64()

INT32 CompareFuncUint64 ( CONST VOID *  Left,
CONST VOID *  Right 
)

Compare function used by qsort(). Compare function used by qsort().

Parameters
LeftLeft operand to compare.
RightRight operand to compare.
Return values
0Left == Right
-1Left < Right
1Left > Right

Definition at line 680 of file Support.c.

◆ DetermineMemoryCacheType()

VOID DetermineMemoryCacheType ( IN MTRR_MEMORY_CACHE_TYPE  DefaultType,
IN OUT MTRR_MEMORY_RANGE Range,
IN MTRR_MEMORY_RANGE Ranges,
IN UINT32  RangeCount 
)

Determin the memory cache type for the Range.

Parameters
DefaultTypeDefault cache type.
RangeThe memory range to determin the cache type.
RangesThe entire memory ranges.
RangeCountCount of the entire memory ranges.

Definition at line 706 of file Support.c.

◆ GenerateRandomCacheType()

MTRR_MEMORY_CACHE_TYPE GenerateRandomCacheType ( VOID  )

Return a random memory cache type.

Definition at line 658 of file Support.c.

◆ GenerateRandomMtrrPair()

VOID GenerateRandomMtrrPair ( IN UINT32  PhysicalAddressBits,
IN MTRR_MEMORY_CACHE_TYPE  CacheType,
OUT MTRR_VARIABLE_SETTING *MtrrPair  OPTIONAL,
OUT MTRR_MEMORY_RANGE *MtrrMemoryRange  OPTIONAL 
)

Generate random MTRR BASE/MASK for a specified type.

Parameters
PhysicalAddressBitsPhysical address bits.
CacheTypeCache type.
MtrrPairReturn the random MTRR.
MtrrMemoryRangeReturn the random memory range.

Definition at line 514 of file Support.c.

◆ GenerateRandomNumbers()

VOID GenerateRandomNumbers ( CHAR8 *  FilePath,
UINTN  Count 
)

Generate Count random numbers in FilePath.

Parameters
FilePathThe file path to put the generated random numbers.
CountCount of random numbers.

Definition at line 68 of file Support.c.

◆ GenerateValidAndConfigurableMtrrPairs()

VOID GenerateValidAndConfigurableMtrrPairs ( IN UINT32  PhysicalAddressBits,
IN OUT MTRR_MEMORY_RANGE RawMemoryRanges,
IN UINT32  UcCount,
IN UINT32  WtCount,
IN UINT32  WbCount,
IN UINT32  WpCount,
IN UINT32  WcCount 
)

Generate random MTRRs.

Parameters
PhysicalAddressBitsPhysical address bits.
RawMemoryRangesReturn the randomly generated MTRRs.
UcCountCount of Uncacheable MTRRs.
WtCountCount of Write Through MTRRs.
WbCountCount of Write Back MTRRs.
WpCountCount of Write Protected MTRRs.
WcCountCount of Write Combine MTRRs.

Definition at line 606 of file Support.c.

◆ GetEffectiveMemoryRanges()

VOID GetEffectiveMemoryRanges ( IN MTRR_MEMORY_CACHE_TYPE  DefaultType,
IN UINT32  PhysicalAddressBits,
IN MTRR_MEMORY_RANGE RawMemoryRanges,
IN UINT32  RawMemoryRangeCount,
OUT MTRR_MEMORY_RANGE MemoryRanges,
OUT UINTN MemoryRangeCount 
)

Convert the MTRR BASE/MASK array to memory ranges.

Parameters
DefaultTypeDefault memory type.
PhysicalAddressBitsPhysical address bits.
RawMemoryRangesRaw memory ranges.
RawMemoryRangeCountCount of raw memory ranges.
MemoryRangesMemory ranges.
MemoryRangeCountCount of memory ranges.

Definition at line 1009 of file Support.c.

◆ GetNextDifferentElementInSortedArray()

UINT32 GetNextDifferentElementInSortedArray ( IN UINT32  Index,
IN UINT64 *  Array,
IN UINT32  Count 
)

Get the index of the element that does NOT equals to Array[Index].

Parameters
IndexCurrent element.
ArrayArray to scan.
CountCount of the array.
Returns
Next element that doesn't equal to current one.

Definition at line 739 of file Support.c.

◆ GetOverlapBitFlag()

UINT64 GetOverlapBitFlag ( IN MTRR_MEMORY_RANGE RawMemoryRanges,
IN UINT32  RawMemoryRangeCount,
IN UINT64  Address 
)

Get the overlap bit flag.

Parameters
RawMemoryRangesRaw memory ranges.
RawMemoryRangeCountCount of raw memory ranges.
AddressThe address to check.

Definition at line 805 of file Support.c.

◆ InitializeMtrrRegs()

UNIT_TEST_STATUS EFIAPI InitializeMtrrRegs ( IN MTRR_LIB_SYSTEM_PARAMETER SystemParameter)

Initialize the MTRR registers.

Parameters
SystemParameterSystem parameter that controls the MTRR registers initialization.

Definition at line 358 of file Support.c.

◆ InitializeSystem()

UNIT_TEST_STATUS EFIAPI InitializeSystem ( IN UNIT_TEST_CONTEXT  Context)

Initialize the MTRR registers.

Parameters
ContextSystem parameter that controls the MTRR registers initialization.

Definition at line 418 of file Support.c.

◆ IsEndpointInRanges()

BOOLEAN IsEndpointInRanges ( IN UINT64  Endpoint,
IN MTRR_MEMORY_RANGE Ranges,
IN UINTN  RangeCount 
)

Return TRUE when the Endpoint is in any of the Ranges.

Parameters
EndpointThe endpoint to check.
RangesThe memory ranges.
RangeCountCount of memory ranges.
Return values
TRUEEndpoint is in one of the range.
FALSEEndpoint is not in any of the ranges.

Definition at line 867 of file Support.c.

◆ Rand()

UINTN Rand ( VOID  )

Return a random number between 0 and RAND_MAX.

If mRandomInput is TRUE, the routine directly calls rand(). Otherwise, the routine returns the pre-generated numbers.

Returns
a number between 0 and RAND_MAX.

Definition at line 39 of file Support.c.

◆ Random32()

UINT32 Random32 ( UINT32  Start,
UINT32  Limit 
)

Return a 32bit random number.

Parameters
StartStart of the random number range.
LimitLimit of the random number range.
Returns
32bit random number

Definition at line 481 of file Support.c.

◆ Random64()

UINT64 Random64 ( UINT64  Start,
UINT64  Limit 
)

Return a 64bit random number.

Parameters
StartStart of the random number range.
LimitLimit of the random number range.
Returns
64bit random number

Definition at line 497 of file Support.c.

◆ RangesOverlap()

BOOLEAN RangesOverlap ( IN MTRR_MEMORY_RANGE Range,
IN MTRR_MEMORY_RANGE Ranges,
IN UINTN  Count 
)

Check whether the Range overlaps with any one in Ranges.

Parameters
RangeThe memory range to check.
RangesThe memory ranges.
CountCount of memory ranges.
Returns
TRUE when overlap exists.

Definition at line 572 of file Support.c.

◆ RemoveDuplicatesInSortedArray()

VOID RemoveDuplicatesInSortedArray ( IN OUT UINT64 *  Array,
IN OUT UINT32 *  Count 
)

Remove the duplicates from the array.

Parameters
ArrayThe array to operate on.
CountCount of the array.

Definition at line 762 of file Support.c.

◆ UnitTestMtrrLibAsmCpuid()

UINT32 EFIAPI UnitTestMtrrLibAsmCpuid ( IN UINT32  Index,
OUT UINT32 *Eax  OPTIONAL,
OUT UINT32 *Ebx  OPTIONAL,
OUT UINT32 *Ecx  OPTIONAL,
OUT UINT32 *Edx  OPTIONAL 
)

Retrieves CPUID information.

Executes the CPUID instruction with EAX set to the value specified by Index. This function always returns Index. If Eax is not NULL, then the value of EAX after CPUID is returned in Eax. If Ebx is not NULL, then the value of EBX after CPUID is returned in Ebx. If Ecx is not NULL, then the value of ECX after CPUID is returned in Ecx. If Edx is not NULL, then the value of EDX after CPUID is returned in Edx. This function is only available on IA-32 and x64.

Parameters
IndexThe 32-bit value to load into EAX prior to invoking the CPUID instruction.
EaxThe pointer to the 32-bit EAX value returned by the CPUID instruction. This is an optional parameter that may be NULL.
EbxThe pointer to the 32-bit EBX value returned by the CPUID instruction. This is an optional parameter that may be NULL.
EcxThe pointer to the 32-bit ECX value returned by the CPUID instruction. This is an optional parameter that may be NULL.
EdxThe pointer to the 32-bit EDX value returned by the CPUID instruction. This is an optional parameter that may be NULL.
Returns
Index.

Definition at line 205 of file Support.c.

◆ UnitTestMtrrLibAsmCpuidEx()

UINT32 EFIAPI UnitTestMtrrLibAsmCpuidEx ( IN UINT32  Index,
IN UINT32  SubIndex,
OUT UINT32 *Eax  OPTIONAL,
OUT UINT32 *Ebx  OPTIONAL,
OUT UINT32 *Ecx  OPTIONAL,
OUT UINT32 *Edx  OPTIONAL 
)

Retrieves CPUID information using an extended leaf identifier.

Executes the CPUID instruction with EAX set to the value specified by Index and ECX set to the value specified by SubIndex. This function always returns Index. This function is only available on IA-32 and x64.

If Eax is not NULL, then the value of EAX after CPUID is returned in Eax. If Ebx is not NULL, then the value of EBX after CPUID is returned in Ebx. If Ecx is not NULL, then the value of ECX after CPUID is returned in Ecx. If Edx is not NULL, then the value of EDX after CPUID is returned in Edx.

Parameters
IndexThe 32-bit value to load into EAX prior to invoking the CPUID instruction.
SubIndexThe 32-bit value to load into ECX prior to invoking the CPUID instruction.
EaxThe pointer to the 32-bit EAX value returned by the CPUID instruction. This is an optional parameter that may be NULL.
EbxThe pointer to the 32-bit EBX value returned by the CPUID instruction. This is an optional parameter that may be NULL.
EcxThe pointer to the 32-bit ECX value returned by the CPUID instruction. This is an optional parameter that may be NULL.
EdxThe pointer to the 32-bit EDX value returned by the CPUID instruction. This is an optional parameter that may be NULL.
Returns
Index.

Definition at line 124 of file Support.c.

◆ UnitTestMtrrLibAsmReadMsr64()

UINT64 EFIAPI UnitTestMtrrLibAsmReadMsr64 ( IN UINT32  MsrIndex)

Returns a 64-bit Machine Specific Register(MSR).

Reads and returns the 64-bit MSR specified by Index. No parameter checking is performed on Index, and some Index values may cause CPU exceptions. The caller must either guarantee that Index is valid, or the caller must set up exception handlers to catch the exceptions. This function is only available on IA-32 and x64.

Parameters
MsrIndexThe 32-bit MSR index to read.
Returns
The value of the MSR identified by MsrIndex.

Definition at line 232 of file Support.c.

◆ UnitTestMtrrLibAsmWriteMsr64()

UINT64 EFIAPI UnitTestMtrrLibAsmWriteMsr64 ( IN UINT32  MsrIndex,
IN UINT64  Value 
)

Writes a 64-bit value to a Machine Specific Register(MSR), and returns the value.

Writes the 64-bit value specified by Value to the MSR specified by Index. The 64-bit value written to the MSR is returned. No parameter checking is performed on Index or Value, and some of these may cause CPU exceptions. The caller must either guarantee that Index and Value are valid, or the caller must establish proper exception handlers. This function is only available on IA-32 and x64.

Parameters
MsrIndexThe 32-bit MSR index to write.
ValueThe 64-bit value to write to the MSR.
Returns
Value

Definition at line 298 of file Support.c.

Variable Documentation

◆ mContentTemplate

CHAR8 mContentTemplate[]
Initial value:
= {
"/** @file\n"
" Pre-generated random number used by MtrrLib test.\n"
"\n"
" Copyright (c) 2020, Intel Corporation. All rights reserved.<BR>\n"
" SPDX-License-Identifier: BSD-2-Clause-Patent\n"
"**/\n"
"UINTN mNumberCount = %d;\n"
"UINTN mNumbers[] = {"
}

Definition at line 50 of file Support.c.

◆ mCpuidExtendedFeatureFlagsEcx

CPUID_STRUCTURED_EXTENDED_FEATURE_FLAGS_ECX mCpuidExtendedFeatureFlagsEcx

Definition at line 22 of file Support.c.

◆ mCpuidVersionInfoEdx

CPUID_VERSION_INFO_EDX mCpuidVersionInfoEdx

Definition at line 21 of file Support.c.

◆ mCpuidVirPhyAddressSizeEax

CPUID_VIR_PHY_ADDRESS_SIZE_EAX mCpuidVirPhyAddressSizeEax

Definition at line 23 of file Support.c.

◆ mDefTypeMsr

Definition at line 18 of file Support.c.

◆ mFixedMtrrsValue

UINT64 mFixedMtrrsValue[MTRR_NUMBER_OF_FIXED_MTRR]

Definition at line 15 of file Support.c.

◆ mMemoryCacheTypes

MTRR_MEMORY_CACHE_TYPE mMemoryCacheTypes[]
Initial value:
= {
CacheUncacheable, CacheWriteCombining, CacheWriteThrough, CacheWriteProtected, CacheWriteBack
}

Definition at line 11 of file Support.c.

◆ mMtrrCapMsr

Definition at line 19 of file Support.c.

◆ mNumberCount

UINTN mNumberCount
extern

Definition at line 7 of file RandomNumber.c.

◆ mNumberIndex

UINTN mNumberIndex = 0

Definition at line 26 of file Support.c.

◆ mNumbers

UINTN mNumbers[]
extern

Definition at line 8 of file RandomNumber.c.

◆ mRandomInput

BOOLEAN mRandomInput

Definition at line 25 of file Support.c.

◆ mTmeActivateMsr

Definition at line 20 of file Support.c.

◆ mVariableMtrrsPhysBase

MSR_IA32_MTRR_PHYSBASE_REGISTER mVariableMtrrsPhysBase[MTRR_NUMBER_OF_VARIABLE_MTRR]

Definition at line 16 of file Support.c.

◆ mVariableMtrrsPhysMask

MSR_IA32_MTRR_PHYSMASK_REGISTER mVariableMtrrsPhysMask[MTRR_NUMBER_OF_VARIABLE_MTRR]

Definition at line 17 of file Support.c.