• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * tools/testing/selftests/kvm/lib/kvm_util_internal.h
3  *
4  * Copyright (C) 2018, Google LLC.
5  *
6  * This work is licensed under the terms of the GNU GPL, version 2.
7  */
8 
9 #ifndef SELFTEST_KVM_UTIL_INTERNAL_H
10 #define SELFTEST_KVM_UTIL_INTERNAL_H
11 
12 #include "sparsebit.h"
13 
14 #define KVM_DEV_PATH		"/dev/kvm"
15 
16 #ifndef BITS_PER_BYTE
17 #define BITS_PER_BYTE		8
18 #endif
19 
20 #ifndef BITS_PER_LONG
21 #define BITS_PER_LONG		(BITS_PER_BYTE * sizeof(long))
22 #endif
23 
24 #define DIV_ROUND_UP(n, d)	(((n) + (d) - 1) / (d))
25 #define BITS_TO_LONGS(nr)	DIV_ROUND_UP(nr, BITS_PER_LONG)
26 
27 struct userspace_mem_region {
28 	struct userspace_mem_region *next, *prev;
29 	struct kvm_userspace_memory_region region;
30 	struct sparsebit *unused_phy_pages;
31 	int fd;
32 	off_t offset;
33 	void *host_mem;
34 	void *mmap_start;
35 	size_t mmap_size;
36 };
37 
38 struct vcpu {
39 	struct vcpu *next, *prev;
40 	uint32_t id;
41 	int fd;
42 	struct kvm_run *state;
43 };
44 
45 struct kvm_vm {
46 	int mode;
47 	int kvm_fd;
48 	int fd;
49 	unsigned int pgtable_levels;
50 	unsigned int page_size;
51 	unsigned int page_shift;
52 	unsigned int pa_bits;
53 	unsigned int va_bits;
54 	uint64_t max_gfn;
55 	struct vcpu *vcpu_head;
56 	struct userspace_mem_region *userspace_mem_region_head;
57 	struct sparsebit *vpages_valid;
58 	struct sparsebit *vpages_mapped;
59 	bool has_irqchip;
60 	bool pgd_created;
61 	vm_paddr_t pgd;
62 	vm_vaddr_t gdt;
63 	vm_vaddr_t tss;
64 };
65 
66 struct vcpu *vcpu_find(struct kvm_vm *vm, uint32_t vcpuid);
67 void vcpu_setup(struct kvm_vm *vm, int vcpuid, int pgd_memslot,
68 		int gdt_memslot);
69 void virt_dump(FILE *stream, struct kvm_vm *vm, uint8_t indent);
70 void regs_dump(FILE *stream, struct kvm_regs *regs, uint8_t indent);
71 void sregs_dump(FILE *stream, struct kvm_sregs *sregs, uint8_t indent);
72 
73 #endif /* SELFTEST_KVM_UTIL_INTERNAL_H */
74