gvisor.dev/gvisor@v0.0.0-20240520182842-f9d4d51c7e0f/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  //go:build amd64
    16  // +build amd64
    17  
    18  package pagetables
    19  
    20  import (
    21  	"testing"
    22  
    23  	"gvisor.dev/gvisor/pkg/hostarch"
    24  )
    25  
    26  func Test2MAnd4K(t *testing.T) {
    27  	pt := New(NewRuntimeAllocator())
    28  
    29  	// Map a small page and a huge page.
    30  	pt.Map(0x400000, pteSize, MapOpts{AccessType: hostarch.ReadWrite}, pteSize*42)
    31  	pt.Map(0x00007f0000000000, pmdSize, MapOpts{AccessType: hostarch.Read}, pmdSize*47)
    32  
    33  	checkMappings(t, pt, []mapping{
    34  		{0x400000, pteSize, pteSize * 42, MapOpts{AccessType: hostarch.ReadWrite}},
    35  		{0x00007f0000000000, pmdSize, pmdSize * 47, MapOpts{AccessType: hostarch.Read}},
    36  	})
    37  }
    38  
    39  func Test1GAnd4K(t *testing.T) {
    40  	pt := New(NewRuntimeAllocator())
    41  
    42  	// Map a small page and a super page.
    43  	pt.Map(0x400000, pteSize, MapOpts{AccessType: hostarch.ReadWrite}, pteSize*42)
    44  	pt.Map(0x00007f0000000000, pudSize, MapOpts{AccessType: hostarch.Read}, pudSize*47)
    45  
    46  	checkMappings(t, pt, []mapping{
    47  		{0x400000, pteSize, pteSize * 42, MapOpts{AccessType: hostarch.ReadWrite}},
    48  		{0x00007f0000000000, pudSize, pudSize * 47, MapOpts{AccessType: hostarch.Read}},
    49  	})
    50  }
    51  
    52  func TestSplit1GPage(t *testing.T) {
    53  	pt := New(NewRuntimeAllocator())
    54  
    55  	// 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))
    58  
    59  	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}},
    62  	})
    63  }
    64  
    65  func TestSplit2MPage(t *testing.T) {
    66  	pt := New(NewRuntimeAllocator())
    67  
    68  	// 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))
    71  
    72  	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}},
    75  	})
    76  }