xref: /illumos-kvm-cmd/kvm.h (revision 36b02c7d)
1 /*
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  * Copyright 2011 Joyent, Inc.
13  */
14 
15 #ifndef QEMU_KVM_H
16 #define QEMU_KVM_H
17 
18 #include <errno.h>
19 #include "config-host.h"
20 #include "qemu-queue.h"
21 #ifdef NEED_CPU_H
22 #include "qemu-kvm.h"
23 #endif
24 
25 #ifdef CONFIG_KVM
26 #ifdef __sun__
27 #include <sys/kvm.h>
28 #else
29 #include <linux/kvm.h>
30 #endif
31 
32 
33 #endif
34 
35 extern int kvm_allowed;
36 
37 #if defined CONFIG_KVM || !defined NEED_CPU_H
38 #define kvm_enabled() (kvm_allowed)
39 #else
40 #define kvm_enabled() (0)
41 #endif
42 
43 #ifdef OBSOLETE_KVM_IMPL
44 struct kvm_run;
45 
46 typedef struct KVMCapabilityInfo {
47     const char *name;
48     int value;
49 } KVMCapabilityInfo;
50 
51 #define KVM_CAP_INFO(CAP) { "KVM_CAP_" stringify(CAP), KVM_CAP_##CAP }
52 #define KVM_CAP_LAST_INFO { NULL, 0 }
53 
54 /* external API */
55 
56 int kvm_init(void);
57 #endif /* OBSOLETE_KVM_IMPL */
58 
59 int kvm_has_sync_mmu(void);
60 int kvm_has_vcpu_events(void);
61 int kvm_has_robust_singlestep(void);
62 int kvm_has_debugregs(void);
63 int kvm_has_xsave(void);
64 int kvm_has_xcrs(void);
65 int kvm_has_many_ioeventfds(void);
66 
67 #ifdef NEED_CPU_H
68 int kvm_init_vcpu(CPUState *env);
69 
70 int kvm_cpu_exec(CPUState *env);
71 
72 #if !defined(CONFIG_USER_ONLY)
73 int kvm_log_start(target_phys_addr_t phys_addr, ram_addr_t size);
74 int kvm_log_stop(target_phys_addr_t phys_addr, ram_addr_t size);
75 
76 void kvm_cpu_register_phys_memory_client(void);
77 
78 void kvm_setup_guest_memory(void *start, size_t size);
79 
80 int kvm_coalesce_mmio_region(target_phys_addr_t start, ram_addr_t size);
81 int kvm_uncoalesce_mmio_region(target_phys_addr_t start, ram_addr_t size);
82 void kvm_flush_coalesced_mmio_buffer(void);
83 #endif
84 
85 int kvm_insert_breakpoint(CPUState *current_env, target_ulong addr,
86                           target_ulong len, int type);
87 int kvm_remove_breakpoint(CPUState *current_env, target_ulong addr,
88                           target_ulong len, int type);
89 void kvm_remove_all_breakpoints(CPUState *current_env);
90 int kvm_update_guest_debug(CPUState *env, unsigned long reinject_trap);
91 #ifndef _WIN32
92 int kvm_set_signal_mask(CPUState *env, const sigset_t *sigset);
93 #endif
94 
95 int kvm_pit_in_kernel(void);
96 
97 /* internal API */
98 
99 struct KVMState;
100 typedef struct KVMState KVMState;
101 
102 int kvm_ioctl(KVMState *s, int type, ...);
103 
104 int kvm_vm_ioctl(KVMState *s, int type, ...);
105 
106 int kvm_vcpu_ioctl(CPUState *env, int type, ...);
107 
108 /* Arch specific hooks */
109 
110 #ifdef OBSOLETE_KVM_IMPL
111 extern const KVMCapabilityInfo kvm_arch_required_capabilities[];
112 #endif
113 
114 int kvm_arch_post_run(CPUState *env, struct kvm_run *run);
115 
116 int kvm_arch_handle_exit(CPUState *env, struct kvm_run *run);
117 
118 int kvm_arch_pre_run(CPUState *env, struct kvm_run *run);
119 
120 #ifdef OBSOLETE_KVM_IMPL
121 int kvm_arch_process_irqchip_events(CPUState *env);
122 #endif
123 
124 int kvm_arch_get_registers(CPUState *env);
125 
126 /* state subset only touched by the VCPU itself during runtime */
127 #define KVM_PUT_RUNTIME_STATE   1
128 /* state subset modified during VCPU reset */
129 #define KVM_PUT_RESET_STATE     2
130 /* full state set, modified during initialization or on vmload */
131 #define KVM_PUT_FULL_STATE      3
132 
133 int kvm_arch_put_registers(CPUState *env, int level);
134 
135 int kvm_arch_init(KVMState *s);
136 
137 int kvm_arch_init_vcpu(CPUState *env);
138 
139 void kvm_arch_reset_vcpu(CPUState *env);
140 
141 int kvm_on_sigbus_vcpu(CPUState *env, int code, void *addr);
142 int kvm_on_sigbus(int code, void *addr);
143 
144 struct kvm_guest_debug;
145 struct kvm_debug_exit_arch;
146 
147 struct kvm_sw_breakpoint {
148     target_ulong pc;
149     target_ulong saved_insn;
150     int use_count;
151     QTAILQ_ENTRY(kvm_sw_breakpoint) entry;
152 };
153 
154 QTAILQ_HEAD(kvm_sw_breakpoint_head, kvm_sw_breakpoint);
155 
156 int kvm_arch_debug(struct kvm_debug_exit_arch *arch_info);
157 
158 struct kvm_sw_breakpoint *kvm_find_sw_breakpoint(CPUState *env,
159                                                  target_ulong pc);
160 
161 int kvm_sw_breakpoints_active(CPUState *env);
162 
163 int kvm_arch_insert_sw_breakpoint(CPUState *current_env,
164                                   struct kvm_sw_breakpoint *bp);
165 int kvm_arch_remove_sw_breakpoint(CPUState *current_env,
166                                   struct kvm_sw_breakpoint *bp);
167 int kvm_arch_insert_hw_breakpoint(target_ulong addr,
168                                   target_ulong len, int type);
169 int kvm_arch_remove_hw_breakpoint(target_ulong addr,
170                                   target_ulong len, int type);
171 void kvm_arch_remove_all_hw_breakpoints(void);
172 
173 void kvm_arch_update_guest_debug(CPUState *env, struct kvm_guest_debug *dbg);
174 
175 bool kvm_arch_stop_on_emulation_error(CPUState *env);
176 
177 int kvm_check_extension(KVMState *s, unsigned int extension);
178 
179 uint32_t kvm_arch_get_supported_cpuid(CPUState *env, uint32_t function,
180                                       uint32_t index, int reg);
181 void kvm_cpu_synchronize_state(CPUState *env);
182 void kvm_cpu_synchronize_post_reset(CPUState *env);
183 void kvm_cpu_synchronize_post_init(CPUState *env);
184 
185 /* generic hooks - to be moved/refactored once there are more users */
186 
cpu_synchronize_state(CPUState * env)187 static inline void cpu_synchronize_state(CPUState *env)
188 {
189     if (kvm_enabled()) {
190         kvm_cpu_synchronize_state(env);
191     }
192 }
193 
cpu_synchronize_post_reset(CPUState * env)194 static inline void cpu_synchronize_post_reset(CPUState *env)
195 {
196     if (kvm_enabled()) {
197         kvm_cpu_synchronize_post_reset(env);
198     }
199 }
200 
cpu_synchronize_post_init(CPUState * env)201 static inline void cpu_synchronize_post_init(CPUState *env)
202 {
203     if (kvm_enabled()) {
204         kvm_cpu_synchronize_post_init(env);
205     }
206 }
207 
208 #if !defined(CONFIG_USER_ONLY)
209 int kvm_physical_memory_addr_from_ram(KVMState *s, ram_addr_t ram_addr,
210                                       target_phys_addr_t *phys_addr);
211 #endif
212 
213 #endif
214 int kvm_set_ioeventfd_mmio_long(int fd, uint32_t adr, uint32_t val, bool assign);
215 
216 #if defined(KVM_IRQFD) && defined(CONFIG_KVM)
217 int kvm_set_irqfd(int gsi, int fd, bool assigned);
218 #else
219 static inline
kvm_set_irqfd(int gsi,int fd,bool assigned)220 int kvm_set_irqfd(int gsi, int fd, bool assigned)
221 {
222     return -ENOSYS;
223 }
224 #endif
225 
226 int kvm_set_ioeventfd_pio_word(int fd, uint16_t adr, uint16_t val, bool assign);
227 
228 int kvm_has_gsi_routing(void);
229 int kvm_get_irq_route_gsi(void);
230 int kvm_add_msix(uint32_t gsi, uint32_t addr_lo,
231                  uint32_t addr_hi, uint32_t data);
232 int kvm_del_msix(uint32_t gsi, uint32_t addr_lo,
233                  uint32_t addr_hi, uint32_t data);
234 int kvm_update_msix(uint32_t old_gsi, uint32_t old_addr_lo,
235                     uint32_t old_addr_hi, uint32_t old_data,
236                     uint32_t new_gsi, uint32_t new_addr_lo,
237                     uint32_t new_addr_hi, uint32_t new_data);
238 int kvm_commit_irq_routes(void);
239 
240 int kvm_irqchip_in_kernel(void);
241 
242 int kvm_set_irq(int irq, int level, int *status);
243 
244 #endif
245