Skip to content

Commit 949019b

Browse files
sean-jcbonzini
authored andcommitted
KVM: x86/mmu: Track shadow MMIO value on a per-VM basis
TDX will use a different shadow PTE entry value for MMIO from VMX. Add a member to kvm_arch and track value for MMIO per-VM instead of a global variable. By using the per-VM EPT entry value for MMIO, the existing VMX logic is kept working. Introduce a separate setter function so that guest TD can use a different value later. Signed-off-by: Sean Christopherson <[email protected]> Signed-off-by: Isaku Yamahata <[email protected]> Message-Id: <229a18434e5d83f45b1fcd7bf1544d79db1becb6.1705965635.git.isaku.yamahata@intel.com> Reviewed-by: Xiaoyao Li <[email protected]> Reviewed-by: Binbin Wu <[email protected]> Signed-off-by: Paolo Bonzini <[email protected]>
1 parent 7fa5e29 commit 949019b

File tree

5 files changed

+13
-10
lines changed

5 files changed

+13
-10
lines changed

arch/x86/include/asm/kvm_host.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1313,6 +1313,8 @@ struct kvm_arch {
13131313
*/
13141314
spinlock_t mmu_unsync_pages_lock;
13151315

1316+
u64 shadow_mmio_value;
1317+
13161318
struct iommu_domain *iommu_domain;
13171319
bool iommu_noncoherent;
13181320
#define __KVM_HAVE_ARCH_NONCOHERENT_DMA

arch/x86/kvm/mmu/mmu.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2462,7 +2462,7 @@ static int mmu_page_zap_pte(struct kvm *kvm, struct kvm_mmu_page *sp,
24622462
return kvm_mmu_prepare_zap_page(kvm, child,
24632463
invalid_list);
24642464
}
2465-
} else if (is_mmio_spte(pte)) {
2465+
} else if (is_mmio_spte(kvm, pte)) {
24662466
mmu_spte_clear_no_track(spte);
24672467
}
24682468
return 0;
@@ -4144,7 +4144,7 @@ static int handle_mmio_page_fault(struct kvm_vcpu *vcpu, u64 addr, bool direct)
41444144
if (WARN_ON_ONCE(reserved))
41454145
return -EINVAL;
41464146

4147-
if (is_mmio_spte(spte)) {
4147+
if (is_mmio_spte(vcpu->kvm, spte)) {
41484148
gfn_t gfn = get_mmio_spte_gfn(spte);
41494149
unsigned int access = get_mmio_spte_access(spte);
41504150

@@ -4760,7 +4760,7 @@ EXPORT_SYMBOL_GPL(kvm_mmu_new_pgd);
47604760
static bool sync_mmio_spte(struct kvm_vcpu *vcpu, u64 *sptep, gfn_t gfn,
47614761
unsigned int access)
47624762
{
4763-
if (unlikely(is_mmio_spte(*sptep))) {
4763+
if (unlikely(is_mmio_spte(vcpu->kvm, *sptep))) {
47644764
if (gfn != get_mmio_spte_gfn(*sptep)) {
47654765
mmu_spte_clear_no_track(sptep);
47664766
return true;
@@ -6267,6 +6267,7 @@ static bool kvm_has_zapped_obsolete_pages(struct kvm *kvm)
62676267

62686268
void kvm_mmu_init_vm(struct kvm *kvm)
62696269
{
6270+
kvm->arch.shadow_mmio_value = shadow_mmio_value;
62706271
INIT_LIST_HEAD(&kvm->arch.active_mmu_pages);
62716272
INIT_LIST_HEAD(&kvm->arch.zapped_obsolete_pages);
62726273
INIT_LIST_HEAD(&kvm->arch.possible_nx_huge_pages);

arch/x86/kvm/mmu/spte.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,10 +74,10 @@ u64 make_mmio_spte(struct kvm_vcpu *vcpu, u64 gfn, unsigned int access)
7474
u64 spte = generation_mmio_spte_mask(gen);
7575
u64 gpa = gfn << PAGE_SHIFT;
7676

77-
WARN_ON_ONCE(!shadow_mmio_value);
77+
WARN_ON_ONCE(!vcpu->kvm->arch.shadow_mmio_value);
7878

7979
access &= shadow_mmio_access_mask;
80-
spte |= shadow_mmio_value | access;
80+
spte |= vcpu->kvm->arch.shadow_mmio_value | access;
8181
spte |= gpa | shadow_nonpresent_or_rsvd_mask;
8282
spte |= (gpa & shadow_nonpresent_or_rsvd_mask)
8383
<< SHADOW_NONPRESENT_OR_RSVD_MASK_LEN;

arch/x86/kvm/mmu/spte.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -265,9 +265,9 @@ static inline struct kvm_mmu_page *root_to_sp(hpa_t root)
265265
return spte_to_child_sp(root);
266266
}
267267

268-
static inline bool is_mmio_spte(u64 spte)
268+
static inline bool is_mmio_spte(struct kvm *kvm, u64 spte)
269269
{
270-
return (spte & shadow_mmio_mask) == shadow_mmio_value &&
270+
return (spte & shadow_mmio_mask) == kvm->arch.shadow_mmio_value &&
271271
likely(enable_mmio_caching);
272272
}
273273

arch/x86/kvm/mmu/tdp_mmu.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -495,8 +495,8 @@ static void handle_changed_spte(struct kvm *kvm, int as_id, gfn_t gfn,
495495
* impact the guest since both the former and current SPTEs
496496
* are nonpresent.
497497
*/
498-
if (WARN_ON_ONCE(!is_mmio_spte(old_spte) &&
499-
!is_mmio_spte(new_spte) &&
498+
if (WARN_ON_ONCE(!is_mmio_spte(kvm, old_spte) &&
499+
!is_mmio_spte(kvm, new_spte) &&
500500
!is_removed_spte(new_spte)))
501501
pr_err("Unexpected SPTE change! Nonpresent SPTEs\n"
502502
"should not be replaced with another,\n"
@@ -1028,7 +1028,7 @@ static int tdp_mmu_map_handle_target_level(struct kvm_vcpu *vcpu,
10281028
}
10291029

10301030
/* If a MMIO SPTE is installed, the MMIO will need to be emulated. */
1031-
if (unlikely(is_mmio_spte(new_spte))) {
1031+
if (unlikely(is_mmio_spte(vcpu->kvm, new_spte))) {
10321032
vcpu->stat.pf_mmio_spte_created++;
10331033
trace_mark_mmio_spte(rcu_dereference(iter->sptep), iter->gfn,
10341034
new_spte);

0 commit comments

Comments
 (0)