1 #define pr_fmt(fmt) KBUILD_MODNAME ": " 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 "smm.h" 10 #include "cpuid.h" 11 #include "pmu.h" 12 13 #include <linux/module.h> 14 #include <linux/mod_devicetable.h> 15 #include <linux/kernel.h> 16 #include <linux/vmalloc.h> 17 #include <linux/highmem.h> 18 #include <linux/amd-iommu.h> 19 #include <linux/sched.h> 20 #include <linux/trace_events.h> 21 #include <linux/slab.h> 22 #include <linux/hashtable.h> 23 #include <linux/objtool.h> 24 #include <linux/psp-sev.h> 25 #include <linux/file.h> 26 #include <linux/pagemap.h> 27 #include <linux/swap.h> 28 #include <linux/rwsem.h> 29 #include <linux/cc_platform.h> 30 #include <linux/smp.h> 31 #include <linux/string_choices.h> 32 #include <linux/mutex.h> 33 34 #include <asm/apic.h> 35 #include <asm/msr.h> 36 #include <asm/perf_event.h> 37 #include <asm/tlbflush.h> 38 #include <asm/desc.h> 39 #include <asm/debugreg.h> 40 #include <asm/kvm_para.h> 41 #include <asm/irq_remapping.h> 42 #include <asm/spec-ctrl.h> 43 #include <asm/cpu_device_id.h> 44 #include <asm/traps.h> 45 #include <asm/reboot.h> 46 #include <asm/fpu/api.h> 47 48 #include <trace/events/ipi.h> 49 50 #include "trace.h" 51 52 #include "svm.h" 53 #include "svm_ops.h" 54 55 #include "kvm_onhyperv.h" 56 #include "svm_onhyperv.h" 57 58 MODULE_AUTHOR("Qumranet"); 59 MODULE_DESCRIPTION("KVM support for SVM (AMD-V) extensions"); 60 MODULE_LICENSE("GPL"); 61 62 #ifdef MODULE 63 static const struct x86_cpu_id svm_cpu_id[] = { 64 X86_MATCH_FEATURE(X86_FEATURE_SVM, NULL), 65 {} 66 }; 67 MODULE_DEVICE_TABLE(x86cpu, svm_cpu_id); 68 #endif 69 70 #define SEG_TYPE_LDT 2 71 #define SEG_TYPE_BUSY_TSS16 3 72 73 static bool erratum_383_found __read_mostly; 74 75 /* 76 * Set osvw_len to higher value when updated Revision Guides 77 * are published and we know what the new status bits are 78 */ 79 static uint64_t osvw_len = 4, osvw_status; 80 81 static DEFINE_PER_CPU(u64, current_tsc_ratio); 82 83 /* 84 * These 2 parameters are used to config the controls for Pause-Loop Exiting: 85 * pause_filter_count: On processors that support Pause filtering(indicated 86 * by CPUID Fn8000_000A_EDX), the VMCB provides a 16 bit pause filter 87 * count value. On VMRUN this value is loaded into an internal counter. 88 * Each time a pause instruction is executed, this counter is decremented 89 * until it reaches zero at which time a #VMEXIT is generated if pause 90 * intercept is enabled. Refer to AMD APM Vol 2 Section 15.14.4 Pause 91 * Intercept Filtering for more details. 92 * This also indicate if ple logic enabled. 93 * 94 * pause_filter_thresh: In addition, some processor families support advanced 95 * pause filtering (indicated by CPUID Fn8000_000A_EDX) upper bound on 96 * the amount of time a guest is allowed to execute in a pause loop. 97 * In this mode, a 16-bit pause filter threshold field is added in the 98 * VMCB. The threshold value is a cycle count that is used to reset the 99 * pause counter. As with simple pause filtering, VMRUN loads the pause 100 * count value from VMCB into an internal counter. Then, on each pause 101 * instruction the hardware checks the elapsed number of cycles since 102 * the most recent pause instruction against the pause filter threshold. 103 * If the elapsed cycle count is greater than the pause filter threshold, 104 * then the internal pause count is reloaded from the VMCB and execution 105 * continues. If the elapsed cycle count is less than the pause filter 106 * threshold, then the internal pause count is decremented. If the count 107 * value is less than zero and PAUSE intercept is enabled, a #VMEXIT is 108 * triggered. If advanced pause filtering is supported and pause filter 109 * threshold field is set to zero, the filter will operate in the simpler, 110 * count only mode. 111 */ 112 113 static unsigned short pause_filter_thresh = KVM_DEFAULT_PLE_GAP; 114 module_param(pause_filter_thresh, ushort, 0444); 115 116 static unsigned short pause_filter_count = KVM_SVM_DEFAULT_PLE_WINDOW; 117 module_param(pause_filter_count, ushort, 0444); 118 119 /* Default doubles per-vcpu window every exit. */ 120 static unsigned short pause_filter_count_grow = KVM_DEFAULT_PLE_WINDOW_GROW; 121 module_param(pause_filter_count_grow, ushort, 0444); 122 123 /* Default resets per-vcpu window every exit to pause_filter_count. */ 124 static unsigned short pause_filter_count_shrink = KVM_DEFAULT_PLE_WINDOW_SHRINK; 125 module_param(pause_filter_count_shrink, ushort, 0444); 126 127 /* Default is to compute the maximum so we can never overflow. */ 128 static unsigned short pause_filter_count_max = KVM_SVM_DEFAULT_PLE_WINDOW_MAX; 129 module_param(pause_filter_count_max, ushort, 0444); 130 131 /* 132 * Use nested page tables by default. Note, NPT may get forced off by 133 * svm_hardware_setup() if it's unsupported by hardware or the host kernel. 134 */ 135 bool npt_enabled = true; 136 module_param_named(npt, npt_enabled, bool, 0444); 137 138 /* allow nested virtualization in KVM/SVM */ 139 static int nested = true; 140 module_param(nested, int, 0444); 141 142 /* enable/disable Next RIP Save */ 143 int nrips = true; 144 module_param(nrips, int, 0444); 145 146 /* enable/disable Virtual VMLOAD VMSAVE */ 147 static int vls = true; 148 module_param(vls, int, 0444); 149 150 /* enable/disable Virtual GIF */ 151 int vgif = true; 152 module_param(vgif, int, 0444); 153 154 /* enable/disable LBR virtualization */ 155 int lbrv = true; 156 module_param(lbrv, int, 0444); 157 158 static int tsc_scaling = true; 159 module_param(tsc_scaling, int, 0444); 160 161 module_param(enable_device_posted_irqs, bool, 0444); 162 163 bool __read_mostly dump_invalid_vmcb; 164 module_param(dump_invalid_vmcb, bool, 0644); 165 166 167 bool intercept_smi = true; 168 module_param(intercept_smi, bool, 0444); 169 170 bool vnmi = true; 171 module_param(vnmi, bool, 0444); 172 173 static bool svm_gp_erratum_intercept = true; 174 175 static u8 rsm_ins_bytes[] = "\x0f\xaa"; 176 177 static unsigned long iopm_base; 178 179 DEFINE_PER_CPU(struct svm_cpu_data, svm_data); 180 181 static DEFINE_MUTEX(vmcb_dump_mutex); 182 183 /* 184 * Only MSR_TSC_AUX is switched via the user return hook. EFER is switched via 185 * the VMCB, and the SYSCALL/SYSENTER MSRs are handled by VMLOAD/VMSAVE. 186 * 187 * RDTSCP and RDPID are not used in the kernel, specifically to allow KVM to 188 * defer the restoration of TSC_AUX until the CPU returns to userspace. 189 */ 190 int tsc_aux_uret_slot __ro_after_init = -1; 191 192 static int get_npt_level(void) 193 { 194 #ifdef CONFIG_X86_64 195 return pgtable_l5_enabled() ? PT64_ROOT_5LEVEL : PT64_ROOT_4LEVEL; 196 #else 197 return PT32E_ROOT_LEVEL; 198 #endif 199 } 200 201 int svm_set_efer(struct kvm_vcpu *vcpu, u64 efer) 202 { 203 struct vcpu_svm *svm = to_svm(vcpu); 204 u64 old_efer = vcpu->arch.efer; 205 vcpu->arch.efer = efer; 206 207 if (!npt_enabled) { 208 /* Shadow paging assumes NX to be available. */ 209 efer |= EFER_NX; 210 211 if (!(efer & EFER_LMA)) 212 efer &= ~EFER_LME; 213 } 214 215 if ((old_efer & EFER_SVME) != (efer & EFER_SVME)) { 216 if (!(efer & EFER_SVME)) { 217 svm_leave_nested(vcpu); 218 svm_set_gif(svm, true); 219 /* #GP intercept is still needed for vmware backdoor */ 220 if (!enable_vmware_backdoor) 221 clr_exception_intercept(svm, GP_VECTOR); 222 223 /* 224 * Free the nested guest state, unless we are in SMM. 225 * In this case we will return to the nested guest 226 * as soon as we leave SMM. 227 */ 228 if (!is_smm(vcpu)) 229 svm_free_nested(svm); 230 231 } else { 232 int ret = svm_allocate_nested(svm); 233 234 if (ret) { 235 vcpu->arch.efer = old_efer; 236 return ret; 237 } 238 239 /* 240 * Never intercept #GP for SEV guests, KVM can't 241 * decrypt guest memory to workaround the erratum. 242 */ 243 if (svm_gp_erratum_intercept && !sev_guest(vcpu->kvm)) 244 set_exception_intercept(svm, GP_VECTOR); 245 } 246 } 247 248 svm->vmcb->save.efer = efer | EFER_SVME; 249 vmcb_mark_dirty(svm->vmcb, VMCB_CR); 250 return 0; 251 } 252 253 static u32 svm_get_interrupt_shadow(struct kvm_vcpu *vcpu) 254 { 255 struct vcpu_svm *svm = to_svm(vcpu); 256 u32 ret = 0; 257 258 if (svm->vmcb->control.int_state & SVM_INTERRUPT_SHADOW_MASK) 259 ret = KVM_X86_SHADOW_INT_STI | KVM_X86_SHADOW_INT_MOV_SS; 260 return ret; 261 } 262 263 static void svm_set_interrupt_shadow(struct kvm_vcpu *vcpu, int mask) 264 { 265 struct vcpu_svm *svm = to_svm(vcpu); 266 267 if (mask == 0) 268 svm->vmcb->control.int_state &= ~SVM_INTERRUPT_SHADOW_MASK; 269 else 270 svm->vmcb->control.int_state |= SVM_INTERRUPT_SHADOW_MASK; 271 272 } 273 274 static int __svm_skip_emulated_instruction(struct kvm_vcpu *vcpu, 275 bool commit_side_effects) 276 { 277 struct vcpu_svm *svm = to_svm(vcpu); 278 unsigned long old_rflags; 279 280 /* 281 * SEV-ES does not expose the next RIP. The RIP update is controlled by 282 * the type of exit and the #VC handler in the guest. 283 */ 284 if (sev_es_guest(vcpu->kvm)) 285 goto done; 286 287 if (nrips && svm->vmcb->control.next_rip != 0) { 288 WARN_ON_ONCE(!static_cpu_has(X86_FEATURE_NRIPS)); 289 svm->next_rip = svm->vmcb->control.next_rip; 290 } 291 292 if (!svm->next_rip) { 293 if (unlikely(!commit_side_effects)) 294 old_rflags = svm->vmcb->save.rflags; 295 296 if (!kvm_emulate_instruction(vcpu, EMULTYPE_SKIP)) 297 return 0; 298 299 if (unlikely(!commit_side_effects)) 300 svm->vmcb->save.rflags = old_rflags; 301 } else { 302 kvm_rip_write(vcpu, svm->next_rip); 303 } 304 305 done: 306 if (likely(commit_side_effects)) 307 svm_set_interrupt_shadow(vcpu, 0); 308 309 return 1; 310 } 311 312 static int svm_skip_emulated_instruction(struct kvm_vcpu *vcpu) 313 { 314 return __svm_skip_emulated_instruction(vcpu, true); 315 } 316 317 static int svm_update_soft_interrupt_rip(struct kvm_vcpu *vcpu) 318 { 319 unsigned long rip, old_rip = kvm_rip_read(vcpu); 320 struct vcpu_svm *svm = to_svm(vcpu); 321 322 /* 323 * Due to architectural shortcomings, the CPU doesn't always provide 324 * NextRIP, e.g. if KVM intercepted an exception that occurred while 325 * the CPU was vectoring an INTO/INT3 in the guest. Temporarily skip 326 * the instruction even if NextRIP is supported to acquire the next 327 * RIP so that it can be shoved into the NextRIP field, otherwise 328 * hardware will fail to advance guest RIP during event injection. 329 * Drop the exception/interrupt if emulation fails and effectively 330 * retry the instruction, it's the least awful option. If NRIPS is 331 * in use, the skip must not commit any side effects such as clearing 332 * the interrupt shadow or RFLAGS.RF. 333 */ 334 if (!__svm_skip_emulated_instruction(vcpu, !nrips)) 335 return -EIO; 336 337 rip = kvm_rip_read(vcpu); 338 339 /* 340 * Save the injection information, even when using next_rip, as the 341 * VMCB's next_rip will be lost (cleared on VM-Exit) if the injection 342 * doesn't complete due to a VM-Exit occurring while the CPU is 343 * vectoring the event. Decoding the instruction isn't guaranteed to 344 * work as there may be no backing instruction, e.g. if the event is 345 * being injected by L1 for L2, or if the guest is patching INT3 into 346 * a different instruction. 347 */ 348 svm->soft_int_injected = true; 349 svm->soft_int_csbase = svm->vmcb->save.cs.base; 350 svm->soft_int_old_rip = old_rip; 351 svm->soft_int_next_rip = rip; 352 353 if (nrips) 354 kvm_rip_write(vcpu, old_rip); 355 356 if (static_cpu_has(X86_FEATURE_NRIPS)) 357 svm->vmcb->control.next_rip = rip; 358 359 return 0; 360 } 361 362 static void svm_inject_exception(struct kvm_vcpu *vcpu) 363 { 364 struct kvm_queued_exception *ex = &vcpu->arch.exception; 365 struct vcpu_svm *svm = to_svm(vcpu); 366 367 kvm_deliver_exception_payload(vcpu, ex); 368 369 if (kvm_exception_is_soft(ex->vector) && 370 svm_update_soft_interrupt_rip(vcpu)) 371 return; 372 373 svm->vmcb->control.event_inj = ex->vector 374 | SVM_EVTINJ_VALID 375 | (ex->has_error_code ? SVM_EVTINJ_VALID_ERR : 0) 376 | SVM_EVTINJ_TYPE_EXEPT; 377 svm->vmcb->control.event_inj_err = ex->error_code; 378 } 379 380 static void svm_init_erratum_383(void) 381 { 382 u64 val; 383 384 if (!static_cpu_has_bug(X86_BUG_AMD_TLB_MMATCH)) 385 return; 386 387 /* Use _safe variants to not break nested virtualization */ 388 if (native_read_msr_safe(MSR_AMD64_DC_CFG, &val)) 389 return; 390 391 val |= (1ULL << 47); 392 393 native_write_msr_safe(MSR_AMD64_DC_CFG, val); 394 395 erratum_383_found = true; 396 } 397 398 static void svm_init_osvw(struct kvm_vcpu *vcpu) 399 { 400 /* 401 * Guests should see errata 400 and 415 as fixed (assuming that 402 * HLT and IO instructions are intercepted). 403 */ 404 vcpu->arch.osvw.length = (osvw_len >= 3) ? (osvw_len) : 3; 405 vcpu->arch.osvw.status = osvw_status & ~(6ULL); 406 407 /* 408 * By increasing VCPU's osvw.length to 3 we are telling the guest that 409 * all osvw.status bits inside that length, including bit 0 (which is 410 * reserved for erratum 298), are valid. However, if host processor's 411 * osvw_len is 0 then osvw_status[0] carries no information. We need to 412 * be conservative here and therefore we tell the guest that erratum 298 413 * is present (because we really don't know). 414 */ 415 if (osvw_len == 0 && boot_cpu_data.x86 == 0x10) 416 vcpu->arch.osvw.status |= 1; 417 } 418 419 static bool __kvm_is_svm_supported(void) 420 { 421 int cpu = smp_processor_id(); 422 struct cpuinfo_x86 *c = &cpu_data(cpu); 423 424 if (c->x86_vendor != X86_VENDOR_AMD && 425 c->x86_vendor != X86_VENDOR_HYGON) { 426 pr_err("CPU %d isn't AMD or Hygon\n", cpu); 427 return false; 428 } 429 430 if (!cpu_has(c, X86_FEATURE_SVM)) { 431 pr_err("SVM not supported by CPU %d\n", cpu); 432 return false; 433 } 434 435 if (cc_platform_has(CC_ATTR_GUEST_MEM_ENCRYPT)) { 436 pr_info("KVM is unsupported when running as an SEV guest\n"); 437 return false; 438 } 439 440 return true; 441 } 442 443 static bool kvm_is_svm_supported(void) 444 { 445 bool supported; 446 447 migrate_disable(); 448 supported = __kvm_is_svm_supported(); 449 migrate_enable(); 450 451 return supported; 452 } 453 454 static int svm_check_processor_compat(void) 455 { 456 if (!__kvm_is_svm_supported()) 457 return -EIO; 458 459 return 0; 460 } 461 462 static void __svm_write_tsc_multiplier(u64 multiplier) 463 { 464 if (multiplier == __this_cpu_read(current_tsc_ratio)) 465 return; 466 467 wrmsrq(MSR_AMD64_TSC_RATIO, multiplier); 468 __this_cpu_write(current_tsc_ratio, multiplier); 469 } 470 471 static __always_inline struct sev_es_save_area *sev_es_host_save_area(struct svm_cpu_data *sd) 472 { 473 return &sd->save_area->host_sev_es_save; 474 } 475 476 static inline void kvm_cpu_svm_disable(void) 477 { 478 uint64_t efer; 479 480 wrmsrq(MSR_VM_HSAVE_PA, 0); 481 rdmsrq(MSR_EFER, efer); 482 if (efer & EFER_SVME) { 483 /* 484 * Force GIF=1 prior to disabling SVM, e.g. to ensure INIT and 485 * NMI aren't blocked. 486 */ 487 stgi(); 488 wrmsrq(MSR_EFER, efer & ~EFER_SVME); 489 } 490 } 491 492 static void svm_emergency_disable_virtualization_cpu(void) 493 { 494 kvm_rebooting = true; 495 496 kvm_cpu_svm_disable(); 497 } 498 499 static void svm_disable_virtualization_cpu(void) 500 { 501 /* Make sure we clean up behind us */ 502 if (tsc_scaling) 503 __svm_write_tsc_multiplier(SVM_TSC_RATIO_DEFAULT); 504 505 kvm_cpu_svm_disable(); 506 507 amd_pmu_disable_virt(); 508 } 509 510 static int svm_enable_virtualization_cpu(void) 511 { 512 513 struct svm_cpu_data *sd; 514 uint64_t efer; 515 int me = raw_smp_processor_id(); 516 517 rdmsrq(MSR_EFER, efer); 518 if (efer & EFER_SVME) 519 return -EBUSY; 520 521 sd = per_cpu_ptr(&svm_data, me); 522 sd->asid_generation = 1; 523 sd->max_asid = cpuid_ebx(SVM_CPUID_FUNC) - 1; 524 sd->next_asid = sd->max_asid + 1; 525 sd->min_asid = max_sev_asid + 1; 526 527 wrmsrq(MSR_EFER, efer | EFER_SVME); 528 529 wrmsrq(MSR_VM_HSAVE_PA, sd->save_area_pa); 530 531 if (static_cpu_has(X86_FEATURE_TSCRATEMSR)) { 532 /* 533 * Set the default value, even if we don't use TSC scaling 534 * to avoid having stale value in the msr 535 */ 536 __svm_write_tsc_multiplier(SVM_TSC_RATIO_DEFAULT); 537 } 538 539 540 /* 541 * Get OSVW bits. 542 * 543 * Note that it is possible to have a system with mixed processor 544 * revisions and therefore different OSVW bits. If bits are not the same 545 * on different processors then choose the worst case (i.e. if erratum 546 * is present on one processor and not on another then assume that the 547 * erratum is present everywhere). 548 */ 549 if (cpu_has(&boot_cpu_data, X86_FEATURE_OSVW)) { 550 u64 len, status = 0; 551 int err; 552 553 err = native_read_msr_safe(MSR_AMD64_OSVW_ID_LENGTH, &len); 554 if (!err) 555 err = native_read_msr_safe(MSR_AMD64_OSVW_STATUS, &status); 556 557 if (err) 558 osvw_status = osvw_len = 0; 559 else { 560 if (len < osvw_len) 561 osvw_len = len; 562 osvw_status |= status; 563 osvw_status &= (1ULL << osvw_len) - 1; 564 } 565 } else 566 osvw_status = osvw_len = 0; 567 568 svm_init_erratum_383(); 569 570 amd_pmu_enable_virt(); 571 572 return 0; 573 } 574 575 static void svm_cpu_uninit(int cpu) 576 { 577 struct svm_cpu_data *sd = per_cpu_ptr(&svm_data, cpu); 578 579 if (!sd->save_area) 580 return; 581 582 kfree(sd->sev_vmcbs); 583 __free_page(__sme_pa_to_page(sd->save_area_pa)); 584 sd->save_area_pa = 0; 585 sd->save_area = NULL; 586 } 587 588 static int svm_cpu_init(int cpu) 589 { 590 struct svm_cpu_data *sd = per_cpu_ptr(&svm_data, cpu); 591 struct page *save_area_page; 592 int ret = -ENOMEM; 593 594 memset(sd, 0, sizeof(struct svm_cpu_data)); 595 save_area_page = snp_safe_alloc_page_node(cpu_to_node(cpu), GFP_KERNEL); 596 if (!save_area_page) 597 return ret; 598 599 ret = sev_cpu_init(sd); 600 if (ret) 601 goto free_save_area; 602 603 sd->save_area = page_address(save_area_page); 604 sd->save_area_pa = __sme_page_pa(save_area_page); 605 return 0; 606 607 free_save_area: 608 __free_page(save_area_page); 609 return ret; 610 611 } 612 613 static void set_dr_intercepts(struct vcpu_svm *svm) 614 { 615 struct vmcb *vmcb = svm->vmcb01.ptr; 616 617 vmcb_set_intercept(&vmcb->control, INTERCEPT_DR0_READ); 618 vmcb_set_intercept(&vmcb->control, INTERCEPT_DR1_READ); 619 vmcb_set_intercept(&vmcb->control, INTERCEPT_DR2_READ); 620 vmcb_set_intercept(&vmcb->control, INTERCEPT_DR3_READ); 621 vmcb_set_intercept(&vmcb->control, INTERCEPT_DR4_READ); 622 vmcb_set_intercept(&vmcb->control, INTERCEPT_DR5_READ); 623 vmcb_set_intercept(&vmcb->control, INTERCEPT_DR6_READ); 624 vmcb_set_intercept(&vmcb->control, INTERCEPT_DR0_WRITE); 625 vmcb_set_intercept(&vmcb->control, INTERCEPT_DR1_WRITE); 626 vmcb_set_intercept(&vmcb->control, INTERCEPT_DR2_WRITE); 627 vmcb_set_intercept(&vmcb->control, INTERCEPT_DR3_WRITE); 628 vmcb_set_intercept(&vmcb->control, INTERCEPT_DR4_WRITE); 629 vmcb_set_intercept(&vmcb->control, INTERCEPT_DR5_WRITE); 630 vmcb_set_intercept(&vmcb->control, INTERCEPT_DR6_WRITE); 631 vmcb_set_intercept(&vmcb->control, INTERCEPT_DR7_READ); 632 vmcb_set_intercept(&vmcb->control, INTERCEPT_DR7_WRITE); 633 634 recalc_intercepts(svm); 635 } 636 637 static void clr_dr_intercepts(struct vcpu_svm *svm) 638 { 639 struct vmcb *vmcb = svm->vmcb01.ptr; 640 641 vmcb->control.intercepts[INTERCEPT_DR] = 0; 642 643 recalc_intercepts(svm); 644 } 645 646 static bool msr_write_intercepted(struct kvm_vcpu *vcpu, u32 msr) 647 { 648 /* 649 * For non-nested case: 650 * If the L01 MSR bitmap does not intercept the MSR, then we need to 651 * save it. 652 * 653 * For nested case: 654 * If the L02 MSR bitmap does not intercept the MSR, then we need to 655 * save it. 656 */ 657 void *msrpm = is_guest_mode(vcpu) ? to_svm(vcpu)->nested.msrpm : 658 to_svm(vcpu)->msrpm; 659 660 return svm_test_msr_bitmap_write(msrpm, msr); 661 } 662 663 void svm_set_intercept_for_msr(struct kvm_vcpu *vcpu, u32 msr, int type, bool set) 664 { 665 struct vcpu_svm *svm = to_svm(vcpu); 666 void *msrpm = svm->msrpm; 667 668 /* Don't disable interception for MSRs userspace wants to handle. */ 669 if (type & MSR_TYPE_R) { 670 if (!set && kvm_msr_allowed(vcpu, msr, KVM_MSR_FILTER_READ)) 671 svm_clear_msr_bitmap_read(msrpm, msr); 672 else 673 svm_set_msr_bitmap_read(msrpm, msr); 674 } 675 676 if (type & MSR_TYPE_W) { 677 if (!set && kvm_msr_allowed(vcpu, msr, KVM_MSR_FILTER_WRITE)) 678 svm_clear_msr_bitmap_write(msrpm, msr); 679 else 680 svm_set_msr_bitmap_write(msrpm, msr); 681 } 682 683 svm_hv_vmcb_dirty_nested_enlightenments(vcpu); 684 svm->nested.force_msr_bitmap_recalc = true; 685 } 686 687 void *svm_alloc_permissions_map(unsigned long size, gfp_t gfp_mask) 688 { 689 unsigned int order = get_order(size); 690 struct page *pages = alloc_pages(gfp_mask, order); 691 void *pm; 692 693 if (!pages) 694 return NULL; 695 696 /* 697 * Set all bits in the permissions map so that all MSR and I/O accesses 698 * are intercepted by default. 699 */ 700 pm = page_address(pages); 701 memset(pm, 0xff, PAGE_SIZE * (1 << order)); 702 703 return pm; 704 } 705 706 static void svm_recalc_lbr_msr_intercepts(struct kvm_vcpu *vcpu) 707 { 708 struct vcpu_svm *svm = to_svm(vcpu); 709 bool intercept = !(svm->vmcb->control.virt_ext & LBR_CTL_ENABLE_MASK); 710 711 if (intercept == svm->lbr_msrs_intercepted) 712 return; 713 714 svm_set_intercept_for_msr(vcpu, MSR_IA32_LASTBRANCHFROMIP, MSR_TYPE_RW, intercept); 715 svm_set_intercept_for_msr(vcpu, MSR_IA32_LASTBRANCHTOIP, MSR_TYPE_RW, intercept); 716 svm_set_intercept_for_msr(vcpu, MSR_IA32_LASTINTFROMIP, MSR_TYPE_RW, intercept); 717 svm_set_intercept_for_msr(vcpu, MSR_IA32_LASTINTTOIP, MSR_TYPE_RW, intercept); 718 719 if (sev_es_guest(vcpu->kvm)) 720 svm_set_intercept_for_msr(vcpu, MSR_IA32_DEBUGCTLMSR, MSR_TYPE_RW, intercept); 721 722 svm->lbr_msrs_intercepted = intercept; 723 } 724 725 void svm_vcpu_free_msrpm(void *msrpm) 726 { 727 __free_pages(virt_to_page(msrpm), get_order(MSRPM_SIZE)); 728 } 729 730 static void svm_recalc_msr_intercepts(struct kvm_vcpu *vcpu) 731 { 732 struct vcpu_svm *svm = to_svm(vcpu); 733 734 svm_disable_intercept_for_msr(vcpu, MSR_STAR, MSR_TYPE_RW); 735 svm_disable_intercept_for_msr(vcpu, MSR_IA32_SYSENTER_CS, MSR_TYPE_RW); 736 737 #ifdef CONFIG_X86_64 738 svm_disable_intercept_for_msr(vcpu, MSR_GS_BASE, MSR_TYPE_RW); 739 svm_disable_intercept_for_msr(vcpu, MSR_FS_BASE, MSR_TYPE_RW); 740 svm_disable_intercept_for_msr(vcpu, MSR_KERNEL_GS_BASE, MSR_TYPE_RW); 741 svm_disable_intercept_for_msr(vcpu, MSR_LSTAR, MSR_TYPE_RW); 742 svm_disable_intercept_for_msr(vcpu, MSR_CSTAR, MSR_TYPE_RW); 743 svm_disable_intercept_for_msr(vcpu, MSR_SYSCALL_MASK, MSR_TYPE_RW); 744 #endif 745 746 if (lbrv) 747 svm_recalc_lbr_msr_intercepts(vcpu); 748 749 if (cpu_feature_enabled(X86_FEATURE_IBPB)) 750 svm_set_intercept_for_msr(vcpu, MSR_IA32_PRED_CMD, MSR_TYPE_W, 751 !guest_has_pred_cmd_msr(vcpu)); 752 753 if (cpu_feature_enabled(X86_FEATURE_FLUSH_L1D)) 754 svm_set_intercept_for_msr(vcpu, MSR_IA32_FLUSH_CMD, MSR_TYPE_W, 755 !guest_cpu_cap_has(vcpu, X86_FEATURE_FLUSH_L1D)); 756 757 /* 758 * Disable interception of SPEC_CTRL if KVM doesn't need to manually 759 * context switch the MSR (SPEC_CTRL is virtualized by the CPU), or if 760 * the guest has a non-zero SPEC_CTRL value, i.e. is likely actively 761 * using SPEC_CTRL. 762 */ 763 if (cpu_feature_enabled(X86_FEATURE_V_SPEC_CTRL)) 764 svm_set_intercept_for_msr(vcpu, MSR_IA32_SPEC_CTRL, MSR_TYPE_RW, 765 !guest_has_spec_ctrl_msr(vcpu)); 766 else 767 svm_set_intercept_for_msr(vcpu, MSR_IA32_SPEC_CTRL, MSR_TYPE_RW, 768 !svm->spec_ctrl); 769 770 /* 771 * Intercept SYSENTER_EIP and SYSENTER_ESP when emulating an Intel CPU, 772 * as AMD hardware only store 32 bits, whereas Intel CPUs track 64 bits. 773 */ 774 svm_set_intercept_for_msr(vcpu, MSR_IA32_SYSENTER_EIP, MSR_TYPE_RW, 775 guest_cpuid_is_intel_compatible(vcpu)); 776 svm_set_intercept_for_msr(vcpu, MSR_IA32_SYSENTER_ESP, MSR_TYPE_RW, 777 guest_cpuid_is_intel_compatible(vcpu)); 778 779 if (kvm_aperfmperf_in_guest(vcpu->kvm)) { 780 svm_disable_intercept_for_msr(vcpu, MSR_IA32_APERF, MSR_TYPE_R); 781 svm_disable_intercept_for_msr(vcpu, MSR_IA32_MPERF, MSR_TYPE_R); 782 } 783 784 if (kvm_cpu_cap_has(X86_FEATURE_SHSTK)) { 785 bool shstk_enabled = guest_cpu_cap_has(vcpu, X86_FEATURE_SHSTK); 786 787 svm_set_intercept_for_msr(vcpu, MSR_IA32_U_CET, MSR_TYPE_RW, !shstk_enabled); 788 svm_set_intercept_for_msr(vcpu, MSR_IA32_S_CET, MSR_TYPE_RW, !shstk_enabled); 789 svm_set_intercept_for_msr(vcpu, MSR_IA32_PL0_SSP, MSR_TYPE_RW, !shstk_enabled); 790 svm_set_intercept_for_msr(vcpu, MSR_IA32_PL1_SSP, MSR_TYPE_RW, !shstk_enabled); 791 svm_set_intercept_for_msr(vcpu, MSR_IA32_PL2_SSP, MSR_TYPE_RW, !shstk_enabled); 792 svm_set_intercept_for_msr(vcpu, MSR_IA32_PL3_SSP, MSR_TYPE_RW, !shstk_enabled); 793 } 794 795 if (sev_es_guest(vcpu->kvm)) 796 sev_es_recalc_msr_intercepts(vcpu); 797 798 /* 799 * x2APIC intercepts are modified on-demand and cannot be filtered by 800 * userspace. 801 */ 802 } 803 804 void svm_copy_lbrs(struct vmcb *to_vmcb, struct vmcb *from_vmcb) 805 { 806 to_vmcb->save.dbgctl = from_vmcb->save.dbgctl; 807 to_vmcb->save.br_from = from_vmcb->save.br_from; 808 to_vmcb->save.br_to = from_vmcb->save.br_to; 809 to_vmcb->save.last_excp_from = from_vmcb->save.last_excp_from; 810 to_vmcb->save.last_excp_to = from_vmcb->save.last_excp_to; 811 812 vmcb_mark_dirty(to_vmcb, VMCB_LBR); 813 } 814 815 static void __svm_enable_lbrv(struct kvm_vcpu *vcpu) 816 { 817 to_svm(vcpu)->vmcb->control.virt_ext |= LBR_CTL_ENABLE_MASK; 818 } 819 820 void svm_enable_lbrv(struct kvm_vcpu *vcpu) 821 { 822 __svm_enable_lbrv(vcpu); 823 svm_recalc_lbr_msr_intercepts(vcpu); 824 } 825 826 static void __svm_disable_lbrv(struct kvm_vcpu *vcpu) 827 { 828 KVM_BUG_ON(sev_es_guest(vcpu->kvm), vcpu->kvm); 829 to_svm(vcpu)->vmcb->control.virt_ext &= ~LBR_CTL_ENABLE_MASK; 830 } 831 832 void svm_update_lbrv(struct kvm_vcpu *vcpu) 833 { 834 struct vcpu_svm *svm = to_svm(vcpu); 835 bool current_enable_lbrv = svm->vmcb->control.virt_ext & LBR_CTL_ENABLE_MASK; 836 bool enable_lbrv = (svm->vmcb->save.dbgctl & DEBUGCTLMSR_LBR) || 837 (is_guest_mode(vcpu) && guest_cpu_cap_has(vcpu, X86_FEATURE_LBRV) && 838 (svm->nested.ctl.virt_ext & LBR_CTL_ENABLE_MASK)); 839 840 if (enable_lbrv && !current_enable_lbrv) 841 __svm_enable_lbrv(vcpu); 842 else if (!enable_lbrv && current_enable_lbrv) 843 __svm_disable_lbrv(vcpu); 844 845 /* 846 * During nested transitions, it is possible that the current VMCB has 847 * LBR_CTL set, but the previous LBR_CTL had it cleared (or vice versa). 848 * In this case, even though LBR_CTL does not need an update, intercepts 849 * do, so always recalculate the intercepts here. 850 */ 851 svm_recalc_lbr_msr_intercepts(vcpu); 852 } 853 854 void disable_nmi_singlestep(struct vcpu_svm *svm) 855 { 856 svm->nmi_singlestep = false; 857 858 if (!(svm->vcpu.guest_debug & KVM_GUESTDBG_SINGLESTEP)) { 859 /* Clear our flags if they were not set by the guest */ 860 if (!(svm->nmi_singlestep_guest_rflags & X86_EFLAGS_TF)) 861 svm->vmcb->save.rflags &= ~X86_EFLAGS_TF; 862 if (!(svm->nmi_singlestep_guest_rflags & X86_EFLAGS_RF)) 863 svm->vmcb->save.rflags &= ~X86_EFLAGS_RF; 864 } 865 } 866 867 static void grow_ple_window(struct kvm_vcpu *vcpu) 868 { 869 struct vcpu_svm *svm = to_svm(vcpu); 870 struct vmcb_control_area *control = &svm->vmcb->control; 871 int old = control->pause_filter_count; 872 873 if (kvm_pause_in_guest(vcpu->kvm)) 874 return; 875 876 control->pause_filter_count = __grow_ple_window(old, 877 pause_filter_count, 878 pause_filter_count_grow, 879 pause_filter_count_max); 880 881 if (control->pause_filter_count != old) { 882 vmcb_mark_dirty(svm->vmcb, VMCB_INTERCEPTS); 883 trace_kvm_ple_window_update(vcpu->vcpu_id, 884 control->pause_filter_count, old); 885 } 886 } 887 888 static void shrink_ple_window(struct kvm_vcpu *vcpu) 889 { 890 struct vcpu_svm *svm = to_svm(vcpu); 891 struct vmcb_control_area *control = &svm->vmcb->control; 892 int old = control->pause_filter_count; 893 894 if (kvm_pause_in_guest(vcpu->kvm)) 895 return; 896 897 control->pause_filter_count = 898 __shrink_ple_window(old, 899 pause_filter_count, 900 pause_filter_count_shrink, 901 pause_filter_count); 902 if (control->pause_filter_count != old) { 903 vmcb_mark_dirty(svm->vmcb, VMCB_INTERCEPTS); 904 trace_kvm_ple_window_update(vcpu->vcpu_id, 905 control->pause_filter_count, old); 906 } 907 } 908 909 static void svm_hardware_unsetup(void) 910 { 911 int cpu; 912 913 avic_hardware_unsetup(); 914 915 sev_hardware_unsetup(); 916 917 for_each_possible_cpu(cpu) 918 svm_cpu_uninit(cpu); 919 920 __free_pages(__sme_pa_to_page(iopm_base), get_order(IOPM_SIZE)); 921 iopm_base = 0; 922 } 923 924 static void init_seg(struct vmcb_seg *seg) 925 { 926 seg->selector = 0; 927 seg->attrib = SVM_SELECTOR_P_MASK | SVM_SELECTOR_S_MASK | 928 SVM_SELECTOR_WRITE_MASK; /* Read/Write Data Segment */ 929 seg->limit = 0xffff; 930 seg->base = 0; 931 } 932 933 static void init_sys_seg(struct vmcb_seg *seg, uint32_t type) 934 { 935 seg->selector = 0; 936 seg->attrib = SVM_SELECTOR_P_MASK | type; 937 seg->limit = 0xffff; 938 seg->base = 0; 939 } 940 941 static u64 svm_get_l2_tsc_offset(struct kvm_vcpu *vcpu) 942 { 943 struct vcpu_svm *svm = to_svm(vcpu); 944 945 return svm->nested.ctl.tsc_offset; 946 } 947 948 static u64 svm_get_l2_tsc_multiplier(struct kvm_vcpu *vcpu) 949 { 950 struct vcpu_svm *svm = to_svm(vcpu); 951 952 return svm->tsc_ratio_msr; 953 } 954 955 static void svm_write_tsc_offset(struct kvm_vcpu *vcpu) 956 { 957 struct vcpu_svm *svm = to_svm(vcpu); 958 959 svm->vmcb01.ptr->control.tsc_offset = vcpu->arch.l1_tsc_offset; 960 svm->vmcb->control.tsc_offset = vcpu->arch.tsc_offset; 961 vmcb_mark_dirty(svm->vmcb, VMCB_INTERCEPTS); 962 } 963 964 void svm_write_tsc_multiplier(struct kvm_vcpu *vcpu) 965 { 966 preempt_disable(); 967 if (to_svm(vcpu)->guest_state_loaded) 968 __svm_write_tsc_multiplier(vcpu->arch.tsc_scaling_ratio); 969 preempt_enable(); 970 } 971 972 /* Evaluate instruction intercepts that depend on guest CPUID features. */ 973 static void svm_recalc_instruction_intercepts(struct kvm_vcpu *vcpu) 974 { 975 struct vcpu_svm *svm = to_svm(vcpu); 976 977 /* 978 * Intercept INVPCID if shadow paging is enabled to sync/free shadow 979 * roots, or if INVPCID is disabled in the guest to inject #UD. 980 */ 981 if (kvm_cpu_cap_has(X86_FEATURE_INVPCID)) { 982 if (!npt_enabled || 983 !guest_cpu_cap_has(&svm->vcpu, X86_FEATURE_INVPCID)) 984 svm_set_intercept(svm, INTERCEPT_INVPCID); 985 else 986 svm_clr_intercept(svm, INTERCEPT_INVPCID); 987 } 988 989 if (kvm_cpu_cap_has(X86_FEATURE_RDTSCP)) { 990 if (guest_cpu_cap_has(vcpu, X86_FEATURE_RDTSCP)) 991 svm_clr_intercept(svm, INTERCEPT_RDTSCP); 992 else 993 svm_set_intercept(svm, INTERCEPT_RDTSCP); 994 } 995 996 if (guest_cpuid_is_intel_compatible(vcpu)) { 997 svm_set_intercept(svm, INTERCEPT_VMLOAD); 998 svm_set_intercept(svm, INTERCEPT_VMSAVE); 999 svm->vmcb->control.virt_ext &= ~VIRTUAL_VMLOAD_VMSAVE_ENABLE_MASK; 1000 } else { 1001 /* 1002 * If hardware supports Virtual VMLOAD VMSAVE then enable it 1003 * in VMCB and clear intercepts to avoid #VMEXIT. 1004 */ 1005 if (vls) { 1006 svm_clr_intercept(svm, INTERCEPT_VMLOAD); 1007 svm_clr_intercept(svm, INTERCEPT_VMSAVE); 1008 svm->vmcb->control.virt_ext |= VIRTUAL_VMLOAD_VMSAVE_ENABLE_MASK; 1009 } 1010 } 1011 } 1012 1013 static void svm_recalc_intercepts(struct kvm_vcpu *vcpu) 1014 { 1015 svm_recalc_instruction_intercepts(vcpu); 1016 svm_recalc_msr_intercepts(vcpu); 1017 } 1018 1019 static void init_vmcb(struct kvm_vcpu *vcpu, bool init_event) 1020 { 1021 struct vcpu_svm *svm = to_svm(vcpu); 1022 struct vmcb *vmcb = svm->vmcb01.ptr; 1023 struct vmcb_control_area *control = &vmcb->control; 1024 struct vmcb_save_area *save = &vmcb->save; 1025 1026 svm_set_intercept(svm, INTERCEPT_CR0_READ); 1027 svm_set_intercept(svm, INTERCEPT_CR3_READ); 1028 svm_set_intercept(svm, INTERCEPT_CR4_READ); 1029 svm_set_intercept(svm, INTERCEPT_CR0_WRITE); 1030 svm_set_intercept(svm, INTERCEPT_CR3_WRITE); 1031 svm_set_intercept(svm, INTERCEPT_CR4_WRITE); 1032 if (!kvm_vcpu_apicv_active(vcpu)) 1033 svm_set_intercept(svm, INTERCEPT_CR8_WRITE); 1034 1035 set_dr_intercepts(svm); 1036 1037 set_exception_intercept(svm, PF_VECTOR); 1038 set_exception_intercept(svm, UD_VECTOR); 1039 set_exception_intercept(svm, MC_VECTOR); 1040 set_exception_intercept(svm, AC_VECTOR); 1041 set_exception_intercept(svm, DB_VECTOR); 1042 /* 1043 * Guest access to VMware backdoor ports could legitimately 1044 * trigger #GP because of TSS I/O permission bitmap. 1045 * We intercept those #GP and allow access to them anyway 1046 * as VMware does. 1047 */ 1048 if (enable_vmware_backdoor) 1049 set_exception_intercept(svm, GP_VECTOR); 1050 1051 svm_set_intercept(svm, INTERCEPT_INTR); 1052 svm_set_intercept(svm, INTERCEPT_NMI); 1053 1054 if (intercept_smi) 1055 svm_set_intercept(svm, INTERCEPT_SMI); 1056 1057 svm_set_intercept(svm, INTERCEPT_SELECTIVE_CR0); 1058 svm_set_intercept(svm, INTERCEPT_RDPMC); 1059 svm_set_intercept(svm, INTERCEPT_CPUID); 1060 svm_set_intercept(svm, INTERCEPT_INVD); 1061 svm_set_intercept(svm, INTERCEPT_INVLPG); 1062 svm_set_intercept(svm, INTERCEPT_INVLPGA); 1063 svm_set_intercept(svm, INTERCEPT_IOIO_PROT); 1064 svm_set_intercept(svm, INTERCEPT_MSR_PROT); 1065 svm_set_intercept(svm, INTERCEPT_TASK_SWITCH); 1066 svm_set_intercept(svm, INTERCEPT_SHUTDOWN); 1067 svm_set_intercept(svm, INTERCEPT_VMRUN); 1068 svm_set_intercept(svm, INTERCEPT_VMMCALL); 1069 svm_set_intercept(svm, INTERCEPT_VMLOAD); 1070 svm_set_intercept(svm, INTERCEPT_VMSAVE); 1071 svm_set_intercept(svm, INTERCEPT_STGI); 1072 svm_set_intercept(svm, INTERCEPT_CLGI); 1073 svm_set_intercept(svm, INTERCEPT_SKINIT); 1074 svm_set_intercept(svm, INTERCEPT_WBINVD); 1075 svm_set_intercept(svm, INTERCEPT_XSETBV); 1076 svm_set_intercept(svm, INTERCEPT_RDPRU); 1077 svm_set_intercept(svm, INTERCEPT_RSM); 1078 1079 if (!kvm_mwait_in_guest(vcpu->kvm)) { 1080 svm_set_intercept(svm, INTERCEPT_MONITOR); 1081 svm_set_intercept(svm, INTERCEPT_MWAIT); 1082 } 1083 1084 if (!kvm_hlt_in_guest(vcpu->kvm)) { 1085 if (cpu_feature_enabled(X86_FEATURE_IDLE_HLT)) 1086 svm_set_intercept(svm, INTERCEPT_IDLE_HLT); 1087 else 1088 svm_set_intercept(svm, INTERCEPT_HLT); 1089 } 1090 1091 control->iopm_base_pa = iopm_base; 1092 control->msrpm_base_pa = __sme_set(__pa(svm->msrpm)); 1093 control->int_ctl = V_INTR_MASKING_MASK; 1094 1095 init_seg(&save->es); 1096 init_seg(&save->ss); 1097 init_seg(&save->ds); 1098 init_seg(&save->fs); 1099 init_seg(&save->gs); 1100 1101 save->cs.selector = 0xf000; 1102 save->cs.base = 0xffff0000; 1103 /* Executable/Readable Code Segment */ 1104 save->cs.attrib = SVM_SELECTOR_READ_MASK | SVM_SELECTOR_P_MASK | 1105 SVM_SELECTOR_S_MASK | SVM_SELECTOR_CODE_MASK; 1106 save->cs.limit = 0xffff; 1107 1108 save->gdtr.base = 0; 1109 save->gdtr.limit = 0xffff; 1110 save->idtr.base = 0; 1111 save->idtr.limit = 0xffff; 1112 1113 init_sys_seg(&save->ldtr, SEG_TYPE_LDT); 1114 init_sys_seg(&save->tr, SEG_TYPE_BUSY_TSS16); 1115 1116 if (npt_enabled) { 1117 /* Setup VMCB for Nested Paging */ 1118 control->nested_ctl |= SVM_NESTED_CTL_NP_ENABLE; 1119 svm_clr_intercept(svm, INTERCEPT_INVLPG); 1120 clr_exception_intercept(svm, PF_VECTOR); 1121 svm_clr_intercept(svm, INTERCEPT_CR3_READ); 1122 svm_clr_intercept(svm, INTERCEPT_CR3_WRITE); 1123 save->g_pat = vcpu->arch.pat; 1124 save->cr3 = 0; 1125 } 1126 svm->current_vmcb->asid_generation = 0; 1127 svm->asid = 0; 1128 1129 svm->nested.vmcb12_gpa = INVALID_GPA; 1130 svm->nested.last_vmcb12_gpa = INVALID_GPA; 1131 1132 if (!kvm_pause_in_guest(vcpu->kvm)) { 1133 control->pause_filter_count = pause_filter_count; 1134 if (pause_filter_thresh) 1135 control->pause_filter_thresh = pause_filter_thresh; 1136 svm_set_intercept(svm, INTERCEPT_PAUSE); 1137 } else { 1138 svm_clr_intercept(svm, INTERCEPT_PAUSE); 1139 } 1140 1141 if (kvm_vcpu_apicv_active(vcpu)) 1142 avic_init_vmcb(svm, vmcb); 1143 1144 if (vnmi) 1145 svm->vmcb->control.int_ctl |= V_NMI_ENABLE_MASK; 1146 1147 if (vgif) { 1148 svm_clr_intercept(svm, INTERCEPT_STGI); 1149 svm_clr_intercept(svm, INTERCEPT_CLGI); 1150 svm->vmcb->control.int_ctl |= V_GIF_ENABLE_MASK; 1151 } 1152 1153 if (vcpu->kvm->arch.bus_lock_detection_enabled) 1154 svm_set_intercept(svm, INTERCEPT_BUSLOCK); 1155 1156 if (sev_guest(vcpu->kvm)) 1157 sev_init_vmcb(svm, init_event); 1158 1159 svm_hv_init_vmcb(vmcb); 1160 1161 kvm_make_request(KVM_REQ_RECALC_INTERCEPTS, vcpu); 1162 1163 vmcb_mark_all_dirty(vmcb); 1164 1165 enable_gif(svm); 1166 } 1167 1168 static void __svm_vcpu_reset(struct kvm_vcpu *vcpu) 1169 { 1170 struct vcpu_svm *svm = to_svm(vcpu); 1171 1172 svm_init_osvw(vcpu); 1173 1174 if (kvm_check_has_quirk(vcpu->kvm, KVM_X86_QUIRK_STUFF_FEATURE_MSRS)) 1175 vcpu->arch.microcode_version = 0x01000065; 1176 svm->tsc_ratio_msr = kvm_caps.default_tsc_scaling_ratio; 1177 1178 svm->nmi_masked = false; 1179 svm->awaiting_iret_completion = false; 1180 } 1181 1182 static void svm_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event) 1183 { 1184 struct vcpu_svm *svm = to_svm(vcpu); 1185 1186 svm->spec_ctrl = 0; 1187 svm->virt_spec_ctrl = 0; 1188 1189 init_vmcb(vcpu, init_event); 1190 1191 if (!init_event) 1192 __svm_vcpu_reset(vcpu); 1193 } 1194 1195 void svm_switch_vmcb(struct vcpu_svm *svm, struct kvm_vmcb_info *target_vmcb) 1196 { 1197 svm->current_vmcb = target_vmcb; 1198 svm->vmcb = target_vmcb->ptr; 1199 } 1200 1201 static int svm_vcpu_create(struct kvm_vcpu *vcpu) 1202 { 1203 struct vcpu_svm *svm; 1204 struct page *vmcb01_page; 1205 int err; 1206 1207 BUILD_BUG_ON(offsetof(struct vcpu_svm, vcpu) != 0); 1208 svm = to_svm(vcpu); 1209 1210 err = -ENOMEM; 1211 vmcb01_page = snp_safe_alloc_page(); 1212 if (!vmcb01_page) 1213 goto out; 1214 1215 err = sev_vcpu_create(vcpu); 1216 if (err) 1217 goto error_free_vmcb_page; 1218 1219 err = avic_init_vcpu(svm); 1220 if (err) 1221 goto error_free_sev; 1222 1223 svm->msrpm = svm_vcpu_alloc_msrpm(); 1224 if (!svm->msrpm) { 1225 err = -ENOMEM; 1226 goto error_free_sev; 1227 } 1228 1229 svm->x2avic_msrs_intercepted = true; 1230 svm->lbr_msrs_intercepted = true; 1231 1232 svm->vmcb01.ptr = page_address(vmcb01_page); 1233 svm->vmcb01.pa = __sme_set(page_to_pfn(vmcb01_page) << PAGE_SHIFT); 1234 svm_switch_vmcb(svm, &svm->vmcb01); 1235 1236 svm->guest_state_loaded = false; 1237 1238 return 0; 1239 1240 error_free_sev: 1241 sev_free_vcpu(vcpu); 1242 error_free_vmcb_page: 1243 __free_page(vmcb01_page); 1244 out: 1245 return err; 1246 } 1247 1248 static void svm_vcpu_free(struct kvm_vcpu *vcpu) 1249 { 1250 struct vcpu_svm *svm = to_svm(vcpu); 1251 1252 WARN_ON_ONCE(!list_empty(&svm->ir_list)); 1253 1254 svm_leave_nested(vcpu); 1255 svm_free_nested(svm); 1256 1257 sev_free_vcpu(vcpu); 1258 1259 __free_page(__sme_pa_to_page(svm->vmcb01.pa)); 1260 svm_vcpu_free_msrpm(svm->msrpm); 1261 } 1262 1263 #ifdef CONFIG_CPU_MITIGATIONS 1264 static DEFINE_SPINLOCK(srso_lock); 1265 static atomic_t srso_nr_vms; 1266 1267 static void svm_srso_clear_bp_spec_reduce(void *ign) 1268 { 1269 struct svm_cpu_data *sd = this_cpu_ptr(&svm_data); 1270 1271 if (!sd->bp_spec_reduce_set) 1272 return; 1273 1274 msr_clear_bit(MSR_ZEN4_BP_CFG, MSR_ZEN4_BP_CFG_BP_SPEC_REDUCE_BIT); 1275 sd->bp_spec_reduce_set = false; 1276 } 1277 1278 static void svm_srso_vm_destroy(void) 1279 { 1280 if (!cpu_feature_enabled(X86_FEATURE_SRSO_BP_SPEC_REDUCE)) 1281 return; 1282 1283 if (atomic_dec_return(&srso_nr_vms)) 1284 return; 1285 1286 guard(spinlock)(&srso_lock); 1287 1288 /* 1289 * Verify a new VM didn't come along, acquire the lock, and increment 1290 * the count before this task acquired the lock. 1291 */ 1292 if (atomic_read(&srso_nr_vms)) 1293 return; 1294 1295 on_each_cpu(svm_srso_clear_bp_spec_reduce, NULL, 1); 1296 } 1297 1298 static void svm_srso_vm_init(void) 1299 { 1300 if (!cpu_feature_enabled(X86_FEATURE_SRSO_BP_SPEC_REDUCE)) 1301 return; 1302 1303 /* 1304 * Acquire the lock on 0 => 1 transitions to ensure a potential 1 => 0 1305 * transition, i.e. destroying the last VM, is fully complete, e.g. so 1306 * that a delayed IPI doesn't clear BP_SPEC_REDUCE after a vCPU runs. 1307 */ 1308 if (atomic_inc_not_zero(&srso_nr_vms)) 1309 return; 1310 1311 guard(spinlock)(&srso_lock); 1312 1313 atomic_inc(&srso_nr_vms); 1314 } 1315 #else 1316 static void svm_srso_vm_init(void) { } 1317 static void svm_srso_vm_destroy(void) { } 1318 #endif 1319 1320 static void svm_prepare_switch_to_guest(struct kvm_vcpu *vcpu) 1321 { 1322 struct vcpu_svm *svm = to_svm(vcpu); 1323 struct svm_cpu_data *sd = per_cpu_ptr(&svm_data, vcpu->cpu); 1324 1325 if (sev_es_guest(vcpu->kvm)) 1326 sev_es_unmap_ghcb(svm); 1327 1328 if (svm->guest_state_loaded) 1329 return; 1330 1331 /* 1332 * Save additional host state that will be restored on VMEXIT (sev-es) 1333 * or subsequent vmload of host save area. 1334 */ 1335 vmsave(sd->save_area_pa); 1336 if (sev_es_guest(vcpu->kvm)) 1337 sev_es_prepare_switch_to_guest(svm, sev_es_host_save_area(sd)); 1338 1339 if (tsc_scaling) 1340 __svm_write_tsc_multiplier(vcpu->arch.tsc_scaling_ratio); 1341 1342 /* 1343 * TSC_AUX is always virtualized (context switched by hardware) for 1344 * SEV-ES guests when the feature is available. For non-SEV-ES guests, 1345 * context switch TSC_AUX via the user_return MSR infrastructure (not 1346 * all CPUs support TSC_AUX virtualization). 1347 */ 1348 if (likely(tsc_aux_uret_slot >= 0) && 1349 (!boot_cpu_has(X86_FEATURE_V_TSC_AUX) || !sev_es_guest(vcpu->kvm))) 1350 kvm_set_user_return_msr(tsc_aux_uret_slot, svm->tsc_aux, -1ull); 1351 1352 if (cpu_feature_enabled(X86_FEATURE_SRSO_BP_SPEC_REDUCE) && 1353 !sd->bp_spec_reduce_set) { 1354 sd->bp_spec_reduce_set = true; 1355 msr_set_bit(MSR_ZEN4_BP_CFG, MSR_ZEN4_BP_CFG_BP_SPEC_REDUCE_BIT); 1356 } 1357 svm->guest_state_loaded = true; 1358 } 1359 1360 static void svm_prepare_host_switch(struct kvm_vcpu *vcpu) 1361 { 1362 to_svm(vcpu)->guest_state_loaded = false; 1363 } 1364 1365 static void svm_vcpu_load(struct kvm_vcpu *vcpu, int cpu) 1366 { 1367 if (vcpu->scheduled_out && !kvm_pause_in_guest(vcpu->kvm)) 1368 shrink_ple_window(vcpu); 1369 1370 if (kvm_vcpu_apicv_active(vcpu)) 1371 avic_vcpu_load(vcpu, cpu); 1372 } 1373 1374 static void svm_vcpu_put(struct kvm_vcpu *vcpu) 1375 { 1376 if (kvm_vcpu_apicv_active(vcpu)) 1377 avic_vcpu_put(vcpu); 1378 1379 svm_prepare_host_switch(vcpu); 1380 1381 ++vcpu->stat.host_state_reload; 1382 } 1383 1384 static unsigned long svm_get_rflags(struct kvm_vcpu *vcpu) 1385 { 1386 struct vcpu_svm *svm = to_svm(vcpu); 1387 unsigned long rflags = svm->vmcb->save.rflags; 1388 1389 if (svm->nmi_singlestep) { 1390 /* Hide our flags if they were not set by the guest */ 1391 if (!(svm->nmi_singlestep_guest_rflags & X86_EFLAGS_TF)) 1392 rflags &= ~X86_EFLAGS_TF; 1393 if (!(svm->nmi_singlestep_guest_rflags & X86_EFLAGS_RF)) 1394 rflags &= ~X86_EFLAGS_RF; 1395 } 1396 return rflags; 1397 } 1398 1399 static void svm_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags) 1400 { 1401 if (to_svm(vcpu)->nmi_singlestep) 1402 rflags |= (X86_EFLAGS_TF | X86_EFLAGS_RF); 1403 1404 /* 1405 * Any change of EFLAGS.VM is accompanied by a reload of SS 1406 * (caused by either a task switch or an inter-privilege IRET), 1407 * so we do not need to update the CPL here. 1408 */ 1409 to_svm(vcpu)->vmcb->save.rflags = rflags; 1410 } 1411 1412 static bool svm_get_if_flag(struct kvm_vcpu *vcpu) 1413 { 1414 struct vmcb *vmcb = to_svm(vcpu)->vmcb; 1415 1416 return sev_es_guest(vcpu->kvm) 1417 ? vmcb->control.int_state & SVM_GUEST_INTERRUPT_MASK 1418 : kvm_get_rflags(vcpu) & X86_EFLAGS_IF; 1419 } 1420 1421 static void svm_cache_reg(struct kvm_vcpu *vcpu, enum kvm_reg reg) 1422 { 1423 kvm_register_mark_available(vcpu, reg); 1424 1425 switch (reg) { 1426 case VCPU_EXREG_PDPTR: 1427 /* 1428 * When !npt_enabled, mmu->pdptrs[] is already available since 1429 * it is always updated per SDM when moving to CRs. 1430 */ 1431 if (npt_enabled) 1432 load_pdptrs(vcpu, kvm_read_cr3(vcpu)); 1433 break; 1434 default: 1435 KVM_BUG_ON(1, vcpu->kvm); 1436 } 1437 } 1438 1439 static void svm_set_vintr(struct vcpu_svm *svm) 1440 { 1441 struct vmcb_control_area *control; 1442 1443 /* 1444 * The following fields are ignored when AVIC is enabled 1445 */ 1446 WARN_ON(kvm_vcpu_apicv_activated(&svm->vcpu)); 1447 1448 svm_set_intercept(svm, INTERCEPT_VINTR); 1449 1450 /* 1451 * Recalculating intercepts may have cleared the VINTR intercept. If 1452 * V_INTR_MASKING is enabled in vmcb12, then the effective RFLAGS.IF 1453 * for L1 physical interrupts is L1's RFLAGS.IF at the time of VMRUN. 1454 * Requesting an interrupt window if save.RFLAGS.IF=0 is pointless as 1455 * interrupts will never be unblocked while L2 is running. 1456 */ 1457 if (!svm_is_intercept(svm, INTERCEPT_VINTR)) 1458 return; 1459 1460 /* 1461 * This is just a dummy VINTR to actually cause a vmexit to happen. 1462 * Actual injection of virtual interrupts happens through EVENTINJ. 1463 */ 1464 control = &svm->vmcb->control; 1465 control->int_vector = 0x0; 1466 control->int_ctl &= ~V_INTR_PRIO_MASK; 1467 control->int_ctl |= V_IRQ_MASK | 1468 ((/*control->int_vector >> 4*/ 0xf) << V_INTR_PRIO_SHIFT); 1469 vmcb_mark_dirty(svm->vmcb, VMCB_INTR); 1470 } 1471 1472 static void svm_clear_vintr(struct vcpu_svm *svm) 1473 { 1474 svm_clr_intercept(svm, INTERCEPT_VINTR); 1475 1476 /* Drop int_ctl fields related to VINTR injection. */ 1477 svm->vmcb->control.int_ctl &= ~V_IRQ_INJECTION_BITS_MASK; 1478 if (is_guest_mode(&svm->vcpu)) { 1479 svm->vmcb01.ptr->control.int_ctl &= ~V_IRQ_INJECTION_BITS_MASK; 1480 1481 WARN_ON((svm->vmcb->control.int_ctl & V_TPR_MASK) != 1482 (svm->nested.ctl.int_ctl & V_TPR_MASK)); 1483 1484 svm->vmcb->control.int_ctl |= svm->nested.ctl.int_ctl & 1485 V_IRQ_INJECTION_BITS_MASK; 1486 1487 svm->vmcb->control.int_vector = svm->nested.ctl.int_vector; 1488 } 1489 1490 vmcb_mark_dirty(svm->vmcb, VMCB_INTR); 1491 } 1492 1493 static struct vmcb_seg *svm_seg(struct kvm_vcpu *vcpu, int seg) 1494 { 1495 struct vmcb_save_area *save = &to_svm(vcpu)->vmcb->save; 1496 struct vmcb_save_area *save01 = &to_svm(vcpu)->vmcb01.ptr->save; 1497 1498 switch (seg) { 1499 case VCPU_SREG_CS: return &save->cs; 1500 case VCPU_SREG_DS: return &save->ds; 1501 case VCPU_SREG_ES: return &save->es; 1502 case VCPU_SREG_FS: return &save01->fs; 1503 case VCPU_SREG_GS: return &save01->gs; 1504 case VCPU_SREG_SS: return &save->ss; 1505 case VCPU_SREG_TR: return &save01->tr; 1506 case VCPU_SREG_LDTR: return &save01->ldtr; 1507 } 1508 BUG(); 1509 return NULL; 1510 } 1511 1512 static u64 svm_get_segment_base(struct kvm_vcpu *vcpu, int seg) 1513 { 1514 struct vmcb_seg *s = svm_seg(vcpu, seg); 1515 1516 return s->base; 1517 } 1518 1519 static void svm_get_segment(struct kvm_vcpu *vcpu, 1520 struct kvm_segment *var, int seg) 1521 { 1522 struct vmcb_seg *s = svm_seg(vcpu, seg); 1523 1524 var->base = s->base; 1525 var->limit = s->limit; 1526 var->selector = s->selector; 1527 var->type = s->attrib & SVM_SELECTOR_TYPE_MASK; 1528 var->s = (s->attrib >> SVM_SELECTOR_S_SHIFT) & 1; 1529 var->dpl = (s->attrib >> SVM_SELECTOR_DPL_SHIFT) & 3; 1530 var->present = (s->attrib >> SVM_SELECTOR_P_SHIFT) & 1; 1531 var->avl = (s->attrib >> SVM_SELECTOR_AVL_SHIFT) & 1; 1532 var->l = (s->attrib >> SVM_SELECTOR_L_SHIFT) & 1; 1533 var->db = (s->attrib >> SVM_SELECTOR_DB_SHIFT) & 1; 1534 1535 /* 1536 * AMD CPUs circa 2014 track the G bit for all segments except CS. 1537 * However, the SVM spec states that the G bit is not observed by the 1538 * CPU, and some VMware virtual CPUs drop the G bit for all segments. 1539 * So let's synthesize a legal G bit for all segments, this helps 1540 * running KVM nested. It also helps cross-vendor migration, because 1541 * Intel's vmentry has a check on the 'G' bit. 1542 */ 1543 var->g = s->limit > 0xfffff; 1544 1545 /* 1546 * AMD's VMCB does not have an explicit unusable field, so emulate it 1547 * for cross vendor migration purposes by "not present" 1548 */ 1549 var->unusable = !var->present; 1550 1551 switch (seg) { 1552 case VCPU_SREG_TR: 1553 /* 1554 * Work around a bug where the busy flag in the tr selector 1555 * isn't exposed 1556 */ 1557 var->type |= 0x2; 1558 break; 1559 case VCPU_SREG_DS: 1560 case VCPU_SREG_ES: 1561 case VCPU_SREG_FS: 1562 case VCPU_SREG_GS: 1563 /* 1564 * The accessed bit must always be set in the segment 1565 * descriptor cache, although it can be cleared in the 1566 * descriptor, the cached bit always remains at 1. Since 1567 * Intel has a check on this, set it here to support 1568 * cross-vendor migration. 1569 */ 1570 if (!var->unusable) 1571 var->type |= 0x1; 1572 break; 1573 case VCPU_SREG_SS: 1574 /* 1575 * On AMD CPUs sometimes the DB bit in the segment 1576 * descriptor is left as 1, although the whole segment has 1577 * been made unusable. Clear it here to pass an Intel VMX 1578 * entry check when cross vendor migrating. 1579 */ 1580 if (var->unusable) 1581 var->db = 0; 1582 /* This is symmetric with svm_set_segment() */ 1583 var->dpl = to_svm(vcpu)->vmcb->save.cpl; 1584 break; 1585 } 1586 } 1587 1588 static int svm_get_cpl(struct kvm_vcpu *vcpu) 1589 { 1590 struct vmcb_save_area *save = &to_svm(vcpu)->vmcb->save; 1591 1592 return save->cpl; 1593 } 1594 1595 static void svm_get_cs_db_l_bits(struct kvm_vcpu *vcpu, int *db, int *l) 1596 { 1597 struct kvm_segment cs; 1598 1599 svm_get_segment(vcpu, &cs, VCPU_SREG_CS); 1600 *db = cs.db; 1601 *l = cs.l; 1602 } 1603 1604 static void svm_get_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt) 1605 { 1606 struct vcpu_svm *svm = to_svm(vcpu); 1607 1608 dt->size = svm->vmcb->save.idtr.limit; 1609 dt->address = svm->vmcb->save.idtr.base; 1610 } 1611 1612 static void svm_set_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt) 1613 { 1614 struct vcpu_svm *svm = to_svm(vcpu); 1615 1616 svm->vmcb->save.idtr.limit = dt->size; 1617 svm->vmcb->save.idtr.base = dt->address ; 1618 vmcb_mark_dirty(svm->vmcb, VMCB_DT); 1619 } 1620 1621 static void svm_get_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt) 1622 { 1623 struct vcpu_svm *svm = to_svm(vcpu); 1624 1625 dt->size = svm->vmcb->save.gdtr.limit; 1626 dt->address = svm->vmcb->save.gdtr.base; 1627 } 1628 1629 static void svm_set_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt) 1630 { 1631 struct vcpu_svm *svm = to_svm(vcpu); 1632 1633 svm->vmcb->save.gdtr.limit = dt->size; 1634 svm->vmcb->save.gdtr.base = dt->address ; 1635 vmcb_mark_dirty(svm->vmcb, VMCB_DT); 1636 } 1637 1638 static void sev_post_set_cr3(struct kvm_vcpu *vcpu, unsigned long cr3) 1639 { 1640 struct vcpu_svm *svm = to_svm(vcpu); 1641 1642 /* 1643 * For guests that don't set guest_state_protected, the cr3 update is 1644 * handled via kvm_mmu_load() while entering the guest. For guests 1645 * that do (SEV-ES/SEV-SNP), the cr3 update needs to be written to 1646 * VMCB save area now, since the save area will become the initial 1647 * contents of the VMSA, and future VMCB save area updates won't be 1648 * seen. 1649 */ 1650 if (sev_es_guest(vcpu->kvm)) { 1651 svm->vmcb->save.cr3 = cr3; 1652 vmcb_mark_dirty(svm->vmcb, VMCB_CR); 1653 } 1654 } 1655 1656 static bool svm_is_valid_cr0(struct kvm_vcpu *vcpu, unsigned long cr0) 1657 { 1658 return true; 1659 } 1660 1661 void svm_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0) 1662 { 1663 struct vcpu_svm *svm = to_svm(vcpu); 1664 u64 hcr0 = cr0; 1665 bool old_paging = is_paging(vcpu); 1666 1667 #ifdef CONFIG_X86_64 1668 if (vcpu->arch.efer & EFER_LME) { 1669 if (!is_paging(vcpu) && (cr0 & X86_CR0_PG)) { 1670 vcpu->arch.efer |= EFER_LMA; 1671 if (!vcpu->arch.guest_state_protected) 1672 svm->vmcb->save.efer |= EFER_LMA | EFER_LME; 1673 } 1674 1675 if (is_paging(vcpu) && !(cr0 & X86_CR0_PG)) { 1676 vcpu->arch.efer &= ~EFER_LMA; 1677 if (!vcpu->arch.guest_state_protected) 1678 svm->vmcb->save.efer &= ~(EFER_LMA | EFER_LME); 1679 } 1680 } 1681 #endif 1682 vcpu->arch.cr0 = cr0; 1683 1684 if (!npt_enabled) { 1685 hcr0 |= X86_CR0_PG | X86_CR0_WP; 1686 if (old_paging != is_paging(vcpu)) 1687 svm_set_cr4(vcpu, kvm_read_cr4(vcpu)); 1688 } 1689 1690 /* 1691 * re-enable caching here because the QEMU bios 1692 * does not do it - this results in some delay at 1693 * reboot 1694 */ 1695 if (kvm_check_has_quirk(vcpu->kvm, KVM_X86_QUIRK_CD_NW_CLEARED)) 1696 hcr0 &= ~(X86_CR0_CD | X86_CR0_NW); 1697 1698 svm->vmcb->save.cr0 = hcr0; 1699 vmcb_mark_dirty(svm->vmcb, VMCB_CR); 1700 1701 /* 1702 * SEV-ES guests must always keep the CR intercepts cleared. CR 1703 * tracking is done using the CR write traps. 1704 */ 1705 if (sev_es_guest(vcpu->kvm)) 1706 return; 1707 1708 if (hcr0 == cr0) { 1709 /* Selective CR0 write remains on. */ 1710 svm_clr_intercept(svm, INTERCEPT_CR0_READ); 1711 svm_clr_intercept(svm, INTERCEPT_CR0_WRITE); 1712 } else { 1713 svm_set_intercept(svm, INTERCEPT_CR0_READ); 1714 svm_set_intercept(svm, INTERCEPT_CR0_WRITE); 1715 } 1716 } 1717 1718 static bool svm_is_valid_cr4(struct kvm_vcpu *vcpu, unsigned long cr4) 1719 { 1720 return true; 1721 } 1722 1723 void svm_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4) 1724 { 1725 unsigned long host_cr4_mce = cr4_read_shadow() & X86_CR4_MCE; 1726 unsigned long old_cr4 = vcpu->arch.cr4; 1727 1728 vcpu->arch.cr4 = cr4; 1729 if (!npt_enabled) { 1730 cr4 |= X86_CR4_PAE; 1731 1732 if (!is_paging(vcpu)) 1733 cr4 &= ~(X86_CR4_SMEP | X86_CR4_SMAP | X86_CR4_PKE); 1734 } 1735 cr4 |= host_cr4_mce; 1736 to_svm(vcpu)->vmcb->save.cr4 = cr4; 1737 vmcb_mark_dirty(to_svm(vcpu)->vmcb, VMCB_CR); 1738 1739 if ((cr4 ^ old_cr4) & (X86_CR4_OSXSAVE | X86_CR4_PKE)) 1740 vcpu->arch.cpuid_dynamic_bits_dirty = true; 1741 } 1742 1743 static void svm_set_segment(struct kvm_vcpu *vcpu, 1744 struct kvm_segment *var, int seg) 1745 { 1746 struct vcpu_svm *svm = to_svm(vcpu); 1747 struct vmcb_seg *s = svm_seg(vcpu, seg); 1748 1749 s->base = var->base; 1750 s->limit = var->limit; 1751 s->selector = var->selector; 1752 s->attrib = (var->type & SVM_SELECTOR_TYPE_MASK); 1753 s->attrib |= (var->s & 1) << SVM_SELECTOR_S_SHIFT; 1754 s->attrib |= (var->dpl & 3) << SVM_SELECTOR_DPL_SHIFT; 1755 s->attrib |= ((var->present & 1) && !var->unusable) << SVM_SELECTOR_P_SHIFT; 1756 s->attrib |= (var->avl & 1) << SVM_SELECTOR_AVL_SHIFT; 1757 s->attrib |= (var->l & 1) << SVM_SELECTOR_L_SHIFT; 1758 s->attrib |= (var->db & 1) << SVM_SELECTOR_DB_SHIFT; 1759 s->attrib |= (var->g & 1) << SVM_SELECTOR_G_SHIFT; 1760 1761 /* 1762 * This is always accurate, except if SYSRET returned to a segment 1763 * with SS.DPL != 3. Intel does not have this quirk, and always 1764 * forces SS.DPL to 3 on sysret, so we ignore that case; fixing it 1765 * would entail passing the CPL to userspace and back. 1766 */ 1767 if (seg == VCPU_SREG_SS) 1768 /* This is symmetric with svm_get_segment() */ 1769 svm->vmcb->save.cpl = (var->dpl & 3); 1770 1771 vmcb_mark_dirty(svm->vmcb, VMCB_SEG); 1772 } 1773 1774 static void svm_update_exception_bitmap(struct kvm_vcpu *vcpu) 1775 { 1776 struct vcpu_svm *svm = to_svm(vcpu); 1777 1778 clr_exception_intercept(svm, BP_VECTOR); 1779 1780 if (vcpu->guest_debug & KVM_GUESTDBG_ENABLE) { 1781 if (vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP) 1782 set_exception_intercept(svm, BP_VECTOR); 1783 } 1784 } 1785 1786 static void new_asid(struct vcpu_svm *svm, struct svm_cpu_data *sd) 1787 { 1788 if (sd->next_asid > sd->max_asid) { 1789 ++sd->asid_generation; 1790 sd->next_asid = sd->min_asid; 1791 svm->vmcb->control.tlb_ctl = TLB_CONTROL_FLUSH_ALL_ASID; 1792 vmcb_mark_dirty(svm->vmcb, VMCB_ASID); 1793 } 1794 1795 svm->current_vmcb->asid_generation = sd->asid_generation; 1796 svm->asid = sd->next_asid++; 1797 } 1798 1799 static void svm_set_dr6(struct kvm_vcpu *vcpu, unsigned long value) 1800 { 1801 struct vmcb *vmcb = to_svm(vcpu)->vmcb; 1802 1803 if (vcpu->arch.guest_state_protected) 1804 return; 1805 1806 if (unlikely(value != vmcb->save.dr6)) { 1807 vmcb->save.dr6 = value; 1808 vmcb_mark_dirty(vmcb, VMCB_DR); 1809 } 1810 } 1811 1812 static void svm_sync_dirty_debug_regs(struct kvm_vcpu *vcpu) 1813 { 1814 struct vcpu_svm *svm = to_svm(vcpu); 1815 1816 if (WARN_ON_ONCE(sev_es_guest(vcpu->kvm))) 1817 return; 1818 1819 get_debugreg(vcpu->arch.db[0], 0); 1820 get_debugreg(vcpu->arch.db[1], 1); 1821 get_debugreg(vcpu->arch.db[2], 2); 1822 get_debugreg(vcpu->arch.db[3], 3); 1823 /* 1824 * We cannot reset svm->vmcb->save.dr6 to DR6_ACTIVE_LOW here, 1825 * because db_interception might need it. We can do it before vmentry. 1826 */ 1827 vcpu->arch.dr6 = svm->vmcb->save.dr6; 1828 vcpu->arch.dr7 = svm->vmcb->save.dr7; 1829 vcpu->arch.switch_db_regs &= ~KVM_DEBUGREG_WONT_EXIT; 1830 set_dr_intercepts(svm); 1831 } 1832 1833 static void svm_set_dr7(struct kvm_vcpu *vcpu, unsigned long value) 1834 { 1835 struct vcpu_svm *svm = to_svm(vcpu); 1836 1837 if (vcpu->arch.guest_state_protected) 1838 return; 1839 1840 svm->vmcb->save.dr7 = value; 1841 vmcb_mark_dirty(svm->vmcb, VMCB_DR); 1842 } 1843 1844 static int pf_interception(struct kvm_vcpu *vcpu) 1845 { 1846 struct vcpu_svm *svm = to_svm(vcpu); 1847 1848 u64 fault_address = svm->vmcb->control.exit_info_2; 1849 u64 error_code = svm->vmcb->control.exit_info_1; 1850 1851 return kvm_handle_page_fault(vcpu, error_code, fault_address, 1852 static_cpu_has(X86_FEATURE_DECODEASSISTS) ? 1853 svm->vmcb->control.insn_bytes : NULL, 1854 svm->vmcb->control.insn_len); 1855 } 1856 1857 static int npf_interception(struct kvm_vcpu *vcpu) 1858 { 1859 struct vcpu_svm *svm = to_svm(vcpu); 1860 int rc; 1861 1862 u64 fault_address = svm->vmcb->control.exit_info_2; 1863 u64 error_code = svm->vmcb->control.exit_info_1; 1864 1865 /* 1866 * WARN if hardware generates a fault with an error code that collides 1867 * with KVM-defined sythentic flags. Clear the flags and continue on, 1868 * i.e. don't terminate the VM, as KVM can't possibly be relying on a 1869 * flag that KVM doesn't know about. 1870 */ 1871 if (WARN_ON_ONCE(error_code & PFERR_SYNTHETIC_MASK)) 1872 error_code &= ~PFERR_SYNTHETIC_MASK; 1873 1874 if (sev_snp_guest(vcpu->kvm) && (error_code & PFERR_GUEST_ENC_MASK)) 1875 error_code |= PFERR_PRIVATE_ACCESS; 1876 1877 trace_kvm_page_fault(vcpu, fault_address, error_code); 1878 rc = kvm_mmu_page_fault(vcpu, fault_address, error_code, 1879 static_cpu_has(X86_FEATURE_DECODEASSISTS) ? 1880 svm->vmcb->control.insn_bytes : NULL, 1881 svm->vmcb->control.insn_len); 1882 1883 if (rc > 0 && error_code & PFERR_GUEST_RMP_MASK) 1884 sev_handle_rmp_fault(vcpu, fault_address, error_code); 1885 1886 return rc; 1887 } 1888 1889 static int db_interception(struct kvm_vcpu *vcpu) 1890 { 1891 struct kvm_run *kvm_run = vcpu->run; 1892 struct vcpu_svm *svm = to_svm(vcpu); 1893 1894 if (!(vcpu->guest_debug & 1895 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP)) && 1896 !svm->nmi_singlestep) { 1897 u32 payload = svm->vmcb->save.dr6 ^ DR6_ACTIVE_LOW; 1898 kvm_queue_exception_p(vcpu, DB_VECTOR, payload); 1899 return 1; 1900 } 1901 1902 if (svm->nmi_singlestep) { 1903 disable_nmi_singlestep(svm); 1904 /* Make sure we check for pending NMIs upon entry */ 1905 kvm_make_request(KVM_REQ_EVENT, vcpu); 1906 } 1907 1908 if (vcpu->guest_debug & 1909 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP)) { 1910 kvm_run->exit_reason = KVM_EXIT_DEBUG; 1911 kvm_run->debug.arch.dr6 = svm->vmcb->save.dr6; 1912 kvm_run->debug.arch.dr7 = svm->vmcb->save.dr7; 1913 kvm_run->debug.arch.pc = 1914 svm->vmcb->save.cs.base + svm->vmcb->save.rip; 1915 kvm_run->debug.arch.exception = DB_VECTOR; 1916 return 0; 1917 } 1918 1919 return 1; 1920 } 1921 1922 static int bp_interception(struct kvm_vcpu *vcpu) 1923 { 1924 struct vcpu_svm *svm = to_svm(vcpu); 1925 struct kvm_run *kvm_run = vcpu->run; 1926 1927 kvm_run->exit_reason = KVM_EXIT_DEBUG; 1928 kvm_run->debug.arch.pc = svm->vmcb->save.cs.base + svm->vmcb->save.rip; 1929 kvm_run->debug.arch.exception = BP_VECTOR; 1930 return 0; 1931 } 1932 1933 static int ud_interception(struct kvm_vcpu *vcpu) 1934 { 1935 return handle_ud(vcpu); 1936 } 1937 1938 static int ac_interception(struct kvm_vcpu *vcpu) 1939 { 1940 kvm_queue_exception_e(vcpu, AC_VECTOR, 0); 1941 return 1; 1942 } 1943 1944 static bool is_erratum_383(void) 1945 { 1946 int i; 1947 u64 value; 1948 1949 if (!erratum_383_found) 1950 return false; 1951 1952 if (native_read_msr_safe(MSR_IA32_MC0_STATUS, &value)) 1953 return false; 1954 1955 /* Bit 62 may or may not be set for this mce */ 1956 value &= ~(1ULL << 62); 1957 1958 if (value != 0xb600000000010015ULL) 1959 return false; 1960 1961 /* Clear MCi_STATUS registers */ 1962 for (i = 0; i < 6; ++i) 1963 native_write_msr_safe(MSR_IA32_MCx_STATUS(i), 0); 1964 1965 if (!native_read_msr_safe(MSR_IA32_MCG_STATUS, &value)) { 1966 value &= ~(1ULL << 2); 1967 native_write_msr_safe(MSR_IA32_MCG_STATUS, value); 1968 } 1969 1970 /* Flush tlb to evict multi-match entries */ 1971 __flush_tlb_all(); 1972 1973 return true; 1974 } 1975 1976 static void svm_handle_mce(struct kvm_vcpu *vcpu) 1977 { 1978 if (is_erratum_383()) { 1979 /* 1980 * Erratum 383 triggered. Guest state is corrupt so kill the 1981 * guest. 1982 */ 1983 pr_err("Guest triggered AMD Erratum 383\n"); 1984 1985 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu); 1986 1987 return; 1988 } 1989 1990 /* 1991 * On an #MC intercept the MCE handler is not called automatically in 1992 * the host. So do it by hand here. 1993 */ 1994 kvm_machine_check(); 1995 } 1996 1997 static int mc_interception(struct kvm_vcpu *vcpu) 1998 { 1999 return 1; 2000 } 2001 2002 static int shutdown_interception(struct kvm_vcpu *vcpu) 2003 { 2004 struct kvm_run *kvm_run = vcpu->run; 2005 struct vcpu_svm *svm = to_svm(vcpu); 2006 2007 2008 /* 2009 * VMCB is undefined after a SHUTDOWN intercept. INIT the vCPU to put 2010 * the VMCB in a known good state. Unfortuately, KVM doesn't have 2011 * KVM_MP_STATE_SHUTDOWN and can't add it without potentially breaking 2012 * userspace. At a platform view, INIT is acceptable behavior as 2013 * there exist bare metal platforms that automatically INIT the CPU 2014 * in response to shutdown. 2015 * 2016 * The VM save area for SEV-ES guests has already been encrypted so it 2017 * cannot be reinitialized, i.e. synthesizing INIT is futile. 2018 */ 2019 if (!sev_es_guest(vcpu->kvm)) { 2020 clear_page(svm->vmcb); 2021 #ifdef CONFIG_KVM_SMM 2022 if (is_smm(vcpu)) 2023 kvm_smm_changed(vcpu, false); 2024 #endif 2025 kvm_vcpu_reset(vcpu, true); 2026 } 2027 2028 kvm_run->exit_reason = KVM_EXIT_SHUTDOWN; 2029 return 0; 2030 } 2031 2032 static int io_interception(struct kvm_vcpu *vcpu) 2033 { 2034 struct vcpu_svm *svm = to_svm(vcpu); 2035 u32 io_info = svm->vmcb->control.exit_info_1; /* address size bug? */ 2036 int size, in, string; 2037 unsigned port; 2038 2039 ++vcpu->stat.io_exits; 2040 string = (io_info & SVM_IOIO_STR_MASK) != 0; 2041 in = (io_info & SVM_IOIO_TYPE_MASK) != 0; 2042 port = io_info >> 16; 2043 size = (io_info & SVM_IOIO_SIZE_MASK) >> SVM_IOIO_SIZE_SHIFT; 2044 2045 if (string) { 2046 if (sev_es_guest(vcpu->kvm)) 2047 return sev_es_string_io(svm, size, port, in); 2048 else 2049 return kvm_emulate_instruction(vcpu, 0); 2050 } 2051 2052 svm->next_rip = svm->vmcb->control.exit_info_2; 2053 2054 return kvm_fast_pio(vcpu, size, port, in); 2055 } 2056 2057 static int nmi_interception(struct kvm_vcpu *vcpu) 2058 { 2059 return 1; 2060 } 2061 2062 static int smi_interception(struct kvm_vcpu *vcpu) 2063 { 2064 return 1; 2065 } 2066 2067 static int intr_interception(struct kvm_vcpu *vcpu) 2068 { 2069 ++vcpu->stat.irq_exits; 2070 return 1; 2071 } 2072 2073 static int vmload_vmsave_interception(struct kvm_vcpu *vcpu, bool vmload) 2074 { 2075 struct vcpu_svm *svm = to_svm(vcpu); 2076 struct vmcb *vmcb12; 2077 struct kvm_host_map map; 2078 int ret; 2079 2080 if (nested_svm_check_permissions(vcpu)) 2081 return 1; 2082 2083 ret = kvm_vcpu_map(vcpu, gpa_to_gfn(svm->vmcb->save.rax), &map); 2084 if (ret) { 2085 if (ret == -EINVAL) 2086 kvm_inject_gp(vcpu, 0); 2087 return 1; 2088 } 2089 2090 vmcb12 = map.hva; 2091 2092 ret = kvm_skip_emulated_instruction(vcpu); 2093 2094 if (vmload) { 2095 svm_copy_vmloadsave_state(svm->vmcb, vmcb12); 2096 svm->sysenter_eip_hi = 0; 2097 svm->sysenter_esp_hi = 0; 2098 } else { 2099 svm_copy_vmloadsave_state(vmcb12, svm->vmcb); 2100 } 2101 2102 kvm_vcpu_unmap(vcpu, &map); 2103 2104 return ret; 2105 } 2106 2107 static int vmload_interception(struct kvm_vcpu *vcpu) 2108 { 2109 return vmload_vmsave_interception(vcpu, true); 2110 } 2111 2112 static int vmsave_interception(struct kvm_vcpu *vcpu) 2113 { 2114 return vmload_vmsave_interception(vcpu, false); 2115 } 2116 2117 static int vmrun_interception(struct kvm_vcpu *vcpu) 2118 { 2119 if (nested_svm_check_permissions(vcpu)) 2120 return 1; 2121 2122 return nested_svm_vmrun(vcpu); 2123 } 2124 2125 enum { 2126 NONE_SVM_INSTR, 2127 SVM_INSTR_VMRUN, 2128 SVM_INSTR_VMLOAD, 2129 SVM_INSTR_VMSAVE, 2130 }; 2131 2132 /* Return NONE_SVM_INSTR if not SVM instrs, otherwise return decode result */ 2133 static int svm_instr_opcode(struct kvm_vcpu *vcpu) 2134 { 2135 struct x86_emulate_ctxt *ctxt = vcpu->arch.emulate_ctxt; 2136 2137 if (ctxt->b != 0x1 || ctxt->opcode_len != 2) 2138 return NONE_SVM_INSTR; 2139 2140 switch (ctxt->modrm) { 2141 case 0xd8: /* VMRUN */ 2142 return SVM_INSTR_VMRUN; 2143 case 0xda: /* VMLOAD */ 2144 return SVM_INSTR_VMLOAD; 2145 case 0xdb: /* VMSAVE */ 2146 return SVM_INSTR_VMSAVE; 2147 default: 2148 break; 2149 } 2150 2151 return NONE_SVM_INSTR; 2152 } 2153 2154 static int emulate_svm_instr(struct kvm_vcpu *vcpu, int opcode) 2155 { 2156 const int guest_mode_exit_codes[] = { 2157 [SVM_INSTR_VMRUN] = SVM_EXIT_VMRUN, 2158 [SVM_INSTR_VMLOAD] = SVM_EXIT_VMLOAD, 2159 [SVM_INSTR_VMSAVE] = SVM_EXIT_VMSAVE, 2160 }; 2161 int (*const svm_instr_handlers[])(struct kvm_vcpu *vcpu) = { 2162 [SVM_INSTR_VMRUN] = vmrun_interception, 2163 [SVM_INSTR_VMLOAD] = vmload_interception, 2164 [SVM_INSTR_VMSAVE] = vmsave_interception, 2165 }; 2166 struct vcpu_svm *svm = to_svm(vcpu); 2167 int ret; 2168 2169 if (is_guest_mode(vcpu)) { 2170 /* Returns '1' or -errno on failure, '0' on success. */ 2171 ret = nested_svm_simple_vmexit(svm, guest_mode_exit_codes[opcode]); 2172 if (ret) 2173 return ret; 2174 return 1; 2175 } 2176 return svm_instr_handlers[opcode](vcpu); 2177 } 2178 2179 /* 2180 * #GP handling code. Note that #GP can be triggered under the following two 2181 * cases: 2182 * 1) SVM VM-related instructions (VMRUN/VMSAVE/VMLOAD) that trigger #GP on 2183 * some AMD CPUs when EAX of these instructions are in the reserved memory 2184 * regions (e.g. SMM memory on host). 2185 * 2) VMware backdoor 2186 */ 2187 static int gp_interception(struct kvm_vcpu *vcpu) 2188 { 2189 struct vcpu_svm *svm = to_svm(vcpu); 2190 u32 error_code = svm->vmcb->control.exit_info_1; 2191 int opcode; 2192 2193 /* Both #GP cases have zero error_code */ 2194 if (error_code) 2195 goto reinject; 2196 2197 /* Decode the instruction for usage later */ 2198 if (x86_decode_emulated_instruction(vcpu, 0, NULL, 0) != EMULATION_OK) 2199 goto reinject; 2200 2201 opcode = svm_instr_opcode(vcpu); 2202 2203 if (opcode == NONE_SVM_INSTR) { 2204 if (!enable_vmware_backdoor) 2205 goto reinject; 2206 2207 /* 2208 * VMware backdoor emulation on #GP interception only handles 2209 * IN{S}, OUT{S}, and RDPMC. 2210 */ 2211 if (!is_guest_mode(vcpu)) 2212 return kvm_emulate_instruction(vcpu, 2213 EMULTYPE_VMWARE_GP | EMULTYPE_NO_DECODE); 2214 } else { 2215 /* All SVM instructions expect page aligned RAX */ 2216 if (svm->vmcb->save.rax & ~PAGE_MASK) 2217 goto reinject; 2218 2219 return emulate_svm_instr(vcpu, opcode); 2220 } 2221 2222 reinject: 2223 kvm_queue_exception_e(vcpu, GP_VECTOR, error_code); 2224 return 1; 2225 } 2226 2227 void svm_set_gif(struct vcpu_svm *svm, bool value) 2228 { 2229 if (value) { 2230 /* 2231 * If VGIF is enabled, the STGI intercept is only added to 2232 * detect the opening of the SMI/NMI window; remove it now. 2233 * Likewise, clear the VINTR intercept, we will set it 2234 * again while processing KVM_REQ_EVENT if needed. 2235 */ 2236 if (vgif) 2237 svm_clr_intercept(svm, INTERCEPT_STGI); 2238 if (svm_is_intercept(svm, INTERCEPT_VINTR)) 2239 svm_clear_vintr(svm); 2240 2241 enable_gif(svm); 2242 if (svm->vcpu.arch.smi_pending || 2243 svm->vcpu.arch.nmi_pending || 2244 kvm_cpu_has_injectable_intr(&svm->vcpu) || 2245 kvm_apic_has_pending_init_or_sipi(&svm->vcpu)) 2246 kvm_make_request(KVM_REQ_EVENT, &svm->vcpu); 2247 } else { 2248 disable_gif(svm); 2249 2250 /* 2251 * After a CLGI no interrupts should come. But if vGIF is 2252 * in use, we still rely on the VINTR intercept (rather than 2253 * STGI) to detect an open interrupt window. 2254 */ 2255 if (!vgif) 2256 svm_clear_vintr(svm); 2257 } 2258 } 2259 2260 static int stgi_interception(struct kvm_vcpu *vcpu) 2261 { 2262 int ret; 2263 2264 if (nested_svm_check_permissions(vcpu)) 2265 return 1; 2266 2267 ret = kvm_skip_emulated_instruction(vcpu); 2268 svm_set_gif(to_svm(vcpu), true); 2269 return ret; 2270 } 2271 2272 static int clgi_interception(struct kvm_vcpu *vcpu) 2273 { 2274 int ret; 2275 2276 if (nested_svm_check_permissions(vcpu)) 2277 return 1; 2278 2279 ret = kvm_skip_emulated_instruction(vcpu); 2280 svm_set_gif(to_svm(vcpu), false); 2281 return ret; 2282 } 2283 2284 static int invlpga_interception(struct kvm_vcpu *vcpu) 2285 { 2286 gva_t gva = kvm_rax_read(vcpu); 2287 u32 asid = kvm_rcx_read(vcpu); 2288 2289 /* FIXME: Handle an address size prefix. */ 2290 if (!is_long_mode(vcpu)) 2291 gva = (u32)gva; 2292 2293 trace_kvm_invlpga(to_svm(vcpu)->vmcb->save.rip, asid, gva); 2294 2295 /* Let's treat INVLPGA the same as INVLPG (can be optimized!) */ 2296 kvm_mmu_invlpg(vcpu, gva); 2297 2298 return kvm_skip_emulated_instruction(vcpu); 2299 } 2300 2301 static int skinit_interception(struct kvm_vcpu *vcpu) 2302 { 2303 trace_kvm_skinit(to_svm(vcpu)->vmcb->save.rip, kvm_rax_read(vcpu)); 2304 2305 kvm_queue_exception(vcpu, UD_VECTOR); 2306 return 1; 2307 } 2308 2309 static int task_switch_interception(struct kvm_vcpu *vcpu) 2310 { 2311 struct vcpu_svm *svm = to_svm(vcpu); 2312 u16 tss_selector; 2313 int reason; 2314 int int_type = svm->vmcb->control.exit_int_info & 2315 SVM_EXITINTINFO_TYPE_MASK; 2316 int int_vec = svm->vmcb->control.exit_int_info & SVM_EVTINJ_VEC_MASK; 2317 uint32_t type = 2318 svm->vmcb->control.exit_int_info & SVM_EXITINTINFO_TYPE_MASK; 2319 uint32_t idt_v = 2320 svm->vmcb->control.exit_int_info & SVM_EXITINTINFO_VALID; 2321 bool has_error_code = false; 2322 u32 error_code = 0; 2323 2324 tss_selector = (u16)svm->vmcb->control.exit_info_1; 2325 2326 if (svm->vmcb->control.exit_info_2 & 2327 (1ULL << SVM_EXITINFOSHIFT_TS_REASON_IRET)) 2328 reason = TASK_SWITCH_IRET; 2329 else if (svm->vmcb->control.exit_info_2 & 2330 (1ULL << SVM_EXITINFOSHIFT_TS_REASON_JMP)) 2331 reason = TASK_SWITCH_JMP; 2332 else if (idt_v) 2333 reason = TASK_SWITCH_GATE; 2334 else 2335 reason = TASK_SWITCH_CALL; 2336 2337 if (reason == TASK_SWITCH_GATE) { 2338 switch (type) { 2339 case SVM_EXITINTINFO_TYPE_NMI: 2340 vcpu->arch.nmi_injected = false; 2341 break; 2342 case SVM_EXITINTINFO_TYPE_EXEPT: 2343 if (svm->vmcb->control.exit_info_2 & 2344 (1ULL << SVM_EXITINFOSHIFT_TS_HAS_ERROR_CODE)) { 2345 has_error_code = true; 2346 error_code = 2347 (u32)svm->vmcb->control.exit_info_2; 2348 } 2349 kvm_clear_exception_queue(vcpu); 2350 break; 2351 case SVM_EXITINTINFO_TYPE_INTR: 2352 case SVM_EXITINTINFO_TYPE_SOFT: 2353 kvm_clear_interrupt_queue(vcpu); 2354 break; 2355 default: 2356 break; 2357 } 2358 } 2359 2360 if (reason != TASK_SWITCH_GATE || 2361 int_type == SVM_EXITINTINFO_TYPE_SOFT || 2362 (int_type == SVM_EXITINTINFO_TYPE_EXEPT && 2363 (int_vec == OF_VECTOR || int_vec == BP_VECTOR))) { 2364 if (!svm_skip_emulated_instruction(vcpu)) 2365 return 0; 2366 } 2367 2368 if (int_type != SVM_EXITINTINFO_TYPE_SOFT) 2369 int_vec = -1; 2370 2371 return kvm_task_switch(vcpu, tss_selector, int_vec, reason, 2372 has_error_code, error_code); 2373 } 2374 2375 static void svm_clr_iret_intercept(struct vcpu_svm *svm) 2376 { 2377 if (!sev_es_guest(svm->vcpu.kvm)) 2378 svm_clr_intercept(svm, INTERCEPT_IRET); 2379 } 2380 2381 static void svm_set_iret_intercept(struct vcpu_svm *svm) 2382 { 2383 if (!sev_es_guest(svm->vcpu.kvm)) 2384 svm_set_intercept(svm, INTERCEPT_IRET); 2385 } 2386 2387 static int iret_interception(struct kvm_vcpu *vcpu) 2388 { 2389 struct vcpu_svm *svm = to_svm(vcpu); 2390 2391 WARN_ON_ONCE(sev_es_guest(vcpu->kvm)); 2392 2393 ++vcpu->stat.nmi_window_exits; 2394 svm->awaiting_iret_completion = true; 2395 2396 svm_clr_iret_intercept(svm); 2397 svm->nmi_iret_rip = kvm_rip_read(vcpu); 2398 2399 kvm_make_request(KVM_REQ_EVENT, vcpu); 2400 return 1; 2401 } 2402 2403 static int invlpg_interception(struct kvm_vcpu *vcpu) 2404 { 2405 if (!static_cpu_has(X86_FEATURE_DECODEASSISTS)) 2406 return kvm_emulate_instruction(vcpu, 0); 2407 2408 kvm_mmu_invlpg(vcpu, to_svm(vcpu)->vmcb->control.exit_info_1); 2409 return kvm_skip_emulated_instruction(vcpu); 2410 } 2411 2412 static int emulate_on_interception(struct kvm_vcpu *vcpu) 2413 { 2414 return kvm_emulate_instruction(vcpu, 0); 2415 } 2416 2417 static int rsm_interception(struct kvm_vcpu *vcpu) 2418 { 2419 return kvm_emulate_instruction_from_buffer(vcpu, rsm_ins_bytes, 2); 2420 } 2421 2422 static bool check_selective_cr0_intercepted(struct kvm_vcpu *vcpu, 2423 unsigned long val) 2424 { 2425 struct vcpu_svm *svm = to_svm(vcpu); 2426 unsigned long cr0 = vcpu->arch.cr0; 2427 bool ret = false; 2428 2429 if (!is_guest_mode(vcpu) || 2430 (!(vmcb12_is_intercept(&svm->nested.ctl, INTERCEPT_SELECTIVE_CR0)))) 2431 return false; 2432 2433 cr0 &= ~SVM_CR0_SELECTIVE_MASK; 2434 val &= ~SVM_CR0_SELECTIVE_MASK; 2435 2436 if (cr0 ^ val) { 2437 svm->vmcb->control.exit_code = SVM_EXIT_CR0_SEL_WRITE; 2438 ret = (nested_svm_exit_handled(svm) == NESTED_EXIT_DONE); 2439 } 2440 2441 return ret; 2442 } 2443 2444 #define CR_VALID (1ULL << 63) 2445 2446 static int cr_interception(struct kvm_vcpu *vcpu) 2447 { 2448 struct vcpu_svm *svm = to_svm(vcpu); 2449 int reg, cr; 2450 unsigned long val; 2451 int err; 2452 2453 if (!static_cpu_has(X86_FEATURE_DECODEASSISTS)) 2454 return emulate_on_interception(vcpu); 2455 2456 if (unlikely((svm->vmcb->control.exit_info_1 & CR_VALID) == 0)) 2457 return emulate_on_interception(vcpu); 2458 2459 reg = svm->vmcb->control.exit_info_1 & SVM_EXITINFO_REG_MASK; 2460 if (svm->vmcb->control.exit_code == SVM_EXIT_CR0_SEL_WRITE) 2461 cr = SVM_EXIT_WRITE_CR0 - SVM_EXIT_READ_CR0; 2462 else 2463 cr = svm->vmcb->control.exit_code - SVM_EXIT_READ_CR0; 2464 2465 err = 0; 2466 if (cr >= 16) { /* mov to cr */ 2467 cr -= 16; 2468 val = kvm_register_read(vcpu, reg); 2469 trace_kvm_cr_write(cr, val); 2470 switch (cr) { 2471 case 0: 2472 if (!check_selective_cr0_intercepted(vcpu, val)) 2473 err = kvm_set_cr0(vcpu, val); 2474 else 2475 return 1; 2476 2477 break; 2478 case 3: 2479 err = kvm_set_cr3(vcpu, val); 2480 break; 2481 case 4: 2482 err = kvm_set_cr4(vcpu, val); 2483 break; 2484 case 8: 2485 err = kvm_set_cr8(vcpu, val); 2486 break; 2487 default: 2488 WARN(1, "unhandled write to CR%d", cr); 2489 kvm_queue_exception(vcpu, UD_VECTOR); 2490 return 1; 2491 } 2492 } else { /* mov from cr */ 2493 switch (cr) { 2494 case 0: 2495 val = kvm_read_cr0(vcpu); 2496 break; 2497 case 2: 2498 val = vcpu->arch.cr2; 2499 break; 2500 case 3: 2501 val = kvm_read_cr3(vcpu); 2502 break; 2503 case 4: 2504 val = kvm_read_cr4(vcpu); 2505 break; 2506 case 8: 2507 val = kvm_get_cr8(vcpu); 2508 break; 2509 default: 2510 WARN(1, "unhandled read from CR%d", cr); 2511 kvm_queue_exception(vcpu, UD_VECTOR); 2512 return 1; 2513 } 2514 kvm_register_write(vcpu, reg, val); 2515 trace_kvm_cr_read(cr, val); 2516 } 2517 return kvm_complete_insn_gp(vcpu, err); 2518 } 2519 2520 static int cr_trap(struct kvm_vcpu *vcpu) 2521 { 2522 struct vcpu_svm *svm = to_svm(vcpu); 2523 unsigned long old_value, new_value; 2524 unsigned int cr; 2525 int ret = 0; 2526 2527 new_value = (unsigned long)svm->vmcb->control.exit_info_1; 2528 2529 cr = svm->vmcb->control.exit_code - SVM_EXIT_CR0_WRITE_TRAP; 2530 switch (cr) { 2531 case 0: 2532 old_value = kvm_read_cr0(vcpu); 2533 svm_set_cr0(vcpu, new_value); 2534 2535 kvm_post_set_cr0(vcpu, old_value, new_value); 2536 break; 2537 case 4: 2538 old_value = kvm_read_cr4(vcpu); 2539 svm_set_cr4(vcpu, new_value); 2540 2541 kvm_post_set_cr4(vcpu, old_value, new_value); 2542 break; 2543 case 8: 2544 ret = kvm_set_cr8(vcpu, new_value); 2545 break; 2546 default: 2547 WARN(1, "unhandled CR%d write trap", cr); 2548 kvm_queue_exception(vcpu, UD_VECTOR); 2549 return 1; 2550 } 2551 2552 return kvm_complete_insn_gp(vcpu, ret); 2553 } 2554 2555 static int dr_interception(struct kvm_vcpu *vcpu) 2556 { 2557 struct vcpu_svm *svm = to_svm(vcpu); 2558 int reg, dr; 2559 int err = 0; 2560 2561 /* 2562 * SEV-ES intercepts DR7 only to disable guest debugging and the guest issues a VMGEXIT 2563 * for DR7 write only. KVM cannot change DR7 (always swapped as type 'A') so return early. 2564 */ 2565 if (sev_es_guest(vcpu->kvm)) 2566 return 1; 2567 2568 if (vcpu->guest_debug == 0) { 2569 /* 2570 * No more DR vmexits; force a reload of the debug registers 2571 * and reenter on this instruction. The next vmexit will 2572 * retrieve the full state of the debug registers. 2573 */ 2574 clr_dr_intercepts(svm); 2575 vcpu->arch.switch_db_regs |= KVM_DEBUGREG_WONT_EXIT; 2576 return 1; 2577 } 2578 2579 if (!boot_cpu_has(X86_FEATURE_DECODEASSISTS)) 2580 return emulate_on_interception(vcpu); 2581 2582 reg = svm->vmcb->control.exit_info_1 & SVM_EXITINFO_REG_MASK; 2583 dr = svm->vmcb->control.exit_code - SVM_EXIT_READ_DR0; 2584 if (dr >= 16) { /* mov to DRn */ 2585 dr -= 16; 2586 err = kvm_set_dr(vcpu, dr, kvm_register_read(vcpu, reg)); 2587 } else { 2588 kvm_register_write(vcpu, reg, kvm_get_dr(vcpu, dr)); 2589 } 2590 2591 return kvm_complete_insn_gp(vcpu, err); 2592 } 2593 2594 static int cr8_write_interception(struct kvm_vcpu *vcpu) 2595 { 2596 int r; 2597 2598 u8 cr8_prev = kvm_get_cr8(vcpu); 2599 /* instruction emulation calls kvm_set_cr8() */ 2600 r = cr_interception(vcpu); 2601 if (lapic_in_kernel(vcpu)) 2602 return r; 2603 if (cr8_prev <= kvm_get_cr8(vcpu)) 2604 return r; 2605 vcpu->run->exit_reason = KVM_EXIT_SET_TPR; 2606 return 0; 2607 } 2608 2609 static int efer_trap(struct kvm_vcpu *vcpu) 2610 { 2611 struct msr_data msr_info; 2612 int ret; 2613 2614 /* 2615 * Clear the EFER_SVME bit from EFER. The SVM code always sets this 2616 * bit in svm_set_efer(), but __kvm_valid_efer() checks it against 2617 * whether the guest has X86_FEATURE_SVM - this avoids a failure if 2618 * the guest doesn't have X86_FEATURE_SVM. 2619 */ 2620 msr_info.host_initiated = false; 2621 msr_info.index = MSR_EFER; 2622 msr_info.data = to_svm(vcpu)->vmcb->control.exit_info_1 & ~EFER_SVME; 2623 ret = kvm_set_msr_common(vcpu, &msr_info); 2624 2625 return kvm_complete_insn_gp(vcpu, ret); 2626 } 2627 2628 static int svm_get_feature_msr(u32 msr, u64 *data) 2629 { 2630 *data = 0; 2631 2632 switch (msr) { 2633 case MSR_AMD64_DE_CFG: 2634 if (cpu_feature_enabled(X86_FEATURE_LFENCE_RDTSC)) 2635 *data |= MSR_AMD64_DE_CFG_LFENCE_SERIALIZE; 2636 break; 2637 default: 2638 return KVM_MSR_RET_UNSUPPORTED; 2639 } 2640 2641 return 0; 2642 } 2643 2644 static bool sev_es_prevent_msr_access(struct kvm_vcpu *vcpu, 2645 struct msr_data *msr_info) 2646 { 2647 return sev_es_guest(vcpu->kvm) && vcpu->arch.guest_state_protected && 2648 msr_info->index != MSR_IA32_XSS && 2649 !msr_write_intercepted(vcpu, msr_info->index); 2650 } 2651 2652 static int svm_get_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info) 2653 { 2654 struct vcpu_svm *svm = to_svm(vcpu); 2655 2656 if (sev_es_prevent_msr_access(vcpu, msr_info)) { 2657 msr_info->data = 0; 2658 return vcpu->kvm->arch.has_protected_state ? -EINVAL : 0; 2659 } 2660 2661 switch (msr_info->index) { 2662 case MSR_AMD64_TSC_RATIO: 2663 if (!msr_info->host_initiated && 2664 !guest_cpu_cap_has(vcpu, X86_FEATURE_TSCRATEMSR)) 2665 return 1; 2666 msr_info->data = svm->tsc_ratio_msr; 2667 break; 2668 case MSR_STAR: 2669 msr_info->data = svm->vmcb01.ptr->save.star; 2670 break; 2671 #ifdef CONFIG_X86_64 2672 case MSR_LSTAR: 2673 msr_info->data = svm->vmcb01.ptr->save.lstar; 2674 break; 2675 case MSR_CSTAR: 2676 msr_info->data = svm->vmcb01.ptr->save.cstar; 2677 break; 2678 case MSR_GS_BASE: 2679 msr_info->data = svm->vmcb01.ptr->save.gs.base; 2680 break; 2681 case MSR_FS_BASE: 2682 msr_info->data = svm->vmcb01.ptr->save.fs.base; 2683 break; 2684 case MSR_KERNEL_GS_BASE: 2685 msr_info->data = svm->vmcb01.ptr->save.kernel_gs_base; 2686 break; 2687 case MSR_SYSCALL_MASK: 2688 msr_info->data = svm->vmcb01.ptr->save.sfmask; 2689 break; 2690 #endif 2691 case MSR_IA32_SYSENTER_CS: 2692 msr_info->data = svm->vmcb01.ptr->save.sysenter_cs; 2693 break; 2694 case MSR_IA32_SYSENTER_EIP: 2695 msr_info->data = (u32)svm->vmcb01.ptr->save.sysenter_eip; 2696 if (guest_cpuid_is_intel_compatible(vcpu)) 2697 msr_info->data |= (u64)svm->sysenter_eip_hi << 32; 2698 break; 2699 case MSR_IA32_SYSENTER_ESP: 2700 msr_info->data = svm->vmcb01.ptr->save.sysenter_esp; 2701 if (guest_cpuid_is_intel_compatible(vcpu)) 2702 msr_info->data |= (u64)svm->sysenter_esp_hi << 32; 2703 break; 2704 case MSR_IA32_S_CET: 2705 msr_info->data = svm->vmcb->save.s_cet; 2706 break; 2707 case MSR_IA32_INT_SSP_TAB: 2708 msr_info->data = svm->vmcb->save.isst_addr; 2709 break; 2710 case MSR_KVM_INTERNAL_GUEST_SSP: 2711 msr_info->data = svm->vmcb->save.ssp; 2712 break; 2713 case MSR_TSC_AUX: 2714 msr_info->data = svm->tsc_aux; 2715 break; 2716 case MSR_IA32_DEBUGCTLMSR: 2717 msr_info->data = svm->vmcb->save.dbgctl; 2718 break; 2719 case MSR_IA32_LASTBRANCHFROMIP: 2720 msr_info->data = svm->vmcb->save.br_from; 2721 break; 2722 case MSR_IA32_LASTBRANCHTOIP: 2723 msr_info->data = svm->vmcb->save.br_to; 2724 break; 2725 case MSR_IA32_LASTINTFROMIP: 2726 msr_info->data = svm->vmcb->save.last_excp_from; 2727 break; 2728 case MSR_IA32_LASTINTTOIP: 2729 msr_info->data = svm->vmcb->save.last_excp_to; 2730 break; 2731 case MSR_VM_HSAVE_PA: 2732 msr_info->data = svm->nested.hsave_msr; 2733 break; 2734 case MSR_VM_CR: 2735 msr_info->data = svm->nested.vm_cr_msr; 2736 break; 2737 case MSR_IA32_SPEC_CTRL: 2738 if (!msr_info->host_initiated && 2739 !guest_has_spec_ctrl_msr(vcpu)) 2740 return 1; 2741 2742 if (boot_cpu_has(X86_FEATURE_V_SPEC_CTRL)) 2743 msr_info->data = svm->vmcb->save.spec_ctrl; 2744 else 2745 msr_info->data = svm->spec_ctrl; 2746 break; 2747 case MSR_AMD64_VIRT_SPEC_CTRL: 2748 if (!msr_info->host_initiated && 2749 !guest_cpu_cap_has(vcpu, X86_FEATURE_VIRT_SSBD)) 2750 return 1; 2751 2752 msr_info->data = svm->virt_spec_ctrl; 2753 break; 2754 case MSR_F15H_IC_CFG: { 2755 2756 int family, model; 2757 2758 family = guest_cpuid_family(vcpu); 2759 model = guest_cpuid_model(vcpu); 2760 2761 if (family < 0 || model < 0) 2762 return kvm_get_msr_common(vcpu, msr_info); 2763 2764 msr_info->data = 0; 2765 2766 if (family == 0x15 && 2767 (model >= 0x2 && model < 0x20)) 2768 msr_info->data = 0x1E; 2769 } 2770 break; 2771 case MSR_AMD64_DE_CFG: 2772 msr_info->data = svm->msr_decfg; 2773 break; 2774 default: 2775 return kvm_get_msr_common(vcpu, msr_info); 2776 } 2777 return 0; 2778 } 2779 2780 static int svm_complete_emulated_msr(struct kvm_vcpu *vcpu, int err) 2781 { 2782 struct vcpu_svm *svm = to_svm(vcpu); 2783 if (!err || !sev_es_guest(vcpu->kvm) || WARN_ON_ONCE(!svm->sev_es.ghcb)) 2784 return kvm_complete_insn_gp(vcpu, err); 2785 2786 svm_vmgexit_inject_exception(svm, X86_TRAP_GP); 2787 return 1; 2788 } 2789 2790 static int svm_set_vm_cr(struct kvm_vcpu *vcpu, u64 data) 2791 { 2792 struct vcpu_svm *svm = to_svm(vcpu); 2793 int svm_dis, chg_mask; 2794 2795 if (data & ~SVM_VM_CR_VALID_MASK) 2796 return 1; 2797 2798 chg_mask = SVM_VM_CR_VALID_MASK; 2799 2800 if (svm->nested.vm_cr_msr & SVM_VM_CR_SVM_DIS_MASK) 2801 chg_mask &= ~(SVM_VM_CR_SVM_LOCK_MASK | SVM_VM_CR_SVM_DIS_MASK); 2802 2803 svm->nested.vm_cr_msr &= ~chg_mask; 2804 svm->nested.vm_cr_msr |= (data & chg_mask); 2805 2806 svm_dis = svm->nested.vm_cr_msr & SVM_VM_CR_SVM_DIS_MASK; 2807 2808 /* check for svm_disable while efer.svme is set */ 2809 if (svm_dis && (vcpu->arch.efer & EFER_SVME)) 2810 return 1; 2811 2812 return 0; 2813 } 2814 2815 static int svm_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr) 2816 { 2817 struct vcpu_svm *svm = to_svm(vcpu); 2818 int ret = 0; 2819 2820 u32 ecx = msr->index; 2821 u64 data = msr->data; 2822 2823 if (sev_es_prevent_msr_access(vcpu, msr)) 2824 return vcpu->kvm->arch.has_protected_state ? -EINVAL : 0; 2825 2826 switch (ecx) { 2827 case MSR_AMD64_TSC_RATIO: 2828 2829 if (!guest_cpu_cap_has(vcpu, X86_FEATURE_TSCRATEMSR)) { 2830 2831 if (!msr->host_initiated) 2832 return 1; 2833 /* 2834 * In case TSC scaling is not enabled, always 2835 * leave this MSR at the default value. 2836 * 2837 * Due to bug in qemu 6.2.0, it would try to set 2838 * this msr to 0 if tsc scaling is not enabled. 2839 * Ignore this value as well. 2840 */ 2841 if (data != 0 && data != svm->tsc_ratio_msr) 2842 return 1; 2843 break; 2844 } 2845 2846 if (data & SVM_TSC_RATIO_RSVD) 2847 return 1; 2848 2849 svm->tsc_ratio_msr = data; 2850 2851 if (guest_cpu_cap_has(vcpu, X86_FEATURE_TSCRATEMSR) && 2852 is_guest_mode(vcpu)) 2853 nested_svm_update_tsc_ratio_msr(vcpu); 2854 2855 break; 2856 case MSR_IA32_CR_PAT: 2857 ret = kvm_set_msr_common(vcpu, msr); 2858 if (ret) 2859 break; 2860 2861 svm->vmcb01.ptr->save.g_pat = data; 2862 if (is_guest_mode(vcpu)) 2863 nested_vmcb02_compute_g_pat(svm); 2864 vmcb_mark_dirty(svm->vmcb, VMCB_NPT); 2865 break; 2866 case MSR_IA32_SPEC_CTRL: 2867 if (!msr->host_initiated && 2868 !guest_has_spec_ctrl_msr(vcpu)) 2869 return 1; 2870 2871 if (kvm_spec_ctrl_test_value(data)) 2872 return 1; 2873 2874 if (boot_cpu_has(X86_FEATURE_V_SPEC_CTRL)) 2875 svm->vmcb->save.spec_ctrl = data; 2876 else 2877 svm->spec_ctrl = data; 2878 if (!data) 2879 break; 2880 2881 /* 2882 * For non-nested: 2883 * When it's written (to non-zero) for the first time, pass 2884 * it through. 2885 * 2886 * For nested: 2887 * The handling of the MSR bitmap for L2 guests is done in 2888 * nested_svm_merge_msrpm(). 2889 * We update the L1 MSR bit as well since it will end up 2890 * touching the MSR anyway now. 2891 */ 2892 svm_disable_intercept_for_msr(vcpu, MSR_IA32_SPEC_CTRL, MSR_TYPE_RW); 2893 break; 2894 case MSR_AMD64_VIRT_SPEC_CTRL: 2895 if (!msr->host_initiated && 2896 !guest_cpu_cap_has(vcpu, X86_FEATURE_VIRT_SSBD)) 2897 return 1; 2898 2899 if (data & ~SPEC_CTRL_SSBD) 2900 return 1; 2901 2902 svm->virt_spec_ctrl = data; 2903 break; 2904 case MSR_STAR: 2905 svm->vmcb01.ptr->save.star = data; 2906 break; 2907 #ifdef CONFIG_X86_64 2908 case MSR_LSTAR: 2909 svm->vmcb01.ptr->save.lstar = data; 2910 break; 2911 case MSR_CSTAR: 2912 svm->vmcb01.ptr->save.cstar = data; 2913 break; 2914 case MSR_GS_BASE: 2915 svm->vmcb01.ptr->save.gs.base = data; 2916 break; 2917 case MSR_FS_BASE: 2918 svm->vmcb01.ptr->save.fs.base = data; 2919 break; 2920 case MSR_KERNEL_GS_BASE: 2921 svm->vmcb01.ptr->save.kernel_gs_base = data; 2922 break; 2923 case MSR_SYSCALL_MASK: 2924 svm->vmcb01.ptr->save.sfmask = data; 2925 break; 2926 #endif 2927 case MSR_IA32_SYSENTER_CS: 2928 svm->vmcb01.ptr->save.sysenter_cs = data; 2929 break; 2930 case MSR_IA32_SYSENTER_EIP: 2931 svm->vmcb01.ptr->save.sysenter_eip = (u32)data; 2932 /* 2933 * We only intercept the MSR_IA32_SYSENTER_{EIP|ESP} msrs 2934 * when we spoof an Intel vendor ID (for cross vendor migration). 2935 * In this case we use this intercept to track the high 2936 * 32 bit part of these msrs to support Intel's 2937 * implementation of SYSENTER/SYSEXIT. 2938 */ 2939 svm->sysenter_eip_hi = guest_cpuid_is_intel_compatible(vcpu) ? (data >> 32) : 0; 2940 break; 2941 case MSR_IA32_SYSENTER_ESP: 2942 svm->vmcb01.ptr->save.sysenter_esp = (u32)data; 2943 svm->sysenter_esp_hi = guest_cpuid_is_intel_compatible(vcpu) ? (data >> 32) : 0; 2944 break; 2945 case MSR_IA32_S_CET: 2946 svm->vmcb->save.s_cet = data; 2947 vmcb_mark_dirty(svm->vmcb01.ptr, VMCB_CET); 2948 break; 2949 case MSR_IA32_INT_SSP_TAB: 2950 svm->vmcb->save.isst_addr = data; 2951 vmcb_mark_dirty(svm->vmcb01.ptr, VMCB_CET); 2952 break; 2953 case MSR_KVM_INTERNAL_GUEST_SSP: 2954 svm->vmcb->save.ssp = data; 2955 vmcb_mark_dirty(svm->vmcb01.ptr, VMCB_CET); 2956 break; 2957 case MSR_TSC_AUX: 2958 /* 2959 * TSC_AUX is always virtualized for SEV-ES guests when the 2960 * feature is available. The user return MSR support is not 2961 * required in this case because TSC_AUX is restored on #VMEXIT 2962 * from the host save area. 2963 */ 2964 if (boot_cpu_has(X86_FEATURE_V_TSC_AUX) && sev_es_guest(vcpu->kvm)) 2965 break; 2966 2967 /* 2968 * TSC_AUX is usually changed only during boot and never read 2969 * directly. Intercept TSC_AUX and switch it via user return. 2970 */ 2971 preempt_disable(); 2972 ret = kvm_set_user_return_msr(tsc_aux_uret_slot, data, -1ull); 2973 preempt_enable(); 2974 if (ret) 2975 break; 2976 2977 svm->tsc_aux = data; 2978 break; 2979 case MSR_IA32_DEBUGCTLMSR: 2980 if (!lbrv) { 2981 kvm_pr_unimpl_wrmsr(vcpu, ecx, data); 2982 break; 2983 } 2984 2985 /* 2986 * Suppress BTF as KVM doesn't virtualize BTF, but there's no 2987 * way to communicate lack of support to the guest. 2988 */ 2989 if (data & DEBUGCTLMSR_BTF) { 2990 kvm_pr_unimpl_wrmsr(vcpu, MSR_IA32_DEBUGCTLMSR, data); 2991 data &= ~DEBUGCTLMSR_BTF; 2992 } 2993 2994 if (data & DEBUGCTL_RESERVED_BITS) 2995 return 1; 2996 2997 if (svm->vmcb->save.dbgctl == data) 2998 break; 2999 3000 svm->vmcb->save.dbgctl = data; 3001 vmcb_mark_dirty(svm->vmcb, VMCB_LBR); 3002 svm_update_lbrv(vcpu); 3003 break; 3004 case MSR_VM_HSAVE_PA: 3005 /* 3006 * Old kernels did not validate the value written to 3007 * MSR_VM_HSAVE_PA. Allow KVM_SET_MSR to set an invalid 3008 * value to allow live migrating buggy or malicious guests 3009 * originating from those kernels. 3010 */ 3011 if (!msr->host_initiated && !page_address_valid(vcpu, data)) 3012 return 1; 3013 3014 svm->nested.hsave_msr = data & PAGE_MASK; 3015 break; 3016 case MSR_VM_CR: 3017 return svm_set_vm_cr(vcpu, data); 3018 case MSR_VM_IGNNE: 3019 kvm_pr_unimpl_wrmsr(vcpu, ecx, data); 3020 break; 3021 case MSR_AMD64_DE_CFG: { 3022 u64 supported_de_cfg; 3023 3024 if (svm_get_feature_msr(ecx, &supported_de_cfg)) 3025 return 1; 3026 3027 if (data & ~supported_de_cfg) 3028 return 1; 3029 3030 svm->msr_decfg = data; 3031 break; 3032 } 3033 default: 3034 return kvm_set_msr_common(vcpu, msr); 3035 } 3036 return ret; 3037 } 3038 3039 static int msr_interception(struct kvm_vcpu *vcpu) 3040 { 3041 if (to_svm(vcpu)->vmcb->control.exit_info_1) 3042 return kvm_emulate_wrmsr(vcpu); 3043 else 3044 return kvm_emulate_rdmsr(vcpu); 3045 } 3046 3047 static int interrupt_window_interception(struct kvm_vcpu *vcpu) 3048 { 3049 kvm_make_request(KVM_REQ_EVENT, vcpu); 3050 svm_clear_vintr(to_svm(vcpu)); 3051 3052 /* 3053 * If not running nested, for AVIC, the only reason to end up here is ExtINTs. 3054 * In this case AVIC was temporarily disabled for 3055 * requesting the IRQ window and we have to re-enable it. 3056 * 3057 * If running nested, still remove the VM wide AVIC inhibit to 3058 * support case in which the interrupt window was requested when the 3059 * vCPU was not running nested. 3060 3061 * All vCPUs which run still run nested, will remain to have their 3062 * AVIC still inhibited due to per-cpu AVIC inhibition. 3063 */ 3064 kvm_clear_apicv_inhibit(vcpu->kvm, APICV_INHIBIT_REASON_IRQWIN); 3065 3066 ++vcpu->stat.irq_window_exits; 3067 return 1; 3068 } 3069 3070 static int pause_interception(struct kvm_vcpu *vcpu) 3071 { 3072 bool in_kernel; 3073 /* 3074 * CPL is not made available for an SEV-ES guest, therefore 3075 * vcpu->arch.preempted_in_kernel can never be true. Just 3076 * set in_kernel to false as well. 3077 */ 3078 in_kernel = !sev_es_guest(vcpu->kvm) && svm_get_cpl(vcpu) == 0; 3079 3080 grow_ple_window(vcpu); 3081 3082 kvm_vcpu_on_spin(vcpu, in_kernel); 3083 return kvm_skip_emulated_instruction(vcpu); 3084 } 3085 3086 static int invpcid_interception(struct kvm_vcpu *vcpu) 3087 { 3088 struct vcpu_svm *svm = to_svm(vcpu); 3089 unsigned long type; 3090 gva_t gva; 3091 3092 if (!guest_cpu_cap_has(vcpu, X86_FEATURE_INVPCID)) { 3093 kvm_queue_exception(vcpu, UD_VECTOR); 3094 return 1; 3095 } 3096 3097 /* 3098 * For an INVPCID intercept: 3099 * EXITINFO1 provides the linear address of the memory operand. 3100 * EXITINFO2 provides the contents of the register operand. 3101 */ 3102 type = svm->vmcb->control.exit_info_2; 3103 gva = svm->vmcb->control.exit_info_1; 3104 3105 /* 3106 * FIXME: Perform segment checks for 32-bit mode, and inject #SS if the 3107 * stack segment is used. The intercept takes priority over all 3108 * #GP checks except CPL>0, but somehow still generates a linear 3109 * address? The APM is sorely lacking. 3110 */ 3111 if (is_noncanonical_address(gva, vcpu, 0)) { 3112 kvm_queue_exception_e(vcpu, GP_VECTOR, 0); 3113 return 1; 3114 } 3115 3116 return kvm_handle_invpcid(vcpu, type, gva); 3117 } 3118 3119 static inline int complete_userspace_buslock(struct kvm_vcpu *vcpu) 3120 { 3121 struct vcpu_svm *svm = to_svm(vcpu); 3122 3123 /* 3124 * If userspace has NOT changed RIP, then KVM's ABI is to let the guest 3125 * execute the bus-locking instruction. Set the bus lock counter to '1' 3126 * to effectively step past the bus lock. 3127 */ 3128 if (kvm_is_linear_rip(vcpu, vcpu->arch.cui_linear_rip)) 3129 svm->vmcb->control.bus_lock_counter = 1; 3130 3131 return 1; 3132 } 3133 3134 static int bus_lock_exit(struct kvm_vcpu *vcpu) 3135 { 3136 struct vcpu_svm *svm = to_svm(vcpu); 3137 3138 vcpu->run->exit_reason = KVM_EXIT_X86_BUS_LOCK; 3139 vcpu->run->flags |= KVM_RUN_X86_BUS_LOCK; 3140 3141 vcpu->arch.cui_linear_rip = kvm_get_linear_rip(vcpu); 3142 vcpu->arch.complete_userspace_io = complete_userspace_buslock; 3143 3144 if (is_guest_mode(vcpu)) 3145 svm->nested.ctl.bus_lock_rip = vcpu->arch.cui_linear_rip; 3146 3147 return 0; 3148 } 3149 3150 static int (*const svm_exit_handlers[])(struct kvm_vcpu *vcpu) = { 3151 [SVM_EXIT_READ_CR0] = cr_interception, 3152 [SVM_EXIT_READ_CR3] = cr_interception, 3153 [SVM_EXIT_READ_CR4] = cr_interception, 3154 [SVM_EXIT_READ_CR8] = cr_interception, 3155 [SVM_EXIT_CR0_SEL_WRITE] = cr_interception, 3156 [SVM_EXIT_WRITE_CR0] = cr_interception, 3157 [SVM_EXIT_WRITE_CR3] = cr_interception, 3158 [SVM_EXIT_WRITE_CR4] = cr_interception, 3159 [SVM_EXIT_WRITE_CR8] = cr8_write_interception, 3160 [SVM_EXIT_READ_DR0] = dr_interception, 3161 [SVM_EXIT_READ_DR1] = dr_interception, 3162 [SVM_EXIT_READ_DR2] = dr_interception, 3163 [SVM_EXIT_READ_DR3] = dr_interception, 3164 [SVM_EXIT_READ_DR4] = dr_interception, 3165 [SVM_EXIT_READ_DR5] = dr_interception, 3166 [SVM_EXIT_READ_DR6] = dr_interception, 3167 [SVM_EXIT_READ_DR7] = dr_interception, 3168 [SVM_EXIT_WRITE_DR0] = dr_interception, 3169 [SVM_EXIT_WRITE_DR1] = dr_interception, 3170 [SVM_EXIT_WRITE_DR2] = dr_interception, 3171 [SVM_EXIT_WRITE_DR3] = dr_interception, 3172 [SVM_EXIT_WRITE_DR4] = dr_interception, 3173 [SVM_EXIT_WRITE_DR5] = dr_interception, 3174 [SVM_EXIT_WRITE_DR6] = dr_interception, 3175 [SVM_EXIT_WRITE_DR7] = dr_interception, 3176 [SVM_EXIT_EXCP_BASE + DB_VECTOR] = db_interception, 3177 [SVM_EXIT_EXCP_BASE + BP_VECTOR] = bp_interception, 3178 [SVM_EXIT_EXCP_BASE + UD_VECTOR] = ud_interception, 3179 [SVM_EXIT_EXCP_BASE + PF_VECTOR] = pf_interception, 3180 [SVM_EXIT_EXCP_BASE + MC_VECTOR] = mc_interception, 3181 [SVM_EXIT_EXCP_BASE + AC_VECTOR] = ac_interception, 3182 [SVM_EXIT_EXCP_BASE + GP_VECTOR] = gp_interception, 3183 [SVM_EXIT_INTR] = intr_interception, 3184 [SVM_EXIT_NMI] = nmi_interception, 3185 [SVM_EXIT_SMI] = smi_interception, 3186 [SVM_EXIT_VINTR] = interrupt_window_interception, 3187 [SVM_EXIT_RDPMC] = kvm_emulate_rdpmc, 3188 [SVM_EXIT_CPUID] = kvm_emulate_cpuid, 3189 [SVM_EXIT_IRET] = iret_interception, 3190 [SVM_EXIT_INVD] = kvm_emulate_invd, 3191 [SVM_EXIT_PAUSE] = pause_interception, 3192 [SVM_EXIT_HLT] = kvm_emulate_halt, 3193 [SVM_EXIT_INVLPG] = invlpg_interception, 3194 [SVM_EXIT_INVLPGA] = invlpga_interception, 3195 [SVM_EXIT_IOIO] = io_interception, 3196 [SVM_EXIT_MSR] = msr_interception, 3197 [SVM_EXIT_TASK_SWITCH] = task_switch_interception, 3198 [SVM_EXIT_SHUTDOWN] = shutdown_interception, 3199 [SVM_EXIT_VMRUN] = vmrun_interception, 3200 [SVM_EXIT_VMMCALL] = kvm_emulate_hypercall, 3201 [SVM_EXIT_VMLOAD] = vmload_interception, 3202 [SVM_EXIT_VMSAVE] = vmsave_interception, 3203 [SVM_EXIT_STGI] = stgi_interception, 3204 [SVM_EXIT_CLGI] = clgi_interception, 3205 [SVM_EXIT_SKINIT] = skinit_interception, 3206 [SVM_EXIT_RDTSCP] = kvm_handle_invalid_op, 3207 [SVM_EXIT_WBINVD] = kvm_emulate_wbinvd, 3208 [SVM_EXIT_MONITOR] = kvm_emulate_monitor, 3209 [SVM_EXIT_MWAIT] = kvm_emulate_mwait, 3210 [SVM_EXIT_XSETBV] = kvm_emulate_xsetbv, 3211 [SVM_EXIT_RDPRU] = kvm_handle_invalid_op, 3212 [SVM_EXIT_EFER_WRITE_TRAP] = efer_trap, 3213 [SVM_EXIT_CR0_WRITE_TRAP] = cr_trap, 3214 [SVM_EXIT_CR4_WRITE_TRAP] = cr_trap, 3215 [SVM_EXIT_CR8_WRITE_TRAP] = cr_trap, 3216 [SVM_EXIT_INVPCID] = invpcid_interception, 3217 [SVM_EXIT_IDLE_HLT] = kvm_emulate_halt, 3218 [SVM_EXIT_NPF] = npf_interception, 3219 [SVM_EXIT_BUS_LOCK] = bus_lock_exit, 3220 [SVM_EXIT_RSM] = rsm_interception, 3221 [SVM_EXIT_AVIC_INCOMPLETE_IPI] = avic_incomplete_ipi_interception, 3222 [SVM_EXIT_AVIC_UNACCELERATED_ACCESS] = avic_unaccelerated_access_interception, 3223 #ifdef CONFIG_KVM_AMD_SEV 3224 [SVM_EXIT_VMGEXIT] = sev_handle_vmgexit, 3225 #endif 3226 }; 3227 3228 static void dump_vmcb(struct kvm_vcpu *vcpu) 3229 { 3230 struct vcpu_svm *svm = to_svm(vcpu); 3231 struct vmcb_control_area *control = &svm->vmcb->control; 3232 struct vmcb_save_area *save = &svm->vmcb->save; 3233 struct vmcb_save_area *save01 = &svm->vmcb01.ptr->save; 3234 char *vm_type; 3235 3236 if (!dump_invalid_vmcb) { 3237 pr_warn_ratelimited("set kvm_amd.dump_invalid_vmcb=1 to dump internal KVM state.\n"); 3238 return; 3239 } 3240 3241 guard(mutex)(&vmcb_dump_mutex); 3242 3243 vm_type = sev_snp_guest(vcpu->kvm) ? "SEV-SNP" : 3244 sev_es_guest(vcpu->kvm) ? "SEV-ES" : 3245 sev_guest(vcpu->kvm) ? "SEV" : "SVM"; 3246 3247 pr_err("%s vCPU%u VMCB %p, last attempted VMRUN on CPU %d\n", 3248 vm_type, vcpu->vcpu_id, svm->current_vmcb->ptr, vcpu->arch.last_vmentry_cpu); 3249 pr_err("VMCB Control Area:\n"); 3250 pr_err("%-20s%04x\n", "cr_read:", control->intercepts[INTERCEPT_CR] & 0xffff); 3251 pr_err("%-20s%04x\n", "cr_write:", control->intercepts[INTERCEPT_CR] >> 16); 3252 pr_err("%-20s%04x\n", "dr_read:", control->intercepts[INTERCEPT_DR] & 0xffff); 3253 pr_err("%-20s%04x\n", "dr_write:", control->intercepts[INTERCEPT_DR] >> 16); 3254 pr_err("%-20s%08x\n", "exceptions:", control->intercepts[INTERCEPT_EXCEPTION]); 3255 pr_err("%-20s%08x %08x\n", "intercepts:", 3256 control->intercepts[INTERCEPT_WORD3], 3257 control->intercepts[INTERCEPT_WORD4]); 3258 pr_err("%-20s%d\n", "pause filter count:", control->pause_filter_count); 3259 pr_err("%-20s%d\n", "pause filter threshold:", 3260 control->pause_filter_thresh); 3261 pr_err("%-20s%016llx\n", "iopm_base_pa:", control->iopm_base_pa); 3262 pr_err("%-20s%016llx\n", "msrpm_base_pa:", control->msrpm_base_pa); 3263 pr_err("%-20s%016llx\n", "tsc_offset:", control->tsc_offset); 3264 pr_err("%-20s%d\n", "asid:", control->asid); 3265 pr_err("%-20s%d\n", "tlb_ctl:", control->tlb_ctl); 3266 pr_err("%-20s%08x\n", "int_ctl:", control->int_ctl); 3267 pr_err("%-20s%08x\n", "int_vector:", control->int_vector); 3268 pr_err("%-20s%08x\n", "int_state:", control->int_state); 3269 pr_err("%-20s%08x\n", "exit_code:", control->exit_code); 3270 pr_err("%-20s%016llx\n", "exit_info1:", control->exit_info_1); 3271 pr_err("%-20s%016llx\n", "exit_info2:", control->exit_info_2); 3272 pr_err("%-20s%08x\n", "exit_int_info:", control->exit_int_info); 3273 pr_err("%-20s%08x\n", "exit_int_info_err:", control->exit_int_info_err); 3274 pr_err("%-20s%lld\n", "nested_ctl:", control->nested_ctl); 3275 pr_err("%-20s%016llx\n", "nested_cr3:", control->nested_cr3); 3276 pr_err("%-20s%016llx\n", "avic_vapic_bar:", control->avic_vapic_bar); 3277 pr_err("%-20s%016llx\n", "ghcb:", control->ghcb_gpa); 3278 pr_err("%-20s%08x\n", "event_inj:", control->event_inj); 3279 pr_err("%-20s%08x\n", "event_inj_err:", control->event_inj_err); 3280 pr_err("%-20s%lld\n", "virt_ext:", control->virt_ext); 3281 pr_err("%-20s%016llx\n", "next_rip:", control->next_rip); 3282 pr_err("%-20s%016llx\n", "avic_backing_page:", control->avic_backing_page); 3283 pr_err("%-20s%016llx\n", "avic_logical_id:", control->avic_logical_id); 3284 pr_err("%-20s%016llx\n", "avic_physical_id:", control->avic_physical_id); 3285 pr_err("%-20s%016llx\n", "vmsa_pa:", control->vmsa_pa); 3286 pr_err("%-20s%016llx\n", "allowed_sev_features:", control->allowed_sev_features); 3287 pr_err("%-20s%016llx\n", "guest_sev_features:", control->guest_sev_features); 3288 3289 if (sev_es_guest(vcpu->kvm)) { 3290 save = sev_decrypt_vmsa(vcpu); 3291 if (!save) 3292 goto no_vmsa; 3293 3294 save01 = save; 3295 } 3296 3297 pr_err("VMCB State Save Area:\n"); 3298 pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n", 3299 "es:", 3300 save->es.selector, save->es.attrib, 3301 save->es.limit, save->es.base); 3302 pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n", 3303 "cs:", 3304 save->cs.selector, save->cs.attrib, 3305 save->cs.limit, save->cs.base); 3306 pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n", 3307 "ss:", 3308 save->ss.selector, save->ss.attrib, 3309 save->ss.limit, save->ss.base); 3310 pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n", 3311 "ds:", 3312 save->ds.selector, save->ds.attrib, 3313 save->ds.limit, save->ds.base); 3314 pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n", 3315 "fs:", 3316 save01->fs.selector, save01->fs.attrib, 3317 save01->fs.limit, save01->fs.base); 3318 pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n", 3319 "gs:", 3320 save01->gs.selector, save01->gs.attrib, 3321 save01->gs.limit, save01->gs.base); 3322 pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n", 3323 "gdtr:", 3324 save->gdtr.selector, save->gdtr.attrib, 3325 save->gdtr.limit, save->gdtr.base); 3326 pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n", 3327 "ldtr:", 3328 save01->ldtr.selector, save01->ldtr.attrib, 3329 save01->ldtr.limit, save01->ldtr.base); 3330 pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n", 3331 "idtr:", 3332 save->idtr.selector, save->idtr.attrib, 3333 save->idtr.limit, save->idtr.base); 3334 pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n", 3335 "tr:", 3336 save01->tr.selector, save01->tr.attrib, 3337 save01->tr.limit, save01->tr.base); 3338 pr_err("vmpl: %d cpl: %d efer: %016llx\n", 3339 save->vmpl, save->cpl, save->efer); 3340 pr_err("%-15s %016llx %-13s %016llx\n", 3341 "cr0:", save->cr0, "cr2:", save->cr2); 3342 pr_err("%-15s %016llx %-13s %016llx\n", 3343 "cr3:", save->cr3, "cr4:", save->cr4); 3344 pr_err("%-15s %016llx %-13s %016llx\n", 3345 "dr6:", save->dr6, "dr7:", save->dr7); 3346 pr_err("%-15s %016llx %-13s %016llx\n", 3347 "rip:", save->rip, "rflags:", save->rflags); 3348 pr_err("%-15s %016llx %-13s %016llx\n", 3349 "rsp:", save->rsp, "rax:", save->rax); 3350 pr_err("%-15s %016llx %-13s %016llx\n", 3351 "s_cet:", save->s_cet, "ssp:", save->ssp); 3352 pr_err("%-15s %016llx\n", 3353 "isst_addr:", save->isst_addr); 3354 pr_err("%-15s %016llx %-13s %016llx\n", 3355 "star:", save01->star, "lstar:", save01->lstar); 3356 pr_err("%-15s %016llx %-13s %016llx\n", 3357 "cstar:", save01->cstar, "sfmask:", save01->sfmask); 3358 pr_err("%-15s %016llx %-13s %016llx\n", 3359 "kernel_gs_base:", save01->kernel_gs_base, 3360 "sysenter_cs:", save01->sysenter_cs); 3361 pr_err("%-15s %016llx %-13s %016llx\n", 3362 "sysenter_esp:", save01->sysenter_esp, 3363 "sysenter_eip:", save01->sysenter_eip); 3364 pr_err("%-15s %016llx %-13s %016llx\n", 3365 "gpat:", save->g_pat, "dbgctl:", save->dbgctl); 3366 pr_err("%-15s %016llx %-13s %016llx\n", 3367 "br_from:", save->br_from, "br_to:", save->br_to); 3368 pr_err("%-15s %016llx %-13s %016llx\n", 3369 "excp_from:", save->last_excp_from, 3370 "excp_to:", save->last_excp_to); 3371 3372 if (sev_es_guest(vcpu->kvm)) { 3373 struct sev_es_save_area *vmsa = (struct sev_es_save_area *)save; 3374 3375 pr_err("%-15s %016llx\n", 3376 "sev_features", vmsa->sev_features); 3377 3378 pr_err("%-15s %016llx %-13s %016llx\n", 3379 "pl0_ssp:", vmsa->pl0_ssp, "pl1_ssp:", vmsa->pl1_ssp); 3380 pr_err("%-15s %016llx %-13s %016llx\n", 3381 "pl2_ssp:", vmsa->pl2_ssp, "pl3_ssp:", vmsa->pl3_ssp); 3382 pr_err("%-15s %016llx\n", 3383 "u_cet:", vmsa->u_cet); 3384 3385 pr_err("%-15s %016llx %-13s %016llx\n", 3386 "rax:", vmsa->rax, "rbx:", vmsa->rbx); 3387 pr_err("%-15s %016llx %-13s %016llx\n", 3388 "rcx:", vmsa->rcx, "rdx:", vmsa->rdx); 3389 pr_err("%-15s %016llx %-13s %016llx\n", 3390 "rsi:", vmsa->rsi, "rdi:", vmsa->rdi); 3391 pr_err("%-15s %016llx %-13s %016llx\n", 3392 "rbp:", vmsa->rbp, "rsp:", vmsa->rsp); 3393 pr_err("%-15s %016llx %-13s %016llx\n", 3394 "r8:", vmsa->r8, "r9:", vmsa->r9); 3395 pr_err("%-15s %016llx %-13s %016llx\n", 3396 "r10:", vmsa->r10, "r11:", vmsa->r11); 3397 pr_err("%-15s %016llx %-13s %016llx\n", 3398 "r12:", vmsa->r12, "r13:", vmsa->r13); 3399 pr_err("%-15s %016llx %-13s %016llx\n", 3400 "r14:", vmsa->r14, "r15:", vmsa->r15); 3401 pr_err("%-15s %016llx %-13s %016llx\n", 3402 "xcr0:", vmsa->xcr0, "xss:", vmsa->xss); 3403 } else { 3404 pr_err("%-15s %016llx %-13s %016lx\n", 3405 "rax:", save->rax, "rbx:", 3406 vcpu->arch.regs[VCPU_REGS_RBX]); 3407 pr_err("%-15s %016lx %-13s %016lx\n", 3408 "rcx:", vcpu->arch.regs[VCPU_REGS_RCX], 3409 "rdx:", vcpu->arch.regs[VCPU_REGS_RDX]); 3410 pr_err("%-15s %016lx %-13s %016lx\n", 3411 "rsi:", vcpu->arch.regs[VCPU_REGS_RSI], 3412 "rdi:", vcpu->arch.regs[VCPU_REGS_RDI]); 3413 pr_err("%-15s %016lx %-13s %016llx\n", 3414 "rbp:", vcpu->arch.regs[VCPU_REGS_RBP], 3415 "rsp:", save->rsp); 3416 #ifdef CONFIG_X86_64 3417 pr_err("%-15s %016lx %-13s %016lx\n", 3418 "r8:", vcpu->arch.regs[VCPU_REGS_R8], 3419 "r9:", vcpu->arch.regs[VCPU_REGS_R9]); 3420 pr_err("%-15s %016lx %-13s %016lx\n", 3421 "r10:", vcpu->arch.regs[VCPU_REGS_R10], 3422 "r11:", vcpu->arch.regs[VCPU_REGS_R11]); 3423 pr_err("%-15s %016lx %-13s %016lx\n", 3424 "r12:", vcpu->arch.regs[VCPU_REGS_R12], 3425 "r13:", vcpu->arch.regs[VCPU_REGS_R13]); 3426 pr_err("%-15s %016lx %-13s %016lx\n", 3427 "r14:", vcpu->arch.regs[VCPU_REGS_R14], 3428 "r15:", vcpu->arch.regs[VCPU_REGS_R15]); 3429 #endif 3430 } 3431 3432 no_vmsa: 3433 if (sev_es_guest(vcpu->kvm)) 3434 sev_free_decrypted_vmsa(vcpu, save); 3435 } 3436 3437 static bool svm_check_exit_valid(u64 exit_code) 3438 { 3439 return (exit_code < ARRAY_SIZE(svm_exit_handlers) && 3440 svm_exit_handlers[exit_code]); 3441 } 3442 3443 static int svm_handle_invalid_exit(struct kvm_vcpu *vcpu, u64 exit_code) 3444 { 3445 vcpu_unimpl(vcpu, "svm: unexpected exit reason 0x%llx\n", exit_code); 3446 dump_vmcb(vcpu); 3447 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR; 3448 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_UNEXPECTED_EXIT_REASON; 3449 vcpu->run->internal.ndata = 2; 3450 vcpu->run->internal.data[0] = exit_code; 3451 vcpu->run->internal.data[1] = vcpu->arch.last_vmentry_cpu; 3452 return 0; 3453 } 3454 3455 int svm_invoke_exit_handler(struct kvm_vcpu *vcpu, u64 exit_code) 3456 { 3457 if (!svm_check_exit_valid(exit_code)) 3458 return svm_handle_invalid_exit(vcpu, exit_code); 3459 3460 #ifdef CONFIG_MITIGATION_RETPOLINE 3461 if (exit_code == SVM_EXIT_MSR) 3462 return msr_interception(vcpu); 3463 else if (exit_code == SVM_EXIT_VINTR) 3464 return interrupt_window_interception(vcpu); 3465 else if (exit_code == SVM_EXIT_INTR) 3466 return intr_interception(vcpu); 3467 else if (exit_code == SVM_EXIT_HLT || exit_code == SVM_EXIT_IDLE_HLT) 3468 return kvm_emulate_halt(vcpu); 3469 else if (exit_code == SVM_EXIT_NPF) 3470 return npf_interception(vcpu); 3471 #ifdef CONFIG_KVM_AMD_SEV 3472 else if (exit_code == SVM_EXIT_VMGEXIT) 3473 return sev_handle_vmgexit(vcpu); 3474 #endif 3475 #endif 3476 return svm_exit_handlers[exit_code](vcpu); 3477 } 3478 3479 static void svm_get_exit_info(struct kvm_vcpu *vcpu, u32 *reason, 3480 u64 *info1, u64 *info2, 3481 u32 *intr_info, u32 *error_code) 3482 { 3483 struct vmcb_control_area *control = &to_svm(vcpu)->vmcb->control; 3484 3485 *reason = control->exit_code; 3486 *info1 = control->exit_info_1; 3487 *info2 = control->exit_info_2; 3488 *intr_info = control->exit_int_info; 3489 if ((*intr_info & SVM_EXITINTINFO_VALID) && 3490 (*intr_info & SVM_EXITINTINFO_VALID_ERR)) 3491 *error_code = control->exit_int_info_err; 3492 else 3493 *error_code = 0; 3494 } 3495 3496 static void svm_get_entry_info(struct kvm_vcpu *vcpu, u32 *intr_info, 3497 u32 *error_code) 3498 { 3499 struct vmcb_control_area *control = &to_svm(vcpu)->vmcb->control; 3500 3501 *intr_info = control->event_inj; 3502 3503 if ((*intr_info & SVM_EXITINTINFO_VALID) && 3504 (*intr_info & SVM_EXITINTINFO_VALID_ERR)) 3505 *error_code = control->event_inj_err; 3506 else 3507 *error_code = 0; 3508 3509 } 3510 3511 static int svm_handle_exit(struct kvm_vcpu *vcpu, fastpath_t exit_fastpath) 3512 { 3513 struct vcpu_svm *svm = to_svm(vcpu); 3514 struct kvm_run *kvm_run = vcpu->run; 3515 u32 exit_code = svm->vmcb->control.exit_code; 3516 3517 /* SEV-ES guests must use the CR write traps to track CR registers. */ 3518 if (!sev_es_guest(vcpu->kvm)) { 3519 if (!svm_is_intercept(svm, INTERCEPT_CR0_WRITE)) 3520 vcpu->arch.cr0 = svm->vmcb->save.cr0; 3521 if (npt_enabled) 3522 vcpu->arch.cr3 = svm->vmcb->save.cr3; 3523 } 3524 3525 if (is_guest_mode(vcpu)) { 3526 int vmexit; 3527 3528 trace_kvm_nested_vmexit(vcpu, KVM_ISA_SVM); 3529 3530 vmexit = nested_svm_exit_special(svm); 3531 3532 if (vmexit == NESTED_EXIT_CONTINUE) 3533 vmexit = nested_svm_exit_handled(svm); 3534 3535 if (vmexit == NESTED_EXIT_DONE) 3536 return 1; 3537 } 3538 3539 if (svm->vmcb->control.exit_code == SVM_EXIT_ERR) { 3540 kvm_run->exit_reason = KVM_EXIT_FAIL_ENTRY; 3541 kvm_run->fail_entry.hardware_entry_failure_reason 3542 = svm->vmcb->control.exit_code; 3543 kvm_run->fail_entry.cpu = vcpu->arch.last_vmentry_cpu; 3544 dump_vmcb(vcpu); 3545 return 0; 3546 } 3547 3548 if (exit_fastpath != EXIT_FASTPATH_NONE) 3549 return 1; 3550 3551 return svm_invoke_exit_handler(vcpu, exit_code); 3552 } 3553 3554 static int pre_svm_run(struct kvm_vcpu *vcpu) 3555 { 3556 struct svm_cpu_data *sd = per_cpu_ptr(&svm_data, vcpu->cpu); 3557 struct vcpu_svm *svm = to_svm(vcpu); 3558 3559 /* 3560 * If the previous vmrun of the vmcb occurred on a different physical 3561 * cpu, then mark the vmcb dirty and assign a new asid. Hardware's 3562 * vmcb clean bits are per logical CPU, as are KVM's asid assignments. 3563 */ 3564 if (unlikely(svm->current_vmcb->cpu != vcpu->cpu)) { 3565 svm->current_vmcb->asid_generation = 0; 3566 vmcb_mark_all_dirty(svm->vmcb); 3567 svm->current_vmcb->cpu = vcpu->cpu; 3568 } 3569 3570 if (sev_guest(vcpu->kvm)) 3571 return pre_sev_run(svm, vcpu->cpu); 3572 3573 /* FIXME: handle wraparound of asid_generation */ 3574 if (svm->current_vmcb->asid_generation != sd->asid_generation) 3575 new_asid(svm, sd); 3576 3577 return 0; 3578 } 3579 3580 static void svm_inject_nmi(struct kvm_vcpu *vcpu) 3581 { 3582 struct vcpu_svm *svm = to_svm(vcpu); 3583 3584 svm->vmcb->control.event_inj = SVM_EVTINJ_VALID | SVM_EVTINJ_TYPE_NMI; 3585 3586 if (svm->nmi_l1_to_l2) 3587 return; 3588 3589 /* 3590 * No need to manually track NMI masking when vNMI is enabled, hardware 3591 * automatically sets V_NMI_BLOCKING_MASK as appropriate, including the 3592 * case where software directly injects an NMI. 3593 */ 3594 if (!is_vnmi_enabled(svm)) { 3595 svm->nmi_masked = true; 3596 svm_set_iret_intercept(svm); 3597 } 3598 ++vcpu->stat.nmi_injections; 3599 } 3600 3601 static bool svm_is_vnmi_pending(struct kvm_vcpu *vcpu) 3602 { 3603 struct vcpu_svm *svm = to_svm(vcpu); 3604 3605 if (!is_vnmi_enabled(svm)) 3606 return false; 3607 3608 return !!(svm->vmcb->control.int_ctl & V_NMI_PENDING_MASK); 3609 } 3610 3611 static bool svm_set_vnmi_pending(struct kvm_vcpu *vcpu) 3612 { 3613 struct vcpu_svm *svm = to_svm(vcpu); 3614 3615 if (!is_vnmi_enabled(svm)) 3616 return false; 3617 3618 if (svm->vmcb->control.int_ctl & V_NMI_PENDING_MASK) 3619 return false; 3620 3621 svm->vmcb->control.int_ctl |= V_NMI_PENDING_MASK; 3622 vmcb_mark_dirty(svm->vmcb, VMCB_INTR); 3623 3624 /* 3625 * Because the pending NMI is serviced by hardware, KVM can't know when 3626 * the NMI is "injected", but for all intents and purposes, passing the 3627 * NMI off to hardware counts as injection. 3628 */ 3629 ++vcpu->stat.nmi_injections; 3630 3631 return true; 3632 } 3633 3634 static void svm_inject_irq(struct kvm_vcpu *vcpu, bool reinjected) 3635 { 3636 struct vcpu_svm *svm = to_svm(vcpu); 3637 u32 type; 3638 3639 if (vcpu->arch.interrupt.soft) { 3640 if (svm_update_soft_interrupt_rip(vcpu)) 3641 return; 3642 3643 type = SVM_EVTINJ_TYPE_SOFT; 3644 } else { 3645 type = SVM_EVTINJ_TYPE_INTR; 3646 } 3647 3648 trace_kvm_inj_virq(vcpu->arch.interrupt.nr, 3649 vcpu->arch.interrupt.soft, reinjected); 3650 ++vcpu->stat.irq_injections; 3651 3652 svm->vmcb->control.event_inj = vcpu->arch.interrupt.nr | 3653 SVM_EVTINJ_VALID | type; 3654 } 3655 3656 void svm_complete_interrupt_delivery(struct kvm_vcpu *vcpu, int delivery_mode, 3657 int trig_mode, int vector) 3658 { 3659 /* 3660 * apic->apicv_active must be read after vcpu->mode. 3661 * Pairs with smp_store_release in vcpu_enter_guest. 3662 */ 3663 bool in_guest_mode = (smp_load_acquire(&vcpu->mode) == IN_GUEST_MODE); 3664 3665 /* Note, this is called iff the local APIC is in-kernel. */ 3666 if (!READ_ONCE(vcpu->arch.apic->apicv_active)) { 3667 /* Process the interrupt via kvm_check_and_inject_events(). */ 3668 kvm_make_request(KVM_REQ_EVENT, vcpu); 3669 kvm_vcpu_kick(vcpu); 3670 return; 3671 } 3672 3673 trace_kvm_apicv_accept_irq(vcpu->vcpu_id, delivery_mode, trig_mode, vector); 3674 if (in_guest_mode) { 3675 /* 3676 * Signal the doorbell to tell hardware to inject the IRQ. If 3677 * the vCPU exits the guest before the doorbell chimes, hardware 3678 * will automatically process AVIC interrupts at the next VMRUN. 3679 */ 3680 avic_ring_doorbell(vcpu); 3681 } else { 3682 /* 3683 * Wake the vCPU if it was blocking. KVM will then detect the 3684 * pending IRQ when checking if the vCPU has a wake event. 3685 */ 3686 kvm_vcpu_wake_up(vcpu); 3687 } 3688 } 3689 3690 static void svm_deliver_interrupt(struct kvm_lapic *apic, int delivery_mode, 3691 int trig_mode, int vector) 3692 { 3693 kvm_lapic_set_irr(vector, apic); 3694 3695 /* 3696 * Pairs with the smp_mb_*() after setting vcpu->guest_mode in 3697 * vcpu_enter_guest() to ensure the write to the vIRR is ordered before 3698 * the read of guest_mode. This guarantees that either VMRUN will see 3699 * and process the new vIRR entry, or that svm_complete_interrupt_delivery 3700 * will signal the doorbell if the CPU has already entered the guest. 3701 */ 3702 smp_mb__after_atomic(); 3703 svm_complete_interrupt_delivery(apic->vcpu, delivery_mode, trig_mode, vector); 3704 } 3705 3706 static void svm_update_cr8_intercept(struct kvm_vcpu *vcpu, int tpr, int irr) 3707 { 3708 struct vcpu_svm *svm = to_svm(vcpu); 3709 3710 /* 3711 * SEV-ES guests must always keep the CR intercepts cleared. CR 3712 * tracking is done using the CR write traps. 3713 */ 3714 if (sev_es_guest(vcpu->kvm)) 3715 return; 3716 3717 if (nested_svm_virtualize_tpr(vcpu)) 3718 return; 3719 3720 svm_clr_intercept(svm, INTERCEPT_CR8_WRITE); 3721 3722 if (irr == -1) 3723 return; 3724 3725 if (tpr >= irr) 3726 svm_set_intercept(svm, INTERCEPT_CR8_WRITE); 3727 } 3728 3729 static bool svm_get_nmi_mask(struct kvm_vcpu *vcpu) 3730 { 3731 struct vcpu_svm *svm = to_svm(vcpu); 3732 3733 if (is_vnmi_enabled(svm)) 3734 return svm->vmcb->control.int_ctl & V_NMI_BLOCKING_MASK; 3735 else 3736 return svm->nmi_masked; 3737 } 3738 3739 static void svm_set_nmi_mask(struct kvm_vcpu *vcpu, bool masked) 3740 { 3741 struct vcpu_svm *svm = to_svm(vcpu); 3742 3743 if (is_vnmi_enabled(svm)) { 3744 if (masked) 3745 svm->vmcb->control.int_ctl |= V_NMI_BLOCKING_MASK; 3746 else 3747 svm->vmcb->control.int_ctl &= ~V_NMI_BLOCKING_MASK; 3748 3749 } else { 3750 svm->nmi_masked = masked; 3751 if (masked) 3752 svm_set_iret_intercept(svm); 3753 else 3754 svm_clr_iret_intercept(svm); 3755 } 3756 } 3757 3758 bool svm_nmi_blocked(struct kvm_vcpu *vcpu) 3759 { 3760 struct vcpu_svm *svm = to_svm(vcpu); 3761 struct vmcb *vmcb = svm->vmcb; 3762 3763 if (!gif_set(svm)) 3764 return true; 3765 3766 if (is_guest_mode(vcpu) && nested_exit_on_nmi(svm)) 3767 return false; 3768 3769 if (svm_get_nmi_mask(vcpu)) 3770 return true; 3771 3772 return vmcb->control.int_state & SVM_INTERRUPT_SHADOW_MASK; 3773 } 3774 3775 static int svm_nmi_allowed(struct kvm_vcpu *vcpu, bool for_injection) 3776 { 3777 struct vcpu_svm *svm = to_svm(vcpu); 3778 if (svm->nested.nested_run_pending) 3779 return -EBUSY; 3780 3781 if (svm_nmi_blocked(vcpu)) 3782 return 0; 3783 3784 /* An NMI must not be injected into L2 if it's supposed to VM-Exit. */ 3785 if (for_injection && is_guest_mode(vcpu) && nested_exit_on_nmi(svm)) 3786 return -EBUSY; 3787 return 1; 3788 } 3789 3790 bool svm_interrupt_blocked(struct kvm_vcpu *vcpu) 3791 { 3792 struct vcpu_svm *svm = to_svm(vcpu); 3793 struct vmcb *vmcb = svm->vmcb; 3794 3795 if (!gif_set(svm)) 3796 return true; 3797 3798 if (is_guest_mode(vcpu)) { 3799 /* As long as interrupts are being delivered... */ 3800 if ((svm->nested.ctl.int_ctl & V_INTR_MASKING_MASK) 3801 ? !(svm->vmcb01.ptr->save.rflags & X86_EFLAGS_IF) 3802 : !(kvm_get_rflags(vcpu) & X86_EFLAGS_IF)) 3803 return true; 3804 3805 /* ... vmexits aren't blocked by the interrupt shadow */ 3806 if (nested_exit_on_intr(svm)) 3807 return false; 3808 } else { 3809 if (!svm_get_if_flag(vcpu)) 3810 return true; 3811 } 3812 3813 return (vmcb->control.int_state & SVM_INTERRUPT_SHADOW_MASK); 3814 } 3815 3816 static int svm_interrupt_allowed(struct kvm_vcpu *vcpu, bool for_injection) 3817 { 3818 struct vcpu_svm *svm = to_svm(vcpu); 3819 3820 if (svm->nested.nested_run_pending) 3821 return -EBUSY; 3822 3823 if (svm_interrupt_blocked(vcpu)) 3824 return 0; 3825 3826 /* 3827 * An IRQ must not be injected into L2 if it's supposed to VM-Exit, 3828 * e.g. if the IRQ arrived asynchronously after checking nested events. 3829 */ 3830 if (for_injection && is_guest_mode(vcpu) && nested_exit_on_intr(svm)) 3831 return -EBUSY; 3832 3833 return 1; 3834 } 3835 3836 static void svm_enable_irq_window(struct kvm_vcpu *vcpu) 3837 { 3838 struct vcpu_svm *svm = to_svm(vcpu); 3839 3840 /* 3841 * In case GIF=0 we can't rely on the CPU to tell us when GIF becomes 3842 * 1, because that's a separate STGI/VMRUN intercept. The next time we 3843 * get that intercept, this function will be called again though and 3844 * we'll get the vintr intercept. However, if the vGIF feature is 3845 * enabled, the STGI interception will not occur. Enable the irq 3846 * window under the assumption that the hardware will set the GIF. 3847 */ 3848 if (vgif || gif_set(svm)) { 3849 /* 3850 * IRQ window is not needed when AVIC is enabled, 3851 * unless we have pending ExtINT since it cannot be injected 3852 * via AVIC. In such case, KVM needs to temporarily disable AVIC, 3853 * and fallback to injecting IRQ via V_IRQ. 3854 * 3855 * If running nested, AVIC is already locally inhibited 3856 * on this vCPU, therefore there is no need to request 3857 * the VM wide AVIC inhibition. 3858 */ 3859 if (!is_guest_mode(vcpu)) 3860 kvm_set_apicv_inhibit(vcpu->kvm, APICV_INHIBIT_REASON_IRQWIN); 3861 3862 svm_set_vintr(svm); 3863 } 3864 } 3865 3866 static void svm_enable_nmi_window(struct kvm_vcpu *vcpu) 3867 { 3868 struct vcpu_svm *svm = to_svm(vcpu); 3869 3870 /* 3871 * If NMIs are outright masked, i.e. the vCPU is already handling an 3872 * NMI, and KVM has not yet intercepted an IRET, then there is nothing 3873 * more to do at this time as KVM has already enabled IRET intercepts. 3874 * If KVM has already intercepted IRET, then single-step over the IRET, 3875 * as NMIs aren't architecturally unmasked until the IRET completes. 3876 * 3877 * If vNMI is enabled, KVM should never request an NMI window if NMIs 3878 * are masked, as KVM allows at most one to-be-injected NMI and one 3879 * pending NMI. If two NMIs arrive simultaneously, KVM will inject one 3880 * NMI and set V_NMI_PENDING for the other, but if and only if NMIs are 3881 * unmasked. KVM _will_ request an NMI window in some situations, e.g. 3882 * if the vCPU is in an STI shadow or if GIF=0, KVM can't immediately 3883 * inject the NMI. In those situations, KVM needs to single-step over 3884 * the STI shadow or intercept STGI. 3885 */ 3886 if (svm_get_nmi_mask(vcpu)) { 3887 WARN_ON_ONCE(is_vnmi_enabled(svm)); 3888 3889 if (!svm->awaiting_iret_completion) 3890 return; /* IRET will cause a vm exit */ 3891 } 3892 3893 /* 3894 * SEV-ES guests are responsible for signaling when a vCPU is ready to 3895 * receive a new NMI, as SEV-ES guests can't be single-stepped, i.e. 3896 * KVM can't intercept and single-step IRET to detect when NMIs are 3897 * unblocked (architecturally speaking). See SVM_VMGEXIT_NMI_COMPLETE. 3898 * 3899 * Note, GIF is guaranteed to be '1' for SEV-ES guests as hardware 3900 * ignores SEV-ES guest writes to EFER.SVME *and* CLGI/STGI are not 3901 * supported NAEs in the GHCB protocol. 3902 */ 3903 if (sev_es_guest(vcpu->kvm)) 3904 return; 3905 3906 if (!gif_set(svm)) { 3907 if (vgif) 3908 svm_set_intercept(svm, INTERCEPT_STGI); 3909 return; /* STGI will cause a vm exit */ 3910 } 3911 3912 /* 3913 * Something prevents NMI from been injected. Single step over possible 3914 * problem (IRET or exception injection or interrupt shadow) 3915 */ 3916 svm->nmi_singlestep_guest_rflags = svm_get_rflags(vcpu); 3917 svm->nmi_singlestep = true; 3918 svm->vmcb->save.rflags |= (X86_EFLAGS_TF | X86_EFLAGS_RF); 3919 } 3920 3921 static void svm_flush_tlb_asid(struct kvm_vcpu *vcpu) 3922 { 3923 struct vcpu_svm *svm = to_svm(vcpu); 3924 3925 /* 3926 * Unlike VMX, SVM doesn't provide a way to flush only NPT TLB entries. 3927 * A TLB flush for the current ASID flushes both "host" and "guest" TLB 3928 * entries, and thus is a superset of Hyper-V's fine grained flushing. 3929 */ 3930 kvm_hv_vcpu_purge_flush_tlb(vcpu); 3931 3932 /* 3933 * Flush only the current ASID even if the TLB flush was invoked via 3934 * kvm_flush_remote_tlbs(). Although flushing remote TLBs requires all 3935 * ASIDs to be flushed, KVM uses a single ASID for L1 and L2, and 3936 * unconditionally does a TLB flush on both nested VM-Enter and nested 3937 * VM-Exit (via kvm_mmu_reset_context()). 3938 */ 3939 if (static_cpu_has(X86_FEATURE_FLUSHBYASID)) 3940 svm->vmcb->control.tlb_ctl = TLB_CONTROL_FLUSH_ASID; 3941 else 3942 svm->current_vmcb->asid_generation--; 3943 } 3944 3945 static void svm_flush_tlb_current(struct kvm_vcpu *vcpu) 3946 { 3947 hpa_t root_tdp = vcpu->arch.mmu->root.hpa; 3948 3949 /* 3950 * When running on Hyper-V with EnlightenedNptTlb enabled, explicitly 3951 * flush the NPT mappings via hypercall as flushing the ASID only 3952 * affects virtual to physical mappings, it does not invalidate guest 3953 * physical to host physical mappings. 3954 */ 3955 if (svm_hv_is_enlightened_tlb_enabled(vcpu) && VALID_PAGE(root_tdp)) 3956 hyperv_flush_guest_mapping(root_tdp); 3957 3958 svm_flush_tlb_asid(vcpu); 3959 } 3960 3961 static void svm_flush_tlb_all(struct kvm_vcpu *vcpu) 3962 { 3963 /* 3964 * When running on Hyper-V with EnlightenedNptTlb enabled, remote TLB 3965 * flushes should be routed to hv_flush_remote_tlbs() without requesting 3966 * a "regular" remote flush. Reaching this point means either there's 3967 * a KVM bug or a prior hv_flush_remote_tlbs() call failed, both of 3968 * which might be fatal to the guest. Yell, but try to recover. 3969 */ 3970 if (WARN_ON_ONCE(svm_hv_is_enlightened_tlb_enabled(vcpu))) 3971 hv_flush_remote_tlbs(vcpu->kvm); 3972 3973 svm_flush_tlb_asid(vcpu); 3974 } 3975 3976 static void svm_flush_tlb_gva(struct kvm_vcpu *vcpu, gva_t gva) 3977 { 3978 struct vcpu_svm *svm = to_svm(vcpu); 3979 3980 invlpga(gva, svm->vmcb->control.asid); 3981 } 3982 3983 static inline void sync_cr8_to_lapic(struct kvm_vcpu *vcpu) 3984 { 3985 struct vcpu_svm *svm = to_svm(vcpu); 3986 3987 if (nested_svm_virtualize_tpr(vcpu)) 3988 return; 3989 3990 if (!svm_is_intercept(svm, INTERCEPT_CR8_WRITE)) { 3991 int cr8 = svm->vmcb->control.int_ctl & V_TPR_MASK; 3992 kvm_set_cr8(vcpu, cr8); 3993 } 3994 } 3995 3996 static inline void sync_lapic_to_cr8(struct kvm_vcpu *vcpu) 3997 { 3998 struct vcpu_svm *svm = to_svm(vcpu); 3999 u64 cr8; 4000 4001 if (nested_svm_virtualize_tpr(vcpu)) 4002 return; 4003 4004 cr8 = kvm_get_cr8(vcpu); 4005 svm->vmcb->control.int_ctl &= ~V_TPR_MASK; 4006 svm->vmcb->control.int_ctl |= cr8 & V_TPR_MASK; 4007 } 4008 4009 static void svm_complete_soft_interrupt(struct kvm_vcpu *vcpu, u8 vector, 4010 int type) 4011 { 4012 bool is_exception = (type == SVM_EXITINTINFO_TYPE_EXEPT); 4013 bool is_soft = (type == SVM_EXITINTINFO_TYPE_SOFT); 4014 struct vcpu_svm *svm = to_svm(vcpu); 4015 4016 /* 4017 * If NRIPS is enabled, KVM must snapshot the pre-VMRUN next_rip that's 4018 * associated with the original soft exception/interrupt. next_rip is 4019 * cleared on all exits that can occur while vectoring an event, so KVM 4020 * needs to manually set next_rip for re-injection. Unlike the !nrips 4021 * case below, this needs to be done if and only if KVM is re-injecting 4022 * the same event, i.e. if the event is a soft exception/interrupt, 4023 * otherwise next_rip is unused on VMRUN. 4024 */ 4025 if (nrips && (is_soft || (is_exception && kvm_exception_is_soft(vector))) && 4026 kvm_is_linear_rip(vcpu, svm->soft_int_old_rip + svm->soft_int_csbase)) 4027 svm->vmcb->control.next_rip = svm->soft_int_next_rip; 4028 /* 4029 * If NRIPS isn't enabled, KVM must manually advance RIP prior to 4030 * injecting the soft exception/interrupt. That advancement needs to 4031 * be unwound if vectoring didn't complete. Note, the new event may 4032 * not be the injected event, e.g. if KVM injected an INTn, the INTn 4033 * hit a #NP in the guest, and the #NP encountered a #PF, the #NP will 4034 * be the reported vectored event, but RIP still needs to be unwound. 4035 */ 4036 else if (!nrips && (is_soft || is_exception) && 4037 kvm_is_linear_rip(vcpu, svm->soft_int_next_rip + svm->soft_int_csbase)) 4038 kvm_rip_write(vcpu, svm->soft_int_old_rip); 4039 } 4040 4041 static void svm_complete_interrupts(struct kvm_vcpu *vcpu) 4042 { 4043 struct vcpu_svm *svm = to_svm(vcpu); 4044 u8 vector; 4045 int type; 4046 u32 exitintinfo = svm->vmcb->control.exit_int_info; 4047 bool nmi_l1_to_l2 = svm->nmi_l1_to_l2; 4048 bool soft_int_injected = svm->soft_int_injected; 4049 4050 svm->nmi_l1_to_l2 = false; 4051 svm->soft_int_injected = false; 4052 4053 /* 4054 * If we've made progress since setting awaiting_iret_completion, we've 4055 * executed an IRET and can allow NMI injection. 4056 */ 4057 if (svm->awaiting_iret_completion && 4058 kvm_rip_read(vcpu) != svm->nmi_iret_rip) { 4059 svm->awaiting_iret_completion = false; 4060 svm->nmi_masked = false; 4061 kvm_make_request(KVM_REQ_EVENT, vcpu); 4062 } 4063 4064 vcpu->arch.nmi_injected = false; 4065 kvm_clear_exception_queue(vcpu); 4066 kvm_clear_interrupt_queue(vcpu); 4067 4068 if (!(exitintinfo & SVM_EXITINTINFO_VALID)) 4069 return; 4070 4071 kvm_make_request(KVM_REQ_EVENT, vcpu); 4072 4073 vector = exitintinfo & SVM_EXITINTINFO_VEC_MASK; 4074 type = exitintinfo & SVM_EXITINTINFO_TYPE_MASK; 4075 4076 if (soft_int_injected) 4077 svm_complete_soft_interrupt(vcpu, vector, type); 4078 4079 switch (type) { 4080 case SVM_EXITINTINFO_TYPE_NMI: 4081 vcpu->arch.nmi_injected = true; 4082 svm->nmi_l1_to_l2 = nmi_l1_to_l2; 4083 break; 4084 case SVM_EXITINTINFO_TYPE_EXEPT: { 4085 u32 error_code = 0; 4086 4087 /* 4088 * Never re-inject a #VC exception. 4089 */ 4090 if (vector == X86_TRAP_VC) 4091 break; 4092 4093 if (exitintinfo & SVM_EXITINTINFO_VALID_ERR) 4094 error_code = svm->vmcb->control.exit_int_info_err; 4095 4096 kvm_requeue_exception(vcpu, vector, 4097 exitintinfo & SVM_EXITINTINFO_VALID_ERR, 4098 error_code); 4099 break; 4100 } 4101 case SVM_EXITINTINFO_TYPE_INTR: 4102 kvm_queue_interrupt(vcpu, vector, false); 4103 break; 4104 case SVM_EXITINTINFO_TYPE_SOFT: 4105 kvm_queue_interrupt(vcpu, vector, true); 4106 break; 4107 default: 4108 break; 4109 } 4110 4111 } 4112 4113 static void svm_cancel_injection(struct kvm_vcpu *vcpu) 4114 { 4115 struct vcpu_svm *svm = to_svm(vcpu); 4116 struct vmcb_control_area *control = &svm->vmcb->control; 4117 4118 control->exit_int_info = control->event_inj; 4119 control->exit_int_info_err = control->event_inj_err; 4120 control->event_inj = 0; 4121 svm_complete_interrupts(vcpu); 4122 } 4123 4124 static int svm_vcpu_pre_run(struct kvm_vcpu *vcpu) 4125 { 4126 if (to_kvm_sev_info(vcpu->kvm)->need_init) 4127 return -EINVAL; 4128 4129 return 1; 4130 } 4131 4132 static fastpath_t svm_exit_handlers_fastpath(struct kvm_vcpu *vcpu) 4133 { 4134 struct vcpu_svm *svm = to_svm(vcpu); 4135 struct vmcb_control_area *control = &svm->vmcb->control; 4136 4137 /* 4138 * Next RIP must be provided as IRQs are disabled, and accessing guest 4139 * memory to decode the instruction might fault, i.e. might sleep. 4140 */ 4141 if (!nrips || !control->next_rip) 4142 return EXIT_FASTPATH_NONE; 4143 4144 if (is_guest_mode(vcpu)) 4145 return EXIT_FASTPATH_NONE; 4146 4147 switch (control->exit_code) { 4148 case SVM_EXIT_MSR: 4149 if (!control->exit_info_1) 4150 break; 4151 return handle_fastpath_wrmsr(vcpu); 4152 case SVM_EXIT_HLT: 4153 return handle_fastpath_hlt(vcpu); 4154 case SVM_EXIT_INVD: 4155 return handle_fastpath_invd(vcpu); 4156 default: 4157 break; 4158 } 4159 4160 return EXIT_FASTPATH_NONE; 4161 } 4162 4163 static noinstr void svm_vcpu_enter_exit(struct kvm_vcpu *vcpu, bool spec_ctrl_intercepted) 4164 { 4165 struct svm_cpu_data *sd = per_cpu_ptr(&svm_data, vcpu->cpu); 4166 struct vcpu_svm *svm = to_svm(vcpu); 4167 4168 guest_state_enter_irqoff(); 4169 4170 /* 4171 * Set RFLAGS.IF prior to VMRUN, as the host's RFLAGS.IF at the time of 4172 * VMRUN controls whether or not physical IRQs are masked (KVM always 4173 * runs with V_INTR_MASKING_MASK). Toggle RFLAGS.IF here to avoid the 4174 * temptation to do STI+VMRUN+CLI, as AMD CPUs bleed the STI shadow 4175 * into guest state if delivery of an event during VMRUN triggers a 4176 * #VMEXIT, and the guest_state transitions already tell lockdep that 4177 * IRQs are being enabled/disabled. Note! GIF=0 for the entirety of 4178 * this path, so IRQs aren't actually unmasked while running host code. 4179 */ 4180 raw_local_irq_enable(); 4181 4182 amd_clear_divider(); 4183 4184 if (sev_es_guest(vcpu->kvm)) 4185 __svm_sev_es_vcpu_run(svm, spec_ctrl_intercepted, 4186 sev_es_host_save_area(sd)); 4187 else 4188 __svm_vcpu_run(svm, spec_ctrl_intercepted); 4189 4190 raw_local_irq_disable(); 4191 4192 guest_state_exit_irqoff(); 4193 } 4194 4195 static __no_kcsan fastpath_t svm_vcpu_run(struct kvm_vcpu *vcpu, u64 run_flags) 4196 { 4197 bool force_immediate_exit = run_flags & KVM_RUN_FORCE_IMMEDIATE_EXIT; 4198 struct vcpu_svm *svm = to_svm(vcpu); 4199 bool spec_ctrl_intercepted = msr_write_intercepted(vcpu, MSR_IA32_SPEC_CTRL); 4200 4201 trace_kvm_entry(vcpu, force_immediate_exit); 4202 4203 svm->vmcb->save.rax = vcpu->arch.regs[VCPU_REGS_RAX]; 4204 svm->vmcb->save.rsp = vcpu->arch.regs[VCPU_REGS_RSP]; 4205 svm->vmcb->save.rip = vcpu->arch.regs[VCPU_REGS_RIP]; 4206 4207 /* 4208 * Disable singlestep if we're injecting an interrupt/exception. 4209 * We don't want our modified rflags to be pushed on the stack where 4210 * we might not be able to easily reset them if we disabled NMI 4211 * singlestep later. 4212 */ 4213 if (svm->nmi_singlestep && svm->vmcb->control.event_inj) { 4214 /* 4215 * Event injection happens before external interrupts cause a 4216 * vmexit and interrupts are disabled here, so smp_send_reschedule 4217 * is enough to force an immediate vmexit. 4218 */ 4219 disable_nmi_singlestep(svm); 4220 force_immediate_exit = true; 4221 } 4222 4223 if (force_immediate_exit) 4224 smp_send_reschedule(vcpu->cpu); 4225 4226 if (pre_svm_run(vcpu)) { 4227 vcpu->run->exit_reason = KVM_EXIT_FAIL_ENTRY; 4228 vcpu->run->fail_entry.hardware_entry_failure_reason = SVM_EXIT_ERR; 4229 vcpu->run->fail_entry.cpu = vcpu->cpu; 4230 return EXIT_FASTPATH_EXIT_USERSPACE; 4231 } 4232 4233 sync_lapic_to_cr8(vcpu); 4234 4235 if (unlikely(svm->asid != svm->vmcb->control.asid)) { 4236 svm->vmcb->control.asid = svm->asid; 4237 vmcb_mark_dirty(svm->vmcb, VMCB_ASID); 4238 } 4239 svm->vmcb->save.cr2 = vcpu->arch.cr2; 4240 4241 svm_hv_update_vp_id(svm->vmcb, vcpu); 4242 4243 /* 4244 * Run with all-zero DR6 unless the guest can write DR6 freely, so that 4245 * KVM can get the exact cause of a #DB. Note, loading guest DR6 from 4246 * KVM's snapshot is only necessary when DR accesses won't exit. 4247 */ 4248 if (unlikely(run_flags & KVM_RUN_LOAD_GUEST_DR6)) 4249 svm_set_dr6(vcpu, vcpu->arch.dr6); 4250 else if (likely(!(vcpu->arch.switch_db_regs & KVM_DEBUGREG_WONT_EXIT))) 4251 svm_set_dr6(vcpu, DR6_ACTIVE_LOW); 4252 4253 clgi(); 4254 kvm_load_guest_xsave_state(vcpu); 4255 4256 /* 4257 * Hardware only context switches DEBUGCTL if LBR virtualization is 4258 * enabled. Manually load DEBUGCTL if necessary (and restore it after 4259 * VM-Exit), as running with the host's DEBUGCTL can negatively affect 4260 * guest state and can even be fatal, e.g. due to Bus Lock Detect. 4261 */ 4262 if (!(svm->vmcb->control.virt_ext & LBR_CTL_ENABLE_MASK) && 4263 vcpu->arch.host_debugctl != svm->vmcb->save.dbgctl) 4264 update_debugctlmsr(svm->vmcb->save.dbgctl); 4265 4266 kvm_wait_lapic_expire(vcpu); 4267 4268 /* 4269 * If this vCPU has touched SPEC_CTRL, restore the guest's value if 4270 * it's non-zero. Since vmentry is serialising on affected CPUs, there 4271 * is no need to worry about the conditional branch over the wrmsr 4272 * being speculatively taken. 4273 */ 4274 if (!static_cpu_has(X86_FEATURE_V_SPEC_CTRL)) 4275 x86_spec_ctrl_set_guest(svm->virt_spec_ctrl); 4276 4277 svm_vcpu_enter_exit(vcpu, spec_ctrl_intercepted); 4278 4279 if (!static_cpu_has(X86_FEATURE_V_SPEC_CTRL)) 4280 x86_spec_ctrl_restore_host(svm->virt_spec_ctrl); 4281 4282 if (!sev_es_guest(vcpu->kvm)) { 4283 vcpu->arch.cr2 = svm->vmcb->save.cr2; 4284 vcpu->arch.regs[VCPU_REGS_RAX] = svm->vmcb->save.rax; 4285 vcpu->arch.regs[VCPU_REGS_RSP] = svm->vmcb->save.rsp; 4286 vcpu->arch.regs[VCPU_REGS_RIP] = svm->vmcb->save.rip; 4287 } 4288 vcpu->arch.regs_dirty = 0; 4289 4290 if (unlikely(svm->vmcb->control.exit_code == SVM_EXIT_NMI)) 4291 kvm_before_interrupt(vcpu, KVM_HANDLING_NMI); 4292 4293 if (!(svm->vmcb->control.virt_ext & LBR_CTL_ENABLE_MASK) && 4294 vcpu->arch.host_debugctl != svm->vmcb->save.dbgctl) 4295 update_debugctlmsr(vcpu->arch.host_debugctl); 4296 4297 kvm_load_host_xsave_state(vcpu); 4298 stgi(); 4299 4300 /* Any pending NMI will happen here */ 4301 4302 if (unlikely(svm->vmcb->control.exit_code == SVM_EXIT_NMI)) 4303 kvm_after_interrupt(vcpu); 4304 4305 sync_cr8_to_lapic(vcpu); 4306 4307 svm->next_rip = 0; 4308 if (is_guest_mode(vcpu)) { 4309 nested_sync_control_from_vmcb02(svm); 4310 4311 /* Track VMRUNs that have made past consistency checking */ 4312 if (svm->nested.nested_run_pending && 4313 svm->vmcb->control.exit_code != SVM_EXIT_ERR) 4314 ++vcpu->stat.nested_run; 4315 4316 svm->nested.nested_run_pending = 0; 4317 } 4318 4319 svm->vmcb->control.tlb_ctl = TLB_CONTROL_DO_NOTHING; 4320 vmcb_mark_all_clean(svm->vmcb); 4321 4322 /* if exit due to PF check for async PF */ 4323 if (svm->vmcb->control.exit_code == SVM_EXIT_EXCP_BASE + PF_VECTOR) 4324 vcpu->arch.apf.host_apf_flags = 4325 kvm_read_and_reset_apf_flags(); 4326 4327 vcpu->arch.regs_avail &= ~SVM_REGS_LAZY_LOAD_SET; 4328 4329 /* 4330 * We need to handle MC intercepts here before the vcpu has a chance to 4331 * change the physical cpu 4332 */ 4333 if (unlikely(svm->vmcb->control.exit_code == 4334 SVM_EXIT_EXCP_BASE + MC_VECTOR)) 4335 svm_handle_mce(vcpu); 4336 4337 trace_kvm_exit(vcpu, KVM_ISA_SVM); 4338 4339 svm_complete_interrupts(vcpu); 4340 4341 return svm_exit_handlers_fastpath(vcpu); 4342 } 4343 4344 static void svm_load_mmu_pgd(struct kvm_vcpu *vcpu, hpa_t root_hpa, 4345 int root_level) 4346 { 4347 struct vcpu_svm *svm = to_svm(vcpu); 4348 unsigned long cr3; 4349 4350 if (npt_enabled) { 4351 svm->vmcb->control.nested_cr3 = __sme_set(root_hpa); 4352 vmcb_mark_dirty(svm->vmcb, VMCB_NPT); 4353 4354 hv_track_root_tdp(vcpu, root_hpa); 4355 4356 cr3 = vcpu->arch.cr3; 4357 } else if (root_level >= PT64_ROOT_4LEVEL) { 4358 cr3 = __sme_set(root_hpa) | kvm_get_active_pcid(vcpu); 4359 } else { 4360 /* PCID in the guest should be impossible with a 32-bit MMU. */ 4361 WARN_ON_ONCE(kvm_get_active_pcid(vcpu)); 4362 cr3 = root_hpa; 4363 } 4364 4365 svm->vmcb->save.cr3 = cr3; 4366 vmcb_mark_dirty(svm->vmcb, VMCB_CR); 4367 } 4368 4369 static void 4370 svm_patch_hypercall(struct kvm_vcpu *vcpu, unsigned char *hypercall) 4371 { 4372 /* 4373 * Patch in the VMMCALL instruction: 4374 */ 4375 hypercall[0] = 0x0f; 4376 hypercall[1] = 0x01; 4377 hypercall[2] = 0xd9; 4378 } 4379 4380 /* 4381 * The kvm parameter can be NULL (module initialization, or invocation before 4382 * VM creation). Be sure to check the kvm parameter before using it. 4383 */ 4384 static bool svm_has_emulated_msr(struct kvm *kvm, u32 index) 4385 { 4386 switch (index) { 4387 case MSR_IA32_MCG_EXT_CTL: 4388 case KVM_FIRST_EMULATED_VMX_MSR ... KVM_LAST_EMULATED_VMX_MSR: 4389 return false; 4390 case MSR_IA32_SMBASE: 4391 if (!IS_ENABLED(CONFIG_KVM_SMM)) 4392 return false; 4393 /* SEV-ES guests do not support SMM, so report false */ 4394 if (kvm && sev_es_guest(kvm)) 4395 return false; 4396 break; 4397 default: 4398 break; 4399 } 4400 4401 return true; 4402 } 4403 4404 static void svm_vcpu_after_set_cpuid(struct kvm_vcpu *vcpu) 4405 { 4406 struct vcpu_svm *svm = to_svm(vcpu); 4407 4408 /* 4409 * SVM doesn't provide a way to disable just XSAVES in the guest, KVM 4410 * can only disable all variants of by disallowing CR4.OSXSAVE from 4411 * being set. As a result, if the host has XSAVE and XSAVES, and the 4412 * guest has XSAVE enabled, the guest can execute XSAVES without 4413 * faulting. Treat XSAVES as enabled in this case regardless of 4414 * whether it's advertised to the guest so that KVM context switches 4415 * XSS on VM-Enter/VM-Exit. Failure to do so would effectively give 4416 * the guest read/write access to the host's XSS. 4417 */ 4418 guest_cpu_cap_change(vcpu, X86_FEATURE_XSAVES, 4419 boot_cpu_has(X86_FEATURE_XSAVES) && 4420 guest_cpu_cap_has(vcpu, X86_FEATURE_XSAVE)); 4421 4422 /* 4423 * Intercept VMLOAD if the vCPU model is Intel in order to emulate that 4424 * VMLOAD drops bits 63:32 of SYSENTER (ignoring the fact that exposing 4425 * SVM on Intel is bonkers and extremely unlikely to work). 4426 */ 4427 if (guest_cpuid_is_intel_compatible(vcpu)) 4428 guest_cpu_cap_clear(vcpu, X86_FEATURE_V_VMSAVE_VMLOAD); 4429 4430 if (sev_guest(vcpu->kvm)) 4431 sev_vcpu_after_set_cpuid(svm); 4432 } 4433 4434 static bool svm_has_wbinvd_exit(void) 4435 { 4436 return true; 4437 } 4438 4439 #define PRE_EX(exit) { .exit_code = (exit), \ 4440 .stage = X86_ICPT_PRE_EXCEPT, } 4441 #define POST_EX(exit) { .exit_code = (exit), \ 4442 .stage = X86_ICPT_POST_EXCEPT, } 4443 #define POST_MEM(exit) { .exit_code = (exit), \ 4444 .stage = X86_ICPT_POST_MEMACCESS, } 4445 4446 static const struct __x86_intercept { 4447 u32 exit_code; 4448 enum x86_intercept_stage stage; 4449 } x86_intercept_map[] = { 4450 [x86_intercept_cr_read] = POST_EX(SVM_EXIT_READ_CR0), 4451 [x86_intercept_cr_write] = POST_EX(SVM_EXIT_WRITE_CR0), 4452 [x86_intercept_clts] = POST_EX(SVM_EXIT_WRITE_CR0), 4453 [x86_intercept_lmsw] = POST_EX(SVM_EXIT_WRITE_CR0), 4454 [x86_intercept_smsw] = POST_EX(SVM_EXIT_READ_CR0), 4455 [x86_intercept_dr_read] = POST_EX(SVM_EXIT_READ_DR0), 4456 [x86_intercept_dr_write] = POST_EX(SVM_EXIT_WRITE_DR0), 4457 [x86_intercept_sldt] = POST_EX(SVM_EXIT_LDTR_READ), 4458 [x86_intercept_str] = POST_EX(SVM_EXIT_TR_READ), 4459 [x86_intercept_lldt] = POST_EX(SVM_EXIT_LDTR_WRITE), 4460 [x86_intercept_ltr] = POST_EX(SVM_EXIT_TR_WRITE), 4461 [x86_intercept_sgdt] = POST_EX(SVM_EXIT_GDTR_READ), 4462 [x86_intercept_sidt] = POST_EX(SVM_EXIT_IDTR_READ), 4463 [x86_intercept_lgdt] = POST_EX(SVM_EXIT_GDTR_WRITE), 4464 [x86_intercept_lidt] = POST_EX(SVM_EXIT_IDTR_WRITE), 4465 [x86_intercept_vmrun] = POST_EX(SVM_EXIT_VMRUN), 4466 [x86_intercept_vmmcall] = POST_EX(SVM_EXIT_VMMCALL), 4467 [x86_intercept_vmload] = POST_EX(SVM_EXIT_VMLOAD), 4468 [x86_intercept_vmsave] = POST_EX(SVM_EXIT_VMSAVE), 4469 [x86_intercept_stgi] = POST_EX(SVM_EXIT_STGI), 4470 [x86_intercept_clgi] = POST_EX(SVM_EXIT_CLGI), 4471 [x86_intercept_skinit] = POST_EX(SVM_EXIT_SKINIT), 4472 [x86_intercept_invlpga] = POST_EX(SVM_EXIT_INVLPGA), 4473 [x86_intercept_rdtscp] = POST_EX(SVM_EXIT_RDTSCP), 4474 [x86_intercept_monitor] = POST_MEM(SVM_EXIT_MONITOR), 4475 [x86_intercept_mwait] = POST_EX(SVM_EXIT_MWAIT), 4476 [x86_intercept_invlpg] = POST_EX(SVM_EXIT_INVLPG), 4477 [x86_intercept_invd] = POST_EX(SVM_EXIT_INVD), 4478 [x86_intercept_wbinvd] = POST_EX(SVM_EXIT_WBINVD), 4479 [x86_intercept_wrmsr] = POST_EX(SVM_EXIT_MSR), 4480 [x86_intercept_rdtsc] = POST_EX(SVM_EXIT_RDTSC), 4481 [x86_intercept_rdmsr] = POST_EX(SVM_EXIT_MSR), 4482 [x86_intercept_rdpmc] = POST_EX(SVM_EXIT_RDPMC), 4483 [x86_intercept_cpuid] = PRE_EX(SVM_EXIT_CPUID), 4484 [x86_intercept_rsm] = PRE_EX(SVM_EXIT_RSM), 4485 [x86_intercept_pause] = PRE_EX(SVM_EXIT_PAUSE), 4486 [x86_intercept_pushf] = PRE_EX(SVM_EXIT_PUSHF), 4487 [x86_intercept_popf] = PRE_EX(SVM_EXIT_POPF), 4488 [x86_intercept_intn] = PRE_EX(SVM_EXIT_SWINT), 4489 [x86_intercept_iret] = PRE_EX(SVM_EXIT_IRET), 4490 [x86_intercept_icebp] = PRE_EX(SVM_EXIT_ICEBP), 4491 [x86_intercept_hlt] = POST_EX(SVM_EXIT_HLT), 4492 [x86_intercept_in] = POST_EX(SVM_EXIT_IOIO), 4493 [x86_intercept_ins] = POST_EX(SVM_EXIT_IOIO), 4494 [x86_intercept_out] = POST_EX(SVM_EXIT_IOIO), 4495 [x86_intercept_outs] = POST_EX(SVM_EXIT_IOIO), 4496 [x86_intercept_xsetbv] = PRE_EX(SVM_EXIT_XSETBV), 4497 }; 4498 4499 #undef PRE_EX 4500 #undef POST_EX 4501 #undef POST_MEM 4502 4503 static int svm_check_intercept(struct kvm_vcpu *vcpu, 4504 struct x86_instruction_info *info, 4505 enum x86_intercept_stage stage, 4506 struct x86_exception *exception) 4507 { 4508 struct vcpu_svm *svm = to_svm(vcpu); 4509 int vmexit, ret = X86EMUL_CONTINUE; 4510 struct __x86_intercept icpt_info; 4511 struct vmcb *vmcb = svm->vmcb; 4512 4513 if (info->intercept >= ARRAY_SIZE(x86_intercept_map)) 4514 goto out; 4515 4516 icpt_info = x86_intercept_map[info->intercept]; 4517 4518 if (stage != icpt_info.stage) 4519 goto out; 4520 4521 switch (icpt_info.exit_code) { 4522 case SVM_EXIT_READ_CR0: 4523 if (info->intercept == x86_intercept_cr_read) 4524 icpt_info.exit_code += info->modrm_reg; 4525 break; 4526 case SVM_EXIT_WRITE_CR0: { 4527 unsigned long cr0, val; 4528 4529 if (info->intercept == x86_intercept_cr_write) 4530 icpt_info.exit_code += info->modrm_reg; 4531 4532 if (icpt_info.exit_code != SVM_EXIT_WRITE_CR0 || 4533 info->intercept == x86_intercept_clts) 4534 break; 4535 4536 if (!(vmcb12_is_intercept(&svm->nested.ctl, 4537 INTERCEPT_SELECTIVE_CR0))) 4538 break; 4539 4540 cr0 = vcpu->arch.cr0 & ~SVM_CR0_SELECTIVE_MASK; 4541 val = info->src_val & ~SVM_CR0_SELECTIVE_MASK; 4542 4543 if (info->intercept == x86_intercept_lmsw) { 4544 cr0 &= 0xfUL; 4545 val &= 0xfUL; 4546 /* lmsw can't clear PE - catch this here */ 4547 if (cr0 & X86_CR0_PE) 4548 val |= X86_CR0_PE; 4549 } 4550 4551 if (cr0 ^ val) 4552 icpt_info.exit_code = SVM_EXIT_CR0_SEL_WRITE; 4553 4554 break; 4555 } 4556 case SVM_EXIT_READ_DR0: 4557 case SVM_EXIT_WRITE_DR0: 4558 icpt_info.exit_code += info->modrm_reg; 4559 break; 4560 case SVM_EXIT_MSR: 4561 if (info->intercept == x86_intercept_wrmsr) 4562 vmcb->control.exit_info_1 = 1; 4563 else 4564 vmcb->control.exit_info_1 = 0; 4565 break; 4566 case SVM_EXIT_PAUSE: 4567 /* 4568 * We get this for NOP only, but pause 4569 * is rep not, check this here 4570 */ 4571 if (info->rep_prefix != REPE_PREFIX) 4572 goto out; 4573 break; 4574 case SVM_EXIT_IOIO: { 4575 u64 exit_info; 4576 u32 bytes; 4577 4578 if (info->intercept == x86_intercept_in || 4579 info->intercept == x86_intercept_ins) { 4580 exit_info = ((info->src_val & 0xffff) << 16) | 4581 SVM_IOIO_TYPE_MASK; 4582 bytes = info->dst_bytes; 4583 } else { 4584 exit_info = (info->dst_val & 0xffff) << 16; 4585 bytes = info->src_bytes; 4586 } 4587 4588 if (info->intercept == x86_intercept_outs || 4589 info->intercept == x86_intercept_ins) 4590 exit_info |= SVM_IOIO_STR_MASK; 4591 4592 if (info->rep_prefix) 4593 exit_info |= SVM_IOIO_REP_MASK; 4594 4595 bytes = min(bytes, 4u); 4596 4597 exit_info |= bytes << SVM_IOIO_SIZE_SHIFT; 4598 4599 exit_info |= (u32)info->ad_bytes << (SVM_IOIO_ASIZE_SHIFT - 1); 4600 4601 vmcb->control.exit_info_1 = exit_info; 4602 vmcb->control.exit_info_2 = info->next_rip; 4603 4604 break; 4605 } 4606 default: 4607 break; 4608 } 4609 4610 /* TODO: Advertise NRIPS to guest hypervisor unconditionally */ 4611 if (static_cpu_has(X86_FEATURE_NRIPS)) 4612 vmcb->control.next_rip = info->next_rip; 4613 vmcb->control.exit_code = icpt_info.exit_code; 4614 vmexit = nested_svm_exit_handled(svm); 4615 4616 ret = (vmexit == NESTED_EXIT_DONE) ? X86EMUL_INTERCEPTED 4617 : X86EMUL_CONTINUE; 4618 4619 out: 4620 return ret; 4621 } 4622 4623 static void svm_handle_exit_irqoff(struct kvm_vcpu *vcpu) 4624 { 4625 if (to_svm(vcpu)->vmcb->control.exit_code == SVM_EXIT_INTR) 4626 vcpu->arch.at_instruction_boundary = true; 4627 } 4628 4629 static void svm_setup_mce(struct kvm_vcpu *vcpu) 4630 { 4631 /* [63:9] are reserved. */ 4632 vcpu->arch.mcg_cap &= 0x1ff; 4633 } 4634 4635 #ifdef CONFIG_KVM_SMM 4636 bool svm_smi_blocked(struct kvm_vcpu *vcpu) 4637 { 4638 struct vcpu_svm *svm = to_svm(vcpu); 4639 4640 /* Per APM Vol.2 15.22.2 "Response to SMI" */ 4641 if (!gif_set(svm)) 4642 return true; 4643 4644 return is_smm(vcpu); 4645 } 4646 4647 static int svm_smi_allowed(struct kvm_vcpu *vcpu, bool for_injection) 4648 { 4649 struct vcpu_svm *svm = to_svm(vcpu); 4650 if (svm->nested.nested_run_pending) 4651 return -EBUSY; 4652 4653 if (svm_smi_blocked(vcpu)) 4654 return 0; 4655 4656 /* An SMI must not be injected into L2 if it's supposed to VM-Exit. */ 4657 if (for_injection && is_guest_mode(vcpu) && nested_exit_on_smi(svm)) 4658 return -EBUSY; 4659 4660 return 1; 4661 } 4662 4663 static int svm_enter_smm(struct kvm_vcpu *vcpu, union kvm_smram *smram) 4664 { 4665 struct vcpu_svm *svm = to_svm(vcpu); 4666 struct kvm_host_map map_save; 4667 int ret; 4668 4669 if (!is_guest_mode(vcpu)) 4670 return 0; 4671 4672 /* 4673 * 32-bit SMRAM format doesn't preserve EFER and SVM state. Userspace is 4674 * responsible for ensuring nested SVM and SMIs are mutually exclusive. 4675 */ 4676 4677 if (!guest_cpu_cap_has(vcpu, X86_FEATURE_LM)) 4678 return 1; 4679 4680 smram->smram64.svm_guest_flag = 1; 4681 smram->smram64.svm_guest_vmcb_gpa = svm->nested.vmcb12_gpa; 4682 4683 svm->vmcb->save.rax = vcpu->arch.regs[VCPU_REGS_RAX]; 4684 svm->vmcb->save.rsp = vcpu->arch.regs[VCPU_REGS_RSP]; 4685 svm->vmcb->save.rip = vcpu->arch.regs[VCPU_REGS_RIP]; 4686 4687 ret = nested_svm_simple_vmexit(svm, SVM_EXIT_SW); 4688 if (ret) 4689 return ret; 4690 4691 /* 4692 * KVM uses VMCB01 to store L1 host state while L2 runs but 4693 * VMCB01 is going to be used during SMM and thus the state will 4694 * be lost. Temporary save non-VMLOAD/VMSAVE state to the host save 4695 * area pointed to by MSR_VM_HSAVE_PA. APM guarantees that the 4696 * format of the area is identical to guest save area offsetted 4697 * by 0x400 (matches the offset of 'struct vmcb_save_area' 4698 * within 'struct vmcb'). Note: HSAVE area may also be used by 4699 * L1 hypervisor to save additional host context (e.g. KVM does 4700 * that, see svm_prepare_switch_to_guest()) which must be 4701 * preserved. 4702 */ 4703 if (kvm_vcpu_map(vcpu, gpa_to_gfn(svm->nested.hsave_msr), &map_save)) 4704 return 1; 4705 4706 BUILD_BUG_ON(offsetof(struct vmcb, save) != 0x400); 4707 4708 svm_copy_vmrun_state(map_save.hva + 0x400, 4709 &svm->vmcb01.ptr->save); 4710 4711 kvm_vcpu_unmap(vcpu, &map_save); 4712 return 0; 4713 } 4714 4715 static int svm_leave_smm(struct kvm_vcpu *vcpu, const union kvm_smram *smram) 4716 { 4717 struct vcpu_svm *svm = to_svm(vcpu); 4718 struct kvm_host_map map, map_save; 4719 struct vmcb *vmcb12; 4720 int ret; 4721 4722 const struct kvm_smram_state_64 *smram64 = &smram->smram64; 4723 4724 if (!guest_cpu_cap_has(vcpu, X86_FEATURE_LM)) 4725 return 0; 4726 4727 /* Non-zero if SMI arrived while vCPU was in guest mode. */ 4728 if (!smram64->svm_guest_flag) 4729 return 0; 4730 4731 if (!guest_cpu_cap_has(vcpu, X86_FEATURE_SVM)) 4732 return 1; 4733 4734 if (!(smram64->efer & EFER_SVME)) 4735 return 1; 4736 4737 if (kvm_vcpu_map(vcpu, gpa_to_gfn(smram64->svm_guest_vmcb_gpa), &map)) 4738 return 1; 4739 4740 ret = 1; 4741 if (kvm_vcpu_map(vcpu, gpa_to_gfn(svm->nested.hsave_msr), &map_save)) 4742 goto unmap_map; 4743 4744 if (svm_allocate_nested(svm)) 4745 goto unmap_save; 4746 4747 /* 4748 * Restore L1 host state from L1 HSAVE area as VMCB01 was 4749 * used during SMM (see svm_enter_smm()) 4750 */ 4751 4752 svm_copy_vmrun_state(&svm->vmcb01.ptr->save, map_save.hva + 0x400); 4753 4754 /* 4755 * Enter the nested guest now 4756 */ 4757 4758 vmcb_mark_all_dirty(svm->vmcb01.ptr); 4759 4760 vmcb12 = map.hva; 4761 nested_copy_vmcb_control_to_cache(svm, &vmcb12->control); 4762 nested_copy_vmcb_save_to_cache(svm, &vmcb12->save); 4763 ret = enter_svm_guest_mode(vcpu, smram64->svm_guest_vmcb_gpa, vmcb12, false); 4764 4765 if (ret) 4766 goto unmap_save; 4767 4768 svm->nested.nested_run_pending = 1; 4769 4770 unmap_save: 4771 kvm_vcpu_unmap(vcpu, &map_save); 4772 unmap_map: 4773 kvm_vcpu_unmap(vcpu, &map); 4774 return ret; 4775 } 4776 4777 static void svm_enable_smi_window(struct kvm_vcpu *vcpu) 4778 { 4779 struct vcpu_svm *svm = to_svm(vcpu); 4780 4781 if (!gif_set(svm)) { 4782 if (vgif) 4783 svm_set_intercept(svm, INTERCEPT_STGI); 4784 /* STGI will cause a vm exit */ 4785 } else { 4786 /* We must be in SMM; RSM will cause a vmexit anyway. */ 4787 } 4788 } 4789 #endif 4790 4791 static int svm_check_emulate_instruction(struct kvm_vcpu *vcpu, int emul_type, 4792 void *insn, int insn_len) 4793 { 4794 struct vcpu_svm *svm = to_svm(vcpu); 4795 bool smep, smap, is_user; 4796 u64 error_code; 4797 4798 /* Check that emulation is possible during event vectoring */ 4799 if ((svm->vmcb->control.exit_int_info & SVM_EXITINTINFO_TYPE_MASK) && 4800 !kvm_can_emulate_event_vectoring(emul_type)) 4801 return X86EMUL_UNHANDLEABLE_VECTORING; 4802 4803 /* Emulation is always possible when KVM has access to all guest state. */ 4804 if (!sev_guest(vcpu->kvm)) 4805 return X86EMUL_CONTINUE; 4806 4807 /* #UD and #GP should never be intercepted for SEV guests. */ 4808 WARN_ON_ONCE(emul_type & (EMULTYPE_TRAP_UD | 4809 EMULTYPE_TRAP_UD_FORCED | 4810 EMULTYPE_VMWARE_GP)); 4811 4812 /* 4813 * Emulation is impossible for SEV-ES guests as KVM doesn't have access 4814 * to guest register state. 4815 */ 4816 if (sev_es_guest(vcpu->kvm)) 4817 return X86EMUL_RETRY_INSTR; 4818 4819 /* 4820 * Emulation is possible if the instruction is already decoded, e.g. 4821 * when completing I/O after returning from userspace. 4822 */ 4823 if (emul_type & EMULTYPE_NO_DECODE) 4824 return X86EMUL_CONTINUE; 4825 4826 /* 4827 * Emulation is possible for SEV guests if and only if a prefilled 4828 * buffer containing the bytes of the intercepted instruction is 4829 * available. SEV guest memory is encrypted with a guest specific key 4830 * and cannot be decrypted by KVM, i.e. KVM would read ciphertext and 4831 * decode garbage. 4832 * 4833 * If KVM is NOT trying to simply skip an instruction, inject #UD if 4834 * KVM reached this point without an instruction buffer. In practice, 4835 * this path should never be hit by a well-behaved guest, e.g. KVM 4836 * doesn't intercept #UD or #GP for SEV guests, but this path is still 4837 * theoretically reachable, e.g. via unaccelerated fault-like AVIC 4838 * access, and needs to be handled by KVM to avoid putting the guest 4839 * into an infinite loop. Injecting #UD is somewhat arbitrary, but 4840 * its the least awful option given lack of insight into the guest. 4841 * 4842 * If KVM is trying to skip an instruction, simply resume the guest. 4843 * If a #NPF occurs while the guest is vectoring an INT3/INTO, then KVM 4844 * will attempt to re-inject the INT3/INTO and skip the instruction. 4845 * In that scenario, retrying the INT3/INTO and hoping the guest will 4846 * make forward progress is the only option that has a chance of 4847 * success (and in practice it will work the vast majority of the time). 4848 */ 4849 if (unlikely(!insn)) { 4850 if (emul_type & EMULTYPE_SKIP) 4851 return X86EMUL_UNHANDLEABLE; 4852 4853 kvm_queue_exception(vcpu, UD_VECTOR); 4854 return X86EMUL_PROPAGATE_FAULT; 4855 } 4856 4857 /* 4858 * Emulate for SEV guests if the insn buffer is not empty. The buffer 4859 * will be empty if the DecodeAssist microcode cannot fetch bytes for 4860 * the faulting instruction because the code fetch itself faulted, e.g. 4861 * the guest attempted to fetch from emulated MMIO or a guest page 4862 * table used to translate CS:RIP resides in emulated MMIO. 4863 */ 4864 if (likely(insn_len)) 4865 return X86EMUL_CONTINUE; 4866 4867 /* 4868 * Detect and workaround Errata 1096 Fam_17h_00_0Fh. 4869 * 4870 * Errata: 4871 * When CPU raises #NPF on guest data access and vCPU CR4.SMAP=1, it is 4872 * possible that CPU microcode implementing DecodeAssist will fail to 4873 * read guest memory at CS:RIP and vmcb.GuestIntrBytes will incorrectly 4874 * be '0'. This happens because microcode reads CS:RIP using a _data_ 4875 * loap uop with CPL=0 privileges. If the load hits a SMAP #PF, ucode 4876 * gives up and does not fill the instruction bytes buffer. 4877 * 4878 * As above, KVM reaches this point iff the VM is an SEV guest, the CPU 4879 * supports DecodeAssist, a #NPF was raised, KVM's page fault handler 4880 * triggered emulation (e.g. for MMIO), and the CPU returned 0 in the 4881 * GuestIntrBytes field of the VMCB. 4882 * 4883 * This does _not_ mean that the erratum has been encountered, as the 4884 * DecodeAssist will also fail if the load for CS:RIP hits a legitimate 4885 * #PF, e.g. if the guest attempt to execute from emulated MMIO and 4886 * encountered a reserved/not-present #PF. 4887 * 4888 * To hit the erratum, the following conditions must be true: 4889 * 1. CR4.SMAP=1 (obviously). 4890 * 2. CR4.SMEP=0 || CPL=3. If SMEP=1 and CPL<3, the erratum cannot 4891 * have been hit as the guest would have encountered a SMEP 4892 * violation #PF, not a #NPF. 4893 * 3. The #NPF is not due to a code fetch, in which case failure to 4894 * retrieve the instruction bytes is legitimate (see abvoe). 4895 * 4896 * In addition, don't apply the erratum workaround if the #NPF occurred 4897 * while translating guest page tables (see below). 4898 */ 4899 error_code = svm->vmcb->control.exit_info_1; 4900 if (error_code & (PFERR_GUEST_PAGE_MASK | PFERR_FETCH_MASK)) 4901 goto resume_guest; 4902 4903 smep = kvm_is_cr4_bit_set(vcpu, X86_CR4_SMEP); 4904 smap = kvm_is_cr4_bit_set(vcpu, X86_CR4_SMAP); 4905 is_user = svm_get_cpl(vcpu) == 3; 4906 if (smap && (!smep || is_user)) { 4907 pr_err_ratelimited("SEV Guest triggered AMD Erratum 1096\n"); 4908 4909 /* 4910 * If the fault occurred in userspace, arbitrarily inject #GP 4911 * to avoid killing the guest and to hopefully avoid confusing 4912 * the guest kernel too much, e.g. injecting #PF would not be 4913 * coherent with respect to the guest's page tables. Request 4914 * triple fault if the fault occurred in the kernel as there's 4915 * no fault that KVM can inject without confusing the guest. 4916 * In practice, the triple fault is moot as no sane SEV kernel 4917 * will execute from user memory while also running with SMAP=1. 4918 */ 4919 if (is_user) 4920 kvm_inject_gp(vcpu, 0); 4921 else 4922 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu); 4923 return X86EMUL_PROPAGATE_FAULT; 4924 } 4925 4926 resume_guest: 4927 /* 4928 * If the erratum was not hit, simply resume the guest and let it fault 4929 * again. While awful, e.g. the vCPU may get stuck in an infinite loop 4930 * if the fault is at CPL=0, it's the lesser of all evils. Exiting to 4931 * userspace will kill the guest, and letting the emulator read garbage 4932 * will yield random behavior and potentially corrupt the guest. 4933 * 4934 * Simply resuming the guest is technically not a violation of the SEV 4935 * architecture. AMD's APM states that all code fetches and page table 4936 * accesses for SEV guest are encrypted, regardless of the C-Bit. The 4937 * APM also states that encrypted accesses to MMIO are "ignored", but 4938 * doesn't explicitly define "ignored", i.e. doing nothing and letting 4939 * the guest spin is technically "ignoring" the access. 4940 */ 4941 return X86EMUL_RETRY_INSTR; 4942 } 4943 4944 static bool svm_apic_init_signal_blocked(struct kvm_vcpu *vcpu) 4945 { 4946 struct vcpu_svm *svm = to_svm(vcpu); 4947 4948 return !gif_set(svm); 4949 } 4950 4951 static void svm_vcpu_deliver_sipi_vector(struct kvm_vcpu *vcpu, u8 vector) 4952 { 4953 if (!sev_es_guest(vcpu->kvm)) 4954 return kvm_vcpu_deliver_sipi_vector(vcpu, vector); 4955 4956 sev_vcpu_deliver_sipi_vector(vcpu, vector); 4957 } 4958 4959 static void svm_vm_destroy(struct kvm *kvm) 4960 { 4961 avic_vm_destroy(kvm); 4962 sev_vm_destroy(kvm); 4963 4964 svm_srso_vm_destroy(); 4965 } 4966 4967 static int svm_vm_init(struct kvm *kvm) 4968 { 4969 int type = kvm->arch.vm_type; 4970 4971 if (type != KVM_X86_DEFAULT_VM && 4972 type != KVM_X86_SW_PROTECTED_VM) { 4973 kvm->arch.has_protected_state = 4974 (type == KVM_X86_SEV_ES_VM || type == KVM_X86_SNP_VM); 4975 to_kvm_sev_info(kvm)->need_init = true; 4976 4977 kvm->arch.has_private_mem = (type == KVM_X86_SNP_VM); 4978 kvm->arch.pre_fault_allowed = !kvm->arch.has_private_mem; 4979 } 4980 4981 if (!pause_filter_count || !pause_filter_thresh) 4982 kvm_disable_exits(kvm, KVM_X86_DISABLE_EXITS_PAUSE); 4983 4984 if (enable_apicv) { 4985 int ret = avic_vm_init(kvm); 4986 if (ret) 4987 return ret; 4988 } 4989 4990 svm_srso_vm_init(); 4991 return 0; 4992 } 4993 4994 static void *svm_alloc_apic_backing_page(struct kvm_vcpu *vcpu) 4995 { 4996 struct page *page = snp_safe_alloc_page(); 4997 4998 if (!page) 4999 return NULL; 5000 5001 return page_address(page); 5002 } 5003 5004 struct kvm_x86_ops svm_x86_ops __initdata = { 5005 .name = KBUILD_MODNAME, 5006 5007 .check_processor_compatibility = svm_check_processor_compat, 5008 5009 .hardware_unsetup = svm_hardware_unsetup, 5010 .enable_virtualization_cpu = svm_enable_virtualization_cpu, 5011 .disable_virtualization_cpu = svm_disable_virtualization_cpu, 5012 .emergency_disable_virtualization_cpu = svm_emergency_disable_virtualization_cpu, 5013 .has_emulated_msr = svm_has_emulated_msr, 5014 5015 .vcpu_create = svm_vcpu_create, 5016 .vcpu_free = svm_vcpu_free, 5017 .vcpu_reset = svm_vcpu_reset, 5018 5019 .vm_size = sizeof(struct kvm_svm), 5020 .vm_init = svm_vm_init, 5021 .vm_destroy = svm_vm_destroy, 5022 5023 .prepare_switch_to_guest = svm_prepare_switch_to_guest, 5024 .vcpu_load = svm_vcpu_load, 5025 .vcpu_put = svm_vcpu_put, 5026 .vcpu_blocking = avic_vcpu_blocking, 5027 .vcpu_unblocking = avic_vcpu_unblocking, 5028 5029 .update_exception_bitmap = svm_update_exception_bitmap, 5030 .get_feature_msr = svm_get_feature_msr, 5031 .get_msr = svm_get_msr, 5032 .set_msr = svm_set_msr, 5033 .get_segment_base = svm_get_segment_base, 5034 .get_segment = svm_get_segment, 5035 .set_segment = svm_set_segment, 5036 .get_cpl = svm_get_cpl, 5037 .get_cpl_no_cache = svm_get_cpl, 5038 .get_cs_db_l_bits = svm_get_cs_db_l_bits, 5039 .is_valid_cr0 = svm_is_valid_cr0, 5040 .set_cr0 = svm_set_cr0, 5041 .post_set_cr3 = sev_post_set_cr3, 5042 .is_valid_cr4 = svm_is_valid_cr4, 5043 .set_cr4 = svm_set_cr4, 5044 .set_efer = svm_set_efer, 5045 .get_idt = svm_get_idt, 5046 .set_idt = svm_set_idt, 5047 .get_gdt = svm_get_gdt, 5048 .set_gdt = svm_set_gdt, 5049 .set_dr7 = svm_set_dr7, 5050 .sync_dirty_debug_regs = svm_sync_dirty_debug_regs, 5051 .cache_reg = svm_cache_reg, 5052 .get_rflags = svm_get_rflags, 5053 .set_rflags = svm_set_rflags, 5054 .get_if_flag = svm_get_if_flag, 5055 5056 .flush_tlb_all = svm_flush_tlb_all, 5057 .flush_tlb_current = svm_flush_tlb_current, 5058 .flush_tlb_gva = svm_flush_tlb_gva, 5059 .flush_tlb_guest = svm_flush_tlb_asid, 5060 5061 .vcpu_pre_run = svm_vcpu_pre_run, 5062 .vcpu_run = svm_vcpu_run, 5063 .handle_exit = svm_handle_exit, 5064 .skip_emulated_instruction = svm_skip_emulated_instruction, 5065 .update_emulated_instruction = NULL, 5066 .set_interrupt_shadow = svm_set_interrupt_shadow, 5067 .get_interrupt_shadow = svm_get_interrupt_shadow, 5068 .patch_hypercall = svm_patch_hypercall, 5069 .inject_irq = svm_inject_irq, 5070 .inject_nmi = svm_inject_nmi, 5071 .is_vnmi_pending = svm_is_vnmi_pending, 5072 .set_vnmi_pending = svm_set_vnmi_pending, 5073 .inject_exception = svm_inject_exception, 5074 .cancel_injection = svm_cancel_injection, 5075 .interrupt_allowed = svm_interrupt_allowed, 5076 .nmi_allowed = svm_nmi_allowed, 5077 .get_nmi_mask = svm_get_nmi_mask, 5078 .set_nmi_mask = svm_set_nmi_mask, 5079 .enable_nmi_window = svm_enable_nmi_window, 5080 .enable_irq_window = svm_enable_irq_window, 5081 .update_cr8_intercept = svm_update_cr8_intercept, 5082 5083 .x2apic_icr_is_split = true, 5084 .set_virtual_apic_mode = avic_refresh_virtual_apic_mode, 5085 .refresh_apicv_exec_ctrl = avic_refresh_apicv_exec_ctrl, 5086 .apicv_post_state_restore = avic_apicv_post_state_restore, 5087 .required_apicv_inhibits = AVIC_REQUIRED_APICV_INHIBITS, 5088 5089 .get_exit_info = svm_get_exit_info, 5090 .get_entry_info = svm_get_entry_info, 5091 5092 .vcpu_after_set_cpuid = svm_vcpu_after_set_cpuid, 5093 5094 .has_wbinvd_exit = svm_has_wbinvd_exit, 5095 5096 .get_l2_tsc_offset = svm_get_l2_tsc_offset, 5097 .get_l2_tsc_multiplier = svm_get_l2_tsc_multiplier, 5098 .write_tsc_offset = svm_write_tsc_offset, 5099 .write_tsc_multiplier = svm_write_tsc_multiplier, 5100 5101 .load_mmu_pgd = svm_load_mmu_pgd, 5102 5103 .check_intercept = svm_check_intercept, 5104 .handle_exit_irqoff = svm_handle_exit_irqoff, 5105 5106 .nested_ops = &svm_nested_ops, 5107 5108 .deliver_interrupt = svm_deliver_interrupt, 5109 .pi_update_irte = avic_pi_update_irte, 5110 .setup_mce = svm_setup_mce, 5111 5112 #ifdef CONFIG_KVM_SMM 5113 .smi_allowed = svm_smi_allowed, 5114 .enter_smm = svm_enter_smm, 5115 .leave_smm = svm_leave_smm, 5116 .enable_smi_window = svm_enable_smi_window, 5117 #endif 5118 5119 #ifdef CONFIG_KVM_AMD_SEV 5120 .dev_get_attr = sev_dev_get_attr, 5121 .mem_enc_ioctl = sev_mem_enc_ioctl, 5122 .mem_enc_register_region = sev_mem_enc_register_region, 5123 .mem_enc_unregister_region = sev_mem_enc_unregister_region, 5124 .guest_memory_reclaimed = sev_guest_memory_reclaimed, 5125 5126 .vm_copy_enc_context_from = sev_vm_copy_enc_context_from, 5127 .vm_move_enc_context_from = sev_vm_move_enc_context_from, 5128 #endif 5129 .check_emulate_instruction = svm_check_emulate_instruction, 5130 5131 .apic_init_signal_blocked = svm_apic_init_signal_blocked, 5132 5133 .recalc_intercepts = svm_recalc_intercepts, 5134 .complete_emulated_msr = svm_complete_emulated_msr, 5135 5136 .vcpu_deliver_sipi_vector = svm_vcpu_deliver_sipi_vector, 5137 .vcpu_get_apicv_inhibit_reasons = avic_vcpu_get_apicv_inhibit_reasons, 5138 .alloc_apic_backing_page = svm_alloc_apic_backing_page, 5139 5140 .gmem_prepare = sev_gmem_prepare, 5141 .gmem_invalidate = sev_gmem_invalidate, 5142 .gmem_max_mapping_level = sev_gmem_max_mapping_level, 5143 }; 5144 5145 /* 5146 * The default MMIO mask is a single bit (excluding the present bit), 5147 * which could conflict with the memory encryption bit. Check for 5148 * memory encryption support and override the default MMIO mask if 5149 * memory encryption is enabled. 5150 */ 5151 static __init void svm_adjust_mmio_mask(void) 5152 { 5153 unsigned int enc_bit, mask_bit; 5154 u64 msr, mask; 5155 5156 /* If there is no memory encryption support, use existing mask */ 5157 if (cpuid_eax(0x80000000) < 0x8000001f) 5158 return; 5159 5160 /* If memory encryption is not enabled, use existing mask */ 5161 rdmsrq(MSR_AMD64_SYSCFG, msr); 5162 if (!(msr & MSR_AMD64_SYSCFG_MEM_ENCRYPT)) 5163 return; 5164 5165 enc_bit = cpuid_ebx(0x8000001f) & 0x3f; 5166 mask_bit = boot_cpu_data.x86_phys_bits; 5167 5168 /* Increment the mask bit if it is the same as the encryption bit */ 5169 if (enc_bit == mask_bit) 5170 mask_bit++; 5171 5172 /* 5173 * If the mask bit location is below 52, then some bits above the 5174 * physical addressing limit will always be reserved, so use the 5175 * rsvd_bits() function to generate the mask. This mask, along with 5176 * the present bit, will be used to generate a page fault with 5177 * PFER.RSV = 1. 5178 * 5179 * If the mask bit location is 52 (or above), then clear the mask. 5180 */ 5181 mask = (mask_bit < 52) ? rsvd_bits(mask_bit, 51) | PT_PRESENT_MASK : 0; 5182 5183 kvm_mmu_set_mmio_spte_mask(mask, mask, PT_WRITABLE_MASK | PT_USER_MASK); 5184 } 5185 5186 static __init void svm_set_cpu_caps(void) 5187 { 5188 kvm_set_cpu_caps(); 5189 5190 kvm_caps.supported_perf_cap = 0; 5191 5192 kvm_cpu_cap_clear(X86_FEATURE_IBT); 5193 5194 /* CPUID 0x80000001 and 0x8000000A (SVM features) */ 5195 if (nested) { 5196 kvm_cpu_cap_set(X86_FEATURE_SVM); 5197 kvm_cpu_cap_set(X86_FEATURE_VMCBCLEAN); 5198 5199 /* 5200 * KVM currently flushes TLBs on *every* nested SVM transition, 5201 * and so for all intents and purposes KVM supports flushing by 5202 * ASID, i.e. KVM is guaranteed to honor every L1 ASID flush. 5203 */ 5204 kvm_cpu_cap_set(X86_FEATURE_FLUSHBYASID); 5205 5206 if (nrips) 5207 kvm_cpu_cap_set(X86_FEATURE_NRIPS); 5208 5209 if (npt_enabled) 5210 kvm_cpu_cap_set(X86_FEATURE_NPT); 5211 5212 if (tsc_scaling) 5213 kvm_cpu_cap_set(X86_FEATURE_TSCRATEMSR); 5214 5215 if (vls) 5216 kvm_cpu_cap_set(X86_FEATURE_V_VMSAVE_VMLOAD); 5217 if (lbrv) 5218 kvm_cpu_cap_set(X86_FEATURE_LBRV); 5219 5220 if (boot_cpu_has(X86_FEATURE_PAUSEFILTER)) 5221 kvm_cpu_cap_set(X86_FEATURE_PAUSEFILTER); 5222 5223 if (boot_cpu_has(X86_FEATURE_PFTHRESHOLD)) 5224 kvm_cpu_cap_set(X86_FEATURE_PFTHRESHOLD); 5225 5226 if (vgif) 5227 kvm_cpu_cap_set(X86_FEATURE_VGIF); 5228 5229 if (vnmi) 5230 kvm_cpu_cap_set(X86_FEATURE_VNMI); 5231 5232 /* Nested VM can receive #VMEXIT instead of triggering #GP */ 5233 kvm_cpu_cap_set(X86_FEATURE_SVME_ADDR_CHK); 5234 } 5235 5236 if (cpu_feature_enabled(X86_FEATURE_BUS_LOCK_THRESHOLD)) 5237 kvm_caps.has_bus_lock_exit = true; 5238 5239 /* CPUID 0x80000008 */ 5240 if (boot_cpu_has(X86_FEATURE_LS_CFG_SSBD) || 5241 boot_cpu_has(X86_FEATURE_AMD_SSBD)) 5242 kvm_cpu_cap_set(X86_FEATURE_VIRT_SSBD); 5243 5244 if (enable_pmu) { 5245 /* 5246 * Enumerate support for PERFCTR_CORE if and only if KVM has 5247 * access to enough counters to virtualize "core" support, 5248 * otherwise limit vPMU support to the legacy number of counters. 5249 */ 5250 if (kvm_pmu_cap.num_counters_gp < AMD64_NUM_COUNTERS_CORE) 5251 kvm_pmu_cap.num_counters_gp = min(AMD64_NUM_COUNTERS, 5252 kvm_pmu_cap.num_counters_gp); 5253 else 5254 kvm_cpu_cap_check_and_set(X86_FEATURE_PERFCTR_CORE); 5255 5256 if (kvm_pmu_cap.version != 2 || 5257 !kvm_cpu_cap_has(X86_FEATURE_PERFCTR_CORE)) 5258 kvm_cpu_cap_clear(X86_FEATURE_PERFMON_V2); 5259 } 5260 5261 /* CPUID 0x8000001F (SME/SEV features) */ 5262 sev_set_cpu_caps(); 5263 5264 /* 5265 * Clear capabilities that are automatically configured by common code, 5266 * but that require explicit SVM support (that isn't yet implemented). 5267 */ 5268 kvm_cpu_cap_clear(X86_FEATURE_BUS_LOCK_DETECT); 5269 kvm_cpu_cap_clear(X86_FEATURE_MSR_IMM); 5270 } 5271 5272 static __init int svm_hardware_setup(void) 5273 { 5274 void *iopm_va; 5275 int cpu, r; 5276 5277 /* 5278 * NX is required for shadow paging and for NPT if the NX huge pages 5279 * mitigation is enabled. 5280 */ 5281 if (!boot_cpu_has(X86_FEATURE_NX)) { 5282 pr_err_ratelimited("NX (Execute Disable) not supported\n"); 5283 return -EOPNOTSUPP; 5284 } 5285 kvm_enable_efer_bits(EFER_NX); 5286 5287 kvm_caps.supported_xcr0 &= ~(XFEATURE_MASK_BNDREGS | 5288 XFEATURE_MASK_BNDCSR); 5289 5290 if (boot_cpu_has(X86_FEATURE_FXSR_OPT)) 5291 kvm_enable_efer_bits(EFER_FFXSR); 5292 5293 if (tsc_scaling) { 5294 if (!boot_cpu_has(X86_FEATURE_TSCRATEMSR)) { 5295 tsc_scaling = false; 5296 } else { 5297 pr_info("TSC scaling supported\n"); 5298 kvm_caps.has_tsc_control = true; 5299 } 5300 } 5301 kvm_caps.max_tsc_scaling_ratio = SVM_TSC_RATIO_MAX; 5302 kvm_caps.tsc_scaling_ratio_frac_bits = 32; 5303 5304 tsc_aux_uret_slot = kvm_add_user_return_msr(MSR_TSC_AUX); 5305 5306 if (boot_cpu_has(X86_FEATURE_AUTOIBRS)) 5307 kvm_enable_efer_bits(EFER_AUTOIBRS); 5308 5309 /* Check for pause filtering support */ 5310 if (!boot_cpu_has(X86_FEATURE_PAUSEFILTER)) { 5311 pause_filter_count = 0; 5312 pause_filter_thresh = 0; 5313 } else if (!boot_cpu_has(X86_FEATURE_PFTHRESHOLD)) { 5314 pause_filter_thresh = 0; 5315 } 5316 5317 if (nested) { 5318 pr_info("Nested Virtualization enabled\n"); 5319 kvm_enable_efer_bits(EFER_SVME | EFER_LMSLE); 5320 5321 r = nested_svm_init_msrpm_merge_offsets(); 5322 if (r) 5323 return r; 5324 } 5325 5326 /* 5327 * KVM's MMU doesn't support using 2-level paging for itself, and thus 5328 * NPT isn't supported if the host is using 2-level paging since host 5329 * CR4 is unchanged on VMRUN. 5330 */ 5331 if (!IS_ENABLED(CONFIG_X86_64) && !IS_ENABLED(CONFIG_X86_PAE)) 5332 npt_enabled = false; 5333 5334 if (!boot_cpu_has(X86_FEATURE_NPT)) 5335 npt_enabled = false; 5336 5337 /* Force VM NPT level equal to the host's paging level */ 5338 kvm_configure_mmu(npt_enabled, get_npt_level(), 5339 get_npt_level(), PG_LEVEL_1G); 5340 pr_info("Nested Paging %s\n", str_enabled_disabled(npt_enabled)); 5341 5342 /* 5343 * It seems that on AMD processors PTE's accessed bit is 5344 * being set by the CPU hardware before the NPF vmexit. 5345 * This is not expected behaviour and our tests fail because 5346 * of it. 5347 * A workaround here is to disable support for 5348 * GUEST_MAXPHYADDR < HOST_MAXPHYADDR if NPT is enabled. 5349 * In this case userspace can know if there is support using 5350 * KVM_CAP_SMALLER_MAXPHYADDR extension and decide how to handle 5351 * it 5352 * If future AMD CPU models change the behaviour described above, 5353 * this variable can be changed accordingly 5354 */ 5355 allow_smaller_maxphyaddr = !npt_enabled; 5356 5357 /* Setup shadow_me_value and shadow_me_mask */ 5358 kvm_mmu_set_me_spte_mask(sme_me_mask, sme_me_mask); 5359 5360 svm_adjust_mmio_mask(); 5361 5362 nrips = nrips && boot_cpu_has(X86_FEATURE_NRIPS); 5363 5364 if (lbrv) { 5365 if (!boot_cpu_has(X86_FEATURE_LBRV)) 5366 lbrv = false; 5367 else 5368 pr_info("LBR virtualization supported\n"); 5369 } 5370 5371 iopm_va = svm_alloc_permissions_map(IOPM_SIZE, GFP_KERNEL); 5372 if (!iopm_va) 5373 return -ENOMEM; 5374 5375 iopm_base = __sme_set(__pa(iopm_va)); 5376 5377 /* 5378 * Note, SEV setup consumes npt_enabled and enable_mmio_caching (which 5379 * may be modified by svm_adjust_mmio_mask()), as well as nrips. 5380 */ 5381 sev_hardware_setup(); 5382 5383 svm_hv_hardware_setup(); 5384 5385 enable_apicv = avic_hardware_setup(); 5386 if (!enable_apicv) { 5387 enable_ipiv = false; 5388 svm_x86_ops.vcpu_blocking = NULL; 5389 svm_x86_ops.vcpu_unblocking = NULL; 5390 svm_x86_ops.vcpu_get_apicv_inhibit_reasons = NULL; 5391 } 5392 5393 if (vls) { 5394 if (!npt_enabled || 5395 !boot_cpu_has(X86_FEATURE_V_VMSAVE_VMLOAD) || 5396 !IS_ENABLED(CONFIG_X86_64)) { 5397 vls = false; 5398 } else { 5399 pr_info("Virtual VMLOAD VMSAVE supported\n"); 5400 } 5401 } 5402 5403 if (boot_cpu_has(X86_FEATURE_SVME_ADDR_CHK)) 5404 svm_gp_erratum_intercept = false; 5405 5406 if (vgif) { 5407 if (!boot_cpu_has(X86_FEATURE_VGIF)) 5408 vgif = false; 5409 else 5410 pr_info("Virtual GIF supported\n"); 5411 } 5412 5413 vnmi = vgif && vnmi && boot_cpu_has(X86_FEATURE_VNMI); 5414 if (vnmi) 5415 pr_info("Virtual NMI enabled\n"); 5416 5417 if (!vnmi) { 5418 svm_x86_ops.is_vnmi_pending = NULL; 5419 svm_x86_ops.set_vnmi_pending = NULL; 5420 } 5421 5422 if (!enable_pmu) 5423 pr_info("PMU virtualization is disabled\n"); 5424 5425 svm_set_cpu_caps(); 5426 5427 kvm_caps.inapplicable_quirks &= ~KVM_X86_QUIRK_CD_NW_CLEARED; 5428 5429 for_each_possible_cpu(cpu) { 5430 r = svm_cpu_init(cpu); 5431 if (r) 5432 goto err; 5433 } 5434 5435 return 0; 5436 5437 err: 5438 svm_hardware_unsetup(); 5439 return r; 5440 } 5441 5442 5443 static struct kvm_x86_init_ops svm_init_ops __initdata = { 5444 .hardware_setup = svm_hardware_setup, 5445 5446 .runtime_ops = &svm_x86_ops, 5447 .pmu_ops = &amd_pmu_ops, 5448 }; 5449 5450 static void __svm_exit(void) 5451 { 5452 kvm_x86_vendor_exit(); 5453 } 5454 5455 static int __init svm_init(void) 5456 { 5457 int r; 5458 5459 KVM_SANITY_CHECK_VM_STRUCT_SIZE(kvm_svm); 5460 5461 __unused_size_checks(); 5462 5463 if (!kvm_is_svm_supported()) 5464 return -EOPNOTSUPP; 5465 5466 r = kvm_x86_vendor_init(&svm_init_ops); 5467 if (r) 5468 return r; 5469 5470 /* 5471 * Common KVM initialization _must_ come last, after this, /dev/kvm is 5472 * exposed to userspace! 5473 */ 5474 r = kvm_init(sizeof(struct vcpu_svm), __alignof__(struct vcpu_svm), 5475 THIS_MODULE); 5476 if (r) 5477 goto err_kvm_init; 5478 5479 return 0; 5480 5481 err_kvm_init: 5482 __svm_exit(); 5483 return r; 5484 } 5485 5486 static void __exit svm_exit(void) 5487 { 5488 kvm_exit(); 5489 __svm_exit(); 5490 } 5491 5492 module_init(svm_init) 5493 module_exit(svm_exit) 5494