-
Notifications
You must be signed in to change notification settings - Fork 143
/
kvm-hello-world.c
495 lines (401 loc) · 10.2 KB
/
kvm-hello-world.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
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <sys/ioctl.h>
#include <sys/mman.h>
#include <string.h>
#include <stdint.h>
#include <linux/kvm.h>
/* CR0 bits */
#define CR0_PE 1u
#define CR0_MP (1U << 1)
#define CR0_EM (1U << 2)
#define CR0_TS (1U << 3)
#define CR0_ET (1U << 4)
#define CR0_NE (1U << 5)
#define CR0_WP (1U << 16)
#define CR0_AM (1U << 18)
#define CR0_NW (1U << 29)
#define CR0_CD (1U << 30)
#define CR0_PG (1U << 31)
/* CR4 bits */
#define CR4_VME 1
#define CR4_PVI (1U << 1)
#define CR4_TSD (1U << 2)
#define CR4_DE (1U << 3)
#define CR4_PSE (1U << 4)
#define CR4_PAE (1U << 5)
#define CR4_MCE (1U << 6)
#define CR4_PGE (1U << 7)
#define CR4_PCE (1U << 8)
#define CR4_OSFXSR (1U << 8)
#define CR4_OSXMMEXCPT (1U << 10)
#define CR4_UMIP (1U << 11)
#define CR4_VMXE (1U << 13)
#define CR4_SMXE (1U << 14)
#define CR4_FSGSBASE (1U << 16)
#define CR4_PCIDE (1U << 17)
#define CR4_OSXSAVE (1U << 18)
#define CR4_SMEP (1U << 20)
#define CR4_SMAP (1U << 21)
#define EFER_SCE 1
#define EFER_LME (1U << 8)
#define EFER_LMA (1U << 10)
#define EFER_NXE (1U << 11)
/* 32-bit page directory entry bits */
#define PDE32_PRESENT 1
#define PDE32_RW (1U << 1)
#define PDE32_USER (1U << 2)
#define PDE32_PS (1U << 7)
/* 64-bit page * entry bits */
#define PDE64_PRESENT 1
#define PDE64_RW (1U << 1)
#define PDE64_USER (1U << 2)
#define PDE64_ACCESSED (1U << 5)
#define PDE64_DIRTY (1U << 6)
#define PDE64_PS (1U << 7)
#define PDE64_G (1U << 8)
struct vm {
int sys_fd;
int fd;
char *mem;
};
void vm_init(struct vm *vm, size_t mem_size)
{
int api_ver;
struct kvm_userspace_memory_region memreg;
vm->sys_fd = open("/dev/kvm", O_RDWR);
if (vm->sys_fd < 0) {
perror("open /dev/kvm");
exit(1);
}
api_ver = ioctl(vm->sys_fd, KVM_GET_API_VERSION, 0);
if (api_ver < 0) {
perror("KVM_GET_API_VERSION");
exit(1);
}
if (api_ver != KVM_API_VERSION) {
fprintf(stderr, "Got KVM api version %d, expected %d\n",
api_ver, KVM_API_VERSION);
exit(1);
}
vm->fd = ioctl(vm->sys_fd, KVM_CREATE_VM, 0);
if (vm->fd < 0) {
perror("KVM_CREATE_VM");
exit(1);
}
if (ioctl(vm->fd, KVM_SET_TSS_ADDR, 0xfffbd000) < 0) {
perror("KVM_SET_TSS_ADDR");
exit(1);
}
vm->mem = mmap(NULL, mem_size, PROT_READ | PROT_WRITE,
MAP_PRIVATE | MAP_ANONYMOUS | MAP_NORESERVE, -1, 0);
if (vm->mem == MAP_FAILED) {
perror("mmap mem");
exit(1);
}
madvise(vm->mem, mem_size, MADV_MERGEABLE);
memreg.slot = 0;
memreg.flags = 0;
memreg.guest_phys_addr = 0;
memreg.memory_size = mem_size;
memreg.userspace_addr = (unsigned long)vm->mem;
if (ioctl(vm->fd, KVM_SET_USER_MEMORY_REGION, &memreg) < 0) {
perror("KVM_SET_USER_MEMORY_REGION");
exit(1);
}
}
struct vcpu {
int fd;
struct kvm_run *kvm_run;
};
void vcpu_init(struct vm *vm, struct vcpu *vcpu)
{
int vcpu_mmap_size;
vcpu->fd = ioctl(vm->fd, KVM_CREATE_VCPU, 0);
if (vcpu->fd < 0) {
perror("KVM_CREATE_VCPU");
exit(1);
}
vcpu_mmap_size = ioctl(vm->sys_fd, KVM_GET_VCPU_MMAP_SIZE, 0);
if (vcpu_mmap_size <= 0) {
perror("KVM_GET_VCPU_MMAP_SIZE");
exit(1);
}
vcpu->kvm_run = mmap(NULL, vcpu_mmap_size, PROT_READ | PROT_WRITE,
MAP_SHARED, vcpu->fd, 0);
if (vcpu->kvm_run == MAP_FAILED) {
perror("mmap kvm_run");
exit(1);
}
}
int run_vm(struct vm *vm, struct vcpu *vcpu, size_t sz)
{
struct kvm_regs regs;
uint64_t memval = 0;
for (;;) {
if (ioctl(vcpu->fd, KVM_RUN, 0) < 0) {
perror("KVM_RUN");
exit(1);
}
switch (vcpu->kvm_run->exit_reason) {
case KVM_EXIT_HLT:
goto check;
case KVM_EXIT_IO:
if (vcpu->kvm_run->io.direction == KVM_EXIT_IO_OUT
&& vcpu->kvm_run->io.port == 0xE9) {
char *p = (char *)vcpu->kvm_run;
fwrite(p + vcpu->kvm_run->io.data_offset,
vcpu->kvm_run->io.size, 1, stdout);
fflush(stdout);
continue;
}
/* fall through */
default:
fprintf(stderr, "Got exit_reason %d,"
" expected KVM_EXIT_HLT (%d)\n",
vcpu->kvm_run->exit_reason, KVM_EXIT_HLT);
exit(1);
}
}
check:
if (ioctl(vcpu->fd, KVM_GET_REGS, ®s) < 0) {
perror("KVM_GET_REGS");
exit(1);
}
if (regs.rax != 42) {
printf("Wrong result: {E,R,}AX is %lld\n", regs.rax);
return 0;
}
memcpy(&memval, &vm->mem[0x400], sz);
if (memval != 42) {
printf("Wrong result: memory at 0x400 is %lld\n",
(unsigned long long)memval);
return 0;
}
return 1;
}
extern const unsigned char guest16[], guest16_end[];
int run_real_mode(struct vm *vm, struct vcpu *vcpu)
{
struct kvm_sregs sregs;
struct kvm_regs regs;
printf("Testing real mode\n");
if (ioctl(vcpu->fd, KVM_GET_SREGS, &sregs) < 0) {
perror("KVM_GET_SREGS");
exit(1);
}
sregs.cs.selector = 0;
sregs.cs.base = 0;
if (ioctl(vcpu->fd, KVM_SET_SREGS, &sregs) < 0) {
perror("KVM_SET_SREGS");
exit(1);
}
memset(®s, 0, sizeof(regs));
/* Clear all FLAGS bits, except bit 1 which is always set. */
regs.rflags = 2;
regs.rip = 0;
if (ioctl(vcpu->fd, KVM_SET_REGS, ®s) < 0) {
perror("KVM_SET_REGS");
exit(1);
}
memcpy(vm->mem, guest16, guest16_end-guest16);
return run_vm(vm, vcpu, 2);
}
static void setup_protected_mode(struct kvm_sregs *sregs)
{
struct kvm_segment seg = {
.base = 0,
.limit = 0xffffffff,
.selector = 1 << 3,
.present = 1,
.type = 11, /* Code: execute, read, accessed */
.dpl = 0,
.db = 1,
.s = 1, /* Code/data */
.l = 0,
.g = 1, /* 4KB granularity */
};
sregs->cr0 |= CR0_PE; /* enter protected mode */
sregs->cs = seg;
seg.type = 3; /* Data: read/write, accessed */
seg.selector = 2 << 3;
sregs->ds = sregs->es = sregs->fs = sregs->gs = sregs->ss = seg;
}
extern const unsigned char guest32[], guest32_end[];
int run_protected_mode(struct vm *vm, struct vcpu *vcpu)
{
struct kvm_sregs sregs;
struct kvm_regs regs;
printf("Testing protected mode\n");
if (ioctl(vcpu->fd, KVM_GET_SREGS, &sregs) < 0) {
perror("KVM_GET_SREGS");
exit(1);
}
setup_protected_mode(&sregs);
if (ioctl(vcpu->fd, KVM_SET_SREGS, &sregs) < 0) {
perror("KVM_SET_SREGS");
exit(1);
}
memset(®s, 0, sizeof(regs));
/* Clear all FLAGS bits, except bit 1 which is always set. */
regs.rflags = 2;
regs.rip = 0;
if (ioctl(vcpu->fd, KVM_SET_REGS, ®s) < 0) {
perror("KVM_SET_REGS");
exit(1);
}
memcpy(vm->mem, guest32, guest32_end-guest32);
return run_vm(vm, vcpu, 4);
}
static void setup_paged_32bit_mode(struct vm *vm, struct kvm_sregs *sregs)
{
uint32_t pd_addr = 0x2000;
uint32_t *pd = (void *)(vm->mem + pd_addr);
/* A single 4MB page to cover the memory region */
pd[0] = PDE32_PRESENT | PDE32_RW | PDE32_USER | PDE32_PS;
/* Other PDEs are left zeroed, meaning not present. */
sregs->cr3 = pd_addr;
sregs->cr4 = CR4_PSE;
sregs->cr0
= CR0_PE | CR0_MP | CR0_ET | CR0_NE | CR0_WP | CR0_AM | CR0_PG;
sregs->efer = 0;
}
int run_paged_32bit_mode(struct vm *vm, struct vcpu *vcpu)
{
struct kvm_sregs sregs;
struct kvm_regs regs;
printf("Testing 32-bit paging\n");
if (ioctl(vcpu->fd, KVM_GET_SREGS, &sregs) < 0) {
perror("KVM_GET_SREGS");
exit(1);
}
setup_protected_mode(&sregs);
setup_paged_32bit_mode(vm, &sregs);
if (ioctl(vcpu->fd, KVM_SET_SREGS, &sregs) < 0) {
perror("KVM_SET_SREGS");
exit(1);
}
memset(®s, 0, sizeof(regs));
/* Clear all FLAGS bits, except bit 1 which is always set. */
regs.rflags = 2;
regs.rip = 0;
if (ioctl(vcpu->fd, KVM_SET_REGS, ®s) < 0) {
perror("KVM_SET_REGS");
exit(1);
}
memcpy(vm->mem, guest32, guest32_end-guest32);
return run_vm(vm, vcpu, 4);
}
extern const unsigned char guest64[], guest64_end[];
static void setup_64bit_code_segment(struct kvm_sregs *sregs)
{
struct kvm_segment seg = {
.base = 0,
.limit = 0xffffffff,
.selector = 1 << 3,
.present = 1,
.type = 11, /* Code: execute, read, accessed */
.dpl = 0,
.db = 0,
.s = 1, /* Code/data */
.l = 1,
.g = 1, /* 4KB granularity */
};
sregs->cs = seg;
seg.type = 3; /* Data: read/write, accessed */
seg.selector = 2 << 3;
sregs->ds = sregs->es = sregs->fs = sregs->gs = sregs->ss = seg;
}
static void setup_long_mode(struct vm *vm, struct kvm_sregs *sregs)
{
uint64_t pml4_addr = 0x2000;
uint64_t *pml4 = (void *)(vm->mem + pml4_addr);
uint64_t pdpt_addr = 0x3000;
uint64_t *pdpt = (void *)(vm->mem + pdpt_addr);
uint64_t pd_addr = 0x4000;
uint64_t *pd = (void *)(vm->mem + pd_addr);
pml4[0] = PDE64_PRESENT | PDE64_RW | PDE64_USER | pdpt_addr;
pdpt[0] = PDE64_PRESENT | PDE64_RW | PDE64_USER | pd_addr;
pd[0] = PDE64_PRESENT | PDE64_RW | PDE64_USER | PDE64_PS;
sregs->cr3 = pml4_addr;
sregs->cr4 = CR4_PAE;
sregs->cr0
= CR0_PE | CR0_MP | CR0_ET | CR0_NE | CR0_WP | CR0_AM | CR0_PG;
sregs->efer = EFER_LME | EFER_LMA;
setup_64bit_code_segment(sregs);
}
int run_long_mode(struct vm *vm, struct vcpu *vcpu)
{
struct kvm_sregs sregs;
struct kvm_regs regs;
printf("Testing 64-bit mode\n");
if (ioctl(vcpu->fd, KVM_GET_SREGS, &sregs) < 0) {
perror("KVM_GET_SREGS");
exit(1);
}
setup_long_mode(vm, &sregs);
if (ioctl(vcpu->fd, KVM_SET_SREGS, &sregs) < 0) {
perror("KVM_SET_SREGS");
exit(1);
}
memset(®s, 0, sizeof(regs));
/* Clear all FLAGS bits, except bit 1 which is always set. */
regs.rflags = 2;
regs.rip = 0;
/* Create stack at top of 2 MB page and grow down. */
regs.rsp = 2 << 20;
if (ioctl(vcpu->fd, KVM_SET_REGS, ®s) < 0) {
perror("KVM_SET_REGS");
exit(1);
}
memcpy(vm->mem, guest64, guest64_end-guest64);
return run_vm(vm, vcpu, 8);
}
int main(int argc, char **argv)
{
struct vm vm;
struct vcpu vcpu;
enum {
REAL_MODE,
PROTECTED_MODE,
PAGED_32BIT_MODE,
LONG_MODE,
} mode = REAL_MODE;
int opt;
while ((opt = getopt(argc, argv, "rspl")) != -1) {
switch (opt) {
case 'r':
mode = REAL_MODE;
break;
case 's':
mode = PROTECTED_MODE;
break;
case 'p':
mode = PAGED_32BIT_MODE;
break;
case 'l':
mode = LONG_MODE;
break;
default:
fprintf(stderr, "Usage: %s [ -r | -s | -p | -l ]\n",
argv[0]);
return 1;
}
}
vm_init(&vm, 0x200000);
vcpu_init(&vm, &vcpu);
switch (mode) {
case REAL_MODE:
return !run_real_mode(&vm, &vcpu);
case PROTECTED_MODE:
return !run_protected_mode(&vm, &vcpu);
case PAGED_32BIT_MODE:
return !run_paged_32bit_mode(&vm, &vcpu);
case LONG_MODE:
return !run_long_mode(&vm, &vcpu);
}
return 1;
}