github.com/SagerNet/gvisor@v0.0.0-20210707092255-7731c139d75c/pkg/ring0/pagetables/pagetables_amd64_test.go (about)

     1  // Copyright 2018 The gVisor Authors.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  // +build amd64
    16  
    17  package pagetables
    18  
    19  import (
    20  	"testing"
    21  
    22  	"github.com/SagerNet/gvisor/pkg/hostarch"
    23  )
    24  
    25  func Test2MAnd4K(t *testing.T) {
    26  	pt := New(NewRuntimeAllocator())
    27  
    28  	// Map a small page and a huge page.
    29  	pt.Map(0x400000, pteSize, MapOpts{AccessType: hostarch.ReadWrite}, pteSize*42)
    30  	pt.Map(0x00007f0000000000, pmdSize, MapOpts{AccessType: hostarch.Read}, pmdSize*47)
    31  
    32  	checkMappings(t, pt, []mapping{
    33  		{0x400000, pteSize, pteSize * 42, MapOpts{AccessType: hostarch.ReadWrite}},
    34  		{0x00007f0000000000, pmdSize, pmdSize * 47, MapOpts{AccessType: hostarch.Read}},
    35  	})
    36  }
    37  
    38  func Test1GAnd4K(t *testing.T) {
    39  	pt := New(NewRuntimeAllocator())
    40  
    41  	// Map a small page and a super page.
    42  	pt.Map(0x400000, pteSize, MapOpts{AccessType: hostarch.ReadWrite}, pteSize*42)
    43  	pt.Map(0x00007f0000000000, pudSize, MapOpts{AccessType: hostarch.Read}, pudSize*47)
    44  
    45  	checkMappings(t, pt, []mapping{
    46  		{0x400000, pteSize, pteSize * 42, MapOpts{AccessType: hostarch.ReadWrite}},
    47  		{0x00007f0000000000, pudSize, pudSize * 47, MapOpts{AccessType: hostarch.Read}},
    48  	})
    49  }
    50  
    51  func TestSplit1GPage(t *testing.T) {
    52  	pt := New(NewRuntimeAllocator())
    53  
    54  	// Map a super page and knock out the middle.
    55  	pt.Map(0x00007f0000000000, pudSize, MapOpts{AccessType: hostarch.Read}, pudSize*42)
    56  	pt.Unmap(hostarch.Addr(0x00007f0000000000+pteSize), pudSize-(2*pteSize))
    57  
    58  	checkMappings(t, pt, []mapping{
    59  		{0x00007f0000000000, pteSize, pudSize * 42, MapOpts{AccessType: hostarch.Read}},
    60  		{0x00007f0000000000 + pudSize - pteSize, pteSize, pudSize*42 + pudSize - pteSize, MapOpts{AccessType: hostarch.Read}},
    61  	})
    62  }
    63  
    64  func TestSplit2MPage(t *testing.T) {
    65  	pt := New(NewRuntimeAllocator())
    66  
    67  	// Map a huge page and knock out the middle.
    68  	pt.Map(0x00007f0000000000, pmdSize, MapOpts{AccessType: hostarch.Read}, pmdSize*42)
    69  	pt.Unmap(hostarch.Addr(0x00007f0000000000+pteSize), pmdSize-(2*pteSize))
    70  
    71  	checkMappings(t, pt, []mapping{
    72  		{0x00007f0000000000, pteSize, pmdSize * 42, MapOpts{AccessType: hostarch.Read}},
    73  		{0x00007f0000000000 + pmdSize - pteSize, pteSize, pmdSize*42 + pmdSize - pteSize, MapOpts{AccessType: hostarch.Read}},
    74  	})
    75  }