blob: 8ef44767afa8d340938c28f622e48d39acb1081e [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
aliguori05330442008-11-05 16:29:27 +000047/* external API */
48
Jan Kiszkacad1e282011-01-21 21:48:16 +010049int kvm_init(void);
aliguori05330442008-11-05 16:29:27 +000050
Paolo Bonzini00a15552010-04-01 19:57:11 +020051int kvm_has_sync_mmu(void);
52int kvm_has_vcpu_events(void);
53int kvm_has_robust_singlestep(void);
Jan Kiszkaff44f1a2010-03-12 15:20:49 +010054int kvm_has_debugregs(void);
Sheng Yangf1665b22010-06-17 17:53:07 +080055int kvm_has_xsave(void);
56int kvm_has_xcrs(void);
Jan Kiszka8a7c7392012-03-02 20:28:48 +010057int kvm_has_pit_state2(void);
Stefan Hajnoczid2f2b8a2011-01-10 13:50:05 +020058int kvm_has_many_ioeventfds(void);
Jan Kiszka84b058d2011-10-15 11:49:47 +020059int kvm_has_gsi_routing(void);
Paolo Bonzini00a15552010-04-01 19:57:11 +020060
Jan Kiszka9b5b76d2011-10-15 14:08:26 +020061int kvm_allows_irq0_override(void);
62
Blue Swirl1c14f162010-03-29 19:23:47 +000063#ifdef NEED_CPU_H
aliguori05330442008-11-05 16:29:27 +000064int kvm_init_vcpu(CPUState *env);
65
66int kvm_cpu_exec(CPUState *env);
67
Paul Brookb3755a92010-03-12 16:54:58 +000068#if !defined(CONFIG_USER_ONLY)
Jan Kiszka6f0437e2009-04-26 18:03:40 +020069void kvm_setup_guest_memory(void *start, size_t size);
70
Anthony Liguoric227f092009-10-01 16:12:16 -050071int kvm_coalesce_mmio_region(target_phys_addr_t start, ram_addr_t size);
72int kvm_uncoalesce_mmio_region(target_phys_addr_t start, ram_addr_t size);
Sheng Yang62a27442010-01-26 19:21:16 +080073void kvm_flush_coalesced_mmio_buffer(void);
Paul Brookb3755a92010-03-12 16:54:58 +000074#endif
aliguorif65ed4c2008-12-09 20:09:57 +000075
aliguorie22a25c2009-03-12 20:12:48 +000076int kvm_insert_breakpoint(CPUState *current_env, target_ulong addr,
77 target_ulong len, int type);
78int kvm_remove_breakpoint(CPUState *current_env, target_ulong addr,
79 target_ulong len, int type);
80void kvm_remove_all_breakpoints(CPUState *current_env);
81int kvm_update_guest_debug(CPUState *env, unsigned long reinject_trap);
Blue Swirl20c20522010-02-23 21:46:28 +000082#ifndef _WIN32
Marcelo Tosatticc84de92010-02-17 20:14:42 -020083int kvm_set_signal_mask(CPUState *env, const sigset_t *sigset);
Blue Swirl20c20522010-02-23 21:46:28 +000084#endif
aliguorie22a25c2009-03-12 20:12:48 +000085
Glauber Costa6f725c12009-07-21 12:26:58 -030086int kvm_pit_in_kernel(void);
Glauber Costa6f725c12009-07-21 12:26:58 -030087
Jan Kiszkaa1b87fe2011-02-01 22:15:51 +010088int kvm_on_sigbus_vcpu(CPUState *env, int code, void *addr);
89int kvm_on_sigbus(int code, void *addr);
90
aliguori05330442008-11-05 16:29:27 +000091/* internal API */
92
93struct KVMState;
94typedef struct KVMState KVMState;
Jan Kiszka6a7af8c2011-02-07 12:19:25 +010095extern KVMState *kvm_state;
aliguori05330442008-11-05 16:29:27 +000096
aliguori984b5182008-11-13 19:21:00 +000097int kvm_ioctl(KVMState *s, int type, ...);
aliguori05330442008-11-05 16:29:27 +000098
aliguori984b5182008-11-13 19:21:00 +000099int kvm_vm_ioctl(KVMState *s, int type, ...);
aliguori05330442008-11-05 16:29:27 +0000100
aliguori984b5182008-11-13 19:21:00 +0000101int kvm_vcpu_ioctl(CPUState *env, int type, ...);
aliguori05330442008-11-05 16:29:27 +0000102
103/* Arch specific hooks */
104
Jan Kiszka94a8d392011-01-21 21:48:17 +0100105extern const KVMCapabilityInfo kvm_arch_required_capabilities[];
106
Jan Kiszka7a39fe52011-02-07 12:19:20 +0100107void kvm_arch_pre_run(CPUState *env, struct kvm_run *run);
108void kvm_arch_post_run(CPUState *env, struct kvm_run *run);
aliguori05330442008-11-05 16:29:27 +0000109
110int kvm_arch_handle_exit(CPUState *env, struct kvm_run *run);
111
Jan Kiszka99036862011-03-02 08:56:13 +0100112int kvm_arch_process_async_events(CPUState *env);
Marcelo Tosatti0af691d2010-05-04 09:45:27 -0300113
aliguori05330442008-11-05 16:29:27 +0000114int kvm_arch_get_registers(CPUState *env);
115
Jan Kiszkaea375f92010-03-01 19:10:30 +0100116/* state subset only touched by the VCPU itself during runtime */
117#define KVM_PUT_RUNTIME_STATE 1
118/* state subset modified during VCPU reset */
119#define KVM_PUT_RESET_STATE 2
120/* full state set, modified during initialization or on vmload */
121#define KVM_PUT_FULL_STATE 3
122
123int kvm_arch_put_registers(CPUState *env, int level);
aliguori05330442008-11-05 16:29:27 +0000124
Jan Kiszkacad1e282011-01-21 21:48:16 +0100125int kvm_arch_init(KVMState *s);
aliguori05330442008-11-05 16:29:27 +0000126
127int kvm_arch_init_vcpu(CPUState *env);
128
Jan Kiszkacaa5af02009-11-06 19:39:24 +0100129void kvm_arch_reset_vcpu(CPUState *env);
130
Jan Kiszkaa1b87fe2011-02-01 22:15:51 +0100131int kvm_arch_on_sigbus_vcpu(CPUState *env, int code, void *addr);
132int kvm_arch_on_sigbus(int code, void *addr);
Marcelo Tosattic0532a72010-10-11 15:31:21 -0300133
Jan Kiszka84b058d2011-10-15 11:49:47 +0200134void kvm_arch_init_irq_routing(KVMState *s);
135
136int kvm_irqchip_set_irq(KVMState *s, int irq, int level);
137
138void kvm_irqchip_add_route(KVMState *s, int gsi, int irqchip, int pin);
139int kvm_irqchip_commit_routes(KVMState *s);
140
Jan Kiszka680c1c62011-10-16 13:23:26 +0200141void kvm_put_apic_state(DeviceState *d, struct kvm_lapic_state *kapic);
142void kvm_get_apic_state(DeviceState *d, struct kvm_lapic_state *kapic);
143
aliguorie22a25c2009-03-12 20:12:48 +0000144struct kvm_guest_debug;
145struct kvm_debug_exit_arch;
146
147struct kvm_sw_breakpoint {
148 target_ulong pc;
149 target_ulong saved_insn;
150 int use_count;
Blue Swirl72cf2d42009-09-12 07:36:22 +0000151 QTAILQ_ENTRY(kvm_sw_breakpoint) entry;
aliguorie22a25c2009-03-12 20:12:48 +0000152};
153
Blue Swirl72cf2d42009-09-12 07:36:22 +0000154QTAILQ_HEAD(kvm_sw_breakpoint_head, kvm_sw_breakpoint);
aliguorie22a25c2009-03-12 20:12:48 +0000155
aliguorie22a25c2009-03-12 20:12:48 +0000156struct kvm_sw_breakpoint *kvm_find_sw_breakpoint(CPUState *env,
157 target_ulong pc);
158
159int kvm_sw_breakpoints_active(CPUState *env);
160
161int kvm_arch_insert_sw_breakpoint(CPUState *current_env,
162 struct kvm_sw_breakpoint *bp);
163int kvm_arch_remove_sw_breakpoint(CPUState *current_env,
164 struct kvm_sw_breakpoint *bp);
165int kvm_arch_insert_hw_breakpoint(target_ulong addr,
166 target_ulong len, int type);
167int kvm_arch_remove_hw_breakpoint(target_ulong addr,
168 target_ulong len, int type);
169void kvm_arch_remove_all_hw_breakpoints(void);
170
171void kvm_arch_update_guest_debug(CPUState *env, struct kvm_guest_debug *dbg);
172
Gleb Natapov4513d922010-05-10 11:21:34 +0300173bool kvm_arch_stop_on_emulation_error(CPUState *env);
174
Anthony Liguoriad7b8b32009-05-08 15:33:24 -0500175int kvm_check_extension(KVMState *s, unsigned int extension);
176
Jan Kiszkaba9bc592011-06-08 16:11:05 +0200177uint32_t kvm_arch_get_supported_cpuid(KVMState *env, uint32_t function,
Sheng Yangc958a8b2010-06-17 15:18:13 +0800178 uint32_t index, int reg);
Avi Kivity4c0960c2009-08-17 23:19:53 +0300179void kvm_cpu_synchronize_state(CPUState *env);
Jan Kiszkaea375f92010-03-01 19:10:30 +0100180void kvm_cpu_synchronize_post_reset(CPUState *env);
181void kvm_cpu_synchronize_post_init(CPUState *env);
Avi Kivityb827df52009-05-03 17:04:01 +0300182
aliguorie22a25c2009-03-12 20:12:48 +0000183/* generic hooks - to be moved/refactored once there are more users */
184
Avi Kivity4c0960c2009-08-17 23:19:53 +0300185static inline void cpu_synchronize_state(CPUState *env)
aliguorie22a25c2009-03-12 20:12:48 +0000186{
187 if (kvm_enabled()) {
Avi Kivity4c0960c2009-08-17 23:19:53 +0300188 kvm_cpu_synchronize_state(env);
aliguorie22a25c2009-03-12 20:12:48 +0000189 }
190}
191
Jan Kiszkaea375f92010-03-01 19:10:30 +0100192static inline void cpu_synchronize_post_reset(CPUState *env)
193{
194 if (kvm_enabled()) {
195 kvm_cpu_synchronize_post_reset(env);
196 }
197}
198
199static inline void cpu_synchronize_post_init(CPUState *env)
200{
201 if (kvm_enabled()) {
202 kvm_cpu_synchronize_post_init(env);
203 }
204}
Michael S. Tsirkinca821802010-03-17 13:07:54 +0200205
Huang Ying983dfc32010-10-11 15:31:20 -0300206
207#if !defined(CONFIG_USER_ONLY)
Avi Kivity9f213ed2011-12-15 19:55:26 +0200208int kvm_physical_memory_addr_from_host(KVMState *s, void *ram_addr,
209 target_phys_addr_t *phys_addr);
Huang Ying983dfc32010-10-11 15:31:20 -0300210#endif
211
Michael S. Tsirkinca821802010-03-17 13:07:54 +0200212#endif
Cam Macdonell44f1a3d2010-07-26 18:10:59 -0600213int kvm_set_ioeventfd_mmio_long(int fd, uint32_t adr, uint32_t val, bool assign);
Michael S. Tsirkinca821802010-03-17 13:07:54 +0200214
Paolo Bonzini98c85732010-04-19 18:59:30 +0000215int kvm_set_ioeventfd_pio_word(int fd, uint16_t adr, uint16_t val, bool assign);
aliguori05330442008-11-05 16:29:27 +0000216#endif