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