1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * Kernel-based Virtual Machine driver for Linux 4 * 5 * AMD SVM support 6 * 7 * Copyright (C) 2006 Qumranet, Inc. 8 * Copyright 2010 Red Hat, Inc. and/or its affiliates. 9 * 10 * Authors: 11 * Yaniv Kamay <yaniv@qumranet.com> 12 * Avi Kivity <avi@qumranet.com> 13 */ 14 15 #ifndef __SVM_SVM_H 16 #define __SVM_SVM_H 17 18 #include <linux/kvm_types.h> 19 #include <linux/kvm_host.h> 20 #include <linux/bits.h> 21 22 #include <asm/svm.h> 23 #include <asm/sev-common.h> 24 25 #include "cpuid.h" 26 #include "regs.h" 27 #include "x86.h" 28 #include "pmu.h" 29 30 /* 31 * Helpers to convert to/from physical addresses for pages whose address is 32 * consumed directly by hardware. Even though it's a physical address, SVM 33 * often restricts the address to the natural width, hence 'unsigned long' 34 * instead of 'hpa_t'. 35 */ 36 static inline unsigned long __sme_page_pa(struct page *page) 37 { 38 return __sme_set(page_to_pfn(page) << PAGE_SHIFT); 39 } 40 41 static inline struct page *__sme_pa_to_page(unsigned long pa) 42 { 43 return pfn_to_page(__sme_clr(pa) >> PAGE_SHIFT); 44 } 45 46 #define IOPM_SIZE PAGE_SIZE * 3 47 #define MSRPM_SIZE PAGE_SIZE * 2 48 49 extern bool gmet_enabled; 50 extern bool npt_enabled; 51 extern int nrips; 52 extern int vgif; 53 extern bool intercept_smi; 54 extern bool vnmi; 55 extern int lbrv; 56 57 extern int tsc_aux_uret_slot __ro_after_init; 58 59 extern struct kvm_x86_ops svm_x86_ops __initdata; 60 61 /* 62 * Clean bits in VMCB. 63 * VMCB_ALL_CLEAN_MASK might also need to 64 * be updated if this enum is modified. 65 */ 66 enum { 67 VMCB_INTERCEPTS, /* Intercept vectors, TSC offset, 68 pause filter count */ 69 VMCB_PERM_MAP, /* IOPM Base and MSRPM Base */ 70 VMCB_ASID, /* ASID */ 71 VMCB_INTR, /* int_ctl, int_vector */ 72 VMCB_NPT, /* npt_en, nCR3, gPAT */ 73 VMCB_CR, /* CR0, CR3, CR4, EFER */ 74 VMCB_DR, /* DR6, DR7 */ 75 VMCB_DT, /* GDT, IDT */ 76 VMCB_SEG, /* CS, DS, SS, ES, CPL */ 77 VMCB_CR2, /* CR2 only */ 78 VMCB_LBR, /* DBGCTL, BR_FROM, BR_TO, LAST_EX_FROM, LAST_EX_TO */ 79 VMCB_AVIC, /* AVIC APIC_BAR, AVIC APIC_BACKING_PAGE, 80 * AVIC PHYSICAL_TABLE pointer, 81 * AVIC LOGICAL_TABLE pointer 82 */ 83 VMCB_CET, /* S_CET, SSP, ISST_ADDR */ 84 VMCB_SW = 31, /* Reserved for hypervisor/software use */ 85 }; 86 87 #define VMCB_ALL_CLEAN_MASK ( \ 88 (1U << VMCB_INTERCEPTS) | (1U << VMCB_PERM_MAP) | \ 89 (1U << VMCB_ASID) | (1U << VMCB_INTR) | \ 90 (1U << VMCB_NPT) | (1U << VMCB_CR) | (1U << VMCB_DR) | \ 91 (1U << VMCB_DT) | (1U << VMCB_SEG) | (1U << VMCB_CR2) | \ 92 (1U << VMCB_LBR) | (1U << VMCB_AVIC) | (1U << VMCB_CET) | \ 93 (1U << VMCB_SW)) 94 95 /* TPR and CR2 are always written before VMRUN */ 96 #define VMCB_ALWAYS_DIRTY_MASK ((1U << VMCB_INTR) | (1U << VMCB_CR2)) 97 98 #ifdef CONFIG_KVM_AMD_SEV 99 struct kvm_sev_info { 100 bool active; /* SEV enabled guest */ 101 bool es_active; /* SEV-ES enabled guest */ 102 bool need_init; /* waiting for SEV_INIT2 */ 103 unsigned int asid; /* ASID used for this guest */ 104 unsigned int handle; /* SEV firmware handle */ 105 int fd; /* SEV device fd */ 106 unsigned long policy; 107 unsigned long pages_locked; /* Number of pages locked */ 108 struct list_head regions_list; /* List of registered regions */ 109 u64 ap_jump_table; /* SEV-ES AP Jump Table address */ 110 u64 vmsa_features; 111 u16 ghcb_version; /* Highest guest GHCB protocol version allowed */ 112 /* The three fields below are protected by sev_mirror_lock */ 113 struct kvm *enc_context_owner; /* Owner of copied encryption context */ 114 struct list_head mirror_vms; /* List of VMs mirroring */ 115 struct list_head mirror_entry; /* Use as a list entry of mirrors */ 116 struct misc_cg *misc_cg; /* For misc cgroup accounting */ 117 atomic_t migration_in_progress; 118 void *snp_context; /* SNP guest context page */ 119 void *guest_req_buf; /* Bounce buffer for SNP Guest Request input */ 120 void *guest_resp_buf; /* Bounce buffer for SNP Guest Request output */ 121 struct mutex guest_req_mutex; /* Must acquire before using bounce buffers */ 122 cpumask_var_t have_run_cpus; /* CPUs that have done VMRUN for this VM. */ 123 bool snp_certs_enabled; /* SNP certificate-fetching support. */ 124 }; 125 #endif 126 127 struct kvm_svm { 128 struct kvm kvm; 129 130 /* Struct members for AVIC */ 131 u32 avic_vm_id; 132 u32 *avic_logical_id_table; 133 u64 *avic_physical_id_table; 134 struct hlist_node hnode; 135 136 #ifdef CONFIG_KVM_AMD_SEV 137 struct kvm_sev_info sev_info; 138 #endif 139 }; 140 141 struct kvm_vcpu; 142 143 struct kvm_vmcb_info { 144 struct vmcb *ptr; 145 unsigned long pa; 146 int cpu; 147 uint64_t asid_generation; 148 }; 149 150 struct vmcb_save_area_cached { 151 struct vmcb_seg es; 152 struct vmcb_seg cs; 153 struct vmcb_seg ss; 154 struct vmcb_seg ds; 155 struct vmcb_seg gdtr; 156 struct vmcb_seg idtr; 157 u8 cpl; 158 u64 efer; 159 u64 cr4; 160 u64 cr3; 161 u64 cr0; 162 u64 dr7; 163 u64 dr6; 164 u64 rflags; 165 u64 rip; 166 u64 rsp; 167 u64 s_cet; 168 u64 ssp; 169 u64 isst_addr; 170 u64 rax; 171 u64 cr2; 172 u64 g_pat; 173 u64 dbgctl; 174 u64 br_from; 175 u64 br_to; 176 u64 last_excp_from; 177 u64 last_excp_to; 178 }; 179 180 struct vmcb_ctrl_area_cached { 181 u32 intercepts[MAX_INTERCEPT]; 182 u16 pause_filter_thresh; 183 u16 pause_filter_count; 184 u64 iopm_base_pa; 185 u64 msrpm_base_pa; 186 u64 tsc_offset; 187 u32 asid; 188 u8 tlb_ctl; 189 u8 erap_ctl; 190 u32 int_ctl; 191 u32 int_vector; 192 u32 int_state; 193 u64 exit_code; 194 u64 exit_info_1; 195 u64 exit_info_2; 196 u32 exit_int_info; 197 u32 exit_int_info_err; 198 u64 misc_ctl; 199 u32 event_inj; 200 u32 event_inj_err; 201 u64 next_rip; 202 u64 nested_cr3; 203 u64 misc_ctl2; 204 u32 clean; 205 union { 206 #if IS_ENABLED(CONFIG_HYPERV) || IS_ENABLED(CONFIG_KVM_HYPERV) 207 struct hv_vmcb_enlightenments hv_enlightenments; 208 #endif 209 u8 reserved_sw[32]; 210 }; 211 }; 212 213 struct svm_nested_state { 214 struct kvm_vmcb_info vmcb02; 215 u64 hsave_msr; 216 u64 vm_cr_msr; 217 u64 vmcb12_gpa; 218 u64 last_vmcb12_gpa; 219 u64 last_bus_lock_rip; 220 221 /* 222 * The MSR permissions map used for vmcb02, which is the merge result 223 * of vmcb01 and vmcb12 224 */ 225 void *msrpm; 226 227 /* cache for control fields of the guest */ 228 struct vmcb_ctrl_area_cached ctl; 229 230 /* 231 * Note: this struct is not kept up-to-date while L2 runs; it is only 232 * valid within nested_svm_vmrun. 233 */ 234 struct vmcb_save_area_cached save; 235 236 bool initialized; 237 238 /* 239 * Indicates whether MSR bitmap for L2 needs to be rebuilt due to 240 * changes in MSR bitmap for L1 or switching to a different L2. Note, 241 * this flag can only be used reliably in conjunction with a paravirt L1 242 * which informs L0 whether any changes to MSR bitmap for L2 were done 243 * on its side. 244 */ 245 bool force_msr_bitmap_recalc; 246 }; 247 248 struct vcpu_sev_es_state { 249 /* SEV-ES support */ 250 struct sev_es_save_area *vmsa; 251 struct ghcb *ghcb; 252 u8 valid_bitmap[16]; 253 struct kvm_host_map ghcb_map; 254 bool received_first_sipi; 255 unsigned int ap_reset_hold_type; 256 257 /* SEV-ES scratch area support */ 258 u64 sw_scratch; 259 void *ghcb_sa; 260 u32 ghcb_sa_len; 261 bool ghcb_sa_sync; 262 bool ghcb_sa_free; 263 264 /* SNP Page-State-Change buffer entries currently being processed */ 265 struct { 266 u16 cur_idx; 267 u16 end_idx; 268 u16 batch_size; 269 bool is_2m; 270 } psc; 271 272 u64 ghcb_registered_gpa; 273 274 struct mutex snp_vmsa_mutex; /* Used to handle concurrent updates of VMSA. */ 275 gpa_t snp_pending_vmsa_gpa; 276 gpa_t snp_guest_vmsa_gpa; 277 bool snp_ap_waiting_for_reset; 278 bool snp_has_guest_vmsa; 279 }; 280 281 struct vcpu_svm { 282 struct kvm_vcpu vcpu; 283 /* vmcb always points at current_vmcb->ptr, it's purely a shorthand. */ 284 struct vmcb *vmcb; 285 struct kvm_vmcb_info vmcb01; 286 struct kvm_vmcb_info *current_vmcb; 287 u32 asid; 288 u32 sysenter_esp_hi; 289 u32 sysenter_eip_hi; 290 uint64_t tsc_aux; 291 292 u64 msr_decfg; 293 294 u64 next_rip; 295 296 u64 spec_ctrl; 297 298 u64 tsc_ratio_msr; 299 /* 300 * Contains guest-controlled bits of VIRT_SPEC_CTRL, which will be 301 * translated into the appropriate L2_CFG bits on the host to 302 * perform speculative control. 303 */ 304 u64 virt_spec_ctrl; 305 306 void *msrpm; 307 308 ulong nmi_iret_rip; 309 310 struct svm_nested_state nested; 311 312 /* NMI mask value, used when vNMI is not enabled */ 313 bool nmi_masked; 314 315 /* 316 * True when NMIs are still masked but guest IRET was just intercepted 317 * and KVM is waiting for RIP to change, which will signal that the 318 * intercepted IRET was retired and thus NMI can be unmasked. 319 */ 320 bool awaiting_iret_completion; 321 322 /* 323 * Set when KVM is awaiting IRET completion and needs to inject NMIs as 324 * soon as the IRET completes (e.g. NMI is pending injection). KVM 325 * temporarily steals RFLAGS.TF to single-step the guest in this case 326 * in order to regain control as soon as the NMI-blocking condition 327 * goes away. 328 */ 329 bool nmi_singlestep; 330 u64 nmi_singlestep_guest_rflags; 331 332 bool nmi_l1_to_l2; 333 334 unsigned long soft_int_csbase; 335 unsigned long soft_int_old_rip; 336 unsigned long soft_int_next_rip; 337 bool soft_int_injected; 338 339 u32 ldr_reg; 340 u32 dfr_reg; 341 342 /* This is essentially a shadow of the vCPU's actual entry in the 343 * Physical ID table that is programmed into the VMCB, i.e. that is 344 * seen by the CPU. If IPI virtualization is disabled, IsRunning is 345 * only ever set in the shadow, i.e. is never propagated to the "real" 346 * table, so that hardware never sees IsRunning=1. 347 */ 348 u64 avic_physical_id_entry; 349 350 /* 351 * Per-vCPU list of irqfds that are eligible to post IRQs directly to 352 * the vCPU (a.k.a. device posted IRQs, a.k.a. IRQ bypass). The list 353 * is used to reconfigure IRTEs when the vCPU is loaded/put (to set the 354 * target pCPU), when AVIC is toggled on/off (to (de)activate bypass), 355 * and if the irqfd becomes ineligible for posting (to put the IRTE 356 * back into remapped mode). 357 */ 358 struct list_head ir_list; 359 raw_spinlock_t ir_list_lock; 360 361 struct vcpu_sev_es_state sev_es; 362 363 bool guest_state_loaded; 364 365 bool avic_irq_window; 366 bool x2avic_msrs_intercepted; 367 bool lbr_msrs_intercepted; 368 369 /* Guest GIF value, used when vGIF is not enabled */ 370 bool guest_gif; 371 }; 372 373 struct svm_cpu_data { 374 u64 asid_generation; 375 u32 max_asid; 376 u32 next_asid; 377 u32 min_asid; 378 379 bool bp_spec_reduce_set; 380 381 struct vmcb *save_area; 382 unsigned long save_area_pa; 383 384 /* index = sev_asid, value = vmcb pointer */ 385 struct vmcb **sev_vmcbs; 386 }; 387 388 DECLARE_PER_CPU(struct svm_cpu_data, svm_data); 389 390 static __always_inline struct kvm_svm *to_kvm_svm(struct kvm *kvm) 391 { 392 return container_of(kvm, struct kvm_svm, kvm); 393 } 394 395 #ifdef CONFIG_KVM_AMD_SEV 396 static __always_inline struct kvm_sev_info *to_kvm_sev_info(struct kvm *kvm) 397 { 398 return &to_kvm_svm(kvm)->sev_info; 399 } 400 401 static __always_inline bool ____sev_guest(struct kvm *kvm) 402 { 403 return to_kvm_sev_info(kvm)->active; 404 } 405 static __always_inline bool ____sev_es_guest(struct kvm *kvm) 406 { 407 struct kvm_sev_info *sev = to_kvm_sev_info(kvm); 408 409 return sev->es_active && !WARN_ON_ONCE(!sev->active); 410 } 411 412 static __always_inline bool ____sev_snp_guest(struct kvm *kvm) 413 { 414 struct kvm_sev_info *sev = to_kvm_sev_info(kvm); 415 416 return (sev->vmsa_features & SVM_SEV_FEAT_SNP_ACTIVE) && 417 !WARN_ON_ONCE(!____sev_es_guest(kvm)); 418 } 419 420 static __always_inline bool is_sev_guest(struct kvm_vcpu *vcpu) 421 { 422 return ____sev_guest(vcpu->kvm); 423 } 424 static __always_inline bool is_sev_es_guest(struct kvm_vcpu *vcpu) 425 { 426 return ____sev_es_guest(vcpu->kvm); 427 } 428 429 static __always_inline bool is_sev_snp_guest(struct kvm_vcpu *vcpu) 430 { 431 return ____sev_snp_guest(vcpu->kvm); 432 } 433 #else 434 static __always_inline bool is_sev_guest(struct kvm_vcpu *vcpu) 435 { 436 return false; 437 } 438 static __always_inline bool is_sev_es_guest(struct kvm_vcpu *vcpu) 439 { 440 return false; 441 } 442 443 static __always_inline bool is_sev_snp_guest(struct kvm_vcpu *vcpu) 444 { 445 return false; 446 } 447 #endif 448 449 static inline bool ghcb_gpa_is_registered(struct vcpu_svm *svm, u64 val) 450 { 451 return svm->sev_es.ghcb_registered_gpa == val; 452 } 453 454 static inline void vmcb_mark_all_dirty(struct vmcb *vmcb) 455 { 456 vmcb->control.clean = 0; 457 } 458 459 static inline void vmcb_mark_all_clean(struct vmcb *vmcb) 460 { 461 vmcb->control.clean = VMCB_ALL_CLEAN_MASK 462 & ~VMCB_ALWAYS_DIRTY_MASK; 463 } 464 465 static inline void vmcb_mark_dirty(struct vmcb *vmcb, int bit) 466 { 467 vmcb->control.clean &= ~(1 << bit); 468 } 469 470 static inline bool vmcb12_is_dirty(struct vmcb_ctrl_area_cached *control, int bit) 471 { 472 return !test_bit(bit, (unsigned long *)&control->clean); 473 } 474 475 static inline void vmcb_set_gpat(struct vmcb *vmcb, u64 data) 476 { 477 vmcb->save.g_pat = data; 478 vmcb_mark_dirty(vmcb, VMCB_NPT); 479 } 480 481 static __always_inline struct vcpu_svm *to_svm(struct kvm_vcpu *vcpu) 482 { 483 return container_of(vcpu, struct vcpu_svm, vcpu); 484 } 485 486 static inline bool svm_is_vmrun_failure(u64 exit_code) 487 { 488 if (cpu_feature_enabled(X86_FEATURE_HYPERVISOR)) 489 return (u32)exit_code == (u32)SVM_EXIT_ERR; 490 491 return exit_code == SVM_EXIT_ERR; 492 } 493 494 /* 495 * Only the PDPTRs are loaded on demand into the shadow MMU. All other 496 * fields are synchronized on VM-Exit, because accessing the VMCB is cheap. 497 * 498 * CR3 might be out of date in the VMCB but it is not marked dirty; instead, 499 * KVM_REQ_LOAD_MMU_PGD is always requested when the cached vcpu->arch.cr3 500 * is changed. svm_load_mmu_pgd() then syncs the new CR3 value into the VMCB. 501 */ 502 #define SVM_REGS_LAZY_LOAD_SET (BIT(VCPU_REG_PDPTR)) 503 504 static inline void __vmcb_set_intercept(unsigned long *intercepts, u32 bit) 505 { 506 WARN_ON_ONCE(bit >= 32 * MAX_INTERCEPT); 507 __set_bit(bit, intercepts); 508 } 509 510 static inline void __vmcb_clr_intercept(unsigned long *intercepts, u32 bit) 511 { 512 WARN_ON_ONCE(bit >= 32 * MAX_INTERCEPT); 513 __clear_bit(bit, intercepts); 514 } 515 516 static inline bool __vmcb_is_intercept(unsigned long *intercepts, u32 bit) 517 { 518 WARN_ON_ONCE(bit >= 32 * MAX_INTERCEPT); 519 return test_bit(bit, intercepts); 520 } 521 522 static inline void vmcb_set_intercept(struct vmcb_control_area *control, u32 bit) 523 { 524 __vmcb_set_intercept((unsigned long *)&control->intercepts, bit); 525 } 526 527 static inline void vmcb_clr_intercept(struct vmcb_control_area *control, u32 bit) 528 { 529 __vmcb_clr_intercept((unsigned long *)&control->intercepts, bit); 530 } 531 532 static inline bool vmcb_is_intercept(struct vmcb_control_area *control, u32 bit) 533 { 534 return __vmcb_is_intercept((unsigned long *)&control->intercepts, bit); 535 } 536 537 static inline void vmcb12_clr_intercept(struct vmcb_ctrl_area_cached *control, u32 bit) 538 { 539 __vmcb_clr_intercept((unsigned long *)&control->intercepts, bit); 540 } 541 542 static inline bool vmcb12_is_intercept(struct vmcb_ctrl_area_cached *control, u32 bit) 543 { 544 return __vmcb_is_intercept((unsigned long *)&control->intercepts, bit); 545 } 546 547 void nested_vmcb02_recalc_intercepts(struct vcpu_svm *svm); 548 549 static inline void svm_mark_intercepts_dirty(struct vcpu_svm *svm) 550 { 551 vmcb_mark_dirty(svm->vmcb01.ptr, VMCB_INTERCEPTS); 552 553 /* 554 * If L2 is active, recalculate the intercepts for vmcb02 to account 555 * for the changes made to vmcb01. All intercept configuration is done 556 * for vmcb01 and then propagated to vmcb02 to combine KVM's intercepts 557 * with L1's intercepts (from the vmcb12 snapshot). 558 */ 559 if (is_guest_mode(&svm->vcpu)) 560 nested_vmcb02_recalc_intercepts(svm); 561 } 562 563 static inline void set_exception_intercept(struct vcpu_svm *svm, u32 bit) 564 { 565 struct vmcb *vmcb = svm->vmcb01.ptr; 566 567 WARN_ON_ONCE(bit >= 32); 568 vmcb_set_intercept(&vmcb->control, INTERCEPT_EXCEPTION_OFFSET + bit); 569 570 svm_mark_intercepts_dirty(svm); 571 } 572 573 static inline void clr_exception_intercept(struct vcpu_svm *svm, u32 bit) 574 { 575 struct vmcb *vmcb = svm->vmcb01.ptr; 576 577 WARN_ON_ONCE(bit >= 32); 578 vmcb_clr_intercept(&vmcb->control, INTERCEPT_EXCEPTION_OFFSET + bit); 579 580 svm_mark_intercepts_dirty(svm); 581 } 582 583 static inline void svm_set_intercept(struct vcpu_svm *svm, int bit) 584 { 585 struct vmcb *vmcb = svm->vmcb01.ptr; 586 587 vmcb_set_intercept(&vmcb->control, bit); 588 589 svm_mark_intercepts_dirty(svm); 590 } 591 592 static inline void svm_clr_intercept(struct vcpu_svm *svm, int bit) 593 { 594 struct vmcb *vmcb = svm->vmcb01.ptr; 595 596 vmcb_clr_intercept(&vmcb->control, bit); 597 598 svm_mark_intercepts_dirty(svm); 599 } 600 601 static inline bool svm_is_intercept(struct vcpu_svm *svm, int bit) 602 { 603 return vmcb_is_intercept(&svm->vmcb->control, bit); 604 } 605 606 static inline bool nested_vgif_enabled(struct vcpu_svm *svm) 607 { 608 return guest_cpu_cap_has(&svm->vcpu, X86_FEATURE_VGIF) && 609 (svm->nested.ctl.int_ctl & V_GIF_ENABLE_MASK); 610 } 611 612 static inline struct vmcb *get_vgif_vmcb(struct vcpu_svm *svm) 613 { 614 if (!vgif) 615 return NULL; 616 617 if (is_guest_mode(&svm->vcpu) && !nested_vgif_enabled(svm)) 618 return svm->nested.vmcb02.ptr; 619 else 620 return svm->vmcb01.ptr; 621 } 622 623 static inline void enable_gif(struct vcpu_svm *svm) 624 { 625 struct vmcb *vmcb = get_vgif_vmcb(svm); 626 627 if (vmcb) 628 vmcb->control.int_ctl |= V_GIF_MASK; 629 else 630 svm->guest_gif = true; 631 } 632 633 static inline void disable_gif(struct vcpu_svm *svm) 634 { 635 struct vmcb *vmcb = get_vgif_vmcb(svm); 636 637 if (vmcb) 638 vmcb->control.int_ctl &= ~V_GIF_MASK; 639 else 640 svm->guest_gif = false; 641 } 642 643 static inline bool gif_set(struct vcpu_svm *svm) 644 { 645 struct vmcb *vmcb = get_vgif_vmcb(svm); 646 647 if (vmcb) 648 return !!(vmcb->control.int_ctl & V_GIF_MASK); 649 else 650 return svm->guest_gif; 651 } 652 653 static inline bool nested_npt_enabled(struct vcpu_svm *svm) 654 { 655 return svm->nested.ctl.misc_ctl & SVM_MISC_ENABLE_NP; 656 } 657 658 static inline bool l2_has_separate_pat(struct kvm_vcpu *vcpu) 659 { 660 /* 661 * If KVM_X86_QUIRK_NESTED_SVM_SHARED_PAT is disabled while a vCPU 662 * is running, the L2 IA32_PAT semantics for that vCPU are undefined. 663 */ 664 return nested_npt_enabled(to_svm(vcpu)) && 665 !kvm_check_has_quirk(vcpu->kvm, KVM_X86_QUIRK_NESTED_SVM_SHARED_PAT); 666 } 667 668 static inline bool nested_vnmi_enabled(struct vcpu_svm *svm) 669 { 670 return guest_cpu_cap_has(&svm->vcpu, X86_FEATURE_VNMI) && 671 (svm->nested.ctl.int_ctl & V_NMI_ENABLE_MASK); 672 } 673 674 static inline bool is_x2apic_msrpm_offset(u32 offset) 675 { 676 /* 4 msrs per u8, and 4 u8 in u32 */ 677 u32 msr = offset * 16; 678 679 return (msr >= APIC_BASE_MSR) && 680 (msr < (APIC_BASE_MSR + 0x100)); 681 } 682 683 static inline struct vmcb *get_vnmi_vmcb_l1(struct vcpu_svm *svm) 684 { 685 if (!vnmi) 686 return NULL; 687 688 if (is_guest_mode(&svm->vcpu)) 689 return NULL; 690 else 691 return svm->vmcb01.ptr; 692 } 693 694 static inline bool is_vnmi_enabled(struct vcpu_svm *svm) 695 { 696 struct vmcb *vmcb = get_vnmi_vmcb_l1(svm); 697 698 if (vmcb) 699 return !!(vmcb->control.int_ctl & V_NMI_ENABLE_MASK); 700 else 701 return false; 702 } 703 704 static inline void svm_vmgexit_set_return_code(struct vcpu_svm *svm, 705 u64 response, u64 data) 706 { 707 ghcb_set_sw_exit_info_1(svm->sev_es.ghcb, response); 708 ghcb_set_sw_exit_info_2(svm->sev_es.ghcb, data); 709 } 710 711 static inline void svm_vmgexit_inject_exception(struct vcpu_svm *svm, u8 vector) 712 { 713 u64 data = SVM_EVTINJ_VALID | SVM_EVTINJ_TYPE_EXEPT | vector; 714 715 svm_vmgexit_set_return_code(svm, GHCB_HV_RESP_ISSUE_EXCEPTION, data); 716 } 717 718 static inline void svm_vmgexit_bad_input(struct vcpu_svm *svm, u64 suberror) 719 { 720 svm_vmgexit_set_return_code(svm, GHCB_HV_RESP_MALFORMED_INPUT, suberror); 721 } 722 723 static inline void svm_vmgexit_success(struct vcpu_svm *svm, u64 data) 724 { 725 svm_vmgexit_set_return_code(svm, GHCB_HV_RESP_NO_ACTION, data); 726 } 727 728 static inline void svm_vmgexit_no_action(struct vcpu_svm *svm, u64 data) 729 { 730 svm_vmgexit_set_return_code(svm, GHCB_HV_RESP_NO_ACTION, data); 731 } 732 733 /* 734 * The MSRPM is 8KiB in size, divided into four 2KiB ranges (the fourth range 735 * is reserved). Each MSR within a range is covered by two bits, one each for 736 * read (bit 0) and write (bit 1), where a bit value of '1' means intercepted. 737 */ 738 #define SVM_MSRPM_BYTES_PER_RANGE 2048 739 #define SVM_BITS_PER_MSR 2 740 #define SVM_MSRS_PER_BYTE (BITS_PER_BYTE / SVM_BITS_PER_MSR) 741 #define SVM_MSRS_PER_RANGE (SVM_MSRPM_BYTES_PER_RANGE * SVM_MSRS_PER_BYTE) 742 static_assert(SVM_MSRS_PER_RANGE == 8192); 743 #define SVM_MSRPM_OFFSET_MASK (SVM_MSRS_PER_RANGE - 1) 744 745 static __always_inline int svm_msrpm_bit_nr(u32 msr) 746 { 747 int range_nr; 748 749 switch (msr & ~SVM_MSRPM_OFFSET_MASK) { 750 case 0: 751 range_nr = 0; 752 break; 753 case 0xc0000000: 754 range_nr = 1; 755 break; 756 case 0xc0010000: 757 range_nr = 2; 758 break; 759 default: 760 return -EINVAL; 761 } 762 763 return range_nr * SVM_MSRPM_BYTES_PER_RANGE * BITS_PER_BYTE + 764 (msr & SVM_MSRPM_OFFSET_MASK) * SVM_BITS_PER_MSR; 765 } 766 767 #define __BUILD_SVM_MSR_BITMAP_HELPER(rtype, action, bitop, access, bit_rw) \ 768 static inline rtype svm_##action##_msr_bitmap_##access(unsigned long *bitmap, \ 769 u32 msr) \ 770 { \ 771 int bit_nr; \ 772 \ 773 bit_nr = svm_msrpm_bit_nr(msr); \ 774 if (bit_nr < 0) \ 775 return (rtype)true; \ 776 \ 777 return bitop##_bit(bit_nr + bit_rw, bitmap); \ 778 } 779 780 #define BUILD_SVM_MSR_BITMAP_HELPERS(ret_type, action, bitop) \ 781 __BUILD_SVM_MSR_BITMAP_HELPER(ret_type, action, bitop, read, 0) \ 782 __BUILD_SVM_MSR_BITMAP_HELPER(ret_type, action, bitop, write, 1) 783 784 BUILD_SVM_MSR_BITMAP_HELPERS(bool, test, test) 785 BUILD_SVM_MSR_BITMAP_HELPERS(void, clear, __clear) 786 BUILD_SVM_MSR_BITMAP_HELPERS(void, set, __set) 787 788 #define DEBUGCTL_RESERVED_BITS (~DEBUGCTLMSR_LBR) 789 790 /* svm.c */ 791 extern bool dump_invalid_vmcb; 792 793 void *svm_alloc_permissions_map(unsigned long size, gfp_t gfp_mask); 794 795 static inline void *svm_vcpu_alloc_msrpm(void) 796 { 797 return svm_alloc_permissions_map(MSRPM_SIZE, GFP_KERNEL_ACCOUNT); 798 } 799 800 #define svm_copy_lbrs(to, from) \ 801 do { \ 802 (to)->dbgctl = (from)->dbgctl; \ 803 (to)->br_from = (from)->br_from; \ 804 (to)->br_to = (from)->br_to; \ 805 (to)->last_excp_from = (from)->last_excp_from; \ 806 (to)->last_excp_to = (from)->last_excp_to; \ 807 } while (0) 808 809 void svm_vcpu_free_msrpm(void *msrpm); 810 void svm_enable_lbrv(struct kvm_vcpu *vcpu); 811 void svm_update_lbrv(struct kvm_vcpu *vcpu); 812 813 int svm_set_efer(struct kvm_vcpu *vcpu, u64 efer); 814 void svm_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0); 815 void svm_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4); 816 void disable_nmi_singlestep(struct vcpu_svm *svm); 817 bool svm_smi_blocked(struct kvm_vcpu *vcpu); 818 bool svm_nmi_blocked(struct kvm_vcpu *vcpu); 819 bool svm_interrupt_blocked(struct kvm_vcpu *vcpu); 820 void svm_set_gif(struct vcpu_svm *svm, bool value); 821 int svm_invoke_exit_handler(struct kvm_vcpu *vcpu, u64 exit_code); 822 void set_msr_interception(struct kvm_vcpu *vcpu, u32 *msrpm, u32 msr, 823 int read, int write); 824 void svm_complete_interrupt_delivery(struct kvm_vcpu *vcpu, int delivery_mode, 825 int trig_mode, int vec); 826 827 void svm_set_intercept_for_msr(struct kvm_vcpu *vcpu, u32 msr, int type, bool set); 828 829 static inline void svm_disable_intercept_for_msr(struct kvm_vcpu *vcpu, 830 u32 msr, int type) 831 { 832 svm_set_intercept_for_msr(vcpu, msr, type, false); 833 } 834 835 static inline void svm_enable_intercept_for_msr(struct kvm_vcpu *vcpu, 836 u32 msr, int type) 837 { 838 svm_set_intercept_for_msr(vcpu, msr, type, true); 839 } 840 841 int svm_skip_emulated_instruction(struct kvm_vcpu *vcpu); 842 843 /* nested.c */ 844 845 #define NESTED_EXIT_HOST 0 /* Exit handled on host level */ 846 #define NESTED_EXIT_DONE 1 /* Exit caused nested vmexit */ 847 #define NESTED_EXIT_CONTINUE 2 /* Further checks needed */ 848 849 static inline bool nested_svm_virtualize_tpr(struct kvm_vcpu *vcpu) 850 { 851 struct vcpu_svm *svm = to_svm(vcpu); 852 853 return is_guest_mode(vcpu) && (svm->nested.ctl.int_ctl & V_INTR_MASKING_MASK); 854 } 855 856 static inline bool nested_exit_on_smi(struct vcpu_svm *svm) 857 { 858 return vmcb12_is_intercept(&svm->nested.ctl, INTERCEPT_SMI); 859 } 860 861 static inline bool nested_exit_on_intr(struct vcpu_svm *svm) 862 { 863 return vmcb12_is_intercept(&svm->nested.ctl, INTERCEPT_INTR); 864 } 865 866 static inline bool nested_exit_on_nmi(struct vcpu_svm *svm) 867 { 868 return vmcb12_is_intercept(&svm->nested.ctl, INTERCEPT_NMI); 869 } 870 871 int __init nested_svm_init_msrpm_merge_offsets(void); 872 873 int enter_svm_guest_mode(struct kvm_vcpu *vcpu, u64 vmcb_gpa, bool from_vmrun); 874 void svm_leave_nested(struct kvm_vcpu *vcpu); 875 void svm_free_nested(struct vcpu_svm *svm); 876 int svm_allocate_nested(struct vcpu_svm *svm); 877 int nested_svm_vmrun(struct kvm_vcpu *vcpu); 878 void svm_copy_vmrun_state(struct vmcb_save_area *to_save, 879 struct vmcb_save_area *from_save); 880 void svm_copy_vmloadsave_state(struct vmcb *to_vmcb, struct vmcb *from_vmcb); 881 void nested_svm_vmexit(struct vcpu_svm *svm); 882 883 static inline void nested_svm_simple_vmexit(struct vcpu_svm *svm, u32 exit_code) 884 { 885 svm->vmcb->control.exit_code = exit_code; 886 svm->vmcb->control.exit_info_1 = 0; 887 svm->vmcb->control.exit_info_2 = 0; 888 nested_svm_vmexit(svm); 889 } 890 891 int nested_svm_exit_handled(struct vcpu_svm *svm); 892 int nested_svm_check_permissions(struct kvm_vcpu *vcpu); 893 int nested_svm_check_cached_vmcb12(struct kvm_vcpu *vcpu); 894 int nested_svm_check_exception(struct vcpu_svm *svm, unsigned nr, 895 bool has_error_code, u32 error_code); 896 int nested_svm_exit_special(struct vcpu_svm *svm); 897 void nested_svm_update_tsc_ratio_msr(struct kvm_vcpu *vcpu); 898 void svm_write_tsc_multiplier(struct kvm_vcpu *vcpu); 899 void nested_copy_vmcb_control_to_cache(struct vcpu_svm *svm, 900 struct vmcb_control_area *control); 901 void nested_copy_vmcb_save_to_cache(struct vcpu_svm *svm, 902 struct vmcb_save_area *save); 903 void nested_sync_control_from_vmcb02(struct vcpu_svm *svm); 904 void svm_switch_vmcb(struct vcpu_svm *svm, struct kvm_vmcb_info *target_vmcb); 905 906 907 static inline void __svm_pmu_handle_nested_transition(struct vcpu_svm *svm, 908 bool defer) 909 { 910 struct kvm_pmu *pmu = vcpu_to_pmu(&svm->vcpu); 911 u64 counters = *(u64 *)pmu->pmc_has_mode_specific_enables; 912 913 __kvm_pmu_reprogram_counters(pmu, counters, defer); 914 } 915 916 static inline void svm_pmu_handle_nested_transition(struct vcpu_svm *svm) 917 { 918 /* 919 * Do NOT defer reprogramming the counters by default. Instructions 920 * causing a state change are counted based on the _new_ CPU state 921 * (e.g. a successful VMRUN is counted in guest mode). Hence, the 922 * counters should be reprogrammed with the new state _before_ the 923 * instruction is potentially counted upon emulation completion. 924 */ 925 __svm_pmu_handle_nested_transition(svm, false); 926 } 927 928 extern struct kvm_x86_nested_ops svm_nested_ops; 929 930 /* avic.c */ 931 #define AVIC_REQUIRED_APICV_INHIBITS \ 932 ( \ 933 BIT(APICV_INHIBIT_REASON_DISABLED) | \ 934 BIT(APICV_INHIBIT_REASON_ABSENT) | \ 935 BIT(APICV_INHIBIT_REASON_HYPERV) | \ 936 BIT(APICV_INHIBIT_REASON_NESTED) | \ 937 BIT(APICV_INHIBIT_REASON_IRQWIN) | \ 938 BIT(APICV_INHIBIT_REASON_PIT_REINJ) | \ 939 BIT(APICV_INHIBIT_REASON_BLOCKIRQ) | \ 940 BIT(APICV_INHIBIT_REASON_SEV) | \ 941 BIT(APICV_INHIBIT_REASON_PHYSICAL_ID_ALIASED) | \ 942 BIT(APICV_INHIBIT_REASON_APIC_ID_MODIFIED) | \ 943 BIT(APICV_INHIBIT_REASON_APIC_BASE_MODIFIED) | \ 944 BIT(APICV_INHIBIT_REASON_LOGICAL_ID_ALIASED) | \ 945 BIT(APICV_INHIBIT_REASON_PHYSICAL_ID_TOO_BIG) \ 946 ) 947 948 bool __init avic_hardware_setup(void); 949 void avic_hardware_unsetup(void); 950 int avic_vcpu_precreate(struct kvm *kvm); 951 void avic_vm_pre_destroy(struct kvm *kvm); 952 void avic_vm_destroy(struct kvm *kvm); 953 void avic_init_vmcb(struct vcpu_svm *svm, struct vmcb *vmcb); 954 int avic_incomplete_ipi_interception(struct kvm_vcpu *vcpu); 955 int avic_unaccelerated_access_interception(struct kvm_vcpu *vcpu); 956 int avic_init_vcpu(struct vcpu_svm *svm); 957 void avic_vcpu_load(struct kvm_vcpu *vcpu, int cpu); 958 void avic_vcpu_put(struct kvm_vcpu *vcpu); 959 void avic_apicv_post_state_restore(struct kvm_vcpu *vcpu); 960 void avic_refresh_apicv_exec_ctrl(struct kvm_vcpu *vcpu); 961 int avic_pi_update_irte(struct kvm_kernel_irqfd *irqfd, struct kvm *kvm, 962 unsigned int host_irq, uint32_t guest_irq, 963 struct kvm_vcpu *vcpu, u32 vector); 964 void avic_vcpu_blocking(struct kvm_vcpu *vcpu); 965 void avic_vcpu_unblocking(struct kvm_vcpu *vcpu); 966 void avic_ring_doorbell(struct kvm_vcpu *vcpu); 967 unsigned long avic_vcpu_get_apicv_inhibit_reasons(struct kvm_vcpu *vcpu); 968 void avic_refresh_virtual_apic_mode(struct kvm_vcpu *vcpu); 969 970 971 /* sev.c */ 972 973 int pre_sev_run(struct vcpu_svm *svm, int cpu); 974 void sev_init_vmcb(struct vcpu_svm *svm, bool init_event); 975 void sev_vcpu_after_set_cpuid(struct vcpu_svm *svm); 976 int sev_es_string_io(struct vcpu_svm *svm, int size, unsigned int port, int in); 977 void sev_es_recalc_msr_intercepts(struct kvm_vcpu *vcpu); 978 void sev_vcpu_deliver_sipi_vector(struct kvm_vcpu *vcpu, u8 vector); 979 void sev_es_prepare_switch_to_guest(struct vcpu_svm *svm, struct sev_es_save_area *hostsa); 980 void sev_es_unmap_ghcb(struct vcpu_svm *svm); 981 982 #ifdef CONFIG_KVM_AMD_SEV 983 bool sev_vcpu_needs_initialization(struct kvm_vcpu *vcpu); 984 int sev_mem_enc_ioctl(struct kvm *kvm, void __user *argp); 985 int sev_mem_enc_register_region(struct kvm *kvm, 986 struct kvm_enc_region *range); 987 int sev_mem_enc_unregister_region(struct kvm *kvm, 988 struct kvm_enc_region *range); 989 int sev_vm_copy_enc_context_from(struct kvm *kvm, unsigned int source_fd); 990 int sev_vm_move_enc_context_from(struct kvm *kvm, unsigned int source_fd); 991 void sev_guest_memory_reclaimed(struct kvm *kvm); 992 int sev_handle_vmgexit(struct kvm_vcpu *vcpu); 993 994 /* These symbols are used in common code and are stubbed below. */ 995 996 struct page *snp_safe_alloc_page_node(int node, gfp_t gfp); 997 static inline struct page *snp_safe_alloc_page(void) 998 { 999 return snp_safe_alloc_page_node(numa_node_id(), GFP_KERNEL_ACCOUNT); 1000 } 1001 void sev_snp_reload_vmsa(struct kvm_vcpu *vcpu); 1002 1003 int sev_vcpu_create(struct kvm_vcpu *vcpu); 1004 void sev_free_vcpu(struct kvm_vcpu *vcpu); 1005 void sev_vm_init(struct kvm *kvm); 1006 void sev_vm_destroy(struct kvm *kvm); 1007 void __init sev_set_cpu_caps(void); 1008 void __init sev_hardware_setup(void); 1009 void sev_hardware_unsetup(void); 1010 int sev_cpu_init(struct svm_cpu_data *sd); 1011 int sev_dev_get_attr(u32 group, u64 attr, u64 *val); 1012 extern unsigned int max_sev_asid; 1013 void sev_handle_rmp_fault(struct kvm_vcpu *vcpu, gpa_t gpa, u64 error_code); 1014 int sev_gmem_make_private(struct kvm *kvm, gfn_t gfn, kvm_pfn_t pfn, kvm_pfn_t nr_pages); 1015 void sev_gmem_make_shared(kvm_pfn_t pfn, kvm_pfn_t nr_pages); 1016 void sev_gmem_invalidate_range(struct kvm *kvm, struct kvm_gfn_range *range); 1017 int sev_gmem_max_mapping_level(struct kvm *kvm, kvm_pfn_t pfn, bool is_private); 1018 struct vmcb_save_area *sev_decrypt_vmsa(struct kvm_vcpu *vcpu); 1019 void sev_free_decrypted_vmsa(struct kvm_vcpu *vcpu, struct vmcb_save_area *vmsa); 1020 #else 1021 static inline struct page *snp_safe_alloc_page_node(int node, gfp_t gfp) 1022 { 1023 return alloc_pages_node(node, gfp | __GFP_ZERO, 0); 1024 } 1025 1026 static inline struct page *snp_safe_alloc_page(void) 1027 { 1028 return snp_safe_alloc_page_node(numa_node_id(), GFP_KERNEL_ACCOUNT); 1029 } 1030 1031 static inline int sev_vcpu_create(struct kvm_vcpu *vcpu) { return 0; } 1032 static inline void sev_free_vcpu(struct kvm_vcpu *vcpu) {} 1033 static inline void sev_vm_init(struct kvm *kvm) {} 1034 static inline void sev_vm_destroy(struct kvm *kvm) {} 1035 static inline void __init sev_set_cpu_caps(void) {} 1036 static inline void __init sev_hardware_setup(void) {} 1037 static inline void sev_hardware_unsetup(void) {} 1038 static inline int sev_cpu_init(struct svm_cpu_data *sd) { return 0; } 1039 static inline int sev_dev_get_attr(u32 group, u64 attr, u64 *val) { return -ENXIO; } 1040 #define max_sev_asid 0 1041 static inline void sev_handle_rmp_fault(struct kvm_vcpu *vcpu, gpa_t gpa, u64 error_code) {} 1042 static inline struct vmcb_save_area *sev_decrypt_vmsa(struct kvm_vcpu *vcpu) 1043 { 1044 return NULL; 1045 } 1046 static inline void sev_free_decrypted_vmsa(struct kvm_vcpu *vcpu, struct vmcb_save_area *vmsa) {} 1047 #endif 1048 1049 /* vmenter.S */ 1050 1051 void __svm_sev_es_vcpu_run(struct vcpu_svm *svm, unsigned int flags, 1052 struct sev_es_save_area *hostsa); 1053 void __svm_vcpu_run(struct vcpu_svm *svm, unsigned int flags); 1054 1055 #define DEFINE_KVM_GHCB_ACCESSORS(field) \ 1056 static __always_inline u64 kvm_ghcb_get_##field(struct vcpu_svm *svm) \ 1057 { \ 1058 return READ_ONCE(svm->sev_es.ghcb->save.field); \ 1059 } \ 1060 \ 1061 static __always_inline bool kvm_ghcb_##field##_is_valid(const struct vcpu_svm *svm) \ 1062 { \ 1063 return test_bit(GHCB_BITMAP_IDX(field), \ 1064 (unsigned long *)&svm->sev_es.valid_bitmap); \ 1065 } \ 1066 \ 1067 static __always_inline u64 kvm_ghcb_get_##field##_if_valid(struct vcpu_svm *svm) \ 1068 { \ 1069 return kvm_ghcb_##field##_is_valid(svm) ? kvm_ghcb_get_##field(svm) : 0; \ 1070 } 1071 1072 DEFINE_KVM_GHCB_ACCESSORS(cpl) 1073 DEFINE_KVM_GHCB_ACCESSORS(rax) 1074 DEFINE_KVM_GHCB_ACCESSORS(rcx) 1075 DEFINE_KVM_GHCB_ACCESSORS(rdx) 1076 DEFINE_KVM_GHCB_ACCESSORS(rbx) 1077 DEFINE_KVM_GHCB_ACCESSORS(rsi) 1078 DEFINE_KVM_GHCB_ACCESSORS(sw_exit_code) 1079 DEFINE_KVM_GHCB_ACCESSORS(sw_exit_info_1) 1080 DEFINE_KVM_GHCB_ACCESSORS(sw_exit_info_2) 1081 DEFINE_KVM_GHCB_ACCESSORS(sw_scratch) 1082 DEFINE_KVM_GHCB_ACCESSORS(xcr0) 1083 DEFINE_KVM_GHCB_ACCESSORS(xss) 1084 1085 #endif 1086