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