github.com/sagernet/gvisor@v0.0.0-20240428053021-e691de28565f/pkg/abi/nvgpu/frontend.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 nvgpu 16 17 import ( 18 "github.com/sagernet/gvisor/pkg/marshal" 19 ) 20 21 // NV_IOCTL_MAGIC is the "canonical" IOC_TYPE for frontend ioctls. 22 // The driver ignores IOC_TYPE, allowing any value to be passed. 23 const NV_IOCTL_MAGIC = uint32('F') 24 25 // Frontend ioctl numbers. 26 // Note that these are only the IOC_NR part of the ioctl command. 27 const ( 28 // From kernel-open/common/inc/nv-ioctl-numbers.h: 29 NV_IOCTL_BASE = 200 30 NV_ESC_CARD_INFO = NV_IOCTL_BASE + 0 31 NV_ESC_REGISTER_FD = NV_IOCTL_BASE + 1 32 NV_ESC_ALLOC_OS_EVENT = NV_IOCTL_BASE + 6 33 NV_ESC_FREE_OS_EVENT = NV_IOCTL_BASE + 7 34 NV_ESC_CHECK_VERSION_STR = NV_IOCTL_BASE + 10 35 NV_ESC_ATTACH_GPUS_TO_FD = NV_IOCTL_BASE + 12 36 NV_ESC_SYS_PARAMS = NV_IOCTL_BASE + 14 37 NV_ESC_WAIT_OPEN_COMPLETE = NV_IOCTL_BASE + 18 38 39 // From kernel-open/common/inc/nv-ioctl-numa.h: 40 NV_ESC_NUMA_INFO = NV_IOCTL_BASE + 15 41 42 // From src/nvidia/arch/nvalloc/unix/include/nv_escape.h: 43 NV_ESC_RM_ALLOC_MEMORY = 0x27 44 NV_ESC_RM_FREE = 0x29 45 NV_ESC_RM_CONTROL = 0x2a 46 NV_ESC_RM_ALLOC = 0x2b 47 NV_ESC_RM_DUP_OBJECT = 0x34 48 NV_ESC_RM_SHARE = 0x35 49 NV_ESC_RM_VID_HEAP_CONTROL = 0x4a 50 NV_ESC_RM_MAP_MEMORY = 0x4e 51 NV_ESC_RM_UNMAP_MEMORY = 0x4f 52 NV_ESC_RM_UPDATE_DEVICE_MAPPING_INFO = 0x5e 53 ) 54 55 // Frontend ioctl parameter structs, from src/common/sdk/nvidia/inc/nvos.h or 56 // kernel-open/common/inc/nv-ioctl.h. 57 58 // IoctlRegisterFD is nv_ioctl_register_fd_t, the parameter type for 59 // NV_ESC_REGISTER_FD. 60 // 61 // +marshal 62 type IoctlRegisterFD struct { 63 CtlFD int32 64 } 65 66 // IoctlAllocOSEvent is nv_ioctl_alloc_os_event_t, the parameter type for 67 // NV_ESC_ALLOC_OS_EVENT. 68 // 69 // +marshal 70 type IoctlAllocOSEvent struct { 71 HClient Handle 72 HDevice Handle 73 FD uint32 74 Status uint32 75 } 76 77 // IoctlFreeOSEvent is nv_ioctl_free_os_event_t, the parameter type for 78 // NV_ESC_FREE_OS_EVENT. 79 // 80 // +marshal 81 type IoctlFreeOSEvent struct { 82 HClient Handle 83 HDevice Handle 84 FD uint32 85 Status uint32 86 } 87 88 // RMAPIVersion is nv_rm_api_version_t, the parameter type for 89 // NV_ESC_CHECK_VERSION_STR. 90 // 91 // +marshal 92 type RMAPIVersion struct { 93 Cmd uint32 94 Reply uint32 95 VersionString [64]byte 96 } 97 98 // IoctlSysParams is nv_ioctl_sys_params_t, the parameter type for 99 // NV_ESC_SYS_PARAMS. 100 // 101 // +marshal 102 type IoctlSysParams struct { 103 MemblockSize uint64 104 } 105 106 // IoctlWaitOpenComplete is nv_ioctl_wait_open_complete_t, the parameter type 107 // for NV_ESC_WAIT_OPEN_COMPLETE. 108 // 109 // +marshal 110 type IoctlWaitOpenComplete struct { 111 Rc int32 112 AdapterStatus uint32 113 } 114 115 // IoctlNVOS02ParametersWithFD is nv_ioctl_nvos2_parameters_with_fd, the 116 // parameter type for NV_ESC_RM_ALLOC_MEMORY. 117 // 118 // +marshal 119 type IoctlNVOS02ParametersWithFD struct { 120 Params NVOS02Parameters 121 FD int32 122 Pad0 [4]byte 123 } 124 125 // +marshal 126 type NVOS02Parameters struct { 127 HRoot Handle 128 HObjectParent Handle 129 HObjectNew Handle 130 HClass ClassID 131 Flags uint32 132 Pad0 [4]byte 133 PMemory P64 // address of application mapping, without indirection 134 Limit uint64 135 Status uint32 136 Pad1 [4]byte 137 } 138 139 // NVOS00Parameters is NVOS00_PARAMETERS, the parameter type for 140 // NV_ESC_RM_FREE. 141 // 142 // +marshal 143 type NVOS00Parameters struct { 144 HRoot Handle 145 HObjectParent Handle 146 HObjectOld Handle 147 Status uint32 148 } 149 150 // RmAllocParamType should be implemented by all possible parameter types for 151 // NV_ESC_RM_ALLOC. 152 type RmAllocParamType interface { 153 GetPAllocParms() P64 154 GetPRightsRequested() P64 155 SetPAllocParms(p P64) 156 SetPRightsRequested(p P64) 157 FromOS64(other NVOS64Parameters) 158 ToOS64() NVOS64Parameters 159 GetPointer() uintptr 160 marshal.Marshallable 161 } 162 163 // GetRmAllocParamObj returns the appropriate implementation of 164 // RmAllocParamType based on passed parameters. 165 func GetRmAllocParamObj(isNVOS64 bool) RmAllocParamType { 166 if isNVOS64 { 167 return &NVOS64Parameters{} 168 } 169 return &NVOS21Parameters{} 170 } 171 172 // NVOS21Parameters is NVOS21_PARAMETERS, one possible parameter type for 173 // NV_ESC_RM_ALLOC. 174 // 175 // +marshal 176 type NVOS21Parameters struct { 177 HRoot Handle 178 HObjectParent Handle 179 HObjectNew Handle 180 HClass ClassID 181 PAllocParms P64 182 ParamsSize uint32 183 Status uint32 184 } 185 186 // GetPAllocParms implements RmAllocParamType.GetPAllocParms. 187 func (n *NVOS21Parameters) GetPAllocParms() P64 { 188 return n.PAllocParms 189 } 190 191 // GetPRightsRequested implements RmAllocParamType.GetPRightsRequested. 192 func (n *NVOS21Parameters) GetPRightsRequested() P64 { 193 return 0 194 } 195 196 // SetPAllocParms implements RmAllocParamType.SetPAllocParms. 197 func (n *NVOS21Parameters) SetPAllocParms(p P64) { n.PAllocParms = p } 198 199 // SetPRightsRequested implements RmAllocParamType.SetPRightsRequested. 200 func (n *NVOS21Parameters) SetPRightsRequested(p P64) { 201 panic("impossible") 202 } 203 204 // FromOS64 implements RmAllocParamType.FromOS64. 205 func (n *NVOS21Parameters) FromOS64(other NVOS64Parameters) { 206 n.HRoot = other.HRoot 207 n.HObjectParent = other.HObjectParent 208 n.HObjectNew = other.HObjectNew 209 n.HClass = other.HClass 210 n.PAllocParms = other.PAllocParms 211 n.ParamsSize = other.ParamsSize 212 n.Status = other.Status 213 } 214 215 // ToOS64 implements RmAllocParamType.ToOS64. 216 func (n *NVOS21Parameters) ToOS64() NVOS64Parameters { 217 return NVOS64Parameters{ 218 HRoot: n.HRoot, 219 HObjectParent: n.HObjectParent, 220 HObjectNew: n.HObjectNew, 221 HClass: n.HClass, 222 PAllocParms: n.PAllocParms, 223 ParamsSize: n.ParamsSize, 224 Status: n.Status, 225 } 226 } 227 228 // NVOS55Parameters is NVOS55_PARAMETERS, the parameter type for 229 // NV_ESC_RM_DUP_OBJECT. 230 // 231 // +marshal 232 type NVOS55Parameters struct { 233 HClient Handle 234 HParent Handle 235 HObject Handle 236 HClientSrc Handle 237 HObjectSrc Handle 238 Flags uint32 239 Status uint32 240 } 241 242 // NVOS57Parameters is NVOS57_PARAMETERS, the parameter type for 243 // NV_ESC_RM_SHARE. 244 // 245 // +marshal 246 type NVOS57Parameters struct { 247 HClient Handle 248 HObject Handle 249 SharePolicy RS_SHARE_POLICY 250 Status uint32 251 } 252 253 // NVOS32Parameters is NVOS32_PARAMETERS, the parameter type for 254 // NV_ESC_RM_VID_HEAP_CONTROL. 255 // 256 // +marshal 257 type NVOS32Parameters struct { 258 HRoot Handle 259 HObjectParent Handle 260 Function uint32 261 HVASpace Handle 262 IVCHeapNumber int16 263 Pad [2]byte 264 Status uint32 265 Total uint64 266 Free uint64 267 Data [144]byte // union 268 } 269 270 // Possible values for NVOS32Parameters.Function: 271 const ( 272 NVOS32_FUNCTION_ALLOC_SIZE = 2 273 ) 274 275 // NVOS32AllocSize is the type of NVOS32Parameters.Data for 276 // NVOS32_FUNCTION_ALLOC_SIZE. 277 type NVOS32AllocSize struct { 278 Owner uint32 279 HMemory Handle 280 Type uint32 281 Flags uint32 282 Attr uint32 283 Format uint32 284 ComprCovg uint32 285 ZcullCovg uint32 286 PartitionStride uint32 287 Width uint32 288 Height uint32 289 Pad0 [4]byte 290 Size uint64 291 Alignment uint64 292 Offset uint64 293 Limit uint64 294 Address P64 295 RangeBegin uint64 296 RangeEnd uint64 297 Attr2 uint32 298 CtagOffset uint32 299 } 300 301 // IoctlNVOS33ParametersWithFD is nv_ioctl_nvos33_parameters_with_fd, the 302 // parameter type for NV_ESC_RM_MAP_MEMORY, from 303 // src/nvidia/arch/nvalloc/unix/include/nv-unix-nvos-params-wrappers.h. 304 // 305 // +marshal 306 type IoctlNVOS33ParametersWithFD struct { 307 Params NVOS33Parameters 308 FD int32 309 Pad0 [4]byte 310 } 311 312 // +marshal 313 type NVOS33Parameters struct { 314 HClient Handle 315 HDevice Handle 316 HMemory Handle 317 Pad0 [4]byte 318 Offset uint64 319 Length uint64 320 PLinearAddress P64 // address of application mapping, without indirection 321 Status uint32 322 Flags uint32 323 } 324 325 // NVOS34Parameters is NVOS34_PARAMETERS, the parameter type for 326 // NV_ESC_RM_UNMAP_MEMORY. 327 // 328 // +marshal 329 type NVOS34Parameters struct { 330 HClient Handle 331 HDevice Handle 332 HMemory Handle 333 Pad0 [4]byte 334 PLinearAddress P64 // address of application mapping, without indirection 335 Status uint32 336 Flags uint32 337 } 338 339 // NVOS54Parameters is NVOS54_PARAMETERS, the parameter type for 340 // NV_ESC_RM_CONTROL. 341 // 342 // +marshal 343 type NVOS54Parameters struct { 344 HClient Handle 345 HObject Handle 346 Cmd uint32 347 Flags uint32 348 Params P64 349 ParamsSize uint32 350 Status uint32 351 } 352 353 // NVOS56Parameters is NVOS56_PARAMETERS, the parameter type for 354 // NV_ESC_RM_UPDATE_DEVICE_MAPPING_INFO. 355 // 356 // +marshal 357 type NVOS56Parameters struct { 358 HClient Handle 359 HDevice Handle 360 HMemory Handle 361 Pad0 [4]byte 362 POldCPUAddress P64 363 PNewCPUAddress P64 364 Status uint32 365 Pad1 [4]byte 366 } 367 368 // NVOS64Parameters is NVOS64_PARAMETERS, one possible parameter type for 369 // NV_ESC_RM_ALLOC. 370 // 371 // +marshal 372 type NVOS64Parameters struct { 373 HRoot Handle 374 HObjectParent Handle 375 HObjectNew Handle 376 HClass ClassID 377 PAllocParms P64 378 PRightsRequested P64 379 ParamsSize uint32 380 Flags uint32 381 Status uint32 382 _ uint32 383 } 384 385 // GetPAllocParms implements RmAllocParamType.GetPAllocParms. 386 func (n *NVOS64Parameters) GetPAllocParms() P64 { 387 return n.PAllocParms 388 } 389 390 // GetPRightsRequested implements RmAllocParamType.GetPRightsRequested. 391 func (n *NVOS64Parameters) GetPRightsRequested() P64 { 392 return n.PRightsRequested 393 } 394 395 // SetPAllocParms implements RmAllocParamType.SetPAllocParms. 396 func (n *NVOS64Parameters) SetPAllocParms(p P64) { n.PAllocParms = p } 397 398 // SetPRightsRequested implements RmAllocParamType.SetPRightsRequested. 399 func (n *NVOS64Parameters) SetPRightsRequested(p P64) { n.PRightsRequested = p } 400 401 // FromOS64 implements RmAllocParamType.FromOS64. 402 func (n *NVOS64Parameters) FromOS64(other NVOS64Parameters) { *n = other } 403 404 // ToOS64 implements RmAllocParamType.ToOS64. 405 func (n *NVOS64Parameters) ToOS64() NVOS64Parameters { return *n } 406 407 // Frontend ioctl parameter struct sizes. 408 var ( 409 SizeofIoctlRegisterFD = uint32((*IoctlRegisterFD)(nil).SizeBytes()) 410 SizeofIoctlAllocOSEvent = uint32((*IoctlAllocOSEvent)(nil).SizeBytes()) 411 SizeofIoctlFreeOSEvent = uint32((*IoctlFreeOSEvent)(nil).SizeBytes()) 412 SizeofRMAPIVersion = uint32((*RMAPIVersion)(nil).SizeBytes()) 413 SizeofIoctlSysParams = uint32((*IoctlSysParams)(nil).SizeBytes()) 414 SizeofIoctlWaitOpenComplete = uint32((*IoctlWaitOpenComplete)(nil).SizeBytes()) 415 SizeofIoctlNVOS02ParametersWithFD = uint32((*IoctlNVOS02ParametersWithFD)(nil).SizeBytes()) 416 SizeofNVOS00Parameters = uint32((*NVOS00Parameters)(nil).SizeBytes()) 417 SizeofNVOS21Parameters = uint32((*NVOS21Parameters)(nil).SizeBytes()) 418 SizeofIoctlNVOS33ParametersWithFD = uint32((*IoctlNVOS33ParametersWithFD)(nil).SizeBytes()) 419 SizeofNVOS55Parameters = uint32((*NVOS55Parameters)(nil).SizeBytes()) 420 SizeofNVOS57Parameters = uint32((*NVOS57Parameters)(nil).SizeBytes()) 421 SizeofNVOS32Parameters = uint32((*NVOS32Parameters)(nil).SizeBytes()) 422 SizeofNVOS34Parameters = uint32((*NVOS34Parameters)(nil).SizeBytes()) 423 SizeofNVOS54Parameters = uint32((*NVOS54Parameters)(nil).SizeBytes()) 424 SizeofNVOS56Parameters = uint32((*NVOS56Parameters)(nil).SizeBytes()) 425 SizeofNVOS64Parameters = uint32((*NVOS64Parameters)(nil).SizeBytes()) 426 )