TianoCore EDK2 master
Loading...
Searching...
No Matches
MtrrLibUnitTest.c
Go to the documentation of this file.
1
9#include "MtrrLibUnitTest.h"
10
11STATIC CONST MTRR_LIB_SYSTEM_PARAMETER mDefaultSystemParameter = {
12 42, TRUE, TRUE, CacheUncacheable, 12
13};
14
15STATIC 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 },
21
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 },
27
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 },
33
34 { 48, TRUE, FALSE, CacheUncacheable, 12 },
35 { 48, TRUE, FALSE, CacheWriteBack, 12 },
36 { 48, TRUE, FALSE, CacheWriteThrough, 12 },
37 { 48, TRUE, FALSE, CacheWriteProtected, 12 },
38 { 48, TRUE, FALSE, CacheWriteCombining, 12 },
39 { 48, TRUE, TRUE, CacheWriteBack, 12, 7}, // 7 bits for MKTME
40};
41
42UINT32 mFixedMtrrsIndex[] = {
54};
56 (ARRAY_SIZE (mFixedMtrrsIndex) == MTRR_NUMBER_OF_FIXED_MTRR),
57 "gFixedMtrrIndex does NOT contain all the fixed MTRRs!"
58 );
59
60//
61// Context structure to be used for most of the test cases.
62//
63typedef struct {
64 CONST MTRR_LIB_SYSTEM_PARAMETER *SystemParameter;
66
67//
68// Context structure to be used for GetFirmwareVariableMtrrCount() test.
69//
70typedef struct {
71 UINT32 NumberOfReservedVariableMtrrs;
72 CONST MTRR_LIB_SYSTEM_PARAMETER *SystemParameter;
74
75STATIC CHAR8 *mCacheDescription[] = { "UC", "WC", "N/A", "N/A", "WT", "WP", "WB" };
76
90 IN MTRR_MEMORY_RANGE *ExpectedMemoryRanges,
91 IN UINTN ExpectedMemoryRangeCount,
92 IN MTRR_MEMORY_RANGE *ActualRanges,
93 IN UINTN ActualRangeCount
94 )
95{
96 UINTN Index;
97
98 UT_ASSERT_EQUAL (ExpectedMemoryRangeCount, ActualRangeCount);
99 for (Index = 0; Index < ExpectedMemoryRangeCount; Index++) {
100 UT_ASSERT_EQUAL (ExpectedMemoryRanges[Index].BaseAddress, ActualRanges[Index].BaseAddress);
101 UT_ASSERT_EQUAL (ExpectedMemoryRanges[Index].Length, ActualRanges[Index].Length);
102 UT_ASSERT_EQUAL (ExpectedMemoryRanges[Index].Type, ActualRanges[Index].Type);
103 }
104
105 return UNIT_TEST_PASSED;
106}
107
114VOID
116 MTRR_MEMORY_RANGE *Ranges,
117 UINTN RangeCount
118 )
119{
120 UINTN Index;
121
122 for (Index = 0; Index < RangeCount; Index++) {
123 UT_LOG_INFO ("\t{ 0x%016llx, 0x%016llx, %a },\n", Ranges[Index].BaseAddress, Ranges[Index].Length, mCacheDescription[Ranges[Index].Type]);
124 }
125}
126
140VOID
142 IN UINT32 TotalCount,
143 OUT UINT32 *UcCount,
144 OUT UINT32 *WtCount,
145 OUT UINT32 *WbCount,
146 OUT UINT32 *WpCount,
147 OUT UINT32 *WcCount
148 )
149{
150 UINTN Index;
151 UINT32 TotalMtrrCount;
152 UINT32 *CountPerType[5];
153
154 CountPerType[0] = UcCount;
155 CountPerType[1] = WtCount;
156 CountPerType[2] = WbCount;
157 CountPerType[3] = WpCount;
158 CountPerType[4] = WcCount;
159
160 //
161 // Initialize the count of each cache type to 0.
162 //
163 for (Index = 0; Index < ARRAY_SIZE (CountPerType); Index++) {
164 *(CountPerType[Index]) = 0;
165 }
166
167 //
168 // Pick a random count of MTRRs
169 //
170 TotalMtrrCount = Random32 (1, TotalCount);
171 for (Index = 0; Index < TotalMtrrCount; Index++) {
172 //
173 // For each of them, pick a random cache type.
174 //
175 (*(CountPerType[Random32 (0, ARRAY_SIZE (CountPerType) - 1)]))++;
176 }
177}
178
191EFIAPI
193 IN UNIT_TEST_CONTEXT Context
194 )
195{
196 CONST MTRR_LIB_SYSTEM_PARAMETER *SystemParameter;
197 RETURN_STATUS Status;
198 UINT32 UcCount;
199 UINT32 WtCount;
200 UINT32 WbCount;
201 UINT32 WpCount;
202 UINT32 WcCount;
203
204 UINT32 MtrrIndex;
205 UINT8 *Scratch;
206 UINTN ScratchSize;
207 MTRR_SETTINGS LocalMtrrs;
208
209 MTRR_MEMORY_RANGE RawMtrrRange[MTRR_NUMBER_OF_VARIABLE_MTRR];
210 MTRR_MEMORY_RANGE ExpectedMemoryRanges[MTRR_NUMBER_OF_FIXED_MTRR * sizeof (UINT64) + 2 * MTRR_NUMBER_OF_VARIABLE_MTRR + 1];
211 UINT32 ExpectedVariableMtrrUsage;
212 UINTN ExpectedMemoryRangesCount;
213
214 MTRR_MEMORY_RANGE ActualMemoryRanges[MTRR_NUMBER_OF_FIXED_MTRR * sizeof (UINT64) + 2 * MTRR_NUMBER_OF_VARIABLE_MTRR + 1];
215 UINT32 ActualVariableMtrrUsage;
216 UINTN ActualMemoryRangesCount;
217
218 MTRR_MEMORY_RANGE ReturnedMemoryRanges[MTRR_NUMBER_OF_FIXED_MTRR * sizeof (UINT64) + 2 * MTRR_NUMBER_OF_VARIABLE_MTRR + 1];
219 UINTN ReturnedMemoryRangesCount;
220
221 MTRR_SETTINGS *Mtrrs[2];
222
223 SystemParameter = (MTRR_LIB_SYSTEM_PARAMETER *)Context;
225 SystemParameter->VariableMtrrCount - PatchPcdGet32 (PcdCpuNumberOfReservedVariableMtrrs),
226 &UcCount,
227 &WtCount,
228 &WbCount,
229 &WpCount,
230 &WcCount
231 );
233 SystemParameter->PhysicalAddressBits - SystemParameter->MkTmeKeyidBits,
234 RawMtrrRange,
235 UcCount,
236 WtCount,
237 WbCount,
238 WpCount,
239 WcCount
240 );
241
242 ExpectedVariableMtrrUsage = UcCount + WtCount + WbCount + WpCount + WcCount;
243 ExpectedMemoryRangesCount = ARRAY_SIZE (ExpectedMemoryRanges);
245 SystemParameter->DefaultCacheType,
246 SystemParameter->PhysicalAddressBits - SystemParameter->MkTmeKeyidBits,
247 RawMtrrRange,
248 ExpectedVariableMtrrUsage,
249 ExpectedMemoryRanges,
250 &ExpectedMemoryRangesCount
251 );
252
254 "Total MTRR [%d]: UC=%d, WT=%d, WB=%d, WP=%d, WC=%d\n",
255 ExpectedVariableMtrrUsage,
256 UcCount,
257 WtCount,
258 WbCount,
259 WpCount,
260 WcCount
261 );
262 UT_LOG_INFO ("--- Expected Memory Ranges [%d] ---\n", ExpectedMemoryRangesCount);
263 DumpMemoryRanges (ExpectedMemoryRanges, ExpectedMemoryRangesCount);
264
265 //
266 // Default cache type is always an INPUT
267 //
268 ZeroMem (&LocalMtrrs, sizeof (LocalMtrrs));
269 LocalMtrrs.MtrrDefType = MtrrGetDefaultMemoryType ();
270 ScratchSize = SCRATCH_BUFFER_SIZE;
271 Mtrrs[0] = &LocalMtrrs;
272 Mtrrs[1] = NULL;
273
274 for (MtrrIndex = 0; MtrrIndex < ARRAY_SIZE (Mtrrs); MtrrIndex++) {
275 Scratch = calloc (ScratchSize, sizeof (UINT8));
276 Status = MtrrSetMemoryAttributesInMtrrSettings (Mtrrs[MtrrIndex], Scratch, &ScratchSize, ExpectedMemoryRanges, ExpectedMemoryRangesCount);
277 if (Status == RETURN_BUFFER_TOO_SMALL) {
278 Scratch = realloc (Scratch, ScratchSize);
279 Status = MtrrSetMemoryAttributesInMtrrSettings (Mtrrs[MtrrIndex], Scratch, &ScratchSize, ExpectedMemoryRanges, ExpectedMemoryRangesCount);
280 }
281
283
284 if (Mtrrs[MtrrIndex] == NULL) {
285 ZeroMem (&LocalMtrrs, sizeof (LocalMtrrs));
286 MtrrGetAllMtrrs (&LocalMtrrs);
287 }
288
289 ActualMemoryRangesCount = ARRAY_SIZE (ActualMemoryRanges);
291 SystemParameter->DefaultCacheType,
292 SystemParameter->PhysicalAddressBits - SystemParameter->MkTmeKeyidBits,
293 SystemParameter->VariableMtrrCount,
294 &LocalMtrrs,
295 ActualMemoryRanges,
296 &ActualMemoryRangesCount,
297 &ActualVariableMtrrUsage
298 );
299
300 UT_LOG_INFO ("--- Actual Memory Ranges [%d] ---\n", ActualMemoryRangesCount);
301 DumpMemoryRanges (ActualMemoryRanges, ActualMemoryRangesCount);
302 VerifyMemoryRanges (ExpectedMemoryRanges, ExpectedMemoryRangesCount, ActualMemoryRanges, ActualMemoryRangesCount);
303 UT_ASSERT_TRUE (ExpectedVariableMtrrUsage >= ActualVariableMtrrUsage);
304
305 ReturnedMemoryRangesCount = ARRAY_SIZE (ReturnedMemoryRanges);
307 Mtrrs[MtrrIndex],
308 ReturnedMemoryRanges,
309 &ReturnedMemoryRangesCount
310 );
312 UT_LOG_INFO ("--- Returned Memory Ranges [%d] ---\n", ReturnedMemoryRangesCount);
313 DumpMemoryRanges (ReturnedMemoryRanges, ReturnedMemoryRangesCount);
314 VerifyMemoryRanges (ExpectedMemoryRanges, ExpectedMemoryRangesCount, ReturnedMemoryRanges, ReturnedMemoryRangesCount);
315
316 ZeroMem (&LocalMtrrs, sizeof (LocalMtrrs));
317 }
318
319 free (Scratch);
320
321 return UNIT_TEST_PASSED;
322}
323
332EFIAPI
334 IN UNIT_TEST_CONTEXT Context
335 )
336{
337 CONST MTRR_LIB_SYSTEM_PARAMETER *SystemParameter;
338 MTRR_MEMORY_RANGE Ranges[MTRR_NUMBER_OF_VARIABLE_MTRR * 2 + 1];
339 UINTN RangeCount;
340 UINT64 MaxAddress;
341 UINT32 Index;
342 UINT64 BaseAddress;
343 UINT64 Length;
344 RETURN_STATUS Status;
345 UINTN ScratchSize;
346
347 SystemParameter = (MTRR_LIB_SYSTEM_PARAMETER *)Context;
348
349 RangeCount = Random32 (1, ARRAY_SIZE (Ranges));
350 MaxAddress = 1ull << (SystemParameter->PhysicalAddressBits - SystemParameter->MkTmeKeyidBits);
351
352 for (Index = 0; Index < RangeCount; Index++) {
353 do {
354 BaseAddress = Random64 (0, MaxAddress);
355 Length = Random64 (1, MaxAddress - BaseAddress);
356 } while (((BaseAddress & 0xFFF) == 0) || ((Length & 0xFFF) == 0));
357
358 Ranges[Index].BaseAddress = BaseAddress;
359 Ranges[Index].Length = Length;
360 Ranges[Index].Type = GenerateRandomCacheType ();
361
362 Status = MtrrSetMemoryAttribute (
363 Ranges[Index].BaseAddress,
364 Ranges[Index].Length,
365 Ranges[Index].Type
366 );
367 UT_ASSERT_TRUE (RETURN_ERROR (Status));
368 }
369
370 ScratchSize = 0;
371 Status = MtrrSetMemoryAttributesInMtrrSettings (NULL, NULL, &ScratchSize, Ranges, RangeCount);
372 UT_ASSERT_TRUE (RETURN_ERROR (Status));
373
374 return UNIT_TEST_PASSED;
375}
376
388EFIAPI
390 IN UNIT_TEST_CONTEXT Context
391 )
392{
393 MTRR_LIB_SYSTEM_PARAMETER SystemParameter;
394 MTRR_LIB_TEST_CONTEXT *LocalContext;
395
396 LocalContext = (MTRR_LIB_TEST_CONTEXT *)Context;
397
398 CopyMem (&SystemParameter, LocalContext->SystemParameter, sizeof (SystemParameter));
399 //
400 // MTRR capability off in CPUID leaf.
401 //
402 SystemParameter.MtrrSupported = FALSE;
403 InitializeMtrrRegs (&SystemParameter);
405
406 //
407 // MTRR capability on in CPUID leaf, but no variable or fixed MTRRs.
408 //
409 SystemParameter.MtrrSupported = TRUE;
410 SystemParameter.VariableMtrrCount = 0;
411 SystemParameter.FixedMtrrSupported = FALSE;
412 InitializeMtrrRegs (&SystemParameter);
414
415 //
416 // MTRR capability on in CPUID leaf, but no variable MTRRs.
417 //
418 SystemParameter.MtrrSupported = TRUE;
419 SystemParameter.VariableMtrrCount = 0;
420 SystemParameter.FixedMtrrSupported = TRUE;
421 InitializeMtrrRegs (&SystemParameter);
423
424 //
425 // MTRR capability on in CPUID leaf, but no fixed MTRRs.
426 //
427 SystemParameter.MtrrSupported = TRUE;
428 SystemParameter.VariableMtrrCount = 7;
429 SystemParameter.FixedMtrrSupported = FALSE;
430 InitializeMtrrRegs (&SystemParameter);
432
433 //
434 // MTRR capability on in CPUID leaf with both variable and fixed MTRRs.
435 //
436 SystemParameter.MtrrSupported = TRUE;
437 SystemParameter.VariableMtrrCount = 7;
438 SystemParameter.FixedMtrrSupported = TRUE;
439 InitializeMtrrRegs (&SystemParameter);
441
442 return UNIT_TEST_PASSED;
443}
444
456EFIAPI
458 IN UNIT_TEST_CONTEXT Context
459 )
460{
461 UINT32 Result;
462 MTRR_LIB_SYSTEM_PARAMETER SystemParameter;
463 MTRR_LIB_TEST_CONTEXT *LocalContext;
464
465 LocalContext = (MTRR_LIB_TEST_CONTEXT *)Context;
466
467 CopyMem (&SystemParameter, LocalContext->SystemParameter, sizeof (SystemParameter));
468 //
469 // If MTRR capability off in CPUID leaf, then the count is always 0.
470 //
471 SystemParameter.MtrrSupported = FALSE;
472 for (SystemParameter.VariableMtrrCount = 1; SystemParameter.VariableMtrrCount <= MTRR_NUMBER_OF_VARIABLE_MTRR; SystemParameter.VariableMtrrCount++) {
473 InitializeMtrrRegs (&SystemParameter);
474 Result = GetVariableMtrrCount ();
475 UT_ASSERT_EQUAL (Result, 0);
476 }
477
478 //
479 // Try all supported variable MTRR counts.
480 // If variable MTRR count is > MTRR_NUMBER_OF_VARIABLE_MTRR, then an ASSERT()
481 // is generated.
482 //
483 SystemParameter.MtrrSupported = TRUE;
484 for (SystemParameter.VariableMtrrCount = 1; SystemParameter.VariableMtrrCount <= MTRR_NUMBER_OF_VARIABLE_MTRR; SystemParameter.VariableMtrrCount++) {
485 InitializeMtrrRegs (&SystemParameter);
486 Result = GetVariableMtrrCount ();
487 UT_ASSERT_EQUAL (Result, SystemParameter.VariableMtrrCount);
488 }
489
490 //
491 // Expect ASSERT() if variable MTRR count is > MTRR_NUMBER_OF_VARIABLE_MTRR
492 //
493 SystemParameter.VariableMtrrCount = MTRR_NUMBER_OF_VARIABLE_MTRR + 1;
494 InitializeMtrrRegs (&SystemParameter);
496
497 SystemParameter.MtrrSupported = TRUE;
498 SystemParameter.VariableMtrrCount = MAX_UINT8;
499 InitializeMtrrRegs (&SystemParameter);
501
502 return UNIT_TEST_PASSED;
503}
504
516EFIAPI
518 IN UNIT_TEST_CONTEXT Context
519 )
520{
521 UINT32 Result;
522 UINT32 ReservedMtrrs;
523 MTRR_LIB_SYSTEM_PARAMETER SystemParameter;
525
527
528 CopyMem (&SystemParameter, LocalContext->SystemParameter, sizeof (SystemParameter));
529
530 InitializeMtrrRegs (&SystemParameter);
531 //
532 // Positive test cases for VCNT = 10 and Reserved PCD in range 0..10
533 //
534 for (ReservedMtrrs = 0; ReservedMtrrs <= SystemParameter.VariableMtrrCount; ReservedMtrrs++) {
535 PatchPcdSet32 (PcdCpuNumberOfReservedVariableMtrrs, ReservedMtrrs);
537 UT_ASSERT_EQUAL (Result, SystemParameter.VariableMtrrCount - ReservedMtrrs);
538 }
539
540 //
541 // Negative test cases when Reserved PCD is larger than VCNT
542 //
543 for (ReservedMtrrs = SystemParameter.VariableMtrrCount + 1; ReservedMtrrs <= 255; ReservedMtrrs++) {
544 PatchPcdSet32 (PcdCpuNumberOfReservedVariableMtrrs, ReservedMtrrs);
546 UT_ASSERT_EQUAL (Result, 0);
547 }
548
549 //
550 // Negative test cases when Reserved PCD is larger than VCNT
551 //
552 PatchPcdSet32 (PcdCpuNumberOfReservedVariableMtrrs, MAX_UINT32);
554 UT_ASSERT_EQUAL (Result, 0);
555
556 //
557 // Negative test case when MTRRs are not supported
558 //
559 SystemParameter.MtrrSupported = FALSE;
560 InitializeMtrrRegs (&SystemParameter);
561 PatchPcdSet32 (PcdCpuNumberOfReservedVariableMtrrs, 2);
563 UT_ASSERT_EQUAL (Result, 0);
564
565 //
566 // Negative test case when Fixed MTRRs are not supported
567 //
568 SystemParameter.MtrrSupported = TRUE;
569 SystemParameter.FixedMtrrSupported = FALSE;
570 InitializeMtrrRegs (&SystemParameter);
571 PatchPcdSet32 (PcdCpuNumberOfReservedVariableMtrrs, 2);
573 UT_ASSERT_EQUAL (Result, SystemParameter.VariableMtrrCount - 2);
574
575 //
576 // Expect ASSERT() if variable MTRR count is > MTRR_NUMBER_OF_VARIABLE_MTRR
577 //
578 SystemParameter.FixedMtrrSupported = TRUE;
579 SystemParameter.VariableMtrrCount = MTRR_NUMBER_OF_VARIABLE_MTRR + 1;
580 InitializeMtrrRegs (&SystemParameter);
582
583 return UNIT_TEST_PASSED;
584}
585
597EFIAPI
599 IN UNIT_TEST_CONTEXT Context
600 )
601{
602 return UNIT_TEST_PASSED;
603}
604
616EFIAPI
618 IN UNIT_TEST_CONTEXT Context
619 )
620{
621 MTRR_FIXED_SETTINGS *Result;
622 MTRR_FIXED_SETTINGS ExpectedFixedSettings;
623 MTRR_FIXED_SETTINGS FixedSettings;
624 UINTN Index;
625 UINTN MsrIndex;
626 UINTN ByteIndex;
627 UINT64 MsrValue;
628 MTRR_LIB_SYSTEM_PARAMETER SystemParameter;
629 MTRR_LIB_TEST_CONTEXT *LocalContext;
630
631 LocalContext = (MTRR_LIB_TEST_CONTEXT *)Context;
632
633 CopyMem (&SystemParameter, LocalContext->SystemParameter, sizeof (SystemParameter));
634 InitializeMtrrRegs (&SystemParameter);
635 //
636 // Set random cache type to different ranges under 1MB and make sure
637 // the fixed MTRR settings are expected.
638 // Try 100 times.
639 //
640 for (Index = 0; Index < 100; Index++) {
641 for (MsrIndex = 0; MsrIndex < ARRAY_SIZE (mFixedMtrrsIndex); MsrIndex++) {
642 MsrValue = 0;
643 for (ByteIndex = 0; ByteIndex < sizeof (UINT64); ByteIndex++) {
644 MsrValue = MsrValue | LShiftU64 (GenerateRandomCacheType (), ByteIndex * 8);
645 }
646
647 ExpectedFixedSettings.Mtrr[MsrIndex] = MsrValue;
648 AsmWriteMsr64 (mFixedMtrrsIndex[MsrIndex], MsrValue);
649 }
650
651 Result = MtrrGetFixedMtrr (&FixedSettings);
652 UT_ASSERT_EQUAL ((UINTN)Result, (UINTN)&FixedSettings);
653 UT_ASSERT_MEM_EQUAL (&FixedSettings, &ExpectedFixedSettings, sizeof (FixedSettings));
654 }
655
656 //
657 // Negative test case when MTRRs are not supported
658 //
659 SystemParameter.MtrrSupported = FALSE;
660 InitializeMtrrRegs (&SystemParameter);
661
662 ZeroMem (&FixedSettings, sizeof (FixedSettings));
663 ZeroMem (&ExpectedFixedSettings, sizeof (ExpectedFixedSettings));
664 Result = MtrrGetFixedMtrr (&FixedSettings);
665 UT_ASSERT_EQUAL ((UINTN)Result, (UINTN)&FixedSettings);
666 UT_ASSERT_MEM_EQUAL (&ExpectedFixedSettings, &FixedSettings, sizeof (ExpectedFixedSettings));
667
668 //
669 // Negative test case when Fixed MTRRs are not supported
670 //
671 SystemParameter.MtrrSupported = TRUE;
672 SystemParameter.FixedMtrrSupported = FALSE;
673 InitializeMtrrRegs (&SystemParameter);
674
675 ZeroMem (&FixedSettings, sizeof (FixedSettings));
676 ZeroMem (&ExpectedFixedSettings, sizeof (ExpectedFixedSettings));
677 Result = MtrrGetFixedMtrr (&FixedSettings);
678 UT_ASSERT_EQUAL ((UINTN)Result, (UINTN)&FixedSettings);
679 UT_ASSERT_MEM_EQUAL (&ExpectedFixedSettings, &FixedSettings, sizeof (ExpectedFixedSettings));
680
681 return UNIT_TEST_PASSED;
682}
683
691VOID
693 IN MTRR_LIB_SYSTEM_PARAMETER *SystemParameter,
694 IN MTRR_SETTINGS *ExpectedMtrrs
695 )
696{
697 UINT32 Index;
698 UINTN MsrIndex;
699 UINTN ByteIndex;
700 UINT64 MsrValue;
702
703 AsmWriteMsr64 (MSR_IA32_MTRR_DEF_TYPE, ExpectedMtrrs->MtrrDefType);
704 //
705 // Randomly generate Variable MTRR BASE/MASK for a specified type and write to MSR.
706 //
707 for (Index = 0; Index < SystemParameter->VariableMtrrCount; Index++) {
708 GenerateRandomMtrrPair (SystemParameter->PhysicalAddressBits, GenerateRandomCacheType (), &ExpectedMtrrs->Variables.Mtrr[Index], NULL);
709 AsmWriteMsr64 (MSR_IA32_MTRR_PHYSBASE0 + (Index << 1), ExpectedMtrrs->Variables.Mtrr[Index].Base);
710 AsmWriteMsr64 (MSR_IA32_MTRR_PHYSMASK0 + (Index << 1), ExpectedMtrrs->Variables.Mtrr[Index].Mask);
711 }
712
713 //
714 // Set Fixed MTRRs when the Fixed MTRRs is enabled and the MTRRs is supported.
715 //
717 if ((Default.Bits.FE == 1) && (SystemParameter->MtrrSupported == TRUE)) {
718 for (MsrIndex = 0; MsrIndex < ARRAY_SIZE (mFixedMtrrsIndex); MsrIndex++) {
719 MsrValue = 0;
720 for (ByteIndex = 0; ByteIndex < sizeof (UINT64); ByteIndex++) {
721 MsrValue = MsrValue | LShiftU64 (GenerateRandomCacheType (), ByteIndex * 8);
722 }
723
724 ExpectedMtrrs->Fixed.Mtrr[MsrIndex] = MsrValue;
725 AsmWriteMsr64 (mFixedMtrrsIndex[MsrIndex], MsrValue);
726 }
727 }
728}
729
741EFIAPI
743 IN UNIT_TEST_CONTEXT Context
744 )
745{
746 MTRR_SETTINGS *Result;
747 MTRR_SETTINGS Mtrrs;
748 MTRR_SETTINGS ExpectedMtrrs;
749 MTRR_LIB_SYSTEM_PARAMETER SystemParameter;
750 MTRR_LIB_TEST_CONTEXT *LocalContext;
752
753 LocalContext = (MTRR_LIB_TEST_CONTEXT *)Context;
754
755 CopyMem (&SystemParameter, LocalContext->SystemParameter, sizeof (SystemParameter));
756
757 //
758 // For the case that Fixed MTRRs is NOT enabled
759 //
760 SystemParameter.MtrrSupported = TRUE;
761 SystemParameter.FixedMtrrSupported = FALSE;
762 InitializeMtrrRegs (&SystemParameter);
763 Default.Uint64 = 0;
764 Default.Bits.E = 1;
765 Default.Bits.FE = 0;
766 ZeroMem (&ExpectedMtrrs, sizeof (ExpectedMtrrs));
767 ExpectedMtrrs.MtrrDefType = Default.Uint64;
768 //
769 // Randomly generate expected MtrrSettings and set to MSR.
770 //
771 SetRandomlyGeneratedMtrrSettings (&SystemParameter, &ExpectedMtrrs);
772 Result = MtrrGetAllMtrrs (&Mtrrs);
773 UT_ASSERT_MEM_EQUAL (&ExpectedMtrrs.Fixed, &Mtrrs.Fixed, sizeof (MTRR_FIXED_SETTINGS));
774 UT_ASSERT_MEM_EQUAL (Mtrrs.Variables.Mtrr, ExpectedMtrrs.Variables.Mtrr, sizeof (MTRR_VARIABLE_SETTING) * (SystemParameter.VariableMtrrCount));
775
776 //
777 // For the case that Fixed MTRRs is enabled
778 //
779 SystemParameter.MtrrSupported = TRUE;
780 SystemParameter.FixedMtrrSupported = TRUE;
781 InitializeMtrrRegs (&SystemParameter);
782 Default.Uint64 = 0;
783 Default.Bits.E = 1;
784 Default.Bits.FE = 1;
785 ZeroMem (&ExpectedMtrrs, sizeof (ExpectedMtrrs));
786 ExpectedMtrrs.MtrrDefType = Default.Uint64;
787 SetRandomlyGeneratedMtrrSettings (&SystemParameter, &ExpectedMtrrs);
788 Result = MtrrGetAllMtrrs (&Mtrrs);
789 UT_ASSERT_MEM_EQUAL (&ExpectedMtrrs.Fixed, &Mtrrs.Fixed, sizeof (MTRR_FIXED_SETTINGS));
790 UT_ASSERT_MEM_EQUAL (Mtrrs.Variables.Mtrr, ExpectedMtrrs.Variables.Mtrr, sizeof (MTRR_VARIABLE_SETTING) * (SystemParameter.VariableMtrrCount));
791
792 //
793 // Negative test case when MTRRs are not supported
794 //
795 ZeroMem (&ExpectedMtrrs, sizeof (ExpectedMtrrs));
796 ZeroMem (&Mtrrs, sizeof (Mtrrs));
797
798 SystemParameter.MtrrSupported = FALSE;
799 InitializeMtrrRegs (&SystemParameter);
800 Result = MtrrGetAllMtrrs (&Mtrrs);
801 UT_ASSERT_EQUAL ((UINTN)Result, (UINTN)&Mtrrs);
802 UT_ASSERT_MEM_EQUAL (&ExpectedMtrrs, &Mtrrs, sizeof (ExpectedMtrrs));
803
804 //
805 // Expect ASSERT() if variable MTRR count is > MTRR_NUMBER_OF_VARIABLE_MTRR
806 //
807 SystemParameter.MtrrSupported = TRUE;
808 SystemParameter.VariableMtrrCount = MTRR_NUMBER_OF_VARIABLE_MTRR + 1;
809 InitializeMtrrRegs (&SystemParameter);
811
812 return UNIT_TEST_PASSED;
813}
814
823EFIAPI
825 IN UNIT_TEST_CONTEXT Context
826 )
827{
828 MTRR_SETTINGS *Result;
829 MTRR_SETTINGS ExpectedMtrrs;
830 UINT32 Index;
832 MTRR_LIB_SYSTEM_PARAMETER SystemParameter;
833 MTRR_LIB_TEST_CONTEXT *LocalContext;
834 UINTN MsrIndex;
835 UINTN ByteIndex;
836 UINT64 MsrValue;
837
838 LocalContext = (MTRR_LIB_TEST_CONTEXT *)Context;
839 CopyMem (&SystemParameter, LocalContext->SystemParameter, sizeof (SystemParameter));
840 InitializeMtrrRegs (&SystemParameter);
841 Default.Uint64 = 0;
842 Default.Bits.E = 1;
843 Default.Bits.FE = 1;
844 Default.Bits.Type = GenerateRandomCacheType ();
845 ZeroMem (&ExpectedMtrrs, sizeof (ExpectedMtrrs));
846 ExpectedMtrrs.MtrrDefType = Default.Uint64;
847 for (Index = 0; Index < SystemParameter.VariableMtrrCount; Index++) {
848 GenerateRandomMtrrPair (SystemParameter.PhysicalAddressBits, GenerateRandomCacheType (), &ExpectedMtrrs.Variables.Mtrr[Index], NULL);
849 }
850
851 for (MsrIndex = 0; MsrIndex < ARRAY_SIZE (mFixedMtrrsIndex); MsrIndex++) {
852 MsrValue = 0;
853 for (ByteIndex = 0; ByteIndex < sizeof (UINT64); ByteIndex++) {
854 MsrValue = MsrValue | LShiftU64 (GenerateRandomCacheType (), ByteIndex * 8);
855 }
856
857 ExpectedMtrrs.Fixed.Mtrr[MsrIndex] = MsrValue;
858 }
859
860 Result = MtrrSetAllMtrrs (&ExpectedMtrrs);
861 UT_ASSERT_EQUAL ((UINTN)Result, (UINTN)&ExpectedMtrrs);
862 UT_ASSERT_EQUAL (AsmReadMsr64 (MSR_IA32_MTRR_DEF_TYPE), ExpectedMtrrs.MtrrDefType);
863 for (Index = 0; Index < SystemParameter.VariableMtrrCount; Index++) {
864 UT_ASSERT_EQUAL (AsmReadMsr64 (MSR_IA32_MTRR_PHYSBASE0 + (Index << 1)), ExpectedMtrrs.Variables.Mtrr[Index].Base);
865 UT_ASSERT_EQUAL (AsmReadMsr64 (MSR_IA32_MTRR_PHYSMASK0 + (Index << 1)), ExpectedMtrrs.Variables.Mtrr[Index].Mask);
866 }
867
868 return UNIT_TEST_PASSED;
869}
870
882EFIAPI
884 IN UNIT_TEST_CONTEXT Context
885 )
886{
887 MTRR_LIB_TEST_CONTEXT *LocalContext;
888 MTRR_LIB_SYSTEM_PARAMETER SystemParameter;
889 UINT32 Result;
890 MTRR_VARIABLE_SETTING VariableSetting[MTRR_NUMBER_OF_VARIABLE_MTRR];
891 VARIABLE_MTRR VariableMtrr[MTRR_NUMBER_OF_VARIABLE_MTRR];
892 UINT64 ValidMtrrBitsMask;
893 UINT64 ValidMtrrAddressMask;
894 UINT32 Index;
897
898 LocalContext = (MTRR_LIB_TEST_CONTEXT *)Context;
899
900 CopyMem (&SystemParameter, LocalContext->SystemParameter, sizeof (SystemParameter));
901
902 InitializeMtrrRegs (&SystemParameter);
903
904 ValidMtrrBitsMask = (1ull << SystemParameter.PhysicalAddressBits) - 1;
905 ValidMtrrAddressMask = ValidMtrrBitsMask & 0xfffffffffffff000ULL;
906
907 for (Index = 0; Index < SystemParameter.VariableMtrrCount; Index++) {
908 GenerateRandomMtrrPair (SystemParameter.PhysicalAddressBits, GenerateRandomCacheType (), &VariableSetting[Index], NULL);
909 AsmWriteMsr64 (MSR_IA32_MTRR_PHYSBASE0 + (Index << 1), VariableSetting[Index].Base);
910 AsmWriteMsr64 (MSR_IA32_MTRR_PHYSMASK0 + (Index << 1), VariableSetting[Index].Mask);
911 }
912
913 Result = MtrrGetMemoryAttributeInVariableMtrr (ValidMtrrBitsMask, ValidMtrrAddressMask, VariableMtrr);
914 UT_ASSERT_EQUAL (Result, SystemParameter.VariableMtrrCount);
915
916 for (Index = 0; Index < SystemParameter.VariableMtrrCount; Index++) {
917 Base.Uint64 = VariableMtrr[Index].BaseAddress;
918 Base.Bits.Type = (UINT32)VariableMtrr[Index].Type;
919 UT_ASSERT_EQUAL (Base.Uint64, VariableSetting[Index].Base);
920
921 Mask.Uint64 = ~(VariableMtrr[Index].Length - 1) & ValidMtrrBitsMask;
922 Mask.Bits.V = 1;
923 UT_ASSERT_EQUAL (Mask.Uint64, VariableSetting[Index].Mask);
924 }
925
926 //
927 // Negative test case when MTRRs are not supported
928 //
929 SystemParameter.MtrrSupported = FALSE;
930 InitializeMtrrRegs (&SystemParameter);
931 Result = MtrrGetMemoryAttributeInVariableMtrr (ValidMtrrBitsMask, ValidMtrrAddressMask, VariableMtrr);
932 UT_ASSERT_EQUAL (Result, 0);
933
934 //
935 // Expect ASSERT() if variable MTRR count is > MTRR_NUMBER_OF_VARIABLE_MTRR
936 //
937 SystemParameter.MtrrSupported = TRUE;
938 SystemParameter.VariableMtrrCount = MTRR_NUMBER_OF_VARIABLE_MTRR + 1;
939 InitializeMtrrRegs (&SystemParameter);
940 UT_EXPECT_ASSERT_FAILURE (MtrrGetMemoryAttributeInVariableMtrr (ValidMtrrBitsMask, ValidMtrrAddressMask, VariableMtrr), NULL);
941
942 return UNIT_TEST_PASSED;
943}
944
956EFIAPI
958 IN UNIT_TEST_CONTEXT Context
959 )
960{
961 return UNIT_TEST_PASSED;
962}
963
975EFIAPI
977 IN UNIT_TEST_CONTEXT Context
978 )
979{
980 MTRR_LIB_TEST_CONTEXT *LocalContext;
981 UINTN Index;
982 MTRR_MEMORY_CACHE_TYPE Result;
983 MTRR_LIB_SYSTEM_PARAMETER SystemParameter;
984 MTRR_MEMORY_CACHE_TYPE CacheType[5];
985
986 CacheType[0] = CacheUncacheable;
987 CacheType[1] = CacheWriteCombining;
988 CacheType[2] = CacheWriteThrough;
989 CacheType[3] = CacheWriteProtected;
990 CacheType[4] = CacheWriteBack;
991
992 LocalContext = (MTRR_LIB_TEST_CONTEXT *)Context;
993
994 CopyMem (&SystemParameter, LocalContext->SystemParameter, sizeof (SystemParameter));
995 //
996 // If MTRRs are supported, then always return the cache type in the MSR
997 // MSR_IA32_MTRR_DEF_TYPE
998 //
999 for (Index = 0; Index < ARRAY_SIZE (CacheType); Index++) {
1000 SystemParameter.DefaultCacheType = CacheType[Index];
1001 InitializeMtrrRegs (&SystemParameter);
1002 Result = MtrrGetDefaultMemoryType ();
1003 UT_ASSERT_EQUAL (Result, SystemParameter.DefaultCacheType);
1004 }
1005
1006 //
1007 // If MTRRs are not supported, then always return CacheUncacheable
1008 //
1009 SystemParameter.MtrrSupported = FALSE;
1010 InitializeMtrrRegs (&SystemParameter);
1011 Result = MtrrGetDefaultMemoryType ();
1012 UT_ASSERT_EQUAL (Result, CacheUncacheable);
1013
1014 //
1015 // If MTRRs are supported, but Fixed MTRRs are not supported.
1016 //
1017 SystemParameter.MtrrSupported = TRUE;
1018 SystemParameter.FixedMtrrSupported = FALSE;
1019 InitializeMtrrRegs (&SystemParameter);
1020 Result = MtrrGetDefaultMemoryType ();
1021 UT_ASSERT_EQUAL (Result, SystemParameter.DefaultCacheType);
1022
1023 //
1024 // If MTRRs are supported, but Variable MTRRs are not supported.
1025 //
1026 SystemParameter.MtrrSupported = TRUE;
1027 SystemParameter.FixedMtrrSupported = TRUE;
1028 SystemParameter.VariableMtrrCount = 0;
1029 InitializeMtrrRegs (&SystemParameter);
1030 Result = MtrrGetDefaultMemoryType ();
1031 UT_ASSERT_EQUAL (Result, SystemParameter.DefaultCacheType);
1032
1033 return UNIT_TEST_PASSED;
1034}
1035
1048EFIAPI
1050 IN UNIT_TEST_CONTEXT Context
1051 )
1052{
1053 CONST MTRR_LIB_SYSTEM_PARAMETER *SystemParameter;
1054 RETURN_STATUS Status;
1055 UINT32 UcCount;
1056 UINT32 WtCount;
1057 UINT32 WbCount;
1058 UINT32 WpCount;
1059 UINT32 WcCount;
1060
1061 UINTN MtrrIndex;
1062 UINTN Index;
1063 MTRR_SETTINGS LocalMtrrs;
1064
1065 MTRR_MEMORY_RANGE RawMtrrRange[MTRR_NUMBER_OF_VARIABLE_MTRR];
1066 MTRR_MEMORY_RANGE ExpectedMemoryRanges[MTRR_NUMBER_OF_FIXED_MTRR * sizeof (UINT64) + 2 * MTRR_NUMBER_OF_VARIABLE_MTRR + 1];
1067 UINT32 ExpectedVariableMtrrUsage;
1068 UINTN ExpectedMemoryRangesCount;
1069
1070 MTRR_MEMORY_RANGE ActualMemoryRanges[MTRR_NUMBER_OF_FIXED_MTRR * sizeof (UINT64) + 2 * MTRR_NUMBER_OF_VARIABLE_MTRR + 1];
1071 UINT32 ActualVariableMtrrUsage;
1072 UINTN ActualMemoryRangesCount;
1073
1074 MTRR_MEMORY_RANGE ReturnedMemoryRanges[MTRR_NUMBER_OF_FIXED_MTRR * sizeof (UINT64) + 2 * MTRR_NUMBER_OF_VARIABLE_MTRR + 1];
1075 UINTN ReturnedMemoryRangesCount;
1076
1077 MTRR_SETTINGS *Mtrrs[2];
1078
1079 SystemParameter = (MTRR_LIB_SYSTEM_PARAMETER *)Context;
1081 SystemParameter->VariableMtrrCount - PatchPcdGet32 (PcdCpuNumberOfReservedVariableMtrrs),
1082 &UcCount,
1083 &WtCount,
1084 &WbCount,
1085 &WpCount,
1086 &WcCount
1087 );
1089 SystemParameter->PhysicalAddressBits - SystemParameter->MkTmeKeyidBits,
1090 RawMtrrRange,
1091 UcCount,
1092 WtCount,
1093 WbCount,
1094 WpCount,
1095 WcCount
1096 );
1097
1098 ExpectedVariableMtrrUsage = UcCount + WtCount + WbCount + WpCount + WcCount;
1099 ExpectedMemoryRangesCount = ARRAY_SIZE (ExpectedMemoryRanges);
1101 SystemParameter->DefaultCacheType,
1102 SystemParameter->PhysicalAddressBits - SystemParameter->MkTmeKeyidBits,
1103 RawMtrrRange,
1104 ExpectedVariableMtrrUsage,
1105 ExpectedMemoryRanges,
1106 &ExpectedMemoryRangesCount
1107 );
1108
1109 UT_LOG_INFO ("--- Expected Memory Ranges [%d] ---\n", ExpectedMemoryRangesCount);
1110 DumpMemoryRanges (ExpectedMemoryRanges, ExpectedMemoryRangesCount);
1111 //
1112 // Default cache type is always an INPUT
1113 //
1114 ZeroMem (&LocalMtrrs, sizeof (LocalMtrrs));
1115 LocalMtrrs.MtrrDefType = MtrrGetDefaultMemoryType ();
1116 Mtrrs[0] = &LocalMtrrs;
1117 Mtrrs[1] = NULL;
1118
1119 for (MtrrIndex = 0; MtrrIndex < ARRAY_SIZE (Mtrrs); MtrrIndex++) {
1120 for (Index = 0; Index < ExpectedMemoryRangesCount; Index++) {
1122 Mtrrs[MtrrIndex],
1123 ExpectedMemoryRanges[Index].BaseAddress,
1124 ExpectedMemoryRanges[Index].Length,
1125 ExpectedMemoryRanges[Index].Type
1126 );
1128 if ((Status == RETURN_OUT_OF_RESOURCES) || (Status == RETURN_BUFFER_TOO_SMALL)) {
1129 return UNIT_TEST_SKIPPED;
1130 }
1131 }
1132
1133 if (Mtrrs[MtrrIndex] == NULL) {
1134 ZeroMem (&LocalMtrrs, sizeof (LocalMtrrs));
1135 MtrrGetAllMtrrs (&LocalMtrrs);
1136 }
1137
1138 ActualMemoryRangesCount = ARRAY_SIZE (ActualMemoryRanges);
1140 SystemParameter->DefaultCacheType,
1141 SystemParameter->PhysicalAddressBits - SystemParameter->MkTmeKeyidBits,
1142 SystemParameter->VariableMtrrCount,
1143 &LocalMtrrs,
1144 ActualMemoryRanges,
1145 &ActualMemoryRangesCount,
1146 &ActualVariableMtrrUsage
1147 );
1148 UT_LOG_INFO ("--- Actual Memory Ranges [%d] ---\n", ActualMemoryRangesCount);
1149 DumpMemoryRanges (ActualMemoryRanges, ActualMemoryRangesCount);
1150 VerifyMemoryRanges (ExpectedMemoryRanges, ExpectedMemoryRangesCount, ActualMemoryRanges, ActualMemoryRangesCount);
1151 UT_ASSERT_TRUE (ExpectedVariableMtrrUsage >= ActualVariableMtrrUsage);
1152
1153 ReturnedMemoryRangesCount = ARRAY_SIZE (ReturnedMemoryRanges);
1155 &LocalMtrrs,
1156 ReturnedMemoryRanges,
1157 &ReturnedMemoryRangesCount
1158 );
1160 UT_LOG_INFO ("--- Returned Memory Ranges [%d] ---\n", ReturnedMemoryRangesCount);
1161 DumpMemoryRanges (ReturnedMemoryRanges, ReturnedMemoryRangesCount);
1162 VerifyMemoryRanges (ExpectedMemoryRanges, ExpectedMemoryRangesCount, ReturnedMemoryRanges, ReturnedMemoryRangesCount);
1163
1164 ZeroMem (&LocalMtrrs, sizeof (LocalMtrrs));
1165 }
1166
1167 return UNIT_TEST_PASSED;
1168}
1169
1176EFIAPI
1178 UNIT_TEST_CONTEXT Context
1179 )
1180{
1182
1183 LocalContext = (MTRR_LIB_GET_FIRMWARE_VARIABLE_MTRR_COUNT_CONTEXT *)Context;
1184 LocalContext->NumberOfReservedVariableMtrrs = PatchPcdGet32 (PcdCpuNumberOfReservedVariableMtrrs);
1185 return UNIT_TEST_PASSED;
1186}
1187
1193VOID
1194EFIAPI
1196 UNIT_TEST_CONTEXT Context
1197 )
1198{
1200
1201 LocalContext = (MTRR_LIB_GET_FIRMWARE_VARIABLE_MTRR_COUNT_CONTEXT *)Context;
1202 PatchPcdSet32 (PcdCpuNumberOfReservedVariableMtrrs, LocalContext->NumberOfReservedVariableMtrrs);
1203}
1204
1216STATIC
1218EFIAPI
1220 UINTN Iteration
1221 )
1222{
1223 EFI_STATUS Status;
1224 UNIT_TEST_FRAMEWORK_HANDLE Framework;
1225 UNIT_TEST_SUITE_HANDLE MtrrApiTests;
1226 UINTN Index;
1227 UINTN SystemIndex;
1228 MTRR_LIB_TEST_CONTEXT Context;
1229 MTRR_LIB_GET_FIRMWARE_VARIABLE_MTRR_COUNT_CONTEXT GetFirmwareVariableMtrrCountContext;
1230
1231 Context.SystemParameter = &mDefaultSystemParameter;
1232 GetFirmwareVariableMtrrCountContext.SystemParameter = &mDefaultSystemParameter;
1233 Framework = NULL;
1234
1235 //
1236 // Setup the test framework for running the tests.
1237 //
1238 Status = InitUnitTestFramework (&Framework, UNIT_TEST_APP_NAME, gEfiCallerBaseName, UNIT_TEST_APP_VERSION);
1239 if (EFI_ERROR (Status)) {
1240 DEBUG ((DEBUG_ERROR, "Failed in InitUnitTestFramework. Status = %r\n", Status));
1241 goto EXIT;
1242 }
1243
1244 //
1245 // --------------Suite-----------Description--------------Name----------Function--------Pre---Post-------------------Context-----------
1246 //
1247
1248 //
1249 // Populate the MtrrLib API Unit Test Suite.
1250 //
1251 Status = CreateUnitTestSuite (&MtrrApiTests, Framework, "MtrrLib API Tests", "MtrrLib.MtrrLib", NULL, NULL);
1252 if (EFI_ERROR (Status)) {
1253 DEBUG ((DEBUG_ERROR, "Failed in CreateUnitTestSuite for MtrrLib API Tests\n"));
1254 Status = EFI_OUT_OF_RESOURCES;
1255 goto EXIT;
1256 }
1257
1258 AddTestCase (MtrrApiTests, "Test IsMtrrSupported", "MtrrSupported", UnitTestIsMtrrSupported, NULL, NULL, &Context);
1259 AddTestCase (MtrrApiTests, "Test GetVariableMtrrCount", "GetVariableMtrrCount", UnitTestGetVariableMtrrCount, NULL, NULL, &Context);
1260 AddTestCase (MtrrApiTests, "Test GetFirmwareVariableMtrrCount", "GetFirmwareVariableMtrrCount", UnitTestGetFirmwareVariableMtrrCount, SavePcdValue, RestorePcdValue, &GetFirmwareVariableMtrrCountContext);
1261 AddTestCase (MtrrApiTests, "Test MtrrGetMemoryAttribute", "MtrrGetMemoryAttribute", UnitTestMtrrGetMemoryAttribute, NULL, NULL, &Context);
1262 AddTestCase (MtrrApiTests, "Test MtrrGetFixedMtrr", "MtrrGetFixedMtrr", UnitTestMtrrGetFixedMtrr, NULL, NULL, &Context);
1263 AddTestCase (MtrrApiTests, "Test MtrrGetAllMtrrs", "MtrrGetAllMtrrs", UnitTestMtrrGetAllMtrrs, NULL, NULL, &Context);
1264 AddTestCase (MtrrApiTests, "Test MtrrSetAllMtrrs", "MtrrSetAllMtrrs", UnitTestMtrrSetAllMtrrs, NULL, NULL, &Context);
1265 AddTestCase (MtrrApiTests, "Test MtrrGetMemoryAttributeInVariableMtrr", "MtrrGetMemoryAttributeInVariableMtrr", UnitTestMtrrGetMemoryAttributeInVariableMtrr, NULL, NULL, &Context);
1266 AddTestCase (MtrrApiTests, "Test MtrrDebugPrintAllMtrrs", "MtrrDebugPrintAllMtrrs", UnitTestMtrrDebugPrintAllMtrrs, NULL, NULL, &Context);
1267 AddTestCase (MtrrApiTests, "Test MtrrGetDefaultMemoryType", "MtrrGetDefaultMemoryType", UnitTestMtrrGetDefaultMemoryType, NULL, NULL, &Context);
1268
1269 for (SystemIndex = 0; SystemIndex < ARRAY_SIZE (mSystemParameters); SystemIndex++) {
1270 for (Index = 0; Index < Iteration; Index++) {
1271 AddTestCase (MtrrApiTests, "Test InvalidMemoryLayouts", "InvalidMemoryLayouts", UnitTestInvalidMemoryLayouts, InitializeSystem, NULL, &mSystemParameters[SystemIndex]);
1272 AddTestCase (MtrrApiTests, "Test MtrrSetMemoryAttributeInMtrrSettings and MtrrGetMemoryAttributesInMtrrSettings", "MtrrSetMemoryAttributeInMtrrSettings and MtrrGetMemoryAttributesInMtrrSettings", UnitTestMtrrSetMemoryAttributeAndGetMemoryAttributesInMtrrSettings, InitializeSystem, NULL, &mSystemParameters[SystemIndex]);
1273 AddTestCase (MtrrApiTests, "Test MtrrSetMemoryAttributesInMtrrSettings and MtrrGetMemoryAttributesInMtrrSettings", "MtrrSetMemoryAttributesInMtrrSettings and MtrrGetMemoryAttributesInMtrrSetting", UnitTestMtrrSetAndGetMemoryAttributesInMtrrSettings, InitializeSystem, NULL, &mSystemParameters[SystemIndex]);
1274 }
1275 }
1276
1277 //
1278 // Execute the tests.
1279 //
1280 Status = RunAllTestSuites (Framework);
1281
1282EXIT:
1283 if (Framework != NULL) {
1284 FreeUnitTestFramework (Framework);
1285 }
1286
1287 return Status;
1288}
1289
1298INT32
1300 INT32 Argc,
1301 CHAR8 *Argv[]
1302 )
1303{
1304 UINTN Count;
1305
1306 DEBUG ((DEBUG_INFO, "%a v%a\n", UNIT_TEST_APP_NAME, UNIT_TEST_APP_VERSION));
1307 srand ((unsigned int)time (NULL));
1308
1309 //
1310 // MtrrLibUnitTest generate-random-numbers <path to MtrrLib/UnitTest/RandomNumber.c> <random-number count>
1311 //
1312 if ((Argc == 4) && (AsciiStriCmp ("generate-random-numbers", Argv[1]) == 0)) {
1313 Count = atoi (Argv[3]);
1314 DEBUG ((DEBUG_INFO, "Generate %d random numbers to %a.\n", Count, Argv[2]));
1315 GenerateRandomNumbers (Argv[2], Count);
1316 return 0;
1317 }
1318
1319 //
1320 // MtrrLibUnitTest [<iterations>]
1321 // <iterations> [fixed|random]
1322 // Default <iterations> is 10.
1323 // Default uses fixed inputs.
1324 //
1325 Count = 10;
1326 mRandomInput = FALSE;
1327 if ((Argc == 2) || (Argc == 3)) {
1328 Count = atoi (Argv[1]);
1329 if (Argc == 3) {
1330 if (AsciiStriCmp ("fixed", Argv[2]) == 0) {
1331 mRandomInput = FALSE;
1332 } else if (AsciiStriCmp ("random", Argv[2]) == 0) {
1333 mRandomInput = TRUE;
1334 }
1335 }
1336 }
1337
1338 DEBUG ((DEBUG_INFO, "Iterations = %d\n", Count));
1339 DEBUG ((DEBUG_INFO, "Input = %a\n", mRandomInput ? "random" : "fixed"));
1340
1341 return UnitTestingEntry (Count);
1342}
UINT64 UINTN
INTN EFIAPI AsciiStriCmp(IN CONST CHAR8 *FirstString, IN CONST CHAR8 *SecondString)
Definition: String.c:814
UINT64 EFIAPI LShiftU64(IN UINT64 Operand, IN UINTN Count)
Definition: LShiftU64.c:28
VOID *EFIAPI CopyMem(OUT VOID *DestinationBuffer, IN CONST VOID *SourceBuffer, IN UINTN Length)
VOID *EFIAPI ZeroMem(OUT VOID *Buffer, IN UINTN Length)
int main()
=== TEST ENGINE ================================================================================
UINT64 EFIAPI AsmReadMsr64(IN UINT32 Index)
Definition: GccInlinePriv.c:60
UINT64 EFIAPI AsmWriteMsr64(IN UINT32 Index, IN UINT64 Value)
#define NULL
Definition: Base.h:319
#define CONST
Definition: Base.h:259
#define RETURN_BUFFER_TOO_SMALL
Definition: Base.h:1093
#define STATIC
Definition: Base.h:264
#define RETURN_ERROR(StatusCode)
Definition: Base.h:1061
#define RETURN_OUT_OF_RESOURCES
Definition: Base.h:1114
#define RETURN_SUCCESS
Definition: Base.h:1066
#define TRUE
Definition: Base.h:301
#define FALSE
Definition: Base.h:307
#define STATIC_ASSERT
Definition: Base.h:808
#define ARRAY_SIZE(Array)
Definition: Base.h:1393
#define IN
Definition: Base.h:279
#define OUT
Definition: Base.h:284
#define DEBUG(Expression)
Definition: DebugLib.h:434
#define MSR_IA32_MTRR_PHYSBASE0
#define MSR_IA32_MTRR_FIX4K_E0000
#define MSR_IA32_MTRR_DEF_TYPE
#define MSR_IA32_MTRR_FIX4K_C8000
#define MSR_IA32_MTRR_FIX4K_E8000
#define MSR_IA32_MTRR_FIX4K_F8000
#define MSR_IA32_MTRR_FIX16K_80000
#define MSR_IA32_MTRR_FIX16K_A0000
#define MSR_IA32_MTRR_FIX4K_D0000
#define MSR_IA32_MTRR_PHYSMASK0
#define MSR_IA32_MTRR_FIX64K_00000
#define MSR_IA32_MTRR_FIX4K_D8000
#define MSR_IA32_MTRR_FIX4K_C0000
#define MSR_IA32_MTRR_FIX4K_F0000
UINT32 EFIAPI MtrrGetMemoryAttributeInVariableMtrr(IN UINT64 MtrrValidBitsMask, IN UINT64 MtrrValidAddressMask, OUT VARIABLE_MTRR *VariableMtrr)
Definition: MtrrLib.c:740
MTRR_SETTINGS *EFIAPI MtrrSetAllMtrrs(IN MTRR_SETTINGS *MtrrSetting)
Definition: MtrrLib.c:2908
RETURN_STATUS EFIAPI MtrrGetMemoryAttributesInMtrrSettings(IN CONST MTRR_SETTINGS *MtrrSetting OPTIONAL, OUT MTRR_MEMORY_RANGE *Ranges, IN OUT UINTN *RangeCount)
Definition: MtrrLib.c:2984
RETURN_STATUS EFIAPI MtrrSetMemoryAttributeInMtrrSettings(IN OUT MTRR_SETTINGS *MtrrSetting, IN PHYSICAL_ADDRESS BaseAddress, IN UINT64 Length, IN MTRR_MEMORY_CACHE_TYPE Attribute)
Definition: MtrrLib.c:2729
UINT32 EFIAPI GetVariableMtrrCount(VOID)
Definition: MtrrLib.c:235
UINT32 EFIAPI GetFirmwareVariableMtrrCount(VOID)
Definition: MtrrLib.c:277
BOOLEAN EFIAPI IsMtrrSupported(VOID)
Definition: MtrrLib.c:2960
RETURN_STATUS EFIAPI MtrrSetMemoryAttribute(IN PHYSICAL_ADDRESS BaseAddress, IN UINT64 Length, IN MTRR_MEMORY_CACHE_TYPE Attribute)
Definition: MtrrLib.c:2782
MTRR_FIXED_SETTINGS *EFIAPI MtrrGetFixedMtrr(OUT MTRR_FIXED_SETTINGS *FixedSettings)
Definition: MtrrLib.c:474
MTRR_MEMORY_CACHE_TYPE EFIAPI MtrrGetDefaultMemoryType(VOID)
Definition: MtrrLib.c:324
RETURN_STATUS EFIAPI MtrrSetMemoryAttributesInMtrrSettings(IN OUT MTRR_SETTINGS *MtrrSetting, IN VOID *Scratch, IN OUT UINTN *ScratchSize, IN CONST MTRR_MEMORY_RANGE *Ranges, IN UINTN RangeCount)
Definition: MtrrLib.c:2336
MTRR_SETTINGS *EFIAPI MtrrGetAllMtrrs(OUT MTRR_SETTINGS *MtrrSetting)
Definition: MtrrLib.c:2851
UNIT_TEST_STATUS EFIAPI UnitTestIsMtrrSupported(IN UNIT_TEST_CONTEXT Context)
VOID SetRandomlyGeneratedMtrrSettings(IN MTRR_LIB_SYSTEM_PARAMETER *SystemParameter, IN MTRR_SETTINGS *ExpectedMtrrs)
UNIT_TEST_STATUS VerifyMemoryRanges(IN MTRR_MEMORY_RANGE *ExpectedMemoryRanges, IN UINTN ExpectedMemoryRangeCount, IN MTRR_MEMORY_RANGE *ActualRanges, IN UINTN ActualRangeCount)
VOID DumpMemoryRanges(MTRR_MEMORY_RANGE *Ranges, UINTN RangeCount)
UNIT_TEST_STATUS EFIAPI UnitTestMtrrGetMemoryAttributeInVariableMtrr(IN UNIT_TEST_CONTEXT Context)
UNIT_TEST_STATUS EFIAPI UnitTestMtrrGetAllMtrrs(IN UNIT_TEST_CONTEXT Context)
UNIT_TEST_STATUS EFIAPI UnitTestMtrrDebugPrintAllMtrrs(IN UNIT_TEST_CONTEXT Context)
UNIT_TEST_STATUS EFIAPI UnitTestGetFirmwareVariableMtrrCount(IN UNIT_TEST_CONTEXT Context)
UNIT_TEST_STATUS EFIAPI UnitTestMtrrSetAllMtrrs(IN UNIT_TEST_CONTEXT Context)
STATIC EFI_STATUS EFIAPI UnitTestingEntry(UINTN Iteration)
VOID GenerateRandomMemoryTypeCombination(IN UINT32 TotalCount, OUT UINT32 *UcCount, OUT UINT32 *WtCount, OUT UINT32 *WbCount, OUT UINT32 *WpCount, OUT UINT32 *WcCount)
UNIT_TEST_STATUS EFIAPI UnitTestMtrrGetFixedMtrr(IN UNIT_TEST_CONTEXT Context)
UNIT_TEST_STATUS EFIAPI UnitTestMtrrSetMemoryAttributeAndGetMemoryAttributesInMtrrSettings(IN UNIT_TEST_CONTEXT Context)
VOID EFIAPI RestorePcdValue(UNIT_TEST_CONTEXT Context)
UNIT_TEST_STATUS EFIAPI UnitTestMtrrSetAndGetMemoryAttributesInMtrrSettings(IN UNIT_TEST_CONTEXT Context)
UNIT_TEST_STATUS EFIAPI UnitTestMtrrGetMemoryAttribute(IN UNIT_TEST_CONTEXT Context)
UNIT_TEST_STATUS EFIAPI SavePcdValue(UNIT_TEST_CONTEXT Context)
UNIT_TEST_STATUS EFIAPI UnitTestMtrrGetDefaultMemoryType(IN UNIT_TEST_CONTEXT Context)
UNIT_TEST_STATUS EFIAPI UnitTestGetVariableMtrrCount(IN UNIT_TEST_CONTEXT Context)
UNIT_TEST_STATUS EFIAPI UnitTestInvalidMemoryLayouts(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)
Definition: Support.c:437
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)
Definition: Support.c:606
VOID GenerateRandomNumbers(CHAR8 *FilePath, UINTN Count)
Definition: Support.c:68
UNIT_TEST_STATUS EFIAPI InitializeMtrrRegs(IN MTRR_LIB_SYSTEM_PARAMETER *SystemParameter)
Definition: Support.c:358
MTRR_MEMORY_CACHE_TYPE GenerateRandomCacheType(VOID)
Definition: Support.c:658
UNIT_TEST_STATUS EFIAPI InitializeSystem(IN UNIT_TEST_CONTEXT Context)
Definition: Support.c:418
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)
Definition: Support.c:1009
VOID GenerateRandomMtrrPair(IN UINT32 PhysicalAddressBits, IN MTRR_MEMORY_CACHE_TYPE CacheType, OUT MTRR_VARIABLE_SETTING *MtrrPair OPTIONAL, OUT MTRR_MEMORY_RANGE *MtrrMemoryRange OPTIONAL)
Definition: Support.c:514
#define PatchPcdSet32(TokenName, Value)
Definition: PcdLib.h:263
#define PatchPcdGet32(TokenName)
Definition: PcdLib.h:176
UINT64 Random64(UINT64 Start, UINT64 Limit)
Definition: RandomTest.c:111
UINT32 Random32(UINT32 Start, UINT32 Limit)
Definition: RandomTest.c:92
RETURN_STATUS EFI_STATUS
Definition: UefiBaseType.h:29
EFI_STATUS EFIAPI RunAllTestSuites(IN UNIT_TEST_FRAMEWORK_HANDLE FrameworkHandle)
Definition: RunTests.c:145
VOID * UNIT_TEST_CONTEXT
Definition: UnitTestLib.h:54
#define UT_EXPECT_ASSERT_FAILURE(FunctionCall, Status)
Definition: UnitTestLib.h:491
#define UT_ASSERT_MEM_EQUAL(BufferA, BufferB, Length)
Definition: UnitTestLib.h:389
#define UT_ASSERT_TRUE(Expression)
Definition: UnitTestLib.h:350
#define UT_ASSERT_EQUAL(ValueA, ValueB)
Definition: UnitTestLib.h:375
#define UT_ASSERT_STATUS_EQUAL(Status, Expected)
Definition: UnitTestLib.h:427
#define UT_LOG_INFO(Format,...)
Definition: UnitTestLib.h:813
EFI_STATUS EFIAPI CreateUnitTestSuite(OUT UNIT_TEST_SUITE_HANDLE *SuiteHandle, IN UNIT_TEST_FRAMEWORK_HANDLE FrameworkHandle, IN CHAR8 *Title, IN CHAR8 *Name, IN UNIT_TEST_SUITE_SETUP Setup OPTIONAL, IN UNIT_TEST_SUITE_TEARDOWN Teardown OPTIONAL)
Definition: UnitTestLib.c:326
EFI_STATUS EFIAPI FreeUnitTestFramework(IN UNIT_TEST_FRAMEWORK_HANDLE FrameworkHandle)
Definition: UnitTestLib.c:150
EFI_STATUS EFIAPI AddTestCase(IN UNIT_TEST_SUITE_HANDLE SuiteHandle, IN CHAR8 *Description, IN CHAR8 *Name, IN UNIT_TEST_FUNCTION Function, IN UNIT_TEST_PREREQUISITE Prerequisite OPTIONAL, IN UNIT_TEST_CLEANUP CleanUp OPTIONAL, IN UNIT_TEST_CONTEXT Context OPTIONAL)
Definition: UnitTestLib.c:426
EFI_STATUS EFIAPI InitUnitTestFramework(OUT UNIT_TEST_FRAMEWORK_HANDLE *FrameworkHandle, IN CHAR8 *Title, IN CHAR8 *ShortTitle, IN CHAR8 *VersionString)
Definition: UnitTestLib.c:204
UINT32 UNIT_TEST_STATUS
Definition: UnitTestLib.h:16
#define UT_ASSERT_FALSE(Expression)
Definition: UnitTestLib.h:362
struct MSR_IA32_MTRR_DEF_TYPE_REGISTER::@653 Bits
struct MSR_IA32_MTRR_PHYSBASE_REGISTER::@649 Bits