summaryrefslogtreecommitdiff
path: root/MdeModulePkg/Core/Dxe/Mem/GoogleTest/MemoryBinGoogleTest.cpp
blob: ba5a50c82ffd306eff07ec0f454d856fb15f7976 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
/** @file
  Unit tests for BaseMemoryBinLib library.

  Copyright (c) Microsoft Corporation.
  SPDX-License-Identifier: BSD-2-Clause-Patent

**/

#include <Library/GoogleTestLib.h>
#include <GoogleTest/Library/MockHobLib.h>

extern "C" {
  #include <Uefi/UefiBaseType.h>
  #include <Uefi/UefiMultiPhase.h>
  #include <Pi/PiMultiPhase.h>
  #include <Library/BaseLib.h>
  #include <Library/DebugLib.h>
  #include <Library/BaseMemoryLib.h>
  #include <Library/MemoryAllocationLib.h>
  #include <Guid/MemoryTypeInformation.h>
  #include <Pi/PiHob.h>
  #include <Pi/PiDxeCis.h>
  #include <MemoryBin.h>

  BOOLEAN  mMemoryTypeInformationInitialized = FALSE;

  EFI_MEMORY_TYPE_STATISTICS  mMemoryTypeStatistics[EfiMaxMemoryType + 1] = {
    { 0, MAX_ALLOC_ADDRESS, 0, 0, EfiMaxMemoryType, TRUE,  FALSE }, // EfiReservedMemoryType
    { 0, MAX_ALLOC_ADDRESS, 0, 0, EfiMaxMemoryType, FALSE, FALSE }, // EfiLoaderCode
    { 0, MAX_ALLOC_ADDRESS, 0, 0, EfiMaxMemoryType, FALSE, FALSE }, // EfiLoaderData
    { 0, MAX_ALLOC_ADDRESS, 0, 0, EfiMaxMemoryType, FALSE, FALSE }, // EfiBootServicesCode
    { 0, MAX_ALLOC_ADDRESS, 0, 0, EfiMaxMemoryType, FALSE, FALSE }, // EfiBootServicesData
    { 0, MAX_ALLOC_ADDRESS, 0, 0, EfiMaxMemoryType, TRUE,  TRUE  }, // EfiRuntimeServicesCode
    { 0, MAX_ALLOC_ADDRESS, 0, 0, EfiMaxMemoryType, TRUE,  TRUE  }, // EfiRuntimeServicesData
    { 0, MAX_ALLOC_ADDRESS, 0, 0, EfiMaxMemoryType, FALSE, FALSE }, // EfiConventionalMemory
    { 0, MAX_ALLOC_ADDRESS, 0, 0, EfiMaxMemoryType, FALSE, FALSE }, // EfiUnusableMemory
    { 0, MAX_ALLOC_ADDRESS, 0, 0, EfiMaxMemoryType, TRUE,  FALSE }, // EfiACPIReclaimMemory
    { 0, MAX_ALLOC_ADDRESS, 0, 0, EfiMaxMemoryType, TRUE,  FALSE }, // EfiACPIMemoryNVS
    { 0, MAX_ALLOC_ADDRESS, 0, 0, EfiMaxMemoryType, FALSE, FALSE }, // EfiMemoryMappedIO
    { 0, MAX_ALLOC_ADDRESS, 0, 0, EfiMaxMemoryType, FALSE, FALSE }, // EfiMemoryMappedIOPortSpace
    { 0, MAX_ALLOC_ADDRESS, 0, 0, EfiMaxMemoryType, TRUE,  TRUE  }, // EfiPalCode
    { 0, MAX_ALLOC_ADDRESS, 0, 0, EfiMaxMemoryType, FALSE, FALSE }, // EfiPersistentMemory
    { 0, MAX_ALLOC_ADDRESS, 0, 0, EfiMaxMemoryType, TRUE,  FALSE }, // EfiUnacceptedMemoryType
    { 0, MAX_ALLOC_ADDRESS, 0, 0, EfiMaxMemoryType, FALSE, FALSE } // EfiMaxMemoryType
  };

  EFI_PHYSICAL_ADDRESS  mDefaultMaximumAddress = MAX_ALLOC_ADDRESS;
  EFI_PHYSICAL_ADDRESS  mDefaultBaseAddress    = MAX_ALLOC_ADDRESS;

  EFI_MEMORY_TYPE_INFORMATION  gMemoryTypeInformation[EfiMaxMemoryType + 1] = {
    { EfiReservedMemoryType,      0 },
    { EfiLoaderCode,              0 },
    { EfiLoaderData,              0 },
    { EfiBootServicesCode,        0 },
    { EfiBootServicesData,        0 },
    { EfiRuntimeServicesCode,     0 },
    { EfiRuntimeServicesData,     0 },
    { EfiConventionalMemory,      0 },
    { EfiUnusableMemory,          0 },
    { EfiACPIReclaimMemory,       0 },
    { EfiACPIMemoryNVS,           0 },
    { EfiMemoryMappedIO,          0 },
    { EfiMemoryMappedIOPortSpace, 0 },
    { EfiPalCode,                 0 },
    { EfiPersistentMemory,        0 },
    { EfiUnacceptedMemoryType,    0 },
    { EfiMaxMemoryType,           0 }
  };
}

using namespace testing;

class BaseMemoryBinLibTest : public ::testing::Test {
protected:
  StrictMock<MockHobLib> HobLib;
  void
  SetUp (
    ) override
  {
    //
    // Reset global state before each test
    //
    mMemoryTypeInformationInitialized = FALSE;
    mDefaultMaximumAddress            = MAX_ALLOC_ADDRESS;
    mDefaultBaseAddress               = MAX_ALLOC_ADDRESS;

    for (UINTN i = 0; i < EfiMaxMemoryType + 1; i++) {
      gMemoryTypeInformation[i].NumberOfPages = 0;
    }

    for (UINTN i = 0; i < EfiMaxMemoryType + 1; i++) {
      mMemoryTypeStatistics[i].BaseAddress          = 0;
      mMemoryTypeStatistics[i].MaximumAddress       = MAX_ALLOC_ADDRESS;
      mMemoryTypeStatistics[i].CurrentNumberOfPages = 0;
      mMemoryTypeStatistics[i].NumberOfPages        = 0;
      mMemoryTypeStatistics[i].InformationIndex     = EfiMaxMemoryType;
    }

    mMemoryTypeStatistics[EfiReservedMemoryType].Special   = TRUE;
    mMemoryTypeStatistics[EfiRuntimeServicesCode].Special  = TRUE;
    mMemoryTypeStatistics[EfiRuntimeServicesCode].Runtime  = TRUE;
    mMemoryTypeStatistics[EfiRuntimeServicesData].Special  = TRUE;
    mMemoryTypeStatistics[EfiRuntimeServicesData].Runtime  = TRUE;
    mMemoryTypeStatistics[EfiACPIReclaimMemory].Special    = TRUE;
    mMemoryTypeStatistics[EfiACPIMemoryNVS].Special        = TRUE;
    mMemoryTypeStatistics[EfiPalCode].Special              = TRUE;
    mMemoryTypeStatistics[EfiPalCode].Runtime              = TRUE;
    mMemoryTypeStatistics[EfiUnacceptedMemoryType].Special = TRUE;
  }
};

//
// Test: CalculateTotalMemoryBinSizeNeeded with no pages allocated
//
TEST_F (BaseMemoryBinLibTest, CalculateTotalMemoryBinSizeNeededReturnsZeroWhenNoPagesAllocated) {
  UINT64  TotalSize;

  TotalSize = CalculateTotalMemoryBinSizeNeeded (NULL, gMemoryTypeInformation);

  ASSERT_EQ (TotalSize, (UINT64)0);
}

//
// Test: CalculateTotalMemoryBinSizeNeeded with pages allocated
//
TEST_F (BaseMemoryBinLibTest, CalculateTotalMemoryBinSizeNeededCalculatesCorrectSize) {
  UINT64  TotalSize;

  gMemoryTypeInformation[0].Type          = EfiRuntimeServicesCode;
  gMemoryTypeInformation[0].NumberOfPages = 10;
  gMemoryTypeInformation[1].Type          = EfiRuntimeServicesData;
  gMemoryTypeInformation[1].NumberOfPages = 20;
  gMemoryTypeInformation[2].Type          = EfiMaxMemoryType;

  TotalSize = CalculateTotalMemoryBinSizeNeeded (NULL, gMemoryTypeInformation);

  ASSERT_EQ (TotalSize, (UINT64)(30 * EFI_PAGE_SIZE));
}

//
// Test: CoreSetMemoryTypeInformationRange initializes bins correctly
//
TEST_F (BaseMemoryBinLibTest, CoreSetMemoryTypeInformationRangeSetsUpBinsCorrectly) {
  EFI_PHYSICAL_ADDRESS  Start;
  UINT64                Length;

  Start  = 0x100000;
  Length = 0x100000;

  gMemoryTypeInformation[0].Type          = EfiRuntimeServicesCode;
  gMemoryTypeInformation[0].NumberOfPages = 20;
  gMemoryTypeInformation[1].Type          = EfiRuntimeServicesData;
  gMemoryTypeInformation[1].NumberOfPages = 10;
  gMemoryTypeInformation[2].Type          = EfiReservedMemoryType;
  gMemoryTypeInformation[2].NumberOfPages = 5;
  gMemoryTypeInformation[3].Type          = EfiACPIReclaimMemory;
  gMemoryTypeInformation[3].NumberOfPages = 15;
  gMemoryTypeInformation[4].Type          = EfiACPIMemoryNVS;
  gMemoryTypeInformation[4].NumberOfPages = 25;
  gMemoryTypeInformation[5].Type          = EfiMaxMemoryType;

  CoreSetMemoryTypeInformationRange (Start, Length, gMemoryTypeInformation, &mMemoryTypeInformationInitialized, mMemoryTypeStatistics, &mDefaultMaximumAddress);

  ASSERT_TRUE (mMemoryTypeInformationInitialized);

  ASSERT_EQ (mMemoryTypeStatistics[EfiRuntimeServicesCode].NumberOfPages, (UINT32)20);
  ASSERT_EQ (mMemoryTypeStatistics[EfiRuntimeServicesData].NumberOfPages, (UINT32)10);
  ASSERT_EQ (mMemoryTypeStatistics[EfiReservedMemoryType].NumberOfPages, (UINT32)5);
  ASSERT_EQ (mMemoryTypeStatistics[EfiACPIReclaimMemory].NumberOfPages, (UINT32)15);
  ASSERT_EQ (mMemoryTypeStatistics[EfiACPIMemoryNVS].NumberOfPages, (UINT32)25);

  // Confirm contiguous bins
  ASSERT_EQ (mMemoryTypeStatistics[EfiRuntimeServicesData].MaximumAddress + 1, mMemoryTypeStatistics[EfiRuntimeServicesCode].BaseAddress);
  ASSERT_EQ (mMemoryTypeStatistics[EfiReservedMemoryType].MaximumAddress + 1, mMemoryTypeStatistics[EfiRuntimeServicesData].BaseAddress);
  ASSERT_EQ (mMemoryTypeStatistics[EfiACPIReclaimMemory].MaximumAddress + 1, mMemoryTypeStatistics[EfiReservedMemoryType].BaseAddress);
  ASSERT_EQ (mMemoryTypeStatistics[EfiACPIMemoryNVS].MaximumAddress + 1, mMemoryTypeStatistics[EfiACPIReclaimMemory].BaseAddress);
}

//
// Test: CoreSetMemoryTypeInformationRange rejects insufficient space
//
TEST_F (BaseMemoryBinLibTest, CoreSetMemoryTypeInformationRangeRejectsInsufficientSpace) {
  EFI_PHYSICAL_ADDRESS  Start;
  UINT64                Length;

  Start  = 0x100000;
  Length = 0x1000;

  gMemoryTypeInformation[0].Type          = EfiRuntimeServicesCode;
  gMemoryTypeInformation[0].NumberOfPages = 100;  // Requires more than 4KB
  gMemoryTypeInformation[1].Type          = EfiMaxMemoryType;

  CoreSetMemoryTypeInformationRange (Start, Length, gMemoryTypeInformation, &mMemoryTypeInformationInitialized, mMemoryTypeStatistics, &mDefaultMaximumAddress);

  ASSERT_FALSE (mMemoryTypeInformationInitialized);
}

//
// Test: CoreSetMemoryTypeInformationRange respects already initialized state
//
TEST_F (BaseMemoryBinLibTest, CoreSetMemoryTypeInformationRangeRespectsInitializedState) {
  EFI_PHYSICAL_ADDRESS  Start;
  UINT64                Length;

  mMemoryTypeInformationInitialized = TRUE;

  Start  = 0x100000;
  Length = 0x100000;

  gMemoryTypeInformation[0].Type                            = EfiRuntimeServicesCode;
  gMemoryTypeInformation[0].NumberOfPages                   = 10;
  mMemoryTypeStatistics[EfiRuntimeServicesCode].BaseAddress = 0x1000;
  gMemoryTypeInformation[1].Type                            = EfiMaxMemoryType;

  CoreSetMemoryTypeInformationRange (Start, Length, gMemoryTypeInformation, &mMemoryTypeInformationInitialized, mMemoryTypeStatistics, &mDefaultMaximumAddress);

  ASSERT_TRUE (mMemoryTypeInformationInitialized);
  ASSERT_EQ (mMemoryTypeStatistics[EfiRuntimeServicesCode].BaseAddress, (EFI_PHYSICAL_ADDRESS)0x1000);
}

//
// Test: UpdateMemoryStatistics updates in-bin statistics correctly
//
TEST_F (BaseMemoryBinLibTest, UpdateMemoryStatisticsUpdatesInBinCorrectly) {
  mMemoryTypeInformationInitialized = TRUE;

  mMemoryTypeStatistics[EfiRuntimeServicesCode].BaseAddress      = 0x100000;
  mMemoryTypeStatistics[EfiRuntimeServicesCode].MaximumAddress   = 0x200000;
  mMemoryTypeStatistics[EfiRuntimeServicesCode].InformationIndex = 0;

  gMemoryTypeInformation[0].Type          = EfiRuntimeServicesCode;
  gMemoryTypeInformation[0].NumberOfPages = 0;

  UpdateMemoryStatistics (
    EfiConventionalMemory,
    EfiRuntimeServicesCode,
    0x150000,  // Within bin range
    10,
    &mMemoryTypeInformationInitialized,
    mMemoryTypeStatistics,
    gMemoryTypeInformation,
    0x10000, // Just set these outside of the range
    0x20000
    );

  ASSERT_EQ (mMemoryTypeStatistics[EfiRuntimeServicesCode].CurrentNumberOfPages, (UINT32)10);
}

//
// Test: UpdateMemoryStatistics updates out-of-bin statistics correctly
//
TEST_F (BaseMemoryBinLibTest, UpdateMemoryStatisticsUpdatesOutOfBinCorrectly) {
  mMemoryTypeInformationInitialized = TRUE;

  mMemoryTypeStatistics[EfiRuntimeServicesCode].BaseAddress      = 0x100000;
  mMemoryTypeStatistics[EfiRuntimeServicesCode].MaximumAddress   = 0x200000;
  mMemoryTypeStatistics[EfiRuntimeServicesCode].InformationIndex = 0;

  gMemoryTypeInformation[0].Type          = EfiRuntimeServicesCode;
  gMemoryTypeInformation[0].NumberOfPages = 0;

  UpdateMemoryStatistics (
    EfiConventionalMemory,
    EfiRuntimeServicesCode,
    0x300000,  // Outside bin range
    10,
    &mMemoryTypeInformationInitialized,
    mMemoryTypeStatistics,
    gMemoryTypeInformation,
    0x10000, // Just set these outside of the range
    0x20000
    );

  ASSERT_EQ (mMemoryTypeStatistics[EfiRuntimeServicesCode].CurrentNumberOfPages, (UINT32)0);
}

//
// Test: UpdateMemoryStatistics handles freeing memory
//
TEST_F (BaseMemoryBinLibTest, UpdateMemoryStatisticsHandlesFreeingMemory) {
  mMemoryTypeInformationInitialized = TRUE;

  mMemoryTypeStatistics[EfiRuntimeServicesCode].BaseAddress          = 0x100000;
  mMemoryTypeStatistics[EfiRuntimeServicesCode].MaximumAddress       = 0x200000;
  mMemoryTypeStatistics[EfiRuntimeServicesCode].CurrentNumberOfPages = 20;
  mMemoryTypeStatistics[EfiRuntimeServicesCode].InformationIndex     = 0;

  gMemoryTypeInformation[0].Type          = EfiRuntimeServicesCode;
  gMemoryTypeInformation[0].NumberOfPages = 20;

  UpdateMemoryStatistics (
    EfiRuntimeServicesCode,
    EfiConventionalMemory,
    0x150000,  // Within bin range
    10,
    &mMemoryTypeInformationInitialized,
    mMemoryTypeStatistics,
    gMemoryTypeInformation,
    0x10000, // Just set these outside of the range
    0x20000
    );

  ASSERT_EQ (mMemoryTypeStatistics[EfiRuntimeServicesCode].CurrentNumberOfPages, (UINT32)10);
}

//
// Tests for AllocateMemoryTypeInformationBins
//

//
// Test: AllocateMemoryTypeInformationBins does nothing when already initialized
//
TEST_F (BaseMemoryBinLibTest, DoesNothingWhenAlreadyInitialized) {
  mMemoryTypeInformationInitialized                         = TRUE;
  mMemoryTypeStatistics[EfiRuntimeServicesCode].BaseAddress = 0x1000;

  AllocateMemoryTypeInformationBins (&mMemoryTypeInformationInitialized, gMemoryTypeInformation, mMemoryTypeStatistics, &mDefaultMaximumAddress, FALSE);

  // Should remain unchanged since already initialized
  ASSERT_TRUE (mMemoryTypeInformationInitialized);
  ASSERT_EQ (mMemoryTypeStatistics[EfiRuntimeServicesCode].BaseAddress, (EFI_PHYSICAL_ADDRESS)0x1000);
}

//
// Test: AllocateMemoryTypeInformationBins does nothing when no pages needed
//
TEST_F (BaseMemoryBinLibTest, DoesNothingWhenNoPagesNeeded) {
  AllocateMemoryTypeInformationBins (&mMemoryTypeInformationInitialized, gMemoryTypeInformation, mMemoryTypeStatistics, &mDefaultMaximumAddress, FALSE);

  ASSERT_TRUE (mMemoryTypeInformationInitialized);
  ASSERT_EQ (mMemoryTypeStatistics[EfiReservedMemoryType].NumberOfPages, (UINT32)0);
  ASSERT_EQ (mMemoryTypeStatistics[EfiRuntimeServicesCode].NumberOfPages, (UINT32)0);
  ASSERT_EQ (mMemoryTypeStatistics[EfiRuntimeServicesData].NumberOfPages, (UINT32)0);
  ASSERT_EQ (mMemoryTypeStatistics[EfiACPIReclaimMemory].NumberOfPages, (UINT32)0);
  ASSERT_EQ (mMemoryTypeStatistics[EfiACPIMemoryNVS].NumberOfPages, (UINT32)0);
}

//
// Test: AllocateMemoryTypeInformationBins allocates pages and initializes bins
//
TEST_F (BaseMemoryBinLibTest, AllocatesPagesAndInitializesBins) {
  gMemoryTypeInformation[0].Type          = EfiRuntimeServicesCode;
  gMemoryTypeInformation[0].NumberOfPages = 20;
  gMemoryTypeInformation[1].Type          = EfiRuntimeServicesData;
  gMemoryTypeInformation[1].NumberOfPages = 10;
  gMemoryTypeInformation[2].Type          = EfiReservedMemoryType;
  gMemoryTypeInformation[2].NumberOfPages = 5;
  gMemoryTypeInformation[3].Type          = EfiACPIReclaimMemory;
  gMemoryTypeInformation[3].NumberOfPages = 15;
  gMemoryTypeInformation[4].Type          = EfiACPIMemoryNVS;
  gMemoryTypeInformation[4].NumberOfPages = 25;
  gMemoryTypeInformation[5].Type          = EfiMaxMemoryType;

  // HOB should not be built in this case
  EXPECT_CALL (HobLib, BuildResourceDescriptorWithOwnerHob (_, _, _, _, _))
    .Times (0);

  AllocateMemoryTypeInformationBins (&mMemoryTypeInformationInitialized, gMemoryTypeInformation, mMemoryTypeStatistics, &mDefaultMaximumAddress, FALSE);

  ASSERT_TRUE (mMemoryTypeInformationInitialized);

  ASSERT_EQ (mMemoryTypeStatistics[EfiRuntimeServicesCode].NumberOfPages, (UINT32)20);
  ASSERT_EQ (mMemoryTypeStatistics[EfiRuntimeServicesData].NumberOfPages, (UINT32)10);
  ASSERT_EQ (mMemoryTypeStatistics[EfiReservedMemoryType].NumberOfPages, (UINT32)5);
  ASSERT_EQ (mMemoryTypeStatistics[EfiACPIReclaimMemory].NumberOfPages, (UINT32)15);
  ASSERT_EQ (mMemoryTypeStatistics[EfiACPIMemoryNVS].NumberOfPages, (UINT32)25);

  // Confirm contiguous bins
  ASSERT_EQ (mMemoryTypeStatistics[EfiRuntimeServicesData].MaximumAddress + 1, mMemoryTypeStatistics[EfiRuntimeServicesCode].BaseAddress);
  ASSERT_EQ (mMemoryTypeStatistics[EfiReservedMemoryType].MaximumAddress + 1, mMemoryTypeStatistics[EfiRuntimeServicesData].BaseAddress);
  ASSERT_EQ (mMemoryTypeStatistics[EfiACPIReclaimMemory].MaximumAddress + 1, mMemoryTypeStatistics[EfiReservedMemoryType].BaseAddress);
  ASSERT_EQ (mMemoryTypeStatistics[EfiACPIMemoryNVS].MaximumAddress + 1, mMemoryTypeStatistics[EfiACPIReclaimMemory].BaseAddress);
}

//
// Test: AllocateMemoryTypeInformationBins creates HOB when requested
//
TEST_F (BaseMemoryBinLibTest, CreatesHobWhenRequested) {
  UINT64  TotalPages = 30;

  gMemoryTypeInformation[0].Type          = EfiRuntimeServicesCode;
  gMemoryTypeInformation[0].NumberOfPages = 10;
  gMemoryTypeInformation[1].Type          = EfiRuntimeServicesData;
  gMemoryTypeInformation[1].NumberOfPages = 20;
  gMemoryTypeInformation[2].Type          = EfiMaxMemoryType;

  EXPECT_CALL (
    HobLib,
    BuildResourceDescriptorWithOwnerHob (
      EFI_RESOURCE_SYSTEM_MEMORY,
      EFI_RESOURCE_ATTRIBUTE_PRESENT | EFI_RESOURCE_ATTRIBUTE_INITIALIZED | EFI_RESOURCE_ATTRIBUTE_TESTED,
      _,
      TotalPages * EFI_PAGE_SIZE,
      &gEfiMemoryTypeInformationGuid
      )
    )
    .Times (1);

  AllocateMemoryTypeInformationBins (&mMemoryTypeInformationInitialized, gMemoryTypeInformation, mMemoryTypeStatistics, &mDefaultMaximumAddress, TRUE);

  ASSERT_TRUE (mMemoryTypeInformationInitialized);
}

//
// Test: PopulateMemoryTypeInformation returns NOT_FOUND when Memory Type Information HOB is missing
//
TEST_F (BaseMemoryBinLibTest, ReturnsNotFoundWhenMemoryTypeInformationHobMissing) {
  EFI_STATUS  Status;

  EXPECT_CALL (HobLib, GetFirstGuidHob (&gEfiMemoryTypeInformationGuid))
    .WillOnce (Return ((VOID *)NULL));

  Status = PopulateMemoryTypeInformation (gMemoryTypeInformation);

  ASSERT_EQ (Status, EFI_NOT_FOUND);
}

//
// Test: PopulateMemoryTypeInformation populates from valid HOB
//
//
// Align a page count to the allocation granularity that
// PopulateMemoryTypeInformation applies for the given memory type.  Runtime
// memory types are rounded up to RUNTIME_PAGE_ALLOCATION_GRANULARITY, which is
// larger than EFI_PAGE_SIZE on some architectures (e.g. 64 KiB on AArch64), so
// the resulting page counts are architecture dependent.
//
static UINT32
ExpectedAlignedPages (
  EFI_MEMORY_TYPE  Type,
  UINT32           NumberOfPages
  )
{
  UINT32  Granularity;

  if ((Type == EfiReservedMemoryType) ||
      (Type == EfiACPIMemoryNVS) ||
      (Type == EfiRuntimeServicesCode) ||
      (Type == EfiRuntimeServicesData))
  {
    Granularity = RUNTIME_PAGE_ALLOCATION_GRANULARITY;
  } else {
    Granularity = DEFAULT_PAGE_ALLOCATION_GRANULARITY;
  }

  return (UINT32)EFI_SIZE_TO_PAGES (
                   ALIGN_VALUE (EFI_PAGES_TO_SIZE ((UINTN)NumberOfPages), Granularity)
                   );
}

TEST_F (BaseMemoryBinLibTest, PopulatesFromValidHob) {
  EFI_STATUS                   Status;
  UINT8                        GuidHobBuffer[sizeof (EFI_HOB_GUID_TYPE) + sizeof (EFI_MEMORY_TYPE_INFORMATION) * 7];
  EFI_HOB_GUID_TYPE            *GuidHob;
  EFI_MEMORY_TYPE_INFORMATION  *MemTypeInfo;

  ZeroMem (GuidHobBuffer, sizeof (GuidHobBuffer));
  GuidHob                   = (EFI_HOB_GUID_TYPE *)GuidHobBuffer;
  GuidHob->Header.HobType   = EFI_HOB_TYPE_GUID_EXTENSION;
  GuidHob->Header.HobLength = sizeof (GuidHobBuffer);
  CopyGuid (&GuidHob->Name, &gEfiMemoryTypeInformationGuid);

  MemTypeInfo                  = (EFI_MEMORY_TYPE_INFORMATION *)(GuidHob + 1);
  MemTypeInfo[0].Type          = EfiReservedMemoryType;
  MemTypeInfo[0].NumberOfPages = 5;
  MemTypeInfo[1].Type          = EfiRuntimeServicesCode;
  MemTypeInfo[1].NumberOfPages = 10;
  MemTypeInfo[2].Type          = EfiRuntimeServicesData;
  MemTypeInfo[2].NumberOfPages = 15;
  MemTypeInfo[3].Type          = EfiACPIReclaimMemory;
  MemTypeInfo[3].NumberOfPages = 20;
  MemTypeInfo[4].Type          = EfiACPIMemoryNVS;
  MemTypeInfo[4].NumberOfPages = 25;
  MemTypeInfo[5].Type          = EfiPalCode;
  MemTypeInfo[5].NumberOfPages = 8;
  MemTypeInfo[6].Type          = EfiMaxMemoryType;
  MemTypeInfo[6].NumberOfPages = 0;

  EXPECT_CALL (HobLib, GetFirstGuidHob (&gEfiMemoryTypeInformationGuid))
    .WillOnce (Return ((VOID *)GuidHob));

  Status = PopulateMemoryTypeInformation (gMemoryTypeInformation);

  ASSERT_EQ (Status, EFI_SUCCESS);
  ASSERT_EQ (gMemoryTypeInformation[0].Type, (UINT32)EfiReservedMemoryType);
  ASSERT_EQ (gMemoryTypeInformation[0].NumberOfPages, ExpectedAlignedPages (EfiReservedMemoryType, 5));
  ASSERT_EQ (gMemoryTypeInformation[1].Type, (UINT32)EfiRuntimeServicesCode);
  ASSERT_EQ (gMemoryTypeInformation[1].NumberOfPages, ExpectedAlignedPages (EfiRuntimeServicesCode, 10));
  ASSERT_EQ (gMemoryTypeInformation[2].Type, (UINT32)EfiRuntimeServicesData);
  ASSERT_EQ (gMemoryTypeInformation[2].NumberOfPages, ExpectedAlignedPages (EfiRuntimeServicesData, 15));
  ASSERT_EQ (gMemoryTypeInformation[3].Type, (UINT32)EfiACPIReclaimMemory);
  ASSERT_EQ (gMemoryTypeInformation[3].NumberOfPages, ExpectedAlignedPages (EfiACPIReclaimMemory, 20));
  ASSERT_EQ (gMemoryTypeInformation[4].Type, (UINT32)EfiACPIMemoryNVS);
  ASSERT_EQ (gMemoryTypeInformation[4].NumberOfPages, ExpectedAlignedPages (EfiACPIMemoryNVS, 25));
  ASSERT_EQ (gMemoryTypeInformation[5].Type, (UINT32)EfiPalCode);
  ASSERT_EQ (gMemoryTypeInformation[5].NumberOfPages, ExpectedAlignedPages (EfiPalCode, 8));
}

//
// Test: PopulateMemoryTypeInformation zeroes all pages when HOB is corrupted
// (i.e. it does not terminate in an EfiMaxMemoryType entry)
//
TEST_F (BaseMemoryBinLibTest, ZeroesAllPagesWhenHobIsCorrupted) {
  EFI_STATUS                   Status;
  UINT8                        GuidHobBuffer[sizeof (EFI_HOB_GUID_TYPE) + sizeof (EFI_MEMORY_TYPE_INFORMATION) * 7];
  EFI_HOB_GUID_TYPE            *GuidHob;
  EFI_MEMORY_TYPE_INFORMATION  *MemTypeInfo;

  ZeroMem (GuidHobBuffer, sizeof (GuidHobBuffer));
  GuidHob                   = (EFI_HOB_GUID_TYPE *)GuidHobBuffer;
  GuidHob->Header.HobType   = EFI_HOB_TYPE_GUID_EXTENSION;
  GuidHob->Header.HobLength = sizeof (GuidHobBuffer);
  CopyGuid (&GuidHob->Name, &gEfiMemoryTypeInformationGuid);

  //
  // Populate every entry with a valid type and a non-zero page count, but
  // deliberately omit the terminating EfiMaxMemoryType entry to corrupt the HOB.
  //
  MemTypeInfo                  = (EFI_MEMORY_TYPE_INFORMATION *)(GuidHob + 1);
  MemTypeInfo[0].Type          = EfiReservedMemoryType;
  MemTypeInfo[0].NumberOfPages = 5;
  MemTypeInfo[1].Type          = EfiRuntimeServicesCode;
  MemTypeInfo[1].NumberOfPages = 10;
  MemTypeInfo[2].Type          = EfiRuntimeServicesData;
  MemTypeInfo[2].NumberOfPages = 15;
  MemTypeInfo[3].Type          = EfiACPIReclaimMemory;
  MemTypeInfo[3].NumberOfPages = 20;
  MemTypeInfo[4].Type          = EfiACPIMemoryNVS;
  MemTypeInfo[4].NumberOfPages = 25;
  MemTypeInfo[5].Type          = EfiPalCode;
  MemTypeInfo[5].NumberOfPages = 8;
  MemTypeInfo[6].Type          = EfiBootServicesData;
  MemTypeInfo[6].NumberOfPages = 30;

  EXPECT_CALL (HobLib, GetFirstGuidHob (&gEfiMemoryTypeInformationGuid))
    .WillOnce (Return ((VOID *)GuidHob));

  Status = PopulateMemoryTypeInformation (gMemoryTypeInformation);

  ASSERT_EQ (Status, EFI_NOT_FOUND);

  //
  // Regardless of the page counts present in the corrupted HOB, every entry
  // should have been reset to zero pages.
  //
  for (UINTN Index = 0; gMemoryTypeInformation[Index].Type != EfiMaxMemoryType; Index++) {
    ASSERT_EQ (gMemoryTypeInformation[Index].NumberOfPages, (UINT32)0);
  }
}

//
// Test: GetMemoryTypeInformationResourceHob returns NOT_FOUND when no resource HOB exists
//
TEST_F (BaseMemoryBinLibTest, GetMemoryTypeInformationResourceHobReturnsNotFoundWhenNoResourceHob) {
  EFI_HOB_RESOURCE_DESCRIPTOR  *ResourceHob;
  UINT8                        HobListBuffer[sizeof (EFI_HOB_HANDOFF_INFO_TABLE) + sizeof (EFI_HOB_GENERIC_HEADER)];
  EFI_HOB_HANDOFF_INFO_TABLE   *HandoffHob;
  EFI_HOB_GENERIC_HEADER       *EndOfHobList;
  VOID                         *HobStart;

  ZeroMem (HobListBuffer, sizeof (HobListBuffer));

  HandoffHob                   = (EFI_HOB_HANDOFF_INFO_TABLE *)HobListBuffer;
  HandoffHob->Header.HobType   = EFI_HOB_TYPE_HANDOFF;
  HandoffHob->Header.HobLength = sizeof (EFI_HOB_HANDOFF_INFO_TABLE);

  EndOfHobList            = (EFI_HOB_GENERIC_HEADER *)(HandoffHob + 1);
  EndOfHobList->HobType   = EFI_HOB_TYPE_END_OF_HOB_LIST;
  EndOfHobList->HobLength = sizeof (EFI_HOB_GENERIC_HEADER);

  HobStart = (VOID *)HobListBuffer;

  ResourceHob = GetMemoryTypeInformationResourceHob (&HobStart, gMemoryTypeInformation);

  ASSERT_EQ (ResourceHob, NULL);
}

//
// Test: GetMemoryTypeInformationResourceHob finds matching resource HOB
//
TEST_F (BaseMemoryBinLibTest, GetMemoryTypeInformationResourceHobFindsMatchingHob) {
  EFI_HOB_RESOURCE_DESCRIPTOR  *ResourceHob;
  UINT8                        HobListBuffer[sizeof (EFI_HOB_HANDOFF_INFO_TABLE) + 3 *sizeof (EFI_HOB_RESOURCE_DESCRIPTOR) + sizeof (EFI_HOB_GENERIC_HEADER)];
  EFI_HOB_HANDOFF_INFO_TABLE   *HandoffHob;
  EFI_HOB_RESOURCE_DESCRIPTOR  *ResourceDescriptor;
  EFI_HOB_GENERIC_HEADER       *EndOfHobList;
  VOID                         *HobStart;
  EFI_GUID                     OtherGuid = { 0x12345678, 0x1234, 0x1234, { 0x12, 0x34, 0x56, 0x78, 0x9A, 0xBC, 0xDE, 0xF0 }
  };

  ZeroMem (HobListBuffer, sizeof (HobListBuffer));

  HandoffHob                   = (EFI_HOB_HANDOFF_INFO_TABLE *)HobListBuffer;
  HandoffHob->Header.HobType   = EFI_HOB_TYPE_HANDOFF;
  HandoffHob->Header.HobLength = sizeof (EFI_HOB_HANDOFF_INFO_TABLE);

  ResourceDescriptor                   = (EFI_HOB_RESOURCE_DESCRIPTOR *)(HandoffHob + 1);
  ResourceDescriptor->Header.HobType   = EFI_HOB_TYPE_RESOURCE_DESCRIPTOR;
  ResourceDescriptor->Header.HobLength = sizeof (EFI_HOB_RESOURCE_DESCRIPTOR);
  ResourceDescriptor->ResourceType     = EFI_RESOURCE_SYSTEM_MEMORY;
  CopyGuid (&ResourceDescriptor->Owner, &OtherGuid);
  ResourceDescriptor->PhysicalStart  = 0x100000;
  ResourceDescriptor->ResourceLength = 0x50000;

  ResourceDescriptor                   = (EFI_HOB_RESOURCE_DESCRIPTOR *)(ResourceDescriptor + 1);
  ResourceDescriptor->Header.HobType   = EFI_HOB_TYPE_RESOURCE_DESCRIPTOR;
  ResourceDescriptor->Header.HobLength = sizeof (EFI_HOB_RESOURCE_DESCRIPTOR);
  ResourceDescriptor->ResourceType     = EFI_RESOURCE_SYSTEM_MEMORY;
  CopyGuid (&ResourceDescriptor->Owner, &gEfiMemoryTypeInformationGuid);
  ResourceDescriptor->PhysicalStart     = 0x200000;
  ResourceDescriptor->ResourceLength    = 0x75000;
  ResourceDescriptor->ResourceAttribute = EFI_RESOURCE_ATTRIBUTE_PRESENT | EFI_RESOURCE_ATTRIBUTE_INITIALIZED | EFI_RESOURCE_ATTRIBUTE_TESTED;

  ResourceDescriptor                   = (EFI_HOB_RESOURCE_DESCRIPTOR *)(ResourceDescriptor + 1);
  ResourceDescriptor->Header.HobType   = EFI_HOB_TYPE_RESOURCE_DESCRIPTOR;
  ResourceDescriptor->Header.HobLength = sizeof (EFI_HOB_RESOURCE_DESCRIPTOR);
  ResourceDescriptor->ResourceType     = EFI_RESOURCE_SYSTEM_MEMORY;
  CopyGuid (&ResourceDescriptor->Owner, &OtherGuid);
  ResourceDescriptor->PhysicalStart  = 0x300000;
  ResourceDescriptor->ResourceLength = 0x50000;

  EndOfHobList            = (EFI_HOB_GENERIC_HEADER *)(ResourceDescriptor + 1);
  EndOfHobList->HobType   = EFI_HOB_TYPE_END_OF_HOB_LIST;
  EndOfHobList->HobLength = sizeof (EFI_HOB_GENERIC_HEADER);

  HobStart = (VOID *)HobListBuffer;

  ResourceHob = GetMemoryTypeInformationResourceHob (&HobStart, gMemoryTypeInformation);

  ASSERT_EQ (ResourceHob->PhysicalStart, (EFI_PHYSICAL_ADDRESS)0x200000);
  ASSERT_EQ (ResourceHob->ResourceLength, (UINT64)0x75000);
}

//
// Test: GetMemoryTypeInformationResourceHob Fails When Multiple Matching HOBs Exist
//
TEST_F (BaseMemoryBinLibTest, GetMemoryTypeInformationResourceHobFailsWithMultipleMatchingHobs) {
  EFI_HOB_RESOURCE_DESCRIPTOR  *ResourceHob;
  UINT8                        HobListBuffer[sizeof (EFI_HOB_HANDOFF_INFO_TABLE) + 2 * sizeof (EFI_HOB_RESOURCE_DESCRIPTOR) + sizeof (EFI_HOB_GENERIC_HEADER)];
  EFI_HOB_HANDOFF_INFO_TABLE   *HandoffHob;
  EFI_HOB_RESOURCE_DESCRIPTOR  *FirstMatchingResource;
  EFI_HOB_RESOURCE_DESCRIPTOR  *SecondMatchingResource;
  EFI_HOB_GENERIC_HEADER       *EndOfHobList;
  VOID                         *HobStart;

  ZeroMem (HobListBuffer, sizeof (HobListBuffer));

  HandoffHob                   = (EFI_HOB_HANDOFF_INFO_TABLE *)HobListBuffer;
  HandoffHob->Header.HobType   = EFI_HOB_TYPE_HANDOFF;
  HandoffHob->Header.HobLength = sizeof (EFI_HOB_HANDOFF_INFO_TABLE);

  // First matching resource HOB
  FirstMatchingResource                   = (EFI_HOB_RESOURCE_DESCRIPTOR *)(HandoffHob + 1);
  FirstMatchingResource->Header.HobType   = EFI_HOB_TYPE_RESOURCE_DESCRIPTOR;
  FirstMatchingResource->Header.HobLength = sizeof (EFI_HOB_RESOURCE_DESCRIPTOR);
  FirstMatchingResource->ResourceType     = EFI_RESOURCE_SYSTEM_MEMORY;
  CopyGuid (&FirstMatchingResource->Owner, &gEfiMemoryTypeInformationGuid);
  FirstMatchingResource->PhysicalStart     = 0x100000;
  FirstMatchingResource->ResourceLength    = 0x50000;
  FirstMatchingResource->ResourceAttribute = EFI_RESOURCE_ATTRIBUTE_PRESENT | EFI_RESOURCE_ATTRIBUTE_INITIALIZED | EFI_RESOURCE_ATTRIBUTE_TESTED;

  // Second matching resource HOB
  SecondMatchingResource                   = (EFI_HOB_RESOURCE_DESCRIPTOR *)(FirstMatchingResource + 1);
  SecondMatchingResource->Header.HobType   = EFI_HOB_TYPE_RESOURCE_DESCRIPTOR;
  SecondMatchingResource->Header.HobLength = sizeof (EFI_HOB_RESOURCE_DESCRIPTOR);
  SecondMatchingResource->ResourceType     = EFI_RESOURCE_SYSTEM_MEMORY;
  CopyGuid (&SecondMatchingResource->Owner, &gEfiMemoryTypeInformationGuid);
  SecondMatchingResource->PhysicalStart     = 0x400000;
  SecondMatchingResource->ResourceLength    = 0x80000;
  SecondMatchingResource->ResourceAttribute = EFI_RESOURCE_ATTRIBUTE_PRESENT | EFI_RESOURCE_ATTRIBUTE_INITIALIZED | EFI_RESOURCE_ATTRIBUTE_TESTED;

  EndOfHobList            = (EFI_HOB_GENERIC_HEADER *)(SecondMatchingResource + 1);
  EndOfHobList->HobType   = EFI_HOB_TYPE_END_OF_HOB_LIST;
  EndOfHobList->HobLength = sizeof (EFI_HOB_GENERIC_HEADER);

  HobStart = (VOID *)HobListBuffer;

  ResourceHob = GetMemoryTypeInformationResourceHob (&HobStart, gMemoryTypeInformation);

  ASSERT_EQ (ResourceHob, NULL);
}

int
main (
  int   argc,
  char  *argv[]
  )
{
  testing::InitGoogleTest (&argc, argv);
  return RUN_ALL_TESTS ();
}