Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 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 | |
Hollis Blanchard | e217402 | 2007-12-03 15:30:24 -0600 | [diff] [blame] | 18 | #include "iodev.h" |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 19 | |
Avi Kivity | edf8841 | 2007-12-16 11:02:48 +0200 | [diff] [blame] | 20 | #include <linux/kvm_host.h> |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 21 | #include <linux/kvm.h> |
| 22 | #include <linux/module.h> |
| 23 | #include <linux/errno.h> |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 24 | #include <linux/percpu.h> |
| 25 | #include <linux/gfp.h> |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 26 | #include <linux/mm.h> |
| 27 | #include <linux/miscdevice.h> |
| 28 | #include <linux/vmalloc.h> |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 29 | #include <linux/reboot.h> |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 30 | #include <linux/debugfs.h> |
| 31 | #include <linux/highmem.h> |
| 32 | #include <linux/file.h> |
Avi Kivity | 59ae6c6 | 2007-02-12 00:54:48 -0800 | [diff] [blame] | 33 | #include <linux/sysdev.h> |
Avi Kivity | 774c47f | 2007-02-12 00:54:47 -0800 | [diff] [blame] | 34 | #include <linux/cpu.h> |
Alexey Dobriyan | e8edc6e | 2007-05-21 01:22:52 +0400 | [diff] [blame] | 35 | #include <linux/sched.h> |
Avi Kivity | d9e368d | 2007-06-07 19:18:30 +0300 | [diff] [blame] | 36 | #include <linux/cpumask.h> |
| 37 | #include <linux/smp.h> |
Avi Kivity | d6d2816 | 2007-06-28 08:38:16 -0400 | [diff] [blame] | 38 | #include <linux/anon_inodes.h> |
Avi Kivity | 04d2cc7 | 2007-09-10 18:10:54 +0300 | [diff] [blame] | 39 | #include <linux/profile.h> |
Anthony Liguori | 7aa81cc | 2007-09-17 14:57:50 -0500 | [diff] [blame] | 40 | #include <linux/kvm_para.h> |
Izik Eidus | 6fc138d | 2007-10-09 19:20:39 +0200 | [diff] [blame] | 41 | #include <linux/pagemap.h> |
Anthony Liguori | 8d4e128 | 2007-10-18 09:59:34 -0500 | [diff] [blame] | 42 | #include <linux/mman.h> |
Anthony Liguori | 35149e2 | 2008-04-02 14:46:56 -0500 | [diff] [blame] | 43 | #include <linux/swap.h> |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 44 | |
Avi Kivity | e495606 | 2007-06-28 14:15:57 -0400 | [diff] [blame] | 45 | #include <asm/processor.h> |
Avi Kivity | e495606 | 2007-06-28 14:15:57 -0400 | [diff] [blame] | 46 | #include <asm/io.h> |
| 47 | #include <asm/uaccess.h> |
Izik Eidus | 3e021bf | 2007-11-19 11:16:57 +0200 | [diff] [blame] | 48 | #include <asm/pgtable.h> |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 49 | |
Laurent Vivier | 5f94c17 | 2008-05-30 16:05:54 +0200 | [diff] [blame] | 50 | #ifdef KVM_COALESCED_MMIO_PAGE_OFFSET |
| 51 | #include "coalesced_mmio.h" |
| 52 | #endif |
| 53 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 54 | MODULE_AUTHOR("Qumranet"); |
| 55 | MODULE_LICENSE("GPL"); |
| 56 | |
Zhang Xiantao | e9b11c1 | 2007-11-14 20:38:21 +0800 | [diff] [blame] | 57 | DEFINE_SPINLOCK(kvm_lock); |
| 58 | LIST_HEAD(vm_list); |
Avi Kivity | 133de90 | 2007-02-12 00:54:44 -0800 | [diff] [blame] | 59 | |
Avi Kivity | 1b6c016 | 2007-05-24 13:03:52 +0300 | [diff] [blame] | 60 | static cpumask_t cpus_hardware_enabled; |
| 61 | |
Rusty Russell | c16f862 | 2007-07-30 21:12:19 +1000 | [diff] [blame] | 62 | struct kmem_cache *kvm_vcpu_cache; |
| 63 | EXPORT_SYMBOL_GPL(kvm_vcpu_cache); |
Avi Kivity | 1165f5f | 2007-04-19 17:27:43 +0300 | [diff] [blame] | 64 | |
Avi Kivity | 15ad714 | 2007-07-11 18:17:21 +0300 | [diff] [blame] | 65 | static __read_mostly struct preempt_ops kvm_preempt_ops; |
| 66 | |
Hollis Blanchard | 76f7c87 | 2008-04-15 16:05:42 -0500 | [diff] [blame] | 67 | struct dentry *kvm_debugfs_dir; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 68 | |
Avi Kivity | bccf215 | 2007-02-21 18:04:26 +0200 | [diff] [blame] | 69 | static long kvm_vcpu_ioctl(struct file *file, unsigned int ioctl, |
| 70 | unsigned long arg); |
| 71 | |
Avi Kivity | 4ecac3f | 2008-05-13 13:23:38 +0300 | [diff] [blame] | 72 | bool kvm_rebooting; |
| 73 | |
James Morris | 5aacf0c | 2006-12-22 01:04:55 -0800 | [diff] [blame] | 74 | static inline int valid_vcpu(int n) |
| 75 | { |
| 76 | return likely(n >= 0 && n < KVM_MAX_VCPUS); |
| 77 | } |
| 78 | |
Ben-Ami Yassour | cbff90a | 2008-07-28 19:26:24 +0300 | [diff] [blame^] | 79 | static inline int is_mmio_pfn(pfn_t pfn) |
| 80 | { |
| 81 | if (pfn_valid(pfn)) |
| 82 | return PageReserved(pfn_to_page(pfn)); |
| 83 | |
| 84 | return true; |
| 85 | } |
| 86 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 87 | /* |
| 88 | * Switches to specified vcpu, until a matching vcpu_put() |
| 89 | */ |
Carsten Otte | 313a3dc | 2007-10-11 19:16:52 +0200 | [diff] [blame] | 90 | void vcpu_load(struct kvm_vcpu *vcpu) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 91 | { |
Avi Kivity | 15ad714 | 2007-07-11 18:17:21 +0300 | [diff] [blame] | 92 | int cpu; |
| 93 | |
Avi Kivity | bccf215 | 2007-02-21 18:04:26 +0200 | [diff] [blame] | 94 | mutex_lock(&vcpu->mutex); |
Avi Kivity | 15ad714 | 2007-07-11 18:17:21 +0300 | [diff] [blame] | 95 | cpu = get_cpu(); |
| 96 | preempt_notifier_register(&vcpu->preempt_notifier); |
Carsten Otte | 313a3dc | 2007-10-11 19:16:52 +0200 | [diff] [blame] | 97 | kvm_arch_vcpu_load(vcpu, cpu); |
Avi Kivity | 15ad714 | 2007-07-11 18:17:21 +0300 | [diff] [blame] | 98 | put_cpu(); |
Avi Kivity | bccf215 | 2007-02-21 18:04:26 +0200 | [diff] [blame] | 99 | } |
| 100 | |
Carsten Otte | 313a3dc | 2007-10-11 19:16:52 +0200 | [diff] [blame] | 101 | void vcpu_put(struct kvm_vcpu *vcpu) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 102 | { |
Avi Kivity | 15ad714 | 2007-07-11 18:17:21 +0300 | [diff] [blame] | 103 | preempt_disable(); |
Carsten Otte | 313a3dc | 2007-10-11 19:16:52 +0200 | [diff] [blame] | 104 | kvm_arch_vcpu_put(vcpu); |
Avi Kivity | 15ad714 | 2007-07-11 18:17:21 +0300 | [diff] [blame] | 105 | preempt_notifier_unregister(&vcpu->preempt_notifier); |
| 106 | preempt_enable(); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 107 | mutex_unlock(&vcpu->mutex); |
| 108 | } |
| 109 | |
Avi Kivity | d9e368d | 2007-06-07 19:18:30 +0300 | [diff] [blame] | 110 | static void ack_flush(void *_completed) |
| 111 | { |
Avi Kivity | d9e368d | 2007-06-07 19:18:30 +0300 | [diff] [blame] | 112 | } |
| 113 | |
| 114 | void kvm_flush_remote_tlbs(struct kvm *kvm) |
| 115 | { |
Avi Kivity | 597a5f5 | 2008-07-20 14:24:22 +0300 | [diff] [blame] | 116 | int i, cpu, me; |
Avi Kivity | d9e368d | 2007-06-07 19:18:30 +0300 | [diff] [blame] | 117 | cpumask_t cpus; |
| 118 | struct kvm_vcpu *vcpu; |
Avi Kivity | d9e368d | 2007-06-07 19:18:30 +0300 | [diff] [blame] | 119 | |
Avi Kivity | 597a5f5 | 2008-07-20 14:24:22 +0300 | [diff] [blame] | 120 | me = get_cpu(); |
Avi Kivity | d9e368d | 2007-06-07 19:18:30 +0300 | [diff] [blame] | 121 | cpus_clear(cpus); |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 122 | for (i = 0; i < KVM_MAX_VCPUS; ++i) { |
| 123 | vcpu = kvm->vcpus[i]; |
| 124 | if (!vcpu) |
| 125 | continue; |
Avi Kivity | 3176bc3 | 2007-10-16 17:22:08 +0200 | [diff] [blame] | 126 | if (test_and_set_bit(KVM_REQ_TLB_FLUSH, &vcpu->requests)) |
Avi Kivity | d9e368d | 2007-06-07 19:18:30 +0300 | [diff] [blame] | 127 | continue; |
| 128 | cpu = vcpu->cpu; |
Avi Kivity | 597a5f5 | 2008-07-20 14:24:22 +0300 | [diff] [blame] | 129 | if (cpu != -1 && cpu != me) |
Laurent Vivier | 49d3bd7 | 2007-10-22 16:33:07 +0200 | [diff] [blame] | 130 | cpu_set(cpu, cpus); |
Avi Kivity | d9e368d | 2007-06-07 19:18:30 +0300 | [diff] [blame] | 131 | } |
Avi Kivity | 0f74a24 | 2007-11-20 23:01:14 +0200 | [diff] [blame] | 132 | if (cpus_empty(cpus)) |
Avi Kivity | 597a5f5 | 2008-07-20 14:24:22 +0300 | [diff] [blame] | 133 | goto out; |
Avi Kivity | 0f74a24 | 2007-11-20 23:01:14 +0200 | [diff] [blame] | 134 | ++kvm->stat.remote_tlb_flush; |
Laurent Vivier | 49d3bd7 | 2007-10-22 16:33:07 +0200 | [diff] [blame] | 135 | smp_call_function_mask(cpus, ack_flush, NULL, 1); |
Avi Kivity | 597a5f5 | 2008-07-20 14:24:22 +0300 | [diff] [blame] | 136 | out: |
| 137 | put_cpu(); |
Avi Kivity | d9e368d | 2007-06-07 19:18:30 +0300 | [diff] [blame] | 138 | } |
| 139 | |
Marcelo Tosatti | 2e53d63 | 2008-02-20 14:47:24 -0500 | [diff] [blame] | 140 | void kvm_reload_remote_mmus(struct kvm *kvm) |
| 141 | { |
Avi Kivity | 597a5f5 | 2008-07-20 14:24:22 +0300 | [diff] [blame] | 142 | int i, cpu, me; |
Marcelo Tosatti | 2e53d63 | 2008-02-20 14:47:24 -0500 | [diff] [blame] | 143 | cpumask_t cpus; |
| 144 | struct kvm_vcpu *vcpu; |
| 145 | |
Avi Kivity | 597a5f5 | 2008-07-20 14:24:22 +0300 | [diff] [blame] | 146 | me = get_cpu(); |
Marcelo Tosatti | 2e53d63 | 2008-02-20 14:47:24 -0500 | [diff] [blame] | 147 | cpus_clear(cpus); |
| 148 | for (i = 0; i < KVM_MAX_VCPUS; ++i) { |
| 149 | vcpu = kvm->vcpus[i]; |
| 150 | if (!vcpu) |
| 151 | continue; |
| 152 | if (test_and_set_bit(KVM_REQ_MMU_RELOAD, &vcpu->requests)) |
| 153 | continue; |
| 154 | cpu = vcpu->cpu; |
Avi Kivity | 597a5f5 | 2008-07-20 14:24:22 +0300 | [diff] [blame] | 155 | if (cpu != -1 && cpu != me) |
Marcelo Tosatti | 2e53d63 | 2008-02-20 14:47:24 -0500 | [diff] [blame] | 156 | cpu_set(cpu, cpus); |
| 157 | } |
| 158 | if (cpus_empty(cpus)) |
Avi Kivity | 597a5f5 | 2008-07-20 14:24:22 +0300 | [diff] [blame] | 159 | goto out; |
Marcelo Tosatti | 2e53d63 | 2008-02-20 14:47:24 -0500 | [diff] [blame] | 160 | smp_call_function_mask(cpus, ack_flush, NULL, 1); |
Avi Kivity | 597a5f5 | 2008-07-20 14:24:22 +0300 | [diff] [blame] | 161 | out: |
| 162 | put_cpu(); |
Marcelo Tosatti | 2e53d63 | 2008-02-20 14:47:24 -0500 | [diff] [blame] | 163 | } |
| 164 | |
| 165 | |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 166 | int kvm_vcpu_init(struct kvm_vcpu *vcpu, struct kvm *kvm, unsigned id) |
| 167 | { |
| 168 | struct page *page; |
| 169 | int r; |
| 170 | |
| 171 | mutex_init(&vcpu->mutex); |
| 172 | vcpu->cpu = -1; |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 173 | vcpu->kvm = kvm; |
| 174 | vcpu->vcpu_id = id; |
Eddie Dong | b6958ce | 2007-07-18 12:15:21 +0300 | [diff] [blame] | 175 | init_waitqueue_head(&vcpu->wq); |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 176 | |
| 177 | page = alloc_page(GFP_KERNEL | __GFP_ZERO); |
| 178 | if (!page) { |
| 179 | r = -ENOMEM; |
| 180 | goto fail; |
| 181 | } |
| 182 | vcpu->run = page_address(page); |
| 183 | |
Zhang Xiantao | e9b11c1 | 2007-11-14 20:38:21 +0800 | [diff] [blame] | 184 | r = kvm_arch_vcpu_init(vcpu); |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 185 | if (r < 0) |
Zhang Xiantao | e9b11c1 | 2007-11-14 20:38:21 +0800 | [diff] [blame] | 186 | goto fail_free_run; |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 187 | return 0; |
| 188 | |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 189 | fail_free_run: |
| 190 | free_page((unsigned long)vcpu->run); |
| 191 | fail: |
Rusty Russell | 76fafa5 | 2007-10-08 10:50:48 +1000 | [diff] [blame] | 192 | return r; |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 193 | } |
| 194 | EXPORT_SYMBOL_GPL(kvm_vcpu_init); |
| 195 | |
| 196 | void kvm_vcpu_uninit(struct kvm_vcpu *vcpu) |
| 197 | { |
Zhang Xiantao | e9b11c1 | 2007-11-14 20:38:21 +0800 | [diff] [blame] | 198 | kvm_arch_vcpu_uninit(vcpu); |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 199 | free_page((unsigned long)vcpu->run); |
| 200 | } |
| 201 | EXPORT_SYMBOL_GPL(kvm_vcpu_uninit); |
| 202 | |
Andrea Arcangeli | e930bff | 2008-07-25 16:24:52 +0200 | [diff] [blame] | 203 | #if defined(CONFIG_MMU_NOTIFIER) && defined(KVM_ARCH_WANT_MMU_NOTIFIER) |
| 204 | static inline struct kvm *mmu_notifier_to_kvm(struct mmu_notifier *mn) |
| 205 | { |
| 206 | return container_of(mn, struct kvm, mmu_notifier); |
| 207 | } |
| 208 | |
| 209 | static void kvm_mmu_notifier_invalidate_page(struct mmu_notifier *mn, |
| 210 | struct mm_struct *mm, |
| 211 | unsigned long address) |
| 212 | { |
| 213 | struct kvm *kvm = mmu_notifier_to_kvm(mn); |
| 214 | int need_tlb_flush; |
| 215 | |
| 216 | /* |
| 217 | * When ->invalidate_page runs, the linux pte has been zapped |
| 218 | * already but the page is still allocated until |
| 219 | * ->invalidate_page returns. So if we increase the sequence |
| 220 | * here the kvm page fault will notice if the spte can't be |
| 221 | * established because the page is going to be freed. If |
| 222 | * instead the kvm page fault establishes the spte before |
| 223 | * ->invalidate_page runs, kvm_unmap_hva will release it |
| 224 | * before returning. |
| 225 | * |
| 226 | * The sequence increase only need to be seen at spin_unlock |
| 227 | * time, and not at spin_lock time. |
| 228 | * |
| 229 | * Increasing the sequence after the spin_unlock would be |
| 230 | * unsafe because the kvm page fault could then establish the |
| 231 | * pte after kvm_unmap_hva returned, without noticing the page |
| 232 | * is going to be freed. |
| 233 | */ |
| 234 | spin_lock(&kvm->mmu_lock); |
| 235 | kvm->mmu_notifier_seq++; |
| 236 | need_tlb_flush = kvm_unmap_hva(kvm, address); |
| 237 | spin_unlock(&kvm->mmu_lock); |
| 238 | |
| 239 | /* we've to flush the tlb before the pages can be freed */ |
| 240 | if (need_tlb_flush) |
| 241 | kvm_flush_remote_tlbs(kvm); |
| 242 | |
| 243 | } |
| 244 | |
| 245 | static void kvm_mmu_notifier_invalidate_range_start(struct mmu_notifier *mn, |
| 246 | struct mm_struct *mm, |
| 247 | unsigned long start, |
| 248 | unsigned long end) |
| 249 | { |
| 250 | struct kvm *kvm = mmu_notifier_to_kvm(mn); |
| 251 | int need_tlb_flush = 0; |
| 252 | |
| 253 | spin_lock(&kvm->mmu_lock); |
| 254 | /* |
| 255 | * The count increase must become visible at unlock time as no |
| 256 | * spte can be established without taking the mmu_lock and |
| 257 | * count is also read inside the mmu_lock critical section. |
| 258 | */ |
| 259 | kvm->mmu_notifier_count++; |
| 260 | for (; start < end; start += PAGE_SIZE) |
| 261 | need_tlb_flush |= kvm_unmap_hva(kvm, start); |
| 262 | spin_unlock(&kvm->mmu_lock); |
| 263 | |
| 264 | /* we've to flush the tlb before the pages can be freed */ |
| 265 | if (need_tlb_flush) |
| 266 | kvm_flush_remote_tlbs(kvm); |
| 267 | } |
| 268 | |
| 269 | static void kvm_mmu_notifier_invalidate_range_end(struct mmu_notifier *mn, |
| 270 | struct mm_struct *mm, |
| 271 | unsigned long start, |
| 272 | unsigned long end) |
| 273 | { |
| 274 | struct kvm *kvm = mmu_notifier_to_kvm(mn); |
| 275 | |
| 276 | spin_lock(&kvm->mmu_lock); |
| 277 | /* |
| 278 | * This sequence increase will notify the kvm page fault that |
| 279 | * the page that is going to be mapped in the spte could have |
| 280 | * been freed. |
| 281 | */ |
| 282 | kvm->mmu_notifier_seq++; |
| 283 | /* |
| 284 | * The above sequence increase must be visible before the |
| 285 | * below count decrease but both values are read by the kvm |
| 286 | * page fault under mmu_lock spinlock so we don't need to add |
| 287 | * a smb_wmb() here in between the two. |
| 288 | */ |
| 289 | kvm->mmu_notifier_count--; |
| 290 | spin_unlock(&kvm->mmu_lock); |
| 291 | |
| 292 | BUG_ON(kvm->mmu_notifier_count < 0); |
| 293 | } |
| 294 | |
| 295 | static int kvm_mmu_notifier_clear_flush_young(struct mmu_notifier *mn, |
| 296 | struct mm_struct *mm, |
| 297 | unsigned long address) |
| 298 | { |
| 299 | struct kvm *kvm = mmu_notifier_to_kvm(mn); |
| 300 | int young; |
| 301 | |
| 302 | spin_lock(&kvm->mmu_lock); |
| 303 | young = kvm_age_hva(kvm, address); |
| 304 | spin_unlock(&kvm->mmu_lock); |
| 305 | |
| 306 | if (young) |
| 307 | kvm_flush_remote_tlbs(kvm); |
| 308 | |
| 309 | return young; |
| 310 | } |
| 311 | |
| 312 | static const struct mmu_notifier_ops kvm_mmu_notifier_ops = { |
| 313 | .invalidate_page = kvm_mmu_notifier_invalidate_page, |
| 314 | .invalidate_range_start = kvm_mmu_notifier_invalidate_range_start, |
| 315 | .invalidate_range_end = kvm_mmu_notifier_invalidate_range_end, |
| 316 | .clear_flush_young = kvm_mmu_notifier_clear_flush_young, |
| 317 | }; |
| 318 | #endif /* CONFIG_MMU_NOTIFIER && KVM_ARCH_WANT_MMU_NOTIFIER */ |
| 319 | |
Avi Kivity | f17abe9 | 2007-02-21 19:28:04 +0200 | [diff] [blame] | 320 | static struct kvm *kvm_create_vm(void) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 321 | { |
Zhang Xiantao | d19a9cd | 2007-11-18 18:43:45 +0800 | [diff] [blame] | 322 | struct kvm *kvm = kvm_arch_create_vm(); |
Laurent Vivier | 5f94c17 | 2008-05-30 16:05:54 +0200 | [diff] [blame] | 323 | #ifdef KVM_COALESCED_MMIO_PAGE_OFFSET |
| 324 | struct page *page; |
| 325 | #endif |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 326 | |
Zhang Xiantao | d19a9cd | 2007-11-18 18:43:45 +0800 | [diff] [blame] | 327 | if (IS_ERR(kvm)) |
| 328 | goto out; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 329 | |
Laurent Vivier | 5f94c17 | 2008-05-30 16:05:54 +0200 | [diff] [blame] | 330 | #ifdef KVM_COALESCED_MMIO_PAGE_OFFSET |
| 331 | page = alloc_page(GFP_KERNEL | __GFP_ZERO); |
| 332 | if (!page) { |
| 333 | kfree(kvm); |
| 334 | return ERR_PTR(-ENOMEM); |
| 335 | } |
| 336 | kvm->coalesced_mmio_ring = |
| 337 | (struct kvm_coalesced_mmio_ring *)page_address(page); |
| 338 | #endif |
| 339 | |
Andrea Arcangeli | e930bff | 2008-07-25 16:24:52 +0200 | [diff] [blame] | 340 | #if defined(CONFIG_MMU_NOTIFIER) && defined(KVM_ARCH_WANT_MMU_NOTIFIER) |
| 341 | { |
| 342 | int err; |
| 343 | kvm->mmu_notifier.ops = &kvm_mmu_notifier_ops; |
| 344 | err = mmu_notifier_register(&kvm->mmu_notifier, current->mm); |
| 345 | if (err) { |
| 346 | #ifdef KVM_COALESCED_MMIO_PAGE_OFFSET |
| 347 | put_page(page); |
| 348 | #endif |
| 349 | kfree(kvm); |
| 350 | return ERR_PTR(err); |
| 351 | } |
| 352 | } |
| 353 | #endif |
| 354 | |
Avi Kivity | 6d4e4c4 | 2007-11-21 16:41:05 +0200 | [diff] [blame] | 355 | kvm->mm = current->mm; |
| 356 | atomic_inc(&kvm->mm->mm_count); |
Marcelo Tosatti | aaee2c9 | 2007-12-20 19:18:26 -0500 | [diff] [blame] | 357 | spin_lock_init(&kvm->mmu_lock); |
Eddie Dong | 7490634 | 2007-06-19 18:05:03 +0300 | [diff] [blame] | 358 | kvm_io_bus_init(&kvm->pio_bus); |
Shaohua Li | 11ec280 | 2007-07-23 14:51:37 +0800 | [diff] [blame] | 359 | mutex_init(&kvm->lock); |
Gregory Haskins | 2eeb2e9 | 2007-05-31 14:08:53 -0400 | [diff] [blame] | 360 | kvm_io_bus_init(&kvm->mmio_bus); |
Izik Eidus | 72dc67a | 2008-02-10 18:04:15 +0200 | [diff] [blame] | 361 | init_rwsem(&kvm->slots_lock); |
Izik Eidus | d39f13b | 2008-03-30 16:01:25 +0300 | [diff] [blame] | 362 | atomic_set(&kvm->users_count, 1); |
Rusty Russell | 5e58cfe | 2007-07-23 17:08:21 +1000 | [diff] [blame] | 363 | spin_lock(&kvm_lock); |
| 364 | list_add(&kvm->vm_list, &vm_list); |
| 365 | spin_unlock(&kvm_lock); |
Laurent Vivier | 5f94c17 | 2008-05-30 16:05:54 +0200 | [diff] [blame] | 366 | #ifdef KVM_COALESCED_MMIO_PAGE_OFFSET |
| 367 | kvm_coalesced_mmio_init(kvm); |
| 368 | #endif |
Zhang Xiantao | d19a9cd | 2007-11-18 18:43:45 +0800 | [diff] [blame] | 369 | out: |
Avi Kivity | f17abe9 | 2007-02-21 19:28:04 +0200 | [diff] [blame] | 370 | return kvm; |
| 371 | } |
| 372 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 373 | /* |
| 374 | * Free any memory in @free but not in @dont. |
| 375 | */ |
| 376 | static void kvm_free_physmem_slot(struct kvm_memory_slot *free, |
| 377 | struct kvm_memory_slot *dont) |
| 378 | { |
Izik Eidus | 290fc38 | 2007-09-27 14:11:22 +0200 | [diff] [blame] | 379 | if (!dont || free->rmap != dont->rmap) |
| 380 | vfree(free->rmap); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 381 | |
| 382 | if (!dont || free->dirty_bitmap != dont->dirty_bitmap) |
| 383 | vfree(free->dirty_bitmap); |
| 384 | |
Marcelo Tosatti | 05da455 | 2008-02-23 11:44:30 -0300 | [diff] [blame] | 385 | if (!dont || free->lpage_info != dont->lpage_info) |
| 386 | vfree(free->lpage_info); |
| 387 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 388 | free->npages = 0; |
Al Viro | 8b6d44c | 2007-02-09 16:38:40 +0000 | [diff] [blame] | 389 | free->dirty_bitmap = NULL; |
Anthony Liguori | 8d4e128 | 2007-10-18 09:59:34 -0500 | [diff] [blame] | 390 | free->rmap = NULL; |
Marcelo Tosatti | 05da455 | 2008-02-23 11:44:30 -0300 | [diff] [blame] | 391 | free->lpage_info = NULL; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 392 | } |
| 393 | |
Zhang Xiantao | d19a9cd | 2007-11-18 18:43:45 +0800 | [diff] [blame] | 394 | void kvm_free_physmem(struct kvm *kvm) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 395 | { |
| 396 | int i; |
| 397 | |
| 398 | for (i = 0; i < kvm->nmemslots; ++i) |
Al Viro | 8b6d44c | 2007-02-09 16:38:40 +0000 | [diff] [blame] | 399 | kvm_free_physmem_slot(&kvm->memslots[i], NULL); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 400 | } |
| 401 | |
Avi Kivity | f17abe9 | 2007-02-21 19:28:04 +0200 | [diff] [blame] | 402 | static void kvm_destroy_vm(struct kvm *kvm) |
| 403 | { |
Avi Kivity | 6d4e4c4 | 2007-11-21 16:41:05 +0200 | [diff] [blame] | 404 | struct mm_struct *mm = kvm->mm; |
| 405 | |
Avi Kivity | 133de90 | 2007-02-12 00:54:44 -0800 | [diff] [blame] | 406 | spin_lock(&kvm_lock); |
| 407 | list_del(&kvm->vm_list); |
| 408 | spin_unlock(&kvm_lock); |
Eddie Dong | 7490634 | 2007-06-19 18:05:03 +0300 | [diff] [blame] | 409 | kvm_io_bus_destroy(&kvm->pio_bus); |
Gregory Haskins | 2eeb2e9 | 2007-05-31 14:08:53 -0400 | [diff] [blame] | 410 | kvm_io_bus_destroy(&kvm->mmio_bus); |
Laurent Vivier | 5f94c17 | 2008-05-30 16:05:54 +0200 | [diff] [blame] | 411 | #ifdef KVM_COALESCED_MMIO_PAGE_OFFSET |
| 412 | if (kvm->coalesced_mmio_ring != NULL) |
| 413 | free_page((unsigned long)kvm->coalesced_mmio_ring); |
| 414 | #endif |
Andrea Arcangeli | e930bff | 2008-07-25 16:24:52 +0200 | [diff] [blame] | 415 | #if defined(CONFIG_MMU_NOTIFIER) && defined(KVM_ARCH_WANT_MMU_NOTIFIER) |
| 416 | mmu_notifier_unregister(&kvm->mmu_notifier, kvm->mm); |
| 417 | #endif |
Zhang Xiantao | d19a9cd | 2007-11-18 18:43:45 +0800 | [diff] [blame] | 418 | kvm_arch_destroy_vm(kvm); |
Avi Kivity | 6d4e4c4 | 2007-11-21 16:41:05 +0200 | [diff] [blame] | 419 | mmdrop(mm); |
Avi Kivity | f17abe9 | 2007-02-21 19:28:04 +0200 | [diff] [blame] | 420 | } |
| 421 | |
Izik Eidus | d39f13b | 2008-03-30 16:01:25 +0300 | [diff] [blame] | 422 | void kvm_get_kvm(struct kvm *kvm) |
| 423 | { |
| 424 | atomic_inc(&kvm->users_count); |
| 425 | } |
| 426 | EXPORT_SYMBOL_GPL(kvm_get_kvm); |
| 427 | |
| 428 | void kvm_put_kvm(struct kvm *kvm) |
| 429 | { |
| 430 | if (atomic_dec_and_test(&kvm->users_count)) |
| 431 | kvm_destroy_vm(kvm); |
| 432 | } |
| 433 | EXPORT_SYMBOL_GPL(kvm_put_kvm); |
| 434 | |
| 435 | |
Avi Kivity | f17abe9 | 2007-02-21 19:28:04 +0200 | [diff] [blame] | 436 | static int kvm_vm_release(struct inode *inode, struct file *filp) |
| 437 | { |
| 438 | struct kvm *kvm = filp->private_data; |
| 439 | |
Izik Eidus | d39f13b | 2008-03-30 16:01:25 +0300 | [diff] [blame] | 440 | kvm_put_kvm(kvm); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 441 | return 0; |
| 442 | } |
| 443 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 444 | /* |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 445 | * Allocate some memory and give it an address in the guest physical address |
| 446 | * space. |
| 447 | * |
| 448 | * Discontiguous memory is allowed, mostly for framebuffers. |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 449 | * |
Marcelo Tosatti | 10589a4 | 2007-12-20 19:18:22 -0500 | [diff] [blame] | 450 | * Must be called holding mmap_sem for write. |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 451 | */ |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 452 | int __kvm_set_memory_region(struct kvm *kvm, |
| 453 | struct kvm_userspace_memory_region *mem, |
| 454 | int user_alloc) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 455 | { |
| 456 | int r; |
| 457 | gfn_t base_gfn; |
| 458 | unsigned long npages; |
| 459 | unsigned long i; |
| 460 | struct kvm_memory_slot *memslot; |
| 461 | struct kvm_memory_slot old, new; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 462 | |
| 463 | r = -EINVAL; |
| 464 | /* General sanity checks */ |
| 465 | if (mem->memory_size & (PAGE_SIZE - 1)) |
| 466 | goto out; |
| 467 | if (mem->guest_phys_addr & (PAGE_SIZE - 1)) |
| 468 | goto out; |
Izik Eidus | e0d62c7 | 2007-10-24 23:57:46 +0200 | [diff] [blame] | 469 | if (mem->slot >= KVM_MEMORY_SLOTS + KVM_PRIVATE_MEM_SLOTS) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 470 | goto out; |
| 471 | if (mem->guest_phys_addr + mem->memory_size < mem->guest_phys_addr) |
| 472 | goto out; |
| 473 | |
| 474 | memslot = &kvm->memslots[mem->slot]; |
| 475 | base_gfn = mem->guest_phys_addr >> PAGE_SHIFT; |
| 476 | npages = mem->memory_size >> PAGE_SHIFT; |
| 477 | |
| 478 | if (!npages) |
| 479 | mem->flags &= ~KVM_MEM_LOG_DIRTY_PAGES; |
| 480 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 481 | new = old = *memslot; |
| 482 | |
| 483 | new.base_gfn = base_gfn; |
| 484 | new.npages = npages; |
| 485 | new.flags = mem->flags; |
| 486 | |
| 487 | /* Disallow changing a memory slot's size. */ |
| 488 | r = -EINVAL; |
| 489 | if (npages && old.npages && npages != old.npages) |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 490 | goto out_free; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 491 | |
| 492 | /* Check for overlaps */ |
| 493 | r = -EEXIST; |
| 494 | for (i = 0; i < KVM_MEMORY_SLOTS; ++i) { |
| 495 | struct kvm_memory_slot *s = &kvm->memslots[i]; |
| 496 | |
| 497 | if (s == memslot) |
| 498 | continue; |
| 499 | if (!((base_gfn + npages <= s->base_gfn) || |
| 500 | (base_gfn >= s->base_gfn + s->npages))) |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 501 | goto out_free; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 502 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 503 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 504 | /* Free page dirty bitmap if unneeded */ |
| 505 | if (!(new.flags & KVM_MEM_LOG_DIRTY_PAGES)) |
Al Viro | 8b6d44c | 2007-02-09 16:38:40 +0000 | [diff] [blame] | 506 | new.dirty_bitmap = NULL; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 507 | |
| 508 | r = -ENOMEM; |
| 509 | |
| 510 | /* Allocate if a slot is being created */ |
Carsten Otte | eff0114 | 2008-06-27 15:05:31 +0200 | [diff] [blame] | 511 | #ifndef CONFIG_S390 |
Anthony Liguori | 8d4e128 | 2007-10-18 09:59:34 -0500 | [diff] [blame] | 512 | if (npages && !new.rmap) { |
Mike Day | d77c26f | 2007-10-08 09:02:08 -0400 | [diff] [blame] | 513 | new.rmap = vmalloc(npages * sizeof(struct page *)); |
Izik Eidus | 290fc38 | 2007-09-27 14:11:22 +0200 | [diff] [blame] | 514 | |
| 515 | if (!new.rmap) |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 516 | goto out_free; |
Izik Eidus | 290fc38 | 2007-09-27 14:11:22 +0200 | [diff] [blame] | 517 | |
Izik Eidus | 290fc38 | 2007-09-27 14:11:22 +0200 | [diff] [blame] | 518 | memset(new.rmap, 0, npages * sizeof(*new.rmap)); |
Anthony Liguori | 8d4e128 | 2007-10-18 09:59:34 -0500 | [diff] [blame] | 519 | |
Izik Eidus | 80b14b5 | 2007-10-25 11:54:04 +0200 | [diff] [blame] | 520 | new.user_alloc = user_alloc; |
Andrea Arcangeli | 604b38a | 2008-07-25 16:32:03 +0200 | [diff] [blame] | 521 | /* |
| 522 | * hva_to_rmmap() serialzies with the mmu_lock and to be |
| 523 | * safe it has to ignore memslots with !user_alloc && |
| 524 | * !userspace_addr. |
| 525 | */ |
| 526 | if (user_alloc) |
| 527 | new.userspace_addr = mem->userspace_addr; |
| 528 | else |
| 529 | new.userspace_addr = 0; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 530 | } |
Marcelo Tosatti | 05da455 | 2008-02-23 11:44:30 -0300 | [diff] [blame] | 531 | if (npages && !new.lpage_info) { |
| 532 | int largepages = npages / KVM_PAGES_PER_HPAGE; |
| 533 | if (npages % KVM_PAGES_PER_HPAGE) |
| 534 | largepages++; |
| 535 | if (base_gfn % KVM_PAGES_PER_HPAGE) |
| 536 | largepages++; |
| 537 | |
| 538 | new.lpage_info = vmalloc(largepages * sizeof(*new.lpage_info)); |
| 539 | |
| 540 | if (!new.lpage_info) |
| 541 | goto out_free; |
| 542 | |
| 543 | memset(new.lpage_info, 0, largepages * sizeof(*new.lpage_info)); |
| 544 | |
| 545 | if (base_gfn % KVM_PAGES_PER_HPAGE) |
| 546 | new.lpage_info[0].write_count = 1; |
| 547 | if ((base_gfn+npages) % KVM_PAGES_PER_HPAGE) |
| 548 | new.lpage_info[largepages-1].write_count = 1; |
| 549 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 550 | |
| 551 | /* Allocate page dirty bitmap if needed */ |
| 552 | if ((new.flags & KVM_MEM_LOG_DIRTY_PAGES) && !new.dirty_bitmap) { |
| 553 | unsigned dirty_bytes = ALIGN(npages, BITS_PER_LONG) / 8; |
| 554 | |
| 555 | new.dirty_bitmap = vmalloc(dirty_bytes); |
| 556 | if (!new.dirty_bitmap) |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 557 | goto out_free; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 558 | memset(new.dirty_bitmap, 0, dirty_bytes); |
| 559 | } |
Carsten Otte | eff0114 | 2008-06-27 15:05:31 +0200 | [diff] [blame] | 560 | #endif /* not defined CONFIG_S390 */ |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 561 | |
Marcelo Tosatti | 34d4cb8 | 2008-07-10 20:49:31 -0300 | [diff] [blame] | 562 | if (!npages) |
| 563 | kvm_arch_flush_shadow(kvm); |
| 564 | |
Andrea Arcangeli | 604b38a | 2008-07-25 16:32:03 +0200 | [diff] [blame] | 565 | spin_lock(&kvm->mmu_lock); |
| 566 | if (mem->slot >= kvm->nmemslots) |
| 567 | kvm->nmemslots = mem->slot + 1; |
| 568 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 569 | *memslot = new; |
Andrea Arcangeli | 604b38a | 2008-07-25 16:32:03 +0200 | [diff] [blame] | 570 | spin_unlock(&kvm->mmu_lock); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 571 | |
Zhang Xiantao | 0de1034 | 2007-11-20 16:25:04 +0800 | [diff] [blame] | 572 | r = kvm_arch_set_memory_region(kvm, mem, old, user_alloc); |
| 573 | if (r) { |
Andrea Arcangeli | 604b38a | 2008-07-25 16:32:03 +0200 | [diff] [blame] | 574 | spin_lock(&kvm->mmu_lock); |
Zhang Xiantao | 0de1034 | 2007-11-20 16:25:04 +0800 | [diff] [blame] | 575 | *memslot = old; |
Andrea Arcangeli | 604b38a | 2008-07-25 16:32:03 +0200 | [diff] [blame] | 576 | spin_unlock(&kvm->mmu_lock); |
Zhang Xiantao | 0de1034 | 2007-11-20 16:25:04 +0800 | [diff] [blame] | 577 | goto out_free; |
Zhang Xiantao | 3ad82a7 | 2007-11-20 13:11:38 +0800 | [diff] [blame] | 578 | } |
| 579 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 580 | kvm_free_physmem_slot(&old, &new); |
| 581 | return 0; |
| 582 | |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 583 | out_free: |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 584 | kvm_free_physmem_slot(&new, &old); |
| 585 | out: |
| 586 | return r; |
Izik Eidus | 210c7c4 | 2007-10-24 23:52:57 +0200 | [diff] [blame] | 587 | |
| 588 | } |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 589 | EXPORT_SYMBOL_GPL(__kvm_set_memory_region); |
| 590 | |
| 591 | int kvm_set_memory_region(struct kvm *kvm, |
| 592 | struct kvm_userspace_memory_region *mem, |
| 593 | int user_alloc) |
| 594 | { |
| 595 | int r; |
| 596 | |
Izik Eidus | 72dc67a | 2008-02-10 18:04:15 +0200 | [diff] [blame] | 597 | down_write(&kvm->slots_lock); |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 598 | r = __kvm_set_memory_region(kvm, mem, user_alloc); |
Izik Eidus | 72dc67a | 2008-02-10 18:04:15 +0200 | [diff] [blame] | 599 | up_write(&kvm->slots_lock); |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 600 | return r; |
| 601 | } |
Izik Eidus | 210c7c4 | 2007-10-24 23:52:57 +0200 | [diff] [blame] | 602 | EXPORT_SYMBOL_GPL(kvm_set_memory_region); |
| 603 | |
Carsten Otte | 1fe779f | 2007-10-29 16:08:35 +0100 | [diff] [blame] | 604 | int kvm_vm_ioctl_set_memory_region(struct kvm *kvm, |
| 605 | struct |
| 606 | kvm_userspace_memory_region *mem, |
| 607 | int user_alloc) |
Izik Eidus | 210c7c4 | 2007-10-24 23:52:57 +0200 | [diff] [blame] | 608 | { |
Izik Eidus | e0d62c7 | 2007-10-24 23:57:46 +0200 | [diff] [blame] | 609 | if (mem->slot >= KVM_MEMORY_SLOTS) |
| 610 | return -EINVAL; |
Izik Eidus | 210c7c4 | 2007-10-24 23:52:57 +0200 | [diff] [blame] | 611 | return kvm_set_memory_region(kvm, mem, user_alloc); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 612 | } |
| 613 | |
Zhang Xiantao | 5bb064d | 2007-11-18 20:29:43 +0800 | [diff] [blame] | 614 | int kvm_get_dirty_log(struct kvm *kvm, |
| 615 | struct kvm_dirty_log *log, int *is_dirty) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 616 | { |
| 617 | struct kvm_memory_slot *memslot; |
| 618 | int r, i; |
| 619 | int n; |
| 620 | unsigned long any = 0; |
| 621 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 622 | r = -EINVAL; |
| 623 | if (log->slot >= KVM_MEMORY_SLOTS) |
| 624 | goto out; |
| 625 | |
| 626 | memslot = &kvm->memslots[log->slot]; |
| 627 | r = -ENOENT; |
| 628 | if (!memslot->dirty_bitmap) |
| 629 | goto out; |
| 630 | |
Uri Lublin | cd1a4a9 | 2007-02-22 16:43:09 +0200 | [diff] [blame] | 631 | n = ALIGN(memslot->npages, BITS_PER_LONG) / 8; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 632 | |
Uri Lublin | cd1a4a9 | 2007-02-22 16:43:09 +0200 | [diff] [blame] | 633 | for (i = 0; !any && i < n/sizeof(long); ++i) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 634 | any = memslot->dirty_bitmap[i]; |
| 635 | |
| 636 | r = -EFAULT; |
| 637 | if (copy_to_user(log->dirty_bitmap, memslot->dirty_bitmap, n)) |
| 638 | goto out; |
| 639 | |
Zhang Xiantao | 5bb064d | 2007-11-18 20:29:43 +0800 | [diff] [blame] | 640 | if (any) |
| 641 | *is_dirty = 1; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 642 | |
| 643 | r = 0; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 644 | out: |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 645 | return r; |
| 646 | } |
| 647 | |
Izik Eidus | cea7bb2 | 2007-10-17 19:17:48 +0200 | [diff] [blame] | 648 | int is_error_page(struct page *page) |
| 649 | { |
| 650 | return page == bad_page; |
| 651 | } |
| 652 | EXPORT_SYMBOL_GPL(is_error_page); |
| 653 | |
Anthony Liguori | 35149e2 | 2008-04-02 14:46:56 -0500 | [diff] [blame] | 654 | int is_error_pfn(pfn_t pfn) |
| 655 | { |
| 656 | return pfn == bad_pfn; |
| 657 | } |
| 658 | EXPORT_SYMBOL_GPL(is_error_pfn); |
| 659 | |
Izik Eidus | f9d46eb | 2007-11-11 22:02:22 +0200 | [diff] [blame] | 660 | static inline unsigned long bad_hva(void) |
| 661 | { |
| 662 | return PAGE_OFFSET; |
| 663 | } |
| 664 | |
| 665 | int kvm_is_error_hva(unsigned long addr) |
| 666 | { |
| 667 | return addr == bad_hva(); |
| 668 | } |
| 669 | EXPORT_SYMBOL_GPL(kvm_is_error_hva); |
| 670 | |
Avi Kivity | e820754 | 2007-03-30 16:54:30 +0300 | [diff] [blame] | 671 | static struct kvm_memory_slot *__gfn_to_memslot(struct kvm *kvm, gfn_t gfn) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 672 | { |
| 673 | int i; |
| 674 | |
| 675 | for (i = 0; i < kvm->nmemslots; ++i) { |
| 676 | struct kvm_memory_slot *memslot = &kvm->memslots[i]; |
| 677 | |
| 678 | if (gfn >= memslot->base_gfn |
| 679 | && gfn < memslot->base_gfn + memslot->npages) |
| 680 | return memslot; |
| 681 | } |
Al Viro | 8b6d44c | 2007-02-09 16:38:40 +0000 | [diff] [blame] | 682 | return NULL; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 683 | } |
Avi Kivity | e820754 | 2007-03-30 16:54:30 +0300 | [diff] [blame] | 684 | |
| 685 | struct kvm_memory_slot *gfn_to_memslot(struct kvm *kvm, gfn_t gfn) |
| 686 | { |
| 687 | gfn = unalias_gfn(kvm, gfn); |
| 688 | return __gfn_to_memslot(kvm, gfn); |
| 689 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 690 | |
Izik Eidus | e0d62c7 | 2007-10-24 23:57:46 +0200 | [diff] [blame] | 691 | int kvm_is_visible_gfn(struct kvm *kvm, gfn_t gfn) |
| 692 | { |
| 693 | int i; |
| 694 | |
| 695 | gfn = unalias_gfn(kvm, gfn); |
| 696 | for (i = 0; i < KVM_MEMORY_SLOTS; ++i) { |
| 697 | struct kvm_memory_slot *memslot = &kvm->memslots[i]; |
| 698 | |
| 699 | if (gfn >= memslot->base_gfn |
| 700 | && gfn < memslot->base_gfn + memslot->npages) |
| 701 | return 1; |
| 702 | } |
| 703 | return 0; |
| 704 | } |
| 705 | EXPORT_SYMBOL_GPL(kvm_is_visible_gfn); |
| 706 | |
Marcelo Tosatti | 05da455 | 2008-02-23 11:44:30 -0300 | [diff] [blame] | 707 | unsigned long gfn_to_hva(struct kvm *kvm, gfn_t gfn) |
Izik Eidus | 539cb66 | 2007-11-11 22:05:04 +0200 | [diff] [blame] | 708 | { |
| 709 | struct kvm_memory_slot *slot; |
| 710 | |
| 711 | gfn = unalias_gfn(kvm, gfn); |
| 712 | slot = __gfn_to_memslot(kvm, gfn); |
| 713 | if (!slot) |
| 714 | return bad_hva(); |
| 715 | return (slot->userspace_addr + (gfn - slot->base_gfn) * PAGE_SIZE); |
| 716 | } |
Sheng Yang | 0d15029 | 2008-04-25 21:44:50 +0800 | [diff] [blame] | 717 | EXPORT_SYMBOL_GPL(gfn_to_hva); |
Izik Eidus | 539cb66 | 2007-11-11 22:05:04 +0200 | [diff] [blame] | 718 | |
Anthony Liguori | aab61cc | 2007-10-29 15:15:20 -0500 | [diff] [blame] | 719 | /* |
| 720 | * Requires current->mm->mmap_sem to be held |
| 721 | */ |
Anthony Liguori | 35149e2 | 2008-04-02 14:46:56 -0500 | [diff] [blame] | 722 | pfn_t gfn_to_pfn(struct kvm *kvm, gfn_t gfn) |
Avi Kivity | 954bbbc | 2007-03-30 14:02:32 +0300 | [diff] [blame] | 723 | { |
Anthony Liguori | 8d4e128 | 2007-10-18 09:59:34 -0500 | [diff] [blame] | 724 | struct page *page[1]; |
Izik Eidus | 539cb66 | 2007-11-11 22:05:04 +0200 | [diff] [blame] | 725 | unsigned long addr; |
Anthony Liguori | 8d4e128 | 2007-10-18 09:59:34 -0500 | [diff] [blame] | 726 | int npages; |
Anthony Liguori | 2e2e373 | 2008-04-30 15:37:07 -0500 | [diff] [blame] | 727 | pfn_t pfn; |
Avi Kivity | 954bbbc | 2007-03-30 14:02:32 +0300 | [diff] [blame] | 728 | |
Avi Kivity | 6039522 | 2007-10-21 11:03:36 +0200 | [diff] [blame] | 729 | might_sleep(); |
| 730 | |
Izik Eidus | 539cb66 | 2007-11-11 22:05:04 +0200 | [diff] [blame] | 731 | addr = gfn_to_hva(kvm, gfn); |
| 732 | if (kvm_is_error_hva(addr)) { |
Izik Eidus | 8a7ae05 | 2007-10-18 11:09:33 +0200 | [diff] [blame] | 733 | get_page(bad_page); |
Anthony Liguori | 35149e2 | 2008-04-02 14:46:56 -0500 | [diff] [blame] | 734 | return page_to_pfn(bad_page); |
Izik Eidus | 8a7ae05 | 2007-10-18 11:09:33 +0200 | [diff] [blame] | 735 | } |
Izik Eidus | 8a7ae05 | 2007-10-18 11:09:33 +0200 | [diff] [blame] | 736 | |
Izik Eidus | 539cb66 | 2007-11-11 22:05:04 +0200 | [diff] [blame] | 737 | npages = get_user_pages(current, current->mm, addr, 1, 1, 1, page, |
| 738 | NULL); |
| 739 | |
Anthony Liguori | 2e2e373 | 2008-04-30 15:37:07 -0500 | [diff] [blame] | 740 | if (unlikely(npages != 1)) { |
| 741 | struct vm_area_struct *vma; |
Anthony Liguori | 8d4e128 | 2007-10-18 09:59:34 -0500 | [diff] [blame] | 742 | |
Anthony Liguori | 2e2e373 | 2008-04-30 15:37:07 -0500 | [diff] [blame] | 743 | vma = find_vma(current->mm, addr); |
| 744 | if (vma == NULL || addr < vma->vm_start || |
| 745 | !(vma->vm_flags & VM_PFNMAP)) { |
| 746 | get_page(bad_page); |
| 747 | return page_to_pfn(bad_page); |
| 748 | } |
| 749 | |
| 750 | pfn = ((addr - vma->vm_start) >> PAGE_SHIFT) + vma->vm_pgoff; |
Ben-Ami Yassour | cbff90a | 2008-07-28 19:26:24 +0300 | [diff] [blame^] | 751 | BUG_ON(!is_mmio_pfn(pfn)); |
Anthony Liguori | 2e2e373 | 2008-04-30 15:37:07 -0500 | [diff] [blame] | 752 | } else |
| 753 | pfn = page_to_pfn(page[0]); |
| 754 | |
| 755 | return pfn; |
Anthony Liguori | 35149e2 | 2008-04-02 14:46:56 -0500 | [diff] [blame] | 756 | } |
| 757 | |
| 758 | EXPORT_SYMBOL_GPL(gfn_to_pfn); |
| 759 | |
| 760 | struct page *gfn_to_page(struct kvm *kvm, gfn_t gfn) |
| 761 | { |
Anthony Liguori | 2e2e373 | 2008-04-30 15:37:07 -0500 | [diff] [blame] | 762 | pfn_t pfn; |
| 763 | |
| 764 | pfn = gfn_to_pfn(kvm, gfn); |
Ben-Ami Yassour | cbff90a | 2008-07-28 19:26:24 +0300 | [diff] [blame^] | 765 | if (!is_mmio_pfn(pfn)) |
Anthony Liguori | 2e2e373 | 2008-04-30 15:37:07 -0500 | [diff] [blame] | 766 | return pfn_to_page(pfn); |
| 767 | |
Ben-Ami Yassour | cbff90a | 2008-07-28 19:26:24 +0300 | [diff] [blame^] | 768 | WARN_ON(is_mmio_pfn(pfn)); |
Anthony Liguori | 2e2e373 | 2008-04-30 15:37:07 -0500 | [diff] [blame] | 769 | |
| 770 | get_page(bad_page); |
| 771 | return bad_page; |
Avi Kivity | 954bbbc | 2007-03-30 14:02:32 +0300 | [diff] [blame] | 772 | } |
Anthony Liguori | aab61cc | 2007-10-29 15:15:20 -0500 | [diff] [blame] | 773 | |
Avi Kivity | 954bbbc | 2007-03-30 14:02:32 +0300 | [diff] [blame] | 774 | EXPORT_SYMBOL_GPL(gfn_to_page); |
| 775 | |
Izik Eidus | b4231d6 | 2007-11-20 11:49:33 +0200 | [diff] [blame] | 776 | void kvm_release_page_clean(struct page *page) |
| 777 | { |
Anthony Liguori | 35149e2 | 2008-04-02 14:46:56 -0500 | [diff] [blame] | 778 | kvm_release_pfn_clean(page_to_pfn(page)); |
Izik Eidus | b4231d6 | 2007-11-20 11:49:33 +0200 | [diff] [blame] | 779 | } |
| 780 | EXPORT_SYMBOL_GPL(kvm_release_page_clean); |
| 781 | |
Anthony Liguori | 35149e2 | 2008-04-02 14:46:56 -0500 | [diff] [blame] | 782 | void kvm_release_pfn_clean(pfn_t pfn) |
| 783 | { |
Ben-Ami Yassour | cbff90a | 2008-07-28 19:26:24 +0300 | [diff] [blame^] | 784 | if (!is_mmio_pfn(pfn)) |
Anthony Liguori | 2e2e373 | 2008-04-30 15:37:07 -0500 | [diff] [blame] | 785 | put_page(pfn_to_page(pfn)); |
Anthony Liguori | 35149e2 | 2008-04-02 14:46:56 -0500 | [diff] [blame] | 786 | } |
| 787 | EXPORT_SYMBOL_GPL(kvm_release_pfn_clean); |
| 788 | |
Izik Eidus | b4231d6 | 2007-11-20 11:49:33 +0200 | [diff] [blame] | 789 | void kvm_release_page_dirty(struct page *page) |
Izik Eidus | 8a7ae05 | 2007-10-18 11:09:33 +0200 | [diff] [blame] | 790 | { |
Anthony Liguori | 35149e2 | 2008-04-02 14:46:56 -0500 | [diff] [blame] | 791 | kvm_release_pfn_dirty(page_to_pfn(page)); |
Izik Eidus | 8a7ae05 | 2007-10-18 11:09:33 +0200 | [diff] [blame] | 792 | } |
Izik Eidus | b4231d6 | 2007-11-20 11:49:33 +0200 | [diff] [blame] | 793 | EXPORT_SYMBOL_GPL(kvm_release_page_dirty); |
Izik Eidus | 8a7ae05 | 2007-10-18 11:09:33 +0200 | [diff] [blame] | 794 | |
Anthony Liguori | 35149e2 | 2008-04-02 14:46:56 -0500 | [diff] [blame] | 795 | void kvm_release_pfn_dirty(pfn_t pfn) |
| 796 | { |
| 797 | kvm_set_pfn_dirty(pfn); |
| 798 | kvm_release_pfn_clean(pfn); |
| 799 | } |
| 800 | EXPORT_SYMBOL_GPL(kvm_release_pfn_dirty); |
| 801 | |
| 802 | void kvm_set_page_dirty(struct page *page) |
| 803 | { |
| 804 | kvm_set_pfn_dirty(page_to_pfn(page)); |
| 805 | } |
| 806 | EXPORT_SYMBOL_GPL(kvm_set_page_dirty); |
| 807 | |
| 808 | void kvm_set_pfn_dirty(pfn_t pfn) |
| 809 | { |
Ben-Ami Yassour | cbff90a | 2008-07-28 19:26:24 +0300 | [diff] [blame^] | 810 | if (!is_mmio_pfn(pfn)) { |
Anthony Liguori | 2e2e373 | 2008-04-30 15:37:07 -0500 | [diff] [blame] | 811 | struct page *page = pfn_to_page(pfn); |
| 812 | if (!PageReserved(page)) |
| 813 | SetPageDirty(page); |
| 814 | } |
Anthony Liguori | 35149e2 | 2008-04-02 14:46:56 -0500 | [diff] [blame] | 815 | } |
| 816 | EXPORT_SYMBOL_GPL(kvm_set_pfn_dirty); |
| 817 | |
| 818 | void kvm_set_pfn_accessed(pfn_t pfn) |
| 819 | { |
Ben-Ami Yassour | cbff90a | 2008-07-28 19:26:24 +0300 | [diff] [blame^] | 820 | if (!is_mmio_pfn(pfn)) |
Anthony Liguori | 2e2e373 | 2008-04-30 15:37:07 -0500 | [diff] [blame] | 821 | mark_page_accessed(pfn_to_page(pfn)); |
Anthony Liguori | 35149e2 | 2008-04-02 14:46:56 -0500 | [diff] [blame] | 822 | } |
| 823 | EXPORT_SYMBOL_GPL(kvm_set_pfn_accessed); |
| 824 | |
| 825 | void kvm_get_pfn(pfn_t pfn) |
| 826 | { |
Ben-Ami Yassour | cbff90a | 2008-07-28 19:26:24 +0300 | [diff] [blame^] | 827 | if (!is_mmio_pfn(pfn)) |
Anthony Liguori | 2e2e373 | 2008-04-30 15:37:07 -0500 | [diff] [blame] | 828 | get_page(pfn_to_page(pfn)); |
Anthony Liguori | 35149e2 | 2008-04-02 14:46:56 -0500 | [diff] [blame] | 829 | } |
| 830 | EXPORT_SYMBOL_GPL(kvm_get_pfn); |
| 831 | |
Izik Eidus | 195aefd | 2007-10-01 22:14:18 +0200 | [diff] [blame] | 832 | static int next_segment(unsigned long len, int offset) |
| 833 | { |
| 834 | if (len > PAGE_SIZE - offset) |
| 835 | return PAGE_SIZE - offset; |
| 836 | else |
| 837 | return len; |
| 838 | } |
| 839 | |
| 840 | int kvm_read_guest_page(struct kvm *kvm, gfn_t gfn, void *data, int offset, |
| 841 | int len) |
| 842 | { |
Izik Eidus | e0506bc | 2007-11-11 22:10:22 +0200 | [diff] [blame] | 843 | int r; |
| 844 | unsigned long addr; |
Izik Eidus | 195aefd | 2007-10-01 22:14:18 +0200 | [diff] [blame] | 845 | |
Izik Eidus | e0506bc | 2007-11-11 22:10:22 +0200 | [diff] [blame] | 846 | addr = gfn_to_hva(kvm, gfn); |
| 847 | if (kvm_is_error_hva(addr)) |
Izik Eidus | 195aefd | 2007-10-01 22:14:18 +0200 | [diff] [blame] | 848 | return -EFAULT; |
Izik Eidus | e0506bc | 2007-11-11 22:10:22 +0200 | [diff] [blame] | 849 | r = copy_from_user(data, (void __user *)addr + offset, len); |
| 850 | if (r) |
| 851 | return -EFAULT; |
Izik Eidus | 195aefd | 2007-10-01 22:14:18 +0200 | [diff] [blame] | 852 | return 0; |
| 853 | } |
| 854 | EXPORT_SYMBOL_GPL(kvm_read_guest_page); |
| 855 | |
| 856 | int kvm_read_guest(struct kvm *kvm, gpa_t gpa, void *data, unsigned long len) |
| 857 | { |
| 858 | gfn_t gfn = gpa >> PAGE_SHIFT; |
| 859 | int seg; |
| 860 | int offset = offset_in_page(gpa); |
| 861 | int ret; |
| 862 | |
| 863 | while ((seg = next_segment(len, offset)) != 0) { |
| 864 | ret = kvm_read_guest_page(kvm, gfn, data, offset, seg); |
| 865 | if (ret < 0) |
| 866 | return ret; |
| 867 | offset = 0; |
| 868 | len -= seg; |
| 869 | data += seg; |
| 870 | ++gfn; |
| 871 | } |
| 872 | return 0; |
| 873 | } |
| 874 | EXPORT_SYMBOL_GPL(kvm_read_guest); |
| 875 | |
Marcelo Tosatti | 7ec5458 | 2007-12-20 19:18:23 -0500 | [diff] [blame] | 876 | int kvm_read_guest_atomic(struct kvm *kvm, gpa_t gpa, void *data, |
| 877 | unsigned long len) |
| 878 | { |
| 879 | int r; |
| 880 | unsigned long addr; |
| 881 | gfn_t gfn = gpa >> PAGE_SHIFT; |
| 882 | int offset = offset_in_page(gpa); |
| 883 | |
| 884 | addr = gfn_to_hva(kvm, gfn); |
| 885 | if (kvm_is_error_hva(addr)) |
| 886 | return -EFAULT; |
Andrea Arcangeli | 0aac03f | 2008-01-30 19:57:35 +0100 | [diff] [blame] | 887 | pagefault_disable(); |
Marcelo Tosatti | 7ec5458 | 2007-12-20 19:18:23 -0500 | [diff] [blame] | 888 | r = __copy_from_user_inatomic(data, (void __user *)addr + offset, len); |
Andrea Arcangeli | 0aac03f | 2008-01-30 19:57:35 +0100 | [diff] [blame] | 889 | pagefault_enable(); |
Marcelo Tosatti | 7ec5458 | 2007-12-20 19:18:23 -0500 | [diff] [blame] | 890 | if (r) |
| 891 | return -EFAULT; |
| 892 | return 0; |
| 893 | } |
| 894 | EXPORT_SYMBOL(kvm_read_guest_atomic); |
| 895 | |
Izik Eidus | 195aefd | 2007-10-01 22:14:18 +0200 | [diff] [blame] | 896 | int kvm_write_guest_page(struct kvm *kvm, gfn_t gfn, const void *data, |
| 897 | int offset, int len) |
| 898 | { |
Izik Eidus | e0506bc | 2007-11-11 22:10:22 +0200 | [diff] [blame] | 899 | int r; |
| 900 | unsigned long addr; |
Izik Eidus | 195aefd | 2007-10-01 22:14:18 +0200 | [diff] [blame] | 901 | |
Izik Eidus | e0506bc | 2007-11-11 22:10:22 +0200 | [diff] [blame] | 902 | addr = gfn_to_hva(kvm, gfn); |
| 903 | if (kvm_is_error_hva(addr)) |
Izik Eidus | 195aefd | 2007-10-01 22:14:18 +0200 | [diff] [blame] | 904 | return -EFAULT; |
Izik Eidus | e0506bc | 2007-11-11 22:10:22 +0200 | [diff] [blame] | 905 | r = copy_to_user((void __user *)addr + offset, data, len); |
| 906 | if (r) |
| 907 | return -EFAULT; |
Izik Eidus | 195aefd | 2007-10-01 22:14:18 +0200 | [diff] [blame] | 908 | mark_page_dirty(kvm, gfn); |
| 909 | return 0; |
| 910 | } |
| 911 | EXPORT_SYMBOL_GPL(kvm_write_guest_page); |
| 912 | |
| 913 | int kvm_write_guest(struct kvm *kvm, gpa_t gpa, const void *data, |
| 914 | unsigned long len) |
| 915 | { |
| 916 | gfn_t gfn = gpa >> PAGE_SHIFT; |
| 917 | int seg; |
| 918 | int offset = offset_in_page(gpa); |
| 919 | int ret; |
| 920 | |
| 921 | while ((seg = next_segment(len, offset)) != 0) { |
| 922 | ret = kvm_write_guest_page(kvm, gfn, data, offset, seg); |
| 923 | if (ret < 0) |
| 924 | return ret; |
| 925 | offset = 0; |
| 926 | len -= seg; |
| 927 | data += seg; |
| 928 | ++gfn; |
| 929 | } |
| 930 | return 0; |
| 931 | } |
| 932 | |
| 933 | int kvm_clear_guest_page(struct kvm *kvm, gfn_t gfn, int offset, int len) |
| 934 | { |
Izik Eidus | 3e021bf | 2007-11-19 11:16:57 +0200 | [diff] [blame] | 935 | return kvm_write_guest_page(kvm, gfn, empty_zero_page, offset, len); |
Izik Eidus | 195aefd | 2007-10-01 22:14:18 +0200 | [diff] [blame] | 936 | } |
| 937 | EXPORT_SYMBOL_GPL(kvm_clear_guest_page); |
| 938 | |
| 939 | int kvm_clear_guest(struct kvm *kvm, gpa_t gpa, unsigned long len) |
| 940 | { |
| 941 | gfn_t gfn = gpa >> PAGE_SHIFT; |
| 942 | int seg; |
| 943 | int offset = offset_in_page(gpa); |
| 944 | int ret; |
| 945 | |
| 946 | while ((seg = next_segment(len, offset)) != 0) { |
| 947 | ret = kvm_clear_guest_page(kvm, gfn, offset, seg); |
| 948 | if (ret < 0) |
| 949 | return ret; |
| 950 | offset = 0; |
| 951 | len -= seg; |
| 952 | ++gfn; |
| 953 | } |
| 954 | return 0; |
| 955 | } |
| 956 | EXPORT_SYMBOL_GPL(kvm_clear_guest); |
| 957 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 958 | void mark_page_dirty(struct kvm *kvm, gfn_t gfn) |
| 959 | { |
Nguyen Anh Quynh | 3138994 | 2007-06-05 10:35:19 +0300 | [diff] [blame] | 960 | struct kvm_memory_slot *memslot; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 961 | |
Uri Lublin | 3b6fff1 | 2007-10-30 10:42:09 +0200 | [diff] [blame] | 962 | gfn = unalias_gfn(kvm, gfn); |
Rusty Russell | 7e9d619 | 2007-07-31 20:41:14 +1000 | [diff] [blame] | 963 | memslot = __gfn_to_memslot(kvm, gfn); |
| 964 | if (memslot && memslot->dirty_bitmap) { |
| 965 | unsigned long rel_gfn = gfn - memslot->base_gfn; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 966 | |
Rusty Russell | 7e9d619 | 2007-07-31 20:41:14 +1000 | [diff] [blame] | 967 | /* avoid RMW */ |
| 968 | if (!test_bit(rel_gfn, memslot->dirty_bitmap)) |
| 969 | set_bit(rel_gfn, memslot->dirty_bitmap); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 970 | } |
| 971 | } |
| 972 | |
Eddie Dong | b6958ce | 2007-07-18 12:15:21 +0300 | [diff] [blame] | 973 | /* |
| 974 | * The vCPU has executed a HLT instruction with in-kernel mode enabled. |
| 975 | */ |
Hollis Blanchard | 8776e51 | 2007-10-31 17:24:24 -0500 | [diff] [blame] | 976 | void kvm_vcpu_block(struct kvm_vcpu *vcpu) |
Eddie Dong | b6958ce | 2007-07-18 12:15:21 +0300 | [diff] [blame] | 977 | { |
Marcelo Tosatti | e5c239c | 2008-05-08 19:47:01 -0300 | [diff] [blame] | 978 | DEFINE_WAIT(wait); |
Eddie Dong | b6958ce | 2007-07-18 12:15:21 +0300 | [diff] [blame] | 979 | |
Marcelo Tosatti | e5c239c | 2008-05-08 19:47:01 -0300 | [diff] [blame] | 980 | for (;;) { |
| 981 | prepare_to_wait(&vcpu->wq, &wait, TASK_INTERRUPTIBLE); |
Eddie Dong | b6958ce | 2007-07-18 12:15:21 +0300 | [diff] [blame] | 982 | |
Marcelo Tosatti | e5c239c | 2008-05-08 19:47:01 -0300 | [diff] [blame] | 983 | if (kvm_cpu_has_interrupt(vcpu)) |
| 984 | break; |
| 985 | if (kvm_cpu_has_pending_timer(vcpu)) |
| 986 | break; |
| 987 | if (kvm_arch_vcpu_runnable(vcpu)) |
| 988 | break; |
| 989 | if (signal_pending(current)) |
| 990 | break; |
| 991 | |
Eddie Dong | b6958ce | 2007-07-18 12:15:21 +0300 | [diff] [blame] | 992 | vcpu_put(vcpu); |
| 993 | schedule(); |
| 994 | vcpu_load(vcpu); |
| 995 | } |
| 996 | |
Marcelo Tosatti | e5c239c | 2008-05-08 19:47:01 -0300 | [diff] [blame] | 997 | finish_wait(&vcpu->wq, &wait); |
Eddie Dong | b6958ce | 2007-07-18 12:15:21 +0300 | [diff] [blame] | 998 | } |
| 999 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1000 | void kvm_resched(struct kvm_vcpu *vcpu) |
| 1001 | { |
Yaozu Dong | 3fca036 | 2007-04-25 16:49:19 +0300 | [diff] [blame] | 1002 | if (!need_resched()) |
| 1003 | return; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1004 | cond_resched(); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1005 | } |
| 1006 | EXPORT_SYMBOL_GPL(kvm_resched); |
| 1007 | |
npiggin@suse.de | e4a533a | 2007-12-05 18:15:52 +1100 | [diff] [blame] | 1008 | static int kvm_vcpu_fault(struct vm_area_struct *vma, struct vm_fault *vmf) |
Avi Kivity | 9a2bb7f | 2007-02-22 12:58:31 +0200 | [diff] [blame] | 1009 | { |
| 1010 | struct kvm_vcpu *vcpu = vma->vm_file->private_data; |
Avi Kivity | 9a2bb7f | 2007-02-22 12:58:31 +0200 | [diff] [blame] | 1011 | struct page *page; |
| 1012 | |
npiggin@suse.de | e4a533a | 2007-12-05 18:15:52 +1100 | [diff] [blame] | 1013 | if (vmf->pgoff == 0) |
Avi Kivity | 039576c | 2007-03-20 12:46:50 +0200 | [diff] [blame] | 1014 | page = virt_to_page(vcpu->run); |
Avi Kivity | 0956676 | 2008-01-23 18:14:23 +0200 | [diff] [blame] | 1015 | #ifdef CONFIG_X86 |
npiggin@suse.de | e4a533a | 2007-12-05 18:15:52 +1100 | [diff] [blame] | 1016 | else if (vmf->pgoff == KVM_PIO_PAGE_OFFSET) |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 1017 | page = virt_to_page(vcpu->arch.pio_data); |
Avi Kivity | 0956676 | 2008-01-23 18:14:23 +0200 | [diff] [blame] | 1018 | #endif |
Laurent Vivier | 5f94c17 | 2008-05-30 16:05:54 +0200 | [diff] [blame] | 1019 | #ifdef KVM_COALESCED_MMIO_PAGE_OFFSET |
| 1020 | else if (vmf->pgoff == KVM_COALESCED_MMIO_PAGE_OFFSET) |
| 1021 | page = virt_to_page(vcpu->kvm->coalesced_mmio_ring); |
| 1022 | #endif |
Avi Kivity | 039576c | 2007-03-20 12:46:50 +0200 | [diff] [blame] | 1023 | else |
npiggin@suse.de | e4a533a | 2007-12-05 18:15:52 +1100 | [diff] [blame] | 1024 | return VM_FAULT_SIGBUS; |
Avi Kivity | 9a2bb7f | 2007-02-22 12:58:31 +0200 | [diff] [blame] | 1025 | get_page(page); |
npiggin@suse.de | e4a533a | 2007-12-05 18:15:52 +1100 | [diff] [blame] | 1026 | vmf->page = page; |
| 1027 | return 0; |
Avi Kivity | 9a2bb7f | 2007-02-22 12:58:31 +0200 | [diff] [blame] | 1028 | } |
| 1029 | |
| 1030 | static struct vm_operations_struct kvm_vcpu_vm_ops = { |
npiggin@suse.de | e4a533a | 2007-12-05 18:15:52 +1100 | [diff] [blame] | 1031 | .fault = kvm_vcpu_fault, |
Avi Kivity | 9a2bb7f | 2007-02-22 12:58:31 +0200 | [diff] [blame] | 1032 | }; |
| 1033 | |
| 1034 | static int kvm_vcpu_mmap(struct file *file, struct vm_area_struct *vma) |
| 1035 | { |
| 1036 | vma->vm_ops = &kvm_vcpu_vm_ops; |
| 1037 | return 0; |
| 1038 | } |
| 1039 | |
Avi Kivity | bccf215 | 2007-02-21 18:04:26 +0200 | [diff] [blame] | 1040 | static int kvm_vcpu_release(struct inode *inode, struct file *filp) |
| 1041 | { |
| 1042 | struct kvm_vcpu *vcpu = filp->private_data; |
| 1043 | |
Al Viro | 66c0b39 | 2008-04-19 20:33:56 +0100 | [diff] [blame] | 1044 | kvm_put_kvm(vcpu->kvm); |
Avi Kivity | bccf215 | 2007-02-21 18:04:26 +0200 | [diff] [blame] | 1045 | return 0; |
| 1046 | } |
| 1047 | |
Jan Engelhardt | 5c50274 | 2008-01-22 20:46:14 +0100 | [diff] [blame] | 1048 | static const struct file_operations kvm_vcpu_fops = { |
Avi Kivity | bccf215 | 2007-02-21 18:04:26 +0200 | [diff] [blame] | 1049 | .release = kvm_vcpu_release, |
| 1050 | .unlocked_ioctl = kvm_vcpu_ioctl, |
| 1051 | .compat_ioctl = kvm_vcpu_ioctl, |
Avi Kivity | 9a2bb7f | 2007-02-22 12:58:31 +0200 | [diff] [blame] | 1052 | .mmap = kvm_vcpu_mmap, |
Avi Kivity | bccf215 | 2007-02-21 18:04:26 +0200 | [diff] [blame] | 1053 | }; |
| 1054 | |
| 1055 | /* |
| 1056 | * Allocates an inode for the vcpu. |
| 1057 | */ |
| 1058 | static int create_vcpu_fd(struct kvm_vcpu *vcpu) |
| 1059 | { |
Ulrich Drepper | 7d9dbca | 2008-07-23 21:29:22 -0700 | [diff] [blame] | 1060 | int fd = anon_inode_getfd("kvm-vcpu", &kvm_vcpu_fops, vcpu, 0); |
Al Viro | 2030a42 | 2008-02-23 06:46:49 -0500 | [diff] [blame] | 1061 | if (fd < 0) |
Al Viro | 66c0b39 | 2008-04-19 20:33:56 +0100 | [diff] [blame] | 1062 | kvm_put_kvm(vcpu->kvm); |
Avi Kivity | bccf215 | 2007-02-21 18:04:26 +0200 | [diff] [blame] | 1063 | return fd; |
Avi Kivity | bccf215 | 2007-02-21 18:04:26 +0200 | [diff] [blame] | 1064 | } |
| 1065 | |
Avi Kivity | c5ea766 | 2007-02-20 18:41:05 +0200 | [diff] [blame] | 1066 | /* |
| 1067 | * Creates some virtual cpus. Good luck creating more than one. |
| 1068 | */ |
| 1069 | static int kvm_vm_ioctl_create_vcpu(struct kvm *kvm, int n) |
| 1070 | { |
| 1071 | int r; |
| 1072 | struct kvm_vcpu *vcpu; |
| 1073 | |
Avi Kivity | c5ea766 | 2007-02-20 18:41:05 +0200 | [diff] [blame] | 1074 | if (!valid_vcpu(n)) |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 1075 | return -EINVAL; |
Avi Kivity | c5ea766 | 2007-02-20 18:41:05 +0200 | [diff] [blame] | 1076 | |
Zhang Xiantao | e9b11c1 | 2007-11-14 20:38:21 +0800 | [diff] [blame] | 1077 | vcpu = kvm_arch_vcpu_create(kvm, n); |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 1078 | if (IS_ERR(vcpu)) |
| 1079 | return PTR_ERR(vcpu); |
Avi Kivity | c5ea766 | 2007-02-20 18:41:05 +0200 | [diff] [blame] | 1080 | |
Avi Kivity | 15ad714 | 2007-07-11 18:17:21 +0300 | [diff] [blame] | 1081 | preempt_notifier_init(&vcpu->preempt_notifier, &kvm_preempt_ops); |
| 1082 | |
Avi Kivity | 26e5215 | 2007-11-20 15:30:24 +0200 | [diff] [blame] | 1083 | r = kvm_arch_vcpu_setup(vcpu); |
| 1084 | if (r) |
| 1085 | goto vcpu_destroy; |
| 1086 | |
Shaohua Li | 11ec280 | 2007-07-23 14:51:37 +0800 | [diff] [blame] | 1087 | mutex_lock(&kvm->lock); |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 1088 | if (kvm->vcpus[n]) { |
| 1089 | r = -EEXIST; |
Shaohua Li | 11ec280 | 2007-07-23 14:51:37 +0800 | [diff] [blame] | 1090 | mutex_unlock(&kvm->lock); |
Zhang Xiantao | e9b11c1 | 2007-11-14 20:38:21 +0800 | [diff] [blame] | 1091 | goto vcpu_destroy; |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 1092 | } |
| 1093 | kvm->vcpus[n] = vcpu; |
Shaohua Li | 11ec280 | 2007-07-23 14:51:37 +0800 | [diff] [blame] | 1094 | mutex_unlock(&kvm->lock); |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 1095 | |
| 1096 | /* Now it's all set up, let userspace reach it */ |
Al Viro | 66c0b39 | 2008-04-19 20:33:56 +0100 | [diff] [blame] | 1097 | kvm_get_kvm(kvm); |
Avi Kivity | bccf215 | 2007-02-21 18:04:26 +0200 | [diff] [blame] | 1098 | r = create_vcpu_fd(vcpu); |
| 1099 | if (r < 0) |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 1100 | goto unlink; |
Avi Kivity | bccf215 | 2007-02-21 18:04:26 +0200 | [diff] [blame] | 1101 | return r; |
Avi Kivity | c5ea766 | 2007-02-20 18:41:05 +0200 | [diff] [blame] | 1102 | |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 1103 | unlink: |
Shaohua Li | 11ec280 | 2007-07-23 14:51:37 +0800 | [diff] [blame] | 1104 | mutex_lock(&kvm->lock); |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 1105 | kvm->vcpus[n] = NULL; |
Shaohua Li | 11ec280 | 2007-07-23 14:51:37 +0800 | [diff] [blame] | 1106 | mutex_unlock(&kvm->lock); |
Zhang Xiantao | e9b11c1 | 2007-11-14 20:38:21 +0800 | [diff] [blame] | 1107 | vcpu_destroy: |
Hollis Blanchard | d40ccc6 | 2007-11-19 14:04:43 -0600 | [diff] [blame] | 1108 | kvm_arch_vcpu_destroy(vcpu); |
Avi Kivity | c5ea766 | 2007-02-20 18:41:05 +0200 | [diff] [blame] | 1109 | return r; |
| 1110 | } |
| 1111 | |
Avi Kivity | 1961d27 | 2007-03-05 19:46:05 +0200 | [diff] [blame] | 1112 | static int kvm_vcpu_ioctl_set_sigmask(struct kvm_vcpu *vcpu, sigset_t *sigset) |
| 1113 | { |
| 1114 | if (sigset) { |
| 1115 | sigdelsetmask(sigset, sigmask(SIGKILL)|sigmask(SIGSTOP)); |
| 1116 | vcpu->sigset_active = 1; |
| 1117 | vcpu->sigset = *sigset; |
| 1118 | } else |
| 1119 | vcpu->sigset_active = 0; |
| 1120 | return 0; |
| 1121 | } |
| 1122 | |
Avi Kivity | bccf215 | 2007-02-21 18:04:26 +0200 | [diff] [blame] | 1123 | static long kvm_vcpu_ioctl(struct file *filp, |
| 1124 | unsigned int ioctl, unsigned long arg) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1125 | { |
Avi Kivity | bccf215 | 2007-02-21 18:04:26 +0200 | [diff] [blame] | 1126 | struct kvm_vcpu *vcpu = filp->private_data; |
Al Viro | 2f366987 | 2007-02-09 16:38:35 +0000 | [diff] [blame] | 1127 | void __user *argp = (void __user *)arg; |
Carsten Otte | 313a3dc | 2007-10-11 19:16:52 +0200 | [diff] [blame] | 1128 | int r; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1129 | |
Avi Kivity | 6d4e4c4 | 2007-11-21 16:41:05 +0200 | [diff] [blame] | 1130 | if (vcpu->kvm->mm != current->mm) |
| 1131 | return -EIO; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1132 | switch (ioctl) { |
Avi Kivity | 9a2bb7f | 2007-02-22 12:58:31 +0200 | [diff] [blame] | 1133 | case KVM_RUN: |
Avi Kivity | f0fe510 | 2007-03-07 13:11:17 +0200 | [diff] [blame] | 1134 | r = -EINVAL; |
| 1135 | if (arg) |
| 1136 | goto out; |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 1137 | r = kvm_arch_vcpu_ioctl_run(vcpu, vcpu->run); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1138 | break; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1139 | case KVM_GET_REGS: { |
Xiantao Zhang | 3e4bb3a | 2008-02-25 18:52:20 +0800 | [diff] [blame] | 1140 | struct kvm_regs *kvm_regs; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1141 | |
Xiantao Zhang | 3e4bb3a | 2008-02-25 18:52:20 +0800 | [diff] [blame] | 1142 | r = -ENOMEM; |
| 1143 | kvm_regs = kzalloc(sizeof(struct kvm_regs), GFP_KERNEL); |
| 1144 | if (!kvm_regs) |
| 1145 | goto out; |
| 1146 | r = kvm_arch_vcpu_ioctl_get_regs(vcpu, kvm_regs); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1147 | if (r) |
Xiantao Zhang | 3e4bb3a | 2008-02-25 18:52:20 +0800 | [diff] [blame] | 1148 | goto out_free1; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1149 | r = -EFAULT; |
Xiantao Zhang | 3e4bb3a | 2008-02-25 18:52:20 +0800 | [diff] [blame] | 1150 | if (copy_to_user(argp, kvm_regs, sizeof(struct kvm_regs))) |
| 1151 | goto out_free1; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1152 | r = 0; |
Xiantao Zhang | 3e4bb3a | 2008-02-25 18:52:20 +0800 | [diff] [blame] | 1153 | out_free1: |
| 1154 | kfree(kvm_regs); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1155 | break; |
| 1156 | } |
| 1157 | case KVM_SET_REGS: { |
Xiantao Zhang | 3e4bb3a | 2008-02-25 18:52:20 +0800 | [diff] [blame] | 1158 | struct kvm_regs *kvm_regs; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1159 | |
Xiantao Zhang | 3e4bb3a | 2008-02-25 18:52:20 +0800 | [diff] [blame] | 1160 | r = -ENOMEM; |
| 1161 | kvm_regs = kzalloc(sizeof(struct kvm_regs), GFP_KERNEL); |
| 1162 | if (!kvm_regs) |
| 1163 | goto out; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1164 | r = -EFAULT; |
Xiantao Zhang | 3e4bb3a | 2008-02-25 18:52:20 +0800 | [diff] [blame] | 1165 | if (copy_from_user(kvm_regs, argp, sizeof(struct kvm_regs))) |
| 1166 | goto out_free2; |
| 1167 | r = kvm_arch_vcpu_ioctl_set_regs(vcpu, kvm_regs); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1168 | if (r) |
Xiantao Zhang | 3e4bb3a | 2008-02-25 18:52:20 +0800 | [diff] [blame] | 1169 | goto out_free2; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1170 | r = 0; |
Xiantao Zhang | 3e4bb3a | 2008-02-25 18:52:20 +0800 | [diff] [blame] | 1171 | out_free2: |
| 1172 | kfree(kvm_regs); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1173 | break; |
| 1174 | } |
| 1175 | case KVM_GET_SREGS: { |
| 1176 | struct kvm_sregs kvm_sregs; |
| 1177 | |
Avi Kivity | bccf215 | 2007-02-21 18:04:26 +0200 | [diff] [blame] | 1178 | memset(&kvm_sregs, 0, sizeof kvm_sregs); |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 1179 | r = kvm_arch_vcpu_ioctl_get_sregs(vcpu, &kvm_sregs); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1180 | if (r) |
| 1181 | goto out; |
| 1182 | r = -EFAULT; |
Al Viro | 2f366987 | 2007-02-09 16:38:35 +0000 | [diff] [blame] | 1183 | if (copy_to_user(argp, &kvm_sregs, sizeof kvm_sregs)) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1184 | goto out; |
| 1185 | r = 0; |
| 1186 | break; |
| 1187 | } |
| 1188 | case KVM_SET_SREGS: { |
| 1189 | struct kvm_sregs kvm_sregs; |
| 1190 | |
| 1191 | r = -EFAULT; |
Al Viro | 2f366987 | 2007-02-09 16:38:35 +0000 | [diff] [blame] | 1192 | if (copy_from_user(&kvm_sregs, argp, sizeof kvm_sregs)) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1193 | goto out; |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 1194 | r = kvm_arch_vcpu_ioctl_set_sregs(vcpu, &kvm_sregs); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1195 | if (r) |
| 1196 | goto out; |
| 1197 | r = 0; |
| 1198 | break; |
| 1199 | } |
Marcelo Tosatti | 62d9f0d | 2008-04-11 13:24:45 -0300 | [diff] [blame] | 1200 | case KVM_GET_MP_STATE: { |
| 1201 | struct kvm_mp_state mp_state; |
| 1202 | |
| 1203 | r = kvm_arch_vcpu_ioctl_get_mpstate(vcpu, &mp_state); |
| 1204 | if (r) |
| 1205 | goto out; |
| 1206 | r = -EFAULT; |
| 1207 | if (copy_to_user(argp, &mp_state, sizeof mp_state)) |
| 1208 | goto out; |
| 1209 | r = 0; |
| 1210 | break; |
| 1211 | } |
| 1212 | case KVM_SET_MP_STATE: { |
| 1213 | struct kvm_mp_state mp_state; |
| 1214 | |
| 1215 | r = -EFAULT; |
| 1216 | if (copy_from_user(&mp_state, argp, sizeof mp_state)) |
| 1217 | goto out; |
| 1218 | r = kvm_arch_vcpu_ioctl_set_mpstate(vcpu, &mp_state); |
| 1219 | if (r) |
| 1220 | goto out; |
| 1221 | r = 0; |
| 1222 | break; |
| 1223 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1224 | case KVM_TRANSLATE: { |
| 1225 | struct kvm_translation tr; |
| 1226 | |
| 1227 | r = -EFAULT; |
Al Viro | 2f366987 | 2007-02-09 16:38:35 +0000 | [diff] [blame] | 1228 | if (copy_from_user(&tr, argp, sizeof tr)) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1229 | goto out; |
Zhang Xiantao | 8b00679 | 2007-11-16 13:05:55 +0800 | [diff] [blame] | 1230 | r = kvm_arch_vcpu_ioctl_translate(vcpu, &tr); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1231 | if (r) |
| 1232 | goto out; |
| 1233 | r = -EFAULT; |
Al Viro | 2f366987 | 2007-02-09 16:38:35 +0000 | [diff] [blame] | 1234 | if (copy_to_user(argp, &tr, sizeof tr)) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1235 | goto out; |
| 1236 | r = 0; |
| 1237 | break; |
| 1238 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1239 | case KVM_DEBUG_GUEST: { |
| 1240 | struct kvm_debug_guest dbg; |
| 1241 | |
| 1242 | r = -EFAULT; |
Al Viro | 2f366987 | 2007-02-09 16:38:35 +0000 | [diff] [blame] | 1243 | if (copy_from_user(&dbg, argp, sizeof dbg)) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1244 | goto out; |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 1245 | r = kvm_arch_vcpu_ioctl_debug_guest(vcpu, &dbg); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1246 | if (r) |
| 1247 | goto out; |
| 1248 | r = 0; |
| 1249 | break; |
| 1250 | } |
Avi Kivity | 1961d27 | 2007-03-05 19:46:05 +0200 | [diff] [blame] | 1251 | case KVM_SET_SIGNAL_MASK: { |
| 1252 | struct kvm_signal_mask __user *sigmask_arg = argp; |
| 1253 | struct kvm_signal_mask kvm_sigmask; |
| 1254 | sigset_t sigset, *p; |
| 1255 | |
| 1256 | p = NULL; |
| 1257 | if (argp) { |
| 1258 | r = -EFAULT; |
| 1259 | if (copy_from_user(&kvm_sigmask, argp, |
| 1260 | sizeof kvm_sigmask)) |
| 1261 | goto out; |
| 1262 | r = -EINVAL; |
| 1263 | if (kvm_sigmask.len != sizeof sigset) |
| 1264 | goto out; |
| 1265 | r = -EFAULT; |
| 1266 | if (copy_from_user(&sigset, sigmask_arg->sigset, |
| 1267 | sizeof sigset)) |
| 1268 | goto out; |
| 1269 | p = &sigset; |
| 1270 | } |
| 1271 | r = kvm_vcpu_ioctl_set_sigmask(vcpu, &sigset); |
| 1272 | break; |
| 1273 | } |
Avi Kivity | b883673 | 2007-04-01 16:34:31 +0300 | [diff] [blame] | 1274 | case KVM_GET_FPU: { |
| 1275 | struct kvm_fpu fpu; |
| 1276 | |
| 1277 | memset(&fpu, 0, sizeof fpu); |
Hollis Blanchard | d075206 | 2007-10-31 17:24:25 -0500 | [diff] [blame] | 1278 | r = kvm_arch_vcpu_ioctl_get_fpu(vcpu, &fpu); |
Avi Kivity | b883673 | 2007-04-01 16:34:31 +0300 | [diff] [blame] | 1279 | if (r) |
| 1280 | goto out; |
| 1281 | r = -EFAULT; |
| 1282 | if (copy_to_user(argp, &fpu, sizeof fpu)) |
| 1283 | goto out; |
| 1284 | r = 0; |
| 1285 | break; |
| 1286 | } |
| 1287 | case KVM_SET_FPU: { |
| 1288 | struct kvm_fpu fpu; |
| 1289 | |
| 1290 | r = -EFAULT; |
| 1291 | if (copy_from_user(&fpu, argp, sizeof fpu)) |
| 1292 | goto out; |
Hollis Blanchard | d075206 | 2007-10-31 17:24:25 -0500 | [diff] [blame] | 1293 | r = kvm_arch_vcpu_ioctl_set_fpu(vcpu, &fpu); |
Avi Kivity | b883673 | 2007-04-01 16:34:31 +0300 | [diff] [blame] | 1294 | if (r) |
| 1295 | goto out; |
| 1296 | r = 0; |
| 1297 | break; |
| 1298 | } |
Avi Kivity | bccf215 | 2007-02-21 18:04:26 +0200 | [diff] [blame] | 1299 | default: |
Carsten Otte | 313a3dc | 2007-10-11 19:16:52 +0200 | [diff] [blame] | 1300 | r = kvm_arch_vcpu_ioctl(filp, ioctl, arg); |
Avi Kivity | bccf215 | 2007-02-21 18:04:26 +0200 | [diff] [blame] | 1301 | } |
| 1302 | out: |
| 1303 | return r; |
| 1304 | } |
| 1305 | |
| 1306 | static long kvm_vm_ioctl(struct file *filp, |
| 1307 | unsigned int ioctl, unsigned long arg) |
| 1308 | { |
| 1309 | struct kvm *kvm = filp->private_data; |
| 1310 | void __user *argp = (void __user *)arg; |
Carsten Otte | 1fe779f | 2007-10-29 16:08:35 +0100 | [diff] [blame] | 1311 | int r; |
Avi Kivity | bccf215 | 2007-02-21 18:04:26 +0200 | [diff] [blame] | 1312 | |
Avi Kivity | 6d4e4c4 | 2007-11-21 16:41:05 +0200 | [diff] [blame] | 1313 | if (kvm->mm != current->mm) |
| 1314 | return -EIO; |
Avi Kivity | bccf215 | 2007-02-21 18:04:26 +0200 | [diff] [blame] | 1315 | switch (ioctl) { |
| 1316 | case KVM_CREATE_VCPU: |
| 1317 | r = kvm_vm_ioctl_create_vcpu(kvm, arg); |
| 1318 | if (r < 0) |
| 1319 | goto out; |
| 1320 | break; |
Izik Eidus | 6fc138d | 2007-10-09 19:20:39 +0200 | [diff] [blame] | 1321 | case KVM_SET_USER_MEMORY_REGION: { |
| 1322 | struct kvm_userspace_memory_region kvm_userspace_mem; |
| 1323 | |
| 1324 | r = -EFAULT; |
| 1325 | if (copy_from_user(&kvm_userspace_mem, argp, |
| 1326 | sizeof kvm_userspace_mem)) |
| 1327 | goto out; |
| 1328 | |
| 1329 | r = kvm_vm_ioctl_set_memory_region(kvm, &kvm_userspace_mem, 1); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1330 | if (r) |
| 1331 | goto out; |
| 1332 | break; |
| 1333 | } |
| 1334 | case KVM_GET_DIRTY_LOG: { |
| 1335 | struct kvm_dirty_log log; |
| 1336 | |
| 1337 | r = -EFAULT; |
Al Viro | 2f366987 | 2007-02-09 16:38:35 +0000 | [diff] [blame] | 1338 | if (copy_from_user(&log, argp, sizeof log)) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1339 | goto out; |
Avi Kivity | 2c6f5df | 2007-02-20 18:27:58 +0200 | [diff] [blame] | 1340 | r = kvm_vm_ioctl_get_dirty_log(kvm, &log); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1341 | if (r) |
| 1342 | goto out; |
| 1343 | break; |
| 1344 | } |
Laurent Vivier | 5f94c17 | 2008-05-30 16:05:54 +0200 | [diff] [blame] | 1345 | #ifdef KVM_COALESCED_MMIO_PAGE_OFFSET |
| 1346 | case KVM_REGISTER_COALESCED_MMIO: { |
| 1347 | struct kvm_coalesced_mmio_zone zone; |
| 1348 | r = -EFAULT; |
| 1349 | if (copy_from_user(&zone, argp, sizeof zone)) |
| 1350 | goto out; |
| 1351 | r = -ENXIO; |
| 1352 | r = kvm_vm_ioctl_register_coalesced_mmio(kvm, &zone); |
| 1353 | if (r) |
| 1354 | goto out; |
| 1355 | r = 0; |
| 1356 | break; |
| 1357 | } |
| 1358 | case KVM_UNREGISTER_COALESCED_MMIO: { |
| 1359 | struct kvm_coalesced_mmio_zone zone; |
| 1360 | r = -EFAULT; |
| 1361 | if (copy_from_user(&zone, argp, sizeof zone)) |
| 1362 | goto out; |
| 1363 | r = -ENXIO; |
| 1364 | r = kvm_vm_ioctl_unregister_coalesced_mmio(kvm, &zone); |
| 1365 | if (r) |
| 1366 | goto out; |
| 1367 | r = 0; |
| 1368 | break; |
| 1369 | } |
| 1370 | #endif |
Avi Kivity | f17abe9 | 2007-02-21 19:28:04 +0200 | [diff] [blame] | 1371 | default: |
Carsten Otte | 1fe779f | 2007-10-29 16:08:35 +0100 | [diff] [blame] | 1372 | r = kvm_arch_vm_ioctl(filp, ioctl, arg); |
Avi Kivity | f17abe9 | 2007-02-21 19:28:04 +0200 | [diff] [blame] | 1373 | } |
| 1374 | out: |
| 1375 | return r; |
| 1376 | } |
| 1377 | |
npiggin@suse.de | e4a533a | 2007-12-05 18:15:52 +1100 | [diff] [blame] | 1378 | static int kvm_vm_fault(struct vm_area_struct *vma, struct vm_fault *vmf) |
Avi Kivity | f17abe9 | 2007-02-21 19:28:04 +0200 | [diff] [blame] | 1379 | { |
| 1380 | struct kvm *kvm = vma->vm_file->private_data; |
Avi Kivity | f17abe9 | 2007-02-21 19:28:04 +0200 | [diff] [blame] | 1381 | struct page *page; |
| 1382 | |
npiggin@suse.de | e4a533a | 2007-12-05 18:15:52 +1100 | [diff] [blame] | 1383 | if (!kvm_is_visible_gfn(kvm, vmf->pgoff)) |
| 1384 | return VM_FAULT_SIGBUS; |
Marcelo Tosatti | 10589a4 | 2007-12-20 19:18:22 -0500 | [diff] [blame] | 1385 | page = gfn_to_page(kvm, vmf->pgoff); |
Izik Eidus | 8a7ae05 | 2007-10-18 11:09:33 +0200 | [diff] [blame] | 1386 | if (is_error_page(page)) { |
Izik Eidus | b4231d6 | 2007-11-20 11:49:33 +0200 | [diff] [blame] | 1387 | kvm_release_page_clean(page); |
npiggin@suse.de | e4a533a | 2007-12-05 18:15:52 +1100 | [diff] [blame] | 1388 | return VM_FAULT_SIGBUS; |
Izik Eidus | 8a7ae05 | 2007-10-18 11:09:33 +0200 | [diff] [blame] | 1389 | } |
npiggin@suse.de | e4a533a | 2007-12-05 18:15:52 +1100 | [diff] [blame] | 1390 | vmf->page = page; |
| 1391 | return 0; |
Avi Kivity | f17abe9 | 2007-02-21 19:28:04 +0200 | [diff] [blame] | 1392 | } |
| 1393 | |
| 1394 | static struct vm_operations_struct kvm_vm_vm_ops = { |
npiggin@suse.de | e4a533a | 2007-12-05 18:15:52 +1100 | [diff] [blame] | 1395 | .fault = kvm_vm_fault, |
Avi Kivity | f17abe9 | 2007-02-21 19:28:04 +0200 | [diff] [blame] | 1396 | }; |
| 1397 | |
| 1398 | static int kvm_vm_mmap(struct file *file, struct vm_area_struct *vma) |
| 1399 | { |
| 1400 | vma->vm_ops = &kvm_vm_vm_ops; |
| 1401 | return 0; |
| 1402 | } |
| 1403 | |
Jan Engelhardt | 5c50274 | 2008-01-22 20:46:14 +0100 | [diff] [blame] | 1404 | static const struct file_operations kvm_vm_fops = { |
Avi Kivity | f17abe9 | 2007-02-21 19:28:04 +0200 | [diff] [blame] | 1405 | .release = kvm_vm_release, |
| 1406 | .unlocked_ioctl = kvm_vm_ioctl, |
| 1407 | .compat_ioctl = kvm_vm_ioctl, |
| 1408 | .mmap = kvm_vm_mmap, |
| 1409 | }; |
| 1410 | |
| 1411 | static int kvm_dev_ioctl_create_vm(void) |
| 1412 | { |
Al Viro | 2030a42 | 2008-02-23 06:46:49 -0500 | [diff] [blame] | 1413 | int fd; |
Avi Kivity | f17abe9 | 2007-02-21 19:28:04 +0200 | [diff] [blame] | 1414 | struct kvm *kvm; |
| 1415 | |
Avi Kivity | f17abe9 | 2007-02-21 19:28:04 +0200 | [diff] [blame] | 1416 | kvm = kvm_create_vm(); |
Avi Kivity | d6d2816 | 2007-06-28 08:38:16 -0400 | [diff] [blame] | 1417 | if (IS_ERR(kvm)) |
| 1418 | return PTR_ERR(kvm); |
Ulrich Drepper | 7d9dbca | 2008-07-23 21:29:22 -0700 | [diff] [blame] | 1419 | fd = anon_inode_getfd("kvm-vm", &kvm_vm_fops, kvm, 0); |
Al Viro | 2030a42 | 2008-02-23 06:46:49 -0500 | [diff] [blame] | 1420 | if (fd < 0) |
Al Viro | 66c0b39 | 2008-04-19 20:33:56 +0100 | [diff] [blame] | 1421 | kvm_put_kvm(kvm); |
Avi Kivity | f17abe9 | 2007-02-21 19:28:04 +0200 | [diff] [blame] | 1422 | |
Avi Kivity | f17abe9 | 2007-02-21 19:28:04 +0200 | [diff] [blame] | 1423 | return fd; |
Avi Kivity | f17abe9 | 2007-02-21 19:28:04 +0200 | [diff] [blame] | 1424 | } |
| 1425 | |
| 1426 | static long kvm_dev_ioctl(struct file *filp, |
| 1427 | unsigned int ioctl, unsigned long arg) |
| 1428 | { |
Avi Kivity | 07c45a3 | 2007-03-07 13:05:38 +0200 | [diff] [blame] | 1429 | long r = -EINVAL; |
Avi Kivity | f17abe9 | 2007-02-21 19:28:04 +0200 | [diff] [blame] | 1430 | |
| 1431 | switch (ioctl) { |
| 1432 | case KVM_GET_API_VERSION: |
Avi Kivity | f0fe510 | 2007-03-07 13:11:17 +0200 | [diff] [blame] | 1433 | r = -EINVAL; |
| 1434 | if (arg) |
| 1435 | goto out; |
Avi Kivity | f17abe9 | 2007-02-21 19:28:04 +0200 | [diff] [blame] | 1436 | r = KVM_API_VERSION; |
| 1437 | break; |
| 1438 | case KVM_CREATE_VM: |
Avi Kivity | f0fe510 | 2007-03-07 13:11:17 +0200 | [diff] [blame] | 1439 | r = -EINVAL; |
| 1440 | if (arg) |
| 1441 | goto out; |
Avi Kivity | f17abe9 | 2007-02-21 19:28:04 +0200 | [diff] [blame] | 1442 | r = kvm_dev_ioctl_create_vm(); |
| 1443 | break; |
Zhang Xiantao | 018d00d | 2007-11-15 23:07:47 +0800 | [diff] [blame] | 1444 | case KVM_CHECK_EXTENSION: |
Christian Borntraeger | 1e1c65e | 2008-04-21 13:48:24 +0200 | [diff] [blame] | 1445 | r = kvm_dev_ioctl_check_extension(arg); |
Avi Kivity | 5d308f4 | 2007-03-01 17:56:20 +0200 | [diff] [blame] | 1446 | break; |
Avi Kivity | 07c45a3 | 2007-03-07 13:05:38 +0200 | [diff] [blame] | 1447 | case KVM_GET_VCPU_MMAP_SIZE: |
| 1448 | r = -EINVAL; |
| 1449 | if (arg) |
| 1450 | goto out; |
Avi Kivity | adb1ff4 | 2008-01-24 15:13:08 +0200 | [diff] [blame] | 1451 | r = PAGE_SIZE; /* struct kvm_run */ |
| 1452 | #ifdef CONFIG_X86 |
| 1453 | r += PAGE_SIZE; /* pio data page */ |
| 1454 | #endif |
Laurent Vivier | 5f94c17 | 2008-05-30 16:05:54 +0200 | [diff] [blame] | 1455 | #ifdef KVM_COALESCED_MMIO_PAGE_OFFSET |
| 1456 | r += PAGE_SIZE; /* coalesced mmio ring page */ |
| 1457 | #endif |
Avi Kivity | 07c45a3 | 2007-03-07 13:05:38 +0200 | [diff] [blame] | 1458 | break; |
Feng(Eric) Liu | d4c9ff2 | 2008-04-10 08:47:53 -0400 | [diff] [blame] | 1459 | case KVM_TRACE_ENABLE: |
| 1460 | case KVM_TRACE_PAUSE: |
| 1461 | case KVM_TRACE_DISABLE: |
| 1462 | r = kvm_trace_ioctl(ioctl, arg); |
| 1463 | break; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1464 | default: |
Carsten Otte | 043405e | 2007-10-10 17:16:19 +0200 | [diff] [blame] | 1465 | return kvm_arch_dev_ioctl(filp, ioctl, arg); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1466 | } |
| 1467 | out: |
| 1468 | return r; |
| 1469 | } |
| 1470 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1471 | static struct file_operations kvm_chardev_ops = { |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1472 | .unlocked_ioctl = kvm_dev_ioctl, |
| 1473 | .compat_ioctl = kvm_dev_ioctl, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1474 | }; |
| 1475 | |
| 1476 | static struct miscdevice kvm_dev = { |
Avi Kivity | bbe4432 | 2007-03-04 13:27:36 +0200 | [diff] [blame] | 1477 | KVM_MINOR, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1478 | "kvm", |
| 1479 | &kvm_chardev_ops, |
| 1480 | }; |
| 1481 | |
Avi Kivity | 1b6c016 | 2007-05-24 13:03:52 +0300 | [diff] [blame] | 1482 | static void hardware_enable(void *junk) |
| 1483 | { |
| 1484 | int cpu = raw_smp_processor_id(); |
| 1485 | |
| 1486 | if (cpu_isset(cpu, cpus_hardware_enabled)) |
| 1487 | return; |
| 1488 | cpu_set(cpu, cpus_hardware_enabled); |
Zhang Xiantao | e9b11c1 | 2007-11-14 20:38:21 +0800 | [diff] [blame] | 1489 | kvm_arch_hardware_enable(NULL); |
Avi Kivity | 1b6c016 | 2007-05-24 13:03:52 +0300 | [diff] [blame] | 1490 | } |
| 1491 | |
| 1492 | static void hardware_disable(void *junk) |
| 1493 | { |
| 1494 | int cpu = raw_smp_processor_id(); |
| 1495 | |
| 1496 | if (!cpu_isset(cpu, cpus_hardware_enabled)) |
| 1497 | return; |
| 1498 | cpu_clear(cpu, cpus_hardware_enabled); |
Zhang Xiantao | e9b11c1 | 2007-11-14 20:38:21 +0800 | [diff] [blame] | 1499 | kvm_arch_hardware_disable(NULL); |
Avi Kivity | 1b6c016 | 2007-05-24 13:03:52 +0300 | [diff] [blame] | 1500 | } |
| 1501 | |
Avi Kivity | 774c47f | 2007-02-12 00:54:47 -0800 | [diff] [blame] | 1502 | static int kvm_cpu_hotplug(struct notifier_block *notifier, unsigned long val, |
| 1503 | void *v) |
| 1504 | { |
| 1505 | int cpu = (long)v; |
| 1506 | |
Avi Kivity | 1a6f4d7 | 2007-11-11 18:37:32 +0200 | [diff] [blame] | 1507 | val &= ~CPU_TASKS_FROZEN; |
Avi Kivity | 774c47f | 2007-02-12 00:54:47 -0800 | [diff] [blame] | 1508 | switch (val) { |
Avi Kivity | cec9ad2 | 2007-05-24 13:11:41 +0300 | [diff] [blame] | 1509 | case CPU_DYING: |
Avi Kivity | 6ec8a85 | 2007-08-19 15:57:26 +0300 | [diff] [blame] | 1510 | printk(KERN_INFO "kvm: disabling virtualization on CPU%d\n", |
| 1511 | cpu); |
| 1512 | hardware_disable(NULL); |
| 1513 | break; |
Avi Kivity | 774c47f | 2007-02-12 00:54:47 -0800 | [diff] [blame] | 1514 | case CPU_UP_CANCELED: |
Jeremy Katz | 43934a3 | 2007-02-19 14:37:46 +0200 | [diff] [blame] | 1515 | printk(KERN_INFO "kvm: disabling virtualization on CPU%d\n", |
| 1516 | cpu); |
Jens Axboe | 8691e5a | 2008-06-06 11:18:06 +0200 | [diff] [blame] | 1517 | smp_call_function_single(cpu, hardware_disable, NULL, 1); |
Avi Kivity | 774c47f | 2007-02-12 00:54:47 -0800 | [diff] [blame] | 1518 | break; |
Jeremy Katz | 43934a3 | 2007-02-19 14:37:46 +0200 | [diff] [blame] | 1519 | case CPU_ONLINE: |
| 1520 | printk(KERN_INFO "kvm: enabling virtualization on CPU%d\n", |
| 1521 | cpu); |
Jens Axboe | 8691e5a | 2008-06-06 11:18:06 +0200 | [diff] [blame] | 1522 | smp_call_function_single(cpu, hardware_enable, NULL, 1); |
Avi Kivity | 774c47f | 2007-02-12 00:54:47 -0800 | [diff] [blame] | 1523 | break; |
| 1524 | } |
| 1525 | return NOTIFY_OK; |
| 1526 | } |
| 1527 | |
Avi Kivity | 4ecac3f | 2008-05-13 13:23:38 +0300 | [diff] [blame] | 1528 | |
| 1529 | asmlinkage void kvm_handle_fault_on_reboot(void) |
| 1530 | { |
| 1531 | if (kvm_rebooting) |
| 1532 | /* spin while reset goes on */ |
| 1533 | while (true) |
| 1534 | ; |
| 1535 | /* Fault while not rebooting. We want the trace. */ |
| 1536 | BUG(); |
| 1537 | } |
| 1538 | EXPORT_SYMBOL_GPL(kvm_handle_fault_on_reboot); |
| 1539 | |
Rusty Russell | 9a2b85c | 2007-07-17 23:17:55 +1000 | [diff] [blame] | 1540 | static int kvm_reboot(struct notifier_block *notifier, unsigned long val, |
Mike Day | d77c26f | 2007-10-08 09:02:08 -0400 | [diff] [blame] | 1541 | void *v) |
Rusty Russell | 9a2b85c | 2007-07-17 23:17:55 +1000 | [diff] [blame] | 1542 | { |
| 1543 | if (val == SYS_RESTART) { |
| 1544 | /* |
| 1545 | * Some (well, at least mine) BIOSes hang on reboot if |
| 1546 | * in vmx root mode. |
| 1547 | */ |
| 1548 | printk(KERN_INFO "kvm: exiting hardware virtualization\n"); |
Avi Kivity | 4ecac3f | 2008-05-13 13:23:38 +0300 | [diff] [blame] | 1549 | kvm_rebooting = true; |
Jens Axboe | 15c8b6c | 2008-05-09 09:39:44 +0200 | [diff] [blame] | 1550 | on_each_cpu(hardware_disable, NULL, 1); |
Rusty Russell | 9a2b85c | 2007-07-17 23:17:55 +1000 | [diff] [blame] | 1551 | } |
| 1552 | return NOTIFY_OK; |
| 1553 | } |
| 1554 | |
| 1555 | static struct notifier_block kvm_reboot_notifier = { |
| 1556 | .notifier_call = kvm_reboot, |
| 1557 | .priority = 0, |
| 1558 | }; |
| 1559 | |
Gregory Haskins | 2eeb2e9 | 2007-05-31 14:08:53 -0400 | [diff] [blame] | 1560 | void kvm_io_bus_init(struct kvm_io_bus *bus) |
| 1561 | { |
| 1562 | memset(bus, 0, sizeof(*bus)); |
| 1563 | } |
| 1564 | |
| 1565 | void kvm_io_bus_destroy(struct kvm_io_bus *bus) |
| 1566 | { |
| 1567 | int i; |
| 1568 | |
| 1569 | for (i = 0; i < bus->dev_count; i++) { |
| 1570 | struct kvm_io_device *pos = bus->devs[i]; |
| 1571 | |
| 1572 | kvm_iodevice_destructor(pos); |
| 1573 | } |
| 1574 | } |
| 1575 | |
Laurent Vivier | 9276049 | 2008-05-30 16:05:53 +0200 | [diff] [blame] | 1576 | struct kvm_io_device *kvm_io_bus_find_dev(struct kvm_io_bus *bus, |
| 1577 | gpa_t addr, int len, int is_write) |
Gregory Haskins | 2eeb2e9 | 2007-05-31 14:08:53 -0400 | [diff] [blame] | 1578 | { |
| 1579 | int i; |
| 1580 | |
| 1581 | for (i = 0; i < bus->dev_count; i++) { |
| 1582 | struct kvm_io_device *pos = bus->devs[i]; |
| 1583 | |
Laurent Vivier | 9276049 | 2008-05-30 16:05:53 +0200 | [diff] [blame] | 1584 | if (pos->in_range(pos, addr, len, is_write)) |
Gregory Haskins | 2eeb2e9 | 2007-05-31 14:08:53 -0400 | [diff] [blame] | 1585 | return pos; |
| 1586 | } |
| 1587 | |
| 1588 | return NULL; |
| 1589 | } |
| 1590 | |
| 1591 | void kvm_io_bus_register_dev(struct kvm_io_bus *bus, struct kvm_io_device *dev) |
| 1592 | { |
| 1593 | BUG_ON(bus->dev_count > (NR_IOBUS_DEVS-1)); |
| 1594 | |
| 1595 | bus->devs[bus->dev_count++] = dev; |
| 1596 | } |
| 1597 | |
Avi Kivity | 774c47f | 2007-02-12 00:54:47 -0800 | [diff] [blame] | 1598 | static struct notifier_block kvm_cpu_notifier = { |
| 1599 | .notifier_call = kvm_cpu_hotplug, |
| 1600 | .priority = 20, /* must be > scheduler priority */ |
| 1601 | }; |
| 1602 | |
Christoph Hellwig | 8b88b09 | 2008-02-08 04:20:26 -0800 | [diff] [blame] | 1603 | static int vm_stat_get(void *_offset, u64 *val) |
Avi Kivity | ba1389b | 2007-11-18 16:24:12 +0200 | [diff] [blame] | 1604 | { |
| 1605 | unsigned offset = (long)_offset; |
Avi Kivity | ba1389b | 2007-11-18 16:24:12 +0200 | [diff] [blame] | 1606 | struct kvm *kvm; |
| 1607 | |
Christoph Hellwig | 8b88b09 | 2008-02-08 04:20:26 -0800 | [diff] [blame] | 1608 | *val = 0; |
Avi Kivity | ba1389b | 2007-11-18 16:24:12 +0200 | [diff] [blame] | 1609 | spin_lock(&kvm_lock); |
| 1610 | list_for_each_entry(kvm, &vm_list, vm_list) |
Christoph Hellwig | 8b88b09 | 2008-02-08 04:20:26 -0800 | [diff] [blame] | 1611 | *val += *(u32 *)((void *)kvm + offset); |
Avi Kivity | ba1389b | 2007-11-18 16:24:12 +0200 | [diff] [blame] | 1612 | spin_unlock(&kvm_lock); |
Christoph Hellwig | 8b88b09 | 2008-02-08 04:20:26 -0800 | [diff] [blame] | 1613 | return 0; |
Avi Kivity | ba1389b | 2007-11-18 16:24:12 +0200 | [diff] [blame] | 1614 | } |
| 1615 | |
| 1616 | DEFINE_SIMPLE_ATTRIBUTE(vm_stat_fops, vm_stat_get, NULL, "%llu\n"); |
| 1617 | |
Christoph Hellwig | 8b88b09 | 2008-02-08 04:20:26 -0800 | [diff] [blame] | 1618 | static int vcpu_stat_get(void *_offset, u64 *val) |
Avi Kivity | 1165f5f | 2007-04-19 17:27:43 +0300 | [diff] [blame] | 1619 | { |
| 1620 | unsigned offset = (long)_offset; |
Avi Kivity | 1165f5f | 2007-04-19 17:27:43 +0300 | [diff] [blame] | 1621 | struct kvm *kvm; |
| 1622 | struct kvm_vcpu *vcpu; |
| 1623 | int i; |
| 1624 | |
Christoph Hellwig | 8b88b09 | 2008-02-08 04:20:26 -0800 | [diff] [blame] | 1625 | *val = 0; |
Avi Kivity | 1165f5f | 2007-04-19 17:27:43 +0300 | [diff] [blame] | 1626 | spin_lock(&kvm_lock); |
| 1627 | list_for_each_entry(kvm, &vm_list, vm_list) |
| 1628 | for (i = 0; i < KVM_MAX_VCPUS; ++i) { |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 1629 | vcpu = kvm->vcpus[i]; |
| 1630 | if (vcpu) |
Christoph Hellwig | 8b88b09 | 2008-02-08 04:20:26 -0800 | [diff] [blame] | 1631 | *val += *(u32 *)((void *)vcpu + offset); |
Avi Kivity | 1165f5f | 2007-04-19 17:27:43 +0300 | [diff] [blame] | 1632 | } |
| 1633 | spin_unlock(&kvm_lock); |
Christoph Hellwig | 8b88b09 | 2008-02-08 04:20:26 -0800 | [diff] [blame] | 1634 | return 0; |
Avi Kivity | 1165f5f | 2007-04-19 17:27:43 +0300 | [diff] [blame] | 1635 | } |
| 1636 | |
Avi Kivity | ba1389b | 2007-11-18 16:24:12 +0200 | [diff] [blame] | 1637 | DEFINE_SIMPLE_ATTRIBUTE(vcpu_stat_fops, vcpu_stat_get, NULL, "%llu\n"); |
| 1638 | |
| 1639 | static struct file_operations *stat_fops[] = { |
| 1640 | [KVM_STAT_VCPU] = &vcpu_stat_fops, |
| 1641 | [KVM_STAT_VM] = &vm_stat_fops, |
| 1642 | }; |
Avi Kivity | 1165f5f | 2007-04-19 17:27:43 +0300 | [diff] [blame] | 1643 | |
Zhang Xiantao | a16b043 | 2007-11-16 14:38:21 +0800 | [diff] [blame] | 1644 | static void kvm_init_debug(void) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1645 | { |
| 1646 | struct kvm_stats_debugfs_item *p; |
| 1647 | |
Hollis Blanchard | 76f7c87 | 2008-04-15 16:05:42 -0500 | [diff] [blame] | 1648 | kvm_debugfs_dir = debugfs_create_dir("kvm", NULL); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1649 | for (p = debugfs_entries; p->name; ++p) |
Hollis Blanchard | 76f7c87 | 2008-04-15 16:05:42 -0500 | [diff] [blame] | 1650 | p->dentry = debugfs_create_file(p->name, 0444, kvm_debugfs_dir, |
Avi Kivity | 1165f5f | 2007-04-19 17:27:43 +0300 | [diff] [blame] | 1651 | (void *)(long)p->offset, |
Avi Kivity | ba1389b | 2007-11-18 16:24:12 +0200 | [diff] [blame] | 1652 | stat_fops[p->kind]); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1653 | } |
| 1654 | |
| 1655 | static void kvm_exit_debug(void) |
| 1656 | { |
| 1657 | struct kvm_stats_debugfs_item *p; |
| 1658 | |
| 1659 | for (p = debugfs_entries; p->name; ++p) |
| 1660 | debugfs_remove(p->dentry); |
Hollis Blanchard | 76f7c87 | 2008-04-15 16:05:42 -0500 | [diff] [blame] | 1661 | debugfs_remove(kvm_debugfs_dir); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1662 | } |
| 1663 | |
Avi Kivity | 59ae6c6 | 2007-02-12 00:54:48 -0800 | [diff] [blame] | 1664 | static int kvm_suspend(struct sys_device *dev, pm_message_t state) |
| 1665 | { |
Avi Kivity | 4267c41 | 2007-05-24 13:09:41 +0300 | [diff] [blame] | 1666 | hardware_disable(NULL); |
Avi Kivity | 59ae6c6 | 2007-02-12 00:54:48 -0800 | [diff] [blame] | 1667 | return 0; |
| 1668 | } |
| 1669 | |
| 1670 | static int kvm_resume(struct sys_device *dev) |
| 1671 | { |
Avi Kivity | 4267c41 | 2007-05-24 13:09:41 +0300 | [diff] [blame] | 1672 | hardware_enable(NULL); |
Avi Kivity | 59ae6c6 | 2007-02-12 00:54:48 -0800 | [diff] [blame] | 1673 | return 0; |
| 1674 | } |
| 1675 | |
| 1676 | static struct sysdev_class kvm_sysdev_class = { |
Kay Sievers | af5ca3f | 2007-12-20 02:09:39 +0100 | [diff] [blame] | 1677 | .name = "kvm", |
Avi Kivity | 59ae6c6 | 2007-02-12 00:54:48 -0800 | [diff] [blame] | 1678 | .suspend = kvm_suspend, |
| 1679 | .resume = kvm_resume, |
| 1680 | }; |
| 1681 | |
| 1682 | static struct sys_device kvm_sysdev = { |
| 1683 | .id = 0, |
| 1684 | .cls = &kvm_sysdev_class, |
| 1685 | }; |
| 1686 | |
Izik Eidus | cea7bb2 | 2007-10-17 19:17:48 +0200 | [diff] [blame] | 1687 | struct page *bad_page; |
Anthony Liguori | 35149e2 | 2008-04-02 14:46:56 -0500 | [diff] [blame] | 1688 | pfn_t bad_pfn; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1689 | |
Avi Kivity | 15ad714 | 2007-07-11 18:17:21 +0300 | [diff] [blame] | 1690 | static inline |
| 1691 | struct kvm_vcpu *preempt_notifier_to_vcpu(struct preempt_notifier *pn) |
| 1692 | { |
| 1693 | return container_of(pn, struct kvm_vcpu, preempt_notifier); |
| 1694 | } |
| 1695 | |
| 1696 | static void kvm_sched_in(struct preempt_notifier *pn, int cpu) |
| 1697 | { |
| 1698 | struct kvm_vcpu *vcpu = preempt_notifier_to_vcpu(pn); |
| 1699 | |
Zhang Xiantao | e9b11c1 | 2007-11-14 20:38:21 +0800 | [diff] [blame] | 1700 | kvm_arch_vcpu_load(vcpu, cpu); |
Avi Kivity | 15ad714 | 2007-07-11 18:17:21 +0300 | [diff] [blame] | 1701 | } |
| 1702 | |
| 1703 | static void kvm_sched_out(struct preempt_notifier *pn, |
| 1704 | struct task_struct *next) |
| 1705 | { |
| 1706 | struct kvm_vcpu *vcpu = preempt_notifier_to_vcpu(pn); |
| 1707 | |
Zhang Xiantao | e9b11c1 | 2007-11-14 20:38:21 +0800 | [diff] [blame] | 1708 | kvm_arch_vcpu_put(vcpu); |
Avi Kivity | 15ad714 | 2007-07-11 18:17:21 +0300 | [diff] [blame] | 1709 | } |
| 1710 | |
Zhang Xiantao | f8c16bb | 2007-11-14 20:40:21 +0800 | [diff] [blame] | 1711 | int kvm_init(void *opaque, unsigned int vcpu_size, |
Rusty Russell | c16f862 | 2007-07-30 21:12:19 +1000 | [diff] [blame] | 1712 | struct module *module) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1713 | { |
| 1714 | int r; |
Yang, Sheng | 002c7f7 | 2007-07-31 14:23:01 +0300 | [diff] [blame] | 1715 | int cpu; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1716 | |
Zhang Xiantao | cb498ea | 2007-11-14 20:39:31 +0800 | [diff] [blame] | 1717 | kvm_init_debug(); |
| 1718 | |
Zhang Xiantao | f8c16bb | 2007-11-14 20:40:21 +0800 | [diff] [blame] | 1719 | r = kvm_arch_init(opaque); |
| 1720 | if (r) |
Zhang Xiantao | d2308784 | 2007-11-29 15:35:39 +0800 | [diff] [blame] | 1721 | goto out_fail; |
Zhang Xiantao | cb498ea | 2007-11-14 20:39:31 +0800 | [diff] [blame] | 1722 | |
| 1723 | bad_page = alloc_page(GFP_KERNEL | __GFP_ZERO); |
| 1724 | |
| 1725 | if (bad_page == NULL) { |
| 1726 | r = -ENOMEM; |
| 1727 | goto out; |
| 1728 | } |
| 1729 | |
Anthony Liguori | 35149e2 | 2008-04-02 14:46:56 -0500 | [diff] [blame] | 1730 | bad_pfn = page_to_pfn(bad_page); |
| 1731 | |
Zhang Xiantao | e9b11c1 | 2007-11-14 20:38:21 +0800 | [diff] [blame] | 1732 | r = kvm_arch_hardware_setup(); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1733 | if (r < 0) |
Zhang Xiantao | d2308784 | 2007-11-29 15:35:39 +0800 | [diff] [blame] | 1734 | goto out_free_0; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1735 | |
Yang, Sheng | 002c7f7 | 2007-07-31 14:23:01 +0300 | [diff] [blame] | 1736 | for_each_online_cpu(cpu) { |
| 1737 | smp_call_function_single(cpu, |
Zhang Xiantao | e9b11c1 | 2007-11-14 20:38:21 +0800 | [diff] [blame] | 1738 | kvm_arch_check_processor_compat, |
Jens Axboe | 8691e5a | 2008-06-06 11:18:06 +0200 | [diff] [blame] | 1739 | &r, 1); |
Yang, Sheng | 002c7f7 | 2007-07-31 14:23:01 +0300 | [diff] [blame] | 1740 | if (r < 0) |
Zhang Xiantao | d2308784 | 2007-11-29 15:35:39 +0800 | [diff] [blame] | 1741 | goto out_free_1; |
Yang, Sheng | 002c7f7 | 2007-07-31 14:23:01 +0300 | [diff] [blame] | 1742 | } |
| 1743 | |
Jens Axboe | 15c8b6c | 2008-05-09 09:39:44 +0200 | [diff] [blame] | 1744 | on_each_cpu(hardware_enable, NULL, 1); |
Avi Kivity | 774c47f | 2007-02-12 00:54:47 -0800 | [diff] [blame] | 1745 | r = register_cpu_notifier(&kvm_cpu_notifier); |
| 1746 | if (r) |
Zhang Xiantao | d2308784 | 2007-11-29 15:35:39 +0800 | [diff] [blame] | 1747 | goto out_free_2; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1748 | register_reboot_notifier(&kvm_reboot_notifier); |
| 1749 | |
Avi Kivity | 59ae6c6 | 2007-02-12 00:54:48 -0800 | [diff] [blame] | 1750 | r = sysdev_class_register(&kvm_sysdev_class); |
| 1751 | if (r) |
Zhang Xiantao | d2308784 | 2007-11-29 15:35:39 +0800 | [diff] [blame] | 1752 | goto out_free_3; |
Avi Kivity | 59ae6c6 | 2007-02-12 00:54:48 -0800 | [diff] [blame] | 1753 | |
| 1754 | r = sysdev_register(&kvm_sysdev); |
| 1755 | if (r) |
Zhang Xiantao | d2308784 | 2007-11-29 15:35:39 +0800 | [diff] [blame] | 1756 | goto out_free_4; |
Avi Kivity | 59ae6c6 | 2007-02-12 00:54:48 -0800 | [diff] [blame] | 1757 | |
Rusty Russell | c16f862 | 2007-07-30 21:12:19 +1000 | [diff] [blame] | 1758 | /* A kmem cache lets us meet the alignment requirements of fx_save. */ |
| 1759 | kvm_vcpu_cache = kmem_cache_create("kvm_vcpu", vcpu_size, |
Joe Perches | 56919c5 | 2007-11-12 20:06:51 -0800 | [diff] [blame] | 1760 | __alignof__(struct kvm_vcpu), |
| 1761 | 0, NULL); |
Rusty Russell | c16f862 | 2007-07-30 21:12:19 +1000 | [diff] [blame] | 1762 | if (!kvm_vcpu_cache) { |
| 1763 | r = -ENOMEM; |
Zhang Xiantao | d2308784 | 2007-11-29 15:35:39 +0800 | [diff] [blame] | 1764 | goto out_free_5; |
Rusty Russell | c16f862 | 2007-07-30 21:12:19 +1000 | [diff] [blame] | 1765 | } |
| 1766 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1767 | kvm_chardev_ops.owner = module; |
| 1768 | |
| 1769 | r = misc_register(&kvm_dev); |
| 1770 | if (r) { |
Mike Day | d77c26f | 2007-10-08 09:02:08 -0400 | [diff] [blame] | 1771 | printk(KERN_ERR "kvm: misc device register failed\n"); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1772 | goto out_free; |
| 1773 | } |
| 1774 | |
Avi Kivity | 15ad714 | 2007-07-11 18:17:21 +0300 | [diff] [blame] | 1775 | kvm_preempt_ops.sched_in = kvm_sched_in; |
| 1776 | kvm_preempt_ops.sched_out = kvm_sched_out; |
| 1777 | |
Avi Kivity | c7addb9 | 2007-09-16 18:58:32 +0200 | [diff] [blame] | 1778 | return 0; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1779 | |
| 1780 | out_free: |
Rusty Russell | c16f862 | 2007-07-30 21:12:19 +1000 | [diff] [blame] | 1781 | kmem_cache_destroy(kvm_vcpu_cache); |
Zhang Xiantao | d2308784 | 2007-11-29 15:35:39 +0800 | [diff] [blame] | 1782 | out_free_5: |
Avi Kivity | 59ae6c6 | 2007-02-12 00:54:48 -0800 | [diff] [blame] | 1783 | sysdev_unregister(&kvm_sysdev); |
Zhang Xiantao | d2308784 | 2007-11-29 15:35:39 +0800 | [diff] [blame] | 1784 | out_free_4: |
Avi Kivity | 59ae6c6 | 2007-02-12 00:54:48 -0800 | [diff] [blame] | 1785 | sysdev_class_unregister(&kvm_sysdev_class); |
Zhang Xiantao | d2308784 | 2007-11-29 15:35:39 +0800 | [diff] [blame] | 1786 | out_free_3: |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1787 | unregister_reboot_notifier(&kvm_reboot_notifier); |
Avi Kivity | 774c47f | 2007-02-12 00:54:47 -0800 | [diff] [blame] | 1788 | unregister_cpu_notifier(&kvm_cpu_notifier); |
Zhang Xiantao | d2308784 | 2007-11-29 15:35:39 +0800 | [diff] [blame] | 1789 | out_free_2: |
Jens Axboe | 15c8b6c | 2008-05-09 09:39:44 +0200 | [diff] [blame] | 1790 | on_each_cpu(hardware_disable, NULL, 1); |
Zhang Xiantao | d2308784 | 2007-11-29 15:35:39 +0800 | [diff] [blame] | 1791 | out_free_1: |
Zhang Xiantao | e9b11c1 | 2007-11-14 20:38:21 +0800 | [diff] [blame] | 1792 | kvm_arch_hardware_unsetup(); |
Zhang Xiantao | d2308784 | 2007-11-29 15:35:39 +0800 | [diff] [blame] | 1793 | out_free_0: |
| 1794 | __free_page(bad_page); |
Avi Kivity | ca45aaa | 2007-03-01 19:21:03 +0200 | [diff] [blame] | 1795 | out: |
Zhang Xiantao | f8c16bb | 2007-11-14 20:40:21 +0800 | [diff] [blame] | 1796 | kvm_arch_exit(); |
Zhang Xiantao | cb498ea | 2007-11-14 20:39:31 +0800 | [diff] [blame] | 1797 | kvm_exit_debug(); |
Zhang Xiantao | d2308784 | 2007-11-29 15:35:39 +0800 | [diff] [blame] | 1798 | out_fail: |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1799 | return r; |
| 1800 | } |
Zhang Xiantao | cb498ea | 2007-11-14 20:39:31 +0800 | [diff] [blame] | 1801 | EXPORT_SYMBOL_GPL(kvm_init); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1802 | |
Zhang Xiantao | cb498ea | 2007-11-14 20:39:31 +0800 | [diff] [blame] | 1803 | void kvm_exit(void) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1804 | { |
Feng(Eric) Liu | d4c9ff2 | 2008-04-10 08:47:53 -0400 | [diff] [blame] | 1805 | kvm_trace_cleanup(); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1806 | misc_deregister(&kvm_dev); |
Rusty Russell | c16f862 | 2007-07-30 21:12:19 +1000 | [diff] [blame] | 1807 | kmem_cache_destroy(kvm_vcpu_cache); |
Avi Kivity | 59ae6c6 | 2007-02-12 00:54:48 -0800 | [diff] [blame] | 1808 | sysdev_unregister(&kvm_sysdev); |
| 1809 | sysdev_class_unregister(&kvm_sysdev_class); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1810 | unregister_reboot_notifier(&kvm_reboot_notifier); |
Avi Kivity | 59ae6c6 | 2007-02-12 00:54:48 -0800 | [diff] [blame] | 1811 | unregister_cpu_notifier(&kvm_cpu_notifier); |
Jens Axboe | 15c8b6c | 2008-05-09 09:39:44 +0200 | [diff] [blame] | 1812 | on_each_cpu(hardware_disable, NULL, 1); |
Zhang Xiantao | e9b11c1 | 2007-11-14 20:38:21 +0800 | [diff] [blame] | 1813 | kvm_arch_hardware_unsetup(); |
Zhang Xiantao | f8c16bb | 2007-11-14 20:40:21 +0800 | [diff] [blame] | 1814 | kvm_arch_exit(); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1815 | kvm_exit_debug(); |
Izik Eidus | cea7bb2 | 2007-10-17 19:17:48 +0200 | [diff] [blame] | 1816 | __free_page(bad_page); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1817 | } |
Zhang Xiantao | cb498ea | 2007-11-14 20:39:31 +0800 | [diff] [blame] | 1818 | EXPORT_SYMBOL_GPL(kvm_exit); |