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