blob: 9c7b0ea6aec1eaf22feb0c1469da1715a0005a86 [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;
Paolo Bonzini98c85732010-04-19 18:59:30 +000027
28#if defined CONFIG_KVM || !defined NEED_CPU_H
Jan Kiszka3d4b2642012-01-31 19:17:52 +010029#define kvm_enabled() (kvm_allowed)
30#define kvm_irqchip_in_kernel() (kvm_kernel_irqchip)
aliguori05330442008-11-05 16:29:27 +000031#else
Jan Kiszka3d4b2642012-01-31 19:17:52 +010032#define kvm_enabled() (0)
33#define kvm_irqchip_in_kernel() (false)
aliguori05330442008-11-05 16:29:27 +000034#endif
35
36struct kvm_run;
Jan Kiszka680c1c62011-10-16 13:23:26 +020037struct kvm_lapic_state;
aliguori05330442008-11-05 16:29:27 +000038
Jan Kiszka94a8d392011-01-21 21:48:17 +010039typedef struct KVMCapabilityInfo {
40 const char *name;
41 int value;
42} KVMCapabilityInfo;
43
44#define KVM_CAP_INFO(CAP) { "KVM_CAP_" stringify(CAP), KVM_CAP_##CAP }
45#define KVM_CAP_LAST_INFO { NULL, 0 }
46
Jan Kiszka92b4e482012-05-17 10:32:33 -030047struct KVMState;
48typedef struct KVMState KVMState;
49extern KVMState *kvm_state;
50
aliguori05330442008-11-05 16:29:27 +000051/* external API */
52
Jan Kiszkacad1e282011-01-21 21:48:16 +010053int kvm_init(void);
aliguori05330442008-11-05 16:29:27 +000054
Paolo Bonzini00a15552010-04-01 19:57:11 +020055int kvm_has_sync_mmu(void);
56int kvm_has_vcpu_events(void);
57int kvm_has_robust_singlestep(void);
Jan Kiszkaff44f1a2010-03-12 15:20:49 +010058int kvm_has_debugregs(void);
Sheng Yangf1665b22010-06-17 17:53:07 +080059int kvm_has_xsave(void);
60int kvm_has_xcrs(void);
Jan Kiszka8a7c7392012-03-02 20:28:48 +010061int kvm_has_pit_state2(void);
Stefan Hajnoczid2f2b8a2011-01-10 13:50:05 +020062int kvm_has_many_ioeventfds(void);
Jan Kiszka84b058d2011-10-15 11:49:47 +020063int kvm_has_gsi_routing(void);
Paolo Bonzini00a15552010-04-01 19:57:11 +020064
Jan Kiszka9b5b76d2011-10-15 14:08:26 +020065int kvm_allows_irq0_override(void);
66
Blue Swirl1c14f162010-03-29 19:23:47 +000067#ifdef NEED_CPU_H
Andreas Färber9349b4f2012-03-14 01:38:32 +010068int kvm_init_vcpu(CPUArchState *env);
aliguori05330442008-11-05 16:29:27 +000069
Andreas Färber9349b4f2012-03-14 01:38:32 +010070int kvm_cpu_exec(CPUArchState *env);
aliguori05330442008-11-05 16:29:27 +000071
Paul Brookb3755a92010-03-12 16:54:58 +000072#if !defined(CONFIG_USER_ONLY)
Jan Kiszka6f0437e2009-04-26 18:03:40 +020073void kvm_setup_guest_memory(void *start, size_t size);
74
Anthony Liguoric227f092009-10-01 16:12:16 -050075int kvm_coalesce_mmio_region(target_phys_addr_t start, ram_addr_t size);
76int kvm_uncoalesce_mmio_region(target_phys_addr_t start, ram_addr_t size);
Sheng Yang62a27442010-01-26 19:21:16 +080077void kvm_flush_coalesced_mmio_buffer(void);
Paul Brookb3755a92010-03-12 16:54:58 +000078#endif
aliguorif65ed4c2008-12-09 20:09:57 +000079
Andreas Färber9349b4f2012-03-14 01:38:32 +010080int kvm_insert_breakpoint(CPUArchState *current_env, target_ulong addr,
aliguorie22a25c2009-03-12 20:12:48 +000081 target_ulong len, int type);
Andreas Färber9349b4f2012-03-14 01:38:32 +010082int kvm_remove_breakpoint(CPUArchState *current_env, target_ulong addr,
aliguorie22a25c2009-03-12 20:12:48 +000083 target_ulong len, int type);
Andreas Färber9349b4f2012-03-14 01:38:32 +010084void kvm_remove_all_breakpoints(CPUArchState *current_env);
85int kvm_update_guest_debug(CPUArchState *env, unsigned long reinject_trap);
Blue Swirl20c20522010-02-23 21:46:28 +000086#ifndef _WIN32
Andreas Färber9349b4f2012-03-14 01:38:32 +010087int kvm_set_signal_mask(CPUArchState *env, const sigset_t *sigset);
Blue Swirl20c20522010-02-23 21:46:28 +000088#endif
aliguorie22a25c2009-03-12 20:12:48 +000089
Andreas Färber9349b4f2012-03-14 01:38:32 +010090int kvm_on_sigbus_vcpu(CPUArchState *env, int code, void *addr);
Jan Kiszkaa1b87fe2011-02-01 22:15:51 +010091int kvm_on_sigbus(int code, void *addr);
92
aliguori05330442008-11-05 16:29:27 +000093/* internal API */
94
aliguori984b5182008-11-13 19:21:00 +000095int kvm_ioctl(KVMState *s, int type, ...);
aliguori05330442008-11-05 16:29:27 +000096
aliguori984b5182008-11-13 19:21:00 +000097int kvm_vm_ioctl(KVMState *s, int type, ...);
aliguori05330442008-11-05 16:29:27 +000098
Andreas Färber9349b4f2012-03-14 01:38:32 +010099int kvm_vcpu_ioctl(CPUArchState *env, int type, ...);
aliguori05330442008-11-05 16:29:27 +0000100
101/* Arch specific hooks */
102
Jan Kiszka94a8d392011-01-21 21:48:17 +0100103extern const KVMCapabilityInfo kvm_arch_required_capabilities[];
104
Andreas Färber9349b4f2012-03-14 01:38:32 +0100105void kvm_arch_pre_run(CPUArchState *env, struct kvm_run *run);
106void kvm_arch_post_run(CPUArchState *env, struct kvm_run *run);
aliguori05330442008-11-05 16:29:27 +0000107
Andreas Färber9349b4f2012-03-14 01:38:32 +0100108int kvm_arch_handle_exit(CPUArchState *env, struct kvm_run *run);
aliguori05330442008-11-05 16:29:27 +0000109
Andreas Färber9349b4f2012-03-14 01:38:32 +0100110int kvm_arch_process_async_events(CPUArchState *env);
Marcelo Tosatti0af691d2010-05-04 09:45:27 -0300111
Andreas Färber9349b4f2012-03-14 01:38:32 +0100112int kvm_arch_get_registers(CPUArchState *env);
aliguori05330442008-11-05 16:29:27 +0000113
Jan Kiszkaea375f92010-03-01 19:10:30 +0100114/* state subset only touched by the VCPU itself during runtime */
115#define KVM_PUT_RUNTIME_STATE 1
116/* state subset modified during VCPU reset */
117#define KVM_PUT_RESET_STATE 2
118/* full state set, modified during initialization or on vmload */
119#define KVM_PUT_FULL_STATE 3
120
Andreas Färber9349b4f2012-03-14 01:38:32 +0100121int kvm_arch_put_registers(CPUArchState *env, int level);
aliguori05330442008-11-05 16:29:27 +0000122
Jan Kiszkacad1e282011-01-21 21:48:16 +0100123int kvm_arch_init(KVMState *s);
aliguori05330442008-11-05 16:29:27 +0000124
Andreas Färber9349b4f2012-03-14 01:38:32 +0100125int kvm_arch_init_vcpu(CPUArchState *env);
aliguori05330442008-11-05 16:29:27 +0000126
Andreas Färber9349b4f2012-03-14 01:38:32 +0100127void kvm_arch_reset_vcpu(CPUArchState *env);
Jan Kiszkacaa5af02009-11-06 19:39:24 +0100128
Andreas Färber9349b4f2012-03-14 01:38:32 +0100129int kvm_arch_on_sigbus_vcpu(CPUArchState *env, int code, void *addr);
Jan Kiszkaa1b87fe2011-02-01 22:15:51 +0100130int kvm_arch_on_sigbus(int code, void *addr);
Marcelo Tosattic0532a72010-10-11 15:31:21 -0300131
Jan Kiszka84b058d2011-10-15 11:49:47 +0200132void kvm_arch_init_irq_routing(KVMState *s);
133
134int kvm_irqchip_set_irq(KVMState *s, int irq, int level);
Jan Kiszka04fa27f2012-05-16 15:41:10 -0300135int kvm_irqchip_send_msi(KVMState *s, MSIMessage msg);
Jan Kiszka84b058d2011-10-15 11:49:47 +0200136
Jan Kiszka1df186d2012-05-17 10:32:32 -0300137void kvm_irqchip_add_irq_route(KVMState *s, int gsi, int irqchip, int pin);
Jan Kiszka84b058d2011-10-15 11:49:47 +0200138
Jan Kiszka680c1c62011-10-16 13:23:26 +0200139void kvm_put_apic_state(DeviceState *d, struct kvm_lapic_state *kapic);
140void kvm_get_apic_state(DeviceState *d, struct kvm_lapic_state *kapic);
141
aliguorie22a25c2009-03-12 20:12:48 +0000142struct kvm_guest_debug;
143struct kvm_debug_exit_arch;
144
145struct kvm_sw_breakpoint {
146 target_ulong pc;
147 target_ulong saved_insn;
148 int use_count;
Blue Swirl72cf2d42009-09-12 07:36:22 +0000149 QTAILQ_ENTRY(kvm_sw_breakpoint) entry;
aliguorie22a25c2009-03-12 20:12:48 +0000150};
151
Blue Swirl72cf2d42009-09-12 07:36:22 +0000152QTAILQ_HEAD(kvm_sw_breakpoint_head, kvm_sw_breakpoint);
aliguorie22a25c2009-03-12 20:12:48 +0000153
Andreas Färber9349b4f2012-03-14 01:38:32 +0100154struct kvm_sw_breakpoint *kvm_find_sw_breakpoint(CPUArchState *env,
aliguorie22a25c2009-03-12 20:12:48 +0000155 target_ulong pc);
156
Andreas Färber9349b4f2012-03-14 01:38:32 +0100157int kvm_sw_breakpoints_active(CPUArchState *env);
aliguorie22a25c2009-03-12 20:12:48 +0000158
Andreas Färber9349b4f2012-03-14 01:38:32 +0100159int kvm_arch_insert_sw_breakpoint(CPUArchState *current_env,
aliguorie22a25c2009-03-12 20:12:48 +0000160 struct kvm_sw_breakpoint *bp);
Andreas Färber9349b4f2012-03-14 01:38:32 +0100161int kvm_arch_remove_sw_breakpoint(CPUArchState *current_env,
aliguorie22a25c2009-03-12 20:12:48 +0000162 struct kvm_sw_breakpoint *bp);
163int kvm_arch_insert_hw_breakpoint(target_ulong addr,
164 target_ulong len, int type);
165int kvm_arch_remove_hw_breakpoint(target_ulong addr,
166 target_ulong len, int type);
167void kvm_arch_remove_all_hw_breakpoints(void);
168
Andreas Färber9349b4f2012-03-14 01:38:32 +0100169void kvm_arch_update_guest_debug(CPUArchState *env, struct kvm_guest_debug *dbg);
aliguorie22a25c2009-03-12 20:12:48 +0000170
Andreas Färber9349b4f2012-03-14 01:38:32 +0100171bool kvm_arch_stop_on_emulation_error(CPUArchState *env);
Gleb Natapov4513d922010-05-10 11:21:34 +0300172
Anthony Liguoriad7b8b32009-05-08 15:33:24 -0500173int kvm_check_extension(KVMState *s, unsigned int extension);
174
Jan Kiszkaba9bc592011-06-08 16:11:05 +0200175uint32_t kvm_arch_get_supported_cpuid(KVMState *env, uint32_t function,
Sheng Yangc958a8b2010-06-17 15:18:13 +0800176 uint32_t index, int reg);
Andreas Färber9349b4f2012-03-14 01:38:32 +0100177void kvm_cpu_synchronize_state(CPUArchState *env);
178void kvm_cpu_synchronize_post_reset(CPUArchState *env);
179void kvm_cpu_synchronize_post_init(CPUArchState *env);
Avi Kivityb827df52009-05-03 17:04:01 +0300180
aliguorie22a25c2009-03-12 20:12:48 +0000181/* generic hooks - to be moved/refactored once there are more users */
182
Andreas Färber9349b4f2012-03-14 01:38:32 +0100183static inline void cpu_synchronize_state(CPUArchState *env)
aliguorie22a25c2009-03-12 20:12:48 +0000184{
185 if (kvm_enabled()) {
Avi Kivity4c0960c2009-08-17 23:19:53 +0300186 kvm_cpu_synchronize_state(env);
aliguorie22a25c2009-03-12 20:12:48 +0000187 }
188}
189
Andreas Färber9349b4f2012-03-14 01:38:32 +0100190static inline void cpu_synchronize_post_reset(CPUArchState *env)
Jan Kiszkaea375f92010-03-01 19:10:30 +0100191{
192 if (kvm_enabled()) {
193 kvm_cpu_synchronize_post_reset(env);
194 }
195}
196
Andreas Färber9349b4f2012-03-14 01:38:32 +0100197static inline void cpu_synchronize_post_init(CPUArchState *env)
Jan Kiszkaea375f92010-03-01 19:10:30 +0100198{
199 if (kvm_enabled()) {
200 kvm_cpu_synchronize_post_init(env);
201 }
202}
Michael S. Tsirkinca821802010-03-17 13:07:54 +0200203
Huang Ying983dfc32010-10-11 15:31:20 -0300204
205#if !defined(CONFIG_USER_ONLY)
Avi Kivity9f213ed2011-12-15 19:55:26 +0200206int kvm_physical_memory_addr_from_host(KVMState *s, void *ram_addr,
207 target_phys_addr_t *phys_addr);
Huang Ying983dfc32010-10-11 15:31:20 -0300208#endif
209
Michael S. Tsirkinca821802010-03-17 13:07:54 +0200210#endif
Michael S. Tsirkin4b8f1c82012-03-20 14:31:38 +0200211int kvm_set_ioeventfd_mmio(int fd, uint32_t adr, uint32_t val, bool assign,
212 uint32_t size);
Michael S. Tsirkinca821802010-03-17 13:07:54 +0200213
Paolo Bonzini98c85732010-04-19 18:59:30 +0000214int kvm_set_ioeventfd_pio_word(int fd, uint16_t adr, uint16_t val, bool assign);
Jan Kiszka92b4e482012-05-17 10:32:33 -0300215
216int kvm_irqchip_add_msi_route(KVMState *s, MSIMessage msg);
Jan Kiszka1e2aa8b2012-05-17 10:32:34 -0300217void kvm_irqchip_release_virq(KVMState *s, int virq);
Jan Kiszka39853bb2012-05-17 10:32:36 -0300218
219int kvm_irqchip_add_irqfd(KVMState *s, int fd, int virq);
220int kvm_irqchip_remove_irqfd(KVMState *s, int fd, int virq);
aliguori05330442008-11-05 16:29:27 +0000221#endif