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