KVM: SVM: Only save/restore MSRs when needed
[GitHub/mt8127/android_kernel_alcatel_ttab.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 53struct kvm_arch_ops *kvm_arch_ops;
1165f5fe
AK
54
55#define STAT_OFFSET(x) offsetof(struct kvm_vcpu, stat.x)
6aa8b732
AK
56
57static struct kvm_stats_debugfs_item {
58 const char *name;
1165f5fe 59 int offset;
6aa8b732
AK
60 struct dentry *dentry;
61} debugfs_entries[] = {
1165f5fe
AK
62 { "pf_fixed", STAT_OFFSET(pf_fixed) },
63 { "pf_guest", STAT_OFFSET(pf_guest) },
64 { "tlb_flush", STAT_OFFSET(tlb_flush) },
65 { "invlpg", STAT_OFFSET(invlpg) },
66 { "exits", STAT_OFFSET(exits) },
67 { "io_exits", STAT_OFFSET(io_exits) },
68 { "mmio_exits", STAT_OFFSET(mmio_exits) },
69 { "signal_exits", STAT_OFFSET(signal_exits) },
70 { "irq_window", STAT_OFFSET(irq_window_exits) },
71 { "halt_exits", STAT_OFFSET(halt_exits) },
72 { "request_irq", STAT_OFFSET(request_irq_exits) },
73 { "irq_exits", STAT_OFFSET(irq_exits) },
74 { 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
039576c0
AK
349static void free_pio_guest_pages(struct kvm_vcpu *vcpu)
350{
351 int i;
352
353 for (i = 0; i < 2; ++i)
354 if (vcpu->pio.guest_pages[i]) {
355 __free_page(vcpu->pio.guest_pages[i]);
356 vcpu->pio.guest_pages[i] = NULL;
357 }
358}
359
6aa8b732
AK
360static void kvm_free_vcpu(struct kvm_vcpu *vcpu)
361{
bccf2150 362 if (!vcpu->vmcs)
1e8ba6fb
IM
363 return;
364
bccf2150 365 vcpu_load(vcpu);
6aa8b732 366 kvm_mmu_destroy(vcpu);
08438475 367 vcpu_put(vcpu);
9ede74e0 368 kvm_arch_ops->vcpu_free(vcpu);
9a2bb7f4
AK
369 free_page((unsigned long)vcpu->run);
370 vcpu->run = NULL;
039576c0
AK
371 free_page((unsigned long)vcpu->pio_data);
372 vcpu->pio_data = NULL;
373 free_pio_guest_pages(vcpu);
6aa8b732
AK
374}
375
376static void kvm_free_vcpus(struct kvm *kvm)
377{
378 unsigned int i;
379
380 for (i = 0; i < KVM_MAX_VCPUS; ++i)
381 kvm_free_vcpu(&kvm->vcpus[i]);
382}
383
384static int kvm_dev_release(struct inode *inode, struct file *filp)
385{
f17abe9a
AK
386 return 0;
387}
6aa8b732 388
f17abe9a
AK
389static void kvm_destroy_vm(struct kvm *kvm)
390{
133de902
AK
391 spin_lock(&kvm_lock);
392 list_del(&kvm->vm_list);
393 spin_unlock(&kvm_lock);
6aa8b732
AK
394 kvm_free_vcpus(kvm);
395 kvm_free_physmem(kvm);
396 kfree(kvm);
f17abe9a
AK
397}
398
399static int kvm_vm_release(struct inode *inode, struct file *filp)
400{
401 struct kvm *kvm = filp->private_data;
402
403 kvm_destroy_vm(kvm);
6aa8b732
AK
404 return 0;
405}
406
407static void inject_gp(struct kvm_vcpu *vcpu)
408{
409 kvm_arch_ops->inject_gp(vcpu, 0);
410}
411
1342d353
AK
412/*
413 * Load the pae pdptrs. Return true is they are all valid.
414 */
415static int load_pdptrs(struct kvm_vcpu *vcpu, unsigned long cr3)
6aa8b732
AK
416{
417 gfn_t pdpt_gfn = cr3 >> PAGE_SHIFT;
1342d353 418 unsigned offset = ((cr3 & (PAGE_SIZE-1)) >> 5) << 2;
6aa8b732
AK
419 int i;
420 u64 pdpte;
421 u64 *pdpt;
1342d353 422 int ret;
954bbbc2 423 struct page *page;
6aa8b732
AK
424
425 spin_lock(&vcpu->kvm->lock);
954bbbc2
AK
426 page = gfn_to_page(vcpu->kvm, pdpt_gfn);
427 /* FIXME: !page - emulate? 0xff? */
428 pdpt = kmap_atomic(page, KM_USER0);
6aa8b732 429
1342d353 430 ret = 1;
6aa8b732
AK
431 for (i = 0; i < 4; ++i) {
432 pdpte = pdpt[offset + i];
1342d353
AK
433 if ((pdpte & 1) && (pdpte & 0xfffffff0000001e6ull)) {
434 ret = 0;
435 goto out;
436 }
6aa8b732
AK
437 }
438
1342d353
AK
439 for (i = 0; i < 4; ++i)
440 vcpu->pdptrs[i] = pdpt[offset + i];
441
442out:
6aa8b732
AK
443 kunmap_atomic(pdpt, KM_USER0);
444 spin_unlock(&vcpu->kvm->lock);
445
1342d353 446 return ret;
6aa8b732
AK
447}
448
449void set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0)
450{
451 if (cr0 & CR0_RESEVED_BITS) {
452 printk(KERN_DEBUG "set_cr0: 0x%lx #GP, reserved bits 0x%lx\n",
453 cr0, vcpu->cr0);
454 inject_gp(vcpu);
455 return;
456 }
457
458 if ((cr0 & CR0_NW_MASK) && !(cr0 & CR0_CD_MASK)) {
459 printk(KERN_DEBUG "set_cr0: #GP, CD == 0 && NW == 1\n");
460 inject_gp(vcpu);
461 return;
462 }
463
464 if ((cr0 & CR0_PG_MASK) && !(cr0 & CR0_PE_MASK)) {
465 printk(KERN_DEBUG "set_cr0: #GP, set PG flag "
466 "and a clear PE flag\n");
467 inject_gp(vcpu);
468 return;
469 }
470
471 if (!is_paging(vcpu) && (cr0 & CR0_PG_MASK)) {
05b3e0c2 472#ifdef CONFIG_X86_64
6aa8b732
AK
473 if ((vcpu->shadow_efer & EFER_LME)) {
474 int cs_db, cs_l;
475
476 if (!is_pae(vcpu)) {
477 printk(KERN_DEBUG "set_cr0: #GP, start paging "
478 "in long mode while PAE is disabled\n");
479 inject_gp(vcpu);
480 return;
481 }
482 kvm_arch_ops->get_cs_db_l_bits(vcpu, &cs_db, &cs_l);
483 if (cs_l) {
484 printk(KERN_DEBUG "set_cr0: #GP, start paging "
485 "in long mode while CS.L == 1\n");
486 inject_gp(vcpu);
487 return;
488
489 }
490 } else
491#endif
1342d353 492 if (is_pae(vcpu) && !load_pdptrs(vcpu, vcpu->cr3)) {
6aa8b732
AK
493 printk(KERN_DEBUG "set_cr0: #GP, pdptrs "
494 "reserved bits\n");
495 inject_gp(vcpu);
496 return;
497 }
498
499 }
500
501 kvm_arch_ops->set_cr0(vcpu, cr0);
502 vcpu->cr0 = cr0;
503
504 spin_lock(&vcpu->kvm->lock);
505 kvm_mmu_reset_context(vcpu);
506 spin_unlock(&vcpu->kvm->lock);
507 return;
508}
509EXPORT_SYMBOL_GPL(set_cr0);
510
511void lmsw(struct kvm_vcpu *vcpu, unsigned long msw)
512{
513 set_cr0(vcpu, (vcpu->cr0 & ~0x0ful) | (msw & 0x0f));
514}
515EXPORT_SYMBOL_GPL(lmsw);
516
517void set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)
518{
519 if (cr4 & CR4_RESEVED_BITS) {
520 printk(KERN_DEBUG "set_cr4: #GP, reserved bits\n");
521 inject_gp(vcpu);
522 return;
523 }
524
a9058ecd 525 if (is_long_mode(vcpu)) {
6aa8b732
AK
526 if (!(cr4 & CR4_PAE_MASK)) {
527 printk(KERN_DEBUG "set_cr4: #GP, clearing PAE while "
528 "in long mode\n");
529 inject_gp(vcpu);
530 return;
531 }
532 } else if (is_paging(vcpu) && !is_pae(vcpu) && (cr4 & CR4_PAE_MASK)
1342d353 533 && !load_pdptrs(vcpu, vcpu->cr3)) {
6aa8b732
AK
534 printk(KERN_DEBUG "set_cr4: #GP, pdptrs reserved bits\n");
535 inject_gp(vcpu);
536 }
537
538 if (cr4 & CR4_VMXE_MASK) {
539 printk(KERN_DEBUG "set_cr4: #GP, setting VMXE\n");
540 inject_gp(vcpu);
541 return;
542 }
543 kvm_arch_ops->set_cr4(vcpu, cr4);
544 spin_lock(&vcpu->kvm->lock);
545 kvm_mmu_reset_context(vcpu);
546 spin_unlock(&vcpu->kvm->lock);
547}
548EXPORT_SYMBOL_GPL(set_cr4);
549
550void set_cr3(struct kvm_vcpu *vcpu, unsigned long cr3)
551{
a9058ecd 552 if (is_long_mode(vcpu)) {
d27d4aca 553 if (cr3 & CR3_L_MODE_RESEVED_BITS) {
6aa8b732
AK
554 printk(KERN_DEBUG "set_cr3: #GP, reserved bits\n");
555 inject_gp(vcpu);
556 return;
557 }
558 } else {
559 if (cr3 & CR3_RESEVED_BITS) {
560 printk(KERN_DEBUG "set_cr3: #GP, reserved bits\n");
561 inject_gp(vcpu);
562 return;
563 }
564 if (is_paging(vcpu) && is_pae(vcpu) &&
1342d353 565 !load_pdptrs(vcpu, cr3)) {
6aa8b732
AK
566 printk(KERN_DEBUG "set_cr3: #GP, pdptrs "
567 "reserved bits\n");
568 inject_gp(vcpu);
569 return;
570 }
571 }
572
573 vcpu->cr3 = cr3;
574 spin_lock(&vcpu->kvm->lock);
d21225ee
IM
575 /*
576 * Does the new cr3 value map to physical memory? (Note, we
577 * catch an invalid cr3 even in real-mode, because it would
578 * cause trouble later on when we turn on paging anyway.)
579 *
580 * A real CPU would silently accept an invalid cr3 and would
581 * attempt to use it - with largely undefined (and often hard
582 * to debug) behavior on the guest side.
583 */
584 if (unlikely(!gfn_to_memslot(vcpu->kvm, cr3 >> PAGE_SHIFT)))
585 inject_gp(vcpu);
586 else
587 vcpu->mmu.new_cr3(vcpu);
6aa8b732
AK
588 spin_unlock(&vcpu->kvm->lock);
589}
590EXPORT_SYMBOL_GPL(set_cr3);
591
592void set_cr8(struct kvm_vcpu *vcpu, unsigned long cr8)
593{
594 if ( cr8 & CR8_RESEVED_BITS) {
595 printk(KERN_DEBUG "set_cr8: #GP, reserved bits 0x%lx\n", cr8);
596 inject_gp(vcpu);
597 return;
598 }
599 vcpu->cr8 = cr8;
600}
601EXPORT_SYMBOL_GPL(set_cr8);
602
603void fx_init(struct kvm_vcpu *vcpu)
604{
605 struct __attribute__ ((__packed__)) fx_image_s {
606 u16 control; //fcw
607 u16 status; //fsw
608 u16 tag; // ftw
609 u16 opcode; //fop
610 u64 ip; // fpu ip
611 u64 operand;// fpu dp
612 u32 mxcsr;
613 u32 mxcsr_mask;
614
615 } *fx_image;
616
617 fx_save(vcpu->host_fx_image);
618 fpu_init();
619 fx_save(vcpu->guest_fx_image);
620 fx_restore(vcpu->host_fx_image);
621
622 fx_image = (struct fx_image_s *)vcpu->guest_fx_image;
623 fx_image->mxcsr = 0x1f80;
624 memset(vcpu->guest_fx_image + sizeof(struct fx_image_s),
625 0, FX_IMAGE_SIZE - sizeof(struct fx_image_s));
626}
627EXPORT_SYMBOL_GPL(fx_init);
628
02b27c1f
UL
629static void do_remove_write_access(struct kvm_vcpu *vcpu, int slot)
630{
631 spin_lock(&vcpu->kvm->lock);
632 kvm_mmu_slot_remove_write_access(vcpu, slot);
633 spin_unlock(&vcpu->kvm->lock);
634}
635
6aa8b732
AK
636/*
637 * Allocate some memory and give it an address in the guest physical address
638 * space.
639 *
640 * Discontiguous memory is allowed, mostly for framebuffers.
641 */
2c6f5df9
AK
642static int kvm_vm_ioctl_set_memory_region(struct kvm *kvm,
643 struct kvm_memory_region *mem)
6aa8b732
AK
644{
645 int r;
646 gfn_t base_gfn;
647 unsigned long npages;
648 unsigned long i;
649 struct kvm_memory_slot *memslot;
650 struct kvm_memory_slot old, new;
651 int memory_config_version;
652
653 r = -EINVAL;
654 /* General sanity checks */
655 if (mem->memory_size & (PAGE_SIZE - 1))
656 goto out;
657 if (mem->guest_phys_addr & (PAGE_SIZE - 1))
658 goto out;
659 if (mem->slot >= KVM_MEMORY_SLOTS)
660 goto out;
661 if (mem->guest_phys_addr + mem->memory_size < mem->guest_phys_addr)
662 goto out;
663
664 memslot = &kvm->memslots[mem->slot];
665 base_gfn = mem->guest_phys_addr >> PAGE_SHIFT;
666 npages = mem->memory_size >> PAGE_SHIFT;
667
668 if (!npages)
669 mem->flags &= ~KVM_MEM_LOG_DIRTY_PAGES;
670
671raced:
672 spin_lock(&kvm->lock);
673
674 memory_config_version = kvm->memory_config_version;
675 new = old = *memslot;
676
677 new.base_gfn = base_gfn;
678 new.npages = npages;
679 new.flags = mem->flags;
680
681 /* Disallow changing a memory slot's size. */
682 r = -EINVAL;
683 if (npages && old.npages && npages != old.npages)
684 goto out_unlock;
685
686 /* Check for overlaps */
687 r = -EEXIST;
688 for (i = 0; i < KVM_MEMORY_SLOTS; ++i) {
689 struct kvm_memory_slot *s = &kvm->memslots[i];
690
691 if (s == memslot)
692 continue;
693 if (!((base_gfn + npages <= s->base_gfn) ||
694 (base_gfn >= s->base_gfn + s->npages)))
695 goto out_unlock;
696 }
697 /*
698 * Do memory allocations outside lock. memory_config_version will
699 * detect any races.
700 */
701 spin_unlock(&kvm->lock);
702
703 /* Deallocate if slot is being removed */
704 if (!npages)
8b6d44c7 705 new.phys_mem = NULL;
6aa8b732
AK
706
707 /* Free page dirty bitmap if unneeded */
708 if (!(new.flags & KVM_MEM_LOG_DIRTY_PAGES))
8b6d44c7 709 new.dirty_bitmap = NULL;
6aa8b732
AK
710
711 r = -ENOMEM;
712
713 /* Allocate if a slot is being created */
714 if (npages && !new.phys_mem) {
715 new.phys_mem = vmalloc(npages * sizeof(struct page *));
716
717 if (!new.phys_mem)
718 goto out_free;
719
720 memset(new.phys_mem, 0, npages * sizeof(struct page *));
721 for (i = 0; i < npages; ++i) {
722 new.phys_mem[i] = alloc_page(GFP_HIGHUSER
723 | __GFP_ZERO);
724 if (!new.phys_mem[i])
725 goto out_free;
5972e953 726 set_page_private(new.phys_mem[i],0);
6aa8b732
AK
727 }
728 }
729
730 /* Allocate page dirty bitmap if needed */
731 if ((new.flags & KVM_MEM_LOG_DIRTY_PAGES) && !new.dirty_bitmap) {
732 unsigned dirty_bytes = ALIGN(npages, BITS_PER_LONG) / 8;
733
734 new.dirty_bitmap = vmalloc(dirty_bytes);
735 if (!new.dirty_bitmap)
736 goto out_free;
737 memset(new.dirty_bitmap, 0, dirty_bytes);
738 }
739
740 spin_lock(&kvm->lock);
741
742 if (memory_config_version != kvm->memory_config_version) {
743 spin_unlock(&kvm->lock);
744 kvm_free_physmem_slot(&new, &old);
745 goto raced;
746 }
747
748 r = -EAGAIN;
749 if (kvm->busy)
750 goto out_unlock;
751
752 if (mem->slot >= kvm->nmemslots)
753 kvm->nmemslots = mem->slot + 1;
754
755 *memslot = new;
756 ++kvm->memory_config_version;
757
758 spin_unlock(&kvm->lock);
759
760 for (i = 0; i < KVM_MAX_VCPUS; ++i) {
761 struct kvm_vcpu *vcpu;
762
bccf2150 763 vcpu = vcpu_load_slot(kvm, i);
6aa8b732
AK
764 if (!vcpu)
765 continue;
ff990d59
UL
766 if (new.flags & KVM_MEM_LOG_DIRTY_PAGES)
767 do_remove_write_access(vcpu, mem->slot);
6aa8b732
AK
768 kvm_mmu_reset_context(vcpu);
769 vcpu_put(vcpu);
770 }
771
772 kvm_free_physmem_slot(&old, &new);
773 return 0;
774
775out_unlock:
776 spin_unlock(&kvm->lock);
777out_free:
778 kvm_free_physmem_slot(&new, &old);
779out:
780 return r;
781}
782
783/*
784 * Get (and clear) the dirty memory log for a memory slot.
785 */
2c6f5df9
AK
786static int kvm_vm_ioctl_get_dirty_log(struct kvm *kvm,
787 struct kvm_dirty_log *log)
6aa8b732
AK
788{
789 struct kvm_memory_slot *memslot;
790 int r, i;
791 int n;
714b93da 792 int cleared;
6aa8b732
AK
793 unsigned long any = 0;
794
795 spin_lock(&kvm->lock);
796
797 /*
798 * Prevent changes to guest memory configuration even while the lock
799 * is not taken.
800 */
801 ++kvm->busy;
802 spin_unlock(&kvm->lock);
803 r = -EINVAL;
804 if (log->slot >= KVM_MEMORY_SLOTS)
805 goto out;
806
807 memslot = &kvm->memslots[log->slot];
808 r = -ENOENT;
809 if (!memslot->dirty_bitmap)
810 goto out;
811
cd1a4a98 812 n = ALIGN(memslot->npages, BITS_PER_LONG) / 8;
6aa8b732 813
cd1a4a98 814 for (i = 0; !any && i < n/sizeof(long); ++i)
6aa8b732
AK
815 any = memslot->dirty_bitmap[i];
816
817 r = -EFAULT;
818 if (copy_to_user(log->dirty_bitmap, memslot->dirty_bitmap, n))
819 goto out;
820
6aa8b732 821 if (any) {
714b93da 822 cleared = 0;
6aa8b732 823 for (i = 0; i < KVM_MAX_VCPUS; ++i) {
bccf2150 824 struct kvm_vcpu *vcpu;
6aa8b732 825
bccf2150 826 vcpu = vcpu_load_slot(kvm, i);
6aa8b732
AK
827 if (!vcpu)
828 continue;
714b93da
AK
829 if (!cleared) {
830 do_remove_write_access(vcpu, log->slot);
831 memset(memslot->dirty_bitmap, 0, n);
832 cleared = 1;
833 }
6aa8b732
AK
834 kvm_arch_ops->tlb_flush(vcpu);
835 vcpu_put(vcpu);
836 }
837 }
838
839 r = 0;
840
841out:
842 spin_lock(&kvm->lock);
843 --kvm->busy;
844 spin_unlock(&kvm->lock);
845 return r;
846}
847
e8207547
AK
848/*
849 * Set a new alias region. Aliases map a portion of physical memory into
850 * another portion. This is useful for memory windows, for example the PC
851 * VGA region.
852 */
853static int kvm_vm_ioctl_set_memory_alias(struct kvm *kvm,
854 struct kvm_memory_alias *alias)
855{
856 int r, n;
857 struct kvm_mem_alias *p;
858
859 r = -EINVAL;
860 /* General sanity checks */
861 if (alias->memory_size & (PAGE_SIZE - 1))
862 goto out;
863 if (alias->guest_phys_addr & (PAGE_SIZE - 1))
864 goto out;
865 if (alias->slot >= KVM_ALIAS_SLOTS)
866 goto out;
867 if (alias->guest_phys_addr + alias->memory_size
868 < alias->guest_phys_addr)
869 goto out;
870 if (alias->target_phys_addr + alias->memory_size
871 < alias->target_phys_addr)
872 goto out;
873
874 spin_lock(&kvm->lock);
875
876 p = &kvm->aliases[alias->slot];
877 p->base_gfn = alias->guest_phys_addr >> PAGE_SHIFT;
878 p->npages = alias->memory_size >> PAGE_SHIFT;
879 p->target_gfn = alias->target_phys_addr >> PAGE_SHIFT;
880
881 for (n = KVM_ALIAS_SLOTS; n > 0; --n)
882 if (kvm->aliases[n - 1].npages)
883 break;
884 kvm->naliases = n;
885
886 spin_unlock(&kvm->lock);
887
888 vcpu_load(&kvm->vcpus[0]);
889 spin_lock(&kvm->lock);
890 kvm_mmu_zap_all(&kvm->vcpus[0]);
891 spin_unlock(&kvm->lock);
892 vcpu_put(&kvm->vcpus[0]);
893
894 return 0;
895
896out:
897 return r;
898}
899
900static gfn_t unalias_gfn(struct kvm *kvm, gfn_t gfn)
901{
902 int i;
903 struct kvm_mem_alias *alias;
904
905 for (i = 0; i < kvm->naliases; ++i) {
906 alias = &kvm->aliases[i];
907 if (gfn >= alias->base_gfn
908 && gfn < alias->base_gfn + alias->npages)
909 return alias->target_gfn + gfn - alias->base_gfn;
910 }
911 return gfn;
912}
913
914static struct kvm_memory_slot *__gfn_to_memslot(struct kvm *kvm, gfn_t gfn)
6aa8b732
AK
915{
916 int i;
917
918 for (i = 0; i < kvm->nmemslots; ++i) {
919 struct kvm_memory_slot *memslot = &kvm->memslots[i];
920
921 if (gfn >= memslot->base_gfn
922 && gfn < memslot->base_gfn + memslot->npages)
923 return memslot;
924 }
8b6d44c7 925 return NULL;
6aa8b732 926}
e8207547
AK
927
928struct kvm_memory_slot *gfn_to_memslot(struct kvm *kvm, gfn_t gfn)
929{
930 gfn = unalias_gfn(kvm, gfn);
931 return __gfn_to_memslot(kvm, gfn);
932}
6aa8b732 933
954bbbc2
AK
934struct page *gfn_to_page(struct kvm *kvm, gfn_t gfn)
935{
936 struct kvm_memory_slot *slot;
937
e8207547
AK
938 gfn = unalias_gfn(kvm, gfn);
939 slot = __gfn_to_memslot(kvm, gfn);
954bbbc2
AK
940 if (!slot)
941 return NULL;
942 return slot->phys_mem[gfn - slot->base_gfn];
943}
944EXPORT_SYMBOL_GPL(gfn_to_page);
945
6aa8b732
AK
946void mark_page_dirty(struct kvm *kvm, gfn_t gfn)
947{
948 int i;
8b6d44c7 949 struct kvm_memory_slot *memslot = NULL;
6aa8b732
AK
950 unsigned long rel_gfn;
951
952 for (i = 0; i < kvm->nmemslots; ++i) {
953 memslot = &kvm->memslots[i];
954
955 if (gfn >= memslot->base_gfn
956 && gfn < memslot->base_gfn + memslot->npages) {
957
958 if (!memslot || !memslot->dirty_bitmap)
959 return;
960
961 rel_gfn = gfn - memslot->base_gfn;
962
963 /* avoid RMW */
964 if (!test_bit(rel_gfn, memslot->dirty_bitmap))
965 set_bit(rel_gfn, memslot->dirty_bitmap);
966 return;
967 }
968 }
969}
970
971static int emulator_read_std(unsigned long addr,
4c690a1e 972 void *val,
6aa8b732
AK
973 unsigned int bytes,
974 struct x86_emulate_ctxt *ctxt)
975{
976 struct kvm_vcpu *vcpu = ctxt->vcpu;
977 void *data = val;
978
979 while (bytes) {
980 gpa_t gpa = vcpu->mmu.gva_to_gpa(vcpu, addr);
981 unsigned offset = addr & (PAGE_SIZE-1);
982 unsigned tocopy = min(bytes, (unsigned)PAGE_SIZE - offset);
983 unsigned long pfn;
954bbbc2
AK
984 struct page *page;
985 void *page_virt;
6aa8b732
AK
986
987 if (gpa == UNMAPPED_GVA)
988 return X86EMUL_PROPAGATE_FAULT;
989 pfn = gpa >> PAGE_SHIFT;
954bbbc2
AK
990 page = gfn_to_page(vcpu->kvm, pfn);
991 if (!page)
6aa8b732 992 return X86EMUL_UNHANDLEABLE;
954bbbc2 993 page_virt = kmap_atomic(page, KM_USER0);
6aa8b732 994
954bbbc2 995 memcpy(data, page_virt + offset, tocopy);
6aa8b732 996
954bbbc2 997 kunmap_atomic(page_virt, KM_USER0);
6aa8b732
AK
998
999 bytes -= tocopy;
1000 data += tocopy;
1001 addr += tocopy;
1002 }
1003
1004 return X86EMUL_CONTINUE;
1005}
1006
1007static int emulator_write_std(unsigned long addr,
4c690a1e 1008 const void *val,
6aa8b732
AK
1009 unsigned int bytes,
1010 struct x86_emulate_ctxt *ctxt)
1011{
1012 printk(KERN_ERR "emulator_write_std: addr %lx n %d\n",
1013 addr, bytes);
1014 return X86EMUL_UNHANDLEABLE;
1015}
1016
1017static int emulator_read_emulated(unsigned long addr,
4c690a1e 1018 void *val,
6aa8b732
AK
1019 unsigned int bytes,
1020 struct x86_emulate_ctxt *ctxt)
1021{
1022 struct kvm_vcpu *vcpu = ctxt->vcpu;
1023
1024 if (vcpu->mmio_read_completed) {
1025 memcpy(val, vcpu->mmio_data, bytes);
1026 vcpu->mmio_read_completed = 0;
1027 return X86EMUL_CONTINUE;
1028 } else if (emulator_read_std(addr, val, bytes, ctxt)
1029 == X86EMUL_CONTINUE)
1030 return X86EMUL_CONTINUE;
1031 else {
1032 gpa_t gpa = vcpu->mmu.gva_to_gpa(vcpu, addr);
d27d4aca 1033
6aa8b732 1034 if (gpa == UNMAPPED_GVA)
d27d4aca 1035 return X86EMUL_PROPAGATE_FAULT;
6aa8b732
AK
1036 vcpu->mmio_needed = 1;
1037 vcpu->mmio_phys_addr = gpa;
1038 vcpu->mmio_size = bytes;
1039 vcpu->mmio_is_write = 0;
1040
1041 return X86EMUL_UNHANDLEABLE;
1042 }
1043}
1044
da4a00f0 1045static int emulator_write_phys(struct kvm_vcpu *vcpu, gpa_t gpa,
4c690a1e 1046 const void *val, int bytes)
da4a00f0 1047{
da4a00f0
AK
1048 struct page *page;
1049 void *virt;
1050
1051 if (((gpa + bytes - 1) >> PAGE_SHIFT) != (gpa >> PAGE_SHIFT))
1052 return 0;
954bbbc2
AK
1053 page = gfn_to_page(vcpu->kvm, gpa >> PAGE_SHIFT);
1054 if (!page)
da4a00f0 1055 return 0;
da4a00f0 1056 kvm_mmu_pre_write(vcpu, gpa, bytes);
ab51a434 1057 mark_page_dirty(vcpu->kvm, gpa >> PAGE_SHIFT);
da4a00f0 1058 virt = kmap_atomic(page, KM_USER0);
4c690a1e 1059 memcpy(virt + offset_in_page(gpa), val, bytes);
da4a00f0
AK
1060 kunmap_atomic(virt, KM_USER0);
1061 kvm_mmu_post_write(vcpu, gpa, bytes);
1062 return 1;
1063}
1064
6aa8b732 1065static int emulator_write_emulated(unsigned long addr,
4c690a1e 1066 const void *val,
6aa8b732
AK
1067 unsigned int bytes,
1068 struct x86_emulate_ctxt *ctxt)
1069{
1070 struct kvm_vcpu *vcpu = ctxt->vcpu;
1071 gpa_t gpa = vcpu->mmu.gva_to_gpa(vcpu, addr);
1072
c9047f53
AK
1073 if (gpa == UNMAPPED_GVA) {
1074 kvm_arch_ops->inject_page_fault(vcpu, addr, 2);
6aa8b732 1075 return X86EMUL_PROPAGATE_FAULT;
c9047f53 1076 }
6aa8b732 1077
da4a00f0
AK
1078 if (emulator_write_phys(vcpu, gpa, val, bytes))
1079 return X86EMUL_CONTINUE;
1080
6aa8b732
AK
1081 vcpu->mmio_needed = 1;
1082 vcpu->mmio_phys_addr = gpa;
1083 vcpu->mmio_size = bytes;
1084 vcpu->mmio_is_write = 1;
4c690a1e 1085 memcpy(vcpu->mmio_data, val, bytes);
6aa8b732
AK
1086
1087 return X86EMUL_CONTINUE;
1088}
1089
1090static int emulator_cmpxchg_emulated(unsigned long addr,
4c690a1e
AK
1091 const void *old,
1092 const void *new,
6aa8b732
AK
1093 unsigned int bytes,
1094 struct x86_emulate_ctxt *ctxt)
1095{
1096 static int reported;
1097
1098 if (!reported) {
1099 reported = 1;
1100 printk(KERN_WARNING "kvm: emulating exchange as write\n");
1101 }
1102 return emulator_write_emulated(addr, new, bytes, ctxt);
1103}
1104
1105static unsigned long get_segment_base(struct kvm_vcpu *vcpu, int seg)
1106{
1107 return kvm_arch_ops->get_segment_base(vcpu, seg);
1108}
1109
1110int emulate_invlpg(struct kvm_vcpu *vcpu, gva_t address)
1111{
6aa8b732
AK
1112 return X86EMUL_CONTINUE;
1113}
1114
1115int emulate_clts(struct kvm_vcpu *vcpu)
1116{
399badf3 1117 unsigned long cr0;
6aa8b732 1118
399badf3 1119 cr0 = vcpu->cr0 & ~CR0_TS_MASK;
6aa8b732
AK
1120 kvm_arch_ops->set_cr0(vcpu, cr0);
1121 return X86EMUL_CONTINUE;
1122}
1123
1124int emulator_get_dr(struct x86_emulate_ctxt* ctxt, int dr, unsigned long *dest)
1125{
1126 struct kvm_vcpu *vcpu = ctxt->vcpu;
1127
1128 switch (dr) {
1129 case 0 ... 3:
1130 *dest = kvm_arch_ops->get_dr(vcpu, dr);
1131 return X86EMUL_CONTINUE;
1132 default:
1133 printk(KERN_DEBUG "%s: unexpected dr %u\n",
1134 __FUNCTION__, dr);
1135 return X86EMUL_UNHANDLEABLE;
1136 }
1137}
1138
1139int emulator_set_dr(struct x86_emulate_ctxt *ctxt, int dr, unsigned long value)
1140{
1141 unsigned long mask = (ctxt->mode == X86EMUL_MODE_PROT64) ? ~0ULL : ~0U;
1142 int exception;
1143
1144 kvm_arch_ops->set_dr(ctxt->vcpu, dr, value & mask, &exception);
1145 if (exception) {
1146 /* FIXME: better handling */
1147 return X86EMUL_UNHANDLEABLE;
1148 }
1149 return X86EMUL_CONTINUE;
1150}
1151
1152static void report_emulation_failure(struct x86_emulate_ctxt *ctxt)
1153{
1154 static int reported;
1155 u8 opcodes[4];
1156 unsigned long rip = ctxt->vcpu->rip;
1157 unsigned long rip_linear;
1158
1159 rip_linear = rip + get_segment_base(ctxt->vcpu, VCPU_SREG_CS);
1160
1161 if (reported)
1162 return;
1163
1164 emulator_read_std(rip_linear, (void *)opcodes, 4, ctxt);
1165
1166 printk(KERN_ERR "emulation failed but !mmio_needed?"
1167 " rip %lx %02x %02x %02x %02x\n",
1168 rip, opcodes[0], opcodes[1], opcodes[2], opcodes[3]);
1169 reported = 1;
1170}
1171
1172struct x86_emulate_ops emulate_ops = {
1173 .read_std = emulator_read_std,
1174 .write_std = emulator_write_std,
1175 .read_emulated = emulator_read_emulated,
1176 .write_emulated = emulator_write_emulated,
1177 .cmpxchg_emulated = emulator_cmpxchg_emulated,
1178};
1179
1180int emulate_instruction(struct kvm_vcpu *vcpu,
1181 struct kvm_run *run,
1182 unsigned long cr2,
1183 u16 error_code)
1184{
1185 struct x86_emulate_ctxt emulate_ctxt;
1186 int r;
1187 int cs_db, cs_l;
1188
1189 kvm_arch_ops->cache_regs(vcpu);
1190
1191 kvm_arch_ops->get_cs_db_l_bits(vcpu, &cs_db, &cs_l);
1192
1193 emulate_ctxt.vcpu = vcpu;
1194 emulate_ctxt.eflags = kvm_arch_ops->get_rflags(vcpu);
1195 emulate_ctxt.cr2 = cr2;
1196 emulate_ctxt.mode = (emulate_ctxt.eflags & X86_EFLAGS_VM)
1197 ? X86EMUL_MODE_REAL : cs_l
1198 ? X86EMUL_MODE_PROT64 : cs_db
1199 ? X86EMUL_MODE_PROT32 : X86EMUL_MODE_PROT16;
1200
1201 if (emulate_ctxt.mode == X86EMUL_MODE_PROT64) {
1202 emulate_ctxt.cs_base = 0;
1203 emulate_ctxt.ds_base = 0;
1204 emulate_ctxt.es_base = 0;
1205 emulate_ctxt.ss_base = 0;
1206 } else {
1207 emulate_ctxt.cs_base = get_segment_base(vcpu, VCPU_SREG_CS);
1208 emulate_ctxt.ds_base = get_segment_base(vcpu, VCPU_SREG_DS);
1209 emulate_ctxt.es_base = get_segment_base(vcpu, VCPU_SREG_ES);
1210 emulate_ctxt.ss_base = get_segment_base(vcpu, VCPU_SREG_SS);
1211 }
1212
1213 emulate_ctxt.gs_base = get_segment_base(vcpu, VCPU_SREG_GS);
1214 emulate_ctxt.fs_base = get_segment_base(vcpu, VCPU_SREG_FS);
1215
1216 vcpu->mmio_is_write = 0;
1217 r = x86_emulate_memop(&emulate_ctxt, &emulate_ops);
1218
1219 if ((r || vcpu->mmio_is_write) && run) {
1220 run->mmio.phys_addr = vcpu->mmio_phys_addr;
1221 memcpy(run->mmio.data, vcpu->mmio_data, 8);
1222 run->mmio.len = vcpu->mmio_size;
1223 run->mmio.is_write = vcpu->mmio_is_write;
1224 }
1225
1226 if (r) {
a436036b
AK
1227 if (kvm_mmu_unprotect_page_virt(vcpu, cr2))
1228 return EMULATE_DONE;
6aa8b732
AK
1229 if (!vcpu->mmio_needed) {
1230 report_emulation_failure(&emulate_ctxt);
1231 return EMULATE_FAIL;
1232 }
1233 return EMULATE_DO_MMIO;
1234 }
1235
1236 kvm_arch_ops->decache_regs(vcpu);
1237 kvm_arch_ops->set_rflags(vcpu, emulate_ctxt.eflags);
1238
1239 if (vcpu->mmio_is_write)
1240 return EMULATE_DO_MMIO;
1241
1242 return EMULATE_DONE;
1243}
1244EXPORT_SYMBOL_GPL(emulate_instruction);
1245
270fd9b9
AK
1246int kvm_hypercall(struct kvm_vcpu *vcpu, struct kvm_run *run)
1247{
1248 unsigned long nr, a0, a1, a2, a3, a4, a5, ret;
1249
9b22bf57 1250 kvm_arch_ops->cache_regs(vcpu);
270fd9b9
AK
1251 ret = -KVM_EINVAL;
1252#ifdef CONFIG_X86_64
1253 if (is_long_mode(vcpu)) {
1254 nr = vcpu->regs[VCPU_REGS_RAX];
1255 a0 = vcpu->regs[VCPU_REGS_RDI];
1256 a1 = vcpu->regs[VCPU_REGS_RSI];
1257 a2 = vcpu->regs[VCPU_REGS_RDX];
1258 a3 = vcpu->regs[VCPU_REGS_RCX];
1259 a4 = vcpu->regs[VCPU_REGS_R8];
1260 a5 = vcpu->regs[VCPU_REGS_R9];
1261 } else
1262#endif
1263 {
1264 nr = vcpu->regs[VCPU_REGS_RBX] & -1u;
1265 a0 = vcpu->regs[VCPU_REGS_RAX] & -1u;
1266 a1 = vcpu->regs[VCPU_REGS_RCX] & -1u;
1267 a2 = vcpu->regs[VCPU_REGS_RDX] & -1u;
1268 a3 = vcpu->regs[VCPU_REGS_RSI] & -1u;
1269 a4 = vcpu->regs[VCPU_REGS_RDI] & -1u;
1270 a5 = vcpu->regs[VCPU_REGS_RBP] & -1u;
1271 }
1272 switch (nr) {
1273 default:
b4e63f56
AK
1274 run->hypercall.args[0] = a0;
1275 run->hypercall.args[1] = a1;
1276 run->hypercall.args[2] = a2;
1277 run->hypercall.args[3] = a3;
1278 run->hypercall.args[4] = a4;
1279 run->hypercall.args[5] = a5;
1280 run->hypercall.ret = ret;
1281 run->hypercall.longmode = is_long_mode(vcpu);
1282 kvm_arch_ops->decache_regs(vcpu);
1283 return 0;
270fd9b9
AK
1284 }
1285 vcpu->regs[VCPU_REGS_RAX] = ret;
9b22bf57 1286 kvm_arch_ops->decache_regs(vcpu);
270fd9b9
AK
1287 return 1;
1288}
1289EXPORT_SYMBOL_GPL(kvm_hypercall);
1290
6aa8b732
AK
1291static u64 mk_cr_64(u64 curr_cr, u32 new_val)
1292{
1293 return (curr_cr & ~((1ULL << 32) - 1)) | new_val;
1294}
1295
1296void realmode_lgdt(struct kvm_vcpu *vcpu, u16 limit, unsigned long base)
1297{
1298 struct descriptor_table dt = { limit, base };
1299
1300 kvm_arch_ops->set_gdt(vcpu, &dt);
1301}
1302
1303void realmode_lidt(struct kvm_vcpu *vcpu, u16 limit, unsigned long base)
1304{
1305 struct descriptor_table dt = { limit, base };
1306
1307 kvm_arch_ops->set_idt(vcpu, &dt);
1308}
1309
1310void realmode_lmsw(struct kvm_vcpu *vcpu, unsigned long msw,
1311 unsigned long *rflags)
1312{
1313 lmsw(vcpu, msw);
1314 *rflags = kvm_arch_ops->get_rflags(vcpu);
1315}
1316
1317unsigned long realmode_get_cr(struct kvm_vcpu *vcpu, int cr)
1318{
25c4c276 1319 kvm_arch_ops->decache_cr4_guest_bits(vcpu);
6aa8b732
AK
1320 switch (cr) {
1321 case 0:
1322 return vcpu->cr0;
1323 case 2:
1324 return vcpu->cr2;
1325 case 3:
1326 return vcpu->cr3;
1327 case 4:
1328 return vcpu->cr4;
1329 default:
1330 vcpu_printf(vcpu, "%s: unexpected cr %u\n", __FUNCTION__, cr);
1331 return 0;
1332 }
1333}
1334
1335void realmode_set_cr(struct kvm_vcpu *vcpu, int cr, unsigned long val,
1336 unsigned long *rflags)
1337{
1338 switch (cr) {
1339 case 0:
1340 set_cr0(vcpu, mk_cr_64(vcpu->cr0, val));
1341 *rflags = kvm_arch_ops->get_rflags(vcpu);
1342 break;
1343 case 2:
1344 vcpu->cr2 = val;
1345 break;
1346 case 3:
1347 set_cr3(vcpu, val);
1348 break;
1349 case 4:
1350 set_cr4(vcpu, mk_cr_64(vcpu->cr4, val));
1351 break;
1352 default:
1353 vcpu_printf(vcpu, "%s: unexpected cr %u\n", __FUNCTION__, cr);
1354 }
1355}
1356
102d8325
IM
1357/*
1358 * Register the para guest with the host:
1359 */
1360static int vcpu_register_para(struct kvm_vcpu *vcpu, gpa_t para_state_gpa)
1361{
1362 struct kvm_vcpu_para_state *para_state;
1363 hpa_t para_state_hpa, hypercall_hpa;
1364 struct page *para_state_page;
1365 unsigned char *hypercall;
1366 gpa_t hypercall_gpa;
1367
1368 printk(KERN_DEBUG "kvm: guest trying to enter paravirtual mode\n");
1369 printk(KERN_DEBUG ".... para_state_gpa: %08Lx\n", para_state_gpa);
1370
1371 /*
1372 * Needs to be page aligned:
1373 */
1374 if (para_state_gpa != PAGE_ALIGN(para_state_gpa))
1375 goto err_gp;
1376
1377 para_state_hpa = gpa_to_hpa(vcpu, para_state_gpa);
1378 printk(KERN_DEBUG ".... para_state_hpa: %08Lx\n", para_state_hpa);
1379 if (is_error_hpa(para_state_hpa))
1380 goto err_gp;
1381
ab51a434 1382 mark_page_dirty(vcpu->kvm, para_state_gpa >> PAGE_SHIFT);
102d8325
IM
1383 para_state_page = pfn_to_page(para_state_hpa >> PAGE_SHIFT);
1384 para_state = kmap_atomic(para_state_page, KM_USER0);
1385
1386 printk(KERN_DEBUG ".... guest version: %d\n", para_state->guest_version);
1387 printk(KERN_DEBUG ".... size: %d\n", para_state->size);
1388
1389 para_state->host_version = KVM_PARA_API_VERSION;
1390 /*
1391 * We cannot support guests that try to register themselves
1392 * with a newer API version than the host supports:
1393 */
1394 if (para_state->guest_version > KVM_PARA_API_VERSION) {
1395 para_state->ret = -KVM_EINVAL;
1396 goto err_kunmap_skip;
1397 }
1398
1399 hypercall_gpa = para_state->hypercall_gpa;
1400 hypercall_hpa = gpa_to_hpa(vcpu, hypercall_gpa);
1401 printk(KERN_DEBUG ".... hypercall_hpa: %08Lx\n", hypercall_hpa);
1402 if (is_error_hpa(hypercall_hpa)) {
1403 para_state->ret = -KVM_EINVAL;
1404 goto err_kunmap_skip;
1405 }
1406
1407 printk(KERN_DEBUG "kvm: para guest successfully registered.\n");
1408 vcpu->para_state_page = para_state_page;
1409 vcpu->para_state_gpa = para_state_gpa;
1410 vcpu->hypercall_gpa = hypercall_gpa;
1411
ab51a434 1412 mark_page_dirty(vcpu->kvm, hypercall_gpa >> PAGE_SHIFT);
102d8325
IM
1413 hypercall = kmap_atomic(pfn_to_page(hypercall_hpa >> PAGE_SHIFT),
1414 KM_USER1) + (hypercall_hpa & ~PAGE_MASK);
1415 kvm_arch_ops->patch_hypercall(vcpu, hypercall);
1416 kunmap_atomic(hypercall, KM_USER1);
1417
1418 para_state->ret = 0;
1419err_kunmap_skip:
1420 kunmap_atomic(para_state, KM_USER0);
1421 return 0;
1422err_gp:
1423 return 1;
1424}
1425
3bab1f5d
AK
1426int kvm_get_msr_common(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata)
1427{
1428 u64 data;
1429
1430 switch (msr) {
1431 case 0xc0010010: /* SYSCFG */
1432 case 0xc0010015: /* HWCR */
1433 case MSR_IA32_PLATFORM_ID:
1434 case MSR_IA32_P5_MC_ADDR:
1435 case MSR_IA32_P5_MC_TYPE:
1436 case MSR_IA32_MC0_CTL:
1437 case MSR_IA32_MCG_STATUS:
1438 case MSR_IA32_MCG_CAP:
1439 case MSR_IA32_MC0_MISC:
1440 case MSR_IA32_MC0_MISC+4:
1441 case MSR_IA32_MC0_MISC+8:
1442 case MSR_IA32_MC0_MISC+12:
1443 case MSR_IA32_MC0_MISC+16:
1444 case MSR_IA32_UCODE_REV:
a8d13ea2 1445 case MSR_IA32_PERF_STATUS:
3bab1f5d
AK
1446 /* MTRR registers */
1447 case 0xfe:
1448 case 0x200 ... 0x2ff:
1449 data = 0;
1450 break;
a8d13ea2
AK
1451 case 0xcd: /* fsb frequency */
1452 data = 3;
1453 break;
3bab1f5d
AK
1454 case MSR_IA32_APICBASE:
1455 data = vcpu->apic_base;
1456 break;
6f00e68f
AK
1457 case MSR_IA32_MISC_ENABLE:
1458 data = vcpu->ia32_misc_enable_msr;
1459 break;
3bab1f5d
AK
1460#ifdef CONFIG_X86_64
1461 case MSR_EFER:
1462 data = vcpu->shadow_efer;
1463 break;
1464#endif
1465 default:
1466 printk(KERN_ERR "kvm: unhandled rdmsr: 0x%x\n", msr);
1467 return 1;
1468 }
1469 *pdata = data;
1470 return 0;
1471}
1472EXPORT_SYMBOL_GPL(kvm_get_msr_common);
1473
6aa8b732
AK
1474/*
1475 * Reads an msr value (of 'msr_index') into 'pdata'.
1476 * Returns 0 on success, non-0 otherwise.
1477 * Assumes vcpu_load() was already called.
1478 */
1479static int get_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 *pdata)
1480{
1481 return kvm_arch_ops->get_msr(vcpu, msr_index, pdata);
1482}
1483
05b3e0c2 1484#ifdef CONFIG_X86_64
6aa8b732 1485
3bab1f5d 1486static void set_efer(struct kvm_vcpu *vcpu, u64 efer)
6aa8b732 1487{
6aa8b732
AK
1488 if (efer & EFER_RESERVED_BITS) {
1489 printk(KERN_DEBUG "set_efer: 0x%llx #GP, reserved bits\n",
1490 efer);
1491 inject_gp(vcpu);
1492 return;
1493 }
1494
1495 if (is_paging(vcpu)
1496 && (vcpu->shadow_efer & EFER_LME) != (efer & EFER_LME)) {
1497 printk(KERN_DEBUG "set_efer: #GP, change LME while paging\n");
1498 inject_gp(vcpu);
1499 return;
1500 }
1501
7725f0ba
AK
1502 kvm_arch_ops->set_efer(vcpu, efer);
1503
6aa8b732
AK
1504 efer &= ~EFER_LMA;
1505 efer |= vcpu->shadow_efer & EFER_LMA;
1506
1507 vcpu->shadow_efer = efer;
6aa8b732 1508}
6aa8b732
AK
1509
1510#endif
1511
3bab1f5d
AK
1512int kvm_set_msr_common(struct kvm_vcpu *vcpu, u32 msr, u64 data)
1513{
1514 switch (msr) {
1515#ifdef CONFIG_X86_64
1516 case MSR_EFER:
1517 set_efer(vcpu, data);
1518 break;
1519#endif
1520 case MSR_IA32_MC0_STATUS:
1521 printk(KERN_WARNING "%s: MSR_IA32_MC0_STATUS 0x%llx, nop\n",
1522 __FUNCTION__, data);
1523 break;
0e5bf0d0
SK
1524 case MSR_IA32_MCG_STATUS:
1525 printk(KERN_WARNING "%s: MSR_IA32_MCG_STATUS 0x%llx, nop\n",
1526 __FUNCTION__, data);
1527 break;
3bab1f5d
AK
1528 case MSR_IA32_UCODE_REV:
1529 case MSR_IA32_UCODE_WRITE:
1530 case 0x200 ... 0x2ff: /* MTRRs */
1531 break;
1532 case MSR_IA32_APICBASE:
1533 vcpu->apic_base = data;
1534 break;
6f00e68f
AK
1535 case MSR_IA32_MISC_ENABLE:
1536 vcpu->ia32_misc_enable_msr = data;
1537 break;
102d8325
IM
1538 /*
1539 * This is the 'probe whether the host is KVM' logic:
1540 */
1541 case MSR_KVM_API_MAGIC:
1542 return vcpu_register_para(vcpu, data);
1543
3bab1f5d
AK
1544 default:
1545 printk(KERN_ERR "kvm: unhandled wrmsr: 0x%x\n", msr);
1546 return 1;
1547 }
1548 return 0;
1549}
1550EXPORT_SYMBOL_GPL(kvm_set_msr_common);
1551
6aa8b732
AK
1552/*
1553 * Writes msr value into into the appropriate "register".
1554 * Returns 0 on success, non-0 otherwise.
1555 * Assumes vcpu_load() was already called.
1556 */
1557static int set_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 data)
1558{
1559 return kvm_arch_ops->set_msr(vcpu, msr_index, data);
1560}
1561
1562void kvm_resched(struct kvm_vcpu *vcpu)
1563{
3fca0365
YD
1564 if (!need_resched())
1565 return;
6aa8b732
AK
1566 vcpu_put(vcpu);
1567 cond_resched();
bccf2150 1568 vcpu_load(vcpu);
6aa8b732
AK
1569}
1570EXPORT_SYMBOL_GPL(kvm_resched);
1571
1572void load_msrs(struct vmx_msr_entry *e, int n)
1573{
1574 int i;
1575
1576 for (i = 0; i < n; ++i)
1577 wrmsrl(e[i].index, e[i].data);
1578}
1579EXPORT_SYMBOL_GPL(load_msrs);
1580
1581void save_msrs(struct vmx_msr_entry *e, int n)
1582{
1583 int i;
1584
1585 for (i = 0; i < n; ++i)
1586 rdmsrl(e[i].index, e[i].data);
1587}
1588EXPORT_SYMBOL_GPL(save_msrs);
1589
06465c5a
AK
1590void kvm_emulate_cpuid(struct kvm_vcpu *vcpu)
1591{
1592 int i;
1593 u32 function;
1594 struct kvm_cpuid_entry *e, *best;
1595
1596 kvm_arch_ops->cache_regs(vcpu);
1597 function = vcpu->regs[VCPU_REGS_RAX];
1598 vcpu->regs[VCPU_REGS_RAX] = 0;
1599 vcpu->regs[VCPU_REGS_RBX] = 0;
1600 vcpu->regs[VCPU_REGS_RCX] = 0;
1601 vcpu->regs[VCPU_REGS_RDX] = 0;
1602 best = NULL;
1603 for (i = 0; i < vcpu->cpuid_nent; ++i) {
1604 e = &vcpu->cpuid_entries[i];
1605 if (e->function == function) {
1606 best = e;
1607 break;
1608 }
1609 /*
1610 * Both basic or both extended?
1611 */
1612 if (((e->function ^ function) & 0x80000000) == 0)
1613 if (!best || e->function > best->function)
1614 best = e;
1615 }
1616 if (best) {
1617 vcpu->regs[VCPU_REGS_RAX] = best->eax;
1618 vcpu->regs[VCPU_REGS_RBX] = best->ebx;
1619 vcpu->regs[VCPU_REGS_RCX] = best->ecx;
1620 vcpu->regs[VCPU_REGS_RDX] = best->edx;
1621 }
1622 kvm_arch_ops->decache_regs(vcpu);
1623 kvm_arch_ops->skip_emulated_instruction(vcpu);
1624}
1625EXPORT_SYMBOL_GPL(kvm_emulate_cpuid);
1626
039576c0 1627static int pio_copy_data(struct kvm_vcpu *vcpu)
46fc1477 1628{
039576c0
AK
1629 void *p = vcpu->pio_data;
1630 void *q;
1631 unsigned bytes;
1632 int nr_pages = vcpu->pio.guest_pages[1] ? 2 : 1;
1633
1634 kvm_arch_ops->vcpu_put(vcpu);
1635 q = vmap(vcpu->pio.guest_pages, nr_pages, VM_READ|VM_WRITE,
1636 PAGE_KERNEL);
1637 if (!q) {
1638 kvm_arch_ops->vcpu_load(vcpu);
1639 free_pio_guest_pages(vcpu);
1640 return -ENOMEM;
1641 }
1642 q += vcpu->pio.guest_page_offset;
1643 bytes = vcpu->pio.size * vcpu->pio.cur_count;
1644 if (vcpu->pio.in)
1645 memcpy(q, p, bytes);
1646 else
1647 memcpy(p, q, bytes);
1648 q -= vcpu->pio.guest_page_offset;
1649 vunmap(q);
1650 kvm_arch_ops->vcpu_load(vcpu);
1651 free_pio_guest_pages(vcpu);
1652 return 0;
1653}
1654
1655static int complete_pio(struct kvm_vcpu *vcpu)
1656{
1657 struct kvm_pio_request *io = &vcpu->pio;
46fc1477 1658 long delta;
039576c0 1659 int r;
46fc1477
AK
1660
1661 kvm_arch_ops->cache_regs(vcpu);
1662
1663 if (!io->string) {
039576c0
AK
1664 if (io->in)
1665 memcpy(&vcpu->regs[VCPU_REGS_RAX], vcpu->pio_data,
46fc1477
AK
1666 io->size);
1667 } else {
039576c0
AK
1668 if (io->in) {
1669 r = pio_copy_data(vcpu);
1670 if (r) {
1671 kvm_arch_ops->cache_regs(vcpu);
1672 return r;
1673 }
1674 }
1675
46fc1477
AK
1676 delta = 1;
1677 if (io->rep) {
039576c0 1678 delta *= io->cur_count;
46fc1477
AK
1679 /*
1680 * The size of the register should really depend on
1681 * current address size.
1682 */
1683 vcpu->regs[VCPU_REGS_RCX] -= delta;
1684 }
039576c0 1685 if (io->down)
46fc1477
AK
1686 delta = -delta;
1687 delta *= io->size;
039576c0 1688 if (io->in)
46fc1477
AK
1689 vcpu->regs[VCPU_REGS_RDI] += delta;
1690 else
1691 vcpu->regs[VCPU_REGS_RSI] += delta;
1692 }
1693
46fc1477
AK
1694 vcpu->run->io_completed = 0;
1695
1696 kvm_arch_ops->decache_regs(vcpu);
1697
039576c0
AK
1698 io->count -= io->cur_count;
1699 io->cur_count = 0;
1700
1701 if (!io->count)
1702 kvm_arch_ops->skip_emulated_instruction(vcpu);
1703 return 0;
46fc1477
AK
1704}
1705
039576c0
AK
1706int kvm_setup_pio(struct kvm_vcpu *vcpu, struct kvm_run *run, int in,
1707 int size, unsigned long count, int string, int down,
1708 gva_t address, int rep, unsigned port)
1709{
1710 unsigned now, in_page;
1711 int i;
1712 int nr_pages = 1;
1713 struct page *page;
1714
1715 vcpu->run->exit_reason = KVM_EXIT_IO;
1716 vcpu->run->io.direction = in ? KVM_EXIT_IO_IN : KVM_EXIT_IO_OUT;
1717 vcpu->run->io.size = size;
1718 vcpu->run->io.data_offset = KVM_PIO_PAGE_OFFSET * PAGE_SIZE;
1719 vcpu->run->io.count = count;
1720 vcpu->run->io.port = port;
1721 vcpu->pio.count = count;
1722 vcpu->pio.cur_count = count;
1723 vcpu->pio.size = size;
1724 vcpu->pio.in = in;
1725 vcpu->pio.string = string;
1726 vcpu->pio.down = down;
1727 vcpu->pio.guest_page_offset = offset_in_page(address);
1728 vcpu->pio.rep = rep;
1729
1730 if (!string) {
1731 kvm_arch_ops->cache_regs(vcpu);
1732 memcpy(vcpu->pio_data, &vcpu->regs[VCPU_REGS_RAX], 4);
1733 kvm_arch_ops->decache_regs(vcpu);
1734 return 0;
1735 }
1736
1737 if (!count) {
1738 kvm_arch_ops->skip_emulated_instruction(vcpu);
1739 return 1;
1740 }
1741
1742 now = min(count, PAGE_SIZE / size);
1743
1744 if (!down)
1745 in_page = PAGE_SIZE - offset_in_page(address);
1746 else
1747 in_page = offset_in_page(address) + size;
1748 now = min(count, (unsigned long)in_page / size);
1749 if (!now) {
1750 /*
1751 * String I/O straddles page boundary. Pin two guest pages
1752 * so that we satisfy atomicity constraints. Do just one
1753 * transaction to avoid complexity.
1754 */
1755 nr_pages = 2;
1756 now = 1;
1757 }
1758 if (down) {
1759 /*
1760 * String I/O in reverse. Yuck. Kill the guest, fix later.
1761 */
1762 printk(KERN_ERR "kvm: guest string pio down\n");
1763 inject_gp(vcpu);
1764 return 1;
1765 }
1766 vcpu->run->io.count = now;
1767 vcpu->pio.cur_count = now;
1768
1769 for (i = 0; i < nr_pages; ++i) {
1770 spin_lock(&vcpu->kvm->lock);
1771 page = gva_to_page(vcpu, address + i * PAGE_SIZE);
1772 if (page)
1773 get_page(page);
1774 vcpu->pio.guest_pages[i] = page;
1775 spin_unlock(&vcpu->kvm->lock);
1776 if (!page) {
1777 inject_gp(vcpu);
1778 free_pio_guest_pages(vcpu);
1779 return 1;
1780 }
1781 }
1782
1783 if (!vcpu->pio.in)
1784 return pio_copy_data(vcpu);
1785 return 0;
1786}
1787EXPORT_SYMBOL_GPL(kvm_setup_pio);
1788
bccf2150 1789static int kvm_vcpu_ioctl_run(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
6aa8b732 1790{
6aa8b732 1791 int r;
1961d276 1792 sigset_t sigsaved;
6aa8b732 1793
bccf2150 1794 vcpu_load(vcpu);
6aa8b732 1795
1961d276
AK
1796 if (vcpu->sigset_active)
1797 sigprocmask(SIG_SETMASK, &vcpu->sigset, &sigsaved);
1798
54810342
DL
1799 /* re-sync apic's tpr */
1800 vcpu->cr8 = kvm_run->cr8;
1801
46fc1477 1802 if (kvm_run->io_completed) {
039576c0
AK
1803 if (vcpu->pio.cur_count) {
1804 r = complete_pio(vcpu);
1805 if (r)
1806 goto out;
1807 } else {
46fc1477
AK
1808 memcpy(vcpu->mmio_data, kvm_run->mmio.data, 8);
1809 vcpu->mmio_read_completed = 1;
1810 }
6aa8b732
AK
1811 }
1812
1813 vcpu->mmio_needed = 0;
1814
8eb7d334 1815 if (kvm_run->exit_reason == KVM_EXIT_HYPERCALL) {
b4e63f56
AK
1816 kvm_arch_ops->cache_regs(vcpu);
1817 vcpu->regs[VCPU_REGS_RAX] = kvm_run->hypercall.ret;
1818 kvm_arch_ops->decache_regs(vcpu);
1819 }
1820
6aa8b732
AK
1821 r = kvm_arch_ops->run(vcpu, kvm_run);
1822
039576c0 1823out:
1961d276
AK
1824 if (vcpu->sigset_active)
1825 sigprocmask(SIG_SETMASK, &sigsaved, NULL);
1826
6aa8b732
AK
1827 vcpu_put(vcpu);
1828 return r;
1829}
1830
bccf2150
AK
1831static int kvm_vcpu_ioctl_get_regs(struct kvm_vcpu *vcpu,
1832 struct kvm_regs *regs)
6aa8b732 1833{
bccf2150 1834 vcpu_load(vcpu);
6aa8b732
AK
1835
1836 kvm_arch_ops->cache_regs(vcpu);
1837
1838 regs->rax = vcpu->regs[VCPU_REGS_RAX];
1839 regs->rbx = vcpu->regs[VCPU_REGS_RBX];
1840 regs->rcx = vcpu->regs[VCPU_REGS_RCX];
1841 regs->rdx = vcpu->regs[VCPU_REGS_RDX];
1842 regs->rsi = vcpu->regs[VCPU_REGS_RSI];
1843 regs->rdi = vcpu->regs[VCPU_REGS_RDI];
1844 regs->rsp = vcpu->regs[VCPU_REGS_RSP];
1845 regs->rbp = vcpu->regs[VCPU_REGS_RBP];
05b3e0c2 1846#ifdef CONFIG_X86_64
6aa8b732
AK
1847 regs->r8 = vcpu->regs[VCPU_REGS_R8];
1848 regs->r9 = vcpu->regs[VCPU_REGS_R9];
1849 regs->r10 = vcpu->regs[VCPU_REGS_R10];
1850 regs->r11 = vcpu->regs[VCPU_REGS_R11];
1851 regs->r12 = vcpu->regs[VCPU_REGS_R12];
1852 regs->r13 = vcpu->regs[VCPU_REGS_R13];
1853 regs->r14 = vcpu->regs[VCPU_REGS_R14];
1854 regs->r15 = vcpu->regs[VCPU_REGS_R15];
1855#endif
1856
1857 regs->rip = vcpu->rip;
1858 regs->rflags = kvm_arch_ops->get_rflags(vcpu);
1859
1860 /*
1861 * Don't leak debug flags in case they were set for guest debugging
1862 */
1863 if (vcpu->guest_debug.enabled && vcpu->guest_debug.singlestep)
1864 regs->rflags &= ~(X86_EFLAGS_TF | X86_EFLAGS_RF);
1865
1866 vcpu_put(vcpu);
1867
1868 return 0;
1869}
1870
bccf2150
AK
1871static int kvm_vcpu_ioctl_set_regs(struct kvm_vcpu *vcpu,
1872 struct kvm_regs *regs)
6aa8b732 1873{
bccf2150 1874 vcpu_load(vcpu);
6aa8b732
AK
1875
1876 vcpu->regs[VCPU_REGS_RAX] = regs->rax;
1877 vcpu->regs[VCPU_REGS_RBX] = regs->rbx;
1878 vcpu->regs[VCPU_REGS_RCX] = regs->rcx;
1879 vcpu->regs[VCPU_REGS_RDX] = regs->rdx;
1880 vcpu->regs[VCPU_REGS_RSI] = regs->rsi;
1881 vcpu->regs[VCPU_REGS_RDI] = regs->rdi;
1882 vcpu->regs[VCPU_REGS_RSP] = regs->rsp;
1883 vcpu->regs[VCPU_REGS_RBP] = regs->rbp;
05b3e0c2 1884#ifdef CONFIG_X86_64
6aa8b732
AK
1885 vcpu->regs[VCPU_REGS_R8] = regs->r8;
1886 vcpu->regs[VCPU_REGS_R9] = regs->r9;
1887 vcpu->regs[VCPU_REGS_R10] = regs->r10;
1888 vcpu->regs[VCPU_REGS_R11] = regs->r11;
1889 vcpu->regs[VCPU_REGS_R12] = regs->r12;
1890 vcpu->regs[VCPU_REGS_R13] = regs->r13;
1891 vcpu->regs[VCPU_REGS_R14] = regs->r14;
1892 vcpu->regs[VCPU_REGS_R15] = regs->r15;
1893#endif
1894
1895 vcpu->rip = regs->rip;
1896 kvm_arch_ops->set_rflags(vcpu, regs->rflags);
1897
1898 kvm_arch_ops->decache_regs(vcpu);
1899
1900 vcpu_put(vcpu);
1901
1902 return 0;
1903}
1904
1905static void get_segment(struct kvm_vcpu *vcpu,
1906 struct kvm_segment *var, int seg)
1907{
1908 return kvm_arch_ops->get_segment(vcpu, var, seg);
1909}
1910
bccf2150
AK
1911static int kvm_vcpu_ioctl_get_sregs(struct kvm_vcpu *vcpu,
1912 struct kvm_sregs *sregs)
6aa8b732 1913{
6aa8b732
AK
1914 struct descriptor_table dt;
1915
bccf2150 1916 vcpu_load(vcpu);
6aa8b732
AK
1917
1918 get_segment(vcpu, &sregs->cs, VCPU_SREG_CS);
1919 get_segment(vcpu, &sregs->ds, VCPU_SREG_DS);
1920 get_segment(vcpu, &sregs->es, VCPU_SREG_ES);
1921 get_segment(vcpu, &sregs->fs, VCPU_SREG_FS);
1922 get_segment(vcpu, &sregs->gs, VCPU_SREG_GS);
1923 get_segment(vcpu, &sregs->ss, VCPU_SREG_SS);
1924
1925 get_segment(vcpu, &sregs->tr, VCPU_SREG_TR);
1926 get_segment(vcpu, &sregs->ldt, VCPU_SREG_LDTR);
1927
1928 kvm_arch_ops->get_idt(vcpu, &dt);
1929 sregs->idt.limit = dt.limit;
1930 sregs->idt.base = dt.base;
1931 kvm_arch_ops->get_gdt(vcpu, &dt);
1932 sregs->gdt.limit = dt.limit;
1933 sregs->gdt.base = dt.base;
1934
25c4c276 1935 kvm_arch_ops->decache_cr4_guest_bits(vcpu);
6aa8b732
AK
1936 sregs->cr0 = vcpu->cr0;
1937 sregs->cr2 = vcpu->cr2;
1938 sregs->cr3 = vcpu->cr3;
1939 sregs->cr4 = vcpu->cr4;
1940 sregs->cr8 = vcpu->cr8;
1941 sregs->efer = vcpu->shadow_efer;
1942 sregs->apic_base = vcpu->apic_base;
1943
1944 memcpy(sregs->interrupt_bitmap, vcpu->irq_pending,
1945 sizeof sregs->interrupt_bitmap);
1946
1947 vcpu_put(vcpu);
1948
1949 return 0;
1950}
1951
1952static void set_segment(struct kvm_vcpu *vcpu,
1953 struct kvm_segment *var, int seg)
1954{
1955 return kvm_arch_ops->set_segment(vcpu, var, seg);
1956}
1957
bccf2150
AK
1958static int kvm_vcpu_ioctl_set_sregs(struct kvm_vcpu *vcpu,
1959 struct kvm_sregs *sregs)
6aa8b732 1960{
6aa8b732
AK
1961 int mmu_reset_needed = 0;
1962 int i;
1963 struct descriptor_table dt;
1964
bccf2150 1965 vcpu_load(vcpu);
6aa8b732 1966
6aa8b732
AK
1967 dt.limit = sregs->idt.limit;
1968 dt.base = sregs->idt.base;
1969 kvm_arch_ops->set_idt(vcpu, &dt);
1970 dt.limit = sregs->gdt.limit;
1971 dt.base = sregs->gdt.base;
1972 kvm_arch_ops->set_gdt(vcpu, &dt);
1973
1974 vcpu->cr2 = sregs->cr2;
1975 mmu_reset_needed |= vcpu->cr3 != sregs->cr3;
1976 vcpu->cr3 = sregs->cr3;
1977
1978 vcpu->cr8 = sregs->cr8;
1979
1980 mmu_reset_needed |= vcpu->shadow_efer != sregs->efer;
05b3e0c2 1981#ifdef CONFIG_X86_64
6aa8b732
AK
1982 kvm_arch_ops->set_efer(vcpu, sregs->efer);
1983#endif
1984 vcpu->apic_base = sregs->apic_base;
1985
25c4c276 1986 kvm_arch_ops->decache_cr4_guest_bits(vcpu);
399badf3 1987
6aa8b732 1988 mmu_reset_needed |= vcpu->cr0 != sregs->cr0;
f6528b03 1989 kvm_arch_ops->set_cr0(vcpu, sregs->cr0);
6aa8b732
AK
1990
1991 mmu_reset_needed |= vcpu->cr4 != sregs->cr4;
1992 kvm_arch_ops->set_cr4(vcpu, sregs->cr4);
1b0973bd
AK
1993 if (!is_long_mode(vcpu) && is_pae(vcpu))
1994 load_pdptrs(vcpu, vcpu->cr3);
6aa8b732
AK
1995
1996 if (mmu_reset_needed)
1997 kvm_mmu_reset_context(vcpu);
1998
1999 memcpy(vcpu->irq_pending, sregs->interrupt_bitmap,
2000 sizeof vcpu->irq_pending);
2001 vcpu->irq_summary = 0;
2002 for (i = 0; i < NR_IRQ_WORDS; ++i)
2003 if (vcpu->irq_pending[i])
2004 __set_bit(i, &vcpu->irq_summary);
2005
024aa1c0
AK
2006 set_segment(vcpu, &sregs->cs, VCPU_SREG_CS);
2007 set_segment(vcpu, &sregs->ds, VCPU_SREG_DS);
2008 set_segment(vcpu, &sregs->es, VCPU_SREG_ES);
2009 set_segment(vcpu, &sregs->fs, VCPU_SREG_FS);
2010 set_segment(vcpu, &sregs->gs, VCPU_SREG_GS);
2011 set_segment(vcpu, &sregs->ss, VCPU_SREG_SS);
2012
2013 set_segment(vcpu, &sregs->tr, VCPU_SREG_TR);
2014 set_segment(vcpu, &sregs->ldt, VCPU_SREG_LDTR);
2015
6aa8b732
AK
2016 vcpu_put(vcpu);
2017
2018 return 0;
2019}
2020
2021/*
2022 * List of msr numbers which we expose to userspace through KVM_GET_MSRS
2023 * and KVM_SET_MSRS, and KVM_GET_MSR_INDEX_LIST.
bf591b24
MR
2024 *
2025 * This list is modified at module load time to reflect the
2026 * capabilities of the host cpu.
6aa8b732
AK
2027 */
2028static u32 msrs_to_save[] = {
2029 MSR_IA32_SYSENTER_CS, MSR_IA32_SYSENTER_ESP, MSR_IA32_SYSENTER_EIP,
2030 MSR_K6_STAR,
05b3e0c2 2031#ifdef CONFIG_X86_64
6aa8b732
AK
2032 MSR_CSTAR, MSR_KERNEL_GS_BASE, MSR_SYSCALL_MASK, MSR_LSTAR,
2033#endif
2034 MSR_IA32_TIME_STAMP_COUNTER,
2035};
2036
bf591b24
MR
2037static unsigned num_msrs_to_save;
2038
6f00e68f
AK
2039static u32 emulated_msrs[] = {
2040 MSR_IA32_MISC_ENABLE,
2041};
2042
bf591b24
MR
2043static __init void kvm_init_msr_list(void)
2044{
2045 u32 dummy[2];
2046 unsigned i, j;
2047
2048 for (i = j = 0; i < ARRAY_SIZE(msrs_to_save); i++) {
2049 if (rdmsr_safe(msrs_to_save[i], &dummy[0], &dummy[1]) < 0)
2050 continue;
2051 if (j < i)
2052 msrs_to_save[j] = msrs_to_save[i];
2053 j++;
2054 }
2055 num_msrs_to_save = j;
2056}
6aa8b732
AK
2057
2058/*
2059 * Adapt set_msr() to msr_io()'s calling convention
2060 */
2061static int do_set_msr(struct kvm_vcpu *vcpu, unsigned index, u64 *data)
2062{
2063 return set_msr(vcpu, index, *data);
2064}
2065
2066/*
2067 * Read or write a bunch of msrs. All parameters are kernel addresses.
2068 *
2069 * @return number of msrs set successfully.
2070 */
bccf2150 2071static int __msr_io(struct kvm_vcpu *vcpu, struct kvm_msrs *msrs,
6aa8b732
AK
2072 struct kvm_msr_entry *entries,
2073 int (*do_msr)(struct kvm_vcpu *vcpu,
2074 unsigned index, u64 *data))
2075{
6aa8b732
AK
2076 int i;
2077
bccf2150 2078 vcpu_load(vcpu);
6aa8b732
AK
2079
2080 for (i = 0; i < msrs->nmsrs; ++i)
2081 if (do_msr(vcpu, entries[i].index, &entries[i].data))
2082 break;
2083
2084 vcpu_put(vcpu);
2085
2086 return i;
2087}
2088
2089/*
2090 * Read or write a bunch of msrs. Parameters are user addresses.
2091 *
2092 * @return number of msrs set successfully.
2093 */
bccf2150 2094static int msr_io(struct kvm_vcpu *vcpu, struct kvm_msrs __user *user_msrs,
6aa8b732
AK
2095 int (*do_msr)(struct kvm_vcpu *vcpu,
2096 unsigned index, u64 *data),
2097 int writeback)
2098{
2099 struct kvm_msrs msrs;
2100 struct kvm_msr_entry *entries;
2101 int r, n;
2102 unsigned size;
2103
2104 r = -EFAULT;
2105 if (copy_from_user(&msrs, user_msrs, sizeof msrs))
2106 goto out;
2107
2108 r = -E2BIG;
2109 if (msrs.nmsrs >= MAX_IO_MSRS)
2110 goto out;
2111
2112 r = -ENOMEM;
2113 size = sizeof(struct kvm_msr_entry) * msrs.nmsrs;
2114 entries = vmalloc(size);
2115 if (!entries)
2116 goto out;
2117
2118 r = -EFAULT;
2119 if (copy_from_user(entries, user_msrs->entries, size))
2120 goto out_free;
2121
bccf2150 2122 r = n = __msr_io(vcpu, &msrs, entries, do_msr);
6aa8b732
AK
2123 if (r < 0)
2124 goto out_free;
2125
2126 r = -EFAULT;
2127 if (writeback && copy_to_user(user_msrs->entries, entries, size))
2128 goto out_free;
2129
2130 r = n;
2131
2132out_free:
2133 vfree(entries);
2134out:
2135 return r;
2136}
2137
2138/*
2139 * Translate a guest virtual address to a guest physical address.
2140 */
bccf2150
AK
2141static int kvm_vcpu_ioctl_translate(struct kvm_vcpu *vcpu,
2142 struct kvm_translation *tr)
6aa8b732
AK
2143{
2144 unsigned long vaddr = tr->linear_address;
6aa8b732
AK
2145 gpa_t gpa;
2146
bccf2150
AK
2147 vcpu_load(vcpu);
2148 spin_lock(&vcpu->kvm->lock);
6aa8b732
AK
2149 gpa = vcpu->mmu.gva_to_gpa(vcpu, vaddr);
2150 tr->physical_address = gpa;
2151 tr->valid = gpa != UNMAPPED_GVA;
2152 tr->writeable = 1;
2153 tr->usermode = 0;
bccf2150 2154 spin_unlock(&vcpu->kvm->lock);
6aa8b732
AK
2155 vcpu_put(vcpu);
2156
2157 return 0;
2158}
2159
bccf2150
AK
2160static int kvm_vcpu_ioctl_interrupt(struct kvm_vcpu *vcpu,
2161 struct kvm_interrupt *irq)
6aa8b732 2162{
6aa8b732
AK
2163 if (irq->irq < 0 || irq->irq >= 256)
2164 return -EINVAL;
bccf2150 2165 vcpu_load(vcpu);
6aa8b732
AK
2166
2167 set_bit(irq->irq, vcpu->irq_pending);
2168 set_bit(irq->irq / BITS_PER_LONG, &vcpu->irq_summary);
2169
2170 vcpu_put(vcpu);
2171
2172 return 0;
2173}
2174
bccf2150
AK
2175static int kvm_vcpu_ioctl_debug_guest(struct kvm_vcpu *vcpu,
2176 struct kvm_debug_guest *dbg)
6aa8b732 2177{
6aa8b732
AK
2178 int r;
2179
bccf2150 2180 vcpu_load(vcpu);
6aa8b732
AK
2181
2182 r = kvm_arch_ops->set_guest_debug(vcpu, dbg);
2183
2184 vcpu_put(vcpu);
2185
2186 return r;
2187}
2188
9a2bb7f4
AK
2189static struct page *kvm_vcpu_nopage(struct vm_area_struct *vma,
2190 unsigned long address,
2191 int *type)
2192{
2193 struct kvm_vcpu *vcpu = vma->vm_file->private_data;
2194 unsigned long pgoff;
2195 struct page *page;
2196
2197 *type = VM_FAULT_MINOR;
2198 pgoff = ((address - vma->vm_start) >> PAGE_SHIFT) + vma->vm_pgoff;
039576c0
AK
2199 if (pgoff == 0)
2200 page = virt_to_page(vcpu->run);
2201 else if (pgoff == KVM_PIO_PAGE_OFFSET)
2202 page = virt_to_page(vcpu->pio_data);
2203 else
9a2bb7f4 2204 return NOPAGE_SIGBUS;
9a2bb7f4
AK
2205 get_page(page);
2206 return page;
2207}
2208
2209static struct vm_operations_struct kvm_vcpu_vm_ops = {
2210 .nopage = kvm_vcpu_nopage,
2211};
2212
2213static int kvm_vcpu_mmap(struct file *file, struct vm_area_struct *vma)
2214{
2215 vma->vm_ops = &kvm_vcpu_vm_ops;
2216 return 0;
2217}
2218
bccf2150
AK
2219static int kvm_vcpu_release(struct inode *inode, struct file *filp)
2220{
2221 struct kvm_vcpu *vcpu = filp->private_data;
2222
2223 fput(vcpu->kvm->filp);
2224 return 0;
2225}
2226
2227static struct file_operations kvm_vcpu_fops = {
2228 .release = kvm_vcpu_release,
2229 .unlocked_ioctl = kvm_vcpu_ioctl,
2230 .compat_ioctl = kvm_vcpu_ioctl,
9a2bb7f4 2231 .mmap = kvm_vcpu_mmap,
bccf2150
AK
2232};
2233
2234/*
2235 * Allocates an inode for the vcpu.
2236 */
2237static int create_vcpu_fd(struct kvm_vcpu *vcpu)
2238{
2239 int fd, r;
2240 struct inode *inode;
2241 struct file *file;
2242
2243 atomic_inc(&vcpu->kvm->filp->f_count);
2244 inode = kvmfs_inode(&kvm_vcpu_fops);
2245 if (IS_ERR(inode)) {
2246 r = PTR_ERR(inode);
2247 goto out1;
2248 }
2249
2250 file = kvmfs_file(inode, vcpu);
2251 if (IS_ERR(file)) {
2252 r = PTR_ERR(file);
2253 goto out2;
2254 }
2255
2256 r = get_unused_fd();
2257 if (r < 0)
2258 goto out3;
2259 fd = r;
2260 fd_install(fd, file);
2261
2262 return fd;
2263
2264out3:
2265 fput(file);
2266out2:
2267 iput(inode);
2268out1:
2269 fput(vcpu->kvm->filp);
2270 return r;
2271}
2272
c5ea7660
AK
2273/*
2274 * Creates some virtual cpus. Good luck creating more than one.
2275 */
2276static int kvm_vm_ioctl_create_vcpu(struct kvm *kvm, int n)
2277{
2278 int r;
2279 struct kvm_vcpu *vcpu;
9a2bb7f4 2280 struct page *page;
c5ea7660
AK
2281
2282 r = -EINVAL;
2283 if (!valid_vcpu(n))
2284 goto out;
2285
2286 vcpu = &kvm->vcpus[n];
2287
2288 mutex_lock(&vcpu->mutex);
2289
2290 if (vcpu->vmcs) {
2291 mutex_unlock(&vcpu->mutex);
2292 return -EEXIST;
2293 }
2294
9a2bb7f4
AK
2295 page = alloc_page(GFP_KERNEL | __GFP_ZERO);
2296 r = -ENOMEM;
2297 if (!page)
2298 goto out_unlock;
2299 vcpu->run = page_address(page);
2300
039576c0
AK
2301 page = alloc_page(GFP_KERNEL | __GFP_ZERO);
2302 r = -ENOMEM;
2303 if (!page)
2304 goto out_free_run;
2305 vcpu->pio_data = page_address(page);
2306
c5ea7660
AK
2307 vcpu->host_fx_image = (char*)ALIGN((hva_t)vcpu->fx_buf,
2308 FX_IMAGE_ALIGN);
2309 vcpu->guest_fx_image = vcpu->host_fx_image + FX_IMAGE_SIZE;
d917a6b9 2310 vcpu->cr0 = 0x10;
c5ea7660
AK
2311
2312 r = kvm_arch_ops->vcpu_create(vcpu);
2313 if (r < 0)
2314 goto out_free_vcpus;
2315
2316 r = kvm_mmu_create(vcpu);
2317 if (r < 0)
2318 goto out_free_vcpus;
2319
2320 kvm_arch_ops->vcpu_load(vcpu);
2321 r = kvm_mmu_setup(vcpu);
2322 if (r >= 0)
2323 r = kvm_arch_ops->vcpu_setup(vcpu);
2324 vcpu_put(vcpu);
2325
2326 if (r < 0)
2327 goto out_free_vcpus;
2328
bccf2150
AK
2329 r = create_vcpu_fd(vcpu);
2330 if (r < 0)
2331 goto out_free_vcpus;
2332
2333 return r;
c5ea7660
AK
2334
2335out_free_vcpus:
2336 kvm_free_vcpu(vcpu);
039576c0
AK
2337out_free_run:
2338 free_page((unsigned long)vcpu->run);
2339 vcpu->run = NULL;
9a2bb7f4 2340out_unlock:
c5ea7660
AK
2341 mutex_unlock(&vcpu->mutex);
2342out:
2343 return r;
2344}
2345
06465c5a
AK
2346static int kvm_vcpu_ioctl_set_cpuid(struct kvm_vcpu *vcpu,
2347 struct kvm_cpuid *cpuid,
2348 struct kvm_cpuid_entry __user *entries)
2349{
2350 int r;
2351
2352 r = -E2BIG;
2353 if (cpuid->nent > KVM_MAX_CPUID_ENTRIES)
2354 goto out;
2355 r = -EFAULT;
2356 if (copy_from_user(&vcpu->cpuid_entries, entries,
2357 cpuid->nent * sizeof(struct kvm_cpuid_entry)))
2358 goto out;
2359 vcpu->cpuid_nent = cpuid->nent;
2360 return 0;
2361
2362out:
2363 return r;
2364}
2365
1961d276
AK
2366static int kvm_vcpu_ioctl_set_sigmask(struct kvm_vcpu *vcpu, sigset_t *sigset)
2367{
2368 if (sigset) {
2369 sigdelsetmask(sigset, sigmask(SIGKILL)|sigmask(SIGSTOP));
2370 vcpu->sigset_active = 1;
2371 vcpu->sigset = *sigset;
2372 } else
2373 vcpu->sigset_active = 0;
2374 return 0;
2375}
2376
b8836737
AK
2377/*
2378 * fxsave fpu state. Taken from x86_64/processor.h. To be killed when
2379 * we have asm/x86/processor.h
2380 */
2381struct fxsave {
2382 u16 cwd;
2383 u16 swd;
2384 u16 twd;
2385 u16 fop;
2386 u64 rip;
2387 u64 rdp;
2388 u32 mxcsr;
2389 u32 mxcsr_mask;
2390 u32 st_space[32]; /* 8*16 bytes for each FP-reg = 128 bytes */
2391#ifdef CONFIG_X86_64
2392 u32 xmm_space[64]; /* 16*16 bytes for each XMM-reg = 256 bytes */
2393#else
2394 u32 xmm_space[32]; /* 8*16 bytes for each XMM-reg = 128 bytes */
2395#endif
2396};
2397
2398static int kvm_vcpu_ioctl_get_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
2399{
2400 struct fxsave *fxsave = (struct fxsave *)vcpu->guest_fx_image;
2401
2402 vcpu_load(vcpu);
2403
2404 memcpy(fpu->fpr, fxsave->st_space, 128);
2405 fpu->fcw = fxsave->cwd;
2406 fpu->fsw = fxsave->swd;
2407 fpu->ftwx = fxsave->twd;
2408 fpu->last_opcode = fxsave->fop;
2409 fpu->last_ip = fxsave->rip;
2410 fpu->last_dp = fxsave->rdp;
2411 memcpy(fpu->xmm, fxsave->xmm_space, sizeof fxsave->xmm_space);
2412
2413 vcpu_put(vcpu);
2414
2415 return 0;
2416}
2417
2418static int kvm_vcpu_ioctl_set_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
2419{
2420 struct fxsave *fxsave = (struct fxsave *)vcpu->guest_fx_image;
2421
2422 vcpu_load(vcpu);
2423
2424 memcpy(fxsave->st_space, fpu->fpr, 128);
2425 fxsave->cwd = fpu->fcw;
2426 fxsave->swd = fpu->fsw;
2427 fxsave->twd = fpu->ftwx;
2428 fxsave->fop = fpu->last_opcode;
2429 fxsave->rip = fpu->last_ip;
2430 fxsave->rdp = fpu->last_dp;
2431 memcpy(fxsave->xmm_space, fpu->xmm, sizeof fxsave->xmm_space);
2432
2433 vcpu_put(vcpu);
2434
2435 return 0;
2436}
2437
bccf2150
AK
2438static long kvm_vcpu_ioctl(struct file *filp,
2439 unsigned int ioctl, unsigned long arg)
6aa8b732 2440{
bccf2150 2441 struct kvm_vcpu *vcpu = filp->private_data;
2f366987 2442 void __user *argp = (void __user *)arg;
6aa8b732
AK
2443 int r = -EINVAL;
2444
2445 switch (ioctl) {
9a2bb7f4 2446 case KVM_RUN:
f0fe5108
AK
2447 r = -EINVAL;
2448 if (arg)
2449 goto out;
9a2bb7f4 2450 r = kvm_vcpu_ioctl_run(vcpu, vcpu->run);
6aa8b732 2451 break;
6aa8b732
AK
2452 case KVM_GET_REGS: {
2453 struct kvm_regs kvm_regs;
2454
bccf2150
AK
2455 memset(&kvm_regs, 0, sizeof kvm_regs);
2456 r = kvm_vcpu_ioctl_get_regs(vcpu, &kvm_regs);
6aa8b732
AK
2457 if (r)
2458 goto out;
2459 r = -EFAULT;
2f366987 2460 if (copy_to_user(argp, &kvm_regs, sizeof kvm_regs))
6aa8b732
AK
2461 goto out;
2462 r = 0;
2463 break;
2464 }
2465 case KVM_SET_REGS: {
2466 struct kvm_regs kvm_regs;
2467
2468 r = -EFAULT;
2f366987 2469 if (copy_from_user(&kvm_regs, argp, sizeof kvm_regs))
6aa8b732 2470 goto out;
bccf2150 2471 r = kvm_vcpu_ioctl_set_regs(vcpu, &kvm_regs);
6aa8b732
AK
2472 if (r)
2473 goto out;
2474 r = 0;
2475 break;
2476 }
2477 case KVM_GET_SREGS: {
2478 struct kvm_sregs kvm_sregs;
2479
bccf2150
AK
2480 memset(&kvm_sregs, 0, sizeof kvm_sregs);
2481 r = kvm_vcpu_ioctl_get_sregs(vcpu, &kvm_sregs);
6aa8b732
AK
2482 if (r)
2483 goto out;
2484 r = -EFAULT;
2f366987 2485 if (copy_to_user(argp, &kvm_sregs, sizeof kvm_sregs))
6aa8b732
AK
2486 goto out;
2487 r = 0;
2488 break;
2489 }
2490 case KVM_SET_SREGS: {
2491 struct kvm_sregs kvm_sregs;
2492
2493 r = -EFAULT;
2f366987 2494 if (copy_from_user(&kvm_sregs, argp, sizeof kvm_sregs))
6aa8b732 2495 goto out;
bccf2150 2496 r = kvm_vcpu_ioctl_set_sregs(vcpu, &kvm_sregs);
6aa8b732
AK
2497 if (r)
2498 goto out;
2499 r = 0;
2500 break;
2501 }
2502 case KVM_TRANSLATE: {
2503 struct kvm_translation tr;
2504
2505 r = -EFAULT;
2f366987 2506 if (copy_from_user(&tr, argp, sizeof tr))
6aa8b732 2507 goto out;
bccf2150 2508 r = kvm_vcpu_ioctl_translate(vcpu, &tr);
6aa8b732
AK
2509 if (r)
2510 goto out;
2511 r = -EFAULT;
2f366987 2512 if (copy_to_user(argp, &tr, sizeof tr))
6aa8b732
AK
2513 goto out;
2514 r = 0;
2515 break;
2516 }
2517 case KVM_INTERRUPT: {
2518 struct kvm_interrupt irq;
2519
2520 r = -EFAULT;
2f366987 2521 if (copy_from_user(&irq, argp, sizeof irq))
6aa8b732 2522 goto out;
bccf2150 2523 r = kvm_vcpu_ioctl_interrupt(vcpu, &irq);
6aa8b732
AK
2524 if (r)
2525 goto out;
2526 r = 0;
2527 break;
2528 }
2529 case KVM_DEBUG_GUEST: {
2530 struct kvm_debug_guest dbg;
2531
2532 r = -EFAULT;
2f366987 2533 if (copy_from_user(&dbg, argp, sizeof dbg))
6aa8b732 2534 goto out;
bccf2150 2535 r = kvm_vcpu_ioctl_debug_guest(vcpu, &dbg);
6aa8b732
AK
2536 if (r)
2537 goto out;
2538 r = 0;
2539 break;
2540 }
bccf2150
AK
2541 case KVM_GET_MSRS:
2542 r = msr_io(vcpu, argp, get_msr, 1);
2543 break;
2544 case KVM_SET_MSRS:
2545 r = msr_io(vcpu, argp, do_set_msr, 0);
2546 break;
06465c5a
AK
2547 case KVM_SET_CPUID: {
2548 struct kvm_cpuid __user *cpuid_arg = argp;
2549 struct kvm_cpuid cpuid;
2550
2551 r = -EFAULT;
2552 if (copy_from_user(&cpuid, cpuid_arg, sizeof cpuid))
2553 goto out;
2554 r = kvm_vcpu_ioctl_set_cpuid(vcpu, &cpuid, cpuid_arg->entries);
2555 if (r)
2556 goto out;
2557 break;
2558 }
1961d276
AK
2559 case KVM_SET_SIGNAL_MASK: {
2560 struct kvm_signal_mask __user *sigmask_arg = argp;
2561 struct kvm_signal_mask kvm_sigmask;
2562 sigset_t sigset, *p;
2563
2564 p = NULL;
2565 if (argp) {
2566 r = -EFAULT;
2567 if (copy_from_user(&kvm_sigmask, argp,
2568 sizeof kvm_sigmask))
2569 goto out;
2570 r = -EINVAL;
2571 if (kvm_sigmask.len != sizeof sigset)
2572 goto out;
2573 r = -EFAULT;
2574 if (copy_from_user(&sigset, sigmask_arg->sigset,
2575 sizeof sigset))
2576 goto out;
2577 p = &sigset;
2578 }
2579 r = kvm_vcpu_ioctl_set_sigmask(vcpu, &sigset);
2580 break;
2581 }
b8836737
AK
2582 case KVM_GET_FPU: {
2583 struct kvm_fpu fpu;
2584
2585 memset(&fpu, 0, sizeof fpu);
2586 r = kvm_vcpu_ioctl_get_fpu(vcpu, &fpu);
2587 if (r)
2588 goto out;
2589 r = -EFAULT;
2590 if (copy_to_user(argp, &fpu, sizeof fpu))
2591 goto out;
2592 r = 0;
2593 break;
2594 }
2595 case KVM_SET_FPU: {
2596 struct kvm_fpu fpu;
2597
2598 r = -EFAULT;
2599 if (copy_from_user(&fpu, argp, sizeof fpu))
2600 goto out;
2601 r = kvm_vcpu_ioctl_set_fpu(vcpu, &fpu);
2602 if (r)
2603 goto out;
2604 r = 0;
2605 break;
2606 }
bccf2150
AK
2607 default:
2608 ;
2609 }
2610out:
2611 return r;
2612}
2613
2614static long kvm_vm_ioctl(struct file *filp,
2615 unsigned int ioctl, unsigned long arg)
2616{
2617 struct kvm *kvm = filp->private_data;
2618 void __user *argp = (void __user *)arg;
2619 int r = -EINVAL;
2620
2621 switch (ioctl) {
2622 case KVM_CREATE_VCPU:
2623 r = kvm_vm_ioctl_create_vcpu(kvm, arg);
2624 if (r < 0)
2625 goto out;
2626 break;
6aa8b732
AK
2627 case KVM_SET_MEMORY_REGION: {
2628 struct kvm_memory_region kvm_mem;
2629
2630 r = -EFAULT;
2f366987 2631 if (copy_from_user(&kvm_mem, argp, sizeof kvm_mem))
6aa8b732 2632 goto out;
2c6f5df9 2633 r = kvm_vm_ioctl_set_memory_region(kvm, &kvm_mem);
6aa8b732
AK
2634 if (r)
2635 goto out;
2636 break;
2637 }
2638 case KVM_GET_DIRTY_LOG: {
2639 struct kvm_dirty_log log;
2640
2641 r = -EFAULT;
2f366987 2642 if (copy_from_user(&log, argp, sizeof log))
6aa8b732 2643 goto out;
2c6f5df9 2644 r = kvm_vm_ioctl_get_dirty_log(kvm, &log);
6aa8b732
AK
2645 if (r)
2646 goto out;
2647 break;
2648 }
e8207547
AK
2649 case KVM_SET_MEMORY_ALIAS: {
2650 struct kvm_memory_alias alias;
2651
2652 r = -EFAULT;
2653 if (copy_from_user(&alias, argp, sizeof alias))
2654 goto out;
2655 r = kvm_vm_ioctl_set_memory_alias(kvm, &alias);
2656 if (r)
2657 goto out;
2658 break;
2659 }
f17abe9a
AK
2660 default:
2661 ;
2662 }
2663out:
2664 return r;
2665}
2666
2667static struct page *kvm_vm_nopage(struct vm_area_struct *vma,
2668 unsigned long address,
2669 int *type)
2670{
2671 struct kvm *kvm = vma->vm_file->private_data;
2672 unsigned long pgoff;
f17abe9a
AK
2673 struct page *page;
2674
2675 *type = VM_FAULT_MINOR;
2676 pgoff = ((address - vma->vm_start) >> PAGE_SHIFT) + vma->vm_pgoff;
954bbbc2 2677 page = gfn_to_page(kvm, pgoff);
f17abe9a
AK
2678 if (!page)
2679 return NOPAGE_SIGBUS;
2680 get_page(page);
2681 return page;
2682}
2683
2684static struct vm_operations_struct kvm_vm_vm_ops = {
2685 .nopage = kvm_vm_nopage,
2686};
2687
2688static int kvm_vm_mmap(struct file *file, struct vm_area_struct *vma)
2689{
2690 vma->vm_ops = &kvm_vm_vm_ops;
2691 return 0;
2692}
2693
2694static struct file_operations kvm_vm_fops = {
2695 .release = kvm_vm_release,
2696 .unlocked_ioctl = kvm_vm_ioctl,
2697 .compat_ioctl = kvm_vm_ioctl,
2698 .mmap = kvm_vm_mmap,
2699};
2700
2701static int kvm_dev_ioctl_create_vm(void)
2702{
2703 int fd, r;
2704 struct inode *inode;
2705 struct file *file;
2706 struct kvm *kvm;
2707
2708 inode = kvmfs_inode(&kvm_vm_fops);
2709 if (IS_ERR(inode)) {
2710 r = PTR_ERR(inode);
2711 goto out1;
2712 }
2713
2714 kvm = kvm_create_vm();
2715 if (IS_ERR(kvm)) {
2716 r = PTR_ERR(kvm);
2717 goto out2;
2718 }
2719
2720 file = kvmfs_file(inode, kvm);
2721 if (IS_ERR(file)) {
2722 r = PTR_ERR(file);
2723 goto out3;
2724 }
bccf2150 2725 kvm->filp = file;
f17abe9a
AK
2726
2727 r = get_unused_fd();
2728 if (r < 0)
2729 goto out4;
2730 fd = r;
2731 fd_install(fd, file);
2732
2733 return fd;
2734
2735out4:
2736 fput(file);
2737out3:
2738 kvm_destroy_vm(kvm);
2739out2:
2740 iput(inode);
2741out1:
2742 return r;
2743}
2744
2745static long kvm_dev_ioctl(struct file *filp,
2746 unsigned int ioctl, unsigned long arg)
2747{
2748 void __user *argp = (void __user *)arg;
07c45a36 2749 long r = -EINVAL;
f17abe9a
AK
2750
2751 switch (ioctl) {
2752 case KVM_GET_API_VERSION:
f0fe5108
AK
2753 r = -EINVAL;
2754 if (arg)
2755 goto out;
f17abe9a
AK
2756 r = KVM_API_VERSION;
2757 break;
2758 case KVM_CREATE_VM:
f0fe5108
AK
2759 r = -EINVAL;
2760 if (arg)
2761 goto out;
f17abe9a
AK
2762 r = kvm_dev_ioctl_create_vm();
2763 break;
6aa8b732 2764 case KVM_GET_MSR_INDEX_LIST: {
2f366987 2765 struct kvm_msr_list __user *user_msr_list = argp;
6aa8b732
AK
2766 struct kvm_msr_list msr_list;
2767 unsigned n;
2768
2769 r = -EFAULT;
2770 if (copy_from_user(&msr_list, user_msr_list, sizeof msr_list))
2771 goto out;
2772 n = msr_list.nmsrs;
6f00e68f 2773 msr_list.nmsrs = num_msrs_to_save + ARRAY_SIZE(emulated_msrs);
6aa8b732
AK
2774 if (copy_to_user(user_msr_list, &msr_list, sizeof msr_list))
2775 goto out;
2776 r = -E2BIG;
bf591b24 2777 if (n < num_msrs_to_save)
6aa8b732
AK
2778 goto out;
2779 r = -EFAULT;
2780 if (copy_to_user(user_msr_list->indices, &msrs_to_save,
bf591b24 2781 num_msrs_to_save * sizeof(u32)))
6aa8b732 2782 goto out;
6f00e68f
AK
2783 if (copy_to_user(user_msr_list->indices
2784 + num_msrs_to_save * sizeof(u32),
2785 &emulated_msrs,
2786 ARRAY_SIZE(emulated_msrs) * sizeof(u32)))
2787 goto out;
6aa8b732 2788 r = 0;
cc1d8955 2789 break;
6aa8b732 2790 }
5d308f45
AK
2791 case KVM_CHECK_EXTENSION:
2792 /*
2793 * No extensions defined at present.
2794 */
2795 r = 0;
2796 break;
07c45a36
AK
2797 case KVM_GET_VCPU_MMAP_SIZE:
2798 r = -EINVAL;
2799 if (arg)
2800 goto out;
039576c0 2801 r = 2 * PAGE_SIZE;
07c45a36 2802 break;
6aa8b732
AK
2803 default:
2804 ;
2805 }
2806out:
2807 return r;
2808}
2809
6aa8b732
AK
2810static struct file_operations kvm_chardev_ops = {
2811 .open = kvm_dev_open,
2812 .release = kvm_dev_release,
2813 .unlocked_ioctl = kvm_dev_ioctl,
2814 .compat_ioctl = kvm_dev_ioctl,
6aa8b732
AK
2815};
2816
2817static struct miscdevice kvm_dev = {
bbe4432e 2818 KVM_MINOR,
6aa8b732
AK
2819 "kvm",
2820 &kvm_chardev_ops,
2821};
2822
2823static int kvm_reboot(struct notifier_block *notifier, unsigned long val,
2824 void *v)
2825{
2826 if (val == SYS_RESTART) {
2827 /*
2828 * Some (well, at least mine) BIOSes hang on reboot if
2829 * in vmx root mode.
2830 */
2831 printk(KERN_INFO "kvm: exiting hardware virtualization\n");
8b6d44c7 2832 on_each_cpu(kvm_arch_ops->hardware_disable, NULL, 0, 1);
6aa8b732
AK
2833 }
2834 return NOTIFY_OK;
2835}
2836
2837static struct notifier_block kvm_reboot_notifier = {
2838 .notifier_call = kvm_reboot,
2839 .priority = 0,
2840};
2841
774c47f1
AK
2842/*
2843 * Make sure that a cpu that is being hot-unplugged does not have any vcpus
2844 * cached on it.
2845 */
2846static void decache_vcpus_on_cpu(int cpu)
2847{
2848 struct kvm *vm;
2849 struct kvm_vcpu *vcpu;
2850 int i;
2851
2852 spin_lock(&kvm_lock);
2853 list_for_each_entry(vm, &vm_list, vm_list)
2854 for (i = 0; i < KVM_MAX_VCPUS; ++i) {
2855 vcpu = &vm->vcpus[i];
2856 /*
2857 * If the vcpu is locked, then it is running on some
2858 * other cpu and therefore it is not cached on the
2859 * cpu in question.
2860 *
2861 * If it's not locked, check the last cpu it executed
2862 * on.
2863 */
2864 if (mutex_trylock(&vcpu->mutex)) {
2865 if (vcpu->cpu == cpu) {
2866 kvm_arch_ops->vcpu_decache(vcpu);
2867 vcpu->cpu = -1;
2868 }
2869 mutex_unlock(&vcpu->mutex);
2870 }
2871 }
2872 spin_unlock(&kvm_lock);
2873}
2874
2875static int kvm_cpu_hotplug(struct notifier_block *notifier, unsigned long val,
2876 void *v)
2877{
2878 int cpu = (long)v;
2879
2880 switch (val) {
43934a38 2881 case CPU_DOWN_PREPARE:
774c47f1 2882 case CPU_UP_CANCELED:
43934a38
JK
2883 printk(KERN_INFO "kvm: disabling virtualization on CPU%d\n",
2884 cpu);
774c47f1
AK
2885 decache_vcpus_on_cpu(cpu);
2886 smp_call_function_single(cpu, kvm_arch_ops->hardware_disable,
2887 NULL, 0, 1);
2888 break;
43934a38
JK
2889 case CPU_ONLINE:
2890 printk(KERN_INFO "kvm: enabling virtualization on CPU%d\n",
2891 cpu);
774c47f1
AK
2892 smp_call_function_single(cpu, kvm_arch_ops->hardware_enable,
2893 NULL, 0, 1);
2894 break;
2895 }
2896 return NOTIFY_OK;
2897}
2898
2899static struct notifier_block kvm_cpu_notifier = {
2900 .notifier_call = kvm_cpu_hotplug,
2901 .priority = 20, /* must be > scheduler priority */
2902};
2903
1165f5fe
AK
2904static u64 stat_get(void *_offset)
2905{
2906 unsigned offset = (long)_offset;
2907 u64 total = 0;
2908 struct kvm *kvm;
2909 struct kvm_vcpu *vcpu;
2910 int i;
2911
2912 spin_lock(&kvm_lock);
2913 list_for_each_entry(kvm, &vm_list, vm_list)
2914 for (i = 0; i < KVM_MAX_VCPUS; ++i) {
2915 vcpu = &kvm->vcpus[i];
2916 total += *(u32 *)((void *)vcpu + offset);
2917 }
2918 spin_unlock(&kvm_lock);
2919 return total;
2920}
2921
2922static void stat_set(void *offset, u64 val)
2923{
2924}
2925
2926DEFINE_SIMPLE_ATTRIBUTE(stat_fops, stat_get, stat_set, "%llu\n");
2927
6aa8b732
AK
2928static __init void kvm_init_debug(void)
2929{
2930 struct kvm_stats_debugfs_item *p;
2931
8b6d44c7 2932 debugfs_dir = debugfs_create_dir("kvm", NULL);
6aa8b732 2933 for (p = debugfs_entries; p->name; ++p)
1165f5fe
AK
2934 p->dentry = debugfs_create_file(p->name, 0444, debugfs_dir,
2935 (void *)(long)p->offset,
2936 &stat_fops);
6aa8b732
AK
2937}
2938
2939static void kvm_exit_debug(void)
2940{
2941 struct kvm_stats_debugfs_item *p;
2942
2943 for (p = debugfs_entries; p->name; ++p)
2944 debugfs_remove(p->dentry);
2945 debugfs_remove(debugfs_dir);
2946}
2947
59ae6c6b
AK
2948static int kvm_suspend(struct sys_device *dev, pm_message_t state)
2949{
2950 decache_vcpus_on_cpu(raw_smp_processor_id());
19d1408d 2951 on_each_cpu(kvm_arch_ops->hardware_disable, NULL, 0, 1);
59ae6c6b
AK
2952 return 0;
2953}
2954
2955static int kvm_resume(struct sys_device *dev)
2956{
19d1408d 2957 on_each_cpu(kvm_arch_ops->hardware_enable, NULL, 0, 1);
59ae6c6b
AK
2958 return 0;
2959}
2960
2961static struct sysdev_class kvm_sysdev_class = {
2962 set_kset_name("kvm"),
2963 .suspend = kvm_suspend,
2964 .resume = kvm_resume,
2965};
2966
2967static struct sys_device kvm_sysdev = {
2968 .id = 0,
2969 .cls = &kvm_sysdev_class,
2970};
2971
6aa8b732
AK
2972hpa_t bad_page_address;
2973
37e29d90
AK
2974static int kvmfs_get_sb(struct file_system_type *fs_type, int flags,
2975 const char *dev_name, void *data, struct vfsmount *mnt)
2976{
e9cdb1e3 2977 return get_sb_pseudo(fs_type, "kvm:", NULL, KVMFS_SUPER_MAGIC, mnt);
37e29d90
AK
2978}
2979
2980static struct file_system_type kvm_fs_type = {
2981 .name = "kvmfs",
2982 .get_sb = kvmfs_get_sb,
2983 .kill_sb = kill_anon_super,
2984};
2985
6aa8b732
AK
2986int kvm_init_arch(struct kvm_arch_ops *ops, struct module *module)
2987{
2988 int r;
2989
09db28b8
YI
2990 if (kvm_arch_ops) {
2991 printk(KERN_ERR "kvm: already loaded the other module\n");
2992 return -EEXIST;
2993 }
2994
e097f35c 2995 if (!ops->cpu_has_kvm_support()) {
6aa8b732
AK
2996 printk(KERN_ERR "kvm: no hardware support\n");
2997 return -EOPNOTSUPP;
2998 }
e097f35c 2999 if (ops->disabled_by_bios()) {
6aa8b732
AK
3000 printk(KERN_ERR "kvm: disabled by bios\n");
3001 return -EOPNOTSUPP;
3002 }
3003
e097f35c
YI
3004 kvm_arch_ops = ops;
3005
6aa8b732
AK
3006 r = kvm_arch_ops->hardware_setup();
3007 if (r < 0)
ca45aaae 3008 goto out;
6aa8b732 3009
8b6d44c7 3010 on_each_cpu(kvm_arch_ops->hardware_enable, NULL, 0, 1);
774c47f1
AK
3011 r = register_cpu_notifier(&kvm_cpu_notifier);
3012 if (r)
3013 goto out_free_1;
6aa8b732
AK
3014 register_reboot_notifier(&kvm_reboot_notifier);
3015
59ae6c6b
AK
3016 r = sysdev_class_register(&kvm_sysdev_class);
3017 if (r)
3018 goto out_free_2;
3019
3020 r = sysdev_register(&kvm_sysdev);
3021 if (r)
3022 goto out_free_3;
3023
6aa8b732
AK
3024 kvm_chardev_ops.owner = module;
3025
3026 r = misc_register(&kvm_dev);
3027 if (r) {
3028 printk (KERN_ERR "kvm: misc device register failed\n");
3029 goto out_free;
3030 }
3031
3032 return r;
3033
3034out_free:
59ae6c6b
AK
3035 sysdev_unregister(&kvm_sysdev);
3036out_free_3:
3037 sysdev_class_unregister(&kvm_sysdev_class);
3038out_free_2:
6aa8b732 3039 unregister_reboot_notifier(&kvm_reboot_notifier);
774c47f1
AK
3040 unregister_cpu_notifier(&kvm_cpu_notifier);
3041out_free_1:
8b6d44c7 3042 on_each_cpu(kvm_arch_ops->hardware_disable, NULL, 0, 1);
6aa8b732 3043 kvm_arch_ops->hardware_unsetup();
ca45aaae
AK
3044out:
3045 kvm_arch_ops = NULL;
6aa8b732
AK
3046 return r;
3047}
3048
3049void kvm_exit_arch(void)
3050{
3051 misc_deregister(&kvm_dev);
59ae6c6b
AK
3052 sysdev_unregister(&kvm_sysdev);
3053 sysdev_class_unregister(&kvm_sysdev_class);
6aa8b732 3054 unregister_reboot_notifier(&kvm_reboot_notifier);
59ae6c6b 3055 unregister_cpu_notifier(&kvm_cpu_notifier);
8b6d44c7 3056 on_each_cpu(kvm_arch_ops->hardware_disable, NULL, 0, 1);
6aa8b732 3057 kvm_arch_ops->hardware_unsetup();
09db28b8 3058 kvm_arch_ops = NULL;
6aa8b732
AK
3059}
3060
3061static __init int kvm_init(void)
3062{
3063 static struct page *bad_page;
37e29d90
AK
3064 int r;
3065
b5a33a75
AK
3066 r = kvm_mmu_module_init();
3067 if (r)
3068 goto out4;
3069
37e29d90
AK
3070 r = register_filesystem(&kvm_fs_type);
3071 if (r)
3072 goto out3;
6aa8b732 3073
37e29d90
AK
3074 kvmfs_mnt = kern_mount(&kvm_fs_type);
3075 r = PTR_ERR(kvmfs_mnt);
3076 if (IS_ERR(kvmfs_mnt))
3077 goto out2;
6aa8b732
AK
3078 kvm_init_debug();
3079
bf591b24
MR
3080 kvm_init_msr_list();
3081
6aa8b732
AK
3082 if ((bad_page = alloc_page(GFP_KERNEL)) == NULL) {
3083 r = -ENOMEM;
3084 goto out;
3085 }
3086
3087 bad_page_address = page_to_pfn(bad_page) << PAGE_SHIFT;
3088 memset(__va(bad_page_address), 0, PAGE_SIZE);
3089
58e690e6 3090 return 0;
6aa8b732
AK
3091
3092out:
3093 kvm_exit_debug();
37e29d90
AK
3094 mntput(kvmfs_mnt);
3095out2:
3096 unregister_filesystem(&kvm_fs_type);
3097out3:
b5a33a75
AK
3098 kvm_mmu_module_exit();
3099out4:
6aa8b732
AK
3100 return r;
3101}
3102
3103static __exit void kvm_exit(void)
3104{
3105 kvm_exit_debug();
3106 __free_page(pfn_to_page(bad_page_address >> PAGE_SHIFT));
37e29d90
AK
3107 mntput(kvmfs_mnt);
3108 unregister_filesystem(&kvm_fs_type);
b5a33a75 3109 kvm_mmu_module_exit();
6aa8b732
AK
3110}
3111
3112module_init(kvm_init)
3113module_exit(kvm_exit)
3114
3115EXPORT_SYMBOL_GPL(kvm_init_arch);
3116EXPORT_SYMBOL_GPL(kvm_exit_arch);