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