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