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