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