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