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