xref: /qemu/include/sysemu/kvm_int.h (revision ce5a9832)
18571ed35SPaolo Bonzini /*
28571ed35SPaolo Bonzini  * Internal definitions for a target's KVM support
38571ed35SPaolo Bonzini  *
48571ed35SPaolo Bonzini  * This work is licensed under the terms of the GNU GPL, version 2 or later.
58571ed35SPaolo Bonzini  * See the COPYING file in the top-level directory.
68571ed35SPaolo Bonzini  *
78571ed35SPaolo Bonzini  */
88571ed35SPaolo Bonzini 
98571ed35SPaolo Bonzini #ifndef QEMU_KVM_INT_H
108571ed35SPaolo Bonzini #define QEMU_KVM_INT_H
118571ed35SPaolo Bonzini 
12ec150c7eSMarkus Armbruster #include "exec/memory.h"
135f8a6bceSChenyi Qiang #include "qapi/qapi-types-common.h"
14940e43aaSClaudio Fontana #include "qemu/accel.h"
15f39b7d2bSDavid Hildenbrand #include "qemu/queue.h"
168571ed35SPaolo Bonzini #include "sysemu/kvm.h"
178571ed35SPaolo Bonzini 
188571ed35SPaolo Bonzini typedef struct KVMSlot
198571ed35SPaolo Bonzini {
208571ed35SPaolo Bonzini     hwaddr start_addr;
218571ed35SPaolo Bonzini     ram_addr_t memory_size;
228571ed35SPaolo Bonzini     void *ram;
238571ed35SPaolo Bonzini     int slot;
248571ed35SPaolo Bonzini     int flags;
256c090d4aSShannon Zhao     int old_flags;
269f4bf4baSPeter Xu     /* Dirty bitmap cache for the slot */
279f4bf4baSPeter Xu     unsigned long *dirty_bmap;
28563d32baSPeter Xu     unsigned long dirty_bmap_size;
29e65e5f50SPeter Xu     /* Cache of the address space ID */
30e65e5f50SPeter Xu     int as_id;
312c20b27eSPeter Xu     /* Cache of the offset in ram address space */
322c20b27eSPeter Xu     ram_addr_t ram_start_offset;
33ce5a9832SChao Peng     int guest_memfd;
34ce5a9832SChao Peng     hwaddr guest_memfd_offset;
358571ed35SPaolo Bonzini } KVMSlot;
368571ed35SPaolo Bonzini 
37f39b7d2bSDavid Hildenbrand typedef struct KVMMemoryUpdate {
38f39b7d2bSDavid Hildenbrand     QSIMPLEQ_ENTRY(KVMMemoryUpdate) next;
39f39b7d2bSDavid Hildenbrand     MemoryRegionSection section;
40f39b7d2bSDavid Hildenbrand } KVMMemoryUpdate;
41f39b7d2bSDavid Hildenbrand 
427bbda04cSPaolo Bonzini typedef struct KVMMemoryListener {
437bbda04cSPaolo Bonzini     MemoryListener listener;
447bbda04cSPaolo Bonzini     KVMSlot *slots;
455b23186aSDavid Hildenbrand     unsigned int nr_used_slots;
4638bfe691SPaolo Bonzini     int as_id;
47f39b7d2bSDavid Hildenbrand     QSIMPLEQ_HEAD(, KVMMemoryUpdate) transaction_add;
48f39b7d2bSDavid Hildenbrand     QSIMPLEQ_HEAD(, KVMMemoryUpdate) transaction_del;
497bbda04cSPaolo Bonzini } KVMMemoryListener;
507bbda04cSPaolo Bonzini 
515f8a6bceSChenyi Qiang #define KVM_MSI_HASHTAB_SIZE    256
525f8a6bceSChenyi Qiang 
535f8a6bceSChenyi Qiang enum KVMDirtyRingReaperState {
545f8a6bceSChenyi Qiang     KVM_DIRTY_RING_REAPER_NONE = 0,
555f8a6bceSChenyi Qiang     /* The reaper is sleeping */
565f8a6bceSChenyi Qiang     KVM_DIRTY_RING_REAPER_WAIT,
575f8a6bceSChenyi Qiang     /* The reaper is reaping for dirty pages */
585f8a6bceSChenyi Qiang     KVM_DIRTY_RING_REAPER_REAPING,
595f8a6bceSChenyi Qiang };
605f8a6bceSChenyi Qiang 
615f8a6bceSChenyi Qiang /*
625f8a6bceSChenyi Qiang  * KVM reaper instance, responsible for collecting the KVM dirty bits
635f8a6bceSChenyi Qiang  * via the dirty ring.
645f8a6bceSChenyi Qiang  */
655f8a6bceSChenyi Qiang struct KVMDirtyRingReaper {
665f8a6bceSChenyi Qiang     /* The reaper thread */
675f8a6bceSChenyi Qiang     QemuThread reaper_thr;
685f8a6bceSChenyi Qiang     volatile uint64_t reaper_iteration; /* iteration number of reaper thr */
695f8a6bceSChenyi Qiang     volatile enum KVMDirtyRingReaperState reaper_state; /* reap thr state */
705f8a6bceSChenyi Qiang };
715f8a6bceSChenyi Qiang struct KVMState
725f8a6bceSChenyi Qiang {
735f8a6bceSChenyi Qiang     AccelState parent_obj;
745f8a6bceSChenyi Qiang 
755f8a6bceSChenyi Qiang     int nr_slots;
765f8a6bceSChenyi Qiang     int fd;
775f8a6bceSChenyi Qiang     int vmfd;
785f8a6bceSChenyi Qiang     int coalesced_mmio;
795f8a6bceSChenyi Qiang     int coalesced_pio;
805f8a6bceSChenyi Qiang     struct kvm_coalesced_mmio_ring *coalesced_mmio_ring;
815f8a6bceSChenyi Qiang     bool coalesced_flush_in_progress;
825f8a6bceSChenyi Qiang     int vcpu_events;
831e1e4879SPaolo Bonzini #ifdef TARGET_KVM_HAVE_GUEST_DEBUG
845f8a6bceSChenyi Qiang     QTAILQ_HEAD(, kvm_sw_breakpoint) kvm_sw_breakpoints;
855f8a6bceSChenyi Qiang #endif
865f8a6bceSChenyi Qiang     int max_nested_state_len;
875f8a6bceSChenyi Qiang     int kvm_shadow_mem;
885f8a6bceSChenyi Qiang     bool kernel_irqchip_allowed;
895f8a6bceSChenyi Qiang     bool kernel_irqchip_required;
905f8a6bceSChenyi Qiang     OnOffAuto kernel_irqchip_split;
915f8a6bceSChenyi Qiang     bool sync_mmu;
925c3131c3SPaolo Bonzini     bool guest_state_protected;
935f8a6bceSChenyi Qiang     uint64_t manual_dirty_log_protect;
945f8a6bceSChenyi Qiang     /* The man page (and posix) say ioctl numbers are signed int, but
955f8a6bceSChenyi Qiang      * they're not.  Linux, glibc and *BSD all treat ioctl numbers as
965f8a6bceSChenyi Qiang      * unsigned, and treating them as signed here can break things */
975f8a6bceSChenyi Qiang     unsigned irq_set_ioctl;
985f8a6bceSChenyi Qiang     unsigned int sigmask_len;
995f8a6bceSChenyi Qiang     GHashTable *gsimap;
1005f8a6bceSChenyi Qiang #ifdef KVM_CAP_IRQ_ROUTING
1015f8a6bceSChenyi Qiang     struct kvm_irq_routing *irq_routes;
1025f8a6bceSChenyi Qiang     int nr_allocated_irq_routes;
1035f8a6bceSChenyi Qiang     unsigned long *used_gsi_bitmap;
1045f8a6bceSChenyi Qiang     unsigned int gsi_count;
1055f8a6bceSChenyi Qiang #endif
1065f8a6bceSChenyi Qiang     KVMMemoryListener memory_listener;
1075f8a6bceSChenyi Qiang     QLIST_HEAD(, KVMParkedVcpu) kvm_parked_vcpus;
1085f8a6bceSChenyi Qiang 
1095f8a6bceSChenyi Qiang     /* For "info mtree -f" to tell if an MR is registered in KVM */
1105f8a6bceSChenyi Qiang     int nr_as;
1115f8a6bceSChenyi Qiang     struct KVMAs {
1125f8a6bceSChenyi Qiang         KVMMemoryListener *ml;
1135f8a6bceSChenyi Qiang         AddressSpace *as;
1145f8a6bceSChenyi Qiang     } *as;
1155f8a6bceSChenyi Qiang     uint64_t kvm_dirty_ring_bytes;  /* Size of the per-vcpu dirty ring */
1165f8a6bceSChenyi Qiang     uint32_t kvm_dirty_ring_size;   /* Number of dirty GFNs per ring */
117b20cc776SGavin Shan     bool kvm_dirty_ring_with_bitmap;
118c8f2eb5dSShameer Kolothum     uint64_t kvm_eager_split_size;  /* Eager Page Splitting chunk size */
1195f8a6bceSChenyi Qiang     struct KVMDirtyRingReaper reaper;
1205f8a6bceSChenyi Qiang     NotifyVmexitOption notify_vmexit;
1215f8a6bceSChenyi Qiang     uint32_t notify_window;
12261491cf4SDavid Woodhouse     uint32_t xen_version;
12361491cf4SDavid Woodhouse     uint32_t xen_caps;
1246f43f2eeSDavid Woodhouse     uint16_t xen_gnttab_max_frames;
125e16aff4cSDavid Woodhouse     uint16_t xen_evtchn_max_pirq;
126aef158b0SDaan De Meyer     char *device;
1275f8a6bceSChenyi Qiang };
1285f8a6bceSChenyi Qiang 
12938bfe691SPaolo Bonzini void kvm_memory_listener_register(KVMState *s, KVMMemoryListener *kml,
130142518bdSPeter Xu                                   AddressSpace *as, int as_id, const char *name);
13138bfe691SPaolo Bonzini 
132023ae9a8SIgor Mammedov void kvm_set_max_memslot_size(hwaddr max_slot_size);
1336b552b9bSDongjiu Geng 
1346b552b9bSDongjiu Geng /**
1356b552b9bSDongjiu Geng  * kvm_hwpoison_page_add:
1366b552b9bSDongjiu Geng  *
1376b552b9bSDongjiu Geng  * Parameters:
1386b552b9bSDongjiu Geng  *  @ram_addr: the address in the RAM for the poisoned page
1396b552b9bSDongjiu Geng  *
1406b552b9bSDongjiu Geng  * Add a poisoned page to the list
1416b552b9bSDongjiu Geng  *
1426b552b9bSDongjiu Geng  * Return: None.
1436b552b9bSDongjiu Geng  */
1446b552b9bSDongjiu Geng void kvm_hwpoison_page_add(ram_addr_t ram_addr);
1458571ed35SPaolo Bonzini #endif
146