-
Notifications
You must be signed in to change notification settings - Fork 3
/
devicebus.c
410 lines (341 loc) · 9.31 KB
/
devicebus.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <signal.h>
#include <sys/wait.h>
#include <sys/types.h>
#include <sys/socket.h>
#include "vmm.h"
#include "apic.h"
#include "devicebus.h"
#include "iostructs.h"
extern hv_t *g_hv;
extern uint32_t g_nvcpus;
extern char **environ;
device_node_t *g_devicelist = NULL;
#define LINK_NODE(l, n) do {\
if (l) {\
n->next = l;\
}\
l = n;\
} while (0)
#define LINK_DEVICE(d) LINK_NODE(g_devicelist, d)
#define LINK_IOPORT(d, i) LINK_NODE(d->ioports, i)
#define LINK_MMIO(d, m) LINK_NODE(d->mmios, m)
device_node_t *deviceNodeForName(char *path) {
device_node_t *cur = g_devicelist;
for(;cur;cur=cur->next) {
if (!strncmp(cur->path, path, strlen(cur->path)))
return cur;
}
return NULL;
}
int dbusConfigFromFile(char *path) {
FILE *config = fopen(path, "r");
if (config == NULL) {
perror("Failed to open device config");
return -1;
}
int ret = 0;
char device[256];
unsigned port_start, port_len;
unsigned mmio_start, mmio_len;
device_node_t *devnode;
do {
port_start = port_len = mmio_start = mmio_len = 0;
ret = fscanf(config, "%256s %x %x %x %x\n",
device, &port_start, &port_len, &mmio_start, &mmio_len);
devnode = deviceNodeForName(device);
if (!devnode) {
devnode = calloc(1, sizeof(device_node_t));
devnode->path = strdup(device);
LINK_DEVICE(devnode);
}
if (port_start) {
ioport_range_t *i = calloc(1, sizeof(ioport_range_t));
if (i == NULL) {
perror("Failed to allocate config node");
goto err;
}
i->start_port = port_start;
i->nports = port_len;
LINK_IOPORT(devnode, i);
}
if (mmio_start) {
mmio_range_t *m = calloc(1, sizeof(mmio_range_t));
if (m == NULL) {
perror("Failed to allocate config node");
goto err;
}
m->mmio_start = mmio_start;
m->mmio_len = mmio_len;
LINK_MMIO(devnode, m);
}
} while (ret > 0);
return 0;
err:
devnode = g_devicelist;
for(;devnode;) {
device_node_t *next = devnode->next;
ioport_range_t *io = devnode->ioports;
for(;io;) {
ioport_range_t *tmp = io->next;
free(io);
io = tmp;
}
mmio_range_t *mmio = devnode->mmios;
for(;mmio;) {
mmio_range_t *tmp = mmio->next;
free(mmio);
mmio = tmp;
}
free(devnode);
devnode = next;
}
return -1;
}
int instantiateDevice(device_node_t *devnode) {
char device_path[513] = {0};
// create the socketpair to be used to comm with the device
int i = 0;
int sv[NR_MAX_VCPUS][2] = {0};
for(i=0;i<g_nvcpus;i++) {
if (socketpair(AF_UNIX, SOCK_STREAM, 0, sv[i]) < 0) {
return -1;
}
}
pid_t child = fork();
if (!child) {
// close the vmm ends
for(i=0;i<g_nvcpus;i++) {
close(sv[i][0]);
}
close(g_hv->ioapic->s[0]);
snprintf(device_path, sizeof(device_path), "%s/%s",
DEVICE_BIN_DIR, devnode->path);
// MUST STAY AT THE TOP. Socketpair is 3,4
// so MEMFD will overwrite it if not dup'd first
if (dup2(g_hv->ioapic->s[1], CHILD_DEVICE_IOAPIC_FD) < 0) {
perror("Failed to dup ioapic socket");
}
// let's ensure it's a predictable fd for the child
if (dup2(sv[0][1], CHILD_DEVICE_CHANNEL_FD) < 0) {
perror("Failed to dup io comm socket");
}
if (dup2(g_hv->fw_memfd, CHILD_DEVICE_FW_MEMFD) < 0) {
perror("Failed to dup fw memfd");
}
if (dup2(g_hv->sys_memfd, CHILD_DEVICE_SYS_MEMFD) < 0) {
perror("Failed to dup sys memfd");
}
// TODO: we may want to setuid here and do some kind of sandboxing
char *argv[] = {device_path, NULL};
if (execve(device_path, argv, environ) < 0) {
perror("Failed to exec device bin");
}
exit(1);
}
devnode->instance_pid = child;
// wait for child to signal it's ready to receive IO
int handshake = 0;
if (recv(sv[0][0], &handshake, sizeof(handshake), MSG_WAITALL)
< sizeof(handshake)) {
perror("Failed to receive init payload\n");
goto failed;
}
if (strncmp((char *)&handshake, "INIT", sizeof(handshake))) {
fprintf(stderr, "Unexpected handshake from device\n");
goto failed;
}
struct init_response response = {0};
response.magic = *(uint32_t *)&"TINI";
for(i=0;i<g_nvcpus;i++) {
response.fds[i] = sv[i][1];
}
// now our end of the bargain
if ((send(sv[0][0], &response, sizeof(response), 0)) < sizeof(response)) {
perror("Failed to send tini payload\n");
goto failed;
}
// close the device ends, keeping this alive until sending the opaque values
for(i=0;i<g_nvcpus;i++) {
close(sv[i][1]);
}
for(i=0;i<g_nvcpus;i++) {
devnode->channel_fd[i] = sv[i][0];
}
return 0;
failed:
for(i=0;i<g_nvcpus;i++) {
close(sv[i][0]);
}
return -1;
}
void dbusTeardown() {
device_node_t *cur = g_devicelist;
for(;cur;cur=cur->next) {
pid_t device_pid = cur->instance_pid;
if (device_pid) {
kill(device_pid, SIGTERM);
}
}
}
int channelForDeviceUnlocked(vcpu_t *vcpu, device_node_t *devnode) {
int channel_fd = devnode->channel_fd[vcpu->id];
if (channel_fd) {
return channel_fd;
}
if (instantiateDevice(devnode) < 0) {
return -1;
}
return devnode->channel_fd[vcpu->id];
}
int channelForDevice(vcpu_t *vcpu, device_node_t *devnode) {
int ret = 0;
pthread_mutex_lock(&g_hv->bus_access_mutex);
ret = channelForDeviceUnlocked(vcpu, devnode);
pthread_mutex_unlock(&g_hv->bus_access_mutex);
return ret;
}
device_node_t *deviceForPort(uint16_t port) {
uint16_t start_port = 0;
size_t nports = 0;
device_node_t *cur = g_devicelist;
for(;cur;cur=cur->next) {
ioport_range_t *io = cur->ioports;
for(;io;io=io->next) {
start_port = io->start_port;
nports = io->nports;
if (port >= start_port && port < start_port + nports) {
return cur;
}
}
}
return NULL;
}
device_node_t *deviceForAddr(uint64_t addr) {
uint64_t start_addr, end_addr;
device_node_t *cur = g_devicelist;
for (;cur;cur=cur->next) {
mmio_range_t *mmio = cur->mmios;
for(;mmio;mmio=mmio->next) {
start_addr = mmio->mmio_start;
end_addr = start_addr + mmio->mmio_len;
if (addr >= start_addr && addr < end_addr) {
return cur;
}
}
}
return NULL;
}
int dbusHandlePioAccess(vcpu_t *vcpu,
uint16_t port,
uint8_t *data,
uint8_t direction,
uint8_t size,
uint32_t count) {
int channel_fd = 0;
device_node_t *devnode = NULL;;
// iterate over the config to find which device has this registered
devnode = deviceForPort(port);
if (devnode == NULL) {
return -1;
}
// find the channel_fd for that device, spinning up a new process for the
// device if it does not yet exist
channel_fd = channelForDevice(vcpu, devnode);
if (channel_fd < 0) {
fprintf(stderr, "Failed to retrieve channel fd in PIO\n");
}
// send a pio request over the wire
struct io_request io = {
.type = IOTYPE_PIO,
.ioport = {
.port = port,
.data = *(uint32_t *)data,
.direction = direction,
.size = size,
.count = count
}
};
if (send(channel_fd, &io, sizeof(io), 0) < 0) {
perror("Send IO to device");
return -1;
}
// wait for ack
int value = 0;
if (recv(channel_fd, &value, sizeof(value), MSG_WAITALL)
< sizeof(value)) {
perror("Failed to receive ack");
return -1;
}
// only if the direction is a read do we populate EAX
if (direction == IO_DIRECTION_IN) {
if (size == 1)
*data = value & 0xff;
else if (size == 2)
*((uint16_t *)data) = value & 0xffff;
else if (size == 4)
*((uint32_t *)data) = value;
else
return -1;
}
return 0;
}
int dbusHandleMmioAccess(vcpu_t *vcpu,
uint64_t phys_addr,
uint64_t *data,
uint32_t len,
uint8_t is_write) {
int channel_fd;
int value = 0;
device_node_t *devnode = NULL;
// let's check for any type of APIC access
// (IO OR L)
if (apicAccess(vcpu, phys_addr)) {
return apicMmio(vcpu, phys_addr, data, len, is_write);
}
// iterate over the config to find which device has this registered
devnode = deviceForAddr(phys_addr);
if (devnode == NULL) {
return -1;
}
// find the channel_fd for that device, spinning up a new process for the
// device if it does not yet exist
channel_fd = channelForDevice(vcpu, devnode);
if (channel_fd < 0) {
fprintf(stderr, "Failed to retrieve channel fd in MMIO\n");
}
struct io_request io = {
.type = IOTYPE_MMIO,
.mmio = {
.phys_addr = phys_addr,
.data = *(uint32_t *)data,
.len = len,
.is_write = is_write
}
};
if (send(channel_fd, &io, sizeof(io), 0) != sizeof(io)) {
perror("Send MMIO to device");
return -1;
}
// wait for ack / ret val
if (recv(channel_fd, &value, sizeof(value), MSG_WAITALL)
< sizeof(value)) {
perror("Failed to receive ack");
return -1;
}
// if it's a read, fill in kvm data
if (!is_write) {
if (len == 1)
*data = value & 0xff;
else if (len == 2)
*((uint16_t *)data) = value & 0xffff;
else if (len == 4)
*((uint32_t *)data) = value;
else
return -1;
}
return 0;
}