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