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" |
| 19 | |
| 20 | #include <linux/kvm.h> |
| 21 | #include <linux/module.h> |
| 22 | #include <linux/errno.h> |
Andrew Morton | e9cdb1e | 2007-03-01 11:28:13 +0200 | [diff] [blame] | 23 | #include <linux/magic.h> |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 24 | #include <asm/processor.h> |
| 25 | #include <linux/percpu.h> |
| 26 | #include <linux/gfp.h> |
| 27 | #include <asm/msr.h> |
| 28 | #include <linux/mm.h> |
| 29 | #include <linux/miscdevice.h> |
| 30 | #include <linux/vmalloc.h> |
| 31 | #include <asm/uaccess.h> |
| 32 | #include <linux/reboot.h> |
| 33 | #include <asm/io.h> |
| 34 | #include <linux/debugfs.h> |
| 35 | #include <linux/highmem.h> |
| 36 | #include <linux/file.h> |
| 37 | #include <asm/desc.h> |
Avi Kivity | 59ae6c6 | 2007-02-12 00:54:48 -0800 | [diff] [blame] | 38 | #include <linux/sysdev.h> |
Avi Kivity | 774c47f | 2007-02-12 00:54:47 -0800 | [diff] [blame] | 39 | #include <linux/cpu.h> |
Avi Kivity | f17abe9 | 2007-02-21 19:28:04 +0200 | [diff] [blame] | 40 | #include <linux/file.h> |
Avi Kivity | 37e29d9 | 2007-02-20 14:07:37 +0200 | [diff] [blame] | 41 | #include <linux/fs.h> |
| 42 | #include <linux/mount.h> |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 43 | |
| 44 | #include "x86_emulate.h" |
| 45 | #include "segment_descriptor.h" |
| 46 | |
| 47 | MODULE_AUTHOR("Qumranet"); |
| 48 | MODULE_LICENSE("GPL"); |
| 49 | |
Avi Kivity | 133de90 | 2007-02-12 00:54:44 -0800 | [diff] [blame] | 50 | static DEFINE_SPINLOCK(kvm_lock); |
| 51 | static LIST_HEAD(vm_list); |
| 52 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 53 | struct kvm_arch_ops *kvm_arch_ops; |
Avi Kivity | 1165f5f | 2007-04-19 17:27:43 +0300 | [diff] [blame^] | 54 | |
| 55 | #define STAT_OFFSET(x) offsetof(struct kvm_vcpu, stat.x) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 56 | |
| 57 | static struct kvm_stats_debugfs_item { |
| 58 | const char *name; |
Avi Kivity | 1165f5f | 2007-04-19 17:27:43 +0300 | [diff] [blame^] | 59 | int offset; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 60 | struct dentry *dentry; |
| 61 | } debugfs_entries[] = { |
Avi Kivity | 1165f5f | 2007-04-19 17:27:43 +0300 | [diff] [blame^] | 62 | { "pf_fixed", STAT_OFFSET(pf_fixed) }, |
| 63 | { "pf_guest", STAT_OFFSET(pf_guest) }, |
| 64 | { "tlb_flush", STAT_OFFSET(tlb_flush) }, |
| 65 | { "invlpg", STAT_OFFSET(invlpg) }, |
| 66 | { "exits", STAT_OFFSET(exits) }, |
| 67 | { "io_exits", STAT_OFFSET(io_exits) }, |
| 68 | { "mmio_exits", STAT_OFFSET(mmio_exits) }, |
| 69 | { "signal_exits", STAT_OFFSET(signal_exits) }, |
| 70 | { "irq_window", STAT_OFFSET(irq_window_exits) }, |
| 71 | { "halt_exits", STAT_OFFSET(halt_exits) }, |
| 72 | { "request_irq", STAT_OFFSET(request_irq_exits) }, |
| 73 | { "irq_exits", STAT_OFFSET(irq_exits) }, |
| 74 | { NULL } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 75 | }; |
| 76 | |
| 77 | static struct dentry *debugfs_dir; |
| 78 | |
Avi Kivity | 37e29d9 | 2007-02-20 14:07:37 +0200 | [diff] [blame] | 79 | struct vfsmount *kvmfs_mnt; |
| 80 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 81 | #define MAX_IO_MSRS 256 |
| 82 | |
| 83 | #define CR0_RESEVED_BITS 0xffffffff1ffaffc0ULL |
| 84 | #define LMSW_GUEST_MASK 0x0eULL |
| 85 | #define CR4_RESEVED_BITS (~((1ULL << 11) - 1)) |
| 86 | #define CR8_RESEVED_BITS (~0x0fULL) |
| 87 | #define EFER_RESERVED_BITS 0xfffffffffffff2fe |
| 88 | |
Avi Kivity | 05b3e0c | 2006-12-13 00:33:45 -0800 | [diff] [blame] | 89 | #ifdef CONFIG_X86_64 |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 90 | // LDT or TSS descriptor in the GDT. 16 bytes. |
| 91 | struct segment_descriptor_64 { |
| 92 | struct segment_descriptor s; |
| 93 | u32 base_higher; |
| 94 | u32 pad_zero; |
| 95 | }; |
| 96 | |
| 97 | #endif |
| 98 | |
Avi Kivity | bccf215 | 2007-02-21 18:04:26 +0200 | [diff] [blame] | 99 | static long kvm_vcpu_ioctl(struct file *file, unsigned int ioctl, |
| 100 | unsigned long arg); |
| 101 | |
Avi Kivity | f17abe9 | 2007-02-21 19:28:04 +0200 | [diff] [blame] | 102 | static struct inode *kvmfs_inode(struct file_operations *fops) |
| 103 | { |
| 104 | int error = -ENOMEM; |
| 105 | struct inode *inode = new_inode(kvmfs_mnt->mnt_sb); |
| 106 | |
| 107 | if (!inode) |
| 108 | goto eexit_1; |
| 109 | |
| 110 | inode->i_fop = fops; |
| 111 | |
| 112 | /* |
| 113 | * Mark the inode dirty from the very beginning, |
| 114 | * that way it will never be moved to the dirty |
| 115 | * list because mark_inode_dirty() will think |
| 116 | * that it already _is_ on the dirty list. |
| 117 | */ |
| 118 | inode->i_state = I_DIRTY; |
| 119 | inode->i_mode = S_IRUSR | S_IWUSR; |
| 120 | inode->i_uid = current->fsuid; |
| 121 | inode->i_gid = current->fsgid; |
| 122 | inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME; |
| 123 | return inode; |
| 124 | |
| 125 | eexit_1: |
| 126 | return ERR_PTR(error); |
| 127 | } |
| 128 | |
| 129 | static struct file *kvmfs_file(struct inode *inode, void *private_data) |
| 130 | { |
| 131 | struct file *file = get_empty_filp(); |
| 132 | |
| 133 | if (!file) |
| 134 | return ERR_PTR(-ENFILE); |
| 135 | |
| 136 | file->f_path.mnt = mntget(kvmfs_mnt); |
| 137 | file->f_path.dentry = d_alloc_anon(inode); |
| 138 | if (!file->f_path.dentry) |
| 139 | return ERR_PTR(-ENOMEM); |
| 140 | file->f_mapping = inode->i_mapping; |
| 141 | |
| 142 | file->f_pos = 0; |
| 143 | file->f_flags = O_RDWR; |
| 144 | file->f_op = inode->i_fop; |
| 145 | file->f_mode = FMODE_READ | FMODE_WRITE; |
| 146 | file->f_version = 0; |
| 147 | file->private_data = private_data; |
| 148 | return file; |
| 149 | } |
| 150 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 151 | unsigned long segment_base(u16 selector) |
| 152 | { |
| 153 | struct descriptor_table gdt; |
| 154 | struct segment_descriptor *d; |
| 155 | unsigned long table_base; |
| 156 | typedef unsigned long ul; |
| 157 | unsigned long v; |
| 158 | |
| 159 | if (selector == 0) |
| 160 | return 0; |
| 161 | |
| 162 | asm ("sgdt %0" : "=m"(gdt)); |
| 163 | table_base = gdt.base; |
| 164 | |
| 165 | if (selector & 4) { /* from ldt */ |
| 166 | u16 ldt_selector; |
| 167 | |
| 168 | asm ("sldt %0" : "=g"(ldt_selector)); |
| 169 | table_base = segment_base(ldt_selector); |
| 170 | } |
| 171 | d = (struct segment_descriptor *)(table_base + (selector & ~7)); |
| 172 | v = d->base_low | ((ul)d->base_mid << 16) | ((ul)d->base_high << 24); |
Avi Kivity | 05b3e0c | 2006-12-13 00:33:45 -0800 | [diff] [blame] | 173 | #ifdef CONFIG_X86_64 |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 174 | if (d->system == 0 |
| 175 | && (d->type == 2 || d->type == 9 || d->type == 11)) |
| 176 | v |= ((ul)((struct segment_descriptor_64 *)d)->base_higher) << 32; |
| 177 | #endif |
| 178 | return v; |
| 179 | } |
| 180 | EXPORT_SYMBOL_GPL(segment_base); |
| 181 | |
James Morris | 5aacf0c | 2006-12-22 01:04:55 -0800 | [diff] [blame] | 182 | static inline int valid_vcpu(int n) |
| 183 | { |
| 184 | return likely(n >= 0 && n < KVM_MAX_VCPUS); |
| 185 | } |
| 186 | |
Avi Kivity | d27d4ac | 2007-02-19 14:37:46 +0200 | [diff] [blame] | 187 | int kvm_read_guest(struct kvm_vcpu *vcpu, gva_t addr, unsigned long size, |
| 188 | void *dest) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 189 | { |
| 190 | unsigned char *host_buf = dest; |
| 191 | unsigned long req_size = size; |
| 192 | |
| 193 | while (size) { |
| 194 | hpa_t paddr; |
| 195 | unsigned now; |
| 196 | unsigned offset; |
| 197 | hva_t guest_buf; |
| 198 | |
| 199 | paddr = gva_to_hpa(vcpu, addr); |
| 200 | |
| 201 | if (is_error_hpa(paddr)) |
| 202 | break; |
| 203 | |
| 204 | guest_buf = (hva_t)kmap_atomic( |
| 205 | pfn_to_page(paddr >> PAGE_SHIFT), |
| 206 | KM_USER0); |
| 207 | offset = addr & ~PAGE_MASK; |
| 208 | guest_buf |= offset; |
| 209 | now = min(size, PAGE_SIZE - offset); |
| 210 | memcpy(host_buf, (void*)guest_buf, now); |
| 211 | host_buf += now; |
| 212 | addr += now; |
| 213 | size -= now; |
| 214 | kunmap_atomic((void *)(guest_buf & PAGE_MASK), KM_USER0); |
| 215 | } |
| 216 | return req_size - size; |
| 217 | } |
| 218 | EXPORT_SYMBOL_GPL(kvm_read_guest); |
| 219 | |
Avi Kivity | d27d4ac | 2007-02-19 14:37:46 +0200 | [diff] [blame] | 220 | int kvm_write_guest(struct kvm_vcpu *vcpu, gva_t addr, unsigned long size, |
| 221 | void *data) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 222 | { |
| 223 | unsigned char *host_buf = data; |
| 224 | unsigned long req_size = size; |
| 225 | |
| 226 | while (size) { |
| 227 | hpa_t paddr; |
| 228 | unsigned now; |
| 229 | unsigned offset; |
| 230 | hva_t guest_buf; |
Uri Lublin | ab51a43 | 2007-02-21 18:25:21 +0200 | [diff] [blame] | 231 | gfn_t gfn; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 232 | |
| 233 | paddr = gva_to_hpa(vcpu, addr); |
| 234 | |
| 235 | if (is_error_hpa(paddr)) |
| 236 | break; |
| 237 | |
Uri Lublin | ab51a43 | 2007-02-21 18:25:21 +0200 | [diff] [blame] | 238 | gfn = vcpu->mmu.gva_to_gpa(vcpu, addr) >> PAGE_SHIFT; |
| 239 | mark_page_dirty(vcpu->kvm, gfn); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 240 | guest_buf = (hva_t)kmap_atomic( |
| 241 | pfn_to_page(paddr >> PAGE_SHIFT), KM_USER0); |
| 242 | offset = addr & ~PAGE_MASK; |
| 243 | guest_buf |= offset; |
| 244 | now = min(size, PAGE_SIZE - offset); |
| 245 | memcpy((void*)guest_buf, host_buf, now); |
| 246 | host_buf += now; |
| 247 | addr += now; |
| 248 | size -= now; |
| 249 | kunmap_atomic((void *)(guest_buf & PAGE_MASK), KM_USER0); |
| 250 | } |
| 251 | return req_size - size; |
| 252 | } |
| 253 | EXPORT_SYMBOL_GPL(kvm_write_guest); |
| 254 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 255 | /* |
| 256 | * Switches to specified vcpu, until a matching vcpu_put() |
| 257 | */ |
Avi Kivity | bccf215 | 2007-02-21 18:04:26 +0200 | [diff] [blame] | 258 | static void vcpu_load(struct kvm_vcpu *vcpu) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 259 | { |
Avi Kivity | bccf215 | 2007-02-21 18:04:26 +0200 | [diff] [blame] | 260 | mutex_lock(&vcpu->mutex); |
| 261 | kvm_arch_ops->vcpu_load(vcpu); |
| 262 | } |
| 263 | |
| 264 | /* |
| 265 | * Switches to specified vcpu, until a matching vcpu_put(). Will return NULL |
| 266 | * if the slot is not populated. |
| 267 | */ |
| 268 | static struct kvm_vcpu *vcpu_load_slot(struct kvm *kvm, int slot) |
| 269 | { |
| 270 | struct kvm_vcpu *vcpu = &kvm->vcpus[slot]; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 271 | |
| 272 | mutex_lock(&vcpu->mutex); |
Avi Kivity | bccf215 | 2007-02-21 18:04:26 +0200 | [diff] [blame] | 273 | if (!vcpu->vmcs) { |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 274 | mutex_unlock(&vcpu->mutex); |
Al Viro | 8b6d44c | 2007-02-09 16:38:40 +0000 | [diff] [blame] | 275 | return NULL; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 276 | } |
Avi Kivity | bccf215 | 2007-02-21 18:04:26 +0200 | [diff] [blame] | 277 | kvm_arch_ops->vcpu_load(vcpu); |
| 278 | return vcpu; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 279 | } |
| 280 | |
| 281 | static void vcpu_put(struct kvm_vcpu *vcpu) |
| 282 | { |
| 283 | kvm_arch_ops->vcpu_put(vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 284 | mutex_unlock(&vcpu->mutex); |
| 285 | } |
| 286 | |
Avi Kivity | f17abe9 | 2007-02-21 19:28:04 +0200 | [diff] [blame] | 287 | static struct kvm *kvm_create_vm(void) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 288 | { |
| 289 | struct kvm *kvm = kzalloc(sizeof(struct kvm), GFP_KERNEL); |
| 290 | int i; |
| 291 | |
| 292 | if (!kvm) |
Avi Kivity | f17abe9 | 2007-02-21 19:28:04 +0200 | [diff] [blame] | 293 | return ERR_PTR(-ENOMEM); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 294 | |
| 295 | spin_lock_init(&kvm->lock); |
| 296 | INIT_LIST_HEAD(&kvm->active_mmu_pages); |
| 297 | for (i = 0; i < KVM_MAX_VCPUS; ++i) { |
| 298 | struct kvm_vcpu *vcpu = &kvm->vcpus[i]; |
| 299 | |
| 300 | mutex_init(&vcpu->mutex); |
Avi Kivity | 133de90 | 2007-02-12 00:54:44 -0800 | [diff] [blame] | 301 | vcpu->cpu = -1; |
Avi Kivity | 86a2b42 | 2007-01-05 16:36:57 -0800 | [diff] [blame] | 302 | vcpu->kvm = kvm; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 303 | vcpu->mmu.root_hpa = INVALID_PAGE; |
| 304 | INIT_LIST_HEAD(&vcpu->free_pages); |
Avi Kivity | 133de90 | 2007-02-12 00:54:44 -0800 | [diff] [blame] | 305 | spin_lock(&kvm_lock); |
| 306 | list_add(&kvm->vm_list, &vm_list); |
| 307 | spin_unlock(&kvm_lock); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 308 | } |
Avi Kivity | f17abe9 | 2007-02-21 19:28:04 +0200 | [diff] [blame] | 309 | return kvm; |
| 310 | } |
| 311 | |
| 312 | static int kvm_dev_open(struct inode *inode, struct file *filp) |
| 313 | { |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 314 | return 0; |
| 315 | } |
| 316 | |
| 317 | /* |
| 318 | * Free any memory in @free but not in @dont. |
| 319 | */ |
| 320 | static void kvm_free_physmem_slot(struct kvm_memory_slot *free, |
| 321 | struct kvm_memory_slot *dont) |
| 322 | { |
| 323 | int i; |
| 324 | |
| 325 | if (!dont || free->phys_mem != dont->phys_mem) |
| 326 | if (free->phys_mem) { |
| 327 | for (i = 0; i < free->npages; ++i) |
Avi Kivity | 55a54f7 | 2006-12-29 16:49:58 -0800 | [diff] [blame] | 328 | if (free->phys_mem[i]) |
| 329 | __free_page(free->phys_mem[i]); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 330 | vfree(free->phys_mem); |
| 331 | } |
| 332 | |
| 333 | if (!dont || free->dirty_bitmap != dont->dirty_bitmap) |
| 334 | vfree(free->dirty_bitmap); |
| 335 | |
Al Viro | 8b6d44c | 2007-02-09 16:38:40 +0000 | [diff] [blame] | 336 | free->phys_mem = NULL; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 337 | free->npages = 0; |
Al Viro | 8b6d44c | 2007-02-09 16:38:40 +0000 | [diff] [blame] | 338 | free->dirty_bitmap = NULL; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 339 | } |
| 340 | |
| 341 | static void kvm_free_physmem(struct kvm *kvm) |
| 342 | { |
| 343 | int i; |
| 344 | |
| 345 | for (i = 0; i < kvm->nmemslots; ++i) |
Al Viro | 8b6d44c | 2007-02-09 16:38:40 +0000 | [diff] [blame] | 346 | kvm_free_physmem_slot(&kvm->memslots[i], NULL); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 347 | } |
| 348 | |
Avi Kivity | 039576c | 2007-03-20 12:46:50 +0200 | [diff] [blame] | 349 | static void free_pio_guest_pages(struct kvm_vcpu *vcpu) |
| 350 | { |
| 351 | int i; |
| 352 | |
| 353 | for (i = 0; i < 2; ++i) |
| 354 | if (vcpu->pio.guest_pages[i]) { |
| 355 | __free_page(vcpu->pio.guest_pages[i]); |
| 356 | vcpu->pio.guest_pages[i] = NULL; |
| 357 | } |
| 358 | } |
| 359 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 360 | static void kvm_free_vcpu(struct kvm_vcpu *vcpu) |
| 361 | { |
Avi Kivity | bccf215 | 2007-02-21 18:04:26 +0200 | [diff] [blame] | 362 | if (!vcpu->vmcs) |
Ingo Molnar | 1e8ba6f | 2007-02-12 00:54:42 -0800 | [diff] [blame] | 363 | return; |
| 364 | |
Avi Kivity | bccf215 | 2007-02-21 18:04:26 +0200 | [diff] [blame] | 365 | vcpu_load(vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 366 | kvm_mmu_destroy(vcpu); |
Avi Kivity | 0843847 | 2007-01-22 20:40:38 -0800 | [diff] [blame] | 367 | vcpu_put(vcpu); |
Avi Kivity | 9ede74e | 2007-01-05 16:36:55 -0800 | [diff] [blame] | 368 | kvm_arch_ops->vcpu_free(vcpu); |
Avi Kivity | 9a2bb7f | 2007-02-22 12:58:31 +0200 | [diff] [blame] | 369 | free_page((unsigned long)vcpu->run); |
| 370 | vcpu->run = NULL; |
Avi Kivity | 039576c | 2007-03-20 12:46:50 +0200 | [diff] [blame] | 371 | free_page((unsigned long)vcpu->pio_data); |
| 372 | vcpu->pio_data = NULL; |
| 373 | free_pio_guest_pages(vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 374 | } |
| 375 | |
| 376 | static void kvm_free_vcpus(struct kvm *kvm) |
| 377 | { |
| 378 | unsigned int i; |
| 379 | |
| 380 | for (i = 0; i < KVM_MAX_VCPUS; ++i) |
| 381 | kvm_free_vcpu(&kvm->vcpus[i]); |
| 382 | } |
| 383 | |
| 384 | static int kvm_dev_release(struct inode *inode, struct file *filp) |
| 385 | { |
Avi Kivity | f17abe9 | 2007-02-21 19:28:04 +0200 | [diff] [blame] | 386 | return 0; |
| 387 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 388 | |
Avi Kivity | f17abe9 | 2007-02-21 19:28:04 +0200 | [diff] [blame] | 389 | static void kvm_destroy_vm(struct kvm *kvm) |
| 390 | { |
Avi Kivity | 133de90 | 2007-02-12 00:54:44 -0800 | [diff] [blame] | 391 | spin_lock(&kvm_lock); |
| 392 | list_del(&kvm->vm_list); |
| 393 | spin_unlock(&kvm_lock); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 394 | kvm_free_vcpus(kvm); |
| 395 | kvm_free_physmem(kvm); |
| 396 | kfree(kvm); |
Avi Kivity | f17abe9 | 2007-02-21 19:28:04 +0200 | [diff] [blame] | 397 | } |
| 398 | |
| 399 | static int kvm_vm_release(struct inode *inode, struct file *filp) |
| 400 | { |
| 401 | struct kvm *kvm = filp->private_data; |
| 402 | |
| 403 | kvm_destroy_vm(kvm); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 404 | return 0; |
| 405 | } |
| 406 | |
| 407 | static void inject_gp(struct kvm_vcpu *vcpu) |
| 408 | { |
| 409 | kvm_arch_ops->inject_gp(vcpu, 0); |
| 410 | } |
| 411 | |
Avi Kivity | 1342d35 | 2007-01-05 16:36:39 -0800 | [diff] [blame] | 412 | /* |
| 413 | * Load the pae pdptrs. Return true is they are all valid. |
| 414 | */ |
| 415 | static int load_pdptrs(struct kvm_vcpu *vcpu, unsigned long cr3) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 416 | { |
| 417 | gfn_t pdpt_gfn = cr3 >> PAGE_SHIFT; |
Avi Kivity | 1342d35 | 2007-01-05 16:36:39 -0800 | [diff] [blame] | 418 | unsigned offset = ((cr3 & (PAGE_SIZE-1)) >> 5) << 2; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 419 | int i; |
| 420 | u64 pdpte; |
| 421 | u64 *pdpt; |
Avi Kivity | 1342d35 | 2007-01-05 16:36:39 -0800 | [diff] [blame] | 422 | int ret; |
Avi Kivity | 954bbbc2 | 2007-03-30 14:02:32 +0300 | [diff] [blame] | 423 | struct page *page; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 424 | |
| 425 | spin_lock(&vcpu->kvm->lock); |
Avi Kivity | 954bbbc2 | 2007-03-30 14:02:32 +0300 | [diff] [blame] | 426 | page = gfn_to_page(vcpu->kvm, pdpt_gfn); |
| 427 | /* FIXME: !page - emulate? 0xff? */ |
| 428 | pdpt = kmap_atomic(page, KM_USER0); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 429 | |
Avi Kivity | 1342d35 | 2007-01-05 16:36:39 -0800 | [diff] [blame] | 430 | ret = 1; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 431 | for (i = 0; i < 4; ++i) { |
| 432 | pdpte = pdpt[offset + i]; |
Avi Kivity | 1342d35 | 2007-01-05 16:36:39 -0800 | [diff] [blame] | 433 | if ((pdpte & 1) && (pdpte & 0xfffffff0000001e6ull)) { |
| 434 | ret = 0; |
| 435 | goto out; |
| 436 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 437 | } |
| 438 | |
Avi Kivity | 1342d35 | 2007-01-05 16:36:39 -0800 | [diff] [blame] | 439 | for (i = 0; i < 4; ++i) |
| 440 | vcpu->pdptrs[i] = pdpt[offset + i]; |
| 441 | |
| 442 | out: |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 443 | kunmap_atomic(pdpt, KM_USER0); |
| 444 | spin_unlock(&vcpu->kvm->lock); |
| 445 | |
Avi Kivity | 1342d35 | 2007-01-05 16:36:39 -0800 | [diff] [blame] | 446 | return ret; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 447 | } |
| 448 | |
| 449 | void set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0) |
| 450 | { |
| 451 | if (cr0 & CR0_RESEVED_BITS) { |
| 452 | printk(KERN_DEBUG "set_cr0: 0x%lx #GP, reserved bits 0x%lx\n", |
| 453 | cr0, vcpu->cr0); |
| 454 | inject_gp(vcpu); |
| 455 | return; |
| 456 | } |
| 457 | |
| 458 | if ((cr0 & CR0_NW_MASK) && !(cr0 & CR0_CD_MASK)) { |
| 459 | printk(KERN_DEBUG "set_cr0: #GP, CD == 0 && NW == 1\n"); |
| 460 | inject_gp(vcpu); |
| 461 | return; |
| 462 | } |
| 463 | |
| 464 | if ((cr0 & CR0_PG_MASK) && !(cr0 & CR0_PE_MASK)) { |
| 465 | printk(KERN_DEBUG "set_cr0: #GP, set PG flag " |
| 466 | "and a clear PE flag\n"); |
| 467 | inject_gp(vcpu); |
| 468 | return; |
| 469 | } |
| 470 | |
| 471 | if (!is_paging(vcpu) && (cr0 & CR0_PG_MASK)) { |
Avi Kivity | 05b3e0c | 2006-12-13 00:33:45 -0800 | [diff] [blame] | 472 | #ifdef CONFIG_X86_64 |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 473 | if ((vcpu->shadow_efer & EFER_LME)) { |
| 474 | int cs_db, cs_l; |
| 475 | |
| 476 | if (!is_pae(vcpu)) { |
| 477 | printk(KERN_DEBUG "set_cr0: #GP, start paging " |
| 478 | "in long mode while PAE is disabled\n"); |
| 479 | inject_gp(vcpu); |
| 480 | return; |
| 481 | } |
| 482 | kvm_arch_ops->get_cs_db_l_bits(vcpu, &cs_db, &cs_l); |
| 483 | if (cs_l) { |
| 484 | printk(KERN_DEBUG "set_cr0: #GP, start paging " |
| 485 | "in long mode while CS.L == 1\n"); |
| 486 | inject_gp(vcpu); |
| 487 | return; |
| 488 | |
| 489 | } |
| 490 | } else |
| 491 | #endif |
Avi Kivity | 1342d35 | 2007-01-05 16:36:39 -0800 | [diff] [blame] | 492 | if (is_pae(vcpu) && !load_pdptrs(vcpu, vcpu->cr3)) { |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 493 | printk(KERN_DEBUG "set_cr0: #GP, pdptrs " |
| 494 | "reserved bits\n"); |
| 495 | inject_gp(vcpu); |
| 496 | return; |
| 497 | } |
| 498 | |
| 499 | } |
| 500 | |
| 501 | kvm_arch_ops->set_cr0(vcpu, cr0); |
| 502 | vcpu->cr0 = cr0; |
| 503 | |
| 504 | spin_lock(&vcpu->kvm->lock); |
| 505 | kvm_mmu_reset_context(vcpu); |
| 506 | spin_unlock(&vcpu->kvm->lock); |
| 507 | return; |
| 508 | } |
| 509 | EXPORT_SYMBOL_GPL(set_cr0); |
| 510 | |
| 511 | void lmsw(struct kvm_vcpu *vcpu, unsigned long msw) |
| 512 | { |
Avi Kivity | 399badf | 2007-01-05 16:36:38 -0800 | [diff] [blame] | 513 | kvm_arch_ops->decache_cr0_cr4_guest_bits(vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 514 | set_cr0(vcpu, (vcpu->cr0 & ~0x0ful) | (msw & 0x0f)); |
| 515 | } |
| 516 | EXPORT_SYMBOL_GPL(lmsw); |
| 517 | |
| 518 | void set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4) |
| 519 | { |
| 520 | if (cr4 & CR4_RESEVED_BITS) { |
| 521 | printk(KERN_DEBUG "set_cr4: #GP, reserved bits\n"); |
| 522 | inject_gp(vcpu); |
| 523 | return; |
| 524 | } |
| 525 | |
Avi Kivity | a9058ec | 2006-12-29 16:49:37 -0800 | [diff] [blame] | 526 | if (is_long_mode(vcpu)) { |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 527 | if (!(cr4 & CR4_PAE_MASK)) { |
| 528 | printk(KERN_DEBUG "set_cr4: #GP, clearing PAE while " |
| 529 | "in long mode\n"); |
| 530 | inject_gp(vcpu); |
| 531 | return; |
| 532 | } |
| 533 | } else if (is_paging(vcpu) && !is_pae(vcpu) && (cr4 & CR4_PAE_MASK) |
Avi Kivity | 1342d35 | 2007-01-05 16:36:39 -0800 | [diff] [blame] | 534 | && !load_pdptrs(vcpu, vcpu->cr3)) { |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 535 | printk(KERN_DEBUG "set_cr4: #GP, pdptrs reserved bits\n"); |
| 536 | inject_gp(vcpu); |
| 537 | } |
| 538 | |
| 539 | if (cr4 & CR4_VMXE_MASK) { |
| 540 | printk(KERN_DEBUG "set_cr4: #GP, setting VMXE\n"); |
| 541 | inject_gp(vcpu); |
| 542 | return; |
| 543 | } |
| 544 | kvm_arch_ops->set_cr4(vcpu, cr4); |
| 545 | spin_lock(&vcpu->kvm->lock); |
| 546 | kvm_mmu_reset_context(vcpu); |
| 547 | spin_unlock(&vcpu->kvm->lock); |
| 548 | } |
| 549 | EXPORT_SYMBOL_GPL(set_cr4); |
| 550 | |
| 551 | void set_cr3(struct kvm_vcpu *vcpu, unsigned long cr3) |
| 552 | { |
Avi Kivity | a9058ec | 2006-12-29 16:49:37 -0800 | [diff] [blame] | 553 | if (is_long_mode(vcpu)) { |
Avi Kivity | d27d4ac | 2007-02-19 14:37:46 +0200 | [diff] [blame] | 554 | if (cr3 & CR3_L_MODE_RESEVED_BITS) { |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 555 | printk(KERN_DEBUG "set_cr3: #GP, reserved bits\n"); |
| 556 | inject_gp(vcpu); |
| 557 | return; |
| 558 | } |
| 559 | } else { |
| 560 | if (cr3 & CR3_RESEVED_BITS) { |
| 561 | printk(KERN_DEBUG "set_cr3: #GP, reserved bits\n"); |
| 562 | inject_gp(vcpu); |
| 563 | return; |
| 564 | } |
| 565 | if (is_paging(vcpu) && is_pae(vcpu) && |
Avi Kivity | 1342d35 | 2007-01-05 16:36:39 -0800 | [diff] [blame] | 566 | !load_pdptrs(vcpu, cr3)) { |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 567 | printk(KERN_DEBUG "set_cr3: #GP, pdptrs " |
| 568 | "reserved bits\n"); |
| 569 | inject_gp(vcpu); |
| 570 | return; |
| 571 | } |
| 572 | } |
| 573 | |
| 574 | vcpu->cr3 = cr3; |
| 575 | spin_lock(&vcpu->kvm->lock); |
Ingo Molnar | d21225e | 2007-01-05 16:36:59 -0800 | [diff] [blame] | 576 | /* |
| 577 | * Does the new cr3 value map to physical memory? (Note, we |
| 578 | * catch an invalid cr3 even in real-mode, because it would |
| 579 | * cause trouble later on when we turn on paging anyway.) |
| 580 | * |
| 581 | * A real CPU would silently accept an invalid cr3 and would |
| 582 | * attempt to use it - with largely undefined (and often hard |
| 583 | * to debug) behavior on the guest side. |
| 584 | */ |
| 585 | if (unlikely(!gfn_to_memslot(vcpu->kvm, cr3 >> PAGE_SHIFT))) |
| 586 | inject_gp(vcpu); |
| 587 | else |
| 588 | vcpu->mmu.new_cr3(vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 589 | spin_unlock(&vcpu->kvm->lock); |
| 590 | } |
| 591 | EXPORT_SYMBOL_GPL(set_cr3); |
| 592 | |
| 593 | void set_cr8(struct kvm_vcpu *vcpu, unsigned long cr8) |
| 594 | { |
| 595 | if ( cr8 & CR8_RESEVED_BITS) { |
| 596 | printk(KERN_DEBUG "set_cr8: #GP, reserved bits 0x%lx\n", cr8); |
| 597 | inject_gp(vcpu); |
| 598 | return; |
| 599 | } |
| 600 | vcpu->cr8 = cr8; |
| 601 | } |
| 602 | EXPORT_SYMBOL_GPL(set_cr8); |
| 603 | |
| 604 | void fx_init(struct kvm_vcpu *vcpu) |
| 605 | { |
| 606 | struct __attribute__ ((__packed__)) fx_image_s { |
| 607 | u16 control; //fcw |
| 608 | u16 status; //fsw |
| 609 | u16 tag; // ftw |
| 610 | u16 opcode; //fop |
| 611 | u64 ip; // fpu ip |
| 612 | u64 operand;// fpu dp |
| 613 | u32 mxcsr; |
| 614 | u32 mxcsr_mask; |
| 615 | |
| 616 | } *fx_image; |
| 617 | |
| 618 | fx_save(vcpu->host_fx_image); |
| 619 | fpu_init(); |
| 620 | fx_save(vcpu->guest_fx_image); |
| 621 | fx_restore(vcpu->host_fx_image); |
| 622 | |
| 623 | fx_image = (struct fx_image_s *)vcpu->guest_fx_image; |
| 624 | fx_image->mxcsr = 0x1f80; |
| 625 | memset(vcpu->guest_fx_image + sizeof(struct fx_image_s), |
| 626 | 0, FX_IMAGE_SIZE - sizeof(struct fx_image_s)); |
| 627 | } |
| 628 | EXPORT_SYMBOL_GPL(fx_init); |
| 629 | |
Uri Lublin | 02b27c1 | 2007-02-22 17:15:33 +0200 | [diff] [blame] | 630 | static void do_remove_write_access(struct kvm_vcpu *vcpu, int slot) |
| 631 | { |
| 632 | spin_lock(&vcpu->kvm->lock); |
| 633 | kvm_mmu_slot_remove_write_access(vcpu, slot); |
| 634 | spin_unlock(&vcpu->kvm->lock); |
| 635 | } |
| 636 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 637 | /* |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 638 | * Allocate some memory and give it an address in the guest physical address |
| 639 | * space. |
| 640 | * |
| 641 | * Discontiguous memory is allowed, mostly for framebuffers. |
| 642 | */ |
Avi Kivity | 2c6f5df | 2007-02-20 18:27:58 +0200 | [diff] [blame] | 643 | static int kvm_vm_ioctl_set_memory_region(struct kvm *kvm, |
| 644 | struct kvm_memory_region *mem) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 645 | { |
| 646 | int r; |
| 647 | gfn_t base_gfn; |
| 648 | unsigned long npages; |
| 649 | unsigned long i; |
| 650 | struct kvm_memory_slot *memslot; |
| 651 | struct kvm_memory_slot old, new; |
| 652 | int memory_config_version; |
| 653 | |
| 654 | r = -EINVAL; |
| 655 | /* General sanity checks */ |
| 656 | if (mem->memory_size & (PAGE_SIZE - 1)) |
| 657 | goto out; |
| 658 | if (mem->guest_phys_addr & (PAGE_SIZE - 1)) |
| 659 | goto out; |
| 660 | if (mem->slot >= KVM_MEMORY_SLOTS) |
| 661 | goto out; |
| 662 | if (mem->guest_phys_addr + mem->memory_size < mem->guest_phys_addr) |
| 663 | goto out; |
| 664 | |
| 665 | memslot = &kvm->memslots[mem->slot]; |
| 666 | base_gfn = mem->guest_phys_addr >> PAGE_SHIFT; |
| 667 | npages = mem->memory_size >> PAGE_SHIFT; |
| 668 | |
| 669 | if (!npages) |
| 670 | mem->flags &= ~KVM_MEM_LOG_DIRTY_PAGES; |
| 671 | |
| 672 | raced: |
| 673 | spin_lock(&kvm->lock); |
| 674 | |
| 675 | memory_config_version = kvm->memory_config_version; |
| 676 | new = old = *memslot; |
| 677 | |
| 678 | new.base_gfn = base_gfn; |
| 679 | new.npages = npages; |
| 680 | new.flags = mem->flags; |
| 681 | |
| 682 | /* Disallow changing a memory slot's size. */ |
| 683 | r = -EINVAL; |
| 684 | if (npages && old.npages && npages != old.npages) |
| 685 | goto out_unlock; |
| 686 | |
| 687 | /* Check for overlaps */ |
| 688 | r = -EEXIST; |
| 689 | for (i = 0; i < KVM_MEMORY_SLOTS; ++i) { |
| 690 | struct kvm_memory_slot *s = &kvm->memslots[i]; |
| 691 | |
| 692 | if (s == memslot) |
| 693 | continue; |
| 694 | if (!((base_gfn + npages <= s->base_gfn) || |
| 695 | (base_gfn >= s->base_gfn + s->npages))) |
| 696 | goto out_unlock; |
| 697 | } |
| 698 | /* |
| 699 | * Do memory allocations outside lock. memory_config_version will |
| 700 | * detect any races. |
| 701 | */ |
| 702 | spin_unlock(&kvm->lock); |
| 703 | |
| 704 | /* Deallocate if slot is being removed */ |
| 705 | if (!npages) |
Al Viro | 8b6d44c | 2007-02-09 16:38:40 +0000 | [diff] [blame] | 706 | new.phys_mem = NULL; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 707 | |
| 708 | /* Free page dirty bitmap if unneeded */ |
| 709 | if (!(new.flags & KVM_MEM_LOG_DIRTY_PAGES)) |
Al Viro | 8b6d44c | 2007-02-09 16:38:40 +0000 | [diff] [blame] | 710 | new.dirty_bitmap = NULL; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 711 | |
| 712 | r = -ENOMEM; |
| 713 | |
| 714 | /* Allocate if a slot is being created */ |
| 715 | if (npages && !new.phys_mem) { |
| 716 | new.phys_mem = vmalloc(npages * sizeof(struct page *)); |
| 717 | |
| 718 | if (!new.phys_mem) |
| 719 | goto out_free; |
| 720 | |
| 721 | memset(new.phys_mem, 0, npages * sizeof(struct page *)); |
| 722 | for (i = 0; i < npages; ++i) { |
| 723 | new.phys_mem[i] = alloc_page(GFP_HIGHUSER |
| 724 | | __GFP_ZERO); |
| 725 | if (!new.phys_mem[i]) |
| 726 | goto out_free; |
Markus Rechberger | 5972e95 | 2007-02-19 14:37:47 +0200 | [diff] [blame] | 727 | set_page_private(new.phys_mem[i],0); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 728 | } |
| 729 | } |
| 730 | |
| 731 | /* Allocate page dirty bitmap if needed */ |
| 732 | if ((new.flags & KVM_MEM_LOG_DIRTY_PAGES) && !new.dirty_bitmap) { |
| 733 | unsigned dirty_bytes = ALIGN(npages, BITS_PER_LONG) / 8; |
| 734 | |
| 735 | new.dirty_bitmap = vmalloc(dirty_bytes); |
| 736 | if (!new.dirty_bitmap) |
| 737 | goto out_free; |
| 738 | memset(new.dirty_bitmap, 0, dirty_bytes); |
| 739 | } |
| 740 | |
| 741 | spin_lock(&kvm->lock); |
| 742 | |
| 743 | if (memory_config_version != kvm->memory_config_version) { |
| 744 | spin_unlock(&kvm->lock); |
| 745 | kvm_free_physmem_slot(&new, &old); |
| 746 | goto raced; |
| 747 | } |
| 748 | |
| 749 | r = -EAGAIN; |
| 750 | if (kvm->busy) |
| 751 | goto out_unlock; |
| 752 | |
| 753 | if (mem->slot >= kvm->nmemslots) |
| 754 | kvm->nmemslots = mem->slot + 1; |
| 755 | |
| 756 | *memslot = new; |
| 757 | ++kvm->memory_config_version; |
| 758 | |
| 759 | spin_unlock(&kvm->lock); |
| 760 | |
| 761 | for (i = 0; i < KVM_MAX_VCPUS; ++i) { |
| 762 | struct kvm_vcpu *vcpu; |
| 763 | |
Avi Kivity | bccf215 | 2007-02-21 18:04:26 +0200 | [diff] [blame] | 764 | vcpu = vcpu_load_slot(kvm, i); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 765 | if (!vcpu) |
| 766 | continue; |
Uri Lublin | ff990d5 | 2007-02-22 17:37:32 +0200 | [diff] [blame] | 767 | if (new.flags & KVM_MEM_LOG_DIRTY_PAGES) |
| 768 | do_remove_write_access(vcpu, mem->slot); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 769 | kvm_mmu_reset_context(vcpu); |
| 770 | vcpu_put(vcpu); |
| 771 | } |
| 772 | |
| 773 | kvm_free_physmem_slot(&old, &new); |
| 774 | return 0; |
| 775 | |
| 776 | out_unlock: |
| 777 | spin_unlock(&kvm->lock); |
| 778 | out_free: |
| 779 | kvm_free_physmem_slot(&new, &old); |
| 780 | out: |
| 781 | return r; |
| 782 | } |
| 783 | |
| 784 | /* |
| 785 | * Get (and clear) the dirty memory log for a memory slot. |
| 786 | */ |
Avi Kivity | 2c6f5df | 2007-02-20 18:27:58 +0200 | [diff] [blame] | 787 | static int kvm_vm_ioctl_get_dirty_log(struct kvm *kvm, |
| 788 | struct kvm_dirty_log *log) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 789 | { |
| 790 | struct kvm_memory_slot *memslot; |
| 791 | int r, i; |
| 792 | int n; |
Avi Kivity | 714b93d | 2007-01-05 16:36:53 -0800 | [diff] [blame] | 793 | int cleared; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 794 | unsigned long any = 0; |
| 795 | |
| 796 | spin_lock(&kvm->lock); |
| 797 | |
| 798 | /* |
| 799 | * Prevent changes to guest memory configuration even while the lock |
| 800 | * is not taken. |
| 801 | */ |
| 802 | ++kvm->busy; |
| 803 | spin_unlock(&kvm->lock); |
| 804 | r = -EINVAL; |
| 805 | if (log->slot >= KVM_MEMORY_SLOTS) |
| 806 | goto out; |
| 807 | |
| 808 | memslot = &kvm->memslots[log->slot]; |
| 809 | r = -ENOENT; |
| 810 | if (!memslot->dirty_bitmap) |
| 811 | goto out; |
| 812 | |
Uri Lublin | cd1a4a9 | 2007-02-22 16:43:09 +0200 | [diff] [blame] | 813 | n = ALIGN(memslot->npages, BITS_PER_LONG) / 8; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 814 | |
Uri Lublin | cd1a4a9 | 2007-02-22 16:43:09 +0200 | [diff] [blame] | 815 | for (i = 0; !any && i < n/sizeof(long); ++i) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 816 | any = memslot->dirty_bitmap[i]; |
| 817 | |
| 818 | r = -EFAULT; |
| 819 | if (copy_to_user(log->dirty_bitmap, memslot->dirty_bitmap, n)) |
| 820 | goto out; |
| 821 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 822 | if (any) { |
Avi Kivity | 714b93d | 2007-01-05 16:36:53 -0800 | [diff] [blame] | 823 | cleared = 0; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 824 | for (i = 0; i < KVM_MAX_VCPUS; ++i) { |
Avi Kivity | bccf215 | 2007-02-21 18:04:26 +0200 | [diff] [blame] | 825 | struct kvm_vcpu *vcpu; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 826 | |
Avi Kivity | bccf215 | 2007-02-21 18:04:26 +0200 | [diff] [blame] | 827 | vcpu = vcpu_load_slot(kvm, i); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 828 | if (!vcpu) |
| 829 | continue; |
Avi Kivity | 714b93d | 2007-01-05 16:36:53 -0800 | [diff] [blame] | 830 | if (!cleared) { |
| 831 | do_remove_write_access(vcpu, log->slot); |
| 832 | memset(memslot->dirty_bitmap, 0, n); |
| 833 | cleared = 1; |
| 834 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 835 | kvm_arch_ops->tlb_flush(vcpu); |
| 836 | vcpu_put(vcpu); |
| 837 | } |
| 838 | } |
| 839 | |
| 840 | r = 0; |
| 841 | |
| 842 | out: |
| 843 | spin_lock(&kvm->lock); |
| 844 | --kvm->busy; |
| 845 | spin_unlock(&kvm->lock); |
| 846 | return r; |
| 847 | } |
| 848 | |
Avi Kivity | e820754 | 2007-03-30 16:54:30 +0300 | [diff] [blame] | 849 | /* |
| 850 | * Set a new alias region. Aliases map a portion of physical memory into |
| 851 | * another portion. This is useful for memory windows, for example the PC |
| 852 | * VGA region. |
| 853 | */ |
| 854 | static int kvm_vm_ioctl_set_memory_alias(struct kvm *kvm, |
| 855 | struct kvm_memory_alias *alias) |
| 856 | { |
| 857 | int r, n; |
| 858 | struct kvm_mem_alias *p; |
| 859 | |
| 860 | r = -EINVAL; |
| 861 | /* General sanity checks */ |
| 862 | if (alias->memory_size & (PAGE_SIZE - 1)) |
| 863 | goto out; |
| 864 | if (alias->guest_phys_addr & (PAGE_SIZE - 1)) |
| 865 | goto out; |
| 866 | if (alias->slot >= KVM_ALIAS_SLOTS) |
| 867 | goto out; |
| 868 | if (alias->guest_phys_addr + alias->memory_size |
| 869 | < alias->guest_phys_addr) |
| 870 | goto out; |
| 871 | if (alias->target_phys_addr + alias->memory_size |
| 872 | < alias->target_phys_addr) |
| 873 | goto out; |
| 874 | |
| 875 | spin_lock(&kvm->lock); |
| 876 | |
| 877 | p = &kvm->aliases[alias->slot]; |
| 878 | p->base_gfn = alias->guest_phys_addr >> PAGE_SHIFT; |
| 879 | p->npages = alias->memory_size >> PAGE_SHIFT; |
| 880 | p->target_gfn = alias->target_phys_addr >> PAGE_SHIFT; |
| 881 | |
| 882 | for (n = KVM_ALIAS_SLOTS; n > 0; --n) |
| 883 | if (kvm->aliases[n - 1].npages) |
| 884 | break; |
| 885 | kvm->naliases = n; |
| 886 | |
| 887 | spin_unlock(&kvm->lock); |
| 888 | |
| 889 | vcpu_load(&kvm->vcpus[0]); |
| 890 | spin_lock(&kvm->lock); |
| 891 | kvm_mmu_zap_all(&kvm->vcpus[0]); |
| 892 | spin_unlock(&kvm->lock); |
| 893 | vcpu_put(&kvm->vcpus[0]); |
| 894 | |
| 895 | return 0; |
| 896 | |
| 897 | out: |
| 898 | return r; |
| 899 | } |
| 900 | |
| 901 | static gfn_t unalias_gfn(struct kvm *kvm, gfn_t gfn) |
| 902 | { |
| 903 | int i; |
| 904 | struct kvm_mem_alias *alias; |
| 905 | |
| 906 | for (i = 0; i < kvm->naliases; ++i) { |
| 907 | alias = &kvm->aliases[i]; |
| 908 | if (gfn >= alias->base_gfn |
| 909 | && gfn < alias->base_gfn + alias->npages) |
| 910 | return alias->target_gfn + gfn - alias->base_gfn; |
| 911 | } |
| 912 | return gfn; |
| 913 | } |
| 914 | |
| 915 | 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] | 916 | { |
| 917 | int i; |
| 918 | |
| 919 | for (i = 0; i < kvm->nmemslots; ++i) { |
| 920 | struct kvm_memory_slot *memslot = &kvm->memslots[i]; |
| 921 | |
| 922 | if (gfn >= memslot->base_gfn |
| 923 | && gfn < memslot->base_gfn + memslot->npages) |
| 924 | return memslot; |
| 925 | } |
Al Viro | 8b6d44c | 2007-02-09 16:38:40 +0000 | [diff] [blame] | 926 | return NULL; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 927 | } |
Avi Kivity | e820754 | 2007-03-30 16:54:30 +0300 | [diff] [blame] | 928 | |
| 929 | struct kvm_memory_slot *gfn_to_memslot(struct kvm *kvm, gfn_t gfn) |
| 930 | { |
| 931 | gfn = unalias_gfn(kvm, gfn); |
| 932 | return __gfn_to_memslot(kvm, gfn); |
| 933 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 934 | |
Avi Kivity | 954bbbc2 | 2007-03-30 14:02:32 +0300 | [diff] [blame] | 935 | struct page *gfn_to_page(struct kvm *kvm, gfn_t gfn) |
| 936 | { |
| 937 | struct kvm_memory_slot *slot; |
| 938 | |
Avi Kivity | e820754 | 2007-03-30 16:54:30 +0300 | [diff] [blame] | 939 | gfn = unalias_gfn(kvm, gfn); |
| 940 | slot = __gfn_to_memslot(kvm, gfn); |
Avi Kivity | 954bbbc2 | 2007-03-30 14:02:32 +0300 | [diff] [blame] | 941 | if (!slot) |
| 942 | return NULL; |
| 943 | return slot->phys_mem[gfn - slot->base_gfn]; |
| 944 | } |
| 945 | EXPORT_SYMBOL_GPL(gfn_to_page); |
| 946 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 947 | void mark_page_dirty(struct kvm *kvm, gfn_t gfn) |
| 948 | { |
| 949 | int i; |
Al Viro | 8b6d44c | 2007-02-09 16:38:40 +0000 | [diff] [blame] | 950 | struct kvm_memory_slot *memslot = NULL; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 951 | unsigned long rel_gfn; |
| 952 | |
| 953 | for (i = 0; i < kvm->nmemslots; ++i) { |
| 954 | memslot = &kvm->memslots[i]; |
| 955 | |
| 956 | if (gfn >= memslot->base_gfn |
| 957 | && gfn < memslot->base_gfn + memslot->npages) { |
| 958 | |
| 959 | if (!memslot || !memslot->dirty_bitmap) |
| 960 | return; |
| 961 | |
| 962 | rel_gfn = gfn - memslot->base_gfn; |
| 963 | |
| 964 | /* avoid RMW */ |
| 965 | if (!test_bit(rel_gfn, memslot->dirty_bitmap)) |
| 966 | set_bit(rel_gfn, memslot->dirty_bitmap); |
| 967 | return; |
| 968 | } |
| 969 | } |
| 970 | } |
| 971 | |
| 972 | static int emulator_read_std(unsigned long addr, |
| 973 | unsigned long *val, |
| 974 | unsigned int bytes, |
| 975 | struct x86_emulate_ctxt *ctxt) |
| 976 | { |
| 977 | struct kvm_vcpu *vcpu = ctxt->vcpu; |
| 978 | void *data = val; |
| 979 | |
| 980 | while (bytes) { |
| 981 | gpa_t gpa = vcpu->mmu.gva_to_gpa(vcpu, addr); |
| 982 | unsigned offset = addr & (PAGE_SIZE-1); |
| 983 | unsigned tocopy = min(bytes, (unsigned)PAGE_SIZE - offset); |
| 984 | unsigned long pfn; |
Avi Kivity | 954bbbc2 | 2007-03-30 14:02:32 +0300 | [diff] [blame] | 985 | struct page *page; |
| 986 | void *page_virt; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 987 | |
| 988 | if (gpa == UNMAPPED_GVA) |
| 989 | return X86EMUL_PROPAGATE_FAULT; |
| 990 | pfn = gpa >> PAGE_SHIFT; |
Avi Kivity | 954bbbc2 | 2007-03-30 14:02:32 +0300 | [diff] [blame] | 991 | page = gfn_to_page(vcpu->kvm, pfn); |
| 992 | if (!page) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 993 | return X86EMUL_UNHANDLEABLE; |
Avi Kivity | 954bbbc2 | 2007-03-30 14:02:32 +0300 | [diff] [blame] | 994 | page_virt = kmap_atomic(page, KM_USER0); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 995 | |
Avi Kivity | 954bbbc2 | 2007-03-30 14:02:32 +0300 | [diff] [blame] | 996 | memcpy(data, page_virt + offset, tocopy); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 997 | |
Avi Kivity | 954bbbc2 | 2007-03-30 14:02:32 +0300 | [diff] [blame] | 998 | kunmap_atomic(page_virt, KM_USER0); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 999 | |
| 1000 | bytes -= tocopy; |
| 1001 | data += tocopy; |
| 1002 | addr += tocopy; |
| 1003 | } |
| 1004 | |
| 1005 | return X86EMUL_CONTINUE; |
| 1006 | } |
| 1007 | |
| 1008 | static int emulator_write_std(unsigned long addr, |
| 1009 | unsigned long val, |
| 1010 | unsigned int bytes, |
| 1011 | struct x86_emulate_ctxt *ctxt) |
| 1012 | { |
| 1013 | printk(KERN_ERR "emulator_write_std: addr %lx n %d\n", |
| 1014 | addr, bytes); |
| 1015 | return X86EMUL_UNHANDLEABLE; |
| 1016 | } |
| 1017 | |
| 1018 | static int emulator_read_emulated(unsigned long addr, |
| 1019 | unsigned long *val, |
| 1020 | unsigned int bytes, |
| 1021 | struct x86_emulate_ctxt *ctxt) |
| 1022 | { |
| 1023 | struct kvm_vcpu *vcpu = ctxt->vcpu; |
| 1024 | |
| 1025 | if (vcpu->mmio_read_completed) { |
| 1026 | memcpy(val, vcpu->mmio_data, bytes); |
| 1027 | vcpu->mmio_read_completed = 0; |
| 1028 | return X86EMUL_CONTINUE; |
| 1029 | } else if (emulator_read_std(addr, val, bytes, ctxt) |
| 1030 | == X86EMUL_CONTINUE) |
| 1031 | return X86EMUL_CONTINUE; |
| 1032 | else { |
| 1033 | gpa_t gpa = vcpu->mmu.gva_to_gpa(vcpu, addr); |
Avi Kivity | d27d4ac | 2007-02-19 14:37:46 +0200 | [diff] [blame] | 1034 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1035 | if (gpa == UNMAPPED_GVA) |
Avi Kivity | d27d4ac | 2007-02-19 14:37:46 +0200 | [diff] [blame] | 1036 | return X86EMUL_PROPAGATE_FAULT; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1037 | vcpu->mmio_needed = 1; |
| 1038 | vcpu->mmio_phys_addr = gpa; |
| 1039 | vcpu->mmio_size = bytes; |
| 1040 | vcpu->mmio_is_write = 0; |
| 1041 | |
| 1042 | return X86EMUL_UNHANDLEABLE; |
| 1043 | } |
| 1044 | } |
| 1045 | |
Avi Kivity | da4a00f | 2007-01-05 16:36:44 -0800 | [diff] [blame] | 1046 | static int emulator_write_phys(struct kvm_vcpu *vcpu, gpa_t gpa, |
| 1047 | unsigned long val, int bytes) |
| 1048 | { |
Avi Kivity | da4a00f | 2007-01-05 16:36:44 -0800 | [diff] [blame] | 1049 | struct page *page; |
| 1050 | void *virt; |
| 1051 | |
| 1052 | if (((gpa + bytes - 1) >> PAGE_SHIFT) != (gpa >> PAGE_SHIFT)) |
| 1053 | return 0; |
Avi Kivity | 954bbbc2 | 2007-03-30 14:02:32 +0300 | [diff] [blame] | 1054 | page = gfn_to_page(vcpu->kvm, gpa >> PAGE_SHIFT); |
| 1055 | if (!page) |
Avi Kivity | da4a00f | 2007-01-05 16:36:44 -0800 | [diff] [blame] | 1056 | return 0; |
Avi Kivity | da4a00f | 2007-01-05 16:36:44 -0800 | [diff] [blame] | 1057 | kvm_mmu_pre_write(vcpu, gpa, bytes); |
Uri Lublin | ab51a43 | 2007-02-21 18:25:21 +0200 | [diff] [blame] | 1058 | mark_page_dirty(vcpu->kvm, gpa >> PAGE_SHIFT); |
Avi Kivity | da4a00f | 2007-01-05 16:36:44 -0800 | [diff] [blame] | 1059 | virt = kmap_atomic(page, KM_USER0); |
| 1060 | memcpy(virt + offset_in_page(gpa), &val, bytes); |
| 1061 | kunmap_atomic(virt, KM_USER0); |
| 1062 | kvm_mmu_post_write(vcpu, gpa, bytes); |
| 1063 | return 1; |
| 1064 | } |
| 1065 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1066 | static int emulator_write_emulated(unsigned long addr, |
| 1067 | unsigned long val, |
| 1068 | unsigned int bytes, |
| 1069 | struct x86_emulate_ctxt *ctxt) |
| 1070 | { |
| 1071 | struct kvm_vcpu *vcpu = ctxt->vcpu; |
| 1072 | gpa_t gpa = vcpu->mmu.gva_to_gpa(vcpu, addr); |
| 1073 | |
Avi Kivity | c9047f5 | 2007-04-17 10:53:22 +0300 | [diff] [blame] | 1074 | if (gpa == UNMAPPED_GVA) { |
| 1075 | kvm_arch_ops->inject_page_fault(vcpu, addr, 2); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1076 | return X86EMUL_PROPAGATE_FAULT; |
Avi Kivity | c9047f5 | 2007-04-17 10:53:22 +0300 | [diff] [blame] | 1077 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1078 | |
Avi Kivity | da4a00f | 2007-01-05 16:36:44 -0800 | [diff] [blame] | 1079 | if (emulator_write_phys(vcpu, gpa, val, bytes)) |
| 1080 | return X86EMUL_CONTINUE; |
| 1081 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1082 | vcpu->mmio_needed = 1; |
| 1083 | vcpu->mmio_phys_addr = gpa; |
| 1084 | vcpu->mmio_size = bytes; |
| 1085 | vcpu->mmio_is_write = 1; |
| 1086 | memcpy(vcpu->mmio_data, &val, bytes); |
| 1087 | |
| 1088 | return X86EMUL_CONTINUE; |
| 1089 | } |
| 1090 | |
| 1091 | static int emulator_cmpxchg_emulated(unsigned long addr, |
| 1092 | unsigned long old, |
| 1093 | unsigned long new, |
| 1094 | unsigned int bytes, |
| 1095 | struct x86_emulate_ctxt *ctxt) |
| 1096 | { |
| 1097 | static int reported; |
| 1098 | |
| 1099 | if (!reported) { |
| 1100 | reported = 1; |
| 1101 | printk(KERN_WARNING "kvm: emulating exchange as write\n"); |
| 1102 | } |
| 1103 | return emulator_write_emulated(addr, new, bytes, ctxt); |
| 1104 | } |
| 1105 | |
Avi Kivity | 32b3562 | 2007-01-05 16:36:51 -0800 | [diff] [blame] | 1106 | #ifdef CONFIG_X86_32 |
| 1107 | |
| 1108 | static int emulator_cmpxchg8b_emulated(unsigned long addr, |
| 1109 | unsigned long old_lo, |
| 1110 | unsigned long old_hi, |
| 1111 | unsigned long new_lo, |
| 1112 | unsigned long new_hi, |
| 1113 | struct x86_emulate_ctxt *ctxt) |
| 1114 | { |
| 1115 | static int reported; |
| 1116 | int r; |
| 1117 | |
| 1118 | if (!reported) { |
| 1119 | reported = 1; |
| 1120 | printk(KERN_WARNING "kvm: emulating exchange8b as write\n"); |
| 1121 | } |
| 1122 | r = emulator_write_emulated(addr, new_lo, 4, ctxt); |
| 1123 | if (r != X86EMUL_CONTINUE) |
| 1124 | return r; |
| 1125 | return emulator_write_emulated(addr+4, new_hi, 4, ctxt); |
| 1126 | } |
| 1127 | |
| 1128 | #endif |
| 1129 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1130 | static unsigned long get_segment_base(struct kvm_vcpu *vcpu, int seg) |
| 1131 | { |
| 1132 | return kvm_arch_ops->get_segment_base(vcpu, seg); |
| 1133 | } |
| 1134 | |
| 1135 | int emulate_invlpg(struct kvm_vcpu *vcpu, gva_t address) |
| 1136 | { |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1137 | return X86EMUL_CONTINUE; |
| 1138 | } |
| 1139 | |
| 1140 | int emulate_clts(struct kvm_vcpu *vcpu) |
| 1141 | { |
Avi Kivity | 399badf | 2007-01-05 16:36:38 -0800 | [diff] [blame] | 1142 | unsigned long cr0; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1143 | |
Avi Kivity | 399badf | 2007-01-05 16:36:38 -0800 | [diff] [blame] | 1144 | kvm_arch_ops->decache_cr0_cr4_guest_bits(vcpu); |
| 1145 | cr0 = vcpu->cr0 & ~CR0_TS_MASK; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1146 | kvm_arch_ops->set_cr0(vcpu, cr0); |
| 1147 | return X86EMUL_CONTINUE; |
| 1148 | } |
| 1149 | |
| 1150 | int emulator_get_dr(struct x86_emulate_ctxt* ctxt, int dr, unsigned long *dest) |
| 1151 | { |
| 1152 | struct kvm_vcpu *vcpu = ctxt->vcpu; |
| 1153 | |
| 1154 | switch (dr) { |
| 1155 | case 0 ... 3: |
| 1156 | *dest = kvm_arch_ops->get_dr(vcpu, dr); |
| 1157 | return X86EMUL_CONTINUE; |
| 1158 | default: |
| 1159 | printk(KERN_DEBUG "%s: unexpected dr %u\n", |
| 1160 | __FUNCTION__, dr); |
| 1161 | return X86EMUL_UNHANDLEABLE; |
| 1162 | } |
| 1163 | } |
| 1164 | |
| 1165 | int emulator_set_dr(struct x86_emulate_ctxt *ctxt, int dr, unsigned long value) |
| 1166 | { |
| 1167 | unsigned long mask = (ctxt->mode == X86EMUL_MODE_PROT64) ? ~0ULL : ~0U; |
| 1168 | int exception; |
| 1169 | |
| 1170 | kvm_arch_ops->set_dr(ctxt->vcpu, dr, value & mask, &exception); |
| 1171 | if (exception) { |
| 1172 | /* FIXME: better handling */ |
| 1173 | return X86EMUL_UNHANDLEABLE; |
| 1174 | } |
| 1175 | return X86EMUL_CONTINUE; |
| 1176 | } |
| 1177 | |
| 1178 | static void report_emulation_failure(struct x86_emulate_ctxt *ctxt) |
| 1179 | { |
| 1180 | static int reported; |
| 1181 | u8 opcodes[4]; |
| 1182 | unsigned long rip = ctxt->vcpu->rip; |
| 1183 | unsigned long rip_linear; |
| 1184 | |
| 1185 | rip_linear = rip + get_segment_base(ctxt->vcpu, VCPU_SREG_CS); |
| 1186 | |
| 1187 | if (reported) |
| 1188 | return; |
| 1189 | |
| 1190 | emulator_read_std(rip_linear, (void *)opcodes, 4, ctxt); |
| 1191 | |
| 1192 | printk(KERN_ERR "emulation failed but !mmio_needed?" |
| 1193 | " rip %lx %02x %02x %02x %02x\n", |
| 1194 | rip, opcodes[0], opcodes[1], opcodes[2], opcodes[3]); |
| 1195 | reported = 1; |
| 1196 | } |
| 1197 | |
| 1198 | struct x86_emulate_ops emulate_ops = { |
| 1199 | .read_std = emulator_read_std, |
| 1200 | .write_std = emulator_write_std, |
| 1201 | .read_emulated = emulator_read_emulated, |
| 1202 | .write_emulated = emulator_write_emulated, |
| 1203 | .cmpxchg_emulated = emulator_cmpxchg_emulated, |
Avi Kivity | 32b3562 | 2007-01-05 16:36:51 -0800 | [diff] [blame] | 1204 | #ifdef CONFIG_X86_32 |
| 1205 | .cmpxchg8b_emulated = emulator_cmpxchg8b_emulated, |
| 1206 | #endif |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1207 | }; |
| 1208 | |
| 1209 | int emulate_instruction(struct kvm_vcpu *vcpu, |
| 1210 | struct kvm_run *run, |
| 1211 | unsigned long cr2, |
| 1212 | u16 error_code) |
| 1213 | { |
| 1214 | struct x86_emulate_ctxt emulate_ctxt; |
| 1215 | int r; |
| 1216 | int cs_db, cs_l; |
| 1217 | |
| 1218 | kvm_arch_ops->cache_regs(vcpu); |
| 1219 | |
| 1220 | kvm_arch_ops->get_cs_db_l_bits(vcpu, &cs_db, &cs_l); |
| 1221 | |
| 1222 | emulate_ctxt.vcpu = vcpu; |
| 1223 | emulate_ctxt.eflags = kvm_arch_ops->get_rflags(vcpu); |
| 1224 | emulate_ctxt.cr2 = cr2; |
| 1225 | emulate_ctxt.mode = (emulate_ctxt.eflags & X86_EFLAGS_VM) |
| 1226 | ? X86EMUL_MODE_REAL : cs_l |
| 1227 | ? X86EMUL_MODE_PROT64 : cs_db |
| 1228 | ? X86EMUL_MODE_PROT32 : X86EMUL_MODE_PROT16; |
| 1229 | |
| 1230 | if (emulate_ctxt.mode == X86EMUL_MODE_PROT64) { |
| 1231 | emulate_ctxt.cs_base = 0; |
| 1232 | emulate_ctxt.ds_base = 0; |
| 1233 | emulate_ctxt.es_base = 0; |
| 1234 | emulate_ctxt.ss_base = 0; |
| 1235 | } else { |
| 1236 | emulate_ctxt.cs_base = get_segment_base(vcpu, VCPU_SREG_CS); |
| 1237 | emulate_ctxt.ds_base = get_segment_base(vcpu, VCPU_SREG_DS); |
| 1238 | emulate_ctxt.es_base = get_segment_base(vcpu, VCPU_SREG_ES); |
| 1239 | emulate_ctxt.ss_base = get_segment_base(vcpu, VCPU_SREG_SS); |
| 1240 | } |
| 1241 | |
| 1242 | emulate_ctxt.gs_base = get_segment_base(vcpu, VCPU_SREG_GS); |
| 1243 | emulate_ctxt.fs_base = get_segment_base(vcpu, VCPU_SREG_FS); |
| 1244 | |
| 1245 | vcpu->mmio_is_write = 0; |
| 1246 | r = x86_emulate_memop(&emulate_ctxt, &emulate_ops); |
| 1247 | |
| 1248 | if ((r || vcpu->mmio_is_write) && run) { |
| 1249 | run->mmio.phys_addr = vcpu->mmio_phys_addr; |
| 1250 | memcpy(run->mmio.data, vcpu->mmio_data, 8); |
| 1251 | run->mmio.len = vcpu->mmio_size; |
| 1252 | run->mmio.is_write = vcpu->mmio_is_write; |
| 1253 | } |
| 1254 | |
| 1255 | if (r) { |
Avi Kivity | a436036 | 2007-01-05 16:36:45 -0800 | [diff] [blame] | 1256 | if (kvm_mmu_unprotect_page_virt(vcpu, cr2)) |
| 1257 | return EMULATE_DONE; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1258 | if (!vcpu->mmio_needed) { |
| 1259 | report_emulation_failure(&emulate_ctxt); |
| 1260 | return EMULATE_FAIL; |
| 1261 | } |
| 1262 | return EMULATE_DO_MMIO; |
| 1263 | } |
| 1264 | |
| 1265 | kvm_arch_ops->decache_regs(vcpu); |
| 1266 | kvm_arch_ops->set_rflags(vcpu, emulate_ctxt.eflags); |
| 1267 | |
| 1268 | if (vcpu->mmio_is_write) |
| 1269 | return EMULATE_DO_MMIO; |
| 1270 | |
| 1271 | return EMULATE_DONE; |
| 1272 | } |
| 1273 | EXPORT_SYMBOL_GPL(emulate_instruction); |
| 1274 | |
Avi Kivity | 270fd9b | 2007-02-19 14:37:47 +0200 | [diff] [blame] | 1275 | int kvm_hypercall(struct kvm_vcpu *vcpu, struct kvm_run *run) |
| 1276 | { |
| 1277 | unsigned long nr, a0, a1, a2, a3, a4, a5, ret; |
| 1278 | |
Dor Laor | 9b22bf5 | 2007-02-19 16:44:49 +0200 | [diff] [blame] | 1279 | kvm_arch_ops->cache_regs(vcpu); |
Avi Kivity | 270fd9b | 2007-02-19 14:37:47 +0200 | [diff] [blame] | 1280 | ret = -KVM_EINVAL; |
| 1281 | #ifdef CONFIG_X86_64 |
| 1282 | if (is_long_mode(vcpu)) { |
| 1283 | nr = vcpu->regs[VCPU_REGS_RAX]; |
| 1284 | a0 = vcpu->regs[VCPU_REGS_RDI]; |
| 1285 | a1 = vcpu->regs[VCPU_REGS_RSI]; |
| 1286 | a2 = vcpu->regs[VCPU_REGS_RDX]; |
| 1287 | a3 = vcpu->regs[VCPU_REGS_RCX]; |
| 1288 | a4 = vcpu->regs[VCPU_REGS_R8]; |
| 1289 | a5 = vcpu->regs[VCPU_REGS_R9]; |
| 1290 | } else |
| 1291 | #endif |
| 1292 | { |
| 1293 | nr = vcpu->regs[VCPU_REGS_RBX] & -1u; |
| 1294 | a0 = vcpu->regs[VCPU_REGS_RAX] & -1u; |
| 1295 | a1 = vcpu->regs[VCPU_REGS_RCX] & -1u; |
| 1296 | a2 = vcpu->regs[VCPU_REGS_RDX] & -1u; |
| 1297 | a3 = vcpu->regs[VCPU_REGS_RSI] & -1u; |
| 1298 | a4 = vcpu->regs[VCPU_REGS_RDI] & -1u; |
| 1299 | a5 = vcpu->regs[VCPU_REGS_RBP] & -1u; |
| 1300 | } |
| 1301 | switch (nr) { |
| 1302 | default: |
Avi Kivity | b4e63f5 | 2007-03-04 13:59:30 +0200 | [diff] [blame] | 1303 | run->hypercall.args[0] = a0; |
| 1304 | run->hypercall.args[1] = a1; |
| 1305 | run->hypercall.args[2] = a2; |
| 1306 | run->hypercall.args[3] = a3; |
| 1307 | run->hypercall.args[4] = a4; |
| 1308 | run->hypercall.args[5] = a5; |
| 1309 | run->hypercall.ret = ret; |
| 1310 | run->hypercall.longmode = is_long_mode(vcpu); |
| 1311 | kvm_arch_ops->decache_regs(vcpu); |
| 1312 | return 0; |
Avi Kivity | 270fd9b | 2007-02-19 14:37:47 +0200 | [diff] [blame] | 1313 | } |
| 1314 | vcpu->regs[VCPU_REGS_RAX] = ret; |
Dor Laor | 9b22bf5 | 2007-02-19 16:44:49 +0200 | [diff] [blame] | 1315 | kvm_arch_ops->decache_regs(vcpu); |
Avi Kivity | 270fd9b | 2007-02-19 14:37:47 +0200 | [diff] [blame] | 1316 | return 1; |
| 1317 | } |
| 1318 | EXPORT_SYMBOL_GPL(kvm_hypercall); |
| 1319 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1320 | static u64 mk_cr_64(u64 curr_cr, u32 new_val) |
| 1321 | { |
| 1322 | return (curr_cr & ~((1ULL << 32) - 1)) | new_val; |
| 1323 | } |
| 1324 | |
| 1325 | void realmode_lgdt(struct kvm_vcpu *vcpu, u16 limit, unsigned long base) |
| 1326 | { |
| 1327 | struct descriptor_table dt = { limit, base }; |
| 1328 | |
| 1329 | kvm_arch_ops->set_gdt(vcpu, &dt); |
| 1330 | } |
| 1331 | |
| 1332 | void realmode_lidt(struct kvm_vcpu *vcpu, u16 limit, unsigned long base) |
| 1333 | { |
| 1334 | struct descriptor_table dt = { limit, base }; |
| 1335 | |
| 1336 | kvm_arch_ops->set_idt(vcpu, &dt); |
| 1337 | } |
| 1338 | |
| 1339 | void realmode_lmsw(struct kvm_vcpu *vcpu, unsigned long msw, |
| 1340 | unsigned long *rflags) |
| 1341 | { |
| 1342 | lmsw(vcpu, msw); |
| 1343 | *rflags = kvm_arch_ops->get_rflags(vcpu); |
| 1344 | } |
| 1345 | |
| 1346 | unsigned long realmode_get_cr(struct kvm_vcpu *vcpu, int cr) |
| 1347 | { |
Avi Kivity | 399badf | 2007-01-05 16:36:38 -0800 | [diff] [blame] | 1348 | kvm_arch_ops->decache_cr0_cr4_guest_bits(vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1349 | switch (cr) { |
| 1350 | case 0: |
| 1351 | return vcpu->cr0; |
| 1352 | case 2: |
| 1353 | return vcpu->cr2; |
| 1354 | case 3: |
| 1355 | return vcpu->cr3; |
| 1356 | case 4: |
| 1357 | return vcpu->cr4; |
| 1358 | default: |
| 1359 | vcpu_printf(vcpu, "%s: unexpected cr %u\n", __FUNCTION__, cr); |
| 1360 | return 0; |
| 1361 | } |
| 1362 | } |
| 1363 | |
| 1364 | void realmode_set_cr(struct kvm_vcpu *vcpu, int cr, unsigned long val, |
| 1365 | unsigned long *rflags) |
| 1366 | { |
| 1367 | switch (cr) { |
| 1368 | case 0: |
| 1369 | set_cr0(vcpu, mk_cr_64(vcpu->cr0, val)); |
| 1370 | *rflags = kvm_arch_ops->get_rflags(vcpu); |
| 1371 | break; |
| 1372 | case 2: |
| 1373 | vcpu->cr2 = val; |
| 1374 | break; |
| 1375 | case 3: |
| 1376 | set_cr3(vcpu, val); |
| 1377 | break; |
| 1378 | case 4: |
| 1379 | set_cr4(vcpu, mk_cr_64(vcpu->cr4, val)); |
| 1380 | break; |
| 1381 | default: |
| 1382 | vcpu_printf(vcpu, "%s: unexpected cr %u\n", __FUNCTION__, cr); |
| 1383 | } |
| 1384 | } |
| 1385 | |
Ingo Molnar | 102d832 | 2007-02-19 14:37:47 +0200 | [diff] [blame] | 1386 | /* |
| 1387 | * Register the para guest with the host: |
| 1388 | */ |
| 1389 | static int vcpu_register_para(struct kvm_vcpu *vcpu, gpa_t para_state_gpa) |
| 1390 | { |
| 1391 | struct kvm_vcpu_para_state *para_state; |
| 1392 | hpa_t para_state_hpa, hypercall_hpa; |
| 1393 | struct page *para_state_page; |
| 1394 | unsigned char *hypercall; |
| 1395 | gpa_t hypercall_gpa; |
| 1396 | |
| 1397 | printk(KERN_DEBUG "kvm: guest trying to enter paravirtual mode\n"); |
| 1398 | printk(KERN_DEBUG ".... para_state_gpa: %08Lx\n", para_state_gpa); |
| 1399 | |
| 1400 | /* |
| 1401 | * Needs to be page aligned: |
| 1402 | */ |
| 1403 | if (para_state_gpa != PAGE_ALIGN(para_state_gpa)) |
| 1404 | goto err_gp; |
| 1405 | |
| 1406 | para_state_hpa = gpa_to_hpa(vcpu, para_state_gpa); |
| 1407 | printk(KERN_DEBUG ".... para_state_hpa: %08Lx\n", para_state_hpa); |
| 1408 | if (is_error_hpa(para_state_hpa)) |
| 1409 | goto err_gp; |
| 1410 | |
Uri Lublin | ab51a43 | 2007-02-21 18:25:21 +0200 | [diff] [blame] | 1411 | mark_page_dirty(vcpu->kvm, para_state_gpa >> PAGE_SHIFT); |
Ingo Molnar | 102d832 | 2007-02-19 14:37:47 +0200 | [diff] [blame] | 1412 | para_state_page = pfn_to_page(para_state_hpa >> PAGE_SHIFT); |
| 1413 | para_state = kmap_atomic(para_state_page, KM_USER0); |
| 1414 | |
| 1415 | printk(KERN_DEBUG ".... guest version: %d\n", para_state->guest_version); |
| 1416 | printk(KERN_DEBUG ".... size: %d\n", para_state->size); |
| 1417 | |
| 1418 | para_state->host_version = KVM_PARA_API_VERSION; |
| 1419 | /* |
| 1420 | * We cannot support guests that try to register themselves |
| 1421 | * with a newer API version than the host supports: |
| 1422 | */ |
| 1423 | if (para_state->guest_version > KVM_PARA_API_VERSION) { |
| 1424 | para_state->ret = -KVM_EINVAL; |
| 1425 | goto err_kunmap_skip; |
| 1426 | } |
| 1427 | |
| 1428 | hypercall_gpa = para_state->hypercall_gpa; |
| 1429 | hypercall_hpa = gpa_to_hpa(vcpu, hypercall_gpa); |
| 1430 | printk(KERN_DEBUG ".... hypercall_hpa: %08Lx\n", hypercall_hpa); |
| 1431 | if (is_error_hpa(hypercall_hpa)) { |
| 1432 | para_state->ret = -KVM_EINVAL; |
| 1433 | goto err_kunmap_skip; |
| 1434 | } |
| 1435 | |
| 1436 | printk(KERN_DEBUG "kvm: para guest successfully registered.\n"); |
| 1437 | vcpu->para_state_page = para_state_page; |
| 1438 | vcpu->para_state_gpa = para_state_gpa; |
| 1439 | vcpu->hypercall_gpa = hypercall_gpa; |
| 1440 | |
Uri Lublin | ab51a43 | 2007-02-21 18:25:21 +0200 | [diff] [blame] | 1441 | mark_page_dirty(vcpu->kvm, hypercall_gpa >> PAGE_SHIFT); |
Ingo Molnar | 102d832 | 2007-02-19 14:37:47 +0200 | [diff] [blame] | 1442 | hypercall = kmap_atomic(pfn_to_page(hypercall_hpa >> PAGE_SHIFT), |
| 1443 | KM_USER1) + (hypercall_hpa & ~PAGE_MASK); |
| 1444 | kvm_arch_ops->patch_hypercall(vcpu, hypercall); |
| 1445 | kunmap_atomic(hypercall, KM_USER1); |
| 1446 | |
| 1447 | para_state->ret = 0; |
| 1448 | err_kunmap_skip: |
| 1449 | kunmap_atomic(para_state, KM_USER0); |
| 1450 | return 0; |
| 1451 | err_gp: |
| 1452 | return 1; |
| 1453 | } |
| 1454 | |
Avi Kivity | 3bab1f5 | 2006-12-29 16:49:48 -0800 | [diff] [blame] | 1455 | int kvm_get_msr_common(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata) |
| 1456 | { |
| 1457 | u64 data; |
| 1458 | |
| 1459 | switch (msr) { |
| 1460 | case 0xc0010010: /* SYSCFG */ |
| 1461 | case 0xc0010015: /* HWCR */ |
| 1462 | case MSR_IA32_PLATFORM_ID: |
| 1463 | case MSR_IA32_P5_MC_ADDR: |
| 1464 | case MSR_IA32_P5_MC_TYPE: |
| 1465 | case MSR_IA32_MC0_CTL: |
| 1466 | case MSR_IA32_MCG_STATUS: |
| 1467 | case MSR_IA32_MCG_CAP: |
| 1468 | case MSR_IA32_MC0_MISC: |
| 1469 | case MSR_IA32_MC0_MISC+4: |
| 1470 | case MSR_IA32_MC0_MISC+8: |
| 1471 | case MSR_IA32_MC0_MISC+12: |
| 1472 | case MSR_IA32_MC0_MISC+16: |
| 1473 | case MSR_IA32_UCODE_REV: |
Avi Kivity | a8d13ea | 2006-12-29 16:49:51 -0800 | [diff] [blame] | 1474 | case MSR_IA32_PERF_STATUS: |
Avi Kivity | 3bab1f5 | 2006-12-29 16:49:48 -0800 | [diff] [blame] | 1475 | /* MTRR registers */ |
| 1476 | case 0xfe: |
| 1477 | case 0x200 ... 0x2ff: |
| 1478 | data = 0; |
| 1479 | break; |
Avi Kivity | a8d13ea | 2006-12-29 16:49:51 -0800 | [diff] [blame] | 1480 | case 0xcd: /* fsb frequency */ |
| 1481 | data = 3; |
| 1482 | break; |
Avi Kivity | 3bab1f5 | 2006-12-29 16:49:48 -0800 | [diff] [blame] | 1483 | case MSR_IA32_APICBASE: |
| 1484 | data = vcpu->apic_base; |
| 1485 | break; |
Avi Kivity | 6f00e68 | 2007-01-26 00:56:40 -0800 | [diff] [blame] | 1486 | case MSR_IA32_MISC_ENABLE: |
| 1487 | data = vcpu->ia32_misc_enable_msr; |
| 1488 | break; |
Avi Kivity | 3bab1f5 | 2006-12-29 16:49:48 -0800 | [diff] [blame] | 1489 | #ifdef CONFIG_X86_64 |
| 1490 | case MSR_EFER: |
| 1491 | data = vcpu->shadow_efer; |
| 1492 | break; |
| 1493 | #endif |
| 1494 | default: |
| 1495 | printk(KERN_ERR "kvm: unhandled rdmsr: 0x%x\n", msr); |
| 1496 | return 1; |
| 1497 | } |
| 1498 | *pdata = data; |
| 1499 | return 0; |
| 1500 | } |
| 1501 | EXPORT_SYMBOL_GPL(kvm_get_msr_common); |
| 1502 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1503 | /* |
| 1504 | * Reads an msr value (of 'msr_index') into 'pdata'. |
| 1505 | * Returns 0 on success, non-0 otherwise. |
| 1506 | * Assumes vcpu_load() was already called. |
| 1507 | */ |
| 1508 | static int get_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 *pdata) |
| 1509 | { |
| 1510 | return kvm_arch_ops->get_msr(vcpu, msr_index, pdata); |
| 1511 | } |
| 1512 | |
Avi Kivity | 05b3e0c | 2006-12-13 00:33:45 -0800 | [diff] [blame] | 1513 | #ifdef CONFIG_X86_64 |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1514 | |
Avi Kivity | 3bab1f5 | 2006-12-29 16:49:48 -0800 | [diff] [blame] | 1515 | static void set_efer(struct kvm_vcpu *vcpu, u64 efer) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1516 | { |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1517 | if (efer & EFER_RESERVED_BITS) { |
| 1518 | printk(KERN_DEBUG "set_efer: 0x%llx #GP, reserved bits\n", |
| 1519 | efer); |
| 1520 | inject_gp(vcpu); |
| 1521 | return; |
| 1522 | } |
| 1523 | |
| 1524 | if (is_paging(vcpu) |
| 1525 | && (vcpu->shadow_efer & EFER_LME) != (efer & EFER_LME)) { |
| 1526 | printk(KERN_DEBUG "set_efer: #GP, change LME while paging\n"); |
| 1527 | inject_gp(vcpu); |
| 1528 | return; |
| 1529 | } |
| 1530 | |
Avi Kivity | 7725f0b | 2006-12-13 00:34:01 -0800 | [diff] [blame] | 1531 | kvm_arch_ops->set_efer(vcpu, efer); |
| 1532 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1533 | efer &= ~EFER_LMA; |
| 1534 | efer |= vcpu->shadow_efer & EFER_LMA; |
| 1535 | |
| 1536 | vcpu->shadow_efer = efer; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1537 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1538 | |
| 1539 | #endif |
| 1540 | |
Avi Kivity | 3bab1f5 | 2006-12-29 16:49:48 -0800 | [diff] [blame] | 1541 | int kvm_set_msr_common(struct kvm_vcpu *vcpu, u32 msr, u64 data) |
| 1542 | { |
| 1543 | switch (msr) { |
| 1544 | #ifdef CONFIG_X86_64 |
| 1545 | case MSR_EFER: |
| 1546 | set_efer(vcpu, data); |
| 1547 | break; |
| 1548 | #endif |
| 1549 | case MSR_IA32_MC0_STATUS: |
| 1550 | printk(KERN_WARNING "%s: MSR_IA32_MC0_STATUS 0x%llx, nop\n", |
| 1551 | __FUNCTION__, data); |
| 1552 | break; |
Sergey Kiselev | 0e5bf0d | 2007-03-22 14:06:18 +0200 | [diff] [blame] | 1553 | case MSR_IA32_MCG_STATUS: |
| 1554 | printk(KERN_WARNING "%s: MSR_IA32_MCG_STATUS 0x%llx, nop\n", |
| 1555 | __FUNCTION__, data); |
| 1556 | break; |
Avi Kivity | 3bab1f5 | 2006-12-29 16:49:48 -0800 | [diff] [blame] | 1557 | case MSR_IA32_UCODE_REV: |
| 1558 | case MSR_IA32_UCODE_WRITE: |
| 1559 | case 0x200 ... 0x2ff: /* MTRRs */ |
| 1560 | break; |
| 1561 | case MSR_IA32_APICBASE: |
| 1562 | vcpu->apic_base = data; |
| 1563 | break; |
Avi Kivity | 6f00e68 | 2007-01-26 00:56:40 -0800 | [diff] [blame] | 1564 | case MSR_IA32_MISC_ENABLE: |
| 1565 | vcpu->ia32_misc_enable_msr = data; |
| 1566 | break; |
Ingo Molnar | 102d832 | 2007-02-19 14:37:47 +0200 | [diff] [blame] | 1567 | /* |
| 1568 | * This is the 'probe whether the host is KVM' logic: |
| 1569 | */ |
| 1570 | case MSR_KVM_API_MAGIC: |
| 1571 | return vcpu_register_para(vcpu, data); |
| 1572 | |
Avi Kivity | 3bab1f5 | 2006-12-29 16:49:48 -0800 | [diff] [blame] | 1573 | default: |
| 1574 | printk(KERN_ERR "kvm: unhandled wrmsr: 0x%x\n", msr); |
| 1575 | return 1; |
| 1576 | } |
| 1577 | return 0; |
| 1578 | } |
| 1579 | EXPORT_SYMBOL_GPL(kvm_set_msr_common); |
| 1580 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1581 | /* |
| 1582 | * Writes msr value into into the appropriate "register". |
| 1583 | * Returns 0 on success, non-0 otherwise. |
| 1584 | * Assumes vcpu_load() was already called. |
| 1585 | */ |
| 1586 | static int set_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 data) |
| 1587 | { |
| 1588 | return kvm_arch_ops->set_msr(vcpu, msr_index, data); |
| 1589 | } |
| 1590 | |
| 1591 | void kvm_resched(struct kvm_vcpu *vcpu) |
| 1592 | { |
Yaozu Dong | 3fca036 | 2007-04-25 16:49:19 +0300 | [diff] [blame] | 1593 | if (!need_resched()) |
| 1594 | return; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1595 | vcpu_put(vcpu); |
| 1596 | cond_resched(); |
Avi Kivity | bccf215 | 2007-02-21 18:04:26 +0200 | [diff] [blame] | 1597 | vcpu_load(vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1598 | } |
| 1599 | EXPORT_SYMBOL_GPL(kvm_resched); |
| 1600 | |
| 1601 | void load_msrs(struct vmx_msr_entry *e, int n) |
| 1602 | { |
| 1603 | int i; |
| 1604 | |
| 1605 | for (i = 0; i < n; ++i) |
| 1606 | wrmsrl(e[i].index, e[i].data); |
| 1607 | } |
| 1608 | EXPORT_SYMBOL_GPL(load_msrs); |
| 1609 | |
| 1610 | void save_msrs(struct vmx_msr_entry *e, int n) |
| 1611 | { |
| 1612 | int i; |
| 1613 | |
| 1614 | for (i = 0; i < n; ++i) |
| 1615 | rdmsrl(e[i].index, e[i].data); |
| 1616 | } |
| 1617 | EXPORT_SYMBOL_GPL(save_msrs); |
| 1618 | |
Avi Kivity | 06465c5 | 2007-02-28 20:46:53 +0200 | [diff] [blame] | 1619 | void kvm_emulate_cpuid(struct kvm_vcpu *vcpu) |
| 1620 | { |
| 1621 | int i; |
| 1622 | u32 function; |
| 1623 | struct kvm_cpuid_entry *e, *best; |
| 1624 | |
| 1625 | kvm_arch_ops->cache_regs(vcpu); |
| 1626 | function = vcpu->regs[VCPU_REGS_RAX]; |
| 1627 | vcpu->regs[VCPU_REGS_RAX] = 0; |
| 1628 | vcpu->regs[VCPU_REGS_RBX] = 0; |
| 1629 | vcpu->regs[VCPU_REGS_RCX] = 0; |
| 1630 | vcpu->regs[VCPU_REGS_RDX] = 0; |
| 1631 | best = NULL; |
| 1632 | for (i = 0; i < vcpu->cpuid_nent; ++i) { |
| 1633 | e = &vcpu->cpuid_entries[i]; |
| 1634 | if (e->function == function) { |
| 1635 | best = e; |
| 1636 | break; |
| 1637 | } |
| 1638 | /* |
| 1639 | * Both basic or both extended? |
| 1640 | */ |
| 1641 | if (((e->function ^ function) & 0x80000000) == 0) |
| 1642 | if (!best || e->function > best->function) |
| 1643 | best = e; |
| 1644 | } |
| 1645 | if (best) { |
| 1646 | vcpu->regs[VCPU_REGS_RAX] = best->eax; |
| 1647 | vcpu->regs[VCPU_REGS_RBX] = best->ebx; |
| 1648 | vcpu->regs[VCPU_REGS_RCX] = best->ecx; |
| 1649 | vcpu->regs[VCPU_REGS_RDX] = best->edx; |
| 1650 | } |
| 1651 | kvm_arch_ops->decache_regs(vcpu); |
| 1652 | kvm_arch_ops->skip_emulated_instruction(vcpu); |
| 1653 | } |
| 1654 | EXPORT_SYMBOL_GPL(kvm_emulate_cpuid); |
| 1655 | |
Avi Kivity | 039576c | 2007-03-20 12:46:50 +0200 | [diff] [blame] | 1656 | static int pio_copy_data(struct kvm_vcpu *vcpu) |
Avi Kivity | 46fc147 | 2007-02-22 19:39:30 +0200 | [diff] [blame] | 1657 | { |
Avi Kivity | 039576c | 2007-03-20 12:46:50 +0200 | [diff] [blame] | 1658 | void *p = vcpu->pio_data; |
| 1659 | void *q; |
| 1660 | unsigned bytes; |
| 1661 | int nr_pages = vcpu->pio.guest_pages[1] ? 2 : 1; |
| 1662 | |
| 1663 | kvm_arch_ops->vcpu_put(vcpu); |
| 1664 | q = vmap(vcpu->pio.guest_pages, nr_pages, VM_READ|VM_WRITE, |
| 1665 | PAGE_KERNEL); |
| 1666 | if (!q) { |
| 1667 | kvm_arch_ops->vcpu_load(vcpu); |
| 1668 | free_pio_guest_pages(vcpu); |
| 1669 | return -ENOMEM; |
| 1670 | } |
| 1671 | q += vcpu->pio.guest_page_offset; |
| 1672 | bytes = vcpu->pio.size * vcpu->pio.cur_count; |
| 1673 | if (vcpu->pio.in) |
| 1674 | memcpy(q, p, bytes); |
| 1675 | else |
| 1676 | memcpy(p, q, bytes); |
| 1677 | q -= vcpu->pio.guest_page_offset; |
| 1678 | vunmap(q); |
| 1679 | kvm_arch_ops->vcpu_load(vcpu); |
| 1680 | free_pio_guest_pages(vcpu); |
| 1681 | return 0; |
| 1682 | } |
| 1683 | |
| 1684 | static int complete_pio(struct kvm_vcpu *vcpu) |
| 1685 | { |
| 1686 | struct kvm_pio_request *io = &vcpu->pio; |
Avi Kivity | 46fc147 | 2007-02-22 19:39:30 +0200 | [diff] [blame] | 1687 | long delta; |
Avi Kivity | 039576c | 2007-03-20 12:46:50 +0200 | [diff] [blame] | 1688 | int r; |
Avi Kivity | 46fc147 | 2007-02-22 19:39:30 +0200 | [diff] [blame] | 1689 | |
| 1690 | kvm_arch_ops->cache_regs(vcpu); |
| 1691 | |
| 1692 | if (!io->string) { |
Avi Kivity | 039576c | 2007-03-20 12:46:50 +0200 | [diff] [blame] | 1693 | if (io->in) |
| 1694 | memcpy(&vcpu->regs[VCPU_REGS_RAX], vcpu->pio_data, |
Avi Kivity | 46fc147 | 2007-02-22 19:39:30 +0200 | [diff] [blame] | 1695 | io->size); |
| 1696 | } else { |
Avi Kivity | 039576c | 2007-03-20 12:46:50 +0200 | [diff] [blame] | 1697 | if (io->in) { |
| 1698 | r = pio_copy_data(vcpu); |
| 1699 | if (r) { |
| 1700 | kvm_arch_ops->cache_regs(vcpu); |
| 1701 | return r; |
| 1702 | } |
| 1703 | } |
| 1704 | |
Avi Kivity | 46fc147 | 2007-02-22 19:39:30 +0200 | [diff] [blame] | 1705 | delta = 1; |
| 1706 | if (io->rep) { |
Avi Kivity | 039576c | 2007-03-20 12:46:50 +0200 | [diff] [blame] | 1707 | delta *= io->cur_count; |
Avi Kivity | 46fc147 | 2007-02-22 19:39:30 +0200 | [diff] [blame] | 1708 | /* |
| 1709 | * The size of the register should really depend on |
| 1710 | * current address size. |
| 1711 | */ |
| 1712 | vcpu->regs[VCPU_REGS_RCX] -= delta; |
| 1713 | } |
Avi Kivity | 039576c | 2007-03-20 12:46:50 +0200 | [diff] [blame] | 1714 | if (io->down) |
Avi Kivity | 46fc147 | 2007-02-22 19:39:30 +0200 | [diff] [blame] | 1715 | delta = -delta; |
| 1716 | delta *= io->size; |
Avi Kivity | 039576c | 2007-03-20 12:46:50 +0200 | [diff] [blame] | 1717 | if (io->in) |
Avi Kivity | 46fc147 | 2007-02-22 19:39:30 +0200 | [diff] [blame] | 1718 | vcpu->regs[VCPU_REGS_RDI] += delta; |
| 1719 | else |
| 1720 | vcpu->regs[VCPU_REGS_RSI] += delta; |
| 1721 | } |
| 1722 | |
Avi Kivity | 46fc147 | 2007-02-22 19:39:30 +0200 | [diff] [blame] | 1723 | vcpu->run->io_completed = 0; |
| 1724 | |
| 1725 | kvm_arch_ops->decache_regs(vcpu); |
| 1726 | |
Avi Kivity | 039576c | 2007-03-20 12:46:50 +0200 | [diff] [blame] | 1727 | io->count -= io->cur_count; |
| 1728 | io->cur_count = 0; |
| 1729 | |
| 1730 | if (!io->count) |
| 1731 | kvm_arch_ops->skip_emulated_instruction(vcpu); |
| 1732 | return 0; |
Avi Kivity | 46fc147 | 2007-02-22 19:39:30 +0200 | [diff] [blame] | 1733 | } |
| 1734 | |
Avi Kivity | 039576c | 2007-03-20 12:46:50 +0200 | [diff] [blame] | 1735 | int kvm_setup_pio(struct kvm_vcpu *vcpu, struct kvm_run *run, int in, |
| 1736 | int size, unsigned long count, int string, int down, |
| 1737 | gva_t address, int rep, unsigned port) |
| 1738 | { |
| 1739 | unsigned now, in_page; |
| 1740 | int i; |
| 1741 | int nr_pages = 1; |
| 1742 | struct page *page; |
| 1743 | |
| 1744 | vcpu->run->exit_reason = KVM_EXIT_IO; |
| 1745 | vcpu->run->io.direction = in ? KVM_EXIT_IO_IN : KVM_EXIT_IO_OUT; |
| 1746 | vcpu->run->io.size = size; |
| 1747 | vcpu->run->io.data_offset = KVM_PIO_PAGE_OFFSET * PAGE_SIZE; |
| 1748 | vcpu->run->io.count = count; |
| 1749 | vcpu->run->io.port = port; |
| 1750 | vcpu->pio.count = count; |
| 1751 | vcpu->pio.cur_count = count; |
| 1752 | vcpu->pio.size = size; |
| 1753 | vcpu->pio.in = in; |
| 1754 | vcpu->pio.string = string; |
| 1755 | vcpu->pio.down = down; |
| 1756 | vcpu->pio.guest_page_offset = offset_in_page(address); |
| 1757 | vcpu->pio.rep = rep; |
| 1758 | |
| 1759 | if (!string) { |
| 1760 | kvm_arch_ops->cache_regs(vcpu); |
| 1761 | memcpy(vcpu->pio_data, &vcpu->regs[VCPU_REGS_RAX], 4); |
| 1762 | kvm_arch_ops->decache_regs(vcpu); |
| 1763 | return 0; |
| 1764 | } |
| 1765 | |
| 1766 | if (!count) { |
| 1767 | kvm_arch_ops->skip_emulated_instruction(vcpu); |
| 1768 | return 1; |
| 1769 | } |
| 1770 | |
| 1771 | now = min(count, PAGE_SIZE / size); |
| 1772 | |
| 1773 | if (!down) |
| 1774 | in_page = PAGE_SIZE - offset_in_page(address); |
| 1775 | else |
| 1776 | in_page = offset_in_page(address) + size; |
| 1777 | now = min(count, (unsigned long)in_page / size); |
| 1778 | if (!now) { |
| 1779 | /* |
| 1780 | * String I/O straddles page boundary. Pin two guest pages |
| 1781 | * so that we satisfy atomicity constraints. Do just one |
| 1782 | * transaction to avoid complexity. |
| 1783 | */ |
| 1784 | nr_pages = 2; |
| 1785 | now = 1; |
| 1786 | } |
| 1787 | if (down) { |
| 1788 | /* |
| 1789 | * String I/O in reverse. Yuck. Kill the guest, fix later. |
| 1790 | */ |
| 1791 | printk(KERN_ERR "kvm: guest string pio down\n"); |
| 1792 | inject_gp(vcpu); |
| 1793 | return 1; |
| 1794 | } |
| 1795 | vcpu->run->io.count = now; |
| 1796 | vcpu->pio.cur_count = now; |
| 1797 | |
| 1798 | for (i = 0; i < nr_pages; ++i) { |
| 1799 | spin_lock(&vcpu->kvm->lock); |
| 1800 | page = gva_to_page(vcpu, address + i * PAGE_SIZE); |
| 1801 | if (page) |
| 1802 | get_page(page); |
| 1803 | vcpu->pio.guest_pages[i] = page; |
| 1804 | spin_unlock(&vcpu->kvm->lock); |
| 1805 | if (!page) { |
| 1806 | inject_gp(vcpu); |
| 1807 | free_pio_guest_pages(vcpu); |
| 1808 | return 1; |
| 1809 | } |
| 1810 | } |
| 1811 | |
| 1812 | if (!vcpu->pio.in) |
| 1813 | return pio_copy_data(vcpu); |
| 1814 | return 0; |
| 1815 | } |
| 1816 | EXPORT_SYMBOL_GPL(kvm_setup_pio); |
| 1817 | |
Avi Kivity | bccf215 | 2007-02-21 18:04:26 +0200 | [diff] [blame] | 1818 | static int kvm_vcpu_ioctl_run(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1819 | { |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1820 | int r; |
Avi Kivity | 1961d27 | 2007-03-05 19:46:05 +0200 | [diff] [blame] | 1821 | sigset_t sigsaved; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1822 | |
Avi Kivity | bccf215 | 2007-02-21 18:04:26 +0200 | [diff] [blame] | 1823 | vcpu_load(vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1824 | |
Avi Kivity | 1961d27 | 2007-03-05 19:46:05 +0200 | [diff] [blame] | 1825 | if (vcpu->sigset_active) |
| 1826 | sigprocmask(SIG_SETMASK, &vcpu->sigset, &sigsaved); |
| 1827 | |
Dor Laor | 5481034 | 2007-02-12 00:54:39 -0800 | [diff] [blame] | 1828 | /* re-sync apic's tpr */ |
| 1829 | vcpu->cr8 = kvm_run->cr8; |
| 1830 | |
Avi Kivity | 46fc147 | 2007-02-22 19:39:30 +0200 | [diff] [blame] | 1831 | if (kvm_run->io_completed) { |
Avi Kivity | 039576c | 2007-03-20 12:46:50 +0200 | [diff] [blame] | 1832 | if (vcpu->pio.cur_count) { |
| 1833 | r = complete_pio(vcpu); |
| 1834 | if (r) |
| 1835 | goto out; |
| 1836 | } else { |
Avi Kivity | 46fc147 | 2007-02-22 19:39:30 +0200 | [diff] [blame] | 1837 | memcpy(vcpu->mmio_data, kvm_run->mmio.data, 8); |
| 1838 | vcpu->mmio_read_completed = 1; |
| 1839 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1840 | } |
| 1841 | |
| 1842 | vcpu->mmio_needed = 0; |
| 1843 | |
Avi Kivity | 8eb7d33 | 2007-03-04 14:17:08 +0200 | [diff] [blame] | 1844 | if (kvm_run->exit_reason == KVM_EXIT_HYPERCALL) { |
Avi Kivity | b4e63f5 | 2007-03-04 13:59:30 +0200 | [diff] [blame] | 1845 | kvm_arch_ops->cache_regs(vcpu); |
| 1846 | vcpu->regs[VCPU_REGS_RAX] = kvm_run->hypercall.ret; |
| 1847 | kvm_arch_ops->decache_regs(vcpu); |
| 1848 | } |
| 1849 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1850 | r = kvm_arch_ops->run(vcpu, kvm_run); |
| 1851 | |
Avi Kivity | 039576c | 2007-03-20 12:46:50 +0200 | [diff] [blame] | 1852 | out: |
Avi Kivity | 1961d27 | 2007-03-05 19:46:05 +0200 | [diff] [blame] | 1853 | if (vcpu->sigset_active) |
| 1854 | sigprocmask(SIG_SETMASK, &sigsaved, NULL); |
| 1855 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1856 | vcpu_put(vcpu); |
| 1857 | return r; |
| 1858 | } |
| 1859 | |
Avi Kivity | bccf215 | 2007-02-21 18:04:26 +0200 | [diff] [blame] | 1860 | static int kvm_vcpu_ioctl_get_regs(struct kvm_vcpu *vcpu, |
| 1861 | struct kvm_regs *regs) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1862 | { |
Avi Kivity | bccf215 | 2007-02-21 18:04:26 +0200 | [diff] [blame] | 1863 | vcpu_load(vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1864 | |
| 1865 | kvm_arch_ops->cache_regs(vcpu); |
| 1866 | |
| 1867 | regs->rax = vcpu->regs[VCPU_REGS_RAX]; |
| 1868 | regs->rbx = vcpu->regs[VCPU_REGS_RBX]; |
| 1869 | regs->rcx = vcpu->regs[VCPU_REGS_RCX]; |
| 1870 | regs->rdx = vcpu->regs[VCPU_REGS_RDX]; |
| 1871 | regs->rsi = vcpu->regs[VCPU_REGS_RSI]; |
| 1872 | regs->rdi = vcpu->regs[VCPU_REGS_RDI]; |
| 1873 | regs->rsp = vcpu->regs[VCPU_REGS_RSP]; |
| 1874 | regs->rbp = vcpu->regs[VCPU_REGS_RBP]; |
Avi Kivity | 05b3e0c | 2006-12-13 00:33:45 -0800 | [diff] [blame] | 1875 | #ifdef CONFIG_X86_64 |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1876 | regs->r8 = vcpu->regs[VCPU_REGS_R8]; |
| 1877 | regs->r9 = vcpu->regs[VCPU_REGS_R9]; |
| 1878 | regs->r10 = vcpu->regs[VCPU_REGS_R10]; |
| 1879 | regs->r11 = vcpu->regs[VCPU_REGS_R11]; |
| 1880 | regs->r12 = vcpu->regs[VCPU_REGS_R12]; |
| 1881 | regs->r13 = vcpu->regs[VCPU_REGS_R13]; |
| 1882 | regs->r14 = vcpu->regs[VCPU_REGS_R14]; |
| 1883 | regs->r15 = vcpu->regs[VCPU_REGS_R15]; |
| 1884 | #endif |
| 1885 | |
| 1886 | regs->rip = vcpu->rip; |
| 1887 | regs->rflags = kvm_arch_ops->get_rflags(vcpu); |
| 1888 | |
| 1889 | /* |
| 1890 | * Don't leak debug flags in case they were set for guest debugging |
| 1891 | */ |
| 1892 | if (vcpu->guest_debug.enabled && vcpu->guest_debug.singlestep) |
| 1893 | regs->rflags &= ~(X86_EFLAGS_TF | X86_EFLAGS_RF); |
| 1894 | |
| 1895 | vcpu_put(vcpu); |
| 1896 | |
| 1897 | return 0; |
| 1898 | } |
| 1899 | |
Avi Kivity | bccf215 | 2007-02-21 18:04:26 +0200 | [diff] [blame] | 1900 | static int kvm_vcpu_ioctl_set_regs(struct kvm_vcpu *vcpu, |
| 1901 | struct kvm_regs *regs) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1902 | { |
Avi Kivity | bccf215 | 2007-02-21 18:04:26 +0200 | [diff] [blame] | 1903 | vcpu_load(vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1904 | |
| 1905 | vcpu->regs[VCPU_REGS_RAX] = regs->rax; |
| 1906 | vcpu->regs[VCPU_REGS_RBX] = regs->rbx; |
| 1907 | vcpu->regs[VCPU_REGS_RCX] = regs->rcx; |
| 1908 | vcpu->regs[VCPU_REGS_RDX] = regs->rdx; |
| 1909 | vcpu->regs[VCPU_REGS_RSI] = regs->rsi; |
| 1910 | vcpu->regs[VCPU_REGS_RDI] = regs->rdi; |
| 1911 | vcpu->regs[VCPU_REGS_RSP] = regs->rsp; |
| 1912 | vcpu->regs[VCPU_REGS_RBP] = regs->rbp; |
Avi Kivity | 05b3e0c | 2006-12-13 00:33:45 -0800 | [diff] [blame] | 1913 | #ifdef CONFIG_X86_64 |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1914 | vcpu->regs[VCPU_REGS_R8] = regs->r8; |
| 1915 | vcpu->regs[VCPU_REGS_R9] = regs->r9; |
| 1916 | vcpu->regs[VCPU_REGS_R10] = regs->r10; |
| 1917 | vcpu->regs[VCPU_REGS_R11] = regs->r11; |
| 1918 | vcpu->regs[VCPU_REGS_R12] = regs->r12; |
| 1919 | vcpu->regs[VCPU_REGS_R13] = regs->r13; |
| 1920 | vcpu->regs[VCPU_REGS_R14] = regs->r14; |
| 1921 | vcpu->regs[VCPU_REGS_R15] = regs->r15; |
| 1922 | #endif |
| 1923 | |
| 1924 | vcpu->rip = regs->rip; |
| 1925 | kvm_arch_ops->set_rflags(vcpu, regs->rflags); |
| 1926 | |
| 1927 | kvm_arch_ops->decache_regs(vcpu); |
| 1928 | |
| 1929 | vcpu_put(vcpu); |
| 1930 | |
| 1931 | return 0; |
| 1932 | } |
| 1933 | |
| 1934 | static void get_segment(struct kvm_vcpu *vcpu, |
| 1935 | struct kvm_segment *var, int seg) |
| 1936 | { |
| 1937 | return kvm_arch_ops->get_segment(vcpu, var, seg); |
| 1938 | } |
| 1939 | |
Avi Kivity | bccf215 | 2007-02-21 18:04:26 +0200 | [diff] [blame] | 1940 | static int kvm_vcpu_ioctl_get_sregs(struct kvm_vcpu *vcpu, |
| 1941 | struct kvm_sregs *sregs) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1942 | { |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1943 | struct descriptor_table dt; |
| 1944 | |
Avi Kivity | bccf215 | 2007-02-21 18:04:26 +0200 | [diff] [blame] | 1945 | vcpu_load(vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1946 | |
| 1947 | get_segment(vcpu, &sregs->cs, VCPU_SREG_CS); |
| 1948 | get_segment(vcpu, &sregs->ds, VCPU_SREG_DS); |
| 1949 | get_segment(vcpu, &sregs->es, VCPU_SREG_ES); |
| 1950 | get_segment(vcpu, &sregs->fs, VCPU_SREG_FS); |
| 1951 | get_segment(vcpu, &sregs->gs, VCPU_SREG_GS); |
| 1952 | get_segment(vcpu, &sregs->ss, VCPU_SREG_SS); |
| 1953 | |
| 1954 | get_segment(vcpu, &sregs->tr, VCPU_SREG_TR); |
| 1955 | get_segment(vcpu, &sregs->ldt, VCPU_SREG_LDTR); |
| 1956 | |
| 1957 | kvm_arch_ops->get_idt(vcpu, &dt); |
| 1958 | sregs->idt.limit = dt.limit; |
| 1959 | sregs->idt.base = dt.base; |
| 1960 | kvm_arch_ops->get_gdt(vcpu, &dt); |
| 1961 | sregs->gdt.limit = dt.limit; |
| 1962 | sregs->gdt.base = dt.base; |
| 1963 | |
Avi Kivity | 399badf | 2007-01-05 16:36:38 -0800 | [diff] [blame] | 1964 | kvm_arch_ops->decache_cr0_cr4_guest_bits(vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1965 | sregs->cr0 = vcpu->cr0; |
| 1966 | sregs->cr2 = vcpu->cr2; |
| 1967 | sregs->cr3 = vcpu->cr3; |
| 1968 | sregs->cr4 = vcpu->cr4; |
| 1969 | sregs->cr8 = vcpu->cr8; |
| 1970 | sregs->efer = vcpu->shadow_efer; |
| 1971 | sregs->apic_base = vcpu->apic_base; |
| 1972 | |
| 1973 | memcpy(sregs->interrupt_bitmap, vcpu->irq_pending, |
| 1974 | sizeof sregs->interrupt_bitmap); |
| 1975 | |
| 1976 | vcpu_put(vcpu); |
| 1977 | |
| 1978 | return 0; |
| 1979 | } |
| 1980 | |
| 1981 | static void set_segment(struct kvm_vcpu *vcpu, |
| 1982 | struct kvm_segment *var, int seg) |
| 1983 | { |
| 1984 | return kvm_arch_ops->set_segment(vcpu, var, seg); |
| 1985 | } |
| 1986 | |
Avi Kivity | bccf215 | 2007-02-21 18:04:26 +0200 | [diff] [blame] | 1987 | static int kvm_vcpu_ioctl_set_sregs(struct kvm_vcpu *vcpu, |
| 1988 | struct kvm_sregs *sregs) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1989 | { |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1990 | int mmu_reset_needed = 0; |
| 1991 | int i; |
| 1992 | struct descriptor_table dt; |
| 1993 | |
Avi Kivity | bccf215 | 2007-02-21 18:04:26 +0200 | [diff] [blame] | 1994 | vcpu_load(vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1995 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1996 | dt.limit = sregs->idt.limit; |
| 1997 | dt.base = sregs->idt.base; |
| 1998 | kvm_arch_ops->set_idt(vcpu, &dt); |
| 1999 | dt.limit = sregs->gdt.limit; |
| 2000 | dt.base = sregs->gdt.base; |
| 2001 | kvm_arch_ops->set_gdt(vcpu, &dt); |
| 2002 | |
| 2003 | vcpu->cr2 = sregs->cr2; |
| 2004 | mmu_reset_needed |= vcpu->cr3 != sregs->cr3; |
| 2005 | vcpu->cr3 = sregs->cr3; |
| 2006 | |
| 2007 | vcpu->cr8 = sregs->cr8; |
| 2008 | |
| 2009 | mmu_reset_needed |= vcpu->shadow_efer != sregs->efer; |
Avi Kivity | 05b3e0c | 2006-12-13 00:33:45 -0800 | [diff] [blame] | 2010 | #ifdef CONFIG_X86_64 |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2011 | kvm_arch_ops->set_efer(vcpu, sregs->efer); |
| 2012 | #endif |
| 2013 | vcpu->apic_base = sregs->apic_base; |
| 2014 | |
Avi Kivity | 399badf | 2007-01-05 16:36:38 -0800 | [diff] [blame] | 2015 | kvm_arch_ops->decache_cr0_cr4_guest_bits(vcpu); |
| 2016 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2017 | mmu_reset_needed |= vcpu->cr0 != sregs->cr0; |
Avi Kivity | f6528b0 | 2007-03-20 18:44:51 +0200 | [diff] [blame] | 2018 | kvm_arch_ops->set_cr0(vcpu, sregs->cr0); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2019 | |
| 2020 | mmu_reset_needed |= vcpu->cr4 != sregs->cr4; |
| 2021 | kvm_arch_ops->set_cr4(vcpu, sregs->cr4); |
Avi Kivity | 1b0973b | 2007-01-05 16:36:41 -0800 | [diff] [blame] | 2022 | if (!is_long_mode(vcpu) && is_pae(vcpu)) |
| 2023 | load_pdptrs(vcpu, vcpu->cr3); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2024 | |
| 2025 | if (mmu_reset_needed) |
| 2026 | kvm_mmu_reset_context(vcpu); |
| 2027 | |
| 2028 | memcpy(vcpu->irq_pending, sregs->interrupt_bitmap, |
| 2029 | sizeof vcpu->irq_pending); |
| 2030 | vcpu->irq_summary = 0; |
| 2031 | for (i = 0; i < NR_IRQ_WORDS; ++i) |
| 2032 | if (vcpu->irq_pending[i]) |
| 2033 | __set_bit(i, &vcpu->irq_summary); |
| 2034 | |
Avi Kivity | 024aa1c | 2007-03-21 13:44:58 +0200 | [diff] [blame] | 2035 | set_segment(vcpu, &sregs->cs, VCPU_SREG_CS); |
| 2036 | set_segment(vcpu, &sregs->ds, VCPU_SREG_DS); |
| 2037 | set_segment(vcpu, &sregs->es, VCPU_SREG_ES); |
| 2038 | set_segment(vcpu, &sregs->fs, VCPU_SREG_FS); |
| 2039 | set_segment(vcpu, &sregs->gs, VCPU_SREG_GS); |
| 2040 | set_segment(vcpu, &sregs->ss, VCPU_SREG_SS); |
| 2041 | |
| 2042 | set_segment(vcpu, &sregs->tr, VCPU_SREG_TR); |
| 2043 | set_segment(vcpu, &sregs->ldt, VCPU_SREG_LDTR); |
| 2044 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2045 | vcpu_put(vcpu); |
| 2046 | |
| 2047 | return 0; |
| 2048 | } |
| 2049 | |
| 2050 | /* |
| 2051 | * List of msr numbers which we expose to userspace through KVM_GET_MSRS |
| 2052 | * and KVM_SET_MSRS, and KVM_GET_MSR_INDEX_LIST. |
Michael Riepe | bf591b2 | 2006-12-22 01:05:36 -0800 | [diff] [blame] | 2053 | * |
| 2054 | * This list is modified at module load time to reflect the |
| 2055 | * capabilities of the host cpu. |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2056 | */ |
| 2057 | static u32 msrs_to_save[] = { |
| 2058 | MSR_IA32_SYSENTER_CS, MSR_IA32_SYSENTER_ESP, MSR_IA32_SYSENTER_EIP, |
| 2059 | MSR_K6_STAR, |
Avi Kivity | 05b3e0c | 2006-12-13 00:33:45 -0800 | [diff] [blame] | 2060 | #ifdef CONFIG_X86_64 |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2061 | MSR_CSTAR, MSR_KERNEL_GS_BASE, MSR_SYSCALL_MASK, MSR_LSTAR, |
| 2062 | #endif |
| 2063 | MSR_IA32_TIME_STAMP_COUNTER, |
| 2064 | }; |
| 2065 | |
Michael Riepe | bf591b2 | 2006-12-22 01:05:36 -0800 | [diff] [blame] | 2066 | static unsigned num_msrs_to_save; |
| 2067 | |
Avi Kivity | 6f00e68 | 2007-01-26 00:56:40 -0800 | [diff] [blame] | 2068 | static u32 emulated_msrs[] = { |
| 2069 | MSR_IA32_MISC_ENABLE, |
| 2070 | }; |
| 2071 | |
Michael Riepe | bf591b2 | 2006-12-22 01:05:36 -0800 | [diff] [blame] | 2072 | static __init void kvm_init_msr_list(void) |
| 2073 | { |
| 2074 | u32 dummy[2]; |
| 2075 | unsigned i, j; |
| 2076 | |
| 2077 | for (i = j = 0; i < ARRAY_SIZE(msrs_to_save); i++) { |
| 2078 | if (rdmsr_safe(msrs_to_save[i], &dummy[0], &dummy[1]) < 0) |
| 2079 | continue; |
| 2080 | if (j < i) |
| 2081 | msrs_to_save[j] = msrs_to_save[i]; |
| 2082 | j++; |
| 2083 | } |
| 2084 | num_msrs_to_save = j; |
| 2085 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2086 | |
| 2087 | /* |
| 2088 | * Adapt set_msr() to msr_io()'s calling convention |
| 2089 | */ |
| 2090 | static int do_set_msr(struct kvm_vcpu *vcpu, unsigned index, u64 *data) |
| 2091 | { |
| 2092 | return set_msr(vcpu, index, *data); |
| 2093 | } |
| 2094 | |
| 2095 | /* |
| 2096 | * Read or write a bunch of msrs. All parameters are kernel addresses. |
| 2097 | * |
| 2098 | * @return number of msrs set successfully. |
| 2099 | */ |
Avi Kivity | bccf215 | 2007-02-21 18:04:26 +0200 | [diff] [blame] | 2100 | static int __msr_io(struct kvm_vcpu *vcpu, struct kvm_msrs *msrs, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2101 | struct kvm_msr_entry *entries, |
| 2102 | int (*do_msr)(struct kvm_vcpu *vcpu, |
| 2103 | unsigned index, u64 *data)) |
| 2104 | { |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2105 | int i; |
| 2106 | |
Avi Kivity | bccf215 | 2007-02-21 18:04:26 +0200 | [diff] [blame] | 2107 | vcpu_load(vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2108 | |
| 2109 | for (i = 0; i < msrs->nmsrs; ++i) |
| 2110 | if (do_msr(vcpu, entries[i].index, &entries[i].data)) |
| 2111 | break; |
| 2112 | |
| 2113 | vcpu_put(vcpu); |
| 2114 | |
| 2115 | return i; |
| 2116 | } |
| 2117 | |
| 2118 | /* |
| 2119 | * Read or write a bunch of msrs. Parameters are user addresses. |
| 2120 | * |
| 2121 | * @return number of msrs set successfully. |
| 2122 | */ |
Avi Kivity | bccf215 | 2007-02-21 18:04:26 +0200 | [diff] [blame] | 2123 | static int msr_io(struct kvm_vcpu *vcpu, struct kvm_msrs __user *user_msrs, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2124 | int (*do_msr)(struct kvm_vcpu *vcpu, |
| 2125 | unsigned index, u64 *data), |
| 2126 | int writeback) |
| 2127 | { |
| 2128 | struct kvm_msrs msrs; |
| 2129 | struct kvm_msr_entry *entries; |
| 2130 | int r, n; |
| 2131 | unsigned size; |
| 2132 | |
| 2133 | r = -EFAULT; |
| 2134 | if (copy_from_user(&msrs, user_msrs, sizeof msrs)) |
| 2135 | goto out; |
| 2136 | |
| 2137 | r = -E2BIG; |
| 2138 | if (msrs.nmsrs >= MAX_IO_MSRS) |
| 2139 | goto out; |
| 2140 | |
| 2141 | r = -ENOMEM; |
| 2142 | size = sizeof(struct kvm_msr_entry) * msrs.nmsrs; |
| 2143 | entries = vmalloc(size); |
| 2144 | if (!entries) |
| 2145 | goto out; |
| 2146 | |
| 2147 | r = -EFAULT; |
| 2148 | if (copy_from_user(entries, user_msrs->entries, size)) |
| 2149 | goto out_free; |
| 2150 | |
Avi Kivity | bccf215 | 2007-02-21 18:04:26 +0200 | [diff] [blame] | 2151 | r = n = __msr_io(vcpu, &msrs, entries, do_msr); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2152 | if (r < 0) |
| 2153 | goto out_free; |
| 2154 | |
| 2155 | r = -EFAULT; |
| 2156 | if (writeback && copy_to_user(user_msrs->entries, entries, size)) |
| 2157 | goto out_free; |
| 2158 | |
| 2159 | r = n; |
| 2160 | |
| 2161 | out_free: |
| 2162 | vfree(entries); |
| 2163 | out: |
| 2164 | return r; |
| 2165 | } |
| 2166 | |
| 2167 | /* |
| 2168 | * Translate a guest virtual address to a guest physical address. |
| 2169 | */ |
Avi Kivity | bccf215 | 2007-02-21 18:04:26 +0200 | [diff] [blame] | 2170 | static int kvm_vcpu_ioctl_translate(struct kvm_vcpu *vcpu, |
| 2171 | struct kvm_translation *tr) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2172 | { |
| 2173 | unsigned long vaddr = tr->linear_address; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2174 | gpa_t gpa; |
| 2175 | |
Avi Kivity | bccf215 | 2007-02-21 18:04:26 +0200 | [diff] [blame] | 2176 | vcpu_load(vcpu); |
| 2177 | spin_lock(&vcpu->kvm->lock); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2178 | gpa = vcpu->mmu.gva_to_gpa(vcpu, vaddr); |
| 2179 | tr->physical_address = gpa; |
| 2180 | tr->valid = gpa != UNMAPPED_GVA; |
| 2181 | tr->writeable = 1; |
| 2182 | tr->usermode = 0; |
Avi Kivity | bccf215 | 2007-02-21 18:04:26 +0200 | [diff] [blame] | 2183 | spin_unlock(&vcpu->kvm->lock); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2184 | vcpu_put(vcpu); |
| 2185 | |
| 2186 | return 0; |
| 2187 | } |
| 2188 | |
Avi Kivity | bccf215 | 2007-02-21 18:04:26 +0200 | [diff] [blame] | 2189 | static int kvm_vcpu_ioctl_interrupt(struct kvm_vcpu *vcpu, |
| 2190 | struct kvm_interrupt *irq) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2191 | { |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2192 | if (irq->irq < 0 || irq->irq >= 256) |
| 2193 | return -EINVAL; |
Avi Kivity | bccf215 | 2007-02-21 18:04:26 +0200 | [diff] [blame] | 2194 | vcpu_load(vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2195 | |
| 2196 | set_bit(irq->irq, vcpu->irq_pending); |
| 2197 | set_bit(irq->irq / BITS_PER_LONG, &vcpu->irq_summary); |
| 2198 | |
| 2199 | vcpu_put(vcpu); |
| 2200 | |
| 2201 | return 0; |
| 2202 | } |
| 2203 | |
Avi Kivity | bccf215 | 2007-02-21 18:04:26 +0200 | [diff] [blame] | 2204 | static int kvm_vcpu_ioctl_debug_guest(struct kvm_vcpu *vcpu, |
| 2205 | struct kvm_debug_guest *dbg) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2206 | { |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2207 | int r; |
| 2208 | |
Avi Kivity | bccf215 | 2007-02-21 18:04:26 +0200 | [diff] [blame] | 2209 | vcpu_load(vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2210 | |
| 2211 | r = kvm_arch_ops->set_guest_debug(vcpu, dbg); |
| 2212 | |
| 2213 | vcpu_put(vcpu); |
| 2214 | |
| 2215 | return r; |
| 2216 | } |
| 2217 | |
Avi Kivity | 9a2bb7f | 2007-02-22 12:58:31 +0200 | [diff] [blame] | 2218 | static struct page *kvm_vcpu_nopage(struct vm_area_struct *vma, |
| 2219 | unsigned long address, |
| 2220 | int *type) |
| 2221 | { |
| 2222 | struct kvm_vcpu *vcpu = vma->vm_file->private_data; |
| 2223 | unsigned long pgoff; |
| 2224 | struct page *page; |
| 2225 | |
| 2226 | *type = VM_FAULT_MINOR; |
| 2227 | pgoff = ((address - vma->vm_start) >> PAGE_SHIFT) + vma->vm_pgoff; |
Avi Kivity | 039576c | 2007-03-20 12:46:50 +0200 | [diff] [blame] | 2228 | if (pgoff == 0) |
| 2229 | page = virt_to_page(vcpu->run); |
| 2230 | else if (pgoff == KVM_PIO_PAGE_OFFSET) |
| 2231 | page = virt_to_page(vcpu->pio_data); |
| 2232 | else |
Avi Kivity | 9a2bb7f | 2007-02-22 12:58:31 +0200 | [diff] [blame] | 2233 | return NOPAGE_SIGBUS; |
Avi Kivity | 9a2bb7f | 2007-02-22 12:58:31 +0200 | [diff] [blame] | 2234 | get_page(page); |
| 2235 | return page; |
| 2236 | } |
| 2237 | |
| 2238 | static struct vm_operations_struct kvm_vcpu_vm_ops = { |
| 2239 | .nopage = kvm_vcpu_nopage, |
| 2240 | }; |
| 2241 | |
| 2242 | static int kvm_vcpu_mmap(struct file *file, struct vm_area_struct *vma) |
| 2243 | { |
| 2244 | vma->vm_ops = &kvm_vcpu_vm_ops; |
| 2245 | return 0; |
| 2246 | } |
| 2247 | |
Avi Kivity | bccf215 | 2007-02-21 18:04:26 +0200 | [diff] [blame] | 2248 | static int kvm_vcpu_release(struct inode *inode, struct file *filp) |
| 2249 | { |
| 2250 | struct kvm_vcpu *vcpu = filp->private_data; |
| 2251 | |
| 2252 | fput(vcpu->kvm->filp); |
| 2253 | return 0; |
| 2254 | } |
| 2255 | |
| 2256 | static struct file_operations kvm_vcpu_fops = { |
| 2257 | .release = kvm_vcpu_release, |
| 2258 | .unlocked_ioctl = kvm_vcpu_ioctl, |
| 2259 | .compat_ioctl = kvm_vcpu_ioctl, |
Avi Kivity | 9a2bb7f | 2007-02-22 12:58:31 +0200 | [diff] [blame] | 2260 | .mmap = kvm_vcpu_mmap, |
Avi Kivity | bccf215 | 2007-02-21 18:04:26 +0200 | [diff] [blame] | 2261 | }; |
| 2262 | |
| 2263 | /* |
| 2264 | * Allocates an inode for the vcpu. |
| 2265 | */ |
| 2266 | static int create_vcpu_fd(struct kvm_vcpu *vcpu) |
| 2267 | { |
| 2268 | int fd, r; |
| 2269 | struct inode *inode; |
| 2270 | struct file *file; |
| 2271 | |
| 2272 | atomic_inc(&vcpu->kvm->filp->f_count); |
| 2273 | inode = kvmfs_inode(&kvm_vcpu_fops); |
| 2274 | if (IS_ERR(inode)) { |
| 2275 | r = PTR_ERR(inode); |
| 2276 | goto out1; |
| 2277 | } |
| 2278 | |
| 2279 | file = kvmfs_file(inode, vcpu); |
| 2280 | if (IS_ERR(file)) { |
| 2281 | r = PTR_ERR(file); |
| 2282 | goto out2; |
| 2283 | } |
| 2284 | |
| 2285 | r = get_unused_fd(); |
| 2286 | if (r < 0) |
| 2287 | goto out3; |
| 2288 | fd = r; |
| 2289 | fd_install(fd, file); |
| 2290 | |
| 2291 | return fd; |
| 2292 | |
| 2293 | out3: |
| 2294 | fput(file); |
| 2295 | out2: |
| 2296 | iput(inode); |
| 2297 | out1: |
| 2298 | fput(vcpu->kvm->filp); |
| 2299 | return r; |
| 2300 | } |
| 2301 | |
Avi Kivity | c5ea766 | 2007-02-20 18:41:05 +0200 | [diff] [blame] | 2302 | /* |
| 2303 | * Creates some virtual cpus. Good luck creating more than one. |
| 2304 | */ |
| 2305 | static int kvm_vm_ioctl_create_vcpu(struct kvm *kvm, int n) |
| 2306 | { |
| 2307 | int r; |
| 2308 | struct kvm_vcpu *vcpu; |
Avi Kivity | 9a2bb7f | 2007-02-22 12:58:31 +0200 | [diff] [blame] | 2309 | struct page *page; |
Avi Kivity | c5ea766 | 2007-02-20 18:41:05 +0200 | [diff] [blame] | 2310 | |
| 2311 | r = -EINVAL; |
| 2312 | if (!valid_vcpu(n)) |
| 2313 | goto out; |
| 2314 | |
| 2315 | vcpu = &kvm->vcpus[n]; |
| 2316 | |
| 2317 | mutex_lock(&vcpu->mutex); |
| 2318 | |
| 2319 | if (vcpu->vmcs) { |
| 2320 | mutex_unlock(&vcpu->mutex); |
| 2321 | return -EEXIST; |
| 2322 | } |
| 2323 | |
Avi Kivity | 9a2bb7f | 2007-02-22 12:58:31 +0200 | [diff] [blame] | 2324 | page = alloc_page(GFP_KERNEL | __GFP_ZERO); |
| 2325 | r = -ENOMEM; |
| 2326 | if (!page) |
| 2327 | goto out_unlock; |
| 2328 | vcpu->run = page_address(page); |
| 2329 | |
Avi Kivity | 039576c | 2007-03-20 12:46:50 +0200 | [diff] [blame] | 2330 | page = alloc_page(GFP_KERNEL | __GFP_ZERO); |
| 2331 | r = -ENOMEM; |
| 2332 | if (!page) |
| 2333 | goto out_free_run; |
| 2334 | vcpu->pio_data = page_address(page); |
| 2335 | |
Avi Kivity | c5ea766 | 2007-02-20 18:41:05 +0200 | [diff] [blame] | 2336 | vcpu->host_fx_image = (char*)ALIGN((hva_t)vcpu->fx_buf, |
| 2337 | FX_IMAGE_ALIGN); |
| 2338 | vcpu->guest_fx_image = vcpu->host_fx_image + FX_IMAGE_SIZE; |
Avi Kivity | d917a6b | 2007-04-12 13:03:01 +0300 | [diff] [blame] | 2339 | vcpu->cr0 = 0x10; |
Avi Kivity | c5ea766 | 2007-02-20 18:41:05 +0200 | [diff] [blame] | 2340 | |
| 2341 | r = kvm_arch_ops->vcpu_create(vcpu); |
| 2342 | if (r < 0) |
| 2343 | goto out_free_vcpus; |
| 2344 | |
| 2345 | r = kvm_mmu_create(vcpu); |
| 2346 | if (r < 0) |
| 2347 | goto out_free_vcpus; |
| 2348 | |
| 2349 | kvm_arch_ops->vcpu_load(vcpu); |
| 2350 | r = kvm_mmu_setup(vcpu); |
| 2351 | if (r >= 0) |
| 2352 | r = kvm_arch_ops->vcpu_setup(vcpu); |
| 2353 | vcpu_put(vcpu); |
| 2354 | |
| 2355 | if (r < 0) |
| 2356 | goto out_free_vcpus; |
| 2357 | |
Avi Kivity | bccf215 | 2007-02-21 18:04:26 +0200 | [diff] [blame] | 2358 | r = create_vcpu_fd(vcpu); |
| 2359 | if (r < 0) |
| 2360 | goto out_free_vcpus; |
| 2361 | |
| 2362 | return r; |
Avi Kivity | c5ea766 | 2007-02-20 18:41:05 +0200 | [diff] [blame] | 2363 | |
| 2364 | out_free_vcpus: |
| 2365 | kvm_free_vcpu(vcpu); |
Avi Kivity | 039576c | 2007-03-20 12:46:50 +0200 | [diff] [blame] | 2366 | out_free_run: |
| 2367 | free_page((unsigned long)vcpu->run); |
| 2368 | vcpu->run = NULL; |
Avi Kivity | 9a2bb7f | 2007-02-22 12:58:31 +0200 | [diff] [blame] | 2369 | out_unlock: |
Avi Kivity | c5ea766 | 2007-02-20 18:41:05 +0200 | [diff] [blame] | 2370 | mutex_unlock(&vcpu->mutex); |
| 2371 | out: |
| 2372 | return r; |
| 2373 | } |
| 2374 | |
Avi Kivity | 06465c5 | 2007-02-28 20:46:53 +0200 | [diff] [blame] | 2375 | static int kvm_vcpu_ioctl_set_cpuid(struct kvm_vcpu *vcpu, |
| 2376 | struct kvm_cpuid *cpuid, |
| 2377 | struct kvm_cpuid_entry __user *entries) |
| 2378 | { |
| 2379 | int r; |
| 2380 | |
| 2381 | r = -E2BIG; |
| 2382 | if (cpuid->nent > KVM_MAX_CPUID_ENTRIES) |
| 2383 | goto out; |
| 2384 | r = -EFAULT; |
| 2385 | if (copy_from_user(&vcpu->cpuid_entries, entries, |
| 2386 | cpuid->nent * sizeof(struct kvm_cpuid_entry))) |
| 2387 | goto out; |
| 2388 | vcpu->cpuid_nent = cpuid->nent; |
| 2389 | return 0; |
| 2390 | |
| 2391 | out: |
| 2392 | return r; |
| 2393 | } |
| 2394 | |
Avi Kivity | 1961d27 | 2007-03-05 19:46:05 +0200 | [diff] [blame] | 2395 | static int kvm_vcpu_ioctl_set_sigmask(struct kvm_vcpu *vcpu, sigset_t *sigset) |
| 2396 | { |
| 2397 | if (sigset) { |
| 2398 | sigdelsetmask(sigset, sigmask(SIGKILL)|sigmask(SIGSTOP)); |
| 2399 | vcpu->sigset_active = 1; |
| 2400 | vcpu->sigset = *sigset; |
| 2401 | } else |
| 2402 | vcpu->sigset_active = 0; |
| 2403 | return 0; |
| 2404 | } |
| 2405 | |
Avi Kivity | b883673 | 2007-04-01 16:34:31 +0300 | [diff] [blame] | 2406 | /* |
| 2407 | * fxsave fpu state. Taken from x86_64/processor.h. To be killed when |
| 2408 | * we have asm/x86/processor.h |
| 2409 | */ |
| 2410 | struct fxsave { |
| 2411 | u16 cwd; |
| 2412 | u16 swd; |
| 2413 | u16 twd; |
| 2414 | u16 fop; |
| 2415 | u64 rip; |
| 2416 | u64 rdp; |
| 2417 | u32 mxcsr; |
| 2418 | u32 mxcsr_mask; |
| 2419 | u32 st_space[32]; /* 8*16 bytes for each FP-reg = 128 bytes */ |
| 2420 | #ifdef CONFIG_X86_64 |
| 2421 | u32 xmm_space[64]; /* 16*16 bytes for each XMM-reg = 256 bytes */ |
| 2422 | #else |
| 2423 | u32 xmm_space[32]; /* 8*16 bytes for each XMM-reg = 128 bytes */ |
| 2424 | #endif |
| 2425 | }; |
| 2426 | |
| 2427 | static int kvm_vcpu_ioctl_get_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu) |
| 2428 | { |
| 2429 | struct fxsave *fxsave = (struct fxsave *)vcpu->guest_fx_image; |
| 2430 | |
| 2431 | vcpu_load(vcpu); |
| 2432 | |
| 2433 | memcpy(fpu->fpr, fxsave->st_space, 128); |
| 2434 | fpu->fcw = fxsave->cwd; |
| 2435 | fpu->fsw = fxsave->swd; |
| 2436 | fpu->ftwx = fxsave->twd; |
| 2437 | fpu->last_opcode = fxsave->fop; |
| 2438 | fpu->last_ip = fxsave->rip; |
| 2439 | fpu->last_dp = fxsave->rdp; |
| 2440 | memcpy(fpu->xmm, fxsave->xmm_space, sizeof fxsave->xmm_space); |
| 2441 | |
| 2442 | vcpu_put(vcpu); |
| 2443 | |
| 2444 | return 0; |
| 2445 | } |
| 2446 | |
| 2447 | static int kvm_vcpu_ioctl_set_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu) |
| 2448 | { |
| 2449 | struct fxsave *fxsave = (struct fxsave *)vcpu->guest_fx_image; |
| 2450 | |
| 2451 | vcpu_load(vcpu); |
| 2452 | |
| 2453 | memcpy(fxsave->st_space, fpu->fpr, 128); |
| 2454 | fxsave->cwd = fpu->fcw; |
| 2455 | fxsave->swd = fpu->fsw; |
| 2456 | fxsave->twd = fpu->ftwx; |
| 2457 | fxsave->fop = fpu->last_opcode; |
| 2458 | fxsave->rip = fpu->last_ip; |
| 2459 | fxsave->rdp = fpu->last_dp; |
| 2460 | memcpy(fxsave->xmm_space, fpu->xmm, sizeof fxsave->xmm_space); |
| 2461 | |
| 2462 | vcpu_put(vcpu); |
| 2463 | |
| 2464 | return 0; |
| 2465 | } |
| 2466 | |
Avi Kivity | bccf215 | 2007-02-21 18:04:26 +0200 | [diff] [blame] | 2467 | static long kvm_vcpu_ioctl(struct file *filp, |
| 2468 | unsigned int ioctl, unsigned long arg) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2469 | { |
Avi Kivity | bccf215 | 2007-02-21 18:04:26 +0200 | [diff] [blame] | 2470 | struct kvm_vcpu *vcpu = filp->private_data; |
Al Viro | 2f36698 | 2007-02-09 16:38:35 +0000 | [diff] [blame] | 2471 | void __user *argp = (void __user *)arg; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2472 | int r = -EINVAL; |
| 2473 | |
| 2474 | switch (ioctl) { |
Avi Kivity | 9a2bb7f | 2007-02-22 12:58:31 +0200 | [diff] [blame] | 2475 | case KVM_RUN: |
Avi Kivity | f0fe510 | 2007-03-07 13:11:17 +0200 | [diff] [blame] | 2476 | r = -EINVAL; |
| 2477 | if (arg) |
| 2478 | goto out; |
Avi Kivity | 9a2bb7f | 2007-02-22 12:58:31 +0200 | [diff] [blame] | 2479 | r = kvm_vcpu_ioctl_run(vcpu, vcpu->run); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2480 | break; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2481 | case KVM_GET_REGS: { |
| 2482 | struct kvm_regs kvm_regs; |
| 2483 | |
Avi Kivity | bccf215 | 2007-02-21 18:04:26 +0200 | [diff] [blame] | 2484 | memset(&kvm_regs, 0, sizeof kvm_regs); |
| 2485 | r = kvm_vcpu_ioctl_get_regs(vcpu, &kvm_regs); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2486 | if (r) |
| 2487 | goto out; |
| 2488 | r = -EFAULT; |
Al Viro | 2f36698 | 2007-02-09 16:38:35 +0000 | [diff] [blame] | 2489 | if (copy_to_user(argp, &kvm_regs, sizeof kvm_regs)) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2490 | goto out; |
| 2491 | r = 0; |
| 2492 | break; |
| 2493 | } |
| 2494 | case KVM_SET_REGS: { |
| 2495 | struct kvm_regs kvm_regs; |
| 2496 | |
| 2497 | r = -EFAULT; |
Al Viro | 2f36698 | 2007-02-09 16:38:35 +0000 | [diff] [blame] | 2498 | if (copy_from_user(&kvm_regs, argp, sizeof kvm_regs)) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2499 | goto out; |
Avi Kivity | bccf215 | 2007-02-21 18:04:26 +0200 | [diff] [blame] | 2500 | r = kvm_vcpu_ioctl_set_regs(vcpu, &kvm_regs); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2501 | if (r) |
| 2502 | goto out; |
| 2503 | r = 0; |
| 2504 | break; |
| 2505 | } |
| 2506 | case KVM_GET_SREGS: { |
| 2507 | struct kvm_sregs kvm_sregs; |
| 2508 | |
Avi Kivity | bccf215 | 2007-02-21 18:04:26 +0200 | [diff] [blame] | 2509 | memset(&kvm_sregs, 0, sizeof kvm_sregs); |
| 2510 | r = kvm_vcpu_ioctl_get_sregs(vcpu, &kvm_sregs); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2511 | if (r) |
| 2512 | goto out; |
| 2513 | r = -EFAULT; |
Al Viro | 2f36698 | 2007-02-09 16:38:35 +0000 | [diff] [blame] | 2514 | if (copy_to_user(argp, &kvm_sregs, sizeof kvm_sregs)) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2515 | goto out; |
| 2516 | r = 0; |
| 2517 | break; |
| 2518 | } |
| 2519 | case KVM_SET_SREGS: { |
| 2520 | struct kvm_sregs kvm_sregs; |
| 2521 | |
| 2522 | r = -EFAULT; |
Al Viro | 2f36698 | 2007-02-09 16:38:35 +0000 | [diff] [blame] | 2523 | if (copy_from_user(&kvm_sregs, argp, sizeof kvm_sregs)) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2524 | goto out; |
Avi Kivity | bccf215 | 2007-02-21 18:04:26 +0200 | [diff] [blame] | 2525 | r = kvm_vcpu_ioctl_set_sregs(vcpu, &kvm_sregs); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2526 | if (r) |
| 2527 | goto out; |
| 2528 | r = 0; |
| 2529 | break; |
| 2530 | } |
| 2531 | case KVM_TRANSLATE: { |
| 2532 | struct kvm_translation tr; |
| 2533 | |
| 2534 | r = -EFAULT; |
Al Viro | 2f36698 | 2007-02-09 16:38:35 +0000 | [diff] [blame] | 2535 | if (copy_from_user(&tr, argp, sizeof tr)) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2536 | goto out; |
Avi Kivity | bccf215 | 2007-02-21 18:04:26 +0200 | [diff] [blame] | 2537 | r = kvm_vcpu_ioctl_translate(vcpu, &tr); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2538 | if (r) |
| 2539 | goto out; |
| 2540 | r = -EFAULT; |
Al Viro | 2f36698 | 2007-02-09 16:38:35 +0000 | [diff] [blame] | 2541 | if (copy_to_user(argp, &tr, sizeof tr)) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2542 | goto out; |
| 2543 | r = 0; |
| 2544 | break; |
| 2545 | } |
| 2546 | case KVM_INTERRUPT: { |
| 2547 | struct kvm_interrupt irq; |
| 2548 | |
| 2549 | r = -EFAULT; |
Al Viro | 2f36698 | 2007-02-09 16:38:35 +0000 | [diff] [blame] | 2550 | if (copy_from_user(&irq, argp, sizeof irq)) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2551 | goto out; |
Avi Kivity | bccf215 | 2007-02-21 18:04:26 +0200 | [diff] [blame] | 2552 | r = kvm_vcpu_ioctl_interrupt(vcpu, &irq); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2553 | if (r) |
| 2554 | goto out; |
| 2555 | r = 0; |
| 2556 | break; |
| 2557 | } |
| 2558 | case KVM_DEBUG_GUEST: { |
| 2559 | struct kvm_debug_guest dbg; |
| 2560 | |
| 2561 | r = -EFAULT; |
Al Viro | 2f36698 | 2007-02-09 16:38:35 +0000 | [diff] [blame] | 2562 | if (copy_from_user(&dbg, argp, sizeof dbg)) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2563 | goto out; |
Avi Kivity | bccf215 | 2007-02-21 18:04:26 +0200 | [diff] [blame] | 2564 | r = kvm_vcpu_ioctl_debug_guest(vcpu, &dbg); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2565 | if (r) |
| 2566 | goto out; |
| 2567 | r = 0; |
| 2568 | break; |
| 2569 | } |
Avi Kivity | bccf215 | 2007-02-21 18:04:26 +0200 | [diff] [blame] | 2570 | case KVM_GET_MSRS: |
| 2571 | r = msr_io(vcpu, argp, get_msr, 1); |
| 2572 | break; |
| 2573 | case KVM_SET_MSRS: |
| 2574 | r = msr_io(vcpu, argp, do_set_msr, 0); |
| 2575 | break; |
Avi Kivity | 06465c5 | 2007-02-28 20:46:53 +0200 | [diff] [blame] | 2576 | case KVM_SET_CPUID: { |
| 2577 | struct kvm_cpuid __user *cpuid_arg = argp; |
| 2578 | struct kvm_cpuid cpuid; |
| 2579 | |
| 2580 | r = -EFAULT; |
| 2581 | if (copy_from_user(&cpuid, cpuid_arg, sizeof cpuid)) |
| 2582 | goto out; |
| 2583 | r = kvm_vcpu_ioctl_set_cpuid(vcpu, &cpuid, cpuid_arg->entries); |
| 2584 | if (r) |
| 2585 | goto out; |
| 2586 | break; |
| 2587 | } |
Avi Kivity | 1961d27 | 2007-03-05 19:46:05 +0200 | [diff] [blame] | 2588 | case KVM_SET_SIGNAL_MASK: { |
| 2589 | struct kvm_signal_mask __user *sigmask_arg = argp; |
| 2590 | struct kvm_signal_mask kvm_sigmask; |
| 2591 | sigset_t sigset, *p; |
| 2592 | |
| 2593 | p = NULL; |
| 2594 | if (argp) { |
| 2595 | r = -EFAULT; |
| 2596 | if (copy_from_user(&kvm_sigmask, argp, |
| 2597 | sizeof kvm_sigmask)) |
| 2598 | goto out; |
| 2599 | r = -EINVAL; |
| 2600 | if (kvm_sigmask.len != sizeof sigset) |
| 2601 | goto out; |
| 2602 | r = -EFAULT; |
| 2603 | if (copy_from_user(&sigset, sigmask_arg->sigset, |
| 2604 | sizeof sigset)) |
| 2605 | goto out; |
| 2606 | p = &sigset; |
| 2607 | } |
| 2608 | r = kvm_vcpu_ioctl_set_sigmask(vcpu, &sigset); |
| 2609 | break; |
| 2610 | } |
Avi Kivity | b883673 | 2007-04-01 16:34:31 +0300 | [diff] [blame] | 2611 | case KVM_GET_FPU: { |
| 2612 | struct kvm_fpu fpu; |
| 2613 | |
| 2614 | memset(&fpu, 0, sizeof fpu); |
| 2615 | r = kvm_vcpu_ioctl_get_fpu(vcpu, &fpu); |
| 2616 | if (r) |
| 2617 | goto out; |
| 2618 | r = -EFAULT; |
| 2619 | if (copy_to_user(argp, &fpu, sizeof fpu)) |
| 2620 | goto out; |
| 2621 | r = 0; |
| 2622 | break; |
| 2623 | } |
| 2624 | case KVM_SET_FPU: { |
| 2625 | struct kvm_fpu fpu; |
| 2626 | |
| 2627 | r = -EFAULT; |
| 2628 | if (copy_from_user(&fpu, argp, sizeof fpu)) |
| 2629 | goto out; |
| 2630 | r = kvm_vcpu_ioctl_set_fpu(vcpu, &fpu); |
| 2631 | if (r) |
| 2632 | goto out; |
| 2633 | r = 0; |
| 2634 | break; |
| 2635 | } |
Avi Kivity | bccf215 | 2007-02-21 18:04:26 +0200 | [diff] [blame] | 2636 | default: |
| 2637 | ; |
| 2638 | } |
| 2639 | out: |
| 2640 | return r; |
| 2641 | } |
| 2642 | |
| 2643 | static long kvm_vm_ioctl(struct file *filp, |
| 2644 | unsigned int ioctl, unsigned long arg) |
| 2645 | { |
| 2646 | struct kvm *kvm = filp->private_data; |
| 2647 | void __user *argp = (void __user *)arg; |
| 2648 | int r = -EINVAL; |
| 2649 | |
| 2650 | switch (ioctl) { |
| 2651 | case KVM_CREATE_VCPU: |
| 2652 | r = kvm_vm_ioctl_create_vcpu(kvm, arg); |
| 2653 | if (r < 0) |
| 2654 | goto out; |
| 2655 | break; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2656 | case KVM_SET_MEMORY_REGION: { |
| 2657 | struct kvm_memory_region kvm_mem; |
| 2658 | |
| 2659 | r = -EFAULT; |
Al Viro | 2f36698 | 2007-02-09 16:38:35 +0000 | [diff] [blame] | 2660 | if (copy_from_user(&kvm_mem, argp, sizeof kvm_mem)) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2661 | goto out; |
Avi Kivity | 2c6f5df | 2007-02-20 18:27:58 +0200 | [diff] [blame] | 2662 | r = kvm_vm_ioctl_set_memory_region(kvm, &kvm_mem); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2663 | if (r) |
| 2664 | goto out; |
| 2665 | break; |
| 2666 | } |
| 2667 | case KVM_GET_DIRTY_LOG: { |
| 2668 | struct kvm_dirty_log log; |
| 2669 | |
| 2670 | r = -EFAULT; |
Al Viro | 2f36698 | 2007-02-09 16:38:35 +0000 | [diff] [blame] | 2671 | if (copy_from_user(&log, argp, sizeof log)) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2672 | goto out; |
Avi Kivity | 2c6f5df | 2007-02-20 18:27:58 +0200 | [diff] [blame] | 2673 | r = kvm_vm_ioctl_get_dirty_log(kvm, &log); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2674 | if (r) |
| 2675 | goto out; |
| 2676 | break; |
| 2677 | } |
Avi Kivity | e820754 | 2007-03-30 16:54:30 +0300 | [diff] [blame] | 2678 | case KVM_SET_MEMORY_ALIAS: { |
| 2679 | struct kvm_memory_alias alias; |
| 2680 | |
| 2681 | r = -EFAULT; |
| 2682 | if (copy_from_user(&alias, argp, sizeof alias)) |
| 2683 | goto out; |
| 2684 | r = kvm_vm_ioctl_set_memory_alias(kvm, &alias); |
| 2685 | if (r) |
| 2686 | goto out; |
| 2687 | break; |
| 2688 | } |
Avi Kivity | f17abe9 | 2007-02-21 19:28:04 +0200 | [diff] [blame] | 2689 | default: |
| 2690 | ; |
| 2691 | } |
| 2692 | out: |
| 2693 | return r; |
| 2694 | } |
| 2695 | |
| 2696 | static struct page *kvm_vm_nopage(struct vm_area_struct *vma, |
| 2697 | unsigned long address, |
| 2698 | int *type) |
| 2699 | { |
| 2700 | struct kvm *kvm = vma->vm_file->private_data; |
| 2701 | unsigned long pgoff; |
Avi Kivity | f17abe9 | 2007-02-21 19:28:04 +0200 | [diff] [blame] | 2702 | struct page *page; |
| 2703 | |
| 2704 | *type = VM_FAULT_MINOR; |
| 2705 | pgoff = ((address - vma->vm_start) >> PAGE_SHIFT) + vma->vm_pgoff; |
Avi Kivity | 954bbbc2 | 2007-03-30 14:02:32 +0300 | [diff] [blame] | 2706 | page = gfn_to_page(kvm, pgoff); |
Avi Kivity | f17abe9 | 2007-02-21 19:28:04 +0200 | [diff] [blame] | 2707 | if (!page) |
| 2708 | return NOPAGE_SIGBUS; |
| 2709 | get_page(page); |
| 2710 | return page; |
| 2711 | } |
| 2712 | |
| 2713 | static struct vm_operations_struct kvm_vm_vm_ops = { |
| 2714 | .nopage = kvm_vm_nopage, |
| 2715 | }; |
| 2716 | |
| 2717 | static int kvm_vm_mmap(struct file *file, struct vm_area_struct *vma) |
| 2718 | { |
| 2719 | vma->vm_ops = &kvm_vm_vm_ops; |
| 2720 | return 0; |
| 2721 | } |
| 2722 | |
| 2723 | static struct file_operations kvm_vm_fops = { |
| 2724 | .release = kvm_vm_release, |
| 2725 | .unlocked_ioctl = kvm_vm_ioctl, |
| 2726 | .compat_ioctl = kvm_vm_ioctl, |
| 2727 | .mmap = kvm_vm_mmap, |
| 2728 | }; |
| 2729 | |
| 2730 | static int kvm_dev_ioctl_create_vm(void) |
| 2731 | { |
| 2732 | int fd, r; |
| 2733 | struct inode *inode; |
| 2734 | struct file *file; |
| 2735 | struct kvm *kvm; |
| 2736 | |
| 2737 | inode = kvmfs_inode(&kvm_vm_fops); |
| 2738 | if (IS_ERR(inode)) { |
| 2739 | r = PTR_ERR(inode); |
| 2740 | goto out1; |
| 2741 | } |
| 2742 | |
| 2743 | kvm = kvm_create_vm(); |
| 2744 | if (IS_ERR(kvm)) { |
| 2745 | r = PTR_ERR(kvm); |
| 2746 | goto out2; |
| 2747 | } |
| 2748 | |
| 2749 | file = kvmfs_file(inode, kvm); |
| 2750 | if (IS_ERR(file)) { |
| 2751 | r = PTR_ERR(file); |
| 2752 | goto out3; |
| 2753 | } |
Avi Kivity | bccf215 | 2007-02-21 18:04:26 +0200 | [diff] [blame] | 2754 | kvm->filp = file; |
Avi Kivity | f17abe9 | 2007-02-21 19:28:04 +0200 | [diff] [blame] | 2755 | |
| 2756 | r = get_unused_fd(); |
| 2757 | if (r < 0) |
| 2758 | goto out4; |
| 2759 | fd = r; |
| 2760 | fd_install(fd, file); |
| 2761 | |
| 2762 | return fd; |
| 2763 | |
| 2764 | out4: |
| 2765 | fput(file); |
| 2766 | out3: |
| 2767 | kvm_destroy_vm(kvm); |
| 2768 | out2: |
| 2769 | iput(inode); |
| 2770 | out1: |
| 2771 | return r; |
| 2772 | } |
| 2773 | |
| 2774 | static long kvm_dev_ioctl(struct file *filp, |
| 2775 | unsigned int ioctl, unsigned long arg) |
| 2776 | { |
| 2777 | void __user *argp = (void __user *)arg; |
Avi Kivity | 07c45a3 | 2007-03-07 13:05:38 +0200 | [diff] [blame] | 2778 | long r = -EINVAL; |
Avi Kivity | f17abe9 | 2007-02-21 19:28:04 +0200 | [diff] [blame] | 2779 | |
| 2780 | switch (ioctl) { |
| 2781 | case KVM_GET_API_VERSION: |
Avi Kivity | f0fe510 | 2007-03-07 13:11:17 +0200 | [diff] [blame] | 2782 | r = -EINVAL; |
| 2783 | if (arg) |
| 2784 | goto out; |
Avi Kivity | f17abe9 | 2007-02-21 19:28:04 +0200 | [diff] [blame] | 2785 | r = KVM_API_VERSION; |
| 2786 | break; |
| 2787 | case KVM_CREATE_VM: |
Avi Kivity | f0fe510 | 2007-03-07 13:11:17 +0200 | [diff] [blame] | 2788 | r = -EINVAL; |
| 2789 | if (arg) |
| 2790 | goto out; |
Avi Kivity | f17abe9 | 2007-02-21 19:28:04 +0200 | [diff] [blame] | 2791 | r = kvm_dev_ioctl_create_vm(); |
| 2792 | break; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2793 | case KVM_GET_MSR_INDEX_LIST: { |
Al Viro | 2f36698 | 2007-02-09 16:38:35 +0000 | [diff] [blame] | 2794 | struct kvm_msr_list __user *user_msr_list = argp; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2795 | struct kvm_msr_list msr_list; |
| 2796 | unsigned n; |
| 2797 | |
| 2798 | r = -EFAULT; |
| 2799 | if (copy_from_user(&msr_list, user_msr_list, sizeof msr_list)) |
| 2800 | goto out; |
| 2801 | n = msr_list.nmsrs; |
Avi Kivity | 6f00e68 | 2007-01-26 00:56:40 -0800 | [diff] [blame] | 2802 | msr_list.nmsrs = num_msrs_to_save + ARRAY_SIZE(emulated_msrs); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2803 | if (copy_to_user(user_msr_list, &msr_list, sizeof msr_list)) |
| 2804 | goto out; |
| 2805 | r = -E2BIG; |
Michael Riepe | bf591b2 | 2006-12-22 01:05:36 -0800 | [diff] [blame] | 2806 | if (n < num_msrs_to_save) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2807 | goto out; |
| 2808 | r = -EFAULT; |
| 2809 | if (copy_to_user(user_msr_list->indices, &msrs_to_save, |
Michael Riepe | bf591b2 | 2006-12-22 01:05:36 -0800 | [diff] [blame] | 2810 | num_msrs_to_save * sizeof(u32))) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2811 | goto out; |
Avi Kivity | 6f00e68 | 2007-01-26 00:56:40 -0800 | [diff] [blame] | 2812 | if (copy_to_user(user_msr_list->indices |
| 2813 | + num_msrs_to_save * sizeof(u32), |
| 2814 | &emulated_msrs, |
| 2815 | ARRAY_SIZE(emulated_msrs) * sizeof(u32))) |
| 2816 | goto out; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2817 | r = 0; |
Avi Kivity | cc1d895 | 2007-01-05 16:36:58 -0800 | [diff] [blame] | 2818 | break; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2819 | } |
Avi Kivity | 5d308f4 | 2007-03-01 17:56:20 +0200 | [diff] [blame] | 2820 | case KVM_CHECK_EXTENSION: |
| 2821 | /* |
| 2822 | * No extensions defined at present. |
| 2823 | */ |
| 2824 | r = 0; |
| 2825 | break; |
Avi Kivity | 07c45a3 | 2007-03-07 13:05:38 +0200 | [diff] [blame] | 2826 | case KVM_GET_VCPU_MMAP_SIZE: |
| 2827 | r = -EINVAL; |
| 2828 | if (arg) |
| 2829 | goto out; |
Avi Kivity | 039576c | 2007-03-20 12:46:50 +0200 | [diff] [blame] | 2830 | r = 2 * PAGE_SIZE; |
Avi Kivity | 07c45a3 | 2007-03-07 13:05:38 +0200 | [diff] [blame] | 2831 | break; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2832 | default: |
| 2833 | ; |
| 2834 | } |
| 2835 | out: |
| 2836 | return r; |
| 2837 | } |
| 2838 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2839 | static struct file_operations kvm_chardev_ops = { |
| 2840 | .open = kvm_dev_open, |
| 2841 | .release = kvm_dev_release, |
| 2842 | .unlocked_ioctl = kvm_dev_ioctl, |
| 2843 | .compat_ioctl = kvm_dev_ioctl, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2844 | }; |
| 2845 | |
| 2846 | static struct miscdevice kvm_dev = { |
Avi Kivity | bbe4432 | 2007-03-04 13:27:36 +0200 | [diff] [blame] | 2847 | KVM_MINOR, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2848 | "kvm", |
| 2849 | &kvm_chardev_ops, |
| 2850 | }; |
| 2851 | |
| 2852 | static int kvm_reboot(struct notifier_block *notifier, unsigned long val, |
| 2853 | void *v) |
| 2854 | { |
| 2855 | if (val == SYS_RESTART) { |
| 2856 | /* |
| 2857 | * Some (well, at least mine) BIOSes hang on reboot if |
| 2858 | * in vmx root mode. |
| 2859 | */ |
| 2860 | printk(KERN_INFO "kvm: exiting hardware virtualization\n"); |
Al Viro | 8b6d44c | 2007-02-09 16:38:40 +0000 | [diff] [blame] | 2861 | on_each_cpu(kvm_arch_ops->hardware_disable, NULL, 0, 1); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2862 | } |
| 2863 | return NOTIFY_OK; |
| 2864 | } |
| 2865 | |
| 2866 | static struct notifier_block kvm_reboot_notifier = { |
| 2867 | .notifier_call = kvm_reboot, |
| 2868 | .priority = 0, |
| 2869 | }; |
| 2870 | |
Avi Kivity | 774c47f | 2007-02-12 00:54:47 -0800 | [diff] [blame] | 2871 | /* |
| 2872 | * Make sure that a cpu that is being hot-unplugged does not have any vcpus |
| 2873 | * cached on it. |
| 2874 | */ |
| 2875 | static void decache_vcpus_on_cpu(int cpu) |
| 2876 | { |
| 2877 | struct kvm *vm; |
| 2878 | struct kvm_vcpu *vcpu; |
| 2879 | int i; |
| 2880 | |
| 2881 | spin_lock(&kvm_lock); |
| 2882 | list_for_each_entry(vm, &vm_list, vm_list) |
| 2883 | for (i = 0; i < KVM_MAX_VCPUS; ++i) { |
| 2884 | vcpu = &vm->vcpus[i]; |
| 2885 | /* |
| 2886 | * If the vcpu is locked, then it is running on some |
| 2887 | * other cpu and therefore it is not cached on the |
| 2888 | * cpu in question. |
| 2889 | * |
| 2890 | * If it's not locked, check the last cpu it executed |
| 2891 | * on. |
| 2892 | */ |
| 2893 | if (mutex_trylock(&vcpu->mutex)) { |
| 2894 | if (vcpu->cpu == cpu) { |
| 2895 | kvm_arch_ops->vcpu_decache(vcpu); |
| 2896 | vcpu->cpu = -1; |
| 2897 | } |
| 2898 | mutex_unlock(&vcpu->mutex); |
| 2899 | } |
| 2900 | } |
| 2901 | spin_unlock(&kvm_lock); |
| 2902 | } |
| 2903 | |
| 2904 | static int kvm_cpu_hotplug(struct notifier_block *notifier, unsigned long val, |
| 2905 | void *v) |
| 2906 | { |
| 2907 | int cpu = (long)v; |
| 2908 | |
| 2909 | switch (val) { |
Jeremy Katz | 43934a3 | 2007-02-19 14:37:46 +0200 | [diff] [blame] | 2910 | case CPU_DOWN_PREPARE: |
Avi Kivity | 774c47f | 2007-02-12 00:54:47 -0800 | [diff] [blame] | 2911 | case CPU_UP_CANCELED: |
Jeremy Katz | 43934a3 | 2007-02-19 14:37:46 +0200 | [diff] [blame] | 2912 | printk(KERN_INFO "kvm: disabling virtualization on CPU%d\n", |
| 2913 | cpu); |
Avi Kivity | 774c47f | 2007-02-12 00:54:47 -0800 | [diff] [blame] | 2914 | decache_vcpus_on_cpu(cpu); |
| 2915 | smp_call_function_single(cpu, kvm_arch_ops->hardware_disable, |
| 2916 | NULL, 0, 1); |
| 2917 | break; |
Jeremy Katz | 43934a3 | 2007-02-19 14:37:46 +0200 | [diff] [blame] | 2918 | case CPU_ONLINE: |
| 2919 | printk(KERN_INFO "kvm: enabling virtualization on CPU%d\n", |
| 2920 | cpu); |
Avi Kivity | 774c47f | 2007-02-12 00:54:47 -0800 | [diff] [blame] | 2921 | smp_call_function_single(cpu, kvm_arch_ops->hardware_enable, |
| 2922 | NULL, 0, 1); |
| 2923 | break; |
| 2924 | } |
| 2925 | return NOTIFY_OK; |
| 2926 | } |
| 2927 | |
| 2928 | static struct notifier_block kvm_cpu_notifier = { |
| 2929 | .notifier_call = kvm_cpu_hotplug, |
| 2930 | .priority = 20, /* must be > scheduler priority */ |
| 2931 | }; |
| 2932 | |
Avi Kivity | 1165f5f | 2007-04-19 17:27:43 +0300 | [diff] [blame^] | 2933 | static u64 stat_get(void *_offset) |
| 2934 | { |
| 2935 | unsigned offset = (long)_offset; |
| 2936 | u64 total = 0; |
| 2937 | struct kvm *kvm; |
| 2938 | struct kvm_vcpu *vcpu; |
| 2939 | int i; |
| 2940 | |
| 2941 | spin_lock(&kvm_lock); |
| 2942 | list_for_each_entry(kvm, &vm_list, vm_list) |
| 2943 | for (i = 0; i < KVM_MAX_VCPUS; ++i) { |
| 2944 | vcpu = &kvm->vcpus[i]; |
| 2945 | total += *(u32 *)((void *)vcpu + offset); |
| 2946 | } |
| 2947 | spin_unlock(&kvm_lock); |
| 2948 | return total; |
| 2949 | } |
| 2950 | |
| 2951 | static void stat_set(void *offset, u64 val) |
| 2952 | { |
| 2953 | } |
| 2954 | |
| 2955 | DEFINE_SIMPLE_ATTRIBUTE(stat_fops, stat_get, stat_set, "%llu\n"); |
| 2956 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2957 | static __init void kvm_init_debug(void) |
| 2958 | { |
| 2959 | struct kvm_stats_debugfs_item *p; |
| 2960 | |
Al Viro | 8b6d44c | 2007-02-09 16:38:40 +0000 | [diff] [blame] | 2961 | debugfs_dir = debugfs_create_dir("kvm", NULL); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2962 | for (p = debugfs_entries; p->name; ++p) |
Avi Kivity | 1165f5f | 2007-04-19 17:27:43 +0300 | [diff] [blame^] | 2963 | p->dentry = debugfs_create_file(p->name, 0444, debugfs_dir, |
| 2964 | (void *)(long)p->offset, |
| 2965 | &stat_fops); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2966 | } |
| 2967 | |
| 2968 | static void kvm_exit_debug(void) |
| 2969 | { |
| 2970 | struct kvm_stats_debugfs_item *p; |
| 2971 | |
| 2972 | for (p = debugfs_entries; p->name; ++p) |
| 2973 | debugfs_remove(p->dentry); |
| 2974 | debugfs_remove(debugfs_dir); |
| 2975 | } |
| 2976 | |
Avi Kivity | 59ae6c6 | 2007-02-12 00:54:48 -0800 | [diff] [blame] | 2977 | static int kvm_suspend(struct sys_device *dev, pm_message_t state) |
| 2978 | { |
| 2979 | decache_vcpus_on_cpu(raw_smp_processor_id()); |
Avi Kivity | 19d1408 | 2007-02-19 14:37:48 +0200 | [diff] [blame] | 2980 | on_each_cpu(kvm_arch_ops->hardware_disable, NULL, 0, 1); |
Avi Kivity | 59ae6c6 | 2007-02-12 00:54:48 -0800 | [diff] [blame] | 2981 | return 0; |
| 2982 | } |
| 2983 | |
| 2984 | static int kvm_resume(struct sys_device *dev) |
| 2985 | { |
Avi Kivity | 19d1408 | 2007-02-19 14:37:48 +0200 | [diff] [blame] | 2986 | on_each_cpu(kvm_arch_ops->hardware_enable, NULL, 0, 1); |
Avi Kivity | 59ae6c6 | 2007-02-12 00:54:48 -0800 | [diff] [blame] | 2987 | return 0; |
| 2988 | } |
| 2989 | |
| 2990 | static struct sysdev_class kvm_sysdev_class = { |
| 2991 | set_kset_name("kvm"), |
| 2992 | .suspend = kvm_suspend, |
| 2993 | .resume = kvm_resume, |
| 2994 | }; |
| 2995 | |
| 2996 | static struct sys_device kvm_sysdev = { |
| 2997 | .id = 0, |
| 2998 | .cls = &kvm_sysdev_class, |
| 2999 | }; |
| 3000 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3001 | hpa_t bad_page_address; |
| 3002 | |
Avi Kivity | 37e29d9 | 2007-02-20 14:07:37 +0200 | [diff] [blame] | 3003 | static int kvmfs_get_sb(struct file_system_type *fs_type, int flags, |
| 3004 | const char *dev_name, void *data, struct vfsmount *mnt) |
| 3005 | { |
Andrew Morton | e9cdb1e | 2007-03-01 11:28:13 +0200 | [diff] [blame] | 3006 | return get_sb_pseudo(fs_type, "kvm:", NULL, KVMFS_SUPER_MAGIC, mnt); |
Avi Kivity | 37e29d9 | 2007-02-20 14:07:37 +0200 | [diff] [blame] | 3007 | } |
| 3008 | |
| 3009 | static struct file_system_type kvm_fs_type = { |
| 3010 | .name = "kvmfs", |
| 3011 | .get_sb = kvmfs_get_sb, |
| 3012 | .kill_sb = kill_anon_super, |
| 3013 | }; |
| 3014 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3015 | int kvm_init_arch(struct kvm_arch_ops *ops, struct module *module) |
| 3016 | { |
| 3017 | int r; |
| 3018 | |
Yoshimi Ichiyanagi | 09db28b | 2006-12-29 16:49:41 -0800 | [diff] [blame] | 3019 | if (kvm_arch_ops) { |
| 3020 | printk(KERN_ERR "kvm: already loaded the other module\n"); |
| 3021 | return -EEXIST; |
| 3022 | } |
| 3023 | |
Yoshimi Ichiyanagi | e097f35 | 2007-01-05 16:36:24 -0800 | [diff] [blame] | 3024 | if (!ops->cpu_has_kvm_support()) { |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3025 | printk(KERN_ERR "kvm: no hardware support\n"); |
| 3026 | return -EOPNOTSUPP; |
| 3027 | } |
Yoshimi Ichiyanagi | e097f35 | 2007-01-05 16:36:24 -0800 | [diff] [blame] | 3028 | if (ops->disabled_by_bios()) { |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3029 | printk(KERN_ERR "kvm: disabled by bios\n"); |
| 3030 | return -EOPNOTSUPP; |
| 3031 | } |
| 3032 | |
Yoshimi Ichiyanagi | e097f35 | 2007-01-05 16:36:24 -0800 | [diff] [blame] | 3033 | kvm_arch_ops = ops; |
| 3034 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3035 | r = kvm_arch_ops->hardware_setup(); |
| 3036 | if (r < 0) |
Avi Kivity | ca45aaa | 2007-03-01 19:21:03 +0200 | [diff] [blame] | 3037 | goto out; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3038 | |
Al Viro | 8b6d44c | 2007-02-09 16:38:40 +0000 | [diff] [blame] | 3039 | on_each_cpu(kvm_arch_ops->hardware_enable, NULL, 0, 1); |
Avi Kivity | 774c47f | 2007-02-12 00:54:47 -0800 | [diff] [blame] | 3040 | r = register_cpu_notifier(&kvm_cpu_notifier); |
| 3041 | if (r) |
| 3042 | goto out_free_1; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3043 | register_reboot_notifier(&kvm_reboot_notifier); |
| 3044 | |
Avi Kivity | 59ae6c6 | 2007-02-12 00:54:48 -0800 | [diff] [blame] | 3045 | r = sysdev_class_register(&kvm_sysdev_class); |
| 3046 | if (r) |
| 3047 | goto out_free_2; |
| 3048 | |
| 3049 | r = sysdev_register(&kvm_sysdev); |
| 3050 | if (r) |
| 3051 | goto out_free_3; |
| 3052 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3053 | kvm_chardev_ops.owner = module; |
| 3054 | |
| 3055 | r = misc_register(&kvm_dev); |
| 3056 | if (r) { |
| 3057 | printk (KERN_ERR "kvm: misc device register failed\n"); |
| 3058 | goto out_free; |
| 3059 | } |
| 3060 | |
| 3061 | return r; |
| 3062 | |
| 3063 | out_free: |
Avi Kivity | 59ae6c6 | 2007-02-12 00:54:48 -0800 | [diff] [blame] | 3064 | sysdev_unregister(&kvm_sysdev); |
| 3065 | out_free_3: |
| 3066 | sysdev_class_unregister(&kvm_sysdev_class); |
| 3067 | out_free_2: |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3068 | unregister_reboot_notifier(&kvm_reboot_notifier); |
Avi Kivity | 774c47f | 2007-02-12 00:54:47 -0800 | [diff] [blame] | 3069 | unregister_cpu_notifier(&kvm_cpu_notifier); |
| 3070 | out_free_1: |
Al Viro | 8b6d44c | 2007-02-09 16:38:40 +0000 | [diff] [blame] | 3071 | on_each_cpu(kvm_arch_ops->hardware_disable, NULL, 0, 1); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3072 | kvm_arch_ops->hardware_unsetup(); |
Avi Kivity | ca45aaa | 2007-03-01 19:21:03 +0200 | [diff] [blame] | 3073 | out: |
| 3074 | kvm_arch_ops = NULL; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3075 | return r; |
| 3076 | } |
| 3077 | |
| 3078 | void kvm_exit_arch(void) |
| 3079 | { |
| 3080 | misc_deregister(&kvm_dev); |
Avi Kivity | 59ae6c6 | 2007-02-12 00:54:48 -0800 | [diff] [blame] | 3081 | sysdev_unregister(&kvm_sysdev); |
| 3082 | sysdev_class_unregister(&kvm_sysdev_class); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3083 | unregister_reboot_notifier(&kvm_reboot_notifier); |
Avi Kivity | 59ae6c6 | 2007-02-12 00:54:48 -0800 | [diff] [blame] | 3084 | unregister_cpu_notifier(&kvm_cpu_notifier); |
Al Viro | 8b6d44c | 2007-02-09 16:38:40 +0000 | [diff] [blame] | 3085 | on_each_cpu(kvm_arch_ops->hardware_disable, NULL, 0, 1); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3086 | kvm_arch_ops->hardware_unsetup(); |
Yoshimi Ichiyanagi | 09db28b | 2006-12-29 16:49:41 -0800 | [diff] [blame] | 3087 | kvm_arch_ops = NULL; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3088 | } |
| 3089 | |
| 3090 | static __init int kvm_init(void) |
| 3091 | { |
| 3092 | static struct page *bad_page; |
Avi Kivity | 37e29d9 | 2007-02-20 14:07:37 +0200 | [diff] [blame] | 3093 | int r; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3094 | |
Avi Kivity | b5a33a7 | 2007-04-15 16:31:09 +0300 | [diff] [blame] | 3095 | r = kvm_mmu_module_init(); |
| 3096 | if (r) |
| 3097 | goto out4; |
| 3098 | |
Avi Kivity | 37e29d9 | 2007-02-20 14:07:37 +0200 | [diff] [blame] | 3099 | r = register_filesystem(&kvm_fs_type); |
| 3100 | if (r) |
| 3101 | goto out3; |
| 3102 | |
| 3103 | kvmfs_mnt = kern_mount(&kvm_fs_type); |
| 3104 | r = PTR_ERR(kvmfs_mnt); |
| 3105 | if (IS_ERR(kvmfs_mnt)) |
| 3106 | goto out2; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3107 | kvm_init_debug(); |
| 3108 | |
Michael Riepe | bf591b2 | 2006-12-22 01:05:36 -0800 | [diff] [blame] | 3109 | kvm_init_msr_list(); |
| 3110 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3111 | if ((bad_page = alloc_page(GFP_KERNEL)) == NULL) { |
| 3112 | r = -ENOMEM; |
| 3113 | goto out; |
| 3114 | } |
| 3115 | |
| 3116 | bad_page_address = page_to_pfn(bad_page) << PAGE_SHIFT; |
| 3117 | memset(__va(bad_page_address), 0, PAGE_SIZE); |
| 3118 | |
Avi Kivity | 58e690e | 2007-02-26 16:29:43 +0200 | [diff] [blame] | 3119 | return 0; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3120 | |
| 3121 | out: |
| 3122 | kvm_exit_debug(); |
Avi Kivity | 37e29d9 | 2007-02-20 14:07:37 +0200 | [diff] [blame] | 3123 | mntput(kvmfs_mnt); |
| 3124 | out2: |
| 3125 | unregister_filesystem(&kvm_fs_type); |
| 3126 | out3: |
Avi Kivity | b5a33a7 | 2007-04-15 16:31:09 +0300 | [diff] [blame] | 3127 | kvm_mmu_module_exit(); |
| 3128 | out4: |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3129 | return r; |
| 3130 | } |
| 3131 | |
| 3132 | static __exit void kvm_exit(void) |
| 3133 | { |
| 3134 | kvm_exit_debug(); |
| 3135 | __free_page(pfn_to_page(bad_page_address >> PAGE_SHIFT)); |
Avi Kivity | 37e29d9 | 2007-02-20 14:07:37 +0200 | [diff] [blame] | 3136 | mntput(kvmfs_mnt); |
| 3137 | unregister_filesystem(&kvm_fs_type); |
Avi Kivity | b5a33a7 | 2007-04-15 16:31:09 +0300 | [diff] [blame] | 3138 | kvm_mmu_module_exit(); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3139 | } |
| 3140 | |
| 3141 | module_init(kvm_init) |
| 3142 | module_exit(kvm_exit) |
| 3143 | |
| 3144 | EXPORT_SYMBOL_GPL(kvm_init_arch); |
| 3145 | EXPORT_SYMBOL_GPL(kvm_exit_arch); |