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