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 "qemu/queue.h" 182e5b09fdSMarkus Armbruster #include "hw/core/cpu.h" 194c663752SPaolo Bonzini #include "exec/memattrs.h" 20940e43aaSClaudio Fontana #include "qemu/accel.h" 21db1015e9SEduardo Habkost #include "qom/object.h" 229c17d615SPaolo Bonzini 23cbca3722SThomas Huth #ifdef NEED_CPU_H 249c17d615SPaolo Bonzini # ifdef CONFIG_KVM 259c17d615SPaolo Bonzini # include <linux/kvm.h> 26cbca3722SThomas Huth # define CONFIG_KVM_IS_POSSIBLE 279c17d615SPaolo Bonzini # endif 28cbca3722SThomas Huth #else 29cbca3722SThomas Huth # define CONFIG_KVM_IS_POSSIBLE 30cbca3722SThomas Huth #endif 31cbca3722SThomas Huth 32cbca3722SThomas Huth #ifdef CONFIG_KVM_IS_POSSIBLE 339c17d615SPaolo Bonzini 34d5286af5Sliguang extern bool kvm_allowed; 359c17d615SPaolo Bonzini extern bool kvm_kernel_irqchip; 3632c18a2dSMatt Gingell extern bool kvm_split_irqchip; 379c17d615SPaolo Bonzini extern bool kvm_async_interrupts_allowed; 38215e79c0SAlexander Graf extern bool kvm_halt_in_kernel_allowed; 3969e03ae6SNikolay Nikolaev extern bool kvm_eventfds_allowed; 409c17d615SPaolo Bonzini extern bool kvm_irqfds_allowed; 41f41389aeSEric Auger extern bool kvm_resamplefds_allowed; 429c17d615SPaolo Bonzini extern bool kvm_msi_via_irqfd_allowed; 439c17d615SPaolo Bonzini extern bool kvm_gsi_routing_allowed; 4476fe21deSAlexey Kardashevskiy extern bool kvm_gsi_direct_mapping; 45df9c8b75SJordan Justen extern bool kvm_readonly_mem_allowed; 4650bf31b9SPavel Fedin extern bool kvm_direct_msi_allowed; 4735108223SJason Wang extern bool kvm_ioeventfd_any_length_allowed; 48767a554aSPavel Fedin extern bool kvm_msi_use_devid; 49*12bc5b4cSMaxim Levitsky extern bool kvm_has_guest_debug; 50*12bc5b4cSMaxim Levitsky extern int kvm_sstep_flags; 519c17d615SPaolo Bonzini 529c17d615SPaolo Bonzini #define kvm_enabled() (kvm_allowed) 539c17d615SPaolo Bonzini /** 549c17d615SPaolo Bonzini * kvm_irqchip_in_kernel: 559c17d615SPaolo Bonzini * 5631c707fbSEduardo Habkost * Returns: true if an in-kernel irqchip was created. 579c17d615SPaolo Bonzini * What this actually means is architecture and machine model 5831c707fbSEduardo Habkost * specific: on PC, for instance, it means that the LAPIC 5931c707fbSEduardo Habkost * is in kernel. This function should never be used from generic 6031c707fbSEduardo Habkost * target-independent code: use one of the following functions or 6131c707fbSEduardo Habkost * some other specific check instead. 629c17d615SPaolo Bonzini */ 639c17d615SPaolo Bonzini #define kvm_irqchip_in_kernel() (kvm_kernel_irqchip) 649c17d615SPaolo Bonzini 659c17d615SPaolo Bonzini /** 6632c18a2dSMatt Gingell * kvm_irqchip_is_split: 6732c18a2dSMatt Gingell * 6831c707fbSEduardo Habkost * Returns: true if the irqchip implementation is split between 6931c707fbSEduardo Habkost * user and kernel space. The details are architecture and 7031c707fbSEduardo Habkost * machine specific. On PC, it means that the PIC, IOAPIC, and 7131c707fbSEduardo Habkost * PIT are in user space while the LAPIC is in the kernel. 7232c18a2dSMatt Gingell */ 7332c18a2dSMatt Gingell #define kvm_irqchip_is_split() (kvm_split_irqchip) 7432c18a2dSMatt Gingell 7532c18a2dSMatt Gingell /** 769c17d615SPaolo Bonzini * kvm_async_interrupts_enabled: 779c17d615SPaolo Bonzini * 789c17d615SPaolo Bonzini * Returns: true if we can deliver interrupts to KVM 799c17d615SPaolo Bonzini * asynchronously (ie by ioctl from any thread at any time) 809c17d615SPaolo Bonzini * rather than having to do interrupt delivery synchronously 819c17d615SPaolo Bonzini * (where the vcpu must be stopped at a suitable point first). 829c17d615SPaolo Bonzini */ 839c17d615SPaolo Bonzini #define kvm_async_interrupts_enabled() (kvm_async_interrupts_allowed) 849c17d615SPaolo Bonzini 859c17d615SPaolo Bonzini /** 86215e79c0SAlexander Graf * kvm_halt_in_kernel 87215e79c0SAlexander Graf * 88215e79c0SAlexander Graf * Returns: true if halted cpus should still get a KVM_RUN ioctl to run 89215e79c0SAlexander Graf * inside of kernel space. This only works if MP state is implemented. 90215e79c0SAlexander Graf */ 91215e79c0SAlexander Graf #define kvm_halt_in_kernel() (kvm_halt_in_kernel_allowed) 92215e79c0SAlexander Graf 93215e79c0SAlexander Graf /** 9469e03ae6SNikolay Nikolaev * kvm_eventfds_enabled: 9569e03ae6SNikolay Nikolaev * 9669e03ae6SNikolay Nikolaev * Returns: true if we can use eventfds to receive notifications 9769e03ae6SNikolay Nikolaev * from a KVM CPU (ie the kernel supports eventds and we are running 9869e03ae6SNikolay Nikolaev * with a configuration where it is meaningful to use them). 9969e03ae6SNikolay Nikolaev */ 10069e03ae6SNikolay Nikolaev #define kvm_eventfds_enabled() (kvm_eventfds_allowed) 10169e03ae6SNikolay Nikolaev 10269e03ae6SNikolay Nikolaev /** 1039c17d615SPaolo Bonzini * kvm_irqfds_enabled: 1049c17d615SPaolo Bonzini * 1059c17d615SPaolo Bonzini * Returns: true if we can use irqfds to inject interrupts into 1069c17d615SPaolo Bonzini * a KVM CPU (ie the kernel supports irqfds and we are running 1079c17d615SPaolo Bonzini * with a configuration where it is meaningful to use them). 1089c17d615SPaolo Bonzini */ 1099c17d615SPaolo Bonzini #define kvm_irqfds_enabled() (kvm_irqfds_allowed) 1109c17d615SPaolo Bonzini 1119c17d615SPaolo Bonzini /** 112f41389aeSEric Auger * kvm_resamplefds_enabled: 113f41389aeSEric Auger * 114f41389aeSEric Auger * Returns: true if we can use resamplefds to inject interrupts into 115f41389aeSEric Auger * a KVM CPU (ie the kernel supports resamplefds and we are running 116f41389aeSEric Auger * with a configuration where it is meaningful to use them). 117f41389aeSEric Auger */ 118f41389aeSEric Auger #define kvm_resamplefds_enabled() (kvm_resamplefds_allowed) 119f41389aeSEric Auger 120f41389aeSEric Auger /** 1219c17d615SPaolo Bonzini * kvm_msi_via_irqfd_enabled: 1229c17d615SPaolo Bonzini * 1239c17d615SPaolo Bonzini * Returns: true if we can route a PCI MSI (Message Signaled Interrupt) 1249c17d615SPaolo Bonzini * to a KVM CPU via an irqfd. This requires that the kernel supports 1259c17d615SPaolo Bonzini * this and that we're running in a configuration that permits it. 1269c17d615SPaolo Bonzini */ 1279c17d615SPaolo Bonzini #define kvm_msi_via_irqfd_enabled() (kvm_msi_via_irqfd_allowed) 1289c17d615SPaolo Bonzini 1299c17d615SPaolo Bonzini /** 1309c17d615SPaolo Bonzini * kvm_gsi_routing_enabled: 1319c17d615SPaolo Bonzini * 1329c17d615SPaolo Bonzini * Returns: true if GSI routing is enabled (ie the kernel supports 1339c17d615SPaolo Bonzini * it and we're running in a configuration that permits it). 1349c17d615SPaolo Bonzini */ 1359c17d615SPaolo Bonzini #define kvm_gsi_routing_enabled() (kvm_gsi_routing_allowed) 1369c17d615SPaolo Bonzini 137df9c8b75SJordan Justen /** 13876fe21deSAlexey Kardashevskiy * kvm_gsi_direct_mapping: 13976fe21deSAlexey Kardashevskiy * 14076fe21deSAlexey Kardashevskiy * Returns: true if GSI direct mapping is enabled. 14176fe21deSAlexey Kardashevskiy */ 14276fe21deSAlexey Kardashevskiy #define kvm_gsi_direct_mapping() (kvm_gsi_direct_mapping) 14376fe21deSAlexey Kardashevskiy 14476fe21deSAlexey Kardashevskiy /** 145df9c8b75SJordan Justen * kvm_readonly_mem_enabled: 146df9c8b75SJordan Justen * 147df9c8b75SJordan Justen * Returns: true if KVM readonly memory is enabled (ie the kernel 148df9c8b75SJordan Justen * supports it and we're running in a configuration that permits it). 149df9c8b75SJordan Justen */ 150df9c8b75SJordan Justen #define kvm_readonly_mem_enabled() (kvm_readonly_mem_allowed) 151df9c8b75SJordan Justen 15250bf31b9SPavel Fedin /** 15350bf31b9SPavel Fedin * kvm_direct_msi_enabled: 15450bf31b9SPavel Fedin * 15550bf31b9SPavel Fedin * Returns: true if KVM allows direct MSI injection. 15650bf31b9SPavel Fedin */ 15750bf31b9SPavel Fedin #define kvm_direct_msi_enabled() (kvm_direct_msi_allowed) 15850bf31b9SPavel Fedin 15935108223SJason Wang /** 16035108223SJason Wang * kvm_ioeventfd_any_length_enabled: 16135108223SJason Wang * Returns: true if KVM allows any length io eventfd. 16235108223SJason Wang */ 16335108223SJason Wang #define kvm_ioeventfd_any_length_enabled() (kvm_ioeventfd_any_length_allowed) 16435108223SJason Wang 165767a554aSPavel Fedin /** 166767a554aSPavel Fedin * kvm_msi_devid_required: 167767a554aSPavel Fedin * Returns: true if KVM requires a device id to be provided while 168767a554aSPavel Fedin * defining an MSI routing entry. 169767a554aSPavel Fedin */ 170767a554aSPavel Fedin #define kvm_msi_devid_required() (kvm_msi_use_devid) 171767a554aSPavel Fedin 172*12bc5b4cSMaxim Levitsky /* 173*12bc5b4cSMaxim Levitsky * Does KVM support guest debugging 174*12bc5b4cSMaxim Levitsky */ 175*12bc5b4cSMaxim Levitsky #define kvm_supports_guest_debug() (kvm_has_guest_debug) 176*12bc5b4cSMaxim Levitsky 177*12bc5b4cSMaxim Levitsky /* 178*12bc5b4cSMaxim Levitsky * kvm_supported_sstep_flags 179*12bc5b4cSMaxim Levitsky * Returns: SSTEP_* flags that KVM supports for guest debug 180*12bc5b4cSMaxim Levitsky */ 181*12bc5b4cSMaxim Levitsky #define kvm_get_supported_sstep_flags() (kvm_sstep_flags) 182*12bc5b4cSMaxim Levitsky 1839c17d615SPaolo Bonzini #else 184cbca3722SThomas Huth 1859c17d615SPaolo Bonzini #define kvm_enabled() (0) 1869c17d615SPaolo Bonzini #define kvm_irqchip_in_kernel() (false) 18715eafc2eSPaolo Bonzini #define kvm_irqchip_is_split() (false) 1889c17d615SPaolo Bonzini #define kvm_async_interrupts_enabled() (false) 189215e79c0SAlexander Graf #define kvm_halt_in_kernel() (false) 19069e03ae6SNikolay Nikolaev #define kvm_eventfds_enabled() (false) 1919c17d615SPaolo Bonzini #define kvm_irqfds_enabled() (false) 192879904e8SEric Auger #define kvm_resamplefds_enabled() (false) 1939c17d615SPaolo Bonzini #define kvm_msi_via_irqfd_enabled() (false) 1949c17d615SPaolo Bonzini #define kvm_gsi_routing_allowed() (false) 19576fe21deSAlexey Kardashevskiy #define kvm_gsi_direct_mapping() (false) 196df9c8b75SJordan Justen #define kvm_readonly_mem_enabled() (false) 19750bf31b9SPavel Fedin #define kvm_direct_msi_enabled() (false) 19835108223SJason Wang #define kvm_ioeventfd_any_length_enabled() (false) 199767a554aSPavel Fedin #define kvm_msi_devid_required() (false) 200*12bc5b4cSMaxim Levitsky #define kvm_supports_guest_debug() (false) 201*12bc5b4cSMaxim Levitsky #define kvm_get_supported_sstep_flags() (0) 202cbca3722SThomas Huth 203cbca3722SThomas Huth #endif /* CONFIG_KVM_IS_POSSIBLE */ 2049c17d615SPaolo Bonzini 2059c17d615SPaolo Bonzini struct kvm_run; 2069c17d615SPaolo Bonzini struct kvm_lapic_state; 2079e03a040SFrank Blaschka struct kvm_irq_routing_entry; 2089c17d615SPaolo Bonzini 2099c17d615SPaolo Bonzini typedef struct KVMCapabilityInfo { 2109c17d615SPaolo Bonzini const char *name; 2119c17d615SPaolo Bonzini int value; 2129c17d615SPaolo Bonzini } KVMCapabilityInfo; 2139c17d615SPaolo Bonzini 2149c17d615SPaolo Bonzini #define KVM_CAP_INFO(CAP) { "KVM_CAP_" stringify(CAP), KVM_CAP_##CAP } 2159c17d615SPaolo Bonzini #define KVM_CAP_LAST_INFO { NULL, 0 } 2169c17d615SPaolo Bonzini 2179c17d615SPaolo Bonzini struct KVMState; 21897e622deSEduardo Habkost 21997e622deSEduardo Habkost #define TYPE_KVM_ACCEL ACCEL_CLASS_NAME("kvm") 2209c17d615SPaolo Bonzini typedef struct KVMState KVMState; 2218110fa1dSEduardo Habkost DECLARE_INSTANCE_CHECKER(KVMState, KVM_STATE, 2228110fa1dSEduardo Habkost TYPE_KVM_ACCEL) 22397e622deSEduardo Habkost 2249c17d615SPaolo Bonzini extern KVMState *kvm_state; 2253607715aSDavid Gibson typedef struct Notifier Notifier; 2269c17d615SPaolo Bonzini 2279c17d615SPaolo Bonzini /* external API */ 2289c17d615SPaolo Bonzini 229b8865591SIgor Mammedov bool kvm_has_free_slot(MachineState *ms); 23062dd4edaSGreg Kurz bool kvm_has_sync_mmu(void); 2319c17d615SPaolo Bonzini int kvm_has_vcpu_events(void); 2329c17d615SPaolo Bonzini int kvm_has_robust_singlestep(void); 2339c17d615SPaolo Bonzini int kvm_has_debugregs(void); 234ebbfef2fSLiran Alon int kvm_max_nested_state_length(void); 2359c17d615SPaolo Bonzini int kvm_has_pit_state2(void); 2369c17d615SPaolo Bonzini int kvm_has_many_ioeventfds(void); 2379c17d615SPaolo Bonzini int kvm_has_gsi_routing(void); 2389c17d615SPaolo Bonzini int kvm_has_intx_set_mask(void); 2399c17d615SPaolo Bonzini 2405d721b78SAlexander Graf /** 2415d721b78SAlexander Graf * kvm_arm_supports_user_irq 2425d721b78SAlexander Graf * 2435d721b78SAlexander Graf * Not all KVM implementations support notifications for kernel generated 2445d721b78SAlexander Graf * interrupt events to user space. This function indicates whether the current 2455d721b78SAlexander Graf * KVM implementation does support them. 2465d721b78SAlexander Graf * 2475d721b78SAlexander Graf * Returns: true if KVM supports using kernel generated IRQs from user space 2485d721b78SAlexander Graf */ 2495d721b78SAlexander Graf bool kvm_arm_supports_user_irq(void); 2505d721b78SAlexander Graf 251b20e3780SBrijesh Singh 252504134d2SAndreas Färber #ifdef NEED_CPU_H 25333c11879SPaolo Bonzini #include "cpu.h" 2549c17d615SPaolo Bonzini 255c4cfef5eSIgor Mammedov void kvm_flush_coalesced_mmio_buffer(void); 256c4cfef5eSIgor Mammedov 25762278814SAndreas Färber int kvm_insert_breakpoint(CPUState *cpu, target_ulong addr, 2589c17d615SPaolo Bonzini target_ulong len, int type); 25962278814SAndreas Färber int kvm_remove_breakpoint(CPUState *cpu, target_ulong addr, 2609c17d615SPaolo Bonzini target_ulong len, int type); 2611d5791f4SAndreas Färber void kvm_remove_all_breakpoints(CPUState *cpu); 26238e478ecSStefan Weil int kvm_update_guest_debug(CPUState *cpu, unsigned long reinject_trap); 2639c17d615SPaolo Bonzini 264290adf38SAndreas Färber int kvm_on_sigbus_vcpu(CPUState *cpu, int code, void *addr); 2659c17d615SPaolo Bonzini int kvm_on_sigbus(int code, void *addr); 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 351501a7ce7SAndreas Färber void kvm_arch_pre_run(CPUState *cpu, struct kvm_run *run); 3524c663752SPaolo Bonzini MemTxAttrs kvm_arch_post_run(CPUState *cpu, struct kvm_run *run); 3539c17d615SPaolo Bonzini 354501a7ce7SAndreas Färber int kvm_arch_handle_exit(CPUState *cpu, struct kvm_run *run); 3559c17d615SPaolo Bonzini 356501a7ce7SAndreas Färber int kvm_arch_process_async_events(CPUState *cpu); 3579c17d615SPaolo Bonzini 358501a7ce7SAndreas Färber int kvm_arch_get_registers(CPUState *cpu); 3599c17d615SPaolo Bonzini 3609c17d615SPaolo Bonzini /* state subset only touched by the VCPU itself during runtime */ 3619c17d615SPaolo Bonzini #define KVM_PUT_RUNTIME_STATE 1 3629c17d615SPaolo Bonzini /* state subset modified during VCPU reset */ 3639c17d615SPaolo Bonzini #define KVM_PUT_RESET_STATE 2 3649c17d615SPaolo Bonzini /* full state set, modified during initialization or on vmload */ 3659c17d615SPaolo Bonzini #define KVM_PUT_FULL_STATE 3 3669c17d615SPaolo Bonzini 367501a7ce7SAndreas Färber int kvm_arch_put_registers(CPUState *cpu, int level); 3689c17d615SPaolo Bonzini 369b16565b3SMarcel Apfelbaum int kvm_arch_init(MachineState *ms, KVMState *s); 3709c17d615SPaolo Bonzini 371501a7ce7SAndreas Färber int kvm_arch_init_vcpu(CPUState *cpu); 372b1115c99SLiran Alon int kvm_arch_destroy_vcpu(CPUState *cpu); 3739c17d615SPaolo Bonzini 37441264b38SGreg Kurz bool kvm_vcpu_id_is_valid(int vcpu_id); 37541264b38SGreg Kurz 376b164e48eSEduardo Habkost /* Returns VCPU ID to be used on KVM_CREATE_VCPU ioctl() */ 377b164e48eSEduardo Habkost unsigned long kvm_arch_vcpu_id(CPUState *cpu); 378b164e48eSEduardo Habkost 379e24fd076SDongjiu Geng #ifdef KVM_HAVE_MCE_INJECTION 3802ae41db2SPaolo Bonzini void kvm_arch_on_sigbus_vcpu(CPUState *cpu, int code, void *addr); 3812ae41db2SPaolo Bonzini #endif 3829c17d615SPaolo Bonzini 3839c17d615SPaolo Bonzini void kvm_arch_init_irq_routing(KVMState *s); 3849c17d615SPaolo Bonzini 3859e03a040SFrank Blaschka int kvm_arch_fixup_msi_route(struct kvm_irq_routing_entry *route, 386dc9f06caSPavel Fedin uint64_t address, uint32_t data, PCIDevice *dev); 3879e03a040SFrank Blaschka 38838d87493SPeter Xu /* Notify arch about newly added MSI routes */ 38938d87493SPeter Xu int kvm_arch_add_msi_route_post(struct kvm_irq_routing_entry *route, 39038d87493SPeter Xu int vector, PCIDevice *dev); 39138d87493SPeter Xu /* Notify arch about released MSI routes */ 39238d87493SPeter Xu int kvm_arch_release_virq_post(int virq); 39338d87493SPeter Xu 3941850b6b7SEric Auger int kvm_arch_msi_data_to_gsi(uint32_t data); 3951850b6b7SEric Auger 3969c17d615SPaolo Bonzini int kvm_set_irq(KVMState *s, int irq, int level); 3979c17d615SPaolo Bonzini int kvm_irqchip_send_msi(KVMState *s, MSIMessage msg); 3989c17d615SPaolo Bonzini 3999c17d615SPaolo Bonzini void kvm_irqchip_add_irq_route(KVMState *s, int gsi, int irqchip, int pin); 4009c17d615SPaolo Bonzini 4013607715aSDavid Gibson void kvm_irqchip_add_change_notifier(Notifier *n); 4023607715aSDavid Gibson void kvm_irqchip_remove_change_notifier(Notifier *n); 4033607715aSDavid Gibson void kvm_irqchip_change_notify(void); 4043607715aSDavid Gibson 4059c17d615SPaolo Bonzini void kvm_get_apic_state(DeviceState *d, struct kvm_lapic_state *kapic); 4069c17d615SPaolo Bonzini 4079c17d615SPaolo Bonzini struct kvm_guest_debug; 4089c17d615SPaolo Bonzini struct kvm_debug_exit_arch; 4099c17d615SPaolo Bonzini 4109c17d615SPaolo Bonzini struct kvm_sw_breakpoint { 4119c17d615SPaolo Bonzini target_ulong pc; 4129c17d615SPaolo Bonzini target_ulong saved_insn; 4139c17d615SPaolo Bonzini int use_count; 4149c17d615SPaolo Bonzini QTAILQ_ENTRY(kvm_sw_breakpoint) entry; 4159c17d615SPaolo Bonzini }; 4169c17d615SPaolo Bonzini 417501a7ce7SAndreas Färber struct kvm_sw_breakpoint *kvm_find_sw_breakpoint(CPUState *cpu, 4189c17d615SPaolo Bonzini target_ulong pc); 4199c17d615SPaolo Bonzini 420501a7ce7SAndreas Färber int kvm_sw_breakpoints_active(CPUState *cpu); 4219c17d615SPaolo Bonzini 42280b7cd73SAndreas Färber int kvm_arch_insert_sw_breakpoint(CPUState *cpu, 4239c17d615SPaolo Bonzini struct kvm_sw_breakpoint *bp); 42480b7cd73SAndreas Färber int kvm_arch_remove_sw_breakpoint(CPUState *cpu, 4259c17d615SPaolo Bonzini struct kvm_sw_breakpoint *bp); 4269c17d615SPaolo Bonzini int kvm_arch_insert_hw_breakpoint(target_ulong addr, 4279c17d615SPaolo Bonzini target_ulong len, int type); 4289c17d615SPaolo Bonzini int kvm_arch_remove_hw_breakpoint(target_ulong addr, 4299c17d615SPaolo Bonzini target_ulong len, int type); 4309c17d615SPaolo Bonzini void kvm_arch_remove_all_hw_breakpoints(void); 4319c17d615SPaolo Bonzini 432501a7ce7SAndreas Färber void kvm_arch_update_guest_debug(CPUState *cpu, struct kvm_guest_debug *dbg); 4339c17d615SPaolo Bonzini 434501a7ce7SAndreas Färber bool kvm_arch_stop_on_emulation_error(CPUState *cpu); 4359c17d615SPaolo Bonzini 4369c17d615SPaolo Bonzini int kvm_check_extension(KVMState *s, unsigned int extension); 4379c17d615SPaolo Bonzini 4387d0a07faSAlexander Graf int kvm_vm_check_extension(KVMState *s, unsigned int extension); 4397d0a07faSAlexander Graf 44040f1ee27SCornelia Huck #define kvm_vm_enable_cap(s, capability, cap_flags, ...) \ 44140f1ee27SCornelia Huck ({ \ 44240f1ee27SCornelia Huck struct kvm_enable_cap cap = { \ 44340f1ee27SCornelia Huck .cap = capability, \ 44440f1ee27SCornelia Huck .flags = cap_flags, \ 44540f1ee27SCornelia Huck }; \ 44640f1ee27SCornelia Huck uint64_t args_tmp[] = { __VA_ARGS__ }; \ 4471b7ac7caSGreg Kurz size_t n = MIN(ARRAY_SIZE(args_tmp), ARRAY_SIZE(cap.args)); \ 4481b7ac7caSGreg Kurz memcpy(cap.args, args_tmp, n * sizeof(cap.args[0])); \ 44940f1ee27SCornelia Huck kvm_vm_ioctl(s, KVM_ENABLE_CAP, &cap); \ 45040f1ee27SCornelia Huck }) 45140f1ee27SCornelia Huck 45240f1ee27SCornelia Huck #define kvm_vcpu_enable_cap(cpu, capability, cap_flags, ...) \ 45340f1ee27SCornelia Huck ({ \ 45440f1ee27SCornelia Huck struct kvm_enable_cap cap = { \ 45540f1ee27SCornelia Huck .cap = capability, \ 45640f1ee27SCornelia Huck .flags = cap_flags, \ 45740f1ee27SCornelia Huck }; \ 45840f1ee27SCornelia Huck uint64_t args_tmp[] = { __VA_ARGS__ }; \ 4591b7ac7caSGreg Kurz size_t n = MIN(ARRAY_SIZE(args_tmp), ARRAY_SIZE(cap.args)); \ 4601b7ac7caSGreg Kurz memcpy(cap.args, args_tmp, n * sizeof(cap.args[0])); \ 46140f1ee27SCornelia Huck kvm_vcpu_ioctl(cpu, KVM_ENABLE_CAP, &cap); \ 46240f1ee27SCornelia Huck }) 46340f1ee27SCornelia Huck 4649c17d615SPaolo Bonzini uint32_t kvm_arch_get_supported_cpuid(KVMState *env, uint32_t function, 4659c17d615SPaolo Bonzini uint32_t index, int reg); 466ede146c2SPaolo Bonzini uint64_t kvm_arch_get_supported_msr_feature(KVMState *s, uint32_t index); 467f57bceb6SRobert Hoo 46897577fd4SJames Hogan 469aed6efb9SJames Hogan void kvm_set_sigmask_len(KVMState *s, unsigned int sigmask_len); 470aed6efb9SJames Hogan 47197577fd4SJames Hogan #if !defined(CONFIG_USER_ONLY) 47297577fd4SJames Hogan int kvm_physical_memory_addr_from_host(KVMState *s, void *ram_addr, 47397577fd4SJames Hogan hwaddr *phys_addr); 47497577fd4SJames Hogan #endif 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 484d1f6af6aSPeter Xu * @s: KVM state 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 */ 493d1f6af6aSPeter Xu int kvm_irqchip_add_msi_route(KVMState *s, 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); 4979c17d615SPaolo Bonzini void kvm_irqchip_release_virq(KVMState *s, int virq); 4989c17d615SPaolo Bonzini 499d426d9fbSCornelia Huck int kvm_irqchip_add_adapter_route(KVMState *s, AdapterInfo *adapter); 500977a8d9cSAndrey Smetanin int kvm_irqchip_add_hv_sint_route(KVMState *s, uint32_t vcpu, uint32_t sint); 501d426d9fbSCornelia Huck 5021c9b71a7SEric Auger int kvm_irqchip_add_irqfd_notifier_gsi(KVMState *s, EventNotifier *n, 503ca916d37SVincenzo Maffione EventNotifier *rn, int virq); 5041c9b71a7SEric Auger int kvm_irqchip_remove_irqfd_notifier_gsi(KVMState *s, EventNotifier *n, 5051c9b71a7SEric Auger int virq); 506197e3524SEric Auger int kvm_irqchip_add_irqfd_notifier(KVMState *s, EventNotifier *n, 507197e3524SEric Auger EventNotifier *rn, qemu_irq irq); 508197e3524SEric Auger int kvm_irqchip_remove_irqfd_notifier(KVMState *s, EventNotifier *n, 509197e3524SEric Auger qemu_irq irq); 510197e3524SEric Auger void kvm_irqchip_set_qemuirq_gsi(KVMState *s, qemu_irq irq, int gsi); 5119c17d615SPaolo Bonzini void kvm_pc_setup_irq_routing(bool pci_enabled); 5127b774593SAlexander Graf void kvm_init_irq_routing(KVMState *s); 513d6032e06SChristoffer Dall 5144376c40dSPaolo Bonzini bool kvm_kernel_irqchip_allowed(void); 5154376c40dSPaolo Bonzini bool kvm_kernel_irqchip_required(void); 5164376c40dSPaolo Bonzini bool kvm_kernel_irqchip_split(void); 5174376c40dSPaolo Bonzini 518d6032e06SChristoffer Dall /** 519d6032e06SChristoffer Dall * kvm_arch_irqchip_create: 520d6032e06SChristoffer Dall * @KVMState: The KVMState pointer 521d6032e06SChristoffer Dall * 522d6032e06SChristoffer Dall * Allow architectures to create an in-kernel irq chip themselves. 523d6032e06SChristoffer Dall * 524d6032e06SChristoffer Dall * Returns: < 0: error 525d6032e06SChristoffer Dall * 0: irq chip was not created 526d6032e06SChristoffer Dall * > 0: irq chip was created 527d6032e06SChristoffer Dall */ 5284376c40dSPaolo Bonzini int kvm_arch_irqchip_create(KVMState *s); 529ada4135fSCornelia Huck 530ada4135fSCornelia Huck /** 531ada4135fSCornelia Huck * kvm_set_one_reg - set a register value in KVM via KVM_SET_ONE_REG ioctl 532ada4135fSCornelia Huck * @id: The register ID 533ada4135fSCornelia Huck * @source: The pointer to the value to be set. It must point to a variable 534ada4135fSCornelia Huck * of the correct type/size for the register being accessed. 535ada4135fSCornelia Huck * 536ada4135fSCornelia Huck * Returns: 0 on success, or a negative errno on failure. 537ada4135fSCornelia Huck */ 538ada4135fSCornelia Huck int kvm_set_one_reg(CPUState *cs, uint64_t id, void *source); 539ada4135fSCornelia Huck 540ada4135fSCornelia Huck /** 541ada4135fSCornelia Huck * kvm_get_one_reg - get a register value from KVM via KVM_GET_ONE_REG ioctl 542ada4135fSCornelia Huck * @id: The register ID 543ada4135fSCornelia Huck * @target: The pointer where the value is to be stored. It must point to a 544ada4135fSCornelia Huck * variable of the correct type/size for the register being accessed. 545ada4135fSCornelia Huck * 546ada4135fSCornelia Huck * Returns: 0 on success, or a negative errno on failure. 547ada4135fSCornelia Huck */ 548ada4135fSCornelia Huck int kvm_get_one_reg(CPUState *cs, uint64_t id, void *target); 549c64abd1fSSam Bobroff struct ppc_radix_page_info *kvm_get_radix_page_info(void); 55044f2e6c1SBharata B Rao int kvm_get_max_memslots(void); 551c82d9d43SPeter Xu 552c82d9d43SPeter Xu /* Notify resamplefd for EOI of specific interrupts. */ 553c82d9d43SPeter Xu void kvm_resample_fd_notify(int gsi); 554c82d9d43SPeter Xu 55592a5199bSTom Lendacky /** 55692a5199bSTom Lendacky * kvm_cpu_check_are_resettable - return whether CPUs can be reset 55792a5199bSTom Lendacky * 55892a5199bSTom Lendacky * Returns: true: CPUs are resettable 55992a5199bSTom Lendacky * false: CPUs are not resettable 56092a5199bSTom Lendacky */ 56192a5199bSTom Lendacky bool kvm_cpu_check_are_resettable(void); 56292a5199bSTom Lendacky 56392a5199bSTom Lendacky bool kvm_arch_cpu_check_are_resettable(void); 56492a5199bSTom Lendacky 5657786ae40SHyman Huang(黄勇) bool kvm_dirty_ring_enabled(void); 5669c17d615SPaolo Bonzini #endif 567