-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathvmm.c
547 lines (446 loc) · 12.3 KB
/
vmm.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
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
#define _GNU_SOURCE
#include <fcntl.h>
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <signal.h>
#include <pthread.h>
#include <strings.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <sys/types.h>
#include "vmm.h"
#include "devicebus.h"
#include "apic.h"
#define DEBUG 0
hv_t *g_hv = NULL;
uint32_t g_nvcpus = 0;
// initialize vCPU to x86 reset state
void x86CpuReset(vcpu_t *vcpu, bool bsp) {
x86_cpu_regs_t *regs = &vcpu->regs;
bzero(regs, sizeof(*regs));
regs->a20_mask = ~0;
// Cache Disable
// Extension Type
regs->control[0] = 0x60000010;
regs->idt.limit = 0xffff;
regs->gdt.limit = 0xffff;
regs->ldt.limit = 0xffff;
regs->ldt.flags = DESC_P_MASK | (2 << DESC_TYPE_SHIFT);
regs->tr.limit = 0xffff;
regs->tr.flags = DESC_P_MASK | (11 << DESC_TYPE_SHIFT);
regs->segment[S_CS].selector = 0xf000;
regs->segment[S_CS].base = 0xf0000;
regs->segment[S_CS].limit = 0xffff;
regs->segment[S_CS].flags = DESC_P_MASK |\
DESC_S_MASK | DESC_CS_MASK | DESC_R_MASK | DESC_A_MASK;
regs->segment[S_DS].selector = 0x0;
regs->segment[S_DS].base = 0x0;
regs->segment[S_DS].limit = 0xffff;
regs->segment[S_DS].flags = DESC_P_MASK |\
DESC_S_MASK | DESC_W_MASK | DESC_A_MASK;
regs->segment[S_ES].selector = 0x0;
regs->segment[S_ES].base = 0x0;
regs->segment[S_ES].limit = 0xffff;
regs->segment[S_ES].flags = DESC_P_MASK |\
DESC_S_MASK | DESC_W_MASK | DESC_A_MASK;
regs->segment[S_SS].selector = 0x0;
regs->segment[S_SS].base = 0x0;
regs->segment[S_SS].limit = 0xffff;
regs->segment[S_SS].flags = DESC_P_MASK |\
DESC_S_MASK | DESC_W_MASK | DESC_A_MASK;
regs->segment[S_FS].selector = 0x0;
regs->segment[S_FS].base = 0x0;
regs->segment[S_FS].limit = 0xffff;
regs->segment[S_FS].flags = DESC_P_MASK |\
DESC_S_MASK | DESC_W_MASK | DESC_A_MASK;
regs->segment[S_GS].selector = 0x0;
regs->segment[S_GS].base = 0x0;
regs->segment[S_GS].limit = 0xffff;
regs->segment[S_GS].flags = DESC_P_MASK |\
DESC_S_MASK | DESC_W_MASK | DESC_A_MASK;
regs->eip = 0xfff0;
regs->gpr[G_EDX] = 0x663; // cpuid version
regs->eflags = 0x2;
regs->tpr = 0;
regs->apicbase = APIC_DEFAULT_ADDRESS | MSR_IA32_APICBASE_ENABLE;
if (bsp)
regs->apicbase |= MSR_IA32_APICBASE_BSP;
}
// thread func that runs CPU in a loop until halt
static int runVcpu(vcpu_t *vcpu) {
return hvRunVcpu(vcpu);
}
static int setupGuestMemory(void) {
int ret = 0;
/* our goal is to roughly create memory similar to the i440fx motherboard
* chipset */
/*
0x00000000 0x000003FF 1 KiB Real Mode IVT (Interrupt Vector Table) unusable in real mode 640 KiB RAM ("Low memory")
0x00000400 0x000004FF 256 bytes BDA (BIOS data area)
0x00000500 0x00007BFF almost 30 KiB Conventional memory usable memory
0x00007C00 0x00007DFF 512 bytes Your OS BootSector
0x00007E00 0x0007FFFF 480.5 KiB Conventional memory
0x00080000 0x0009FFFF 128 KiB EBDA (Extended BIOS Data Area) partially used by the EBDA
0x000A0000 0x000BFFFF 128 KiB Video display memory hardware mapped 384 KiB System / Reserved ("Upper Memory")
0x000C0000 0x000C7FFF 32 KiB (typically) Video BIOS ROM and hardware mapped / Shadow RAM
0x000C8000 0x000EFFFF 160 KiB (typically) BIOS Expansions
0x000F0000 0x000FFFFF 64 KiB Motherboard BIOS
*/
ret = memfd_create("vmram", 0);
if (ret < 0) {
return ret;
}
g_hv->fw_memfd = ret;
ret = ftruncate(g_hv->fw_memfd, HOST_FW_SIZE);
if (ret < 0) {
goto cleanup;
}
/* map in main ram, goes up to video memory */
g_hv->fw_size = HOST_FW_SIZE;
g_hv->fw = mmap((void *)HOST_FW_VADDR,
HOST_FW_SIZE,
PROT_READ|PROT_WRITE,
MAP_SHARED|MAP_FIXED,
g_hv->fw_memfd,
0);
if (g_hv->fw == MAP_FAILED) {
ret = -1;
goto cleanup;
}
ret = hvSetMemory(g_hv, g_hv->fw, g_hv->fw_size, GUEST_FW_PADDR, false);
if (ret < 0)
goto cleanup;
g_hv->fw_mem_id = ret;
ret = memfd_create("vmsysmem", 0);
if (ret < 0) {
return ret;
}
g_hv->sys_memfd = ret;
ret = ftruncate(g_hv->sys_memfd, HOST_SYS_MEM_SIZE);
if (ret < 0) {
goto cleanup;
}
/* map in Kernel mem. TODO: command line. for now it's 1MB starting at 1MB*/
g_hv->sys_mem_size = HOST_SYS_MEM_SIZE;
g_hv->sys_mem = mmap((void *)HOST_SYS_MEM_VADDR,
HOST_SYS_MEM_SIZE,
PROT_READ|PROT_WRITE,
MAP_SHARED|MAP_FIXED,
g_hv->sys_memfd,
0);
if (g_hv->sys_mem == MAP_FAILED) {
ret = -1;
printf("Map failed\n");
goto cleanup;
}
ret = hvSetMemory(g_hv,
g_hv->sys_mem,
g_hv->sys_mem_size,
GUEST_SYS_MEM_PADDR,
false);
if (ret < 0) {
printf("hvSetMem failed\n");
goto cleanup;
}
g_hv->sys_mem_id = ret;
/* now BIOS, the VCPU will jump to 0xFFFF0 when it boots */
g_hv->bios_rom_size = HOST_BIOS_SIZE;
g_hv->bios_rom = mmap((void *)HOST_BIOS_VADDR,
HOST_BIOS_SIZE,
PROT_READ|PROT_WRITE,
MAP_ANON|MAP_SHARED|MAP_FIXED,
-1,
0);
if (g_hv->bios_rom == MAP_FAILED) {
ret = -1;
goto cleanup;
}
// TODO: we might need to dynamically resolve the filesystem location of the
// bios blob
int fd = open("bios/bios", O_RDONLY);
if (fd < 0) {
ret = -1;
goto cleanup;
}
ret = read(fd, g_hv->bios_rom + 0x3c000, 0x4000);
if (ret < 0) {
ret = -1;
goto cleanup;
}
close(fd);
ret = hvSetMemory(g_hv,
g_hv->bios_rom,
g_hv->bios_rom_size,
GUEST_BIOS_PADDR,
true);
if (ret < 0)
goto cleanup;
g_hv->bios_rom_mem_id = ret;
return 0;
cleanup:
if (g_hv->fw_memfd) {
close(g_hv->fw_memfd);
}
if (g_hv->sys_memfd) {
close(g_hv->sys_memfd);
}
if (g_hv->fw_mem_id) {
hvDelMemory(g_hv, g_hv->fw_mem_id);
g_hv->fw_mem_id = 0;
}
if (g_hv->vga_mem_id) {
hvDelMemory(g_hv, g_hv->vga_mem_id);
g_hv->vga_mem_id = 0;
}
if (g_hv->sys_mem_id) {
hvDelMemory(g_hv, g_hv->sys_mem_id);
g_hv->sys_mem_id = 0;
}
if (g_hv->bios_rom_mem_id) {
hvDelMemory(g_hv, g_hv->bios_rom_mem_id);
g_hv->bios_rom_mem_id = 0;
}
if (g_hv->fw) {
munmap(g_hv->fw, g_hv->fw_size);
}
if (g_hv->vga) {
munmap(g_hv->vga, g_hv->vga_size);
}
if (g_hv->bios_rom) {
munmap(g_hv->bios_rom, g_hv->bios_rom_size);
}
if (g_hv->sys_mem) {
munmap(g_hv->sys_mem, g_hv->sys_mem_size);
}
return ret;
}
static int initVcpu(vcpu_t *vcpu, bool bsp) {
// ask the hypervisor for a new vcpu
int ret = hvCreateVcpu(g_hv, vcpu);
if (ret < 0) {
return ret;
}
vcpu->driver_fd = ret;
vcpu->hv = g_hv;
vcpu->dirty = true;
// establish sync primitives
// TODO: ensure default mutex attributes are sane
pthread_mutex_init(&vcpu->state_access_mutex, NULL);
pthread_cond_init(&vcpu->startcpu, NULL);
// init apic access mutex
pthread_mutex_init(&vcpu->lapic_access_mutex, NULL);
// only the BSP begins running
if (bsp) {
vcpu->state = STATE_RUNNING;
}
else {
vcpu->state = STATE_NOT_STARTED;
}
// establish a channel so we can get notifications for the vcpu
ret = hvEstablishComm(vcpu);
if (ret < 0) {
return ret;
}
x86CpuReset(vcpu, bsp);
ret = hvSetVcpuRegisters(vcpu);
if (ret < 0) {
return ret;
}
ret = hvSetCpuid(vcpu);
if (ret < 0) {
return ret;
}
return ret;
}
static vcpu_t *createVcpu(void) {
vcpu_t *vcpu = NULL;
vcpu = malloc(sizeof(*vcpu));
if (vcpu == NULL) {
return NULL;
}
bzero(vcpu, sizeof(*vcpu));
vcpu->id = g_nvcpus++;
g_hv->vcpus[vcpu->id] = vcpu;
return vcpu;
}
int waitForSipi(vcpu_t *vcpu) {
hv_t *hv = vcpu->hv;
// if all are halted, we've shut down
bool all_halted = true;
int i;
// take the shutdown lock so none can change on us during check
for (i=0; i < g_nvcpus; i++) {
if (hv->vcpus[i]->state == STATE_RUNNING)
all_halted = false;
}
if (all_halted) {
printf("All vcpus are halted. Shutting down\n");
return VM_SHUTDOWN;
}
// otherwise we can still hope for a wakeup
pthread_mutex_lock(&vcpu->state_access_mutex);
while (vcpu->state != STATE_RUNNING)
pthread_cond_wait(&vcpu->startcpu, &vcpu->state_access_mutex);
pthread_mutex_unlock(&vcpu->state_access_mutex);
if (DEBUG)
printf("VCPU %d woke up!\n", vcpu->id);
return 0;
}
static void *vcpuThread(void *arg) {
vcpu_t *vcpu = (vcpu_t *)arg;
if (vcpu->state == STATE_NOT_STARTED)
waitForSipi(vcpu);
switch (runVcpu(vcpu)) {
case VM_SHUTDOWN:
printf("VM shutdown gracefully\n");
break;
case VM_HALTED:
printf("VM executed hlt\n");
break;
case VM_UNHANDLED_EXIT:
printf("VM attempted some unhandled case\n");
break;
default:
printf("Unexpected VM exit, device emulation may have failed\n");
break;
}
return NULL;
}
static int startVcpuThread(bool bsp, pthread_t *thread) {
vcpu_t *v = createVcpu();
if (v == NULL) {
perror("Failed to create demo vcpu");
return -1;
}
if (initVcpu(v, bsp) < 0) {
perror("Failed to initialize vcpu");
return -1;
}
if (pthread_create(thread, NULL, vcpuThread, v) < 0) {
perror("Failed to create vCPU thread");
return -1;
}
return 0;
}
void handleSigterm(int sig) {
dbusTeardown();
exit(0);
}
int initVmStore(char *vmname) {
bool needsEnv = false;
char *vmStoreDirPath = NULL;
vmStoreDirPath = getenv("OOOWS_VM_STORE_DIR");
if (vmStoreDirPath == NULL) {
vmStoreDirPath = DEFAULT_VM_STORE_DIR;
needsEnv = true;
}
if (access(vmStoreDirPath, O_RDONLY) < 0) {
if (mkdir(vmStoreDirPath, 0700) < 0) {
perror("Failed to make VM runtime store directory");
return -1;
}
}
int dirfd = open(vmStoreDirPath, O_DIRECTORY);
if (dirfd < 0) {
perror("Failed to open VM runtime store");
return -1;
}
if (faccessat(dirfd, vmname, O_RDONLY, 0) < 0) {
if (mkdirat(dirfd, vmname, 0700) < 0) {
perror("Failed to make runtime store for requested VM");
return -1;
}
}
close(dirfd);
if (needsEnv) {
if (setenv("OOOWS_VM_STORE_DIR", vmStoreDirPath, 0)) {
perror("Failed to set env for VM runtime store");
return 1;
}
}
return 0;
}
int main(int argc, char **argv) {
int nvcpus = 1;
if (argc < 3) {
fprintf(stderr, "usage: %s <vmname> <virtdisk> [vcpus]\n", argv[0]);
return 1;
}
if (setenv("OOOWS_VM_NAME", argv[1], 0)) {
perror("Failed to set vm name env var");
return 1;
}
if (setenv("OOOWS_VM_VIRTDISK", argv[2], 0)) {
perror("Failed to set virtdisk env var");
return 1;
}
if (initVmStore(argv[1]) < 0) {
perror("Failed to initialize VM runtime storage");
return 1;
}
if (argc > 3) {
nvcpus = atoi(argv[3]);
if (nvcpus > NR_MAX_VCPUS) {
fprintf(stderr,
"Invalid number of vCPUs specified, max is %d\n", NR_MAX_VCPUS);
}
}
char *devconfigPath = "devices.config";
if (argc > 4) {
devconfigPath = argv[4];
}
if (dbusConfigFromFile(devconfigPath)) {
perror("Failed to instantiate virtual hardware layout");
return 1;
}
if (atexit(dbusTeardown)) {
perror("Failed to install teardown logic");
return 1;
}
signal(SIGTERM, handleSigterm);
g_hv = hvInitHypervisor();
if (g_hv == NULL) {
perror("Failed to initialize hypervisor");
return 1;
}
// spin up the ioapic thread
if (pthread_create(&g_hv->ioapic->ioapic_thread, NULL, ioApicThread, g_hv) < 0) {
printf("Failed to start ioapic thread\n");
return 1;
}
/* if (hvCreateInterruptController(g_hv) < 0) { */
/* perror("Failed to setup interrupt controller"); */
/* return 1; */
/* } */
if (setupGuestMemory() < 0) {
perror("Failed to setup guest memory");
return 1;
}
pthread_t *threads = calloc(nvcpus, sizeof(pthread_t));
if (threads == NULL) {
perror("Failed to allocate space for vCPU threads");
return 1;
}
bzero(threads, nvcpus * sizeof(pthread_t));
int i=0;
for (i=0;i<nvcpus;i++) {
if (startVcpuThread(i == 0, &threads[i]) < 0) {
goto cancel;
}
}
int retval = 0;
void *retp = &retval;
for (i=0;i<nvcpus;i++) {
pthread_join(threads[i], &retp);
}
return 0;
cancel:
for (i=0;i<nvcpus;i++) {
if (threads[i]) {
pthread_cancel(threads[i]);
}
}
return 1;
}