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