xref: /linux/arch/x86/kvm/mmu/tdp_mmu.h (revision c10743a1824b9db449eb631745ed6f2d3cdf9762)
1fe5db27dSBen Gardon // SPDX-License-Identifier: GPL-2.0
2fe5db27dSBen Gardon 
3fe5db27dSBen Gardon #ifndef __KVM_X86_MMU_TDP_MMU_H
4fe5db27dSBen Gardon #define __KVM_X86_MMU_TDP_MMU_H
5fe5db27dSBen Gardon 
6fe5db27dSBen Gardon #include <linux/kvm_host.h>
7fe5db27dSBen Gardon 
802c00b3aSBen Gardon hpa_t kvm_tdp_mmu_get_vcpu_root_hpa(struct kvm_vcpu *vcpu);
902c00b3aSBen Gardon 
10ad6d6b94SJinrong Liang __must_check static inline bool kvm_tdp_mmu_get_root(struct kvm_mmu_page *root)
1176eb54e7SBen Gardon {
12fb101293SBen Gardon 	return refcount_inc_not_zero(&root->tdp_mmu_root_count);
1376eb54e7SBen Gardon }
1476eb54e7SBen Gardon 
156103bc07SBen Gardon void kvm_tdp_mmu_put_root(struct kvm *kvm, struct kvm_mmu_page *root,
166103bc07SBen Gardon 			  bool shared);
1776eb54e7SBen Gardon 
182b9663d8SSean Christopherson bool __kvm_tdp_mmu_zap_gfn_range(struct kvm *kvm, int as_id, gfn_t start,
195a324c24SSean Christopherson 				 gfn_t end, bool can_yield, bool flush);
202b9663d8SSean Christopherson static inline bool kvm_tdp_mmu_zap_gfn_range(struct kvm *kvm, int as_id,
215a324c24SSean Christopherson 					     gfn_t start, gfn_t end, bool flush)
2233a31641SSean Christopherson {
235a324c24SSean Christopherson 	return __kvm_tdp_mmu_zap_gfn_range(kvm, as_id, start, end, true, flush);
2433a31641SSean Christopherson }
2533a31641SSean Christopherson 
26*c10743a1SSean Christopherson bool kvm_tdp_mmu_zap_sp(struct kvm *kvm, struct kvm_mmu_page *sp);
27faaf05b0SBen Gardon void kvm_tdp_mmu_zap_all(struct kvm *kvm);
28b7cccd39SBen Gardon void kvm_tdp_mmu_invalidate_all_roots(struct kvm *kvm);
294c6654bdSBen Gardon void kvm_tdp_mmu_zap_invalidated_roots(struct kvm *kvm);
30bb18842eSBen Gardon 
312f6305ddSPaolo Bonzini int kvm_tdp_mmu_map(struct kvm_vcpu *vcpu, struct kvm_page_fault *fault);
32063afacdSBen Gardon 
333039bcc7SSean Christopherson bool kvm_tdp_mmu_unmap_gfn_range(struct kvm *kvm, struct kvm_gfn_range *range,
343039bcc7SSean Christopherson 				 bool flush);
353039bcc7SSean Christopherson bool kvm_tdp_mmu_age_gfn_range(struct kvm *kvm, struct kvm_gfn_range *range);
363039bcc7SSean Christopherson bool kvm_tdp_mmu_test_age_gfn(struct kvm *kvm, struct kvm_gfn_range *range);
373039bcc7SSean Christopherson bool kvm_tdp_mmu_set_spte_gfn(struct kvm *kvm, struct kvm_gfn_range *range);
38a6a0b05dSBen Gardon 
39269e9552SHamza Mahfooz bool kvm_tdp_mmu_wrprot_slot(struct kvm *kvm,
40269e9552SHamza Mahfooz 			     const struct kvm_memory_slot *slot, int min_level);
41a6a0b05dSBen Gardon bool kvm_tdp_mmu_clear_dirty_slot(struct kvm *kvm,
42269e9552SHamza Mahfooz 				  const struct kvm_memory_slot *slot);
43a6a0b05dSBen Gardon void kvm_tdp_mmu_clear_dirty_pt_masked(struct kvm *kvm,
44a6a0b05dSBen Gardon 				       struct kvm_memory_slot *slot,
45a6a0b05dSBen Gardon 				       gfn_t gfn, unsigned long mask,
46a6a0b05dSBen Gardon 				       bool wrprot);
474b85c921SSean Christopherson void kvm_tdp_mmu_zap_collapsible_sptes(struct kvm *kvm,
484b85c921SSean Christopherson 				       const struct kvm_memory_slot *slot);
4946044f72SBen Gardon 
5046044f72SBen Gardon bool kvm_tdp_mmu_write_protect_gfn(struct kvm *kvm,
513ad93562SKeqian Zhu 				   struct kvm_memory_slot *slot, gfn_t gfn,
523ad93562SKeqian Zhu 				   int min_level);
5395fb5b02SBen Gardon 
54a3fe5dbdSDavid Matlack void kvm_tdp_mmu_try_split_huge_pages(struct kvm *kvm,
55a3fe5dbdSDavid Matlack 				      const struct kvm_memory_slot *slot,
56a3fe5dbdSDavid Matlack 				      gfn_t start, gfn_t end,
57cb00a70bSDavid Matlack 				      int target_level, bool shared);
58a3fe5dbdSDavid Matlack 
59c5c8c7c5SDavid Matlack static inline void kvm_tdp_mmu_walk_lockless_begin(void)
60c5c8c7c5SDavid Matlack {
61c5c8c7c5SDavid Matlack 	rcu_read_lock();
62c5c8c7c5SDavid Matlack }
63c5c8c7c5SDavid Matlack 
64c5c8c7c5SDavid Matlack static inline void kvm_tdp_mmu_walk_lockless_end(void)
65c5c8c7c5SDavid Matlack {
66c5c8c7c5SDavid Matlack 	rcu_read_unlock();
67c5c8c7c5SDavid Matlack }
68c5c8c7c5SDavid Matlack 
6939b4d43eSSean Christopherson int kvm_tdp_mmu_get_walk(struct kvm_vcpu *vcpu, u64 addr, u64 *sptes,
7039b4d43eSSean Christopherson 			 int *root_level);
716e8eb206SDavid Matlack u64 *kvm_tdp_mmu_fast_pf_get_last_sptep(struct kvm_vcpu *vcpu, u64 addr,
726e8eb206SDavid Matlack 					u64 *spte);
7339b4d43eSSean Christopherson 
74897218ffSPaolo Bonzini #ifdef CONFIG_X86_64
75d501f747SBen Gardon bool kvm_mmu_init_tdp_mmu(struct kvm *kvm);
76897218ffSPaolo Bonzini void kvm_mmu_uninit_tdp_mmu(struct kvm *kvm);
77897218ffSPaolo Bonzini static inline bool is_tdp_mmu_page(struct kvm_mmu_page *sp) { return sp->tdp_mmu_page; }
78897218ffSPaolo Bonzini 
7963c0cac9SDavid Matlack static inline bool is_tdp_mmu(struct kvm_mmu *mmu)
80897218ffSPaolo Bonzini {
81897218ffSPaolo Bonzini 	struct kvm_mmu_page *sp;
82b9e5603cSPaolo Bonzini 	hpa_t hpa = mmu->root.hpa;
83897218ffSPaolo Bonzini 
84897218ffSPaolo Bonzini 	if (WARN_ON(!VALID_PAGE(hpa)))
85897218ffSPaolo Bonzini 		return false;
86897218ffSPaolo Bonzini 
876c6e166bSSean Christopherson 	/*
886c6e166bSSean Christopherson 	 * A NULL shadow page is legal when shadowing a non-paging guest with
896c6e166bSSean Christopherson 	 * PAE paging, as the MMU will be direct with root_hpa pointing at the
906c6e166bSSean Christopherson 	 * pae_root page, not a shadow page.
916c6e166bSSean Christopherson 	 */
92897218ffSPaolo Bonzini 	sp = to_shadow_page(hpa);
936c6e166bSSean Christopherson 	return sp && is_tdp_mmu_page(sp) && sp->root_count;
94897218ffSPaolo Bonzini }
95c62efff2SPaolo Bonzini #else
96c62efff2SPaolo Bonzini static inline bool kvm_mmu_init_tdp_mmu(struct kvm *kvm) { return false; }
97c62efff2SPaolo Bonzini static inline void kvm_mmu_uninit_tdp_mmu(struct kvm *kvm) {}
98c62efff2SPaolo Bonzini static inline bool is_tdp_mmu_page(struct kvm_mmu_page *sp) { return false; }
9963c0cac9SDavid Matlack static inline bool is_tdp_mmu(struct kvm_mmu *mmu) { return false; }
100c62efff2SPaolo Bonzini #endif
101897218ffSPaolo Bonzini 
102fe5db27dSBen Gardon #endif /* __KVM_X86_MMU_TDP_MMU_H */
103