github.com/MerlinKodo/gvisor@v0.0.0-20231110090155-957f62ecf90e/pkg/sentry/platform/kvm/kvm_amd64_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  //go:build amd64
    16  // +build amd64
    17  
    18  package kvm
    19  
    20  import (
    21  	"fmt"
    22  	"unsafe"
    23  
    24  	"golang.org/x/sys/unix"
    25  )
    26  
    27  var (
    28  	runDataSize    int
    29  	hasGuestPCID   bool
    30  	cpuidSupported = cpuidEntries{nr: _KVM_NR_CPUID_ENTRIES}
    31  )
    32  
    33  func updateSystemValues(fd int) error {
    34  	// Extract the mmap size.
    35  	sz, _, errno := unix.RawSyscall(unix.SYS_IOCTL, uintptr(fd), KVM_GET_VCPU_MMAP_SIZE, 0)
    36  	if errno != 0 {
    37  		return fmt.Errorf("getting VCPU mmap size: %v", errno)
    38  	}
    39  
    40  	// Save the data.
    41  	runDataSize = int(sz)
    42  
    43  	// Must do the dance to figure out the number of entries.
    44  	_, _, errno = unix.RawSyscall(
    45  		unix.SYS_IOCTL,
    46  		uintptr(fd),
    47  		KVM_GET_SUPPORTED_CPUID,
    48  		uintptr(unsafe.Pointer(&cpuidSupported)))
    49  	if errno != 0 && errno != unix.ENOMEM {
    50  		// Some other error occurred.
    51  		return fmt.Errorf("getting supported CPUID: %v", errno)
    52  	}
    53  
    54  	// The number should now be correct.
    55  	_, _, errno = unix.RawSyscall(
    56  		unix.SYS_IOCTL,
    57  		uintptr(fd),
    58  		KVM_GET_SUPPORTED_CPUID,
    59  		uintptr(unsafe.Pointer(&cpuidSupported)))
    60  	if errno != 0 {
    61  		// Didn't work with the right number.
    62  		return fmt.Errorf("getting supported CPUID (2nd attempt): %v", errno)
    63  	}
    64  
    65  	// Success.
    66  	return nil
    67  }