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