@@ -20,62 +20,100 @@ package pagetables
2020import (
2121 "testing"
2222
23+ "gvisor.dev/gvisor/pkg/cpuid"
2324 "gvisor.dev/gvisor/pkg/hostarch"
2425)
2526
26- func Test2MAnd4K (t * testing.T ) {
27- pt := New (NewRuntimeAllocator ())
27+ var (
28+ lowerTopAligned uintptr = 0x00007f0000000000
29+ pt * PageTables
30+ )
31+
32+ func getLargeAddressesEnabled () bool {
33+ featureSet := cpuid .HostFeatureSet ()
34+ return featureSet .HasFeature (cpuid .X86FeatureLA57 )
35+ }
36+
37+ func getLowerTopAligned () uintptr {
38+ if getLargeAddressesEnabled () {
39+ return 0x00FF000000000000
40+ }
41+ return lowerTopAligned
42+ }
43+
44+ func InitTest () {
45+ cpuid .Initialize ()
46+ pt = New (NewRuntimeAllocator ())
47+ pt .InitArch (NewRuntimeAllocator ())
48+ }
49+
50+ func TestLargeAddresses (t * testing.T ) {
51+ InitTest ()
52+ if ! getLargeAddressesEnabled () {
53+ t .Skip ("Large addresses are not supported on this platform" )
54+ }
55+ pt .Map (hostarch .Addr (1 << 50 ), pteSize , MapOpts {AccessType : hostarch .ReadWrite }, pteSize * 42 )
56+ pt .Map (hostarch .Addr (1 << 54 ), pmdSize , MapOpts {AccessType : hostarch .Read }, pmdSize * 42 )
57+
58+ checkMappings (t , pt , []mapping {
59+ {uintptr (1 << 50 ), pteSize , pteSize * 42 , MapOpts {AccessType : hostarch .ReadWrite }},
60+ {uintptr (1 << 54 ), pmdSize , pmdSize * 42 , MapOpts {AccessType : hostarch .Read }},
61+ })
62+ }
2863
64+ func Test2MAnd4K (t * testing.T ) {
65+ InitTest ()
2966 // Map a small page and a huge page.
3067 pt .Map (0x400000 , pteSize , MapOpts {AccessType : hostarch .ReadWrite }, pteSize * 42 )
31- pt .Map (0x00007f0000000000 , pmdSize , MapOpts {AccessType : hostarch .Read }, pmdSize * 47 )
68+ pt .Map (hostarch . Addr ( getLowerTopAligned ()) , pmdSize , MapOpts {AccessType : hostarch .Read }, pmdSize * 47 )
3269
3370 checkMappings (t , pt , []mapping {
3471 {0x400000 , pteSize , pteSize * 42 , MapOpts {AccessType : hostarch .ReadWrite }},
35- {0x00007f0000000000 , pmdSize , pmdSize * 47 , MapOpts {AccessType : hostarch .Read }},
72+ {getLowerTopAligned () , pmdSize , pmdSize * 47 , MapOpts {AccessType : hostarch .Read }},
3673 })
3774}
3875
3976func Test1GAnd4K (t * testing.T ) {
40- pt := New ( NewRuntimeAllocator () )
77+ InitTest ( )
4178
4279 // Map a small page and a super page.
4380 pt .Map (0x400000 , pteSize , MapOpts {AccessType : hostarch .ReadWrite }, pteSize * 42 )
44- pt .Map (0x00007f0000000000 , pudSize , MapOpts {AccessType : hostarch .Read }, pudSize * 47 )
81+ pt .Map (hostarch . Addr ( getLowerTopAligned ()) , pudSize , MapOpts {AccessType : hostarch .Read }, pudSize * 47 )
4582
4683 checkMappings (t , pt , []mapping {
4784 {0x400000 , pteSize , pteSize * 42 , MapOpts {AccessType : hostarch .ReadWrite }},
48- {0x00007f0000000000 , pudSize , pudSize * 47 , MapOpts {AccessType : hostarch .Read }},
85+ {getLowerTopAligned () , pudSize , pudSize * 47 , MapOpts {AccessType : hostarch .Read }},
4986 })
5087}
5188
5289func TestSplit1GPage (t * testing.T ) {
53- pt := New ( NewRuntimeAllocator () )
90+ InitTest ( )
5491
5592 // Map a super page and knock out the middle.
56- pt .Map (0x00007f0000000000 , pudSize , MapOpts {AccessType : hostarch .Read }, pudSize * 42 )
57- pt .Unmap (hostarch .Addr (0x00007f0000000000 + pteSize ), pudSize - (2 * pteSize ))
93+ pt .Map (hostarch . Addr ( getLowerTopAligned ()) , pudSize , MapOpts {AccessType : hostarch .Read }, pudSize * 42 )
94+ pt .Unmap (hostarch .Addr (getLowerTopAligned () + pteSize ), pudSize - (2 * pteSize ))
5895
5996 checkMappings (t , pt , []mapping {
60- {0x00007f0000000000 , pteSize , pudSize * 42 , MapOpts {AccessType : hostarch .Read }},
61- {0x00007f0000000000 + pudSize - pteSize , pteSize , pudSize * 42 + pudSize - pteSize , MapOpts {AccessType : hostarch .Read }},
97+ {getLowerTopAligned () , pteSize , pudSize * 42 , MapOpts {AccessType : hostarch .Read }},
98+ {getLowerTopAligned () + pudSize - pteSize , pteSize , pudSize * 42 + pudSize - pteSize , MapOpts {AccessType : hostarch .Read }},
6299 })
63100}
64101
65102func TestSplit2MPage (t * testing.T ) {
66- pt := New ( NewRuntimeAllocator () )
103+ InitTest ( )
67104
68105 // Map a huge page and knock out the middle.
69- pt .Map (0x00007f0000000000 , pmdSize , MapOpts {AccessType : hostarch .Read }, pmdSize * 42 )
70- pt .Unmap (hostarch .Addr (0x00007f0000000000 + pteSize ), pmdSize - (2 * pteSize ))
106+ pt .Map (hostarch . Addr ( getLowerTopAligned ()) , pmdSize , MapOpts {AccessType : hostarch .Read }, pmdSize * 42 )
107+ pt .Unmap (hostarch .Addr (getLowerTopAligned () + pteSize ), pmdSize - (2 * pteSize ))
71108
72109 checkMappings (t , pt , []mapping {
73- {0x00007f0000000000 , pteSize , pmdSize * 42 , MapOpts {AccessType : hostarch .Read }},
74- {0x00007f0000000000 + pmdSize - pteSize , pteSize , pmdSize * 42 + pmdSize - pteSize , MapOpts {AccessType : hostarch .Read }},
110+ {getLowerTopAligned () , pteSize , pmdSize * 42 , MapOpts {AccessType : hostarch .Read }},
111+ {getLowerTopAligned () + pmdSize - pteSize , pteSize , pmdSize * 42 + pmdSize - pteSize , MapOpts {AccessType : hostarch .Read }},
75112 })
76113}
77114
78115func TestNumMemoryTypes (t * testing.T ) {
116+ InitTest ()
79117 // The PAT accommodates up to 8 entries. However, PTE.Set() currently
80118 // assumes that NumMemoryTypes <= 4, since the location of the most
81119 // significant bit of the PAT index in page table entries varies depending
0 commit comments