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