Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Kernel-based Virtual Machine driver for Linux |
| 3 | * |
| 4 | * This module enables machines with Intel VT-x extensions to run virtual |
| 5 | * machines without emulation or binary translation. |
| 6 | * |
| 7 | * Copyright (C) 2006 Qumranet, Inc. |
| 8 | * |
| 9 | * Authors: |
| 10 | * Avi Kivity <avi@qumranet.com> |
| 11 | * Yaniv Kamay <yaniv@qumranet.com> |
| 12 | * |
| 13 | * This work is licensed under the terms of the GNU GPL, version 2. See |
| 14 | * the COPYING file in the top-level directory. |
| 15 | * |
| 16 | */ |
| 17 | |
Hollis Blanchard | e217402 | 2007-12-03 15:30:24 -0600 | [diff] [blame] | 18 | #include "iodev.h" |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 19 | |
Avi Kivity | edf8841 | 2007-12-16 11:02:48 +0200 | [diff] [blame] | 20 | #include <linux/kvm_host.h> |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 21 | #include <linux/kvm.h> |
| 22 | #include <linux/module.h> |
| 23 | #include <linux/errno.h> |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 24 | #include <linux/percpu.h> |
| 25 | #include <linux/gfp.h> |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 26 | #include <linux/mm.h> |
| 27 | #include <linux/miscdevice.h> |
| 28 | #include <linux/vmalloc.h> |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 29 | #include <linux/reboot.h> |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 30 | #include <linux/debugfs.h> |
| 31 | #include <linux/highmem.h> |
| 32 | #include <linux/file.h> |
Avi Kivity | 59ae6c6 | 2007-02-12 00:54:48 -0800 | [diff] [blame] | 33 | #include <linux/sysdev.h> |
Avi Kivity | 774c47f | 2007-02-12 00:54:47 -0800 | [diff] [blame] | 34 | #include <linux/cpu.h> |
Alexey Dobriyan | e8edc6e | 2007-05-21 01:22:52 +0400 | [diff] [blame] | 35 | #include <linux/sched.h> |
Avi Kivity | d9e368d | 2007-06-07 19:18:30 +0300 | [diff] [blame] | 36 | #include <linux/cpumask.h> |
| 37 | #include <linux/smp.h> |
Avi Kivity | d6d2816 | 2007-06-28 08:38:16 -0400 | [diff] [blame] | 38 | #include <linux/anon_inodes.h> |
Avi Kivity | 04d2cc7 | 2007-09-10 18:10:54 +0300 | [diff] [blame] | 39 | #include <linux/profile.h> |
Anthony Liguori | 7aa81cc | 2007-09-17 14:57:50 -0500 | [diff] [blame] | 40 | #include <linux/kvm_para.h> |
Izik Eidus | 6fc138d | 2007-10-09 19:20:39 +0200 | [diff] [blame] | 41 | #include <linux/pagemap.h> |
Anthony Liguori | 8d4e128 | 2007-10-18 09:59:34 -0500 | [diff] [blame] | 42 | #include <linux/mman.h> |
Anthony Liguori | 35149e2 | 2008-04-02 14:46:56 -0500 | [diff] [blame] | 43 | #include <linux/swap.h> |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 44 | |
Avi Kivity | e495606 | 2007-06-28 14:15:57 -0400 | [diff] [blame] | 45 | #include <asm/processor.h> |
Avi Kivity | e495606 | 2007-06-28 14:15:57 -0400 | [diff] [blame] | 46 | #include <asm/io.h> |
| 47 | #include <asm/uaccess.h> |
Izik Eidus | 3e021bf | 2007-11-19 11:16:57 +0200 | [diff] [blame] | 48 | #include <asm/pgtable.h> |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 49 | |
| 50 | MODULE_AUTHOR("Qumranet"); |
| 51 | MODULE_LICENSE("GPL"); |
| 52 | |
Zhang Xiantao | e9b11c1 | 2007-11-14 20:38:21 +0800 | [diff] [blame] | 53 | DEFINE_SPINLOCK(kvm_lock); |
| 54 | LIST_HEAD(vm_list); |
Avi Kivity | 133de90 | 2007-02-12 00:54:44 -0800 | [diff] [blame] | 55 | |
Avi Kivity | 1b6c016 | 2007-05-24 13:03:52 +0300 | [diff] [blame] | 56 | static cpumask_t cpus_hardware_enabled; |
| 57 | |
Rusty Russell | c16f862 | 2007-07-30 21:12:19 +1000 | [diff] [blame] | 58 | struct kmem_cache *kvm_vcpu_cache; |
| 59 | EXPORT_SYMBOL_GPL(kvm_vcpu_cache); |
Avi Kivity | 1165f5f | 2007-04-19 17:27:43 +0300 | [diff] [blame] | 60 | |
Avi Kivity | 15ad714 | 2007-07-11 18:17:21 +0300 | [diff] [blame] | 61 | static __read_mostly struct preempt_ops kvm_preempt_ops; |
| 62 | |
Feng(Eric) Liu | d4c9ff2 | 2008-04-10 08:47:53 -0400 | [diff] [blame] | 63 | struct dentry *debugfs_dir; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 64 | |
Avi Kivity | bccf215 | 2007-02-21 18:04:26 +0200 | [diff] [blame] | 65 | static long kvm_vcpu_ioctl(struct file *file, unsigned int ioctl, |
| 66 | unsigned long arg); |
| 67 | |
James Morris | 5aacf0c | 2006-12-22 01:04:55 -0800 | [diff] [blame] | 68 | static inline int valid_vcpu(int n) |
| 69 | { |
| 70 | return likely(n >= 0 && n < KVM_MAX_VCPUS); |
| 71 | } |
| 72 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 73 | /* |
| 74 | * Switches to specified vcpu, until a matching vcpu_put() |
| 75 | */ |
Carsten Otte | 313a3dc | 2007-10-11 19:16:52 +0200 | [diff] [blame] | 76 | void vcpu_load(struct kvm_vcpu *vcpu) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 77 | { |
Avi Kivity | 15ad714 | 2007-07-11 18:17:21 +0300 | [diff] [blame] | 78 | int cpu; |
| 79 | |
Avi Kivity | bccf215 | 2007-02-21 18:04:26 +0200 | [diff] [blame] | 80 | mutex_lock(&vcpu->mutex); |
Avi Kivity | 15ad714 | 2007-07-11 18:17:21 +0300 | [diff] [blame] | 81 | cpu = get_cpu(); |
| 82 | preempt_notifier_register(&vcpu->preempt_notifier); |
Carsten Otte | 313a3dc | 2007-10-11 19:16:52 +0200 | [diff] [blame] | 83 | kvm_arch_vcpu_load(vcpu, cpu); |
Avi Kivity | 15ad714 | 2007-07-11 18:17:21 +0300 | [diff] [blame] | 84 | put_cpu(); |
Avi Kivity | bccf215 | 2007-02-21 18:04:26 +0200 | [diff] [blame] | 85 | } |
| 86 | |
Carsten Otte | 313a3dc | 2007-10-11 19:16:52 +0200 | [diff] [blame] | 87 | void vcpu_put(struct kvm_vcpu *vcpu) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 88 | { |
Avi Kivity | 15ad714 | 2007-07-11 18:17:21 +0300 | [diff] [blame] | 89 | preempt_disable(); |
Carsten Otte | 313a3dc | 2007-10-11 19:16:52 +0200 | [diff] [blame] | 90 | kvm_arch_vcpu_put(vcpu); |
Avi Kivity | 15ad714 | 2007-07-11 18:17:21 +0300 | [diff] [blame] | 91 | preempt_notifier_unregister(&vcpu->preempt_notifier); |
| 92 | preempt_enable(); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 93 | mutex_unlock(&vcpu->mutex); |
| 94 | } |
| 95 | |
Avi Kivity | d9e368d | 2007-06-07 19:18:30 +0300 | [diff] [blame] | 96 | static void ack_flush(void *_completed) |
| 97 | { |
Avi Kivity | d9e368d | 2007-06-07 19:18:30 +0300 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | void kvm_flush_remote_tlbs(struct kvm *kvm) |
| 101 | { |
Laurent Vivier | 49d3bd7 | 2007-10-22 16:33:07 +0200 | [diff] [blame] | 102 | int i, cpu; |
Avi Kivity | d9e368d | 2007-06-07 19:18:30 +0300 | [diff] [blame] | 103 | cpumask_t cpus; |
| 104 | struct kvm_vcpu *vcpu; |
Avi Kivity | d9e368d | 2007-06-07 19:18:30 +0300 | [diff] [blame] | 105 | |
Avi Kivity | d9e368d | 2007-06-07 19:18:30 +0300 | [diff] [blame] | 106 | cpus_clear(cpus); |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 107 | for (i = 0; i < KVM_MAX_VCPUS; ++i) { |
| 108 | vcpu = kvm->vcpus[i]; |
| 109 | if (!vcpu) |
| 110 | continue; |
Avi Kivity | 3176bc3 | 2007-10-16 17:22:08 +0200 | [diff] [blame] | 111 | if (test_and_set_bit(KVM_REQ_TLB_FLUSH, &vcpu->requests)) |
Avi Kivity | d9e368d | 2007-06-07 19:18:30 +0300 | [diff] [blame] | 112 | continue; |
| 113 | cpu = vcpu->cpu; |
| 114 | if (cpu != -1 && cpu != raw_smp_processor_id()) |
Laurent Vivier | 49d3bd7 | 2007-10-22 16:33:07 +0200 | [diff] [blame] | 115 | cpu_set(cpu, cpus); |
Avi Kivity | d9e368d | 2007-06-07 19:18:30 +0300 | [diff] [blame] | 116 | } |
Avi Kivity | 0f74a24 | 2007-11-20 23:01:14 +0200 | [diff] [blame] | 117 | if (cpus_empty(cpus)) |
| 118 | return; |
| 119 | ++kvm->stat.remote_tlb_flush; |
Laurent Vivier | 49d3bd7 | 2007-10-22 16:33:07 +0200 | [diff] [blame] | 120 | smp_call_function_mask(cpus, ack_flush, NULL, 1); |
Avi Kivity | d9e368d | 2007-06-07 19:18:30 +0300 | [diff] [blame] | 121 | } |
| 122 | |
Marcelo Tosatti | 2e53d63 | 2008-02-20 14:47:24 -0500 | [diff] [blame] | 123 | void kvm_reload_remote_mmus(struct kvm *kvm) |
| 124 | { |
| 125 | int i, cpu; |
| 126 | cpumask_t cpus; |
| 127 | struct kvm_vcpu *vcpu; |
| 128 | |
| 129 | cpus_clear(cpus); |
| 130 | for (i = 0; i < KVM_MAX_VCPUS; ++i) { |
| 131 | vcpu = kvm->vcpus[i]; |
| 132 | if (!vcpu) |
| 133 | continue; |
| 134 | if (test_and_set_bit(KVM_REQ_MMU_RELOAD, &vcpu->requests)) |
| 135 | continue; |
| 136 | cpu = vcpu->cpu; |
| 137 | if (cpu != -1 && cpu != raw_smp_processor_id()) |
| 138 | cpu_set(cpu, cpus); |
| 139 | } |
| 140 | if (cpus_empty(cpus)) |
| 141 | return; |
| 142 | smp_call_function_mask(cpus, ack_flush, NULL, 1); |
| 143 | } |
| 144 | |
| 145 | |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 146 | int kvm_vcpu_init(struct kvm_vcpu *vcpu, struct kvm *kvm, unsigned id) |
| 147 | { |
| 148 | struct page *page; |
| 149 | int r; |
| 150 | |
| 151 | mutex_init(&vcpu->mutex); |
| 152 | vcpu->cpu = -1; |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 153 | vcpu->kvm = kvm; |
| 154 | vcpu->vcpu_id = id; |
Eddie Dong | b6958ce | 2007-07-18 12:15:21 +0300 | [diff] [blame] | 155 | init_waitqueue_head(&vcpu->wq); |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 156 | |
| 157 | page = alloc_page(GFP_KERNEL | __GFP_ZERO); |
| 158 | if (!page) { |
| 159 | r = -ENOMEM; |
| 160 | goto fail; |
| 161 | } |
| 162 | vcpu->run = page_address(page); |
| 163 | |
Zhang Xiantao | e9b11c1 | 2007-11-14 20:38:21 +0800 | [diff] [blame] | 164 | r = kvm_arch_vcpu_init(vcpu); |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 165 | if (r < 0) |
Zhang Xiantao | e9b11c1 | 2007-11-14 20:38:21 +0800 | [diff] [blame] | 166 | goto fail_free_run; |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 167 | return 0; |
| 168 | |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 169 | fail_free_run: |
| 170 | free_page((unsigned long)vcpu->run); |
| 171 | fail: |
Rusty Russell | 76fafa5 | 2007-10-08 10:50:48 +1000 | [diff] [blame] | 172 | return r; |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 173 | } |
| 174 | EXPORT_SYMBOL_GPL(kvm_vcpu_init); |
| 175 | |
| 176 | void kvm_vcpu_uninit(struct kvm_vcpu *vcpu) |
| 177 | { |
Zhang Xiantao | e9b11c1 | 2007-11-14 20:38:21 +0800 | [diff] [blame] | 178 | kvm_arch_vcpu_uninit(vcpu); |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 179 | free_page((unsigned long)vcpu->run); |
| 180 | } |
| 181 | EXPORT_SYMBOL_GPL(kvm_vcpu_uninit); |
| 182 | |
Avi Kivity | f17abe9 | 2007-02-21 19:28:04 +0200 | [diff] [blame] | 183 | static struct kvm *kvm_create_vm(void) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 184 | { |
Zhang Xiantao | d19a9cd | 2007-11-18 18:43:45 +0800 | [diff] [blame] | 185 | struct kvm *kvm = kvm_arch_create_vm(); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 186 | |
Zhang Xiantao | d19a9cd | 2007-11-18 18:43:45 +0800 | [diff] [blame] | 187 | if (IS_ERR(kvm)) |
| 188 | goto out; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 189 | |
Avi Kivity | 6d4e4c4 | 2007-11-21 16:41:05 +0200 | [diff] [blame] | 190 | kvm->mm = current->mm; |
| 191 | atomic_inc(&kvm->mm->mm_count); |
Marcelo Tosatti | aaee2c9 | 2007-12-20 19:18:26 -0500 | [diff] [blame] | 192 | spin_lock_init(&kvm->mmu_lock); |
Eddie Dong | 7490634 | 2007-06-19 18:05:03 +0300 | [diff] [blame] | 193 | kvm_io_bus_init(&kvm->pio_bus); |
Shaohua Li | 11ec280 | 2007-07-23 14:51:37 +0800 | [diff] [blame] | 194 | mutex_init(&kvm->lock); |
Gregory Haskins | 2eeb2e9 | 2007-05-31 14:08:53 -0400 | [diff] [blame] | 195 | kvm_io_bus_init(&kvm->mmio_bus); |
Izik Eidus | 72dc67a | 2008-02-10 18:04:15 +0200 | [diff] [blame] | 196 | init_rwsem(&kvm->slots_lock); |
Izik Eidus | d39f13b | 2008-03-30 16:01:25 +0300 | [diff] [blame] | 197 | atomic_set(&kvm->users_count, 1); |
Rusty Russell | 5e58cfe | 2007-07-23 17:08:21 +1000 | [diff] [blame] | 198 | spin_lock(&kvm_lock); |
| 199 | list_add(&kvm->vm_list, &vm_list); |
| 200 | spin_unlock(&kvm_lock); |
Zhang Xiantao | d19a9cd | 2007-11-18 18:43:45 +0800 | [diff] [blame] | 201 | out: |
Avi Kivity | f17abe9 | 2007-02-21 19:28:04 +0200 | [diff] [blame] | 202 | return kvm; |
| 203 | } |
| 204 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 205 | /* |
| 206 | * Free any memory in @free but not in @dont. |
| 207 | */ |
| 208 | static void kvm_free_physmem_slot(struct kvm_memory_slot *free, |
| 209 | struct kvm_memory_slot *dont) |
| 210 | { |
Izik Eidus | 290fc38 | 2007-09-27 14:11:22 +0200 | [diff] [blame] | 211 | if (!dont || free->rmap != dont->rmap) |
| 212 | vfree(free->rmap); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 213 | |
| 214 | if (!dont || free->dirty_bitmap != dont->dirty_bitmap) |
| 215 | vfree(free->dirty_bitmap); |
| 216 | |
Marcelo Tosatti | 05da455 | 2008-02-23 11:44:30 -0300 | [diff] [blame] | 217 | if (!dont || free->lpage_info != dont->lpage_info) |
| 218 | vfree(free->lpage_info); |
| 219 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 220 | free->npages = 0; |
Al Viro | 8b6d44c | 2007-02-09 16:38:40 +0000 | [diff] [blame] | 221 | free->dirty_bitmap = NULL; |
Anthony Liguori | 8d4e128 | 2007-10-18 09:59:34 -0500 | [diff] [blame] | 222 | free->rmap = NULL; |
Marcelo Tosatti | 05da455 | 2008-02-23 11:44:30 -0300 | [diff] [blame] | 223 | free->lpage_info = NULL; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 224 | } |
| 225 | |
Zhang Xiantao | d19a9cd | 2007-11-18 18:43:45 +0800 | [diff] [blame] | 226 | void kvm_free_physmem(struct kvm *kvm) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 227 | { |
| 228 | int i; |
| 229 | |
| 230 | for (i = 0; i < kvm->nmemslots; ++i) |
Al Viro | 8b6d44c | 2007-02-09 16:38:40 +0000 | [diff] [blame] | 231 | kvm_free_physmem_slot(&kvm->memslots[i], NULL); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 232 | } |
| 233 | |
Avi Kivity | f17abe9 | 2007-02-21 19:28:04 +0200 | [diff] [blame] | 234 | static void kvm_destroy_vm(struct kvm *kvm) |
| 235 | { |
Avi Kivity | 6d4e4c4 | 2007-11-21 16:41:05 +0200 | [diff] [blame] | 236 | struct mm_struct *mm = kvm->mm; |
| 237 | |
Avi Kivity | 133de90 | 2007-02-12 00:54:44 -0800 | [diff] [blame] | 238 | spin_lock(&kvm_lock); |
| 239 | list_del(&kvm->vm_list); |
| 240 | spin_unlock(&kvm_lock); |
Eddie Dong | 7490634 | 2007-06-19 18:05:03 +0300 | [diff] [blame] | 241 | kvm_io_bus_destroy(&kvm->pio_bus); |
Gregory Haskins | 2eeb2e9 | 2007-05-31 14:08:53 -0400 | [diff] [blame] | 242 | kvm_io_bus_destroy(&kvm->mmio_bus); |
Zhang Xiantao | d19a9cd | 2007-11-18 18:43:45 +0800 | [diff] [blame] | 243 | kvm_arch_destroy_vm(kvm); |
Avi Kivity | 6d4e4c4 | 2007-11-21 16:41:05 +0200 | [diff] [blame] | 244 | mmdrop(mm); |
Avi Kivity | f17abe9 | 2007-02-21 19:28:04 +0200 | [diff] [blame] | 245 | } |
| 246 | |
Izik Eidus | d39f13b | 2008-03-30 16:01:25 +0300 | [diff] [blame] | 247 | void kvm_get_kvm(struct kvm *kvm) |
| 248 | { |
| 249 | atomic_inc(&kvm->users_count); |
| 250 | } |
| 251 | EXPORT_SYMBOL_GPL(kvm_get_kvm); |
| 252 | |
| 253 | void kvm_put_kvm(struct kvm *kvm) |
| 254 | { |
| 255 | if (atomic_dec_and_test(&kvm->users_count)) |
| 256 | kvm_destroy_vm(kvm); |
| 257 | } |
| 258 | EXPORT_SYMBOL_GPL(kvm_put_kvm); |
| 259 | |
| 260 | |
Avi Kivity | f17abe9 | 2007-02-21 19:28:04 +0200 | [diff] [blame] | 261 | static int kvm_vm_release(struct inode *inode, struct file *filp) |
| 262 | { |
| 263 | struct kvm *kvm = filp->private_data; |
| 264 | |
Izik Eidus | d39f13b | 2008-03-30 16:01:25 +0300 | [diff] [blame] | 265 | kvm_put_kvm(kvm); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 266 | return 0; |
| 267 | } |
| 268 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 269 | /* |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 270 | * Allocate some memory and give it an address in the guest physical address |
| 271 | * space. |
| 272 | * |
| 273 | * Discontiguous memory is allowed, mostly for framebuffers. |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 274 | * |
Marcelo Tosatti | 10589a4 | 2007-12-20 19:18:22 -0500 | [diff] [blame] | 275 | * Must be called holding mmap_sem for write. |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 276 | */ |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 277 | int __kvm_set_memory_region(struct kvm *kvm, |
| 278 | struct kvm_userspace_memory_region *mem, |
| 279 | int user_alloc) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 280 | { |
| 281 | int r; |
| 282 | gfn_t base_gfn; |
| 283 | unsigned long npages; |
| 284 | unsigned long i; |
| 285 | struct kvm_memory_slot *memslot; |
| 286 | struct kvm_memory_slot old, new; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 287 | |
| 288 | r = -EINVAL; |
| 289 | /* General sanity checks */ |
| 290 | if (mem->memory_size & (PAGE_SIZE - 1)) |
| 291 | goto out; |
| 292 | if (mem->guest_phys_addr & (PAGE_SIZE - 1)) |
| 293 | goto out; |
Izik Eidus | e0d62c7 | 2007-10-24 23:57:46 +0200 | [diff] [blame] | 294 | if (mem->slot >= KVM_MEMORY_SLOTS + KVM_PRIVATE_MEM_SLOTS) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 295 | goto out; |
| 296 | if (mem->guest_phys_addr + mem->memory_size < mem->guest_phys_addr) |
| 297 | goto out; |
| 298 | |
| 299 | memslot = &kvm->memslots[mem->slot]; |
| 300 | base_gfn = mem->guest_phys_addr >> PAGE_SHIFT; |
| 301 | npages = mem->memory_size >> PAGE_SHIFT; |
| 302 | |
| 303 | if (!npages) |
| 304 | mem->flags &= ~KVM_MEM_LOG_DIRTY_PAGES; |
| 305 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 306 | new = old = *memslot; |
| 307 | |
| 308 | new.base_gfn = base_gfn; |
| 309 | new.npages = npages; |
| 310 | new.flags = mem->flags; |
| 311 | |
| 312 | /* Disallow changing a memory slot's size. */ |
| 313 | r = -EINVAL; |
| 314 | if (npages && old.npages && npages != old.npages) |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 315 | goto out_free; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 316 | |
| 317 | /* Check for overlaps */ |
| 318 | r = -EEXIST; |
| 319 | for (i = 0; i < KVM_MEMORY_SLOTS; ++i) { |
| 320 | struct kvm_memory_slot *s = &kvm->memslots[i]; |
| 321 | |
| 322 | if (s == memslot) |
| 323 | continue; |
| 324 | if (!((base_gfn + npages <= s->base_gfn) || |
| 325 | (base_gfn >= s->base_gfn + s->npages))) |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 326 | goto out_free; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 327 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 328 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 329 | /* Free page dirty bitmap if unneeded */ |
| 330 | if (!(new.flags & KVM_MEM_LOG_DIRTY_PAGES)) |
Al Viro | 8b6d44c | 2007-02-09 16:38:40 +0000 | [diff] [blame] | 331 | new.dirty_bitmap = NULL; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 332 | |
| 333 | r = -ENOMEM; |
| 334 | |
| 335 | /* Allocate if a slot is being created */ |
Anthony Liguori | 8d4e128 | 2007-10-18 09:59:34 -0500 | [diff] [blame] | 336 | if (npages && !new.rmap) { |
Mike Day | d77c26f | 2007-10-08 09:02:08 -0400 | [diff] [blame] | 337 | new.rmap = vmalloc(npages * sizeof(struct page *)); |
Izik Eidus | 290fc38 | 2007-09-27 14:11:22 +0200 | [diff] [blame] | 338 | |
| 339 | if (!new.rmap) |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 340 | goto out_free; |
Izik Eidus | 290fc38 | 2007-09-27 14:11:22 +0200 | [diff] [blame] | 341 | |
Izik Eidus | 290fc38 | 2007-09-27 14:11:22 +0200 | [diff] [blame] | 342 | memset(new.rmap, 0, npages * sizeof(*new.rmap)); |
Anthony Liguori | 8d4e128 | 2007-10-18 09:59:34 -0500 | [diff] [blame] | 343 | |
Izik Eidus | 80b14b5 | 2007-10-25 11:54:04 +0200 | [diff] [blame] | 344 | new.user_alloc = user_alloc; |
Zhang Xiantao | 0de1034 | 2007-11-20 16:25:04 +0800 | [diff] [blame] | 345 | new.userspace_addr = mem->userspace_addr; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 346 | } |
Marcelo Tosatti | 05da455 | 2008-02-23 11:44:30 -0300 | [diff] [blame] | 347 | if (npages && !new.lpage_info) { |
| 348 | int largepages = npages / KVM_PAGES_PER_HPAGE; |
| 349 | if (npages % KVM_PAGES_PER_HPAGE) |
| 350 | largepages++; |
| 351 | if (base_gfn % KVM_PAGES_PER_HPAGE) |
| 352 | largepages++; |
| 353 | |
| 354 | new.lpage_info = vmalloc(largepages * sizeof(*new.lpage_info)); |
| 355 | |
| 356 | if (!new.lpage_info) |
| 357 | goto out_free; |
| 358 | |
| 359 | memset(new.lpage_info, 0, largepages * sizeof(*new.lpage_info)); |
| 360 | |
| 361 | if (base_gfn % KVM_PAGES_PER_HPAGE) |
| 362 | new.lpage_info[0].write_count = 1; |
| 363 | if ((base_gfn+npages) % KVM_PAGES_PER_HPAGE) |
| 364 | new.lpage_info[largepages-1].write_count = 1; |
| 365 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 366 | |
| 367 | /* Allocate page dirty bitmap if needed */ |
| 368 | if ((new.flags & KVM_MEM_LOG_DIRTY_PAGES) && !new.dirty_bitmap) { |
| 369 | unsigned dirty_bytes = ALIGN(npages, BITS_PER_LONG) / 8; |
| 370 | |
| 371 | new.dirty_bitmap = vmalloc(dirty_bytes); |
| 372 | if (!new.dirty_bitmap) |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 373 | goto out_free; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 374 | memset(new.dirty_bitmap, 0, dirty_bytes); |
| 375 | } |
| 376 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 377 | if (mem->slot >= kvm->nmemslots) |
| 378 | kvm->nmemslots = mem->slot + 1; |
| 379 | |
| 380 | *memslot = new; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 381 | |
Zhang Xiantao | 0de1034 | 2007-11-20 16:25:04 +0800 | [diff] [blame] | 382 | r = kvm_arch_set_memory_region(kvm, mem, old, user_alloc); |
| 383 | if (r) { |
| 384 | *memslot = old; |
| 385 | goto out_free; |
Zhang Xiantao | 3ad82a7 | 2007-11-20 13:11:38 +0800 | [diff] [blame] | 386 | } |
| 387 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 388 | kvm_free_physmem_slot(&old, &new); |
| 389 | return 0; |
| 390 | |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 391 | out_free: |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 392 | kvm_free_physmem_slot(&new, &old); |
| 393 | out: |
| 394 | return r; |
Izik Eidus | 210c7c4 | 2007-10-24 23:52:57 +0200 | [diff] [blame] | 395 | |
| 396 | } |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 397 | EXPORT_SYMBOL_GPL(__kvm_set_memory_region); |
| 398 | |
| 399 | int kvm_set_memory_region(struct kvm *kvm, |
| 400 | struct kvm_userspace_memory_region *mem, |
| 401 | int user_alloc) |
| 402 | { |
| 403 | int r; |
| 404 | |
Izik Eidus | 72dc67a | 2008-02-10 18:04:15 +0200 | [diff] [blame] | 405 | down_write(&kvm->slots_lock); |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 406 | r = __kvm_set_memory_region(kvm, mem, user_alloc); |
Izik Eidus | 72dc67a | 2008-02-10 18:04:15 +0200 | [diff] [blame] | 407 | up_write(&kvm->slots_lock); |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 408 | return r; |
| 409 | } |
Izik Eidus | 210c7c4 | 2007-10-24 23:52:57 +0200 | [diff] [blame] | 410 | EXPORT_SYMBOL_GPL(kvm_set_memory_region); |
| 411 | |
Carsten Otte | 1fe779f | 2007-10-29 16:08:35 +0100 | [diff] [blame] | 412 | int kvm_vm_ioctl_set_memory_region(struct kvm *kvm, |
| 413 | struct |
| 414 | kvm_userspace_memory_region *mem, |
| 415 | int user_alloc) |
Izik Eidus | 210c7c4 | 2007-10-24 23:52:57 +0200 | [diff] [blame] | 416 | { |
Izik Eidus | e0d62c7 | 2007-10-24 23:57:46 +0200 | [diff] [blame] | 417 | if (mem->slot >= KVM_MEMORY_SLOTS) |
| 418 | return -EINVAL; |
Izik Eidus | 210c7c4 | 2007-10-24 23:52:57 +0200 | [diff] [blame] | 419 | return kvm_set_memory_region(kvm, mem, user_alloc); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 420 | } |
| 421 | |
Zhang Xiantao | 5bb064d | 2007-11-18 20:29:43 +0800 | [diff] [blame] | 422 | int kvm_get_dirty_log(struct kvm *kvm, |
| 423 | struct kvm_dirty_log *log, int *is_dirty) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 424 | { |
| 425 | struct kvm_memory_slot *memslot; |
| 426 | int r, i; |
| 427 | int n; |
| 428 | unsigned long any = 0; |
| 429 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 430 | r = -EINVAL; |
| 431 | if (log->slot >= KVM_MEMORY_SLOTS) |
| 432 | goto out; |
| 433 | |
| 434 | memslot = &kvm->memslots[log->slot]; |
| 435 | r = -ENOENT; |
| 436 | if (!memslot->dirty_bitmap) |
| 437 | goto out; |
| 438 | |
Uri Lublin | cd1a4a9 | 2007-02-22 16:43:09 +0200 | [diff] [blame] | 439 | n = ALIGN(memslot->npages, BITS_PER_LONG) / 8; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 440 | |
Uri Lublin | cd1a4a9 | 2007-02-22 16:43:09 +0200 | [diff] [blame] | 441 | for (i = 0; !any && i < n/sizeof(long); ++i) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 442 | any = memslot->dirty_bitmap[i]; |
| 443 | |
| 444 | r = -EFAULT; |
| 445 | if (copy_to_user(log->dirty_bitmap, memslot->dirty_bitmap, n)) |
| 446 | goto out; |
| 447 | |
Zhang Xiantao | 5bb064d | 2007-11-18 20:29:43 +0800 | [diff] [blame] | 448 | if (any) |
| 449 | *is_dirty = 1; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 450 | |
| 451 | r = 0; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 452 | out: |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 453 | return r; |
| 454 | } |
| 455 | |
Izik Eidus | cea7bb2 | 2007-10-17 19:17:48 +0200 | [diff] [blame] | 456 | int is_error_page(struct page *page) |
| 457 | { |
| 458 | return page == bad_page; |
| 459 | } |
| 460 | EXPORT_SYMBOL_GPL(is_error_page); |
| 461 | |
Anthony Liguori | 35149e2 | 2008-04-02 14:46:56 -0500 | [diff] [blame] | 462 | int is_error_pfn(pfn_t pfn) |
| 463 | { |
| 464 | return pfn == bad_pfn; |
| 465 | } |
| 466 | EXPORT_SYMBOL_GPL(is_error_pfn); |
| 467 | |
Izik Eidus | f9d46eb | 2007-11-11 22:02:22 +0200 | [diff] [blame] | 468 | static inline unsigned long bad_hva(void) |
| 469 | { |
| 470 | return PAGE_OFFSET; |
| 471 | } |
| 472 | |
| 473 | int kvm_is_error_hva(unsigned long addr) |
| 474 | { |
| 475 | return addr == bad_hva(); |
| 476 | } |
| 477 | EXPORT_SYMBOL_GPL(kvm_is_error_hva); |
| 478 | |
Avi Kivity | e820754 | 2007-03-30 16:54:30 +0300 | [diff] [blame] | 479 | static 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] | 480 | { |
| 481 | int i; |
| 482 | |
| 483 | for (i = 0; i < kvm->nmemslots; ++i) { |
| 484 | struct kvm_memory_slot *memslot = &kvm->memslots[i]; |
| 485 | |
| 486 | if (gfn >= memslot->base_gfn |
| 487 | && gfn < memslot->base_gfn + memslot->npages) |
| 488 | return memslot; |
| 489 | } |
Al Viro | 8b6d44c | 2007-02-09 16:38:40 +0000 | [diff] [blame] | 490 | return NULL; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 491 | } |
Avi Kivity | e820754 | 2007-03-30 16:54:30 +0300 | [diff] [blame] | 492 | |
| 493 | struct kvm_memory_slot *gfn_to_memslot(struct kvm *kvm, gfn_t gfn) |
| 494 | { |
| 495 | gfn = unalias_gfn(kvm, gfn); |
| 496 | return __gfn_to_memslot(kvm, gfn); |
| 497 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 498 | |
Izik Eidus | e0d62c7 | 2007-10-24 23:57:46 +0200 | [diff] [blame] | 499 | int kvm_is_visible_gfn(struct kvm *kvm, gfn_t gfn) |
| 500 | { |
| 501 | int i; |
| 502 | |
| 503 | gfn = unalias_gfn(kvm, gfn); |
| 504 | for (i = 0; i < KVM_MEMORY_SLOTS; ++i) { |
| 505 | struct kvm_memory_slot *memslot = &kvm->memslots[i]; |
| 506 | |
| 507 | if (gfn >= memslot->base_gfn |
| 508 | && gfn < memslot->base_gfn + memslot->npages) |
| 509 | return 1; |
| 510 | } |
| 511 | return 0; |
| 512 | } |
| 513 | EXPORT_SYMBOL_GPL(kvm_is_visible_gfn); |
| 514 | |
Marcelo Tosatti | 05da455 | 2008-02-23 11:44:30 -0300 | [diff] [blame] | 515 | unsigned long gfn_to_hva(struct kvm *kvm, gfn_t gfn) |
Izik Eidus | 539cb66 | 2007-11-11 22:05:04 +0200 | [diff] [blame] | 516 | { |
| 517 | struct kvm_memory_slot *slot; |
| 518 | |
| 519 | gfn = unalias_gfn(kvm, gfn); |
| 520 | slot = __gfn_to_memslot(kvm, gfn); |
| 521 | if (!slot) |
| 522 | return bad_hva(); |
| 523 | return (slot->userspace_addr + (gfn - slot->base_gfn) * PAGE_SIZE); |
| 524 | } |
| 525 | |
Anthony Liguori | aab61cc | 2007-10-29 15:15:20 -0500 | [diff] [blame] | 526 | /* |
| 527 | * Requires current->mm->mmap_sem to be held |
| 528 | */ |
Anthony Liguori | 35149e2 | 2008-04-02 14:46:56 -0500 | [diff] [blame] | 529 | pfn_t gfn_to_pfn(struct kvm *kvm, gfn_t gfn) |
Avi Kivity | 954bbbc | 2007-03-30 14:02:32 +0300 | [diff] [blame] | 530 | { |
Anthony Liguori | 8d4e128 | 2007-10-18 09:59:34 -0500 | [diff] [blame] | 531 | struct page *page[1]; |
Izik Eidus | 539cb66 | 2007-11-11 22:05:04 +0200 | [diff] [blame] | 532 | unsigned long addr; |
Anthony Liguori | 8d4e128 | 2007-10-18 09:59:34 -0500 | [diff] [blame] | 533 | int npages; |
Avi Kivity | 954bbbc | 2007-03-30 14:02:32 +0300 | [diff] [blame] | 534 | |
Avi Kivity | 6039522 | 2007-10-21 11:03:36 +0200 | [diff] [blame] | 535 | might_sleep(); |
| 536 | |
Izik Eidus | 539cb66 | 2007-11-11 22:05:04 +0200 | [diff] [blame] | 537 | addr = gfn_to_hva(kvm, gfn); |
| 538 | if (kvm_is_error_hva(addr)) { |
Izik Eidus | 8a7ae05 | 2007-10-18 11:09:33 +0200 | [diff] [blame] | 539 | get_page(bad_page); |
Anthony Liguori | 35149e2 | 2008-04-02 14:46:56 -0500 | [diff] [blame] | 540 | return page_to_pfn(bad_page); |
Izik Eidus | 8a7ae05 | 2007-10-18 11:09:33 +0200 | [diff] [blame] | 541 | } |
Izik Eidus | 8a7ae05 | 2007-10-18 11:09:33 +0200 | [diff] [blame] | 542 | |
Izik Eidus | 539cb66 | 2007-11-11 22:05:04 +0200 | [diff] [blame] | 543 | npages = get_user_pages(current, current->mm, addr, 1, 1, 1, page, |
| 544 | NULL); |
| 545 | |
Anthony Liguori | 8d4e128 | 2007-10-18 09:59:34 -0500 | [diff] [blame] | 546 | if (npages != 1) { |
| 547 | get_page(bad_page); |
Anthony Liguori | 35149e2 | 2008-04-02 14:46:56 -0500 | [diff] [blame] | 548 | return page_to_pfn(bad_page); |
Izik Eidus | 8a7ae05 | 2007-10-18 11:09:33 +0200 | [diff] [blame] | 549 | } |
Anthony Liguori | 8d4e128 | 2007-10-18 09:59:34 -0500 | [diff] [blame] | 550 | |
Anthony Liguori | 35149e2 | 2008-04-02 14:46:56 -0500 | [diff] [blame] | 551 | return page_to_pfn(page[0]); |
| 552 | } |
| 553 | |
| 554 | EXPORT_SYMBOL_GPL(gfn_to_pfn); |
| 555 | |
| 556 | struct page *gfn_to_page(struct kvm *kvm, gfn_t gfn) |
| 557 | { |
| 558 | return pfn_to_page(gfn_to_pfn(kvm, gfn)); |
Avi Kivity | 954bbbc | 2007-03-30 14:02:32 +0300 | [diff] [blame] | 559 | } |
Anthony Liguori | aab61cc | 2007-10-29 15:15:20 -0500 | [diff] [blame] | 560 | |
Avi Kivity | 954bbbc | 2007-03-30 14:02:32 +0300 | [diff] [blame] | 561 | EXPORT_SYMBOL_GPL(gfn_to_page); |
| 562 | |
Izik Eidus | b4231d6 | 2007-11-20 11:49:33 +0200 | [diff] [blame] | 563 | void kvm_release_page_clean(struct page *page) |
| 564 | { |
Anthony Liguori | 35149e2 | 2008-04-02 14:46:56 -0500 | [diff] [blame] | 565 | kvm_release_pfn_clean(page_to_pfn(page)); |
Izik Eidus | b4231d6 | 2007-11-20 11:49:33 +0200 | [diff] [blame] | 566 | } |
| 567 | EXPORT_SYMBOL_GPL(kvm_release_page_clean); |
| 568 | |
Anthony Liguori | 35149e2 | 2008-04-02 14:46:56 -0500 | [diff] [blame] | 569 | void kvm_release_pfn_clean(pfn_t pfn) |
| 570 | { |
| 571 | put_page(pfn_to_page(pfn)); |
| 572 | } |
| 573 | EXPORT_SYMBOL_GPL(kvm_release_pfn_clean); |
| 574 | |
Izik Eidus | b4231d6 | 2007-11-20 11:49:33 +0200 | [diff] [blame] | 575 | void kvm_release_page_dirty(struct page *page) |
Izik Eidus | 8a7ae05 | 2007-10-18 11:09:33 +0200 | [diff] [blame] | 576 | { |
Anthony Liguori | 35149e2 | 2008-04-02 14:46:56 -0500 | [diff] [blame] | 577 | kvm_release_pfn_dirty(page_to_pfn(page)); |
Izik Eidus | 8a7ae05 | 2007-10-18 11:09:33 +0200 | [diff] [blame] | 578 | } |
Izik Eidus | b4231d6 | 2007-11-20 11:49:33 +0200 | [diff] [blame] | 579 | EXPORT_SYMBOL_GPL(kvm_release_page_dirty); |
Izik Eidus | 8a7ae05 | 2007-10-18 11:09:33 +0200 | [diff] [blame] | 580 | |
Anthony Liguori | 35149e2 | 2008-04-02 14:46:56 -0500 | [diff] [blame] | 581 | void kvm_release_pfn_dirty(pfn_t pfn) |
| 582 | { |
| 583 | kvm_set_pfn_dirty(pfn); |
| 584 | kvm_release_pfn_clean(pfn); |
| 585 | } |
| 586 | EXPORT_SYMBOL_GPL(kvm_release_pfn_dirty); |
| 587 | |
| 588 | void kvm_set_page_dirty(struct page *page) |
| 589 | { |
| 590 | kvm_set_pfn_dirty(page_to_pfn(page)); |
| 591 | } |
| 592 | EXPORT_SYMBOL_GPL(kvm_set_page_dirty); |
| 593 | |
| 594 | void kvm_set_pfn_dirty(pfn_t pfn) |
| 595 | { |
| 596 | struct page *page = pfn_to_page(pfn); |
| 597 | if (!PageReserved(page)) |
| 598 | SetPageDirty(page); |
| 599 | } |
| 600 | EXPORT_SYMBOL_GPL(kvm_set_pfn_dirty); |
| 601 | |
| 602 | void kvm_set_pfn_accessed(pfn_t pfn) |
| 603 | { |
| 604 | mark_page_accessed(pfn_to_page(pfn)); |
| 605 | } |
| 606 | EXPORT_SYMBOL_GPL(kvm_set_pfn_accessed); |
| 607 | |
| 608 | void kvm_get_pfn(pfn_t pfn) |
| 609 | { |
| 610 | get_page(pfn_to_page(pfn)); |
| 611 | } |
| 612 | EXPORT_SYMBOL_GPL(kvm_get_pfn); |
| 613 | |
Izik Eidus | 195aefd | 2007-10-01 22:14:18 +0200 | [diff] [blame] | 614 | static int next_segment(unsigned long len, int offset) |
| 615 | { |
| 616 | if (len > PAGE_SIZE - offset) |
| 617 | return PAGE_SIZE - offset; |
| 618 | else |
| 619 | return len; |
| 620 | } |
| 621 | |
| 622 | int kvm_read_guest_page(struct kvm *kvm, gfn_t gfn, void *data, int offset, |
| 623 | int len) |
| 624 | { |
Izik Eidus | e0506bc | 2007-11-11 22:10:22 +0200 | [diff] [blame] | 625 | int r; |
| 626 | unsigned long addr; |
Izik Eidus | 195aefd | 2007-10-01 22:14:18 +0200 | [diff] [blame] | 627 | |
Izik Eidus | e0506bc | 2007-11-11 22:10:22 +0200 | [diff] [blame] | 628 | addr = gfn_to_hva(kvm, gfn); |
| 629 | if (kvm_is_error_hva(addr)) |
Izik Eidus | 195aefd | 2007-10-01 22:14:18 +0200 | [diff] [blame] | 630 | return -EFAULT; |
Izik Eidus | e0506bc | 2007-11-11 22:10:22 +0200 | [diff] [blame] | 631 | r = copy_from_user(data, (void __user *)addr + offset, len); |
| 632 | if (r) |
| 633 | return -EFAULT; |
Izik Eidus | 195aefd | 2007-10-01 22:14:18 +0200 | [diff] [blame] | 634 | return 0; |
| 635 | } |
| 636 | EXPORT_SYMBOL_GPL(kvm_read_guest_page); |
| 637 | |
| 638 | int kvm_read_guest(struct kvm *kvm, gpa_t gpa, void *data, unsigned long len) |
| 639 | { |
| 640 | gfn_t gfn = gpa >> PAGE_SHIFT; |
| 641 | int seg; |
| 642 | int offset = offset_in_page(gpa); |
| 643 | int ret; |
| 644 | |
| 645 | while ((seg = next_segment(len, offset)) != 0) { |
| 646 | ret = kvm_read_guest_page(kvm, gfn, data, offset, seg); |
| 647 | if (ret < 0) |
| 648 | return ret; |
| 649 | offset = 0; |
| 650 | len -= seg; |
| 651 | data += seg; |
| 652 | ++gfn; |
| 653 | } |
| 654 | return 0; |
| 655 | } |
| 656 | EXPORT_SYMBOL_GPL(kvm_read_guest); |
| 657 | |
Marcelo Tosatti | 7ec5458 | 2007-12-20 19:18:23 -0500 | [diff] [blame] | 658 | int kvm_read_guest_atomic(struct kvm *kvm, gpa_t gpa, void *data, |
| 659 | unsigned long len) |
| 660 | { |
| 661 | int r; |
| 662 | unsigned long addr; |
| 663 | gfn_t gfn = gpa >> PAGE_SHIFT; |
| 664 | int offset = offset_in_page(gpa); |
| 665 | |
| 666 | addr = gfn_to_hva(kvm, gfn); |
| 667 | if (kvm_is_error_hva(addr)) |
| 668 | return -EFAULT; |
Andrea Arcangeli | 0aac03f | 2008-01-30 19:57:35 +0100 | [diff] [blame] | 669 | pagefault_disable(); |
Marcelo Tosatti | 7ec5458 | 2007-12-20 19:18:23 -0500 | [diff] [blame] | 670 | r = __copy_from_user_inatomic(data, (void __user *)addr + offset, len); |
Andrea Arcangeli | 0aac03f | 2008-01-30 19:57:35 +0100 | [diff] [blame] | 671 | pagefault_enable(); |
Marcelo Tosatti | 7ec5458 | 2007-12-20 19:18:23 -0500 | [diff] [blame] | 672 | if (r) |
| 673 | return -EFAULT; |
| 674 | return 0; |
| 675 | } |
| 676 | EXPORT_SYMBOL(kvm_read_guest_atomic); |
| 677 | |
Izik Eidus | 195aefd | 2007-10-01 22:14:18 +0200 | [diff] [blame] | 678 | int kvm_write_guest_page(struct kvm *kvm, gfn_t gfn, const void *data, |
| 679 | int offset, int len) |
| 680 | { |
Izik Eidus | e0506bc | 2007-11-11 22:10:22 +0200 | [diff] [blame] | 681 | int r; |
| 682 | unsigned long addr; |
Izik Eidus | 195aefd | 2007-10-01 22:14:18 +0200 | [diff] [blame] | 683 | |
Izik Eidus | e0506bc | 2007-11-11 22:10:22 +0200 | [diff] [blame] | 684 | addr = gfn_to_hva(kvm, gfn); |
| 685 | if (kvm_is_error_hva(addr)) |
Izik Eidus | 195aefd | 2007-10-01 22:14:18 +0200 | [diff] [blame] | 686 | return -EFAULT; |
Izik Eidus | e0506bc | 2007-11-11 22:10:22 +0200 | [diff] [blame] | 687 | r = copy_to_user((void __user *)addr + offset, data, len); |
| 688 | if (r) |
| 689 | return -EFAULT; |
Izik Eidus | 195aefd | 2007-10-01 22:14:18 +0200 | [diff] [blame] | 690 | mark_page_dirty(kvm, gfn); |
| 691 | return 0; |
| 692 | } |
| 693 | EXPORT_SYMBOL_GPL(kvm_write_guest_page); |
| 694 | |
| 695 | int kvm_write_guest(struct kvm *kvm, gpa_t gpa, const void *data, |
| 696 | unsigned long len) |
| 697 | { |
| 698 | gfn_t gfn = gpa >> PAGE_SHIFT; |
| 699 | int seg; |
| 700 | int offset = offset_in_page(gpa); |
| 701 | int ret; |
| 702 | |
| 703 | while ((seg = next_segment(len, offset)) != 0) { |
| 704 | ret = kvm_write_guest_page(kvm, gfn, data, offset, seg); |
| 705 | if (ret < 0) |
| 706 | return ret; |
| 707 | offset = 0; |
| 708 | len -= seg; |
| 709 | data += seg; |
| 710 | ++gfn; |
| 711 | } |
| 712 | return 0; |
| 713 | } |
| 714 | |
| 715 | int kvm_clear_guest_page(struct kvm *kvm, gfn_t gfn, int offset, int len) |
| 716 | { |
Izik Eidus | 3e021bf | 2007-11-19 11:16:57 +0200 | [diff] [blame] | 717 | return kvm_write_guest_page(kvm, gfn, empty_zero_page, offset, len); |
Izik Eidus | 195aefd | 2007-10-01 22:14:18 +0200 | [diff] [blame] | 718 | } |
| 719 | EXPORT_SYMBOL_GPL(kvm_clear_guest_page); |
| 720 | |
| 721 | int kvm_clear_guest(struct kvm *kvm, gpa_t gpa, unsigned long len) |
| 722 | { |
| 723 | gfn_t gfn = gpa >> PAGE_SHIFT; |
| 724 | int seg; |
| 725 | int offset = offset_in_page(gpa); |
| 726 | int ret; |
| 727 | |
| 728 | while ((seg = next_segment(len, offset)) != 0) { |
| 729 | ret = kvm_clear_guest_page(kvm, gfn, offset, seg); |
| 730 | if (ret < 0) |
| 731 | return ret; |
| 732 | offset = 0; |
| 733 | len -= seg; |
| 734 | ++gfn; |
| 735 | } |
| 736 | return 0; |
| 737 | } |
| 738 | EXPORT_SYMBOL_GPL(kvm_clear_guest); |
| 739 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 740 | void mark_page_dirty(struct kvm *kvm, gfn_t gfn) |
| 741 | { |
Nguyen Anh Quynh | 3138994 | 2007-06-05 10:35:19 +0300 | [diff] [blame] | 742 | struct kvm_memory_slot *memslot; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 743 | |
Uri Lublin | 3b6fff1 | 2007-10-30 10:42:09 +0200 | [diff] [blame] | 744 | gfn = unalias_gfn(kvm, gfn); |
Rusty Russell | 7e9d619 | 2007-07-31 20:41:14 +1000 | [diff] [blame] | 745 | memslot = __gfn_to_memslot(kvm, gfn); |
| 746 | if (memslot && memslot->dirty_bitmap) { |
| 747 | unsigned long rel_gfn = gfn - memslot->base_gfn; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 748 | |
Rusty Russell | 7e9d619 | 2007-07-31 20:41:14 +1000 | [diff] [blame] | 749 | /* avoid RMW */ |
| 750 | if (!test_bit(rel_gfn, memslot->dirty_bitmap)) |
| 751 | set_bit(rel_gfn, memslot->dirty_bitmap); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 752 | } |
| 753 | } |
| 754 | |
Eddie Dong | b6958ce | 2007-07-18 12:15:21 +0300 | [diff] [blame] | 755 | /* |
| 756 | * The vCPU has executed a HLT instruction with in-kernel mode enabled. |
| 757 | */ |
Hollis Blanchard | 8776e51 | 2007-10-31 17:24:24 -0500 | [diff] [blame] | 758 | void kvm_vcpu_block(struct kvm_vcpu *vcpu) |
Eddie Dong | b6958ce | 2007-07-18 12:15:21 +0300 | [diff] [blame] | 759 | { |
| 760 | DECLARE_WAITQUEUE(wait, current); |
| 761 | |
| 762 | add_wait_queue(&vcpu->wq, &wait); |
| 763 | |
| 764 | /* |
| 765 | * We will block until either an interrupt or a signal wakes us up |
| 766 | */ |
He, Qing | c5ec153 | 2007-09-03 17:07:41 +0300 | [diff] [blame] | 767 | while (!kvm_cpu_has_interrupt(vcpu) |
Marcelo Tosatti | 3d80840 | 2008-04-11 14:53:26 -0300 | [diff] [blame^] | 768 | && !kvm_cpu_has_pending_timer(vcpu) |
He, Qing | c5ec153 | 2007-09-03 17:07:41 +0300 | [diff] [blame] | 769 | && !signal_pending(current) |
Hollis Blanchard | 53e0aa7 | 2007-12-03 16:15:26 -0600 | [diff] [blame] | 770 | && !kvm_arch_vcpu_runnable(vcpu)) { |
Eddie Dong | b6958ce | 2007-07-18 12:15:21 +0300 | [diff] [blame] | 771 | set_current_state(TASK_INTERRUPTIBLE); |
| 772 | vcpu_put(vcpu); |
| 773 | schedule(); |
| 774 | vcpu_load(vcpu); |
| 775 | } |
| 776 | |
He, Qing | c5ec153 | 2007-09-03 17:07:41 +0300 | [diff] [blame] | 777 | __set_current_state(TASK_RUNNING); |
Eddie Dong | b6958ce | 2007-07-18 12:15:21 +0300 | [diff] [blame] | 778 | remove_wait_queue(&vcpu->wq, &wait); |
Eddie Dong | b6958ce | 2007-07-18 12:15:21 +0300 | [diff] [blame] | 779 | } |
| 780 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 781 | void kvm_resched(struct kvm_vcpu *vcpu) |
| 782 | { |
Yaozu Dong | 3fca036 | 2007-04-25 16:49:19 +0300 | [diff] [blame] | 783 | if (!need_resched()) |
| 784 | return; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 785 | cond_resched(); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 786 | } |
| 787 | EXPORT_SYMBOL_GPL(kvm_resched); |
| 788 | |
npiggin@suse.de | e4a533a | 2007-12-05 18:15:52 +1100 | [diff] [blame] | 789 | 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] | 790 | { |
| 791 | struct kvm_vcpu *vcpu = vma->vm_file->private_data; |
Avi Kivity | 9a2bb7f | 2007-02-22 12:58:31 +0200 | [diff] [blame] | 792 | struct page *page; |
| 793 | |
npiggin@suse.de | e4a533a | 2007-12-05 18:15:52 +1100 | [diff] [blame] | 794 | if (vmf->pgoff == 0) |
Avi Kivity | 039576c | 2007-03-20 12:46:50 +0200 | [diff] [blame] | 795 | page = virt_to_page(vcpu->run); |
Avi Kivity | 0956676 | 2008-01-23 18:14:23 +0200 | [diff] [blame] | 796 | #ifdef CONFIG_X86 |
npiggin@suse.de | e4a533a | 2007-12-05 18:15:52 +1100 | [diff] [blame] | 797 | else if (vmf->pgoff == KVM_PIO_PAGE_OFFSET) |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 798 | page = virt_to_page(vcpu->arch.pio_data); |
Avi Kivity | 0956676 | 2008-01-23 18:14:23 +0200 | [diff] [blame] | 799 | #endif |
Avi Kivity | 039576c | 2007-03-20 12:46:50 +0200 | [diff] [blame] | 800 | else |
npiggin@suse.de | e4a533a | 2007-12-05 18:15:52 +1100 | [diff] [blame] | 801 | return VM_FAULT_SIGBUS; |
Avi Kivity | 9a2bb7f | 2007-02-22 12:58:31 +0200 | [diff] [blame] | 802 | get_page(page); |
npiggin@suse.de | e4a533a | 2007-12-05 18:15:52 +1100 | [diff] [blame] | 803 | vmf->page = page; |
| 804 | return 0; |
Avi Kivity | 9a2bb7f | 2007-02-22 12:58:31 +0200 | [diff] [blame] | 805 | } |
| 806 | |
| 807 | static struct vm_operations_struct kvm_vcpu_vm_ops = { |
npiggin@suse.de | e4a533a | 2007-12-05 18:15:52 +1100 | [diff] [blame] | 808 | .fault = kvm_vcpu_fault, |
Avi Kivity | 9a2bb7f | 2007-02-22 12:58:31 +0200 | [diff] [blame] | 809 | }; |
| 810 | |
| 811 | static int kvm_vcpu_mmap(struct file *file, struct vm_area_struct *vma) |
| 812 | { |
| 813 | vma->vm_ops = &kvm_vcpu_vm_ops; |
| 814 | return 0; |
| 815 | } |
| 816 | |
Avi Kivity | bccf215 | 2007-02-21 18:04:26 +0200 | [diff] [blame] | 817 | static int kvm_vcpu_release(struct inode *inode, struct file *filp) |
| 818 | { |
| 819 | struct kvm_vcpu *vcpu = filp->private_data; |
| 820 | |
| 821 | fput(vcpu->kvm->filp); |
| 822 | return 0; |
| 823 | } |
| 824 | |
Jan Engelhardt | 5c50274 | 2008-01-22 20:46:14 +0100 | [diff] [blame] | 825 | static const struct file_operations kvm_vcpu_fops = { |
Avi Kivity | bccf215 | 2007-02-21 18:04:26 +0200 | [diff] [blame] | 826 | .release = kvm_vcpu_release, |
| 827 | .unlocked_ioctl = kvm_vcpu_ioctl, |
| 828 | .compat_ioctl = kvm_vcpu_ioctl, |
Avi Kivity | 9a2bb7f | 2007-02-22 12:58:31 +0200 | [diff] [blame] | 829 | .mmap = kvm_vcpu_mmap, |
Avi Kivity | bccf215 | 2007-02-21 18:04:26 +0200 | [diff] [blame] | 830 | }; |
| 831 | |
| 832 | /* |
| 833 | * Allocates an inode for the vcpu. |
| 834 | */ |
| 835 | static int create_vcpu_fd(struct kvm_vcpu *vcpu) |
| 836 | { |
| 837 | int fd, r; |
| 838 | struct inode *inode; |
| 839 | struct file *file; |
| 840 | |
Avi Kivity | d6d2816 | 2007-06-28 08:38:16 -0400 | [diff] [blame] | 841 | r = anon_inode_getfd(&fd, &inode, &file, |
| 842 | "kvm-vcpu", &kvm_vcpu_fops, vcpu); |
| 843 | if (r) |
| 844 | return r; |
Avi Kivity | bccf215 | 2007-02-21 18:04:26 +0200 | [diff] [blame] | 845 | atomic_inc(&vcpu->kvm->filp->f_count); |
Avi Kivity | bccf215 | 2007-02-21 18:04:26 +0200 | [diff] [blame] | 846 | return fd; |
Avi Kivity | bccf215 | 2007-02-21 18:04:26 +0200 | [diff] [blame] | 847 | } |
| 848 | |
Avi Kivity | c5ea766 | 2007-02-20 18:41:05 +0200 | [diff] [blame] | 849 | /* |
| 850 | * Creates some virtual cpus. Good luck creating more than one. |
| 851 | */ |
| 852 | static int kvm_vm_ioctl_create_vcpu(struct kvm *kvm, int n) |
| 853 | { |
| 854 | int r; |
| 855 | struct kvm_vcpu *vcpu; |
| 856 | |
Avi Kivity | c5ea766 | 2007-02-20 18:41:05 +0200 | [diff] [blame] | 857 | if (!valid_vcpu(n)) |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 858 | return -EINVAL; |
Avi Kivity | c5ea766 | 2007-02-20 18:41:05 +0200 | [diff] [blame] | 859 | |
Zhang Xiantao | e9b11c1 | 2007-11-14 20:38:21 +0800 | [diff] [blame] | 860 | vcpu = kvm_arch_vcpu_create(kvm, n); |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 861 | if (IS_ERR(vcpu)) |
| 862 | return PTR_ERR(vcpu); |
Avi Kivity | c5ea766 | 2007-02-20 18:41:05 +0200 | [diff] [blame] | 863 | |
Avi Kivity | 15ad714 | 2007-07-11 18:17:21 +0300 | [diff] [blame] | 864 | preempt_notifier_init(&vcpu->preempt_notifier, &kvm_preempt_ops); |
| 865 | |
Avi Kivity | 26e5215 | 2007-11-20 15:30:24 +0200 | [diff] [blame] | 866 | r = kvm_arch_vcpu_setup(vcpu); |
| 867 | if (r) |
| 868 | goto vcpu_destroy; |
| 869 | |
Shaohua Li | 11ec280 | 2007-07-23 14:51:37 +0800 | [diff] [blame] | 870 | mutex_lock(&kvm->lock); |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 871 | if (kvm->vcpus[n]) { |
| 872 | r = -EEXIST; |
Shaohua Li | 11ec280 | 2007-07-23 14:51:37 +0800 | [diff] [blame] | 873 | mutex_unlock(&kvm->lock); |
Zhang Xiantao | e9b11c1 | 2007-11-14 20:38:21 +0800 | [diff] [blame] | 874 | goto vcpu_destroy; |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 875 | } |
| 876 | kvm->vcpus[n] = vcpu; |
Shaohua Li | 11ec280 | 2007-07-23 14:51:37 +0800 | [diff] [blame] | 877 | mutex_unlock(&kvm->lock); |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 878 | |
| 879 | /* Now it's all set up, let userspace reach it */ |
Avi Kivity | bccf215 | 2007-02-21 18:04:26 +0200 | [diff] [blame] | 880 | r = create_vcpu_fd(vcpu); |
| 881 | if (r < 0) |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 882 | goto unlink; |
Avi Kivity | bccf215 | 2007-02-21 18:04:26 +0200 | [diff] [blame] | 883 | return r; |
Avi Kivity | c5ea766 | 2007-02-20 18:41:05 +0200 | [diff] [blame] | 884 | |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 885 | unlink: |
Shaohua Li | 11ec280 | 2007-07-23 14:51:37 +0800 | [diff] [blame] | 886 | mutex_lock(&kvm->lock); |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 887 | kvm->vcpus[n] = NULL; |
Shaohua Li | 11ec280 | 2007-07-23 14:51:37 +0800 | [diff] [blame] | 888 | mutex_unlock(&kvm->lock); |
Zhang Xiantao | e9b11c1 | 2007-11-14 20:38:21 +0800 | [diff] [blame] | 889 | vcpu_destroy: |
Hollis Blanchard | d40ccc6 | 2007-11-19 14:04:43 -0600 | [diff] [blame] | 890 | kvm_arch_vcpu_destroy(vcpu); |
Avi Kivity | c5ea766 | 2007-02-20 18:41:05 +0200 | [diff] [blame] | 891 | return r; |
| 892 | } |
| 893 | |
Avi Kivity | 1961d27 | 2007-03-05 19:46:05 +0200 | [diff] [blame] | 894 | static int kvm_vcpu_ioctl_set_sigmask(struct kvm_vcpu *vcpu, sigset_t *sigset) |
| 895 | { |
| 896 | if (sigset) { |
| 897 | sigdelsetmask(sigset, sigmask(SIGKILL)|sigmask(SIGSTOP)); |
| 898 | vcpu->sigset_active = 1; |
| 899 | vcpu->sigset = *sigset; |
| 900 | } else |
| 901 | vcpu->sigset_active = 0; |
| 902 | return 0; |
| 903 | } |
| 904 | |
Avi Kivity | bccf215 | 2007-02-21 18:04:26 +0200 | [diff] [blame] | 905 | static long kvm_vcpu_ioctl(struct file *filp, |
| 906 | unsigned int ioctl, unsigned long arg) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 907 | { |
Avi Kivity | bccf215 | 2007-02-21 18:04:26 +0200 | [diff] [blame] | 908 | struct kvm_vcpu *vcpu = filp->private_data; |
Al Viro | 2f366987 | 2007-02-09 16:38:35 +0000 | [diff] [blame] | 909 | void __user *argp = (void __user *)arg; |
Carsten Otte | 313a3dc | 2007-10-11 19:16:52 +0200 | [diff] [blame] | 910 | int r; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 911 | |
Avi Kivity | 6d4e4c4 | 2007-11-21 16:41:05 +0200 | [diff] [blame] | 912 | if (vcpu->kvm->mm != current->mm) |
| 913 | return -EIO; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 914 | switch (ioctl) { |
Avi Kivity | 9a2bb7f | 2007-02-22 12:58:31 +0200 | [diff] [blame] | 915 | case KVM_RUN: |
Avi Kivity | f0fe510 | 2007-03-07 13:11:17 +0200 | [diff] [blame] | 916 | r = -EINVAL; |
| 917 | if (arg) |
| 918 | goto out; |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 919 | r = kvm_arch_vcpu_ioctl_run(vcpu, vcpu->run); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 920 | break; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 921 | case KVM_GET_REGS: { |
Xiantao Zhang | 3e4bb3a | 2008-02-25 18:52:20 +0800 | [diff] [blame] | 922 | struct kvm_regs *kvm_regs; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 923 | |
Xiantao Zhang | 3e4bb3a | 2008-02-25 18:52:20 +0800 | [diff] [blame] | 924 | r = -ENOMEM; |
| 925 | kvm_regs = kzalloc(sizeof(struct kvm_regs), GFP_KERNEL); |
| 926 | if (!kvm_regs) |
| 927 | goto out; |
| 928 | r = kvm_arch_vcpu_ioctl_get_regs(vcpu, kvm_regs); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 929 | if (r) |
Xiantao Zhang | 3e4bb3a | 2008-02-25 18:52:20 +0800 | [diff] [blame] | 930 | goto out_free1; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 931 | r = -EFAULT; |
Xiantao Zhang | 3e4bb3a | 2008-02-25 18:52:20 +0800 | [diff] [blame] | 932 | if (copy_to_user(argp, kvm_regs, sizeof(struct kvm_regs))) |
| 933 | goto out_free1; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 934 | r = 0; |
Xiantao Zhang | 3e4bb3a | 2008-02-25 18:52:20 +0800 | [diff] [blame] | 935 | out_free1: |
| 936 | kfree(kvm_regs); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 937 | break; |
| 938 | } |
| 939 | case KVM_SET_REGS: { |
Xiantao Zhang | 3e4bb3a | 2008-02-25 18:52:20 +0800 | [diff] [blame] | 940 | struct kvm_regs *kvm_regs; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 941 | |
Xiantao Zhang | 3e4bb3a | 2008-02-25 18:52:20 +0800 | [diff] [blame] | 942 | r = -ENOMEM; |
| 943 | kvm_regs = kzalloc(sizeof(struct kvm_regs), GFP_KERNEL); |
| 944 | if (!kvm_regs) |
| 945 | goto out; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 946 | r = -EFAULT; |
Xiantao Zhang | 3e4bb3a | 2008-02-25 18:52:20 +0800 | [diff] [blame] | 947 | if (copy_from_user(kvm_regs, argp, sizeof(struct kvm_regs))) |
| 948 | goto out_free2; |
| 949 | r = kvm_arch_vcpu_ioctl_set_regs(vcpu, kvm_regs); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 950 | if (r) |
Xiantao Zhang | 3e4bb3a | 2008-02-25 18:52:20 +0800 | [diff] [blame] | 951 | goto out_free2; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 952 | r = 0; |
Xiantao Zhang | 3e4bb3a | 2008-02-25 18:52:20 +0800 | [diff] [blame] | 953 | out_free2: |
| 954 | kfree(kvm_regs); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 955 | break; |
| 956 | } |
| 957 | case KVM_GET_SREGS: { |
| 958 | struct kvm_sregs kvm_sregs; |
| 959 | |
Avi Kivity | bccf215 | 2007-02-21 18:04:26 +0200 | [diff] [blame] | 960 | memset(&kvm_sregs, 0, sizeof kvm_sregs); |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 961 | r = kvm_arch_vcpu_ioctl_get_sregs(vcpu, &kvm_sregs); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 962 | if (r) |
| 963 | goto out; |
| 964 | r = -EFAULT; |
Al Viro | 2f366987 | 2007-02-09 16:38:35 +0000 | [diff] [blame] | 965 | if (copy_to_user(argp, &kvm_sregs, sizeof kvm_sregs)) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 966 | goto out; |
| 967 | r = 0; |
| 968 | break; |
| 969 | } |
| 970 | case KVM_SET_SREGS: { |
| 971 | struct kvm_sregs kvm_sregs; |
| 972 | |
| 973 | r = -EFAULT; |
Al Viro | 2f366987 | 2007-02-09 16:38:35 +0000 | [diff] [blame] | 974 | if (copy_from_user(&kvm_sregs, argp, sizeof kvm_sregs)) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 975 | goto out; |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 976 | r = kvm_arch_vcpu_ioctl_set_sregs(vcpu, &kvm_sregs); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 977 | if (r) |
| 978 | goto out; |
| 979 | r = 0; |
| 980 | break; |
| 981 | } |
| 982 | case KVM_TRANSLATE: { |
| 983 | struct kvm_translation tr; |
| 984 | |
| 985 | r = -EFAULT; |
Al Viro | 2f366987 | 2007-02-09 16:38:35 +0000 | [diff] [blame] | 986 | if (copy_from_user(&tr, argp, sizeof tr)) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 987 | goto out; |
Zhang Xiantao | 8b00679 | 2007-11-16 13:05:55 +0800 | [diff] [blame] | 988 | r = kvm_arch_vcpu_ioctl_translate(vcpu, &tr); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 989 | if (r) |
| 990 | goto out; |
| 991 | r = -EFAULT; |
Al Viro | 2f366987 | 2007-02-09 16:38:35 +0000 | [diff] [blame] | 992 | if (copy_to_user(argp, &tr, sizeof tr)) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 993 | goto out; |
| 994 | r = 0; |
| 995 | break; |
| 996 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 997 | case KVM_DEBUG_GUEST: { |
| 998 | struct kvm_debug_guest dbg; |
| 999 | |
| 1000 | r = -EFAULT; |
Al Viro | 2f366987 | 2007-02-09 16:38:35 +0000 | [diff] [blame] | 1001 | if (copy_from_user(&dbg, argp, sizeof dbg)) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1002 | goto out; |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 1003 | r = kvm_arch_vcpu_ioctl_debug_guest(vcpu, &dbg); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1004 | if (r) |
| 1005 | goto out; |
| 1006 | r = 0; |
| 1007 | break; |
| 1008 | } |
Avi Kivity | 1961d27 | 2007-03-05 19:46:05 +0200 | [diff] [blame] | 1009 | case KVM_SET_SIGNAL_MASK: { |
| 1010 | struct kvm_signal_mask __user *sigmask_arg = argp; |
| 1011 | struct kvm_signal_mask kvm_sigmask; |
| 1012 | sigset_t sigset, *p; |
| 1013 | |
| 1014 | p = NULL; |
| 1015 | if (argp) { |
| 1016 | r = -EFAULT; |
| 1017 | if (copy_from_user(&kvm_sigmask, argp, |
| 1018 | sizeof kvm_sigmask)) |
| 1019 | goto out; |
| 1020 | r = -EINVAL; |
| 1021 | if (kvm_sigmask.len != sizeof sigset) |
| 1022 | goto out; |
| 1023 | r = -EFAULT; |
| 1024 | if (copy_from_user(&sigset, sigmask_arg->sigset, |
| 1025 | sizeof sigset)) |
| 1026 | goto out; |
| 1027 | p = &sigset; |
| 1028 | } |
| 1029 | r = kvm_vcpu_ioctl_set_sigmask(vcpu, &sigset); |
| 1030 | break; |
| 1031 | } |
Avi Kivity | b883673 | 2007-04-01 16:34:31 +0300 | [diff] [blame] | 1032 | case KVM_GET_FPU: { |
| 1033 | struct kvm_fpu fpu; |
| 1034 | |
| 1035 | memset(&fpu, 0, sizeof fpu); |
Hollis Blanchard | d075206 | 2007-10-31 17:24:25 -0500 | [diff] [blame] | 1036 | r = kvm_arch_vcpu_ioctl_get_fpu(vcpu, &fpu); |
Avi Kivity | b883673 | 2007-04-01 16:34:31 +0300 | [diff] [blame] | 1037 | if (r) |
| 1038 | goto out; |
| 1039 | r = -EFAULT; |
| 1040 | if (copy_to_user(argp, &fpu, sizeof fpu)) |
| 1041 | goto out; |
| 1042 | r = 0; |
| 1043 | break; |
| 1044 | } |
| 1045 | case KVM_SET_FPU: { |
| 1046 | struct kvm_fpu fpu; |
| 1047 | |
| 1048 | r = -EFAULT; |
| 1049 | if (copy_from_user(&fpu, argp, sizeof fpu)) |
| 1050 | goto out; |
Hollis Blanchard | d075206 | 2007-10-31 17:24:25 -0500 | [diff] [blame] | 1051 | r = kvm_arch_vcpu_ioctl_set_fpu(vcpu, &fpu); |
Avi Kivity | b883673 | 2007-04-01 16:34:31 +0300 | [diff] [blame] | 1052 | if (r) |
| 1053 | goto out; |
| 1054 | r = 0; |
| 1055 | break; |
| 1056 | } |
Avi Kivity | bccf215 | 2007-02-21 18:04:26 +0200 | [diff] [blame] | 1057 | default: |
Carsten Otte | 313a3dc | 2007-10-11 19:16:52 +0200 | [diff] [blame] | 1058 | r = kvm_arch_vcpu_ioctl(filp, ioctl, arg); |
Avi Kivity | bccf215 | 2007-02-21 18:04:26 +0200 | [diff] [blame] | 1059 | } |
| 1060 | out: |
| 1061 | return r; |
| 1062 | } |
| 1063 | |
| 1064 | static long kvm_vm_ioctl(struct file *filp, |
| 1065 | unsigned int ioctl, unsigned long arg) |
| 1066 | { |
| 1067 | struct kvm *kvm = filp->private_data; |
| 1068 | void __user *argp = (void __user *)arg; |
Carsten Otte | 1fe779f | 2007-10-29 16:08:35 +0100 | [diff] [blame] | 1069 | int r; |
Avi Kivity | bccf215 | 2007-02-21 18:04:26 +0200 | [diff] [blame] | 1070 | |
Avi Kivity | 6d4e4c4 | 2007-11-21 16:41:05 +0200 | [diff] [blame] | 1071 | if (kvm->mm != current->mm) |
| 1072 | return -EIO; |
Avi Kivity | bccf215 | 2007-02-21 18:04:26 +0200 | [diff] [blame] | 1073 | switch (ioctl) { |
| 1074 | case KVM_CREATE_VCPU: |
| 1075 | r = kvm_vm_ioctl_create_vcpu(kvm, arg); |
| 1076 | if (r < 0) |
| 1077 | goto out; |
| 1078 | break; |
Izik Eidus | 6fc138d | 2007-10-09 19:20:39 +0200 | [diff] [blame] | 1079 | case KVM_SET_USER_MEMORY_REGION: { |
| 1080 | struct kvm_userspace_memory_region kvm_userspace_mem; |
| 1081 | |
| 1082 | r = -EFAULT; |
| 1083 | if (copy_from_user(&kvm_userspace_mem, argp, |
| 1084 | sizeof kvm_userspace_mem)) |
| 1085 | goto out; |
| 1086 | |
| 1087 | r = kvm_vm_ioctl_set_memory_region(kvm, &kvm_userspace_mem, 1); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1088 | if (r) |
| 1089 | goto out; |
| 1090 | break; |
| 1091 | } |
| 1092 | case KVM_GET_DIRTY_LOG: { |
| 1093 | struct kvm_dirty_log log; |
| 1094 | |
| 1095 | r = -EFAULT; |
Al Viro | 2f366987 | 2007-02-09 16:38:35 +0000 | [diff] [blame] | 1096 | if (copy_from_user(&log, argp, sizeof log)) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1097 | goto out; |
Avi Kivity | 2c6f5df | 2007-02-20 18:27:58 +0200 | [diff] [blame] | 1098 | r = kvm_vm_ioctl_get_dirty_log(kvm, &log); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1099 | if (r) |
| 1100 | goto out; |
| 1101 | break; |
| 1102 | } |
Avi Kivity | f17abe9 | 2007-02-21 19:28:04 +0200 | [diff] [blame] | 1103 | default: |
Carsten Otte | 1fe779f | 2007-10-29 16:08:35 +0100 | [diff] [blame] | 1104 | r = kvm_arch_vm_ioctl(filp, ioctl, arg); |
Avi Kivity | f17abe9 | 2007-02-21 19:28:04 +0200 | [diff] [blame] | 1105 | } |
| 1106 | out: |
| 1107 | return r; |
| 1108 | } |
| 1109 | |
npiggin@suse.de | e4a533a | 2007-12-05 18:15:52 +1100 | [diff] [blame] | 1110 | 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] | 1111 | { |
| 1112 | struct kvm *kvm = vma->vm_file->private_data; |
Avi Kivity | f17abe9 | 2007-02-21 19:28:04 +0200 | [diff] [blame] | 1113 | struct page *page; |
| 1114 | |
npiggin@suse.de | e4a533a | 2007-12-05 18:15:52 +1100 | [diff] [blame] | 1115 | if (!kvm_is_visible_gfn(kvm, vmf->pgoff)) |
| 1116 | return VM_FAULT_SIGBUS; |
Marcelo Tosatti | 10589a4 | 2007-12-20 19:18:22 -0500 | [diff] [blame] | 1117 | page = gfn_to_page(kvm, vmf->pgoff); |
Izik Eidus | 8a7ae05 | 2007-10-18 11:09:33 +0200 | [diff] [blame] | 1118 | if (is_error_page(page)) { |
Izik Eidus | b4231d6 | 2007-11-20 11:49:33 +0200 | [diff] [blame] | 1119 | kvm_release_page_clean(page); |
npiggin@suse.de | e4a533a | 2007-12-05 18:15:52 +1100 | [diff] [blame] | 1120 | return VM_FAULT_SIGBUS; |
Izik Eidus | 8a7ae05 | 2007-10-18 11:09:33 +0200 | [diff] [blame] | 1121 | } |
npiggin@suse.de | e4a533a | 2007-12-05 18:15:52 +1100 | [diff] [blame] | 1122 | vmf->page = page; |
| 1123 | return 0; |
Avi Kivity | f17abe9 | 2007-02-21 19:28:04 +0200 | [diff] [blame] | 1124 | } |
| 1125 | |
| 1126 | static struct vm_operations_struct kvm_vm_vm_ops = { |
npiggin@suse.de | e4a533a | 2007-12-05 18:15:52 +1100 | [diff] [blame] | 1127 | .fault = kvm_vm_fault, |
Avi Kivity | f17abe9 | 2007-02-21 19:28:04 +0200 | [diff] [blame] | 1128 | }; |
| 1129 | |
| 1130 | static int kvm_vm_mmap(struct file *file, struct vm_area_struct *vma) |
| 1131 | { |
| 1132 | vma->vm_ops = &kvm_vm_vm_ops; |
| 1133 | return 0; |
| 1134 | } |
| 1135 | |
Jan Engelhardt | 5c50274 | 2008-01-22 20:46:14 +0100 | [diff] [blame] | 1136 | static const struct file_operations kvm_vm_fops = { |
Avi Kivity | f17abe9 | 2007-02-21 19:28:04 +0200 | [diff] [blame] | 1137 | .release = kvm_vm_release, |
| 1138 | .unlocked_ioctl = kvm_vm_ioctl, |
| 1139 | .compat_ioctl = kvm_vm_ioctl, |
| 1140 | .mmap = kvm_vm_mmap, |
| 1141 | }; |
| 1142 | |
| 1143 | static int kvm_dev_ioctl_create_vm(void) |
| 1144 | { |
| 1145 | int fd, r; |
| 1146 | struct inode *inode; |
| 1147 | struct file *file; |
| 1148 | struct kvm *kvm; |
| 1149 | |
Avi Kivity | f17abe9 | 2007-02-21 19:28:04 +0200 | [diff] [blame] | 1150 | kvm = kvm_create_vm(); |
Avi Kivity | d6d2816 | 2007-06-28 08:38:16 -0400 | [diff] [blame] | 1151 | if (IS_ERR(kvm)) |
| 1152 | return PTR_ERR(kvm); |
| 1153 | r = anon_inode_getfd(&fd, &inode, &file, "kvm-vm", &kvm_vm_fops, kvm); |
| 1154 | if (r) { |
| 1155 | kvm_destroy_vm(kvm); |
| 1156 | return r; |
Avi Kivity | f17abe9 | 2007-02-21 19:28:04 +0200 | [diff] [blame] | 1157 | } |
| 1158 | |
Avi Kivity | bccf215 | 2007-02-21 18:04:26 +0200 | [diff] [blame] | 1159 | kvm->filp = file; |
Avi Kivity | f17abe9 | 2007-02-21 19:28:04 +0200 | [diff] [blame] | 1160 | |
Avi Kivity | f17abe9 | 2007-02-21 19:28:04 +0200 | [diff] [blame] | 1161 | return fd; |
Avi Kivity | f17abe9 | 2007-02-21 19:28:04 +0200 | [diff] [blame] | 1162 | } |
| 1163 | |
| 1164 | static long kvm_dev_ioctl(struct file *filp, |
| 1165 | unsigned int ioctl, unsigned long arg) |
| 1166 | { |
| 1167 | void __user *argp = (void __user *)arg; |
Avi Kivity | 07c45a3 | 2007-03-07 13:05:38 +0200 | [diff] [blame] | 1168 | long r = -EINVAL; |
Avi Kivity | f17abe9 | 2007-02-21 19:28:04 +0200 | [diff] [blame] | 1169 | |
| 1170 | switch (ioctl) { |
| 1171 | case KVM_GET_API_VERSION: |
Avi Kivity | f0fe510 | 2007-03-07 13:11:17 +0200 | [diff] [blame] | 1172 | r = -EINVAL; |
| 1173 | if (arg) |
| 1174 | goto out; |
Avi Kivity | f17abe9 | 2007-02-21 19:28:04 +0200 | [diff] [blame] | 1175 | r = KVM_API_VERSION; |
| 1176 | break; |
| 1177 | case KVM_CREATE_VM: |
Avi Kivity | f0fe510 | 2007-03-07 13:11:17 +0200 | [diff] [blame] | 1178 | r = -EINVAL; |
| 1179 | if (arg) |
| 1180 | goto out; |
Avi Kivity | f17abe9 | 2007-02-21 19:28:04 +0200 | [diff] [blame] | 1181 | r = kvm_dev_ioctl_create_vm(); |
| 1182 | break; |
Zhang Xiantao | 018d00d | 2007-11-15 23:07:47 +0800 | [diff] [blame] | 1183 | case KVM_CHECK_EXTENSION: |
| 1184 | r = kvm_dev_ioctl_check_extension((long)argp); |
Avi Kivity | 5d308f4 | 2007-03-01 17:56:20 +0200 | [diff] [blame] | 1185 | break; |
Avi Kivity | 07c45a3 | 2007-03-07 13:05:38 +0200 | [diff] [blame] | 1186 | case KVM_GET_VCPU_MMAP_SIZE: |
| 1187 | r = -EINVAL; |
| 1188 | if (arg) |
| 1189 | goto out; |
Avi Kivity | adb1ff4 | 2008-01-24 15:13:08 +0200 | [diff] [blame] | 1190 | r = PAGE_SIZE; /* struct kvm_run */ |
| 1191 | #ifdef CONFIG_X86 |
| 1192 | r += PAGE_SIZE; /* pio data page */ |
| 1193 | #endif |
Avi Kivity | 07c45a3 | 2007-03-07 13:05:38 +0200 | [diff] [blame] | 1194 | break; |
Feng(Eric) Liu | d4c9ff2 | 2008-04-10 08:47:53 -0400 | [diff] [blame] | 1195 | case KVM_TRACE_ENABLE: |
| 1196 | case KVM_TRACE_PAUSE: |
| 1197 | case KVM_TRACE_DISABLE: |
| 1198 | r = kvm_trace_ioctl(ioctl, arg); |
| 1199 | break; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1200 | default: |
Carsten Otte | 043405e | 2007-10-10 17:16:19 +0200 | [diff] [blame] | 1201 | return kvm_arch_dev_ioctl(filp, ioctl, arg); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1202 | } |
| 1203 | out: |
| 1204 | return r; |
| 1205 | } |
| 1206 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1207 | static struct file_operations kvm_chardev_ops = { |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1208 | .unlocked_ioctl = kvm_dev_ioctl, |
| 1209 | .compat_ioctl = kvm_dev_ioctl, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1210 | }; |
| 1211 | |
| 1212 | static struct miscdevice kvm_dev = { |
Avi Kivity | bbe4432 | 2007-03-04 13:27:36 +0200 | [diff] [blame] | 1213 | KVM_MINOR, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1214 | "kvm", |
| 1215 | &kvm_chardev_ops, |
| 1216 | }; |
| 1217 | |
Avi Kivity | 1b6c016 | 2007-05-24 13:03:52 +0300 | [diff] [blame] | 1218 | static void hardware_enable(void *junk) |
| 1219 | { |
| 1220 | int cpu = raw_smp_processor_id(); |
| 1221 | |
| 1222 | if (cpu_isset(cpu, cpus_hardware_enabled)) |
| 1223 | return; |
| 1224 | cpu_set(cpu, cpus_hardware_enabled); |
Zhang Xiantao | e9b11c1 | 2007-11-14 20:38:21 +0800 | [diff] [blame] | 1225 | kvm_arch_hardware_enable(NULL); |
Avi Kivity | 1b6c016 | 2007-05-24 13:03:52 +0300 | [diff] [blame] | 1226 | } |
| 1227 | |
| 1228 | static void hardware_disable(void *junk) |
| 1229 | { |
| 1230 | int cpu = raw_smp_processor_id(); |
| 1231 | |
| 1232 | if (!cpu_isset(cpu, cpus_hardware_enabled)) |
| 1233 | return; |
| 1234 | cpu_clear(cpu, cpus_hardware_enabled); |
| 1235 | decache_vcpus_on_cpu(cpu); |
Zhang Xiantao | e9b11c1 | 2007-11-14 20:38:21 +0800 | [diff] [blame] | 1236 | kvm_arch_hardware_disable(NULL); |
Avi Kivity | 1b6c016 | 2007-05-24 13:03:52 +0300 | [diff] [blame] | 1237 | } |
| 1238 | |
Avi Kivity | 774c47f | 2007-02-12 00:54:47 -0800 | [diff] [blame] | 1239 | static int kvm_cpu_hotplug(struct notifier_block *notifier, unsigned long val, |
| 1240 | void *v) |
| 1241 | { |
| 1242 | int cpu = (long)v; |
| 1243 | |
Avi Kivity | 1a6f4d7 | 2007-11-11 18:37:32 +0200 | [diff] [blame] | 1244 | val &= ~CPU_TASKS_FROZEN; |
Avi Kivity | 774c47f | 2007-02-12 00:54:47 -0800 | [diff] [blame] | 1245 | switch (val) { |
Avi Kivity | cec9ad2 | 2007-05-24 13:11:41 +0300 | [diff] [blame] | 1246 | case CPU_DYING: |
Avi Kivity | 6ec8a856 | 2007-08-19 15:57:26 +0300 | [diff] [blame] | 1247 | printk(KERN_INFO "kvm: disabling virtualization on CPU%d\n", |
| 1248 | cpu); |
| 1249 | hardware_disable(NULL); |
| 1250 | break; |
Avi Kivity | 774c47f | 2007-02-12 00:54:47 -0800 | [diff] [blame] | 1251 | case CPU_UP_CANCELED: |
Jeremy Katz | 43934a3 | 2007-02-19 14:37:46 +0200 | [diff] [blame] | 1252 | printk(KERN_INFO "kvm: disabling virtualization on CPU%d\n", |
| 1253 | cpu); |
Avi Kivity | 1b6c016 | 2007-05-24 13:03:52 +0300 | [diff] [blame] | 1254 | smp_call_function_single(cpu, hardware_disable, NULL, 0, 1); |
Avi Kivity | 774c47f | 2007-02-12 00:54:47 -0800 | [diff] [blame] | 1255 | break; |
Jeremy Katz | 43934a3 | 2007-02-19 14:37:46 +0200 | [diff] [blame] | 1256 | case CPU_ONLINE: |
| 1257 | printk(KERN_INFO "kvm: enabling virtualization on CPU%d\n", |
| 1258 | cpu); |
Avi Kivity | 1b6c016 | 2007-05-24 13:03:52 +0300 | [diff] [blame] | 1259 | smp_call_function_single(cpu, hardware_enable, NULL, 0, 1); |
Avi Kivity | 774c47f | 2007-02-12 00:54:47 -0800 | [diff] [blame] | 1260 | break; |
| 1261 | } |
| 1262 | return NOTIFY_OK; |
| 1263 | } |
| 1264 | |
Rusty Russell | 9a2b85c | 2007-07-17 23:17:55 +1000 | [diff] [blame] | 1265 | static int kvm_reboot(struct notifier_block *notifier, unsigned long val, |
Mike Day | d77c26f | 2007-10-08 09:02:08 -0400 | [diff] [blame] | 1266 | void *v) |
Rusty Russell | 9a2b85c | 2007-07-17 23:17:55 +1000 | [diff] [blame] | 1267 | { |
| 1268 | if (val == SYS_RESTART) { |
| 1269 | /* |
| 1270 | * Some (well, at least mine) BIOSes hang on reboot if |
| 1271 | * in vmx root mode. |
| 1272 | */ |
| 1273 | printk(KERN_INFO "kvm: exiting hardware virtualization\n"); |
| 1274 | on_each_cpu(hardware_disable, NULL, 0, 1); |
| 1275 | } |
| 1276 | return NOTIFY_OK; |
| 1277 | } |
| 1278 | |
| 1279 | static struct notifier_block kvm_reboot_notifier = { |
| 1280 | .notifier_call = kvm_reboot, |
| 1281 | .priority = 0, |
| 1282 | }; |
| 1283 | |
Gregory Haskins | 2eeb2e9 | 2007-05-31 14:08:53 -0400 | [diff] [blame] | 1284 | void kvm_io_bus_init(struct kvm_io_bus *bus) |
| 1285 | { |
| 1286 | memset(bus, 0, sizeof(*bus)); |
| 1287 | } |
| 1288 | |
| 1289 | void kvm_io_bus_destroy(struct kvm_io_bus *bus) |
| 1290 | { |
| 1291 | int i; |
| 1292 | |
| 1293 | for (i = 0; i < bus->dev_count; i++) { |
| 1294 | struct kvm_io_device *pos = bus->devs[i]; |
| 1295 | |
| 1296 | kvm_iodevice_destructor(pos); |
| 1297 | } |
| 1298 | } |
| 1299 | |
| 1300 | struct kvm_io_device *kvm_io_bus_find_dev(struct kvm_io_bus *bus, gpa_t addr) |
| 1301 | { |
| 1302 | int i; |
| 1303 | |
| 1304 | for (i = 0; i < bus->dev_count; i++) { |
| 1305 | struct kvm_io_device *pos = bus->devs[i]; |
| 1306 | |
| 1307 | if (pos->in_range(pos, addr)) |
| 1308 | return pos; |
| 1309 | } |
| 1310 | |
| 1311 | return NULL; |
| 1312 | } |
| 1313 | |
| 1314 | void kvm_io_bus_register_dev(struct kvm_io_bus *bus, struct kvm_io_device *dev) |
| 1315 | { |
| 1316 | BUG_ON(bus->dev_count > (NR_IOBUS_DEVS-1)); |
| 1317 | |
| 1318 | bus->devs[bus->dev_count++] = dev; |
| 1319 | } |
| 1320 | |
Avi Kivity | 774c47f | 2007-02-12 00:54:47 -0800 | [diff] [blame] | 1321 | static struct notifier_block kvm_cpu_notifier = { |
| 1322 | .notifier_call = kvm_cpu_hotplug, |
| 1323 | .priority = 20, /* must be > scheduler priority */ |
| 1324 | }; |
| 1325 | |
Christoph Hellwig | 8b88b09 | 2008-02-08 04:20:26 -0800 | [diff] [blame] | 1326 | static int vm_stat_get(void *_offset, u64 *val) |
Avi Kivity | ba1389b | 2007-11-18 16:24:12 +0200 | [diff] [blame] | 1327 | { |
| 1328 | unsigned offset = (long)_offset; |
Avi Kivity | ba1389b | 2007-11-18 16:24:12 +0200 | [diff] [blame] | 1329 | struct kvm *kvm; |
| 1330 | |
Christoph Hellwig | 8b88b09 | 2008-02-08 04:20:26 -0800 | [diff] [blame] | 1331 | *val = 0; |
Avi Kivity | ba1389b | 2007-11-18 16:24:12 +0200 | [diff] [blame] | 1332 | spin_lock(&kvm_lock); |
| 1333 | list_for_each_entry(kvm, &vm_list, vm_list) |
Christoph Hellwig | 8b88b09 | 2008-02-08 04:20:26 -0800 | [diff] [blame] | 1334 | *val += *(u32 *)((void *)kvm + offset); |
Avi Kivity | ba1389b | 2007-11-18 16:24:12 +0200 | [diff] [blame] | 1335 | spin_unlock(&kvm_lock); |
Christoph Hellwig | 8b88b09 | 2008-02-08 04:20:26 -0800 | [diff] [blame] | 1336 | return 0; |
Avi Kivity | ba1389b | 2007-11-18 16:24:12 +0200 | [diff] [blame] | 1337 | } |
| 1338 | |
| 1339 | DEFINE_SIMPLE_ATTRIBUTE(vm_stat_fops, vm_stat_get, NULL, "%llu\n"); |
| 1340 | |
Christoph Hellwig | 8b88b09 | 2008-02-08 04:20:26 -0800 | [diff] [blame] | 1341 | static int vcpu_stat_get(void *_offset, u64 *val) |
Avi Kivity | 1165f5f | 2007-04-19 17:27:43 +0300 | [diff] [blame] | 1342 | { |
| 1343 | unsigned offset = (long)_offset; |
Avi Kivity | 1165f5f | 2007-04-19 17:27:43 +0300 | [diff] [blame] | 1344 | struct kvm *kvm; |
| 1345 | struct kvm_vcpu *vcpu; |
| 1346 | int i; |
| 1347 | |
Christoph Hellwig | 8b88b09 | 2008-02-08 04:20:26 -0800 | [diff] [blame] | 1348 | *val = 0; |
Avi Kivity | 1165f5f | 2007-04-19 17:27:43 +0300 | [diff] [blame] | 1349 | spin_lock(&kvm_lock); |
| 1350 | list_for_each_entry(kvm, &vm_list, vm_list) |
| 1351 | for (i = 0; i < KVM_MAX_VCPUS; ++i) { |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 1352 | vcpu = kvm->vcpus[i]; |
| 1353 | if (vcpu) |
Christoph Hellwig | 8b88b09 | 2008-02-08 04:20:26 -0800 | [diff] [blame] | 1354 | *val += *(u32 *)((void *)vcpu + offset); |
Avi Kivity | 1165f5f | 2007-04-19 17:27:43 +0300 | [diff] [blame] | 1355 | } |
| 1356 | spin_unlock(&kvm_lock); |
Christoph Hellwig | 8b88b09 | 2008-02-08 04:20:26 -0800 | [diff] [blame] | 1357 | return 0; |
Avi Kivity | 1165f5f | 2007-04-19 17:27:43 +0300 | [diff] [blame] | 1358 | } |
| 1359 | |
Avi Kivity | ba1389b | 2007-11-18 16:24:12 +0200 | [diff] [blame] | 1360 | DEFINE_SIMPLE_ATTRIBUTE(vcpu_stat_fops, vcpu_stat_get, NULL, "%llu\n"); |
| 1361 | |
| 1362 | static struct file_operations *stat_fops[] = { |
| 1363 | [KVM_STAT_VCPU] = &vcpu_stat_fops, |
| 1364 | [KVM_STAT_VM] = &vm_stat_fops, |
| 1365 | }; |
Avi Kivity | 1165f5f | 2007-04-19 17:27:43 +0300 | [diff] [blame] | 1366 | |
Zhang Xiantao | a16b043 | 2007-11-16 14:38:21 +0800 | [diff] [blame] | 1367 | static void kvm_init_debug(void) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1368 | { |
| 1369 | struct kvm_stats_debugfs_item *p; |
| 1370 | |
Al Viro | 8b6d44c | 2007-02-09 16:38:40 +0000 | [diff] [blame] | 1371 | debugfs_dir = debugfs_create_dir("kvm", NULL); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1372 | for (p = debugfs_entries; p->name; ++p) |
Avi Kivity | 1165f5f | 2007-04-19 17:27:43 +0300 | [diff] [blame] | 1373 | p->dentry = debugfs_create_file(p->name, 0444, debugfs_dir, |
| 1374 | (void *)(long)p->offset, |
Avi Kivity | ba1389b | 2007-11-18 16:24:12 +0200 | [diff] [blame] | 1375 | stat_fops[p->kind]); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1376 | } |
| 1377 | |
| 1378 | static void kvm_exit_debug(void) |
| 1379 | { |
| 1380 | struct kvm_stats_debugfs_item *p; |
| 1381 | |
| 1382 | for (p = debugfs_entries; p->name; ++p) |
| 1383 | debugfs_remove(p->dentry); |
| 1384 | debugfs_remove(debugfs_dir); |
| 1385 | } |
| 1386 | |
Avi Kivity | 59ae6c6 | 2007-02-12 00:54:48 -0800 | [diff] [blame] | 1387 | static int kvm_suspend(struct sys_device *dev, pm_message_t state) |
| 1388 | { |
Avi Kivity | 4267c41 | 2007-05-24 13:09:41 +0300 | [diff] [blame] | 1389 | hardware_disable(NULL); |
Avi Kivity | 59ae6c6 | 2007-02-12 00:54:48 -0800 | [diff] [blame] | 1390 | return 0; |
| 1391 | } |
| 1392 | |
| 1393 | static int kvm_resume(struct sys_device *dev) |
| 1394 | { |
Avi Kivity | 4267c41 | 2007-05-24 13:09:41 +0300 | [diff] [blame] | 1395 | hardware_enable(NULL); |
Avi Kivity | 59ae6c6 | 2007-02-12 00:54:48 -0800 | [diff] [blame] | 1396 | return 0; |
| 1397 | } |
| 1398 | |
| 1399 | static struct sysdev_class kvm_sysdev_class = { |
Kay Sievers | af5ca3f | 2007-12-20 02:09:39 +0100 | [diff] [blame] | 1400 | .name = "kvm", |
Avi Kivity | 59ae6c6 | 2007-02-12 00:54:48 -0800 | [diff] [blame] | 1401 | .suspend = kvm_suspend, |
| 1402 | .resume = kvm_resume, |
| 1403 | }; |
| 1404 | |
| 1405 | static struct sys_device kvm_sysdev = { |
| 1406 | .id = 0, |
| 1407 | .cls = &kvm_sysdev_class, |
| 1408 | }; |
| 1409 | |
Izik Eidus | cea7bb2 | 2007-10-17 19:17:48 +0200 | [diff] [blame] | 1410 | struct page *bad_page; |
Anthony Liguori | 35149e2 | 2008-04-02 14:46:56 -0500 | [diff] [blame] | 1411 | pfn_t bad_pfn; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1412 | |
Avi Kivity | 15ad714 | 2007-07-11 18:17:21 +0300 | [diff] [blame] | 1413 | static inline |
| 1414 | struct kvm_vcpu *preempt_notifier_to_vcpu(struct preempt_notifier *pn) |
| 1415 | { |
| 1416 | return container_of(pn, struct kvm_vcpu, preempt_notifier); |
| 1417 | } |
| 1418 | |
| 1419 | static void kvm_sched_in(struct preempt_notifier *pn, int cpu) |
| 1420 | { |
| 1421 | struct kvm_vcpu *vcpu = preempt_notifier_to_vcpu(pn); |
| 1422 | |
Zhang Xiantao | e9b11c1 | 2007-11-14 20:38:21 +0800 | [diff] [blame] | 1423 | kvm_arch_vcpu_load(vcpu, cpu); |
Avi Kivity | 15ad714 | 2007-07-11 18:17:21 +0300 | [diff] [blame] | 1424 | } |
| 1425 | |
| 1426 | static void kvm_sched_out(struct preempt_notifier *pn, |
| 1427 | struct task_struct *next) |
| 1428 | { |
| 1429 | struct kvm_vcpu *vcpu = preempt_notifier_to_vcpu(pn); |
| 1430 | |
Zhang Xiantao | e9b11c1 | 2007-11-14 20:38:21 +0800 | [diff] [blame] | 1431 | kvm_arch_vcpu_put(vcpu); |
Avi Kivity | 15ad714 | 2007-07-11 18:17:21 +0300 | [diff] [blame] | 1432 | } |
| 1433 | |
Zhang Xiantao | f8c16bb | 2007-11-14 20:40:21 +0800 | [diff] [blame] | 1434 | int kvm_init(void *opaque, unsigned int vcpu_size, |
Rusty Russell | c16f862 | 2007-07-30 21:12:19 +1000 | [diff] [blame] | 1435 | struct module *module) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1436 | { |
| 1437 | int r; |
Yang, Sheng | 002c7f7 | 2007-07-31 14:23:01 +0300 | [diff] [blame] | 1438 | int cpu; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1439 | |
Zhang Xiantao | cb498ea | 2007-11-14 20:39:31 +0800 | [diff] [blame] | 1440 | kvm_init_debug(); |
| 1441 | |
Zhang Xiantao | f8c16bb | 2007-11-14 20:40:21 +0800 | [diff] [blame] | 1442 | r = kvm_arch_init(opaque); |
| 1443 | if (r) |
Zhang Xiantao | d2308784 | 2007-11-29 15:35:39 +0800 | [diff] [blame] | 1444 | goto out_fail; |
Zhang Xiantao | cb498ea | 2007-11-14 20:39:31 +0800 | [diff] [blame] | 1445 | |
| 1446 | bad_page = alloc_page(GFP_KERNEL | __GFP_ZERO); |
| 1447 | |
| 1448 | if (bad_page == NULL) { |
| 1449 | r = -ENOMEM; |
| 1450 | goto out; |
| 1451 | } |
| 1452 | |
Anthony Liguori | 35149e2 | 2008-04-02 14:46:56 -0500 | [diff] [blame] | 1453 | bad_pfn = page_to_pfn(bad_page); |
| 1454 | |
Zhang Xiantao | e9b11c1 | 2007-11-14 20:38:21 +0800 | [diff] [blame] | 1455 | r = kvm_arch_hardware_setup(); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1456 | if (r < 0) |
Zhang Xiantao | d2308784 | 2007-11-29 15:35:39 +0800 | [diff] [blame] | 1457 | goto out_free_0; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1458 | |
Yang, Sheng | 002c7f7 | 2007-07-31 14:23:01 +0300 | [diff] [blame] | 1459 | for_each_online_cpu(cpu) { |
| 1460 | smp_call_function_single(cpu, |
Zhang Xiantao | e9b11c1 | 2007-11-14 20:38:21 +0800 | [diff] [blame] | 1461 | kvm_arch_check_processor_compat, |
Yang, Sheng | 002c7f7 | 2007-07-31 14:23:01 +0300 | [diff] [blame] | 1462 | &r, 0, 1); |
| 1463 | if (r < 0) |
Zhang Xiantao | d2308784 | 2007-11-29 15:35:39 +0800 | [diff] [blame] | 1464 | goto out_free_1; |
Yang, Sheng | 002c7f7 | 2007-07-31 14:23:01 +0300 | [diff] [blame] | 1465 | } |
| 1466 | |
Avi Kivity | 1b6c016 | 2007-05-24 13:03:52 +0300 | [diff] [blame] | 1467 | on_each_cpu(hardware_enable, NULL, 0, 1); |
Avi Kivity | 774c47f | 2007-02-12 00:54:47 -0800 | [diff] [blame] | 1468 | r = register_cpu_notifier(&kvm_cpu_notifier); |
| 1469 | if (r) |
Zhang Xiantao | d2308784 | 2007-11-29 15:35:39 +0800 | [diff] [blame] | 1470 | goto out_free_2; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1471 | register_reboot_notifier(&kvm_reboot_notifier); |
| 1472 | |
Avi Kivity | 59ae6c6 | 2007-02-12 00:54:48 -0800 | [diff] [blame] | 1473 | r = sysdev_class_register(&kvm_sysdev_class); |
| 1474 | if (r) |
Zhang Xiantao | d2308784 | 2007-11-29 15:35:39 +0800 | [diff] [blame] | 1475 | goto out_free_3; |
Avi Kivity | 59ae6c6 | 2007-02-12 00:54:48 -0800 | [diff] [blame] | 1476 | |
| 1477 | r = sysdev_register(&kvm_sysdev); |
| 1478 | if (r) |
Zhang Xiantao | d2308784 | 2007-11-29 15:35:39 +0800 | [diff] [blame] | 1479 | goto out_free_4; |
Avi Kivity | 59ae6c6 | 2007-02-12 00:54:48 -0800 | [diff] [blame] | 1480 | |
Rusty Russell | c16f862 | 2007-07-30 21:12:19 +1000 | [diff] [blame] | 1481 | /* A kmem cache lets us meet the alignment requirements of fx_save. */ |
| 1482 | kvm_vcpu_cache = kmem_cache_create("kvm_vcpu", vcpu_size, |
Joe Perches | 56919c5 | 2007-11-12 20:06:51 -0800 | [diff] [blame] | 1483 | __alignof__(struct kvm_vcpu), |
| 1484 | 0, NULL); |
Rusty Russell | c16f862 | 2007-07-30 21:12:19 +1000 | [diff] [blame] | 1485 | if (!kvm_vcpu_cache) { |
| 1486 | r = -ENOMEM; |
Zhang Xiantao | d2308784 | 2007-11-29 15:35:39 +0800 | [diff] [blame] | 1487 | goto out_free_5; |
Rusty Russell | c16f862 | 2007-07-30 21:12:19 +1000 | [diff] [blame] | 1488 | } |
| 1489 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1490 | kvm_chardev_ops.owner = module; |
| 1491 | |
| 1492 | r = misc_register(&kvm_dev); |
| 1493 | if (r) { |
Mike Day | d77c26f | 2007-10-08 09:02:08 -0400 | [diff] [blame] | 1494 | printk(KERN_ERR "kvm: misc device register failed\n"); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1495 | goto out_free; |
| 1496 | } |
| 1497 | |
Avi Kivity | 15ad714 | 2007-07-11 18:17:21 +0300 | [diff] [blame] | 1498 | kvm_preempt_ops.sched_in = kvm_sched_in; |
| 1499 | kvm_preempt_ops.sched_out = kvm_sched_out; |
| 1500 | |
Avi Kivity | c7addb9 | 2007-09-16 18:58:32 +0200 | [diff] [blame] | 1501 | return 0; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1502 | |
| 1503 | out_free: |
Rusty Russell | c16f862 | 2007-07-30 21:12:19 +1000 | [diff] [blame] | 1504 | kmem_cache_destroy(kvm_vcpu_cache); |
Zhang Xiantao | d2308784 | 2007-11-29 15:35:39 +0800 | [diff] [blame] | 1505 | out_free_5: |
Avi Kivity | 59ae6c6 | 2007-02-12 00:54:48 -0800 | [diff] [blame] | 1506 | sysdev_unregister(&kvm_sysdev); |
Zhang Xiantao | d2308784 | 2007-11-29 15:35:39 +0800 | [diff] [blame] | 1507 | out_free_4: |
Avi Kivity | 59ae6c6 | 2007-02-12 00:54:48 -0800 | [diff] [blame] | 1508 | sysdev_class_unregister(&kvm_sysdev_class); |
Zhang Xiantao | d2308784 | 2007-11-29 15:35:39 +0800 | [diff] [blame] | 1509 | out_free_3: |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1510 | unregister_reboot_notifier(&kvm_reboot_notifier); |
Avi Kivity | 774c47f | 2007-02-12 00:54:47 -0800 | [diff] [blame] | 1511 | unregister_cpu_notifier(&kvm_cpu_notifier); |
Zhang Xiantao | d2308784 | 2007-11-29 15:35:39 +0800 | [diff] [blame] | 1512 | out_free_2: |
Avi Kivity | 1b6c016 | 2007-05-24 13:03:52 +0300 | [diff] [blame] | 1513 | on_each_cpu(hardware_disable, NULL, 0, 1); |
Zhang Xiantao | d2308784 | 2007-11-29 15:35:39 +0800 | [diff] [blame] | 1514 | out_free_1: |
Zhang Xiantao | e9b11c1 | 2007-11-14 20:38:21 +0800 | [diff] [blame] | 1515 | kvm_arch_hardware_unsetup(); |
Zhang Xiantao | d2308784 | 2007-11-29 15:35:39 +0800 | [diff] [blame] | 1516 | out_free_0: |
| 1517 | __free_page(bad_page); |
Avi Kivity | ca45aaa | 2007-03-01 19:21:03 +0200 | [diff] [blame] | 1518 | out: |
Zhang Xiantao | f8c16bb | 2007-11-14 20:40:21 +0800 | [diff] [blame] | 1519 | kvm_arch_exit(); |
Zhang Xiantao | cb498ea | 2007-11-14 20:39:31 +0800 | [diff] [blame] | 1520 | kvm_exit_debug(); |
Zhang Xiantao | d2308784 | 2007-11-29 15:35:39 +0800 | [diff] [blame] | 1521 | out_fail: |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1522 | return r; |
| 1523 | } |
Zhang Xiantao | cb498ea | 2007-11-14 20:39:31 +0800 | [diff] [blame] | 1524 | EXPORT_SYMBOL_GPL(kvm_init); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1525 | |
Zhang Xiantao | cb498ea | 2007-11-14 20:39:31 +0800 | [diff] [blame] | 1526 | void kvm_exit(void) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1527 | { |
Feng(Eric) Liu | d4c9ff2 | 2008-04-10 08:47:53 -0400 | [diff] [blame] | 1528 | kvm_trace_cleanup(); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1529 | misc_deregister(&kvm_dev); |
Rusty Russell | c16f862 | 2007-07-30 21:12:19 +1000 | [diff] [blame] | 1530 | kmem_cache_destroy(kvm_vcpu_cache); |
Avi Kivity | 59ae6c6 | 2007-02-12 00:54:48 -0800 | [diff] [blame] | 1531 | sysdev_unregister(&kvm_sysdev); |
| 1532 | sysdev_class_unregister(&kvm_sysdev_class); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1533 | unregister_reboot_notifier(&kvm_reboot_notifier); |
Avi Kivity | 59ae6c6 | 2007-02-12 00:54:48 -0800 | [diff] [blame] | 1534 | unregister_cpu_notifier(&kvm_cpu_notifier); |
Avi Kivity | 1b6c016 | 2007-05-24 13:03:52 +0300 | [diff] [blame] | 1535 | on_each_cpu(hardware_disable, NULL, 0, 1); |
Zhang Xiantao | e9b11c1 | 2007-11-14 20:38:21 +0800 | [diff] [blame] | 1536 | kvm_arch_hardware_unsetup(); |
Zhang Xiantao | f8c16bb | 2007-11-14 20:40:21 +0800 | [diff] [blame] | 1537 | kvm_arch_exit(); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1538 | kvm_exit_debug(); |
Izik Eidus | cea7bb2 | 2007-10-17 19:17:48 +0200 | [diff] [blame] | 1539 | __free_page(bad_page); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1540 | } |
Zhang Xiantao | cb498ea | 2007-11-14 20:39:31 +0800 | [diff] [blame] | 1541 | EXPORT_SYMBOL_GPL(kvm_exit); |