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