xref: /openbmc/qemu/include/sysemu/kvm_int.h (revision 7d87775f)
1 /*
2  * Internal definitions for a target's KVM support
3  *
4  * This work is licensed under the terms of the GNU GPL, version 2 or later.
5  * See the COPYING file in the top-level directory.
6  *
7  */
8 
9 #ifndef QEMU_KVM_INT_H
10 #define QEMU_KVM_INT_H
11 
12 #include "exec/memory.h"
13 #include "qapi/qapi-types-common.h"
14 #include "qemu/accel.h"
15 #include "qemu/queue.h"
16 #include "sysemu/kvm.h"
17 #include "hw/boards.h"
18 #include "hw/i386/topology.h"
19 #include "io/channel-socket.h"
20 
21 typedef struct KVMSlot
22 {
23     hwaddr start_addr;
24     ram_addr_t memory_size;
25     void *ram;
26     int slot;
27     int flags;
28     int old_flags;
29     /* Dirty bitmap cache for the slot */
30     unsigned long *dirty_bmap;
31     unsigned long dirty_bmap_size;
32     /* Cache of the address space ID */
33     int as_id;
34     /* Cache of the offset in ram address space */
35     ram_addr_t ram_start_offset;
36     int guest_memfd;
37     hwaddr guest_memfd_offset;
38 } KVMSlot;
39 
40 typedef struct KVMMemoryUpdate {
41     QSIMPLEQ_ENTRY(KVMMemoryUpdate) next;
42     MemoryRegionSection section;
43 } KVMMemoryUpdate;
44 
45 typedef struct KVMMemoryListener {
46     MemoryListener listener;
47     KVMSlot *slots;
48     unsigned int nr_slots_used;
49     unsigned int nr_slots_allocated;
50     int as_id;
51     QSIMPLEQ_HEAD(, KVMMemoryUpdate) transaction_add;
52     QSIMPLEQ_HEAD(, KVMMemoryUpdate) transaction_del;
53 } KVMMemoryListener;
54 
55 #define KVM_MSI_HASHTAB_SIZE    256
56 
57 typedef struct KVMHostTopoInfo {
58     /* Number of package on the Host */
59     unsigned int maxpkgs;
60     /* Number of cpus on the Host */
61     unsigned int maxcpus;
62     /* Number of cpus on each different package */
63     unsigned int *pkg_cpu_count;
64     /* Each package can have different maxticks */
65     unsigned int *maxticks;
66 } KVMHostTopoInfo;
67 
68 struct KVMMsrEnergy {
69     pid_t pid;
70     bool enable;
71     char *socket_path;
72     QIOChannelSocket *sioc;
73     QemuThread msr_thr;
74     unsigned int guest_vcpus;
75     unsigned int guest_vsockets;
76     X86CPUTopoInfo guest_topo_info;
77     KVMHostTopoInfo host_topo;
78     const CPUArchIdList *guest_cpu_list;
79     uint64_t *msr_value;
80     uint64_t msr_unit;
81     uint64_t msr_limit;
82     uint64_t msr_info;
83 };
84 
85 enum KVMDirtyRingReaperState {
86     KVM_DIRTY_RING_REAPER_NONE = 0,
87     /* The reaper is sleeping */
88     KVM_DIRTY_RING_REAPER_WAIT,
89     /* The reaper is reaping for dirty pages */
90     KVM_DIRTY_RING_REAPER_REAPING,
91 };
92 
93 /*
94  * KVM reaper instance, responsible for collecting the KVM dirty bits
95  * via the dirty ring.
96  */
97 struct KVMDirtyRingReaper {
98     /* The reaper thread */
99     QemuThread reaper_thr;
100     volatile uint64_t reaper_iteration; /* iteration number of reaper thr */
101     volatile enum KVMDirtyRingReaperState reaper_state; /* reap thr state */
102 };
103 struct KVMState
104 {
105     AccelState parent_obj;
106     /* Max number of KVM slots supported */
107     int nr_slots_max;
108     int fd;
109     int vmfd;
110     int coalesced_mmio;
111     int coalesced_pio;
112     struct kvm_coalesced_mmio_ring *coalesced_mmio_ring;
113     bool coalesced_flush_in_progress;
114     int vcpu_events;
115 #ifdef TARGET_KVM_HAVE_GUEST_DEBUG
116     QTAILQ_HEAD(, kvm_sw_breakpoint) kvm_sw_breakpoints;
117 #endif
118     int max_nested_state_len;
119     int kvm_shadow_mem;
120     bool kernel_irqchip_allowed;
121     bool kernel_irqchip_required;
122     OnOffAuto kernel_irqchip_split;
123     bool sync_mmu;
124     bool guest_state_protected;
125     uint64_t manual_dirty_log_protect;
126     /*
127      * Older POSIX says that ioctl numbers are signed int, but in
128      * practice they are not. (Newer POSIX doesn't specify ioctl
129      * at all.) Linux, glibc and *BSD all treat ioctl numbers as
130      * unsigned, and real-world ioctl values like KVM_GET_XSAVE have
131      * bit 31 set, which means that passing them via an 'int' will
132      * result in sign-extension when they get converted back to the
133      * 'unsigned long' which the ioctl() prototype uses. Luckily Linux
134      * always treats the argument as an unsigned 32-bit int, so any
135      * possible sign-extension is deliberately ignored, but for
136      * consistency we keep to the same type that glibc is using.
137      */
138     unsigned long irq_set_ioctl;
139     unsigned int sigmask_len;
140     GHashTable *gsimap;
141 #ifdef KVM_CAP_IRQ_ROUTING
142     struct kvm_irq_routing *irq_routes;
143     int nr_allocated_irq_routes;
144     unsigned long *used_gsi_bitmap;
145     unsigned int gsi_count;
146 #endif
147     KVMMemoryListener memory_listener;
148     QLIST_HEAD(, KVMParkedVcpu) kvm_parked_vcpus;
149 
150     /* For "info mtree -f" to tell if an MR is registered in KVM */
151     int nr_as;
152     struct KVMAs {
153         KVMMemoryListener *ml;
154         AddressSpace *as;
155     } *as;
156     uint64_t kvm_dirty_ring_bytes;  /* Size of the per-vcpu dirty ring */
157     uint32_t kvm_dirty_ring_size;   /* Number of dirty GFNs per ring */
158     bool kvm_dirty_ring_with_bitmap;
159     uint64_t kvm_eager_split_size;  /* Eager Page Splitting chunk size */
160     struct KVMDirtyRingReaper reaper;
161     struct KVMMsrEnergy msr_energy;
162     NotifyVmexitOption notify_vmexit;
163     uint32_t notify_window;
164     uint32_t xen_version;
165     uint32_t xen_caps;
166     uint16_t xen_gnttab_max_frames;
167     uint16_t xen_evtchn_max_pirq;
168     char *device;
169 };
170 
171 void kvm_memory_listener_register(KVMState *s, KVMMemoryListener *kml,
172                                   AddressSpace *as, int as_id, const char *name);
173 
174 void kvm_set_max_memslot_size(hwaddr max_slot_size);
175 
176 /**
177  * kvm_hwpoison_page_add:
178  *
179  * Parameters:
180  *  @ram_addr: the address in the RAM for the poisoned page
181  *
182  * Add a poisoned page to the list
183  *
184  * Return: None.
185  */
186 void kvm_hwpoison_page_add(ram_addr_t ram_addr);
187 #endif
188