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
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
|
/** @file
Unit tests of the CpuPageTableLib instance of the CpuPageTableLib class
Copyright (c) 2022, Intel Corporation. All rights reserved.<BR>
SPDX-License-Identifier: BSD-2-Clause-Patent
**/
#include "CpuPageTableLibUnitTest.h"
// ----------------------------------------------------------------------- PageMode--TestCount-TestRangeCount---RandomOptions
static CPU_PAGE_TABLE_LIB_RANDOM_TEST_CONTEXT mTestContextPaging4Level = { Paging4Level, 100, 20, ONLY_ONE_ONE_MAPPING|USE_RANDOM_ARRAY };
static CPU_PAGE_TABLE_LIB_RANDOM_TEST_CONTEXT mTestContextPaging4Level1GB = { Paging4Level1GB, 100, 20, ONLY_ONE_ONE_MAPPING|USE_RANDOM_ARRAY };
static CPU_PAGE_TABLE_LIB_RANDOM_TEST_CONTEXT mTestContextPaging5Level = { Paging5Level, 100, 20, ONLY_ONE_ONE_MAPPING|USE_RANDOM_ARRAY };
static CPU_PAGE_TABLE_LIB_RANDOM_TEST_CONTEXT mTestContextPaging5Level1GB = { Paging5Level1GB, 100, 20, ONLY_ONE_ONE_MAPPING|USE_RANDOM_ARRAY };
/**
Check if the input parameters are not supported.
@param[in] Context [Optional] An optional parameter that enables:
1) test-case reuse with varied parameters and
2) test-case re-entry for Target tests that need a
reboot. This parameter is a VOID* and it is the
responsibility of the test author to ensure that the
contents are well understood by all test cases that may
consume it.
@retval UNIT_TEST_PASSED The Unit test has completed and the test
case was successful.
@retval UNIT_TEST_ERROR_TEST_FAILED A test case assertion has failed.
**/
UNIT_TEST_STATUS
EFIAPI
TestCaseForParameter (
IN UNIT_TEST_CONTEXT Context
)
{
UINTN PageTable;
PAGING_MODE PagingMode;
UINTN Buffer;
UINTN PageTableBufferSize;
IA32_MAP_ATTRIBUTE MapAttribute;
IA32_MAP_ATTRIBUTE MapMask;
MapAttribute.Uint64 = 0;
MapMask.Uint64 = 0;
PagingMode = Paging5Level1GB;
PageTableBufferSize = 0;
PageTable = 0;
//
// If the input linear address is not 4K align, it should return invalid parameter
//
UT_ASSERT_EQUAL (PageTableMap (&PageTable, PagingMode, &Buffer, &PageTableBufferSize, 1, SIZE_4KB, &MapAttribute, &MapMask), RETURN_INVALID_PARAMETER);
//
// If the input PageTableBufferSize is not 4K align, it should return invalid parameter
//
PageTableBufferSize = 10;
UT_ASSERT_EQUAL (PageTableMap (&PageTable, PagingMode, &Buffer, &PageTableBufferSize, 0, SIZE_4KB, &MapAttribute, &MapMask), RETURN_INVALID_PARAMETER);
//
// If the input PagingMode is Paging32bit, it should return invalid parameter
//
PageTableBufferSize = 0;
PagingMode = Paging32bit;
UT_ASSERT_EQUAL (PageTableMap (&PageTable, PagingMode, &Buffer, &PageTableBufferSize, 1, SIZE_4KB, &MapAttribute, &MapMask), RETURN_UNSUPPORTED);
//
// If the input MapMask is NULL, it should return invalid parameter
//
PagingMode = Paging5Level1GB;
UT_ASSERT_EQUAL (PageTableMap (&PageTable, PagingMode, &Buffer, &PageTableBufferSize, 1, SIZE_4KB, &MapAttribute, NULL), RETURN_INVALID_PARAMETER);
return UNIT_TEST_PASSED;
}
/**
Check the case that modifying page table doesn't need extra buffe
@param[in] Context [Optional] An optional parameter that enables:
1) test-case reuse with varied parameters and
2) test-case re-entry for Target tests that need a
reboot. This parameter is a VOID* and it is the
responsibility of the test author to ensure that the
contents are well understood by all test cases that may
consume it.
@retval UNIT_TEST_PASSED The Unit test has completed and the test
case was successful.
@retval UNIT_TEST_ERROR_TEST_FAILED A test case assertion has failed.
**/
UNIT_TEST_STATUS
EFIAPI
TestCaseWhichNoNeedExtraSize (
IN UNIT_TEST_CONTEXT Context
)
{
UINTN PageTable;
PAGING_MODE PagingMode;
VOID *Buffer;
UINTN PageTableBufferSize;
IA32_MAP_ATTRIBUTE MapAttribute;
IA32_MAP_ATTRIBUTE MapMask;
RETURN_STATUS Status;
UNIT_TEST_STATUS TestStatus;
MapAttribute.Uint64 = 0;
MapMask.Uint64 = 0;
PagingMode = Paging4Level1GB;
PageTableBufferSize = 0;
PageTable = 0;
Buffer = NULL;
MapAttribute.Bits.Present = 1;
MapAttribute.Bits.Nx = 1;
MapMask.Bits.Present = 1;
MapMask.Uint64 = MAX_UINT64;
//
// Create page table to cover [0, 10M], it should have 5 PTE
//
Status = PageTableMap (&PageTable, PagingMode, Buffer, &PageTableBufferSize, 0, (UINT64)SIZE_2MB * 5, &MapAttribute, &MapMask);
UT_ASSERT_EQUAL (Status, RETURN_BUFFER_TOO_SMALL);
Buffer = AllocatePages (EFI_SIZE_TO_PAGES (PageTableBufferSize));
Status = PageTableMap (&PageTable, PagingMode, Buffer, &PageTableBufferSize, 0, (UINT64)SIZE_2MB * 5, &MapAttribute, &MapMask);
UT_ASSERT_EQUAL (Status, RETURN_SUCCESS);
TestStatus = IsPageTableValid (PageTable, PagingMode);
if (TestStatus != UNIT_TEST_PASSED) {
return TestStatus;
}
//
// call library to cover [0, 4K], because the page table is already cover [0, 10M], and no attribute change,
// We assume the fucntion doesn't need to change page table, return success and output BufferSize is 0
//
Buffer = NULL;
Status = PageTableMap (&PageTable, PagingMode, Buffer, &PageTableBufferSize, 0, (UINT64)SIZE_4KB, &MapAttribute, &MapMask);
UT_ASSERT_EQUAL (PageTableBufferSize, 0);
UT_ASSERT_EQUAL (Status, RETURN_SUCCESS);
TestStatus = IsPageTableValid (PageTable, PagingMode);
if (TestStatus != UNIT_TEST_PASSED) {
return TestStatus;
}
//
// Same range and same attribute, only clear one mask attribute bit
// We assume the fucntion doesn't need to change page table, return success and output BufferSize is 0
//
MapMask.Bits.Nx = 0;
PageTableBufferSize = 0;
Status = PageTableMap (&PageTable, PagingMode, NULL, &PageTableBufferSize, 0, (UINT64)SIZE_4KB, &MapAttribute, &MapMask);
UT_ASSERT_EQUAL (Status, RETURN_SUCCESS);
UT_ASSERT_EQUAL (PageTableBufferSize, 0);
TestStatus = IsPageTableValid (PageTable, PagingMode);
if (TestStatus != UNIT_TEST_PASSED) {
return TestStatus;
}
//
// call library to cover [2M, 4M], while the page table is already cover [0, 10M],
// only change one attribute bit, we assume the page table change be modified even if the
// input Buffer is NULL, and BufferSize is 0
//
MapAttribute.Bits.Accessed = 1;
MapMask.Bits.Accessed = 1;
PageTableBufferSize = 0;
Status = PageTableMap (&PageTable, PagingMode, NULL, &PageTableBufferSize, (UINT64)SIZE_2MB, (UINT64)SIZE_2MB, &MapAttribute, &MapMask);
UT_ASSERT_EQUAL (Status, RETURN_SUCCESS);
UT_ASSERT_EQUAL (PageTableBufferSize, 0);
TestStatus = IsPageTableValid (PageTable, PagingMode);
if (TestStatus != UNIT_TEST_PASSED) {
return TestStatus;
}
return UNIT_TEST_PASSED;
}
/**
Test Case that check the case to map [0, 1G] to [8K, 1G+8K]
@param[in] Context [Optional] An optional parameter that enables:
1) test-case reuse with varied parameters and
2) test-case re-entry for Target tests that need a
reboot. This parameter is a VOID* and it is the
responsibility of the test author to ensure that the
contents are well understood by all test cases that may
consume it.
@retval UNIT_TEST_PASSED The Unit test has completed and the test
case was successful.
@retval UNIT_TEST_ERROR_TEST_FAILED A test case assertion has failed.
**/
UNIT_TEST_STATUS
EFIAPI
TestCase1Gmapto4K (
IN UNIT_TEST_CONTEXT Context
)
{
UINTN PageTable;
PAGING_MODE PagingMode;
VOID *Buffer;
UINTN PageTableBufferSize;
IA32_MAP_ATTRIBUTE MapAttribute;
IA32_MAP_ATTRIBUTE MapMask;
RETURN_STATUS Status;
UNIT_TEST_STATUS TestStatus;
//
// Create Page table to map [0,1G] to [8K, 1G+8K]
//
PagingMode = Paging4Level1GB;
PageTableBufferSize = 0;
PageTable = 0;
Buffer = NULL;
MapAttribute.Uint64 = (UINT64)SIZE_4KB * 2;
MapMask.Uint64 = (UINT64)SIZE_4KB * 2;
MapAttribute.Bits.Present = 1;
MapMask.Bits.Present = 1;
MapMask.Uint64 = MAX_UINT64;
Status = PageTableMap (&PageTable, PagingMode, Buffer, &PageTableBufferSize, (UINT64)0, (UINT64)SIZE_1GB, &MapAttribute, &MapMask);
UT_ASSERT_EQUAL (Status, RETURN_BUFFER_TOO_SMALL);
Buffer = AllocatePages (EFI_SIZE_TO_PAGES (PageTableBufferSize));
Status = PageTableMap (&PageTable, PagingMode, Buffer, &PageTableBufferSize, (UINT64)0, (UINT64)SIZE_1GB, &MapAttribute, &MapMask);
UT_ASSERT_EQUAL (Status, RETURN_SUCCESS);
//
// Page table should be valid. (All reserved bits are zero)
//
TestStatus = IsPageTableValid (PageTable, PagingMode);
if (TestStatus != UNIT_TEST_PASSED) {
return TestStatus;
}
return UNIT_TEST_PASSED;
}
/**
Check if the parent entry has different R/W attribute
@param[in] Context [Optional] An optional parameter that enables:
1) test-case reuse with varied parameters and
2) test-case re-entry for Target tests that need a
reboot. This parameter is a VOID* and it is the
responsibility of the test author to ensure that the
contents are well understood by all test cases that may
consume it.
@retval UNIT_TEST_PASSED The Unit test has completed and the test
case was successful.
@retval UNIT_TEST_ERROR_TEST_FAILED A test case assertion has failed.
**/
UNIT_TEST_STATUS
EFIAPI
TestCaseManualChangeReadWrite (
IN UNIT_TEST_CONTEXT Context
)
{
UINTN PageTable;
PAGING_MODE PagingMode;
VOID *Buffer;
UINTN PageTableBufferSize;
IA32_MAP_ATTRIBUTE MapAttribute;
IA32_MAP_ATTRIBUTE ExpectedMapAttribute;
IA32_MAP_ATTRIBUTE MapMask;
RETURN_STATUS Status;
IA32_MAP_ENTRY *Map;
UINTN MapCount;
IA32_PAGING_ENTRY *PagingEntry;
VOID *BackupBuffer;
UINTN BackupPageTableBufferSize;
PagingMode = Paging4Level;
PageTableBufferSize = 0;
PageTable = 0;
Buffer = NULL;
MapAttribute.Uint64 = 0;
MapMask.Uint64 = MAX_UINT64;
MapAttribute.Bits.Present = 1;
MapAttribute.Bits.ReadWrite = 1;
//
// Create Page table to cover [0,2G], with ReadWrite = 1
//
Status = PageTableMap (&PageTable, PagingMode, Buffer, &PageTableBufferSize, 0, SIZE_2GB, &MapAttribute, &MapMask);
UT_ASSERT_EQUAL (Status, RETURN_BUFFER_TOO_SMALL);
BackupPageTableBufferSize = PageTableBufferSize;
Buffer = AllocatePages (EFI_SIZE_TO_PAGES (PageTableBufferSize));
Status = PageTableMap (&PageTable, PagingMode, Buffer, &PageTableBufferSize, 0, SIZE_2GB, &MapAttribute, &MapMask);
UT_ASSERT_EQUAL (Status, RETURN_SUCCESS);
IsPageTableValid (PageTable, PagingMode);
MapCount = 0;
Status = PageTableParse (PageTable, PagingMode, NULL, &MapCount);
UT_ASSERT_EQUAL (Status, RETURN_BUFFER_TOO_SMALL);
IsPageTableValid (PageTable, PagingMode);
Map = AllocatePages (EFI_SIZE_TO_PAGES (MapCount));
Status = PageTableParse (PageTable, PagingMode, Map, &MapCount);
UT_ASSERT_EQUAL (Status, RETURN_SUCCESS);
UT_ASSERT_EQUAL (MapCount, 1);
UT_ASSERT_EQUAL (Map[0].LinearAddress, 0);
UT_ASSERT_EQUAL (Map[0].Length, SIZE_2GB);
ExpectedMapAttribute.Uint64 = MapAttribute.Uint64;
UT_ASSERT_EQUAL (Map[0].Attribute.Uint64, ExpectedMapAttribute.Uint64);
//
// Manually change ReadWrite to 0 for non-leaf entry, which covers [0,2G]
//
PagingEntry = (IA32_PAGING_ENTRY *)(UINTN)PageTable;
PagingEntry->Uint64 = PagingEntry->Uint64 & (~(UINT64)0x2);
MapCount = 0;
Status = PageTableParse (PageTable, PagingMode, NULL, &MapCount);
UT_ASSERT_EQUAL (Status, RETURN_BUFFER_TOO_SMALL);
Map = AllocatePages (EFI_SIZE_TO_PAGES (MapCount));
Status = PageTableParse (PageTable, PagingMode, Map, &MapCount);
UT_ASSERT_EQUAL (Status, RETURN_SUCCESS);
UT_ASSERT_EQUAL (MapCount, 1);
UT_ASSERT_EQUAL (Map[0].LinearAddress, 0);
UT_ASSERT_EQUAL (Map[0].Length, SIZE_2GB);
ExpectedMapAttribute.Uint64 = MapAttribute.Uint64;
ExpectedMapAttribute.Bits.ReadWrite = 0;
UT_ASSERT_EQUAL (Map[0].Attribute.Uint64, ExpectedMapAttribute.Uint64);
//
// Copy the page entry structure memory for future compare
//
BackupBuffer = AllocateCopyPool (BackupPageTableBufferSize, Buffer);
UT_ASSERT_MEM_EQUAL (Buffer, BackupBuffer, BackupPageTableBufferSize);
//
// Call library to change ReadWrite to 0 for [0,2M]
//
MapAttribute.Bits.ReadWrite = 0;
Status = PageTableMap (&PageTable, PagingMode, NULL, &PageTableBufferSize, 0, SIZE_2MB, &MapAttribute, &MapMask);
UT_ASSERT_EQUAL (Status, RETURN_SUCCESS);
IsPageTableValid (PageTable, PagingMode);
MapCount = 0;
Status = PageTableParse (PageTable, PagingMode, NULL, &MapCount);
UT_ASSERT_EQUAL (Status, RETURN_BUFFER_TOO_SMALL);
Map = AllocatePages (EFI_SIZE_TO_PAGES (MapCount));
Status = PageTableParse (PageTable, PagingMode, Map, &MapCount);
//
// There should be 1 range [0, 2G] with ReadWrite = 0
//
UT_ASSERT_EQUAL (Status, RETURN_SUCCESS);
UT_ASSERT_EQUAL (MapCount, 1);
UT_ASSERT_EQUAL (Map[0].LinearAddress, 0);
UT_ASSERT_EQUAL (Map[0].Length, SIZE_2GB);
ExpectedMapAttribute.Uint64 = MapAttribute.Uint64;
UT_ASSERT_EQUAL (Map[0].Attribute.Uint64, ExpectedMapAttribute.Uint64);
//
// The latest PageTableMap call should change nothing.
// The memory should be identical before and after the funtion is called.
//
UT_ASSERT_MEM_EQUAL (Buffer, BackupBuffer, BackupPageTableBufferSize);
//
// Call library to change ReadWrite to 1 for [0, 2M]
//
MapAttribute.Bits.ReadWrite = 1;
PageTableBufferSize = 0;
Status = PageTableMap (&PageTable, PagingMode, NULL, &PageTableBufferSize, 0, SIZE_2MB, &MapAttribute, &MapMask);
UT_ASSERT_EQUAL (Status, RETURN_SUCCESS);
IsPageTableValid (PageTable, PagingMode);
MapCount = 0;
Status = PageTableParse (PageTable, PagingMode, NULL, &MapCount);
UT_ASSERT_EQUAL (Status, RETURN_BUFFER_TOO_SMALL);
Map = AllocatePages (EFI_SIZE_TO_PAGES (MapCount));
Status = PageTableParse (PageTable, PagingMode, Map, &MapCount);
//
// There should be 2 range [0, 2M] with ReadWrite = 1 and [2M, 2G] with ReadWrite = 0
//
UT_ASSERT_EQUAL (Status, RETURN_SUCCESS);
UT_ASSERT_EQUAL (MapCount, 2);
UT_ASSERT_EQUAL (Map[0].LinearAddress, 0);
UT_ASSERT_EQUAL (Map[0].Length, SIZE_2MB);
ExpectedMapAttribute.Uint64 = MapAttribute.Uint64;
UT_ASSERT_EQUAL (Map[0].Attribute.Uint64, ExpectedMapAttribute.Uint64);
UT_ASSERT_EQUAL (Map[1].LinearAddress, SIZE_2MB);
UT_ASSERT_EQUAL (Map[1].Length, SIZE_2GB - SIZE_2MB);
ExpectedMapAttribute.Uint64 = SIZE_2MB;
ExpectedMapAttribute.Bits.ReadWrite = 0;
ExpectedMapAttribute.Bits.Present = 1;
UT_ASSERT_EQUAL (Map[1].Attribute.Uint64, ExpectedMapAttribute.Uint64);
return UNIT_TEST_PASSED;
}
/**
Check if the needed size is expected
@param[in] Context [Optional] An optional parameter that enables:
1) test-case reuse with varied parameters and
2) test-case re-entry for Target tests that need a
reboot. This parameter is a VOID* and it is the
responsibility of the test author to ensure that the
contents are well understood by all test cases that may
consume it.
@retval UNIT_TEST_PASSED The Unit test has completed and the test
case was successful.
@retval UNIT_TEST_ERROR_TEST_FAILED A test case assertion has failed.
**/
UNIT_TEST_STATUS
EFIAPI
TestCaseManualSizeNotMatch (
IN UNIT_TEST_CONTEXT Context
)
{
UINTN PageTable;
PAGING_MODE PagingMode;
VOID *Buffer;
UINTN PageTableBufferSize;
IA32_MAP_ATTRIBUTE MapAttribute;
IA32_MAP_ATTRIBUTE ExpectedMapAttribute;
IA32_MAP_ATTRIBUTE MapMask;
RETURN_STATUS Status;
IA32_MAP_ENTRY *Map;
UINTN MapCount;
IA32_PAGING_ENTRY *PagingEntry;
PagingMode = Paging4Level;
PageTableBufferSize = 0;
PageTable = 0;
Buffer = NULL;
MapAttribute.Uint64 = 0;
MapMask.Uint64 = MAX_UINT64;
MapAttribute.Bits.Present = 1;
MapAttribute.Bits.ReadWrite = 1;
MapAttribute.Bits.PageTableBaseAddress = (SIZE_2MB - SIZE_4KB) >> 12;
//
// Create Page table to cover [2M-4K, 4M], with ReadWrite = 1
//
Status = PageTableMap (&PageTable, PagingMode, Buffer, &PageTableBufferSize, SIZE_2MB - SIZE_4KB, SIZE_4KB + SIZE_2MB, &MapAttribute, &MapMask);
UT_ASSERT_EQUAL (Status, RETURN_BUFFER_TOO_SMALL);
Buffer = AllocatePages (EFI_SIZE_TO_PAGES (PageTableBufferSize));
Status = PageTableMap (&PageTable, PagingMode, Buffer, &PageTableBufferSize, SIZE_2MB - SIZE_4KB, SIZE_4KB + SIZE_2MB, &MapAttribute, &MapMask);
UT_ASSERT_EQUAL (Status, RETURN_SUCCESS);
IsPageTableValid (PageTable, PagingMode);
MapCount = 0;
Status = PageTableParse (PageTable, PagingMode, NULL, &MapCount);
UT_ASSERT_EQUAL (Status, RETURN_BUFFER_TOO_SMALL);
IsPageTableValid (PageTable, PagingMode);
Map = AllocatePages (EFI_SIZE_TO_PAGES (MapCount));
Status = PageTableParse (PageTable, PagingMode, Map, &MapCount);
UT_ASSERT_EQUAL (Status, RETURN_SUCCESS);
UT_ASSERT_EQUAL (MapCount, 1);
UT_ASSERT_EQUAL (Map[0].LinearAddress, SIZE_2MB - SIZE_4KB);
UT_ASSERT_EQUAL (Map[0].Length, SIZE_4KB + SIZE_2MB);
ExpectedMapAttribute.Uint64 = MapAttribute.Uint64;
UT_ASSERT_EQUAL (Map[0].Attribute.Uint64, ExpectedMapAttribute.Uint64);
//
// Manually change ReadWrite to 0 for 3 level non-leaf entry, which covers [0,2M]
// Then the map is:
// [2M-4K,2M], R/W = 0
// [2M ,4M], R/W = 1
//
PagingEntry = (IA32_PAGING_ENTRY *)(UINTN)PageTable; // Get 4 level entry
PagingEntry = (IA32_PAGING_ENTRY *)(UINTN)(PagingEntry->Pnle.Bits.PageTableBaseAddress << 12); // Get 3 level entry
PagingEntry = (IA32_PAGING_ENTRY *)(UINTN)(PagingEntry->Pnle.Bits.PageTableBaseAddress << 12); // Get 2 level entry
PagingEntry->Uint64 = PagingEntry->Uint64 & (~(UINT64)0x2);
MapCount = 0;
Status = PageTableParse (PageTable, PagingMode, NULL, &MapCount);
UT_ASSERT_EQUAL (Status, RETURN_BUFFER_TOO_SMALL);
Map = AllocatePages (EFI_SIZE_TO_PAGES (MapCount));
Status = PageTableParse (PageTable, PagingMode, Map, &MapCount);
UT_ASSERT_EQUAL (Status, RETURN_SUCCESS);
UT_ASSERT_EQUAL (MapCount, 2);
UT_ASSERT_EQUAL (Map[0].LinearAddress, SIZE_2MB - SIZE_4KB);
UT_ASSERT_EQUAL (Map[0].Length, SIZE_4KB);
ExpectedMapAttribute.Uint64 = MapAttribute.Uint64;
ExpectedMapAttribute.Bits.ReadWrite = 0;
UT_ASSERT_EQUAL (Map[0].Attribute.Uint64, ExpectedMapAttribute.Uint64);
UT_ASSERT_EQUAL (Map[1].LinearAddress, SIZE_2MB);
UT_ASSERT_EQUAL (Map[1].Length, SIZE_2MB);
ExpectedMapAttribute.Uint64 = MapAttribute.Uint64;
ExpectedMapAttribute.Bits.ReadWrite = 1;
ExpectedMapAttribute.Bits.PageTableBaseAddress = SIZE_2MB >> 12;
UT_ASSERT_EQUAL (Map[1].Attribute.Uint64, ExpectedMapAttribute.Uint64);
//
// Set Page table [2M-4K, 2M+4K]'s ReadWrite = 1, [2M,2M+4K]'s ReadWrite is already 1
// Just need to set [2M-4K,2M], won't need extra size, so the status should be success
//
MapAttribute.Bits.Present = 1;
MapAttribute.Bits.ReadWrite = 1;
PageTableBufferSize = 0;
MapAttribute.Bits.PageTableBaseAddress = (SIZE_2MB - SIZE_4KB) >> 12;
Status = PageTableMap (&PageTable, PagingMode, Buffer, &PageTableBufferSize, SIZE_2MB - SIZE_4KB, SIZE_4KB * 2, &MapAttribute, &MapMask);
UT_ASSERT_EQUAL (Status, RETURN_SUCCESS);
return UNIT_TEST_PASSED;
}
/**
Check that won't merge entries
@param[in] Context [Optional] An optional parameter that enables:
1) test-case reuse with varied parameters and
2) test-case re-entry for Target tests that need a
reboot. This parameter is a VOID* and it is the
responsibility of the test author to ensure that the
contents are well understood by all test cases that may
consume it.
@retval UNIT_TEST_PASSED The Unit test has completed and the test
case was successful.
@retval UNIT_TEST_ERROR_TEST_FAILED A test case assertion has failed.
**/
UNIT_TEST_STATUS
EFIAPI
TestCaseManualNotMergeEntry (
IN UNIT_TEST_CONTEXT Context
)
{
UINTN PageTable;
PAGING_MODE PagingMode;
VOID *Buffer;
UINTN PageTableBufferSize;
IA32_MAP_ATTRIBUTE MapAttribute;
IA32_MAP_ATTRIBUTE MapMask;
RETURN_STATUS Status;
UNIT_TEST_STATUS TestStatus;
PagingMode = Paging4Level1GB;
PageTableBufferSize = 0;
PageTable = 0;
Buffer = NULL;
MapAttribute.Uint64 = 0;
MapMask.Uint64 = MAX_UINT64;
MapAttribute.Bits.Present = 1;
MapMask.Bits.Present = 1;
//
// Create Page table to cover [0,4M], and [4M, 1G] is not present
//
Status = PageTableMap (&PageTable, PagingMode, Buffer, &PageTableBufferSize, (UINT64)0, (UINT64)SIZE_2MB * 2, &MapAttribute, &MapMask);
UT_ASSERT_EQUAL (Status, RETURN_BUFFER_TOO_SMALL);
Buffer = AllocatePages (EFI_SIZE_TO_PAGES (PageTableBufferSize));
Status = PageTableMap (&PageTable, PagingMode, Buffer, &PageTableBufferSize, (UINT64)0, (UINT64)SIZE_2MB * 2, &MapAttribute, &MapMask);
UT_ASSERT_EQUAL (Status, RETURN_SUCCESS);
TestStatus = IsPageTableValid (PageTable, PagingMode);
if (TestStatus != UNIT_TEST_PASSED) {
return TestStatus;
}
//
// Let Page table to cover [0,1G], we assume it won't use a big 1G entry to cover whole range
// It looks like the chioce is not bad, but sometime, we need to keep some small entry
//
PageTableBufferSize = 0;
Status = PageTableMap (&PageTable, PagingMode, NULL, &PageTableBufferSize, (UINT64)0, (UINT64)SIZE_1GB, &MapAttribute, &MapMask);
UT_ASSERT_EQUAL (Status, RETURN_SUCCESS);
TestStatus = IsPageTableValid (PageTable, PagingMode);
if (TestStatus != UNIT_TEST_PASSED) {
return TestStatus;
}
MapAttribute.Bits.Accessed = 1;
PageTableBufferSize = 0;
Status = PageTableMap (&PageTable, PagingMode, NULL, &PageTableBufferSize, (UINT64)0, (UINT64)SIZE_2MB, &MapAttribute, &MapMask);
//
// If it didn't use a big 1G entry to cover whole range, only change [0,2M] for some attribute won't need extra memory
//
UT_ASSERT_EQUAL (Status, RETURN_SUCCESS);
return UNIT_TEST_PASSED;
}
/**
Check if the parent entry has different Nx attribute
@param[in] Context [Optional] An optional parameter that enables:
1) test-case reuse with varied parameters and
2) test-case re-entry for Target tests that need a
reboot. This parameter is a VOID* and it is the
responsibility of the test author to ensure that the
contents are well understood by all test cases that may
consume it.
@retval UNIT_TEST_PASSED The Unit test has completed and the test
case was successful.
@retval UNIT_TEST_ERROR_TEST_FAILED A test case assertion has failed.
**/
UNIT_TEST_STATUS
EFIAPI
TestCaseManualChangeNx (
IN UNIT_TEST_CONTEXT Context
)
{
UINTN PageTable;
PAGING_MODE PagingMode;
VOID *Buffer;
UINTN PageTableBufferSize;
IA32_MAP_ATTRIBUTE MapAttribute;
IA32_MAP_ATTRIBUTE ExpectedMapAttribute;
IA32_MAP_ATTRIBUTE MapMask;
RETURN_STATUS Status;
IA32_MAP_ENTRY *Map;
UINTN MapCount;
IA32_PAGING_ENTRY *PagingEntry;
UNIT_TEST_STATUS TestStatus;
PagingMode = Paging4Level1GB;
PageTableBufferSize = 0;
PageTable = 0;
Buffer = NULL;
MapAttribute.Uint64 = 0;
MapMask.Uint64 = MAX_UINT64;
MapAttribute.Bits.Present = 1;
MapAttribute.Bits.Nx = 0;
//
// Create Page table to cover [0,2G], with Nx = 0
//
Status = PageTableMap (&PageTable, PagingMode, Buffer, &PageTableBufferSize, (UINT64)0, (UINT64)SIZE_1GB * 2, &MapAttribute, &MapMask);
UT_ASSERT_EQUAL (Status, RETURN_BUFFER_TOO_SMALL);
Buffer = AllocatePages (EFI_SIZE_TO_PAGES (PageTableBufferSize));
Status = PageTableMap (&PageTable, PagingMode, Buffer, &PageTableBufferSize, (UINT64)0, (UINT64)SIZE_1GB * 2, &MapAttribute, &MapMask);
UT_ASSERT_EQUAL (Status, RETURN_SUCCESS);
TestStatus = IsPageTableValid (PageTable, PagingMode);
if (TestStatus != UNIT_TEST_PASSED) {
return TestStatus;
}
MapCount = 0;
Status = PageTableParse (PageTable, PagingMode, NULL, &MapCount);
UT_ASSERT_EQUAL (Status, RETURN_BUFFER_TOO_SMALL);
TestStatus = IsPageTableValid (PageTable, PagingMode);
if (TestStatus != UNIT_TEST_PASSED) {
return TestStatus;
}
Map = AllocatePages (EFI_SIZE_TO_PAGES (MapCount* sizeof (IA32_MAP_ENTRY)));
Status = PageTableParse (PageTable, PagingMode, Map, &MapCount);
UT_ASSERT_EQUAL (Status, RETURN_SUCCESS);
UT_ASSERT_EQUAL (MapCount, 1);
UT_ASSERT_EQUAL (Map[0].LinearAddress, 0);
UT_ASSERT_EQUAL (Map[0].Length, SIZE_2GB);
ExpectedMapAttribute.Uint64 = MapAttribute.Uint64;
UT_ASSERT_EQUAL (Map[0].Attribute.Uint64, ExpectedMapAttribute.Uint64);
//
// Manually change Nx to 1 for non-leaf entry, which covers [0,2G]
//
PagingEntry = (IA32_PAGING_ENTRY *)(UINTN)PageTable;
PagingEntry->Uint64 = PagingEntry->Uint64 | BIT63;
MapCount = 0;
Status = PageTableParse (PageTable, PagingMode, NULL, &MapCount);
UT_ASSERT_EQUAL (Status, RETURN_BUFFER_TOO_SMALL);
Map = AllocatePages (EFI_SIZE_TO_PAGES (MapCount* sizeof (IA32_MAP_ENTRY)));
Status = PageTableParse (PageTable, PagingMode, Map, &MapCount);
UT_ASSERT_EQUAL (Status, RETURN_SUCCESS);
UT_ASSERT_EQUAL (MapCount, 1);
UT_ASSERT_EQUAL (Map[0].LinearAddress, 0);
UT_ASSERT_EQUAL (Map[0].Length, SIZE_2GB);
ExpectedMapAttribute.Bits.Nx = 1;
UT_ASSERT_EQUAL (Map[0].Attribute.Uint64, ExpectedMapAttribute.Uint64);
//
// Call library to change Nx to 0 for [0,1G]
//
Status = PageTableMap (&PageTable, PagingMode, NULL, &PageTableBufferSize, (UINT64)0, (UINT64)SIZE_1GB, &MapAttribute, &MapMask);
UT_ASSERT_EQUAL (Status, RETURN_SUCCESS);
TestStatus = IsPageTableValid (PageTable, PagingMode);
if (TestStatus != UNIT_TEST_PASSED) {
return TestStatus;
}
MapCount = 0;
Status = PageTableParse (PageTable, PagingMode, NULL, &MapCount);
UT_ASSERT_EQUAL (Status, RETURN_BUFFER_TOO_SMALL);
Map = AllocatePages (EFI_SIZE_TO_PAGES (MapCount* sizeof (IA32_MAP_ENTRY)));
Status = PageTableParse (PageTable, PagingMode, Map, &MapCount);
UT_ASSERT_EQUAL (Status, RETURN_SUCCESS);
//
// There should be two ranges [0, 1G] with Nx = 0 and [1G, 2G] with Nx = 1
//
UT_ASSERT_EQUAL (MapCount, 2);
UT_ASSERT_EQUAL (Map[0].LinearAddress, 0);
UT_ASSERT_EQUAL (Map[0].Length, SIZE_1GB);
ExpectedMapAttribute.Uint64 = MapAttribute.Uint64;
UT_ASSERT_EQUAL (Map[0].Attribute.Uint64, ExpectedMapAttribute.Uint64);
UT_ASSERT_EQUAL (Map[1].LinearAddress, SIZE_1GB);
UT_ASSERT_EQUAL (Map[1].Length, SIZE_1GB);
ExpectedMapAttribute.Uint64 = SIZE_1GB;
ExpectedMapAttribute.Bits.Present = 1;
ExpectedMapAttribute.Bits.Nx = 1;
UT_ASSERT_EQUAL (Map[1].Attribute.Uint64, ExpectedMapAttribute.Uint64);
return UNIT_TEST_PASSED;
}
/**
Initialize the unit test framework, suite, and unit tests for the
sample unit tests and run the unit tests.
@retval EFI_SUCCESS All test cases were dispatched.
@retval EFI_OUT_OF_RESOURCES There are not enough resources available to
initialize the unit tests.
**/
EFI_STATUS
EFIAPI
UefiTestMain (
VOID
)
{
EFI_STATUS Status;
UNIT_TEST_FRAMEWORK_HANDLE Framework;
UNIT_TEST_SUITE_HANDLE ManualTestCase;
UNIT_TEST_SUITE_HANDLE RandomTestCase;
Framework = NULL;
DEBUG ((DEBUG_INFO, "%a v%a\n", UNIT_TEST_APP_NAME, UNIT_TEST_APP_VERSION));
//
// Start setting up the test framework for running the tests.
//
Status = InitUnitTestFramework (&Framework, UNIT_TEST_APP_NAME, gEfiCallerBaseName, UNIT_TEST_APP_VERSION);
if (EFI_ERROR (Status)) {
DEBUG ((DEBUG_ERROR, "Failed in InitUnitTestFramework. Status = %r\n", Status));
goto EXIT;
}
//
// Populate the Manual Test Cases.
//
Status = CreateUnitTestSuite (&ManualTestCase, Framework, "Manual Test Cases", "CpuPageTableLib.Manual", NULL, NULL);
if (EFI_ERROR (Status)) {
DEBUG ((DEBUG_ERROR, "Failed in CreateUnitTestSuite for Manual Test Cases\n"));
Status = EFI_OUT_OF_RESOURCES;
goto EXIT;
}
AddTestCase (ManualTestCase, "Check if the input parameters are not supported.", "Manual Test Case1", TestCaseForParameter, NULL, NULL, NULL);
AddTestCase (ManualTestCase, "Check the case that modifying page table doesn't need extra buffer", "Manual Test Case2", TestCaseWhichNoNeedExtraSize, NULL, NULL, NULL);
AddTestCase (ManualTestCase, "Check the case to map [0, 1G] to [8K, 1G+8K]", "Manual Test Case3", TestCase1Gmapto4K, NULL, NULL, NULL);
AddTestCase (ManualTestCase, "Check won't merge entries", "Manual Test Case4", TestCaseManualNotMergeEntry, NULL, NULL, NULL);
AddTestCase (ManualTestCase, "Check if the parent entry has different ReadWrite attribute", "Manual Test Case5", TestCaseManualChangeReadWrite, NULL, NULL, NULL);
AddTestCase (ManualTestCase, "Check if the parent entry has different Nx attribute", "Manual Test Case6", TestCaseManualChangeNx, NULL, NULL, NULL);
AddTestCase (ManualTestCase, "Check if the needed size is expected", "Manual Test Case7", TestCaseManualSizeNotMatch, NULL, NULL, NULL);
//
// Populate the Random Test Cases.
//
Status = CreateUnitTestSuite (&RandomTestCase, Framework, "Random Test Cases", "CpuPageTableLib.Random", NULL, NULL);
if (EFI_ERROR (Status)) {
DEBUG ((DEBUG_ERROR, "Failed in CreateUnitTestSuite for Random Test Cases\n"));
Status = EFI_OUT_OF_RESOURCES;
goto EXIT;
}
AddTestCase (RandomTestCase, "Random Test for Paging4Level", "Random Test Case1", TestCaseforRandomTest, NULL, NULL, &mTestContextPaging4Level);
AddTestCase (RandomTestCase, "Random Test for Paging4Level1G", "Random Test Case2", TestCaseforRandomTest, NULL, NULL, &mTestContextPaging4Level1GB);
AddTestCase (RandomTestCase, "Random Test for Paging5Level", "Random Test Case3", TestCaseforRandomTest, NULL, NULL, &mTestContextPaging5Level);
AddTestCase (RandomTestCase, "Random Test for Paging5Level1G", "Random Test Case4", TestCaseforRandomTest, NULL, NULL, &mTestContextPaging5Level1GB);
//
// Execute the tests.
//
Status = RunAllTestSuites (Framework);
EXIT:
if (Framework) {
FreeUnitTestFramework (Framework);
}
return Status;
}
/**
Standard POSIX C entry point for host based unit test execution.
@param Argc Number of arguments.
@param Argv Array of arguments.
@return Test application exit code.
**/
INT32
main (
INT32 Argc,
CHAR8 *Argv[]
)
{
InitGlobalData (52);
return UefiTestMain ();
}
|