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