xref: /openbmc/qemu/include/sysemu/kvm_int.h (revision 26111a30)
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     int as_id;
50     QSIMPLEQ_HEAD(, KVMMemoryUpdate) transaction_add;
51     QSIMPLEQ_HEAD(, KVMMemoryUpdate) transaction_del;
52 } KVMMemoryListener;
53 
54 #define KVM_MSI_HASHTAB_SIZE    256
55 
56 typedef struct KVMHostTopoInfo {
57     /* Number of package on the Host */
58     unsigned int maxpkgs;
59     /* Number of cpus on the Host */
60     unsigned int maxcpus;
61     /* Number of cpus on each different package */
62     unsigned int *pkg_cpu_count;
63     /* Each package can have different maxticks */
64     unsigned int *maxticks;
65 } KVMHostTopoInfo;
66 
67 struct KVMMsrEnergy {
68     pid_t pid;
69     bool enable;
70     char *socket_path;
71     QIOChannelSocket *sioc;
72     QemuThread msr_thr;
73     unsigned int guest_vcpus;
74     unsigned int guest_vsockets;
75     X86CPUTopoInfo guest_topo_info;
76     KVMHostTopoInfo host_topo;
77     const CPUArchIdList *guest_cpu_list;
78     uint64_t *msr_value;
79     uint64_t msr_unit;
80     uint64_t msr_limit;
81     uint64_t msr_info;
82 };
83 
84 enum KVMDirtyRingReaperState {
85     KVM_DIRTY_RING_REAPER_NONE = 0,
86     /* The reaper is sleeping */
87     KVM_DIRTY_RING_REAPER_WAIT,
88     /* The reaper is reaping for dirty pages */
89     KVM_DIRTY_RING_REAPER_REAPING,
90 };
91 
92 /*
93  * KVM reaper instance, responsible for collecting the KVM dirty bits
94  * via the dirty ring.
95  */
96 struct KVMDirtyRingReaper {
97     /* The reaper thread */
98     QemuThread reaper_thr;
99     volatile uint64_t reaper_iteration; /* iteration number of reaper thr */
100     volatile enum KVMDirtyRingReaperState reaper_state; /* reap thr state */
101 };
102 struct KVMState
103 {
104     AccelState parent_obj;
105 
106     int nr_slots;
107     int fd;
108     int vmfd;
109     int coalesced_mmio;
110     int coalesced_pio;
111     struct kvm_coalesced_mmio_ring *coalesced_mmio_ring;
112     bool coalesced_flush_in_progress;
113     int vcpu_events;
114 #ifdef TARGET_KVM_HAVE_GUEST_DEBUG
115     QTAILQ_HEAD(, kvm_sw_breakpoint) kvm_sw_breakpoints;
116 #endif
117     int max_nested_state_len;
118     int kvm_shadow_mem;
119     bool kernel_irqchip_allowed;
120     bool kernel_irqchip_required;
121     OnOffAuto kernel_irqchip_split;
122     bool sync_mmu;
123     bool guest_state_protected;
124     uint64_t manual_dirty_log_protect;
125     /*
126      * Older POSIX says that ioctl numbers are signed int, but in
127      * practice they are not. (Newer POSIX doesn't specify ioctl
128      * at all.) Linux, glibc and *BSD all treat ioctl numbers as
129      * unsigned, and real-world ioctl values like KVM_GET_XSAVE have
130      * bit 31 set, which means that passing them via an 'int' will
131      * result in sign-extension when they get converted back to the
132      * 'unsigned long' which the ioctl() prototype uses. Luckily Linux
133      * always treats the argument as an unsigned 32-bit int, so any
134      * possible sign-extension is deliberately ignored, but for
135      * consistency we keep to the same type that glibc is using.
136      */
137     unsigned long irq_set_ioctl;
138     unsigned int sigmask_len;
139     GHashTable *gsimap;
140 #ifdef KVM_CAP_IRQ_ROUTING
141     struct kvm_irq_routing *irq_routes;
142     int nr_allocated_irq_routes;
143     unsigned long *used_gsi_bitmap;
144     unsigned int gsi_count;
145 #endif
146     KVMMemoryListener memory_listener;
147     QLIST_HEAD(, KVMParkedVcpu) kvm_parked_vcpus;
148 
149     /* For "info mtree -f" to tell if an MR is registered in KVM */
150     int nr_as;
151     struct KVMAs {
152         KVMMemoryListener *ml;
153         AddressSpace *as;
154     } *as;
155     uint64_t kvm_dirty_ring_bytes;  /* Size of the per-vcpu dirty ring */
156     uint32_t kvm_dirty_ring_size;   /* Number of dirty GFNs per ring */
157     bool kvm_dirty_ring_with_bitmap;
158     uint64_t kvm_eager_split_size;  /* Eager Page Splitting chunk size */
159     struct KVMDirtyRingReaper reaper;
160     struct KVMMsrEnergy msr_energy;
161     NotifyVmexitOption notify_vmexit;
162     uint32_t notify_window;
163     uint32_t xen_version;
164     uint32_t xen_caps;
165     uint16_t xen_gnttab_max_frames;
166     uint16_t xen_evtchn_max_pirq;
167     char *device;
168 };
169 
170 void kvm_memory_listener_register(KVMState *s, KVMMemoryListener *kml,
171                                   AddressSpace *as, int as_id, const char *name);
172 
173 void kvm_set_max_memslot_size(hwaddr max_slot_size);
174 
175 /**
176  * kvm_hwpoison_page_add:
177  *
178  * Parameters:
179  *  @ram_addr: the address in the RAM for the poisoned page
180  *
181  * Add a poisoned page to the list
182  *
183  * Return: None.
184  */
185 void kvm_hwpoison_page_add(ram_addr_t ram_addr);
186 #endif
187