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