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