]> git.proxmox.com Git - mirror_edk2.git/blob - UefiCpuPkg/Library/MtrrLib/UnitTest/MtrrLibUnitTest.c
UefiCpuPkg: Apply uncrustify changes
[mirror_edk2.git] / UefiCpuPkg / Library / MtrrLib / UnitTest / MtrrLibUnitTest.c
1 /** @file
2 Unit tests of the MtrrLib instance of the MtrrLib class
3
4 Copyright (c) 2020, Intel Corporation. All rights reserved.<BR>
5 SPDX-License-Identifier: BSD-2-Clause-Patent
6
7 **/
8
9 #include "MtrrLibUnitTest.h"
10
11 STATIC CONST MTRR_LIB_SYSTEM_PARAMETER mDefaultSystemParameter = {
12 42, TRUE, TRUE, CacheUncacheable, 12
13 };
14
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 },
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
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
47 };
48 STATIC_ASSERT (
49 (ARRAY_SIZE (mFixedMtrrsIndex) == MTRR_NUMBER_OF_FIXED_MTRR),
50 "gFixedMtrrIndex does NOT contain all the fixed MTRRs!"
51 );
52
53 //
54 // Context structure to be used for most of the test cases.
55 //
56 typedef struct {
57 CONST MTRR_LIB_SYSTEM_PARAMETER *SystemParameter;
58 } MTRR_LIB_TEST_CONTEXT;
59
60 //
61 // Context structure to be used for GetFirmwareVariableMtrrCount() test.
62 //
63 typedef struct {
64 UINT32 NumberOfReservedVariableMtrrs;
65 CONST MTRR_LIB_SYSTEM_PARAMETER *SystemParameter;
66 } MTRR_LIB_GET_FIRMWARE_VARIABLE_MTRR_COUNT_CONTEXT;
67
68 STATIC CHAR8 *mCacheDescription[] = { "UC", "WC", "N/A", "N/A", "WT", "WP", "WB" };
69
70 /**
71 Compare the actual memory ranges against expected memory ranges and return PASS when they match.
72
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.
77
78 @retval UNIT_TEST_PASSED Test passed.
79 @retval others Test failed.
80 **/
81 UNIT_TEST_STATUS
82 VerifyMemoryRanges (
83 IN MTRR_MEMORY_RANGE *ExpectedMemoryRanges,
84 IN UINTN ExpectedMemoryRangeCount,
85 IN MTRR_MEMORY_RANGE *ActualRanges,
86 IN UINTN ActualRangeCount
87 )
88 {
89 UINTN Index;
90
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);
96 }
97
98 return UNIT_TEST_PASSED;
99 }
100
101 /**
102 Dump the memory ranges.
103
104 @param Ranges Memory ranges to dump.
105 @param RangeCount Count of memory ranges.
106 **/
107 VOID
108 DumpMemoryRanges (
109 MTRR_MEMORY_RANGE *Ranges,
110 UINTN RangeCount
111 )
112 {
113 UINTN Index;
114
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]);
117 }
118 }
119
120 /**
121 **/
122
123 /**
124 Generate random count of MTRRs for each cache type.
125
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.
132 **/
133 VOID
134 GenerateRandomMemoryTypeCombination (
135 IN UINT32 TotalCount,
136 OUT UINT32 *UcCount,
137 OUT UINT32 *WtCount,
138 OUT UINT32 *WbCount,
139 OUT UINT32 *WpCount,
140 OUT UINT32 *WcCount
141 )
142 {
143 UINTN Index;
144 UINT32 TotalMtrrCount;
145 UINT32 *CountPerType[5];
146
147 CountPerType[0] = UcCount;
148 CountPerType[1] = WtCount;
149 CountPerType[2] = WbCount;
150 CountPerType[3] = WpCount;
151 CountPerType[4] = WcCount;
152
153 //
154 // Initialize the count of each cache type to 0.
155 //
156 for (Index = 0; Index < ARRAY_SIZE (CountPerType); Index++) {
157 *(CountPerType[Index]) = 0;
158 }
159
160 //
161 // Pick a random count of MTRRs
162 //
163 TotalMtrrCount = Random32 (1, TotalCount);
164 for (Index = 0; Index < TotalMtrrCount; Index++) {
165 //
166 // For each of them, pick a random cache type.
167 //
168 (*(CountPerType[Random32 (0, ARRAY_SIZE (CountPerType) - 1)]))++;
169 }
170 }
171
172 /**
173 Unit test of MtrrLib service MtrrSetMemoryAttribute()
174
175 @param[in] Context Ignored
176
177 @retval UNIT_TEST_PASSED The Unit test has completed and the test
178 case was successful.
179 @retval UNIT_TEST_ERROR_TEST_FAILED A test case assertion has failed.
180
181 **/
182 UNIT_TEST_STATUS
183 EFIAPI
184 UnitTestMtrrSetMemoryAttributesInMtrrSettings (
185 IN UNIT_TEST_CONTEXT Context
186 )
187 {
188 CONST MTRR_LIB_SYSTEM_PARAMETER *SystemParameter;
189 RETURN_STATUS Status;
190 UINT32 UcCount;
191 UINT32 WtCount;
192 UINT32 WbCount;
193 UINT32 WpCount;
194 UINT32 WcCount;
195
196 UINT32 MtrrIndex;
197 UINT8 *Scratch;
198 UINTN ScratchSize;
199 MTRR_SETTINGS LocalMtrrs;
200
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;
205
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;
209
210 MTRR_SETTINGS *Mtrrs[2];
211
212 SystemParameter = (MTRR_LIB_SYSTEM_PARAMETER *)Context;
213 GenerateRandomMemoryTypeCombination (
214 SystemParameter->VariableMtrrCount - PatchPcdGet32 (PcdCpuNumberOfReservedVariableMtrrs),
215 &UcCount,
216 &WtCount,
217 &WbCount,
218 &WpCount,
219 &WcCount
220 );
221 GenerateValidAndConfigurableMtrrPairs (
222 SystemParameter->PhysicalAddressBits,
223 RawMtrrRange,
224 UcCount,
225 WtCount,
226 WbCount,
227 WpCount,
228 WcCount
229 );
230
231 ExpectedVariableMtrrUsage = UcCount + WtCount + WbCount + WpCount + WcCount;
232 ExpectedMemoryRangesCount = ARRAY_SIZE (ExpectedMemoryRanges);
233 GetEffectiveMemoryRanges (
234 SystemParameter->DefaultCacheType,
235 SystemParameter->PhysicalAddressBits,
236 RawMtrrRange,
237 ExpectedVariableMtrrUsage,
238 ExpectedMemoryRanges,
239 &ExpectedMemoryRangesCount
240 );
241
242 UT_LOG_INFO (
243 "Total MTRR [%d]: UC=%d, WT=%d, WB=%d, WP=%d, WC=%d\n",
244 ExpectedVariableMtrrUsage,
245 UcCount,
246 WtCount,
247 WbCount,
248 WpCount,
249 WcCount
250 );
251 UT_LOG_INFO ("--- Expected Memory Ranges [%d] ---\n", ExpectedMemoryRangesCount);
252 DumpMemoryRanges (ExpectedMemoryRanges, ExpectedMemoryRangesCount);
253
254 //
255 // Default cache type is always an INPUT
256 //
257 ZeroMem (&LocalMtrrs, sizeof (LocalMtrrs));
258 LocalMtrrs.MtrrDefType = MtrrGetDefaultMemoryType ();
259 ScratchSize = SCRATCH_BUFFER_SIZE;
260 Mtrrs[0] = &LocalMtrrs;
261 Mtrrs[1] = NULL;
262
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);
269 }
270
271 UT_ASSERT_STATUS_EQUAL (Status, RETURN_SUCCESS);
272
273 if (Mtrrs[MtrrIndex] == NULL) {
274 ZeroMem (&LocalMtrrs, sizeof (LocalMtrrs));
275 MtrrGetAllMtrrs (&LocalMtrrs);
276 }
277
278 ActualMemoryRangesCount = ARRAY_SIZE (ActualMemoryRanges);
279 CollectTestResult (
280 SystemParameter->DefaultCacheType,
281 SystemParameter->PhysicalAddressBits,
282 SystemParameter->VariableMtrrCount,
283 &LocalMtrrs,
284 ActualMemoryRanges,
285 &ActualMemoryRangesCount,
286 &ActualVariableMtrrUsage
287 );
288
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);
293
294 ZeroMem (&LocalMtrrs, sizeof (LocalMtrrs));
295 }
296
297 free (Scratch);
298
299 return UNIT_TEST_PASSED;
300 }
301
302 /**
303 Test routine to check whether invalid base/size can be rejected.
304
305 @param Context Pointer to MTRR_LIB_SYSTEM_PARAMETER.
306
307 @return Test status.
308 **/
309 UNIT_TEST_STATUS
310 EFIAPI
311 UnitTestInvalidMemoryLayouts (
312 IN UNIT_TEST_CONTEXT Context
313 )
314 {
315 CONST MTRR_LIB_SYSTEM_PARAMETER *SystemParameter;
316 MTRR_MEMORY_RANGE Ranges[MTRR_NUMBER_OF_VARIABLE_MTRR * 2 + 1];
317 UINTN RangeCount;
318 UINT64 MaxAddress;
319 UINT32 Index;
320 UINT64 BaseAddress;
321 UINT64 Length;
322 RETURN_STATUS Status;
323 UINTN ScratchSize;
324
325 SystemParameter = (MTRR_LIB_SYSTEM_PARAMETER *)Context;
326
327 RangeCount = Random32 (1, ARRAY_SIZE (Ranges));
328 MaxAddress = 1ull << SystemParameter->PhysicalAddressBits;
329
330 for (Index = 0; Index < RangeCount; Index++) {
331 do {
332 BaseAddress = Random64 (0, MaxAddress);
333 Length = Random64 (1, MaxAddress - BaseAddress);
334 } while (((BaseAddress & 0xFFF) == 0) || ((Length & 0xFFF) == 0));
335
336 Ranges[Index].BaseAddress = BaseAddress;
337 Ranges[Index].Length = Length;
338 Ranges[Index].Type = GenerateRandomCacheType ();
339
340 Status = MtrrSetMemoryAttribute (
341 Ranges[Index].BaseAddress,
342 Ranges[Index].Length,
343 Ranges[Index].Type
344 );
345 UT_ASSERT_TRUE (RETURN_ERROR (Status));
346 }
347
348 ScratchSize = 0;
349 Status = MtrrSetMemoryAttributesInMtrrSettings (NULL, NULL, &ScratchSize, Ranges, RangeCount);
350 UT_ASSERT_TRUE (RETURN_ERROR (Status));
351
352 return UNIT_TEST_PASSED;
353 }
354
355 /**
356 Unit test of MtrrLib service IsMtrrSupported()
357
358 @param[in] Context Ignored
359
360 @retval UNIT_TEST_PASSED The Unit test has completed and the test
361 case was successful.
362 @retval UNIT_TEST_ERROR_TEST_FAILED A test case assertion has failed.
363
364 **/
365 UNIT_TEST_STATUS
366 EFIAPI
367 UnitTestIsMtrrSupported (
368 IN UNIT_TEST_CONTEXT Context
369 )
370 {
371 MTRR_LIB_SYSTEM_PARAMETER SystemParameter;
372 MTRR_LIB_TEST_CONTEXT *LocalContext;
373
374 LocalContext = (MTRR_LIB_TEST_CONTEXT *)Context;
375
376 CopyMem (&SystemParameter, LocalContext->SystemParameter, sizeof (SystemParameter));
377 //
378 // MTRR capability off in CPUID leaf.
379 //
380 SystemParameter.MtrrSupported = FALSE;
381 InitializeMtrrRegs (&SystemParameter);
382 UT_ASSERT_FALSE (IsMtrrSupported ());
383
384 //
385 // MTRR capability on in CPUID leaf, but no variable or fixed MTRRs.
386 //
387 SystemParameter.MtrrSupported = TRUE;
388 SystemParameter.VariableMtrrCount = 0;
389 SystemParameter.FixedMtrrSupported = FALSE;
390 InitializeMtrrRegs (&SystemParameter);
391 UT_ASSERT_FALSE (IsMtrrSupported ());
392
393 //
394 // MTRR capability on in CPUID leaf, but no variable MTRRs.
395 //
396 SystemParameter.MtrrSupported = TRUE;
397 SystemParameter.VariableMtrrCount = 0;
398 SystemParameter.FixedMtrrSupported = TRUE;
399 InitializeMtrrRegs (&SystemParameter);
400 UT_ASSERT_FALSE (IsMtrrSupported ());
401
402 //
403 // MTRR capability on in CPUID leaf, but no fixed MTRRs.
404 //
405 SystemParameter.MtrrSupported = TRUE;
406 SystemParameter.VariableMtrrCount = 7;
407 SystemParameter.FixedMtrrSupported = FALSE;
408 InitializeMtrrRegs (&SystemParameter);
409 UT_ASSERT_FALSE (IsMtrrSupported ());
410
411 //
412 // MTRR capability on in CPUID leaf with both variable and fixed MTRRs.
413 //
414 SystemParameter.MtrrSupported = TRUE;
415 SystemParameter.VariableMtrrCount = 7;
416 SystemParameter.FixedMtrrSupported = TRUE;
417 InitializeMtrrRegs (&SystemParameter);
418 UT_ASSERT_TRUE (IsMtrrSupported ());
419
420 return UNIT_TEST_PASSED;
421 }
422
423 /**
424 Unit test of MtrrLib service GetVariableMtrrCount()
425
426 @param[in] Context Ignored
427
428 @retval UNIT_TEST_PASSED The Unit test has completed and the test
429 case was successful.
430 @retval UNIT_TEST_ERROR_TEST_FAILED A test case assertion has failed.
431
432 **/
433 UNIT_TEST_STATUS
434 EFIAPI
435 UnitTestGetVariableMtrrCount (
436 IN UNIT_TEST_CONTEXT Context
437 )
438 {
439 UINT32 Result;
440 MTRR_LIB_SYSTEM_PARAMETER SystemParameter;
441 MTRR_LIB_TEST_CONTEXT *LocalContext;
442
443 LocalContext = (MTRR_LIB_TEST_CONTEXT *)Context;
444
445 CopyMem (&SystemParameter, LocalContext->SystemParameter, sizeof (SystemParameter));
446 //
447 // If MTRR capability off in CPUID leaf, then the count is always 0.
448 //
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);
454 }
455
456 //
457 // Try all supported variable MTRR counts.
458 // If variable MTRR count is > MTRR_NUMBER_OF_VARIABLE_MTRR, then an ASSERT()
459 // is generated.
460 //
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);
466 }
467
468 //
469 // Expect ASSERT() if variable MTRR count is > MTRR_NUMBER_OF_VARIABLE_MTRR
470 //
471 SystemParameter.VariableMtrrCount = MTRR_NUMBER_OF_VARIABLE_MTRR + 1;
472 InitializeMtrrRegs (&SystemParameter);
473 UT_EXPECT_ASSERT_FAILURE (GetVariableMtrrCount (), NULL);
474
475 SystemParameter.MtrrSupported = TRUE;
476 SystemParameter.VariableMtrrCount = MAX_UINT8;
477 InitializeMtrrRegs (&SystemParameter);
478 UT_EXPECT_ASSERT_FAILURE (GetVariableMtrrCount (), NULL);
479
480 return UNIT_TEST_PASSED;
481 }
482
483 /**
484 Unit test of MtrrLib service GetFirmwareVariableMtrrCount()
485
486 @param[in] Context Ignored
487
488 @retval UNIT_TEST_PASSED The Unit test has completed and the test
489 case was successful.
490 @retval UNIT_TEST_ERROR_TEST_FAILED A test case assertion has failed.
491
492 **/
493 UNIT_TEST_STATUS
494 EFIAPI
495 UnitTestGetFirmwareVariableMtrrCount (
496 IN UNIT_TEST_CONTEXT Context
497 )
498 {
499 UINT32 Result;
500 UINT32 ReservedMtrrs;
501 MTRR_LIB_SYSTEM_PARAMETER SystemParameter;
502 MTRR_LIB_GET_FIRMWARE_VARIABLE_MTRR_COUNT_CONTEXT *LocalContext;
503
504 LocalContext = (MTRR_LIB_GET_FIRMWARE_VARIABLE_MTRR_COUNT_CONTEXT *)Context;
505
506 CopyMem (&SystemParameter, LocalContext->SystemParameter, sizeof (SystemParameter));
507
508 InitializeMtrrRegs (&SystemParameter);
509 //
510 // Positive test cases for VCNT = 10 and Reserved PCD in range 0..10
511 //
512 for (ReservedMtrrs = 0; ReservedMtrrs <= SystemParameter.VariableMtrrCount; ReservedMtrrs++) {
513 PatchPcdSet32 (PcdCpuNumberOfReservedVariableMtrrs, ReservedMtrrs);
514 Result = GetFirmwareVariableMtrrCount ();
515 UT_ASSERT_EQUAL (Result, SystemParameter.VariableMtrrCount - ReservedMtrrs);
516 }
517
518 //
519 // Negative test cases when Reserved PCD is larger than VCNT
520 //
521 for (ReservedMtrrs = SystemParameter.VariableMtrrCount + 1; ReservedMtrrs <= 255; ReservedMtrrs++) {
522 PatchPcdSet32 (PcdCpuNumberOfReservedVariableMtrrs, ReservedMtrrs);
523 Result = GetFirmwareVariableMtrrCount ();
524 UT_ASSERT_EQUAL (Result, 0);
525 }
526
527 //
528 // Negative test cases when Reserved PCD is larger than VCNT
529 //
530 PatchPcdSet32 (PcdCpuNumberOfReservedVariableMtrrs, MAX_UINT32);
531 Result = GetFirmwareVariableMtrrCount ();
532 UT_ASSERT_EQUAL (Result, 0);
533
534 //
535 // Negative test case when MTRRs are not supported
536 //
537 SystemParameter.MtrrSupported = FALSE;
538 InitializeMtrrRegs (&SystemParameter);
539 PatchPcdSet32 (PcdCpuNumberOfReservedVariableMtrrs, 2);
540 Result = GetFirmwareVariableMtrrCount ();
541 UT_ASSERT_EQUAL (Result, 0);
542
543 //
544 // Negative test case when Fixed MTRRs are not supported
545 //
546 SystemParameter.MtrrSupported = TRUE;
547 SystemParameter.FixedMtrrSupported = FALSE;
548 InitializeMtrrRegs (&SystemParameter);
549 PatchPcdSet32 (PcdCpuNumberOfReservedVariableMtrrs, 2);
550 Result = GetFirmwareVariableMtrrCount ();
551 UT_ASSERT_EQUAL (Result, 0);
552
553 //
554 // Expect ASSERT() if variable MTRR count is > MTRR_NUMBER_OF_VARIABLE_MTRR
555 //
556 SystemParameter.FixedMtrrSupported = TRUE;
557 SystemParameter.VariableMtrrCount = MTRR_NUMBER_OF_VARIABLE_MTRR + 1;
558 InitializeMtrrRegs (&SystemParameter);
559 UT_EXPECT_ASSERT_FAILURE (GetFirmwareVariableMtrrCount (), NULL);
560
561 return UNIT_TEST_PASSED;
562 }
563
564 /**
565 Unit test of MtrrLib service MtrrGetMemoryAttribute()
566
567 @param[in] Context Ignored
568
569 @retval UNIT_TEST_PASSED The Unit test has completed and the test
570 case was successful.
571 @retval UNIT_TEST_ERROR_TEST_FAILED A test case assertion has failed.
572
573 **/
574 UNIT_TEST_STATUS
575 EFIAPI
576 UnitTestMtrrGetMemoryAttribute (
577 IN UNIT_TEST_CONTEXT Context
578 )
579 {
580 return UNIT_TEST_PASSED;
581 }
582
583 /**
584 Unit test of MtrrLib service MtrrGetFixedMtrr()
585
586 @param[in] Context Ignored
587
588 @retval UNIT_TEST_PASSED The Unit test has completed and the test
589 case was successful.
590 @retval UNIT_TEST_ERROR_TEST_FAILED A test case assertion has failed.
591
592 **/
593 UNIT_TEST_STATUS
594 EFIAPI
595 UnitTestMtrrGetFixedMtrr (
596 IN UNIT_TEST_CONTEXT Context
597 )
598 {
599 MTRR_FIXED_SETTINGS *Result;
600 MTRR_FIXED_SETTINGS ExpectedFixedSettings;
601 MTRR_FIXED_SETTINGS FixedSettings;
602 UINTN Index;
603 UINTN MsrIndex;
604 UINTN ByteIndex;
605 UINT64 MsrValue;
606 MTRR_LIB_SYSTEM_PARAMETER SystemParameter;
607 MTRR_LIB_TEST_CONTEXT *LocalContext;
608
609 LocalContext = (MTRR_LIB_TEST_CONTEXT *)Context;
610
611 CopyMem (&SystemParameter, LocalContext->SystemParameter, sizeof (SystemParameter));
612 InitializeMtrrRegs (&SystemParameter);
613 //
614 // Set random cache type to different ranges under 1MB and make sure
615 // the fixed MTRR settings are expected.
616 // Try 100 times.
617 //
618 for (Index = 0; Index < 100; Index++) {
619 for (MsrIndex = 0; MsrIndex < ARRAY_SIZE (mFixedMtrrsIndex); MsrIndex++) {
620 MsrValue = 0;
621 for (ByteIndex = 0; ByteIndex < sizeof (UINT64); ByteIndex++) {
622 MsrValue = MsrValue | LShiftU64 (GenerateRandomCacheType (), ByteIndex * 8);
623 }
624
625 ExpectedFixedSettings.Mtrr[MsrIndex] = MsrValue;
626 AsmWriteMsr64 (mFixedMtrrsIndex[MsrIndex], MsrValue);
627 }
628
629 Result = MtrrGetFixedMtrr (&FixedSettings);
630 UT_ASSERT_EQUAL ((UINTN)Result, (UINTN)&FixedSettings);
631 UT_ASSERT_MEM_EQUAL (&FixedSettings, &ExpectedFixedSettings, sizeof (FixedSettings));
632 }
633
634 //
635 // Negative test case when MTRRs are not supported
636 //
637 SystemParameter.MtrrSupported = FALSE;
638 InitializeMtrrRegs (&SystemParameter);
639
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));
645
646 return UNIT_TEST_PASSED;
647 }
648
649 /**
650 Unit test of MtrrLib service MtrrGetAllMtrrs()
651
652 @param[in] Context Ignored
653
654 @retval UNIT_TEST_PASSED The Unit test has completed and the test
655 case was successful.
656 @retval UNIT_TEST_ERROR_TEST_FAILED A test case assertion has failed.
657
658 **/
659 UNIT_TEST_STATUS
660 EFIAPI
661 UnitTestMtrrGetAllMtrrs (
662 IN UNIT_TEST_CONTEXT Context
663 )
664 {
665 MTRR_SETTINGS *Result;
666 MTRR_SETTINGS Mtrrs;
667 MTRR_SETTINGS ExpectedMtrrs;
668 MTRR_VARIABLE_SETTING VariableMtrr[MTRR_NUMBER_OF_VARIABLE_MTRR];
669 UINT32 Index;
670 MTRR_LIB_SYSTEM_PARAMETER SystemParameter;
671 MTRR_LIB_TEST_CONTEXT *LocalContext;
672
673 LocalContext = (MTRR_LIB_TEST_CONTEXT *)Context;
674
675 CopyMem (&SystemParameter, LocalContext->SystemParameter, sizeof (SystemParameter));
676 InitializeMtrrRegs (&SystemParameter);
677
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);
682 }
683
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);
687
688 //
689 // Negative test case when MTRRs are not supported
690 //
691 ZeroMem (&ExpectedMtrrs, sizeof (ExpectedMtrrs));
692 ZeroMem (&Mtrrs, sizeof (Mtrrs));
693
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));
699
700 //
701 // Expect ASSERT() if variable MTRR count is > MTRR_NUMBER_OF_VARIABLE_MTRR
702 //
703 SystemParameter.MtrrSupported = TRUE;
704 SystemParameter.VariableMtrrCount = MTRR_NUMBER_OF_VARIABLE_MTRR + 1;
705 InitializeMtrrRegs (&SystemParameter);
706 UT_EXPECT_ASSERT_FAILURE (MtrrGetAllMtrrs (&Mtrrs), NULL);
707
708 return UNIT_TEST_PASSED;
709 }
710
711 /**
712 Unit test of MtrrLib service MtrrSetAllMtrrs()
713
714 @param[in] Context Ignored
715
716 @retval UNIT_TEST_PASSED The Unit test has completed and the test
717 case was successful.
718 @retval UNIT_TEST_ERROR_TEST_FAILED A test case assertion has failed.
719
720 **/
721 UNIT_TEST_STATUS
722 EFIAPI
723 UnitTestMtrrSetAllMtrrs (
724 IN UNIT_TEST_CONTEXT Context
725 )
726 {
727 MTRR_SETTINGS *Result;
728 MTRR_SETTINGS Mtrrs;
729 UINT32 Index;
730 MSR_IA32_MTRR_DEF_TYPE_REGISTER Default;
731 MTRR_LIB_SYSTEM_PARAMETER SystemParameter;
732 MTRR_LIB_TEST_CONTEXT *LocalContext;
733
734 LocalContext = (MTRR_LIB_TEST_CONTEXT *)Context;
735
736 CopyMem (&SystemParameter, LocalContext->SystemParameter, sizeof (SystemParameter));
737 InitializeMtrrRegs (&SystemParameter);
738
739 Default.Uint64 = 0;
740 Default.Bits.E = 1;
741 Default.Bits.FE = 1;
742 Default.Bits.Type = GenerateRandomCacheType ();
743
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);
748 }
749
750 Result = MtrrSetAllMtrrs (&Mtrrs);
751 UT_ASSERT_EQUAL ((UINTN)Result, (UINTN)&Mtrrs);
752
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);
757 }
758
759 return UNIT_TEST_PASSED;
760 }
761
762 /**
763 Unit test of MtrrLib service MtrrGetMemoryAttributeInVariableMtrr()
764
765 @param[in] Context Ignored
766
767 @retval UNIT_TEST_PASSED The Unit test has completed and the test
768 case was successful.
769 @retval UNIT_TEST_ERROR_TEST_FAILED A test case assertion has failed.
770
771 **/
772 UNIT_TEST_STATUS
773 EFIAPI
774 UnitTestMtrrGetMemoryAttributeInVariableMtrr (
775 IN UNIT_TEST_CONTEXT Context
776 )
777 {
778 MTRR_LIB_TEST_CONTEXT *LocalContext;
779 MTRR_LIB_SYSTEM_PARAMETER SystemParameter;
780 UINT32 Result;
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;
785 UINT32 Index;
786 MSR_IA32_MTRR_PHYSBASE_REGISTER Base;
787 MSR_IA32_MTRR_PHYSMASK_REGISTER Mask;
788
789 LocalContext = (MTRR_LIB_TEST_CONTEXT *)Context;
790
791 CopyMem (&SystemParameter, LocalContext->SystemParameter, sizeof (SystemParameter));
792
793 InitializeMtrrRegs (&SystemParameter);
794
795 ValidMtrrBitsMask = (1ull << SystemParameter.PhysicalAddressBits) - 1;
796 ValidMtrrAddressMask = ValidMtrrBitsMask & 0xfffffffffffff000ULL;
797
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);
802 }
803
804 Result = MtrrGetMemoryAttributeInVariableMtrr (ValidMtrrBitsMask, ValidMtrrAddressMask, VariableMtrr);
805 UT_ASSERT_EQUAL (Result, SystemParameter.VariableMtrrCount);
806
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);
811
812 Mask.Uint64 = ~(VariableMtrr[Index].Length - 1) & ValidMtrrBitsMask;
813 Mask.Bits.V = 1;
814 UT_ASSERT_EQUAL (Mask.Uint64, VariableSetting[Index].Mask);
815 }
816
817 //
818 // Negative test case when MTRRs are not supported
819 //
820 SystemParameter.MtrrSupported = FALSE;
821 InitializeMtrrRegs (&SystemParameter);
822 Result = MtrrGetMemoryAttributeInVariableMtrr (ValidMtrrBitsMask, ValidMtrrAddressMask, VariableMtrr);
823 UT_ASSERT_EQUAL (Result, 0);
824
825 //
826 // Expect ASSERT() if variable MTRR count is > MTRR_NUMBER_OF_VARIABLE_MTRR
827 //
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);
832
833 return UNIT_TEST_PASSED;
834 }
835
836 /**
837 Unit test of MtrrLib service MtrrDebugPrintAllMtrrs()
838
839 @param[in] Context Ignored
840
841 @retval UNIT_TEST_PASSED The Unit test has completed and the test
842 case was successful.
843 @retval UNIT_TEST_ERROR_TEST_FAILED A test case assertion has failed.
844
845 **/
846 UNIT_TEST_STATUS
847 EFIAPI
848 UnitTestMtrrDebugPrintAllMtrrs (
849 IN UNIT_TEST_CONTEXT Context
850 )
851 {
852 return UNIT_TEST_PASSED;
853 }
854
855 /**
856 Unit test of MtrrLib service MtrrGetDefaultMemoryType().
857
858 @param[in] Context Ignored
859
860 @retval UNIT_TEST_PASSED The Unit test has completed and the test
861 case was successful.
862 @retval UNIT_TEST_ERROR_TEST_FAILED A test case assertion has failed.
863
864 **/
865 UNIT_TEST_STATUS
866 EFIAPI
867 UnitTestMtrrGetDefaultMemoryType (
868 IN UNIT_TEST_CONTEXT Context
869 )
870 {
871 MTRR_LIB_TEST_CONTEXT *LocalContext;
872 UINTN Index;
873 MTRR_MEMORY_CACHE_TYPE Result;
874 MTRR_LIB_SYSTEM_PARAMETER SystemParameter;
875 MTRR_MEMORY_CACHE_TYPE CacheType[5];
876
877 CacheType[0] = CacheUncacheable;
878 CacheType[1] = CacheWriteCombining;
879 CacheType[2] = CacheWriteThrough;
880 CacheType[3] = CacheWriteProtected;
881 CacheType[4] = CacheWriteBack;
882
883 LocalContext = (MTRR_LIB_TEST_CONTEXT *)Context;
884
885 CopyMem (&SystemParameter, LocalContext->SystemParameter, sizeof (SystemParameter));
886 //
887 // If MTRRs are supported, then always return the cache type in the MSR
888 // MSR_IA32_MTRR_DEF_TYPE
889 //
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);
895 }
896
897 //
898 // If MTRRs are not supported, then always return CacheUncacheable
899 //
900 SystemParameter.MtrrSupported = FALSE;
901 InitializeMtrrRegs (&SystemParameter);
902 Result = MtrrGetDefaultMemoryType ();
903 UT_ASSERT_EQUAL (Result, CacheUncacheable);
904
905 SystemParameter.MtrrSupported = TRUE;
906 SystemParameter.FixedMtrrSupported = FALSE;
907 InitializeMtrrRegs (&SystemParameter);
908 Result = MtrrGetDefaultMemoryType ();
909 UT_ASSERT_EQUAL (Result, CacheUncacheable);
910
911 SystemParameter.MtrrSupported = TRUE;
912 SystemParameter.FixedMtrrSupported = TRUE;
913 SystemParameter.VariableMtrrCount = 0;
914 InitializeMtrrRegs (&SystemParameter);
915 Result = MtrrGetDefaultMemoryType ();
916 UT_ASSERT_EQUAL (Result, CacheUncacheable);
917
918 return UNIT_TEST_PASSED;
919 }
920
921 /**
922 Unit test of MtrrLib service MtrrSetMemoryAttributeInMtrrSettings().
923
924 @param[in] Context Ignored
925
926 @retval UNIT_TEST_PASSED The Unit test has completed and the test
927 case was successful.
928 @retval UNIT_TEST_ERROR_TEST_FAILED A test case assertion has failed.
929
930 **/
931 UNIT_TEST_STATUS
932 EFIAPI
933 UnitTestMtrrSetMemoryAttributeInMtrrSettings (
934 IN UNIT_TEST_CONTEXT Context
935 )
936 {
937 CONST MTRR_LIB_SYSTEM_PARAMETER *SystemParameter;
938 RETURN_STATUS Status;
939 UINT32 UcCount;
940 UINT32 WtCount;
941 UINT32 WbCount;
942 UINT32 WpCount;
943 UINT32 WcCount;
944
945 UINTN MtrrIndex;
946 UINTN Index;
947 MTRR_SETTINGS LocalMtrrs;
948
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;
953
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;
957
958 MTRR_SETTINGS *Mtrrs[2];
959
960 SystemParameter = (MTRR_LIB_SYSTEM_PARAMETER *)Context;
961 GenerateRandomMemoryTypeCombination (
962 SystemParameter->VariableMtrrCount - PatchPcdGet32 (PcdCpuNumberOfReservedVariableMtrrs),
963 &UcCount,
964 &WtCount,
965 &WbCount,
966 &WpCount,
967 &WcCount
968 );
969 GenerateValidAndConfigurableMtrrPairs (
970 SystemParameter->PhysicalAddressBits,
971 RawMtrrRange,
972 UcCount,
973 WtCount,
974 WbCount,
975 WpCount,
976 WcCount
977 );
978
979 ExpectedVariableMtrrUsage = UcCount + WtCount + WbCount + WpCount + WcCount;
980 ExpectedMemoryRangesCount = ARRAY_SIZE (ExpectedMemoryRanges);
981 GetEffectiveMemoryRanges (
982 SystemParameter->DefaultCacheType,
983 SystemParameter->PhysicalAddressBits,
984 RawMtrrRange,
985 ExpectedVariableMtrrUsage,
986 ExpectedMemoryRanges,
987 &ExpectedMemoryRangesCount
988 );
989
990 UT_LOG_INFO ("--- Expected Memory Ranges [%d] ---\n", ExpectedMemoryRangesCount);
991 DumpMemoryRanges (ExpectedMemoryRanges, ExpectedMemoryRangesCount);
992 //
993 // Default cache type is always an INPUT
994 //
995 ZeroMem (&LocalMtrrs, sizeof (LocalMtrrs));
996 LocalMtrrs.MtrrDefType = MtrrGetDefaultMemoryType ();
997 Mtrrs[0] = &LocalMtrrs;
998 Mtrrs[1] = NULL;
999
1000 for (MtrrIndex = 0; MtrrIndex < ARRAY_SIZE (Mtrrs); MtrrIndex++) {
1001 for (Index = 0; Index < ExpectedMemoryRangesCount; Index++) {
1002 Status = MtrrSetMemoryAttributeInMtrrSettings (
1003 Mtrrs[MtrrIndex],
1004 ExpectedMemoryRanges[Index].BaseAddress,
1005 ExpectedMemoryRanges[Index].Length,
1006 ExpectedMemoryRanges[Index].Type
1007 );
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;
1011 }
1012 }
1013
1014 if (Mtrrs[MtrrIndex] == NULL) {
1015 ZeroMem (&LocalMtrrs, sizeof (LocalMtrrs));
1016 MtrrGetAllMtrrs (&LocalMtrrs);
1017 }
1018
1019 ActualMemoryRangesCount = ARRAY_SIZE (ActualMemoryRanges);
1020 CollectTestResult (
1021 SystemParameter->DefaultCacheType,
1022 SystemParameter->PhysicalAddressBits,
1023 SystemParameter->VariableMtrrCount,
1024 &LocalMtrrs,
1025 ActualMemoryRanges,
1026 &ActualMemoryRangesCount,
1027 &ActualVariableMtrrUsage
1028 );
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);
1033
1034 ZeroMem (&LocalMtrrs, sizeof (LocalMtrrs));
1035 }
1036
1037 return UNIT_TEST_PASSED;
1038 }
1039
1040 /**
1041 Prep routine for UnitTestGetFirmwareVariableMtrrCount().
1042
1043 @param Context Point to a UINT32 data to save the PcdCpuNumberOfReservedVariableMtrrs.
1044 **/
1045 UNIT_TEST_STATUS
1046 EFIAPI
1047 SavePcdValue (
1048 UNIT_TEST_CONTEXT Context
1049 )
1050 {
1051 MTRR_LIB_GET_FIRMWARE_VARIABLE_MTRR_COUNT_CONTEXT *LocalContext;
1052
1053 LocalContext = (MTRR_LIB_GET_FIRMWARE_VARIABLE_MTRR_COUNT_CONTEXT *)Context;
1054 LocalContext->NumberOfReservedVariableMtrrs = PatchPcdGet32 (PcdCpuNumberOfReservedVariableMtrrs);
1055 return UNIT_TEST_PASSED;
1056 }
1057
1058 /**
1059 Clean up routine for UnitTestGetFirmwareVariableMtrrCount().
1060
1061 @param Context Point to a UINT32 data to save the PcdCpuNumberOfReservedVariableMtrrs.
1062 **/
1063 VOID
1064 EFIAPI
1065 RestorePcdValue (
1066 UNIT_TEST_CONTEXT Context
1067 )
1068 {
1069 MTRR_LIB_GET_FIRMWARE_VARIABLE_MTRR_COUNT_CONTEXT *LocalContext;
1070
1071 LocalContext = (MTRR_LIB_GET_FIRMWARE_VARIABLE_MTRR_COUNT_CONTEXT *)Context;
1072 PatchPcdSet32 (PcdCpuNumberOfReservedVariableMtrrs, LocalContext->NumberOfReservedVariableMtrrs);
1073 }
1074
1075 /**
1076 Initialize the unit test framework, suite, and unit tests for the
1077 ResetSystemLib and run the ResetSystemLib unit test.
1078
1079 @param Iteration Iteration of testing MtrrSetMemoryAttributeInMtrrSettings
1080 and MtrrSetMemoryAttributesInMtrrSettings using random inputs.
1081
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.
1085 **/
1086 STATIC
1087 EFI_STATUS
1088 EFIAPI
1089 UnitTestingEntry (
1090 UINTN Iteration
1091 )
1092 {
1093 EFI_STATUS Status;
1094 UNIT_TEST_FRAMEWORK_HANDLE Framework;
1095 UNIT_TEST_SUITE_HANDLE MtrrApiTests;
1096 UINTN Index;
1097 UINTN SystemIndex;
1098 MTRR_LIB_TEST_CONTEXT Context;
1099 MTRR_LIB_GET_FIRMWARE_VARIABLE_MTRR_COUNT_CONTEXT GetFirmwareVariableMtrrCountContext;
1100
1101 Context.SystemParameter = &mDefaultSystemParameter;
1102 GetFirmwareVariableMtrrCountContext.SystemParameter = &mDefaultSystemParameter;
1103 Framework = NULL;
1104
1105 //
1106 // Setup the test framework for running the tests.
1107 //
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));
1111 goto EXIT;
1112 }
1113
1114 //
1115 // --------------Suite-----------Description--------------Name----------Function--------Pre---Post-------------------Context-----------
1116 //
1117
1118 //
1119 // Populate the MtrrLib API Unit Test Suite.
1120 //
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;
1125 goto EXIT;
1126 }
1127
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);
1138
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]);
1144 }
1145 }
1146
1147 //
1148 // Execute the tests.
1149 //
1150 Status = RunAllTestSuites (Framework);
1151
1152 EXIT:
1153 if (Framework != NULL) {
1154 FreeUnitTestFramework (Framework);
1155 }
1156
1157 return Status;
1158 }
1159
1160 /**
1161 Standard POSIX C entry point for host based unit test execution.
1162
1163 @param Argc Number of arguments.
1164 @param Argv Array of arguments.
1165
1166 @return Test application exit code.
1167 **/
1168 INT32
1169 main (
1170 INT32 Argc,
1171 CHAR8 *Argv[]
1172 )
1173 {
1174 UINTN Count;
1175
1176 DEBUG ((DEBUG_INFO, "%a v%a\n", UNIT_TEST_APP_NAME, UNIT_TEST_APP_VERSION));
1177 srand ((unsigned int)time (NULL));
1178
1179 //
1180 // MtrrLibUnitTest generate-random-numbers <path to MtrrLib/UnitTest/RandomNumber.c> <random-number count>
1181 //
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);
1186 return 0;
1187 }
1188
1189 //
1190 // MtrrLibUnitTest [<iterations>]
1191 // <iterations> [fixed|random]
1192 // Default <iterations> is 10.
1193 // Default uses fixed inputs.
1194 //
1195 Count = 10;
1196 mRandomInput = FALSE;
1197 if ((Argc == 2) || (Argc == 3)) {
1198 Count = atoi (Argv[1]);
1199 if (Argc == 3) {
1200 if (AsciiStriCmp ("fixed", Argv[2]) == 0) {
1201 mRandomInput = FALSE;
1202 } else if (AsciiStriCmp ("random", Argv[2]) == 0) {
1203 mRandomInput = TRUE;
1204 }
1205 }
1206 }
1207
1208 DEBUG ((DEBUG_INFO, "Iterations = %d\n", Count));
1209 DEBUG ((DEBUG_INFO, "Input = %a\n", mRandomInput ? "random" : "fixed"));
1210
1211 return UnitTestingEntry (Count);
1212 }