Commit | Line | Data |
---|---|---|
6aa8b732 AK |
1 | /* |
2 | * Kernel-based Virtual Machine driver for Linux | |
3 | * | |
4 | * This module enables machines with Intel VT-x extensions to run virtual | |
5 | * machines without emulation or binary translation. | |
6 | * | |
7 | * Copyright (C) 2006 Qumranet, Inc. | |
8 | * | |
9 | * Authors: | |
10 | * Avi Kivity <avi@qumranet.com> | |
11 | * Yaniv Kamay <yaniv@qumranet.com> | |
12 | * | |
13 | * This work is licensed under the terms of the GNU GPL, version 2. See | |
14 | * the COPYING file in the top-level directory. | |
15 | * | |
16 | */ | |
17 | ||
18 | #include "kvm.h" | |
e495606d AK |
19 | #include "x86_emulate.h" |
20 | #include "segment_descriptor.h" | |
85f455f7 | 21 | #include "irq.h" |
6aa8b732 AK |
22 | |
23 | #include <linux/kvm.h> | |
24 | #include <linux/module.h> | |
25 | #include <linux/errno.h> | |
6aa8b732 AK |
26 | #include <linux/percpu.h> |
27 | #include <linux/gfp.h> | |
6aa8b732 AK |
28 | #include <linux/mm.h> |
29 | #include <linux/miscdevice.h> | |
30 | #include <linux/vmalloc.h> | |
6aa8b732 | 31 | #include <linux/reboot.h> |
6aa8b732 AK |
32 | #include <linux/debugfs.h> |
33 | #include <linux/highmem.h> | |
34 | #include <linux/file.h> | |
59ae6c6b | 35 | #include <linux/sysdev.h> |
774c47f1 | 36 | #include <linux/cpu.h> |
e8edc6e0 | 37 | #include <linux/sched.h> |
d9e368d6 AK |
38 | #include <linux/cpumask.h> |
39 | #include <linux/smp.h> | |
d6d28168 | 40 | #include <linux/anon_inodes.h> |
04d2cc77 | 41 | #include <linux/profile.h> |
6aa8b732 | 42 | |
e495606d AK |
43 | #include <asm/processor.h> |
44 | #include <asm/msr.h> | |
45 | #include <asm/io.h> | |
46 | #include <asm/uaccess.h> | |
47 | #include <asm/desc.h> | |
6aa8b732 AK |
48 | |
49 | MODULE_AUTHOR("Qumranet"); | |
50 | MODULE_LICENSE("GPL"); | |
51 | ||
133de902 AK |
52 | static DEFINE_SPINLOCK(kvm_lock); |
53 | static LIST_HEAD(vm_list); | |
54 | ||
1b6c0168 AK |
55 | static cpumask_t cpus_hardware_enabled; |
56 | ||
cbdd1bea | 57 | struct kvm_x86_ops *kvm_x86_ops; |
c16f862d RR |
58 | struct kmem_cache *kvm_vcpu_cache; |
59 | EXPORT_SYMBOL_GPL(kvm_vcpu_cache); | |
1165f5fe | 60 | |
15ad7146 AK |
61 | static __read_mostly struct preempt_ops kvm_preempt_ops; |
62 | ||
1165f5fe | 63 | #define STAT_OFFSET(x) offsetof(struct kvm_vcpu, stat.x) |
6aa8b732 AK |
64 | |
65 | static struct kvm_stats_debugfs_item { | |
66 | const char *name; | |
1165f5fe | 67 | int offset; |
6aa8b732 AK |
68 | struct dentry *dentry; |
69 | } debugfs_entries[] = { | |
1165f5fe AK |
70 | { "pf_fixed", STAT_OFFSET(pf_fixed) }, |
71 | { "pf_guest", STAT_OFFSET(pf_guest) }, | |
72 | { "tlb_flush", STAT_OFFSET(tlb_flush) }, | |
73 | { "invlpg", STAT_OFFSET(invlpg) }, | |
74 | { "exits", STAT_OFFSET(exits) }, | |
75 | { "io_exits", STAT_OFFSET(io_exits) }, | |
76 | { "mmio_exits", STAT_OFFSET(mmio_exits) }, | |
77 | { "signal_exits", STAT_OFFSET(signal_exits) }, | |
78 | { "irq_window", STAT_OFFSET(irq_window_exits) }, | |
79 | { "halt_exits", STAT_OFFSET(halt_exits) }, | |
b6958ce4 | 80 | { "halt_wakeup", STAT_OFFSET(halt_wakeup) }, |
1165f5fe AK |
81 | { "request_irq", STAT_OFFSET(request_irq_exits) }, |
82 | { "irq_exits", STAT_OFFSET(irq_exits) }, | |
e6adf283 | 83 | { "light_exits", STAT_OFFSET(light_exits) }, |
2cc51560 | 84 | { "efer_reload", STAT_OFFSET(efer_reload) }, |
1165f5fe | 85 | { NULL } |
6aa8b732 AK |
86 | }; |
87 | ||
88 | static struct dentry *debugfs_dir; | |
89 | ||
90 | #define MAX_IO_MSRS 256 | |
91 | ||
707d92fa RR |
92 | #define CR0_RESERVED_BITS \ |
93 | (~(unsigned long)(X86_CR0_PE | X86_CR0_MP | X86_CR0_EM | X86_CR0_TS \ | |
94 | | X86_CR0_ET | X86_CR0_NE | X86_CR0_WP | X86_CR0_AM \ | |
95 | | X86_CR0_NW | X86_CR0_CD | X86_CR0_PG)) | |
66aee91a RR |
96 | #define CR4_RESERVED_BITS \ |
97 | (~(unsigned long)(X86_CR4_VME | X86_CR4_PVI | X86_CR4_TSD | X86_CR4_DE\ | |
98 | | X86_CR4_PSE | X86_CR4_PAE | X86_CR4_MCE \ | |
99 | | X86_CR4_PGE | X86_CR4_PCE | X86_CR4_OSFXSR \ | |
100 | | X86_CR4_OSXMMEXCPT | X86_CR4_VMXE)) | |
101 | ||
7075bc81 | 102 | #define CR8_RESERVED_BITS (~(unsigned long)X86_CR8_TPR) |
6aa8b732 AK |
103 | #define EFER_RESERVED_BITS 0xfffffffffffff2fe |
104 | ||
05b3e0c2 | 105 | #ifdef CONFIG_X86_64 |
6aa8b732 AK |
106 | // LDT or TSS descriptor in the GDT. 16 bytes. |
107 | struct segment_descriptor_64 { | |
108 | struct segment_descriptor s; | |
109 | u32 base_higher; | |
110 | u32 pad_zero; | |
111 | }; | |
112 | ||
113 | #endif | |
114 | ||
bccf2150 AK |
115 | static long kvm_vcpu_ioctl(struct file *file, unsigned int ioctl, |
116 | unsigned long arg); | |
117 | ||
6aa8b732 AK |
118 | unsigned long segment_base(u16 selector) |
119 | { | |
120 | struct descriptor_table gdt; | |
121 | struct segment_descriptor *d; | |
122 | unsigned long table_base; | |
123 | typedef unsigned long ul; | |
124 | unsigned long v; | |
125 | ||
126 | if (selector == 0) | |
127 | return 0; | |
128 | ||
129 | asm ("sgdt %0" : "=m"(gdt)); | |
130 | table_base = gdt.base; | |
131 | ||
132 | if (selector & 4) { /* from ldt */ | |
133 | u16 ldt_selector; | |
134 | ||
135 | asm ("sldt %0" : "=g"(ldt_selector)); | |
136 | table_base = segment_base(ldt_selector); | |
137 | } | |
138 | d = (struct segment_descriptor *)(table_base + (selector & ~7)); | |
139 | v = d->base_low | ((ul)d->base_mid << 16) | ((ul)d->base_high << 24); | |
05b3e0c2 | 140 | #ifdef CONFIG_X86_64 |
6aa8b732 AK |
141 | if (d->system == 0 |
142 | && (d->type == 2 || d->type == 9 || d->type == 11)) | |
143 | v |= ((ul)((struct segment_descriptor_64 *)d)->base_higher) << 32; | |
144 | #endif | |
145 | return v; | |
146 | } | |
147 | EXPORT_SYMBOL_GPL(segment_base); | |
148 | ||
5aacf0ca JM |
149 | static inline int valid_vcpu(int n) |
150 | { | |
151 | return likely(n >= 0 && n < KVM_MAX_VCPUS); | |
152 | } | |
153 | ||
7702fd1f AK |
154 | void kvm_load_guest_fpu(struct kvm_vcpu *vcpu) |
155 | { | |
156 | if (!vcpu->fpu_active || vcpu->guest_fpu_loaded) | |
157 | return; | |
158 | ||
159 | vcpu->guest_fpu_loaded = 1; | |
b114b080 RR |
160 | fx_save(&vcpu->host_fx_image); |
161 | fx_restore(&vcpu->guest_fx_image); | |
7702fd1f AK |
162 | } |
163 | EXPORT_SYMBOL_GPL(kvm_load_guest_fpu); | |
164 | ||
165 | void kvm_put_guest_fpu(struct kvm_vcpu *vcpu) | |
166 | { | |
167 | if (!vcpu->guest_fpu_loaded) | |
168 | return; | |
169 | ||
170 | vcpu->guest_fpu_loaded = 0; | |
b114b080 RR |
171 | fx_save(&vcpu->guest_fx_image); |
172 | fx_restore(&vcpu->host_fx_image); | |
7702fd1f AK |
173 | } |
174 | EXPORT_SYMBOL_GPL(kvm_put_guest_fpu); | |
175 | ||
bccf2150 AK |
176 | /* |
177 | * Switches to specified vcpu, until a matching vcpu_put() | |
178 | */ | |
179 | static void vcpu_load(struct kvm_vcpu *vcpu) | |
6aa8b732 | 180 | { |
15ad7146 AK |
181 | int cpu; |
182 | ||
bccf2150 | 183 | mutex_lock(&vcpu->mutex); |
15ad7146 AK |
184 | cpu = get_cpu(); |
185 | preempt_notifier_register(&vcpu->preempt_notifier); | |
cbdd1bea | 186 | kvm_x86_ops->vcpu_load(vcpu, cpu); |
15ad7146 | 187 | put_cpu(); |
6aa8b732 AK |
188 | } |
189 | ||
6aa8b732 AK |
190 | static void vcpu_put(struct kvm_vcpu *vcpu) |
191 | { | |
15ad7146 | 192 | preempt_disable(); |
cbdd1bea | 193 | kvm_x86_ops->vcpu_put(vcpu); |
15ad7146 AK |
194 | preempt_notifier_unregister(&vcpu->preempt_notifier); |
195 | preempt_enable(); | |
6aa8b732 AK |
196 | mutex_unlock(&vcpu->mutex); |
197 | } | |
198 | ||
d9e368d6 AK |
199 | static void ack_flush(void *_completed) |
200 | { | |
201 | atomic_t *completed = _completed; | |
202 | ||
203 | atomic_inc(completed); | |
204 | } | |
205 | ||
206 | void kvm_flush_remote_tlbs(struct kvm *kvm) | |
207 | { | |
208 | int i, cpu, needed; | |
209 | cpumask_t cpus; | |
210 | struct kvm_vcpu *vcpu; | |
211 | atomic_t completed; | |
212 | ||
213 | atomic_set(&completed, 0); | |
214 | cpus_clear(cpus); | |
215 | needed = 0; | |
fb3f0f51 RR |
216 | for (i = 0; i < KVM_MAX_VCPUS; ++i) { |
217 | vcpu = kvm->vcpus[i]; | |
218 | if (!vcpu) | |
219 | continue; | |
d9e368d6 AK |
220 | if (test_and_set_bit(KVM_TLB_FLUSH, &vcpu->requests)) |
221 | continue; | |
222 | cpu = vcpu->cpu; | |
223 | if (cpu != -1 && cpu != raw_smp_processor_id()) | |
224 | if (!cpu_isset(cpu, cpus)) { | |
225 | cpu_set(cpu, cpus); | |
226 | ++needed; | |
227 | } | |
228 | } | |
229 | ||
230 | /* | |
231 | * We really want smp_call_function_mask() here. But that's not | |
232 | * available, so ipi all cpus in parallel and wait for them | |
233 | * to complete. | |
234 | */ | |
235 | for (cpu = first_cpu(cpus); cpu != NR_CPUS; cpu = next_cpu(cpu, cpus)) | |
236 | smp_call_function_single(cpu, ack_flush, &completed, 1, 0); | |
237 | while (atomic_read(&completed) != needed) { | |
238 | cpu_relax(); | |
239 | barrier(); | |
240 | } | |
241 | } | |
242 | ||
fb3f0f51 RR |
243 | int kvm_vcpu_init(struct kvm_vcpu *vcpu, struct kvm *kvm, unsigned id) |
244 | { | |
245 | struct page *page; | |
246 | int r; | |
247 | ||
248 | mutex_init(&vcpu->mutex); | |
249 | vcpu->cpu = -1; | |
250 | vcpu->mmu.root_hpa = INVALID_PAGE; | |
251 | vcpu->kvm = kvm; | |
252 | vcpu->vcpu_id = id; | |
c5ec1534 HQ |
253 | if (!irqchip_in_kernel(kvm) || id == 0) |
254 | vcpu->mp_state = VCPU_MP_STATE_RUNNABLE; | |
255 | else | |
256 | vcpu->mp_state = VCPU_MP_STATE_UNINITIALIZED; | |
b6958ce4 | 257 | init_waitqueue_head(&vcpu->wq); |
fb3f0f51 RR |
258 | |
259 | page = alloc_page(GFP_KERNEL | __GFP_ZERO); | |
260 | if (!page) { | |
261 | r = -ENOMEM; | |
262 | goto fail; | |
263 | } | |
264 | vcpu->run = page_address(page); | |
265 | ||
266 | page = alloc_page(GFP_KERNEL | __GFP_ZERO); | |
267 | if (!page) { | |
268 | r = -ENOMEM; | |
269 | goto fail_free_run; | |
270 | } | |
271 | vcpu->pio_data = page_address(page); | |
272 | ||
fb3f0f51 RR |
273 | r = kvm_mmu_create(vcpu); |
274 | if (r < 0) | |
275 | goto fail_free_pio_data; | |
276 | ||
277 | return 0; | |
278 | ||
279 | fail_free_pio_data: | |
280 | free_page((unsigned long)vcpu->pio_data); | |
281 | fail_free_run: | |
282 | free_page((unsigned long)vcpu->run); | |
283 | fail: | |
284 | return -ENOMEM; | |
285 | } | |
286 | EXPORT_SYMBOL_GPL(kvm_vcpu_init); | |
287 | ||
288 | void kvm_vcpu_uninit(struct kvm_vcpu *vcpu) | |
289 | { | |
290 | kvm_mmu_destroy(vcpu); | |
1b9778da ED |
291 | if (vcpu->apic) |
292 | hrtimer_cancel(&vcpu->apic->timer.dev); | |
97222cc8 | 293 | kvm_free_apic(vcpu->apic); |
fb3f0f51 RR |
294 | free_page((unsigned long)vcpu->pio_data); |
295 | free_page((unsigned long)vcpu->run); | |
296 | } | |
297 | EXPORT_SYMBOL_GPL(kvm_vcpu_uninit); | |
298 | ||
f17abe9a | 299 | static struct kvm *kvm_create_vm(void) |
6aa8b732 AK |
300 | { |
301 | struct kvm *kvm = kzalloc(sizeof(struct kvm), GFP_KERNEL); | |
6aa8b732 AK |
302 | |
303 | if (!kvm) | |
f17abe9a | 304 | return ERR_PTR(-ENOMEM); |
6aa8b732 | 305 | |
74906345 | 306 | kvm_io_bus_init(&kvm->pio_bus); |
11ec2804 | 307 | mutex_init(&kvm->lock); |
6aa8b732 | 308 | INIT_LIST_HEAD(&kvm->active_mmu_pages); |
2eeb2e94 | 309 | kvm_io_bus_init(&kvm->mmio_bus); |
5e58cfe4 RR |
310 | spin_lock(&kvm_lock); |
311 | list_add(&kvm->vm_list, &vm_list); | |
312 | spin_unlock(&kvm_lock); | |
f17abe9a AK |
313 | return kvm; |
314 | } | |
315 | ||
6aa8b732 AK |
316 | /* |
317 | * Free any memory in @free but not in @dont. | |
318 | */ | |
319 | static void kvm_free_physmem_slot(struct kvm_memory_slot *free, | |
320 | struct kvm_memory_slot *dont) | |
321 | { | |
322 | int i; | |
323 | ||
324 | if (!dont || free->phys_mem != dont->phys_mem) | |
325 | if (free->phys_mem) { | |
326 | for (i = 0; i < free->npages; ++i) | |
55a54f79 AK |
327 | if (free->phys_mem[i]) |
328 | __free_page(free->phys_mem[i]); | |
6aa8b732 AK |
329 | vfree(free->phys_mem); |
330 | } | |
331 | ||
332 | if (!dont || free->dirty_bitmap != dont->dirty_bitmap) | |
333 | vfree(free->dirty_bitmap); | |
334 | ||
8b6d44c7 | 335 | free->phys_mem = NULL; |
6aa8b732 | 336 | free->npages = 0; |
8b6d44c7 | 337 | free->dirty_bitmap = NULL; |
6aa8b732 AK |
338 | } |
339 | ||
340 | static void kvm_free_physmem(struct kvm *kvm) | |
341 | { | |
342 | int i; | |
343 | ||
344 | for (i = 0; i < kvm->nmemslots; ++i) | |
8b6d44c7 | 345 | kvm_free_physmem_slot(&kvm->memslots[i], NULL); |
6aa8b732 AK |
346 | } |
347 | ||
039576c0 AK |
348 | static void free_pio_guest_pages(struct kvm_vcpu *vcpu) |
349 | { | |
350 | int i; | |
351 | ||
3077c451 | 352 | for (i = 0; i < ARRAY_SIZE(vcpu->pio.guest_pages); ++i) |
039576c0 AK |
353 | if (vcpu->pio.guest_pages[i]) { |
354 | __free_page(vcpu->pio.guest_pages[i]); | |
355 | vcpu->pio.guest_pages[i] = NULL; | |
356 | } | |
357 | } | |
358 | ||
7b53aa56 AK |
359 | static void kvm_unload_vcpu_mmu(struct kvm_vcpu *vcpu) |
360 | { | |
7b53aa56 AK |
361 | vcpu_load(vcpu); |
362 | kvm_mmu_unload(vcpu); | |
363 | vcpu_put(vcpu); | |
364 | } | |
365 | ||
6aa8b732 AK |
366 | static void kvm_free_vcpus(struct kvm *kvm) |
367 | { | |
368 | unsigned int i; | |
369 | ||
7b53aa56 AK |
370 | /* |
371 | * Unpin any mmu pages first. | |
372 | */ | |
373 | for (i = 0; i < KVM_MAX_VCPUS; ++i) | |
fb3f0f51 RR |
374 | if (kvm->vcpus[i]) |
375 | kvm_unload_vcpu_mmu(kvm->vcpus[i]); | |
376 | for (i = 0; i < KVM_MAX_VCPUS; ++i) { | |
377 | if (kvm->vcpus[i]) { | |
cbdd1bea | 378 | kvm_x86_ops->vcpu_free(kvm->vcpus[i]); |
fb3f0f51 RR |
379 | kvm->vcpus[i] = NULL; |
380 | } | |
381 | } | |
382 | ||
6aa8b732 AK |
383 | } |
384 | ||
f17abe9a AK |
385 | static void kvm_destroy_vm(struct kvm *kvm) |
386 | { | |
133de902 AK |
387 | spin_lock(&kvm_lock); |
388 | list_del(&kvm->vm_list); | |
389 | spin_unlock(&kvm_lock); | |
74906345 | 390 | kvm_io_bus_destroy(&kvm->pio_bus); |
2eeb2e94 | 391 | kvm_io_bus_destroy(&kvm->mmio_bus); |
85f455f7 | 392 | kfree(kvm->vpic); |
1fd4f2a5 | 393 | kfree(kvm->vioapic); |
6aa8b732 AK |
394 | kvm_free_vcpus(kvm); |
395 | kvm_free_physmem(kvm); | |
396 | kfree(kvm); | |
f17abe9a AK |
397 | } |
398 | ||
399 | static int kvm_vm_release(struct inode *inode, struct file *filp) | |
400 | { | |
401 | struct kvm *kvm = filp->private_data; | |
402 | ||
403 | kvm_destroy_vm(kvm); | |
6aa8b732 AK |
404 | return 0; |
405 | } | |
406 | ||
407 | static void inject_gp(struct kvm_vcpu *vcpu) | |
408 | { | |
cbdd1bea | 409 | kvm_x86_ops->inject_gp(vcpu, 0); |
6aa8b732 AK |
410 | } |
411 | ||
1342d353 AK |
412 | /* |
413 | * Load the pae pdptrs. Return true is they are all valid. | |
414 | */ | |
415 | static int load_pdptrs(struct kvm_vcpu *vcpu, unsigned long cr3) | |
6aa8b732 AK |
416 | { |
417 | gfn_t pdpt_gfn = cr3 >> PAGE_SHIFT; | |
1342d353 | 418 | unsigned offset = ((cr3 & (PAGE_SIZE-1)) >> 5) << 2; |
6aa8b732 | 419 | int i; |
6aa8b732 | 420 | u64 *pdpt; |
1342d353 | 421 | int ret; |
954bbbc2 | 422 | struct page *page; |
c820c2aa | 423 | u64 pdpte[ARRAY_SIZE(vcpu->pdptrs)]; |
6aa8b732 | 424 | |
11ec2804 | 425 | mutex_lock(&vcpu->kvm->lock); |
954bbbc2 | 426 | page = gfn_to_page(vcpu->kvm, pdpt_gfn); |
c820c2aa RR |
427 | if (!page) { |
428 | ret = 0; | |
429 | goto out; | |
430 | } | |
431 | ||
954bbbc2 | 432 | pdpt = kmap_atomic(page, KM_USER0); |
c820c2aa RR |
433 | memcpy(pdpte, pdpt+offset, sizeof(pdpte)); |
434 | kunmap_atomic(pdpt, KM_USER0); | |
6aa8b732 | 435 | |
c820c2aa RR |
436 | for (i = 0; i < ARRAY_SIZE(pdpte); ++i) { |
437 | if ((pdpte[i] & 1) && (pdpte[i] & 0xfffffff0000001e6ull)) { | |
1342d353 AK |
438 | ret = 0; |
439 | goto out; | |
440 | } | |
6aa8b732 | 441 | } |
c820c2aa | 442 | ret = 1; |
6aa8b732 | 443 | |
c820c2aa | 444 | memcpy(vcpu->pdptrs, pdpte, sizeof(vcpu->pdptrs)); |
1342d353 | 445 | out: |
11ec2804 | 446 | mutex_unlock(&vcpu->kvm->lock); |
6aa8b732 | 447 | |
1342d353 | 448 | return ret; |
6aa8b732 AK |
449 | } |
450 | ||
451 | void set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0) | |
452 | { | |
707d92fa | 453 | if (cr0 & CR0_RESERVED_BITS) { |
6aa8b732 AK |
454 | printk(KERN_DEBUG "set_cr0: 0x%lx #GP, reserved bits 0x%lx\n", |
455 | cr0, vcpu->cr0); | |
456 | inject_gp(vcpu); | |
457 | return; | |
458 | } | |
459 | ||
707d92fa | 460 | if ((cr0 & X86_CR0_NW) && !(cr0 & X86_CR0_CD)) { |
6aa8b732 AK |
461 | printk(KERN_DEBUG "set_cr0: #GP, CD == 0 && NW == 1\n"); |
462 | inject_gp(vcpu); | |
463 | return; | |
464 | } | |
465 | ||
707d92fa | 466 | if ((cr0 & X86_CR0_PG) && !(cr0 & X86_CR0_PE)) { |
6aa8b732 AK |
467 | printk(KERN_DEBUG "set_cr0: #GP, set PG flag " |
468 | "and a clear PE flag\n"); | |
469 | inject_gp(vcpu); | |
470 | return; | |
471 | } | |
472 | ||
707d92fa | 473 | if (!is_paging(vcpu) && (cr0 & X86_CR0_PG)) { |
05b3e0c2 | 474 | #ifdef CONFIG_X86_64 |
6aa8b732 AK |
475 | if ((vcpu->shadow_efer & EFER_LME)) { |
476 | int cs_db, cs_l; | |
477 | ||
478 | if (!is_pae(vcpu)) { | |
479 | printk(KERN_DEBUG "set_cr0: #GP, start paging " | |
480 | "in long mode while PAE is disabled\n"); | |
481 | inject_gp(vcpu); | |
482 | return; | |
483 | } | |
cbdd1bea | 484 | kvm_x86_ops->get_cs_db_l_bits(vcpu, &cs_db, &cs_l); |
6aa8b732 AK |
485 | if (cs_l) { |
486 | printk(KERN_DEBUG "set_cr0: #GP, start paging " | |
487 | "in long mode while CS.L == 1\n"); | |
488 | inject_gp(vcpu); | |
489 | return; | |
490 | ||
491 | } | |
492 | } else | |
493 | #endif | |
1342d353 | 494 | if (is_pae(vcpu) && !load_pdptrs(vcpu, vcpu->cr3)) { |
6aa8b732 AK |
495 | printk(KERN_DEBUG "set_cr0: #GP, pdptrs " |
496 | "reserved bits\n"); | |
497 | inject_gp(vcpu); | |
498 | return; | |
499 | } | |
500 | ||
501 | } | |
502 | ||
cbdd1bea | 503 | kvm_x86_ops->set_cr0(vcpu, cr0); |
6aa8b732 AK |
504 | vcpu->cr0 = cr0; |
505 | ||
11ec2804 | 506 | mutex_lock(&vcpu->kvm->lock); |
6aa8b732 | 507 | kvm_mmu_reset_context(vcpu); |
11ec2804 | 508 | mutex_unlock(&vcpu->kvm->lock); |
6aa8b732 AK |
509 | return; |
510 | } | |
511 | EXPORT_SYMBOL_GPL(set_cr0); | |
512 | ||
513 | void lmsw(struct kvm_vcpu *vcpu, unsigned long msw) | |
514 | { | |
515 | set_cr0(vcpu, (vcpu->cr0 & ~0x0ful) | (msw & 0x0f)); | |
516 | } | |
517 | EXPORT_SYMBOL_GPL(lmsw); | |
518 | ||
519 | void set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4) | |
520 | { | |
66aee91a | 521 | if (cr4 & CR4_RESERVED_BITS) { |
6aa8b732 AK |
522 | printk(KERN_DEBUG "set_cr4: #GP, reserved bits\n"); |
523 | inject_gp(vcpu); | |
524 | return; | |
525 | } | |
526 | ||
a9058ecd | 527 | if (is_long_mode(vcpu)) { |
66aee91a | 528 | if (!(cr4 & X86_CR4_PAE)) { |
6aa8b732 AK |
529 | printk(KERN_DEBUG "set_cr4: #GP, clearing PAE while " |
530 | "in long mode\n"); | |
531 | inject_gp(vcpu); | |
532 | return; | |
533 | } | |
66aee91a | 534 | } else if (is_paging(vcpu) && !is_pae(vcpu) && (cr4 & X86_CR4_PAE) |
1342d353 | 535 | && !load_pdptrs(vcpu, vcpu->cr3)) { |
6aa8b732 AK |
536 | printk(KERN_DEBUG "set_cr4: #GP, pdptrs reserved bits\n"); |
537 | inject_gp(vcpu); | |
310bc76c | 538 | return; |
6aa8b732 AK |
539 | } |
540 | ||
66aee91a | 541 | if (cr4 & X86_CR4_VMXE) { |
6aa8b732 AK |
542 | printk(KERN_DEBUG "set_cr4: #GP, setting VMXE\n"); |
543 | inject_gp(vcpu); | |
544 | return; | |
545 | } | |
cbdd1bea | 546 | kvm_x86_ops->set_cr4(vcpu, cr4); |
81f50e3b | 547 | vcpu->cr4 = cr4; |
11ec2804 | 548 | mutex_lock(&vcpu->kvm->lock); |
6aa8b732 | 549 | kvm_mmu_reset_context(vcpu); |
11ec2804 | 550 | mutex_unlock(&vcpu->kvm->lock); |
6aa8b732 AK |
551 | } |
552 | EXPORT_SYMBOL_GPL(set_cr4); | |
553 | ||
554 | void set_cr3(struct kvm_vcpu *vcpu, unsigned long cr3) | |
555 | { | |
a9058ecd | 556 | if (is_long_mode(vcpu)) { |
f802a307 | 557 | if (cr3 & CR3_L_MODE_RESERVED_BITS) { |
6aa8b732 AK |
558 | printk(KERN_DEBUG "set_cr3: #GP, reserved bits\n"); |
559 | inject_gp(vcpu); | |
560 | return; | |
561 | } | |
562 | } else { | |
f802a307 RR |
563 | if (is_pae(vcpu)) { |
564 | if (cr3 & CR3_PAE_RESERVED_BITS) { | |
565 | printk(KERN_DEBUG | |
566 | "set_cr3: #GP, reserved bits\n"); | |
567 | inject_gp(vcpu); | |
568 | return; | |
569 | } | |
570 | if (is_paging(vcpu) && !load_pdptrs(vcpu, cr3)) { | |
571 | printk(KERN_DEBUG "set_cr3: #GP, pdptrs " | |
572 | "reserved bits\n"); | |
573 | inject_gp(vcpu); | |
574 | return; | |
575 | } | |
576 | } else { | |
577 | if (cr3 & CR3_NONPAE_RESERVED_BITS) { | |
578 | printk(KERN_DEBUG | |
579 | "set_cr3: #GP, reserved bits\n"); | |
580 | inject_gp(vcpu); | |
581 | return; | |
582 | } | |
6aa8b732 AK |
583 | } |
584 | } | |
585 | ||
11ec2804 | 586 | mutex_lock(&vcpu->kvm->lock); |
d21225ee IM |
587 | /* |
588 | * Does the new cr3 value map to physical memory? (Note, we | |
589 | * catch an invalid cr3 even in real-mode, because it would | |
590 | * cause trouble later on when we turn on paging anyway.) | |
591 | * | |
592 | * A real CPU would silently accept an invalid cr3 and would | |
593 | * attempt to use it - with largely undefined (and often hard | |
594 | * to debug) behavior on the guest side. | |
595 | */ | |
596 | if (unlikely(!gfn_to_memslot(vcpu->kvm, cr3 >> PAGE_SHIFT))) | |
597 | inject_gp(vcpu); | |
fb764416 RR |
598 | else { |
599 | vcpu->cr3 = cr3; | |
d21225ee | 600 | vcpu->mmu.new_cr3(vcpu); |
fb764416 | 601 | } |
11ec2804 | 602 | mutex_unlock(&vcpu->kvm->lock); |
6aa8b732 AK |
603 | } |
604 | EXPORT_SYMBOL_GPL(set_cr3); | |
605 | ||
606 | void set_cr8(struct kvm_vcpu *vcpu, unsigned long cr8) | |
607 | { | |
7075bc81 | 608 | if (cr8 & CR8_RESERVED_BITS) { |
6aa8b732 AK |
609 | printk(KERN_DEBUG "set_cr8: #GP, reserved bits 0x%lx\n", cr8); |
610 | inject_gp(vcpu); | |
611 | return; | |
612 | } | |
97222cc8 ED |
613 | if (irqchip_in_kernel(vcpu->kvm)) |
614 | kvm_lapic_set_tpr(vcpu, cr8); | |
615 | else | |
616 | vcpu->cr8 = cr8; | |
6aa8b732 AK |
617 | } |
618 | EXPORT_SYMBOL_GPL(set_cr8); | |
619 | ||
7017fc3d ED |
620 | unsigned long get_cr8(struct kvm_vcpu *vcpu) |
621 | { | |
97222cc8 ED |
622 | if (irqchip_in_kernel(vcpu->kvm)) |
623 | return kvm_lapic_get_cr8(vcpu); | |
624 | else | |
625 | return vcpu->cr8; | |
7017fc3d ED |
626 | } |
627 | EXPORT_SYMBOL_GPL(get_cr8); | |
628 | ||
629 | u64 kvm_get_apic_base(struct kvm_vcpu *vcpu) | |
630 | { | |
97222cc8 ED |
631 | if (irqchip_in_kernel(vcpu->kvm)) |
632 | return vcpu->apic_base; | |
633 | else | |
634 | return vcpu->apic_base; | |
7017fc3d ED |
635 | } |
636 | EXPORT_SYMBOL_GPL(kvm_get_apic_base); | |
637 | ||
638 | void kvm_set_apic_base(struct kvm_vcpu *vcpu, u64 data) | |
639 | { | |
97222cc8 ED |
640 | /* TODO: reserve bits check */ |
641 | if (irqchip_in_kernel(vcpu->kvm)) | |
642 | kvm_lapic_set_base(vcpu, data); | |
643 | else | |
644 | vcpu->apic_base = data; | |
7017fc3d ED |
645 | } |
646 | EXPORT_SYMBOL_GPL(kvm_set_apic_base); | |
647 | ||
6aa8b732 AK |
648 | void fx_init(struct kvm_vcpu *vcpu) |
649 | { | |
b114b080 | 650 | unsigned after_mxcsr_mask; |
6aa8b732 | 651 | |
9bd01506 RR |
652 | /* Initialize guest FPU by resetting ours and saving into guest's */ |
653 | preempt_disable(); | |
b114b080 | 654 | fx_save(&vcpu->host_fx_image); |
6aa8b732 | 655 | fpu_init(); |
b114b080 RR |
656 | fx_save(&vcpu->guest_fx_image); |
657 | fx_restore(&vcpu->host_fx_image); | |
9bd01506 | 658 | preempt_enable(); |
6aa8b732 | 659 | |
380102c8 | 660 | vcpu->cr0 |= X86_CR0_ET; |
b114b080 RR |
661 | after_mxcsr_mask = offsetof(struct i387_fxsave_struct, st_space); |
662 | vcpu->guest_fx_image.mxcsr = 0x1f80; | |
663 | memset((void *)&vcpu->guest_fx_image + after_mxcsr_mask, | |
664 | 0, sizeof(struct i387_fxsave_struct) - after_mxcsr_mask); | |
6aa8b732 AK |
665 | } |
666 | EXPORT_SYMBOL_GPL(fx_init); | |
667 | ||
6aa8b732 AK |
668 | /* |
669 | * Allocate some memory and give it an address in the guest physical address | |
670 | * space. | |
671 | * | |
672 | * Discontiguous memory is allowed, mostly for framebuffers. | |
673 | */ | |
2c6f5df9 AK |
674 | static int kvm_vm_ioctl_set_memory_region(struct kvm *kvm, |
675 | struct kvm_memory_region *mem) | |
6aa8b732 AK |
676 | { |
677 | int r; | |
678 | gfn_t base_gfn; | |
679 | unsigned long npages; | |
680 | unsigned long i; | |
681 | struct kvm_memory_slot *memslot; | |
682 | struct kvm_memory_slot old, new; | |
6aa8b732 AK |
683 | |
684 | r = -EINVAL; | |
685 | /* General sanity checks */ | |
686 | if (mem->memory_size & (PAGE_SIZE - 1)) | |
687 | goto out; | |
688 | if (mem->guest_phys_addr & (PAGE_SIZE - 1)) | |
689 | goto out; | |
690 | if (mem->slot >= KVM_MEMORY_SLOTS) | |
691 | goto out; | |
692 | if (mem->guest_phys_addr + mem->memory_size < mem->guest_phys_addr) | |
693 | goto out; | |
694 | ||
695 | memslot = &kvm->memslots[mem->slot]; | |
696 | base_gfn = mem->guest_phys_addr >> PAGE_SHIFT; | |
697 | npages = mem->memory_size >> PAGE_SHIFT; | |
698 | ||
699 | if (!npages) | |
700 | mem->flags &= ~KVM_MEM_LOG_DIRTY_PAGES; | |
701 | ||
11ec2804 | 702 | mutex_lock(&kvm->lock); |
6aa8b732 | 703 | |
6aa8b732 AK |
704 | new = old = *memslot; |
705 | ||
706 | new.base_gfn = base_gfn; | |
707 | new.npages = npages; | |
708 | new.flags = mem->flags; | |
709 | ||
710 | /* Disallow changing a memory slot's size. */ | |
711 | r = -EINVAL; | |
712 | if (npages && old.npages && npages != old.npages) | |
713 | goto out_unlock; | |
714 | ||
715 | /* Check for overlaps */ | |
716 | r = -EEXIST; | |
717 | for (i = 0; i < KVM_MEMORY_SLOTS; ++i) { | |
718 | struct kvm_memory_slot *s = &kvm->memslots[i]; | |
719 | ||
720 | if (s == memslot) | |
721 | continue; | |
722 | if (!((base_gfn + npages <= s->base_gfn) || | |
723 | (base_gfn >= s->base_gfn + s->npages))) | |
724 | goto out_unlock; | |
725 | } | |
6aa8b732 AK |
726 | |
727 | /* Deallocate if slot is being removed */ | |
728 | if (!npages) | |
8b6d44c7 | 729 | new.phys_mem = NULL; |
6aa8b732 AK |
730 | |
731 | /* Free page dirty bitmap if unneeded */ | |
732 | if (!(new.flags & KVM_MEM_LOG_DIRTY_PAGES)) | |
8b6d44c7 | 733 | new.dirty_bitmap = NULL; |
6aa8b732 AK |
734 | |
735 | r = -ENOMEM; | |
736 | ||
737 | /* Allocate if a slot is being created */ | |
738 | if (npages && !new.phys_mem) { | |
739 | new.phys_mem = vmalloc(npages * sizeof(struct page *)); | |
740 | ||
741 | if (!new.phys_mem) | |
0d8d2bd4 | 742 | goto out_unlock; |
6aa8b732 AK |
743 | |
744 | memset(new.phys_mem, 0, npages * sizeof(struct page *)); | |
745 | for (i = 0; i < npages; ++i) { | |
746 | new.phys_mem[i] = alloc_page(GFP_HIGHUSER | |
747 | | __GFP_ZERO); | |
748 | if (!new.phys_mem[i]) | |
0d8d2bd4 | 749 | goto out_unlock; |
5972e953 | 750 | set_page_private(new.phys_mem[i],0); |
6aa8b732 AK |
751 | } |
752 | } | |
753 | ||
754 | /* Allocate page dirty bitmap if needed */ | |
755 | if ((new.flags & KVM_MEM_LOG_DIRTY_PAGES) && !new.dirty_bitmap) { | |
756 | unsigned dirty_bytes = ALIGN(npages, BITS_PER_LONG) / 8; | |
757 | ||
758 | new.dirty_bitmap = vmalloc(dirty_bytes); | |
759 | if (!new.dirty_bitmap) | |
0d8d2bd4 | 760 | goto out_unlock; |
6aa8b732 AK |
761 | memset(new.dirty_bitmap, 0, dirty_bytes); |
762 | } | |
763 | ||
6aa8b732 AK |
764 | if (mem->slot >= kvm->nmemslots) |
765 | kvm->nmemslots = mem->slot + 1; | |
766 | ||
767 | *memslot = new; | |
6aa8b732 | 768 | |
90cb0529 AK |
769 | kvm_mmu_slot_remove_write_access(kvm, mem->slot); |
770 | kvm_flush_remote_tlbs(kvm); | |
6aa8b732 | 771 | |
11ec2804 | 772 | mutex_unlock(&kvm->lock); |
6aa8b732 AK |
773 | |
774 | kvm_free_physmem_slot(&old, &new); | |
775 | return 0; | |
776 | ||
777 | out_unlock: | |
11ec2804 | 778 | mutex_unlock(&kvm->lock); |
6aa8b732 AK |
779 | kvm_free_physmem_slot(&new, &old); |
780 | out: | |
781 | return r; | |
782 | } | |
783 | ||
784 | /* | |
785 | * Get (and clear) the dirty memory log for a memory slot. | |
786 | */ | |
2c6f5df9 AK |
787 | static int kvm_vm_ioctl_get_dirty_log(struct kvm *kvm, |
788 | struct kvm_dirty_log *log) | |
6aa8b732 AK |
789 | { |
790 | struct kvm_memory_slot *memslot; | |
791 | int r, i; | |
792 | int n; | |
793 | unsigned long any = 0; | |
794 | ||
11ec2804 | 795 | mutex_lock(&kvm->lock); |
6aa8b732 | 796 | |
6aa8b732 AK |
797 | r = -EINVAL; |
798 | if (log->slot >= KVM_MEMORY_SLOTS) | |
799 | goto out; | |
800 | ||
801 | memslot = &kvm->memslots[log->slot]; | |
802 | r = -ENOENT; | |
803 | if (!memslot->dirty_bitmap) | |
804 | goto out; | |
805 | ||
cd1a4a98 | 806 | n = ALIGN(memslot->npages, BITS_PER_LONG) / 8; |
6aa8b732 | 807 | |
cd1a4a98 | 808 | for (i = 0; !any && i < n/sizeof(long); ++i) |
6aa8b732 AK |
809 | any = memslot->dirty_bitmap[i]; |
810 | ||
811 | r = -EFAULT; | |
812 | if (copy_to_user(log->dirty_bitmap, memslot->dirty_bitmap, n)) | |
813 | goto out; | |
814 | ||
39214915 RR |
815 | /* If nothing is dirty, don't bother messing with page tables. */ |
816 | if (any) { | |
39214915 RR |
817 | kvm_mmu_slot_remove_write_access(kvm, log->slot); |
818 | kvm_flush_remote_tlbs(kvm); | |
819 | memset(memslot->dirty_bitmap, 0, n); | |
39214915 | 820 | } |
6aa8b732 AK |
821 | |
822 | r = 0; | |
823 | ||
824 | out: | |
11ec2804 | 825 | mutex_unlock(&kvm->lock); |
6aa8b732 AK |
826 | return r; |
827 | } | |
828 | ||
e8207547 AK |
829 | /* |
830 | * Set a new alias region. Aliases map a portion of physical memory into | |
831 | * another portion. This is useful for memory windows, for example the PC | |
832 | * VGA region. | |
833 | */ | |
834 | static int kvm_vm_ioctl_set_memory_alias(struct kvm *kvm, | |
835 | struct kvm_memory_alias *alias) | |
836 | { | |
837 | int r, n; | |
838 | struct kvm_mem_alias *p; | |
839 | ||
840 | r = -EINVAL; | |
841 | /* General sanity checks */ | |
842 | if (alias->memory_size & (PAGE_SIZE - 1)) | |
843 | goto out; | |
844 | if (alias->guest_phys_addr & (PAGE_SIZE - 1)) | |
845 | goto out; | |
846 | if (alias->slot >= KVM_ALIAS_SLOTS) | |
847 | goto out; | |
848 | if (alias->guest_phys_addr + alias->memory_size | |
849 | < alias->guest_phys_addr) | |
850 | goto out; | |
851 | if (alias->target_phys_addr + alias->memory_size | |
852 | < alias->target_phys_addr) | |
853 | goto out; | |
854 | ||
11ec2804 | 855 | mutex_lock(&kvm->lock); |
e8207547 AK |
856 | |
857 | p = &kvm->aliases[alias->slot]; | |
858 | p->base_gfn = alias->guest_phys_addr >> PAGE_SHIFT; | |
859 | p->npages = alias->memory_size >> PAGE_SHIFT; | |
860 | p->target_gfn = alias->target_phys_addr >> PAGE_SHIFT; | |
861 | ||
862 | for (n = KVM_ALIAS_SLOTS; n > 0; --n) | |
863 | if (kvm->aliases[n - 1].npages) | |
864 | break; | |
865 | kvm->naliases = n; | |
866 | ||
90cb0529 | 867 | kvm_mmu_zap_all(kvm); |
e8207547 | 868 | |
11ec2804 | 869 | mutex_unlock(&kvm->lock); |
e8207547 AK |
870 | |
871 | return 0; | |
872 | ||
873 | out: | |
874 | return r; | |
875 | } | |
876 | ||
6ceb9d79 HQ |
877 | static int kvm_vm_ioctl_get_irqchip(struct kvm *kvm, struct kvm_irqchip *chip) |
878 | { | |
879 | int r; | |
880 | ||
881 | r = 0; | |
882 | switch (chip->chip_id) { | |
883 | case KVM_IRQCHIP_PIC_MASTER: | |
884 | memcpy (&chip->chip.pic, | |
885 | &pic_irqchip(kvm)->pics[0], | |
886 | sizeof(struct kvm_pic_state)); | |
887 | break; | |
888 | case KVM_IRQCHIP_PIC_SLAVE: | |
889 | memcpy (&chip->chip.pic, | |
890 | &pic_irqchip(kvm)->pics[1], | |
891 | sizeof(struct kvm_pic_state)); | |
892 | break; | |
6bf9e962 HQ |
893 | case KVM_IRQCHIP_IOAPIC: |
894 | memcpy (&chip->chip.ioapic, | |
895 | ioapic_irqchip(kvm), | |
896 | sizeof(struct kvm_ioapic_state)); | |
897 | break; | |
6ceb9d79 HQ |
898 | default: |
899 | r = -EINVAL; | |
900 | break; | |
901 | } | |
902 | return r; | |
903 | } | |
904 | ||
905 | static int kvm_vm_ioctl_set_irqchip(struct kvm *kvm, struct kvm_irqchip *chip) | |
906 | { | |
907 | int r; | |
908 | ||
909 | r = 0; | |
910 | switch (chip->chip_id) { | |
911 | case KVM_IRQCHIP_PIC_MASTER: | |
912 | memcpy (&pic_irqchip(kvm)->pics[0], | |
913 | &chip->chip.pic, | |
914 | sizeof(struct kvm_pic_state)); | |
915 | break; | |
916 | case KVM_IRQCHIP_PIC_SLAVE: | |
917 | memcpy (&pic_irqchip(kvm)->pics[1], | |
918 | &chip->chip.pic, | |
919 | sizeof(struct kvm_pic_state)); | |
920 | break; | |
6bf9e962 HQ |
921 | case KVM_IRQCHIP_IOAPIC: |
922 | memcpy (ioapic_irqchip(kvm), | |
923 | &chip->chip.ioapic, | |
924 | sizeof(struct kvm_ioapic_state)); | |
925 | break; | |
6ceb9d79 HQ |
926 | default: |
927 | r = -EINVAL; | |
928 | break; | |
929 | } | |
930 | kvm_pic_update_irq(pic_irqchip(kvm)); | |
931 | return r; | |
932 | } | |
933 | ||
e8207547 AK |
934 | static gfn_t unalias_gfn(struct kvm *kvm, gfn_t gfn) |
935 | { | |
936 | int i; | |
937 | struct kvm_mem_alias *alias; | |
938 | ||
939 | for (i = 0; i < kvm->naliases; ++i) { | |
940 | alias = &kvm->aliases[i]; | |
941 | if (gfn >= alias->base_gfn | |
942 | && gfn < alias->base_gfn + alias->npages) | |
943 | return alias->target_gfn + gfn - alias->base_gfn; | |
944 | } | |
945 | return gfn; | |
946 | } | |
947 | ||
948 | static struct kvm_memory_slot *__gfn_to_memslot(struct kvm *kvm, gfn_t gfn) | |
6aa8b732 AK |
949 | { |
950 | int i; | |
951 | ||
952 | for (i = 0; i < kvm->nmemslots; ++i) { | |
953 | struct kvm_memory_slot *memslot = &kvm->memslots[i]; | |
954 | ||
955 | if (gfn >= memslot->base_gfn | |
956 | && gfn < memslot->base_gfn + memslot->npages) | |
957 | return memslot; | |
958 | } | |
8b6d44c7 | 959 | return NULL; |
6aa8b732 | 960 | } |
e8207547 AK |
961 | |
962 | struct kvm_memory_slot *gfn_to_memslot(struct kvm *kvm, gfn_t gfn) | |
963 | { | |
964 | gfn = unalias_gfn(kvm, gfn); | |
965 | return __gfn_to_memslot(kvm, gfn); | |
966 | } | |
6aa8b732 | 967 | |
954bbbc2 AK |
968 | struct page *gfn_to_page(struct kvm *kvm, gfn_t gfn) |
969 | { | |
970 | struct kvm_memory_slot *slot; | |
971 | ||
e8207547 AK |
972 | gfn = unalias_gfn(kvm, gfn); |
973 | slot = __gfn_to_memslot(kvm, gfn); | |
954bbbc2 AK |
974 | if (!slot) |
975 | return NULL; | |
976 | return slot->phys_mem[gfn - slot->base_gfn]; | |
977 | } | |
978 | EXPORT_SYMBOL_GPL(gfn_to_page); | |
979 | ||
7e9d619d | 980 | /* WARNING: Does not work on aliased pages. */ |
6aa8b732 AK |
981 | void mark_page_dirty(struct kvm *kvm, gfn_t gfn) |
982 | { | |
31389947 | 983 | struct kvm_memory_slot *memslot; |
6aa8b732 | 984 | |
7e9d619d RR |
985 | memslot = __gfn_to_memslot(kvm, gfn); |
986 | if (memslot && memslot->dirty_bitmap) { | |
987 | unsigned long rel_gfn = gfn - memslot->base_gfn; | |
6aa8b732 | 988 | |
7e9d619d RR |
989 | /* avoid RMW */ |
990 | if (!test_bit(rel_gfn, memslot->dirty_bitmap)) | |
991 | set_bit(rel_gfn, memslot->dirty_bitmap); | |
6aa8b732 AK |
992 | } |
993 | } | |
994 | ||
e7d5d76c | 995 | int emulator_read_std(unsigned long addr, |
4c690a1e | 996 | void *val, |
6aa8b732 | 997 | unsigned int bytes, |
cebff02b | 998 | struct kvm_vcpu *vcpu) |
6aa8b732 | 999 | { |
6aa8b732 AK |
1000 | void *data = val; |
1001 | ||
1002 | while (bytes) { | |
1003 | gpa_t gpa = vcpu->mmu.gva_to_gpa(vcpu, addr); | |
1004 | unsigned offset = addr & (PAGE_SIZE-1); | |
1005 | unsigned tocopy = min(bytes, (unsigned)PAGE_SIZE - offset); | |
1006 | unsigned long pfn; | |
954bbbc2 AK |
1007 | struct page *page; |
1008 | void *page_virt; | |
6aa8b732 AK |
1009 | |
1010 | if (gpa == UNMAPPED_GVA) | |
1011 | return X86EMUL_PROPAGATE_FAULT; | |
1012 | pfn = gpa >> PAGE_SHIFT; | |
954bbbc2 AK |
1013 | page = gfn_to_page(vcpu->kvm, pfn); |
1014 | if (!page) | |
6aa8b732 | 1015 | return X86EMUL_UNHANDLEABLE; |
954bbbc2 | 1016 | page_virt = kmap_atomic(page, KM_USER0); |
6aa8b732 | 1017 | |
954bbbc2 | 1018 | memcpy(data, page_virt + offset, tocopy); |
6aa8b732 | 1019 | |
954bbbc2 | 1020 | kunmap_atomic(page_virt, KM_USER0); |
6aa8b732 AK |
1021 | |
1022 | bytes -= tocopy; | |
1023 | data += tocopy; | |
1024 | addr += tocopy; | |
1025 | } | |
1026 | ||
1027 | return X86EMUL_CONTINUE; | |
1028 | } | |
e7d5d76c | 1029 | EXPORT_SYMBOL_GPL(emulator_read_std); |
6aa8b732 AK |
1030 | |
1031 | static int emulator_write_std(unsigned long addr, | |
4c690a1e | 1032 | const void *val, |
6aa8b732 | 1033 | unsigned int bytes, |
cebff02b | 1034 | struct kvm_vcpu *vcpu) |
6aa8b732 | 1035 | { |
f0242478 | 1036 | pr_unimpl(vcpu, "emulator_write_std: addr %lx n %d\n", addr, bytes); |
6aa8b732 AK |
1037 | return X86EMUL_UNHANDLEABLE; |
1038 | } | |
1039 | ||
97222cc8 ED |
1040 | /* |
1041 | * Only apic need an MMIO device hook, so shortcut now.. | |
1042 | */ | |
1043 | static struct kvm_io_device *vcpu_find_pervcpu_dev(struct kvm_vcpu *vcpu, | |
1044 | gpa_t addr) | |
1045 | { | |
1046 | struct kvm_io_device *dev; | |
1047 | ||
1048 | if (vcpu->apic) { | |
1049 | dev = &vcpu->apic->dev; | |
1050 | if (dev->in_range(dev, addr)) | |
1051 | return dev; | |
1052 | } | |
1053 | return NULL; | |
1054 | } | |
1055 | ||
2eeb2e94 GH |
1056 | static struct kvm_io_device *vcpu_find_mmio_dev(struct kvm_vcpu *vcpu, |
1057 | gpa_t addr) | |
1058 | { | |
97222cc8 ED |
1059 | struct kvm_io_device *dev; |
1060 | ||
1061 | dev = vcpu_find_pervcpu_dev(vcpu, addr); | |
1062 | if (dev == NULL) | |
1063 | dev = kvm_io_bus_find_dev(&vcpu->kvm->mmio_bus, addr); | |
1064 | return dev; | |
2eeb2e94 GH |
1065 | } |
1066 | ||
74906345 ED |
1067 | static struct kvm_io_device *vcpu_find_pio_dev(struct kvm_vcpu *vcpu, |
1068 | gpa_t addr) | |
1069 | { | |
1070 | return kvm_io_bus_find_dev(&vcpu->kvm->pio_bus, addr); | |
1071 | } | |
1072 | ||
6aa8b732 | 1073 | static int emulator_read_emulated(unsigned long addr, |
4c690a1e | 1074 | void *val, |
6aa8b732 | 1075 | unsigned int bytes, |
cebff02b | 1076 | struct kvm_vcpu *vcpu) |
6aa8b732 | 1077 | { |
2eeb2e94 GH |
1078 | struct kvm_io_device *mmio_dev; |
1079 | gpa_t gpa; | |
6aa8b732 AK |
1080 | |
1081 | if (vcpu->mmio_read_completed) { | |
1082 | memcpy(val, vcpu->mmio_data, bytes); | |
1083 | vcpu->mmio_read_completed = 0; | |
1084 | return X86EMUL_CONTINUE; | |
cebff02b | 1085 | } else if (emulator_read_std(addr, val, bytes, vcpu) |
6aa8b732 AK |
1086 | == X86EMUL_CONTINUE) |
1087 | return X86EMUL_CONTINUE; | |
d27d4aca | 1088 | |
2eeb2e94 GH |
1089 | gpa = vcpu->mmu.gva_to_gpa(vcpu, addr); |
1090 | if (gpa == UNMAPPED_GVA) | |
1091 | return X86EMUL_PROPAGATE_FAULT; | |
6aa8b732 | 1092 | |
2eeb2e94 GH |
1093 | /* |
1094 | * Is this MMIO handled locally? | |
1095 | */ | |
1096 | mmio_dev = vcpu_find_mmio_dev(vcpu, gpa); | |
1097 | if (mmio_dev) { | |
1098 | kvm_iodevice_read(mmio_dev, gpa, bytes, val); | |
1099 | return X86EMUL_CONTINUE; | |
6aa8b732 | 1100 | } |
2eeb2e94 GH |
1101 | |
1102 | vcpu->mmio_needed = 1; | |
1103 | vcpu->mmio_phys_addr = gpa; | |
1104 | vcpu->mmio_size = bytes; | |
1105 | vcpu->mmio_is_write = 0; | |
1106 | ||
1107 | return X86EMUL_UNHANDLEABLE; | |
6aa8b732 AK |
1108 | } |
1109 | ||
da4a00f0 | 1110 | static int emulator_write_phys(struct kvm_vcpu *vcpu, gpa_t gpa, |
4c690a1e | 1111 | const void *val, int bytes) |
da4a00f0 | 1112 | { |
da4a00f0 AK |
1113 | struct page *page; |
1114 | void *virt; | |
1115 | ||
1116 | if (((gpa + bytes - 1) >> PAGE_SHIFT) != (gpa >> PAGE_SHIFT)) | |
1117 | return 0; | |
954bbbc2 AK |
1118 | page = gfn_to_page(vcpu->kvm, gpa >> PAGE_SHIFT); |
1119 | if (!page) | |
da4a00f0 | 1120 | return 0; |
ab51a434 | 1121 | mark_page_dirty(vcpu->kvm, gpa >> PAGE_SHIFT); |
da4a00f0 | 1122 | virt = kmap_atomic(page, KM_USER0); |
fe551881 | 1123 | kvm_mmu_pte_write(vcpu, gpa, val, bytes); |
7cfa4b0a | 1124 | memcpy(virt + offset_in_page(gpa), val, bytes); |
da4a00f0 | 1125 | kunmap_atomic(virt, KM_USER0); |
da4a00f0 AK |
1126 | return 1; |
1127 | } | |
1128 | ||
b0fcd903 AK |
1129 | static int emulator_write_emulated_onepage(unsigned long addr, |
1130 | const void *val, | |
1131 | unsigned int bytes, | |
cebff02b | 1132 | struct kvm_vcpu *vcpu) |
6aa8b732 | 1133 | { |
2eeb2e94 GH |
1134 | struct kvm_io_device *mmio_dev; |
1135 | gpa_t gpa = vcpu->mmu.gva_to_gpa(vcpu, addr); | |
6aa8b732 | 1136 | |
c9047f53 | 1137 | if (gpa == UNMAPPED_GVA) { |
cbdd1bea | 1138 | kvm_x86_ops->inject_page_fault(vcpu, addr, 2); |
6aa8b732 | 1139 | return X86EMUL_PROPAGATE_FAULT; |
c9047f53 | 1140 | } |
6aa8b732 | 1141 | |
da4a00f0 AK |
1142 | if (emulator_write_phys(vcpu, gpa, val, bytes)) |
1143 | return X86EMUL_CONTINUE; | |
1144 | ||
2eeb2e94 GH |
1145 | /* |
1146 | * Is this MMIO handled locally? | |
1147 | */ | |
1148 | mmio_dev = vcpu_find_mmio_dev(vcpu, gpa); | |
1149 | if (mmio_dev) { | |
1150 | kvm_iodevice_write(mmio_dev, gpa, bytes, val); | |
1151 | return X86EMUL_CONTINUE; | |
1152 | } | |
1153 | ||
6aa8b732 AK |
1154 | vcpu->mmio_needed = 1; |
1155 | vcpu->mmio_phys_addr = gpa; | |
1156 | vcpu->mmio_size = bytes; | |
1157 | vcpu->mmio_is_write = 1; | |
4c690a1e | 1158 | memcpy(vcpu->mmio_data, val, bytes); |
6aa8b732 AK |
1159 | |
1160 | return X86EMUL_CONTINUE; | |
1161 | } | |
1162 | ||
e7d5d76c | 1163 | int emulator_write_emulated(unsigned long addr, |
b0fcd903 AK |
1164 | const void *val, |
1165 | unsigned int bytes, | |
cebff02b | 1166 | struct kvm_vcpu *vcpu) |
b0fcd903 AK |
1167 | { |
1168 | /* Crossing a page boundary? */ | |
1169 | if (((addr + bytes - 1) ^ addr) & PAGE_MASK) { | |
1170 | int rc, now; | |
1171 | ||
1172 | now = -addr & ~PAGE_MASK; | |
cebff02b | 1173 | rc = emulator_write_emulated_onepage(addr, val, now, vcpu); |
b0fcd903 AK |
1174 | if (rc != X86EMUL_CONTINUE) |
1175 | return rc; | |
1176 | addr += now; | |
1177 | val += now; | |
1178 | bytes -= now; | |
1179 | } | |
cebff02b | 1180 | return emulator_write_emulated_onepage(addr, val, bytes, vcpu); |
b0fcd903 | 1181 | } |
e7d5d76c | 1182 | EXPORT_SYMBOL_GPL(emulator_write_emulated); |
b0fcd903 | 1183 | |
6aa8b732 | 1184 | static int emulator_cmpxchg_emulated(unsigned long addr, |
4c690a1e AK |
1185 | const void *old, |
1186 | const void *new, | |
6aa8b732 | 1187 | unsigned int bytes, |
cebff02b | 1188 | struct kvm_vcpu *vcpu) |
6aa8b732 AK |
1189 | { |
1190 | static int reported; | |
1191 | ||
1192 | if (!reported) { | |
1193 | reported = 1; | |
1194 | printk(KERN_WARNING "kvm: emulating exchange as write\n"); | |
1195 | } | |
cebff02b | 1196 | return emulator_write_emulated(addr, new, bytes, vcpu); |
6aa8b732 AK |
1197 | } |
1198 | ||
1199 | static unsigned long get_segment_base(struct kvm_vcpu *vcpu, int seg) | |
1200 | { | |
cbdd1bea | 1201 | return kvm_x86_ops->get_segment_base(vcpu, seg); |
6aa8b732 AK |
1202 | } |
1203 | ||
1204 | int emulate_invlpg(struct kvm_vcpu *vcpu, gva_t address) | |
1205 | { | |
6aa8b732 AK |
1206 | return X86EMUL_CONTINUE; |
1207 | } | |
1208 | ||
1209 | int emulate_clts(struct kvm_vcpu *vcpu) | |
1210 | { | |
81f50e3b | 1211 | vcpu->cr0 &= ~X86_CR0_TS; |
cbdd1bea | 1212 | kvm_x86_ops->set_cr0(vcpu, vcpu->cr0); |
6aa8b732 AK |
1213 | return X86EMUL_CONTINUE; |
1214 | } | |
1215 | ||
1216 | int emulator_get_dr(struct x86_emulate_ctxt* ctxt, int dr, unsigned long *dest) | |
1217 | { | |
1218 | struct kvm_vcpu *vcpu = ctxt->vcpu; | |
1219 | ||
1220 | switch (dr) { | |
1221 | case 0 ... 3: | |
cbdd1bea | 1222 | *dest = kvm_x86_ops->get_dr(vcpu, dr); |
6aa8b732 AK |
1223 | return X86EMUL_CONTINUE; |
1224 | default: | |
f0242478 | 1225 | pr_unimpl(vcpu, "%s: unexpected dr %u\n", __FUNCTION__, dr); |
6aa8b732 AK |
1226 | return X86EMUL_UNHANDLEABLE; |
1227 | } | |
1228 | } | |
1229 | ||
1230 | int emulator_set_dr(struct x86_emulate_ctxt *ctxt, int dr, unsigned long value) | |
1231 | { | |
1232 | unsigned long mask = (ctxt->mode == X86EMUL_MODE_PROT64) ? ~0ULL : ~0U; | |
1233 | int exception; | |
1234 | ||
cbdd1bea | 1235 | kvm_x86_ops->set_dr(ctxt->vcpu, dr, value & mask, &exception); |
6aa8b732 AK |
1236 | if (exception) { |
1237 | /* FIXME: better handling */ | |
1238 | return X86EMUL_UNHANDLEABLE; | |
1239 | } | |
1240 | return X86EMUL_CONTINUE; | |
1241 | } | |
1242 | ||
054b1369 | 1243 | void kvm_report_emulation_failure(struct kvm_vcpu *vcpu, const char *context) |
6aa8b732 AK |
1244 | { |
1245 | static int reported; | |
1246 | u8 opcodes[4]; | |
054b1369 | 1247 | unsigned long rip = vcpu->rip; |
6aa8b732 AK |
1248 | unsigned long rip_linear; |
1249 | ||
054b1369 | 1250 | rip_linear = rip + get_segment_base(vcpu, VCPU_SREG_CS); |
6aa8b732 AK |
1251 | |
1252 | if (reported) | |
1253 | return; | |
1254 | ||
054b1369 | 1255 | emulator_read_std(rip_linear, (void *)opcodes, 4, vcpu); |
6aa8b732 | 1256 | |
054b1369 AK |
1257 | printk(KERN_ERR "emulation failed (%s) rip %lx %02x %02x %02x %02x\n", |
1258 | context, rip, opcodes[0], opcodes[1], opcodes[2], opcodes[3]); | |
6aa8b732 AK |
1259 | reported = 1; |
1260 | } | |
054b1369 | 1261 | EXPORT_SYMBOL_GPL(kvm_report_emulation_failure); |
6aa8b732 AK |
1262 | |
1263 | struct x86_emulate_ops emulate_ops = { | |
1264 | .read_std = emulator_read_std, | |
1265 | .write_std = emulator_write_std, | |
1266 | .read_emulated = emulator_read_emulated, | |
1267 | .write_emulated = emulator_write_emulated, | |
1268 | .cmpxchg_emulated = emulator_cmpxchg_emulated, | |
1269 | }; | |
1270 | ||
1271 | int emulate_instruction(struct kvm_vcpu *vcpu, | |
1272 | struct kvm_run *run, | |
1273 | unsigned long cr2, | |
1274 | u16 error_code) | |
1275 | { | |
1276 | struct x86_emulate_ctxt emulate_ctxt; | |
1277 | int r; | |
1278 | int cs_db, cs_l; | |
1279 | ||
e7df56e4 | 1280 | vcpu->mmio_fault_cr2 = cr2; |
cbdd1bea | 1281 | kvm_x86_ops->cache_regs(vcpu); |
6aa8b732 | 1282 | |
cbdd1bea | 1283 | kvm_x86_ops->get_cs_db_l_bits(vcpu, &cs_db, &cs_l); |
6aa8b732 AK |
1284 | |
1285 | emulate_ctxt.vcpu = vcpu; | |
cbdd1bea | 1286 | emulate_ctxt.eflags = kvm_x86_ops->get_rflags(vcpu); |
6aa8b732 AK |
1287 | emulate_ctxt.cr2 = cr2; |
1288 | emulate_ctxt.mode = (emulate_ctxt.eflags & X86_EFLAGS_VM) | |
1289 | ? X86EMUL_MODE_REAL : cs_l | |
1290 | ? X86EMUL_MODE_PROT64 : cs_db | |
1291 | ? X86EMUL_MODE_PROT32 : X86EMUL_MODE_PROT16; | |
1292 | ||
1293 | if (emulate_ctxt.mode == X86EMUL_MODE_PROT64) { | |
1294 | emulate_ctxt.cs_base = 0; | |
1295 | emulate_ctxt.ds_base = 0; | |
1296 | emulate_ctxt.es_base = 0; | |
1297 | emulate_ctxt.ss_base = 0; | |
1298 | } else { | |
1299 | emulate_ctxt.cs_base = get_segment_base(vcpu, VCPU_SREG_CS); | |
1300 | emulate_ctxt.ds_base = get_segment_base(vcpu, VCPU_SREG_DS); | |
1301 | emulate_ctxt.es_base = get_segment_base(vcpu, VCPU_SREG_ES); | |
1302 | emulate_ctxt.ss_base = get_segment_base(vcpu, VCPU_SREG_SS); | |
1303 | } | |
1304 | ||
1305 | emulate_ctxt.gs_base = get_segment_base(vcpu, VCPU_SREG_GS); | |
1306 | emulate_ctxt.fs_base = get_segment_base(vcpu, VCPU_SREG_FS); | |
1307 | ||
1308 | vcpu->mmio_is_write = 0; | |
e70669ab | 1309 | vcpu->pio.string = 0; |
6aa8b732 | 1310 | r = x86_emulate_memop(&emulate_ctxt, &emulate_ops); |
e70669ab LV |
1311 | if (vcpu->pio.string) |
1312 | return EMULATE_DO_MMIO; | |
6aa8b732 AK |
1313 | |
1314 | if ((r || vcpu->mmio_is_write) && run) { | |
8fc0d085 | 1315 | run->exit_reason = KVM_EXIT_MMIO; |
6aa8b732 AK |
1316 | run->mmio.phys_addr = vcpu->mmio_phys_addr; |
1317 | memcpy(run->mmio.data, vcpu->mmio_data, 8); | |
1318 | run->mmio.len = vcpu->mmio_size; | |
1319 | run->mmio.is_write = vcpu->mmio_is_write; | |
1320 | } | |
1321 | ||
1322 | if (r) { | |
a436036b AK |
1323 | if (kvm_mmu_unprotect_page_virt(vcpu, cr2)) |
1324 | return EMULATE_DONE; | |
6aa8b732 | 1325 | if (!vcpu->mmio_needed) { |
054b1369 | 1326 | kvm_report_emulation_failure(vcpu, "mmio"); |
6aa8b732 AK |
1327 | return EMULATE_FAIL; |
1328 | } | |
1329 | return EMULATE_DO_MMIO; | |
1330 | } | |
1331 | ||
cbdd1bea CE |
1332 | kvm_x86_ops->decache_regs(vcpu); |
1333 | kvm_x86_ops->set_rflags(vcpu, emulate_ctxt.eflags); | |
6aa8b732 | 1334 | |
02c83209 AK |
1335 | if (vcpu->mmio_is_write) { |
1336 | vcpu->mmio_needed = 0; | |
6aa8b732 | 1337 | return EMULATE_DO_MMIO; |
02c83209 | 1338 | } |
6aa8b732 AK |
1339 | |
1340 | return EMULATE_DONE; | |
1341 | } | |
1342 | EXPORT_SYMBOL_GPL(emulate_instruction); | |
1343 | ||
b6958ce4 ED |
1344 | /* |
1345 | * The vCPU has executed a HLT instruction with in-kernel mode enabled. | |
1346 | */ | |
c5ec1534 | 1347 | static void kvm_vcpu_block(struct kvm_vcpu *vcpu) |
d3bef15f | 1348 | { |
b6958ce4 ED |
1349 | DECLARE_WAITQUEUE(wait, current); |
1350 | ||
1351 | add_wait_queue(&vcpu->wq, &wait); | |
1352 | ||
1353 | /* | |
1354 | * We will block until either an interrupt or a signal wakes us up | |
1355 | */ | |
c5ec1534 HQ |
1356 | while (!kvm_cpu_has_interrupt(vcpu) |
1357 | && !signal_pending(current) | |
1358 | && vcpu->mp_state != VCPU_MP_STATE_RUNNABLE | |
1359 | && vcpu->mp_state != VCPU_MP_STATE_SIPI_RECEIVED) { | |
b6958ce4 ED |
1360 | set_current_state(TASK_INTERRUPTIBLE); |
1361 | vcpu_put(vcpu); | |
1362 | schedule(); | |
1363 | vcpu_load(vcpu); | |
1364 | } | |
d3bef15f | 1365 | |
c5ec1534 | 1366 | __set_current_state(TASK_RUNNING); |
b6958ce4 | 1367 | remove_wait_queue(&vcpu->wq, &wait); |
b6958ce4 ED |
1368 | } |
1369 | ||
1370 | int kvm_emulate_halt(struct kvm_vcpu *vcpu) | |
1371 | { | |
d3bef15f | 1372 | ++vcpu->stat.halt_exits; |
b6958ce4 | 1373 | if (irqchip_in_kernel(vcpu->kvm)) { |
c5ec1534 HQ |
1374 | vcpu->mp_state = VCPU_MP_STATE_HALTED; |
1375 | kvm_vcpu_block(vcpu); | |
1376 | if (vcpu->mp_state != VCPU_MP_STATE_RUNNABLE) | |
1377 | return -EINTR; | |
b6958ce4 ED |
1378 | return 1; |
1379 | } else { | |
1380 | vcpu->run->exit_reason = KVM_EXIT_HLT; | |
1381 | return 0; | |
1382 | } | |
d3bef15f AK |
1383 | } |
1384 | EXPORT_SYMBOL_GPL(kvm_emulate_halt); | |
1385 | ||
270fd9b9 AK |
1386 | int kvm_hypercall(struct kvm_vcpu *vcpu, struct kvm_run *run) |
1387 | { | |
1388 | unsigned long nr, a0, a1, a2, a3, a4, a5, ret; | |
1389 | ||
cbdd1bea | 1390 | kvm_x86_ops->cache_regs(vcpu); |
270fd9b9 AK |
1391 | ret = -KVM_EINVAL; |
1392 | #ifdef CONFIG_X86_64 | |
1393 | if (is_long_mode(vcpu)) { | |
1394 | nr = vcpu->regs[VCPU_REGS_RAX]; | |
1395 | a0 = vcpu->regs[VCPU_REGS_RDI]; | |
1396 | a1 = vcpu->regs[VCPU_REGS_RSI]; | |
1397 | a2 = vcpu->regs[VCPU_REGS_RDX]; | |
1398 | a3 = vcpu->regs[VCPU_REGS_RCX]; | |
1399 | a4 = vcpu->regs[VCPU_REGS_R8]; | |
1400 | a5 = vcpu->regs[VCPU_REGS_R9]; | |
1401 | } else | |
1402 | #endif | |
1403 | { | |
1404 | nr = vcpu->regs[VCPU_REGS_RBX] & -1u; | |
1405 | a0 = vcpu->regs[VCPU_REGS_RAX] & -1u; | |
1406 | a1 = vcpu->regs[VCPU_REGS_RCX] & -1u; | |
1407 | a2 = vcpu->regs[VCPU_REGS_RDX] & -1u; | |
1408 | a3 = vcpu->regs[VCPU_REGS_RSI] & -1u; | |
1409 | a4 = vcpu->regs[VCPU_REGS_RDI] & -1u; | |
1410 | a5 = vcpu->regs[VCPU_REGS_RBP] & -1u; | |
1411 | } | |
1412 | switch (nr) { | |
1413 | default: | |
519ef353 | 1414 | run->hypercall.nr = nr; |
b4e63f56 AK |
1415 | run->hypercall.args[0] = a0; |
1416 | run->hypercall.args[1] = a1; | |
1417 | run->hypercall.args[2] = a2; | |
1418 | run->hypercall.args[3] = a3; | |
1419 | run->hypercall.args[4] = a4; | |
1420 | run->hypercall.args[5] = a5; | |
1421 | run->hypercall.ret = ret; | |
1422 | run->hypercall.longmode = is_long_mode(vcpu); | |
cbdd1bea | 1423 | kvm_x86_ops->decache_regs(vcpu); |
b4e63f56 | 1424 | return 0; |
270fd9b9 AK |
1425 | } |
1426 | vcpu->regs[VCPU_REGS_RAX] = ret; | |
cbdd1bea | 1427 | kvm_x86_ops->decache_regs(vcpu); |
270fd9b9 AK |
1428 | return 1; |
1429 | } | |
1430 | EXPORT_SYMBOL_GPL(kvm_hypercall); | |
1431 | ||
6aa8b732 AK |
1432 | static u64 mk_cr_64(u64 curr_cr, u32 new_val) |
1433 | { | |
1434 | return (curr_cr & ~((1ULL << 32) - 1)) | new_val; | |
1435 | } | |
1436 | ||
1437 | void realmode_lgdt(struct kvm_vcpu *vcpu, u16 limit, unsigned long base) | |
1438 | { | |
1439 | struct descriptor_table dt = { limit, base }; | |
1440 | ||
cbdd1bea | 1441 | kvm_x86_ops->set_gdt(vcpu, &dt); |
6aa8b732 AK |
1442 | } |
1443 | ||
1444 | void realmode_lidt(struct kvm_vcpu *vcpu, u16 limit, unsigned long base) | |
1445 | { | |
1446 | struct descriptor_table dt = { limit, base }; | |
1447 | ||
cbdd1bea | 1448 | kvm_x86_ops->set_idt(vcpu, &dt); |
6aa8b732 AK |
1449 | } |
1450 | ||
1451 | void realmode_lmsw(struct kvm_vcpu *vcpu, unsigned long msw, | |
1452 | unsigned long *rflags) | |
1453 | { | |
1454 | lmsw(vcpu, msw); | |
cbdd1bea | 1455 | *rflags = kvm_x86_ops->get_rflags(vcpu); |
6aa8b732 AK |
1456 | } |
1457 | ||
1458 | unsigned long realmode_get_cr(struct kvm_vcpu *vcpu, int cr) | |
1459 | { | |
cbdd1bea | 1460 | kvm_x86_ops->decache_cr4_guest_bits(vcpu); |
6aa8b732 AK |
1461 | switch (cr) { |
1462 | case 0: | |
1463 | return vcpu->cr0; | |
1464 | case 2: | |
1465 | return vcpu->cr2; | |
1466 | case 3: | |
1467 | return vcpu->cr3; | |
1468 | case 4: | |
1469 | return vcpu->cr4; | |
1470 | default: | |
1471 | vcpu_printf(vcpu, "%s: unexpected cr %u\n", __FUNCTION__, cr); | |
1472 | return 0; | |
1473 | } | |
1474 | } | |
1475 | ||
1476 | void realmode_set_cr(struct kvm_vcpu *vcpu, int cr, unsigned long val, | |
1477 | unsigned long *rflags) | |
1478 | { | |
1479 | switch (cr) { | |
1480 | case 0: | |
1481 | set_cr0(vcpu, mk_cr_64(vcpu->cr0, val)); | |
cbdd1bea | 1482 | *rflags = kvm_x86_ops->get_rflags(vcpu); |
6aa8b732 AK |
1483 | break; |
1484 | case 2: | |
1485 | vcpu->cr2 = val; | |
1486 | break; | |
1487 | case 3: | |
1488 | set_cr3(vcpu, val); | |
1489 | break; | |
1490 | case 4: | |
1491 | set_cr4(vcpu, mk_cr_64(vcpu->cr4, val)); | |
1492 | break; | |
1493 | default: | |
1494 | vcpu_printf(vcpu, "%s: unexpected cr %u\n", __FUNCTION__, cr); | |
1495 | } | |
1496 | } | |
1497 | ||
102d8325 IM |
1498 | /* |
1499 | * Register the para guest with the host: | |
1500 | */ | |
1501 | static int vcpu_register_para(struct kvm_vcpu *vcpu, gpa_t para_state_gpa) | |
1502 | { | |
1503 | struct kvm_vcpu_para_state *para_state; | |
1504 | hpa_t para_state_hpa, hypercall_hpa; | |
1505 | struct page *para_state_page; | |
1506 | unsigned char *hypercall; | |
1507 | gpa_t hypercall_gpa; | |
1508 | ||
1509 | printk(KERN_DEBUG "kvm: guest trying to enter paravirtual mode\n"); | |
1510 | printk(KERN_DEBUG ".... para_state_gpa: %08Lx\n", para_state_gpa); | |
1511 | ||
1512 | /* | |
1513 | * Needs to be page aligned: | |
1514 | */ | |
1515 | if (para_state_gpa != PAGE_ALIGN(para_state_gpa)) | |
1516 | goto err_gp; | |
1517 | ||
1518 | para_state_hpa = gpa_to_hpa(vcpu, para_state_gpa); | |
1519 | printk(KERN_DEBUG ".... para_state_hpa: %08Lx\n", para_state_hpa); | |
1520 | if (is_error_hpa(para_state_hpa)) | |
1521 | goto err_gp; | |
1522 | ||
ab51a434 | 1523 | mark_page_dirty(vcpu->kvm, para_state_gpa >> PAGE_SHIFT); |
102d8325 | 1524 | para_state_page = pfn_to_page(para_state_hpa >> PAGE_SHIFT); |
fe551881 | 1525 | para_state = kmap(para_state_page); |
102d8325 IM |
1526 | |
1527 | printk(KERN_DEBUG ".... guest version: %d\n", para_state->guest_version); | |
1528 | printk(KERN_DEBUG ".... size: %d\n", para_state->size); | |
1529 | ||
1530 | para_state->host_version = KVM_PARA_API_VERSION; | |
1531 | /* | |
1532 | * We cannot support guests that try to register themselves | |
1533 | * with a newer API version than the host supports: | |
1534 | */ | |
1535 | if (para_state->guest_version > KVM_PARA_API_VERSION) { | |
1536 | para_state->ret = -KVM_EINVAL; | |
1537 | goto err_kunmap_skip; | |
1538 | } | |
1539 | ||
1540 | hypercall_gpa = para_state->hypercall_gpa; | |
1541 | hypercall_hpa = gpa_to_hpa(vcpu, hypercall_gpa); | |
1542 | printk(KERN_DEBUG ".... hypercall_hpa: %08Lx\n", hypercall_hpa); | |
1543 | if (is_error_hpa(hypercall_hpa)) { | |
1544 | para_state->ret = -KVM_EINVAL; | |
1545 | goto err_kunmap_skip; | |
1546 | } | |
1547 | ||
1548 | printk(KERN_DEBUG "kvm: para guest successfully registered.\n"); | |
1549 | vcpu->para_state_page = para_state_page; | |
1550 | vcpu->para_state_gpa = para_state_gpa; | |
1551 | vcpu->hypercall_gpa = hypercall_gpa; | |
1552 | ||
ab51a434 | 1553 | mark_page_dirty(vcpu->kvm, hypercall_gpa >> PAGE_SHIFT); |
102d8325 IM |
1554 | hypercall = kmap_atomic(pfn_to_page(hypercall_hpa >> PAGE_SHIFT), |
1555 | KM_USER1) + (hypercall_hpa & ~PAGE_MASK); | |
cbdd1bea | 1556 | kvm_x86_ops->patch_hypercall(vcpu, hypercall); |
102d8325 IM |
1557 | kunmap_atomic(hypercall, KM_USER1); |
1558 | ||
1559 | para_state->ret = 0; | |
1560 | err_kunmap_skip: | |
fe551881 | 1561 | kunmap(para_state_page); |
102d8325 IM |
1562 | return 0; |
1563 | err_gp: | |
1564 | return 1; | |
1565 | } | |
1566 | ||
3bab1f5d AK |
1567 | int kvm_get_msr_common(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata) |
1568 | { | |
1569 | u64 data; | |
1570 | ||
1571 | switch (msr) { | |
1572 | case 0xc0010010: /* SYSCFG */ | |
1573 | case 0xc0010015: /* HWCR */ | |
1574 | case MSR_IA32_PLATFORM_ID: | |
1575 | case MSR_IA32_P5_MC_ADDR: | |
1576 | case MSR_IA32_P5_MC_TYPE: | |
1577 | case MSR_IA32_MC0_CTL: | |
1578 | case MSR_IA32_MCG_STATUS: | |
1579 | case MSR_IA32_MCG_CAP: | |
1580 | case MSR_IA32_MC0_MISC: | |
1581 | case MSR_IA32_MC0_MISC+4: | |
1582 | case MSR_IA32_MC0_MISC+8: | |
1583 | case MSR_IA32_MC0_MISC+12: | |
1584 | case MSR_IA32_MC0_MISC+16: | |
1585 | case MSR_IA32_UCODE_REV: | |
a8d13ea2 | 1586 | case MSR_IA32_PERF_STATUS: |
2dc7094b | 1587 | case MSR_IA32_EBL_CR_POWERON: |
3bab1f5d AK |
1588 | /* MTRR registers */ |
1589 | case 0xfe: | |
1590 | case 0x200 ... 0x2ff: | |
1591 | data = 0; | |
1592 | break; | |
a8d13ea2 AK |
1593 | case 0xcd: /* fsb frequency */ |
1594 | data = 3; | |
1595 | break; | |
3bab1f5d | 1596 | case MSR_IA32_APICBASE: |
7017fc3d | 1597 | data = kvm_get_apic_base(vcpu); |
3bab1f5d | 1598 | break; |
6f00e68f AK |
1599 | case MSR_IA32_MISC_ENABLE: |
1600 | data = vcpu->ia32_misc_enable_msr; | |
1601 | break; | |
3bab1f5d AK |
1602 | #ifdef CONFIG_X86_64 |
1603 | case MSR_EFER: | |
1604 | data = vcpu->shadow_efer; | |
1605 | break; | |
1606 | #endif | |
1607 | default: | |
f0242478 | 1608 | pr_unimpl(vcpu, "unhandled rdmsr: 0x%x\n", msr); |
3bab1f5d AK |
1609 | return 1; |
1610 | } | |
1611 | *pdata = data; | |
1612 | return 0; | |
1613 | } | |
1614 | EXPORT_SYMBOL_GPL(kvm_get_msr_common); | |
1615 | ||
6aa8b732 AK |
1616 | /* |
1617 | * Reads an msr value (of 'msr_index') into 'pdata'. | |
1618 | * Returns 0 on success, non-0 otherwise. | |
1619 | * Assumes vcpu_load() was already called. | |
1620 | */ | |
35f3f286 | 1621 | int kvm_get_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 *pdata) |
6aa8b732 | 1622 | { |
cbdd1bea | 1623 | return kvm_x86_ops->get_msr(vcpu, msr_index, pdata); |
6aa8b732 AK |
1624 | } |
1625 | ||
05b3e0c2 | 1626 | #ifdef CONFIG_X86_64 |
6aa8b732 | 1627 | |
3bab1f5d | 1628 | static void set_efer(struct kvm_vcpu *vcpu, u64 efer) |
6aa8b732 | 1629 | { |
6aa8b732 AK |
1630 | if (efer & EFER_RESERVED_BITS) { |
1631 | printk(KERN_DEBUG "set_efer: 0x%llx #GP, reserved bits\n", | |
1632 | efer); | |
1633 | inject_gp(vcpu); | |
1634 | return; | |
1635 | } | |
1636 | ||
1637 | if (is_paging(vcpu) | |
1638 | && (vcpu->shadow_efer & EFER_LME) != (efer & EFER_LME)) { | |
1639 | printk(KERN_DEBUG "set_efer: #GP, change LME while paging\n"); | |
1640 | inject_gp(vcpu); | |
1641 | return; | |
1642 | } | |
1643 | ||
cbdd1bea | 1644 | kvm_x86_ops->set_efer(vcpu, efer); |
7725f0ba | 1645 | |
6aa8b732 AK |
1646 | efer &= ~EFER_LMA; |
1647 | efer |= vcpu->shadow_efer & EFER_LMA; | |
1648 | ||
1649 | vcpu->shadow_efer = efer; | |
6aa8b732 | 1650 | } |
6aa8b732 AK |
1651 | |
1652 | #endif | |
1653 | ||
3bab1f5d AK |
1654 | int kvm_set_msr_common(struct kvm_vcpu *vcpu, u32 msr, u64 data) |
1655 | { | |
1656 | switch (msr) { | |
1657 | #ifdef CONFIG_X86_64 | |
1658 | case MSR_EFER: | |
1659 | set_efer(vcpu, data); | |
1660 | break; | |
1661 | #endif | |
1662 | case MSR_IA32_MC0_STATUS: | |
f0242478 | 1663 | pr_unimpl(vcpu, "%s: MSR_IA32_MC0_STATUS 0x%llx, nop\n", |
3bab1f5d AK |
1664 | __FUNCTION__, data); |
1665 | break; | |
0e5bf0d0 | 1666 | case MSR_IA32_MCG_STATUS: |
f0242478 | 1667 | pr_unimpl(vcpu, "%s: MSR_IA32_MCG_STATUS 0x%llx, nop\n", |
0e5bf0d0 SK |
1668 | __FUNCTION__, data); |
1669 | break; | |
3bab1f5d AK |
1670 | case MSR_IA32_UCODE_REV: |
1671 | case MSR_IA32_UCODE_WRITE: | |
1672 | case 0x200 ... 0x2ff: /* MTRRs */ | |
1673 | break; | |
1674 | case MSR_IA32_APICBASE: | |
7017fc3d | 1675 | kvm_set_apic_base(vcpu, data); |
3bab1f5d | 1676 | break; |
6f00e68f AK |
1677 | case MSR_IA32_MISC_ENABLE: |
1678 | vcpu->ia32_misc_enable_msr = data; | |
1679 | break; | |
102d8325 IM |
1680 | /* |
1681 | * This is the 'probe whether the host is KVM' logic: | |
1682 | */ | |
1683 | case MSR_KVM_API_MAGIC: | |
1684 | return vcpu_register_para(vcpu, data); | |
1685 | ||
3bab1f5d | 1686 | default: |
f0242478 | 1687 | pr_unimpl(vcpu, "unhandled wrmsr: 0x%x\n", msr); |
3bab1f5d AK |
1688 | return 1; |
1689 | } | |
1690 | return 0; | |
1691 | } | |
1692 | EXPORT_SYMBOL_GPL(kvm_set_msr_common); | |
1693 | ||
6aa8b732 AK |
1694 | /* |
1695 | * Writes msr value into into the appropriate "register". | |
1696 | * Returns 0 on success, non-0 otherwise. | |
1697 | * Assumes vcpu_load() was already called. | |
1698 | */ | |
35f3f286 | 1699 | int kvm_set_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 data) |
6aa8b732 | 1700 | { |
cbdd1bea | 1701 | return kvm_x86_ops->set_msr(vcpu, msr_index, data); |
6aa8b732 AK |
1702 | } |
1703 | ||
1704 | void kvm_resched(struct kvm_vcpu *vcpu) | |
1705 | { | |
3fca0365 YD |
1706 | if (!need_resched()) |
1707 | return; | |
6aa8b732 | 1708 | cond_resched(); |
6aa8b732 AK |
1709 | } |
1710 | EXPORT_SYMBOL_GPL(kvm_resched); | |
1711 | ||
06465c5a AK |
1712 | void kvm_emulate_cpuid(struct kvm_vcpu *vcpu) |
1713 | { | |
1714 | int i; | |
1715 | u32 function; | |
1716 | struct kvm_cpuid_entry *e, *best; | |
1717 | ||
cbdd1bea | 1718 | kvm_x86_ops->cache_regs(vcpu); |
06465c5a AK |
1719 | function = vcpu->regs[VCPU_REGS_RAX]; |
1720 | vcpu->regs[VCPU_REGS_RAX] = 0; | |
1721 | vcpu->regs[VCPU_REGS_RBX] = 0; | |
1722 | vcpu->regs[VCPU_REGS_RCX] = 0; | |
1723 | vcpu->regs[VCPU_REGS_RDX] = 0; | |
1724 | best = NULL; | |
1725 | for (i = 0; i < vcpu->cpuid_nent; ++i) { | |
1726 | e = &vcpu->cpuid_entries[i]; | |
1727 | if (e->function == function) { | |
1728 | best = e; | |
1729 | break; | |
1730 | } | |
1731 | /* | |
1732 | * Both basic or both extended? | |
1733 | */ | |
1734 | if (((e->function ^ function) & 0x80000000) == 0) | |
1735 | if (!best || e->function > best->function) | |
1736 | best = e; | |
1737 | } | |
1738 | if (best) { | |
1739 | vcpu->regs[VCPU_REGS_RAX] = best->eax; | |
1740 | vcpu->regs[VCPU_REGS_RBX] = best->ebx; | |
1741 | vcpu->regs[VCPU_REGS_RCX] = best->ecx; | |
1742 | vcpu->regs[VCPU_REGS_RDX] = best->edx; | |
1743 | } | |
cbdd1bea CE |
1744 | kvm_x86_ops->decache_regs(vcpu); |
1745 | kvm_x86_ops->skip_emulated_instruction(vcpu); | |
06465c5a AK |
1746 | } |
1747 | EXPORT_SYMBOL_GPL(kvm_emulate_cpuid); | |
1748 | ||
039576c0 | 1749 | static int pio_copy_data(struct kvm_vcpu *vcpu) |
46fc1477 | 1750 | { |
039576c0 AK |
1751 | void *p = vcpu->pio_data; |
1752 | void *q; | |
1753 | unsigned bytes; | |
1754 | int nr_pages = vcpu->pio.guest_pages[1] ? 2 : 1; | |
1755 | ||
039576c0 AK |
1756 | q = vmap(vcpu->pio.guest_pages, nr_pages, VM_READ|VM_WRITE, |
1757 | PAGE_KERNEL); | |
1758 | if (!q) { | |
039576c0 AK |
1759 | free_pio_guest_pages(vcpu); |
1760 | return -ENOMEM; | |
1761 | } | |
1762 | q += vcpu->pio.guest_page_offset; | |
1763 | bytes = vcpu->pio.size * vcpu->pio.cur_count; | |
1764 | if (vcpu->pio.in) | |
1765 | memcpy(q, p, bytes); | |
1766 | else | |
1767 | memcpy(p, q, bytes); | |
1768 | q -= vcpu->pio.guest_page_offset; | |
1769 | vunmap(q); | |
039576c0 AK |
1770 | free_pio_guest_pages(vcpu); |
1771 | return 0; | |
1772 | } | |
1773 | ||
1774 | static int complete_pio(struct kvm_vcpu *vcpu) | |
1775 | { | |
1776 | struct kvm_pio_request *io = &vcpu->pio; | |
46fc1477 | 1777 | long delta; |
039576c0 | 1778 | int r; |
46fc1477 | 1779 | |
cbdd1bea | 1780 | kvm_x86_ops->cache_regs(vcpu); |
46fc1477 AK |
1781 | |
1782 | if (!io->string) { | |
039576c0 AK |
1783 | if (io->in) |
1784 | memcpy(&vcpu->regs[VCPU_REGS_RAX], vcpu->pio_data, | |
46fc1477 AK |
1785 | io->size); |
1786 | } else { | |
039576c0 AK |
1787 | if (io->in) { |
1788 | r = pio_copy_data(vcpu); | |
1789 | if (r) { | |
cbdd1bea | 1790 | kvm_x86_ops->cache_regs(vcpu); |
039576c0 AK |
1791 | return r; |
1792 | } | |
1793 | } | |
1794 | ||
46fc1477 AK |
1795 | delta = 1; |
1796 | if (io->rep) { | |
039576c0 | 1797 | delta *= io->cur_count; |
46fc1477 AK |
1798 | /* |
1799 | * The size of the register should really depend on | |
1800 | * current address size. | |
1801 | */ | |
1802 | vcpu->regs[VCPU_REGS_RCX] -= delta; | |
1803 | } | |
039576c0 | 1804 | if (io->down) |
46fc1477 AK |
1805 | delta = -delta; |
1806 | delta *= io->size; | |
039576c0 | 1807 | if (io->in) |
46fc1477 AK |
1808 | vcpu->regs[VCPU_REGS_RDI] += delta; |
1809 | else | |
1810 | vcpu->regs[VCPU_REGS_RSI] += delta; | |
1811 | } | |
1812 | ||
cbdd1bea | 1813 | kvm_x86_ops->decache_regs(vcpu); |
46fc1477 | 1814 | |
039576c0 AK |
1815 | io->count -= io->cur_count; |
1816 | io->cur_count = 0; | |
1817 | ||
1818 | if (!io->count) | |
cbdd1bea | 1819 | kvm_x86_ops->skip_emulated_instruction(vcpu); |
039576c0 | 1820 | return 0; |
46fc1477 AK |
1821 | } |
1822 | ||
65619eb5 ED |
1823 | static void kernel_pio(struct kvm_io_device *pio_dev, |
1824 | struct kvm_vcpu *vcpu, | |
1825 | void *pd) | |
74906345 ED |
1826 | { |
1827 | /* TODO: String I/O for in kernel device */ | |
1828 | ||
9cf98828 | 1829 | mutex_lock(&vcpu->kvm->lock); |
74906345 ED |
1830 | if (vcpu->pio.in) |
1831 | kvm_iodevice_read(pio_dev, vcpu->pio.port, | |
1832 | vcpu->pio.size, | |
65619eb5 | 1833 | pd); |
74906345 ED |
1834 | else |
1835 | kvm_iodevice_write(pio_dev, vcpu->pio.port, | |
1836 | vcpu->pio.size, | |
65619eb5 | 1837 | pd); |
9cf98828 | 1838 | mutex_unlock(&vcpu->kvm->lock); |
65619eb5 ED |
1839 | } |
1840 | ||
1841 | static void pio_string_write(struct kvm_io_device *pio_dev, | |
1842 | struct kvm_vcpu *vcpu) | |
1843 | { | |
1844 | struct kvm_pio_request *io = &vcpu->pio; | |
1845 | void *pd = vcpu->pio_data; | |
1846 | int i; | |
1847 | ||
9cf98828 | 1848 | mutex_lock(&vcpu->kvm->lock); |
65619eb5 ED |
1849 | for (i = 0; i < io->cur_count; i++) { |
1850 | kvm_iodevice_write(pio_dev, io->port, | |
1851 | io->size, | |
1852 | pd); | |
1853 | pd += io->size; | |
1854 | } | |
9cf98828 | 1855 | mutex_unlock(&vcpu->kvm->lock); |
74906345 ED |
1856 | } |
1857 | ||
3090dd73 LV |
1858 | int kvm_emulate_pio (struct kvm_vcpu *vcpu, struct kvm_run *run, int in, |
1859 | int size, unsigned port) | |
1860 | { | |
1861 | struct kvm_io_device *pio_dev; | |
1862 | ||
1863 | vcpu->run->exit_reason = KVM_EXIT_IO; | |
1864 | vcpu->run->io.direction = in ? KVM_EXIT_IO_IN : KVM_EXIT_IO_OUT; | |
1865 | vcpu->run->io.size = vcpu->pio.size = size; | |
1866 | vcpu->run->io.data_offset = KVM_PIO_PAGE_OFFSET * PAGE_SIZE; | |
1867 | vcpu->run->io.count = vcpu->pio.count = vcpu->pio.cur_count = 1; | |
1868 | vcpu->run->io.port = vcpu->pio.port = port; | |
1869 | vcpu->pio.in = in; | |
1870 | vcpu->pio.string = 0; | |
1871 | vcpu->pio.down = 0; | |
1872 | vcpu->pio.guest_page_offset = 0; | |
1873 | vcpu->pio.rep = 0; | |
1874 | ||
cbdd1bea | 1875 | kvm_x86_ops->cache_regs(vcpu); |
3090dd73 | 1876 | memcpy(vcpu->pio_data, &vcpu->regs[VCPU_REGS_RAX], 4); |
cbdd1bea | 1877 | kvm_x86_ops->decache_regs(vcpu); |
3090dd73 LV |
1878 | |
1879 | pio_dev = vcpu_find_pio_dev(vcpu, port); | |
1880 | if (pio_dev) { | |
1881 | kernel_pio(pio_dev, vcpu, vcpu->pio_data); | |
1882 | complete_pio(vcpu); | |
1883 | return 1; | |
1884 | } | |
1885 | return 0; | |
1886 | } | |
1887 | EXPORT_SYMBOL_GPL(kvm_emulate_pio); | |
1888 | ||
1889 | int kvm_emulate_pio_string(struct kvm_vcpu *vcpu, struct kvm_run *run, int in, | |
1890 | int size, unsigned long count, int down, | |
039576c0 AK |
1891 | gva_t address, int rep, unsigned port) |
1892 | { | |
1893 | unsigned now, in_page; | |
65619eb5 | 1894 | int i, ret = 0; |
039576c0 AK |
1895 | int nr_pages = 1; |
1896 | struct page *page; | |
74906345 | 1897 | struct kvm_io_device *pio_dev; |
039576c0 AK |
1898 | |
1899 | vcpu->run->exit_reason = KVM_EXIT_IO; | |
1900 | vcpu->run->io.direction = in ? KVM_EXIT_IO_IN : KVM_EXIT_IO_OUT; | |
3090dd73 | 1901 | vcpu->run->io.size = vcpu->pio.size = size; |
039576c0 | 1902 | vcpu->run->io.data_offset = KVM_PIO_PAGE_OFFSET * PAGE_SIZE; |
3090dd73 LV |
1903 | vcpu->run->io.count = vcpu->pio.count = vcpu->pio.cur_count = count; |
1904 | vcpu->run->io.port = vcpu->pio.port = port; | |
039576c0 | 1905 | vcpu->pio.in = in; |
3090dd73 | 1906 | vcpu->pio.string = 1; |
039576c0 AK |
1907 | vcpu->pio.down = down; |
1908 | vcpu->pio.guest_page_offset = offset_in_page(address); | |
1909 | vcpu->pio.rep = rep; | |
1910 | ||
039576c0 | 1911 | if (!count) { |
cbdd1bea | 1912 | kvm_x86_ops->skip_emulated_instruction(vcpu); |
039576c0 AK |
1913 | return 1; |
1914 | } | |
1915 | ||
039576c0 AK |
1916 | if (!down) |
1917 | in_page = PAGE_SIZE - offset_in_page(address); | |
1918 | else | |
1919 | in_page = offset_in_page(address) + size; | |
1920 | now = min(count, (unsigned long)in_page / size); | |
1921 | if (!now) { | |
1922 | /* | |
1923 | * String I/O straddles page boundary. Pin two guest pages | |
1924 | * so that we satisfy atomicity constraints. Do just one | |
1925 | * transaction to avoid complexity. | |
1926 | */ | |
1927 | nr_pages = 2; | |
1928 | now = 1; | |
1929 | } | |
1930 | if (down) { | |
1931 | /* | |
1932 | * String I/O in reverse. Yuck. Kill the guest, fix later. | |
1933 | */ | |
f0242478 | 1934 | pr_unimpl(vcpu, "guest string pio down\n"); |
039576c0 AK |
1935 | inject_gp(vcpu); |
1936 | return 1; | |
1937 | } | |
1938 | vcpu->run->io.count = now; | |
1939 | vcpu->pio.cur_count = now; | |
1940 | ||
1941 | for (i = 0; i < nr_pages; ++i) { | |
11ec2804 | 1942 | mutex_lock(&vcpu->kvm->lock); |
039576c0 AK |
1943 | page = gva_to_page(vcpu, address + i * PAGE_SIZE); |
1944 | if (page) | |
1945 | get_page(page); | |
1946 | vcpu->pio.guest_pages[i] = page; | |
11ec2804 | 1947 | mutex_unlock(&vcpu->kvm->lock); |
039576c0 AK |
1948 | if (!page) { |
1949 | inject_gp(vcpu); | |
1950 | free_pio_guest_pages(vcpu); | |
1951 | return 1; | |
1952 | } | |
1953 | } | |
1954 | ||
3090dd73 | 1955 | pio_dev = vcpu_find_pio_dev(vcpu, port); |
65619eb5 ED |
1956 | if (!vcpu->pio.in) { |
1957 | /* string PIO write */ | |
1958 | ret = pio_copy_data(vcpu); | |
1959 | if (ret >= 0 && pio_dev) { | |
1960 | pio_string_write(pio_dev, vcpu); | |
1961 | complete_pio(vcpu); | |
1962 | if (vcpu->pio.count == 0) | |
1963 | ret = 1; | |
1964 | } | |
1965 | } else if (pio_dev) | |
f0242478 | 1966 | pr_unimpl(vcpu, "no string pio read support yet, " |
65619eb5 ED |
1967 | "port %x size %d count %ld\n", |
1968 | port, size, count); | |
1969 | ||
1970 | return ret; | |
039576c0 | 1971 | } |
3090dd73 | 1972 | EXPORT_SYMBOL_GPL(kvm_emulate_pio_string); |
039576c0 | 1973 | |
04d2cc77 AK |
1974 | /* |
1975 | * Check if userspace requested an interrupt window, and that the | |
1976 | * interrupt window is open. | |
1977 | * | |
1978 | * No need to exit to userspace if we already have an interrupt queued. | |
1979 | */ | |
1980 | static int dm_request_for_irq_injection(struct kvm_vcpu *vcpu, | |
1981 | struct kvm_run *kvm_run) | |
1982 | { | |
1983 | return (!vcpu->irq_summary && | |
1984 | kvm_run->request_interrupt_window && | |
1985 | vcpu->interrupt_window_open && | |
1986 | (kvm_x86_ops->get_rflags(vcpu) & X86_EFLAGS_IF)); | |
1987 | } | |
1988 | ||
1989 | static void post_kvm_run_save(struct kvm_vcpu *vcpu, | |
1990 | struct kvm_run *kvm_run) | |
1991 | { | |
1992 | kvm_run->if_flag = (kvm_x86_ops->get_rflags(vcpu) & X86_EFLAGS_IF) != 0; | |
1993 | kvm_run->cr8 = get_cr8(vcpu); | |
1994 | kvm_run->apic_base = kvm_get_apic_base(vcpu); | |
1995 | if (irqchip_in_kernel(vcpu->kvm)) | |
1996 | kvm_run->ready_for_interrupt_injection = 1; | |
1997 | else | |
1998 | kvm_run->ready_for_interrupt_injection = | |
1999 | (vcpu->interrupt_window_open && | |
2000 | vcpu->irq_summary == 0); | |
2001 | } | |
2002 | ||
2003 | static int __vcpu_run(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run) | |
2004 | { | |
2005 | int r; | |
2006 | ||
2007 | if (unlikely(vcpu->mp_state == VCPU_MP_STATE_SIPI_RECEIVED)) { | |
2008 | printk("vcpu %d received sipi with vector # %x\n", | |
2009 | vcpu->vcpu_id, vcpu->sipi_vector); | |
2010 | kvm_lapic_reset(vcpu); | |
2011 | kvm_x86_ops->vcpu_reset(vcpu); | |
2012 | vcpu->mp_state = VCPU_MP_STATE_RUNNABLE; | |
2013 | } | |
2014 | ||
2015 | preempted: | |
2016 | if (vcpu->guest_debug.enabled) | |
2017 | kvm_x86_ops->guest_debug_pre(vcpu); | |
2018 | ||
2019 | again: | |
2020 | r = kvm_mmu_reload(vcpu); | |
2021 | if (unlikely(r)) | |
2022 | goto out; | |
2023 | ||
2024 | preempt_disable(); | |
2025 | ||
2026 | kvm_x86_ops->prepare_guest_switch(vcpu); | |
2027 | kvm_load_guest_fpu(vcpu); | |
2028 | ||
2029 | local_irq_disable(); | |
2030 | ||
2031 | if (signal_pending(current)) { | |
2032 | local_irq_enable(); | |
2033 | preempt_enable(); | |
2034 | r = -EINTR; | |
2035 | kvm_run->exit_reason = KVM_EXIT_INTR; | |
2036 | ++vcpu->stat.signal_exits; | |
2037 | goto out; | |
2038 | } | |
2039 | ||
2040 | if (irqchip_in_kernel(vcpu->kvm)) | |
2041 | kvm_x86_ops->inject_pending_irq(vcpu); | |
2042 | else if (!vcpu->mmio_read_completed) | |
2043 | kvm_x86_ops->inject_pending_vectors(vcpu, kvm_run); | |
2044 | ||
2045 | vcpu->guest_mode = 1; | |
2046 | ||
2047 | if (vcpu->requests) | |
2048 | if (test_and_clear_bit(KVM_TLB_FLUSH, &vcpu->requests)) | |
2049 | kvm_x86_ops->tlb_flush(vcpu); | |
2050 | ||
2051 | kvm_x86_ops->run(vcpu, kvm_run); | |
2052 | ||
2053 | vcpu->guest_mode = 0; | |
2054 | local_irq_enable(); | |
2055 | ||
2056 | ++vcpu->stat.exits; | |
2057 | ||
2058 | preempt_enable(); | |
2059 | ||
2060 | /* | |
2061 | * Profile KVM exit RIPs: | |
2062 | */ | |
2063 | if (unlikely(prof_on == KVM_PROFILING)) { | |
2064 | kvm_x86_ops->cache_regs(vcpu); | |
2065 | profile_hit(KVM_PROFILING, (void *)vcpu->rip); | |
2066 | } | |
2067 | ||
2068 | r = kvm_x86_ops->handle_exit(kvm_run, vcpu); | |
2069 | ||
2070 | if (r > 0) { | |
2071 | if (dm_request_for_irq_injection(vcpu, kvm_run)) { | |
2072 | r = -EINTR; | |
2073 | kvm_run->exit_reason = KVM_EXIT_INTR; | |
2074 | ++vcpu->stat.request_irq_exits; | |
2075 | goto out; | |
2076 | } | |
2077 | if (!need_resched()) { | |
2078 | ++vcpu->stat.light_exits; | |
2079 | goto again; | |
2080 | } | |
2081 | } | |
2082 | ||
2083 | out: | |
2084 | if (r > 0) { | |
2085 | kvm_resched(vcpu); | |
2086 | goto preempted; | |
2087 | } | |
2088 | ||
2089 | post_kvm_run_save(vcpu, kvm_run); | |
2090 | ||
2091 | return r; | |
2092 | } | |
2093 | ||
2094 | ||
bccf2150 | 2095 | static int kvm_vcpu_ioctl_run(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run) |
6aa8b732 | 2096 | { |
6aa8b732 | 2097 | int r; |
1961d276 | 2098 | sigset_t sigsaved; |
6aa8b732 | 2099 | |
bccf2150 | 2100 | vcpu_load(vcpu); |
6aa8b732 | 2101 | |
c5ec1534 HQ |
2102 | if (unlikely(vcpu->mp_state == VCPU_MP_STATE_UNINITIALIZED)) { |
2103 | kvm_vcpu_block(vcpu); | |
2104 | vcpu_put(vcpu); | |
2105 | return -EAGAIN; | |
2106 | } | |
2107 | ||
1961d276 AK |
2108 | if (vcpu->sigset_active) |
2109 | sigprocmask(SIG_SETMASK, &vcpu->sigset, &sigsaved); | |
2110 | ||
54810342 | 2111 | /* re-sync apic's tpr */ |
5cd4f6fd HQ |
2112 | if (!irqchip_in_kernel(vcpu->kvm)) |
2113 | set_cr8(vcpu, kvm_run->cr8); | |
54810342 | 2114 | |
02c83209 AK |
2115 | if (vcpu->pio.cur_count) { |
2116 | r = complete_pio(vcpu); | |
2117 | if (r) | |
2118 | goto out; | |
2119 | } | |
2120 | ||
2121 | if (vcpu->mmio_needed) { | |
2122 | memcpy(vcpu->mmio_data, kvm_run->mmio.data, 8); | |
2123 | vcpu->mmio_read_completed = 1; | |
2124 | vcpu->mmio_needed = 0; | |
2125 | r = emulate_instruction(vcpu, kvm_run, | |
2126 | vcpu->mmio_fault_cr2, 0); | |
2127 | if (r == EMULATE_DO_MMIO) { | |
2128 | /* | |
2129 | * Read-modify-write. Back to userspace. | |
2130 | */ | |
02c83209 AK |
2131 | r = 0; |
2132 | goto out; | |
46fc1477 | 2133 | } |
6aa8b732 AK |
2134 | } |
2135 | ||
8eb7d334 | 2136 | if (kvm_run->exit_reason == KVM_EXIT_HYPERCALL) { |
cbdd1bea | 2137 | kvm_x86_ops->cache_regs(vcpu); |
b4e63f56 | 2138 | vcpu->regs[VCPU_REGS_RAX] = kvm_run->hypercall.ret; |
cbdd1bea | 2139 | kvm_x86_ops->decache_regs(vcpu); |
b4e63f56 AK |
2140 | } |
2141 | ||
04d2cc77 | 2142 | r = __vcpu_run(vcpu, kvm_run); |
6aa8b732 | 2143 | |
039576c0 | 2144 | out: |
1961d276 AK |
2145 | if (vcpu->sigset_active) |
2146 | sigprocmask(SIG_SETMASK, &sigsaved, NULL); | |
2147 | ||
6aa8b732 AK |
2148 | vcpu_put(vcpu); |
2149 | return r; | |
2150 | } | |
2151 | ||
bccf2150 AK |
2152 | static int kvm_vcpu_ioctl_get_regs(struct kvm_vcpu *vcpu, |
2153 | struct kvm_regs *regs) | |
6aa8b732 | 2154 | { |
bccf2150 | 2155 | vcpu_load(vcpu); |
6aa8b732 | 2156 | |
cbdd1bea | 2157 | kvm_x86_ops->cache_regs(vcpu); |
6aa8b732 AK |
2158 | |
2159 | regs->rax = vcpu->regs[VCPU_REGS_RAX]; | |
2160 | regs->rbx = vcpu->regs[VCPU_REGS_RBX]; | |
2161 | regs->rcx = vcpu->regs[VCPU_REGS_RCX]; | |
2162 | regs->rdx = vcpu->regs[VCPU_REGS_RDX]; | |
2163 | regs->rsi = vcpu->regs[VCPU_REGS_RSI]; | |
2164 | regs->rdi = vcpu->regs[VCPU_REGS_RDI]; | |
2165 | regs->rsp = vcpu->regs[VCPU_REGS_RSP]; | |
2166 | regs->rbp = vcpu->regs[VCPU_REGS_RBP]; | |
05b3e0c2 | 2167 | #ifdef CONFIG_X86_64 |
6aa8b732 AK |
2168 | regs->r8 = vcpu->regs[VCPU_REGS_R8]; |
2169 | regs->r9 = vcpu->regs[VCPU_REGS_R9]; | |
2170 | regs->r10 = vcpu->regs[VCPU_REGS_R10]; | |
2171 | regs->r11 = vcpu->regs[VCPU_REGS_R11]; | |
2172 | regs->r12 = vcpu->regs[VCPU_REGS_R12]; | |
2173 | regs->r13 = vcpu->regs[VCPU_REGS_R13]; | |
2174 | regs->r14 = vcpu->regs[VCPU_REGS_R14]; | |
2175 | regs->r15 = vcpu->regs[VCPU_REGS_R15]; | |
2176 | #endif | |
2177 | ||
2178 | regs->rip = vcpu->rip; | |
cbdd1bea | 2179 | regs->rflags = kvm_x86_ops->get_rflags(vcpu); |
6aa8b732 AK |
2180 | |
2181 | /* | |
2182 | * Don't leak debug flags in case they were set for guest debugging | |
2183 | */ | |
2184 | if (vcpu->guest_debug.enabled && vcpu->guest_debug.singlestep) | |
2185 | regs->rflags &= ~(X86_EFLAGS_TF | X86_EFLAGS_RF); | |
2186 | ||
2187 | vcpu_put(vcpu); | |
2188 | ||
2189 | return 0; | |
2190 | } | |
2191 | ||
bccf2150 AK |
2192 | static int kvm_vcpu_ioctl_set_regs(struct kvm_vcpu *vcpu, |
2193 | struct kvm_regs *regs) | |
6aa8b732 | 2194 | { |
bccf2150 | 2195 | vcpu_load(vcpu); |
6aa8b732 AK |
2196 | |
2197 | vcpu->regs[VCPU_REGS_RAX] = regs->rax; | |
2198 | vcpu->regs[VCPU_REGS_RBX] = regs->rbx; | |
2199 | vcpu->regs[VCPU_REGS_RCX] = regs->rcx; | |
2200 | vcpu->regs[VCPU_REGS_RDX] = regs->rdx; | |
2201 | vcpu->regs[VCPU_REGS_RSI] = regs->rsi; | |
2202 | vcpu->regs[VCPU_REGS_RDI] = regs->rdi; | |
2203 | vcpu->regs[VCPU_REGS_RSP] = regs->rsp; | |
2204 | vcpu->regs[VCPU_REGS_RBP] = regs->rbp; | |
05b3e0c2 | 2205 | #ifdef CONFIG_X86_64 |
6aa8b732 AK |
2206 | vcpu->regs[VCPU_REGS_R8] = regs->r8; |
2207 | vcpu->regs[VCPU_REGS_R9] = regs->r9; | |
2208 | vcpu->regs[VCPU_REGS_R10] = regs->r10; | |
2209 | vcpu->regs[VCPU_REGS_R11] = regs->r11; | |
2210 | vcpu->regs[VCPU_REGS_R12] = regs->r12; | |
2211 | vcpu->regs[VCPU_REGS_R13] = regs->r13; | |
2212 | vcpu->regs[VCPU_REGS_R14] = regs->r14; | |
2213 | vcpu->regs[VCPU_REGS_R15] = regs->r15; | |
2214 | #endif | |
2215 | ||
2216 | vcpu->rip = regs->rip; | |
cbdd1bea | 2217 | kvm_x86_ops->set_rflags(vcpu, regs->rflags); |
6aa8b732 | 2218 | |
cbdd1bea | 2219 | kvm_x86_ops->decache_regs(vcpu); |
6aa8b732 AK |
2220 | |
2221 | vcpu_put(vcpu); | |
2222 | ||
2223 | return 0; | |
2224 | } | |
2225 | ||
2226 | static void get_segment(struct kvm_vcpu *vcpu, | |
2227 | struct kvm_segment *var, int seg) | |
2228 | { | |
cbdd1bea | 2229 | return kvm_x86_ops->get_segment(vcpu, var, seg); |
6aa8b732 AK |
2230 | } |
2231 | ||
bccf2150 AK |
2232 | static int kvm_vcpu_ioctl_get_sregs(struct kvm_vcpu *vcpu, |
2233 | struct kvm_sregs *sregs) | |
6aa8b732 | 2234 | { |
6aa8b732 | 2235 | struct descriptor_table dt; |
2a8067f1 | 2236 | int pending_vec; |
6aa8b732 | 2237 | |
bccf2150 | 2238 | vcpu_load(vcpu); |
6aa8b732 AK |
2239 | |
2240 | get_segment(vcpu, &sregs->cs, VCPU_SREG_CS); | |
2241 | get_segment(vcpu, &sregs->ds, VCPU_SREG_DS); | |
2242 | get_segment(vcpu, &sregs->es, VCPU_SREG_ES); | |
2243 | get_segment(vcpu, &sregs->fs, VCPU_SREG_FS); | |
2244 | get_segment(vcpu, &sregs->gs, VCPU_SREG_GS); | |
2245 | get_segment(vcpu, &sregs->ss, VCPU_SREG_SS); | |
2246 | ||
2247 | get_segment(vcpu, &sregs->tr, VCPU_SREG_TR); | |
2248 | get_segment(vcpu, &sregs->ldt, VCPU_SREG_LDTR); | |
2249 | ||
cbdd1bea | 2250 | kvm_x86_ops->get_idt(vcpu, &dt); |
6aa8b732 AK |
2251 | sregs->idt.limit = dt.limit; |
2252 | sregs->idt.base = dt.base; | |
cbdd1bea | 2253 | kvm_x86_ops->get_gdt(vcpu, &dt); |
6aa8b732 AK |
2254 | sregs->gdt.limit = dt.limit; |
2255 | sregs->gdt.base = dt.base; | |
2256 | ||
cbdd1bea | 2257 | kvm_x86_ops->decache_cr4_guest_bits(vcpu); |
6aa8b732 AK |
2258 | sregs->cr0 = vcpu->cr0; |
2259 | sregs->cr2 = vcpu->cr2; | |
2260 | sregs->cr3 = vcpu->cr3; | |
2261 | sregs->cr4 = vcpu->cr4; | |
7017fc3d | 2262 | sregs->cr8 = get_cr8(vcpu); |
6aa8b732 | 2263 | sregs->efer = vcpu->shadow_efer; |
7017fc3d | 2264 | sregs->apic_base = kvm_get_apic_base(vcpu); |
6aa8b732 | 2265 | |
2a8067f1 | 2266 | if (irqchip_in_kernel(vcpu->kvm)) { |
c52fb35a HQ |
2267 | memset(sregs->interrupt_bitmap, 0, |
2268 | sizeof sregs->interrupt_bitmap); | |
cbdd1bea | 2269 | pending_vec = kvm_x86_ops->get_irq(vcpu); |
2a8067f1 ED |
2270 | if (pending_vec >= 0) |
2271 | set_bit(pending_vec, (unsigned long *)sregs->interrupt_bitmap); | |
2272 | } else | |
c52fb35a HQ |
2273 | memcpy(sregs->interrupt_bitmap, vcpu->irq_pending, |
2274 | sizeof sregs->interrupt_bitmap); | |
6aa8b732 AK |
2275 | |
2276 | vcpu_put(vcpu); | |
2277 | ||
2278 | return 0; | |
2279 | } | |
2280 | ||
2281 | static void set_segment(struct kvm_vcpu *vcpu, | |
2282 | struct kvm_segment *var, int seg) | |
2283 | { | |
cbdd1bea | 2284 | return kvm_x86_ops->set_segment(vcpu, var, seg); |
6aa8b732 AK |
2285 | } |
2286 | ||
bccf2150 AK |
2287 | static int kvm_vcpu_ioctl_set_sregs(struct kvm_vcpu *vcpu, |
2288 | struct kvm_sregs *sregs) | |
6aa8b732 | 2289 | { |
6aa8b732 | 2290 | int mmu_reset_needed = 0; |
2a8067f1 | 2291 | int i, pending_vec, max_bits; |
6aa8b732 AK |
2292 | struct descriptor_table dt; |
2293 | ||
bccf2150 | 2294 | vcpu_load(vcpu); |
6aa8b732 | 2295 | |
6aa8b732 AK |
2296 | dt.limit = sregs->idt.limit; |
2297 | dt.base = sregs->idt.base; | |
cbdd1bea | 2298 | kvm_x86_ops->set_idt(vcpu, &dt); |
6aa8b732 AK |
2299 | dt.limit = sregs->gdt.limit; |
2300 | dt.base = sregs->gdt.base; | |
cbdd1bea | 2301 | kvm_x86_ops->set_gdt(vcpu, &dt); |
6aa8b732 AK |
2302 | |
2303 | vcpu->cr2 = sregs->cr2; | |
2304 | mmu_reset_needed |= vcpu->cr3 != sregs->cr3; | |
2305 | vcpu->cr3 = sregs->cr3; | |
2306 | ||
7017fc3d | 2307 | set_cr8(vcpu, sregs->cr8); |
6aa8b732 AK |
2308 | |
2309 | mmu_reset_needed |= vcpu->shadow_efer != sregs->efer; | |
05b3e0c2 | 2310 | #ifdef CONFIG_X86_64 |
cbdd1bea | 2311 | kvm_x86_ops->set_efer(vcpu, sregs->efer); |
6aa8b732 | 2312 | #endif |
7017fc3d | 2313 | kvm_set_apic_base(vcpu, sregs->apic_base); |
6aa8b732 | 2314 | |
cbdd1bea | 2315 | kvm_x86_ops->decache_cr4_guest_bits(vcpu); |
399badf3 | 2316 | |
6aa8b732 | 2317 | mmu_reset_needed |= vcpu->cr0 != sregs->cr0; |
81f50e3b | 2318 | vcpu->cr0 = sregs->cr0; |
cbdd1bea | 2319 | kvm_x86_ops->set_cr0(vcpu, sregs->cr0); |
6aa8b732 AK |
2320 | |
2321 | mmu_reset_needed |= vcpu->cr4 != sregs->cr4; | |
cbdd1bea | 2322 | kvm_x86_ops->set_cr4(vcpu, sregs->cr4); |
1b0973bd AK |
2323 | if (!is_long_mode(vcpu) && is_pae(vcpu)) |
2324 | load_pdptrs(vcpu, vcpu->cr3); | |
6aa8b732 AK |
2325 | |
2326 | if (mmu_reset_needed) | |
2327 | kvm_mmu_reset_context(vcpu); | |
2328 | ||
c52fb35a HQ |
2329 | if (!irqchip_in_kernel(vcpu->kvm)) { |
2330 | memcpy(vcpu->irq_pending, sregs->interrupt_bitmap, | |
2331 | sizeof vcpu->irq_pending); | |
2332 | vcpu->irq_summary = 0; | |
2333 | for (i = 0; i < ARRAY_SIZE(vcpu->irq_pending); ++i) | |
2334 | if (vcpu->irq_pending[i]) | |
2335 | __set_bit(i, &vcpu->irq_summary); | |
2a8067f1 ED |
2336 | } else { |
2337 | max_bits = (sizeof sregs->interrupt_bitmap) << 3; | |
2338 | pending_vec = find_first_bit( | |
2339 | (const unsigned long *)sregs->interrupt_bitmap, | |
2340 | max_bits); | |
2341 | /* Only pending external irq is handled here */ | |
2342 | if (pending_vec < max_bits) { | |
cbdd1bea | 2343 | kvm_x86_ops->set_irq(vcpu, pending_vec); |
2a8067f1 ED |
2344 | printk("Set back pending irq %d\n", pending_vec); |
2345 | } | |
c52fb35a | 2346 | } |
6aa8b732 | 2347 | |
024aa1c0 AK |
2348 | set_segment(vcpu, &sregs->cs, VCPU_SREG_CS); |
2349 | set_segment(vcpu, &sregs->ds, VCPU_SREG_DS); | |
2350 | set_segment(vcpu, &sregs->es, VCPU_SREG_ES); | |
2351 | set_segment(vcpu, &sregs->fs, VCPU_SREG_FS); | |
2352 | set_segment(vcpu, &sregs->gs, VCPU_SREG_GS); | |
2353 | set_segment(vcpu, &sregs->ss, VCPU_SREG_SS); | |
2354 | ||
2355 | set_segment(vcpu, &sregs->tr, VCPU_SREG_TR); | |
2356 | set_segment(vcpu, &sregs->ldt, VCPU_SREG_LDTR); | |
2357 | ||
6aa8b732 AK |
2358 | vcpu_put(vcpu); |
2359 | ||
2360 | return 0; | |
2361 | } | |
2362 | ||
1747fb71 RR |
2363 | void kvm_get_cs_db_l_bits(struct kvm_vcpu *vcpu, int *db, int *l) |
2364 | { | |
2365 | struct kvm_segment cs; | |
2366 | ||
2367 | get_segment(vcpu, &cs, VCPU_SREG_CS); | |
2368 | *db = cs.db; | |
2369 | *l = cs.l; | |
2370 | } | |
2371 | EXPORT_SYMBOL_GPL(kvm_get_cs_db_l_bits); | |
2372 | ||
6aa8b732 AK |
2373 | /* |
2374 | * List of msr numbers which we expose to userspace through KVM_GET_MSRS | |
2375 | * and KVM_SET_MSRS, and KVM_GET_MSR_INDEX_LIST. | |
bf591b24 MR |
2376 | * |
2377 | * This list is modified at module load time to reflect the | |
2378 | * capabilities of the host cpu. | |
6aa8b732 AK |
2379 | */ |
2380 | static u32 msrs_to_save[] = { | |
2381 | MSR_IA32_SYSENTER_CS, MSR_IA32_SYSENTER_ESP, MSR_IA32_SYSENTER_EIP, | |
2382 | MSR_K6_STAR, | |
05b3e0c2 | 2383 | #ifdef CONFIG_X86_64 |
6aa8b732 AK |
2384 | MSR_CSTAR, MSR_KERNEL_GS_BASE, MSR_SYSCALL_MASK, MSR_LSTAR, |
2385 | #endif | |
2386 | MSR_IA32_TIME_STAMP_COUNTER, | |
2387 | }; | |
2388 | ||
bf591b24 MR |
2389 | static unsigned num_msrs_to_save; |
2390 | ||
6f00e68f AK |
2391 | static u32 emulated_msrs[] = { |
2392 | MSR_IA32_MISC_ENABLE, | |
2393 | }; | |
2394 | ||
bf591b24 MR |
2395 | static __init void kvm_init_msr_list(void) |
2396 | { | |
2397 | u32 dummy[2]; | |
2398 | unsigned i, j; | |
2399 | ||
2400 | for (i = j = 0; i < ARRAY_SIZE(msrs_to_save); i++) { | |
2401 | if (rdmsr_safe(msrs_to_save[i], &dummy[0], &dummy[1]) < 0) | |
2402 | continue; | |
2403 | if (j < i) | |
2404 | msrs_to_save[j] = msrs_to_save[i]; | |
2405 | j++; | |
2406 | } | |
2407 | num_msrs_to_save = j; | |
2408 | } | |
6aa8b732 AK |
2409 | |
2410 | /* | |
2411 | * Adapt set_msr() to msr_io()'s calling convention | |
2412 | */ | |
2413 | static int do_set_msr(struct kvm_vcpu *vcpu, unsigned index, u64 *data) | |
2414 | { | |
35f3f286 | 2415 | return kvm_set_msr(vcpu, index, *data); |
6aa8b732 AK |
2416 | } |
2417 | ||
2418 | /* | |
2419 | * Read or write a bunch of msrs. All parameters are kernel addresses. | |
2420 | * | |
2421 | * @return number of msrs set successfully. | |
2422 | */ | |
bccf2150 | 2423 | static int __msr_io(struct kvm_vcpu *vcpu, struct kvm_msrs *msrs, |
6aa8b732 AK |
2424 | struct kvm_msr_entry *entries, |
2425 | int (*do_msr)(struct kvm_vcpu *vcpu, | |
2426 | unsigned index, u64 *data)) | |
2427 | { | |
6aa8b732 AK |
2428 | int i; |
2429 | ||
bccf2150 | 2430 | vcpu_load(vcpu); |
6aa8b732 AK |
2431 | |
2432 | for (i = 0; i < msrs->nmsrs; ++i) | |
2433 | if (do_msr(vcpu, entries[i].index, &entries[i].data)) | |
2434 | break; | |
2435 | ||
2436 | vcpu_put(vcpu); | |
2437 | ||
2438 | return i; | |
2439 | } | |
2440 | ||
2441 | /* | |
2442 | * Read or write a bunch of msrs. Parameters are user addresses. | |
2443 | * | |
2444 | * @return number of msrs set successfully. | |
2445 | */ | |
bccf2150 | 2446 | static int msr_io(struct kvm_vcpu *vcpu, struct kvm_msrs __user *user_msrs, |
6aa8b732 AK |
2447 | int (*do_msr)(struct kvm_vcpu *vcpu, |
2448 | unsigned index, u64 *data), | |
2449 | int writeback) | |
2450 | { | |
2451 | struct kvm_msrs msrs; | |
2452 | struct kvm_msr_entry *entries; | |
2453 | int r, n; | |
2454 | unsigned size; | |
2455 | ||
2456 | r = -EFAULT; | |
2457 | if (copy_from_user(&msrs, user_msrs, sizeof msrs)) | |
2458 | goto out; | |
2459 | ||
2460 | r = -E2BIG; | |
2461 | if (msrs.nmsrs >= MAX_IO_MSRS) | |
2462 | goto out; | |
2463 | ||
2464 | r = -ENOMEM; | |
2465 | size = sizeof(struct kvm_msr_entry) * msrs.nmsrs; | |
2466 | entries = vmalloc(size); | |
2467 | if (!entries) | |
2468 | goto out; | |
2469 | ||
2470 | r = -EFAULT; | |
2471 | if (copy_from_user(entries, user_msrs->entries, size)) | |
2472 | goto out_free; | |
2473 | ||
bccf2150 | 2474 | r = n = __msr_io(vcpu, &msrs, entries, do_msr); |
6aa8b732 AK |
2475 | if (r < 0) |
2476 | goto out_free; | |
2477 | ||
2478 | r = -EFAULT; | |
2479 | if (writeback && copy_to_user(user_msrs->entries, entries, size)) | |
2480 | goto out_free; | |
2481 | ||
2482 | r = n; | |
2483 | ||
2484 | out_free: | |
2485 | vfree(entries); | |
2486 | out: | |
2487 | return r; | |
2488 | } | |
2489 | ||
2490 | /* | |
2491 | * Translate a guest virtual address to a guest physical address. | |
2492 | */ | |
bccf2150 AK |
2493 | static int kvm_vcpu_ioctl_translate(struct kvm_vcpu *vcpu, |
2494 | struct kvm_translation *tr) | |
6aa8b732 AK |
2495 | { |
2496 | unsigned long vaddr = tr->linear_address; | |
6aa8b732 AK |
2497 | gpa_t gpa; |
2498 | ||
bccf2150 | 2499 | vcpu_load(vcpu); |
11ec2804 | 2500 | mutex_lock(&vcpu->kvm->lock); |
6aa8b732 AK |
2501 | gpa = vcpu->mmu.gva_to_gpa(vcpu, vaddr); |
2502 | tr->physical_address = gpa; | |
2503 | tr->valid = gpa != UNMAPPED_GVA; | |
2504 | tr->writeable = 1; | |
2505 | tr->usermode = 0; | |
11ec2804 | 2506 | mutex_unlock(&vcpu->kvm->lock); |
6aa8b732 AK |
2507 | vcpu_put(vcpu); |
2508 | ||
2509 | return 0; | |
2510 | } | |
2511 | ||
bccf2150 AK |
2512 | static int kvm_vcpu_ioctl_interrupt(struct kvm_vcpu *vcpu, |
2513 | struct kvm_interrupt *irq) | |
6aa8b732 | 2514 | { |
6aa8b732 AK |
2515 | if (irq->irq < 0 || irq->irq >= 256) |
2516 | return -EINVAL; | |
97222cc8 ED |
2517 | if (irqchip_in_kernel(vcpu->kvm)) |
2518 | return -ENXIO; | |
bccf2150 | 2519 | vcpu_load(vcpu); |
6aa8b732 AK |
2520 | |
2521 | set_bit(irq->irq, vcpu->irq_pending); | |
2522 | set_bit(irq->irq / BITS_PER_LONG, &vcpu->irq_summary); | |
2523 | ||
2524 | vcpu_put(vcpu); | |
2525 | ||
2526 | return 0; | |
2527 | } | |
2528 | ||
bccf2150 AK |
2529 | static int kvm_vcpu_ioctl_debug_guest(struct kvm_vcpu *vcpu, |
2530 | struct kvm_debug_guest *dbg) | |
6aa8b732 | 2531 | { |
6aa8b732 AK |
2532 | int r; |
2533 | ||
bccf2150 | 2534 | vcpu_load(vcpu); |
6aa8b732 | 2535 | |
cbdd1bea | 2536 | r = kvm_x86_ops->set_guest_debug(vcpu, dbg); |
6aa8b732 AK |
2537 | |
2538 | vcpu_put(vcpu); | |
2539 | ||
2540 | return r; | |
2541 | } | |
2542 | ||
9a2bb7f4 AK |
2543 | static struct page *kvm_vcpu_nopage(struct vm_area_struct *vma, |
2544 | unsigned long address, | |
2545 | int *type) | |
2546 | { | |
2547 | struct kvm_vcpu *vcpu = vma->vm_file->private_data; | |
2548 | unsigned long pgoff; | |
2549 | struct page *page; | |
2550 | ||
9a2bb7f4 | 2551 | pgoff = ((address - vma->vm_start) >> PAGE_SHIFT) + vma->vm_pgoff; |
039576c0 AK |
2552 | if (pgoff == 0) |
2553 | page = virt_to_page(vcpu->run); | |
2554 | else if (pgoff == KVM_PIO_PAGE_OFFSET) | |
2555 | page = virt_to_page(vcpu->pio_data); | |
2556 | else | |
9a2bb7f4 | 2557 | return NOPAGE_SIGBUS; |
9a2bb7f4 | 2558 | get_page(page); |
cd0d9137 NAQ |
2559 | if (type != NULL) |
2560 | *type = VM_FAULT_MINOR; | |
2561 | ||
9a2bb7f4 AK |
2562 | return page; |
2563 | } | |
2564 | ||
2565 | static struct vm_operations_struct kvm_vcpu_vm_ops = { | |
2566 | .nopage = kvm_vcpu_nopage, | |
2567 | }; | |
2568 | ||
2569 | static int kvm_vcpu_mmap(struct file *file, struct vm_area_struct *vma) | |
2570 | { | |
2571 | vma->vm_ops = &kvm_vcpu_vm_ops; | |
2572 | return 0; | |
2573 | } | |
2574 | ||
bccf2150 AK |
2575 | static int kvm_vcpu_release(struct inode *inode, struct file *filp) |
2576 | { | |
2577 | struct kvm_vcpu *vcpu = filp->private_data; | |
2578 | ||
2579 | fput(vcpu->kvm->filp); | |
2580 | return 0; | |
2581 | } | |
2582 | ||
2583 | static struct file_operations kvm_vcpu_fops = { | |
2584 | .release = kvm_vcpu_release, | |
2585 | .unlocked_ioctl = kvm_vcpu_ioctl, | |
2586 | .compat_ioctl = kvm_vcpu_ioctl, | |
9a2bb7f4 | 2587 | .mmap = kvm_vcpu_mmap, |
bccf2150 AK |
2588 | }; |
2589 | ||
2590 | /* | |
2591 | * Allocates an inode for the vcpu. | |
2592 | */ | |
2593 | static int create_vcpu_fd(struct kvm_vcpu *vcpu) | |
2594 | { | |
2595 | int fd, r; | |
2596 | struct inode *inode; | |
2597 | struct file *file; | |
2598 | ||
d6d28168 AK |
2599 | r = anon_inode_getfd(&fd, &inode, &file, |
2600 | "kvm-vcpu", &kvm_vcpu_fops, vcpu); | |
2601 | if (r) | |
2602 | return r; | |
bccf2150 | 2603 | atomic_inc(&vcpu->kvm->filp->f_count); |
bccf2150 | 2604 | return fd; |
bccf2150 AK |
2605 | } |
2606 | ||
c5ea7660 AK |
2607 | /* |
2608 | * Creates some virtual cpus. Good luck creating more than one. | |
2609 | */ | |
2610 | static int kvm_vm_ioctl_create_vcpu(struct kvm *kvm, int n) | |
2611 | { | |
2612 | int r; | |
2613 | struct kvm_vcpu *vcpu; | |
2614 | ||
c5ea7660 | 2615 | if (!valid_vcpu(n)) |
fb3f0f51 | 2616 | return -EINVAL; |
c5ea7660 | 2617 | |
cbdd1bea | 2618 | vcpu = kvm_x86_ops->vcpu_create(kvm, n); |
fb3f0f51 RR |
2619 | if (IS_ERR(vcpu)) |
2620 | return PTR_ERR(vcpu); | |
c5ea7660 | 2621 | |
15ad7146 AK |
2622 | preempt_notifier_init(&vcpu->preempt_notifier, &kvm_preempt_ops); |
2623 | ||
b114b080 RR |
2624 | /* We do fxsave: this must be aligned. */ |
2625 | BUG_ON((unsigned long)&vcpu->host_fx_image & 0xF); | |
2626 | ||
fb3f0f51 | 2627 | vcpu_load(vcpu); |
c5ea7660 | 2628 | r = kvm_mmu_setup(vcpu); |
c5ea7660 | 2629 | vcpu_put(vcpu); |
c5ea7660 | 2630 | if (r < 0) |
fb3f0f51 RR |
2631 | goto free_vcpu; |
2632 | ||
11ec2804 | 2633 | mutex_lock(&kvm->lock); |
fb3f0f51 RR |
2634 | if (kvm->vcpus[n]) { |
2635 | r = -EEXIST; | |
11ec2804 | 2636 | mutex_unlock(&kvm->lock); |
fb3f0f51 RR |
2637 | goto mmu_unload; |
2638 | } | |
2639 | kvm->vcpus[n] = vcpu; | |
11ec2804 | 2640 | mutex_unlock(&kvm->lock); |
c5ea7660 | 2641 | |
fb3f0f51 | 2642 | /* Now it's all set up, let userspace reach it */ |
bccf2150 AK |
2643 | r = create_vcpu_fd(vcpu); |
2644 | if (r < 0) | |
fb3f0f51 RR |
2645 | goto unlink; |
2646 | return r; | |
39c3b86e | 2647 | |
fb3f0f51 | 2648 | unlink: |
11ec2804 | 2649 | mutex_lock(&kvm->lock); |
fb3f0f51 | 2650 | kvm->vcpus[n] = NULL; |
11ec2804 | 2651 | mutex_unlock(&kvm->lock); |
a2fa3e9f | 2652 | |
fb3f0f51 RR |
2653 | mmu_unload: |
2654 | vcpu_load(vcpu); | |
2655 | kvm_mmu_unload(vcpu); | |
2656 | vcpu_put(vcpu); | |
c5ea7660 | 2657 | |
fb3f0f51 | 2658 | free_vcpu: |
cbdd1bea | 2659 | kvm_x86_ops->vcpu_free(vcpu); |
c5ea7660 AK |
2660 | return r; |
2661 | } | |
2662 | ||
2cc51560 ED |
2663 | static void cpuid_fix_nx_cap(struct kvm_vcpu *vcpu) |
2664 | { | |
2665 | u64 efer; | |
2666 | int i; | |
2667 | struct kvm_cpuid_entry *e, *entry; | |
2668 | ||
2669 | rdmsrl(MSR_EFER, efer); | |
2670 | entry = NULL; | |
2671 | for (i = 0; i < vcpu->cpuid_nent; ++i) { | |
2672 | e = &vcpu->cpuid_entries[i]; | |
2673 | if (e->function == 0x80000001) { | |
2674 | entry = e; | |
2675 | break; | |
2676 | } | |
2677 | } | |
4c981b43 | 2678 | if (entry && (entry->edx & (1 << 20)) && !(efer & EFER_NX)) { |
2cc51560 | 2679 | entry->edx &= ~(1 << 20); |
4c981b43 | 2680 | printk(KERN_INFO "kvm: guest NX capability removed\n"); |
2cc51560 ED |
2681 | } |
2682 | } | |
2683 | ||
06465c5a AK |
2684 | static int kvm_vcpu_ioctl_set_cpuid(struct kvm_vcpu *vcpu, |
2685 | struct kvm_cpuid *cpuid, | |
2686 | struct kvm_cpuid_entry __user *entries) | |
2687 | { | |
2688 | int r; | |
2689 | ||
2690 | r = -E2BIG; | |
2691 | if (cpuid->nent > KVM_MAX_CPUID_ENTRIES) | |
2692 | goto out; | |
2693 | r = -EFAULT; | |
2694 | if (copy_from_user(&vcpu->cpuid_entries, entries, | |
2695 | cpuid->nent * sizeof(struct kvm_cpuid_entry))) | |
2696 | goto out; | |
2697 | vcpu->cpuid_nent = cpuid->nent; | |
2cc51560 | 2698 | cpuid_fix_nx_cap(vcpu); |
06465c5a AK |
2699 | return 0; |
2700 | ||
2701 | out: | |
2702 | return r; | |
2703 | } | |
2704 | ||
1961d276 AK |
2705 | static int kvm_vcpu_ioctl_set_sigmask(struct kvm_vcpu *vcpu, sigset_t *sigset) |
2706 | { | |
2707 | if (sigset) { | |
2708 | sigdelsetmask(sigset, sigmask(SIGKILL)|sigmask(SIGSTOP)); | |
2709 | vcpu->sigset_active = 1; | |
2710 | vcpu->sigset = *sigset; | |
2711 | } else | |
2712 | vcpu->sigset_active = 0; | |
2713 | return 0; | |
2714 | } | |
2715 | ||
b8836737 AK |
2716 | /* |
2717 | * fxsave fpu state. Taken from x86_64/processor.h. To be killed when | |
2718 | * we have asm/x86/processor.h | |
2719 | */ | |
2720 | struct fxsave { | |
2721 | u16 cwd; | |
2722 | u16 swd; | |
2723 | u16 twd; | |
2724 | u16 fop; | |
2725 | u64 rip; | |
2726 | u64 rdp; | |
2727 | u32 mxcsr; | |
2728 | u32 mxcsr_mask; | |
2729 | u32 st_space[32]; /* 8*16 bytes for each FP-reg = 128 bytes */ | |
2730 | #ifdef CONFIG_X86_64 | |
2731 | u32 xmm_space[64]; /* 16*16 bytes for each XMM-reg = 256 bytes */ | |
2732 | #else | |
2733 | u32 xmm_space[32]; /* 8*16 bytes for each XMM-reg = 128 bytes */ | |
2734 | #endif | |
2735 | }; | |
2736 | ||
2737 | static int kvm_vcpu_ioctl_get_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu) | |
2738 | { | |
b114b080 | 2739 | struct fxsave *fxsave = (struct fxsave *)&vcpu->guest_fx_image; |
b8836737 AK |
2740 | |
2741 | vcpu_load(vcpu); | |
2742 | ||
2743 | memcpy(fpu->fpr, fxsave->st_space, 128); | |
2744 | fpu->fcw = fxsave->cwd; | |
2745 | fpu->fsw = fxsave->swd; | |
2746 | fpu->ftwx = fxsave->twd; | |
2747 | fpu->last_opcode = fxsave->fop; | |
2748 | fpu->last_ip = fxsave->rip; | |
2749 | fpu->last_dp = fxsave->rdp; | |
2750 | memcpy(fpu->xmm, fxsave->xmm_space, sizeof fxsave->xmm_space); | |
2751 | ||
2752 | vcpu_put(vcpu); | |
2753 | ||
2754 | return 0; | |
2755 | } | |
2756 | ||
2757 | static int kvm_vcpu_ioctl_set_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu) | |
2758 | { | |
b114b080 | 2759 | struct fxsave *fxsave = (struct fxsave *)&vcpu->guest_fx_image; |
b8836737 AK |
2760 | |
2761 | vcpu_load(vcpu); | |
2762 | ||
2763 | memcpy(fxsave->st_space, fpu->fpr, 128); | |
2764 | fxsave->cwd = fpu->fcw; | |
2765 | fxsave->swd = fpu->fsw; | |
2766 | fxsave->twd = fpu->ftwx; | |
2767 | fxsave->fop = fpu->last_opcode; | |
2768 | fxsave->rip = fpu->last_ip; | |
2769 | fxsave->rdp = fpu->last_dp; | |
2770 | memcpy(fxsave->xmm_space, fpu->xmm, sizeof fxsave->xmm_space); | |
2771 | ||
2772 | vcpu_put(vcpu); | |
2773 | ||
2774 | return 0; | |
2775 | } | |
2776 | ||
96ad2cc6 ED |
2777 | static int kvm_vcpu_ioctl_get_lapic(struct kvm_vcpu *vcpu, |
2778 | struct kvm_lapic_state *s) | |
2779 | { | |
2780 | vcpu_load(vcpu); | |
2781 | memcpy(s->regs, vcpu->apic->regs, sizeof *s); | |
2782 | vcpu_put(vcpu); | |
2783 | ||
2784 | return 0; | |
2785 | } | |
2786 | ||
2787 | static int kvm_vcpu_ioctl_set_lapic(struct kvm_vcpu *vcpu, | |
2788 | struct kvm_lapic_state *s) | |
2789 | { | |
2790 | vcpu_load(vcpu); | |
2791 | memcpy(vcpu->apic->regs, s->regs, sizeof *s); | |
2792 | kvm_apic_post_state_restore(vcpu); | |
2793 | vcpu_put(vcpu); | |
2794 | ||
2795 | return 0; | |
2796 | } | |
2797 | ||
bccf2150 AK |
2798 | static long kvm_vcpu_ioctl(struct file *filp, |
2799 | unsigned int ioctl, unsigned long arg) | |
6aa8b732 | 2800 | { |
bccf2150 | 2801 | struct kvm_vcpu *vcpu = filp->private_data; |
2f366987 | 2802 | void __user *argp = (void __user *)arg; |
6aa8b732 AK |
2803 | int r = -EINVAL; |
2804 | ||
2805 | switch (ioctl) { | |
9a2bb7f4 | 2806 | case KVM_RUN: |
f0fe5108 AK |
2807 | r = -EINVAL; |
2808 | if (arg) | |
2809 | goto out; | |
9a2bb7f4 | 2810 | r = kvm_vcpu_ioctl_run(vcpu, vcpu->run); |
6aa8b732 | 2811 | break; |
6aa8b732 AK |
2812 | case KVM_GET_REGS: { |
2813 | struct kvm_regs kvm_regs; | |
2814 | ||
bccf2150 AK |
2815 | memset(&kvm_regs, 0, sizeof kvm_regs); |
2816 | r = kvm_vcpu_ioctl_get_regs(vcpu, &kvm_regs); | |
6aa8b732 AK |
2817 | if (r) |
2818 | goto out; | |
2819 | r = -EFAULT; | |
2f366987 | 2820 | if (copy_to_user(argp, &kvm_regs, sizeof kvm_regs)) |
6aa8b732 AK |
2821 | goto out; |
2822 | r = 0; | |
2823 | break; | |
2824 | } | |
2825 | case KVM_SET_REGS: { | |
2826 | struct kvm_regs kvm_regs; | |
2827 | ||
2828 | r = -EFAULT; | |
2f366987 | 2829 | if (copy_from_user(&kvm_regs, argp, sizeof kvm_regs)) |
6aa8b732 | 2830 | goto out; |
bccf2150 | 2831 | r = kvm_vcpu_ioctl_set_regs(vcpu, &kvm_regs); |
6aa8b732 AK |
2832 | if (r) |
2833 | goto out; | |
2834 | r = 0; | |
2835 | break; | |
2836 | } | |
2837 | case KVM_GET_SREGS: { | |
2838 | struct kvm_sregs kvm_sregs; | |
2839 | ||
bccf2150 AK |
2840 | memset(&kvm_sregs, 0, sizeof kvm_sregs); |
2841 | r = kvm_vcpu_ioctl_get_sregs(vcpu, &kvm_sregs); | |
6aa8b732 AK |
2842 | if (r) |
2843 | goto out; | |
2844 | r = -EFAULT; | |
2f366987 | 2845 | if (copy_to_user(argp, &kvm_sregs, sizeof kvm_sregs)) |
6aa8b732 AK |
2846 | goto out; |
2847 | r = 0; | |
2848 | break; | |
2849 | } | |
2850 | case KVM_SET_SREGS: { | |
2851 | struct kvm_sregs kvm_sregs; | |
2852 | ||
2853 | r = -EFAULT; | |
2f366987 | 2854 | if (copy_from_user(&kvm_sregs, argp, sizeof kvm_sregs)) |
6aa8b732 | 2855 | goto out; |
bccf2150 | 2856 | r = kvm_vcpu_ioctl_set_sregs(vcpu, &kvm_sregs); |
6aa8b732 AK |
2857 | if (r) |
2858 | goto out; | |
2859 | r = 0; | |
2860 | break; | |
2861 | } | |
2862 | case KVM_TRANSLATE: { | |
2863 | struct kvm_translation tr; | |
2864 | ||
2865 | r = -EFAULT; | |
2f366987 | 2866 | if (copy_from_user(&tr, argp, sizeof tr)) |
6aa8b732 | 2867 | goto out; |
bccf2150 | 2868 | r = kvm_vcpu_ioctl_translate(vcpu, &tr); |
6aa8b732 AK |
2869 | if (r) |
2870 | goto out; | |
2871 | r = -EFAULT; | |
2f366987 | 2872 | if (copy_to_user(argp, &tr, sizeof tr)) |
6aa8b732 AK |
2873 | goto out; |
2874 | r = 0; | |
2875 | break; | |
2876 | } | |
2877 | case KVM_INTERRUPT: { | |
2878 | struct kvm_interrupt irq; | |
2879 | ||
2880 | r = -EFAULT; | |
2f366987 | 2881 | if (copy_from_user(&irq, argp, sizeof irq)) |
6aa8b732 | 2882 | goto out; |
bccf2150 | 2883 | r = kvm_vcpu_ioctl_interrupt(vcpu, &irq); |
6aa8b732 AK |
2884 | if (r) |
2885 | goto out; | |
2886 | r = 0; | |
2887 | break; | |
2888 | } | |
2889 | case KVM_DEBUG_GUEST: { | |
2890 | struct kvm_debug_guest dbg; | |
2891 | ||
2892 | r = -EFAULT; | |
2f366987 | 2893 | if (copy_from_user(&dbg, argp, sizeof dbg)) |
6aa8b732 | 2894 | goto out; |
bccf2150 | 2895 | r = kvm_vcpu_ioctl_debug_guest(vcpu, &dbg); |
6aa8b732 AK |
2896 | if (r) |
2897 | goto out; | |
2898 | r = 0; | |
2899 | break; | |
2900 | } | |
bccf2150 | 2901 | case KVM_GET_MSRS: |
35f3f286 | 2902 | r = msr_io(vcpu, argp, kvm_get_msr, 1); |
bccf2150 AK |
2903 | break; |
2904 | case KVM_SET_MSRS: | |
2905 | r = msr_io(vcpu, argp, do_set_msr, 0); | |
2906 | break; | |
06465c5a AK |
2907 | case KVM_SET_CPUID: { |
2908 | struct kvm_cpuid __user *cpuid_arg = argp; | |
2909 | struct kvm_cpuid cpuid; | |
2910 | ||
2911 | r = -EFAULT; | |
2912 | if (copy_from_user(&cpuid, cpuid_arg, sizeof cpuid)) | |
2913 | goto out; | |
2914 | r = kvm_vcpu_ioctl_set_cpuid(vcpu, &cpuid, cpuid_arg->entries); | |
2915 | if (r) | |
2916 | goto out; | |
2917 | break; | |
2918 | } | |
1961d276 AK |
2919 | case KVM_SET_SIGNAL_MASK: { |
2920 | struct kvm_signal_mask __user *sigmask_arg = argp; | |
2921 | struct kvm_signal_mask kvm_sigmask; | |
2922 | sigset_t sigset, *p; | |
2923 | ||
2924 | p = NULL; | |
2925 | if (argp) { | |
2926 | r = -EFAULT; | |
2927 | if (copy_from_user(&kvm_sigmask, argp, | |
2928 | sizeof kvm_sigmask)) | |
2929 | goto out; | |
2930 | r = -EINVAL; | |
2931 | if (kvm_sigmask.len != sizeof sigset) | |
2932 | goto out; | |
2933 | r = -EFAULT; | |
2934 | if (copy_from_user(&sigset, sigmask_arg->sigset, | |
2935 | sizeof sigset)) | |
2936 | goto out; | |
2937 | p = &sigset; | |
2938 | } | |
2939 | r = kvm_vcpu_ioctl_set_sigmask(vcpu, &sigset); | |
2940 | break; | |
2941 | } | |
b8836737 AK |
2942 | case KVM_GET_FPU: { |
2943 | struct kvm_fpu fpu; | |
2944 | ||
2945 | memset(&fpu, 0, sizeof fpu); | |
2946 | r = kvm_vcpu_ioctl_get_fpu(vcpu, &fpu); | |
2947 | if (r) | |
2948 | goto out; | |
2949 | r = -EFAULT; | |
2950 | if (copy_to_user(argp, &fpu, sizeof fpu)) | |
2951 | goto out; | |
2952 | r = 0; | |
2953 | break; | |
2954 | } | |
2955 | case KVM_SET_FPU: { | |
2956 | struct kvm_fpu fpu; | |
2957 | ||
2958 | r = -EFAULT; | |
2959 | if (copy_from_user(&fpu, argp, sizeof fpu)) | |
2960 | goto out; | |
2961 | r = kvm_vcpu_ioctl_set_fpu(vcpu, &fpu); | |
2962 | if (r) | |
2963 | goto out; | |
2964 | r = 0; | |
2965 | break; | |
2966 | } | |
96ad2cc6 ED |
2967 | case KVM_GET_LAPIC: { |
2968 | struct kvm_lapic_state lapic; | |
2969 | ||
2970 | memset(&lapic, 0, sizeof lapic); | |
2971 | r = kvm_vcpu_ioctl_get_lapic(vcpu, &lapic); | |
2972 | if (r) | |
2973 | goto out; | |
2974 | r = -EFAULT; | |
2975 | if (copy_to_user(argp, &lapic, sizeof lapic)) | |
2976 | goto out; | |
2977 | r = 0; | |
2978 | break; | |
2979 | } | |
2980 | case KVM_SET_LAPIC: { | |
2981 | struct kvm_lapic_state lapic; | |
2982 | ||
2983 | r = -EFAULT; | |
2984 | if (copy_from_user(&lapic, argp, sizeof lapic)) | |
2985 | goto out; | |
2986 | r = kvm_vcpu_ioctl_set_lapic(vcpu, &lapic);; | |
2987 | if (r) | |
2988 | goto out; | |
2989 | r = 0; | |
2990 | break; | |
2991 | } | |
bccf2150 AK |
2992 | default: |
2993 | ; | |
2994 | } | |
2995 | out: | |
2996 | return r; | |
2997 | } | |
2998 | ||
2999 | static long kvm_vm_ioctl(struct file *filp, | |
3000 | unsigned int ioctl, unsigned long arg) | |
3001 | { | |
3002 | struct kvm *kvm = filp->private_data; | |
3003 | void __user *argp = (void __user *)arg; | |
3004 | int r = -EINVAL; | |
3005 | ||
3006 | switch (ioctl) { | |
3007 | case KVM_CREATE_VCPU: | |
3008 | r = kvm_vm_ioctl_create_vcpu(kvm, arg); | |
3009 | if (r < 0) | |
3010 | goto out; | |
3011 | break; | |
6aa8b732 AK |
3012 | case KVM_SET_MEMORY_REGION: { |
3013 | struct kvm_memory_region kvm_mem; | |
3014 | ||
3015 | r = -EFAULT; | |
2f366987 | 3016 | if (copy_from_user(&kvm_mem, argp, sizeof kvm_mem)) |
6aa8b732 | 3017 | goto out; |
2c6f5df9 | 3018 | r = kvm_vm_ioctl_set_memory_region(kvm, &kvm_mem); |
6aa8b732 AK |
3019 | if (r) |
3020 | goto out; | |
3021 | break; | |
3022 | } | |
3023 | case KVM_GET_DIRTY_LOG: { | |
3024 | struct kvm_dirty_log log; | |
3025 | ||
3026 | r = -EFAULT; | |
2f366987 | 3027 | if (copy_from_user(&log, argp, sizeof log)) |
6aa8b732 | 3028 | goto out; |
2c6f5df9 | 3029 | r = kvm_vm_ioctl_get_dirty_log(kvm, &log); |
6aa8b732 AK |
3030 | if (r) |
3031 | goto out; | |
3032 | break; | |
3033 | } | |
e8207547 AK |
3034 | case KVM_SET_MEMORY_ALIAS: { |
3035 | struct kvm_memory_alias alias; | |
3036 | ||
3037 | r = -EFAULT; | |
3038 | if (copy_from_user(&alias, argp, sizeof alias)) | |
3039 | goto out; | |
3040 | r = kvm_vm_ioctl_set_memory_alias(kvm, &alias); | |
3041 | if (r) | |
3042 | goto out; | |
3043 | break; | |
3044 | } | |
85f455f7 ED |
3045 | case KVM_CREATE_IRQCHIP: |
3046 | r = -ENOMEM; | |
3047 | kvm->vpic = kvm_create_pic(kvm); | |
1fd4f2a5 ED |
3048 | if (kvm->vpic) { |
3049 | r = kvm_ioapic_init(kvm); | |
3050 | if (r) { | |
3051 | kfree(kvm->vpic); | |
3052 | kvm->vpic = NULL; | |
3053 | goto out; | |
3054 | } | |
3055 | } | |
85f455f7 ED |
3056 | else |
3057 | goto out; | |
3058 | break; | |
3059 | case KVM_IRQ_LINE: { | |
3060 | struct kvm_irq_level irq_event; | |
3061 | ||
3062 | r = -EFAULT; | |
3063 | if (copy_from_user(&irq_event, argp, sizeof irq_event)) | |
3064 | goto out; | |
3065 | if (irqchip_in_kernel(kvm)) { | |
9cf98828 | 3066 | mutex_lock(&kvm->lock); |
85f455f7 ED |
3067 | if (irq_event.irq < 16) |
3068 | kvm_pic_set_irq(pic_irqchip(kvm), | |
3069 | irq_event.irq, | |
3070 | irq_event.level); | |
1fd4f2a5 ED |
3071 | kvm_ioapic_set_irq(kvm->vioapic, |
3072 | irq_event.irq, | |
3073 | irq_event.level); | |
9cf98828 | 3074 | mutex_unlock(&kvm->lock); |
85f455f7 ED |
3075 | r = 0; |
3076 | } | |
3077 | break; | |
3078 | } | |
6ceb9d79 HQ |
3079 | case KVM_GET_IRQCHIP: { |
3080 | /* 0: PIC master, 1: PIC slave, 2: IOAPIC */ | |
3081 | struct kvm_irqchip chip; | |
3082 | ||
3083 | r = -EFAULT; | |
3084 | if (copy_from_user(&chip, argp, sizeof chip)) | |
3085 | goto out; | |
3086 | r = -ENXIO; | |
3087 | if (!irqchip_in_kernel(kvm)) | |
3088 | goto out; | |
3089 | r = kvm_vm_ioctl_get_irqchip(kvm, &chip); | |
3090 | if (r) | |
3091 | goto out; | |
3092 | r = -EFAULT; | |
3093 | if (copy_to_user(argp, &chip, sizeof chip)) | |
3094 | goto out; | |
3095 | r = 0; | |
3096 | break; | |
3097 | } | |
3098 | case KVM_SET_IRQCHIP: { | |
3099 | /* 0: PIC master, 1: PIC slave, 2: IOAPIC */ | |
3100 | struct kvm_irqchip chip; | |
3101 | ||
3102 | r = -EFAULT; | |
3103 | if (copy_from_user(&chip, argp, sizeof chip)) | |
3104 | goto out; | |
3105 | r = -ENXIO; | |
3106 | if (!irqchip_in_kernel(kvm)) | |
3107 | goto out; | |
3108 | r = kvm_vm_ioctl_set_irqchip(kvm, &chip); | |
3109 | if (r) | |
3110 | goto out; | |
3111 | r = 0; | |
3112 | break; | |
3113 | } | |
f17abe9a AK |
3114 | default: |
3115 | ; | |
3116 | } | |
3117 | out: | |
3118 | return r; | |
3119 | } | |
3120 | ||
3121 | static struct page *kvm_vm_nopage(struct vm_area_struct *vma, | |
3122 | unsigned long address, | |
3123 | int *type) | |
3124 | { | |
3125 | struct kvm *kvm = vma->vm_file->private_data; | |
3126 | unsigned long pgoff; | |
f17abe9a AK |
3127 | struct page *page; |
3128 | ||
f17abe9a | 3129 | pgoff = ((address - vma->vm_start) >> PAGE_SHIFT) + vma->vm_pgoff; |
954bbbc2 | 3130 | page = gfn_to_page(kvm, pgoff); |
f17abe9a AK |
3131 | if (!page) |
3132 | return NOPAGE_SIGBUS; | |
3133 | get_page(page); | |
cd0d9137 NAQ |
3134 | if (type != NULL) |
3135 | *type = VM_FAULT_MINOR; | |
3136 | ||
f17abe9a AK |
3137 | return page; |
3138 | } | |
3139 | ||
3140 | static struct vm_operations_struct kvm_vm_vm_ops = { | |
3141 | .nopage = kvm_vm_nopage, | |
3142 | }; | |
3143 | ||
3144 | static int kvm_vm_mmap(struct file *file, struct vm_area_struct *vma) | |
3145 | { | |
3146 | vma->vm_ops = &kvm_vm_vm_ops; | |
3147 | return 0; | |
3148 | } | |
3149 | ||
3150 | static struct file_operations kvm_vm_fops = { | |
3151 | .release = kvm_vm_release, | |
3152 | .unlocked_ioctl = kvm_vm_ioctl, | |
3153 | .compat_ioctl = kvm_vm_ioctl, | |
3154 | .mmap = kvm_vm_mmap, | |
3155 | }; | |
3156 | ||
3157 | static int kvm_dev_ioctl_create_vm(void) | |
3158 | { | |
3159 | int fd, r; | |
3160 | struct inode *inode; | |
3161 | struct file *file; | |
3162 | struct kvm *kvm; | |
3163 | ||
f17abe9a | 3164 | kvm = kvm_create_vm(); |
d6d28168 AK |
3165 | if (IS_ERR(kvm)) |
3166 | return PTR_ERR(kvm); | |
3167 | r = anon_inode_getfd(&fd, &inode, &file, "kvm-vm", &kvm_vm_fops, kvm); | |
3168 | if (r) { | |
3169 | kvm_destroy_vm(kvm); | |
3170 | return r; | |
f17abe9a AK |
3171 | } |
3172 | ||
bccf2150 | 3173 | kvm->filp = file; |
f17abe9a | 3174 | |
f17abe9a | 3175 | return fd; |
f17abe9a AK |
3176 | } |
3177 | ||
3178 | static long kvm_dev_ioctl(struct file *filp, | |
3179 | unsigned int ioctl, unsigned long arg) | |
3180 | { | |
3181 | void __user *argp = (void __user *)arg; | |
07c45a36 | 3182 | long r = -EINVAL; |
f17abe9a AK |
3183 | |
3184 | switch (ioctl) { | |
3185 | case KVM_GET_API_VERSION: | |
f0fe5108 AK |
3186 | r = -EINVAL; |
3187 | if (arg) | |
3188 | goto out; | |
f17abe9a AK |
3189 | r = KVM_API_VERSION; |
3190 | break; | |
3191 | case KVM_CREATE_VM: | |
f0fe5108 AK |
3192 | r = -EINVAL; |
3193 | if (arg) | |
3194 | goto out; | |
f17abe9a AK |
3195 | r = kvm_dev_ioctl_create_vm(); |
3196 | break; | |
6aa8b732 | 3197 | case KVM_GET_MSR_INDEX_LIST: { |
2f366987 | 3198 | struct kvm_msr_list __user *user_msr_list = argp; |
6aa8b732 AK |
3199 | struct kvm_msr_list msr_list; |
3200 | unsigned n; | |
3201 | ||
3202 | r = -EFAULT; | |
3203 | if (copy_from_user(&msr_list, user_msr_list, sizeof msr_list)) | |
3204 | goto out; | |
3205 | n = msr_list.nmsrs; | |
6f00e68f | 3206 | msr_list.nmsrs = num_msrs_to_save + ARRAY_SIZE(emulated_msrs); |
6aa8b732 AK |
3207 | if (copy_to_user(user_msr_list, &msr_list, sizeof msr_list)) |
3208 | goto out; | |
3209 | r = -E2BIG; | |
bf591b24 | 3210 | if (n < num_msrs_to_save) |
6aa8b732 AK |
3211 | goto out; |
3212 | r = -EFAULT; | |
3213 | if (copy_to_user(user_msr_list->indices, &msrs_to_save, | |
bf591b24 | 3214 | num_msrs_to_save * sizeof(u32))) |
6aa8b732 | 3215 | goto out; |
6f00e68f AK |
3216 | if (copy_to_user(user_msr_list->indices |
3217 | + num_msrs_to_save * sizeof(u32), | |
3218 | &emulated_msrs, | |
3219 | ARRAY_SIZE(emulated_msrs) * sizeof(u32))) | |
3220 | goto out; | |
6aa8b732 | 3221 | r = 0; |
cc1d8955 | 3222 | break; |
6aa8b732 | 3223 | } |
85f455f7 ED |
3224 | case KVM_CHECK_EXTENSION: { |
3225 | int ext = (long)argp; | |
3226 | ||
3227 | switch (ext) { | |
3228 | case KVM_CAP_IRQCHIP: | |
b6958ce4 | 3229 | case KVM_CAP_HLT: |
85f455f7 ED |
3230 | r = 1; |
3231 | break; | |
3232 | default: | |
3233 | r = 0; | |
3234 | break; | |
3235 | } | |
5d308f45 | 3236 | break; |
85f455f7 | 3237 | } |
07c45a36 AK |
3238 | case KVM_GET_VCPU_MMAP_SIZE: |
3239 | r = -EINVAL; | |
3240 | if (arg) | |
3241 | goto out; | |
039576c0 | 3242 | r = 2 * PAGE_SIZE; |
07c45a36 | 3243 | break; |
6aa8b732 AK |
3244 | default: |
3245 | ; | |
3246 | } | |
3247 | out: | |
3248 | return r; | |
3249 | } | |
3250 | ||
6aa8b732 | 3251 | static struct file_operations kvm_chardev_ops = { |
6aa8b732 AK |
3252 | .unlocked_ioctl = kvm_dev_ioctl, |
3253 | .compat_ioctl = kvm_dev_ioctl, | |
6aa8b732 AK |
3254 | }; |
3255 | ||
3256 | static struct miscdevice kvm_dev = { | |
bbe4432e | 3257 | KVM_MINOR, |
6aa8b732 AK |
3258 | "kvm", |
3259 | &kvm_chardev_ops, | |
3260 | }; | |
3261 | ||
774c47f1 AK |
3262 | /* |
3263 | * Make sure that a cpu that is being hot-unplugged does not have any vcpus | |
3264 | * cached on it. | |
3265 | */ | |
3266 | static void decache_vcpus_on_cpu(int cpu) | |
3267 | { | |
3268 | struct kvm *vm; | |
3269 | struct kvm_vcpu *vcpu; | |
3270 | int i; | |
3271 | ||
3272 | spin_lock(&kvm_lock); | |
11ec2804 | 3273 | list_for_each_entry(vm, &vm_list, vm_list) |
774c47f1 | 3274 | for (i = 0; i < KVM_MAX_VCPUS; ++i) { |
fb3f0f51 RR |
3275 | vcpu = vm->vcpus[i]; |
3276 | if (!vcpu) | |
3277 | continue; | |
774c47f1 AK |
3278 | /* |
3279 | * If the vcpu is locked, then it is running on some | |
3280 | * other cpu and therefore it is not cached on the | |
3281 | * cpu in question. | |
3282 | * | |
3283 | * If it's not locked, check the last cpu it executed | |
3284 | * on. | |
3285 | */ | |
3286 | if (mutex_trylock(&vcpu->mutex)) { | |
3287 | if (vcpu->cpu == cpu) { | |
cbdd1bea | 3288 | kvm_x86_ops->vcpu_decache(vcpu); |
774c47f1 AK |
3289 | vcpu->cpu = -1; |
3290 | } | |
3291 | mutex_unlock(&vcpu->mutex); | |
3292 | } | |
3293 | } | |
3294 | spin_unlock(&kvm_lock); | |
3295 | } | |
3296 | ||
1b6c0168 AK |
3297 | static void hardware_enable(void *junk) |
3298 | { | |
3299 | int cpu = raw_smp_processor_id(); | |
3300 | ||
3301 | if (cpu_isset(cpu, cpus_hardware_enabled)) | |
3302 | return; | |
3303 | cpu_set(cpu, cpus_hardware_enabled); | |
cbdd1bea | 3304 | kvm_x86_ops->hardware_enable(NULL); |
1b6c0168 AK |
3305 | } |
3306 | ||
3307 | static void hardware_disable(void *junk) | |
3308 | { | |
3309 | int cpu = raw_smp_processor_id(); | |
3310 | ||
3311 | if (!cpu_isset(cpu, cpus_hardware_enabled)) | |
3312 | return; | |
3313 | cpu_clear(cpu, cpus_hardware_enabled); | |
3314 | decache_vcpus_on_cpu(cpu); | |
cbdd1bea | 3315 | kvm_x86_ops->hardware_disable(NULL); |
1b6c0168 AK |
3316 | } |
3317 | ||
774c47f1 AK |
3318 | static int kvm_cpu_hotplug(struct notifier_block *notifier, unsigned long val, |
3319 | void *v) | |
3320 | { | |
3321 | int cpu = (long)v; | |
3322 | ||
3323 | switch (val) { | |
cec9ad27 AK |
3324 | case CPU_DYING: |
3325 | case CPU_DYING_FROZEN: | |
6ec8a856 AK |
3326 | printk(KERN_INFO "kvm: disabling virtualization on CPU%d\n", |
3327 | cpu); | |
3328 | hardware_disable(NULL); | |
3329 | break; | |
774c47f1 | 3330 | case CPU_UP_CANCELED: |
8bb78442 | 3331 | case CPU_UP_CANCELED_FROZEN: |
43934a38 JK |
3332 | printk(KERN_INFO "kvm: disabling virtualization on CPU%d\n", |
3333 | cpu); | |
1b6c0168 | 3334 | smp_call_function_single(cpu, hardware_disable, NULL, 0, 1); |
774c47f1 | 3335 | break; |
43934a38 | 3336 | case CPU_ONLINE: |
8bb78442 | 3337 | case CPU_ONLINE_FROZEN: |
43934a38 JK |
3338 | printk(KERN_INFO "kvm: enabling virtualization on CPU%d\n", |
3339 | cpu); | |
1b6c0168 | 3340 | smp_call_function_single(cpu, hardware_enable, NULL, 0, 1); |
774c47f1 AK |
3341 | break; |
3342 | } | |
3343 | return NOTIFY_OK; | |
3344 | } | |
3345 | ||
9a2b85c6 RR |
3346 | static int kvm_reboot(struct notifier_block *notifier, unsigned long val, |
3347 | void *v) | |
3348 | { | |
3349 | if (val == SYS_RESTART) { | |
3350 | /* | |
3351 | * Some (well, at least mine) BIOSes hang on reboot if | |
3352 | * in vmx root mode. | |
3353 | */ | |
3354 | printk(KERN_INFO "kvm: exiting hardware virtualization\n"); | |
3355 | on_each_cpu(hardware_disable, NULL, 0, 1); | |
3356 | } | |
3357 | return NOTIFY_OK; | |
3358 | } | |
3359 | ||
3360 | static struct notifier_block kvm_reboot_notifier = { | |
3361 | .notifier_call = kvm_reboot, | |
3362 | .priority = 0, | |
3363 | }; | |
3364 | ||
2eeb2e94 GH |
3365 | void kvm_io_bus_init(struct kvm_io_bus *bus) |
3366 | { | |
3367 | memset(bus, 0, sizeof(*bus)); | |
3368 | } | |
3369 | ||
3370 | void kvm_io_bus_destroy(struct kvm_io_bus *bus) | |
3371 | { | |
3372 | int i; | |
3373 | ||
3374 | for (i = 0; i < bus->dev_count; i++) { | |
3375 | struct kvm_io_device *pos = bus->devs[i]; | |
3376 | ||
3377 | kvm_iodevice_destructor(pos); | |
3378 | } | |
3379 | } | |
3380 | ||
3381 | struct kvm_io_device *kvm_io_bus_find_dev(struct kvm_io_bus *bus, gpa_t addr) | |
3382 | { | |
3383 | int i; | |
3384 | ||
3385 | for (i = 0; i < bus->dev_count; i++) { | |
3386 | struct kvm_io_device *pos = bus->devs[i]; | |
3387 | ||
3388 | if (pos->in_range(pos, addr)) | |
3389 | return pos; | |
3390 | } | |
3391 | ||
3392 | return NULL; | |
3393 | } | |
3394 | ||
3395 | void kvm_io_bus_register_dev(struct kvm_io_bus *bus, struct kvm_io_device *dev) | |
3396 | { | |
3397 | BUG_ON(bus->dev_count > (NR_IOBUS_DEVS-1)); | |
3398 | ||
3399 | bus->devs[bus->dev_count++] = dev; | |
3400 | } | |
3401 | ||
774c47f1 AK |
3402 | static struct notifier_block kvm_cpu_notifier = { |
3403 | .notifier_call = kvm_cpu_hotplug, | |
3404 | .priority = 20, /* must be > scheduler priority */ | |
3405 | }; | |
3406 | ||
1165f5fe AK |
3407 | static u64 stat_get(void *_offset) |
3408 | { | |
3409 | unsigned offset = (long)_offset; | |
3410 | u64 total = 0; | |
3411 | struct kvm *kvm; | |
3412 | struct kvm_vcpu *vcpu; | |
3413 | int i; | |
3414 | ||
3415 | spin_lock(&kvm_lock); | |
3416 | list_for_each_entry(kvm, &vm_list, vm_list) | |
3417 | for (i = 0; i < KVM_MAX_VCPUS; ++i) { | |
fb3f0f51 RR |
3418 | vcpu = kvm->vcpus[i]; |
3419 | if (vcpu) | |
3420 | total += *(u32 *)((void *)vcpu + offset); | |
1165f5fe AK |
3421 | } |
3422 | spin_unlock(&kvm_lock); | |
3423 | return total; | |
3424 | } | |
3425 | ||
3dea7ca7 | 3426 | DEFINE_SIMPLE_ATTRIBUTE(stat_fops, stat_get, NULL, "%llu\n"); |
1165f5fe | 3427 | |
6aa8b732 AK |
3428 | static __init void kvm_init_debug(void) |
3429 | { | |
3430 | struct kvm_stats_debugfs_item *p; | |
3431 | ||
8b6d44c7 | 3432 | debugfs_dir = debugfs_create_dir("kvm", NULL); |
6aa8b732 | 3433 | for (p = debugfs_entries; p->name; ++p) |
1165f5fe AK |
3434 | p->dentry = debugfs_create_file(p->name, 0444, debugfs_dir, |
3435 | (void *)(long)p->offset, | |
3436 | &stat_fops); | |
6aa8b732 AK |
3437 | } |
3438 | ||
3439 | static void kvm_exit_debug(void) | |
3440 | { | |
3441 | struct kvm_stats_debugfs_item *p; | |
3442 | ||
3443 | for (p = debugfs_entries; p->name; ++p) | |
3444 | debugfs_remove(p->dentry); | |
3445 | debugfs_remove(debugfs_dir); | |
3446 | } | |
3447 | ||
59ae6c6b AK |
3448 | static int kvm_suspend(struct sys_device *dev, pm_message_t state) |
3449 | { | |
4267c41a | 3450 | hardware_disable(NULL); |
59ae6c6b AK |
3451 | return 0; |
3452 | } | |
3453 | ||
3454 | static int kvm_resume(struct sys_device *dev) | |
3455 | { | |
4267c41a | 3456 | hardware_enable(NULL); |
59ae6c6b AK |
3457 | return 0; |
3458 | } | |
3459 | ||
3460 | static struct sysdev_class kvm_sysdev_class = { | |
3461 | set_kset_name("kvm"), | |
3462 | .suspend = kvm_suspend, | |
3463 | .resume = kvm_resume, | |
3464 | }; | |
3465 | ||
3466 | static struct sys_device kvm_sysdev = { | |
3467 | .id = 0, | |
3468 | .cls = &kvm_sysdev_class, | |
3469 | }; | |
3470 | ||
6aa8b732 AK |
3471 | hpa_t bad_page_address; |
3472 | ||
15ad7146 AK |
3473 | static inline |
3474 | struct kvm_vcpu *preempt_notifier_to_vcpu(struct preempt_notifier *pn) | |
3475 | { | |
3476 | return container_of(pn, struct kvm_vcpu, preempt_notifier); | |
3477 | } | |
3478 | ||
3479 | static void kvm_sched_in(struct preempt_notifier *pn, int cpu) | |
3480 | { | |
3481 | struct kvm_vcpu *vcpu = preempt_notifier_to_vcpu(pn); | |
3482 | ||
cbdd1bea | 3483 | kvm_x86_ops->vcpu_load(vcpu, cpu); |
15ad7146 AK |
3484 | } |
3485 | ||
3486 | static void kvm_sched_out(struct preempt_notifier *pn, | |
3487 | struct task_struct *next) | |
3488 | { | |
3489 | struct kvm_vcpu *vcpu = preempt_notifier_to_vcpu(pn); | |
3490 | ||
cbdd1bea | 3491 | kvm_x86_ops->vcpu_put(vcpu); |
15ad7146 AK |
3492 | } |
3493 | ||
cbdd1bea | 3494 | int kvm_init_x86(struct kvm_x86_ops *ops, unsigned int vcpu_size, |
c16f862d | 3495 | struct module *module) |
6aa8b732 AK |
3496 | { |
3497 | int r; | |
002c7f7c | 3498 | int cpu; |
6aa8b732 | 3499 | |
cbdd1bea | 3500 | if (kvm_x86_ops) { |
09db28b8 YI |
3501 | printk(KERN_ERR "kvm: already loaded the other module\n"); |
3502 | return -EEXIST; | |
3503 | } | |
3504 | ||
e097f35c | 3505 | if (!ops->cpu_has_kvm_support()) { |
6aa8b732 AK |
3506 | printk(KERN_ERR "kvm: no hardware support\n"); |
3507 | return -EOPNOTSUPP; | |
3508 | } | |
e097f35c | 3509 | if (ops->disabled_by_bios()) { |
6aa8b732 AK |
3510 | printk(KERN_ERR "kvm: disabled by bios\n"); |
3511 | return -EOPNOTSUPP; | |
3512 | } | |
3513 | ||
cbdd1bea | 3514 | kvm_x86_ops = ops; |
e097f35c | 3515 | |
cbdd1bea | 3516 | r = kvm_x86_ops->hardware_setup(); |
6aa8b732 | 3517 | if (r < 0) |
ca45aaae | 3518 | goto out; |
6aa8b732 | 3519 | |
002c7f7c YS |
3520 | for_each_online_cpu(cpu) { |
3521 | smp_call_function_single(cpu, | |
cbdd1bea | 3522 | kvm_x86_ops->check_processor_compatibility, |
002c7f7c YS |
3523 | &r, 0, 1); |
3524 | if (r < 0) | |
3525 | goto out_free_0; | |
3526 | } | |
3527 | ||
1b6c0168 | 3528 | on_each_cpu(hardware_enable, NULL, 0, 1); |
774c47f1 AK |
3529 | r = register_cpu_notifier(&kvm_cpu_notifier); |
3530 | if (r) | |
3531 | goto out_free_1; | |
6aa8b732 AK |
3532 | register_reboot_notifier(&kvm_reboot_notifier); |
3533 | ||
59ae6c6b AK |
3534 | r = sysdev_class_register(&kvm_sysdev_class); |
3535 | if (r) | |
3536 | goto out_free_2; | |
3537 | ||
3538 | r = sysdev_register(&kvm_sysdev); | |
3539 | if (r) | |
3540 | goto out_free_3; | |
3541 | ||
c16f862d RR |
3542 | /* A kmem cache lets us meet the alignment requirements of fx_save. */ |
3543 | kvm_vcpu_cache = kmem_cache_create("kvm_vcpu", vcpu_size, | |
3544 | __alignof__(struct kvm_vcpu), 0, 0); | |
3545 | if (!kvm_vcpu_cache) { | |
3546 | r = -ENOMEM; | |
3547 | goto out_free_4; | |
3548 | } | |
3549 | ||
6aa8b732 AK |
3550 | kvm_chardev_ops.owner = module; |
3551 | ||
3552 | r = misc_register(&kvm_dev); | |
3553 | if (r) { | |
3554 | printk (KERN_ERR "kvm: misc device register failed\n"); | |
3555 | goto out_free; | |
3556 | } | |
3557 | ||
15ad7146 AK |
3558 | kvm_preempt_ops.sched_in = kvm_sched_in; |
3559 | kvm_preempt_ops.sched_out = kvm_sched_out; | |
3560 | ||
6aa8b732 AK |
3561 | return r; |
3562 | ||
3563 | out_free: | |
c16f862d RR |
3564 | kmem_cache_destroy(kvm_vcpu_cache); |
3565 | out_free_4: | |
59ae6c6b AK |
3566 | sysdev_unregister(&kvm_sysdev); |
3567 | out_free_3: | |
3568 | sysdev_class_unregister(&kvm_sysdev_class); | |
3569 | out_free_2: | |
6aa8b732 | 3570 | unregister_reboot_notifier(&kvm_reboot_notifier); |
774c47f1 AK |
3571 | unregister_cpu_notifier(&kvm_cpu_notifier); |
3572 | out_free_1: | |
1b6c0168 | 3573 | on_each_cpu(hardware_disable, NULL, 0, 1); |
002c7f7c | 3574 | out_free_0: |
cbdd1bea | 3575 | kvm_x86_ops->hardware_unsetup(); |
ca45aaae | 3576 | out: |
cbdd1bea | 3577 | kvm_x86_ops = NULL; |
6aa8b732 AK |
3578 | return r; |
3579 | } | |
3580 | ||
cbdd1bea | 3581 | void kvm_exit_x86(void) |
6aa8b732 AK |
3582 | { |
3583 | misc_deregister(&kvm_dev); | |
c16f862d | 3584 | kmem_cache_destroy(kvm_vcpu_cache); |
59ae6c6b AK |
3585 | sysdev_unregister(&kvm_sysdev); |
3586 | sysdev_class_unregister(&kvm_sysdev_class); | |
6aa8b732 | 3587 | unregister_reboot_notifier(&kvm_reboot_notifier); |
59ae6c6b | 3588 | unregister_cpu_notifier(&kvm_cpu_notifier); |
1b6c0168 | 3589 | on_each_cpu(hardware_disable, NULL, 0, 1); |
cbdd1bea CE |
3590 | kvm_x86_ops->hardware_unsetup(); |
3591 | kvm_x86_ops = NULL; | |
6aa8b732 AK |
3592 | } |
3593 | ||
3594 | static __init int kvm_init(void) | |
3595 | { | |
3596 | static struct page *bad_page; | |
37e29d90 AK |
3597 | int r; |
3598 | ||
b5a33a75 AK |
3599 | r = kvm_mmu_module_init(); |
3600 | if (r) | |
3601 | goto out4; | |
3602 | ||
6aa8b732 AK |
3603 | kvm_init_debug(); |
3604 | ||
bf591b24 MR |
3605 | kvm_init_msr_list(); |
3606 | ||
6aa8b732 AK |
3607 | if ((bad_page = alloc_page(GFP_KERNEL)) == NULL) { |
3608 | r = -ENOMEM; | |
3609 | goto out; | |
3610 | } | |
3611 | ||
3612 | bad_page_address = page_to_pfn(bad_page) << PAGE_SHIFT; | |
3613 | memset(__va(bad_page_address), 0, PAGE_SIZE); | |
3614 | ||
58e690e6 | 3615 | return 0; |
6aa8b732 AK |
3616 | |
3617 | out: | |
3618 | kvm_exit_debug(); | |
b5a33a75 AK |
3619 | kvm_mmu_module_exit(); |
3620 | out4: | |
6aa8b732 AK |
3621 | return r; |
3622 | } | |
3623 | ||
3624 | static __exit void kvm_exit(void) | |
3625 | { | |
3626 | kvm_exit_debug(); | |
3627 | __free_page(pfn_to_page(bad_page_address >> PAGE_SHIFT)); | |
b5a33a75 | 3628 | kvm_mmu_module_exit(); |
6aa8b732 AK |
3629 | } |
3630 | ||
3631 | module_init(kvm_init) | |
3632 | module_exit(kvm_exit) | |
3633 | ||
cbdd1bea CE |
3634 | EXPORT_SYMBOL_GPL(kvm_init_x86); |
3635 | EXPORT_SYMBOL_GPL(kvm_exit_x86); |