/
Pte.h
1561 lines (1345 loc) · 58.9 KB
/
Pte.h
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
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#pragma once
/*
Tables:
PML5 = Page-Map Level-5.
PML4 = Page-Map Level-4.
PDP = Page Directory Pointer.
PD = Page Directory.
PT = Page Table.
PML5E/PML4E/PDPE/PDE/PTE = Entries in the corresponding tables.
PFN = Page Frame Number (number of a physical page).
PageSize = Size of a page depending on CR4.PSE, PDPE.PS and PDE.PS bits (4Kb, 2Mb, 4Mb or 1Gb).
Each entry contains a 4Kb-based physical PFN of a next level table
except the last translation level that contains a PFN of a physical page.
Physical address of the first level translation table = [CR3.PML5/CR3.PML4/CR3.PDP/CR3.PD depending on the current processor mode] * 4Kb
Physical address of the next translation level table = [Current level table entry].PFN * 4Kb
Physical address translated from a given linear address = [The last table entry].PFN * PageSize + LinearAddress.Offset
FirstLevelTable = CR3.PFN * 4Kb
NextLevelTable = CurrentLevelTableEntry.PFN * 4Kb
PhysAddress = LastLevelTableEntry.PFN * PageSize + LinearAddress.Offset
CPU mode is specified by the EFER.LME and the EFER.LMA bits (Long Mode Enabled and Long Mode Active).
+--------+---------+---------+----------+---------+--------+-----------+------------+----------------------------------------------+
| Mode | CR4.PAE | CR4.PSE | CR4.LA57 | PDPE.PS | PDE.PS | Page size | MAXPHYADDR | Translation chain |
+--------+---------+---------+----------+---------+--------+-----------+------------+----------------------------------------------+
| i386 | 0 | 0 | N/A | N/A | 0 | 4Kb | 32 bit | PDE -> PTE -> Phys |
+--------+---------+---------+----------+---------+--------+-----------+------------+----------------------------------------------+
| i386 | 0 | 1 | N/A | N/A | 0 | 4Kb | 32 bit | PDE -> PTE -> Phys |
+--------+---------+---------+----------+---------+--------+-----------+------------+----------------------------------------------+
| i386 | 0 | 1 | N/A | N/A | 1 | 4Mb | 40 bit | PDE -> Phys |
+--------+---------+---------+----------+---------+--------+-----------+------------+----------------------------------------------+
| i386 | 1 | Ignored | N/A | N/A | 0 | 4Kb | 52 bit | PDPE -> PDE -> PTE -> Phys |
+--------+---------+---------+----------+---------+--------+-----------+------------+----------------------------------------------+
| i386 | 1 | Ignored | N/A | N/A | 1 | 2Mb | 52 bit | PDPE -> PDE -> Phys |
+--------+---------+---------+----------+---------+--------+-----------+------------+----------------------------------------------+
| amd64 | Always1 | Ignored | 0 | 0 | 0 | 4Kb | 52 bit | PML4E -> PDPE -> PDE -> PTE -> Phys |
+--------+---------+---------+----------+---------+--------+-----------+------------+----------------------------------------------+
| amd64 | Always1 | Ignored | 0 | 0 | 1 | 2Mb | 52 bit | PML4E -> PDPE -> PDE -> Phys |
+--------+---------+---------+----------+---------+--------+-----------+------------+----------------------------------------------+
| amd64 | Always1 | Ignored | 0 | 1 | N/A | 1Gb | 52 bit | PML4E -> PDPE -> Phys |
+--------+---------+---------+----------+---------+--------+-----------+------------+----------------------------------------------+
| amd64 | Always1 | Ignored | 1 | 0 | 0 | 4Kb | 52 bit | PML5E -> PML4E -> PDPE -> PDE -> PTE -> Phys |
+--------+---------+---------+----------+---------+--------+-----------+------------+----------------------------------------------+
| amd64 | Always1 | Ignored | 1 | 0 | 1 | 2Mb | 52 bit | PML5E -> PML4E -> PDPE -> PDE -> Phys |
+--------+---------+---------+----------+---------+--------+-----------+------------+----------------------------------------------+
| amd64 | Always1 | Ignored | 1 | 1 | N/A | 1Gb | 52 bit | PML5E -> PML4E -> PDPE -> Phys |
+--------+---------+---------+----------+---------+--------+-----------+------------+----------------------------------------------+
Linear address is interpreted differently depending on a CPU mode specified by EFER.LMA and CR4.[PAE/PSE/LA57]
and depending on a PageSize bit in PDPE/PDE entries belonging to the linear address.
The exact form of a linear address determines size of page tables.
Pseudocode describing translation principles:
if (EFER.LME && EFER.LMA) // Is long mode enabled and active?
{
// It's a long mode (AMD64):
PML4 = {}; // Depends on paging type (4-Level or 5-Level)
if (CR4.LA57) // Is 5-level paging enabled?
{
// 5-level paging:
PML5 = CR3.PML5_PFN * 4Kb;
PML5E = PML5[LinearAddress.PML5_INDEX];
PML4 = PML5E.PML4_PFN * 4Kb;
}
else
{
// 4-level paging:
PML4 = CR3.PML4_PFN * 4Kb;
}
PML4E = PML4[LinearAddress.PML4_INDEX];
PDP = PML4E.PDP_PFN * 4Kb;
PDPE = PDP[LinearAddress.PDP_INDEX]
if (PDPE.PS)
{
// PML4E -> PDPE -> PA
PageSize = 1 Gbyte;
PhysAddr = PDPE.PFN * PageSize + LinearAddress.OffsetInPage;
}
else
{
PD = PDPE.PD_PFN * 4Kb;
PDE = PD[LinearAddress.PD_INDEX];
if (PDE.PS)
{
// PML4E -> PDPE -> PDE -> PA
PageSize = 2 Mbyte;
PhysAddr = PDE.PFN * PageSize + LinearAddress.OffsetInPage;
}
else
{
// PML4E -> PDPE -> PDE -> PTE -> PA
PT = PDE.PT_PFN * 4Kb;
PTE = PT[LinearAddress.PT_INDEX];
PageSize = 4 Kbyte;
PhysAddr = PTE.PFN * PageSize + LinearAddress.OffsetInPage;
}
}
}
else
{
// It's a legacy mode (i386):
if (CR4.PAE)
{
// CR4.PSE is ignored in PAE mode.
PDP = CR3.PDP_PFN * 4Kb;
PDPE = PDP[LinearAddress.PDP_INDEX];
PD = PDPE.PD_PFN * 4Kb;
PDE = PD[LinearAddress.PD_INDEX];
if (PDE.PS)
{
// PDPE -> PDE -> PA
PageSize = 2 Mbyte;
PhysAddr = PDE.PFN * PageSize + LinearAddress.OffsetInPage;
}
else
{
// PDPE -> PDE -> PTE -> PA
PT = PDE.PT_PFN * 4Kb;
PTE = PT[LinearAddress.PT_INDEX];
PageSize = 4 Kbyte;
PhysAddr = PTE.PFN * PageSize + LinearAddress.OffsetInPage;
}
}
else
{
// Non-PAE mode:
if (CR4.PSE) // Whether Page Size Extensions are enabled?
{
PD = CR3.PD_PFN * 4Kb;
PDE = PD[LinearAddress.PD_INDEX];
if (PDE.PS)
{
// PDE -> PA
PageSize = 4 Mbyte;
PhysAddr = PDE.PFN * PageSize + LinerAddress.OffsetInPage;
}
else
{
// PDE -> PTE -> PA
PT = PDE.PT_PFN * 4Kb;
PTE = PT[LinearAddress.PT_INDEX];
PageSize = 4 Kbyte;
PhysAddr = PTE.PFN * PageSize + LinearAddress.OffsetInPage;
}
}
else
{
// PDE -> PTE -> PA
// PDE.PS is ignored if Page Size Extentsions are disabled.
PD = CR3.PD_PFN * 4Kb;
PDE = PD[LinearAddress.PD_INDEX];
PT = PTE.PT_PFN * 4Kb;
PTE = PT[LinearAddress.PT_INDEX];
PageSize = 4 Kbyte;
PhysAddr = PTE.PFN * PageSize + LinearAddress.OffsetInPage;
}
}
}
Legacy mode, 4Kb pages:
EFER.LMA == 0
CR4.PAE == 0
CR4.PSE == 0
Linear address:
+----------+----------+----------------+
| PD Index | PT Index | Offset in page |
+--+-------+-----+----+---------+------+
| | |
| PD Table | |
CR3 -----> +-----+ | |
| | PDE | | |
| +-----+ | +--------------------+
| | PDE | | |
| +-----+ | Page Table |
+--> | PDE | ---> +-----+ |
+-----+ | | PTE | |
| | | +-----+ V
... ... +-> | PTE | -> (PFN * PageSize) + Offset = PhysicalAddress
+-----+ [4Kb]
| |
... ...
Legacy mode, PSE, 4Kb pages:
EFER.LMA == 0
CR4.PAE == 0
CR4.PSE == 1
PDE.PS == 0
Linear address:
+----------+----------+----------------+
| PD Index | PT Index | Offset in page |
+--+-------+-----+----+---------+------+
| | |
| PD Table | |
CR3 -----> +-----+ | |
| | PDE | | |
| +-----+ | +--------------------+
| | PDE | | |
| +-----+ | Page Table |
+--> | PDE | ---> +-----+ |
/ +-----+ | | PTE | |
/ | | | +-----+ V
/ ... ... +-> | PTE | -> (PFN * PageSize) + Offset = PhysicalAddress
/ +-----+ [4Kb]
PDE.PS == 0 | |
... ...
Legacy mode, PSE, large (4Mb) pages:
EFER.LMA == 0
CR4.PAE == 0
CR4.PSE == 1
PDE.PS == 1
Linear address:
+----------+---------------------------+
| PD Index | Offset in page |
+--+-------+--------------------+------+
| |
| PD Table |
CR3 -----> +-----+ |
| | PDE | |
| +-----+ +-------+
| | PDE | |
| +-----+ V
+--> | PDE | -> (PFN * PageSize) + Offset = PhysicalAddress
/ +-----+ [4Mb]
/ | |
/ ... ...
/
PDE.PS == 1
Legacy mode, PAE, 4Kb pages:
EFER.LMA == 0
CR4.PAE == 1
PDE.PS == 0
Linear address:
+------------+----------+----------+----------------+
| PDPE Index | PD Index | PT Index | Offset in page |
+-+----------+----+-----+------+---+----------+-----+
| | | |
| PDP Table | PD Table | |
CR3 ---> +------+ +---> +-----+ | |
| | PDPE | | | | PDE | | |
| +------+ | | +-----+ | +--------------------+
+-> | PDPE | -+ | | PDE | | |
+------+ | +-----+ | Page Table |
| | +-> | PDE | ---> +-----+ |
... ... / +-----+ | | PTE | |
/ | | | +-----+ V
/ ... ... +-> | PTE | -> (PFN * PageSize) + Offset = PhysicalAddress
/ +-----+ [4Kb]
PDE.PS == 0 | |
... ...
Legacy mode, PAE, large (2Mb) pages:
EFER.LMA == 0
CR4.PAE == 1
PDE.PS == 1
Linear address:
+------------+----------+---------------------------+
| PDPE Index | PD Index | Offset in page |
+-+----------+----+-----+---------------------+-----+
| | |
| PDP Table | PD Table |
CR3 ---> +------+ +---> +-----+ |
| | PDPE | | | | PDE | +-------+
| +------+ | | +-----+ |
+-> | PDPE | -+ | | PDE | |
+------+ | +-----+ V
| | +-> | PDE | -> (PFN * PageSize) + Offset = PhysicalAddress
... ... / +-----+
/ | |
/ ... ...
/
PDE.PS == 1
Long mode, 4-Level paging, 4Kb pages:
EFER.LMA == 1
CR4.LA57 == 0
PDPE.PS == 0
PDE.PS == 0
Linear address:
+------------+-----------+----------+----------+----------------+
| PML4 Index | PDP Index | PD Index | PT Index | Offset in page |
+-+----------+-----+-----+-------+--+-----+----+---------+------+
| | | | |
| PML4 Table | | | +-------+
CR3 -----> +-------+ | | | |
| | PML4E | | | +---+ Page Table |
| +-------+ | PDP Table | | +--> +-----+ |
+---> | PML4E | ---> +------+ | | | | PTE | |
+-------+ | | PDPE | | | | +-----+ +--------------------+
| PML4E | | +------+ | PD Table | | | PTE | |
+-------+ +-> | PDPE | ---> +-----+ | | +-----+ V
| | / +------+ | | PDE | +--|--> | PTE | -> (PFN * PageSize) + Offset = Physical Address
... ... / | | | +-----+ | +-----+ [4Kb]
/ ... ... | | PDE | | | |
/ | +-----+ | ... ...
PDPE.PS == 0 +-> | PDE | ----+
/ +-----+
/ | |
/ ... ...
/
PDE.PS == 0
Long mode, 4-Level paging, large (2Mb) pages:
EFER.LMA == 1
CR4.LA57 == 0
PDPE.PS == 0
PDE.PS == 1
Linear address:
+------------+-----------+----------+------------------------+
| PML4 Index | PDP Index | PD Index | Offset in page |
+-+----------+-----+-----+-------+--+---------------------+--+
| | | |
| PML4 Table | | |
CR3 -----> +-------+ | | |
| | PML4E | | | |
| +-------+ | PDP Table | |
+---> | PML4E | ---> +------+ | |
+-------+ | | PDPE | | +----------+
| PML4E | | +------+ | PD Table |
+-------+ +-> | PDPE | ---> +-----+ |
| | / +------+ | | PDE | |
... ... / | | | +-----+ |
/ ... ... | | PDE | |
/ | +-----+ V
/ +-> | PDE | -> (PFN * PageSize) + Offset = Physical Address
PDPE.PS == 0 / +-----+ [2Mb]
/ | |
/ ... ...
/
PDE.PS == 1
Long mode, 4-Level paging, huge (1Gb) pages:
EFER.LMA == 1
CR4.LA57 == 0
PDPE.PS == 1
Linear address:
+------------+-----------+-----------------------------------+
| PML4 Index | PDP Index | Offset in page |
+-+----------+-----+-----+------------------------------+----+
| | |
| PML4 Table | |
CR3 -----> +-------+ | |
| | PML4E | | |
| +-------+ | PDP Table |
+---> | PML4E | ---> +------+ |
+-------+ | | PDPE | |
| PML4E | | +------+ V
+-------+ +-> | PDPE | -> (PFN * PageSize) + Offset = Physical Address
| | / +------+ [1Gb]
... ... / | |
/ ... ...
/
PDPE.PS == 1
Long mode, 5-Level paging:
EFER.LMA == 1
CR4.LA57 == 1
PDPE.PS == The same variants as in 4-Level paging
PDE.PS == The same variants as in 4-Level paging
Linear address:
+------------+------------+-----------+----------+----------+----------------+
| PML5 Index | PML4 Index | PDP Index | PD Index | PT Index | Offset in page |
+------------+-----+------+--------+--+--------+-+--------+-+-----------+----+
| | | | | |
| PML5 Table | ... ... ... ...
CR3 -----> +-------+ |
| | PML5E | |
| +-------+ | PML4 Table
+---> | PML5E | ---> +-------+
+-------+ | | PML4E |
| PML5E | | +-------+
+-------+ +-> | PML4E | -> ...The same variants as in 4-Level paging...
| | +-------+
... ... | |
... ...
*/
#pragma pack(push, 1)
namespace Pte
{
//
// 5-Level paging is available starting with Intel Ice Lake (10th Gen) and AMD EPYC 7xxx "Genoa" (Zen 4).
//
enum class Mode
{
legacyNonPae, // i386 (EFER.LMA == 0, CR4.PAE == 0, CR4.PSE is meaningful)
legacyPae, // i386 with Physical Address Extension (EFER.LMA == 0, CR4.PAE == 1, CR4.PSE is ignored)
longMode4Level, // amd64 with 4-Level paging (EFER.LMA == 1, CR4.PAE is always 1, CR4.LA57 = 0, CR4.PSE is ignored)
longMode5Level, // amd64 with 5-Level paging (EFER.LMA == 1, CR4.PAE is always 1, CR4.LA57 = 1, CR4.PSE is ignored)
};
enum class PageSize
{
generic,
nonPse,
pse
};
struct PhysicalAddress
{
unsigned long long physicalAddress{};
};
template <typename Type>
struct PhysicalLayout : public PhysicalAddress
{
using Layout = Type;
};
template <Mode mode>
union LinearAddress;
template <>
union LinearAddress<Mode::legacyNonPae> // CR4.PAE == 0
{
unsigned int raw{};
struct
{
unsigned int : 22; // Depends on page size
unsigned int PageDirectoryOffset : 10;
} generic;
union
{
struct
{
unsigned int PageOffset : 12; // Offset into the physical page
unsigned int PageTableOffset : 10; // Index into the 1024-entry page-table
unsigned int PageDirectoryOffset : 10; // Index into the 1024-entry Page-Directory Table
} page4Kb;
} pdeNonPageSize; // PDE.PS == 0
union
{
struct
{
unsigned int PageOffset : 22; // Offset into the physical page
unsigned int PageDirectoryOffset : 10; // Index into the 1024-entry Page-Directory Table
} page4Mb;
} pdePageSize; // PDE.PS == 1
};
static_assert(sizeof(LinearAddress<Mode::legacyNonPae>) == sizeof(unsigned int));
template <>
union LinearAddress<Mode::legacyPae> // CR4.PAE == 1
{
unsigned int raw{};
struct
{
unsigned int : 21; // Depends on page size
unsigned int PageDirectoryOffset : 9;
unsigned int PageDirectoryPointerOffset : 2;
} generic;
union
{
struct
{
unsigned int PageOffset : 12; // Byte offset into the physical page
unsigned int PageTableOffset : 9; // Index into the 512-entry Page Table
unsigned int PageDirectoryOffset : 9; // Index into the 512-entry Page-Directory Table
unsigned int PageDirectoryPointerOffset : 2; // Index into a 4-entry Page-Directory Pointer table
} page4Kb;
} pdeNonPageSize; // PDE.PS == 0
union
{
struct
{
unsigned int PageOffset : 21; // Byte offset into the physical page
unsigned int PageDirectoryOffset : 9; // Index into the 512-entry Page-Directory Table
unsigned int PageDirectoryPointerOffset : 2; // Index into a 4-entry Page-Directory Pointer table
} page2Mb;
} pdePageSize; // PDE.PS == 1
};
static_assert(sizeof(LinearAddress<Mode::legacyPae>) == sizeof(unsigned int));
template <>
union LinearAddress<Mode::longMode4Level>
{
unsigned long long raw{};
struct
{
unsigned long long : 30; // Depends on page size
unsigned long long PageDirectoryPointerOffset : 9;
unsigned long long PageMapLevel4Offset : 9;
unsigned long long SignExtend : 16;
} generic;
union
{
struct
{
unsigned long long : 21; // Depends on page size
unsigned long long PageDirectoryOffset : 9;
unsigned long long PageDirectoryPointerOffset : 9;
unsigned long long PageMapLevel4Offset : 9;
unsigned long long SignExtend : 16;
} generic; // To determine whether the page size is 4Kb or 2Mb
union
{
struct
{
unsigned long long PageOffset : 12; // Byte offset into the physical page
unsigned long long PageTableOffset : 9; // Index into the 512-entry Page Table
unsigned long long PageDirectoryOffset : 9; // Index into the 512-entry Page-Directory Table
unsigned long long PageDirectoryPointerOffset : 9; // Index into the 512-entry Page-Directory Pointer table
unsigned long long PageMapLevel4Offset : 9; // Index into the 512-entry Page-Map Level-4 table
unsigned long long SignExtend : 16;
} page4Kb;
} pdeNonPageSize; // PDE.PS == 0
union
{
struct
{
unsigned long long PageOffset : 21; // Byte offset into the physical page
unsigned long long PageDirectoryOffset : 9; // Index into the 512-entry Page-Directory Table
unsigned long long PageDirectoryPointerOffset : 9; // Index into the 512-entry Page-Directory Pointer table
unsigned long long PageMapLevel4Offset : 9; // Index into the 512-entry Page-Map Level-4 table
unsigned long long SignExtend : 16;
} page2Mb;
} pdePageSize; // PDE.PS == 1
} pdpeNonPageSize; // PDPE.PS == 0
union
{
struct
{
unsigned long long PageOffset : 30; // Byte offset into the physical page
unsigned long long PageDirectoryPointerOffset : 9; // Index into the 512-entry Page-Directory Pointer table
unsigned long long PageMapLevel4Offset : 9; // Index into the 512-entry Page-Map Level-4 table
unsigned long long SignExtend : 16;
} page1Gb;
} pdpePageSize; // PDPE.PS == 1
};
static_assert(sizeof(LinearAddress<Mode::longMode4Level>) == sizeof(unsigned long long));
template <>
union LinearAddress<Mode::longMode5Level>
{
unsigned long long raw{};
struct
{
unsigned long long : 30; // Depends on page size
unsigned long long PageDirectoryPointerOffset : 9; // Index into the 512-entry Page-Directory Pointer table
unsigned long long PageMapLevel4Offset : 9; // Index into the 512-entry Page-Map Level-4 table
unsigned long long PageMapLevel5Offset : 9; // Index into the 512-entry Page-Map Level-5 table
unsigned long long SignExtend : 7;
} generic;
union
{
struct
{
unsigned long long : 21; // Depends on page size
unsigned long long PageDirectoryOffset : 9;
unsigned long long PageDirectoryPointerOffset : 9;
unsigned long long PageMapLevel4Offset : 9;
unsigned long long PageMapLevel5Offset : 9;
unsigned long long SignExtend : 7;
} generic; // To determine whether the page size is 4Kb or 2Mb
union
{
struct
{
unsigned long long PageOffset : 12; // Byte offset into the physical page
unsigned long long PageTableOffset : 9; // Index into the 512-entry Page Table
unsigned long long PageDirectoryOffset : 9; // Index into the 512-entry Page-Directory Table
unsigned long long PageDirectoryPointerOffset : 9; // Index into the 512-entry Page-Directory Pointer table
unsigned long long PageMapLevel4Offset : 9; // Index into the 512-entry Page-Map Level-4 table
unsigned long long PageMapLevel5Offset : 9; // Index into the 512-entry Page-Map Level-5 table
unsigned long long SignExtend : 7;
} page4Kb;
} pdeNonPageSize; // PDE.PS == 0
union
{
struct
{
unsigned long long PageOffset : 21; // Byte offset into the physical page
unsigned long long PageDirectoryOffset : 9; // Index into the 512-entry Page-Directory Table
unsigned long long PageDirectoryPointerOffset : 9; // Index into the 512-entry Page-Directory Pointer table
unsigned long long PageMapLevel4Offset : 9; // Index into the 512-entry Page-Map Level-4 table
unsigned long long PageMapLevel5Offset : 9; // Index into the 512-entry Page-Map Level-5 table
unsigned long long SignExtend : 7;
} page2Mb;
} pdePageSize; // PDE.PS == 1
} pdpeNonPageSize; // PDPE.PS == 0
union
{
struct
{
unsigned long long PageOffset : 30; // Byte offset into the physical page
unsigned long long PageDirectoryPointerOffset : 9; // Index into the 512-entry Page-Directory Pointer table
unsigned long long PageMapLevel4Offset : 9; // Index into the 512-entry Page-Map Level-4 table
unsigned long long PageMapLevel5Offset : 9; // Index into the 512-entry Page-Map Level-5 table
unsigned long long SignExtend : 7;
} page1Gb;
} pdpePageSize; // PDPE.PS == 1
};
static_assert(sizeof(LinearAddress<Mode::longMode5Level>) == sizeof(unsigned long long));
// Page frame number:
template <Mode mode>
struct Pfn;
template <>
struct Pfn<Mode::legacyNonPae>
{
static constexpr unsigned int toPage(const unsigned int pfn) noexcept
{
return pfn << 12u; // 1 << 12 == 4096 (0x1000, 4Kb)
}
static constexpr unsigned int toLarge(const unsigned int pfn) noexcept
{
return pfn << 22u; // 1 << 22 == 4'194'304 (0x400'000, 4Mb)
}
};
template <>
struct Pfn<Mode::legacyPae>
{
static constexpr unsigned int toPage(const unsigned int pfn) noexcept
{
return pfn << 12u; // 1 << 12 == 4096 (0x1000, 4Kb)
}
static constexpr unsigned int toLarge(const unsigned int pfn) noexcept
{
return pfn << 21u; // 1 << 21 == 2'097'152 (0x200'000, 2Mb)
}
};
template <>
struct Pfn<Mode::longMode4Level>
{
static constexpr unsigned long long toPage(const unsigned long long pfn) noexcept
{
return pfn << 12ull; // 1 << 12 == 4096 (0x1000, 4Kb)
}
static constexpr unsigned long long toLarge(const unsigned long long pfn) noexcept
{
return pfn << 21ull; // 1 << 21 == 2'097'152 (0x20'0000, 2Mb)
}
static constexpr unsigned long long toHuge(const unsigned long long pfn) noexcept
{
return pfn << 30ull; // 1 << 30 == 1'073'741'824 (0x4000'0000, 1Gb)
}
};
template <>
struct Pfn<Mode::longMode5Level>
{
static constexpr unsigned long long toPage(const unsigned long long pfn) noexcept
{
return pfn << 12ull; // 1 << 12 == 4096 (0x1000, 4Kb)
}
static constexpr unsigned long long toLarge(const unsigned long long pfn) noexcept
{
return pfn << 21ull; // 1 << 21 == 2'097'152 (0x20'0000, 2Mb)
}
static constexpr unsigned long long toHuge(const unsigned long long pfn) noexcept
{
return pfn << 30ull; // 1 << 30 == 1'073'741'824 (0x4000'0000, 1Gb)
}
};
template <Mode mode>
struct Tables;
template <>
struct Tables<Mode::legacyNonPae>
{
static constexpr Mode k_mode = Mode::legacyNonPae;
static constexpr auto k_entrySize = sizeof(unsigned int);
using LinearAddress = LinearAddress<k_mode>;
using Pfn = Pfn<k_mode>;
template <PageSize pageSize>
union Pde;
//
// Describes 4Mb in 1024x4Kb pages: PDE(PS=0) -> PTE -> PA (4Kb)
//
template <>
union Pde<PageSize::nonPse>
{
//
// Describes 4Kb page: PDE -> PTE -> PA (4Kb)
//
union Pte
{
unsigned int raw;
struct
{
unsigned int P : 1; // Present
unsigned int RW : 1; // Read/Write
unsigned int US : 1; // User/Supervisor
unsigned int PWT : 1; // Page-Level Writethrough
unsigned int PCD : 1; // Page-Level Cache Disable
unsigned int A : 1; // Accessed
unsigned int D : 1; // Dirty
unsigned int PAT : 1; // Page-Attribute Table
unsigned int G : 1; // Global Page
unsigned int AVL : 3; // Available to software
unsigned int PhysicalPageFrameNumber : 20;
} page4Kb;
constexpr PhysicalAddress physicalPageBase() const noexcept
{
return PhysicalAddress{ Pfn::toPage(this->page4Kb.PhysicalPageFrameNumber) };
}
constexpr PhysicalAddress physicalAddress(const LinearAddress& linear) const noexcept
{
return PhysicalAddress{ this->physicalPageBase().physicalAddress + linear.pdeNonPageSize.page4Kb.PageOffset };
}
};
//
// Describes 4Mb of linear address space.
//
using Pt = Pte[1024];
unsigned int raw{};
struct
{
unsigned int P : 1; // Present
unsigned int RW : 1; // Read/Write
unsigned int US : 1; // User/Supervisor
unsigned int PWT : 1; // Page-Level Writethrough
unsigned int PCD : 1; // Page-Level Cache Disable
unsigned int A : 1; // Accessed
unsigned int Ignored0 : 1;
unsigned int PS : 1; // PageSize == 0
unsigned int Ignored1 : 1;
unsigned int AVL : 3; // Available to software
unsigned int PT : 20; // Page frame number
} layout;
constexpr PhysicalLayout<Pt> pt() const noexcept
{
return PhysicalLayout<Pt>{ Pfn::toPage(this->layout.PT) };
}
constexpr PhysicalLayout<Pte> pte(const LinearAddress& linear) const noexcept
{
return PhysicalLayout<Pte>{ this->pt().physicalAddress + linear.pdeNonPageSize.page4Kb.PageTableOffset * k_entrySize };
}
};
static_assert(sizeof(Pde<PageSize::nonPse>) == sizeof(unsigned int));
//
// Describes 4Mb page itself: PDE(PS=1) -> PA (4Mb)
//
template <>
union Pde<PageSize::pse>
{
unsigned int raw;
struct
{
unsigned int P : 1; // Present
unsigned int RW : 1; // Read/Write
unsigned int US : 1; // User/Supervisor
unsigned int PWT : 1; // Page-Level Writethrough
unsigned int PCD : 1; // Page-Level Cache Disable
unsigned int A : 1; // Accessed
unsigned int D : 1; // Dirty
unsigned int PS : 1; // PageSize == 1
unsigned int G : 1; // Global Page
unsigned int AVL : 3; // Available to software
unsigned int PAT : 1; // Page-Attribute Table
unsigned int PhysicalPageFrameNumberHigh : 8;
unsigned int : 1;
unsigned int PhysicalPageFrameNumberLow : 10;
} page4Mb;
constexpr PhysicalAddress physicalPageBase() const noexcept
{
return PhysicalAddress{ Pfn::toLarge((this->page4Mb.PhysicalPageFrameNumberHigh << 10) | this->page4Mb.PhysicalPageFrameNumberLow) };
}
constexpr PhysicalAddress physicalAddress(const LinearAddress& linear) const noexcept
{
return PhysicalAddress{ this->physicalPageBase().physicalAddress + linear.pdePageSize.page4Mb.PageOffset };
}
};
static_assert(sizeof(Pde<PageSize::pse>) == sizeof(unsigned int));
//
// Describes 4 Mb in the following ways:
// 4Mb page itself | PDE(PS=1) -> PA (4Mb)
// 1024x4Kb pages | PDE(PS=0) -> PTE -> PA (4Kb)
//
template <>
union Pde<PageSize::generic>
{
unsigned int raw;
struct
{
unsigned int P : 1; // Present
unsigned int RW : 1; // Read/Write
unsigned int US : 1; // User/Supervisor
unsigned int PWT : 1; // Page-Level Writethrough
unsigned int PCD : 1; // Page-Level Cache Disable
unsigned int A : 1; // Accessed
unsigned int : 1;
unsigned int PS : 1; // PageSize bit
unsigned int : 1;
unsigned int AVL : 3; // Available to software
unsigned int : 20;
} layout;
Pde<PageSize::nonPse> nonPse;
Pde<PageSize::pse> pse;
constexpr PageSize pageSize() const noexcept
{
return this->layout.PS
? PageSize::pse
: PageSize::nonPse;
}
};
static_assert(sizeof(Pde<PageSize::generic>) == sizeof(unsigned int));
using PdeGeneric = Pde<PageSize::generic>;
using PdeNonPse = Pde<PageSize::nonPse>;
using PdePse = Pde<PageSize::pse>;
//
// Describes 4 Gb of linear address space.
//
using Pd = PdeGeneric[1024];
static constexpr PhysicalLayout<Pd> pd(const unsigned int cr3Pfn) noexcept
{
return PhysicalLayout<Pd>{ Pfn::toPage(cr3Pfn) };
}
static constexpr PhysicalLayout<PdeGeneric> pde(const unsigned int cr3Pfn, const LinearAddress& linear) noexcept
{
return PhysicalLayout<PdeGeneric>{ pd(cr3Pfn).physicalAddress + linear.generic.PageDirectoryOffset * k_entrySize };
}
};
template <>
struct Tables<Mode::legacyPae> // EFER.LMA == 0, CR4.PAE == 1, CR4.PSE is ignored
{
static constexpr Mode k_mode = Mode::legacyPae;
static constexpr auto k_entrySize = sizeof(unsigned long long);
using LinearAddress = LinearAddress<k_mode>;
using Pfn = Pfn<k_mode>;
//
// Describes 1 Gb in one of the following ways:
// 512x2Mb pages | PDPE -> PDE(PS=1) -> PA (2Mb)
// 512x[512x4Kb] pages | PDPE -> PDE(PS=0) -> PA (4Kb)
//
union Pdpe
{
unsigned long long raw;
struct
{
unsigned long long P : 1; // Present
unsigned long long : 2;
unsigned long long PWT : 1; // Page-Level Writethrough
unsigned long long PCD : 1; // Page-Level Cache Disable
unsigned long long : 4;
unsigned long long AVL : 3; // Available to software
unsigned long long PD : 40; // Page frame number
unsigned long long : 12;
} layout;
template <PageSize pageSize>
union Pde;
//
// Describes 2 Mb in 512x4Kb pages: PDPE -> PDE(PS=0) -> PTE -> PA (2Mb)
//
template <>
union Pde<PageSize::nonPse>
{
unsigned long long raw;
struct
{
unsigned long long P : 1; // Present
unsigned long long RW : 1; // Read/Write
unsigned long long US : 1; // User/Supervisor
unsigned long long PWT : 1; // Page-Level Writethrough
unsigned long long PCD : 1; // Page-Level Cache Disable
unsigned long long A : 1; // Accessed
unsigned long long Ignored0 : 1;
unsigned long long PS : 1; // PageSize == 0
unsigned long long Ignored1 : 1;
unsigned long long AVL : 3; // Available to software
unsigned long long PT : 40; // Page frame number
unsigned long long Available : 11;
unsigned long long NX : 1; // No Execute
} layout;
//
// Describes 4Kb page: PDPE -> PDE(PS=0) -> PTE -> PA (4Kb)
//
union Pte
{
unsigned long long raw;
struct
{