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