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