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