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 14*69700301SPhilippe Mathieu-Daudé /* header to be included in non-KVM-specific code */ 15*69700301SPhilippe Mathieu-Daudé 169c17d615SPaolo Bonzini #ifndef QEMU_KVM_H 179c17d615SPaolo Bonzini #define QEMU_KVM_H 189c17d615SPaolo Bonzini 19940e43aaSClaudio Fontana #include "qemu/accel.h" 20db1015e9SEduardo Habkost #include "qom/object.h" 219c17d615SPaolo Bonzini 22cbca3722SThomas Huth #ifdef NEED_CPU_H 239c17d615SPaolo Bonzini # ifdef CONFIG_KVM 249c17d615SPaolo Bonzini # include <linux/kvm.h> 25cbca3722SThomas Huth # define CONFIG_KVM_IS_POSSIBLE 269c17d615SPaolo Bonzini # endif 27cbca3722SThomas Huth #else 28cbca3722SThomas Huth # define CONFIG_KVM_IS_POSSIBLE 29cbca3722SThomas Huth #endif 30cbca3722SThomas Huth 31cbca3722SThomas Huth #ifdef CONFIG_KVM_IS_POSSIBLE 329c17d615SPaolo Bonzini 33d5286af5Sliguang extern bool kvm_allowed; 349c17d615SPaolo Bonzini extern bool kvm_kernel_irqchip; 3532c18a2dSMatt Gingell extern bool kvm_split_irqchip; 369c17d615SPaolo Bonzini extern bool kvm_async_interrupts_allowed; 37215e79c0SAlexander Graf extern bool kvm_halt_in_kernel_allowed; 3869e03ae6SNikolay Nikolaev extern bool kvm_eventfds_allowed; 399c17d615SPaolo Bonzini extern bool kvm_irqfds_allowed; 40f41389aeSEric Auger extern bool kvm_resamplefds_allowed; 419c17d615SPaolo Bonzini extern bool kvm_msi_via_irqfd_allowed; 429c17d615SPaolo Bonzini extern bool kvm_gsi_routing_allowed; 4376fe21deSAlexey Kardashevskiy extern bool kvm_gsi_direct_mapping; 44df9c8b75SJordan Justen extern bool kvm_readonly_mem_allowed; 4550bf31b9SPavel Fedin extern bool kvm_direct_msi_allowed; 4635108223SJason Wang extern bool kvm_ioeventfd_any_length_allowed; 47767a554aSPavel Fedin extern bool kvm_msi_use_devid; 489c17d615SPaolo Bonzini 499c17d615SPaolo Bonzini #define kvm_enabled() (kvm_allowed) 509c17d615SPaolo Bonzini /** 519c17d615SPaolo Bonzini * kvm_irqchip_in_kernel: 529c17d615SPaolo Bonzini * 5331c707fbSEduardo Habkost * Returns: true if an in-kernel irqchip was created. 549c17d615SPaolo Bonzini * What this actually means is architecture and machine model 5531c707fbSEduardo Habkost * specific: on PC, for instance, it means that the LAPIC 5631c707fbSEduardo Habkost * is in kernel. This function should never be used from generic 5731c707fbSEduardo Habkost * target-independent code: use one of the following functions or 5831c707fbSEduardo Habkost * some other specific check instead. 599c17d615SPaolo Bonzini */ 609c17d615SPaolo Bonzini #define kvm_irqchip_in_kernel() (kvm_kernel_irqchip) 619c17d615SPaolo Bonzini 629c17d615SPaolo Bonzini /** 6332c18a2dSMatt Gingell * kvm_irqchip_is_split: 6432c18a2dSMatt Gingell * 6531c707fbSEduardo Habkost * Returns: true if the irqchip implementation is split between 6631c707fbSEduardo Habkost * user and kernel space. The details are architecture and 6731c707fbSEduardo Habkost * machine specific. On PC, it means that the PIC, IOAPIC, and 6831c707fbSEduardo Habkost * PIT are in user space while the LAPIC is in the kernel. 6932c18a2dSMatt Gingell */ 7032c18a2dSMatt Gingell #define kvm_irqchip_is_split() (kvm_split_irqchip) 7132c18a2dSMatt Gingell 7232c18a2dSMatt Gingell /** 739c17d615SPaolo Bonzini * kvm_async_interrupts_enabled: 749c17d615SPaolo Bonzini * 759c17d615SPaolo Bonzini * Returns: true if we can deliver interrupts to KVM 769c17d615SPaolo Bonzini * asynchronously (ie by ioctl from any thread at any time) 779c17d615SPaolo Bonzini * rather than having to do interrupt delivery synchronously 789c17d615SPaolo Bonzini * (where the vcpu must be stopped at a suitable point first). 799c17d615SPaolo Bonzini */ 809c17d615SPaolo Bonzini #define kvm_async_interrupts_enabled() (kvm_async_interrupts_allowed) 819c17d615SPaolo Bonzini 829c17d615SPaolo Bonzini /** 83215e79c0SAlexander Graf * kvm_halt_in_kernel 84215e79c0SAlexander Graf * 85215e79c0SAlexander Graf * Returns: true if halted cpus should still get a KVM_RUN ioctl to run 86215e79c0SAlexander Graf * inside of kernel space. This only works if MP state is implemented. 87215e79c0SAlexander Graf */ 88215e79c0SAlexander Graf #define kvm_halt_in_kernel() (kvm_halt_in_kernel_allowed) 89215e79c0SAlexander Graf 90215e79c0SAlexander Graf /** 9169e03ae6SNikolay Nikolaev * kvm_eventfds_enabled: 9269e03ae6SNikolay Nikolaev * 9369e03ae6SNikolay Nikolaev * Returns: true if we can use eventfds to receive notifications 9469e03ae6SNikolay Nikolaev * from a KVM CPU (ie the kernel supports eventds and we are running 9569e03ae6SNikolay Nikolaev * with a configuration where it is meaningful to use them). 9669e03ae6SNikolay Nikolaev */ 9769e03ae6SNikolay Nikolaev #define kvm_eventfds_enabled() (kvm_eventfds_allowed) 9869e03ae6SNikolay Nikolaev 9969e03ae6SNikolay Nikolaev /** 1009c17d615SPaolo Bonzini * kvm_irqfds_enabled: 1019c17d615SPaolo Bonzini * 1029c17d615SPaolo Bonzini * Returns: true if we can use irqfds to inject interrupts into 1039c17d615SPaolo Bonzini * a KVM CPU (ie the kernel supports irqfds and we are running 1049c17d615SPaolo Bonzini * with a configuration where it is meaningful to use them). 1059c17d615SPaolo Bonzini */ 1069c17d615SPaolo Bonzini #define kvm_irqfds_enabled() (kvm_irqfds_allowed) 1079c17d615SPaolo Bonzini 1089c17d615SPaolo Bonzini /** 109f41389aeSEric Auger * kvm_resamplefds_enabled: 110f41389aeSEric Auger * 111f41389aeSEric Auger * Returns: true if we can use resamplefds to inject interrupts into 112f41389aeSEric Auger * a KVM CPU (ie the kernel supports resamplefds and we are running 113f41389aeSEric Auger * with a configuration where it is meaningful to use them). 114f41389aeSEric Auger */ 115f41389aeSEric Auger #define kvm_resamplefds_enabled() (kvm_resamplefds_allowed) 116f41389aeSEric Auger 117f41389aeSEric Auger /** 1189c17d615SPaolo Bonzini * kvm_msi_via_irqfd_enabled: 1199c17d615SPaolo Bonzini * 1209c17d615SPaolo Bonzini * Returns: true if we can route a PCI MSI (Message Signaled Interrupt) 1219c17d615SPaolo Bonzini * to a KVM CPU via an irqfd. This requires that the kernel supports 1229c17d615SPaolo Bonzini * this and that we're running in a configuration that permits it. 1239c17d615SPaolo Bonzini */ 1249c17d615SPaolo Bonzini #define kvm_msi_via_irqfd_enabled() (kvm_msi_via_irqfd_allowed) 1259c17d615SPaolo Bonzini 1269c17d615SPaolo Bonzini /** 1279c17d615SPaolo Bonzini * kvm_gsi_routing_enabled: 1289c17d615SPaolo Bonzini * 1299c17d615SPaolo Bonzini * Returns: true if GSI routing is enabled (ie the kernel supports 1309c17d615SPaolo Bonzini * it and we're running in a configuration that permits it). 1319c17d615SPaolo Bonzini */ 1329c17d615SPaolo Bonzini #define kvm_gsi_routing_enabled() (kvm_gsi_routing_allowed) 1339c17d615SPaolo Bonzini 134df9c8b75SJordan Justen /** 13576fe21deSAlexey Kardashevskiy * kvm_gsi_direct_mapping: 13676fe21deSAlexey Kardashevskiy * 13776fe21deSAlexey Kardashevskiy * Returns: true if GSI direct mapping is enabled. 13876fe21deSAlexey Kardashevskiy */ 13976fe21deSAlexey Kardashevskiy #define kvm_gsi_direct_mapping() (kvm_gsi_direct_mapping) 14076fe21deSAlexey Kardashevskiy 14176fe21deSAlexey Kardashevskiy /** 142df9c8b75SJordan Justen * kvm_readonly_mem_enabled: 143df9c8b75SJordan Justen * 144df9c8b75SJordan Justen * Returns: true if KVM readonly memory is enabled (ie the kernel 145df9c8b75SJordan Justen * supports it and we're running in a configuration that permits it). 146df9c8b75SJordan Justen */ 147df9c8b75SJordan Justen #define kvm_readonly_mem_enabled() (kvm_readonly_mem_allowed) 148df9c8b75SJordan Justen 14950bf31b9SPavel Fedin /** 15050bf31b9SPavel Fedin * kvm_direct_msi_enabled: 15150bf31b9SPavel Fedin * 15250bf31b9SPavel Fedin * Returns: true if KVM allows direct MSI injection. 15350bf31b9SPavel Fedin */ 15450bf31b9SPavel Fedin #define kvm_direct_msi_enabled() (kvm_direct_msi_allowed) 15550bf31b9SPavel Fedin 15635108223SJason Wang /** 15735108223SJason Wang * kvm_ioeventfd_any_length_enabled: 15835108223SJason Wang * Returns: true if KVM allows any length io eventfd. 15935108223SJason Wang */ 16035108223SJason Wang #define kvm_ioeventfd_any_length_enabled() (kvm_ioeventfd_any_length_allowed) 16135108223SJason Wang 162767a554aSPavel Fedin /** 163767a554aSPavel Fedin * kvm_msi_devid_required: 164767a554aSPavel Fedin * Returns: true if KVM requires a device id to be provided while 165767a554aSPavel Fedin * defining an MSI routing entry. 166767a554aSPavel Fedin */ 167767a554aSPavel Fedin #define kvm_msi_devid_required() (kvm_msi_use_devid) 168767a554aSPavel Fedin 1699c17d615SPaolo Bonzini #else 170cbca3722SThomas Huth 1719c17d615SPaolo Bonzini #define kvm_enabled() (0) 1729c17d615SPaolo Bonzini #define kvm_irqchip_in_kernel() (false) 17315eafc2eSPaolo Bonzini #define kvm_irqchip_is_split() (false) 1749c17d615SPaolo Bonzini #define kvm_async_interrupts_enabled() (false) 175215e79c0SAlexander Graf #define kvm_halt_in_kernel() (false) 17669e03ae6SNikolay Nikolaev #define kvm_eventfds_enabled() (false) 1779c17d615SPaolo Bonzini #define kvm_irqfds_enabled() (false) 178879904e8SEric Auger #define kvm_resamplefds_enabled() (false) 1799c17d615SPaolo Bonzini #define kvm_msi_via_irqfd_enabled() (false) 1809c17d615SPaolo Bonzini #define kvm_gsi_routing_allowed() (false) 18176fe21deSAlexey Kardashevskiy #define kvm_gsi_direct_mapping() (false) 182df9c8b75SJordan Justen #define kvm_readonly_mem_enabled() (false) 18350bf31b9SPavel Fedin #define kvm_direct_msi_enabled() (false) 18435108223SJason Wang #define kvm_ioeventfd_any_length_enabled() (false) 185767a554aSPavel Fedin #define kvm_msi_devid_required() (false) 186cbca3722SThomas Huth 187cbca3722SThomas Huth #endif /* CONFIG_KVM_IS_POSSIBLE */ 1889c17d615SPaolo Bonzini 1899c17d615SPaolo Bonzini struct kvm_run; 1909c17d615SPaolo Bonzini struct kvm_lapic_state; 1919e03a040SFrank Blaschka struct kvm_irq_routing_entry; 1929c17d615SPaolo Bonzini 1939c17d615SPaolo Bonzini typedef struct KVMCapabilityInfo { 1949c17d615SPaolo Bonzini const char *name; 1959c17d615SPaolo Bonzini int value; 1969c17d615SPaolo Bonzini } KVMCapabilityInfo; 1979c17d615SPaolo Bonzini 1989c17d615SPaolo Bonzini #define KVM_CAP_INFO(CAP) { "KVM_CAP_" stringify(CAP), KVM_CAP_##CAP } 1999c17d615SPaolo Bonzini #define KVM_CAP_LAST_INFO { NULL, 0 } 2009c17d615SPaolo Bonzini 2019c17d615SPaolo Bonzini struct KVMState; 20297e622deSEduardo Habkost 20397e622deSEduardo Habkost #define TYPE_KVM_ACCEL ACCEL_CLASS_NAME("kvm") 2049c17d615SPaolo Bonzini typedef struct KVMState KVMState; 2058110fa1dSEduardo Habkost DECLARE_INSTANCE_CHECKER(KVMState, KVM_STATE, 2068110fa1dSEduardo Habkost TYPE_KVM_ACCEL) 20797e622deSEduardo Habkost 2089c17d615SPaolo Bonzini extern KVMState *kvm_state; 2093607715aSDavid Gibson typedef struct Notifier Notifier; 2109c17d615SPaolo Bonzini 21195686908SLongpeng(Mike) typedef struct KVMRouteChange { 21295686908SLongpeng(Mike) KVMState *s; 21395686908SLongpeng(Mike) int changes; 21495686908SLongpeng(Mike) } KVMRouteChange; 21595686908SLongpeng(Mike) 2169c17d615SPaolo Bonzini /* external API */ 2179c17d615SPaolo Bonzini 218b8865591SIgor Mammedov bool kvm_has_free_slot(MachineState *ms); 21962dd4edaSGreg Kurz bool kvm_has_sync_mmu(void); 2209c17d615SPaolo Bonzini int kvm_has_vcpu_events(void); 2219c17d615SPaolo Bonzini int kvm_has_robust_singlestep(void); 2229c17d615SPaolo Bonzini int kvm_has_debugregs(void); 223ebbfef2fSLiran Alon int kvm_max_nested_state_length(void); 2249c17d615SPaolo Bonzini int kvm_has_pit_state2(void); 2259c17d615SPaolo Bonzini int kvm_has_many_ioeventfds(void); 2269c17d615SPaolo Bonzini int kvm_has_gsi_routing(void); 2279c17d615SPaolo Bonzini int kvm_has_intx_set_mask(void); 2289c17d615SPaolo Bonzini 2295d721b78SAlexander Graf /** 2305d721b78SAlexander Graf * kvm_arm_supports_user_irq 2315d721b78SAlexander Graf * 2325d721b78SAlexander Graf * Not all KVM implementations support notifications for kernel generated 2335d721b78SAlexander Graf * interrupt events to user space. This function indicates whether the current 2345d721b78SAlexander Graf * KVM implementation does support them. 2355d721b78SAlexander Graf * 2365d721b78SAlexander Graf * Returns: true if KVM supports using kernel generated IRQs from user space 2375d721b78SAlexander Graf */ 2385d721b78SAlexander Graf bool kvm_arm_supports_user_irq(void); 2395d721b78SAlexander Graf 240b20e3780SBrijesh Singh 24182bd4ca3SPhilippe Mathieu-Daudé int kvm_on_sigbus_vcpu(CPUState *cpu, int code, void *addr); 24282bd4ca3SPhilippe Mathieu-Daudé int kvm_on_sigbus(int code, void *addr); 24382bd4ca3SPhilippe Mathieu-Daudé 244504134d2SAndreas Färber #ifdef NEED_CPU_H 24533c11879SPaolo Bonzini #include "cpu.h" 2469c17d615SPaolo Bonzini 247c4cfef5eSIgor Mammedov void kvm_flush_coalesced_mmio_buffer(void); 248c4cfef5eSIgor Mammedov 249c7f1c537SAlex Bennée /** 250c7f1c537SAlex Bennée * kvm_update_guest_debug(): ensure KVM debug structures updated 251c7f1c537SAlex Bennée * @cs: the CPUState for this cpu 252c7f1c537SAlex Bennée * @reinject_trap: KVM trap injection control 253c7f1c537SAlex Bennée * 254c7f1c537SAlex Bennée * There are usually per-arch specifics which will be handled by 255c7f1c537SAlex Bennée * calling down to kvm_arch_update_guest_debug after the generic 256c7f1c537SAlex Bennée * fields have been set. 257c7f1c537SAlex Bennée */ 258c7f1c537SAlex Bennée #ifdef KVM_CAP_SET_GUEST_DEBUG 25938e478ecSStefan Weil int kvm_update_guest_debug(CPUState *cpu, unsigned long reinject_trap); 260c7f1c537SAlex Bennée #else 261c7f1c537SAlex Bennée static inline int kvm_update_guest_debug(CPUState *cpu, unsigned long reinject_trap) 262c7f1c537SAlex Bennée { 263c7f1c537SAlex Bennée return -EINVAL; 264c7f1c537SAlex Bennée } 265c7f1c537SAlex Bennée #endif 2669c17d615SPaolo Bonzini 2679c17d615SPaolo Bonzini /* internal API */ 2689c17d615SPaolo Bonzini 2699c17d615SPaolo Bonzini int kvm_ioctl(KVMState *s, int type, ...); 2709c17d615SPaolo Bonzini 2719c17d615SPaolo Bonzini int kvm_vm_ioctl(KVMState *s, int type, ...); 2729c17d615SPaolo Bonzini 273501a7ce7SAndreas Färber int kvm_vcpu_ioctl(CPUState *cpu, int type, ...); 2749c17d615SPaolo Bonzini 2750a6a7ccaSChristoffer Dall /** 2760a6a7ccaSChristoffer Dall * kvm_device_ioctl - call an ioctl on a kvm device 2770a6a7ccaSChristoffer Dall * @fd: The KVM device file descriptor as returned from KVM_CREATE_DEVICE 2780a6a7ccaSChristoffer Dall * @type: The device-ctrl ioctl number 2790a6a7ccaSChristoffer Dall * 2800a6a7ccaSChristoffer Dall * Returns: -errno on error, nonnegative on success 2810a6a7ccaSChristoffer Dall */ 2820a6a7ccaSChristoffer Dall int kvm_device_ioctl(int fd, int type, ...); 2830a6a7ccaSChristoffer Dall 2840a6a7ccaSChristoffer Dall /** 285d0a073a1SDominik Dingel * kvm_vm_check_attr - check for existence of a specific vm attribute 286d0a073a1SDominik Dingel * @s: The KVMState pointer 287d0a073a1SDominik Dingel * @group: the group 288d0a073a1SDominik Dingel * @attr: the attribute of that group to query for 289d0a073a1SDominik Dingel * 290d0a073a1SDominik Dingel * Returns: 1 if the attribute exists 291d0a073a1SDominik Dingel * 0 if the attribute either does not exist or if the vm device 292d0a073a1SDominik Dingel * interface is unavailable 293d0a073a1SDominik Dingel */ 294d0a073a1SDominik Dingel int kvm_vm_check_attr(KVMState *s, uint32_t group, uint64_t attr); 295d0a073a1SDominik Dingel 296d0a073a1SDominik Dingel /** 2974b3cfe72SPavel Fedin * kvm_device_check_attr - check for existence of a specific device attribute 2984b3cfe72SPavel Fedin * @fd: The device file descriptor 2994b3cfe72SPavel Fedin * @group: the group 3004b3cfe72SPavel Fedin * @attr: the attribute of that group to query for 3014b3cfe72SPavel Fedin * 3024b3cfe72SPavel Fedin * Returns: 1 if the attribute exists 3034b3cfe72SPavel Fedin * 0 if the attribute either does not exist or if the vm device 3044b3cfe72SPavel Fedin * interface is unavailable 3054b3cfe72SPavel Fedin */ 3064b3cfe72SPavel Fedin int kvm_device_check_attr(int fd, uint32_t group, uint64_t attr); 3074b3cfe72SPavel Fedin 3084b3cfe72SPavel Fedin /** 30984b6ea05SGreg Kurz * kvm_device_access - set or get value of a specific device attribute 3104b3cfe72SPavel Fedin * @fd: The device file descriptor 3114b3cfe72SPavel Fedin * @group: the group 3124b3cfe72SPavel Fedin * @attr: the attribute of that group to set or get 3134b3cfe72SPavel Fedin * @val: pointer to a storage area for the value 3144b3cfe72SPavel Fedin * @write: true for set and false for get operation 315556969e9SEric Auger * @errp: error object handle 3164b3cfe72SPavel Fedin * 317556969e9SEric Auger * Returns: 0 on success 318556969e9SEric Auger * < 0 on error 319556969e9SEric Auger * Use kvm_device_check_attr() in order to check for the availability 320556969e9SEric Auger * of optional attributes. 3214b3cfe72SPavel Fedin */ 322556969e9SEric Auger int kvm_device_access(int fd, int group, uint64_t attr, 323556969e9SEric Auger void *val, bool write, Error **errp); 3244b3cfe72SPavel Fedin 3254b3cfe72SPavel Fedin /** 3260a6a7ccaSChristoffer Dall * kvm_create_device - create a KVM device for the device control API 3270a6a7ccaSChristoffer Dall * @KVMState: The KVMState pointer 3280a6a7ccaSChristoffer Dall * @type: The KVM device type (see Documentation/virtual/kvm/devices in the 3290a6a7ccaSChristoffer Dall * kernel source) 3300a6a7ccaSChristoffer Dall * @test: If true, only test if device can be created, but don't actually 3310a6a7ccaSChristoffer Dall * create the device. 3320a6a7ccaSChristoffer Dall * 3330a6a7ccaSChristoffer Dall * Returns: -errno on error, nonnegative on success: @test ? 0 : device fd; 3340a6a7ccaSChristoffer Dall */ 3350a6a7ccaSChristoffer Dall int kvm_create_device(KVMState *s, uint64_t type, bool test); 3360a6a7ccaSChristoffer Dall 33729039acfSPeter Xu /** 33829039acfSPeter Xu * kvm_device_supported - probe whether KVM supports specific device 33929039acfSPeter Xu * 34029039acfSPeter Xu * @vmfd: The fd handler for VM 34129039acfSPeter Xu * @type: type of device 34229039acfSPeter Xu * 34329039acfSPeter Xu * @return: true if supported, otherwise false. 34429039acfSPeter Xu */ 34529039acfSPeter Xu bool kvm_device_supported(int vmfd, uint64_t type); 3460a6a7ccaSChristoffer Dall 3479c17d615SPaolo Bonzini /* Arch specific hooks */ 3489c17d615SPaolo Bonzini 3499c17d615SPaolo Bonzini extern const KVMCapabilityInfo kvm_arch_required_capabilities[]; 3509c17d615SPaolo Bonzini 3513dba0a33SPaolo Bonzini void kvm_arch_accel_class_init(ObjectClass *oc); 3523dba0a33SPaolo Bonzini 353501a7ce7SAndreas Färber void kvm_arch_pre_run(CPUState *cpu, struct kvm_run *run); 3544c663752SPaolo Bonzini MemTxAttrs kvm_arch_post_run(CPUState *cpu, struct kvm_run *run); 3559c17d615SPaolo Bonzini 356501a7ce7SAndreas Färber int kvm_arch_handle_exit(CPUState *cpu, struct kvm_run *run); 3579c17d615SPaolo Bonzini 358501a7ce7SAndreas Färber int kvm_arch_process_async_events(CPUState *cpu); 3599c17d615SPaolo Bonzini 360501a7ce7SAndreas Färber int kvm_arch_get_registers(CPUState *cpu); 3619c17d615SPaolo Bonzini 3629c17d615SPaolo Bonzini /* state subset only touched by the VCPU itself during runtime */ 3639c17d615SPaolo Bonzini #define KVM_PUT_RUNTIME_STATE 1 3649c17d615SPaolo Bonzini /* state subset modified during VCPU reset */ 3659c17d615SPaolo Bonzini #define KVM_PUT_RESET_STATE 2 3669c17d615SPaolo Bonzini /* full state set, modified during initialization or on vmload */ 3679c17d615SPaolo Bonzini #define KVM_PUT_FULL_STATE 3 3689c17d615SPaolo Bonzini 369501a7ce7SAndreas Färber int kvm_arch_put_registers(CPUState *cpu, int level); 3709c17d615SPaolo Bonzini 371b16565b3SMarcel Apfelbaum int kvm_arch_init(MachineState *ms, KVMState *s); 3729c17d615SPaolo Bonzini 373501a7ce7SAndreas Färber int kvm_arch_init_vcpu(CPUState *cpu); 374b1115c99SLiran Alon int kvm_arch_destroy_vcpu(CPUState *cpu); 3759c17d615SPaolo Bonzini 37641264b38SGreg Kurz bool kvm_vcpu_id_is_valid(int vcpu_id); 37741264b38SGreg Kurz 378b164e48eSEduardo Habkost /* Returns VCPU ID to be used on KVM_CREATE_VCPU ioctl() */ 379b164e48eSEduardo Habkost unsigned long kvm_arch_vcpu_id(CPUState *cpu); 380b164e48eSEduardo Habkost 381e24fd076SDongjiu Geng #ifdef KVM_HAVE_MCE_INJECTION 3822ae41db2SPaolo Bonzini void kvm_arch_on_sigbus_vcpu(CPUState *cpu, int code, void *addr); 3832ae41db2SPaolo Bonzini #endif 3849c17d615SPaolo Bonzini 3859c17d615SPaolo Bonzini void kvm_arch_init_irq_routing(KVMState *s); 3869c17d615SPaolo Bonzini 3879e03a040SFrank Blaschka int kvm_arch_fixup_msi_route(struct kvm_irq_routing_entry *route, 388dc9f06caSPavel Fedin uint64_t address, uint32_t data, PCIDevice *dev); 3899e03a040SFrank Blaschka 39038d87493SPeter Xu /* Notify arch about newly added MSI routes */ 39138d87493SPeter Xu int kvm_arch_add_msi_route_post(struct kvm_irq_routing_entry *route, 39238d87493SPeter Xu int vector, PCIDevice *dev); 39338d87493SPeter Xu /* Notify arch about released MSI routes */ 39438d87493SPeter Xu int kvm_arch_release_virq_post(int virq); 39538d87493SPeter Xu 3961850b6b7SEric Auger int kvm_arch_msi_data_to_gsi(uint32_t data); 3971850b6b7SEric Auger 3989c17d615SPaolo Bonzini int kvm_set_irq(KVMState *s, int irq, int level); 3999c17d615SPaolo Bonzini int kvm_irqchip_send_msi(KVMState *s, MSIMessage msg); 4009c17d615SPaolo Bonzini 4019c17d615SPaolo Bonzini void kvm_irqchip_add_irq_route(KVMState *s, int gsi, int irqchip, int pin); 4029c17d615SPaolo Bonzini 4033607715aSDavid Gibson void kvm_irqchip_add_change_notifier(Notifier *n); 4043607715aSDavid Gibson void kvm_irqchip_remove_change_notifier(Notifier *n); 4053607715aSDavid Gibson void kvm_irqchip_change_notify(void); 4063607715aSDavid Gibson 4079c17d615SPaolo Bonzini void kvm_get_apic_state(DeviceState *d, struct kvm_lapic_state *kapic); 4089c17d615SPaolo Bonzini 4099c17d615SPaolo Bonzini struct kvm_guest_debug; 4109c17d615SPaolo Bonzini struct kvm_debug_exit_arch; 4119c17d615SPaolo Bonzini 4129c17d615SPaolo Bonzini struct kvm_sw_breakpoint { 4139c17d615SPaolo Bonzini target_ulong pc; 4149c17d615SPaolo Bonzini target_ulong saved_insn; 4159c17d615SPaolo Bonzini int use_count; 4169c17d615SPaolo Bonzini QTAILQ_ENTRY(kvm_sw_breakpoint) entry; 4179c17d615SPaolo Bonzini }; 4189c17d615SPaolo Bonzini 419501a7ce7SAndreas Färber struct kvm_sw_breakpoint *kvm_find_sw_breakpoint(CPUState *cpu, 4209c17d615SPaolo Bonzini target_ulong pc); 4219c17d615SPaolo Bonzini 422501a7ce7SAndreas Färber int kvm_sw_breakpoints_active(CPUState *cpu); 4239c17d615SPaolo Bonzini 42480b7cd73SAndreas Färber int kvm_arch_insert_sw_breakpoint(CPUState *cpu, 4259c17d615SPaolo Bonzini struct kvm_sw_breakpoint *bp); 42680b7cd73SAndreas Färber int kvm_arch_remove_sw_breakpoint(CPUState *cpu, 4279c17d615SPaolo Bonzini struct kvm_sw_breakpoint *bp); 4289c17d615SPaolo Bonzini int kvm_arch_insert_hw_breakpoint(target_ulong addr, 4299c17d615SPaolo Bonzini target_ulong len, int type); 4309c17d615SPaolo Bonzini int kvm_arch_remove_hw_breakpoint(target_ulong addr, 4319c17d615SPaolo Bonzini target_ulong len, int type); 4329c17d615SPaolo Bonzini void kvm_arch_remove_all_hw_breakpoints(void); 4339c17d615SPaolo Bonzini 434501a7ce7SAndreas Färber void kvm_arch_update_guest_debug(CPUState *cpu, struct kvm_guest_debug *dbg); 4359c17d615SPaolo Bonzini 436501a7ce7SAndreas Färber bool kvm_arch_stop_on_emulation_error(CPUState *cpu); 4379c17d615SPaolo Bonzini 4389c17d615SPaolo Bonzini int kvm_check_extension(KVMState *s, unsigned int extension); 4399c17d615SPaolo Bonzini 4407d0a07faSAlexander Graf int kvm_vm_check_extension(KVMState *s, unsigned int extension); 4417d0a07faSAlexander Graf 44240f1ee27SCornelia Huck #define kvm_vm_enable_cap(s, capability, cap_flags, ...) \ 44340f1ee27SCornelia Huck ({ \ 44440f1ee27SCornelia Huck struct kvm_enable_cap cap = { \ 44540f1ee27SCornelia Huck .cap = capability, \ 44640f1ee27SCornelia Huck .flags = cap_flags, \ 44740f1ee27SCornelia Huck }; \ 44840f1ee27SCornelia Huck uint64_t args_tmp[] = { __VA_ARGS__ }; \ 4491b7ac7caSGreg Kurz size_t n = MIN(ARRAY_SIZE(args_tmp), ARRAY_SIZE(cap.args)); \ 4501b7ac7caSGreg Kurz memcpy(cap.args, args_tmp, n * sizeof(cap.args[0])); \ 45140f1ee27SCornelia Huck kvm_vm_ioctl(s, KVM_ENABLE_CAP, &cap); \ 45240f1ee27SCornelia Huck }) 45340f1ee27SCornelia Huck 45440f1ee27SCornelia Huck #define kvm_vcpu_enable_cap(cpu, capability, cap_flags, ...) \ 45540f1ee27SCornelia Huck ({ \ 45640f1ee27SCornelia Huck struct kvm_enable_cap cap = { \ 45740f1ee27SCornelia Huck .cap = capability, \ 45840f1ee27SCornelia Huck .flags = cap_flags, \ 45940f1ee27SCornelia Huck }; \ 46040f1ee27SCornelia Huck uint64_t args_tmp[] = { __VA_ARGS__ }; \ 4611b7ac7caSGreg Kurz size_t n = MIN(ARRAY_SIZE(args_tmp), ARRAY_SIZE(cap.args)); \ 4621b7ac7caSGreg Kurz memcpy(cap.args, args_tmp, n * sizeof(cap.args[0])); \ 46340f1ee27SCornelia Huck kvm_vcpu_ioctl(cpu, KVM_ENABLE_CAP, &cap); \ 46440f1ee27SCornelia Huck }) 46540f1ee27SCornelia Huck 4669c17d615SPaolo Bonzini uint32_t kvm_arch_get_supported_cpuid(KVMState *env, uint32_t function, 4679c17d615SPaolo Bonzini uint32_t index, int reg); 468ede146c2SPaolo Bonzini uint64_t kvm_arch_get_supported_msr_feature(KVMState *s, uint32_t index); 469f57bceb6SRobert Hoo 47097577fd4SJames Hogan 471aed6efb9SJames Hogan void kvm_set_sigmask_len(KVMState *s, unsigned int sigmask_len); 472aed6efb9SJames Hogan 47397577fd4SJames Hogan int kvm_physical_memory_addr_from_host(KVMState *s, void *ram_addr, 47497577fd4SJames Hogan hwaddr *phys_addr); 47597577fd4SJames Hogan 47697577fd4SJames Hogan #endif /* NEED_CPU_H */ 47797577fd4SJames Hogan 478dd1750d7SAndreas Färber void kvm_cpu_synchronize_state(CPUState *cpu); 4799c17d615SPaolo Bonzini 48018268b60SPaolo Bonzini void kvm_init_cpu_signals(CPUState *cpu); 48118268b60SPaolo Bonzini 482d1f6af6aSPeter Xu /** 483d1f6af6aSPeter Xu * kvm_irqchip_add_msi_route - Add MSI route for specific vector 484def4c557SLongpeng(Mike) * @c: KVMRouteChange instance. 485d1f6af6aSPeter Xu * @vector: which vector to add. This can be either MSI/MSIX 486d1f6af6aSPeter Xu * vector. The function will automatically detect whether 487d1f6af6aSPeter Xu * MSI/MSIX is enabled, and fetch corresponding MSI 488d1f6af6aSPeter Xu * message. 489d1f6af6aSPeter Xu * @dev: Owner PCI device to add the route. If @dev is specified 490d1f6af6aSPeter Xu * as @NULL, an empty MSI message will be inited. 491d1f6af6aSPeter Xu * @return: virq (>=0) when success, errno (<0) when failed. 492d1f6af6aSPeter Xu */ 493def4c557SLongpeng(Mike) int kvm_irqchip_add_msi_route(KVMRouteChange *c, int vector, PCIDevice *dev); 494dc9f06caSPavel Fedin int kvm_irqchip_update_msi_route(KVMState *s, int virq, MSIMessage msg, 495dc9f06caSPavel Fedin PCIDevice *dev); 4963f1fea0fSPeter Xu void kvm_irqchip_commit_routes(KVMState *s); 49795686908SLongpeng(Mike) 49895686908SLongpeng(Mike) static inline KVMRouteChange kvm_irqchip_begin_route_changes(KVMState *s) 49995686908SLongpeng(Mike) { 50095686908SLongpeng(Mike) return (KVMRouteChange) { .s = s, .changes = 0 }; 50195686908SLongpeng(Mike) } 50295686908SLongpeng(Mike) 50395686908SLongpeng(Mike) static inline void kvm_irqchip_commit_route_changes(KVMRouteChange *c) 50495686908SLongpeng(Mike) { 50595686908SLongpeng(Mike) if (c->changes) { 50695686908SLongpeng(Mike) kvm_irqchip_commit_routes(c->s); 50795686908SLongpeng(Mike) c->changes = 0; 50895686908SLongpeng(Mike) } 50995686908SLongpeng(Mike) } 51095686908SLongpeng(Mike) 5119c17d615SPaolo Bonzini void kvm_irqchip_release_virq(KVMState *s, int virq); 5129c17d615SPaolo Bonzini 513d426d9fbSCornelia Huck int kvm_irqchip_add_adapter_route(KVMState *s, AdapterInfo *adapter); 514977a8d9cSAndrey Smetanin int kvm_irqchip_add_hv_sint_route(KVMState *s, uint32_t vcpu, uint32_t sint); 515d426d9fbSCornelia Huck 5161c9b71a7SEric Auger int kvm_irqchip_add_irqfd_notifier_gsi(KVMState *s, EventNotifier *n, 517ca916d37SVincenzo Maffione EventNotifier *rn, int virq); 5181c9b71a7SEric Auger int kvm_irqchip_remove_irqfd_notifier_gsi(KVMState *s, EventNotifier *n, 5191c9b71a7SEric Auger int virq); 520197e3524SEric Auger int kvm_irqchip_add_irqfd_notifier(KVMState *s, EventNotifier *n, 521197e3524SEric Auger EventNotifier *rn, qemu_irq irq); 522197e3524SEric Auger int kvm_irqchip_remove_irqfd_notifier(KVMState *s, EventNotifier *n, 523197e3524SEric Auger qemu_irq irq); 524197e3524SEric Auger void kvm_irqchip_set_qemuirq_gsi(KVMState *s, qemu_irq irq, int gsi); 5259c17d615SPaolo Bonzini void kvm_pc_setup_irq_routing(bool pci_enabled); 5267b774593SAlexander Graf void kvm_init_irq_routing(KVMState *s); 527d6032e06SChristoffer Dall 5284376c40dSPaolo Bonzini bool kvm_kernel_irqchip_allowed(void); 5294376c40dSPaolo Bonzini bool kvm_kernel_irqchip_required(void); 5304376c40dSPaolo Bonzini bool kvm_kernel_irqchip_split(void); 5314376c40dSPaolo Bonzini 532d6032e06SChristoffer Dall /** 533d6032e06SChristoffer Dall * kvm_arch_irqchip_create: 534d6032e06SChristoffer Dall * @KVMState: The KVMState pointer 535d6032e06SChristoffer Dall * 536d6032e06SChristoffer Dall * Allow architectures to create an in-kernel irq chip themselves. 537d6032e06SChristoffer Dall * 538d6032e06SChristoffer Dall * Returns: < 0: error 539d6032e06SChristoffer Dall * 0: irq chip was not created 540d6032e06SChristoffer Dall * > 0: irq chip was created 541d6032e06SChristoffer Dall */ 5424376c40dSPaolo Bonzini int kvm_arch_irqchip_create(KVMState *s); 543ada4135fSCornelia Huck 544ada4135fSCornelia Huck /** 545ada4135fSCornelia Huck * kvm_set_one_reg - set a register value in KVM via KVM_SET_ONE_REG ioctl 546ada4135fSCornelia Huck * @id: The register ID 547ada4135fSCornelia Huck * @source: The pointer to the value to be set. It must point to a variable 548ada4135fSCornelia Huck * of the correct type/size for the register being accessed. 549ada4135fSCornelia Huck * 550ada4135fSCornelia Huck * Returns: 0 on success, or a negative errno on failure. 551ada4135fSCornelia Huck */ 552ada4135fSCornelia Huck int kvm_set_one_reg(CPUState *cs, uint64_t id, void *source); 553ada4135fSCornelia Huck 554ada4135fSCornelia Huck /** 555ada4135fSCornelia Huck * kvm_get_one_reg - get a register value from KVM via KVM_GET_ONE_REG ioctl 556ada4135fSCornelia Huck * @id: The register ID 557ada4135fSCornelia Huck * @target: The pointer where the value is to be stored. It must point to a 558ada4135fSCornelia Huck * variable of the correct type/size for the register being accessed. 559ada4135fSCornelia Huck * 560ada4135fSCornelia Huck * Returns: 0 on success, or a negative errno on failure. 561ada4135fSCornelia Huck */ 562ada4135fSCornelia Huck int kvm_get_one_reg(CPUState *cs, uint64_t id, void *target); 563c64abd1fSSam Bobroff struct ppc_radix_page_info *kvm_get_radix_page_info(void); 56444f2e6c1SBharata B Rao int kvm_get_max_memslots(void); 565c82d9d43SPeter Xu 566c82d9d43SPeter Xu /* Notify resamplefd for EOI of specific interrupts. */ 567c82d9d43SPeter Xu void kvm_resample_fd_notify(int gsi); 568c82d9d43SPeter Xu 56992a5199bSTom Lendacky /** 57092a5199bSTom Lendacky * kvm_cpu_check_are_resettable - return whether CPUs can be reset 57192a5199bSTom Lendacky * 57292a5199bSTom Lendacky * Returns: true: CPUs are resettable 57392a5199bSTom Lendacky * false: CPUs are not resettable 57492a5199bSTom Lendacky */ 57592a5199bSTom Lendacky bool kvm_cpu_check_are_resettable(void); 57692a5199bSTom Lendacky 57792a5199bSTom Lendacky bool kvm_arch_cpu_check_are_resettable(void); 57892a5199bSTom Lendacky 5797786ae40SHyman Huang(黄勇) bool kvm_dirty_ring_enabled(void); 5804a06a7ccSHyman Huang(黄勇) 5814a06a7ccSHyman Huang(黄勇) uint32_t kvm_dirty_ring_size(void); 5829c17d615SPaolo Bonzini #endif 583