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