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