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" 18504134d2SAndreas Färber #include "qom/cpu.h" 194c663752SPaolo Bonzini #include "exec/memattrs.h" 20197e3524SEric Auger #include "hw/irq.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; 4432c18a2dSMatt Gingell extern bool kvm_split_irqchip; 459c17d615SPaolo Bonzini extern bool kvm_async_interrupts_allowed; 46215e79c0SAlexander Graf extern bool kvm_halt_in_kernel_allowed; 4769e03ae6SNikolay Nikolaev extern bool kvm_eventfds_allowed; 489c17d615SPaolo Bonzini extern bool kvm_irqfds_allowed; 49f41389aeSEric Auger extern bool kvm_resamplefds_allowed; 509c17d615SPaolo Bonzini extern bool kvm_msi_via_irqfd_allowed; 519c17d615SPaolo Bonzini extern bool kvm_gsi_routing_allowed; 5276fe21deSAlexey Kardashevskiy extern bool kvm_gsi_direct_mapping; 53df9c8b75SJordan Justen extern bool kvm_readonly_mem_allowed; 5450bf31b9SPavel Fedin extern bool kvm_direct_msi_allowed; 5535108223SJason Wang extern bool kvm_ioeventfd_any_length_allowed; 56*767a554aSPavel Fedin extern bool kvm_msi_use_devid; 579c17d615SPaolo Bonzini 589c17d615SPaolo Bonzini #if defined CONFIG_KVM || !defined NEED_CPU_H 599c17d615SPaolo Bonzini #define kvm_enabled() (kvm_allowed) 609c17d615SPaolo Bonzini /** 619c17d615SPaolo Bonzini * kvm_irqchip_in_kernel: 629c17d615SPaolo Bonzini * 639c17d615SPaolo Bonzini * Returns: true if the user asked us to create an in-kernel 649c17d615SPaolo Bonzini * irqchip via the "kernel_irqchip=on" machine option. 659c17d615SPaolo Bonzini * What this actually means is architecture and machine model 669c17d615SPaolo Bonzini * specific: on PC, for instance, it means that the LAPIC, 679c17d615SPaolo Bonzini * IOAPIC and PIT are all in kernel. This function should never 689c17d615SPaolo Bonzini * be used from generic target-independent code: use one of the 699c17d615SPaolo Bonzini * following functions or some other specific check instead. 709c17d615SPaolo Bonzini */ 719c17d615SPaolo Bonzini #define kvm_irqchip_in_kernel() (kvm_kernel_irqchip) 729c17d615SPaolo Bonzini 739c17d615SPaolo Bonzini /** 7432c18a2dSMatt Gingell * kvm_irqchip_is_split: 7532c18a2dSMatt Gingell * 7632c18a2dSMatt Gingell * Returns: true if the user asked us to split the irqchip 7732c18a2dSMatt Gingell * implementation between user and kernel space. The details are 7832c18a2dSMatt Gingell * architecture and machine specific. On PC, it means that the PIC, 7932c18a2dSMatt Gingell * IOAPIC, and PIT are in user space while the LAPIC is in the kernel. 8032c18a2dSMatt Gingell */ 8132c18a2dSMatt Gingell #define kvm_irqchip_is_split() (kvm_split_irqchip) 8232c18a2dSMatt Gingell 8332c18a2dSMatt Gingell /** 849c17d615SPaolo Bonzini * kvm_async_interrupts_enabled: 859c17d615SPaolo Bonzini * 869c17d615SPaolo Bonzini * Returns: true if we can deliver interrupts to KVM 879c17d615SPaolo Bonzini * asynchronously (ie by ioctl from any thread at any time) 889c17d615SPaolo Bonzini * rather than having to do interrupt delivery synchronously 899c17d615SPaolo Bonzini * (where the vcpu must be stopped at a suitable point first). 909c17d615SPaolo Bonzini */ 919c17d615SPaolo Bonzini #define kvm_async_interrupts_enabled() (kvm_async_interrupts_allowed) 929c17d615SPaolo Bonzini 939c17d615SPaolo Bonzini /** 94215e79c0SAlexander Graf * kvm_halt_in_kernel 95215e79c0SAlexander Graf * 96215e79c0SAlexander Graf * Returns: true if halted cpus should still get a KVM_RUN ioctl to run 97215e79c0SAlexander Graf * inside of kernel space. This only works if MP state is implemented. 98215e79c0SAlexander Graf */ 99215e79c0SAlexander Graf #define kvm_halt_in_kernel() (kvm_halt_in_kernel_allowed) 100215e79c0SAlexander Graf 101215e79c0SAlexander Graf /** 10269e03ae6SNikolay Nikolaev * kvm_eventfds_enabled: 10369e03ae6SNikolay Nikolaev * 10469e03ae6SNikolay Nikolaev * Returns: true if we can use eventfds to receive notifications 10569e03ae6SNikolay Nikolaev * from a KVM CPU (ie the kernel supports eventds and we are running 10669e03ae6SNikolay Nikolaev * with a configuration where it is meaningful to use them). 10769e03ae6SNikolay Nikolaev */ 10869e03ae6SNikolay Nikolaev #define kvm_eventfds_enabled() (kvm_eventfds_allowed) 10969e03ae6SNikolay Nikolaev 11069e03ae6SNikolay Nikolaev /** 1119c17d615SPaolo Bonzini * kvm_irqfds_enabled: 1129c17d615SPaolo Bonzini * 1139c17d615SPaolo Bonzini * Returns: true if we can use irqfds to inject interrupts into 1149c17d615SPaolo Bonzini * a KVM CPU (ie the kernel supports irqfds and we are running 1159c17d615SPaolo Bonzini * with a configuration where it is meaningful to use them). 1169c17d615SPaolo Bonzini */ 1179c17d615SPaolo Bonzini #define kvm_irqfds_enabled() (kvm_irqfds_allowed) 1189c17d615SPaolo Bonzini 1199c17d615SPaolo Bonzini /** 120f41389aeSEric Auger * kvm_resamplefds_enabled: 121f41389aeSEric Auger * 122f41389aeSEric Auger * Returns: true if we can use resamplefds to inject interrupts into 123f41389aeSEric Auger * a KVM CPU (ie the kernel supports resamplefds and we are running 124f41389aeSEric Auger * with a configuration where it is meaningful to use them). 125f41389aeSEric Auger */ 126f41389aeSEric Auger #define kvm_resamplefds_enabled() (kvm_resamplefds_allowed) 127f41389aeSEric Auger 128f41389aeSEric Auger /** 1299c17d615SPaolo Bonzini * kvm_msi_via_irqfd_enabled: 1309c17d615SPaolo Bonzini * 1319c17d615SPaolo Bonzini * Returns: true if we can route a PCI MSI (Message Signaled Interrupt) 1329c17d615SPaolo Bonzini * to a KVM CPU via an irqfd. This requires that the kernel supports 1339c17d615SPaolo Bonzini * this and that we're running in a configuration that permits it. 1349c17d615SPaolo Bonzini */ 1359c17d615SPaolo Bonzini #define kvm_msi_via_irqfd_enabled() (kvm_msi_via_irqfd_allowed) 1369c17d615SPaolo Bonzini 1379c17d615SPaolo Bonzini /** 1389c17d615SPaolo Bonzini * kvm_gsi_routing_enabled: 1399c17d615SPaolo Bonzini * 1409c17d615SPaolo Bonzini * Returns: true if GSI routing is enabled (ie the kernel supports 1419c17d615SPaolo Bonzini * it and we're running in a configuration that permits it). 1429c17d615SPaolo Bonzini */ 1439c17d615SPaolo Bonzini #define kvm_gsi_routing_enabled() (kvm_gsi_routing_allowed) 1449c17d615SPaolo Bonzini 145df9c8b75SJordan Justen /** 14676fe21deSAlexey Kardashevskiy * kvm_gsi_direct_mapping: 14776fe21deSAlexey Kardashevskiy * 14876fe21deSAlexey Kardashevskiy * Returns: true if GSI direct mapping is enabled. 14976fe21deSAlexey Kardashevskiy */ 15076fe21deSAlexey Kardashevskiy #define kvm_gsi_direct_mapping() (kvm_gsi_direct_mapping) 15176fe21deSAlexey Kardashevskiy 15276fe21deSAlexey Kardashevskiy /** 153df9c8b75SJordan Justen * kvm_readonly_mem_enabled: 154df9c8b75SJordan Justen * 155df9c8b75SJordan Justen * Returns: true if KVM readonly memory is enabled (ie the kernel 156df9c8b75SJordan Justen * supports it and we're running in a configuration that permits it). 157df9c8b75SJordan Justen */ 158df9c8b75SJordan Justen #define kvm_readonly_mem_enabled() (kvm_readonly_mem_allowed) 159df9c8b75SJordan Justen 16050bf31b9SPavel Fedin /** 16150bf31b9SPavel Fedin * kvm_direct_msi_enabled: 16250bf31b9SPavel Fedin * 16350bf31b9SPavel Fedin * Returns: true if KVM allows direct MSI injection. 16450bf31b9SPavel Fedin */ 16550bf31b9SPavel Fedin #define kvm_direct_msi_enabled() (kvm_direct_msi_allowed) 16650bf31b9SPavel Fedin 16735108223SJason Wang /** 16835108223SJason Wang * kvm_ioeventfd_any_length_enabled: 16935108223SJason Wang * Returns: true if KVM allows any length io eventfd. 17035108223SJason Wang */ 17135108223SJason Wang #define kvm_ioeventfd_any_length_enabled() (kvm_ioeventfd_any_length_allowed) 17235108223SJason Wang 173*767a554aSPavel Fedin /** 174*767a554aSPavel Fedin * kvm_msi_devid_required: 175*767a554aSPavel Fedin * Returns: true if KVM requires a device id to be provided while 176*767a554aSPavel Fedin * defining an MSI routing entry. 177*767a554aSPavel Fedin */ 178*767a554aSPavel Fedin #define kvm_msi_devid_required() (kvm_msi_use_devid) 179*767a554aSPavel Fedin 1809c17d615SPaolo Bonzini #else 1819c17d615SPaolo Bonzini #define kvm_enabled() (0) 1829c17d615SPaolo Bonzini #define kvm_irqchip_in_kernel() (false) 18315eafc2eSPaolo Bonzini #define kvm_irqchip_is_split() (false) 1849c17d615SPaolo Bonzini #define kvm_async_interrupts_enabled() (false) 185215e79c0SAlexander Graf #define kvm_halt_in_kernel() (false) 18669e03ae6SNikolay Nikolaev #define kvm_eventfds_enabled() (false) 1879c17d615SPaolo Bonzini #define kvm_irqfds_enabled() (false) 188879904e8SEric Auger #define kvm_resamplefds_enabled() (false) 1899c17d615SPaolo Bonzini #define kvm_msi_via_irqfd_enabled() (false) 1909c17d615SPaolo Bonzini #define kvm_gsi_routing_allowed() (false) 19176fe21deSAlexey Kardashevskiy #define kvm_gsi_direct_mapping() (false) 192df9c8b75SJordan Justen #define kvm_readonly_mem_enabled() (false) 19350bf31b9SPavel Fedin #define kvm_direct_msi_enabled() (false) 19435108223SJason Wang #define kvm_ioeventfd_any_length_enabled() (false) 195*767a554aSPavel Fedin #define kvm_msi_devid_required() (false) 1969c17d615SPaolo Bonzini #endif 1979c17d615SPaolo Bonzini 1989c17d615SPaolo Bonzini struct kvm_run; 1999c17d615SPaolo Bonzini struct kvm_lapic_state; 2009e03a040SFrank Blaschka struct kvm_irq_routing_entry; 2019c17d615SPaolo Bonzini 2029c17d615SPaolo Bonzini typedef struct KVMCapabilityInfo { 2039c17d615SPaolo Bonzini const char *name; 2049c17d615SPaolo Bonzini int value; 2059c17d615SPaolo Bonzini } KVMCapabilityInfo; 2069c17d615SPaolo Bonzini 2079c17d615SPaolo Bonzini #define KVM_CAP_INFO(CAP) { "KVM_CAP_" stringify(CAP), KVM_CAP_##CAP } 2089c17d615SPaolo Bonzini #define KVM_CAP_LAST_INFO { NULL, 0 } 2099c17d615SPaolo Bonzini 2109c17d615SPaolo Bonzini struct KVMState; 2119c17d615SPaolo Bonzini typedef struct KVMState KVMState; 2129c17d615SPaolo Bonzini extern KVMState *kvm_state; 2139c17d615SPaolo Bonzini 2149c17d615SPaolo Bonzini /* external API */ 2159c17d615SPaolo Bonzini 216b8865591SIgor Mammedov bool kvm_has_free_slot(MachineState *ms); 2179c17d615SPaolo Bonzini int kvm_has_sync_mmu(void); 2189c17d615SPaolo Bonzini int kvm_has_vcpu_events(void); 2199c17d615SPaolo Bonzini int kvm_has_robust_singlestep(void); 2209c17d615SPaolo Bonzini int kvm_has_debugregs(void); 2219c17d615SPaolo Bonzini int kvm_has_pit_state2(void); 2229c17d615SPaolo Bonzini int kvm_has_many_ioeventfds(void); 2239c17d615SPaolo Bonzini int kvm_has_gsi_routing(void); 2249c17d615SPaolo Bonzini int kvm_has_intx_set_mask(void); 2259c17d615SPaolo Bonzini 226504134d2SAndreas Färber int kvm_init_vcpu(CPUState *cpu); 2271458c363SAndreas Färber int kvm_cpu_exec(CPUState *cpu); 2284c055ab5SGu Zheng int kvm_destroy_vcpu(CPUState *cpu); 2299c17d615SPaolo Bonzini 230504134d2SAndreas Färber #ifdef NEED_CPU_H 23133c11879SPaolo Bonzini #include "cpu.h" 2329c17d615SPaolo Bonzini 233c4cfef5eSIgor Mammedov void kvm_flush_coalesced_mmio_buffer(void); 234c4cfef5eSIgor Mammedov 23562278814SAndreas Färber int kvm_insert_breakpoint(CPUState *cpu, target_ulong addr, 2369c17d615SPaolo Bonzini target_ulong len, int type); 23762278814SAndreas Färber int kvm_remove_breakpoint(CPUState *cpu, target_ulong addr, 2389c17d615SPaolo Bonzini target_ulong len, int type); 2391d5791f4SAndreas Färber void kvm_remove_all_breakpoints(CPUState *cpu); 24038e478ecSStefan Weil int kvm_update_guest_debug(CPUState *cpu, unsigned long reinject_trap); 2419c17d615SPaolo Bonzini #ifndef _WIN32 242491d6e80SAndreas Färber int kvm_set_signal_mask(CPUState *cpu, const sigset_t *sigset); 2439c17d615SPaolo Bonzini #endif 2449c17d615SPaolo Bonzini 245290adf38SAndreas Färber int kvm_on_sigbus_vcpu(CPUState *cpu, int code, void *addr); 2469c17d615SPaolo Bonzini int kvm_on_sigbus(int code, void *addr); 2479c17d615SPaolo Bonzini 24888401cbcSPaolo Bonzini /* interface with exec.c */ 24988401cbcSPaolo Bonzini 25088401cbcSPaolo Bonzini void phys_mem_set_alloc(void *(*alloc)(size_t, uint64_t *align)); 25188401cbcSPaolo Bonzini 2529c17d615SPaolo Bonzini /* internal API */ 2539c17d615SPaolo Bonzini 2549c17d615SPaolo Bonzini int kvm_ioctl(KVMState *s, int type, ...); 2559c17d615SPaolo Bonzini 2569c17d615SPaolo Bonzini int kvm_vm_ioctl(KVMState *s, int type, ...); 2579c17d615SPaolo Bonzini 258501a7ce7SAndreas Färber int kvm_vcpu_ioctl(CPUState *cpu, int type, ...); 2599c17d615SPaolo Bonzini 2600a6a7ccaSChristoffer Dall /** 2610a6a7ccaSChristoffer Dall * kvm_device_ioctl - call an ioctl on a kvm device 2620a6a7ccaSChristoffer Dall * @fd: The KVM device file descriptor as returned from KVM_CREATE_DEVICE 2630a6a7ccaSChristoffer Dall * @type: The device-ctrl ioctl number 2640a6a7ccaSChristoffer Dall * 2650a6a7ccaSChristoffer Dall * Returns: -errno on error, nonnegative on success 2660a6a7ccaSChristoffer Dall */ 2670a6a7ccaSChristoffer Dall int kvm_device_ioctl(int fd, int type, ...); 2680a6a7ccaSChristoffer Dall 2690a6a7ccaSChristoffer Dall /** 270d0a073a1SDominik Dingel * kvm_vm_check_attr - check for existence of a specific vm attribute 271d0a073a1SDominik Dingel * @s: The KVMState pointer 272d0a073a1SDominik Dingel * @group: the group 273d0a073a1SDominik Dingel * @attr: the attribute of that group to query for 274d0a073a1SDominik Dingel * 275d0a073a1SDominik Dingel * Returns: 1 if the attribute exists 276d0a073a1SDominik Dingel * 0 if the attribute either does not exist or if the vm device 277d0a073a1SDominik Dingel * interface is unavailable 278d0a073a1SDominik Dingel */ 279d0a073a1SDominik Dingel int kvm_vm_check_attr(KVMState *s, uint32_t group, uint64_t attr); 280d0a073a1SDominik Dingel 281d0a073a1SDominik Dingel /** 2824b3cfe72SPavel Fedin * kvm_device_check_attr - check for existence of a specific device attribute 2834b3cfe72SPavel Fedin * @fd: The device file descriptor 2844b3cfe72SPavel Fedin * @group: the group 2854b3cfe72SPavel Fedin * @attr: the attribute of that group to query for 2864b3cfe72SPavel Fedin * 2874b3cfe72SPavel Fedin * Returns: 1 if the attribute exists 2884b3cfe72SPavel Fedin * 0 if the attribute either does not exist or if the vm device 2894b3cfe72SPavel Fedin * interface is unavailable 2904b3cfe72SPavel Fedin */ 2914b3cfe72SPavel Fedin int kvm_device_check_attr(int fd, uint32_t group, uint64_t attr); 2924b3cfe72SPavel Fedin 2934b3cfe72SPavel Fedin /** 2944b3cfe72SPavel Fedin * kvm_device_access - set or get value of a specific vm attribute 2954b3cfe72SPavel Fedin * @fd: The device file descriptor 2964b3cfe72SPavel Fedin * @group: the group 2974b3cfe72SPavel Fedin * @attr: the attribute of that group to set or get 2984b3cfe72SPavel Fedin * @val: pointer to a storage area for the value 2994b3cfe72SPavel Fedin * @write: true for set and false for get operation 3004b3cfe72SPavel Fedin * 3014b3cfe72SPavel Fedin * This function is not allowed to fail. Use kvm_device_check_attr() 3024b3cfe72SPavel Fedin * in order to check for the availability of optional attributes. 3034b3cfe72SPavel Fedin */ 3044b3cfe72SPavel Fedin void kvm_device_access(int fd, int group, uint64_t attr, 3054b3cfe72SPavel Fedin void *val, bool write); 3064b3cfe72SPavel Fedin 3074b3cfe72SPavel Fedin /** 3080a6a7ccaSChristoffer Dall * kvm_create_device - create a KVM device for the device control API 3090a6a7ccaSChristoffer Dall * @KVMState: The KVMState pointer 3100a6a7ccaSChristoffer Dall * @type: The KVM device type (see Documentation/virtual/kvm/devices in the 3110a6a7ccaSChristoffer Dall * kernel source) 3120a6a7ccaSChristoffer Dall * @test: If true, only test if device can be created, but don't actually 3130a6a7ccaSChristoffer Dall * create the device. 3140a6a7ccaSChristoffer Dall * 3150a6a7ccaSChristoffer Dall * Returns: -errno on error, nonnegative on success: @test ? 0 : device fd; 3160a6a7ccaSChristoffer Dall */ 3170a6a7ccaSChristoffer Dall int kvm_create_device(KVMState *s, uint64_t type, bool test); 3180a6a7ccaSChristoffer Dall 31929039acfSPeter Xu /** 32029039acfSPeter Xu * kvm_device_supported - probe whether KVM supports specific device 32129039acfSPeter Xu * 32229039acfSPeter Xu * @vmfd: The fd handler for VM 32329039acfSPeter Xu * @type: type of device 32429039acfSPeter Xu * 32529039acfSPeter Xu * @return: true if supported, otherwise false. 32629039acfSPeter Xu */ 32729039acfSPeter Xu bool kvm_device_supported(int vmfd, uint64_t type); 3280a6a7ccaSChristoffer Dall 3299c17d615SPaolo Bonzini /* Arch specific hooks */ 3309c17d615SPaolo Bonzini 3319c17d615SPaolo Bonzini extern const KVMCapabilityInfo kvm_arch_required_capabilities[]; 3329c17d615SPaolo Bonzini 333501a7ce7SAndreas Färber void kvm_arch_pre_run(CPUState *cpu, struct kvm_run *run); 3344c663752SPaolo Bonzini MemTxAttrs kvm_arch_post_run(CPUState *cpu, struct kvm_run *run); 3359c17d615SPaolo Bonzini 336501a7ce7SAndreas Färber int kvm_arch_handle_exit(CPUState *cpu, struct kvm_run *run); 3379c17d615SPaolo Bonzini 338501a7ce7SAndreas Färber int kvm_arch_process_async_events(CPUState *cpu); 3399c17d615SPaolo Bonzini 340501a7ce7SAndreas Färber int kvm_arch_get_registers(CPUState *cpu); 3419c17d615SPaolo Bonzini 3429c17d615SPaolo Bonzini /* state subset only touched by the VCPU itself during runtime */ 3439c17d615SPaolo Bonzini #define KVM_PUT_RUNTIME_STATE 1 3449c17d615SPaolo Bonzini /* state subset modified during VCPU reset */ 3459c17d615SPaolo Bonzini #define KVM_PUT_RESET_STATE 2 3469c17d615SPaolo Bonzini /* full state set, modified during initialization or on vmload */ 3479c17d615SPaolo Bonzini #define KVM_PUT_FULL_STATE 3 3489c17d615SPaolo Bonzini 349501a7ce7SAndreas Färber int kvm_arch_put_registers(CPUState *cpu, int level); 3509c17d615SPaolo Bonzini 351b16565b3SMarcel Apfelbaum int kvm_arch_init(MachineState *ms, KVMState *s); 3529c17d615SPaolo Bonzini 353501a7ce7SAndreas Färber int kvm_arch_init_vcpu(CPUState *cpu); 3549c17d615SPaolo Bonzini 35541264b38SGreg Kurz bool kvm_vcpu_id_is_valid(int vcpu_id); 35641264b38SGreg Kurz 357b164e48eSEduardo Habkost /* Returns VCPU ID to be used on KVM_CREATE_VCPU ioctl() */ 358b164e48eSEduardo Habkost unsigned long kvm_arch_vcpu_id(CPUState *cpu); 359b164e48eSEduardo Habkost 360501a7ce7SAndreas Färber int kvm_arch_on_sigbus_vcpu(CPUState *cpu, int code, void *addr); 3619c17d615SPaolo Bonzini int kvm_arch_on_sigbus(int code, void *addr); 3629c17d615SPaolo Bonzini 3639c17d615SPaolo Bonzini void kvm_arch_init_irq_routing(KVMState *s); 3649c17d615SPaolo Bonzini 3659e03a040SFrank Blaschka int kvm_arch_fixup_msi_route(struct kvm_irq_routing_entry *route, 366dc9f06caSPavel Fedin uint64_t address, uint32_t data, PCIDevice *dev); 3679e03a040SFrank Blaschka 36838d87493SPeter Xu /* Notify arch about newly added MSI routes */ 36938d87493SPeter Xu int kvm_arch_add_msi_route_post(struct kvm_irq_routing_entry *route, 37038d87493SPeter Xu int vector, PCIDevice *dev); 37138d87493SPeter Xu /* Notify arch about released MSI routes */ 37238d87493SPeter Xu int kvm_arch_release_virq_post(int virq); 37338d87493SPeter Xu 3741850b6b7SEric Auger int kvm_arch_msi_data_to_gsi(uint32_t data); 3751850b6b7SEric Auger 3769c17d615SPaolo Bonzini int kvm_set_irq(KVMState *s, int irq, int level); 3779c17d615SPaolo Bonzini int kvm_irqchip_send_msi(KVMState *s, MSIMessage msg); 3789c17d615SPaolo Bonzini 3799c17d615SPaolo Bonzini void kvm_irqchip_add_irq_route(KVMState *s, int gsi, int irqchip, int pin); 3809c17d615SPaolo Bonzini 3819c17d615SPaolo Bonzini void kvm_get_apic_state(DeviceState *d, struct kvm_lapic_state *kapic); 3829c17d615SPaolo Bonzini 3839c17d615SPaolo Bonzini struct kvm_guest_debug; 3849c17d615SPaolo Bonzini struct kvm_debug_exit_arch; 3859c17d615SPaolo Bonzini 3869c17d615SPaolo Bonzini struct kvm_sw_breakpoint { 3879c17d615SPaolo Bonzini target_ulong pc; 3889c17d615SPaolo Bonzini target_ulong saved_insn; 3899c17d615SPaolo Bonzini int use_count; 3909c17d615SPaolo Bonzini QTAILQ_ENTRY(kvm_sw_breakpoint) entry; 3919c17d615SPaolo Bonzini }; 3929c17d615SPaolo Bonzini 3939c17d615SPaolo Bonzini QTAILQ_HEAD(kvm_sw_breakpoint_head, kvm_sw_breakpoint); 3949c17d615SPaolo Bonzini 395501a7ce7SAndreas Färber struct kvm_sw_breakpoint *kvm_find_sw_breakpoint(CPUState *cpu, 3969c17d615SPaolo Bonzini target_ulong pc); 3979c17d615SPaolo Bonzini 398501a7ce7SAndreas Färber int kvm_sw_breakpoints_active(CPUState *cpu); 3999c17d615SPaolo Bonzini 40080b7cd73SAndreas Färber int kvm_arch_insert_sw_breakpoint(CPUState *cpu, 4019c17d615SPaolo Bonzini struct kvm_sw_breakpoint *bp); 40280b7cd73SAndreas Färber int kvm_arch_remove_sw_breakpoint(CPUState *cpu, 4039c17d615SPaolo Bonzini struct kvm_sw_breakpoint *bp); 4049c17d615SPaolo Bonzini int kvm_arch_insert_hw_breakpoint(target_ulong addr, 4059c17d615SPaolo Bonzini target_ulong len, int type); 4069c17d615SPaolo Bonzini int kvm_arch_remove_hw_breakpoint(target_ulong addr, 4079c17d615SPaolo Bonzini target_ulong len, int type); 4089c17d615SPaolo Bonzini void kvm_arch_remove_all_hw_breakpoints(void); 4099c17d615SPaolo Bonzini 410501a7ce7SAndreas Färber void kvm_arch_update_guest_debug(CPUState *cpu, struct kvm_guest_debug *dbg); 4119c17d615SPaolo Bonzini 412501a7ce7SAndreas Färber bool kvm_arch_stop_on_emulation_error(CPUState *cpu); 4139c17d615SPaolo Bonzini 4149c17d615SPaolo Bonzini int kvm_check_extension(KVMState *s, unsigned int extension); 4159c17d615SPaolo Bonzini 4167d0a07faSAlexander Graf int kvm_vm_check_extension(KVMState *s, unsigned int extension); 4177d0a07faSAlexander Graf 41840f1ee27SCornelia Huck #define kvm_vm_enable_cap(s, capability, cap_flags, ...) \ 41940f1ee27SCornelia Huck ({ \ 42040f1ee27SCornelia Huck struct kvm_enable_cap cap = { \ 42140f1ee27SCornelia Huck .cap = capability, \ 42240f1ee27SCornelia Huck .flags = cap_flags, \ 42340f1ee27SCornelia Huck }; \ 42440f1ee27SCornelia Huck uint64_t args_tmp[] = { __VA_ARGS__ }; \ 42540f1ee27SCornelia Huck int i; \ 42661c7bbd2SAlexander Graf for (i = 0; i < (int)ARRAY_SIZE(args_tmp) && \ 42740f1ee27SCornelia Huck i < ARRAY_SIZE(cap.args); i++) { \ 42840f1ee27SCornelia Huck cap.args[i] = args_tmp[i]; \ 42940f1ee27SCornelia Huck } \ 43040f1ee27SCornelia Huck kvm_vm_ioctl(s, KVM_ENABLE_CAP, &cap); \ 43140f1ee27SCornelia Huck }) 43240f1ee27SCornelia Huck 43340f1ee27SCornelia Huck #define kvm_vcpu_enable_cap(cpu, capability, cap_flags, ...) \ 43440f1ee27SCornelia Huck ({ \ 43540f1ee27SCornelia Huck struct kvm_enable_cap cap = { \ 43640f1ee27SCornelia Huck .cap = capability, \ 43740f1ee27SCornelia Huck .flags = cap_flags, \ 43840f1ee27SCornelia Huck }; \ 43940f1ee27SCornelia Huck uint64_t args_tmp[] = { __VA_ARGS__ }; \ 44040f1ee27SCornelia Huck int i; \ 44161c7bbd2SAlexander Graf for (i = 0; i < (int)ARRAY_SIZE(args_tmp) && \ 44240f1ee27SCornelia Huck i < ARRAY_SIZE(cap.args); i++) { \ 44340f1ee27SCornelia Huck cap.args[i] = args_tmp[i]; \ 44440f1ee27SCornelia Huck } \ 44540f1ee27SCornelia Huck kvm_vcpu_ioctl(cpu, KVM_ENABLE_CAP, &cap); \ 44640f1ee27SCornelia Huck }) 44740f1ee27SCornelia Huck 4489c17d615SPaolo Bonzini uint32_t kvm_arch_get_supported_cpuid(KVMState *env, uint32_t function, 4499c17d615SPaolo Bonzini uint32_t index, int reg); 45097577fd4SJames Hogan 451aed6efb9SJames Hogan void kvm_set_sigmask_len(KVMState *s, unsigned int sigmask_len); 452aed6efb9SJames Hogan 45397577fd4SJames Hogan #if !defined(CONFIG_USER_ONLY) 45497577fd4SJames Hogan int kvm_physical_memory_addr_from_host(KVMState *s, void *ram_addr, 45597577fd4SJames Hogan hwaddr *phys_addr); 45697577fd4SJames Hogan #endif 45797577fd4SJames Hogan 45897577fd4SJames Hogan #endif /* NEED_CPU_H */ 45997577fd4SJames Hogan 460dd1750d7SAndreas Färber void kvm_cpu_synchronize_state(CPUState *cpu); 46197577fd4SJames Hogan void kvm_cpu_synchronize_post_reset(CPUState *cpu); 46297577fd4SJames Hogan void kvm_cpu_synchronize_post_init(CPUState *cpu); 4639c17d615SPaolo Bonzini 4649c17d615SPaolo Bonzini /* generic hooks - to be moved/refactored once there are more users */ 4659c17d615SPaolo Bonzini 466cb446ecaSAndreas Färber static inline void cpu_synchronize_state(CPUState *cpu) 4679c17d615SPaolo Bonzini { 4689c17d615SPaolo Bonzini if (kvm_enabled()) { 469cb446ecaSAndreas Färber kvm_cpu_synchronize_state(cpu); 4709c17d615SPaolo Bonzini } 4719c17d615SPaolo Bonzini } 4729c17d615SPaolo Bonzini 4733f24a58fSIgor Mammedov static inline void cpu_synchronize_post_reset(CPUState *cpu) 4749c17d615SPaolo Bonzini { 4759c17d615SPaolo Bonzini if (kvm_enabled()) { 4763f24a58fSIgor Mammedov kvm_cpu_synchronize_post_reset(cpu); 4779c17d615SPaolo Bonzini } 4789c17d615SPaolo Bonzini } 4799c17d615SPaolo Bonzini 4803f24a58fSIgor Mammedov static inline void cpu_synchronize_post_init(CPUState *cpu) 4819c17d615SPaolo Bonzini { 4829c17d615SPaolo Bonzini if (kvm_enabled()) { 4833f24a58fSIgor Mammedov kvm_cpu_synchronize_post_init(cpu); 4849c17d615SPaolo Bonzini } 4859c17d615SPaolo Bonzini } 4869c17d615SPaolo Bonzini 487d1f6af6aSPeter Xu /** 488d1f6af6aSPeter Xu * kvm_irqchip_add_msi_route - Add MSI route for specific vector 489d1f6af6aSPeter Xu * @s: KVM state 490d1f6af6aSPeter Xu * @vector: which vector to add. This can be either MSI/MSIX 491d1f6af6aSPeter Xu * vector. The function will automatically detect whether 492d1f6af6aSPeter Xu * MSI/MSIX is enabled, and fetch corresponding MSI 493d1f6af6aSPeter Xu * message. 494d1f6af6aSPeter Xu * @dev: Owner PCI device to add the route. If @dev is specified 495d1f6af6aSPeter Xu * as @NULL, an empty MSI message will be inited. 496d1f6af6aSPeter Xu * @return: virq (>=0) when success, errno (<0) when failed. 497d1f6af6aSPeter Xu */ 498d1f6af6aSPeter Xu int kvm_irqchip_add_msi_route(KVMState *s, int vector, PCIDevice *dev); 499dc9f06caSPavel Fedin int kvm_irqchip_update_msi_route(KVMState *s, int virq, MSIMessage msg, 500dc9f06caSPavel Fedin PCIDevice *dev); 5013f1fea0fSPeter Xu void kvm_irqchip_commit_routes(KVMState *s); 5029c17d615SPaolo Bonzini void kvm_irqchip_release_virq(KVMState *s, int virq); 5039c17d615SPaolo Bonzini 504d426d9fbSCornelia Huck int kvm_irqchip_add_adapter_route(KVMState *s, AdapterInfo *adapter); 505977a8d9cSAndrey Smetanin int kvm_irqchip_add_hv_sint_route(KVMState *s, uint32_t vcpu, uint32_t sint); 506d426d9fbSCornelia Huck 5071c9b71a7SEric Auger int kvm_irqchip_add_irqfd_notifier_gsi(KVMState *s, EventNotifier *n, 508ca916d37SVincenzo Maffione EventNotifier *rn, int virq); 5091c9b71a7SEric Auger int kvm_irqchip_remove_irqfd_notifier_gsi(KVMState *s, EventNotifier *n, 5101c9b71a7SEric Auger int virq); 511197e3524SEric Auger int kvm_irqchip_add_irqfd_notifier(KVMState *s, EventNotifier *n, 512197e3524SEric Auger EventNotifier *rn, qemu_irq irq); 513197e3524SEric Auger int kvm_irqchip_remove_irqfd_notifier(KVMState *s, EventNotifier *n, 514197e3524SEric Auger qemu_irq irq); 515197e3524SEric Auger void kvm_irqchip_set_qemuirq_gsi(KVMState *s, qemu_irq irq, int gsi); 5169c17d615SPaolo Bonzini void kvm_pc_gsi_handler(void *opaque, int n, int level); 5179c17d615SPaolo Bonzini void kvm_pc_setup_irq_routing(bool pci_enabled); 5187b774593SAlexander Graf void kvm_init_irq_routing(KVMState *s); 519d6032e06SChristoffer Dall 520d6032e06SChristoffer Dall /** 521d6032e06SChristoffer Dall * kvm_arch_irqchip_create: 522d6032e06SChristoffer Dall * @KVMState: The KVMState pointer 52315eafc2eSPaolo Bonzini * @MachineState: The MachineState pointer 524d6032e06SChristoffer Dall * 525d6032e06SChristoffer Dall * Allow architectures to create an in-kernel irq chip themselves. 526d6032e06SChristoffer Dall * 527d6032e06SChristoffer Dall * Returns: < 0: error 528d6032e06SChristoffer Dall * 0: irq chip was not created 529d6032e06SChristoffer Dall * > 0: irq chip was created 530d6032e06SChristoffer Dall */ 53115eafc2eSPaolo Bonzini int kvm_arch_irqchip_create(MachineState *ms, KVMState *s); 532ada4135fSCornelia Huck 533ada4135fSCornelia Huck /** 534ada4135fSCornelia Huck * kvm_set_one_reg - set a register value in KVM via KVM_SET_ONE_REG ioctl 535ada4135fSCornelia Huck * @id: The register ID 536ada4135fSCornelia Huck * @source: The pointer to the value to be set. It must point to a variable 537ada4135fSCornelia Huck * of the correct type/size for the register being accessed. 538ada4135fSCornelia Huck * 539ada4135fSCornelia Huck * Returns: 0 on success, or a negative errno on failure. 540ada4135fSCornelia Huck */ 541ada4135fSCornelia Huck int kvm_set_one_reg(CPUState *cs, uint64_t id, void *source); 542ada4135fSCornelia Huck 543ada4135fSCornelia Huck /** 544ada4135fSCornelia Huck * kvm_get_one_reg - get a register value from KVM via KVM_GET_ONE_REG ioctl 545ada4135fSCornelia Huck * @id: The register ID 546ada4135fSCornelia Huck * @target: The pointer where the value is to be stored. It must point to a 547ada4135fSCornelia Huck * variable of the correct type/size for the register being accessed. 548ada4135fSCornelia Huck * 549ada4135fSCornelia Huck * Returns: 0 on success, or a negative errno on failure. 550ada4135fSCornelia Huck */ 551ada4135fSCornelia Huck int kvm_get_one_reg(CPUState *cs, uint64_t id, void *target); 55244f2e6c1SBharata B Rao int kvm_get_max_memslots(void); 5539c17d615SPaolo Bonzini #endif 554