MtrrLibUnitTest.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. /** @file
  2. Copyright (c) 2020, Intel Corporation. All rights reserved.<BR>
  3. SPDX-License-Identifier: BSD-2-Clause-Patent
  4. **/
  5. #ifndef _MTRR_SUPPORT_H_
  6. #define _MTRR_SUPPORT_H_
  7. #include <stdio.h>
  8. #include <stdlib.h>
  9. #include <string.h>
  10. #include <stdarg.h>
  11. #include <stddef.h>
  12. #include <setjmp.h>
  13. #include <cmocka.h>
  14. #include <time.h>
  15. #include <Uefi.h>
  16. #include <Library/BaseLib.h>
  17. #include <Library/BaseMemoryLib.h>
  18. #include <Library/DebugLib.h>
  19. #include <Library/UnitTestLib.h>
  20. #include <Library/MtrrLib.h>
  21. #include <Library/UnitTestHostBaseLib.h>
  22. #include <Register/ArchitecturalMsr.h>
  23. #include <Register/Cpuid.h>
  24. #include <Register/Msr.h>
  25. #define UNIT_TEST_APP_NAME "MtrrLib Unit Tests"
  26. #define UNIT_TEST_APP_VERSION "1.0"
  27. #define SCRATCH_BUFFER_SIZE SIZE_16KB
  28. typedef struct {
  29. UINT8 PhysicalAddressBits;
  30. BOOLEAN MtrrSupported;
  31. BOOLEAN FixedMtrrSupported;
  32. MTRR_MEMORY_CACHE_TYPE DefaultCacheType;
  33. UINT32 VariableMtrrCount;
  34. } MTRR_LIB_SYSTEM_PARAMETER;
  35. extern UINT32 mFixedMtrrsIndex[];
  36. extern BOOLEAN mRandomInput;
  37. /**
  38. Initialize the MTRR registers.
  39. @param SystemParameter System parameter that controls the MTRR registers initialization.
  40. **/
  41. UNIT_TEST_STATUS
  42. EFIAPI
  43. InitializeMtrrRegs (
  44. IN MTRR_LIB_SYSTEM_PARAMETER *SystemParameter
  45. );
  46. /**
  47. Initialize the MTRR registers.
  48. @param Context System parameter that controls the MTRR registers initialization.
  49. **/
  50. UNIT_TEST_STATUS
  51. EFIAPI
  52. InitializeSystem (
  53. IN UNIT_TEST_CONTEXT Context
  54. );
  55. /**
  56. Return a random memory cache type.
  57. **/
  58. MTRR_MEMORY_CACHE_TYPE
  59. GenerateRandomCacheType (
  60. VOID
  61. );
  62. /**
  63. Generate random MTRRs.
  64. @param PhysicalAddressBits Physical address bits.
  65. @param RawMemoryRanges Return the randomly generated MTRRs.
  66. @param UcCount Count of Uncacheable MTRRs.
  67. @param WtCount Count of Write Through MTRRs.
  68. @param WbCount Count of Write Back MTRRs.
  69. @param WpCount Count of Write Protected MTRRs.
  70. @param WcCount Count of Write Combining MTRRs.
  71. **/
  72. VOID
  73. GenerateValidAndConfigurableMtrrPairs (
  74. IN UINT32 PhysicalAddressBits,
  75. IN OUT MTRR_MEMORY_RANGE *RawMemoryRanges,
  76. IN UINT32 UcCount,
  77. IN UINT32 WtCount,
  78. IN UINT32 WbCount,
  79. IN UINT32 WpCount,
  80. IN UINT32 WcCount
  81. );
  82. /**
  83. Convert the MTRR BASE/MASK array to memory ranges.
  84. @param DefaultType Default memory type.
  85. @param PhysicalAddressBits Physical address bits.
  86. @param RawMemoryRanges Raw memory ranges.
  87. @param RawMemoryRangeCount Count of raw memory ranges.
  88. @param MemoryRanges Memory ranges.
  89. @param MemoryRangeCount Count of memory ranges.
  90. **/
  91. VOID
  92. GetEffectiveMemoryRanges (
  93. IN MTRR_MEMORY_CACHE_TYPE DefaultType,
  94. IN UINT32 PhysicalAddressBits,
  95. IN MTRR_MEMORY_RANGE *RawMemoryRanges,
  96. IN UINT32 RawMemoryRangeCount,
  97. OUT MTRR_MEMORY_RANGE *MemoryRanges,
  98. OUT UINTN *MemoryRangeCount
  99. );
  100. /**
  101. Generate random MTRR BASE/MASK for a specified type.
  102. @param PhysicalAddressBits Physical address bits.
  103. @param CacheType Cache type.
  104. @param MtrrPair Return the random MTRR.
  105. @param MtrrMemoryRange Return the random memory range.
  106. **/
  107. VOID
  108. GenerateRandomMtrrPair (
  109. IN UINT32 PhysicalAddressBits,
  110. IN MTRR_MEMORY_CACHE_TYPE CacheType,
  111. OUT MTRR_VARIABLE_SETTING *MtrrPair OPTIONAL,
  112. OUT MTRR_MEMORY_RANGE *MtrrMemoryRange OPTIONAL
  113. );
  114. /**
  115. Collect the test result.
  116. @param DefaultType Default memory type.
  117. @param PhysicalAddressBits Physical address bits.
  118. @param VariableMtrrCount Count of variable MTRRs.
  119. @param Mtrrs MTRR settings to collect from.
  120. @param Ranges Return the memory ranges.
  121. @param RangeCount Return the count of memory ranges.
  122. @param MtrrCount Return the count of variable MTRRs being used.
  123. **/
  124. VOID
  125. CollectTestResult (
  126. IN MTRR_MEMORY_CACHE_TYPE DefaultType,
  127. IN UINT32 PhysicalAddressBits,
  128. IN UINT32 VariableMtrrCount,
  129. IN MTRR_SETTINGS *Mtrrs,
  130. OUT MTRR_MEMORY_RANGE *Ranges,
  131. IN OUT UINTN *RangeCount,
  132. OUT UINT32 *MtrrCount
  133. );
  134. /**
  135. Return a 64bit random number.
  136. @param Start Start of the random number range.
  137. @param Limit Limit of the random number range.
  138. @return 64bit random number
  139. **/
  140. UINT64
  141. Random64 (
  142. UINT64 Start,
  143. UINT64 Limit
  144. );
  145. /**
  146. Return a 32bit random number.
  147. @param Start Start of the random number range.
  148. @param Limit Limit of the random number range.
  149. @return 32bit random number
  150. **/
  151. UINT32
  152. Random32 (
  153. UINT32 Start,
  154. UINT32 Limit
  155. );
  156. /**
  157. Generate Count random numbers in FilePath.
  158. @param FilePath The file path to put the generated random numbers.
  159. @param Count Count of random numbers.
  160. **/
  161. VOID
  162. GenerateRandomNumbers (
  163. CHAR8 *FilePath,
  164. UINTN Count
  165. );
  166. #endif