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