Carsten Otte | 043405e | 2007-10-10 17:16:19 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Kernel-based Virtual Machine driver for Linux |
| 3 | * |
| 4 | * derived from drivers/kvm/kvm_main.c |
| 5 | * |
| 6 | * Copyright (C) 2006 Qumranet, Inc. |
| 7 | * |
| 8 | * Authors: |
| 9 | * Avi Kivity <avi@qumranet.com> |
| 10 | * Yaniv Kamay <yaniv@qumranet.com> |
| 11 | * |
| 12 | * This work is licensed under the terms of the GNU GPL, version 2. See |
| 13 | * the COPYING file in the top-level directory. |
| 14 | * |
| 15 | */ |
| 16 | |
Carsten Otte | 313a3dc | 2007-10-11 19:16:52 +0200 | [diff] [blame] | 17 | #include "kvm.h" |
Carsten Otte | 043405e | 2007-10-10 17:16:19 +0200 | [diff] [blame] | 18 | #include "x86.h" |
Zhang Xiantao | d825ed0 | 2007-11-14 20:08:51 +0800 | [diff] [blame] | 19 | #include "x86_emulate.h" |
Carsten Otte | 5fb76f9 | 2007-10-29 16:08:51 +0100 | [diff] [blame] | 20 | #include "segment_descriptor.h" |
Carsten Otte | 313a3dc | 2007-10-11 19:16:52 +0200 | [diff] [blame] | 21 | #include "irq.h" |
| 22 | |
| 23 | #include <linux/kvm.h> |
| 24 | #include <linux/fs.h> |
| 25 | #include <linux/vmalloc.h> |
Carsten Otte | 5fb76f9 | 2007-10-29 16:08:51 +0100 | [diff] [blame] | 26 | #include <linux/module.h> |
Carsten Otte | 043405e | 2007-10-10 17:16:19 +0200 | [diff] [blame] | 27 | |
| 28 | #include <asm/uaccess.h> |
Zhang Xiantao | d825ed0 | 2007-11-14 20:08:51 +0800 | [diff] [blame] | 29 | #include <asm/msr.h> |
Carsten Otte | 043405e | 2007-10-10 17:16:19 +0200 | [diff] [blame] | 30 | |
Carsten Otte | 313a3dc | 2007-10-11 19:16:52 +0200 | [diff] [blame] | 31 | #define MAX_IO_MSRS 256 |
Carsten Otte | a03490e | 2007-10-29 16:09:35 +0100 | [diff] [blame] | 32 | #define CR0_RESERVED_BITS \ |
| 33 | (~(unsigned long)(X86_CR0_PE | X86_CR0_MP | X86_CR0_EM | X86_CR0_TS \ |
| 34 | | X86_CR0_ET | X86_CR0_NE | X86_CR0_WP | X86_CR0_AM \ |
| 35 | | X86_CR0_NW | X86_CR0_CD | X86_CR0_PG)) |
| 36 | #define CR4_RESERVED_BITS \ |
| 37 | (~(unsigned long)(X86_CR4_VME | X86_CR4_PVI | X86_CR4_TSD | X86_CR4_DE\ |
| 38 | | X86_CR4_PSE | X86_CR4_PAE | X86_CR4_MCE \ |
| 39 | | X86_CR4_PGE | X86_CR4_PCE | X86_CR4_OSFXSR \ |
| 40 | | X86_CR4_OSXMMEXCPT | X86_CR4_VMXE)) |
| 41 | |
| 42 | #define CR8_RESERVED_BITS (~(unsigned long)X86_CR8_TPR) |
Carsten Otte | 15c4a64 | 2007-10-30 18:44:17 +0100 | [diff] [blame] | 43 | #define EFER_RESERVED_BITS 0xfffffffffffff2fe |
Carsten Otte | 313a3dc | 2007-10-11 19:16:52 +0200 | [diff] [blame] | 44 | |
Hollis Blanchard | 417bc30 | 2007-10-31 17:24:23 -0500 | [diff] [blame] | 45 | #define STAT_OFFSET(x) offsetof(struct kvm_vcpu, stat.x) |
| 46 | |
Zhang Xiantao | 97896d0 | 2007-11-14 20:09:30 +0800 | [diff] [blame] | 47 | struct kvm_x86_ops *kvm_x86_ops; |
| 48 | |
Hollis Blanchard | 417bc30 | 2007-10-31 17:24:23 -0500 | [diff] [blame] | 49 | struct kvm_stats_debugfs_item debugfs_entries[] = { |
| 50 | { "pf_fixed", STAT_OFFSET(pf_fixed) }, |
| 51 | { "pf_guest", STAT_OFFSET(pf_guest) }, |
| 52 | { "tlb_flush", STAT_OFFSET(tlb_flush) }, |
| 53 | { "invlpg", STAT_OFFSET(invlpg) }, |
| 54 | { "exits", STAT_OFFSET(exits) }, |
| 55 | { "io_exits", STAT_OFFSET(io_exits) }, |
| 56 | { "mmio_exits", STAT_OFFSET(mmio_exits) }, |
| 57 | { "signal_exits", STAT_OFFSET(signal_exits) }, |
| 58 | { "irq_window", STAT_OFFSET(irq_window_exits) }, |
| 59 | { "halt_exits", STAT_OFFSET(halt_exits) }, |
| 60 | { "halt_wakeup", STAT_OFFSET(halt_wakeup) }, |
| 61 | { "request_irq", STAT_OFFSET(request_irq_exits) }, |
| 62 | { "irq_exits", STAT_OFFSET(irq_exits) }, |
Avi Kivity | e1beb1d | 2007-11-18 13:50:24 +0200 | [diff] [blame] | 63 | { "host_state_reload", STAT_OFFSET(host_state_reload) }, |
Hollis Blanchard | 417bc30 | 2007-10-31 17:24:23 -0500 | [diff] [blame] | 64 | { "efer_reload", STAT_OFFSET(efer_reload) }, |
Avi Kivity | f096ed8 | 2007-11-18 13:54:33 +0200 | [diff] [blame^] | 65 | { "fpu_reload", STAT_OFFSET(fpu_reload) }, |
Hollis Blanchard | 417bc30 | 2007-10-31 17:24:23 -0500 | [diff] [blame] | 66 | { NULL } |
| 67 | }; |
| 68 | |
| 69 | |
Carsten Otte | 5fb76f9 | 2007-10-29 16:08:51 +0100 | [diff] [blame] | 70 | unsigned long segment_base(u16 selector) |
| 71 | { |
| 72 | struct descriptor_table gdt; |
| 73 | struct segment_descriptor *d; |
| 74 | unsigned long table_base; |
| 75 | unsigned long v; |
| 76 | |
| 77 | if (selector == 0) |
| 78 | return 0; |
| 79 | |
| 80 | asm("sgdt %0" : "=m"(gdt)); |
| 81 | table_base = gdt.base; |
| 82 | |
| 83 | if (selector & 4) { /* from ldt */ |
| 84 | u16 ldt_selector; |
| 85 | |
| 86 | asm("sldt %0" : "=g"(ldt_selector)); |
| 87 | table_base = segment_base(ldt_selector); |
| 88 | } |
| 89 | d = (struct segment_descriptor *)(table_base + (selector & ~7)); |
| 90 | v = d->base_low | ((unsigned long)d->base_mid << 16) | |
| 91 | ((unsigned long)d->base_high << 24); |
| 92 | #ifdef CONFIG_X86_64 |
| 93 | if (d->system == 0 && (d->type == 2 || d->type == 9 || d->type == 11)) |
| 94 | v |= ((unsigned long) \ |
| 95 | ((struct segment_descriptor_64 *)d)->base_higher) << 32; |
| 96 | #endif |
| 97 | return v; |
| 98 | } |
| 99 | EXPORT_SYMBOL_GPL(segment_base); |
| 100 | |
Carsten Otte | 6866b83 | 2007-10-29 16:09:10 +0100 | [diff] [blame] | 101 | u64 kvm_get_apic_base(struct kvm_vcpu *vcpu) |
| 102 | { |
| 103 | if (irqchip_in_kernel(vcpu->kvm)) |
| 104 | return vcpu->apic_base; |
| 105 | else |
| 106 | return vcpu->apic_base; |
| 107 | } |
| 108 | EXPORT_SYMBOL_GPL(kvm_get_apic_base); |
| 109 | |
| 110 | void kvm_set_apic_base(struct kvm_vcpu *vcpu, u64 data) |
| 111 | { |
| 112 | /* TODO: reserve bits check */ |
| 113 | if (irqchip_in_kernel(vcpu->kvm)) |
| 114 | kvm_lapic_set_base(vcpu, data); |
| 115 | else |
| 116 | vcpu->apic_base = data; |
| 117 | } |
| 118 | EXPORT_SYMBOL_GPL(kvm_set_apic_base); |
| 119 | |
Carsten Otte | a03490e | 2007-10-29 16:09:35 +0100 | [diff] [blame] | 120 | static void inject_gp(struct kvm_vcpu *vcpu) |
| 121 | { |
| 122 | kvm_x86_ops->inject_gp(vcpu, 0); |
| 123 | } |
| 124 | |
| 125 | /* |
| 126 | * Load the pae pdptrs. Return true is they are all valid. |
| 127 | */ |
| 128 | int load_pdptrs(struct kvm_vcpu *vcpu, unsigned long cr3) |
| 129 | { |
| 130 | gfn_t pdpt_gfn = cr3 >> PAGE_SHIFT; |
| 131 | unsigned offset = ((cr3 & (PAGE_SIZE-1)) >> 5) << 2; |
| 132 | int i; |
| 133 | int ret; |
| 134 | u64 pdpte[ARRAY_SIZE(vcpu->pdptrs)]; |
| 135 | |
| 136 | mutex_lock(&vcpu->kvm->lock); |
| 137 | ret = kvm_read_guest_page(vcpu->kvm, pdpt_gfn, pdpte, |
| 138 | offset * sizeof(u64), sizeof(pdpte)); |
| 139 | if (ret < 0) { |
| 140 | ret = 0; |
| 141 | goto out; |
| 142 | } |
| 143 | for (i = 0; i < ARRAY_SIZE(pdpte); ++i) { |
| 144 | if ((pdpte[i] & 1) && (pdpte[i] & 0xfffffff0000001e6ull)) { |
| 145 | ret = 0; |
| 146 | goto out; |
| 147 | } |
| 148 | } |
| 149 | ret = 1; |
| 150 | |
| 151 | memcpy(vcpu->pdptrs, pdpte, sizeof(vcpu->pdptrs)); |
| 152 | out: |
| 153 | mutex_unlock(&vcpu->kvm->lock); |
| 154 | |
| 155 | return ret; |
| 156 | } |
| 157 | |
| 158 | void set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0) |
| 159 | { |
| 160 | if (cr0 & CR0_RESERVED_BITS) { |
| 161 | printk(KERN_DEBUG "set_cr0: 0x%lx #GP, reserved bits 0x%lx\n", |
| 162 | cr0, vcpu->cr0); |
| 163 | inject_gp(vcpu); |
| 164 | return; |
| 165 | } |
| 166 | |
| 167 | if ((cr0 & X86_CR0_NW) && !(cr0 & X86_CR0_CD)) { |
| 168 | printk(KERN_DEBUG "set_cr0: #GP, CD == 0 && NW == 1\n"); |
| 169 | inject_gp(vcpu); |
| 170 | return; |
| 171 | } |
| 172 | |
| 173 | if ((cr0 & X86_CR0_PG) && !(cr0 & X86_CR0_PE)) { |
| 174 | printk(KERN_DEBUG "set_cr0: #GP, set PG flag " |
| 175 | "and a clear PE flag\n"); |
| 176 | inject_gp(vcpu); |
| 177 | return; |
| 178 | } |
| 179 | |
| 180 | if (!is_paging(vcpu) && (cr0 & X86_CR0_PG)) { |
| 181 | #ifdef CONFIG_X86_64 |
| 182 | if ((vcpu->shadow_efer & EFER_LME)) { |
| 183 | int cs_db, cs_l; |
| 184 | |
| 185 | if (!is_pae(vcpu)) { |
| 186 | printk(KERN_DEBUG "set_cr0: #GP, start paging " |
| 187 | "in long mode while PAE is disabled\n"); |
| 188 | inject_gp(vcpu); |
| 189 | return; |
| 190 | } |
| 191 | kvm_x86_ops->get_cs_db_l_bits(vcpu, &cs_db, &cs_l); |
| 192 | if (cs_l) { |
| 193 | printk(KERN_DEBUG "set_cr0: #GP, start paging " |
| 194 | "in long mode while CS.L == 1\n"); |
| 195 | inject_gp(vcpu); |
| 196 | return; |
| 197 | |
| 198 | } |
| 199 | } else |
| 200 | #endif |
| 201 | if (is_pae(vcpu) && !load_pdptrs(vcpu, vcpu->cr3)) { |
| 202 | printk(KERN_DEBUG "set_cr0: #GP, pdptrs " |
| 203 | "reserved bits\n"); |
| 204 | inject_gp(vcpu); |
| 205 | return; |
| 206 | } |
| 207 | |
| 208 | } |
| 209 | |
| 210 | kvm_x86_ops->set_cr0(vcpu, cr0); |
| 211 | vcpu->cr0 = cr0; |
| 212 | |
| 213 | mutex_lock(&vcpu->kvm->lock); |
| 214 | kvm_mmu_reset_context(vcpu); |
| 215 | mutex_unlock(&vcpu->kvm->lock); |
| 216 | return; |
| 217 | } |
| 218 | EXPORT_SYMBOL_GPL(set_cr0); |
| 219 | |
| 220 | void lmsw(struct kvm_vcpu *vcpu, unsigned long msw) |
| 221 | { |
| 222 | set_cr0(vcpu, (vcpu->cr0 & ~0x0ful) | (msw & 0x0f)); |
| 223 | } |
| 224 | EXPORT_SYMBOL_GPL(lmsw); |
| 225 | |
| 226 | void set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4) |
| 227 | { |
| 228 | if (cr4 & CR4_RESERVED_BITS) { |
| 229 | printk(KERN_DEBUG "set_cr4: #GP, reserved bits\n"); |
| 230 | inject_gp(vcpu); |
| 231 | return; |
| 232 | } |
| 233 | |
| 234 | if (is_long_mode(vcpu)) { |
| 235 | if (!(cr4 & X86_CR4_PAE)) { |
| 236 | printk(KERN_DEBUG "set_cr4: #GP, clearing PAE while " |
| 237 | "in long mode\n"); |
| 238 | inject_gp(vcpu); |
| 239 | return; |
| 240 | } |
| 241 | } else if (is_paging(vcpu) && !is_pae(vcpu) && (cr4 & X86_CR4_PAE) |
| 242 | && !load_pdptrs(vcpu, vcpu->cr3)) { |
| 243 | printk(KERN_DEBUG "set_cr4: #GP, pdptrs reserved bits\n"); |
| 244 | inject_gp(vcpu); |
| 245 | return; |
| 246 | } |
| 247 | |
| 248 | if (cr4 & X86_CR4_VMXE) { |
| 249 | printk(KERN_DEBUG "set_cr4: #GP, setting VMXE\n"); |
| 250 | inject_gp(vcpu); |
| 251 | return; |
| 252 | } |
| 253 | kvm_x86_ops->set_cr4(vcpu, cr4); |
| 254 | vcpu->cr4 = cr4; |
| 255 | mutex_lock(&vcpu->kvm->lock); |
| 256 | kvm_mmu_reset_context(vcpu); |
| 257 | mutex_unlock(&vcpu->kvm->lock); |
| 258 | } |
| 259 | EXPORT_SYMBOL_GPL(set_cr4); |
| 260 | |
| 261 | void set_cr3(struct kvm_vcpu *vcpu, unsigned long cr3) |
| 262 | { |
| 263 | if (is_long_mode(vcpu)) { |
| 264 | if (cr3 & CR3_L_MODE_RESERVED_BITS) { |
| 265 | printk(KERN_DEBUG "set_cr3: #GP, reserved bits\n"); |
| 266 | inject_gp(vcpu); |
| 267 | return; |
| 268 | } |
| 269 | } else { |
| 270 | if (is_pae(vcpu)) { |
| 271 | if (cr3 & CR3_PAE_RESERVED_BITS) { |
| 272 | printk(KERN_DEBUG |
| 273 | "set_cr3: #GP, reserved bits\n"); |
| 274 | inject_gp(vcpu); |
| 275 | return; |
| 276 | } |
| 277 | if (is_paging(vcpu) && !load_pdptrs(vcpu, cr3)) { |
| 278 | printk(KERN_DEBUG "set_cr3: #GP, pdptrs " |
| 279 | "reserved bits\n"); |
| 280 | inject_gp(vcpu); |
| 281 | return; |
| 282 | } |
| 283 | } |
| 284 | /* |
| 285 | * We don't check reserved bits in nonpae mode, because |
| 286 | * this isn't enforced, and VMware depends on this. |
| 287 | */ |
| 288 | } |
| 289 | |
| 290 | mutex_lock(&vcpu->kvm->lock); |
| 291 | /* |
| 292 | * Does the new cr3 value map to physical memory? (Note, we |
| 293 | * catch an invalid cr3 even in real-mode, because it would |
| 294 | * cause trouble later on when we turn on paging anyway.) |
| 295 | * |
| 296 | * A real CPU would silently accept an invalid cr3 and would |
| 297 | * attempt to use it - with largely undefined (and often hard |
| 298 | * to debug) behavior on the guest side. |
| 299 | */ |
| 300 | if (unlikely(!gfn_to_memslot(vcpu->kvm, cr3 >> PAGE_SHIFT))) |
| 301 | inject_gp(vcpu); |
| 302 | else { |
| 303 | vcpu->cr3 = cr3; |
| 304 | vcpu->mmu.new_cr3(vcpu); |
| 305 | } |
| 306 | mutex_unlock(&vcpu->kvm->lock); |
| 307 | } |
| 308 | EXPORT_SYMBOL_GPL(set_cr3); |
| 309 | |
| 310 | void set_cr8(struct kvm_vcpu *vcpu, unsigned long cr8) |
| 311 | { |
| 312 | if (cr8 & CR8_RESERVED_BITS) { |
| 313 | printk(KERN_DEBUG "set_cr8: #GP, reserved bits 0x%lx\n", cr8); |
| 314 | inject_gp(vcpu); |
| 315 | return; |
| 316 | } |
| 317 | if (irqchip_in_kernel(vcpu->kvm)) |
| 318 | kvm_lapic_set_tpr(vcpu, cr8); |
| 319 | else |
| 320 | vcpu->cr8 = cr8; |
| 321 | } |
| 322 | EXPORT_SYMBOL_GPL(set_cr8); |
| 323 | |
| 324 | unsigned long get_cr8(struct kvm_vcpu *vcpu) |
| 325 | { |
| 326 | if (irqchip_in_kernel(vcpu->kvm)) |
| 327 | return kvm_lapic_get_cr8(vcpu); |
| 328 | else |
| 329 | return vcpu->cr8; |
| 330 | } |
| 331 | EXPORT_SYMBOL_GPL(get_cr8); |
| 332 | |
Carsten Otte | 043405e | 2007-10-10 17:16:19 +0200 | [diff] [blame] | 333 | /* |
| 334 | * List of msr numbers which we expose to userspace through KVM_GET_MSRS |
| 335 | * and KVM_SET_MSRS, and KVM_GET_MSR_INDEX_LIST. |
| 336 | * |
| 337 | * This list is modified at module load time to reflect the |
| 338 | * capabilities of the host cpu. |
| 339 | */ |
| 340 | static u32 msrs_to_save[] = { |
| 341 | MSR_IA32_SYSENTER_CS, MSR_IA32_SYSENTER_ESP, MSR_IA32_SYSENTER_EIP, |
| 342 | MSR_K6_STAR, |
| 343 | #ifdef CONFIG_X86_64 |
| 344 | MSR_CSTAR, MSR_KERNEL_GS_BASE, MSR_SYSCALL_MASK, MSR_LSTAR, |
| 345 | #endif |
| 346 | MSR_IA32_TIME_STAMP_COUNTER, |
| 347 | }; |
| 348 | |
| 349 | static unsigned num_msrs_to_save; |
| 350 | |
| 351 | static u32 emulated_msrs[] = { |
| 352 | MSR_IA32_MISC_ENABLE, |
| 353 | }; |
| 354 | |
Carsten Otte | 15c4a64 | 2007-10-30 18:44:17 +0100 | [diff] [blame] | 355 | #ifdef CONFIG_X86_64 |
| 356 | |
| 357 | static void set_efer(struct kvm_vcpu *vcpu, u64 efer) |
| 358 | { |
| 359 | if (efer & EFER_RESERVED_BITS) { |
| 360 | printk(KERN_DEBUG "set_efer: 0x%llx #GP, reserved bits\n", |
| 361 | efer); |
| 362 | inject_gp(vcpu); |
| 363 | return; |
| 364 | } |
| 365 | |
| 366 | if (is_paging(vcpu) |
| 367 | && (vcpu->shadow_efer & EFER_LME) != (efer & EFER_LME)) { |
| 368 | printk(KERN_DEBUG "set_efer: #GP, change LME while paging\n"); |
| 369 | inject_gp(vcpu); |
| 370 | return; |
| 371 | } |
| 372 | |
| 373 | kvm_x86_ops->set_efer(vcpu, efer); |
| 374 | |
| 375 | efer &= ~EFER_LMA; |
| 376 | efer |= vcpu->shadow_efer & EFER_LMA; |
| 377 | |
| 378 | vcpu->shadow_efer = efer; |
| 379 | } |
| 380 | |
| 381 | #endif |
| 382 | |
| 383 | /* |
| 384 | * Writes msr value into into the appropriate "register". |
| 385 | * Returns 0 on success, non-0 otherwise. |
| 386 | * Assumes vcpu_load() was already called. |
| 387 | */ |
| 388 | int kvm_set_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 data) |
| 389 | { |
| 390 | return kvm_x86_ops->set_msr(vcpu, msr_index, data); |
| 391 | } |
| 392 | |
Carsten Otte | 313a3dc | 2007-10-11 19:16:52 +0200 | [diff] [blame] | 393 | /* |
| 394 | * Adapt set_msr() to msr_io()'s calling convention |
| 395 | */ |
| 396 | static int do_set_msr(struct kvm_vcpu *vcpu, unsigned index, u64 *data) |
| 397 | { |
| 398 | return kvm_set_msr(vcpu, index, *data); |
| 399 | } |
| 400 | |
Carsten Otte | 15c4a64 | 2007-10-30 18:44:17 +0100 | [diff] [blame] | 401 | |
| 402 | int kvm_set_msr_common(struct kvm_vcpu *vcpu, u32 msr, u64 data) |
| 403 | { |
| 404 | switch (msr) { |
| 405 | #ifdef CONFIG_X86_64 |
| 406 | case MSR_EFER: |
| 407 | set_efer(vcpu, data); |
| 408 | break; |
| 409 | #endif |
| 410 | case MSR_IA32_MC0_STATUS: |
| 411 | pr_unimpl(vcpu, "%s: MSR_IA32_MC0_STATUS 0x%llx, nop\n", |
| 412 | __FUNCTION__, data); |
| 413 | break; |
| 414 | case MSR_IA32_MCG_STATUS: |
| 415 | pr_unimpl(vcpu, "%s: MSR_IA32_MCG_STATUS 0x%llx, nop\n", |
| 416 | __FUNCTION__, data); |
| 417 | break; |
| 418 | case MSR_IA32_UCODE_REV: |
| 419 | case MSR_IA32_UCODE_WRITE: |
| 420 | case 0x200 ... 0x2ff: /* MTRRs */ |
| 421 | break; |
| 422 | case MSR_IA32_APICBASE: |
| 423 | kvm_set_apic_base(vcpu, data); |
| 424 | break; |
| 425 | case MSR_IA32_MISC_ENABLE: |
| 426 | vcpu->ia32_misc_enable_msr = data; |
| 427 | break; |
| 428 | default: |
| 429 | pr_unimpl(vcpu, "unhandled wrmsr: 0x%x\n", msr); |
| 430 | return 1; |
| 431 | } |
| 432 | return 0; |
| 433 | } |
| 434 | EXPORT_SYMBOL_GPL(kvm_set_msr_common); |
| 435 | |
| 436 | |
| 437 | /* |
| 438 | * Reads an msr value (of 'msr_index') into 'pdata'. |
| 439 | * Returns 0 on success, non-0 otherwise. |
| 440 | * Assumes vcpu_load() was already called. |
| 441 | */ |
| 442 | int kvm_get_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 *pdata) |
| 443 | { |
| 444 | return kvm_x86_ops->get_msr(vcpu, msr_index, pdata); |
| 445 | } |
| 446 | |
| 447 | int kvm_get_msr_common(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata) |
| 448 | { |
| 449 | u64 data; |
| 450 | |
| 451 | switch (msr) { |
| 452 | case 0xc0010010: /* SYSCFG */ |
| 453 | case 0xc0010015: /* HWCR */ |
| 454 | case MSR_IA32_PLATFORM_ID: |
| 455 | case MSR_IA32_P5_MC_ADDR: |
| 456 | case MSR_IA32_P5_MC_TYPE: |
| 457 | case MSR_IA32_MC0_CTL: |
| 458 | case MSR_IA32_MCG_STATUS: |
| 459 | case MSR_IA32_MCG_CAP: |
| 460 | case MSR_IA32_MC0_MISC: |
| 461 | case MSR_IA32_MC0_MISC+4: |
| 462 | case MSR_IA32_MC0_MISC+8: |
| 463 | case MSR_IA32_MC0_MISC+12: |
| 464 | case MSR_IA32_MC0_MISC+16: |
| 465 | case MSR_IA32_UCODE_REV: |
| 466 | case MSR_IA32_PERF_STATUS: |
| 467 | case MSR_IA32_EBL_CR_POWERON: |
| 468 | /* MTRR registers */ |
| 469 | case 0xfe: |
| 470 | case 0x200 ... 0x2ff: |
| 471 | data = 0; |
| 472 | break; |
| 473 | case 0xcd: /* fsb frequency */ |
| 474 | data = 3; |
| 475 | break; |
| 476 | case MSR_IA32_APICBASE: |
| 477 | data = kvm_get_apic_base(vcpu); |
| 478 | break; |
| 479 | case MSR_IA32_MISC_ENABLE: |
| 480 | data = vcpu->ia32_misc_enable_msr; |
| 481 | break; |
| 482 | #ifdef CONFIG_X86_64 |
| 483 | case MSR_EFER: |
| 484 | data = vcpu->shadow_efer; |
| 485 | break; |
| 486 | #endif |
| 487 | default: |
| 488 | pr_unimpl(vcpu, "unhandled rdmsr: 0x%x\n", msr); |
| 489 | return 1; |
| 490 | } |
| 491 | *pdata = data; |
| 492 | return 0; |
| 493 | } |
| 494 | EXPORT_SYMBOL_GPL(kvm_get_msr_common); |
| 495 | |
Carsten Otte | 313a3dc | 2007-10-11 19:16:52 +0200 | [diff] [blame] | 496 | /* |
| 497 | * Read or write a bunch of msrs. All parameters are kernel addresses. |
| 498 | * |
| 499 | * @return number of msrs set successfully. |
| 500 | */ |
| 501 | static int __msr_io(struct kvm_vcpu *vcpu, struct kvm_msrs *msrs, |
| 502 | struct kvm_msr_entry *entries, |
| 503 | int (*do_msr)(struct kvm_vcpu *vcpu, |
| 504 | unsigned index, u64 *data)) |
| 505 | { |
| 506 | int i; |
| 507 | |
| 508 | vcpu_load(vcpu); |
| 509 | |
| 510 | for (i = 0; i < msrs->nmsrs; ++i) |
| 511 | if (do_msr(vcpu, entries[i].index, &entries[i].data)) |
| 512 | break; |
| 513 | |
| 514 | vcpu_put(vcpu); |
| 515 | |
| 516 | return i; |
| 517 | } |
| 518 | |
| 519 | /* |
| 520 | * Read or write a bunch of msrs. Parameters are user addresses. |
| 521 | * |
| 522 | * @return number of msrs set successfully. |
| 523 | */ |
| 524 | static int msr_io(struct kvm_vcpu *vcpu, struct kvm_msrs __user *user_msrs, |
| 525 | int (*do_msr)(struct kvm_vcpu *vcpu, |
| 526 | unsigned index, u64 *data), |
| 527 | int writeback) |
| 528 | { |
| 529 | struct kvm_msrs msrs; |
| 530 | struct kvm_msr_entry *entries; |
| 531 | int r, n; |
| 532 | unsigned size; |
| 533 | |
| 534 | r = -EFAULT; |
| 535 | if (copy_from_user(&msrs, user_msrs, sizeof msrs)) |
| 536 | goto out; |
| 537 | |
| 538 | r = -E2BIG; |
| 539 | if (msrs.nmsrs >= MAX_IO_MSRS) |
| 540 | goto out; |
| 541 | |
| 542 | r = -ENOMEM; |
| 543 | size = sizeof(struct kvm_msr_entry) * msrs.nmsrs; |
| 544 | entries = vmalloc(size); |
| 545 | if (!entries) |
| 546 | goto out; |
| 547 | |
| 548 | r = -EFAULT; |
| 549 | if (copy_from_user(entries, user_msrs->entries, size)) |
| 550 | goto out_free; |
| 551 | |
| 552 | r = n = __msr_io(vcpu, &msrs, entries, do_msr); |
| 553 | if (r < 0) |
| 554 | goto out_free; |
| 555 | |
| 556 | r = -EFAULT; |
| 557 | if (writeback && copy_to_user(user_msrs->entries, entries, size)) |
| 558 | goto out_free; |
| 559 | |
| 560 | r = n; |
| 561 | |
| 562 | out_free: |
| 563 | vfree(entries); |
| 564 | out: |
| 565 | return r; |
| 566 | } |
| 567 | |
Zhang Xiantao | e9b11c1 | 2007-11-14 20:38:21 +0800 | [diff] [blame] | 568 | /* |
| 569 | * Make sure that a cpu that is being hot-unplugged does not have any vcpus |
| 570 | * cached on it. |
| 571 | */ |
| 572 | void decache_vcpus_on_cpu(int cpu) |
| 573 | { |
| 574 | struct kvm *vm; |
| 575 | struct kvm_vcpu *vcpu; |
| 576 | int i; |
| 577 | |
| 578 | spin_lock(&kvm_lock); |
| 579 | list_for_each_entry(vm, &vm_list, vm_list) |
| 580 | for (i = 0; i < KVM_MAX_VCPUS; ++i) { |
| 581 | vcpu = vm->vcpus[i]; |
| 582 | if (!vcpu) |
| 583 | continue; |
| 584 | /* |
| 585 | * If the vcpu is locked, then it is running on some |
| 586 | * other cpu and therefore it is not cached on the |
| 587 | * cpu in question. |
| 588 | * |
| 589 | * If it's not locked, check the last cpu it executed |
| 590 | * on. |
| 591 | */ |
| 592 | if (mutex_trylock(&vcpu->mutex)) { |
| 593 | if (vcpu->cpu == cpu) { |
| 594 | kvm_x86_ops->vcpu_decache(vcpu); |
| 595 | vcpu->cpu = -1; |
| 596 | } |
| 597 | mutex_unlock(&vcpu->mutex); |
| 598 | } |
| 599 | } |
| 600 | spin_unlock(&kvm_lock); |
| 601 | } |
| 602 | |
Zhang Xiantao | 018d00d | 2007-11-15 23:07:47 +0800 | [diff] [blame] | 603 | int kvm_dev_ioctl_check_extension(long ext) |
| 604 | { |
| 605 | int r; |
| 606 | |
| 607 | switch (ext) { |
| 608 | case KVM_CAP_IRQCHIP: |
| 609 | case KVM_CAP_HLT: |
| 610 | case KVM_CAP_MMU_SHADOW_CACHE_CONTROL: |
| 611 | case KVM_CAP_USER_MEMORY: |
| 612 | case KVM_CAP_SET_TSS_ADDR: |
| 613 | r = 1; |
| 614 | break; |
| 615 | default: |
| 616 | r = 0; |
| 617 | break; |
| 618 | } |
| 619 | return r; |
| 620 | |
| 621 | } |
| 622 | |
Carsten Otte | 043405e | 2007-10-10 17:16:19 +0200 | [diff] [blame] | 623 | long kvm_arch_dev_ioctl(struct file *filp, |
| 624 | unsigned int ioctl, unsigned long arg) |
| 625 | { |
| 626 | void __user *argp = (void __user *)arg; |
| 627 | long r; |
| 628 | |
| 629 | switch (ioctl) { |
| 630 | case KVM_GET_MSR_INDEX_LIST: { |
| 631 | struct kvm_msr_list __user *user_msr_list = argp; |
| 632 | struct kvm_msr_list msr_list; |
| 633 | unsigned n; |
| 634 | |
| 635 | r = -EFAULT; |
| 636 | if (copy_from_user(&msr_list, user_msr_list, sizeof msr_list)) |
| 637 | goto out; |
| 638 | n = msr_list.nmsrs; |
| 639 | msr_list.nmsrs = num_msrs_to_save + ARRAY_SIZE(emulated_msrs); |
| 640 | if (copy_to_user(user_msr_list, &msr_list, sizeof msr_list)) |
| 641 | goto out; |
| 642 | r = -E2BIG; |
| 643 | if (n < num_msrs_to_save) |
| 644 | goto out; |
| 645 | r = -EFAULT; |
| 646 | if (copy_to_user(user_msr_list->indices, &msrs_to_save, |
| 647 | num_msrs_to_save * sizeof(u32))) |
| 648 | goto out; |
| 649 | if (copy_to_user(user_msr_list->indices |
| 650 | + num_msrs_to_save * sizeof(u32), |
| 651 | &emulated_msrs, |
| 652 | ARRAY_SIZE(emulated_msrs) * sizeof(u32))) |
| 653 | goto out; |
| 654 | r = 0; |
| 655 | break; |
| 656 | } |
| 657 | default: |
| 658 | r = -EINVAL; |
| 659 | } |
| 660 | out: |
| 661 | return r; |
| 662 | } |
| 663 | |
Carsten Otte | 313a3dc | 2007-10-11 19:16:52 +0200 | [diff] [blame] | 664 | void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu) |
| 665 | { |
| 666 | kvm_x86_ops->vcpu_load(vcpu, cpu); |
| 667 | } |
| 668 | |
| 669 | void kvm_arch_vcpu_put(struct kvm_vcpu *vcpu) |
| 670 | { |
| 671 | kvm_x86_ops->vcpu_put(vcpu); |
| 672 | } |
| 673 | |
| 674 | static void cpuid_fix_nx_cap(struct kvm_vcpu *vcpu) |
| 675 | { |
| 676 | u64 efer; |
| 677 | int i; |
| 678 | struct kvm_cpuid_entry *e, *entry; |
| 679 | |
| 680 | rdmsrl(MSR_EFER, efer); |
| 681 | entry = NULL; |
| 682 | for (i = 0; i < vcpu->cpuid_nent; ++i) { |
| 683 | e = &vcpu->cpuid_entries[i]; |
| 684 | if (e->function == 0x80000001) { |
| 685 | entry = e; |
| 686 | break; |
| 687 | } |
| 688 | } |
| 689 | if (entry && (entry->edx & (1 << 20)) && !(efer & EFER_NX)) { |
| 690 | entry->edx &= ~(1 << 20); |
| 691 | printk(KERN_INFO "kvm: guest NX capability removed\n"); |
| 692 | } |
| 693 | } |
| 694 | |
| 695 | static int kvm_vcpu_ioctl_set_cpuid(struct kvm_vcpu *vcpu, |
| 696 | struct kvm_cpuid *cpuid, |
| 697 | struct kvm_cpuid_entry __user *entries) |
| 698 | { |
| 699 | int r; |
| 700 | |
| 701 | r = -E2BIG; |
| 702 | if (cpuid->nent > KVM_MAX_CPUID_ENTRIES) |
| 703 | goto out; |
| 704 | r = -EFAULT; |
| 705 | if (copy_from_user(&vcpu->cpuid_entries, entries, |
| 706 | cpuid->nent * sizeof(struct kvm_cpuid_entry))) |
| 707 | goto out; |
| 708 | vcpu->cpuid_nent = cpuid->nent; |
| 709 | cpuid_fix_nx_cap(vcpu); |
| 710 | return 0; |
| 711 | |
| 712 | out: |
| 713 | return r; |
| 714 | } |
| 715 | |
| 716 | static int kvm_vcpu_ioctl_get_lapic(struct kvm_vcpu *vcpu, |
| 717 | struct kvm_lapic_state *s) |
| 718 | { |
| 719 | vcpu_load(vcpu); |
| 720 | memcpy(s->regs, vcpu->apic->regs, sizeof *s); |
| 721 | vcpu_put(vcpu); |
| 722 | |
| 723 | return 0; |
| 724 | } |
| 725 | |
| 726 | static int kvm_vcpu_ioctl_set_lapic(struct kvm_vcpu *vcpu, |
| 727 | struct kvm_lapic_state *s) |
| 728 | { |
| 729 | vcpu_load(vcpu); |
| 730 | memcpy(vcpu->apic->regs, s->regs, sizeof *s); |
| 731 | kvm_apic_post_state_restore(vcpu); |
| 732 | vcpu_put(vcpu); |
| 733 | |
| 734 | return 0; |
| 735 | } |
| 736 | |
| 737 | long kvm_arch_vcpu_ioctl(struct file *filp, |
| 738 | unsigned int ioctl, unsigned long arg) |
| 739 | { |
| 740 | struct kvm_vcpu *vcpu = filp->private_data; |
| 741 | void __user *argp = (void __user *)arg; |
| 742 | int r; |
| 743 | |
| 744 | switch (ioctl) { |
| 745 | case KVM_GET_LAPIC: { |
| 746 | struct kvm_lapic_state lapic; |
| 747 | |
| 748 | memset(&lapic, 0, sizeof lapic); |
| 749 | r = kvm_vcpu_ioctl_get_lapic(vcpu, &lapic); |
| 750 | if (r) |
| 751 | goto out; |
| 752 | r = -EFAULT; |
| 753 | if (copy_to_user(argp, &lapic, sizeof lapic)) |
| 754 | goto out; |
| 755 | r = 0; |
| 756 | break; |
| 757 | } |
| 758 | case KVM_SET_LAPIC: { |
| 759 | struct kvm_lapic_state lapic; |
| 760 | |
| 761 | r = -EFAULT; |
| 762 | if (copy_from_user(&lapic, argp, sizeof lapic)) |
| 763 | goto out; |
| 764 | r = kvm_vcpu_ioctl_set_lapic(vcpu, &lapic);; |
| 765 | if (r) |
| 766 | goto out; |
| 767 | r = 0; |
| 768 | break; |
| 769 | } |
| 770 | case KVM_SET_CPUID: { |
| 771 | struct kvm_cpuid __user *cpuid_arg = argp; |
| 772 | struct kvm_cpuid cpuid; |
| 773 | |
| 774 | r = -EFAULT; |
| 775 | if (copy_from_user(&cpuid, cpuid_arg, sizeof cpuid)) |
| 776 | goto out; |
| 777 | r = kvm_vcpu_ioctl_set_cpuid(vcpu, &cpuid, cpuid_arg->entries); |
| 778 | if (r) |
| 779 | goto out; |
| 780 | break; |
| 781 | } |
| 782 | case KVM_GET_MSRS: |
| 783 | r = msr_io(vcpu, argp, kvm_get_msr, 1); |
| 784 | break; |
| 785 | case KVM_SET_MSRS: |
| 786 | r = msr_io(vcpu, argp, do_set_msr, 0); |
| 787 | break; |
| 788 | default: |
| 789 | r = -EINVAL; |
| 790 | } |
| 791 | out: |
| 792 | return r; |
| 793 | } |
| 794 | |
Carsten Otte | 1fe779f | 2007-10-29 16:08:35 +0100 | [diff] [blame] | 795 | static int kvm_vm_ioctl_set_tss_addr(struct kvm *kvm, unsigned long addr) |
| 796 | { |
| 797 | int ret; |
| 798 | |
| 799 | if (addr > (unsigned int)(-3 * PAGE_SIZE)) |
| 800 | return -1; |
| 801 | ret = kvm_x86_ops->set_tss_addr(kvm, addr); |
| 802 | return ret; |
| 803 | } |
| 804 | |
| 805 | static int kvm_vm_ioctl_set_nr_mmu_pages(struct kvm *kvm, |
| 806 | u32 kvm_nr_mmu_pages) |
| 807 | { |
| 808 | if (kvm_nr_mmu_pages < KVM_MIN_ALLOC_MMU_PAGES) |
| 809 | return -EINVAL; |
| 810 | |
| 811 | mutex_lock(&kvm->lock); |
| 812 | |
| 813 | kvm_mmu_change_mmu_pages(kvm, kvm_nr_mmu_pages); |
| 814 | kvm->n_requested_mmu_pages = kvm_nr_mmu_pages; |
| 815 | |
| 816 | mutex_unlock(&kvm->lock); |
| 817 | return 0; |
| 818 | } |
| 819 | |
| 820 | static int kvm_vm_ioctl_get_nr_mmu_pages(struct kvm *kvm) |
| 821 | { |
| 822 | return kvm->n_alloc_mmu_pages; |
| 823 | } |
| 824 | |
| 825 | /* |
| 826 | * Set a new alias region. Aliases map a portion of physical memory into |
| 827 | * another portion. This is useful for memory windows, for example the PC |
| 828 | * VGA region. |
| 829 | */ |
| 830 | static int kvm_vm_ioctl_set_memory_alias(struct kvm *kvm, |
| 831 | struct kvm_memory_alias *alias) |
| 832 | { |
| 833 | int r, n; |
| 834 | struct kvm_mem_alias *p; |
| 835 | |
| 836 | r = -EINVAL; |
| 837 | /* General sanity checks */ |
| 838 | if (alias->memory_size & (PAGE_SIZE - 1)) |
| 839 | goto out; |
| 840 | if (alias->guest_phys_addr & (PAGE_SIZE - 1)) |
| 841 | goto out; |
| 842 | if (alias->slot >= KVM_ALIAS_SLOTS) |
| 843 | goto out; |
| 844 | if (alias->guest_phys_addr + alias->memory_size |
| 845 | < alias->guest_phys_addr) |
| 846 | goto out; |
| 847 | if (alias->target_phys_addr + alias->memory_size |
| 848 | < alias->target_phys_addr) |
| 849 | goto out; |
| 850 | |
| 851 | mutex_lock(&kvm->lock); |
| 852 | |
| 853 | p = &kvm->aliases[alias->slot]; |
| 854 | p->base_gfn = alias->guest_phys_addr >> PAGE_SHIFT; |
| 855 | p->npages = alias->memory_size >> PAGE_SHIFT; |
| 856 | p->target_gfn = alias->target_phys_addr >> PAGE_SHIFT; |
| 857 | |
| 858 | for (n = KVM_ALIAS_SLOTS; n > 0; --n) |
| 859 | if (kvm->aliases[n - 1].npages) |
| 860 | break; |
| 861 | kvm->naliases = n; |
| 862 | |
| 863 | kvm_mmu_zap_all(kvm); |
| 864 | |
| 865 | mutex_unlock(&kvm->lock); |
| 866 | |
| 867 | return 0; |
| 868 | |
| 869 | out: |
| 870 | return r; |
| 871 | } |
| 872 | |
| 873 | static int kvm_vm_ioctl_get_irqchip(struct kvm *kvm, struct kvm_irqchip *chip) |
| 874 | { |
| 875 | int r; |
| 876 | |
| 877 | r = 0; |
| 878 | switch (chip->chip_id) { |
| 879 | case KVM_IRQCHIP_PIC_MASTER: |
| 880 | memcpy(&chip->chip.pic, |
| 881 | &pic_irqchip(kvm)->pics[0], |
| 882 | sizeof(struct kvm_pic_state)); |
| 883 | break; |
| 884 | case KVM_IRQCHIP_PIC_SLAVE: |
| 885 | memcpy(&chip->chip.pic, |
| 886 | &pic_irqchip(kvm)->pics[1], |
| 887 | sizeof(struct kvm_pic_state)); |
| 888 | break; |
| 889 | case KVM_IRQCHIP_IOAPIC: |
| 890 | memcpy(&chip->chip.ioapic, |
| 891 | ioapic_irqchip(kvm), |
| 892 | sizeof(struct kvm_ioapic_state)); |
| 893 | break; |
| 894 | default: |
| 895 | r = -EINVAL; |
| 896 | break; |
| 897 | } |
| 898 | return r; |
| 899 | } |
| 900 | |
| 901 | static int kvm_vm_ioctl_set_irqchip(struct kvm *kvm, struct kvm_irqchip *chip) |
| 902 | { |
| 903 | int r; |
| 904 | |
| 905 | r = 0; |
| 906 | switch (chip->chip_id) { |
| 907 | case KVM_IRQCHIP_PIC_MASTER: |
| 908 | memcpy(&pic_irqchip(kvm)->pics[0], |
| 909 | &chip->chip.pic, |
| 910 | sizeof(struct kvm_pic_state)); |
| 911 | break; |
| 912 | case KVM_IRQCHIP_PIC_SLAVE: |
| 913 | memcpy(&pic_irqchip(kvm)->pics[1], |
| 914 | &chip->chip.pic, |
| 915 | sizeof(struct kvm_pic_state)); |
| 916 | break; |
| 917 | case KVM_IRQCHIP_IOAPIC: |
| 918 | memcpy(ioapic_irqchip(kvm), |
| 919 | &chip->chip.ioapic, |
| 920 | sizeof(struct kvm_ioapic_state)); |
| 921 | break; |
| 922 | default: |
| 923 | r = -EINVAL; |
| 924 | break; |
| 925 | } |
| 926 | kvm_pic_update_irq(pic_irqchip(kvm)); |
| 927 | return r; |
| 928 | } |
| 929 | |
| 930 | long kvm_arch_vm_ioctl(struct file *filp, |
| 931 | unsigned int ioctl, unsigned long arg) |
| 932 | { |
| 933 | struct kvm *kvm = filp->private_data; |
| 934 | void __user *argp = (void __user *)arg; |
| 935 | int r = -EINVAL; |
| 936 | |
| 937 | switch (ioctl) { |
| 938 | case KVM_SET_TSS_ADDR: |
| 939 | r = kvm_vm_ioctl_set_tss_addr(kvm, arg); |
| 940 | if (r < 0) |
| 941 | goto out; |
| 942 | break; |
| 943 | case KVM_SET_MEMORY_REGION: { |
| 944 | struct kvm_memory_region kvm_mem; |
| 945 | struct kvm_userspace_memory_region kvm_userspace_mem; |
| 946 | |
| 947 | r = -EFAULT; |
| 948 | if (copy_from_user(&kvm_mem, argp, sizeof kvm_mem)) |
| 949 | goto out; |
| 950 | kvm_userspace_mem.slot = kvm_mem.slot; |
| 951 | kvm_userspace_mem.flags = kvm_mem.flags; |
| 952 | kvm_userspace_mem.guest_phys_addr = kvm_mem.guest_phys_addr; |
| 953 | kvm_userspace_mem.memory_size = kvm_mem.memory_size; |
| 954 | r = kvm_vm_ioctl_set_memory_region(kvm, &kvm_userspace_mem, 0); |
| 955 | if (r) |
| 956 | goto out; |
| 957 | break; |
| 958 | } |
| 959 | case KVM_SET_NR_MMU_PAGES: |
| 960 | r = kvm_vm_ioctl_set_nr_mmu_pages(kvm, arg); |
| 961 | if (r) |
| 962 | goto out; |
| 963 | break; |
| 964 | case KVM_GET_NR_MMU_PAGES: |
| 965 | r = kvm_vm_ioctl_get_nr_mmu_pages(kvm); |
| 966 | break; |
| 967 | case KVM_SET_MEMORY_ALIAS: { |
| 968 | struct kvm_memory_alias alias; |
| 969 | |
| 970 | r = -EFAULT; |
| 971 | if (copy_from_user(&alias, argp, sizeof alias)) |
| 972 | goto out; |
| 973 | r = kvm_vm_ioctl_set_memory_alias(kvm, &alias); |
| 974 | if (r) |
| 975 | goto out; |
| 976 | break; |
| 977 | } |
| 978 | case KVM_CREATE_IRQCHIP: |
| 979 | r = -ENOMEM; |
| 980 | kvm->vpic = kvm_create_pic(kvm); |
| 981 | if (kvm->vpic) { |
| 982 | r = kvm_ioapic_init(kvm); |
| 983 | if (r) { |
| 984 | kfree(kvm->vpic); |
| 985 | kvm->vpic = NULL; |
| 986 | goto out; |
| 987 | } |
| 988 | } else |
| 989 | goto out; |
| 990 | break; |
| 991 | case KVM_IRQ_LINE: { |
| 992 | struct kvm_irq_level irq_event; |
| 993 | |
| 994 | r = -EFAULT; |
| 995 | if (copy_from_user(&irq_event, argp, sizeof irq_event)) |
| 996 | goto out; |
| 997 | if (irqchip_in_kernel(kvm)) { |
| 998 | mutex_lock(&kvm->lock); |
| 999 | if (irq_event.irq < 16) |
| 1000 | kvm_pic_set_irq(pic_irqchip(kvm), |
| 1001 | irq_event.irq, |
| 1002 | irq_event.level); |
| 1003 | kvm_ioapic_set_irq(kvm->vioapic, |
| 1004 | irq_event.irq, |
| 1005 | irq_event.level); |
| 1006 | mutex_unlock(&kvm->lock); |
| 1007 | r = 0; |
| 1008 | } |
| 1009 | break; |
| 1010 | } |
| 1011 | case KVM_GET_IRQCHIP: { |
| 1012 | /* 0: PIC master, 1: PIC slave, 2: IOAPIC */ |
| 1013 | struct kvm_irqchip chip; |
| 1014 | |
| 1015 | r = -EFAULT; |
| 1016 | if (copy_from_user(&chip, argp, sizeof chip)) |
| 1017 | goto out; |
| 1018 | r = -ENXIO; |
| 1019 | if (!irqchip_in_kernel(kvm)) |
| 1020 | goto out; |
| 1021 | r = kvm_vm_ioctl_get_irqchip(kvm, &chip); |
| 1022 | if (r) |
| 1023 | goto out; |
| 1024 | r = -EFAULT; |
| 1025 | if (copy_to_user(argp, &chip, sizeof chip)) |
| 1026 | goto out; |
| 1027 | r = 0; |
| 1028 | break; |
| 1029 | } |
| 1030 | case KVM_SET_IRQCHIP: { |
| 1031 | /* 0: PIC master, 1: PIC slave, 2: IOAPIC */ |
| 1032 | struct kvm_irqchip chip; |
| 1033 | |
| 1034 | r = -EFAULT; |
| 1035 | if (copy_from_user(&chip, argp, sizeof chip)) |
| 1036 | goto out; |
| 1037 | r = -ENXIO; |
| 1038 | if (!irqchip_in_kernel(kvm)) |
| 1039 | goto out; |
| 1040 | r = kvm_vm_ioctl_set_irqchip(kvm, &chip); |
| 1041 | if (r) |
| 1042 | goto out; |
| 1043 | r = 0; |
| 1044 | break; |
| 1045 | } |
| 1046 | default: |
| 1047 | ; |
| 1048 | } |
| 1049 | out: |
| 1050 | return r; |
| 1051 | } |
| 1052 | |
Zhang Xiantao | a16b043 | 2007-11-16 14:38:21 +0800 | [diff] [blame] | 1053 | static void kvm_init_msr_list(void) |
Carsten Otte | 043405e | 2007-10-10 17:16:19 +0200 | [diff] [blame] | 1054 | { |
| 1055 | u32 dummy[2]; |
| 1056 | unsigned i, j; |
| 1057 | |
| 1058 | for (i = j = 0; i < ARRAY_SIZE(msrs_to_save); i++) { |
| 1059 | if (rdmsr_safe(msrs_to_save[i], &dummy[0], &dummy[1]) < 0) |
| 1060 | continue; |
| 1061 | if (j < i) |
| 1062 | msrs_to_save[j] = msrs_to_save[i]; |
| 1063 | j++; |
| 1064 | } |
| 1065 | num_msrs_to_save = j; |
| 1066 | } |
| 1067 | |
Carsten Otte | bbd9b64 | 2007-10-30 18:44:21 +0100 | [diff] [blame] | 1068 | /* |
| 1069 | * Only apic need an MMIO device hook, so shortcut now.. |
| 1070 | */ |
| 1071 | static struct kvm_io_device *vcpu_find_pervcpu_dev(struct kvm_vcpu *vcpu, |
| 1072 | gpa_t addr) |
| 1073 | { |
| 1074 | struct kvm_io_device *dev; |
| 1075 | |
| 1076 | if (vcpu->apic) { |
| 1077 | dev = &vcpu->apic->dev; |
| 1078 | if (dev->in_range(dev, addr)) |
| 1079 | return dev; |
| 1080 | } |
| 1081 | return NULL; |
| 1082 | } |
| 1083 | |
| 1084 | |
| 1085 | static struct kvm_io_device *vcpu_find_mmio_dev(struct kvm_vcpu *vcpu, |
| 1086 | gpa_t addr) |
| 1087 | { |
| 1088 | struct kvm_io_device *dev; |
| 1089 | |
| 1090 | dev = vcpu_find_pervcpu_dev(vcpu, addr); |
| 1091 | if (dev == NULL) |
| 1092 | dev = kvm_io_bus_find_dev(&vcpu->kvm->mmio_bus, addr); |
| 1093 | return dev; |
| 1094 | } |
| 1095 | |
| 1096 | int emulator_read_std(unsigned long addr, |
| 1097 | void *val, |
| 1098 | unsigned int bytes, |
| 1099 | struct kvm_vcpu *vcpu) |
| 1100 | { |
| 1101 | void *data = val; |
| 1102 | |
| 1103 | while (bytes) { |
| 1104 | gpa_t gpa = vcpu->mmu.gva_to_gpa(vcpu, addr); |
| 1105 | unsigned offset = addr & (PAGE_SIZE-1); |
| 1106 | unsigned tocopy = min(bytes, (unsigned)PAGE_SIZE - offset); |
| 1107 | int ret; |
| 1108 | |
| 1109 | if (gpa == UNMAPPED_GVA) |
| 1110 | return X86EMUL_PROPAGATE_FAULT; |
| 1111 | ret = kvm_read_guest(vcpu->kvm, gpa, data, tocopy); |
| 1112 | if (ret < 0) |
| 1113 | return X86EMUL_UNHANDLEABLE; |
| 1114 | |
| 1115 | bytes -= tocopy; |
| 1116 | data += tocopy; |
| 1117 | addr += tocopy; |
| 1118 | } |
| 1119 | |
| 1120 | return X86EMUL_CONTINUE; |
| 1121 | } |
| 1122 | EXPORT_SYMBOL_GPL(emulator_read_std); |
| 1123 | |
| 1124 | static int emulator_write_std(unsigned long addr, |
| 1125 | const void *val, |
| 1126 | unsigned int bytes, |
| 1127 | struct kvm_vcpu *vcpu) |
| 1128 | { |
| 1129 | pr_unimpl(vcpu, "emulator_write_std: addr %lx n %d\n", addr, bytes); |
| 1130 | return X86EMUL_UNHANDLEABLE; |
| 1131 | } |
| 1132 | |
| 1133 | static int emulator_read_emulated(unsigned long addr, |
| 1134 | void *val, |
| 1135 | unsigned int bytes, |
| 1136 | struct kvm_vcpu *vcpu) |
| 1137 | { |
| 1138 | struct kvm_io_device *mmio_dev; |
| 1139 | gpa_t gpa; |
| 1140 | |
| 1141 | if (vcpu->mmio_read_completed) { |
| 1142 | memcpy(val, vcpu->mmio_data, bytes); |
| 1143 | vcpu->mmio_read_completed = 0; |
| 1144 | return X86EMUL_CONTINUE; |
| 1145 | } |
| 1146 | |
| 1147 | gpa = vcpu->mmu.gva_to_gpa(vcpu, addr); |
| 1148 | |
| 1149 | /* For APIC access vmexit */ |
| 1150 | if ((gpa & PAGE_MASK) == APIC_DEFAULT_PHYS_BASE) |
| 1151 | goto mmio; |
| 1152 | |
| 1153 | if (emulator_read_std(addr, val, bytes, vcpu) |
| 1154 | == X86EMUL_CONTINUE) |
| 1155 | return X86EMUL_CONTINUE; |
| 1156 | if (gpa == UNMAPPED_GVA) |
| 1157 | return X86EMUL_PROPAGATE_FAULT; |
| 1158 | |
| 1159 | mmio: |
| 1160 | /* |
| 1161 | * Is this MMIO handled locally? |
| 1162 | */ |
| 1163 | mmio_dev = vcpu_find_mmio_dev(vcpu, gpa); |
| 1164 | if (mmio_dev) { |
| 1165 | kvm_iodevice_read(mmio_dev, gpa, bytes, val); |
| 1166 | return X86EMUL_CONTINUE; |
| 1167 | } |
| 1168 | |
| 1169 | vcpu->mmio_needed = 1; |
| 1170 | vcpu->mmio_phys_addr = gpa; |
| 1171 | vcpu->mmio_size = bytes; |
| 1172 | vcpu->mmio_is_write = 0; |
| 1173 | |
| 1174 | return X86EMUL_UNHANDLEABLE; |
| 1175 | } |
| 1176 | |
| 1177 | static int emulator_write_phys(struct kvm_vcpu *vcpu, gpa_t gpa, |
| 1178 | const void *val, int bytes) |
| 1179 | { |
| 1180 | int ret; |
| 1181 | |
| 1182 | ret = kvm_write_guest(vcpu->kvm, gpa, val, bytes); |
| 1183 | if (ret < 0) |
| 1184 | return 0; |
| 1185 | kvm_mmu_pte_write(vcpu, gpa, val, bytes); |
| 1186 | return 1; |
| 1187 | } |
| 1188 | |
| 1189 | static int emulator_write_emulated_onepage(unsigned long addr, |
| 1190 | const void *val, |
| 1191 | unsigned int bytes, |
| 1192 | struct kvm_vcpu *vcpu) |
| 1193 | { |
| 1194 | struct kvm_io_device *mmio_dev; |
| 1195 | gpa_t gpa = vcpu->mmu.gva_to_gpa(vcpu, addr); |
| 1196 | |
| 1197 | if (gpa == UNMAPPED_GVA) { |
| 1198 | kvm_x86_ops->inject_page_fault(vcpu, addr, 2); |
| 1199 | return X86EMUL_PROPAGATE_FAULT; |
| 1200 | } |
| 1201 | |
| 1202 | /* For APIC access vmexit */ |
| 1203 | if ((gpa & PAGE_MASK) == APIC_DEFAULT_PHYS_BASE) |
| 1204 | goto mmio; |
| 1205 | |
| 1206 | if (emulator_write_phys(vcpu, gpa, val, bytes)) |
| 1207 | return X86EMUL_CONTINUE; |
| 1208 | |
| 1209 | mmio: |
| 1210 | /* |
| 1211 | * Is this MMIO handled locally? |
| 1212 | */ |
| 1213 | mmio_dev = vcpu_find_mmio_dev(vcpu, gpa); |
| 1214 | if (mmio_dev) { |
| 1215 | kvm_iodevice_write(mmio_dev, gpa, bytes, val); |
| 1216 | return X86EMUL_CONTINUE; |
| 1217 | } |
| 1218 | |
| 1219 | vcpu->mmio_needed = 1; |
| 1220 | vcpu->mmio_phys_addr = gpa; |
| 1221 | vcpu->mmio_size = bytes; |
| 1222 | vcpu->mmio_is_write = 1; |
| 1223 | memcpy(vcpu->mmio_data, val, bytes); |
| 1224 | |
| 1225 | return X86EMUL_CONTINUE; |
| 1226 | } |
| 1227 | |
| 1228 | int emulator_write_emulated(unsigned long addr, |
| 1229 | const void *val, |
| 1230 | unsigned int bytes, |
| 1231 | struct kvm_vcpu *vcpu) |
| 1232 | { |
| 1233 | /* Crossing a page boundary? */ |
| 1234 | if (((addr + bytes - 1) ^ addr) & PAGE_MASK) { |
| 1235 | int rc, now; |
| 1236 | |
| 1237 | now = -addr & ~PAGE_MASK; |
| 1238 | rc = emulator_write_emulated_onepage(addr, val, now, vcpu); |
| 1239 | if (rc != X86EMUL_CONTINUE) |
| 1240 | return rc; |
| 1241 | addr += now; |
| 1242 | val += now; |
| 1243 | bytes -= now; |
| 1244 | } |
| 1245 | return emulator_write_emulated_onepage(addr, val, bytes, vcpu); |
| 1246 | } |
| 1247 | EXPORT_SYMBOL_GPL(emulator_write_emulated); |
| 1248 | |
| 1249 | static int emulator_cmpxchg_emulated(unsigned long addr, |
| 1250 | const void *old, |
| 1251 | const void *new, |
| 1252 | unsigned int bytes, |
| 1253 | struct kvm_vcpu *vcpu) |
| 1254 | { |
| 1255 | static int reported; |
| 1256 | |
| 1257 | if (!reported) { |
| 1258 | reported = 1; |
| 1259 | printk(KERN_WARNING "kvm: emulating exchange as write\n"); |
| 1260 | } |
| 1261 | return emulator_write_emulated(addr, new, bytes, vcpu); |
| 1262 | } |
| 1263 | |
| 1264 | static unsigned long get_segment_base(struct kvm_vcpu *vcpu, int seg) |
| 1265 | { |
| 1266 | return kvm_x86_ops->get_segment_base(vcpu, seg); |
| 1267 | } |
| 1268 | |
| 1269 | int emulate_invlpg(struct kvm_vcpu *vcpu, gva_t address) |
| 1270 | { |
| 1271 | return X86EMUL_CONTINUE; |
| 1272 | } |
| 1273 | |
| 1274 | int emulate_clts(struct kvm_vcpu *vcpu) |
| 1275 | { |
| 1276 | kvm_x86_ops->set_cr0(vcpu, vcpu->cr0 & ~X86_CR0_TS); |
| 1277 | return X86EMUL_CONTINUE; |
| 1278 | } |
| 1279 | |
| 1280 | int emulator_get_dr(struct x86_emulate_ctxt *ctxt, int dr, unsigned long *dest) |
| 1281 | { |
| 1282 | struct kvm_vcpu *vcpu = ctxt->vcpu; |
| 1283 | |
| 1284 | switch (dr) { |
| 1285 | case 0 ... 3: |
| 1286 | *dest = kvm_x86_ops->get_dr(vcpu, dr); |
| 1287 | return X86EMUL_CONTINUE; |
| 1288 | default: |
| 1289 | pr_unimpl(vcpu, "%s: unexpected dr %u\n", __FUNCTION__, dr); |
| 1290 | return X86EMUL_UNHANDLEABLE; |
| 1291 | } |
| 1292 | } |
| 1293 | |
| 1294 | int emulator_set_dr(struct x86_emulate_ctxt *ctxt, int dr, unsigned long value) |
| 1295 | { |
| 1296 | unsigned long mask = (ctxt->mode == X86EMUL_MODE_PROT64) ? ~0ULL : ~0U; |
| 1297 | int exception; |
| 1298 | |
| 1299 | kvm_x86_ops->set_dr(ctxt->vcpu, dr, value & mask, &exception); |
| 1300 | if (exception) { |
| 1301 | /* FIXME: better handling */ |
| 1302 | return X86EMUL_UNHANDLEABLE; |
| 1303 | } |
| 1304 | return X86EMUL_CONTINUE; |
| 1305 | } |
| 1306 | |
| 1307 | void kvm_report_emulation_failure(struct kvm_vcpu *vcpu, const char *context) |
| 1308 | { |
| 1309 | static int reported; |
| 1310 | u8 opcodes[4]; |
| 1311 | unsigned long rip = vcpu->rip; |
| 1312 | unsigned long rip_linear; |
| 1313 | |
| 1314 | rip_linear = rip + get_segment_base(vcpu, VCPU_SREG_CS); |
| 1315 | |
| 1316 | if (reported) |
| 1317 | return; |
| 1318 | |
| 1319 | emulator_read_std(rip_linear, (void *)opcodes, 4, vcpu); |
| 1320 | |
| 1321 | printk(KERN_ERR "emulation failed (%s) rip %lx %02x %02x %02x %02x\n", |
| 1322 | context, rip, opcodes[0], opcodes[1], opcodes[2], opcodes[3]); |
| 1323 | reported = 1; |
| 1324 | } |
| 1325 | EXPORT_SYMBOL_GPL(kvm_report_emulation_failure); |
| 1326 | |
| 1327 | struct x86_emulate_ops emulate_ops = { |
| 1328 | .read_std = emulator_read_std, |
| 1329 | .write_std = emulator_write_std, |
| 1330 | .read_emulated = emulator_read_emulated, |
| 1331 | .write_emulated = emulator_write_emulated, |
| 1332 | .cmpxchg_emulated = emulator_cmpxchg_emulated, |
| 1333 | }; |
| 1334 | |
| 1335 | int emulate_instruction(struct kvm_vcpu *vcpu, |
| 1336 | struct kvm_run *run, |
| 1337 | unsigned long cr2, |
| 1338 | u16 error_code, |
| 1339 | int no_decode) |
| 1340 | { |
| 1341 | int r; |
| 1342 | |
| 1343 | vcpu->mmio_fault_cr2 = cr2; |
| 1344 | kvm_x86_ops->cache_regs(vcpu); |
| 1345 | |
| 1346 | vcpu->mmio_is_write = 0; |
| 1347 | vcpu->pio.string = 0; |
| 1348 | |
| 1349 | if (!no_decode) { |
| 1350 | int cs_db, cs_l; |
| 1351 | kvm_x86_ops->get_cs_db_l_bits(vcpu, &cs_db, &cs_l); |
| 1352 | |
| 1353 | vcpu->emulate_ctxt.vcpu = vcpu; |
| 1354 | vcpu->emulate_ctxt.eflags = kvm_x86_ops->get_rflags(vcpu); |
| 1355 | vcpu->emulate_ctxt.cr2 = cr2; |
| 1356 | vcpu->emulate_ctxt.mode = |
| 1357 | (vcpu->emulate_ctxt.eflags & X86_EFLAGS_VM) |
| 1358 | ? X86EMUL_MODE_REAL : cs_l |
| 1359 | ? X86EMUL_MODE_PROT64 : cs_db |
| 1360 | ? X86EMUL_MODE_PROT32 : X86EMUL_MODE_PROT16; |
| 1361 | |
| 1362 | if (vcpu->emulate_ctxt.mode == X86EMUL_MODE_PROT64) { |
| 1363 | vcpu->emulate_ctxt.cs_base = 0; |
| 1364 | vcpu->emulate_ctxt.ds_base = 0; |
| 1365 | vcpu->emulate_ctxt.es_base = 0; |
| 1366 | vcpu->emulate_ctxt.ss_base = 0; |
| 1367 | } else { |
| 1368 | vcpu->emulate_ctxt.cs_base = |
| 1369 | get_segment_base(vcpu, VCPU_SREG_CS); |
| 1370 | vcpu->emulate_ctxt.ds_base = |
| 1371 | get_segment_base(vcpu, VCPU_SREG_DS); |
| 1372 | vcpu->emulate_ctxt.es_base = |
| 1373 | get_segment_base(vcpu, VCPU_SREG_ES); |
| 1374 | vcpu->emulate_ctxt.ss_base = |
| 1375 | get_segment_base(vcpu, VCPU_SREG_SS); |
| 1376 | } |
| 1377 | |
| 1378 | vcpu->emulate_ctxt.gs_base = |
| 1379 | get_segment_base(vcpu, VCPU_SREG_GS); |
| 1380 | vcpu->emulate_ctxt.fs_base = |
| 1381 | get_segment_base(vcpu, VCPU_SREG_FS); |
| 1382 | |
| 1383 | r = x86_decode_insn(&vcpu->emulate_ctxt, &emulate_ops); |
| 1384 | if (r) { |
| 1385 | if (kvm_mmu_unprotect_page_virt(vcpu, cr2)) |
| 1386 | return EMULATE_DONE; |
| 1387 | return EMULATE_FAIL; |
| 1388 | } |
| 1389 | } |
| 1390 | |
| 1391 | r = x86_emulate_insn(&vcpu->emulate_ctxt, &emulate_ops); |
| 1392 | |
| 1393 | if (vcpu->pio.string) |
| 1394 | return EMULATE_DO_MMIO; |
| 1395 | |
| 1396 | if ((r || vcpu->mmio_is_write) && run) { |
| 1397 | run->exit_reason = KVM_EXIT_MMIO; |
| 1398 | run->mmio.phys_addr = vcpu->mmio_phys_addr; |
| 1399 | memcpy(run->mmio.data, vcpu->mmio_data, 8); |
| 1400 | run->mmio.len = vcpu->mmio_size; |
| 1401 | run->mmio.is_write = vcpu->mmio_is_write; |
| 1402 | } |
| 1403 | |
| 1404 | if (r) { |
| 1405 | if (kvm_mmu_unprotect_page_virt(vcpu, cr2)) |
| 1406 | return EMULATE_DONE; |
| 1407 | if (!vcpu->mmio_needed) { |
| 1408 | kvm_report_emulation_failure(vcpu, "mmio"); |
| 1409 | return EMULATE_FAIL; |
| 1410 | } |
| 1411 | return EMULATE_DO_MMIO; |
| 1412 | } |
| 1413 | |
| 1414 | kvm_x86_ops->decache_regs(vcpu); |
| 1415 | kvm_x86_ops->set_rflags(vcpu, vcpu->emulate_ctxt.eflags); |
| 1416 | |
| 1417 | if (vcpu->mmio_is_write) { |
| 1418 | vcpu->mmio_needed = 0; |
| 1419 | return EMULATE_DO_MMIO; |
| 1420 | } |
| 1421 | |
| 1422 | return EMULATE_DONE; |
| 1423 | } |
| 1424 | EXPORT_SYMBOL_GPL(emulate_instruction); |
| 1425 | |
Carsten Otte | de7d789 | 2007-10-30 18:44:25 +0100 | [diff] [blame] | 1426 | static void free_pio_guest_pages(struct kvm_vcpu *vcpu) |
| 1427 | { |
| 1428 | int i; |
| 1429 | |
| 1430 | for (i = 0; i < ARRAY_SIZE(vcpu->pio.guest_pages); ++i) |
| 1431 | if (vcpu->pio.guest_pages[i]) { |
| 1432 | kvm_release_page(vcpu->pio.guest_pages[i]); |
| 1433 | vcpu->pio.guest_pages[i] = NULL; |
| 1434 | } |
| 1435 | } |
| 1436 | |
| 1437 | static int pio_copy_data(struct kvm_vcpu *vcpu) |
| 1438 | { |
| 1439 | void *p = vcpu->pio_data; |
| 1440 | void *q; |
| 1441 | unsigned bytes; |
| 1442 | int nr_pages = vcpu->pio.guest_pages[1] ? 2 : 1; |
| 1443 | |
| 1444 | q = vmap(vcpu->pio.guest_pages, nr_pages, VM_READ|VM_WRITE, |
| 1445 | PAGE_KERNEL); |
| 1446 | if (!q) { |
| 1447 | free_pio_guest_pages(vcpu); |
| 1448 | return -ENOMEM; |
| 1449 | } |
| 1450 | q += vcpu->pio.guest_page_offset; |
| 1451 | bytes = vcpu->pio.size * vcpu->pio.cur_count; |
| 1452 | if (vcpu->pio.in) |
| 1453 | memcpy(q, p, bytes); |
| 1454 | else |
| 1455 | memcpy(p, q, bytes); |
| 1456 | q -= vcpu->pio.guest_page_offset; |
| 1457 | vunmap(q); |
| 1458 | free_pio_guest_pages(vcpu); |
| 1459 | return 0; |
| 1460 | } |
| 1461 | |
| 1462 | int complete_pio(struct kvm_vcpu *vcpu) |
| 1463 | { |
| 1464 | struct kvm_pio_request *io = &vcpu->pio; |
| 1465 | long delta; |
| 1466 | int r; |
| 1467 | |
| 1468 | kvm_x86_ops->cache_regs(vcpu); |
| 1469 | |
| 1470 | if (!io->string) { |
| 1471 | if (io->in) |
| 1472 | memcpy(&vcpu->regs[VCPU_REGS_RAX], vcpu->pio_data, |
| 1473 | io->size); |
| 1474 | } else { |
| 1475 | if (io->in) { |
| 1476 | r = pio_copy_data(vcpu); |
| 1477 | if (r) { |
| 1478 | kvm_x86_ops->cache_regs(vcpu); |
| 1479 | return r; |
| 1480 | } |
| 1481 | } |
| 1482 | |
| 1483 | delta = 1; |
| 1484 | if (io->rep) { |
| 1485 | delta *= io->cur_count; |
| 1486 | /* |
| 1487 | * The size of the register should really depend on |
| 1488 | * current address size. |
| 1489 | */ |
| 1490 | vcpu->regs[VCPU_REGS_RCX] -= delta; |
| 1491 | } |
| 1492 | if (io->down) |
| 1493 | delta = -delta; |
| 1494 | delta *= io->size; |
| 1495 | if (io->in) |
| 1496 | vcpu->regs[VCPU_REGS_RDI] += delta; |
| 1497 | else |
| 1498 | vcpu->regs[VCPU_REGS_RSI] += delta; |
| 1499 | } |
| 1500 | |
| 1501 | kvm_x86_ops->decache_regs(vcpu); |
| 1502 | |
| 1503 | io->count -= io->cur_count; |
| 1504 | io->cur_count = 0; |
| 1505 | |
| 1506 | return 0; |
| 1507 | } |
| 1508 | |
| 1509 | static void kernel_pio(struct kvm_io_device *pio_dev, |
| 1510 | struct kvm_vcpu *vcpu, |
| 1511 | void *pd) |
| 1512 | { |
| 1513 | /* TODO: String I/O for in kernel device */ |
| 1514 | |
| 1515 | mutex_lock(&vcpu->kvm->lock); |
| 1516 | if (vcpu->pio.in) |
| 1517 | kvm_iodevice_read(pio_dev, vcpu->pio.port, |
| 1518 | vcpu->pio.size, |
| 1519 | pd); |
| 1520 | else |
| 1521 | kvm_iodevice_write(pio_dev, vcpu->pio.port, |
| 1522 | vcpu->pio.size, |
| 1523 | pd); |
| 1524 | mutex_unlock(&vcpu->kvm->lock); |
| 1525 | } |
| 1526 | |
| 1527 | static void pio_string_write(struct kvm_io_device *pio_dev, |
| 1528 | struct kvm_vcpu *vcpu) |
| 1529 | { |
| 1530 | struct kvm_pio_request *io = &vcpu->pio; |
| 1531 | void *pd = vcpu->pio_data; |
| 1532 | int i; |
| 1533 | |
| 1534 | mutex_lock(&vcpu->kvm->lock); |
| 1535 | for (i = 0; i < io->cur_count; i++) { |
| 1536 | kvm_iodevice_write(pio_dev, io->port, |
| 1537 | io->size, |
| 1538 | pd); |
| 1539 | pd += io->size; |
| 1540 | } |
| 1541 | mutex_unlock(&vcpu->kvm->lock); |
| 1542 | } |
| 1543 | |
| 1544 | static struct kvm_io_device *vcpu_find_pio_dev(struct kvm_vcpu *vcpu, |
| 1545 | gpa_t addr) |
| 1546 | { |
| 1547 | return kvm_io_bus_find_dev(&vcpu->kvm->pio_bus, addr); |
| 1548 | } |
| 1549 | |
| 1550 | int kvm_emulate_pio(struct kvm_vcpu *vcpu, struct kvm_run *run, int in, |
| 1551 | int size, unsigned port) |
| 1552 | { |
| 1553 | struct kvm_io_device *pio_dev; |
| 1554 | |
| 1555 | vcpu->run->exit_reason = KVM_EXIT_IO; |
| 1556 | vcpu->run->io.direction = in ? KVM_EXIT_IO_IN : KVM_EXIT_IO_OUT; |
| 1557 | vcpu->run->io.size = vcpu->pio.size = size; |
| 1558 | vcpu->run->io.data_offset = KVM_PIO_PAGE_OFFSET * PAGE_SIZE; |
| 1559 | vcpu->run->io.count = vcpu->pio.count = vcpu->pio.cur_count = 1; |
| 1560 | vcpu->run->io.port = vcpu->pio.port = port; |
| 1561 | vcpu->pio.in = in; |
| 1562 | vcpu->pio.string = 0; |
| 1563 | vcpu->pio.down = 0; |
| 1564 | vcpu->pio.guest_page_offset = 0; |
| 1565 | vcpu->pio.rep = 0; |
| 1566 | |
| 1567 | kvm_x86_ops->cache_regs(vcpu); |
| 1568 | memcpy(vcpu->pio_data, &vcpu->regs[VCPU_REGS_RAX], 4); |
| 1569 | kvm_x86_ops->decache_regs(vcpu); |
| 1570 | |
| 1571 | kvm_x86_ops->skip_emulated_instruction(vcpu); |
| 1572 | |
| 1573 | pio_dev = vcpu_find_pio_dev(vcpu, port); |
| 1574 | if (pio_dev) { |
| 1575 | kernel_pio(pio_dev, vcpu, vcpu->pio_data); |
| 1576 | complete_pio(vcpu); |
| 1577 | return 1; |
| 1578 | } |
| 1579 | return 0; |
| 1580 | } |
| 1581 | EXPORT_SYMBOL_GPL(kvm_emulate_pio); |
| 1582 | |
| 1583 | int kvm_emulate_pio_string(struct kvm_vcpu *vcpu, struct kvm_run *run, int in, |
| 1584 | int size, unsigned long count, int down, |
| 1585 | gva_t address, int rep, unsigned port) |
| 1586 | { |
| 1587 | unsigned now, in_page; |
| 1588 | int i, ret = 0; |
| 1589 | int nr_pages = 1; |
| 1590 | struct page *page; |
| 1591 | struct kvm_io_device *pio_dev; |
| 1592 | |
| 1593 | vcpu->run->exit_reason = KVM_EXIT_IO; |
| 1594 | vcpu->run->io.direction = in ? KVM_EXIT_IO_IN : KVM_EXIT_IO_OUT; |
| 1595 | vcpu->run->io.size = vcpu->pio.size = size; |
| 1596 | vcpu->run->io.data_offset = KVM_PIO_PAGE_OFFSET * PAGE_SIZE; |
| 1597 | vcpu->run->io.count = vcpu->pio.count = vcpu->pio.cur_count = count; |
| 1598 | vcpu->run->io.port = vcpu->pio.port = port; |
| 1599 | vcpu->pio.in = in; |
| 1600 | vcpu->pio.string = 1; |
| 1601 | vcpu->pio.down = down; |
| 1602 | vcpu->pio.guest_page_offset = offset_in_page(address); |
| 1603 | vcpu->pio.rep = rep; |
| 1604 | |
| 1605 | if (!count) { |
| 1606 | kvm_x86_ops->skip_emulated_instruction(vcpu); |
| 1607 | return 1; |
| 1608 | } |
| 1609 | |
| 1610 | if (!down) |
| 1611 | in_page = PAGE_SIZE - offset_in_page(address); |
| 1612 | else |
| 1613 | in_page = offset_in_page(address) + size; |
| 1614 | now = min(count, (unsigned long)in_page / size); |
| 1615 | if (!now) { |
| 1616 | /* |
| 1617 | * String I/O straddles page boundary. Pin two guest pages |
| 1618 | * so that we satisfy atomicity constraints. Do just one |
| 1619 | * transaction to avoid complexity. |
| 1620 | */ |
| 1621 | nr_pages = 2; |
| 1622 | now = 1; |
| 1623 | } |
| 1624 | if (down) { |
| 1625 | /* |
| 1626 | * String I/O in reverse. Yuck. Kill the guest, fix later. |
| 1627 | */ |
| 1628 | pr_unimpl(vcpu, "guest string pio down\n"); |
| 1629 | inject_gp(vcpu); |
| 1630 | return 1; |
| 1631 | } |
| 1632 | vcpu->run->io.count = now; |
| 1633 | vcpu->pio.cur_count = now; |
| 1634 | |
| 1635 | if (vcpu->pio.cur_count == vcpu->pio.count) |
| 1636 | kvm_x86_ops->skip_emulated_instruction(vcpu); |
| 1637 | |
| 1638 | for (i = 0; i < nr_pages; ++i) { |
| 1639 | mutex_lock(&vcpu->kvm->lock); |
| 1640 | page = gva_to_page(vcpu, address + i * PAGE_SIZE); |
| 1641 | vcpu->pio.guest_pages[i] = page; |
| 1642 | mutex_unlock(&vcpu->kvm->lock); |
| 1643 | if (!page) { |
| 1644 | inject_gp(vcpu); |
| 1645 | free_pio_guest_pages(vcpu); |
| 1646 | return 1; |
| 1647 | } |
| 1648 | } |
| 1649 | |
| 1650 | pio_dev = vcpu_find_pio_dev(vcpu, port); |
| 1651 | if (!vcpu->pio.in) { |
| 1652 | /* string PIO write */ |
| 1653 | ret = pio_copy_data(vcpu); |
| 1654 | if (ret >= 0 && pio_dev) { |
| 1655 | pio_string_write(pio_dev, vcpu); |
| 1656 | complete_pio(vcpu); |
| 1657 | if (vcpu->pio.count == 0) |
| 1658 | ret = 1; |
| 1659 | } |
| 1660 | } else if (pio_dev) |
| 1661 | pr_unimpl(vcpu, "no string pio read support yet, " |
| 1662 | "port %x size %d count %ld\n", |
| 1663 | port, size, count); |
| 1664 | |
| 1665 | return ret; |
| 1666 | } |
| 1667 | EXPORT_SYMBOL_GPL(kvm_emulate_pio_string); |
| 1668 | |
Zhang Xiantao | f8c16bb | 2007-11-14 20:40:21 +0800 | [diff] [blame] | 1669 | int kvm_arch_init(void *opaque) |
Carsten Otte | 043405e | 2007-10-10 17:16:19 +0200 | [diff] [blame] | 1670 | { |
Zhang Xiantao | f8c16bb | 2007-11-14 20:40:21 +0800 | [diff] [blame] | 1671 | struct kvm_x86_ops *ops = (struct kvm_x86_ops *)opaque; |
| 1672 | |
Carsten Otte | 043405e | 2007-10-10 17:16:19 +0200 | [diff] [blame] | 1673 | kvm_init_msr_list(); |
Zhang Xiantao | f8c16bb | 2007-11-14 20:40:21 +0800 | [diff] [blame] | 1674 | |
| 1675 | if (kvm_x86_ops) { |
| 1676 | printk(KERN_ERR "kvm: already loaded the other module\n"); |
| 1677 | return -EEXIST; |
| 1678 | } |
| 1679 | |
| 1680 | if (!ops->cpu_has_kvm_support()) { |
| 1681 | printk(KERN_ERR "kvm: no hardware support\n"); |
| 1682 | return -EOPNOTSUPP; |
| 1683 | } |
| 1684 | if (ops->disabled_by_bios()) { |
| 1685 | printk(KERN_ERR "kvm: disabled by bios\n"); |
| 1686 | return -EOPNOTSUPP; |
| 1687 | } |
| 1688 | |
| 1689 | kvm_x86_ops = ops; |
| 1690 | |
| 1691 | return 0; |
Carsten Otte | 043405e | 2007-10-10 17:16:19 +0200 | [diff] [blame] | 1692 | } |
Hollis Blanchard | 8776e51 | 2007-10-31 17:24:24 -0500 | [diff] [blame] | 1693 | |
Zhang Xiantao | f8c16bb | 2007-11-14 20:40:21 +0800 | [diff] [blame] | 1694 | void kvm_arch_exit(void) |
| 1695 | { |
| 1696 | kvm_x86_ops = NULL; |
| 1697 | } |
| 1698 | |
Hollis Blanchard | 8776e51 | 2007-10-31 17:24:24 -0500 | [diff] [blame] | 1699 | int kvm_emulate_halt(struct kvm_vcpu *vcpu) |
| 1700 | { |
| 1701 | ++vcpu->stat.halt_exits; |
| 1702 | if (irqchip_in_kernel(vcpu->kvm)) { |
| 1703 | vcpu->mp_state = VCPU_MP_STATE_HALTED; |
| 1704 | kvm_vcpu_block(vcpu); |
| 1705 | if (vcpu->mp_state != VCPU_MP_STATE_RUNNABLE) |
| 1706 | return -EINTR; |
| 1707 | return 1; |
| 1708 | } else { |
| 1709 | vcpu->run->exit_reason = KVM_EXIT_HLT; |
| 1710 | return 0; |
| 1711 | } |
| 1712 | } |
| 1713 | EXPORT_SYMBOL_GPL(kvm_emulate_halt); |
| 1714 | |
| 1715 | int kvm_emulate_hypercall(struct kvm_vcpu *vcpu) |
| 1716 | { |
| 1717 | unsigned long nr, a0, a1, a2, a3, ret; |
| 1718 | |
| 1719 | kvm_x86_ops->cache_regs(vcpu); |
| 1720 | |
| 1721 | nr = vcpu->regs[VCPU_REGS_RAX]; |
| 1722 | a0 = vcpu->regs[VCPU_REGS_RBX]; |
| 1723 | a1 = vcpu->regs[VCPU_REGS_RCX]; |
| 1724 | a2 = vcpu->regs[VCPU_REGS_RDX]; |
| 1725 | a3 = vcpu->regs[VCPU_REGS_RSI]; |
| 1726 | |
| 1727 | if (!is_long_mode(vcpu)) { |
| 1728 | nr &= 0xFFFFFFFF; |
| 1729 | a0 &= 0xFFFFFFFF; |
| 1730 | a1 &= 0xFFFFFFFF; |
| 1731 | a2 &= 0xFFFFFFFF; |
| 1732 | a3 &= 0xFFFFFFFF; |
| 1733 | } |
| 1734 | |
| 1735 | switch (nr) { |
| 1736 | default: |
| 1737 | ret = -KVM_ENOSYS; |
| 1738 | break; |
| 1739 | } |
| 1740 | vcpu->regs[VCPU_REGS_RAX] = ret; |
| 1741 | kvm_x86_ops->decache_regs(vcpu); |
| 1742 | return 0; |
| 1743 | } |
| 1744 | EXPORT_SYMBOL_GPL(kvm_emulate_hypercall); |
| 1745 | |
| 1746 | int kvm_fix_hypercall(struct kvm_vcpu *vcpu) |
| 1747 | { |
| 1748 | char instruction[3]; |
| 1749 | int ret = 0; |
| 1750 | |
| 1751 | mutex_lock(&vcpu->kvm->lock); |
| 1752 | |
| 1753 | /* |
| 1754 | * Blow out the MMU to ensure that no other VCPU has an active mapping |
| 1755 | * to ensure that the updated hypercall appears atomically across all |
| 1756 | * VCPUs. |
| 1757 | */ |
| 1758 | kvm_mmu_zap_all(vcpu->kvm); |
| 1759 | |
| 1760 | kvm_x86_ops->cache_regs(vcpu); |
| 1761 | kvm_x86_ops->patch_hypercall(vcpu, instruction); |
| 1762 | if (emulator_write_emulated(vcpu->rip, instruction, 3, vcpu) |
| 1763 | != X86EMUL_CONTINUE) |
| 1764 | ret = -EFAULT; |
| 1765 | |
| 1766 | mutex_unlock(&vcpu->kvm->lock); |
| 1767 | |
| 1768 | return ret; |
| 1769 | } |
| 1770 | |
| 1771 | static u64 mk_cr_64(u64 curr_cr, u32 new_val) |
| 1772 | { |
| 1773 | return (curr_cr & ~((1ULL << 32) - 1)) | new_val; |
| 1774 | } |
| 1775 | |
| 1776 | void realmode_lgdt(struct kvm_vcpu *vcpu, u16 limit, unsigned long base) |
| 1777 | { |
| 1778 | struct descriptor_table dt = { limit, base }; |
| 1779 | |
| 1780 | kvm_x86_ops->set_gdt(vcpu, &dt); |
| 1781 | } |
| 1782 | |
| 1783 | void realmode_lidt(struct kvm_vcpu *vcpu, u16 limit, unsigned long base) |
| 1784 | { |
| 1785 | struct descriptor_table dt = { limit, base }; |
| 1786 | |
| 1787 | kvm_x86_ops->set_idt(vcpu, &dt); |
| 1788 | } |
| 1789 | |
| 1790 | void realmode_lmsw(struct kvm_vcpu *vcpu, unsigned long msw, |
| 1791 | unsigned long *rflags) |
| 1792 | { |
| 1793 | lmsw(vcpu, msw); |
| 1794 | *rflags = kvm_x86_ops->get_rflags(vcpu); |
| 1795 | } |
| 1796 | |
| 1797 | unsigned long realmode_get_cr(struct kvm_vcpu *vcpu, int cr) |
| 1798 | { |
| 1799 | kvm_x86_ops->decache_cr4_guest_bits(vcpu); |
| 1800 | switch (cr) { |
| 1801 | case 0: |
| 1802 | return vcpu->cr0; |
| 1803 | case 2: |
| 1804 | return vcpu->cr2; |
| 1805 | case 3: |
| 1806 | return vcpu->cr3; |
| 1807 | case 4: |
| 1808 | return vcpu->cr4; |
| 1809 | default: |
| 1810 | vcpu_printf(vcpu, "%s: unexpected cr %u\n", __FUNCTION__, cr); |
| 1811 | return 0; |
| 1812 | } |
| 1813 | } |
| 1814 | |
| 1815 | void realmode_set_cr(struct kvm_vcpu *vcpu, int cr, unsigned long val, |
| 1816 | unsigned long *rflags) |
| 1817 | { |
| 1818 | switch (cr) { |
| 1819 | case 0: |
| 1820 | set_cr0(vcpu, mk_cr_64(vcpu->cr0, val)); |
| 1821 | *rflags = kvm_x86_ops->get_rflags(vcpu); |
| 1822 | break; |
| 1823 | case 2: |
| 1824 | vcpu->cr2 = val; |
| 1825 | break; |
| 1826 | case 3: |
| 1827 | set_cr3(vcpu, val); |
| 1828 | break; |
| 1829 | case 4: |
| 1830 | set_cr4(vcpu, mk_cr_64(vcpu->cr4, val)); |
| 1831 | break; |
| 1832 | default: |
| 1833 | vcpu_printf(vcpu, "%s: unexpected cr %u\n", __FUNCTION__, cr); |
| 1834 | } |
| 1835 | } |
| 1836 | |
| 1837 | void kvm_emulate_cpuid(struct kvm_vcpu *vcpu) |
| 1838 | { |
| 1839 | int i; |
| 1840 | u32 function; |
| 1841 | struct kvm_cpuid_entry *e, *best; |
| 1842 | |
| 1843 | kvm_x86_ops->cache_regs(vcpu); |
| 1844 | function = vcpu->regs[VCPU_REGS_RAX]; |
| 1845 | vcpu->regs[VCPU_REGS_RAX] = 0; |
| 1846 | vcpu->regs[VCPU_REGS_RBX] = 0; |
| 1847 | vcpu->regs[VCPU_REGS_RCX] = 0; |
| 1848 | vcpu->regs[VCPU_REGS_RDX] = 0; |
| 1849 | best = NULL; |
| 1850 | for (i = 0; i < vcpu->cpuid_nent; ++i) { |
| 1851 | e = &vcpu->cpuid_entries[i]; |
| 1852 | if (e->function == function) { |
| 1853 | best = e; |
| 1854 | break; |
| 1855 | } |
| 1856 | /* |
| 1857 | * Both basic or both extended? |
| 1858 | */ |
| 1859 | if (((e->function ^ function) & 0x80000000) == 0) |
| 1860 | if (!best || e->function > best->function) |
| 1861 | best = e; |
| 1862 | } |
| 1863 | if (best) { |
| 1864 | vcpu->regs[VCPU_REGS_RAX] = best->eax; |
| 1865 | vcpu->regs[VCPU_REGS_RBX] = best->ebx; |
| 1866 | vcpu->regs[VCPU_REGS_RCX] = best->ecx; |
| 1867 | vcpu->regs[VCPU_REGS_RDX] = best->edx; |
| 1868 | } |
| 1869 | kvm_x86_ops->decache_regs(vcpu); |
| 1870 | kvm_x86_ops->skip_emulated_instruction(vcpu); |
| 1871 | } |
| 1872 | EXPORT_SYMBOL_GPL(kvm_emulate_cpuid); |
Hollis Blanchard | d075206 | 2007-10-31 17:24:25 -0500 | [diff] [blame] | 1873 | |
| 1874 | /* |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 1875 | * Check if userspace requested an interrupt window, and that the |
| 1876 | * interrupt window is open. |
| 1877 | * |
| 1878 | * No need to exit to userspace if we already have an interrupt queued. |
| 1879 | */ |
| 1880 | static int dm_request_for_irq_injection(struct kvm_vcpu *vcpu, |
| 1881 | struct kvm_run *kvm_run) |
| 1882 | { |
| 1883 | return (!vcpu->irq_summary && |
| 1884 | kvm_run->request_interrupt_window && |
| 1885 | vcpu->interrupt_window_open && |
| 1886 | (kvm_x86_ops->get_rflags(vcpu) & X86_EFLAGS_IF)); |
| 1887 | } |
| 1888 | |
| 1889 | static void post_kvm_run_save(struct kvm_vcpu *vcpu, |
| 1890 | struct kvm_run *kvm_run) |
| 1891 | { |
| 1892 | kvm_run->if_flag = (kvm_x86_ops->get_rflags(vcpu) & X86_EFLAGS_IF) != 0; |
| 1893 | kvm_run->cr8 = get_cr8(vcpu); |
| 1894 | kvm_run->apic_base = kvm_get_apic_base(vcpu); |
| 1895 | if (irqchip_in_kernel(vcpu->kvm)) |
| 1896 | kvm_run->ready_for_interrupt_injection = 1; |
| 1897 | else |
| 1898 | kvm_run->ready_for_interrupt_injection = |
| 1899 | (vcpu->interrupt_window_open && |
| 1900 | vcpu->irq_summary == 0); |
| 1901 | } |
| 1902 | |
| 1903 | static int __vcpu_run(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run) |
| 1904 | { |
| 1905 | int r; |
| 1906 | |
| 1907 | if (unlikely(vcpu->mp_state == VCPU_MP_STATE_SIPI_RECEIVED)) { |
| 1908 | pr_debug("vcpu %d received sipi with vector # %x\n", |
| 1909 | vcpu->vcpu_id, vcpu->sipi_vector); |
| 1910 | kvm_lapic_reset(vcpu); |
| 1911 | r = kvm_x86_ops->vcpu_reset(vcpu); |
| 1912 | if (r) |
| 1913 | return r; |
| 1914 | vcpu->mp_state = VCPU_MP_STATE_RUNNABLE; |
| 1915 | } |
| 1916 | |
| 1917 | preempted: |
| 1918 | if (vcpu->guest_debug.enabled) |
| 1919 | kvm_x86_ops->guest_debug_pre(vcpu); |
| 1920 | |
| 1921 | again: |
| 1922 | r = kvm_mmu_reload(vcpu); |
| 1923 | if (unlikely(r)) |
| 1924 | goto out; |
| 1925 | |
| 1926 | kvm_inject_pending_timer_irqs(vcpu); |
| 1927 | |
| 1928 | preempt_disable(); |
| 1929 | |
| 1930 | kvm_x86_ops->prepare_guest_switch(vcpu); |
| 1931 | kvm_load_guest_fpu(vcpu); |
| 1932 | |
| 1933 | local_irq_disable(); |
| 1934 | |
| 1935 | if (signal_pending(current)) { |
| 1936 | local_irq_enable(); |
| 1937 | preempt_enable(); |
| 1938 | r = -EINTR; |
| 1939 | kvm_run->exit_reason = KVM_EXIT_INTR; |
| 1940 | ++vcpu->stat.signal_exits; |
| 1941 | goto out; |
| 1942 | } |
| 1943 | |
| 1944 | if (irqchip_in_kernel(vcpu->kvm)) |
| 1945 | kvm_x86_ops->inject_pending_irq(vcpu); |
| 1946 | else if (!vcpu->mmio_read_completed) |
| 1947 | kvm_x86_ops->inject_pending_vectors(vcpu, kvm_run); |
| 1948 | |
| 1949 | vcpu->guest_mode = 1; |
| 1950 | kvm_guest_enter(); |
| 1951 | |
| 1952 | if (vcpu->requests) |
| 1953 | if (test_and_clear_bit(KVM_REQ_TLB_FLUSH, &vcpu->requests)) |
| 1954 | kvm_x86_ops->tlb_flush(vcpu); |
| 1955 | |
| 1956 | kvm_x86_ops->run(vcpu, kvm_run); |
| 1957 | |
| 1958 | vcpu->guest_mode = 0; |
| 1959 | local_irq_enable(); |
| 1960 | |
| 1961 | ++vcpu->stat.exits; |
| 1962 | |
| 1963 | /* |
| 1964 | * We must have an instruction between local_irq_enable() and |
| 1965 | * kvm_guest_exit(), so the timer interrupt isn't delayed by |
| 1966 | * the interrupt shadow. The stat.exits increment will do nicely. |
| 1967 | * But we need to prevent reordering, hence this barrier(): |
| 1968 | */ |
| 1969 | barrier(); |
| 1970 | |
| 1971 | kvm_guest_exit(); |
| 1972 | |
| 1973 | preempt_enable(); |
| 1974 | |
| 1975 | /* |
| 1976 | * Profile KVM exit RIPs: |
| 1977 | */ |
| 1978 | if (unlikely(prof_on == KVM_PROFILING)) { |
| 1979 | kvm_x86_ops->cache_regs(vcpu); |
| 1980 | profile_hit(KVM_PROFILING, (void *)vcpu->rip); |
| 1981 | } |
| 1982 | |
| 1983 | r = kvm_x86_ops->handle_exit(kvm_run, vcpu); |
| 1984 | |
| 1985 | if (r > 0) { |
| 1986 | if (dm_request_for_irq_injection(vcpu, kvm_run)) { |
| 1987 | r = -EINTR; |
| 1988 | kvm_run->exit_reason = KVM_EXIT_INTR; |
| 1989 | ++vcpu->stat.request_irq_exits; |
| 1990 | goto out; |
| 1991 | } |
Avi Kivity | e1beb1d | 2007-11-18 13:50:24 +0200 | [diff] [blame] | 1992 | if (!need_resched()) |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 1993 | goto again; |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 1994 | } |
| 1995 | |
| 1996 | out: |
| 1997 | if (r > 0) { |
| 1998 | kvm_resched(vcpu); |
| 1999 | goto preempted; |
| 2000 | } |
| 2001 | |
| 2002 | post_kvm_run_save(vcpu, kvm_run); |
| 2003 | |
| 2004 | return r; |
| 2005 | } |
| 2006 | |
| 2007 | int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run) |
| 2008 | { |
| 2009 | int r; |
| 2010 | sigset_t sigsaved; |
| 2011 | |
| 2012 | vcpu_load(vcpu); |
| 2013 | |
| 2014 | if (unlikely(vcpu->mp_state == VCPU_MP_STATE_UNINITIALIZED)) { |
| 2015 | kvm_vcpu_block(vcpu); |
| 2016 | vcpu_put(vcpu); |
| 2017 | return -EAGAIN; |
| 2018 | } |
| 2019 | |
| 2020 | if (vcpu->sigset_active) |
| 2021 | sigprocmask(SIG_SETMASK, &vcpu->sigset, &sigsaved); |
| 2022 | |
| 2023 | /* re-sync apic's tpr */ |
| 2024 | if (!irqchip_in_kernel(vcpu->kvm)) |
| 2025 | set_cr8(vcpu, kvm_run->cr8); |
| 2026 | |
| 2027 | if (vcpu->pio.cur_count) { |
| 2028 | r = complete_pio(vcpu); |
| 2029 | if (r) |
| 2030 | goto out; |
| 2031 | } |
| 2032 | #if CONFIG_HAS_IOMEM |
| 2033 | if (vcpu->mmio_needed) { |
| 2034 | memcpy(vcpu->mmio_data, kvm_run->mmio.data, 8); |
| 2035 | vcpu->mmio_read_completed = 1; |
| 2036 | vcpu->mmio_needed = 0; |
| 2037 | r = emulate_instruction(vcpu, kvm_run, |
| 2038 | vcpu->mmio_fault_cr2, 0, 1); |
| 2039 | if (r == EMULATE_DO_MMIO) { |
| 2040 | /* |
| 2041 | * Read-modify-write. Back to userspace. |
| 2042 | */ |
| 2043 | r = 0; |
| 2044 | goto out; |
| 2045 | } |
| 2046 | } |
| 2047 | #endif |
| 2048 | if (kvm_run->exit_reason == KVM_EXIT_HYPERCALL) { |
| 2049 | kvm_x86_ops->cache_regs(vcpu); |
| 2050 | vcpu->regs[VCPU_REGS_RAX] = kvm_run->hypercall.ret; |
| 2051 | kvm_x86_ops->decache_regs(vcpu); |
| 2052 | } |
| 2053 | |
| 2054 | r = __vcpu_run(vcpu, kvm_run); |
| 2055 | |
| 2056 | out: |
| 2057 | if (vcpu->sigset_active) |
| 2058 | sigprocmask(SIG_SETMASK, &sigsaved, NULL); |
| 2059 | |
| 2060 | vcpu_put(vcpu); |
| 2061 | return r; |
| 2062 | } |
| 2063 | |
| 2064 | int kvm_arch_vcpu_ioctl_get_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs) |
| 2065 | { |
| 2066 | vcpu_load(vcpu); |
| 2067 | |
| 2068 | kvm_x86_ops->cache_regs(vcpu); |
| 2069 | |
| 2070 | regs->rax = vcpu->regs[VCPU_REGS_RAX]; |
| 2071 | regs->rbx = vcpu->regs[VCPU_REGS_RBX]; |
| 2072 | regs->rcx = vcpu->regs[VCPU_REGS_RCX]; |
| 2073 | regs->rdx = vcpu->regs[VCPU_REGS_RDX]; |
| 2074 | regs->rsi = vcpu->regs[VCPU_REGS_RSI]; |
| 2075 | regs->rdi = vcpu->regs[VCPU_REGS_RDI]; |
| 2076 | regs->rsp = vcpu->regs[VCPU_REGS_RSP]; |
| 2077 | regs->rbp = vcpu->regs[VCPU_REGS_RBP]; |
| 2078 | #ifdef CONFIG_X86_64 |
| 2079 | regs->r8 = vcpu->regs[VCPU_REGS_R8]; |
| 2080 | regs->r9 = vcpu->regs[VCPU_REGS_R9]; |
| 2081 | regs->r10 = vcpu->regs[VCPU_REGS_R10]; |
| 2082 | regs->r11 = vcpu->regs[VCPU_REGS_R11]; |
| 2083 | regs->r12 = vcpu->regs[VCPU_REGS_R12]; |
| 2084 | regs->r13 = vcpu->regs[VCPU_REGS_R13]; |
| 2085 | regs->r14 = vcpu->regs[VCPU_REGS_R14]; |
| 2086 | regs->r15 = vcpu->regs[VCPU_REGS_R15]; |
| 2087 | #endif |
| 2088 | |
| 2089 | regs->rip = vcpu->rip; |
| 2090 | regs->rflags = kvm_x86_ops->get_rflags(vcpu); |
| 2091 | |
| 2092 | /* |
| 2093 | * Don't leak debug flags in case they were set for guest debugging |
| 2094 | */ |
| 2095 | if (vcpu->guest_debug.enabled && vcpu->guest_debug.singlestep) |
| 2096 | regs->rflags &= ~(X86_EFLAGS_TF | X86_EFLAGS_RF); |
| 2097 | |
| 2098 | vcpu_put(vcpu); |
| 2099 | |
| 2100 | return 0; |
| 2101 | } |
| 2102 | |
| 2103 | int kvm_arch_vcpu_ioctl_set_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs) |
| 2104 | { |
| 2105 | vcpu_load(vcpu); |
| 2106 | |
| 2107 | vcpu->regs[VCPU_REGS_RAX] = regs->rax; |
| 2108 | vcpu->regs[VCPU_REGS_RBX] = regs->rbx; |
| 2109 | vcpu->regs[VCPU_REGS_RCX] = regs->rcx; |
| 2110 | vcpu->regs[VCPU_REGS_RDX] = regs->rdx; |
| 2111 | vcpu->regs[VCPU_REGS_RSI] = regs->rsi; |
| 2112 | vcpu->regs[VCPU_REGS_RDI] = regs->rdi; |
| 2113 | vcpu->regs[VCPU_REGS_RSP] = regs->rsp; |
| 2114 | vcpu->regs[VCPU_REGS_RBP] = regs->rbp; |
| 2115 | #ifdef CONFIG_X86_64 |
| 2116 | vcpu->regs[VCPU_REGS_R8] = regs->r8; |
| 2117 | vcpu->regs[VCPU_REGS_R9] = regs->r9; |
| 2118 | vcpu->regs[VCPU_REGS_R10] = regs->r10; |
| 2119 | vcpu->regs[VCPU_REGS_R11] = regs->r11; |
| 2120 | vcpu->regs[VCPU_REGS_R12] = regs->r12; |
| 2121 | vcpu->regs[VCPU_REGS_R13] = regs->r13; |
| 2122 | vcpu->regs[VCPU_REGS_R14] = regs->r14; |
| 2123 | vcpu->regs[VCPU_REGS_R15] = regs->r15; |
| 2124 | #endif |
| 2125 | |
| 2126 | vcpu->rip = regs->rip; |
| 2127 | kvm_x86_ops->set_rflags(vcpu, regs->rflags); |
| 2128 | |
| 2129 | kvm_x86_ops->decache_regs(vcpu); |
| 2130 | |
| 2131 | vcpu_put(vcpu); |
| 2132 | |
| 2133 | return 0; |
| 2134 | } |
| 2135 | |
| 2136 | static void get_segment(struct kvm_vcpu *vcpu, |
| 2137 | struct kvm_segment *var, int seg) |
| 2138 | { |
| 2139 | return kvm_x86_ops->get_segment(vcpu, var, seg); |
| 2140 | } |
| 2141 | |
| 2142 | void kvm_get_cs_db_l_bits(struct kvm_vcpu *vcpu, int *db, int *l) |
| 2143 | { |
| 2144 | struct kvm_segment cs; |
| 2145 | |
| 2146 | get_segment(vcpu, &cs, VCPU_SREG_CS); |
| 2147 | *db = cs.db; |
| 2148 | *l = cs.l; |
| 2149 | } |
| 2150 | EXPORT_SYMBOL_GPL(kvm_get_cs_db_l_bits); |
| 2151 | |
| 2152 | int kvm_arch_vcpu_ioctl_get_sregs(struct kvm_vcpu *vcpu, |
| 2153 | struct kvm_sregs *sregs) |
| 2154 | { |
| 2155 | struct descriptor_table dt; |
| 2156 | int pending_vec; |
| 2157 | |
| 2158 | vcpu_load(vcpu); |
| 2159 | |
| 2160 | get_segment(vcpu, &sregs->cs, VCPU_SREG_CS); |
| 2161 | get_segment(vcpu, &sregs->ds, VCPU_SREG_DS); |
| 2162 | get_segment(vcpu, &sregs->es, VCPU_SREG_ES); |
| 2163 | get_segment(vcpu, &sregs->fs, VCPU_SREG_FS); |
| 2164 | get_segment(vcpu, &sregs->gs, VCPU_SREG_GS); |
| 2165 | get_segment(vcpu, &sregs->ss, VCPU_SREG_SS); |
| 2166 | |
| 2167 | get_segment(vcpu, &sregs->tr, VCPU_SREG_TR); |
| 2168 | get_segment(vcpu, &sregs->ldt, VCPU_SREG_LDTR); |
| 2169 | |
| 2170 | kvm_x86_ops->get_idt(vcpu, &dt); |
| 2171 | sregs->idt.limit = dt.limit; |
| 2172 | sregs->idt.base = dt.base; |
| 2173 | kvm_x86_ops->get_gdt(vcpu, &dt); |
| 2174 | sregs->gdt.limit = dt.limit; |
| 2175 | sregs->gdt.base = dt.base; |
| 2176 | |
| 2177 | kvm_x86_ops->decache_cr4_guest_bits(vcpu); |
| 2178 | sregs->cr0 = vcpu->cr0; |
| 2179 | sregs->cr2 = vcpu->cr2; |
| 2180 | sregs->cr3 = vcpu->cr3; |
| 2181 | sregs->cr4 = vcpu->cr4; |
| 2182 | sregs->cr8 = get_cr8(vcpu); |
| 2183 | sregs->efer = vcpu->shadow_efer; |
| 2184 | sregs->apic_base = kvm_get_apic_base(vcpu); |
| 2185 | |
| 2186 | if (irqchip_in_kernel(vcpu->kvm)) { |
| 2187 | memset(sregs->interrupt_bitmap, 0, |
| 2188 | sizeof sregs->interrupt_bitmap); |
| 2189 | pending_vec = kvm_x86_ops->get_irq(vcpu); |
| 2190 | if (pending_vec >= 0) |
| 2191 | set_bit(pending_vec, |
| 2192 | (unsigned long *)sregs->interrupt_bitmap); |
| 2193 | } else |
| 2194 | memcpy(sregs->interrupt_bitmap, vcpu->irq_pending, |
| 2195 | sizeof sregs->interrupt_bitmap); |
| 2196 | |
| 2197 | vcpu_put(vcpu); |
| 2198 | |
| 2199 | return 0; |
| 2200 | } |
| 2201 | |
| 2202 | static void set_segment(struct kvm_vcpu *vcpu, |
| 2203 | struct kvm_segment *var, int seg) |
| 2204 | { |
| 2205 | return kvm_x86_ops->set_segment(vcpu, var, seg); |
| 2206 | } |
| 2207 | |
| 2208 | int kvm_arch_vcpu_ioctl_set_sregs(struct kvm_vcpu *vcpu, |
| 2209 | struct kvm_sregs *sregs) |
| 2210 | { |
| 2211 | int mmu_reset_needed = 0; |
| 2212 | int i, pending_vec, max_bits; |
| 2213 | struct descriptor_table dt; |
| 2214 | |
| 2215 | vcpu_load(vcpu); |
| 2216 | |
| 2217 | dt.limit = sregs->idt.limit; |
| 2218 | dt.base = sregs->idt.base; |
| 2219 | kvm_x86_ops->set_idt(vcpu, &dt); |
| 2220 | dt.limit = sregs->gdt.limit; |
| 2221 | dt.base = sregs->gdt.base; |
| 2222 | kvm_x86_ops->set_gdt(vcpu, &dt); |
| 2223 | |
| 2224 | vcpu->cr2 = sregs->cr2; |
| 2225 | mmu_reset_needed |= vcpu->cr3 != sregs->cr3; |
| 2226 | vcpu->cr3 = sregs->cr3; |
| 2227 | |
| 2228 | set_cr8(vcpu, sregs->cr8); |
| 2229 | |
| 2230 | mmu_reset_needed |= vcpu->shadow_efer != sregs->efer; |
| 2231 | #ifdef CONFIG_X86_64 |
| 2232 | kvm_x86_ops->set_efer(vcpu, sregs->efer); |
| 2233 | #endif |
| 2234 | kvm_set_apic_base(vcpu, sregs->apic_base); |
| 2235 | |
| 2236 | kvm_x86_ops->decache_cr4_guest_bits(vcpu); |
| 2237 | |
| 2238 | mmu_reset_needed |= vcpu->cr0 != sregs->cr0; |
| 2239 | vcpu->cr0 = sregs->cr0; |
| 2240 | kvm_x86_ops->set_cr0(vcpu, sregs->cr0); |
| 2241 | |
| 2242 | mmu_reset_needed |= vcpu->cr4 != sregs->cr4; |
| 2243 | kvm_x86_ops->set_cr4(vcpu, sregs->cr4); |
| 2244 | if (!is_long_mode(vcpu) && is_pae(vcpu)) |
| 2245 | load_pdptrs(vcpu, vcpu->cr3); |
| 2246 | |
| 2247 | if (mmu_reset_needed) |
| 2248 | kvm_mmu_reset_context(vcpu); |
| 2249 | |
| 2250 | if (!irqchip_in_kernel(vcpu->kvm)) { |
| 2251 | memcpy(vcpu->irq_pending, sregs->interrupt_bitmap, |
| 2252 | sizeof vcpu->irq_pending); |
| 2253 | vcpu->irq_summary = 0; |
| 2254 | for (i = 0; i < ARRAY_SIZE(vcpu->irq_pending); ++i) |
| 2255 | if (vcpu->irq_pending[i]) |
| 2256 | __set_bit(i, &vcpu->irq_summary); |
| 2257 | } else { |
| 2258 | max_bits = (sizeof sregs->interrupt_bitmap) << 3; |
| 2259 | pending_vec = find_first_bit( |
| 2260 | (const unsigned long *)sregs->interrupt_bitmap, |
| 2261 | max_bits); |
| 2262 | /* Only pending external irq is handled here */ |
| 2263 | if (pending_vec < max_bits) { |
| 2264 | kvm_x86_ops->set_irq(vcpu, pending_vec); |
| 2265 | pr_debug("Set back pending irq %d\n", |
| 2266 | pending_vec); |
| 2267 | } |
| 2268 | } |
| 2269 | |
| 2270 | set_segment(vcpu, &sregs->cs, VCPU_SREG_CS); |
| 2271 | set_segment(vcpu, &sregs->ds, VCPU_SREG_DS); |
| 2272 | set_segment(vcpu, &sregs->es, VCPU_SREG_ES); |
| 2273 | set_segment(vcpu, &sregs->fs, VCPU_SREG_FS); |
| 2274 | set_segment(vcpu, &sregs->gs, VCPU_SREG_GS); |
| 2275 | set_segment(vcpu, &sregs->ss, VCPU_SREG_SS); |
| 2276 | |
| 2277 | set_segment(vcpu, &sregs->tr, VCPU_SREG_TR); |
| 2278 | set_segment(vcpu, &sregs->ldt, VCPU_SREG_LDTR); |
| 2279 | |
| 2280 | vcpu_put(vcpu); |
| 2281 | |
| 2282 | return 0; |
| 2283 | } |
| 2284 | |
| 2285 | int kvm_arch_vcpu_ioctl_debug_guest(struct kvm_vcpu *vcpu, |
| 2286 | struct kvm_debug_guest *dbg) |
| 2287 | { |
| 2288 | int r; |
| 2289 | |
| 2290 | vcpu_load(vcpu); |
| 2291 | |
| 2292 | r = kvm_x86_ops->set_guest_debug(vcpu, dbg); |
| 2293 | |
| 2294 | vcpu_put(vcpu); |
| 2295 | |
| 2296 | return r; |
| 2297 | } |
| 2298 | |
| 2299 | /* |
Hollis Blanchard | d075206 | 2007-10-31 17:24:25 -0500 | [diff] [blame] | 2300 | * fxsave fpu state. Taken from x86_64/processor.h. To be killed when |
| 2301 | * we have asm/x86/processor.h |
| 2302 | */ |
| 2303 | struct fxsave { |
| 2304 | u16 cwd; |
| 2305 | u16 swd; |
| 2306 | u16 twd; |
| 2307 | u16 fop; |
| 2308 | u64 rip; |
| 2309 | u64 rdp; |
| 2310 | u32 mxcsr; |
| 2311 | u32 mxcsr_mask; |
| 2312 | u32 st_space[32]; /* 8*16 bytes for each FP-reg = 128 bytes */ |
| 2313 | #ifdef CONFIG_X86_64 |
| 2314 | u32 xmm_space[64]; /* 16*16 bytes for each XMM-reg = 256 bytes */ |
| 2315 | #else |
| 2316 | u32 xmm_space[32]; /* 8*16 bytes for each XMM-reg = 128 bytes */ |
| 2317 | #endif |
| 2318 | }; |
| 2319 | |
Zhang Xiantao | 8b00679 | 2007-11-16 13:05:55 +0800 | [diff] [blame] | 2320 | /* |
| 2321 | * Translate a guest virtual address to a guest physical address. |
| 2322 | */ |
| 2323 | int kvm_arch_vcpu_ioctl_translate(struct kvm_vcpu *vcpu, |
| 2324 | struct kvm_translation *tr) |
| 2325 | { |
| 2326 | unsigned long vaddr = tr->linear_address; |
| 2327 | gpa_t gpa; |
| 2328 | |
| 2329 | vcpu_load(vcpu); |
| 2330 | mutex_lock(&vcpu->kvm->lock); |
| 2331 | gpa = vcpu->mmu.gva_to_gpa(vcpu, vaddr); |
| 2332 | tr->physical_address = gpa; |
| 2333 | tr->valid = gpa != UNMAPPED_GVA; |
| 2334 | tr->writeable = 1; |
| 2335 | tr->usermode = 0; |
| 2336 | mutex_unlock(&vcpu->kvm->lock); |
| 2337 | vcpu_put(vcpu); |
| 2338 | |
| 2339 | return 0; |
| 2340 | } |
| 2341 | |
Hollis Blanchard | d075206 | 2007-10-31 17:24:25 -0500 | [diff] [blame] | 2342 | int kvm_arch_vcpu_ioctl_get_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu) |
| 2343 | { |
| 2344 | struct fxsave *fxsave = (struct fxsave *)&vcpu->guest_fx_image; |
| 2345 | |
| 2346 | vcpu_load(vcpu); |
| 2347 | |
| 2348 | memcpy(fpu->fpr, fxsave->st_space, 128); |
| 2349 | fpu->fcw = fxsave->cwd; |
| 2350 | fpu->fsw = fxsave->swd; |
| 2351 | fpu->ftwx = fxsave->twd; |
| 2352 | fpu->last_opcode = fxsave->fop; |
| 2353 | fpu->last_ip = fxsave->rip; |
| 2354 | fpu->last_dp = fxsave->rdp; |
| 2355 | memcpy(fpu->xmm, fxsave->xmm_space, sizeof fxsave->xmm_space); |
| 2356 | |
| 2357 | vcpu_put(vcpu); |
| 2358 | |
| 2359 | return 0; |
| 2360 | } |
| 2361 | |
| 2362 | int kvm_arch_vcpu_ioctl_set_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu) |
| 2363 | { |
| 2364 | struct fxsave *fxsave = (struct fxsave *)&vcpu->guest_fx_image; |
| 2365 | |
| 2366 | vcpu_load(vcpu); |
| 2367 | |
| 2368 | memcpy(fxsave->st_space, fpu->fpr, 128); |
| 2369 | fxsave->cwd = fpu->fcw; |
| 2370 | fxsave->swd = fpu->fsw; |
| 2371 | fxsave->twd = fpu->ftwx; |
| 2372 | fxsave->fop = fpu->last_opcode; |
| 2373 | fxsave->rip = fpu->last_ip; |
| 2374 | fxsave->rdp = fpu->last_dp; |
| 2375 | memcpy(fxsave->xmm_space, fpu->xmm, sizeof fxsave->xmm_space); |
| 2376 | |
| 2377 | vcpu_put(vcpu); |
| 2378 | |
| 2379 | return 0; |
| 2380 | } |
| 2381 | |
| 2382 | void fx_init(struct kvm_vcpu *vcpu) |
| 2383 | { |
| 2384 | unsigned after_mxcsr_mask; |
| 2385 | |
| 2386 | /* Initialize guest FPU by resetting ours and saving into guest's */ |
| 2387 | preempt_disable(); |
| 2388 | fx_save(&vcpu->host_fx_image); |
| 2389 | fpu_init(); |
| 2390 | fx_save(&vcpu->guest_fx_image); |
| 2391 | fx_restore(&vcpu->host_fx_image); |
| 2392 | preempt_enable(); |
| 2393 | |
| 2394 | vcpu->cr0 |= X86_CR0_ET; |
| 2395 | after_mxcsr_mask = offsetof(struct i387_fxsave_struct, st_space); |
| 2396 | vcpu->guest_fx_image.mxcsr = 0x1f80; |
| 2397 | memset((void *)&vcpu->guest_fx_image + after_mxcsr_mask, |
| 2398 | 0, sizeof(struct i387_fxsave_struct) - after_mxcsr_mask); |
| 2399 | } |
| 2400 | EXPORT_SYMBOL_GPL(fx_init); |
| 2401 | |
| 2402 | void kvm_load_guest_fpu(struct kvm_vcpu *vcpu) |
| 2403 | { |
| 2404 | if (!vcpu->fpu_active || vcpu->guest_fpu_loaded) |
| 2405 | return; |
| 2406 | |
| 2407 | vcpu->guest_fpu_loaded = 1; |
| 2408 | fx_save(&vcpu->host_fx_image); |
| 2409 | fx_restore(&vcpu->guest_fx_image); |
| 2410 | } |
| 2411 | EXPORT_SYMBOL_GPL(kvm_load_guest_fpu); |
| 2412 | |
| 2413 | void kvm_put_guest_fpu(struct kvm_vcpu *vcpu) |
| 2414 | { |
| 2415 | if (!vcpu->guest_fpu_loaded) |
| 2416 | return; |
| 2417 | |
| 2418 | vcpu->guest_fpu_loaded = 0; |
| 2419 | fx_save(&vcpu->guest_fx_image); |
| 2420 | fx_restore(&vcpu->host_fx_image); |
Avi Kivity | f096ed8 | 2007-11-18 13:54:33 +0200 | [diff] [blame^] | 2421 | ++vcpu->stat.fpu_reload; |
Hollis Blanchard | d075206 | 2007-10-31 17:24:25 -0500 | [diff] [blame] | 2422 | } |
| 2423 | EXPORT_SYMBOL_GPL(kvm_put_guest_fpu); |
Zhang Xiantao | e9b11c1 | 2007-11-14 20:38:21 +0800 | [diff] [blame] | 2424 | |
| 2425 | void kvm_arch_vcpu_free(struct kvm_vcpu *vcpu) |
| 2426 | { |
| 2427 | kvm_x86_ops->vcpu_free(vcpu); |
| 2428 | } |
| 2429 | |
| 2430 | struct kvm_vcpu *kvm_arch_vcpu_create(struct kvm *kvm, |
| 2431 | unsigned int id) |
| 2432 | { |
| 2433 | int r; |
| 2434 | struct kvm_vcpu *vcpu = kvm_x86_ops->vcpu_create(kvm, id); |
| 2435 | |
| 2436 | if (IS_ERR(vcpu)) { |
| 2437 | r = -ENOMEM; |
| 2438 | goto fail; |
| 2439 | } |
| 2440 | |
| 2441 | /* We do fxsave: this must be aligned. */ |
| 2442 | BUG_ON((unsigned long)&vcpu->host_fx_image & 0xF); |
| 2443 | |
| 2444 | vcpu_load(vcpu); |
| 2445 | r = kvm_arch_vcpu_reset(vcpu); |
| 2446 | if (r == 0) |
| 2447 | r = kvm_mmu_setup(vcpu); |
| 2448 | vcpu_put(vcpu); |
| 2449 | if (r < 0) |
| 2450 | goto free_vcpu; |
| 2451 | |
| 2452 | return vcpu; |
| 2453 | free_vcpu: |
| 2454 | kvm_x86_ops->vcpu_free(vcpu); |
| 2455 | fail: |
| 2456 | return ERR_PTR(r); |
| 2457 | } |
| 2458 | |
| 2459 | void kvm_arch_vcpu_destory(struct kvm_vcpu *vcpu) |
| 2460 | { |
| 2461 | vcpu_load(vcpu); |
| 2462 | kvm_mmu_unload(vcpu); |
| 2463 | vcpu_put(vcpu); |
| 2464 | |
| 2465 | kvm_x86_ops->vcpu_free(vcpu); |
| 2466 | } |
| 2467 | |
| 2468 | int kvm_arch_vcpu_reset(struct kvm_vcpu *vcpu) |
| 2469 | { |
| 2470 | return kvm_x86_ops->vcpu_reset(vcpu); |
| 2471 | } |
| 2472 | |
| 2473 | void kvm_arch_hardware_enable(void *garbage) |
| 2474 | { |
| 2475 | kvm_x86_ops->hardware_enable(garbage); |
| 2476 | } |
| 2477 | |
| 2478 | void kvm_arch_hardware_disable(void *garbage) |
| 2479 | { |
| 2480 | kvm_x86_ops->hardware_disable(garbage); |
| 2481 | } |
| 2482 | |
| 2483 | int kvm_arch_hardware_setup(void) |
| 2484 | { |
| 2485 | return kvm_x86_ops->hardware_setup(); |
| 2486 | } |
| 2487 | |
| 2488 | void kvm_arch_hardware_unsetup(void) |
| 2489 | { |
| 2490 | kvm_x86_ops->hardware_unsetup(); |
| 2491 | } |
| 2492 | |
| 2493 | void kvm_arch_check_processor_compat(void *rtn) |
| 2494 | { |
| 2495 | kvm_x86_ops->check_processor_compatibility(rtn); |
| 2496 | } |
| 2497 | |
| 2498 | int kvm_arch_vcpu_init(struct kvm_vcpu *vcpu) |
| 2499 | { |
| 2500 | struct page *page; |
| 2501 | struct kvm *kvm; |
| 2502 | int r; |
| 2503 | |
| 2504 | BUG_ON(vcpu->kvm == NULL); |
| 2505 | kvm = vcpu->kvm; |
| 2506 | |
| 2507 | vcpu->mmu.root_hpa = INVALID_PAGE; |
| 2508 | if (!irqchip_in_kernel(kvm) || vcpu->vcpu_id == 0) |
| 2509 | vcpu->mp_state = VCPU_MP_STATE_RUNNABLE; |
| 2510 | else |
| 2511 | vcpu->mp_state = VCPU_MP_STATE_UNINITIALIZED; |
| 2512 | |
| 2513 | page = alloc_page(GFP_KERNEL | __GFP_ZERO); |
| 2514 | if (!page) { |
| 2515 | r = -ENOMEM; |
| 2516 | goto fail; |
| 2517 | } |
| 2518 | vcpu->pio_data = page_address(page); |
| 2519 | |
| 2520 | r = kvm_mmu_create(vcpu); |
| 2521 | if (r < 0) |
| 2522 | goto fail_free_pio_data; |
| 2523 | |
| 2524 | if (irqchip_in_kernel(kvm)) { |
| 2525 | r = kvm_create_lapic(vcpu); |
| 2526 | if (r < 0) |
| 2527 | goto fail_mmu_destroy; |
| 2528 | } |
| 2529 | |
| 2530 | return 0; |
| 2531 | |
| 2532 | fail_mmu_destroy: |
| 2533 | kvm_mmu_destroy(vcpu); |
| 2534 | fail_free_pio_data: |
| 2535 | free_page((unsigned long)vcpu->pio_data); |
| 2536 | fail: |
| 2537 | return r; |
| 2538 | } |
| 2539 | |
| 2540 | void kvm_arch_vcpu_uninit(struct kvm_vcpu *vcpu) |
| 2541 | { |
| 2542 | kvm_free_lapic(vcpu); |
| 2543 | kvm_mmu_destroy(vcpu); |
| 2544 | free_page((unsigned long)vcpu->pio_data); |
| 2545 | } |
Zhang Xiantao | d19a9cd | 2007-11-18 18:43:45 +0800 | [diff] [blame] | 2546 | |
| 2547 | struct kvm *kvm_arch_create_vm(void) |
| 2548 | { |
| 2549 | struct kvm *kvm = kzalloc(sizeof(struct kvm), GFP_KERNEL); |
| 2550 | |
| 2551 | if (!kvm) |
| 2552 | return ERR_PTR(-ENOMEM); |
| 2553 | |
| 2554 | INIT_LIST_HEAD(&kvm->active_mmu_pages); |
| 2555 | |
| 2556 | return kvm; |
| 2557 | } |
| 2558 | |
| 2559 | static void kvm_unload_vcpu_mmu(struct kvm_vcpu *vcpu) |
| 2560 | { |
| 2561 | vcpu_load(vcpu); |
| 2562 | kvm_mmu_unload(vcpu); |
| 2563 | vcpu_put(vcpu); |
| 2564 | } |
| 2565 | |
| 2566 | static void kvm_free_vcpus(struct kvm *kvm) |
| 2567 | { |
| 2568 | unsigned int i; |
| 2569 | |
| 2570 | /* |
| 2571 | * Unpin any mmu pages first. |
| 2572 | */ |
| 2573 | for (i = 0; i < KVM_MAX_VCPUS; ++i) |
| 2574 | if (kvm->vcpus[i]) |
| 2575 | kvm_unload_vcpu_mmu(kvm->vcpus[i]); |
| 2576 | for (i = 0; i < KVM_MAX_VCPUS; ++i) { |
| 2577 | if (kvm->vcpus[i]) { |
| 2578 | kvm_arch_vcpu_free(kvm->vcpus[i]); |
| 2579 | kvm->vcpus[i] = NULL; |
| 2580 | } |
| 2581 | } |
| 2582 | |
| 2583 | } |
| 2584 | |
| 2585 | void kvm_arch_destroy_vm(struct kvm *kvm) |
| 2586 | { |
| 2587 | kfree(kvm->vpic); |
| 2588 | kfree(kvm->vioapic); |
| 2589 | kvm_free_vcpus(kvm); |
| 2590 | kvm_free_physmem(kvm); |
| 2591 | kfree(kvm); |
| 2592 | } |