github.com/metacubex/gvisor@v0.0.0-20240320004321-933faba989ec/pkg/sentry/devices/nvproxy/nvproxy_unsafe.go (about) 1 // Copyright 2023 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 nvproxy 16 17 import ( 18 "bytes" 19 "fmt" 20 "unsafe" 21 22 "golang.org/x/sys/unix" 23 "github.com/metacubex/gvisor/pkg/abi/nvgpu" 24 ) 25 26 // HostDriverVersion returns the version of the host Nvidia driver. 27 func HostDriverVersion() (string, error) { 28 ctlFD, err := unix.Openat(-1, "/dev/nvidiactl", unix.O_RDONLY|unix.O_NOFOLLOW, 0) 29 if err != nil { 30 return "", fmt.Errorf("failed to open /dev/nvidiactl: %w", err) 31 } 32 defer unix.Close(ctlFD) 33 34 // From src/nvidia/arch/nvalloc/unix/include/nv-ioctl.h: 35 const NV_RM_API_VERSION_REPLY_RECOGNIZED = 1 36 37 // 530.30.02 and later versions of the host driver `#define 38 // NV_RM_API_VERSION_CMD_QUERY '2'`, which causes this ioctl to return the 39 // driver version without performing a check. Earlier versions of the 40 // driver `#define NV_RM_API_VERSION_CMD_OVERRIDE '2'`, which causes the 41 // ioctl to no-op. Try with Cmd '2' first, hoping that the driver 42 // interprets it as _QUERY; if the returned string is empty, then it was 43 // interpreted as _OVERRIDE and we need to perform an actual check (Cmd 0), 44 // which has the downside of logging an error message. 45 ioctlParams := nvgpu.RMAPIVersion{ 46 Cmd: '2', 47 } 48 if _, _, errno := unix.RawSyscall(unix.SYS_IOCTL, uintptr(ctlFD), frontendIoctlCmd(nvgpu.NV_ESC_CHECK_VERSION_STR, uint32(unsafe.Sizeof(ioctlParams))), uintptr(unsafe.Pointer(&ioctlParams))); errno != 0 { 49 return "", fmt.Errorf("NV_ESC_CHECK_VERSION_STR ioctl error: %w", errno) 50 } 51 if ioctlParams.Reply != NV_RM_API_VERSION_REPLY_RECOGNIZED { 52 return "", fmt.Errorf("unknown NV_ESC_CHECK_VERSION_STR reply: %d", ioctlParams.Reply) 53 } 54 if ioctlParams.VersionString[0] == '\x00' { 55 ioctlParams.Cmd = 0 56 ioctlParams.Reply = 0 57 // We expect the check to fail on our empty version string, so tolerate 58 // EINVAL. 59 if _, _, errno := unix.RawSyscall(unix.SYS_IOCTL, uintptr(ctlFD), frontendIoctlCmd(nvgpu.NV_ESC_CHECK_VERSION_STR, uint32(unsafe.Sizeof(ioctlParams))), uintptr(unsafe.Pointer(&ioctlParams))); errno != 0 && errno != unix.EINVAL { 60 return "", fmt.Errorf("fallback NV_ESC_CHECK_VERSION_STR ioctl error: %w", errno) 61 } 62 if ioctlParams.Reply != NV_RM_API_VERSION_REPLY_RECOGNIZED { 63 return "", fmt.Errorf("unknown fallback NV_ESC_CHECK_VERSION_STR reply: %d", ioctlParams.Reply) 64 } 65 } 66 67 if i := bytes.IndexByte(ioctlParams.VersionString[:], '\x00'); i >= 0 { 68 return string(ioctlParams.VersionString[:i]), nil 69 } 70 return string(ioctlParams.VersionString[:]), nil 71 } 72 73 func p64FromPtr(ptr unsafe.Pointer) nvgpu.P64 { 74 return nvgpu.P64(uint64(uintptr(ptr))) 75 }