Commit | Line | Data |
---|---|---|
6aa8b732 AK |
1 | #ifndef __KVM_H |
2 | #define __KVM_H | |
3 | ||
4 | /* | |
5 | * This work is licensed under the terms of the GNU GPL, version 2. See | |
6 | * the COPYING file in the top-level directory. | |
7 | */ | |
8 | ||
9 | #include <linux/types.h> | |
e56a7a28 | 10 | #include <linux/hardirq.h> |
6aa8b732 AK |
11 | #include <linux/list.h> |
12 | #include <linux/mutex.h> | |
13 | #include <linux/spinlock.h> | |
06ff0d37 MR |
14 | #include <linux/signal.h> |
15 | #include <linux/sched.h> | |
6aa8b732 | 16 | #include <linux/mm.h> |
15ad7146 | 17 | #include <linux/preempt.h> |
e8edc6e0 | 18 | #include <asm/signal.h> |
6aa8b732 | 19 | |
6aa8b732 | 20 | #include <linux/kvm.h> |
102d8325 | 21 | #include <linux/kvm_para.h> |
6aa8b732 | 22 | |
d77a39d9 HB |
23 | #include "types.h" |
24 | ||
d657a98e ZX |
25 | #include "x86.h" |
26 | ||
ef9254df | 27 | #define KVM_MAX_VCPUS 4 |
e8207547 | 28 | #define KVM_ALIAS_SLOTS 4 |
2e2c618d | 29 | #define KVM_MEMORY_SLOTS 8 |
e0d62c7f IE |
30 | /* memory slots that does not exposed to userspace */ |
31 | #define KVM_PRIVATE_MEM_SLOTS 4 | |
6aa8b732 | 32 | |
039576c0 AK |
33 | #define KVM_PIO_PAGE_OFFSET 1 |
34 | ||
d9e368d6 AK |
35 | /* |
36 | * vcpu->requests bit members | |
37 | */ | |
3176bc3e | 38 | #define KVM_REQ_TLB_FLUSH 0 |
d9e368d6 | 39 | |
6aa8b732 | 40 | |
6aa8b732 | 41 | struct kvm_vcpu; |
c16f862d | 42 | extern struct kmem_cache *kvm_vcpu_cache; |
6aa8b732 | 43 | |
6aa8b732 AK |
44 | struct kvm_guest_debug { |
45 | int enabled; | |
46 | unsigned long bp[4]; | |
47 | int singlestep; | |
48 | }; | |
49 | ||
ba1389b7 | 50 | struct kvm_vcpu_stat { |
1165f5fe AK |
51 | u32 pf_fixed; |
52 | u32 pf_guest; | |
53 | u32 tlb_flush; | |
54 | u32 invlpg; | |
55 | ||
56 | u32 exits; | |
57 | u32 io_exits; | |
58 | u32 mmio_exits; | |
59 | u32 signal_exits; | |
60 | u32 irq_window_exits; | |
61 | u32 halt_exits; | |
b6958ce4 | 62 | u32 halt_wakeup; |
1165f5fe AK |
63 | u32 request_irq_exits; |
64 | u32 irq_exits; | |
e1beb1d3 | 65 | u32 host_state_reload; |
2cc51560 | 66 | u32 efer_reload; |
f096ed85 | 67 | u32 fpu_reload; |
f2b5756b AK |
68 | u32 insn_emulation; |
69 | u32 insn_emulation_fail; | |
1165f5fe AK |
70 | }; |
71 | ||
2eeb2e94 GH |
72 | /* |
73 | * It would be nice to use something smarter than a linear search, TBD... | |
74 | * Thankfully we dont expect many devices to register (famous last words :), | |
75 | * so until then it will suffice. At least its abstracted so we can change | |
76 | * in one place. | |
77 | */ | |
78 | struct kvm_io_bus { | |
79 | int dev_count; | |
80 | #define NR_IOBUS_DEVS 6 | |
81 | struct kvm_io_device *devs[NR_IOBUS_DEVS]; | |
82 | }; | |
83 | ||
84 | void kvm_io_bus_init(struct kvm_io_bus *bus); | |
85 | void kvm_io_bus_destroy(struct kvm_io_bus *bus); | |
86 | struct kvm_io_device *kvm_io_bus_find_dev(struct kvm_io_bus *bus, gpa_t addr); | |
87 | void kvm_io_bus_register_dev(struct kvm_io_bus *bus, | |
88 | struct kvm_io_device *dev); | |
89 | ||
d17fbbf7 ZX |
90 | struct kvm_vcpu { |
91 | struct kvm *kvm; | |
92 | struct preempt_notifier preempt_notifier; | |
93 | int vcpu_id; | |
94 | struct mutex mutex; | |
95 | int cpu; | |
96 | struct kvm_run *run; | |
97 | int guest_mode; | |
98 | unsigned long requests; | |
99 | struct kvm_guest_debug guest_debug; | |
100 | int fpu_active; | |
101 | int guest_fpu_loaded; | |
102 | wait_queue_head_t wq; | |
103 | int sigset_active; | |
104 | sigset_t sigset; | |
105 | struct kvm_vcpu_stat stat; | |
106 | ||
34c16eec | 107 | #ifdef CONFIG_HAS_IOMEM |
d17fbbf7 ZX |
108 | int mmio_needed; |
109 | int mmio_read_completed; | |
110 | int mmio_is_write; | |
111 | int mmio_size; | |
112 | unsigned char mmio_data[8]; | |
6aa8b732 | 113 | gpa_t mmio_phys_addr; |
34c16eec | 114 | #endif |
1165f5fe | 115 | |
d657a98e ZX |
116 | struct kvm_vcpu_arch arch; |
117 | }; | |
118 | ||
e8207547 AK |
119 | struct kvm_mem_alias { |
120 | gfn_t base_gfn; | |
121 | unsigned long npages; | |
122 | gfn_t target_gfn; | |
123 | }; | |
124 | ||
6aa8b732 AK |
125 | struct kvm_memory_slot { |
126 | gfn_t base_gfn; | |
127 | unsigned long npages; | |
128 | unsigned long flags; | |
290fc38d | 129 | unsigned long *rmap; |
6aa8b732 | 130 | unsigned long *dirty_bitmap; |
8a7ae055 | 131 | unsigned long userspace_addr; |
80b14b5b | 132 | int user_alloc; |
6aa8b732 AK |
133 | }; |
134 | ||
ba1389b7 | 135 | struct kvm_vm_stat { |
4cee5764 AK |
136 | u32 mmu_shadow_zapped; |
137 | u32 mmu_pte_write; | |
138 | u32 mmu_pte_updated; | |
139 | u32 mmu_pde_zapped; | |
140 | u32 mmu_flooded; | |
141 | u32 mmu_recycled; | |
0f74a24c | 142 | u32 remote_tlb_flush; |
ba1389b7 AK |
143 | }; |
144 | ||
6aa8b732 | 145 | struct kvm { |
11ec2804 | 146 | struct mutex lock; /* protects everything except vcpus */ |
6d4e4c4f | 147 | struct mm_struct *mm; /* userspace tied to this vm */ |
e8207547 AK |
148 | int naliases; |
149 | struct kvm_mem_alias aliases[KVM_ALIAS_SLOTS]; | |
6aa8b732 | 150 | int nmemslots; |
e0d62c7f IE |
151 | struct kvm_memory_slot memslots[KVM_MEMORY_SLOTS + |
152 | KVM_PRIVATE_MEM_SLOTS]; | |
cea0f0e7 AK |
153 | /* |
154 | * Hash table of struct kvm_mmu_page. | |
155 | */ | |
6aa8b732 | 156 | struct list_head active_mmu_pages; |
82ce2c96 IE |
157 | unsigned int n_free_mmu_pages; |
158 | unsigned int n_requested_mmu_pages; | |
159 | unsigned int n_alloc_mmu_pages; | |
cea0f0e7 | 160 | struct hlist_head mmu_page_hash[KVM_NUM_MMU_PAGES]; |
fb3f0f51 | 161 | struct kvm_vcpu *vcpus[KVM_MAX_VCPUS]; |
133de902 | 162 | struct list_head vm_list; |
bccf2150 | 163 | struct file *filp; |
2eeb2e94 | 164 | struct kvm_io_bus mmio_bus; |
74906345 | 165 | struct kvm_io_bus pio_bus; |
85f455f7 | 166 | struct kvm_pic *vpic; |
1fd4f2a5 | 167 | struct kvm_ioapic *vioapic; |
932f72ad | 168 | int round_robin_prev_vcpu; |
cbc94022 | 169 | unsigned int tss_addr; |
f78e0e2e | 170 | struct page *apic_access_page; |
ba1389b7 | 171 | struct kvm_vm_stat stat; |
6aa8b732 AK |
172 | }; |
173 | ||
f0242478 RR |
174 | /* The guest did something we don't support. */ |
175 | #define pr_unimpl(vcpu, fmt, ...) \ | |
176 | do { \ | |
177 | if (printk_ratelimit()) \ | |
178 | printk(KERN_ERR "kvm: %i: cpu%i " fmt, \ | |
179 | current->tgid, (vcpu)->vcpu_id , ## __VA_ARGS__); \ | |
d77c26fc | 180 | } while (0) |
f0242478 | 181 | |
6aa8b732 AK |
182 | #define kvm_printf(kvm, fmt ...) printk(KERN_DEBUG fmt) |
183 | #define vcpu_printf(vcpu, fmt...) kvm_printf(vcpu->kvm, fmt) | |
184 | ||
fb3f0f51 RR |
185 | int kvm_vcpu_init(struct kvm_vcpu *vcpu, struct kvm *kvm, unsigned id); |
186 | void kvm_vcpu_uninit(struct kvm_vcpu *vcpu); | |
187 | ||
313a3dc7 CO |
188 | void vcpu_load(struct kvm_vcpu *vcpu); |
189 | void vcpu_put(struct kvm_vcpu *vcpu); | |
190 | ||
e9b11c17 ZX |
191 | void decache_vcpus_on_cpu(int cpu); |
192 | ||
313a3dc7 | 193 | |
f8c16bba | 194 | int kvm_init(void *opaque, unsigned int vcpu_size, |
c16f862d | 195 | struct module *module); |
cb498ea2 | 196 | void kvm_exit(void); |
6aa8b732 | 197 | |
6aa8b732 AK |
198 | #define HPA_MSB ((sizeof(hpa_t) * 8) - 1) |
199 | #define HPA_ERR_MASK ((hpa_t)1 << HPA_MSB) | |
200 | static inline int is_error_hpa(hpa_t hpa) { return hpa >> HPA_MSB; } | |
039576c0 | 201 | struct page *gva_to_page(struct kvm_vcpu *vcpu, gva_t gva); |
6aa8b732 | 202 | |
cea7bb21 | 203 | extern struct page *bad_page; |
6aa8b732 | 204 | |
cea7bb21 | 205 | int is_error_page(struct page *page); |
f9d46eb0 | 206 | int kvm_is_error_hva(unsigned long addr); |
210c7c4d IE |
207 | int kvm_set_memory_region(struct kvm *kvm, |
208 | struct kvm_userspace_memory_region *mem, | |
209 | int user_alloc); | |
f78e0e2e SY |
210 | int __kvm_set_memory_region(struct kvm *kvm, |
211 | struct kvm_userspace_memory_region *mem, | |
212 | int user_alloc); | |
0de10343 ZX |
213 | int kvm_arch_set_memory_region(struct kvm *kvm, |
214 | struct kvm_userspace_memory_region *mem, | |
215 | struct kvm_memory_slot old, | |
216 | int user_alloc); | |
290fc38d | 217 | gfn_t unalias_gfn(struct kvm *kvm, gfn_t gfn); |
954bbbc2 | 218 | struct page *gfn_to_page(struct kvm *kvm, gfn_t gfn); |
b4231d61 IE |
219 | void kvm_release_page_clean(struct page *page); |
220 | void kvm_release_page_dirty(struct page *page); | |
195aefde IE |
221 | int kvm_read_guest_page(struct kvm *kvm, gfn_t gfn, void *data, int offset, |
222 | int len); | |
223 | int kvm_read_guest(struct kvm *kvm, gpa_t gpa, void *data, unsigned long len); | |
224 | int kvm_write_guest_page(struct kvm *kvm, gfn_t gfn, const void *data, | |
225 | int offset, int len); | |
226 | int kvm_write_guest(struct kvm *kvm, gpa_t gpa, const void *data, | |
227 | unsigned long len); | |
228 | int kvm_clear_guest_page(struct kvm *kvm, gfn_t gfn, int offset, int len); | |
229 | int kvm_clear_guest(struct kvm *kvm, gpa_t gpa, unsigned long len); | |
6aa8b732 | 230 | struct kvm_memory_slot *gfn_to_memslot(struct kvm *kvm, gfn_t gfn); |
e0d62c7f | 231 | int kvm_is_visible_gfn(struct kvm *kvm, gfn_t gfn); |
6aa8b732 AK |
232 | void mark_page_dirty(struct kvm *kvm, gfn_t gfn); |
233 | ||
8776e519 | 234 | void kvm_vcpu_block(struct kvm_vcpu *vcpu); |
6aa8b732 | 235 | void kvm_resched(struct kvm_vcpu *vcpu); |
7702fd1f AK |
236 | void kvm_load_guest_fpu(struct kvm_vcpu *vcpu); |
237 | void kvm_put_guest_fpu(struct kvm_vcpu *vcpu); | |
d9e368d6 | 238 | void kvm_flush_remote_tlbs(struct kvm *kvm); |
6aa8b732 | 239 | |
043405e1 CO |
240 | long kvm_arch_dev_ioctl(struct file *filp, |
241 | unsigned int ioctl, unsigned long arg); | |
313a3dc7 CO |
242 | long kvm_arch_vcpu_ioctl(struct file *filp, |
243 | unsigned int ioctl, unsigned long arg); | |
244 | void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu); | |
245 | void kvm_arch_vcpu_put(struct kvm_vcpu *vcpu); | |
018d00d2 ZX |
246 | |
247 | int kvm_dev_ioctl_check_extension(long ext); | |
248 | ||
5bb064dc ZX |
249 | int kvm_get_dirty_log(struct kvm *kvm, |
250 | struct kvm_dirty_log *log, int *is_dirty); | |
251 | int kvm_vm_ioctl_get_dirty_log(struct kvm *kvm, | |
252 | struct kvm_dirty_log *log); | |
253 | ||
1fe779f8 CO |
254 | int kvm_vm_ioctl_set_memory_region(struct kvm *kvm, |
255 | struct | |
256 | kvm_userspace_memory_region *mem, | |
257 | int user_alloc); | |
258 | long kvm_arch_vm_ioctl(struct file *filp, | |
259 | unsigned int ioctl, unsigned long arg); | |
260 | void kvm_arch_destroy_vm(struct kvm *kvm); | |
313a3dc7 | 261 | |
d0752060 HB |
262 | int kvm_arch_vcpu_ioctl_get_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu); |
263 | int kvm_arch_vcpu_ioctl_set_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu); | |
264 | ||
8b006791 ZX |
265 | int kvm_arch_vcpu_ioctl_translate(struct kvm_vcpu *vcpu, |
266 | struct kvm_translation *tr); | |
267 | ||
b6c7a5dc HB |
268 | int kvm_arch_vcpu_ioctl_get_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs); |
269 | int kvm_arch_vcpu_ioctl_set_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs); | |
270 | int kvm_arch_vcpu_ioctl_get_sregs(struct kvm_vcpu *vcpu, | |
271 | struct kvm_sregs *sregs); | |
272 | int kvm_arch_vcpu_ioctl_set_sregs(struct kvm_vcpu *vcpu, | |
273 | struct kvm_sregs *sregs); | |
274 | int kvm_arch_vcpu_ioctl_debug_guest(struct kvm_vcpu *vcpu, | |
275 | struct kvm_debug_guest *dbg); | |
276 | int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run); | |
277 | ||
f8c16bba ZX |
278 | int kvm_arch_init(void *opaque); |
279 | void kvm_arch_exit(void); | |
043405e1 | 280 | |
e9b11c17 ZX |
281 | int kvm_arch_vcpu_init(struct kvm_vcpu *vcpu); |
282 | void kvm_arch_vcpu_uninit(struct kvm_vcpu *vcpu); | |
283 | ||
284 | void kvm_arch_vcpu_free(struct kvm_vcpu *vcpu); | |
285 | void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu); | |
286 | void kvm_arch_vcpu_put(struct kvm_vcpu *vcpu); | |
287 | struct kvm_vcpu *kvm_arch_vcpu_create(struct kvm *kvm, unsigned int id); | |
26e5215f | 288 | int kvm_arch_vcpu_setup(struct kvm_vcpu *vcpu); |
d40ccc62 | 289 | void kvm_arch_vcpu_destroy(struct kvm_vcpu *vcpu); |
e9b11c17 ZX |
290 | |
291 | int kvm_arch_vcpu_reset(struct kvm_vcpu *vcpu); | |
292 | void kvm_arch_hardware_enable(void *garbage); | |
293 | void kvm_arch_hardware_disable(void *garbage); | |
294 | int kvm_arch_hardware_setup(void); | |
295 | void kvm_arch_hardware_unsetup(void); | |
296 | void kvm_arch_check_processor_compat(void *rtn); | |
1d737c8a | 297 | int kvm_arch_vcpu_runnable(struct kvm_vcpu *vcpu); |
e9b11c17 | 298 | |
d19a9cd2 ZX |
299 | void kvm_free_physmem(struct kvm *kvm); |
300 | ||
301 | struct kvm *kvm_arch_create_vm(void); | |
302 | void kvm_arch_destroy_vm(struct kvm *kvm); | |
e9b11c17 | 303 | |
682c59a3 ZX |
304 | int kvm_cpu_get_interrupt(struct kvm_vcpu *v); |
305 | int kvm_cpu_has_interrupt(struct kvm_vcpu *v); | |
306 | ||
d172fcd3 LV |
307 | static inline void kvm_guest_enter(void) |
308 | { | |
e56a7a28 | 309 | account_system_vtime(current); |
d172fcd3 LV |
310 | current->flags |= PF_VCPU; |
311 | } | |
312 | ||
313 | static inline void kvm_guest_exit(void) | |
314 | { | |
e56a7a28 | 315 | account_system_vtime(current); |
d172fcd3 LV |
316 | current->flags &= ~PF_VCPU; |
317 | } | |
318 | ||
6aa8b732 AK |
319 | static inline int memslot_id(struct kvm *kvm, struct kvm_memory_slot *slot) |
320 | { | |
321 | return slot - kvm->memslots; | |
322 | } | |
323 | ||
1755fbcc AK |
324 | static inline gpa_t gfn_to_gpa(gfn_t gfn) |
325 | { | |
326 | return (gpa_t)gfn << PAGE_SHIFT; | |
327 | } | |
6aa8b732 | 328 | |
ba1389b7 AK |
329 | enum kvm_stat_kind { |
330 | KVM_STAT_VM, | |
331 | KVM_STAT_VCPU, | |
332 | }; | |
333 | ||
417bc304 HB |
334 | struct kvm_stats_debugfs_item { |
335 | const char *name; | |
336 | int offset; | |
ba1389b7 | 337 | enum kvm_stat_kind kind; |
417bc304 HB |
338 | struct dentry *dentry; |
339 | }; | |
340 | extern struct kvm_stats_debugfs_item debugfs_entries[]; | |
341 | ||
6aa8b732 | 342 | #endif |