gvisor.dev/gvisor@v0.0.0-20240520182842-f9d4d51c7e0f/pkg/sentry/devices/nvproxy/uvm_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 "runtime" 19 "unsafe" 20 21 "golang.org/x/sys/unix" 22 "gvisor.dev/gvisor/pkg/abi/nvgpu" 23 "gvisor.dev/gvisor/pkg/errors/linuxerr" 24 "gvisor.dev/gvisor/pkg/log" 25 ) 26 27 func uvmIoctlInvoke[Params any](ui *uvmIoctlState, ioctlParams *Params) (uintptr, error) { 28 n, _, errno := unix.RawSyscall(unix.SYS_IOCTL, uintptr(ui.fd.hostFD), uintptr(ui.cmd), uintptr(unsafe.Pointer(ioctlParams))) 29 if errno != 0 { 30 return n, errno 31 } 32 return n, nil 33 } 34 35 // BufferReadAt implements memmap.File.BufferReadAt. 36 func (mf *uvmFDMemmapFile) BufferReadAt(off uint64, dst []byte) (uint64, error) { 37 // kernel-open/nvidia-uvm/uvm.c:uvm_fops.{read,read_iter,splice_read} == 38 // NULL, so UVM data can only be read via ioctl. 39 if len(dst) == 0 { 40 return 0, nil 41 } 42 defer runtime.KeepAlive(dst) 43 params := nvgpu.UVM_TOOLS_READ_PROCESS_MEMORY_PARAMS{ 44 Buffer: uint64(uintptr(unsafe.Pointer(&dst[0]))), 45 Size: uint64(len(dst)), 46 TargetVA: off, 47 } 48 _, _, errno := unix.Syscall(unix.SYS_IOCTL, uintptr(mf.fd.hostFD), nvgpu.UVM_TOOLS_READ_PROCESS_MEMORY, uintptr(unsafe.Pointer(¶ms))) 49 if errno != 0 { 50 return 0, errno 51 } 52 if params.RMStatus != nvgpu.NV_OK { 53 log.Warningf("nvproxy: UVM_TOOLS_READ_PROCESS_MEMORY(targetVa=%#x, len=%d) returned status %d", off, len(dst), params.RMStatus) 54 return params.BytesRead, linuxerr.EINVAL 55 } 56 if params.BytesRead != uint64(len(dst)) { 57 log.Warningf("nvproxy: UVM_TOOLS_READ_PROCESS_MEMORY(targetVa=%#x, len=%d) returned %d bytes", off, len(dst), params.BytesRead) 58 return params.BytesRead, linuxerr.EINVAL 59 } 60 return params.BytesRead, nil 61 } 62 63 // BufferWriteAt implements memmap.File.BufferWriteAt. 64 func (mf *uvmFDMemmapFile) BufferWriteAt(off uint64, src []byte) (uint64, error) { 65 // kernel-open/nvidia-uvm/uvm.c:uvm_fops.{write,write_iter,splice_write} == 66 // NULL, so UVM data can only be written via ioctl. 67 if len(src) == 0 { 68 return 0, nil 69 } 70 defer runtime.KeepAlive(src) 71 params := nvgpu.UVM_TOOLS_WRITE_PROCESS_MEMORY_PARAMS{ 72 Buffer: uint64(uintptr(unsafe.Pointer(&src[0]))), 73 Size: uint64(len(src)), 74 TargetVA: off, 75 } 76 _, _, errno := unix.Syscall(unix.SYS_IOCTL, uintptr(mf.fd.hostFD), nvgpu.UVM_TOOLS_WRITE_PROCESS_MEMORY, uintptr(unsafe.Pointer(¶ms))) 77 if errno != 0 { 78 return 0, errno 79 } 80 if params.RMStatus != nvgpu.NV_OK { 81 log.Warningf("nvproxy: UVM_TOOLS_WRITE_PROCESS_MEMORY(targetVa=%#x, len=%d) returned status %d", off, len(src), params.RMStatus) 82 return params.BytesWritten, linuxerr.EINVAL 83 } 84 if params.BytesWritten != uint64(len(src)) { 85 log.Warningf("nvproxy: UVM_TOOLS_WRITE_PROCESS_MEMORY(targetVa=%#x, len=%d) returned %d bytes", off, len(src), params.BytesWritten) 86 return params.BytesWritten, linuxerr.EINVAL 87 } 88 return params.BytesWritten, nil 89 }