blob: 3b25f27a7cc56dd9d8c8da703565df8725d81d71 [file] [log] [blame]
aliguori05330442008-11-05 16:29:27 +00001/*
2 * QEMU KVM support
3 *
4 * Copyright IBM, Corp. 2008
5 *
6 * Authors:
7 * Anthony Liguori <aliguori@us.ibm.com>
8 *
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.
11 *
12 */
13
14#ifndef QEMU_KVM_H
15#define QEMU_KVM_H
16
Michael S. Tsirkinca821802010-03-17 13:07:54 +020017#include <errno.h>
Blue Swirl1c14f162010-03-29 19:23:47 +000018#include "config-host.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010019#include "qemu/queue.h"
Andreas Färber504134d2012-12-17 06:38:45 +010020#include "qom/cpu.h"
aliguori05330442008-11-05 16:29:27 +000021
Michael S. Tsirkinca821802010-03-17 13:07:54 +020022#ifdef CONFIG_KVM
23#include <linux/kvm.h>
Eduardo Habkostbc74b7d2012-10-04 17:49:05 -030024#include <linux/kvm_para.h>
Eduardo Habkost9ca58922013-01-07 16:20:42 -020025#else
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.
29 */
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
Eduardo Habkostd61a23b2013-01-17 18:59:27 -020039#define KVM_FEATURE_CLOCKSOURCE_STABLE_BIT 0
Michael S. Tsirkinca821802010-03-17 13:07:54 +020040#endif
aliguori05330442008-11-05 16:29:27 +000041
liguangd5286af2013-01-24 13:03:27 +080042extern bool kvm_allowed;
Jan Kiszka3d4b2642012-01-31 19:17:52 +010043extern bool kvm_kernel_irqchip;
Peter Maydell7ae26bd2012-07-26 15:35:11 +010044extern bool kvm_async_interrupts_allowed;
Alexander Graf215e79c2013-04-24 22:24:12 +020045extern bool kvm_halt_in_kernel_allowed;
Peter Maydellcc7e0dd2012-07-26 15:35:14 +010046extern bool kvm_irqfds_allowed;
Peter Maydell614e41b2012-07-26 15:35:15 +010047extern bool kvm_msi_via_irqfd_allowed;
Peter Maydellf3e1bed2012-07-26 15:35:16 +010048extern bool kvm_gsi_routing_allowed;
Alexey Kardashevskiy76fe21d2013-09-03 18:08:25 +100049extern bool kvm_gsi_direct_mapping;
Jordan Justendf9c8b72013-05-29 01:27:25 -070050extern bool kvm_readonly_mem_allowed;
Paolo Bonzini98c85732010-04-19 18:59:30 +000051
52#if defined CONFIG_KVM || !defined NEED_CPU_H
Jan Kiszka3d4b2642012-01-31 19:17:52 +010053#define kvm_enabled() (kvm_allowed)
Peter Maydell96fda352012-07-26 15:35:17 +010054/**
55 * kvm_irqchip_in_kernel:
56 *
57 * Returns: true if the user asked us to create an in-kernel
58 * irqchip via the "kernel_irqchip=on" machine option.
59 * What this actually means is architecture and machine model
60 * specific: on PC, for instance, it means that the LAPIC,
61 * IOAPIC and PIT are all in kernel. This function should never
62 * be used from generic target-independent code: use one of the
63 * following functions or some other specific check instead.
64 */
Jan Kiszka3d4b2642012-01-31 19:17:52 +010065#define kvm_irqchip_in_kernel() (kvm_kernel_irqchip)
Peter Maydell7ae26bd2012-07-26 15:35:11 +010066
67/**
68 * kvm_async_interrupts_enabled:
69 *
70 * Returns: true if we can deliver interrupts to KVM
71 * asynchronously (ie by ioctl from any thread at any time)
72 * rather than having to do interrupt delivery synchronously
73 * (where the vcpu must be stopped at a suitable point first).
74 */
75#define kvm_async_interrupts_enabled() (kvm_async_interrupts_allowed)
76
Peter Maydellcc7e0dd2012-07-26 15:35:14 +010077/**
Alexander Graf215e79c2013-04-24 22:24:12 +020078 * kvm_halt_in_kernel
79 *
80 * Returns: true if halted cpus should still get a KVM_RUN ioctl to run
81 * inside of kernel space. This only works if MP state is implemented.
82 */
83#define kvm_halt_in_kernel() (kvm_halt_in_kernel_allowed)
84
85/**
Peter Maydellcc7e0dd2012-07-26 15:35:14 +010086 * kvm_irqfds_enabled:
87 *
88 * Returns: true if we can use irqfds to inject interrupts into
89 * a KVM CPU (ie the kernel supports irqfds and we are running
90 * with a configuration where it is meaningful to use them).
91 */
92#define kvm_irqfds_enabled() (kvm_irqfds_allowed)
93
Peter Maydell614e41b2012-07-26 15:35:15 +010094/**
95 * kvm_msi_via_irqfd_enabled:
96 *
97 * Returns: true if we can route a PCI MSI (Message Signaled Interrupt)
98 * to a KVM CPU via an irqfd. This requires that the kernel supports
99 * this and that we're running in a configuration that permits it.
100 */
101#define kvm_msi_via_irqfd_enabled() (kvm_msi_via_irqfd_allowed)
102
Peter Maydellf3e1bed2012-07-26 15:35:16 +0100103/**
104 * kvm_gsi_routing_enabled:
105 *
106 * Returns: true if GSI routing is enabled (ie the kernel supports
107 * it and we're running in a configuration that permits it).
108 */
109#define kvm_gsi_routing_enabled() (kvm_gsi_routing_allowed)
110
Jordan Justendf9c8b72013-05-29 01:27:25 -0700111/**
Alexey Kardashevskiy76fe21d2013-09-03 18:08:25 +1000112 * kvm_gsi_direct_mapping:
113 *
114 * Returns: true if GSI direct mapping is enabled.
115 */
116#define kvm_gsi_direct_mapping() (kvm_gsi_direct_mapping)
117
118/**
Jordan Justendf9c8b72013-05-29 01:27:25 -0700119 * kvm_readonly_mem_enabled:
120 *
121 * Returns: true if KVM readonly memory is enabled (ie the kernel
122 * supports it and we're running in a configuration that permits it).
123 */
124#define kvm_readonly_mem_enabled() (kvm_readonly_mem_allowed)
125
aliguori05330442008-11-05 16:29:27 +0000126#else
Jan Kiszka3d4b2642012-01-31 19:17:52 +0100127#define kvm_enabled() (0)
128#define kvm_irqchip_in_kernel() (false)
Peter Maydell7ae26bd2012-07-26 15:35:11 +0100129#define kvm_async_interrupts_enabled() (false)
Alexander Graf215e79c2013-04-24 22:24:12 +0200130#define kvm_halt_in_kernel() (false)
Peter Maydellcc7e0dd2012-07-26 15:35:14 +0100131#define kvm_irqfds_enabled() (false)
Peter Maydell614e41b2012-07-26 15:35:15 +0100132#define kvm_msi_via_irqfd_enabled() (false)
Peter Maydellf3e1bed2012-07-26 15:35:16 +0100133#define kvm_gsi_routing_allowed() (false)
Alexey Kardashevskiy76fe21d2013-09-03 18:08:25 +1000134#define kvm_gsi_direct_mapping() (false)
Jordan Justendf9c8b72013-05-29 01:27:25 -0700135#define kvm_readonly_mem_enabled() (false)
aliguori05330442008-11-05 16:29:27 +0000136#endif
137
138struct kvm_run;
Jan Kiszka680c1c62011-10-16 13:23:26 +0200139struct kvm_lapic_state;
aliguori05330442008-11-05 16:29:27 +0000140
Jan Kiszka94a8d392011-01-21 21:48:17 +0100141typedef struct KVMCapabilityInfo {
142 const char *name;
143 int value;
144} KVMCapabilityInfo;
145
146#define KVM_CAP_INFO(CAP) { "KVM_CAP_" stringify(CAP), KVM_CAP_##CAP }
147#define KVM_CAP_LAST_INFO { NULL, 0 }
148
Jan Kiszka92b4e482012-05-17 10:32:33 -0300149struct KVMState;
150typedef struct KVMState KVMState;
151extern KVMState *kvm_state;
152
aliguori05330442008-11-05 16:29:27 +0000153/* external API */
154
Jan Kiszkacad1e282011-01-21 21:48:16 +0100155int kvm_init(void);
aliguori05330442008-11-05 16:29:27 +0000156
Paolo Bonzini00a15552010-04-01 19:57:11 +0200157int kvm_has_sync_mmu(void);
158int kvm_has_vcpu_events(void);
159int kvm_has_robust_singlestep(void);
Jan Kiszkaff44f1a2010-03-12 15:20:49 +0100160int kvm_has_debugregs(void);
Sheng Yangf1665b22010-06-17 17:53:07 +0800161int kvm_has_xsave(void);
162int kvm_has_xcrs(void);
Jan Kiszka8a7c7392012-03-02 20:28:48 +0100163int kvm_has_pit_state2(void);
Stefan Hajnoczid2f2b8a2011-01-10 13:50:05 +0200164int kvm_has_many_ioeventfds(void);
Jan Kiszka84b058d2011-10-15 11:49:47 +0200165int kvm_has_gsi_routing(void);
Jan Kiszka3ab73842012-08-27 08:28:39 +0200166int kvm_has_intx_set_mask(void);
Paolo Bonzini00a15552010-04-01 19:57:11 +0200167
Andreas Färber504134d2012-12-17 06:38:45 +0100168int kvm_init_vcpu(CPUState *cpu);
Andreas Färber1458c362013-05-26 23:46:55 +0200169int kvm_cpu_exec(CPUState *cpu);
aliguori05330442008-11-05 16:29:27 +0000170
Andreas Färber504134d2012-12-17 06:38:45 +0100171#ifdef NEED_CPU_H
aliguori05330442008-11-05 16:29:27 +0000172
Igor Mammedovc4cfef52013-04-23 10:29:35 +0200173void kvm_setup_guest_memory(void *start, size_t size);
174void kvm_flush_coalesced_mmio_buffer(void);
175
Andreas Färber62278812013-06-27 17:12:06 +0200176int kvm_insert_breakpoint(CPUState *cpu, target_ulong addr,
aliguorie22a25c2009-03-12 20:12:48 +0000177 target_ulong len, int type);
Andreas Färber62278812013-06-27 17:12:06 +0200178int kvm_remove_breakpoint(CPUState *cpu, target_ulong addr,
aliguorie22a25c2009-03-12 20:12:48 +0000179 target_ulong len, int type);
Andreas Färber1d5791f2013-05-27 14:40:48 +0200180void kvm_remove_all_breakpoints(CPUState *cpu);
Stefan Weil38e478e2013-07-25 20:50:21 +0200181int kvm_update_guest_debug(CPUState *cpu, unsigned long reinject_trap);
Blue Swirl20c20522010-02-23 21:46:28 +0000182#ifndef _WIN32
Andreas Färber491d6e82013-05-26 23:38:10 +0200183int kvm_set_signal_mask(CPUState *cpu, const sigset_t *sigset);
Blue Swirl20c20522010-02-23 21:46:28 +0000184#endif
aliguorie22a25c2009-03-12 20:12:48 +0000185
Andreas Färber290adf32013-01-17 09:30:27 +0100186int kvm_on_sigbus_vcpu(CPUState *cpu, int code, void *addr);
Jan Kiszkaa1b87fe2011-02-01 22:15:51 +0100187int kvm_on_sigbus(int code, void *addr);
188
aliguori05330442008-11-05 16:29:27 +0000189/* internal API */
190
aliguori984b5182008-11-13 19:21:00 +0000191int kvm_ioctl(KVMState *s, int type, ...);
aliguori05330442008-11-05 16:29:27 +0000192
aliguori984b5182008-11-13 19:21:00 +0000193int kvm_vm_ioctl(KVMState *s, int type, ...);
aliguori05330442008-11-05 16:29:27 +0000194
Andreas Färber1bc22652012-10-31 06:06:49 +0100195int kvm_vcpu_ioctl(CPUState *cpu, int type, ...);
aliguori05330442008-11-05 16:29:27 +0000196
197/* Arch specific hooks */
198
Jan Kiszka94a8d392011-01-21 21:48:17 +0100199extern const KVMCapabilityInfo kvm_arch_required_capabilities[];
200
Andreas Färber20d695a2012-10-31 06:57:49 +0100201void kvm_arch_pre_run(CPUState *cpu, struct kvm_run *run);
202void kvm_arch_post_run(CPUState *cpu, struct kvm_run *run);
aliguori05330442008-11-05 16:29:27 +0000203
Andreas Färber20d695a2012-10-31 06:57:49 +0100204int kvm_arch_handle_exit(CPUState *cpu, struct kvm_run *run);
aliguori05330442008-11-05 16:29:27 +0000205
Andreas Färber20d695a2012-10-31 06:57:49 +0100206int kvm_arch_process_async_events(CPUState *cpu);
Marcelo Tosatti0af691d2010-05-04 09:45:27 -0300207
Andreas Färber20d695a2012-10-31 06:57:49 +0100208int kvm_arch_get_registers(CPUState *cpu);
aliguori05330442008-11-05 16:29:27 +0000209
Jan Kiszkaea375f92010-03-01 19:10:30 +0100210/* state subset only touched by the VCPU itself during runtime */
211#define KVM_PUT_RUNTIME_STATE 1
212/* state subset modified during VCPU reset */
213#define KVM_PUT_RESET_STATE 2
214/* full state set, modified during initialization or on vmload */
215#define KVM_PUT_FULL_STATE 3
216
Andreas Färber20d695a2012-10-31 06:57:49 +0100217int kvm_arch_put_registers(CPUState *cpu, int level);
aliguori05330442008-11-05 16:29:27 +0000218
Jan Kiszkacad1e282011-01-21 21:48:16 +0100219int kvm_arch_init(KVMState *s);
aliguori05330442008-11-05 16:29:27 +0000220
Andreas Färber20d695a2012-10-31 06:57:49 +0100221int kvm_arch_init_vcpu(CPUState *cpu);
aliguori05330442008-11-05 16:29:27 +0000222
Eduardo Habkostb164e482013-01-22 18:25:01 -0200223/* Returns VCPU ID to be used on KVM_CREATE_VCPU ioctl() */
224unsigned long kvm_arch_vcpu_id(CPUState *cpu);
225
Andreas Färber20d695a2012-10-31 06:57:49 +0100226void kvm_arch_reset_vcpu(CPUState *cpu);
Jan Kiszkacaa5af02009-11-06 19:39:24 +0100227
Andreas Färber20d695a2012-10-31 06:57:49 +0100228int kvm_arch_on_sigbus_vcpu(CPUState *cpu, int code, void *addr);
Jan Kiszkaa1b87fe2011-02-01 22:15:51 +0100229int kvm_arch_on_sigbus(int code, void *addr);
Marcelo Tosattic0532a72010-10-11 15:31:21 -0300230
Jan Kiszka84b058d2011-10-15 11:49:47 +0200231void kvm_arch_init_irq_routing(KVMState *s);
232
Peter Maydell3889c3f2012-07-26 15:35:12 +0100233int kvm_set_irq(KVMState *s, int irq, int level);
Jan Kiszka04fa27f2012-05-16 15:41:10 -0300234int kvm_irqchip_send_msi(KVMState *s, MSIMessage msg);
Jan Kiszka84b058d2011-10-15 11:49:47 +0200235
Jan Kiszka1df186d2012-05-17 10:32:32 -0300236void kvm_irqchip_add_irq_route(KVMState *s, int gsi, int irqchip, int pin);
Alexander Grafcb925cf2013-04-17 01:11:55 +0200237void kvm_irqchip_commit_routes(KVMState *s);
Jan Kiszka84b058d2011-10-15 11:49:47 +0200238
Jan Kiszka680c1c62011-10-16 13:23:26 +0200239void kvm_put_apic_state(DeviceState *d, struct kvm_lapic_state *kapic);
240void kvm_get_apic_state(DeviceState *d, struct kvm_lapic_state *kapic);
241
aliguorie22a25c2009-03-12 20:12:48 +0000242struct kvm_guest_debug;
243struct kvm_debug_exit_arch;
244
245struct kvm_sw_breakpoint {
246 target_ulong pc;
247 target_ulong saved_insn;
248 int use_count;
Blue Swirl72cf2d42009-09-12 07:36:22 +0000249 QTAILQ_ENTRY(kvm_sw_breakpoint) entry;
aliguorie22a25c2009-03-12 20:12:48 +0000250};
251
Blue Swirl72cf2d42009-09-12 07:36:22 +0000252QTAILQ_HEAD(kvm_sw_breakpoint_head, kvm_sw_breakpoint);
aliguorie22a25c2009-03-12 20:12:48 +0000253
Andreas Färbera60f24b2012-12-01 05:35:08 +0100254struct kvm_sw_breakpoint *kvm_find_sw_breakpoint(CPUState *cpu,
aliguorie22a25c2009-03-12 20:12:48 +0000255 target_ulong pc);
256
Andreas Färbera60f24b2012-12-01 05:35:08 +0100257int kvm_sw_breakpoints_active(CPUState *cpu);
aliguorie22a25c2009-03-12 20:12:48 +0000258
Andreas Färber80b7cd72013-06-19 17:37:31 +0200259int kvm_arch_insert_sw_breakpoint(CPUState *cpu,
aliguorie22a25c2009-03-12 20:12:48 +0000260 struct kvm_sw_breakpoint *bp);
Andreas Färber80b7cd72013-06-19 17:37:31 +0200261int kvm_arch_remove_sw_breakpoint(CPUState *cpu,
aliguorie22a25c2009-03-12 20:12:48 +0000262 struct kvm_sw_breakpoint *bp);
263int kvm_arch_insert_hw_breakpoint(target_ulong addr,
264 target_ulong len, int type);
265int kvm_arch_remove_hw_breakpoint(target_ulong addr,
266 target_ulong len, int type);
267void kvm_arch_remove_all_hw_breakpoints(void);
268
Andreas Färber20d695a2012-10-31 06:57:49 +0100269void kvm_arch_update_guest_debug(CPUState *cpu, struct kvm_guest_debug *dbg);
aliguorie22a25c2009-03-12 20:12:48 +0000270
Andreas Färber20d695a2012-10-31 06:57:49 +0100271bool kvm_arch_stop_on_emulation_error(CPUState *cpu);
Gleb Natapov4513d922010-05-10 11:21:34 +0300272
Anthony Liguoriad7b8b32009-05-08 15:33:24 -0500273int kvm_check_extension(KVMState *s, unsigned int extension);
274
Jan Kiszkaba9bc592011-06-08 16:11:05 +0200275uint32_t kvm_arch_get_supported_cpuid(KVMState *env, uint32_t function,
Sheng Yangc958a8b2010-06-17 15:18:13 +0800276 uint32_t index, int reg);
James Hogan97577fd2013-08-27 12:19:10 +0100277
278#if !defined(CONFIG_USER_ONLY)
279int kvm_physical_memory_addr_from_host(KVMState *s, void *ram_addr,
280 hwaddr *phys_addr);
281#endif
282
283#endif /* NEED_CPU_H */
284
Andreas Färberdd1750d2013-05-01 13:45:44 +0200285void kvm_cpu_synchronize_state(CPUState *cpu);
James Hogan97577fd2013-08-27 12:19:10 +0100286void kvm_cpu_synchronize_post_reset(CPUState *cpu);
287void kvm_cpu_synchronize_post_init(CPUState *cpu);
Avi Kivityb827df52009-05-03 17:04:01 +0300288
aliguorie22a25c2009-03-12 20:12:48 +0000289/* generic hooks - to be moved/refactored once there are more users */
290
Andreas Färbercb446ec2013-05-01 14:24:52 +0200291static inline void cpu_synchronize_state(CPUState *cpu)
aliguorie22a25c2009-03-12 20:12:48 +0000292{
293 if (kvm_enabled()) {
Andreas Färbercb446ec2013-05-01 14:24:52 +0200294 kvm_cpu_synchronize_state(cpu);
aliguorie22a25c2009-03-12 20:12:48 +0000295 }
296}
297
Igor Mammedov3f24a582013-04-11 16:51:41 +0200298static inline void cpu_synchronize_post_reset(CPUState *cpu)
Jan Kiszkaea375f92010-03-01 19:10:30 +0100299{
300 if (kvm_enabled()) {
Igor Mammedov3f24a582013-04-11 16:51:41 +0200301 kvm_cpu_synchronize_post_reset(cpu);
Jan Kiszkaea375f92010-03-01 19:10:30 +0100302 }
303}
304
Igor Mammedov3f24a582013-04-11 16:51:41 +0200305static inline void cpu_synchronize_post_init(CPUState *cpu)
Jan Kiszkaea375f92010-03-01 19:10:30 +0100306{
307 if (kvm_enabled()) {
Igor Mammedov3f24a582013-04-11 16:51:41 +0200308 kvm_cpu_synchronize_post_init(cpu);
Jan Kiszkaea375f92010-03-01 19:10:30 +0100309 }
310}
Michael S. Tsirkinca821802010-03-17 13:07:54 +0200311
Jan Kiszka92b4e482012-05-17 10:32:33 -0300312int kvm_irqchip_add_msi_route(KVMState *s, MSIMessage msg);
Jan Kiszkacc574072012-08-27 08:28:38 +0200313int kvm_irqchip_update_msi_route(KVMState *s, int virq, MSIMessage msg);
Jan Kiszka1e2aa8b2012-05-17 10:32:34 -0300314void kvm_irqchip_release_virq(KVMState *s, int virq);
Jan Kiszka39853bb2012-05-17 10:32:36 -0300315
Vincenzo Maffioneca916d32013-07-22 11:51:33 +0200316int kvm_irqchip_add_irqfd_notifier(KVMState *s, EventNotifier *n,
317 EventNotifier *rn, int virq);
Jan Kiszkab131c742012-08-20 10:55:56 +0200318int kvm_irqchip_remove_irqfd_notifier(KVMState *s, EventNotifier *n, int virq);
Jason Barond8ee0382012-11-14 15:54:02 -0500319void kvm_pc_gsi_handler(void *opaque, int n, int level);
320void kvm_pc_setup_irq_routing(bool pci_enabled);
Alexander Graf7b774592013-04-16 15:58:13 +0200321void kvm_init_irq_routing(KVMState *s);
aliguori05330442008-11-05 16:29:27 +0000322#endif