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 *host_alias;
23 	void *mmap_start;
24 	void *mmap_alias;
25 	size_t mmap_size;
26 	struct rb_node gpa_node;
27 	struct rb_node hva_node;
28 	struct hlist_node slot_node;
29 };
30 
31 struct vcpu {
32 	struct list_head list;
33 	uint32_t id;
34 	int fd;
35 	struct kvm_run *state;
36 	struct kvm_dirty_gfn *dirty_gfns;
37 	uint32_t fetch_index;
38 	uint32_t dirty_gfns_count;
39 };
40 
41 struct userspace_mem_regions {
42 	struct rb_root gpa_tree;
43 	struct rb_root hva_tree;
44 	DECLARE_HASHTABLE(slot_hash, 9);
45 };
46 
47 struct kvm_vm {
48 	int mode;
49 	unsigned long type;
50 	int kvm_fd;
51 	int fd;
52 	unsigned int pgtable_levels;
53 	unsigned int page_size;
54 	unsigned int page_shift;
55 	unsigned int pa_bits;
56 	unsigned int va_bits;
57 	uint64_t max_gfn;
58 	struct list_head vcpus;
59 	struct userspace_mem_regions regions;
60 	struct sparsebit *vpages_valid;
61 	struct sparsebit *vpages_mapped;
62 	bool has_irqchip;
63 	bool pgd_created;
64 	vm_paddr_t pgd;
65 	vm_vaddr_t gdt;
66 	vm_vaddr_t tss;
67 	vm_vaddr_t idt;
68 	vm_vaddr_t handlers;
69 	uint32_t dirty_ring_size;
70 };
71 
72 struct vcpu *vcpu_find(struct kvm_vm *vm, uint32_t vcpuid);
73 
74 /*
75  * Virtual Translation Tables Dump
76  *
77  * Input Args:
78  *   stream - Output FILE stream
79  *   vm     - Virtual Machine
80  *   indent - Left margin indent amount
81  *
82  * Output Args: None
83  *
84  * Return: None
85  *
86  * Dumps to the FILE stream given by @stream, the contents of all the
87  * virtual translation tables for the VM given by @vm.
88  */
89 void virt_dump(FILE *stream, struct kvm_vm *vm, uint8_t indent);
90 
91 /*
92  * Register Dump
93  *
94  * Input Args:
95  *   stream - Output FILE stream
96  *   regs   - Registers
97  *   indent - Left margin indent amount
98  *
99  * Output Args: None
100  *
101  * Return: None
102  *
103  * Dumps the state of the registers given by @regs, to the FILE stream
104  * given by @stream.
105  */
106 void regs_dump(FILE *stream, struct kvm_regs *regs, uint8_t indent);
107 
108 /*
109  * System Register Dump
110  *
111  * Input Args:
112  *   stream - Output FILE stream
113  *   sregs  - System registers
114  *   indent - Left margin indent amount
115  *
116  * Output Args: None
117  *
118  * Return: None
119  *
120  * Dumps the state of the system registers given by @sregs, to the FILE stream
121  * given by @stream.
122  */
123 void sregs_dump(FILE *stream, struct kvm_sregs *sregs, uint8_t indent);
124 
125 struct userspace_mem_region *
126 memslot2region(struct kvm_vm *vm, uint32_t memslot);
127 
128 #endif /* SELFTEST_KVM_UTIL_INTERNAL_H */
129