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
91 UT_ASSERT_EQUAL (ExpectedMemoryRangeCount
, ActualRangeCount
);
92 for (Index
= 0; Index
< ExpectedMemoryRangeCount
; Index
++) {
93 UT_ASSERT_EQUAL (ExpectedMemoryRanges
[Index
].BaseAddress
, ActualRanges
[Index
].BaseAddress
);
94 UT_ASSERT_EQUAL (ExpectedMemoryRanges
[Index
].Length
, ActualRanges
[Index
].Length
);
95 UT_ASSERT_EQUAL (ExpectedMemoryRanges
[Index
].Type
, ActualRanges
[Index
].Type
);
98 return UNIT_TEST_PASSED
;
102 Dump the memory ranges.
104 @param Ranges Memory ranges to dump.
105 @param RangeCount Count of memory ranges.
109 MTRR_MEMORY_RANGE
*Ranges
,
115 for (Index
= 0; Index
< RangeCount
; Index
++) {
116 UT_LOG_INFO ("\t{ 0x%016llx, 0x%016llx, %a },\n", Ranges
[Index
].BaseAddress
, Ranges
[Index
].Length
, mCacheDescription
[Ranges
[Index
].Type
]);
124 Generate random count of MTRRs for each cache type.
126 @param TotalCount Total MTRR count.
127 @param UcCount Return count of Uncacheable type.
128 @param WtCount Return count of Write Through type.
129 @param WbCount Return count of Write Back type.
130 @param WpCount Return count of Write Protected type.
131 @param WcCount Return count of Write Combining type.
134 GenerateRandomMemoryTypeCombination (
135 IN UINT32 TotalCount
,
144 UINT32 TotalMtrrCount
;
145 UINT32
*CountPerType
[5];
147 CountPerType
[0] = UcCount
;
148 CountPerType
[1] = WtCount
;
149 CountPerType
[2] = WbCount
;
150 CountPerType
[3] = WpCount
;
151 CountPerType
[4] = WcCount
;
154 // Initialize the count of each cache type to 0.
156 for (Index
= 0; Index
< ARRAY_SIZE (CountPerType
); Index
++) {
157 *(CountPerType
[Index
]) = 0;
161 // Pick a random count of MTRRs
163 TotalMtrrCount
= Random32 (1, TotalCount
);
164 for (Index
= 0; Index
< TotalMtrrCount
; Index
++) {
166 // For each of them, pick a random cache type.
168 (*(CountPerType
[Random32 (0, ARRAY_SIZE (CountPerType
) - 1)]))++;
173 Unit test of MtrrLib service MtrrSetMemoryAttribute()
175 @param[in] Context Ignored
177 @retval UNIT_TEST_PASSED The Unit test has completed and the test
179 @retval UNIT_TEST_ERROR_TEST_FAILED A test case assertion has failed.
184 UnitTestMtrrSetMemoryAttributesInMtrrSettings (
185 IN UNIT_TEST_CONTEXT Context
188 CONST MTRR_LIB_SYSTEM_PARAMETER
*SystemParameter
;
189 RETURN_STATUS Status
;
199 MTRR_SETTINGS LocalMtrrs
;
201 MTRR_MEMORY_RANGE RawMtrrRange
[MTRR_NUMBER_OF_VARIABLE_MTRR
];
202 MTRR_MEMORY_RANGE ExpectedMemoryRanges
[MTRR_NUMBER_OF_FIXED_MTRR
* sizeof (UINT64
) + 2 * MTRR_NUMBER_OF_VARIABLE_MTRR
+ 1];
203 UINT32 ExpectedVariableMtrrUsage
;
204 UINTN ExpectedMemoryRangesCount
;
206 MTRR_MEMORY_RANGE ActualMemoryRanges
[MTRR_NUMBER_OF_FIXED_MTRR
* sizeof (UINT64
) + 2 * MTRR_NUMBER_OF_VARIABLE_MTRR
+ 1];
207 UINT32 ActualVariableMtrrUsage
;
208 UINTN ActualMemoryRangesCount
;
210 MTRR_SETTINGS
*Mtrrs
[2];
212 SystemParameter
= (MTRR_LIB_SYSTEM_PARAMETER
*)Context
;
213 GenerateRandomMemoryTypeCombination (
214 SystemParameter
->VariableMtrrCount
- PatchPcdGet32 (PcdCpuNumberOfReservedVariableMtrrs
),
221 GenerateValidAndConfigurableMtrrPairs (
222 SystemParameter
->PhysicalAddressBits
,
231 ExpectedVariableMtrrUsage
= UcCount
+ WtCount
+ WbCount
+ WpCount
+ WcCount
;
232 ExpectedMemoryRangesCount
= ARRAY_SIZE (ExpectedMemoryRanges
);
233 GetEffectiveMemoryRanges (
234 SystemParameter
->DefaultCacheType
,
235 SystemParameter
->PhysicalAddressBits
,
237 ExpectedVariableMtrrUsage
,
238 ExpectedMemoryRanges
,
239 &ExpectedMemoryRangesCount
243 "Total MTRR [%d]: UC=%d, WT=%d, WB=%d, WP=%d, WC=%d\n",
244 ExpectedVariableMtrrUsage
,
251 UT_LOG_INFO ("--- Expected Memory Ranges [%d] ---\n", ExpectedMemoryRangesCount
);
252 DumpMemoryRanges (ExpectedMemoryRanges
, ExpectedMemoryRangesCount
);
255 // Default cache type is always an INPUT
257 ZeroMem (&LocalMtrrs
, sizeof (LocalMtrrs
));
258 LocalMtrrs
.MtrrDefType
= MtrrGetDefaultMemoryType ();
259 ScratchSize
= SCRATCH_BUFFER_SIZE
;
260 Mtrrs
[0] = &LocalMtrrs
;
263 for (MtrrIndex
= 0; MtrrIndex
< ARRAY_SIZE (Mtrrs
); MtrrIndex
++) {
264 Scratch
= calloc (ScratchSize
, sizeof (UINT8
));
265 Status
= MtrrSetMemoryAttributesInMtrrSettings (Mtrrs
[MtrrIndex
], Scratch
, &ScratchSize
, ExpectedMemoryRanges
, ExpectedMemoryRangesCount
);
266 if (Status
== RETURN_BUFFER_TOO_SMALL
) {
267 Scratch
= realloc (Scratch
, ScratchSize
);
268 Status
= MtrrSetMemoryAttributesInMtrrSettings (Mtrrs
[MtrrIndex
], Scratch
, &ScratchSize
, ExpectedMemoryRanges
, ExpectedMemoryRangesCount
);
271 UT_ASSERT_STATUS_EQUAL (Status
, RETURN_SUCCESS
);
273 if (Mtrrs
[MtrrIndex
] == NULL
) {
274 ZeroMem (&LocalMtrrs
, sizeof (LocalMtrrs
));
275 MtrrGetAllMtrrs (&LocalMtrrs
);
278 ActualMemoryRangesCount
= ARRAY_SIZE (ActualMemoryRanges
);
280 SystemParameter
->DefaultCacheType
,
281 SystemParameter
->PhysicalAddressBits
,
282 SystemParameter
->VariableMtrrCount
,
285 &ActualMemoryRangesCount
,
286 &ActualVariableMtrrUsage
289 UT_LOG_INFO ("--- Actual Memory Ranges [%d] ---\n", ActualMemoryRangesCount
);
290 DumpMemoryRanges (ActualMemoryRanges
, ActualMemoryRangesCount
);
291 VerifyMemoryRanges (ExpectedMemoryRanges
, ExpectedMemoryRangesCount
, ActualMemoryRanges
, ActualMemoryRangesCount
);
292 UT_ASSERT_TRUE (ExpectedVariableMtrrUsage
>= ActualVariableMtrrUsage
);
294 ZeroMem (&LocalMtrrs
, sizeof (LocalMtrrs
));
299 return UNIT_TEST_PASSED
;
303 Test routine to check whether invalid base/size can be rejected.
305 @param Context Pointer to MTRR_LIB_SYSTEM_PARAMETER.
311 UnitTestInvalidMemoryLayouts (
312 IN UNIT_TEST_CONTEXT Context
315 CONST MTRR_LIB_SYSTEM_PARAMETER
*SystemParameter
;
316 MTRR_MEMORY_RANGE Ranges
[MTRR_NUMBER_OF_VARIABLE_MTRR
* 2 + 1];
322 RETURN_STATUS Status
;
325 SystemParameter
= (MTRR_LIB_SYSTEM_PARAMETER
*)Context
;
327 RangeCount
= Random32 (1, ARRAY_SIZE (Ranges
));
328 MaxAddress
= 1ull << SystemParameter
->PhysicalAddressBits
;
330 for (Index
= 0; Index
< RangeCount
; Index
++) {
332 BaseAddress
= Random64 (0, MaxAddress
);
333 Length
= Random64 (1, MaxAddress
- BaseAddress
);
334 } while (((BaseAddress
& 0xFFF) == 0) || ((Length
& 0xFFF) == 0));
336 Ranges
[Index
].BaseAddress
= BaseAddress
;
337 Ranges
[Index
].Length
= Length
;
338 Ranges
[Index
].Type
= GenerateRandomCacheType ();
340 Status
= MtrrSetMemoryAttribute (
341 Ranges
[Index
].BaseAddress
,
342 Ranges
[Index
].Length
,
345 UT_ASSERT_TRUE (RETURN_ERROR (Status
));
349 Status
= MtrrSetMemoryAttributesInMtrrSettings (NULL
, NULL
, &ScratchSize
, Ranges
, RangeCount
);
350 UT_ASSERT_TRUE (RETURN_ERROR (Status
));
352 return UNIT_TEST_PASSED
;
356 Unit test of MtrrLib service IsMtrrSupported()
358 @param[in] Context Ignored
360 @retval UNIT_TEST_PASSED The Unit test has completed and the test
362 @retval UNIT_TEST_ERROR_TEST_FAILED A test case assertion has failed.
367 UnitTestIsMtrrSupported (
368 IN UNIT_TEST_CONTEXT Context
371 MTRR_LIB_SYSTEM_PARAMETER SystemParameter
;
372 MTRR_LIB_TEST_CONTEXT
*LocalContext
;
374 LocalContext
= (MTRR_LIB_TEST_CONTEXT
*)Context
;
376 CopyMem (&SystemParameter
, LocalContext
->SystemParameter
, sizeof (SystemParameter
));
378 // MTRR capability off in CPUID leaf.
380 SystemParameter
.MtrrSupported
= FALSE
;
381 InitializeMtrrRegs (&SystemParameter
);
382 UT_ASSERT_FALSE (IsMtrrSupported ());
385 // MTRR capability on in CPUID leaf, but no variable or fixed MTRRs.
387 SystemParameter
.MtrrSupported
= TRUE
;
388 SystemParameter
.VariableMtrrCount
= 0;
389 SystemParameter
.FixedMtrrSupported
= FALSE
;
390 InitializeMtrrRegs (&SystemParameter
);
391 UT_ASSERT_FALSE (IsMtrrSupported ());
394 // MTRR capability on in CPUID leaf, but no variable MTRRs.
396 SystemParameter
.MtrrSupported
= TRUE
;
397 SystemParameter
.VariableMtrrCount
= 0;
398 SystemParameter
.FixedMtrrSupported
= TRUE
;
399 InitializeMtrrRegs (&SystemParameter
);
400 UT_ASSERT_FALSE (IsMtrrSupported ());
403 // MTRR capability on in CPUID leaf, but no fixed MTRRs.
405 SystemParameter
.MtrrSupported
= TRUE
;
406 SystemParameter
.VariableMtrrCount
= 7;
407 SystemParameter
.FixedMtrrSupported
= FALSE
;
408 InitializeMtrrRegs (&SystemParameter
);
409 UT_ASSERT_FALSE (IsMtrrSupported ());
412 // MTRR capability on in CPUID leaf with both variable and fixed MTRRs.
414 SystemParameter
.MtrrSupported
= TRUE
;
415 SystemParameter
.VariableMtrrCount
= 7;
416 SystemParameter
.FixedMtrrSupported
= TRUE
;
417 InitializeMtrrRegs (&SystemParameter
);
418 UT_ASSERT_TRUE (IsMtrrSupported ());
420 return UNIT_TEST_PASSED
;
424 Unit test of MtrrLib service GetVariableMtrrCount()
426 @param[in] Context Ignored
428 @retval UNIT_TEST_PASSED The Unit test has completed and the test
430 @retval UNIT_TEST_ERROR_TEST_FAILED A test case assertion has failed.
435 UnitTestGetVariableMtrrCount (
436 IN UNIT_TEST_CONTEXT Context
440 MTRR_LIB_SYSTEM_PARAMETER SystemParameter
;
441 MTRR_LIB_TEST_CONTEXT
*LocalContext
;
443 LocalContext
= (MTRR_LIB_TEST_CONTEXT
*)Context
;
445 CopyMem (&SystemParameter
, LocalContext
->SystemParameter
, sizeof (SystemParameter
));
447 // If MTRR capability off in CPUID leaf, then the count is always 0.
449 SystemParameter
.MtrrSupported
= FALSE
;
450 for (SystemParameter
.VariableMtrrCount
= 1; SystemParameter
.VariableMtrrCount
<= MTRR_NUMBER_OF_VARIABLE_MTRR
; SystemParameter
.VariableMtrrCount
++) {
451 InitializeMtrrRegs (&SystemParameter
);
452 Result
= GetVariableMtrrCount ();
453 UT_ASSERT_EQUAL (Result
, 0);
457 // Try all supported variable MTRR counts.
458 // If variable MTRR count is > MTRR_NUMBER_OF_VARIABLE_MTRR, then an ASSERT()
461 SystemParameter
.MtrrSupported
= TRUE
;
462 for (SystemParameter
.VariableMtrrCount
= 1; SystemParameter
.VariableMtrrCount
<= MTRR_NUMBER_OF_VARIABLE_MTRR
; SystemParameter
.VariableMtrrCount
++) {
463 InitializeMtrrRegs (&SystemParameter
);
464 Result
= GetVariableMtrrCount ();
465 UT_ASSERT_EQUAL (Result
, SystemParameter
.VariableMtrrCount
);
469 // Expect ASSERT() if variable MTRR count is > MTRR_NUMBER_OF_VARIABLE_MTRR
471 SystemParameter
.VariableMtrrCount
= MTRR_NUMBER_OF_VARIABLE_MTRR
+ 1;
472 InitializeMtrrRegs (&SystemParameter
);
473 UT_EXPECT_ASSERT_FAILURE (GetVariableMtrrCount (), NULL
);
475 SystemParameter
.MtrrSupported
= TRUE
;
476 SystemParameter
.VariableMtrrCount
= MAX_UINT8
;
477 InitializeMtrrRegs (&SystemParameter
);
478 UT_EXPECT_ASSERT_FAILURE (GetVariableMtrrCount (), NULL
);
480 return UNIT_TEST_PASSED
;
484 Unit test of MtrrLib service GetFirmwareVariableMtrrCount()
486 @param[in] Context Ignored
488 @retval UNIT_TEST_PASSED The Unit test has completed and the test
490 @retval UNIT_TEST_ERROR_TEST_FAILED A test case assertion has failed.
495 UnitTestGetFirmwareVariableMtrrCount (
496 IN UNIT_TEST_CONTEXT Context
500 UINT32 ReservedMtrrs
;
501 MTRR_LIB_SYSTEM_PARAMETER SystemParameter
;
502 MTRR_LIB_GET_FIRMWARE_VARIABLE_MTRR_COUNT_CONTEXT
*LocalContext
;
504 LocalContext
= (MTRR_LIB_GET_FIRMWARE_VARIABLE_MTRR_COUNT_CONTEXT
*)Context
;
506 CopyMem (&SystemParameter
, LocalContext
->SystemParameter
, sizeof (SystemParameter
));
508 InitializeMtrrRegs (&SystemParameter
);
510 // Positive test cases for VCNT = 10 and Reserved PCD in range 0..10
512 for (ReservedMtrrs
= 0; ReservedMtrrs
<= SystemParameter
.VariableMtrrCount
; ReservedMtrrs
++) {
513 PatchPcdSet32 (PcdCpuNumberOfReservedVariableMtrrs
, ReservedMtrrs
);
514 Result
= GetFirmwareVariableMtrrCount ();
515 UT_ASSERT_EQUAL (Result
, SystemParameter
.VariableMtrrCount
- ReservedMtrrs
);
519 // Negative test cases when Reserved PCD is larger than VCNT
521 for (ReservedMtrrs
= SystemParameter
.VariableMtrrCount
+ 1; ReservedMtrrs
<= 255; ReservedMtrrs
++) {
522 PatchPcdSet32 (PcdCpuNumberOfReservedVariableMtrrs
, ReservedMtrrs
);
523 Result
= GetFirmwareVariableMtrrCount ();
524 UT_ASSERT_EQUAL (Result
, 0);
528 // Negative test cases when Reserved PCD is larger than VCNT
530 PatchPcdSet32 (PcdCpuNumberOfReservedVariableMtrrs
, MAX_UINT32
);
531 Result
= GetFirmwareVariableMtrrCount ();
532 UT_ASSERT_EQUAL (Result
, 0);
535 // Negative test case when MTRRs are not supported
537 SystemParameter
.MtrrSupported
= FALSE
;
538 InitializeMtrrRegs (&SystemParameter
);
539 PatchPcdSet32 (PcdCpuNumberOfReservedVariableMtrrs
, 2);
540 Result
= GetFirmwareVariableMtrrCount ();
541 UT_ASSERT_EQUAL (Result
, 0);
544 // Negative test case when Fixed MTRRs are not supported
546 SystemParameter
.MtrrSupported
= TRUE
;
547 SystemParameter
.FixedMtrrSupported
= FALSE
;
548 InitializeMtrrRegs (&SystemParameter
);
549 PatchPcdSet32 (PcdCpuNumberOfReservedVariableMtrrs
, 2);
550 Result
= GetFirmwareVariableMtrrCount ();
551 UT_ASSERT_EQUAL (Result
, 0);
554 // Expect ASSERT() if variable MTRR count is > MTRR_NUMBER_OF_VARIABLE_MTRR
556 SystemParameter
.FixedMtrrSupported
= TRUE
;
557 SystemParameter
.VariableMtrrCount
= MTRR_NUMBER_OF_VARIABLE_MTRR
+ 1;
558 InitializeMtrrRegs (&SystemParameter
);
559 UT_EXPECT_ASSERT_FAILURE (GetFirmwareVariableMtrrCount (), NULL
);
561 return UNIT_TEST_PASSED
;
565 Unit test of MtrrLib service MtrrGetMemoryAttribute()
567 @param[in] Context Ignored
569 @retval UNIT_TEST_PASSED The Unit test has completed and the test
571 @retval UNIT_TEST_ERROR_TEST_FAILED A test case assertion has failed.
576 UnitTestMtrrGetMemoryAttribute (
577 IN UNIT_TEST_CONTEXT Context
580 return UNIT_TEST_PASSED
;
584 Unit test of MtrrLib service MtrrGetFixedMtrr()
586 @param[in] Context Ignored
588 @retval UNIT_TEST_PASSED The Unit test has completed and the test
590 @retval UNIT_TEST_ERROR_TEST_FAILED A test case assertion has failed.
595 UnitTestMtrrGetFixedMtrr (
596 IN UNIT_TEST_CONTEXT Context
599 MTRR_FIXED_SETTINGS
*Result
;
600 MTRR_FIXED_SETTINGS ExpectedFixedSettings
;
601 MTRR_FIXED_SETTINGS FixedSettings
;
606 MTRR_LIB_SYSTEM_PARAMETER SystemParameter
;
607 MTRR_LIB_TEST_CONTEXT
*LocalContext
;
609 LocalContext
= (MTRR_LIB_TEST_CONTEXT
*)Context
;
611 CopyMem (&SystemParameter
, LocalContext
->SystemParameter
, sizeof (SystemParameter
));
612 InitializeMtrrRegs (&SystemParameter
);
614 // Set random cache type to different ranges under 1MB and make sure
615 // the fixed MTRR settings are expected.
618 for (Index
= 0; Index
< 100; Index
++) {
619 for (MsrIndex
= 0; MsrIndex
< ARRAY_SIZE (mFixedMtrrsIndex
); MsrIndex
++) {
621 for (ByteIndex
= 0; ByteIndex
< sizeof (UINT64
); ByteIndex
++) {
622 MsrValue
= MsrValue
| LShiftU64 (GenerateRandomCacheType (), ByteIndex
* 8);
625 ExpectedFixedSettings
.Mtrr
[MsrIndex
] = MsrValue
;
626 AsmWriteMsr64 (mFixedMtrrsIndex
[MsrIndex
], MsrValue
);
629 Result
= MtrrGetFixedMtrr (&FixedSettings
);
630 UT_ASSERT_EQUAL ((UINTN
)Result
, (UINTN
)&FixedSettings
);
631 UT_ASSERT_MEM_EQUAL (&FixedSettings
, &ExpectedFixedSettings
, sizeof (FixedSettings
));
635 // Negative test case when MTRRs are not supported
637 SystemParameter
.MtrrSupported
= FALSE
;
638 InitializeMtrrRegs (&SystemParameter
);
640 ZeroMem (&FixedSettings
, sizeof (FixedSettings
));
641 ZeroMem (&ExpectedFixedSettings
, sizeof (ExpectedFixedSettings
));
642 Result
= MtrrGetFixedMtrr (&FixedSettings
);
643 UT_ASSERT_EQUAL ((UINTN
)Result
, (UINTN
)&FixedSettings
);
644 UT_ASSERT_MEM_EQUAL (&ExpectedFixedSettings
, &FixedSettings
, sizeof (ExpectedFixedSettings
));
646 return UNIT_TEST_PASSED
;
650 Unit test of MtrrLib service MtrrGetAllMtrrs()
652 @param[in] Context Ignored
654 @retval UNIT_TEST_PASSED The Unit test has completed and the test
656 @retval UNIT_TEST_ERROR_TEST_FAILED A test case assertion has failed.
661 UnitTestMtrrGetAllMtrrs (
662 IN UNIT_TEST_CONTEXT Context
665 MTRR_SETTINGS
*Result
;
667 MTRR_SETTINGS ExpectedMtrrs
;
668 MTRR_VARIABLE_SETTING VariableMtrr
[MTRR_NUMBER_OF_VARIABLE_MTRR
];
670 MTRR_LIB_SYSTEM_PARAMETER SystemParameter
;
671 MTRR_LIB_TEST_CONTEXT
*LocalContext
;
673 LocalContext
= (MTRR_LIB_TEST_CONTEXT
*)Context
;
675 CopyMem (&SystemParameter
, LocalContext
->SystemParameter
, sizeof (SystemParameter
));
676 InitializeMtrrRegs (&SystemParameter
);
678 for (Index
= 0; Index
< SystemParameter
.VariableMtrrCount
; Index
++) {
679 GenerateRandomMtrrPair (SystemParameter
.PhysicalAddressBits
, GenerateRandomCacheType (), &VariableMtrr
[Index
], NULL
);
680 AsmWriteMsr64 (MSR_IA32_MTRR_PHYSBASE0
+ (Index
<< 1), VariableMtrr
[Index
].Base
);
681 AsmWriteMsr64 (MSR_IA32_MTRR_PHYSMASK0
+ (Index
<< 1), VariableMtrr
[Index
].Mask
);
684 Result
= MtrrGetAllMtrrs (&Mtrrs
);
685 UT_ASSERT_EQUAL ((UINTN
)Result
, (UINTN
)&Mtrrs
);
686 UT_ASSERT_MEM_EQUAL (Mtrrs
.Variables
.Mtrr
, VariableMtrr
, sizeof (MTRR_VARIABLE_SETTING
) * SystemParameter
.VariableMtrrCount
);
689 // Negative test case when MTRRs are not supported
691 ZeroMem (&ExpectedMtrrs
, sizeof (ExpectedMtrrs
));
692 ZeroMem (&Mtrrs
, sizeof (Mtrrs
));
694 SystemParameter
.MtrrSupported
= FALSE
;
695 InitializeMtrrRegs (&SystemParameter
);
696 Result
= MtrrGetAllMtrrs (&Mtrrs
);
697 UT_ASSERT_EQUAL ((UINTN
)Result
, (UINTN
)&Mtrrs
);
698 UT_ASSERT_MEM_EQUAL (&ExpectedMtrrs
, &Mtrrs
, sizeof (ExpectedMtrrs
));
701 // Expect ASSERT() if variable MTRR count is > MTRR_NUMBER_OF_VARIABLE_MTRR
703 SystemParameter
.MtrrSupported
= TRUE
;
704 SystemParameter
.VariableMtrrCount
= MTRR_NUMBER_OF_VARIABLE_MTRR
+ 1;
705 InitializeMtrrRegs (&SystemParameter
);
706 UT_EXPECT_ASSERT_FAILURE (MtrrGetAllMtrrs (&Mtrrs
), NULL
);
708 return UNIT_TEST_PASSED
;
712 Unit test of MtrrLib service MtrrSetAllMtrrs()
714 @param[in] Context Ignored
716 @retval UNIT_TEST_PASSED The Unit test has completed and the test
718 @retval UNIT_TEST_ERROR_TEST_FAILED A test case assertion has failed.
723 UnitTestMtrrSetAllMtrrs (
724 IN UNIT_TEST_CONTEXT Context
727 MTRR_SETTINGS
*Result
;
730 MSR_IA32_MTRR_DEF_TYPE_REGISTER Default
;
731 MTRR_LIB_SYSTEM_PARAMETER SystemParameter
;
732 MTRR_LIB_TEST_CONTEXT
*LocalContext
;
734 LocalContext
= (MTRR_LIB_TEST_CONTEXT
*)Context
;
736 CopyMem (&SystemParameter
, LocalContext
->SystemParameter
, sizeof (SystemParameter
));
737 InitializeMtrrRegs (&SystemParameter
);
742 Default
.Bits
.Type
= GenerateRandomCacheType ();
744 ZeroMem (&Mtrrs
, sizeof (Mtrrs
));
745 Mtrrs
.MtrrDefType
= Default
.Uint64
;
746 for (Index
= 0; Index
< SystemParameter
.VariableMtrrCount
; Index
++) {
747 GenerateRandomMtrrPair (SystemParameter
.PhysicalAddressBits
, GenerateRandomCacheType (), &Mtrrs
.Variables
.Mtrr
[Index
], NULL
);
750 Result
= MtrrSetAllMtrrs (&Mtrrs
);
751 UT_ASSERT_EQUAL ((UINTN
)Result
, (UINTN
)&Mtrrs
);
753 UT_ASSERT_EQUAL (AsmReadMsr64 (MSR_IA32_MTRR_DEF_TYPE
), Mtrrs
.MtrrDefType
);
754 for (Index
= 0; Index
< SystemParameter
.VariableMtrrCount
; Index
++) {
755 UT_ASSERT_EQUAL (AsmReadMsr64 (MSR_IA32_MTRR_PHYSBASE0
+ (Index
<< 1)), Mtrrs
.Variables
.Mtrr
[Index
].Base
);
756 UT_ASSERT_EQUAL (AsmReadMsr64 (MSR_IA32_MTRR_PHYSMASK0
+ (Index
<< 1)), Mtrrs
.Variables
.Mtrr
[Index
].Mask
);
759 return UNIT_TEST_PASSED
;
763 Unit test of MtrrLib service MtrrGetMemoryAttributeInVariableMtrr()
765 @param[in] Context Ignored
767 @retval UNIT_TEST_PASSED The Unit test has completed and the test
769 @retval UNIT_TEST_ERROR_TEST_FAILED A test case assertion has failed.
774 UnitTestMtrrGetMemoryAttributeInVariableMtrr (
775 IN UNIT_TEST_CONTEXT Context
778 MTRR_LIB_TEST_CONTEXT
*LocalContext
;
779 MTRR_LIB_SYSTEM_PARAMETER SystemParameter
;
781 MTRR_VARIABLE_SETTING VariableSetting
[MTRR_NUMBER_OF_VARIABLE_MTRR
];
782 VARIABLE_MTRR VariableMtrr
[MTRR_NUMBER_OF_VARIABLE_MTRR
];
783 UINT64 ValidMtrrBitsMask
;
784 UINT64 ValidMtrrAddressMask
;
786 MSR_IA32_MTRR_PHYSBASE_REGISTER Base
;
787 MSR_IA32_MTRR_PHYSMASK_REGISTER Mask
;
789 LocalContext
= (MTRR_LIB_TEST_CONTEXT
*)Context
;
791 CopyMem (&SystemParameter
, LocalContext
->SystemParameter
, sizeof (SystemParameter
));
793 InitializeMtrrRegs (&SystemParameter
);
795 ValidMtrrBitsMask
= (1ull << SystemParameter
.PhysicalAddressBits
) - 1;
796 ValidMtrrAddressMask
= ValidMtrrBitsMask
& 0xfffffffffffff000ULL
;
798 for (Index
= 0; Index
< SystemParameter
.VariableMtrrCount
; Index
++) {
799 GenerateRandomMtrrPair (SystemParameter
.PhysicalAddressBits
, GenerateRandomCacheType (), &VariableSetting
[Index
], NULL
);
800 AsmWriteMsr64 (MSR_IA32_MTRR_PHYSBASE0
+ (Index
<< 1), VariableSetting
[Index
].Base
);
801 AsmWriteMsr64 (MSR_IA32_MTRR_PHYSMASK0
+ (Index
<< 1), VariableSetting
[Index
].Mask
);
804 Result
= MtrrGetMemoryAttributeInVariableMtrr (ValidMtrrBitsMask
, ValidMtrrAddressMask
, VariableMtrr
);
805 UT_ASSERT_EQUAL (Result
, SystemParameter
.VariableMtrrCount
);
807 for (Index
= 0; Index
< SystemParameter
.VariableMtrrCount
; Index
++) {
808 Base
.Uint64
= VariableMtrr
[Index
].BaseAddress
;
809 Base
.Bits
.Type
= (UINT32
)VariableMtrr
[Index
].Type
;
810 UT_ASSERT_EQUAL (Base
.Uint64
, VariableSetting
[Index
].Base
);
812 Mask
.Uint64
= ~(VariableMtrr
[Index
].Length
- 1) & ValidMtrrBitsMask
;
814 UT_ASSERT_EQUAL (Mask
.Uint64
, VariableSetting
[Index
].Mask
);
818 // Negative test case when MTRRs are not supported
820 SystemParameter
.MtrrSupported
= FALSE
;
821 InitializeMtrrRegs (&SystemParameter
);
822 Result
= MtrrGetMemoryAttributeInVariableMtrr (ValidMtrrBitsMask
, ValidMtrrAddressMask
, VariableMtrr
);
823 UT_ASSERT_EQUAL (Result
, 0);
826 // Expect ASSERT() if variable MTRR count is > MTRR_NUMBER_OF_VARIABLE_MTRR
828 SystemParameter
.MtrrSupported
= TRUE
;
829 SystemParameter
.VariableMtrrCount
= MTRR_NUMBER_OF_VARIABLE_MTRR
+ 1;
830 InitializeMtrrRegs (&SystemParameter
);
831 UT_EXPECT_ASSERT_FAILURE (MtrrGetMemoryAttributeInVariableMtrr (ValidMtrrBitsMask
, ValidMtrrAddressMask
, VariableMtrr
), NULL
);
833 return UNIT_TEST_PASSED
;
837 Unit test of MtrrLib service MtrrDebugPrintAllMtrrs()
839 @param[in] Context Ignored
841 @retval UNIT_TEST_PASSED The Unit test has completed and the test
843 @retval UNIT_TEST_ERROR_TEST_FAILED A test case assertion has failed.
848 UnitTestMtrrDebugPrintAllMtrrs (
849 IN UNIT_TEST_CONTEXT Context
852 return UNIT_TEST_PASSED
;
856 Unit test of MtrrLib service MtrrGetDefaultMemoryType().
858 @param[in] Context Ignored
860 @retval UNIT_TEST_PASSED The Unit test has completed and the test
862 @retval UNIT_TEST_ERROR_TEST_FAILED A test case assertion has failed.
867 UnitTestMtrrGetDefaultMemoryType (
868 IN UNIT_TEST_CONTEXT Context
871 MTRR_LIB_TEST_CONTEXT
*LocalContext
;
873 MTRR_MEMORY_CACHE_TYPE Result
;
874 MTRR_LIB_SYSTEM_PARAMETER SystemParameter
;
875 MTRR_MEMORY_CACHE_TYPE CacheType
[5];
877 CacheType
[0] = CacheUncacheable
;
878 CacheType
[1] = CacheWriteCombining
;
879 CacheType
[2] = CacheWriteThrough
;
880 CacheType
[3] = CacheWriteProtected
;
881 CacheType
[4] = CacheWriteBack
;
883 LocalContext
= (MTRR_LIB_TEST_CONTEXT
*)Context
;
885 CopyMem (&SystemParameter
, LocalContext
->SystemParameter
, sizeof (SystemParameter
));
887 // If MTRRs are supported, then always return the cache type in the MSR
888 // MSR_IA32_MTRR_DEF_TYPE
890 for (Index
= 0; Index
< ARRAY_SIZE (CacheType
); Index
++) {
891 SystemParameter
.DefaultCacheType
= CacheType
[Index
];
892 InitializeMtrrRegs (&SystemParameter
);
893 Result
= MtrrGetDefaultMemoryType ();
894 UT_ASSERT_EQUAL (Result
, SystemParameter
.DefaultCacheType
);
898 // If MTRRs are not supported, then always return CacheUncacheable
900 SystemParameter
.MtrrSupported
= FALSE
;
901 InitializeMtrrRegs (&SystemParameter
);
902 Result
= MtrrGetDefaultMemoryType ();
903 UT_ASSERT_EQUAL (Result
, CacheUncacheable
);
905 SystemParameter
.MtrrSupported
= TRUE
;
906 SystemParameter
.FixedMtrrSupported
= FALSE
;
907 InitializeMtrrRegs (&SystemParameter
);
908 Result
= MtrrGetDefaultMemoryType ();
909 UT_ASSERT_EQUAL (Result
, CacheUncacheable
);
911 SystemParameter
.MtrrSupported
= TRUE
;
912 SystemParameter
.FixedMtrrSupported
= TRUE
;
913 SystemParameter
.VariableMtrrCount
= 0;
914 InitializeMtrrRegs (&SystemParameter
);
915 Result
= MtrrGetDefaultMemoryType ();
916 UT_ASSERT_EQUAL (Result
, CacheUncacheable
);
918 return UNIT_TEST_PASSED
;
922 Unit test of MtrrLib service MtrrSetMemoryAttributeInMtrrSettings().
924 @param[in] Context Ignored
926 @retval UNIT_TEST_PASSED The Unit test has completed and the test
928 @retval UNIT_TEST_ERROR_TEST_FAILED A test case assertion has failed.
933 UnitTestMtrrSetMemoryAttributeInMtrrSettings (
934 IN UNIT_TEST_CONTEXT Context
937 CONST MTRR_LIB_SYSTEM_PARAMETER
*SystemParameter
;
938 RETURN_STATUS Status
;
947 MTRR_SETTINGS LocalMtrrs
;
949 MTRR_MEMORY_RANGE RawMtrrRange
[MTRR_NUMBER_OF_VARIABLE_MTRR
];
950 MTRR_MEMORY_RANGE ExpectedMemoryRanges
[MTRR_NUMBER_OF_FIXED_MTRR
* sizeof (UINT64
) + 2 * MTRR_NUMBER_OF_VARIABLE_MTRR
+ 1];
951 UINT32 ExpectedVariableMtrrUsage
;
952 UINTN ExpectedMemoryRangesCount
;
954 MTRR_MEMORY_RANGE ActualMemoryRanges
[MTRR_NUMBER_OF_FIXED_MTRR
* sizeof (UINT64
) + 2 * MTRR_NUMBER_OF_VARIABLE_MTRR
+ 1];
955 UINT32 ActualVariableMtrrUsage
;
956 UINTN ActualMemoryRangesCount
;
958 MTRR_SETTINGS
*Mtrrs
[2];
960 SystemParameter
= (MTRR_LIB_SYSTEM_PARAMETER
*)Context
;
961 GenerateRandomMemoryTypeCombination (
962 SystemParameter
->VariableMtrrCount
- PatchPcdGet32 (PcdCpuNumberOfReservedVariableMtrrs
),
969 GenerateValidAndConfigurableMtrrPairs (
970 SystemParameter
->PhysicalAddressBits
,
979 ExpectedVariableMtrrUsage
= UcCount
+ WtCount
+ WbCount
+ WpCount
+ WcCount
;
980 ExpectedMemoryRangesCount
= ARRAY_SIZE (ExpectedMemoryRanges
);
981 GetEffectiveMemoryRanges (
982 SystemParameter
->DefaultCacheType
,
983 SystemParameter
->PhysicalAddressBits
,
985 ExpectedVariableMtrrUsage
,
986 ExpectedMemoryRanges
,
987 &ExpectedMemoryRangesCount
990 UT_LOG_INFO ("--- Expected Memory Ranges [%d] ---\n", ExpectedMemoryRangesCount
);
991 DumpMemoryRanges (ExpectedMemoryRanges
, ExpectedMemoryRangesCount
);
993 // Default cache type is always an INPUT
995 ZeroMem (&LocalMtrrs
, sizeof (LocalMtrrs
));
996 LocalMtrrs
.MtrrDefType
= MtrrGetDefaultMemoryType ();
997 Mtrrs
[0] = &LocalMtrrs
;
1000 for (MtrrIndex
= 0; MtrrIndex
< ARRAY_SIZE (Mtrrs
); MtrrIndex
++) {
1001 for (Index
= 0; Index
< ExpectedMemoryRangesCount
; Index
++) {
1002 Status
= MtrrSetMemoryAttributeInMtrrSettings (
1004 ExpectedMemoryRanges
[Index
].BaseAddress
,
1005 ExpectedMemoryRanges
[Index
].Length
,
1006 ExpectedMemoryRanges
[Index
].Type
1008 UT_ASSERT_TRUE (Status
== RETURN_SUCCESS
|| Status
== RETURN_OUT_OF_RESOURCES
|| Status
== RETURN_BUFFER_TOO_SMALL
);
1009 if ((Status
== RETURN_OUT_OF_RESOURCES
) || (Status
== RETURN_BUFFER_TOO_SMALL
)) {
1010 return UNIT_TEST_SKIPPED
;
1014 if (Mtrrs
[MtrrIndex
] == NULL
) {
1015 ZeroMem (&LocalMtrrs
, sizeof (LocalMtrrs
));
1016 MtrrGetAllMtrrs (&LocalMtrrs
);
1019 ActualMemoryRangesCount
= ARRAY_SIZE (ActualMemoryRanges
);
1021 SystemParameter
->DefaultCacheType
,
1022 SystemParameter
->PhysicalAddressBits
,
1023 SystemParameter
->VariableMtrrCount
,
1026 &ActualMemoryRangesCount
,
1027 &ActualVariableMtrrUsage
1029 UT_LOG_INFO ("--- Actual Memory Ranges [%d] ---\n", ActualMemoryRangesCount
);
1030 DumpMemoryRanges (ActualMemoryRanges
, ActualMemoryRangesCount
);
1031 VerifyMemoryRanges (ExpectedMemoryRanges
, ExpectedMemoryRangesCount
, ActualMemoryRanges
, ActualMemoryRangesCount
);
1032 UT_ASSERT_TRUE (ExpectedVariableMtrrUsage
>= ActualVariableMtrrUsage
);
1034 ZeroMem (&LocalMtrrs
, sizeof (LocalMtrrs
));
1037 return UNIT_TEST_PASSED
;
1041 Prep routine for UnitTestGetFirmwareVariableMtrrCount().
1043 @param Context Point to a UINT32 data to save the PcdCpuNumberOfReservedVariableMtrrs.
1048 UNIT_TEST_CONTEXT Context
1051 MTRR_LIB_GET_FIRMWARE_VARIABLE_MTRR_COUNT_CONTEXT
*LocalContext
;
1053 LocalContext
= (MTRR_LIB_GET_FIRMWARE_VARIABLE_MTRR_COUNT_CONTEXT
*)Context
;
1054 LocalContext
->NumberOfReservedVariableMtrrs
= PatchPcdGet32 (PcdCpuNumberOfReservedVariableMtrrs
);
1055 return UNIT_TEST_PASSED
;
1059 Clean up routine for UnitTestGetFirmwareVariableMtrrCount().
1061 @param Context Point to a UINT32 data to save the PcdCpuNumberOfReservedVariableMtrrs.
1066 UNIT_TEST_CONTEXT Context
1069 MTRR_LIB_GET_FIRMWARE_VARIABLE_MTRR_COUNT_CONTEXT
*LocalContext
;
1071 LocalContext
= (MTRR_LIB_GET_FIRMWARE_VARIABLE_MTRR_COUNT_CONTEXT
*)Context
;
1072 PatchPcdSet32 (PcdCpuNumberOfReservedVariableMtrrs
, LocalContext
->NumberOfReservedVariableMtrrs
);
1076 Initialize the unit test framework, suite, and unit tests for the
1077 ResetSystemLib and run the ResetSystemLib unit test.
1079 @param Iteration Iteration of testing MtrrSetMemoryAttributeInMtrrSettings
1080 and MtrrSetMemoryAttributesInMtrrSettings using random inputs.
1082 @retval EFI_SUCCESS All test cases were dispatched.
1083 @retval EFI_OUT_OF_RESOURCES There are not enough resources available to
1084 initialize the unit tests.
1094 UNIT_TEST_FRAMEWORK_HANDLE Framework
;
1095 UNIT_TEST_SUITE_HANDLE MtrrApiTests
;
1098 MTRR_LIB_TEST_CONTEXT Context
;
1099 MTRR_LIB_GET_FIRMWARE_VARIABLE_MTRR_COUNT_CONTEXT GetFirmwareVariableMtrrCountContext
;
1101 Context
.SystemParameter
= &mDefaultSystemParameter
;
1102 GetFirmwareVariableMtrrCountContext
.SystemParameter
= &mDefaultSystemParameter
;
1106 // Setup the test framework for running the tests.
1108 Status
= InitUnitTestFramework (&Framework
, UNIT_TEST_APP_NAME
, gEfiCallerBaseName
, UNIT_TEST_APP_VERSION
);
1109 if (EFI_ERROR (Status
)) {
1110 DEBUG ((DEBUG_ERROR
, "Failed in InitUnitTestFramework. Status = %r\n", Status
));
1115 // --------------Suite-----------Description--------------Name----------Function--------Pre---Post-------------------Context-----------
1119 // Populate the MtrrLib API Unit Test Suite.
1121 Status
= CreateUnitTestSuite (&MtrrApiTests
, Framework
, "MtrrLib API Tests", "MtrrLib.MtrrLib", NULL
, NULL
);
1122 if (EFI_ERROR (Status
)) {
1123 DEBUG ((DEBUG_ERROR
, "Failed in CreateUnitTestSuite for MtrrLib API Tests\n"));
1124 Status
= EFI_OUT_OF_RESOURCES
;
1128 AddTestCase (MtrrApiTests
, "Test IsMtrrSupported", "MtrrSupported", UnitTestIsMtrrSupported
, NULL
, NULL
, &Context
);
1129 AddTestCase (MtrrApiTests
, "Test GetVariableMtrrCount", "GetVariableMtrrCount", UnitTestGetVariableMtrrCount
, NULL
, NULL
, &Context
);
1130 AddTestCase (MtrrApiTests
, "Test GetFirmwareVariableMtrrCount", "GetFirmwareVariableMtrrCount", UnitTestGetFirmwareVariableMtrrCount
, SavePcdValue
, RestorePcdValue
, &GetFirmwareVariableMtrrCountContext
);
1131 AddTestCase (MtrrApiTests
, "Test MtrrGetMemoryAttribute", "MtrrGetMemoryAttribute", UnitTestMtrrGetMemoryAttribute
, NULL
, NULL
, &Context
);
1132 AddTestCase (MtrrApiTests
, "Test MtrrGetFixedMtrr", "MtrrGetFixedMtrr", UnitTestMtrrGetFixedMtrr
, NULL
, NULL
, &Context
);
1133 AddTestCase (MtrrApiTests
, "Test MtrrGetAllMtrrs", "MtrrGetAllMtrrs", UnitTestMtrrGetAllMtrrs
, NULL
, NULL
, &Context
);
1134 AddTestCase (MtrrApiTests
, "Test MtrrSetAllMtrrs", "MtrrSetAllMtrrs", UnitTestMtrrSetAllMtrrs
, NULL
, NULL
, &Context
);
1135 AddTestCase (MtrrApiTests
, "Test MtrrGetMemoryAttributeInVariableMtrr", "MtrrGetMemoryAttributeInVariableMtrr", UnitTestMtrrGetMemoryAttributeInVariableMtrr
, NULL
, NULL
, &Context
);
1136 AddTestCase (MtrrApiTests
, "Test MtrrDebugPrintAllMtrrs", "MtrrDebugPrintAllMtrrs", UnitTestMtrrDebugPrintAllMtrrs
, NULL
, NULL
, &Context
);
1137 AddTestCase (MtrrApiTests
, "Test MtrrGetDefaultMemoryType", "MtrrGetDefaultMemoryType", UnitTestMtrrGetDefaultMemoryType
, NULL
, NULL
, &Context
);
1139 for (SystemIndex
= 0; SystemIndex
< ARRAY_SIZE (mSystemParameters
); SystemIndex
++) {
1140 for (Index
= 0; Index
< Iteration
; Index
++) {
1141 AddTestCase (MtrrApiTests
, "Test InvalidMemoryLayouts", "InvalidMemoryLayouts", UnitTestInvalidMemoryLayouts
, InitializeSystem
, NULL
, &mSystemParameters
[SystemIndex
]);
1142 AddTestCase (MtrrApiTests
, "Test MtrrSetMemoryAttributeInMtrrSettings", "MtrrSetMemoryAttributeInMtrrSettings", UnitTestMtrrSetMemoryAttributeInMtrrSettings
, InitializeSystem
, NULL
, &mSystemParameters
[SystemIndex
]);
1143 AddTestCase (MtrrApiTests
, "Test MtrrSetMemoryAttributesInMtrrSettings", "MtrrSetMemoryAttributesInMtrrSettings", UnitTestMtrrSetMemoryAttributesInMtrrSettings
, InitializeSystem
, NULL
, &mSystemParameters
[SystemIndex
]);
1148 // Execute the tests.
1150 Status
= RunAllTestSuites (Framework
);
1153 if (Framework
!= NULL
) {
1154 FreeUnitTestFramework (Framework
);
1161 Standard POSIX C entry point for host based unit test execution.
1163 @param Argc Number of arguments.
1164 @param Argv Array of arguments.
1166 @return Test application exit code.
1176 DEBUG ((DEBUG_INFO
, "%a v%a\n", UNIT_TEST_APP_NAME
, UNIT_TEST_APP_VERSION
));
1177 srand ((unsigned int)time (NULL
));
1180 // MtrrLibUnitTest generate-random-numbers <path to MtrrLib/UnitTest/RandomNumber.c> <random-number count>
1182 if ((Argc
== 4) && (AsciiStriCmp ("generate-random-numbers", Argv
[1]) == 0)) {
1183 Count
= atoi (Argv
[3]);
1184 DEBUG ((DEBUG_INFO
, "Generate %d random numbers to %a.\n", Count
, Argv
[2]));
1185 GenerateRandomNumbers (Argv
[2], Count
);
1190 // MtrrLibUnitTest [<iterations>]
1191 // <iterations> [fixed|random]
1192 // Default <iterations> is 10.
1193 // Default uses fixed inputs.
1196 mRandomInput
= FALSE
;
1197 if ((Argc
== 2) || (Argc
== 3)) {
1198 Count
= atoi (Argv
[1]);
1200 if (AsciiStriCmp ("fixed", Argv
[2]) == 0) {
1201 mRandomInput
= FALSE
;
1202 } else if (AsciiStriCmp ("random", Argv
[2]) == 0) {
1203 mRandomInput
= TRUE
;
1208 DEBUG ((DEBUG_INFO
, "Iterations = %d\n", Count
));
1209 DEBUG ((DEBUG_INFO
, "Input = %a\n", mRandomInput
? "random" : "fixed"));
1211 return UnitTestingEntry (Count
);