Skip to content

Commit

Permalink
Add //pkg/sentry/devices/nvproxy and //pkg/abi/nvgpu.
Browse files Browse the repository at this point in the history
Very few ioctls are initially implemented.

Updates #14

PiperOrigin-RevId: 529511917
  • Loading branch information
nixprime authored and gvisor-bot committed May 9, 2023
1 parent 68eae97 commit ea05eab
Show file tree
Hide file tree
Showing 15 changed files with 1,511 additions and 0 deletions.
19 changes: 19 additions & 0 deletions pkg/abi/nvgpu/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
load("//tools:defs.bzl", "go_library")

package(default_applicable_licenses = ["//:license"])

licenses(["notice"])

go_library(
name = "nvgpu",
srcs = [
"classes.go",
"ctrl.go",
"frontend.go",
"frontend_unsafe.go",
"nvgpu.go",
"uvm.go",
],
marshal = True,
visibility = ["//pkg/sentry:internal"],
)
49 changes: 49 additions & 0 deletions pkg/abi/nvgpu/classes.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
// Copyright 2023 The gVisor Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package nvgpu

// Class handles, from src/nvidia/generated/g_allclasses.h.
const (
NV01_ROOT = 0x00000000
NV01_ROOT_NON_PRIV = 0x00000001
NV01_ROOT_CLIENT = 0x00000041
NV01_DEVICE_0 = 0x00000080
NV20_SUBDEVICE_0 = 0x00002080
)

// NV0080_ALLOC_PARAMETERS is the alloc params type for NV01_DEVICE_0, from
// src/common/sdk/nvidia/inc/class/cl0080.h.
//
// +marshal
type NV0080_ALLOC_PARAMETERS struct {
DeviceID uint32
HClientShare Handle
HTargetClient Handle
HTargetDevice Handle
Flags uint32
Pad [4]byte
VASpaceSize uint64
VAStartInternal uint64
VALimitInternal uint64
VAMode uint32
}

// NV2080_ALLOC_PARAMETERS is the alloc params type for NV20_SUBDEVICE_0, from
// src/common/sdk/nvidia/inc/class/cl2080.h.
//
// +marshal
type NV2080_ALLOC_PARAMETERS struct {
SubDeviceID uint32
}
147 changes: 147 additions & 0 deletions pkg/abi/nvgpu/ctrl.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
// Copyright 2023 The gVisor Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package nvgpu

// From src/common/sdk/nvidia/inc/ctrl/ctrlxxxx.h:

// +marshal
type NVXXXX_CTRL_XXX_INFO struct {
Index uint32
Data uint32
}

// From src/common/sdk/nvidia/inc/ctrl/ctrl0000/ctrl0000client.h:
const (
NV0000_CTRL_CMD_CLIENT_SET_INHERITED_SHARE_POLICY = 0xd04
)

// From src/common/sdk/nvidia/inc/ctrl/ctrl0000/ctrl0000gpu.h:
const (
NV0000_CTRL_CMD_GPU_GET_ATTACHED_IDS = 0x201
NV0000_CTRL_CMD_GPU_GET_ID_INFO = 0x202
NV0000_CTRL_CMD_GPU_GET_ID_INFO_V2 = 0x205
NV0000_CTRL_CMD_GPU_GET_PROBED_IDS = 0x214
NV0000_CTRL_CMD_GPU_ATTACH_IDS = 0x215
NV0000_CTRL_CMD_GPU_DETACH_IDS = 0x216
NV0000_CTRL_CMD_GPU_GET_PCI_INFO = 0x21b
NV0000_CTRL_CMD_GPU_QUERY_DRAIN_STATE = 0x279
NV0000_CTRL_CMD_GPU_GET_MEMOP_ENABLE = 0x27b
)

// From src/common/sdk/nvidia/inc/ctrl/ctrl0000/ctrl0000syncgpuboost.h:
const (
NV0000_CTRL_CMD_SYNC_GPU_BOOST_GROUP_INFO = 0xa04
)

// From src/common/sdk/nvidia/inc/ctrl/ctrl0000/ctrl0000system.h:
const (
NV0000_CTRL_CMD_SYSTEM_GET_BUILD_VERSION = 0x101
)

// +marshal
type NV0000_CTRL_SYSTEM_GET_BUILD_VERSION_PARAMS struct {
SizeOfStrings uint32
Pad [4]byte
PDriverVersionBuffer P64
PVersionBuffer P64
PTitleBuffer P64
ChangelistNumber uint32
OfficialChangelistNumber uint32
}

// From src/common/sdk/nvidia/inc/ctrl/ctrl0080/ctrl0080fb.h:
const (
NV0080_CTRL_CMD_FB_GET_CAPS_V2 = 0x801307
)

// From src/common/sdk/nvidia/inc/ctrl/ctrl0080/ctrl0080gpu.h:
const (
NV0080_CTRL_CMD_GPU_GET_NUM_SUBDEVICES = 0x800280
NV0080_CTRL_CMD_GPU_QUERY_SW_STATE_PERSISTENCE = 0x800288
NV0080_CTRL_CMD_GPU_GET_VIRTUALIZATION_MODE = 0x800289
NV0080_CTRL_CMD_GPU_GET_CLASSLIST_V2 = 0x800292
)

// From src/common/sdk/nvidia/inc/ctrl/ctrl0080/ctrl0080gr.h:

// +marshal
type NV0080_CTRL_GR_ROUTE_INFO struct {
Flags uint32
Pad [4]byte
Route uint64
}

// From src/common/sdk/nvidia/inc/ctrl/ctrl0080/ctrl0080host.h:
const (
NV0080_CTRL_CMD_HOST_GET_CAPS_V2 = 0x801402
)

// From src/common/sdk/nvidia/inc/ctrl/ctrl2080/ctrl2080bus.h:
const (
NV2080_CTRL_CMD_BUS_GET_PCI_INFO = 0x20801801
NV2080_CTRL_CMD_BUS_GET_PCI_BAR_INFO = 0x20801803
NV2080_CTRL_CMD_BUS_GET_INFO_V2 = 0x20801823
NV2080_CTRL_CMD_BUS_GET_PCIE_SUPPORTED_GPU_ATOMICS = 0x2080182a
)

// From src/common/sdk/nvidia/inc/ctrl/ctrl2080/ctrl2080ce.h:
const (
NV2080_CTRL_CMD_CE_GET_ALL_CAPS = 0x20802a0a
)

// From src/common/sdk/nvidia/inc/ctrl/ctrl2080/ctrl2080fb.h:
const (
NV2080_CTRL_CMD_FB_GET_INFO_V2 = 0x20801303
)

// From src/common/sdk/nvidia/inc/ctrl/ctrl2080/ctrl2080gpu.h:
const (
NV2080_CTRL_CMD_GPU_GET_INFO_V2 = 0x20800102
NV2080_CTRL_CMD_GPU_GET_NAME_STRING = 0x20800110
NV2080_CTRL_CMD_GPU_GET_SIMULATION_INFO = 0x20800119
NV2080_CTRL_CMD_GPU_QUERY_ECC_STATUS = 0x2080012f
NV2080_CTRL_CMD_GPU_QUERY_COMPUTE_MODE_RULES = 0x20800131
NV2080_CTRL_CMD_GPU_GET_GID_INFO = 0x2080014a
NV2080_CTRL_CMD_GPU_GET_ENGINES_V2 = 0x20800170
NV2080_CTRL_CMD_GPU_GET_ACTIVE_PARTITION_IDS = 0x2080018b
NV2080_CTRL_CMD_GPU_GET_COMPUTE_POLICY_CONFIG = 0x20800195
)

// From src/common/sdk/nvidia/inc/ctrl/ctrl2080/ctrl2080gr.h:
const (
NV2080_CTRL_CMD_GR_GET_INFO = 0x20801201
NV2080_CTRL_CMD_GR_GET_GLOBAL_SM_ORDER = 0x2080121b
NV2080_CTRL_CMD_GR_GET_CAPS_V2 = 0x20801227
NV2080_CTRL_CMD_GR_GET_GPC_MASK = 0x2080122a
NV2080_CTRL_CMD_GR_GET_TPC_MASK = 0x2080122b
)

// +marshal
type NV2080_CTRL_CMD_GR_GET_INFO_PARAMS struct {
GRInfoListSize uint32 // in elements
Pad [4]byte
GRInfoList P64
GRRouteInfo NV0080_CTRL_GR_ROUTE_INFO
}

// From src/common/sdk/nvidia/inc/ctrl/ctrl2080/ctrl2080mc.h:
const (
NV2080_CTRL_CMD_MC_GET_ARCH_INFO = 0x20801701
)

// From src/common/sdk/nvidia/inc/ctrl/ctrl2080/ctrl2080tmr.h:
const (
NV2080_CTRL_CMD_TIMER_GET_GPU_CPU_TIME_CORRELATION_INFO = 0x20800406
)
122 changes: 122 additions & 0 deletions pkg/abi/nvgpu/frontend.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
// Copyright 2023 The gVisor Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package nvgpu

// NV_IOCTL_MAGIC is the "canonical" IOC_TYPE for frontend ioctls.
// The driver ignores IOC_TYPE, allowing any value to be passed.
const NV_IOCTL_MAGIC = uint32('F')

// Frontend ioctl numbers.
// Note that these are only the IOC_NR part of the ioctl command.
const (
// From kernel-open/common/inc/nv-ioctl-numbers.h:
NV_IOCTL_BASE = 200
NV_ESC_CARD_INFO = NV_IOCTL_BASE + 0
NV_ESC_REGISTER_FD = NV_IOCTL_BASE + 1
NV_ESC_CHECK_VERSION_STR = NV_IOCTL_BASE + 10
NV_ESC_SYS_PARAMS = NV_IOCTL_BASE + 14

// From kernel-open/common/inc/nv-ioctl-numa.h:
NV_ESC_NUMA_INFO = NV_IOCTL_BASE + 15

// From src/nvidia/arch/nvalloc/unix/include/nv_escape.h:
NV_ESC_RM_FREE = 0x29
NV_ESC_RM_CONTROL = 0x2a
NV_ESC_RM_ALLOC = 0x2b
)

// Frontend ioctl parameter structs.
// NV_ESC_RM_* ioctl parameter structs are from
// src/common/sdk/nvidia/inc/nvos.h.
// Other ioctl parameter structs are from kernel-open/common/inc/nv-ioctl.h.

// IoctlRegisterFD is nv_ioctl_register_fd_t, the parameter type for
// NV_ESC_REGISTER_FD.
//
// +marshal
type IoctlRegisterFD struct {
CtlFD int32
}

// RMAPIVersion is nv_rm_api_version_t, the parameter type for
// NV_ESC_CHECK_VERSION_STR.
//
// +marshal
type RMAPIVersion struct {
Cmd uint32
Reply uint32
VersionString [64]byte
}

// IoctlSysParams is nv_ioctl_sys_params_t, the parameter type for
// NV_ESC_SYS_PARAMS.
//
// +marshal
type IoctlSysParams struct {
MemblockSize uint64
}

// NVOS00Parameters is NVOS00_PARAMETERS, the parameter type for
// NV_ESC_RM_FREE.
//
// +marshal
type NVOS00Parameters struct {
HRoot Handle
HObjectParent Handle
HObjectOld Handle
Status uint32
}

// NVOS21Parameters is NVOS21_PARAMETERS, one possible parameter type for
// NV_ESC_RM_ALLOC.
//
// +marshal
type NVOS21Parameters struct {
HRoot Handle
HObjectParent Handle
HObjectNew Handle
HClass uint32
PAllocParms P64
Status uint32
}

// NVOS54Parameters is NVOS54_PARAMETERS, the parameter type for
// NV_ESC_RM_CONTROL.
//
// +marshal
type NVOS54Parameters struct {
HClient Handle
HObject Handle
Cmd uint32
Flags uint32
Params P64
ParamsSize uint32
Status uint32
}

// NVOS64Parameters is NVOS64_PARAMETERS, one possible parameter type for
// NV_ESC_RM_ALLOC.
//
// +marshal
type NVOS64Parameters struct {
HRoot Handle
HObjectParent Handle
HObjectNew Handle
HClass uint32
PAllocParms P64
PRightsRequested P64
Flags uint32
Status uint32
}
30 changes: 30 additions & 0 deletions pkg/abi/nvgpu/frontend_unsafe.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// Copyright 2023 The gVisor Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package nvgpu

import (
"unsafe"
)

// Frontend ioctl parameter struct sizes.
const (
SizeofIoctlRegisterFD = unsafe.Sizeof(IoctlRegisterFD{})
SizeofRMAPIVersion = unsafe.Sizeof(RMAPIVersion{})
SizeofIoctlSysParams = unsafe.Sizeof(IoctlSysParams{})
SizeofNVOS00Parameters = unsafe.Sizeof(NVOS00Parameters{})
SizeofNVOS21Parameters = unsafe.Sizeof(NVOS21Parameters{})
SizeofNVOS54Parameters = unsafe.Sizeof(NVOS54Parameters{})
SizeofNVOS64Parameters = unsafe.Sizeof(NVOS64Parameters{})
)
Loading

0 comments on commit ea05eab

Please sign in to comment.