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 | bccf215 | 2007-02-21 18:04:26 +0200 | [diff] [blame] | 764 | static int kvm_vcpu_ioctl_interrupt(struct kvm_vcpu *vcpu, |
| 765 | struct kvm_interrupt *irq) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 766 | { |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 767 | if (irq->irq < 0 || irq->irq >= 256) |
| 768 | return -EINVAL; |
Eddie Dong | 97222cc | 2007-09-12 10:58:04 +0300 | [diff] [blame] | 769 | if (irqchip_in_kernel(vcpu->kvm)) |
| 770 | return -ENXIO; |
Avi Kivity | bccf215 | 2007-02-21 18:04:26 +0200 | [diff] [blame] | 771 | vcpu_load(vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 772 | |
| 773 | set_bit(irq->irq, vcpu->irq_pending); |
| 774 | set_bit(irq->irq / BITS_PER_LONG, &vcpu->irq_summary); |
| 775 | |
| 776 | vcpu_put(vcpu); |
| 777 | |
| 778 | return 0; |
| 779 | } |
| 780 | |
Avi Kivity | 9a2bb7f | 2007-02-22 12:58:31 +0200 | [diff] [blame] | 781 | static struct page *kvm_vcpu_nopage(struct vm_area_struct *vma, |
| 782 | unsigned long address, |
| 783 | int *type) |
| 784 | { |
| 785 | struct kvm_vcpu *vcpu = vma->vm_file->private_data; |
| 786 | unsigned long pgoff; |
| 787 | struct page *page; |
| 788 | |
Avi Kivity | 9a2bb7f | 2007-02-22 12:58:31 +0200 | [diff] [blame] | 789 | pgoff = ((address - vma->vm_start) >> PAGE_SHIFT) + vma->vm_pgoff; |
Avi Kivity | 039576c | 2007-03-20 12:46:50 +0200 | [diff] [blame] | 790 | if (pgoff == 0) |
| 791 | page = virt_to_page(vcpu->run); |
| 792 | else if (pgoff == KVM_PIO_PAGE_OFFSET) |
| 793 | page = virt_to_page(vcpu->pio_data); |
| 794 | else |
Avi Kivity | 9a2bb7f | 2007-02-22 12:58:31 +0200 | [diff] [blame] | 795 | return NOPAGE_SIGBUS; |
Avi Kivity | 9a2bb7f | 2007-02-22 12:58:31 +0200 | [diff] [blame] | 796 | get_page(page); |
Nguyen Anh Quynh | cd0d913 | 2007-07-11 14:30:54 +0300 | [diff] [blame] | 797 | if (type != NULL) |
| 798 | *type = VM_FAULT_MINOR; |
| 799 | |
Avi Kivity | 9a2bb7f | 2007-02-22 12:58:31 +0200 | [diff] [blame] | 800 | return page; |
| 801 | } |
| 802 | |
| 803 | static struct vm_operations_struct kvm_vcpu_vm_ops = { |
| 804 | .nopage = kvm_vcpu_nopage, |
| 805 | }; |
| 806 | |
| 807 | static int kvm_vcpu_mmap(struct file *file, struct vm_area_struct *vma) |
| 808 | { |
| 809 | vma->vm_ops = &kvm_vcpu_vm_ops; |
| 810 | return 0; |
| 811 | } |
| 812 | |
Avi Kivity | bccf215 | 2007-02-21 18:04:26 +0200 | [diff] [blame] | 813 | static int kvm_vcpu_release(struct inode *inode, struct file *filp) |
| 814 | { |
| 815 | struct kvm_vcpu *vcpu = filp->private_data; |
| 816 | |
| 817 | fput(vcpu->kvm->filp); |
| 818 | return 0; |
| 819 | } |
| 820 | |
| 821 | static struct file_operations kvm_vcpu_fops = { |
| 822 | .release = kvm_vcpu_release, |
| 823 | .unlocked_ioctl = kvm_vcpu_ioctl, |
| 824 | .compat_ioctl = kvm_vcpu_ioctl, |
Avi Kivity | 9a2bb7f | 2007-02-22 12:58:31 +0200 | [diff] [blame] | 825 | .mmap = kvm_vcpu_mmap, |
Avi Kivity | bccf215 | 2007-02-21 18:04:26 +0200 | [diff] [blame] | 826 | }; |
| 827 | |
| 828 | /* |
| 829 | * Allocates an inode for the vcpu. |
| 830 | */ |
| 831 | static int create_vcpu_fd(struct kvm_vcpu *vcpu) |
| 832 | { |
| 833 | int fd, r; |
| 834 | struct inode *inode; |
| 835 | struct file *file; |
| 836 | |
Avi Kivity | d6d2816 | 2007-06-28 08:38:16 -0400 | [diff] [blame] | 837 | r = anon_inode_getfd(&fd, &inode, &file, |
| 838 | "kvm-vcpu", &kvm_vcpu_fops, vcpu); |
| 839 | if (r) |
| 840 | return r; |
Avi Kivity | bccf215 | 2007-02-21 18:04:26 +0200 | [diff] [blame] | 841 | atomic_inc(&vcpu->kvm->filp->f_count); |
Avi Kivity | bccf215 | 2007-02-21 18:04:26 +0200 | [diff] [blame] | 842 | return fd; |
Avi Kivity | bccf215 | 2007-02-21 18:04:26 +0200 | [diff] [blame] | 843 | } |
| 844 | |
Avi Kivity | c5ea766 | 2007-02-20 18:41:05 +0200 | [diff] [blame] | 845 | /* |
| 846 | * Creates some virtual cpus. Good luck creating more than one. |
| 847 | */ |
| 848 | static int kvm_vm_ioctl_create_vcpu(struct kvm *kvm, int n) |
| 849 | { |
| 850 | int r; |
| 851 | struct kvm_vcpu *vcpu; |
| 852 | |
Avi Kivity | c5ea766 | 2007-02-20 18:41:05 +0200 | [diff] [blame] | 853 | if (!valid_vcpu(n)) |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 854 | return -EINVAL; |
Avi Kivity | c5ea766 | 2007-02-20 18:41:05 +0200 | [diff] [blame] | 855 | |
Zhang Xiantao | e9b11c1 | 2007-11-14 20:38:21 +0800 | [diff] [blame] | 856 | vcpu = kvm_arch_vcpu_create(kvm, n); |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 857 | if (IS_ERR(vcpu)) |
| 858 | return PTR_ERR(vcpu); |
Avi Kivity | c5ea766 | 2007-02-20 18:41:05 +0200 | [diff] [blame] | 859 | |
Avi Kivity | 15ad714 | 2007-07-11 18:17:21 +0300 | [diff] [blame] | 860 | preempt_notifier_init(&vcpu->preempt_notifier, &kvm_preempt_ops); |
| 861 | |
Shaohua Li | 11ec280 | 2007-07-23 14:51:37 +0800 | [diff] [blame] | 862 | mutex_lock(&kvm->lock); |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 863 | if (kvm->vcpus[n]) { |
| 864 | r = -EEXIST; |
Shaohua Li | 11ec280 | 2007-07-23 14:51:37 +0800 | [diff] [blame] | 865 | mutex_unlock(&kvm->lock); |
Zhang Xiantao | e9b11c1 | 2007-11-14 20:38:21 +0800 | [diff] [blame] | 866 | goto vcpu_destroy; |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 867 | } |
| 868 | kvm->vcpus[n] = vcpu; |
Shaohua Li | 11ec280 | 2007-07-23 14:51:37 +0800 | [diff] [blame] | 869 | mutex_unlock(&kvm->lock); |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 870 | |
| 871 | /* Now it's all set up, let userspace reach it */ |
Avi Kivity | bccf215 | 2007-02-21 18:04:26 +0200 | [diff] [blame] | 872 | r = create_vcpu_fd(vcpu); |
| 873 | if (r < 0) |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 874 | goto unlink; |
Avi Kivity | bccf215 | 2007-02-21 18:04:26 +0200 | [diff] [blame] | 875 | return r; |
Avi Kivity | c5ea766 | 2007-02-20 18:41:05 +0200 | [diff] [blame] | 876 | |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 877 | unlink: |
Shaohua Li | 11ec280 | 2007-07-23 14:51:37 +0800 | [diff] [blame] | 878 | mutex_lock(&kvm->lock); |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 879 | kvm->vcpus[n] = NULL; |
Shaohua Li | 11ec280 | 2007-07-23 14:51:37 +0800 | [diff] [blame] | 880 | mutex_unlock(&kvm->lock); |
Zhang Xiantao | e9b11c1 | 2007-11-14 20:38:21 +0800 | [diff] [blame] | 881 | vcpu_destroy: |
| 882 | kvm_arch_vcpu_destory(vcpu); |
Avi Kivity | c5ea766 | 2007-02-20 18:41:05 +0200 | [diff] [blame] | 883 | return r; |
| 884 | } |
| 885 | |
Avi Kivity | 1961d27 | 2007-03-05 19:46:05 +0200 | [diff] [blame] | 886 | static int kvm_vcpu_ioctl_set_sigmask(struct kvm_vcpu *vcpu, sigset_t *sigset) |
| 887 | { |
| 888 | if (sigset) { |
| 889 | sigdelsetmask(sigset, sigmask(SIGKILL)|sigmask(SIGSTOP)); |
| 890 | vcpu->sigset_active = 1; |
| 891 | vcpu->sigset = *sigset; |
| 892 | } else |
| 893 | vcpu->sigset_active = 0; |
| 894 | return 0; |
| 895 | } |
| 896 | |
Avi Kivity | bccf215 | 2007-02-21 18:04:26 +0200 | [diff] [blame] | 897 | static long kvm_vcpu_ioctl(struct file *filp, |
| 898 | unsigned int ioctl, unsigned long arg) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 899 | { |
Avi Kivity | bccf215 | 2007-02-21 18:04:26 +0200 | [diff] [blame] | 900 | struct kvm_vcpu *vcpu = filp->private_data; |
Al Viro | 2f366987 | 2007-02-09 16:38:35 +0000 | [diff] [blame] | 901 | void __user *argp = (void __user *)arg; |
Carsten Otte | 313a3dc | 2007-10-11 19:16:52 +0200 | [diff] [blame] | 902 | int r; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 903 | |
| 904 | switch (ioctl) { |
Avi Kivity | 9a2bb7f | 2007-02-22 12:58:31 +0200 | [diff] [blame] | 905 | case KVM_RUN: |
Avi Kivity | f0fe510 | 2007-03-07 13:11:17 +0200 | [diff] [blame] | 906 | r = -EINVAL; |
| 907 | if (arg) |
| 908 | goto out; |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 909 | r = kvm_arch_vcpu_ioctl_run(vcpu, vcpu->run); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 910 | break; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 911 | case KVM_GET_REGS: { |
| 912 | struct kvm_regs kvm_regs; |
| 913 | |
Avi Kivity | bccf215 | 2007-02-21 18:04:26 +0200 | [diff] [blame] | 914 | memset(&kvm_regs, 0, sizeof kvm_regs); |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 915 | r = kvm_arch_vcpu_ioctl_get_regs(vcpu, &kvm_regs); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 916 | if (r) |
| 917 | goto out; |
| 918 | r = -EFAULT; |
Al Viro | 2f366987 | 2007-02-09 16:38:35 +0000 | [diff] [blame] | 919 | if (copy_to_user(argp, &kvm_regs, sizeof kvm_regs)) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 920 | goto out; |
| 921 | r = 0; |
| 922 | break; |
| 923 | } |
| 924 | case KVM_SET_REGS: { |
| 925 | struct kvm_regs kvm_regs; |
| 926 | |
| 927 | r = -EFAULT; |
Al Viro | 2f366987 | 2007-02-09 16:38:35 +0000 | [diff] [blame] | 928 | if (copy_from_user(&kvm_regs, argp, sizeof kvm_regs)) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 929 | goto out; |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 930 | r = kvm_arch_vcpu_ioctl_set_regs(vcpu, &kvm_regs); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 931 | if (r) |
| 932 | goto out; |
| 933 | r = 0; |
| 934 | break; |
| 935 | } |
| 936 | case KVM_GET_SREGS: { |
| 937 | struct kvm_sregs kvm_sregs; |
| 938 | |
Avi Kivity | bccf215 | 2007-02-21 18:04:26 +0200 | [diff] [blame] | 939 | memset(&kvm_sregs, 0, sizeof kvm_sregs); |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 940 | r = kvm_arch_vcpu_ioctl_get_sregs(vcpu, &kvm_sregs); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 941 | if (r) |
| 942 | goto out; |
| 943 | r = -EFAULT; |
Al Viro | 2f366987 | 2007-02-09 16:38:35 +0000 | [diff] [blame] | 944 | if (copy_to_user(argp, &kvm_sregs, sizeof kvm_sregs)) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 945 | goto out; |
| 946 | r = 0; |
| 947 | break; |
| 948 | } |
| 949 | case KVM_SET_SREGS: { |
| 950 | struct kvm_sregs kvm_sregs; |
| 951 | |
| 952 | r = -EFAULT; |
Al Viro | 2f366987 | 2007-02-09 16:38:35 +0000 | [diff] [blame] | 953 | if (copy_from_user(&kvm_sregs, argp, sizeof kvm_sregs)) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 954 | goto out; |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 955 | r = kvm_arch_vcpu_ioctl_set_sregs(vcpu, &kvm_sregs); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 956 | if (r) |
| 957 | goto out; |
| 958 | r = 0; |
| 959 | break; |
| 960 | } |
| 961 | case KVM_TRANSLATE: { |
| 962 | struct kvm_translation tr; |
| 963 | |
| 964 | r = -EFAULT; |
Al Viro | 2f366987 | 2007-02-09 16:38:35 +0000 | [diff] [blame] | 965 | if (copy_from_user(&tr, argp, sizeof tr)) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 966 | goto out; |
Zhang Xiantao | 8b00679 | 2007-11-16 13:05:55 +0800 | [diff] [blame^] | 967 | r = kvm_arch_vcpu_ioctl_translate(vcpu, &tr); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 968 | if (r) |
| 969 | goto out; |
| 970 | r = -EFAULT; |
Al Viro | 2f366987 | 2007-02-09 16:38:35 +0000 | [diff] [blame] | 971 | if (copy_to_user(argp, &tr, sizeof tr)) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 972 | goto out; |
| 973 | r = 0; |
| 974 | break; |
| 975 | } |
| 976 | case KVM_INTERRUPT: { |
| 977 | struct kvm_interrupt irq; |
| 978 | |
| 979 | r = -EFAULT; |
Al Viro | 2f366987 | 2007-02-09 16:38:35 +0000 | [diff] [blame] | 980 | if (copy_from_user(&irq, argp, sizeof irq)) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 981 | goto out; |
Avi Kivity | bccf215 | 2007-02-21 18:04:26 +0200 | [diff] [blame] | 982 | r = kvm_vcpu_ioctl_interrupt(vcpu, &irq); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 983 | if (r) |
| 984 | goto out; |
| 985 | r = 0; |
| 986 | break; |
| 987 | } |
| 988 | case KVM_DEBUG_GUEST: { |
| 989 | struct kvm_debug_guest dbg; |
| 990 | |
| 991 | r = -EFAULT; |
Al Viro | 2f366987 | 2007-02-09 16:38:35 +0000 | [diff] [blame] | 992 | if (copy_from_user(&dbg, argp, sizeof dbg)) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 993 | goto out; |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 994 | r = kvm_arch_vcpu_ioctl_debug_guest(vcpu, &dbg); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 995 | if (r) |
| 996 | goto out; |
| 997 | r = 0; |
| 998 | break; |
| 999 | } |
Avi Kivity | 1961d27 | 2007-03-05 19:46:05 +0200 | [diff] [blame] | 1000 | case KVM_SET_SIGNAL_MASK: { |
| 1001 | struct kvm_signal_mask __user *sigmask_arg = argp; |
| 1002 | struct kvm_signal_mask kvm_sigmask; |
| 1003 | sigset_t sigset, *p; |
| 1004 | |
| 1005 | p = NULL; |
| 1006 | if (argp) { |
| 1007 | r = -EFAULT; |
| 1008 | if (copy_from_user(&kvm_sigmask, argp, |
| 1009 | sizeof kvm_sigmask)) |
| 1010 | goto out; |
| 1011 | r = -EINVAL; |
| 1012 | if (kvm_sigmask.len != sizeof sigset) |
| 1013 | goto out; |
| 1014 | r = -EFAULT; |
| 1015 | if (copy_from_user(&sigset, sigmask_arg->sigset, |
| 1016 | sizeof sigset)) |
| 1017 | goto out; |
| 1018 | p = &sigset; |
| 1019 | } |
| 1020 | r = kvm_vcpu_ioctl_set_sigmask(vcpu, &sigset); |
| 1021 | break; |
| 1022 | } |
Avi Kivity | b883673 | 2007-04-01 16:34:31 +0300 | [diff] [blame] | 1023 | case KVM_GET_FPU: { |
| 1024 | struct kvm_fpu fpu; |
| 1025 | |
| 1026 | memset(&fpu, 0, sizeof fpu); |
Hollis Blanchard | d075206 | 2007-10-31 17:24:25 -0500 | [diff] [blame] | 1027 | r = kvm_arch_vcpu_ioctl_get_fpu(vcpu, &fpu); |
Avi Kivity | b883673 | 2007-04-01 16:34:31 +0300 | [diff] [blame] | 1028 | if (r) |
| 1029 | goto out; |
| 1030 | r = -EFAULT; |
| 1031 | if (copy_to_user(argp, &fpu, sizeof fpu)) |
| 1032 | goto out; |
| 1033 | r = 0; |
| 1034 | break; |
| 1035 | } |
| 1036 | case KVM_SET_FPU: { |
| 1037 | struct kvm_fpu fpu; |
| 1038 | |
| 1039 | r = -EFAULT; |
| 1040 | if (copy_from_user(&fpu, argp, sizeof fpu)) |
| 1041 | goto out; |
Hollis Blanchard | d075206 | 2007-10-31 17:24:25 -0500 | [diff] [blame] | 1042 | r = kvm_arch_vcpu_ioctl_set_fpu(vcpu, &fpu); |
Avi Kivity | b883673 | 2007-04-01 16:34:31 +0300 | [diff] [blame] | 1043 | if (r) |
| 1044 | goto out; |
| 1045 | r = 0; |
| 1046 | break; |
| 1047 | } |
Avi Kivity | bccf215 | 2007-02-21 18:04:26 +0200 | [diff] [blame] | 1048 | default: |
Carsten Otte | 313a3dc | 2007-10-11 19:16:52 +0200 | [diff] [blame] | 1049 | r = kvm_arch_vcpu_ioctl(filp, ioctl, arg); |
Avi Kivity | bccf215 | 2007-02-21 18:04:26 +0200 | [diff] [blame] | 1050 | } |
| 1051 | out: |
| 1052 | return r; |
| 1053 | } |
| 1054 | |
| 1055 | static long kvm_vm_ioctl(struct file *filp, |
| 1056 | unsigned int ioctl, unsigned long arg) |
| 1057 | { |
| 1058 | struct kvm *kvm = filp->private_data; |
| 1059 | void __user *argp = (void __user *)arg; |
Carsten Otte | 1fe779f | 2007-10-29 16:08:35 +0100 | [diff] [blame] | 1060 | int r; |
Avi Kivity | bccf215 | 2007-02-21 18:04:26 +0200 | [diff] [blame] | 1061 | |
| 1062 | switch (ioctl) { |
| 1063 | case KVM_CREATE_VCPU: |
| 1064 | r = kvm_vm_ioctl_create_vcpu(kvm, arg); |
| 1065 | if (r < 0) |
| 1066 | goto out; |
| 1067 | break; |
Izik Eidus | 6fc138d | 2007-10-09 19:20:39 +0200 | [diff] [blame] | 1068 | case KVM_SET_USER_MEMORY_REGION: { |
| 1069 | struct kvm_userspace_memory_region kvm_userspace_mem; |
| 1070 | |
| 1071 | r = -EFAULT; |
| 1072 | if (copy_from_user(&kvm_userspace_mem, argp, |
| 1073 | sizeof kvm_userspace_mem)) |
| 1074 | goto out; |
| 1075 | |
| 1076 | r = kvm_vm_ioctl_set_memory_region(kvm, &kvm_userspace_mem, 1); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1077 | if (r) |
| 1078 | goto out; |
| 1079 | break; |
| 1080 | } |
| 1081 | case KVM_GET_DIRTY_LOG: { |
| 1082 | struct kvm_dirty_log log; |
| 1083 | |
| 1084 | r = -EFAULT; |
Al Viro | 2f366987 | 2007-02-09 16:38:35 +0000 | [diff] [blame] | 1085 | if (copy_from_user(&log, argp, sizeof log)) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1086 | goto out; |
Avi Kivity | 2c6f5df | 2007-02-20 18:27:58 +0200 | [diff] [blame] | 1087 | r = kvm_vm_ioctl_get_dirty_log(kvm, &log); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1088 | if (r) |
| 1089 | goto out; |
| 1090 | break; |
| 1091 | } |
Avi Kivity | f17abe9 | 2007-02-21 19:28:04 +0200 | [diff] [blame] | 1092 | default: |
Carsten Otte | 1fe779f | 2007-10-29 16:08:35 +0100 | [diff] [blame] | 1093 | r = kvm_arch_vm_ioctl(filp, ioctl, arg); |
Avi Kivity | f17abe9 | 2007-02-21 19:28:04 +0200 | [diff] [blame] | 1094 | } |
| 1095 | out: |
| 1096 | return r; |
| 1097 | } |
| 1098 | |
| 1099 | static struct page *kvm_vm_nopage(struct vm_area_struct *vma, |
| 1100 | unsigned long address, |
| 1101 | int *type) |
| 1102 | { |
| 1103 | struct kvm *kvm = vma->vm_file->private_data; |
| 1104 | unsigned long pgoff; |
Avi Kivity | f17abe9 | 2007-02-21 19:28:04 +0200 | [diff] [blame] | 1105 | struct page *page; |
| 1106 | |
Avi Kivity | f17abe9 | 2007-02-21 19:28:04 +0200 | [diff] [blame] | 1107 | pgoff = ((address - vma->vm_start) >> PAGE_SHIFT) + vma->vm_pgoff; |
Izik Eidus | e0d62c7 | 2007-10-24 23:57:46 +0200 | [diff] [blame] | 1108 | if (!kvm_is_visible_gfn(kvm, pgoff)) |
| 1109 | return NOPAGE_SIGBUS; |
Anthony Liguori | aab61cc | 2007-10-29 15:15:20 -0500 | [diff] [blame] | 1110 | /* current->mm->mmap_sem is already held so call lockless version */ |
| 1111 | page = __gfn_to_page(kvm, pgoff); |
Izik Eidus | 8a7ae05 | 2007-10-18 11:09:33 +0200 | [diff] [blame] | 1112 | if (is_error_page(page)) { |
| 1113 | kvm_release_page(page); |
Avi Kivity | f17abe9 | 2007-02-21 19:28:04 +0200 | [diff] [blame] | 1114 | return NOPAGE_SIGBUS; |
Izik Eidus | 8a7ae05 | 2007-10-18 11:09:33 +0200 | [diff] [blame] | 1115 | } |
Nguyen Anh Quynh | cd0d913 | 2007-07-11 14:30:54 +0300 | [diff] [blame] | 1116 | if (type != NULL) |
| 1117 | *type = VM_FAULT_MINOR; |
| 1118 | |
Avi Kivity | f17abe9 | 2007-02-21 19:28:04 +0200 | [diff] [blame] | 1119 | return page; |
| 1120 | } |
| 1121 | |
| 1122 | static struct vm_operations_struct kvm_vm_vm_ops = { |
| 1123 | .nopage = kvm_vm_nopage, |
| 1124 | }; |
| 1125 | |
| 1126 | static int kvm_vm_mmap(struct file *file, struct vm_area_struct *vma) |
| 1127 | { |
| 1128 | vma->vm_ops = &kvm_vm_vm_ops; |
| 1129 | return 0; |
| 1130 | } |
| 1131 | |
| 1132 | static struct file_operations kvm_vm_fops = { |
| 1133 | .release = kvm_vm_release, |
| 1134 | .unlocked_ioctl = kvm_vm_ioctl, |
| 1135 | .compat_ioctl = kvm_vm_ioctl, |
| 1136 | .mmap = kvm_vm_mmap, |
| 1137 | }; |
| 1138 | |
| 1139 | static int kvm_dev_ioctl_create_vm(void) |
| 1140 | { |
| 1141 | int fd, r; |
| 1142 | struct inode *inode; |
| 1143 | struct file *file; |
| 1144 | struct kvm *kvm; |
| 1145 | |
Avi Kivity | f17abe9 | 2007-02-21 19:28:04 +0200 | [diff] [blame] | 1146 | kvm = kvm_create_vm(); |
Avi Kivity | d6d2816 | 2007-06-28 08:38:16 -0400 | [diff] [blame] | 1147 | if (IS_ERR(kvm)) |
| 1148 | return PTR_ERR(kvm); |
| 1149 | r = anon_inode_getfd(&fd, &inode, &file, "kvm-vm", &kvm_vm_fops, kvm); |
| 1150 | if (r) { |
| 1151 | kvm_destroy_vm(kvm); |
| 1152 | return r; |
Avi Kivity | f17abe9 | 2007-02-21 19:28:04 +0200 | [diff] [blame] | 1153 | } |
| 1154 | |
Avi Kivity | bccf215 | 2007-02-21 18:04:26 +0200 | [diff] [blame] | 1155 | kvm->filp = file; |
Avi Kivity | f17abe9 | 2007-02-21 19:28:04 +0200 | [diff] [blame] | 1156 | |
Avi Kivity | f17abe9 | 2007-02-21 19:28:04 +0200 | [diff] [blame] | 1157 | return fd; |
Avi Kivity | f17abe9 | 2007-02-21 19:28:04 +0200 | [diff] [blame] | 1158 | } |
| 1159 | |
| 1160 | static long kvm_dev_ioctl(struct file *filp, |
| 1161 | unsigned int ioctl, unsigned long arg) |
| 1162 | { |
| 1163 | void __user *argp = (void __user *)arg; |
Avi Kivity | 07c45a3 | 2007-03-07 13:05:38 +0200 | [diff] [blame] | 1164 | long r = -EINVAL; |
Avi Kivity | f17abe9 | 2007-02-21 19:28:04 +0200 | [diff] [blame] | 1165 | |
| 1166 | switch (ioctl) { |
| 1167 | case KVM_GET_API_VERSION: |
Avi Kivity | f0fe510 | 2007-03-07 13:11:17 +0200 | [diff] [blame] | 1168 | r = -EINVAL; |
| 1169 | if (arg) |
| 1170 | goto out; |
Avi Kivity | f17abe9 | 2007-02-21 19:28:04 +0200 | [diff] [blame] | 1171 | r = KVM_API_VERSION; |
| 1172 | break; |
| 1173 | case KVM_CREATE_VM: |
Avi Kivity | f0fe510 | 2007-03-07 13:11:17 +0200 | [diff] [blame] | 1174 | r = -EINVAL; |
| 1175 | if (arg) |
| 1176 | goto out; |
Avi Kivity | f17abe9 | 2007-02-21 19:28:04 +0200 | [diff] [blame] | 1177 | r = kvm_dev_ioctl_create_vm(); |
| 1178 | break; |
Zhang Xiantao | 018d00d | 2007-11-15 23:07:47 +0800 | [diff] [blame] | 1179 | case KVM_CHECK_EXTENSION: |
| 1180 | r = kvm_dev_ioctl_check_extension((long)argp); |
Avi Kivity | 5d308f4 | 2007-03-01 17:56:20 +0200 | [diff] [blame] | 1181 | break; |
Avi Kivity | 07c45a3 | 2007-03-07 13:05:38 +0200 | [diff] [blame] | 1182 | case KVM_GET_VCPU_MMAP_SIZE: |
| 1183 | r = -EINVAL; |
| 1184 | if (arg) |
| 1185 | goto out; |
Avi Kivity | 039576c | 2007-03-20 12:46:50 +0200 | [diff] [blame] | 1186 | r = 2 * PAGE_SIZE; |
Avi Kivity | 07c45a3 | 2007-03-07 13:05:38 +0200 | [diff] [blame] | 1187 | break; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1188 | default: |
Carsten Otte | 043405e | 2007-10-10 17:16:19 +0200 | [diff] [blame] | 1189 | return kvm_arch_dev_ioctl(filp, ioctl, arg); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1190 | } |
| 1191 | out: |
| 1192 | return r; |
| 1193 | } |
| 1194 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1195 | static struct file_operations kvm_chardev_ops = { |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1196 | .unlocked_ioctl = kvm_dev_ioctl, |
| 1197 | .compat_ioctl = kvm_dev_ioctl, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1198 | }; |
| 1199 | |
| 1200 | static struct miscdevice kvm_dev = { |
Avi Kivity | bbe4432 | 2007-03-04 13:27:36 +0200 | [diff] [blame] | 1201 | KVM_MINOR, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1202 | "kvm", |
| 1203 | &kvm_chardev_ops, |
| 1204 | }; |
| 1205 | |
Avi Kivity | 1b6c016 | 2007-05-24 13:03:52 +0300 | [diff] [blame] | 1206 | static void hardware_enable(void *junk) |
| 1207 | { |
| 1208 | int cpu = raw_smp_processor_id(); |
| 1209 | |
| 1210 | if (cpu_isset(cpu, cpus_hardware_enabled)) |
| 1211 | return; |
| 1212 | cpu_set(cpu, cpus_hardware_enabled); |
Zhang Xiantao | e9b11c1 | 2007-11-14 20:38:21 +0800 | [diff] [blame] | 1213 | kvm_arch_hardware_enable(NULL); |
Avi Kivity | 1b6c016 | 2007-05-24 13:03:52 +0300 | [diff] [blame] | 1214 | } |
| 1215 | |
| 1216 | static void hardware_disable(void *junk) |
| 1217 | { |
| 1218 | int cpu = raw_smp_processor_id(); |
| 1219 | |
| 1220 | if (!cpu_isset(cpu, cpus_hardware_enabled)) |
| 1221 | return; |
| 1222 | cpu_clear(cpu, cpus_hardware_enabled); |
| 1223 | decache_vcpus_on_cpu(cpu); |
Zhang Xiantao | e9b11c1 | 2007-11-14 20:38:21 +0800 | [diff] [blame] | 1224 | kvm_arch_hardware_disable(NULL); |
Avi Kivity | 1b6c016 | 2007-05-24 13:03:52 +0300 | [diff] [blame] | 1225 | } |
| 1226 | |
Avi Kivity | 774c47f | 2007-02-12 00:54:47 -0800 | [diff] [blame] | 1227 | static int kvm_cpu_hotplug(struct notifier_block *notifier, unsigned long val, |
| 1228 | void *v) |
| 1229 | { |
| 1230 | int cpu = (long)v; |
| 1231 | |
Avi Kivity | 1a6f4d7 | 2007-11-11 18:37:32 +0200 | [diff] [blame] | 1232 | val &= ~CPU_TASKS_FROZEN; |
Avi Kivity | 774c47f | 2007-02-12 00:54:47 -0800 | [diff] [blame] | 1233 | switch (val) { |
Avi Kivity | cec9ad2 | 2007-05-24 13:11:41 +0300 | [diff] [blame] | 1234 | case CPU_DYING: |
Avi Kivity | 6ec8a85 | 2007-08-19 15:57:26 +0300 | [diff] [blame] | 1235 | printk(KERN_INFO "kvm: disabling virtualization on CPU%d\n", |
| 1236 | cpu); |
| 1237 | hardware_disable(NULL); |
| 1238 | break; |
Avi Kivity | 774c47f | 2007-02-12 00:54:47 -0800 | [diff] [blame] | 1239 | case CPU_UP_CANCELED: |
Jeremy Katz | 43934a3 | 2007-02-19 14:37:46 +0200 | [diff] [blame] | 1240 | printk(KERN_INFO "kvm: disabling virtualization on CPU%d\n", |
| 1241 | cpu); |
Avi Kivity | 1b6c016 | 2007-05-24 13:03:52 +0300 | [diff] [blame] | 1242 | smp_call_function_single(cpu, hardware_disable, NULL, 0, 1); |
Avi Kivity | 774c47f | 2007-02-12 00:54:47 -0800 | [diff] [blame] | 1243 | break; |
Jeremy Katz | 43934a3 | 2007-02-19 14:37:46 +0200 | [diff] [blame] | 1244 | case CPU_ONLINE: |
| 1245 | printk(KERN_INFO "kvm: enabling virtualization on CPU%d\n", |
| 1246 | cpu); |
Avi Kivity | 1b6c016 | 2007-05-24 13:03:52 +0300 | [diff] [blame] | 1247 | smp_call_function_single(cpu, hardware_enable, NULL, 0, 1); |
Avi Kivity | 774c47f | 2007-02-12 00:54:47 -0800 | [diff] [blame] | 1248 | break; |
| 1249 | } |
| 1250 | return NOTIFY_OK; |
| 1251 | } |
| 1252 | |
Rusty Russell | 9a2b85c | 2007-07-17 23:17:55 +1000 | [diff] [blame] | 1253 | static int kvm_reboot(struct notifier_block *notifier, unsigned long val, |
Mike Day | d77c26f | 2007-10-08 09:02:08 -0400 | [diff] [blame] | 1254 | void *v) |
Rusty Russell | 9a2b85c | 2007-07-17 23:17:55 +1000 | [diff] [blame] | 1255 | { |
| 1256 | if (val == SYS_RESTART) { |
| 1257 | /* |
| 1258 | * Some (well, at least mine) BIOSes hang on reboot if |
| 1259 | * in vmx root mode. |
| 1260 | */ |
| 1261 | printk(KERN_INFO "kvm: exiting hardware virtualization\n"); |
| 1262 | on_each_cpu(hardware_disable, NULL, 0, 1); |
| 1263 | } |
| 1264 | return NOTIFY_OK; |
| 1265 | } |
| 1266 | |
| 1267 | static struct notifier_block kvm_reboot_notifier = { |
| 1268 | .notifier_call = kvm_reboot, |
| 1269 | .priority = 0, |
| 1270 | }; |
| 1271 | |
Gregory Haskins | 2eeb2e9 | 2007-05-31 14:08:53 -0400 | [diff] [blame] | 1272 | void kvm_io_bus_init(struct kvm_io_bus *bus) |
| 1273 | { |
| 1274 | memset(bus, 0, sizeof(*bus)); |
| 1275 | } |
| 1276 | |
| 1277 | void kvm_io_bus_destroy(struct kvm_io_bus *bus) |
| 1278 | { |
| 1279 | int i; |
| 1280 | |
| 1281 | for (i = 0; i < bus->dev_count; i++) { |
| 1282 | struct kvm_io_device *pos = bus->devs[i]; |
| 1283 | |
| 1284 | kvm_iodevice_destructor(pos); |
| 1285 | } |
| 1286 | } |
| 1287 | |
| 1288 | struct kvm_io_device *kvm_io_bus_find_dev(struct kvm_io_bus *bus, gpa_t addr) |
| 1289 | { |
| 1290 | int i; |
| 1291 | |
| 1292 | for (i = 0; i < bus->dev_count; i++) { |
| 1293 | struct kvm_io_device *pos = bus->devs[i]; |
| 1294 | |
| 1295 | if (pos->in_range(pos, addr)) |
| 1296 | return pos; |
| 1297 | } |
| 1298 | |
| 1299 | return NULL; |
| 1300 | } |
| 1301 | |
| 1302 | void kvm_io_bus_register_dev(struct kvm_io_bus *bus, struct kvm_io_device *dev) |
| 1303 | { |
| 1304 | BUG_ON(bus->dev_count > (NR_IOBUS_DEVS-1)); |
| 1305 | |
| 1306 | bus->devs[bus->dev_count++] = dev; |
| 1307 | } |
| 1308 | |
Avi Kivity | 774c47f | 2007-02-12 00:54:47 -0800 | [diff] [blame] | 1309 | static struct notifier_block kvm_cpu_notifier = { |
| 1310 | .notifier_call = kvm_cpu_hotplug, |
| 1311 | .priority = 20, /* must be > scheduler priority */ |
| 1312 | }; |
| 1313 | |
Avi Kivity | 1165f5f | 2007-04-19 17:27:43 +0300 | [diff] [blame] | 1314 | static u64 stat_get(void *_offset) |
| 1315 | { |
| 1316 | unsigned offset = (long)_offset; |
| 1317 | u64 total = 0; |
| 1318 | struct kvm *kvm; |
| 1319 | struct kvm_vcpu *vcpu; |
| 1320 | int i; |
| 1321 | |
| 1322 | spin_lock(&kvm_lock); |
| 1323 | list_for_each_entry(kvm, &vm_list, vm_list) |
| 1324 | for (i = 0; i < KVM_MAX_VCPUS; ++i) { |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 1325 | vcpu = kvm->vcpus[i]; |
| 1326 | if (vcpu) |
| 1327 | total += *(u32 *)((void *)vcpu + offset); |
Avi Kivity | 1165f5f | 2007-04-19 17:27:43 +0300 | [diff] [blame] | 1328 | } |
| 1329 | spin_unlock(&kvm_lock); |
| 1330 | return total; |
| 1331 | } |
| 1332 | |
Rusty Russell | 3dea7ca | 2007-08-01 10:12:22 +1000 | [diff] [blame] | 1333 | DEFINE_SIMPLE_ATTRIBUTE(stat_fops, stat_get, NULL, "%llu\n"); |
Avi Kivity | 1165f5f | 2007-04-19 17:27:43 +0300 | [diff] [blame] | 1334 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1335 | static __init void kvm_init_debug(void) |
| 1336 | { |
| 1337 | struct kvm_stats_debugfs_item *p; |
| 1338 | |
Al Viro | 8b6d44c | 2007-02-09 16:38:40 +0000 | [diff] [blame] | 1339 | debugfs_dir = debugfs_create_dir("kvm", NULL); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1340 | for (p = debugfs_entries; p->name; ++p) |
Avi Kivity | 1165f5f | 2007-04-19 17:27:43 +0300 | [diff] [blame] | 1341 | p->dentry = debugfs_create_file(p->name, 0444, debugfs_dir, |
| 1342 | (void *)(long)p->offset, |
| 1343 | &stat_fops); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1344 | } |
| 1345 | |
| 1346 | static void kvm_exit_debug(void) |
| 1347 | { |
| 1348 | struct kvm_stats_debugfs_item *p; |
| 1349 | |
| 1350 | for (p = debugfs_entries; p->name; ++p) |
| 1351 | debugfs_remove(p->dentry); |
| 1352 | debugfs_remove(debugfs_dir); |
| 1353 | } |
| 1354 | |
Avi Kivity | 59ae6c6 | 2007-02-12 00:54:48 -0800 | [diff] [blame] | 1355 | static int kvm_suspend(struct sys_device *dev, pm_message_t state) |
| 1356 | { |
Avi Kivity | 4267c41 | 2007-05-24 13:09:41 +0300 | [diff] [blame] | 1357 | hardware_disable(NULL); |
Avi Kivity | 59ae6c6 | 2007-02-12 00:54:48 -0800 | [diff] [blame] | 1358 | return 0; |
| 1359 | } |
| 1360 | |
| 1361 | static int kvm_resume(struct sys_device *dev) |
| 1362 | { |
Avi Kivity | 4267c41 | 2007-05-24 13:09:41 +0300 | [diff] [blame] | 1363 | hardware_enable(NULL); |
Avi Kivity | 59ae6c6 | 2007-02-12 00:54:48 -0800 | [diff] [blame] | 1364 | return 0; |
| 1365 | } |
| 1366 | |
| 1367 | static struct sysdev_class kvm_sysdev_class = { |
Kay Sievers | af5ca3f | 2007-12-20 02:09:39 +0100 | [diff] [blame] | 1368 | .name = "kvm", |
Avi Kivity | 59ae6c6 | 2007-02-12 00:54:48 -0800 | [diff] [blame] | 1369 | .suspend = kvm_suspend, |
| 1370 | .resume = kvm_resume, |
| 1371 | }; |
| 1372 | |
| 1373 | static struct sys_device kvm_sysdev = { |
| 1374 | .id = 0, |
| 1375 | .cls = &kvm_sysdev_class, |
| 1376 | }; |
| 1377 | |
Izik Eidus | cea7bb2 | 2007-10-17 19:17:48 +0200 | [diff] [blame] | 1378 | struct page *bad_page; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1379 | |
Avi Kivity | 15ad714 | 2007-07-11 18:17:21 +0300 | [diff] [blame] | 1380 | static inline |
| 1381 | struct kvm_vcpu *preempt_notifier_to_vcpu(struct preempt_notifier *pn) |
| 1382 | { |
| 1383 | return container_of(pn, struct kvm_vcpu, preempt_notifier); |
| 1384 | } |
| 1385 | |
| 1386 | static void kvm_sched_in(struct preempt_notifier *pn, int cpu) |
| 1387 | { |
| 1388 | struct kvm_vcpu *vcpu = preempt_notifier_to_vcpu(pn); |
| 1389 | |
Zhang Xiantao | e9b11c1 | 2007-11-14 20:38:21 +0800 | [diff] [blame] | 1390 | kvm_arch_vcpu_load(vcpu, cpu); |
Avi Kivity | 15ad714 | 2007-07-11 18:17:21 +0300 | [diff] [blame] | 1391 | } |
| 1392 | |
| 1393 | static void kvm_sched_out(struct preempt_notifier *pn, |
| 1394 | struct task_struct *next) |
| 1395 | { |
| 1396 | struct kvm_vcpu *vcpu = preempt_notifier_to_vcpu(pn); |
| 1397 | |
Zhang Xiantao | e9b11c1 | 2007-11-14 20:38:21 +0800 | [diff] [blame] | 1398 | kvm_arch_vcpu_put(vcpu); |
Avi Kivity | 15ad714 | 2007-07-11 18:17:21 +0300 | [diff] [blame] | 1399 | } |
| 1400 | |
Zhang Xiantao | f8c16bb | 2007-11-14 20:40:21 +0800 | [diff] [blame] | 1401 | int kvm_init(void *opaque, unsigned int vcpu_size, |
Rusty Russell | c16f862 | 2007-07-30 21:12:19 +1000 | [diff] [blame] | 1402 | struct module *module) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1403 | { |
| 1404 | int r; |
Yang, Sheng | 002c7f7 | 2007-07-31 14:23:01 +0300 | [diff] [blame] | 1405 | int cpu; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1406 | |
Zhang Xiantao | cb498ea | 2007-11-14 20:39:31 +0800 | [diff] [blame] | 1407 | r = kvm_mmu_module_init(); |
| 1408 | if (r) |
| 1409 | goto out4; |
| 1410 | |
| 1411 | kvm_init_debug(); |
| 1412 | |
Zhang Xiantao | f8c16bb | 2007-11-14 20:40:21 +0800 | [diff] [blame] | 1413 | r = kvm_arch_init(opaque); |
| 1414 | if (r) |
| 1415 | goto out4; |
Zhang Xiantao | cb498ea | 2007-11-14 20:39:31 +0800 | [diff] [blame] | 1416 | |
| 1417 | bad_page = alloc_page(GFP_KERNEL | __GFP_ZERO); |
| 1418 | |
| 1419 | if (bad_page == NULL) { |
| 1420 | r = -ENOMEM; |
| 1421 | goto out; |
| 1422 | } |
| 1423 | |
Zhang Xiantao | e9b11c1 | 2007-11-14 20:38:21 +0800 | [diff] [blame] | 1424 | r = kvm_arch_hardware_setup(); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1425 | if (r < 0) |
Avi Kivity | ca45aaa | 2007-03-01 19:21:03 +0200 | [diff] [blame] | 1426 | goto out; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1427 | |
Yang, Sheng | 002c7f7 | 2007-07-31 14:23:01 +0300 | [diff] [blame] | 1428 | for_each_online_cpu(cpu) { |
| 1429 | smp_call_function_single(cpu, |
Zhang Xiantao | e9b11c1 | 2007-11-14 20:38:21 +0800 | [diff] [blame] | 1430 | kvm_arch_check_processor_compat, |
Yang, Sheng | 002c7f7 | 2007-07-31 14:23:01 +0300 | [diff] [blame] | 1431 | &r, 0, 1); |
| 1432 | if (r < 0) |
| 1433 | goto out_free_0; |
| 1434 | } |
| 1435 | |
Avi Kivity | 1b6c016 | 2007-05-24 13:03:52 +0300 | [diff] [blame] | 1436 | on_each_cpu(hardware_enable, NULL, 0, 1); |
Avi Kivity | 774c47f | 2007-02-12 00:54:47 -0800 | [diff] [blame] | 1437 | r = register_cpu_notifier(&kvm_cpu_notifier); |
| 1438 | if (r) |
| 1439 | goto out_free_1; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1440 | register_reboot_notifier(&kvm_reboot_notifier); |
| 1441 | |
Avi Kivity | 59ae6c6 | 2007-02-12 00:54:48 -0800 | [diff] [blame] | 1442 | r = sysdev_class_register(&kvm_sysdev_class); |
| 1443 | if (r) |
| 1444 | goto out_free_2; |
| 1445 | |
| 1446 | r = sysdev_register(&kvm_sysdev); |
| 1447 | if (r) |
| 1448 | goto out_free_3; |
| 1449 | |
Rusty Russell | c16f862 | 2007-07-30 21:12:19 +1000 | [diff] [blame] | 1450 | /* A kmem cache lets us meet the alignment requirements of fx_save. */ |
| 1451 | kvm_vcpu_cache = kmem_cache_create("kvm_vcpu", vcpu_size, |
| 1452 | __alignof__(struct kvm_vcpu), 0, 0); |
| 1453 | if (!kvm_vcpu_cache) { |
| 1454 | r = -ENOMEM; |
| 1455 | goto out_free_4; |
| 1456 | } |
| 1457 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1458 | kvm_chardev_ops.owner = module; |
| 1459 | |
| 1460 | r = misc_register(&kvm_dev); |
| 1461 | if (r) { |
Mike Day | d77c26f | 2007-10-08 09:02:08 -0400 | [diff] [blame] | 1462 | printk(KERN_ERR "kvm: misc device register failed\n"); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1463 | goto out_free; |
| 1464 | } |
| 1465 | |
Avi Kivity | 15ad714 | 2007-07-11 18:17:21 +0300 | [diff] [blame] | 1466 | kvm_preempt_ops.sched_in = kvm_sched_in; |
| 1467 | kvm_preempt_ops.sched_out = kvm_sched_out; |
| 1468 | |
Avi Kivity | c7addb9 | 2007-09-16 18:58:32 +0200 | [diff] [blame] | 1469 | kvm_mmu_set_nonpresent_ptes(0ull, 0ull); |
| 1470 | |
| 1471 | return 0; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1472 | |
| 1473 | out_free: |
Rusty Russell | c16f862 | 2007-07-30 21:12:19 +1000 | [diff] [blame] | 1474 | kmem_cache_destroy(kvm_vcpu_cache); |
| 1475 | out_free_4: |
Avi Kivity | 59ae6c6 | 2007-02-12 00:54:48 -0800 | [diff] [blame] | 1476 | sysdev_unregister(&kvm_sysdev); |
| 1477 | out_free_3: |
| 1478 | sysdev_class_unregister(&kvm_sysdev_class); |
| 1479 | out_free_2: |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1480 | unregister_reboot_notifier(&kvm_reboot_notifier); |
Avi Kivity | 774c47f | 2007-02-12 00:54:47 -0800 | [diff] [blame] | 1481 | unregister_cpu_notifier(&kvm_cpu_notifier); |
| 1482 | out_free_1: |
Avi Kivity | 1b6c016 | 2007-05-24 13:03:52 +0300 | [diff] [blame] | 1483 | on_each_cpu(hardware_disable, NULL, 0, 1); |
Yang, Sheng | 002c7f7 | 2007-07-31 14:23:01 +0300 | [diff] [blame] | 1484 | out_free_0: |
Zhang Xiantao | e9b11c1 | 2007-11-14 20:38:21 +0800 | [diff] [blame] | 1485 | kvm_arch_hardware_unsetup(); |
Avi Kivity | ca45aaa | 2007-03-01 19:21:03 +0200 | [diff] [blame] | 1486 | out: |
Zhang Xiantao | f8c16bb | 2007-11-14 20:40:21 +0800 | [diff] [blame] | 1487 | kvm_arch_exit(); |
Zhang Xiantao | cb498ea | 2007-11-14 20:39:31 +0800 | [diff] [blame] | 1488 | kvm_exit_debug(); |
| 1489 | kvm_mmu_module_exit(); |
| 1490 | out4: |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1491 | return r; |
| 1492 | } |
Zhang Xiantao | cb498ea | 2007-11-14 20:39:31 +0800 | [diff] [blame] | 1493 | EXPORT_SYMBOL_GPL(kvm_init); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1494 | |
Zhang Xiantao | cb498ea | 2007-11-14 20:39:31 +0800 | [diff] [blame] | 1495 | void kvm_exit(void) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1496 | { |
| 1497 | misc_deregister(&kvm_dev); |
Rusty Russell | c16f862 | 2007-07-30 21:12:19 +1000 | [diff] [blame] | 1498 | kmem_cache_destroy(kvm_vcpu_cache); |
Avi Kivity | 59ae6c6 | 2007-02-12 00:54:48 -0800 | [diff] [blame] | 1499 | sysdev_unregister(&kvm_sysdev); |
| 1500 | sysdev_class_unregister(&kvm_sysdev_class); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1501 | unregister_reboot_notifier(&kvm_reboot_notifier); |
Avi Kivity | 59ae6c6 | 2007-02-12 00:54:48 -0800 | [diff] [blame] | 1502 | unregister_cpu_notifier(&kvm_cpu_notifier); |
Avi Kivity | 1b6c016 | 2007-05-24 13:03:52 +0300 | [diff] [blame] | 1503 | on_each_cpu(hardware_disable, NULL, 0, 1); |
Zhang Xiantao | e9b11c1 | 2007-11-14 20:38:21 +0800 | [diff] [blame] | 1504 | kvm_arch_hardware_unsetup(); |
Zhang Xiantao | f8c16bb | 2007-11-14 20:40:21 +0800 | [diff] [blame] | 1505 | kvm_arch_exit(); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1506 | kvm_exit_debug(); |
Izik Eidus | cea7bb2 | 2007-10-17 19:17:48 +0200 | [diff] [blame] | 1507 | __free_page(bad_page); |
Avi Kivity | b5a33a7 | 2007-04-15 16:31:09 +0300 | [diff] [blame] | 1508 | kvm_mmu_module_exit(); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1509 | } |
Zhang Xiantao | cb498ea | 2007-11-14 20:39:31 +0800 | [diff] [blame] | 1510 | EXPORT_SYMBOL_GPL(kvm_exit); |