1 #define pr_fmt(fmt) "SVM: " fmt 2 3 #include <linux/kvm_host.h> 4 5 #include "irq.h" 6 #include "mmu.h" 7 #include "kvm_cache_regs.h" 8 #include "x86.h" 9 #include "cpuid.h" 10 #include "pmu.h" 11 12 #include <linux/module.h> 13 #include <linux/mod_devicetable.h> 14 #include <linux/kernel.h> 15 #include <linux/vmalloc.h> 16 #include <linux/highmem.h> 17 #include <linux/amd-iommu.h> 18 #include <linux/sched.h> 19 #include <linux/trace_events.h> 20 #include <linux/slab.h> 21 #include <linux/hashtable.h> 22 #include <linux/objtool.h> 23 #include <linux/psp-sev.h> 24 #include <linux/file.h> 25 #include <linux/pagemap.h> 26 #include <linux/swap.h> 27 #include <linux/rwsem.h> 28 #include <linux/cc_platform.h> 29 30 #include <asm/apic.h> 31 #include <asm/perf_event.h> 32 #include <asm/tlbflush.h> 33 #include <asm/desc.h> 34 #include <asm/debugreg.h> 35 #include <asm/kvm_para.h> 36 #include <asm/irq_remapping.h> 37 #include <asm/spec-ctrl.h> 38 #include <asm/cpu_device_id.h> 39 #include <asm/traps.h> 40 #include <asm/fpu/api.h> 41 42 #include <asm/virtext.h> 43 #include "trace.h" 44 45 #include "svm.h" 46 #include "svm_ops.h" 47 48 #include "kvm_onhyperv.h" 49 #include "svm_onhyperv.h" 50 51 MODULE_AUTHOR("Qumranet"); 52 MODULE_LICENSE("GPL"); 53 54 #ifdef MODULE 55 static const struct x86_cpu_id svm_cpu_id[] = { 56 X86_MATCH_FEATURE(X86_FEATURE_SVM, NULL), 57 {} 58 }; 59 MODULE_DEVICE_TABLE(x86cpu, svm_cpu_id); 60 #endif 61 62 #define SEG_TYPE_LDT 2 63 #define SEG_TYPE_BUSY_TSS16 3 64 65 #define SVM_FEATURE_LBRV (1 << 1) 66 #define SVM_FEATURE_SVML (1 << 2) 67 #define SVM_FEATURE_TSC_RATE (1 << 4) 68 #define SVM_FEATURE_VMCB_CLEAN (1 << 5) 69 #define SVM_FEATURE_FLUSH_ASID (1 << 6) 70 #define SVM_FEATURE_DECODE_ASSIST (1 << 7) 71 #define SVM_FEATURE_PAUSE_FILTER (1 << 10) 72 73 #define DEBUGCTL_RESERVED_BITS (~(0x3fULL)) 74 75 #define TSC_RATIO_RSVD 0xffffff0000000000ULL 76 #define TSC_RATIO_MIN 0x0000000000000001ULL 77 #define TSC_RATIO_MAX 0x000000ffffffffffULL 78 79 static bool erratum_383_found __read_mostly; 80 81 u32 msrpm_offsets[MSRPM_OFFSETS] __read_mostly; 82 83 /* 84 * Set osvw_len to higher value when updated Revision Guides 85 * are published and we know what the new status bits are 86 */ 87 static uint64_t osvw_len = 4, osvw_status; 88 89 static DEFINE_PER_CPU(u64, current_tsc_ratio); 90 #define TSC_RATIO_DEFAULT 0x0100000000ULL 91 92 static const struct svm_direct_access_msrs { 93 u32 index; /* Index of the MSR */ 94 bool always; /* True if intercept is initially cleared */ 95 } direct_access_msrs[MAX_DIRECT_ACCESS_MSRS] = { 96 { .index = MSR_STAR, .always = true }, 97 { .index = MSR_IA32_SYSENTER_CS, .always = true }, 98 { .index = MSR_IA32_SYSENTER_EIP, .always = false }, 99 { .index = MSR_IA32_SYSENTER_ESP, .always = false }, 100 #ifdef CONFIG_X86_64 101 { .index = MSR_GS_BASE, .always = true }, 102 { .index = MSR_FS_BASE, .always = true }, 103 { .index = MSR_KERNEL_GS_BASE, .always = true }, 104 { .index = MSR_LSTAR, .always = true }, 105 { .index = MSR_CSTAR, .always = true }, 106 { .index = MSR_SYSCALL_MASK, .always = true }, 107 #endif 108 { .index = MSR_IA32_SPEC_CTRL, .always = false }, 109 { .index = MSR_IA32_PRED_CMD, .always = false }, 110 { .index = MSR_IA32_LASTBRANCHFROMIP, .always = false }, 111 { .index = MSR_IA32_LASTBRANCHTOIP, .always = false }, 112 { .index = MSR_IA32_LASTINTFROMIP, .always = false }, 113 { .index = MSR_IA32_LASTINTTOIP, .always = false }, 114 { .index = MSR_EFER, .always = false }, 115 { .index = MSR_IA32_CR_PAT, .always = false }, 116 { .index = MSR_AMD64_SEV_ES_GHCB, .always = true }, 117 { .index = MSR_INVALID, .always = false }, 118 }; 119 120 /* 121 * These 2 parameters are used to config the controls for Pause-Loop Exiting: 122 * pause_filter_count: On processors that support Pause filtering(indicated 123 * by CPUID Fn8000_000A_EDX), the VMCB provides a 16 bit pause filter 124 * count value. On VMRUN this value is loaded into an internal counter. 125 * Each time a pause instruction is executed, this counter is decremented 126 * until it reaches zero at which time a #VMEXIT is generated if pause 127 * intercept is enabled. Refer to AMD APM Vol 2 Section 15.14.4 Pause 128 * Intercept Filtering for more details. 129 * This also indicate if ple logic enabled. 130 * 131 * pause_filter_thresh: In addition, some processor families support advanced 132 * pause filtering (indicated by CPUID Fn8000_000A_EDX) upper bound on 133 * the amount of time a guest is allowed to execute in a pause loop. 134 * In this mode, a 16-bit pause filter threshold field is added in the 135 * VMCB. The threshold value is a cycle count that is used to reset the 136 * pause counter. As with simple pause filtering, VMRUN loads the pause 137 * count value from VMCB into an internal counter. Then, on each pause 138 * instruction the hardware checks the elapsed number of cycles since 139 * the most recent pause instruction against the pause filter threshold. 140 * If the elapsed cycle count is greater than the pause filter threshold, 141 * then the internal pause count is reloaded from the VMCB and execution 142 * continues. If the elapsed cycle count is less than the pause filter 143 * threshold, then the internal pause count is decremented. If the count 144 * value is less than zero and PAUSE intercept is enabled, a #VMEXIT is 145 * triggered. If advanced pause filtering is supported and pause filter 146 * threshold field is set to zero, the filter will operate in the simpler, 147 * count only mode. 148 */ 149 150 static unsigned short pause_filter_thresh = KVM_DEFAULT_PLE_GAP; 151 module_param(pause_filter_thresh, ushort, 0444); 152 153 static unsigned short pause_filter_count = KVM_SVM_DEFAULT_PLE_WINDOW; 154 module_param(pause_filter_count, ushort, 0444); 155 156 /* Default doubles per-vcpu window every exit. */ 157 static unsigned short pause_filter_count_grow = KVM_DEFAULT_PLE_WINDOW_GROW; 158 module_param(pause_filter_count_grow, ushort, 0444); 159 160 /* Default resets per-vcpu window every exit to pause_filter_count. */ 161 static unsigned short pause_filter_count_shrink = KVM_DEFAULT_PLE_WINDOW_SHRINK; 162 module_param(pause_filter_count_shrink, ushort, 0444); 163 164 /* Default is to compute the maximum so we can never overflow. */ 165 static unsigned short pause_filter_count_max = KVM_SVM_DEFAULT_PLE_WINDOW_MAX; 166 module_param(pause_filter_count_max, ushort, 0444); 167 168 /* 169 * Use nested page tables by default. Note, NPT may get forced off by 170 * svm_hardware_setup() if it's unsupported by hardware or the host kernel. 171 */ 172 bool npt_enabled = true; 173 module_param_named(npt, npt_enabled, bool, 0444); 174 175 /* allow nested virtualization in KVM/SVM */ 176 static int nested = true; 177 module_param(nested, int, S_IRUGO); 178 179 /* enable/disable Next RIP Save */ 180 static int nrips = true; 181 module_param(nrips, int, 0444); 182 183 /* enable/disable Virtual VMLOAD VMSAVE */ 184 static int vls = true; 185 module_param(vls, int, 0444); 186 187 /* enable/disable Virtual GIF */ 188 static int vgif = true; 189 module_param(vgif, int, 0444); 190 191 /* enable/disable LBR virtualization */ 192 static int lbrv = true; 193 module_param(lbrv, int, 0444); 194 195 static int tsc_scaling = true; 196 module_param(tsc_scaling, int, 0444); 197 198 /* 199 * enable / disable AVIC. Because the defaults differ for APICv 200 * support between VMX and SVM we cannot use module_param_named. 201 */ 202 static bool avic; 203 module_param(avic, bool, 0444); 204 205 bool __read_mostly dump_invalid_vmcb; 206 module_param(dump_invalid_vmcb, bool, 0644); 207 208 209 bool intercept_smi = true; 210 module_param(intercept_smi, bool, 0444); 211 212 213 static bool svm_gp_erratum_intercept = true; 214 215 static u8 rsm_ins_bytes[] = "\x0f\xaa"; 216 217 static unsigned long iopm_base; 218 219 struct kvm_ldttss_desc { 220 u16 limit0; 221 u16 base0; 222 unsigned base1:8, type:5, dpl:2, p:1; 223 unsigned limit1:4, zero0:3, g:1, base2:8; 224 u32 base3; 225 u32 zero1; 226 } __attribute__((packed)); 227 228 DEFINE_PER_CPU(struct svm_cpu_data *, svm_data); 229 230 /* 231 * Only MSR_TSC_AUX is switched via the user return hook. EFER is switched via 232 * the VMCB, and the SYSCALL/SYSENTER MSRs are handled by VMLOAD/VMSAVE. 233 * 234 * RDTSCP and RDPID are not used in the kernel, specifically to allow KVM to 235 * defer the restoration of TSC_AUX until the CPU returns to userspace. 236 */ 237 static int tsc_aux_uret_slot __read_mostly = -1; 238 239 static const u32 msrpm_ranges[] = {0, 0xc0000000, 0xc0010000}; 240 241 #define NUM_MSR_MAPS ARRAY_SIZE(msrpm_ranges) 242 #define MSRS_RANGE_SIZE 2048 243 #define MSRS_IN_RANGE (MSRS_RANGE_SIZE * 8 / 2) 244 245 u32 svm_msrpm_offset(u32 msr) 246 { 247 u32 offset; 248 int i; 249 250 for (i = 0; i < NUM_MSR_MAPS; i++) { 251 if (msr < msrpm_ranges[i] || 252 msr >= msrpm_ranges[i] + MSRS_IN_RANGE) 253 continue; 254 255 offset = (msr - msrpm_ranges[i]) / 4; /* 4 msrs per u8 */ 256 offset += (i * MSRS_RANGE_SIZE); /* add range offset */ 257 258 /* Now we have the u8 offset - but need the u32 offset */ 259 return offset / 4; 260 } 261 262 /* MSR not in any range */ 263 return MSR_INVALID; 264 } 265 266 #define MAX_INST_SIZE 15 267 268 static int get_max_npt_level(void) 269 { 270 #ifdef CONFIG_X86_64 271 return pgtable_l5_enabled() ? PT64_ROOT_5LEVEL : PT64_ROOT_4LEVEL; 272 #else 273 return PT32E_ROOT_LEVEL; 274 #endif 275 } 276 277 int svm_set_efer(struct kvm_vcpu *vcpu, u64 efer) 278 { 279 struct vcpu_svm *svm = to_svm(vcpu); 280 u64 old_efer = vcpu->arch.efer; 281 vcpu->arch.efer = efer; 282 283 if (!npt_enabled) { 284 /* Shadow paging assumes NX to be available. */ 285 efer |= EFER_NX; 286 287 if (!(efer & EFER_LMA)) 288 efer &= ~EFER_LME; 289 } 290 291 if ((old_efer & EFER_SVME) != (efer & EFER_SVME)) { 292 if (!(efer & EFER_SVME)) { 293 svm_leave_nested(svm); 294 svm_set_gif(svm, true); 295 /* #GP intercept is still needed for vmware backdoor */ 296 if (!enable_vmware_backdoor) 297 clr_exception_intercept(svm, GP_VECTOR); 298 299 /* 300 * Free the nested guest state, unless we are in SMM. 301 * In this case we will return to the nested guest 302 * as soon as we leave SMM. 303 */ 304 if (!is_smm(vcpu)) 305 svm_free_nested(svm); 306 307 } else { 308 int ret = svm_allocate_nested(svm); 309 310 if (ret) { 311 vcpu->arch.efer = old_efer; 312 return ret; 313 } 314 315 if (svm_gp_erratum_intercept) 316 set_exception_intercept(svm, GP_VECTOR); 317 } 318 } 319 320 svm->vmcb->save.efer = efer | EFER_SVME; 321 vmcb_mark_dirty(svm->vmcb, VMCB_CR); 322 return 0; 323 } 324 325 static int is_external_interrupt(u32 info) 326 { 327 info &= SVM_EVTINJ_TYPE_MASK | SVM_EVTINJ_VALID; 328 return info == (SVM_EVTINJ_VALID | SVM_EVTINJ_TYPE_INTR); 329 } 330 331 static u32 svm_get_interrupt_shadow(struct kvm_vcpu *vcpu) 332 { 333 struct vcpu_svm *svm = to_svm(vcpu); 334 u32 ret = 0; 335 336 if (svm->vmcb->control.int_state & SVM_INTERRUPT_SHADOW_MASK) 337 ret = KVM_X86_SHADOW_INT_STI | KVM_X86_SHADOW_INT_MOV_SS; 338 return ret; 339 } 340 341 static void svm_set_interrupt_shadow(struct kvm_vcpu *vcpu, int mask) 342 { 343 struct vcpu_svm *svm = to_svm(vcpu); 344 345 if (mask == 0) 346 svm->vmcb->control.int_state &= ~SVM_INTERRUPT_SHADOW_MASK; 347 else 348 svm->vmcb->control.int_state |= SVM_INTERRUPT_SHADOW_MASK; 349 350 } 351 352 static int skip_emulated_instruction(struct kvm_vcpu *vcpu) 353 { 354 struct vcpu_svm *svm = to_svm(vcpu); 355 356 /* 357 * SEV-ES does not expose the next RIP. The RIP update is controlled by 358 * the type of exit and the #VC handler in the guest. 359 */ 360 if (sev_es_guest(vcpu->kvm)) 361 goto done; 362 363 if (nrips && svm->vmcb->control.next_rip != 0) { 364 WARN_ON_ONCE(!static_cpu_has(X86_FEATURE_NRIPS)); 365 svm->next_rip = svm->vmcb->control.next_rip; 366 } 367 368 if (!svm->next_rip) { 369 if (!kvm_emulate_instruction(vcpu, EMULTYPE_SKIP)) 370 return 0; 371 } else { 372 kvm_rip_write(vcpu, svm->next_rip); 373 } 374 375 done: 376 svm_set_interrupt_shadow(vcpu, 0); 377 378 return 1; 379 } 380 381 static void svm_queue_exception(struct kvm_vcpu *vcpu) 382 { 383 struct vcpu_svm *svm = to_svm(vcpu); 384 unsigned nr = vcpu->arch.exception.nr; 385 bool has_error_code = vcpu->arch.exception.has_error_code; 386 u32 error_code = vcpu->arch.exception.error_code; 387 388 kvm_deliver_exception_payload(vcpu); 389 390 if (nr == BP_VECTOR && !nrips) { 391 unsigned long rip, old_rip = kvm_rip_read(vcpu); 392 393 /* 394 * For guest debugging where we have to reinject #BP if some 395 * INT3 is guest-owned: 396 * Emulate nRIP by moving RIP forward. Will fail if injection 397 * raises a fault that is not intercepted. Still better than 398 * failing in all cases. 399 */ 400 (void)skip_emulated_instruction(vcpu); 401 rip = kvm_rip_read(vcpu); 402 svm->int3_rip = rip + svm->vmcb->save.cs.base; 403 svm->int3_injected = rip - old_rip; 404 } 405 406 svm->vmcb->control.event_inj = nr 407 | SVM_EVTINJ_VALID 408 | (has_error_code ? SVM_EVTINJ_VALID_ERR : 0) 409 | SVM_EVTINJ_TYPE_EXEPT; 410 svm->vmcb->control.event_inj_err = error_code; 411 } 412 413 static void svm_init_erratum_383(void) 414 { 415 u32 low, high; 416 int err; 417 u64 val; 418 419 if (!static_cpu_has_bug(X86_BUG_AMD_TLB_MMATCH)) 420 return; 421 422 /* Use _safe variants to not break nested virtualization */ 423 val = native_read_msr_safe(MSR_AMD64_DC_CFG, &err); 424 if (err) 425 return; 426 427 val |= (1ULL << 47); 428 429 low = lower_32_bits(val); 430 high = upper_32_bits(val); 431 432 native_write_msr_safe(MSR_AMD64_DC_CFG, low, high); 433 434 erratum_383_found = true; 435 } 436 437 static void svm_init_osvw(struct kvm_vcpu *vcpu) 438 { 439 /* 440 * Guests should see errata 400 and 415 as fixed (assuming that 441 * HLT and IO instructions are intercepted). 442 */ 443 vcpu->arch.osvw.length = (osvw_len >= 3) ? (osvw_len) : 3; 444 vcpu->arch.osvw.status = osvw_status & ~(6ULL); 445 446 /* 447 * By increasing VCPU's osvw.length to 3 we are telling the guest that 448 * all osvw.status bits inside that length, including bit 0 (which is 449 * reserved for erratum 298), are valid. However, if host processor's 450 * osvw_len is 0 then osvw_status[0] carries no information. We need to 451 * be conservative here and therefore we tell the guest that erratum 298 452 * is present (because we really don't know). 453 */ 454 if (osvw_len == 0 && boot_cpu_data.x86 == 0x10) 455 vcpu->arch.osvw.status |= 1; 456 } 457 458 static int has_svm(void) 459 { 460 const char *msg; 461 462 if (!cpu_has_svm(&msg)) { 463 printk(KERN_INFO "has_svm: %s\n", msg); 464 return 0; 465 } 466 467 if (cc_platform_has(CC_ATTR_GUEST_MEM_ENCRYPT)) { 468 pr_info("KVM is unsupported when running as an SEV guest\n"); 469 return 0; 470 } 471 472 return 1; 473 } 474 475 static void svm_hardware_disable(void) 476 { 477 /* Make sure we clean up behind us */ 478 if (tsc_scaling) 479 wrmsrl(MSR_AMD64_TSC_RATIO, TSC_RATIO_DEFAULT); 480 481 cpu_svm_disable(); 482 483 amd_pmu_disable_virt(); 484 } 485 486 static int svm_hardware_enable(void) 487 { 488 489 struct svm_cpu_data *sd; 490 uint64_t efer; 491 struct desc_struct *gdt; 492 int me = raw_smp_processor_id(); 493 494 rdmsrl(MSR_EFER, efer); 495 if (efer & EFER_SVME) 496 return -EBUSY; 497 498 if (!has_svm()) { 499 pr_err("%s: err EOPNOTSUPP on %d\n", __func__, me); 500 return -EINVAL; 501 } 502 sd = per_cpu(svm_data, me); 503 if (!sd) { 504 pr_err("%s: svm_data is NULL on %d\n", __func__, me); 505 return -EINVAL; 506 } 507 508 sd->asid_generation = 1; 509 sd->max_asid = cpuid_ebx(SVM_CPUID_FUNC) - 1; 510 sd->next_asid = sd->max_asid + 1; 511 sd->min_asid = max_sev_asid + 1; 512 513 gdt = get_current_gdt_rw(); 514 sd->tss_desc = (struct kvm_ldttss_desc *)(gdt + GDT_ENTRY_TSS); 515 516 wrmsrl(MSR_EFER, efer | EFER_SVME); 517 518 wrmsrl(MSR_VM_HSAVE_PA, __sme_page_pa(sd->save_area)); 519 520 if (static_cpu_has(X86_FEATURE_TSCRATEMSR)) { 521 /* 522 * Set the default value, even if we don't use TSC scaling 523 * to avoid having stale value in the msr 524 */ 525 wrmsrl(MSR_AMD64_TSC_RATIO, TSC_RATIO_DEFAULT); 526 __this_cpu_write(current_tsc_ratio, TSC_RATIO_DEFAULT); 527 } 528 529 530 /* 531 * Get OSVW bits. 532 * 533 * Note that it is possible to have a system with mixed processor 534 * revisions and therefore different OSVW bits. If bits are not the same 535 * on different processors then choose the worst case (i.e. if erratum 536 * is present on one processor and not on another then assume that the 537 * erratum is present everywhere). 538 */ 539 if (cpu_has(&boot_cpu_data, X86_FEATURE_OSVW)) { 540 uint64_t len, status = 0; 541 int err; 542 543 len = native_read_msr_safe(MSR_AMD64_OSVW_ID_LENGTH, &err); 544 if (!err) 545 status = native_read_msr_safe(MSR_AMD64_OSVW_STATUS, 546 &err); 547 548 if (err) 549 osvw_status = osvw_len = 0; 550 else { 551 if (len < osvw_len) 552 osvw_len = len; 553 osvw_status |= status; 554 osvw_status &= (1ULL << osvw_len) - 1; 555 } 556 } else 557 osvw_status = osvw_len = 0; 558 559 svm_init_erratum_383(); 560 561 amd_pmu_enable_virt(); 562 563 return 0; 564 } 565 566 static void svm_cpu_uninit(int cpu) 567 { 568 struct svm_cpu_data *sd = per_cpu(svm_data, cpu); 569 570 if (!sd) 571 return; 572 573 per_cpu(svm_data, cpu) = NULL; 574 kfree(sd->sev_vmcbs); 575 __free_page(sd->save_area); 576 kfree(sd); 577 } 578 579 static int svm_cpu_init(int cpu) 580 { 581 struct svm_cpu_data *sd; 582 int ret = -ENOMEM; 583 584 sd = kzalloc(sizeof(struct svm_cpu_data), GFP_KERNEL); 585 if (!sd) 586 return ret; 587 sd->cpu = cpu; 588 sd->save_area = alloc_page(GFP_KERNEL); 589 if (!sd->save_area) 590 goto free_cpu_data; 591 592 clear_page(page_address(sd->save_area)); 593 594 ret = sev_cpu_init(sd); 595 if (ret) 596 goto free_save_area; 597 598 per_cpu(svm_data, cpu) = sd; 599 600 return 0; 601 602 free_save_area: 603 __free_page(sd->save_area); 604 free_cpu_data: 605 kfree(sd); 606 return ret; 607 608 } 609 610 static int direct_access_msr_slot(u32 msr) 611 { 612 u32 i; 613 614 for (i = 0; direct_access_msrs[i].index != MSR_INVALID; i++) 615 if (direct_access_msrs[i].index == msr) 616 return i; 617 618 return -ENOENT; 619 } 620 621 static void set_shadow_msr_intercept(struct kvm_vcpu *vcpu, u32 msr, int read, 622 int write) 623 { 624 struct vcpu_svm *svm = to_svm(vcpu); 625 int slot = direct_access_msr_slot(msr); 626 627 if (slot == -ENOENT) 628 return; 629 630 /* Set the shadow bitmaps to the desired intercept states */ 631 if (read) 632 set_bit(slot, svm->shadow_msr_intercept.read); 633 else 634 clear_bit(slot, svm->shadow_msr_intercept.read); 635 636 if (write) 637 set_bit(slot, svm->shadow_msr_intercept.write); 638 else 639 clear_bit(slot, svm->shadow_msr_intercept.write); 640 } 641 642 static bool valid_msr_intercept(u32 index) 643 { 644 return direct_access_msr_slot(index) != -ENOENT; 645 } 646 647 static bool msr_write_intercepted(struct kvm_vcpu *vcpu, u32 msr) 648 { 649 u8 bit_write; 650 unsigned long tmp; 651 u32 offset; 652 u32 *msrpm; 653 654 msrpm = is_guest_mode(vcpu) ? to_svm(vcpu)->nested.msrpm: 655 to_svm(vcpu)->msrpm; 656 657 offset = svm_msrpm_offset(msr); 658 bit_write = 2 * (msr & 0x0f) + 1; 659 tmp = msrpm[offset]; 660 661 BUG_ON(offset == MSR_INVALID); 662 663 return !!test_bit(bit_write, &tmp); 664 } 665 666 static void set_msr_interception_bitmap(struct kvm_vcpu *vcpu, u32 *msrpm, 667 u32 msr, int read, int write) 668 { 669 u8 bit_read, bit_write; 670 unsigned long tmp; 671 u32 offset; 672 673 /* 674 * If this warning triggers extend the direct_access_msrs list at the 675 * beginning of the file 676 */ 677 WARN_ON(!valid_msr_intercept(msr)); 678 679 /* Enforce non allowed MSRs to trap */ 680 if (read && !kvm_msr_allowed(vcpu, msr, KVM_MSR_FILTER_READ)) 681 read = 0; 682 683 if (write && !kvm_msr_allowed(vcpu, msr, KVM_MSR_FILTER_WRITE)) 684 write = 0; 685 686 offset = svm_msrpm_offset(msr); 687 bit_read = 2 * (msr & 0x0f); 688 bit_write = 2 * (msr & 0x0f) + 1; 689 tmp = msrpm[offset]; 690 691 BUG_ON(offset == MSR_INVALID); 692 693 read ? clear_bit(bit_read, &tmp) : set_bit(bit_read, &tmp); 694 write ? clear_bit(bit_write, &tmp) : set_bit(bit_write, &tmp); 695 696 msrpm[offset] = tmp; 697 698 svm_hv_vmcb_dirty_nested_enlightenments(vcpu); 699 700 } 701 702 void set_msr_interception(struct kvm_vcpu *vcpu, u32 *msrpm, u32 msr, 703 int read, int write) 704 { 705 set_shadow_msr_intercept(vcpu, msr, read, write); 706 set_msr_interception_bitmap(vcpu, msrpm, msr, read, write); 707 } 708 709 u32 *svm_vcpu_alloc_msrpm(void) 710 { 711 unsigned int order = get_order(MSRPM_SIZE); 712 struct page *pages = alloc_pages(GFP_KERNEL_ACCOUNT, order); 713 u32 *msrpm; 714 715 if (!pages) 716 return NULL; 717 718 msrpm = page_address(pages); 719 memset(msrpm, 0xff, PAGE_SIZE * (1 << order)); 720 721 return msrpm; 722 } 723 724 void svm_vcpu_init_msrpm(struct kvm_vcpu *vcpu, u32 *msrpm) 725 { 726 int i; 727 728 for (i = 0; direct_access_msrs[i].index != MSR_INVALID; i++) { 729 if (!direct_access_msrs[i].always) 730 continue; 731 set_msr_interception(vcpu, msrpm, direct_access_msrs[i].index, 1, 1); 732 } 733 } 734 735 736 void svm_vcpu_free_msrpm(u32 *msrpm) 737 { 738 __free_pages(virt_to_page(msrpm), get_order(MSRPM_SIZE)); 739 } 740 741 static void svm_msr_filter_changed(struct kvm_vcpu *vcpu) 742 { 743 struct vcpu_svm *svm = to_svm(vcpu); 744 u32 i; 745 746 /* 747 * Set intercept permissions for all direct access MSRs again. They 748 * will automatically get filtered through the MSR filter, so we are 749 * back in sync after this. 750 */ 751 for (i = 0; direct_access_msrs[i].index != MSR_INVALID; i++) { 752 u32 msr = direct_access_msrs[i].index; 753 u32 read = test_bit(i, svm->shadow_msr_intercept.read); 754 u32 write = test_bit(i, svm->shadow_msr_intercept.write); 755 756 set_msr_interception_bitmap(vcpu, svm->msrpm, msr, read, write); 757 } 758 } 759 760 static void add_msr_offset(u32 offset) 761 { 762 int i; 763 764 for (i = 0; i < MSRPM_OFFSETS; ++i) { 765 766 /* Offset already in list? */ 767 if (msrpm_offsets[i] == offset) 768 return; 769 770 /* Slot used by another offset? */ 771 if (msrpm_offsets[i] != MSR_INVALID) 772 continue; 773 774 /* Add offset to list */ 775 msrpm_offsets[i] = offset; 776 777 return; 778 } 779 780 /* 781 * If this BUG triggers the msrpm_offsets table has an overflow. Just 782 * increase MSRPM_OFFSETS in this case. 783 */ 784 BUG(); 785 } 786 787 static void init_msrpm_offsets(void) 788 { 789 int i; 790 791 memset(msrpm_offsets, 0xff, sizeof(msrpm_offsets)); 792 793 for (i = 0; direct_access_msrs[i].index != MSR_INVALID; i++) { 794 u32 offset; 795 796 offset = svm_msrpm_offset(direct_access_msrs[i].index); 797 BUG_ON(offset == MSR_INVALID); 798 799 add_msr_offset(offset); 800 } 801 } 802 803 static void svm_enable_lbrv(struct kvm_vcpu *vcpu) 804 { 805 struct vcpu_svm *svm = to_svm(vcpu); 806 807 svm->vmcb->control.virt_ext |= LBR_CTL_ENABLE_MASK; 808 set_msr_interception(vcpu, svm->msrpm, MSR_IA32_LASTBRANCHFROMIP, 1, 1); 809 set_msr_interception(vcpu, svm->msrpm, MSR_IA32_LASTBRANCHTOIP, 1, 1); 810 set_msr_interception(vcpu, svm->msrpm, MSR_IA32_LASTINTFROMIP, 1, 1); 811 set_msr_interception(vcpu, svm->msrpm, MSR_IA32_LASTINTTOIP, 1, 1); 812 } 813 814 static void svm_disable_lbrv(struct kvm_vcpu *vcpu) 815 { 816 struct vcpu_svm *svm = to_svm(vcpu); 817 818 svm->vmcb->control.virt_ext &= ~LBR_CTL_ENABLE_MASK; 819 set_msr_interception(vcpu, svm->msrpm, MSR_IA32_LASTBRANCHFROMIP, 0, 0); 820 set_msr_interception(vcpu, svm->msrpm, MSR_IA32_LASTBRANCHTOIP, 0, 0); 821 set_msr_interception(vcpu, svm->msrpm, MSR_IA32_LASTINTFROMIP, 0, 0); 822 set_msr_interception(vcpu, svm->msrpm, MSR_IA32_LASTINTTOIP, 0, 0); 823 } 824 825 void disable_nmi_singlestep(struct vcpu_svm *svm) 826 { 827 svm->nmi_singlestep = false; 828 829 if (!(svm->vcpu.guest_debug & KVM_GUESTDBG_SINGLESTEP)) { 830 /* Clear our flags if they were not set by the guest */ 831 if (!(svm->nmi_singlestep_guest_rflags & X86_EFLAGS_TF)) 832 svm->vmcb->save.rflags &= ~X86_EFLAGS_TF; 833 if (!(svm->nmi_singlestep_guest_rflags & X86_EFLAGS_RF)) 834 svm->vmcb->save.rflags &= ~X86_EFLAGS_RF; 835 } 836 } 837 838 static void grow_ple_window(struct kvm_vcpu *vcpu) 839 { 840 struct vcpu_svm *svm = to_svm(vcpu); 841 struct vmcb_control_area *control = &svm->vmcb->control; 842 int old = control->pause_filter_count; 843 844 control->pause_filter_count = __grow_ple_window(old, 845 pause_filter_count, 846 pause_filter_count_grow, 847 pause_filter_count_max); 848 849 if (control->pause_filter_count != old) { 850 vmcb_mark_dirty(svm->vmcb, VMCB_INTERCEPTS); 851 trace_kvm_ple_window_update(vcpu->vcpu_id, 852 control->pause_filter_count, old); 853 } 854 } 855 856 static void shrink_ple_window(struct kvm_vcpu *vcpu) 857 { 858 struct vcpu_svm *svm = to_svm(vcpu); 859 struct vmcb_control_area *control = &svm->vmcb->control; 860 int old = control->pause_filter_count; 861 862 control->pause_filter_count = 863 __shrink_ple_window(old, 864 pause_filter_count, 865 pause_filter_count_shrink, 866 pause_filter_count); 867 if (control->pause_filter_count != old) { 868 vmcb_mark_dirty(svm->vmcb, VMCB_INTERCEPTS); 869 trace_kvm_ple_window_update(vcpu->vcpu_id, 870 control->pause_filter_count, old); 871 } 872 } 873 874 /* 875 * The default MMIO mask is a single bit (excluding the present bit), 876 * which could conflict with the memory encryption bit. Check for 877 * memory encryption support and override the default MMIO mask if 878 * memory encryption is enabled. 879 */ 880 static __init void svm_adjust_mmio_mask(void) 881 { 882 unsigned int enc_bit, mask_bit; 883 u64 msr, mask; 884 885 /* If there is no memory encryption support, use existing mask */ 886 if (cpuid_eax(0x80000000) < 0x8000001f) 887 return; 888 889 /* If memory encryption is not enabled, use existing mask */ 890 rdmsrl(MSR_AMD64_SYSCFG, msr); 891 if (!(msr & MSR_AMD64_SYSCFG_MEM_ENCRYPT)) 892 return; 893 894 enc_bit = cpuid_ebx(0x8000001f) & 0x3f; 895 mask_bit = boot_cpu_data.x86_phys_bits; 896 897 /* Increment the mask bit if it is the same as the encryption bit */ 898 if (enc_bit == mask_bit) 899 mask_bit++; 900 901 /* 902 * If the mask bit location is below 52, then some bits above the 903 * physical addressing limit will always be reserved, so use the 904 * rsvd_bits() function to generate the mask. This mask, along with 905 * the present bit, will be used to generate a page fault with 906 * PFER.RSV = 1. 907 * 908 * If the mask bit location is 52 (or above), then clear the mask. 909 */ 910 mask = (mask_bit < 52) ? rsvd_bits(mask_bit, 51) | PT_PRESENT_MASK : 0; 911 912 kvm_mmu_set_mmio_spte_mask(mask, mask, PT_WRITABLE_MASK | PT_USER_MASK); 913 } 914 915 static void svm_hardware_teardown(void) 916 { 917 int cpu; 918 919 sev_hardware_teardown(); 920 921 for_each_possible_cpu(cpu) 922 svm_cpu_uninit(cpu); 923 924 __free_pages(pfn_to_page(iopm_base >> PAGE_SHIFT), 925 get_order(IOPM_SIZE)); 926 iopm_base = 0; 927 } 928 929 static __init void svm_set_cpu_caps(void) 930 { 931 kvm_set_cpu_caps(); 932 933 supported_xss = 0; 934 935 /* CPUID 0x80000001 and 0x8000000A (SVM features) */ 936 if (nested) { 937 kvm_cpu_cap_set(X86_FEATURE_SVM); 938 939 if (nrips) 940 kvm_cpu_cap_set(X86_FEATURE_NRIPS); 941 942 if (npt_enabled) 943 kvm_cpu_cap_set(X86_FEATURE_NPT); 944 945 if (tsc_scaling) 946 kvm_cpu_cap_set(X86_FEATURE_TSCRATEMSR); 947 948 /* Nested VM can receive #VMEXIT instead of triggering #GP */ 949 kvm_cpu_cap_set(X86_FEATURE_SVME_ADDR_CHK); 950 } 951 952 /* CPUID 0x80000008 */ 953 if (boot_cpu_has(X86_FEATURE_LS_CFG_SSBD) || 954 boot_cpu_has(X86_FEATURE_AMD_SSBD)) 955 kvm_cpu_cap_set(X86_FEATURE_VIRT_SSBD); 956 957 /* CPUID 0x8000001F (SME/SEV features) */ 958 sev_set_cpu_caps(); 959 } 960 961 static __init int svm_hardware_setup(void) 962 { 963 int cpu; 964 struct page *iopm_pages; 965 void *iopm_va; 966 int r; 967 unsigned int order = get_order(IOPM_SIZE); 968 969 /* 970 * NX is required for shadow paging and for NPT if the NX huge pages 971 * mitigation is enabled. 972 */ 973 if (!boot_cpu_has(X86_FEATURE_NX)) { 974 pr_err_ratelimited("NX (Execute Disable) not supported\n"); 975 return -EOPNOTSUPP; 976 } 977 kvm_enable_efer_bits(EFER_NX); 978 979 iopm_pages = alloc_pages(GFP_KERNEL, order); 980 981 if (!iopm_pages) 982 return -ENOMEM; 983 984 iopm_va = page_address(iopm_pages); 985 memset(iopm_va, 0xff, PAGE_SIZE * (1 << order)); 986 iopm_base = page_to_pfn(iopm_pages) << PAGE_SHIFT; 987 988 init_msrpm_offsets(); 989 990 supported_xcr0 &= ~(XFEATURE_MASK_BNDREGS | XFEATURE_MASK_BNDCSR); 991 992 if (boot_cpu_has(X86_FEATURE_FXSR_OPT)) 993 kvm_enable_efer_bits(EFER_FFXSR); 994 995 if (tsc_scaling) { 996 if (!boot_cpu_has(X86_FEATURE_TSCRATEMSR)) { 997 tsc_scaling = false; 998 } else { 999 pr_info("TSC scaling supported\n"); 1000 kvm_has_tsc_control = true; 1001 kvm_max_tsc_scaling_ratio = TSC_RATIO_MAX; 1002 kvm_tsc_scaling_ratio_frac_bits = 32; 1003 } 1004 } 1005 1006 tsc_aux_uret_slot = kvm_add_user_return_msr(MSR_TSC_AUX); 1007 1008 /* Check for pause filtering support */ 1009 if (!boot_cpu_has(X86_FEATURE_PAUSEFILTER)) { 1010 pause_filter_count = 0; 1011 pause_filter_thresh = 0; 1012 } else if (!boot_cpu_has(X86_FEATURE_PFTHRESHOLD)) { 1013 pause_filter_thresh = 0; 1014 } 1015 1016 if (nested) { 1017 printk(KERN_INFO "kvm: Nested Virtualization enabled\n"); 1018 kvm_enable_efer_bits(EFER_SVME | EFER_LMSLE); 1019 } 1020 1021 /* 1022 * KVM's MMU doesn't support using 2-level paging for itself, and thus 1023 * NPT isn't supported if the host is using 2-level paging since host 1024 * CR4 is unchanged on VMRUN. 1025 */ 1026 if (!IS_ENABLED(CONFIG_X86_64) && !IS_ENABLED(CONFIG_X86_PAE)) 1027 npt_enabled = false; 1028 1029 if (!boot_cpu_has(X86_FEATURE_NPT)) 1030 npt_enabled = false; 1031 1032 /* Force VM NPT level equal to the host's max NPT level */ 1033 kvm_configure_mmu(npt_enabled, get_max_npt_level(), 1034 get_max_npt_level(), PG_LEVEL_1G); 1035 pr_info("kvm: Nested Paging %sabled\n", npt_enabled ? "en" : "dis"); 1036 1037 /* Note, SEV setup consumes npt_enabled. */ 1038 sev_hardware_setup(); 1039 1040 svm_hv_hardware_setup(); 1041 1042 svm_adjust_mmio_mask(); 1043 1044 for_each_possible_cpu(cpu) { 1045 r = svm_cpu_init(cpu); 1046 if (r) 1047 goto err; 1048 } 1049 1050 if (nrips) { 1051 if (!boot_cpu_has(X86_FEATURE_NRIPS)) 1052 nrips = false; 1053 } 1054 1055 enable_apicv = avic = avic && npt_enabled && boot_cpu_has(X86_FEATURE_AVIC); 1056 1057 if (enable_apicv) { 1058 pr_info("AVIC enabled\n"); 1059 1060 amd_iommu_register_ga_log_notifier(&avic_ga_log_notifier); 1061 } 1062 1063 if (vls) { 1064 if (!npt_enabled || 1065 !boot_cpu_has(X86_FEATURE_V_VMSAVE_VMLOAD) || 1066 !IS_ENABLED(CONFIG_X86_64)) { 1067 vls = false; 1068 } else { 1069 pr_info("Virtual VMLOAD VMSAVE supported\n"); 1070 } 1071 } 1072 1073 if (boot_cpu_has(X86_FEATURE_SVME_ADDR_CHK)) 1074 svm_gp_erratum_intercept = false; 1075 1076 if (vgif) { 1077 if (!boot_cpu_has(X86_FEATURE_VGIF)) 1078 vgif = false; 1079 else 1080 pr_info("Virtual GIF supported\n"); 1081 } 1082 1083 if (lbrv) { 1084 if (!boot_cpu_has(X86_FEATURE_LBRV)) 1085 lbrv = false; 1086 else 1087 pr_info("LBR virtualization supported\n"); 1088 } 1089 1090 svm_set_cpu_caps(); 1091 1092 /* 1093 * It seems that on AMD processors PTE's accessed bit is 1094 * being set by the CPU hardware before the NPF vmexit. 1095 * This is not expected behaviour and our tests fail because 1096 * of it. 1097 * A workaround here is to disable support for 1098 * GUEST_MAXPHYADDR < HOST_MAXPHYADDR if NPT is enabled. 1099 * In this case userspace can know if there is support using 1100 * KVM_CAP_SMALLER_MAXPHYADDR extension and decide how to handle 1101 * it 1102 * If future AMD CPU models change the behaviour described above, 1103 * this variable can be changed accordingly 1104 */ 1105 allow_smaller_maxphyaddr = !npt_enabled; 1106 1107 return 0; 1108 1109 err: 1110 svm_hardware_teardown(); 1111 return r; 1112 } 1113 1114 static void init_seg(struct vmcb_seg *seg) 1115 { 1116 seg->selector = 0; 1117 seg->attrib = SVM_SELECTOR_P_MASK | SVM_SELECTOR_S_MASK | 1118 SVM_SELECTOR_WRITE_MASK; /* Read/Write Data Segment */ 1119 seg->limit = 0xffff; 1120 seg->base = 0; 1121 } 1122 1123 static void init_sys_seg(struct vmcb_seg *seg, uint32_t type) 1124 { 1125 seg->selector = 0; 1126 seg->attrib = SVM_SELECTOR_P_MASK | type; 1127 seg->limit = 0xffff; 1128 seg->base = 0; 1129 } 1130 1131 static u64 svm_get_l2_tsc_offset(struct kvm_vcpu *vcpu) 1132 { 1133 struct vcpu_svm *svm = to_svm(vcpu); 1134 1135 return svm->nested.ctl.tsc_offset; 1136 } 1137 1138 static u64 svm_get_l2_tsc_multiplier(struct kvm_vcpu *vcpu) 1139 { 1140 struct vcpu_svm *svm = to_svm(vcpu); 1141 1142 return svm->tsc_ratio_msr; 1143 } 1144 1145 static void svm_write_tsc_offset(struct kvm_vcpu *vcpu, u64 offset) 1146 { 1147 struct vcpu_svm *svm = to_svm(vcpu); 1148 1149 svm->vmcb01.ptr->control.tsc_offset = vcpu->arch.l1_tsc_offset; 1150 svm->vmcb->control.tsc_offset = offset; 1151 vmcb_mark_dirty(svm->vmcb, VMCB_INTERCEPTS); 1152 } 1153 1154 void svm_write_tsc_multiplier(struct kvm_vcpu *vcpu, u64 multiplier) 1155 { 1156 wrmsrl(MSR_AMD64_TSC_RATIO, multiplier); 1157 } 1158 1159 /* Evaluate instruction intercepts that depend on guest CPUID features. */ 1160 static void svm_recalc_instruction_intercepts(struct kvm_vcpu *vcpu, 1161 struct vcpu_svm *svm) 1162 { 1163 /* 1164 * Intercept INVPCID if shadow paging is enabled to sync/free shadow 1165 * roots, or if INVPCID is disabled in the guest to inject #UD. 1166 */ 1167 if (kvm_cpu_cap_has(X86_FEATURE_INVPCID)) { 1168 if (!npt_enabled || 1169 !guest_cpuid_has(&svm->vcpu, X86_FEATURE_INVPCID)) 1170 svm_set_intercept(svm, INTERCEPT_INVPCID); 1171 else 1172 svm_clr_intercept(svm, INTERCEPT_INVPCID); 1173 } 1174 1175 if (kvm_cpu_cap_has(X86_FEATURE_RDTSCP)) { 1176 if (guest_cpuid_has(vcpu, X86_FEATURE_RDTSCP)) 1177 svm_clr_intercept(svm, INTERCEPT_RDTSCP); 1178 else 1179 svm_set_intercept(svm, INTERCEPT_RDTSCP); 1180 } 1181 } 1182 1183 static inline void init_vmcb_after_set_cpuid(struct kvm_vcpu *vcpu) 1184 { 1185 struct vcpu_svm *svm = to_svm(vcpu); 1186 1187 if (guest_cpuid_is_intel(vcpu)) { 1188 /* 1189 * We must intercept SYSENTER_EIP and SYSENTER_ESP 1190 * accesses because the processor only stores 32 bits. 1191 * For the same reason we cannot use virtual VMLOAD/VMSAVE. 1192 */ 1193 svm_set_intercept(svm, INTERCEPT_VMLOAD); 1194 svm_set_intercept(svm, INTERCEPT_VMSAVE); 1195 svm->vmcb->control.virt_ext &= ~VIRTUAL_VMLOAD_VMSAVE_ENABLE_MASK; 1196 1197 set_msr_interception(vcpu, svm->msrpm, MSR_IA32_SYSENTER_EIP, 0, 0); 1198 set_msr_interception(vcpu, svm->msrpm, MSR_IA32_SYSENTER_ESP, 0, 0); 1199 } else { 1200 /* 1201 * If hardware supports Virtual VMLOAD VMSAVE then enable it 1202 * in VMCB and clear intercepts to avoid #VMEXIT. 1203 */ 1204 if (vls) { 1205 svm_clr_intercept(svm, INTERCEPT_VMLOAD); 1206 svm_clr_intercept(svm, INTERCEPT_VMSAVE); 1207 svm->vmcb->control.virt_ext |= VIRTUAL_VMLOAD_VMSAVE_ENABLE_MASK; 1208 } 1209 /* No need to intercept these MSRs */ 1210 set_msr_interception(vcpu, svm->msrpm, MSR_IA32_SYSENTER_EIP, 1, 1); 1211 set_msr_interception(vcpu, svm->msrpm, MSR_IA32_SYSENTER_ESP, 1, 1); 1212 } 1213 } 1214 1215 static void init_vmcb(struct kvm_vcpu *vcpu) 1216 { 1217 struct vcpu_svm *svm = to_svm(vcpu); 1218 struct vmcb_control_area *control = &svm->vmcb->control; 1219 struct vmcb_save_area *save = &svm->vmcb->save; 1220 1221 svm_set_intercept(svm, INTERCEPT_CR0_READ); 1222 svm_set_intercept(svm, INTERCEPT_CR3_READ); 1223 svm_set_intercept(svm, INTERCEPT_CR4_READ); 1224 svm_set_intercept(svm, INTERCEPT_CR0_WRITE); 1225 svm_set_intercept(svm, INTERCEPT_CR3_WRITE); 1226 svm_set_intercept(svm, INTERCEPT_CR4_WRITE); 1227 if (!kvm_vcpu_apicv_active(vcpu)) 1228 svm_set_intercept(svm, INTERCEPT_CR8_WRITE); 1229 1230 set_dr_intercepts(svm); 1231 1232 set_exception_intercept(svm, PF_VECTOR); 1233 set_exception_intercept(svm, UD_VECTOR); 1234 set_exception_intercept(svm, MC_VECTOR); 1235 set_exception_intercept(svm, AC_VECTOR); 1236 set_exception_intercept(svm, DB_VECTOR); 1237 /* 1238 * Guest access to VMware backdoor ports could legitimately 1239 * trigger #GP because of TSS I/O permission bitmap. 1240 * We intercept those #GP and allow access to them anyway 1241 * as VMware does. 1242 */ 1243 if (enable_vmware_backdoor) 1244 set_exception_intercept(svm, GP_VECTOR); 1245 1246 svm_set_intercept(svm, INTERCEPT_INTR); 1247 svm_set_intercept(svm, INTERCEPT_NMI); 1248 1249 if (intercept_smi) 1250 svm_set_intercept(svm, INTERCEPT_SMI); 1251 1252 svm_set_intercept(svm, INTERCEPT_SELECTIVE_CR0); 1253 svm_set_intercept(svm, INTERCEPT_RDPMC); 1254 svm_set_intercept(svm, INTERCEPT_CPUID); 1255 svm_set_intercept(svm, INTERCEPT_INVD); 1256 svm_set_intercept(svm, INTERCEPT_INVLPG); 1257 svm_set_intercept(svm, INTERCEPT_INVLPGA); 1258 svm_set_intercept(svm, INTERCEPT_IOIO_PROT); 1259 svm_set_intercept(svm, INTERCEPT_MSR_PROT); 1260 svm_set_intercept(svm, INTERCEPT_TASK_SWITCH); 1261 svm_set_intercept(svm, INTERCEPT_SHUTDOWN); 1262 svm_set_intercept(svm, INTERCEPT_VMRUN); 1263 svm_set_intercept(svm, INTERCEPT_VMMCALL); 1264 svm_set_intercept(svm, INTERCEPT_VMLOAD); 1265 svm_set_intercept(svm, INTERCEPT_VMSAVE); 1266 svm_set_intercept(svm, INTERCEPT_STGI); 1267 svm_set_intercept(svm, INTERCEPT_CLGI); 1268 svm_set_intercept(svm, INTERCEPT_SKINIT); 1269 svm_set_intercept(svm, INTERCEPT_WBINVD); 1270 svm_set_intercept(svm, INTERCEPT_XSETBV); 1271 svm_set_intercept(svm, INTERCEPT_RDPRU); 1272 svm_set_intercept(svm, INTERCEPT_RSM); 1273 1274 if (!kvm_mwait_in_guest(vcpu->kvm)) { 1275 svm_set_intercept(svm, INTERCEPT_MONITOR); 1276 svm_set_intercept(svm, INTERCEPT_MWAIT); 1277 } 1278 1279 if (!kvm_hlt_in_guest(vcpu->kvm)) 1280 svm_set_intercept(svm, INTERCEPT_HLT); 1281 1282 control->iopm_base_pa = __sme_set(iopm_base); 1283 control->msrpm_base_pa = __sme_set(__pa(svm->msrpm)); 1284 control->int_ctl = V_INTR_MASKING_MASK; 1285 1286 init_seg(&save->es); 1287 init_seg(&save->ss); 1288 init_seg(&save->ds); 1289 init_seg(&save->fs); 1290 init_seg(&save->gs); 1291 1292 save->cs.selector = 0xf000; 1293 save->cs.base = 0xffff0000; 1294 /* Executable/Readable Code Segment */ 1295 save->cs.attrib = SVM_SELECTOR_READ_MASK | SVM_SELECTOR_P_MASK | 1296 SVM_SELECTOR_S_MASK | SVM_SELECTOR_CODE_MASK; 1297 save->cs.limit = 0xffff; 1298 1299 save->gdtr.base = 0; 1300 save->gdtr.limit = 0xffff; 1301 save->idtr.base = 0; 1302 save->idtr.limit = 0xffff; 1303 1304 init_sys_seg(&save->ldtr, SEG_TYPE_LDT); 1305 init_sys_seg(&save->tr, SEG_TYPE_BUSY_TSS16); 1306 1307 if (npt_enabled) { 1308 /* Setup VMCB for Nested Paging */ 1309 control->nested_ctl |= SVM_NESTED_CTL_NP_ENABLE; 1310 svm_clr_intercept(svm, INTERCEPT_INVLPG); 1311 clr_exception_intercept(svm, PF_VECTOR); 1312 svm_clr_intercept(svm, INTERCEPT_CR3_READ); 1313 svm_clr_intercept(svm, INTERCEPT_CR3_WRITE); 1314 save->g_pat = vcpu->arch.pat; 1315 save->cr3 = 0; 1316 } 1317 svm->current_vmcb->asid_generation = 0; 1318 svm->asid = 0; 1319 1320 svm->nested.vmcb12_gpa = INVALID_GPA; 1321 svm->nested.last_vmcb12_gpa = INVALID_GPA; 1322 1323 if (!kvm_pause_in_guest(vcpu->kvm)) { 1324 control->pause_filter_count = pause_filter_count; 1325 if (pause_filter_thresh) 1326 control->pause_filter_thresh = pause_filter_thresh; 1327 svm_set_intercept(svm, INTERCEPT_PAUSE); 1328 } else { 1329 svm_clr_intercept(svm, INTERCEPT_PAUSE); 1330 } 1331 1332 svm_recalc_instruction_intercepts(vcpu, svm); 1333 1334 /* 1335 * If the host supports V_SPEC_CTRL then disable the interception 1336 * of MSR_IA32_SPEC_CTRL. 1337 */ 1338 if (boot_cpu_has(X86_FEATURE_V_SPEC_CTRL)) 1339 set_msr_interception(vcpu, svm->msrpm, MSR_IA32_SPEC_CTRL, 1, 1); 1340 1341 if (kvm_vcpu_apicv_active(vcpu)) 1342 avic_init_vmcb(svm); 1343 1344 if (vgif) { 1345 svm_clr_intercept(svm, INTERCEPT_STGI); 1346 svm_clr_intercept(svm, INTERCEPT_CLGI); 1347 svm->vmcb->control.int_ctl |= V_GIF_ENABLE_MASK; 1348 } 1349 1350 if (sev_guest(vcpu->kvm)) { 1351 svm->vmcb->control.nested_ctl |= SVM_NESTED_CTL_SEV_ENABLE; 1352 clr_exception_intercept(svm, UD_VECTOR); 1353 1354 if (sev_es_guest(vcpu->kvm)) { 1355 /* Perform SEV-ES specific VMCB updates */ 1356 sev_es_init_vmcb(svm); 1357 } 1358 } 1359 1360 svm_hv_init_vmcb(svm->vmcb); 1361 init_vmcb_after_set_cpuid(vcpu); 1362 1363 vmcb_mark_all_dirty(svm->vmcb); 1364 1365 enable_gif(svm); 1366 } 1367 1368 static void __svm_vcpu_reset(struct kvm_vcpu *vcpu) 1369 { 1370 struct vcpu_svm *svm = to_svm(vcpu); 1371 1372 svm_vcpu_init_msrpm(vcpu, svm->msrpm); 1373 1374 svm_init_osvw(vcpu); 1375 vcpu->arch.microcode_version = 0x01000065; 1376 svm->tsc_ratio_msr = kvm_default_tsc_scaling_ratio; 1377 1378 if (sev_es_guest(vcpu->kvm)) 1379 sev_es_vcpu_reset(svm); 1380 } 1381 1382 static void svm_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event) 1383 { 1384 struct vcpu_svm *svm = to_svm(vcpu); 1385 1386 svm->spec_ctrl = 0; 1387 svm->virt_spec_ctrl = 0; 1388 1389 init_vmcb(vcpu); 1390 1391 if (!init_event) 1392 __svm_vcpu_reset(vcpu); 1393 } 1394 1395 void svm_switch_vmcb(struct vcpu_svm *svm, struct kvm_vmcb_info *target_vmcb) 1396 { 1397 svm->current_vmcb = target_vmcb; 1398 svm->vmcb = target_vmcb->ptr; 1399 } 1400 1401 static int svm_create_vcpu(struct kvm_vcpu *vcpu) 1402 { 1403 struct vcpu_svm *svm; 1404 struct page *vmcb01_page; 1405 struct page *vmsa_page = NULL; 1406 int err; 1407 1408 BUILD_BUG_ON(offsetof(struct vcpu_svm, vcpu) != 0); 1409 svm = to_svm(vcpu); 1410 1411 err = -ENOMEM; 1412 vmcb01_page = alloc_page(GFP_KERNEL_ACCOUNT | __GFP_ZERO); 1413 if (!vmcb01_page) 1414 goto out; 1415 1416 if (sev_es_guest(vcpu->kvm)) { 1417 /* 1418 * SEV-ES guests require a separate VMSA page used to contain 1419 * the encrypted register state of the guest. 1420 */ 1421 vmsa_page = alloc_page(GFP_KERNEL_ACCOUNT | __GFP_ZERO); 1422 if (!vmsa_page) 1423 goto error_free_vmcb_page; 1424 1425 /* 1426 * SEV-ES guests maintain an encrypted version of their FPU 1427 * state which is restored and saved on VMRUN and VMEXIT. 1428 * Mark vcpu->arch.guest_fpu->fpstate as scratch so it won't 1429 * do xsave/xrstor on it. 1430 */ 1431 fpstate_set_confidential(&vcpu->arch.guest_fpu); 1432 } 1433 1434 err = avic_init_vcpu(svm); 1435 if (err) 1436 goto error_free_vmsa_page; 1437 1438 /* We initialize this flag to true to make sure that the is_running 1439 * bit would be set the first time the vcpu is loaded. 1440 */ 1441 if (irqchip_in_kernel(vcpu->kvm) && kvm_apicv_activated(vcpu->kvm)) 1442 svm->avic_is_running = true; 1443 1444 svm->msrpm = svm_vcpu_alloc_msrpm(); 1445 if (!svm->msrpm) { 1446 err = -ENOMEM; 1447 goto error_free_vmsa_page; 1448 } 1449 1450 svm->vmcb01.ptr = page_address(vmcb01_page); 1451 svm->vmcb01.pa = __sme_set(page_to_pfn(vmcb01_page) << PAGE_SHIFT); 1452 svm_switch_vmcb(svm, &svm->vmcb01); 1453 1454 if (vmsa_page) 1455 svm->sev_es.vmsa = page_address(vmsa_page); 1456 1457 svm->guest_state_loaded = false; 1458 1459 return 0; 1460 1461 error_free_vmsa_page: 1462 if (vmsa_page) 1463 __free_page(vmsa_page); 1464 error_free_vmcb_page: 1465 __free_page(vmcb01_page); 1466 out: 1467 return err; 1468 } 1469 1470 static void svm_clear_current_vmcb(struct vmcb *vmcb) 1471 { 1472 int i; 1473 1474 for_each_online_cpu(i) 1475 cmpxchg(&per_cpu(svm_data, i)->current_vmcb, vmcb, NULL); 1476 } 1477 1478 static void svm_free_vcpu(struct kvm_vcpu *vcpu) 1479 { 1480 struct vcpu_svm *svm = to_svm(vcpu); 1481 1482 /* 1483 * The vmcb page can be recycled, causing a false negative in 1484 * svm_vcpu_load(). So, ensure that no logical CPU has this 1485 * vmcb page recorded as its current vmcb. 1486 */ 1487 svm_clear_current_vmcb(svm->vmcb); 1488 1489 svm_free_nested(svm); 1490 1491 sev_free_vcpu(vcpu); 1492 1493 __free_page(pfn_to_page(__sme_clr(svm->vmcb01.pa) >> PAGE_SHIFT)); 1494 __free_pages(virt_to_page(svm->msrpm), get_order(MSRPM_SIZE)); 1495 } 1496 1497 static void svm_prepare_guest_switch(struct kvm_vcpu *vcpu) 1498 { 1499 struct vcpu_svm *svm = to_svm(vcpu); 1500 struct svm_cpu_data *sd = per_cpu(svm_data, vcpu->cpu); 1501 1502 if (sev_es_guest(vcpu->kvm)) 1503 sev_es_unmap_ghcb(svm); 1504 1505 if (svm->guest_state_loaded) 1506 return; 1507 1508 /* 1509 * Save additional host state that will be restored on VMEXIT (sev-es) 1510 * or subsequent vmload of host save area. 1511 */ 1512 if (sev_es_guest(vcpu->kvm)) { 1513 sev_es_prepare_guest_switch(svm, vcpu->cpu); 1514 } else { 1515 vmsave(__sme_page_pa(sd->save_area)); 1516 } 1517 1518 if (tsc_scaling) { 1519 u64 tsc_ratio = vcpu->arch.tsc_scaling_ratio; 1520 if (tsc_ratio != __this_cpu_read(current_tsc_ratio)) { 1521 __this_cpu_write(current_tsc_ratio, tsc_ratio); 1522 wrmsrl(MSR_AMD64_TSC_RATIO, tsc_ratio); 1523 } 1524 } 1525 1526 if (likely(tsc_aux_uret_slot >= 0)) 1527 kvm_set_user_return_msr(tsc_aux_uret_slot, svm->tsc_aux, -1ull); 1528 1529 svm->guest_state_loaded = true; 1530 } 1531 1532 static void svm_prepare_host_switch(struct kvm_vcpu *vcpu) 1533 { 1534 to_svm(vcpu)->guest_state_loaded = false; 1535 } 1536 1537 static void svm_vcpu_load(struct kvm_vcpu *vcpu, int cpu) 1538 { 1539 struct vcpu_svm *svm = to_svm(vcpu); 1540 struct svm_cpu_data *sd = per_cpu(svm_data, cpu); 1541 1542 if (sd->current_vmcb != svm->vmcb) { 1543 sd->current_vmcb = svm->vmcb; 1544 indirect_branch_prediction_barrier(); 1545 } 1546 if (kvm_vcpu_apicv_active(vcpu)) 1547 avic_vcpu_load(vcpu, cpu); 1548 } 1549 1550 static void svm_vcpu_put(struct kvm_vcpu *vcpu) 1551 { 1552 if (kvm_vcpu_apicv_active(vcpu)) 1553 avic_vcpu_put(vcpu); 1554 1555 svm_prepare_host_switch(vcpu); 1556 1557 ++vcpu->stat.host_state_reload; 1558 } 1559 1560 static unsigned long svm_get_rflags(struct kvm_vcpu *vcpu) 1561 { 1562 struct vcpu_svm *svm = to_svm(vcpu); 1563 unsigned long rflags = svm->vmcb->save.rflags; 1564 1565 if (svm->nmi_singlestep) { 1566 /* Hide our flags if they were not set by the guest */ 1567 if (!(svm->nmi_singlestep_guest_rflags & X86_EFLAGS_TF)) 1568 rflags &= ~X86_EFLAGS_TF; 1569 if (!(svm->nmi_singlestep_guest_rflags & X86_EFLAGS_RF)) 1570 rflags &= ~X86_EFLAGS_RF; 1571 } 1572 return rflags; 1573 } 1574 1575 static void svm_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags) 1576 { 1577 if (to_svm(vcpu)->nmi_singlestep) 1578 rflags |= (X86_EFLAGS_TF | X86_EFLAGS_RF); 1579 1580 /* 1581 * Any change of EFLAGS.VM is accompanied by a reload of SS 1582 * (caused by either a task switch or an inter-privilege IRET), 1583 * so we do not need to update the CPL here. 1584 */ 1585 to_svm(vcpu)->vmcb->save.rflags = rflags; 1586 } 1587 1588 static bool svm_get_if_flag(struct kvm_vcpu *vcpu) 1589 { 1590 struct vmcb *vmcb = to_svm(vcpu)->vmcb; 1591 1592 return sev_es_guest(vcpu->kvm) 1593 ? vmcb->control.int_state & SVM_GUEST_INTERRUPT_MASK 1594 : kvm_get_rflags(vcpu) & X86_EFLAGS_IF; 1595 } 1596 1597 static void svm_cache_reg(struct kvm_vcpu *vcpu, enum kvm_reg reg) 1598 { 1599 switch (reg) { 1600 case VCPU_EXREG_PDPTR: 1601 BUG_ON(!npt_enabled); 1602 load_pdptrs(vcpu, vcpu->arch.walk_mmu, kvm_read_cr3(vcpu)); 1603 break; 1604 default: 1605 KVM_BUG_ON(1, vcpu->kvm); 1606 } 1607 } 1608 1609 static void svm_set_vintr(struct vcpu_svm *svm) 1610 { 1611 struct vmcb_control_area *control; 1612 1613 /* 1614 * The following fields are ignored when AVIC is enabled 1615 */ 1616 WARN_ON(kvm_apicv_activated(svm->vcpu.kvm)); 1617 1618 svm_set_intercept(svm, INTERCEPT_VINTR); 1619 1620 /* 1621 * This is just a dummy VINTR to actually cause a vmexit to happen. 1622 * Actual injection of virtual interrupts happens through EVENTINJ. 1623 */ 1624 control = &svm->vmcb->control; 1625 control->int_vector = 0x0; 1626 control->int_ctl &= ~V_INTR_PRIO_MASK; 1627 control->int_ctl |= V_IRQ_MASK | 1628 ((/*control->int_vector >> 4*/ 0xf) << V_INTR_PRIO_SHIFT); 1629 vmcb_mark_dirty(svm->vmcb, VMCB_INTR); 1630 } 1631 1632 static void svm_clear_vintr(struct vcpu_svm *svm) 1633 { 1634 svm_clr_intercept(svm, INTERCEPT_VINTR); 1635 1636 /* Drop int_ctl fields related to VINTR injection. */ 1637 svm->vmcb->control.int_ctl &= ~V_IRQ_INJECTION_BITS_MASK; 1638 if (is_guest_mode(&svm->vcpu)) { 1639 svm->vmcb01.ptr->control.int_ctl &= ~V_IRQ_INJECTION_BITS_MASK; 1640 1641 WARN_ON((svm->vmcb->control.int_ctl & V_TPR_MASK) != 1642 (svm->nested.ctl.int_ctl & V_TPR_MASK)); 1643 1644 svm->vmcb->control.int_ctl |= svm->nested.ctl.int_ctl & 1645 V_IRQ_INJECTION_BITS_MASK; 1646 1647 svm->vmcb->control.int_vector = svm->nested.ctl.int_vector; 1648 } 1649 1650 vmcb_mark_dirty(svm->vmcb, VMCB_INTR); 1651 } 1652 1653 static struct vmcb_seg *svm_seg(struct kvm_vcpu *vcpu, int seg) 1654 { 1655 struct vmcb_save_area *save = &to_svm(vcpu)->vmcb->save; 1656 struct vmcb_save_area *save01 = &to_svm(vcpu)->vmcb01.ptr->save; 1657 1658 switch (seg) { 1659 case VCPU_SREG_CS: return &save->cs; 1660 case VCPU_SREG_DS: return &save->ds; 1661 case VCPU_SREG_ES: return &save->es; 1662 case VCPU_SREG_FS: return &save01->fs; 1663 case VCPU_SREG_GS: return &save01->gs; 1664 case VCPU_SREG_SS: return &save->ss; 1665 case VCPU_SREG_TR: return &save01->tr; 1666 case VCPU_SREG_LDTR: return &save01->ldtr; 1667 } 1668 BUG(); 1669 return NULL; 1670 } 1671 1672 static u64 svm_get_segment_base(struct kvm_vcpu *vcpu, int seg) 1673 { 1674 struct vmcb_seg *s = svm_seg(vcpu, seg); 1675 1676 return s->base; 1677 } 1678 1679 static void svm_get_segment(struct kvm_vcpu *vcpu, 1680 struct kvm_segment *var, int seg) 1681 { 1682 struct vmcb_seg *s = svm_seg(vcpu, seg); 1683 1684 var->base = s->base; 1685 var->limit = s->limit; 1686 var->selector = s->selector; 1687 var->type = s->attrib & SVM_SELECTOR_TYPE_MASK; 1688 var->s = (s->attrib >> SVM_SELECTOR_S_SHIFT) & 1; 1689 var->dpl = (s->attrib >> SVM_SELECTOR_DPL_SHIFT) & 3; 1690 var->present = (s->attrib >> SVM_SELECTOR_P_SHIFT) & 1; 1691 var->avl = (s->attrib >> SVM_SELECTOR_AVL_SHIFT) & 1; 1692 var->l = (s->attrib >> SVM_SELECTOR_L_SHIFT) & 1; 1693 var->db = (s->attrib >> SVM_SELECTOR_DB_SHIFT) & 1; 1694 1695 /* 1696 * AMD CPUs circa 2014 track the G bit for all segments except CS. 1697 * However, the SVM spec states that the G bit is not observed by the 1698 * CPU, and some VMware virtual CPUs drop the G bit for all segments. 1699 * So let's synthesize a legal G bit for all segments, this helps 1700 * running KVM nested. It also helps cross-vendor migration, because 1701 * Intel's vmentry has a check on the 'G' bit. 1702 */ 1703 var->g = s->limit > 0xfffff; 1704 1705 /* 1706 * AMD's VMCB does not have an explicit unusable field, so emulate it 1707 * for cross vendor migration purposes by "not present" 1708 */ 1709 var->unusable = !var->present; 1710 1711 switch (seg) { 1712 case VCPU_SREG_TR: 1713 /* 1714 * Work around a bug where the busy flag in the tr selector 1715 * isn't exposed 1716 */ 1717 var->type |= 0x2; 1718 break; 1719 case VCPU_SREG_DS: 1720 case VCPU_SREG_ES: 1721 case VCPU_SREG_FS: 1722 case VCPU_SREG_GS: 1723 /* 1724 * The accessed bit must always be set in the segment 1725 * descriptor cache, although it can be cleared in the 1726 * descriptor, the cached bit always remains at 1. Since 1727 * Intel has a check on this, set it here to support 1728 * cross-vendor migration. 1729 */ 1730 if (!var->unusable) 1731 var->type |= 0x1; 1732 break; 1733 case VCPU_SREG_SS: 1734 /* 1735 * On AMD CPUs sometimes the DB bit in the segment 1736 * descriptor is left as 1, although the whole segment has 1737 * been made unusable. Clear it here to pass an Intel VMX 1738 * entry check when cross vendor migrating. 1739 */ 1740 if (var->unusable) 1741 var->db = 0; 1742 /* This is symmetric with svm_set_segment() */ 1743 var->dpl = to_svm(vcpu)->vmcb->save.cpl; 1744 break; 1745 } 1746 } 1747 1748 static int svm_get_cpl(struct kvm_vcpu *vcpu) 1749 { 1750 struct vmcb_save_area *save = &to_svm(vcpu)->vmcb->save; 1751 1752 return save->cpl; 1753 } 1754 1755 static void svm_get_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt) 1756 { 1757 struct vcpu_svm *svm = to_svm(vcpu); 1758 1759 dt->size = svm->vmcb->save.idtr.limit; 1760 dt->address = svm->vmcb->save.idtr.base; 1761 } 1762 1763 static void svm_set_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt) 1764 { 1765 struct vcpu_svm *svm = to_svm(vcpu); 1766 1767 svm->vmcb->save.idtr.limit = dt->size; 1768 svm->vmcb->save.idtr.base = dt->address ; 1769 vmcb_mark_dirty(svm->vmcb, VMCB_DT); 1770 } 1771 1772 static void svm_get_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt) 1773 { 1774 struct vcpu_svm *svm = to_svm(vcpu); 1775 1776 dt->size = svm->vmcb->save.gdtr.limit; 1777 dt->address = svm->vmcb->save.gdtr.base; 1778 } 1779 1780 static void svm_set_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt) 1781 { 1782 struct vcpu_svm *svm = to_svm(vcpu); 1783 1784 svm->vmcb->save.gdtr.limit = dt->size; 1785 svm->vmcb->save.gdtr.base = dt->address ; 1786 vmcb_mark_dirty(svm->vmcb, VMCB_DT); 1787 } 1788 1789 void svm_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0) 1790 { 1791 struct vcpu_svm *svm = to_svm(vcpu); 1792 u64 hcr0 = cr0; 1793 1794 #ifdef CONFIG_X86_64 1795 if (vcpu->arch.efer & EFER_LME && !vcpu->arch.guest_state_protected) { 1796 if (!is_paging(vcpu) && (cr0 & X86_CR0_PG)) { 1797 vcpu->arch.efer |= EFER_LMA; 1798 svm->vmcb->save.efer |= EFER_LMA | EFER_LME; 1799 } 1800 1801 if (is_paging(vcpu) && !(cr0 & X86_CR0_PG)) { 1802 vcpu->arch.efer &= ~EFER_LMA; 1803 svm->vmcb->save.efer &= ~(EFER_LMA | EFER_LME); 1804 } 1805 } 1806 #endif 1807 vcpu->arch.cr0 = cr0; 1808 1809 if (!npt_enabled) 1810 hcr0 |= X86_CR0_PG | X86_CR0_WP; 1811 1812 /* 1813 * re-enable caching here because the QEMU bios 1814 * does not do it - this results in some delay at 1815 * reboot 1816 */ 1817 if (kvm_check_has_quirk(vcpu->kvm, KVM_X86_QUIRK_CD_NW_CLEARED)) 1818 hcr0 &= ~(X86_CR0_CD | X86_CR0_NW); 1819 1820 svm->vmcb->save.cr0 = hcr0; 1821 vmcb_mark_dirty(svm->vmcb, VMCB_CR); 1822 1823 /* 1824 * SEV-ES guests must always keep the CR intercepts cleared. CR 1825 * tracking is done using the CR write traps. 1826 */ 1827 if (sev_es_guest(vcpu->kvm)) 1828 return; 1829 1830 if (hcr0 == cr0) { 1831 /* Selective CR0 write remains on. */ 1832 svm_clr_intercept(svm, INTERCEPT_CR0_READ); 1833 svm_clr_intercept(svm, INTERCEPT_CR0_WRITE); 1834 } else { 1835 svm_set_intercept(svm, INTERCEPT_CR0_READ); 1836 svm_set_intercept(svm, INTERCEPT_CR0_WRITE); 1837 } 1838 } 1839 1840 static bool svm_is_valid_cr4(struct kvm_vcpu *vcpu, unsigned long cr4) 1841 { 1842 return true; 1843 } 1844 1845 void svm_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4) 1846 { 1847 unsigned long host_cr4_mce = cr4_read_shadow() & X86_CR4_MCE; 1848 unsigned long old_cr4 = vcpu->arch.cr4; 1849 1850 if (npt_enabled && ((old_cr4 ^ cr4) & X86_CR4_PGE)) 1851 svm_flush_tlb(vcpu); 1852 1853 vcpu->arch.cr4 = cr4; 1854 if (!npt_enabled) 1855 cr4 |= X86_CR4_PAE; 1856 cr4 |= host_cr4_mce; 1857 to_svm(vcpu)->vmcb->save.cr4 = cr4; 1858 vmcb_mark_dirty(to_svm(vcpu)->vmcb, VMCB_CR); 1859 1860 if ((cr4 ^ old_cr4) & (X86_CR4_OSXSAVE | X86_CR4_PKE)) 1861 kvm_update_cpuid_runtime(vcpu); 1862 } 1863 1864 static void svm_set_segment(struct kvm_vcpu *vcpu, 1865 struct kvm_segment *var, int seg) 1866 { 1867 struct vcpu_svm *svm = to_svm(vcpu); 1868 struct vmcb_seg *s = svm_seg(vcpu, seg); 1869 1870 s->base = var->base; 1871 s->limit = var->limit; 1872 s->selector = var->selector; 1873 s->attrib = (var->type & SVM_SELECTOR_TYPE_MASK); 1874 s->attrib |= (var->s & 1) << SVM_SELECTOR_S_SHIFT; 1875 s->attrib |= (var->dpl & 3) << SVM_SELECTOR_DPL_SHIFT; 1876 s->attrib |= ((var->present & 1) && !var->unusable) << SVM_SELECTOR_P_SHIFT; 1877 s->attrib |= (var->avl & 1) << SVM_SELECTOR_AVL_SHIFT; 1878 s->attrib |= (var->l & 1) << SVM_SELECTOR_L_SHIFT; 1879 s->attrib |= (var->db & 1) << SVM_SELECTOR_DB_SHIFT; 1880 s->attrib |= (var->g & 1) << SVM_SELECTOR_G_SHIFT; 1881 1882 /* 1883 * This is always accurate, except if SYSRET returned to a segment 1884 * with SS.DPL != 3. Intel does not have this quirk, and always 1885 * forces SS.DPL to 3 on sysret, so we ignore that case; fixing it 1886 * would entail passing the CPL to userspace and back. 1887 */ 1888 if (seg == VCPU_SREG_SS) 1889 /* This is symmetric with svm_get_segment() */ 1890 svm->vmcb->save.cpl = (var->dpl & 3); 1891 1892 vmcb_mark_dirty(svm->vmcb, VMCB_SEG); 1893 } 1894 1895 static void svm_update_exception_bitmap(struct kvm_vcpu *vcpu) 1896 { 1897 struct vcpu_svm *svm = to_svm(vcpu); 1898 1899 clr_exception_intercept(svm, BP_VECTOR); 1900 1901 if (vcpu->guest_debug & KVM_GUESTDBG_ENABLE) { 1902 if (vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP) 1903 set_exception_intercept(svm, BP_VECTOR); 1904 } 1905 } 1906 1907 static void new_asid(struct vcpu_svm *svm, struct svm_cpu_data *sd) 1908 { 1909 if (sd->next_asid > sd->max_asid) { 1910 ++sd->asid_generation; 1911 sd->next_asid = sd->min_asid; 1912 svm->vmcb->control.tlb_ctl = TLB_CONTROL_FLUSH_ALL_ASID; 1913 vmcb_mark_dirty(svm->vmcb, VMCB_ASID); 1914 } 1915 1916 svm->current_vmcb->asid_generation = sd->asid_generation; 1917 svm->asid = sd->next_asid++; 1918 } 1919 1920 static void svm_set_dr6(struct vcpu_svm *svm, unsigned long value) 1921 { 1922 struct vmcb *vmcb = svm->vmcb; 1923 1924 if (svm->vcpu.arch.guest_state_protected) 1925 return; 1926 1927 if (unlikely(value != vmcb->save.dr6)) { 1928 vmcb->save.dr6 = value; 1929 vmcb_mark_dirty(vmcb, VMCB_DR); 1930 } 1931 } 1932 1933 static void svm_sync_dirty_debug_regs(struct kvm_vcpu *vcpu) 1934 { 1935 struct vcpu_svm *svm = to_svm(vcpu); 1936 1937 if (vcpu->arch.guest_state_protected) 1938 return; 1939 1940 get_debugreg(vcpu->arch.db[0], 0); 1941 get_debugreg(vcpu->arch.db[1], 1); 1942 get_debugreg(vcpu->arch.db[2], 2); 1943 get_debugreg(vcpu->arch.db[3], 3); 1944 /* 1945 * We cannot reset svm->vmcb->save.dr6 to DR6_ACTIVE_LOW here, 1946 * because db_interception might need it. We can do it before vmentry. 1947 */ 1948 vcpu->arch.dr6 = svm->vmcb->save.dr6; 1949 vcpu->arch.dr7 = svm->vmcb->save.dr7; 1950 vcpu->arch.switch_db_regs &= ~KVM_DEBUGREG_WONT_EXIT; 1951 set_dr_intercepts(svm); 1952 } 1953 1954 static void svm_set_dr7(struct kvm_vcpu *vcpu, unsigned long value) 1955 { 1956 struct vcpu_svm *svm = to_svm(vcpu); 1957 1958 if (vcpu->arch.guest_state_protected) 1959 return; 1960 1961 svm->vmcb->save.dr7 = value; 1962 vmcb_mark_dirty(svm->vmcb, VMCB_DR); 1963 } 1964 1965 static int pf_interception(struct kvm_vcpu *vcpu) 1966 { 1967 struct vcpu_svm *svm = to_svm(vcpu); 1968 1969 u64 fault_address = svm->vmcb->control.exit_info_2; 1970 u64 error_code = svm->vmcb->control.exit_info_1; 1971 1972 return kvm_handle_page_fault(vcpu, error_code, fault_address, 1973 static_cpu_has(X86_FEATURE_DECODEASSISTS) ? 1974 svm->vmcb->control.insn_bytes : NULL, 1975 svm->vmcb->control.insn_len); 1976 } 1977 1978 static int npf_interception(struct kvm_vcpu *vcpu) 1979 { 1980 struct vcpu_svm *svm = to_svm(vcpu); 1981 1982 u64 fault_address = svm->vmcb->control.exit_info_2; 1983 u64 error_code = svm->vmcb->control.exit_info_1; 1984 1985 trace_kvm_page_fault(fault_address, error_code); 1986 return kvm_mmu_page_fault(vcpu, fault_address, error_code, 1987 static_cpu_has(X86_FEATURE_DECODEASSISTS) ? 1988 svm->vmcb->control.insn_bytes : NULL, 1989 svm->vmcb->control.insn_len); 1990 } 1991 1992 static int db_interception(struct kvm_vcpu *vcpu) 1993 { 1994 struct kvm_run *kvm_run = vcpu->run; 1995 struct vcpu_svm *svm = to_svm(vcpu); 1996 1997 if (!(vcpu->guest_debug & 1998 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP)) && 1999 !svm->nmi_singlestep) { 2000 u32 payload = svm->vmcb->save.dr6 ^ DR6_ACTIVE_LOW; 2001 kvm_queue_exception_p(vcpu, DB_VECTOR, payload); 2002 return 1; 2003 } 2004 2005 if (svm->nmi_singlestep) { 2006 disable_nmi_singlestep(svm); 2007 /* Make sure we check for pending NMIs upon entry */ 2008 kvm_make_request(KVM_REQ_EVENT, vcpu); 2009 } 2010 2011 if (vcpu->guest_debug & 2012 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP)) { 2013 kvm_run->exit_reason = KVM_EXIT_DEBUG; 2014 kvm_run->debug.arch.dr6 = svm->vmcb->save.dr6; 2015 kvm_run->debug.arch.dr7 = svm->vmcb->save.dr7; 2016 kvm_run->debug.arch.pc = 2017 svm->vmcb->save.cs.base + svm->vmcb->save.rip; 2018 kvm_run->debug.arch.exception = DB_VECTOR; 2019 return 0; 2020 } 2021 2022 return 1; 2023 } 2024 2025 static int bp_interception(struct kvm_vcpu *vcpu) 2026 { 2027 struct vcpu_svm *svm = to_svm(vcpu); 2028 struct kvm_run *kvm_run = vcpu->run; 2029 2030 kvm_run->exit_reason = KVM_EXIT_DEBUG; 2031 kvm_run->debug.arch.pc = svm->vmcb->save.cs.base + svm->vmcb->save.rip; 2032 kvm_run->debug.arch.exception = BP_VECTOR; 2033 return 0; 2034 } 2035 2036 static int ud_interception(struct kvm_vcpu *vcpu) 2037 { 2038 return handle_ud(vcpu); 2039 } 2040 2041 static int ac_interception(struct kvm_vcpu *vcpu) 2042 { 2043 kvm_queue_exception_e(vcpu, AC_VECTOR, 0); 2044 return 1; 2045 } 2046 2047 static bool is_erratum_383(void) 2048 { 2049 int err, i; 2050 u64 value; 2051 2052 if (!erratum_383_found) 2053 return false; 2054 2055 value = native_read_msr_safe(MSR_IA32_MC0_STATUS, &err); 2056 if (err) 2057 return false; 2058 2059 /* Bit 62 may or may not be set for this mce */ 2060 value &= ~(1ULL << 62); 2061 2062 if (value != 0xb600000000010015ULL) 2063 return false; 2064 2065 /* Clear MCi_STATUS registers */ 2066 for (i = 0; i < 6; ++i) 2067 native_write_msr_safe(MSR_IA32_MCx_STATUS(i), 0, 0); 2068 2069 value = native_read_msr_safe(MSR_IA32_MCG_STATUS, &err); 2070 if (!err) { 2071 u32 low, high; 2072 2073 value &= ~(1ULL << 2); 2074 low = lower_32_bits(value); 2075 high = upper_32_bits(value); 2076 2077 native_write_msr_safe(MSR_IA32_MCG_STATUS, low, high); 2078 } 2079 2080 /* Flush tlb to evict multi-match entries */ 2081 __flush_tlb_all(); 2082 2083 return true; 2084 } 2085 2086 static void svm_handle_mce(struct kvm_vcpu *vcpu) 2087 { 2088 if (is_erratum_383()) { 2089 /* 2090 * Erratum 383 triggered. Guest state is corrupt so kill the 2091 * guest. 2092 */ 2093 pr_err("KVM: Guest triggered AMD Erratum 383\n"); 2094 2095 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu); 2096 2097 return; 2098 } 2099 2100 /* 2101 * On an #MC intercept the MCE handler is not called automatically in 2102 * the host. So do it by hand here. 2103 */ 2104 kvm_machine_check(); 2105 } 2106 2107 static int mc_interception(struct kvm_vcpu *vcpu) 2108 { 2109 return 1; 2110 } 2111 2112 static int shutdown_interception(struct kvm_vcpu *vcpu) 2113 { 2114 struct kvm_run *kvm_run = vcpu->run; 2115 struct vcpu_svm *svm = to_svm(vcpu); 2116 2117 /* 2118 * The VM save area has already been encrypted so it 2119 * cannot be reinitialized - just terminate. 2120 */ 2121 if (sev_es_guest(vcpu->kvm)) 2122 return -EINVAL; 2123 2124 /* 2125 * VMCB is undefined after a SHUTDOWN intercept. INIT the vCPU to put 2126 * the VMCB in a known good state. Unfortuately, KVM doesn't have 2127 * KVM_MP_STATE_SHUTDOWN and can't add it without potentially breaking 2128 * userspace. At a platform view, INIT is acceptable behavior as 2129 * there exist bare metal platforms that automatically INIT the CPU 2130 * in response to shutdown. 2131 */ 2132 clear_page(svm->vmcb); 2133 kvm_vcpu_reset(vcpu, true); 2134 2135 kvm_run->exit_reason = KVM_EXIT_SHUTDOWN; 2136 return 0; 2137 } 2138 2139 static int io_interception(struct kvm_vcpu *vcpu) 2140 { 2141 struct vcpu_svm *svm = to_svm(vcpu); 2142 u32 io_info = svm->vmcb->control.exit_info_1; /* address size bug? */ 2143 int size, in, string; 2144 unsigned port; 2145 2146 ++vcpu->stat.io_exits; 2147 string = (io_info & SVM_IOIO_STR_MASK) != 0; 2148 in = (io_info & SVM_IOIO_TYPE_MASK) != 0; 2149 port = io_info >> 16; 2150 size = (io_info & SVM_IOIO_SIZE_MASK) >> SVM_IOIO_SIZE_SHIFT; 2151 2152 if (string) { 2153 if (sev_es_guest(vcpu->kvm)) 2154 return sev_es_string_io(svm, size, port, in); 2155 else 2156 return kvm_emulate_instruction(vcpu, 0); 2157 } 2158 2159 svm->next_rip = svm->vmcb->control.exit_info_2; 2160 2161 return kvm_fast_pio(vcpu, size, port, in); 2162 } 2163 2164 static int nmi_interception(struct kvm_vcpu *vcpu) 2165 { 2166 return 1; 2167 } 2168 2169 static int smi_interception(struct kvm_vcpu *vcpu) 2170 { 2171 return 1; 2172 } 2173 2174 static int intr_interception(struct kvm_vcpu *vcpu) 2175 { 2176 ++vcpu->stat.irq_exits; 2177 return 1; 2178 } 2179 2180 static int vmload_vmsave_interception(struct kvm_vcpu *vcpu, bool vmload) 2181 { 2182 struct vcpu_svm *svm = to_svm(vcpu); 2183 struct vmcb *vmcb12; 2184 struct kvm_host_map map; 2185 int ret; 2186 2187 if (nested_svm_check_permissions(vcpu)) 2188 return 1; 2189 2190 ret = kvm_vcpu_map(vcpu, gpa_to_gfn(svm->vmcb->save.rax), &map); 2191 if (ret) { 2192 if (ret == -EINVAL) 2193 kvm_inject_gp(vcpu, 0); 2194 return 1; 2195 } 2196 2197 vmcb12 = map.hva; 2198 2199 ret = kvm_skip_emulated_instruction(vcpu); 2200 2201 if (vmload) { 2202 svm_copy_vmloadsave_state(svm->vmcb, vmcb12); 2203 svm->sysenter_eip_hi = 0; 2204 svm->sysenter_esp_hi = 0; 2205 } else { 2206 svm_copy_vmloadsave_state(vmcb12, svm->vmcb); 2207 } 2208 2209 kvm_vcpu_unmap(vcpu, &map, true); 2210 2211 return ret; 2212 } 2213 2214 static int vmload_interception(struct kvm_vcpu *vcpu) 2215 { 2216 return vmload_vmsave_interception(vcpu, true); 2217 } 2218 2219 static int vmsave_interception(struct kvm_vcpu *vcpu) 2220 { 2221 return vmload_vmsave_interception(vcpu, false); 2222 } 2223 2224 static int vmrun_interception(struct kvm_vcpu *vcpu) 2225 { 2226 if (nested_svm_check_permissions(vcpu)) 2227 return 1; 2228 2229 return nested_svm_vmrun(vcpu); 2230 } 2231 2232 enum { 2233 NONE_SVM_INSTR, 2234 SVM_INSTR_VMRUN, 2235 SVM_INSTR_VMLOAD, 2236 SVM_INSTR_VMSAVE, 2237 }; 2238 2239 /* Return NONE_SVM_INSTR if not SVM instrs, otherwise return decode result */ 2240 static int svm_instr_opcode(struct kvm_vcpu *vcpu) 2241 { 2242 struct x86_emulate_ctxt *ctxt = vcpu->arch.emulate_ctxt; 2243 2244 if (ctxt->b != 0x1 || ctxt->opcode_len != 2) 2245 return NONE_SVM_INSTR; 2246 2247 switch (ctxt->modrm) { 2248 case 0xd8: /* VMRUN */ 2249 return SVM_INSTR_VMRUN; 2250 case 0xda: /* VMLOAD */ 2251 return SVM_INSTR_VMLOAD; 2252 case 0xdb: /* VMSAVE */ 2253 return SVM_INSTR_VMSAVE; 2254 default: 2255 break; 2256 } 2257 2258 return NONE_SVM_INSTR; 2259 } 2260 2261 static int emulate_svm_instr(struct kvm_vcpu *vcpu, int opcode) 2262 { 2263 const int guest_mode_exit_codes[] = { 2264 [SVM_INSTR_VMRUN] = SVM_EXIT_VMRUN, 2265 [SVM_INSTR_VMLOAD] = SVM_EXIT_VMLOAD, 2266 [SVM_INSTR_VMSAVE] = SVM_EXIT_VMSAVE, 2267 }; 2268 int (*const svm_instr_handlers[])(struct kvm_vcpu *vcpu) = { 2269 [SVM_INSTR_VMRUN] = vmrun_interception, 2270 [SVM_INSTR_VMLOAD] = vmload_interception, 2271 [SVM_INSTR_VMSAVE] = vmsave_interception, 2272 }; 2273 struct vcpu_svm *svm = to_svm(vcpu); 2274 int ret; 2275 2276 if (is_guest_mode(vcpu)) { 2277 /* Returns '1' or -errno on failure, '0' on success. */ 2278 ret = nested_svm_simple_vmexit(svm, guest_mode_exit_codes[opcode]); 2279 if (ret) 2280 return ret; 2281 return 1; 2282 } 2283 return svm_instr_handlers[opcode](vcpu); 2284 } 2285 2286 /* 2287 * #GP handling code. Note that #GP can be triggered under the following two 2288 * cases: 2289 * 1) SVM VM-related instructions (VMRUN/VMSAVE/VMLOAD) that trigger #GP on 2290 * some AMD CPUs when EAX of these instructions are in the reserved memory 2291 * regions (e.g. SMM memory on host). 2292 * 2) VMware backdoor 2293 */ 2294 static int gp_interception(struct kvm_vcpu *vcpu) 2295 { 2296 struct vcpu_svm *svm = to_svm(vcpu); 2297 u32 error_code = svm->vmcb->control.exit_info_1; 2298 int opcode; 2299 2300 /* Both #GP cases have zero error_code */ 2301 if (error_code) 2302 goto reinject; 2303 2304 /* All SVM instructions expect page aligned RAX */ 2305 if (svm->vmcb->save.rax & ~PAGE_MASK) 2306 goto reinject; 2307 2308 /* Decode the instruction for usage later */ 2309 if (x86_decode_emulated_instruction(vcpu, 0, NULL, 0) != EMULATION_OK) 2310 goto reinject; 2311 2312 opcode = svm_instr_opcode(vcpu); 2313 2314 if (opcode == NONE_SVM_INSTR) { 2315 if (!enable_vmware_backdoor) 2316 goto reinject; 2317 2318 /* 2319 * VMware backdoor emulation on #GP interception only handles 2320 * IN{S}, OUT{S}, and RDPMC. 2321 */ 2322 if (!is_guest_mode(vcpu)) 2323 return kvm_emulate_instruction(vcpu, 2324 EMULTYPE_VMWARE_GP | EMULTYPE_NO_DECODE); 2325 } else 2326 return emulate_svm_instr(vcpu, opcode); 2327 2328 reinject: 2329 kvm_queue_exception_e(vcpu, GP_VECTOR, error_code); 2330 return 1; 2331 } 2332 2333 void svm_set_gif(struct vcpu_svm *svm, bool value) 2334 { 2335 if (value) { 2336 /* 2337 * If VGIF is enabled, the STGI intercept is only added to 2338 * detect the opening of the SMI/NMI window; remove it now. 2339 * Likewise, clear the VINTR intercept, we will set it 2340 * again while processing KVM_REQ_EVENT if needed. 2341 */ 2342 if (vgif_enabled(svm)) 2343 svm_clr_intercept(svm, INTERCEPT_STGI); 2344 if (svm_is_intercept(svm, INTERCEPT_VINTR)) 2345 svm_clear_vintr(svm); 2346 2347 enable_gif(svm); 2348 if (svm->vcpu.arch.smi_pending || 2349 svm->vcpu.arch.nmi_pending || 2350 kvm_cpu_has_injectable_intr(&svm->vcpu)) 2351 kvm_make_request(KVM_REQ_EVENT, &svm->vcpu); 2352 } else { 2353 disable_gif(svm); 2354 2355 /* 2356 * After a CLGI no interrupts should come. But if vGIF is 2357 * in use, we still rely on the VINTR intercept (rather than 2358 * STGI) to detect an open interrupt window. 2359 */ 2360 if (!vgif_enabled(svm)) 2361 svm_clear_vintr(svm); 2362 } 2363 } 2364 2365 static int stgi_interception(struct kvm_vcpu *vcpu) 2366 { 2367 int ret; 2368 2369 if (nested_svm_check_permissions(vcpu)) 2370 return 1; 2371 2372 ret = kvm_skip_emulated_instruction(vcpu); 2373 svm_set_gif(to_svm(vcpu), true); 2374 return ret; 2375 } 2376 2377 static int clgi_interception(struct kvm_vcpu *vcpu) 2378 { 2379 int ret; 2380 2381 if (nested_svm_check_permissions(vcpu)) 2382 return 1; 2383 2384 ret = kvm_skip_emulated_instruction(vcpu); 2385 svm_set_gif(to_svm(vcpu), false); 2386 return ret; 2387 } 2388 2389 static int invlpga_interception(struct kvm_vcpu *vcpu) 2390 { 2391 gva_t gva = kvm_rax_read(vcpu); 2392 u32 asid = kvm_rcx_read(vcpu); 2393 2394 /* FIXME: Handle an address size prefix. */ 2395 if (!is_long_mode(vcpu)) 2396 gva = (u32)gva; 2397 2398 trace_kvm_invlpga(to_svm(vcpu)->vmcb->save.rip, asid, gva); 2399 2400 /* Let's treat INVLPGA the same as INVLPG (can be optimized!) */ 2401 kvm_mmu_invlpg(vcpu, gva); 2402 2403 return kvm_skip_emulated_instruction(vcpu); 2404 } 2405 2406 static int skinit_interception(struct kvm_vcpu *vcpu) 2407 { 2408 trace_kvm_skinit(to_svm(vcpu)->vmcb->save.rip, kvm_rax_read(vcpu)); 2409 2410 kvm_queue_exception(vcpu, UD_VECTOR); 2411 return 1; 2412 } 2413 2414 static int task_switch_interception(struct kvm_vcpu *vcpu) 2415 { 2416 struct vcpu_svm *svm = to_svm(vcpu); 2417 u16 tss_selector; 2418 int reason; 2419 int int_type = svm->vmcb->control.exit_int_info & 2420 SVM_EXITINTINFO_TYPE_MASK; 2421 int int_vec = svm->vmcb->control.exit_int_info & SVM_EVTINJ_VEC_MASK; 2422 uint32_t type = 2423 svm->vmcb->control.exit_int_info & SVM_EXITINTINFO_TYPE_MASK; 2424 uint32_t idt_v = 2425 svm->vmcb->control.exit_int_info & SVM_EXITINTINFO_VALID; 2426 bool has_error_code = false; 2427 u32 error_code = 0; 2428 2429 tss_selector = (u16)svm->vmcb->control.exit_info_1; 2430 2431 if (svm->vmcb->control.exit_info_2 & 2432 (1ULL << SVM_EXITINFOSHIFT_TS_REASON_IRET)) 2433 reason = TASK_SWITCH_IRET; 2434 else if (svm->vmcb->control.exit_info_2 & 2435 (1ULL << SVM_EXITINFOSHIFT_TS_REASON_JMP)) 2436 reason = TASK_SWITCH_JMP; 2437 else if (idt_v) 2438 reason = TASK_SWITCH_GATE; 2439 else 2440 reason = TASK_SWITCH_CALL; 2441 2442 if (reason == TASK_SWITCH_GATE) { 2443 switch (type) { 2444 case SVM_EXITINTINFO_TYPE_NMI: 2445 vcpu->arch.nmi_injected = false; 2446 break; 2447 case SVM_EXITINTINFO_TYPE_EXEPT: 2448 if (svm->vmcb->control.exit_info_2 & 2449 (1ULL << SVM_EXITINFOSHIFT_TS_HAS_ERROR_CODE)) { 2450 has_error_code = true; 2451 error_code = 2452 (u32)svm->vmcb->control.exit_info_2; 2453 } 2454 kvm_clear_exception_queue(vcpu); 2455 break; 2456 case SVM_EXITINTINFO_TYPE_INTR: 2457 kvm_clear_interrupt_queue(vcpu); 2458 break; 2459 default: 2460 break; 2461 } 2462 } 2463 2464 if (reason != TASK_SWITCH_GATE || 2465 int_type == SVM_EXITINTINFO_TYPE_SOFT || 2466 (int_type == SVM_EXITINTINFO_TYPE_EXEPT && 2467 (int_vec == OF_VECTOR || int_vec == BP_VECTOR))) { 2468 if (!skip_emulated_instruction(vcpu)) 2469 return 0; 2470 } 2471 2472 if (int_type != SVM_EXITINTINFO_TYPE_SOFT) 2473 int_vec = -1; 2474 2475 return kvm_task_switch(vcpu, tss_selector, int_vec, reason, 2476 has_error_code, error_code); 2477 } 2478 2479 static int iret_interception(struct kvm_vcpu *vcpu) 2480 { 2481 struct vcpu_svm *svm = to_svm(vcpu); 2482 2483 ++vcpu->stat.nmi_window_exits; 2484 vcpu->arch.hflags |= HF_IRET_MASK; 2485 if (!sev_es_guest(vcpu->kvm)) { 2486 svm_clr_intercept(svm, INTERCEPT_IRET); 2487 svm->nmi_iret_rip = kvm_rip_read(vcpu); 2488 } 2489 kvm_make_request(KVM_REQ_EVENT, vcpu); 2490 return 1; 2491 } 2492 2493 static int invlpg_interception(struct kvm_vcpu *vcpu) 2494 { 2495 if (!static_cpu_has(X86_FEATURE_DECODEASSISTS)) 2496 return kvm_emulate_instruction(vcpu, 0); 2497 2498 kvm_mmu_invlpg(vcpu, to_svm(vcpu)->vmcb->control.exit_info_1); 2499 return kvm_skip_emulated_instruction(vcpu); 2500 } 2501 2502 static int emulate_on_interception(struct kvm_vcpu *vcpu) 2503 { 2504 return kvm_emulate_instruction(vcpu, 0); 2505 } 2506 2507 static int rsm_interception(struct kvm_vcpu *vcpu) 2508 { 2509 return kvm_emulate_instruction_from_buffer(vcpu, rsm_ins_bytes, 2); 2510 } 2511 2512 static bool check_selective_cr0_intercepted(struct kvm_vcpu *vcpu, 2513 unsigned long val) 2514 { 2515 struct vcpu_svm *svm = to_svm(vcpu); 2516 unsigned long cr0 = vcpu->arch.cr0; 2517 bool ret = false; 2518 2519 if (!is_guest_mode(vcpu) || 2520 (!(vmcb_is_intercept(&svm->nested.ctl, INTERCEPT_SELECTIVE_CR0)))) 2521 return false; 2522 2523 cr0 &= ~SVM_CR0_SELECTIVE_MASK; 2524 val &= ~SVM_CR0_SELECTIVE_MASK; 2525 2526 if (cr0 ^ val) { 2527 svm->vmcb->control.exit_code = SVM_EXIT_CR0_SEL_WRITE; 2528 ret = (nested_svm_exit_handled(svm) == NESTED_EXIT_DONE); 2529 } 2530 2531 return ret; 2532 } 2533 2534 #define CR_VALID (1ULL << 63) 2535 2536 static int cr_interception(struct kvm_vcpu *vcpu) 2537 { 2538 struct vcpu_svm *svm = to_svm(vcpu); 2539 int reg, cr; 2540 unsigned long val; 2541 int err; 2542 2543 if (!static_cpu_has(X86_FEATURE_DECODEASSISTS)) 2544 return emulate_on_interception(vcpu); 2545 2546 if (unlikely((svm->vmcb->control.exit_info_1 & CR_VALID) == 0)) 2547 return emulate_on_interception(vcpu); 2548 2549 reg = svm->vmcb->control.exit_info_1 & SVM_EXITINFO_REG_MASK; 2550 if (svm->vmcb->control.exit_code == SVM_EXIT_CR0_SEL_WRITE) 2551 cr = SVM_EXIT_WRITE_CR0 - SVM_EXIT_READ_CR0; 2552 else 2553 cr = svm->vmcb->control.exit_code - SVM_EXIT_READ_CR0; 2554 2555 err = 0; 2556 if (cr >= 16) { /* mov to cr */ 2557 cr -= 16; 2558 val = kvm_register_read(vcpu, reg); 2559 trace_kvm_cr_write(cr, val); 2560 switch (cr) { 2561 case 0: 2562 if (!check_selective_cr0_intercepted(vcpu, val)) 2563 err = kvm_set_cr0(vcpu, val); 2564 else 2565 return 1; 2566 2567 break; 2568 case 3: 2569 err = kvm_set_cr3(vcpu, val); 2570 break; 2571 case 4: 2572 err = kvm_set_cr4(vcpu, val); 2573 break; 2574 case 8: 2575 err = kvm_set_cr8(vcpu, val); 2576 break; 2577 default: 2578 WARN(1, "unhandled write to CR%d", cr); 2579 kvm_queue_exception(vcpu, UD_VECTOR); 2580 return 1; 2581 } 2582 } else { /* mov from cr */ 2583 switch (cr) { 2584 case 0: 2585 val = kvm_read_cr0(vcpu); 2586 break; 2587 case 2: 2588 val = vcpu->arch.cr2; 2589 break; 2590 case 3: 2591 val = kvm_read_cr3(vcpu); 2592 break; 2593 case 4: 2594 val = kvm_read_cr4(vcpu); 2595 break; 2596 case 8: 2597 val = kvm_get_cr8(vcpu); 2598 break; 2599 default: 2600 WARN(1, "unhandled read from CR%d", cr); 2601 kvm_queue_exception(vcpu, UD_VECTOR); 2602 return 1; 2603 } 2604 kvm_register_write(vcpu, reg, val); 2605 trace_kvm_cr_read(cr, val); 2606 } 2607 return kvm_complete_insn_gp(vcpu, err); 2608 } 2609 2610 static int cr_trap(struct kvm_vcpu *vcpu) 2611 { 2612 struct vcpu_svm *svm = to_svm(vcpu); 2613 unsigned long old_value, new_value; 2614 unsigned int cr; 2615 int ret = 0; 2616 2617 new_value = (unsigned long)svm->vmcb->control.exit_info_1; 2618 2619 cr = svm->vmcb->control.exit_code - SVM_EXIT_CR0_WRITE_TRAP; 2620 switch (cr) { 2621 case 0: 2622 old_value = kvm_read_cr0(vcpu); 2623 svm_set_cr0(vcpu, new_value); 2624 2625 kvm_post_set_cr0(vcpu, old_value, new_value); 2626 break; 2627 case 4: 2628 old_value = kvm_read_cr4(vcpu); 2629 svm_set_cr4(vcpu, new_value); 2630 2631 kvm_post_set_cr4(vcpu, old_value, new_value); 2632 break; 2633 case 8: 2634 ret = kvm_set_cr8(vcpu, new_value); 2635 break; 2636 default: 2637 WARN(1, "unhandled CR%d write trap", cr); 2638 kvm_queue_exception(vcpu, UD_VECTOR); 2639 return 1; 2640 } 2641 2642 return kvm_complete_insn_gp(vcpu, ret); 2643 } 2644 2645 static int dr_interception(struct kvm_vcpu *vcpu) 2646 { 2647 struct vcpu_svm *svm = to_svm(vcpu); 2648 int reg, dr; 2649 unsigned long val; 2650 int err = 0; 2651 2652 if (vcpu->guest_debug == 0) { 2653 /* 2654 * No more DR vmexits; force a reload of the debug registers 2655 * and reenter on this instruction. The next vmexit will 2656 * retrieve the full state of the debug registers. 2657 */ 2658 clr_dr_intercepts(svm); 2659 vcpu->arch.switch_db_regs |= KVM_DEBUGREG_WONT_EXIT; 2660 return 1; 2661 } 2662 2663 if (!boot_cpu_has(X86_FEATURE_DECODEASSISTS)) 2664 return emulate_on_interception(vcpu); 2665 2666 reg = svm->vmcb->control.exit_info_1 & SVM_EXITINFO_REG_MASK; 2667 dr = svm->vmcb->control.exit_code - SVM_EXIT_READ_DR0; 2668 if (dr >= 16) { /* mov to DRn */ 2669 dr -= 16; 2670 val = kvm_register_read(vcpu, reg); 2671 err = kvm_set_dr(vcpu, dr, val); 2672 } else { 2673 kvm_get_dr(vcpu, dr, &val); 2674 kvm_register_write(vcpu, reg, val); 2675 } 2676 2677 return kvm_complete_insn_gp(vcpu, err); 2678 } 2679 2680 static int cr8_write_interception(struct kvm_vcpu *vcpu) 2681 { 2682 int r; 2683 2684 u8 cr8_prev = kvm_get_cr8(vcpu); 2685 /* instruction emulation calls kvm_set_cr8() */ 2686 r = cr_interception(vcpu); 2687 if (lapic_in_kernel(vcpu)) 2688 return r; 2689 if (cr8_prev <= kvm_get_cr8(vcpu)) 2690 return r; 2691 vcpu->run->exit_reason = KVM_EXIT_SET_TPR; 2692 return 0; 2693 } 2694 2695 static int efer_trap(struct kvm_vcpu *vcpu) 2696 { 2697 struct msr_data msr_info; 2698 int ret; 2699 2700 /* 2701 * Clear the EFER_SVME bit from EFER. The SVM code always sets this 2702 * bit in svm_set_efer(), but __kvm_valid_efer() checks it against 2703 * whether the guest has X86_FEATURE_SVM - this avoids a failure if 2704 * the guest doesn't have X86_FEATURE_SVM. 2705 */ 2706 msr_info.host_initiated = false; 2707 msr_info.index = MSR_EFER; 2708 msr_info.data = to_svm(vcpu)->vmcb->control.exit_info_1 & ~EFER_SVME; 2709 ret = kvm_set_msr_common(vcpu, &msr_info); 2710 2711 return kvm_complete_insn_gp(vcpu, ret); 2712 } 2713 2714 static int svm_get_msr_feature(struct kvm_msr_entry *msr) 2715 { 2716 msr->data = 0; 2717 2718 switch (msr->index) { 2719 case MSR_F10H_DECFG: 2720 if (boot_cpu_has(X86_FEATURE_LFENCE_RDTSC)) 2721 msr->data |= MSR_F10H_DECFG_LFENCE_SERIALIZE; 2722 break; 2723 case MSR_IA32_PERF_CAPABILITIES: 2724 return 0; 2725 default: 2726 return KVM_MSR_RET_INVALID; 2727 } 2728 2729 return 0; 2730 } 2731 2732 static int svm_get_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info) 2733 { 2734 struct vcpu_svm *svm = to_svm(vcpu); 2735 2736 switch (msr_info->index) { 2737 case MSR_AMD64_TSC_RATIO: 2738 if (!msr_info->host_initiated && !svm->tsc_scaling_enabled) 2739 return 1; 2740 msr_info->data = svm->tsc_ratio_msr; 2741 break; 2742 case MSR_STAR: 2743 msr_info->data = svm->vmcb01.ptr->save.star; 2744 break; 2745 #ifdef CONFIG_X86_64 2746 case MSR_LSTAR: 2747 msr_info->data = svm->vmcb01.ptr->save.lstar; 2748 break; 2749 case MSR_CSTAR: 2750 msr_info->data = svm->vmcb01.ptr->save.cstar; 2751 break; 2752 case MSR_KERNEL_GS_BASE: 2753 msr_info->data = svm->vmcb01.ptr->save.kernel_gs_base; 2754 break; 2755 case MSR_SYSCALL_MASK: 2756 msr_info->data = svm->vmcb01.ptr->save.sfmask; 2757 break; 2758 #endif 2759 case MSR_IA32_SYSENTER_CS: 2760 msr_info->data = svm->vmcb01.ptr->save.sysenter_cs; 2761 break; 2762 case MSR_IA32_SYSENTER_EIP: 2763 msr_info->data = (u32)svm->vmcb01.ptr->save.sysenter_eip; 2764 if (guest_cpuid_is_intel(vcpu)) 2765 msr_info->data |= (u64)svm->sysenter_eip_hi << 32; 2766 break; 2767 case MSR_IA32_SYSENTER_ESP: 2768 msr_info->data = svm->vmcb01.ptr->save.sysenter_esp; 2769 if (guest_cpuid_is_intel(vcpu)) 2770 msr_info->data |= (u64)svm->sysenter_esp_hi << 32; 2771 break; 2772 case MSR_TSC_AUX: 2773 msr_info->data = svm->tsc_aux; 2774 break; 2775 /* 2776 * Nobody will change the following 5 values in the VMCB so we can 2777 * safely return them on rdmsr. They will always be 0 until LBRV is 2778 * implemented. 2779 */ 2780 case MSR_IA32_DEBUGCTLMSR: 2781 msr_info->data = svm->vmcb->save.dbgctl; 2782 break; 2783 case MSR_IA32_LASTBRANCHFROMIP: 2784 msr_info->data = svm->vmcb->save.br_from; 2785 break; 2786 case MSR_IA32_LASTBRANCHTOIP: 2787 msr_info->data = svm->vmcb->save.br_to; 2788 break; 2789 case MSR_IA32_LASTINTFROMIP: 2790 msr_info->data = svm->vmcb->save.last_excp_from; 2791 break; 2792 case MSR_IA32_LASTINTTOIP: 2793 msr_info->data = svm->vmcb->save.last_excp_to; 2794 break; 2795 case MSR_VM_HSAVE_PA: 2796 msr_info->data = svm->nested.hsave_msr; 2797 break; 2798 case MSR_VM_CR: 2799 msr_info->data = svm->nested.vm_cr_msr; 2800 break; 2801 case MSR_IA32_SPEC_CTRL: 2802 if (!msr_info->host_initiated && 2803 !guest_has_spec_ctrl_msr(vcpu)) 2804 return 1; 2805 2806 if (boot_cpu_has(X86_FEATURE_V_SPEC_CTRL)) 2807 msr_info->data = svm->vmcb->save.spec_ctrl; 2808 else 2809 msr_info->data = svm->spec_ctrl; 2810 break; 2811 case MSR_AMD64_VIRT_SPEC_CTRL: 2812 if (!msr_info->host_initiated && 2813 !guest_cpuid_has(vcpu, X86_FEATURE_VIRT_SSBD)) 2814 return 1; 2815 2816 msr_info->data = svm->virt_spec_ctrl; 2817 break; 2818 case MSR_F15H_IC_CFG: { 2819 2820 int family, model; 2821 2822 family = guest_cpuid_family(vcpu); 2823 model = guest_cpuid_model(vcpu); 2824 2825 if (family < 0 || model < 0) 2826 return kvm_get_msr_common(vcpu, msr_info); 2827 2828 msr_info->data = 0; 2829 2830 if (family == 0x15 && 2831 (model >= 0x2 && model < 0x20)) 2832 msr_info->data = 0x1E; 2833 } 2834 break; 2835 case MSR_F10H_DECFG: 2836 msr_info->data = svm->msr_decfg; 2837 break; 2838 default: 2839 return kvm_get_msr_common(vcpu, msr_info); 2840 } 2841 return 0; 2842 } 2843 2844 static int svm_complete_emulated_msr(struct kvm_vcpu *vcpu, int err) 2845 { 2846 struct vcpu_svm *svm = to_svm(vcpu); 2847 if (!err || !sev_es_guest(vcpu->kvm) || WARN_ON_ONCE(!svm->sev_es.ghcb)) 2848 return kvm_complete_insn_gp(vcpu, err); 2849 2850 ghcb_set_sw_exit_info_1(svm->sev_es.ghcb, 1); 2851 ghcb_set_sw_exit_info_2(svm->sev_es.ghcb, 2852 X86_TRAP_GP | 2853 SVM_EVTINJ_TYPE_EXEPT | 2854 SVM_EVTINJ_VALID); 2855 return 1; 2856 } 2857 2858 static int svm_set_vm_cr(struct kvm_vcpu *vcpu, u64 data) 2859 { 2860 struct vcpu_svm *svm = to_svm(vcpu); 2861 int svm_dis, chg_mask; 2862 2863 if (data & ~SVM_VM_CR_VALID_MASK) 2864 return 1; 2865 2866 chg_mask = SVM_VM_CR_VALID_MASK; 2867 2868 if (svm->nested.vm_cr_msr & SVM_VM_CR_SVM_DIS_MASK) 2869 chg_mask &= ~(SVM_VM_CR_SVM_LOCK_MASK | SVM_VM_CR_SVM_DIS_MASK); 2870 2871 svm->nested.vm_cr_msr &= ~chg_mask; 2872 svm->nested.vm_cr_msr |= (data & chg_mask); 2873 2874 svm_dis = svm->nested.vm_cr_msr & SVM_VM_CR_SVM_DIS_MASK; 2875 2876 /* check for svm_disable while efer.svme is set */ 2877 if (svm_dis && (vcpu->arch.efer & EFER_SVME)) 2878 return 1; 2879 2880 return 0; 2881 } 2882 2883 static int svm_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr) 2884 { 2885 struct vcpu_svm *svm = to_svm(vcpu); 2886 int r; 2887 2888 u32 ecx = msr->index; 2889 u64 data = msr->data; 2890 switch (ecx) { 2891 case MSR_AMD64_TSC_RATIO: 2892 if (!msr->host_initiated && !svm->tsc_scaling_enabled) 2893 return 1; 2894 2895 if (data & TSC_RATIO_RSVD) 2896 return 1; 2897 2898 svm->tsc_ratio_msr = data; 2899 2900 if (svm->tsc_scaling_enabled && is_guest_mode(vcpu)) 2901 nested_svm_update_tsc_ratio_msr(vcpu); 2902 2903 break; 2904 case MSR_IA32_CR_PAT: 2905 if (!kvm_mtrr_valid(vcpu, MSR_IA32_CR_PAT, data)) 2906 return 1; 2907 vcpu->arch.pat = data; 2908 svm->vmcb01.ptr->save.g_pat = data; 2909 if (is_guest_mode(vcpu)) 2910 nested_vmcb02_compute_g_pat(svm); 2911 vmcb_mark_dirty(svm->vmcb, VMCB_NPT); 2912 break; 2913 case MSR_IA32_SPEC_CTRL: 2914 if (!msr->host_initiated && 2915 !guest_has_spec_ctrl_msr(vcpu)) 2916 return 1; 2917 2918 if (kvm_spec_ctrl_test_value(data)) 2919 return 1; 2920 2921 if (boot_cpu_has(X86_FEATURE_V_SPEC_CTRL)) 2922 svm->vmcb->save.spec_ctrl = data; 2923 else 2924 svm->spec_ctrl = data; 2925 if (!data) 2926 break; 2927 2928 /* 2929 * For non-nested: 2930 * When it's written (to non-zero) for the first time, pass 2931 * it through. 2932 * 2933 * For nested: 2934 * The handling of the MSR bitmap for L2 guests is done in 2935 * nested_svm_vmrun_msrpm. 2936 * We update the L1 MSR bit as well since it will end up 2937 * touching the MSR anyway now. 2938 */ 2939 set_msr_interception(vcpu, svm->msrpm, MSR_IA32_SPEC_CTRL, 1, 1); 2940 break; 2941 case MSR_IA32_PRED_CMD: 2942 if (!msr->host_initiated && 2943 !guest_has_pred_cmd_msr(vcpu)) 2944 return 1; 2945 2946 if (data & ~PRED_CMD_IBPB) 2947 return 1; 2948 if (!boot_cpu_has(X86_FEATURE_IBPB)) 2949 return 1; 2950 if (!data) 2951 break; 2952 2953 wrmsrl(MSR_IA32_PRED_CMD, PRED_CMD_IBPB); 2954 set_msr_interception(vcpu, svm->msrpm, MSR_IA32_PRED_CMD, 0, 1); 2955 break; 2956 case MSR_AMD64_VIRT_SPEC_CTRL: 2957 if (!msr->host_initiated && 2958 !guest_cpuid_has(vcpu, X86_FEATURE_VIRT_SSBD)) 2959 return 1; 2960 2961 if (data & ~SPEC_CTRL_SSBD) 2962 return 1; 2963 2964 svm->virt_spec_ctrl = data; 2965 break; 2966 case MSR_STAR: 2967 svm->vmcb01.ptr->save.star = data; 2968 break; 2969 #ifdef CONFIG_X86_64 2970 case MSR_LSTAR: 2971 svm->vmcb01.ptr->save.lstar = data; 2972 break; 2973 case MSR_CSTAR: 2974 svm->vmcb01.ptr->save.cstar = data; 2975 break; 2976 case MSR_KERNEL_GS_BASE: 2977 svm->vmcb01.ptr->save.kernel_gs_base = data; 2978 break; 2979 case MSR_SYSCALL_MASK: 2980 svm->vmcb01.ptr->save.sfmask = data; 2981 break; 2982 #endif 2983 case MSR_IA32_SYSENTER_CS: 2984 svm->vmcb01.ptr->save.sysenter_cs = data; 2985 break; 2986 case MSR_IA32_SYSENTER_EIP: 2987 svm->vmcb01.ptr->save.sysenter_eip = (u32)data; 2988 /* 2989 * We only intercept the MSR_IA32_SYSENTER_{EIP|ESP} msrs 2990 * when we spoof an Intel vendor ID (for cross vendor migration). 2991 * In this case we use this intercept to track the high 2992 * 32 bit part of these msrs to support Intel's 2993 * implementation of SYSENTER/SYSEXIT. 2994 */ 2995 svm->sysenter_eip_hi = guest_cpuid_is_intel(vcpu) ? (data >> 32) : 0; 2996 break; 2997 case MSR_IA32_SYSENTER_ESP: 2998 svm->vmcb01.ptr->save.sysenter_esp = (u32)data; 2999 svm->sysenter_esp_hi = guest_cpuid_is_intel(vcpu) ? (data >> 32) : 0; 3000 break; 3001 case MSR_TSC_AUX: 3002 /* 3003 * TSC_AUX is usually changed only during boot and never read 3004 * directly. Intercept TSC_AUX instead of exposing it to the 3005 * guest via direct_access_msrs, and switch it via user return. 3006 */ 3007 preempt_disable(); 3008 r = kvm_set_user_return_msr(tsc_aux_uret_slot, data, -1ull); 3009 preempt_enable(); 3010 if (r) 3011 return 1; 3012 3013 svm->tsc_aux = data; 3014 break; 3015 case MSR_IA32_DEBUGCTLMSR: 3016 if (!lbrv) { 3017 vcpu_unimpl(vcpu, "%s: MSR_IA32_DEBUGCTL 0x%llx, nop\n", 3018 __func__, data); 3019 break; 3020 } 3021 if (data & DEBUGCTL_RESERVED_BITS) 3022 return 1; 3023 3024 svm->vmcb->save.dbgctl = data; 3025 vmcb_mark_dirty(svm->vmcb, VMCB_LBR); 3026 if (data & (1ULL<<0)) 3027 svm_enable_lbrv(vcpu); 3028 else 3029 svm_disable_lbrv(vcpu); 3030 break; 3031 case MSR_VM_HSAVE_PA: 3032 /* 3033 * Old kernels did not validate the value written to 3034 * MSR_VM_HSAVE_PA. Allow KVM_SET_MSR to set an invalid 3035 * value to allow live migrating buggy or malicious guests 3036 * originating from those kernels. 3037 */ 3038 if (!msr->host_initiated && !page_address_valid(vcpu, data)) 3039 return 1; 3040 3041 svm->nested.hsave_msr = data & PAGE_MASK; 3042 break; 3043 case MSR_VM_CR: 3044 return svm_set_vm_cr(vcpu, data); 3045 case MSR_VM_IGNNE: 3046 vcpu_unimpl(vcpu, "unimplemented wrmsr: 0x%x data 0x%llx\n", ecx, data); 3047 break; 3048 case MSR_F10H_DECFG: { 3049 struct kvm_msr_entry msr_entry; 3050 3051 msr_entry.index = msr->index; 3052 if (svm_get_msr_feature(&msr_entry)) 3053 return 1; 3054 3055 /* Check the supported bits */ 3056 if (data & ~msr_entry.data) 3057 return 1; 3058 3059 /* Don't allow the guest to change a bit, #GP */ 3060 if (!msr->host_initiated && (data ^ msr_entry.data)) 3061 return 1; 3062 3063 svm->msr_decfg = data; 3064 break; 3065 } 3066 default: 3067 return kvm_set_msr_common(vcpu, msr); 3068 } 3069 return 0; 3070 } 3071 3072 static int msr_interception(struct kvm_vcpu *vcpu) 3073 { 3074 if (to_svm(vcpu)->vmcb->control.exit_info_1) 3075 return kvm_emulate_wrmsr(vcpu); 3076 else 3077 return kvm_emulate_rdmsr(vcpu); 3078 } 3079 3080 static int interrupt_window_interception(struct kvm_vcpu *vcpu) 3081 { 3082 kvm_make_request(KVM_REQ_EVENT, vcpu); 3083 svm_clear_vintr(to_svm(vcpu)); 3084 3085 /* 3086 * For AVIC, the only reason to end up here is ExtINTs. 3087 * In this case AVIC was temporarily disabled for 3088 * requesting the IRQ window and we have to re-enable it. 3089 */ 3090 kvm_request_apicv_update(vcpu->kvm, true, APICV_INHIBIT_REASON_IRQWIN); 3091 3092 ++vcpu->stat.irq_window_exits; 3093 return 1; 3094 } 3095 3096 static int pause_interception(struct kvm_vcpu *vcpu) 3097 { 3098 bool in_kernel; 3099 3100 /* 3101 * CPL is not made available for an SEV-ES guest, therefore 3102 * vcpu->arch.preempted_in_kernel can never be true. Just 3103 * set in_kernel to false as well. 3104 */ 3105 in_kernel = !sev_es_guest(vcpu->kvm) && svm_get_cpl(vcpu) == 0; 3106 3107 if (!kvm_pause_in_guest(vcpu->kvm)) 3108 grow_ple_window(vcpu); 3109 3110 kvm_vcpu_on_spin(vcpu, in_kernel); 3111 return kvm_skip_emulated_instruction(vcpu); 3112 } 3113 3114 static int invpcid_interception(struct kvm_vcpu *vcpu) 3115 { 3116 struct vcpu_svm *svm = to_svm(vcpu); 3117 unsigned long type; 3118 gva_t gva; 3119 3120 if (!guest_cpuid_has(vcpu, X86_FEATURE_INVPCID)) { 3121 kvm_queue_exception(vcpu, UD_VECTOR); 3122 return 1; 3123 } 3124 3125 /* 3126 * For an INVPCID intercept: 3127 * EXITINFO1 provides the linear address of the memory operand. 3128 * EXITINFO2 provides the contents of the register operand. 3129 */ 3130 type = svm->vmcb->control.exit_info_2; 3131 gva = svm->vmcb->control.exit_info_1; 3132 3133 return kvm_handle_invpcid(vcpu, type, gva); 3134 } 3135 3136 static int (*const svm_exit_handlers[])(struct kvm_vcpu *vcpu) = { 3137 [SVM_EXIT_READ_CR0] = cr_interception, 3138 [SVM_EXIT_READ_CR3] = cr_interception, 3139 [SVM_EXIT_READ_CR4] = cr_interception, 3140 [SVM_EXIT_READ_CR8] = cr_interception, 3141 [SVM_EXIT_CR0_SEL_WRITE] = cr_interception, 3142 [SVM_EXIT_WRITE_CR0] = cr_interception, 3143 [SVM_EXIT_WRITE_CR3] = cr_interception, 3144 [SVM_EXIT_WRITE_CR4] = cr_interception, 3145 [SVM_EXIT_WRITE_CR8] = cr8_write_interception, 3146 [SVM_EXIT_READ_DR0] = dr_interception, 3147 [SVM_EXIT_READ_DR1] = dr_interception, 3148 [SVM_EXIT_READ_DR2] = dr_interception, 3149 [SVM_EXIT_READ_DR3] = dr_interception, 3150 [SVM_EXIT_READ_DR4] = dr_interception, 3151 [SVM_EXIT_READ_DR5] = dr_interception, 3152 [SVM_EXIT_READ_DR6] = dr_interception, 3153 [SVM_EXIT_READ_DR7] = dr_interception, 3154 [SVM_EXIT_WRITE_DR0] = dr_interception, 3155 [SVM_EXIT_WRITE_DR1] = dr_interception, 3156 [SVM_EXIT_WRITE_DR2] = dr_interception, 3157 [SVM_EXIT_WRITE_DR3] = dr_interception, 3158 [SVM_EXIT_WRITE_DR4] = dr_interception, 3159 [SVM_EXIT_WRITE_DR5] = dr_interception, 3160 [SVM_EXIT_WRITE_DR6] = dr_interception, 3161 [SVM_EXIT_WRITE_DR7] = dr_interception, 3162 [SVM_EXIT_EXCP_BASE + DB_VECTOR] = db_interception, 3163 [SVM_EXIT_EXCP_BASE + BP_VECTOR] = bp_interception, 3164 [SVM_EXIT_EXCP_BASE + UD_VECTOR] = ud_interception, 3165 [SVM_EXIT_EXCP_BASE + PF_VECTOR] = pf_interception, 3166 [SVM_EXIT_EXCP_BASE + MC_VECTOR] = mc_interception, 3167 [SVM_EXIT_EXCP_BASE + AC_VECTOR] = ac_interception, 3168 [SVM_EXIT_EXCP_BASE + GP_VECTOR] = gp_interception, 3169 [SVM_EXIT_INTR] = intr_interception, 3170 [SVM_EXIT_NMI] = nmi_interception, 3171 [SVM_EXIT_SMI] = smi_interception, 3172 [SVM_EXIT_VINTR] = interrupt_window_interception, 3173 [SVM_EXIT_RDPMC] = kvm_emulate_rdpmc, 3174 [SVM_EXIT_CPUID] = kvm_emulate_cpuid, 3175 [SVM_EXIT_IRET] = iret_interception, 3176 [SVM_EXIT_INVD] = kvm_emulate_invd, 3177 [SVM_EXIT_PAUSE] = pause_interception, 3178 [SVM_EXIT_HLT] = kvm_emulate_halt, 3179 [SVM_EXIT_INVLPG] = invlpg_interception, 3180 [SVM_EXIT_INVLPGA] = invlpga_interception, 3181 [SVM_EXIT_IOIO] = io_interception, 3182 [SVM_EXIT_MSR] = msr_interception, 3183 [SVM_EXIT_TASK_SWITCH] = task_switch_interception, 3184 [SVM_EXIT_SHUTDOWN] = shutdown_interception, 3185 [SVM_EXIT_VMRUN] = vmrun_interception, 3186 [SVM_EXIT_VMMCALL] = kvm_emulate_hypercall, 3187 [SVM_EXIT_VMLOAD] = vmload_interception, 3188 [SVM_EXIT_VMSAVE] = vmsave_interception, 3189 [SVM_EXIT_STGI] = stgi_interception, 3190 [SVM_EXIT_CLGI] = clgi_interception, 3191 [SVM_EXIT_SKINIT] = skinit_interception, 3192 [SVM_EXIT_RDTSCP] = kvm_handle_invalid_op, 3193 [SVM_EXIT_WBINVD] = kvm_emulate_wbinvd, 3194 [SVM_EXIT_MONITOR] = kvm_emulate_monitor, 3195 [SVM_EXIT_MWAIT] = kvm_emulate_mwait, 3196 [SVM_EXIT_XSETBV] = kvm_emulate_xsetbv, 3197 [SVM_EXIT_RDPRU] = kvm_handle_invalid_op, 3198 [SVM_EXIT_EFER_WRITE_TRAP] = efer_trap, 3199 [SVM_EXIT_CR0_WRITE_TRAP] = cr_trap, 3200 [SVM_EXIT_CR4_WRITE_TRAP] = cr_trap, 3201 [SVM_EXIT_CR8_WRITE_TRAP] = cr_trap, 3202 [SVM_EXIT_INVPCID] = invpcid_interception, 3203 [SVM_EXIT_NPF] = npf_interception, 3204 [SVM_EXIT_RSM] = rsm_interception, 3205 [SVM_EXIT_AVIC_INCOMPLETE_IPI] = avic_incomplete_ipi_interception, 3206 [SVM_EXIT_AVIC_UNACCELERATED_ACCESS] = avic_unaccelerated_access_interception, 3207 [SVM_EXIT_VMGEXIT] = sev_handle_vmgexit, 3208 }; 3209 3210 static void dump_vmcb(struct kvm_vcpu *vcpu) 3211 { 3212 struct vcpu_svm *svm = to_svm(vcpu); 3213 struct vmcb_control_area *control = &svm->vmcb->control; 3214 struct vmcb_save_area *save = &svm->vmcb->save; 3215 struct vmcb_save_area *save01 = &svm->vmcb01.ptr->save; 3216 3217 if (!dump_invalid_vmcb) { 3218 pr_warn_ratelimited("set kvm_amd.dump_invalid_vmcb=1 to dump internal KVM state.\n"); 3219 return; 3220 } 3221 3222 pr_err("VMCB %p, last attempted VMRUN on CPU %d\n", 3223 svm->current_vmcb->ptr, vcpu->arch.last_vmentry_cpu); 3224 pr_err("VMCB Control Area:\n"); 3225 pr_err("%-20s%04x\n", "cr_read:", control->intercepts[INTERCEPT_CR] & 0xffff); 3226 pr_err("%-20s%04x\n", "cr_write:", control->intercepts[INTERCEPT_CR] >> 16); 3227 pr_err("%-20s%04x\n", "dr_read:", control->intercepts[INTERCEPT_DR] & 0xffff); 3228 pr_err("%-20s%04x\n", "dr_write:", control->intercepts[INTERCEPT_DR] >> 16); 3229 pr_err("%-20s%08x\n", "exceptions:", control->intercepts[INTERCEPT_EXCEPTION]); 3230 pr_err("%-20s%08x %08x\n", "intercepts:", 3231 control->intercepts[INTERCEPT_WORD3], 3232 control->intercepts[INTERCEPT_WORD4]); 3233 pr_err("%-20s%d\n", "pause filter count:", control->pause_filter_count); 3234 pr_err("%-20s%d\n", "pause filter threshold:", 3235 control->pause_filter_thresh); 3236 pr_err("%-20s%016llx\n", "iopm_base_pa:", control->iopm_base_pa); 3237 pr_err("%-20s%016llx\n", "msrpm_base_pa:", control->msrpm_base_pa); 3238 pr_err("%-20s%016llx\n", "tsc_offset:", control->tsc_offset); 3239 pr_err("%-20s%d\n", "asid:", control->asid); 3240 pr_err("%-20s%d\n", "tlb_ctl:", control->tlb_ctl); 3241 pr_err("%-20s%08x\n", "int_ctl:", control->int_ctl); 3242 pr_err("%-20s%08x\n", "int_vector:", control->int_vector); 3243 pr_err("%-20s%08x\n", "int_state:", control->int_state); 3244 pr_err("%-20s%08x\n", "exit_code:", control->exit_code); 3245 pr_err("%-20s%016llx\n", "exit_info1:", control->exit_info_1); 3246 pr_err("%-20s%016llx\n", "exit_info2:", control->exit_info_2); 3247 pr_err("%-20s%08x\n", "exit_int_info:", control->exit_int_info); 3248 pr_err("%-20s%08x\n", "exit_int_info_err:", control->exit_int_info_err); 3249 pr_err("%-20s%lld\n", "nested_ctl:", control->nested_ctl); 3250 pr_err("%-20s%016llx\n", "nested_cr3:", control->nested_cr3); 3251 pr_err("%-20s%016llx\n", "avic_vapic_bar:", control->avic_vapic_bar); 3252 pr_err("%-20s%016llx\n", "ghcb:", control->ghcb_gpa); 3253 pr_err("%-20s%08x\n", "event_inj:", control->event_inj); 3254 pr_err("%-20s%08x\n", "event_inj_err:", control->event_inj_err); 3255 pr_err("%-20s%lld\n", "virt_ext:", control->virt_ext); 3256 pr_err("%-20s%016llx\n", "next_rip:", control->next_rip); 3257 pr_err("%-20s%016llx\n", "avic_backing_page:", control->avic_backing_page); 3258 pr_err("%-20s%016llx\n", "avic_logical_id:", control->avic_logical_id); 3259 pr_err("%-20s%016llx\n", "avic_physical_id:", control->avic_physical_id); 3260 pr_err("%-20s%016llx\n", "vmsa_pa:", control->vmsa_pa); 3261 pr_err("VMCB State Save Area:\n"); 3262 pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n", 3263 "es:", 3264 save->es.selector, save->es.attrib, 3265 save->es.limit, save->es.base); 3266 pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n", 3267 "cs:", 3268 save->cs.selector, save->cs.attrib, 3269 save->cs.limit, save->cs.base); 3270 pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n", 3271 "ss:", 3272 save->ss.selector, save->ss.attrib, 3273 save->ss.limit, save->ss.base); 3274 pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n", 3275 "ds:", 3276 save->ds.selector, save->ds.attrib, 3277 save->ds.limit, save->ds.base); 3278 pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n", 3279 "fs:", 3280 save01->fs.selector, save01->fs.attrib, 3281 save01->fs.limit, save01->fs.base); 3282 pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n", 3283 "gs:", 3284 save01->gs.selector, save01->gs.attrib, 3285 save01->gs.limit, save01->gs.base); 3286 pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n", 3287 "gdtr:", 3288 save->gdtr.selector, save->gdtr.attrib, 3289 save->gdtr.limit, save->gdtr.base); 3290 pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n", 3291 "ldtr:", 3292 save01->ldtr.selector, save01->ldtr.attrib, 3293 save01->ldtr.limit, save01->ldtr.base); 3294 pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n", 3295 "idtr:", 3296 save->idtr.selector, save->idtr.attrib, 3297 save->idtr.limit, save->idtr.base); 3298 pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n", 3299 "tr:", 3300 save01->tr.selector, save01->tr.attrib, 3301 save01->tr.limit, save01->tr.base); 3302 pr_err("cpl: %d efer: %016llx\n", 3303 save->cpl, save->efer); 3304 pr_err("%-15s %016llx %-13s %016llx\n", 3305 "cr0:", save->cr0, "cr2:", save->cr2); 3306 pr_err("%-15s %016llx %-13s %016llx\n", 3307 "cr3:", save->cr3, "cr4:", save->cr4); 3308 pr_err("%-15s %016llx %-13s %016llx\n", 3309 "dr6:", save->dr6, "dr7:", save->dr7); 3310 pr_err("%-15s %016llx %-13s %016llx\n", 3311 "rip:", save->rip, "rflags:", save->rflags); 3312 pr_err("%-15s %016llx %-13s %016llx\n", 3313 "rsp:", save->rsp, "rax:", save->rax); 3314 pr_err("%-15s %016llx %-13s %016llx\n", 3315 "star:", save01->star, "lstar:", save01->lstar); 3316 pr_err("%-15s %016llx %-13s %016llx\n", 3317 "cstar:", save01->cstar, "sfmask:", save01->sfmask); 3318 pr_err("%-15s %016llx %-13s %016llx\n", 3319 "kernel_gs_base:", save01->kernel_gs_base, 3320 "sysenter_cs:", save01->sysenter_cs); 3321 pr_err("%-15s %016llx %-13s %016llx\n", 3322 "sysenter_esp:", save01->sysenter_esp, 3323 "sysenter_eip:", save01->sysenter_eip); 3324 pr_err("%-15s %016llx %-13s %016llx\n", 3325 "gpat:", save->g_pat, "dbgctl:", save->dbgctl); 3326 pr_err("%-15s %016llx %-13s %016llx\n", 3327 "br_from:", save->br_from, "br_to:", save->br_to); 3328 pr_err("%-15s %016llx %-13s %016llx\n", 3329 "excp_from:", save->last_excp_from, 3330 "excp_to:", save->last_excp_to); 3331 } 3332 3333 static bool svm_check_exit_valid(struct kvm_vcpu *vcpu, u64 exit_code) 3334 { 3335 return (exit_code < ARRAY_SIZE(svm_exit_handlers) && 3336 svm_exit_handlers[exit_code]); 3337 } 3338 3339 static int svm_handle_invalid_exit(struct kvm_vcpu *vcpu, u64 exit_code) 3340 { 3341 vcpu_unimpl(vcpu, "svm: unexpected exit reason 0x%llx\n", exit_code); 3342 dump_vmcb(vcpu); 3343 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR; 3344 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_UNEXPECTED_EXIT_REASON; 3345 vcpu->run->internal.ndata = 2; 3346 vcpu->run->internal.data[0] = exit_code; 3347 vcpu->run->internal.data[1] = vcpu->arch.last_vmentry_cpu; 3348 return 0; 3349 } 3350 3351 int svm_invoke_exit_handler(struct kvm_vcpu *vcpu, u64 exit_code) 3352 { 3353 if (!svm_check_exit_valid(vcpu, exit_code)) 3354 return svm_handle_invalid_exit(vcpu, exit_code); 3355 3356 #ifdef CONFIG_RETPOLINE 3357 if (exit_code == SVM_EXIT_MSR) 3358 return msr_interception(vcpu); 3359 else if (exit_code == SVM_EXIT_VINTR) 3360 return interrupt_window_interception(vcpu); 3361 else if (exit_code == SVM_EXIT_INTR) 3362 return intr_interception(vcpu); 3363 else if (exit_code == SVM_EXIT_HLT) 3364 return kvm_emulate_halt(vcpu); 3365 else if (exit_code == SVM_EXIT_NPF) 3366 return npf_interception(vcpu); 3367 #endif 3368 return svm_exit_handlers[exit_code](vcpu); 3369 } 3370 3371 static void svm_get_exit_info(struct kvm_vcpu *vcpu, u32 *reason, 3372 u64 *info1, u64 *info2, 3373 u32 *intr_info, u32 *error_code) 3374 { 3375 struct vmcb_control_area *control = &to_svm(vcpu)->vmcb->control; 3376 3377 *reason = control->exit_code; 3378 *info1 = control->exit_info_1; 3379 *info2 = control->exit_info_2; 3380 *intr_info = control->exit_int_info; 3381 if ((*intr_info & SVM_EXITINTINFO_VALID) && 3382 (*intr_info & SVM_EXITINTINFO_VALID_ERR)) 3383 *error_code = control->exit_int_info_err; 3384 else 3385 *error_code = 0; 3386 } 3387 3388 static int handle_exit(struct kvm_vcpu *vcpu, fastpath_t exit_fastpath) 3389 { 3390 struct vcpu_svm *svm = to_svm(vcpu); 3391 struct kvm_run *kvm_run = vcpu->run; 3392 u32 exit_code = svm->vmcb->control.exit_code; 3393 3394 trace_kvm_exit(vcpu, KVM_ISA_SVM); 3395 3396 /* SEV-ES guests must use the CR write traps to track CR registers. */ 3397 if (!sev_es_guest(vcpu->kvm)) { 3398 if (!svm_is_intercept(svm, INTERCEPT_CR0_WRITE)) 3399 vcpu->arch.cr0 = svm->vmcb->save.cr0; 3400 if (npt_enabled) 3401 vcpu->arch.cr3 = svm->vmcb->save.cr3; 3402 } 3403 3404 if (is_guest_mode(vcpu)) { 3405 int vmexit; 3406 3407 trace_kvm_nested_vmexit(vcpu, KVM_ISA_SVM); 3408 3409 vmexit = nested_svm_exit_special(svm); 3410 3411 if (vmexit == NESTED_EXIT_CONTINUE) 3412 vmexit = nested_svm_exit_handled(svm); 3413 3414 if (vmexit == NESTED_EXIT_DONE) 3415 return 1; 3416 } 3417 3418 if (svm->vmcb->control.exit_code == SVM_EXIT_ERR) { 3419 kvm_run->exit_reason = KVM_EXIT_FAIL_ENTRY; 3420 kvm_run->fail_entry.hardware_entry_failure_reason 3421 = svm->vmcb->control.exit_code; 3422 kvm_run->fail_entry.cpu = vcpu->arch.last_vmentry_cpu; 3423 dump_vmcb(vcpu); 3424 return 0; 3425 } 3426 3427 if (is_external_interrupt(svm->vmcb->control.exit_int_info) && 3428 exit_code != SVM_EXIT_EXCP_BASE + PF_VECTOR && 3429 exit_code != SVM_EXIT_NPF && exit_code != SVM_EXIT_TASK_SWITCH && 3430 exit_code != SVM_EXIT_INTR && exit_code != SVM_EXIT_NMI) 3431 printk(KERN_ERR "%s: unexpected exit_int_info 0x%x " 3432 "exit_code 0x%x\n", 3433 __func__, svm->vmcb->control.exit_int_info, 3434 exit_code); 3435 3436 if (exit_fastpath != EXIT_FASTPATH_NONE) 3437 return 1; 3438 3439 return svm_invoke_exit_handler(vcpu, exit_code); 3440 } 3441 3442 static void reload_tss(struct kvm_vcpu *vcpu) 3443 { 3444 struct svm_cpu_data *sd = per_cpu(svm_data, vcpu->cpu); 3445 3446 sd->tss_desc->type = 9; /* available 32/64-bit TSS */ 3447 load_TR_desc(); 3448 } 3449 3450 static void pre_svm_run(struct kvm_vcpu *vcpu) 3451 { 3452 struct svm_cpu_data *sd = per_cpu(svm_data, vcpu->cpu); 3453 struct vcpu_svm *svm = to_svm(vcpu); 3454 3455 /* 3456 * If the previous vmrun of the vmcb occurred on a different physical 3457 * cpu, then mark the vmcb dirty and assign a new asid. Hardware's 3458 * vmcb clean bits are per logical CPU, as are KVM's asid assignments. 3459 */ 3460 if (unlikely(svm->current_vmcb->cpu != vcpu->cpu)) { 3461 svm->current_vmcb->asid_generation = 0; 3462 vmcb_mark_all_dirty(svm->vmcb); 3463 svm->current_vmcb->cpu = vcpu->cpu; 3464 } 3465 3466 if (sev_guest(vcpu->kvm)) 3467 return pre_sev_run(svm, vcpu->cpu); 3468 3469 /* FIXME: handle wraparound of asid_generation */ 3470 if (svm->current_vmcb->asid_generation != sd->asid_generation) 3471 new_asid(svm, sd); 3472 } 3473 3474 static void svm_inject_nmi(struct kvm_vcpu *vcpu) 3475 { 3476 struct vcpu_svm *svm = to_svm(vcpu); 3477 3478 svm->vmcb->control.event_inj = SVM_EVTINJ_VALID | SVM_EVTINJ_TYPE_NMI; 3479 vcpu->arch.hflags |= HF_NMI_MASK; 3480 if (!sev_es_guest(vcpu->kvm)) 3481 svm_set_intercept(svm, INTERCEPT_IRET); 3482 ++vcpu->stat.nmi_injections; 3483 } 3484 3485 static void svm_set_irq(struct kvm_vcpu *vcpu) 3486 { 3487 struct vcpu_svm *svm = to_svm(vcpu); 3488 3489 BUG_ON(!(gif_set(svm))); 3490 3491 trace_kvm_inj_virq(vcpu->arch.interrupt.nr); 3492 ++vcpu->stat.irq_injections; 3493 3494 svm->vmcb->control.event_inj = vcpu->arch.interrupt.nr | 3495 SVM_EVTINJ_VALID | SVM_EVTINJ_TYPE_INTR; 3496 } 3497 3498 static void svm_update_cr8_intercept(struct kvm_vcpu *vcpu, int tpr, int irr) 3499 { 3500 struct vcpu_svm *svm = to_svm(vcpu); 3501 3502 /* 3503 * SEV-ES guests must always keep the CR intercepts cleared. CR 3504 * tracking is done using the CR write traps. 3505 */ 3506 if (sev_es_guest(vcpu->kvm)) 3507 return; 3508 3509 if (nested_svm_virtualize_tpr(vcpu)) 3510 return; 3511 3512 svm_clr_intercept(svm, INTERCEPT_CR8_WRITE); 3513 3514 if (irr == -1) 3515 return; 3516 3517 if (tpr >= irr) 3518 svm_set_intercept(svm, INTERCEPT_CR8_WRITE); 3519 } 3520 3521 bool svm_nmi_blocked(struct kvm_vcpu *vcpu) 3522 { 3523 struct vcpu_svm *svm = to_svm(vcpu); 3524 struct vmcb *vmcb = svm->vmcb; 3525 bool ret; 3526 3527 if (!gif_set(svm)) 3528 return true; 3529 3530 if (is_guest_mode(vcpu) && nested_exit_on_nmi(svm)) 3531 return false; 3532 3533 ret = (vmcb->control.int_state & SVM_INTERRUPT_SHADOW_MASK) || 3534 (vcpu->arch.hflags & HF_NMI_MASK); 3535 3536 return ret; 3537 } 3538 3539 static int svm_nmi_allowed(struct kvm_vcpu *vcpu, bool for_injection) 3540 { 3541 struct vcpu_svm *svm = to_svm(vcpu); 3542 if (svm->nested.nested_run_pending) 3543 return -EBUSY; 3544 3545 /* An NMI must not be injected into L2 if it's supposed to VM-Exit. */ 3546 if (for_injection && is_guest_mode(vcpu) && nested_exit_on_nmi(svm)) 3547 return -EBUSY; 3548 3549 return !svm_nmi_blocked(vcpu); 3550 } 3551 3552 static bool svm_get_nmi_mask(struct kvm_vcpu *vcpu) 3553 { 3554 return !!(vcpu->arch.hflags & HF_NMI_MASK); 3555 } 3556 3557 static void svm_set_nmi_mask(struct kvm_vcpu *vcpu, bool masked) 3558 { 3559 struct vcpu_svm *svm = to_svm(vcpu); 3560 3561 if (masked) { 3562 vcpu->arch.hflags |= HF_NMI_MASK; 3563 if (!sev_es_guest(vcpu->kvm)) 3564 svm_set_intercept(svm, INTERCEPT_IRET); 3565 } else { 3566 vcpu->arch.hflags &= ~HF_NMI_MASK; 3567 if (!sev_es_guest(vcpu->kvm)) 3568 svm_clr_intercept(svm, INTERCEPT_IRET); 3569 } 3570 } 3571 3572 bool svm_interrupt_blocked(struct kvm_vcpu *vcpu) 3573 { 3574 struct vcpu_svm *svm = to_svm(vcpu); 3575 struct vmcb *vmcb = svm->vmcb; 3576 3577 if (!gif_set(svm)) 3578 return true; 3579 3580 if (is_guest_mode(vcpu)) { 3581 /* As long as interrupts are being delivered... */ 3582 if ((svm->nested.ctl.int_ctl & V_INTR_MASKING_MASK) 3583 ? !(svm->vmcb01.ptr->save.rflags & X86_EFLAGS_IF) 3584 : !(kvm_get_rflags(vcpu) & X86_EFLAGS_IF)) 3585 return true; 3586 3587 /* ... vmexits aren't blocked by the interrupt shadow */ 3588 if (nested_exit_on_intr(svm)) 3589 return false; 3590 } else { 3591 if (!svm_get_if_flag(vcpu)) 3592 return true; 3593 } 3594 3595 return (vmcb->control.int_state & SVM_INTERRUPT_SHADOW_MASK); 3596 } 3597 3598 static int svm_interrupt_allowed(struct kvm_vcpu *vcpu, bool for_injection) 3599 { 3600 struct vcpu_svm *svm = to_svm(vcpu); 3601 if (svm->nested.nested_run_pending) 3602 return -EBUSY; 3603 3604 /* 3605 * An IRQ must not be injected into L2 if it's supposed to VM-Exit, 3606 * e.g. if the IRQ arrived asynchronously after checking nested events. 3607 */ 3608 if (for_injection && is_guest_mode(vcpu) && nested_exit_on_intr(svm)) 3609 return -EBUSY; 3610 3611 return !svm_interrupt_blocked(vcpu); 3612 } 3613 3614 static void svm_enable_irq_window(struct kvm_vcpu *vcpu) 3615 { 3616 struct vcpu_svm *svm = to_svm(vcpu); 3617 3618 /* 3619 * In case GIF=0 we can't rely on the CPU to tell us when GIF becomes 3620 * 1, because that's a separate STGI/VMRUN intercept. The next time we 3621 * get that intercept, this function will be called again though and 3622 * we'll get the vintr intercept. However, if the vGIF feature is 3623 * enabled, the STGI interception will not occur. Enable the irq 3624 * window under the assumption that the hardware will set the GIF. 3625 */ 3626 if (vgif_enabled(svm) || gif_set(svm)) { 3627 /* 3628 * IRQ window is not needed when AVIC is enabled, 3629 * unless we have pending ExtINT since it cannot be injected 3630 * via AVIC. In such case, we need to temporarily disable AVIC, 3631 * and fallback to injecting IRQ via V_IRQ. 3632 */ 3633 kvm_request_apicv_update(vcpu->kvm, false, APICV_INHIBIT_REASON_IRQWIN); 3634 svm_set_vintr(svm); 3635 } 3636 } 3637 3638 static void svm_enable_nmi_window(struct kvm_vcpu *vcpu) 3639 { 3640 struct vcpu_svm *svm = to_svm(vcpu); 3641 3642 if ((vcpu->arch.hflags & (HF_NMI_MASK | HF_IRET_MASK)) == HF_NMI_MASK) 3643 return; /* IRET will cause a vm exit */ 3644 3645 if (!gif_set(svm)) { 3646 if (vgif_enabled(svm)) 3647 svm_set_intercept(svm, INTERCEPT_STGI); 3648 return; /* STGI will cause a vm exit */ 3649 } 3650 3651 /* 3652 * Something prevents NMI from been injected. Single step over possible 3653 * problem (IRET or exception injection or interrupt shadow) 3654 */ 3655 svm->nmi_singlestep_guest_rflags = svm_get_rflags(vcpu); 3656 svm->nmi_singlestep = true; 3657 svm->vmcb->save.rflags |= (X86_EFLAGS_TF | X86_EFLAGS_RF); 3658 } 3659 3660 static int svm_set_tss_addr(struct kvm *kvm, unsigned int addr) 3661 { 3662 return 0; 3663 } 3664 3665 static int svm_set_identity_map_addr(struct kvm *kvm, u64 ident_addr) 3666 { 3667 return 0; 3668 } 3669 3670 void svm_flush_tlb(struct kvm_vcpu *vcpu) 3671 { 3672 struct vcpu_svm *svm = to_svm(vcpu); 3673 3674 /* 3675 * Flush only the current ASID even if the TLB flush was invoked via 3676 * kvm_flush_remote_tlbs(). Although flushing remote TLBs requires all 3677 * ASIDs to be flushed, KVM uses a single ASID for L1 and L2, and 3678 * unconditionally does a TLB flush on both nested VM-Enter and nested 3679 * VM-Exit (via kvm_mmu_reset_context()). 3680 */ 3681 if (static_cpu_has(X86_FEATURE_FLUSHBYASID)) 3682 svm->vmcb->control.tlb_ctl = TLB_CONTROL_FLUSH_ASID; 3683 else 3684 svm->current_vmcb->asid_generation--; 3685 } 3686 3687 static void svm_flush_tlb_gva(struct kvm_vcpu *vcpu, gva_t gva) 3688 { 3689 struct vcpu_svm *svm = to_svm(vcpu); 3690 3691 invlpga(gva, svm->vmcb->control.asid); 3692 } 3693 3694 static inline void sync_cr8_to_lapic(struct kvm_vcpu *vcpu) 3695 { 3696 struct vcpu_svm *svm = to_svm(vcpu); 3697 3698 if (nested_svm_virtualize_tpr(vcpu)) 3699 return; 3700 3701 if (!svm_is_intercept(svm, INTERCEPT_CR8_WRITE)) { 3702 int cr8 = svm->vmcb->control.int_ctl & V_TPR_MASK; 3703 kvm_set_cr8(vcpu, cr8); 3704 } 3705 } 3706 3707 static inline void sync_lapic_to_cr8(struct kvm_vcpu *vcpu) 3708 { 3709 struct vcpu_svm *svm = to_svm(vcpu); 3710 u64 cr8; 3711 3712 if (nested_svm_virtualize_tpr(vcpu) || 3713 kvm_vcpu_apicv_active(vcpu)) 3714 return; 3715 3716 cr8 = kvm_get_cr8(vcpu); 3717 svm->vmcb->control.int_ctl &= ~V_TPR_MASK; 3718 svm->vmcb->control.int_ctl |= cr8 & V_TPR_MASK; 3719 } 3720 3721 static void svm_complete_interrupts(struct kvm_vcpu *vcpu) 3722 { 3723 struct vcpu_svm *svm = to_svm(vcpu); 3724 u8 vector; 3725 int type; 3726 u32 exitintinfo = svm->vmcb->control.exit_int_info; 3727 unsigned int3_injected = svm->int3_injected; 3728 3729 svm->int3_injected = 0; 3730 3731 /* 3732 * If we've made progress since setting HF_IRET_MASK, we've 3733 * executed an IRET and can allow NMI injection. 3734 */ 3735 if ((vcpu->arch.hflags & HF_IRET_MASK) && 3736 (sev_es_guest(vcpu->kvm) || 3737 kvm_rip_read(vcpu) != svm->nmi_iret_rip)) { 3738 vcpu->arch.hflags &= ~(HF_NMI_MASK | HF_IRET_MASK); 3739 kvm_make_request(KVM_REQ_EVENT, vcpu); 3740 } 3741 3742 vcpu->arch.nmi_injected = false; 3743 kvm_clear_exception_queue(vcpu); 3744 kvm_clear_interrupt_queue(vcpu); 3745 3746 if (!(exitintinfo & SVM_EXITINTINFO_VALID)) 3747 return; 3748 3749 kvm_make_request(KVM_REQ_EVENT, vcpu); 3750 3751 vector = exitintinfo & SVM_EXITINTINFO_VEC_MASK; 3752 type = exitintinfo & SVM_EXITINTINFO_TYPE_MASK; 3753 3754 switch (type) { 3755 case SVM_EXITINTINFO_TYPE_NMI: 3756 vcpu->arch.nmi_injected = true; 3757 break; 3758 case SVM_EXITINTINFO_TYPE_EXEPT: 3759 /* 3760 * Never re-inject a #VC exception. 3761 */ 3762 if (vector == X86_TRAP_VC) 3763 break; 3764 3765 /* 3766 * In case of software exceptions, do not reinject the vector, 3767 * but re-execute the instruction instead. Rewind RIP first 3768 * if we emulated INT3 before. 3769 */ 3770 if (kvm_exception_is_soft(vector)) { 3771 if (vector == BP_VECTOR && int3_injected && 3772 kvm_is_linear_rip(vcpu, svm->int3_rip)) 3773 kvm_rip_write(vcpu, 3774 kvm_rip_read(vcpu) - int3_injected); 3775 break; 3776 } 3777 if (exitintinfo & SVM_EXITINTINFO_VALID_ERR) { 3778 u32 err = svm->vmcb->control.exit_int_info_err; 3779 kvm_requeue_exception_e(vcpu, vector, err); 3780 3781 } else 3782 kvm_requeue_exception(vcpu, vector); 3783 break; 3784 case SVM_EXITINTINFO_TYPE_INTR: 3785 kvm_queue_interrupt(vcpu, vector, false); 3786 break; 3787 default: 3788 break; 3789 } 3790 } 3791 3792 static void svm_cancel_injection(struct kvm_vcpu *vcpu) 3793 { 3794 struct vcpu_svm *svm = to_svm(vcpu); 3795 struct vmcb_control_area *control = &svm->vmcb->control; 3796 3797 control->exit_int_info = control->event_inj; 3798 control->exit_int_info_err = control->event_inj_err; 3799 control->event_inj = 0; 3800 svm_complete_interrupts(vcpu); 3801 } 3802 3803 static fastpath_t svm_exit_handlers_fastpath(struct kvm_vcpu *vcpu) 3804 { 3805 if (to_svm(vcpu)->vmcb->control.exit_code == SVM_EXIT_MSR && 3806 to_svm(vcpu)->vmcb->control.exit_info_1) 3807 return handle_fastpath_set_msr_irqoff(vcpu); 3808 3809 return EXIT_FASTPATH_NONE; 3810 } 3811 3812 static noinstr void svm_vcpu_enter_exit(struct kvm_vcpu *vcpu) 3813 { 3814 struct vcpu_svm *svm = to_svm(vcpu); 3815 unsigned long vmcb_pa = svm->current_vmcb->pa; 3816 3817 kvm_guest_enter_irqoff(); 3818 3819 if (sev_es_guest(vcpu->kvm)) { 3820 __svm_sev_es_vcpu_run(vmcb_pa); 3821 } else { 3822 struct svm_cpu_data *sd = per_cpu(svm_data, vcpu->cpu); 3823 3824 /* 3825 * Use a single vmcb (vmcb01 because it's always valid) for 3826 * context switching guest state via VMLOAD/VMSAVE, that way 3827 * the state doesn't need to be copied between vmcb01 and 3828 * vmcb02 when switching vmcbs for nested virtualization. 3829 */ 3830 vmload(svm->vmcb01.pa); 3831 __svm_vcpu_run(vmcb_pa, (unsigned long *)&vcpu->arch.regs); 3832 vmsave(svm->vmcb01.pa); 3833 3834 vmload(__sme_page_pa(sd->save_area)); 3835 } 3836 3837 kvm_guest_exit_irqoff(); 3838 } 3839 3840 static __no_kcsan fastpath_t svm_vcpu_run(struct kvm_vcpu *vcpu) 3841 { 3842 struct vcpu_svm *svm = to_svm(vcpu); 3843 3844 trace_kvm_entry(vcpu); 3845 3846 svm->vmcb->save.rax = vcpu->arch.regs[VCPU_REGS_RAX]; 3847 svm->vmcb->save.rsp = vcpu->arch.regs[VCPU_REGS_RSP]; 3848 svm->vmcb->save.rip = vcpu->arch.regs[VCPU_REGS_RIP]; 3849 3850 /* 3851 * Disable singlestep if we're injecting an interrupt/exception. 3852 * We don't want our modified rflags to be pushed on the stack where 3853 * we might not be able to easily reset them if we disabled NMI 3854 * singlestep later. 3855 */ 3856 if (svm->nmi_singlestep && svm->vmcb->control.event_inj) { 3857 /* 3858 * Event injection happens before external interrupts cause a 3859 * vmexit and interrupts are disabled here, so smp_send_reschedule 3860 * is enough to force an immediate vmexit. 3861 */ 3862 disable_nmi_singlestep(svm); 3863 smp_send_reschedule(vcpu->cpu); 3864 } 3865 3866 pre_svm_run(vcpu); 3867 3868 sync_lapic_to_cr8(vcpu); 3869 3870 if (unlikely(svm->asid != svm->vmcb->control.asid)) { 3871 svm->vmcb->control.asid = svm->asid; 3872 vmcb_mark_dirty(svm->vmcb, VMCB_ASID); 3873 } 3874 svm->vmcb->save.cr2 = vcpu->arch.cr2; 3875 3876 svm_hv_update_vp_id(svm->vmcb, vcpu); 3877 3878 /* 3879 * Run with all-zero DR6 unless needed, so that we can get the exact cause 3880 * of a #DB. 3881 */ 3882 if (unlikely(vcpu->arch.switch_db_regs & KVM_DEBUGREG_WONT_EXIT)) 3883 svm_set_dr6(svm, vcpu->arch.dr6); 3884 else 3885 svm_set_dr6(svm, DR6_ACTIVE_LOW); 3886 3887 clgi(); 3888 kvm_load_guest_xsave_state(vcpu); 3889 3890 kvm_wait_lapic_expire(vcpu); 3891 3892 /* 3893 * If this vCPU has touched SPEC_CTRL, restore the guest's value if 3894 * it's non-zero. Since vmentry is serialising on affected CPUs, there 3895 * is no need to worry about the conditional branch over the wrmsr 3896 * being speculatively taken. 3897 */ 3898 if (!static_cpu_has(X86_FEATURE_V_SPEC_CTRL)) 3899 x86_spec_ctrl_set_guest(svm->spec_ctrl, svm->virt_spec_ctrl); 3900 3901 svm_vcpu_enter_exit(vcpu); 3902 3903 /* 3904 * We do not use IBRS in the kernel. If this vCPU has used the 3905 * SPEC_CTRL MSR it may have left it on; save the value and 3906 * turn it off. This is much more efficient than blindly adding 3907 * it to the atomic save/restore list. Especially as the former 3908 * (Saving guest MSRs on vmexit) doesn't even exist in KVM. 3909 * 3910 * For non-nested case: 3911 * If the L01 MSR bitmap does not intercept the MSR, then we need to 3912 * save it. 3913 * 3914 * For nested case: 3915 * If the L02 MSR bitmap does not intercept the MSR, then we need to 3916 * save it. 3917 */ 3918 if (!static_cpu_has(X86_FEATURE_V_SPEC_CTRL) && 3919 unlikely(!msr_write_intercepted(vcpu, MSR_IA32_SPEC_CTRL))) 3920 svm->spec_ctrl = native_read_msr(MSR_IA32_SPEC_CTRL); 3921 3922 if (!sev_es_guest(vcpu->kvm)) 3923 reload_tss(vcpu); 3924 3925 if (!static_cpu_has(X86_FEATURE_V_SPEC_CTRL)) 3926 x86_spec_ctrl_restore_host(svm->spec_ctrl, svm->virt_spec_ctrl); 3927 3928 if (!sev_es_guest(vcpu->kvm)) { 3929 vcpu->arch.cr2 = svm->vmcb->save.cr2; 3930 vcpu->arch.regs[VCPU_REGS_RAX] = svm->vmcb->save.rax; 3931 vcpu->arch.regs[VCPU_REGS_RSP] = svm->vmcb->save.rsp; 3932 vcpu->arch.regs[VCPU_REGS_RIP] = svm->vmcb->save.rip; 3933 } 3934 3935 if (unlikely(svm->vmcb->control.exit_code == SVM_EXIT_NMI)) 3936 kvm_before_interrupt(vcpu); 3937 3938 kvm_load_host_xsave_state(vcpu); 3939 stgi(); 3940 3941 /* Any pending NMI will happen here */ 3942 3943 if (unlikely(svm->vmcb->control.exit_code == SVM_EXIT_NMI)) 3944 kvm_after_interrupt(vcpu); 3945 3946 sync_cr8_to_lapic(vcpu); 3947 3948 svm->next_rip = 0; 3949 if (is_guest_mode(vcpu)) { 3950 nested_sync_control_from_vmcb02(svm); 3951 3952 /* Track VMRUNs that have made past consistency checking */ 3953 if (svm->nested.nested_run_pending && 3954 svm->vmcb->control.exit_code != SVM_EXIT_ERR) 3955 ++vcpu->stat.nested_run; 3956 3957 svm->nested.nested_run_pending = 0; 3958 } 3959 3960 svm->vmcb->control.tlb_ctl = TLB_CONTROL_DO_NOTHING; 3961 vmcb_mark_all_clean(svm->vmcb); 3962 3963 /* if exit due to PF check for async PF */ 3964 if (svm->vmcb->control.exit_code == SVM_EXIT_EXCP_BASE + PF_VECTOR) 3965 vcpu->arch.apf.host_apf_flags = 3966 kvm_read_and_reset_apf_flags(); 3967 3968 if (npt_enabled) 3969 kvm_register_clear_available(vcpu, VCPU_EXREG_PDPTR); 3970 3971 /* 3972 * We need to handle MC intercepts here before the vcpu has a chance to 3973 * change the physical cpu 3974 */ 3975 if (unlikely(svm->vmcb->control.exit_code == 3976 SVM_EXIT_EXCP_BASE + MC_VECTOR)) 3977 svm_handle_mce(vcpu); 3978 3979 svm_complete_interrupts(vcpu); 3980 3981 if (is_guest_mode(vcpu)) 3982 return EXIT_FASTPATH_NONE; 3983 3984 return svm_exit_handlers_fastpath(vcpu); 3985 } 3986 3987 static void svm_load_mmu_pgd(struct kvm_vcpu *vcpu, hpa_t root_hpa, 3988 int root_level) 3989 { 3990 struct vcpu_svm *svm = to_svm(vcpu); 3991 unsigned long cr3; 3992 3993 if (npt_enabled) { 3994 svm->vmcb->control.nested_cr3 = __sme_set(root_hpa); 3995 vmcb_mark_dirty(svm->vmcb, VMCB_NPT); 3996 3997 hv_track_root_tdp(vcpu, root_hpa); 3998 3999 /* Loading L2's CR3 is handled by enter_svm_guest_mode. */ 4000 if (!test_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail)) 4001 return; 4002 cr3 = vcpu->arch.cr3; 4003 } else if (vcpu->arch.mmu->shadow_root_level >= PT64_ROOT_4LEVEL) { 4004 cr3 = __sme_set(root_hpa) | kvm_get_active_pcid(vcpu); 4005 } else { 4006 /* PCID in the guest should be impossible with a 32-bit MMU. */ 4007 WARN_ON_ONCE(kvm_get_active_pcid(vcpu)); 4008 cr3 = root_hpa; 4009 } 4010 4011 svm->vmcb->save.cr3 = cr3; 4012 vmcb_mark_dirty(svm->vmcb, VMCB_CR); 4013 } 4014 4015 static int is_disabled(void) 4016 { 4017 u64 vm_cr; 4018 4019 rdmsrl(MSR_VM_CR, vm_cr); 4020 if (vm_cr & (1 << SVM_VM_CR_SVM_DISABLE)) 4021 return 1; 4022 4023 return 0; 4024 } 4025 4026 static void 4027 svm_patch_hypercall(struct kvm_vcpu *vcpu, unsigned char *hypercall) 4028 { 4029 /* 4030 * Patch in the VMMCALL instruction: 4031 */ 4032 hypercall[0] = 0x0f; 4033 hypercall[1] = 0x01; 4034 hypercall[2] = 0xd9; 4035 } 4036 4037 static int __init svm_check_processor_compat(void) 4038 { 4039 return 0; 4040 } 4041 4042 static bool svm_cpu_has_accelerated_tpr(void) 4043 { 4044 return false; 4045 } 4046 4047 /* 4048 * The kvm parameter can be NULL (module initialization, or invocation before 4049 * VM creation). Be sure to check the kvm parameter before using it. 4050 */ 4051 static bool svm_has_emulated_msr(struct kvm *kvm, u32 index) 4052 { 4053 switch (index) { 4054 case MSR_IA32_MCG_EXT_CTL: 4055 case MSR_IA32_VMX_BASIC ... MSR_IA32_VMX_VMFUNC: 4056 return false; 4057 case MSR_IA32_SMBASE: 4058 /* SEV-ES guests do not support SMM, so report false */ 4059 if (kvm && sev_es_guest(kvm)) 4060 return false; 4061 break; 4062 default: 4063 break; 4064 } 4065 4066 return true; 4067 } 4068 4069 static u64 svm_get_mt_mask(struct kvm_vcpu *vcpu, gfn_t gfn, bool is_mmio) 4070 { 4071 return 0; 4072 } 4073 4074 static void svm_vcpu_after_set_cpuid(struct kvm_vcpu *vcpu) 4075 { 4076 struct vcpu_svm *svm = to_svm(vcpu); 4077 struct kvm_cpuid_entry2 *best; 4078 4079 vcpu->arch.xsaves_enabled = guest_cpuid_has(vcpu, X86_FEATURE_XSAVE) && 4080 boot_cpu_has(X86_FEATURE_XSAVE) && 4081 boot_cpu_has(X86_FEATURE_XSAVES); 4082 4083 /* Update nrips enabled cache */ 4084 svm->nrips_enabled = kvm_cpu_cap_has(X86_FEATURE_NRIPS) && 4085 guest_cpuid_has(vcpu, X86_FEATURE_NRIPS); 4086 4087 svm->tsc_scaling_enabled = tsc_scaling && guest_cpuid_has(vcpu, X86_FEATURE_TSCRATEMSR); 4088 4089 svm_recalc_instruction_intercepts(vcpu, svm); 4090 4091 /* For sev guests, the memory encryption bit is not reserved in CR3. */ 4092 if (sev_guest(vcpu->kvm)) { 4093 best = kvm_find_cpuid_entry(vcpu, 0x8000001F, 0); 4094 if (best) 4095 vcpu->arch.reserved_gpa_bits &= ~(1UL << (best->ebx & 0x3f)); 4096 } 4097 4098 if (kvm_vcpu_apicv_active(vcpu)) { 4099 /* 4100 * AVIC does not work with an x2APIC mode guest. If the X2APIC feature 4101 * is exposed to the guest, disable AVIC. 4102 */ 4103 if (guest_cpuid_has(vcpu, X86_FEATURE_X2APIC)) 4104 kvm_request_apicv_update(vcpu->kvm, false, 4105 APICV_INHIBIT_REASON_X2APIC); 4106 4107 /* 4108 * Currently, AVIC does not work with nested virtualization. 4109 * So, we disable AVIC when cpuid for SVM is set in the L1 guest. 4110 */ 4111 if (nested && guest_cpuid_has(vcpu, X86_FEATURE_SVM)) 4112 kvm_request_apicv_update(vcpu->kvm, false, 4113 APICV_INHIBIT_REASON_NESTED); 4114 } 4115 init_vmcb_after_set_cpuid(vcpu); 4116 } 4117 4118 static bool svm_has_wbinvd_exit(void) 4119 { 4120 return true; 4121 } 4122 4123 #define PRE_EX(exit) { .exit_code = (exit), \ 4124 .stage = X86_ICPT_PRE_EXCEPT, } 4125 #define POST_EX(exit) { .exit_code = (exit), \ 4126 .stage = X86_ICPT_POST_EXCEPT, } 4127 #define POST_MEM(exit) { .exit_code = (exit), \ 4128 .stage = X86_ICPT_POST_MEMACCESS, } 4129 4130 static const struct __x86_intercept { 4131 u32 exit_code; 4132 enum x86_intercept_stage stage; 4133 } x86_intercept_map[] = { 4134 [x86_intercept_cr_read] = POST_EX(SVM_EXIT_READ_CR0), 4135 [x86_intercept_cr_write] = POST_EX(SVM_EXIT_WRITE_CR0), 4136 [x86_intercept_clts] = POST_EX(SVM_EXIT_WRITE_CR0), 4137 [x86_intercept_lmsw] = POST_EX(SVM_EXIT_WRITE_CR0), 4138 [x86_intercept_smsw] = POST_EX(SVM_EXIT_READ_CR0), 4139 [x86_intercept_dr_read] = POST_EX(SVM_EXIT_READ_DR0), 4140 [x86_intercept_dr_write] = POST_EX(SVM_EXIT_WRITE_DR0), 4141 [x86_intercept_sldt] = POST_EX(SVM_EXIT_LDTR_READ), 4142 [x86_intercept_str] = POST_EX(SVM_EXIT_TR_READ), 4143 [x86_intercept_lldt] = POST_EX(SVM_EXIT_LDTR_WRITE), 4144 [x86_intercept_ltr] = POST_EX(SVM_EXIT_TR_WRITE), 4145 [x86_intercept_sgdt] = POST_EX(SVM_EXIT_GDTR_READ), 4146 [x86_intercept_sidt] = POST_EX(SVM_EXIT_IDTR_READ), 4147 [x86_intercept_lgdt] = POST_EX(SVM_EXIT_GDTR_WRITE), 4148 [x86_intercept_lidt] = POST_EX(SVM_EXIT_IDTR_WRITE), 4149 [x86_intercept_vmrun] = POST_EX(SVM_EXIT_VMRUN), 4150 [x86_intercept_vmmcall] = POST_EX(SVM_EXIT_VMMCALL), 4151 [x86_intercept_vmload] = POST_EX(SVM_EXIT_VMLOAD), 4152 [x86_intercept_vmsave] = POST_EX(SVM_EXIT_VMSAVE), 4153 [x86_intercept_stgi] = POST_EX(SVM_EXIT_STGI), 4154 [x86_intercept_clgi] = POST_EX(SVM_EXIT_CLGI), 4155 [x86_intercept_skinit] = POST_EX(SVM_EXIT_SKINIT), 4156 [x86_intercept_invlpga] = POST_EX(SVM_EXIT_INVLPGA), 4157 [x86_intercept_rdtscp] = POST_EX(SVM_EXIT_RDTSCP), 4158 [x86_intercept_monitor] = POST_MEM(SVM_EXIT_MONITOR), 4159 [x86_intercept_mwait] = POST_EX(SVM_EXIT_MWAIT), 4160 [x86_intercept_invlpg] = POST_EX(SVM_EXIT_INVLPG), 4161 [x86_intercept_invd] = POST_EX(SVM_EXIT_INVD), 4162 [x86_intercept_wbinvd] = POST_EX(SVM_EXIT_WBINVD), 4163 [x86_intercept_wrmsr] = POST_EX(SVM_EXIT_MSR), 4164 [x86_intercept_rdtsc] = POST_EX(SVM_EXIT_RDTSC), 4165 [x86_intercept_rdmsr] = POST_EX(SVM_EXIT_MSR), 4166 [x86_intercept_rdpmc] = POST_EX(SVM_EXIT_RDPMC), 4167 [x86_intercept_cpuid] = PRE_EX(SVM_EXIT_CPUID), 4168 [x86_intercept_rsm] = PRE_EX(SVM_EXIT_RSM), 4169 [x86_intercept_pause] = PRE_EX(SVM_EXIT_PAUSE), 4170 [x86_intercept_pushf] = PRE_EX(SVM_EXIT_PUSHF), 4171 [x86_intercept_popf] = PRE_EX(SVM_EXIT_POPF), 4172 [x86_intercept_intn] = PRE_EX(SVM_EXIT_SWINT), 4173 [x86_intercept_iret] = PRE_EX(SVM_EXIT_IRET), 4174 [x86_intercept_icebp] = PRE_EX(SVM_EXIT_ICEBP), 4175 [x86_intercept_hlt] = POST_EX(SVM_EXIT_HLT), 4176 [x86_intercept_in] = POST_EX(SVM_EXIT_IOIO), 4177 [x86_intercept_ins] = POST_EX(SVM_EXIT_IOIO), 4178 [x86_intercept_out] = POST_EX(SVM_EXIT_IOIO), 4179 [x86_intercept_outs] = POST_EX(SVM_EXIT_IOIO), 4180 [x86_intercept_xsetbv] = PRE_EX(SVM_EXIT_XSETBV), 4181 }; 4182 4183 #undef PRE_EX 4184 #undef POST_EX 4185 #undef POST_MEM 4186 4187 static int svm_check_intercept(struct kvm_vcpu *vcpu, 4188 struct x86_instruction_info *info, 4189 enum x86_intercept_stage stage, 4190 struct x86_exception *exception) 4191 { 4192 struct vcpu_svm *svm = to_svm(vcpu); 4193 int vmexit, ret = X86EMUL_CONTINUE; 4194 struct __x86_intercept icpt_info; 4195 struct vmcb *vmcb = svm->vmcb; 4196 4197 if (info->intercept >= ARRAY_SIZE(x86_intercept_map)) 4198 goto out; 4199 4200 icpt_info = x86_intercept_map[info->intercept]; 4201 4202 if (stage != icpt_info.stage) 4203 goto out; 4204 4205 switch (icpt_info.exit_code) { 4206 case SVM_EXIT_READ_CR0: 4207 if (info->intercept == x86_intercept_cr_read) 4208 icpt_info.exit_code += info->modrm_reg; 4209 break; 4210 case SVM_EXIT_WRITE_CR0: { 4211 unsigned long cr0, val; 4212 4213 if (info->intercept == x86_intercept_cr_write) 4214 icpt_info.exit_code += info->modrm_reg; 4215 4216 if (icpt_info.exit_code != SVM_EXIT_WRITE_CR0 || 4217 info->intercept == x86_intercept_clts) 4218 break; 4219 4220 if (!(vmcb_is_intercept(&svm->nested.ctl, 4221 INTERCEPT_SELECTIVE_CR0))) 4222 break; 4223 4224 cr0 = vcpu->arch.cr0 & ~SVM_CR0_SELECTIVE_MASK; 4225 val = info->src_val & ~SVM_CR0_SELECTIVE_MASK; 4226 4227 if (info->intercept == x86_intercept_lmsw) { 4228 cr0 &= 0xfUL; 4229 val &= 0xfUL; 4230 /* lmsw can't clear PE - catch this here */ 4231 if (cr0 & X86_CR0_PE) 4232 val |= X86_CR0_PE; 4233 } 4234 4235 if (cr0 ^ val) 4236 icpt_info.exit_code = SVM_EXIT_CR0_SEL_WRITE; 4237 4238 break; 4239 } 4240 case SVM_EXIT_READ_DR0: 4241 case SVM_EXIT_WRITE_DR0: 4242 icpt_info.exit_code += info->modrm_reg; 4243 break; 4244 case SVM_EXIT_MSR: 4245 if (info->intercept == x86_intercept_wrmsr) 4246 vmcb->control.exit_info_1 = 1; 4247 else 4248 vmcb->control.exit_info_1 = 0; 4249 break; 4250 case SVM_EXIT_PAUSE: 4251 /* 4252 * We get this for NOP only, but pause 4253 * is rep not, check this here 4254 */ 4255 if (info->rep_prefix != REPE_PREFIX) 4256 goto out; 4257 break; 4258 case SVM_EXIT_IOIO: { 4259 u64 exit_info; 4260 u32 bytes; 4261 4262 if (info->intercept == x86_intercept_in || 4263 info->intercept == x86_intercept_ins) { 4264 exit_info = ((info->src_val & 0xffff) << 16) | 4265 SVM_IOIO_TYPE_MASK; 4266 bytes = info->dst_bytes; 4267 } else { 4268 exit_info = (info->dst_val & 0xffff) << 16; 4269 bytes = info->src_bytes; 4270 } 4271 4272 if (info->intercept == x86_intercept_outs || 4273 info->intercept == x86_intercept_ins) 4274 exit_info |= SVM_IOIO_STR_MASK; 4275 4276 if (info->rep_prefix) 4277 exit_info |= SVM_IOIO_REP_MASK; 4278 4279 bytes = min(bytes, 4u); 4280 4281 exit_info |= bytes << SVM_IOIO_SIZE_SHIFT; 4282 4283 exit_info |= (u32)info->ad_bytes << (SVM_IOIO_ASIZE_SHIFT - 1); 4284 4285 vmcb->control.exit_info_1 = exit_info; 4286 vmcb->control.exit_info_2 = info->next_rip; 4287 4288 break; 4289 } 4290 default: 4291 break; 4292 } 4293 4294 /* TODO: Advertise NRIPS to guest hypervisor unconditionally */ 4295 if (static_cpu_has(X86_FEATURE_NRIPS)) 4296 vmcb->control.next_rip = info->next_rip; 4297 vmcb->control.exit_code = icpt_info.exit_code; 4298 vmexit = nested_svm_exit_handled(svm); 4299 4300 ret = (vmexit == NESTED_EXIT_DONE) ? X86EMUL_INTERCEPTED 4301 : X86EMUL_CONTINUE; 4302 4303 out: 4304 return ret; 4305 } 4306 4307 static void svm_handle_exit_irqoff(struct kvm_vcpu *vcpu) 4308 { 4309 } 4310 4311 static void svm_sched_in(struct kvm_vcpu *vcpu, int cpu) 4312 { 4313 if (!kvm_pause_in_guest(vcpu->kvm)) 4314 shrink_ple_window(vcpu); 4315 } 4316 4317 static void svm_setup_mce(struct kvm_vcpu *vcpu) 4318 { 4319 /* [63:9] are reserved. */ 4320 vcpu->arch.mcg_cap &= 0x1ff; 4321 } 4322 4323 bool svm_smi_blocked(struct kvm_vcpu *vcpu) 4324 { 4325 struct vcpu_svm *svm = to_svm(vcpu); 4326 4327 /* Per APM Vol.2 15.22.2 "Response to SMI" */ 4328 if (!gif_set(svm)) 4329 return true; 4330 4331 return is_smm(vcpu); 4332 } 4333 4334 static int svm_smi_allowed(struct kvm_vcpu *vcpu, bool for_injection) 4335 { 4336 struct vcpu_svm *svm = to_svm(vcpu); 4337 if (svm->nested.nested_run_pending) 4338 return -EBUSY; 4339 4340 /* An SMI must not be injected into L2 if it's supposed to VM-Exit. */ 4341 if (for_injection && is_guest_mode(vcpu) && nested_exit_on_smi(svm)) 4342 return -EBUSY; 4343 4344 return !svm_smi_blocked(vcpu); 4345 } 4346 4347 static int svm_enter_smm(struct kvm_vcpu *vcpu, char *smstate) 4348 { 4349 struct vcpu_svm *svm = to_svm(vcpu); 4350 struct kvm_host_map map_save; 4351 int ret; 4352 4353 if (!is_guest_mode(vcpu)) 4354 return 0; 4355 4356 /* FED8h - SVM Guest */ 4357 put_smstate(u64, smstate, 0x7ed8, 1); 4358 /* FEE0h - SVM Guest VMCB Physical Address */ 4359 put_smstate(u64, smstate, 0x7ee0, svm->nested.vmcb12_gpa); 4360 4361 svm->vmcb->save.rax = vcpu->arch.regs[VCPU_REGS_RAX]; 4362 svm->vmcb->save.rsp = vcpu->arch.regs[VCPU_REGS_RSP]; 4363 svm->vmcb->save.rip = vcpu->arch.regs[VCPU_REGS_RIP]; 4364 4365 ret = nested_svm_vmexit(svm); 4366 if (ret) 4367 return ret; 4368 4369 /* 4370 * KVM uses VMCB01 to store L1 host state while L2 runs but 4371 * VMCB01 is going to be used during SMM and thus the state will 4372 * be lost. Temporary save non-VMLOAD/VMSAVE state to the host save 4373 * area pointed to by MSR_VM_HSAVE_PA. APM guarantees that the 4374 * format of the area is identical to guest save area offsetted 4375 * by 0x400 (matches the offset of 'struct vmcb_save_area' 4376 * within 'struct vmcb'). Note: HSAVE area may also be used by 4377 * L1 hypervisor to save additional host context (e.g. KVM does 4378 * that, see svm_prepare_guest_switch()) which must be 4379 * preserved. 4380 */ 4381 if (kvm_vcpu_map(vcpu, gpa_to_gfn(svm->nested.hsave_msr), 4382 &map_save) == -EINVAL) 4383 return 1; 4384 4385 BUILD_BUG_ON(offsetof(struct vmcb, save) != 0x400); 4386 4387 svm_copy_vmrun_state(map_save.hva + 0x400, 4388 &svm->vmcb01.ptr->save); 4389 4390 kvm_vcpu_unmap(vcpu, &map_save, true); 4391 return 0; 4392 } 4393 4394 static int svm_leave_smm(struct kvm_vcpu *vcpu, const char *smstate) 4395 { 4396 struct vcpu_svm *svm = to_svm(vcpu); 4397 struct kvm_host_map map, map_save; 4398 u64 saved_efer, vmcb12_gpa; 4399 struct vmcb *vmcb12; 4400 int ret; 4401 4402 if (!guest_cpuid_has(vcpu, X86_FEATURE_LM)) 4403 return 0; 4404 4405 /* Non-zero if SMI arrived while vCPU was in guest mode. */ 4406 if (!GET_SMSTATE(u64, smstate, 0x7ed8)) 4407 return 0; 4408 4409 if (!guest_cpuid_has(vcpu, X86_FEATURE_SVM)) 4410 return 1; 4411 4412 saved_efer = GET_SMSTATE(u64, smstate, 0x7ed0); 4413 if (!(saved_efer & EFER_SVME)) 4414 return 1; 4415 4416 vmcb12_gpa = GET_SMSTATE(u64, smstate, 0x7ee0); 4417 if (kvm_vcpu_map(vcpu, gpa_to_gfn(vmcb12_gpa), &map) == -EINVAL) 4418 return 1; 4419 4420 ret = 1; 4421 if (kvm_vcpu_map(vcpu, gpa_to_gfn(svm->nested.hsave_msr), &map_save) == -EINVAL) 4422 goto unmap_map; 4423 4424 if (svm_allocate_nested(svm)) 4425 goto unmap_save; 4426 4427 /* 4428 * Restore L1 host state from L1 HSAVE area as VMCB01 was 4429 * used during SMM (see svm_enter_smm()) 4430 */ 4431 4432 svm_copy_vmrun_state(&svm->vmcb01.ptr->save, map_save.hva + 0x400); 4433 4434 /* 4435 * Enter the nested guest now 4436 */ 4437 4438 vmcb12 = map.hva; 4439 nested_load_control_from_vmcb12(svm, &vmcb12->control); 4440 ret = enter_svm_guest_mode(vcpu, vmcb12_gpa, vmcb12, false); 4441 4442 unmap_save: 4443 kvm_vcpu_unmap(vcpu, &map_save, true); 4444 unmap_map: 4445 kvm_vcpu_unmap(vcpu, &map, true); 4446 return ret; 4447 } 4448 4449 static void svm_enable_smi_window(struct kvm_vcpu *vcpu) 4450 { 4451 struct vcpu_svm *svm = to_svm(vcpu); 4452 4453 if (!gif_set(svm)) { 4454 if (vgif_enabled(svm)) 4455 svm_set_intercept(svm, INTERCEPT_STGI); 4456 /* STGI will cause a vm exit */ 4457 } else { 4458 /* We must be in SMM; RSM will cause a vmexit anyway. */ 4459 } 4460 } 4461 4462 static bool svm_can_emulate_instruction(struct kvm_vcpu *vcpu, void *insn, int insn_len) 4463 { 4464 bool smep, smap, is_user; 4465 unsigned long cr4; 4466 4467 /* 4468 * When the guest is an SEV-ES guest, emulation is not possible. 4469 */ 4470 if (sev_es_guest(vcpu->kvm)) 4471 return false; 4472 4473 /* 4474 * Detect and workaround Errata 1096 Fam_17h_00_0Fh. 4475 * 4476 * Errata: 4477 * When CPU raise #NPF on guest data access and vCPU CR4.SMAP=1, it is 4478 * possible that CPU microcode implementing DecodeAssist will fail 4479 * to read bytes of instruction which caused #NPF. In this case, 4480 * GuestIntrBytes field of the VMCB on a VMEXIT will incorrectly 4481 * return 0 instead of the correct guest instruction bytes. 4482 * 4483 * This happens because CPU microcode reading instruction bytes 4484 * uses a special opcode which attempts to read data using CPL=0 4485 * privileges. The microcode reads CS:RIP and if it hits a SMAP 4486 * fault, it gives up and returns no instruction bytes. 4487 * 4488 * Detection: 4489 * We reach here in case CPU supports DecodeAssist, raised #NPF and 4490 * returned 0 in GuestIntrBytes field of the VMCB. 4491 * First, errata can only be triggered in case vCPU CR4.SMAP=1. 4492 * Second, if vCPU CR4.SMEP=1, errata could only be triggered 4493 * in case vCPU CPL==3 (Because otherwise guest would have triggered 4494 * a SMEP fault instead of #NPF). 4495 * Otherwise, vCPU CR4.SMEP=0, errata could be triggered by any vCPU CPL. 4496 * As most guests enable SMAP if they have also enabled SMEP, use above 4497 * logic in order to attempt minimize false-positive of detecting errata 4498 * while still preserving all cases semantic correctness. 4499 * 4500 * Workaround: 4501 * To determine what instruction the guest was executing, the hypervisor 4502 * will have to decode the instruction at the instruction pointer. 4503 * 4504 * In non SEV guest, hypervisor will be able to read the guest 4505 * memory to decode the instruction pointer when insn_len is zero 4506 * so we return true to indicate that decoding is possible. 4507 * 4508 * But in the SEV guest, the guest memory is encrypted with the 4509 * guest specific key and hypervisor will not be able to decode the 4510 * instruction pointer so we will not able to workaround it. Lets 4511 * print the error and request to kill the guest. 4512 */ 4513 if (likely(!insn || insn_len)) 4514 return true; 4515 4516 /* 4517 * If RIP is invalid, go ahead with emulation which will cause an 4518 * internal error exit. 4519 */ 4520 if (!kvm_vcpu_gfn_to_memslot(vcpu, kvm_rip_read(vcpu) >> PAGE_SHIFT)) 4521 return true; 4522 4523 cr4 = kvm_read_cr4(vcpu); 4524 smep = cr4 & X86_CR4_SMEP; 4525 smap = cr4 & X86_CR4_SMAP; 4526 is_user = svm_get_cpl(vcpu) == 3; 4527 if (smap && (!smep || is_user)) { 4528 if (!sev_guest(vcpu->kvm)) 4529 return true; 4530 4531 pr_err_ratelimited("KVM: SEV Guest triggered AMD Erratum 1096\n"); 4532 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu); 4533 } 4534 4535 return false; 4536 } 4537 4538 static bool svm_apic_init_signal_blocked(struct kvm_vcpu *vcpu) 4539 { 4540 struct vcpu_svm *svm = to_svm(vcpu); 4541 4542 /* 4543 * TODO: Last condition latch INIT signals on vCPU when 4544 * vCPU is in guest-mode and vmcb12 defines intercept on INIT. 4545 * To properly emulate the INIT intercept, 4546 * svm_check_nested_events() should call nested_svm_vmexit() 4547 * if an INIT signal is pending. 4548 */ 4549 return !gif_set(svm) || 4550 (vmcb_is_intercept(&svm->vmcb->control, INTERCEPT_INIT)); 4551 } 4552 4553 static void svm_vcpu_deliver_sipi_vector(struct kvm_vcpu *vcpu, u8 vector) 4554 { 4555 if (!sev_es_guest(vcpu->kvm)) 4556 return kvm_vcpu_deliver_sipi_vector(vcpu, vector); 4557 4558 sev_vcpu_deliver_sipi_vector(vcpu, vector); 4559 } 4560 4561 static void svm_vm_destroy(struct kvm *kvm) 4562 { 4563 avic_vm_destroy(kvm); 4564 sev_vm_destroy(kvm); 4565 } 4566 4567 static int svm_vm_init(struct kvm *kvm) 4568 { 4569 if (!pause_filter_count || !pause_filter_thresh) 4570 kvm->arch.pause_in_guest = true; 4571 4572 if (enable_apicv) { 4573 int ret = avic_vm_init(kvm); 4574 if (ret) 4575 return ret; 4576 } 4577 4578 return 0; 4579 } 4580 4581 static struct kvm_x86_ops svm_x86_ops __initdata = { 4582 .name = "kvm_amd", 4583 4584 .hardware_unsetup = svm_hardware_teardown, 4585 .hardware_enable = svm_hardware_enable, 4586 .hardware_disable = svm_hardware_disable, 4587 .cpu_has_accelerated_tpr = svm_cpu_has_accelerated_tpr, 4588 .has_emulated_msr = svm_has_emulated_msr, 4589 4590 .vcpu_create = svm_create_vcpu, 4591 .vcpu_free = svm_free_vcpu, 4592 .vcpu_reset = svm_vcpu_reset, 4593 4594 .vm_size = sizeof(struct kvm_svm), 4595 .vm_init = svm_vm_init, 4596 .vm_destroy = svm_vm_destroy, 4597 4598 .prepare_guest_switch = svm_prepare_guest_switch, 4599 .vcpu_load = svm_vcpu_load, 4600 .vcpu_put = svm_vcpu_put, 4601 .vcpu_blocking = svm_vcpu_blocking, 4602 .vcpu_unblocking = svm_vcpu_unblocking, 4603 4604 .update_exception_bitmap = svm_update_exception_bitmap, 4605 .get_msr_feature = svm_get_msr_feature, 4606 .get_msr = svm_get_msr, 4607 .set_msr = svm_set_msr, 4608 .get_segment_base = svm_get_segment_base, 4609 .get_segment = svm_get_segment, 4610 .set_segment = svm_set_segment, 4611 .get_cpl = svm_get_cpl, 4612 .get_cs_db_l_bits = kvm_get_cs_db_l_bits, 4613 .set_cr0 = svm_set_cr0, 4614 .is_valid_cr4 = svm_is_valid_cr4, 4615 .set_cr4 = svm_set_cr4, 4616 .set_efer = svm_set_efer, 4617 .get_idt = svm_get_idt, 4618 .set_idt = svm_set_idt, 4619 .get_gdt = svm_get_gdt, 4620 .set_gdt = svm_set_gdt, 4621 .set_dr7 = svm_set_dr7, 4622 .sync_dirty_debug_regs = svm_sync_dirty_debug_regs, 4623 .cache_reg = svm_cache_reg, 4624 .get_rflags = svm_get_rflags, 4625 .set_rflags = svm_set_rflags, 4626 .get_if_flag = svm_get_if_flag, 4627 4628 .tlb_flush_all = svm_flush_tlb, 4629 .tlb_flush_current = svm_flush_tlb, 4630 .tlb_flush_gva = svm_flush_tlb_gva, 4631 .tlb_flush_guest = svm_flush_tlb, 4632 4633 .run = svm_vcpu_run, 4634 .handle_exit = handle_exit, 4635 .skip_emulated_instruction = skip_emulated_instruction, 4636 .update_emulated_instruction = NULL, 4637 .set_interrupt_shadow = svm_set_interrupt_shadow, 4638 .get_interrupt_shadow = svm_get_interrupt_shadow, 4639 .patch_hypercall = svm_patch_hypercall, 4640 .set_irq = svm_set_irq, 4641 .set_nmi = svm_inject_nmi, 4642 .queue_exception = svm_queue_exception, 4643 .cancel_injection = svm_cancel_injection, 4644 .interrupt_allowed = svm_interrupt_allowed, 4645 .nmi_allowed = svm_nmi_allowed, 4646 .get_nmi_mask = svm_get_nmi_mask, 4647 .set_nmi_mask = svm_set_nmi_mask, 4648 .enable_nmi_window = svm_enable_nmi_window, 4649 .enable_irq_window = svm_enable_irq_window, 4650 .update_cr8_intercept = svm_update_cr8_intercept, 4651 .set_virtual_apic_mode = svm_set_virtual_apic_mode, 4652 .refresh_apicv_exec_ctrl = svm_refresh_apicv_exec_ctrl, 4653 .check_apicv_inhibit_reasons = svm_check_apicv_inhibit_reasons, 4654 .load_eoi_exitmap = svm_load_eoi_exitmap, 4655 .hwapic_irr_update = svm_hwapic_irr_update, 4656 .hwapic_isr_update = svm_hwapic_isr_update, 4657 .apicv_post_state_restore = avic_post_state_restore, 4658 4659 .set_tss_addr = svm_set_tss_addr, 4660 .set_identity_map_addr = svm_set_identity_map_addr, 4661 .get_mt_mask = svm_get_mt_mask, 4662 4663 .get_exit_info = svm_get_exit_info, 4664 4665 .vcpu_after_set_cpuid = svm_vcpu_after_set_cpuid, 4666 4667 .has_wbinvd_exit = svm_has_wbinvd_exit, 4668 4669 .get_l2_tsc_offset = svm_get_l2_tsc_offset, 4670 .get_l2_tsc_multiplier = svm_get_l2_tsc_multiplier, 4671 .write_tsc_offset = svm_write_tsc_offset, 4672 .write_tsc_multiplier = svm_write_tsc_multiplier, 4673 4674 .load_mmu_pgd = svm_load_mmu_pgd, 4675 4676 .check_intercept = svm_check_intercept, 4677 .handle_exit_irqoff = svm_handle_exit_irqoff, 4678 4679 .request_immediate_exit = __kvm_request_immediate_exit, 4680 4681 .sched_in = svm_sched_in, 4682 4683 .pmu_ops = &amd_pmu_ops, 4684 .nested_ops = &svm_nested_ops, 4685 4686 .deliver_posted_interrupt = svm_deliver_avic_intr, 4687 .dy_apicv_has_pending_interrupt = svm_dy_apicv_has_pending_interrupt, 4688 .update_pi_irte = svm_update_pi_irte, 4689 .setup_mce = svm_setup_mce, 4690 4691 .smi_allowed = svm_smi_allowed, 4692 .enter_smm = svm_enter_smm, 4693 .leave_smm = svm_leave_smm, 4694 .enable_smi_window = svm_enable_smi_window, 4695 4696 .mem_enc_op = svm_mem_enc_op, 4697 .mem_enc_reg_region = svm_register_enc_region, 4698 .mem_enc_unreg_region = svm_unregister_enc_region, 4699 4700 .vm_copy_enc_context_from = svm_vm_copy_asid_from, 4701 .vm_move_enc_context_from = svm_vm_migrate_from, 4702 4703 .can_emulate_instruction = svm_can_emulate_instruction, 4704 4705 .apic_init_signal_blocked = svm_apic_init_signal_blocked, 4706 4707 .msr_filter_changed = svm_msr_filter_changed, 4708 .complete_emulated_msr = svm_complete_emulated_msr, 4709 4710 .vcpu_deliver_sipi_vector = svm_vcpu_deliver_sipi_vector, 4711 }; 4712 4713 static struct kvm_x86_init_ops svm_init_ops __initdata = { 4714 .cpu_has_kvm_support = has_svm, 4715 .disabled_by_bios = is_disabled, 4716 .hardware_setup = svm_hardware_setup, 4717 .check_processor_compatibility = svm_check_processor_compat, 4718 4719 .runtime_ops = &svm_x86_ops, 4720 }; 4721 4722 static int __init svm_init(void) 4723 { 4724 __unused_size_checks(); 4725 4726 return kvm_init(&svm_init_ops, sizeof(struct vcpu_svm), 4727 __alignof__(struct vcpu_svm), THIS_MODULE); 4728 } 4729 4730 static void __exit svm_exit(void) 4731 { 4732 kvm_exit(); 4733 } 4734 4735 module_init(svm_init) 4736 module_exit(svm_exit) 4737