xref: /openbmc/qemu/include/sysemu/kvm_int.h (revision bef6a77f)
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 
18 typedef struct KVMSlot
19 {
20     hwaddr start_addr;
21     ram_addr_t memory_size;
22     void *ram;
23     int slot;
24     int flags;
25     int old_flags;
26     /* Dirty bitmap cache for the slot */
27     unsigned long *dirty_bmap;
28     unsigned long dirty_bmap_size;
29     /* Cache of the address space ID */
30     int as_id;
31     /* Cache of the offset in ram address space */
32     ram_addr_t ram_start_offset;
33     int guest_memfd;
34     hwaddr guest_memfd_offset;
35 } KVMSlot;
36 
37 typedef struct KVMMemoryUpdate {
38     QSIMPLEQ_ENTRY(KVMMemoryUpdate) next;
39     MemoryRegionSection section;
40 } KVMMemoryUpdate;
41 
42 typedef struct KVMMemoryListener {
43     MemoryListener listener;
44     KVMSlot *slots;
45     unsigned int nr_used_slots;
46     int as_id;
47     QSIMPLEQ_HEAD(, KVMMemoryUpdate) transaction_add;
48     QSIMPLEQ_HEAD(, KVMMemoryUpdate) transaction_del;
49 } KVMMemoryListener;
50 
51 #define KVM_MSI_HASHTAB_SIZE    256
52 
53 enum KVMDirtyRingReaperState {
54     KVM_DIRTY_RING_REAPER_NONE = 0,
55     /* The reaper is sleeping */
56     KVM_DIRTY_RING_REAPER_WAIT,
57     /* The reaper is reaping for dirty pages */
58     KVM_DIRTY_RING_REAPER_REAPING,
59 };
60 
61 /*
62  * KVM reaper instance, responsible for collecting the KVM dirty bits
63  * via the dirty ring.
64  */
65 struct KVMDirtyRingReaper {
66     /* The reaper thread */
67     QemuThread reaper_thr;
68     volatile uint64_t reaper_iteration; /* iteration number of reaper thr */
69     volatile enum KVMDirtyRingReaperState reaper_state; /* reap thr state */
70 };
71 struct KVMState
72 {
73     AccelState parent_obj;
74 
75     int nr_slots;
76     int fd;
77     int vmfd;
78     int coalesced_mmio;
79     int coalesced_pio;
80     struct kvm_coalesced_mmio_ring *coalesced_mmio_ring;
81     bool coalesced_flush_in_progress;
82     int vcpu_events;
83 #ifdef TARGET_KVM_HAVE_GUEST_DEBUG
84     QTAILQ_HEAD(, kvm_sw_breakpoint) kvm_sw_breakpoints;
85 #endif
86     int max_nested_state_len;
87     int kvm_shadow_mem;
88     bool kernel_irqchip_allowed;
89     bool kernel_irqchip_required;
90     OnOffAuto kernel_irqchip_split;
91     bool sync_mmu;
92     bool guest_state_protected;
93     uint64_t manual_dirty_log_protect;
94     /* The man page (and posix) say ioctl numbers are signed int, but
95      * they're not.  Linux, glibc and *BSD all treat ioctl numbers as
96      * unsigned, and treating them as signed here can break things */
97     unsigned irq_set_ioctl;
98     unsigned int sigmask_len;
99     GHashTable *gsimap;
100 #ifdef KVM_CAP_IRQ_ROUTING
101     struct kvm_irq_routing *irq_routes;
102     int nr_allocated_irq_routes;
103     unsigned long *used_gsi_bitmap;
104     unsigned int gsi_count;
105 #endif
106     KVMMemoryListener memory_listener;
107     QLIST_HEAD(, KVMParkedVcpu) kvm_parked_vcpus;
108 
109     /* For "info mtree -f" to tell if an MR is registered in KVM */
110     int nr_as;
111     struct KVMAs {
112         KVMMemoryListener *ml;
113         AddressSpace *as;
114     } *as;
115     uint64_t kvm_dirty_ring_bytes;  /* Size of the per-vcpu dirty ring */
116     uint32_t kvm_dirty_ring_size;   /* Number of dirty GFNs per ring */
117     bool kvm_dirty_ring_with_bitmap;
118     uint64_t kvm_eager_split_size;  /* Eager Page Splitting chunk size */
119     struct KVMDirtyRingReaper reaper;
120     NotifyVmexitOption notify_vmexit;
121     uint32_t notify_window;
122     uint32_t xen_version;
123     uint32_t xen_caps;
124     uint16_t xen_gnttab_max_frames;
125     uint16_t xen_evtchn_max_pirq;
126     char *device;
127 };
128 
129 void kvm_memory_listener_register(KVMState *s, KVMMemoryListener *kml,
130                                   AddressSpace *as, int as_id, const char *name);
131 
132 void kvm_set_max_memslot_size(hwaddr max_slot_size);
133 
134 /**
135  * kvm_hwpoison_page_add:
136  *
137  * Parameters:
138  *  @ram_addr: the address in the RAM for the poisoned page
139  *
140  * Add a poisoned page to the list
141  *
142  * Return: None.
143  */
144 void kvm_hwpoison_page_add(ram_addr_t ram_addr);
145 #endif
146