@@ -20,62 +20,100 @@ package pagetables
20
20
import (
21
21
"testing"
22
22
23
+ "gvisor.dev/gvisor/pkg/cpuid"
23
24
"gvisor.dev/gvisor/pkg/hostarch"
24
25
)
25
26
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
+ }
28
63
64
+ func Test2MAnd4K (t * testing.T ) {
65
+ InitTest ()
29
66
// Map a small page and a huge page.
30
67
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 )
32
69
33
70
checkMappings (t , pt , []mapping {
34
71
{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 }},
36
73
})
37
74
}
38
75
39
76
func Test1GAnd4K (t * testing.T ) {
40
- pt := New ( NewRuntimeAllocator () )
77
+ InitTest ( )
41
78
42
79
// Map a small page and a super page.
43
80
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 )
45
82
46
83
checkMappings (t , pt , []mapping {
47
84
{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 }},
49
86
})
50
87
}
51
88
52
89
func TestSplit1GPage (t * testing.T ) {
53
- pt := New ( NewRuntimeAllocator () )
90
+ InitTest ( )
54
91
55
92
// 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 ))
58
95
59
96
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 }},
62
99
})
63
100
}
64
101
65
102
func TestSplit2MPage (t * testing.T ) {
66
- pt := New ( NewRuntimeAllocator () )
103
+ InitTest ( )
67
104
68
105
// 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 ))
71
108
72
109
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 }},
75
112
})
76
113
}
77
114
78
115
func TestNumMemoryTypes (t * testing.T ) {
116
+ InitTest ()
79
117
// The PAT accommodates up to 8 entries. However, PTE.Set() currently
80
118
// assumes that NumMemoryTypes <= 4, since the location of the most
81
119
// significant bit of the PAT index in page table entries varies depending
0 commit comments