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