blob: 5b8f588813ae774a1a736c8a92d674ffdd033c61 [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"
Blue Swirl72cf2d42009-09-12 07:36:22 +000019#include "qemu-queue.h"
aliguori05330442008-11-05 16:29:27 +000020
Michael S. Tsirkinca821802010-03-17 13:07:54 +020021#ifdef CONFIG_KVM
22#include <linux/kvm.h>
23#endif
aliguori05330442008-11-05 16:29:27 +000024
Michael S. Tsirkinca821802010-03-17 13:07:54 +020025extern int kvm_allowed;
Jan Kiszka3d4b2642012-01-31 19:17:52 +010026extern bool kvm_kernel_irqchip;
Peter Maydell7ae26bd2012-07-26 15:35:11 +010027extern bool kvm_async_interrupts_allowed;
Peter Maydellcc7e0dd2012-07-26 15:35:14 +010028extern bool kvm_irqfds_allowed;
Peter Maydell614e41b2012-07-26 15:35:15 +010029extern bool kvm_msi_via_irqfd_allowed;
Peter Maydellf3e1bed2012-07-26 15:35:16 +010030extern bool kvm_gsi_routing_allowed;
Paolo Bonzini98c85732010-04-19 18:59:30 +000031
32#if defined CONFIG_KVM || !defined NEED_CPU_H
Jan Kiszka3d4b2642012-01-31 19:17:52 +010033#define kvm_enabled() (kvm_allowed)
Peter Maydell96fda352012-07-26 15:35:17 +010034/**
35 * kvm_irqchip_in_kernel:
36 *
37 * Returns: true if the user asked us to create an in-kernel
38 * irqchip via the "kernel_irqchip=on" machine option.
39 * What this actually means is architecture and machine model
40 * specific: on PC, for instance, it means that the LAPIC,
41 * IOAPIC and PIT are all in kernel. This function should never
42 * be used from generic target-independent code: use one of the
43 * following functions or some other specific check instead.
44 */
Jan Kiszka3d4b2642012-01-31 19:17:52 +010045#define kvm_irqchip_in_kernel() (kvm_kernel_irqchip)
Peter Maydell7ae26bd2012-07-26 15:35:11 +010046
47/**
48 * kvm_async_interrupts_enabled:
49 *
50 * Returns: true if we can deliver interrupts to KVM
51 * asynchronously (ie by ioctl from any thread at any time)
52 * rather than having to do interrupt delivery synchronously
53 * (where the vcpu must be stopped at a suitable point first).
54 */
55#define kvm_async_interrupts_enabled() (kvm_async_interrupts_allowed)
56
Peter Maydellcc7e0dd2012-07-26 15:35:14 +010057/**
58 * kvm_irqfds_enabled:
59 *
60 * Returns: true if we can use irqfds to inject interrupts into
61 * a KVM CPU (ie the kernel supports irqfds and we are running
62 * with a configuration where it is meaningful to use them).
63 */
64#define kvm_irqfds_enabled() (kvm_irqfds_allowed)
65
Peter Maydell614e41b2012-07-26 15:35:15 +010066/**
67 * kvm_msi_via_irqfd_enabled:
68 *
69 * Returns: true if we can route a PCI MSI (Message Signaled Interrupt)
70 * to a KVM CPU via an irqfd. This requires that the kernel supports
71 * this and that we're running in a configuration that permits it.
72 */
73#define kvm_msi_via_irqfd_enabled() (kvm_msi_via_irqfd_allowed)
74
Peter Maydellf3e1bed2012-07-26 15:35:16 +010075/**
76 * kvm_gsi_routing_enabled:
77 *
78 * Returns: true if GSI routing is enabled (ie the kernel supports
79 * it and we're running in a configuration that permits it).
80 */
81#define kvm_gsi_routing_enabled() (kvm_gsi_routing_allowed)
82
aliguori05330442008-11-05 16:29:27 +000083#else
Jan Kiszka3d4b2642012-01-31 19:17:52 +010084#define kvm_enabled() (0)
85#define kvm_irqchip_in_kernel() (false)
Peter Maydell7ae26bd2012-07-26 15:35:11 +010086#define kvm_async_interrupts_enabled() (false)
Peter Maydellcc7e0dd2012-07-26 15:35:14 +010087#define kvm_irqfds_enabled() (false)
Peter Maydell614e41b2012-07-26 15:35:15 +010088#define kvm_msi_via_irqfd_enabled() (false)
Peter Maydellf3e1bed2012-07-26 15:35:16 +010089#define kvm_gsi_routing_allowed() (false)
aliguori05330442008-11-05 16:29:27 +000090#endif
91
92struct kvm_run;
Jan Kiszka680c1c62011-10-16 13:23:26 +020093struct kvm_lapic_state;
aliguori05330442008-11-05 16:29:27 +000094
Jan Kiszka94a8d392011-01-21 21:48:17 +010095typedef struct KVMCapabilityInfo {
96 const char *name;
97 int value;
98} KVMCapabilityInfo;
99
100#define KVM_CAP_INFO(CAP) { "KVM_CAP_" stringify(CAP), KVM_CAP_##CAP }
101#define KVM_CAP_LAST_INFO { NULL, 0 }
102
Jan Kiszka92b4e482012-05-17 10:32:33 -0300103struct KVMState;
104typedef struct KVMState KVMState;
105extern KVMState *kvm_state;
106
aliguori05330442008-11-05 16:29:27 +0000107/* external API */
108
Jan Kiszkacad1e282011-01-21 21:48:16 +0100109int kvm_init(void);
aliguori05330442008-11-05 16:29:27 +0000110
Paolo Bonzini00a15552010-04-01 19:57:11 +0200111int kvm_has_sync_mmu(void);
112int kvm_has_vcpu_events(void);
113int kvm_has_robust_singlestep(void);
Jan Kiszkaff44f1a2010-03-12 15:20:49 +0100114int kvm_has_debugregs(void);
Sheng Yangf1665b22010-06-17 17:53:07 +0800115int kvm_has_xsave(void);
116int kvm_has_xcrs(void);
Jan Kiszka8a7c7392012-03-02 20:28:48 +0100117int kvm_has_pit_state2(void);
Stefan Hajnoczid2f2b8a2011-01-10 13:50:05 +0200118int kvm_has_many_ioeventfds(void);
Jan Kiszka84b058d2011-10-15 11:49:47 +0200119int kvm_has_gsi_routing(void);
Paolo Bonzini00a15552010-04-01 19:57:11 +0200120
Blue Swirl1c14f162010-03-29 19:23:47 +0000121#ifdef NEED_CPU_H
Andreas Färber9349b4f2012-03-14 01:38:32 +0100122int kvm_init_vcpu(CPUArchState *env);
aliguori05330442008-11-05 16:29:27 +0000123
Andreas Färber9349b4f2012-03-14 01:38:32 +0100124int kvm_cpu_exec(CPUArchState *env);
aliguori05330442008-11-05 16:29:27 +0000125
Paul Brookb3755a92010-03-12 16:54:58 +0000126#if !defined(CONFIG_USER_ONLY)
Christian Borntraegerfdec9912012-06-15 05:10:30 +0000127void *kvm_vmalloc(ram_addr_t size);
128void *kvm_arch_vmalloc(ram_addr_t size);
Jan Kiszka6f0437e2009-04-26 18:03:40 +0200129void kvm_setup_guest_memory(void *start, size_t size);
130
Anthony Liguoric227f092009-10-01 16:12:16 -0500131int kvm_coalesce_mmio_region(target_phys_addr_t start, ram_addr_t size);
132int kvm_uncoalesce_mmio_region(target_phys_addr_t start, ram_addr_t size);
Sheng Yang62a27442010-01-26 19:21:16 +0800133void kvm_flush_coalesced_mmio_buffer(void);
Paul Brookb3755a92010-03-12 16:54:58 +0000134#endif
aliguorif65ed4c2008-12-09 20:09:57 +0000135
Andreas Färber9349b4f2012-03-14 01:38:32 +0100136int kvm_insert_breakpoint(CPUArchState *current_env, target_ulong addr,
aliguorie22a25c2009-03-12 20:12:48 +0000137 target_ulong len, int type);
Andreas Färber9349b4f2012-03-14 01:38:32 +0100138int kvm_remove_breakpoint(CPUArchState *current_env, target_ulong addr,
aliguorie22a25c2009-03-12 20:12:48 +0000139 target_ulong len, int type);
Andreas Färber9349b4f2012-03-14 01:38:32 +0100140void kvm_remove_all_breakpoints(CPUArchState *current_env);
141int kvm_update_guest_debug(CPUArchState *env, unsigned long reinject_trap);
Blue Swirl20c20522010-02-23 21:46:28 +0000142#ifndef _WIN32
Andreas Färber9349b4f2012-03-14 01:38:32 +0100143int kvm_set_signal_mask(CPUArchState *env, const sigset_t *sigset);
Blue Swirl20c20522010-02-23 21:46:28 +0000144#endif
aliguorie22a25c2009-03-12 20:12:48 +0000145
Andreas Färber9349b4f2012-03-14 01:38:32 +0100146int kvm_on_sigbus_vcpu(CPUArchState *env, int code, void *addr);
Jan Kiszkaa1b87fe2011-02-01 22:15:51 +0100147int kvm_on_sigbus(int code, void *addr);
148
aliguori05330442008-11-05 16:29:27 +0000149/* internal API */
150
aliguori984b5182008-11-13 19:21:00 +0000151int kvm_ioctl(KVMState *s, int type, ...);
aliguori05330442008-11-05 16:29:27 +0000152
aliguori984b5182008-11-13 19:21:00 +0000153int kvm_vm_ioctl(KVMState *s, int type, ...);
aliguori05330442008-11-05 16:29:27 +0000154
Andreas Färber9349b4f2012-03-14 01:38:32 +0100155int kvm_vcpu_ioctl(CPUArchState *env, int type, ...);
aliguori05330442008-11-05 16:29:27 +0000156
157/* Arch specific hooks */
158
Jan Kiszka94a8d392011-01-21 21:48:17 +0100159extern const KVMCapabilityInfo kvm_arch_required_capabilities[];
160
Andreas Färber9349b4f2012-03-14 01:38:32 +0100161void kvm_arch_pre_run(CPUArchState *env, struct kvm_run *run);
162void kvm_arch_post_run(CPUArchState *env, struct kvm_run *run);
aliguori05330442008-11-05 16:29:27 +0000163
Andreas Färber9349b4f2012-03-14 01:38:32 +0100164int kvm_arch_handle_exit(CPUArchState *env, struct kvm_run *run);
aliguori05330442008-11-05 16:29:27 +0000165
Andreas Färber9349b4f2012-03-14 01:38:32 +0100166int kvm_arch_process_async_events(CPUArchState *env);
Marcelo Tosatti0af691d2010-05-04 09:45:27 -0300167
Andreas Färber9349b4f2012-03-14 01:38:32 +0100168int kvm_arch_get_registers(CPUArchState *env);
aliguori05330442008-11-05 16:29:27 +0000169
Jan Kiszkaea375f92010-03-01 19:10:30 +0100170/* state subset only touched by the VCPU itself during runtime */
171#define KVM_PUT_RUNTIME_STATE 1
172/* state subset modified during VCPU reset */
173#define KVM_PUT_RESET_STATE 2
174/* full state set, modified during initialization or on vmload */
175#define KVM_PUT_FULL_STATE 3
176
Andreas Färber9349b4f2012-03-14 01:38:32 +0100177int kvm_arch_put_registers(CPUArchState *env, int level);
aliguori05330442008-11-05 16:29:27 +0000178
Jan Kiszkacad1e282011-01-21 21:48:16 +0100179int kvm_arch_init(KVMState *s);
aliguori05330442008-11-05 16:29:27 +0000180
Andreas Färber9349b4f2012-03-14 01:38:32 +0100181int kvm_arch_init_vcpu(CPUArchState *env);
aliguori05330442008-11-05 16:29:27 +0000182
Andreas Färber9349b4f2012-03-14 01:38:32 +0100183void kvm_arch_reset_vcpu(CPUArchState *env);
Jan Kiszkacaa5af02009-11-06 19:39:24 +0100184
Andreas Färber9349b4f2012-03-14 01:38:32 +0100185int kvm_arch_on_sigbus_vcpu(CPUArchState *env, int code, void *addr);
Jan Kiszkaa1b87fe2011-02-01 22:15:51 +0100186int kvm_arch_on_sigbus(int code, void *addr);
Marcelo Tosattic0532a72010-10-11 15:31:21 -0300187
Jan Kiszka84b058d2011-10-15 11:49:47 +0200188void kvm_arch_init_irq_routing(KVMState *s);
189
Peter Maydell3889c3f2012-07-26 15:35:12 +0100190int kvm_set_irq(KVMState *s, int irq, int level);
Jan Kiszka04fa27f2012-05-16 15:41:10 -0300191int kvm_irqchip_send_msi(KVMState *s, MSIMessage msg);
Jan Kiszka84b058d2011-10-15 11:49:47 +0200192
Jan Kiszka1df186d2012-05-17 10:32:32 -0300193void kvm_irqchip_add_irq_route(KVMState *s, int gsi, int irqchip, int pin);
Jan Kiszka84b058d2011-10-15 11:49:47 +0200194
Jan Kiszka680c1c62011-10-16 13:23:26 +0200195void kvm_put_apic_state(DeviceState *d, struct kvm_lapic_state *kapic);
196void kvm_get_apic_state(DeviceState *d, struct kvm_lapic_state *kapic);
197
aliguorie22a25c2009-03-12 20:12:48 +0000198struct kvm_guest_debug;
199struct kvm_debug_exit_arch;
200
201struct kvm_sw_breakpoint {
202 target_ulong pc;
203 target_ulong saved_insn;
204 int use_count;
Blue Swirl72cf2d42009-09-12 07:36:22 +0000205 QTAILQ_ENTRY(kvm_sw_breakpoint) entry;
aliguorie22a25c2009-03-12 20:12:48 +0000206};
207
Blue Swirl72cf2d42009-09-12 07:36:22 +0000208QTAILQ_HEAD(kvm_sw_breakpoint_head, kvm_sw_breakpoint);
aliguorie22a25c2009-03-12 20:12:48 +0000209
Andreas Färber9349b4f2012-03-14 01:38:32 +0100210struct kvm_sw_breakpoint *kvm_find_sw_breakpoint(CPUArchState *env,
aliguorie22a25c2009-03-12 20:12:48 +0000211 target_ulong pc);
212
Andreas Färber9349b4f2012-03-14 01:38:32 +0100213int kvm_sw_breakpoints_active(CPUArchState *env);
aliguorie22a25c2009-03-12 20:12:48 +0000214
Andreas Färber9349b4f2012-03-14 01:38:32 +0100215int kvm_arch_insert_sw_breakpoint(CPUArchState *current_env,
aliguorie22a25c2009-03-12 20:12:48 +0000216 struct kvm_sw_breakpoint *bp);
Andreas Färber9349b4f2012-03-14 01:38:32 +0100217int kvm_arch_remove_sw_breakpoint(CPUArchState *current_env,
aliguorie22a25c2009-03-12 20:12:48 +0000218 struct kvm_sw_breakpoint *bp);
219int kvm_arch_insert_hw_breakpoint(target_ulong addr,
220 target_ulong len, int type);
221int kvm_arch_remove_hw_breakpoint(target_ulong addr,
222 target_ulong len, int type);
223void kvm_arch_remove_all_hw_breakpoints(void);
224
Andreas Färber9349b4f2012-03-14 01:38:32 +0100225void kvm_arch_update_guest_debug(CPUArchState *env, struct kvm_guest_debug *dbg);
aliguorie22a25c2009-03-12 20:12:48 +0000226
Andreas Färber9349b4f2012-03-14 01:38:32 +0100227bool kvm_arch_stop_on_emulation_error(CPUArchState *env);
Gleb Natapov4513d922010-05-10 11:21:34 +0300228
Anthony Liguoriad7b8b32009-05-08 15:33:24 -0500229int kvm_check_extension(KVMState *s, unsigned int extension);
230
Jan Kiszkaba9bc592011-06-08 16:11:05 +0200231uint32_t kvm_arch_get_supported_cpuid(KVMState *env, uint32_t function,
Sheng Yangc958a8b2010-06-17 15:18:13 +0800232 uint32_t index, int reg);
Andreas Färber9349b4f2012-03-14 01:38:32 +0100233void kvm_cpu_synchronize_state(CPUArchState *env);
234void kvm_cpu_synchronize_post_reset(CPUArchState *env);
235void kvm_cpu_synchronize_post_init(CPUArchState *env);
Avi Kivityb827df52009-05-03 17:04:01 +0300236
aliguorie22a25c2009-03-12 20:12:48 +0000237/* generic hooks - to be moved/refactored once there are more users */
238
Andreas Färber9349b4f2012-03-14 01:38:32 +0100239static inline void cpu_synchronize_state(CPUArchState *env)
aliguorie22a25c2009-03-12 20:12:48 +0000240{
241 if (kvm_enabled()) {
Avi Kivity4c0960c2009-08-17 23:19:53 +0300242 kvm_cpu_synchronize_state(env);
aliguorie22a25c2009-03-12 20:12:48 +0000243 }
244}
245
Andreas Färber9349b4f2012-03-14 01:38:32 +0100246static inline void cpu_synchronize_post_reset(CPUArchState *env)
Jan Kiszkaea375f92010-03-01 19:10:30 +0100247{
248 if (kvm_enabled()) {
249 kvm_cpu_synchronize_post_reset(env);
250 }
251}
252
Andreas Färber9349b4f2012-03-14 01:38:32 +0100253static inline void cpu_synchronize_post_init(CPUArchState *env)
Jan Kiszkaea375f92010-03-01 19:10:30 +0100254{
255 if (kvm_enabled()) {
256 kvm_cpu_synchronize_post_init(env);
257 }
258}
Michael S. Tsirkinca821802010-03-17 13:07:54 +0200259
Huang Ying983dfc32010-10-11 15:31:20 -0300260
261#if !defined(CONFIG_USER_ONLY)
Avi Kivity9f213ed2011-12-15 19:55:26 +0200262int kvm_physical_memory_addr_from_host(KVMState *s, void *ram_addr,
263 target_phys_addr_t *phys_addr);
Huang Ying983dfc32010-10-11 15:31:20 -0300264#endif
265
Michael S. Tsirkinca821802010-03-17 13:07:54 +0200266#endif
Michael S. Tsirkin4b8f1c82012-03-20 14:31:38 +0200267int kvm_set_ioeventfd_mmio(int fd, uint32_t adr, uint32_t val, bool assign,
268 uint32_t size);
Michael S. Tsirkinca821802010-03-17 13:07:54 +0200269
Paolo Bonzini98c85732010-04-19 18:59:30 +0000270int kvm_set_ioeventfd_pio_word(int fd, uint16_t adr, uint16_t val, bool assign);
Jan Kiszka92b4e482012-05-17 10:32:33 -0300271
272int kvm_irqchip_add_msi_route(KVMState *s, MSIMessage msg);
Jan Kiszka1e2aa8b2012-05-17 10:32:34 -0300273void kvm_irqchip_release_virq(KVMState *s, int virq);
Jan Kiszka39853bb2012-05-17 10:32:36 -0300274
275int kvm_irqchip_add_irqfd(KVMState *s, int fd, int virq);
276int kvm_irqchip_remove_irqfd(KVMState *s, int fd, int virq);
Paolo Bonzini15b2bd12012-07-05 17:16:30 +0200277int kvm_irqchip_add_irq_notifier(KVMState *s, EventNotifier *n, int virq);
278int kvm_irqchip_remove_irq_notifier(KVMState *s, EventNotifier *n, int virq);
aliguori05330442008-11-05 16:29:27 +0000279#endif