KVM: Do not communicate to userspace through cpu registers during PIO
[deliverable/linux.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>
e9cdb1e3 23#include <linux/magic.h>
6aa8b732
AK
24#include <asm/processor.h>
25#include <linux/percpu.h>
26#include <linux/gfp.h>
27#include <asm/msr.h>
28#include <linux/mm.h>
29#include <linux/miscdevice.h>
30#include <linux/vmalloc.h>
31#include <asm/uaccess.h>
32#include <linux/reboot.h>
33#include <asm/io.h>
34#include <linux/debugfs.h>
35#include <linux/highmem.h>
36#include <linux/file.h>
37#include <asm/desc.h>
59ae6c6b 38#include <linux/sysdev.h>
774c47f1 39#include <linux/cpu.h>
f17abe9a 40#include <linux/file.h>
37e29d90
AK
41#include <linux/fs.h>
42#include <linux/mount.h>
6aa8b732
AK
43
44#include "x86_emulate.h"
45#include "segment_descriptor.h"
46
47MODULE_AUTHOR("Qumranet");
48MODULE_LICENSE("GPL");
49
133de902
AK
50static DEFINE_SPINLOCK(kvm_lock);
51static LIST_HEAD(vm_list);
52
6aa8b732
AK
53struct kvm_arch_ops *kvm_arch_ops;
54struct kvm_stat kvm_stat;
55EXPORT_SYMBOL_GPL(kvm_stat);
56
57static struct kvm_stats_debugfs_item {
58 const char *name;
59 u32 *data;
60 struct dentry *dentry;
61} debugfs_entries[] = {
62 { "pf_fixed", &kvm_stat.pf_fixed },
63 { "pf_guest", &kvm_stat.pf_guest },
64 { "tlb_flush", &kvm_stat.tlb_flush },
65 { "invlpg", &kvm_stat.invlpg },
66 { "exits", &kvm_stat.exits },
67 { "io_exits", &kvm_stat.io_exits },
68 { "mmio_exits", &kvm_stat.mmio_exits },
69 { "signal_exits", &kvm_stat.signal_exits },
c1150d8c
DL
70 { "irq_window", &kvm_stat.irq_window_exits },
71 { "halt_exits", &kvm_stat.halt_exits },
72 { "request_irq", &kvm_stat.request_irq_exits },
6aa8b732 73 { "irq_exits", &kvm_stat.irq_exits },
8b6d44c7 74 { NULL, NULL }
6aa8b732
AK
75};
76
77static struct dentry *debugfs_dir;
78
37e29d90
AK
79struct vfsmount *kvmfs_mnt;
80
6aa8b732
AK
81#define MAX_IO_MSRS 256
82
83#define CR0_RESEVED_BITS 0xffffffff1ffaffc0ULL
84#define LMSW_GUEST_MASK 0x0eULL
85#define CR4_RESEVED_BITS (~((1ULL << 11) - 1))
86#define CR8_RESEVED_BITS (~0x0fULL)
87#define EFER_RESERVED_BITS 0xfffffffffffff2fe
88
05b3e0c2 89#ifdef CONFIG_X86_64
6aa8b732
AK
90// LDT or TSS descriptor in the GDT. 16 bytes.
91struct segment_descriptor_64 {
92 struct segment_descriptor s;
93 u32 base_higher;
94 u32 pad_zero;
95};
96
97#endif
98
bccf2150
AK
99static long kvm_vcpu_ioctl(struct file *file, unsigned int ioctl,
100 unsigned long arg);
101
f17abe9a
AK
102static struct inode *kvmfs_inode(struct file_operations *fops)
103{
104 int error = -ENOMEM;
105 struct inode *inode = new_inode(kvmfs_mnt->mnt_sb);
106
107 if (!inode)
108 goto eexit_1;
109
110 inode->i_fop = fops;
111
112 /*
113 * Mark the inode dirty from the very beginning,
114 * that way it will never be moved to the dirty
115 * list because mark_inode_dirty() will think
116 * that it already _is_ on the dirty list.
117 */
118 inode->i_state = I_DIRTY;
119 inode->i_mode = S_IRUSR | S_IWUSR;
120 inode->i_uid = current->fsuid;
121 inode->i_gid = current->fsgid;
122 inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
123 return inode;
124
125eexit_1:
126 return ERR_PTR(error);
127}
128
129static struct file *kvmfs_file(struct inode *inode, void *private_data)
130{
131 struct file *file = get_empty_filp();
132
133 if (!file)
134 return ERR_PTR(-ENFILE);
135
136 file->f_path.mnt = mntget(kvmfs_mnt);
137 file->f_path.dentry = d_alloc_anon(inode);
138 if (!file->f_path.dentry)
139 return ERR_PTR(-ENOMEM);
140 file->f_mapping = inode->i_mapping;
141
142 file->f_pos = 0;
143 file->f_flags = O_RDWR;
144 file->f_op = inode->i_fop;
145 file->f_mode = FMODE_READ | FMODE_WRITE;
146 file->f_version = 0;
147 file->private_data = private_data;
148 return file;
149}
150
6aa8b732
AK
151unsigned long segment_base(u16 selector)
152{
153 struct descriptor_table gdt;
154 struct segment_descriptor *d;
155 unsigned long table_base;
156 typedef unsigned long ul;
157 unsigned long v;
158
159 if (selector == 0)
160 return 0;
161
162 asm ("sgdt %0" : "=m"(gdt));
163 table_base = gdt.base;
164
165 if (selector & 4) { /* from ldt */
166 u16 ldt_selector;
167
168 asm ("sldt %0" : "=g"(ldt_selector));
169 table_base = segment_base(ldt_selector);
170 }
171 d = (struct segment_descriptor *)(table_base + (selector & ~7));
172 v = d->base_low | ((ul)d->base_mid << 16) | ((ul)d->base_high << 24);
05b3e0c2 173#ifdef CONFIG_X86_64
6aa8b732
AK
174 if (d->system == 0
175 && (d->type == 2 || d->type == 9 || d->type == 11))
176 v |= ((ul)((struct segment_descriptor_64 *)d)->base_higher) << 32;
177#endif
178 return v;
179}
180EXPORT_SYMBOL_GPL(segment_base);
181
5aacf0ca
JM
182static inline int valid_vcpu(int n)
183{
184 return likely(n >= 0 && n < KVM_MAX_VCPUS);
185}
186
d27d4aca
AK
187int kvm_read_guest(struct kvm_vcpu *vcpu, gva_t addr, unsigned long size,
188 void *dest)
6aa8b732
AK
189{
190 unsigned char *host_buf = dest;
191 unsigned long req_size = size;
192
193 while (size) {
194 hpa_t paddr;
195 unsigned now;
196 unsigned offset;
197 hva_t guest_buf;
198
199 paddr = gva_to_hpa(vcpu, addr);
200
201 if (is_error_hpa(paddr))
202 break;
203
204 guest_buf = (hva_t)kmap_atomic(
205 pfn_to_page(paddr >> PAGE_SHIFT),
206 KM_USER0);
207 offset = addr & ~PAGE_MASK;
208 guest_buf |= offset;
209 now = min(size, PAGE_SIZE - offset);
210 memcpy(host_buf, (void*)guest_buf, now);
211 host_buf += now;
212 addr += now;
213 size -= now;
214 kunmap_atomic((void *)(guest_buf & PAGE_MASK), KM_USER0);
215 }
216 return req_size - size;
217}
218EXPORT_SYMBOL_GPL(kvm_read_guest);
219
d27d4aca
AK
220int kvm_write_guest(struct kvm_vcpu *vcpu, gva_t addr, unsigned long size,
221 void *data)
6aa8b732
AK
222{
223 unsigned char *host_buf = data;
224 unsigned long req_size = size;
225
226 while (size) {
227 hpa_t paddr;
228 unsigned now;
229 unsigned offset;
230 hva_t guest_buf;
ab51a434 231 gfn_t gfn;
6aa8b732
AK
232
233 paddr = gva_to_hpa(vcpu, addr);
234
235 if (is_error_hpa(paddr))
236 break;
237
ab51a434
UL
238 gfn = vcpu->mmu.gva_to_gpa(vcpu, addr) >> PAGE_SHIFT;
239 mark_page_dirty(vcpu->kvm, gfn);
6aa8b732
AK
240 guest_buf = (hva_t)kmap_atomic(
241 pfn_to_page(paddr >> PAGE_SHIFT), KM_USER0);
242 offset = addr & ~PAGE_MASK;
243 guest_buf |= offset;
244 now = min(size, PAGE_SIZE - offset);
245 memcpy((void*)guest_buf, host_buf, now);
246 host_buf += now;
247 addr += now;
248 size -= now;
249 kunmap_atomic((void *)(guest_buf & PAGE_MASK), KM_USER0);
250 }
251 return req_size - size;
252}
253EXPORT_SYMBOL_GPL(kvm_write_guest);
254
bccf2150
AK
255/*
256 * Switches to specified vcpu, until a matching vcpu_put()
257 */
258static void vcpu_load(struct kvm_vcpu *vcpu)
6aa8b732 259{
bccf2150
AK
260 mutex_lock(&vcpu->mutex);
261 kvm_arch_ops->vcpu_load(vcpu);
6aa8b732
AK
262}
263
264/*
bccf2150
AK
265 * Switches to specified vcpu, until a matching vcpu_put(). Will return NULL
266 * if the slot is not populated.
6aa8b732 267 */
bccf2150 268static struct kvm_vcpu *vcpu_load_slot(struct kvm *kvm, int slot)
6aa8b732 269{
bccf2150 270 struct kvm_vcpu *vcpu = &kvm->vcpus[slot];
6aa8b732
AK
271
272 mutex_lock(&vcpu->mutex);
bccf2150 273 if (!vcpu->vmcs) {
6aa8b732 274 mutex_unlock(&vcpu->mutex);
8b6d44c7 275 return NULL;
6aa8b732 276 }
bccf2150
AK
277 kvm_arch_ops->vcpu_load(vcpu);
278 return vcpu;
6aa8b732
AK
279}
280
281static void vcpu_put(struct kvm_vcpu *vcpu)
282{
283 kvm_arch_ops->vcpu_put(vcpu);
6aa8b732
AK
284 mutex_unlock(&vcpu->mutex);
285}
286
f17abe9a 287static struct kvm *kvm_create_vm(void)
6aa8b732
AK
288{
289 struct kvm *kvm = kzalloc(sizeof(struct kvm), GFP_KERNEL);
290 int i;
291
292 if (!kvm)
f17abe9a 293 return ERR_PTR(-ENOMEM);
6aa8b732
AK
294
295 spin_lock_init(&kvm->lock);
296 INIT_LIST_HEAD(&kvm->active_mmu_pages);
297 for (i = 0; i < KVM_MAX_VCPUS; ++i) {
298 struct kvm_vcpu *vcpu = &kvm->vcpus[i];
299
300 mutex_init(&vcpu->mutex);
133de902 301 vcpu->cpu = -1;
86a2b42e 302 vcpu->kvm = kvm;
6aa8b732
AK
303 vcpu->mmu.root_hpa = INVALID_PAGE;
304 INIT_LIST_HEAD(&vcpu->free_pages);
133de902
AK
305 spin_lock(&kvm_lock);
306 list_add(&kvm->vm_list, &vm_list);
307 spin_unlock(&kvm_lock);
6aa8b732 308 }
f17abe9a
AK
309 return kvm;
310}
311
312static int kvm_dev_open(struct inode *inode, struct file *filp)
313{
6aa8b732
AK
314 return 0;
315}
316
317/*
318 * Free any memory in @free but not in @dont.
319 */
320static void kvm_free_physmem_slot(struct kvm_memory_slot *free,
321 struct kvm_memory_slot *dont)
322{
323 int i;
324
325 if (!dont || free->phys_mem != dont->phys_mem)
326 if (free->phys_mem) {
327 for (i = 0; i < free->npages; ++i)
55a54f79
AK
328 if (free->phys_mem[i])
329 __free_page(free->phys_mem[i]);
6aa8b732
AK
330 vfree(free->phys_mem);
331 }
332
333 if (!dont || free->dirty_bitmap != dont->dirty_bitmap)
334 vfree(free->dirty_bitmap);
335
8b6d44c7 336 free->phys_mem = NULL;
6aa8b732 337 free->npages = 0;
8b6d44c7 338 free->dirty_bitmap = NULL;
6aa8b732
AK
339}
340
341static void kvm_free_physmem(struct kvm *kvm)
342{
343 int i;
344
345 for (i = 0; i < kvm->nmemslots; ++i)
8b6d44c7 346 kvm_free_physmem_slot(&kvm->memslots[i], NULL);
6aa8b732
AK
347}
348
349static void kvm_free_vcpu(struct kvm_vcpu *vcpu)
350{
bccf2150 351 if (!vcpu->vmcs)
1e8ba6fb
IM
352 return;
353
bccf2150 354 vcpu_load(vcpu);
6aa8b732 355 kvm_mmu_destroy(vcpu);
08438475 356 vcpu_put(vcpu);
9ede74e0 357 kvm_arch_ops->vcpu_free(vcpu);
9a2bb7f4
AK
358 free_page((unsigned long)vcpu->run);
359 vcpu->run = NULL;
6aa8b732
AK
360}
361
362static void kvm_free_vcpus(struct kvm *kvm)
363{
364 unsigned int i;
365
366 for (i = 0; i < KVM_MAX_VCPUS; ++i)
367 kvm_free_vcpu(&kvm->vcpus[i]);
368}
369
370static int kvm_dev_release(struct inode *inode, struct file *filp)
371{
f17abe9a
AK
372 return 0;
373}
6aa8b732 374
f17abe9a
AK
375static void kvm_destroy_vm(struct kvm *kvm)
376{
133de902
AK
377 spin_lock(&kvm_lock);
378 list_del(&kvm->vm_list);
379 spin_unlock(&kvm_lock);
6aa8b732
AK
380 kvm_free_vcpus(kvm);
381 kvm_free_physmem(kvm);
382 kfree(kvm);
f17abe9a
AK
383}
384
385static int kvm_vm_release(struct inode *inode, struct file *filp)
386{
387 struct kvm *kvm = filp->private_data;
388
389 kvm_destroy_vm(kvm);
6aa8b732
AK
390 return 0;
391}
392
393static void inject_gp(struct kvm_vcpu *vcpu)
394{
395 kvm_arch_ops->inject_gp(vcpu, 0);
396}
397
1342d353
AK
398/*
399 * Load the pae pdptrs. Return true is they are all valid.
400 */
401static int load_pdptrs(struct kvm_vcpu *vcpu, unsigned long cr3)
6aa8b732
AK
402{
403 gfn_t pdpt_gfn = cr3 >> PAGE_SHIFT;
1342d353 404 unsigned offset = ((cr3 & (PAGE_SIZE-1)) >> 5) << 2;
6aa8b732
AK
405 int i;
406 u64 pdpte;
407 u64 *pdpt;
1342d353 408 int ret;
6aa8b732
AK
409 struct kvm_memory_slot *memslot;
410
411 spin_lock(&vcpu->kvm->lock);
412 memslot = gfn_to_memslot(vcpu->kvm, pdpt_gfn);
413 /* FIXME: !memslot - emulate? 0xff? */
414 pdpt = kmap_atomic(gfn_to_page(memslot, pdpt_gfn), KM_USER0);
415
1342d353 416 ret = 1;
6aa8b732
AK
417 for (i = 0; i < 4; ++i) {
418 pdpte = pdpt[offset + i];
1342d353
AK
419 if ((pdpte & 1) && (pdpte & 0xfffffff0000001e6ull)) {
420 ret = 0;
421 goto out;
422 }
6aa8b732
AK
423 }
424
1342d353
AK
425 for (i = 0; i < 4; ++i)
426 vcpu->pdptrs[i] = pdpt[offset + i];
427
428out:
6aa8b732
AK
429 kunmap_atomic(pdpt, KM_USER0);
430 spin_unlock(&vcpu->kvm->lock);
431
1342d353 432 return ret;
6aa8b732
AK
433}
434
435void set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0)
436{
437 if (cr0 & CR0_RESEVED_BITS) {
438 printk(KERN_DEBUG "set_cr0: 0x%lx #GP, reserved bits 0x%lx\n",
439 cr0, vcpu->cr0);
440 inject_gp(vcpu);
441 return;
442 }
443
444 if ((cr0 & CR0_NW_MASK) && !(cr0 & CR0_CD_MASK)) {
445 printk(KERN_DEBUG "set_cr0: #GP, CD == 0 && NW == 1\n");
446 inject_gp(vcpu);
447 return;
448 }
449
450 if ((cr0 & CR0_PG_MASK) && !(cr0 & CR0_PE_MASK)) {
451 printk(KERN_DEBUG "set_cr0: #GP, set PG flag "
452 "and a clear PE flag\n");
453 inject_gp(vcpu);
454 return;
455 }
456
457 if (!is_paging(vcpu) && (cr0 & CR0_PG_MASK)) {
05b3e0c2 458#ifdef CONFIG_X86_64
6aa8b732
AK
459 if ((vcpu->shadow_efer & EFER_LME)) {
460 int cs_db, cs_l;
461
462 if (!is_pae(vcpu)) {
463 printk(KERN_DEBUG "set_cr0: #GP, start paging "
464 "in long mode while PAE is disabled\n");
465 inject_gp(vcpu);
466 return;
467 }
468 kvm_arch_ops->get_cs_db_l_bits(vcpu, &cs_db, &cs_l);
469 if (cs_l) {
470 printk(KERN_DEBUG "set_cr0: #GP, start paging "
471 "in long mode while CS.L == 1\n");
472 inject_gp(vcpu);
473 return;
474
475 }
476 } else
477#endif
1342d353 478 if (is_pae(vcpu) && !load_pdptrs(vcpu, vcpu->cr3)) {
6aa8b732
AK
479 printk(KERN_DEBUG "set_cr0: #GP, pdptrs "
480 "reserved bits\n");
481 inject_gp(vcpu);
482 return;
483 }
484
485 }
486
487 kvm_arch_ops->set_cr0(vcpu, cr0);
488 vcpu->cr0 = cr0;
489
490 spin_lock(&vcpu->kvm->lock);
491 kvm_mmu_reset_context(vcpu);
492 spin_unlock(&vcpu->kvm->lock);
493 return;
494}
495EXPORT_SYMBOL_GPL(set_cr0);
496
497void lmsw(struct kvm_vcpu *vcpu, unsigned long msw)
498{
399badf3 499 kvm_arch_ops->decache_cr0_cr4_guest_bits(vcpu);
6aa8b732
AK
500 set_cr0(vcpu, (vcpu->cr0 & ~0x0ful) | (msw & 0x0f));
501}
502EXPORT_SYMBOL_GPL(lmsw);
503
504void set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)
505{
506 if (cr4 & CR4_RESEVED_BITS) {
507 printk(KERN_DEBUG "set_cr4: #GP, reserved bits\n");
508 inject_gp(vcpu);
509 return;
510 }
511
a9058ecd 512 if (is_long_mode(vcpu)) {
6aa8b732
AK
513 if (!(cr4 & CR4_PAE_MASK)) {
514 printk(KERN_DEBUG "set_cr4: #GP, clearing PAE while "
515 "in long mode\n");
516 inject_gp(vcpu);
517 return;
518 }
519 } else if (is_paging(vcpu) && !is_pae(vcpu) && (cr4 & CR4_PAE_MASK)
1342d353 520 && !load_pdptrs(vcpu, vcpu->cr3)) {
6aa8b732
AK
521 printk(KERN_DEBUG "set_cr4: #GP, pdptrs reserved bits\n");
522 inject_gp(vcpu);
523 }
524
525 if (cr4 & CR4_VMXE_MASK) {
526 printk(KERN_DEBUG "set_cr4: #GP, setting VMXE\n");
527 inject_gp(vcpu);
528 return;
529 }
530 kvm_arch_ops->set_cr4(vcpu, cr4);
531 spin_lock(&vcpu->kvm->lock);
532 kvm_mmu_reset_context(vcpu);
533 spin_unlock(&vcpu->kvm->lock);
534}
535EXPORT_SYMBOL_GPL(set_cr4);
536
537void set_cr3(struct kvm_vcpu *vcpu, unsigned long cr3)
538{
a9058ecd 539 if (is_long_mode(vcpu)) {
d27d4aca 540 if (cr3 & CR3_L_MODE_RESEVED_BITS) {
6aa8b732
AK
541 printk(KERN_DEBUG "set_cr3: #GP, reserved bits\n");
542 inject_gp(vcpu);
543 return;
544 }
545 } else {
546 if (cr3 & CR3_RESEVED_BITS) {
547 printk(KERN_DEBUG "set_cr3: #GP, reserved bits\n");
548 inject_gp(vcpu);
549 return;
550 }
551 if (is_paging(vcpu) && is_pae(vcpu) &&
1342d353 552 !load_pdptrs(vcpu, cr3)) {
6aa8b732
AK
553 printk(KERN_DEBUG "set_cr3: #GP, pdptrs "
554 "reserved bits\n");
555 inject_gp(vcpu);
556 return;
557 }
558 }
559
560 vcpu->cr3 = cr3;
561 spin_lock(&vcpu->kvm->lock);
d21225ee
IM
562 /*
563 * Does the new cr3 value map to physical memory? (Note, we
564 * catch an invalid cr3 even in real-mode, because it would
565 * cause trouble later on when we turn on paging anyway.)
566 *
567 * A real CPU would silently accept an invalid cr3 and would
568 * attempt to use it - with largely undefined (and often hard
569 * to debug) behavior on the guest side.
570 */
571 if (unlikely(!gfn_to_memslot(vcpu->kvm, cr3 >> PAGE_SHIFT)))
572 inject_gp(vcpu);
573 else
574 vcpu->mmu.new_cr3(vcpu);
6aa8b732
AK
575 spin_unlock(&vcpu->kvm->lock);
576}
577EXPORT_SYMBOL_GPL(set_cr3);
578
579void set_cr8(struct kvm_vcpu *vcpu, unsigned long cr8)
580{
581 if ( cr8 & CR8_RESEVED_BITS) {
582 printk(KERN_DEBUG "set_cr8: #GP, reserved bits 0x%lx\n", cr8);
583 inject_gp(vcpu);
584 return;
585 }
586 vcpu->cr8 = cr8;
587}
588EXPORT_SYMBOL_GPL(set_cr8);
589
590void fx_init(struct kvm_vcpu *vcpu)
591{
592 struct __attribute__ ((__packed__)) fx_image_s {
593 u16 control; //fcw
594 u16 status; //fsw
595 u16 tag; // ftw
596 u16 opcode; //fop
597 u64 ip; // fpu ip
598 u64 operand;// fpu dp
599 u32 mxcsr;
600 u32 mxcsr_mask;
601
602 } *fx_image;
603
604 fx_save(vcpu->host_fx_image);
605 fpu_init();
606 fx_save(vcpu->guest_fx_image);
607 fx_restore(vcpu->host_fx_image);
608
609 fx_image = (struct fx_image_s *)vcpu->guest_fx_image;
610 fx_image->mxcsr = 0x1f80;
611 memset(vcpu->guest_fx_image + sizeof(struct fx_image_s),
612 0, FX_IMAGE_SIZE - sizeof(struct fx_image_s));
613}
614EXPORT_SYMBOL_GPL(fx_init);
615
02b27c1f
UL
616static void do_remove_write_access(struct kvm_vcpu *vcpu, int slot)
617{
618 spin_lock(&vcpu->kvm->lock);
619 kvm_mmu_slot_remove_write_access(vcpu, slot);
620 spin_unlock(&vcpu->kvm->lock);
621}
622
6aa8b732
AK
623/*
624 * Allocate some memory and give it an address in the guest physical address
625 * space.
626 *
627 * Discontiguous memory is allowed, mostly for framebuffers.
628 */
2c6f5df9
AK
629static int kvm_vm_ioctl_set_memory_region(struct kvm *kvm,
630 struct kvm_memory_region *mem)
6aa8b732
AK
631{
632 int r;
633 gfn_t base_gfn;
634 unsigned long npages;
635 unsigned long i;
636 struct kvm_memory_slot *memslot;
637 struct kvm_memory_slot old, new;
638 int memory_config_version;
639
640 r = -EINVAL;
641 /* General sanity checks */
642 if (mem->memory_size & (PAGE_SIZE - 1))
643 goto out;
644 if (mem->guest_phys_addr & (PAGE_SIZE - 1))
645 goto out;
646 if (mem->slot >= KVM_MEMORY_SLOTS)
647 goto out;
648 if (mem->guest_phys_addr + mem->memory_size < mem->guest_phys_addr)
649 goto out;
650
651 memslot = &kvm->memslots[mem->slot];
652 base_gfn = mem->guest_phys_addr >> PAGE_SHIFT;
653 npages = mem->memory_size >> PAGE_SHIFT;
654
655 if (!npages)
656 mem->flags &= ~KVM_MEM_LOG_DIRTY_PAGES;
657
658raced:
659 spin_lock(&kvm->lock);
660
661 memory_config_version = kvm->memory_config_version;
662 new = old = *memslot;
663
664 new.base_gfn = base_gfn;
665 new.npages = npages;
666 new.flags = mem->flags;
667
668 /* Disallow changing a memory slot's size. */
669 r = -EINVAL;
670 if (npages && old.npages && npages != old.npages)
671 goto out_unlock;
672
673 /* Check for overlaps */
674 r = -EEXIST;
675 for (i = 0; i < KVM_MEMORY_SLOTS; ++i) {
676 struct kvm_memory_slot *s = &kvm->memslots[i];
677
678 if (s == memslot)
679 continue;
680 if (!((base_gfn + npages <= s->base_gfn) ||
681 (base_gfn >= s->base_gfn + s->npages)))
682 goto out_unlock;
683 }
684 /*
685 * Do memory allocations outside lock. memory_config_version will
686 * detect any races.
687 */
688 spin_unlock(&kvm->lock);
689
690 /* Deallocate if slot is being removed */
691 if (!npages)
8b6d44c7 692 new.phys_mem = NULL;
6aa8b732
AK
693
694 /* Free page dirty bitmap if unneeded */
695 if (!(new.flags & KVM_MEM_LOG_DIRTY_PAGES))
8b6d44c7 696 new.dirty_bitmap = NULL;
6aa8b732
AK
697
698 r = -ENOMEM;
699
700 /* Allocate if a slot is being created */
701 if (npages && !new.phys_mem) {
702 new.phys_mem = vmalloc(npages * sizeof(struct page *));
703
704 if (!new.phys_mem)
705 goto out_free;
706
707 memset(new.phys_mem, 0, npages * sizeof(struct page *));
708 for (i = 0; i < npages; ++i) {
709 new.phys_mem[i] = alloc_page(GFP_HIGHUSER
710 | __GFP_ZERO);
711 if (!new.phys_mem[i])
712 goto out_free;
5972e953 713 set_page_private(new.phys_mem[i],0);
6aa8b732
AK
714 }
715 }
716
717 /* Allocate page dirty bitmap if needed */
718 if ((new.flags & KVM_MEM_LOG_DIRTY_PAGES) && !new.dirty_bitmap) {
719 unsigned dirty_bytes = ALIGN(npages, BITS_PER_LONG) / 8;
720
721 new.dirty_bitmap = vmalloc(dirty_bytes);
722 if (!new.dirty_bitmap)
723 goto out_free;
724 memset(new.dirty_bitmap, 0, dirty_bytes);
725 }
726
727 spin_lock(&kvm->lock);
728
729 if (memory_config_version != kvm->memory_config_version) {
730 spin_unlock(&kvm->lock);
731 kvm_free_physmem_slot(&new, &old);
732 goto raced;
733 }
734
735 r = -EAGAIN;
736 if (kvm->busy)
737 goto out_unlock;
738
739 if (mem->slot >= kvm->nmemslots)
740 kvm->nmemslots = mem->slot + 1;
741
742 *memslot = new;
743 ++kvm->memory_config_version;
744
745 spin_unlock(&kvm->lock);
746
747 for (i = 0; i < KVM_MAX_VCPUS; ++i) {
748 struct kvm_vcpu *vcpu;
749
bccf2150 750 vcpu = vcpu_load_slot(kvm, i);
6aa8b732
AK
751 if (!vcpu)
752 continue;
ff990d59
UL
753 if (new.flags & KVM_MEM_LOG_DIRTY_PAGES)
754 do_remove_write_access(vcpu, mem->slot);
6aa8b732
AK
755 kvm_mmu_reset_context(vcpu);
756 vcpu_put(vcpu);
757 }
758
759 kvm_free_physmem_slot(&old, &new);
760 return 0;
761
762out_unlock:
763 spin_unlock(&kvm->lock);
764out_free:
765 kvm_free_physmem_slot(&new, &old);
766out:
767 return r;
768}
769
770/*
771 * Get (and clear) the dirty memory log for a memory slot.
772 */
2c6f5df9
AK
773static int kvm_vm_ioctl_get_dirty_log(struct kvm *kvm,
774 struct kvm_dirty_log *log)
6aa8b732
AK
775{
776 struct kvm_memory_slot *memslot;
777 int r, i;
778 int n;
714b93da 779 int cleared;
6aa8b732
AK
780 unsigned long any = 0;
781
782 spin_lock(&kvm->lock);
783
784 /*
785 * Prevent changes to guest memory configuration even while the lock
786 * is not taken.
787 */
788 ++kvm->busy;
789 spin_unlock(&kvm->lock);
790 r = -EINVAL;
791 if (log->slot >= KVM_MEMORY_SLOTS)
792 goto out;
793
794 memslot = &kvm->memslots[log->slot];
795 r = -ENOENT;
796 if (!memslot->dirty_bitmap)
797 goto out;
798
cd1a4a98 799 n = ALIGN(memslot->npages, BITS_PER_LONG) / 8;
6aa8b732 800
cd1a4a98 801 for (i = 0; !any && i < n/sizeof(long); ++i)
6aa8b732
AK
802 any = memslot->dirty_bitmap[i];
803
804 r = -EFAULT;
805 if (copy_to_user(log->dirty_bitmap, memslot->dirty_bitmap, n))
806 goto out;
807
6aa8b732 808 if (any) {
714b93da 809 cleared = 0;
6aa8b732 810 for (i = 0; i < KVM_MAX_VCPUS; ++i) {
bccf2150 811 struct kvm_vcpu *vcpu;
6aa8b732 812
bccf2150 813 vcpu = vcpu_load_slot(kvm, i);
6aa8b732
AK
814 if (!vcpu)
815 continue;
714b93da
AK
816 if (!cleared) {
817 do_remove_write_access(vcpu, log->slot);
818 memset(memslot->dirty_bitmap, 0, n);
819 cleared = 1;
820 }
6aa8b732
AK
821 kvm_arch_ops->tlb_flush(vcpu);
822 vcpu_put(vcpu);
823 }
824 }
825
826 r = 0;
827
828out:
829 spin_lock(&kvm->lock);
830 --kvm->busy;
831 spin_unlock(&kvm->lock);
832 return r;
833}
834
835struct kvm_memory_slot *gfn_to_memslot(struct kvm *kvm, gfn_t gfn)
836{
837 int i;
838
839 for (i = 0; i < kvm->nmemslots; ++i) {
840 struct kvm_memory_slot *memslot = &kvm->memslots[i];
841
842 if (gfn >= memslot->base_gfn
843 && gfn < memslot->base_gfn + memslot->npages)
844 return memslot;
845 }
8b6d44c7 846 return NULL;
6aa8b732
AK
847}
848EXPORT_SYMBOL_GPL(gfn_to_memslot);
849
850void mark_page_dirty(struct kvm *kvm, gfn_t gfn)
851{
852 int i;
8b6d44c7 853 struct kvm_memory_slot *memslot = NULL;
6aa8b732
AK
854 unsigned long rel_gfn;
855
856 for (i = 0; i < kvm->nmemslots; ++i) {
857 memslot = &kvm->memslots[i];
858
859 if (gfn >= memslot->base_gfn
860 && gfn < memslot->base_gfn + memslot->npages) {
861
862 if (!memslot || !memslot->dirty_bitmap)
863 return;
864
865 rel_gfn = gfn - memslot->base_gfn;
866
867 /* avoid RMW */
868 if (!test_bit(rel_gfn, memslot->dirty_bitmap))
869 set_bit(rel_gfn, memslot->dirty_bitmap);
870 return;
871 }
872 }
873}
874
875static int emulator_read_std(unsigned long addr,
876 unsigned long *val,
877 unsigned int bytes,
878 struct x86_emulate_ctxt *ctxt)
879{
880 struct kvm_vcpu *vcpu = ctxt->vcpu;
881 void *data = val;
882
883 while (bytes) {
884 gpa_t gpa = vcpu->mmu.gva_to_gpa(vcpu, addr);
885 unsigned offset = addr & (PAGE_SIZE-1);
886 unsigned tocopy = min(bytes, (unsigned)PAGE_SIZE - offset);
887 unsigned long pfn;
888 struct kvm_memory_slot *memslot;
889 void *page;
890
891 if (gpa == UNMAPPED_GVA)
892 return X86EMUL_PROPAGATE_FAULT;
893 pfn = gpa >> PAGE_SHIFT;
894 memslot = gfn_to_memslot(vcpu->kvm, pfn);
895 if (!memslot)
896 return X86EMUL_UNHANDLEABLE;
897 page = kmap_atomic(gfn_to_page(memslot, pfn), KM_USER0);
898
899 memcpy(data, page + offset, tocopy);
900
901 kunmap_atomic(page, KM_USER0);
902
903 bytes -= tocopy;
904 data += tocopy;
905 addr += tocopy;
906 }
907
908 return X86EMUL_CONTINUE;
909}
910
911static int emulator_write_std(unsigned long addr,
912 unsigned long val,
913 unsigned int bytes,
914 struct x86_emulate_ctxt *ctxt)
915{
916 printk(KERN_ERR "emulator_write_std: addr %lx n %d\n",
917 addr, bytes);
918 return X86EMUL_UNHANDLEABLE;
919}
920
921static int emulator_read_emulated(unsigned long addr,
922 unsigned long *val,
923 unsigned int bytes,
924 struct x86_emulate_ctxt *ctxt)
925{
926 struct kvm_vcpu *vcpu = ctxt->vcpu;
927
928 if (vcpu->mmio_read_completed) {
929 memcpy(val, vcpu->mmio_data, bytes);
930 vcpu->mmio_read_completed = 0;
931 return X86EMUL_CONTINUE;
932 } else if (emulator_read_std(addr, val, bytes, ctxt)
933 == X86EMUL_CONTINUE)
934 return X86EMUL_CONTINUE;
935 else {
936 gpa_t gpa = vcpu->mmu.gva_to_gpa(vcpu, addr);
d27d4aca 937
6aa8b732 938 if (gpa == UNMAPPED_GVA)
d27d4aca 939 return X86EMUL_PROPAGATE_FAULT;
6aa8b732
AK
940 vcpu->mmio_needed = 1;
941 vcpu->mmio_phys_addr = gpa;
942 vcpu->mmio_size = bytes;
943 vcpu->mmio_is_write = 0;
944
945 return X86EMUL_UNHANDLEABLE;
946 }
947}
948
da4a00f0
AK
949static int emulator_write_phys(struct kvm_vcpu *vcpu, gpa_t gpa,
950 unsigned long val, int bytes)
951{
952 struct kvm_memory_slot *m;
953 struct page *page;
954 void *virt;
955
956 if (((gpa + bytes - 1) >> PAGE_SHIFT) != (gpa >> PAGE_SHIFT))
957 return 0;
958 m = gfn_to_memslot(vcpu->kvm, gpa >> PAGE_SHIFT);
959 if (!m)
960 return 0;
961 page = gfn_to_page(m, gpa >> PAGE_SHIFT);
962 kvm_mmu_pre_write(vcpu, gpa, bytes);
ab51a434 963 mark_page_dirty(vcpu->kvm, gpa >> PAGE_SHIFT);
da4a00f0
AK
964 virt = kmap_atomic(page, KM_USER0);
965 memcpy(virt + offset_in_page(gpa), &val, bytes);
966 kunmap_atomic(virt, KM_USER0);
967 kvm_mmu_post_write(vcpu, gpa, bytes);
968 return 1;
969}
970
6aa8b732
AK
971static int emulator_write_emulated(unsigned long addr,
972 unsigned long val,
973 unsigned int bytes,
974 struct x86_emulate_ctxt *ctxt)
975{
976 struct kvm_vcpu *vcpu = ctxt->vcpu;
977 gpa_t gpa = vcpu->mmu.gva_to_gpa(vcpu, addr);
978
979 if (gpa == UNMAPPED_GVA)
980 return X86EMUL_PROPAGATE_FAULT;
981
da4a00f0
AK
982 if (emulator_write_phys(vcpu, gpa, val, bytes))
983 return X86EMUL_CONTINUE;
984
6aa8b732
AK
985 vcpu->mmio_needed = 1;
986 vcpu->mmio_phys_addr = gpa;
987 vcpu->mmio_size = bytes;
988 vcpu->mmio_is_write = 1;
989 memcpy(vcpu->mmio_data, &val, bytes);
990
991 return X86EMUL_CONTINUE;
992}
993
994static int emulator_cmpxchg_emulated(unsigned long addr,
995 unsigned long old,
996 unsigned long new,
997 unsigned int bytes,
998 struct x86_emulate_ctxt *ctxt)
999{
1000 static int reported;
1001
1002 if (!reported) {
1003 reported = 1;
1004 printk(KERN_WARNING "kvm: emulating exchange as write\n");
1005 }
1006 return emulator_write_emulated(addr, new, bytes, ctxt);
1007}
1008
32b35627
AK
1009#ifdef CONFIG_X86_32
1010
1011static int emulator_cmpxchg8b_emulated(unsigned long addr,
1012 unsigned long old_lo,
1013 unsigned long old_hi,
1014 unsigned long new_lo,
1015 unsigned long new_hi,
1016 struct x86_emulate_ctxt *ctxt)
1017{
1018 static int reported;
1019 int r;
1020
1021 if (!reported) {
1022 reported = 1;
1023 printk(KERN_WARNING "kvm: emulating exchange8b as write\n");
1024 }
1025 r = emulator_write_emulated(addr, new_lo, 4, ctxt);
1026 if (r != X86EMUL_CONTINUE)
1027 return r;
1028 return emulator_write_emulated(addr+4, new_hi, 4, ctxt);
1029}
1030
1031#endif
1032
6aa8b732
AK
1033static unsigned long get_segment_base(struct kvm_vcpu *vcpu, int seg)
1034{
1035 return kvm_arch_ops->get_segment_base(vcpu, seg);
1036}
1037
1038int emulate_invlpg(struct kvm_vcpu *vcpu, gva_t address)
1039{
6aa8b732
AK
1040 return X86EMUL_CONTINUE;
1041}
1042
1043int emulate_clts(struct kvm_vcpu *vcpu)
1044{
399badf3 1045 unsigned long cr0;
6aa8b732 1046
399badf3
AK
1047 kvm_arch_ops->decache_cr0_cr4_guest_bits(vcpu);
1048 cr0 = vcpu->cr0 & ~CR0_TS_MASK;
6aa8b732
AK
1049 kvm_arch_ops->set_cr0(vcpu, cr0);
1050 return X86EMUL_CONTINUE;
1051}
1052
1053int emulator_get_dr(struct x86_emulate_ctxt* ctxt, int dr, unsigned long *dest)
1054{
1055 struct kvm_vcpu *vcpu = ctxt->vcpu;
1056
1057 switch (dr) {
1058 case 0 ... 3:
1059 *dest = kvm_arch_ops->get_dr(vcpu, dr);
1060 return X86EMUL_CONTINUE;
1061 default:
1062 printk(KERN_DEBUG "%s: unexpected dr %u\n",
1063 __FUNCTION__, dr);
1064 return X86EMUL_UNHANDLEABLE;
1065 }
1066}
1067
1068int emulator_set_dr(struct x86_emulate_ctxt *ctxt, int dr, unsigned long value)
1069{
1070 unsigned long mask = (ctxt->mode == X86EMUL_MODE_PROT64) ? ~0ULL : ~0U;
1071 int exception;
1072
1073 kvm_arch_ops->set_dr(ctxt->vcpu, dr, value & mask, &exception);
1074 if (exception) {
1075 /* FIXME: better handling */
1076 return X86EMUL_UNHANDLEABLE;
1077 }
1078 return X86EMUL_CONTINUE;
1079}
1080
1081static void report_emulation_failure(struct x86_emulate_ctxt *ctxt)
1082{
1083 static int reported;
1084 u8 opcodes[4];
1085 unsigned long rip = ctxt->vcpu->rip;
1086 unsigned long rip_linear;
1087
1088 rip_linear = rip + get_segment_base(ctxt->vcpu, VCPU_SREG_CS);
1089
1090 if (reported)
1091 return;
1092
1093 emulator_read_std(rip_linear, (void *)opcodes, 4, ctxt);
1094
1095 printk(KERN_ERR "emulation failed but !mmio_needed?"
1096 " rip %lx %02x %02x %02x %02x\n",
1097 rip, opcodes[0], opcodes[1], opcodes[2], opcodes[3]);
1098 reported = 1;
1099}
1100
1101struct x86_emulate_ops emulate_ops = {
1102 .read_std = emulator_read_std,
1103 .write_std = emulator_write_std,
1104 .read_emulated = emulator_read_emulated,
1105 .write_emulated = emulator_write_emulated,
1106 .cmpxchg_emulated = emulator_cmpxchg_emulated,
32b35627
AK
1107#ifdef CONFIG_X86_32
1108 .cmpxchg8b_emulated = emulator_cmpxchg8b_emulated,
1109#endif
6aa8b732
AK
1110};
1111
1112int emulate_instruction(struct kvm_vcpu *vcpu,
1113 struct kvm_run *run,
1114 unsigned long cr2,
1115 u16 error_code)
1116{
1117 struct x86_emulate_ctxt emulate_ctxt;
1118 int r;
1119 int cs_db, cs_l;
1120
1121 kvm_arch_ops->cache_regs(vcpu);
1122
1123 kvm_arch_ops->get_cs_db_l_bits(vcpu, &cs_db, &cs_l);
1124
1125 emulate_ctxt.vcpu = vcpu;
1126 emulate_ctxt.eflags = kvm_arch_ops->get_rflags(vcpu);
1127 emulate_ctxt.cr2 = cr2;
1128 emulate_ctxt.mode = (emulate_ctxt.eflags & X86_EFLAGS_VM)
1129 ? X86EMUL_MODE_REAL : cs_l
1130 ? X86EMUL_MODE_PROT64 : cs_db
1131 ? X86EMUL_MODE_PROT32 : X86EMUL_MODE_PROT16;
1132
1133 if (emulate_ctxt.mode == X86EMUL_MODE_PROT64) {
1134 emulate_ctxt.cs_base = 0;
1135 emulate_ctxt.ds_base = 0;
1136 emulate_ctxt.es_base = 0;
1137 emulate_ctxt.ss_base = 0;
1138 } else {
1139 emulate_ctxt.cs_base = get_segment_base(vcpu, VCPU_SREG_CS);
1140 emulate_ctxt.ds_base = get_segment_base(vcpu, VCPU_SREG_DS);
1141 emulate_ctxt.es_base = get_segment_base(vcpu, VCPU_SREG_ES);
1142 emulate_ctxt.ss_base = get_segment_base(vcpu, VCPU_SREG_SS);
1143 }
1144
1145 emulate_ctxt.gs_base = get_segment_base(vcpu, VCPU_SREG_GS);
1146 emulate_ctxt.fs_base = get_segment_base(vcpu, VCPU_SREG_FS);
1147
1148 vcpu->mmio_is_write = 0;
1149 r = x86_emulate_memop(&emulate_ctxt, &emulate_ops);
1150
1151 if ((r || vcpu->mmio_is_write) && run) {
1152 run->mmio.phys_addr = vcpu->mmio_phys_addr;
1153 memcpy(run->mmio.data, vcpu->mmio_data, 8);
1154 run->mmio.len = vcpu->mmio_size;
1155 run->mmio.is_write = vcpu->mmio_is_write;
1156 }
1157
1158 if (r) {
a436036b
AK
1159 if (kvm_mmu_unprotect_page_virt(vcpu, cr2))
1160 return EMULATE_DONE;
6aa8b732
AK
1161 if (!vcpu->mmio_needed) {
1162 report_emulation_failure(&emulate_ctxt);
1163 return EMULATE_FAIL;
1164 }
1165 return EMULATE_DO_MMIO;
1166 }
1167
1168 kvm_arch_ops->decache_regs(vcpu);
1169 kvm_arch_ops->set_rflags(vcpu, emulate_ctxt.eflags);
1170
1171 if (vcpu->mmio_is_write)
1172 return EMULATE_DO_MMIO;
1173
1174 return EMULATE_DONE;
1175}
1176EXPORT_SYMBOL_GPL(emulate_instruction);
1177
270fd9b9
AK
1178int kvm_hypercall(struct kvm_vcpu *vcpu, struct kvm_run *run)
1179{
1180 unsigned long nr, a0, a1, a2, a3, a4, a5, ret;
1181
9b22bf57 1182 kvm_arch_ops->cache_regs(vcpu);
270fd9b9
AK
1183 ret = -KVM_EINVAL;
1184#ifdef CONFIG_X86_64
1185 if (is_long_mode(vcpu)) {
1186 nr = vcpu->regs[VCPU_REGS_RAX];
1187 a0 = vcpu->regs[VCPU_REGS_RDI];
1188 a1 = vcpu->regs[VCPU_REGS_RSI];
1189 a2 = vcpu->regs[VCPU_REGS_RDX];
1190 a3 = vcpu->regs[VCPU_REGS_RCX];
1191 a4 = vcpu->regs[VCPU_REGS_R8];
1192 a5 = vcpu->regs[VCPU_REGS_R9];
1193 } else
1194#endif
1195 {
1196 nr = vcpu->regs[VCPU_REGS_RBX] & -1u;
1197 a0 = vcpu->regs[VCPU_REGS_RAX] & -1u;
1198 a1 = vcpu->regs[VCPU_REGS_RCX] & -1u;
1199 a2 = vcpu->regs[VCPU_REGS_RDX] & -1u;
1200 a3 = vcpu->regs[VCPU_REGS_RSI] & -1u;
1201 a4 = vcpu->regs[VCPU_REGS_RDI] & -1u;
1202 a5 = vcpu->regs[VCPU_REGS_RBP] & -1u;
1203 }
1204 switch (nr) {
1205 default:
1206 ;
1207 }
1208 vcpu->regs[VCPU_REGS_RAX] = ret;
9b22bf57 1209 kvm_arch_ops->decache_regs(vcpu);
270fd9b9
AK
1210 return 1;
1211}
1212EXPORT_SYMBOL_GPL(kvm_hypercall);
1213
6aa8b732
AK
1214static u64 mk_cr_64(u64 curr_cr, u32 new_val)
1215{
1216 return (curr_cr & ~((1ULL << 32) - 1)) | new_val;
1217}
1218
1219void realmode_lgdt(struct kvm_vcpu *vcpu, u16 limit, unsigned long base)
1220{
1221 struct descriptor_table dt = { limit, base };
1222
1223 kvm_arch_ops->set_gdt(vcpu, &dt);
1224}
1225
1226void realmode_lidt(struct kvm_vcpu *vcpu, u16 limit, unsigned long base)
1227{
1228 struct descriptor_table dt = { limit, base };
1229
1230 kvm_arch_ops->set_idt(vcpu, &dt);
1231}
1232
1233void realmode_lmsw(struct kvm_vcpu *vcpu, unsigned long msw,
1234 unsigned long *rflags)
1235{
1236 lmsw(vcpu, msw);
1237 *rflags = kvm_arch_ops->get_rflags(vcpu);
1238}
1239
1240unsigned long realmode_get_cr(struct kvm_vcpu *vcpu, int cr)
1241{
399badf3 1242 kvm_arch_ops->decache_cr0_cr4_guest_bits(vcpu);
6aa8b732
AK
1243 switch (cr) {
1244 case 0:
1245 return vcpu->cr0;
1246 case 2:
1247 return vcpu->cr2;
1248 case 3:
1249 return vcpu->cr3;
1250 case 4:
1251 return vcpu->cr4;
1252 default:
1253 vcpu_printf(vcpu, "%s: unexpected cr %u\n", __FUNCTION__, cr);
1254 return 0;
1255 }
1256}
1257
1258void realmode_set_cr(struct kvm_vcpu *vcpu, int cr, unsigned long val,
1259 unsigned long *rflags)
1260{
1261 switch (cr) {
1262 case 0:
1263 set_cr0(vcpu, mk_cr_64(vcpu->cr0, val));
1264 *rflags = kvm_arch_ops->get_rflags(vcpu);
1265 break;
1266 case 2:
1267 vcpu->cr2 = val;
1268 break;
1269 case 3:
1270 set_cr3(vcpu, val);
1271 break;
1272 case 4:
1273 set_cr4(vcpu, mk_cr_64(vcpu->cr4, val));
1274 break;
1275 default:
1276 vcpu_printf(vcpu, "%s: unexpected cr %u\n", __FUNCTION__, cr);
1277 }
1278}
1279
102d8325
IM
1280/*
1281 * Register the para guest with the host:
1282 */
1283static int vcpu_register_para(struct kvm_vcpu *vcpu, gpa_t para_state_gpa)
1284{
1285 struct kvm_vcpu_para_state *para_state;
1286 hpa_t para_state_hpa, hypercall_hpa;
1287 struct page *para_state_page;
1288 unsigned char *hypercall;
1289 gpa_t hypercall_gpa;
1290
1291 printk(KERN_DEBUG "kvm: guest trying to enter paravirtual mode\n");
1292 printk(KERN_DEBUG ".... para_state_gpa: %08Lx\n", para_state_gpa);
1293
1294 /*
1295 * Needs to be page aligned:
1296 */
1297 if (para_state_gpa != PAGE_ALIGN(para_state_gpa))
1298 goto err_gp;
1299
1300 para_state_hpa = gpa_to_hpa(vcpu, para_state_gpa);
1301 printk(KERN_DEBUG ".... para_state_hpa: %08Lx\n", para_state_hpa);
1302 if (is_error_hpa(para_state_hpa))
1303 goto err_gp;
1304
ab51a434 1305 mark_page_dirty(vcpu->kvm, para_state_gpa >> PAGE_SHIFT);
102d8325
IM
1306 para_state_page = pfn_to_page(para_state_hpa >> PAGE_SHIFT);
1307 para_state = kmap_atomic(para_state_page, KM_USER0);
1308
1309 printk(KERN_DEBUG ".... guest version: %d\n", para_state->guest_version);
1310 printk(KERN_DEBUG ".... size: %d\n", para_state->size);
1311
1312 para_state->host_version = KVM_PARA_API_VERSION;
1313 /*
1314 * We cannot support guests that try to register themselves
1315 * with a newer API version than the host supports:
1316 */
1317 if (para_state->guest_version > KVM_PARA_API_VERSION) {
1318 para_state->ret = -KVM_EINVAL;
1319 goto err_kunmap_skip;
1320 }
1321
1322 hypercall_gpa = para_state->hypercall_gpa;
1323 hypercall_hpa = gpa_to_hpa(vcpu, hypercall_gpa);
1324 printk(KERN_DEBUG ".... hypercall_hpa: %08Lx\n", hypercall_hpa);
1325 if (is_error_hpa(hypercall_hpa)) {
1326 para_state->ret = -KVM_EINVAL;
1327 goto err_kunmap_skip;
1328 }
1329
1330 printk(KERN_DEBUG "kvm: para guest successfully registered.\n");
1331 vcpu->para_state_page = para_state_page;
1332 vcpu->para_state_gpa = para_state_gpa;
1333 vcpu->hypercall_gpa = hypercall_gpa;
1334
ab51a434 1335 mark_page_dirty(vcpu->kvm, hypercall_gpa >> PAGE_SHIFT);
102d8325
IM
1336 hypercall = kmap_atomic(pfn_to_page(hypercall_hpa >> PAGE_SHIFT),
1337 KM_USER1) + (hypercall_hpa & ~PAGE_MASK);
1338 kvm_arch_ops->patch_hypercall(vcpu, hypercall);
1339 kunmap_atomic(hypercall, KM_USER1);
1340
1341 para_state->ret = 0;
1342err_kunmap_skip:
1343 kunmap_atomic(para_state, KM_USER0);
1344 return 0;
1345err_gp:
1346 return 1;
1347}
1348
3bab1f5d
AK
1349int kvm_get_msr_common(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata)
1350{
1351 u64 data;
1352
1353 switch (msr) {
1354 case 0xc0010010: /* SYSCFG */
1355 case 0xc0010015: /* HWCR */
1356 case MSR_IA32_PLATFORM_ID:
1357 case MSR_IA32_P5_MC_ADDR:
1358 case MSR_IA32_P5_MC_TYPE:
1359 case MSR_IA32_MC0_CTL:
1360 case MSR_IA32_MCG_STATUS:
1361 case MSR_IA32_MCG_CAP:
1362 case MSR_IA32_MC0_MISC:
1363 case MSR_IA32_MC0_MISC+4:
1364 case MSR_IA32_MC0_MISC+8:
1365 case MSR_IA32_MC0_MISC+12:
1366 case MSR_IA32_MC0_MISC+16:
1367 case MSR_IA32_UCODE_REV:
a8d13ea2 1368 case MSR_IA32_PERF_STATUS:
3bab1f5d
AK
1369 /* MTRR registers */
1370 case 0xfe:
1371 case 0x200 ... 0x2ff:
1372 data = 0;
1373 break;
a8d13ea2
AK
1374 case 0xcd: /* fsb frequency */
1375 data = 3;
1376 break;
3bab1f5d
AK
1377 case MSR_IA32_APICBASE:
1378 data = vcpu->apic_base;
1379 break;
6f00e68f
AK
1380 case MSR_IA32_MISC_ENABLE:
1381 data = vcpu->ia32_misc_enable_msr;
1382 break;
3bab1f5d
AK
1383#ifdef CONFIG_X86_64
1384 case MSR_EFER:
1385 data = vcpu->shadow_efer;
1386 break;
1387#endif
1388 default:
1389 printk(KERN_ERR "kvm: unhandled rdmsr: 0x%x\n", msr);
1390 return 1;
1391 }
1392 *pdata = data;
1393 return 0;
1394}
1395EXPORT_SYMBOL_GPL(kvm_get_msr_common);
1396
6aa8b732
AK
1397/*
1398 * Reads an msr value (of 'msr_index') into 'pdata'.
1399 * Returns 0 on success, non-0 otherwise.
1400 * Assumes vcpu_load() was already called.
1401 */
1402static int get_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 *pdata)
1403{
1404 return kvm_arch_ops->get_msr(vcpu, msr_index, pdata);
1405}
1406
05b3e0c2 1407#ifdef CONFIG_X86_64
6aa8b732 1408
3bab1f5d 1409static void set_efer(struct kvm_vcpu *vcpu, u64 efer)
6aa8b732 1410{
6aa8b732
AK
1411 if (efer & EFER_RESERVED_BITS) {
1412 printk(KERN_DEBUG "set_efer: 0x%llx #GP, reserved bits\n",
1413 efer);
1414 inject_gp(vcpu);
1415 return;
1416 }
1417
1418 if (is_paging(vcpu)
1419 && (vcpu->shadow_efer & EFER_LME) != (efer & EFER_LME)) {
1420 printk(KERN_DEBUG "set_efer: #GP, change LME while paging\n");
1421 inject_gp(vcpu);
1422 return;
1423 }
1424
7725f0ba
AK
1425 kvm_arch_ops->set_efer(vcpu, efer);
1426
6aa8b732
AK
1427 efer &= ~EFER_LMA;
1428 efer |= vcpu->shadow_efer & EFER_LMA;
1429
1430 vcpu->shadow_efer = efer;
6aa8b732 1431}
6aa8b732
AK
1432
1433#endif
1434
3bab1f5d
AK
1435int kvm_set_msr_common(struct kvm_vcpu *vcpu, u32 msr, u64 data)
1436{
1437 switch (msr) {
1438#ifdef CONFIG_X86_64
1439 case MSR_EFER:
1440 set_efer(vcpu, data);
1441 break;
1442#endif
1443 case MSR_IA32_MC0_STATUS:
1444 printk(KERN_WARNING "%s: MSR_IA32_MC0_STATUS 0x%llx, nop\n",
1445 __FUNCTION__, data);
1446 break;
1447 case MSR_IA32_UCODE_REV:
1448 case MSR_IA32_UCODE_WRITE:
1449 case 0x200 ... 0x2ff: /* MTRRs */
1450 break;
1451 case MSR_IA32_APICBASE:
1452 vcpu->apic_base = data;
1453 break;
6f00e68f
AK
1454 case MSR_IA32_MISC_ENABLE:
1455 vcpu->ia32_misc_enable_msr = data;
1456 break;
102d8325
IM
1457 /*
1458 * This is the 'probe whether the host is KVM' logic:
1459 */
1460 case MSR_KVM_API_MAGIC:
1461 return vcpu_register_para(vcpu, data);
1462
3bab1f5d
AK
1463 default:
1464 printk(KERN_ERR "kvm: unhandled wrmsr: 0x%x\n", msr);
1465 return 1;
1466 }
1467 return 0;
1468}
1469EXPORT_SYMBOL_GPL(kvm_set_msr_common);
1470
6aa8b732
AK
1471/*
1472 * Writes msr value into into the appropriate "register".
1473 * Returns 0 on success, non-0 otherwise.
1474 * Assumes vcpu_load() was already called.
1475 */
1476static int set_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 data)
1477{
1478 return kvm_arch_ops->set_msr(vcpu, msr_index, data);
1479}
1480
1481void kvm_resched(struct kvm_vcpu *vcpu)
1482{
1483 vcpu_put(vcpu);
1484 cond_resched();
bccf2150 1485 vcpu_load(vcpu);
6aa8b732
AK
1486}
1487EXPORT_SYMBOL_GPL(kvm_resched);
1488
1489void load_msrs(struct vmx_msr_entry *e, int n)
1490{
1491 int i;
1492
1493 for (i = 0; i < n; ++i)
1494 wrmsrl(e[i].index, e[i].data);
1495}
1496EXPORT_SYMBOL_GPL(load_msrs);
1497
1498void save_msrs(struct vmx_msr_entry *e, int n)
1499{
1500 int i;
1501
1502 for (i = 0; i < n; ++i)
1503 rdmsrl(e[i].index, e[i].data);
1504}
1505EXPORT_SYMBOL_GPL(save_msrs);
1506
46fc1477
AK
1507static void complete_pio(struct kvm_vcpu *vcpu)
1508{
1509 struct kvm_io *io = &vcpu->run->io;
1510 long delta;
1511
1512 kvm_arch_ops->cache_regs(vcpu);
1513
1514 if (!io->string) {
1515 if (io->direction == KVM_EXIT_IO_IN)
1516 memcpy(&vcpu->regs[VCPU_REGS_RAX], &io->value,
1517 io->size);
1518 } else {
1519 delta = 1;
1520 if (io->rep) {
1521 delta *= io->count;
1522 /*
1523 * The size of the register should really depend on
1524 * current address size.
1525 */
1526 vcpu->regs[VCPU_REGS_RCX] -= delta;
1527 }
1528 if (io->string_down)
1529 delta = -delta;
1530 delta *= io->size;
1531 if (io->direction == KVM_EXIT_IO_IN)
1532 vcpu->regs[VCPU_REGS_RDI] += delta;
1533 else
1534 vcpu->regs[VCPU_REGS_RSI] += delta;
1535 }
1536
1537 vcpu->pio_pending = 0;
1538 vcpu->run->io_completed = 0;
1539
1540 kvm_arch_ops->decache_regs(vcpu);
1541
1542 kvm_arch_ops->skip_emulated_instruction(vcpu);
1543}
1544
bccf2150 1545static int kvm_vcpu_ioctl_run(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
6aa8b732 1546{
6aa8b732
AK
1547 int r;
1548
bccf2150 1549 vcpu_load(vcpu);
6aa8b732 1550
54810342
DL
1551 /* re-sync apic's tpr */
1552 vcpu->cr8 = kvm_run->cr8;
1553
6aa8b732
AK
1554 if (kvm_run->emulated) {
1555 kvm_arch_ops->skip_emulated_instruction(vcpu);
1556 kvm_run->emulated = 0;
1557 }
1558
46fc1477
AK
1559 if (kvm_run->io_completed) {
1560 if (vcpu->pio_pending)
1561 complete_pio(vcpu);
1562 else {
1563 memcpy(vcpu->mmio_data, kvm_run->mmio.data, 8);
1564 vcpu->mmio_read_completed = 1;
1565 }
6aa8b732
AK
1566 }
1567
1568 vcpu->mmio_needed = 0;
1569
1570 r = kvm_arch_ops->run(vcpu, kvm_run);
1571
1572 vcpu_put(vcpu);
1573 return r;
1574}
1575
bccf2150
AK
1576static int kvm_vcpu_ioctl_get_regs(struct kvm_vcpu *vcpu,
1577 struct kvm_regs *regs)
6aa8b732 1578{
bccf2150 1579 vcpu_load(vcpu);
6aa8b732
AK
1580
1581 kvm_arch_ops->cache_regs(vcpu);
1582
1583 regs->rax = vcpu->regs[VCPU_REGS_RAX];
1584 regs->rbx = vcpu->regs[VCPU_REGS_RBX];
1585 regs->rcx = vcpu->regs[VCPU_REGS_RCX];
1586 regs->rdx = vcpu->regs[VCPU_REGS_RDX];
1587 regs->rsi = vcpu->regs[VCPU_REGS_RSI];
1588 regs->rdi = vcpu->regs[VCPU_REGS_RDI];
1589 regs->rsp = vcpu->regs[VCPU_REGS_RSP];
1590 regs->rbp = vcpu->regs[VCPU_REGS_RBP];
05b3e0c2 1591#ifdef CONFIG_X86_64
6aa8b732
AK
1592 regs->r8 = vcpu->regs[VCPU_REGS_R8];
1593 regs->r9 = vcpu->regs[VCPU_REGS_R9];
1594 regs->r10 = vcpu->regs[VCPU_REGS_R10];
1595 regs->r11 = vcpu->regs[VCPU_REGS_R11];
1596 regs->r12 = vcpu->regs[VCPU_REGS_R12];
1597 regs->r13 = vcpu->regs[VCPU_REGS_R13];
1598 regs->r14 = vcpu->regs[VCPU_REGS_R14];
1599 regs->r15 = vcpu->regs[VCPU_REGS_R15];
1600#endif
1601
1602 regs->rip = vcpu->rip;
1603 regs->rflags = kvm_arch_ops->get_rflags(vcpu);
1604
1605 /*
1606 * Don't leak debug flags in case they were set for guest debugging
1607 */
1608 if (vcpu->guest_debug.enabled && vcpu->guest_debug.singlestep)
1609 regs->rflags &= ~(X86_EFLAGS_TF | X86_EFLAGS_RF);
1610
1611 vcpu_put(vcpu);
1612
1613 return 0;
1614}
1615
bccf2150
AK
1616static int kvm_vcpu_ioctl_set_regs(struct kvm_vcpu *vcpu,
1617 struct kvm_regs *regs)
6aa8b732 1618{
bccf2150 1619 vcpu_load(vcpu);
6aa8b732
AK
1620
1621 vcpu->regs[VCPU_REGS_RAX] = regs->rax;
1622 vcpu->regs[VCPU_REGS_RBX] = regs->rbx;
1623 vcpu->regs[VCPU_REGS_RCX] = regs->rcx;
1624 vcpu->regs[VCPU_REGS_RDX] = regs->rdx;
1625 vcpu->regs[VCPU_REGS_RSI] = regs->rsi;
1626 vcpu->regs[VCPU_REGS_RDI] = regs->rdi;
1627 vcpu->regs[VCPU_REGS_RSP] = regs->rsp;
1628 vcpu->regs[VCPU_REGS_RBP] = regs->rbp;
05b3e0c2 1629#ifdef CONFIG_X86_64
6aa8b732
AK
1630 vcpu->regs[VCPU_REGS_R8] = regs->r8;
1631 vcpu->regs[VCPU_REGS_R9] = regs->r9;
1632 vcpu->regs[VCPU_REGS_R10] = regs->r10;
1633 vcpu->regs[VCPU_REGS_R11] = regs->r11;
1634 vcpu->regs[VCPU_REGS_R12] = regs->r12;
1635 vcpu->regs[VCPU_REGS_R13] = regs->r13;
1636 vcpu->regs[VCPU_REGS_R14] = regs->r14;
1637 vcpu->regs[VCPU_REGS_R15] = regs->r15;
1638#endif
1639
1640 vcpu->rip = regs->rip;
1641 kvm_arch_ops->set_rflags(vcpu, regs->rflags);
1642
1643 kvm_arch_ops->decache_regs(vcpu);
1644
1645 vcpu_put(vcpu);
1646
1647 return 0;
1648}
1649
1650static void get_segment(struct kvm_vcpu *vcpu,
1651 struct kvm_segment *var, int seg)
1652{
1653 return kvm_arch_ops->get_segment(vcpu, var, seg);
1654}
1655
bccf2150
AK
1656static int kvm_vcpu_ioctl_get_sregs(struct kvm_vcpu *vcpu,
1657 struct kvm_sregs *sregs)
6aa8b732 1658{
6aa8b732
AK
1659 struct descriptor_table dt;
1660
bccf2150 1661 vcpu_load(vcpu);
6aa8b732
AK
1662
1663 get_segment(vcpu, &sregs->cs, VCPU_SREG_CS);
1664 get_segment(vcpu, &sregs->ds, VCPU_SREG_DS);
1665 get_segment(vcpu, &sregs->es, VCPU_SREG_ES);
1666 get_segment(vcpu, &sregs->fs, VCPU_SREG_FS);
1667 get_segment(vcpu, &sregs->gs, VCPU_SREG_GS);
1668 get_segment(vcpu, &sregs->ss, VCPU_SREG_SS);
1669
1670 get_segment(vcpu, &sregs->tr, VCPU_SREG_TR);
1671 get_segment(vcpu, &sregs->ldt, VCPU_SREG_LDTR);
1672
1673 kvm_arch_ops->get_idt(vcpu, &dt);
1674 sregs->idt.limit = dt.limit;
1675 sregs->idt.base = dt.base;
1676 kvm_arch_ops->get_gdt(vcpu, &dt);
1677 sregs->gdt.limit = dt.limit;
1678 sregs->gdt.base = dt.base;
1679
399badf3 1680 kvm_arch_ops->decache_cr0_cr4_guest_bits(vcpu);
6aa8b732
AK
1681 sregs->cr0 = vcpu->cr0;
1682 sregs->cr2 = vcpu->cr2;
1683 sregs->cr3 = vcpu->cr3;
1684 sregs->cr4 = vcpu->cr4;
1685 sregs->cr8 = vcpu->cr8;
1686 sregs->efer = vcpu->shadow_efer;
1687 sregs->apic_base = vcpu->apic_base;
1688
1689 memcpy(sregs->interrupt_bitmap, vcpu->irq_pending,
1690 sizeof sregs->interrupt_bitmap);
1691
1692 vcpu_put(vcpu);
1693
1694 return 0;
1695}
1696
1697static void set_segment(struct kvm_vcpu *vcpu,
1698 struct kvm_segment *var, int seg)
1699{
1700 return kvm_arch_ops->set_segment(vcpu, var, seg);
1701}
1702
bccf2150
AK
1703static int kvm_vcpu_ioctl_set_sregs(struct kvm_vcpu *vcpu,
1704 struct kvm_sregs *sregs)
6aa8b732 1705{
6aa8b732
AK
1706 int mmu_reset_needed = 0;
1707 int i;
1708 struct descriptor_table dt;
1709
bccf2150 1710 vcpu_load(vcpu);
6aa8b732
AK
1711
1712 set_segment(vcpu, &sregs->cs, VCPU_SREG_CS);
1713 set_segment(vcpu, &sregs->ds, VCPU_SREG_DS);
1714 set_segment(vcpu, &sregs->es, VCPU_SREG_ES);
1715 set_segment(vcpu, &sregs->fs, VCPU_SREG_FS);
1716 set_segment(vcpu, &sregs->gs, VCPU_SREG_GS);
1717 set_segment(vcpu, &sregs->ss, VCPU_SREG_SS);
1718
1719 set_segment(vcpu, &sregs->tr, VCPU_SREG_TR);
1720 set_segment(vcpu, &sregs->ldt, VCPU_SREG_LDTR);
1721
1722 dt.limit = sregs->idt.limit;
1723 dt.base = sregs->idt.base;
1724 kvm_arch_ops->set_idt(vcpu, &dt);
1725 dt.limit = sregs->gdt.limit;
1726 dt.base = sregs->gdt.base;
1727 kvm_arch_ops->set_gdt(vcpu, &dt);
1728
1729 vcpu->cr2 = sregs->cr2;
1730 mmu_reset_needed |= vcpu->cr3 != sregs->cr3;
1731 vcpu->cr3 = sregs->cr3;
1732
1733 vcpu->cr8 = sregs->cr8;
1734
1735 mmu_reset_needed |= vcpu->shadow_efer != sregs->efer;
05b3e0c2 1736#ifdef CONFIG_X86_64
6aa8b732
AK
1737 kvm_arch_ops->set_efer(vcpu, sregs->efer);
1738#endif
1739 vcpu->apic_base = sregs->apic_base;
1740
399badf3
AK
1741 kvm_arch_ops->decache_cr0_cr4_guest_bits(vcpu);
1742
6aa8b732
AK
1743 mmu_reset_needed |= vcpu->cr0 != sregs->cr0;
1744 kvm_arch_ops->set_cr0_no_modeswitch(vcpu, sregs->cr0);
1745
1746 mmu_reset_needed |= vcpu->cr4 != sregs->cr4;
1747 kvm_arch_ops->set_cr4(vcpu, sregs->cr4);
1b0973bd
AK
1748 if (!is_long_mode(vcpu) && is_pae(vcpu))
1749 load_pdptrs(vcpu, vcpu->cr3);
6aa8b732
AK
1750
1751 if (mmu_reset_needed)
1752 kvm_mmu_reset_context(vcpu);
1753
1754 memcpy(vcpu->irq_pending, sregs->interrupt_bitmap,
1755 sizeof vcpu->irq_pending);
1756 vcpu->irq_summary = 0;
1757 for (i = 0; i < NR_IRQ_WORDS; ++i)
1758 if (vcpu->irq_pending[i])
1759 __set_bit(i, &vcpu->irq_summary);
1760
1761 vcpu_put(vcpu);
1762
1763 return 0;
1764}
1765
1766/*
1767 * List of msr numbers which we expose to userspace through KVM_GET_MSRS
1768 * and KVM_SET_MSRS, and KVM_GET_MSR_INDEX_LIST.
bf591b24
MR
1769 *
1770 * This list is modified at module load time to reflect the
1771 * capabilities of the host cpu.
6aa8b732
AK
1772 */
1773static u32 msrs_to_save[] = {
1774 MSR_IA32_SYSENTER_CS, MSR_IA32_SYSENTER_ESP, MSR_IA32_SYSENTER_EIP,
1775 MSR_K6_STAR,
05b3e0c2 1776#ifdef CONFIG_X86_64
6aa8b732
AK
1777 MSR_CSTAR, MSR_KERNEL_GS_BASE, MSR_SYSCALL_MASK, MSR_LSTAR,
1778#endif
1779 MSR_IA32_TIME_STAMP_COUNTER,
1780};
1781
bf591b24
MR
1782static unsigned num_msrs_to_save;
1783
6f00e68f
AK
1784static u32 emulated_msrs[] = {
1785 MSR_IA32_MISC_ENABLE,
1786};
1787
bf591b24
MR
1788static __init void kvm_init_msr_list(void)
1789{
1790 u32 dummy[2];
1791 unsigned i, j;
1792
1793 for (i = j = 0; i < ARRAY_SIZE(msrs_to_save); i++) {
1794 if (rdmsr_safe(msrs_to_save[i], &dummy[0], &dummy[1]) < 0)
1795 continue;
1796 if (j < i)
1797 msrs_to_save[j] = msrs_to_save[i];
1798 j++;
1799 }
1800 num_msrs_to_save = j;
1801}
6aa8b732
AK
1802
1803/*
1804 * Adapt set_msr() to msr_io()'s calling convention
1805 */
1806static int do_set_msr(struct kvm_vcpu *vcpu, unsigned index, u64 *data)
1807{
1808 return set_msr(vcpu, index, *data);
1809}
1810
1811/*
1812 * Read or write a bunch of msrs. All parameters are kernel addresses.
1813 *
1814 * @return number of msrs set successfully.
1815 */
bccf2150 1816static int __msr_io(struct kvm_vcpu *vcpu, struct kvm_msrs *msrs,
6aa8b732
AK
1817 struct kvm_msr_entry *entries,
1818 int (*do_msr)(struct kvm_vcpu *vcpu,
1819 unsigned index, u64 *data))
1820{
6aa8b732
AK
1821 int i;
1822
bccf2150 1823 vcpu_load(vcpu);
6aa8b732
AK
1824
1825 for (i = 0; i < msrs->nmsrs; ++i)
1826 if (do_msr(vcpu, entries[i].index, &entries[i].data))
1827 break;
1828
1829 vcpu_put(vcpu);
1830
1831 return i;
1832}
1833
1834/*
1835 * Read or write a bunch of msrs. Parameters are user addresses.
1836 *
1837 * @return number of msrs set successfully.
1838 */
bccf2150 1839static int msr_io(struct kvm_vcpu *vcpu, struct kvm_msrs __user *user_msrs,
6aa8b732
AK
1840 int (*do_msr)(struct kvm_vcpu *vcpu,
1841 unsigned index, u64 *data),
1842 int writeback)
1843{
1844 struct kvm_msrs msrs;
1845 struct kvm_msr_entry *entries;
1846 int r, n;
1847 unsigned size;
1848
1849 r = -EFAULT;
1850 if (copy_from_user(&msrs, user_msrs, sizeof msrs))
1851 goto out;
1852
1853 r = -E2BIG;
1854 if (msrs.nmsrs >= MAX_IO_MSRS)
1855 goto out;
1856
1857 r = -ENOMEM;
1858 size = sizeof(struct kvm_msr_entry) * msrs.nmsrs;
1859 entries = vmalloc(size);
1860 if (!entries)
1861 goto out;
1862
1863 r = -EFAULT;
1864 if (copy_from_user(entries, user_msrs->entries, size))
1865 goto out_free;
1866
bccf2150 1867 r = n = __msr_io(vcpu, &msrs, entries, do_msr);
6aa8b732
AK
1868 if (r < 0)
1869 goto out_free;
1870
1871 r = -EFAULT;
1872 if (writeback && copy_to_user(user_msrs->entries, entries, size))
1873 goto out_free;
1874
1875 r = n;
1876
1877out_free:
1878 vfree(entries);
1879out:
1880 return r;
1881}
1882
1883/*
1884 * Translate a guest virtual address to a guest physical address.
1885 */
bccf2150
AK
1886static int kvm_vcpu_ioctl_translate(struct kvm_vcpu *vcpu,
1887 struct kvm_translation *tr)
6aa8b732
AK
1888{
1889 unsigned long vaddr = tr->linear_address;
6aa8b732
AK
1890 gpa_t gpa;
1891
bccf2150
AK
1892 vcpu_load(vcpu);
1893 spin_lock(&vcpu->kvm->lock);
6aa8b732
AK
1894 gpa = vcpu->mmu.gva_to_gpa(vcpu, vaddr);
1895 tr->physical_address = gpa;
1896 tr->valid = gpa != UNMAPPED_GVA;
1897 tr->writeable = 1;
1898 tr->usermode = 0;
bccf2150 1899 spin_unlock(&vcpu->kvm->lock);
6aa8b732
AK
1900 vcpu_put(vcpu);
1901
1902 return 0;
1903}
1904
bccf2150
AK
1905static int kvm_vcpu_ioctl_interrupt(struct kvm_vcpu *vcpu,
1906 struct kvm_interrupt *irq)
6aa8b732 1907{
6aa8b732
AK
1908 if (irq->irq < 0 || irq->irq >= 256)
1909 return -EINVAL;
bccf2150 1910 vcpu_load(vcpu);
6aa8b732
AK
1911
1912 set_bit(irq->irq, vcpu->irq_pending);
1913 set_bit(irq->irq / BITS_PER_LONG, &vcpu->irq_summary);
1914
1915 vcpu_put(vcpu);
1916
1917 return 0;
1918}
1919
bccf2150
AK
1920static int kvm_vcpu_ioctl_debug_guest(struct kvm_vcpu *vcpu,
1921 struct kvm_debug_guest *dbg)
6aa8b732 1922{
6aa8b732
AK
1923 int r;
1924
bccf2150 1925 vcpu_load(vcpu);
6aa8b732
AK
1926
1927 r = kvm_arch_ops->set_guest_debug(vcpu, dbg);
1928
1929 vcpu_put(vcpu);
1930
1931 return r;
1932}
1933
9a2bb7f4
AK
1934static struct page *kvm_vcpu_nopage(struct vm_area_struct *vma,
1935 unsigned long address,
1936 int *type)
1937{
1938 struct kvm_vcpu *vcpu = vma->vm_file->private_data;
1939 unsigned long pgoff;
1940 struct page *page;
1941
1942 *type = VM_FAULT_MINOR;
1943 pgoff = ((address - vma->vm_start) >> PAGE_SHIFT) + vma->vm_pgoff;
1944 if (pgoff != 0)
1945 return NOPAGE_SIGBUS;
1946 page = virt_to_page(vcpu->run);
1947 get_page(page);
1948 return page;
1949}
1950
1951static struct vm_operations_struct kvm_vcpu_vm_ops = {
1952 .nopage = kvm_vcpu_nopage,
1953};
1954
1955static int kvm_vcpu_mmap(struct file *file, struct vm_area_struct *vma)
1956{
1957 vma->vm_ops = &kvm_vcpu_vm_ops;
1958 return 0;
1959}
1960
bccf2150
AK
1961static int kvm_vcpu_release(struct inode *inode, struct file *filp)
1962{
1963 struct kvm_vcpu *vcpu = filp->private_data;
1964
1965 fput(vcpu->kvm->filp);
1966 return 0;
1967}
1968
1969static struct file_operations kvm_vcpu_fops = {
1970 .release = kvm_vcpu_release,
1971 .unlocked_ioctl = kvm_vcpu_ioctl,
1972 .compat_ioctl = kvm_vcpu_ioctl,
9a2bb7f4 1973 .mmap = kvm_vcpu_mmap,
bccf2150
AK
1974};
1975
1976/*
1977 * Allocates an inode for the vcpu.
1978 */
1979static int create_vcpu_fd(struct kvm_vcpu *vcpu)
1980{
1981 int fd, r;
1982 struct inode *inode;
1983 struct file *file;
1984
1985 atomic_inc(&vcpu->kvm->filp->f_count);
1986 inode = kvmfs_inode(&kvm_vcpu_fops);
1987 if (IS_ERR(inode)) {
1988 r = PTR_ERR(inode);
1989 goto out1;
1990 }
1991
1992 file = kvmfs_file(inode, vcpu);
1993 if (IS_ERR(file)) {
1994 r = PTR_ERR(file);
1995 goto out2;
1996 }
1997
1998 r = get_unused_fd();
1999 if (r < 0)
2000 goto out3;
2001 fd = r;
2002 fd_install(fd, file);
2003
2004 return fd;
2005
2006out3:
2007 fput(file);
2008out2:
2009 iput(inode);
2010out1:
2011 fput(vcpu->kvm->filp);
2012 return r;
2013}
2014
c5ea7660
AK
2015/*
2016 * Creates some virtual cpus. Good luck creating more than one.
2017 */
2018static int kvm_vm_ioctl_create_vcpu(struct kvm *kvm, int n)
2019{
2020 int r;
2021 struct kvm_vcpu *vcpu;
9a2bb7f4 2022 struct page *page;
c5ea7660
AK
2023
2024 r = -EINVAL;
2025 if (!valid_vcpu(n))
2026 goto out;
2027
2028 vcpu = &kvm->vcpus[n];
2029
2030 mutex_lock(&vcpu->mutex);
2031
2032 if (vcpu->vmcs) {
2033 mutex_unlock(&vcpu->mutex);
2034 return -EEXIST;
2035 }
2036
9a2bb7f4
AK
2037 page = alloc_page(GFP_KERNEL | __GFP_ZERO);
2038 r = -ENOMEM;
2039 if (!page)
2040 goto out_unlock;
2041 vcpu->run = page_address(page);
2042
c5ea7660
AK
2043 vcpu->host_fx_image = (char*)ALIGN((hva_t)vcpu->fx_buf,
2044 FX_IMAGE_ALIGN);
2045 vcpu->guest_fx_image = vcpu->host_fx_image + FX_IMAGE_SIZE;
2046
2047 r = kvm_arch_ops->vcpu_create(vcpu);
2048 if (r < 0)
2049 goto out_free_vcpus;
2050
2051 r = kvm_mmu_create(vcpu);
2052 if (r < 0)
2053 goto out_free_vcpus;
2054
2055 kvm_arch_ops->vcpu_load(vcpu);
2056 r = kvm_mmu_setup(vcpu);
2057 if (r >= 0)
2058 r = kvm_arch_ops->vcpu_setup(vcpu);
2059 vcpu_put(vcpu);
2060
2061 if (r < 0)
2062 goto out_free_vcpus;
2063
bccf2150
AK
2064 r = create_vcpu_fd(vcpu);
2065 if (r < 0)
2066 goto out_free_vcpus;
2067
2068 return r;
c5ea7660
AK
2069
2070out_free_vcpus:
2071 kvm_free_vcpu(vcpu);
9a2bb7f4 2072out_unlock:
c5ea7660
AK
2073 mutex_unlock(&vcpu->mutex);
2074out:
2075 return r;
2076}
2077
bccf2150
AK
2078static long kvm_vcpu_ioctl(struct file *filp,
2079 unsigned int ioctl, unsigned long arg)
6aa8b732 2080{
bccf2150 2081 struct kvm_vcpu *vcpu = filp->private_data;
2f366987 2082 void __user *argp = (void __user *)arg;
6aa8b732
AK
2083 int r = -EINVAL;
2084
2085 switch (ioctl) {
9a2bb7f4
AK
2086 case KVM_RUN:
2087 r = kvm_vcpu_ioctl_run(vcpu, vcpu->run);
6aa8b732 2088 break;
6aa8b732
AK
2089 case KVM_GET_REGS: {
2090 struct kvm_regs kvm_regs;
2091
bccf2150
AK
2092 memset(&kvm_regs, 0, sizeof kvm_regs);
2093 r = kvm_vcpu_ioctl_get_regs(vcpu, &kvm_regs);
6aa8b732
AK
2094 if (r)
2095 goto out;
2096 r = -EFAULT;
2f366987 2097 if (copy_to_user(argp, &kvm_regs, sizeof kvm_regs))
6aa8b732
AK
2098 goto out;
2099 r = 0;
2100 break;
2101 }
2102 case KVM_SET_REGS: {
2103 struct kvm_regs kvm_regs;
2104
2105 r = -EFAULT;
2f366987 2106 if (copy_from_user(&kvm_regs, argp, sizeof kvm_regs))
6aa8b732 2107 goto out;
bccf2150 2108 r = kvm_vcpu_ioctl_set_regs(vcpu, &kvm_regs);
6aa8b732
AK
2109 if (r)
2110 goto out;
2111 r = 0;
2112 break;
2113 }
2114 case KVM_GET_SREGS: {
2115 struct kvm_sregs kvm_sregs;
2116
bccf2150
AK
2117 memset(&kvm_sregs, 0, sizeof kvm_sregs);
2118 r = kvm_vcpu_ioctl_get_sregs(vcpu, &kvm_sregs);
6aa8b732
AK
2119 if (r)
2120 goto out;
2121 r = -EFAULT;
2f366987 2122 if (copy_to_user(argp, &kvm_sregs, sizeof kvm_sregs))
6aa8b732
AK
2123 goto out;
2124 r = 0;
2125 break;
2126 }
2127 case KVM_SET_SREGS: {
2128 struct kvm_sregs kvm_sregs;
2129
2130 r = -EFAULT;
2f366987 2131 if (copy_from_user(&kvm_sregs, argp, sizeof kvm_sregs))
6aa8b732 2132 goto out;
bccf2150 2133 r = kvm_vcpu_ioctl_set_sregs(vcpu, &kvm_sregs);
6aa8b732
AK
2134 if (r)
2135 goto out;
2136 r = 0;
2137 break;
2138 }
2139 case KVM_TRANSLATE: {
2140 struct kvm_translation tr;
2141
2142 r = -EFAULT;
2f366987 2143 if (copy_from_user(&tr, argp, sizeof tr))
6aa8b732 2144 goto out;
bccf2150 2145 r = kvm_vcpu_ioctl_translate(vcpu, &tr);
6aa8b732
AK
2146 if (r)
2147 goto out;
2148 r = -EFAULT;
2f366987 2149 if (copy_to_user(argp, &tr, sizeof tr))
6aa8b732
AK
2150 goto out;
2151 r = 0;
2152 break;
2153 }
2154 case KVM_INTERRUPT: {
2155 struct kvm_interrupt irq;
2156
2157 r = -EFAULT;
2f366987 2158 if (copy_from_user(&irq, argp, sizeof irq))
6aa8b732 2159 goto out;
bccf2150 2160 r = kvm_vcpu_ioctl_interrupt(vcpu, &irq);
6aa8b732
AK
2161 if (r)
2162 goto out;
2163 r = 0;
2164 break;
2165 }
2166 case KVM_DEBUG_GUEST: {
2167 struct kvm_debug_guest dbg;
2168
2169 r = -EFAULT;
2f366987 2170 if (copy_from_user(&dbg, argp, sizeof dbg))
6aa8b732 2171 goto out;
bccf2150 2172 r = kvm_vcpu_ioctl_debug_guest(vcpu, &dbg);
6aa8b732
AK
2173 if (r)
2174 goto out;
2175 r = 0;
2176 break;
2177 }
bccf2150
AK
2178 case KVM_GET_MSRS:
2179 r = msr_io(vcpu, argp, get_msr, 1);
2180 break;
2181 case KVM_SET_MSRS:
2182 r = msr_io(vcpu, argp, do_set_msr, 0);
2183 break;
2184 default:
2185 ;
2186 }
2187out:
2188 return r;
2189}
2190
2191static long kvm_vm_ioctl(struct file *filp,
2192 unsigned int ioctl, unsigned long arg)
2193{
2194 struct kvm *kvm = filp->private_data;
2195 void __user *argp = (void __user *)arg;
2196 int r = -EINVAL;
2197
2198 switch (ioctl) {
2199 case KVM_CREATE_VCPU:
2200 r = kvm_vm_ioctl_create_vcpu(kvm, arg);
2201 if (r < 0)
2202 goto out;
2203 break;
6aa8b732
AK
2204 case KVM_SET_MEMORY_REGION: {
2205 struct kvm_memory_region kvm_mem;
2206
2207 r = -EFAULT;
2f366987 2208 if (copy_from_user(&kvm_mem, argp, sizeof kvm_mem))
6aa8b732 2209 goto out;
2c6f5df9 2210 r = kvm_vm_ioctl_set_memory_region(kvm, &kvm_mem);
6aa8b732
AK
2211 if (r)
2212 goto out;
2213 break;
2214 }
2215 case KVM_GET_DIRTY_LOG: {
2216 struct kvm_dirty_log log;
2217
2218 r = -EFAULT;
2f366987 2219 if (copy_from_user(&log, argp, sizeof log))
6aa8b732 2220 goto out;
2c6f5df9 2221 r = kvm_vm_ioctl_get_dirty_log(kvm, &log);
6aa8b732
AK
2222 if (r)
2223 goto out;
2224 break;
2225 }
f17abe9a
AK
2226 default:
2227 ;
2228 }
2229out:
2230 return r;
2231}
2232
2233static struct page *kvm_vm_nopage(struct vm_area_struct *vma,
2234 unsigned long address,
2235 int *type)
2236{
2237 struct kvm *kvm = vma->vm_file->private_data;
2238 unsigned long pgoff;
2239 struct kvm_memory_slot *slot;
2240 struct page *page;
2241
2242 *type = VM_FAULT_MINOR;
2243 pgoff = ((address - vma->vm_start) >> PAGE_SHIFT) + vma->vm_pgoff;
2244 slot = gfn_to_memslot(kvm, pgoff);
2245 if (!slot)
2246 return NOPAGE_SIGBUS;
2247 page = gfn_to_page(slot, pgoff);
2248 if (!page)
2249 return NOPAGE_SIGBUS;
2250 get_page(page);
2251 return page;
2252}
2253
2254static struct vm_operations_struct kvm_vm_vm_ops = {
2255 .nopage = kvm_vm_nopage,
2256};
2257
2258static int kvm_vm_mmap(struct file *file, struct vm_area_struct *vma)
2259{
2260 vma->vm_ops = &kvm_vm_vm_ops;
2261 return 0;
2262}
2263
2264static struct file_operations kvm_vm_fops = {
2265 .release = kvm_vm_release,
2266 .unlocked_ioctl = kvm_vm_ioctl,
2267 .compat_ioctl = kvm_vm_ioctl,
2268 .mmap = kvm_vm_mmap,
2269};
2270
2271static int kvm_dev_ioctl_create_vm(void)
2272{
2273 int fd, r;
2274 struct inode *inode;
2275 struct file *file;
2276 struct kvm *kvm;
2277
2278 inode = kvmfs_inode(&kvm_vm_fops);
2279 if (IS_ERR(inode)) {
2280 r = PTR_ERR(inode);
2281 goto out1;
2282 }
2283
2284 kvm = kvm_create_vm();
2285 if (IS_ERR(kvm)) {
2286 r = PTR_ERR(kvm);
2287 goto out2;
2288 }
2289
2290 file = kvmfs_file(inode, kvm);
2291 if (IS_ERR(file)) {
2292 r = PTR_ERR(file);
2293 goto out3;
2294 }
bccf2150 2295 kvm->filp = file;
f17abe9a
AK
2296
2297 r = get_unused_fd();
2298 if (r < 0)
2299 goto out4;
2300 fd = r;
2301 fd_install(fd, file);
2302
2303 return fd;
2304
2305out4:
2306 fput(file);
2307out3:
2308 kvm_destroy_vm(kvm);
2309out2:
2310 iput(inode);
2311out1:
2312 return r;
2313}
2314
2315static long kvm_dev_ioctl(struct file *filp,
2316 unsigned int ioctl, unsigned long arg)
2317{
2318 void __user *argp = (void __user *)arg;
2319 int r = -EINVAL;
2320
2321 switch (ioctl) {
2322 case KVM_GET_API_VERSION:
2323 r = KVM_API_VERSION;
2324 break;
2325 case KVM_CREATE_VM:
2326 r = kvm_dev_ioctl_create_vm();
2327 break;
6aa8b732 2328 case KVM_GET_MSR_INDEX_LIST: {
2f366987 2329 struct kvm_msr_list __user *user_msr_list = argp;
6aa8b732
AK
2330 struct kvm_msr_list msr_list;
2331 unsigned n;
2332
2333 r = -EFAULT;
2334 if (copy_from_user(&msr_list, user_msr_list, sizeof msr_list))
2335 goto out;
2336 n = msr_list.nmsrs;
6f00e68f 2337 msr_list.nmsrs = num_msrs_to_save + ARRAY_SIZE(emulated_msrs);
6aa8b732
AK
2338 if (copy_to_user(user_msr_list, &msr_list, sizeof msr_list))
2339 goto out;
2340 r = -E2BIG;
bf591b24 2341 if (n < num_msrs_to_save)
6aa8b732
AK
2342 goto out;
2343 r = -EFAULT;
2344 if (copy_to_user(user_msr_list->indices, &msrs_to_save,
bf591b24 2345 num_msrs_to_save * sizeof(u32)))
6aa8b732 2346 goto out;
6f00e68f
AK
2347 if (copy_to_user(user_msr_list->indices
2348 + num_msrs_to_save * sizeof(u32),
2349 &emulated_msrs,
2350 ARRAY_SIZE(emulated_msrs) * sizeof(u32)))
2351 goto out;
6aa8b732 2352 r = 0;
cc1d8955 2353 break;
6aa8b732
AK
2354 }
2355 default:
2356 ;
2357 }
2358out:
2359 return r;
2360}
2361
6aa8b732
AK
2362static struct file_operations kvm_chardev_ops = {
2363 .open = kvm_dev_open,
2364 .release = kvm_dev_release,
2365 .unlocked_ioctl = kvm_dev_ioctl,
2366 .compat_ioctl = kvm_dev_ioctl,
6aa8b732
AK
2367};
2368
2369static struct miscdevice kvm_dev = {
bbe4432e 2370 KVM_MINOR,
6aa8b732
AK
2371 "kvm",
2372 &kvm_chardev_ops,
2373};
2374
2375static int kvm_reboot(struct notifier_block *notifier, unsigned long val,
2376 void *v)
2377{
2378 if (val == SYS_RESTART) {
2379 /*
2380 * Some (well, at least mine) BIOSes hang on reboot if
2381 * in vmx root mode.
2382 */
2383 printk(KERN_INFO "kvm: exiting hardware virtualization\n");
8b6d44c7 2384 on_each_cpu(kvm_arch_ops->hardware_disable, NULL, 0, 1);
6aa8b732
AK
2385 }
2386 return NOTIFY_OK;
2387}
2388
2389static struct notifier_block kvm_reboot_notifier = {
2390 .notifier_call = kvm_reboot,
2391 .priority = 0,
2392};
2393
774c47f1
AK
2394/*
2395 * Make sure that a cpu that is being hot-unplugged does not have any vcpus
2396 * cached on it.
2397 */
2398static void decache_vcpus_on_cpu(int cpu)
2399{
2400 struct kvm *vm;
2401 struct kvm_vcpu *vcpu;
2402 int i;
2403
2404 spin_lock(&kvm_lock);
2405 list_for_each_entry(vm, &vm_list, vm_list)
2406 for (i = 0; i < KVM_MAX_VCPUS; ++i) {
2407 vcpu = &vm->vcpus[i];
2408 /*
2409 * If the vcpu is locked, then it is running on some
2410 * other cpu and therefore it is not cached on the
2411 * cpu in question.
2412 *
2413 * If it's not locked, check the last cpu it executed
2414 * on.
2415 */
2416 if (mutex_trylock(&vcpu->mutex)) {
2417 if (vcpu->cpu == cpu) {
2418 kvm_arch_ops->vcpu_decache(vcpu);
2419 vcpu->cpu = -1;
2420 }
2421 mutex_unlock(&vcpu->mutex);
2422 }
2423 }
2424 spin_unlock(&kvm_lock);
2425}
2426
2427static int kvm_cpu_hotplug(struct notifier_block *notifier, unsigned long val,
2428 void *v)
2429{
2430 int cpu = (long)v;
2431
2432 switch (val) {
43934a38 2433 case CPU_DOWN_PREPARE:
774c47f1 2434 case CPU_UP_CANCELED:
43934a38
JK
2435 printk(KERN_INFO "kvm: disabling virtualization on CPU%d\n",
2436 cpu);
774c47f1
AK
2437 decache_vcpus_on_cpu(cpu);
2438 smp_call_function_single(cpu, kvm_arch_ops->hardware_disable,
2439 NULL, 0, 1);
2440 break;
43934a38
JK
2441 case CPU_ONLINE:
2442 printk(KERN_INFO "kvm: enabling virtualization on CPU%d\n",
2443 cpu);
774c47f1
AK
2444 smp_call_function_single(cpu, kvm_arch_ops->hardware_enable,
2445 NULL, 0, 1);
2446 break;
2447 }
2448 return NOTIFY_OK;
2449}
2450
2451static struct notifier_block kvm_cpu_notifier = {
2452 .notifier_call = kvm_cpu_hotplug,
2453 .priority = 20, /* must be > scheduler priority */
2454};
2455
6aa8b732
AK
2456static __init void kvm_init_debug(void)
2457{
2458 struct kvm_stats_debugfs_item *p;
2459
8b6d44c7 2460 debugfs_dir = debugfs_create_dir("kvm", NULL);
6aa8b732
AK
2461 for (p = debugfs_entries; p->name; ++p)
2462 p->dentry = debugfs_create_u32(p->name, 0444, debugfs_dir,
2463 p->data);
2464}
2465
2466static void kvm_exit_debug(void)
2467{
2468 struct kvm_stats_debugfs_item *p;
2469
2470 for (p = debugfs_entries; p->name; ++p)
2471 debugfs_remove(p->dentry);
2472 debugfs_remove(debugfs_dir);
2473}
2474
59ae6c6b
AK
2475static int kvm_suspend(struct sys_device *dev, pm_message_t state)
2476{
2477 decache_vcpus_on_cpu(raw_smp_processor_id());
19d1408d 2478 on_each_cpu(kvm_arch_ops->hardware_disable, NULL, 0, 1);
59ae6c6b
AK
2479 return 0;
2480}
2481
2482static int kvm_resume(struct sys_device *dev)
2483{
19d1408d 2484 on_each_cpu(kvm_arch_ops->hardware_enable, NULL, 0, 1);
59ae6c6b
AK
2485 return 0;
2486}
2487
2488static struct sysdev_class kvm_sysdev_class = {
2489 set_kset_name("kvm"),
2490 .suspend = kvm_suspend,
2491 .resume = kvm_resume,
2492};
2493
2494static struct sys_device kvm_sysdev = {
2495 .id = 0,
2496 .cls = &kvm_sysdev_class,
2497};
2498
6aa8b732
AK
2499hpa_t bad_page_address;
2500
37e29d90
AK
2501static int kvmfs_get_sb(struct file_system_type *fs_type, int flags,
2502 const char *dev_name, void *data, struct vfsmount *mnt)
2503{
e9cdb1e3 2504 return get_sb_pseudo(fs_type, "kvm:", NULL, KVMFS_SUPER_MAGIC, mnt);
37e29d90
AK
2505}
2506
2507static struct file_system_type kvm_fs_type = {
2508 .name = "kvmfs",
2509 .get_sb = kvmfs_get_sb,
2510 .kill_sb = kill_anon_super,
2511};
2512
6aa8b732
AK
2513int kvm_init_arch(struct kvm_arch_ops *ops, struct module *module)
2514{
2515 int r;
2516
09db28b8
YI
2517 if (kvm_arch_ops) {
2518 printk(KERN_ERR "kvm: already loaded the other module\n");
2519 return -EEXIST;
2520 }
2521
e097f35c 2522 if (!ops->cpu_has_kvm_support()) {
6aa8b732
AK
2523 printk(KERN_ERR "kvm: no hardware support\n");
2524 return -EOPNOTSUPP;
2525 }
e097f35c 2526 if (ops->disabled_by_bios()) {
6aa8b732
AK
2527 printk(KERN_ERR "kvm: disabled by bios\n");
2528 return -EOPNOTSUPP;
2529 }
2530
e097f35c
YI
2531 kvm_arch_ops = ops;
2532
6aa8b732
AK
2533 r = kvm_arch_ops->hardware_setup();
2534 if (r < 0)
ca45aaae 2535 goto out;
6aa8b732 2536
8b6d44c7 2537 on_each_cpu(kvm_arch_ops->hardware_enable, NULL, 0, 1);
774c47f1
AK
2538 r = register_cpu_notifier(&kvm_cpu_notifier);
2539 if (r)
2540 goto out_free_1;
6aa8b732
AK
2541 register_reboot_notifier(&kvm_reboot_notifier);
2542
59ae6c6b
AK
2543 r = sysdev_class_register(&kvm_sysdev_class);
2544 if (r)
2545 goto out_free_2;
2546
2547 r = sysdev_register(&kvm_sysdev);
2548 if (r)
2549 goto out_free_3;
2550
6aa8b732
AK
2551 kvm_chardev_ops.owner = module;
2552
2553 r = misc_register(&kvm_dev);
2554 if (r) {
2555 printk (KERN_ERR "kvm: misc device register failed\n");
2556 goto out_free;
2557 }
2558
2559 return r;
2560
2561out_free:
59ae6c6b
AK
2562 sysdev_unregister(&kvm_sysdev);
2563out_free_3:
2564 sysdev_class_unregister(&kvm_sysdev_class);
2565out_free_2:
6aa8b732 2566 unregister_reboot_notifier(&kvm_reboot_notifier);
774c47f1
AK
2567 unregister_cpu_notifier(&kvm_cpu_notifier);
2568out_free_1:
8b6d44c7 2569 on_each_cpu(kvm_arch_ops->hardware_disable, NULL, 0, 1);
6aa8b732 2570 kvm_arch_ops->hardware_unsetup();
ca45aaae
AK
2571out:
2572 kvm_arch_ops = NULL;
6aa8b732
AK
2573 return r;
2574}
2575
2576void kvm_exit_arch(void)
2577{
2578 misc_deregister(&kvm_dev);
59ae6c6b
AK
2579 sysdev_unregister(&kvm_sysdev);
2580 sysdev_class_unregister(&kvm_sysdev_class);
6aa8b732 2581 unregister_reboot_notifier(&kvm_reboot_notifier);
59ae6c6b 2582 unregister_cpu_notifier(&kvm_cpu_notifier);
8b6d44c7 2583 on_each_cpu(kvm_arch_ops->hardware_disable, NULL, 0, 1);
6aa8b732 2584 kvm_arch_ops->hardware_unsetup();
09db28b8 2585 kvm_arch_ops = NULL;
6aa8b732
AK
2586}
2587
2588static __init int kvm_init(void)
2589{
2590 static struct page *bad_page;
37e29d90
AK
2591 int r;
2592
2593 r = register_filesystem(&kvm_fs_type);
2594 if (r)
2595 goto out3;
6aa8b732 2596
37e29d90
AK
2597 kvmfs_mnt = kern_mount(&kvm_fs_type);
2598 r = PTR_ERR(kvmfs_mnt);
2599 if (IS_ERR(kvmfs_mnt))
2600 goto out2;
6aa8b732
AK
2601 kvm_init_debug();
2602
bf591b24
MR
2603 kvm_init_msr_list();
2604
6aa8b732
AK
2605 if ((bad_page = alloc_page(GFP_KERNEL)) == NULL) {
2606 r = -ENOMEM;
2607 goto out;
2608 }
2609
2610 bad_page_address = page_to_pfn(bad_page) << PAGE_SHIFT;
2611 memset(__va(bad_page_address), 0, PAGE_SIZE);
2612
58e690e6 2613 return 0;
6aa8b732
AK
2614
2615out:
2616 kvm_exit_debug();
37e29d90
AK
2617 mntput(kvmfs_mnt);
2618out2:
2619 unregister_filesystem(&kvm_fs_type);
2620out3:
6aa8b732
AK
2621 return r;
2622}
2623
2624static __exit void kvm_exit(void)
2625{
2626 kvm_exit_debug();
2627 __free_page(pfn_to_page(bad_page_address >> PAGE_SHIFT));
37e29d90
AK
2628 mntput(kvmfs_mnt);
2629 unregister_filesystem(&kvm_fs_type);
6aa8b732
AK
2630}
2631
2632module_init(kvm_init)
2633module_exit(kvm_exit)
2634
2635EXPORT_SYMBOL_GPL(kvm_init_arch);
2636EXPORT_SYMBOL_GPL(kvm_exit_arch);
This page took 0.365549 seconds and 5 git commands to generate.