xref: /openbmc/qemu/include/sysemu/kvm.h (revision 491d6e80)
19c17d615SPaolo Bonzini /*
29c17d615SPaolo Bonzini  * QEMU KVM support
39c17d615SPaolo Bonzini  *
49c17d615SPaolo Bonzini  * Copyright IBM, Corp. 2008
59c17d615SPaolo Bonzini  *
69c17d615SPaolo Bonzini  * Authors:
79c17d615SPaolo Bonzini  *  Anthony Liguori   <aliguori@us.ibm.com>
89c17d615SPaolo Bonzini  *
99c17d615SPaolo Bonzini  * This work is licensed under the terms of the GNU GPL, version 2 or later.
109c17d615SPaolo Bonzini  * See the COPYING file in the top-level directory.
119c17d615SPaolo Bonzini  *
129c17d615SPaolo Bonzini  */
139c17d615SPaolo Bonzini 
149c17d615SPaolo Bonzini #ifndef QEMU_KVM_H
159c17d615SPaolo Bonzini #define QEMU_KVM_H
169c17d615SPaolo Bonzini 
179c17d615SPaolo Bonzini #include <errno.h>
189c17d615SPaolo Bonzini #include "config-host.h"
199c17d615SPaolo Bonzini #include "qemu/queue.h"
20504134d2SAndreas Färber #include "qom/cpu.h"
219c17d615SPaolo Bonzini 
229c17d615SPaolo Bonzini #ifdef CONFIG_KVM
239c17d615SPaolo Bonzini #include <linux/kvm.h>
249c17d615SPaolo Bonzini #include <linux/kvm_para.h>
259ca58923SEduardo Habkost #else
269ca58923SEduardo Habkost /* These constants must never be used at runtime if kvm_enabled() is false.
279ca58923SEduardo Habkost  * They exist so we don't need #ifdefs around KVM-specific code that already
289ca58923SEduardo Habkost  * checks kvm_enabled() properly.
299ca58923SEduardo Habkost  */
309ca58923SEduardo Habkost #define KVM_CPUID_SIGNATURE      0
319ca58923SEduardo Habkost #define KVM_CPUID_FEATURES       0
329ca58923SEduardo Habkost #define KVM_FEATURE_CLOCKSOURCE  0
339ca58923SEduardo Habkost #define KVM_FEATURE_NOP_IO_DELAY 0
349ca58923SEduardo Habkost #define KVM_FEATURE_MMU_OP       0
359ca58923SEduardo Habkost #define KVM_FEATURE_CLOCKSOURCE2 0
369ca58923SEduardo Habkost #define KVM_FEATURE_ASYNC_PF     0
379ca58923SEduardo Habkost #define KVM_FEATURE_STEAL_TIME   0
389ca58923SEduardo Habkost #define KVM_FEATURE_PV_EOI       0
39d61a23baSEduardo Habkost #define KVM_FEATURE_CLOCKSOURCE_STABLE_BIT 0
409c17d615SPaolo Bonzini #endif
419c17d615SPaolo Bonzini 
42d5286af5Sliguang extern bool kvm_allowed;
439c17d615SPaolo Bonzini extern bool kvm_kernel_irqchip;
449c17d615SPaolo Bonzini extern bool kvm_async_interrupts_allowed;
459c17d615SPaolo Bonzini extern bool kvm_irqfds_allowed;
469c17d615SPaolo Bonzini extern bool kvm_msi_via_irqfd_allowed;
479c17d615SPaolo Bonzini extern bool kvm_gsi_routing_allowed;
48df9c8b75SJordan Justen extern bool kvm_readonly_mem_allowed;
499c17d615SPaolo Bonzini 
509c17d615SPaolo Bonzini #if defined CONFIG_KVM || !defined NEED_CPU_H
519c17d615SPaolo Bonzini #define kvm_enabled()           (kvm_allowed)
529c17d615SPaolo Bonzini /**
539c17d615SPaolo Bonzini  * kvm_irqchip_in_kernel:
549c17d615SPaolo Bonzini  *
559c17d615SPaolo Bonzini  * Returns: true if the user asked us to create an in-kernel
569c17d615SPaolo Bonzini  * irqchip via the "kernel_irqchip=on" machine option.
579c17d615SPaolo Bonzini  * What this actually means is architecture and machine model
589c17d615SPaolo Bonzini  * specific: on PC, for instance, it means that the LAPIC,
599c17d615SPaolo Bonzini  * IOAPIC and PIT are all in kernel. This function should never
609c17d615SPaolo Bonzini  * be used from generic target-independent code: use one of the
619c17d615SPaolo Bonzini  * following functions or some other specific check instead.
629c17d615SPaolo Bonzini  */
639c17d615SPaolo Bonzini #define kvm_irqchip_in_kernel() (kvm_kernel_irqchip)
649c17d615SPaolo Bonzini 
659c17d615SPaolo Bonzini /**
669c17d615SPaolo Bonzini  * kvm_async_interrupts_enabled:
679c17d615SPaolo Bonzini  *
689c17d615SPaolo Bonzini  * Returns: true if we can deliver interrupts to KVM
699c17d615SPaolo Bonzini  * asynchronously (ie by ioctl from any thread at any time)
709c17d615SPaolo Bonzini  * rather than having to do interrupt delivery synchronously
719c17d615SPaolo Bonzini  * (where the vcpu must be stopped at a suitable point first).
729c17d615SPaolo Bonzini  */
739c17d615SPaolo Bonzini #define kvm_async_interrupts_enabled() (kvm_async_interrupts_allowed)
749c17d615SPaolo Bonzini 
759c17d615SPaolo Bonzini /**
769c17d615SPaolo Bonzini  * kvm_irqfds_enabled:
779c17d615SPaolo Bonzini  *
789c17d615SPaolo Bonzini  * Returns: true if we can use irqfds to inject interrupts into
799c17d615SPaolo Bonzini  * a KVM CPU (ie the kernel supports irqfds and we are running
809c17d615SPaolo Bonzini  * with a configuration where it is meaningful to use them).
819c17d615SPaolo Bonzini  */
829c17d615SPaolo Bonzini #define kvm_irqfds_enabled() (kvm_irqfds_allowed)
839c17d615SPaolo Bonzini 
849c17d615SPaolo Bonzini /**
859c17d615SPaolo Bonzini  * kvm_msi_via_irqfd_enabled:
869c17d615SPaolo Bonzini  *
879c17d615SPaolo Bonzini  * Returns: true if we can route a PCI MSI (Message Signaled Interrupt)
889c17d615SPaolo Bonzini  * to a KVM CPU via an irqfd. This requires that the kernel supports
899c17d615SPaolo Bonzini  * this and that we're running in a configuration that permits it.
909c17d615SPaolo Bonzini  */
919c17d615SPaolo Bonzini #define kvm_msi_via_irqfd_enabled() (kvm_msi_via_irqfd_allowed)
929c17d615SPaolo Bonzini 
939c17d615SPaolo Bonzini /**
949c17d615SPaolo Bonzini  * kvm_gsi_routing_enabled:
959c17d615SPaolo Bonzini  *
969c17d615SPaolo Bonzini  * Returns: true if GSI routing is enabled (ie the kernel supports
979c17d615SPaolo Bonzini  * it and we're running in a configuration that permits it).
989c17d615SPaolo Bonzini  */
999c17d615SPaolo Bonzini #define kvm_gsi_routing_enabled() (kvm_gsi_routing_allowed)
1009c17d615SPaolo Bonzini 
101df9c8b75SJordan Justen /**
102df9c8b75SJordan Justen  * kvm_readonly_mem_enabled:
103df9c8b75SJordan Justen  *
104df9c8b75SJordan Justen  * Returns: true if KVM readonly memory is enabled (ie the kernel
105df9c8b75SJordan Justen  * supports it and we're running in a configuration that permits it).
106df9c8b75SJordan Justen  */
107df9c8b75SJordan Justen #define kvm_readonly_mem_enabled() (kvm_readonly_mem_allowed)
108df9c8b75SJordan Justen 
1099c17d615SPaolo Bonzini #else
1109c17d615SPaolo Bonzini #define kvm_enabled()           (0)
1119c17d615SPaolo Bonzini #define kvm_irqchip_in_kernel() (false)
1129c17d615SPaolo Bonzini #define kvm_async_interrupts_enabled() (false)
1139c17d615SPaolo Bonzini #define kvm_irqfds_enabled() (false)
1149c17d615SPaolo Bonzini #define kvm_msi_via_irqfd_enabled() (false)
1159c17d615SPaolo Bonzini #define kvm_gsi_routing_allowed() (false)
116df9c8b75SJordan Justen #define kvm_readonly_mem_enabled() (false)
1179c17d615SPaolo Bonzini #endif
1189c17d615SPaolo Bonzini 
1199c17d615SPaolo Bonzini struct kvm_run;
1209c17d615SPaolo Bonzini struct kvm_lapic_state;
1219c17d615SPaolo Bonzini 
1229c17d615SPaolo Bonzini typedef struct KVMCapabilityInfo {
1239c17d615SPaolo Bonzini     const char *name;
1249c17d615SPaolo Bonzini     int value;
1259c17d615SPaolo Bonzini } KVMCapabilityInfo;
1269c17d615SPaolo Bonzini 
1279c17d615SPaolo Bonzini #define KVM_CAP_INFO(CAP) { "KVM_CAP_" stringify(CAP), KVM_CAP_##CAP }
1289c17d615SPaolo Bonzini #define KVM_CAP_LAST_INFO { NULL, 0 }
1299c17d615SPaolo Bonzini 
1309c17d615SPaolo Bonzini struct KVMState;
1319c17d615SPaolo Bonzini typedef struct KVMState KVMState;
1329c17d615SPaolo Bonzini extern KVMState *kvm_state;
1339c17d615SPaolo Bonzini 
1349c17d615SPaolo Bonzini /* external API */
1359c17d615SPaolo Bonzini 
1369c17d615SPaolo Bonzini int kvm_init(void);
1379c17d615SPaolo Bonzini 
1389c17d615SPaolo Bonzini int kvm_has_sync_mmu(void);
1399c17d615SPaolo Bonzini int kvm_has_vcpu_events(void);
1409c17d615SPaolo Bonzini int kvm_has_robust_singlestep(void);
1419c17d615SPaolo Bonzini int kvm_has_debugregs(void);
1429c17d615SPaolo Bonzini int kvm_has_xsave(void);
1439c17d615SPaolo Bonzini int kvm_has_xcrs(void);
1449c17d615SPaolo Bonzini int kvm_has_pit_state2(void);
1459c17d615SPaolo Bonzini int kvm_has_many_ioeventfds(void);
1469c17d615SPaolo Bonzini int kvm_has_gsi_routing(void);
1479c17d615SPaolo Bonzini int kvm_has_intx_set_mask(void);
1489c17d615SPaolo Bonzini 
149504134d2SAndreas Färber int kvm_init_vcpu(CPUState *cpu);
1509c17d615SPaolo Bonzini 
151504134d2SAndreas Färber #ifdef NEED_CPU_H
1529c17d615SPaolo Bonzini int kvm_cpu_exec(CPUArchState *env);
1539c17d615SPaolo Bonzini 
1549c17d615SPaolo Bonzini #if !defined(CONFIG_USER_ONLY)
1556eebf958SPaolo Bonzini void *kvm_ram_alloc(ram_addr_t size);
1566eebf958SPaolo Bonzini void *kvm_arch_ram_alloc(ram_addr_t size);
1579c17d615SPaolo Bonzini #endif
1589c17d615SPaolo Bonzini 
159c4cfef5eSIgor Mammedov void kvm_setup_guest_memory(void *start, size_t size);
160c4cfef5eSIgor Mammedov void kvm_flush_coalesced_mmio_buffer(void);
161c4cfef5eSIgor Mammedov 
1629c17d615SPaolo Bonzini int kvm_insert_breakpoint(CPUArchState *current_env, target_ulong addr,
1639c17d615SPaolo Bonzini                           target_ulong len, int type);
1649c17d615SPaolo Bonzini int kvm_remove_breakpoint(CPUArchState *current_env, target_ulong addr,
1659c17d615SPaolo Bonzini                           target_ulong len, int type);
1669c17d615SPaolo Bonzini void kvm_remove_all_breakpoints(CPUArchState *current_env);
1679c17d615SPaolo Bonzini int kvm_update_guest_debug(CPUArchState *env, unsigned long reinject_trap);
1689c17d615SPaolo Bonzini #ifndef _WIN32
169*491d6e80SAndreas Färber int kvm_set_signal_mask(CPUState *cpu, const sigset_t *sigset);
1709c17d615SPaolo Bonzini #endif
1719c17d615SPaolo Bonzini 
172290adf38SAndreas Färber int kvm_on_sigbus_vcpu(CPUState *cpu, int code, void *addr);
1739c17d615SPaolo Bonzini int kvm_on_sigbus(int code, void *addr);
1749c17d615SPaolo Bonzini 
1759c17d615SPaolo Bonzini /* internal API */
1769c17d615SPaolo Bonzini 
1779c17d615SPaolo Bonzini int kvm_ioctl(KVMState *s, int type, ...);
1789c17d615SPaolo Bonzini 
1799c17d615SPaolo Bonzini int kvm_vm_ioctl(KVMState *s, int type, ...);
1809c17d615SPaolo Bonzini 
181501a7ce7SAndreas Färber int kvm_vcpu_ioctl(CPUState *cpu, int type, ...);
1829c17d615SPaolo Bonzini 
1839c17d615SPaolo Bonzini /* Arch specific hooks */
1849c17d615SPaolo Bonzini 
1859c17d615SPaolo Bonzini extern const KVMCapabilityInfo kvm_arch_required_capabilities[];
1869c17d615SPaolo Bonzini 
187501a7ce7SAndreas Färber void kvm_arch_pre_run(CPUState *cpu, struct kvm_run *run);
188501a7ce7SAndreas Färber void kvm_arch_post_run(CPUState *cpu, struct kvm_run *run);
1899c17d615SPaolo Bonzini 
190501a7ce7SAndreas Färber int kvm_arch_handle_exit(CPUState *cpu, struct kvm_run *run);
1919c17d615SPaolo Bonzini 
192501a7ce7SAndreas Färber int kvm_arch_process_async_events(CPUState *cpu);
1939c17d615SPaolo Bonzini 
194501a7ce7SAndreas Färber int kvm_arch_get_registers(CPUState *cpu);
1959c17d615SPaolo Bonzini 
1969c17d615SPaolo Bonzini /* state subset only touched by the VCPU itself during runtime */
1979c17d615SPaolo Bonzini #define KVM_PUT_RUNTIME_STATE   1
1989c17d615SPaolo Bonzini /* state subset modified during VCPU reset */
1999c17d615SPaolo Bonzini #define KVM_PUT_RESET_STATE     2
2009c17d615SPaolo Bonzini /* full state set, modified during initialization or on vmload */
2019c17d615SPaolo Bonzini #define KVM_PUT_FULL_STATE      3
2029c17d615SPaolo Bonzini 
203501a7ce7SAndreas Färber int kvm_arch_put_registers(CPUState *cpu, int level);
2049c17d615SPaolo Bonzini 
2059c17d615SPaolo Bonzini int kvm_arch_init(KVMState *s);
2069c17d615SPaolo Bonzini 
207501a7ce7SAndreas Färber int kvm_arch_init_vcpu(CPUState *cpu);
2089c17d615SPaolo Bonzini 
209b164e48eSEduardo Habkost /* Returns VCPU ID to be used on KVM_CREATE_VCPU ioctl() */
210b164e48eSEduardo Habkost unsigned long kvm_arch_vcpu_id(CPUState *cpu);
211b164e48eSEduardo Habkost 
212501a7ce7SAndreas Färber void kvm_arch_reset_vcpu(CPUState *cpu);
2139c17d615SPaolo Bonzini 
214501a7ce7SAndreas Färber int kvm_arch_on_sigbus_vcpu(CPUState *cpu, int code, void *addr);
2159c17d615SPaolo Bonzini int kvm_arch_on_sigbus(int code, void *addr);
2169c17d615SPaolo Bonzini 
2179c17d615SPaolo Bonzini void kvm_arch_init_irq_routing(KVMState *s);
2189c17d615SPaolo Bonzini 
2199c17d615SPaolo Bonzini int kvm_set_irq(KVMState *s, int irq, int level);
2209c17d615SPaolo Bonzini int kvm_irqchip_send_msi(KVMState *s, MSIMessage msg);
2219c17d615SPaolo Bonzini 
2229c17d615SPaolo Bonzini void kvm_irqchip_add_irq_route(KVMState *s, int gsi, int irqchip, int pin);
2239c17d615SPaolo Bonzini 
2249c17d615SPaolo Bonzini void kvm_put_apic_state(DeviceState *d, struct kvm_lapic_state *kapic);
2259c17d615SPaolo Bonzini void kvm_get_apic_state(DeviceState *d, struct kvm_lapic_state *kapic);
2269c17d615SPaolo Bonzini 
2279c17d615SPaolo Bonzini struct kvm_guest_debug;
2289c17d615SPaolo Bonzini struct kvm_debug_exit_arch;
2299c17d615SPaolo Bonzini 
2309c17d615SPaolo Bonzini struct kvm_sw_breakpoint {
2319c17d615SPaolo Bonzini     target_ulong pc;
2329c17d615SPaolo Bonzini     target_ulong saved_insn;
2339c17d615SPaolo Bonzini     int use_count;
2349c17d615SPaolo Bonzini     QTAILQ_ENTRY(kvm_sw_breakpoint) entry;
2359c17d615SPaolo Bonzini };
2369c17d615SPaolo Bonzini 
2379c17d615SPaolo Bonzini QTAILQ_HEAD(kvm_sw_breakpoint_head, kvm_sw_breakpoint);
2389c17d615SPaolo Bonzini 
239501a7ce7SAndreas Färber struct kvm_sw_breakpoint *kvm_find_sw_breakpoint(CPUState *cpu,
2409c17d615SPaolo Bonzini                                                  target_ulong pc);
2419c17d615SPaolo Bonzini 
242501a7ce7SAndreas Färber int kvm_sw_breakpoints_active(CPUState *cpu);
2439c17d615SPaolo Bonzini 
244501a7ce7SAndreas Färber int kvm_arch_insert_sw_breakpoint(CPUState *current_cpu,
2459c17d615SPaolo Bonzini                                   struct kvm_sw_breakpoint *bp);
246501a7ce7SAndreas Färber int kvm_arch_remove_sw_breakpoint(CPUState *current_cpu,
2479c17d615SPaolo Bonzini                                   struct kvm_sw_breakpoint *bp);
2489c17d615SPaolo Bonzini int kvm_arch_insert_hw_breakpoint(target_ulong addr,
2499c17d615SPaolo Bonzini                                   target_ulong len, int type);
2509c17d615SPaolo Bonzini int kvm_arch_remove_hw_breakpoint(target_ulong addr,
2519c17d615SPaolo Bonzini                                   target_ulong len, int type);
2529c17d615SPaolo Bonzini void kvm_arch_remove_all_hw_breakpoints(void);
2539c17d615SPaolo Bonzini 
254501a7ce7SAndreas Färber void kvm_arch_update_guest_debug(CPUState *cpu, struct kvm_guest_debug *dbg);
2559c17d615SPaolo Bonzini 
256501a7ce7SAndreas Färber bool kvm_arch_stop_on_emulation_error(CPUState *cpu);
2579c17d615SPaolo Bonzini 
2589c17d615SPaolo Bonzini int kvm_check_extension(KVMState *s, unsigned int extension);
2599c17d615SPaolo Bonzini 
2609c17d615SPaolo Bonzini uint32_t kvm_arch_get_supported_cpuid(KVMState *env, uint32_t function,
2619c17d615SPaolo Bonzini                                       uint32_t index, int reg);
262dd1750d7SAndreas Färber void kvm_cpu_synchronize_state(CPUState *cpu);
2639c17d615SPaolo Bonzini 
2649c17d615SPaolo Bonzini /* generic hooks - to be moved/refactored once there are more users */
2659c17d615SPaolo Bonzini 
266cb446ecaSAndreas Färber static inline void cpu_synchronize_state(CPUState *cpu)
2679c17d615SPaolo Bonzini {
2689c17d615SPaolo Bonzini     if (kvm_enabled()) {
269cb446ecaSAndreas Färber         kvm_cpu_synchronize_state(cpu);
2709c17d615SPaolo Bonzini     }
2719c17d615SPaolo Bonzini }
2729c17d615SPaolo Bonzini 
27313eed94eSIgor Mammedov #if !defined(CONFIG_USER_ONLY)
27413eed94eSIgor Mammedov int kvm_physical_memory_addr_from_host(KVMState *s, void *ram_addr,
27513eed94eSIgor Mammedov                                        hwaddr *phys_addr);
27613eed94eSIgor Mammedov #endif
27713eed94eSIgor Mammedov 
27813eed94eSIgor Mammedov #endif /* NEED_CPU_H */
27913eed94eSIgor Mammedov 
28013eed94eSIgor Mammedov void kvm_cpu_synchronize_post_reset(CPUState *cpu);
28113eed94eSIgor Mammedov void kvm_cpu_synchronize_post_init(CPUState *cpu);
28213eed94eSIgor Mammedov 
2833f24a58fSIgor Mammedov static inline void cpu_synchronize_post_reset(CPUState *cpu)
2849c17d615SPaolo Bonzini {
2859c17d615SPaolo Bonzini     if (kvm_enabled()) {
2863f24a58fSIgor Mammedov         kvm_cpu_synchronize_post_reset(cpu);
2879c17d615SPaolo Bonzini     }
2889c17d615SPaolo Bonzini }
2899c17d615SPaolo Bonzini 
2903f24a58fSIgor Mammedov static inline void cpu_synchronize_post_init(CPUState *cpu)
2919c17d615SPaolo Bonzini {
2929c17d615SPaolo Bonzini     if (kvm_enabled()) {
2933f24a58fSIgor Mammedov         kvm_cpu_synchronize_post_init(cpu);
2949c17d615SPaolo Bonzini     }
2959c17d615SPaolo Bonzini }
2969c17d615SPaolo Bonzini 
2979c17d615SPaolo Bonzini int kvm_irqchip_add_msi_route(KVMState *s, MSIMessage msg);
2989c17d615SPaolo Bonzini int kvm_irqchip_update_msi_route(KVMState *s, int virq, MSIMessage msg);
2999c17d615SPaolo Bonzini void kvm_irqchip_release_virq(KVMState *s, int virq);
3009c17d615SPaolo Bonzini 
3019c17d615SPaolo Bonzini int kvm_irqchip_add_irqfd_notifier(KVMState *s, EventNotifier *n, int virq);
3029c17d615SPaolo Bonzini int kvm_irqchip_remove_irqfd_notifier(KVMState *s, EventNotifier *n, int virq);
3039c17d615SPaolo Bonzini void kvm_pc_gsi_handler(void *opaque, int n, int level);
3049c17d615SPaolo Bonzini void kvm_pc_setup_irq_routing(bool pci_enabled);
3059c17d615SPaolo Bonzini #endif
306