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