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