KVM: selftests: Keep track of memslots more efficiently
[linux-2.6-microblaze.git] / tools / testing / selftests / kvm / lib / kvm_util_internal.h
1 /* SPDX-License-Identifier: GPL-2.0-only */
2 /*
3  * tools/testing/selftests/kvm/lib/kvm_util_internal.h
4  *
5  * Copyright (C) 2018, Google LLC.
6  */
7
8 #ifndef SELFTEST_KVM_UTIL_INTERNAL_H
9 #define SELFTEST_KVM_UTIL_INTERNAL_H
10
11 #include "linux/hashtable.h"
12 #include "linux/rbtree.h"
13
14 #include "sparsebit.h"
15
16 struct userspace_mem_region {
17         struct kvm_userspace_memory_region region;
18         struct sparsebit *unused_phy_pages;
19         int fd;
20         off_t offset;
21         void *host_mem;
22         void *mmap_start;
23         size_t mmap_size;
24         struct rb_node gpa_node;
25         struct rb_node hva_node;
26         struct hlist_node slot_node;
27 };
28
29 struct vcpu {
30         struct list_head list;
31         uint32_t id;
32         int fd;
33         struct kvm_run *state;
34         struct kvm_dirty_gfn *dirty_gfns;
35         uint32_t fetch_index;
36         uint32_t dirty_gfns_count;
37 };
38
39 struct userspace_mem_regions {
40         struct rb_root gpa_tree;
41         struct rb_root hva_tree;
42         DECLARE_HASHTABLE(slot_hash, 9);
43 };
44
45 struct kvm_vm {
46         int mode;
47         unsigned long type;
48         int kvm_fd;
49         int fd;
50         unsigned int pgtable_levels;
51         unsigned int page_size;
52         unsigned int page_shift;
53         unsigned int pa_bits;
54         unsigned int va_bits;
55         uint64_t max_gfn;
56         struct list_head vcpus;
57         struct userspace_mem_regions regions;
58         struct sparsebit *vpages_valid;
59         struct sparsebit *vpages_mapped;
60         bool has_irqchip;
61         bool pgd_created;
62         vm_paddr_t pgd;
63         vm_vaddr_t gdt;
64         vm_vaddr_t tss;
65         vm_vaddr_t idt;
66         vm_vaddr_t handlers;
67         uint32_t dirty_ring_size;
68 };
69
70 struct vcpu *vcpu_find(struct kvm_vm *vm, uint32_t vcpuid);
71
72 /*
73  * Virtual Translation Tables Dump
74  *
75  * Input Args:
76  *   stream - Output FILE stream
77  *   vm     - Virtual Machine
78  *   indent - Left margin indent amount
79  *
80  * Output Args: None
81  *
82  * Return: None
83  *
84  * Dumps to the FILE stream given by @stream, the contents of all the
85  * virtual translation tables for the VM given by @vm.
86  */
87 void virt_dump(FILE *stream, struct kvm_vm *vm, uint8_t indent);
88
89 /*
90  * Register Dump
91  *
92  * Input Args:
93  *   stream - Output FILE stream
94  *   regs   - Registers
95  *   indent - Left margin indent amount
96  *
97  * Output Args: None
98  *
99  * Return: None
100  *
101  * Dumps the state of the registers given by @regs, to the FILE stream
102  * given by @stream.
103  */
104 void regs_dump(FILE *stream, struct kvm_regs *regs, uint8_t indent);
105
106 /*
107  * System Register Dump
108  *
109  * Input Args:
110  *   stream - Output FILE stream
111  *   sregs  - System registers
112  *   indent - Left margin indent amount
113  *
114  * Output Args: None
115  *
116  * Return: None
117  *
118  * Dumps the state of the system registers given by @sregs, to the FILE stream
119  * given by @stream.
120  */
121 void sregs_dump(FILE *stream, struct kvm_sregs *sregs, uint8_t indent);
122
123 struct userspace_mem_region *
124 memslot2region(struct kvm_vm *vm, uint32_t memslot);
125
126 #endif /* SELFTEST_KVM_UTIL_INTERNAL_H */