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