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
|
#include "libkvm.h"
#include "kvm-x86.h"
#include <errno.h>
#include <sys/ioctl.h>
#include <string.h>
#include <unistd.h>
#include <stropts.h>
#include <sys/mman.h>
#include <stdio.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdlib.h>
int kvm_set_tss_addr(kvm_context_t kvm, unsigned long addr)
{
#ifdef KVM_CAP_SET_TSS_ADDR
int r;
r = ioctl(kvm->fd, KVM_CHECK_EXTENSION, KVM_CAP_SET_TSS_ADDR);
if (r > 0) {
r = ioctl(kvm->vm_fd, KVM_SET_TSS_ADDR, addr);
if (r == -1) {
fprintf(stderr, "kvm_set_tss_addr: %m\n");
return -errno;
}
return 0;
}
#endif
return -ENOSYS;
}
static int kvm_init_tss(kvm_context_t kvm)
{
#ifdef KVM_CAP_SET_TSS_ADDR
int r;
r = ioctl(kvm->fd, KVM_CHECK_EXTENSION, KVM_CAP_SET_TSS_ADDR);
if (r > 0) {
/*
* this address is 3 pages before the bios, and the bios should present
* as unavaible memory
*/
r = kvm_set_tss_addr(kvm, 0xfffbd000);
if (r < 0) {
fprintf(stderr, "kvm_init_tss: unable to set tss addr\n");
return r;
}
}
#endif
return 0;
}
int kvm_create_pit(kvm_context_t kvm)
{
#ifdef KVM_CAP_PIT
int r;
kvm->pit_in_kernel = 0;
if (!kvm->no_pit_creation) {
r = ioctl(kvm->fd, KVM_CHECK_EXTENSION, KVM_CAP_PIT);
if (r > 0) {
r = ioctl(kvm->vm_fd, KVM_CREATE_PIT);
if (r >= 0)
kvm->pit_in_kernel = 1;
else {
fprintf(stderr, "Create kernel PIC irqchip failed\n");
return r;
}
}
}
#endif
return 0;
}
int kvm_arch_create(kvm_context_t kvm, unsigned long phys_mem_bytes,
void **vm_mem)
{
int r = 0;
r = kvm_init_tss(kvm);
if (r < 0)
return r;
r = kvm_create_pit(kvm);
if (r < 0)
return r;
r = kvm_init_coalesced_mmio(kvm);
if (r < 0)
return r;
return 0;
}
#ifdef KVM_EXIT_TPR_ACCESS
static int handle_tpr_access(kvm_context_t kvm, struct kvm_run *run, int vcpu)
{
return kvm->callbacks->tpr_access(kvm->opaque, vcpu,
run->tpr_access.rip,
run->tpr_access.is_write);
}
int kvm_enable_vapic(kvm_context_t kvm, int vcpu, uint64_t vapic)
{
int r;
struct kvm_vapic_addr va = {
.vapic_addr = vapic,
};
r = ioctl(kvm->vcpu_fd[vcpu], KVM_SET_VAPIC_ADDR, &va);
if (r == -1) {
r = -errno;
perror("kvm_enable_vapic");
return r;
}
return 0;
}
#endif
int kvm_arch_run(struct kvm_run *run,kvm_context_t kvm, int vcpu)
{
int r = 0;
switch (run->exit_reason) {
#ifdef KVM_EXIT_SET_TPR
case KVM_EXIT_SET_TPR:
break;
#endif
#ifdef KVM_EXIT_TPR_ACCESS
case KVM_EXIT_TPR_ACCESS:
r = handle_tpr_access(kvm, run, vcpu);
break;
#endif
default:
r = 1;
break;
}
return r;
}
#define MAX_ALIAS_SLOTS 4
static struct {
uint64_t start;
uint64_t len;
} kvm_aliases[MAX_ALIAS_SLOTS];
static int get_alias_slot(uint64_t start)
{
int i;
for (i=0; i<MAX_ALIAS_SLOTS; i++)
if (kvm_aliases[i].start == start)
return i;
return -1;
}
static int get_free_alias_slot(void)
{
int i;
for (i=0; i<MAX_ALIAS_SLOTS; i++)
if (kvm_aliases[i].len == 0)
return i;
return -1;
}
static void register_alias(int slot, uint64_t start, uint64_t len)
{
kvm_aliases[slot].start = start;
kvm_aliases[slot].len = len;
}
int kvm_create_memory_alias(kvm_context_t kvm,
uint64_t phys_start,
uint64_t len,
uint64_t target_phys)
{
struct kvm_memory_alias alias = {
.flags = 0,
.guest_phys_addr = phys_start,
.memory_size = len,
.target_phys_addr = target_phys,
};
int fd = kvm->vm_fd;
int r;
int slot;
slot = get_alias_slot(phys_start);
if (slot < 0)
slot = get_free_alias_slot();
if (slot < 0)
return -EBUSY;
alias.slot = slot;
r = ioctl(fd, KVM_SET_MEMORY_ALIAS, &alias);
if (r == -1)
return -errno;
register_alias(slot, phys_start, len);
return 0;
}
int kvm_destroy_memory_alias(kvm_context_t kvm, uint64_t phys_start)
{
return kvm_create_memory_alias(kvm, phys_start, 0, 0);
}
#ifdef KVM_CAP_IRQCHIP
int kvm_get_lapic(kvm_context_t kvm, int vcpu, struct kvm_lapic_state *s)
{
int r;
if (!kvm->irqchip_in_kernel)
return 0;
r = ioctl(kvm->vcpu_fd[vcpu], KVM_GET_LAPIC, s);
if (r == -1) {
r = -errno;
perror("kvm_get_lapic");
}
return r;
}
int kvm_set_lapic(kvm_context_t kvm, int vcpu, struct kvm_lapic_state *s)
{
int r;
if (!kvm->irqchip_in_kernel)
return 0;
r = ioctl(kvm->vcpu_fd[vcpu], KVM_SET_LAPIC, s);
if (r == -1) {
r = -errno;
perror("kvm_set_lapic");
}
return r;
}
#endif
#ifdef KVM_CAP_PIT
int kvm_get_pit(kvm_context_t kvm, struct kvm_pit_state *s)
{
int r;
if (!kvm->pit_in_kernel)
return 0;
r = ioctl(kvm->vm_fd, KVM_GET_PIT, s);
if (r == -1) {
r = -errno;
perror("kvm_get_pit");
}
return r;
}
int kvm_set_pit(kvm_context_t kvm, struct kvm_pit_state *s)
{
int r;
if (!kvm->pit_in_kernel)
return 0;
r = ioctl(kvm->vm_fd, KVM_SET_PIT, s);
if (r == -1) {
r = -errno;
perror("kvm_set_pit");
}
return r;
}
#endif
void kvm_show_code(kvm_context_t kvm, int vcpu)
{
#define SHOW_CODE_LEN 50
int fd = kvm->vcpu_fd[vcpu];
struct kvm_regs regs;
struct kvm_sregs sregs;
int r, n;
int back_offset;
unsigned char code;
char code_str[SHOW_CODE_LEN * 3 + 1];
unsigned long rip;
r = ioctl(fd, KVM_GET_SREGS, &sregs);
if (r == -1) {
perror("KVM_GET_SREGS");
return;
}
r = ioctl(fd, KVM_GET_REGS, ®s);
if (r == -1) {
perror("KVM_GET_REGS");
return;
}
rip = sregs.cs.base + regs.rip;
back_offset = regs.rip;
if (back_offset > 20)
back_offset = 20;
*code_str = 0;
for (n = -back_offset; n < SHOW_CODE_LEN-back_offset; ++n) {
if (n == 0)
strcat(code_str, " -->");
r = kvm->callbacks->mmio_read(kvm->opaque, rip + n, &code, 1);
if (r < 0) {
strcat(code_str, " xx");
continue;
}
sprintf(code_str + strlen(code_str), " %02x", code);
}
fprintf(stderr, "code:%s\n", code_str);
}
/*
* Returns available msr list. User must free.
*/
struct kvm_msr_list *kvm_get_msr_list(kvm_context_t kvm)
{
struct kvm_msr_list sizer, *msrs;
int r, e;
sizer.nmsrs = 0;
r = ioctl(kvm->fd, KVM_GET_MSR_INDEX_LIST, &sizer);
if (r == -1 && errno != E2BIG)
return NULL;
msrs = malloc(sizeof *msrs + sizer.nmsrs * sizeof *msrs->indices);
if (!msrs) {
errno = ENOMEM;
return NULL;
}
msrs->nmsrs = sizer.nmsrs;
r = ioctl(kvm->fd, KVM_GET_MSR_INDEX_LIST, msrs);
if (r == -1) {
e = errno;
free(msrs);
errno = e;
return NULL;
}
return msrs;
}
int kvm_get_msrs(kvm_context_t kvm, int vcpu, struct kvm_msr_entry *msrs,
int n)
{
struct kvm_msrs *kmsrs = malloc(sizeof *kmsrs + n * sizeof *msrs);
int r, e;
if (!kmsrs) {
errno = ENOMEM;
return -1;
}
kmsrs->nmsrs = n;
memcpy(kmsrs->entries, msrs, n * sizeof *msrs);
r = ioctl(kvm->vcpu_fd[vcpu], KVM_GET_MSRS, kmsrs);
e = errno;
memcpy(msrs, kmsrs->entries, n * sizeof *msrs);
free(kmsrs);
errno = e;
return r;
}
int kvm_set_msrs(kvm_context_t kvm, int vcpu, struct kvm_msr_entry *msrs,
int n)
{
struct kvm_msrs *kmsrs = malloc(sizeof *kmsrs + n * sizeof *msrs);
int r, e;
if (!kmsrs) {
errno = ENOMEM;
return -1;
}
kmsrs->nmsrs = n;
memcpy(kmsrs->entries, msrs, n * sizeof *msrs);
r = ioctl(kvm->vcpu_fd[vcpu], KVM_SET_MSRS, kmsrs);
e = errno;
free(kmsrs);
errno = e;
return r;
}
static void print_seg(FILE *file, const char *name, struct kvm_segment *seg)
{
fprintf(stderr,
"%s %04x (%08llx/%08x p %d dpl %d db %d s %d type %x l %d"
" g %d avl %d)\n",
name, seg->selector, seg->base, seg->limit, seg->present,
seg->dpl, seg->db, seg->s, seg->type, seg->l, seg->g,
seg->avl);
}
static void print_dt(FILE *file, const char *name, struct kvm_dtable *dt)
{
fprintf(stderr, "%s %llx/%x\n", name, dt->base, dt->limit);
}
void kvm_show_regs(kvm_context_t kvm, int vcpu)
{
int fd = kvm->vcpu_fd[vcpu];
struct kvm_regs regs;
struct kvm_sregs sregs;
int r;
r = ioctl(fd, KVM_GET_REGS, ®s);
if (r == -1) {
perror("KVM_GET_REGS");
return;
}
fprintf(stderr,
"rax %016llx rbx %016llx rcx %016llx rdx %016llx\n"
"rsi %016llx rdi %016llx rsp %016llx rbp %016llx\n"
"r8 %016llx r9 %016llx r10 %016llx r11 %016llx\n"
"r12 %016llx r13 %016llx r14 %016llx r15 %016llx\n"
"rip %016llx rflags %08llx\n",
regs.rax, regs.rbx, regs.rcx, regs.rdx,
regs.rsi, regs.rdi, regs.rsp, regs.rbp,
regs.r8, regs.r9, regs.r10, regs.r11,
regs.r12, regs.r13, regs.r14, regs.r15,
regs.rip, regs.rflags);
r = ioctl(fd, KVM_GET_SREGS, &sregs);
if (r == -1) {
perror("KVM_GET_SREGS");
return;
}
print_seg(stderr, "cs", &sregs.cs);
print_seg(stderr, "ds", &sregs.ds);
print_seg(stderr, "es", &sregs.es);
print_seg(stderr, "ss", &sregs.ss);
print_seg(stderr, "fs", &sregs.fs);
print_seg(stderr, "gs", &sregs.gs);
print_seg(stderr, "tr", &sregs.tr);
print_seg(stderr, "ldt", &sregs.ldt);
print_dt(stderr, "gdt", &sregs.gdt);
print_dt(stderr, "idt", &sregs.idt);
fprintf(stderr, "cr0 %llx cr2 %llx cr3 %llx cr4 %llx cr8 %llx"
" efer %llx\n",
sregs.cr0, sregs.cr2, sregs.cr3, sregs.cr4, sregs.cr8,
sregs.efer);
}
uint64_t kvm_get_apic_base(kvm_context_t kvm, int vcpu)
{
struct kvm_run *run = kvm->run[vcpu];
return run->apic_base;
}
void kvm_set_cr8(kvm_context_t kvm, int vcpu, uint64_t cr8)
{
struct kvm_run *run = kvm->run[vcpu];
run->cr8 = cr8;
}
__u64 kvm_get_cr8(kvm_context_t kvm, int vcpu)
{
return kvm->run[vcpu]->cr8;
}
int kvm_setup_cpuid(kvm_context_t kvm, int vcpu, int nent,
struct kvm_cpuid_entry *entries)
{
struct kvm_cpuid *cpuid;
int r;
cpuid = malloc(sizeof(*cpuid) + nent * sizeof(*entries));
if (!cpuid)
return -ENOMEM;
cpuid->nent = nent;
memcpy(cpuid->entries, entries, nent * sizeof(*entries));
r = ioctl(kvm->vcpu_fd[vcpu], KVM_SET_CPUID, cpuid);
free(cpuid);
return r;
}
int kvm_set_shadow_pages(kvm_context_t kvm, unsigned int nrshadow_pages)
{
#ifdef KVM_CAP_MMU_SHADOW_CACHE_CONTROL
int r;
r = ioctl(kvm->fd, KVM_CHECK_EXTENSION,
KVM_CAP_MMU_SHADOW_CACHE_CONTROL);
if (r > 0) {
r = ioctl(kvm->vm_fd, KVM_SET_NR_MMU_PAGES, nrshadow_pages);
if (r == -1) {
fprintf(stderr, "kvm_set_shadow_pages: %m\n");
return -errno;
}
return 0;
}
#endif
return -1;
}
int kvm_get_shadow_pages(kvm_context_t kvm, unsigned int *nrshadow_pages)
{
#ifdef KVM_CAP_MMU_SHADOW_CACHE_CONTROL
int r;
r = ioctl(kvm->fd, KVM_CHECK_EXTENSION,
KVM_CAP_MMU_SHADOW_CACHE_CONTROL);
if (r > 0) {
*nrshadow_pages = ioctl(kvm->vm_fd, KVM_GET_NR_MMU_PAGES);
return 0;
}
#endif
return -1;
}
#ifdef KVM_CAP_VAPIC
static int tpr_access_reporting(kvm_context_t kvm, int vcpu, int enabled)
{
int r;
struct kvm_tpr_access_ctl tac = {
.enabled = enabled,
};
r = ioctl(kvm->fd, KVM_CHECK_EXTENSION, KVM_CAP_VAPIC);
if (r == -1 || r == 0)
return -ENOSYS;
r = ioctl(kvm->vcpu_fd[vcpu], KVM_TPR_ACCESS_REPORTING, &tac);
if (r == -1) {
r = -errno;
perror("KVM_TPR_ACCESS_REPORTING");
return r;
}
return 0;
}
int kvm_enable_tpr_access_reporting(kvm_context_t kvm, int vcpu)
{
return tpr_access_reporting(kvm, vcpu, 1);
}
int kvm_disable_tpr_access_reporting(kvm_context_t kvm, int vcpu)
{
return tpr_access_reporting(kvm, vcpu, 0);
}
#endif
|