GVM User Suite
User tools for the GVM open source project.
manager.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 <gpu/nvidia/resman/api.h>
23 
24 #include <gvm/nvidia/init.h>
25 #include <gvm/nvidia/manager.h>
26 
27 #include <sys/select.h>
28 
29 #include <stdlib.h>
30 #include <string.h>
31 #include <unistd.h>
32 
34 const uint32_t RM_EVENT = 4;
35 
37 static inline uint32_t compose_vm_id(uint32_t t, uint32_t i)
38 {
39  return 0xDAE00000 | (t << 8) | i;
40 }
41 
53 static int event_init(
54  int ctl_fd,
55  struct NvResource* res,
56  uint32_t event,
57  uint32_t notify,
58  uint32_t flags
59 )
60 {
61  int fd = nv_open_dev(255);
62 
63  rm_alloc_os_event(fd, res->client, res->object);
64 
65  struct RmAllocEvent rm_event = {};
66 
67  rm_event.parent = res->client;
68  rm_event.event_class = 0x00000079;
69  rm_event.notify = notify | flags;
70  rm_event.event_data = fd;
71 
72  rm_alloc_res(ctl_fd, res, event, 0x00000005, &rm_event);
73 
74  struct RmSetNotification set_notify = {};
75 
76  set_notify.event = notify;
77  set_notify.action = 2;
78 
79  RM_CTRL(ctl_fd, res, 0x00000501, set_notify);
80 
81  return fd;
82 }
83 
86 struct VmMgr init_nv_vm_mgr(struct NvMdev* mgr)
87 {
88  const uint32_t event_start = compose_vm_id(RM_EVENT, 0);
89  const uint32_t event_bind = compose_vm_id(RM_EVENT, 1);
90 
91  struct VmMgr ret = {};
92 
93  for (int i = 0; i < 32 && mgr->gpus[i] != NULL; ++i) {
94  struct NvMdevGpu* gpu = mgr->gpus[i];
95 
96  uint32_t persistence = 0;
97 
98  RM_CTRL(gpu->ctl_fd, gpu->dev, 0x00800288, persistence);
99 
100  if (persistence == 1) {
101  persistence = 0;
102  RM_CTRL(gpu->ctl_fd, gpu->dev, 0x00800287, persistence);
103  }
104  }
105 
106  printf("Persistence set correctly.\n");
107 
108  ret.event_start = event_init(mgr->fd, mgr->res, event_start, 2, 0x10000000);
109  ret.event_bind = event_init(mgr->fd, mgr->res, event_bind, 3, 0x10000000);
110  ret.root = mgr->res->client;
111 
112  printf("Events initialized for start and bind VM.\n");
113 
114  return ret;
115 }
116 
119 void handle_vm_start(struct VmMgr* mgr, struct NvMdev* mdev_mgr)
120 {
121  int n_fds;
122  fd_set read_fds;
123 
124  FD_ZERO(&read_fds);
125 
126  FD_SET(mgr->event_start, &read_fds);
127  FD_SET(mgr->event_bind, &read_fds);
128 
129  n_fds = (mgr->event_start > mgr->event_bind ? mgr->event_start : mgr->event_bind) + 1;
130 
131  select(n_fds, &read_fds, NULL, NULL, NULL);
132 
133  if (FD_ISSET(mgr->event_start, &read_fds)) {
134  printf("Got a start request from the NVIDIA kernel module\n");
135  start_vm(mgr, mdev_mgr);
136  } else if (FD_ISSET(mgr->event_bind, &read_fds)) {
137  printf("Got a bind request from the NVIDIA kernel module\n");
138  }
139 }
140 
143 void start_vm(struct VmMgr* mgr, struct NvMdev* mdev_mgr)
144 {
145  struct RmVmStartInfo vm_start_info = {};
146 
147  RM_CTRL(mdev_mgr->fd, mdev_mgr->res, 0x00000C01, vm_start_info);
148 
149  struct UUID uuid = vm_start_info.uuid;
150  struct UUID vm_uuid = {};
151 
152  vm_uuid.time_low = 0xEE1AEACA;
153  vm_uuid.time_mid = 0xFD56;
154  vm_uuid.time_hi_and_version = 0x470F;
155  vm_uuid.clock_seq_hi_and_reserved = 0xBB;
156  vm_uuid.clock_seq_low = 0x85;
157  vm_uuid.node[0] = 0x1E;
158  vm_uuid.node[1] = 0xF6;
159  vm_uuid.node[2] = 0x67;
160  vm_uuid.node[3] = 0x54;
161  vm_uuid.node[4] = 0x9D;
162  vm_uuid.node[5] = 0xE5;
163 
164  printf(
165  "MDEV UUID: %.8X-%.4X-%.4X-%.2X%.2X-%.2X%.2X%.2X%.2X%.2X%.2X\n"
166  "Got config \"%s\" for QEMU PID 0x%.8X\n"
167  "Starting VM...\n",
168  uuid.time_low, uuid.time_mid, uuid.time_hi_and_version,
170  uuid.node[0], uuid.node[1], uuid.node[2], uuid.node[3],
171  uuid.node[4], uuid.node[5],
172  vm_start_info.config,
173  vm_start_info.qemu_pid
174  );
175 
176  struct RmVmNotifyStart notify_start = {};
177 
178  notify_start.mdev = uuid;
179  notify_start.vm = vm_uuid;
180  strcpy(notify_start.name, "GVM VM");
181 
182  printf("Opened /dev/nvidia-vgpu%d\n", vm_start_info.mdev_id);
183 
184  mgr->mdev_fd = nv_open_mdev(vm_start_info.mdev_id);
185 
186  for (int i = 0; i < 32 && mdev_mgr->gpus[i] != NULL; ++i) {
187  struct NvMdevGpu* gpu = mdev_mgr->gpus[i];
188 
189  if (gpu->gpu->identifier != vm_start_info.pci_id)
190  continue;
191 
192  RM_CTRL(mgr->mdev_fd, gpu->mdev, 0xA0810107, notify_start);
193  printf("Started VM\n");
194  break;
195  }
196 }
uint8_t rm_alloc_os_event(int fd, uint32_t client_id, uint32_t device_id)
Allocates an Operating System Event.
Definition: api.c:208
#define RM_CTRL(fd, res, cmd, data)
Controls a RM Resource.
Definition: api.h:133
struct NvResource * rm_alloc_res(int fd, struct NvResource *parent, uint32_t object, uint32_t rm_class, void *data)
Allocates a Node for a resource.
Definition: api.c:65
int nv_open_dev(uint16_t minor)
Opens a traditional NVIDIA device.
Definition: device.c:65
int nv_open_mdev(uint16_t minor)
Opens a Mediated NVIDIA device.
Definition: device.c:82
struct VmMgr init_nv_vm_mgr(struct NvMdev *mgr)
Initializes a NVIDIA VM manager to connect to a VM.
Definition: manager.c:86
static uint32_t compose_vm_id(uint32_t t, uint32_t i)
Inline function to create VM specific Event IDs.
Definition: manager.c:37
const uint32_t RM_EVENT
RM Event ID.
Definition: manager.c:34
void handle_vm_start(struct VmMgr *mgr, struct NvMdev *mdev_mgr)
Handles a start request.
Definition: manager.c:119
void start_vm(struct VmMgr *mgr, struct NvMdev *mdev_mgr)
Connects the VM to the user program.
Definition: manager.c:143
static int event_init(int ctl_fd, struct NvResource *res, uint32_t event, uint32_t notify, uint32_t flags)
Code to initialize events for the VM manager.
Definition: manager.c:53
uint32_t identifier
Identifier for the GPU.
Definition: mdev.h:42
Control Mechanism for the NVIDIA GPU.
Definition: resources.h:56
struct Gpu * gpu
GPU structure corresponding to the GPU.
Definition: resources.h:60
Structure for managing the mediated stack.
Definition: resources.h:74
int fd
Control file descriptor.
Definition: resources.h:75
struct NvMdevGpu * gpus[32]
Available GPUs.
Definition: resources.h:76
struct NvResource * res
Resource tree.
Definition: resources.h:77
Resource for managing NVIDIA kernel module objects.
Definition: resources.h:39
uint32_t client
All resources require a client/root.
Definition: resources.h:43
uint32_t object
Object of the resource.
Definition: resources.h:45
Allocates a RM Event.
Definition: types.h:68
uint32_t parent
Parent of the event.
Definition: types.h:69
uint64_t event_data
Data for the event.
Definition: types.h:73
uint32_t notify
Notification of the event.
Definition: types.h:72
uint32_t event_class
Class of the event.
Definition: types.h:71
Sets the type of notification to use.
Definition: types.h:80
uint32_t action
Action to use for the notification.
Definition: types.h:82
uint32_t event
Event to set the notification structure.
Definition: types.h:81
Notifies the start of a VM.
Definition: init.h:48
char name[128]
VM Name.
Definition: init.h:51
struct UUID vm
VM UUID.
Definition: init.h:50
struct UUID mdev
MDEV UUID.
Definition: init.h:49
Reverse engineered start vm info structure.
Definition: init.h:35
char config[1024]
Config for starting the MDEV.
Definition: init.h:37
uint32_t qemu_pid
QEMU PID.
Definition: init.h:38
struct UUID uuid
UUID for the MDEV.
Definition: init.h:36
uint32_t pci_id
PCI id.
Definition: init.h:39
uint16_t mdev_id
MDEV id.
Definition: init.h:40
UUID Data structure.
Definition: types.h:32
uint16_t time_mid
Definition: types.h:34
uint32_t time_low
Definition: types.h:33
uint8_t clock_seq_hi_and_reserved
Definition: types.h:36
uint8_t node[6]
Definition: types.h:38
uint16_t time_hi_and_version
Definition: types.h:35
uint8_t clock_seq_low
Definition: types.h:37
Structure for VM management.
Definition: vm_mgr.h:33
int event_bind
Event bind fd.
Definition: vm_mgr.h:35
int mdev_fd
MDEV file descriptor.
Definition: vm_mgr.h:37
uint32_t root
Root of the VM manager.
Definition: vm_mgr.h:36
int event_start
Event start fd.
Definition: vm_mgr.h:34