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> |
| 23 | #include <asm/processor.h> |
| 24 | #include <linux/percpu.h> |
| 25 | #include <linux/gfp.h> |
| 26 | #include <asm/msr.h> |
| 27 | #include <linux/mm.h> |
| 28 | #include <linux/miscdevice.h> |
| 29 | #include <linux/vmalloc.h> |
| 30 | #include <asm/uaccess.h> |
| 31 | #include <linux/reboot.h> |
| 32 | #include <asm/io.h> |
| 33 | #include <linux/debugfs.h> |
| 34 | #include <linux/highmem.h> |
| 35 | #include <linux/file.h> |
| 36 | #include <asm/desc.h> |
| 37 | |
| 38 | #include "x86_emulate.h" |
| 39 | #include "segment_descriptor.h" |
| 40 | |
| 41 | MODULE_AUTHOR("Qumranet"); |
| 42 | MODULE_LICENSE("GPL"); |
| 43 | |
| 44 | struct kvm_arch_ops *kvm_arch_ops; |
| 45 | struct kvm_stat kvm_stat; |
| 46 | EXPORT_SYMBOL_GPL(kvm_stat); |
| 47 | |
| 48 | static struct kvm_stats_debugfs_item { |
| 49 | const char *name; |
| 50 | u32 *data; |
| 51 | struct dentry *dentry; |
| 52 | } debugfs_entries[] = { |
| 53 | { "pf_fixed", &kvm_stat.pf_fixed }, |
| 54 | { "pf_guest", &kvm_stat.pf_guest }, |
| 55 | { "tlb_flush", &kvm_stat.tlb_flush }, |
| 56 | { "invlpg", &kvm_stat.invlpg }, |
| 57 | { "exits", &kvm_stat.exits }, |
| 58 | { "io_exits", &kvm_stat.io_exits }, |
| 59 | { "mmio_exits", &kvm_stat.mmio_exits }, |
| 60 | { "signal_exits", &kvm_stat.signal_exits }, |
| 61 | { "irq_exits", &kvm_stat.irq_exits }, |
| 62 | { 0, 0 } |
| 63 | }; |
| 64 | |
| 65 | static struct dentry *debugfs_dir; |
| 66 | |
| 67 | #define MAX_IO_MSRS 256 |
| 68 | |
| 69 | #define CR0_RESEVED_BITS 0xffffffff1ffaffc0ULL |
| 70 | #define LMSW_GUEST_MASK 0x0eULL |
| 71 | #define CR4_RESEVED_BITS (~((1ULL << 11) - 1)) |
| 72 | #define CR8_RESEVED_BITS (~0x0fULL) |
| 73 | #define EFER_RESERVED_BITS 0xfffffffffffff2fe |
| 74 | |
Avi Kivity | 05b3e0c | 2006-12-13 00:33:45 -0800 | [diff] [blame] | 75 | #ifdef CONFIG_X86_64 |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 76 | // LDT or TSS descriptor in the GDT. 16 bytes. |
| 77 | struct segment_descriptor_64 { |
| 78 | struct segment_descriptor s; |
| 79 | u32 base_higher; |
| 80 | u32 pad_zero; |
| 81 | }; |
| 82 | |
| 83 | #endif |
| 84 | |
| 85 | unsigned long segment_base(u16 selector) |
| 86 | { |
| 87 | struct descriptor_table gdt; |
| 88 | struct segment_descriptor *d; |
| 89 | unsigned long table_base; |
| 90 | typedef unsigned long ul; |
| 91 | unsigned long v; |
| 92 | |
| 93 | if (selector == 0) |
| 94 | return 0; |
| 95 | |
| 96 | asm ("sgdt %0" : "=m"(gdt)); |
| 97 | table_base = gdt.base; |
| 98 | |
| 99 | if (selector & 4) { /* from ldt */ |
| 100 | u16 ldt_selector; |
| 101 | |
| 102 | asm ("sldt %0" : "=g"(ldt_selector)); |
| 103 | table_base = segment_base(ldt_selector); |
| 104 | } |
| 105 | d = (struct segment_descriptor *)(table_base + (selector & ~7)); |
| 106 | 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] | 107 | #ifdef CONFIG_X86_64 |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 108 | if (d->system == 0 |
| 109 | && (d->type == 2 || d->type == 9 || d->type == 11)) |
| 110 | v |= ((ul)((struct segment_descriptor_64 *)d)->base_higher) << 32; |
| 111 | #endif |
| 112 | return v; |
| 113 | } |
| 114 | EXPORT_SYMBOL_GPL(segment_base); |
| 115 | |
| 116 | int kvm_read_guest(struct kvm_vcpu *vcpu, |
| 117 | gva_t addr, |
| 118 | unsigned long size, |
| 119 | void *dest) |
| 120 | { |
| 121 | unsigned char *host_buf = dest; |
| 122 | unsigned long req_size = size; |
| 123 | |
| 124 | while (size) { |
| 125 | hpa_t paddr; |
| 126 | unsigned now; |
| 127 | unsigned offset; |
| 128 | hva_t guest_buf; |
| 129 | |
| 130 | paddr = gva_to_hpa(vcpu, addr); |
| 131 | |
| 132 | if (is_error_hpa(paddr)) |
| 133 | break; |
| 134 | |
| 135 | guest_buf = (hva_t)kmap_atomic( |
| 136 | pfn_to_page(paddr >> PAGE_SHIFT), |
| 137 | KM_USER0); |
| 138 | offset = addr & ~PAGE_MASK; |
| 139 | guest_buf |= offset; |
| 140 | now = min(size, PAGE_SIZE - offset); |
| 141 | memcpy(host_buf, (void*)guest_buf, now); |
| 142 | host_buf += now; |
| 143 | addr += now; |
| 144 | size -= now; |
| 145 | kunmap_atomic((void *)(guest_buf & PAGE_MASK), KM_USER0); |
| 146 | } |
| 147 | return req_size - size; |
| 148 | } |
| 149 | EXPORT_SYMBOL_GPL(kvm_read_guest); |
| 150 | |
| 151 | int kvm_write_guest(struct kvm_vcpu *vcpu, |
| 152 | gva_t addr, |
| 153 | unsigned long size, |
| 154 | void *data) |
| 155 | { |
| 156 | unsigned char *host_buf = data; |
| 157 | unsigned long req_size = size; |
| 158 | |
| 159 | while (size) { |
| 160 | hpa_t paddr; |
| 161 | unsigned now; |
| 162 | unsigned offset; |
| 163 | hva_t guest_buf; |
| 164 | |
| 165 | paddr = gva_to_hpa(vcpu, addr); |
| 166 | |
| 167 | if (is_error_hpa(paddr)) |
| 168 | break; |
| 169 | |
| 170 | guest_buf = (hva_t)kmap_atomic( |
| 171 | pfn_to_page(paddr >> PAGE_SHIFT), KM_USER0); |
| 172 | offset = addr & ~PAGE_MASK; |
| 173 | guest_buf |= offset; |
| 174 | now = min(size, PAGE_SIZE - offset); |
| 175 | memcpy((void*)guest_buf, host_buf, now); |
| 176 | host_buf += now; |
| 177 | addr += now; |
| 178 | size -= now; |
| 179 | kunmap_atomic((void *)(guest_buf & PAGE_MASK), KM_USER0); |
| 180 | } |
| 181 | return req_size - size; |
| 182 | } |
| 183 | EXPORT_SYMBOL_GPL(kvm_write_guest); |
| 184 | |
| 185 | static int vcpu_slot(struct kvm_vcpu *vcpu) |
| 186 | { |
| 187 | return vcpu - vcpu->kvm->vcpus; |
| 188 | } |
| 189 | |
| 190 | /* |
| 191 | * Switches to specified vcpu, until a matching vcpu_put() |
| 192 | */ |
| 193 | static struct kvm_vcpu *vcpu_load(struct kvm *kvm, int vcpu_slot) |
| 194 | { |
| 195 | struct kvm_vcpu *vcpu = &kvm->vcpus[vcpu_slot]; |
| 196 | |
| 197 | mutex_lock(&vcpu->mutex); |
| 198 | if (unlikely(!vcpu->vmcs)) { |
| 199 | mutex_unlock(&vcpu->mutex); |
| 200 | return 0; |
| 201 | } |
| 202 | return kvm_arch_ops->vcpu_load(vcpu); |
| 203 | } |
| 204 | |
| 205 | static void vcpu_put(struct kvm_vcpu *vcpu) |
| 206 | { |
| 207 | kvm_arch_ops->vcpu_put(vcpu); |
| 208 | put_cpu(); |
| 209 | mutex_unlock(&vcpu->mutex); |
| 210 | } |
| 211 | |
| 212 | static int kvm_dev_open(struct inode *inode, struct file *filp) |
| 213 | { |
| 214 | struct kvm *kvm = kzalloc(sizeof(struct kvm), GFP_KERNEL); |
| 215 | int i; |
| 216 | |
| 217 | if (!kvm) |
| 218 | return -ENOMEM; |
| 219 | |
| 220 | spin_lock_init(&kvm->lock); |
| 221 | INIT_LIST_HEAD(&kvm->active_mmu_pages); |
| 222 | for (i = 0; i < KVM_MAX_VCPUS; ++i) { |
| 223 | struct kvm_vcpu *vcpu = &kvm->vcpus[i]; |
| 224 | |
| 225 | mutex_init(&vcpu->mutex); |
| 226 | vcpu->mmu.root_hpa = INVALID_PAGE; |
| 227 | INIT_LIST_HEAD(&vcpu->free_pages); |
| 228 | } |
| 229 | filp->private_data = kvm; |
| 230 | return 0; |
| 231 | } |
| 232 | |
| 233 | /* |
| 234 | * Free any memory in @free but not in @dont. |
| 235 | */ |
| 236 | static void kvm_free_physmem_slot(struct kvm_memory_slot *free, |
| 237 | struct kvm_memory_slot *dont) |
| 238 | { |
| 239 | int i; |
| 240 | |
| 241 | if (!dont || free->phys_mem != dont->phys_mem) |
| 242 | if (free->phys_mem) { |
| 243 | for (i = 0; i < free->npages; ++i) |
| 244 | __free_page(free->phys_mem[i]); |
| 245 | vfree(free->phys_mem); |
| 246 | } |
| 247 | |
| 248 | if (!dont || free->dirty_bitmap != dont->dirty_bitmap) |
| 249 | vfree(free->dirty_bitmap); |
| 250 | |
| 251 | free->phys_mem = 0; |
| 252 | free->npages = 0; |
| 253 | free->dirty_bitmap = 0; |
| 254 | } |
| 255 | |
| 256 | static void kvm_free_physmem(struct kvm *kvm) |
| 257 | { |
| 258 | int i; |
| 259 | |
| 260 | for (i = 0; i < kvm->nmemslots; ++i) |
| 261 | kvm_free_physmem_slot(&kvm->memslots[i], 0); |
| 262 | } |
| 263 | |
| 264 | static void kvm_free_vcpu(struct kvm_vcpu *vcpu) |
| 265 | { |
| 266 | kvm_arch_ops->vcpu_free(vcpu); |
| 267 | kvm_mmu_destroy(vcpu); |
| 268 | } |
| 269 | |
| 270 | static void kvm_free_vcpus(struct kvm *kvm) |
| 271 | { |
| 272 | unsigned int i; |
| 273 | |
| 274 | for (i = 0; i < KVM_MAX_VCPUS; ++i) |
| 275 | kvm_free_vcpu(&kvm->vcpus[i]); |
| 276 | } |
| 277 | |
| 278 | static int kvm_dev_release(struct inode *inode, struct file *filp) |
| 279 | { |
| 280 | struct kvm *kvm = filp->private_data; |
| 281 | |
| 282 | kvm_free_vcpus(kvm); |
| 283 | kvm_free_physmem(kvm); |
| 284 | kfree(kvm); |
| 285 | return 0; |
| 286 | } |
| 287 | |
| 288 | static void inject_gp(struct kvm_vcpu *vcpu) |
| 289 | { |
| 290 | kvm_arch_ops->inject_gp(vcpu, 0); |
| 291 | } |
| 292 | |
| 293 | static int pdptrs_have_reserved_bits_set(struct kvm_vcpu *vcpu, |
| 294 | unsigned long cr3) |
| 295 | { |
| 296 | gfn_t pdpt_gfn = cr3 >> PAGE_SHIFT; |
| 297 | unsigned offset = (cr3 & (PAGE_SIZE-1)) >> 5; |
| 298 | int i; |
| 299 | u64 pdpte; |
| 300 | u64 *pdpt; |
| 301 | struct kvm_memory_slot *memslot; |
| 302 | |
| 303 | spin_lock(&vcpu->kvm->lock); |
| 304 | memslot = gfn_to_memslot(vcpu->kvm, pdpt_gfn); |
| 305 | /* FIXME: !memslot - emulate? 0xff? */ |
| 306 | pdpt = kmap_atomic(gfn_to_page(memslot, pdpt_gfn), KM_USER0); |
| 307 | |
| 308 | for (i = 0; i < 4; ++i) { |
| 309 | pdpte = pdpt[offset + i]; |
| 310 | if ((pdpte & 1) && (pdpte & 0xfffffff0000001e6ull)) |
| 311 | break; |
| 312 | } |
| 313 | |
| 314 | kunmap_atomic(pdpt, KM_USER0); |
| 315 | spin_unlock(&vcpu->kvm->lock); |
| 316 | |
| 317 | return i != 4; |
| 318 | } |
| 319 | |
| 320 | void set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0) |
| 321 | { |
| 322 | if (cr0 & CR0_RESEVED_BITS) { |
| 323 | printk(KERN_DEBUG "set_cr0: 0x%lx #GP, reserved bits 0x%lx\n", |
| 324 | cr0, vcpu->cr0); |
| 325 | inject_gp(vcpu); |
| 326 | return; |
| 327 | } |
| 328 | |
| 329 | if ((cr0 & CR0_NW_MASK) && !(cr0 & CR0_CD_MASK)) { |
| 330 | printk(KERN_DEBUG "set_cr0: #GP, CD == 0 && NW == 1\n"); |
| 331 | inject_gp(vcpu); |
| 332 | return; |
| 333 | } |
| 334 | |
| 335 | if ((cr0 & CR0_PG_MASK) && !(cr0 & CR0_PE_MASK)) { |
| 336 | printk(KERN_DEBUG "set_cr0: #GP, set PG flag " |
| 337 | "and a clear PE flag\n"); |
| 338 | inject_gp(vcpu); |
| 339 | return; |
| 340 | } |
| 341 | |
| 342 | if (!is_paging(vcpu) && (cr0 & CR0_PG_MASK)) { |
Avi Kivity | 05b3e0c | 2006-12-13 00:33:45 -0800 | [diff] [blame] | 343 | #ifdef CONFIG_X86_64 |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 344 | if ((vcpu->shadow_efer & EFER_LME)) { |
| 345 | int cs_db, cs_l; |
| 346 | |
| 347 | if (!is_pae(vcpu)) { |
| 348 | printk(KERN_DEBUG "set_cr0: #GP, start paging " |
| 349 | "in long mode while PAE is disabled\n"); |
| 350 | inject_gp(vcpu); |
| 351 | return; |
| 352 | } |
| 353 | kvm_arch_ops->get_cs_db_l_bits(vcpu, &cs_db, &cs_l); |
| 354 | if (cs_l) { |
| 355 | printk(KERN_DEBUG "set_cr0: #GP, start paging " |
| 356 | "in long mode while CS.L == 1\n"); |
| 357 | inject_gp(vcpu); |
| 358 | return; |
| 359 | |
| 360 | } |
| 361 | } else |
| 362 | #endif |
| 363 | if (is_pae(vcpu) && |
| 364 | pdptrs_have_reserved_bits_set(vcpu, vcpu->cr3)) { |
| 365 | printk(KERN_DEBUG "set_cr0: #GP, pdptrs " |
| 366 | "reserved bits\n"); |
| 367 | inject_gp(vcpu); |
| 368 | return; |
| 369 | } |
| 370 | |
| 371 | } |
| 372 | |
| 373 | kvm_arch_ops->set_cr0(vcpu, cr0); |
| 374 | vcpu->cr0 = cr0; |
| 375 | |
| 376 | spin_lock(&vcpu->kvm->lock); |
| 377 | kvm_mmu_reset_context(vcpu); |
| 378 | spin_unlock(&vcpu->kvm->lock); |
| 379 | return; |
| 380 | } |
| 381 | EXPORT_SYMBOL_GPL(set_cr0); |
| 382 | |
| 383 | void lmsw(struct kvm_vcpu *vcpu, unsigned long msw) |
| 384 | { |
| 385 | set_cr0(vcpu, (vcpu->cr0 & ~0x0ful) | (msw & 0x0f)); |
| 386 | } |
| 387 | EXPORT_SYMBOL_GPL(lmsw); |
| 388 | |
| 389 | void set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4) |
| 390 | { |
| 391 | if (cr4 & CR4_RESEVED_BITS) { |
| 392 | printk(KERN_DEBUG "set_cr4: #GP, reserved bits\n"); |
| 393 | inject_gp(vcpu); |
| 394 | return; |
| 395 | } |
| 396 | |
| 397 | if (kvm_arch_ops->is_long_mode(vcpu)) { |
| 398 | if (!(cr4 & CR4_PAE_MASK)) { |
| 399 | printk(KERN_DEBUG "set_cr4: #GP, clearing PAE while " |
| 400 | "in long mode\n"); |
| 401 | inject_gp(vcpu); |
| 402 | return; |
| 403 | } |
| 404 | } else if (is_paging(vcpu) && !is_pae(vcpu) && (cr4 & CR4_PAE_MASK) |
| 405 | && pdptrs_have_reserved_bits_set(vcpu, vcpu->cr3)) { |
| 406 | printk(KERN_DEBUG "set_cr4: #GP, pdptrs reserved bits\n"); |
| 407 | inject_gp(vcpu); |
| 408 | } |
| 409 | |
| 410 | if (cr4 & CR4_VMXE_MASK) { |
| 411 | printk(KERN_DEBUG "set_cr4: #GP, setting VMXE\n"); |
| 412 | inject_gp(vcpu); |
| 413 | return; |
| 414 | } |
| 415 | kvm_arch_ops->set_cr4(vcpu, cr4); |
| 416 | spin_lock(&vcpu->kvm->lock); |
| 417 | kvm_mmu_reset_context(vcpu); |
| 418 | spin_unlock(&vcpu->kvm->lock); |
| 419 | } |
| 420 | EXPORT_SYMBOL_GPL(set_cr4); |
| 421 | |
| 422 | void set_cr3(struct kvm_vcpu *vcpu, unsigned long cr3) |
| 423 | { |
| 424 | if (kvm_arch_ops->is_long_mode(vcpu)) { |
| 425 | if ( cr3 & CR3_L_MODE_RESEVED_BITS) { |
| 426 | printk(KERN_DEBUG "set_cr3: #GP, reserved bits\n"); |
| 427 | inject_gp(vcpu); |
| 428 | return; |
| 429 | } |
| 430 | } else { |
| 431 | if (cr3 & CR3_RESEVED_BITS) { |
| 432 | printk(KERN_DEBUG "set_cr3: #GP, reserved bits\n"); |
| 433 | inject_gp(vcpu); |
| 434 | return; |
| 435 | } |
| 436 | if (is_paging(vcpu) && is_pae(vcpu) && |
| 437 | pdptrs_have_reserved_bits_set(vcpu, cr3)) { |
| 438 | printk(KERN_DEBUG "set_cr3: #GP, pdptrs " |
| 439 | "reserved bits\n"); |
| 440 | inject_gp(vcpu); |
| 441 | return; |
| 442 | } |
| 443 | } |
| 444 | |
| 445 | vcpu->cr3 = cr3; |
| 446 | spin_lock(&vcpu->kvm->lock); |
| 447 | vcpu->mmu.new_cr3(vcpu); |
| 448 | spin_unlock(&vcpu->kvm->lock); |
| 449 | } |
| 450 | EXPORT_SYMBOL_GPL(set_cr3); |
| 451 | |
| 452 | void set_cr8(struct kvm_vcpu *vcpu, unsigned long cr8) |
| 453 | { |
| 454 | if ( cr8 & CR8_RESEVED_BITS) { |
| 455 | printk(KERN_DEBUG "set_cr8: #GP, reserved bits 0x%lx\n", cr8); |
| 456 | inject_gp(vcpu); |
| 457 | return; |
| 458 | } |
| 459 | vcpu->cr8 = cr8; |
| 460 | } |
| 461 | EXPORT_SYMBOL_GPL(set_cr8); |
| 462 | |
| 463 | void fx_init(struct kvm_vcpu *vcpu) |
| 464 | { |
| 465 | struct __attribute__ ((__packed__)) fx_image_s { |
| 466 | u16 control; //fcw |
| 467 | u16 status; //fsw |
| 468 | u16 tag; // ftw |
| 469 | u16 opcode; //fop |
| 470 | u64 ip; // fpu ip |
| 471 | u64 operand;// fpu dp |
| 472 | u32 mxcsr; |
| 473 | u32 mxcsr_mask; |
| 474 | |
| 475 | } *fx_image; |
| 476 | |
| 477 | fx_save(vcpu->host_fx_image); |
| 478 | fpu_init(); |
| 479 | fx_save(vcpu->guest_fx_image); |
| 480 | fx_restore(vcpu->host_fx_image); |
| 481 | |
| 482 | fx_image = (struct fx_image_s *)vcpu->guest_fx_image; |
| 483 | fx_image->mxcsr = 0x1f80; |
| 484 | memset(vcpu->guest_fx_image + sizeof(struct fx_image_s), |
| 485 | 0, FX_IMAGE_SIZE - sizeof(struct fx_image_s)); |
| 486 | } |
| 487 | EXPORT_SYMBOL_GPL(fx_init); |
| 488 | |
| 489 | /* |
| 490 | * Creates some virtual cpus. Good luck creating more than one. |
| 491 | */ |
| 492 | static int kvm_dev_ioctl_create_vcpu(struct kvm *kvm, int n) |
| 493 | { |
| 494 | int r; |
| 495 | struct kvm_vcpu *vcpu; |
| 496 | |
| 497 | r = -EINVAL; |
| 498 | if (n < 0 || n >= KVM_MAX_VCPUS) |
| 499 | goto out; |
| 500 | |
| 501 | vcpu = &kvm->vcpus[n]; |
| 502 | |
| 503 | mutex_lock(&vcpu->mutex); |
| 504 | |
| 505 | if (vcpu->vmcs) { |
| 506 | mutex_unlock(&vcpu->mutex); |
| 507 | return -EEXIST; |
| 508 | } |
| 509 | |
| 510 | vcpu->host_fx_image = (char*)ALIGN((hva_t)vcpu->fx_buf, |
| 511 | FX_IMAGE_ALIGN); |
| 512 | vcpu->guest_fx_image = vcpu->host_fx_image + FX_IMAGE_SIZE; |
| 513 | |
| 514 | vcpu->cpu = -1; /* First load will set up TR */ |
| 515 | vcpu->kvm = kvm; |
| 516 | r = kvm_arch_ops->vcpu_create(vcpu); |
| 517 | if (r < 0) |
| 518 | goto out_free_vcpus; |
| 519 | |
| 520 | kvm_arch_ops->vcpu_load(vcpu); |
| 521 | |
| 522 | r = kvm_arch_ops->vcpu_setup(vcpu); |
| 523 | if (r >= 0) |
| 524 | r = kvm_mmu_init(vcpu); |
| 525 | |
| 526 | vcpu_put(vcpu); |
| 527 | |
| 528 | if (r < 0) |
| 529 | goto out_free_vcpus; |
| 530 | |
| 531 | return 0; |
| 532 | |
| 533 | out_free_vcpus: |
| 534 | kvm_free_vcpu(vcpu); |
| 535 | mutex_unlock(&vcpu->mutex); |
| 536 | out: |
| 537 | return r; |
| 538 | } |
| 539 | |
| 540 | /* |
| 541 | * Allocate some memory and give it an address in the guest physical address |
| 542 | * space. |
| 543 | * |
| 544 | * Discontiguous memory is allowed, mostly for framebuffers. |
| 545 | */ |
| 546 | static int kvm_dev_ioctl_set_memory_region(struct kvm *kvm, |
| 547 | struct kvm_memory_region *mem) |
| 548 | { |
| 549 | int r; |
| 550 | gfn_t base_gfn; |
| 551 | unsigned long npages; |
| 552 | unsigned long i; |
| 553 | struct kvm_memory_slot *memslot; |
| 554 | struct kvm_memory_slot old, new; |
| 555 | int memory_config_version; |
| 556 | |
| 557 | r = -EINVAL; |
| 558 | /* General sanity checks */ |
| 559 | if (mem->memory_size & (PAGE_SIZE - 1)) |
| 560 | goto out; |
| 561 | if (mem->guest_phys_addr & (PAGE_SIZE - 1)) |
| 562 | goto out; |
| 563 | if (mem->slot >= KVM_MEMORY_SLOTS) |
| 564 | goto out; |
| 565 | if (mem->guest_phys_addr + mem->memory_size < mem->guest_phys_addr) |
| 566 | goto out; |
| 567 | |
| 568 | memslot = &kvm->memslots[mem->slot]; |
| 569 | base_gfn = mem->guest_phys_addr >> PAGE_SHIFT; |
| 570 | npages = mem->memory_size >> PAGE_SHIFT; |
| 571 | |
| 572 | if (!npages) |
| 573 | mem->flags &= ~KVM_MEM_LOG_DIRTY_PAGES; |
| 574 | |
| 575 | raced: |
| 576 | spin_lock(&kvm->lock); |
| 577 | |
| 578 | memory_config_version = kvm->memory_config_version; |
| 579 | new = old = *memslot; |
| 580 | |
| 581 | new.base_gfn = base_gfn; |
| 582 | new.npages = npages; |
| 583 | new.flags = mem->flags; |
| 584 | |
| 585 | /* Disallow changing a memory slot's size. */ |
| 586 | r = -EINVAL; |
| 587 | if (npages && old.npages && npages != old.npages) |
| 588 | goto out_unlock; |
| 589 | |
| 590 | /* Check for overlaps */ |
| 591 | r = -EEXIST; |
| 592 | for (i = 0; i < KVM_MEMORY_SLOTS; ++i) { |
| 593 | struct kvm_memory_slot *s = &kvm->memslots[i]; |
| 594 | |
| 595 | if (s == memslot) |
| 596 | continue; |
| 597 | if (!((base_gfn + npages <= s->base_gfn) || |
| 598 | (base_gfn >= s->base_gfn + s->npages))) |
| 599 | goto out_unlock; |
| 600 | } |
| 601 | /* |
| 602 | * Do memory allocations outside lock. memory_config_version will |
| 603 | * detect any races. |
| 604 | */ |
| 605 | spin_unlock(&kvm->lock); |
| 606 | |
| 607 | /* Deallocate if slot is being removed */ |
| 608 | if (!npages) |
| 609 | new.phys_mem = 0; |
| 610 | |
| 611 | /* Free page dirty bitmap if unneeded */ |
| 612 | if (!(new.flags & KVM_MEM_LOG_DIRTY_PAGES)) |
| 613 | new.dirty_bitmap = 0; |
| 614 | |
| 615 | r = -ENOMEM; |
| 616 | |
| 617 | /* Allocate if a slot is being created */ |
| 618 | if (npages && !new.phys_mem) { |
| 619 | new.phys_mem = vmalloc(npages * sizeof(struct page *)); |
| 620 | |
| 621 | if (!new.phys_mem) |
| 622 | goto out_free; |
| 623 | |
| 624 | memset(new.phys_mem, 0, npages * sizeof(struct page *)); |
| 625 | for (i = 0; i < npages; ++i) { |
| 626 | new.phys_mem[i] = alloc_page(GFP_HIGHUSER |
| 627 | | __GFP_ZERO); |
| 628 | if (!new.phys_mem[i]) |
| 629 | goto out_free; |
| 630 | } |
| 631 | } |
| 632 | |
| 633 | /* Allocate page dirty bitmap if needed */ |
| 634 | if ((new.flags & KVM_MEM_LOG_DIRTY_PAGES) && !new.dirty_bitmap) { |
| 635 | unsigned dirty_bytes = ALIGN(npages, BITS_PER_LONG) / 8; |
| 636 | |
| 637 | new.dirty_bitmap = vmalloc(dirty_bytes); |
| 638 | if (!new.dirty_bitmap) |
| 639 | goto out_free; |
| 640 | memset(new.dirty_bitmap, 0, dirty_bytes); |
| 641 | } |
| 642 | |
| 643 | spin_lock(&kvm->lock); |
| 644 | |
| 645 | if (memory_config_version != kvm->memory_config_version) { |
| 646 | spin_unlock(&kvm->lock); |
| 647 | kvm_free_physmem_slot(&new, &old); |
| 648 | goto raced; |
| 649 | } |
| 650 | |
| 651 | r = -EAGAIN; |
| 652 | if (kvm->busy) |
| 653 | goto out_unlock; |
| 654 | |
| 655 | if (mem->slot >= kvm->nmemslots) |
| 656 | kvm->nmemslots = mem->slot + 1; |
| 657 | |
| 658 | *memslot = new; |
| 659 | ++kvm->memory_config_version; |
| 660 | |
| 661 | spin_unlock(&kvm->lock); |
| 662 | |
| 663 | for (i = 0; i < KVM_MAX_VCPUS; ++i) { |
| 664 | struct kvm_vcpu *vcpu; |
| 665 | |
| 666 | vcpu = vcpu_load(kvm, i); |
| 667 | if (!vcpu) |
| 668 | continue; |
| 669 | kvm_mmu_reset_context(vcpu); |
| 670 | vcpu_put(vcpu); |
| 671 | } |
| 672 | |
| 673 | kvm_free_physmem_slot(&old, &new); |
| 674 | return 0; |
| 675 | |
| 676 | out_unlock: |
| 677 | spin_unlock(&kvm->lock); |
| 678 | out_free: |
| 679 | kvm_free_physmem_slot(&new, &old); |
| 680 | out: |
| 681 | return r; |
| 682 | } |
| 683 | |
| 684 | /* |
| 685 | * Get (and clear) the dirty memory log for a memory slot. |
| 686 | */ |
| 687 | static int kvm_dev_ioctl_get_dirty_log(struct kvm *kvm, |
| 688 | struct kvm_dirty_log *log) |
| 689 | { |
| 690 | struct kvm_memory_slot *memslot; |
| 691 | int r, i; |
| 692 | int n; |
| 693 | unsigned long any = 0; |
| 694 | |
| 695 | spin_lock(&kvm->lock); |
| 696 | |
| 697 | /* |
| 698 | * Prevent changes to guest memory configuration even while the lock |
| 699 | * is not taken. |
| 700 | */ |
| 701 | ++kvm->busy; |
| 702 | spin_unlock(&kvm->lock); |
| 703 | r = -EINVAL; |
| 704 | if (log->slot >= KVM_MEMORY_SLOTS) |
| 705 | goto out; |
| 706 | |
| 707 | memslot = &kvm->memslots[log->slot]; |
| 708 | r = -ENOENT; |
| 709 | if (!memslot->dirty_bitmap) |
| 710 | goto out; |
| 711 | |
| 712 | n = ALIGN(memslot->npages, 8) / 8; |
| 713 | |
| 714 | for (i = 0; !any && i < n; ++i) |
| 715 | any = memslot->dirty_bitmap[i]; |
| 716 | |
| 717 | r = -EFAULT; |
| 718 | if (copy_to_user(log->dirty_bitmap, memslot->dirty_bitmap, n)) |
| 719 | goto out; |
| 720 | |
| 721 | |
| 722 | if (any) { |
| 723 | spin_lock(&kvm->lock); |
| 724 | kvm_mmu_slot_remove_write_access(kvm, log->slot); |
| 725 | spin_unlock(&kvm->lock); |
| 726 | memset(memslot->dirty_bitmap, 0, n); |
| 727 | for (i = 0; i < KVM_MAX_VCPUS; ++i) { |
| 728 | struct kvm_vcpu *vcpu = vcpu_load(kvm, i); |
| 729 | |
| 730 | if (!vcpu) |
| 731 | continue; |
| 732 | kvm_arch_ops->tlb_flush(vcpu); |
| 733 | vcpu_put(vcpu); |
| 734 | } |
| 735 | } |
| 736 | |
| 737 | r = 0; |
| 738 | |
| 739 | out: |
| 740 | spin_lock(&kvm->lock); |
| 741 | --kvm->busy; |
| 742 | spin_unlock(&kvm->lock); |
| 743 | return r; |
| 744 | } |
| 745 | |
| 746 | struct kvm_memory_slot *gfn_to_memslot(struct kvm *kvm, gfn_t gfn) |
| 747 | { |
| 748 | int i; |
| 749 | |
| 750 | for (i = 0; i < kvm->nmemslots; ++i) { |
| 751 | struct kvm_memory_slot *memslot = &kvm->memslots[i]; |
| 752 | |
| 753 | if (gfn >= memslot->base_gfn |
| 754 | && gfn < memslot->base_gfn + memslot->npages) |
| 755 | return memslot; |
| 756 | } |
| 757 | return 0; |
| 758 | } |
| 759 | EXPORT_SYMBOL_GPL(gfn_to_memslot); |
| 760 | |
| 761 | void mark_page_dirty(struct kvm *kvm, gfn_t gfn) |
| 762 | { |
| 763 | int i; |
| 764 | struct kvm_memory_slot *memslot = 0; |
| 765 | unsigned long rel_gfn; |
| 766 | |
| 767 | for (i = 0; i < kvm->nmemslots; ++i) { |
| 768 | memslot = &kvm->memslots[i]; |
| 769 | |
| 770 | if (gfn >= memslot->base_gfn |
| 771 | && gfn < memslot->base_gfn + memslot->npages) { |
| 772 | |
| 773 | if (!memslot || !memslot->dirty_bitmap) |
| 774 | return; |
| 775 | |
| 776 | rel_gfn = gfn - memslot->base_gfn; |
| 777 | |
| 778 | /* avoid RMW */ |
| 779 | if (!test_bit(rel_gfn, memslot->dirty_bitmap)) |
| 780 | set_bit(rel_gfn, memslot->dirty_bitmap); |
| 781 | return; |
| 782 | } |
| 783 | } |
| 784 | } |
| 785 | |
| 786 | static int emulator_read_std(unsigned long addr, |
| 787 | unsigned long *val, |
| 788 | unsigned int bytes, |
| 789 | struct x86_emulate_ctxt *ctxt) |
| 790 | { |
| 791 | struct kvm_vcpu *vcpu = ctxt->vcpu; |
| 792 | void *data = val; |
| 793 | |
| 794 | while (bytes) { |
| 795 | gpa_t gpa = vcpu->mmu.gva_to_gpa(vcpu, addr); |
| 796 | unsigned offset = addr & (PAGE_SIZE-1); |
| 797 | unsigned tocopy = min(bytes, (unsigned)PAGE_SIZE - offset); |
| 798 | unsigned long pfn; |
| 799 | struct kvm_memory_slot *memslot; |
| 800 | void *page; |
| 801 | |
| 802 | if (gpa == UNMAPPED_GVA) |
| 803 | return X86EMUL_PROPAGATE_FAULT; |
| 804 | pfn = gpa >> PAGE_SHIFT; |
| 805 | memslot = gfn_to_memslot(vcpu->kvm, pfn); |
| 806 | if (!memslot) |
| 807 | return X86EMUL_UNHANDLEABLE; |
| 808 | page = kmap_atomic(gfn_to_page(memslot, pfn), KM_USER0); |
| 809 | |
| 810 | memcpy(data, page + offset, tocopy); |
| 811 | |
| 812 | kunmap_atomic(page, KM_USER0); |
| 813 | |
| 814 | bytes -= tocopy; |
| 815 | data += tocopy; |
| 816 | addr += tocopy; |
| 817 | } |
| 818 | |
| 819 | return X86EMUL_CONTINUE; |
| 820 | } |
| 821 | |
| 822 | static int emulator_write_std(unsigned long addr, |
| 823 | unsigned long val, |
| 824 | unsigned int bytes, |
| 825 | struct x86_emulate_ctxt *ctxt) |
| 826 | { |
| 827 | printk(KERN_ERR "emulator_write_std: addr %lx n %d\n", |
| 828 | addr, bytes); |
| 829 | return X86EMUL_UNHANDLEABLE; |
| 830 | } |
| 831 | |
| 832 | static int emulator_read_emulated(unsigned long addr, |
| 833 | unsigned long *val, |
| 834 | unsigned int bytes, |
| 835 | struct x86_emulate_ctxt *ctxt) |
| 836 | { |
| 837 | struct kvm_vcpu *vcpu = ctxt->vcpu; |
| 838 | |
| 839 | if (vcpu->mmio_read_completed) { |
| 840 | memcpy(val, vcpu->mmio_data, bytes); |
| 841 | vcpu->mmio_read_completed = 0; |
| 842 | return X86EMUL_CONTINUE; |
| 843 | } else if (emulator_read_std(addr, val, bytes, ctxt) |
| 844 | == X86EMUL_CONTINUE) |
| 845 | return X86EMUL_CONTINUE; |
| 846 | else { |
| 847 | gpa_t gpa = vcpu->mmu.gva_to_gpa(vcpu, addr); |
| 848 | if (gpa == UNMAPPED_GVA) |
| 849 | return vcpu_printf(vcpu, "not present\n"), X86EMUL_PROPAGATE_FAULT; |
| 850 | vcpu->mmio_needed = 1; |
| 851 | vcpu->mmio_phys_addr = gpa; |
| 852 | vcpu->mmio_size = bytes; |
| 853 | vcpu->mmio_is_write = 0; |
| 854 | |
| 855 | return X86EMUL_UNHANDLEABLE; |
| 856 | } |
| 857 | } |
| 858 | |
| 859 | static int emulator_write_emulated(unsigned long addr, |
| 860 | unsigned long val, |
| 861 | unsigned int bytes, |
| 862 | struct x86_emulate_ctxt *ctxt) |
| 863 | { |
| 864 | struct kvm_vcpu *vcpu = ctxt->vcpu; |
| 865 | gpa_t gpa = vcpu->mmu.gva_to_gpa(vcpu, addr); |
| 866 | |
| 867 | if (gpa == UNMAPPED_GVA) |
| 868 | return X86EMUL_PROPAGATE_FAULT; |
| 869 | |
| 870 | vcpu->mmio_needed = 1; |
| 871 | vcpu->mmio_phys_addr = gpa; |
| 872 | vcpu->mmio_size = bytes; |
| 873 | vcpu->mmio_is_write = 1; |
| 874 | memcpy(vcpu->mmio_data, &val, bytes); |
| 875 | |
| 876 | return X86EMUL_CONTINUE; |
| 877 | } |
| 878 | |
| 879 | static int emulator_cmpxchg_emulated(unsigned long addr, |
| 880 | unsigned long old, |
| 881 | unsigned long new, |
| 882 | unsigned int bytes, |
| 883 | struct x86_emulate_ctxt *ctxt) |
| 884 | { |
| 885 | static int reported; |
| 886 | |
| 887 | if (!reported) { |
| 888 | reported = 1; |
| 889 | printk(KERN_WARNING "kvm: emulating exchange as write\n"); |
| 890 | } |
| 891 | return emulator_write_emulated(addr, new, bytes, ctxt); |
| 892 | } |
| 893 | |
| 894 | static unsigned long get_segment_base(struct kvm_vcpu *vcpu, int seg) |
| 895 | { |
| 896 | return kvm_arch_ops->get_segment_base(vcpu, seg); |
| 897 | } |
| 898 | |
| 899 | int emulate_invlpg(struct kvm_vcpu *vcpu, gva_t address) |
| 900 | { |
| 901 | spin_lock(&vcpu->kvm->lock); |
| 902 | vcpu->mmu.inval_page(vcpu, address); |
| 903 | spin_unlock(&vcpu->kvm->lock); |
| 904 | kvm_arch_ops->invlpg(vcpu, address); |
| 905 | return X86EMUL_CONTINUE; |
| 906 | } |
| 907 | |
| 908 | int emulate_clts(struct kvm_vcpu *vcpu) |
| 909 | { |
| 910 | unsigned long cr0 = vcpu->cr0; |
| 911 | |
| 912 | cr0 &= ~CR0_TS_MASK; |
| 913 | kvm_arch_ops->set_cr0(vcpu, cr0); |
| 914 | return X86EMUL_CONTINUE; |
| 915 | } |
| 916 | |
| 917 | int emulator_get_dr(struct x86_emulate_ctxt* ctxt, int dr, unsigned long *dest) |
| 918 | { |
| 919 | struct kvm_vcpu *vcpu = ctxt->vcpu; |
| 920 | |
| 921 | switch (dr) { |
| 922 | case 0 ... 3: |
| 923 | *dest = kvm_arch_ops->get_dr(vcpu, dr); |
| 924 | return X86EMUL_CONTINUE; |
| 925 | default: |
| 926 | printk(KERN_DEBUG "%s: unexpected dr %u\n", |
| 927 | __FUNCTION__, dr); |
| 928 | return X86EMUL_UNHANDLEABLE; |
| 929 | } |
| 930 | } |
| 931 | |
| 932 | int emulator_set_dr(struct x86_emulate_ctxt *ctxt, int dr, unsigned long value) |
| 933 | { |
| 934 | unsigned long mask = (ctxt->mode == X86EMUL_MODE_PROT64) ? ~0ULL : ~0U; |
| 935 | int exception; |
| 936 | |
| 937 | kvm_arch_ops->set_dr(ctxt->vcpu, dr, value & mask, &exception); |
| 938 | if (exception) { |
| 939 | /* FIXME: better handling */ |
| 940 | return X86EMUL_UNHANDLEABLE; |
| 941 | } |
| 942 | return X86EMUL_CONTINUE; |
| 943 | } |
| 944 | |
| 945 | static void report_emulation_failure(struct x86_emulate_ctxt *ctxt) |
| 946 | { |
| 947 | static int reported; |
| 948 | u8 opcodes[4]; |
| 949 | unsigned long rip = ctxt->vcpu->rip; |
| 950 | unsigned long rip_linear; |
| 951 | |
| 952 | rip_linear = rip + get_segment_base(ctxt->vcpu, VCPU_SREG_CS); |
| 953 | |
| 954 | if (reported) |
| 955 | return; |
| 956 | |
| 957 | emulator_read_std(rip_linear, (void *)opcodes, 4, ctxt); |
| 958 | |
| 959 | printk(KERN_ERR "emulation failed but !mmio_needed?" |
| 960 | " rip %lx %02x %02x %02x %02x\n", |
| 961 | rip, opcodes[0], opcodes[1], opcodes[2], opcodes[3]); |
| 962 | reported = 1; |
| 963 | } |
| 964 | |
| 965 | struct x86_emulate_ops emulate_ops = { |
| 966 | .read_std = emulator_read_std, |
| 967 | .write_std = emulator_write_std, |
| 968 | .read_emulated = emulator_read_emulated, |
| 969 | .write_emulated = emulator_write_emulated, |
| 970 | .cmpxchg_emulated = emulator_cmpxchg_emulated, |
| 971 | }; |
| 972 | |
| 973 | int emulate_instruction(struct kvm_vcpu *vcpu, |
| 974 | struct kvm_run *run, |
| 975 | unsigned long cr2, |
| 976 | u16 error_code) |
| 977 | { |
| 978 | struct x86_emulate_ctxt emulate_ctxt; |
| 979 | int r; |
| 980 | int cs_db, cs_l; |
| 981 | |
| 982 | kvm_arch_ops->cache_regs(vcpu); |
| 983 | |
| 984 | kvm_arch_ops->get_cs_db_l_bits(vcpu, &cs_db, &cs_l); |
| 985 | |
| 986 | emulate_ctxt.vcpu = vcpu; |
| 987 | emulate_ctxt.eflags = kvm_arch_ops->get_rflags(vcpu); |
| 988 | emulate_ctxt.cr2 = cr2; |
| 989 | emulate_ctxt.mode = (emulate_ctxt.eflags & X86_EFLAGS_VM) |
| 990 | ? X86EMUL_MODE_REAL : cs_l |
| 991 | ? X86EMUL_MODE_PROT64 : cs_db |
| 992 | ? X86EMUL_MODE_PROT32 : X86EMUL_MODE_PROT16; |
| 993 | |
| 994 | if (emulate_ctxt.mode == X86EMUL_MODE_PROT64) { |
| 995 | emulate_ctxt.cs_base = 0; |
| 996 | emulate_ctxt.ds_base = 0; |
| 997 | emulate_ctxt.es_base = 0; |
| 998 | emulate_ctxt.ss_base = 0; |
| 999 | } else { |
| 1000 | emulate_ctxt.cs_base = get_segment_base(vcpu, VCPU_SREG_CS); |
| 1001 | emulate_ctxt.ds_base = get_segment_base(vcpu, VCPU_SREG_DS); |
| 1002 | emulate_ctxt.es_base = get_segment_base(vcpu, VCPU_SREG_ES); |
| 1003 | emulate_ctxt.ss_base = get_segment_base(vcpu, VCPU_SREG_SS); |
| 1004 | } |
| 1005 | |
| 1006 | emulate_ctxt.gs_base = get_segment_base(vcpu, VCPU_SREG_GS); |
| 1007 | emulate_ctxt.fs_base = get_segment_base(vcpu, VCPU_SREG_FS); |
| 1008 | |
| 1009 | vcpu->mmio_is_write = 0; |
| 1010 | r = x86_emulate_memop(&emulate_ctxt, &emulate_ops); |
| 1011 | |
| 1012 | if ((r || vcpu->mmio_is_write) && run) { |
| 1013 | run->mmio.phys_addr = vcpu->mmio_phys_addr; |
| 1014 | memcpy(run->mmio.data, vcpu->mmio_data, 8); |
| 1015 | run->mmio.len = vcpu->mmio_size; |
| 1016 | run->mmio.is_write = vcpu->mmio_is_write; |
| 1017 | } |
| 1018 | |
| 1019 | if (r) { |
| 1020 | if (!vcpu->mmio_needed) { |
| 1021 | report_emulation_failure(&emulate_ctxt); |
| 1022 | return EMULATE_FAIL; |
| 1023 | } |
| 1024 | return EMULATE_DO_MMIO; |
| 1025 | } |
| 1026 | |
| 1027 | kvm_arch_ops->decache_regs(vcpu); |
| 1028 | kvm_arch_ops->set_rflags(vcpu, emulate_ctxt.eflags); |
| 1029 | |
| 1030 | if (vcpu->mmio_is_write) |
| 1031 | return EMULATE_DO_MMIO; |
| 1032 | |
| 1033 | return EMULATE_DONE; |
| 1034 | } |
| 1035 | EXPORT_SYMBOL_GPL(emulate_instruction); |
| 1036 | |
| 1037 | static u64 mk_cr_64(u64 curr_cr, u32 new_val) |
| 1038 | { |
| 1039 | return (curr_cr & ~((1ULL << 32) - 1)) | new_val; |
| 1040 | } |
| 1041 | |
| 1042 | void realmode_lgdt(struct kvm_vcpu *vcpu, u16 limit, unsigned long base) |
| 1043 | { |
| 1044 | struct descriptor_table dt = { limit, base }; |
| 1045 | |
| 1046 | kvm_arch_ops->set_gdt(vcpu, &dt); |
| 1047 | } |
| 1048 | |
| 1049 | void realmode_lidt(struct kvm_vcpu *vcpu, u16 limit, unsigned long base) |
| 1050 | { |
| 1051 | struct descriptor_table dt = { limit, base }; |
| 1052 | |
| 1053 | kvm_arch_ops->set_idt(vcpu, &dt); |
| 1054 | } |
| 1055 | |
| 1056 | void realmode_lmsw(struct kvm_vcpu *vcpu, unsigned long msw, |
| 1057 | unsigned long *rflags) |
| 1058 | { |
| 1059 | lmsw(vcpu, msw); |
| 1060 | *rflags = kvm_arch_ops->get_rflags(vcpu); |
| 1061 | } |
| 1062 | |
| 1063 | unsigned long realmode_get_cr(struct kvm_vcpu *vcpu, int cr) |
| 1064 | { |
| 1065 | switch (cr) { |
| 1066 | case 0: |
| 1067 | return vcpu->cr0; |
| 1068 | case 2: |
| 1069 | return vcpu->cr2; |
| 1070 | case 3: |
| 1071 | return vcpu->cr3; |
| 1072 | case 4: |
| 1073 | return vcpu->cr4; |
| 1074 | default: |
| 1075 | vcpu_printf(vcpu, "%s: unexpected cr %u\n", __FUNCTION__, cr); |
| 1076 | return 0; |
| 1077 | } |
| 1078 | } |
| 1079 | |
| 1080 | void realmode_set_cr(struct kvm_vcpu *vcpu, int cr, unsigned long val, |
| 1081 | unsigned long *rflags) |
| 1082 | { |
| 1083 | switch (cr) { |
| 1084 | case 0: |
| 1085 | set_cr0(vcpu, mk_cr_64(vcpu->cr0, val)); |
| 1086 | *rflags = kvm_arch_ops->get_rflags(vcpu); |
| 1087 | break; |
| 1088 | case 2: |
| 1089 | vcpu->cr2 = val; |
| 1090 | break; |
| 1091 | case 3: |
| 1092 | set_cr3(vcpu, val); |
| 1093 | break; |
| 1094 | case 4: |
| 1095 | set_cr4(vcpu, mk_cr_64(vcpu->cr4, val)); |
| 1096 | break; |
| 1097 | default: |
| 1098 | vcpu_printf(vcpu, "%s: unexpected cr %u\n", __FUNCTION__, cr); |
| 1099 | } |
| 1100 | } |
| 1101 | |
| 1102 | /* |
| 1103 | * Reads an msr value (of 'msr_index') into 'pdata'. |
| 1104 | * Returns 0 on success, non-0 otherwise. |
| 1105 | * Assumes vcpu_load() was already called. |
| 1106 | */ |
| 1107 | static int get_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 *pdata) |
| 1108 | { |
| 1109 | return kvm_arch_ops->get_msr(vcpu, msr_index, pdata); |
| 1110 | } |
| 1111 | |
Avi Kivity | 05b3e0c | 2006-12-13 00:33:45 -0800 | [diff] [blame] | 1112 | #ifdef CONFIG_X86_64 |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1113 | |
| 1114 | void set_efer(struct kvm_vcpu *vcpu, u64 efer) |
| 1115 | { |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1116 | if (efer & EFER_RESERVED_BITS) { |
| 1117 | printk(KERN_DEBUG "set_efer: 0x%llx #GP, reserved bits\n", |
| 1118 | efer); |
| 1119 | inject_gp(vcpu); |
| 1120 | return; |
| 1121 | } |
| 1122 | |
| 1123 | if (is_paging(vcpu) |
| 1124 | && (vcpu->shadow_efer & EFER_LME) != (efer & EFER_LME)) { |
| 1125 | printk(KERN_DEBUG "set_efer: #GP, change LME while paging\n"); |
| 1126 | inject_gp(vcpu); |
| 1127 | return; |
| 1128 | } |
| 1129 | |
Avi Kivity | 7725f0b | 2006-12-13 00:34:01 -0800 | [diff] [blame^] | 1130 | kvm_arch_ops->set_efer(vcpu, efer); |
| 1131 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1132 | efer &= ~EFER_LMA; |
| 1133 | efer |= vcpu->shadow_efer & EFER_LMA; |
| 1134 | |
| 1135 | vcpu->shadow_efer = efer; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1136 | } |
| 1137 | EXPORT_SYMBOL_GPL(set_efer); |
| 1138 | |
| 1139 | #endif |
| 1140 | |
| 1141 | /* |
| 1142 | * Writes msr value into into the appropriate "register". |
| 1143 | * Returns 0 on success, non-0 otherwise. |
| 1144 | * Assumes vcpu_load() was already called. |
| 1145 | */ |
| 1146 | static int set_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 data) |
| 1147 | { |
| 1148 | return kvm_arch_ops->set_msr(vcpu, msr_index, data); |
| 1149 | } |
| 1150 | |
| 1151 | void kvm_resched(struct kvm_vcpu *vcpu) |
| 1152 | { |
| 1153 | vcpu_put(vcpu); |
| 1154 | cond_resched(); |
| 1155 | /* Cannot fail - no vcpu unplug yet. */ |
| 1156 | vcpu_load(vcpu->kvm, vcpu_slot(vcpu)); |
| 1157 | } |
| 1158 | EXPORT_SYMBOL_GPL(kvm_resched); |
| 1159 | |
| 1160 | void load_msrs(struct vmx_msr_entry *e, int n) |
| 1161 | { |
| 1162 | int i; |
| 1163 | |
| 1164 | for (i = 0; i < n; ++i) |
| 1165 | wrmsrl(e[i].index, e[i].data); |
| 1166 | } |
| 1167 | EXPORT_SYMBOL_GPL(load_msrs); |
| 1168 | |
| 1169 | void save_msrs(struct vmx_msr_entry *e, int n) |
| 1170 | { |
| 1171 | int i; |
| 1172 | |
| 1173 | for (i = 0; i < n; ++i) |
| 1174 | rdmsrl(e[i].index, e[i].data); |
| 1175 | } |
| 1176 | EXPORT_SYMBOL_GPL(save_msrs); |
| 1177 | |
| 1178 | static int kvm_dev_ioctl_run(struct kvm *kvm, struct kvm_run *kvm_run) |
| 1179 | { |
| 1180 | struct kvm_vcpu *vcpu; |
| 1181 | int r; |
| 1182 | |
| 1183 | if (kvm_run->vcpu < 0 || kvm_run->vcpu >= KVM_MAX_VCPUS) |
| 1184 | return -EINVAL; |
| 1185 | |
| 1186 | vcpu = vcpu_load(kvm, kvm_run->vcpu); |
| 1187 | if (!vcpu) |
| 1188 | return -ENOENT; |
| 1189 | |
| 1190 | if (kvm_run->emulated) { |
| 1191 | kvm_arch_ops->skip_emulated_instruction(vcpu); |
| 1192 | kvm_run->emulated = 0; |
| 1193 | } |
| 1194 | |
| 1195 | if (kvm_run->mmio_completed) { |
| 1196 | memcpy(vcpu->mmio_data, kvm_run->mmio.data, 8); |
| 1197 | vcpu->mmio_read_completed = 1; |
| 1198 | } |
| 1199 | |
| 1200 | vcpu->mmio_needed = 0; |
| 1201 | |
| 1202 | r = kvm_arch_ops->run(vcpu, kvm_run); |
| 1203 | |
| 1204 | vcpu_put(vcpu); |
| 1205 | return r; |
| 1206 | } |
| 1207 | |
| 1208 | static int kvm_dev_ioctl_get_regs(struct kvm *kvm, struct kvm_regs *regs) |
| 1209 | { |
| 1210 | struct kvm_vcpu *vcpu; |
| 1211 | |
| 1212 | if (regs->vcpu < 0 || regs->vcpu >= KVM_MAX_VCPUS) |
| 1213 | return -EINVAL; |
| 1214 | |
| 1215 | vcpu = vcpu_load(kvm, regs->vcpu); |
| 1216 | if (!vcpu) |
| 1217 | return -ENOENT; |
| 1218 | |
| 1219 | kvm_arch_ops->cache_regs(vcpu); |
| 1220 | |
| 1221 | regs->rax = vcpu->regs[VCPU_REGS_RAX]; |
| 1222 | regs->rbx = vcpu->regs[VCPU_REGS_RBX]; |
| 1223 | regs->rcx = vcpu->regs[VCPU_REGS_RCX]; |
| 1224 | regs->rdx = vcpu->regs[VCPU_REGS_RDX]; |
| 1225 | regs->rsi = vcpu->regs[VCPU_REGS_RSI]; |
| 1226 | regs->rdi = vcpu->regs[VCPU_REGS_RDI]; |
| 1227 | regs->rsp = vcpu->regs[VCPU_REGS_RSP]; |
| 1228 | regs->rbp = vcpu->regs[VCPU_REGS_RBP]; |
Avi Kivity | 05b3e0c | 2006-12-13 00:33:45 -0800 | [diff] [blame] | 1229 | #ifdef CONFIG_X86_64 |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1230 | regs->r8 = vcpu->regs[VCPU_REGS_R8]; |
| 1231 | regs->r9 = vcpu->regs[VCPU_REGS_R9]; |
| 1232 | regs->r10 = vcpu->regs[VCPU_REGS_R10]; |
| 1233 | regs->r11 = vcpu->regs[VCPU_REGS_R11]; |
| 1234 | regs->r12 = vcpu->regs[VCPU_REGS_R12]; |
| 1235 | regs->r13 = vcpu->regs[VCPU_REGS_R13]; |
| 1236 | regs->r14 = vcpu->regs[VCPU_REGS_R14]; |
| 1237 | regs->r15 = vcpu->regs[VCPU_REGS_R15]; |
| 1238 | #endif |
| 1239 | |
| 1240 | regs->rip = vcpu->rip; |
| 1241 | regs->rflags = kvm_arch_ops->get_rflags(vcpu); |
| 1242 | |
| 1243 | /* |
| 1244 | * Don't leak debug flags in case they were set for guest debugging |
| 1245 | */ |
| 1246 | if (vcpu->guest_debug.enabled && vcpu->guest_debug.singlestep) |
| 1247 | regs->rflags &= ~(X86_EFLAGS_TF | X86_EFLAGS_RF); |
| 1248 | |
| 1249 | vcpu_put(vcpu); |
| 1250 | |
| 1251 | return 0; |
| 1252 | } |
| 1253 | |
| 1254 | static int kvm_dev_ioctl_set_regs(struct kvm *kvm, struct kvm_regs *regs) |
| 1255 | { |
| 1256 | struct kvm_vcpu *vcpu; |
| 1257 | |
| 1258 | if (regs->vcpu < 0 || regs->vcpu >= KVM_MAX_VCPUS) |
| 1259 | return -EINVAL; |
| 1260 | |
| 1261 | vcpu = vcpu_load(kvm, regs->vcpu); |
| 1262 | if (!vcpu) |
| 1263 | return -ENOENT; |
| 1264 | |
| 1265 | vcpu->regs[VCPU_REGS_RAX] = regs->rax; |
| 1266 | vcpu->regs[VCPU_REGS_RBX] = regs->rbx; |
| 1267 | vcpu->regs[VCPU_REGS_RCX] = regs->rcx; |
| 1268 | vcpu->regs[VCPU_REGS_RDX] = regs->rdx; |
| 1269 | vcpu->regs[VCPU_REGS_RSI] = regs->rsi; |
| 1270 | vcpu->regs[VCPU_REGS_RDI] = regs->rdi; |
| 1271 | vcpu->regs[VCPU_REGS_RSP] = regs->rsp; |
| 1272 | vcpu->regs[VCPU_REGS_RBP] = regs->rbp; |
Avi Kivity | 05b3e0c | 2006-12-13 00:33:45 -0800 | [diff] [blame] | 1273 | #ifdef CONFIG_X86_64 |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1274 | vcpu->regs[VCPU_REGS_R8] = regs->r8; |
| 1275 | vcpu->regs[VCPU_REGS_R9] = regs->r9; |
| 1276 | vcpu->regs[VCPU_REGS_R10] = regs->r10; |
| 1277 | vcpu->regs[VCPU_REGS_R11] = regs->r11; |
| 1278 | vcpu->regs[VCPU_REGS_R12] = regs->r12; |
| 1279 | vcpu->regs[VCPU_REGS_R13] = regs->r13; |
| 1280 | vcpu->regs[VCPU_REGS_R14] = regs->r14; |
| 1281 | vcpu->regs[VCPU_REGS_R15] = regs->r15; |
| 1282 | #endif |
| 1283 | |
| 1284 | vcpu->rip = regs->rip; |
| 1285 | kvm_arch_ops->set_rflags(vcpu, regs->rflags); |
| 1286 | |
| 1287 | kvm_arch_ops->decache_regs(vcpu); |
| 1288 | |
| 1289 | vcpu_put(vcpu); |
| 1290 | |
| 1291 | return 0; |
| 1292 | } |
| 1293 | |
| 1294 | static void get_segment(struct kvm_vcpu *vcpu, |
| 1295 | struct kvm_segment *var, int seg) |
| 1296 | { |
| 1297 | return kvm_arch_ops->get_segment(vcpu, var, seg); |
| 1298 | } |
| 1299 | |
| 1300 | static int kvm_dev_ioctl_get_sregs(struct kvm *kvm, struct kvm_sregs *sregs) |
| 1301 | { |
| 1302 | struct kvm_vcpu *vcpu; |
| 1303 | struct descriptor_table dt; |
| 1304 | |
| 1305 | if (sregs->vcpu < 0 || sregs->vcpu >= KVM_MAX_VCPUS) |
| 1306 | return -EINVAL; |
| 1307 | vcpu = vcpu_load(kvm, sregs->vcpu); |
| 1308 | if (!vcpu) |
| 1309 | return -ENOENT; |
| 1310 | |
| 1311 | get_segment(vcpu, &sregs->cs, VCPU_SREG_CS); |
| 1312 | get_segment(vcpu, &sregs->ds, VCPU_SREG_DS); |
| 1313 | get_segment(vcpu, &sregs->es, VCPU_SREG_ES); |
| 1314 | get_segment(vcpu, &sregs->fs, VCPU_SREG_FS); |
| 1315 | get_segment(vcpu, &sregs->gs, VCPU_SREG_GS); |
| 1316 | get_segment(vcpu, &sregs->ss, VCPU_SREG_SS); |
| 1317 | |
| 1318 | get_segment(vcpu, &sregs->tr, VCPU_SREG_TR); |
| 1319 | get_segment(vcpu, &sregs->ldt, VCPU_SREG_LDTR); |
| 1320 | |
| 1321 | kvm_arch_ops->get_idt(vcpu, &dt); |
| 1322 | sregs->idt.limit = dt.limit; |
| 1323 | sregs->idt.base = dt.base; |
| 1324 | kvm_arch_ops->get_gdt(vcpu, &dt); |
| 1325 | sregs->gdt.limit = dt.limit; |
| 1326 | sregs->gdt.base = dt.base; |
| 1327 | |
| 1328 | sregs->cr0 = vcpu->cr0; |
| 1329 | sregs->cr2 = vcpu->cr2; |
| 1330 | sregs->cr3 = vcpu->cr3; |
| 1331 | sregs->cr4 = vcpu->cr4; |
| 1332 | sregs->cr8 = vcpu->cr8; |
| 1333 | sregs->efer = vcpu->shadow_efer; |
| 1334 | sregs->apic_base = vcpu->apic_base; |
| 1335 | |
| 1336 | memcpy(sregs->interrupt_bitmap, vcpu->irq_pending, |
| 1337 | sizeof sregs->interrupt_bitmap); |
| 1338 | |
| 1339 | vcpu_put(vcpu); |
| 1340 | |
| 1341 | return 0; |
| 1342 | } |
| 1343 | |
| 1344 | static void set_segment(struct kvm_vcpu *vcpu, |
| 1345 | struct kvm_segment *var, int seg) |
| 1346 | { |
| 1347 | return kvm_arch_ops->set_segment(vcpu, var, seg); |
| 1348 | } |
| 1349 | |
| 1350 | static int kvm_dev_ioctl_set_sregs(struct kvm *kvm, struct kvm_sregs *sregs) |
| 1351 | { |
| 1352 | struct kvm_vcpu *vcpu; |
| 1353 | int mmu_reset_needed = 0; |
| 1354 | int i; |
| 1355 | struct descriptor_table dt; |
| 1356 | |
| 1357 | if (sregs->vcpu < 0 || sregs->vcpu >= KVM_MAX_VCPUS) |
| 1358 | return -EINVAL; |
| 1359 | vcpu = vcpu_load(kvm, sregs->vcpu); |
| 1360 | if (!vcpu) |
| 1361 | return -ENOENT; |
| 1362 | |
| 1363 | set_segment(vcpu, &sregs->cs, VCPU_SREG_CS); |
| 1364 | set_segment(vcpu, &sregs->ds, VCPU_SREG_DS); |
| 1365 | set_segment(vcpu, &sregs->es, VCPU_SREG_ES); |
| 1366 | set_segment(vcpu, &sregs->fs, VCPU_SREG_FS); |
| 1367 | set_segment(vcpu, &sregs->gs, VCPU_SREG_GS); |
| 1368 | set_segment(vcpu, &sregs->ss, VCPU_SREG_SS); |
| 1369 | |
| 1370 | set_segment(vcpu, &sregs->tr, VCPU_SREG_TR); |
| 1371 | set_segment(vcpu, &sregs->ldt, VCPU_SREG_LDTR); |
| 1372 | |
| 1373 | dt.limit = sregs->idt.limit; |
| 1374 | dt.base = sregs->idt.base; |
| 1375 | kvm_arch_ops->set_idt(vcpu, &dt); |
| 1376 | dt.limit = sregs->gdt.limit; |
| 1377 | dt.base = sregs->gdt.base; |
| 1378 | kvm_arch_ops->set_gdt(vcpu, &dt); |
| 1379 | |
| 1380 | vcpu->cr2 = sregs->cr2; |
| 1381 | mmu_reset_needed |= vcpu->cr3 != sregs->cr3; |
| 1382 | vcpu->cr3 = sregs->cr3; |
| 1383 | |
| 1384 | vcpu->cr8 = sregs->cr8; |
| 1385 | |
| 1386 | mmu_reset_needed |= vcpu->shadow_efer != sregs->efer; |
Avi Kivity | 05b3e0c | 2006-12-13 00:33:45 -0800 | [diff] [blame] | 1387 | #ifdef CONFIG_X86_64 |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1388 | kvm_arch_ops->set_efer(vcpu, sregs->efer); |
| 1389 | #endif |
| 1390 | vcpu->apic_base = sregs->apic_base; |
| 1391 | |
| 1392 | mmu_reset_needed |= vcpu->cr0 != sregs->cr0; |
| 1393 | kvm_arch_ops->set_cr0_no_modeswitch(vcpu, sregs->cr0); |
| 1394 | |
| 1395 | mmu_reset_needed |= vcpu->cr4 != sregs->cr4; |
| 1396 | kvm_arch_ops->set_cr4(vcpu, sregs->cr4); |
| 1397 | |
| 1398 | if (mmu_reset_needed) |
| 1399 | kvm_mmu_reset_context(vcpu); |
| 1400 | |
| 1401 | memcpy(vcpu->irq_pending, sregs->interrupt_bitmap, |
| 1402 | sizeof vcpu->irq_pending); |
| 1403 | vcpu->irq_summary = 0; |
| 1404 | for (i = 0; i < NR_IRQ_WORDS; ++i) |
| 1405 | if (vcpu->irq_pending[i]) |
| 1406 | __set_bit(i, &vcpu->irq_summary); |
| 1407 | |
| 1408 | vcpu_put(vcpu); |
| 1409 | |
| 1410 | return 0; |
| 1411 | } |
| 1412 | |
| 1413 | /* |
| 1414 | * List of msr numbers which we expose to userspace through KVM_GET_MSRS |
| 1415 | * and KVM_SET_MSRS, and KVM_GET_MSR_INDEX_LIST. |
| 1416 | */ |
| 1417 | static u32 msrs_to_save[] = { |
| 1418 | MSR_IA32_SYSENTER_CS, MSR_IA32_SYSENTER_ESP, MSR_IA32_SYSENTER_EIP, |
| 1419 | MSR_K6_STAR, |
Avi Kivity | 05b3e0c | 2006-12-13 00:33:45 -0800 | [diff] [blame] | 1420 | #ifdef CONFIG_X86_64 |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1421 | MSR_CSTAR, MSR_KERNEL_GS_BASE, MSR_SYSCALL_MASK, MSR_LSTAR, |
| 1422 | #endif |
| 1423 | MSR_IA32_TIME_STAMP_COUNTER, |
| 1424 | }; |
| 1425 | |
| 1426 | |
| 1427 | /* |
| 1428 | * Adapt set_msr() to msr_io()'s calling convention |
| 1429 | */ |
| 1430 | static int do_set_msr(struct kvm_vcpu *vcpu, unsigned index, u64 *data) |
| 1431 | { |
| 1432 | return set_msr(vcpu, index, *data); |
| 1433 | } |
| 1434 | |
| 1435 | /* |
| 1436 | * Read or write a bunch of msrs. All parameters are kernel addresses. |
| 1437 | * |
| 1438 | * @return number of msrs set successfully. |
| 1439 | */ |
| 1440 | static int __msr_io(struct kvm *kvm, struct kvm_msrs *msrs, |
| 1441 | struct kvm_msr_entry *entries, |
| 1442 | int (*do_msr)(struct kvm_vcpu *vcpu, |
| 1443 | unsigned index, u64 *data)) |
| 1444 | { |
| 1445 | struct kvm_vcpu *vcpu; |
| 1446 | int i; |
| 1447 | |
| 1448 | if (msrs->vcpu < 0 || msrs->vcpu >= KVM_MAX_VCPUS) |
| 1449 | return -EINVAL; |
| 1450 | |
| 1451 | vcpu = vcpu_load(kvm, msrs->vcpu); |
| 1452 | if (!vcpu) |
| 1453 | return -ENOENT; |
| 1454 | |
| 1455 | for (i = 0; i < msrs->nmsrs; ++i) |
| 1456 | if (do_msr(vcpu, entries[i].index, &entries[i].data)) |
| 1457 | break; |
| 1458 | |
| 1459 | vcpu_put(vcpu); |
| 1460 | |
| 1461 | return i; |
| 1462 | } |
| 1463 | |
| 1464 | /* |
| 1465 | * Read or write a bunch of msrs. Parameters are user addresses. |
| 1466 | * |
| 1467 | * @return number of msrs set successfully. |
| 1468 | */ |
| 1469 | static int msr_io(struct kvm *kvm, struct kvm_msrs __user *user_msrs, |
| 1470 | int (*do_msr)(struct kvm_vcpu *vcpu, |
| 1471 | unsigned index, u64 *data), |
| 1472 | int writeback) |
| 1473 | { |
| 1474 | struct kvm_msrs msrs; |
| 1475 | struct kvm_msr_entry *entries; |
| 1476 | int r, n; |
| 1477 | unsigned size; |
| 1478 | |
| 1479 | r = -EFAULT; |
| 1480 | if (copy_from_user(&msrs, user_msrs, sizeof msrs)) |
| 1481 | goto out; |
| 1482 | |
| 1483 | r = -E2BIG; |
| 1484 | if (msrs.nmsrs >= MAX_IO_MSRS) |
| 1485 | goto out; |
| 1486 | |
| 1487 | r = -ENOMEM; |
| 1488 | size = sizeof(struct kvm_msr_entry) * msrs.nmsrs; |
| 1489 | entries = vmalloc(size); |
| 1490 | if (!entries) |
| 1491 | goto out; |
| 1492 | |
| 1493 | r = -EFAULT; |
| 1494 | if (copy_from_user(entries, user_msrs->entries, size)) |
| 1495 | goto out_free; |
| 1496 | |
| 1497 | r = n = __msr_io(kvm, &msrs, entries, do_msr); |
| 1498 | if (r < 0) |
| 1499 | goto out_free; |
| 1500 | |
| 1501 | r = -EFAULT; |
| 1502 | if (writeback && copy_to_user(user_msrs->entries, entries, size)) |
| 1503 | goto out_free; |
| 1504 | |
| 1505 | r = n; |
| 1506 | |
| 1507 | out_free: |
| 1508 | vfree(entries); |
| 1509 | out: |
| 1510 | return r; |
| 1511 | } |
| 1512 | |
| 1513 | /* |
| 1514 | * Translate a guest virtual address to a guest physical address. |
| 1515 | */ |
| 1516 | static int kvm_dev_ioctl_translate(struct kvm *kvm, struct kvm_translation *tr) |
| 1517 | { |
| 1518 | unsigned long vaddr = tr->linear_address; |
| 1519 | struct kvm_vcpu *vcpu; |
| 1520 | gpa_t gpa; |
| 1521 | |
| 1522 | vcpu = vcpu_load(kvm, tr->vcpu); |
| 1523 | if (!vcpu) |
| 1524 | return -ENOENT; |
| 1525 | spin_lock(&kvm->lock); |
| 1526 | gpa = vcpu->mmu.gva_to_gpa(vcpu, vaddr); |
| 1527 | tr->physical_address = gpa; |
| 1528 | tr->valid = gpa != UNMAPPED_GVA; |
| 1529 | tr->writeable = 1; |
| 1530 | tr->usermode = 0; |
| 1531 | spin_unlock(&kvm->lock); |
| 1532 | vcpu_put(vcpu); |
| 1533 | |
| 1534 | return 0; |
| 1535 | } |
| 1536 | |
| 1537 | static int kvm_dev_ioctl_interrupt(struct kvm *kvm, struct kvm_interrupt *irq) |
| 1538 | { |
| 1539 | struct kvm_vcpu *vcpu; |
| 1540 | |
| 1541 | if (irq->vcpu < 0 || irq->vcpu >= KVM_MAX_VCPUS) |
| 1542 | return -EINVAL; |
| 1543 | if (irq->irq < 0 || irq->irq >= 256) |
| 1544 | return -EINVAL; |
| 1545 | vcpu = vcpu_load(kvm, irq->vcpu); |
| 1546 | if (!vcpu) |
| 1547 | return -ENOENT; |
| 1548 | |
| 1549 | set_bit(irq->irq, vcpu->irq_pending); |
| 1550 | set_bit(irq->irq / BITS_PER_LONG, &vcpu->irq_summary); |
| 1551 | |
| 1552 | vcpu_put(vcpu); |
| 1553 | |
| 1554 | return 0; |
| 1555 | } |
| 1556 | |
| 1557 | static int kvm_dev_ioctl_debug_guest(struct kvm *kvm, |
| 1558 | struct kvm_debug_guest *dbg) |
| 1559 | { |
| 1560 | struct kvm_vcpu *vcpu; |
| 1561 | int r; |
| 1562 | |
| 1563 | if (dbg->vcpu < 0 || dbg->vcpu >= KVM_MAX_VCPUS) |
| 1564 | return -EINVAL; |
| 1565 | vcpu = vcpu_load(kvm, dbg->vcpu); |
| 1566 | if (!vcpu) |
| 1567 | return -ENOENT; |
| 1568 | |
| 1569 | r = kvm_arch_ops->set_guest_debug(vcpu, dbg); |
| 1570 | |
| 1571 | vcpu_put(vcpu); |
| 1572 | |
| 1573 | return r; |
| 1574 | } |
| 1575 | |
| 1576 | static long kvm_dev_ioctl(struct file *filp, |
| 1577 | unsigned int ioctl, unsigned long arg) |
| 1578 | { |
| 1579 | struct kvm *kvm = filp->private_data; |
| 1580 | int r = -EINVAL; |
| 1581 | |
| 1582 | switch (ioctl) { |
| 1583 | case KVM_CREATE_VCPU: { |
| 1584 | r = kvm_dev_ioctl_create_vcpu(kvm, arg); |
| 1585 | if (r) |
| 1586 | goto out; |
| 1587 | break; |
| 1588 | } |
| 1589 | case KVM_RUN: { |
| 1590 | struct kvm_run kvm_run; |
| 1591 | |
| 1592 | r = -EFAULT; |
| 1593 | if (copy_from_user(&kvm_run, (void *)arg, sizeof kvm_run)) |
| 1594 | goto out; |
| 1595 | r = kvm_dev_ioctl_run(kvm, &kvm_run); |
| 1596 | if (r < 0) |
| 1597 | goto out; |
| 1598 | r = -EFAULT; |
| 1599 | if (copy_to_user((void *)arg, &kvm_run, sizeof kvm_run)) |
| 1600 | goto out; |
| 1601 | r = 0; |
| 1602 | break; |
| 1603 | } |
| 1604 | case KVM_GET_REGS: { |
| 1605 | struct kvm_regs kvm_regs; |
| 1606 | |
| 1607 | r = -EFAULT; |
| 1608 | if (copy_from_user(&kvm_regs, (void *)arg, sizeof kvm_regs)) |
| 1609 | goto out; |
| 1610 | r = kvm_dev_ioctl_get_regs(kvm, &kvm_regs); |
| 1611 | if (r) |
| 1612 | goto out; |
| 1613 | r = -EFAULT; |
| 1614 | if (copy_to_user((void *)arg, &kvm_regs, sizeof kvm_regs)) |
| 1615 | goto out; |
| 1616 | r = 0; |
| 1617 | break; |
| 1618 | } |
| 1619 | case KVM_SET_REGS: { |
| 1620 | struct kvm_regs kvm_regs; |
| 1621 | |
| 1622 | r = -EFAULT; |
| 1623 | if (copy_from_user(&kvm_regs, (void *)arg, sizeof kvm_regs)) |
| 1624 | goto out; |
| 1625 | r = kvm_dev_ioctl_set_regs(kvm, &kvm_regs); |
| 1626 | if (r) |
| 1627 | goto out; |
| 1628 | r = 0; |
| 1629 | break; |
| 1630 | } |
| 1631 | case KVM_GET_SREGS: { |
| 1632 | struct kvm_sregs kvm_sregs; |
| 1633 | |
| 1634 | r = -EFAULT; |
| 1635 | if (copy_from_user(&kvm_sregs, (void *)arg, sizeof kvm_sregs)) |
| 1636 | goto out; |
| 1637 | r = kvm_dev_ioctl_get_sregs(kvm, &kvm_sregs); |
| 1638 | if (r) |
| 1639 | goto out; |
| 1640 | r = -EFAULT; |
| 1641 | if (copy_to_user((void *)arg, &kvm_sregs, sizeof kvm_sregs)) |
| 1642 | goto out; |
| 1643 | r = 0; |
| 1644 | break; |
| 1645 | } |
| 1646 | case KVM_SET_SREGS: { |
| 1647 | struct kvm_sregs kvm_sregs; |
| 1648 | |
| 1649 | r = -EFAULT; |
| 1650 | if (copy_from_user(&kvm_sregs, (void *)arg, sizeof kvm_sregs)) |
| 1651 | goto out; |
| 1652 | r = kvm_dev_ioctl_set_sregs(kvm, &kvm_sregs); |
| 1653 | if (r) |
| 1654 | goto out; |
| 1655 | r = 0; |
| 1656 | break; |
| 1657 | } |
| 1658 | case KVM_TRANSLATE: { |
| 1659 | struct kvm_translation tr; |
| 1660 | |
| 1661 | r = -EFAULT; |
| 1662 | if (copy_from_user(&tr, (void *)arg, sizeof tr)) |
| 1663 | goto out; |
| 1664 | r = kvm_dev_ioctl_translate(kvm, &tr); |
| 1665 | if (r) |
| 1666 | goto out; |
| 1667 | r = -EFAULT; |
| 1668 | if (copy_to_user((void *)arg, &tr, sizeof tr)) |
| 1669 | goto out; |
| 1670 | r = 0; |
| 1671 | break; |
| 1672 | } |
| 1673 | case KVM_INTERRUPT: { |
| 1674 | struct kvm_interrupt irq; |
| 1675 | |
| 1676 | r = -EFAULT; |
| 1677 | if (copy_from_user(&irq, (void *)arg, sizeof irq)) |
| 1678 | goto out; |
| 1679 | r = kvm_dev_ioctl_interrupt(kvm, &irq); |
| 1680 | if (r) |
| 1681 | goto out; |
| 1682 | r = 0; |
| 1683 | break; |
| 1684 | } |
| 1685 | case KVM_DEBUG_GUEST: { |
| 1686 | struct kvm_debug_guest dbg; |
| 1687 | |
| 1688 | r = -EFAULT; |
| 1689 | if (copy_from_user(&dbg, (void *)arg, sizeof dbg)) |
| 1690 | goto out; |
| 1691 | r = kvm_dev_ioctl_debug_guest(kvm, &dbg); |
| 1692 | if (r) |
| 1693 | goto out; |
| 1694 | r = 0; |
| 1695 | break; |
| 1696 | } |
| 1697 | case KVM_SET_MEMORY_REGION: { |
| 1698 | struct kvm_memory_region kvm_mem; |
| 1699 | |
| 1700 | r = -EFAULT; |
| 1701 | if (copy_from_user(&kvm_mem, (void *)arg, sizeof kvm_mem)) |
| 1702 | goto out; |
| 1703 | r = kvm_dev_ioctl_set_memory_region(kvm, &kvm_mem); |
| 1704 | if (r) |
| 1705 | goto out; |
| 1706 | break; |
| 1707 | } |
| 1708 | case KVM_GET_DIRTY_LOG: { |
| 1709 | struct kvm_dirty_log log; |
| 1710 | |
| 1711 | r = -EFAULT; |
| 1712 | if (copy_from_user(&log, (void *)arg, sizeof log)) |
| 1713 | goto out; |
| 1714 | r = kvm_dev_ioctl_get_dirty_log(kvm, &log); |
| 1715 | if (r) |
| 1716 | goto out; |
| 1717 | break; |
| 1718 | } |
| 1719 | case KVM_GET_MSRS: |
| 1720 | r = msr_io(kvm, (void __user *)arg, get_msr, 1); |
| 1721 | break; |
| 1722 | case KVM_SET_MSRS: |
| 1723 | r = msr_io(kvm, (void __user *)arg, do_set_msr, 0); |
| 1724 | break; |
| 1725 | case KVM_GET_MSR_INDEX_LIST: { |
| 1726 | struct kvm_msr_list __user *user_msr_list = (void __user *)arg; |
| 1727 | struct kvm_msr_list msr_list; |
| 1728 | unsigned n; |
| 1729 | |
| 1730 | r = -EFAULT; |
| 1731 | if (copy_from_user(&msr_list, user_msr_list, sizeof msr_list)) |
| 1732 | goto out; |
| 1733 | n = msr_list.nmsrs; |
| 1734 | msr_list.nmsrs = ARRAY_SIZE(msrs_to_save); |
| 1735 | if (copy_to_user(user_msr_list, &msr_list, sizeof msr_list)) |
| 1736 | goto out; |
| 1737 | r = -E2BIG; |
| 1738 | if (n < ARRAY_SIZE(msrs_to_save)) |
| 1739 | goto out; |
| 1740 | r = -EFAULT; |
| 1741 | if (copy_to_user(user_msr_list->indices, &msrs_to_save, |
| 1742 | sizeof msrs_to_save)) |
| 1743 | goto out; |
| 1744 | r = 0; |
| 1745 | } |
| 1746 | default: |
| 1747 | ; |
| 1748 | } |
| 1749 | out: |
| 1750 | return r; |
| 1751 | } |
| 1752 | |
| 1753 | static struct page *kvm_dev_nopage(struct vm_area_struct *vma, |
| 1754 | unsigned long address, |
| 1755 | int *type) |
| 1756 | { |
| 1757 | struct kvm *kvm = vma->vm_file->private_data; |
| 1758 | unsigned long pgoff; |
| 1759 | struct kvm_memory_slot *slot; |
| 1760 | struct page *page; |
| 1761 | |
| 1762 | *type = VM_FAULT_MINOR; |
| 1763 | pgoff = ((address - vma->vm_start) >> PAGE_SHIFT) + vma->vm_pgoff; |
| 1764 | slot = gfn_to_memslot(kvm, pgoff); |
| 1765 | if (!slot) |
| 1766 | return NOPAGE_SIGBUS; |
| 1767 | page = gfn_to_page(slot, pgoff); |
| 1768 | if (!page) |
| 1769 | return NOPAGE_SIGBUS; |
| 1770 | get_page(page); |
| 1771 | return page; |
| 1772 | } |
| 1773 | |
| 1774 | static struct vm_operations_struct kvm_dev_vm_ops = { |
| 1775 | .nopage = kvm_dev_nopage, |
| 1776 | }; |
| 1777 | |
| 1778 | static int kvm_dev_mmap(struct file *file, struct vm_area_struct *vma) |
| 1779 | { |
| 1780 | vma->vm_ops = &kvm_dev_vm_ops; |
| 1781 | return 0; |
| 1782 | } |
| 1783 | |
| 1784 | static struct file_operations kvm_chardev_ops = { |
| 1785 | .open = kvm_dev_open, |
| 1786 | .release = kvm_dev_release, |
| 1787 | .unlocked_ioctl = kvm_dev_ioctl, |
| 1788 | .compat_ioctl = kvm_dev_ioctl, |
| 1789 | .mmap = kvm_dev_mmap, |
| 1790 | }; |
| 1791 | |
| 1792 | static struct miscdevice kvm_dev = { |
| 1793 | MISC_DYNAMIC_MINOR, |
| 1794 | "kvm", |
| 1795 | &kvm_chardev_ops, |
| 1796 | }; |
| 1797 | |
| 1798 | static int kvm_reboot(struct notifier_block *notifier, unsigned long val, |
| 1799 | void *v) |
| 1800 | { |
| 1801 | if (val == SYS_RESTART) { |
| 1802 | /* |
| 1803 | * Some (well, at least mine) BIOSes hang on reboot if |
| 1804 | * in vmx root mode. |
| 1805 | */ |
| 1806 | printk(KERN_INFO "kvm: exiting hardware virtualization\n"); |
| 1807 | on_each_cpu(kvm_arch_ops->hardware_disable, 0, 0, 1); |
| 1808 | } |
| 1809 | return NOTIFY_OK; |
| 1810 | } |
| 1811 | |
| 1812 | static struct notifier_block kvm_reboot_notifier = { |
| 1813 | .notifier_call = kvm_reboot, |
| 1814 | .priority = 0, |
| 1815 | }; |
| 1816 | |
| 1817 | static __init void kvm_init_debug(void) |
| 1818 | { |
| 1819 | struct kvm_stats_debugfs_item *p; |
| 1820 | |
| 1821 | debugfs_dir = debugfs_create_dir("kvm", 0); |
| 1822 | for (p = debugfs_entries; p->name; ++p) |
| 1823 | p->dentry = debugfs_create_u32(p->name, 0444, debugfs_dir, |
| 1824 | p->data); |
| 1825 | } |
| 1826 | |
| 1827 | static void kvm_exit_debug(void) |
| 1828 | { |
| 1829 | struct kvm_stats_debugfs_item *p; |
| 1830 | |
| 1831 | for (p = debugfs_entries; p->name; ++p) |
| 1832 | debugfs_remove(p->dentry); |
| 1833 | debugfs_remove(debugfs_dir); |
| 1834 | } |
| 1835 | |
| 1836 | hpa_t bad_page_address; |
| 1837 | |
| 1838 | int kvm_init_arch(struct kvm_arch_ops *ops, struct module *module) |
| 1839 | { |
| 1840 | int r; |
| 1841 | |
| 1842 | kvm_arch_ops = ops; |
| 1843 | |
| 1844 | if (!kvm_arch_ops->cpu_has_kvm_support()) { |
| 1845 | printk(KERN_ERR "kvm: no hardware support\n"); |
| 1846 | return -EOPNOTSUPP; |
| 1847 | } |
| 1848 | if (kvm_arch_ops->disabled_by_bios()) { |
| 1849 | printk(KERN_ERR "kvm: disabled by bios\n"); |
| 1850 | return -EOPNOTSUPP; |
| 1851 | } |
| 1852 | |
| 1853 | r = kvm_arch_ops->hardware_setup(); |
| 1854 | if (r < 0) |
| 1855 | return r; |
| 1856 | |
| 1857 | on_each_cpu(kvm_arch_ops->hardware_enable, 0, 0, 1); |
| 1858 | register_reboot_notifier(&kvm_reboot_notifier); |
| 1859 | |
| 1860 | kvm_chardev_ops.owner = module; |
| 1861 | |
| 1862 | r = misc_register(&kvm_dev); |
| 1863 | if (r) { |
| 1864 | printk (KERN_ERR "kvm: misc device register failed\n"); |
| 1865 | goto out_free; |
| 1866 | } |
| 1867 | |
| 1868 | return r; |
| 1869 | |
| 1870 | out_free: |
| 1871 | unregister_reboot_notifier(&kvm_reboot_notifier); |
| 1872 | on_each_cpu(kvm_arch_ops->hardware_disable, 0, 0, 1); |
| 1873 | kvm_arch_ops->hardware_unsetup(); |
| 1874 | return r; |
| 1875 | } |
| 1876 | |
| 1877 | void kvm_exit_arch(void) |
| 1878 | { |
| 1879 | misc_deregister(&kvm_dev); |
| 1880 | |
| 1881 | unregister_reboot_notifier(&kvm_reboot_notifier); |
| 1882 | on_each_cpu(kvm_arch_ops->hardware_disable, 0, 0, 1); |
| 1883 | kvm_arch_ops->hardware_unsetup(); |
| 1884 | } |
| 1885 | |
| 1886 | static __init int kvm_init(void) |
| 1887 | { |
| 1888 | static struct page *bad_page; |
| 1889 | int r = 0; |
| 1890 | |
| 1891 | kvm_init_debug(); |
| 1892 | |
| 1893 | if ((bad_page = alloc_page(GFP_KERNEL)) == NULL) { |
| 1894 | r = -ENOMEM; |
| 1895 | goto out; |
| 1896 | } |
| 1897 | |
| 1898 | bad_page_address = page_to_pfn(bad_page) << PAGE_SHIFT; |
| 1899 | memset(__va(bad_page_address), 0, PAGE_SIZE); |
| 1900 | |
| 1901 | return r; |
| 1902 | |
| 1903 | out: |
| 1904 | kvm_exit_debug(); |
| 1905 | return r; |
| 1906 | } |
| 1907 | |
| 1908 | static __exit void kvm_exit(void) |
| 1909 | { |
| 1910 | kvm_exit_debug(); |
| 1911 | __free_page(pfn_to_page(bad_page_address >> PAGE_SHIFT)); |
| 1912 | } |
| 1913 | |
| 1914 | module_init(kvm_init) |
| 1915 | module_exit(kvm_exit) |
| 1916 | |
| 1917 | EXPORT_SYMBOL_GPL(kvm_init_arch); |
| 1918 | EXPORT_SYMBOL_GPL(kvm_exit_arch); |