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