diff options
Diffstat (limited to 'drivers/kvm/kvm_main.c')
-rw-r--r-- | drivers/kvm/kvm_main.c | 776 |
1 files changed, 544 insertions, 232 deletions
diff --git a/drivers/kvm/kvm_main.c b/drivers/kvm/kvm_main.c index af866147ff25..a163bca38973 100644 --- a/drivers/kvm/kvm_main.c +++ b/drivers/kvm/kvm_main.c | |||
@@ -20,6 +20,7 @@ | |||
20 | #include <linux/kvm.h> | 20 | #include <linux/kvm.h> |
21 | #include <linux/module.h> | 21 | #include <linux/module.h> |
22 | #include <linux/errno.h> | 22 | #include <linux/errno.h> |
23 | #include <linux/magic.h> | ||
23 | #include <asm/processor.h> | 24 | #include <asm/processor.h> |
24 | #include <linux/percpu.h> | 25 | #include <linux/percpu.h> |
25 | #include <linux/gfp.h> | 26 | #include <linux/gfp.h> |
@@ -36,6 +37,9 @@ | |||
36 | #include <asm/desc.h> | 37 | #include <asm/desc.h> |
37 | #include <linux/sysdev.h> | 38 | #include <linux/sysdev.h> |
38 | #include <linux/cpu.h> | 39 | #include <linux/cpu.h> |
40 | #include <linux/file.h> | ||
41 | #include <linux/fs.h> | ||
42 | #include <linux/mount.h> | ||
39 | 43 | ||
40 | #include "x86_emulate.h" | 44 | #include "x86_emulate.h" |
41 | #include "segment_descriptor.h" | 45 | #include "segment_descriptor.h" |
@@ -72,6 +76,8 @@ static struct kvm_stats_debugfs_item { | |||
72 | 76 | ||
73 | static struct dentry *debugfs_dir; | 77 | static struct dentry *debugfs_dir; |
74 | 78 | ||
79 | struct vfsmount *kvmfs_mnt; | ||
80 | |||
75 | #define MAX_IO_MSRS 256 | 81 | #define MAX_IO_MSRS 256 |
76 | 82 | ||
77 | #define CR0_RESEVED_BITS 0xffffffff1ffaffc0ULL | 83 | #define CR0_RESEVED_BITS 0xffffffff1ffaffc0ULL |
@@ -90,6 +96,58 @@ struct segment_descriptor_64 { | |||
90 | 96 | ||
91 | #endif | 97 | #endif |
92 | 98 | ||
99 | static long kvm_vcpu_ioctl(struct file *file, unsigned int ioctl, | ||
100 | unsigned long arg); | ||
101 | |||
102 | static struct inode *kvmfs_inode(struct file_operations *fops) | ||
103 | { | ||
104 | int error = -ENOMEM; | ||
105 | struct inode *inode = new_inode(kvmfs_mnt->mnt_sb); | ||
106 | |||
107 | if (!inode) | ||
108 | goto eexit_1; | ||
109 | |||
110 | inode->i_fop = fops; | ||
111 | |||
112 | /* | ||
113 | * Mark the inode dirty from the very beginning, | ||
114 | * that way it will never be moved to the dirty | ||
115 | * list because mark_inode_dirty() will think | ||
116 | * that it already _is_ on the dirty list. | ||
117 | */ | ||
118 | inode->i_state = I_DIRTY; | ||
119 | inode->i_mode = S_IRUSR | S_IWUSR; | ||
120 | inode->i_uid = current->fsuid; | ||
121 | inode->i_gid = current->fsgid; | ||
122 | inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME; | ||
123 | return inode; | ||
124 | |||
125 | eexit_1: | ||
126 | return ERR_PTR(error); | ||
127 | } | ||
128 | |||
129 | static struct file *kvmfs_file(struct inode *inode, void *private_data) | ||
130 | { | ||
131 | struct file *file = get_empty_filp(); | ||
132 | |||
133 | if (!file) | ||
134 | return ERR_PTR(-ENFILE); | ||
135 | |||
136 | file->f_path.mnt = mntget(kvmfs_mnt); | ||
137 | file->f_path.dentry = d_alloc_anon(inode); | ||
138 | if (!file->f_path.dentry) | ||
139 | return ERR_PTR(-ENOMEM); | ||
140 | file->f_mapping = inode->i_mapping; | ||
141 | |||
142 | file->f_pos = 0; | ||
143 | file->f_flags = O_RDWR; | ||
144 | file->f_op = inode->i_fop; | ||
145 | file->f_mode = FMODE_READ | FMODE_WRITE; | ||
146 | file->f_version = 0; | ||
147 | file->private_data = private_data; | ||
148 | return file; | ||
149 | } | ||
150 | |||
93 | unsigned long segment_base(u16 selector) | 151 | unsigned long segment_base(u16 selector) |
94 | { | 152 | { |
95 | struct descriptor_table gdt; | 153 | struct descriptor_table gdt; |
@@ -126,10 +184,8 @@ static inline int valid_vcpu(int n) | |||
126 | return likely(n >= 0 && n < KVM_MAX_VCPUS); | 184 | return likely(n >= 0 && n < KVM_MAX_VCPUS); |
127 | } | 185 | } |
128 | 186 | ||
129 | int kvm_read_guest(struct kvm_vcpu *vcpu, | 187 | int kvm_read_guest(struct kvm_vcpu *vcpu, gva_t addr, unsigned long size, |
130 | gva_t addr, | 188 | void *dest) |
131 | unsigned long size, | ||
132 | void *dest) | ||
133 | { | 189 | { |
134 | unsigned char *host_buf = dest; | 190 | unsigned char *host_buf = dest; |
135 | unsigned long req_size = size; | 191 | unsigned long req_size = size; |
@@ -161,10 +217,8 @@ int kvm_read_guest(struct kvm_vcpu *vcpu, | |||
161 | } | 217 | } |
162 | EXPORT_SYMBOL_GPL(kvm_read_guest); | 218 | EXPORT_SYMBOL_GPL(kvm_read_guest); |
163 | 219 | ||
164 | int kvm_write_guest(struct kvm_vcpu *vcpu, | 220 | int kvm_write_guest(struct kvm_vcpu *vcpu, gva_t addr, unsigned long size, |
165 | gva_t addr, | 221 | void *data) |
166 | unsigned long size, | ||
167 | void *data) | ||
168 | { | 222 | { |
169 | unsigned char *host_buf = data; | 223 | unsigned char *host_buf = data; |
170 | unsigned long req_size = size; | 224 | unsigned long req_size = size; |
@@ -174,12 +228,15 @@ int kvm_write_guest(struct kvm_vcpu *vcpu, | |||
174 | unsigned now; | 228 | unsigned now; |
175 | unsigned offset; | 229 | unsigned offset; |
176 | hva_t guest_buf; | 230 | hva_t guest_buf; |
231 | gfn_t gfn; | ||
177 | 232 | ||
178 | paddr = gva_to_hpa(vcpu, addr); | 233 | paddr = gva_to_hpa(vcpu, addr); |
179 | 234 | ||
180 | if (is_error_hpa(paddr)) | 235 | if (is_error_hpa(paddr)) |
181 | break; | 236 | break; |
182 | 237 | ||
238 | gfn = vcpu->mmu.gva_to_gpa(vcpu, addr) >> PAGE_SHIFT; | ||
239 | mark_page_dirty(vcpu->kvm, gfn); | ||
183 | guest_buf = (hva_t)kmap_atomic( | 240 | guest_buf = (hva_t)kmap_atomic( |
184 | pfn_to_page(paddr >> PAGE_SHIFT), KM_USER0); | 241 | pfn_to_page(paddr >> PAGE_SHIFT), KM_USER0); |
185 | offset = addr & ~PAGE_MASK; | 242 | offset = addr & ~PAGE_MASK; |
@@ -195,24 +252,30 @@ int kvm_write_guest(struct kvm_vcpu *vcpu, | |||
195 | } | 252 | } |
196 | EXPORT_SYMBOL_GPL(kvm_write_guest); | 253 | EXPORT_SYMBOL_GPL(kvm_write_guest); |
197 | 254 | ||
198 | static int vcpu_slot(struct kvm_vcpu *vcpu) | 255 | /* |
256 | * Switches to specified vcpu, until a matching vcpu_put() | ||
257 | */ | ||
258 | static void vcpu_load(struct kvm_vcpu *vcpu) | ||
199 | { | 259 | { |
200 | return vcpu - vcpu->kvm->vcpus; | 260 | mutex_lock(&vcpu->mutex); |
261 | kvm_arch_ops->vcpu_load(vcpu); | ||
201 | } | 262 | } |
202 | 263 | ||
203 | /* | 264 | /* |
204 | * Switches to specified vcpu, until a matching vcpu_put() | 265 | * Switches to specified vcpu, until a matching vcpu_put(). Will return NULL |
266 | * if the slot is not populated. | ||
205 | */ | 267 | */ |
206 | static struct kvm_vcpu *vcpu_load(struct kvm *kvm, int vcpu_slot) | 268 | static struct kvm_vcpu *vcpu_load_slot(struct kvm *kvm, int slot) |
207 | { | 269 | { |
208 | struct kvm_vcpu *vcpu = &kvm->vcpus[vcpu_slot]; | 270 | struct kvm_vcpu *vcpu = &kvm->vcpus[slot]; |
209 | 271 | ||
210 | mutex_lock(&vcpu->mutex); | 272 | mutex_lock(&vcpu->mutex); |
211 | if (unlikely(!vcpu->vmcs)) { | 273 | if (!vcpu->vmcs) { |
212 | mutex_unlock(&vcpu->mutex); | 274 | mutex_unlock(&vcpu->mutex); |
213 | return NULL; | 275 | return NULL; |
214 | } | 276 | } |
215 | return kvm_arch_ops->vcpu_load(vcpu); | 277 | kvm_arch_ops->vcpu_load(vcpu); |
278 | return vcpu; | ||
216 | } | 279 | } |
217 | 280 | ||
218 | static void vcpu_put(struct kvm_vcpu *vcpu) | 281 | static void vcpu_put(struct kvm_vcpu *vcpu) |
@@ -221,13 +284,13 @@ static void vcpu_put(struct kvm_vcpu *vcpu) | |||
221 | mutex_unlock(&vcpu->mutex); | 284 | mutex_unlock(&vcpu->mutex); |
222 | } | 285 | } |
223 | 286 | ||
224 | static int kvm_dev_open(struct inode *inode, struct file *filp) | 287 | static struct kvm *kvm_create_vm(void) |
225 | { | 288 | { |
226 | struct kvm *kvm = kzalloc(sizeof(struct kvm), GFP_KERNEL); | 289 | struct kvm *kvm = kzalloc(sizeof(struct kvm), GFP_KERNEL); |
227 | int i; | 290 | int i; |
228 | 291 | ||
229 | if (!kvm) | 292 | if (!kvm) |
230 | return -ENOMEM; | 293 | return ERR_PTR(-ENOMEM); |
231 | 294 | ||
232 | spin_lock_init(&kvm->lock); | 295 | spin_lock_init(&kvm->lock); |
233 | INIT_LIST_HEAD(&kvm->active_mmu_pages); | 296 | INIT_LIST_HEAD(&kvm->active_mmu_pages); |
@@ -243,7 +306,11 @@ static int kvm_dev_open(struct inode *inode, struct file *filp) | |||
243 | list_add(&kvm->vm_list, &vm_list); | 306 | list_add(&kvm->vm_list, &vm_list); |
244 | spin_unlock(&kvm_lock); | 307 | spin_unlock(&kvm_lock); |
245 | } | 308 | } |
246 | filp->private_data = kvm; | 309 | return kvm; |
310 | } | ||
311 | |||
312 | static int kvm_dev_open(struct inode *inode, struct file *filp) | ||
313 | { | ||
247 | return 0; | 314 | return 0; |
248 | } | 315 | } |
249 | 316 | ||
@@ -281,9 +348,10 @@ static void kvm_free_physmem(struct kvm *kvm) | |||
281 | 348 | ||
282 | static void kvm_free_vcpu(struct kvm_vcpu *vcpu) | 349 | static void kvm_free_vcpu(struct kvm_vcpu *vcpu) |
283 | { | 350 | { |
284 | if (!vcpu_load(vcpu->kvm, vcpu_slot(vcpu))) | 351 | if (!vcpu->vmcs) |
285 | return; | 352 | return; |
286 | 353 | ||
354 | vcpu_load(vcpu); | ||
287 | kvm_mmu_destroy(vcpu); | 355 | kvm_mmu_destroy(vcpu); |
288 | vcpu_put(vcpu); | 356 | vcpu_put(vcpu); |
289 | kvm_arch_ops->vcpu_free(vcpu); | 357 | kvm_arch_ops->vcpu_free(vcpu); |
@@ -299,14 +367,24 @@ static void kvm_free_vcpus(struct kvm *kvm) | |||
299 | 367 | ||
300 | static int kvm_dev_release(struct inode *inode, struct file *filp) | 368 | static int kvm_dev_release(struct inode *inode, struct file *filp) |
301 | { | 369 | { |
302 | struct kvm *kvm = filp->private_data; | 370 | return 0; |
371 | } | ||
303 | 372 | ||
373 | static void kvm_destroy_vm(struct kvm *kvm) | ||
374 | { | ||
304 | spin_lock(&kvm_lock); | 375 | spin_lock(&kvm_lock); |
305 | list_del(&kvm->vm_list); | 376 | list_del(&kvm->vm_list); |
306 | spin_unlock(&kvm_lock); | 377 | spin_unlock(&kvm_lock); |
307 | kvm_free_vcpus(kvm); | 378 | kvm_free_vcpus(kvm); |
308 | kvm_free_physmem(kvm); | 379 | kvm_free_physmem(kvm); |
309 | kfree(kvm); | 380 | kfree(kvm); |
381 | } | ||
382 | |||
383 | static int kvm_vm_release(struct inode *inode, struct file *filp) | ||
384 | { | ||
385 | struct kvm *kvm = filp->private_data; | ||
386 | |||
387 | kvm_destroy_vm(kvm); | ||
310 | return 0; | 388 | return 0; |
311 | } | 389 | } |
312 | 390 | ||
@@ -457,7 +535,7 @@ EXPORT_SYMBOL_GPL(set_cr4); | |||
457 | void set_cr3(struct kvm_vcpu *vcpu, unsigned long cr3) | 535 | void set_cr3(struct kvm_vcpu *vcpu, unsigned long cr3) |
458 | { | 536 | { |
459 | if (is_long_mode(vcpu)) { | 537 | if (is_long_mode(vcpu)) { |
460 | if ( cr3 & CR3_L_MODE_RESEVED_BITS) { | 538 | if (cr3 & CR3_L_MODE_RESEVED_BITS) { |
461 | printk(KERN_DEBUG "set_cr3: #GP, reserved bits\n"); | 539 | printk(KERN_DEBUG "set_cr3: #GP, reserved bits\n"); |
462 | inject_gp(vcpu); | 540 | inject_gp(vcpu); |
463 | return; | 541 | return; |
@@ -533,55 +611,11 @@ void fx_init(struct kvm_vcpu *vcpu) | |||
533 | } | 611 | } |
534 | EXPORT_SYMBOL_GPL(fx_init); | 612 | EXPORT_SYMBOL_GPL(fx_init); |
535 | 613 | ||
536 | /* | 614 | static void do_remove_write_access(struct kvm_vcpu *vcpu, int slot) |
537 | * Creates some virtual cpus. Good luck creating more than one. | ||
538 | */ | ||
539 | static int kvm_dev_ioctl_create_vcpu(struct kvm *kvm, int n) | ||
540 | { | 615 | { |
541 | int r; | 616 | spin_lock(&vcpu->kvm->lock); |
542 | struct kvm_vcpu *vcpu; | 617 | kvm_mmu_slot_remove_write_access(vcpu, slot); |
543 | 618 | spin_unlock(&vcpu->kvm->lock); | |
544 | r = -EINVAL; | ||
545 | if (!valid_vcpu(n)) | ||
546 | goto out; | ||
547 | |||
548 | vcpu = &kvm->vcpus[n]; | ||
549 | |||
550 | mutex_lock(&vcpu->mutex); | ||
551 | |||
552 | if (vcpu->vmcs) { | ||
553 | mutex_unlock(&vcpu->mutex); | ||
554 | return -EEXIST; | ||
555 | } | ||
556 | |||
557 | vcpu->host_fx_image = (char*)ALIGN((hva_t)vcpu->fx_buf, | ||
558 | FX_IMAGE_ALIGN); | ||
559 | vcpu->guest_fx_image = vcpu->host_fx_image + FX_IMAGE_SIZE; | ||
560 | |||
561 | r = kvm_arch_ops->vcpu_create(vcpu); | ||
562 | if (r < 0) | ||
563 | goto out_free_vcpus; | ||
564 | |||
565 | r = kvm_mmu_create(vcpu); | ||
566 | if (r < 0) | ||
567 | goto out_free_vcpus; | ||
568 | |||
569 | kvm_arch_ops->vcpu_load(vcpu); | ||
570 | r = kvm_mmu_setup(vcpu); | ||
571 | if (r >= 0) | ||
572 | r = kvm_arch_ops->vcpu_setup(vcpu); | ||
573 | vcpu_put(vcpu); | ||
574 | |||
575 | if (r < 0) | ||
576 | goto out_free_vcpus; | ||
577 | |||
578 | return 0; | ||
579 | |||
580 | out_free_vcpus: | ||
581 | kvm_free_vcpu(vcpu); | ||
582 | mutex_unlock(&vcpu->mutex); | ||
583 | out: | ||
584 | return r; | ||
585 | } | 619 | } |
586 | 620 | ||
587 | /* | 621 | /* |
@@ -590,8 +624,8 @@ out: | |||
590 | * | 624 | * |
591 | * Discontiguous memory is allowed, mostly for framebuffers. | 625 | * Discontiguous memory is allowed, mostly for framebuffers. |
592 | */ | 626 | */ |
593 | static int kvm_dev_ioctl_set_memory_region(struct kvm *kvm, | 627 | static int kvm_vm_ioctl_set_memory_region(struct kvm *kvm, |
594 | struct kvm_memory_region *mem) | 628 | struct kvm_memory_region *mem) |
595 | { | 629 | { |
596 | int r; | 630 | int r; |
597 | gfn_t base_gfn; | 631 | gfn_t base_gfn; |
@@ -674,7 +708,7 @@ raced: | |||
674 | | __GFP_ZERO); | 708 | | __GFP_ZERO); |
675 | if (!new.phys_mem[i]) | 709 | if (!new.phys_mem[i]) |
676 | goto out_free; | 710 | goto out_free; |
677 | new.phys_mem[i]->private = 0; | 711 | set_page_private(new.phys_mem[i],0); |
678 | } | 712 | } |
679 | } | 713 | } |
680 | 714 | ||
@@ -711,9 +745,11 @@ raced: | |||
711 | for (i = 0; i < KVM_MAX_VCPUS; ++i) { | 745 | for (i = 0; i < KVM_MAX_VCPUS; ++i) { |
712 | struct kvm_vcpu *vcpu; | 746 | struct kvm_vcpu *vcpu; |
713 | 747 | ||
714 | vcpu = vcpu_load(kvm, i); | 748 | vcpu = vcpu_load_slot(kvm, i); |
715 | if (!vcpu) | 749 | if (!vcpu) |
716 | continue; | 750 | continue; |
751 | if (new.flags & KVM_MEM_LOG_DIRTY_PAGES) | ||
752 | do_remove_write_access(vcpu, mem->slot); | ||
717 | kvm_mmu_reset_context(vcpu); | 753 | kvm_mmu_reset_context(vcpu); |
718 | vcpu_put(vcpu); | 754 | vcpu_put(vcpu); |
719 | } | 755 | } |
@@ -729,18 +765,11 @@ out: | |||
729 | return r; | 765 | return r; |
730 | } | 766 | } |
731 | 767 | ||
732 | static void do_remove_write_access(struct kvm_vcpu *vcpu, int slot) | ||
733 | { | ||
734 | spin_lock(&vcpu->kvm->lock); | ||
735 | kvm_mmu_slot_remove_write_access(vcpu, slot); | ||
736 | spin_unlock(&vcpu->kvm->lock); | ||
737 | } | ||
738 | |||
739 | /* | 768 | /* |
740 | * Get (and clear) the dirty memory log for a memory slot. | 769 | * Get (and clear) the dirty memory log for a memory slot. |
741 | */ | 770 | */ |
742 | static int kvm_dev_ioctl_get_dirty_log(struct kvm *kvm, | 771 | static int kvm_vm_ioctl_get_dirty_log(struct kvm *kvm, |
743 | struct kvm_dirty_log *log) | 772 | struct kvm_dirty_log *log) |
744 | { | 773 | { |
745 | struct kvm_memory_slot *memslot; | 774 | struct kvm_memory_slot *memslot; |
746 | int r, i; | 775 | int r, i; |
@@ -765,21 +794,21 @@ static int kvm_dev_ioctl_get_dirty_log(struct kvm *kvm, | |||
765 | if (!memslot->dirty_bitmap) | 794 | if (!memslot->dirty_bitmap) |
766 | goto out; | 795 | goto out; |
767 | 796 | ||
768 | n = ALIGN(memslot->npages, 8) / 8; | 797 | n = ALIGN(memslot->npages, BITS_PER_LONG) / 8; |
769 | 798 | ||
770 | for (i = 0; !any && i < n; ++i) | 799 | for (i = 0; !any && i < n/sizeof(long); ++i) |
771 | any = memslot->dirty_bitmap[i]; | 800 | any = memslot->dirty_bitmap[i]; |
772 | 801 | ||
773 | r = -EFAULT; | 802 | r = -EFAULT; |
774 | if (copy_to_user(log->dirty_bitmap, memslot->dirty_bitmap, n)) | 803 | if (copy_to_user(log->dirty_bitmap, memslot->dirty_bitmap, n)) |
775 | goto out; | 804 | goto out; |
776 | 805 | ||
777 | |||
778 | if (any) { | 806 | if (any) { |
779 | cleared = 0; | 807 | cleared = 0; |
780 | for (i = 0; i < KVM_MAX_VCPUS; ++i) { | 808 | for (i = 0; i < KVM_MAX_VCPUS; ++i) { |
781 | struct kvm_vcpu *vcpu = vcpu_load(kvm, i); | 809 | struct kvm_vcpu *vcpu; |
782 | 810 | ||
811 | vcpu = vcpu_load_slot(kvm, i); | ||
783 | if (!vcpu) | 812 | if (!vcpu) |
784 | continue; | 813 | continue; |
785 | if (!cleared) { | 814 | if (!cleared) { |
@@ -903,8 +932,9 @@ static int emulator_read_emulated(unsigned long addr, | |||
903 | return X86EMUL_CONTINUE; | 932 | return X86EMUL_CONTINUE; |
904 | else { | 933 | else { |
905 | gpa_t gpa = vcpu->mmu.gva_to_gpa(vcpu, addr); | 934 | gpa_t gpa = vcpu->mmu.gva_to_gpa(vcpu, addr); |
935 | |||
906 | if (gpa == UNMAPPED_GVA) | 936 | if (gpa == UNMAPPED_GVA) |
907 | return vcpu_printf(vcpu, "not present\n"), X86EMUL_PROPAGATE_FAULT; | 937 | return X86EMUL_PROPAGATE_FAULT; |
908 | vcpu->mmio_needed = 1; | 938 | vcpu->mmio_needed = 1; |
909 | vcpu->mmio_phys_addr = gpa; | 939 | vcpu->mmio_phys_addr = gpa; |
910 | vcpu->mmio_size = bytes; | 940 | vcpu->mmio_size = bytes; |
@@ -928,6 +958,7 @@ static int emulator_write_phys(struct kvm_vcpu *vcpu, gpa_t gpa, | |||
928 | return 0; | 958 | return 0; |
929 | page = gfn_to_page(m, gpa >> PAGE_SHIFT); | 959 | page = gfn_to_page(m, gpa >> PAGE_SHIFT); |
930 | kvm_mmu_pre_write(vcpu, gpa, bytes); | 960 | kvm_mmu_pre_write(vcpu, gpa, bytes); |
961 | mark_page_dirty(vcpu->kvm, gpa >> PAGE_SHIFT); | ||
931 | virt = kmap_atomic(page, KM_USER0); | 962 | virt = kmap_atomic(page, KM_USER0); |
932 | memcpy(virt + offset_in_page(gpa), &val, bytes); | 963 | memcpy(virt + offset_in_page(gpa), &val, bytes); |
933 | kunmap_atomic(virt, KM_USER0); | 964 | kunmap_atomic(virt, KM_USER0); |
@@ -1142,6 +1173,42 @@ int emulate_instruction(struct kvm_vcpu *vcpu, | |||
1142 | } | 1173 | } |
1143 | EXPORT_SYMBOL_GPL(emulate_instruction); | 1174 | EXPORT_SYMBOL_GPL(emulate_instruction); |
1144 | 1175 | ||
1176 | int kvm_hypercall(struct kvm_vcpu *vcpu, struct kvm_run *run) | ||
1177 | { | ||
1178 | unsigned long nr, a0, a1, a2, a3, a4, a5, ret; | ||
1179 | |||
1180 | kvm_arch_ops->decache_regs(vcpu); | ||
1181 | ret = -KVM_EINVAL; | ||
1182 | #ifdef CONFIG_X86_64 | ||
1183 | if (is_long_mode(vcpu)) { | ||
1184 | nr = vcpu->regs[VCPU_REGS_RAX]; | ||
1185 | a0 = vcpu->regs[VCPU_REGS_RDI]; | ||
1186 | a1 = vcpu->regs[VCPU_REGS_RSI]; | ||
1187 | a2 = vcpu->regs[VCPU_REGS_RDX]; | ||
1188 | a3 = vcpu->regs[VCPU_REGS_RCX]; | ||
1189 | a4 = vcpu->regs[VCPU_REGS_R8]; | ||
1190 | a5 = vcpu->regs[VCPU_REGS_R9]; | ||
1191 | } else | ||
1192 | #endif | ||
1193 | { | ||
1194 | nr = vcpu->regs[VCPU_REGS_RBX] & -1u; | ||
1195 | a0 = vcpu->regs[VCPU_REGS_RAX] & -1u; | ||
1196 | a1 = vcpu->regs[VCPU_REGS_RCX] & -1u; | ||
1197 | a2 = vcpu->regs[VCPU_REGS_RDX] & -1u; | ||
1198 | a3 = vcpu->regs[VCPU_REGS_RSI] & -1u; | ||
1199 | a4 = vcpu->regs[VCPU_REGS_RDI] & -1u; | ||
1200 | a5 = vcpu->regs[VCPU_REGS_RBP] & -1u; | ||
1201 | } | ||
1202 | switch (nr) { | ||
1203 | default: | ||
1204 | ; | ||
1205 | } | ||
1206 | vcpu->regs[VCPU_REGS_RAX] = ret; | ||
1207 | kvm_arch_ops->cache_regs(vcpu); | ||
1208 | return 1; | ||
1209 | } | ||
1210 | EXPORT_SYMBOL_GPL(kvm_hypercall); | ||
1211 | |||
1145 | static u64 mk_cr_64(u64 curr_cr, u32 new_val) | 1212 | static u64 mk_cr_64(u64 curr_cr, u32 new_val) |
1146 | { | 1213 | { |
1147 | return (curr_cr & ~((1ULL << 32) - 1)) | new_val; | 1214 | return (curr_cr & ~((1ULL << 32) - 1)) | new_val; |
@@ -1208,6 +1275,75 @@ void realmode_set_cr(struct kvm_vcpu *vcpu, int cr, unsigned long val, | |||
1208 | } | 1275 | } |
1209 | } | 1276 | } |
1210 | 1277 | ||
1278 | /* | ||
1279 | * Register the para guest with the host: | ||
1280 | */ | ||
1281 | static int vcpu_register_para(struct kvm_vcpu *vcpu, gpa_t para_state_gpa) | ||
1282 | { | ||
1283 | struct kvm_vcpu_para_state *para_state; | ||
1284 | hpa_t para_state_hpa, hypercall_hpa; | ||
1285 | struct page *para_state_page; | ||
1286 | unsigned char *hypercall; | ||
1287 | gpa_t hypercall_gpa; | ||
1288 | |||
1289 | printk(KERN_DEBUG "kvm: guest trying to enter paravirtual mode\n"); | ||
1290 | printk(KERN_DEBUG ".... para_state_gpa: %08Lx\n", para_state_gpa); | ||
1291 | |||
1292 | /* | ||
1293 | * Needs to be page aligned: | ||
1294 | */ | ||
1295 | if (para_state_gpa != PAGE_ALIGN(para_state_gpa)) | ||
1296 | goto err_gp; | ||
1297 | |||
1298 | para_state_hpa = gpa_to_hpa(vcpu, para_state_gpa); | ||
1299 | printk(KERN_DEBUG ".... para_state_hpa: %08Lx\n", para_state_hpa); | ||
1300 | if (is_error_hpa(para_state_hpa)) | ||
1301 | goto err_gp; | ||
1302 | |||
1303 | mark_page_dirty(vcpu->kvm, para_state_gpa >> PAGE_SHIFT); | ||
1304 | para_state_page = pfn_to_page(para_state_hpa >> PAGE_SHIFT); | ||
1305 | para_state = kmap_atomic(para_state_page, KM_USER0); | ||
1306 | |||
1307 | printk(KERN_DEBUG ".... guest version: %d\n", para_state->guest_version); | ||
1308 | printk(KERN_DEBUG ".... size: %d\n", para_state->size); | ||
1309 | |||
1310 | para_state->host_version = KVM_PARA_API_VERSION; | ||
1311 | /* | ||
1312 | * We cannot support guests that try to register themselves | ||
1313 | * with a newer API version than the host supports: | ||
1314 | */ | ||
1315 | if (para_state->guest_version > KVM_PARA_API_VERSION) { | ||
1316 | para_state->ret = -KVM_EINVAL; | ||
1317 | goto err_kunmap_skip; | ||
1318 | } | ||
1319 | |||
1320 | hypercall_gpa = para_state->hypercall_gpa; | ||
1321 | hypercall_hpa = gpa_to_hpa(vcpu, hypercall_gpa); | ||
1322 | printk(KERN_DEBUG ".... hypercall_hpa: %08Lx\n", hypercall_hpa); | ||
1323 | if (is_error_hpa(hypercall_hpa)) { | ||
1324 | para_state->ret = -KVM_EINVAL; | ||
1325 | goto err_kunmap_skip; | ||
1326 | } | ||
1327 | |||
1328 | printk(KERN_DEBUG "kvm: para guest successfully registered.\n"); | ||
1329 | vcpu->para_state_page = para_state_page; | ||
1330 | vcpu->para_state_gpa = para_state_gpa; | ||
1331 | vcpu->hypercall_gpa = hypercall_gpa; | ||
1332 | |||
1333 | mark_page_dirty(vcpu->kvm, hypercall_gpa >> PAGE_SHIFT); | ||
1334 | hypercall = kmap_atomic(pfn_to_page(hypercall_hpa >> PAGE_SHIFT), | ||
1335 | KM_USER1) + (hypercall_hpa & ~PAGE_MASK); | ||
1336 | kvm_arch_ops->patch_hypercall(vcpu, hypercall); | ||
1337 | kunmap_atomic(hypercall, KM_USER1); | ||
1338 | |||
1339 | para_state->ret = 0; | ||
1340 | err_kunmap_skip: | ||
1341 | kunmap_atomic(para_state, KM_USER0); | ||
1342 | return 0; | ||
1343 | err_gp: | ||
1344 | return 1; | ||
1345 | } | ||
1346 | |||
1211 | int kvm_get_msr_common(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata) | 1347 | int kvm_get_msr_common(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata) |
1212 | { | 1348 | { |
1213 | u64 data; | 1349 | u64 data; |
@@ -1316,6 +1452,12 @@ int kvm_set_msr_common(struct kvm_vcpu *vcpu, u32 msr, u64 data) | |||
1316 | case MSR_IA32_MISC_ENABLE: | 1452 | case MSR_IA32_MISC_ENABLE: |
1317 | vcpu->ia32_misc_enable_msr = data; | 1453 | vcpu->ia32_misc_enable_msr = data; |
1318 | break; | 1454 | break; |
1455 | /* | ||
1456 | * This is the 'probe whether the host is KVM' logic: | ||
1457 | */ | ||
1458 | case MSR_KVM_API_MAGIC: | ||
1459 | return vcpu_register_para(vcpu, data); | ||
1460 | |||
1319 | default: | 1461 | default: |
1320 | printk(KERN_ERR "kvm: unhandled wrmsr: 0x%x\n", msr); | 1462 | printk(KERN_ERR "kvm: unhandled wrmsr: 0x%x\n", msr); |
1321 | return 1; | 1463 | return 1; |
@@ -1338,8 +1480,7 @@ void kvm_resched(struct kvm_vcpu *vcpu) | |||
1338 | { | 1480 | { |
1339 | vcpu_put(vcpu); | 1481 | vcpu_put(vcpu); |
1340 | cond_resched(); | 1482 | cond_resched(); |
1341 | /* Cannot fail - no vcpu unplug yet. */ | 1483 | vcpu_load(vcpu); |
1342 | vcpu_load(vcpu->kvm, vcpu_slot(vcpu)); | ||
1343 | } | 1484 | } |
1344 | EXPORT_SYMBOL_GPL(kvm_resched); | 1485 | EXPORT_SYMBOL_GPL(kvm_resched); |
1345 | 1486 | ||
@@ -1361,17 +1502,11 @@ void save_msrs(struct vmx_msr_entry *e, int n) | |||
1361 | } | 1502 | } |
1362 | EXPORT_SYMBOL_GPL(save_msrs); | 1503 | EXPORT_SYMBOL_GPL(save_msrs); |
1363 | 1504 | ||
1364 | static int kvm_dev_ioctl_run(struct kvm *kvm, struct kvm_run *kvm_run) | 1505 | static int kvm_vcpu_ioctl_run(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run) |
1365 | { | 1506 | { |
1366 | struct kvm_vcpu *vcpu; | ||
1367 | int r; | 1507 | int r; |
1368 | 1508 | ||
1369 | if (!valid_vcpu(kvm_run->vcpu)) | 1509 | vcpu_load(vcpu); |
1370 | return -EINVAL; | ||
1371 | |||
1372 | vcpu = vcpu_load(kvm, kvm_run->vcpu); | ||
1373 | if (!vcpu) | ||
1374 | return -ENOENT; | ||
1375 | 1510 | ||
1376 | /* re-sync apic's tpr */ | 1511 | /* re-sync apic's tpr */ |
1377 | vcpu->cr8 = kvm_run->cr8; | 1512 | vcpu->cr8 = kvm_run->cr8; |
@@ -1394,16 +1529,10 @@ static int kvm_dev_ioctl_run(struct kvm *kvm, struct kvm_run *kvm_run) | |||
1394 | return r; | 1529 | return r; |
1395 | } | 1530 | } |
1396 | 1531 | ||
1397 | static int kvm_dev_ioctl_get_regs(struct kvm *kvm, struct kvm_regs *regs) | 1532 | static int kvm_vcpu_ioctl_get_regs(struct kvm_vcpu *vcpu, |
1533 | struct kvm_regs *regs) | ||
1398 | { | 1534 | { |
1399 | struct kvm_vcpu *vcpu; | 1535 | vcpu_load(vcpu); |
1400 | |||
1401 | if (!valid_vcpu(regs->vcpu)) | ||
1402 | return -EINVAL; | ||
1403 | |||
1404 | vcpu = vcpu_load(kvm, regs->vcpu); | ||
1405 | if (!vcpu) | ||
1406 | return -ENOENT; | ||
1407 | 1536 | ||
1408 | kvm_arch_ops->cache_regs(vcpu); | 1537 | kvm_arch_ops->cache_regs(vcpu); |
1409 | 1538 | ||
@@ -1440,16 +1569,10 @@ static int kvm_dev_ioctl_get_regs(struct kvm *kvm, struct kvm_regs *regs) | |||
1440 | return 0; | 1569 | return 0; |
1441 | } | 1570 | } |
1442 | 1571 | ||
1443 | static int kvm_dev_ioctl_set_regs(struct kvm *kvm, struct kvm_regs *regs) | 1572 | static int kvm_vcpu_ioctl_set_regs(struct kvm_vcpu *vcpu, |
1573 | struct kvm_regs *regs) | ||
1444 | { | 1574 | { |
1445 | struct kvm_vcpu *vcpu; | 1575 | vcpu_load(vcpu); |
1446 | |||
1447 | if (!valid_vcpu(regs->vcpu)) | ||
1448 | return -EINVAL; | ||
1449 | |||
1450 | vcpu = vcpu_load(kvm, regs->vcpu); | ||
1451 | if (!vcpu) | ||
1452 | return -ENOENT; | ||
1453 | 1576 | ||
1454 | vcpu->regs[VCPU_REGS_RAX] = regs->rax; | 1577 | vcpu->regs[VCPU_REGS_RAX] = regs->rax; |
1455 | vcpu->regs[VCPU_REGS_RBX] = regs->rbx; | 1578 | vcpu->regs[VCPU_REGS_RBX] = regs->rbx; |
@@ -1486,16 +1609,12 @@ static void get_segment(struct kvm_vcpu *vcpu, | |||
1486 | return kvm_arch_ops->get_segment(vcpu, var, seg); | 1609 | return kvm_arch_ops->get_segment(vcpu, var, seg); |
1487 | } | 1610 | } |
1488 | 1611 | ||
1489 | static int kvm_dev_ioctl_get_sregs(struct kvm *kvm, struct kvm_sregs *sregs) | 1612 | static int kvm_vcpu_ioctl_get_sregs(struct kvm_vcpu *vcpu, |
1613 | struct kvm_sregs *sregs) | ||
1490 | { | 1614 | { |
1491 | struct kvm_vcpu *vcpu; | ||
1492 | struct descriptor_table dt; | 1615 | struct descriptor_table dt; |
1493 | 1616 | ||
1494 | if (!valid_vcpu(sregs->vcpu)) | 1617 | vcpu_load(vcpu); |
1495 | return -EINVAL; | ||
1496 | vcpu = vcpu_load(kvm, sregs->vcpu); | ||
1497 | if (!vcpu) | ||
1498 | return -ENOENT; | ||
1499 | 1618 | ||
1500 | get_segment(vcpu, &sregs->cs, VCPU_SREG_CS); | 1619 | get_segment(vcpu, &sregs->cs, VCPU_SREG_CS); |
1501 | get_segment(vcpu, &sregs->ds, VCPU_SREG_DS); | 1620 | get_segment(vcpu, &sregs->ds, VCPU_SREG_DS); |
@@ -1537,18 +1656,14 @@ static void set_segment(struct kvm_vcpu *vcpu, | |||
1537 | return kvm_arch_ops->set_segment(vcpu, var, seg); | 1656 | return kvm_arch_ops->set_segment(vcpu, var, seg); |
1538 | } | 1657 | } |
1539 | 1658 | ||
1540 | static int kvm_dev_ioctl_set_sregs(struct kvm *kvm, struct kvm_sregs *sregs) | 1659 | static int kvm_vcpu_ioctl_set_sregs(struct kvm_vcpu *vcpu, |
1660 | struct kvm_sregs *sregs) | ||
1541 | { | 1661 | { |
1542 | struct kvm_vcpu *vcpu; | ||
1543 | int mmu_reset_needed = 0; | 1662 | int mmu_reset_needed = 0; |
1544 | int i; | 1663 | int i; |
1545 | struct descriptor_table dt; | 1664 | struct descriptor_table dt; |
1546 | 1665 | ||
1547 | if (!valid_vcpu(sregs->vcpu)) | 1666 | vcpu_load(vcpu); |
1548 | return -EINVAL; | ||
1549 | vcpu = vcpu_load(kvm, sregs->vcpu); | ||
1550 | if (!vcpu) | ||
1551 | return -ENOENT; | ||
1552 | 1667 | ||
1553 | set_segment(vcpu, &sregs->cs, VCPU_SREG_CS); | 1668 | set_segment(vcpu, &sregs->cs, VCPU_SREG_CS); |
1554 | set_segment(vcpu, &sregs->ds, VCPU_SREG_DS); | 1669 | set_segment(vcpu, &sregs->ds, VCPU_SREG_DS); |
@@ -1654,20 +1769,14 @@ static int do_set_msr(struct kvm_vcpu *vcpu, unsigned index, u64 *data) | |||
1654 | * | 1769 | * |
1655 | * @return number of msrs set successfully. | 1770 | * @return number of msrs set successfully. |
1656 | */ | 1771 | */ |
1657 | static int __msr_io(struct kvm *kvm, struct kvm_msrs *msrs, | 1772 | static int __msr_io(struct kvm_vcpu *vcpu, struct kvm_msrs *msrs, |
1658 | struct kvm_msr_entry *entries, | 1773 | struct kvm_msr_entry *entries, |
1659 | int (*do_msr)(struct kvm_vcpu *vcpu, | 1774 | int (*do_msr)(struct kvm_vcpu *vcpu, |
1660 | unsigned index, u64 *data)) | 1775 | unsigned index, u64 *data)) |
1661 | { | 1776 | { |
1662 | struct kvm_vcpu *vcpu; | ||
1663 | int i; | 1777 | int i; |
1664 | 1778 | ||
1665 | if (!valid_vcpu(msrs->vcpu)) | 1779 | vcpu_load(vcpu); |
1666 | return -EINVAL; | ||
1667 | |||
1668 | vcpu = vcpu_load(kvm, msrs->vcpu); | ||
1669 | if (!vcpu) | ||
1670 | return -ENOENT; | ||
1671 | 1780 | ||
1672 | for (i = 0; i < msrs->nmsrs; ++i) | 1781 | for (i = 0; i < msrs->nmsrs; ++i) |
1673 | if (do_msr(vcpu, entries[i].index, &entries[i].data)) | 1782 | if (do_msr(vcpu, entries[i].index, &entries[i].data)) |
@@ -1683,7 +1792,7 @@ static int __msr_io(struct kvm *kvm, struct kvm_msrs *msrs, | |||
1683 | * | 1792 | * |
1684 | * @return number of msrs set successfully. | 1793 | * @return number of msrs set successfully. |
1685 | */ | 1794 | */ |
1686 | static int msr_io(struct kvm *kvm, struct kvm_msrs __user *user_msrs, | 1795 | static int msr_io(struct kvm_vcpu *vcpu, struct kvm_msrs __user *user_msrs, |
1687 | int (*do_msr)(struct kvm_vcpu *vcpu, | 1796 | int (*do_msr)(struct kvm_vcpu *vcpu, |
1688 | unsigned index, u64 *data), | 1797 | unsigned index, u64 *data), |
1689 | int writeback) | 1798 | int writeback) |
@@ -1711,7 +1820,7 @@ static int msr_io(struct kvm *kvm, struct kvm_msrs __user *user_msrs, | |||
1711 | if (copy_from_user(entries, user_msrs->entries, size)) | 1820 | if (copy_from_user(entries, user_msrs->entries, size)) |
1712 | goto out_free; | 1821 | goto out_free; |
1713 | 1822 | ||
1714 | r = n = __msr_io(kvm, &msrs, entries, do_msr); | 1823 | r = n = __msr_io(vcpu, &msrs, entries, do_msr); |
1715 | if (r < 0) | 1824 | if (r < 0) |
1716 | goto out_free; | 1825 | goto out_free; |
1717 | 1826 | ||
@@ -1730,38 +1839,31 @@ out: | |||
1730 | /* | 1839 | /* |
1731 | * Translate a guest virtual address to a guest physical address. | 1840 | * Translate a guest virtual address to a guest physical address. |
1732 | */ | 1841 | */ |
1733 | static int kvm_dev_ioctl_translate(struct kvm *kvm, struct kvm_translation *tr) | 1842 | static int kvm_vcpu_ioctl_translate(struct kvm_vcpu *vcpu, |
1843 | struct kvm_translation *tr) | ||
1734 | { | 1844 | { |
1735 | unsigned long vaddr = tr->linear_address; | 1845 | unsigned long vaddr = tr->linear_address; |
1736 | struct kvm_vcpu *vcpu; | ||
1737 | gpa_t gpa; | 1846 | gpa_t gpa; |
1738 | 1847 | ||
1739 | vcpu = vcpu_load(kvm, tr->vcpu); | 1848 | vcpu_load(vcpu); |
1740 | if (!vcpu) | 1849 | spin_lock(&vcpu->kvm->lock); |
1741 | return -ENOENT; | ||
1742 | spin_lock(&kvm->lock); | ||
1743 | gpa = vcpu->mmu.gva_to_gpa(vcpu, vaddr); | 1850 | gpa = vcpu->mmu.gva_to_gpa(vcpu, vaddr); |
1744 | tr->physical_address = gpa; | 1851 | tr->physical_address = gpa; |
1745 | tr->valid = gpa != UNMAPPED_GVA; | 1852 | tr->valid = gpa != UNMAPPED_GVA; |
1746 | tr->writeable = 1; | 1853 | tr->writeable = 1; |
1747 | tr->usermode = 0; | 1854 | tr->usermode = 0; |
1748 | spin_unlock(&kvm->lock); | 1855 | spin_unlock(&vcpu->kvm->lock); |
1749 | vcpu_put(vcpu); | 1856 | vcpu_put(vcpu); |
1750 | 1857 | ||
1751 | return 0; | 1858 | return 0; |
1752 | } | 1859 | } |
1753 | 1860 | ||
1754 | static int kvm_dev_ioctl_interrupt(struct kvm *kvm, struct kvm_interrupt *irq) | 1861 | static int kvm_vcpu_ioctl_interrupt(struct kvm_vcpu *vcpu, |
1862 | struct kvm_interrupt *irq) | ||
1755 | { | 1863 | { |
1756 | struct kvm_vcpu *vcpu; | ||
1757 | |||
1758 | if (!valid_vcpu(irq->vcpu)) | ||
1759 | return -EINVAL; | ||
1760 | if (irq->irq < 0 || irq->irq >= 256) | 1864 | if (irq->irq < 0 || irq->irq >= 256) |
1761 | return -EINVAL; | 1865 | return -EINVAL; |
1762 | vcpu = vcpu_load(kvm, irq->vcpu); | 1866 | vcpu_load(vcpu); |
1763 | if (!vcpu) | ||
1764 | return -ENOENT; | ||
1765 | 1867 | ||
1766 | set_bit(irq->irq, vcpu->irq_pending); | 1868 | set_bit(irq->irq, vcpu->irq_pending); |
1767 | set_bit(irq->irq / BITS_PER_LONG, &vcpu->irq_summary); | 1869 | set_bit(irq->irq / BITS_PER_LONG, &vcpu->irq_summary); |
@@ -1771,17 +1873,12 @@ static int kvm_dev_ioctl_interrupt(struct kvm *kvm, struct kvm_interrupt *irq) | |||
1771 | return 0; | 1873 | return 0; |
1772 | } | 1874 | } |
1773 | 1875 | ||
1774 | static int kvm_dev_ioctl_debug_guest(struct kvm *kvm, | 1876 | static int kvm_vcpu_ioctl_debug_guest(struct kvm_vcpu *vcpu, |
1775 | struct kvm_debug_guest *dbg) | 1877 | struct kvm_debug_guest *dbg) |
1776 | { | 1878 | { |
1777 | struct kvm_vcpu *vcpu; | ||
1778 | int r; | 1879 | int r; |
1779 | 1880 | ||
1780 | if (!valid_vcpu(dbg->vcpu)) | 1881 | vcpu_load(vcpu); |
1781 | return -EINVAL; | ||
1782 | vcpu = vcpu_load(kvm, dbg->vcpu); | ||
1783 | if (!vcpu) | ||
1784 | return -ENOENT; | ||
1785 | 1882 | ||
1786 | r = kvm_arch_ops->set_guest_debug(vcpu, dbg); | 1883 | r = kvm_arch_ops->set_guest_debug(vcpu, dbg); |
1787 | 1884 | ||
@@ -1790,30 +1887,129 @@ static int kvm_dev_ioctl_debug_guest(struct kvm *kvm, | |||
1790 | return r; | 1887 | return r; |
1791 | } | 1888 | } |
1792 | 1889 | ||
1793 | static long kvm_dev_ioctl(struct file *filp, | 1890 | static int kvm_vcpu_release(struct inode *inode, struct file *filp) |
1794 | unsigned int ioctl, unsigned long arg) | ||
1795 | { | 1891 | { |
1796 | struct kvm *kvm = filp->private_data; | 1892 | struct kvm_vcpu *vcpu = filp->private_data; |
1893 | |||
1894 | fput(vcpu->kvm->filp); | ||
1895 | return 0; | ||
1896 | } | ||
1897 | |||
1898 | static struct file_operations kvm_vcpu_fops = { | ||
1899 | .release = kvm_vcpu_release, | ||
1900 | .unlocked_ioctl = kvm_vcpu_ioctl, | ||
1901 | .compat_ioctl = kvm_vcpu_ioctl, | ||
1902 | }; | ||
1903 | |||
1904 | /* | ||
1905 | * Allocates an inode for the vcpu. | ||
1906 | */ | ||
1907 | static int create_vcpu_fd(struct kvm_vcpu *vcpu) | ||
1908 | { | ||
1909 | int fd, r; | ||
1910 | struct inode *inode; | ||
1911 | struct file *file; | ||
1912 | |||
1913 | atomic_inc(&vcpu->kvm->filp->f_count); | ||
1914 | inode = kvmfs_inode(&kvm_vcpu_fops); | ||
1915 | if (IS_ERR(inode)) { | ||
1916 | r = PTR_ERR(inode); | ||
1917 | goto out1; | ||
1918 | } | ||
1919 | |||
1920 | file = kvmfs_file(inode, vcpu); | ||
1921 | if (IS_ERR(file)) { | ||
1922 | r = PTR_ERR(file); | ||
1923 | goto out2; | ||
1924 | } | ||
1925 | |||
1926 | r = get_unused_fd(); | ||
1927 | if (r < 0) | ||
1928 | goto out3; | ||
1929 | fd = r; | ||
1930 | fd_install(fd, file); | ||
1931 | |||
1932 | return fd; | ||
1933 | |||
1934 | out3: | ||
1935 | fput(file); | ||
1936 | out2: | ||
1937 | iput(inode); | ||
1938 | out1: | ||
1939 | fput(vcpu->kvm->filp); | ||
1940 | return r; | ||
1941 | } | ||
1942 | |||
1943 | /* | ||
1944 | * Creates some virtual cpus. Good luck creating more than one. | ||
1945 | */ | ||
1946 | static int kvm_vm_ioctl_create_vcpu(struct kvm *kvm, int n) | ||
1947 | { | ||
1948 | int r; | ||
1949 | struct kvm_vcpu *vcpu; | ||
1950 | |||
1951 | r = -EINVAL; | ||
1952 | if (!valid_vcpu(n)) | ||
1953 | goto out; | ||
1954 | |||
1955 | vcpu = &kvm->vcpus[n]; | ||
1956 | |||
1957 | mutex_lock(&vcpu->mutex); | ||
1958 | |||
1959 | if (vcpu->vmcs) { | ||
1960 | mutex_unlock(&vcpu->mutex); | ||
1961 | return -EEXIST; | ||
1962 | } | ||
1963 | |||
1964 | vcpu->host_fx_image = (char*)ALIGN((hva_t)vcpu->fx_buf, | ||
1965 | FX_IMAGE_ALIGN); | ||
1966 | vcpu->guest_fx_image = vcpu->host_fx_image + FX_IMAGE_SIZE; | ||
1967 | |||
1968 | r = kvm_arch_ops->vcpu_create(vcpu); | ||
1969 | if (r < 0) | ||
1970 | goto out_free_vcpus; | ||
1971 | |||
1972 | r = kvm_mmu_create(vcpu); | ||
1973 | if (r < 0) | ||
1974 | goto out_free_vcpus; | ||
1975 | |||
1976 | kvm_arch_ops->vcpu_load(vcpu); | ||
1977 | r = kvm_mmu_setup(vcpu); | ||
1978 | if (r >= 0) | ||
1979 | r = kvm_arch_ops->vcpu_setup(vcpu); | ||
1980 | vcpu_put(vcpu); | ||
1981 | |||
1982 | if (r < 0) | ||
1983 | goto out_free_vcpus; | ||
1984 | |||
1985 | r = create_vcpu_fd(vcpu); | ||
1986 | if (r < 0) | ||
1987 | goto out_free_vcpus; | ||
1988 | |||
1989 | return r; | ||
1990 | |||
1991 | out_free_vcpus: | ||
1992 | kvm_free_vcpu(vcpu); | ||
1993 | mutex_unlock(&vcpu->mutex); | ||
1994 | out: | ||
1995 | return r; | ||
1996 | } | ||
1997 | |||
1998 | static long kvm_vcpu_ioctl(struct file *filp, | ||
1999 | unsigned int ioctl, unsigned long arg) | ||
2000 | { | ||
2001 | struct kvm_vcpu *vcpu = filp->private_data; | ||
1797 | void __user *argp = (void __user *)arg; | 2002 | void __user *argp = (void __user *)arg; |
1798 | int r = -EINVAL; | 2003 | int r = -EINVAL; |
1799 | 2004 | ||
1800 | switch (ioctl) { | 2005 | switch (ioctl) { |
1801 | case KVM_GET_API_VERSION: | ||
1802 | r = KVM_API_VERSION; | ||
1803 | break; | ||
1804 | case KVM_CREATE_VCPU: { | ||
1805 | r = kvm_dev_ioctl_create_vcpu(kvm, arg); | ||
1806 | if (r) | ||
1807 | goto out; | ||
1808 | break; | ||
1809 | } | ||
1810 | case KVM_RUN: { | 2006 | case KVM_RUN: { |
1811 | struct kvm_run kvm_run; | 2007 | struct kvm_run kvm_run; |
1812 | 2008 | ||
1813 | r = -EFAULT; | 2009 | r = -EFAULT; |
1814 | if (copy_from_user(&kvm_run, argp, sizeof kvm_run)) | 2010 | if (copy_from_user(&kvm_run, argp, sizeof kvm_run)) |
1815 | goto out; | 2011 | goto out; |
1816 | r = kvm_dev_ioctl_run(kvm, &kvm_run); | 2012 | r = kvm_vcpu_ioctl_run(vcpu, &kvm_run); |
1817 | if (r < 0 && r != -EINTR) | 2013 | if (r < 0 && r != -EINTR) |
1818 | goto out; | 2014 | goto out; |
1819 | if (copy_to_user(argp, &kvm_run, sizeof kvm_run)) { | 2015 | if (copy_to_user(argp, &kvm_run, sizeof kvm_run)) { |
@@ -1825,10 +2021,8 @@ static long kvm_dev_ioctl(struct file *filp, | |||
1825 | case KVM_GET_REGS: { | 2021 | case KVM_GET_REGS: { |
1826 | struct kvm_regs kvm_regs; | 2022 | struct kvm_regs kvm_regs; |
1827 | 2023 | ||
1828 | r = -EFAULT; | 2024 | memset(&kvm_regs, 0, sizeof kvm_regs); |
1829 | if (copy_from_user(&kvm_regs, argp, sizeof kvm_regs)) | 2025 | r = kvm_vcpu_ioctl_get_regs(vcpu, &kvm_regs); |
1830 | goto out; | ||
1831 | r = kvm_dev_ioctl_get_regs(kvm, &kvm_regs); | ||
1832 | if (r) | 2026 | if (r) |
1833 | goto out; | 2027 | goto out; |
1834 | r = -EFAULT; | 2028 | r = -EFAULT; |
@@ -1843,7 +2037,7 @@ static long kvm_dev_ioctl(struct file *filp, | |||
1843 | r = -EFAULT; | 2037 | r = -EFAULT; |
1844 | if (copy_from_user(&kvm_regs, argp, sizeof kvm_regs)) | 2038 | if (copy_from_user(&kvm_regs, argp, sizeof kvm_regs)) |
1845 | goto out; | 2039 | goto out; |
1846 | r = kvm_dev_ioctl_set_regs(kvm, &kvm_regs); | 2040 | r = kvm_vcpu_ioctl_set_regs(vcpu, &kvm_regs); |
1847 | if (r) | 2041 | if (r) |
1848 | goto out; | 2042 | goto out; |
1849 | r = 0; | 2043 | r = 0; |
@@ -1852,10 +2046,8 @@ static long kvm_dev_ioctl(struct file *filp, | |||
1852 | case KVM_GET_SREGS: { | 2046 | case KVM_GET_SREGS: { |
1853 | struct kvm_sregs kvm_sregs; | 2047 | struct kvm_sregs kvm_sregs; |
1854 | 2048 | ||
1855 | r = -EFAULT; | 2049 | memset(&kvm_sregs, 0, sizeof kvm_sregs); |
1856 | if (copy_from_user(&kvm_sregs, argp, sizeof kvm_sregs)) | 2050 | r = kvm_vcpu_ioctl_get_sregs(vcpu, &kvm_sregs); |
1857 | goto out; | ||
1858 | r = kvm_dev_ioctl_get_sregs(kvm, &kvm_sregs); | ||
1859 | if (r) | 2051 | if (r) |
1860 | goto out; | 2052 | goto out; |
1861 | r = -EFAULT; | 2053 | r = -EFAULT; |
@@ -1870,7 +2062,7 @@ static long kvm_dev_ioctl(struct file *filp, | |||
1870 | r = -EFAULT; | 2062 | r = -EFAULT; |
1871 | if (copy_from_user(&kvm_sregs, argp, sizeof kvm_sregs)) | 2063 | if (copy_from_user(&kvm_sregs, argp, sizeof kvm_sregs)) |
1872 | goto out; | 2064 | goto out; |
1873 | r = kvm_dev_ioctl_set_sregs(kvm, &kvm_sregs); | 2065 | r = kvm_vcpu_ioctl_set_sregs(vcpu, &kvm_sregs); |
1874 | if (r) | 2066 | if (r) |
1875 | goto out; | 2067 | goto out; |
1876 | r = 0; | 2068 | r = 0; |
@@ -1882,7 +2074,7 @@ static long kvm_dev_ioctl(struct file *filp, | |||
1882 | r = -EFAULT; | 2074 | r = -EFAULT; |
1883 | if (copy_from_user(&tr, argp, sizeof tr)) | 2075 | if (copy_from_user(&tr, argp, sizeof tr)) |
1884 | goto out; | 2076 | goto out; |
1885 | r = kvm_dev_ioctl_translate(kvm, &tr); | 2077 | r = kvm_vcpu_ioctl_translate(vcpu, &tr); |
1886 | if (r) | 2078 | if (r) |
1887 | goto out; | 2079 | goto out; |
1888 | r = -EFAULT; | 2080 | r = -EFAULT; |
@@ -1897,7 +2089,7 @@ static long kvm_dev_ioctl(struct file *filp, | |||
1897 | r = -EFAULT; | 2089 | r = -EFAULT; |
1898 | if (copy_from_user(&irq, argp, sizeof irq)) | 2090 | if (copy_from_user(&irq, argp, sizeof irq)) |
1899 | goto out; | 2091 | goto out; |
1900 | r = kvm_dev_ioctl_interrupt(kvm, &irq); | 2092 | r = kvm_vcpu_ioctl_interrupt(vcpu, &irq); |
1901 | if (r) | 2093 | if (r) |
1902 | goto out; | 2094 | goto out; |
1903 | r = 0; | 2095 | r = 0; |
@@ -1909,19 +2101,45 @@ static long kvm_dev_ioctl(struct file *filp, | |||
1909 | r = -EFAULT; | 2101 | r = -EFAULT; |
1910 | if (copy_from_user(&dbg, argp, sizeof dbg)) | 2102 | if (copy_from_user(&dbg, argp, sizeof dbg)) |
1911 | goto out; | 2103 | goto out; |
1912 | r = kvm_dev_ioctl_debug_guest(kvm, &dbg); | 2104 | r = kvm_vcpu_ioctl_debug_guest(vcpu, &dbg); |
1913 | if (r) | 2105 | if (r) |
1914 | goto out; | 2106 | goto out; |
1915 | r = 0; | 2107 | r = 0; |
1916 | break; | 2108 | break; |
1917 | } | 2109 | } |
2110 | case KVM_GET_MSRS: | ||
2111 | r = msr_io(vcpu, argp, get_msr, 1); | ||
2112 | break; | ||
2113 | case KVM_SET_MSRS: | ||
2114 | r = msr_io(vcpu, argp, do_set_msr, 0); | ||
2115 | break; | ||
2116 | default: | ||
2117 | ; | ||
2118 | } | ||
2119 | out: | ||
2120 | return r; | ||
2121 | } | ||
2122 | |||
2123 | static long kvm_vm_ioctl(struct file *filp, | ||
2124 | unsigned int ioctl, unsigned long arg) | ||
2125 | { | ||
2126 | struct kvm *kvm = filp->private_data; | ||
2127 | void __user *argp = (void __user *)arg; | ||
2128 | int r = -EINVAL; | ||
2129 | |||
2130 | switch (ioctl) { | ||
2131 | case KVM_CREATE_VCPU: | ||
2132 | r = kvm_vm_ioctl_create_vcpu(kvm, arg); | ||
2133 | if (r < 0) | ||
2134 | goto out; | ||
2135 | break; | ||
1918 | case KVM_SET_MEMORY_REGION: { | 2136 | case KVM_SET_MEMORY_REGION: { |
1919 | struct kvm_memory_region kvm_mem; | 2137 | struct kvm_memory_region kvm_mem; |
1920 | 2138 | ||
1921 | r = -EFAULT; | 2139 | r = -EFAULT; |
1922 | if (copy_from_user(&kvm_mem, argp, sizeof kvm_mem)) | 2140 | if (copy_from_user(&kvm_mem, argp, sizeof kvm_mem)) |
1923 | goto out; | 2141 | goto out; |
1924 | r = kvm_dev_ioctl_set_memory_region(kvm, &kvm_mem); | 2142 | r = kvm_vm_ioctl_set_memory_region(kvm, &kvm_mem); |
1925 | if (r) | 2143 | if (r) |
1926 | goto out; | 2144 | goto out; |
1927 | break; | 2145 | break; |
@@ -1932,16 +2150,112 @@ static long kvm_dev_ioctl(struct file *filp, | |||
1932 | r = -EFAULT; | 2150 | r = -EFAULT; |
1933 | if (copy_from_user(&log, argp, sizeof log)) | 2151 | if (copy_from_user(&log, argp, sizeof log)) |
1934 | goto out; | 2152 | goto out; |
1935 | r = kvm_dev_ioctl_get_dirty_log(kvm, &log); | 2153 | r = kvm_vm_ioctl_get_dirty_log(kvm, &log); |
1936 | if (r) | 2154 | if (r) |
1937 | goto out; | 2155 | goto out; |
1938 | break; | 2156 | break; |
1939 | } | 2157 | } |
1940 | case KVM_GET_MSRS: | 2158 | default: |
1941 | r = msr_io(kvm, argp, get_msr, 1); | 2159 | ; |
2160 | } | ||
2161 | out: | ||
2162 | return r; | ||
2163 | } | ||
2164 | |||
2165 | static struct page *kvm_vm_nopage(struct vm_area_struct *vma, | ||
2166 | unsigned long address, | ||
2167 | int *type) | ||
2168 | { | ||
2169 | struct kvm *kvm = vma->vm_file->private_data; | ||
2170 | unsigned long pgoff; | ||
2171 | struct kvm_memory_slot *slot; | ||
2172 | struct page *page; | ||
2173 | |||
2174 | *type = VM_FAULT_MINOR; | ||
2175 | pgoff = ((address - vma->vm_start) >> PAGE_SHIFT) + vma->vm_pgoff; | ||
2176 | slot = gfn_to_memslot(kvm, pgoff); | ||
2177 | if (!slot) | ||
2178 | return NOPAGE_SIGBUS; | ||
2179 | page = gfn_to_page(slot, pgoff); | ||
2180 | if (!page) | ||
2181 | return NOPAGE_SIGBUS; | ||
2182 | get_page(page); | ||
2183 | return page; | ||
2184 | } | ||
2185 | |||
2186 | static struct vm_operations_struct kvm_vm_vm_ops = { | ||
2187 | .nopage = kvm_vm_nopage, | ||
2188 | }; | ||
2189 | |||
2190 | static int kvm_vm_mmap(struct file *file, struct vm_area_struct *vma) | ||
2191 | { | ||
2192 | vma->vm_ops = &kvm_vm_vm_ops; | ||
2193 | return 0; | ||
2194 | } | ||
2195 | |||
2196 | static struct file_operations kvm_vm_fops = { | ||
2197 | .release = kvm_vm_release, | ||
2198 | .unlocked_ioctl = kvm_vm_ioctl, | ||
2199 | .compat_ioctl = kvm_vm_ioctl, | ||
2200 | .mmap = kvm_vm_mmap, | ||
2201 | }; | ||
2202 | |||
2203 | static int kvm_dev_ioctl_create_vm(void) | ||
2204 | { | ||
2205 | int fd, r; | ||
2206 | struct inode *inode; | ||
2207 | struct file *file; | ||
2208 | struct kvm *kvm; | ||
2209 | |||
2210 | inode = kvmfs_inode(&kvm_vm_fops); | ||
2211 | if (IS_ERR(inode)) { | ||
2212 | r = PTR_ERR(inode); | ||
2213 | goto out1; | ||
2214 | } | ||
2215 | |||
2216 | kvm = kvm_create_vm(); | ||
2217 | if (IS_ERR(kvm)) { | ||
2218 | r = PTR_ERR(kvm); | ||
2219 | goto out2; | ||
2220 | } | ||
2221 | |||
2222 | file = kvmfs_file(inode, kvm); | ||
2223 | if (IS_ERR(file)) { | ||
2224 | r = PTR_ERR(file); | ||
2225 | goto out3; | ||
2226 | } | ||
2227 | kvm->filp = file; | ||
2228 | |||
2229 | r = get_unused_fd(); | ||
2230 | if (r < 0) | ||
2231 | goto out4; | ||
2232 | fd = r; | ||
2233 | fd_install(fd, file); | ||
2234 | |||
2235 | return fd; | ||
2236 | |||
2237 | out4: | ||
2238 | fput(file); | ||
2239 | out3: | ||
2240 | kvm_destroy_vm(kvm); | ||
2241 | out2: | ||
2242 | iput(inode); | ||
2243 | out1: | ||
2244 | return r; | ||
2245 | } | ||
2246 | |||
2247 | static long kvm_dev_ioctl(struct file *filp, | ||
2248 | unsigned int ioctl, unsigned long arg) | ||
2249 | { | ||
2250 | void __user *argp = (void __user *)arg; | ||
2251 | int r = -EINVAL; | ||
2252 | |||
2253 | switch (ioctl) { | ||
2254 | case KVM_GET_API_VERSION: | ||
2255 | r = KVM_API_VERSION; | ||
1942 | break; | 2256 | break; |
1943 | case KVM_SET_MSRS: | 2257 | case KVM_CREATE_VM: |
1944 | r = msr_io(kvm, argp, do_set_msr, 0); | 2258 | r = kvm_dev_ioctl_create_vm(); |
1945 | break; | 2259 | break; |
1946 | case KVM_GET_MSR_INDEX_LIST: { | 2260 | case KVM_GET_MSR_INDEX_LIST: { |
1947 | struct kvm_msr_list __user *user_msr_list = argp; | 2261 | struct kvm_msr_list __user *user_msr_list = argp; |
@@ -1977,43 +2291,11 @@ out: | |||
1977 | return r; | 2291 | return r; |
1978 | } | 2292 | } |
1979 | 2293 | ||
1980 | static struct page *kvm_dev_nopage(struct vm_area_struct *vma, | ||
1981 | unsigned long address, | ||
1982 | int *type) | ||
1983 | { | ||
1984 | struct kvm *kvm = vma->vm_file->private_data; | ||
1985 | unsigned long pgoff; | ||
1986 | struct kvm_memory_slot *slot; | ||
1987 | struct page *page; | ||
1988 | |||
1989 | *type = VM_FAULT_MINOR; | ||
1990 | pgoff = ((address - vma->vm_start) >> PAGE_SHIFT) + vma->vm_pgoff; | ||
1991 | slot = gfn_to_memslot(kvm, pgoff); | ||
1992 | if (!slot) | ||
1993 | return NOPAGE_SIGBUS; | ||
1994 | page = gfn_to_page(slot, pgoff); | ||
1995 | if (!page) | ||
1996 | return NOPAGE_SIGBUS; | ||
1997 | get_page(page); | ||
1998 | return page; | ||
1999 | } | ||
2000 | |||
2001 | static struct vm_operations_struct kvm_dev_vm_ops = { | ||
2002 | .nopage = kvm_dev_nopage, | ||
2003 | }; | ||
2004 | |||
2005 | static int kvm_dev_mmap(struct file *file, struct vm_area_struct *vma) | ||
2006 | { | ||
2007 | vma->vm_ops = &kvm_dev_vm_ops; | ||
2008 | return 0; | ||
2009 | } | ||
2010 | |||
2011 | static struct file_operations kvm_chardev_ops = { | 2294 | static struct file_operations kvm_chardev_ops = { |
2012 | .open = kvm_dev_open, | 2295 | .open = kvm_dev_open, |
2013 | .release = kvm_dev_release, | 2296 | .release = kvm_dev_release, |
2014 | .unlocked_ioctl = kvm_dev_ioctl, | 2297 | .unlocked_ioctl = kvm_dev_ioctl, |
2015 | .compat_ioctl = kvm_dev_ioctl, | 2298 | .compat_ioctl = kvm_dev_ioctl, |
2016 | .mmap = kvm_dev_mmap, | ||
2017 | }; | 2299 | }; |
2018 | 2300 | ||
2019 | static struct miscdevice kvm_dev = { | 2301 | static struct miscdevice kvm_dev = { |
@@ -2080,13 +2362,17 @@ static int kvm_cpu_hotplug(struct notifier_block *notifier, unsigned long val, | |||
2080 | int cpu = (long)v; | 2362 | int cpu = (long)v; |
2081 | 2363 | ||
2082 | switch (val) { | 2364 | switch (val) { |
2083 | case CPU_DEAD: | 2365 | case CPU_DOWN_PREPARE: |
2084 | case CPU_UP_CANCELED: | 2366 | case CPU_UP_CANCELED: |
2367 | printk(KERN_INFO "kvm: disabling virtualization on CPU%d\n", | ||
2368 | cpu); | ||
2085 | decache_vcpus_on_cpu(cpu); | 2369 | decache_vcpus_on_cpu(cpu); |
2086 | smp_call_function_single(cpu, kvm_arch_ops->hardware_disable, | 2370 | smp_call_function_single(cpu, kvm_arch_ops->hardware_disable, |
2087 | NULL, 0, 1); | 2371 | NULL, 0, 1); |
2088 | break; | 2372 | break; |
2089 | case CPU_UP_PREPARE: | 2373 | case CPU_ONLINE: |
2374 | printk(KERN_INFO "kvm: enabling virtualization on CPU%d\n", | ||
2375 | cpu); | ||
2090 | smp_call_function_single(cpu, kvm_arch_ops->hardware_enable, | 2376 | smp_call_function_single(cpu, kvm_arch_ops->hardware_enable, |
2091 | NULL, 0, 1); | 2377 | NULL, 0, 1); |
2092 | break; | 2378 | break; |
@@ -2121,13 +2407,13 @@ static void kvm_exit_debug(void) | |||
2121 | static int kvm_suspend(struct sys_device *dev, pm_message_t state) | 2407 | static int kvm_suspend(struct sys_device *dev, pm_message_t state) |
2122 | { | 2408 | { |
2123 | decache_vcpus_on_cpu(raw_smp_processor_id()); | 2409 | decache_vcpus_on_cpu(raw_smp_processor_id()); |
2124 | on_each_cpu(kvm_arch_ops->hardware_disable, 0, 0, 1); | 2410 | on_each_cpu(kvm_arch_ops->hardware_disable, NULL, 0, 1); |
2125 | return 0; | 2411 | return 0; |
2126 | } | 2412 | } |
2127 | 2413 | ||
2128 | static int kvm_resume(struct sys_device *dev) | 2414 | static int kvm_resume(struct sys_device *dev) |
2129 | { | 2415 | { |
2130 | on_each_cpu(kvm_arch_ops->hardware_enable, 0, 0, 1); | 2416 | on_each_cpu(kvm_arch_ops->hardware_enable, NULL, 0, 1); |
2131 | return 0; | 2417 | return 0; |
2132 | } | 2418 | } |
2133 | 2419 | ||
@@ -2144,6 +2430,18 @@ static struct sys_device kvm_sysdev = { | |||
2144 | 2430 | ||
2145 | hpa_t bad_page_address; | 2431 | hpa_t bad_page_address; |
2146 | 2432 | ||
2433 | static int kvmfs_get_sb(struct file_system_type *fs_type, int flags, | ||
2434 | const char *dev_name, void *data, struct vfsmount *mnt) | ||
2435 | { | ||
2436 | return get_sb_pseudo(fs_type, "kvm:", NULL, KVMFS_SUPER_MAGIC, mnt); | ||
2437 | } | ||
2438 | |||
2439 | static struct file_system_type kvm_fs_type = { | ||
2440 | .name = "kvmfs", | ||
2441 | .get_sb = kvmfs_get_sb, | ||
2442 | .kill_sb = kill_anon_super, | ||
2443 | }; | ||
2444 | |||
2147 | int kvm_init_arch(struct kvm_arch_ops *ops, struct module *module) | 2445 | int kvm_init_arch(struct kvm_arch_ops *ops, struct module *module) |
2148 | { | 2446 | { |
2149 | int r; | 2447 | int r; |
@@ -2220,8 +2518,16 @@ void kvm_exit_arch(void) | |||
2220 | static __init int kvm_init(void) | 2518 | static __init int kvm_init(void) |
2221 | { | 2519 | { |
2222 | static struct page *bad_page; | 2520 | static struct page *bad_page; |
2223 | int r = 0; | 2521 | int r; |
2522 | |||
2523 | r = register_filesystem(&kvm_fs_type); | ||
2524 | if (r) | ||
2525 | goto out3; | ||
2224 | 2526 | ||
2527 | kvmfs_mnt = kern_mount(&kvm_fs_type); | ||
2528 | r = PTR_ERR(kvmfs_mnt); | ||
2529 | if (IS_ERR(kvmfs_mnt)) | ||
2530 | goto out2; | ||
2225 | kvm_init_debug(); | 2531 | kvm_init_debug(); |
2226 | 2532 | ||
2227 | kvm_init_msr_list(); | 2533 | kvm_init_msr_list(); |
@@ -2234,10 +2540,14 @@ static __init int kvm_init(void) | |||
2234 | bad_page_address = page_to_pfn(bad_page) << PAGE_SHIFT; | 2540 | bad_page_address = page_to_pfn(bad_page) << PAGE_SHIFT; |
2235 | memset(__va(bad_page_address), 0, PAGE_SIZE); | 2541 | memset(__va(bad_page_address), 0, PAGE_SIZE); |
2236 | 2542 | ||
2237 | return r; | 2543 | return 0; |
2238 | 2544 | ||
2239 | out: | 2545 | out: |
2240 | kvm_exit_debug(); | 2546 | kvm_exit_debug(); |
2547 | mntput(kvmfs_mnt); | ||
2548 | out2: | ||
2549 | unregister_filesystem(&kvm_fs_type); | ||
2550 | out3: | ||
2241 | return r; | 2551 | return r; |
2242 | } | 2552 | } |
2243 | 2553 | ||
@@ -2245,6 +2555,8 @@ static __exit void kvm_exit(void) | |||
2245 | { | 2555 | { |
2246 | kvm_exit_debug(); | 2556 | kvm_exit_debug(); |
2247 | __free_page(pfn_to_page(bad_page_address >> PAGE_SHIFT)); | 2557 | __free_page(pfn_to_page(bad_page_address >> PAGE_SHIFT)); |
2558 | mntput(kvmfs_mnt); | ||
2559 | unregister_filesystem(&kvm_fs_type); | ||
2248 | } | 2560 | } |
2249 | 2561 | ||
2250 | module_init(kvm_init) | 2562 | module_init(kvm_init) |