github.com/nicocha30/gvisor-ligolo@v0.0.0-20230726075806-989fa2c0a413/pkg/ring0/pagetables/allocator_unsafe.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  package pagetables
    16  
    17  import (
    18  	"unsafe"
    19  
    20  	"github.com/nicocha30/gvisor-ligolo/pkg/hostarch"
    21  )
    22  
    23  // newAlignedPTEs returns a set of aligned PTEs.
    24  func newAlignedPTEs() *PTEs {
    25  	ptes := new(PTEs)
    26  	offset := physicalFor(ptes) & (hostarch.PageSize - 1)
    27  	if offset == 0 {
    28  		// Already aligned.
    29  		return ptes
    30  	}
    31  
    32  	// Need to force an aligned allocation.
    33  	unaligned := make([]byte, (2*hostarch.PageSize)-1)
    34  	offset = uintptr(unsafe.Pointer(&unaligned[0])) & (hostarch.PageSize - 1)
    35  	if offset != 0 {
    36  		offset = hostarch.PageSize - offset
    37  	}
    38  	return (*PTEs)(unsafe.Pointer(&unaligned[offset]))
    39  }
    40  
    41  // physicalFor returns the "physical" address for PTEs.
    42  //
    43  //go:nosplit
    44  func physicalFor(ptes *PTEs) uintptr {
    45  	return uintptr(unsafe.Pointer(ptes))
    46  }
    47  
    48  // fromPhysical returns the PTEs from the "physical" address.
    49  //
    50  //go:nosplit
    51  func fromPhysical(physical uintptr) *PTEs {
    52  	return (*PTEs)(unsafe.Pointer(physical))
    53  }