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