GVM User Suite
User tools for the GVM open source project.
device.c
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2022 2666680 Ontario Inc.
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  *
18  */
19 #include <gpu/nvidia/device.h>
20 #include <utils/device.h>
21 
22 #include <stdio.h>
23 #include <sys/stat.h>
24 #include <sys/types.h>
25 #include <fcntl.h>
26 #include <unistd.h>
27 
28 #define MAKE_DEV(major, minor) \
29  ((dev_t) ((minor & 0xff) | (major << 8) | ((minor & ~0xff) << 12)))
30 
33 int nv_open(
34  int32_t major,
35  uint16_t minor,
36  const char* path,
37  const char* proc_path
38 )
39 {
40  if (major == -1)
41  return -1;
42 
43  int fd = -1;
44  dev_t dev = MAKE_DEV(major, minor);
45  uid_t uid = get_param(proc_path, "DeviceFileUID");
46  gid_t gid = get_param(proc_path, "DeviceFileGID");
47  mode_t mode = get_param(proc_path, "DeviceFileMode");
48 
49  if (!device_exists(path)) {
50  if (mknod(path, mode | S_IFCHR, dev) == -1) {
51  return fd;
52  }
53  }
54 
55  if (chown(path, uid, gid) == -1)
56  return fd;
57 
58  fd = open(path, O_RDWR | O_CLOEXEC);
59 
60  return fd;
61 }
62 
65 int nv_open_dev(uint16_t minor)
66 {
67  const char *params = "/proc/driver/nvidia/params";
68 
69  int32_t major = get_major("nvidia-frontend");
70  char path[1024] = "";
71 
72  if (minor == 255)
73  sprintf(path, "/dev/nvidiactl");
74  else
75  sprintf(path, "/dev/nvidia%d", minor);
76 
77  return nv_open(major, minor, path, params);
78 }
79 
82 int nv_open_mdev(uint16_t minor)
83 {
84  const char *params = "/proc/driver/nvidia/params";
85 
86  int32_t major = get_major("nvidia-vgpu-vfio");
87  char path[1024] = "";
88 
89  sprintf(path, "/dev/nvidia-vgpu%d", minor);
90 
91  return nv_open(major, minor, path, params);
92 }
int nv_open_dev(uint16_t minor)
Opens a traditional NVIDIA device.
Definition: device.c:65
int nv_open(int32_t major, uint16_t minor, const char *path, const char *proc_path)
Opens a Nvidia device.
Definition: device.c:33
int nv_open_mdev(uint16_t minor)
Opens a Mediated NVIDIA device.
Definition: device.c:82
#define MAKE_DEV(major, minor)
Definition: device.c:28
uint8_t device_exists(const char *device_name)
Does a device file exist?
Definition: device.c:30
int32_t get_major(const char *device)
Gets the device major id from /proc/devices.
Definition: device.c:42
int32_t get_param(const char *filename, const char *parameter)
Gets a parameter from a driver param file.
Definition: device.c:71