]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - drivers/kvm/kvm_main.c
[PATCH] kvm: Fix mismatch between 32-bit and 64-bit abi
[mirror_ubuntu-artful-kernel.git] / drivers / kvm / kvm_main.c
CommitLineData
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"
19
20#include <linux/kvm.h>
21#include <linux/module.h>
22#include <linux/errno.h>
23#include <asm/processor.h>
24#include <linux/percpu.h>
25#include <linux/gfp.h>
26#include <asm/msr.h>
27#include <linux/mm.h>
28#include <linux/miscdevice.h>
29#include <linux/vmalloc.h>
30#include <asm/uaccess.h>
31#include <linux/reboot.h>
32#include <asm/io.h>
33#include <linux/debugfs.h>
34#include <linux/highmem.h>
35#include <linux/file.h>
36#include <asm/desc.h>
37
38#include "x86_emulate.h"
39#include "segment_descriptor.h"
40
41MODULE_AUTHOR("Qumranet");
42MODULE_LICENSE("GPL");
43
44struct kvm_arch_ops *kvm_arch_ops;
45struct kvm_stat kvm_stat;
46EXPORT_SYMBOL_GPL(kvm_stat);
47
48static struct kvm_stats_debugfs_item {
49 const char *name;
50 u32 *data;
51 struct dentry *dentry;
52} debugfs_entries[] = {
53 { "pf_fixed", &kvm_stat.pf_fixed },
54 { "pf_guest", &kvm_stat.pf_guest },
55 { "tlb_flush", &kvm_stat.tlb_flush },
56 { "invlpg", &kvm_stat.invlpg },
57 { "exits", &kvm_stat.exits },
58 { "io_exits", &kvm_stat.io_exits },
59 { "mmio_exits", &kvm_stat.mmio_exits },
60 { "signal_exits", &kvm_stat.signal_exits },
c1150d8c
DL
61 { "irq_window", &kvm_stat.irq_window_exits },
62 { "halt_exits", &kvm_stat.halt_exits },
63 { "request_irq", &kvm_stat.request_irq_exits },
6aa8b732 64 { "irq_exits", &kvm_stat.irq_exits },
8b6d44c7 65 { NULL, NULL }
6aa8b732
AK
66};
67
68static struct dentry *debugfs_dir;
69
70#define MAX_IO_MSRS 256
71
72#define CR0_RESEVED_BITS 0xffffffff1ffaffc0ULL
73#define LMSW_GUEST_MASK 0x0eULL
74#define CR4_RESEVED_BITS (~((1ULL << 11) - 1))
75#define CR8_RESEVED_BITS (~0x0fULL)
76#define EFER_RESERVED_BITS 0xfffffffffffff2fe
77
05b3e0c2 78#ifdef CONFIG_X86_64
6aa8b732
AK
79// LDT or TSS descriptor in the GDT. 16 bytes.
80struct segment_descriptor_64 {
81 struct segment_descriptor s;
82 u32 base_higher;
83 u32 pad_zero;
84};
85
86#endif
87
88unsigned long segment_base(u16 selector)
89{
90 struct descriptor_table gdt;
91 struct segment_descriptor *d;
92 unsigned long table_base;
93 typedef unsigned long ul;
94 unsigned long v;
95
96 if (selector == 0)
97 return 0;
98
99 asm ("sgdt %0" : "=m"(gdt));
100 table_base = gdt.base;
101
102 if (selector & 4) { /* from ldt */
103 u16 ldt_selector;
104
105 asm ("sldt %0" : "=g"(ldt_selector));
106 table_base = segment_base(ldt_selector);
107 }
108 d = (struct segment_descriptor *)(table_base + (selector & ~7));
109 v = d->base_low | ((ul)d->base_mid << 16) | ((ul)d->base_high << 24);
05b3e0c2 110#ifdef CONFIG_X86_64
6aa8b732
AK
111 if (d->system == 0
112 && (d->type == 2 || d->type == 9 || d->type == 11))
113 v |= ((ul)((struct segment_descriptor_64 *)d)->base_higher) << 32;
114#endif
115 return v;
116}
117EXPORT_SYMBOL_GPL(segment_base);
118
5aacf0ca
JM
119static inline int valid_vcpu(int n)
120{
121 return likely(n >= 0 && n < KVM_MAX_VCPUS);
122}
123
6aa8b732
AK
124int kvm_read_guest(struct kvm_vcpu *vcpu,
125 gva_t addr,
126 unsigned long size,
127 void *dest)
128{
129 unsigned char *host_buf = dest;
130 unsigned long req_size = size;
131
132 while (size) {
133 hpa_t paddr;
134 unsigned now;
135 unsigned offset;
136 hva_t guest_buf;
137
138 paddr = gva_to_hpa(vcpu, addr);
139
140 if (is_error_hpa(paddr))
141 break;
142
143 guest_buf = (hva_t)kmap_atomic(
144 pfn_to_page(paddr >> PAGE_SHIFT),
145 KM_USER0);
146 offset = addr & ~PAGE_MASK;
147 guest_buf |= offset;
148 now = min(size, PAGE_SIZE - offset);
149 memcpy(host_buf, (void*)guest_buf, now);
150 host_buf += now;
151 addr += now;
152 size -= now;
153 kunmap_atomic((void *)(guest_buf & PAGE_MASK), KM_USER0);
154 }
155 return req_size - size;
156}
157EXPORT_SYMBOL_GPL(kvm_read_guest);
158
159int kvm_write_guest(struct kvm_vcpu *vcpu,
160 gva_t addr,
161 unsigned long size,
162 void *data)
163{
164 unsigned char *host_buf = data;
165 unsigned long req_size = size;
166
167 while (size) {
168 hpa_t paddr;
169 unsigned now;
170 unsigned offset;
171 hva_t guest_buf;
172
173 paddr = gva_to_hpa(vcpu, addr);
174
175 if (is_error_hpa(paddr))
176 break;
177
178 guest_buf = (hva_t)kmap_atomic(
179 pfn_to_page(paddr >> PAGE_SHIFT), KM_USER0);
180 offset = addr & ~PAGE_MASK;
181 guest_buf |= offset;
182 now = min(size, PAGE_SIZE - offset);
183 memcpy((void*)guest_buf, host_buf, now);
184 host_buf += now;
185 addr += now;
186 size -= now;
187 kunmap_atomic((void *)(guest_buf & PAGE_MASK), KM_USER0);
188 }
189 return req_size - size;
190}
191EXPORT_SYMBOL_GPL(kvm_write_guest);
192
193static int vcpu_slot(struct kvm_vcpu *vcpu)
194{
195 return vcpu - vcpu->kvm->vcpus;
196}
197
198/*
199 * Switches to specified vcpu, until a matching vcpu_put()
200 */
201static struct kvm_vcpu *vcpu_load(struct kvm *kvm, int vcpu_slot)
202{
203 struct kvm_vcpu *vcpu = &kvm->vcpus[vcpu_slot];
204
205 mutex_lock(&vcpu->mutex);
206 if (unlikely(!vcpu->vmcs)) {
207 mutex_unlock(&vcpu->mutex);
8b6d44c7 208 return NULL;
6aa8b732
AK
209 }
210 return kvm_arch_ops->vcpu_load(vcpu);
211}
212
213static void vcpu_put(struct kvm_vcpu *vcpu)
214{
215 kvm_arch_ops->vcpu_put(vcpu);
6aa8b732
AK
216 mutex_unlock(&vcpu->mutex);
217}
218
219static int kvm_dev_open(struct inode *inode, struct file *filp)
220{
221 struct kvm *kvm = kzalloc(sizeof(struct kvm), GFP_KERNEL);
222 int i;
223
224 if (!kvm)
225 return -ENOMEM;
226
227 spin_lock_init(&kvm->lock);
228 INIT_LIST_HEAD(&kvm->active_mmu_pages);
229 for (i = 0; i < KVM_MAX_VCPUS; ++i) {
230 struct kvm_vcpu *vcpu = &kvm->vcpus[i];
231
232 mutex_init(&vcpu->mutex);
86a2b42e 233 vcpu->kvm = kvm;
6aa8b732
AK
234 vcpu->mmu.root_hpa = INVALID_PAGE;
235 INIT_LIST_HEAD(&vcpu->free_pages);
236 }
237 filp->private_data = kvm;
238 return 0;
239}
240
241/*
242 * Free any memory in @free but not in @dont.
243 */
244static void kvm_free_physmem_slot(struct kvm_memory_slot *free,
245 struct kvm_memory_slot *dont)
246{
247 int i;
248
249 if (!dont || free->phys_mem != dont->phys_mem)
250 if (free->phys_mem) {
251 for (i = 0; i < free->npages; ++i)
55a54f79
AK
252 if (free->phys_mem[i])
253 __free_page(free->phys_mem[i]);
6aa8b732
AK
254 vfree(free->phys_mem);
255 }
256
257 if (!dont || free->dirty_bitmap != dont->dirty_bitmap)
258 vfree(free->dirty_bitmap);
259
8b6d44c7 260 free->phys_mem = NULL;
6aa8b732 261 free->npages = 0;
8b6d44c7 262 free->dirty_bitmap = NULL;
6aa8b732
AK
263}
264
265static void kvm_free_physmem(struct kvm *kvm)
266{
267 int i;
268
269 for (i = 0; i < kvm->nmemslots; ++i)
8b6d44c7 270 kvm_free_physmem_slot(&kvm->memslots[i], NULL);
6aa8b732
AK
271}
272
273static void kvm_free_vcpu(struct kvm_vcpu *vcpu)
274{
08438475 275 vcpu_load(vcpu->kvm, vcpu_slot(vcpu));
6aa8b732 276 kvm_mmu_destroy(vcpu);
08438475 277 vcpu_put(vcpu);
9ede74e0 278 kvm_arch_ops->vcpu_free(vcpu);
6aa8b732
AK
279}
280
281static void kvm_free_vcpus(struct kvm *kvm)
282{
283 unsigned int i;
284
285 for (i = 0; i < KVM_MAX_VCPUS; ++i)
286 kvm_free_vcpu(&kvm->vcpus[i]);
287}
288
289static int kvm_dev_release(struct inode *inode, struct file *filp)
290{
291 struct kvm *kvm = filp->private_data;
292
293 kvm_free_vcpus(kvm);
294 kvm_free_physmem(kvm);
295 kfree(kvm);
296 return 0;
297}
298
299static void inject_gp(struct kvm_vcpu *vcpu)
300{
301 kvm_arch_ops->inject_gp(vcpu, 0);
302}
303
1342d353
AK
304/*
305 * Load the pae pdptrs. Return true is they are all valid.
306 */
307static int load_pdptrs(struct kvm_vcpu *vcpu, unsigned long cr3)
6aa8b732
AK
308{
309 gfn_t pdpt_gfn = cr3 >> PAGE_SHIFT;
1342d353 310 unsigned offset = ((cr3 & (PAGE_SIZE-1)) >> 5) << 2;
6aa8b732
AK
311 int i;
312 u64 pdpte;
313 u64 *pdpt;
1342d353 314 int ret;
6aa8b732
AK
315 struct kvm_memory_slot *memslot;
316
317 spin_lock(&vcpu->kvm->lock);
318 memslot = gfn_to_memslot(vcpu->kvm, pdpt_gfn);
319 /* FIXME: !memslot - emulate? 0xff? */
320 pdpt = kmap_atomic(gfn_to_page(memslot, pdpt_gfn), KM_USER0);
321
1342d353 322 ret = 1;
6aa8b732
AK
323 for (i = 0; i < 4; ++i) {
324 pdpte = pdpt[offset + i];
1342d353
AK
325 if ((pdpte & 1) && (pdpte & 0xfffffff0000001e6ull)) {
326 ret = 0;
327 goto out;
328 }
6aa8b732
AK
329 }
330
1342d353
AK
331 for (i = 0; i < 4; ++i)
332 vcpu->pdptrs[i] = pdpt[offset + i];
333
334out:
6aa8b732
AK
335 kunmap_atomic(pdpt, KM_USER0);
336 spin_unlock(&vcpu->kvm->lock);
337
1342d353 338 return ret;
6aa8b732
AK
339}
340
341void set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0)
342{
343 if (cr0 & CR0_RESEVED_BITS) {
344 printk(KERN_DEBUG "set_cr0: 0x%lx #GP, reserved bits 0x%lx\n",
345 cr0, vcpu->cr0);
346 inject_gp(vcpu);
347 return;
348 }
349
350 if ((cr0 & CR0_NW_MASK) && !(cr0 & CR0_CD_MASK)) {
351 printk(KERN_DEBUG "set_cr0: #GP, CD == 0 && NW == 1\n");
352 inject_gp(vcpu);
353 return;
354 }
355
356 if ((cr0 & CR0_PG_MASK) && !(cr0 & CR0_PE_MASK)) {
357 printk(KERN_DEBUG "set_cr0: #GP, set PG flag "
358 "and a clear PE flag\n");
359 inject_gp(vcpu);
360 return;
361 }
362
363 if (!is_paging(vcpu) && (cr0 & CR0_PG_MASK)) {
05b3e0c2 364#ifdef CONFIG_X86_64
6aa8b732
AK
365 if ((vcpu->shadow_efer & EFER_LME)) {
366 int cs_db, cs_l;
367
368 if (!is_pae(vcpu)) {
369 printk(KERN_DEBUG "set_cr0: #GP, start paging "
370 "in long mode while PAE is disabled\n");
371 inject_gp(vcpu);
372 return;
373 }
374 kvm_arch_ops->get_cs_db_l_bits(vcpu, &cs_db, &cs_l);
375 if (cs_l) {
376 printk(KERN_DEBUG "set_cr0: #GP, start paging "
377 "in long mode while CS.L == 1\n");
378 inject_gp(vcpu);
379 return;
380
381 }
382 } else
383#endif
1342d353 384 if (is_pae(vcpu) && !load_pdptrs(vcpu, vcpu->cr3)) {
6aa8b732
AK
385 printk(KERN_DEBUG "set_cr0: #GP, pdptrs "
386 "reserved bits\n");
387 inject_gp(vcpu);
388 return;
389 }
390
391 }
392
393 kvm_arch_ops->set_cr0(vcpu, cr0);
394 vcpu->cr0 = cr0;
395
396 spin_lock(&vcpu->kvm->lock);
397 kvm_mmu_reset_context(vcpu);
398 spin_unlock(&vcpu->kvm->lock);
399 return;
400}
401EXPORT_SYMBOL_GPL(set_cr0);
402
403void lmsw(struct kvm_vcpu *vcpu, unsigned long msw)
404{
399badf3 405 kvm_arch_ops->decache_cr0_cr4_guest_bits(vcpu);
6aa8b732
AK
406 set_cr0(vcpu, (vcpu->cr0 & ~0x0ful) | (msw & 0x0f));
407}
408EXPORT_SYMBOL_GPL(lmsw);
409
410void set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)
411{
412 if (cr4 & CR4_RESEVED_BITS) {
413 printk(KERN_DEBUG "set_cr4: #GP, reserved bits\n");
414 inject_gp(vcpu);
415 return;
416 }
417
a9058ecd 418 if (is_long_mode(vcpu)) {
6aa8b732
AK
419 if (!(cr4 & CR4_PAE_MASK)) {
420 printk(KERN_DEBUG "set_cr4: #GP, clearing PAE while "
421 "in long mode\n");
422 inject_gp(vcpu);
423 return;
424 }
425 } else if (is_paging(vcpu) && !is_pae(vcpu) && (cr4 & CR4_PAE_MASK)
1342d353 426 && !load_pdptrs(vcpu, vcpu->cr3)) {
6aa8b732
AK
427 printk(KERN_DEBUG "set_cr4: #GP, pdptrs reserved bits\n");
428 inject_gp(vcpu);
429 }
430
431 if (cr4 & CR4_VMXE_MASK) {
432 printk(KERN_DEBUG "set_cr4: #GP, setting VMXE\n");
433 inject_gp(vcpu);
434 return;
435 }
436 kvm_arch_ops->set_cr4(vcpu, cr4);
437 spin_lock(&vcpu->kvm->lock);
438 kvm_mmu_reset_context(vcpu);
439 spin_unlock(&vcpu->kvm->lock);
440}
441EXPORT_SYMBOL_GPL(set_cr4);
442
443void set_cr3(struct kvm_vcpu *vcpu, unsigned long cr3)
444{
a9058ecd 445 if (is_long_mode(vcpu)) {
6aa8b732
AK
446 if ( cr3 & CR3_L_MODE_RESEVED_BITS) {
447 printk(KERN_DEBUG "set_cr3: #GP, reserved bits\n");
448 inject_gp(vcpu);
449 return;
450 }
451 } else {
452 if (cr3 & CR3_RESEVED_BITS) {
453 printk(KERN_DEBUG "set_cr3: #GP, reserved bits\n");
454 inject_gp(vcpu);
455 return;
456 }
457 if (is_paging(vcpu) && is_pae(vcpu) &&
1342d353 458 !load_pdptrs(vcpu, cr3)) {
6aa8b732
AK
459 printk(KERN_DEBUG "set_cr3: #GP, pdptrs "
460 "reserved bits\n");
461 inject_gp(vcpu);
462 return;
463 }
464 }
465
466 vcpu->cr3 = cr3;
467 spin_lock(&vcpu->kvm->lock);
d21225ee
IM
468 /*
469 * Does the new cr3 value map to physical memory? (Note, we
470 * catch an invalid cr3 even in real-mode, because it would
471 * cause trouble later on when we turn on paging anyway.)
472 *
473 * A real CPU would silently accept an invalid cr3 and would
474 * attempt to use it - with largely undefined (and often hard
475 * to debug) behavior on the guest side.
476 */
477 if (unlikely(!gfn_to_memslot(vcpu->kvm, cr3 >> PAGE_SHIFT)))
478 inject_gp(vcpu);
479 else
480 vcpu->mmu.new_cr3(vcpu);
6aa8b732
AK
481 spin_unlock(&vcpu->kvm->lock);
482}
483EXPORT_SYMBOL_GPL(set_cr3);
484
485void set_cr8(struct kvm_vcpu *vcpu, unsigned long cr8)
486{
487 if ( cr8 & CR8_RESEVED_BITS) {
488 printk(KERN_DEBUG "set_cr8: #GP, reserved bits 0x%lx\n", cr8);
489 inject_gp(vcpu);
490 return;
491 }
492 vcpu->cr8 = cr8;
493}
494EXPORT_SYMBOL_GPL(set_cr8);
495
496void fx_init(struct kvm_vcpu *vcpu)
497{
498 struct __attribute__ ((__packed__)) fx_image_s {
499 u16 control; //fcw
500 u16 status; //fsw
501 u16 tag; // ftw
502 u16 opcode; //fop
503 u64 ip; // fpu ip
504 u64 operand;// fpu dp
505 u32 mxcsr;
506 u32 mxcsr_mask;
507
508 } *fx_image;
509
510 fx_save(vcpu->host_fx_image);
511 fpu_init();
512 fx_save(vcpu->guest_fx_image);
513 fx_restore(vcpu->host_fx_image);
514
515 fx_image = (struct fx_image_s *)vcpu->guest_fx_image;
516 fx_image->mxcsr = 0x1f80;
517 memset(vcpu->guest_fx_image + sizeof(struct fx_image_s),
518 0, FX_IMAGE_SIZE - sizeof(struct fx_image_s));
519}
520EXPORT_SYMBOL_GPL(fx_init);
521
522/*
523 * Creates some virtual cpus. Good luck creating more than one.
524 */
525static int kvm_dev_ioctl_create_vcpu(struct kvm *kvm, int n)
526{
527 int r;
528 struct kvm_vcpu *vcpu;
529
530 r = -EINVAL;
5aacf0ca 531 if (!valid_vcpu(n))
6aa8b732
AK
532 goto out;
533
534 vcpu = &kvm->vcpus[n];
535
536 mutex_lock(&vcpu->mutex);
537
538 if (vcpu->vmcs) {
539 mutex_unlock(&vcpu->mutex);
540 return -EEXIST;
541 }
542
543 vcpu->host_fx_image = (char*)ALIGN((hva_t)vcpu->fx_buf,
544 FX_IMAGE_ALIGN);
545 vcpu->guest_fx_image = vcpu->host_fx_image + FX_IMAGE_SIZE;
546
547 vcpu->cpu = -1; /* First load will set up TR */
6aa8b732
AK
548 r = kvm_arch_ops->vcpu_create(vcpu);
549 if (r < 0)
550 goto out_free_vcpus;
551
8018c27b
IM
552 r = kvm_mmu_create(vcpu);
553 if (r < 0)
554 goto out_free_vcpus;
6aa8b732 555
8018c27b
IM
556 kvm_arch_ops->vcpu_load(vcpu);
557 r = kvm_mmu_setup(vcpu);
6aa8b732 558 if (r >= 0)
8018c27b 559 r = kvm_arch_ops->vcpu_setup(vcpu);
6aa8b732
AK
560 vcpu_put(vcpu);
561
562 if (r < 0)
563 goto out_free_vcpus;
564
565 return 0;
566
567out_free_vcpus:
568 kvm_free_vcpu(vcpu);
569 mutex_unlock(&vcpu->mutex);
570out:
571 return r;
572}
573
574/*
575 * Allocate some memory and give it an address in the guest physical address
576 * space.
577 *
578 * Discontiguous memory is allowed, mostly for framebuffers.
579 */
580static int kvm_dev_ioctl_set_memory_region(struct kvm *kvm,
581 struct kvm_memory_region *mem)
582{
583 int r;
584 gfn_t base_gfn;
585 unsigned long npages;
586 unsigned long i;
587 struct kvm_memory_slot *memslot;
588 struct kvm_memory_slot old, new;
589 int memory_config_version;
590
591 r = -EINVAL;
592 /* General sanity checks */
593 if (mem->memory_size & (PAGE_SIZE - 1))
594 goto out;
595 if (mem->guest_phys_addr & (PAGE_SIZE - 1))
596 goto out;
597 if (mem->slot >= KVM_MEMORY_SLOTS)
598 goto out;
599 if (mem->guest_phys_addr + mem->memory_size < mem->guest_phys_addr)
600 goto out;
601
602 memslot = &kvm->memslots[mem->slot];
603 base_gfn = mem->guest_phys_addr >> PAGE_SHIFT;
604 npages = mem->memory_size >> PAGE_SHIFT;
605
606 if (!npages)
607 mem->flags &= ~KVM_MEM_LOG_DIRTY_PAGES;
608
609raced:
610 spin_lock(&kvm->lock);
611
612 memory_config_version = kvm->memory_config_version;
613 new = old = *memslot;
614
615 new.base_gfn = base_gfn;
616 new.npages = npages;
617 new.flags = mem->flags;
618
619 /* Disallow changing a memory slot's size. */
620 r = -EINVAL;
621 if (npages && old.npages && npages != old.npages)
622 goto out_unlock;
623
624 /* Check for overlaps */
625 r = -EEXIST;
626 for (i = 0; i < KVM_MEMORY_SLOTS; ++i) {
627 struct kvm_memory_slot *s = &kvm->memslots[i];
628
629 if (s == memslot)
630 continue;
631 if (!((base_gfn + npages <= s->base_gfn) ||
632 (base_gfn >= s->base_gfn + s->npages)))
633 goto out_unlock;
634 }
635 /*
636 * Do memory allocations outside lock. memory_config_version will
637 * detect any races.
638 */
639 spin_unlock(&kvm->lock);
640
641 /* Deallocate if slot is being removed */
642 if (!npages)
8b6d44c7 643 new.phys_mem = NULL;
6aa8b732
AK
644
645 /* Free page dirty bitmap if unneeded */
646 if (!(new.flags & KVM_MEM_LOG_DIRTY_PAGES))
8b6d44c7 647 new.dirty_bitmap = NULL;
6aa8b732
AK
648
649 r = -ENOMEM;
650
651 /* Allocate if a slot is being created */
652 if (npages && !new.phys_mem) {
653 new.phys_mem = vmalloc(npages * sizeof(struct page *));
654
655 if (!new.phys_mem)
656 goto out_free;
657
658 memset(new.phys_mem, 0, npages * sizeof(struct page *));
659 for (i = 0; i < npages; ++i) {
660 new.phys_mem[i] = alloc_page(GFP_HIGHUSER
661 | __GFP_ZERO);
662 if (!new.phys_mem[i])
663 goto out_free;
cd4a4e53 664 new.phys_mem[i]->private = 0;
6aa8b732
AK
665 }
666 }
667
668 /* Allocate page dirty bitmap if needed */
669 if ((new.flags & KVM_MEM_LOG_DIRTY_PAGES) && !new.dirty_bitmap) {
670 unsigned dirty_bytes = ALIGN(npages, BITS_PER_LONG) / 8;
671
672 new.dirty_bitmap = vmalloc(dirty_bytes);
673 if (!new.dirty_bitmap)
674 goto out_free;
675 memset(new.dirty_bitmap, 0, dirty_bytes);
676 }
677
678 spin_lock(&kvm->lock);
679
680 if (memory_config_version != kvm->memory_config_version) {
681 spin_unlock(&kvm->lock);
682 kvm_free_physmem_slot(&new, &old);
683 goto raced;
684 }
685
686 r = -EAGAIN;
687 if (kvm->busy)
688 goto out_unlock;
689
690 if (mem->slot >= kvm->nmemslots)
691 kvm->nmemslots = mem->slot + 1;
692
693 *memslot = new;
694 ++kvm->memory_config_version;
695
696 spin_unlock(&kvm->lock);
697
698 for (i = 0; i < KVM_MAX_VCPUS; ++i) {
699 struct kvm_vcpu *vcpu;
700
701 vcpu = vcpu_load(kvm, i);
702 if (!vcpu)
703 continue;
704 kvm_mmu_reset_context(vcpu);
705 vcpu_put(vcpu);
706 }
707
708 kvm_free_physmem_slot(&old, &new);
709 return 0;
710
711out_unlock:
712 spin_unlock(&kvm->lock);
713out_free:
714 kvm_free_physmem_slot(&new, &old);
715out:
716 return r;
717}
718
714b93da
AK
719static void do_remove_write_access(struct kvm_vcpu *vcpu, int slot)
720{
721 spin_lock(&vcpu->kvm->lock);
722 kvm_mmu_slot_remove_write_access(vcpu, slot);
723 spin_unlock(&vcpu->kvm->lock);
724}
725
6aa8b732
AK
726/*
727 * Get (and clear) the dirty memory log for a memory slot.
728 */
729static int kvm_dev_ioctl_get_dirty_log(struct kvm *kvm,
730 struct kvm_dirty_log *log)
731{
732 struct kvm_memory_slot *memslot;
733 int r, i;
734 int n;
714b93da 735 int cleared;
6aa8b732
AK
736 unsigned long any = 0;
737
738 spin_lock(&kvm->lock);
739
740 /*
741 * Prevent changes to guest memory configuration even while the lock
742 * is not taken.
743 */
744 ++kvm->busy;
745 spin_unlock(&kvm->lock);
746 r = -EINVAL;
747 if (log->slot >= KVM_MEMORY_SLOTS)
748 goto out;
749
750 memslot = &kvm->memslots[log->slot];
751 r = -ENOENT;
752 if (!memslot->dirty_bitmap)
753 goto out;
754
755 n = ALIGN(memslot->npages, 8) / 8;
756
757 for (i = 0; !any && i < n; ++i)
758 any = memslot->dirty_bitmap[i];
759
760 r = -EFAULT;
761 if (copy_to_user(log->dirty_bitmap, memslot->dirty_bitmap, n))
762 goto out;
763
764
765 if (any) {
714b93da 766 cleared = 0;
6aa8b732
AK
767 for (i = 0; i < KVM_MAX_VCPUS; ++i) {
768 struct kvm_vcpu *vcpu = vcpu_load(kvm, i);
769
770 if (!vcpu)
771 continue;
714b93da
AK
772 if (!cleared) {
773 do_remove_write_access(vcpu, log->slot);
774 memset(memslot->dirty_bitmap, 0, n);
775 cleared = 1;
776 }
6aa8b732
AK
777 kvm_arch_ops->tlb_flush(vcpu);
778 vcpu_put(vcpu);
779 }
780 }
781
782 r = 0;
783
784out:
785 spin_lock(&kvm->lock);
786 --kvm->busy;
787 spin_unlock(&kvm->lock);
788 return r;
789}
790
791struct kvm_memory_slot *gfn_to_memslot(struct kvm *kvm, gfn_t gfn)
792{
793 int i;
794
795 for (i = 0; i < kvm->nmemslots; ++i) {
796 struct kvm_memory_slot *memslot = &kvm->memslots[i];
797
798 if (gfn >= memslot->base_gfn
799 && gfn < memslot->base_gfn + memslot->npages)
800 return memslot;
801 }
8b6d44c7 802 return NULL;
6aa8b732
AK
803}
804EXPORT_SYMBOL_GPL(gfn_to_memslot);
805
806void mark_page_dirty(struct kvm *kvm, gfn_t gfn)
807{
808 int i;
8b6d44c7 809 struct kvm_memory_slot *memslot = NULL;
6aa8b732
AK
810 unsigned long rel_gfn;
811
812 for (i = 0; i < kvm->nmemslots; ++i) {
813 memslot = &kvm->memslots[i];
814
815 if (gfn >= memslot->base_gfn
816 && gfn < memslot->base_gfn + memslot->npages) {
817
818 if (!memslot || !memslot->dirty_bitmap)
819 return;
820
821 rel_gfn = gfn - memslot->base_gfn;
822
823 /* avoid RMW */
824 if (!test_bit(rel_gfn, memslot->dirty_bitmap))
825 set_bit(rel_gfn, memslot->dirty_bitmap);
826 return;
827 }
828 }
829}
830
831static int emulator_read_std(unsigned long addr,
832 unsigned long *val,
833 unsigned int bytes,
834 struct x86_emulate_ctxt *ctxt)
835{
836 struct kvm_vcpu *vcpu = ctxt->vcpu;
837 void *data = val;
838
839 while (bytes) {
840 gpa_t gpa = vcpu->mmu.gva_to_gpa(vcpu, addr);
841 unsigned offset = addr & (PAGE_SIZE-1);
842 unsigned tocopy = min(bytes, (unsigned)PAGE_SIZE - offset);
843 unsigned long pfn;
844 struct kvm_memory_slot *memslot;
845 void *page;
846
847 if (gpa == UNMAPPED_GVA)
848 return X86EMUL_PROPAGATE_FAULT;
849 pfn = gpa >> PAGE_SHIFT;
850 memslot = gfn_to_memslot(vcpu->kvm, pfn);
851 if (!memslot)
852 return X86EMUL_UNHANDLEABLE;
853 page = kmap_atomic(gfn_to_page(memslot, pfn), KM_USER0);
854
855 memcpy(data, page + offset, tocopy);
856
857 kunmap_atomic(page, KM_USER0);
858
859 bytes -= tocopy;
860 data += tocopy;
861 addr += tocopy;
862 }
863
864 return X86EMUL_CONTINUE;
865}
866
867static int emulator_write_std(unsigned long addr,
868 unsigned long val,
869 unsigned int bytes,
870 struct x86_emulate_ctxt *ctxt)
871{
872 printk(KERN_ERR "emulator_write_std: addr %lx n %d\n",
873 addr, bytes);
874 return X86EMUL_UNHANDLEABLE;
875}
876
877static int emulator_read_emulated(unsigned long addr,
878 unsigned long *val,
879 unsigned int bytes,
880 struct x86_emulate_ctxt *ctxt)
881{
882 struct kvm_vcpu *vcpu = ctxt->vcpu;
883
884 if (vcpu->mmio_read_completed) {
885 memcpy(val, vcpu->mmio_data, bytes);
886 vcpu->mmio_read_completed = 0;
887 return X86EMUL_CONTINUE;
888 } else if (emulator_read_std(addr, val, bytes, ctxt)
889 == X86EMUL_CONTINUE)
890 return X86EMUL_CONTINUE;
891 else {
892 gpa_t gpa = vcpu->mmu.gva_to_gpa(vcpu, addr);
893 if (gpa == UNMAPPED_GVA)
894 return vcpu_printf(vcpu, "not present\n"), X86EMUL_PROPAGATE_FAULT;
895 vcpu->mmio_needed = 1;
896 vcpu->mmio_phys_addr = gpa;
897 vcpu->mmio_size = bytes;
898 vcpu->mmio_is_write = 0;
899
900 return X86EMUL_UNHANDLEABLE;
901 }
902}
903
da4a00f0
AK
904static int emulator_write_phys(struct kvm_vcpu *vcpu, gpa_t gpa,
905 unsigned long val, int bytes)
906{
907 struct kvm_memory_slot *m;
908 struct page *page;
909 void *virt;
910
911 if (((gpa + bytes - 1) >> PAGE_SHIFT) != (gpa >> PAGE_SHIFT))
912 return 0;
913 m = gfn_to_memslot(vcpu->kvm, gpa >> PAGE_SHIFT);
914 if (!m)
915 return 0;
916 page = gfn_to_page(m, gpa >> PAGE_SHIFT);
917 kvm_mmu_pre_write(vcpu, gpa, bytes);
918 virt = kmap_atomic(page, KM_USER0);
919 memcpy(virt + offset_in_page(gpa), &val, bytes);
920 kunmap_atomic(virt, KM_USER0);
921 kvm_mmu_post_write(vcpu, gpa, bytes);
922 return 1;
923}
924
6aa8b732
AK
925static int emulator_write_emulated(unsigned long addr,
926 unsigned long val,
927 unsigned int bytes,
928 struct x86_emulate_ctxt *ctxt)
929{
930 struct kvm_vcpu *vcpu = ctxt->vcpu;
931 gpa_t gpa = vcpu->mmu.gva_to_gpa(vcpu, addr);
932
933 if (gpa == UNMAPPED_GVA)
934 return X86EMUL_PROPAGATE_FAULT;
935
da4a00f0
AK
936 if (emulator_write_phys(vcpu, gpa, val, bytes))
937 return X86EMUL_CONTINUE;
938
6aa8b732
AK
939 vcpu->mmio_needed = 1;
940 vcpu->mmio_phys_addr = gpa;
941 vcpu->mmio_size = bytes;
942 vcpu->mmio_is_write = 1;
943 memcpy(vcpu->mmio_data, &val, bytes);
944
945 return X86EMUL_CONTINUE;
946}
947
948static int emulator_cmpxchg_emulated(unsigned long addr,
949 unsigned long old,
950 unsigned long new,
951 unsigned int bytes,
952 struct x86_emulate_ctxt *ctxt)
953{
954 static int reported;
955
956 if (!reported) {
957 reported = 1;
958 printk(KERN_WARNING "kvm: emulating exchange as write\n");
959 }
960 return emulator_write_emulated(addr, new, bytes, ctxt);
961}
962
32b35627
AK
963#ifdef CONFIG_X86_32
964
965static int emulator_cmpxchg8b_emulated(unsigned long addr,
966 unsigned long old_lo,
967 unsigned long old_hi,
968 unsigned long new_lo,
969 unsigned long new_hi,
970 struct x86_emulate_ctxt *ctxt)
971{
972 static int reported;
973 int r;
974
975 if (!reported) {
976 reported = 1;
977 printk(KERN_WARNING "kvm: emulating exchange8b as write\n");
978 }
979 r = emulator_write_emulated(addr, new_lo, 4, ctxt);
980 if (r != X86EMUL_CONTINUE)
981 return r;
982 return emulator_write_emulated(addr+4, new_hi, 4, ctxt);
983}
984
985#endif
986
6aa8b732
AK
987static unsigned long get_segment_base(struct kvm_vcpu *vcpu, int seg)
988{
989 return kvm_arch_ops->get_segment_base(vcpu, seg);
990}
991
992int emulate_invlpg(struct kvm_vcpu *vcpu, gva_t address)
993{
6aa8b732
AK
994 return X86EMUL_CONTINUE;
995}
996
997int emulate_clts(struct kvm_vcpu *vcpu)
998{
399badf3 999 unsigned long cr0;
6aa8b732 1000
399badf3
AK
1001 kvm_arch_ops->decache_cr0_cr4_guest_bits(vcpu);
1002 cr0 = vcpu->cr0 & ~CR0_TS_MASK;
6aa8b732
AK
1003 kvm_arch_ops->set_cr0(vcpu, cr0);
1004 return X86EMUL_CONTINUE;
1005}
1006
1007int emulator_get_dr(struct x86_emulate_ctxt* ctxt, int dr, unsigned long *dest)
1008{
1009 struct kvm_vcpu *vcpu = ctxt->vcpu;
1010
1011 switch (dr) {
1012 case 0 ... 3:
1013 *dest = kvm_arch_ops->get_dr(vcpu, dr);
1014 return X86EMUL_CONTINUE;
1015 default:
1016 printk(KERN_DEBUG "%s: unexpected dr %u\n",
1017 __FUNCTION__, dr);
1018 return X86EMUL_UNHANDLEABLE;
1019 }
1020}
1021
1022int emulator_set_dr(struct x86_emulate_ctxt *ctxt, int dr, unsigned long value)
1023{
1024 unsigned long mask = (ctxt->mode == X86EMUL_MODE_PROT64) ? ~0ULL : ~0U;
1025 int exception;
1026
1027 kvm_arch_ops->set_dr(ctxt->vcpu, dr, value & mask, &exception);
1028 if (exception) {
1029 /* FIXME: better handling */
1030 return X86EMUL_UNHANDLEABLE;
1031 }
1032 return X86EMUL_CONTINUE;
1033}
1034
1035static void report_emulation_failure(struct x86_emulate_ctxt *ctxt)
1036{
1037 static int reported;
1038 u8 opcodes[4];
1039 unsigned long rip = ctxt->vcpu->rip;
1040 unsigned long rip_linear;
1041
1042 rip_linear = rip + get_segment_base(ctxt->vcpu, VCPU_SREG_CS);
1043
1044 if (reported)
1045 return;
1046
1047 emulator_read_std(rip_linear, (void *)opcodes, 4, ctxt);
1048
1049 printk(KERN_ERR "emulation failed but !mmio_needed?"
1050 " rip %lx %02x %02x %02x %02x\n",
1051 rip, opcodes[0], opcodes[1], opcodes[2], opcodes[3]);
1052 reported = 1;
1053}
1054
1055struct x86_emulate_ops emulate_ops = {
1056 .read_std = emulator_read_std,
1057 .write_std = emulator_write_std,
1058 .read_emulated = emulator_read_emulated,
1059 .write_emulated = emulator_write_emulated,
1060 .cmpxchg_emulated = emulator_cmpxchg_emulated,
32b35627
AK
1061#ifdef CONFIG_X86_32
1062 .cmpxchg8b_emulated = emulator_cmpxchg8b_emulated,
1063#endif
6aa8b732
AK
1064};
1065
1066int emulate_instruction(struct kvm_vcpu *vcpu,
1067 struct kvm_run *run,
1068 unsigned long cr2,
1069 u16 error_code)
1070{
1071 struct x86_emulate_ctxt emulate_ctxt;
1072 int r;
1073 int cs_db, cs_l;
1074
1075 kvm_arch_ops->cache_regs(vcpu);
1076
1077 kvm_arch_ops->get_cs_db_l_bits(vcpu, &cs_db, &cs_l);
1078
1079 emulate_ctxt.vcpu = vcpu;
1080 emulate_ctxt.eflags = kvm_arch_ops->get_rflags(vcpu);
1081 emulate_ctxt.cr2 = cr2;
1082 emulate_ctxt.mode = (emulate_ctxt.eflags & X86_EFLAGS_VM)
1083 ? X86EMUL_MODE_REAL : cs_l
1084 ? X86EMUL_MODE_PROT64 : cs_db
1085 ? X86EMUL_MODE_PROT32 : X86EMUL_MODE_PROT16;
1086
1087 if (emulate_ctxt.mode == X86EMUL_MODE_PROT64) {
1088 emulate_ctxt.cs_base = 0;
1089 emulate_ctxt.ds_base = 0;
1090 emulate_ctxt.es_base = 0;
1091 emulate_ctxt.ss_base = 0;
1092 } else {
1093 emulate_ctxt.cs_base = get_segment_base(vcpu, VCPU_SREG_CS);
1094 emulate_ctxt.ds_base = get_segment_base(vcpu, VCPU_SREG_DS);
1095 emulate_ctxt.es_base = get_segment_base(vcpu, VCPU_SREG_ES);
1096 emulate_ctxt.ss_base = get_segment_base(vcpu, VCPU_SREG_SS);
1097 }
1098
1099 emulate_ctxt.gs_base = get_segment_base(vcpu, VCPU_SREG_GS);
1100 emulate_ctxt.fs_base = get_segment_base(vcpu, VCPU_SREG_FS);
1101
1102 vcpu->mmio_is_write = 0;
1103 r = x86_emulate_memop(&emulate_ctxt, &emulate_ops);
1104
1105 if ((r || vcpu->mmio_is_write) && run) {
1106 run->mmio.phys_addr = vcpu->mmio_phys_addr;
1107 memcpy(run->mmio.data, vcpu->mmio_data, 8);
1108 run->mmio.len = vcpu->mmio_size;
1109 run->mmio.is_write = vcpu->mmio_is_write;
1110 }
1111
1112 if (r) {
a436036b
AK
1113 if (kvm_mmu_unprotect_page_virt(vcpu, cr2))
1114 return EMULATE_DONE;
6aa8b732
AK
1115 if (!vcpu->mmio_needed) {
1116 report_emulation_failure(&emulate_ctxt);
1117 return EMULATE_FAIL;
1118 }
1119 return EMULATE_DO_MMIO;
1120 }
1121
1122 kvm_arch_ops->decache_regs(vcpu);
1123 kvm_arch_ops->set_rflags(vcpu, emulate_ctxt.eflags);
1124
1125 if (vcpu->mmio_is_write)
1126 return EMULATE_DO_MMIO;
1127
1128 return EMULATE_DONE;
1129}
1130EXPORT_SYMBOL_GPL(emulate_instruction);
1131
1132static u64 mk_cr_64(u64 curr_cr, u32 new_val)
1133{
1134 return (curr_cr & ~((1ULL << 32) - 1)) | new_val;
1135}
1136
1137void realmode_lgdt(struct kvm_vcpu *vcpu, u16 limit, unsigned long base)
1138{
1139 struct descriptor_table dt = { limit, base };
1140
1141 kvm_arch_ops->set_gdt(vcpu, &dt);
1142}
1143
1144void realmode_lidt(struct kvm_vcpu *vcpu, u16 limit, unsigned long base)
1145{
1146 struct descriptor_table dt = { limit, base };
1147
1148 kvm_arch_ops->set_idt(vcpu, &dt);
1149}
1150
1151void realmode_lmsw(struct kvm_vcpu *vcpu, unsigned long msw,
1152 unsigned long *rflags)
1153{
1154 lmsw(vcpu, msw);
1155 *rflags = kvm_arch_ops->get_rflags(vcpu);
1156}
1157
1158unsigned long realmode_get_cr(struct kvm_vcpu *vcpu, int cr)
1159{
399badf3 1160 kvm_arch_ops->decache_cr0_cr4_guest_bits(vcpu);
6aa8b732
AK
1161 switch (cr) {
1162 case 0:
1163 return vcpu->cr0;
1164 case 2:
1165 return vcpu->cr2;
1166 case 3:
1167 return vcpu->cr3;
1168 case 4:
1169 return vcpu->cr4;
1170 default:
1171 vcpu_printf(vcpu, "%s: unexpected cr %u\n", __FUNCTION__, cr);
1172 return 0;
1173 }
1174}
1175
1176void realmode_set_cr(struct kvm_vcpu *vcpu, int cr, unsigned long val,
1177 unsigned long *rflags)
1178{
1179 switch (cr) {
1180 case 0:
1181 set_cr0(vcpu, mk_cr_64(vcpu->cr0, val));
1182 *rflags = kvm_arch_ops->get_rflags(vcpu);
1183 break;
1184 case 2:
1185 vcpu->cr2 = val;
1186 break;
1187 case 3:
1188 set_cr3(vcpu, val);
1189 break;
1190 case 4:
1191 set_cr4(vcpu, mk_cr_64(vcpu->cr4, val));
1192 break;
1193 default:
1194 vcpu_printf(vcpu, "%s: unexpected cr %u\n", __FUNCTION__, cr);
1195 }
1196}
1197
3bab1f5d
AK
1198int kvm_get_msr_common(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata)
1199{
1200 u64 data;
1201
1202 switch (msr) {
1203 case 0xc0010010: /* SYSCFG */
1204 case 0xc0010015: /* HWCR */
1205 case MSR_IA32_PLATFORM_ID:
1206 case MSR_IA32_P5_MC_ADDR:
1207 case MSR_IA32_P5_MC_TYPE:
1208 case MSR_IA32_MC0_CTL:
1209 case MSR_IA32_MCG_STATUS:
1210 case MSR_IA32_MCG_CAP:
1211 case MSR_IA32_MC0_MISC:
1212 case MSR_IA32_MC0_MISC+4:
1213 case MSR_IA32_MC0_MISC+8:
1214 case MSR_IA32_MC0_MISC+12:
1215 case MSR_IA32_MC0_MISC+16:
1216 case MSR_IA32_UCODE_REV:
a8d13ea2 1217 case MSR_IA32_PERF_STATUS:
3bab1f5d
AK
1218 /* MTRR registers */
1219 case 0xfe:
1220 case 0x200 ... 0x2ff:
1221 data = 0;
1222 break;
a8d13ea2
AK
1223 case 0xcd: /* fsb frequency */
1224 data = 3;
1225 break;
3bab1f5d
AK
1226 case MSR_IA32_APICBASE:
1227 data = vcpu->apic_base;
1228 break;
6f00e68f
AK
1229 case MSR_IA32_MISC_ENABLE:
1230 data = vcpu->ia32_misc_enable_msr;
1231 break;
3bab1f5d
AK
1232#ifdef CONFIG_X86_64
1233 case MSR_EFER:
1234 data = vcpu->shadow_efer;
1235 break;
1236#endif
1237 default:
1238 printk(KERN_ERR "kvm: unhandled rdmsr: 0x%x\n", msr);
1239 return 1;
1240 }
1241 *pdata = data;
1242 return 0;
1243}
1244EXPORT_SYMBOL_GPL(kvm_get_msr_common);
1245
6aa8b732
AK
1246/*
1247 * Reads an msr value (of 'msr_index') into 'pdata'.
1248 * Returns 0 on success, non-0 otherwise.
1249 * Assumes vcpu_load() was already called.
1250 */
1251static int get_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 *pdata)
1252{
1253 return kvm_arch_ops->get_msr(vcpu, msr_index, pdata);
1254}
1255
05b3e0c2 1256#ifdef CONFIG_X86_64
6aa8b732 1257
3bab1f5d 1258static void set_efer(struct kvm_vcpu *vcpu, u64 efer)
6aa8b732 1259{
6aa8b732
AK
1260 if (efer & EFER_RESERVED_BITS) {
1261 printk(KERN_DEBUG "set_efer: 0x%llx #GP, reserved bits\n",
1262 efer);
1263 inject_gp(vcpu);
1264 return;
1265 }
1266
1267 if (is_paging(vcpu)
1268 && (vcpu->shadow_efer & EFER_LME) != (efer & EFER_LME)) {
1269 printk(KERN_DEBUG "set_efer: #GP, change LME while paging\n");
1270 inject_gp(vcpu);
1271 return;
1272 }
1273
7725f0ba
AK
1274 kvm_arch_ops->set_efer(vcpu, efer);
1275
6aa8b732
AK
1276 efer &= ~EFER_LMA;
1277 efer |= vcpu->shadow_efer & EFER_LMA;
1278
1279 vcpu->shadow_efer = efer;
6aa8b732 1280}
6aa8b732
AK
1281
1282#endif
1283
3bab1f5d
AK
1284int kvm_set_msr_common(struct kvm_vcpu *vcpu, u32 msr, u64 data)
1285{
1286 switch (msr) {
1287#ifdef CONFIG_X86_64
1288 case MSR_EFER:
1289 set_efer(vcpu, data);
1290 break;
1291#endif
1292 case MSR_IA32_MC0_STATUS:
1293 printk(KERN_WARNING "%s: MSR_IA32_MC0_STATUS 0x%llx, nop\n",
1294 __FUNCTION__, data);
1295 break;
1296 case MSR_IA32_UCODE_REV:
1297 case MSR_IA32_UCODE_WRITE:
1298 case 0x200 ... 0x2ff: /* MTRRs */
1299 break;
1300 case MSR_IA32_APICBASE:
1301 vcpu->apic_base = data;
1302 break;
6f00e68f
AK
1303 case MSR_IA32_MISC_ENABLE:
1304 vcpu->ia32_misc_enable_msr = data;
1305 break;
3bab1f5d
AK
1306 default:
1307 printk(KERN_ERR "kvm: unhandled wrmsr: 0x%x\n", msr);
1308 return 1;
1309 }
1310 return 0;
1311}
1312EXPORT_SYMBOL_GPL(kvm_set_msr_common);
1313
6aa8b732
AK
1314/*
1315 * Writes msr value into into the appropriate "register".
1316 * Returns 0 on success, non-0 otherwise.
1317 * Assumes vcpu_load() was already called.
1318 */
1319static int set_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 data)
1320{
1321 return kvm_arch_ops->set_msr(vcpu, msr_index, data);
1322}
1323
1324void kvm_resched(struct kvm_vcpu *vcpu)
1325{
1326 vcpu_put(vcpu);
1327 cond_resched();
1328 /* Cannot fail - no vcpu unplug yet. */
1329 vcpu_load(vcpu->kvm, vcpu_slot(vcpu));
1330}
1331EXPORT_SYMBOL_GPL(kvm_resched);
1332
1333void load_msrs(struct vmx_msr_entry *e, int n)
1334{
1335 int i;
1336
1337 for (i = 0; i < n; ++i)
1338 wrmsrl(e[i].index, e[i].data);
1339}
1340EXPORT_SYMBOL_GPL(load_msrs);
1341
1342void save_msrs(struct vmx_msr_entry *e, int n)
1343{
1344 int i;
1345
1346 for (i = 0; i < n; ++i)
1347 rdmsrl(e[i].index, e[i].data);
1348}
1349EXPORT_SYMBOL_GPL(save_msrs);
1350
1351static int kvm_dev_ioctl_run(struct kvm *kvm, struct kvm_run *kvm_run)
1352{
1353 struct kvm_vcpu *vcpu;
1354 int r;
1355
5aacf0ca 1356 if (!valid_vcpu(kvm_run->vcpu))
6aa8b732
AK
1357 return -EINVAL;
1358
1359 vcpu = vcpu_load(kvm, kvm_run->vcpu);
1360 if (!vcpu)
1361 return -ENOENT;
1362
54810342
DL
1363 /* re-sync apic's tpr */
1364 vcpu->cr8 = kvm_run->cr8;
1365
6aa8b732
AK
1366 if (kvm_run->emulated) {
1367 kvm_arch_ops->skip_emulated_instruction(vcpu);
1368 kvm_run->emulated = 0;
1369 }
1370
1371 if (kvm_run->mmio_completed) {
1372 memcpy(vcpu->mmio_data, kvm_run->mmio.data, 8);
1373 vcpu->mmio_read_completed = 1;
1374 }
1375
1376 vcpu->mmio_needed = 0;
1377
1378 r = kvm_arch_ops->run(vcpu, kvm_run);
1379
1380 vcpu_put(vcpu);
1381 return r;
1382}
1383
1384static int kvm_dev_ioctl_get_regs(struct kvm *kvm, struct kvm_regs *regs)
1385{
1386 struct kvm_vcpu *vcpu;
1387
5aacf0ca 1388 if (!valid_vcpu(regs->vcpu))
6aa8b732
AK
1389 return -EINVAL;
1390
1391 vcpu = vcpu_load(kvm, regs->vcpu);
1392 if (!vcpu)
1393 return -ENOENT;
1394
1395 kvm_arch_ops->cache_regs(vcpu);
1396
1397 regs->rax = vcpu->regs[VCPU_REGS_RAX];
1398 regs->rbx = vcpu->regs[VCPU_REGS_RBX];
1399 regs->rcx = vcpu->regs[VCPU_REGS_RCX];
1400 regs->rdx = vcpu->regs[VCPU_REGS_RDX];
1401 regs->rsi = vcpu->regs[VCPU_REGS_RSI];
1402 regs->rdi = vcpu->regs[VCPU_REGS_RDI];
1403 regs->rsp = vcpu->regs[VCPU_REGS_RSP];
1404 regs->rbp = vcpu->regs[VCPU_REGS_RBP];
05b3e0c2 1405#ifdef CONFIG_X86_64
6aa8b732
AK
1406 regs->r8 = vcpu->regs[VCPU_REGS_R8];
1407 regs->r9 = vcpu->regs[VCPU_REGS_R9];
1408 regs->r10 = vcpu->regs[VCPU_REGS_R10];
1409 regs->r11 = vcpu->regs[VCPU_REGS_R11];
1410 regs->r12 = vcpu->regs[VCPU_REGS_R12];
1411 regs->r13 = vcpu->regs[VCPU_REGS_R13];
1412 regs->r14 = vcpu->regs[VCPU_REGS_R14];
1413 regs->r15 = vcpu->regs[VCPU_REGS_R15];
1414#endif
1415
1416 regs->rip = vcpu->rip;
1417 regs->rflags = kvm_arch_ops->get_rflags(vcpu);
1418
1419 /*
1420 * Don't leak debug flags in case they were set for guest debugging
1421 */
1422 if (vcpu->guest_debug.enabled && vcpu->guest_debug.singlestep)
1423 regs->rflags &= ~(X86_EFLAGS_TF | X86_EFLAGS_RF);
1424
1425 vcpu_put(vcpu);
1426
1427 return 0;
1428}
1429
1430static int kvm_dev_ioctl_set_regs(struct kvm *kvm, struct kvm_regs *regs)
1431{
1432 struct kvm_vcpu *vcpu;
1433
5aacf0ca 1434 if (!valid_vcpu(regs->vcpu))
6aa8b732
AK
1435 return -EINVAL;
1436
1437 vcpu = vcpu_load(kvm, regs->vcpu);
1438 if (!vcpu)
1439 return -ENOENT;
1440
1441 vcpu->regs[VCPU_REGS_RAX] = regs->rax;
1442 vcpu->regs[VCPU_REGS_RBX] = regs->rbx;
1443 vcpu->regs[VCPU_REGS_RCX] = regs->rcx;
1444 vcpu->regs[VCPU_REGS_RDX] = regs->rdx;
1445 vcpu->regs[VCPU_REGS_RSI] = regs->rsi;
1446 vcpu->regs[VCPU_REGS_RDI] = regs->rdi;
1447 vcpu->regs[VCPU_REGS_RSP] = regs->rsp;
1448 vcpu->regs[VCPU_REGS_RBP] = regs->rbp;
05b3e0c2 1449#ifdef CONFIG_X86_64
6aa8b732
AK
1450 vcpu->regs[VCPU_REGS_R8] = regs->r8;
1451 vcpu->regs[VCPU_REGS_R9] = regs->r9;
1452 vcpu->regs[VCPU_REGS_R10] = regs->r10;
1453 vcpu->regs[VCPU_REGS_R11] = regs->r11;
1454 vcpu->regs[VCPU_REGS_R12] = regs->r12;
1455 vcpu->regs[VCPU_REGS_R13] = regs->r13;
1456 vcpu->regs[VCPU_REGS_R14] = regs->r14;
1457 vcpu->regs[VCPU_REGS_R15] = regs->r15;
1458#endif
1459
1460 vcpu->rip = regs->rip;
1461 kvm_arch_ops->set_rflags(vcpu, regs->rflags);
1462
1463 kvm_arch_ops->decache_regs(vcpu);
1464
1465 vcpu_put(vcpu);
1466
1467 return 0;
1468}
1469
1470static void get_segment(struct kvm_vcpu *vcpu,
1471 struct kvm_segment *var, int seg)
1472{
1473 return kvm_arch_ops->get_segment(vcpu, var, seg);
1474}
1475
1476static int kvm_dev_ioctl_get_sregs(struct kvm *kvm, struct kvm_sregs *sregs)
1477{
1478 struct kvm_vcpu *vcpu;
1479 struct descriptor_table dt;
1480
5aacf0ca 1481 if (!valid_vcpu(sregs->vcpu))
6aa8b732
AK
1482 return -EINVAL;
1483 vcpu = vcpu_load(kvm, sregs->vcpu);
1484 if (!vcpu)
1485 return -ENOENT;
1486
1487 get_segment(vcpu, &sregs->cs, VCPU_SREG_CS);
1488 get_segment(vcpu, &sregs->ds, VCPU_SREG_DS);
1489 get_segment(vcpu, &sregs->es, VCPU_SREG_ES);
1490 get_segment(vcpu, &sregs->fs, VCPU_SREG_FS);
1491 get_segment(vcpu, &sregs->gs, VCPU_SREG_GS);
1492 get_segment(vcpu, &sregs->ss, VCPU_SREG_SS);
1493
1494 get_segment(vcpu, &sregs->tr, VCPU_SREG_TR);
1495 get_segment(vcpu, &sregs->ldt, VCPU_SREG_LDTR);
1496
1497 kvm_arch_ops->get_idt(vcpu, &dt);
1498 sregs->idt.limit = dt.limit;
1499 sregs->idt.base = dt.base;
1500 kvm_arch_ops->get_gdt(vcpu, &dt);
1501 sregs->gdt.limit = dt.limit;
1502 sregs->gdt.base = dt.base;
1503
399badf3 1504 kvm_arch_ops->decache_cr0_cr4_guest_bits(vcpu);
6aa8b732
AK
1505 sregs->cr0 = vcpu->cr0;
1506 sregs->cr2 = vcpu->cr2;
1507 sregs->cr3 = vcpu->cr3;
1508 sregs->cr4 = vcpu->cr4;
1509 sregs->cr8 = vcpu->cr8;
1510 sregs->efer = vcpu->shadow_efer;
1511 sregs->apic_base = vcpu->apic_base;
1512
1513 memcpy(sregs->interrupt_bitmap, vcpu->irq_pending,
1514 sizeof sregs->interrupt_bitmap);
1515
1516 vcpu_put(vcpu);
1517
1518 return 0;
1519}
1520
1521static void set_segment(struct kvm_vcpu *vcpu,
1522 struct kvm_segment *var, int seg)
1523{
1524 return kvm_arch_ops->set_segment(vcpu, var, seg);
1525}
1526
1527static int kvm_dev_ioctl_set_sregs(struct kvm *kvm, struct kvm_sregs *sregs)
1528{
1529 struct kvm_vcpu *vcpu;
1530 int mmu_reset_needed = 0;
1531 int i;
1532 struct descriptor_table dt;
1533
5aacf0ca 1534 if (!valid_vcpu(sregs->vcpu))
6aa8b732
AK
1535 return -EINVAL;
1536 vcpu = vcpu_load(kvm, sregs->vcpu);
1537 if (!vcpu)
1538 return -ENOENT;
1539
1540 set_segment(vcpu, &sregs->cs, VCPU_SREG_CS);
1541 set_segment(vcpu, &sregs->ds, VCPU_SREG_DS);
1542 set_segment(vcpu, &sregs->es, VCPU_SREG_ES);
1543 set_segment(vcpu, &sregs->fs, VCPU_SREG_FS);
1544 set_segment(vcpu, &sregs->gs, VCPU_SREG_GS);
1545 set_segment(vcpu, &sregs->ss, VCPU_SREG_SS);
1546
1547 set_segment(vcpu, &sregs->tr, VCPU_SREG_TR);
1548 set_segment(vcpu, &sregs->ldt, VCPU_SREG_LDTR);
1549
1550 dt.limit = sregs->idt.limit;
1551 dt.base = sregs->idt.base;
1552 kvm_arch_ops->set_idt(vcpu, &dt);
1553 dt.limit = sregs->gdt.limit;
1554 dt.base = sregs->gdt.base;
1555 kvm_arch_ops->set_gdt(vcpu, &dt);
1556
1557 vcpu->cr2 = sregs->cr2;
1558 mmu_reset_needed |= vcpu->cr3 != sregs->cr3;
1559 vcpu->cr3 = sregs->cr3;
1560
1561 vcpu->cr8 = sregs->cr8;
1562
1563 mmu_reset_needed |= vcpu->shadow_efer != sregs->efer;
05b3e0c2 1564#ifdef CONFIG_X86_64
6aa8b732
AK
1565 kvm_arch_ops->set_efer(vcpu, sregs->efer);
1566#endif
1567 vcpu->apic_base = sregs->apic_base;
1568
399badf3
AK
1569 kvm_arch_ops->decache_cr0_cr4_guest_bits(vcpu);
1570
6aa8b732
AK
1571 mmu_reset_needed |= vcpu->cr0 != sregs->cr0;
1572 kvm_arch_ops->set_cr0_no_modeswitch(vcpu, sregs->cr0);
1573
1574 mmu_reset_needed |= vcpu->cr4 != sregs->cr4;
1575 kvm_arch_ops->set_cr4(vcpu, sregs->cr4);
1b0973bd
AK
1576 if (!is_long_mode(vcpu) && is_pae(vcpu))
1577 load_pdptrs(vcpu, vcpu->cr3);
6aa8b732
AK
1578
1579 if (mmu_reset_needed)
1580 kvm_mmu_reset_context(vcpu);
1581
1582 memcpy(vcpu->irq_pending, sregs->interrupt_bitmap,
1583 sizeof vcpu->irq_pending);
1584 vcpu->irq_summary = 0;
1585 for (i = 0; i < NR_IRQ_WORDS; ++i)
1586 if (vcpu->irq_pending[i])
1587 __set_bit(i, &vcpu->irq_summary);
1588
1589 vcpu_put(vcpu);
1590
1591 return 0;
1592}
1593
1594/*
1595 * List of msr numbers which we expose to userspace through KVM_GET_MSRS
1596 * and KVM_SET_MSRS, and KVM_GET_MSR_INDEX_LIST.
bf591b24
MR
1597 *
1598 * This list is modified at module load time to reflect the
1599 * capabilities of the host cpu.
6aa8b732
AK
1600 */
1601static u32 msrs_to_save[] = {
1602 MSR_IA32_SYSENTER_CS, MSR_IA32_SYSENTER_ESP, MSR_IA32_SYSENTER_EIP,
1603 MSR_K6_STAR,
05b3e0c2 1604#ifdef CONFIG_X86_64
6aa8b732
AK
1605 MSR_CSTAR, MSR_KERNEL_GS_BASE, MSR_SYSCALL_MASK, MSR_LSTAR,
1606#endif
1607 MSR_IA32_TIME_STAMP_COUNTER,
1608};
1609
bf591b24
MR
1610static unsigned num_msrs_to_save;
1611
6f00e68f
AK
1612static u32 emulated_msrs[] = {
1613 MSR_IA32_MISC_ENABLE,
1614};
1615
bf591b24
MR
1616static __init void kvm_init_msr_list(void)
1617{
1618 u32 dummy[2];
1619 unsigned i, j;
1620
1621 for (i = j = 0; i < ARRAY_SIZE(msrs_to_save); i++) {
1622 if (rdmsr_safe(msrs_to_save[i], &dummy[0], &dummy[1]) < 0)
1623 continue;
1624 if (j < i)
1625 msrs_to_save[j] = msrs_to_save[i];
1626 j++;
1627 }
1628 num_msrs_to_save = j;
1629}
6aa8b732
AK
1630
1631/*
1632 * Adapt set_msr() to msr_io()'s calling convention
1633 */
1634static int do_set_msr(struct kvm_vcpu *vcpu, unsigned index, u64 *data)
1635{
1636 return set_msr(vcpu, index, *data);
1637}
1638
1639/*
1640 * Read or write a bunch of msrs. All parameters are kernel addresses.
1641 *
1642 * @return number of msrs set successfully.
1643 */
1644static int __msr_io(struct kvm *kvm, struct kvm_msrs *msrs,
1645 struct kvm_msr_entry *entries,
1646 int (*do_msr)(struct kvm_vcpu *vcpu,
1647 unsigned index, u64 *data))
1648{
1649 struct kvm_vcpu *vcpu;
1650 int i;
1651
5aacf0ca 1652 if (!valid_vcpu(msrs->vcpu))
6aa8b732
AK
1653 return -EINVAL;
1654
1655 vcpu = vcpu_load(kvm, msrs->vcpu);
1656 if (!vcpu)
1657 return -ENOENT;
1658
1659 for (i = 0; i < msrs->nmsrs; ++i)
1660 if (do_msr(vcpu, entries[i].index, &entries[i].data))
1661 break;
1662
1663 vcpu_put(vcpu);
1664
1665 return i;
1666}
1667
1668/*
1669 * Read or write a bunch of msrs. Parameters are user addresses.
1670 *
1671 * @return number of msrs set successfully.
1672 */
1673static int msr_io(struct kvm *kvm, struct kvm_msrs __user *user_msrs,
1674 int (*do_msr)(struct kvm_vcpu *vcpu,
1675 unsigned index, u64 *data),
1676 int writeback)
1677{
1678 struct kvm_msrs msrs;
1679 struct kvm_msr_entry *entries;
1680 int r, n;
1681 unsigned size;
1682
1683 r = -EFAULT;
1684 if (copy_from_user(&msrs, user_msrs, sizeof msrs))
1685 goto out;
1686
1687 r = -E2BIG;
1688 if (msrs.nmsrs >= MAX_IO_MSRS)
1689 goto out;
1690
1691 r = -ENOMEM;
1692 size = sizeof(struct kvm_msr_entry) * msrs.nmsrs;
1693 entries = vmalloc(size);
1694 if (!entries)
1695 goto out;
1696
1697 r = -EFAULT;
1698 if (copy_from_user(entries, user_msrs->entries, size))
1699 goto out_free;
1700
1701 r = n = __msr_io(kvm, &msrs, entries, do_msr);
1702 if (r < 0)
1703 goto out_free;
1704
1705 r = -EFAULT;
1706 if (writeback && copy_to_user(user_msrs->entries, entries, size))
1707 goto out_free;
1708
1709 r = n;
1710
1711out_free:
1712 vfree(entries);
1713out:
1714 return r;
1715}
1716
1717/*
1718 * Translate a guest virtual address to a guest physical address.
1719 */
1720static int kvm_dev_ioctl_translate(struct kvm *kvm, struct kvm_translation *tr)
1721{
1722 unsigned long vaddr = tr->linear_address;
1723 struct kvm_vcpu *vcpu;
1724 gpa_t gpa;
1725
1726 vcpu = vcpu_load(kvm, tr->vcpu);
1727 if (!vcpu)
1728 return -ENOENT;
1729 spin_lock(&kvm->lock);
1730 gpa = vcpu->mmu.gva_to_gpa(vcpu, vaddr);
1731 tr->physical_address = gpa;
1732 tr->valid = gpa != UNMAPPED_GVA;
1733 tr->writeable = 1;
1734 tr->usermode = 0;
1735 spin_unlock(&kvm->lock);
1736 vcpu_put(vcpu);
1737
1738 return 0;
1739}
1740
1741static int kvm_dev_ioctl_interrupt(struct kvm *kvm, struct kvm_interrupt *irq)
1742{
1743 struct kvm_vcpu *vcpu;
1744
5aacf0ca 1745 if (!valid_vcpu(irq->vcpu))
6aa8b732
AK
1746 return -EINVAL;
1747 if (irq->irq < 0 || irq->irq >= 256)
1748 return -EINVAL;
1749 vcpu = vcpu_load(kvm, irq->vcpu);
1750 if (!vcpu)
1751 return -ENOENT;
1752
1753 set_bit(irq->irq, vcpu->irq_pending);
1754 set_bit(irq->irq / BITS_PER_LONG, &vcpu->irq_summary);
1755
1756 vcpu_put(vcpu);
1757
1758 return 0;
1759}
1760
1761static int kvm_dev_ioctl_debug_guest(struct kvm *kvm,
1762 struct kvm_debug_guest *dbg)
1763{
1764 struct kvm_vcpu *vcpu;
1765 int r;
1766
5aacf0ca 1767 if (!valid_vcpu(dbg->vcpu))
6aa8b732
AK
1768 return -EINVAL;
1769 vcpu = vcpu_load(kvm, dbg->vcpu);
1770 if (!vcpu)
1771 return -ENOENT;
1772
1773 r = kvm_arch_ops->set_guest_debug(vcpu, dbg);
1774
1775 vcpu_put(vcpu);
1776
1777 return r;
1778}
1779
1780static long kvm_dev_ioctl(struct file *filp,
1781 unsigned int ioctl, unsigned long arg)
1782{
1783 struct kvm *kvm = filp->private_data;
2f366987 1784 void __user *argp = (void __user *)arg;
6aa8b732
AK
1785 int r = -EINVAL;
1786
1787 switch (ioctl) {
0b76e20b
AK
1788 case KVM_GET_API_VERSION:
1789 r = KVM_API_VERSION;
1790 break;
6aa8b732
AK
1791 case KVM_CREATE_VCPU: {
1792 r = kvm_dev_ioctl_create_vcpu(kvm, arg);
1793 if (r)
1794 goto out;
1795 break;
1796 }
1797 case KVM_RUN: {
1798 struct kvm_run kvm_run;
1799
1800 r = -EFAULT;
2f366987 1801 if (copy_from_user(&kvm_run, argp, sizeof kvm_run))
6aa8b732
AK
1802 goto out;
1803 r = kvm_dev_ioctl_run(kvm, &kvm_run);
c1150d8c 1804 if (r < 0 && r != -EINTR)
6aa8b732 1805 goto out;
2f366987 1806 if (copy_to_user(argp, &kvm_run, sizeof kvm_run)) {
c1150d8c 1807 r = -EFAULT;
6aa8b732 1808 goto out;
c1150d8c 1809 }
6aa8b732
AK
1810 break;
1811 }
1812 case KVM_GET_REGS: {
1813 struct kvm_regs kvm_regs;
1814
1815 r = -EFAULT;
2f366987 1816 if (copy_from_user(&kvm_regs, argp, sizeof kvm_regs))
6aa8b732
AK
1817 goto out;
1818 r = kvm_dev_ioctl_get_regs(kvm, &kvm_regs);
1819 if (r)
1820 goto out;
1821 r = -EFAULT;
2f366987 1822 if (copy_to_user(argp, &kvm_regs, sizeof kvm_regs))
6aa8b732
AK
1823 goto out;
1824 r = 0;
1825 break;
1826 }
1827 case KVM_SET_REGS: {
1828 struct kvm_regs kvm_regs;
1829
1830 r = -EFAULT;
2f366987 1831 if (copy_from_user(&kvm_regs, argp, sizeof kvm_regs))
6aa8b732
AK
1832 goto out;
1833 r = kvm_dev_ioctl_set_regs(kvm, &kvm_regs);
1834 if (r)
1835 goto out;
1836 r = 0;
1837 break;
1838 }
1839 case KVM_GET_SREGS: {
1840 struct kvm_sregs kvm_sregs;
1841
1842 r = -EFAULT;
2f366987 1843 if (copy_from_user(&kvm_sregs, argp, sizeof kvm_sregs))
6aa8b732
AK
1844 goto out;
1845 r = kvm_dev_ioctl_get_sregs(kvm, &kvm_sregs);
1846 if (r)
1847 goto out;
1848 r = -EFAULT;
2f366987 1849 if (copy_to_user(argp, &kvm_sregs, sizeof kvm_sregs))
6aa8b732
AK
1850 goto out;
1851 r = 0;
1852 break;
1853 }
1854 case KVM_SET_SREGS: {
1855 struct kvm_sregs kvm_sregs;
1856
1857 r = -EFAULT;
2f366987 1858 if (copy_from_user(&kvm_sregs, argp, sizeof kvm_sregs))
6aa8b732
AK
1859 goto out;
1860 r = kvm_dev_ioctl_set_sregs(kvm, &kvm_sregs);
1861 if (r)
1862 goto out;
1863 r = 0;
1864 break;
1865 }
1866 case KVM_TRANSLATE: {
1867 struct kvm_translation tr;
1868
1869 r = -EFAULT;
2f366987 1870 if (copy_from_user(&tr, argp, sizeof tr))
6aa8b732
AK
1871 goto out;
1872 r = kvm_dev_ioctl_translate(kvm, &tr);
1873 if (r)
1874 goto out;
1875 r = -EFAULT;
2f366987 1876 if (copy_to_user(argp, &tr, sizeof tr))
6aa8b732
AK
1877 goto out;
1878 r = 0;
1879 break;
1880 }
1881 case KVM_INTERRUPT: {
1882 struct kvm_interrupt irq;
1883
1884 r = -EFAULT;
2f366987 1885 if (copy_from_user(&irq, argp, sizeof irq))
6aa8b732
AK
1886 goto out;
1887 r = kvm_dev_ioctl_interrupt(kvm, &irq);
1888 if (r)
1889 goto out;
1890 r = 0;
1891 break;
1892 }
1893 case KVM_DEBUG_GUEST: {
1894 struct kvm_debug_guest dbg;
1895
1896 r = -EFAULT;
2f366987 1897 if (copy_from_user(&dbg, argp, sizeof dbg))
6aa8b732
AK
1898 goto out;
1899 r = kvm_dev_ioctl_debug_guest(kvm, &dbg);
1900 if (r)
1901 goto out;
1902 r = 0;
1903 break;
1904 }
1905 case KVM_SET_MEMORY_REGION: {
1906 struct kvm_memory_region kvm_mem;
1907
1908 r = -EFAULT;
2f366987 1909 if (copy_from_user(&kvm_mem, argp, sizeof kvm_mem))
6aa8b732
AK
1910 goto out;
1911 r = kvm_dev_ioctl_set_memory_region(kvm, &kvm_mem);
1912 if (r)
1913 goto out;
1914 break;
1915 }
1916 case KVM_GET_DIRTY_LOG: {
1917 struct kvm_dirty_log log;
1918
1919 r = -EFAULT;
2f366987 1920 if (copy_from_user(&log, argp, sizeof log))
6aa8b732
AK
1921 goto out;
1922 r = kvm_dev_ioctl_get_dirty_log(kvm, &log);
1923 if (r)
1924 goto out;
1925 break;
1926 }
1927 case KVM_GET_MSRS:
2f366987 1928 r = msr_io(kvm, argp, get_msr, 1);
6aa8b732
AK
1929 break;
1930 case KVM_SET_MSRS:
2f366987 1931 r = msr_io(kvm, argp, do_set_msr, 0);
6aa8b732
AK
1932 break;
1933 case KVM_GET_MSR_INDEX_LIST: {
2f366987 1934 struct kvm_msr_list __user *user_msr_list = argp;
6aa8b732
AK
1935 struct kvm_msr_list msr_list;
1936 unsigned n;
1937
1938 r = -EFAULT;
1939 if (copy_from_user(&msr_list, user_msr_list, sizeof msr_list))
1940 goto out;
1941 n = msr_list.nmsrs;
6f00e68f 1942 msr_list.nmsrs = num_msrs_to_save + ARRAY_SIZE(emulated_msrs);
6aa8b732
AK
1943 if (copy_to_user(user_msr_list, &msr_list, sizeof msr_list))
1944 goto out;
1945 r = -E2BIG;
bf591b24 1946 if (n < num_msrs_to_save)
6aa8b732
AK
1947 goto out;
1948 r = -EFAULT;
1949 if (copy_to_user(user_msr_list->indices, &msrs_to_save,
bf591b24 1950 num_msrs_to_save * sizeof(u32)))
6aa8b732 1951 goto out;
6f00e68f
AK
1952 if (copy_to_user(user_msr_list->indices
1953 + num_msrs_to_save * sizeof(u32),
1954 &emulated_msrs,
1955 ARRAY_SIZE(emulated_msrs) * sizeof(u32)))
1956 goto out;
6aa8b732 1957 r = 0;
cc1d8955 1958 break;
6aa8b732
AK
1959 }
1960 default:
1961 ;
1962 }
1963out:
1964 return r;
1965}
1966
1967static struct page *kvm_dev_nopage(struct vm_area_struct *vma,
1968 unsigned long address,
1969 int *type)
1970{
1971 struct kvm *kvm = vma->vm_file->private_data;
1972 unsigned long pgoff;
1973 struct kvm_memory_slot *slot;
1974 struct page *page;
1975
1976 *type = VM_FAULT_MINOR;
1977 pgoff = ((address - vma->vm_start) >> PAGE_SHIFT) + vma->vm_pgoff;
1978 slot = gfn_to_memslot(kvm, pgoff);
1979 if (!slot)
1980 return NOPAGE_SIGBUS;
1981 page = gfn_to_page(slot, pgoff);
1982 if (!page)
1983 return NOPAGE_SIGBUS;
1984 get_page(page);
1985 return page;
1986}
1987
1988static struct vm_operations_struct kvm_dev_vm_ops = {
1989 .nopage = kvm_dev_nopage,
1990};
1991
1992static int kvm_dev_mmap(struct file *file, struct vm_area_struct *vma)
1993{
1994 vma->vm_ops = &kvm_dev_vm_ops;
1995 return 0;
1996}
1997
1998static struct file_operations kvm_chardev_ops = {
1999 .open = kvm_dev_open,
2000 .release = kvm_dev_release,
2001 .unlocked_ioctl = kvm_dev_ioctl,
2002 .compat_ioctl = kvm_dev_ioctl,
2003 .mmap = kvm_dev_mmap,
2004};
2005
2006static struct miscdevice kvm_dev = {
2007 MISC_DYNAMIC_MINOR,
2008 "kvm",
2009 &kvm_chardev_ops,
2010};
2011
2012static int kvm_reboot(struct notifier_block *notifier, unsigned long val,
2013 void *v)
2014{
2015 if (val == SYS_RESTART) {
2016 /*
2017 * Some (well, at least mine) BIOSes hang on reboot if
2018 * in vmx root mode.
2019 */
2020 printk(KERN_INFO "kvm: exiting hardware virtualization\n");
8b6d44c7 2021 on_each_cpu(kvm_arch_ops->hardware_disable, NULL, 0, 1);
6aa8b732
AK
2022 }
2023 return NOTIFY_OK;
2024}
2025
2026static struct notifier_block kvm_reboot_notifier = {
2027 .notifier_call = kvm_reboot,
2028 .priority = 0,
2029};
2030
2031static __init void kvm_init_debug(void)
2032{
2033 struct kvm_stats_debugfs_item *p;
2034
8b6d44c7 2035 debugfs_dir = debugfs_create_dir("kvm", NULL);
6aa8b732
AK
2036 for (p = debugfs_entries; p->name; ++p)
2037 p->dentry = debugfs_create_u32(p->name, 0444, debugfs_dir,
2038 p->data);
2039}
2040
2041static void kvm_exit_debug(void)
2042{
2043 struct kvm_stats_debugfs_item *p;
2044
2045 for (p = debugfs_entries; p->name; ++p)
2046 debugfs_remove(p->dentry);
2047 debugfs_remove(debugfs_dir);
2048}
2049
2050hpa_t bad_page_address;
2051
2052int kvm_init_arch(struct kvm_arch_ops *ops, struct module *module)
2053{
2054 int r;
2055
09db28b8
YI
2056 if (kvm_arch_ops) {
2057 printk(KERN_ERR "kvm: already loaded the other module\n");
2058 return -EEXIST;
2059 }
2060
e097f35c 2061 if (!ops->cpu_has_kvm_support()) {
6aa8b732
AK
2062 printk(KERN_ERR "kvm: no hardware support\n");
2063 return -EOPNOTSUPP;
2064 }
e097f35c 2065 if (ops->disabled_by_bios()) {
6aa8b732
AK
2066 printk(KERN_ERR "kvm: disabled by bios\n");
2067 return -EOPNOTSUPP;
2068 }
2069
e097f35c
YI
2070 kvm_arch_ops = ops;
2071
6aa8b732
AK
2072 r = kvm_arch_ops->hardware_setup();
2073 if (r < 0)
2074 return r;
2075
8b6d44c7 2076 on_each_cpu(kvm_arch_ops->hardware_enable, NULL, 0, 1);
6aa8b732
AK
2077 register_reboot_notifier(&kvm_reboot_notifier);
2078
2079 kvm_chardev_ops.owner = module;
2080
2081 r = misc_register(&kvm_dev);
2082 if (r) {
2083 printk (KERN_ERR "kvm: misc device register failed\n");
2084 goto out_free;
2085 }
2086
2087 return r;
2088
2089out_free:
2090 unregister_reboot_notifier(&kvm_reboot_notifier);
8b6d44c7 2091 on_each_cpu(kvm_arch_ops->hardware_disable, NULL, 0, 1);
6aa8b732
AK
2092 kvm_arch_ops->hardware_unsetup();
2093 return r;
2094}
2095
2096void kvm_exit_arch(void)
2097{
2098 misc_deregister(&kvm_dev);
2099
2100 unregister_reboot_notifier(&kvm_reboot_notifier);
8b6d44c7 2101 on_each_cpu(kvm_arch_ops->hardware_disable, NULL, 0, 1);
6aa8b732 2102 kvm_arch_ops->hardware_unsetup();
09db28b8 2103 kvm_arch_ops = NULL;
6aa8b732
AK
2104}
2105
2106static __init int kvm_init(void)
2107{
2108 static struct page *bad_page;
2109 int r = 0;
2110
2111 kvm_init_debug();
2112
bf591b24
MR
2113 kvm_init_msr_list();
2114
6aa8b732
AK
2115 if ((bad_page = alloc_page(GFP_KERNEL)) == NULL) {
2116 r = -ENOMEM;
2117 goto out;
2118 }
2119
2120 bad_page_address = page_to_pfn(bad_page) << PAGE_SHIFT;
2121 memset(__va(bad_page_address), 0, PAGE_SIZE);
2122
2123 return r;
2124
2125out:
2126 kvm_exit_debug();
2127 return r;
2128}
2129
2130static __exit void kvm_exit(void)
2131{
2132 kvm_exit_debug();
2133 __free_page(pfn_to_page(bad_page_address >> PAGE_SHIFT));
2134}
2135
2136module_init(kvm_init)
2137module_exit(kvm_exit)
2138
2139EXPORT_SYMBOL_GPL(kvm_init_arch);
2140EXPORT_SYMBOL_GPL(kvm_exit_arch);