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