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