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