4 * Copyright IBM, Corp. 2008
7 * Anthony Liguori <aliguori@us.ibm.com>
9 * This work is licensed under the terms of the GNU GPL, version 2 or later.
10 * See the COPYING file in the top-level directory.
18 #include "config-host.h"
19 #include "qemu/queue.h"
23 #include <linux/kvm.h>
24 #include <linux/kvm_para.h>
26 /* These constants must never be used at runtime if kvm_enabled() is false.
27 * They exist so we don't need #ifdefs around KVM-specific code that already
28 * checks kvm_enabled() properly.
30 #define KVM_CPUID_SIGNATURE 0
31 #define KVM_CPUID_FEATURES 0
32 #define KVM_FEATURE_CLOCKSOURCE 0
33 #define KVM_FEATURE_NOP_IO_DELAY 0
34 #define KVM_FEATURE_MMU_OP 0
35 #define KVM_FEATURE_CLOCKSOURCE2 0
36 #define KVM_FEATURE_ASYNC_PF 0
37 #define KVM_FEATURE_STEAL_TIME 0
38 #define KVM_FEATURE_PV_EOI 0
41 extern int kvm_allowed
;
42 extern bool kvm_kernel_irqchip
;
43 extern bool kvm_async_interrupts_allowed
;
44 extern bool kvm_irqfds_allowed
;
45 extern bool kvm_msi_via_irqfd_allowed
;
46 extern bool kvm_gsi_routing_allowed
;
48 #if defined CONFIG_KVM || !defined NEED_CPU_H
49 #define kvm_enabled() (kvm_allowed)
51 * kvm_irqchip_in_kernel:
53 * Returns: true if the user asked us to create an in-kernel
54 * irqchip via the "kernel_irqchip=on" machine option.
55 * What this actually means is architecture and machine model
56 * specific: on PC, for instance, it means that the LAPIC,
57 * IOAPIC and PIT are all in kernel. This function should never
58 * be used from generic target-independent code: use one of the
59 * following functions or some other specific check instead.
61 #define kvm_irqchip_in_kernel() (kvm_kernel_irqchip)
64 * kvm_async_interrupts_enabled:
66 * Returns: true if we can deliver interrupts to KVM
67 * asynchronously (ie by ioctl from any thread at any time)
68 * rather than having to do interrupt delivery synchronously
69 * (where the vcpu must be stopped at a suitable point first).
71 #define kvm_async_interrupts_enabled() (kvm_async_interrupts_allowed)
76 * Returns: true if we can use irqfds to inject interrupts into
77 * a KVM CPU (ie the kernel supports irqfds and we are running
78 * with a configuration where it is meaningful to use them).
80 #define kvm_irqfds_enabled() (kvm_irqfds_allowed)
83 * kvm_msi_via_irqfd_enabled:
85 * Returns: true if we can route a PCI MSI (Message Signaled Interrupt)
86 * to a KVM CPU via an irqfd. This requires that the kernel supports
87 * this and that we're running in a configuration that permits it.
89 #define kvm_msi_via_irqfd_enabled() (kvm_msi_via_irqfd_allowed)
92 * kvm_gsi_routing_enabled:
94 * Returns: true if GSI routing is enabled (ie the kernel supports
95 * it and we're running in a configuration that permits it).
97 #define kvm_gsi_routing_enabled() (kvm_gsi_routing_allowed)
100 #define kvm_enabled() (0)
101 #define kvm_irqchip_in_kernel() (false)
102 #define kvm_async_interrupts_enabled() (false)
103 #define kvm_irqfds_enabled() (false)
104 #define kvm_msi_via_irqfd_enabled() (false)
105 #define kvm_gsi_routing_allowed() (false)
109 struct kvm_lapic_state
;
111 typedef struct KVMCapabilityInfo
{
116 #define KVM_CAP_INFO(CAP) { "KVM_CAP_" stringify(CAP), KVM_CAP_##CAP }
117 #define KVM_CAP_LAST_INFO { NULL, 0 }
120 typedef struct KVMState KVMState
;
121 extern KVMState
*kvm_state
;
127 int kvm_has_sync_mmu(void);
128 int kvm_has_vcpu_events(void);
129 int kvm_has_robust_singlestep(void);
130 int kvm_has_debugregs(void);
131 int kvm_has_xsave(void);
132 int kvm_has_xcrs(void);
133 int kvm_has_pit_state2(void);
134 int kvm_has_many_ioeventfds(void);
135 int kvm_has_gsi_routing(void);
136 int kvm_has_intx_set_mask(void);
138 int kvm_init_vcpu(CPUState
*cpu
);
141 int kvm_cpu_exec(CPUArchState
*env
);
143 #if !defined(CONFIG_USER_ONLY)
144 void *kvm_vmalloc(ram_addr_t size
);
145 void *kvm_arch_vmalloc(ram_addr_t size
);
146 void kvm_setup_guest_memory(void *start
, size_t size
);
148 void kvm_flush_coalesced_mmio_buffer(void);
151 int kvm_insert_breakpoint(CPUArchState
*current_env
, target_ulong addr
,
152 target_ulong len
, int type
);
153 int kvm_remove_breakpoint(CPUArchState
*current_env
, target_ulong addr
,
154 target_ulong len
, int type
);
155 void kvm_remove_all_breakpoints(CPUArchState
*current_env
);
156 int kvm_update_guest_debug(CPUArchState
*env
, unsigned long reinject_trap
);
158 int kvm_set_signal_mask(CPUArchState
*env
, const sigset_t
*sigset
);
161 int kvm_on_sigbus_vcpu(CPUArchState
*env
, int code
, void *addr
);
162 int kvm_on_sigbus(int code
, void *addr
);
166 int kvm_ioctl(KVMState
*s
, int type
, ...);
168 int kvm_vm_ioctl(KVMState
*s
, int type
, ...);
170 int kvm_vcpu_ioctl(CPUState
*cpu
, int type
, ...);
172 /* Arch specific hooks */
174 extern const KVMCapabilityInfo kvm_arch_required_capabilities
[];
176 void kvm_arch_pre_run(CPUState
*cpu
, struct kvm_run
*run
);
177 void kvm_arch_post_run(CPUState
*cpu
, struct kvm_run
*run
);
179 int kvm_arch_handle_exit(CPUState
*cpu
, struct kvm_run
*run
);
181 int kvm_arch_process_async_events(CPUState
*cpu
);
183 int kvm_arch_get_registers(CPUState
*cpu
);
185 /* state subset only touched by the VCPU itself during runtime */
186 #define KVM_PUT_RUNTIME_STATE 1
187 /* state subset modified during VCPU reset */
188 #define KVM_PUT_RESET_STATE 2
189 /* full state set, modified during initialization or on vmload */
190 #define KVM_PUT_FULL_STATE 3
192 int kvm_arch_put_registers(CPUState
*cpu
, int level
);
194 int kvm_arch_init(KVMState
*s
);
196 int kvm_arch_init_vcpu(CPUState
*cpu
);
198 void kvm_arch_reset_vcpu(CPUState
*cpu
);
200 int kvm_arch_on_sigbus_vcpu(CPUState
*cpu
, int code
, void *addr
);
201 int kvm_arch_on_sigbus(int code
, void *addr
);
203 void kvm_arch_init_irq_routing(KVMState
*s
);
205 int kvm_set_irq(KVMState
*s
, int irq
, int level
);
206 int kvm_irqchip_send_msi(KVMState
*s
, MSIMessage msg
);
208 void kvm_irqchip_add_irq_route(KVMState
*s
, int gsi
, int irqchip
, int pin
);
210 void kvm_put_apic_state(DeviceState
*d
, struct kvm_lapic_state
*kapic
);
211 void kvm_get_apic_state(DeviceState
*d
, struct kvm_lapic_state
*kapic
);
213 struct kvm_guest_debug
;
214 struct kvm_debug_exit_arch
;
216 struct kvm_sw_breakpoint
{
218 target_ulong saved_insn
;
220 QTAILQ_ENTRY(kvm_sw_breakpoint
) entry
;
223 QTAILQ_HEAD(kvm_sw_breakpoint_head
, kvm_sw_breakpoint
);
225 struct kvm_sw_breakpoint
*kvm_find_sw_breakpoint(CPUState
*cpu
,
228 int kvm_sw_breakpoints_active(CPUState
*cpu
);
230 int kvm_arch_insert_sw_breakpoint(CPUState
*current_cpu
,
231 struct kvm_sw_breakpoint
*bp
);
232 int kvm_arch_remove_sw_breakpoint(CPUState
*current_cpu
,
233 struct kvm_sw_breakpoint
*bp
);
234 int kvm_arch_insert_hw_breakpoint(target_ulong addr
,
235 target_ulong len
, int type
);
236 int kvm_arch_remove_hw_breakpoint(target_ulong addr
,
237 target_ulong len
, int type
);
238 void kvm_arch_remove_all_hw_breakpoints(void);
240 void kvm_arch_update_guest_debug(CPUState
*cpu
, struct kvm_guest_debug
*dbg
);
242 bool kvm_arch_stop_on_emulation_error(CPUState
*cpu
);
244 int kvm_check_extension(KVMState
*s
, unsigned int extension
);
246 uint32_t kvm_arch_get_supported_cpuid(KVMState
*env
, uint32_t function
,
247 uint32_t index
, int reg
);
248 void kvm_cpu_synchronize_state(CPUArchState
*env
);
249 void kvm_cpu_synchronize_post_reset(CPUArchState
*env
);
250 void kvm_cpu_synchronize_post_init(CPUArchState
*env
);
252 /* generic hooks - to be moved/refactored once there are more users */
254 static inline void cpu_synchronize_state(CPUArchState
*env
)
257 kvm_cpu_synchronize_state(env
);
261 static inline void cpu_synchronize_post_reset(CPUArchState
*env
)
264 kvm_cpu_synchronize_post_reset(env
);
268 static inline void cpu_synchronize_post_init(CPUArchState
*env
)
271 kvm_cpu_synchronize_post_init(env
);
276 #if !defined(CONFIG_USER_ONLY)
277 int kvm_physical_memory_addr_from_host(KVMState
*s
, void *ram_addr
,
282 int kvm_set_ioeventfd_mmio(int fd
, uint32_t adr
, uint32_t val
, bool assign
,
285 int kvm_set_ioeventfd_pio_word(int fd
, uint16_t adr
, uint16_t val
, bool assign
);
287 int kvm_irqchip_add_msi_route(KVMState
*s
, MSIMessage msg
);
288 int kvm_irqchip_update_msi_route(KVMState
*s
, int virq
, MSIMessage msg
);
289 void kvm_irqchip_release_virq(KVMState
*s
, int virq
);
291 int kvm_irqchip_add_irqfd_notifier(KVMState
*s
, EventNotifier
*n
, int virq
);
292 int kvm_irqchip_remove_irqfd_notifier(KVMState
*s
, EventNotifier
*n
, int virq
);
293 void kvm_pc_gsi_handler(void *opaque
, int n
, int level
);
294 void kvm_pc_setup_irq_routing(bool pci_enabled
);