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