2 Unit tests of the MtrrLib instance of the MtrrLib class
4 Copyright (c) 2020, Intel Corporation. All rights reserved.<BR>
5 SPDX-License-Identifier: BSD-2-Clause-Patent
9 #include "MtrrLibUnitTest.h"
11 STATIC CONST MTRR_LIB_SYSTEM_PARAMETER mDefaultSystemParameter
= {
12 42, TRUE
, TRUE
, CacheUncacheable
, 12
15 STATIC MTRR_LIB_SYSTEM_PARAMETER mSystemParameters
[] = {
16 { 38, TRUE
, TRUE
, CacheUncacheable
, 12 },
17 { 38, TRUE
, TRUE
, CacheWriteBack
, 12 },
18 { 38, TRUE
, TRUE
, CacheWriteThrough
, 12 },
19 { 38, TRUE
, TRUE
, CacheWriteProtected
, 12 },
20 { 38, TRUE
, TRUE
, CacheWriteCombining
, 12 },
22 { 42, TRUE
, TRUE
, CacheUncacheable
, 12 },
23 { 42, TRUE
, TRUE
, CacheWriteBack
, 12 },
24 { 42, TRUE
, TRUE
, CacheWriteThrough
, 12 },
25 { 42, TRUE
, TRUE
, CacheWriteProtected
, 12 },
26 { 42, TRUE
, TRUE
, CacheWriteCombining
, 12 },
28 { 48, TRUE
, TRUE
, CacheUncacheable
, 12 },
29 { 48, TRUE
, TRUE
, CacheWriteBack
, 12 },
30 { 48, TRUE
, TRUE
, CacheWriteThrough
, 12 },
31 { 48, TRUE
, TRUE
, CacheWriteProtected
, 12 },
32 { 48, TRUE
, TRUE
, CacheWriteCombining
, 12 },
35 UINT32 mFixedMtrrsIndex
[] = {
36 MSR_IA32_MTRR_FIX64K_00000
,
37 MSR_IA32_MTRR_FIX16K_80000
,
38 MSR_IA32_MTRR_FIX16K_A0000
,
39 MSR_IA32_MTRR_FIX4K_C0000
,
40 MSR_IA32_MTRR_FIX4K_C8000
,
41 MSR_IA32_MTRR_FIX4K_D0000
,
42 MSR_IA32_MTRR_FIX4K_D8000
,
43 MSR_IA32_MTRR_FIX4K_E0000
,
44 MSR_IA32_MTRR_FIX4K_E8000
,
45 MSR_IA32_MTRR_FIX4K_F0000
,
46 MSR_IA32_MTRR_FIX4K_F8000
49 (ARRAY_SIZE (mFixedMtrrsIndex
) == MTRR_NUMBER_OF_FIXED_MTRR
),
50 "gFixedMtrrIndex does NOT contain all the fixed MTRRs!"
54 // Context structure to be used for most of the test cases.
57 CONST MTRR_LIB_SYSTEM_PARAMETER
*SystemParameter
;
58 } MTRR_LIB_TEST_CONTEXT
;
61 // Context structure to be used for GetFirmwareVariableMtrrCount() test.
64 UINT32 NumberOfReservedVariableMtrrs
;
65 CONST MTRR_LIB_SYSTEM_PARAMETER
*SystemParameter
;
66 } MTRR_LIB_GET_FIRMWARE_VARIABLE_MTRR_COUNT_CONTEXT
;
68 STATIC CHAR8
*mCacheDescription
[] = { "UC", "WC", "N/A", "N/A", "WT", "WP", "WB" };
71 Compare the actual memory ranges against expected memory ranges and return PASS when they match.
73 @param ExpectedMemoryRanges Expected memory ranges.
74 @param ExpectedMemoryRangeCount Count of expected memory ranges.
75 @param ActualRanges Actual memory ranges.
76 @param ActualRangeCount Count of actual memory ranges.
78 @retval UNIT_TEST_PASSED Test passed.
79 @retval others Test failed.
83 IN MTRR_MEMORY_RANGE
*ExpectedMemoryRanges
,
84 IN UINTN ExpectedMemoryRangeCount
,
85 IN MTRR_MEMORY_RANGE
*ActualRanges
,
86 IN UINTN ActualRangeCount
90 UT_ASSERT_EQUAL (ExpectedMemoryRangeCount
, ActualRangeCount
);
91 for (Index
= 0; Index
< ExpectedMemoryRangeCount
; Index
++) {
92 UT_ASSERT_EQUAL (ExpectedMemoryRanges
[Index
].BaseAddress
, ActualRanges
[Index
].BaseAddress
);
93 UT_ASSERT_EQUAL (ExpectedMemoryRanges
[Index
].Length
, ActualRanges
[Index
].Length
);
94 UT_ASSERT_EQUAL (ExpectedMemoryRanges
[Index
].Type
, ActualRanges
[Index
].Type
);
97 return UNIT_TEST_PASSED
;
101 Dump the memory ranges.
103 @param Ranges Memory ranges to dump.
104 @param RangeCount Count of memory ranges.
108 MTRR_MEMORY_RANGE
*Ranges
,
113 for (Index
= 0; Index
< RangeCount
; Index
++) {
114 UT_LOG_INFO ("\t{ 0x%016llx, 0x%016llx, %a },\n", Ranges
[Index
].BaseAddress
, Ranges
[Index
].Length
, mCacheDescription
[Ranges
[Index
].Type
]);
122 Generate random count of MTRRs for each cache type.
124 @param TotalCount Total MTRR count.
125 @param UcCount Return count of Uncacheable type.
126 @param WtCount Return count of Write Through type.
127 @param WbCount Return count of Write Back type.
128 @param WpCount Return count of Write Protected type.
129 @param WcCount Return count of Write Combining type.
132 GenerateRandomMemoryTypeCombination (
133 IN UINT32 TotalCount
,
142 UINT32 TotalMtrrCount
;
143 UINT32
*CountPerType
[5];
145 CountPerType
[0] = UcCount
;
146 CountPerType
[1] = WtCount
;
147 CountPerType
[2] = WbCount
;
148 CountPerType
[3] = WpCount
;
149 CountPerType
[4] = WcCount
;
152 // Initialize the count of each cache type to 0.
154 for (Index
= 0; Index
< ARRAY_SIZE (CountPerType
); Index
++) {
155 *(CountPerType
[Index
]) = 0;
159 // Pick a random count of MTRRs
161 TotalMtrrCount
= Random32 (1, TotalCount
);
162 for (Index
= 0; Index
< TotalMtrrCount
; Index
++) {
164 // For each of them, pick a random cache type.
166 (*(CountPerType
[Random32 (0, ARRAY_SIZE (CountPerType
) - 1)]))++;
171 Unit test of MtrrLib service MtrrSetMemoryAttribute()
173 @param[in] Context Ignored
175 @retval UNIT_TEST_PASSED The Unit test has completed and the test
177 @retval UNIT_TEST_ERROR_TEST_FAILED A test case assertion has failed.
182 UnitTestMtrrSetMemoryAttributesInMtrrSettings (
183 IN UNIT_TEST_CONTEXT Context
186 CONST MTRR_LIB_SYSTEM_PARAMETER
*SystemParameter
;
187 RETURN_STATUS Status
;
197 MTRR_SETTINGS LocalMtrrs
;
199 MTRR_MEMORY_RANGE RawMtrrRange
[MTRR_NUMBER_OF_VARIABLE_MTRR
];
200 MTRR_MEMORY_RANGE ExpectedMemoryRanges
[MTRR_NUMBER_OF_FIXED_MTRR
* sizeof (UINT64
) + 2 * MTRR_NUMBER_OF_VARIABLE_MTRR
+ 1];
201 UINT32 ExpectedVariableMtrrUsage
;
202 UINTN ExpectedMemoryRangesCount
;
204 MTRR_MEMORY_RANGE ActualMemoryRanges
[MTRR_NUMBER_OF_FIXED_MTRR
* sizeof (UINT64
) + 2 * MTRR_NUMBER_OF_VARIABLE_MTRR
+ 1];
205 UINT32 ActualVariableMtrrUsage
;
206 UINTN ActualMemoryRangesCount
;
208 MTRR_SETTINGS
*Mtrrs
[2];
210 SystemParameter
= (MTRR_LIB_SYSTEM_PARAMETER
*) Context
;
211 GenerateRandomMemoryTypeCombination (
212 SystemParameter
->VariableMtrrCount
- PatchPcdGet32 (PcdCpuNumberOfReservedVariableMtrrs
),
213 &UcCount
, &WtCount
, &WbCount
, &WpCount
, &WcCount
215 GenerateValidAndConfigurableMtrrPairs (
216 SystemParameter
->PhysicalAddressBits
, RawMtrrRange
,
217 UcCount
, WtCount
, WbCount
, WpCount
, WcCount
220 ExpectedVariableMtrrUsage
= UcCount
+ WtCount
+ WbCount
+ WpCount
+ WcCount
;
221 ExpectedMemoryRangesCount
= ARRAY_SIZE (ExpectedMemoryRanges
);
222 GetEffectiveMemoryRanges (
223 SystemParameter
->DefaultCacheType
,
224 SystemParameter
->PhysicalAddressBits
,
225 RawMtrrRange
, ExpectedVariableMtrrUsage
,
226 ExpectedMemoryRanges
, &ExpectedMemoryRangesCount
230 "Total MTRR [%d]: UC=%d, WT=%d, WB=%d, WP=%d, WC=%d\n",
231 ExpectedVariableMtrrUsage
, UcCount
, WtCount
, WbCount
, WpCount
, WcCount
233 UT_LOG_INFO ("--- Expected Memory Ranges [%d] ---\n", ExpectedMemoryRangesCount
);
234 DumpMemoryRanges (ExpectedMemoryRanges
, ExpectedMemoryRangesCount
);
237 // Default cache type is always an INPUT
239 ZeroMem (&LocalMtrrs
, sizeof (LocalMtrrs
));
240 LocalMtrrs
.MtrrDefType
= MtrrGetDefaultMemoryType ();
241 ScratchSize
= SCRATCH_BUFFER_SIZE
;
242 Mtrrs
[0] = &LocalMtrrs
;
245 for (MtrrIndex
= 0; MtrrIndex
< ARRAY_SIZE (Mtrrs
); MtrrIndex
++) {
246 Scratch
= calloc (ScratchSize
, sizeof (UINT8
));
247 Status
= MtrrSetMemoryAttributesInMtrrSettings (Mtrrs
[MtrrIndex
], Scratch
, &ScratchSize
, ExpectedMemoryRanges
, ExpectedMemoryRangesCount
);
248 if (Status
== RETURN_BUFFER_TOO_SMALL
) {
249 Scratch
= realloc (Scratch
, ScratchSize
);
250 Status
= MtrrSetMemoryAttributesInMtrrSettings (Mtrrs
[MtrrIndex
], Scratch
, &ScratchSize
, ExpectedMemoryRanges
, ExpectedMemoryRangesCount
);
252 UT_ASSERT_STATUS_EQUAL (Status
, RETURN_SUCCESS
);
254 if (Mtrrs
[MtrrIndex
] == NULL
) {
255 ZeroMem (&LocalMtrrs
, sizeof (LocalMtrrs
));
256 MtrrGetAllMtrrs (&LocalMtrrs
);
258 ActualMemoryRangesCount
= ARRAY_SIZE (ActualMemoryRanges
);
260 SystemParameter
->DefaultCacheType
, SystemParameter
->PhysicalAddressBits
, SystemParameter
->VariableMtrrCount
,
261 &LocalMtrrs
, ActualMemoryRanges
, &ActualMemoryRangesCount
, &ActualVariableMtrrUsage
264 UT_LOG_INFO ("--- Actual Memory Ranges [%d] ---\n", ActualMemoryRangesCount
);
265 DumpMemoryRanges (ActualMemoryRanges
, ActualMemoryRangesCount
);
266 VerifyMemoryRanges (ExpectedMemoryRanges
, ExpectedMemoryRangesCount
, ActualMemoryRanges
, ActualMemoryRangesCount
);
267 UT_ASSERT_TRUE (ExpectedVariableMtrrUsage
>= ActualVariableMtrrUsage
);
269 ZeroMem (&LocalMtrrs
, sizeof (LocalMtrrs
));
274 return UNIT_TEST_PASSED
;
278 Test routine to check whether invalid base/size can be rejected.
280 @param Context Pointer to MTRR_LIB_SYSTEM_PARAMETER.
286 UnitTestInvalidMemoryLayouts (
287 IN UNIT_TEST_CONTEXT Context
290 CONST MTRR_LIB_SYSTEM_PARAMETER
*SystemParameter
;
291 MTRR_MEMORY_RANGE Ranges
[MTRR_NUMBER_OF_VARIABLE_MTRR
* 2 + 1];
297 RETURN_STATUS Status
;
300 SystemParameter
= (MTRR_LIB_SYSTEM_PARAMETER
*) Context
;
302 RangeCount
= Random32 (1, ARRAY_SIZE (Ranges
));
303 MaxAddress
= 1ull << SystemParameter
->PhysicalAddressBits
;
305 for (Index
= 0; Index
< RangeCount
; Index
++) {
307 BaseAddress
= Random64 (0, MaxAddress
);
308 Length
= Random64 (1, MaxAddress
- BaseAddress
);
309 } while (((BaseAddress
& 0xFFF) == 0) || ((Length
& 0xFFF) == 0));
311 Ranges
[Index
].BaseAddress
= BaseAddress
;
312 Ranges
[Index
].Length
= Length
;
313 Ranges
[Index
].Type
= GenerateRandomCacheType ();
315 Status
= MtrrSetMemoryAttribute (
316 Ranges
[Index
].BaseAddress
, Ranges
[Index
].Length
, Ranges
[Index
].Type
318 UT_ASSERT_TRUE (RETURN_ERROR (Status
));
322 Status
= MtrrSetMemoryAttributesInMtrrSettings (NULL
, NULL
, &ScratchSize
, Ranges
, RangeCount
);
323 UT_ASSERT_TRUE (RETURN_ERROR (Status
));
325 return UNIT_TEST_PASSED
;
329 Unit test of MtrrLib service IsMtrrSupported()
331 @param[in] Context Ignored
333 @retval UNIT_TEST_PASSED The Unit test has completed and the test
335 @retval UNIT_TEST_ERROR_TEST_FAILED A test case assertion has failed.
340 UnitTestIsMtrrSupported (
341 IN UNIT_TEST_CONTEXT Context
344 MTRR_LIB_SYSTEM_PARAMETER SystemParameter
;
345 MTRR_LIB_TEST_CONTEXT
*LocalContext
;
347 LocalContext
= (MTRR_LIB_TEST_CONTEXT
*) Context
;
349 CopyMem (&SystemParameter
, LocalContext
->SystemParameter
, sizeof (SystemParameter
));
351 // MTRR capability off in CPUID leaf.
353 SystemParameter
.MtrrSupported
= FALSE
;
354 InitializeMtrrRegs (&SystemParameter
);
355 UT_ASSERT_FALSE (IsMtrrSupported ());
358 // MTRR capability on in CPUID leaf, but no variable or fixed MTRRs.
360 SystemParameter
.MtrrSupported
= TRUE
;
361 SystemParameter
.VariableMtrrCount
= 0;
362 SystemParameter
.FixedMtrrSupported
= FALSE
;
363 InitializeMtrrRegs (&SystemParameter
);
364 UT_ASSERT_FALSE (IsMtrrSupported ());
367 // MTRR capability on in CPUID leaf, but no variable MTRRs.
369 SystemParameter
.MtrrSupported
= TRUE
;
370 SystemParameter
.VariableMtrrCount
= 0;
371 SystemParameter
.FixedMtrrSupported
= TRUE
;
372 InitializeMtrrRegs (&SystemParameter
);
373 UT_ASSERT_FALSE (IsMtrrSupported ());
376 // MTRR capability on in CPUID leaf, but no fixed MTRRs.
378 SystemParameter
.MtrrSupported
= TRUE
;
379 SystemParameter
.VariableMtrrCount
= 7;
380 SystemParameter
.FixedMtrrSupported
= FALSE
;
381 InitializeMtrrRegs (&SystemParameter
);
382 UT_ASSERT_FALSE (IsMtrrSupported ());
385 // MTRR capability on in CPUID leaf with both variable and fixed MTRRs.
387 SystemParameter
.MtrrSupported
= TRUE
;
388 SystemParameter
.VariableMtrrCount
= 7;
389 SystemParameter
.FixedMtrrSupported
= TRUE
;
390 InitializeMtrrRegs (&SystemParameter
);
391 UT_ASSERT_TRUE (IsMtrrSupported ());
393 return UNIT_TEST_PASSED
;
397 Unit test of MtrrLib service GetVariableMtrrCount()
399 @param[in] Context Ignored
401 @retval UNIT_TEST_PASSED The Unit test has completed and the test
403 @retval UNIT_TEST_ERROR_TEST_FAILED A test case assertion has failed.
408 UnitTestGetVariableMtrrCount (
409 IN UNIT_TEST_CONTEXT Context
413 MTRR_LIB_SYSTEM_PARAMETER SystemParameter
;
414 MTRR_LIB_TEST_CONTEXT
*LocalContext
;
416 LocalContext
= (MTRR_LIB_TEST_CONTEXT
*) Context
;
418 CopyMem (&SystemParameter
, LocalContext
->SystemParameter
, sizeof (SystemParameter
));
420 // If MTRR capability off in CPUID leaf, then the count is always 0.
422 SystemParameter
.MtrrSupported
= FALSE
;
423 for (SystemParameter
.VariableMtrrCount
= 1; SystemParameter
.VariableMtrrCount
<= MTRR_NUMBER_OF_VARIABLE_MTRR
; SystemParameter
.VariableMtrrCount
++) {
424 InitializeMtrrRegs (&SystemParameter
);
425 Result
= GetVariableMtrrCount ();
426 UT_ASSERT_EQUAL (Result
, 0);
430 // Try all supported variable MTRR counts.
431 // If variable MTRR count is > MTRR_NUMBER_OF_VARIABLE_MTRR, then an ASSERT()
434 SystemParameter
.MtrrSupported
= TRUE
;
435 for (SystemParameter
.VariableMtrrCount
= 1; SystemParameter
.VariableMtrrCount
<= MTRR_NUMBER_OF_VARIABLE_MTRR
; SystemParameter
.VariableMtrrCount
++) {
436 InitializeMtrrRegs (&SystemParameter
);
437 Result
= GetVariableMtrrCount ();
438 UT_ASSERT_EQUAL (Result
, SystemParameter
.VariableMtrrCount
);
442 // Expect ASSERT() if variable MTRR count is > MTRR_NUMBER_OF_VARIABLE_MTRR
444 SystemParameter
.VariableMtrrCount
= MTRR_NUMBER_OF_VARIABLE_MTRR
+ 1;
445 InitializeMtrrRegs (&SystemParameter
);
446 UT_EXPECT_ASSERT_FAILURE (GetVariableMtrrCount (), NULL
);
448 SystemParameter
.MtrrSupported
= TRUE
;
449 SystemParameter
.VariableMtrrCount
= MAX_UINT8
;
450 InitializeMtrrRegs (&SystemParameter
);
451 UT_EXPECT_ASSERT_FAILURE (GetVariableMtrrCount (), NULL
);
453 return UNIT_TEST_PASSED
;
457 Unit test of MtrrLib service GetFirmwareVariableMtrrCount()
459 @param[in] Context Ignored
461 @retval UNIT_TEST_PASSED The Unit test has completed and the test
463 @retval UNIT_TEST_ERROR_TEST_FAILED A test case assertion has failed.
468 UnitTestGetFirmwareVariableMtrrCount (
469 IN UNIT_TEST_CONTEXT Context
473 UINT32 ReservedMtrrs
;
474 MTRR_LIB_SYSTEM_PARAMETER SystemParameter
;
475 MTRR_LIB_GET_FIRMWARE_VARIABLE_MTRR_COUNT_CONTEXT
*LocalContext
;
477 LocalContext
= (MTRR_LIB_GET_FIRMWARE_VARIABLE_MTRR_COUNT_CONTEXT
*) Context
;
479 CopyMem (&SystemParameter
, LocalContext
->SystemParameter
, sizeof (SystemParameter
));
481 InitializeMtrrRegs (&SystemParameter
);
483 // Positive test cases for VCNT = 10 and Reserved PCD in range 0..10
485 for (ReservedMtrrs
= 0; ReservedMtrrs
<= SystemParameter
.VariableMtrrCount
; ReservedMtrrs
++) {
486 PatchPcdSet32 (PcdCpuNumberOfReservedVariableMtrrs
, ReservedMtrrs
);
487 Result
= GetFirmwareVariableMtrrCount ();
488 UT_ASSERT_EQUAL (Result
, SystemParameter
.VariableMtrrCount
- ReservedMtrrs
);
492 // Negative test cases when Reserved PCD is larger than VCNT
494 for (ReservedMtrrs
= SystemParameter
.VariableMtrrCount
+ 1; ReservedMtrrs
<= 255; ReservedMtrrs
++) {
495 PatchPcdSet32 (PcdCpuNumberOfReservedVariableMtrrs
, ReservedMtrrs
);
496 Result
= GetFirmwareVariableMtrrCount ();
497 UT_ASSERT_EQUAL (Result
, 0);
501 // Negative test cases when Reserved PCD is larger than VCNT
503 PatchPcdSet32 (PcdCpuNumberOfReservedVariableMtrrs
, MAX_UINT32
);
504 Result
= GetFirmwareVariableMtrrCount ();
505 UT_ASSERT_EQUAL (Result
, 0);
508 // Negative test case when MTRRs are not supported
510 SystemParameter
.MtrrSupported
= FALSE
;
511 InitializeMtrrRegs (&SystemParameter
);
512 PatchPcdSet32 (PcdCpuNumberOfReservedVariableMtrrs
, 2);
513 Result
= GetFirmwareVariableMtrrCount ();
514 UT_ASSERT_EQUAL (Result
, 0);
517 // Negative test case when Fixed MTRRs are not supported
519 SystemParameter
.MtrrSupported
= TRUE
;
520 SystemParameter
.FixedMtrrSupported
= FALSE
;
521 InitializeMtrrRegs (&SystemParameter
);
522 PatchPcdSet32 (PcdCpuNumberOfReservedVariableMtrrs
, 2);
523 Result
= GetFirmwareVariableMtrrCount ();
524 UT_ASSERT_EQUAL (Result
, 0);
527 // Expect ASSERT() if variable MTRR count is > MTRR_NUMBER_OF_VARIABLE_MTRR
529 SystemParameter
.FixedMtrrSupported
= TRUE
;
530 SystemParameter
.VariableMtrrCount
= MTRR_NUMBER_OF_VARIABLE_MTRR
+ 1;
531 InitializeMtrrRegs (&SystemParameter
);
532 UT_EXPECT_ASSERT_FAILURE (GetFirmwareVariableMtrrCount (), NULL
);
534 return UNIT_TEST_PASSED
;
538 Unit test of MtrrLib service MtrrGetMemoryAttribute()
540 @param[in] Context Ignored
542 @retval UNIT_TEST_PASSED The Unit test has completed and the test
544 @retval UNIT_TEST_ERROR_TEST_FAILED A test case assertion has failed.
549 UnitTestMtrrGetMemoryAttribute (
550 IN UNIT_TEST_CONTEXT Context
553 return UNIT_TEST_PASSED
;
557 Unit test of MtrrLib service MtrrGetFixedMtrr()
559 @param[in] Context Ignored
561 @retval UNIT_TEST_PASSED The Unit test has completed and the test
563 @retval UNIT_TEST_ERROR_TEST_FAILED A test case assertion has failed.
568 UnitTestMtrrGetFixedMtrr (
569 IN UNIT_TEST_CONTEXT Context
572 MTRR_FIXED_SETTINGS
*Result
;
573 MTRR_FIXED_SETTINGS ExpectedFixedSettings
;
574 MTRR_FIXED_SETTINGS FixedSettings
;
579 MTRR_LIB_SYSTEM_PARAMETER SystemParameter
;
580 MTRR_LIB_TEST_CONTEXT
*LocalContext
;
582 LocalContext
= (MTRR_LIB_TEST_CONTEXT
*) Context
;
584 CopyMem (&SystemParameter
, LocalContext
->SystemParameter
, sizeof (SystemParameter
));
585 InitializeMtrrRegs (&SystemParameter
);
587 // Set random cache type to different ranges under 1MB and make sure
588 // the fixed MTRR settings are expected.
591 for (Index
= 0; Index
< 100; Index
++) {
592 for (MsrIndex
= 0; MsrIndex
< ARRAY_SIZE (mFixedMtrrsIndex
); MsrIndex
++) {
594 for (ByteIndex
= 0; ByteIndex
< sizeof (UINT64
); ByteIndex
++) {
595 MsrValue
= MsrValue
| LShiftU64 (GenerateRandomCacheType (), ByteIndex
* 8);
597 ExpectedFixedSettings
.Mtrr
[MsrIndex
] = MsrValue
;
598 AsmWriteMsr64 (mFixedMtrrsIndex
[MsrIndex
], MsrValue
);
601 Result
= MtrrGetFixedMtrr (&FixedSettings
);
602 UT_ASSERT_EQUAL ((UINTN
)Result
, (UINTN
)&FixedSettings
);
603 UT_ASSERT_MEM_EQUAL (&FixedSettings
, &ExpectedFixedSettings
, sizeof (FixedSettings
));
607 // Negative test case when MTRRs are not supported
609 SystemParameter
.MtrrSupported
= FALSE
;
610 InitializeMtrrRegs (&SystemParameter
);
612 ZeroMem (&FixedSettings
, sizeof (FixedSettings
));
613 ZeroMem (&ExpectedFixedSettings
, sizeof (ExpectedFixedSettings
));
614 Result
= MtrrGetFixedMtrr (&FixedSettings
);
615 UT_ASSERT_EQUAL ((UINTN
)Result
, (UINTN
)&FixedSettings
);
616 UT_ASSERT_MEM_EQUAL (&ExpectedFixedSettings
, &FixedSettings
, sizeof (ExpectedFixedSettings
));
618 return UNIT_TEST_PASSED
;
622 Unit test of MtrrLib service MtrrGetAllMtrrs()
624 @param[in] Context Ignored
626 @retval UNIT_TEST_PASSED The Unit test has completed and the test
628 @retval UNIT_TEST_ERROR_TEST_FAILED A test case assertion has failed.
633 UnitTestMtrrGetAllMtrrs (
634 IN UNIT_TEST_CONTEXT Context
637 MTRR_SETTINGS
*Result
;
639 MTRR_SETTINGS ExpectedMtrrs
;
640 MTRR_VARIABLE_SETTING VariableMtrr
[MTRR_NUMBER_OF_VARIABLE_MTRR
];
642 MTRR_LIB_SYSTEM_PARAMETER SystemParameter
;
643 MTRR_LIB_TEST_CONTEXT
*LocalContext
;
645 LocalContext
= (MTRR_LIB_TEST_CONTEXT
*) Context
;
647 CopyMem (&SystemParameter
, LocalContext
->SystemParameter
, sizeof (SystemParameter
));
648 InitializeMtrrRegs (&SystemParameter
);
650 for (Index
= 0; Index
< SystemParameter
.VariableMtrrCount
; Index
++) {
651 GenerateRandomMtrrPair (SystemParameter
.PhysicalAddressBits
, GenerateRandomCacheType (), &VariableMtrr
[Index
], NULL
);
652 AsmWriteMsr64 (MSR_IA32_MTRR_PHYSBASE0
+ (Index
<< 1), VariableMtrr
[Index
].Base
);
653 AsmWriteMsr64 (MSR_IA32_MTRR_PHYSMASK0
+ (Index
<< 1), VariableMtrr
[Index
].Mask
);
655 Result
= MtrrGetAllMtrrs (&Mtrrs
);
656 UT_ASSERT_EQUAL ((UINTN
)Result
, (UINTN
)&Mtrrs
);
657 UT_ASSERT_MEM_EQUAL (Mtrrs
.Variables
.Mtrr
, VariableMtrr
, sizeof (MTRR_VARIABLE_SETTING
) * SystemParameter
.VariableMtrrCount
);
660 // Negative test case when MTRRs are not supported
662 ZeroMem (&ExpectedMtrrs
, sizeof (ExpectedMtrrs
));
663 ZeroMem (&Mtrrs
, sizeof (Mtrrs
));
665 SystemParameter
.MtrrSupported
= FALSE
;
666 InitializeMtrrRegs (&SystemParameter
);
667 Result
= MtrrGetAllMtrrs (&Mtrrs
);
668 UT_ASSERT_EQUAL ((UINTN
)Result
, (UINTN
)&Mtrrs
);
669 UT_ASSERT_MEM_EQUAL (&ExpectedMtrrs
, &Mtrrs
, sizeof (ExpectedMtrrs
));
672 // Expect ASSERT() if variable MTRR count is > MTRR_NUMBER_OF_VARIABLE_MTRR
674 SystemParameter
.MtrrSupported
= TRUE
;
675 SystemParameter
.VariableMtrrCount
= MTRR_NUMBER_OF_VARIABLE_MTRR
+ 1;
676 InitializeMtrrRegs (&SystemParameter
);
677 UT_EXPECT_ASSERT_FAILURE (MtrrGetAllMtrrs (&Mtrrs
), NULL
);
679 return UNIT_TEST_PASSED
;
683 Unit test of MtrrLib service MtrrSetAllMtrrs()
685 @param[in] Context Ignored
687 @retval UNIT_TEST_PASSED The Unit test has completed and the test
689 @retval UNIT_TEST_ERROR_TEST_FAILED A test case assertion has failed.
694 UnitTestMtrrSetAllMtrrs (
695 IN UNIT_TEST_CONTEXT Context
698 MTRR_SETTINGS
*Result
;
701 MSR_IA32_MTRR_DEF_TYPE_REGISTER Default
;
702 MTRR_LIB_SYSTEM_PARAMETER SystemParameter
;
703 MTRR_LIB_TEST_CONTEXT
*LocalContext
;
705 LocalContext
= (MTRR_LIB_TEST_CONTEXT
*) Context
;
707 CopyMem (&SystemParameter
, LocalContext
->SystemParameter
, sizeof (SystemParameter
));
708 InitializeMtrrRegs (&SystemParameter
);
713 Default
.Bits
.Type
= GenerateRandomCacheType ();
715 ZeroMem (&Mtrrs
, sizeof (Mtrrs
));
716 Mtrrs
.MtrrDefType
= Default
.Uint64
;
717 for (Index
= 0; Index
< SystemParameter
.VariableMtrrCount
; Index
++) {
718 GenerateRandomMtrrPair (SystemParameter
.PhysicalAddressBits
, GenerateRandomCacheType (), &Mtrrs
.Variables
.Mtrr
[Index
], NULL
);
720 Result
= MtrrSetAllMtrrs (&Mtrrs
);
721 UT_ASSERT_EQUAL ((UINTN
)Result
, (UINTN
)&Mtrrs
);
723 UT_ASSERT_EQUAL (AsmReadMsr64 (MSR_IA32_MTRR_DEF_TYPE
), Mtrrs
.MtrrDefType
);
724 for (Index
= 0; Index
< SystemParameter
.VariableMtrrCount
; Index
++) {
725 UT_ASSERT_EQUAL (AsmReadMsr64 (MSR_IA32_MTRR_PHYSBASE0
+ (Index
<< 1)), Mtrrs
.Variables
.Mtrr
[Index
].Base
);
726 UT_ASSERT_EQUAL (AsmReadMsr64 (MSR_IA32_MTRR_PHYSMASK0
+ (Index
<< 1)), Mtrrs
.Variables
.Mtrr
[Index
].Mask
);
729 return UNIT_TEST_PASSED
;
733 Unit test of MtrrLib service MtrrGetMemoryAttributeInVariableMtrr()
735 @param[in] Context Ignored
737 @retval UNIT_TEST_PASSED The Unit test has completed and the test
739 @retval UNIT_TEST_ERROR_TEST_FAILED A test case assertion has failed.
744 UnitTestMtrrGetMemoryAttributeInVariableMtrr (
745 IN UNIT_TEST_CONTEXT Context
748 MTRR_LIB_TEST_CONTEXT
*LocalContext
;
749 MTRR_LIB_SYSTEM_PARAMETER SystemParameter
;
751 MTRR_VARIABLE_SETTING VariableSetting
[MTRR_NUMBER_OF_VARIABLE_MTRR
];
752 VARIABLE_MTRR VariableMtrr
[MTRR_NUMBER_OF_VARIABLE_MTRR
];
753 UINT64 ValidMtrrBitsMask
;
754 UINT64 ValidMtrrAddressMask
;
756 MSR_IA32_MTRR_PHYSBASE_REGISTER Base
;
757 MSR_IA32_MTRR_PHYSMASK_REGISTER Mask
;
759 LocalContext
= (MTRR_LIB_TEST_CONTEXT
*) Context
;
761 CopyMem (&SystemParameter
, LocalContext
->SystemParameter
, sizeof (SystemParameter
));
763 InitializeMtrrRegs (&SystemParameter
);
765 ValidMtrrBitsMask
= (1ull << SystemParameter
.PhysicalAddressBits
) - 1;
766 ValidMtrrAddressMask
= ValidMtrrBitsMask
& 0xfffffffffffff000ULL
;
768 for (Index
= 0; Index
< SystemParameter
.VariableMtrrCount
; Index
++) {
769 GenerateRandomMtrrPair (SystemParameter
.PhysicalAddressBits
, GenerateRandomCacheType (), &VariableSetting
[Index
], NULL
);
770 AsmWriteMsr64 (MSR_IA32_MTRR_PHYSBASE0
+ (Index
<< 1), VariableSetting
[Index
].Base
);
771 AsmWriteMsr64 (MSR_IA32_MTRR_PHYSMASK0
+ (Index
<< 1), VariableSetting
[Index
].Mask
);
773 Result
= MtrrGetMemoryAttributeInVariableMtrr (ValidMtrrBitsMask
, ValidMtrrAddressMask
, VariableMtrr
);
774 UT_ASSERT_EQUAL (Result
, SystemParameter
.VariableMtrrCount
);
776 for (Index
= 0; Index
< SystemParameter
.VariableMtrrCount
; Index
++) {
777 Base
.Uint64
= VariableMtrr
[Index
].BaseAddress
;
778 Base
.Bits
.Type
= (UINT32
) VariableMtrr
[Index
].Type
;
779 UT_ASSERT_EQUAL (Base
.Uint64
, VariableSetting
[Index
].Base
);
781 Mask
.Uint64
= ~(VariableMtrr
[Index
].Length
- 1) & ValidMtrrBitsMask
;
783 UT_ASSERT_EQUAL (Mask
.Uint64
, VariableSetting
[Index
].Mask
);
787 // Negative test case when MTRRs are not supported
789 SystemParameter
.MtrrSupported
= FALSE
;
790 InitializeMtrrRegs (&SystemParameter
);
791 Result
= MtrrGetMemoryAttributeInVariableMtrr (ValidMtrrBitsMask
, ValidMtrrAddressMask
, VariableMtrr
);
792 UT_ASSERT_EQUAL (Result
, 0);
795 // Expect ASSERT() if variable MTRR count is > MTRR_NUMBER_OF_VARIABLE_MTRR
797 SystemParameter
.MtrrSupported
= TRUE
;
798 SystemParameter
.VariableMtrrCount
= MTRR_NUMBER_OF_VARIABLE_MTRR
+ 1;
799 InitializeMtrrRegs (&SystemParameter
);
800 UT_EXPECT_ASSERT_FAILURE (MtrrGetMemoryAttributeInVariableMtrr (ValidMtrrBitsMask
, ValidMtrrAddressMask
, VariableMtrr
), NULL
);
802 return UNIT_TEST_PASSED
;
806 Unit test of MtrrLib service MtrrDebugPrintAllMtrrs()
808 @param[in] Context Ignored
810 @retval UNIT_TEST_PASSED The Unit test has completed and the test
812 @retval UNIT_TEST_ERROR_TEST_FAILED A test case assertion has failed.
817 UnitTestMtrrDebugPrintAllMtrrs (
818 IN UNIT_TEST_CONTEXT Context
821 return UNIT_TEST_PASSED
;
825 Unit test of MtrrLib service MtrrGetDefaultMemoryType().
827 @param[in] Context Ignored
829 @retval UNIT_TEST_PASSED The Unit test has completed and the test
831 @retval UNIT_TEST_ERROR_TEST_FAILED A test case assertion has failed.
836 UnitTestMtrrGetDefaultMemoryType (
837 IN UNIT_TEST_CONTEXT Context
840 MTRR_LIB_TEST_CONTEXT
*LocalContext
;
842 MTRR_MEMORY_CACHE_TYPE Result
;
843 MTRR_LIB_SYSTEM_PARAMETER SystemParameter
;
844 MTRR_MEMORY_CACHE_TYPE CacheType
[5];
846 CacheType
[0] = CacheUncacheable
;
847 CacheType
[1] = CacheWriteCombining
;
848 CacheType
[2] = CacheWriteThrough
;
849 CacheType
[3] = CacheWriteProtected
;
850 CacheType
[4] = CacheWriteBack
;
852 LocalContext
= (MTRR_LIB_TEST_CONTEXT
*) Context
;
854 CopyMem (&SystemParameter
, LocalContext
->SystemParameter
, sizeof (SystemParameter
));
856 // If MTRRs are supported, then always return the cache type in the MSR
857 // MSR_IA32_MTRR_DEF_TYPE
859 for (Index
= 0; Index
< ARRAY_SIZE (CacheType
); Index
++) {
860 SystemParameter
.DefaultCacheType
= CacheType
[Index
];
861 InitializeMtrrRegs (&SystemParameter
);
862 Result
= MtrrGetDefaultMemoryType ();
863 UT_ASSERT_EQUAL (Result
, SystemParameter
.DefaultCacheType
);
867 // If MTRRs are not supported, then always return CacheUncacheable
869 SystemParameter
.MtrrSupported
= FALSE
;
870 InitializeMtrrRegs (&SystemParameter
);
871 Result
= MtrrGetDefaultMemoryType ();
872 UT_ASSERT_EQUAL (Result
, CacheUncacheable
);
874 SystemParameter
.MtrrSupported
= TRUE
;
875 SystemParameter
.FixedMtrrSupported
= FALSE
;
876 InitializeMtrrRegs (&SystemParameter
);
877 Result
= MtrrGetDefaultMemoryType ();
878 UT_ASSERT_EQUAL (Result
, CacheUncacheable
);
880 SystemParameter
.MtrrSupported
= TRUE
;
881 SystemParameter
.FixedMtrrSupported
= TRUE
;
882 SystemParameter
.VariableMtrrCount
= 0;
883 InitializeMtrrRegs (&SystemParameter
);
884 Result
= MtrrGetDefaultMemoryType ();
885 UT_ASSERT_EQUAL (Result
, CacheUncacheable
);
887 return UNIT_TEST_PASSED
;
891 Unit test of MtrrLib service MtrrSetMemoryAttributeInMtrrSettings().
893 @param[in] Context Ignored
895 @retval UNIT_TEST_PASSED The Unit test has completed and the test
897 @retval UNIT_TEST_ERROR_TEST_FAILED A test case assertion has failed.
902 UnitTestMtrrSetMemoryAttributeInMtrrSettings (
903 IN UNIT_TEST_CONTEXT Context
906 CONST MTRR_LIB_SYSTEM_PARAMETER
*SystemParameter
;
907 RETURN_STATUS Status
;
916 MTRR_SETTINGS LocalMtrrs
;
918 MTRR_MEMORY_RANGE RawMtrrRange
[MTRR_NUMBER_OF_VARIABLE_MTRR
];
919 MTRR_MEMORY_RANGE ExpectedMemoryRanges
[MTRR_NUMBER_OF_FIXED_MTRR
* sizeof (UINT64
) + 2 * MTRR_NUMBER_OF_VARIABLE_MTRR
+ 1];
920 UINT32 ExpectedVariableMtrrUsage
;
921 UINTN ExpectedMemoryRangesCount
;
923 MTRR_MEMORY_RANGE ActualMemoryRanges
[MTRR_NUMBER_OF_FIXED_MTRR
* sizeof (UINT64
) + 2 * MTRR_NUMBER_OF_VARIABLE_MTRR
+ 1];
924 UINT32 ActualVariableMtrrUsage
;
925 UINTN ActualMemoryRangesCount
;
927 MTRR_SETTINGS
*Mtrrs
[2];
929 SystemParameter
= (MTRR_LIB_SYSTEM_PARAMETER
*) Context
;
930 GenerateRandomMemoryTypeCombination (
931 SystemParameter
->VariableMtrrCount
- PatchPcdGet32 (PcdCpuNumberOfReservedVariableMtrrs
),
932 &UcCount
, &WtCount
, &WbCount
, &WpCount
, &WcCount
934 GenerateValidAndConfigurableMtrrPairs (
935 SystemParameter
->PhysicalAddressBits
, RawMtrrRange
,
936 UcCount
, WtCount
, WbCount
, WpCount
, WcCount
939 ExpectedVariableMtrrUsage
= UcCount
+ WtCount
+ WbCount
+ WpCount
+ WcCount
;
940 ExpectedMemoryRangesCount
= ARRAY_SIZE (ExpectedMemoryRanges
);
941 GetEffectiveMemoryRanges (
942 SystemParameter
->DefaultCacheType
,
943 SystemParameter
->PhysicalAddressBits
,
944 RawMtrrRange
, ExpectedVariableMtrrUsage
,
945 ExpectedMemoryRanges
, &ExpectedMemoryRangesCount
948 UT_LOG_INFO ("--- Expected Memory Ranges [%d] ---\n", ExpectedMemoryRangesCount
);
949 DumpMemoryRanges (ExpectedMemoryRanges
, ExpectedMemoryRangesCount
);
951 // Default cache type is always an INPUT
953 ZeroMem (&LocalMtrrs
, sizeof (LocalMtrrs
));
954 LocalMtrrs
.MtrrDefType
= MtrrGetDefaultMemoryType ();
955 Mtrrs
[0] = &LocalMtrrs
;
958 for (MtrrIndex
= 0; MtrrIndex
< ARRAY_SIZE (Mtrrs
); MtrrIndex
++) {
959 for (Index
= 0; Index
< ExpectedMemoryRangesCount
; Index
++) {
960 Status
= MtrrSetMemoryAttributeInMtrrSettings (
962 ExpectedMemoryRanges
[Index
].BaseAddress
,
963 ExpectedMemoryRanges
[Index
].Length
,
964 ExpectedMemoryRanges
[Index
].Type
966 UT_ASSERT_TRUE (Status
== RETURN_SUCCESS
|| Status
== RETURN_OUT_OF_RESOURCES
|| Status
== RETURN_BUFFER_TOO_SMALL
);
967 if (Status
== RETURN_OUT_OF_RESOURCES
|| Status
== RETURN_BUFFER_TOO_SMALL
) {
968 return UNIT_TEST_SKIPPED
;
972 if (Mtrrs
[MtrrIndex
] == NULL
) {
973 ZeroMem (&LocalMtrrs
, sizeof (LocalMtrrs
));
974 MtrrGetAllMtrrs (&LocalMtrrs
);
976 ActualMemoryRangesCount
= ARRAY_SIZE (ActualMemoryRanges
);
978 SystemParameter
->DefaultCacheType
, SystemParameter
->PhysicalAddressBits
, SystemParameter
->VariableMtrrCount
,
979 &LocalMtrrs
, ActualMemoryRanges
, &ActualMemoryRangesCount
, &ActualVariableMtrrUsage
981 UT_LOG_INFO ("--- Actual Memory Ranges [%d] ---\n", ActualMemoryRangesCount
);
982 DumpMemoryRanges (ActualMemoryRanges
, ActualMemoryRangesCount
);
983 VerifyMemoryRanges (ExpectedMemoryRanges
, ExpectedMemoryRangesCount
, ActualMemoryRanges
, ActualMemoryRangesCount
);
984 UT_ASSERT_TRUE (ExpectedVariableMtrrUsage
>= ActualVariableMtrrUsage
);
986 ZeroMem (&LocalMtrrs
, sizeof (LocalMtrrs
));
989 return UNIT_TEST_PASSED
;
994 Prep routine for UnitTestGetFirmwareVariableMtrrCount().
996 @param Context Point to a UINT32 data to save the PcdCpuNumberOfReservedVariableMtrrs.
1001 UNIT_TEST_CONTEXT Context
1004 MTRR_LIB_GET_FIRMWARE_VARIABLE_MTRR_COUNT_CONTEXT
*LocalContext
;
1006 LocalContext
= (MTRR_LIB_GET_FIRMWARE_VARIABLE_MTRR_COUNT_CONTEXT
*) Context
;
1007 LocalContext
->NumberOfReservedVariableMtrrs
= PatchPcdGet32 (PcdCpuNumberOfReservedVariableMtrrs
);
1008 return UNIT_TEST_PASSED
;
1012 Clean up routine for UnitTestGetFirmwareVariableMtrrCount().
1014 @param Context Point to a UINT32 data to save the PcdCpuNumberOfReservedVariableMtrrs.
1019 UNIT_TEST_CONTEXT Context
1022 MTRR_LIB_GET_FIRMWARE_VARIABLE_MTRR_COUNT_CONTEXT
*LocalContext
;
1024 LocalContext
= (MTRR_LIB_GET_FIRMWARE_VARIABLE_MTRR_COUNT_CONTEXT
*) Context
;
1025 PatchPcdSet32 (PcdCpuNumberOfReservedVariableMtrrs
, LocalContext
->NumberOfReservedVariableMtrrs
);
1029 Initialize the unit test framework, suite, and unit tests for the
1030 ResetSystemLib and run the ResetSystemLib unit test.
1032 @param Iteration Iteration of testing MtrrSetMemoryAttributeInMtrrSettings
1033 and MtrrSetMemoryAttributesInMtrrSettings using random inputs.
1035 @retval EFI_SUCCESS All test cases were dispatched.
1036 @retval EFI_OUT_OF_RESOURCES There are not enough resources available to
1037 initialize the unit tests.
1047 UNIT_TEST_FRAMEWORK_HANDLE Framework
;
1048 UNIT_TEST_SUITE_HANDLE MtrrApiTests
;
1051 MTRR_LIB_TEST_CONTEXT Context
;
1052 MTRR_LIB_GET_FIRMWARE_VARIABLE_MTRR_COUNT_CONTEXT GetFirmwareVariableMtrrCountContext
;
1054 Context
.SystemParameter
= &mDefaultSystemParameter
;
1055 GetFirmwareVariableMtrrCountContext
.SystemParameter
= &mDefaultSystemParameter
;
1059 // Setup the test framework for running the tests.
1061 Status
= InitUnitTestFramework (&Framework
, UNIT_TEST_APP_NAME
, gEfiCallerBaseName
, UNIT_TEST_APP_VERSION
);
1062 if (EFI_ERROR (Status
)) {
1063 DEBUG ((DEBUG_ERROR
, "Failed in InitUnitTestFramework. Status = %r\n", Status
));
1068 // --------------Suite-----------Description--------------Name----------Function--------Pre---Post-------------------Context-----------
1072 // Populate the MtrrLib API Unit Test Suite.
1074 Status
= CreateUnitTestSuite (&MtrrApiTests
, Framework
, "MtrrLib API Tests", "MtrrLib.MtrrLib", NULL
, NULL
);
1075 if (EFI_ERROR (Status
)) {
1076 DEBUG ((DEBUG_ERROR
, "Failed in CreateUnitTestSuite for MtrrLib API Tests\n"));
1077 Status
= EFI_OUT_OF_RESOURCES
;
1080 AddTestCase (MtrrApiTests
, "Test IsMtrrSupported", "MtrrSupported", UnitTestIsMtrrSupported
, NULL
, NULL
, &Context
);
1081 AddTestCase (MtrrApiTests
, "Test GetVariableMtrrCount", "GetVariableMtrrCount", UnitTestGetVariableMtrrCount
, NULL
, NULL
, &Context
);
1082 AddTestCase (MtrrApiTests
, "Test GetFirmwareVariableMtrrCount", "GetFirmwareVariableMtrrCount", UnitTestGetFirmwareVariableMtrrCount
, SavePcdValue
, RestorePcdValue
, &GetFirmwareVariableMtrrCountContext
);
1083 AddTestCase (MtrrApiTests
, "Test MtrrGetMemoryAttribute", "MtrrGetMemoryAttribute", UnitTestMtrrGetMemoryAttribute
, NULL
, NULL
, &Context
);
1084 AddTestCase (MtrrApiTests
, "Test MtrrGetFixedMtrr", "MtrrGetFixedMtrr", UnitTestMtrrGetFixedMtrr
, NULL
, NULL
, &Context
);
1085 AddTestCase (MtrrApiTests
, "Test MtrrGetAllMtrrs", "MtrrGetAllMtrrs", UnitTestMtrrGetAllMtrrs
, NULL
, NULL
, &Context
);
1086 AddTestCase (MtrrApiTests
, "Test MtrrSetAllMtrrs", "MtrrSetAllMtrrs", UnitTestMtrrSetAllMtrrs
, NULL
, NULL
, &Context
);
1087 AddTestCase (MtrrApiTests
, "Test MtrrGetMemoryAttributeInVariableMtrr", "MtrrGetMemoryAttributeInVariableMtrr", UnitTestMtrrGetMemoryAttributeInVariableMtrr
, NULL
, NULL
, &Context
);
1088 AddTestCase (MtrrApiTests
, "Test MtrrDebugPrintAllMtrrs", "MtrrDebugPrintAllMtrrs", UnitTestMtrrDebugPrintAllMtrrs
, NULL
, NULL
, &Context
);
1089 AddTestCase (MtrrApiTests
, "Test MtrrGetDefaultMemoryType", "MtrrGetDefaultMemoryType", UnitTestMtrrGetDefaultMemoryType
, NULL
, NULL
, &Context
);
1091 for (SystemIndex
= 0; SystemIndex
< ARRAY_SIZE (mSystemParameters
); SystemIndex
++) {
1092 for (Index
= 0; Index
< Iteration
; Index
++) {
1093 AddTestCase (MtrrApiTests
, "Test InvalidMemoryLayouts", "InvalidMemoryLayouts", UnitTestInvalidMemoryLayouts
, InitializeSystem
, NULL
, &mSystemParameters
[SystemIndex
]);
1094 AddTestCase (MtrrApiTests
, "Test MtrrSetMemoryAttributeInMtrrSettings", "MtrrSetMemoryAttributeInMtrrSettings", UnitTestMtrrSetMemoryAttributeInMtrrSettings
, InitializeSystem
, NULL
, &mSystemParameters
[SystemIndex
]);
1095 AddTestCase (MtrrApiTests
, "Test MtrrSetMemoryAttributesInMtrrSettings", "MtrrSetMemoryAttributesInMtrrSettings", UnitTestMtrrSetMemoryAttributesInMtrrSettings
, InitializeSystem
, NULL
, &mSystemParameters
[SystemIndex
]);
1099 // Execute the tests.
1101 Status
= RunAllTestSuites (Framework
);
1104 if (Framework
!= NULL
) {
1105 FreeUnitTestFramework (Framework
);
1112 Standard POSIX C entry point for host based unit test execution.
1114 @param Argc Number of arguments.
1115 @param Argv Array of arguments.
1117 @return Test application exit code.
1127 DEBUG ((DEBUG_INFO
, "%a v%a\n", UNIT_TEST_APP_NAME
, UNIT_TEST_APP_VERSION
));
1128 srand ((unsigned int) time (NULL
));
1131 // MtrrLibUnitTest generate-random-numbers <path to MtrrLib/UnitTest/RandomNumber.c> <random-number count>
1133 if ((Argc
== 4) && (AsciiStriCmp ("generate-random-numbers", Argv
[1]) == 0)) {
1134 Count
= atoi (Argv
[3]);
1135 DEBUG ((DEBUG_INFO
, "Generate %d random numbers to %a.\n", Count
, Argv
[2]));
1136 GenerateRandomNumbers (Argv
[2], Count
);
1141 // MtrrLibUnitTest [<iterations>]
1142 // <iterations> [fixed|random]
1143 // Default <iterations> is 10.
1144 // Default uses fixed inputs.
1147 mRandomInput
= FALSE
;
1148 if ((Argc
== 2) || (Argc
== 3)) {
1149 Count
= atoi (Argv
[1]);
1151 if (AsciiStriCmp ("fixed", Argv
[2]) == 0) {
1152 mRandomInput
= FALSE
;
1153 } else if (AsciiStriCmp ("random", Argv
[2]) == 0) {
1154 mRandomInput
= TRUE
;
1159 DEBUG ((DEBUG_INFO
, "Iterations = %d\n", Count
));
1160 DEBUG ((DEBUG_INFO
, "Input = %a\n", mRandomInput
? "random" : "fixed"));
1162 return UnitTestingEntry (Count
);