xref: /openbmc/qemu/include/sysemu/kvm_int.h (revision e69b2c67)
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_used_slots;
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 
107     int nr_slots;
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     /* The man page (and posix) say ioctl numbers are signed int, but
127      * they're not.  Linux, glibc and *BSD all treat ioctl numbers as
128      * unsigned, and treating them as signed here can break things */
129     unsigned irq_set_ioctl;
130     unsigned int sigmask_len;
131     GHashTable *gsimap;
132 #ifdef KVM_CAP_IRQ_ROUTING
133     struct kvm_irq_routing *irq_routes;
134     int nr_allocated_irq_routes;
135     unsigned long *used_gsi_bitmap;
136     unsigned int gsi_count;
137 #endif
138     KVMMemoryListener memory_listener;
139     QLIST_HEAD(, KVMParkedVcpu) kvm_parked_vcpus;
140 
141     /* For "info mtree -f" to tell if an MR is registered in KVM */
142     int nr_as;
143     struct KVMAs {
144         KVMMemoryListener *ml;
145         AddressSpace *as;
146     } *as;
147     uint64_t kvm_dirty_ring_bytes;  /* Size of the per-vcpu dirty ring */
148     uint32_t kvm_dirty_ring_size;   /* Number of dirty GFNs per ring */
149     bool kvm_dirty_ring_with_bitmap;
150     uint64_t kvm_eager_split_size;  /* Eager Page Splitting chunk size */
151     struct KVMDirtyRingReaper reaper;
152     struct KVMMsrEnergy msr_energy;
153     NotifyVmexitOption notify_vmexit;
154     uint32_t notify_window;
155     uint32_t xen_version;
156     uint32_t xen_caps;
157     uint16_t xen_gnttab_max_frames;
158     uint16_t xen_evtchn_max_pirq;
159     char *device;
160 };
161 
162 void kvm_memory_listener_register(KVMState *s, KVMMemoryListener *kml,
163                                   AddressSpace *as, int as_id, const char *name);
164 
165 void kvm_set_max_memslot_size(hwaddr max_slot_size);
166 
167 /**
168  * kvm_hwpoison_page_add:
169  *
170  * Parameters:
171  *  @ram_addr: the address in the RAM for the poisoned page
172  *
173  * Add a poisoned page to the list
174  *
175  * Return: None.
176  */
177 void kvm_hwpoison_page_add(ram_addr_t ram_addr);
178 #endif
179