1 // SPDX-License-Identifier: GPL-2.0-only
3 * tools/testing/selftests/kvm/lib/x86_64/processor.c
5 * Copyright (C) 2018, Google LLC.
10 #include "../kvm_util_internal.h"
11 #include "processor.h"
13 #ifndef NUM_INTERRUPTS
14 #define NUM_INTERRUPTS 256
17 #define DEFAULT_CODE_SELECTOR 0x8
18 #define DEFAULT_DATA_SELECTOR 0x10
20 vm_vaddr_t exception_handlers;
22 /* Virtual translation table structure declarations */
23 struct pageMapL4Entry {
27 uint64_t write_through:1;
28 uint64_t cache_disable:1;
30 uint64_t ignored_06:1;
32 uint64_t ignored_11_08:4;
34 uint64_t ignored_62_52:11;
35 uint64_t execute_disable:1;
38 struct pageDirectoryPointerEntry {
42 uint64_t write_through:1;
43 uint64_t cache_disable:1;
45 uint64_t ignored_06:1;
47 uint64_t ignored_11_08:4;
49 uint64_t ignored_62_52:11;
50 uint64_t execute_disable:1;
53 struct pageDirectoryEntry {
57 uint64_t write_through:1;
58 uint64_t cache_disable:1;
60 uint64_t ignored_06:1;
62 uint64_t ignored_11_08:4;
64 uint64_t ignored_62_52:11;
65 uint64_t execute_disable:1;
68 struct pageTableEntry {
72 uint64_t write_through:1;
73 uint64_t cache_disable:1;
76 uint64_t reserved_07:1;
78 uint64_t ignored_11_09:3;
80 uint64_t ignored_62_52:11;
81 uint64_t execute_disable:1;
84 void regs_dump(FILE *stream, struct kvm_regs *regs,
87 fprintf(stream, "%*srax: 0x%.16llx rbx: 0x%.16llx "
88 "rcx: 0x%.16llx rdx: 0x%.16llx\n",
90 regs->rax, regs->rbx, regs->rcx, regs->rdx);
91 fprintf(stream, "%*srsi: 0x%.16llx rdi: 0x%.16llx "
92 "rsp: 0x%.16llx rbp: 0x%.16llx\n",
94 regs->rsi, regs->rdi, regs->rsp, regs->rbp);
95 fprintf(stream, "%*sr8: 0x%.16llx r9: 0x%.16llx "
96 "r10: 0x%.16llx r11: 0x%.16llx\n",
98 regs->r8, regs->r9, regs->r10, regs->r11);
99 fprintf(stream, "%*sr12: 0x%.16llx r13: 0x%.16llx "
100 "r14: 0x%.16llx r15: 0x%.16llx\n",
102 regs->r12, regs->r13, regs->r14, regs->r15);
103 fprintf(stream, "%*srip: 0x%.16llx rfl: 0x%.16llx\n",
105 regs->rip, regs->rflags);
112 * stream - Output FILE stream
113 * segment - KVM segment
114 * indent - Left margin indent amount
120 * Dumps the state of the KVM segment given by @segment, to the FILE stream
123 static void segment_dump(FILE *stream, struct kvm_segment *segment,
126 fprintf(stream, "%*sbase: 0x%.16llx limit: 0x%.8x "
127 "selector: 0x%.4x type: 0x%.2x\n",
128 indent, "", segment->base, segment->limit,
129 segment->selector, segment->type);
130 fprintf(stream, "%*spresent: 0x%.2x dpl: 0x%.2x "
131 "db: 0x%.2x s: 0x%.2x l: 0x%.2x\n",
132 indent, "", segment->present, segment->dpl,
133 segment->db, segment->s, segment->l);
134 fprintf(stream, "%*sg: 0x%.2x avl: 0x%.2x "
135 "unusable: 0x%.2x padding: 0x%.2x\n",
136 indent, "", segment->g, segment->avl,
137 segment->unusable, segment->padding);
144 * stream - Output FILE stream
145 * dtable - KVM dtable
146 * indent - Left margin indent amount
152 * Dumps the state of the KVM dtable given by @dtable, to the FILE stream
155 static void dtable_dump(FILE *stream, struct kvm_dtable *dtable,
158 fprintf(stream, "%*sbase: 0x%.16llx limit: 0x%.4x "
159 "padding: 0x%.4x 0x%.4x 0x%.4x\n",
160 indent, "", dtable->base, dtable->limit,
161 dtable->padding[0], dtable->padding[1], dtable->padding[2]);
164 void sregs_dump(FILE *stream, struct kvm_sregs *sregs,
169 fprintf(stream, "%*scs:\n", indent, "");
170 segment_dump(stream, &sregs->cs, indent + 2);
171 fprintf(stream, "%*sds:\n", indent, "");
172 segment_dump(stream, &sregs->ds, indent + 2);
173 fprintf(stream, "%*ses:\n", indent, "");
174 segment_dump(stream, &sregs->es, indent + 2);
175 fprintf(stream, "%*sfs:\n", indent, "");
176 segment_dump(stream, &sregs->fs, indent + 2);
177 fprintf(stream, "%*sgs:\n", indent, "");
178 segment_dump(stream, &sregs->gs, indent + 2);
179 fprintf(stream, "%*sss:\n", indent, "");
180 segment_dump(stream, &sregs->ss, indent + 2);
181 fprintf(stream, "%*str:\n", indent, "");
182 segment_dump(stream, &sregs->tr, indent + 2);
183 fprintf(stream, "%*sldt:\n", indent, "");
184 segment_dump(stream, &sregs->ldt, indent + 2);
186 fprintf(stream, "%*sgdt:\n", indent, "");
187 dtable_dump(stream, &sregs->gdt, indent + 2);
188 fprintf(stream, "%*sidt:\n", indent, "");
189 dtable_dump(stream, &sregs->idt, indent + 2);
191 fprintf(stream, "%*scr0: 0x%.16llx cr2: 0x%.16llx "
192 "cr3: 0x%.16llx cr4: 0x%.16llx\n",
194 sregs->cr0, sregs->cr2, sregs->cr3, sregs->cr4);
195 fprintf(stream, "%*scr8: 0x%.16llx efer: 0x%.16llx "
196 "apic_base: 0x%.16llx\n",
198 sregs->cr8, sregs->efer, sregs->apic_base);
200 fprintf(stream, "%*sinterrupt_bitmap:\n", indent, "");
201 for (i = 0; i < (KVM_NR_INTERRUPTS + 63) / 64; i++) {
202 fprintf(stream, "%*s%.16llx\n", indent + 2, "",
203 sregs->interrupt_bitmap[i]);
207 void virt_pgd_alloc(struct kvm_vm *vm)
209 TEST_ASSERT(vm->mode == VM_MODE_PXXV48_4K, "Attempt to use "
210 "unknown or unsupported guest mode, mode: 0x%x", vm->mode);
212 /* If needed, create page map l4 table. */
213 if (!vm->pgd_created) {
214 vm->pgd = vm_alloc_page_table(vm);
215 vm->pgd_created = true;
219 void virt_pg_map(struct kvm_vm *vm, uint64_t vaddr, uint64_t paddr)
222 struct pageMapL4Entry *pml4e;
224 TEST_ASSERT(vm->mode == VM_MODE_PXXV48_4K, "Attempt to use "
225 "unknown or unsupported guest mode, mode: 0x%x", vm->mode);
227 TEST_ASSERT((vaddr % vm->page_size) == 0,
228 "Virtual address not on page boundary,\n"
229 " vaddr: 0x%lx vm->page_size: 0x%x",
230 vaddr, vm->page_size);
231 TEST_ASSERT(sparsebit_is_set(vm->vpages_valid,
232 (vaddr >> vm->page_shift)),
233 "Invalid virtual address, vaddr: 0x%lx",
235 TEST_ASSERT((paddr % vm->page_size) == 0,
236 "Physical address not on page boundary,\n"
237 " paddr: 0x%lx vm->page_size: 0x%x",
238 paddr, vm->page_size);
239 TEST_ASSERT((paddr >> vm->page_shift) <= vm->max_gfn,
240 "Physical address beyond beyond maximum supported,\n"
241 " paddr: 0x%lx vm->max_gfn: 0x%lx vm->page_size: 0x%x",
242 paddr, vm->max_gfn, vm->page_size);
244 index[0] = (vaddr >> 12) & 0x1ffu;
245 index[1] = (vaddr >> 21) & 0x1ffu;
246 index[2] = (vaddr >> 30) & 0x1ffu;
247 index[3] = (vaddr >> 39) & 0x1ffu;
249 /* Allocate page directory pointer table if not present. */
250 pml4e = addr_gpa2hva(vm, vm->pgd);
251 if (!pml4e[index[3]].present) {
252 pml4e[index[3]].address = vm_alloc_page_table(vm) >> vm->page_shift;
253 pml4e[index[3]].writable = true;
254 pml4e[index[3]].present = true;
257 /* Allocate page directory table if not present. */
258 struct pageDirectoryPointerEntry *pdpe;
259 pdpe = addr_gpa2hva(vm, pml4e[index[3]].address * vm->page_size);
260 if (!pdpe[index[2]].present) {
261 pdpe[index[2]].address = vm_alloc_page_table(vm) >> vm->page_shift;
262 pdpe[index[2]].writable = true;
263 pdpe[index[2]].present = true;
266 /* Allocate page table if not present. */
267 struct pageDirectoryEntry *pde;
268 pde = addr_gpa2hva(vm, pdpe[index[2]].address * vm->page_size);
269 if (!pde[index[1]].present) {
270 pde[index[1]].address = vm_alloc_page_table(vm) >> vm->page_shift;
271 pde[index[1]].writable = true;
272 pde[index[1]].present = true;
275 /* Fill in page table entry. */
276 struct pageTableEntry *pte;
277 pte = addr_gpa2hva(vm, pde[index[1]].address * vm->page_size);
278 pte[index[0]].address = paddr >> vm->page_shift;
279 pte[index[0]].writable = true;
280 pte[index[0]].present = 1;
283 void virt_dump(FILE *stream, struct kvm_vm *vm, uint8_t indent)
285 struct pageMapL4Entry *pml4e, *pml4e_start;
286 struct pageDirectoryPointerEntry *pdpe, *pdpe_start;
287 struct pageDirectoryEntry *pde, *pde_start;
288 struct pageTableEntry *pte, *pte_start;
290 if (!vm->pgd_created)
293 fprintf(stream, "%*s "
294 " no\n", indent, "");
295 fprintf(stream, "%*s index hvaddr gpaddr "
296 "addr w exec dirty\n",
298 pml4e_start = (struct pageMapL4Entry *) addr_gpa2hva(vm,
300 for (uint16_t n1 = 0; n1 <= 0x1ffu; n1++) {
301 pml4e = &pml4e_start[n1];
304 fprintf(stream, "%*spml4e 0x%-3zx %p 0x%-12lx 0x%-10lx %u "
307 pml4e - pml4e_start, pml4e,
308 addr_hva2gpa(vm, pml4e), (uint64_t) pml4e->address,
309 pml4e->writable, pml4e->execute_disable);
311 pdpe_start = addr_gpa2hva(vm, pml4e->address
313 for (uint16_t n2 = 0; n2 <= 0x1ffu; n2++) {
314 pdpe = &pdpe_start[n2];
317 fprintf(stream, "%*spdpe 0x%-3zx %p 0x%-12lx 0x%-10lx "
320 pdpe - pdpe_start, pdpe,
321 addr_hva2gpa(vm, pdpe),
322 (uint64_t) pdpe->address, pdpe->writable,
323 pdpe->execute_disable);
325 pde_start = addr_gpa2hva(vm,
326 pdpe->address * vm->page_size);
327 for (uint16_t n3 = 0; n3 <= 0x1ffu; n3++) {
328 pde = &pde_start[n3];
331 fprintf(stream, "%*spde 0x%-3zx %p "
332 "0x%-12lx 0x%-10lx %u %u\n",
333 indent, "", pde - pde_start, pde,
334 addr_hva2gpa(vm, pde),
335 (uint64_t) pde->address, pde->writable,
336 pde->execute_disable);
338 pte_start = addr_gpa2hva(vm,
339 pde->address * vm->page_size);
340 for (uint16_t n4 = 0; n4 <= 0x1ffu; n4++) {
341 pte = &pte_start[n4];
344 fprintf(stream, "%*spte 0x%-3zx %p "
345 "0x%-12lx 0x%-10lx %u %u "
348 pte - pte_start, pte,
349 addr_hva2gpa(vm, pte),
350 (uint64_t) pte->address,
352 pte->execute_disable,
354 ((uint64_t) n1 << 27)
355 | ((uint64_t) n2 << 18)
356 | ((uint64_t) n3 << 9)
365 * Set Unusable Segment
370 * segp - Pointer to segment register
374 * Sets the segment register pointed to by @segp to an unusable state.
376 static void kvm_seg_set_unusable(struct kvm_segment *segp)
378 memset(segp, 0, sizeof(*segp));
379 segp->unusable = true;
382 static void kvm_seg_fill_gdt_64bit(struct kvm_vm *vm, struct kvm_segment *segp)
384 void *gdt = addr_gva2hva(vm, vm->gdt);
385 struct desc64 *desc = gdt + (segp->selector >> 3) * 8;
387 desc->limit0 = segp->limit & 0xFFFF;
388 desc->base0 = segp->base & 0xFFFF;
389 desc->base1 = segp->base >> 16;
390 desc->type = segp->type;
392 desc->dpl = segp->dpl;
393 desc->p = segp->present;
394 desc->limit1 = segp->limit >> 16;
395 desc->avl = segp->avl;
399 desc->base2 = segp->base >> 24;
401 desc->base3 = segp->base >> 32;
406 * Set Long Mode Flat Kernel Code Segment
409 * vm - VM whose GDT is being filled, or NULL to only write segp
410 * selector - selector value
413 * segp - Pointer to KVM segment
417 * Sets up the KVM segment pointed to by @segp, to be a code segment
418 * with the selector value given by @selector.
420 static void kvm_seg_set_kernel_code_64bit(struct kvm_vm *vm, uint16_t selector,
421 struct kvm_segment *segp)
423 memset(segp, 0, sizeof(*segp));
424 segp->selector = selector;
425 segp->limit = 0xFFFFFFFFu;
426 segp->s = 0x1; /* kTypeCodeData */
427 segp->type = 0x08 | 0x01 | 0x02; /* kFlagCode | kFlagCodeAccessed
428 * | kFlagCodeReadable
434 kvm_seg_fill_gdt_64bit(vm, segp);
438 * Set Long Mode Flat Kernel Data Segment
441 * vm - VM whose GDT is being filled, or NULL to only write segp
442 * selector - selector value
445 * segp - Pointer to KVM segment
449 * Sets up the KVM segment pointed to by @segp, to be a data segment
450 * with the selector value given by @selector.
452 static void kvm_seg_set_kernel_data_64bit(struct kvm_vm *vm, uint16_t selector,
453 struct kvm_segment *segp)
455 memset(segp, 0, sizeof(*segp));
456 segp->selector = selector;
457 segp->limit = 0xFFFFFFFFu;
458 segp->s = 0x1; /* kTypeCodeData */
459 segp->type = 0x00 | 0x01 | 0x02; /* kFlagData | kFlagDataAccessed
460 * | kFlagDataWritable
463 segp->present = true;
465 kvm_seg_fill_gdt_64bit(vm, segp);
468 vm_paddr_t addr_gva2gpa(struct kvm_vm *vm, vm_vaddr_t gva)
471 struct pageMapL4Entry *pml4e;
472 struct pageDirectoryPointerEntry *pdpe;
473 struct pageDirectoryEntry *pde;
474 struct pageTableEntry *pte;
476 TEST_ASSERT(vm->mode == VM_MODE_PXXV48_4K, "Attempt to use "
477 "unknown or unsupported guest mode, mode: 0x%x", vm->mode);
479 index[0] = (gva >> 12) & 0x1ffu;
480 index[1] = (gva >> 21) & 0x1ffu;
481 index[2] = (gva >> 30) & 0x1ffu;
482 index[3] = (gva >> 39) & 0x1ffu;
484 if (!vm->pgd_created)
486 pml4e = addr_gpa2hva(vm, vm->pgd);
487 if (!pml4e[index[3]].present)
490 pdpe = addr_gpa2hva(vm, pml4e[index[3]].address * vm->page_size);
491 if (!pdpe[index[2]].present)
494 pde = addr_gpa2hva(vm, pdpe[index[2]].address * vm->page_size);
495 if (!pde[index[1]].present)
498 pte = addr_gpa2hva(vm, pde[index[1]].address * vm->page_size);
499 if (!pte[index[0]].present)
502 return (pte[index[0]].address * vm->page_size) + (gva & 0xfffu);
505 TEST_FAIL("No mapping for vm virtual address, gva: 0x%lx", gva);
509 static void kvm_setup_gdt(struct kvm_vm *vm, struct kvm_dtable *dt)
512 vm->gdt = vm_vaddr_alloc_page(vm);
515 dt->limit = getpagesize();
518 static void kvm_setup_tss_64bit(struct kvm_vm *vm, struct kvm_segment *segp,
522 vm->tss = vm_vaddr_alloc_page(vm);
524 memset(segp, 0, sizeof(*segp));
525 segp->base = vm->tss;
527 segp->selector = selector;
530 kvm_seg_fill_gdt_64bit(vm, segp);
533 static void vcpu_setup(struct kvm_vm *vm, int vcpuid)
535 struct kvm_sregs sregs;
537 /* Set mode specific system register values. */
538 vcpu_sregs_get(vm, vcpuid, &sregs);
542 kvm_setup_gdt(vm, &sregs.gdt);
545 case VM_MODE_PXXV48_4K:
546 sregs.cr0 = X86_CR0_PE | X86_CR0_NE | X86_CR0_PG;
547 sregs.cr4 |= X86_CR4_PAE | X86_CR4_OSFXSR;
548 sregs.efer |= (EFER_LME | EFER_LMA | EFER_NX);
550 kvm_seg_set_unusable(&sregs.ldt);
551 kvm_seg_set_kernel_code_64bit(vm, DEFAULT_CODE_SELECTOR, &sregs.cs);
552 kvm_seg_set_kernel_data_64bit(vm, DEFAULT_DATA_SELECTOR, &sregs.ds);
553 kvm_seg_set_kernel_data_64bit(vm, DEFAULT_DATA_SELECTOR, &sregs.es);
554 kvm_setup_tss_64bit(vm, &sregs.tr, 0x18);
558 TEST_FAIL("Unknown guest mode, mode: 0x%x", vm->mode);
562 vcpu_sregs_set(vm, vcpuid, &sregs);
565 void vm_vcpu_add_default(struct kvm_vm *vm, uint32_t vcpuid, void *guest_code)
567 struct kvm_mp_state mp_state;
568 struct kvm_regs regs;
569 vm_vaddr_t stack_vaddr;
570 stack_vaddr = vm_vaddr_alloc(vm, DEFAULT_STACK_PGS * getpagesize(),
571 DEFAULT_GUEST_STACK_VADDR_MIN);
574 vm_vcpu_add(vm, vcpuid);
575 vcpu_setup(vm, vcpuid);
577 /* Setup guest general purpose registers */
578 vcpu_regs_get(vm, vcpuid, ®s);
579 regs.rflags = regs.rflags | 0x2;
580 regs.rsp = stack_vaddr + (DEFAULT_STACK_PGS * getpagesize());
581 regs.rip = (unsigned long) guest_code;
582 vcpu_regs_set(vm, vcpuid, ®s);
584 /* Setup the MP state */
585 mp_state.mp_state = 0;
586 vcpu_set_mp_state(vm, vcpuid, &mp_state);
588 /* Setup supported CPUIDs */
589 vcpu_set_cpuid(vm, vcpuid, kvm_get_supported_cpuid());
593 * Allocate an instance of struct kvm_cpuid2
599 * Return: A pointer to the allocated struct. The caller is responsible
600 * for freeing this struct.
602 * Since kvm_cpuid2 uses a 0-length array to allow a the size of the
603 * array to be decided at allocation time, allocation is slightly
604 * complicated. This function uses a reasonable default length for
605 * the array and performs the appropriate allocation.
607 static struct kvm_cpuid2 *allocate_kvm_cpuid2(void)
609 struct kvm_cpuid2 *cpuid;
613 size = sizeof(*cpuid);
614 size += nent * sizeof(struct kvm_cpuid_entry2);
615 cpuid = malloc(size);
627 * KVM Supported CPUID Get
633 * Return: The supported KVM CPUID
635 * Get the guest CPUID supported by KVM.
637 struct kvm_cpuid2 *kvm_get_supported_cpuid(void)
639 static struct kvm_cpuid2 *cpuid;
646 cpuid = allocate_kvm_cpuid2();
647 kvm_fd = open_kvm_dev_path_or_exit();
649 ret = ioctl(kvm_fd, KVM_GET_SUPPORTED_CPUID, cpuid);
650 TEST_ASSERT(ret == 0, "KVM_GET_SUPPORTED_CPUID failed %d %d\n",
661 * msr_index - Index of MSR
665 * Return: On success, value of the MSR. On failure a TEST_ASSERT is produced.
667 * Get value of MSR for VCPU.
669 uint64_t kvm_get_feature_msr(uint64_t msr_index)
672 struct kvm_msrs header;
673 struct kvm_msr_entry entry;
677 buffer.header.nmsrs = 1;
678 buffer.entry.index = msr_index;
679 kvm_fd = open_kvm_dev_path_or_exit();
681 r = ioctl(kvm_fd, KVM_GET_MSRS, &buffer.header);
682 TEST_ASSERT(r == 1, "KVM_GET_MSRS IOCTL failed,\n"
683 " rc: %i errno: %i", r, errno);
686 return buffer.entry.data;
693 * vm - Virtual Machine
698 * Return: KVM CPUID (KVM_GET_CPUID2)
700 * Set the VCPU's CPUID.
702 struct kvm_cpuid2 *vcpu_get_cpuid(struct kvm_vm *vm, uint32_t vcpuid)
704 struct vcpu *vcpu = vcpu_find(vm, vcpuid);
705 struct kvm_cpuid2 *cpuid;
709 TEST_ASSERT(vcpu != NULL, "vcpu not found, vcpuid: %u", vcpuid);
711 cpuid = allocate_kvm_cpuid2();
712 max_ent = cpuid->nent;
714 for (cpuid->nent = 1; cpuid->nent <= max_ent; cpuid->nent++) {
715 rc = ioctl(vcpu->fd, KVM_GET_CPUID2, cpuid);
719 TEST_ASSERT(rc == -1 && errno == E2BIG,
720 "KVM_GET_CPUID2 should either succeed or give E2BIG: %d %d",
724 TEST_ASSERT(rc == 0, "KVM_GET_CPUID2 failed, rc: %i errno: %i",
733 * Locate a cpuid entry.
736 * function: The function of the cpuid entry to find.
737 * index: The index of the cpuid entry.
741 * Return: A pointer to the cpuid entry. Never returns NULL.
743 struct kvm_cpuid_entry2 *
744 kvm_get_supported_cpuid_index(uint32_t function, uint32_t index)
746 struct kvm_cpuid2 *cpuid;
747 struct kvm_cpuid_entry2 *entry = NULL;
750 cpuid = kvm_get_supported_cpuid();
751 for (i = 0; i < cpuid->nent; i++) {
752 if (cpuid->entries[i].function == function &&
753 cpuid->entries[i].index == index) {
754 entry = &cpuid->entries[i];
759 TEST_ASSERT(entry, "Guest CPUID entry not found: (EAX=%x, ECX=%x).",
768 * vm - Virtual Machine
770 * cpuid - The CPUID values to set.
776 * Set the VCPU's CPUID.
778 void vcpu_set_cpuid(struct kvm_vm *vm,
779 uint32_t vcpuid, struct kvm_cpuid2 *cpuid)
781 struct vcpu *vcpu = vcpu_find(vm, vcpuid);
784 TEST_ASSERT(vcpu != NULL, "vcpu not found, vcpuid: %u", vcpuid);
786 rc = ioctl(vcpu->fd, KVM_SET_CPUID2, cpuid);
787 TEST_ASSERT(rc == 0, "KVM_SET_CPUID2 failed, rc: %i errno: %i",
796 * vm - Virtual Machine
798 * msr_index - Index of MSR
802 * Return: On success, value of the MSR. On failure a TEST_ASSERT is produced.
804 * Get value of MSR for VCPU.
806 uint64_t vcpu_get_msr(struct kvm_vm *vm, uint32_t vcpuid, uint64_t msr_index)
808 struct vcpu *vcpu = vcpu_find(vm, vcpuid);
810 struct kvm_msrs header;
811 struct kvm_msr_entry entry;
815 TEST_ASSERT(vcpu != NULL, "vcpu not found, vcpuid: %u", vcpuid);
816 buffer.header.nmsrs = 1;
817 buffer.entry.index = msr_index;
818 r = ioctl(vcpu->fd, KVM_GET_MSRS, &buffer.header);
819 TEST_ASSERT(r == 1, "KVM_GET_MSRS IOCTL failed,\n"
820 " rc: %i errno: %i", r, errno);
822 return buffer.entry.data;
829 * vm - Virtual Machine
831 * msr_index - Index of MSR
832 * msr_value - New value of MSR
836 * Return: The result of KVM_SET_MSRS.
838 * Sets the value of an MSR for the given VCPU.
840 int _vcpu_set_msr(struct kvm_vm *vm, uint32_t vcpuid, uint64_t msr_index,
843 struct vcpu *vcpu = vcpu_find(vm, vcpuid);
845 struct kvm_msrs header;
846 struct kvm_msr_entry entry;
850 TEST_ASSERT(vcpu != NULL, "vcpu not found, vcpuid: %u", vcpuid);
851 memset(&buffer, 0, sizeof(buffer));
852 buffer.header.nmsrs = 1;
853 buffer.entry.index = msr_index;
854 buffer.entry.data = msr_value;
855 r = ioctl(vcpu->fd, KVM_SET_MSRS, &buffer.header);
863 * vm - Virtual Machine
865 * msr_index - Index of MSR
866 * msr_value - New value of MSR
870 * Return: On success, nothing. On failure a TEST_ASSERT is produced.
872 * Set value of MSR for VCPU.
874 void vcpu_set_msr(struct kvm_vm *vm, uint32_t vcpuid, uint64_t msr_index,
879 r = _vcpu_set_msr(vm, vcpuid, msr_index, msr_value);
880 TEST_ASSERT(r == 1, "KVM_SET_MSRS IOCTL failed,\n"
881 " rc: %i errno: %i", r, errno);
884 void vcpu_args_set(struct kvm_vm *vm, uint32_t vcpuid, unsigned int num, ...)
887 struct kvm_regs regs;
889 TEST_ASSERT(num >= 1 && num <= 6, "Unsupported number of args,\n"
894 vcpu_regs_get(vm, vcpuid, ®s);
897 regs.rdi = va_arg(ap, uint64_t);
900 regs.rsi = va_arg(ap, uint64_t);
903 regs.rdx = va_arg(ap, uint64_t);
906 regs.rcx = va_arg(ap, uint64_t);
909 regs.r8 = va_arg(ap, uint64_t);
912 regs.r9 = va_arg(ap, uint64_t);
914 vcpu_regs_set(vm, vcpuid, ®s);
918 void vcpu_dump(FILE *stream, struct kvm_vm *vm, uint32_t vcpuid, uint8_t indent)
920 struct kvm_regs regs;
921 struct kvm_sregs sregs;
923 fprintf(stream, "%*scpuid: %u\n", indent, "", vcpuid);
925 fprintf(stream, "%*sregs:\n", indent + 2, "");
926 vcpu_regs_get(vm, vcpuid, ®s);
927 regs_dump(stream, ®s, indent + 4);
929 fprintf(stream, "%*ssregs:\n", indent + 2, "");
930 vcpu_sregs_get(vm, vcpuid, &sregs);
931 sregs_dump(stream, &sregs, indent + 4);
934 struct kvm_x86_state {
935 struct kvm_vcpu_events events;
936 struct kvm_mp_state mp_state;
937 struct kvm_regs regs;
938 struct kvm_xsave xsave;
939 struct kvm_xcrs xcrs;
940 struct kvm_sregs sregs;
941 struct kvm_debugregs debugregs;
943 struct kvm_nested_state nested;
946 struct kvm_msrs msrs;
949 static int kvm_get_num_msrs_fd(int kvm_fd)
951 struct kvm_msr_list nmsrs;
955 r = ioctl(kvm_fd, KVM_GET_MSR_INDEX_LIST, &nmsrs);
956 TEST_ASSERT(r == -1 && errno == E2BIG, "Unexpected result from KVM_GET_MSR_INDEX_LIST probe, r: %i",
962 static int kvm_get_num_msrs(struct kvm_vm *vm)
964 return kvm_get_num_msrs_fd(vm->kvm_fd);
967 struct kvm_msr_list *kvm_get_msr_index_list(void)
969 struct kvm_msr_list *list;
970 int nmsrs, r, kvm_fd;
972 kvm_fd = open_kvm_dev_path_or_exit();
974 nmsrs = kvm_get_num_msrs_fd(kvm_fd);
975 list = malloc(sizeof(*list) + nmsrs * sizeof(list->indices[0]));
977 r = ioctl(kvm_fd, KVM_GET_MSR_INDEX_LIST, list);
980 TEST_ASSERT(r == 0, "Unexpected result from KVM_GET_MSR_INDEX_LIST, r: %i",
986 struct kvm_x86_state *vcpu_save_state(struct kvm_vm *vm, uint32_t vcpuid)
988 struct vcpu *vcpu = vcpu_find(vm, vcpuid);
989 struct kvm_msr_list *list;
990 struct kvm_x86_state *state;
992 static int nested_size = -1;
994 if (nested_size == -1) {
995 nested_size = kvm_check_cap(KVM_CAP_NESTED_STATE);
996 TEST_ASSERT(nested_size <= sizeof(state->nested_),
997 "Nested state size too big, %i > %zi",
998 nested_size, sizeof(state->nested_));
1002 * When KVM exits to userspace with KVM_EXIT_IO, KVM guarantees
1003 * guest state is consistent only after userspace re-enters the
1004 * kernel with KVM_RUN. Complete IO prior to migrating state
1007 vcpu_run_complete_io(vm, vcpuid);
1009 nmsrs = kvm_get_num_msrs(vm);
1010 list = malloc(sizeof(*list) + nmsrs * sizeof(list->indices[0]));
1011 list->nmsrs = nmsrs;
1012 r = ioctl(vm->kvm_fd, KVM_GET_MSR_INDEX_LIST, list);
1013 TEST_ASSERT(r == 0, "Unexpected result from KVM_GET_MSR_INDEX_LIST, r: %i",
1016 state = malloc(sizeof(*state) + nmsrs * sizeof(state->msrs.entries[0]));
1017 r = ioctl(vcpu->fd, KVM_GET_VCPU_EVENTS, &state->events);
1018 TEST_ASSERT(r == 0, "Unexpected result from KVM_GET_VCPU_EVENTS, r: %i",
1021 r = ioctl(vcpu->fd, KVM_GET_MP_STATE, &state->mp_state);
1022 TEST_ASSERT(r == 0, "Unexpected result from KVM_GET_MP_STATE, r: %i",
1025 r = ioctl(vcpu->fd, KVM_GET_REGS, &state->regs);
1026 TEST_ASSERT(r == 0, "Unexpected result from KVM_GET_REGS, r: %i",
1029 r = ioctl(vcpu->fd, KVM_GET_XSAVE, &state->xsave);
1030 TEST_ASSERT(r == 0, "Unexpected result from KVM_GET_XSAVE, r: %i",
1033 if (kvm_check_cap(KVM_CAP_XCRS)) {
1034 r = ioctl(vcpu->fd, KVM_GET_XCRS, &state->xcrs);
1035 TEST_ASSERT(r == 0, "Unexpected result from KVM_GET_XCRS, r: %i",
1039 r = ioctl(vcpu->fd, KVM_GET_SREGS, &state->sregs);
1040 TEST_ASSERT(r == 0, "Unexpected result from KVM_GET_SREGS, r: %i",
1044 state->nested.size = sizeof(state->nested_);
1045 r = ioctl(vcpu->fd, KVM_GET_NESTED_STATE, &state->nested);
1046 TEST_ASSERT(r == 0, "Unexpected result from KVM_GET_NESTED_STATE, r: %i",
1048 TEST_ASSERT(state->nested.size <= nested_size,
1049 "Nested state size too big, %i (KVM_CHECK_CAP gave %i)",
1050 state->nested.size, nested_size);
1052 state->nested.size = 0;
1054 state->msrs.nmsrs = nmsrs;
1055 for (i = 0; i < nmsrs; i++)
1056 state->msrs.entries[i].index = list->indices[i];
1057 r = ioctl(vcpu->fd, KVM_GET_MSRS, &state->msrs);
1058 TEST_ASSERT(r == nmsrs, "Unexpected result from KVM_GET_MSRS, r: %i (failed MSR was 0x%x)",
1059 r, r == nmsrs ? -1 : list->indices[r]);
1061 r = ioctl(vcpu->fd, KVM_GET_DEBUGREGS, &state->debugregs);
1062 TEST_ASSERT(r == 0, "Unexpected result from KVM_GET_DEBUGREGS, r: %i",
1069 void vcpu_load_state(struct kvm_vm *vm, uint32_t vcpuid, struct kvm_x86_state *state)
1071 struct vcpu *vcpu = vcpu_find(vm, vcpuid);
1074 r = ioctl(vcpu->fd, KVM_SET_XSAVE, &state->xsave);
1075 TEST_ASSERT(r == 0, "Unexpected result from KVM_SET_XSAVE, r: %i",
1078 if (kvm_check_cap(KVM_CAP_XCRS)) {
1079 r = ioctl(vcpu->fd, KVM_SET_XCRS, &state->xcrs);
1080 TEST_ASSERT(r == 0, "Unexpected result from KVM_SET_XCRS, r: %i",
1084 r = ioctl(vcpu->fd, KVM_SET_SREGS, &state->sregs);
1085 TEST_ASSERT(r == 0, "Unexpected result from KVM_SET_SREGS, r: %i",
1088 r = ioctl(vcpu->fd, KVM_SET_MSRS, &state->msrs);
1089 TEST_ASSERT(r == state->msrs.nmsrs, "Unexpected result from KVM_SET_MSRS, r: %i (failed at %x)",
1090 r, r == state->msrs.nmsrs ? -1 : state->msrs.entries[r].index);
1092 r = ioctl(vcpu->fd, KVM_SET_VCPU_EVENTS, &state->events);
1093 TEST_ASSERT(r == 0, "Unexpected result from KVM_SET_VCPU_EVENTS, r: %i",
1096 r = ioctl(vcpu->fd, KVM_SET_MP_STATE, &state->mp_state);
1097 TEST_ASSERT(r == 0, "Unexpected result from KVM_SET_MP_STATE, r: %i",
1100 r = ioctl(vcpu->fd, KVM_SET_DEBUGREGS, &state->debugregs);
1101 TEST_ASSERT(r == 0, "Unexpected result from KVM_SET_DEBUGREGS, r: %i",
1104 r = ioctl(vcpu->fd, KVM_SET_REGS, &state->regs);
1105 TEST_ASSERT(r == 0, "Unexpected result from KVM_SET_REGS, r: %i",
1108 if (state->nested.size) {
1109 r = ioctl(vcpu->fd, KVM_SET_NESTED_STATE, &state->nested);
1110 TEST_ASSERT(r == 0, "Unexpected result from KVM_SET_NESTED_STATE, r: %i",
1115 bool is_intel_cpu(void)
1117 int eax, ebx, ecx, edx;
1118 const uint32_t *chunk;
1121 __asm__ __volatile__(
1123 : /* output */ "=a"(eax), "=b"(ebx),
1124 "=c"(ecx), "=d"(edx)
1125 : /* input */ "0"(leaf), "2"(0));
1127 chunk = (const uint32_t *)("GenuineIntel");
1128 return (ebx == chunk[0] && edx == chunk[1] && ecx == chunk[2]);
1131 uint32_t kvm_get_cpuid_max_basic(void)
1133 return kvm_get_supported_cpuid_entry(0)->eax;
1136 uint32_t kvm_get_cpuid_max_extended(void)
1138 return kvm_get_supported_cpuid_entry(0x80000000)->eax;
1141 void kvm_get_cpu_address_width(unsigned int *pa_bits, unsigned int *va_bits)
1143 struct kvm_cpuid_entry2 *entry;
1147 if (kvm_get_cpuid_max_extended() < 0x80000008) {
1148 pae = kvm_get_supported_cpuid_entry(1)->edx & (1 << 6);
1149 *pa_bits = pae ? 36 : 32;
1152 entry = kvm_get_supported_cpuid_entry(0x80000008);
1153 *pa_bits = entry->eax & 0xff;
1154 *va_bits = (entry->eax >> 8) & 0xff;
1168 uint32_t offset2; uint32_t reserved;
1171 static void set_idt_entry(struct kvm_vm *vm, int vector, unsigned long addr,
1172 int dpl, unsigned short selector)
1174 struct idt_entry *base =
1175 (struct idt_entry *)addr_gva2hva(vm, vm->idt);
1176 struct idt_entry *e = &base[vector];
1178 memset(e, 0, sizeof(*e));
1180 e->selector = selector;
1185 e->offset1 = addr >> 16;
1186 e->offset2 = addr >> 32;
1189 void kvm_exit_unexpected_vector(uint32_t value)
1191 outl(UNEXPECTED_VECTOR_PORT, value);
1194 void route_exception(struct ex_regs *regs)
1196 typedef void(*handler)(struct ex_regs *);
1197 handler *handlers = (handler *)exception_handlers;
1199 if (handlers && handlers[regs->vector]) {
1200 handlers[regs->vector](regs);
1204 kvm_exit_unexpected_vector(regs->vector);
1207 void vm_init_descriptor_tables(struct kvm_vm *vm)
1209 extern void *idt_handlers;
1212 vm->idt = vm_vaddr_alloc_page(vm);
1213 vm->handlers = vm_vaddr_alloc_page(vm);
1214 /* Handlers have the same address in both address spaces.*/
1215 for (i = 0; i < NUM_INTERRUPTS; i++)
1216 set_idt_entry(vm, i, (unsigned long)(&idt_handlers)[i], 0,
1217 DEFAULT_CODE_SELECTOR);
1220 void vcpu_init_descriptor_tables(struct kvm_vm *vm, uint32_t vcpuid)
1222 struct kvm_sregs sregs;
1224 vcpu_sregs_get(vm, vcpuid, &sregs);
1225 sregs.idt.base = vm->idt;
1226 sregs.idt.limit = NUM_INTERRUPTS * sizeof(struct idt_entry) - 1;
1227 sregs.gdt.base = vm->gdt;
1228 sregs.gdt.limit = getpagesize() - 1;
1229 kvm_seg_set_kernel_data_64bit(NULL, DEFAULT_DATA_SELECTOR, &sregs.gs);
1230 vcpu_sregs_set(vm, vcpuid, &sregs);
1231 *(vm_vaddr_t *)addr_gva2hva(vm, (vm_vaddr_t)(&exception_handlers)) = vm->handlers;
1234 void vm_handle_exception(struct kvm_vm *vm, int vector,
1235 void (*handler)(struct ex_regs *))
1237 vm_vaddr_t *handlers = (vm_vaddr_t *)addr_gva2hva(vm, vm->handlers);
1239 handlers[vector] = (vm_vaddr_t)handler;
1242 void assert_on_unhandled_exception(struct kvm_vm *vm, uint32_t vcpuid)
1244 if (vcpu_state(vm, vcpuid)->exit_reason == KVM_EXIT_IO
1245 && vcpu_state(vm, vcpuid)->io.port == UNEXPECTED_VECTOR_PORT
1246 && vcpu_state(vm, vcpuid)->io.size == 4) {
1247 /* Grab pointer to io data */
1248 uint32_t *data = (void *)vcpu_state(vm, vcpuid)
1249 + vcpu_state(vm, vcpuid)->io.data_offset;
1252 "Unexpected vectored event in guest (vector:0x%x)",
1257 bool set_cpuid(struct kvm_cpuid2 *cpuid,
1258 struct kvm_cpuid_entry2 *ent)
1262 for (i = 0; i < cpuid->nent; i++) {
1263 struct kvm_cpuid_entry2 *cur = &cpuid->entries[i];
1265 if (cur->function != ent->function || cur->index != ent->index)
1268 memcpy(cur, ent, sizeof(struct kvm_cpuid_entry2));
1275 uint64_t kvm_hypercall(uint64_t nr, uint64_t a0, uint64_t a1, uint64_t a2,
1280 asm volatile("vmcall"
1282 : "b"(a0), "c"(a1), "d"(a2), "S"(a3));
1286 struct kvm_cpuid2 *kvm_get_supported_hv_cpuid(void)
1288 static struct kvm_cpuid2 *cpuid;
1295 cpuid = allocate_kvm_cpuid2();
1296 kvm_fd = open_kvm_dev_path_or_exit();
1298 ret = ioctl(kvm_fd, KVM_GET_SUPPORTED_HV_CPUID, cpuid);
1299 TEST_ASSERT(ret == 0, "KVM_GET_SUPPORTED_HV_CPUID failed %d %d\n",
1306 void vcpu_set_hv_cpuid(struct kvm_vm *vm, uint32_t vcpuid)
1308 static struct kvm_cpuid2 *cpuid_full;
1309 struct kvm_cpuid2 *cpuid_sys, *cpuid_hv;
1313 cpuid_sys = kvm_get_supported_cpuid();
1314 cpuid_hv = kvm_get_supported_hv_cpuid();
1316 cpuid_full = malloc(sizeof(*cpuid_full) +
1317 (cpuid_sys->nent + cpuid_hv->nent) *
1318 sizeof(struct kvm_cpuid_entry2));
1324 /* Need to skip KVM CPUID leaves 0x400000xx */
1325 for (i = 0; i < cpuid_sys->nent; i++) {
1326 if (cpuid_sys->entries[i].function >= 0x40000000 &&
1327 cpuid_sys->entries[i].function < 0x40000100)
1329 cpuid_full->entries[nent] = cpuid_sys->entries[i];
1333 memcpy(&cpuid_full->entries[nent], cpuid_hv->entries,
1334 cpuid_hv->nent * sizeof(struct kvm_cpuid_entry2));
1335 cpuid_full->nent = nent + cpuid_hv->nent;
1338 vcpu_set_cpuid(vm, vcpuid, cpuid_full);
1341 struct kvm_cpuid2 *vcpu_get_supported_hv_cpuid(struct kvm_vm *vm, uint32_t vcpuid)
1343 static struct kvm_cpuid2 *cpuid;
1345 cpuid = allocate_kvm_cpuid2();
1347 vcpu_ioctl(vm, vcpuid, KVM_GET_SUPPORTED_HV_CPUID, cpuid);