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