gvisor.dev/gvisor@v0.0.0-20240520182842-f9d4d51c7e0f/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 "gvisor.dev/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 // GetFrontendFD implements HasFrontendFD.GetFrontendFD. 78 func (p *IoctlAllocOSEvent) GetFrontendFD() int32 { 79 return int32(p.FD) 80 } 81 82 // SetFrontendFD implements HasFrontendFD.SetFrontendFD. 83 func (p *IoctlAllocOSEvent) SetFrontendFD(fd int32) { 84 p.FD = uint32(fd) 85 } 86 87 // IoctlFreeOSEvent is nv_ioctl_free_os_event_t, the parameter type for 88 // NV_ESC_FREE_OS_EVENT. 89 // 90 // +marshal 91 type IoctlFreeOSEvent struct { 92 HClient Handle 93 HDevice Handle 94 FD uint32 95 Status uint32 96 } 97 98 // GetFrontendFD implements HasFrontendFD.GetFrontendFD. 99 func (p *IoctlFreeOSEvent) GetFrontendFD() int32 { 100 return int32(p.FD) 101 } 102 103 // SetFrontendFD implements HasFrontendFD.SetFrontendFD. 104 func (p *IoctlFreeOSEvent) SetFrontendFD(fd int32) { 105 p.FD = uint32(fd) 106 } 107 108 // RMAPIVersion is nv_rm_api_version_t, the parameter type for 109 // NV_ESC_CHECK_VERSION_STR. 110 // 111 // +marshal 112 type RMAPIVersion struct { 113 Cmd uint32 114 Reply uint32 115 VersionString [64]byte 116 } 117 118 // IoctlSysParams is nv_ioctl_sys_params_t, the parameter type for 119 // NV_ESC_SYS_PARAMS. 120 // 121 // +marshal 122 type IoctlSysParams struct { 123 MemblockSize uint64 124 } 125 126 // IoctlWaitOpenComplete is nv_ioctl_wait_open_complete_t, the parameter type 127 // for NV_ESC_WAIT_OPEN_COMPLETE. 128 // 129 // +marshal 130 type IoctlWaitOpenComplete struct { 131 Rc int32 132 AdapterStatus uint32 133 } 134 135 // IoctlNVOS02ParametersWithFD is nv_ioctl_nvos2_parameters_with_fd, the 136 // parameter type for NV_ESC_RM_ALLOC_MEMORY. 137 // 138 // +marshal 139 type IoctlNVOS02ParametersWithFD struct { 140 Params NVOS02Parameters 141 FD int32 142 Pad0 [4]byte 143 } 144 145 // +marshal 146 type NVOS02Parameters struct { 147 HRoot Handle 148 HObjectParent Handle 149 HObjectNew Handle 150 HClass ClassID 151 Flags uint32 152 Pad0 [4]byte 153 PMemory P64 // address of application mapping, without indirection 154 Limit uint64 155 Status uint32 156 Pad1 [4]byte 157 } 158 159 // NVOS00Parameters is NVOS00_PARAMETERS, the parameter type for 160 // NV_ESC_RM_FREE. 161 // 162 // +marshal 163 type NVOS00Parameters struct { 164 HRoot Handle 165 HObjectParent Handle 166 HObjectOld Handle 167 Status uint32 168 } 169 170 // RmAllocParamType should be implemented by all possible parameter types for 171 // NV_ESC_RM_ALLOC. 172 type RmAllocParamType interface { 173 GetPAllocParms() P64 174 GetPRightsRequested() P64 175 SetPAllocParms(p P64) 176 SetPRightsRequested(p P64) 177 FromOS64(other NVOS64Parameters) 178 ToOS64() NVOS64Parameters 179 GetPointer() uintptr 180 marshal.Marshallable 181 } 182 183 // GetRmAllocParamObj returns the appropriate implementation of 184 // RmAllocParamType based on passed parameters. 185 func GetRmAllocParamObj(isNVOS64 bool) RmAllocParamType { 186 if isNVOS64 { 187 return &NVOS64Parameters{} 188 } 189 return &NVOS21Parameters{} 190 } 191 192 // NVOS21Parameters is NVOS21_PARAMETERS, one possible parameter type for 193 // NV_ESC_RM_ALLOC. 194 // 195 // +marshal 196 type NVOS21Parameters struct { 197 HRoot Handle 198 HObjectParent Handle 199 HObjectNew Handle 200 HClass ClassID 201 PAllocParms P64 202 ParamsSize uint32 203 Status uint32 204 } 205 206 // GetPAllocParms implements RmAllocParamType.GetPAllocParms. 207 func (n *NVOS21Parameters) GetPAllocParms() P64 { 208 return n.PAllocParms 209 } 210 211 // GetPRightsRequested implements RmAllocParamType.GetPRightsRequested. 212 func (n *NVOS21Parameters) GetPRightsRequested() P64 { 213 return 0 214 } 215 216 // SetPAllocParms implements RmAllocParamType.SetPAllocParms. 217 func (n *NVOS21Parameters) SetPAllocParms(p P64) { n.PAllocParms = p } 218 219 // SetPRightsRequested implements RmAllocParamType.SetPRightsRequested. 220 func (n *NVOS21Parameters) SetPRightsRequested(p P64) { 221 panic("impossible") 222 } 223 224 // FromOS64 implements RmAllocParamType.FromOS64. 225 func (n *NVOS21Parameters) FromOS64(other NVOS64Parameters) { 226 n.HRoot = other.HRoot 227 n.HObjectParent = other.HObjectParent 228 n.HObjectNew = other.HObjectNew 229 n.HClass = other.HClass 230 n.PAllocParms = other.PAllocParms 231 n.ParamsSize = other.ParamsSize 232 n.Status = other.Status 233 } 234 235 // ToOS64 implements RmAllocParamType.ToOS64. 236 func (n *NVOS21Parameters) ToOS64() NVOS64Parameters { 237 return NVOS64Parameters{ 238 HRoot: n.HRoot, 239 HObjectParent: n.HObjectParent, 240 HObjectNew: n.HObjectNew, 241 HClass: n.HClass, 242 PAllocParms: n.PAllocParms, 243 ParamsSize: n.ParamsSize, 244 Status: n.Status, 245 } 246 } 247 248 // NVOS55Parameters is NVOS55_PARAMETERS, the parameter type for 249 // NV_ESC_RM_DUP_OBJECT. 250 // 251 // +marshal 252 type NVOS55Parameters struct { 253 HClient Handle 254 HParent Handle 255 HObject Handle 256 HClientSrc Handle 257 HObjectSrc Handle 258 Flags uint32 259 Status uint32 260 } 261 262 // NVOS57Parameters is NVOS57_PARAMETERS, the parameter type for 263 // NV_ESC_RM_SHARE. 264 // 265 // +marshal 266 type NVOS57Parameters struct { 267 HClient Handle 268 HObject Handle 269 SharePolicy RS_SHARE_POLICY 270 Status uint32 271 } 272 273 // NVOS32Parameters is NVOS32_PARAMETERS, the parameter type for 274 // NV_ESC_RM_VID_HEAP_CONTROL. 275 // 276 // +marshal 277 type NVOS32Parameters struct { 278 HRoot Handle 279 HObjectParent Handle 280 Function uint32 281 HVASpace Handle 282 IVCHeapNumber int16 283 Pad [2]byte 284 Status uint32 285 Total uint64 286 Free uint64 287 Data [144]byte // union 288 } 289 290 // Possible values for NVOS32Parameters.Function: 291 const ( 292 NVOS32_FUNCTION_ALLOC_SIZE = 2 293 ) 294 295 // NVOS32AllocSize is the type of NVOS32Parameters.Data for 296 // NVOS32_FUNCTION_ALLOC_SIZE. 297 type NVOS32AllocSize struct { 298 Owner uint32 299 HMemory Handle 300 Type uint32 301 Flags uint32 302 Attr uint32 303 Format uint32 304 ComprCovg uint32 305 ZcullCovg uint32 306 PartitionStride uint32 307 Width uint32 308 Height uint32 309 Pad0 [4]byte 310 Size uint64 311 Alignment uint64 312 Offset uint64 313 Limit uint64 314 Address P64 315 RangeBegin uint64 316 RangeEnd uint64 317 Attr2 uint32 318 CtagOffset uint32 319 } 320 321 // IoctlNVOS33ParametersWithFD is nv_ioctl_nvos33_parameters_with_fd, the 322 // parameter type for NV_ESC_RM_MAP_MEMORY, from 323 // src/nvidia/arch/nvalloc/unix/include/nv-unix-nvos-params-wrappers.h. 324 // 325 // +marshal 326 type IoctlNVOS33ParametersWithFD struct { 327 Params NVOS33Parameters 328 FD int32 329 Pad0 [4]byte 330 } 331 332 // +marshal 333 type NVOS33Parameters struct { 334 HClient Handle 335 HDevice Handle 336 HMemory Handle 337 Pad0 [4]byte 338 Offset uint64 339 Length uint64 340 PLinearAddress P64 // address of application mapping, without indirection 341 Status uint32 342 Flags uint32 343 } 344 345 // NVOS34Parameters is NVOS34_PARAMETERS, the parameter type for 346 // NV_ESC_RM_UNMAP_MEMORY. 347 // 348 // +marshal 349 type NVOS34Parameters struct { 350 HClient Handle 351 HDevice Handle 352 HMemory Handle 353 Pad0 [4]byte 354 PLinearAddress P64 // address of application mapping, without indirection 355 Status uint32 356 Flags uint32 357 } 358 359 // NVOS54Parameters is NVOS54_PARAMETERS, the parameter type for 360 // NV_ESC_RM_CONTROL. 361 // 362 // +marshal 363 type NVOS54Parameters struct { 364 HClient Handle 365 HObject Handle 366 Cmd uint32 367 Flags uint32 368 Params P64 369 ParamsSize uint32 370 Status uint32 371 } 372 373 // NVOS56Parameters is NVOS56_PARAMETERS, the parameter type for 374 // NV_ESC_RM_UPDATE_DEVICE_MAPPING_INFO. 375 // 376 // +marshal 377 type NVOS56Parameters struct { 378 HClient Handle 379 HDevice Handle 380 HMemory Handle 381 Pad0 [4]byte 382 POldCPUAddress P64 383 PNewCPUAddress P64 384 Status uint32 385 Pad1 [4]byte 386 } 387 388 // NVOS64Parameters is NVOS64_PARAMETERS, one possible parameter type for 389 // NV_ESC_RM_ALLOC. 390 // 391 // +marshal 392 // +stateify savable 393 type NVOS64Parameters struct { 394 HRoot Handle 395 HObjectParent Handle 396 HObjectNew Handle 397 HClass ClassID 398 PAllocParms P64 399 PRightsRequested P64 400 ParamsSize uint32 401 Flags uint32 402 Status uint32 403 _ uint32 404 } 405 406 // GetPAllocParms implements RmAllocParamType.GetPAllocParms. 407 func (n *NVOS64Parameters) GetPAllocParms() P64 { 408 return n.PAllocParms 409 } 410 411 // GetPRightsRequested implements RmAllocParamType.GetPRightsRequested. 412 func (n *NVOS64Parameters) GetPRightsRequested() P64 { 413 return n.PRightsRequested 414 } 415 416 // SetPAllocParms implements RmAllocParamType.SetPAllocParms. 417 func (n *NVOS64Parameters) SetPAllocParms(p P64) { n.PAllocParms = p } 418 419 // SetPRightsRequested implements RmAllocParamType.SetPRightsRequested. 420 func (n *NVOS64Parameters) SetPRightsRequested(p P64) { n.PRightsRequested = p } 421 422 // FromOS64 implements RmAllocParamType.FromOS64. 423 func (n *NVOS64Parameters) FromOS64(other NVOS64Parameters) { *n = other } 424 425 // ToOS64 implements RmAllocParamType.ToOS64. 426 func (n *NVOS64Parameters) ToOS64() NVOS64Parameters { return *n } 427 428 // HasFrontendFD is a type constraint for parameter structs containing a 429 // frontend FD field. This is necessary because, as of this writing (Go 1.20), 430 // there is no way to enable field access using a Go type constraint. 431 type HasFrontendFD interface { 432 GetFrontendFD() int32 433 SetFrontendFD(int32) 434 } 435 436 // Frontend ioctl parameter struct sizes. 437 var ( 438 SizeofIoctlRegisterFD = uint32((*IoctlRegisterFD)(nil).SizeBytes()) 439 SizeofIoctlAllocOSEvent = uint32((*IoctlAllocOSEvent)(nil).SizeBytes()) 440 SizeofIoctlFreeOSEvent = uint32((*IoctlFreeOSEvent)(nil).SizeBytes()) 441 SizeofRMAPIVersion = uint32((*RMAPIVersion)(nil).SizeBytes()) 442 SizeofIoctlSysParams = uint32((*IoctlSysParams)(nil).SizeBytes()) 443 SizeofIoctlWaitOpenComplete = uint32((*IoctlWaitOpenComplete)(nil).SizeBytes()) 444 SizeofIoctlNVOS02ParametersWithFD = uint32((*IoctlNVOS02ParametersWithFD)(nil).SizeBytes()) 445 SizeofNVOS00Parameters = uint32((*NVOS00Parameters)(nil).SizeBytes()) 446 SizeofNVOS21Parameters = uint32((*NVOS21Parameters)(nil).SizeBytes()) 447 SizeofIoctlNVOS33ParametersWithFD = uint32((*IoctlNVOS33ParametersWithFD)(nil).SizeBytes()) 448 SizeofNVOS55Parameters = uint32((*NVOS55Parameters)(nil).SizeBytes()) 449 SizeofNVOS57Parameters = uint32((*NVOS57Parameters)(nil).SizeBytes()) 450 SizeofNVOS32Parameters = uint32((*NVOS32Parameters)(nil).SizeBytes()) 451 SizeofNVOS34Parameters = uint32((*NVOS34Parameters)(nil).SizeBytes()) 452 SizeofNVOS54Parameters = uint32((*NVOS54Parameters)(nil).SizeBytes()) 453 SizeofNVOS56Parameters = uint32((*NVOS56Parameters)(nil).SizeBytes()) 454 SizeofNVOS64Parameters = uint32((*NVOS64Parameters)(nil).SizeBytes()) 455 )